diff --git a/.github/workflows/arm64-aes.yml b/.github/workflows/arm64-aes.yml index 250690e41..eaa012b16 100644 --- a/.github/workflows/arm64-aes.yml +++ b/.github/workflows/arm64-aes.yml @@ -13,17 +13,17 @@ permissions: jobs: aes: - runs-on: macos-15 + runs-on: macos-26 timeout-minutes: 20 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json - dotnet-version: '10.0.x' - - uses: leanprover/lean-action@24581d74d674803a25a81b10b059c5c0cdd344c6 # v1.6.2 + dotnet-version: '11.0.x' + - uses: leanprover/lean-action@ba3fdfe5c4476c925da13bf11a005ddb47e47aee # v1.7.1 with: auto-config: false use-github-cache: false @@ -42,7 +42,7 @@ jobs: - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 if: always() with: - name: arm64-aes-macos-15 + name: arm64-aes-macos-26 path: | artifacts/macos-runtime.json artifacts/AesHardware/AesHardware.lean diff --git a/.github/workflows/cache-cleanup.yml b/.github/workflows/cache-cleanup.yml index 3d8816310..d85d4073a 100644 --- a/.github/workflows/cache-cleanup.yml +++ b/.github/workflows/cache-cleanup.yml @@ -17,7 +17,7 @@ concurrency: jobs: cleanup: - runs-on: ubuntu-latest + runs-on: ubuntu-26.04 timeout-minutes: 10 steps: # Use only the trusted workflow and GitHub API; never check out PR code. diff --git a/.github/workflows/compiler-coverage.yml b/.github/workflows/compiler-coverage.yml index c7536bc94..8199e1f56 100644 --- a/.github/workflows/compiler-coverage.yml +++ b/.github/workflows/compiler-coverage.yml @@ -15,13 +15,13 @@ concurrency: jobs: latest: name: Latest stable Roslyn admission (advisory) - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 timeout-minutes: 15 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json - run: dotnet build Extractor -c Release diff --git a/.github/workflows/runtime-profiles.yml b/.github/workflows/runtime-profiles.yml index 20870d861..5ecc4ec70 100644 --- a/.github/workflows/runtime-profiles.yml +++ b/.github/workflows/runtime-profiles.yml @@ -15,18 +15,18 @@ jobs: strategy: fail-fast: false matrix: - runner: [ubuntu-24.04, ubuntu-24.04-arm] + runner: [ubuntu-26.04, ubuntu-26.04-arm] runs-on: ${{ matrix.runner }} timeout-minutes: 15 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json - dotnet-version: '10.0.x' - - uses: leanprover/lean-action@24581d74d674803a25a81b10b059c5c0cdd344c6 # v1.6.2 + dotnet-version: '11.0.x' + - uses: leanprover/lean-action@ba3fdfe5c4476c925da13bf11a005ddb47e47aee # v1.7.1 with: auto-config: false use-github-cache: false diff --git a/.github/workflows/verification.yml b/.github/workflows/verification.yml index 8b73f7cfb..4b55ab0d2 100644 --- a/.github/workflows/verification.yml +++ b/.github/workflows/verification.yml @@ -16,7 +16,7 @@ concurrency: jobs: select: name: Select verification suites - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 timeout-minutes: 5 outputs: matrix: ${{ steps.select.outputs.matrix }} @@ -27,7 +27,7 @@ jobs: with: persist-credentials: false fetch-depth: 0 - - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json - name: Test verification helpers @@ -75,7 +75,7 @@ jobs: name: Prepare certified Lean library needs: select if: needs.select.outputs.has-suites == 'true' - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 timeout-minutes: 20 permissions: contents: read @@ -102,7 +102,7 @@ jobs: persist-credentials: false # The first parent is the main commit this checkout builds on. fetch-depth: 2 - - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json - name: Install pinned elan @@ -262,7 +262,7 @@ jobs: name: Build extractor, examples and shared fixtures needs: select if: needs.select.outputs.has-suites == 'true' - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 timeout-minutes: 10 outputs: artifact-id: ${{ steps.tools-artifact.outputs.artifact-id }} @@ -275,7 +275,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json - name: Build the extractor and example assembly @@ -299,7 +299,7 @@ jobs: name: Verify (${{ matrix.suite }}) needs: [select, prepare, tools] if: needs.select.outputs.has-suites == 'true' - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 permissions: contents: read # The certified library may be an artifact of the earlier run that donated it. @@ -334,7 +334,7 @@ jobs: fetch-depth: 0 - name: Install .NET SDK - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json @@ -466,7 +466,7 @@ jobs: needs: [select, suites] if: >- ${{ !cancelled() && needs.select.outputs.cost-sources != '' && needs.select.outputs.cost-sources != '[]' }} - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 timeout-minutes: 10 permissions: contents: read @@ -483,7 +483,7 @@ jobs: with: persist-credentials: false fetch-depth: 2 - - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json - name: Download the head's rows @@ -554,13 +554,13 @@ jobs: name: Build and verify needs: [select, prepare, tools, suites] if: always() - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 timeout-minutes: 5 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json - name: Download this run's verification transcripts and timings diff --git a/CIL.lean b/CIL.lean index 8ead1c5fd..761f4e888 100644 --- a/CIL.lean +++ b/CIL.lean @@ -2,15 +2,17 @@ -- modules are tried in every consumer, so regrouping could change proof behaviour. -- Reference semantics, feature profiles, method embeddings and extraction metadata -import CIL.AggregateMemory import CIL.CodeSuffix import CIL.ExecutionLemmas import CIL.Embedding.Basic import CIL.Embedding.BaseStep import CIL.Embedding.Tactic import CIL.Features +import CIL.Identity +import CIL.ExceptionType import CIL.Imports import CIL.InstructionEquations +import CIL.Extraction.TemplateCode import CIL.Instructions import CIL.Layout import CIL.LiteralDisplay @@ -48,7 +50,6 @@ import CIL.Safety.AccessRequirements import CIL.Safety.AccessSlices import CIL.Safety.AllocationExtension import CIL.Safety.ArgumentEquivalence -import CIL.Safety.ArgumentHomeValues import CIL.Safety.AggregateHomeValues import CIL.Safety.AggregateLocals import CIL.Safety.ValueBitCasts @@ -73,6 +74,8 @@ import CIL.Safety.ConfinedSnapshots import CIL.Safety.ConstructComposition import CIL.Safety.ConstructContract import CIL.Safety.ConstructSetup +import CIL.Safety.DecodedSegment +import CIL.Safety.DecodedSummary import CIL.Safety.Equations import CIL.Safety.Execution import CIL.Safety.ExecutionTrace @@ -116,6 +119,7 @@ import CIL.Safety.OutcomeContract import CIL.Safety.RuntimeExceptions import CIL.Safety.Imports import CIL.Safety.Linking +import CIL.Safety.ProgramClosure import CIL.Safety.StepFlow import CIL.Safety.RegionRun import CIL.Safety.NegationReturn @@ -193,6 +197,9 @@ import CIL.Safety.WideMultiply import CIL.Safety.TupleDivision import CIL.Extraction.LayoutProjection import CIL.Extraction.Translation +import CIL.Extraction.DecodedMethod +import CIL.Extraction.Prefixes +import CIL.Extraction.DecodedDefinition import CIL.Safety.MemoryEquivalence import CIL.Safety.FrameEquivalence import CIL.Safety.ExecutionEquivalence @@ -203,7 +210,9 @@ import CIL.SIMD.UnspecifiedLanes import CIL.Safety.IntrinsicExecution import CIL.Safety.Segment +import CIL.Safety.RuntimeTypeSegment import CIL.Safety.SegmentLocals +import CIL.Safety.SegmentContract import CIL.Safety.LocalCalls /-! @@ -214,6 +223,11 @@ Allocation-renaming proofs are optional: import `CIL.Safety.AllocationRefinement * `CIL.Types`, `CIL.Instructions`, `CIL.Layout` and `CIL.Memory`: values, opcodes, aggregate layouts and byte memory. +* `CIL.Identity`: metadata names, user strings, assemblies, type identities, primitive types and + type kinds as values whose equality the kernel decides on codes, with `name!` and `str!` + literals. +* `CIL.ExceptionType`: the .NET exceptions the model raises, as a closed enumeration whose + members stand for distinct type identities. * `CIL.Semantics`: the shared single-instruction evaluator (`CIL.step`). Checked execution delegates value-only instructions to it; `CIL.SymbolicExecution` supplies shared simp sets. * `CIL.Features`, `CIL.SIMD.*` and `CIL.ProfileEquivalence`: hardware feature profiles, the diff --git a/CIL/AggregateMemory.lean b/CIL/AggregateMemory.lean deleted file mode 100644 index 17eb65992..000000000 --- a/CIL/AggregateMemory.lean +++ /dev/null @@ -1,123 +0,0 @@ -import CIL.Memory - -/-! -# Vector homes - -The 32-byte private homes that hold 256-bit vector locals, arguments and construction -temporaries in the reference interpreter, and the byte lemmas behind them. A home is -disjoint from caller bytes and scalar locals. General aggregate storage belongs to the -checked interpreter, in `CIL.Safety.Frames` and `CIL.Safety.InstructionMemory`. --/ - -namespace CIL - -/-- Allocation invalidates all previous bytes in the reused private home. - Unknown fields remain absent until the actual body writes them. -/ -def clearHome (memory : Memory) (frame kind index : Nat) : Memory := - fun address => match address with - | .home otherFrame otherKind otherIndex offset => - if otherFrame = frame ∧ otherKind = kind ∧ otherIndex = index ∧ offset < 32 then none - else memory address - | _ => memory address - -/-- The 256-bit vector in a home; `none` unless all 32 bytes hold `i8` values. -/ -def readVectorHome (memory : Memory) (frame kind index : Nat) : Option Value := do - return .v256 (BitVec.ofNat 256 (← readHomeBytes memory frame kind index 0 32)) - -/-- Stores a 256-bit vector little-endian in all 32 bytes of a home. -/ -def writeVectorHome (memory : Memory) (frame kind index : Nat) (bits : BitVec 256) : Memory := - writeHomeBytes memory frame kind index 0 bits.toNat 32 - -@[simp] theorem clearHome_caller (m : Memory) (frame kind index address : Nat) : - clearHome m frame kind index (.byte address) = m (.byte address) := by rfl - -@[simp] theorem writeHomeBytes_caller (m : Memory) - (frame kind index offset value count address : Nat) : - writeHomeBytes m frame kind index offset value count (.byte address) = m (.byte address) := by - induction count generalizing m offset value with - | zero => rfl - | succ count ih => simp [writeHomeBytes, ih, write] - -@[simp] theorem readVectorHome_uninitialized (m : Memory) (frame kind index : Nat) : - readVectorHome (clearHome m frame kind index) frame kind index = none := by - simp [readVectorHome, readHomeBytes, clearHome] - -theorem writeHomeBytes_outside (m : Memory) (frame kind index offset value count address : Nat) - (h : address < offset ∨ offset + count ≤ address) : - writeHomeBytes m frame kind index offset value count (.home frame kind index address) = - m (.home frame kind index address) := by - induction count generalizing m offset value with - | zero => rfl - | succ count ih => - rw [writeHomeBytes, ih _ _ _ (by omega)] - simp only [write] - split - · rename_i he - have : address = offset := by cases he; rfl - omega - · rfl - -/-- Reading back the bytes just written gives the stored number truncated to their width. -/ -theorem readHomeBytes_after_write (m : Memory) (frame kind index offset number count : Nat) : - readHomeBytes (writeHomeBytes m frame kind index offset number count) - frame kind index offset count = some (number % 256^count) := by - induction count generalizing m offset number with - | zero => simp only [readHomeBytes, Nat.pow_zero, Nat.mod_one] - | succ count ih => - rw [writeHomeBytes, readHomeBytes] - rw [writeHomeBytes_outside _ frame kind index (offset + 1) (number / 256) count offset - (Or.inl (by omega))] - simp only [write, ↓reduceIte] - rw [ih] - simp only [Nat.pow_succ] - change some (number % 256 + 256 * (number / 256 % 256^count)) = - some (number % (256^count * 256)) - congr 1 - simpa only [Nat.mod_mul_left_mod, Nat.mod_mul_left_div_self] using - Nat.mod_add_div (number % (256^count * 256)) 256 - -@[simp] theorem vector_snapshot_after_write (m : Memory) - (frame kind index : Nat) (bits : BitVec 256) : - readVectorHome (writeVectorHome m frame kind index bits) frame kind index = - some (.v256 bits) := by - simp only [readVectorHome, writeVectorHome, readHomeBytes_after_write, - show (256 : Nat)^32 = 2^256 from rfl, Nat.mod_eq_of_lt bits.isLt] - simp - -theorem readHomeBytes_write_disjoint (m : Memory) - (frame kind index writtenOffset number writtenCount offset count : Nat) - (h : offset + count ≤ writtenOffset ∨ writtenOffset + writtenCount ≤ offset) : - readHomeBytes (writeHomeBytes m frame kind index writtenOffset number writtenCount) - frame kind index offset count = readHomeBytes m frame kind index offset count := by - induction count generalizing offset with - | zero => rfl - | succ count ih => - rw [readHomeBytes, readHomeBytes, - writeHomeBytes_outside _ frame kind index writtenOffset number writtenCount offset - (by omega), - ih (offset + 1) (by omega)] - -theorem readHomeBytes_append (m : Memory) (frame kind index offset low high : Nat) : - readHomeBytes m frame kind index offset (low + high) = do - let lo ← readHomeBytes m frame kind index offset low - let hi ← readHomeBytes m frame kind index (offset + low) high - return lo + 256^low * hi := by - induction low generalizing offset with - | zero => simp [readHomeBytes] - | succ low ih => - rw [Nat.succ_add, readHomeBytes] - cases cell : m (.home frame kind index offset) with - | none => simp [cell, readHomeBytes] - | some value => - cases value <;> simp only [cell, readHomeBytes] - all_goals try rfl - case i8 lo => - rw [ih] - have offsets : offset + 1 + low = offset + (low + 1) := by omega - rw [offsets] - cases tail : readHomeBytes m frame kind index (offset + 1) low <;> - cases highBytes : readHomeBytes m frame kind index (offset + (low + 1)) high <;> - simp [Nat.pow_succ, Nat.add_mul, Nat.mul_assoc, Nat.add_assoc, - Nat.mul_comm 256] - -end CIL diff --git a/CIL/ArrayTypes.lean b/CIL/ArrayTypes.lean index ad3ecec72..fe5c2d907 100644 --- a/CIL/ArrayTypes.lean +++ b/CIL/ArrayTypes.lean @@ -54,7 +54,15 @@ def ArrayElement.allocationSupported (element : ArrayElement) : Bool := def ArrayElement.signed : ArrayElement → Bool | .signed8 | .signed16 => true | .enumeration _ storage => storage.signed - | _ => false + | .unsigned8 | .unsigned16 | .signed32 | .unsigned32 | .signed64 | .unsigned64 | .nativeInt + | .nativeUInt | .boolean | .character | .aggregate .. | .vector .. => false + +/-- Struct and vector elements, which typed element accesses move as checked snapshots rather + than as one numeric word. -/ +def ArrayElement.composite : ArrayElement → Bool + | .aggregate .. | .vector .. => true + | .signed8 | .unsigned8 | .signed16 | .unsigned16 | .signed32 | .unsigned32 | .signed64 + | .unsigned64 | .nativeInt | .nativeUInt | .boolean | .character | .enumeration .. => false /-- Native elements keep their stack kind distinct from fixed-width int64 elements. -/ def ArrayElement.native : ArrayElement → Bool diff --git a/CIL/CallSite.lean b/CIL/CallSite.lean new file mode 100644 index 000000000..b86ee5615 --- /dev/null +++ b/CIL/CallSite.lean @@ -0,0 +1,115 @@ +import CIL.Identity +import CIL.TypeExpr + +/-! +# Call sites and field references + +The decoded metadata of a call-family operand: the target method, its type arguments and the +declared signature at the site. Shared by the extraction evidence that checks call binding and +by the instruction vocabulary. A field operand is named the same way, by its owner, its index in +the owner's model, its declared type and its metadata name. +-/ + +namespace CIL.Extraction +/-- A call target: a method of this module by metadata token, or another assembly's + method by its `scope::signature` identity. -/ +inductive MethodId where + | managed (token : Nat) + | external (identity : String) + deriving DecidableEq, Repr + +/-- A method by declaring type, name and declared signature, with the type arguments of a + generic method instance; types are in the closed form the call site states. -/ +structure MethodRef where + owner : TypeExpr + name : MetadataName + hasThis : Bool + genericArity : Nat + returnType : TypeExpr + parameters : List TypeExpr + methodArguments : List TypeExpr := [] + deriving DecidableEq, Repr + +/-- The number of values a call to `method` consumes: its parameters and, for an instance + method, the receiver. -/ +def MethodRef.argumentCount (method : MethodRef) : Nat := + method.parameters.length + (if method.hasThis then 1 else 0) + +/-- A constructor reference that states only that it takes `count` parameters, for a body built in + Lean whose link names the constructor; its owner, name and types are left unstated. -/ +def MethodRef.arity (count : Nat) : MethodRef := + { owner := .void, name := ⟨0⟩, hasThis := true, genericArity := 0, returnType := .void, + parameters := List.replicate count .void } + +/-- `MethodRef.arity count` takes `count` parameters. -/ +@[simp] theorem MethodRef.arity_parameters_length (count : Nat) : + (MethodRef.arity count).parameters.length = count := List.length_replicate + +#print axioms MethodRef.arity_parameters_length + +/-- The member's signature before substitution. Owner and method variables keep their binder + and their index within it. -/ +structure OpenSignature where + convention : Nat + returnType : TypeExpr := .void + parameters : List TypeExpr := [] + deriving DecidableEq, Repr + +/-- A signature whose declared types are the types of the reference. -/ +def MethodRef.signature (method : MethodRef) (convention : Nat) : OpenSignature := + { convention, returnType := method.returnType, parameters := method.parameters } + +/-- The metadata of a `call` or `newobj` operand, as declared at the call site. -/ +structure CallSite where + method : MethodRef + declared : OpenSignature + name : String := "" + target : MethodId + typeArguments : List TypeExpr + parameterCount : Nat + returnsValue : Bool + /-- The suffix of `typeArguments` belonging to the declaring type, not the method. -/ + ownerArgumentCount : Nat := 0 + /-- The target is a static virtual MethodDef declared on an interface in a carried slice. -/ + staticVirtualInterfaceSlot : Bool := false + deriving DecidableEq, Repr + +/-- The type that declares the method a site calls. -/ +abbrev CallSite.declaringType (site : CallSite) : TypeExpr := site.method.owner + +/-- Whether the method a site calls takes a receiver. -/ +abbrev CallSite.hasThis (site : CallSite) : Bool := site.method.hasThis + +/-- The declared result type of the method a site calls. -/ +abbrev CallSite.returnType (site : CallSite) : TypeExpr := site.method.returnType + +/-- The declared parameter types of the method a site calls, receiver excluded. -/ +abbrev CallSite.parameterTypes (site : CallSite) : List TypeExpr := site.method.parameters + +/-- The method type arguments of a call: the prefix of `typeArguments` before the owner's. -/ +def siteMethodArguments (site : CallSite) : List TypeExpr := + site.typeArguments.take (site.typeArguments.length - site.ownerArgumentCount) + +/-- The number of values a call at `site` consumes: its parameters and, for an instance + method, the receiver. -/ +def CallSite.argumentCount (site : CallSite) : Nat := + site.parameterCount + (if site.hasThis then 1 else 0) + +/-- A field operand: its declaring type, its index among the owner's fields as the model counts + them, the type it holds and its `#Strings` name. The index and type are claims that metadata + binding checks against the field's row. -/ +structure FieldRef where + owner : TypeExpr + index : Nat + type : TypeExpr + name : MetadataName + deriving DecidableEq, Repr + +/-- System.Runtime's `BitConverter.IsLittleEndian`, the one static field of a type no carried + slice defines that a body may name: `ldsfld` of it is the profile's endianness, never a static + load. -/ +def littleEndianField : FieldRef := + { owner := .atom runtimeScope "System.BitConverter", index := 0, + type := .atom runtimeScope "System.Boolean", name := name!"IsLittleEndian" } + +end CIL.Extraction diff --git a/CIL/CodeSuffix.lean b/CIL/CodeSuffix.lean index 7a7724f2c..6e8c6b6c1 100644 --- a/CIL/CodeSuffix.lean +++ b/CIL/CodeSuffix.lean @@ -9,13 +9,15 @@ method would cost time quadratic in its length. The command instead names the su code at each block boundary, proves each named suffix from the one before it with `advance`, and proves each instruction from the suffix that starts its block with `lookup`. No proof then walks more than one block, and the equations cost time linear in the method's length. +The lemmas hold for any list; `cil_resolution_facts` reads parameter types, parameter homes and +layout lists through named suffixes the same way. -/ namespace CIL.CodeSuffix -variable {code suffix next : List Op} {start : Nat} +variable {α : Type} {code suffix next : List α} {start : Nat} -/-- Dropping `count` more instructions from the suffix at `start` leaves the suffix at +/-- Dropping `count` more elements from the suffix at `start` leaves the suffix at `start + count`. -/ theorem advance (count : Nat) (placed : code.drop start = suffix) (rest : suffix.drop count = next) : code.drop (start + count) = next := by @@ -24,9 +26,9 @@ theorem advance (count : Nat) (placed : code.drop start = suffix) /-- info: 'CIL.CodeSuffix.advance' depends on axioms: [propext] -/ #guard_msgs in #print axioms advance -/-- The instruction at `index` of the suffix at `start` is the code's instruction at +/-- The element at `index` of the suffix at `start` is the list's element at `start + index`. -/ -theorem lookup {op : Op} (index : Nat) (placed : code.drop start = suffix) +theorem lookup {op : α} (index : Nat) (placed : code.drop start = suffix) (found : suffix[index]? = some op) : code[start + index]? = some op := by rw [← placed, List.getElem?_drop] at found exact found diff --git a/CIL/Crypto/Sha1.lean b/CIL/Crypto/Sha1.lean new file mode 100644 index 000000000..981e51f01 --- /dev/null +++ b/CIL/Crypto/Sha1.lean @@ -0,0 +1,71 @@ +import CIL.Crypto.Words + +/-! +# SHA-1 and public key tokens + +FIPS 180-4 SHA-1 for kernel evaluation, sharing the padding and word helpers of +`CIL.Crypto`. SHA-1 appears only in the strong-name public key token of an assembly +identity: the token is the last eight bytes of the SHA-1 of the public key, reversed. +-/ + +namespace CIL.Crypto.Sha1 + +open CIL.Crypto (add xor3 choose majority blockWords padded wordsValue) + +/-- Rotation of a 32-bit word to the left. -/ +def rotl (word amount : Nat) : Nat := + Nat.lor (Nat.mod (Nat.shiftLeft word amount) 4294967296) (Nat.shiftRight word (32 - amount)) + +/-- The initial state. -/ +def initialState : List Nat := [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0] + +/-- The `count` message schedule words that follow the sixteen words of a block. -/ +def expand : Nat → List Nat → List Nat + | 0, _ => [] + | count + 1, w0 :: w1 :: w2 :: w3 :: w4 :: w5 :: w6 :: w7 :: w8 :: w9 :: w10 :: w11 :: w12 :: + w13 :: w14 :: w15 :: _ => + let next := rotl (xor3 w13 w8 (Nat.xor w2 w0)) 1 + next :: expand count [w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15, next] + | _ + 1, _ => [] + +/-- The round function and constant of round `index`. -/ +def roundFunction (index b c d : Nat) : Nat × Nat := + if index < 20 then (choose b c d, 0x5a827999) + else if index < 40 then (xor3 b c d, 0x6ed9eba1) + else if index < 60 then (majority b c d, 0x8f1bbcdc) + else (xor3 b c d, 0xca62c1d6) + +/-- The 80 rounds over the schedule words, returning the working variables. -/ +def rounds (index : Nat) : List Nat → Nat → Nat → Nat → Nat → Nat → List Nat + | w :: ws, a, b, c, d, e => + let (f, k) := roundFunction index b c d + rounds (index + 1) ws (add (add (add (rotl a 5) f) (add e k)) w) a (rotl b 30) c d + | [], a, b, c, d, e => [a, b, c, d, e] + +/-- One block applied to the state. -/ +def compress (state : List Nat) (block : Nat) : List Nat := + match state with + | [a, b, c, d, e] => + let words := blockWords block + match rounds 0 (words ++ expand 64 words) a b c d e with + | [a', b', c', d', e'] => [add a a', add b b', add c c', add d d', add e e'] + | _ => [] + | _ => [] + +/-- All blocks applied in order, evaluating each block's state once. -/ +def hashBlocks : List Nat → List Nat → List Nat + | state, [] => state + | state, block :: blocks => + let next := compress state block + bif Nat.beq (next.foldl Nat.add 0) 0 then hashBlocks next blocks else hashBlocks next blocks + +/-- The SHA-1 digest, as a number, of a byte list. -/ +def ofBytes (bytes : List Nat) : Nat := + wordsValue (hashBlocks initialState (padded (chunkValues bytes.length bytes) bytes.length)) + +/-- The strong-name token of a public key, as the number whose big-endian hex is the token's + text. -/ +def keyToken (key : List Nat) : Nat := + littleEndian 8 (ofBytes key % 2 ^ 64) + +end CIL.Crypto.Sha1 diff --git a/CIL/Crypto/Words.lean b/CIL/Crypto/Words.lean new file mode 100644 index 000000000..e4d15b7e7 --- /dev/null +++ b/CIL/Crypto/Words.lean @@ -0,0 +1,79 @@ +import Init + +/-! +# Hash words and byte blocks + +Numeric operations shared by SHA-1, SHA-256 and image reads. Byte lists are joined in balanced +groups, and hash messages are padded to 64-byte blocks. +-/ + +namespace CIL.Crypto + +/-- Addition modulo `2 ^ 32`. -/ +def add (left right : Nat) : Nat := Nat.mod (Nat.add left right) 4294967296 + +/-- Exclusive or of three words. -/ +def xor3 (a b c : Nat) : Nat := Nat.xor (Nat.xor a b) c + +/-- Selects `f` where `e` has a bit set and `g` elsewhere. -/ +def choose (e f g : Nat) : Nat := + Nat.xor (Nat.land e f) (Nat.land (Nat.sub 4294967295 e) g) + +/-- The bitwise majority of three words. -/ +def majority (a b c : Nat) : Nat := + xor3 (Nat.land a b) (Nat.land a c) (Nat.land b c) + +/-- The sixteen big-endian words of one 64-byte block. -/ +def blockWords (block : Nat) : List Nat := + [480, 448, 416, 384, 352, 320, 288, 256, 224, 192, 160, 128, 96, 64, 32, 0].map + fun shift => Nat.mod (Nat.shiftRight block shift) 4294967296 + +/-- The chunks of a message of `length` bytes with the FIPS padding: the `0x80` byte follows the + last data byte and the bit length ends the last block. -/ +def padded (chunks : List Nat) (length : Nat) : List Nat := + let tail := length % 64 + let opened := + if tail == 0 then 0x80 * 2 ^ 504 else chunks.getLastD 0 + 0x80 * 2 ^ (8 * (63 - tail)) + let full := if tail == 0 then chunks else chunks.dropLast + if tail < 56 then full ++ [opened + 8 * length] else full ++ [opened, 8 * length] + +/-- The big-endian number of a list of 32-bit words. -/ +def wordsValue (words : List Nat) : Nat := + words.foldl (fun acc word => acc * 4294967296 + word) 0 + +/-- Values with their bit widths, adjacent pairs joined into one value of the summed width. -/ +def joinPairs : List (Nat × Nat) → List (Nat × Nat) + | (high, highWidth) :: (low, lowWidth) :: rest => + (high * 2 ^ lowWidth + low, highWidth + lowWidth) :: joinPairs rest + | items => items + +/-- The big-endian concatenation of values and the sum of their widths, joined level by level; + the fuel is the item count, which halves at every level. -/ +def joinValues : Nat → List (Nat × Nat) → Nat × Nat + | 0, items => items.headD (0, 0) + | fuel + 1, items => + match items with + | [] => (0, 0) + | [single] => single + | _ => joinValues fuel (joinPairs items) + +/-- The big-endian value of a byte list. -/ +def bytesValue (bytes : List Nat) : Nat := + (joinValues bytes.length (bytes.map fun byte => (byte, 8))).1 + +/-- Reverses the order of the `count` bytes of a big-endian value. -/ +def littleEndian : Nat → Nat → Nat + | 0, _ => 0 + | count + 1, value => (value % 256) * 256 ^ count + littleEndian count (value / 256) + +/-- Big-endian chunks of a byte list, zero-padded to whole chunks. -/ +def chunkValues : Nat → List Nat → List Nat + | 0, _ => [] + | fuel + 1, bytes => + if bytes.isEmpty then [] + else + let block := bytes.take 64 + let chunk := bytesValue (block ++ List.replicate (64 - block.length) 0) + chunk :: chunkValues fuel (bytes.drop 64) + +end CIL.Crypto diff --git a/CIL/DebugNames.lean b/CIL/DebugNames.lean index 014ab20c8..348a8e954 100644 --- a/CIL/DebugNames.lean +++ b/CIL/DebugNames.lean @@ -95,7 +95,7 @@ def localSlot (method : Nat) (name : String) (pc : Option Nat) : MetaM Nat := do \n{listing named}" /-- `local_named method "name"` is the slot of a PDB-named local, and `at pc` resolves it in -the innermost scope containing `pc`, e.g. `.local (local_named root "carry" at reduce)`. -/ +the innermost scope containing `pc`, e.g. `.ldloc (local_named root "carry" at reduce)`. -/ syntax (name := localNamed) "local_named " term:max ppSpace str (" at " term:max)? : term /-- Elaborates `local_named` to the slot number as a literal. -/ diff --git a/CIL/Embedding/BaseStep.lean b/CIL/Embedding/BaseStep.lean index 805055a29..f08138f2d 100644 --- a/CIL/Embedding/BaseStep.lean +++ b/CIL/Embedding/BaseStep.lean @@ -1,5 +1,5 @@ import CIL.Embedding.Basic -import CIL.NativeEvaluation +import CIL.Semantics /-! # Relabeling value-only steps @@ -17,21 +17,22 @@ theorem option_bind_some {x : Option α} {f : α → Option β} {y : β} (h : x ∃ a, x = some a ∧ f a = some y := Option.bind_eq_some_iff.mp h /-- Relabeling `op` maps its successor when the fallthrough pc is mapped consistently. -/ -def BaseRelabel (op : Op) : Prop := +def BaseRelabel (op : Instruction) : Prop := ∀ (r : Nat → Nat) (pc q : Nat) (values : List Value) (profile : FeatureProfile) (target : Nat) (result : List Value) (memory : Memory), (op.continues = true → r (pc + 1) = q + 1) → - step op false pc [] 0 values (fun _ => none) profile = some (.next target result memory) → - step (op.relabel r) false q [] 0 values (fun _ => none) profile = + step op false none pc values (fun _ => none) profile = some (.next target result memory) → + step (op.relabel r) false none q values (fun _ => none) profile = some (.next (r target) result memory) /-- A value-only instruction computes its output independently of the program counter. -/ -def Fallthrough (op : Op) (evaluate : List Value → FeatureProfile → Option (List Value)) : Prop := - ∀ pc stack profile, step op false pc [] 0 stack (fun _ => none) profile = do +def Fallthrough (op : Instruction) (evaluate : List Value → FeatureProfile → Option (List Value)) : + Prop := + ∀ pc stack profile, step op false none pc stack (fun _ => none) profile = do return .next (pc + 1) (← evaluate stack profile) (fun _ => none) /-- A falling-through value-only instruction that relabeling leaves unchanged satisfies `BaseRelabel`. -/ -theorem base_relabel_fallthrough {op : Op} {evaluate} +theorem base_relabel_fallthrough {op : Instruction} {evaluate} (equation : Fallthrough op evaluate) (relabel : ∀ r, op.relabel r = op) (continues : op.continues = true) : BaseRelabel op := by intro r pc q values profile target result memory next h @@ -44,13 +45,15 @@ theorem base_relabel_fallthrough {op : Op} {evaluate} rfl /-- The stack after a one-operand instruction. -/ -def unaryValues (op : Op) (stack : List Value) (_ : FeatureProfile) : Option (List Value) := do +def unaryValues (op : Instruction) (stack : List Value) (_ : FeatureProfile) : + Option (List Value) := do match stack with | value :: rest => return (← unary op value) :: rest | [] => none /-- The stack after a two-operand instruction; `a` was pushed first. -/ -def binaryValues (op : Op) (stack : List Value) (_ : FeatureProfile) : Option (List Value) := do +def binaryValues (op : Instruction) (stack : List Value) (_ : FeatureProfile) : + Option (List Value) := do match stack with | b :: a :: rest => return (← binary op a b) :: rest | _ => none @@ -63,20 +66,21 @@ def comparisonValues (compare32 : W32 → W32 → Bool) (compare64 : W64 → W64 | .i64 b :: .i64 a :: rest => some (compare64 a b, rest) | _ => none -/-- Whether `brnonzero` (or, with `negate`, `brzero`) is taken, and the remaining stack. -/ +/-- Whether `brtrue` (or, with `negate`, `brfalse`) is taken, and the remaining stack. -/ def truthValues (negate : Bool) (stack : List Value) : Option (Bool × List Value) := do match stack with | value :: rest => return ((← truth value) != negate, rest) | [] => none /-- A conditional branch observes values independently of either destination. -/ -def Branching (op : Nat → Op) (evaluate : List Value → Option (Bool × List Value)) : Prop := - ∀ target pc stack profile, step (op target) false pc [] 0 stack (fun _ => none) profile = do +def Branching (op : Nat → Instruction) (evaluate : List Value → Option (Bool × List Value)) : + Prop := + ∀ target pc stack profile, step (op target) false none pc stack (fun _ => none) profile = do let (taken, rest) ← evaluate stack return .next (if taken then target else pc + 1) rest (fun _ => none) /-- A conditional branch family whose relabeling maps its target satisfies `BaseRelabel`. -/ -theorem base_relabel_branching {op : Nat → Op} {evaluate} +theorem base_relabel_branching {op : Nat → Instruction} {evaluate} (equation : Branching op evaluate) (relabel : ∀ r t, (op t).relabel r = op (r t)) (continues : ∀ t, (op t).continues = true) (t : Nat) : BaseRelabel (op t) := by intro r pc q values profile target result memory next h @@ -89,9 +93,12 @@ theorem base_relabel_branching {op : Nat → Op} {evaluate} cases taken <;> simp only [Bool.false_eq_true, ↓reduceIte, next (continues t)] <;> rfl set_option hygiene false in -/-- Proves `BaseRelabel` for a compare-branch with the given 32- and 64-bit comparisons. -/ -macro "base_relabel_comparison " compare32:term ", " compare64:term : tactic => `(tactic| ( - apply base_relabel_branching (evaluate := comparisonValues $compare32 $compare64) +/-- Proves `BaseRelabel` for the compare-branch `op` with the given 32- and 64-bit comparisons + of the stack `widenMixed` leaves, which does not depend on the branch target. -/ +macro "base_relabel_comparison " op:term:max compare32:term ", " compare64:term : tactic => + `(tactic| ( + apply base_relabel_branching + (evaluate := fun stack => comparisonValues $compare32 $compare64 (widenMixed ($op 0) stack)) · intro target pc stack profile cases stack with | nil => rfl @@ -100,7 +107,8 @@ macro "base_relabel_comparison " compare32:term ", " compare64:term : tactic => | nil => cases b <;> rfl | cons a rest => cases b <;> first | rfl | (cases a <;> - simp only [comparisonValues, bind, Option.bind, pure, decide_eq_true_eq] <;> + simp only [comparisonValues, widenMixed, nativePureBinary, bind, Option.bind, pure, + decide_eq_true_eq] <;> first | rfl | (simp only [ite_not]; rfl)) · intro r t; rfl · intro t; rfl)) @@ -119,11 +127,16 @@ macro "base_relabel_unary" : tactic => `(tactic| ( · rfl)) set_option hygiene false in -/-- Proves `BaseRelabel` for a two-operand instruction evaluated by `binary`. -/ -macro "base_relabel_binary" : tactic => `(tactic| ( - apply base_relabel_fallthrough (evaluate := binaryValues _) +/-- Proves `BaseRelabel` for the two-operand instruction `op` evaluated by `binary` on the + stack `widenMixed` leaves. -/ +macro "base_relabel_binary " op:term:max : tactic => `(tactic| ( + apply base_relabel_fallthrough + (evaluate := fun stack profile => binaryValues $op (widenMixed $op stack) profile) · intro pc stack profile - cases stack with + show _ = (binaryValues $op (widenMixed $op stack) profile >>= _) + unfold step + generalize widenMixed $op stack = widened + cases widened with | nil => rfl | cons b rest => cases rest with @@ -135,7 +148,7 @@ macro "base_relabel_binary" : tactic => `(tactic| ( · rfl)) set_option hygiene false in -/-- Proves `BaseRelabel` for `brzero` (`negate` true) or `brnonzero` (`negate` false). -/ +/-- Proves `BaseRelabel` for `brfalse` (`negate` true) or `brtrue` (`negate` false). -/ macro "base_relabel_truth " negate:term : tactic => `(tactic| ( apply base_relabel_branching (evaluate := truthValues $negate) · intro target pc stack profile @@ -153,40 +166,43 @@ theorem base_relabel_nop : BaseRelabel (.nop) := base_relabel_fallthrough (evaluate := fun stack _ => some stack) (fun _ _ _ => rfl) (fun _ => rfl) rfl theorem base_relabel_neg : BaseRelabel (.neg) := by base_relabel_unary -theorem base_relabel_gt : BaseRelabel (.gt) := by base_relabel_binary +theorem base_relabel_cgt : BaseRelabel (.cgt) := by base_relabel_binary .cgt theorem base_relabel_bgt (t : Nat) : BaseRelabel (.bgt t) := by - base_relabel_comparison (fun a b => decide (b.toInt < a.toInt)), + base_relabel_comparison .bgt (fun a b => decide (b.toInt < a.toInt)), (fun a b => decide (b.toInt < a.toInt)) theorem base_relabel_ble (t : Nat) : BaseRelabel (.ble t) := by - base_relabel_comparison (fun a b => decide (a.toInt ≤ b.toInt)), + base_relabel_comparison .ble (fun a b => decide (a.toInt ≤ b.toInt)), (fun a b => decide (a.toInt ≤ b.toInt)) -theorem base_relabel_bleu (t : Nat) : BaseRelabel (.bleu t) := by - base_relabel_comparison (fun a b => decide (a ≤ b)), (fun a b => decide (a ≤ b)) -theorem base_relabel_convI1 : BaseRelabel (.convI1) := by base_relabel_unary -theorem base_relabel_convI2 : BaseRelabel (.convI2) := by base_relabel_unary -theorem base_relabel_convU2 : BaseRelabel (.convU2) := by base_relabel_unary -theorem base_relabel_convI8 : BaseRelabel (.convI8) := by base_relabel_unary -theorem base_relabel_convI4 : BaseRelabel (.convI4) := by base_relabel_unary -theorem base_relabel_convU1 : BaseRelabel (.convU1) := by base_relabel_unary -theorem base_relabel_convU4 : BaseRelabel (.convU4) := by base_relabel_unary -theorem base_relabel_convU8 : BaseRelabel (.convU8) := by base_relabel_unary -theorem base_relabel_convU : BaseRelabel (.convU) := by base_relabel_unary -theorem base_relabel_bnot : BaseRelabel (.bnot) := by base_relabel_unary -theorem base_relabel_add : BaseRelabel (.add) := by base_relabel_binary -theorem base_relabel_sub : BaseRelabel (.sub) := by base_relabel_binary -theorem base_relabel_mul : BaseRelabel (.mul) := by base_relabel_binary -theorem base_relabel_band : BaseRelabel (.band) := by base_relabel_binary -theorem base_relabel_bor : BaseRelabel (.bor) := by base_relabel_binary -theorem base_relabel_bxor : BaseRelabel (.bxor) := by base_relabel_binary -theorem base_relabel_shl : BaseRelabel (.shl) := by base_relabel_binary -theorem base_relabel_shr : BaseRelabel (.shr) := by base_relabel_binary -theorem base_relabel_shrUn : BaseRelabel (.shrUn) := by base_relabel_binary -theorem base_relabel_lt : BaseRelabel (.lt) := by base_relabel_binary -theorem base_relabel_ltu : BaseRelabel (.ltu) := by base_relabel_binary -theorem base_relabel_gtu : BaseRelabel (.gtu) := by base_relabel_binary -theorem base_relabel_eq : BaseRelabel (.eq) := by base_relabel_binary -theorem base_relabel_brzero (t : Nat) : BaseRelabel (.brzero t) := by base_relabel_truth true -theorem base_relabel_brnonzero (t : Nat) : BaseRelabel (.brnonzero t) := by base_relabel_truth false +theorem base_relabel_ble_un (t : Nat) : BaseRelabel (.ble_un t) := by + base_relabel_comparison .ble_un (fun a b => decide (a ≤ b)), (fun a b => decide (a ≤ b)) +theorem base_relabel_conv_i1 : BaseRelabel (.conv_i1) := by base_relabel_unary +theorem base_relabel_conv_i2 : BaseRelabel (.conv_i2) := by base_relabel_unary +theorem base_relabel_conv_u2 : BaseRelabel (.conv_u2) := by base_relabel_unary +theorem base_relabel_conv_i8 : BaseRelabel (.conv_i8) := by base_relabel_unary +theorem base_relabel_conv_i4 : BaseRelabel (.conv_i4) := by base_relabel_unary +theorem base_relabel_conv_u1 : BaseRelabel (.conv_u1) := by base_relabel_unary +theorem base_relabel_conv_u4 : BaseRelabel (.conv_u4) := by base_relabel_unary +theorem base_relabel_conv_u8 : BaseRelabel (.conv_u8) := by base_relabel_unary +theorem base_relabel_conv_u : BaseRelabel (.conv_u) := by base_relabel_unary +/-- Relabeling `conv_i` maps its successor when the fallthrough pc is mapped consistently. -/ +theorem base_relabel_conv_i : BaseRelabel (.conv_i) := by base_relabel_unary +#print axioms base_relabel_conv_i +theorem base_relabel_not : BaseRelabel (.not) := by base_relabel_unary +theorem base_relabel_add : BaseRelabel (.add) := by base_relabel_binary .add +theorem base_relabel_sub : BaseRelabel (.sub) := by base_relabel_binary .sub +theorem base_relabel_mul : BaseRelabel (.mul) := by base_relabel_binary .mul +theorem base_relabel_and : BaseRelabel (.and) := by base_relabel_binary .and +theorem base_relabel_or : BaseRelabel (.or) := by base_relabel_binary .or +theorem base_relabel_xor : BaseRelabel (.xor) := by base_relabel_binary .xor +theorem base_relabel_shl : BaseRelabel (.shl) := by base_relabel_binary .shl +theorem base_relabel_shr : BaseRelabel (.shr) := by base_relabel_binary .shr +theorem base_relabel_shr_un : BaseRelabel (.shr_un) := by base_relabel_binary .shr_un +theorem base_relabel_clt : BaseRelabel (.clt) := by base_relabel_binary .clt +theorem base_relabel_clt_un : BaseRelabel (.clt_un) := by base_relabel_binary .clt_un +theorem base_relabel_cgt_un : BaseRelabel (.cgt_un) := by base_relabel_binary .cgt_un +theorem base_relabel_ceq : BaseRelabel (.ceq) := by base_relabel_binary .ceq +theorem base_relabel_brfalse (t : Nat) : BaseRelabel (.brfalse t) := by base_relabel_truth true +theorem base_relabel_brtrue (t : Nat) : BaseRelabel (.brtrue t) := by base_relabel_truth false theorem base_relabel_switch (ts : List Nat) : BaseRelabel (.switch ts) := by intro r pc q values profile target result memory next h cases values with @@ -200,49 +216,51 @@ theorem base_relabel_switch (ts : List Nat) : BaseRelabel (.switch ts) := by change some (Action.next ((ts.map r)[index.toNat]?.getD (q + 1)) rest (fun _ => none)) = _ rw [List.getElem?_map, ← next rfl] cases ts[index.toNat]? <;> rfl -theorem base_relabel_bltu (t : Nat) : BaseRelabel (.bltu t) := by - base_relabel_comparison (fun a b => decide (a < b)), (fun a b => decide (a < b)) -theorem base_relabel_bgeu (t : Nat) : BaseRelabel (.bgeu t) := by - base_relabel_comparison (fun a b => decide (b ≤ a)), (fun a b => decide (b ≤ a)) -theorem base_relabel_bgtu (t : Nat) : BaseRelabel (.bgtu t) := by - base_relabel_comparison (fun a b => decide (b < a)), (fun a b => decide (b < a)) +theorem base_relabel_blt_un (t : Nat) : BaseRelabel (.blt_un t) := by + base_relabel_comparison .blt_un (fun a b => decide (a < b)), (fun a b => decide (a < b)) +theorem base_relabel_bge_un (t : Nat) : BaseRelabel (.bge_un t) := by + base_relabel_comparison .bge_un (fun a b => decide (b ≤ a)), (fun a b => decide (b ≤ a)) +theorem base_relabel_bgt_un (t : Nat) : BaseRelabel (.bgt_un t) := by + base_relabel_comparison .bgt_un (fun a b => decide (b < a)), (fun a b => decide (b < a)) theorem base_relabel_bge (t : Nat) : BaseRelabel (.bge t) := by - base_relabel_comparison (fun a b => decide (b.toInt ≤ a.toInt)), + base_relabel_comparison .bge (fun a b => decide (b.toInt ≤ a.toInt)), (fun a b => decide (b.toInt ≤ a.toInt)) theorem base_relabel_blt (t : Nat) : BaseRelabel (.blt t) := by - base_relabel_comparison (fun a b => decide (a.toInt < b.toInt)), + base_relabel_comparison .blt (fun a b => decide (a.toInt < b.toInt)), (fun a b => decide (a.toInt < b.toInt)) theorem base_relabel_beq (t : Nat) : BaseRelabel (.beq t) := by - base_relabel_comparison (fun a b => decide (a = b)), (fun a b => decide (a = b)) -theorem base_relabel_bne (t : Nat) : BaseRelabel (.bne t) := by - base_relabel_comparison (fun a b => decide (a ≠ b)), (fun a b => decide (a ≠ b)) -theorem base_relabel_branch (t : Nat) : BaseRelabel (.branch t) := by + base_relabel_comparison .beq (fun a b => decide (a = b)), (fun a b => decide (a = b)) +theorem base_relabel_bne_un (t : Nat) : BaseRelabel (.bne_un t) := by + base_relabel_comparison .bne_un (fun a b => decide (a ≠ b)), (fun a b => decide (a ≠ b)) +theorem base_relabel_br (t : Nat) : BaseRelabel (.br t) := by intro r pc q values profile target result memory next h change some (Action.next t values (fun _ => none)) = _ at h cases h rfl -theorem base_relabel_feature (id : Feature) : BaseRelabel (.feature id) := - base_relabel_fallthrough - (evaluate := fun stack profile => some (.i32 (if profile.evaluate id then 1 else 0) :: stack)) - (fun _ _ _ => rfl) (fun _ => rfl) rfl -theorem widenNativeOperand_relabel (r : Nat → Nat) (op : Op) : - widenNativeOperand (op.relabel r) = widenNativeOperand op := by +/-- Relabeling keeps the flags of a division or remainder. -/ +theorem divideKind?_relabel (r : Nat → Nat) (op : Instruction) : + (op.relabel r).divideKind? = op.divideKind? := by cases op <;> rfl -theorem nativePureBinary_relabel (r : Nat → Nat) (op : Op) : - nativePureBinary (op.relabel r) = nativePureBinary op := by +#print axioms divideKind?_relabel + +/-- Relabeling keeps the flags of overflow-checked arithmetic. -/ +theorem checkedKind?_relabel (r : Nat → Nat) (op : Instruction) : + (op.relabel r).checkedKind? = op.checkedKind? := by cases op <;> rfl -/-- Every value-only instruction a native-width wrapper may carry satisfies `BaseRelabel`. -/ -theorem base_relabel_of_nativePure (op : Op) (pure : nativePureBinary op = true) : +#print axioms checkedKind?_relabel + +/-- Every instruction whose mixed operands `widenMixed` widens satisfies `BaseRelabel`. -/ +theorem base_relabel_of_nativePure (op : Instruction) (pure : nativePureBinary op = true) : BaseRelabel op := by cases op <;> simp only [nativePureBinary, Bool.false_eq_true] at pure all_goals first | exact base_relabel_add | exact base_relabel_sub | exact base_relabel_mul - | exact base_relabel_band | exact base_relabel_bor | exact base_relabel_bxor - | exact base_relabel_lt | exact base_relabel_ltu | exact base_relabel_gt - | exact base_relabel_gtu | exact base_relabel_eq | exact base_relabel_bgt _ + | exact base_relabel_and | exact base_relabel_or | exact base_relabel_xor + | exact base_relabel_clt | exact base_relabel_clt_un | exact base_relabel_cgt + | exact base_relabel_cgt_un | exact base_relabel_ceq | exact base_relabel_bgt _ | exact base_relabel_ble _ | exact base_relabel_bge _ | exact base_relabel_blt _ | exact base_relabel_beq _ diff --git a/CIL/Embedding/Basic.lean b/CIL/Embedding/Basic.lean index 2f13cd445..79cf59add 100644 --- a/CIL/Embedding/Basic.lean +++ b/CIL/Embedding/Basic.lean @@ -1,14 +1,15 @@ import CIL.Instructions +import CIL.Extraction.LinkIndex /-! # Method embeddings An expected method body embeds into an actual one when every live expected instruction appears in the actual body at a mapped pc, with every code target -mapped the same way. Instructions the expected body marks `.unsupported` impose +mapped the same way. Holes (`.unadmitted`) in the expected body impose nothing, so dead code may be inserted, removed or renumbered. Every condition is a list equality or Boolean test the kernel decides by evaluation; no equality -instance on `Op` is involved. +instance on `Instruction` is involved. `CIL.Safety.Embedding` proves that a successful run of the expected body is one of the actual body, so a proof about a hand-written body transfers to the extracted method. @@ -19,47 +20,48 @@ creates without allocating. namespace CIL /-- Replaces every code target of an instruction by its image under `r`. -/ -def Op.relabel (r : Nat → Nat) : Op → Op - | .nativeBinary op => .nativeBinary (op.relabel r) - | .bgt t => .bgt (r t) | .ble t => .ble (r t) | .bleu t => .bleu (r t) - | .branch t => .branch (r t) | .switch ts => .switch (ts.map r) - | .brzero t => .brzero (r t) | .brnonzero t => .brnonzero (r t) - | .bltu t => .bltu (r t) | .bgeu t => .bgeu (r t) | .bgtu t => .bgtu (r t) - | .bge t => .bge (r t) | .blt t => .blt (r t) | .beq t => .beq (r t) | .bne t => .bne (r t) +def Instruction.relabel (r : Nat → Nat) : Instruction → Instruction + | .bgt t => .bgt (r t) | .ble t => .ble (r t) | .ble_un t => .ble_un (r t) + | .br t => .br (r t) | .switch ts => .switch (ts.map r) + | .brfalse t => .brfalse (r t) | .brtrue t => .brtrue (r t) + | .blt_un t => .blt_un (r t) | .bge_un t => .bge_un (r t) | .bgt_un t => .bgt_un (r t) + | .bge t => .bge (r t) | .blt t => .blt (r t) | .beq t => .beq (r t) + | .bne_un t => .bne_un (r t) | op => op /-- Every code target of an instruction, in operand order. -/ -def Op.targets : Op → List Nat - | .nativeBinary op => op.targets - | .bgt t | .ble t | .bleu t | .branch t | .brzero t | .brnonzero t - | .bltu t | .bgeu t | .bgtu t | .bge t | .blt t | .beq t | .bne t => [t] +def Instruction.targets : Instruction → List Nat + | .bgt t | .ble t | .ble_un t | .br t | .brfalse t | .brtrue t + | .blt_un t | .bge_un t | .bgt_un t | .bge t | .blt t | .beq t | .bne_un t => [t] | .switch ts => ts | _ => [] -- Together these fail if a branch constructor is added to one of `relabel` and `targets` only. -theorem Op.targets_relabel (r : Nat → Nat) (op : Op) : +theorem Instruction.targets_relabel (r : Nat → Nat) (op : Instruction) : (op.relabel r).targets = op.targets.map r := by - induction op <;> simp_all [Op.relabel, Op.targets] + induction op <;> simp_all [Instruction.relabel, Instruction.targets] -theorem Op.relabel_eq_self (r : Nat → Nat) (op : Op) +theorem Instruction.relabel_eq_self (r : Nat → Nat) (op : Instruction) (fixed : ∀ target ∈ op.targets, r target = target) : op.relabel r = op := by - induction op <;> simp_all [Op.relabel, Op.targets] + induction op <;> simp_all [Instruction.relabel, Instruction.targets] exact (List.map_congr_left fixed).trans (List.map_id _) -theorem Op.relabel_of_targets_nil (r : Nat → Nat) (op : Op) (none : op.targets = []) : +theorem Instruction.relabel_of_targets_nil (r : Nat → Nat) (op : Instruction) + (none : op.targets = []) : op.relabel r = op := op.relabel_eq_self r (by simp [none]) /-- False only for instructions that never resume at the next pc, including after a call. -/ -def Op.continues : Op → Bool - | .managed (.call _ _ true) => false - | .branch _ | .tailCall .. | .ret _ | .throw _ | .unsupported _ => false +def Instruction.continues : Instruction → Bool + | .calli _ true => false + | .call _ prefixes => !prefixes.tail + | .br _ | .ret | .throw _ | .unadmitted _ => false | _ => true -/-- `.unsupported` faults whenever it executes, so it constrains nothing. -/ -def Op.live : Op → Bool - | .unsupported _ => false +/-- A hole (`.unadmitted`) faults whenever it executes, so it constrains nothing. -/ +def Instruction.live : Instruction → Bool + | .unadmitted _ => false | _ => true namespace Embedding @@ -76,45 +78,43 @@ structure Site where pc : Nat image : Nat next : Nat - op : Op + op : Instruction /-- Live sites of `code` numbered from `pc`, reading `map` in step; one pass over both lists. -/ -def sites (limit : Nat) : Nat → List Op → List Nat → List Site +def sites (limit : Nat) : Nat → List Instruction → List Nat → List Site | _, [], _ => [] | pc, op :: rest, map => if op.live then ⟨pc, map.headD limit, map.tail.headD limit, op⟩ :: sites limit (pc + 1) rest map.tail else sites limit (pc + 1) rest map.tail -/-- `code[q]?` for each `q`, walking `code` once while the pcs increase; `rest` is - `code.drop start`. -/ -def pick (code : List Op) : List Op → Nat → List Nat → List (Option Op) +/-- `code[q]?` for each `q`, walking `code` while the pcs do not decrease and starting again from + its head at a pc below the one before; `rest` is `code.drop start`. The pcs cost one step each + and the code's length once per descent. -/ +def pick (code : List Instruction) : List Instruction → Nat → List Nat → List (Option Instruction) | _, _, [] => [] | rest, start, q :: qs => - if start ≤ q then - let tail := rest.drop (q - start) - tail.head? :: pick code tail q qs - else code[q]? :: pick code rest start qs - -/-- The static descriptor a `staticAddress` or `staticValue` at `pc` resolves to. -/ -def staticAt (body : Method) (pc : Nat) : Option StaticDescriptor := - (body.staticSites.find? (fun site => site.1 == pc)).map Prod.snd + let tail := if start ≤ q then rest.drop (q - start) else code.drop q + tail.head? :: pick code tail q qs end Embedding open Embedding in /-- `expected` embeds into `actual` under the pc map `map`. Non-code fields agree, except that static sites may move as long as each live instruction resolves the - same descriptor and the program-wide descriptor list is unchanged. -/ + same descriptor and the program-wide descriptor list is unchanged. Link tables + move the same way: each live instruction has the link its image has. The reads walk a table + whose keys ascend, starting again from its head at each queried pc below the one before; a + table whose keys do not ascend is scanned once per query. -/ structure Embeds (expected actual : Method) (map : List Nat) : Prop where profile : actual.profile = expected.profile returnsValue : actual.returnsValue = expected.returnsValue locals : actual.locals = expected.locals localKinds : actual.localKinds = expected.localKinds - vectorLocals : actual.vectorLocals = expected.vectorLocals opaqueVectorLocals : actual.opaqueVectorLocals = expected.opaqueVectorLocals - vectorArgs : actual.vectorArgs = expected.vectorArgs parameterHomes : actual.parameterHomes = expected.parameterHomes + declared : actual.declared = expected.declared + environment : actual.environment = expected.environment descriptors : actual.staticSites.map Prod.snd = expected.staticSites.map Prod.snd entry : rename map actual.code.length 0 = 0 code : pick actual.code actual.code 0 @@ -122,8 +122,13 @@ structure Embeds (expected actual : Method) (map : List Nat) : Prop where (sites actual.code.length 0 expected.code map).map fun site => some (site.op.relabel (rename map actual.code.length)) statics : - (sites actual.code.length 0 expected.code map).map (fun site => staticAt actual site.image) = - (sites actual.code.length 0 expected.code map).map (fun site => staticAt expected site.pc) + (sites actual.code.length 0 expected.code map).map (fun site => actual.staticAt site.image) = + (sites actual.code.length 0 expected.code map).map (fun site => expected.staticAt site.pc) + links : + Extraction.linkReads actual.links + ((sites actual.code.length 0 expected.code map).map Site.image) = + Extraction.linkReads expected.links + ((sites actual.code.length 0 expected.code map).map Site.pc) flow : (sites actual.code.length 0 expected.code map).all (fun site => !site.op.continues || site.next == site.image + 1) = true diff --git a/CIL/Embedding/Tactic.lean b/CIL/Embedding/Tactic.lean index 7e431b086..2b01a90fe 100644 --- a/CIL/Embedding/Tactic.lean +++ b/CIL/Embedding/Tactic.lean @@ -3,6 +3,7 @@ import CIL.Embedding.Basic import Lean.Meta.Tactic.AuxLemma import Lean.Elab.Tactic.Simp import CIL.TermAccess +import CIL.EntryIndex /-! # The `cil_embed` tactic @@ -26,14 +27,14 @@ namespace CIL.MethodBinding open Lean Meta Elab Tactic CIL.Embedding /-- An instruction with its code targets erased. -/ -def shape (op : Op) : Op := op.relabel (fun _ => 0) +def shape (op : Instruction) : Instruction := op.relabel (fun _ => 0) /-- The instruction at `pc`, if any. -/ -private def opAt (code : Array Op) (pc : Nat) : Option Op := code[pc]? +private def opAt (code : Array Instruction) (pc : Nat) : Option Instruction := code[pc]? /-- Maps each live expected pc to an actual pc: the next pc after a fallthrough, else a pc an already aligned branch targets, else the next actual instruction of the same shape. -/ -def alignLive (expected actual : Array Op) : Array Nat := Id.run do +def alignLive (expected actual : Array Instruction) : Array Nat := Id.run do let mut map : Array (Option Nat) := Array.replicate expected.size none let mut hints : Std.HashMap Nat Nat := {} let mut cursor := 0 @@ -76,7 +77,7 @@ def alignLive (expected actual : Array Op) : Array Nat := Id.run do return full /-- An instruction for a failure message. -/ -private def showOp (op : Option Op) : String := match op with +private def showOp (op : Option Instruction) : String := match op with | some op => reprStr op | none => "(past the end)" @@ -88,17 +89,21 @@ def embedFailure (expected actual : Method) (map : List Nat) : Option (String × valueField "returnsValue" expected.returnsValue actual.returnsValue, listField "locals" expected.locals actual.locals, listField "localKinds" expected.localKinds actual.localKinds, - listField "vectorLocals" expected.vectorLocals actual.vectorLocals, listField "opaqueVectorLocals" expected.opaqueVectorLocals actual.opaqueVectorLocals, - listField "vectorArgs" expected.vectorArgs actual.vectorArgs, listField "parameterHomes" expected.parameterHomes actual.parameterHomes, - listField "static descriptors" (expected.staticSites.map Prod.snd) - (actual.staticSites.map Prod.snd) ].filterMap id + valueField "declared" expected.declared actual.declared, + environmentField expected.environment actual.environment, + (listField "static descriptors" (expected.staticSites.map Prod.snd) + (actual.staticSites.map Prod.snd)).map (· ++ staticSitesHint) ].filterMap id if let some field := fields.head? then return some (s!"non-code field differs (left: expected, right: actual)\n{field}", none) let limit := actual.code.length let r := rename map limit let actualCode := actual.code.toArray + -- One index per table, the first entry of a pc winning, so each live instruction costs a + -- logarithmic lookup. + let expectedLinks := firstEntryIndex expected.links + let actualLinks := firstEntryIndex actual.links if r 0 != 0 then return some (s!"expected pc 0 must map to actual pc 0, not {r 0}", none) let imageOf : Std.HashMap Nat Nat := (sites limit 0 expected.code map).foldl (fun table site => table.insert site.image site.pc) {} @@ -136,10 +141,14 @@ def embedFailure (expected actual : Method) (map : List Nat) : Option (String × so it must be actual pc {r prior + 1}" else "" | _, _ => "" return some (s!"{header}\n the instructions differ{entered}", some q) - if staticAt actual q != staticAt expected p then + if actual.staticAt q != expected.staticAt p then return some (s!"expected pc {p} (actual pc {q}) resolves a different static descriptor\n \ - expected: {reprStr (staticAt expected p)}\n \ - actual: {reprStr (staticAt actual q)}", some q) + expected: {reprStr (expected.staticAt p)}\n \ + actual: {reprStr (actual.staticAt q)}{staticSitesHint}", some q) + if actualLinks[q]? != expectedLinks[p]? then + return some (s!"expected pc {p} (actual pc {q}) has a different call link\n \ + expected: {reprStr expectedLinks[p]?}\n \ + actual: {reprStr actualLinks[q]?}{linksHint}", some q) if op.continues && r (p + 1) != q + 1 then return some (s!"fallthrough differs: expected pc {p} ({reprStr op}) continues at \ expected pc {p + 1}, which maps to actual pc {r (p + 1)}, but actual pc {q} continues \ @@ -193,7 +202,9 @@ private opaque evaluate (T : Type) (type value : Expr) : MetaM T syntax (name := cilEmbed) "cil_embed" (" [" term,* "]")? : tactic /-- `ctor` applied to `prefixArgs` and an `Eq.refl` of the left side of each remaining - equation field; only the kernel compares the sides. -/ + equation field; only the kernel compares the sides. The link reads of the two bodies are + compared by evaluating a decision instead: for sides that are different terms, `Eq.refl` makes + the kernel unify the unevaluated walks step by step, which costs the square of the calls. -/ private def reflFields (ctor : Name) (prefixArgs : Array Expr) : MetaM Expr := do let mut fieldsType ← instantiateForall (← inferType (mkConst ctor)) prefixArgs let mut fields : Array Expr := #[] @@ -202,13 +213,15 @@ private def reflFields (ctor : Name) (prefixArgs : Array Expr) : MetaM Expr := d throwError "cil_embed: unexpected `{ctor}` fields {fieldsType}" let some (_, lhs, _) := domain.eq? | throwError "cil_embed: unexpected `{ctor}` field {domain}" - let proof ← mkEqRefl lhs + let proof ← if lhs.getAppFn.isConstOf ``CIL.Extraction.linkReads + then mkDecideProof domain else mkEqRefl lhs fields := fields.push proof fieldsType := body.instantiate1 proof return mkAppN (mkConst ctor) (prefixArgs ++ fields) /-- Elaborates `cil_embed`: evaluates the goal's terms, aligns the bodies, reports the first - failure, or builds the `Embeds` proof from `Eq.refl` fields as an auxiliary lemma. -/ + failure, or builds the `Embeds` proof as an auxiliary lemma, from `Eq.refl` fields and a + decision of the links field. -/ @[tactic cilEmbed] def evalCilEmbed : Tactic := fun stx => focus <| withMainContext do let checkShape (type : Expr) : TacticM Unit := do unless type.isAppOfArity ``CIL.MethodEmbeds 3 || diff --git a/CIL/EntryIndex.lean b/CIL/EntryIndex.lean new file mode 100644 index 000000000..fcc6a025f --- /dev/null +++ b/CIL/EntryIndex.lean @@ -0,0 +1,35 @@ +import Std.Data.TreeMap.Lemmas + +/-! +# First-entry index + +A pc-keyed association list read many times is indexed once: lookups are logarithmic and the first +entry for a key wins, as in a list scan. The extraction checks use it for static sites and the +embedding diagnostic for link tables. Its lemma depends on a classical axiom through the tree +library, so statements that proofs unfold downstream avoid it where they can. +-/ + +namespace CIL + +/-- The entries as a tree; the first entry of the list for a key wins. -/ +def firstEntryIndex {α : Type} : List (Nat × α) → Std.TreeMap Nat α + | [] => ∅ + | (key, value) :: rest => (firstEntryIndex rest).insert key value + +/-- The index answers each key with the first entry of the list for it. -/ +theorem firstEntryIndex_get {α : Type} (entries : List (Nat × α)) (key : Nat) : + (firstEntryIndex entries)[key]? = + (entries.find? fun entry => entry.1 == key).map Prod.snd := by + induction entries with + | nil => simp [firstEntryIndex] + | cons entry entries ih => + rcases entry with ⟨index, value⟩ + simp only [firstEntryIndex, Std.TreeMap.getElem?_insert, List.find?_cons] + by_cases same : index = key + · subst index; simp + · have different : (index == key) = false := beq_eq_false_iff_ne.mpr same + simp [same, ih, different] + +#print axioms firstEntryIndex_get + +end CIL diff --git a/CIL/ExceptionType.lean b/CIL/ExceptionType.lean new file mode 100644 index 000000000..169dced85 --- /dev/null +++ b/CIL/ExceptionType.lean @@ -0,0 +1,175 @@ +import CIL.TypeExpr + +/-! +# Modelled exception types + +`ExceptionType` is the closed set of .NET exceptions the model raises: those instructions raise +and those the admitted constructors of a folded `throw` build. Each member stands for one type of +namespace `System` in `runtimeAssembly` (`ExceptionType.typeIdentity`, injective), and +`ExceptionType.ownerName` is the same type as the text a `TypeExpr` names it by, tied to its +identity in bytes by `ExceptionType.ownerName_spells`. +-/ + +namespace CIL + +open CIL.Extraction (runtimeAssembly keyBytes) + +/-- The .NET exceptions the model raises: those instructions raise and those the admitted + constructors of a folded `throw` build. -/ +inductive ExceptionType where + /-- `System.Exception`. -/ + | baseException + /-- `System.SystemException`. -/ + | systemException + /-- `System.ApplicationException`. -/ + | applicationException + /-- `System.ArithmeticException`. -/ + | arithmetic + /-- `System.DivideByZeroException`. -/ + | divideByZero + /-- `System.OverflowException`. -/ + | overflow + /-- `System.InvalidOperationException`. -/ + | invalidOperation + /-- `System.NotSupportedException`. -/ + | notSupported + /-- `System.NotImplementedException`. -/ + | notImplemented + /-- `System.ArgumentException`. -/ + | argument + /-- `System.ArgumentNullException`. -/ + | argumentNull + /-- `System.ArgumentOutOfRangeException`. -/ + | argumentOutOfRange + /-- `System.IndexOutOfRangeException`. -/ + | indexOutOfRange + /-- `System.NullReferenceException`. -/ + | nullReference + /-- `System.FormatException`. -/ + | format + /-- `System.InvalidCastException`. -/ + | invalidCast + /-- `System.ArrayTypeMismatchException`. -/ + | arrayTypeMismatch + deriving DecidableEq, Repr + +namespace ExceptionType + +/-- The name column of the member's `TypeDef` row, such as `OverflowException`. -/ +def metadataName : ExceptionType → MetadataName + | .baseException => name!"Exception" + | .systemException => name!"SystemException" + | .applicationException => name!"ApplicationException" + | .arithmetic => name!"ArithmeticException" + | .divideByZero => name!"DivideByZeroException" + | .overflow => name!"OverflowException" + | .invalidOperation => name!"InvalidOperationException" + | .notSupported => name!"NotSupportedException" + | .notImplemented => name!"NotImplementedException" + | .argument => name!"ArgumentException" + | .argumentNull => name!"ArgumentNullException" + | .argumentOutOfRange => name!"ArgumentOutOfRangeException" + | .indexOutOfRange => name!"IndexOutOfRangeException" + | .nullReference => name!"NullReferenceException" + | .format => name!"FormatException" + | .invalidCast => name!"InvalidCastException" + | .arrayTypeMismatch => name!"ArrayTypeMismatchException" + +/-- The .NET type a member stands for: its type of namespace `System` in `runtimeAssembly`. -/ +def typeIdentity (raised : ExceptionType) : TypeIdentity := + { assembly := runtimeAssembly, typeNamespace := name!"System", name := raised.metadataName } + +/-- Every member, in declaration order. -/ +def all : List ExceptionType := + [.baseException, .systemException, .applicationException, .arithmetic, .divideByZero, .overflow, + .invalidOperation, .notSupported, .notImplemented, .argument, .argumentNull, + .argumentOutOfRange, .indexOutOfRange, .nullReference, .format, .invalidCast, + .arrayTypeMismatch] + +/-- `all` lists every member. -/ +theorem mem_all (raised : ExceptionType) : raised ∈ all := by + cases raised <;> simp [all] + +#print axioms mem_all + +/-- The member that stands for `type`, or `none` for a type the model does not raise. -/ +def ofTypeIdentity? (type : TypeIdentity) : Option ExceptionType := + all.find? (·.typeIdentity == type) + +/-- A member's type identifies the member, so no two members stand for one type. -/ +theorem ofTypeIdentity?_typeIdentity (raised : ExceptionType) : + ofTypeIdentity? raised.typeIdentity = some raised := by + cases raised <;> decide +kernel + +#print axioms ofTypeIdentity?_typeIdentity + +/-- Distinct members stand for distinct .NET types. -/ +theorem typeIdentity_injective : Function.Injective typeIdentity := by + intro left right same + have found := ofTypeIdentity?_typeIdentity left + rw [same, ofTypeIdentity?_typeIdentity] at found + exact (Option.some.inj found).symm + +#print axioms typeIdentity_injective + +/-- The member's type as the full name a `TypeExpr` atom holds, such as + `System.OverflowException`. -/ +def ownerName : ExceptionType → String + | .baseException => "System.Exception" + | .systemException => "System.SystemException" + | .applicationException => "System.ApplicationException" + | .arithmetic => "System.ArithmeticException" + | .divideByZero => "System.DivideByZeroException" + | .overflow => "System.OverflowException" + | .invalidOperation => "System.InvalidOperationException" + | .notSupported => "System.NotSupportedException" + | .notImplemented => "System.NotImplementedException" + | .argument => "System.ArgumentException" + | .argumentNull => "System.ArgumentNullException" + | .argumentOutOfRange => "System.ArgumentOutOfRangeException" + | .indexOutOfRange => "System.IndexOutOfRangeException" + | .nullReference => "System.NullReferenceException" + | .format => "System.FormatException" + | .invalidCast => "System.InvalidCastException" + | .arrayTypeMismatch => "System.ArrayTypeMismatchException" + +/-- The UTF-8 bytes of `System.`, the namespace of every member and the separator before its + name. -/ +def ownerPrefix : List Nat := [83, 121, 115, 116, 101, 109, 46] + +/-- `ownerPrefix` is the bytes of the namespace `System` followed by a `.`. -/ +theorem ownerPrefix_spells : + MetadataName.ofBytes? ownerPrefix.dropLast = some name!"System" ∧ + ownerPrefix.getLast? = some '.'.toNat := by + decide +kernel + +#print axioms ownerPrefix_spells + +/-- The owner name encodes the member's non-nested type in the System namespace. -/ +theorem ownerName_spells (raised : ExceptionType) : + ((keyBytes raised.ownerName).map UInt8.toNat).take ownerPrefix.length = ownerPrefix ∧ + MetadataName.ofBytes? (((keyBytes raised.ownerName).map UInt8.toNat).drop + ownerPrefix.length) = some raised.typeIdentity.name ∧ + raised.typeIdentity.typeNamespace = name!"System" ∧ + raised.typeIdentity.enclosing = [] := by + cases raised <;> decide +kernel + +#print axioms ownerName_spells + +/-- Distinct members have distinct owner names. -/ +theorem ownerName_injective : Function.Injective ownerName := by + intro left right same + apply typeIdentity_injective + have leftName := (ownerName_spells left).2.1 + have rightName := (ownerName_spells right).2.1 + rw [same, rightName] at leftName + have names : left.metadataName = right.metadataName := (Option.some.inj leftName).symm + unfold typeIdentity + rw [names] + +#print axioms ownerName_injective + + +end ExceptionType + +end CIL diff --git a/CIL/ExecutionLemmas.lean b/CIL/ExecutionLemmas.lean index 2a14c5fc4..05eab3c42 100644 --- a/CIL/ExecutionLemmas.lean +++ b/CIL/ExecutionLemmas.lean @@ -1,22 +1,13 @@ import CIL.Semantics -/-! Exact equations for the shared feature and return evaluators. -/ +/-! The step equation for a CIL return. -/ namespace CIL -/-- A feature test pushes the profile's answer as `1` or `0`. -/ -theorem step_feature (id : Feature) (returns : Bool) (pc : Nat) (args : List Value) - (frame : Nat) (stack : List Value) (memory : Memory) (profile : FeatureProfile) : - step (.feature id) returns pc args frame stack memory profile = - some (.next (pc + 1) (.i32 (if profile.evaluate id then 1 else 0) :: stack) memory) := - rfl -#print axioms step_feature - /-- A return instruction checks the result without inspecting other instruction families. -/ theorem step_return (narrow : Option NarrowKind) (returns : Bool) (pc : Nat) - (args : List Value) (frame : Nat) (stack : List Value) (memory : Memory) - (profile : FeatureProfile) : - step (.ret narrow) returns pc args frame stack memory profile = + (stack : List Value) (memory : Memory) (profile : FeatureProfile) : + step .ret returns narrow pc stack memory profile = returnFromStack narrow returns stack memory := rfl #print axioms step_return diff --git a/CIL/Extraction/AsSpanModels.lean b/CIL/Extraction/AsSpanModels.lean new file mode 100644 index 000000000..f005ca374 --- /dev/null +++ b/CIL/Extraction/AsSpanModels.lean @@ -0,0 +1,62 @@ +import CIL.Extraction.SpanModels + +/-! +# Array overloads of MemoryExtensions.AsSpan + +The checked array element and the method's parameter types determine the writable view. +-/ + +namespace CIL.Extraction + +/-- All array overloads belong to the same nongeneric type in System.Memory. -/ +private def owner : TypeExpr := .atom spanMemoryScope "System.MemoryExtensions" + +/-- The number of int32 parameters determines which part of the array the call exposes. -/ +private def signature (range : ArraySpanRange) (element : TypeExpr) : OpenSignature := + let bounds := List.replicate (range.argumentCount - 1) (.atom runtimeScope "System.Int32") + ⟨0x10, .apply (spanHead true) element, .array element :: bounds⟩ + +/-- The external identity retains the method type variable from the original MemberRef. -/ +private def operand (range : ArraySpanRange) : ModelOperand := + let parameters := ",".intercalate ("!!0[]" :: List.replicate (range.argumentCount - 1) + "System.Int32") + let target := s!"{spanMemoryScope}::System.Span`1 System.MemoryExtensions::AsSpan" + .external (fun _ => signature range (.parameter 0)) "AsSpan" (fun _ => s!"{target}({parameters})") + +/-- Array storage excludes reference-bearing and byref-like elements from the checked view. -/ +private def operation (range : ArraySpanRange) (reader : CallLayouts) (method : MethodRef) : + Option MemoryOp := do + let [element] := method.methodArguments | none + let _ ← reader.storage (.array element) + let width ← (reader.valueShape element).size? + return .arraySpan (← arrayElementType element width) true range + +/-- Only a static generic method with this closed array signature selects the overload. -/ +private def overload (range : ArraySpanRange) : ModelOverload := + { operand := operand range + accepts := fun method => match method.methodArguments with + | [element] => !method.hasThis && method.genericArity == 1 && method.owner == owner && + method.name == name!"AsSpan" && method.signature 0x10 == signature range element + | _ => false + resolve := fun reader method => RuntimeModel.memory <$> operation range reader method } + +/-- The reserved member admits the whole-array, start-index and counted overloads. -/ +def asSpanEntries : List ModelEntry := + [{ opcode := .call, owner, name := name!"AsSpan", ownsName := true, + overloads := [.whole, .fromStart, .counted].map overload }] + +/-- Each selected array overload executes a checked memory operation. -/ +theorem asSpanEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ op, property (.memory op)) : + ∀ entry ∈ asSpanEntries, entry.Yields property := by + intro entry member reader method model found + simp only [asSpanEntries, List.mem_singleton] at member + subst entry + obtain ⟨row, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + obtain ⟨range, _, rfl⟩ := List.mem_map.mp (ModelEntry.select?_mem selected) + obtain ⟨op, _, rfl⟩ := Option.map_eq_some_iff.mp resolved + exact memory op + +#print axioms asSpanEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/BinaryModels.lean b/CIL/Extraction/BinaryModels.lean new file mode 100644 index 000000000..eeefe9d47 --- /dev/null +++ b/CIL/Extraction/BinaryModels.lean @@ -0,0 +1,142 @@ +import CIL.Extraction.ModelTable +import CIL.Extraction.SpanCalls + +/-! Runtime models for endian-aware integer reads and writes on byte spans. -/ + +namespace CIL.Extraction + +/-- A fixed integer transfer on the 64-bit target. Native integers occupy eight bytes. -/ +private structure EndianMember where + key : MetadataName + integer : String + big : Bool + read : Bool + trying : Bool + +/-- All admitted endian members have fixed, nongeneric declarations. -/ +private def endianMembers : List EndianMember := [ + ⟨name!"ReadInt16BigEndian", "Int16", true, true, false⟩, + ⟨name!"ReadInt16LittleEndian", "Int16", false, true, false⟩, + ⟨name!"TryReadInt16BigEndian", "Int16", true, true, true⟩, + ⟨name!"TryReadInt16LittleEndian", "Int16", false, true, true⟩, + ⟨name!"WriteInt16BigEndian", "Int16", true, false, false⟩, + ⟨name!"WriteInt16LittleEndian", "Int16", false, false, false⟩, + ⟨name!"TryWriteInt16BigEndian", "Int16", true, false, true⟩, + ⟨name!"TryWriteInt16LittleEndian", "Int16", false, false, true⟩, + ⟨name!"ReadUInt16BigEndian", "UInt16", true, true, false⟩, + ⟨name!"ReadUInt16LittleEndian", "UInt16", false, true, false⟩, + ⟨name!"TryReadUInt16BigEndian", "UInt16", true, true, true⟩, + ⟨name!"TryReadUInt16LittleEndian", "UInt16", false, true, true⟩, + ⟨name!"WriteUInt16BigEndian", "UInt16", true, false, false⟩, + ⟨name!"WriteUInt16LittleEndian", "UInt16", false, false, false⟩, + ⟨name!"TryWriteUInt16BigEndian", "UInt16", true, false, true⟩, + ⟨name!"TryWriteUInt16LittleEndian", "UInt16", false, false, true⟩, + ⟨name!"ReadInt32BigEndian", "Int32", true, true, false⟩, + ⟨name!"ReadInt32LittleEndian", "Int32", false, true, false⟩, + ⟨name!"TryReadInt32BigEndian", "Int32", true, true, true⟩, + ⟨name!"TryReadInt32LittleEndian", "Int32", false, true, true⟩, + ⟨name!"WriteInt32BigEndian", "Int32", true, false, false⟩, + ⟨name!"WriteInt32LittleEndian", "Int32", false, false, false⟩, + ⟨name!"TryWriteInt32BigEndian", "Int32", true, false, true⟩, + ⟨name!"TryWriteInt32LittleEndian", "Int32", false, false, true⟩, + ⟨name!"ReadUInt32BigEndian", "UInt32", true, true, false⟩, + ⟨name!"ReadUInt32LittleEndian", "UInt32", false, true, false⟩, + ⟨name!"TryReadUInt32BigEndian", "UInt32", true, true, true⟩, + ⟨name!"TryReadUInt32LittleEndian", "UInt32", false, true, true⟩, + ⟨name!"WriteUInt32BigEndian", "UInt32", true, false, false⟩, + ⟨name!"WriteUInt32LittleEndian", "UInt32", false, false, false⟩, + ⟨name!"TryWriteUInt32BigEndian", "UInt32", true, false, true⟩, + ⟨name!"TryWriteUInt32LittleEndian", "UInt32", false, false, true⟩, + ⟨name!"ReadInt64BigEndian", "Int64", true, true, false⟩, + ⟨name!"ReadInt64LittleEndian", "Int64", false, true, false⟩, + ⟨name!"TryReadInt64BigEndian", "Int64", true, true, true⟩, + ⟨name!"TryReadInt64LittleEndian", "Int64", false, true, true⟩, + ⟨name!"WriteInt64BigEndian", "Int64", true, false, false⟩, + ⟨name!"WriteInt64LittleEndian", "Int64", false, false, false⟩, + ⟨name!"TryWriteInt64BigEndian", "Int64", true, false, true⟩, + ⟨name!"TryWriteInt64LittleEndian", "Int64", false, false, true⟩, + ⟨name!"ReadUInt64BigEndian", "UInt64", true, true, false⟩, + ⟨name!"ReadUInt64LittleEndian", "UInt64", false, true, false⟩, + ⟨name!"TryReadUInt64BigEndian", "UInt64", true, true, true⟩, + ⟨name!"TryReadUInt64LittleEndian", "UInt64", false, true, true⟩, + ⟨name!"WriteUInt64BigEndian", "UInt64", true, false, false⟩, + ⟨name!"WriteUInt64LittleEndian", "UInt64", false, false, false⟩, + ⟨name!"TryWriteUInt64BigEndian", "UInt64", true, false, true⟩, + ⟨name!"TryWriteUInt64LittleEndian", "UInt64", false, false, true⟩, + ⟨name!"ReadIntPtrBigEndian", "IntPtr", true, true, false⟩, + ⟨name!"ReadIntPtrLittleEndian", "IntPtr", false, true, false⟩, + ⟨name!"TryReadIntPtrBigEndian", "IntPtr", true, true, true⟩, + ⟨name!"TryReadIntPtrLittleEndian", "IntPtr", false, true, true⟩, + ⟨name!"WriteIntPtrBigEndian", "IntPtr", true, false, false⟩, + ⟨name!"WriteIntPtrLittleEndian", "IntPtr", false, false, false⟩, + ⟨name!"TryWriteIntPtrBigEndian", "IntPtr", true, false, true⟩, + ⟨name!"TryWriteIntPtrLittleEndian", "IntPtr", false, false, true⟩, + ⟨name!"ReadUIntPtrBigEndian", "UIntPtr", true, true, false⟩, + ⟨name!"ReadUIntPtrLittleEndian", "UIntPtr", false, true, false⟩, + ⟨name!"TryReadUIntPtrBigEndian", "UIntPtr", true, true, true⟩, + ⟨name!"TryReadUIntPtrLittleEndian", "UIntPtr", false, true, true⟩, + ⟨name!"WriteUIntPtrBigEndian", "UIntPtr", true, false, false⟩, + ⟨name!"WriteUIntPtrLittleEndian", "UIntPtr", false, false, false⟩, + ⟨name!"TryWriteUIntPtrBigEndian", "UIntPtr", true, false, true⟩, + ⟨name!"TryWriteUIntPtrLittleEndian", "UIntPtr", false, false, true⟩ +] + +/-- The integer's checked primitive width fixes the number of bytes transferred. -/ +private def EndianMember.operation (member : EndianMember) (reader : CallLayouts) + (integer : TypeExpr) : Option MemoryOp := do + let .primitive width _ := reader.valueShape integer | none + if member.read then + let signed := ["Int16", "Int32", "Int64", "IntPtr"].contains member.integer + return .binaryRead width signed member.big member.trying + return .binaryWrite width member.big member.trying + +/-- The byte span, integer representation, result and external identity of this member. -/ +private def EndianMember.entry (member : EndianMember) : ModelEntry := + let owner := "System.Buffers.Binary.BinaryPrimitives" + let tryName := if member.trying then "Try" else "" + let action := if member.read then "Read" else "Write" + let order := if member.big then "BigEndian" else "LittleEndian" + let name := s!"{tryName}{action}{member.integer}{order}" + let integerName := "System." ++ member.integer + let integer := TypeExpr.atom runtimeScope integerName + let spanName := if member.read then "System.ReadOnlySpan`1" else "System.Span`1" + let byte := TypeExpr.atom runtimeScope "System.Byte" + let span := TypeExpr.apply (.constructor runtimeScope spanName) byte + let single := member.read && !member.trying + let parameters := if single then [span] + else [span, if member.read then .byref integer else integer] + let result := if member.trying then TypeExpr.atom runtimeScope "System.Boolean" + else if member.read then integer else .void + let resultName := if member.trying then "System.Boolean" else if member.read then integerName + else "System.Void" + let suffix := if single then "" else "," ++ integerName ++ (if member.read then "&" else "") + let arguments := s!"{spanName}{suffix}" + let target := s!"{spanMemoryScope}::{resultName} {owner}::{name}({arguments})" + let expected : MethodRef := + { owner := .atom spanMemoryScope owner, name := member.key, hasThis := false, + genericArity := 0, parameters, returnType := result } + { opcode := .call, owner := expected.owner, name := member.key, ownsName := true, + overloads := [ + { operand := .external (fun _ => expected.signature 0) name (fun _ => some target), + accepts := (· == expected), + resolve := fun reader _ => RuntimeModel.memory <$> member.operation reader integer }] } + +/-- The endian transfer entries of BinaryPrimitives. -/ +def binaryEntries : List ModelEntry := endianMembers.map EndianMember.entry + +/-- Every endian member executes a checked memory operation. -/ +theorem binaryEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : + ∀ entry ∈ binaryEntries, entry.Yields property := by + intro entry listed reader method model found + obtain ⟨member, _, rfl⟩ := List.mem_map.mp listed + obtain ⟨row, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + have listedRow := ModelEntry.select?_mem selected + simp only [EndianMember.entry, List.mem_singleton] at listedRow + subst row + obtain ⟨_, _, rfl⟩ := Option.map_eq_some_iff.mp resolved + exact memory _ + +#print axioms binaryEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/BinaryTransfers.lean b/CIL/Extraction/BinaryTransfers.lean deleted file mode 100644 index b3cbc9213..000000000 --- a/CIL/Extraction/BinaryTransfers.lean +++ /dev/null @@ -1,70 +0,0 @@ -import CIL.Extraction.Generics - -/-! -# Exact endian-aware integer transfers - -BinaryPrimitives span operations retain their integer width, result signedness, byte order -and failure behavior. The 16-, 32-, 64-bit and native integer overloads use the validated 64-bit target. --/ - -namespace CIL.Extraction - -/-- The semantic choice made by a fixed-width endian-aware transfer. -/ -private inductive BinaryTransfer where - | read (width : Nat) (signed bigEndian trying : Bool) - | write (width : Nat) (bigEndian trying : Bool) - deriving DecidableEq - -/-- Exact assembly identity for the endian-aware span API. -/ -private def binaryScope : String := - "System.Memory, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" - -/-- Decode a bounded overload family, retaining its complete raw signature. -/ -private def binaryCall (raw : RawInstruction) : Option BinaryTransfer := do - if raw.opcode != 40 then none else pure () - let .call site := raw.operand | none - if site.hasThis || !site.typeArguments.isEmpty || site.declaringType != - .atom binaryScope "System.Buffers.Binary.BinaryPrimitives" then none else pure () - let (read, trying, suffix) ← - if site.name.startsWith "TryRead" then some (true, true, site.name.drop 7 |>.toString) - else if site.name.startsWith "TryWrite" then some (false, true, site.name.drop 8 |>.toString) - else if site.name.startsWith "Read" then some (true, false, site.name.drop 4 |>.toString) - else if site.name.startsWith "Write" then some (false, false, site.name.drop 5 |>.toString) - else none - let bigEndian := suffix.endsWith "BigEndian" - let endian := if bigEndian then "BigEndian" else "LittleEndian" - let (name, width, signed) ← - [("Int16", 2, true), ("UInt16", 2, false), ("Int32", 4, true), - ("UInt32", 4, false), ("Int64", 8, true), ("UInt64", 8, false), - ("IntPtr", 8, true), ("UIntPtr", 8, false)].find? - (fun (name, _, _) => suffix == name ++ endian) - let integer := TypeExpr.atom runtimeScope ("System." ++ name) - let spanName := if read then "System.ReadOnlySpan`1" else "System.Span`1" - let span := TypeExpr.apply (.constructor runtimeScope spanName) (.atom runtimeScope "System.Byte") - let parameters := if read && !trying then [span] - else [span, if read then .byref integer else integer] - let result := if trying then TypeExpr.atom runtimeScope "System.Boolean" - else if read then integer else .void - let resultName := if trying then "System.Boolean" else if read then "System." ++ name - else "System.Void" - let parameterNames := spanName ++ "" ++ - (if read && !trying then "" else ",System." ++ name ++ (if read then "&" else "")) - if site.parameterTypes != parameters || site.parameterCount != parameters.length || - site.returnType != result || site.returnsValue != (read || trying) || - site.target != .external (binaryScope ++ "::" ++ resultName ++ - " System.Buffers.Binary.BinaryPrimitives::" ++ site.name ++ "(" ++ parameterNames ++ ")") - then none - else if read then some (.read width signed bigEndian trying) - else some (.write width bigEndian trying) - -/-- Covered transfers cannot be forged from another call with the same stack effect. -/ -def binaryTransferMatches (raw : RawInstruction) (emitted : Op) : Bool := - let expected := binaryCall raw - let actual := match emitted with - | .memory (.binaryRead width signed bigEndian trying) => - some (.read width signed bigEndian trying) - | .memory (.binaryWrite width bigEndian trying) => some (.write width bigEndian trying) - | _ => none - expected == actual - -end CIL.Extraction diff --git a/CIL/Extraction/BitModels.lean b/CIL/Extraction/BitModels.lean new file mode 100644 index 000000000..a3859d32d --- /dev/null +++ b/CIL/Extraction/BitModels.lean @@ -0,0 +1,146 @@ +import CIL.Extraction.IntegerMath +import CIL.Extraction.SpanCalls + +/-! +# Bit and byte-order helper models + +`BitOperations.IsPow2`, `PopCount`, `LeadingZeroCount`, `TrailingZeroCount`, `Log2`, +`RoundUpToPowerOf2`, `RotateLeft` and `RotateRight`, the same helpers each integer type declares, +and `BinaryPrimitives.ReverseEndianness` are modelled by the scalar intrinsic of that operation +and width. Each entry covers one owner and name. Its resolve gives a model only to a static, +non-generic reference that takes one runtime integer type, followed by `System.Int32` for a +rotation, and returns the result its owner declares for that type: `System.Int32` for a count +of `BitOperations`, the integer type for a count the type declares, and `System.Boolean` for +`IsPow2`. +-/ + +namespace CIL.Extraction + +/-- The modelled bit and byte-order helpers. -/ +inductive BitHelper where + | isPow2 | popCount | leadingZeroCount | trailingZeroCount | log2 | roundUpToPowerOf2 + | rotateLeft | rotateRight | reverseEndianness + +/-- Where a helper is declared: `System.Numerics.BitOperations`, the integer type it takes, or + `System.Buffers.Binary.BinaryPrimitives`. -/ +inductive BitOwner where + | bitOperations | integer | binaryPrimitives + +/-- `System.Boolean`, the result of `IsPow2`. -/ +def booleanType : TypeExpr := .atom runtimeScope "System.Boolean" + +/-- `System.Int32`, a rotation's count and the result of a count of `BitOperations`. -/ +def int32Type : TypeExpr := .atom runtimeScope "System.Int32" + +/-- The result `owner` declares for `helper` on the integer `type` of `width` bits and + `signed`ness, with the intrinsic the helper runs as; `none` where the owner declares no + such helper on the type or it has no model. A count the type declares returns the type, as + a 64-bit word at 64 bits and a 32-bit word otherwise; `Log2` of a signed type throws on a + negative value and has no model, nor does a rotation of a signed 8- or 16-bit type. -/ +def bitHelperModel (owner : BitOwner) (helper : BitHelper) (type : TypeExpr) (width : Nat) + (signed : Bool) : Option (TypeExpr × ScalarIntrinsic) := + let wide := width == 32 || width == 64 + let count := if width == 64 then ScalarResultWidth.word64 else .word32 + match owner, helper with + | .bitOperations, .isPow2 => if wide then some (booleanType, .isPow2 width signed) else none + | .bitOperations, .popCount => if wide && !signed then some (int32Type, .popCount width .word32) + else none + | .bitOperations, .leadingZeroCount => if wide && !signed then + some (int32Type, .leadingZeroCount width .word32) else none + | .bitOperations, .trailingZeroCount => if wide && !signed then + some (int32Type, .trailingZeroCount width .word32) else none + | .bitOperations, .log2 => if wide && !signed then some (int32Type, .log2 width .word32) + else none + | .bitOperations, .roundUpToPowerOf2 => if wide && !signed then + some (type, .roundUpToPowerOf2 width) else none + | .bitOperations, .rotateLeft => if wide && !signed then some (type, .rotateLeft width) + else none + | .bitOperations, .rotateRight => if wide && !signed then some (type, .rotateRight width) + else none + | .integer, .isPow2 => some (booleanType, .isPow2 width signed) + | .integer, .popCount => some (type, .popCount width count) + | .integer, .leadingZeroCount => some (type, .leadingZeroCount width count) + | .integer, .trailingZeroCount => some (type, .trailingZeroCount width count) + | .integer, .log2 => if signed then none else some (type, .log2 width count) + | .integer, .rotateLeft => if wide || !signed then some (type, .rotateLeft width) else none + | .integer, .rotateRight => if wide || !signed then some (type, .rotateRight width) else none + | .binaryPrimitives, .reverseEndianness => some (type, .reverseEndianness width signed) + | _, _ => none + +/-- The operation on the given parameter and result types. A rotation takes an additional + `System.Int32` count. A primitive owner fixes the operand type. -/ +def bitOperation (owner : BitOwner) (helper : BitHelper) (fixed : Option TypeExpr) + (returned : TypeExpr) (parameters : List TypeExpr) : Option ScalarIntrinsic := do + let type ← parameters.head? + let rotation := match helper with | .rotateLeft | .rotateRight => true | _ => false + if parameters != (if rotation then [type, int32Type] else [type]) then none + if fixed.any (· != type) then none + let .atom scope name := type | none + if scope != runtimeScope then none + let (width, signed) ← mathIntegers.lookup name + let (result, operation) ← bitHelperModel owner helper type width signed + if returned != result then none + some operation + +/-- The model of a call of `method` as `helper` declared by `owner`: the scalar intrinsic + `bitOperation` gives, on the call's arguments. -/ +def bitModel (owner : BitOwner) (helper : BitHelper) (fixed : Option TypeExpr) + (method : MethodRef) : Option RuntimeModel := do + if method.hasThis || method.genericArity != 0 || !method.methodArguments.isEmpty then none + let operation ← bitOperation owner helper fixed method.returnType method.parameters + some (.intrinsic (.scalar operation) method.parameters.length) + +/-- The helpers `BitOperations` and the integer types declare, by metadata name. -/ +def bitHelpers : List (MetadataName × BitHelper) := + [(name!"IsPow2", .isPow2), (name!"PopCount", .popCount), + (name!"LeadingZeroCount", .leadingZeroCount), (name!"TrailingZeroCount", .trailingZeroCount), + (name!"Log2", .log2), (name!"RoundUpToPowerOf2", .roundUpToPowerOf2), + (name!"RotateLeft", .rotateLeft), (name!"RotateRight", .rotateRight)] + +/-- The `call` entry of `helper` named `name` and declared by `owner` as the type `declaring`, + whose parameter must be `fixed` when given. -/ +def bitEntry (owner : BitOwner) (declaring : TypeExpr) (fixed : Option TypeExpr) + (helper : MetadataName × BitHelper) : ModelEntry := + .single .call declaring helper.1 (.declaration fun sig => sig.convention == 0 && + (bitOperation owner helper.2 fixed sig.returnType sig.parameters).isSome) + (fun _ => bitModel owner helper.2 fixed) + +/-- The helpers of `BitOperations`, those each integer type declares on itself, and + `BinaryPrimitives.ReverseEndianness`. A type has an entry only for a helper it declares + with a model. -/ +def bitEntries : List ModelEntry := + bitHelpers.map + (bitEntry .bitOperations (.atom runtimeScope "System.Numerics.BitOperations") none) ++ + mathIntegers.flatMap (fun (name, width, signed) => + let type := TypeExpr.atom runtimeScope name + (bitHelpers.filter fun helper => + (bitHelperModel .integer helper.2 type width signed).isSome).map + (bitEntry .integer type (some type))) ++ + [bitEntry .binaryPrimitives + (.atom spanMemoryScope "System.Buffers.Binary.BinaryPrimitives") none + (name!"ReverseEndianness", .reverseEndianness)] + +/-- Every model a bit helper entry resolves is an intrinsic, so a property every intrinsic model + has holds of them. -/ +theorem bitEntries_yields {property : RuntimeModel → Prop} + (intrinsic : ∀ operation argc, property (.intrinsic operation argc)) : + ∀ entry ∈ bitEntries, entry.Yields property := by + intro entry member environment method model found + have resolved : ∃ owner helper fixed, bitModel owner helper fixed method = some model := by + simp only [bitEntries, bitEntry, List.mem_append, List.mem_map, List.mem_flatMap, + List.mem_filter, List.mem_singleton] at member + rcases member with (⟨helper, _, rfl⟩ | ⟨integer, _, helper, _, rfl⟩) | rfl + · exact ⟨_, _, _, found⟩ + · exact ⟨_, _, _, found⟩ + · exact ⟨.binaryPrimitives, .reverseEndianness, none, found⟩ + rcases resolved with ⟨owner, helper, fixed, resolved⟩ + unfold bitModel at resolved + split at resolved + · contradiction + obtain ⟨operation, _, same⟩ := Option.bind_eq_some_iff.mp resolved + cases same + exact intrinsic _ _ + +#print axioms bitEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/BodyDecoding.lean b/CIL/Extraction/BodyDecoding.lean new file mode 100644 index 000000000..957d494ca --- /dev/null +++ b/CIL/Extraction/BodyDecoding.lean @@ -0,0 +1,1090 @@ +import CIL.Extraction.MetadataBinding +import CIL.Extraction.PinnedIndex +import CIL.Extraction.Prefixes +import Std.Data.TreeMap +import Std.Data.TreeMap.Lemmas + +/-! +# Method body decoding + +`decodeBody` reads the exact bytes of an ECMA-335 method body (II.25.4): the tiny or fat +header and the instruction stream (III and VI.C). `BodyDecodes` states that the result is the +`maxStack`, `initLocals`, `locals` and `code` of a `RawMethod`, so an extractor's decoded +definition cannot differ from the bytes it was read from. + +A `TokenTable` says which decoded operand each metadata token resolves to in this method. The +decoder ties every operand to the four token bytes in the body, and `MetadataBinding` checks +each table entry, the local variable types the header's signature token names and the method's +own definition against the metadata rows the body carries, and reads what it relies on about a +type no carried slice defines from the body's platform definitions, which `BodyDecodes` equates +with the extraction's one list. That the carried rows are the image's rows and the PE container +stay trusted. + +Opcodes use the numbering of `RawInstruction`: one-byte opcodes keep their byte and two-byte +opcodes `0xFE nn` are `0xFE00 + nn`. Branch operands become instruction indices, and a branch may +not enter a prefixed instruction after its first prefix, so the decoded code satisfies +`PrefixEntryFree` (`checkBody_prefixEntryFree`). Executable methods require section-free bodies. The separate +`decodeSingleCatch` reader binds a small typed exception clause for proofs about the protected +range and handler. +-/ + +namespace CIL.Extraction + +/-- How the operand that follows an opcode is encoded. Variable operands split by short or + long form and by argument or local, since the opcode decides which one is meant. -/ +inductive OperandKind where + | none + | signed8 + | unsigned8 + | signed32 + | signed64 + | float32 + | float64 + | shortArgument + | longArgument + | shortLocal + | longLocal + | shortBranch + | longBranch + | switch + | method + | field + | type + | string + | signature + | token + deriving DecidableEq, Repr + +/-- The operand encoding of every opcode in the ECMA-335 table, or `none` for a byte that is + not an opcode. Each comment names the opcodes of the row below it. -/ +def operandKind : Nat → Option OperandKind + -- nop, break, ldarg.0, ldarg.1, ldarg.2, ldarg.3 + | 0x00 | 0x01 | 0x02 | 0x03 | 0x04 | 0x05 + -- ldloc.0, ldloc.1, ldloc.2, ldloc.3, stloc.0, stloc.1 + | 0x06 | 0x07 | 0x08 | 0x09 | 0x0A | 0x0B + -- stloc.2, stloc.3, ldnull, ldc.i4.m1, ldc.i4.0, ldc.i4.1 + | 0x0C | 0x0D | 0x14 | 0x15 | 0x16 | 0x17 + -- ldc.i4.2, ldc.i4.3, ldc.i4.4, ldc.i4.5, ldc.i4.6, ldc.i4.7 + | 0x18 | 0x19 | 0x1A | 0x1B | 0x1C | 0x1D + -- ldc.i4.8, dup, pop, ret, ldind.i1, ldind.u1 + | 0x1E | 0x25 | 0x26 | 0x2A | 0x46 | 0x47 + -- ldind.i2, ldind.u2, ldind.i4, ldind.u4, ldind.i8, ldind.i + | 0x48 | 0x49 | 0x4A | 0x4B | 0x4C | 0x4D + -- ldind.r4, ldind.r8, ldind.ref, stind.ref, stind.i1, stind.i2 + | 0x4E | 0x4F | 0x50 | 0x51 | 0x52 | 0x53 + -- stind.i4, stind.i8, stind.r4, stind.r8, add, sub + | 0x54 | 0x55 | 0x56 | 0x57 | 0x58 | 0x59 + -- mul, div, div.un, rem, rem.un, and + | 0x5A | 0x5B | 0x5C | 0x5D | 0x5E | 0x5F + -- or, xor, shl, shr, shr.un, neg + | 0x60 | 0x61 | 0x62 | 0x63 | 0x64 | 0x65 + -- not, conv.i1, conv.i2, conv.i4, conv.i8, conv.r4 + | 0x66 | 0x67 | 0x68 | 0x69 | 0x6A | 0x6B + -- conv.r8, conv.u4, conv.u8, conv.r.un, throw, conv.ovf.i1.un + | 0x6C | 0x6D | 0x6E | 0x76 | 0x7A | 0x82 + -- conv.ovf.i2.un, conv.ovf.i4.un, conv.ovf.i8.un, conv.ovf.u1.un, conv.ovf.u2.un, conv.ovf.u4.un + | 0x83 | 0x84 | 0x85 | 0x86 | 0x87 | 0x88 + -- conv.ovf.u8.un, conv.ovf.i.un, conv.ovf.u.un, ldlen, ldelem.i1, ldelem.u1 + | 0x89 | 0x8A | 0x8B | 0x8E | 0x90 | 0x91 + -- ldelem.i2, ldelem.u2, ldelem.i4, ldelem.u4, ldelem.i8, ldelem.i + | 0x92 | 0x93 | 0x94 | 0x95 | 0x96 | 0x97 + -- ldelem.r4, ldelem.r8, ldelem.ref, stelem.i, stelem.i1, stelem.i2 + | 0x98 | 0x99 | 0x9A | 0x9B | 0x9C | 0x9D + -- stelem.i4, stelem.i8, stelem.r4, stelem.r8, stelem.ref, conv.ovf.i1 + | 0x9E | 0x9F | 0xA0 | 0xA1 | 0xA2 | 0xB3 + -- conv.ovf.u1, conv.ovf.i2, conv.ovf.u2, conv.ovf.i4, conv.ovf.u4, conv.ovf.i8 + | 0xB4 | 0xB5 | 0xB6 | 0xB7 | 0xB8 | 0xB9 + -- conv.ovf.u8, ckfinite, conv.u2, conv.u1, conv.i, conv.ovf.i + | 0xBA | 0xC3 | 0xD1 | 0xD2 | 0xD3 | 0xD4 + -- conv.ovf.u, add.ovf, add.ovf.un, mul.ovf, mul.ovf.un, sub.ovf + | 0xD5 | 0xD6 | 0xD7 | 0xD8 | 0xD9 | 0xDA + -- sub.ovf.un, endfinally, stind.i, conv.u, arglist, ceq + | 0xDB | 0xDC | 0xDF | 0xE0 | 0xFE00 | 0xFE01 + -- cgt, cgt.un, clt, clt.un, localloc, endfilter + | 0xFE02 | 0xFE03 | 0xFE04 | 0xFE05 | 0xFE0F | 0xFE11 + -- volatile., tail., cpblk, initblk, rethrow, refanytype + | 0xFE13 | 0xFE14 | 0xFE17 | 0xFE18 | 0xFE1A | 0xFE1D + -- readonly. + | 0xFE1E => some .none + -- ldc.i4.s + | 0x1F => some .signed8 + -- unaligned., no. + | 0xFE12 | 0xFE19 => some .unsigned8 + -- ldc.i4 + | 0x20 => some .signed32 + -- ldc.i8 + | 0x21 => some .signed64 + -- ldc.r4 + | 0x22 => some .float32 + -- ldc.r8 + | 0x23 => some .float64 + -- ldarg.s, ldarga.s, starg.s + | 0x0E | 0x0F | 0x10 => some .shortArgument + -- ldarg, ldarga, starg + | 0xFE09 | 0xFE0A | 0xFE0B => some .longArgument + -- ldloc.s, ldloca.s, stloc.s + | 0x11 | 0x12 | 0x13 => some .shortLocal + -- ldloc, ldloca, stloc + | 0xFE0C | 0xFE0D | 0xFE0E => some .longLocal + -- br.s, brfalse.s, brtrue.s, beq.s, bge.s, bgt.s + | 0x2B | 0x2C | 0x2D | 0x2E | 0x2F | 0x30 + -- ble.s, blt.s, bne.un.s, bge.un.s, bgt.un.s, ble.un.s + | 0x31 | 0x32 | 0x33 | 0x34 | 0x35 | 0x36 + -- blt.un.s, leave.s + | 0x37 | 0xDE => some .shortBranch + -- br, brfalse, brtrue, beq, bge, bgt + | 0x38 | 0x39 | 0x3A | 0x3B | 0x3C | 0x3D + -- ble, blt, bne.un, bge.un, bgt.un, ble.un + | 0x3E | 0x3F | 0x40 | 0x41 | 0x42 | 0x43 + -- blt.un, leave + | 0x44 | 0xDD => some .longBranch + -- switch + | 0x45 => some .switch + -- jmp, call, callvirt, newobj, ldftn, ldvirtftn + | 0x27 | 0x28 | 0x6F | 0x73 | 0xFE06 | 0xFE07 => some .method + -- ldfld, ldflda, stfld, ldsfld, ldsflda, stsfld + | 0x7B | 0x7C | 0x7D | 0x7E | 0x7F | 0x80 => some .field + -- cpobj, ldobj, castclass, isinst, unbox, stobj + | 0x70 | 0x71 | 0x74 | 0x75 | 0x79 | 0x81 + -- box, newarr, ldelema, ldelem.any, stelem.any, unbox.any + | 0x8C | 0x8D | 0x8F | 0xA3 | 0xA4 | 0xA5 + -- refanyval, mkrefany, initobj, constrained., sizeof + | 0xC2 | 0xC6 | 0xFE15 | 0xFE16 | 0xFE1C => some .type + -- ldstr + | 0x72 => some .string + -- calli + | 0x29 => some .signature + -- ldtoken + | 0xD0 => some .token + | _ => none + +/-- The extractor's claim of the operand each metadata token resolves to in this method. -/ +structure TokenTable where + operands : List (Nat × RawOperand) + deriving DecidableEq, Repr + +/-- The extraction's import slots and the sorted names of their published layouts. -/ +structure ImportSlotIndex where + slots : FlowIndex ImportSlot + layouts : Array (List Nat) + +/-- Index the verified slots by program position and their published layouts by full identity. -/ +def ImportSlotIndex.ofList (imports : List ImportSlot) : ImportSlotIndex := + let names := imports.flatMap fun slot => + slot.layouts.published.map fun row => bytesOf row.identity + { slots := FlowIndex.ofList imports, layouts := (Std.TreeSet.ofList names).toList.toArray } + +/-- Static call slots at their direct-link pcs and the value layouts published by the imports. -/ +structure BodyImports where + calls : List (Prod Nat ImportSlot) := [] + layouts : Array (List Nat) := #[] + deriving DecidableEq, Repr + +/-- Only direct links into the import part of the program grant a call its export identity. -/ +def ImportSlotIndex.forLinks (imports : ImportSlotIndex) (methodCount : Nat) + (links : List (Prod Nat Link)) : BodyImports := + match imports.slots with + | .empty => {} + | slots => + let calls := links.filterMap fun (pc, link) => do + let .direct target := link | none + guard (methodCount <= target) + return (pc, <- slots.get? (target - methodCount)) + { calls, layouts := imports.layouts } + +/-- Each mention of an imported method in the instruction stream is a call at its linked pc. -/ +def BodyImports.checkUses (bound : BodyImports) + (identities : Std.TreeMap (List Nat) ImportSlot) (code : List RawInstruction) : Bool := + let positions := Std.TreeMap.ofList bound.calls + code.zipIdx.all fun (instruction, pc) => + match instruction.operand, positions[pc]? with + | .call site, expected => + match site.target, expected with + | .external name, some slot => instruction.opcode == 0x28 && slot.identity == name + | .external name, none => !identities.contains (bytesOf name) + | .managed _, slot => slot.isNone + | _, slot => slot.isNone + +/-- Every use of a registered import identity has its own direct call at its linked pc. -/ +theorem BodyImports.checked_call_has_link {bound : BodyImports} + {identities : Std.TreeMap (List Nat) ImportSlot} {code : List RawInstruction} + {instruction : RawInstruction} {pc : Nat} {site : CallSite} {identity : String} + (checked : bound.checkUses identities code = true) + (atPc : Membership.mem code.zipIdx (instruction, pc)) + (operand : instruction.operand = .call site) + (target : site.target = .external identity) + (registered : identities.contains (bytesOf identity) = true) : + Exists fun slot => (Std.TreeMap.ofList bound.calls)[pc]? = some slot /\ + instruction.opcode = 0x28 /\ slot.identity = identity := by + unfold BodyImports.checkUses at checked + have accepted := List.all_eq_true.mp checked (instruction, pc) atPc + cases entry : (Std.TreeMap.ofList bound.calls)[pc]? with + | none => simp [operand, target, entry, registered] at accepted + | some slot => + refine Exists.intro slot (And.intro rfl ?_) + simpa [operand, target, entry] using accepted + +#print axioms BodyImports.checked_call_has_link + +/-- Method bytes, their operand table and the metadata used to resolve the tokens. + Executable evidence carries the header and code. Catch inspection also takes the section. -/ +structure BodyEvidence where + bytes : List Nat + tokens : TokenTable + metadata : MethodMetadata + /-- The identity each slice's assembly spells, in the slices' order; `AssembliesIdentified` + ties them to the assemblies' bytes once per assembly. -/ + identities : List (List Nat) + /-- `MetadataSlice.tables` of each slice of `metadata`, built once for the extraction. -/ + tables : List IndexTables + /-- The owner and base class names the method's definition resolves to. -/ + names : MethodNames + /-- The identity of every assembly the extraction pins, as bytes; `BodyDecodes` ties them to + the pins. A reference into one of them needs its slice among `metadata`. -/ + pinned : PinnedIndex + /-- The platform definitions the checks read a type outside the carried slices from; + `BodyDecodes` equates them with the extraction's. -/ + platform : PlatformTable := {} + /-- The direct import links and published layout names bound by the translation certificate. -/ + imports : BodyImports := {} + /-- The type arguments of a generic instance, method arguments first, as the types its + signature names; empty for a method that is not an instance. -/ + instantiation : List InstanceArgument := [] + /-- The instruction index and token of each static field access and field `ldtoken` whose + field a carried `Field` row defines, in code order; the decoder records the same list. -/ + statics : List (Nat × Nat) := [] + deriving DecidableEq, Repr + +/-- What a body's bytes say about its method: the fields `RawMethod` takes from the body, and + the token of the local-variable signature the locals are checked against. -/ +structure DecodedBody where + maxStack : Nat + initLocals : Bool + localSignature : Nat + code : List RawInstruction + /-- The instruction index and token of each static field access whose field a carried row + defines, as `BodyEvidence.statics` lists them. -/ + statics : List (Nat × Nat) + deriving DecidableEq, Repr + +/-- Why a body does not decode. Offsets are byte offsets of the instruction within the code. -/ +inductive BodyError where + | invalidByte + | truncatedHeader + | truncatedOpcode (offset : Nat) + | unsupportedHeader (flags : Nat) + | extraSections + | invalidCatchSection + | codeSizeMismatch (declared supplied : Nat) + | duplicateToken (token : Nat) + | metadata (error : MetadataError) + | unknownOpcode (offset opcode : Nat) + | truncatedOperand (offset : Nat) + | missingToken (offset token : Nat) + | mismatchedToken (offset token : Nat) + | branchOutsideBody (offset : Nat) + | branchIntoInstruction (offset target : Nat) + | branchIntoPrefixed (offset target : Nat) + deriving DecidableEq, Repr + +/-- Tokens naming a method: MethodDef, MemberRef or MethodSpec. -/ +private def methodTable (table : Nat) : Bool := table == 0x06 || table == 0x0A || table == 0x2B + +/-- Tokens naming a field: Field or MemberRef. -/ +private def fieldTable (table : Nat) : Bool := table == 0x04 || table == 0x0A + +/-- Tokens naming a type: TypeRef, TypeDef or TypeSpec. -/ +private def typeTable (table : Nat) : Bool := table == 0x01 || table == 0x02 || table == 0x1B + +/-- Whether a token of metadata table `table` may supply `operand` to an opcode whose operand + kind is `kind`; a user string is table `0x70` and a standalone signature is `0x11`. -/ +def tokenAdmits (kind : OperandKind) (table : Nat) (operand : RawOperand) : Bool := + match kind, operand with + | .method, .call _ => methodTable table + | .field, .field .. => fieldTable table + | .type, .type _ => typeTable table + | .string, .text _ => table == 0x70 + | .signature, .indirectCall .. => table == 0x11 + | .token, .call _ => methodTable table + | .token, .field .. => fieldTable table + | .token, .type _ => typeTable table + | _, _ => false + +/-- The little-endian value of the next `width` bytes, and the bytes after them. -/ +private def readUnsigned (offset : Nat) : + Nat → List Nat → Except BodyError (Nat × List Nat) + | 0, rest => .ok (0, rest) + | width + 1, byte :: rest => do + let (high, tail) ← readUnsigned offset width rest + .ok (byte + 256 * high, tail) + | _ + 1, [] => .error (.truncatedOperand offset) + +/-- The two's-complement integer of `width` bytes whose unsigned value is `value`. -/ +private def signedOf (width value : Nat) : Int := + if value < 2 ^ (8 * width - 1) then (value : Int) else (value : Int) - 2 ^ (8 * width) + +/-- The absolute byte offset a branch reaches from the end of its instruction. -/ +private def branchTarget (offset following : Nat) (relative : Int) : Except BodyError Nat := + if 0 ≤ (following : Int) + relative then .ok ((following : Int) + relative).toNat + else .error (.branchOutsideBody offset) + +/-- The next `count` signed 32-bit values, and the bytes after them. -/ +private def readRelatives (offset : Nat) : + Nat → List Nat → Except BodyError (List Int × List Nat) + | 0, rest => .ok ([], rest) + | count + 1, rest => do + let (value, tail) ← readUnsigned offset 4 rest + let (values, remaining) ← readRelatives offset count tail + .ok (signedOf 4 value :: values, remaining) + +/-- An operand read as one fixed-width unsigned value and built from it. -/ +private def readValue (offset start width : Nat) (build : Nat → RawOperand) + (rest : List Nat) : + Except BodyError (RawOperand × Nat × List Nat) := do + let (value, tail) ← readUnsigned offset width rest + .ok (build value, start + width, tail) + +/-- A branch operand of `width` bytes; the target stays an absolute byte offset until the + instruction offsets are known. -/ +private def readBranch (offset start width : Nat) (rest : List Nat) : + Except BodyError (RawOperand × Nat × List Nat) := do + let (value, tail) ← readUnsigned offset width rest + let target ← branchTarget offset (start + width) (signedOf width value) + .ok (.target target, start + width, tail) + +/-- A `switch` operand: its count, then one offset per case relative to the end of the table. -/ +private def readSwitch (offset start : Nat) (rest : List Nat) : + Except BodyError (RawOperand × Nat × List Nat) := do + let (count, afterCount) ← readUnsigned offset 4 rest + let following := start + 4 + 4 * count + let (relatives, tail) ← readRelatives offset count afterCount + let targets ← relatives.mapM (branchTarget offset following) + .ok (.targets targets, following, tail) + +/-- A token operand: the four token bytes, the operand the table gives them, and the check that + the token's metadata table suits the opcode. -/ +private def readToken (tokens : Std.TreeMap Nat (RawOperand × TokenFacts)) (offset start : Nat) + (opcode : Nat) (kind : OperandKind) (rest : List Nat) : + Except BodyError (RawOperand × Nat × List Nat) := do + let (token, tail) ← readUnsigned offset 4 rest + let some (operand, facts) := tokens[token]? | throw (.missingToken offset token) + if !tokenAdmits kind (metadataTableOf token) operand || !checkSite opcode facts operand then + throw (.mismatchedToken offset token) + .ok (operand, start + 4, tail) + +/-- Decode the operand that starts at byte offset `start`, returning it, the offset after it + and the remaining bytes. Branch operands carry absolute byte offsets. -/ +private def decodeOperand (tokens : Std.TreeMap Nat (RawOperand × TokenFacts)) + (offset start opcode : Nat) : + OperandKind → List Nat → Except BodyError (RawOperand × Nat × List Nat) + | .none, rest => .ok (.none, start, rest) + | .signed8, rest => readValue offset start 1 (fun value => .integer (signedOf 1 value)) rest + | .unsigned8, rest => readValue offset start 1 (fun value => .integer value) rest + | .signed32, rest => readValue offset start 4 (fun value => .integer (signedOf 4 value)) rest + | .signed64, rest => readValue offset start 8 (fun value => .integer (signedOf 8 value)) rest + | .float32, rest => + readValue offset start 4 (fun value => .text ("float:" ++ toString (signedOf 4 value))) rest + | .float64, rest => + readValue offset start 8 (fun value => .text ("double:" ++ toString (signedOf 8 value))) rest + | .shortArgument, rest => readValue offset start 1 .argument rest + | .longArgument, rest => readValue offset start 2 .argument rest + | .shortLocal, rest => readValue offset start 1 .local rest + | .longLocal, rest => readValue offset start 2 .local rest + | .shortBranch, rest => readBranch offset start 1 rest + | .longBranch, rest => readBranch offset start 4 rest + | .switch, rest => readSwitch offset start rest + | kind, rest => readToken tokens offset start opcode kind rest + +/-- The opcode at the front of the bytes, its width in bytes and the bytes after it. -/ +private def readOpcode (offset : Nat) : List Nat → Except BodyError (Nat × Nat × List Nat) + | [] => .error (.truncatedOpcode offset) + | 0xFE :: second :: rest => .ok (0xFE00 + second, 2, rest) + | [0xFE] => .error (.truncatedOpcode offset) + | first :: rest => .ok (first, 1, rest) + +/-- The token of a static field operand whose binding found the carried `Field` row defining + it, read again from the operand bytes; `none` for any other operand. -/ +private def boundStaticToken (tokens : Std.TreeMap Nat (RawOperand × TokenFacts)) (offset : Nat) + (operand : RawOperand) (bytes : List Nat) : Option Nat := + match operand with + | .field _ _ _ _ true => do + let (token, _) ← (readUnsigned offset 4 bytes).toOption + let (_, facts) ← tokens[token]? + if facts.staticField == some true then some token else none + | _ => none + +/-- Decode the instruction stream starting at byte `offset`, the instruction numbered `index`, + with the index and token of each bound static field operand. `fuel` is the byte count, which + bounds the instruction count since each instruction takes at least one byte. Branch operands + carry absolute byte offsets. -/ +private def decodeInstructions (tokens : Std.TreeMap Nat (RawOperand × TokenFacts)) : + Nat → Nat → Nat → List Nat → Except BodyError (List RawInstruction × List (Nat × Nat)) + | _, _, _, [] => .ok ([], []) + | 0, offset, _, _ => .error (.truncatedOperand offset) + | fuel + 1, offset, index, bytes => do + let (opcode, width, afterOpcode) ← readOpcode offset bytes + let some kind := operandKind opcode | throw (.unknownOpcode offset opcode) + let (operand, following, remaining) ← + decodeOperand tokens offset (offset + width) opcode kind afterOpcode + let (rest, statics) ← decodeInstructions tokens fuel following (index + 1) remaining + let instruction : RawInstruction := { offset := offset, opcode := opcode, operand := operand } + match boundStaticToken tokens offset operand afterOpcode with + | some token => .ok (instruction :: rest, (index, token) :: statics) + | none => .ok (instruction :: rest, statics) + +/-- Map each instruction's start offset to its index and to whether a prefix opcode precedes + it. -/ +private def indexStarts : List RawInstruction → Nat → Bool → Std.TreeMap Nat (Nat × Bool) → + Std.TreeMap Nat (Nat × Bool) + | [], _, _, starts => starts + | instruction :: rest, index, afterPrefix, starts => + indexStarts rest (index + 1) (isPrefixOpcode instruction.opcode) + (starts.insert instruction.offset (index, afterPrefix)) + +/-- Replace a branch's absolute byte offset by the index of the instruction starting there. The + target must lie inside the code, start an instruction, and not follow a prefix: control may + enter a prefixed instruction only at its first prefix (ECMA-335 III.2). -/ +private def instructionIndex (starts : Std.TreeMap Nat (Nat × Bool)) + (codeLength offset target : Nat) : Except BodyError Nat := + if codeLength ≤ target then .error (.branchOutsideBody offset) + else match starts[target]? with + | some (index, false) => .ok index + | some (_, true) => .error (.branchIntoPrefixed offset target) + | none => .error (.branchIntoInstruction offset target) + +/-- Turn the absolute branch offsets of one instruction into instruction indices. -/ +private def resolveBranches (starts : Std.TreeMap Nat (Nat × Bool)) (codeLength : Nat) + (instruction : RawInstruction) : Except BodyError RawInstruction := + match instruction.operand with + | .target target => do + let index ← instructionIndex starts codeLength instruction.offset target + .ok { instruction with operand := .target index } + | .targets targets => do + let indices ← targets.mapM (instructionIndex starts codeLength instruction.offset) + .ok { instruction with operand := .targets indices } + | _ => .ok instruction +/-- The fields of a method body header. -/ +structure BodyHeader where + maxStack : Nat + initLocals : Bool + codeSize : Nat + localSignature : Nat + +/-- A tiny header carries no locals and the CLR's fixed evaluation stack depth. -/ +def tinyMaxStack : Nat := 8 + +/-- Decode the tiny or fat header (II.25.4.2 and II.25.4.3), returning the bytes after it. A fat + header has three words and can set InitLocals. MoreSects must match the requested form. -/ +private def decodeHeader (withSection : Bool) : List Nat → Except BodyError (BodyHeader × List Nat) + | [] => .error .truncatedHeader + | first :: rest => + if first % 4 == 2 then + if withSection then .error .invalidCatchSection else + .ok ({ maxStack := tinyMaxStack, initLocals := false, codeSize := first / 4, + localSignature := 0 }, rest) + else if first % 4 == 3 then + match rest with + | second :: stack0 :: stack1 :: size0 :: size1 :: size2 :: size3 :: + token0 :: token1 :: token2 :: token3 :: code => + let flags := first + 256 * (second % 16) + if second / 16 != 3 then .error (.unsupportedHeader flags) + else if !withSection && flags / 8 % 2 == 1 then .error .extraSections + else if withSection && !(flags == 0x0B || flags == 0x1B) then .error .invalidCatchSection + else if !withSection && flags != 0x03 && flags != 0x13 then .error (.unsupportedHeader flags) + else .ok ({ maxStack := stack0 + 256 * stack1, initLocals := flags / 16 % 2 == 1, + codeSize := size0 + 256 * (size1 + 256 * (size2 + 256 * size3)), + localSignature := token0 + 256 * (token1 + 256 * (token2 + 256 * token3)) }, + code) + | _ => .error .truncatedHeader + else .error (.unsupportedHeader first) + +/-- Look up the slot authorized for an external target by the method's links. -/ +def callImportSlot (imports : Std.TreeMap (List Nat) ImportSlot) (call : CallSite) : Option ImportSlot := + match call.target with + | .external name => imports[bytesOf name]? + | .managed _ => none + +/-- Index the slots for all imported calls in a body. -/ +def BodyImports.byIdentity (imports : BodyImports) : Std.TreeMap (List Nat) ImportSlot := + imports.calls.foldl (fun slots (_, entry) => slots.insert (bytesOf entry.identity) entry) {} + +/-- The checked table of a body: every entry is checked against the metadata, and no token has + two entries. -/ +private def checkTable (env : MetadataEnv) (frame : Frame) + (imports : Std.TreeMap (List Nat) ImportSlot) : + List (Nat × RawOperand) → Std.TreeMap Nat (RawOperand × TokenFacts) → + Except BodyError (Std.TreeMap Nat (RawOperand × TokenFacts)) + | [], table => .ok table + | (token, operand) :: rest, table => do + if table.contains token then throw (.duplicateToken token) + let checked := match operand with + | .call site => checkCallOperand env frame token site (callImportSlot imports site) + | _ => checkEntryIn env frame token operand + let facts <- checked.mapError BodyError.metadata + checkTable env frame imports rest (table.insert token (operand, facts)) + +/-- Every call in an accepted token table passed the operand check in the same metadata frame. -/ +private theorem checkTable_calls {env : MetadataEnv} {frame : Frame} {imports} + {entries : List (Nat × RawOperand)} {start result : Std.TreeMap Nat (RawOperand × TokenFacts)} + (checked : checkTable env frame imports entries start = .ok result) + {token : Nat} {call : CallSite} (listed : (token, .call call) ∈ entries) : + ∃ facts, checkCallOperand env frame token call (callImportSlot imports call) = .ok facts := by + induction entries generalizing start with + | nil => simp at listed + | cons head rest induction => + obtain ⟨key, operand⟩ := head + simp only [checkTable, bind, Except.bind] at checked + split at checked + · contradiction + split at checked + · contradiction + rename_i facts accepted + rcases List.mem_cons.mp listed with first | later + · cases first + cases bound : checkCallOperand env frame token call (callImportSlot imports call) with + | error fault => simp [checkEntryIn, bound, Except.mapError] at accepted + | ok value => exact ⟨value, rfl⟩ + · exact induction checked later + +#print axioms checkTable_calls + +/-- The metadata of a body as an environment of indexed slices, its own first, over the given + identities and tables: independent of the number of rows. -/ +def MethodMetadata.environmentWith (metadata : MethodMetadata) (identities : List (List Nat)) + (tables : List IndexTables) (pinned : PinnedIndex) (platform : PlatformTable) : + Except MetadataError MetadataEnv := + if tables.length != metadata.others.length + 1 || identities.length != tables.length then + .error .tablesMismatch + else do + let indexed ← (metadata.slices.zip (identities.zip tables)).mapM + fun (slice, identity, sliceTables) => slice.indexWith sliceTables identity + .ok (MetadataEnv.ofSlices indexed pinned.prefixes platform pinned.exact) + +/-- The metadata of a body as an environment of indexed slices, built from its bytes alone. -/ +def MethodMetadata.environment (metadata : MethodMetadata) + (pinned : PinnedIndex := PinnedIndex.ofIdentities []) : + Except MetadataError MetadataEnv := do + .ok (MetadataEnv.ofSlices (← metadata.slices.mapM MetadataSlice.index) + pinned.prefixes (exactPins := pinned.exact)) + +/-- Decode the complete instruction stream under the checked header and operand table. -/ +private def decodeCode (env : MetadataEnv) (arity ownerArity : Nat) (body : BodyEvidence) + (header : BodyHeader) (code : List Nat) : Except BodyError DecodedBody := do + if code.length != header.codeSize then + throw (.codeSizeMismatch header.codeSize code.length) + let frame ← (env.frame arity ownerArity).mapError BodyError.metadata + let imports := body.imports.byIdentity + let tokens <- checkTable env frame imports body.tokens.operands {} + let (raw, statics) ← decodeInstructions tokens code.length 0 0 code + if !body.imports.calls.isEmpty && !body.imports.checkUses imports raw then + throw (.metadata (.operandMismatch 0)) + let starts := indexStarts raw 0 false {} + let instructions ← raw.mapM (resolveBranches starts code.length) + .ok { maxStack := header.maxStack, initLocals := header.initLocals, + localSignature := header.localSignature, code := instructions, statics := statics } + +/-- Decode a method body: its header and its instructions, with branch operands as instruction + indices and every token operand checked against the metadata. `arity` is the number of + generic parameters of the method and `ownerArity` that of its owner. The supplied bytes + must be exactly the header and the declared code. -/ +def decodeBody (env : MetadataEnv) (arity ownerArity : Nat) (body : BodyEvidence) : + Except BodyError DecodedBody := do + if !body.bytes.all (· < 256) then throw .invalidByte + let (header, code) ← decodeHeader false body.bytes + decodeCode env arity ownerArity body header code + +/-- A single typed catch clause with half-open IL byte ranges. -/ +structure CatchClause where + tryStart : Nat + tryEnd : Nat + handlerStart : Nat + handlerEnd : Nat + classToken : Nat + deriving Repr, DecidableEq + +/-- Read the code and one small typed catch (II.25.4.6). Both half-open ranges end at + instruction boundaries or the end of code. A boundary in a prefix group must precede + its first prefix. -/ +def decodeSingleCatch (env : MetadataEnv) (arity ownerArity : Nat) (body : BodyEvidence) + (catchType : TypeExpr) : Except BodyError (DecodedBody × CatchClause) := do + unless body.bytes.all (· < 256) do throw .invalidByte + let (header, payload) ← decodeHeader true body.bytes + let (code, tail) := payload.splitAt header.codeSize + let decoded ← decodeCode env arity ownerArity body header code + let padding := (4 - header.codeSize % 4) % 4 + let [1, 16, 0, 0, 0, 0, start0, start1, tryLength, handler0, handler1, handlerLength, + token0, token1, token2, table] := tail.drop padding | throw .invalidCatchSection + unless table ∈ [Table.typeRef, Table.typeDef, Table.typeSpec] && catchType matches .object .. do + throw .invalidCatchSection + let classToken := token0 + 256 * (token1 + 256 * (token2 + 256 * table)) + let frame ← (env.frame arity ownerArity).mapError BodyError.metadata + let _ ← (checkTypeOperand env frame classToken catchType).mapError BodyError.metadata + let tryStart := start0 + 256 * start1 + let handlerStart := handler0 + 256 * handler1 + let tryEnd := tryStart + tryLength + let handlerEnd := handlerStart + handlerLength + let starts := indexStarts decoded.code 0 false {} + let boundary := fun offset => offset == code.length || (starts[offset]?).any (!·.2) + unless 0 < tryLength && 0 < handlerLength && (tryEnd ≤ handlerStart || handlerEnd ≤ tryStart) && + [tryStart, tryEnd, handlerStart, handlerEnd].all boundary do throw .invalidCatchSection + return (decoded, ⟨tryStart, tryEnd, handlerStart, handlerEnd, classToken⟩) + +/-- The metadata environment and imported layouts used to check the body's own tokens. -/ +def BodyEvidence.environment (body : BodyEvidence) : Except MetadataError MetadataEnv := do + let env ← body.metadata.environmentWith body.identities body.tables body.pinned body.platform + return { env with publishedLayouts := body.imports.layouts } + +/-- The method's definition, its locals and its body, each checked against the metadata the + body carries: the decoded body, when everything agrees with the method instantiated at + `arguments`. -/ +def checkBody (body : BodyEvidence) (method : RawMethod) (arguments : List TypeExpr) : + Except BodyError DecodedBody := do + let env ← body.environment.mapError BodyError.metadata + let names ← (methodIdentityValid env method body.instantiation arguments).mapError + BodyError.metadata + if names != body.names then throw (.metadata (.methodMismatch method.token)) + -- `methodIdentityValid` has tied both arities to the `GenericParam` rows. + let arity := method.genericParameters.length + let ownerArity := method.ownerParameters.length + let decoded ← decodeBody env arity ownerArity body + (checkLocals env arity decoded.localSignature method.locals ownerArity).mapError + BodyError.metadata + .ok decoded + +/-- Whether the body decodes to exactly the method's `maxStack`, `initLocals` and `code`, with + the method's definition and locals agreeing with the metadata. Only the kernel decides it: + the elaborator's default reduction evaluates a call by name and would unfold the decoder, + the metadata slices and every token's resolution without sharing, so the definition is + irreducible and `decide` fails at once; `decide +kernel` evaluates it. -/ +@[irreducible] def bodyMatches (body : BodyEvidence) (method : RawMethod) + (arguments : List TypeExpr) : Bool := + match checkBody body method arguments with + | .ok decoded => decoded.maxStack == method.maxStack && + decoded.initLocals == method.initLocals && decoded.code == method.code && + decoded.statics == body.statics + | .error _ => false + +/-- The body's tables are what its metadata bytes give, and the method's definition, locals, + `maxStack`, `initLocals` and `code` are what the body bytes and the metadata say, through the + body's token table. Names are tied to bytes by equations, so no decision evaluates a string: + each slice's assembly spells the identity the body lists, the method's own assembly is the + pin, the pinned identities the body lists are those of the extraction's pins, its platform + definitions are the extraction's `platform`, its imported calls and layouts are those of + the verified links and slots, the owner and base class names the metadata + resolves are the ones the method states, and `signature`, the string a proof selects the + method by, is Cecil's full name of the method the row defines, instantiated at + `arguments`. -/ +abbrev BodyDecodes (pins : List AssemblyPin) (platform : PlatformTable) (pin : AssemblyPin) + (body : BodyEvidence) (method : RawMethod) (arguments : List TypeExpr) (signature : String) + (imports : BodyImports := {}) : Prop := + AssembliesIdentified body.metadata body.identities ∧ + tablesMatch body.metadata body.tables = true ∧ + body.metadata.own.assembly.identity = pin.identity ∧ + body.pinned.identities.mapM spelled? = some (pins.map (·.identity)) ∧ + (body.platform, body.imports) = (platform, imports) /\ + body.names.Spell method ∧ body.names.Selects signature ∧ + bodyMatches body method arguments = true + +/-- Whether the instruction before `index` of `code` is a prefix; the first never is. -/ +private def followsPrefix (code : List RawInstruction) : Nat → Bool + | 0 => false + | index + 1 => (code[index]?).any (isPrefixOpcode ·.opcode) + +/-- Every entry of `starts` records whether its index follows a prefix in `code`. -/ +private def StartsFollow (code : List RawInstruction) + (starts : Std.TreeMap Nat (Nat × Bool)) : Prop := + ∀ (offset index : Nat) (after : Bool), starts[offset]? = some (index, after) → + after = followsPrefix code index + +/-- `indexStarts` records, for each instruction it indexes, whether a prefix precedes it. -/ +private theorem indexStarts_follow (code : List RawInstruction) : + ∀ (rest : List RawInstruction) (index : Nat) (after : Bool) + (starts : Std.TreeMap Nat (Nat × Bool)), + code.drop index = rest → after = followsPrefix code index → StartsFollow code starts → + StartsFollow code (indexStarts rest index after starts) + | [], _, _, _, _, _, follow => by simpa [indexStarts] using follow + | instruction :: rest, index, after, starts, dropped, current, follow => by + simp only [indexStarts] + have fetched : code[index]? = some instruction := by + rw [← List.head?_drop, dropped]; rfl + apply indexStarts_follow code rest (index + 1) + · rw [← List.tail_drop, dropped]; rfl + · simp [followsPrefix, fetched] + · unfold StartsFollow at follow ⊢ + intro offset found after' entry + rw [Std.TreeMap.getElem?_insert] at entry + split at entry + · simp only [Option.some.injEq, Prod.mk.injEq] at entry + obtain ⟨rfl, rfl⟩ := entry + exact current + · exact follow offset found after' entry + +#print axioms indexStarts_follow + +/-- A branch destination the body decoder resolves is an entry of `starts` that follows no + prefix. -/ +private theorem instructionIndex_unprefixed {starts : Std.TreeMap Nat (Nat × Bool)} + {codeLength offset target index : Nat} + (resolved : instructionIndex starts codeLength offset target = .ok index) : + ∃ entry : Nat, starts[entry]? = some (index, false) := by + unfold instructionIndex at resolved + by_cases outside : codeLength ≤ target + · simp [outside] at resolved + · cases found : starts[target]? with + | none => simp [outside, found] at resolved + | some entry => + obtain ⟨foundIndex, after⟩ := entry + cases after + · simp only [outside, found, ite_false, Except.ok.injEq] at resolved + exact ⟨target, resolved ▸ found⟩ + · simp [outside, found] at resolved + +#print axioms instructionIndex_unprefixed + +/-- A list an `Except`-valued function maps entirely holds at each index the image of the + input there. -/ +private theorem mapM_ok_at {α β ε : Type} {make : α → Except ε β} : + ∀ {inputs : List α} {outputs : List β}, inputs.mapM make = .ok outputs → + ∀ (index : Nat) (output : β), outputs[index]? = some output → + ∃ input, inputs[index]? = some input ∧ make input = .ok output + | [], outputs, made, index, output, found => by + simp only [List.mapM_nil, pure, Except.pure, Except.ok.injEq] at made + subst made + simp at found + | input :: rest, outputs, made, index, output, found => by + simp only [List.mapM_cons, bind, Except.bind, pure, Except.pure] at made + cases first : make input with + | error => simp [first] at made + | ok head => + cases others : rest.mapM make with + | error => simp [first, others] at made + | ok tail => + simp only [first, others, Except.ok.injEq] at made + subst made + cases index with + | zero => + simp only [List.getElem?_cons_zero, Option.some.injEq] at found + exact ⟨input, rfl, found ▸ first⟩ + | succ index => + simpa using mapM_ok_at others index output (by simpa using found) + +#print axioms mapM_ok_at + +/-- An instruction the decoder resolves keeps its opcode, and each of its branch destinations is + an entry of `starts` that follows no prefix. -/ +private theorem resolveBranches_unprefixed {starts : Std.TreeMap Nat (Nat × Bool)} + {codeLength : Nat} {instruction resolved : RawInstruction} + (ok : resolveBranches starts codeLength instruction = .ok resolved) : + resolved.opcode = instruction.opcode ∧ ∀ target, branchesTo target resolved = true → + ∃ entry : Nat, starts[entry]? = some (target, false) := by + unfold resolveBranches at ok + split at ok + · next destination operand => + cases index : instructionIndex starts codeLength instruction.offset destination with + | error => simp [index, bind, Except.bind] at ok + | ok found => + simp only [index, bind, Except.bind, Except.ok.injEq] at ok + subst ok + refine ⟨rfl, fun target branches => ?_⟩ + simp only [branchesTo, beq_iff_eq] at branches + exact branches ▸ instructionIndex_unprefixed index + · next destinations operand => + cases indices : destinations.mapM (instructionIndex starts codeLength instruction.offset) with + | error => simp [indices, bind, Except.bind] at ok + | ok found => + simp only [indices, bind, Except.bind, Except.ok.injEq] at ok + subst ok + refine ⟨rfl, fun target branches => ?_⟩ + simp only [branchesTo, List.contains_iff_mem] at branches + obtain ⟨position, at_position⟩ := List.mem_iff_getElem?.mp branches + obtain ⟨_, _, resolvedAt⟩ := mapM_ok_at indices position target at_position + exact instructionIndex_unprefixed resolvedAt + · next unbranched => + simp only [Except.ok.injEq] at ok + subst ok + refine ⟨rfl, fun target branches => ?_⟩ + simp only [branchesTo] at branches + simp at branches + +#print axioms resolveBranches_unprefixed + +/-- Instructions whose branch destinations the decoder resolved against their own starts + have no branch into the instruction after a prefix. -/ +private theorem resolved_prefixEntryFree {raw instructions : List RawInstruction} + {codeLength : Nat} + (ok : raw.mapM (resolveBranches (indexStarts raw 0 false ∅) codeLength) = .ok instructions) : + PrefixEntryFree instructions := by + have follow : StartsFollow raw (indexStarts raw 0 false ∅) := + indexStarts_follow raw raw 0 false ∅ List.drop_zero rfl (by + unfold StartsFollow + intro offset index after found + simp at found) + intro pc instruction fetched isPrefix + rw [List.any_eq_false] + intro other member branches + obtain ⟨position, otherAt⟩ := List.mem_iff_getElem?.mp member + obtain ⟨_, _, resolvedOther⟩ := mapM_ok_at ok position other otherAt + obtain ⟨entry, found⟩ := (resolveBranches_unprefixed resolvedOther).2 (pc + 1) branches + have unprefixed := follow entry (pc + 1) false found + obtain ⟨original, originalAt, resolvedHere⟩ := mapM_ok_at ok pc instruction fetched + have sameOpcode := (resolveBranches_unprefixed resolvedHere).1 + simp [followsPrefix, originalAt, ← sameOpcode, isPrefix] at unprefixed + +#print axioms resolved_prefixEntryFree + +/-- No branch of a decoded body, reachable or not, enters the instruction after a prefix. -/ +theorem decodeBody_prefixEntryFree {env : MetadataEnv} {arity ownerArity : Nat} + {body : BodyEvidence} {decoded : DecodedBody} + (ok : decodeBody env arity ownerArity body = .ok decoded) : PrefixEntryFree decoded.code := by + unfold decodeBody at ok + simp only [decodeCode, bind, Except.bind] at ok + repeat' split at ok + all_goals first + | (simp only [Except.ok.injEq] at ok; subst ok; exact resolved_prefixEntryFree ‹_›) + | simp_all + +#print axioms decodeBody_prefixEntryFree + +/-- No branch of a body the check accepts enters the instruction after a prefix. -/ +theorem checkBody_prefixEntryFree {body : BodyEvidence} {method : RawMethod} + {arguments : List TypeExpr} {decoded : DecodedBody} + (ok : checkBody body method arguments = .ok decoded) : PrefixEntryFree decoded.code := by + unfold checkBody at ok + simp only [bind, Except.bind] at ok + repeat' split at ok + all_goals first + | (simp only [Except.ok.injEq] at ok; subst ok; exact decodeBody_prefixEntryFree ‹_›) + | simp_all + +#print axioms checkBody_prefixEntryFree + +/-- A successful decode used one frame to check every operand in its table. -/ +private theorem decodeBody_checkedTable {env : MetadataEnv} {arity ownerArity : Nat} + {body : BodyEvidence} {decoded : DecodedBody} (ok : decodeBody env arity ownerArity body = + .ok decoded) : + ∃ frame table, env.frame arity ownerArity = .ok frame ∧ + checkTable env frame body.imports.byIdentity body.tokens.operands ∅ = .ok table := by + unfold decodeBody at ok + simp only [decodeCode, bind, Except.bind] at ok + repeat' split at ok + all_goals first + | contradiction + | (refine ⟨_, _, ?_, by assumption⟩ + cases frameEq : env.frame arity ownerArity <;> + simp_all [Except.mapError]) + +#print axioms decodeBody_checkedTable + +/-- A bound body validates every call reference listed in its token table in its own type frame. -/ +private theorem BodyDecodes.checkedCall {pins platform pin body method arguments signature imports} + (decodes : BodyDecodes pins platform pin body method arguments signature imports) {token : Nat} {call : CallSite} + (listed : (token, RawOperand.call call) ∈ body.tokens.operands) : + ∃ env frame facts, + body.environment = .ok env ∧ + env.frame method.genericParameters.length method.ownerParameters.length = .ok frame ∧ + checkCallOperand env frame token call (callImportSlot body.imports.byIdentity call) = .ok facts := by + have matched := decodes.2.2.2.2.2.2.2 + unfold bodyMatches at matched + split at matched + · rename_i checked + unfold checkBody at checked + simp only [bind, Except.bind] at checked + repeat' split at checked + all_goals first + | contradiction + | (obtain ⟨frame, table, frameEq, tableEq⟩ := decodeBody_checkedTable + (show decodeBody _ _ _ body = .ok _ from by assumption) + obtain ⟨facts, accepted⟩ := checkTable_calls tableEq listed + refine ⟨_, frame, facts, ?_, frameEq, accepted⟩ + cases envEq : body.environment <;> + simp_all [Except.mapError]) + · contradiction + +#print axioms BodyDecodes.checkedCall + +/-- A bound body checks each token's call reference and external identity against one member. -/ +theorem BodyDecodes.externalTarget {pins : List AssemblyPin} {pin : AssemblyPin} + {platform : PlatformTable} {imports : BodyImports} + {body : BodyEvidence} {method : RawMethod} {arguments : List TypeExpr} {signature : String} + (decodes : BodyDecodes pins platform pin body method arguments signature imports) {token : Nat} + {call : CallSite} {identity : String} (listed : (token, .call call) ∈ body.tokens.operands) + (externalTarget : call.target = .external identity) : + ∃ env frame site, + body.environment = .ok env ∧ + env.frame method.genericParameters.length method.ownerParameters.length = .ok frame ∧ + resolveCallMember env frame token call (callImportSlot body.imports.byIdentity call) = .ok site ∧ + checkCallReference env frame token site call = .ok () ∧ site.definition = none ∧ (site.externalName = some (bytesOf identity) ∨ + site.contractName frame = some (bytesOf identity)) := by + obtain ⟨env, frame, _, built, localFrame, checked⟩ := decodes.checkedCall listed + obtain ⟨site, target⟩ := checkCallOperand_external checked externalTarget + exact ⟨env, frame, site, built, localFrame, target⟩ + +#print axioms BodyDecodes.externalTarget + +/-- The checked table contains only supplied token operands or operands already in its input. -/ +private theorem checkTable_origin {env : MetadataEnv} {frame : Frame} {imports} + {entries : List (Nat × RawOperand)} {start result : Std.TreeMap Nat (RawOperand × TokenFacts)} + (checked : checkTable env frame imports entries start = .ok result) {key operand facts} + (found : result[key]? = some (operand, facts)) : + (key, operand) ∈ entries ∨ start[key]? = some (operand, facts) := by + induction entries generalizing start with + | nil => cases checked; exact Or.inr found + | cons head rest ih => + obtain ⟨token, value⟩ := head + simp only [checkTable, bind, Except.bind] at checked + split at checked <;> try contradiction + split at checked <;> try contradiction + rcases ih checked with later | earlier + · exact Or.inl (List.mem_cons_of_mem _ later) + · rw [Std.TreeMap.getElem?_insert] at earlier + split at earlier + · rename_i same + obtain rfl := Nat.compare_eq_eq.mp same + cases earlier + exact Or.inl List.mem_cons_self + · exact Or.inr earlier + +#print axioms checkTable_origin + +/-- A call operand can only come from a token lookup in the instruction bytes. -/ +private theorem decodeOperand_call {table offset start opcode kind bytes value call} + (ok : decodeOperand table offset start opcode kind bytes = .ok value) + (called : value.1 = RawOperand.call call) : + ∃ (token : Nat) (facts : TokenFacts), table[token]? = some (RawOperand.call call, facts) := by + obtain ⟨operand, following, remaining⟩ := value + cases called + cases kind <;> simp only [decodeOperand, readToken, readValue, readBranch, readSwitch, + bind, Except.bind] at ok + all_goals repeat' split at ok + all_goals first + | contradiction + | (solve | simp at ok) + | (have same := congrArg Prod.fst (Except.ok.inj ok) + simp only at same + subst_vars + exact ⟨_, _, by assumption⟩) + +#print axioms decodeOperand_call + +/-- Each call in a decoded instruction stream belongs to the operand table. -/ +private theorem decodeInstructions_call {table fuel offset index bytes code statics} + (ok : decodeInstructions table fuel offset index bytes = .ok (code, statics)) + {instruction : RawInstruction} (member : instruction ∈ code) {call} + (called : instruction.operand = .call call) : + ∃ (token : Nat) (facts : TokenFacts), table[token]? = some (RawOperand.call call, facts) := by + induction fuel generalizing offset index bytes code statics with + | zero => + cases bytes <;> simp only [decodeInstructions] at ok <;> cases ok + simp at member + | succ fuel ih => + cases bytes with + | nil => cases ok; simp at member + | cons byte bytes => + simp only [decodeInstructions, bind, Except.bind] at ok + repeat' split at ok + all_goals first + | contradiction + | (cases ok + rcases List.mem_cons.mp member with rfl | later + · simp only at called + exact decodeOperand_call (by assumption) called + · exact ih (by assumption) later) + +#print axioms decodeInstructions_call + +/-- Resolving a branch can never introduce or change a call operand. -/ +private theorem resolveBranches_call {starts size raw instruction call} + (ok : resolveBranches starts size raw = .ok instruction) + (called : instruction.operand = .call call) : raw.operand = .call call := by + unfold resolveBranches at ok + simp only [bind, Except.bind] at ok + repeat' split at ok + all_goals first | contradiction | (cases ok; exact called) | (cases ok; cases called) + +#print axioms resolveBranches_call + +/-- The bytes of a decoded call selected an operand supplied at a token of this body. -/ +private theorem decodeBody_call {env arity ownerArity body decoded} + (ok : decodeBody env arity ownerArity body = .ok decoded) + {pc : Nat} {instruction : RawInstruction} {call : CallSite} + (fetched : decoded.code[pc]? = some instruction) (called : instruction.operand = .call call) : + ∃ token, (token, RawOperand.call call) ∈ body.tokens.operands := by + unfold decodeBody at ok + simp only [decodeCode, bind, Except.bind] at ok + repeat' split at ok + all_goals first + | contradiction + | (cases ok + obtain ⟨raw, rawAt, resolved⟩ := mapM_ok_at (by assumption) pc instruction fetched + obtain ⟨token, facts, found⟩ := decodeInstructions_call (by assumption) + (List.mem_of_getElem? rawAt) (resolveBranches_call resolved called) + rcases checkTable_origin (by assumption) found with member | empty + · exact ⟨token, member⟩ + · simp at empty) + +#print axioms decodeBody_call + +/-- Each call instruction of the decoded method reads an operand from this body's token table. -/ +private theorem BodyDecodes.callOperand {pins platform pin body method arguments signature imports} + (decodes : BodyDecodes pins platform pin body method arguments signature imports) + {pc : Nat} {instruction : RawInstruction} {call : CallSite} + (fetched : method.code[pc]? = some instruction) (called : instruction.operand = .call call) : + ∃ token, (token, RawOperand.call call) ∈ body.tokens.operands := by + have matched := decodes.2.2.2.2.2.2.2 + unfold bodyMatches at matched + split at matched + · rename_i decoded checked + simp only [Bool.and_eq_true, beq_iff_eq] at matched + rw [← matched.1.2] at fetched + unfold checkBody at checked + simp only [bind, Except.bind] at checked + repeat' split at checked + all_goals first + | contradiction + | (cases checked + exact decodeBody_call (by assumption) fetched called) + · contradiction + +#print axioms BodyDecodes.callOperand + +/-- Every decoded external call names the metadata member of its own reference. -/ +theorem BodyDecodes.callExternalTarget {pins platform pin body method arguments signature imports} + (decodes : BodyDecodes pins platform pin body method arguments signature imports) + {pc : Nat} {instruction : RawInstruction} {call : CallSite} {identity : String} + (fetched : method.code[pc]? = some instruction) (called : instruction.operand = .call call) + (externalTarget : call.target = .external identity) : + ∃ token env frame site, + (token, RawOperand.call call) ∈ body.tokens.operands ∧ + body.environment = .ok env ∧ + env.frame method.genericParameters.length method.ownerParameters.length = .ok frame ∧ + resolveCallMember env frame token call (callImportSlot body.imports.byIdentity call) = .ok site ∧ + checkCallReference env frame token site call = .ok () ∧ site.definition = none ∧ (site.externalName = some (bytesOf identity) ∨ + site.contractName frame = some (bytesOf identity)) := by + obtain ⟨token, listed⟩ := decodes.callOperand fetched called + obtain ⟨env, frame, site, resolved⟩ := decodes.externalTarget listed externalTarget + exact ⟨token, env, frame, site, listed, resolved⟩ + +#print axioms BodyDecodes.callExternalTarget + +/-- Each managed call in the decoded body resolves its operand to a MethodDef in a particular + metadata slice. A MemberRef in the caller may have a different token from that definition. -/ +theorem BodyDecodes.callManagedTarget {pins platform pin body method arguments signature imports} + (decodes : BodyDecodes pins platform pin body method arguments signature imports) + {pc target : Nat} {instruction : RawInstruction} {call : CallSite} + (fetched : method.code[pc]? = some instruction) (called : instruction.operand = .call call) + (managed : call.target = .managed target) : + ∃ token env frame site position, + (token, RawOperand.call call) ∈ body.tokens.operands ∧ + body.environment = .ok env ∧ + env.frame method.genericParameters.length method.ownerParameters.length = .ok frame ∧ + env.methodSite token = .ok site ∧ checkCallReference env frame token site call = .ok () ∧ + site.definition = some (position, target) := by + obtain ⟨token, listed⟩ := decodes.callOperand fetched called + obtain ⟨env, frame, _, built, localFrame, checked⟩ := decodes.checkedCall listed + simp only [callImportSlot, managed] at checked + obtain ⟨site, position, resolved⟩ := checkCallOperand_managed checked managed + exact ⟨token, env, frame, site, position, listed, built, localFrame, resolved⟩ + +#print axioms BodyDecodes.callManagedTarget + +end CIL.Extraction diff --git a/CIL/Extraction/BodyDiagnostics.lean b/CIL/Extraction/BodyDiagnostics.lean new file mode 100644 index 000000000..a856b1a68 --- /dev/null +++ b/CIL/Extraction/BodyDiagnostics.lean @@ -0,0 +1,56 @@ +import Lean +import CIL.Extraction.BodyDecoding + +/-! +# Naming a body that does not decode + +`cil_body_decodes` proves `bodyMatches body method arguments = true` by kernel evaluation. When +the kernel refutes it, the tactic says which method failed and why: the error the body and +metadata check raises, or the field of the decoded body that differs from the emitted definition. +The check itself stays the kernel's decision; the evaluation here only words the failure. +-/ + +namespace CIL.Extraction + +/-- The first field where the decoded body differs from the supplied evidence or method. -/ +def bodyDifference (decoded : DecodedBody) (body : BodyEvidence) (method : RawMethod) : + Option String := + if decoded.statics != body.statics then some "the recorded static field tokens" + else if decoded.maxStack != method.maxStack then some "the maximum stack depth" + else if decoded.initLocals != method.initLocals then some "the init-locals flag" + else if decoded.code != method.code then some "the instructions" + else none + +/-- The decoding error, mismatching field or kernel reduction failure for the named method. -/ +def describeBodyFailure (signature : String) (body : BodyEvidence) (method : RawMethod) + (arguments : List TypeExpr) : String := + let lead := s!"Method {signature}: the decoded method does not match its body bytes or metadata" + match checkBody body method arguments with + | .error error => s!"{lead}: {repr error}" + | .ok decoded => + match bodyDifference decoded body method with + | some field => s!"{lead}: {field} differ" + | none => s!"Method {signature}: the decoded fields agree, but kernel reduction failed" + +open Lean Elab Tactic Meta in +/-- Prove `bodyMatches body method arguments = true` with the kernel, naming the method and the + failure when the kernel refutes it. The body must be a constant `methodNBody` with its + method's `methodNSignature` beside it, as extractions emit them. -/ +elab "cil_body_decodes" : tactic => do + let goal ← getMainGoal + let target ← instantiateMVars (← goal.getType) + try evalTactic (← `(tactic| decide +kernel)) + catch kernelFailure => + let arguments := (target.getAppArgs[1]?.getD target).getAppArgs + let some bodyName := arguments[0]?.bind (·.constName?) + | throw kernelFailure + let some typeArguments := arguments[2]? + | throw kernelFailure + let stem := bodyName.getString! + let signatureName := Name.mkStr bodyName.getPrefix ((stem.dropEnd 4).toString ++ "Signature") + let message ← unsafe evalExpr String (mkConst ``String) + (mkAppN (mkConst ``describeBodyFailure) + #[mkConst signatureName, arguments[0]!, arguments[1]!, typeArguments]) + throwError message + +end CIL.Extraction diff --git a/CIL/Extraction/ByteLiterals.lean b/CIL/Extraction/ByteLiterals.lean new file mode 100644 index 000000000..b4bdc39d0 --- /dev/null +++ b/CIL/Extraction/ByteLiterals.lean @@ -0,0 +1,106 @@ +import CIL.Identity +import CIL.Extraction.FixedDigits + +/-! +# Byte literals + +The extractor writes a list of bytes, such as a method body, a metadata row or a UTF-8 name, as +`fixedBytes count code`: the byte count and one number holding the bytes big-endian. The code of +a list of at most 256 bytes is one hexadecimal numeral. A longer list's code joins numerals of at +most 256 bytes with `Nat.lor` and `Nat.shiftLeft`, since the elaborator parses a numeral in time +quadratic in its digits, and the kernel keys its caches by a hash that reads only the low 64 bits +of a numeral, so separate numerals with equal last bytes would collide there. A numeral costs the +elaborator a few allocations, where a list literal costs a numeral and a cons per byte. The kernel +recovers the list by splitting the code in halves down to words of at most eight digits, so a +decode takes a linear number of steps. `fixedDigitsWithin` splits a code into digits of any width; +the splitter is defined in [FixedDigits](FixedDigits.lean), and this module proves what it reads: +`fixedDigitsWithin_eq_fixedDigits` ties it to the digit lists of `CIL.Identity`. +-/ + +namespace CIL.Extraction +open CIL + +/-- Reading the digits of a word back appends the word to the code read. -/ +theorem wordDigits_spec (width : Nat) : ∀ (count word : Nat) (tail : List Nat) (start : Nat), + word < (2 ^ width) ^ count → + radixCode? (2 ^ width) (wordDigits width count word tail) start = + radixCode? (2 ^ width) tail (start * (2 ^ width) ^ count + word) + | 0, word, tail, start, bounded => by + have zero : word = 0 := by simpa using bounded + subst zero + simp [wordDigits] + | count + 1, word, tail, start, bounded => by + have quotient : Nat.shiftRight word width = word / 2 ^ width := + Nat.shiftRight_eq_div_pow word width + have remainder : Nat.mod word (Nat.shiftLeft 1 width) = word % 2 ^ width := by + show word % (1 <<< width) = _ + rw [Nat.shiftLeft_eq, Nat.one_mul] + have upper : word / 2 ^ width < (2 ^ width) ^ count := by + rw [Nat.pow_succ] at bounded + exact Nat.div_lt_of_lt_mul (by rw [Nat.mul_comm]; exact bounded) + have small : word % 2 ^ width < 2 ^ width := Nat.mod_lt word (Nat.two_pow_pos width) + rw [wordDigits, quotient, remainder, + wordDigits_spec width count (word / 2 ^ width) _ start upper] + simp only [radixCode?, small, ↓reduceIte] + congr 1 + have joined := Nat.div_add_mod word (2 ^ width) + rw [Nat.pow_succ, ← Nat.mul_assoc] + generalize 2 ^ width = base at joined ⊢ + generalize start * base ^ count = scaled + rw [Nat.add_mul, Nat.mul_comm (word / base)] + omega + +#print axioms wordDigits_spec + +/-- Reading the split digits back appends the code to the code read, at every depth. -/ +theorem fixedDigitsWithin_spec (width : Nat) : + ∀ (depth count code : Nat) (tail : List Nat) (start : Nat), + code < (2 ^ width) ^ count → + radixCode? (2 ^ width) (fixedDigitsWithin width depth count code tail) start = + radixCode? (2 ^ width) tail (start * (2 ^ width) ^ count + code) + | 0, count, code, tail, start, bounded => by + rw [fixedDigitsWithin] + exact wordDigits_spec width count code tail start bounded + | depth + 1, count, code, tail, start, bounded => by + rw [fixedDigitsWithin] + cases Nat.ble count wordDigitCount + · have power : 2 ^ (width * (count / 2)) = (2 ^ width) ^ (count / 2) := Nat.pow_mul .. + have positive : 0 < (2 ^ width) ^ (count / 2) := Nat.pow_pos (Nat.two_pow_pos width) + have splitCount : count - count / 2 + count / 2 = count := by omega + have upper : code / (2 ^ width) ^ (count / 2) < (2 ^ width) ^ (count - count / 2) := by + apply (Nat.div_lt_iff_lt_mul positive).mpr + rw [← Nat.pow_add, splitCount] + exact bounded + have lower : code % (2 ^ width) ^ (count / 2) < (2 ^ width) ^ (count / 2) := + Nat.mod_lt _ positive + change radixCode? (2 ^ width) (fixedDigitsWithin width depth (count - count / 2) + (code >>> (width * (count / 2))) + (fixedDigitsWithin width depth (count / 2) (code % (1 <<< (width * (count / 2)))) + tail)) start = _ + rw [Nat.shiftRight_eq_div_pow, Nat.shiftLeft_eq, Nat.one_mul, power, + fixedDigitsWithin_spec width depth _ _ _ start upper, + fixedDigitsWithin_spec width depth _ _ _ _ lower] + congr 1 + have joined := Nat.div_add_mod code ((2 ^ width) ^ (count / 2)) + rw [Nat.add_mul, Nat.mul_assoc, ← Nat.pow_add, splitCount] + generalize (2 ^ width) ^ (count / 2) = scale at joined + generalize start * (2 ^ width) ^ count = scaled + rw [Nat.mul_comm] at joined + omega + · exact wordDigits_spec width count code tail start bounded + +#print axioms fixedDigitsWithin_spec + +/-- A code below `(2 ^ width) ^ count` splits into the `count` digits of `width` bits that + `fixedDigits` gives; `fixedBytes count code` is the case of eight bits. -/ +theorem fixedDigitsWithin_eq_fixedDigits (width count code : Nat) (wide : 0 < width) + (bounded : code < (2 ^ width) ^ count) : + fixedDigitsWithin width count count code [] = fixedDigits width count code [] := by + have decoded := fixedDigitsWithin_spec width count count code [] 1 bounded + have digits := fixedDigits_spec width count code bounded [] 1 + simp only [radixCode?] at decoded digits + exact radixCode?_injective (Nat.le_self_pow (by omega) 2) decoded digits + +#print axioms fixedDigitsWithin_eq_fixedDigits + +end CIL.Extraction diff --git a/CIL/Extraction/CallAgreement.lean b/CIL/Extraction/CallAgreement.lean new file mode 100644 index 000000000..10e749e19 --- /dev/null +++ b/CIL/Extraction/CallAgreement.lean @@ -0,0 +1,402 @@ +import CIL.Extraction.OpaqueLocals +import CIL.Extraction.UnsafeCalls +import CIL.Extraction.Scalars +import CIL.Extraction.LinkIndex +import CIL.Extraction.StaticValues +import CIL.EntryIndex + +/-! +# Call-site agreement + +The method decoder and the literal-method validator interpret the same raw claim. A link leaves +its instruction to the managed-call checks. An unlinked site either names its exact instruction +or requires a hole, which cannot be live in a validated method. +-/ + +namespace CIL.Extraction + +deriving instance DecidableEq for Instruction + +/-- The required emission at a method-token site. A linked operation is checked with its target. -/ +inductive CallRequirement where + | linked + | instruction (operation : Instruction) + | hole (opcode : Nat) + +/-- An occupied site preserves a resolving entry's declaration and profile requirements. -/ +def ModelCatalog.linkedRequirement (catalog : ModelCatalog) (environment : CallLayouts) + (profile : FeatureProfile) (raw : RawInstruction) : CallRequirement := + match raw.operand, callOpcode? raw.opcode with + | .call site, some opcode => + if (catalog.table.entry? opcode site.method).any (fun entry => + match entry.select? site.method with + | none => entry.ownsName + | some overload => + match overload.resolve environment site.method with + | some model => !overload.operand.bindsSite site || !model.available profile + | none => entry.ownsName) then + .hole raw.opcode + else .linked + | _, _ => .linked + +/-- Decode the raw claim using the layouts and profile. -/ +def ModelCatalog.callRequirement (catalog : ModelCatalog) (environment : CallLayouts) + (profile : FeatureProfile) (link : Option Link) (raw : RawInstruction) : CallRequirement := + let missing := CallRequirement.hole raw.opcode + let layouts := environment.toLayoutEnvironment + match siteClaim layouts link raw catalog.table.claims with + | .link => catalog.linkedRequirement environment profile raw + | .refused => missing + | .model => + match raw.operand, callOpcode? raw.opcode with + | .call site, some opcode => + let overload := (catalog.table.entry? opcode site.method).bind (·.select? site.method) + if overload.any (fun selected => selected.operand.bindsSite site && + (selected.resolve environment site.method).any (·.available profile)) then + match opcode with + | .call => .instruction (.call site.method {}) + | .newobj => .instruction (.newobj site.method) + else missing + | _, _ => missing + +/-- The raw decoder supplies an instruction only where a link claims the site. -/ +def CallRequirement.decode (otherwise : Unit → Instruction) : CallRequirement → Instruction + | .linked => otherwise () + | .instruction operation => operation + | .hole opcode => .unadmitted opcode + +/-- A missing instruction has a single representation and cannot occur at a live pc. -/ +def CallRequirement.accepts (live : Bool) (emitted : Instruction) : CallRequirement → Bool + | .linked => true + | .instruction operation => decide (emitted = operation) + | .hole opcode => !live && decide (emitted = .unadmitted opcode) + +/-- A method-token site satisfies its raw claim. At other opcodes no call obligation applies. -/ +def ModelCatalog.agreesAt (catalog : ModelCatalog) (environment : CallLayouts) + (profile : FeatureProfile) (raw : RawInstruction) (emitted : Instruction) + (link : Option Link) (live : Bool) : Bool := + if methodTokenCall raw.opcode then + (catalog.callRequirement environment profile link raw).accepts live emitted + else true + +/-- Check one emitted instruction against its method's layouts and raw claim. -/ +def ModelCatalog.siteAgrees (catalog : ModelCatalog) (environment : LayoutEnvironment) + (profile : FeatureProfile) (raw : RawInstruction) (emitted : Instruction) + (link : Option Link := none) (live : Bool := false) : Bool := + catalog.agreesAt environment.callLayouts profile raw emitted link live + +/-- A folded site emits its role, a live call satisfies its claim, and a dead call is a hole. -/ +def ModelCatalog.emissionAgrees (catalog : ModelCatalog) (layouts : CallLayouts) + (profile : FeatureProfile) (raw : RawInstruction) (emitted : Instruction) + (link : Option Link) (role : Option FoldRole) (live : Bool) : Bool := + if live then + if foldOwnsSite role then decide (role.bind (Instruction.ofRole? · none raw) = some emitted) + else catalog.agreesAt layouts profile raw emitted link true + else !methodTokenCall raw.opcode || decide (emitted = .unadmitted raw.opcode) + +/-- The instruction walk checks live claims and the representation of dead calls. -/ +private def modelSitesAgree (catalog : ModelCatalog) (environment : LayoutEnvironment) + (profile : FeatureProfile) (closed : RawMethod) (reachable : List Bool) + (code : List Instruction) (links : Array (Option Link)) : Bool := + let layouts := environment.callLayouts + let roles := (foldRoles closed.code).toArray + all3 (fun (raw, pc) live emitted => + catalog.emissionAgrees layouts profile raw emitted + (indexedRead? links pc).join (indexedRead? roles pc).join live) + closed.code.zipIdx reachable code + +/-- Every live call fits its claim. Links and raw fold roles are indexed once per body. -/ +def ModelsAgree (catalog : ModelCatalog) (environment : LayoutEnvironment) + (profile : FeatureProfile) (closed : RawMethod) (reachable : List Bool) + (code : List Instruction) (links : Array (Option Link) := #[]) : Prop := + closed.code.length = reachable.length ∧ closed.code.length = code.length ∧ + modelSitesAgree catalog environment profile closed reachable code links = true + +instance (catalog : ModelCatalog) (environment : LayoutEnvironment) (profile : FeatureProfile) + (closed : RawMethod) (reachable : List Bool) (code : List Instruction) + (links : Array (Option Link)) : Decidable (ModelsAgree catalog environment profile closed + reachable code links) := + decidable_of_iff (modelSitesAgree catalog environment profile closed reachable code links = + true) <| by + constructor + · intro checked + unfold modelSitesAgree at checked + have sizes := all3_lengths checked + exact ⟨by simpa using sizes.1, by simpa using sizes.2, checked⟩ + · exact fun agreed => agreed.2.2 + +/-- Every live method-token call satisfies the runtime catalog or its linked-call obligation. -/ +def RuntimeModelsAgree (closed : RawMethod) (reachable : List Bool) (method : Method) : Prop := + ModelsAgree runtimeCatalog method.environment method.profile closed reachable method.code + (if reachable.contains true then + (linkReads method.links (List.range method.code.length)).toArray else #[]) + +instance (closed : RawMethod) (reachable : List Bool) (method : Method) : + Decidable (RuntimeModelsAgree closed reachable method) := + inferInstanceAs (Decidable (ModelsAgree _ _ _ _ _ _ _)) + +/-- A live method-token site outside a fold has a claim. -/ +def SiteClaimTotal (environment : LayoutEnvironment) (link : Option Link) (raw : RawInstruction) + (live : Bool) (role : Option FoldRole := none) : Prop := + (live && !foldOwnsSite role) = true → methodTokenCall raw.opcode = true → + siteClaim environment link raw ≠ .refused + +/-- Every live method-token site outside a fold in a validated body has a raw claim. -/ +theorem RuntimeModelsAgree.siteClaimTotal {closed : RawMethod} {reachable : List Bool} + {method : Method} (agree : RuntimeModelsAgree closed reachable method) + {pc : Nat} {raw : RawInstruction} (atRaw : closed.code[pc]? = some raw) + (atLive : reachable[pc]? = some true) : + SiteClaimTotal method.environment (method.linkAt pc) raw true + ((foldRoles closed.code)[pc]?.join) := by + intro ordinary token refused + simp only [Bool.true_and, Bool.not_eq_true'] at ordinary + have live := List.contains_iff_mem.mpr (List.mem_of_getElem? atLive) + simp only [RuntimeModelsAgree, live, ite_true] at agree + obtain ⟨_, lengthCode, checks⟩ := agree + have inside : pc < closed.code.length := (List.getElem?_eq_some_iff.mp atRaw).1 + have inCode : pc < method.code.length := lengthCode ▸ inside + have zipped : closed.code.zipIdx[pc]? = some (raw, pc) := by + simp [List.getElem?_zipIdx, atRaw] + have checked := all3_at checks zipped atLive (List.getElem?_eq_getElem inCode) + have linkAt : (linkReads method.links (List.range method.code.length))[pc]?.join = + method.linkAt pc := by simp [linkReads_eq, inCode, Method.linkAt] + change siteClaim method.environment (method.linkAt pc) raw + runtimeCatalog.table.claims = .refused at refused + simp [ModelCatalog.emissionAgrees, ordinary, ModelCatalog.agreesAt, token, linkAt, + ModelCatalog.callRequirement, refused, + CallRequirement.accepts] at checked + +#print axioms RuntimeModelsAgree.siteClaimTotal + +/-- At a live pc that a fold owns, a validated body emits exactly the instruction the fold role + fixes, so a call inside a fold cannot emit anything else. -/ +theorem RuntimeModelsAgree.foldOwnedSite {closed : RawMethod} {reachable : List Bool} + {method : Method} (agree : RuntimeModelsAgree closed reachable method) + {pc : Nat} {raw : RawInstruction} {emitted : Instruction} + (atRaw : closed.code[pc]? = some raw) (atLive : reachable[pc]? = some true) + (atCode : method.code[pc]? = some emitted) + (owned : foldOwnsSite ((foldRoles closed.code)[pc]?.join) = true) : + ((foldRoles closed.code)[pc]?.join).bind (Instruction.ofRole? · none raw) = + some emitted := by + have live := List.contains_iff_mem.mpr (List.mem_of_getElem? atLive) + simp only [RuntimeModelsAgree, live, ite_true] at agree + obtain ⟨_, _, checks⟩ := agree + have zipped : closed.code.zipIdx[pc]? = some (raw, pc) := by + simp [List.getElem?_zipIdx, atRaw] + have checked := all3_at checks zipped atLive atCode + simp only [ModelCatalog.emissionAgrees, ite_true] at checked + simpa [owned] using checked + +#print axioms RuntimeModelsAgree.foldOwnedSite + +/-- An ordinary static field belongs to a closed type in the method's assembly. -/ +private def localStaticOwner (methodOwner : String) (owner : TypeExpr) : Bool := + let scope := String.ofList ((metadataChars methodOwner).takeWhile (· != ':')) + if scope.isEmpty || !ownedBy scope methodOwner then false else + match owner with + | .object assembly _ | .atom assembly _ => scope == assembly + | .aggregate identity _ => ownedBy scope identity + | _ => false + +/-- Loads and addresses bind immutable storage. A store does not decode, so its position stays + a hole and a method with a reachable store is refused. -/ +private def ordinaryStaticInstruction (environment : CallLayouts) (methodOwner : String) + (descriptor : StaticDescriptor) (opcode : Nat) : Option Instruction := do + let field := descriptor.field + guard (localStaticOwner methodOwner field.owner && descriptor.alignment == 1) + guard (staticStorageMatches field.type descriptor) + if descriptor.scalar.isNone then + guard (environment.valueShape field.type == .aggregate descriptor.bytes.length) + if descriptor.opaqueValue then + guard (opcode == 126 && initializerYields [] descriptor) + else + let init ← descriptor.initializer + guard (init.literal.isNone || initializerYields [] descriptor) + match opcode with + | 126 => return .ldsfld field + | 127 => return .ldsflda field + | _ => none + +/-- The raw operand names the descriptor's field. An RVA site also requires its full blob. -/ +private def staticInstruction (profile : FeatureProfile) (environment : CallLayouts) + (methodOwner : String) (site : Option StaticFieldSite) (raw : RawInstruction) : + Option Instruction := do + if let some word := platformField raw then + guard (profile.littleEndian && site.isNone) + return .ldc_i4 word + let data ← site + let field := data.descriptor.field + guard (raw.operand == .field field.owner field.index field.type field.name true) + let some size := data.rvaSize | + return ← ordinaryStaticInstruction environment methodOwner data.descriptor raw.opcode + guard (size > 0 && data.descriptor.bytes.length == size && + data.descriptor.alignment ∈ [1, 2, 4, 8] && data.descriptor.initializer.isNone && + data.descriptor.scalar.isNone && !data.descriptor.opaqueValue) + match raw.opcode with + | 127 => return .ldsflda field + | 208 => return .ldtoken (.field field) + | _ => none + +/-- A call's raw claim determines its required instruction. Occupied pcs keep the linked + prefixes for the managed-call checks. An unlinked lowering cannot discard a prefix. -/ +def methodInstruction (profile : FeatureProfile) (environment : CallLayouts) + (methodOwner : String) (statics : Std.TreeMap Nat StaticFieldSite) (occupied : Std.TreeSet Nat) + (roles : Array (Option FoldRole)) (reachable : Array Bool) : + SiteDecoder := + let live := fun pc => (indexedRead? reachable pc).getD false + Instruction.withSiteDecoder roles live fun pc previous raw => + if !methodTokenCall raw.opcode then + if occupied.contains pc || previous.any (isPrefixOpcode ·.opcode) then .unadmitted raw.opcode + else (staticInstruction profile environment methodOwner statics[pc]? raw).getD + (.unadmitted raw.opcode) + else if occupied.contains pc then + (runtimeCatalog.linkedRequirement environment profile raw).decode + (fun _ => Instruction.ofRaw previous raw) + else if previous.any (isPrefixOpcode ·.opcode) then .unadmitted raw.opcode + else + (runtimeCatalog.callRequirement environment profile none raw).decode + (fun _ => Instruction.ofRaw previous raw) + +/-- Decode an ordinary body using only the predecessor required by the value decoder. -/ +private def plainCode (previous : Option RawInstruction) : List RawInstruction → List Instruction + | [] => [] + | raw :: rest => Instruction.ofClosedValue previous raw :: plainCode (some raw) rest + +/-- Ignoring the unused pc in an ordinary body preserves each decoded instruction. -/ +private theorem plainCode_eq (code : List RawInstruction) (previous : Option RawInstruction) + (pc : Nat) : plainCode previous code = + Instruction.ofRawCode previous code (fun _ => Instruction.ofClosedValue) pc := by + induction code generalizing previous pc with + | nil => rfl + | cons first rest ih => exact congrArg (List.cons _) (ih (some first) (pc + 1)) + +#print axioms plainCode_eq + +/-- The closed body's instruction list, including call claims at its occupied and unlinked pcs. -/ +def methodCode (profile : FeatureProfile) (environment : LayoutEnvironment) + (links : List (Nat × Link)) (reachable : List Bool) (closed : RawMethod) : List Instruction := + if plainBody closed.code reachable then plainCode none closed.code + else + let occupied := Std.TreeSet.ofList (links.map Prod.fst) compare + let roles := (foldRoles closed.code).toArray + Instruction.ofRawCode none closed.code (methodInstruction profile environment.callLayouts + closed.owner (firstEntryIndex closed.staticFields) occupied roles reachable.toArray) + +/-- Both decoding paths implement the same reachability, link and fold rule at every pc. -/ +theorem methodCode_eq (profile : FeatureProfile) (environment : LayoutEnvironment) + (links : List (Nat × Link)) (reachable : List Bool) (closed : RawMethod) : + methodCode profile environment links reachable closed = + Instruction.ofRawCode none closed.code (methodInstruction profile environment.callLayouts + closed.owner (firstEntryIndex closed.staticFields) + (Std.TreeSet.ofList (links.map Prod.fst) compare) + (foldRoles closed.code).toArray reachable.toArray) := by + unfold methodCode + split + · rename_i plain + rw [plainCode_eq _ _ 0] + apply List.ext_getElem? + intro pc + simp only [Instruction.ofRawCode_getElem?, Instruction.ofRawAt] + cases found : closed.code[pc]? with + | none => rfl + | some raw => + obtain ⟨live, ordinary, role⟩ := plainBody_at (closed := closed) plain found + simp [methodInstruction, Instruction.withSiteDecoder, live, ordinary, role] + · rfl + +#print axioms methodCode_eq + +/-- The occupied-pc index contains precisely the positions with a link. -/ +private theorem occupied_eq (links : List (Nat × Link)) (pc : Nat) : + (Std.TreeSet.ofList (links.map Prod.fst) compare).contains pc = + (linkLookup links pc).isSome := by + simp only [Std.TreeSet.contains_ofList, linkLookup] + induction links with + | nil => rfl + | cons entry rest ih => + simp only [List.map_cons, List.contains_cons, List.find?_cons] + by_cases same : entry.1 = pc + · simp [same] + · simp only [beq_eq_false_iff_ne.mpr same, beq_eq_false_iff_ne.mpr (Ne.symm same), + Bool.false_or, ih] + +#print axioms occupied_eq + +/-- A method-token opcode with a different operand cannot decode as a linked instruction. -/ +private theorem malformedCall (previous : Option RawInstruction) (raw : RawInstruction) + (token : methodTokenCall raw.opcode = true) (missing : (raw.operand matches .call _) = false) : + Instruction.ofRaw previous raw = .unadmitted raw.opcode := by + obtain ⟨offset, opcode, operand⟩ := raw + simp only [methodTokenCall, Bool.or_eq_true, beq_iff_eq] at token + rcases token with (rfl | rfl) | rfl <;> cases operand <;> try contradiction + all_goals + unfold Instruction.ofRaw Instruction.ofRaw? + split <;> try rfl + all_goals + rename_i isStatic _ + cases isStatic <;> simp [Instruction.instanceField?, FieldRef.ofOperand?, Option.bind] + all_goals repeat' split <;> rfl + +#print axioms malformedCall + +/-- The runtime agreement check holds at every decoded site except a live refused instruction. -/ +theorem RuntimeModelsAgree.of_decoded_site {profile : FeatureProfile} + {env : LayoutEnvironment} {links : List (Nat × Link)} {reachable : List Bool} + {closed : RawMethod} {pc : Nat} {raw : RawInstruction} {live : Bool} {emitted : Instruction} + (found : closed.code[pc]? = some raw) (active : reachable[pc]? = some live) + (decoded : (methodCode profile env links reachable closed)[pc]? = some emitted) + (admitted : live = true → ∀ opcode, emitted ≠ .unadmitted opcode) : + runtimeCatalog.emissionAgrees env.callLayouts profile raw emitted (linkLookup links pc) + ((foldRoles closed.code)[pc]?.join) live = true := by + rw [methodCode_eq, Instruction.ofRawCode_getElem? closed.code pc _, Instruction.ofRawAt, + found] at decoded + have same := Option.some.inj decoded + simp only [methodInstruction, Instruction.withSiteDecoder, indexedRead?_eq, + List.getElem?_toArray, active, Option.getD_some, occupied_eq] at same + cases live with + | false => simp_all [ModelCatalog.emissionAgrees] + | true => + generalize (foldRoles closed.code)[pc]?.join = role at * + simp only [Bool.not_true, Bool.false_eq_true, ite_false] at same + simp only [ModelCatalog.emissionAgrees, ite_true] + cases owned : foldOwnsSite role + · simp only [Bool.false_eq_true, ite_false, ModelCatalog.agreesAt] + cases token : methodTokenCall raw.opcode + · rfl + · simp only [ite_true] + simp only [boundSite, token, Bool.true_or, owned, Bool.not_false, Bool.and_true, + ite_true, Bool.not_true, Bool.false_eq_true, ite_false] at same + cases link : linkLookup links pc with + | none => + simp only [link, Option.isSome_none, Bool.false_eq_true, ite_false] at same + split at same + · exact absurd same.symm (admitted rfl _) + · cases req : runtimeCatalog.callRequirement env.callLayouts profile none raw <;> + simp_all [CallRequirement.decode, CallRequirement.accepts] + exact admitted _ same.symm + | some target => + simp only [link, Option.isSome_some, ite_true] at same + cases operand : raw.operand with + | call site => + simp only [ModelCatalog.callRequirement, siteClaim, token, operand, Bool.not_true, + Bool.false_eq_true, ite_false, Option.isSome_some, ite_true] + unfold ModelCatalog.linkedRequirement at same ⊢ + rw [operand] at same ⊢ + repeat' split at same + all_goals simp_all [CallRequirement.accepts, CallRequirement.decode] + exact admitted _ same.symm + | _ => + have hole := malformedCall (rawBefore (closed.code[·]?) pc) raw token + (by simp [operand]) + simp [ModelCatalog.linkedRequirement, operand, CallRequirement.decode, hole] at same + exact absurd same.symm (admitted rfl _) + · cases role with + | none => contradiction + | some part => + cases part <;> simp_all [foldOwnsSite, Instruction.ofRole?] + all_goals by_cases load : raw.opcode = 20 <;> by_cases valid : raw.operand = .none <;> + simp_all [eq_comm] + +#print axioms RuntimeModelsAgree.of_decoded_site + +end CIL.Extraction diff --git a/CIL/Extraction/CallLayouts.lean b/CIL/Extraction/CallLayouts.lean new file mode 100644 index 000000000..cea50c8f0 --- /dev/null +++ b/CIL/Extraction/CallLayouts.lean @@ -0,0 +1,61 @@ +import CIL.Resolution + +/-! Indexed storage, value shape and alignment for resolving method operands. -/ + +namespace CIL.Extraction + +/-- Positional reads into both layout tables during one method's call checks. -/ +structure CallLayouts extends LayoutEnvironment where + private mk :: + +/-- The reader shares the positional index built with the method's environment. -/ +def _root_.CIL.LayoutEnvironment.callLayouts (environment : LayoutEnvironment) : CallLayouts := + ⟨environment⟩ + +/-- Preparing positional reads preserves the original model environment. -/ +@[simp] theorem callLayouts_environment (environment : LayoutEnvironment) : + environment.callLayouts.toLayoutEnvironment = environment := rfl + +#print axioms callLayouts_environment + +/-- Storage of a closed argument under the indexed raw layout metadata. -/ +def CallLayouts.storage (environment : CallLayouts) : TypeExpr → Option StorageType := + typeStorageAt (layoutAt? environment.indexedRaw) + +/-- A value representation uses the same storage and computed layout as execution. -/ +def CallLayouts.valueShape (environment : CallLayouts) : TypeExpr → ValueShape := + valueShapeAt environment.layouts environment.storage + +/-- Indexed storage reads give the same result as reads from the declared metadata list. -/ +theorem CallLayouts.storage_eq (environment : CallLayouts) : + environment.storage = typeStorage environment.rawLayouts := by + unfold CallLayouts.storage typeStorage + rw [environment.index_eq] + congr 1 + funext index + simp + +#print axioms CallLayouts.storage_eq + +/-- The call checker and execution assign identical shapes to every type. -/ +theorem CallLayouts.valueShape_eq (environment : LayoutEnvironment) (type : TypeExpr) : + environment.callLayouts.valueShape type = environment.valueShape type := by + rw [CallLayouts.valueShape, CallLayouts.storage_eq] + rfl + +#print axioms CallLayouts.valueShape_eq + +/-- The computed alignment of the aggregate with the given identity and index. -/ +def CallLayouts.alignment? (reader : CallLayouts) (type : TypeExpr) : Option Nat := do + let .aggregate _ index := type | none + _root_.guard (reader.storage type == some (.field (.aggregate index))) + return (← layoutAt? reader.layouts index).alignment + +/-- Preparing the same layout environment for execution gives the validation reader. -/ +@[simp] theorem CallLayouts.prepare_eq (reader : CallLayouts) : + reader.toLayoutEnvironment.callLayouts = reader := by + rfl + +#print axioms CallLayouts.prepare_eq + +end CIL.Extraction diff --git a/CIL/Extraction/ClosedMethod.lean b/CIL/Extraction/ClosedMethod.lean new file mode 100644 index 000000000..084eaf33c --- /dev/null +++ b/CIL/Extraction/ClosedMethod.lean @@ -0,0 +1,152 @@ +import CIL.Extraction.CallAgreement + +/-! A closed CIL body, its storage and its reachable sites determine the executable method. -/ + +namespace CIL.Extraction + +/-- The zero value of each local kind, used when the method zero-initializes its locals. -/ +private def initialLocal : LocalKind → Value + | .reference | .span .. | .array _ | .methodPointer _ | .methodTable _ | .object => .nullRef + | .byte => .i8 0 + | .narrow _ | .word32 => .i32 0 + | .word64 => .i64 0 + | .vector128 => .v128 0 + | .vector256 => .v256 0 + | .vector512 => .v512 0 + | .aggregate size => .aggregate (List.replicate size.val 0) + +/-- The value a local of `kind` starts with: its zero when the body initializes locals. -/ +def _root_.CIL.LocalKind.initialValue (initLocals : Bool) (kind : LocalKind) : Value := + if initLocals then initialLocal kind else .unmodeled + +/-- The declared signature a closed raw method implies: its receiver kind, parameter types, + return type and localsinit flag. Instantiation and metadata binding refuse explicit receiver + signatures. -/ +def _root_.CIL.DeclaredSignature.ofRaw (closed : RawMethod) : DeclaredSignature where + receiver := + if closed.hasThis then some (if closed.objectReceiver then .object else .reference) else none + parameters := closed.parameters + returnType := closed.returnType + initLocals := closed.initLocals + +/-- A closed body fails at a live opcode or cannot supply its declared frame or prefixes. -/ +inductive MethodDecodeError where + | liveHole (pc opcode : Nat) + | invalidFrame + | invalidPrefixes + deriving DecidableEq, Repr + +/-- The first reachable hole carries its instruction index and raw opcode. -/ +def firstLiveHole : Nat → List Bool → List Instruction → Option MethodDecodeError + | pc, live :: rest, instruction :: code => + match live, instruction with + | true, .unadmitted opcode => some (.liveHole pc opcode) + | _, _ => firstLiveHole (pc + 1) rest code + | _, _, _ => none + +/-- The method denoted by a closed definition and its argument storage. Its code contains + one instruction per raw site and a hole carrying the opcode at each dead site. The frame + requires supported local kinds, valid argument homes and accesses, and valid prefixes. -/ +def _root_.CIL.Method.ofClosed? (profile : FeatureProfile) (environment : LayoutEnvironment) + (links : List (Nat × Link)) (reachable : List Bool) (closed : RawMethod) + (storage : RawArguments) : Except MethodDecodeError Method := + let code := methodCode profile environment links reachable closed + if let some failure := firstLiveHole 0 reachable code then .error failure else + match storage.locals.mapM (storageKind environment.layouts), + expectedHomes environment.layouts storage reachable with + | some localKinds, some parameterHomes => + let homes := requiredHomeIndices storage reachable + if !rawPrefixesValid closed.returnType closed.code then .error .invalidPrefixes else + if !argumentInstructionsValid storage homes reachable code then .error .invalidFrame else + .ok { + profile := profile, code := code, localKinds := localKinds, + locals := localKinds.map (LocalKind.initialValue closed.initLocals), + opaqueVectorLocals := rawOpaqueLocals closed reachable, + parameterHomes := parameterHomes, declared := DeclaredSignature.ofRaw closed, + environment := environment, links := links, + staticSites := closed.staticDescriptors, + returnsValue := closed.returnType != .void } + | _, _ => .error .invalidFrame + +/-- Closed decoding binds the code and every static descriptor to the same raw method. -/ +theorem _root_.CIL.Method.ofClosed?_fields {profile : FeatureProfile} + {environment : LayoutEnvironment} {links : List (Nat × Link)} {reachable : List Bool} + {closed : RawMethod} {storage : RawArguments} {method : Method} + (decoded : Method.ofClosed? profile environment links reachable closed storage = .ok method) : + method.code = methodCode profile environment links reachable closed ∧ + method.staticSites = closed.staticDescriptors := by + unfold Method.ofClosed? at decoded + cases failure : firstLiveHole 0 reachable (methodCode profile environment links reachable closed) + <;> simp only [failure] at decoded + repeat' split at decoded + all_goals first | contradiction | (cases decoded; exact ⟨rfl, rfl⟩) + +#print axioms Method.ofClosed?_fields + +/-- At a reachable instruction, a hole is incompatible with a successful scan. -/ +private theorem firstLiveHole_ne_none {start pc opcode : Nat} {reachable : List Bool} + {code : List Instruction} (active : reachable[pc]? = some true) + (hole : code[pc]? = some (.unadmitted opcode)) : firstLiveHole start reachable code ≠ none := by + induction reachable generalizing start pc code with + | nil => simp at active + | cons live rest ih => + cases code with + | nil => simp at hole + | cons op ops => + cases pc with + | zero => + simp only [List.getElem?_cons_zero, Option.some.injEq] at active hole + subst live + subst op + simp [firstLiveHole] + | succ pc => + simp only [List.getElem?_cons_succ] at active hole + have tail := ih (start := start + 1) active hole + unfold firstLiveHole + split <;> simp_all + +#print axioms firstLiveHole_ne_none + +/-- Successful closed decoding rules out every reachable hole at its instruction index. -/ +theorem _root_.CIL.Method.ofClosed?_no_live_hole {profile environment links reachable} + {closed storage} {method : Method} {pc opcode : Nat} + (decoded : Method.ofClosed? profile environment links reachable closed storage = .ok method) + (active : reachable[pc]? = some true) : + method.code[pc]? ≠ some (.unadmitted opcode) := by + intro hole + rw [(Method.ofClosed?_fields decoded).1] at hole + cases found : firstLiveHole 0 reachable + (methodCode profile environment links reachable closed) with + | none => exact firstLiveHole_ne_none active hole found + | some failure => simp [Method.ofClosed?, found] at decoded + +#print axioms Method.ofClosed?_no_live_hole + +/-- A feature-checked mask and successful decoding discharge every runtime call comparison. -/ +theorem RuntimeModelsAgree.of_closed {closed storage reachable layouts constants linkAt certificate} + {method : Method} + (decoded : Method.ofClosed? method.profile method.environment method.links reachable closed + storage = .ok method) (features : FeatureFlowValid layouts constants linkAt closed + method.profile certificate reachable method.code) : + RuntimeModelsAgree closed reachable method := by + have maskSize : closed.code.length = reachable.length := by + rw [features.2.2.1, List.length_map, features.1] + have codeSize := features.2.1.symm + refine ⟨maskSize, codeSize, ?_⟩ + apply all3_of_getElem? (by simpa using maskSize) (by simpa using codeSize) + rintro pc ⟨raw, index⟩ live emitted zipped active found + simp only [List.getElem?_zipIdx] at zipped + obtain ⟨instruction, read, eq⟩ := Option.map_eq_some_iff.mp zipped + cases eq + have inside := (List.getElem?_eq_some_iff.mp found).1 + have same := (Method.ofClosed?_fields decoded).1 + have modeled := RuntimeModelsAgree.of_decoded_site read active (same ▸ found) + (fun yes opcode hole => Method.ofClosed?_no_live_hole decoded (yes ▸ active) (hole ▸ found)) + cases live with + | false => simpa [ModelCatalog.emissionAgrees] using modeled + | true => + have present := List.mem_of_getElem? active + simpa [present, linkReads_eq, inside, Method.linkAt] using modeled + +#print axioms RuntimeModelsAgree.of_closed +end CIL.Extraction diff --git a/CIL/Extraction/Constrained.lean b/CIL/Extraction/Constrained.lean index 2ff9e5753..bf8d19126 100644 --- a/CIL/Extraction/Constrained.lean +++ b/CIL/Extraction/Constrained.lean @@ -1,25 +1,25 @@ import CIL.Extraction.Dispatch +import CIL.Extraction.FeatureEffects +import CIL.Extraction.Prefixes /-! Checked constrained-call instruction windows and direct-call lowering. -/ namespace CIL.Extraction -/-- A branch cannot enter the call after skipping its constrained prefix. -/ -private def branchesTo (pc : Nat) (instruction : RawInstruction) : Bool := - match instruction.operand with - | .target target => target == pc - | .targets targets => targets.contains pc - | _ => false - -/-- The selected program entry must be precisely the resolved target, declared by its closed - owner and instantiated at the site's method type arguments, with a receiver argument exactly - when the target is an instance method. -/ +/-- The emitted call names the site's method under a `constrained.` prefix on `receiver`, and the + program entry its link selects is precisely the resolved target, declared by its closed owner + and instantiated at the site's method type arguments, with a receiver argument exactly when + the target is an instance method. -/ private def selectedImplementation (methods : Nat → Option SpecializedMethod) (receiver : TypeExpr) - (site : CallSite) (target : ConstrainedTarget) (emitted : Op) : Bool := + (site : CallSite) (target : ConstrainedTarget) (emitted : Instruction) (link : Option Link) : + Bool := let resolved := target.method let arguments := siteMethodArguments site - match emitted, namedType (ownerBase (target.declaringType receiver)) with - | .call index count, some (scope, name) => match methods index with + match emitted, link, namedType (ownerBase (target.declaringType receiver)) with + | .call method prefixes, some (.direct index), some (scope, name) => + let count := method.argumentCount + method == site.method && prefixes.constrained == some receiver && !prefixes.tail && + match methods index with | none => false | some method => method.arguments == arguments ++ resolved.ownerArguments && @@ -33,11 +33,7 @@ private def selectedImplementation (methods : Nat → Option SpecializedMethod) substituteType arguments resolved.returnType == some method.closed.returnType && resolved.parameters.mapM (substituteType arguments) == some method.closed.parameters && count == resolved.parameters.length + (if resolved.hasThis then 1 else 0) - | _, _ => false - -/-- The call opcode a constrained prefix may precede: `callvirt` for an instance slot and - `call` for a static one. -/ -def constrainedCallOpcode (site : CallSite) : Nat := if site.hasThis then 111 else 40 + | _, _, _ => false /-- Check prefix adjacency, branch integrity, metadata resolution and the emitted target, and that a type test comes from `castclass` or `isinst` against a `castable` class. -/ @@ -45,11 +41,12 @@ def constrainedSiteWindow (resolve : TypeExpr → CallSite → Option Constraine (virtualValid : CallSite → List (TypeExpr × Nat) → Nat → Bool) (castable : TypeExpr → Bool) (methods : Nat → Option SpecializedMethod) (current previous next : Option RawInstruction) (live nextLive : Option Bool) - (emitted : Option Op) (targeted : Nat → Bool) (pc : Nat) : Bool := + (emitted : Option Instruction) (link : Option Link) (targeted : Nat → Bool) (pc : Nat) : + Bool := if live != some true then true else -- A type test stays in the opcode it lowers, against a class its lineage decides. - if let some (.objectTest type _) := emitted then - castable type && current.any fun instruction => instruction.opcode ∈ [116, 117] else + if let some (opcode, type) := emitted.bind typeTestSource then + castable type && current.any fun instruction => instruction.opcode == opcode else match current with | some ⟨_, 65046, .type _⟩ => (match emitted with | some .nop => true | _ => false) && @@ -61,12 +58,14 @@ def constrainedSiteWindow (resolve : TypeExpr → CallSite → Option Constraine | some ⟨_, 65046, .type receiver⟩, some emitted => if pc == 0 || targeted pc || !site.hasThis then false else match resolve receiver site with - | some resolved => selectedImplementation methods receiver site resolved emitted + | some resolved => selectedImplementation methods receiver site resolved emitted link | none => false -- Unprefixed, only a receiver-checked call, whose target the managed-call rule binds, or a -- virtual call whose table re-resolves against the catalog. - | _, some (.callInstance ..) => site.hasThis - | _, some (.callVirtual targets count) => site.hasThis && virtualValid site targets count + | _, some (.callvirt method) => site.hasThis && method == site.method && match link with + | some (.direct _) => true + | some (.virtual targets) => virtualValid site targets method.argumentCount + | _ => false | _, _ => false | some ⟨_, 40, .call site⟩ => match previous with @@ -74,20 +73,108 @@ def constrainedSiteWindow (resolve : TypeExpr → CallSite → Option Constraine if pc == 0 || targeted pc || site.hasThis then false else match emitted, resolve receiver site with | some emitted, some resolved => - selectedImplementation methods receiver site resolved emitted + selectedImplementation methods receiver site resolved emitted link | _, _ => false - | _ => true + -- Unprefixed, a call carries no constrained type. + | _ => match emitted with + | some (.call _ prefixes) => prefixes.constrained.isNone + | _ => true | some ⟨_, 65046, _⟩ | some ⟨_, 111, _⟩ => false | _ => true +/-- The window reads branch entry only at its own pc, and only after a `constrained.` prefix past + the first pc, so any other target lookup agrees with it there. -/ +theorem constrainedSiteWindow_unentered (resolve : TypeExpr → CallSite → Option ConstrainedTarget) + (virtualValid : CallSite → List (TypeExpr × Nat) → Nat → Bool) (castable : TypeExpr → Bool) + (methods : Nat → Option SpecializedMethod) + (current previous next : Option RawInstruction) (live nextLive : Option Bool) + (emitted : Option Instruction) (link : Option Link) (targeted other : Nat → Bool) (pc : Nat) + (unentered : pc = 0 ∨ targeted pc = other pc ∨ previous.any (·.opcode == 65046) = false) : + constrainedSiteWindow resolve virtualValid castable methods current previous next live nextLive + emitted link targeted pc = + constrainedSiteWindow resolve virtualValid castable methods current previous next live + nextLive emitted link other pc := by + unfold constrainedSiteWindow + rcases unentered with rfl | agrees | unprefixed + · repeat' split + all_goals simp_all + · simp only [agrees] + · repeat' split + all_goals simp_all + +#print axioms constrainedSiteWindow_unentered + +/-- A call the rule accepts at a live pc after a `constrained.` prefix has no branch entering + it. -/ +theorem constrainedSiteWindow_untargeted + (resolve : TypeExpr → CallSite → Option ConstrainedTarget) + (virtualValid : CallSite → List (TypeExpr × Nat) → Nat → Bool) (castable : TypeExpr → Bool) + (methods : Nat → Option SpecializedMethod) (prefixOffset callOffset : Nat) + (receiver : TypeExpr) (opcode : Nat) (site : CallSite) (next : Option RawInstruction) + (nextLive : Option Bool) (emitted : Option Instruction) (link : Option Link) + (targeted : Nat → Bool) (pc : Nat) (call : opcode = 111 ∨ opcode = 40) + (accepted : constrainedSiteWindow resolve virtualValid castable methods + (some ⟨callOffset, opcode, .call site⟩) (some ⟨prefixOffset, 65046, .type receiver⟩) next + (some true) nextLive emitted link targeted pc = true) : + targeted pc = false := by + have typeTest : ∀ instruction opcode type, typeTestSource instruction = some (opcode, type) → + opcode = 116 ∨ opcode = 117 := by + intro instruction opcode type found + cases instruction <;> simp only [typeTestSource, Option.some.injEq, Prod.mk.injEq, + reduceCtorEq] at found + · exact .inl found.1.symm + · exact .inr found.1.symm + unfold constrainedSiteWindow at accepted + cases tested : emitted.bind typeTestSource with + | some test => + rcases emitted with _ | instruction + · simp at tested + have := typeTest instruction test.1 test.2 (by simpa using tested) + simp [tested] at accepted + omega + | none => + rcases call with rfl | rfl <;> simp only [tested] at accepted + all_goals (repeat' split at accepted) + all_goals simp_all + +#print axioms constrainedSiteWindow_untargeted + /-- The instruction window supplied through arbitrary optional lookup functions. -/ def constrainedSiteCheckWith (resolve : TypeExpr → CallSite → Option ConstrainedTarget) (virtualValid : CallSite → List (TypeExpr × Nat) → Nat → Bool) (castable : TypeExpr → Bool) (methods : Nat → Option SpecializedMethod) (rawAt : Nat → Option RawInstruction) (liveAt : Nat → Option Bool) - (codeAt : Nat → Option Op) (targeted : Nat → Bool) (pc : Nat) : Bool := + (codeAt : Nat → Option Instruction) (linkAt : Nat → Option Link) (targeted : Nat → Bool) + (pc : Nat) : Bool := constrainedSiteWindow resolve virtualValid castable methods (rawAt pc) (rawAt (pc - 1)) - (rawAt (pc + 1)) (liveAt pc) (liveAt (pc + 1)) (codeAt pc) targeted pc + (rawAt (pc + 1)) (liveAt pc) (liveAt (pc + 1)) (codeAt pc) (linkAt pc) targeted pc + +/-- In a body where no branch enters the instruction after a prefix, the constrained rule's + branch-entry checks refuse nothing, so it accepts what it accepts with no branch targets. -/ +theorem constrainedSiteCheckWith_prefixEntryFree + (resolve : TypeExpr → CallSite → Option ConstrainedTarget) + (virtualValid : CallSite → List (TypeExpr × Nat) → Nat → Bool) (castable : TypeExpr → Bool) + (methods : Nat → Option SpecializedMethod) {code : List RawInstruction} + (free : PrefixEntryFree code) (liveAt : Nat → Option Bool) + (codeAt : Nat → Option Instruction) (linkAt : Nat → Option Link) (pc : Nat) : + constrainedSiteCheckWith resolve virtualValid castable methods (code[·]?) liveAt codeAt linkAt + (fun target => code.any (branchesTo target)) pc = + constrainedSiteCheckWith resolve virtualValid castable methods (code[·]?) liveAt codeAt + linkAt (fun _ => false) pc := by + unfold constrainedSiteCheckWith + apply constrainedSiteWindow_unentered + rcases pc with _ | previousPc + · exact .inl rfl + · have constrainedPrefix : isPrefixOpcode 65046 = true := by decide + simp only [Nat.add_sub_cancel] + cases previous : code[previousPc]? with + | none => exact .inr (.inr (by simp)) + | some before => + by_cases constrained : before.opcode = 65046 + · exact .inr (.inl (free previousPc before previous (constrained ▸ constrainedPrefix))) + · exact .inr (.inr (by simp [constrained])) + +#print axioms constrainedSiteCheckWith_prefixEntryFree /-- The original metadata-list rule presented through optional instruction lookups. -/ def constrainedSiteCheck (types : List DispatchType) (methods : Nat → Option SpecializedMethod) := @@ -96,65 +183,42 @@ def constrainedSiteCheck (types : List DispatchType) (methods : Nat → Option S (castTargetValid (findDispatchType types)) methods /-- The per-site rule stated with optional list lookups, including missing-neighbor rejection. -/ def constrainedSiteValid (types : List DispatchType) (methods : List SpecializedMethod) - (raw : RawMethod) (reachable : List Bool) (code : List Op) (pc : Nat) : Bool := - constrainedSiteCheck types (methods[·]?) (raw.code[·]?) (reachable[·]?) (code[·]?) - (fun target => raw.code.any (branchesTo target)) pc - -/-- All decoded branch destinations, including switch arms and unreachable instructions. -/ -def constrainedBranchTargets (raw : List RawInstruction) : List Nat := - raw.flatMap fun instruction => match instruction.operand with - | .target target => [target] - | .targets targets => targets - | _ => [] - -/-- Collecting branch destinations preserves the rejection rule at every possible pc. -/ -theorem constrainedBranchTargets_contains (raw : List RawInstruction) (pc : Nat) : - (constrainedBranchTargets raw).contains pc = raw.any (branchesTo pc) := by - induction raw with - | nil => rfl - | cons instruction rest ih => - cases instruction with - | mk offset opcode operand => - change _ = (branchesTo pc ⟨offset, opcode, operand⟩ || rest.any (branchesTo pc)) - simp only [constrainedBranchTargets, List.flatMap_cons, List.contains_append] - change (_ || (constrainedBranchTargets rest).contains pc) = _ - rw [ih] - cases operand <;> simp [branchesTo, BEq.beq, eq_comm] - -#print axioms constrainedBranchTargets_contains + (raw : RawMethod) (reachable : List Bool) (emitted : Method) (pc : Nat) : Bool := + constrainedSiteCheck types (methods[·]?) (raw.code[·]?) (reachable[·]?) (emitted.code[·]?) + emitted.linkAt (fun target => raw.code.any (branchesTo target)) pc /-- Traverse adjacent raw instructions and parallel state/code lists without positional lookup. -/ def constrainedSites (resolve : TypeExpr → CallSite → Option ConstrainedTarget) (virtualValid : CallSite → List (TypeExpr × Nat) → Nat → Bool) (castable : TypeExpr → Bool) - (methods : Nat → Option SpecializedMethod) + (methods : Nat → Option SpecializedMethod) (linkAt : Nat → Option Link) (targeted : Nat → Bool) : Nat → Option RawInstruction → - List RawInstruction → List Bool → List Op → Bool + List RawInstruction → List Bool → List Instruction → Bool | pc, previous, instruction :: raw, live :: states, emitted :: code => constrainedSiteWindow resolve virtualValid castable methods (some instruction) previous - raw.head? - (some live) states.head? (some emitted) targeted pc && - constrainedSites resolve virtualValid castable methods targeted (pc + 1) (some instruction) - raw - states code + raw.head? (some live) states.head? (some emitted) (linkAt pc) targeted pc && + constrainedSites resolve virtualValid castable methods linkAt targeted (pc + 1) + (some instruction) raw states code | _, _, [], [], [] => true | _, _, _, _, _ => false /-- Streaming windows agree with the indexed rule on equally long evidence suffixes. -/ theorem constrainedSites_eq (resolve : TypeExpr → CallSite → Option ConstrainedTarget) (virtualValid : CallSite → List (TypeExpr × Nat) → Nat → Bool) (castable : TypeExpr → Bool) - (methods : Nat → Option SpecializedMethod) (targeted : Nat → Bool) - (wholeRaw : List RawInstruction) (wholeStates : List Bool) (wholeCode : List Op) - (raw : List RawInstruction) (states : List Bool) (code : List Op) + (methods : Nat → Option SpecializedMethod) (linkAt : Nat → Option Link) + (targeted : Nat → Bool) (wholeRaw : List RawInstruction) (wholeStates : List Bool) + (wholeCode : List Instruction) + (raw : List RawInstruction) (states : List Bool) (code : List Instruction) (pc : Nat) (previous : Option RawInstruction) (rawSuffix : raw = wholeRaw.drop pc) (stateSuffix : states = wholeStates.drop pc) (codeSuffix : code = wholeCode.drop pc) (prior : previous = wholeRaw[pc - 1]?) (stateLength : raw.length = states.length) (codeLength : raw.length = code.length) : - constrainedSites resolve virtualValid castable methods targeted pc previous raw states code = + constrainedSites resolve virtualValid castable methods linkAt targeted pc previous raw states + code = (List.range' pc raw.length).all (constrainedSiteCheckWith resolve virtualValid castable methods - (wholeRaw[·]?) (wholeStates[·]?) (wholeCode[·]?) targeted) := by + (wholeRaw[·]?) (wholeStates[·]?) (wholeCode[·]?) linkAt targeted) := by induction raw generalizing states code pc previous with | nil => have statesEmpty : states = [] := List.length_eq_zero_iff.mp stateLength.symm @@ -191,7 +255,8 @@ theorem constrainedSites_eq (resolve : TypeExpr → CallSite → Option Constrai rawTail, stateTail, List.head?_drop] #print axioms constrainedSites_eq -/-- Every reachable virtual call is statically resolved; no standalone prefix is admitted. -/ +/-- Every reachable virtual call is statically resolved; no standalone prefix is admitted. A + `call` names the body its pc's link gives, read through one index of the link table. -/ def ConstrainedCallsValid (types : List DispatchType) (methods : List SpecializedMethod) (raw : RawMethod) (reachable : List Bool) (emitted : Method) : Prop := raw.code.length = reachable.length ∧ raw.code.length = emitted.code.length ∧ @@ -201,8 +266,8 @@ def ConstrainedCallsValid (types : List DispatchType) (methods : List Specialize (FlowIndex.ofList methods).get?) (castTargetValid fun identity => ((DispatchIndex.ofTypes types).findType identity).map (·.raw)) - (FlowIndex.ofList methods).get? - (Std.TreeSet.ofList (constrainedBranchTargets raw.code) compare).contains + (FlowIndex.ofList methods).get? (linkIndex emitted) + (Std.TreeSet.ofList (branchTargets raw.code) compare).contains 0 raw.code.head? raw.code reachable emitted.code = true /-- Indexed validation is exactly the original per-pc rule, including malformed evidence. -/ @@ -212,27 +277,27 @@ theorem ConstrainedCallsValid_iff_original (types : List DispatchType) ConstrainedCallsValid types methods raw reachable emitted ↔ raw.code.length = reachable.length ∧ raw.code.length = emitted.code.length ∧ (List.range raw.code.length).all - (constrainedSiteValid types methods raw reachable emitted.code) = true := by + (constrainedSiteValid types methods raw reachable emitted) = true := by have resolve : (DispatchIndex.ofTypes types).resolve = resolveConstrained types := by funext receiver site exact DispatchIndex.resolve_ofTypes types receiver site unfold ConstrainedCallsValid - rw [resolve, DispatchIndex.findType_raw] + rw [resolve, DispatchIndex.findType_raw, linkIndex_eq] by_cases stateLength : raw.code.length = reachable.length · by_cases codeLength : raw.code.length = emitted.code.length · rw [constrainedSites_eq (resolveConstrained types) (virtualTableValid types (findDispatchType types) (FlowIndex.ofList methods).get?) - (castTargetValid (findDispatchType types)) (FlowIndex.ofList methods).get? - (Std.TreeSet.ofList (constrainedBranchTargets raw.code) compare).contains + (castTargetValid (findDispatchType types)) (FlowIndex.ofList methods).get? emitted.linkAt + (Std.TreeSet.ofList (branchTargets raw.code) compare).contains raw.code reachable emitted.code raw.code reachable emitted.code 0 raw.code.head? (by simp) (by simp) (by simp) (by simp [List.head?_eq_getElem?]) stateLength codeLength] have index : (FlowIndex.ofList methods).get? = (methods[·]?) := by funext pc exact FlowIndex.get?_ofList methods pc - have targets : (Std.TreeSet.ofList (constrainedBranchTargets raw.code) compare).contains = + have targets : (Std.TreeSet.ofList (branchTargets raw.code) compare).contains = (fun target => raw.code.any (branchesTo target)) := by funext pc - rw [Std.TreeSet.contains_ofList, constrainedBranchTargets_contains] + rw [Std.TreeSet.contains_ofList, branchTargets_contains] rw [index, targets, ← List.range_eq_range'] rfl · simp [codeLength] @@ -250,7 +315,7 @@ def constrainedTargetSite (receiver : TypeExpr) (site : CallSite) (target : Cons CallSite := { site with target := .managed target.method.token, - declaringType := target.declaringType receiver, + method.owner := target.declaringType receiver, typeArguments := siteMethodArguments site ++ target.method.ownerArguments, ownerArgumentCount := target.method.ownerArguments.length } diff --git a/CIL/Extraction/DecodedDefinition.lean b/CIL/Extraction/DecodedDefinition.lean new file mode 100644 index 000000000..1eebb8ffe --- /dev/null +++ b/CIL/Extraction/DecodedDefinition.lean @@ -0,0 +1,363 @@ +import Lean +import CIL.Extraction.DecodedMethod +import CIL.SymbolicExecution + +/-! +# Methods defined by evaluating the decoder + +`cil_decoded_method name := decoder` evaluates an optional method or a typed closed decoder, +defines `name` as its result and proves that the decoder returns `some name` or `.ok name`. +Compiled code computes the value, which is written as constructors over numerals, so +the definition and the `@[cil_code]` field facts `name_locals`, `name_declared`, ... read as +literals to `cil_instruction_equations`, `cil_resolution_facts` and stepping, and no text an +extractor writes supplies any part of the method. A field whose type is that of a constant +passed to the decoder, such as an extraction's `profile`, `environment` and link table, is written +as that constant. `cil_decoded_method name [operand, ...] := decoder` writes matching field +and type operands as their listed `FieldRef` and `TypeExpr` constants. Stepping can then use +their `cil_field_shape` and `cil_value_shape` facts. Decoded operands share their raw layouts' +identity expressions, and a closed decoder keeps the raw type expressions +in the declared signature. The kernel checks `name_decoder_eq` and every fact. +-/ + +namespace CIL.Extraction.DecodedDefinition + +open Lean Meta Elab Command + +/-- The weak head normal form of a closed term, as the kernel reduces it. -/ +def kernelWhnf (term : Expr) : MetaM Expr := do + ofExceptKernelException (Kernel.whnf (← getEnv) (← getLCtx) term) + +/-- The natural number a closed `Nat` term reduces to. -/ +partial def natValue (term : Expr) : MetaM Nat := do + let value ← kernelWhnf term + if let .lit (.natVal number) := value then return number + if value.isConstOf ``Nat.zero then return 0 + if value.isAppOfArity ``Nat.succ 1 then return (← natValue value.appArg!) + 1 + throwError "cil_decoded_method: {term} does not reduce to a natural number" + +/-- The integer a closed `Int` term reduces to. -/ +def intValue (term : Expr) : MetaM Int := do + let value ← kernelWhnf term + match value.getAppFnArgs with + | (``Int.ofNat, #[number]) => return Int.ofNat (← natValue number) + | (``Int.negSucc, #[number]) => return Int.negSucc (← natValue number) + | _ => throwError "cil_decoded_method: {term} does not reduce to an integer" + +/-- The numeral `value` at `type`, with the instance elaborating the numeral there finds. -/ +def numeral (type : Expr) (value : Int) : MetaM Expr := do + if 0 ≤ value then mkNumeral type value.toNat + else mkAppM ``Neg.neg #[← mkNumeral type (-value).toNat] + +/-- The literal form of the closed term `term` of type `type`: a numeral for a natural + number, integer, bit vector or `Fin`, a string literal or named string, and otherwise the + constructor it reduces to applied to the literal forms of its fields. Parameters and proofs + stay as they are, and a term `named` maps to a constant is written as that constant. Each + node is reduced once, from the term its parent reduced to, so the walk is linear in the size + of the value. -/ +partial def literal (named : Expr → Option Expr) (type term : Expr) : MetaM Expr := do + if let some constant := named term then return constant + let shape ← whnfD type + match shape.getAppFnArgs with + | (``Nat, #[]) => numeral type (← natValue term) + | (``Int, #[]) => numeral type (← intValue term) + | (``BitVec, #[width]) => + numeral type (← natValue (mkApp2 (mkConst ``BitVec.toNat) width term)) + | (``Fin, #[bound]) => numeral type (← natValue (mkApp2 (mkConst ``Fin.val) bound term)) + | (``String, #[]) => + -- A named string, such as `runtimeScope`, stays named: fact commands match it by name. + if term.isConst || term.isStringLit then return term + let .lit (.strVal text) ← kernelWhnf term + | throwError "cil_decoded_method: {term} does not reduce to a string literal" + return mkStrLit text + | _ => + let value ← kernelWhnf term + let .const head levels := value.getAppFn + | throwError "cil_decoded_method: {term} does not reduce to a constructor" + let some (.ctorInfo constructor) := (← getEnv).find? head + | throwError "cil_decoded_method: {term} does not reduce to a constructor" + let arguments := value.getAppArgs + unless arguments.size == constructor.numParams + constructor.numFields do + throwError "cil_decoded_method: {value} is not a full constructor application" + let mut remaining := constructor.type.instantiateLevelParams constructor.levelParams levels + let mut written := #[] + for inRange : index in [0:arguments.size] do + let .forallE _ domain body _ ← whnfD remaining + | throwError "cil_decoded_method: {head} takes fewer arguments than given" + let argument := arguments[index] + let field ← if index < constructor.numParams || (← isProp domain) then pure argument + else literal named domain argument + written := written.push field + remaining := body.instantiate1 field + return mkAppN value.getAppFn written + +/-- Decoded assembly scopes share the runtime table's byte-backed identities, including + member owners and signature types with no layout row. -/ +local instance : ToExpr String where + toExpr text := + if text == runtimeScope then mkConst ``runtimeScope + else if text == intrinsicsScope then mkConst ``intrinsicsScope + else if text == spanMemoryScope then mkConst ``spanMemoryScope + else mkStrLit text + toTypeExpr := mkConst ``String + +deriving instance ToExpr for ComparisonGuard +deriving instance ToExpr for ZeroGuard +deriving instance ToExpr for EnumStorage +deriving instance ToExpr for ArrayElement +deriving instance ToExpr for ArraySpanRange +deriving instance ToExpr for SnapshotShape +deriving instance ToExpr for TransferKind +deriving instance ToExpr for ExceptionType +deriving instance ToExpr for NumericKind +deriving instance ToExpr for Extraction.TypeExpr +deriving instance ToExpr for MetadataName +deriving instance ToExpr for Extraction.FieldRef +deriving instance ToExpr for Extraction.MethodRef +deriving instance ToExpr for CallPrefixes +deriving instance ToExpr for ViewObservation +deriving instance ToExpr for MemoryOp +deriving instance ToExpr for Address +deriving instance ToExpr for ManagedSignature +deriving instance ToExpr for ArmAESIntrinsic +deriving instance ToExpr for AESIntrinsic +deriving instance ToExpr for X86BaseIntrinsic +deriving instance ToExpr for AVX512DQIntrinsic +deriving instance ToExpr for ArmBaseIntrinsic +deriving instance ToExpr for BMI2Intrinsic +deriving instance ToExpr for BMI1Intrinsic +deriving instance ToExpr for AVXIntrinsic +deriving instance ToExpr for AVX512Intrinsic +deriving instance ToExpr for AVX2Intrinsic +deriving instance ToExpr for Value +deriving instance ToExpr for SSEIntrinsic +deriving instance ToExpr for ManagedIdentity +deriving instance ToExpr for AdvSimdIntrinsic +deriving instance ToExpr for StaticInitializer +deriving instance ToExpr for VectorIntrinsic +deriving instance ToExpr for ScalarResultWidth +deriving instance ToExpr for IntegerMath +deriving instance ToExpr for ScalarIntrinsic +deriving instance ToExpr for ManagedTarget +deriving instance ToExpr for Receiver +deriving instance ToExpr for NarrowKind +deriving instance ToExpr for Intrinsic +deriving instance ToExpr for Feature +deriving instance ToExpr for ConvTarget +deriving instance ToExpr for TokenOperand +deriving instance ToExpr for Instruction +deriving instance ToExpr for LocalKind +deriving instance ToExpr for StaticDescriptor +deriving instance ToExpr for DeclaredSignature +deriving instance ToExpr for Link + +/-- The value of the closed term `decoder`, computed by compiled code. -/ +unsafe def evaluateUnsafe (decoder : Expr) : MetaM (Option Method) := + evalExpr (Option Method) (mkApp (mkConst ``Option [.zero]) (mkConst ``Method)) decoder + +/-- The value of the closed term `decoder`, computed by compiled code. -/ +@[implemented_by evaluateUnsafe] +opaque evaluate (decoder : Expr) : MetaM (Option Method) + +/-- Closed decoding reports the position and opcode of a reachable hole. -/ +private unsafe def evaluateClosedUnsafe (decoder : Expr) : + MetaM (Except MethodDecodeError Method) := do + evalExpr (Except MethodDecodeError Method) (← inferType decoder) decoder + +/-- The typed result of evaluating a closed decoder. -/ +@[implemented_by evaluateClosedUnsafe] +private opaque evaluateClosed (decoder : Expr) : MetaM (Except MethodDecodeError Method) + +/-- The literal of a named field or value type used by a decoded instruction. -/ +unsafe def operandLiteralUnsafe (operand : Expr) : MetaM Expr := do + let type ← inferType operand + if type.isConstOf ``Extraction.FieldRef then + return toExpr (← evalExpr Extraction.FieldRef type operand) + if type.isConstOf ``Extraction.TypeExpr then + return toExpr (← evalExpr Extraction.TypeExpr type operand) + throwError "cil_decoded_method: {operand} must be a field reference or type operand" + +/-- The closed constructor term for an extraction's named field or type operand. -/ +@[implemented_by operandLiteralUnsafe] +opaque operandLiteral (operand : Expr) : MetaM Expr + +/-- Each named environment shares its raw aggregate identity expressions across decoded methods. -/ +private initialize layoutIdentityCache : EnvExtension (NameMap (Std.HashMap Expr Expr)) ← + registerEnvExtension (pure {}) (asyncMode := .local) + +/-- Decoded aggregate names share the raw layout's spelling during model and operand checks. -/ +private def decodedLayoutIdentities (decoder : Expr) (method : Method) : + MetaM (Std.HashMap Expr Expr) := do + let_expr CIL.Method.ofClosed? _ environment _ _ _ _ := decoder | return {} + let .const name _ := environment | return {} + if method.environment.rawLayouts.isEmpty then return {} + if let some identities := (layoutIdentityCache.getState (← getEnv)).find? name then + return identities + let mut entries := mkApp (mkConst ``LayoutEnvironment.rawLayouts) environment + let mut identities := {} + for layout in method.environment.rawLayouts do + let (``List.cons, #[_, entry, tail]) := (← kernelWhnf entries).getAppFnArgs | break + let raw ← kernelWhnf entry + unless raw.isAppOf ``RawLayout.mk do break + let identity := raw.getArg! 0 + unless identity.isStringLit do + identities := identities.insert (mkStrLit layout.identity) identity + entries := tail + modifyEnv (layoutIdentityCache.modifyState · (·.insert name identities)) + return identities + +/-- The term `method` denotes, using the decoder's expressions for fields of matching types. -/ +def methodTerm (passed : Array Expr) (method : Method) : MetaM Expr := do + let written : List (Name × Expr) := [(`code, toExpr method.code), + (`locals, toExpr method.locals), (`localKinds, toExpr method.localKinds), + (`staticSites, toExpr method.staticSites), + (`opaqueVectorLocals, toExpr method.opaqueVectorLocals), + (`parameterHomes, toExpr method.parameterHomes), (`declared, toExpr method.declared), + (`links, toExpr method.links), (`returnsValue, toExpr method.returnsValue)] + let mut fields := #[] + for field in getStructureFields (← getEnv) ``CIL.Method do + let fieldType := (← inferType (mkConst (``CIL.Method ++ field))).bindingBody! + let mut shared := none + for candidate in passed do + if (← inferType candidate) == fieldType then shared := some candidate + match shared, written.lookup field with + | some candidate, _ => fields := fields.push candidate + | none, some value => fields := fields.push value + | none, none => throwError "cil_decoded_method: no term is written for the field {field}" + return mkAppN (mkConst ``CIL.Method.mk) fields + +/-- The term `written` that `literal` gives for the initial value `value` of a local, with a + zero-filled aggregate written as `List.replicate size 0`: the form `LocalKind.initialValue` + and an extraction give, which a caller's hypothesis about making the local names. -/ +def initialLocal (value : Value) (written : Expr) : MetaM Expr := do + let .aggregate bytes := value | return written + unless bytes.all (· == 0) do return written + let bytesType := (← inferType (mkConst ``Value.aggregate)).bindingDomain! + let byteType := bytesType.appArg! + let zeros := mkApp3 (mkConst ``List.replicate [.zero]) byteType + (← mkNumeral (mkConst ``Nat) bytes.length) (← mkNumeral byteType 0) + return mkApp (mkConst ``Value.aggregate) zeros + +/-- The literal list `written` of the initial values `values`, each written by `initialLocal`. -/ +def initialLocals : List Value → Expr → MetaM Expr + | value :: values, written => do + let some (type, head, tail) := written.app3? ``List.cons + | throwError "cil_decoded_method: the locals {written} are not a literal list" + return mkApp3 written.getAppFn type (← initialLocal value head) (← initialLocals values tail) + | [], written => return written + +/-- The fields an extraction states as `@[cil_code]` facts; stepping, `cil_resolution_facts` + and the contract tactics read them by name, as `{method}_{field}`. -/ +def factFields : List Name := + [`profile, `locals, `localKinds, `returnsValue, `staticSites, `declared, `environment, `links, + `opaqueVectorLocals, `parameterHomes] + +/-- Adds the kernel-checked theorem `name : statement` proved by `Eq.refl`, documented by + `doc`; the kernel accepts it only when both sides of `statement` reduce to one term. -/ +def proveByReduction (name : Name) (doc : String) (statement : Expr) : MetaM Unit := do + let some (type, left, _) := statement.eq? + | throwError "cil_decoded_method: {statement} is not an equation" + let declaration := Declaration.thmDecl { + name, levelParams := [], type := statement, + value := mkApp2 (mkConst ``Eq.refl [← getLevel type]) type left } + -- Checked before the command goes on, so a refused fact stops it rather than being logged after + -- the facts that follow it; a window per declaration keeps earlier work out of its budget. + try withOptions (Elab.async.set · false) <| withCurrHeartbeats <| addDecl declaration + catch error => + if error.isInterrupt || error.isRuntime then throw error + throwError "cil_decoded_method: the kernel does not reduce both sides of {name} to one term" + addDocStringCore name doc + +/-- The decoder's constant arguments and its declared signature with the raw parameter types. -/ +private def decoderFields (decoder : Expr) : MetaM (Array Expr) := do + let inputs := decoder.getAppArgs.filter (·.isConst) + let_expr Method.ofClosed? _ _ _ _ closed _ := decoder | return inputs + let signature ← kernelWhnf (mkApp (mkConst ``DeclaredSignature.ofRaw) closed) + let fields ← signature.getAppArgs.mapM kernelWhnf + return inputs.push (mkAppN signature.getAppFn fields) + +/-- The field and type constants a decoded method names in its operands. -/ +syntax decodedOperands := "[" ident,* "]" + +/-- `cil_decoded_method name [operand, ...] := decoder` defines `name` as the method the closed + decoder evaluates to, with each listed field or type operand + written as its constant and each zero-initialized struct local as `List.replicate size 0`. + It proves the successful decoder equation and one tagged fact for each field in `factFields`. -/ +syntax (name := decodedMethod) + "cil_decoded_method " ident (decodedOperands)? " := " term : command + +elab_rules : command + | `(cil_decoded_method $nameSyntax:ident $[$fieldsSyntax:decodedOperands]? := $decoderSyntax) => + liftTermElabM do + let methodType := mkConst ``CIL.Method + let optionType := mkApp (mkConst ``Option [.zero]) methodType + let closedType := mkApp2 (mkConst ``Except [.zero, .zero]) + (mkConst ``MethodDecodeError) methodType + let decoder ← Term.elabTerm decoderSyntax (some optionType) + Term.synthesizeSyntheticMVarsNoPostponing + let decoder ← instantiateMVars decoder + if decoder.hasMVar || decoder.hasFVar then + throwError "cil_decoded_method: the decoder must be a closed term{indentExpr decoder}" + let some simpSet ← getSimpExtension? `cil_code + | throwError "cil_decoded_method: the cil_code simp set is not registered" + let name := (← getCurrNamespace) ++ nameSyntax.getId + let passed ← decoderFields decoder + let operandIdents : Array Ident ← match fieldsSyntax with + | none => pure #[] + | some listedSyntax => + match listedSyntax with + | `(decodedOperands| [$listed,*]) => pure listed.getElems + | _ => throwError "cil_decoded_method: malformed operand list {listedSyntax}" + let mut operandConstants : Std.HashMap Expr Expr := {} + let mut operandNames := #[] + for operand in operandIdents do + let operandName ← realizeGlobalConstNoOverloadWithInfo operand + let constant := mkConst operandName + operandConstants := operandConstants.insert (← operandLiteral constant) constant + operandNames := operandNames.push operandName + let (method, success) ← if ← isDefEq (← inferType decoder) closedType then do + match ← evaluateClosed decoder with + | .error reason => throwError "cil_decoded_method: {repr reason}" + | .ok method => pure (method, mkApp2 (mkConst ``Except.ok [.zero, .zero]) + (mkConst ``MethodDecodeError) methodType) + else do + unless ← isDefEq (← inferType decoder) optionType do + throwError "cil_decoded_method: expected an optional method or closed decoder result" + let some method ← evaluate decoder + | throwError "cil_decoded_method: the decoder gives no method{indentExpr decoder}" + pure (method, mkApp (mkConst ``Option.some [.zero]) methodType) + let identities ← decodedLayoutIdentities decoder method + let named (term : Expr) : Option Expr := + if passed.contains term then some term + else operandConstants.get? term <|> identities.get? term + let written ← literal named methodType (← methodTerm passed method) + let fields := getStructureFields (← getEnv) ``CIL.Method + let some localsPosition := fields.idxOf? `locals + | throwError "cil_decoded_method: CIL.Method has no field locals" + let arguments := written.getAppArgs + let value := mkAppN written.getAppFn <| arguments.set! localsPosition + (← initialLocals method.locals arguments[localsPosition]!) + let used := value.getUsedConstantsAsSet + for operandName in operandNames do + unless used.contains operandName do + let label := if (← inferType (mkConst operandName)).isConstOf ``Extraction.FieldRef then + "field" else "type" + throwError "cil_decoded_method: the decoded method has no {label} operand {operandName}" + addAndCompile <| .defnDecl { + name, levelParams := [], type := methodType, value, + hints := .regular (getMaxHeight (← getEnv) value + 1), safety := .safe } + -- Without it, unfolding the method in the module that defines it, as a batch does, fails. + enableRealizationsForConst name + addDeclarationRangesFromSyntax name (← getRef) nameSyntax + addDocStringCore name s!"The method `{decoderSyntax}` decodes to." + let method := mkConst name + proveByReduction (name.appendAfter "_decoder_eq") s!"`{name}` is the decoded method." + (← mkEq decoder (mkApp success method)) + for field in factFields do + let some position := fields.idxOf? field + | throwError "cil_decoded_method: CIL.Method has no field {field}" + let fact := name.appendAfter s!"_{field}" + proveByReduction fact s!"The {field} of `{name}`." + (← mkEq (mkApp (mkConst (``CIL.Method ++ field)) method) value.getAppArgs[position]!) + addSimpTheorem simpSet fact (post := true) (inv := false) .global (eval_prio default) + +end CIL.Extraction.DecodedDefinition diff --git a/CIL/Extraction/DecodedMethod.lean b/CIL/Extraction/DecodedMethod.lean new file mode 100644 index 000000000..f762b7f68 --- /dev/null +++ b/CIL/Extraction/DecodedMethod.lean @@ -0,0 +1,246 @@ +import CIL.Extraction.ValueDecoding +import CIL.Extraction.Translation +import CIL.Extraction.Prefixes + +/-! +# Methods decoded from raw definitions + +`Method.ofRaw?` decodes a raw definition at its type arguments under its reachability mask. +Live sites use the instantiated operands and the preceding instruction. A dead site is a hole +carrying its raw opcode. The same definition supplies the signature, local kinds and live homes. +An unlinked call resolves against the runtime catalog or its named lowering family. The +resolver uses the method's layouts, profile and raw fold role. The first live hole prevents +method construction and identifies the pc and opcode in the closed decoder's error. +`Method.ofBody?` reads the code and init flag from pinned body bytes instead of the definition, +and `decodedProgram?` checks each method and the static values and identities of the program. +`Method.ofBody?_of_decodes` shows that for a definition whose body check passes, the method of +the bytes is the method of the definition, so a proof about the decoded program needs no +translator output. +-/ + +namespace CIL.Extraction + +/-- The method a raw definition denotes at its type arguments, with the supplied link table. -/ +def _root_.CIL.Method.ofRaw? (profile : FeatureProfile) (environment : LayoutEnvironment) + (links : List (Nat × Link)) (reachable : List Bool) (source : RawMethod) + (arguments : List TypeExpr) : Option Method := do + let closed ← instantiateMethod environment.rawLayouts source arguments + let storage ← closedArguments environment.rawLayouts closed + (Method.ofClosed? profile environment links reachable closed storage).toOption + +/-- A validated specialization gives the closed definition and storage for raw decoding. -/ +theorem SpecializationValid.ofRaw?_eq_ofClosed? {environment : LayoutEnvironment} + {source closed : RawMethod} {arguments : List TypeExpr} {storage : RawArguments} + (valid : SpecializationValid environment.rawLayouts source arguments closed storage) + (profile : FeatureProfile) (links : List (Nat × Link)) (reachable : List Bool) : + Method.ofRaw? profile environment links reachable source arguments = + (Method.ofClosed? profile environment links reachable closed storage).toOption := by + simp only [Method.ofRaw?, valid.1, valid.2, bind, Option.bind] + +#print axioms SpecializationValid.ofRaw?_eq_ofClosed? + +/-- The method pinned body bytes denote with link table `links`: `checkBody` checks the + definition's metadata, and the code and init flag are the ones decoded from the bytes. -/ +def _root_.CIL.Method.ofBody? (profile : FeatureProfile) (environment : LayoutEnvironment) + (links : List (Nat × Link)) (reachable : List Bool) (body : BodyEvidence) + (original : RawMethod) (arguments : List TypeExpr) : Option Method := do + let decoded ← (checkBody body original arguments).toOption + Method.ofRaw? profile environment links reachable + { original with code := decoded.code, initLocals := decoded.initLocals } arguments + +/-- The feature certificate checks this method's mask and code against its pinned body's + specialization, using the constant returns of the surrounding records. -/ +abbrev MethodEvidence.MaskValid (item : MethodEvidence) (all : List MethodEvidence) + (method : Method) : Prop := + bodyMatches item.body item.original item.arguments = true ∧ + instantiateMethod method.environment.rawLayouts item.original item.arguments = some item.closed ∧ + FeatureFlowValid method.environment.rawLayouts.toArray (evidenceConstants all) (linkIndex method) + item.closed method.profile item.featureStates item.reachable method.code + +/-- A validated record checks the mask against the method and its pinned, specialized body. -/ +theorem MethodEvidenceValid.maskValid {rawLayouts layouts pins platform all imports item method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) : + item.MaskValid all method := by + obtain ⟨specialized, -, -, features, -, -, -, -, -, -, -, -, -, body, environment, -⟩ := valid + unfold MethodEvidence.MaskValid + rw [show method.environment = _ from environment] + exact ⟨body.2.2.2.2.2.2.2, specialized.1, features⟩ + +#print axioms MethodEvidenceValid.maskValid + +/-- The program binds each static identity and value and each method's mask and call targets. -/ +def programBindingsValid (evidence : List MethodEvidence) (program : Program) + (imports : List ImportSlot := []) : Bool := + let methods := specializationTable evidence + decide (StaticValuesValid program ∧ StaticIdentitiesValid program (evidence.map (·.body))) && + (evidence.zip program).all fun (item, method) => + decide (item.MaskValid evidence method ∧ + ManagedCallsValid methods imports (callModelled method) + (lowerConstrainedRaw item.dispatchTypes item.closed) item.reachable method ∧ + ConstrainedCallsValid item.dispatchTypes methods item.closed item.reachable method) + +/-- A validated translation checks each mask and call binding against the pinned, closed body. -/ +theorem TranslationValid.programBindingsValid {rawLayouts layouts pins platform evidence imports program} + (valid : TranslationValid rawLayouts layouts pins platform evidence imports program) : + programBindingsValid evidence program imports = true := by + apply Bool.and_eq_true_iff.mpr + refine ⟨decide_eq_true ⟨valid.2.2.2.1, valid.2.2.2.2.1⟩, ?_⟩ + apply List.all_eq_true.mpr + rintro ⟨item, method⟩ member + obtain ⟨index, found⟩ := List.mem_iff_getElem?.mp member + obtain ⟨recorded, compiled⟩ := List.getElem?_zip_eq_some.mp found + obtain ⟨bound, rfl⟩ := List.getElem?_eq_some_iff.mp recorded + have checked := translation_method_checked valid ⟨index, bound⟩ method compiled + have mask := checked.maskValid + obtain ⟨-, -, -, -, calls, -, -, -, -, -, -, -, dispatch, -⟩ := checked + exact decide_eq_true ⟨mask, calls, dispatch⟩ + +#print axioms TranslationValid.programBindingsValid + +/-- The pinned bodies determine the program. Its masks, calls, static identities and initializer + results agree with the supplied evidence and import slots. -/ +def decodedProgram? (profile : FeatureProfile) (environment : LayoutEnvironment) + (evidence : List MethodEvidence) (imports : List ImportSlot := []) : Option Program := + (evidence.mapM fun item => + Method.ofBody? profile environment item.links item.reachable item.body item.original + item.arguments).filter fun program => programBindingsValid evidence program imports + +/-- A decoded program validates initializer results and the identity of every static field. -/ +theorem decodedProgram?_statics {profile : FeatureProfile} {environment : LayoutEnvironment} + {evidence : List MethodEvidence} {imports : List ImportSlot} {program : Program} + (decoded : decodedProgram? profile environment evidence imports = some program) : + StaticValuesValid program ∧ StaticIdentitiesValid program (evidence.map (·.body)) := + of_decide_eq_true (Bool.and_eq_true_iff.mp (Option.filter_eq_some_iff.mp decoded).2).1 + +#print axioms decodedProgram?_statics + +/-- A body that decodes to `method` passes `checkBody` with the method's own code and init flag. -/ +theorem BodyDecodes.code {pins : List AssemblyPin} {platform : PlatformTable} + {pin : AssemblyPin} {body : BodyEvidence} + {method : RawMethod} {arguments : List TypeExpr} {signature : String} + {calls : BodyImports} + (decodes : BodyDecodes pins platform pin body method arguments signature calls) : + ∃ decoded, checkBody body method arguments = .ok decoded ∧ + decoded.code = method.code ∧ decoded.initLocals = method.initLocals := by + have matched := decodes.2.2.2.2.2.2.2 + unfold bodyMatches at matched + split at matched + · next decoded checks => + simp only [Bool.and_eq_true, beq_iff_eq] at matched + exact ⟨decoded, checks, matched.1.2, matched.1.1.2⟩ + · exact absurd matched Bool.false_ne_true + +#print axioms BodyDecodes.code + +/-- No branch of a method whose body decodes enters the instruction after a prefix. -/ +theorem BodyDecodes.prefixEntryFree {pins : List AssemblyPin} {platform : PlatformTable} + {pin : AssemblyPin} + {body : BodyEvidence} {method : RawMethod} {arguments : List TypeExpr} {signature : String} + {calls : BodyImports} + (decodes : BodyDecodes pins platform pin body method arguments signature calls) : + PrefixEntryFree method.code := by + obtain ⟨decoded, checks, sameCode, _⟩ := BodyDecodes.code decodes + exact sameCode ▸ checkBody_prefixEntryFree checks + +#print axioms BodyDecodes.prefixEntryFree + +/-- The closed code of a method's evidence has no branch into the instruction after a prefix: + its body decodes, and instantiation keeps every opcode and branch destination. -/ +theorem MethodEvidenceValid.prefixEntryFree {rawLayouts : List RawLayout} + {layouts : Array ComputedLayout} {pins : List AssemblyPin} {platform : PlatformTable} + {all : List MethodEvidence} + {imports : List ImportSlot} {item : MethodEvidence} {method : Method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) : + PrefixEntryFree item.closed.code := by + obtain ⟨specialized, -, -, -, -, -, -, -, -, -, -, -, -, decodes, -, -⟩ := valid + exact instantiateMethod_prefixEntryFree specialized.1 decodes.prefixEntryFree + +#print axioms MethodEvidenceValid.prefixEntryFree + +/-- The instantiated code of a definition whose body the check accepts, read from that body, + has no branch into the instruction after a prefix. -/ +theorem checkBody_instantiated_prefixEntryFree {layouts : List RawLayout} {body : BodyEvidence} + {original closed : RawMethod} {arguments : List TypeExpr} {decoded : DecodedBody} + (checked : checkBody body original arguments = .ok decoded) + (instantiated : instantiateMethod layouts + { original with code := decoded.code, initLocals := decoded.initLocals } arguments = + some closed) : + PrefixEntryFree closed.code := + instantiateMethod_prefixEntryFree instantiated (checkBody_prefixEntryFree checked) + +#print axioms checkBody_instantiated_prefixEntryFree + +/-- For a body that decodes to `original`, the method of the bytes is the method of the + definition. -/ +theorem _root_.CIL.Method.ofBody?_of_decodes {pins : List AssemblyPin} {platform : PlatformTable} + {pin : AssemblyPin} + {body : BodyEvidence} {original : RawMethod} {arguments : List TypeExpr} + {signature : String} {profile : FeatureProfile} {environment : LayoutEnvironment} + {links : List (Nat × Link)} {reachable : List Bool} + {calls : BodyImports} + (decodes : BodyDecodes pins platform pin body original arguments signature calls) : + Method.ofBody? profile environment links reachable body original arguments = + Method.ofRaw? profile environment links reachable original arguments := by + obtain ⟨decoded, checks, sameCode, sameInit⟩ := BodyDecodes.code decodes + unfold Method.ofBody? + rw [checks] + show Method.ofRaw? profile environment links reachable + { original with code := decoded.code, initLocals := decoded.initLocals } arguments = _ + rw [sameCode, sameInit] + +#print axioms Method.ofBody?_of_decodes + +/-- The pinned body and metadata of a validated record decode to the entire executable method. -/ +theorem MethodEvidenceValid.ofBody? {rawLayouts layouts pins platform all imports item method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) : + Method.ofBody? method.profile method.environment item.links item.reachable item.body + item.original item.arguments = some method := by + have decoded := valid.decoded + obtain ⟨specialized, -, -, -, -, -, -, -, -, -, -, -, -, body, environment, -⟩ := valid + have storage : SpecializationValid method.environment.rawLayouts item.original item.arguments + item.closed item.parameterInput := by + rw [show method.environment = _ from environment] + exact specialized + rw [Method.ofBody?_of_decodes body, storage.ofRaw?_eq_ofClosed?, decoded] + rfl + +#print axioms MethodEvidenceValid.ofBody? + +/-- A decoded method contains exactly the instructions of its closed definition. -/ +theorem _root_.CIL.Method.ofClosed?_code {profile : FeatureProfile} + {environment : LayoutEnvironment} {links : List (Nat × Link)} {reachable : List Bool} + {closed : RawMethod} {storage : RawArguments} {method : Method} + (decoded : Method.ofClosed? profile environment links reachable closed storage = .ok method) : + method.code = methodCode profile environment links reachable closed := + (Method.ofClosed?_fields decoded).1 + +#print axioms Method.ofClosed?_code + +/-- Each pc of a decoded method holds what `Instruction.ofRawAt` decodes at that pc of the + instantiated definition's code, so the method has one instruction per raw instruction and a + type test names the type argument and a field access the instantiated field. -/ +theorem _root_.CIL.Method.ofRaw?_code {profile : FeatureProfile} + {environment : LayoutEnvironment} {links : List (Nat × Link)} {reachable : List Bool} + {source : RawMethod} {arguments : List TypeExpr} {method : Method} + (decoded : Method.ofRaw? profile environment links reachable source arguments = some method) : + ∃ closed, instantiateMethod environment.rawLayouts source arguments = some closed ∧ + ∀ pc, method.code[pc]? = Instruction.ofRawAt closed.code pc + (methodInstruction profile environment.callLayouts closed.owner + (firstEntryIndex closed.staticFields) + (Std.TreeSet.ofList (links.map Prod.fst)) + (foldRoles closed.code).toArray reachable.toArray) := by + unfold Method.ofRaw? at decoded + simp only [Option.bind_eq_bind, Option.bind_eq_some_iff] at decoded + obtain ⟨closed, instantiated, storage, _, built⟩ := decoded + refine ⟨closed, instantiated, fun pc => ?_⟩ + cases checked : Method.ofClosed? profile environment links reachable closed storage with + | error => rw [checked] at built; cases built + | ok value => + rw [checked] at built + cases built + rw [Method.ofClosed?_code checked, methodCode_eq, + Instruction.ofRawCode_getElem? closed.code pc _] + +#print axioms Method.ofRaw?_code + +end CIL.Extraction diff --git a/CIL/Extraction/Dispatch.lean b/CIL/Extraction/Dispatch.lean index cd8a19270..5f4a22d8e 100644 --- a/CIL/Extraction/Dispatch.lean +++ b/CIL/Extraction/Dispatch.lean @@ -59,10 +59,6 @@ private def unique : List α → Option α | [value] => some value | _ => none -/-- The method type arguments of a call: the prefix of `typeArguments` before the owner's. -/ -def siteMethodArguments (site : CallSite) : List TypeExpr := - site.typeArguments.take (site.typeArguments.length - site.ownerArgumentCount) - /-- The ECMA-335 calling-convention bit of a generic method signature. -/ private def genericCallingConvention : Nat := 16 @@ -84,21 +80,28 @@ def ownerBase : TypeExpr → TypeExpr | .apply owner _ => ownerBase owner | type => type -/-- Generic dispatch metadata must name precisely the closed receiver specialization. -/ +/-- A generic implementation must name the value type and arguments of its struct receiver. -/ def dispatchOwnerMatches (receiver : TypeExpr) (target : DispatchMethod) : Bool := if target.ownerArguments.isEmpty then target.owner == .void else - match receiver with - | .aggregate identity _ => genericOwnerIdentity target.owner target.ownerArguments == some identity - | _ => false - -/-- A closed struct slot uses its receiver's layout identity; interface identities stay structural. -/ -private def declaringIdentity (receiver : TypeExpr) (site : CallSite) : TypeExpr := + match receiver, target.owner with + | .aggregate identity _, owner@(.constructor ..) => + genericOwnerIdentity owner target.ownerArguments == some identity + | _, _ => false + +/-- A matching struct head denotes the receiver only with at least one owner argument. + A reference-type head at that identity conflicts. Other slots retain their declared type. -/ +private def declaringIdentity (receiver : TypeExpr) (site : CallSite) : Option TypeExpr := match receiver with | .aggregate identity _ => - if !site.typeArguments.isEmpty && - genericOwnerIdentity (ownerBase site.declaringType) site.typeArguments == some identity - then receiver else site.declaringType - | _ => site.declaringType + -- A generic owner containing the receiver as an argument has a strictly longer identity. + if site.typeArguments.contains receiver then some site.declaringType else + let owner := ownerBase site.declaringType + if genericOwnerIdentity owner site.typeArguments == some identity then + match owner with + | .constructor .. => if site.ownerArgumentCount > 0 then some receiver else none + | _ => none + else some site.declaringType + | _ => some site.declaringType /-- Owner arguments are the site's closed owner arguments; method arguments precede them, must be closed, and are admitted only for static slots. -/ @@ -235,14 +238,15 @@ def resolveConstrained (types : List DispatchType) (receiver : TypeExpr) (site : let .managed token := site.target | none let concrete ← unique (types.filter fun type => type.identity == receiver) if !concrete.valueType || concrete.interfaceType || !concrete.pinned then none else pure () - let declaring ← unique (types.filter fun type => type.identity == declaringIdentity receiver site) + let identity <- declaringIdentity receiver site + let declaring <- unique (types.filter fun type => type.identity == identity) if !declaring.pinned then none else pure () let slot ← unique (declaring.methods.filter fun method => method.token == token) if slot.name != site.name || !signatureMatches slot site then none else pure () - let target ← if receiver == declaring.identity then + let target <- if receiver == identity then (if site.hasThis then some (.implementation slot) else none) else do if !declaring.interfaceType || !slot.isVirtual || - !concrete.interfaces.contains declaring.identity then none else pure () + !concrete.interfaces.contains identity then none else pure () selectTarget site slot (concrete.methods.filter fun method => method.overrides.contains (token, site.declaringType)) (concrete.methods.filter fun method => @@ -341,28 +345,6 @@ def castTargetValid (find : TypeExpr → Option DispatchType) (target : TypeExpr entry.pinned && !entry.valueType && !entry.interfaceType | _ => false - -/-- Ordered candidate buckets retain every matching metadata entry. -/ -private def groupIndex {α κ : Type} [Ord κ] (key : α → κ) : - List α → Std.TreeMap κ (List α) - | [] => ∅ - | value :: rest => - let index := groupIndex key rest - index.insert (key value) (value :: index.getD (key value) []) -/-- Each bucket is exactly the corresponding ordered filter of the input. -/ -private theorem groupIndex_get {α κ : Type} [Ord κ] [Std.TransOrd κ] [Std.LawfulEqOrd κ] - [BEq κ] [LawfulBEq κ] (key : α → κ) (values : List α) (query : κ) : - (groupIndex key values).getD query [] = values.filter (fun value => key value == query) := by - induction values with - | nil => simp [groupIndex] - | cons value rest ih => - simp only [groupIndex, Std.TreeMap.getD_insert, Std.compare_eq_iff_eq] - by_cases same : key value = query - · simp [same, ih] - · simp [same, ih] - -#print axioms groupIndex_get - /-- Add one candidate at each advertised key, without duplicating repeated keys. -/ private def insertKeys (base : Std.TreeMap Nat (List α)) (value : α) : List Nat → Std.TreeMap Nat (List α) @@ -398,35 +380,6 @@ private theorem multiGroup_get (keys : α → List Nat) (values : List α) (quer #print axioms multiGroup_get -/-- Linked UTF-8 bytes avoid indexed string decoding while comparing dispatch keys. -/ -private def keyBytes (text : String) : List UInt8 := text.toByteArray.data.toList - -/-- Constructor-delimited type buckets retain scopes and aggregate positions. Exact identity - checks below remain mandatory even if two encodings were to share a bucket. -/ -private def typeKeyInto : TypeExpr → List (List UInt8) → List (List UInt8) - | .void, rest => keyBytes "void" :: rest - | .atom scope name, rest => keyBytes "atom" :: keyBytes scope :: keyBytes name :: rest - | .aggregate identity index, rest => - keyBytes "aggregate" :: keyBytes identity :: keyBytes (toString index) :: rest - | .enum identity underlying, rest => - keyBytes "enum" :: keyBytes identity :: typeKeyInto underlying rest - | .parameter index, rest => keyBytes "parameter" :: keyBytes (toString index) :: rest - | .byref element, rest => keyBytes "byref" :: typeKeyInto element rest - | .pointer element, rest => keyBytes "pointer" :: typeKeyInto element rest - | .array element, rest => keyBytes "array" :: typeKeyInto element rest - | .functionPointer result parameters convention hasThis explicitThis, rest => - keyBytes "functionPointer" :: keyBytes (toString convention) :: - keyBytes (toString hasThis) :: keyBytes (toString explicitThis) :: - typeKeyInto result (typeKeyInto parameters rest) - | .signatureParameter head tail, rest => - keyBytes "signatureParameter" :: typeKeyInto head (typeKeyInto tail rest) - | .constructor scope name, rest => - keyBytes "constructor" :: keyBytes scope :: keyBytes name :: rest - | .apply constructor argument, rest => - keyBytes "apply" :: typeKeyInto constructor (typeKeyInto argument rest) - | .object scope name, rest => - keyBytes "object" :: keyBytes scope :: keyBytes name :: rest - /-- Raw metadata and its order-preserving method candidate indexes. -/ structure DispatchTypeIndex where raw : DispatchType @@ -498,14 +451,15 @@ def DispatchIndex.resolve (index : DispatchIndex) (receiver : TypeExpr) (site : let concrete ← index.findType receiver if !concrete.raw.valueType || concrete.raw.interfaceType || !concrete.raw.pinned then none else pure () - let declaring ← index.findType (declaringIdentity receiver site) + let identity <- declaringIdentity receiver site + let declaring <- index.findType identity if !declaring.raw.pinned then none else pure () let slot ← unique (declaring.tokens.getD token []) if slot.name != site.name || !signatureMatches slot site then none else pure () - let target ← if receiver == declaring.raw.identity then + let target <- if receiver == identity then (if site.hasThis then some (.implementation slot) else none) else do if !declaring.raw.interfaceType || !slot.isVirtual || - !concrete.raw.interfaces.contains declaring.raw.identity then none else pure () + !concrete.raw.interfaces.contains identity then none else pure () selectTarget site slot ((concrete.overrides.getD token []).filter fun method => method.overrides.contains (token, site.declaringType)) diff --git a/CIL/Extraction/ExactIntrinsics.lean b/CIL/Extraction/ExactIntrinsics.lean deleted file mode 100644 index ca77d8fa5..000000000 --- a/CIL/Extraction/ExactIntrinsics.lean +++ /dev/null @@ -1,565 +0,0 @@ -import CIL.Extraction.Features -import Std.Data.TreeSet.Lemmas -import Lean.Elab.Tactic.BuiltinTactic - -/-! -# Exact vector calls and opaque vector locals - -`ExactIntrinsicsValid` checks selected hardware calls, used by vectorized division and -byte reversal, and lane-independent portable integer vector calls. Each covered call requires -exact raw metadata and the matching emitted intrinsic, available under the method's profile. -Other intrinsic families retain their existing extraction checks. - -`Vector128.CreateScalarUnsafe` leaves the upper half of its result unspecified, so a -method that uses it keeps eligible `Vector128` locals as opaque whole values rather -than bytes; `OpaqueLocalsValid` fixes that set from the raw body. - -Signature strings below use string gaps, so each literal is one unbroken identity. --/ - -namespace CIL.Extraction - -/-- Each admitted intrinsic: owner class, method name, full signature, emitted intrinsic and - arity. -/ -private def exactSignatures : List (String × String × String × Intrinsic × Nat) := [ - ("System.Runtime.Intrinsics.X86.Avx2", "And", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx2::And(\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector256`1)", - .avx2 .band, 2), - ("System.Runtime.Intrinsics.X86.Avx2", "Or", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx2::Or(\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector256`1)", - .avx2 .bor, 2), - ("System.Runtime.Intrinsics.X86.Avx2", "Xor", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx2::Xor(\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector256`1)", - .avx2 .bxor, 2), - ("System.Runtime.Intrinsics.X86.Avx2", "AndNot", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx2::AndNot(\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector256`1)", - .avx2 .andNot, 2), - ("System.Runtime.Intrinsics.X86.Avx2", "ShiftLeftLogical", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx2::ShiftLeftLogical(\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector128`1)", - .avx2 .shl64Count, 2), - ("System.Runtime.Intrinsics.X86.Avx2", "ShiftRightLogical", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx2::ShiftRightLogical(\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector128`1)", - .avx2 .shr64Count, 2), - ("System.Runtime.Intrinsics.X86.Avx512F", "CompareLessThan", - "System.Runtime.Intrinsics.Vector512`1 \ - System.Runtime.Intrinsics.X86.Avx512F::CompareLessThan(\ - System.Runtime.Intrinsics.Vector512`1,\ - System.Runtime.Intrinsics.Vector512`1)", - .avx512 .ltu512, 2), - ("System.Runtime.Intrinsics.X86.Avx512F", "CompareGreaterThan", - "System.Runtime.Intrinsics.Vector512`1 \ - System.Runtime.Intrinsics.X86.Avx512F::CompareGreaterThan(\ - System.Runtime.Intrinsics.Vector512`1,\ - System.Runtime.Intrinsics.Vector512`1)", - .avx512 .gtu512, 2), - ("System.Runtime.Intrinsics.X86.Avx512DQ", "MoveMask", - "System.Int32 \ - System.Runtime.Intrinsics.X86.Avx512DQ::MoveMask(\ - System.Runtime.Intrinsics.Vector512`1)", - .avx512DQ .moveMask512, 1), - ("System.Runtime.Intrinsics.Vector512", "Create", - "System.Runtime.Intrinsics.Vector512`1 \ - System.Runtime.Intrinsics.Vector512::Create(\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector256`1)", - .vector .combine256, 2), - ("System.Runtime.Intrinsics.Vector128", "CreateScalarUnsafe", - "System.Runtime.Intrinsics.Vector128`1 \ - System.Runtime.Intrinsics.Vector128::CreateScalarUnsafe(System.UInt64)", - .vector .createScalarUnsafe64, 1), - ("System.Runtime.Intrinsics.X86.Avx2", "ShiftLeftLogical", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx2::ShiftLeftLogical(\ - System.Runtime.Intrinsics.Vector256`1,System.Byte)", - .avx2 .shl64, 2), - ("System.Runtime.Intrinsics.X86.Avx2", "ShiftRightLogical", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx2::ShiftRightLogical(\ - System.Runtime.Intrinsics.Vector256`1,System.Byte)", - .avx2 .shr64, 2), - ("System.Runtime.Intrinsics.X86.Avx512DQ", "MoveMask", - "System.Int32 \ - System.Runtime.Intrinsics.X86.Avx512DQ::MoveMask(\ - System.Runtime.Intrinsics.Vector256`1)", - .avx512DQ .moveMask64, 1), - ("System.Runtime.Intrinsics.X86.Avx2", "Shuffle", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx2::Shuffle(\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector256`1)", - .avx2 .shuffleBytes, 2), - ("System.Runtime.Intrinsics.X86.Ssse3", "Shuffle", - "System.Runtime.Intrinsics.Vector128`1 \ - System.Runtime.Intrinsics.X86.Ssse3::Shuffle(\ - System.Runtime.Intrinsics.Vector128`1,\ - System.Runtime.Intrinsics.Vector128`1)", - .sse .shuffleBytes, 2), - ("System.Runtime.Intrinsics.X86.Sse2", "Multiply", - "System.Runtime.Intrinsics.Vector128`1 \ - System.Runtime.Intrinsics.X86.Sse2::Multiply(\ - System.Runtime.Intrinsics.Vector128`1,\ - System.Runtime.Intrinsics.Vector128`1)", - .sse .multiplyEven32, 2), - ("System.Runtime.Intrinsics.X86.Aes", "Encrypt", - "System.Runtime.Intrinsics.Vector128`1 \ - System.Runtime.Intrinsics.X86.Aes::Encrypt(\ - System.Runtime.Intrinsics.Vector128`1,\ - System.Runtime.Intrinsics.Vector128`1)", - .aes .encrypt, 2), - ("System.Runtime.Intrinsics.Arm.Aes", "Encrypt", - "System.Runtime.Intrinsics.Vector128`1 \ - System.Runtime.Intrinsics.Arm.Aes::Encrypt(\ - System.Runtime.Intrinsics.Vector128`1,\ - System.Runtime.Intrinsics.Vector128`1)", - .armAes .encrypt, 2), - ("System.Runtime.Intrinsics.Arm.Aes", "MixColumns", - "System.Runtime.Intrinsics.Vector128`1 \ - System.Runtime.Intrinsics.Arm.Aes::MixColumns(\ - System.Runtime.Intrinsics.Vector128`1)", - .armAes .mixColumns, 1), - ("System.Runtime.Intrinsics.Arm.AdvSimd", "ReverseElement8", - "System.Runtime.Intrinsics.Vector128`1 \ - System.Runtime.Intrinsics.Arm.AdvSimd::ReverseElement8(\ - System.Runtime.Intrinsics.Vector128`1)", - .advSimd .reverseElement8, 1), - ("System.Runtime.Intrinsics.X86.Avx512F/VL", "PermuteVar4x64x2", - "System.Runtime.Intrinsics.Vector256`1 \ - System.Runtime.Intrinsics.X86.Avx512F/VL::PermuteVar4x64x2(\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector256`1,\ - System.Runtime.Intrinsics.Vector256`1)", - .avx512 .permuteVar4x64x2, 3)] - -/-- `Vector{width}` with exact scopes. -/ -private def ulongVector (width : String) : TypeExpr := - .apply (.constructor intrinsicsScope ("System.Runtime.Intrinsics.Vector" ++ width ++ "`1")) - (.atom runtimeScope "System.UInt64") - -/-- `Vector{width}` with exact scopes. -/ -private def uintVector (width : String) : TypeExpr := - .apply (.constructor intrinsicsScope ("System.Runtime.Intrinsics.Vector" ++ width ++ "`1")) - (.atom runtimeScope "System.UInt32") - -/-- `Vector{width}` with exact scopes. -/ -private def byteVector (width : String) : TypeExpr := - .apply (.constructor intrinsicsScope ("System.Runtime.Intrinsics.Vector" ++ width ++ "`1")) - (.atom runtimeScope "System.Byte") - -/-- The return and parameter types each admitted intrinsic's call site must declare. -/ -private def exactCallTypes : Intrinsic → Option (TypeExpr × List TypeExpr) - | .avx2 .band | .avx2 .bor | .avx2 .bxor | .avx2 .andNot => - some (ulongVector "256", [ulongVector "256", ulongVector "256"]) - | .avx2 .shl64Count | .avx2 .shr64Count => - some (ulongVector "256", [ulongVector "256", ulongVector "128"]) - | .avx2 .shl64 | .avx2 .shr64 => - some (ulongVector "256", [ulongVector "256", .atom runtimeScope "System.Byte"]) - | .avx512 .ltu512 | .avx512 .gtu512 => - some (ulongVector "512", [ulongVector "512", ulongVector "512"]) - | .avx512DQ .moveMask512 => some (.atom runtimeScope "System.Int32", [ulongVector "512"]) - | .avx512DQ .moveMask64 => some (.atom runtimeScope "System.Int32", [ulongVector "256"]) - | .vector .combine256 => some (ulongVector "512", [ulongVector "256", ulongVector "256"]) - | .vector .createScalarUnsafe64 => - some (ulongVector "128", [.atom runtimeScope "System.UInt64"]) - | .avx2 .shuffleBytes => some (byteVector "256", [byteVector "256", byteVector "256"]) - | .sse .shuffleBytes | .aes .encrypt | .armAes .encrypt => - some (byteVector "128", [byteVector "128", byteVector "128"]) - | .armAes .mixColumns => some (byteVector "128", [byteVector "128"]) - | .sse .multiplyEven32 => some (ulongVector "128", [uintVector "128", uintVector "128"]) - | .advSimd .reverseElement8 => some (ulongVector "128", [ulongVector "128"]) - | .avx512 .permuteVar4x64x2 => - some (ulongVector "256", [ulongVector "256", ulongVector "256", ulongVector "256"]) - | _ => none - -/-- Lane-independent integer operations share vector semantics at each supported width. -/ -private def portableVectorOperation (name : String) (width : Nat) : - Option (Intrinsic × Nat) := - match name with - | "op_BitwiseAnd" => some (.vector (.band width), 2) - | "op_BitwiseOr" => some (.vector (.bor width), 2) - | "op_ExclusiveOr" => some (.vector (.bxor width), 2) - | "op_OnesComplement" => some (.vector (.bnot width), 1) - | "op_Equality" | "EqualsAll" => some (.vector (.equalsAll width), 2) - | "get_Zero" => some (.vector (.zero width), 0) - | "get_AllBitsSet" => some (.vector (.ones width), 0) - | _ => none - -/-- Exact portable vector calls retain their integer element, width, types and generic identity. -/ -private def portableVectorIntrinsic (instruction : RawInstruction) : Option (Intrinsic × Nat) := do - if instruction.opcode != 40 then none else pure () - let .call site := instruction.operand | none - if site.hasThis || !site.returnsValue || site.ownerArgumentCount != 0 then none else pure () - let isStatic := site.name == "EqualsAll" - let vector ← if isStatic then - match site.declaringType, site.typeArguments with - | .atom scope owner, [element] => - if scope != intrinsicsScope then none - else some (TypeExpr.apply (.constructor scope (owner ++ "`1")) element) - | _, _ => none - else if site.typeArguments.isEmpty then some site.declaringType else none - let bytes ← integerVectorWidth vector - if bytes != 16 && bytes != 32 && bytes != 64 then none else pure () - let (intrinsic, arity) ← portableVectorOperation site.name (bytes * 8) - let .apply (.constructor _ owner) (.atom _ element) := vector | none - let staticOwner := "System.Runtime.Intrinsics.Vector" ++ toString (bytes * 8) - let parameterName := owner ++ (if isStatic then "" else "") - let equality := site.name == "op_Equality" || isStatic - let result := if equality then TypeExpr.atom runtimeScope "System.Boolean" else vector - let resultName := if equality then "System.Boolean" else parameterName - let declaringName := if isStatic then staticOwner else owner ++ "<" ++ element ++ ">" - let signature := resultName ++ " " ++ declaringName ++ "::" ++ site.name ++ "(" ++ - String.intercalate "," (List.replicate arity parameterName) ++ ")" - if site.parameterCount != arity || site.parameterTypes != List.replicate arity vector || - site.returnType != result || site.target != .external (intrinsicsScope ++ "::" ++ signature) - then none else some (intrinsic, arity) - -/-- Owner names reserve portable vector calls even when their metadata is malformed. -/ -private def portableVectorOwner (owner : String) : Bool := - owner == "System.Runtime.Intrinsics.Vector128" || - owner == "System.Runtime.Intrinsics.Vector256" || - owner == "System.Runtime.Intrinsics.Vector512" || - owner.startsWith "System.Runtime.Intrinsics.Vector128`1" || - owner.startsWith "System.Runtime.Intrinsics.Vector256`1" || - owner.startsWith "System.Runtime.Intrinsics.Vector512`1" - -/-- The intrinsic and arity of an exact admitted call, including its generic types and identity. -/ -def exactIntrinsic (instruction : RawInstruction) : Option (Intrinsic × Nat) := - portableVectorIntrinsic instruction <|> do - if instruction.opcode != 40 then none else - let .call site := instruction.operand | none - if site.hasThis || !site.returnsValue || !site.typeArguments.isEmpty then none else - let (_, _, _, intrinsic, arity) ← - exactSignatures.find? fun (owner, name, signature, _, arity) => - site.name == name && site.parameterCount == arity && - site.declaringType == .atom intrinsicsScope owner && - site.target == .external (intrinsicsScope ++ "::" ++ signature) - let (returnType, parameterTypes) ← exactCallTypes intrinsic - if site.returnType != returnType || site.parameterTypes != parameterTypes then none else - return (intrinsic, arity) - -/-- Checking names and arities first preserves the original admitted-signature lookup and metadata - checks, without any assumptions on the raw instruction. -/ -theorem exactIntrinsic_eq_original (instruction : RawInstruction) : - exactIntrinsic instruction = (portableVectorIntrinsic instruction <|> do - if instruction.opcode != 40 then none else - let .call site := instruction.operand | none - if site.hasThis || !site.returnsValue || !site.typeArguments.isEmpty then none else - let (_, _, _, intrinsic, arity) ← - exactSignatures.find? fun (owner, name, signature, _, arity) => - site.declaringType == .atom intrinsicsScope owner && site.name == name && - site.parameterCount == arity && - site.target == .external (intrinsicsScope ++ "::" ++ signature) - let (returnType, parameterTypes) ← exactCallTypes intrinsic - if site.returnType != returnType || site.parameterTypes != parameterTypes then none else - return (intrinsic, arity)) := by - simp only [exactIntrinsic, Bool.and_assoc, Bool.and_left_comm, Bool.and_comm] -#print axioms exactIntrinsic_eq_original - -/-- The exact UTF-8 bytes of an identity, including non-ASCII metadata. -/ -private def identityBytes (identity : String) : List UInt8 := - identity.toByteArray.data.toList - -/-- Signature bytes materialized once; the kernel checks their equality to the signature table. -/ -private def exactSignatureBytes : List (String × String × List UInt8) := by - run_tac do - let expression ← Lean.Elab.Term.elabTerm - (← `(exactSignatures.map fun (owner, name, signature, _, _) => - (owner, name, identityBytes signature))) none - Lean.Elab.Term.synthesizeSyntheticMVarsNoPostponing - let expression ← Lean.instantiateMVars expression - let values ← unsafe Lean.Meta.evalExpr (List (String × String × List UInt8)) - (← Lean.Meta.inferType expression) expression - Lean.Elab.Tactic.closeMainGoal `exactSignatureBytes (Lean.toExpr values) - -set_option maxRecDepth 4096 in -/-- The materialized bytes contain exactly the original signatures and owner/name metadata. -/ -private theorem exactSignatureBytes_eq : exactSignatureBytes = - exactSignatures.map (fun (owner, name, signature, _, _) => - (owner, name, identityBytes signature)) := by - decide +kernel -#print axioms exactSignatureBytes_eq - -/-- The unqualified signature of an external target under the exact intrinsic scope. -/ -private def intrinsicSignature (target : MethodId) : Option (List UInt8) := - match target with - | .managed _ => none - | .external identity => - stripIdentityPrefix (identityBytes (intrinsicsScope ++ "::")) (identityBytes identity) - -/-- Removing the common scope preserves exact external identity equality. -/ -private theorem intrinsicSignature_matches (target : MethodId) (signature : String) : - (intrinsicSignature target == some (identityBytes signature)) = - (target == .external (intrinsicsScope ++ "::" ++ signature)) := by - apply Bool.eq_iff_iff.mpr - cases target with - | managed token => simp [intrinsicSignature, beq_iff_eq] - | external identity => - simp only [intrinsicSignature, beq_iff_eq, stripIdentityPrefix_eq, MethodId.external.injEq] - simp only [identityBytes] - rw [← ByteArray.toList_data_append, ← String.toByteArray_append, Array.toList_inj] - constructor - · intro equal - exact String.toByteArray_inj.mp (ByteArray.ext equal) - · intro equal - cases equal - rfl -#print axioms intrinsicSignature_matches - -/-- Split one metadata identity separator without decoding byte-backed strings. -/ -private def splitIdentityOwner : List UInt8 → List UInt8 × List UInt8 - -- ASCII ':' separates the assembly, owner and member identities. - | 58 :: 58 :: tail => ([], tail) - | head :: tail => - let (owner, member) := splitIdentityOwner tail - (head :: owner, member) - | [] => ([], []) - -/-- Family recognition uses both structured metadata and the independently supplied target. -/ -private def portableVectorFamily (instruction : RawInstruction) : Bool := - match instruction.operand with - | .call site => - let owner := match site.declaringType with - | .atom _ owner | .apply (.constructor _ owner) _ => owner - | _ => "" - (portableVectorOwner owner && (portableVectorOperation site.name 128).isSome) || - match intrinsicSignature site.target with - | some signature => - let (signatureOwner, member) := splitIdentityOwner signature - -- ASCII space ends the return type; '<' begins type arguments and '(' begins parameters. - let owner := (signatureOwner.reverse.takeWhile (· != 32)).reverse.takeWhile (· != 60) - let name := member.takeWhile (· != 40) - (["System.Runtime.Intrinsics.Vector128", "System.Runtime.Intrinsics.Vector256", - "System.Runtime.Intrinsics.Vector512", "System.Runtime.Intrinsics.Vector128`1", - "System.Runtime.Intrinsics.Vector256`1", "System.Runtime.Intrinsics.Vector512`1"].any - (fun expected => owner == identityBytes expected)) && - (["op_BitwiseAnd", "op_BitwiseOr", "op_ExclusiveOr", "op_OnesComplement", "op_Equality", - "EqualsAll", "get_Zero", "get_AllBitsSet"].any - (fun expected => name == identityBytes expected)) - | none => false - | _ => false - -/-- A call that resembles an admitted entry by identity, or by owner and name; such a call - must then match an entry exactly. -/ -private def exactCallFamily (instruction : RawInstruction) : Bool := - portableVectorFamily instruction || match instruction.operand with - | .call site => match intrinsicSignature site.target with - | none => exactSignatures.any fun (owner, name, _, _, _) => - site.name == name && match site.declaringType with - | .atom _ declared => declared == owner - | _ => false - | some signature => exactSignatureBytes.any fun (owner, name, expected) => - (some signature == some expected) || - site.name == name && match site.declaringType with - | .atom _ declared => declared == owner - | _ => false - | _ => false - -/-- Sharing scope removal preserves family recognition by either full identity or owner/name. -/ -private theorem exactCallFamily_eq_original (instruction : RawInstruction) : - exactCallFamily instruction = (portableVectorFamily instruction || - match instruction.operand with - | .call site => exactSignatures.any fun (owner, name, signature, _, _) => - site.target == .external (intrinsicsScope ++ "::" ++ signature) || - site.name == name && match site.declaringType with - | .atom _ declared => declared == owner - | _ => false - | _ => false) := by - cases operand : instruction.operand <;> simp only [exactCallFamily, operand] - rename_i site - cases signature : intrinsicSignature site.target <;> - simp [exactSignatureBytes_eq, List.any_map, Function.comp_def, - ← intrinsicSignature_matches, signature] -#print axioms exactCallFamily_eq_original - -/-- The intrinsic and arity of an emitted admitted intrinsic. -/ -def emittedExactIntrinsic : Op → Option (Intrinsic × Nat) - | .intrinsic intrinsic arity => match intrinsic with - | .avx2 .band | .avx2 .bor | .avx2 .bxor | .avx2 .andNot - | .avx2 .shl64Count | .avx2 .shr64Count | .avx2 .shl64 | .avx2 .shr64 - | .avx512 .ltu512 | .avx512 .gtu512 - | .avx512DQ .moveMask512 | .avx512DQ .moveMask64 | .vector .combine256 - | .vector .createScalarUnsafe64 | .avx2 .shuffleBytes | .sse .shuffleBytes - | .sse .multiplyEven32 | .aes .encrypt | .armAes .encrypt | .armAes .mixColumns - | .advSimd .reverseElement8 | .avx512 .permuteVar4x64x2 - | .vector (.band _) | .vector (.bor _) | .vector (.bxor _) | .vector (.bnot _) - | .vector (.equalsAll _) | .vector (.zero _) | .vector (.ones _) => some (intrinsic, arity) - | _ => none - | _ => none - -/-- An emitted exact intrinsic must match the raw call and be available. Other emitted - operations must not replace a call in the admitted family, including malformed calls. -/ -private def exactSiteMatches (profile : FeatureProfile) (raw : RawInstruction) - (emitted : Op) : Bool := - match emittedExactIntrinsic emitted with - | none => !exactCallFamily raw - | some expected => match exactIntrinsic raw with - | none => false - | some actual => decide (actual = expected) && Intrinsic.available profile expected.1 - -/-- Dispatching on the emitted operation preserves every original site check, including - malformed raw metadata, arity mismatches and unavailable intrinsics. -/ -private theorem exactSiteMatches_eq (profile : FeatureProfile) (raw : RawInstruction) - (emitted : Op) : - exactSiteMatches profile raw emitted = - ((if exactCallFamily raw || (emittedExactIntrinsic emitted).isSome then - (exactIntrinsic raw).isSome && - decide (exactIntrinsic raw = emittedExactIntrinsic emitted) - else true) && - match emittedExactIntrinsic emitted with - | some (intrinsic, _) => Intrinsic.available profile intrinsic - | none => true) := by - cases emittedResult : emittedExactIntrinsic emitted with - | none => - cases rawResult : exactIntrinsic raw <;> cases family : exactCallFamily raw <;> - simp [exactSiteMatches, emittedResult, family] - | some expected => - rcases expected with ⟨intrinsic, arity⟩ - cases rawResult : exactIntrinsic raw <;> - simp [exactSiteMatches, emittedResult, rawResult] - -#print axioms exactSiteMatches_eq - -/-- The profile is valid, the three lists have equal length, and at every live pc a - resembling call or an emitted admitted intrinsic requires an exact match between the two, - and every emitted one is available under the profile. -/ -def ExactIntrinsicsValid (closed : RawMethod) (reachable : List Bool) (method : Method) : - Prop := - method.profile.Valid ∧ closed.code.length = reachable.length ∧ - closed.code.length = method.code.length ∧ - all3 (fun raw live emitted => !live || exactSiteMatches method.profile raw emitted) - closed.code reachable method.code = true - -/-- The optimized validator is equivalent to the original signature-family, exact-call - and availability rules for all inputs, including unequal certificate lengths. -/ -theorem ExactIntrinsicsValid_iff_original (closed : RawMethod) (reachable : List Bool) - (method : Method) : - ExactIntrinsicsValid closed reachable method ↔ - let classify := fun instruction : RawInstruction => (portableVectorIntrinsic instruction <|> do - if instruction.opcode != 40 then none else - let .call site := instruction.operand | none - if site.hasThis || !site.returnsValue || !site.typeArguments.isEmpty then none else - let (_, _, _, intrinsic, arity) ← - exactSignatures.find? fun (owner, name, signature, _, arity) => - site.declaringType == .atom intrinsicsScope owner && site.name == name && - site.parameterCount == arity && - site.target == .external (intrinsicsScope ++ "::" ++ signature) - let (returnType, parameterTypes) ← exactCallTypes intrinsic - if site.returnType != returnType || site.parameterTypes != parameterTypes then none else - return (intrinsic, arity)) - let family := fun instruction : RawInstruction => - portableVectorFamily instruction || match instruction.operand with - | .call site => exactSignatures.any fun (owner, name, signature, _, _) => - site.target == .external (intrinsicsScope ++ "::" ++ signature) || - site.name == name && match site.declaringType with - | .atom _ declared => declared == owner - | _ => false - | _ => false - method.profile.Valid ∧ closed.code.length = reachable.length ∧ - closed.code.length = method.code.length ∧ - all3 (fun raw live emitted => !live || - (if family raw || (emittedExactIntrinsic emitted).isSome then - (classify raw).isSome && decide (classify raw = emittedExactIntrinsic emitted) - else true) && - match emittedExactIntrinsic emitted with - | some (intrinsic, _) => Intrinsic.available method.profile intrinsic - | none => true) closed.code reachable method.code = true := by - simp only [ExactIntrinsicsValid, exactSiteMatches_eq, exactIntrinsic_eq_original, - exactCallFamily_eq_original] - -#print axioms ExactIntrinsicsValid_iff_original - -instance (closed : RawMethod) (reachable : List Bool) (method : Method) : - Decidable (ExactIntrinsicsValid closed reachable method) := - inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _)) - -/-- Whether a raw local is the whole-vector type eligible for opaque storage. -/ -private def opaqueLocalType : Option TypeExpr → Bool - | some (.apply (.constructor scope "System.Runtime.Intrinsics.Vector128`1") - (.atom elementScope "System.UInt64")) => - scope == intrinsicsScope && elementScope == runtimeScope - | _ => false - -/-- Every eligible local, in index order; addresses are taken by `ldloca` (18 or 0xFE0D). -/ -def opaqueLocalIndices (closed : RawMethod) : List Nat := - let addressed := Std.TreeSet.ofList (closed.code.filterMap fun instruction => - if instruction.opcode == 18 || instruction.opcode == 65037 then localIndex instruction else none) - ((closed.locals.zipIdx).filter fun (type, index) => - opaqueLocalType (some type) && !addressed.contains index).map Prod.snd - -/-- Indexed opaque-local discovery agrees with direct local lookup and address membership. -/ -theorem opaqueLocalIndices_eq_scan (closed : RawMethod) : - opaqueLocalIndices closed = (List.range closed.locals.length).filter (fun index => - (match (closed.locals[index]? : Option TypeExpr) with - | some (.apply (.constructor scope "System.Runtime.Intrinsics.Vector128`1") - (.atom elementScope "System.UInt64")) => - scope == intrinsicsScope && elementScope == runtimeScope - | _ => false) && !(closed.code.filterMap (fun instruction => - if instruction.opcode == 18 || instruction.opcode == 65037 then localIndex instruction - else none)).contains index) := by - simp only [opaqueLocalIndices, Std.TreeSet.contains_ofList] - rw [List.range_eq_range', ← List.zipIdx_map_snd 0 closed.locals, List.filter_map] - congr 1 - apply List.filter_congr - intro entry present - have found := List.mem_zipIdx_iff_getElem?.mp present - simp only [Function.comp_def, found] - rfl - -#print axioms opaqueLocalIndices_eq_scan -/-- Every claimed opaque local has the emitted whole-vector slot kind. -/ -private def opaqueKindsValid (method : Method) : Bool := - let kinds := FlowIndex.ofList method.localKinds - method.opaqueVectorLocals.all fun index => kinds.get? index == some .vector128 - -/-- No emitted address operation exposes an opaque local's unspecified bytes. -/ -private def opaqueAddressesValid (method : Method) : Bool := - let slots := Std.TreeSet.ofList method.opaqueVectorLocals - method.code.all fun operation => match operation with - | .localAddr index => !slots.contains index - | _ => true -/-- The opaque locals are exactly the eligible ones when the method uses - `CreateScalarUnsafe` and none otherwise; each is a 128-bit vector slot whose address the - emitted code never takes. -/ -def OpaqueLocalsValid (closed : RawMethod) (method : Method) : Prop := - method.opaqueVectorLocals = (if method.code.any (fun op => - match op with - | .intrinsic (.vector .createScalarUnsafe64) _ => true - | _ => false) then opaqueLocalIndices closed else []) ∧ - opaqueKindsValid method = true ∧ opaqueAddressesValid method = true - -/-- Indexed opaque-local checks preserve the original kind and no-address obligations. -/ -theorem OpaqueLocalsValid_iff_scan (closed : RawMethod) (method : Method) : - OpaqueLocalsValid closed method ↔ - method.opaqueVectorLocals = (if method.code.any (fun op => match op with - | .intrinsic (.vector .createScalarUnsafe64) _ => true - | _ => false) then opaqueLocalIndices closed else []) ∧ - method.opaqueVectorLocals.all (fun index => method.localKinds[index]? == some .vector128) = - true ∧ - method.code.all (fun operation => match operation with - | .localAddr index => !(index ∈ method.opaqueVectorLocals) - | _ => true) = true := by - simp only [OpaqueLocalsValid, opaqueKindsValid, opaqueAddressesValid, - FlowIndex.get?_ofList, Std.TreeSet.contains_ofList, List.contains_eq_mem] - -#print axioms OpaqueLocalsValid_iff_scan -instance (closed : RawMethod) (method : Method) : Decidable (OpaqueLocalsValid closed method) := - inferInstanceAs (Decidable (_ ∧ _ ∧ _)) - -end CIL.Extraction diff --git a/CIL/Extraction/FeatureEffects.lean b/CIL/Extraction/FeatureEffects.lean index 9c9d25405..175a53191 100644 --- a/CIL/Extraction/FeatureEffects.lean +++ b/CIL/Extraction/FeatureEffects.lean @@ -6,89 +6,58 @@ import CIL.Instructions The feature-flow check in `Features` matches most raw instructions to their emitted counterpart only by stack effect. This file gives that effect, as `(pops, pushes)`, for each emitted instruction that cannot disturb a tracked local; the rest have none and must -be matched exactly. +be matched exactly. Indirect loads, stores and block operations also meet the exact rule of +`IndirectAccess`. -/ namespace CIL.Extraction -/-- `(pops, pushes)` of each memory operation. -/ -private def memoryFeatureEffect : MemoryOp → Nat × Nat - | .spanCopy _ tryCopy => (2, if tryCopy then 1 else 0) - | .spanFill _ clear => (if clear then 1 else 2, 0) - | .arrayFill _ counted => (if counted then 4 else 2, 0) - | .marshalRead _ trying => (if trying then 2 else 1, 1) - | .binaryRead _ _ _ trying => (if trying then 2 else 1, 1) - | .binaryWrite _ _ trying => (2, if trying then 1 else 0) - | .marshalWrite _ trying => (2, if trying then 1 else 0) - | .pointerTransfer _ _ store => if store then (2, 0) else (1, 1) - | .vector128Unsafe store offset => - let pops := (if store then 2 else 1) + (if offset then 1 else 0) - (pops, if store then 0 else 1) - | .bigMul64 _ | .divRemOut _ => (3, 1) - | .spanSlice explicitLength => (if explicitLength then 3 else 2, 1) - | .spanFromReference .. | .byteOffset => (2, 1) - | .spanSingleReference .. => (1, 1) - | .spanEmpty .. => (0, 1) - | .arraySpan _ _ range => (range.argumentCount, 1) - | .observeView _ | .spanReinterpret .. | .spanReadOnly | .arrayLength => (1, 1) - | .arrayLoad .. | .arrayAddress .. => (2, 1) - | .arrayTransfer _ store => if store then (3, 0) else (2, 1) - | .arrayStore _ => (3, 0) - | .copyBlock _ | .initBlock _ => (3, 0) - | .store128 | .store256 | .store512 => (2, 0) - | .init256 | .skipInit => (1, 0) - | .staticAddress _ | .staticValue _ | .nullRef | .nullObject | .stringLiteral _ => (0, 1) - | .objectField .. | .objectReferenceField .. | .referenceArrayLength | .arrayField .. => (1, 1) - | .setObjectField .. | .setObjectReferenceField .. | .setArrayField .. => (2, 0) - | .referenceArrayLoad => (2, 1) - | .referenceArrayStore => (3, 0) - | .throwIfNull | .zeroGuard .. => (2, 0) - | .comparisonGuard .. => (3, 0) - | .add .. | .spanCreate | .spanItem _ => (2, 1) - | .load128 | .load256 | .load512 | .asRef | .bitcast256 | .bitcastByteBool | .bitcastValue .. - | .createSpan _ | .spanReference | .spanLength | .isNullRef => (1, 1) +/-- The raw opcode a class type test lowers from, `castclass` (116) or `isinst` (117), and the + class it tests; `none` for any other instruction. -/ +def typeTestSource : Instruction → Option (Nat × TypeExpr) + | .castclass type => some (116, type) + | .isinst type => some (117, type) + | _ => none /-- Successful stack effects for instructions that cannot create a local alias or change a local root. Control flow, returns, stores and addresses require exact raw-instruction matching instead. No unrecognized instruction is admitted. -/ -def emittedFeatureEffect (op : Op) (returnsValue : Bool) : Option (Nat × Nat) := +def emittedFeatureEffect (op : Instruction) (returnsValue : Bool) : Option (Nat × Nat) := match op with - | .managed (.address ..) => some (0, 1) - | .managed .tableLength | .managed (.tableLoad _) => some (1, 1) - | .managed (.tableElement _) | .managed (.tableAddress _) | .managed .tableOffset => some (2, 1) - | .managed (.call signature _ _) => + | .ldftn .. => some (0, 1) + | .calli signature _ => some (signature.parameters.length + 1, if signature.result == .void then 0 else 1) - | .stackAlloc _ | .newArray _ | .newReferenceArray _ => some (1, 1) + | .localloc | .newarr _ => some (1, 1) -- Pushes and one-operand operations. | .nop => some (0, 0) - | .const32 _ | .const64 _ | .arg .. | .vectorArg _ | .local .. | .vectorLocal _ => some (0, 1) - | .neg | .bnot | .convI1 | .convI2 | .convU2 | .convI8 | .convI4 | .convU1 - | .convU4 | .convU8 | .convU | .checkedConvert .. - | .loadAggregate _ | .loadScalar .. | .load64 | .fieldAt .. | .fieldAddress _ - | .aggregateFieldAt .. | .lane _ | .laneAddress _ | .asRef | .objectTest .. => some (1, 1) + | .sizeof _ | .ldc_i4 _ | .ldc_i8 _ | .ldstr _ | .ldnull | .ldarg _ | .ldloc .. => some (0, 1) + | .neg | .not | .conv_i1 | .conv_i2 | .conv_u2 | .conv_i8 | .conv_i4 | .conv_u1 + | .conv_u4 | .conv_u8 | .conv_u | .conv_i | .conv_ovf _ | .conv_ovf_un _ + | .ldobj _ | .ldind _ + | .castclass _ | .isinst _ | .ldfld _ | .ldflda _ | .ldlen => some (1, 1) -- Two-operand arithmetic and comparisons, plain or with a widened native operand. - | .add | .sub | .mul | .band | .bor | .bxor | .shl | .shr | .shrUn - | .lt | .ltu | .gt | .gtu | .eq | .divide .. | .checkedArithmetic .. => some (2, 1) - | .nativeBinary operation => match operation with - | .add | .sub | .mul | .band | .bor | .bxor | .shl | .shr | .shrUn - | .lt | .ltu | .gt | .gtu | .eq | .divide .. | .checkedArithmetic .. => some (2, 1) - | _ => none + | .add | .sub | .mul | .and | .or | .xor | .shl | .shr | .shr_un + | .clt | .clt_un | .cgt | .cgt_un | .ceq | .div | .div_un | .rem | .rem_un | .add_ovf + | .add_ovf_un | .sub_ovf | .sub_ovf_un | .mul_ovf | .mul_ovf_un => some (2, 1) + -- Array element loads and stores. + | .ldelem _ | .ldelem_ref | .ldelem_any _ | .ldelema .. => some (2, 1) + | .stelem _ | .stelem_ref | .stelem_any _ => some (3, 0) -- Stores and initialization through a reference, `pop` and `dup`. - | .storeAggregate _ | .storeScalar _ | .store64 | .copyObject _ - | .setFieldAt .. | .setAggregateFieldAt .. | .setLane _ => some (2, 0) - | .initAggregate _ | .skipInit | .pop => some (1, 0) + | .stind _ | .cpobj _ | .stobj _ + | .stfld _ => some (2, 0) + | .initobj _ | .pop => some (1, 0) | .dup => some (1, 2) -- Calls, construction, intrinsics and memory operations. - | .call _ count | .callInstance _ count | .callVirtual _ count | .tailCall _ count => - some (count, if returnsValue then 1 else 0) - | .newAggregate _ count _ | .newVector256 _ count | .newObject _ count .. => some (count, 1) - | .intrinsic _ count => some (count, 1) - | .memory operation => some (memoryFeatureEffect operation) + | .call method _ => some (method.argumentCount, if returnsValue then 1 else 0) + | .callvirt method => some (method.argumentCount, if returnsValue then 1 else 0) + | .newobj constructor => some (constructor.parameters.length, 1) + | .cpblk _ | .initblk _ => some (3, 0) + | .ldsfld _ | .ldsflda _ | .ldtoken _ => some (0, 1) + | .stsfld _ => some (1, 0) -- Local writes, local addresses and control flow must match their raw instruction exactly. - | .localAddr _ | .vectorLocalAddr _ | .vectorArgAddr _ | .setLocal .. | .setVectorLocal _ - | .initializeSpanLocal .. - | .feature _ | .ret _ | .throw _ | .unsupported _ | .branch _ | .switch _ | .brzero _ - | .brnonzero _ | .bgt _ | .ble _ | .bleu _ | .bltu _ | .bgeu _ | .bgtu _ | .bge _ | .blt _ - | .beq _ | .bne _ => none + | .ldloca _ | .stloc .. | .starg _ | .ldarga _ + | .ret | .throw _ | .unadmitted _ | .br _ | .switch _ | .brfalse _ + | .brtrue _ | .bgt _ | .ble _ | .ble_un _ | .blt_un _ | .bge_un _ | .bgt_un _ | .bge _ + | .blt _ | .beq _ | .bne_un _ => none end CIL.Extraction diff --git a/CIL/Extraction/FeatureLocals.lean b/CIL/Extraction/FeatureLocals.lean index 4911d41d3..b94ecc1d1 100644 --- a/CIL/Extraction/FeatureLocals.lean +++ b/CIL/Extraction/FeatureLocals.lean @@ -3,7 +3,15 @@ import CIL.Extraction.FlowIndex import CIL.Extraction.Folds import Std.Data.TreeSet.Lemmas -/-! Indexed local eligibility for feature facts, equivalent to scanning raw metadata. -/ +/-! +# Feature local facts + +Indexed local eligibility for feature facts, equivalent to scanning raw metadata, and the +known-fact lists that certificate states keep for locals: the facts known at a pc as +(local index, word) pairs in strictly increasing index order. `expandKnown` gives the full +per-local list such a list stands for, and the lemmas here relate reads, stores, weakening and +eligibility on the two forms. +-/ namespace CIL.Extraction @@ -97,4 +105,417 @@ theorem FeatureLocalIndex.eligible_eq (raw : RawMethod) (index : Nat) (fact : Op #print axioms FeatureLocalIndex.eligible_eq +/-- `target` is no more precise than `source`. -/ +def factWeakens (source target : Option W32) : Bool := target.isNone || source == target + +/-- Pointwise `factWeakens` over lists of equal length. -/ +def factsWeaken (source target : List (Option W32)) : Bool := + source.length == target.length && (source.zip target).all fun (a, b) => factWeakens a b + +/-- Known local facts in strictly increasing local index order, each index at least `next` and + below `count`. -/ +def knownBelow (count : Nat) : Nat → List (Nat × W32) → Bool + | _, [] => true + | next, (index, _) :: rest => next ≤ index && index < count && knownBelow count (index + 1) rest + +/-- Record a fact for local `index` in an increasing known-fact list: its entry is replaced, + inserted in order, or removed when the fact is unknown. -/ +def storeKnown (index : Nat) (fact : Option W32) : List (Nat × W32) → List (Nat × W32) + | [] => match fact with | some word => [(index, word)] | none => [] + | (other, word) :: rest => + if other < index then (other, word) :: storeKnown index fact rest + else + let later := if other = index then rest else (other, word) :: rest + match fact with | some value => (index, value) :: later | none => later + +/-- The full per-local list over `count` locals that a known-fact list stands for. -/ +def expandKnown (count : Nat) (locals : List (Nat × W32)) : List (Option W32) := + (List.range count).map fun index => locals.lookup index + +/-- The known facts of a full per-local list whose first entry is the local the first argument + numbers. -/ +def compressKnown : Nat → List (Option W32) → List (Nat × W32) + | _, [] => [] + | index, none :: rest => compressKnown (index + 1) rest + | index, some word :: rest => (index, word) :: compressKnown (index + 1) rest + +/-- An entry heads a known-fact list exactly when it is in range and the rest follows it. -/ +theorem knownBelow_cons {count next index : Nat} {word : W32} {rest : List (Nat × W32)} : + knownBelow count next ((index, word) :: rest) = true ↔ + next ≤ index ∧ index < count ∧ knownBelow count (index + 1) rest = true := by + simp [knownBelow, Bool.and_eq_true, and_assoc] +#print axioms knownBelow_cons + +/-- A lower starting bound admits every list a higher one admits. -/ +theorem knownBelow_mono {count next lower : Nat} {locals : List (Nat × W32)} + (known : knownBelow count next locals = true) (below : lower ≤ next) : + knownBelow count lower locals = true := by + cases locals with + | nil => rfl + | cons entry rest => + obtain ⟨index, word⟩ := entry + rw [knownBelow_cons] at known ⊢ + exact ⟨Nat.le_trans below known.1, known.2⟩ +#print axioms knownBelow_mono + +/-- Every entry of a known-fact list lies between its bounds. -/ +theorem knownBelow_mem {count next : Nat} {locals : List (Nat × W32)} + (known : knownBelow count next locals = true) {entry : Nat × W32} (member : entry ∈ locals) : + next ≤ entry.1 ∧ entry.1 < count := by + induction locals generalizing next with + | nil => simp at member + | cons head rest ih => + obtain ⟨index, word⟩ := head + rw [knownBelow_cons] at known + rcases List.mem_cons.mp member with same | later + · subst same + exact ⟨known.1, known.2.1⟩ + · have bounds := ih known.2.2 later + exact ⟨by omega, bounds.2⟩ +#print axioms knownBelow_mem + +/-- A known-fact list has no fact for an index outside its bounds. -/ +theorem knownBelow_lookup_none {count next : Nat} {locals : List (Nat × W32)} + (known : knownBelow count next locals = true) {key : Nat} + (outside : key < next ∨ count ≤ key) : + locals.lookup key = none := by + rw [List.lookup_eq_none_iff] + intro entry member + have bounds := knownBelow_mem known member + simp only [bne_iff_ne, ne_eq] + omega +#print axioms knownBelow_lookup_none + +/-- In a known-fact list an entry is present exactly when lookup finds its word. -/ +theorem knownBelow_mem_iff {count next : Nat} {locals : List (Nat × W32)} + (known : knownBelow count next locals = true) {key : Nat} {word : W32} : + (key, word) ∈ locals ↔ locals.lookup key = some word := by + induction locals generalizing next with + | nil => simp + | cons head rest ih => + obtain ⟨index, value⟩ := head + rw [knownBelow_cons] at known + by_cases same : key = index + · subst same + have absent : (key, word) ∉ rest := fun member => by + have := (knownBelow_mem known.2.2 member).1 + simp only at this + omega + simp [absent, eq_comm] + · have differs : (key == index) = false := by simpa using same + simp [List.lookup_cons, differs, same, ih known.2.2] +#print axioms knownBelow_mem_iff + +/-- Storing keeps the list increasing and in range, and changes the fact of one local only. -/ +theorem storeKnown_spec {count next index : Nat} {fact : Option W32} {locals : List (Nat × W32)} + (known : knownBelow count next locals = true) (low : next ≤ index) (high : index < count) : + knownBelow count next (storeKnown index fact locals) = true ∧ + ∀ key, (storeKnown index fact locals).lookup key = + if key = index then fact else locals.lookup key := by + induction locals generalizing next with + | nil => + cases fact with + | none => simp [storeKnown, knownBelow] + | some word => + refine ⟨by simp [storeKnown, knownBelow, low, high], fun key => ?_⟩ + by_cases same : key = index + · simp [storeKnown, same] + · have differs : (key == index) = false := by simpa using same + simp [storeKnown, List.lookup_cons, differs, same] + | cons head rest ih => + obtain ⟨other, word⟩ := head + obtain ⟨otherLow, otherHigh, tail⟩ := knownBelow_cons.mp known + by_cases before : other < index + · have inner := ih tail (by omega) + simp only [storeKnown, before, ↓reduceIte] + refine ⟨knownBelow_cons.mpr ⟨otherLow, otherHigh, inner.1⟩, fun key => ?_⟩ + by_cases same : key = other + · subst same + have differs : key ≠ index := by omega + simp [differs] + · have differs : (key == other) = false := by simpa using same + simp only [List.lookup_cons, differs, inner.2] + · by_cases equal : other = index + · subst equal + have absent : rest.lookup other = none := knownBelow_lookup_none tail (by omega) + cases fact with + | none => + simp only [storeKnown, before, ↓reduceIte, ↓reduceIte] + refine ⟨knownBelow_mono tail (by omega), fun key => ?_⟩ + by_cases same : key = other + · subst same; simp [absent] + · have differs : (key == other) = false := by simpa using same + simp [List.lookup_cons, differs, same] + | some value => + simp only [storeKnown, before, ↓reduceIte, ↓reduceIte] + refine ⟨knownBelow_cons.mpr ⟨low, high, tail⟩, fun key => ?_⟩ + by_cases same : key = other + · subst same; simp + · have differs : (key == other) = false := by simpa using same + simp [List.lookup_cons, differs, same] + · have after : index < other := by omega + have absent : ((other, word) :: rest).lookup index = none := + knownBelow_lookup_none (knownBelow_cons.mpr ⟨Nat.le_refl other, otherHigh, tail⟩) + (Or.inl after) + cases fact with + | none => + simp only [storeKnown, before, equal, ↓reduceIte] + refine ⟨known, fun key => ?_⟩ + by_cases same : key = index + · subst same; simp [absent] + · simp [same] + | some value => + simp only [storeKnown, before, equal, ↓reduceIte] + refine ⟨knownBelow_cons.mpr ⟨low, high, knownBelow_cons.mpr ⟨after, otherHigh, tail⟩⟩, + fun key => ?_⟩ + by_cases same : key = index + · subst same; simp + · have differs : (key == index) = false := by simpa using same + simp [List.lookup_cons, differs, same] +#print axioms storeKnown_spec + +/-- The expansion holds each local's looked-up fact and nothing past the last local. -/ +theorem expandKnown_getElem? {count : Nat} {locals : List (Nat × W32)} {key : Nat} : + (expandKnown count locals)[key]? = if key < count then some (locals.lookup key) else none := by + unfold expandKnown + rw [List.getElem?_map] + by_cases inside : key < count + · simp [inside] + · simp [inside] +#print axioms expandKnown_getElem? + +/-- The expansion has one fact per local. -/ +@[simp] theorem expandKnown_length {count : Nat} {locals : List (Nat × W32)} : + (expandKnown count locals).length = count := by + simp [expandKnown] +#print axioms expandKnown_length + +/-- Storing into known facts is setting the fact in the full per-local list. -/ +theorem expandKnown_storeKnown {count index : Nat} {fact : Option W32} {locals : List (Nat × W32)} + (known : knownBelow count 0 locals = true) (high : index < count) : + expandKnown count (storeKnown index fact locals) = + (expandKnown count locals).set index fact := by + apply List.ext_getElem? + intro key + rw [List.getElem?_set, expandKnown_getElem?, expandKnown_getElem?, + (storeKnown_spec known (Nat.zero_le _) high).2, expandKnown_length] + by_cases same : index = key + · subst same; simp [high] + · simp [same, Ne.symm same] +#print axioms expandKnown_storeKnown + +/-- Between known-fact lists, containment of entries is the sublist order. -/ +theorem knownBelow_sublist {count : Nat} : + ∀ {source target : List (Nat × W32)} {sourceNext targetNext : Nat}, + knownBelow count sourceNext source = true → knownBelow count targetNext target = true → + target ⊆ source → target.Sublist source + | [], target, _, _, _, _, subset => by + cases target with + | nil => exact List.Sublist.slnil + | cons head rest => exact absurd (subset (List.mem_cons_self ..)) (by simp) + | (index, word) :: source, [], _, _, _, _, _ => List.nil_sublist _ + | (index, word) :: source, (other, value) :: target, sourceNext, targetNext, + sourceKnown, targetKnown, subset => by + have sourceSplit := knownBelow_cons.mp sourceKnown + have targetSplit := knownBelow_cons.mp targetKnown + have head := subset (List.mem_cons_self ..) + by_cases same : (other, value) = (index, word) + · cases same + apply List.Sublist.cons_cons + refine knownBelow_sublist sourceSplit.2.2 targetSplit.2.2 fun entry member => ?_ + have bound := (knownBelow_mem targetSplit.2.2 member).1 + rcases List.mem_cons.mp (subset (List.mem_cons_of_mem _ member)) with equal | later + · subst equal; omega + · exact later + · have later : (other, value) ∈ source := by + rcases List.mem_cons.mp head with equal | later + · exact absurd equal same + · exact later + have above := (knownBelow_mem sourceSplit.2.2 later).1 + apply List.Sublist.cons + refine knownBelow_sublist sourceSplit.2.2 targetKnown fun entry member => ?_ + have bound := (knownBelow_mem targetKnown member).1 + rcases List.mem_cons.mp (subset member) with equal | inner + · rcases List.mem_cons.mp member with first | rest + · subst first; exact absurd equal same + · have := (knownBelow_mem targetSplit.2.2 rest).1 + subst equal; simp only at above this; omega + · exact inner +#print axioms knownBelow_sublist + +/-- Known-fact lists with the same lookups are equal. -/ +theorem knownBelow_eq {count : Nat} {source target : List (Nat × W32)} + {sourceNext targetNext : Nat} + (sourceKnown : knownBelow count sourceNext source = true) + (targetKnown : knownBelow count targetNext target = true) + (lookups : ∀ key, source.lookup key = target.lookup key) : source = target := by + have members : ∀ entry, entry ∈ source ↔ entry ∈ target := by + intro ⟨key, word⟩ + rw [knownBelow_mem_iff sourceKnown, knownBelow_mem_iff targetKnown, lookups] + have inward := knownBelow_sublist sourceKnown targetKnown fun entry => (members entry).mpr + have outward := knownBelow_sublist targetKnown sourceKnown fun entry => (members entry).mp + exact outward.eq_of_length (Nat.le_antisymm outward.length_le inward.length_le) +#print axioms knownBelow_eq + +/-- Pointwise weakening: equal lengths, and every known fact of the target is the source's. -/ +theorem factsWeaken_iff {source target : List (Option W32)} : + factsWeaken source target = true ↔ source.length = target.length ∧ + ∀ (key : Nat) (word : W32), target[key]? = some (some word) → + source[key]? = some (some word) := by + induction source generalizing target with + | nil => + cases target with + | nil => simp [factsWeaken] + | cons head rest => simp [factsWeaken] + | cons first source ih => + cases target with + | nil => simp [factsWeaken] + | cons head rest => + have step : factsWeaken (first :: source) (head :: rest) = + (factWeakens first head && factsWeaken source rest) := by + simp only [factsWeaken, List.length_cons, List.zip_cons_cons, List.all_cons] + cases factWeakens first head <;> simp [Bool.and_comm] + rw [step, Bool.and_eq_true, ih] + constructor + · rintro ⟨weakens, length, later⟩ + refine ⟨by simp [length], fun key word found => ?_⟩ + cases key with + | zero => + simp only [List.getElem?_cons_zero, Option.some.injEq] at found ⊢ + subst found + simpa [factWeakens] using weakens + | succ key => simpa using later key word (by simpa using found) + · rintro ⟨length, later⟩ + refine ⟨?_, by simpa using length, fun key word found => by + simpa using later (key + 1) word (by simpa using found)⟩ + cases head with + | none => rfl + | some word => + have := later 0 word rfl + simp only [List.getElem?_cons_zero, Option.some.injEq] at this + simp [factWeakens, this] +#print axioms factsWeaken_iff + +/-- A local holds a known fact in the expansion exactly when the list has that entry. -/ +theorem expandKnown_some {count : Nat} {locals : List (Nat × W32)} + (known : knownBelow count 0 locals = true) {key : Nat} {word : W32} : + (expandKnown count locals)[key]? = some (some word) ↔ (key, word) ∈ locals := by + rw [expandKnown_getElem?, knownBelow_mem_iff known] + by_cases inside : key < count + · simp [inside] + · simp only [inside, ↓reduceIte, reduceCtorEq, false_iff] + rw [knownBelow_lookup_none known (Or.inr (Nat.le_of_not_lt inside))] + simp +#print axioms expandKnown_some + +/-- The sublist test on known facts is pointwise weakening of their expansions. -/ +theorem isSublist_eq_factsWeaken {count : Nat} {source target : List (Nat × W32)} + (sourceKnown : knownBelow count 0 source = true) + (targetKnown : knownBelow count 0 target = true) : + target.isSublist source = + factsWeaken (expandKnown count source) (expandKnown count target) := by + apply Bool.eq_iff_iff.mpr + rw [List.isSublist_iff_sublist, factsWeaken_iff] + simp only [expandKnown_length, true_and, expandKnown_some sourceKnown, + expandKnown_some targetKnown] + constructor + · intro sublist key word member + exact sublist.subset member + · intro subset + exact knownBelow_sublist sourceKnown targetKnown fun ⟨key, word⟩ member => + subset key word member +#print axioms isSublist_eq_factsWeaken + +/-- Different known-fact lists over the same locals expand to different full lists. -/ +theorem expandKnown_inj {count : Nat} {source target : List (Nat × W32)} + (sourceKnown : knownBelow count 0 source = true) + (targetKnown : knownBelow count 0 target = true) + (expanded : expandKnown count source = expandKnown count target) : source = target := by + refine knownBelow_eq sourceKnown targetKnown fun key => ?_ + by_cases inside : key < count + · have := congrArg (·[key]?) expanded + simp only [expandKnown_getElem?, inside, ↓reduceIte, Option.some.injEq] at this + exact this + · rw [knownBelow_lookup_none sourceKnown (Or.inr (Nat.le_of_not_lt inside)), + knownBelow_lookup_none targetKnown (Or.inr (Nat.le_of_not_lt inside))] +#print axioms expandKnown_inj + +/-- With unknown always eligible, the expansion is eligible exactly when every known fact is. -/ +theorem expandKnown_all {count : Nat} {locals : List (Nat × W32)} + {eligible : Nat → Option W32 → Bool} + (unknown : ∀ index, eligible index none = true) (known : knownBelow count 0 locals = true) : + ((expandKnown count locals).zipIdx.all fun (fact, index) => eligible index fact) = + locals.all fun entry => eligible entry.1 (some entry.2) := by + apply Bool.eq_iff_iff.mpr + simp only [List.all_eq_true, Prod.forall, List.mem_zipIdx_iff_getElem?, expandKnown_getElem?] + constructor + · intro all index word member + have inside := (knownBelow_mem known member).2 + have := all (some word) index + rw [(knownBelow_mem_iff known).mp member] at this + exact this (by simp [inside]) + · intro all fact index found + by_cases inside : index < count + · simp only [inside, ↓reduceIte, Option.some.injEq] at found + subst found + cases looked : locals.lookup index with + | none => exact unknown index + | some word => exact all index word ((knownBelow_mem_iff known).mpr looked) + · simp [inside] at found +#print axioms expandKnown_all + +/-- Compressing a full list keeps its known facts in order, numbered from the start index. -/ +theorem compressKnown_spec (facts : List (Option W32)) : + ∀ (start count : Nat), start + facts.length ≤ count → + knownBelow count start (compressKnown start facts) = true ∧ + ∀ key, (compressKnown start facts).lookup key = + if start ≤ key then facts[key - start]?.join else none := by + induction facts with + | nil => intro start count _; simp [compressKnown, knownBelow] + | cons fact rest ih => + intro start count bound + have inner := ih (start + 1) count (by simp at bound; omega) + cases fact with + | none => + refine ⟨knownBelow_mono inner.1 (by omega), fun key => ?_⟩ + simp only [compressKnown, inner.2] + by_cases same : key = start + · subst same + rw [ite_eq_right (by omega), ite_eq_left (Nat.le_refl _)] + simp + · by_cases above : start + 1 ≤ key + · have shifted : key - start = (key - (start + 1)) + 1 := by omega + simp [above, show start ≤ key by omega, shifted] + · simp [above, show ¬ start ≤ key by omega] + | some word => + simp only [compressKnown] + refine ⟨knownBelow_cons.mpr ⟨Nat.le_refl _, by simp at bound; omega, inner.1⟩, + fun key => ?_⟩ + by_cases same : key = start + · subst same; simp + · have differs : (key == start) = false := by simpa using same + simp only [List.lookup_cons, differs, inner.2] + by_cases above : start + 1 ≤ key + · have shifted : key - start = (key - (start + 1)) + 1 := by omega + simp [above, show start ≤ key by omega, shifted] + · simp [above, show ¬ start ≤ key by omega] +#print axioms compressKnown_spec + +/-- Compressing a full list gives a known-fact list over its locals. -/ +theorem compressKnown_known (facts : List (Option W32)) : + knownBelow facts.length 0 (compressKnown 0 facts) = true := + (compressKnown_spec facts 0 facts.length (by simp)).1 +#print axioms compressKnown_known + +/-- Expanding the compression of a full list over its own length gives it back. -/ +theorem expandKnown_compressKnown (facts : List (Option W32)) : + expandKnown facts.length (compressKnown 0 facts) = facts := by + apply List.ext_getElem? + intro key + rw [expandKnown_getElem?, (compressKnown_spec facts 0 facts.length (by simp)).2] + by_cases inside : key < facts.length + · simp [inside] + · simp [inside] +#print axioms expandKnown_compressKnown + end CIL.Extraction diff --git a/CIL/Extraction/Features.lean b/CIL/Extraction/Features.lean index 342eaaf56..e54c02e7e 100644 --- a/CIL/Extraction/Features.lean +++ b/CIL/Extraction/Features.lean @@ -1,10 +1,12 @@ import CIL.Extraction.Generics +import CIL.Extraction.Constrained import CIL.Extraction.FeatureEffects import CIL.Extraction.Folds import CIL.Extraction.FlowIndex import CIL.Extraction.FeatureLocals +import CIL.Extraction.IndirectAccess import CIL.Semantics -import Lean.Elab.Tactic.BuiltinTactic +import CIL.Extraction.RuntimeModels /-! # Feature flow @@ -14,212 +16,105 @@ the extractor prunes the branches they decide. `FeatureFlowValid` checks that pr abstract interpretation tracks known 32-bit words on the stack and in eligible locals, its transfer function and successor edges are recomputed here from the raw body, and the extractor's per-pc certificate must be closed under them. Only pcs the certificate reaches -may be live; every other emitted instruction must be an `.unsupported` hole. +may be live; every other emitted instruction must be a hole (`.unadmitted`). The same pass binds each raw instruction to its emitted counterpart, exactly for control flow, locals and feature tests, and by stack effect for everything else. A parameterless static Boolean call pushes the word `constantReturn` reads from the callee's own certificate, which the whole-program obligation validates alongside the caller's. Known words -only decide which pcs are live, and every pc they rule out is emitted as an `.unsupported` hole -that has no step. A wrong claim, cyclic or not, therefore sends execution into a hole that faults: +only decide which pcs are live, and every pc they rule out is emitted as a hole, whose step +faults. A wrong claim, cyclic or not, therefore sends execution into a hole that faults: it cannot make a successful outcome provable, only unreachable. -/ namespace CIL.Extraction -/-- Feature facts use stack order (top first). `none` includes every runtime value; - `some` describes exactly a 32-bit evaluation-stack word. -/ -structure FeatureState where +/-- Feature facts use stack order (top first), with the local facts held as `Locals`. `none` + includes every runtime value; `some` describes exactly a 32-bit evaluation-stack word. -/ +structure FeatureFacts (Locals : Type) where stack : List (Option W32) - locals : List (Option W32) + locals : Locals deriving DecidableEq, Repr +/-- A certificate state. Its `locals` hold only the known local facts, as (local index, word) + pairs in strictly increasing index order; a local without an entry has no known fact. -/ +abbrev FeatureState := FeatureFacts (List (Nat × W32)) + /-- The claimed state at each raw pc; `none` marks a pc the certificate claims unreachable. -/ abbrev FeatureCertificate := List (Option FeatureState) -/-- `target` is no more precise than `source`. -/ -def factWeakens (source target : Option W32) : Bool := target.isNone || source == target +/-- The specification's state, with one fact per local. -/ +abbrev FeatureListState := FeatureFacts (List (Option W32)) + +/-- The specification's certificate: one fact per local in every claimed state. -/ +abbrev FeatureListCertificate := List (Option FeatureListState) + +/-- How a state holds the facts of a method's locals, with the method's eligibility rule. -/ +structure LocalFacts (Locals : Type) where + /-- Whether a local may hold a fact; unknown is admitted everywhere. -/ + eligible : Nat → Option W32 → Bool + /-- The fact of a local, or `none` for an index outside the method's locals. -/ + load : Locals → Nat → Option (Option W32) + /-- Record a fact for a local, or `none` for an index outside the method's locals. -/ + store : Locals → Nat → Option W32 → Option Locals + /-- The facts cover the method's locals and each known fact is eligible. -/ + fits : Locals → Bool + /-- Every fact of the second argument is unknown or the same as in the first. -/ + weakens : Locals → Locals → Bool + +/-- One fact per local over `count` locals: the specification's form. -/ +def LocalFacts.list (count : Nat) (eligible : Nat → Option W32 → Bool) : + LocalFacts (List (Option W32)) where + eligible := eligible + load locals index := locals[index]? + store locals index fact := + if index >= locals.length then none else some (locals.set index fact) + fits locals := + locals.length == count && (locals.zipIdx).all fun (fact, index) => eligible index fact + weakens := factsWeaken -/-- Pointwise `factWeakens` over lists of equal length. -/ -def factsWeaken (source target : List (Option W32)) : Bool := - source.length == target.length && (source.zip target).all fun (a, b) => factWeakens a b +/-- Known facts over `count` locals; reads, stores and weakening walk the known entries only. -/ +def LocalFacts.known (count : Nat) (eligible : Nat → Option W32 → Bool) : + LocalFacts (List (Nat × W32)) where + eligible := eligible + load locals index := if index < count then some (locals.lookup index) else none + store locals index fact := if index < count then some (storeKnown index fact locals) else none + fits locals := + knownBelow count 0 locals && locals.all fun entry => eligible entry.1 (some entry.2) + weakens source target := target.isSublist source /-- Stack and locals of `target` both weaken those of `source`. -/ -def FeatureState.weakens (source target : FeatureState) : Bool := - factsWeaken source.stack target.stack && factsWeaken source.locals target.locals - -/-- The admitted feature classes, by name under `System.Runtime.Intrinsics`. - .NET 10 reports `AdvSimd.Arm64` exactly when it reports `AdvSimd`, `Avx512Vbmi.VL` exactly - when it reports `Avx512Vbmi`, and each `Aes.X64` or `Aes.Arm64` exactly when it reports that - architecture's `Aes` (docs/RUNTIME-PROFILES.md). -/ -private def featureNames : List (String × Feature) := [ - ("Arm.AdvSimd", .advSimd), ("Arm.AdvSimd/Arm64", .advSimd), ("Arm.ArmBase/Arm64", .armBase64), - ("X86.Sse2", .sse2), ("X86.Ssse3", .ssse3), ("X86.Sse41", .sse41), - ("X86.Sse42", .sse42), ("X86.Avx", .avx), ("X86.Avx2", .avx2), - ("X86.Avx512F", .avx512F), ("X86.Avx512F/VL", .avx512FVL), - ("X86.Avx512DQ", .avx512DQ), ("X86.Avx512DQ/VL", .avx512DQVL), - ("X86.Avx512Vbmi", .avx512Vbmi), ("X86.Avx512Vbmi/VL", .avx512Vbmi), - ("X86.Aes", .aes), ("X86.Aes/X64", .aes), ("Arm.Aes", .armAes), ("Arm.Aes/Arm64", .armAes), - ("X86.Bmi1", .bmi1), ("X86.Bmi2/X64", .bmi2), ("X86.X86Base/X64", .x86Base64), - ("Vector128", .vector128Accelerated), ("Vector256", .vector256Accelerated)] - -/-- The exact assembly, return type and namespace shared by feature getters. -/ -private def featurePrefix := intrinsicsScope ++ "::System.Boolean System.Runtime.Intrinsics." - -/-- The class and member portion of each admitted feature getter. -/ -private def featureSuffix (name : String) (feature : Feature) : String := - name ++ "::" ++ - (if feature == .vector128Accelerated || feature == .vector256Accelerated then - "get_IsHardwareAccelerated" else "get_IsSupported") ++ "()" - -/-- Literal identities avoid rebuilding common assembly and namespace prefixes per lookup. -/ -private def featureIdentities : List (List UInt8) := by - run_tac do - let expression ← Lean.Elab.Term.elabTerm - (← `(featureNames.map fun (name, feature) => - (featureSuffix name feature).toByteArray.data.toList)) none - Lean.Elab.Term.synthesizeSyntheticMVarsNoPostponing - let expression ← Lean.instantiateMVars expression - let values ← unsafe Lean.Meta.evalExpr (List (List UInt8)) - (← Lean.Meta.inferType expression) expression - Lean.Elab.Tactic.closeMainGoal `featureIdentities (Lean.toExpr values) - -set_option maxRecDepth 4096 in -/-- The kernel checks every materialized identity against the original feature names. -/ -private theorem featureIdentities_eq : featureIdentities = - featureNames.map (fun (name, feature) => - (featureSuffix name feature).toByteArray.data.toList) := by rfl -#print axioms featureIdentities_eq - -/-- Remove an exact byte prefix, rejecting mismatches and truncated identities. -/ -private def stripFeaturePrefix : List UInt8 → List UInt8 → Option (List UInt8) - | [], bytes => some bytes - | first :: expected, actual :: bytes => - if first == actual then stripFeaturePrefix expected bytes else none - | _, _ => none +def FeatureFacts.weakens {Locals : Type} (facts : LocalFacts Locals) + (source target : FeatureFacts Locals) : Bool := + factsWeaken source.stack target.stack && facts.weakens source.locals target.locals -/-- Successful prefix removal retains exactly the remaining bytes. -/ -private theorem stripFeaturePrefix_eq (expected bytes suffix : List UInt8) : - stripFeaturePrefix expected bytes = some suffix ↔ bytes = expected ++ suffix := by - induction expected generalizing bytes with - | nil => simp [stripFeaturePrefix] - | cons first expected ih => - cases bytes with - | nil => simp [stripFeaturePrefix] - | cons actual bytes => - by_cases equal : first = actual - · subst actual - simp [stripFeaturePrefix, ih] - · simp [stripFeaturePrefix, equal, beq_iff_eq, Ne.symm equal] -#print axioms stripFeaturePrefix_eq - -/-- The class and member bytes under the exact feature-getter prefix. -/ -private def featureSignature (identity : String) : Option (List UInt8) := - stripFeaturePrefix featurePrefix.toByteArray.data.toList identity.toByteArray.data.toList - -/-- Platform query names whose exact signatures must bind emitted constants. -/ -def platformQuery (site : CallSite) : Bool := - match site.declaringType with - | .atom _ owner => - (site.name == "get_Size" && owner ∈ ["System.IntPtr", "System.UIntPtr"]) || - (site.name == "get_Is64BitProcess" && owner == "System.Environment") - | _ => false - -/-- Exact process-width getters under the supported 64-bit runtime profiles. -/ -def platformConstant (site : CallSite) : Option W32 := do - let .atom scope owner := site.declaringType | none - if !platformQuery site || scope != runtimeScope || site.parameterCount != 0 || - !site.parameterTypes.isEmpty || site.hasThis || !site.returnsValue || - site.ownerArgumentCount != 0 || !site.typeArguments.isEmpty then none - else - let result := if site.name == "get_Size" then "System.Int32" else "System.Boolean" - if site.returnType != .atom runtimeScope result || - site.target != .external (runtimeScope ++ "::" ++ result ++ " " ++ owner ++ - "::" ++ site.name ++ "()") then none - else some (if site.name == "get_Size" then 8 else 1) - -/-- `ldsfld` of the System.Runtime `BitConverter` static Boolean, whose only such field is - `IsLittleEndian`; the raw operand omits field names, so the extractor checks the member. - Valid profiles are little-endian, so the load is the constant 1. -/ +/-- The specification state a certificate state stands for over `count` locals. -/ +def FeatureState.expand (count : Nat) (state : FeatureState) : FeatureListState := + { stack := state.stack, locals := expandKnown count state.locals } + +/-- The bound getter model supplies the same fact to reachability and execution. -/ +def platformGetterModel (site : CallSite) : Option RuntimeModel := do + let name := site.method.name + guard (name == name!"get_IsSupported" || name == name!"get_IsHardwareAccelerated" || + name == name!"get_Size" || name == name!"get_Is64BitProcess") + let entry ← runtimeModels.entry? .call site.method + let row ← entry.select? site.method + guard (row.operand.bindsSite site) + row.resolve (LayoutEnvironment.callLayouts {}) site.method + +/-- `ldsfld` of the System.Runtime `BitConverter` static Boolean `IsLittleEndian`. Valid + profiles are little-endian, so the load is the constant 1. -/ def platformField (instruction : RawInstruction) : Option W32 := - if instruction.opcode == 126 && instruction.operand == - .field (.atom runtimeScope "System.BitConverter") 0 (.atom runtimeScope "System.Boolean") true + if instruction.opcode == 126 && instruction.operand == .field littleEndianField.owner + littleEndianField.index littleEndianField.type littleEndianField.name true then some 1 else none -/-- Classify reference-free storage against a table whose `LayoutsValid` is checked separately. - Aggregate identity lookup does not revalidate fields or confer memory access authority. -/ -def referenceFreeStorage (layouts : Array RawLayout) : TypeExpr → Bool - | type@(.atom ..) | type@(.enum _ (.atom ..)) => - match typeStorage [] type with | some (.field _) => true | _ => false - | .aggregate identity index => (layouts[index]?).any (·.identity == identity) - | type => (integerVectorWidth type).isSome - -/-- Reserve the runtime query name even when malformed metadata prevents exact recognition. -/ -def referenceContentQuery (site : CallSite) : Bool := - site.name == "IsReferenceOrContainsReferences" && - (match site.declaringType with - | .atom _ owner => owner == "System.Runtime.CompilerServices.RuntimeHelpers" - | _ => false) - -/-- Bind a false reference-content answer to an exact helper and independently admitted type. -/ -def referenceContentConstant (layouts : Array RawLayout) (site : CallSite) : Option W32 := do - let [type] := site.typeArguments | none - if !referenceContentQuery site || - site.declaringType != .atom runtimeScope "System.Runtime.CompilerServices.RuntimeHelpers" || - site.parameterCount != 0 || !site.parameterTypes.isEmpty || site.hasThis || - !site.returnsValue || site.ownerArgumentCount != 0 || - site.returnType != .atom runtimeScope "System.Boolean" || - site.target != .external (runtimeScope ++ - "::System.Boolean System.Runtime.CompilerServices.RuntimeHelpers::" ++ - "IsReferenceOrContainsReferences()") || !referenceFreeStorage layouts type then none - else some 0 - -/-- Identify getters from raw member and assembly identity, never an emitter-supplied flag. -/ -def featureGetter (site : CallSite) : Option Feature := do - let .external identity := site.target | none - if site.parameterCount != 0 || site.hasThis || !site.returnsValue || - !site.typeArguments.isEmpty then none - else - let signature := featureSignature identity - ((featureIdentities.zip (featureNames.map Prod.snd)).find? - fun (expected, _) => signature == some expected).map Prod.snd - -/-- UTF-8 byte equality is exact string equality, including non-ASCII metadata. -/ -private theorem featureBytes_eq (identity suffix : String) : - (featureSignature identity == some suffix.toByteArray.data.toList) = - (identity == featurePrefix ++ suffix) := by - apply Bool.eq_iff_iff.mpr - simp only [featureSignature, beq_iff_eq, stripFeaturePrefix_eq] - rw [← ByteArray.toList_data_append, ← String.toByteArray_append, Array.toList_inj] - constructor - · intro equal - exact String.toByteArray_inj.mp (ByteArray.ext equal) - · intro equal - cases equal - rfl -#print axioms featureBytes_eq -/-- Literal lookup preserves the admitted getters for every raw call site. -/ -theorem featureGetter_eq_original (site : CallSite) : featureGetter site = (do - let .external identity := site.target | none - if site.parameterCount != 0 || site.hasThis || !site.returnsValue || - !site.typeArguments.isEmpty then none - else (featureNames.find? fun (name, feature) => - identity == intrinsicsScope ++ "::System.Boolean System.Runtime.Intrinsics." ++ name ++ - "::" ++ (if feature == .vector128Accelerated || feature == .vector256Accelerated then - "get_IsHardwareAccelerated" else "get_IsSupported") ++ "()").map Prod.snd) := by - simp only [featureGetter, featureIdentities_eq, List.zip_map', List.find?_map, - Function.comp_def, featureBytes_eq, Option.map_map, featurePrefix, featureSuffix, - String.append_assoc] -#print axioms featureGetter_eq_original - -/-- A certificate state fits the method: one fact per local, at most `maxStack` stack - entries, and only eligible locals known. -/ -private def wellShaped (raw : RawMethod) - (eligible : Nat → Option W32 → Bool) - (state : FeatureState) : Bool := - state.locals.length == raw.locals.length && state.stack.length <= raw.maxStack && - (state.locals.zipIdx).all fun (fact, index) => eligible index fact +/-- A certificate state fits the method: its local facts fit the method's locals and the stack + holds at most `maxStack` entries. -/ +private def wellShaped {Locals : Type} (raw : RawMethod) (facts : LocalFacts Locals) + (state : FeatureFacts Locals) : Bool := + facts.fits state.locals && state.stack.length <= raw.maxStack /-- The word an `ldc.i4` instruction pushes, in any of its forms. -/ private def constant32 (instruction : RawInstruction) : Option W32 := @@ -245,10 +140,15 @@ where | (⟨_, 42, _⟩, some state) => some state.stack.head?.join | _ => none -/-- The word an emitted parameterless call pushes when `constants` knows its callee's return. - `constants` reads each callee's own certificate, which its own obligation validates. -/ -def calleeConstant (constants : Nat → Option W32) : Op → Option W32 - | .call index 0 => constants index +/-- The word an emitted parameterless call, not a tail call, pushes when `constants` knows the + return of the callee that `link`, the link of its pc, names. `constants` reads each callee's + own certificate, which its own obligation validates. -/ +def calleeConstant (constants : Nat → Option W32) (link : Option Link) : + Instruction → Option W32 + | .call method prefixes => match link with + | some (.direct index) => + if method.argumentCount = 0 && !prefixes.tail then constants index else none + | _ => none | _ => none /-- Fixed CLI stack effects; unknown values are never interpreted as false. @@ -275,10 +175,8 @@ def rawStackEffect : Nat → Option (Nat × Nat) | _ => none /-- Pops `pops` facts and pushes `pushes` unknowns; fails on stack underflow. -/ -private def forget (state : FeatureState) (pops pushes : Nat) : Option FeatureState := - if pops <= state.stack.length then - some { state with stack := List.replicate pushes none ++ state.stack.drop pops } - else none +private def forget (stack : List (Option W32)) (pops pushes : Nat) : Option (List (Option W32)) := + if pops <= stack.length then some (List.replicate pushes none ++ stack.drop pops) else none /-- Short and long compare branches share the CLI's ten-operation ordering. -/ def comparisonBranchIndex (opcode : Nat) : Option Nat := @@ -292,41 +190,27 @@ def constantComparison (opcode : Nat) (left right : W32) : Option W32 := do | 65028 => some 4 | 65029 => some 9 | _ => comparisonBranchIndex opcode let (operation, negate) ← match index with - | 0 => some (Op.eq, false) | 5 => some (.eq, true) - | 1 => some (.lt, true) | 2 => some (.gt, false) - | 3 => some (.gt, true) | 4 => some (.lt, false) - | 6 => some (.ltu, true) | 7 => some (.gtu, false) - | 8 => some (.gtu, true) | 9 => some (.ltu, false) + | 0 => some (Instruction.ceq, false) | 5 => some (.ceq, true) + | 1 => some (.clt, true) | 2 => some (.cgt, false) + | 3 => some (.cgt, true) | 4 => some (.clt, false) + | 6 => some (.clt_un, true) | 7 => some (.cgt_un, false) + | 8 => some (.cgt_un, true) | 9 => some (.clt_un, false) | _ => none let .i32 value ← binary operation (.i32 left) (.i32 right) | none return if negate then (if value == 0 then 1 else 0) else value -/-- Recompute a transfer from raw CLI encodings and metadata, independently of the - extractor's worklist and its claimed incoming/outgoing facts. -/ -private def featureTransferWith (layouts : Array RawLayout) (raw : RawMethod) - (eligible : Nat → Option W32 → Bool) - (profile : FeatureProfile) - (instruction : RawInstruction) (state : FeatureState) : Option FeatureState := do - if let some word := constant32 instruction <|> platformField instruction then - return { state with stack := some word :: state.stack } - -- `ldloc` and `stloc`; a store keeps its fact only in an eligible local. - if instruction.opcode ∈ [6, 7, 8, 9, 17, 65036] then - let index ← localIndex instruction - let value ← state.locals[index]? - return { state with stack := value :: state.stack } - if instruction.opcode ∈ [10, 11, 12, 13, 19, 65038] then - let index ← localIndex instruction - let value :: stack := state.stack | none - if index >= state.locals.length then none else - return { stack, locals := - state.locals.set index (if eligible index value then value else none) } +/-- The stack effect of an instruction that neither pushes a constant word nor accesses a local, + recomputed from raw CLI encodings and metadata. -/ +private def featureStackTransfer (layouts : Array RawLayout) (raw : RawMethod) + (profile : FeatureProfile) (instruction : RawInstruction) (stack : List (Option W32)) : + Option (List (Option W32)) := do -- `dup`, then bitwise operations and comparisons over known words. if instruction.opcode == 37 then - let value :: stack := state.stack | none - return { state with stack := value :: value :: stack } + let value :: rest := stack | none + return value :: value :: rest if instruction.opcode ∈ [95, 96, 97] || (65025 <= instruction.opcode && instruction.opcode <= 65029) then - let right :: left :: stack := state.stack | none + let right :: left :: rest := stack | none let value := do let a ← left let b ← right @@ -334,48 +218,78 @@ private def featureTransferWith (layouts : Array RawLayout) (raw : RawMethod) else if instruction.opcode == 96 then some (a ||| b) else if instruction.opcode == 97 then some (a ^^^ b) else constantComparison instruction.opcode a b - return { state with stack := value :: stack } + return value :: rest -- `call` and `newobj`; a feature getter pushes the profile's answer. if instruction.opcode == 41 then let .indirectCall result parameters false false 0 := instruction.operand | none - forget state (parameters.length + 1) (if result == .void then 0 else 1) + forget stack (parameters.length + 1) (if result == .void then 0 else 1) else if instruction.opcode == 40 || instruction.opcode == 111 || instruction.opcode == 115 then let .call site := instruction.operand | none - let next ← forget state + let next ← forget stack (site.parameterCount + if instruction.opcode != 115 && site.hasThis then 1 else 0) 0 - if let some constant := referenceContentConstant layouts site <|> platformConstant site then + if let some constant := referenceContentFact layouts site then if instruction.opcode != 40 then none else - return { next with stack := some constant :: next.stack } - if let some feature := featureGetter site then - if instruction.opcode != 40 then none else - return { next with stack := some (if profile.evaluate feature then 1 else 0) :: next.stack } - return { next with stack := - if instruction.opcode == 115 || site.returnsValue then none :: next.stack else next.stack } + return some constant :: next + if let some model := platformGetterModel site then + guard (instruction.opcode == 40) + match model with + | .feature feature => return some (if profile.evaluate feature then 1 else 0) :: next + | .constant word => return some word :: next + | _ => return none :: next + return if instruction.opcode == 115 || site.returnsValue then none :: next else next -- `ret`, then every other opcode by its fixed stack effect. - if instruction.opcode == 42 then forget state (if raw.returnType == .void then 0 else 1) 0 + if instruction.opcode == 42 then forget stack (if raw.returnType == .void then 0 else 1) 0 else let (pops, pushes) ← rawStackEffect instruction.opcode - forget state pops pushes + forget stack pops pushes + +/-- Recompute a transfer from raw CLI encodings and metadata, independently of the + extractor's worklist and its claimed incoming/outgoing facts. -/ +private def featureTransferWith {Locals : Type} (layouts : Array RawLayout) (raw : RawMethod) + (facts : LocalFacts Locals) + (profile : FeatureProfile) + (instruction : RawInstruction) (state : FeatureFacts Locals) : + Option (FeatureFacts Locals) := do + if let some word := constant32 instruction <|> platformField instruction then + return { state with stack := some word :: state.stack } + -- `ldloc` and `stloc`; a store keeps its fact only in an eligible local. + if instruction.opcode ∈ [6, 7, 8, 9, 17, 65036] then + let index ← localIndex instruction + let value ← facts.load state.locals index + return { state with stack := value :: state.stack } + if instruction.opcode ∈ [10, 11, 12, 13, 19, 65038] then + let index ← localIndex instruction + let value :: stack := state.stack | none + let locals ← facts.store state.locals index + (if facts.eligible index value then value else none) + return { stack, locals } + let stack ← featureStackTransfer layouts raw profile instruction state.stack + return { state with stack } + +/-- The known facts of a method's locals under the raw local-eligibility rule. -/ +def knownLocals (raw : RawMethod) : LocalFacts (List (Nat × W32)) := + LocalFacts.known raw.locals.length (featureLocalEligible raw) -/-- Recompute the transfer using the raw local-eligibility rule. -/ +/-- Recompute the transfer of a certificate state using the raw local-eligibility rule. -/ def featureTransfer (layouts : Array RawLayout) (raw : RawMethod) := - featureTransferWith layouts raw (featureLocalEligible raw) + featureTransferWith layouts raw (knownLocals raw) /-- A decided type test replaces the raw comparison result by its exact word, and a parameterless static call pushes `constant`, the word its emitted callee is known to return. -/ -private def featureTransferAtWith (layouts : Array RawLayout) (raw : RawMethod) - (eligible : Nat → Option W32 → Bool) +private def featureTransferAtWith {Locals : Type} (layouts : Array RawLayout) (raw : RawMethod) + (facts : LocalFacts Locals) (profile : FeatureProfile) (constant : Option W32) (role : Option FoldRole) - (instruction : RawInstruction) (state : FeatureState) : Option FeatureState := + (instruction : RawInstruction) (state : FeatureFacts Locals) : + Option (FeatureFacts Locals) := match role with | some .tailCall => match instruction.operand with | .indirectCall _ parameters false false 0 => if state.stack.length == parameters.length + 1 then - featureTransferWith layouts raw eligible profile instruction state else none + featureTransferWith layouts raw facts profile instruction state else none | .call site => if state.stack.length == site.parameterCount + (if site.hasThis then 1 else 0) then - featureTransferWith layouts raw eligible profile instruction state else none + featureTransferWith layouts raw facts profile instruction state else none | _ => none | some (.result value) => match state.stack with | _ :: _ :: stack => some { state with stack := some (if value then 1 else 0) :: stack } @@ -384,12 +298,13 @@ private def featureTransferAtWith (layouts : Array RawLayout) (raw : RawMethod) | ⟨_, 40, .call site⟩ => match site.parameterCount == 0 && !site.hasThis && site.returnsValue, constant with | true, some word => some { state with stack := some word :: state.stack } - | _, _ => featureTransferWith layouts raw eligible profile instruction state - | _ => featureTransferWith layouts raw eligible profile instruction state + | _, _ => featureTransferWith layouts raw facts profile instruction state + | _ => featureTransferWith layouts raw facts profile instruction state -/-- The folded transfer stated independently of any local-eligibility index. -/ +/-- The folded transfer of a certificate state, stated independently of any local-eligibility + index. -/ def featureTransferAt (layouts : Array RawLayout) (raw : RawMethod) := - featureTransferAtWith layouts raw (featureLocalEligible raw) + featureTransferAtWith layouts raw (knownLocals raw) /-- The taken target or the next instruction for a decided conditional branch. -/ private def selectedSuccessor (pc target : Nat) (taken : Bool) : List Nat := @@ -397,7 +312,7 @@ private def selectedSuccessor (pc target : Nat) (taken : Bool) : List Nat := /-- Known integer conditions and closed type tests select an edge; unknowns retain both. -/ def featureSuccessors (role : Option FoldRole) (pc : Nat) - (instruction : RawInstruction) (state : FeatureState) : + (instruction : RawInstruction) (stack : List (Option W32)) : Option (List Nat) := if let some (.typeBranch taken) := role then match instruction.operand with @@ -411,7 +326,7 @@ def featureSuccessors (role : Option FoldRole) (pc : Nat) match instruction.operand with | .target target => some [target] | _ => none else if instruction.opcode == 69 then match instruction.operand with - | .targets targets => match state.stack with + | .targets targets => match stack with | some selector :: _ => some [targets[selector.toNat]?.getD (pc + 1)] | _ => some ((pc + 1) :: targets) | _ => none @@ -420,12 +335,12 @@ def featureSuccessors (role : Option FoldRole) (pc : Nat) (57 <= instruction.opcode && instruction.opcode <= 68) then do let .target target := instruction.operand | none if instruction.opcode ∈ [44, 45, 57, 58] then - match state.stack with + match stack with | some value :: _ => return selectedSuccessor pc target ((value != 0) == (instruction.opcode == 45 || instruction.opcode == 58)) | _ => return [target, pc + 1] - else match state.stack with + else match stack with | some right :: some left :: _ => match constantComparison instruction.opcode left right with | some value => return selectedSuccessor pc target (value != 0) @@ -446,16 +361,17 @@ private def rawFeatureEffect (instruction : RawInstruction) : Option (Nat × Nat /-- Raw and emitted instructions have the same known stack effect, and an emitted call or construction comes from the matching raw opcode. -/ -private def fallbackFeatureEffect (instruction : RawInstruction) (emitted : Op) : Bool := +private def fallbackFeatureEffect (instruction : RawInstruction) (emitted : Instruction) : Bool := let returnsValue := match instruction.operand with | .call site => site.returnsValue | .indirectCall result .. => result != .void | _ => false let properCall := match emitted with | .call .. => instruction.opcode == 40 || instruction.opcode == 111 - | .callInstance .. | .callVirtual .. => instruction.opcode == 111 - | .newAggregate .. | .newVector256 .. | .newObject .. => instruction.opcode == 115 - | .objectTest .. => instruction.opcode ∈ [116, 117] + | .callvirt .. => instruction.opcode == 111 + | .newobj _ => instruction.opcode == 115 + | .castclass _ | .isinst _ => + (typeTestSource emitted).any fun (opcode, _) => opcode == instruction.opcode | _ => true properCall && match rawFeatureEffect instruction, emittedFeatureEffect emitted returnsValue with | some rawEffect, some emittedEffect => rawEffect == emittedEffect @@ -463,230 +379,715 @@ private def fallbackFeatureEffect (instruction : RawInstruction) (emitted : Op) /-- Bind every control-flow operation and every operation that propagates a known feature fact to the actual emitted instruction. Unknown operations may not - emit an unsupported hole on a reachable path. -/ -private def featureOpMatchesWith (layouts : Array RawLayout) (raw : RawMethod) - (eligible : Nat → Option W32 → Bool) - (instruction : RawInstruction) (emitted : Op) : Bool := + emit a hole on a reachable path. -/ +def featureOpMatches (layouts : Array RawLayout) (raw : RawMethod) + (instruction : RawInstruction) (emitted : Instruction) : Bool := Id.run do -- These terminal/transfer encodings have no admitted emitted counterpart. if instruction.opcode ∈ [39, 122, 220, 65041, 65044, 65050] then return false if (match emitted with - | .tailCall .. | .managed (.call _ _ true) => true | _ => false) then return false + | .call _ prefixes => prefixes.tail | .calli _ true => true | _ => false) then + return false if instruction.opcode == 65039 then return match instruction.operand, emitted with - | .none, .stackAlloc flag => flag == raw.initLocals + | .none, .localloc => true | _, _ => false - if let .stackAlloc _ := emitted then return false + if let .localloc := emitted then return false if let some word := constant32 instruction <|> platformField instruction then - return match emitted with | .const32 actual => word == actual | _ => false + return match emitted with | .ldc_i4 actual => word == actual | _ => false -- Any other static load is never a constant. - if instruction.opcode == 126 && (match emitted with | .const32 _ => true | _ => false) then + if instruction.opcode == 126 && (match emitted with | .ldc_i4 _ => true | _ => false) then return false if let .call site := instruction.operand then - if referenceContentQuery site then - return match referenceContentConstant layouts site, emitted with - | some expected, .const32 actual => instruction.opcode == 40 && expected == actual - | _, _ => false - if platformQuery site then - return match platformConstant site, emitted with - | some expected, .const32 actual => instruction.opcode == 40 && expected == actual - | _, _ => false - if let some feature := featureGetter site then + if (referenceContentFact layouts site).isSome || (platformGetterModel site).isSome then return match emitted with - | .feature actual => instruction.opcode == 40 && feature == actual + | .call method prefixes => instruction.opcode == 40 && method == site.method && + !prefixes.tail && prefixes.constrained.isNone | _ => false + if emitted matches .ldc_i4 _ then return false if instruction.opcode ∈ [6, 7, 8, 9, 17, 65036, 10, 11, 12, 13, 19, 65038] then let some index := localIndex instruction | return false - let narrow := match raw.locals[index]? with - | some (.atom _ "System.Boolean") => some NarrowKind.unsigned8 | _ => none return match emitted with - | .local actual actualNarrow => - instruction.opcode ∈ [6, 7, 8, 9, 17, 65036] && actual == index && - (!eligible index (some 0) || actualNarrow == narrow) - | .setLocal actual actualNarrow => - instruction.opcode ∈ [10, 11, 12, 13, 19, 65038] && actual == index && - (!eligible index (some 0) || actualNarrow == narrow) + | .ldloc actual => instruction.opcode ∈ [6, 7, 8, 9, 17, 65036] && actual == index + | .stloc actual => instruction.opcode ∈ [10, 11, 12, 13, 19, 65038] && actual == index | _ => false if instruction.opcode ∈ [18, 65037] then return match localIndex instruction, emitted with - | some index, .localAddr actual => index == actual - | some index, .local actual none => index == actual && spanLocalOp raw.locals instruction + | some index, .ldloca actual => index == actual | _, _ => false - -- Argument homes are appended after all raw locals. Their precise mapping is - -- independently checked by ParameterHomesValid; they cannot alias tracked locals. + -- An argument's address and store name the argument itself; ParameterHomesValid checks that + -- it has a home, so neither can alias a tracked local. if instruction.opcode ∈ [15, 65034] then - return match emitted with - | .localAddr index | .local index none => raw.locals.length <= index - | _ => false + return match argumentOperation instruction, emitted with + | some (.address index), .ldarga actual => index == actual + | _, _ => false if instruction.opcode ∈ [16, 65035] then - return match emitted with | .setLocal index none => raw.locals.length <= index | _ => false - -- A span address on the stack is already the view it names, so copying the span emits nothing. - if instruction.opcode == 113 then + return match argumentOperation instruction, emitted with + | some (.store index), .starg actual => index == actual + | _, _ => false + -- A span load forwards its view and retains the raw span operand. + if instruction.opcode == 0x71 then if let .type type := instruction.operand then - if spanLocal type then return match emitted with | .nop => true | _ => false - -- A native-width wrapper is looked through only for bitwise operations, comparisons and - -- two-operand branches. - let emitted := match emitted with - | .nativeBinary operation => if instruction.opcode ∈ [95, 96, 97] || - (65025 <= instruction.opcode && instruction.opcode <= 65029) || - (46 <= instruction.opcode && instruction.opcode <= 55) || - (59 <= instruction.opcode && instruction.opcode <= 68) then operation else emitted - | _ => emitted + if spanLocal type then + return match emitted with | .ldobj operand => operand == type | _ => false return match instruction.opcode, instruction.operand, emitted with -- Stack duplication, bitwise operations and exact comparison signedness. - | 37, _, .dup | 95, _, .band | 96, _, .bor | 97, _, .bxor | 65025, _, .eq => true - | 65026, _, .gt | 65027, _, .gtu | 65028, _, .lt | 65029, _, .ltu => true + | 37, _, .dup | 95, _, .and | 96, _, .or | 97, _, .xor | 65025, _, .ceq => true + | 65026, _, .cgt | 65027, _, .cgt_un | 65028, _, .clt | 65029, _, .clt_un => true -- Branches, short and long forms, must keep their target. - | 43, .target x, .branch y | 56, .target x, .branch y - | 44, .target x, .brzero y | 57, .target x, .brzero y - | 45, .target x, .brnonzero y | 58, .target x, .brnonzero y + | 43, .target x, .br y | 56, .target x, .br y + | 44, .target x, .brfalse y | 57, .target x, .brfalse y + | 45, .target x, .brtrue y | 58, .target x, .brtrue y | 46, .target x, .beq y | 59, .target x, .beq y | 47, .target x, .bge y | 60, .target x, .bge y | 48, .target x, .bgt y | 61, .target x, .bgt y | 49, .target x, .ble y | 62, .target x, .ble y | 50, .target x, .blt y | 63, .target x, .blt y - | 51, .target x, .bne y | 64, .target x, .bne y - | 52, .target x, .bgeu y | 65, .target x, .bgeu y - | 53, .target x, .bgtu y | 66, .target x, .bgtu y - | 54, .target x, .bleu y | 67, .target x, .bleu y - | 55, .target x, .bltu y | 68, .target x, .bltu y => x == y + | 51, .target x, .bne_un y | 64, .target x, .bne_un y + | 52, .target x, .bge_un y | 65, .target x, .bge_un y + | 53, .target x, .bgt_un y | 66, .target x, .bgt_un y + | 54, .target x, .ble_un y | 67, .target x, .ble_un y + | 55, .target x, .blt_un y | 68, .target x, .blt_un y => x == y | 69, .targets x, .switch y => x == y - | 42, _, .ret _ => true + | 42, _, .ret => true -- No other raw instruction may emit control flow, a feature test or a hole. - | _, _, .unsupported _ | _, _, .throw _ | _, _, .feature _ | _, _, .branch _ | _, _, .switch _ - | _, _, .brzero _ | _, _, .brnonzero _ => false + | _, _, .unadmitted _ | _, _, .throw _ | _, _, .br _ | _, _, .switch _ + | _, _, .brfalse _ | _, _, .brtrue _ => false | opcode, _, _ => !(opcode ∈ [37, 95, 96, 97, 42] || (65025 <= opcode && opcode <= 65029) || (43 <= opcode && opcode <= 69)) && fallbackFeatureEffect instruction emitted -/-- Bind an emitted operation using the raw local-eligibility rule. -/ -def featureOpMatches (layouts : Array RawLayout) (raw : RawMethod) := - featureOpMatchesWith layouts raw (featureLocalEligible raw) - -/-- A decided typeof branch preserves its raw target or falls through without a stack effect. -/ -def typeBranchMatches (instruction : RawInstruction) (taken : Bool) (emitted : Op) : Bool := - match instruction.operand, emitted with - | .target target, .branch actual => taken && actual == target - | .target _, .nop => !taken - | _, _ => false - -/-- Fold operands emit nothing, a type comparison emits its decided word or branch and a folded - `throw` raises its constructed type. Outside a decided test, type handles and - `System.Type` calls have no admitted translation. -/ -private def featureOpMatchesAtWith (layouts : Array RawLayout) (raw : RawMethod) - (eligible : Nat → Option W32 → Bool) +/-- A closed type comparison retains its decoded loads, calls and comparison. A null operand in a + folded exception sequence pushes its object reference, and the throw raises the constructed type. -/ +def featureOpMatchesAt (layouts : Array RawLayout) (raw : RawMethod) (role : Option FoldRole) - (instruction : RawInstruction) (emitted : Op) : Bool := + (instruction : RawInstruction) (emitted : Instruction) + (previous : Option RawInstruction := none) : Bool := match role, emitted with - | some .head, .nop | some .interior, .nop => true - | some (.result value), .const32 word => word == if value then 1 else 0 - | some (.typeBranch taken), _ => typeBranchMatches instruction taken emitted - | some (.raise name), .throw exception => name == exception - | some (.spanConstructor index constructor), .initializeSpanLocal actual operation => - index == actual && spanConstructorMatches constructor operation - | some .tailCall, .tailCall target count => match instruction.operand with - | .call site => site.returnType == raw.returnType && - featureOpMatchesWith layouts raw eligible instruction (.call target count) + | some .head, .nop | some .interior, .nop => instruction.opcode != 20 + | some .head, .ldnull | some .interior, .ldnull => + instruction.opcode == 20 && instruction.operand == .none + | some .typeHead, _ | some .typeOperand, _ | some (.result _), _ | some (.typeBranch _), _ => + valueEncodingMatches previous instruction emitted + | some (.raise raised), .throw exception => raised == exception + | some .tailCall, .call method prefixes => match instruction.operand with + | .call site => prefixes.tail && site.returnType == raw.returnType && + featureOpMatches layouts raw instruction (.call method { prefixes with tail := false }) | _ => false - | some .tailCall, .managed (.call signature catalog true) => + | some .tailCall, .calli signature true => signature.result == callableType raw.returnType && - featureOpMatchesWith layouts raw eligible instruction - (.managed (.call signature catalog false)) - | some .argument, _ => featureOpMatchesWith layouts raw eligible instruction emitted + featureOpMatches layouts raw instruction + (.calli signature false) + | some .argument, _ => valueEncodingMatches previous instruction emitted && + featureOpMatches layouts raw instruction emitted | some _, _ => false | none, _ => !typeTestInstruction instruction && - featureOpMatchesWith layouts raw eligible instruction emitted + featureOpMatches layouts raw instruction emitted + +/-- A fixed fold role accepts exactly its decoded instruction, including the branch target + and the exception kind. -/ +theorem fixedRole_matches (layouts : Array RawLayout) (method : RawMethod) + (role : FoldRole) (previous : Option RawInstruction) (raw : RawInstruction) + (emitted : Instruction) + (fixed : match role with + | .head | .interior | .typeHead | .typeOperand | .result _ | .typeBranch _ | .raise _ => True | _ => False) : + featureOpMatchesAt layouts method (some role) raw emitted previous = true ↔ + Instruction.ofRole? role previous raw = some emitted := by + cases role with + | typeHead | typeOperand | result | typeBranch => + exact valueEncodingMatches_iff + | argument | tailCall | blocked => exact fixed.elim + | head | interior => + by_cases null : raw.opcode = 20 <;> by_cases valid : raw.operand = .none <;> + cases emitted <;> simp [featureOpMatchesAt, Instruction.ofRole?, null, valid] + | raise => + cases emitted <;> first | exact Iff.rfl | simp [featureOpMatchesAt, Instruction.ofRole?] -/-- Fold-aware instruction matching stated independently of any eligibility index. -/ -def featureOpMatchesAt (layouts : Array RawLayout) (raw : RawMethod) := - featureOpMatchesAtWith layouts raw (featureLocalEligible raw) +#print axioms fixedRole_matches + +/-- An accepted fold argument has the value and index of its encoded push or return. -/ +theorem argument_of_featureOpMatchesAt {layouts : Array RawLayout} {method : RawMethod} + {raw : RawInstruction} {emitted : Instruction} {previous : Option RawInstruction} + (checked : featureOpMatchesAt layouts method (some .argument) raw emitted previous = true) : + Instruction.ofRole? .argument previous raw = some emitted := by + exact valueEncodingMatches_sound (Bool.and_eq_true_iff.mp checked).1 + +#print axioms argument_of_featureOpMatchesAt + +/-- A live managed tail call has the method named by its raw operand, exactly the tail prefix, + and a linked callee with that operand's token and type arguments. -/ +theorem ManagedCallsValid.tailCall_target {types : List DispatchType} + {methods : List SpecializedMethod} {imports : List ImportSlot} + {modelled : CallOpcode × MethodRef → Bool} + {raw : RawMethod} {reachable : List Bool} {body : Method} {layouts : Array RawLayout} + (calls : ManagedCallsValid methods imports modelled (lowerConstrainedRaw types raw) + reachable body) + (constrained : ConstrainedCallsValid types methods raw reachable body) + {pc target token prefixOffset callOffset : Nat} {site : CallSite} {op : Instruction} + (rawAt : raw.code[pc]? = some ⟨callOffset, 40, .call site⟩) + (tailAt : rawBefore (raw.code[·]?) pc = some ⟨prefixOffset, 0xFE14, .none⟩) + (found : body.code[pc]? = some op) (live : reachable[pc]? = some true) + (feature : featureOpMatchesAt layouts raw (some .tailCall) ⟨callOffset, 40, .call site⟩ + op (rawBefore (raw.code[·]?) pc) = true) + (managed : site.target = .managed token) (linked : body.linkAt pc = some (.direct target)) : + Instruction.ofRole? .tailCall (rawBefore (raw.code[·]?) pc) ⟨callOffset, 40, .call site⟩ = + some op ∧ ∃ callee, methods[target]? = some callee ∧ callee.closed.token = token ∧ + callee.arguments = site.typeArguments := by + have previous : raw.code[pc - 1]? = some ⟨prefixOffset, 0xFE14, .none⟩ := by + unfold rawBefore at tailAt + split at tailAt <;> simp_all + have lowered : (lowerConstrainedRaw types raw).code[pc]? = some ⟨callOffset, 40, .call site⟩ := by + rw [lowerConstrainedRaw_eq_original] + split + · exact rawAt + · simp [List.getElem?_map, List.getElem?_zipIdx, rawAt, previous] + have shape : ∃ method prefixes, op = .call method prefixes ∧ prefixes.tail = true := by + cases op <;> first | contradiction | skip + case call method prefixes => + exact ⟨_, _, rfl, (Bool.and_eq_true_iff.mp (Bool.and_eq_true_iff.mp feature).1).1⟩ + case calli => + have fits := calls.linkFits found rfl + rw [linked] at fits + contradiction + obtain ⟨method, prefixes, rfl, tail⟩ := shape + have window := List.all_eq_true.mp + ((ConstrainedCallsValid_iff_original _ _ _ _ _).mp constrained).2.2 pc + (List.mem_range.mpr (List.getElem?_eq_some_iff.mp rawAt).1) + have noReceiver : prefixes.constrained = none := by + simpa [constrainedSiteValid, constrainedSiteCheck, constrainedSiteCheckWith, + constrainedSiteWindow, live, rawAt, previous, found, typeTestSource] using window + obtain ⟨operand, callee⟩ := calls.direct_operand found live lowered rfl managed linked + have name : method = site.method := by simpa [callOperandMatches, noReceiver] using operand + refine ⟨?_, callee⟩ + rw [tailAt, name] + obtain ⟨flag, receiver⟩ := prefixes + change flag = true at tail + change receiver = none at noReceiver + subst flag receiver + rfl + +#print axioms ManagedCallsValid.tailCall_target + +/-- The feature check reads a raw instruction together with its predecessor and closed role. -/ +private structure FeatureSite where + pc : Nat + previous : Option RawInstruction + instruction : RawInstruction + role : Option FoldRole + +/-- Pair each raw site with the modifier immediately before it, including across dead sites. -/ +private def featureSites (pc : Nat) (previous : Option RawInstruction) : + List RawInstruction → List (Option FoldRole) → List FeatureSite + | instruction :: code, role :: roles => + ⟨pc, previous, instruction, role⟩ :: featureSites (pc + 1) (some instruction) code roles + | _, _ => [] + +/-- The streamed site at a pc has exactly the raw predecessor specified by that pc. -/ +private theorem featureSites_at {code : List RawInstruction} {roles : List (Option FoldRole)} + {pc : Nat} {instruction : RawInstruction} {role : Option FoldRole} + (rawAt : code[pc]? = some instruction) (roleAt : roles[pc]? = some role) : + (featureSites 0 none code roles)[pc]? = + some ⟨pc, rawBefore (code[·]?) pc, instruction, role⟩ := by + have walk (start : Nat) (previous : Option RawInstruction) : + (featureSites start previous code roles)[pc]? = + some ⟨start + pc, (if pc == 0 then previous else code[pc - 1]?), instruction, role⟩ := by + induction pc generalizing start previous code roles with + | zero => + cases code <;> cases roles <;> simp_all [featureSites] + | succ index ih => + cases code with + | nil => simp at rawAt + | cons head tail => + cases roles with + | nil => simp at roleAt + | cons first rest => + have checked := ih (by simpa using rawAt) (by simpa using roleAt) + (start + 1) (some head) + cases index <;> simpa [featureSites, Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] + using checked + simpa [rawBefore] using walk 0 none + +#print axioms featureSites_at /-- One pc of the certificate: an unreachable pc must emit a hole; a reachable one must - be well shaped, match its emitted instruction, and flow into a weaker state at every - recomputed successor. -/ -private def checkFeatureSiteWith (layouts : Array RawLayout) (constants : Nat → Option W32) - (raw : RawMethod) - (eligible : Nat → Option W32 → Bool) + be well shaped, match its emitted instruction, including the indirect access rule over the + raw instructions `rawAt` gives, and flow into a weaker state at every recomputed + successor. -/ +private def checkFeatureSiteWith {Locals : Type} (layouts : Array RawLayout) + (constants : Nat → Option W32) (linkAt : Nat → Option Link) (raw : RawMethod) + (facts : LocalFacts Locals) (profile : FeatureProfile) - (certificate : Nat → Option (Option FeatureState)) - (roles : Nat → Option (Option FoldRole)) : - (RawInstruction × Option FoldRole) × Nat → Option FeatureState → Op → Bool - | _, none, .unsupported _ => true - | ((instruction, role), pc), some state, emitted => - wellShaped raw eligible state && - featureOpMatchesAtWith layouts raw eligible role instruction emitted && - match featureTransferAtWith layouts raw eligible profile (calleeConstant constants emitted) + (certificate : Nat → Option (Option (FeatureFacts Locals))) + (roles : Nat → Option (Option FoldRole)) (rawAt : Nat → Option RawInstruction) : + FeatureSite → Option (FeatureFacts Locals) → Instruction → Bool + | _, none, .unadmitted _ => true + | ⟨pc, previous, instruction, role⟩, some state, emitted => + wellShaped raw facts state && + (featureOpMatchesAt layouts raw role instruction emitted previous && + indirectSiteWindow rawAt pc instruction emitted false previous) && + match featureTransferAtWith layouts raw facts profile + (calleeConstant constants (linkAt pc) emitted) role instruction state, - featureSuccessors role pc instruction state with + featureSuccessors role pc instruction state.stack with | some after, some successors => -- Even an unselected encoded target must be a valid entry, not a fold's interior. - let validTarget := fun target => - let role := roles target - role.isSome && foldEntry role + let validTarget := fun target => foldEntry (roles target) (match instruction.operand with | .target target => validTarget target | .targets targets => targets.all validTarget | _ => true) && successors.all fun target => match certificate target with - | some (some incoming) => after.weakens incoming + | some (some incoming) => FeatureFacts.weakens facts after incoming | _ => false | _, _ => false | _, none, _ => false -/-- The per-site specification uses the original raw-metadata eligibility rule. -/ -private def checkFeatureSite (layouts : Array RawLayout) (constants : Nat → Option W32) - (raw : RawMethod) := - checkFeatureSiteWith layouts constants raw (featureLocalEligible raw) - /-- No optional sites: entry, every raw PC, the entire parameter-reachability mask, - every successor, and all unsupported holes are covered. `constants` gives, by emitted method - index, the word a parameterless call to that method pushes. -/ + every successor, and all holes are covered. The entry state has no known fact. + `constants` gives, by emitted method index, the word a parameterless call to that method + pushes. -/ def FeatureFlowValid (layouts : Array RawLayout) (constants : Nat → Option W32) - (raw : RawMethod) (profile : FeatureProfile) - (certificate : FeatureCertificate) (reachable : List Bool) (code : List Op) : Prop := + (linkAt : Nat → Option Link) (raw : RawMethod) (profile : FeatureProfile) + (certificate : FeatureCertificate) (reachable : List Bool) (code : List Instruction) : Prop := certificate.length = raw.code.length ∧ code.length = raw.code.length ∧ reachable = certificate.map Option.isSome ∧ - certificate[0]? = some (some { stack := [], locals := List.replicate raw.locals.length none }) ∧ - (let eligible := (FeatureLocalIndex.ofRaw raw).eligible - all3Flow (fun lookup => checkFeatureSiteWith layouts constants raw eligible profile lookup - (FlowIndex.ofList (foldRoles raw.locals raw.code)).get?) + certificate[0]? = some (some { stack := [], locals := [] }) ∧ + (let facts := LocalFacts.known raw.locals.length (FeatureLocalIndex.ofRaw raw).eligible + all3Flow (fun lookup => checkFeatureSiteWith layouts constants linkAt raw facts profile lookup + (FlowIndex.ofList (foldRoles raw.code)).get? (FlowIndex.ofList raw.code).get?) (.ofList certificate) 0 - (raw.code.zip (foldRoles raw.locals raw.code)).zipIdx certificate code) = true + (featureSites 0 none raw.code (foldRoles raw.code)) certificate code) = true -instance (layouts : Array RawLayout) (constants : Nat → Option W32) (raw : RawMethod) - (profile : FeatureProfile) (certificate : FeatureCertificate) - (reachable : List Bool) (code : List Op) : - Decidable (FeatureFlowValid layouts constants raw profile certificate reachable code) := +instance (layouts : Array RawLayout) (constants : Nat → Option W32) + (linkAt : Nat → Option Link) (raw : RawMethod) (profile : FeatureProfile) + (certificate : FeatureCertificate) (reachable : List Bool) (code : List Instruction) : + Decidable (FeatureFlowValid layouts constants linkAt raw profile certificate reachable code) := inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _)) +/-- The specification of the feature check over full per-local lists: the entry state has no + known fact, and every site is checked against the complete certificate and fold-role lists + with the raw local-eligibility rule. -/ +def FeatureListValid (layouts : Array RawLayout) (constants : Nat → Option W32) + (linkAt : Nat → Option Link) (raw : RawMethod) (profile : FeatureProfile) + (certificate : FeatureListCertificate) (reachable : List Bool) (code : List Instruction) : + Prop := + certificate.length = raw.code.length ∧ code.length = raw.code.length ∧ + reachable = certificate.map Option.isSome ∧ + certificate[0]? = some (some + { stack := [], locals := List.replicate raw.locals.length none }) ∧ + all3 (checkFeatureSiteWith layouts constants linkAt raw + (LocalFacts.list raw.locals.length (featureLocalEligible raw)) profile + (fun target => certificate[target]?) + (fun target => (foldRoles raw.code)[target]?) + (fun target => raw.code[target]?)) + (featureSites 0 none raw.code (foldRoles raw.code)) certificate code = true + /-- Streaming validation has exactly the same entry, mask, shape and successor checks as indexing the complete certificate and fold-role lists at every site. -/ -theorem FeatureFlowValid_iff_list (layouts : Array RawLayout) (constants : Nat → Option W32) - (raw : RawMethod) (profile : FeatureProfile) - (certificate : FeatureCertificate) (reachable : List Bool) (code : List Op) : - FeatureFlowValid layouts constants raw profile certificate reachable code ↔ +private theorem FeatureFlowValid_iff_indexed (layouts : Array RawLayout) + (constants : Nat → Option W32) (linkAt : Nat → Option Link) (raw : RawMethod) + (profile : FeatureProfile) + (certificate : FeatureCertificate) (reachable : List Bool) (code : List Instruction) : + FeatureFlowValid layouts constants linkAt raw profile certificate reachable code ↔ certificate.length = raw.code.length ∧ code.length = raw.code.length ∧ reachable = certificate.map Option.isSome ∧ - certificate[0]? = some (some - { stack := [], locals := List.replicate raw.locals.length none }) ∧ - all3 (checkFeatureSite layouts constants raw profile (fun target => certificate[target]?) - (fun target => (foldRoles raw.locals raw.code)[target]?)) - (raw.code.zip (foldRoles raw.locals raw.code)).zipIdx certificate code = true := by + certificate[0]? = some (some { stack := [], locals := [] }) ∧ + all3 (checkFeatureSiteWith layouts constants linkAt raw (knownLocals raw) profile + (fun target => certificate[target]?) + (fun target => (foldRoles raw.code)[target]?) + (fun target => raw.code[target]?)) + (featureSites 0 none raw.code (foldRoles raw.code)) certificate code = true := by unfold FeatureFlowValid have eligible : (FeatureLocalIndex.ofRaw raw).eligible = featureLocalEligible raw := by funext index fact exact FeatureLocalIndex.eligible_eq raw index fact rw [eligible] rw [all3Flow_eq _ _ _ _ _ _ List.drop_zero.symm] - have roles : (FlowIndex.ofList (foldRoles raw.locals raw.code)).get? = - (fun target => (foldRoles raw.locals raw.code)[target]?) := by + have roles : (FlowIndex.ofList (foldRoles raw.code)).get? = + (fun target => (foldRoles raw.code)[target]?) := by + funext target + exact FlowIndex.get?_ofList _ _ + have rawAt : (FlowIndex.ofList raw.code).get? = (fun target => raw.code[target]?) := by funext target exact FlowIndex.get?_ofList _ _ - rw [roles] + rw [roles, rawAt] rfl -#print axioms FeatureFlowValid_iff_list +#print axioms FeatureFlowValid_iff_indexed + +/-- At a live instruction the feature certificate checks the role and the actual predecessor. -/ +theorem FeatureFlowValid.instruction_checked {layouts : Array RawLayout} {raw : RawMethod} + {constants : Nat → Option W32} {linkAt : Nat → Option Link} {profile : FeatureProfile} + {certificate : FeatureCertificate} {reachable : List Bool} {code : List Instruction} + (valid : FeatureFlowValid layouts constants linkAt raw profile certificate reachable code) + {pc : Nat} {instruction : RawInstruction} {role : Option FoldRole} + {state : FeatureState} {emitted : Instruction} + (rawAt : raw.code[pc]? = some instruction) + (roleAt : (foldRoles raw.code)[pc]? = some role) + (stateAt : certificate[pc]? = some (some state)) (codeAt : code[pc]? = some emitted) : + featureOpMatchesAt layouts raw role instruction emitted + (rawBefore (raw.code[·]?) pc) = true := by + have pairAt := featureSites_at rawAt roleAt + have sites := ((FeatureFlowValid_iff_indexed _ _ _ _ _ _ _ _).mp valid).2.2.2.2 + have checked := all3_at sites pairAt stateAt codeAt + simp only [checkFeatureSiteWith, Bool.and_eq_true] at checked + exact checked.1.2.1 + +#print axioms FeatureFlowValid.instruction_checked + +/-- Expanding a known-fact state commutes with the transfer, which keeps its locals known. -/ +private theorem featureTransferWith_expand (layouts : Array RawLayout) (raw : RawMethod) + (count : Nat) (eligible : Nat → Option W32 → Bool) (profile : FeatureProfile) + (instruction : RawInstruction) (state : FeatureState) + (known : knownBelow count 0 state.locals = true) : + featureTransferWith layouts raw (LocalFacts.list count eligible) profile instruction + (state.expand count) = + (featureTransferWith layouts raw (LocalFacts.known count eligible) profile instruction + state).map (FeatureState.expand count) ∧ + ∀ after, featureTransferWith layouts raw (LocalFacts.known count eligible) profile + instruction state = some after → knownBelow count 0 after.locals = true := by + obtain ⟨stack, locals⟩ := state + simp only at known + unfold featureTransferWith + split + · constructor + · rfl + · intro after found + cases found + exact known + split + · simp only [FeatureState.expand, LocalFacts.list, LocalFacts.known, expandKnown_getElem?] + cases localIndex instruction with + | none => simp + | some index => by_cases inside : index < count <;> simp [inside, known, FeatureState.expand] + split + · simp only [FeatureState.expand, LocalFacts.list, LocalFacts.known, expandKnown_length] + cases localIndex instruction with + | none => simp + | some index => + cases stack with + | nil => simp + | cons value rest => + by_cases inside : index < count + · have stored := storeKnown_spec (index := index) + (fact := if eligible index value = true then value else none) known (Nat.zero_le _) + inside + simp [inside, show ¬ count ≤ index by omega, expandKnown_storeKnown known inside, + FeatureState.expand] + exact ⟨rfl, stored.1⟩ + · simp [inside] + · simp only [FeatureState.expand] + cases featureStackTransfer layouts raw profile instruction stack <;> + simp [known, FeatureState.expand] + +#print axioms featureTransferWith_expand + +/-- The folded transfer commutes with expansion in the same way. -/ +private theorem featureTransferAtWith_expand (layouts : Array RawLayout) (raw : RawMethod) + (count : Nat) (eligible : Nat → Option W32 → Bool) (profile : FeatureProfile) + (constant : Option W32) (role : Option FoldRole) (instruction : RawInstruction) + (state : FeatureState) (known : knownBelow count 0 state.locals = true) : + featureTransferAtWith layouts raw (LocalFacts.list count eligible) profile constant role + instruction (state.expand count) = + (featureTransferAtWith layouts raw (LocalFacts.known count eligible) profile constant role + instruction state).map (FeatureState.expand count) ∧ + ∀ after, featureTransferAtWith layouts raw (LocalFacts.known count eligible) profile constant + role instruction state = some after → knownBelow count 0 after.locals = true := by + have base := + featureTransferWith_expand layouts raw count eligible profile instruction state known + obtain ⟨stack, locals⟩ := state + simp only at known + unfold featureTransferAtWith + split + · split + · split <;> split <;> simp_all [FeatureState.expand] + · split <;> split <;> simp_all [FeatureState.expand] + · simp + · split <;> simp_all [FeatureState.expand] + · split + · split <;> simp_all [FeatureState.expand] + · exact base + +#print axioms featureTransferAtWith_expand + +/-- Expansion keeps the stack. -/ +private theorem FeatureState.expand_stack (count : Nat) (state : FeatureState) : + (state.expand count).stack = state.stack := rfl + +#print axioms FeatureState.expand_stack + +/-- Weakening of known-fact states is weakening of their expansions. -/ +private theorem weakens_expand {count : Nat} {eligible : Nat → Option W32 → Bool} + {source target : FeatureState} (sourceKnown : knownBelow count 0 source.locals = true) + (targetKnown : knownBelow count 0 target.locals = true) : + FeatureFacts.weakens (LocalFacts.list count eligible) (source.expand count) + (target.expand count) = + FeatureFacts.weakens (LocalFacts.known count eligible) source target := by + simp only [FeatureFacts.weakens, FeatureState.expand, LocalFacts.list, LocalFacts.known, + isSublist_eq_factsWeaken sourceKnown targetKnown] + +#print axioms weakens_expand + +/-- A known-fact state is well shaped exactly when its list is increasing and in range and its + expansion is well shaped. -/ +private theorem wellShaped_expand {raw : RawMethod} {count : Nat} + {eligible : Nat → Option W32 → Bool} (unknown : ∀ index, eligible index none = true) + (state : FeatureState) : + wellShaped raw (LocalFacts.known count eligible) state = + (knownBelow count 0 state.locals && + wellShaped raw (LocalFacts.list count eligible) (state.expand count)) := by + simp only [wellShaped, LocalFacts.known, LocalFacts.list, FeatureState.expand, + expandKnown_length, beq_self_eq_true, Bool.true_and] + cases known : knownBelow count 0 state.locals + · simp + · simp only [Bool.true_and] + rw [expandKnown_all unknown known] + rfl + +#print axioms wellShaped_expand + +/-- Unknown facts are eligible in every local. -/ +private theorem featureLocalEligible_none (raw : RawMethod) (index : Nat) : + featureLocalEligible raw index none = true := by + simp [featureLocalEligible] + +#print axioms featureLocalEligible_none + +/-- Every state a certificate claims holds known facts of the method's locals. -/ +private def AllKnown (raw : RawMethod) (certificate : FeatureCertificate) : Prop := + ∀ state, some state ∈ certificate → knownBelow raw.locals.length 0 state.locals = true + +/-- The certificate of full per-local lists a known-fact certificate stands for. -/ +private def expandCertificate (raw : RawMethod) (certificate : FeatureCertificate) : + FeatureListCertificate := + certificate.map (Option.map (FeatureState.expand raw.locals.length)) + +/-- On a certificate of known-fact states, each site check agrees with the list check of the + expanded certificate. -/ +private theorem checkFeatureSiteWith_known_eq_list (layouts : Array RawLayout) + (constants : Nat → Option W32) (linkAt : Nat → Option Link) (raw : RawMethod) + (profile : FeatureProfile) + (certificate : FeatureCertificate) (roles : Nat → Option (Option FoldRole)) + (rawAt : Nat → Option RawInstruction) (allKnown : AllKnown raw certificate) + (site : FeatureSite) (state : Option FeatureState) + (emitted : Instruction) (member : state ∈ certificate) : + checkFeatureSiteWith layouts constants linkAt raw (knownLocals raw) profile + (fun target => certificate[target]?) roles rawAt site state emitted = + checkFeatureSiteWith layouts constants linkAt raw + (LocalFacts.list raw.locals.length (featureLocalEligible raw)) profile + (fun target => (expandCertificate raw certificate)[target]?) roles rawAt site + (state.map (FeatureState.expand raw.locals.length)) emitted := by + cases state with + | none => cases emitted <;> rfl + | some state => + have known := allKnown state member + obtain ⟨pc, previous, instruction, role⟩ := site + have transfer := featureTransferAtWith_expand layouts raw raw.locals.length + (featureLocalEligible raw) profile (calleeConstant constants (linkAt pc) emitted) role + instruction state known + simp only [checkFeatureSiteWith, knownLocals, Option.map_some, + wellShaped_expand (featureLocalEligible_none raw), known, Bool.true_and, transfer.1, + FeatureState.expand_stack] + cases transferred : featureTransferAtWith layouts raw + (LocalFacts.known raw.locals.length (featureLocalEligible raw)) profile + (calleeConstant constants (linkAt pc) emitted) role instruction state with + | none => rfl + | some after => + have afterKnown := transfer.2 after transferred + cases featureSuccessors role pc instruction state.stack with + | none => rfl + | some successors => + simp only [Option.map_some] + congr 2 + apply List.all_congr rfl + intro target + simp only [expandCertificate, List.getElem?_map] + cases found : certificate[target]? with + | none => rfl + | some incoming => + cases incoming with + | none => rfl + | some incoming => + exact (weakens_expand afterKnown + (allKnown incoming (List.mem_of_getElem? found))).symm + +#print axioms checkFeatureSiteWith_known_eq_list + +@[simp] private theorem expandKnown_nil (count : Nat) : + expandKnown count [] = List.replicate count none := by + apply List.ext_getElem? + intro index + simp [expandKnown_getElem?, List.getElem?_replicate] + +#print axioms expandKnown_nil + +/-- The check accepts a certificate exactly when its states hold known facts and its expansion + satisfies the list specification. -/ +private theorem FeatureFlowValid_iff_expand (layouts : Array RawLayout) + (constants : Nat → Option W32) (linkAt : Nat → Option Link) (raw : RawMethod) + (profile : FeatureProfile) + (certificate : FeatureCertificate) (reachable : List Bool) (code : List Instruction) : + FeatureFlowValid layouts constants linkAt raw profile certificate reachable code ↔ + AllKnown raw certificate ∧ FeatureListValid layouts constants linkAt raw profile + (expandCertificate raw certificate) reachable code := by + rw [FeatureFlowValid_iff_indexed] + unfold FeatureListValid + have sitesEqual (allKnown : AllKnown raw certificate) := all3_map + (Option.map (FeatureState.expand raw.locals.length)) + (featureSites 0 none raw.code (foldRoles raw.code)) certificate code + fun site state emitted member => + checkFeatureSiteWith_known_eq_list layouts constants linkAt raw profile certificate + (fun target => (foldRoles raw.code)[target]?) + (fun target => raw.code[target]?) allKnown site state emitted member + constructor + · rintro ⟨length, codeLength, mask, entry, sites⟩ + have allKnown : AllKnown raw certificate := by + intro state member + obtain ⟨⟨pc, previous, instruction, role⟩, emitted, checked⟩ := all3_mem sites member + simp only [checkFeatureSiteWith, knownLocals, Bool.and_eq_true, + wellShaped_expand (featureLocalEligible_none raw)] at checked + exact checked.1.1.1 + refine ⟨allKnown, by simp [expandCertificate, length], codeLength, ?_, ?_, ?_⟩ + · simp [expandCertificate, mask, Function.comp_def] + · simp [expandCertificate, entry, FeatureState.expand] + · rw [sitesEqual allKnown] at sites + exact sites + · rintro ⟨allKnown, length, codeLength, mask, entry, sites⟩ + refine ⟨by simpa [expandCertificate] using length, codeLength, ?_, ?_, ?_⟩ + · simpa [expandCertificate, Function.comp_def] using mask + · simp only [expandCertificate, List.getElem?_map] at entry + cases found : certificate[0]? with + | none => simp [found] at entry + | some first => + cases first with + | none => simp [found] at entry + | some state => + simp only [found, Option.map_some, Option.some.injEq, FeatureState.expand, + FeatureFacts.mk.injEq] at entry + have empty : state.locals = [] := expandKnown_inj + (allKnown state (List.mem_of_getElem? found)) rfl (by rw [entry.2, expandKnown_nil]) + obtain ⟨stack, locals⟩ := state + simp only at entry empty + rw [entry.1, empty] + · rw [sitesEqual allKnown] + exact sites + +#print axioms FeatureFlowValid_iff_expand + +/-- A certificate the check accepts expands to one the list specification accepts. -/ +theorem FeatureFlowValid.expand {layouts : Array RawLayout} {constants : Nat → Option W32} + {linkAt : Nat → Option Link} + {raw : RawMethod} {profile : FeatureProfile} {certificate : FeatureCertificate} + {reachable : List Bool} {code : List Instruction} + (valid : FeatureFlowValid layouts constants linkAt raw profile certificate reachable code) : + FeatureListValid layouts constants linkAt raw profile + (certificate.map (Option.map (FeatureState.expand raw.locals.length))) reachable code := + ((FeatureFlowValid_iff_expand layouts constants linkAt raw profile certificate reachable code).mp + valid).2 + +#print axioms FeatureFlowValid.expand + +/-- The known facts of a specification state. -/ +private def FeatureListState.compress (state : FeatureListState) : FeatureState := + { stack := state.stack, locals := compressKnown 0 state.locals } + +/-- Every state a certificate the list specification accepts holds one fact per local. -/ +private theorem FeatureListValid.lengths {layouts : Array RawLayout} + {constants : Nat → Option W32} {linkAt : Nat → Option Link} {raw : RawMethod} + {profile : FeatureProfile} + {certificate : FeatureListCertificate} {reachable : List Bool} {code : List Instruction} + (valid : FeatureListValid layouts constants linkAt raw profile certificate reachable code) + {state : FeatureListState} (member : some state ∈ certificate) : + state.locals.length = raw.locals.length := by + obtain ⟨⟨pc, previous, instruction, role⟩, emitted, checked⟩ := all3_mem valid.2.2.2.2 member + simp only [checkFeatureSiteWith, wellShaped, LocalFacts.list, Bool.and_eq_true, + beq_iff_eq] at checked + exact checked.1.1.1.1 + +#print axioms FeatureListValid.lengths + +/-- Known-fact certificates with the same expansion are equal. -/ +private theorem expandCertificate_inj {raw : RawMethod} : + ∀ {first second : FeatureCertificate}, AllKnown raw first → AllKnown raw second → + expandCertificate raw first = expandCertificate raw second → first = second + | [], [], _, _, _ => rfl + | [], _ :: _, _, _, expanded | _ :: _, [], _, _, expanded => by + simp [expandCertificate] at expanded + | head :: rest, other :: others, firstKnown, secondKnown, expanded => by + simp only [expandCertificate, List.map_cons, List.cons.injEq] at expanded + have tail := expandCertificate_inj (raw := raw) (first := rest) (second := others) + (fun state member => firstKnown state (List.mem_cons_of_mem _ member)) + (fun state member => secondKnown state (List.mem_cons_of_mem _ member)) expanded.2 + rw [tail] + congr 1 + cases head with + | none => cases other <;> simp_all + | some state => + cases other with + | none => simp at expanded + | some next => + simp only [Option.map_some, Option.some.injEq, FeatureState.expand, + FeatureFacts.mk.injEq] at expanded + have locals := expandKnown_inj (firstKnown state (List.mem_cons_self ..)) + (secondKnown next (List.mem_cons_self ..)) expanded.1.2 + obtain ⟨firstStack, _⟩ := state + obtain ⟨secondStack, _⟩ := next + simp only at locals expanded + rw [expanded.1.1, locals] + +#print axioms expandCertificate_inj + +/-- Every certificate the list specification accepts is the expansion of exactly one certificate + the check accepts. -/ +theorem FeatureListValid.known {layouts : Array RawLayout} {constants : Nat → Option W32} + {linkAt : Nat → Option Link} + {raw : RawMethod} {profile : FeatureProfile} {certificate : FeatureListCertificate} + {reachable : List Bool} {code : List Instruction} + (valid : FeatureListValid layouts constants linkAt raw profile certificate reachable code) : + ∃ known : FeatureCertificate, + (FeatureFlowValid layouts constants linkAt raw profile known reachable code ∧ + known.map (Option.map (FeatureState.expand raw.locals.length)) = certificate) ∧ + ∀ other : FeatureCertificate, + FeatureFlowValid layouts constants linkAt raw profile other reachable code → + other.map (Option.map (FeatureState.expand raw.locals.length)) = certificate → + other = known := by + let known := certificate.map (Option.map FeatureListState.compress) + have expanded : expandCertificate raw known = certificate := by + simp only [expandCertificate, known, List.map_map] + conv => rhs; rw [← List.map_id certificate] + apply List.map_congr_left + intro entry member + cases entry with + | none => rfl + | some state => + have length := valid.lengths member + simp only [Function.comp_apply, Option.map_some, id, FeatureState.expand, + FeatureListState.compress] + rw [← length, expandKnown_compressKnown] + have allKnown : AllKnown raw known := by + intro state member + simp only [known, List.mem_map] at member + obtain ⟨entry, inside, compressed⟩ := member + cases entry with + | none => simp at compressed + | some listed => + simp only [Option.map_some, Option.some.injEq] at compressed + subst compressed + have length := valid.lengths inside + simp only [FeatureListState.compress] + rw [← length] + exact compressKnown_known listed.locals + have accepted : FeatureFlowValid layouts constants linkAt raw profile known reachable code := + (FeatureFlowValid_iff_expand layouts constants linkAt raw profile known reachable code).mpr + ⟨allKnown, expanded ▸ valid⟩ + refine ⟨known, ⟨accepted, expanded⟩, fun other otherValid otherExpanded => ?_⟩ + have otherKnown := + ((FeatureFlowValid_iff_expand layouts constants linkAt raw profile other reachable code).mp + otherValid).1 + exact expandCertificate_inj otherKnown allKnown (otherExpanded.trans expanded.symm) + +#print axioms FeatureListValid.known /-- A feature fact denotes an exact word, while unknown admits any value. -/ def FactRepresents (fact : Option W32) (value : Value) : Prop := @@ -728,30 +1129,30 @@ theorem factsWeaken_preserves {source target : List (Option W32)} {values : List /-- Abstract execution edges are computed from raw instructions, incoming facts and the emitted call targets, rather than copied from the extractor's claimed reachable mask. -/ -def FeatureEdge (layouts : Array RawLayout) (constants : Nat → Option W32) (raw : RawMethod) - (profile : FeatureProfile) (certificate : FeatureCertificate) (code : List Op) - (pc target : Nat) : Prop := +def FeatureEdge (layouts : Array RawLayout) (constants : Nat → Option W32) + (linkAt : Nat → Option Link) (raw : RawMethod) (profile : FeatureProfile) + (certificate : FeatureCertificate) (code : List Instruction) (pc target : Nat) : Prop := ∃ instruction role state emitted after successors, - (raw.code.zip (foldRoles raw.locals raw.code))[pc]? = some (instruction, role) ∧ + (raw.code.zip (foldRoles raw.code))[pc]? = some (instruction, role) ∧ certificate[pc]? = some (some state) ∧ code[pc]? = some emitted ∧ - featureTransferAt layouts raw profile (calleeConstant constants emitted) role instruction - state = some after ∧ - featureSuccessors role pc instruction state = some successors ∧ target ∈ successors + featureTransferAt layouts raw profile (calleeConstant constants (linkAt pc) emitted) role + instruction state = some after ∧ + featureSuccessors role pc instruction state.stack = some successors ∧ target ∈ successors /-- Validation establishes closure under every independently recomputed edge, including backedges and every possible switch destination. -/ theorem FeatureFlowValid.edge_closed {layouts : Array RawLayout} - {constants : Nat → Option W32} {raw : RawMethod} {profile : FeatureProfile} - {certificate : FeatureCertificate} {reachable : List Bool} {code : List Op} - (valid : FeatureFlowValid layouts constants raw profile certificate reachable code) + {constants : Nat → Option W32} {linkAt : Nat → Option Link} {raw : RawMethod} + {profile : FeatureProfile} + {certificate : FeatureCertificate} {reachable : List Bool} {code : List Instruction} + (valid : FeatureFlowValid layouts constants linkAt raw profile certificate reachable code) {pc target : Nat} - (edge : FeatureEdge layouts constants raw profile certificate code pc target) : + (edge : FeatureEdge layouts constants linkAt raw profile certificate code pc target) : ∃ incoming, certificate[target]? = some (some incoming) := by obtain ⟨instruction, role, state, emitted, after, successors, rawAt, stateAt, codeAt, transferred, successorsAt, member⟩ := edge - have indexed : (raw.code.zip (foldRoles raw.locals raw.code)).zipIdx[pc]? = - some ((instruction, role), pc) := by - simp [List.getElem?_zipIdx, rawAt] + obtain ⟨instructionAt, roleAt⟩ := List.getElem?_zip_eq_some.mp rawAt + have indexed := featureSites_at instructionAt roleAt have sites := valid.2.2.2.2 have eligible : (FeatureLocalIndex.ofRaw raw).eligible = featureLocalEligible raw := by funext index fact @@ -760,7 +1161,7 @@ theorem FeatureFlowValid.edge_closed {layouts : Array RawLayout} rw [eligible] at sites rw [all3Flow_eq _ _ _ _ _ _ List.drop_zero.symm] at sites have checked := all3_at sites indexed stateAt codeAt - unfold featureTransferAt at transferred + unfold featureTransferAt knownLocals at transferred simp only [checkFeatureSiteWith, transferred, successorsAt, Bool.and_eq_true] at checked have successor := List.all_eq_true.mp checked.2.2 target member @@ -772,20 +1173,21 @@ theorem FeatureFlowValid.edge_closed {layouts : Array RawLayout} /-- The pcs reachable from entry along recomputed feature edges. -/ inductive FeaturePath (layouts : Array RawLayout) (constants : Nat → Option W32) - (raw : RawMethod) (profile : FeatureProfile) (certificate : FeatureCertificate) - (code : List Op) : Nat → Prop where - | entry : FeaturePath layouts constants raw profile certificate code 0 - | next {pc target} : FeaturePath layouts constants raw profile certificate code pc → - FeatureEdge layouts constants raw profile certificate code pc target → - FeaturePath layouts constants raw profile certificate code target + (linkAt : Nat → Option Link) (raw : RawMethod) (profile : FeatureProfile) + (certificate : FeatureCertificate) (code : List Instruction) : Nat → Prop where + | entry : FeaturePath layouts constants linkAt raw profile certificate code 0 + | next {pc target} : FeaturePath layouts constants linkAt raw profile certificate code pc → + FeatureEdge layouts constants linkAt raw profile certificate code pc target → + FeaturePath layouts constants linkAt raw profile certificate code target /-- Arbitrarily many steps through the validated graph remain in the emitted reachable mask; this includes cyclic paths without a bound on iterations. -/ theorem FeatureFlowValid.path_reachable {layouts : Array RawLayout} - {constants : Nat → Option W32} {raw : RawMethod} {profile : FeatureProfile} - {certificate : FeatureCertificate} {reachable : List Bool} {code : List Op} - (valid : FeatureFlowValid layouts constants raw profile certificate reachable code) - {pc : Nat} (path : FeaturePath layouts constants raw profile certificate code pc) : + {constants : Nat → Option W32} {linkAt : Nat → Option Link} {raw : RawMethod} + {profile : FeatureProfile} + {certificate : FeatureCertificate} {reachable : List Bool} {code : List Instruction} + (valid : FeatureFlowValid layouts constants linkAt raw profile certificate reachable code) + {pc : Nat} (path : FeaturePath layouts constants linkAt raw profile certificate code pc) : reachable[pc]? = some true := by have present : ∃ incoming, certificate[pc]? = some (some incoming) := by cases path with diff --git a/CIL/Extraction/FixedDigits.lean b/CIL/Extraction/FixedDigits.lean new file mode 100644 index 000000000..bfac15b0c --- /dev/null +++ b/CIL/Extraction/FixedDigits.lean @@ -0,0 +1,39 @@ +/-! +# Fixed-width digit lists + +The splitter that turns one number into its digits of a fixed width, which the extractor's byte +lists (`fixedBytes`) and the pages of a strict binding image (`fixedDigitsWithin 512`) are +written through. The module imports only the prelude, so a generated module that needs only the +splitter, such as an image module, does not load `Lean`; its specifications are in +[ByteLiterals](ByteLiterals.lean). +-/ + +namespace CIL.Extraction + +/-- The most digits the decoder reads from one word without splitting it further. -/ +def wordDigitCount : Nat := 8 + +/-- The low `count` digits of `width` bits of `word`, most significant first, followed by + `tail`. -/ +def wordDigits (width : Nat) : Nat → Nat → List Nat → List Nat + | 0, _, tail => tail + | count + 1, word, tail => + wordDigits width count (Nat.shiftRight word width) + (Nat.mod word (Nat.shiftLeft 1 width) :: tail) + +/-- The low `count` digits of `width` bits of `code`, most significant first, followed by + `tail`; each of the `depth` levels halves a code longer than a word. -/ +def fixedDigitsWithin (width : Nat) : Nat → Nat → Nat → List Nat → List Nat + | 0, count, code, tail => wordDigits width count code tail + | depth + 1, count, code, tail => + bif Nat.ble count wordDigitCount then wordDigits width count code tail + else + let low := Nat.div count 2 + let lowBits := Nat.mul width low + fixedDigitsWithin width depth (Nat.sub count low) (Nat.shiftRight code lowBits) + (fixedDigitsWithin width depth low (Nat.mod code (Nat.shiftLeft 1 lowBits)) tail) + +/-- The `count` bytes that `code` holds big-endian, as the extractor writes a byte list. -/ +def fixedBytes (count code : Nat) : List Nat := fixedDigitsWithin 8 count count code [] + +end CIL.Extraction diff --git a/CIL/Extraction/FlowIndex.lean b/CIL/Extraction/FlowIndex.lean index 5cec08218..0d48c17bc 100644 --- a/CIL/Extraction/FlowIndex.lean +++ b/CIL/Extraction/FlowIndex.lean @@ -1,20 +1,60 @@ import CIL.Extraction.Lockstep import Init.Omega +import Lean /-! # Certificate successor lookup A balanced index serves branch targets, while adjacent successor states are read directly from the remaining certificate. The lookup theorem preserves the list-based validation rules. +Monotone reads of indexed method data share list suffixes in the kernel and use array access +in compiled code. `program_index% values` writes `FlowIndex.ofList values` as a literal whose +leaves are the list's own cell terms. The kernel checks it equal to `ofList` by comparing those +terms without unfolding them. -/ namespace CIL.Extraction +/-- The suffix at an index, shared by kernel reduction with the lookup at the next index. -/ +private def suffixAt (values : List α) : Nat → List α + | 0 => values + | index + 1 => (suffixAt values index).tail + +/-- The shared suffix contains exactly the elements starting at the given index. -/ +private theorem suffixAt_eq_drop (values : List α) (index : Nat) : + suffixAt values index = values.drop index := by + induction index with + | zero => rfl + | succ index ih => rw [suffixAt, ih, List.tail_drop] +#print axioms suffixAt_eq_drop + +/-- Repeated reads share the preceding suffix in the kernel and use array indexing at runtime. -/ +def indexedRead? (values : Array α) (index : Nat) : Option α := + if index < values.size then (suffixAt values.toList index).head? else none + +/-- The compiled read of an indexed certificate or method body. -/ +private def arrayRead? (values : Array α) (index : Nat) := values[index]? + +/-- Kernel and compiled reads agree both inside and outside the array. -/ +@[csimp] private theorem indexedRead?_eq_arrayRead? : @indexedRead? = @arrayRead? := by + funext α values index + simp only [indexedRead?, arrayRead?, suffixAt_eq_drop, List.head?_drop, Array.getElem?_toList] + split + · rfl + · exact (getElem?_neg values index (by assumption)).symm +#print axioms indexedRead?_eq_arrayRead? + +/-- Indexed reads return the element at the requested position. -/ +@[simp] theorem indexedRead?_eq (values : Array α) (index : Nat) : + indexedRead? values index = values[index]? := by rw [indexedRead?_eq_arrayRead?]; rfl +#print axioms indexedRead?_eq + /-- A certificate index with explicit subtree lengths and no implicit default values. -/ inductive FlowIndex (α : Type) where | empty | leaf (value : α) | branch (leftSize : Nat) (left right : FlowIndex α) + deriving DecidableEq /-- Lookup rejects every index outside the indexed certificate. -/ def FlowIndex.get? : FlowIndex α → Nat → Option α @@ -105,6 +145,49 @@ theorem FlowIndex.get?_ofList (values : List α) (index : Nat) : #print axioms FlowIndex.get?_ofList +open Lean Meta in +/-- The cells of a list term, unfolded until its spine is literal; the cells stay as written. -/ +def literalCells (list : Expr) : MetaM (Array Expr) := do + let mut cells := #[] + let mut rest := list + repeat + -- A literal tail already exposes its constructor, even in a long shared list spine. + let literal := rest.isAppOf ``List.cons || rest.isAppOf ``List.nil + let spine ← if literal then pure rest else whnf rest + match_expr spine with + | List.cons _ head tail => + cells := cells.push head + rest := tail + | List.nil _ => return cells + | _ => throwError "the list does not unfold to literal cells: {spine}" + return cells + +open Lean Meta in +/-- The literal index of `cells`, split at the same middles as `FlowIndex.build`. -/ +partial def literalFlowIndex (type : Expr) (cells : Array Expr) : Expr := + build 0 cells.size +where + /-- The subtree covering `count` consecutive cells starting at `start`. -/ + build (start count : Nat) : Expr := + if count = 0 then mkApp (mkConst ``FlowIndex.empty) type + else if count = 1 then mkApp2 (mkConst ``FlowIndex.leaf) type cells[start]! + else + let middle := count / 2 + mkApp4 (mkConst ``FlowIndex.branch) type (mkNatLit middle) + (build start middle) (build (start + middle) (count - middle)) + +open Lean Meta Elab Term in +/-- `program_index% values` is `FlowIndex.ofList values` written out as a literal tree whose + leaves are the cell terms of `values`, so the kernel never measures or splits the list. -/ +elab "program_index% " values:term : term => do + let list ← instantiateMVars (← elabTerm values none) + synthesizeSyntheticMVarsNoPostponing + let list ← instantiateMVars list + let listType ← whnf (← inferType list) + let some type := listType.app1? ``List + | throwError "program_index%: {list} is not a list" + return literalFlowIndex type (← literalCells list) + /-- Check parallel lists using the next certificate entry for fallthrough edges. -/ def all3Flow (check : (Nat → Option β) → α → β → γ → Bool) (index : FlowIndex β) (pc : Nat) : List α → List β → List γ → Bool diff --git a/CIL/Extraction/Folds.lean b/CIL/Extraction/Folds.lean index 959df05b6..5b64e8bb0 100644 --- a/CIL/Extraction/Folds.lean +++ b/CIL/Extraction/Folds.lean @@ -1,66 +1,25 @@ -import CIL.Extraction.Generics +import CIL.ExceptionType +import CIL.Extraction.ValueDecoding import CIL.Extraction.SpanCalls -import Std.Data.TreeMap.Lemmas +import CIL.Extraction.RuntimeTypeModels /-! # Folded instruction sequences -Some raw instruction sequences have no instruction-by-instruction translation and are -emitted as a unit: `typeof(A) == typeof(B)` becomes its decided Boolean, `throw new E(...)` -becomes one `throw`, and `stloc s; ldloca s` over a span local keeps the span on the stack. +A closed `typeof(A) == typeof(B)` comparison yields its decided Boolean. An allowlisted +exception construction binds the raised type to the `throw`. Its `ldnull` operands push the +same null object reference as in ordinary code. `foldRoles` finds these sequences from the raw body alone and gives each instruction its role, which the feature and scalar checks then enforce. -/ namespace CIL.Extraction -/-- Pointer, byref and generic instance types, which are never equal to a named type. -/ -private def constructed : TypeExpr → Bool - | .pointer _ | .byref _ | .array _ | .apply .. => true - | _ => false - -/-- Conjunction of two three-valued answers: false wins over unknown. -/ -private def both : Option Bool → Option Bool → Option Bool - | some false, _ | _, some false => some false - | some true, some true => some true - | _, _ => none - -/-- Runtime identity of two closed metadata types, or `none` when metadata cannot decide it. - Full names are never forwarded, but one name in two scopes may denote one type. -/ -def sameRuntimeType : TypeExpr → TypeExpr → Option Bool - | .pointer x, .pointer y | .byref x, .byref y | .array x, .array y => sameRuntimeType x y - | .apply f x, .apply g y => both (sameRuntimeType f g) (sameRuntimeType x y) - | a, b => - -- `void` is serialized without its scope, so equal spellings do not establish identity. - if a == b then (if a == .void then none else some true) else - match namedType a, namedType b with - | some (scope, name), some (scope', name') => - if name != name' then some false else if scope == scope' then some true else none - | _, _ => - if (constructed a || (namedType a).isSome) && (constructed b || (namedType b).isSome) then - some false - else none - /-- The type operand of `ldtoken` (208). -/ private def typeHandle : RawInstruction → Option TypeExpr | ⟨_, 208, .type type⟩ => some type | _ => none -/-- The exact call site of a static `System.Type` method with `parameters` parameters. -/ -private def typeMethod (name signature : String) (parameters : Nat) : CallSite := { - declaringType := .atom runtimeScope "System.Type", name, - target := .external (runtimeScope ++ "::" ++ signature), - typeArguments := [], parameterCount := parameters, hasThis := false, returnsValue := true, - returnType := .atom runtimeScope - (if name == "GetTypeFromHandle" then "System.Type" else "System.Boolean"), - parameterTypes := if name == "GetTypeFromHandle" then - [.atom runtimeScope "System.RuntimeTypeHandle"] - else [.atom runtimeScope "System.Type", .atom runtimeScope "System.Type"] } - -private def getTypeFromHandle : CallSite := - typeMethod "GetTypeFromHandle" - "System.Type System.Type::GetTypeFromHandle(System.RuntimeTypeHandle)" 1 - /-- Exactly `call Type.GetTypeFromHandle`. -/ private def typeFromHandle (instruction : RawInstruction) : Bool := instruction.opcode == 40 && instruction.operand == .call getTypeFromHandle @@ -74,10 +33,10 @@ private def typeComparison (instruction : RawInstruction) : Option Bool := | .target _ => some (instruction.opcode == 46 || instruction.opcode == 59) | _ => none else if instruction.opcode != 40 then none - else if instruction.operand == .call (typeMethod "op_Equality" - "System.Boolean System.Type::op_Equality(System.Type,System.Type)" 2) then some true - else if instruction.operand == .call (typeMethod "op_Inequality" - "System.Boolean System.Type::op_Inequality(System.Type,System.Type)" 2) then some false + else if instruction.operand == .call (runtimeTypeComparison true) then + some true + else if instruction.operand == .call (runtimeTypeComparison false) then + some false else none /-- The exact `typeof(A) == typeof(B)` encoding and its decided result. -/ @@ -100,99 +59,107 @@ def typeTestInstruction (instruction : RawInstruction) : Bool := | _ => false | _ => false -/-- The local a `ldloc`, `stloc` or `ldloca` instruction names, in any of its forms. -/ -def localIndex (instruction : RawInstruction) : Option Nat := - match instruction.opcode, instruction.operand with - -- `ldloc.0` to `ldloc.3` and `stloc.0` to `stloc.3`. - | 6, .none | 10, .none => some 0 - | 7, .none | 11, .none => some 1 - | 8, .none | 12, .none => some 2 - | 9, .none | 13, .none => some 3 - -- `ldloc`, `ldloca` and `stloc`, short and long forms. - | 17, .local n | 18, .local n | 19, .local n - | 65036, .local n | 65037, .local n | 65038, .local n => some n - | _, _ => none - -/-- Whether `instruction` names a typed span local. -/ -def spanLocalOp (locals : List TypeExpr) (instruction : RawInstruction) : Bool := - match localIndex instruction with - | some index => (locals[index]?.map spanLocal).getD false - | none => false - -/-- `stloc s; ldloca s` over a span local `s`: the span stays on the stack as the receiver. -/ -def receiverFold (locals : List TypeExpr) (a b : RawInstruction) : Bool := - a.opcode ∈ [10, 11, 12, 13, 19, 65038] && (b.opcode == 18 || b.opcode == 65037) && - localIndex a == localIndex b && spanLocalOp locals a - -/-- A receiver fold ends at a length call or an item call with a single index push. -/ -private def spanReceiverUse (code : List RawInstruction) : Bool := - let member (instruction : RawInstruction) (name : String) := - match instruction with - | ⟨_, 40, .call site⟩ => site.hasThis && site.name == name && spanLocal site.declaringType - | _ => false - match code with - | call :: _ => - member call "get_Length" || match code with - | index :: call :: _ => - index.opcode ∈ [2, 3, 4, 5, 14, 65033, 6, 7, 8, 9, 17, 65036, - 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] && member call "get_Item" - | _ => false +/-- `ldstr` (114) or `ldnull` (20): a message argument of a folded `throw`. -/ +private def messageLoad : RawInstruction → Bool + | ⟨_, 114, .text _⟩ | ⟨_, 20, .none⟩ => true | _ => false -/-- `ldstr` (114) or `ldnull` (20): a message argument of a folded `throw`. -/ -private def messageLoad (instruction : RawInstruction) : Bool := - instruction.opcode == 114 || instruction.opcode == 20 - -/-- Admitted framework exceptions with parameterless/string constructors, plus the three - argument exceptions' two-string overloads. Other constructors remain unsupported. -/ -private def stringExceptionConstructor (name : String) (count : Nat) : Bool := - (count ≤ 1 && name ∈ ["System.Exception", "System.SystemException", - "System.ApplicationException", "System.ArithmeticException", - "System.DivideByZeroException", "System.OverflowException", - "System.InvalidOperationException", "System.NotSupportedException", - "System.NotImplementedException", "System.ArgumentException", - "System.ArgumentNullException", "System.ArgumentOutOfRangeException", - "System.IndexOutOfRangeException", "System.NullReferenceException", - "System.FormatException"]) || - (count == 2 && name ∈ ["System.ArgumentException", "System.ArgumentNullException", - "System.ArgumentOutOfRangeException"]) - -/-- Exact framework signatures whose literal/null arguments cannot change the raised type. -/ -def exceptionConstructor (name : String) (parameters : List TypeExpr) : Bool := - let string := TypeExpr.object runtimeScope "System.String" - let exception := TypeExpr.atom runtimeScope "System.Exception" - (parameters.all (· == string) && stringExceptionConstructor name parameters.length) || - (parameters == [string, exception] && name ∈ ["System.Exception", - "System.InvalidOperationException", "System.ArgumentException", - "System.ArgumentOutOfRangeException"]) || - (name == "System.ArgumentException" && parameters == [string, string, exception]) || - (name == "System.ArgumentOutOfRangeException" && - parameters == [string, .atom runtimeScope "System.Object", string]) - -/-- An exact allowlisted `System.Runtime` constructor, including its complete signature. -/ +/-- The member a constructor's owner spells, the scope compared once; `none` for any other type. -/ +def exceptionOwner? : TypeExpr → Option ExceptionType + | .atom scope name => + if scope == runtimeScope then ExceptionType.all.find? (·.ownerName == name) else none + | _ => none + +/-- A constructor owner that names a member is that member's runtime type. -/ +theorem exceptionOwner?_eq_some {type : TypeExpr} {raised : ExceptionType} + (found : exceptionOwner? type = some raised) : type = .atom runtimeScope raised.ownerName := by + cases type with + | atom scope name => + simp only [exceptionOwner?] at found + split at found + next sameScope => + have named := List.find?_some found + simp only [beq_iff_eq] at named sameScope + rw [sameScope, named] + next => simp at found + | _ => simp [exceptionOwner?] at found + +#print axioms exceptionOwner?_eq_some + +/-- Each member's owner name identifies that member: no member shadows another. -/ +theorem exceptionOwner?_ownerName (raised : ExceptionType) : + exceptionOwner? (.atom runtimeScope raised.ownerName) = some raised := by + have sameOwner : ∀ other : ExceptionType, + (other.ownerName == raised.ownerName) = (other == raised) := by + intro other + by_cases equal : other = raised + · subst equal + simp + · have names : other.ownerName ≠ raised.ownerName := ExceptionType.ownerName_injective.ne equal + simp only [beq_eq_false_iff_ne.mpr names, beq_eq_false_iff_ne.mpr equal] + simp only [exceptionOwner?, beq_self_eq_true, ↓reduceIte, sameOwner] + cases raised <;> decide + +#print axioms exceptionOwner?_ownerName + +/-- The `System.String` parameter of an exception constructor: a message or a parameter name. -/ +def messageParameter : TypeExpr := .object runtimeScope "System.String" + +/-- The `System.Exception` parameter of an exception constructor: the inner exception. -/ +def innerParameter : TypeExpr := .atom runtimeScope "System.Exception" + +/-- The `System.Object` parameter of an exception constructor: the actual argument value. -/ +def actualParameter : TypeExpr := .atom runtimeScope "System.Object" + +/-- The parameter lists of the constructors a folded `throw` may call for each member; the + members only instructions raise admit none. -/ +def admittedParameters : ExceptionType → List (List TypeExpr) + | .baseException | .invalidOperation => + [[], [messageParameter], [messageParameter, innerParameter]] + | .systemException | .applicationException | .arithmetic | .divideByZero | .overflow + | .notSupported | .notImplemented | .indexOutOfRange | .nullReference | .format => + [[], [messageParameter]] + | .argument => + [[], [messageParameter], [messageParameter, messageParameter], + [messageParameter, innerParameter], [messageParameter, messageParameter, innerParameter]] + | .argumentNull => [[], [messageParameter], [messageParameter, messageParameter]] + | .argumentOutOfRange => + [[], [messageParameter], [messageParameter, messageParameter], + [messageParameter, innerParameter], [messageParameter, actualParameter, messageParameter]] + | .invalidCast | .arrayTypeMismatch => [] + +/-- Resolve an allowlisted System.Runtime exception constructor from the closed member + and its declared MemberRef signature. Its printed target must identify the same overload. -/ +def exceptionConstructor? (site : CallSite) : Option ExceptionType := do + guard (site.declared == site.method.signature 0x20 && site.hasThis && !site.returnsValue && + site.returnType == .void && site.method.name == name!".ctor" && site.method.genericArity == 0 && + site.method.methodArguments.isEmpty && site.typeArguments.isEmpty && site.ownerArgumentCount == 0 && + !site.staticVirtualInterfaceSlot && site.parameterCount == site.parameterTypes.length) + let raised ← exceptionOwner? site.declaringType + let .external signature := site.target | none + guard (site.parameterTypes ∈ admittedParameters raised) + let names := site.parameterTypes.map fun type => + match type with | .atom _ name | .object _ name => name | _ => "" + guard (signature == runtimeScope ++ "::System.Void " ++ raised.ownerName ++ "::.ctor(" ++ + ",".intercalate names ++ ")") + return raised + +/-- The operands of the constructor are the permitted literal or null loads at this throw. -/ private def raisedType (count : Nat) (arguments : List RawInstruction) - (instruction : RawInstruction) : Option String := + (instruction : RawInstruction) : Option ExceptionType := if 3 < count then none else match instruction with - | ⟨_, 115, .call site⟩ => match site.declaringType, site.target with - | .atom scope name, .external signature => - if scope == runtimeScope && exceptionConstructor name site.parameterTypes && - site.parameterTypes.length == count && arguments.length == count && - (arguments.zip site.parameterTypes).all (fun (argument, parameter) => - parameter == .object runtimeScope "System.String" || argument.opcode == 20) && - signature == runtimeScope ++ "::System.Void " ++ name ++ "::.ctor(" ++ - ",".intercalate (site.parameterTypes.map fun type => - match type with | .atom _ name | .object _ name => name | _ => "") ++ ")" && - site.name == ".ctor" && site.hasThis && !site.returnsValue && - site.returnType == .void && site.typeArguments.isEmpty && - site.parameterCount == count then some name else none - | _, _ => none + | ⟨_, 115, .call site⟩ => + if site.parameterCount == count && arguments.length == count && + (arguments.zip site.parameterTypes).all (fun (argument, parameter) => + parameter == messageParameter || argument.opcode == 20) then exceptionConstructor? site + else none | _ => none /-- One beyond the largest admitted message arity; reaching it rules out a throw fold. -/ private def throwMessageLimit : Nat := 4 /-- `throw new E(...)` with only string or null arguments: the argument count and raised type. -/ -def throwFold (code : List RawInstruction) : Option (Nat × String) := +def throwFold (code : List RawInstruction) : Option (Nat × ExceptionType) := let count := ((code.take throwMessageLimit).takeWhile messageLoad).length if count == throwMessageLimit then none else match code.drop count with @@ -232,141 +199,162 @@ theorem throwFold_eq_takeWhile (code : List RawInstruction) : /-- An instruction's part in a folded sequence. -/ inductive FoldRole where - /-- The first instruction: emits nothing, and may be a branch target. -/ + /-- The first instruction of the sequence, which may be a branch target. -/ | head - /-- Later instructions: emit nothing, and are entered only from the preceding instruction. -/ + /-- A later operand entered only from the preceding instruction. -/ | interior - /-- A type comparison: pushes the decided Boolean. -/ + /-- An initial type token whose identity is known at a closed comparison. -/ + | typeHead + /-- A type token or conversion inside a closed type comparison. -/ + | typeOperand + /-- The comparison produces this known Boolean. -/ | result (value : Bool) /-- A reference-equality branch over two closed typeof values takes this decided edge. -/ | typeBranch (taken : Bool) /-- The raw `throw`: raises the type bound by the exact folded operands. -/ - | raise (exception : String) + | raise (exception : ExceptionType) /-- An unchanged argument push protected from non-fallthrough entry. -/ | argument - /-- A checked constructor initializes the local whose address starts the fold. -/ - | spanConstructor (index : Nat) (constructor : SpanConstructor) /-- A direct managed call whose prefix requests frame retirement before transfer. -/ | tailCall + /-- A branch enters this candidate after its head, so none of its roles may execute. -/ + | blocked deriving DecidableEq, Repr -/-- Resetting a span local binds its exact type and never exposes its storage address. -/ -private def spanDefaultFold (locals : List TypeExpr) (head : RawInstruction) - (tail : List RawInstruction) : Option (List (Option FoldRole)) := do - if !(head.opcode ∈ [18, 65037]) then none else pure () - let index ← localIndex head - let localType ← locals[index]? - let some ⟨_, 65045, .type initialized⟩ := tail.head? | none - if initialized != localType then none else pure () - let .span width writable ← spanStorage localType | none - return [some .head, some (.spanConstructor index (.empty width writable))] - -/-- A constructor fold admits at most three pure single-value argument pushes. -/ -private def spanConstructorFold (locals : List TypeExpr) (head : RawInstruction) - (tail : List RawInstruction) : Option (List (Option FoldRole)) := do - if !(head.opcode ∈ [18, 65037]) then none else pure () - let index ← localIndex head - let localType ← locals[index]? - let pushes := (tail.take 4).takeWhile fun instruction => instruction.opcode ∈ - [2, 3, 4, 5, 14, 65033, 6, 7, 8, 9, 17, 65036, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] - if pushes.length > 3 then none else pure () - let call ← tail[pushes.length]? - let .call site := call.operand | none - if call.opcode != 40 || site.declaringType != localType then none else pure () - let constructor ← spanLocalConstructor site - if pushes.length != site.parameterCount then none else - some (some .head :: List.replicate pushes.length (some .argument) ++ - [some (.spanConstructor index constructor)]) - -/-- The roles of a fold starting at `a`, if any; type tests take precedence. -/ -private def foldAt (locals receiverLocals : List TypeExpr) - (a : RawInstruction) (tail : List RawInstruction) : +/-- The instruction fixed by a fold role. -/ +def Instruction.ofRole? (role : FoldRole) (previous : Option RawInstruction) + (raw : RawInstruction) : Option Instruction := + match role with + | .head | .interior => + if raw.opcode == 20 then + if raw.operand == .none then some .ldnull else none + else some .nop + | .typeHead | .typeOperand | .result _ | .typeBranch _ => Instruction.ofRaw? previous raw + | .raise exception => some (.throw exception) + | .argument => Instruction.ofRaw? previous raw + | .tailCall => if raw.opcode == 40 || raw.opcode == 41 then Instruction.ofRaw? previous raw + else none + | .blocked => none + +/-- Calls and static field operands need the method's bindings or profile to decode. -/ +def boundSite (raw : RawInstruction) : Bool := + methodTokenCall raw.opcode || raw.opcode == 126 || raw.opcode == 127 || + raw.opcode == 128 || raw.opcode == 208 && (raw.operand matches .field ..) + +/-- These roles determine the entire instruction, including a call inside a fold. -/ +def foldOwnsSite : Option FoldRole → Bool + | some .head | some .interior => true + | _ => false + +/-- Bound operands outside a recognized fold use their site decoder. -/ +def Instruction.withSiteDecoder (roles : Array (Option FoldRole)) + (live : Nat → Bool) (decodeSite : SiteDecoder) : + SiteDecoder := fun pc previous raw => + if !live pc then .unadmitted raw.opcode + else + let role := (indexedRead? roles pc).join + if boundSite raw && !foldOwnsSite role then decodeSite pc previous raw + else match role with + | none | some .blocked => Instruction.ofClosedValue previous raw + | some role => (Instruction.ofRole? role previous raw).getD (.unadmitted raw.opcode) + +/-- The raw head determines which bounded instruction window can form a fold. -/ +private inductive FoldHead where + | exception | tailPrefix | typeHandle + +/-- Only these instructions can start a folded sequence. -/ +private def foldHead? : Nat → Option FoldHead + | 20 | 114 | 115 => some .exception + | 65044 => some .tailPrefix + | 208 => some .typeHandle + | _ => none + +/-- The roles of a fold starting at `a`. -/ +private def foldAt (a : RawInstruction) (tail : List RawInstruction) : Option (List (Option FoldRole)) := - spanDefaultFold locals a tail <|> spanConstructorFold locals a tail <|> - (match a, tail with - | ⟨_, 20, .none⟩, ⟨_, 122, .none⟩ :: _ => - some [some .head, some (.raise "System.NullReferenceException")] - | ⟨_, 65044, .none⟩, ⟨_, 41, .indirectCall _ _ false false 0⟩ :: - ⟨_, 42, .none⟩ :: _ => some [some .head, some .tailCall, some .argument] - | ⟨_, 65044, .none⟩, ⟨_, 40, .call site⟩ :: ⟨_, 42, .none⟩ :: _ => - match site.target with + match foldHead? a.opcode with + | some .exception => + if a.opcode == 20 && a.operand == .none && (tail.head? matches some ⟨_, 122, .none⟩) then + some [some .head, some (.raise .nullReference)] + else (throwFold (a :: tail)).map fun (count, raised) => + some .head :: List.replicate count (some .interior) ++ [some (.raise raised)] + | some .tailPrefix => match a.operand, tail with + | .none, ⟨_, 41, .indirectCall _ _ false false 0⟩ :: ⟨_, 42, .none⟩ :: _ => + some [some .head, some .tailCall, some .argument] + | .none, ⟨_, 40, .call site⟩ :: ⟨_, 42, .none⟩ :: _ => match site.target with | .managed _ => some [some .head, some .tailCall, some .argument] | _ => none - | _, _ => none) <|> - (match tail with - | b :: rest => if receiverFold receiverLocals a b && spanReceiverUse rest then - some [some .head, some .interior] else none - | _ => none) <|> - (match tail with + | _, _ => none + | some .typeHandle => match tail with | b :: c :: d :: e :: _ => (typeTest a b c d e).map fun value => - [some .head, some .interior, some .interior, some .interior, - some (if e.opcode == 40 || e.opcode == 65025 then .result value else .typeBranch value)] - | _ => none) <|> - (throwFold (a :: tail)).map fun (count, name) => - some .head :: List.replicate count (some .interior) ++ [some (.raise name)] - -/-- Assign `pending` roles first; each step consumes one instruction, so the walk is structural. -/ -def foldRolesFrom (locals receiverLocals : List TypeExpr) : - List (Option FoldRole) → List RawInstruction → List (Option FoldRole) - | _, [] => [] - | role :: pending, _ :: tail => role :: foldRolesFrom locals receiverLocals pending tail - | [], a :: tail => match foldAt locals receiverLocals a tail with - | some (role :: pending) => role :: foldRolesFrom locals receiverLocals pending tail - | _ => none :: foldRolesFrom locals receiverLocals [] tail - -/-- Exact counts of decoded local operands, including indexes outside the declared locals. -/ -private def localUseCounts : List RawInstruction → Std.TreeMap Nat Nat - | [] => ∅ - | instruction :: rest => - let counts := localUseCounts rest - match localIndex instruction with - | none => counts - | some index => counts.insert index (counts.getD index 0 + 1) - -/-- Each indexed count equals the original raw-instruction filter, even for malformed operands. -/ -private theorem localUseCounts_get (code : List RawInstruction) (index : Nat) : - (localUseCounts code).getD index 0 = - (code.filter fun instruction => localIndex instruction == some index).length := by + [some .typeHead, some .typeOperand, some .typeOperand, some .typeOperand, + some (if e.opcode == 40 || e.opcode == 65025 then .result value else .typeBranch value)] + | _ => none + | _ => none + +/-- A non-fallthrough edge may enter ordinary code or the head of a fold. -/ +def foldEntry : Option (Option FoldRole) → Bool + | some none | some (some .head) | some (some .typeHead) => true + | _ => false + +/-- Assign all roles of a candidate together, using every encoded target in the body. -/ +private def foldRolesFrom (targeted : Nat → Bool) : + Nat → List (Option FoldRole) → List RawInstruction → List (Option FoldRole) + | _, _, [] => [] + | pc, role :: pending, _ :: tail => role :: foldRolesFrom targeted (pc + 1) pending tail + | pc, [], first :: tail => + match foldAt first tail with + | some (head :: rest) => + if (rest.zipIdx (pc + 1)).all (fun (role, atPc) => !targeted atPc || foldEntry (some role)) + then head :: foldRolesFrom targeted (pc + 1) rest tail + else some .blocked :: foldRolesFrom targeted (pc + 1) + (List.replicate rest.length (some .blocked)) tail + | _ => none :: foldRolesFrom targeted (pc + 1) [] tail + +/-- Roles derived from raw instructions and their branch targets. -/ +def foldRoles (code : List RawInstruction) : List (Option FoldRole) := + let targets := Std.TreeSet.ofList (branchTargets code) compare + foldRolesFrom targets.contains 0 [] code + +/-- Every raw instruction is live and independent of call claims and folded sequences. -/ +def plainBody : List RawInstruction → List Bool → Bool + | [], _ => true + | first :: rest, true :: live => + !boundSite first && (foldHead? first.opcode).isNone && plainBody rest live + | _, _ => false + +/-- At each plain site the mask is live, the role is absent, and no call claim applies. -/ +theorem plainBody_at {closed : RawMethod} {reachable : List Bool} + (plain : plainBody closed.code reachable) + {pc : Nat} {raw : RawInstruction} (found : closed.code[pc]? = some raw) : + reachable[pc]? = some true ∧ boundSite raw = false ∧ + (foldRoles closed.code)[pc]? = some none := by + let targeted := (Std.TreeSet.ofList (branchTargets closed.code) compare).contains + suffices walk : ∀ (code : List RawInstruction) (mask : List Bool) (start index : Nat), + plainBody code mask = true → code[index]? = some raw → + mask[index]? = some true ∧ boundSite raw = false ∧ + (foldRolesFrom targeted start [] code)[index]? = some none by + exact walk closed.code reachable 0 pc plain found + intro code induction code with - | nil => simp [localUseCounts] - | cons instruction rest ih => - cases decoded : localIndex instruction with - | none => simp [localUseCounts, decoded, ih] - | some decodedIndex => - simp only [localUseCounts, decoded, Std.TreeMap.getD_insert, Nat.compare_eq_eq] - by_cases same : decodedIndex = index - · simp [same, decoded, ih] - · simp [same, decoded, ih] - -#print axioms localUseCounts_get - -/-- Roles derived from the raw body alone; extraction evidence cannot add or move a fold. -/ -def foldRoles (locals : List TypeExpr) (code : List RawInstruction) : List (Option FoldRole) := - if locals.isEmpty then foldRolesFrom [] [] [] code else - let counts := localUseCounts code - let singleUse := locals.zipIdx.map fun (type, index) => - if counts.getD index 0 == 2 then type else .void - foldRolesFrom locals singleUse [] code - -/-- Indexed role discovery agrees with filtering the whole body separately for each local. -/ -theorem foldRoles_eq_filter (locals : List TypeExpr) (code : List RawInstruction) : - foldRoles locals code = foldRolesFrom locals - (locals.zipIdx.map fun (type, index) => - if (code.filter fun instruction => localIndex instruction == some index).length == 2 - then type else .void) [] code := by - cases locals <;> simp only [foldRoles, List.isEmpty_nil, List.isEmpty_cons, - Bool.false_eq_true, ↓reduceIte, List.zipIdx_nil, List.map_nil, localUseCounts_get] - -#print axioms foldRoles_eq_filter - -/-- A non-fallthrough edge may enter a fold only at its first instruction. -/ -def foldEntry (role : Option (Option FoldRole)) : Bool := - match role with - | some (some .interior) | some (some (.result _)) | some (some (.raise _)) - | some (some (.typeBranch _)) - | some (some .argument) | some (some (.spanConstructor ..)) => false - | some (some .tailCall) => false - | _ => true + | nil => simp + | cons first rest ih => + intro mask start index checked atIndex + cases mask with + | nil => cases checked + | cons head tail => + cases head with + | false => cases checked + | true => + simp only [plainBody, Bool.and_eq_true, Bool.not_eq_true', + Option.isNone_iff_eq_none] at checked + simp only [foldRolesFrom, foldAt, checked.1.2] + cases index with + | zero => + obtain rfl := Option.some.inj atIndex + exact ⟨rfl, checked.1.1, rfl⟩ + | succ index => exact ih tail (start + 1) index checked.2 atIndex + +#print axioms plainBody_at end CIL.Extraction diff --git a/CIL/Extraction/Generics.lean b/CIL/Extraction/Generics.lean index efadc1b2c..495eb0db5 100644 --- a/CIL/Extraction/Generics.lean +++ b/CIL/Extraction/Generics.lean @@ -1,5 +1,8 @@ -import CIL.Extraction.Parameters +import CIL.RuntimeModel +import CIL.CallSite +import CIL.Extraction.TypeStorage import CIL.Extraction.FlowIndex +import CIL.Extraction.LinkIndex import CIL.Imports import CIL.Extraction.MetadataNames @@ -17,203 +20,9 @@ Opcodes are numeric ECMA-335 encodings; two-byte opcodes `0xFE nn` appear as `0x namespace CIL.Extraction -/-- The exact `System.Runtime.Intrinsics` assembly identity; `runtimeScope` is in `TypeExpr`. -/ -def intrinsicsScope : String := - "System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" - -/-- Byte width of an admitted integer vector, bound to both exact runtime type identities. -/ -def integerVectorWidth : TypeExpr → Option Nat - | .apply (.constructor scope name) (.atom elementScope element) => - if scope != intrinsicsScope || elementScope != runtimeScope || - element ∉ ["System.Byte", "System.Int32", "System.UInt32", "System.Int64", - "System.UInt64"] then none - else if name == "System.Runtime.Intrinsics.Vector128`1" then some 16 - else if name == "System.Runtime.Intrinsics.Vector256`1" then some 32 - else if name == "System.Runtime.Intrinsics.Vector512`1" then some 64 - else none - | _ => none - -/-- The field type of a primitive runtime type, by full name. -/ -private def primitiveField : String → Option FieldType - | "System.SByte" => some .signed8 | "System.Byte" => some .unsigned8 - | "System.Boolean" => some .boolean | "System.Int16" => some .signed16 - | "System.UInt16" => some .unsigned16 | "System.Char" => some .character - | "System.Int32" => some .signed32 | "System.UInt32" => some .unsigned32 - | "System.Int64" => some .signed64 | "System.UInt64" => some .unsigned64 - | "System.IntPtr" => some .nativeInt | "System.UIntPtr" => some .nativeUInt - | _ => none - -/-- The integer types an enum may have as its underlying type. -/ -private def enumField : FieldType → Bool - | .signed8 | .unsigned8 | .signed16 | .unsigned16 | .signed32 | .unsigned32 - | .signed64 | .unsigned64 => true - | _ => false - -/-- Primitive array element identity is derived from the exact runtime metadata scope. -/ -def arrayElementType (type : TypeExpr) (aggregateWidth : Nat := 0) : Option ArrayElement := - match type with - | .aggregate identity _ => some (.aggregate identity aggregateWidth) - | type@(.apply (.constructor _ name) (.atom _ element)) => do - let width ← integerVectorWidth type - return .vector (name ++ "<" ++ element ++ ">") width - | .enum identity (.atom scope name) => do - if scope != runtimeScope then none else pure () - let storage ← match name with - | "System.SByte" => some EnumStorage.signed8 | "System.Byte" => some .unsigned8 - | "System.Int16" => some .signed16 | "System.UInt16" => some .unsigned16 - | "System.Int32" => some .signed32 | "System.UInt32" => some .unsigned32 - | "System.Int64" => some .signed64 | "System.UInt64" => some .unsigned64 - | _ => none - return .enumeration identity storage - | .atom scope name => - if scope != runtimeScope then none else match name with - | "System.SByte" => some .signed8 | "System.Byte" => some .unsigned8 - | "System.Int16" => some .signed16 | "System.UInt16" => some .unsigned16 - | "System.Int32" => some .signed32 | "System.UInt32" => some .unsigned32 - | "System.Int64" => some .signed64 | "System.UInt64" => some .unsigned64 - | "System.IntPtr" => some .nativeInt | "System.UIntPtr" => some .nativeUInt - | "System.Boolean" => some .boolean | "System.Char" => some .character - | _ => none - | _ => none -/-- Primitive/vector widths are intrinsic; aggregate widths come from checked layout uses. -/ -def spanElementWidth (type : TypeExpr) (aggregateWidth : Nat := 0) : Option Nat := - if let .aggregate .. := type then some aggregateWidth else - ((arrayElementType type).map ArrayElement.width).orElse (fun _ => integerVectorWidth type) - -/-- Exact span metadata, including element width and write capability. -/ -def spanStorage (type : TypeExpr) (aggregateWidth : Nat := 0) : Option StorageType := - match type with - | .apply (.constructor scope name) elementType => do - if scope != runtimeScope || - name ∉ ["System.Span`1", "System.ReadOnlySpan`1"] then none else - let width ← spanElementWidth elementType aggregateWidth - return .span width (name == "System.Span`1") - | _ => none - -/-- Whether a local has a supported span metadata shape; layouts are validated separately. -/ -def spanLocal (type : TypeExpr) : Bool := (spanStorage type).isSome - -/-- Callable identities retain scoped aggregate names, independent of layout-table ordering. - Argument storage still validates the original index against the checked layout metadata. -/ -def callableType : TypeExpr → TypeExpr - | .aggregate identity _ => .aggregate identity 0 - | .byref element => .byref (callableType element) - | .apply constructor argument => .apply (callableType constructor) (callableType argument) - | .functionPointer result parameters convention hasThis explicitThis => - .functionPointer (callableType result) (callableType parameters) convention hasThis explicitThis - | .signatureParameter head tail => .signatureParameter (callableType head) (callableType tail) - | type => type - -/-- Function pointers admit checked aggregates, integer vectors and integer/reference types. -/ -def managedParameter : TypeExpr → Bool - | .atom scope name => scope == runtimeScope && (primitiveField name).isSome - | .enum _ (.atom scope name) => scope == runtimeScope && - ((primitiveField name).map enumField).getD false - | .byref element => match element with - | .atom .. | .enum .. | .aggregate .. | .apply .. => managedParameter element - | _ => false - | .aggregate .. => true - | type => (integerVectorWidth type).isSome - -/-- Decode the ordered parameter chain without accepting another metadata constructor. -/ -def managedParameters : TypeExpr → Option (List TypeExpr) - | .void => some [] - | .signatureParameter head tail => do - if !managedParameter head then none else pure () - return callableType head :: (← managedParameters tail) - | _ => none - -/-- Only the exact static managed convention gives callable provenance. -/ -def managedSignature : TypeExpr → Option ManagedSignature - | .functionPointer result parameters 0 false false => do - if result != .void && !managedParameter result then none else pure () - return ⟨callableType result, ← managedParameters parameters⟩ - | _ => none - -/-- Raw callable types must bind every aggregate index before their identity is canonicalized. -/ -def callableLayoutsValid (layouts : List RawLayout) : TypeExpr → Bool - | .aggregate identity index => (layouts[index]?).any (·.identity == identity) - | .byref element => callableLayoutsValid layouts element - | .functionPointer result parameters _ _ _ => - callableLayoutsValid layouts result && callableLayoutsValid layouts parameters - | .signatureParameter head tail => - callableLayoutsValid layouts head && callableLayoutsValid layouts tail - | _ => true - -/-- Decode storage independently from the exact metadata type identity. -/ -def typeStorage (layouts : List RawLayout) : TypeExpr → Option StorageType - | .atom scope name => if scope = runtimeScope then .field <$> primitiveField name else none - | .object _ _ => some .object - | .aggregate identity index => do - let metadata ← layouts[index]? - if metadata.identity = identity then return .field (.aggregate index) else none - | .enum _ underlying => do - let .field kind ← typeStorage layouts underlying | none - if enumField kind then return .field kind else none - | .byref _ => some .reference - -- An array of class references is itself an object on the heap. - | .array (.object _ _) => some .object - | .array (.aggregate identity index) => do - let metadata ← layouts[index]? - if metadata.identity == identity && !metadata.byRefLike then - some (.aggregateArray identity index) else none - | .array element => match managedSignature element with - | some signature => if callableLayoutsValid layouts element then some (.methodTable signature) - else none - | none => StorageType.array <$> arrayElementType element - | type@(.functionPointer ..) => if callableLayoutsValid layouts type then - StorageType.methodPointer <$> managedSignature type else none - | .pointer (.atom scope name) => - if scope == runtimeScope && name ∈ ["System.SByte", "System.Byte", "System.Int16", - "System.UInt16", "System.Char", "System.Int32", "System.UInt32", "System.Int64", - "System.UInt64", "System.IntPtr", "System.UIntPtr"] then some .reference else none - | .pointer (.enum identity underlying) => do - let .field _ ← typeStorage layouts (.enum identity underlying) | none - return .reference - | .pointer (.aggregate identity index) => do - let metadata ← layouts[index]? - if metadata.identity == identity then some .reference else none - | .pointer type => if (integerVectorWidth type).isSome then some .reference else none - | type@(.apply (.constructor scope name) elementType) => do - if let some storage := spanStorage type then - if let .aggregate identity index := elementType then - let metadata ← layouts[index]? - if metadata.identity != identity then none else - return .aggregateSpan index (name == "System.Span`1") - else return storage - let .atom elementScope element := elementType | none - if scope != intrinsicsScope || elementScope != runtimeScope || - element ∉ ["System.UInt64", "System.Int64", "System.UInt32", "System.Int32", - "System.Byte", "System.Double", "System.Single"] then none - else if name = "System.Runtime.Intrinsics.Vector128`1" then some .vector128 - else if name = "System.Runtime.Intrinsics.Vector256`1" then some .vector256 - else if name = "System.Runtime.Intrinsics.Vector512`1" then some .vector512 else none - | _ => none - -/-- A call target: a method of this module by metadata token, or another assembly's - method by its `scope::signature` identity. -/ -inductive MethodId where - | managed (token : Nat) - | external (identity : String) - deriving DecidableEq, Repr - -/-- The metadata of a `call` or `newobj` operand, as declared at the call site. -/ -structure CallSite where - declaringType : TypeExpr := .void - name : String := "" - target : MethodId - typeArguments : List TypeExpr - parameterCount : Nat - hasThis : Bool - returnsValue : Bool - returnType : TypeExpr := .void - parameterTypes : List TypeExpr := [] - /-- The suffix of `typeArguments` belonging to the declaring type, not the method. -/ - ownerArgumentCount : Nat := 0 - deriving DecidableEq, Repr -/-- A decoded instruction operand. A field operand names its owner type and the field's - index in the owner's raw layout. -/ +/-- A decoded instruction operand. A field operand names its owner type, the field's index in + the owner's raw layout, the type it holds and the name of the metadata row its token names. -/ inductive RawOperand where | none | integer (value : Int) @@ -223,12 +32,36 @@ inductive RawOperand where | target (pc : Nat) | targets (pcs : List Nat) | type (value : TypeExpr) - | field (owner : TypeExpr) (index : Nat) (type : TypeExpr) (isStatic : Bool) + | field (owner : TypeExpr) (index : Nat) (type : TypeExpr) (name : MetadataName) + (isStatic : Bool) | call (site : CallSite) | indirectCall (returnType : TypeExpr) (parameters : List TypeExpr) (hasThis explicitThis : Bool) (callingConvention : Nat) deriving DecidableEq, Repr +/-- The typed reference a field operand names: its owner, index, held type and name. `none` for + any other operand; the opcode, not the reference, says whether the field is static. -/ +def FieldRef.ofOperand? : RawOperand → Option FieldRef + | .field owner index type name _ => some { owner, index, type, name } + | _ => none + +/-- A reference is built exactly from a field operand with its owner, index, type and name. -/ +theorem FieldRef.ofOperand?_eq_some {operand : RawOperand} {reference : FieldRef} : + FieldRef.ofOperand? operand = some reference ↔ ∃ isStatic, + operand = .field reference.owner reference.index reference.type reference.name isStatic := by + cases operand + case field owner index type name isStatic => + cases reference + simp only [FieldRef.ofOperand?, Option.some.injEq, FieldRef.mk.injEq, RawOperand.field.injEq] + constructor + · rintro ⟨rfl, rfl, rfl, rfl⟩ + exact ⟨isStatic, rfl, rfl, rfl, rfl, rfl⟩ + · rintro ⟨_, rfl, rfl, rfl, rfl, rfl⟩ + exact ⟨rfl, rfl, rfl, rfl⟩ + all_goals simp [FieldRef.ofOperand?] + +#print axioms FieldRef.ofOperand?_eq_some + /-- One CIL instruction: its byte offset, opcode and operand. Branch operands are already instruction indices. -/ structure RawInstruction where @@ -237,6 +70,10 @@ structure RawInstruction where operand : RawOperand deriving DecidableEq, Repr +/-- Direct calls, virtual calls and constructions take a method token. -/ +def methodTokenCall (opcode : Nat) : Bool := + opcode == 40 || opcode == 111 || opcode == 115 + /-- A generic parameter's attribute flags and constraint types. -/ structure GenericConstraint where flags : Nat @@ -247,6 +84,12 @@ structure GenericConstraint where interfaces : List TypeExpr := [] deriving DecidableEq, Repr +/-- Each static access carries its storage descriptor. RVA data also has a declared class size. -/ +structure StaticFieldSite where + descriptor : StaticDescriptor + rvaSize : Option Nat := none + deriving DecidableEq, Repr + /-- A method definition as read from metadata: signature, locals and body. -/ structure RawMethod where token : Nat @@ -274,17 +117,42 @@ structure RawMethod where objectFieldCount : Nat := 0 /-- The admitted base classes of a class declaring type, nearest first. -/ objectBases : List TypeExpr := [] + /-- Each reachable static access has a descriptor at its instruction index. -/ + staticFields : List (Nat × StaticFieldSite) := [] deriving DecidableEq, Repr +/-- The method frame retains the descriptor at each static instruction index in source order. -/ +def RawMethod.staticDescriptors (method : RawMethod) : List (Nat × StaticDescriptor) := + method.staticFields.map fun (pc, site) => (pc, site.descriptor) + /-- Close metadata storage after substitution without changing callable owner expressions. -/ def substituteStorageType (layouts : List RawLayout) (arguments : List TypeExpr) (type : TypeExpr) : Option TypeExpr := closeLayoutType layouts <$> substituteType arguments type +/-- The closed type of an operand that names a runtime span also names a runtime span. -/ +theorem substituteStorageType_span {layouts : List RawLayout} {arguments : List TypeExpr} + {original closed : TypeExpr} (span : original.isRuntimeSpan = true) + (storage : substituteStorageType layouts arguments original = some closed) : + closed.isRuntimeSpan = true := by + cases original <;> try cases span + case apply owner element => + change owner.isSpanHead = true at span + cases owner <;> try cases span + case constructor scope name => + simp only [substituteStorageType, substituteType, Option.pure_def, Option.bind_some, + Option.map_eq_map, Option.bind_eq_bind] at storage + cases found : substituteType arguments element <;> + simp [found, closeLayoutType_span layouts scope name _ span] at storage + subst closed + exact span +#print axioms substituteStorageType_span + /-- Whether a type mentions a generic parameter of the method being instantiated. -/ private def mentionsParameter : TypeExpr → Bool | .parameter _ => true - | .byref element | .pointer element | .array element | .enum _ element => + | .byref element | .pointer element | .array element | .enum _ element + | .modified _ _ _ element => mentionsParameter element | .functionPointer result parameters .. | .signatureParameter result parameters => mentionsParameter result || mentionsParameter parameters @@ -321,7 +189,7 @@ private def splitOwner : List Char → List Char → Option (List Char × List C /-- The assembly scope of an owner's generic type definition. -/ private def ownerScope : TypeExpr → Option String - | .constructor scope _ => some scope + | .constructor scope _ | .objectConstructor scope _ => some scope | .apply constructor _ => ownerScope constructor | _ => none @@ -348,19 +216,22 @@ def substituteTarget (methodArity : Nat) (openOwner closedOwner : TypeExpr) : def substituteOperand (arguments : List TypeExpr) (layouts : List RawLayout := []) (methodArity : Nat := 0) : RawOperand → Option RawOperand | .type type => RawOperand.type <$> substituteStorageType layouts arguments type - | .field owner index type isStatic => do - return .field (← substituteStorageType layouts arguments owner) index (← substituteStorageType layouts arguments type) - isStatic + | .field owner index type name isStatic => do + return .field (← substituteStorageType layouts arguments owner) index + (← substituteStorageType layouts arguments type) name isStatic | .call site => do let declaringType ← substituteType arguments site.declaringType return .call { site with - declaringType, + method := { site.method with + owner := declaringType, + returnType := ← substituteStorageType layouts arguments site.returnType, + parameters := ← site.parameterTypes.mapM (substituteStorageType layouts arguments), + methodArguments := + ← site.method.methodArguments.mapM (substituteStorageType layouts arguments) }, target := ← if mentionsParameter site.declaringType then substituteTarget methodArity site.declaringType declaringType site.target else pure site.target, - typeArguments := ← site.typeArguments.mapM (substituteStorageType layouts arguments), - returnType := ← substituteStorageType layouts arguments site.returnType, - parameterTypes := ← site.parameterTypes.mapM (substituteStorageType layouts arguments) } + typeArguments := ← site.typeArguments.mapM (substituteStorageType layouts arguments) } | .indirectCall returnType parameters hasThis explicitThis convention => do return .indirectCall (← substituteStorageType layouts arguments returnType) (← parameters.mapM (substituteStorageType layouts arguments)) hasThis explicitThis convention @@ -373,11 +244,12 @@ private def admittedTypeArgument (layouts : List RawLayout) (type : TypeExpr) : | some _ => true /-- A generic type application split into its constructor and ordered arguments. -/ -private def appliedArguments : TypeExpr → TypeExpr × List TypeExpr - | .apply constructor argument => - let (head, arguments) := appliedArguments constructor - (head, arguments ++ [argument]) - | type => (type, []) +def appliedArguments (type : TypeExpr) : TypeExpr × List TypeExpr := collect type [] +where + /-- Peeled arguments form the suffix after the remaining constructor's own arguments. -/ + collect : TypeExpr → List TypeExpr → TypeExpr × List TypeExpr + | .apply constructor argument, suffix => collect constructor (argument :: suffix) + | head, suffix => (head, suffix) /-- An interface constraint naming generic parameters holds when its closure at `arguments` is among the direct interfaces of the constrained argument's layout. -/ @@ -393,10 +265,11 @@ def openConstraintValid (layouts : List RawLayout) (arguments : List TypeExpr) | none => false | _, _ => false -/-- Instantiation preserves instruction encodings, offsets and branch targets exactly. -/ +/-- Instantiation preserves instruction encodings, offsets and branch targets exactly. + Explicit receiver signatures have no supported instantiation. -/ def instantiateMethod (layouts : List RawLayout) (original : RawMethod) (arguments : List TypeExpr) : Option RawMethod := do - if original.ownerArgumentCount != 0 || + if original.explicitThis || original.ownerArgumentCount != 0 || arguments.length != original.genericParameters.length + original.ownerParameters.length || !arguments.all (admittedTypeArgument layouts) || !((original.genericParameters ++ original.ownerParameters).zip arguments).all @@ -417,6 +290,36 @@ def instantiateMethod (layouts : List RawLayout) (original : RawMethod) ownerArgumentCount := original.ownerParameters.length, callingConvention := original.callingConvention &&& 239 } +/-- Instantiation keeps the metadata token, so a closed body names the definition it closes. -/ +theorem instantiateMethod_token {layouts : List RawLayout} {original closed : RawMethod} + {arguments : List TypeExpr} + (instantiated : instantiateMethod layouts original arguments = some closed) : + closed.token = original.token := by + unfold instantiateMethod at instantiated + split at instantiated + · cases instantiated + · simp only [Option.bind_eq_bind, Option.bind_eq_some_iff, Option.pure_def, + Option.some.injEq] at instantiated + obtain ⟨_, _, _, _, _, _, _, _, rfl⟩ := instantiated + rfl + +#print axioms instantiateMethod_token + +/-- A closed method retains its definition's local initialization flag. -/ +theorem instantiateMethod_initLocals {layouts : List RawLayout} {original closed : RawMethod} + {arguments : List TypeExpr} + (instantiated : instantiateMethod layouts original arguments = some closed) : + closed.initLocals = original.initLocals := by + unfold instantiateMethod at instantiated + split at instantiated + · cases instantiated + · simp only [Option.bind_eq_bind, Option.bind_eq_some_iff, Option.pure_def, + Option.some.injEq] at instantiated + obtain ⟨_, _, _, _, _, _, _, _, rfl⟩ := instantiated + rfl + +#print axioms instantiateMethod_initLocals + /-- Closed argument metadata is derived from this checked body, including short opcodes. -/ def argumentOperation (instruction : RawInstruction) : Option ArgumentOp := match instruction.opcode, instruction.operand with @@ -431,6 +334,37 @@ def argumentOperation (instruction : RawInstruction) : Option ArgumentOp := | opcode, _ => if opcode ∈ [2, 3, 4, 5, 14, 15, 16, 65033, 65034, 65035] then none else some .other +/-- Whether `instruction` names `pc` as a branch destination or switch arm. -/ +def branchesTo (pc : Nat) (instruction : RawInstruction) : Bool := + match instruction.operand with + | .target target => target == pc + | .targets targets => targets.contains pc + | _ => false + +/-- All decoded branch destinations, including switch arms and unreachable instructions. -/ +def branchTargets (raw : List RawInstruction) : List Nat := + raw.flatMap fun instruction => match instruction.operand with + | .target target => [target] + | .targets targets => targets + | _ => [] + +/-- Collecting branch destinations once answers, for every pc, whether some instruction + branches to it. -/ +theorem branchTargets_contains (raw : List RawInstruction) (pc : Nat) : + (branchTargets raw).contains pc = raw.any (branchesTo pc) := by + induction raw with + | nil => rfl + | cons instruction rest ih => + cases instruction with + | mk offset opcode operand => + change _ = (branchesTo pc ⟨offset, opcode, operand⟩ || rest.any (branchesTo pc)) + simp only [branchTargets, List.flatMap_cons, List.contains_append] + change (_ || (branchTargets rest).contains pc) = _ + rw [ih] + cases operand <;> simp [branchesTo, BEq.beq, eq_comm] + +#print axioms branchTargets_contains + /-- The unique raw managed indirect signature and native locals directly consumed as targets. -/ structure NativeDispatchContext where signature : Option ManagedSignature := none @@ -462,27 +396,51 @@ def nativeDispatchContext (raw : RawMethod) : NativeDispatchContext := Id.run do if instruction.opcode != 65044 then previous := some instruction return ⟨signature, locals⟩ -/-- A native pointer is an input table view; only raw target-feeding integer locals are callable. -/ +/-- A native pointer is an input table view; only raw target-feeding integer locals, those + `pointer` holds, are callable. -/ def nativeDispatchStorage (context : NativeDispatchContext) (type : TypeExpr) - (localIndex : Option Nat := none) : Option StorageType := do + (localIndex : Option Nat := none) (pointer : Nat → Bool := context.pointerLocals.contains) : + Option StorageType := do let signature ← context.signature if let .pointer element := type then if nativeDispatchInteger element then return .methodTable signature - if nativeDispatchInteger type && localIndex.any context.pointerLocals.contains then + if nativeDispatchInteger type && localIndex.any pointer then return .methodPointer signature none -/-- Closed argument and local storage, including typed span views, and argument accesses. -/ -def closedArguments (layouts : List RawLayout) (closed : RawMethod) : Option RawArguments := do - let context := nativeDispatchContext closed +/-- Closed argument and local storage, including typed span views, and argument accesses, where + `pointer` holds the locals a `calli` consumes. -/ +def closedArgumentsWith (layouts : List RawLayout) (closed : RawMethod) + (context : NativeDispatchContext) (pointer : Nat → Bool) : Option RawArguments := do let parameters ← closed.parameters.mapM fun type => nativeDispatchStorage context type <|> typeStorage layouts type let locals ← closed.locals.zipIdx.mapM fun (type, index) => - nativeDispatchStorage context type (some index) <|> typeStorage layouts type + nativeDispatchStorage context type (some index) pointer <|> typeStorage layouts type let code ← closed.code.mapM argumentOperation let receiver := if closed.objectReceiver then StorageType.object else .reference return ⟨(if closed.hasThis then [receiver] else []) ++ parameters, locals, code⟩ +/-- Closed argument and local storage, including typed span views, and argument accesses, with + the locals a `calli` consumes read from a `Std.TreeSet` built once per body. -/ +def closedArguments (layouts : List RawLayout) (closed : RawMethod) : Option RawArguments := + let context := nativeDispatchContext closed + closedArgumentsWith layouts closed context + (Std.TreeSet.ofList context.pointerLocals compare).contains + +/-- Reading pointer locals from a set built once gives exactly the storage that scanning the + pointer local list at each local gives. -/ +theorem closedArguments_eq_list (layouts : List RawLayout) (closed : RawMethod) : + closedArguments layouts closed = closedArgumentsWith layouts closed + (nativeDispatchContext closed) (nativeDispatchContext closed).pointerLocals.contains := by + have pointer : (Std.TreeSet.ofList (nativeDispatchContext closed).pointerLocals + compare).contains = (nativeDispatchContext closed).pointerLocals.contains := by + funext index + rw [Std.TreeSet.contains_ofList] + dsimp only [closedArguments] + rw [pointer] + +#print axioms closedArguments_eq_list + /-- `closed` is `original` instantiated at `arguments`, and `parameterInput` is the argument metadata derived from it. -/ def SpecializationValid (layouts : List RawLayout) (original : RawMethod) @@ -514,25 +472,83 @@ structure SpecializedMethod where deriving DecidableEq, Repr /-- The callee index and argument count of an emitted call, if its kind matches the raw - opcode: `call` (40) for `.call`, `callvirt` (111) for a receiver-checked call, `newobj` - (115) for a construction. -/ -private def managedCallTarget (opcode : Nat) : Op → Option (Nat × Nat) - | .call target count | .tailCall target count => - if opcode = 40 then some (target, count) else none - | .callInstance target count => if opcode = 111 then some (target, count) else none - | .newAggregate target count _ | .newVector256 target count | .newObject target count .. => - if opcode = 115 then some (target, count) else none + opcode: `call` (40) for `.call`, whose callee is the `.direct` link of its pc, and `callvirt` + (111) for a receiver-checked call. -/ +private def managedCallTarget (opcode : Nat) (link : Option Link) : + Instruction → Option (Nat × Nat) + | .call method _ => match link with + | some (.direct target) => if opcode = 40 then some (target, method.argumentCount) else none + | _ => none + | .callvirt method => match link with + | some (.direct target) => if opcode = 111 then some (target, method.argumentCount) else none + | _ => none | _ => none -/-- A class construction allocates an instance of the call site's declaring class, with that - class's bases and one slot per instance field, whose constructor takes that instance as its - object receiver. -/ -def constructedOwnerMatches (site : CallSite) (callee : RawMethod) : Op → Bool - | .newObject _ _ owner bases size => owner == site.declaringType && callee.objectReceiver && - bases == callee.objectBases && size == objectFieldOffset callee.objectFieldCount && - match owner with | .object .. => true | _ => false +/-- A `call` names the method its raw operand names, unless a `constrained.` prefix lowered the + operand to the resolved implementation; the constrained-call check ties that operand. -/ +def callOperandMatches (site : CallSite) : Instruction → Bool + | .call method prefixes => prefixes.constrained.isSome || method == site.method + | .callvirt method => method == site.method | _ => true +/-- Split a scoped declaring type from its ordered generic arguments. -/ +def managedOwner : TypeExpr → Option (String × List TypeExpr) + | .atom scope name | .constructor scope name | .objectConstructor scope name + | .object scope name => some (scope ++ "::" ++ name, []) + | .aggregate identity _ => some (identity, []) + | .apply owner argument => do + let (identity, arguments) ← managedOwner owner + return (identity, arguments ++ [argument]) + | _ => none + +/-- Owner arguments occupy the suffix after method arguments, with independently checked arity. -/ +def managedOwnerMatches (site : CallSite) (owner : String) (ownerCount : Nat) : Bool := + match managedOwner site.declaringType with + | some (identity, arguments) => identity == owner && ownerCount == site.ownerArgumentCount && + arguments.length == ownerCount && ownerCount ≤ site.typeArguments.length && + site.typeArguments.drop (site.typeArguments.length - ownerCount) == arguments + | none => false + +/-- `callee` is the extracted method a managed site with metadata token `token` names: the same + owner with its type arguments, token, method type arguments and declared signature. -/ +def calleeMatchesSite (site : CallSite) (token : Nat) (callee : SpecializedMethod) : Bool := + managedOwnerMatches site callee.closed.owner callee.closed.ownerArgumentCount && + callee.closed.token == token && callee.arguments == site.typeArguments && + callee.closed.parameters.length == site.parameterCount && + callee.closed.hasThis == site.hasThis && + (callee.closed.returnType != .void) == site.returnsValue && + callee.closed.returnType == site.returnType && + callee.closed.parameters == site.parameterTypes + +/-- What a live `newobj` of `constructor` may be linked to. Its raw instruction is `newobj` (115) + of a managed site naming `constructor`, and the linked method is the one the site names. A + value-type constructor, whose owner is no class and whose receiver is no object, is linked + `.aggregate`; a class constructor taking its instance as object receiver is linked `.object` + with the class's bases and one slot per instance field. A dead site passes. -/ +def validateConstructionWith (methods : Nat → Option SpecializedMethod) (raw : RawInstruction) + (live : Bool) (link : Option Link) (constructor : MethodRef) : Bool := + if !live then true else + match link with + | none => false + | some link => + let isClass := match constructor.owner with | .object .. => true | _ => false + raw.opcode == 115 && match raw.operand with + | .call site => site.method == constructor && match site.target with + | .managed token => match link with + | .aggregate index _ => match methods index with + | some callee => calleeMatchesSite site token callee && !isClass && + !callee.closed.objectReceiver + | none => false + | .object index bases size => match methods index with + | some callee => calleeMatchesSite site token callee && isClass && + callee.closed.objectReceiver && bases == callee.closed.objectBases && + size == objectFieldOffset callee.closed.objectFieldCount + | none => false + | _ => false + | .external _ => false + | _ => false + + /-- The `Virtual` flag of a method's metadata attributes (ECMA-335 II.23.1.10). -/ def virtualMethodAttribute : Nat := 0x40 @@ -543,8 +559,8 @@ def RawMethod.isVirtual (method : RawMethod) : Bool := /-- A receiver-checked call enters a non-virtual method of an admitted class, whose receiver is an object reference; no other managed call checks its receiver. A virtual callee would need dispatch, which one fixed target does not perform. -/ -def checkedReceiverMatches (callee : RawMethod) : Op → Bool - | .callInstance .. => callee.objectReceiver && !callee.isVirtual +def checkedReceiverMatches (callee : RawMethod) : Instruction → Bool + | .callvirt .. => callee.objectReceiver && !callee.isVirtual | _ => true /-- Legacy slots accept only primitive signatures; new families require exact typed metadata. -/ @@ -585,24 +601,6 @@ def managedTargetMatches (methods : Nat → Option SpecializedMethod) callee.closed.parameters.map callableType == signature.parameters && managedSignature signature.typeExpr == some signature -/-- Split a scoped declaring type from its ordered generic arguments. -/ -def managedOwner : TypeExpr → Option (String × List TypeExpr) - | .atom scope name | .constructor scope name | .object scope name => - some (scope ++ "::" ++ name, []) - | .aggregate identity _ => some (identity, []) - | .apply owner argument => do - let (identity, arguments) ← managedOwner owner - return (identity, arguments ++ [argument]) - | _ => none - -/-- Owner arguments occupy the suffix after method arguments, with independently checked arity. -/ -def managedOwnerMatches (site : CallSite) (owner : String) (ownerCount : Nat) : Bool := - match managedOwner site.declaringType with - | some (identity, arguments) => identity == owner && ownerCount == site.ownerArgumentCount && - arguments.length == ownerCount && ownerCount ≤ site.typeArguments.length && - site.typeArguments.drop (site.typeArguments.length - ownerCount) == arguments - | none => false - /-- Exact function address evidence; integer constants cannot acquire callable provenance. -/ def managedAddressMatches (methods : Nat → Option SpecializedMethod) (raw : RawInstruction) (target : ManagedTarget) (signature : ManagedSignature) : Bool := @@ -636,20 +634,24 @@ def managedIndirectMatches (methods : Nat → Option SpecializedMethod) | _ => false /-- Every live managed call must select the independently instantiated definition -with the exact substituted type arguments and signature; an external one, a matching import slot. -/ +with the exact substituted type arguments and signature; an external one, a matching import slot. +`link` is the link of the call's pc, which names the body a `call` enters. -/ def validateManagedCallWith (methodCount : Nat) (methods : Nat → Option SpecializedMethod) (imports : Nat → Option ImportSlot) - (raw : RawInstruction) (live : Bool) (emitted : Op) : Bool := + (raw : RawInstruction) (live : Bool) (link : Option Link) (emitted : Instruction) : Bool := if !live then true else - match emitted with - | .managed (.address target signature) => managedAddressMatches methods raw target signature - | .managed (.call signature catalog _) => managedIndirectMatches methods raw signature catalog + match emitted, link with + | .ldftn identity signature, some (.address index) => + managedAddressMatches methods raw ⟨identity, index⟩ signature + | .calli signature _, some (.indirect catalog) => + managedIndirectMatches methods raw signature catalog + | .ldftn .., _ | .calli .., _ => false -- Each listed body is a virtual instance method of a class with the slot's signature; which -- body each class selects is checked against the dispatch catalog. - | .callVirtual targets count => match raw.operand with - | .call site => raw.opcode == 111 && site.hasThis && !targets.isEmpty && - (match site.target with | .managed _ => true | .external _ => false) && - count == site.parameterCount + 1 && + | .callvirt method, some (.virtual targets) => match raw.operand with + | .call site => raw.opcode == 111 && method == site.method && site.hasThis && + !targets.isEmpty && (match site.target with | .managed _ => true | .external _ => false) && + method.argumentCount == site.parameterCount + 1 && targets.all fun (_, index) => match methods index with | some callee => callee.arguments.isEmpty && callee.closed.objectReceiver && callee.closed.hasThis && callee.closed.isVirtual && @@ -657,36 +659,118 @@ def validateManagedCallWith (methodCount : Nat) callee.closed.returnType == site.returnType | none => false | _ => false - | _ => + -- A live construction is checked with its link by `validateConstructionWith`; without the link + -- it passes nothing. + | .newobj _, _ => false + | _, _ => match raw.operand with | .call site => match site.target with - | .managed token => match managedCallTarget raw.opcode emitted with + | .managed token => match managedCallTarget raw.opcode link emitted with | none => false | some (target, count) => match methods target with | none => false | some callee => - managedOwnerMatches site callee.closed.owner callee.closed.ownerArgumentCount && - callee.closed.token == token && callee.arguments == site.typeArguments && - callee.closed.parameters.length == site.parameterCount && - callee.closed.hasThis == site.hasThis && - (callee.closed.returnType != .void) == site.returnsValue && - callee.closed.returnType == site.returnType && - callee.closed.parameters == site.parameterTypes && - count == site.parameterCount + (if raw.opcode != 115 && site.hasThis then 1 else 0) && - constructedOwnerMatches site callee.closed emitted && - checkedReceiverMatches callee.closed emitted - | .external identity => match emitted with - | .call target count => - validateImportCallWith methodCount imports raw.opcode site identity target count - | .callInstance .. | .tailCall .. | .newAggregate .. | .newVector256 .. | .newObject .. => - false - | _ => true + calleeMatchesSite site token callee && + count == site.parameterCount + (if site.hasThis then 1 else 0) && + checkedReceiverMatches callee.closed emitted && callOperandMatches site emitted + | .external identity => match emitted, link with + | .call method prefixes, some (.direct target) => + !prefixes.tail && prefixes.constrained.isNone && method == site.method && + validateImportCallWith methodCount imports raw.opcode site identity target + method.argumentCount + | .call .., _ | .callvirt .., _ => false + | _, _ => true | .indirectCall .. => false | _ => match emitted with - | .call .. | .callInstance .. | .tailCall .. | .newAggregate .. | .newVector256 .. - | .newObject .. => false + | .call .. | .callvirt .. => false | _ => true +/-- Link kinds for receiver calls, managed addresses and indirect calls. -/ +private def operandLinkFits (emitted : Instruction) (link : Option Link) : Bool := + match emitted, link with + | .callvirt _, some (.direct _) | .callvirt _, some (.virtual _) + | .ldftn .., some (.address _) | .calli .., some (.indirect _) => true + | .callvirt _, _ | .ldftn .., _ | .calli .., _ => false + | _, _ => link.isNone + +/-- The link kind permitted at an instruction other than a construction. -/ +def _root_.CIL.Instruction.linkFits (modelled : CallOpcode × MethodRef → Bool) + (emitted : Instruction) (link : Option Link) : Bool := + match emitted with + | .call method _ => match link with + | some (.direct _) => !modelled (.call, method) + | none => modelled (.call, method) + | some _ => false + | _ => operandLinkFits emitted link + +/-- A live call that states no link keeps the call its raw site makes: a `call` of an external + method, naming the site's own method, with no prefix. A dead one passes. -/ +def validateModelCall (raw : RawInstruction) (live : Bool) (emitted : Instruction) : Bool := + !live || match raw.opcode, raw.operand, emitted with + | 40, .call site, .call method prefixes => + (site.target matches .external _) && method == site.method && !prefixes.tail && + prefixes.constrained.isNone + | _, _, _ => false + +/-- A raw call or construction claims the runtime model registered for that opcode. -/ +def modelledCallSite (modelled : CallOpcode × MethodRef → Bool) (raw : RawInstruction) : Bool := + match raw.opcode, raw.operand with + | 40, .call site => modelled (.call, site.method) + | 115, .call site => modelled (.newobj, site.method) + | _, _ => false + +/-- A modelled site keeps its opcode and external method without a link. Live linked sites must + agree with the managed method or import selected by their link. -/ +def validateLinkedCallWith (modelled : CallOpcode × MethodRef → Bool) (methodCount : Nat) + (methods : Nat → Option SpecializedMethod) (imports : Nat → Option ImportSlot) + (raw : RawInstruction) (live : Bool) (link : Option Link) (emitted : Instruction) : Bool := + match emitted.constructor? with + | some constructor => + if modelled (.newobj, constructor) then + link.isNone && (!live || (raw.opcode == 115 && match raw.operand with + | .call site => (site.target matches .external _) && site.method == constructor + | _ => false)) + else validateConstructionWith methods raw live link constructor + | none => + -- The arms decide `Instruction.linkFits` in the same match, since a second match on the + -- instruction costs the kernel about 700 heartbeats at every pc. + match emitted, link with + | .call method _, none => modelled (.call, method) && validateModelCall raw live emitted + | .call method _, some (.direct _) => !modelled (.call, method) && + ((!live || !modelledCallSite modelled raw) && + validateManagedCallWith methodCount methods imports raw live link emitted) + | .call .., some _ => false + | _, _ => operandLinkFits emitted link && + ((!live || !modelledCallSite modelled raw) && + validateManagedCallWith methodCount methods imports raw live link emitted) + +/-- A pc other than a construction that passes the linked call rule fits its link. -/ +theorem validateLinkedCallWith_linkFits {modelled : CallOpcode × MethodRef → Bool} + {methodCount : Nat} + {methods : Nat → Option SpecializedMethod} {imports : Nat → Option ImportSlot} + {raw : RawInstruction} {live : Bool} {link : Option Link} {emitted : Instruction} + (unnamed : emitted.constructor? = none) + (checked : validateLinkedCallWith modelled methodCount methods imports raw live link emitted = + true) : + emitted.linkFits modelled link = true := by + simp only [validateLinkedCallWith, unnamed] at checked + split at checked + · simp only [Bool.and_eq_true] at checked + simp [Instruction.linkFits, checked.1] + · simp only [Bool.and_eq_true] at checked + simp [Instruction.linkFits, checked.1] + · simp at checked + · rename_i noModel _ noOther + simp only [Bool.and_eq_true] at checked + unfold Instruction.linkFits + split + · cases link with + | none => exact (noModel _ _ rfl rfl).elim + | some _ => exact (noOther _ _ _ rfl rfl).elim + · exact checked.1 + +#print axioms validateLinkedCallWith_linkFits + /-- The external-call rule stated against the original lists, including bounds and offset. -/ def validateImportCall (methods : List SpecializedMethod) (imports : List ImportSlot) (opcode : Nat) (site : CallSite) (identity : String) (target count : Nat) : Bool := @@ -694,83 +778,193 @@ def validateImportCall (methods : List SpecializedMethod) (imports : List Import /-- The call rule stated against the original lists for use in semantic consequences. -/ def validateManagedCall (methods : List SpecializedMethod) (imports : List ImportSlot) - (raw : RawInstruction) (live : Bool) (emitted : Op) : Bool := - validateManagedCallWith methods.length (methods[·]?) (imports[·]?) raw live emitted - -/-- Every call emitted at a live call site of `raw` binds to the method table or an import - slot, and no other instruction emits a call. An external site may instead emit a - non-call translation, which other validators check. -/ + (raw : RawInstruction) (live : Bool) (link : Option Link) (emitted : Instruction) : Bool := + validateManagedCallWith methods.length (methods[·]?) (imports[·]?) raw live link emitted + +/-- The linked call rule stated against the original lists. -/ +def validateLinkedCall (modelled : CallOpcode × MethodRef → Bool) (methods : List SpecializedMethod) + (imports : List ImportSlot) (raw : RawInstruction) (live : Bool) (link : Option Link) + (emitted : Instruction) : Bool := + validateLinkedCallWith modelled methods.length (methods[·]?) (imports[·]?) raw live link + emitted + +/-- Each live managed transfer binds its operand to the indicated method, import or dispatch + table. Modelled calls and constructors keep their external operand and have no link. -/ def ManagedCallsValid (methods : List SpecializedMethod) (imports : List ImportSlot) - (raw : RawMethod) (reachable : List Bool) (emitted : Method) : Prop := + (modelled : CallOpcode × MethodRef → Bool) (raw : RawMethod) (reachable : List Bool) + (emitted : Method) : + Prop := + keysAscend 0 emitted.links = true ∧ + emitted.links.all (fun entry => decide (entry.1 < emitted.code.length)) = true ∧ raw.code.length = reachable.length ∧ raw.code.length = emitted.code.length ∧ - all3 (validateManagedCallWith methods.length (FlowIndex.ofList methods).get? - (FlowIndex.ofList imports).get?) raw.code reachable emitted.code = true - -/-- Indexed call lookup preserves every metadata check and rejects the same invalid targets. -/ + all3 (fun instruction (live, link) op => validateLinkedCallWith modelled methods.length + (FlowIndex.ofList methods).get? (FlowIndex.ofList imports).get? instruction live link op) + raw.code (reachable.zip (linkWalk emitted.links emitted.links 0 + (List.range emitted.code.length))) emitted.code = true + +/-- The check accepts exactly the bodies whose instructions pass the list-indexed rules with the + link a scan of the table gives each pc: indexed lookups and links read beside the code lose + no evidence the scans accept and admit none they reject. -/ theorem ManagedCallsValid_iff_original (methods : List SpecializedMethod) - (imports : List ImportSlot) (raw : RawMethod) (reachable : List Bool) (emitted : Method) : - ManagedCallsValid methods imports raw reachable emitted ↔ + (imports : List ImportSlot) (modelled : CallOpcode × MethodRef → Bool) (raw : RawMethod) + (reachable : List Bool) (emitted : Method) : + ManagedCallsValid methods imports modelled raw reachable emitted ↔ + keysAscend 0 emitted.links = true ∧ + emitted.links.all (fun entry => decide (entry.1 < emitted.code.length)) = true ∧ raw.code.length = reachable.length ∧ raw.code.length = emitted.code.length ∧ - all3 (validateManagedCall methods imports) raw.code reachable emitted.code = true := by + all3 (fun instruction (live, link) op => validateLinkedCallWith modelled methods.length + (methods[·]?) (imports[·]?) instruction live link op) + raw.code (reachable.zip ((List.range emitted.code.length).map emitted.linkAt)) + emitted.code = true := by have index {α : Type} (values : List α) : (FlowIndex.ofList values).get? = (values[·]?) := by funext target exact FlowIndex.get?_ofList values target + have reads (ascending : keysAscend 0 emitted.links = true) : + linkWalk emitted.links emitted.links 0 (List.range emitted.code.length) = + (List.range emitted.code.length).map emitted.linkAt := + linkWalk_eq _ ascending _ _ 0 (dropBelow_zero _).symm unfold ManagedCallsValid rw [index, index] - rfl + constructor + · rintro ⟨ascending, rest⟩ + rw [reads ascending] at rest + exact ⟨ascending, rest⟩ + · rintro ⟨ascending, rest⟩ + rw [reads ascending] + exact ⟨ascending, rest⟩ #print axioms ManagedCallsValid_iff_original -instance (methods : List SpecializedMethod) (imports : List ImportSlot) (raw : RawMethod) - (reachable : List Bool) (emitted : Method) : - Decidable (ManagedCallsValid methods imports raw reachable emitted) := - inferInstanceAs (Decidable (_ ∧ _ ∧ _)) - -/-- Checks generated call bindings structurally, retaining kernel decision for residual checks. - Reflexive metadata comparisons use equality proofs instead of re-evaluating strings. -/ -macro "cil_managed_calls" "[" facts:term,* "]" : tactic => - `(tactic| first - | (apply (ManagedCallsValid_iff_original _ _ _ _ _).mpr - refine ⟨?_, ?_, ?_⟩ - · rfl - · rfl - · simp [all3, validateManagedCall, validateManagedCallWith, managedCallTarget, - constructedOwnerMatches, - $[$facts:term],*] <;> - decide +kernel) - | decide +kernel) - +/-- An admitted body gives every pc of an instruction other than a construction a link the + instruction fits: no link at a `call` of a modelled method, a `.direct` link at any other + `call` and the link its program index implies elsewhere, so a step of such an instruction + never takes the `unlinkedCall` branch for want of a link. -/ +theorem ManagedCallsValid.linkFits {methods : List SpecializedMethod} + {imports : List ImportSlot} {modelled : CallOpcode × MethodRef → Bool} {raw : RawMethod} + {reachable : List Bool} {emitted : Method} + (valid : ManagedCallsValid methods imports modelled raw reachable emitted) {pc : Nat} + {op : Instruction} (found : emitted.code[pc]? = some op) (implied : op.constructs = false) : + op.linkFits modelled (emitted.linkAt pc) = true := by + obtain ⟨_, _, statesLength, codeLength, checks⟩ := + (ManagedCallsValid_iff_original _ _ _ _ _ _).mp valid + have inside : pc < emitted.code.length := (List.getElem?_eq_some_iff.mp found).1 + obtain ⟨instruction, rawFound⟩ : + ∃ instruction, raw.code[pc]? = some instruction := + ⟨raw.code[pc]'(codeLength ▸ inside), List.getElem?_eq_getElem _⟩ + have liveInside : pc < reachable.length := statesLength ▸ codeLength ▸ inside + have paired : (reachable.zip ((List.range emitted.code.length).map emitted.linkAt))[pc]? = + some (reachable[pc], emitted.linkAt pc) := by + simp [List.getElem?_zip_eq_some, liveInside, inside] + have checked := all3_at checks rawFound paired found + have unnamed : op.constructor? = none := by + simpa [Instruction.constructs] using implied + exact validateLinkedCallWith_linkFits unnamed checked + +#print axioms ManagedCallsValid.linkFits + +/-- A direct managed call enters a method whose owner, signature, definition token and type + arguments all match its checked operand. -/ +theorem ManagedCallsValid.direct_callee {methods : List SpecializedMethod} + {imports : List ImportSlot} {modelled : CallOpcode × MethodRef → Bool} {raw : RawMethod} + {reachable : List Bool} {emitted : Method} + (valid : ManagedCallsValid methods imports modelled raw reachable emitted) + {pc target token : Nat} {method : MethodRef} {prefixes : CallPrefixes} + {instruction : RawInstruction} {site : CallSite} + (found : emitted.code[pc]? = some (.call method prefixes)) + (live : reachable[pc]? = some true) (rawFound : raw.code[pc]? = some instruction) + (operand : instruction.operand = .call site) (managed : site.target = .managed token) + (linked : emitted.linkAt pc = some (.direct target)) : + callOperandMatches site (.call method prefixes) = true ∧ + ∃ callee, methods[target]? = some callee ∧ calleeMatchesSite site token callee = true := by + obtain ⟨_, _, _, _, checks⟩ := (ManagedCallsValid_iff_original _ _ _ _ _ _).mp valid + have inside : pc < emitted.code.length := (List.getElem?_eq_some_iff.mp found).1 + have paired : (reachable.zip ((List.range emitted.code.length).map emitted.linkAt))[pc]? = + some (true, some (.direct target)) := by + simp [List.getElem?_zip_eq_some, live, inside, linked] + have checked := all3_at checks rawFound paired found + simp only [validateLinkedCallWith, Instruction.constructor?, validateManagedCallWith, + managedCallTarget, operand, managed, Bool.not_true, Bool.false_eq_true, ite_false, + Bool.and_eq_true] at checked + obtain ⟨_, _, checked⟩ := checked + by_cases opcode : instruction.opcode = 40 + · simp only [opcode, ite_true] at checked + cases selected : methods[target]? with + | none => simp [selected] at checked + | some callee => + simp only [selected, Bool.and_eq_true] at checked + exact ⟨checked.2, callee, rfl, checked.1.1.1⟩ + · simp [opcode] at checked + +#print axioms ManagedCallsValid.direct_callee + +/-- At a live `call` whose raw operand names managed token `token`, the method its `.direct` + link enters has that token and the site's type arguments. Its operand passes the name check. -/ +theorem ManagedCallsValid.direct_operand {methods : List SpecializedMethod} + {imports : List ImportSlot} {modelled : CallOpcode × MethodRef → Bool} {raw : RawMethod} + {reachable : List Bool} {emitted : Method} + (valid : ManagedCallsValid methods imports modelled raw reachable emitted) + {pc target token : Nat} {method : MethodRef} {prefixes : CallPrefixes} + {instruction : RawInstruction} {site : CallSite} + (found : emitted.code[pc]? = some (.call method prefixes)) + (live : reachable[pc]? = some true) (rawFound : raw.code[pc]? = some instruction) + (operand : instruction.operand = .call site) (managed : site.target = .managed token) + (linked : emitted.linkAt pc = some (.direct target)) : + callOperandMatches site (.call method prefixes) = true ∧ + ∃ callee, methods[target]? = some callee ∧ callee.closed.token = token ∧ + callee.arguments = site.typeArguments := by + obtain ⟨reference, callee, selected, agrees⟩ := valid.direct_callee found live rawFound operand + managed linked + simp only [calleeMatchesSite, Bool.and_eq_true, beq_iff_eq, and_assoc] at agrees + exact ⟨reference, callee, selected, agrees.2.1, agrees.2.2.1⟩ + +#print axioms ManagedCallsValid.direct_operand + +instance (methods : List SpecializedMethod) (imports : List ImportSlot) + (modelled : CallOpcode × MethodRef → Bool) (raw : RawMethod) (reachable : List Bool) + (emitted : Method) : + Decidable (ManagedCallsValid methods imports modelled raw reachable emitted) := + inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _)) + +/-- Every live pc passes the linked call rule with the link the table gives it. -/ theorem validated_managed_call {methods : List SpecializedMethod} {imports : List ImportSlot} - {raw : RawMethod} {reachable : List Bool} {emitted : Method} - (valid : ManagedCallsValid methods imports raw reachable emitted) (pc : Fin raw.code.length) : - validateManagedCall methods imports raw.code[pc] reachable[pc.val]! - (emitted.code[pc.val]?.getD (.unsupported "Missing instruction")) = true := by - have valid := (ManagedCallsValid_iff_original _ _ _ _ _).mp valid - have live : pc.val < reachable.length := valid.1 ▸ pc.isLt - have present : pc.val < emitted.code.length := valid.2.1 ▸ pc.isLt + {modelled : CallOpcode × MethodRef → Bool} {raw : RawMethod} {reachable : List Bool} + {emitted : Method} + (valid : ManagedCallsValid methods imports modelled raw reachable emitted) + (pc : Fin raw.code.length) : + validateLinkedCall modelled methods imports raw.code[pc] reachable[pc.val]! + (emitted.linkAt pc.val) (emitted.code[pc.val]?.getD (.unadmitted 0)) = true := by + obtain ⟨_, _, statesLength, codeLength, checks⟩ := + (ManagedCallsValid_iff_original _ _ _ _ _ _).mp valid + have live : pc.val < reachable.length := statesLength ▸ pc.isLt + have present : pc.val < emitted.code.length := codeLength ▸ pc.isLt rw [getElem!_pos reachable pc.val live, List.getElem?_eq_getElem present, Option.getD_some] - exact all3_at valid.2.2 (List.getElem?_eq_getElem pc.isLt) (List.getElem?_eq_getElem live) + have paired : (reachable.zip ((List.range emitted.code.length).map emitted.linkAt))[pc.val]? = + some (reachable[pc.val], emitted.linkAt pc) := by + simp [List.getElem?_zip_eq_some, live, present] + have pass := all3_at checks (List.getElem?_eq_getElem pc.isLt) paired (List.getElem?_eq_getElem present) + exact pass -/-- An accepted live `call` past every extracted method is an external site calling a slot - with its identity and arity. -/ +/-- An accepted live `call` linked past every extracted method is an external site calling a + slot with its identity and arity. -/ theorem validated_import_call {methods : List SpecializedMethod} {imports : List ImportSlot} - {raw : RawInstruction} {target count : Nat} - (valid : validateManagedCall methods imports raw true (.call target count) = true) + {raw : RawInstruction} {method : MethodRef} {prefixes : CallPrefixes} {target : Nat} + (valid : validateManagedCall methods imports raw true (some (.direct target)) + (.call method prefixes) = true) (outside : methods.length ≤ target) : ∃ site identity slot, raw.operand = .call site ∧ site.target = .external identity ∧ raw.opcode = 40 ∧ importSignatureMatches slot site = true ∧ imports[target - methods.length]? = some slot ∧ slot.identity = identity ∧ slot.parameterCount = site.parameterCount ∧ slot.hasThis = site.hasThis ∧ slot.returnsValue = site.returnsValue ∧ - count = site.parameterCount + (if site.hasThis then 1 else 0) := by + method.argumentCount = site.parameterCount + (if site.hasThis then 1 else 0) := by obtain ⟨offset, opcode, operand⟩ := raw cases operand with | call site => - obtain ⟨declaringType, name, method, typeArguments, parameterCount, hasThis, returnsValue, - returnType, parameterTypes, ownerArgumentCount⟩ := site - cases method with + obtain ⟨methodRef, declared, name, called, typeArguments, parameterCount, returnsValue, + ownerArgumentCount⟩ := site + cases called with | managed token => have missing : methods[target]? = none := List.getElem?_eq_none_iff.mpr outside simp only [validateManagedCall, validateManagedCallWith, managedCallTarget, @@ -785,15 +979,17 @@ theorem validated_import_call {methods : List SpecializedMethod} {imports : List | none => simp [found] at valid | some slot => simp [found] at valid - obtain ⟨⟨opcode40, _⟩, ⟨⟨⟨⟨⟨same, arity⟩, receiver⟩, result⟩, argc⟩, typed⟩⟩ := valid + obtain ⟨_, ⟨opcode40, _⟩, ⟨⟨⟨⟨⟨same, arity⟩, receiver⟩, result⟩, argc⟩, typed⟩⟩ := + valid exact ⟨_, identity, slot, rfl, rfl, opcode40, typed, rfl, same, arity, receiver, result, argc⟩ | _ => simp [validateManagedCall, validateManagedCallWith] at valid -/-- Every accepted live `call` targets an extracted method or an existing import slot. -/ +/-- Every accepted live `call` is linked to an extracted method or an existing import slot. -/ theorem validated_call_bound {methods : List SpecializedMethod} {imports : List ImportSlot} - {raw : RawInstruction} {target count : Nat} - (valid : validateManagedCall methods imports raw true (.call target count) = true) : + {raw : RawInstruction} {method : MethodRef} {prefixes : CallPrefixes} {target : Nat} + (valid : validateManagedCall methods imports raw true (some (.direct target)) + (.call method prefixes) = true) : target < methods.length + imports.length := by by_cases inside : target < methods.length · exact Nat.lt_add_right _ inside diff --git a/CIL/Extraction/GuardCalls.lean b/CIL/Extraction/GuardCalls.lean deleted file mode 100644 index e40fc463c..000000000 --- a/CIL/Extraction/GuardCalls.lean +++ /dev/null @@ -1,94 +0,0 @@ -import CIL.Extraction.Generics - -/-! -# Argument guard calls - -`ArgumentNullException.ThrowIfNull(object, string)` and the integer instances of the -`ArgumentOutOfRangeException` guards emit exactly their guard operation, decoded from the raw -owner, name, type argument, parameter shape and external identity. No other instruction emits a -guard, and a guard call emits nothing else. --/ - -namespace CIL.Extraction - -/-- The integer type arguments of the guards, with the width and signedness each denotes; the - native integers take the 64-bit target. -/ -private def guardIntegers : List (String × Nat × Bool) := - [("System.SByte", 8, true), ("System.Byte", 8, false), ("System.Int16", 16, true), - ("System.UInt16", 16, false), ("System.Int32", 32, true), ("System.UInt32", 32, false), - ("System.Int64", 64, true), ("System.UInt64", 64, false), ("System.IntPtr", 64, true), - ("System.UIntPtr", 64, false)] - -/-- The `ArgumentOutOfRangeException` methods that test one value against zero. -/ -private def zeroGuards : List (String × ZeroGuard) := - [("ThrowIfZero", .zero), ("ThrowIfNegative", .negative), - ("ThrowIfNegativeOrZero", .negativeOrZero)] - -/-- The `ArgumentOutOfRangeException` methods that compare a value with a bound. -/ -private def comparisonGuards : List (String × ComparisonGuard) := - [("ThrowIfEqual", .equal), ("ThrowIfNotEqual", .notEqual), ("ThrowIfGreaterThan", .greaterThan), - ("ThrowIfGreaterThanOrEqual", .greaterThanOrEqual), ("ThrowIfLessThan", .lessThan), - ("ThrowIfLessThanOrEqual", .lessThanOrEqual)] - -/-- A guard operation, compared between the raw call and the emitted instruction. -/ -private inductive Guard where - | null - | zero (guard : ZeroGuard) (width : Nat) (signed : Bool) - | comparison (guard : ComparisonGuard) (width : Nat) (signed : Bool) - deriving DecidableEq - -/-- The `System.String` parameter name every guard takes last. -/ -private def parameterName : TypeExpr := .object runtimeScope "System.String" - -/-- The external identity of a guard on `owner` with the given signature tail. -/ -private def guardTarget (owner signature : String) : MethodId := - .external (runtimeScope ++ "::System.Void " ++ owner ++ "::" ++ signature) - -/-- The guard operation a raw call implements, from its metadata alone. -/ -private def guardCall (raw : RawInstruction) : Option Guard := do - if raw.opcode != 40 then none else pure () - let .call site := raw.operand | none - if site.hasThis || site.returnsValue || site.returnType != .void then none else pure () - if site.declaringType == .atom runtimeScope "System.ArgumentNullException" then - let parameters := [.atom runtimeScope "System.Object", parameterName] - if site.name == "ThrowIfNull" && site.typeArguments.isEmpty && - site.parameterCount == 2 && site.parameterTypes == parameters && - site.target == guardTarget "System.ArgumentNullException" - "ThrowIfNull(System.Object,System.String)" - then some .null else none - else if site.declaringType == .atom runtimeScope "System.ArgumentOutOfRangeException" then - let [argument] := site.typeArguments | none - let .atom scope type := argument | none - if scope != runtimeScope then none else pure () - let (_, width, signed) ← guardIntegers.find? (·.1 == type) - let owner := "System.ArgumentOutOfRangeException" - match zeroGuards.find? (·.1 == site.name) with - | some (name, guard) => - if site.parameterCount == 2 && site.parameterTypes == [argument, parameterName] && - site.target == guardTarget owner (name ++ "(!!0,System.String)") - then some (.zero guard width signed) else none - | none => - let (name, guard) ← comparisonGuards.find? (·.1 == site.name) - if site.parameterCount == 3 && - site.parameterTypes == [argument, argument, parameterName] && - site.target == guardTarget owner (name ++ "(!!0,!!0,System.String)") - then some (.comparison guard width signed) else none - else none - -/-- A call to `ArgumentNullException.ThrowIfNull(object, string)`, whose `object` parameter takes - any object reference. -/ -def nullGuardCall (raw : RawInstruction) : Bool := - guardCall raw == some .null - -/-- The guard an emitted operation performs, if any. -/ -private def emittedGuard : Op → Option Guard - | .memory .throwIfNull => some .null - | .memory (.zeroGuard guard width signed) => some (.zero guard width signed) - | .memory (.comparisonGuard guard width signed) => some (.comparison guard width signed) - | _ => none - -/-- A guard operation comes only from its exact call, and a guard call emits only its guard. -/ -def guardCallMatches (raw : RawInstruction) (emitted : Op) : Bool := - guardCall raw == emittedGuard emitted - -end CIL.Extraction diff --git a/CIL/Extraction/GuardModels.lean b/CIL/Extraction/GuardModels.lean new file mode 100644 index 000000000..613b63781 --- /dev/null +++ b/CIL/Extraction/GuardModels.lean @@ -0,0 +1,117 @@ +import CIL.Extraction.ModelTable + +/-! The null and integer argument guards called through System.Runtime. -/ + +namespace CIL.Extraction + +/-- The integer guard determines the number and order of operands before the parameter name. -/ +private inductive GuardKind where + | null + | zero (operation : ZeroGuard) + | comparison (operation : ComparisonGuard) + +/-- The integer guard reads the low bits at the declared width and signedness. -/ +private def guardInteger (element : TypeExpr) : Option (Nat × Bool) := do + let .atom scope name := element | none + guard (scope == runtimeScope) + match name with + | "System.SByte" => some (8, true) + | "System.Byte" => some (8, false) + | "System.Int16" => some (16, true) + | "System.UInt16" => some (16, false) + | "System.Int32" => some (32, true) + | "System.UInt32" => some (32, false) + | "System.Int64" | "System.IntPtr" => some (64, true) + | "System.UInt64" | "System.UIntPtr" => some (64, false) + | _ => none + +/-- Every guard has a final string argument naming the parameter. -/ +private def GuardKind.signature (kind : GuardKind) (element : TypeExpr) : OpenSignature := + let label := TypeExpr.object runtimeScope "System.String" + match kind with + | .null => { convention := 0, parameters := [.atom runtimeScope "System.Object", label] } + | .zero _ => { convention := 0x10, parameters := [element, label] } + | .comparison _ => { convention := 0x10, parameters := [element, element, label] } + +/-- The closed reference instantiates the same value type in each integer argument. -/ +private def GuardKind.accepts (kind : GuardKind) (method : MethodRef) : Bool := + !method.hasThis && match kind, method.genericArity, method.methodArguments with + | .null, 0, [] => method.signature 0 == kind.signature .void + | .zero _, 1, [element] | .comparison _, 1, [element] => + method.signature 0x10 == kind.signature element + | _, _, _ => false + +/-- The selected operation reads the same checked operands as the integer or null guard. -/ +private def GuardKind.operation (kind : GuardKind) (method : MethodRef) : Option MemoryOp := do + match kind with + | .null => return .throwIfNull + | .zero operation => + let (width, signed) ← method.methodArguments.head? >>= guardInteger + return .zeroGuard operation width signed + | .comparison operation => + let (width, signed) ← method.methodArguments.head? >>= guardInteger + return .comparisonGuard operation width signed + +/-- The owned guard row binds the uninstantiated member and its assembly-qualified target. -/ +private def guardEntry (key : MetadataName) (name : String) (kind : GuardKind) : ModelEntry := + let (owner, parameters) := match kind with + | .null => ("System.ArgumentNullException", "System.Object,System.String") + | .zero _ => ("System.ArgumentOutOfRangeException", "!!0,System.String") + | .comparison _ => ("System.ArgumentOutOfRangeException", "!!0,!!0,System.String") + let target := s!"{runtimeScope}::System.Void {owner}::{name}({parameters})" + let row : ModelOverload := + { operand := .external (fun _ => kind.signature (.parameter 0)) name (fun _ => some target) + accepts := kind.accepts + resolve := fun _ method => RuntimeModel.memory <$> kind.operation method } + { opcode := .call, owner := .atom runtimeScope owner, name := key, ownsName := true, + overloads := [row] } + +/-- The runtime row whose object parameter requires object-reference provenance on the stack. -/ +def nullGuardEntry := guardEntry name!"ThrowIfNull" "ThrowIfNull" .null + +/-- The reserved runtime slots of the null guard and each integer guard. -/ +def guardEntries : List ModelEntry := [ + nullGuardEntry, + guardEntry name!"ThrowIfZero" "ThrowIfZero" + (.zero .zero), + guardEntry name!"ThrowIfNegative" "ThrowIfNegative" + (.zero .negative), + guardEntry name!"ThrowIfNegativeOrZero" "ThrowIfNegativeOrZero" + (.zero .negativeOrZero), + guardEntry name!"ThrowIfEqual" "ThrowIfEqual" + (.comparison .equal), + guardEntry name!"ThrowIfNotEqual" "ThrowIfNotEqual" + (.comparison .notEqual), + guardEntry name!"ThrowIfGreaterThan" "ThrowIfGreaterThan" + (.comparison .greaterThan), + guardEntry name!"ThrowIfGreaterThanOrEqual" "ThrowIfGreaterThanOrEqual" + (.comparison .greaterThanOrEqual), + guardEntry name!"ThrowIfLessThan" "ThrowIfLessThan" + (.comparison .lessThan), + guardEntry name!"ThrowIfLessThanOrEqual" "ThrowIfLessThanOrEqual" + (.comparison .lessThanOrEqual)] + +/-- A guard row resolves only to a checked memory operation. -/ +private theorem guardEntry_yields {key name kind} {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : + (guardEntry key name kind).Yields property := by + intro reader method model found + simp only [ModelEntry.resolve, guardEntry, ModelEntry.select?, ModelEntry.select?.select] at found + split at found <;> simp_all + obtain ⟨_, _, rfl⟩ := found + exact memory _ + +#print axioms guardEntry_yields + +/-- Each guard transports and executes as its checked memory operation. -/ +theorem guardEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : + ∀ entry ∈ guardEntries, entry.Yields property := by + intro entry listed + simp only [guardEntries, List.mem_cons, List.not_mem_nil, or_false] at listed + repeat' rcases listed with rfl | listed + all_goals first | exact guardEntry_yields memory | subst entry; exact guardEntry_yields memory + +#print axioms guardEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/HardwareModels.lean b/CIL/Extraction/HardwareModels.lean new file mode 100644 index 000000000..98f1d0440 --- /dev/null +++ b/CIL/Extraction/HardwareModels.lean @@ -0,0 +1,613 @@ +import CIL.Extraction.WideArithmetic + +/-! Hardware overloads bind the closed member and its open declaration to one checked intrinsic. -/ + +namespace CIL.Extraction + +/-- A declaration can use a runtime primitive, a vector, its generic element, or a pair result. -/ +private inductive HWType where + | word (name : String) + | vector (width : Nat) (element : HWType) + | element + | pair + deriving DecidableEq + +/-- The structured type and Cecil spelling of a signature component. -/ +private abbrev Operand := TypeExpr × String + +/-- The checked scope and metadata name of a vector constructor. -/ +private def vectorHead (width : Nat) := s!"System.Runtime.Intrinsics.Vector{width}`1" + +/-- A declaration component retains its type variable in the external identity. -/ +private def HWType.operand (argument : Operand) (result : TypeExpr) : HWType → Operand + | .element => argument + | .pair => (result, "System.ValueTuple`2") + | .word name => (.atom runtimeScope ("System." ++ name), "System." ++ name) + | .vector width scalar => + let (type, name) := scalar.operand argument result + let head := vectorHead width + (.apply (.constructor intrinsicsScope head) type, s!"{head}<{name}>") + +/-- The allowed instantiations of an open declaration determine its operation. -/ +private structure HardwareRow where + result : HWType + parameters : List HWType + operation : String → Intrinsic + elements : List String := [] + +/-- A signature with one operation at every admitted element. -/ +private def signature (result : HWType) (parameters : List HWType) (operation : Intrinsic) + (elements : List String := []) : HardwareRow := + ⟨result, parameters, fun _ => operation, elements⟩ + +/-- One opcode, owner and metadata name reserves every overload in its slot. -/ +private structure HardwareSlot where + owner : String + key : MetadataName + name : String + rows : List HardwareRow + genericOwner : Bool := false + +/-- The full owner spelling includes its namespace and any generic arity. -/ +private def HardwareSlot.fullOwner (slot : HardwareSlot) := + "System.Runtime.Intrinsics." ++ slot.owner + +/-- The exact declaring type or open vector constructor used for table lookup. -/ +private def HardwareSlot.head (slot : HardwareSlot) : TypeExpr := + if slot.genericOwner then .constructor intrinsicsScope slot.fullOwner + else .atom intrinsicsScope slot.fullOwner + +/-- A static member of a vector constructor carries its element in the declaring type. -/ +private def HardwareSlot.matches (slot : HardwareSlot) (method : MethodRef) : Bool := + method.name == slot.key && if slot.genericOwner then + match method.owner with + | .apply head _ => head == slot.head + | _ => false + else method.owner == slot.head + +/-- The closed signature selects a primitive argument from exactly the declared binder. -/ +private def HardwareRow.argument (row : HardwareRow) (slot : HardwareSlot) (method : MethodRef) : + Option Operand := do + guard (slot.matches method && !method.hasThis) + let generic := !slot.genericOwner && !row.elements.isEmpty + guard (method.genericArity == (if generic then 1 else 0)) + let type ← if generic then + match method.methodArguments with + | [argument] => some argument + | _ => none + else do + guard method.methodArguments.isEmpty + if !slot.genericOwner then pure TypeExpr.void + else match method.owner with + | .apply _ element => pure element + | _ => none + if row.elements.isEmpty then return (.void, "") + let .atom scope name := type | none + guard (scope == runtimeScope && row.elements.contains name) + return (type, name) + +/-- An overload checks the full closed signature before assigning an intrinsic and arity. -/ +private def HardwareRow.accepts (row : HardwareRow) (slot : HardwareSlot) (method : MethodRef) : + Bool := + (row.argument slot method).any fun argument => + let operand := HWType.operand argument method.returnType + method.parameters == row.parameters.map (fun type => (operand type).1) && + method.returnType == (operand row.result).1 && + (row.result != .pair || (match method.returnType with + | .aggregate identity _ => identity == integerPairIdentity "System.UInt64" + | _ => false)) + +/-- The open signature and external identity distinguish owner and method binders. -/ +private def HardwareRow.binding (row : HardwareRow) (slot : HardwareSlot) : ModelOperand := + let generic := !slot.genericOwner && !row.elements.isEmpty + let binder : Operand := if slot.genericOwner then (.ownerParameter 0, "!0") + else (.parameter 0, "!!0") + let declared (method : MethodRef) : OpenSignature := + let operand := HWType.operand binder method.returnType + { convention := if generic then 16 else 0, returnType := (operand row.result).1, + parameters := row.parameters.map (fun type => (operand type).1) } + .external declared slot.name fun method => do + let argument ← row.argument slot method + let owner := if slot.genericOwner then s!"{slot.fullOwner}<{argument.2}>" else slot.fullOwner + let operand := HWType.operand binder method.returnType + let names := ",".intercalate (row.parameters.map (fun type => (operand type).2)) + return s!"{intrinsicsScope}::{(operand row.result).2} {owner}::{slot.name}({names})" + +/-- Only a checked pair layout can receive the tuple produced by hardware division. -/ +private def HardwareRow.model (row : HardwareRow) (slot : HardwareSlot) (reader : CallLayouts) + (method : MethodRef) : Option RuntimeModel := do + let argument ← row.argument slot method + guard (row.result != .pair || integerPairResult reader method.returnType "System.UInt64" 8) + return .intrinsic (row.operation argument.2) row.parameters.length + +/-- The row selected by the closed types also supplies the external operand check. -/ +private def HardwareSlot.entry (slot : HardwareSlot) : ModelEntry := + { opcode := .call, owner := slot.head, name := slot.key, ownsName := true, + overloads := slot.rows.map fun row => + { operand := row.binding slot, accepts := row.accepts slot, resolve := row.model slot } } + +/-- The UInt64 hardware signature component. -/ +private def u64 := HWType.word "UInt64" + +/-- The Int64 hardware signature component. -/ +private def i64 := HWType.word "Int64" + +/-- The UInt32 hardware signature component. -/ +private def u32 := HWType.word "UInt32" + +/-- The Int32 hardware signature component. -/ +private def i32 := HWType.word "Int32" + +/-- The Byte hardware signature component. -/ +private def u8 := HWType.word "Byte" + +/-- The Boolean hardware signature component. -/ +private def boolean := HWType.word "Boolean" + +/-- The Single hardware signature component. -/ +private def f32 := HWType.word "Single" + +/-- The Double hardware signature component. -/ +private def f64 := HWType.word "Double" + +/-- The element type determines the 128-bit vector signature. -/ +private def v128 := HWType.vector 128 + +/-- The element type determines the 256-bit vector signature. -/ +private def v256 := HWType.vector 256 + +/-- The element type determines the 512-bit vector signature. -/ +private def v512 := HWType.vector 512 + +/-- The fixed declarations of `X86.Avx2.And`. -/ +private def x86Avx2And : HardwareSlot := + ⟨"X86.Avx2", name!"And", "And", + [signature (v256 u64) [v256 u64, v256 u64] (.avx2 .band)], false⟩ + +/-- The fixed declarations of `X86.Avx2.Or`. -/ +private def x86Avx2Or : HardwareSlot := + ⟨"X86.Avx2", name!"Or", "Or", + [signature (v256 u64) [v256 u64, v256 u64] (.avx2 .bor)], false⟩ + +/-- The fixed declarations of `X86.Avx2.Xor`. -/ +private def x86Avx2Xor : HardwareSlot := + ⟨"X86.Avx2", name!"Xor", "Xor", + [signature (v256 u64) [v256 u64, v256 u64] (.avx2 .bxor)], false⟩ + +/-- The fixed declarations of `X86.Avx2.AndNot`. -/ +private def x86Avx2AndNot : HardwareSlot := + ⟨"X86.Avx2", name!"AndNot", "AndNot", + [signature (v256 u64) [v256 u64, v256 u64] (.avx2 .andNot)], false⟩ + +/-- The fixed declarations of `X86.Avx2.ShiftLeftLogical`. -/ +private def x86Avx2ShiftLeftLogical : HardwareSlot := + ⟨"X86.Avx2", name!"ShiftLeftLogical", "ShiftLeftLogical", + [signature (v256 u64) [v256 u64, v128 u64] (.avx2 .shl64Count), + signature (v256 u64) [v256 u64, u8] (.avx2 .shl64)], false⟩ + +/-- The fixed declarations of `X86.Avx2.ShiftRightLogical`. -/ +private def x86Avx2ShiftRightLogical : HardwareSlot := + ⟨"X86.Avx2", name!"ShiftRightLogical", "ShiftRightLogical", + [signature (v256 u64) [v256 u64, v128 u64] (.avx2 .shr64Count), + signature (v256 u64) [v256 u64, u8] (.avx2 .shr64)], false⟩ + +/-- The fixed declarations of `X86.Avx512F.CompareLessThan`. -/ +private def x86Avx512FCompareLessThan : HardwareSlot := + ⟨"X86.Avx512F", name!"CompareLessThan", "CompareLessThan", + [signature (v512 u64) [v512 u64, v512 u64] (.avx512 .ltu512)], false⟩ + +/-- The fixed declarations of `X86.Avx512F.CompareGreaterThan`. -/ +private def x86Avx512FCompareGreaterThan : HardwareSlot := + ⟨"X86.Avx512F", name!"CompareGreaterThan", "CompareGreaterThan", + [signature (v512 u64) [v512 u64, v512 u64] (.avx512 .gtu512)], false⟩ + +/-- The fixed declarations of `X86.Avx512DQ.MoveMask`. -/ +private def x86Avx512DQMoveMask : HardwareSlot := + ⟨"X86.Avx512DQ", name!"MoveMask", "MoveMask", + [signature i32 [v512 u64] (.avx512DQ .moveMask512), + signature i32 [v256 u64] (.avx512DQ .moveMask64)], false⟩ + +/-- The fixed declarations of `Arm.AdvSimd.ReverseElement8`. -/ +private def armAdvSimdReverseElement8 : HardwareSlot := + ⟨"Arm.AdvSimd", name!"ReverseElement8", "ReverseElement8", + [signature (v128 u64) [v128 u64] (.advSimd .reverseElement8)], false⟩ + +/-- The fixed declarations of `X86.Sse2.Multiply`. -/ +private def x86Sse2Multiply : HardwareSlot := + ⟨"X86.Sse2", name!"Multiply", "Multiply", + [signature (v128 u64) [v128 u32, v128 u32] (.sse .multiplyEven32)], false⟩ + +/-- The fixed declarations of `X86.Ssse3.Shuffle`. -/ +private def x86Ssse3Shuffle : HardwareSlot := + ⟨"X86.Ssse3", name!"Shuffle", "Shuffle", + [signature (v128 u8) [v128 u8, v128 u8] (.sse .shuffleBytes)], false⟩ + +/-- The fixed declarations of `X86.Avx2.Shuffle`. -/ +private def x86Avx2Shuffle : HardwareSlot := + ⟨"X86.Avx2", name!"Shuffle", "Shuffle", + [signature (v256 u8) [v256 u8, v256 u8] (.avx2 .shuffleBytes)], false⟩ + +/-- The fixed declarations of `X86.Aes.Encrypt`. -/ +private def x86AesEncrypt : HardwareSlot := + ⟨"X86.Aes", name!"Encrypt", "Encrypt", + [signature (v128 u8) [v128 u8, v128 u8] (.aes .encrypt)], false⟩ + +/-- The fixed declarations of `Arm.Aes.Encrypt`. -/ +private def armAesEncrypt : HardwareSlot := + ⟨"Arm.Aes", name!"Encrypt", "Encrypt", + [signature (v128 u8) [v128 u8, v128 u8] (.armAes .encrypt)], false⟩ + +/-- The fixed declarations of `Arm.Aes.MixColumns`. -/ +private def armAesMixColumns : HardwareSlot := + ⟨"Arm.Aes", name!"MixColumns", "MixColumns", + [signature (v128 u8) [v128 u8] (.armAes .mixColumns)], false⟩ + +/-- The fixed declarations of `X86.Avx512F/VL.PermuteVar4x64x2`. -/ +private def x86Avx512FVLPermuteVar4x64x2 : HardwareSlot := + ⟨"X86.Avx512F/VL", name!"PermuteVar4x64x2", "PermuteVar4x64x2", + [signature (v256 u64) [v256 u64, v256 u64, v256 u64] (.avx512 .permuteVar4x64x2)], false⟩ + +/-- The exact machine hardware slots. -/ +private def exactMachineSlots : List HardwareSlot := [ + x86Avx2And, + x86Avx2Or, + x86Avx2Xor, + x86Avx2AndNot, + x86Avx2ShiftLeftLogical, + x86Avx2ShiftRightLogical, + x86Avx512FCompareLessThan, + x86Avx512FCompareGreaterThan, + x86Avx512DQMoveMask, + armAdvSimdReverseElement8, + x86Sse2Multiply, + x86Ssse3Shuffle, + x86Avx2Shuffle, + x86AesEncrypt, + armAesEncrypt, + armAesMixColumns, + x86Avx512FVLPermuteVar4x64x2] + + +/-- The fixed declarations of `Arm.AdvSimd.ExtractVector128`. -/ +private def armAdvSimdExtractVector128 : HardwareSlot := + ⟨"Arm.AdvSimd", name!"ExtractVector128", "ExtractVector128", + [signature (v128 u64) [v128 u64, v128 u64, u8] (.advSimd .extract64)], false⟩ + +/-- The fixed declarations of `X86.Sse2.ShiftLeftLogical128BitLane`. -/ +private def x86Sse2ShiftLeftLogical128BitLane : HardwareSlot := + ⟨"X86.Sse2", name!"ShiftLeftLogical128BitLane", "ShiftLeftLogical128BitLane", + [signature (v128 u64) [v128 u64, u8] (.sse .shiftLeftBytes)], false⟩ + +/-- The fixed declarations of `X86.Ssse3.AlignRight`. -/ +private def x86Ssse3AlignRight : HardwareSlot := + ⟨"X86.Ssse3", name!"AlignRight", "AlignRight", + [signature (v128 u8) [v128 u8, v128 u8, u8] (.sse .alignBytes)], false⟩ + +/-- The fixed declarations of `X86.Avx2.Permute4x64`. -/ +private def x86Avx2Permute4x64 : HardwareSlot := + ⟨"X86.Avx2", name!"Permute4x64", "Permute4x64", + [signature (v256 u64) [v256 u64, u8] (.avx2 .permute4x64)], false⟩ + +/-- The fixed declarations of `X86.Avx2.Blend`. -/ +private def x86Avx2Blend : HardwareSlot := + ⟨"X86.Avx2", name!"Blend", "Blend", + [signature (v256 u32) [v256 u32, v256 u32, u8] (.avx2 .blend32), + signature (v256 i32) [v256 i32, v256 i32, u8] (.avx2 .blend32)], false⟩ + +/-- The fixed declarations of `X86.Avx2.Add`. -/ +private def x86Avx2Add : HardwareSlot := + ⟨"X86.Avx2", name!"Add", "Add", + [signature (v256 u64) [v256 u64, v256 u64] (.avx2 .add64)], false⟩ + +/-- The fixed declarations of `X86.Avx2.CompareEqual`. -/ +private def x86Avx2CompareEqual : HardwareSlot := + ⟨"X86.Avx2", name!"CompareEqual", "CompareEqual", + [signature (v256 u64) [v256 u64, v256 u64] (.avx2 .eq64)], false⟩ + +/-- The fixed declarations of `X86.Avx2.CompareGreaterThan`. -/ +private def x86Avx2CompareGreaterThan : HardwareSlot := + ⟨"X86.Avx2", name!"CompareGreaterThan", "CompareGreaterThan", + [signature (v256 i64) [v256 i64, v256 i64] (.avx2 .signedgt64)], false⟩ + +/-- The fixed declarations of `X86.Avx2.Multiply`. -/ +private def x86Avx2Multiply : HardwareSlot := + ⟨"X86.Avx2", name!"Multiply", "Multiply", + [signature (v256 u64) [v256 u32, v256 u32] (.avx2 .multiplyEven32)], false⟩ + +/-- The fixed declarations of `X86.Avx512F/VL.AlignRight64`. -/ +private def x86Avx512FVLAlignRight64 : HardwareSlot := + ⟨"X86.Avx512F/VL", name!"AlignRight64", "AlignRight64", + [signature (v256 u64) [v256 u64, v256 u64, u8] (.avx512 .alignRight64)], false⟩ + +/-- The fixed declarations of `X86.Avx512F/VL.TernaryLogic`. -/ +private def x86Avx512FVLTernaryLogic : HardwareSlot := + ⟨"X86.Avx512F/VL", name!"TernaryLogic", "TernaryLogic", + [signature (v256 u64) [v256 u64, v256 u64, v256 u64, u8] (.avx512 .ternaryLogic)], false⟩ + +/-- The fixed declarations of `X86.Avx512F/VL.CompareLessThan`. -/ +private def x86Avx512FVLCompareLessThan : HardwareSlot := + ⟨"X86.Avx512F/VL", name!"CompareLessThan", "CompareLessThan", + [signature (v256 u64) [v256 u64, v256 u64] (.avx512 .ltu64)], false⟩ + +/-- The fixed declarations of `X86.Avx512F/VL.CompareGreaterThan`. -/ +private def x86Avx512FVLCompareGreaterThan : HardwareSlot := + ⟨"X86.Avx512F/VL", name!"CompareGreaterThan", "CompareGreaterThan", + [signature (v256 u64) [v256 u64, v256 u64] (.avx512 .gtu64)], false⟩ + +/-- The fixed declarations of `X86.Avx512F/VL.CompareGreaterThanOrEqual`. -/ +private def x86Avx512FVLCompareGreaterThanOrEqual : HardwareSlot := + ⟨"X86.Avx512F/VL", name!"CompareGreaterThanOrEqual", "CompareGreaterThanOrEqual", + [signature (v256 u64) [v256 u64, v256 u64] (.avx512 .geu64)], false⟩ + +/-- The fixed declarations of `X86.Avx512DQ/VL.MultiplyLow`. -/ +private def x86Avx512DQVLMultiplyLow : HardwareSlot := + ⟨"X86.Avx512DQ/VL", name!"MultiplyLow", "MultiplyLow", + [signature (v256 u64) [v256 u64, v256 u64] (.avx512DQ .mul64)], false⟩ + +/-- The fixed declarations of `X86.Avx.MoveMask`. -/ +private def x86AvxMoveMask : HardwareSlot := + ⟨"X86.Avx", name!"MoveMask", "MoveMask", + [signature i32 [v256 f64] (.avx .moveMask64), + signature i32 [v256 f32] (.avx .moveMask32)], false⟩ + +/-- The fixed declarations of `X86.Avx.Blend`. -/ +private def x86AvxBlend : HardwareSlot := + ⟨"X86.Avx", name!"Blend", "Blend", + [signature (v256 f32) [v256 f32, v256 f32, u8] (.avx .blend32)], false⟩ + +/-- The fixed declarations of `X86.Avx.TestZ`. -/ +private def x86AvxTestZ : HardwareSlot := + ⟨"X86.Avx", name!"TestZ", "TestZ", + [signature boolean [v256 u64, v256 u64] (.avx .testZ64)], false⟩ + +/-- The fixed declarations of `X86.Bmi1.BitFieldExtract`. -/ +private def x86Bmi1BitFieldExtract : HardwareSlot := + ⟨"X86.Bmi1", name!"BitFieldExtract", "BitFieldExtract", + [signature u32 [u32, u8, u8] (.bmi1 .bextr32)], false⟩ + +/-- The fixed declarations of `X86.Bmi2/X64.MultiplyNoFlags`. -/ +private def x86Bmi2X64MultiplyNoFlags : HardwareSlot := + ⟨"X86.Bmi2/X64", name!"MultiplyNoFlags", "MultiplyNoFlags", + [signature u64 [u64, u64] (.bmi2 .multiplyHigh64)], false⟩ + +/-- The fixed declarations of `Arm.ArmBase/Arm64.MultiplyHigh`. -/ +private def armArmBaseArm64MultiplyHigh : HardwareSlot := + ⟨"Arm.ArmBase/Arm64", name!"MultiplyHigh", "MultiplyHigh", + [signature u64 [u64, u64] (.armBase64 .multiplyHigh64)], false⟩ + +/-- The fixed declarations of `X86.X86Base/X64.DivRem`. -/ +private def x86X86BaseX64DivRem : HardwareSlot := + ⟨"X86.X86Base/X64", name!"DivRem", "DivRem", + [signature .pair [u64, u64, u64] (.x86Base .divRem64)], false⟩ + +/-- The other machine hardware slots. -/ +private def otherMachineSlots : List HardwareSlot := [ + armAdvSimdExtractVector128, + x86Sse2ShiftLeftLogical128BitLane, + x86Ssse3AlignRight, + x86Avx2Permute4x64, + x86Avx2Blend, + x86Avx2Add, + x86Avx2CompareEqual, + x86Avx2CompareGreaterThan, + x86Avx2Multiply, + x86Avx512FVLAlignRight64, + x86Avx512FVLTernaryLogic, + x86Avx512FVLCompareLessThan, + x86Avx512FVLCompareGreaterThan, + x86Avx512FVLCompareGreaterThanOrEqual, + x86Avx512DQVLMultiplyLow, + x86AvxMoveMask, + x86AvxBlend, + x86AvxTestZ, + x86Bmi1BitFieldExtract, + x86Bmi2X64MultiplyNoFlags, + armArmBaseArm64MultiplyHigh, + x86X86BaseX64DivRem] + +/-- Portable integer bitwise operations admit the same five primitive elements at each width. -/ +private def integerElements := + ["System.Byte", "System.Int32", "System.UInt32", "System.Int64", "System.UInt64"] + +/-- A portable helper uses the named static class or the vector constructor of its width. -/ +private def portableSlot (width : Nat) (key : MetadataName) (name : String) + (rows : List HardwareRow) (genericOwner := false) : HardwareSlot := + ⟨s!"Vector{width}" ++ (if genericOwner then "`1" else ""), key, name, rows, genericOwner⟩ + +/-- Every integer bitwise declaration keeps its element, arity and vector width. -/ +private def exactVectorSlots (width : Nat) : List HardwareSlot := + let vector := HWType.vector width .element + let member := fun key name count op => + portableSlot width key name + [signature vector (List.replicate count vector) (.vector op) integerElements] true + [member name!"op_BitwiseAnd" "op_BitwiseAnd" 2 (.band width), + member name!"op_BitwiseOr" "op_BitwiseOr" 2 (.bor width), + member name!"op_ExclusiveOr" "op_ExclusiveOr" 2 (.bxor width), + member name!"op_OnesComplement" "op_OnesComplement" 1 (.bnot width), + member name!"get_Zero" "get_Zero" 0 (.zero width), + member name!"get_AllBitsSet" "get_AllBitsSet" 0 (.ones width), + portableSlot width name!"EqualsAll" "EqualsAll" + [signature (.word "Boolean") [vector, vector] (.vector (.equalsAll width)) integerElements]] + +/-- Arithmetic and reinterpretation helpers admit the specified closed elements. -/ +private def otherVectorSlots (width : Nat) : List HardwareSlot := + let vec := HWType.vector width + let openVector := vec .element + let words := ["System.UInt64"] + let cast := fun key name element => portableSlot width key name + [signature (vec (.word element)) [openVector] (.vector (.reinterpret width)) + (integerElements ++ ["System.Single"])] + [portableSlot width name!"op_Addition" "op_Addition" + [signature openVector [openVector, openVector] (.vector (.add64 width)) words] true, + portableSlot width name!"op_Subtraction" "op_Subtraction" + [signature openVector [openVector, openVector] (.vector (.sub64 width)) words] true, + portableSlot width name!"Equals" "Equals" + [signature openVector [openVector, openVector] (.vector (.eq64 width)) words], + portableSlot width name!"LessThan" "LessThan" + [⟨openVector, [openVector, openVector], fun element => + .vector (if element == "System.Int64" then .lt64 width else .ltu64 width), + ["System.Int64", "System.UInt64"]⟩], + portableSlot width name!"Sum" "Sum" + [signature .element [openVector] (.vector (.sum64 width)) words], + portableSlot width name!"ExtractMostSignificantBits" "ExtractMostSignificantBits" + [signature u32 [openVector] (.vector (.extractMSB64 width)) words], + portableSlot width name!"ShiftRightArithmetic" "ShiftRightArithmetic" + [signature (vec i64) [vec i64, .word "Int32"] (.vector (.ashr64 width))], + portableSlot width name!"CreateScalar" "CreateScalar" + [signature (vec u32) [u32] (.vector (.createScalar32 width)), + signature (vec u64) [u64] (.vector (.createScalar64 width))], + cast name!"AsByte" "AsByte" "Byte", + cast name!"AsDouble" "AsDouble" "Double", + cast name!"AsInt32" "AsInt32" "Int32", + cast name!"AsInt64" "AsInt64" "Int64", + cast name!"AsSingle" "AsSingle" "Single", + cast name!"AsUInt32" "AsUInt32" "UInt32", + cast name!"AsUInt64" "AsUInt64" "UInt64"] + +/-- The exact family contains fixed signatures and portable integer bitwise declarations. -/ +private def exactSlots := exactMachineSlots ++ [128, 256, 512].flatMap exactVectorSlots + +/-- Each hardware name reserves its exact owner, including names with several declarations. -/ +private def hardwareSlots := + exactSlots ++ otherMachineSlots ++ [128, 256].flatMap otherVectorSlots + +/-- Hardware calls retain their closed method reference for execution. -/ +def hardwareEntries : List ModelEntry := hardwareSlots.map HardwareSlot.entry + +/-- Every registered hardware overload executes a checked intrinsic with the declared arity. -/ +theorem hardwareEntries_yields {property : RuntimeModel → Prop} + (intrinsic : ∀ operation count, property (.intrinsic operation count)) : + ∀ entry ∈ hardwareEntries, entry.Yields property := by + intro entry listed reader method model found + obtain ⟨slot, _, rfl⟩ := List.mem_map.mp listed + obtain ⟨overload, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + obtain ⟨row, _, rfl⟩ := List.mem_map.mp (ModelEntry.select?_mem selected) + change row.model slot reader method = some model at resolved + unfold HardwareRow.model at resolved + cases argument : row.argument slot method <;> + simp [argument, bind, Option.bind, guard] at resolved + split at resolved <;> simp_all + subst model + exact intrinsic _ _ + +#print axioms hardwareEntries_yields + +/-- The exact hardware slot selected by the closed reference's owner and name. -/ +private def exactSlot (method : MethodRef) := exactSlots.find? (·.matches method) + +/-- A closed reference belongs to the exact family even if its overload is inadmissible. -/ +def exactCallFamily (method : MethodRef) : Bool := (exactSlot method).isSome + +/-- Exact signatures have no layout-dependent result and select only their declared operation. -/ +def exactIntrinsic (method : MethodRef) : Option RuntimeModel := do + let slot ← exactSlot method + slot.entry.resolve (LayoutEnvironment.callLayouts {}) method + +/-- Table lookup examines only these fields of a closed reference. -/ +private def slotReference (owner : TypeExpr) (name : MetadataName) : MethodRef := + { owner, name, genericArity := 0, hasThis := false, returnType := .void, parameters := [] } + +/-- Each owner shape of this slot selects its own entry in the registered table. -/ +private def HardwareSlot.Registered (slot : HardwareSlot) (table : ModelTable) : Prop := + if slot.genericOwner then ∀ element, + table.entry? .call (slotReference (.apply slot.head element) slot.key) = some slot.entry + else table.entry? .call (slotReference slot.head slot.key) = some slot.entry + +/-- Every hardware slot selects its own registered entry for every instantiation of its owner, + so no other entry of the table shadows a hardware name. -/ +def HardwareRegistered (table : ModelTable) : Prop := + ∀ slot ∈ hardwareSlots, slot.Registered table + +/-- Literal lookups at the static hardware slots certify the runtime table's registration. -/ +macro "hardware_registrations" : tactic => `(tactic| + (simp only [HardwareRegistered, hardwareSlots, exactSlots, exactMachineSlots, exactVectorSlots, + otherMachineSlots, otherVectorSlots, + List.flatMap_cons, List.flatMap_nil, List.forall_mem_append, List.forall_mem_cons, + List.not_mem_nil, false_implies, implies_true, and_true, true_and] + repeat' first | apply And.intro | exact True.intro + all_goals first | rfl | exact fun _ => rfl)) + +/-- The closed reference selects the registered entry of its exact slot. -/ +private theorem HardwareSlot.lookup {table : ModelTable} {slot : HardwareSlot} {method : MethodRef} + (registered : slot.Registered table) (matched : slot.matches method = true) : + table.entry? .call method = some slot.entry := by + unfold HardwareSlot.matches at matched + obtain ⟨name, owner⟩ := Bool.and_eq_true_iff.mp matched + have named := beq_iff_eq.mp name + cases generic : slot.genericOwner with + | false => + simp [generic] at owner + simpa only [ModelTable.entry?, named, owner, slotReference] using + (show table.entry? .call (slotReference slot.head slot.key) = _ from + by simpa [HardwareSlot.Registered, generic] using registered) + | true => + cases shape : method.owner <;> simp [generic, shape] at owner + rename_i head element + subst head + have registration : ∀ arg, table.entry? .call + (slotReference (.apply slot.head arg) slot.key) = some slot.entry := by + simpa [HardwareSlot.Registered, generic] using registered + simpa only [ModelTable.entry?, named, shape, slotReference] using registration element + +#print axioms HardwareSlot.lookup + +/-- Every exact row returns a scalar or vector and needs no tuple layout. -/ +private theorem exactSlots_noPair : exactSlots.all (fun slot => + slot.rows.all (fun row => row.result != .pair)) = true := by decide +kernel + +#print axioms exactSlots_noPair + +/-- The hardware slot selected by the closed reference's owner and name. -/ +private def hardwareSlot (method : MethodRef) := hardwareSlots.find? (·.matches method) + +/-- A closed reference names a hardware member even if no row of its slot admits it. -/ +def hardwareCallFamily (method : MethodRef) : Bool := (hardwareSlot method).isSome + +/-- The model the rows of a hardware reference's own slot select. -/ +def hardwareModel (environment : LayoutEnvironment) (method : MethodRef) : + Option RuntimeModel := do + let slot ← hardwareSlot method + slot.entry.resolve environment.callLayouts method + +/-- The table resolves a hardware reference only through the rows of its own slot. -/ +theorem HardwareRegistered.resolve {table : ModelTable} (registered : HardwareRegistered table) + (environment : LayoutEnvironment) (method : MethodRef) + (member : hardwareCallFamily method = true) : + table.find environment .call method = hardwareModel environment method := by + obtain ⟨slot, selected⟩ := Option.isSome_iff_exists.mp member + have found : hardwareSlots.find? (·.matches method) = some slot := selected + have listed := List.mem_of_find?_eq_some found + have matched := List.find?_some found + rw [ModelTable.find, HardwareSlot.lookup (registered slot listed) matched] + simp only [hardwareModel, selected] + rfl + +#print axioms HardwareRegistered.resolve + +/-- The runtime table resolves an exact-family method only through its exact closed signature. -/ +theorem HardwareRegistered.resolveExact {table : ModelTable} + (registered : HardwareRegistered table) + (environment : LayoutEnvironment) (method : MethodRef) + (member : exactCallFamily method = true) : + table.find environment .call method = exactIntrinsic method := by + obtain ⟨slot, selected⟩ := Option.isSome_iff_exists.mp member + have found : exactSlots.find? (·.matches method) = some slot := selected + have listed := List.mem_of_find?_eq_some found + have matched := List.find?_some found + have exact : slot ∈ hardwareSlots := List.mem_append_left _ (List.mem_append_left _ listed) + rw [ModelTable.find, HardwareSlot.lookup (registered slot exact) matched] + simp only [exactIntrinsic, selected] + change slot.entry.resolve environment.callLayouts method = slot.entry.resolve _ method + unfold ModelEntry.resolve + cases chosen : slot.entry.select? method with + | none => rfl + | some overload => + obtain ⟨row, rowMem, rfl⟩ := List.mem_map.mp (ModelEntry.select?_mem chosen) + have rows := List.all_eq_true.mp exactSlots_noPair slot listed + have valid := List.all_eq_true.mp rows row rowMem + change row.model slot environment.callLayouts method = row.model slot _ method + simp [HardwareRow.model, valid] + +#print axioms HardwareRegistered.resolveExact + +end CIL.Extraction diff --git a/CIL/Extraction/IndirectAccess.lean b/CIL/Extraction/IndirectAccess.lean new file mode 100644 index 000000000..58dfd8c64 --- /dev/null +++ b/CIL/Extraction/IndirectAccess.lean @@ -0,0 +1,60 @@ +import CIL.Extraction.ValueDecoding + +/-! +# Indirect accesses + +`Instruction.ofRaw?` decodes integer `ldind` and `stind`, and `cpblk` and `initblk` at the block +alignment `blockAlignment` reads from an `unaligned.` before them. `indirectAccessMatches` +checks the emitted instruction against the opcode, operand type and preceding instruction. It +reserves the block instructions for their two raw opcodes. Typed transfer calls retain their +method reference and use the bound runtime model. `FeatureFlowValid` and `ScalarFlowValid` apply `indirectSiteWindow` at every +reachable pc, reading the neighbouring raw instructions through an index of the body built once. +-/ + +namespace CIL.Extraction + +/-- The alignment the instruction before an access gives it: `some none` without a prefix and + `some (some n)` after `unaligned. n` for an `n` of 1, 2 or 4. `volatile.` and any other + alignment give `none`, since extraction refuses both. -/ +def accessAlignment : Option RawInstruction → Option (Option Nat) + | some ⟨_, 65042, operand⟩ => (prefixAlignment operand).map some + | some ⟨_, 65043, _⟩ => none + | _ => some none + +/-- Indirect instructions whose operand or alignment comes from their raw site. -/ +def indirectEmission : Instruction → Bool + | .ldind _ | .stind _ | .cpblk _ | .initblk _ => true + | _ => false + +/-- The indirect load and store opcodes and the block memory operations. -/ +def indirectOpcode (opcode : Nat) : Bool := + (70 ≤ opcode && opcode ≤ 87) || opcode ∈ [223, 65047, 65048] + +/-- An indirect access after `previous` emits exactly the instruction its opcode, operand type + and prefix give. Integer indirect loads and stores retain their decoded kinds. Any other + raw opcode emits none of these four instructions. Modelled calls retain their method reference. -/ +def indirectAccessMatches (previous : Option RawInstruction) (instruction : RawInstruction) + (emitted : Instruction) (skipEncoding := false) : Bool := + if instruction.opcode == 0x71 || instruction.opcode == 0x81 || + (emitted matches .ldobj _ | .stobj _) then + skipEncoding || valueEncodingMatches previous instruction emitted + else if !indirectOpcode instruction.opcode then + !indirectEmission emitted + else match accessAlignment previous, instruction.operand with + | some _, .none => skipEncoding || valueEncodingMatches previous instruction emitted + | _, _ => false + +/-- The indirect access rule at `pc`, reading the raw instructions next to it through `rawAt`: + `unaligned.` has an admitted alignment, precedes an instruction that takes it and emits + nothing; `volatile.` is refused; every other instruction meets `indirectAccessMatches` with + the instruction before it. -/ +def indirectSiteWindow (rawAt : Nat → Option RawInstruction) (pc : Nat) + (instruction : RawInstruction) (emitted : Instruction) (skipEncoding := false) + (previous := rawBefore rawAt pc) : Bool := + match instruction.opcode with + | 65042 => + (rawAt (pc + 1)).any (prefixTakes instruction) && (emitted matches .nop) + | 65043 => false + | _ => indirectAccessMatches previous instruction emitted skipEncoding + +end CIL.Extraction diff --git a/CIL/Extraction/IntegerMath.lean b/CIL/Extraction/IntegerMath.lean new file mode 100644 index 000000000..ff8826f3c --- /dev/null +++ b/CIL/Extraction/IntegerMath.lean @@ -0,0 +1,99 @@ +import CIL.Extraction.ModelTable + +/-! +# Integer math helpers + +`Math.Min`, `Max`, `Clamp`, `Abs` and `Sign` on one integer type, and the same helpers that +each primitive integer type declares, are modelled by the scalar intrinsic of that operation, width +and signedness. Each entry covers one owner and name, so a helper of another owner or assembly +has no model, and its resolve gives a model only to a static, non-generic reference whose +parameters and result are that integer type, `Sign` returning `System.Int32`. +-/ + +namespace CIL.Extraction + +/-- The integer types the helpers are modelled on, with their width in bits and signedness; the + native integers are 64 bits wide. -/ +def mathIntegers : List (String × Nat × Bool) := + [("System.SByte", 8, true), ("System.Byte", 8, false), + ("System.Int16", 16, true), ("System.UInt16", 16, false), + ("System.Int32", 32, true), ("System.UInt32", 32, false), + ("System.Int64", 64, true), ("System.UInt64", 64, false), + ("System.IntPtr", 64, true), ("System.UIntPtr", 64, false)] + +/-- The modelled helpers by metadata name, with their operation and number of parameters. -/ +def mathOperations : List (MetadataName × IntegerMath × Nat) := + [(name!"Min", .min, 2), (name!"Max", .max, 2), (name!"Clamp", .clamp, 3), + (name!"Abs", .abs, 1), (name!"Sign", .sign, 1)] + +/-- Whether `operation` is defined only on signed integers. -/ +def _root_.CIL.IntegerMath.signedOnly : IntegerMath → Bool + | .abs | .sign => true + | .min | .max | .clamp => false + +/-- The width and signedness for a helper taking `arity` parameters of one integer type, fixed + for a primitive owner. The result is that type, or `System.Int32` for `Sign`. -/ +def integerMathOperand (operation : IntegerMath) (arity : Nat) (fixed : Option TypeExpr) + (returned : TypeExpr) (parameters : List TypeExpr) : Option (Nat × Bool) := do + let parameter ← parameters.head? + if parameters != List.replicate arity parameter then none + if fixed.any (· != parameter) then none + let .atom scope name := parameter | none + if scope != runtimeScope then none + let (width, signed) ← mathIntegers.lookup name + if operation.signedOnly && !signed then none + let result := if operation == .sign then TypeExpr.atom runtimeScope "System.Int32" else parameter + if returned != result then none + some (width, signed) + +/-- The model of a call of `method` as the helper `operation` of `arity` parameters: the scalar + intrinsic at the width and signedness of the integer type it takes. -/ +def integerMathModel (operation : IntegerMath) (arity : Nat) (fixed : Option TypeExpr) + (method : MethodRef) : Option RuntimeModel := do + if method.hasThis || method.genericArity != 0 || !method.methodArguments.isEmpty then none + let (width, signed) ← + integerMathOperand operation arity fixed method.returnType method.parameters + some (.intrinsic (.scalar (.math operation width signed)) arity) + +/-- The `call` entry of the helper `name` declared by `owner`, whose parameters must be + `fixed` when given. -/ +def integerMathEntry (owner : TypeExpr) (fixed : Option TypeExpr) + (helper : MetadataName × IntegerMath × Nat) : ModelEntry := + let (name, operation, arity) := helper + .single .call owner name (.declaration fun sig => sig.convention == 0 && + (integerMathOperand operation arity fixed sig.returnType sig.parameters).isSome) + (fun _ => integerMathModel operation arity fixed) + +/-- The helpers of `System.Math`, then those each integer type declares on itself; an unsigned + type declares no `Abs` or `Sign`. -/ +def integerMathEntries : List ModelEntry := + mathOperations.map (integerMathEntry (.atom runtimeScope "System.Math") none) ++ + mathIntegers.flatMap fun (name, _, signed) => + let owner := TypeExpr.atom runtimeScope name + (mathOperations.filter fun (_, operation, _) => signed || !operation.signedOnly).map + (integerMathEntry owner (some owner)) + +/-- Every model an integer math entry resolves is an intrinsic, so a property every intrinsic + model has holds of them. -/ +theorem integerMathEntries_yields {property : RuntimeModel → Prop} + (intrinsic : ∀ operation argc, property (.intrinsic operation argc)) : + ∀ entry ∈ integerMathEntries, entry.Yields property := by + intro entry _ environment method model found + have resolved : ∃ operation arity fixed, integerMathModel operation arity fixed method = + some model := by + simp only [integerMathEntries, integerMathEntry, List.mem_append, List.mem_map, + List.mem_flatMap, List.mem_filter] at * + rename_i member + rcases member with ⟨helper, _, rfl⟩ | ⟨integer, _, helper, _, rfl⟩ <;> + exact ⟨_, _, _, found⟩ + rcases resolved with ⟨operation, arity, fixed, resolved⟩ + unfold integerMathModel at resolved + split at resolved + · contradiction + obtain ⟨⟨width, signed⟩, _, same⟩ := Option.bind_eq_some_iff.mp resolved + cases same + exact intrinsic _ _ + +#print axioms integerMathEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/KernelEquation.lean b/CIL/Extraction/KernelEquation.lean new file mode 100644 index 000000000..4a1c95bda --- /dev/null +++ b/CIL/Extraction/KernelEquation.lean @@ -0,0 +1,368 @@ +import Lean + +/-! +# Equating names read from bytes with the names an extraction states + +A name the extractor writes is a `String` literal; the checker reads the same name from heap bytes. +Comparing them in a `Bool` decision evaluates the string, about ten thousand kernel heartbeats per +character. `spelled?` turns bytes into the `String` they spell with one pass of structural +recursion, and `cil_kernel_rfl` proves an equation between closed terms by letting the kernel +compare them, which expands a string literal to its characters and compares two strings +argument by argument. Decisions over indices, ranges and flags stay `decide +kernel`; a name is +tied to the bytes by an equation `spelled? bytes = some name`. `utf8Characters_encode` proves that +the characters read encode back to exactly the bytes they were read from. +-/ + +namespace CIL.Extraction + +/-- Whether `byte` is a UTF-8 continuation byte. -/ +private def isContinuation (byte : Nat) : Bool := 0x80 ≤ byte && byte < 0xC0 + +/-- The character with the given code point, when the encoding that gave it was the shortest one + and the code point is a scalar value. A longer encoding of a smaller value is not UTF-8. -/ +private def scalar (minimum value : Nat) : Option Char := + if value < minimum || (0xD800 ≤ value && value < 0xE000) || 0x10FFFF < value then none + else some (Char.ofNat value) + +/-- The character UTF-8 bytes start with, `first` then `rest`, and the bytes after it, or `none` + when they do not start with a well-formed character. -/ +def utf8Character (first : Nat) (rest : List Nat) : Option (Char × List Nat) := + if first < 0x80 then some (Char.ofNat first, rest) + else if 0xC2 ≤ first && first < 0xE0 then + match rest with + | second :: tail => + if isContinuation second then + (·, tail) <$> scalar 0x80 ((first - 0xC0) * 0x40 + (second - 0x80)) + else none + | [] => none + else if 0xE0 ≤ first && first < 0xF0 then + match rest with + | second :: third :: tail => + if isContinuation second && isContinuation third then + (·, tail) <$> scalar 0x800 + (((first - 0xE0) * 0x40 + (second - 0x80)) * 0x40 + (third - 0x80)) + else none + | _ => none + else if 0xF0 ≤ first && first < 0xF5 then + match rest with + | second :: third :: fourth :: tail => + if isContinuation second && isContinuation third && isContinuation fourth then + (·, tail) <$> scalar 0x10000 + ((((first - 0xF0) * 0x40 + (second - 0x80)) * 0x40 + (third - 0x80)) * 0x40 + + (fourth - 0x80)) + else none + | _ => none + else none + +/-- The characters of UTF-8 bytes, or `none` when they are not well-formed UTF-8. `fuel` is the + byte count: every character takes at least one byte, so one pass reads it all. -/ +def utf8Characters : Nat → List Nat → Option (List Char) + | _, [] => some [] + | 0, _ :: _ => none + | fuel + 1, first :: rest => + match utf8Character first rest with + | some (character, tail) => (character :: ·) <$> utf8Characters fuel tail + | none => none + +/-- The UTF-8 bytes of a character, as numbers. -/ +private def encodedCharacter (character : Char) : List Nat := + (String.utf8EncodeChar character).map UInt8.toNat + +/-- A character `scalar` gives has the value it was given, within the bounds it checked. -/ +private theorem value_of_scalar {minimum value : Nat} {character : Char} + (found : scalar minimum value = some character) : + minimum ≤ value ∧ character.val.toNat = value ∧ value ≤ 0x10FFFF ∧ + (value < 0xD800 ∨ 0xE000 ≤ value) := by + unfold scalar at found + split at found + · simp at found + · rename_i bounds + simp only [Option.some.injEq] at found + subst found + simp only [Bool.or_eq_true, decide_eq_true_eq, Bool.and_eq_true, not_or, not_and, + Nat.not_lt] at bounds + have valid : value.isValidChar := by + unfold Nat.isValidChar; omega + refine ⟨by omega, ?_, by omega, by omega⟩ + simp [Char.ofNat, valid, Char.ofNatAux] + +/-- The bytes of a character `scalar` gives, by the value it was given. -/ +private theorem encoded_scalar {minimum value : Nat} {character : Char} + (found : scalar minimum value = some character) : + encodedCharacter character = + if value ≤ 0x7F then [value % 256] + else if value ≤ 0x7FF then [(value / 64 % 0x20 + 0xC0) % 256, (value % 0x40 + 0x80) % 256] + else if value ≤ 0xFFFF then + [(value / 4096 % 0x10 + 0xE0) % 256, (value / 64 % 0x40 + 0x80) % 256, + (value % 0x40 + 0x80) % 256] + else + [(value / 262144 % 0x08 + 0xF0) % 256, (value / 4096 % 0x40 + 0x80) % 256, + (value / 64 % 0x40 + 0x80) % 256, (value % 0x40 + 0x80) % 256] := by + unfold encodedCharacter String.utf8EncodeChar + rw [(value_of_scalar found).2.1] + simp only + split <;> (try split) <;> (try split) <;> simp + +/-- A character read from the front of the bytes encodes to exactly the bytes it consumed. -/ +private theorem utf8Character_encode {first : Nat} {rest tail : List Nat} {character : Char} + (decoded : utf8Character first rest = some (character, tail)) : + first :: rest = encodedCharacter character ++ tail := by + unfold utf8Character at decoded + split at decoded + · simp only [Option.some.injEq, Prod.mk.injEq] at decoded + obtain ⟨rfl, rfl⟩ := decoded + have found : scalar 0 first = some (Char.ofNat first) := by + unfold scalar; rw [ite_eq_right (by simp; omega)] + rw [encoded_scalar found, ite_eq_left (by omega)] + simp; omega + split at decoded + · rename_i lead + simp only [Bool.and_eq_true, decide_eq_true_eq] at lead + match rest, decoded with + | second :: following, decoded => + simp only at decoded + split at decoded + · rename_i continuation + simp only [isContinuation, Bool.and_eq_true, decide_eq_true_eq] at continuation + obtain ⟨decodedCharacter, found, step⟩ := Option.map_eq_some_iff.mp decoded + simp only [Prod.mk.injEq] at step + obtain ⟨sameCharacter, sameTail⟩ := step + subst sameTail sameCharacter + have bounds := value_of_scalar found + rw [encoded_scalar found, ite_eq_right (by omega), ite_eq_left (by omega)] + simp only [List.cons_append, List.nil_append, List.cons.injEq] + exact ⟨by omega, by omega, trivial⟩ + · cases decoded + split at decoded + · rename_i lead + simp only [Bool.and_eq_true, decide_eq_true_eq] at lead + match rest, decoded with + | second :: third :: following, decoded => + simp only at decoded + split at decoded + · rename_i continuation + simp only [isContinuation, Bool.and_eq_true, decide_eq_true_eq] at continuation + obtain ⟨decodedCharacter, found, step⟩ := Option.map_eq_some_iff.mp decoded + simp only [Prod.mk.injEq] at step + obtain ⟨sameCharacter, sameTail⟩ := step + subst sameTail sameCharacter + have bounds := value_of_scalar found + rw [encoded_scalar found, ite_eq_right (by omega), ite_eq_right (by omega), + ite_eq_left (by omega)] + simp only [List.cons_append, List.nil_append, List.cons.injEq] + exact ⟨by omega, by omega, by omega, trivial⟩ + · cases decoded + split at decoded + · rename_i lead + simp only [Bool.and_eq_true, decide_eq_true_eq] at lead + match rest, decoded with + | second :: third :: fourth :: following, decoded => + simp only at decoded + split at decoded + · rename_i continuation + simp only [isContinuation, Bool.and_eq_true, decide_eq_true_eq] at continuation + obtain ⟨decodedCharacter, found, step⟩ := Option.map_eq_some_iff.mp decoded + simp only [Prod.mk.injEq] at step + obtain ⟨sameCharacter, sameTail⟩ := step + subst sameTail sameCharacter + have bounds := value_of_scalar found + rw [encoded_scalar found, ite_eq_right (by omega), ite_eq_right (by omega), + ite_eq_right (by omega)] + simp only [List.cons_append, List.nil_append, List.cons.injEq] + exact ⟨by omega, by omega, by omega, by omega, trivial⟩ + · cases decoded + cases decoded + +/-- Characters read from UTF-8 bytes encode back to exactly those bytes. -/ +theorem utf8Characters_encode : ∀ (fuel : Nat) (bytes : List Nat) (characters : List Char), + utf8Characters fuel bytes = some characters → + characters.flatMap (fun character => (String.utf8EncodeChar character).map UInt8.toNat) = + bytes + | _, [], characters, decoded => by + rw [utf8Characters.eq_1] at decoded + cases decoded; rfl + | 0, _ :: _, _, decoded => by + rw [utf8Characters.eq_2] at decoded + cases decoded + | fuel + 1, first :: rest, characters, decoded => by + rw [utf8Characters.eq_3] at decoded + split at decoded + · rename_i character tail step + cases decodedTail : utf8Characters fuel tail with + | none => simp [decodedTail] at decoded + | some others => + simp only [decodedTail, Option.map_eq_map, Option.map_some, Option.some.injEq] at decoded + subst decoded + rw [List.flatMap_cons, utf8Characters_encode fuel tail others decodedTail] + exact (utf8Character_encode step).symm + · cases decoded +#print axioms utf8Characters_encode + +/-- The character a scalar value names, when the value is at least the minimum it was encoded + above. Every character is a scalar value, so only the minimum can refuse it. -/ +private theorem scalar_toNat (character : Char) {minimum : Nat} + (above : minimum ≤ character.val.toNat) : + scalar minimum character.val.toNat = some character := by + have valid := character.valid + simp only [UInt32.isValidChar, Nat.isValidChar] at valid + have inside : (character.val.toNat < minimum || (0xD800 ≤ character.val.toNat && + character.val.toNat < 0xE000) || 0x10FFFF < character.val.toNat) = false := by + simp only [Bool.or_eq_false_iff, decide_eq_false_iff_not, Bool.and_eq_false_iff]; omega + unfold scalar + simp only [inside, Bool.false_eq_true, ↓reduceIte] + exact congrArg some (Char.ofNat_toNat character) + +/-- The bytes of a character, read from the front of any bytes after them, give the character + and those bytes. -/ +private theorem utf8Character_encoded (character : Char) (rest : List Nat) : + ∃ first tail, encodedCharacter character ++ rest = first :: tail ∧ + utf8Character first tail = some (character, rest) := by + have valid := character.valid + simp only [UInt32.isValidChar, Nat.isValidChar] at valid + generalize hv : character.val.toNat = v at valid + by_cases one : v ≤ 0x7F + · have found := scalar_toNat character (minimum := 0) (Nat.zero_le _) + rw [hv] at found + rw [encoded_scalar found] + simp only [one, ↓reduceIte, List.cons_append, List.nil_append] + refine ⟨_, _, rfl, ?_⟩ + have small : v < 0x80 := by omega + have same : v % 256 = v := by omega + have : Char.ofNat v = character := by rw [← hv]; exact Char.ofNat_toNat character + simp only [utf8Character, small, ↓reduceIte, same, this] + by_cases two : v ≤ 0x7FF + · have found := scalar_toNat character (minimum := 0x80) (by omega) + rw [hv] at found + rw [encoded_scalar found] + simp only [one, two, ↓reduceIte, List.cons_append, List.nil_append] + refine ⟨_, _, rfl, ?_⟩ + have lead : ¬ (v / 64 % 0x20 + 0xC0) % 256 < 0x80 := by omega + have range : (decide (0xC2 ≤ (v / 64 % 0x20 + 0xC0) % 256) && + decide ((v / 64 % 0x20 + 0xC0) % 256 < 0xE0)) = true := by + simp only [Bool.and_eq_true, decide_eq_true_eq]; omega + have second : isContinuation ((v % 0x40 + 0x80) % 256) = true := by + simp only [isContinuation, Bool.and_eq_true, decide_eq_true_eq]; omega + have value : ((v / 64 % 0x20 + 0xC0) % 256 - 0xC0) * 0x40 + ((v % 0x40 + 0x80) % 256 - 0x80) + = v := by omega + simp only [utf8Character, lead, range, second, value, found, ↓reduceIte, Option.map_eq_map, + Option.map_some] + by_cases three : v ≤ 0xFFFF + · have found := scalar_toNat character (minimum := 0x800) (by omega) + rw [hv] at found + rw [encoded_scalar found] + simp only [one, two, three, ↓reduceIte, List.cons_append, List.nil_append] + refine ⟨_, _, rfl, ?_⟩ + have lead : ¬ (v / 4096 % 0x10 + 0xE0) % 256 < 0x80 := by omega + have notTwo : (decide (0xC2 ≤ (v / 4096 % 0x10 + 0xE0) % 256) && + decide ((v / 4096 % 0x10 + 0xE0) % 256 < 0xE0)) = false := by + simp only [Bool.and_eq_false_iff, decide_eq_false_iff_not]; omega + have range : (decide (0xE0 ≤ (v / 4096 % 0x10 + 0xE0) % 256) && + decide ((v / 4096 % 0x10 + 0xE0) % 256 < 0xF0)) = true := by + simp only [Bool.and_eq_true, decide_eq_true_eq]; omega + have rest : (isContinuation ((v / 64 % 0x40 + 0x80) % 256) && + isContinuation ((v % 0x40 + 0x80) % 256)) = true := by + simp only [isContinuation, Bool.and_eq_true, decide_eq_true_eq]; omega + have value : (((v / 4096 % 0x10 + 0xE0) % 256 - 0xE0) * 0x40 + + ((v / 64 % 0x40 + 0x80) % 256 - 0x80)) * 0x40 + ((v % 0x40 + 0x80) % 256 - 0x80) + = v := by omega + simp only [utf8Character, lead, notTwo, range, rest, value, found, ↓reduceIte, + Bool.false_eq_true, Option.map_eq_map, Option.map_some] + · have found := scalar_toNat character (minimum := 0x10000) (by omega) + rw [hv] at found + rw [encoded_scalar found] + simp only [one, two, three, ↓reduceIte, List.cons_append, List.nil_append] + refine ⟨_, _, rfl, ?_⟩ + have lead : ¬ (v / 262144 % 0x08 + 0xF0) % 256 < 0x80 := by omega + have notTwo : (decide (0xC2 ≤ (v / 262144 % 0x08 + 0xF0) % 256) && + decide ((v / 262144 % 0x08 + 0xF0) % 256 < 0xE0)) = false := by + simp only [Bool.and_eq_false_iff, decide_eq_false_iff_not]; omega + have notThree : (decide (0xE0 ≤ (v / 262144 % 0x08 + 0xF0) % 256) && + decide ((v / 262144 % 0x08 + 0xF0) % 256 < 0xF0)) = false := by + simp only [Bool.and_eq_false_iff, decide_eq_false_iff_not]; omega + have range : (decide (0xF0 ≤ (v / 262144 % 0x08 + 0xF0) % 256) && + decide ((v / 262144 % 0x08 + 0xF0) % 256 < 0xF5)) = true := by + simp only [Bool.and_eq_true, decide_eq_true_eq]; omega + have rest : (isContinuation ((v / 4096 % 0x40 + 0x80) % 256) && + isContinuation ((v / 64 % 0x40 + 0x80) % 256) && + isContinuation ((v % 0x40 + 0x80) % 256)) = true := by + simp only [isContinuation, Bool.and_eq_true, decide_eq_true_eq]; omega + have value : ((((v / 262144 % 0x08 + 0xF0) % 256 - 0xF0) * 0x40 + + ((v / 4096 % 0x40 + 0x80) % 256 - 0x80)) * 0x40 + + ((v / 64 % 0x40 + 0x80) % 256 - 0x80)) * 0x40 + ((v % 0x40 + 0x80) % 256 - 0x80) + = v := by omega + simp only [utf8Character, lead, notTwo, notThree, range, rest, value, found, ↓reduceIte, + Bool.false_eq_true, Option.map_eq_map, Option.map_some] + +/-- The encoding of characters reads back as those characters, given fuel for every byte. -/ +theorem utf8Characters_decode : ∀ (characters : List Char) (fuel : Nat), + (characters.flatMap fun character => (String.utf8EncodeChar character).map UInt8.toNat).length + ≤ fuel → + utf8Characters fuel + (characters.flatMap fun character => (String.utf8EncodeChar character).map UInt8.toNat) = + some characters + | [], fuel, _ => by cases fuel <;> rfl + | character :: others, fuel, enough => by + obtain ⟨first, tail, split, decoded⟩ := utf8Character_encoded character + (others.flatMap fun character => (String.utf8EncodeChar character).map UInt8.toNat) + have encoded : encodedCharacter character ++ + (others.flatMap fun character => (String.utf8EncodeChar character).map UInt8.toNat) = + (character :: others).flatMap + fun character => (String.utf8EncodeChar character).map UInt8.toNat := rfl + rw [← encoded, split] at enough ⊢ + have size : 0 < (encodedCharacter character).length := by + simp [encodedCharacter, Char.utf8Size_pos] + have lengths := congrArg List.length split + simp only [List.length_append, List.length_cons] at lengths enough + match fuel, enough with + | fuel + 1, enough => + rw [utf8Characters.eq_3] + simp only [decoded] + rw [utf8Characters_decode others fuel (by omega)] + rfl +#print axioms utf8Characters_decode + +/-- The string the bytes spell as UTF-8, or `none` when they are not well-formed UTF-8. The + result is a `String.ofList` of characters, which the kernel compares with a literal without + computing the literal's bytes. -/ +def spelled? (bytes : List Nat) : Option String := + (utf8Characters bytes.length bytes).map String.ofList + +open Lean Elab Tactic Meta in +/-- Prove an equation `left = right` by `Eq.refl left`, which the kernel accepts when it can + reduce both sides to the same term. Unlike `rfl`, the elaborator never unfolds either side, + so a closed computation over a large literal costs the kernel's evaluation once. The proof + is added as an auxiliary lemma first; when the kernel refuses it, the tactic fails at the + goal and prints both sides, and no proof is assigned. -/ +elab "cil_kernel_rfl" : tactic => do + let goal ← getMainGoal + let target ← instantiateMVars (← goal.getType') + let some (type, left, right) := target.eq? + | throwError "cil_kernel_rfl: the goal must be an equation, but it is{indentExpr target}" + if target.hasExprMVar then + throwError "cil_kernel_rfl: the equation has unassigned metavariables{indentExpr target}" + let proof := mkApp2 (mkConst ``Eq.refl [← getLevel type]) type left + let levelsInType := (collectLevelParams {} target).params + let lemmaLevels := (← Term.getLevelNames).reverse.filter levelsInType.contains + let lemmaName ← + try withOptions (Elab.async.set · false) do mkAuxLemma lemmaLevels target proof + catch _ => + throwError "cil_kernel_rfl: the kernel does not accept the two sides as equal\ + \nleft:{indentExpr left}\nright:{indentExpr right}" + goal.assign (mkConst lemmaName (lemmaLevels.map .param)) + replaceMainGoal [] + +open Lean Elab Command Meta in +/-- Report the axioms of a group of facts with one traversal of their shared dependencies. -/ +elab "#print " &"axioms " "[" facts:ident,+ "]" : command => withoutModifyingEnv do + let audit ← liftTermElabM do + let mut proof := mkConst ``True.intro + -- Each closed binding retains its fact as a dependency. The result type stays constant. + for fact in facts.getElems do + let name ← realizeGlobalConstNoOverloadWithInfo fact + let constant ← mkConstWithLevelParams name + proof := .letE .anonymous (← inferType constant) constant proof true + mkAuxLemma (collectLevelParams {} proof).params.toList (mkConst ``True) proof + (kind? := some `axiomReport) (cache := false) + elabCommand (← `(#print axioms $(mkCIdent audit))) + +end CIL.Extraction diff --git a/CIL/Extraction/Layout.lean b/CIL/Extraction/Layout.lean index 0b6c6698b..f2940cb0c 100644 --- a/CIL/Extraction/Layout.lean +++ b/CIL/Extraction/Layout.lean @@ -1,6 +1,9 @@ import CIL.Layout import CIL.Features import CIL.TypeExpr +import CIL.Extraction.FlowIndex +import Std.Data.TreeSet.Lemmas +import Lean.Elab.Tactic.BuiltinTactic /-! # Aggregate layouts from raw metadata @@ -9,18 +12,116 @@ The extractor emits both raw struct metadata (field types, packing, explicit off declared size) and the computed layout table its instructions use. This file recomputes CoreCLR's layout of reference-free structs from the raw metadata alone, so `LayoutsValid` binds every emitted field offset and struct size to an independent calculation. +`layoutsValidIndexed` checks distinct identities by their UTF-8 bytes and reads both tables +through indexes built once. The `Decidable` instance of `LayoutsValid` runs it. -/ namespace CIL.Extraction -/-- Storage tags retain metadata distinctions before layout computation. -/ +/-- The integer element types a struct's vector field may declare, named by their field kind. -/ +inductive VectorLane where + | unsigned8 | signed32 | unsigned32 | signed64 | unsigned64 + deriving DecidableEq, Repr + +/-- Storage tags retain metadata distinctions before layout computation: a vector field's element + type as its lane, a nested struct as its layout index. -/ inductive FieldType where | signed8 | unsigned8 | boolean | signed16 | unsigned16 | character | signed32 | unsigned32 | signed64 | unsigned64 | nativeInt | nativeUInt - | vector128 | vector256 + | vector128 (lane : VectorLane) | vector256 (lane : VectorLane) | aggregate (index : Nat) deriving DecidableEq, Repr +/-- The exact primitive runtime names and their field storage. -/ +private def primitiveFields : List (Prod String FieldType) := + [("System.SByte", .signed8), ("System.Byte", .unsigned8), ("System.Boolean", .boolean), + ("System.Int16", .signed16), ("System.UInt16", .unsigned16), ("System.Char", .character), + ("System.Int32", .signed32), ("System.UInt32", .unsigned32), ("System.Int64", .signed64), + ("System.UInt64", .unsigned64), ("System.IntPtr", .nativeInt), ("System.UIntPtr", .nativeUInt)] + +/-- Reversed UTF-8 bytes preserve every character and expose primitive-name suffixes first. -/ +private def storageNameBytes (name : String) : List UInt8 := name.toByteArray.data.toList.reverse + +/-- Byte-name equality uses structural decisions without order-package proof dependencies. -/ +private def storageBytesEqual (left right : List UInt8) : Bool := + @decide (left = right) (@List.hasDecEq UInt8 UInt8.decEq left right) + +/-- Primitive name bytes are materialized so a lookup never encodes the fixed names. -/ +private def primitiveNameBytes : List (List UInt8) := by + run_tac do + let expression <- Lean.Elab.Term.elabTerm + (<- `(primitiveFields.map fun entry => storageNameBytes entry.1)) none + Lean.Elab.Term.synthesizeSyntheticMVarsNoPostponing + let expression <- Lean.instantiateMVars expression + let values <- unsafe Lean.Meta.evalExpr (List (List UInt8)) + (<- Lean.Meta.inferType expression) expression + Lean.Elab.Tactic.closeMainGoal `primitiveNameBytes (Lean.toExpr values) + +/-- The materialized bytes spell exactly the primitive field names. -/ +private theorem primitiveNameBytes_eq : + primitiveNameBytes = primitiveFields.map (fun entry => storageNameBytes entry.1) := by + decide +kernel +#print axioms primitiveNameBytes_eq + +/-- Byte equality agrees with string equality for all metadata names. -/ +private theorem storageNameBytes_beq (left right : String) : + storageBytesEqual (storageNameBytes left) (storageNameBytes right) = (left == right) := by + apply Bool.eq_iff_iff.mpr + simp only [storageBytesEqual, storageNameBytes, decide_eq_true_eq, beq_iff_eq, + List.reverse_inj, Array.toList_inj] + constructor + . intro equal + exact String.toByteArray_inj.mp (ByteArray.ext equal) + . intro equal + cases equal + rfl +#print axioms storageNameBytes_beq + +/-- The field type of a primitive runtime type, by its exact UTF-8 name. -/ +def primitiveField (name : String) : Option FieldType := + ((primitiveNameBytes.zip (primitiveFields.map Prod.snd)).find? + fun entry => storageBytesEqual (storageNameBytes name) entry.1).map Prod.snd + +/-- info: 'CIL.Extraction.primitiveField' does not depend on any axioms -/ +#guard_msgs in +#print axioms primitiveField + +/-- Primitive byte lookup gives exactly the field selected by the runtime name. -/ +private theorem primitiveField_eq (name : String) : primitiveField name = (match name with + | "System.SByte" => some .signed8 | "System.Byte" => some .unsigned8 + | "System.Boolean" => some .boolean | "System.Int16" => some .signed16 + | "System.UInt16" => some .unsigned16 | "System.Char" => some .character + | "System.Int32" => some .signed32 | "System.UInt32" => some .unsigned32 + | "System.Int64" => some .signed64 | "System.UInt64" => some .unsigned64 + | "System.IntPtr" => some .nativeInt | "System.UIntPtr" => some .nativeUInt + | _ => none) := by + simp only [primitiveField, primitiveNameBytes_eq, primitiveFields, List.map_cons, + List.map_nil, List.zip_cons_cons, List.zip_nil_left, List.find?_cons, List.find?_nil, + storageNameBytes_beq] + repeat' (split <;> simp_all) +#print axioms primitiveField_eq + +/-- The field kind of an enum whose immediate underlying type is a runtime integer atom. -/ +def enumUnderlyingField : TypeExpr -> Option FieldType + | .atom scope name => + -- UInt64 has one of the longest admitted names. Bound the byte lookup for other names. + if scope != runtimeScope || name.utf8ByteSize > "System.UInt64".utf8ByteSize then none else + (primitiveField name).filter fun kind => match kind with + | .signed8 | .unsigned8 | .signed16 | .unsigned16 | .signed32 | .unsigned32 + | .signed64 | .unsigned64 => true + | _ => false + | _ => none + +/-- The lane of a vector whose elements have field kind `kind`; `none` for a kind no vector + field holds. -/ +def VectorLane.ofField? : FieldType → Option VectorLane + | .unsigned8 => some .unsigned8 + | .signed32 => some .signed32 + | .unsigned32 => some .unsigned32 + | .signed64 => some .signed64 + | .unsigned64 => some .unsigned64 + | _ => none + /-- One struct field as declared: its type and, for explicit layout, its byte offset. -/ structure RawField where type : FieldType @@ -63,18 +164,18 @@ structure ComputedLayout where alignment : Nat deriving DecidableEq, Repr -/-- Width, natural alignment and whether the field is a nested struct. A nested struct - must already be in `previous`, which rules out forward references. -/ -private def fieldStorage (previous : Array ComputedLayout) (architecture : Architecture) : +/-- Width, natural alignment and whether the field is a nested struct. A nested struct must be + found by `previous`, which callers limit to earlier entries to rule out forward references. -/ +private def fieldStorage (previous : Nat → Option ComputedLayout) (architecture : Architecture) : FieldType → Option (Nat × Nat × Bool) | .signed8 | .unsigned8 | .boolean => some (1, 1, false) | .signed16 | .unsigned16 | .character => some (2, 2, false) | .signed32 | .unsigned32 => some (4, 4, false) | .signed64 | .unsigned64 | .nativeInt | .nativeUInt => some (8, 8, false) - | .vector128 => some (16, 16, false) - | .vector256 => some (32, if architecture == .arm64 then 16 else 32, false) + | .vector128 _ => some (16, 16, false) + | .vector256 _ => some (32, if architecture == .arm64 then 16 else 32, false) | .aggregate index => do - let child ← previous[index]? + let child ← previous index return (child.layout.size, child.alignment, true) /-- Byte width of a primitive integer, `bool` or `char` field; vectors and structs have none. -/ @@ -89,7 +190,7 @@ def primitiveFieldWidth : FieldType → Option Nat so `sizeof` and `initobj` checks agree with field layout on every architecture. -/ theorem primitiveFieldWidth_layout {kind : FieldType} {width : Nat} (primitive : primitiveFieldWidth kind = some width) - (previous : Array ComputedLayout) (architecture : Architecture) : + (previous : Nat → Option ComputedLayout) (architecture : Architecture) : fieldStorage previous architecture kind = some (width, width, false) := by cases kind <;> simp [primitiveFieldWidth] at primitive <;> subst primitive <;> rfl @@ -100,21 +201,20 @@ private def alignUp (size alignment : Nat) : Nat := (size + alignment - 1) / alignment * alignment /-- Auto layout is admitted only where it coincides with sequential placement. -/ -private def autoSequential (previous : Array ComputedLayout) (raw : RawLayout) : Bool := +private def autoSequential (previous : Nat → Option ComputedLayout) (raw : RawLayout) : Bool := !raw.explicit && raw.packing ≤ 0 && raw.declaredSize ≤ 0 && match raw.fields.mapM (fun field => do - if field.offset.isSome || field.type == FieldType.vector128 || - field.type == FieldType.vector256 then none else + if field.offset.isSome || (field.type matches .vector128 _ | .vector256 _) then none else let (width, _, aggregate) ← fieldStorage previous .scalar field.type if aggregate then none else some width) with | some (width :: widths) => widths.all (· == width) | _ => false /-- Compute one field's placement without extending the field list. -/ -private def placeField (previous : Array ComputedLayout) (raw : RawLayout) +private def placeField (previous : Nat → Option ComputedLayout) (raw : RawLayout) (architecture : Architecture) (pack size alignment : Nat) (field : RawField) : Option (Nat × Nat × FieldLayout) := do - if field.type == .vector256 && pack > 16 && architecture == .scalar then none else do + if (field.type matches .vector256 _) && pack > 16 && architecture == .scalar then none else do let (width, natural, aggregate) ← fieldStorage previous architecture field.type let aligned := min pack natural if aligned = 0 then none else do @@ -154,8 +254,9 @@ private theorem layoutFields_eq_fold #print axioms layoutFields_eq_fold -/-- Recompute CoreCLR reference-free 64-bit layout from ordered metadata fields. -/ -def computeLayout (previous : Array ComputedLayout) (raw : RawLayout) +/-- Recompute CoreCLR reference-free 64-bit layout from ordered metadata fields; `previous` + finds the computed layout of each nested struct. -/ +def computeLayout (previous : Nat → Option ComputedLayout) (raw : RawLayout) (architecture : Architecture := .scalar) : Option ComputedLayout := do if raw.packing ∉ [-1, 0, 1, 2, 4, 8, 16, 32, 64, 128] then none else if raw.declaredSize < -1 || raw.declaredSize > 65536 then none @@ -179,13 +280,13 @@ private theorem bind_if (condition : Prop) [Decidable condition] (left right : O #print axioms bind_if /-- Extending an ordered accumulator after placement is the original field step. -/ -private theorem placeField_append (previous : Array ComputedLayout) (raw : RawLayout) +private theorem placeField_append (previous : Nat → Option ComputedLayout) (raw : RawLayout) (architecture : Architecture) (pack size alignment : Nat) (fields : List FieldLayout) (field : RawField) : (do let (size, alignment, placed) ← placeField previous raw architecture pack size alignment field return (size, alignment, fields ++ [placed])) = (do - if field.type == .vector256 && pack > 16 && architecture == .scalar then none else do + if (field.type matches .vector256 _) && pack > 16 && architecture == .scalar then none else do let (width, natural, aggregate) ← fieldStorage previous architecture field.type let aligned := min pack natural if aligned = 0 then none else do @@ -202,7 +303,7 @@ private theorem placeField_append (previous : Array ComputedLayout) (raw : RawLa #print axioms placeField_append /-- Linear field construction is exactly the former append-based layout computation. -/ -theorem computeLayout_eq_append (previous : Array ComputedLayout) (raw : RawLayout) +theorem computeLayout_eq_append (previous : Nat → Option ComputedLayout) (raw : RawLayout) (architecture : Architecture) : computeLayout previous raw architecture = do if raw.packing ∉ [-1, 0, 1, 2, 4, 8, 16, 32, 64, 128] then none else if raw.declaredSize < -1 || raw.declaredSize > 65536 then none @@ -212,7 +313,7 @@ theorem computeLayout_eq_append (previous : Array ComputedLayout) (raw : RawLayo let pack := if raw.packing ≤ 0 then 64 else raw.packing.toNat let (size, alignment, fields) ← raw.fields.foldlM (init := (0, 1, ([] : List FieldLayout))) fun (size, alignment, fields) field => do - if field.type == .vector256 && pack > 16 && architecture == .scalar then none else do + if (field.type matches .vector256 _) && pack > 16 && architecture == .scalar then none else do let (width, natural, aggregate) ← fieldStorage previous architecture field.type let aligned := min pack natural if aligned = 0 then none else do @@ -234,50 +335,162 @@ theorem computeLayout_eq_append (previous : Array ComputedLayout) (raw : RawLayo def genericLayoutIndex (layouts : List RawLayout) (type : TypeExpr) : Option Nat := layouts.findIdx? fun layout => layout.generic.any (fun generic => generic.closedType == type) -/-- Resolve specialized storage tokens after substituting owner or method parameters. -/ -def closeLayoutType (layouts : List RawLayout) : TypeExpr → TypeExpr - | .byref type => .byref (closeLayoutType layouts type) - | .pointer type => .pointer (closeLayoutType layouts type) - | .array type => .array (closeLayoutType layouts type) +/-- Reads into one raw layout table: the entry at a position, and the position of the first + closed generic layout whose owner and arguments spell a type. -/ +structure LayoutTable where + entry : Nat → Option RawLayout + owner : TypeExpr → Option Nat + +/-- The reads of a raw layout list, each scanning the list. -/ +def LayoutTable.ofList (layouts : List RawLayout) : LayoutTable := + ⟨(layouts[·]?), genericLayoutIndex layouts⟩ + +/-- Closed generic layouts in table order, each with its closed type and position. -/ +private def genericOwnersFrom : Nat → List RawLayout → List (TypeExpr × Nat) + | _, [] => [] + | position, layout :: rest => + let later := genericOwnersFrom (position + 1) rest + match layout.generic with + | some generic => (generic.closedType, position) :: later + | none => later + +/-- Closed generic layout positions in table order, bucketed by the key of their closed type. -/ +def GenericOwnerIndex := Std.TreeMap (List (List UInt8)) (List (TypeExpr × Nat)) + +/-- Bucket every closed generic layout of a table once. -/ +def GenericOwnerIndex.ofList (layouts : List RawLayout) : GenericOwnerIndex := + groupIndex (fun owner => typeKeyInto owner.1 []) (genericOwnersFrom 0 layouts) + +/-- The first position, in table order, of a closed generic layout with exactly this type. -/ +def GenericOwnerIndex.find (index : GenericOwnerIndex) (type : TypeExpr) : Option Nat := + ((index.getD (typeKeyInto type []) []).find? (·.1 == type)).map (·.2) + +/-- Closed generic owners listed from `start` are found where `genericLayoutIndex` finds them, + shifted by `start`. -/ +private theorem genericOwnersFrom_find (type : TypeExpr) (layouts : List RawLayout) + (start : Nat) : + ((genericOwnersFrom start layouts).find? (·.1 == type)).map (·.2) = + (genericLayoutIndex layouts type).map (· + start) := by + induction layouts generalizing start with + | nil => rfl + | cons layout rest ih => + unfold genericLayoutIndex at ih ⊢ + have step := ih (start + 1) + rw [List.findIdx?_cons] + cases found : layout.generic with + | none => + simp only [genericOwnersFrom, found, Option.any_none, Bool.false_eq_true, ↓reduceIte, + step, Option.map_map] + cases List.findIdx? (fun layout => layout.generic.any (·.closedType == type)) rest <;> + simp <;> omega + | some generic => + by_cases same : generic.closedType = type + · simp [genericOwnersFrom, found, same] + · have skip : (generic.closedType == type) = false := by simpa using same + simp only [genericOwnersFrom, found, Option.any_some, skip, Bool.false_eq_true, + ↓reduceIte, List.find?_cons, step, Option.map_map] + cases List.findIdx? (fun layout => layout.generic.any (·.closedType == type)) rest <;> + simp <;> omega + +#print axioms genericOwnersFrom_find + +/-- The bucketed owner lookup is exactly the first-match scan of the raw list. -/ +theorem GenericOwnerIndex.find_ofList (layouts : List RawLayout) (type : TypeExpr) : + (GenericOwnerIndex.ofList layouts).find type = genericLayoutIndex layouts type := by + have bucketed : ((genericOwnersFrom 0 layouts).filter + (fun owner => typeKeyInto owner.1 [] == typeKeyInto type [])).find? (·.1 == type) = + (genericOwnersFrom 0 layouts).find? (·.1 == type) := by + rw [List.find?_filter] + congr 1 + funext owner + by_cases same : owner.1 = type <;> simp [same] + simp only [GenericOwnerIndex.find, GenericOwnerIndex.ofList, groupIndex_get, bucketed, + genericOwnersFrom_find] + cases genericLayoutIndex layouts type <;> rfl + +#print axioms GenericOwnerIndex.find_ofList + +/-- The reads of a raw layout list through a balanced index and an owner index, each built once, + so every read costs time logarithmic in the table. -/ +def LayoutTable.indexed (layouts : List RawLayout) : LayoutTable := + ⟨(FlowIndex.ofList layouts).get?, (GenericOwnerIndex.ofList layouts).find⟩ + +/-- Indexed reads return exactly what scanning the list returns. -/ +theorem LayoutTable.indexed_eq (layouts : List RawLayout) : + LayoutTable.indexed layouts = LayoutTable.ofList layouts := by + unfold LayoutTable.indexed LayoutTable.ofList + congr 1 + · funext position + exact FlowIndex.get?_ofList layouts position + · funext type + exact GenericOwnerIndex.find_ofList layouts type + +#print axioms LayoutTable.indexed_eq + +/-- `closeLayoutTypeWith` together with whether the type is a class head or a class + application, found in the same walk, so a class application is never looked up. -/ +private def closeLayoutTypeKind (table : LayoutTable) : TypeExpr → TypeExpr × Bool + | .byref type => (.byref (closeLayoutTypeKind table type).1, false) + | .pointer type => (.pointer (closeLayoutTypeKind table type).1, false) + | .array type => (.array (closeLayoutTypeKind table type).1, false) + | .objectConstructor scope name => (.objectConstructor scope name, true) | .apply owner argument => - let type := .apply (closeLayoutType layouts owner) (closeLayoutType layouts argument) - match genericLayoutIndex layouts type with - | some index => .aggregate (layouts[index]?.map RawLayout.identity |>.getD "") index - | none => type - | type => type + let (closedOwner, classHead) := closeLayoutTypeKind table owner + let type := .apply closedOwner (closeLayoutTypeKind table argument).1 + if classHead then (type, true) else + if closedOwner.isSpanHead then (type, false) else + match table.owner type with + | some index => + (.aggregate ((table.entry index).map RawLayout.identity |>.getD "") index, false) + | none => (type, false) + | type => (type, false) + +/-- Resolve specialized storage tokens after substituting owner or method parameters. A class + application stays a class whatever layout the table lists for it. -/ +def closeLayoutTypeWith (table : LayoutTable) (type : TypeExpr) : TypeExpr := + (closeLayoutTypeKind table type).1 + +/-- `closeLayoutTypeWith` reading a raw layout list. -/ +def closeLayoutType (layouts : List RawLayout) : TypeExpr → TypeExpr := + closeLayoutTypeWith (.ofList layouts) + +/-- Closing layout indices inside a runtime span preserves its constructor. -/ +theorem closeLayoutType_span (layouts : List RawLayout) (scope name : String) (element : TypeExpr) + (span : (TypeExpr.constructor scope name).isSpanHead = true) : + closeLayoutType layouts (.apply (.constructor scope name) element) = + .apply (.constructor scope name) (closeLayoutType layouts element) := by + simp [closeLayoutType, closeLayoutTypeWith, closeLayoutTypeKind, span] +#print axioms closeLayoutType_span /-- Field storage is derived from substituted metadata, including exact aggregate identities. -/ -private def genericFieldType (layouts : List RawLayout) : TypeExpr → Option FieldType - | .enum _ underlying => genericFieldType layouts underlying - | type => - match closeLayoutType layouts type with +private def genericFieldType (table : LayoutTable) (type : TypeExpr) : Option FieldType := + match closeLayoutTypeWith table type with + | .enum _ underlying => enumUnderlyingField underlying | .aggregate identity index => - if (layouts[index]?).any (·.identity == identity) then some (.aggregate index) else none - | .atom scope name => if scope != runtimeScope then none else match name with - | "System.SByte" => some .signed8 | "System.Byte" => some .unsigned8 - | "System.Boolean" => some .boolean | "System.Char" => some .character - | "System.Int16" => some .signed16 | "System.UInt16" => some .unsigned16 - | "System.Int32" => some .signed32 | "System.UInt32" => some .unsigned32 - | "System.Int64" => some .signed64 | "System.UInt64" => some .unsigned64 - | "System.IntPtr" => some .nativeInt | "System.UIntPtr" => some .nativeUInt - | _ => none - | .apply (.constructor scope name) (.atom elementScope element) => - if scope != "System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, " ++ - "PublicKeyToken=cc7b13ffcd2ddd51" || elementScope != runtimeScope || - element ∉ ["System.Byte", "System.Int32", "System.UInt32", - "System.Int64", "System.UInt64"] then none else - if name == "System.Runtime.Intrinsics.Vector128`1" then some .vector128 - else if name == "System.Runtime.Intrinsics.Vector256`1" then some .vector256 else none + if (table.entry index).any (·.identity == identity) then some (.aggregate index) else none + | .atom scope name => if scope == runtimeScope then primitiveField name else none + | .apply (.constructor scope name) (.atom elementScope element) => do + if scope != "System.Runtime.Intrinsics, Version=11.0.0.0, Culture=neutral, " ++ + "PublicKeyToken=cc7b13ffcd2ddd51" || elementScope != runtimeScope then none + let lane ← match element with + | "System.Byte" => some VectorLane.unsigned8 | "System.Int32" => some .signed32 + | "System.UInt32" => some .unsigned32 | "System.Int64" => some .signed64 + | "System.UInt64" => some .unsigned64 | _ => none + if name == "System.Runtime.Intrinsics.Vector128`1" then some (.vector128 lane) + else if name == "System.Runtime.Intrinsics.Vector256`1" then some (.vector256 lane) else none | _ => none /-- Closed generic identities retain the scope and ordering of every supported argument. -/ def genericOwnerIdentity (owner : TypeExpr) (arguments : List TypeExpr) : Option String := do - let .constructor scope name := owner | none + let (scope, name) ← match owner with + | .constructor scope name | .objectConstructor scope name => some (scope, name) + | _ => none let names ← arguments.mapM fun argument => match argument with | .atom scope name => some (scope ++ "::" ++ name) | .enum identity _ | .aggregate identity _ => some identity - | .apply (.constructor scope name) (.atom elementScope element) => + | .apply (.constructor scope name) (.atom elementScope element) + | .apply (.objectConstructor scope name) (.atom elementScope element) => some (scope ++ "::" ++ name ++ "<" ++ elementScope ++ "::" ++ element ++ ">") | _ => none return scope ++ "::" ++ name ++ "<" ++ String.intercalate "," names ++ ">" @@ -286,7 +499,7 @@ def genericOwnerIdentity (owner : TypeExpr) (arguments : List TypeExpr) : Option def valueTypeConstraint : String := { toByteArray := ⟨#[ 83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 44, 32, - 86, 101, 114, 115, 105, 111, 110, 61, 49, 48, 46, 48, 46, 48, 46, 48, + 86, 101, 114, 115, 105, 111, 110, 61, 49, 49, 46, 48, 46, 48, 46, 48, 44, 32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, 98, 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 98, 48, 51, 102, 53, 102, 55, 102, 49, 49, 100, 53, 48, 97, @@ -303,7 +516,7 @@ theorem valueTypeConstraint_eq : def unmanagedTypeConstraint : String := { toByteArray := ⟨#[ 83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 44, 32, - 86, 101, 114, 115, 105, 111, 110, 61, 49, 48, 46, 48, 46, 48, 46, 48, + 86, 101, 114, 115, 105, 111, 110, 61, 49, 49, 46, 48, 46, 48, 46, 48, 44, 32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, 98, 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 98, 48, 51, 102, 53, 102, 55, 102, 49, 49, 100, 53, 48, 97, @@ -311,7 +524,7 @@ def unmanagedTypeConstraint : String := 109, 101, 46, 73, 110, 116, 101, 114, 111, 112, 83, 101, 114, 118, 105, 99, 101, 115, 46, 85, 110, 109, 97, 110, 97, 103, 101, 100, 84, 121, 112, 101, 33, 83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 44, - 32, 86, 101, 114, 115, 105, 111, 110, 61, 49, 48, 46, 48, 46, 48, 46, + 32, 86, 101, 114, 115, 105, 111, 110, 61, 49, 49, 46, 48, 46, 48, 46, 48, 44, 32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, 98, 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 98, 48, 51, 102, 53, 102, 55, 102, 49, 49, 100, 53, 48, @@ -326,55 +539,297 @@ theorem unmanagedTypeConstraint_eq : #print axioms unmanagedTypeConstraint_eq /-- Value-type flags and exact direct interface identities constrain a closed type argument. -/ -def genericConstraintValid (layouts : List RawLayout) (argument : TypeExpr) +def genericConstraintValidWith (table : LayoutTable) (argument : TypeExpr) (flags : Nat) (types : List String) : Bool := flags &&& 56 == flags && types.all fun type => type == valueTypeConstraint || type == unmanagedTypeConstraint || match argument with - | .aggregate identity index => match layouts[index]? with + | .aggregate identity index => match table.entry index with | some layout => layout.identity == identity && layout.interfaces.contains type | none => false | _ => false -/-- Generic layouts cannot replace their argument ordering or original field storage. -/ -def genericLayoutValid (layouts : List RawLayout) (layout : RawLayout) : Bool := +/-- `genericConstraintValidWith` reading a raw layout list. -/ +def genericConstraintValid (layouts : List RawLayout) (argument : TypeExpr) + (flags : Nat) (types : List String) : Bool := + genericConstraintValidWith (.ofList layouts) argument flags types + +/-- Generic layouts cannot replace their argument ordering or original field storage, and only + a struct's generic definition owns one, since a class instance is a reference. -/ +def genericLayoutValid (table : LayoutTable) (layout : RawLayout) : Bool := match layout.generic with | none => true | some generic => + generic.owner matches .constructor .. && genericOwnerIdentity generic.owner generic.arguments == some layout.identity && !layout.explicit && !layout.auto && generic.arity > 0 && generic.arguments.length == generic.arity && generic.constraints.length == generic.arity && (generic.constraints.zip generic.arguments).all (fun (constraint, argument) => - genericConstraintValid layouts argument constraint.1 constraint.2) && + genericConstraintValidWith table argument constraint.1 constraint.2) && generic.arguments.all (fun type => substituteType [] type == some type && - (genericFieldType layouts type).isSome) && + (genericFieldType table type).isSome) && (generic.fields.mapM fun type => do - genericFieldType layouts (← substituteType generic.arguments type)) == + genericFieldType table (← substituteType generic.arguments type)) == some (layout.fields.map RawField.type) /-- A heap-storable struct cannot contain a byref-like struct, even with no reference fields. -/ -def byRefLikeFieldsValid (raw : Array RawLayout) (layout : RawLayout) : Bool := +def byRefLikeFieldsValid (table : LayoutTable) (layout : RawLayout) : Bool := layout.byRefLike || layout.fields.all (fun field => match field.type with - | .aggregate index => (raw[index]?).any (fun child => !child.byRefLike) + | .aggregate index => (table.entry index).any (fun child => !child.byRefLike) | _ => true) /-- Reject forward references and cycles by computing strictly in metadata-table order. -/ def computeLayouts (raw : List RawLayout) (architecture : Architecture := .scalar) : Option (Array ComputedLayout) := - let indexed := raw.toArray - if !raw.all (fun layout => genericLayoutValid raw layout && byRefLikeFieldsValid indexed layout) + let table := LayoutTable.ofList raw + if !raw.all (fun layout => genericLayoutValid table layout && byRefLikeFieldsValid table layout) then none else raw.foldlM (init := #[]) fun previous entry => do - return previous.push (← computeLayout previous entry architecture) - -/-- The claimed table is checked against a separate raw metadata table. -/ + return previous.push (← computeLayout (previous[·]?) entry architecture) + +/-- Reject repeated values and values already in the ordered set. -/ +private def distinctFrom [Ord α] : Std.TreeSet α → List α → Bool + | _, [] => true + | seen, value :: rest => !seen.contains value && distinctFrom (seen.insert value) rest + +/-- Ordered membership preserves the exact list uniqueness condition. -/ +private theorem distinctFrom_correct [Ord α] [Std.TransOrd α] [Std.LawfulEqOrd α] + (seen : Std.TreeSet α) (values : List α) : + distinctFrom seen values = true ↔ values.Nodup ∧ ∀ value ∈ values, value ∉ seen := by + induction values generalizing seen with + | nil => simp [distinctFrom] + | cons value rest ih => + simp only [distinctFrom, Bool.and_eq_true, Bool.not_eq_true', Bool.eq_false_iff, + ih, List.nodup_cons, List.forall_mem_cons, + Std.TreeSet.mem_insert, Std.compare_eq_iff_eq, not_or] + constructor + · rintro ⟨fresh, distinct, remaining⟩ + refine ⟨⟨?_, distinct⟩, fresh, ?_⟩ + · intro member + exact (remaining value member).1 rfl + · intro other member + exact (remaining other member).2 + · rintro ⟨⟨fresh, distinct⟩, unseen, remaining⟩ + refine ⟨unseen, distinct, ?_⟩ + intro other member + exact ⟨fun same => fresh (same ▸ member), remaining other member⟩ + +#print axioms distinctFrom_correct + +/-- Check uniqueness using logarithmic ordered membership instead of repeated list scans. -/ +def orderedDistinct [Ord α] (values : List α) : Bool := distinctFrom ∅ values + +/-- The ordered uniqueness check accepts exactly the duplicate-free lists. -/ +theorem orderedDistinct_iff [Ord α] [Std.TransOrd α] [Std.LawfulEqOrd α] + (values : List α) : orderedDistinct values = true ↔ values.Nodup := by + simp [orderedDistinct, distinctFrom_correct] + +#print axioms orderedDistinct_iff + +/-- Distinct UTF-8 keys denote distinct layout identities. -/ +private theorem keyBytes_inj {left right : String} : + keyBytes left = keyBytes right ↔ left = right := by + simp only [keyBytes, Array.toList_inj, ← ByteArray.ext_iff, String.toByteArray_inj] + +#print axioms keyBytes_inj + +/-- Suffix-first byte keys distinguish type names before their common assembly prefix. -/ +def layoutIdentitiesDistinct (raw : List RawLayout) : Bool := + orderedDistinct (raw.map fun row => (keyBytes row.identity).reverse) + +/-- The byte-key check accepts exactly the tables with distinct identity strings. -/ +theorem layoutIdentitiesDistinct_iff (raw : List RawLayout) : + layoutIdentitiesDistinct raw = true ↔ (raw.map RawLayout.identity).Nodup := by + simp only [layoutIdentitiesDistinct, orderedDistinct_iff, List.Nodup, List.pairwise_map, + ne_eq, List.reverse_inj, keyBytes_inj] + +#print axioms layoutIdentitiesDistinct_iff + +/-- Each identity names one raw row, and its recomputed layout agrees with the claimed row. -/ def LayoutsValid (raw : List RawLayout) (claimed : Array ComputedLayout) (architecture : Architecture := .scalar) : Prop := - computeLayouts raw architecture = some claimed ∧ ∀ item ∈ claimed.toList, item.layout.Valid + (raw.map RawLayout.identity).Nodup ∧ computeLayouts raw architecture = some claimed ∧ + ∀ item ∈ claimed.toList, item.layout.Valid + +/-- Two indexed reads of the same validated type identity select the same layout row. -/ +theorem LayoutsValid.index_eq_of_identity_eq {raw : List RawLayout} + {claimed : Array ComputedLayout} {architecture : Architecture} + (valid : LayoutsValid raw claimed architecture) {first second : Nat} + {left right : RawLayout} (readLeft : raw[first]? = some left) + (readRight : raw[second]? = some right) (same : left.identity = right.identity) : + first = second := by + apply valid.1.eq_of_getElem?_eq + · simpa using (List.getElem?_eq_some_iff.mp readLeft).choose + · simp [List.getElem?_map, readLeft, readRight, same] + +#print axioms LayoutsValid.index_eq_of_identity_eq + +/-- Check raw entries against claimed entries in table order. The entry at `position` reads only + claimed layouts at earlier positions, and both lists must end together. -/ +private def layoutEntriesValid (table : LayoutTable) (architecture : Architecture) + (claimed : Nat → Option ComputedLayout) : Nat → List RawLayout → List ComputedLayout → Bool + | position, entry :: rest, item :: items => + (genericLayoutValid table entry && byRefLikeFieldsValid table entry && + decide (computeLayout (fun child => if child < position then claimed child else none) + entry architecture = some item) && + decide item.layout.Valid) && + layoutEntriesValid table architecture claimed (position + 1) rest items + | _, [], [] => true + | _, _, _ => false + +/-- Read raw entries through `LayoutTable.indexed` and + claimed layouts through one balanced index, so `L` layouts with `A` fields in total cost + `O((L + A) log L)` lookups. A generic layout also looks up the closed owner of each `.apply` + node of its argument and original field types by that node's type key, so a type of size `s` + and depth `d` adds `O(s * d * log L)`. -/ +def layoutsValidIndexed (raw : List RawLayout) (claimed : Array ComputedLayout) + (architecture : Architecture) : Bool := + layoutIdentitiesDistinct raw && + layoutEntriesValid (.indexed raw) architecture (FlowIndex.ofList claimed.toList).get? 0 raw + claimed.toList + +/-- Each step of the layout fold appends one computed layout to its accumulator. -/ +private theorem layoutFold_extends (architecture : Architecture) (raw : List RawLayout) + (done result : Array ComputedLayout) + (folded : raw.foldlM (init := done) (fun previous entry => do + return previous.push (← computeLayout (previous[·]?) entry architecture)) = some result) : + ∃ added : List ComputedLayout, + result.toList = done.toList ++ added ∧ added.length = raw.length := by + induction raw generalizing done with + | nil => + simp only [List.foldlM_nil, Option.pure_def, Option.some.injEq] at folded + exact ⟨[], by simp [folded], rfl⟩ + | cons entry rest ih => + simp only [List.foldlM_cons] at folded + cases computed : computeLayout (done[·]?) entry architecture with + | none => simp [computed] at folded + | some layout => + simp only [computed, Option.bind_eq_bind, Option.bind_some, Option.pure_def] at folded + obtain ⟨added, extended, length⟩ := ih (done.push layout) folded + exact ⟨layout :: added, by simp [extended], by simp [length]⟩ + +#print axioms layoutFold_extends + +/-- Reading the claimed table below the accumulator's size reads the accumulator. -/ +private theorem claimed_prefix_lookup (claimed done : Array ComputedLayout) + (remaining : List ComputedLayout) (split : done.toList ++ remaining = claimed.toList) : + (fun child => if child < done.size then claimed.toList[child]? else none) = + (done[·]?) := by + funext child + by_cases below : child < done.size + · simp only [below, ↓reduceIte] + rw [← split, List.getElem?_append_left (by simpa using below), Array.getElem?_toList] + · simp only [below, ↓reduceIte] + rw [Array.getElem?_eq_none (by omega)] + +#print axioms claimed_prefix_lookup + +/-- From any accumulator that is a prefix of the claimed table, the indexed pass accepts exactly + when the raw-table rules and claimed well-formedness hold for the rest and the fold reaches + the claimed table. -/ +private theorem layoutEntriesValid_iff (table : LayoutTable) (architecture : Architecture) + (claimed : Array ComputedLayout) (raw : List RawLayout) (done : Array ComputedLayout) + (remaining : List ComputedLayout) (split : done.toList ++ remaining = claimed.toList) : + layoutEntriesValid table architecture (claimed.toList[·]?) done.size raw remaining = true ↔ + (∀ entry ∈ raw, genericLayoutValid table entry = true ∧ + byRefLikeFieldsValid table entry = true) ∧ + (∀ item ∈ remaining, item.layout.Valid) ∧ + raw.foldlM (init := done) (fun previous entry => do + return previous.push (← computeLayout (previous[·]?) entry architecture)) = + some claimed := by + induction raw generalizing done remaining with + | nil => + cases remaining with + | nil => + have same : done = claimed := Array.toList_inj.mp (by simpa using split) + simp [layoutEntriesValid, same] + | cons item items => + simp only [layoutEntriesValid, Bool.false_eq_true, false_iff, List.foldlM_nil, + Option.pure_def, Option.some.injEq, not_and] + intro _ _ same + subst same + simp at split + | cons entry rest ih => + cases remaining with + | nil => + simp only [layoutEntriesValid, Bool.false_eq_true, false_iff, not_and] + intro _ _ folded + obtain ⟨added, extended, length⟩ := layoutFold_extends architecture _ done claimed folded + rw [← split, List.append_nil] at extended + have empty : added = [] := by simpa using extended + simp [empty] at length + | cons item items => + simp only [layoutEntriesValid, claimed_prefix_lookup claimed done (item :: items) split, + List.foldlM_cons, List.forall_mem_cons, Bool.and_eq_true, decide_eq_true_eq] + cases computed : computeLayout (done[·]?) entry architecture with + | none => simp + | some layout => + simp only [Option.some.injEq, Option.bind_eq_bind, Option.bind_some, Option.pure_def] + by_cases same : layout = item + · subst same + have pushed : (done.push layout).toList ++ items = claimed.toList := by simp [← split] + have next := ih (done.push layout) items pushed + rw [Array.size_push] at next + rw [next] + constructor + · rintro ⟨⟨⟨⟨generic, byRefLike⟩, -⟩, valid⟩, rest, items, folded⟩ + exact ⟨⟨⟨generic, byRefLike⟩, rest⟩, ⟨valid, items⟩, folded⟩ + · rintro ⟨⟨⟨generic, byRefLike⟩, rest⟩, ⟨valid, items⟩, folded⟩ + exact ⟨⟨⟨⟨generic, byRefLike⟩, rfl⟩, valid⟩, rest, items, folded⟩ + · simp only [same, and_false, false_and, false_iff, not_and] + intro _ _ folded + obtain ⟨added, extended, -⟩ := layoutFold_extends architecture _ _ claimed folded + rw [← split] at extended + simp at extended + exact same extended.1.symm + +#print axioms layoutEntriesValid_iff + +/-- The indexed checks are equivalent to identity uniqueness, recomputation and field bounds. -/ +theorem layoutsValidIndexed_iff (raw : List RawLayout) (claimed : Array ComputedLayout) + (architecture : Architecture) : + layoutsValidIndexed raw claimed architecture = true ↔ + LayoutsValid raw claimed architecture := by + have lookup : (FlowIndex.ofList claimed.toList).get? = (claimed.toList[·]?) := + funext (FlowIndex.get?_ofList claimed.toList) + have entries := layoutEntriesValid_iff (.ofList raw) architecture claimed raw #[] + claimed.toList (by simp) + rw [Array.size_empty] at entries + unfold layoutsValidIndexed LayoutsValid computeLayouts + rw [Bool.and_eq_true, layoutIdentitiesDistinct_iff, LayoutTable.indexed_eq, lookup, entries] + apply and_congr_right + intro _ + by_cases checks : (raw.all fun layout => genericLayoutValid (.ofList raw) layout && + byRefLikeFieldsValid (.ofList raw) layout) = true + · have holds := checks + simp only [List.all_eq_true, Bool.and_eq_true] at holds + simp only [checks, Bool.not_true, Bool.false_eq_true, ↓reduceIte] + exact ⟨fun ⟨_, valid, folded⟩ => ⟨folded, valid⟩, + fun ⟨folded, valid⟩ => ⟨holds, valid, folded⟩⟩ + · have fails : ¬ ∀ entry ∈ raw, genericLayoutValid (.ofList raw) entry = true ∧ + byRefLikeFieldsValid (.ofList raw) entry = true := by + simpa [List.all_eq_true] using checks + simp only [Bool.not_eq_true] at checks + simp only [checks, Bool.not_false, ↓reduceIte, reduceCtorEq, false_and, iff_false] + exact fun ⟨holds, _⟩ => fails holds + +/-- +info: 'CIL.Extraction.layoutsValidIndexed_iff' depends on axioms: +[propext, Classical.choice, Quot.sound] +-/ +#guard_msgs (whitespace := lax) in #print axioms layoutsValidIndexed_iff + +/-- Every decision of `LayoutsValid`, in the extraction header, per method, for import catalogs + and for layout projections, runs the one-pass indexed check. -/ +instance layoutsValidDecidable (raw : List RawLayout) (claimed : Array ComputedLayout) + (architecture : Architecture) : Decidable (LayoutsValid raw claimed architecture) := + decidable_of_iff _ (layoutsValidIndexed_iff raw claimed architecture) -instance (raw : List RawLayout) (claimed : Array ComputedLayout) (architecture : Architecture) : - Decidable (LayoutsValid raw claimed architecture) := - inferInstanceAs (Decidable (_ ∧ _)) +/-- +info: 'CIL.Extraction.layoutsValidDecidable' depends on axioms: +[propext, Classical.choice, Quot.sound] +-/ +#guard_msgs (whitespace := lax) in #print axioms layoutsValidDecidable /-- `LayoutsValid` as a Boolean, for checking by evaluation. -/ def validateLayouts (raw : List RawLayout) (claimed : Array ComputedLayout) @@ -393,6 +848,6 @@ theorem validated_layout_within_bounds {raw : List RawLayout} {claimed : Array C (checked : LayoutsValid raw claimed architecture) (item : ComputedLayout) (member : item ∈ claimed.toList) (field : FieldLayout) (present : field ∈ item.layout.fields) : field.offset + field.width ≤ item.layout.size := - field_within_layout item.layout (checked.2 item member) field present + field_within_layout item.layout (checked.2.2 item member) field present end CIL.Extraction diff --git a/CIL/Extraction/LayoutCoverage.lean b/CIL/Extraction/LayoutCoverage.lean index 466ae1336..5332a805a 100644 --- a/CIL/Extraction/LayoutCoverage.lean +++ b/CIL/Extraction/LayoutCoverage.lean @@ -4,10 +4,9 @@ import CIL.Extraction.Generics # Layout coverage `MethodLayoutsValid` checks only the layout uses it is given. `LayoutUsesComplete` closes -that gap: it derives every layout-dependent site from the raw body (field accesses, struct -loads, stores, copies, initializations, sizes, constructions and Unsafe element offsets), plus every -struct-typed local and homed parameter, and requires the extractor's list to be exactly -that list. +that gap by deriving the rows for model calls, constructions, struct-typed locals and homed +parameters. Typed object instructions resolve their operands +through the method's checked layout environment. -/ namespace CIL.Extraction @@ -22,91 +21,36 @@ private def aggregateIndex : TypeExpr → Option Nat def instructionLayoutUses (layouts : List RawLayout) (pc : Nat) (instruction : RawInstruction) : Option (List LayoutUse) := do match instruction.opcode, instruction.operand with - -- Class fields use model offsets that `scalarEmissionMatches` checks without a layout table; - -- a field holding an array of structs takes its element width from the struct's layout. - | 0x7b, .field (.object _ _) _ type false | 0x7d, .field (.object _ _) _ type false => - match type with - | .array (.aggregate identity index) => - if typeStorage layouts (.aggregate identity index) != some (.field (.aggregate index)) then - none else return [.size pc index .arrayElement] - | _ => return [] - -- `ldfld`, `ldflda` and `stfld` on an instance field, whose declared type must match. - | 0x7b, .field owner index type false - | 0x7c, .field owner index type false - | 0x7d, .field owner index type false => + -- A class field access carries no layout data: its step resolves the slot, and the size of a + -- struct array element, from the checked layouts. + | 0x7b, .field (.object _ _) _ _ _ false | 0x7c, .field (.object _ _) _ _ _ false + | 0x7d, .field (.object _ _) _ _ _ false => return [] + -- `ldfld`, `ldflda` and `stfld` on a struct instance field, whose declared type must match. + -- The step reads the offset and width from the checked layouts, so the site needs no use. + | 0x7b, .field owner index type _ false + | 0x7c, .field owner index type _ false + | 0x7d, .field owner index type _ false => let .field (.aggregate parent) ← typeStorage layouts owner | none let metadata ← layouts[parent]? let field ← metadata.fields[index]? - let storage := match field.type with - | .vector128 => StorageType.vector128 - | .vector256 => StorageType.vector256 - | kind => .field kind - if typeStorage layouts type != some storage then none else - return [.field pc parent index (if instruction.opcode = 0x7b then .load - else if instruction.opcode = 0x7c then .address else .store)] - -- `ldobj`, `stobj`, `initobj`, `cpobj` and `sizeof` of a struct. - | 0xfe15, .type (.apply (.constructor scope owner) (.aggregate identity index)) => - if scope == runtimeScope && owner ∈ ["System.Span`1", "System.ReadOnlySpan`1"] then - if typeStorage layouts (.aggregate identity index) != some (.field (.aggregate index)) then - none else return [.size pc index .spanElement] - else return [] - | opcode, .type type => + if fieldKind? layouts type != some field.type then none else return [] + | _, .type type => match aggregateIndex type with | none => return [] | some index => if typeStorage layouts type ≠ some (.field (.aggregate index)) then none - else if opcode = 0x71 then return [.size pc index .load] - else if opcode = 0x81 then return [.size pc index .store] - else if opcode = 0xfe15 then return [.size pc index .initialize] - else if opcode = 0x70 then return [.size pc index .copy] - else if opcode = 0xfe1c then return [.size pc index .sizeOf] - else if opcode ∈ [0x8d, 0x8f, 0xa3, 0xa4] then return [.size pc index .arrayElement] else return [] - -- `newobj` of a struct, then `Unsafe.SizeOf` and `Unsafe.Add`. + -- `newobj` of a struct, then `Unsafe.Add`. | 0x73, .call site => - if let .apply (.constructor scope owner) (.aggregate _ index) := site.declaringType then - if scope == runtimeScope && owner ∈ ["System.Span`1", "System.ReadOnlySpan`1"] && - site.name == ".ctor" then - let access := match site.parameterTypes.head? with - | some (.array _) => SizeAccess.arrayElement | _ => .spanElement - return [.size pc index access] return (aggregateIndex (closeLayoutType layouts site.declaringType)).toList.map - fun index => .size pc index .construct + fun index => .size pc index | 0x28, .call site => - if site.name ∈ ["AsBytes", "Cast"] && - site.declaringType == .atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal" then - return ((site.typeArguments[0]?).bind aggregateIndex).toList.map - (fun index => .size pc index .spanSource) ++ - ((site.typeArguments[1]?).bind aggregateIndex).toList.map - (fun index => .size pc index .spanTarget) match site.declaringType, site.typeArguments with | .apply (.constructor scope owner) (.aggregate identity index), [] => - if site.name == "get_Empty" && + let spanOwner := scope == runtimeScope && owner ∈ ["System.Span`1", "System.ReadOnlySpan`1"] + if !spanOwner && site.name == "get_Empty" && typeStorage layouts (.aggregate identity index) != some (.field (.aggregate index)) then - none else - if scope == runtimeScope && owner ∈ ["System.Span`1", "System.ReadOnlySpan`1"] && - site.name ∈ [".ctor", "op_Implicit"] && - (site.parameterTypes.head?.map fun type => match type with - | .array _ => true | _ => false).getD false then - return [.size pc index .arrayElement] - else if scope == runtimeScope && owner ∈ ["System.Span`1", "System.ReadOnlySpan`1"] && - site.name ∈ ["get_Item", "CopyTo", "TryCopyTo", "Clear", "Fill", ".ctor", "get_Empty"] then - return [.size pc index .spanElement] else return [] - | .atom _ "System.MemoryExtensions", [.aggregate _ index] => - if site.name == "AsSpan" then return [.size pc index .arrayElement] else return [] - | .atom _ "System.Runtime.InteropServices.MemoryMarshal", [.aggregate _ index] => - if site.name = "Read" || site.name = "TryRead" then return [.size pc index .load] - else if site.name = "Write" || site.name = "TryWrite" then return [.size pc index .store] - else if site.name ∈ ["CreateSpan", "CreateReadOnlySpan"] then - return [.size pc index .spanElement] - else return [] - | .atom _ "System.Runtime.CompilerServices.Unsafe", [.aggregate _ index] => - if site.name = "SizeOf" then return [.size pc index .sizeOf] - else if site.name = "Add" then return [.size pc index .referenceAdd] - else if site.name = "Subtract" then return [.size pc index .referenceSubtract] - else if site.name ∈ ["ReadUnaligned", "Read"] then return [.size pc index .load] - else if site.name ∈ ["WriteUnaligned", "Write"] then return [.size pc index .store] - else return [] + none else return [] | _, _ => return [] | _, _ => return [] diff --git a/CIL/Extraction/LayoutProjection.lean b/CIL/Extraction/LayoutProjection.lean index 695c34876..e405bfca4 100644 --- a/CIL/Extraction/LayoutProjection.lean +++ b/CIL/Extraction/LayoutProjection.lean @@ -1,6 +1,5 @@ import CIL.Extraction.Layout import CIL.Extraction.FlowIndex -import Std.Data.TreeSet.Lemmas /-! # Public layout projection @@ -13,45 +12,6 @@ metadata lists, and ordered sets check identity and index uniqueness. namespace CIL.Extraction -/-- Reject repeated values and values already in the ordered set. -/ -private def distinctFrom [Ord α] : Std.TreeSet α → List α → Bool - | _, [] => true - | seen, value :: rest => !seen.contains value && distinctFrom (seen.insert value) rest - -/-- Ordered membership preserves the exact list uniqueness condition. -/ -private theorem distinctFrom_correct [Ord α] [Std.TransOrd α] [Std.LawfulEqOrd α] - (seen : Std.TreeSet α) (values : List α) : - distinctFrom seen values = true ↔ values.Nodup ∧ ∀ value ∈ values, value ∉ seen := by - induction values generalizing seen with - | nil => simp [distinctFrom] - | cons value rest ih => - simp only [distinctFrom, Bool.and_eq_true, Bool.not_eq_true', Bool.eq_false_iff, - ih, List.nodup_cons, List.forall_mem_cons, - Std.TreeSet.mem_insert, Std.compare_eq_iff_eq, not_or] - constructor - · rintro ⟨fresh, distinct, remaining⟩ - refine ⟨⟨?_, distinct⟩, fresh, ?_⟩ - · intro member - exact (remaining value member).1 rfl - · intro other member - exact (remaining other member).2 - · rintro ⟨⟨fresh, distinct⟩, unseen, remaining⟩ - refine ⟨unseen, distinct, ?_⟩ - intro other member - exact ⟨fun same => fresh (same ▸ member), remaining other member⟩ - -#print axioms distinctFrom_correct - -/-- Check uniqueness using logarithmic ordered membership instead of repeated list scans. -/ -def projectionDistinct [Ord α] (values : List α) : Bool := distinctFrom ∅ values - -/-- The ordered uniqueness check accepts exactly the duplicate-free lists. -/ -theorem projectionDistinct_iff [Ord α] [Std.TransOrd α] [Std.LawfulEqOrd α] - (values : List α) : projectionDistinct values = true ↔ values.Nodup := by - simp [projectionDistinct, distinctFrom_correct] - -#print axioms projectionDistinct_iff - /-- Maps a nested-struct field's public index to its artifact index. -/ private def expandPublicField (lookup : Nat → Option Nat) (field : RawField) : Option RawField := do @@ -97,9 +57,7 @@ def LayoutProjectionValid (artifact : List RawLayout) (computed : Array Computed (indices : List Nat) (architecture : Architecture) : Prop := LayoutsValid artifact computed architecture ∧ LayoutsValid published publicComputed architecture ∧ - projectionDistinct (artifact.map RawLayout.identity) = true ∧ - projectionDistinct (published.map RawLayout.identity) = true ∧ - indices.length = published.length ∧ projectionDistinct indices = true ∧ + indices.length = published.length ∧ orderedDistinct indices = true ∧ projectionMetadataMatches artifact published indices ∧ indices.mapM (fun index => computed[index]?) = some publicComputed.toList @@ -108,7 +66,7 @@ instance (artifact : List RawLayout) (computed : Array ComputedLayout) (indices : List Nat) (architecture : Architecture) : Decidable (LayoutProjectionValid artifact computed published publicComputed indices architecture) := - inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _)) + inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _)) /-- The indexed validator accepts exactly the original list-based projection conditions. -/ theorem LayoutProjectionValid_iff_list (artifact : List RawLayout) @@ -122,7 +80,12 @@ theorem LayoutProjectionValid_iff_list (artifact : List RawLayout) published.mapM (expandPublicLayout indices) = indices.mapM (fun index => artifact[index]?) ∧ indices.mapM (fun index => computed[index]?) = some publicComputed.toList := by - simp only [LayoutProjectionValid, projectionDistinct_iff, projectionMetadataMatches_iff] + simp only [LayoutProjectionValid, orderedDistinct_iff, projectionMetadataMatches_iff] + constructor + · rintro ⟨source, target, rest⟩ + exact ⟨source, target, source.1, target.1, rest⟩ + · rintro ⟨source, target, _, _, rest⟩ + exact ⟨source, target, rest⟩ #print axioms LayoutProjectionValid_iff_list diff --git a/CIL/Extraction/LayoutUses.lean b/CIL/Extraction/LayoutUses.lean index 59e29a1c3..f3f505ec9 100644 --- a/CIL/Extraction/LayoutUses.lean +++ b/CIL/Extraction/LayoutUses.lean @@ -4,26 +4,14 @@ import CIL.Instructions /-! # Layout uses -Emitted instructions carry concrete field offsets, widths and struct sizes. A `LayoutUse` -names one such site by raw type and field identity only; `MethodLayoutsValid` then checks -each named site's emitted operands against the independently computed layout table. -`LayoutCoverage` separately proves that the list of uses names every such site. +A `LayoutUse` names a struct construction, local or parameter home. `MethodLayoutsValid` +checks its size against the computed layout for the method's architecture. Instance field +instructions carry a typed field reference and read their offset and width from that layout. +`LayoutUsesComplete` derives the required uses from the raw method. -/ namespace CIL.Extraction -/-- How an instruction uses a struct field. -/ -inductive FieldAccess where - | load | store | address - deriving DecidableEq, Repr - -/-- How an instruction uses a struct's size, including scaled reference addition/subtraction. -/ -inductive SizeAccess where - | load | store | initialize | copy | sizeOf | construct | referenceAdd | referenceSubtract - | spanElement | spanSource | spanTarget - | arrayElement - deriving DecidableEq, Repr - /-- The native-width negative stride used to subtract an element offset. -/ def subtractionStride (width : Nat) : Nat := (-(BitVec.ofNat 64 width)).toNat @@ -33,79 +21,29 @@ theorem subtractionStride_mul (width : Nat) (offset : BitVec 64) : simp only [subtractionStride, BitVec.ofNat_toNat, BitVec.setWidth_eq, BitVec.neg_mul] #print axioms subtractionStride_mul -/-- Raw type/field identities at layout-dependent uses; no computed offsets or sizes. -/ +/-- Whether a load of a field of this kind sign-extends it to the evaluation stack: only the + narrow signed integers do. -/ +def signedField (kind : FieldType) : Bool := kind == .signed8 || kind == .signed16 + +/-- The site and raw layout index of a struct construction or frame slot. -/ inductive LayoutUse where - | field (pc type field : Nat) (access : FieldAccess) - | size (pc type : Nat) (access : SizeAccess) + /-- A `newobj` of the struct, whose link states the size it allocates. -/ + | size (pc type : Nat) | local (slot type : Nat) | parameter (slot type : Nat) deriving DecidableEq, Repr -/-- The emitted field operation uses exactly the computed offset and width, and the - aggregate form exactly when the field is a nested struct. -/ -private def checkField (layout : FieldLayout) (signed : Bool) : FieldAccess → Op → Bool - | .address, .fieldAddress offset => offset == layout.offset - | .load, .fieldAt offset width actualSigned => - !layout.aggregate && offset == layout.offset && width == layout.width && - actualSigned == signed - | .store, .setFieldAt offset width => - !layout.aggregate && offset == layout.offset && width == layout.width - | .load, .aggregateFieldAt offset width => - layout.aggregate && offset == layout.offset && width == layout.width - | .store, .setAggregateFieldAt offset width => - layout.aggregate && offset == layout.offset && width == layout.width - | _, _ => false - -/-- The emitted size-dependent operation uses exactly the computed struct size. -/ -private def checkSize (size alignment : Nat) : SizeAccess → Op → Bool - | .load, .memory (.pointerTransfer (.snapshot (.aggregate width)) actual false) - | .store, .memory (.pointerTransfer (.snapshot (.aggregate width)) actual true) => - width == size && actual == alignment - | .arrayElement, .newArray (.aggregate _ width) - | .arrayElement, .memory (.arrayAddress (.aggregate _ width) _) - | .arrayElement, .memory (.arrayTransfer (.aggregate _ width) _) - | .arrayElement, .memory (.arraySpan (.aggregate _ width) _ _) - | .arrayElement, .memory (.arrayField _ _ (.aggregate _ width)) - | .arrayElement, .memory (.setArrayField _ _ (.aggregate _ width)) - | .arrayElement, .initializeSpanLocal _ (.array (.aggregate _ width) _ _) => width == size - | .load, .memory (.marshalRead (.snapshot (.aggregate width)) _) - | .store, .memory (.marshalWrite (.snapshot (.aggregate width)) _) => width == size - | .load, .loadAggregate width | .store, .storeAggregate width - | .initialize, .initAggregate width | .copy, .copyObject width => width == size - | .sizeOf, .const32 bits => bits.toNat == size - | .construct, .newAggregate _ _ width => width.val == size - | .spanElement, .memory (.spanEmpty width _) - | .spanElement, .initializeSpanLocal _ (.empty width _) - | .spanElement, .memory (.spanItem width) - | .spanElement, .memory (.spanCopy width _) - | .spanElement, .memory (.spanFill width _) - | .spanElement, .memory (.spanFromReference width _) - | .spanElement, .memory (.spanSingleReference width _) - | .spanElement, .initializeSpanLocal _ (.reference width _) - | .spanElement, .initializeSpanLocal _ (.singleReference width _) => width == size - | .spanSource, .memory (.spanReinterpret width _ _) => width == size - | .spanTarget, .memory (.spanReinterpret _ width _) => width == size - | .referenceAdd, .memory (.add width _) => width == size - | .referenceSubtract, .memory (.add stride _) => stride == subtractionStride size - | _, _ => false - /-- Bind independently computed layout data to actual instructions and frame slots. -/ def validateLayoutUse (raw : List RawLayout) (layouts : Array ComputedLayout) (method : Method) : LayoutUse → Bool - | .field pc type index access => (do - let metadata ← raw[type]? - let field ← metadata.fields[index]? - let layout ← layouts[type]? - let computed ← layout.layout.fields[index]? - let operation ← method.code[pc]? - let signed := field.type == FieldType.signed8 || field.type == FieldType.signed16 - return checkField computed signed access operation).getD false - | .size pc type access => (do - let metadata ← raw[type]? - if access = SizeAccess.arrayElement ∧ metadata.byRefLike = true then none else pure () + | .size pc type => (do + let _ ← raw[type]? let layout ← layouts[type]? let operation ← method.code[pc]? - return checkSize layout.layout.size layout.alignment access operation).getD false + -- A construction allocates the size its link states; its operand states none. + return operation.constructs && match method.linkAt pc with + | some (.aggregate _ width) => width.val == layout.layout.size + | _ => false).getD false | .local slot type => (do let layout ← layouts[type]? match ← method.localKinds[slot]? with diff --git a/CIL/Extraction/LinkIndex.lean b/CIL/Extraction/LinkIndex.lean new file mode 100644 index 000000000..b476518a2 --- /dev/null +++ b/CIL/Extraction/LinkIndex.lean @@ -0,0 +1,341 @@ +import CIL.Extraction.FlowIndex +import CIL.Instructions + +/-! +# Link lookups + +Two forms keep a read of a method's link table from costing the table's length. For a table +whose keys ascend, the entry at a position is the first for its key (`linkAt_of_position`), so +`cil_instruction_equations` proves each call site's link from its entry, read through named +suffixes of the table, after checking once that the keys ascend. An embedding reads the link of +every live instruction on each side; when the table keys ascend, which holds for every extracted +table, a walk over the table answers all queries. The walk +advances while the queried pcs do not decrease and starts again from the head of the table at +each pc below the one before, so a map that reorders a few blocks costs a few passes. A +hand-written table without ascending keys takes the scan, which costs the table's length per +query. The extraction call check reads each pc's link by the walk, and `linkIndex` indexes the +walk once for checks that ask a pc's link out of order. +-/ + +namespace CIL.Extraction + +/-- True when the keys increase strictly and none is below `lower`. -/ +def keysAscend {α : Type} : Nat → List (Nat × α) → Bool + | _, [] => true + | lower, (key, _) :: rest => decide (lower ≤ key) && keysAscend (key + 1) rest + +/-- Ascending keys are all at least the lower bound. -/ +theorem keysAscend_bound {α : Type} (entries : List (Nat × α)) (lower : Nat) + (ascending : keysAscend lower entries = true) : ∀ entry ∈ entries, lower ≤ entry.1 := by + induction entries generalizing lower with + | nil => simp + | cons head rest ih => + simp only [keysAscend, Bool.and_eq_true, decide_eq_true_eq] at ascending + intro entry member + rcases List.mem_cons.mp member with rfl | member + · exact ascending.1 + · have := ih (head.1 + 1) ascending.2 entry member + omega + +#print axioms keysAscend_bound + +/-- A table ascending from `lower` ascends from any bound its first key reaches. -/ +theorem keysAscend_raise {α : Type} {entries : List (Nat × α)} {lower higher : Nat} + (ascending : keysAscend lower entries = true) + (raised : (entries.head?.all fun entry => decide (higher ≤ entry.1)) = true) : + keysAscend higher entries = true := by + cases entries with + | nil => rfl + | cons head rest => + obtain ⟨key, value⟩ := head + simp only [keysAscend, Bool.and_eq_true, decide_eq_true_eq] at ascending ⊢ + simp only [List.head?_cons, Option.all_some, decide_eq_true_eq] at raised + exact ⟨raised, ascending.2⟩ + +#print axioms keysAscend_raise + +/-- The entry at a position of an ascending table is the first entry for its key. -/ +theorem find_entry_of_ascending (links : List (Nat × Link)) (lower : Nat) + (ascending : keysAscend lower links = true) (position : Nat) (entry : Nat × Link) + (found : links[position]? = some entry) : + links.find? (fun candidate => candidate.1 == entry.1) = some entry := by + induction links generalizing lower position with + | nil => simp at found + | cons head rest ih => + obtain ⟨key, link⟩ := head + simp only [keysAscend, Bool.and_eq_true, decide_eq_true_eq] at ascending + cases position with + | zero => + simp only [List.getElem?_cons_zero, Option.some.injEq] at found + subst found + exact List.find?_cons_of_pos (Nat.beq_refl key) + | succ position => + simp only [List.getElem?_cons_succ] at found + have above := keysAscend_bound rest (key + 1) ascending.2 entry + (List.mem_of_getElem? found) + have different : ¬ (key == entry.1) = true := by + simp only [beq_iff_eq] + omega + have skip : (((key, link) :: rest).find? fun candidate => candidate.1 == entry.1) = + rest.find? fun candidate => candidate.1 == entry.1 := List.find?_cons_of_neg different + rw [skip] + exact ih (key + 1) ascending.2 position found + +#print axioms find_entry_of_ascending + +/-- An entry of a table whose keys ascend is the link the method gives its pc. -/ +theorem linkAt_of_position {body : Method} (ascending : keysAscend 0 body.links = true) + {position pc : Nat} {link : Link} (found : body.links[position]? = some (pc, link)) : + body.linkAt pc = some link := by + have first : body.links.find? (fun entry => entry.1 == pc) = some (pc, link) := + find_entry_of_ascending body.links 0 ascending position (pc, link) found + simp only [Method.linkAt, linkLookup] + rw [first] + rfl + +#print axioms linkAt_of_position + +/-- The entries from the first whose key is at least `query`. -/ +def dropBelow (query : Nat) : List (Nat × Link) → List (Nat × Link) + | [] => [] + | (key, link) :: rest => if key < query then dropBelow query rest else (key, link) :: rest + +/-- The link the first entry gives `query`: its link when its key is `query`, else none. -/ +def readHead (query : Nat) : List (Nat × Link) → Option Link + | [] => none + | (key, link) :: _ => if key = query then some link else none + +/-- One link per query over `full`, whose keys ascend. `table` is `full` from the entry the + previous query stopped at; a query below the previous one starts again from the head of + `full`. The queries cost one step each and `full`'s length once per such descent. -/ +def linkWalk (full : List (Nat × Link)) : + List (Nat × Link) → Nat → List Nat → List (Option Link) + | _, _, [] => [] + | table, previous, query :: queries => + let remaining := dropBelow query (if query < previous then full else table) + readHead query remaining :: linkWalk full remaining query queries + +/-- Entries below the query never match it or a later query. -/ +theorem linkLookup_dropBelow (query : Nat) (table : List (Nat × Link)) (later : Nat) + (after : query ≤ later) : + linkLookup (dropBelow query table) later = linkLookup table later := by + induction table with + | nil => rfl + | cons head rest ih => + obtain ⟨key, link⟩ := head + by_cases below : key < query + · have different : ¬ (key == later) = true := by + simp only [beq_iff_eq] + omega + have skip : (((key, link) :: rest).find? fun entry => entry.1 == later) = + rest.find? fun entry => entry.1 == later := List.find?_cons_of_neg different + have step := ih + simp only [dropBelow, below, ite_true, linkLookup, skip] at step ⊢ + exact step + · simp [dropBelow, below] + +#print axioms linkLookup_dropBelow + +/-- Dropping the entries below a query keeps the table ascending from the query. -/ +theorem dropBelow_ascend (query : Nat) (table : List (Nat × Link)) : + ∀ lower, keysAscend lower table = true → + keysAscend query (dropBelow query table) = true := by + induction table with + | nil => intro _ _; rfl + | cons head rest ih => + obtain ⟨key, link⟩ := head + intro lower ascending + simp only [keysAscend, Bool.and_eq_true, decide_eq_true_eq] at ascending + by_cases below : key < query + · simp only [dropBelow, below, ite_true] + exact ih (key + 1) ascending.2 + · simp only [dropBelow, below, ite_false, keysAscend, Bool.and_eq_true, + decide_eq_true_eq] + exact ⟨by omega, ascending.2⟩ + +#print axioms dropBelow_ascend + +/-- Dropping below a query what is left after dropping below an earlier one drops below the + query. -/ +theorem dropBelow_dropBelow (earlier query : Nat) (table : List (Nat × Link)) + (after : earlier ≤ query) : + dropBelow query (dropBelow earlier table) = dropBelow query table := by + induction table with + | nil => rfl + | cons head rest ih => + obtain ⟨key, link⟩ := head + by_cases below : key < earlier + · simp only [dropBelow, below, ite_true, show key < query by omega] + exact ih + · simp only [dropBelow, below, ite_false] + +#print axioms dropBelow_dropBelow + +/-- No key is below zero, so dropping below zero keeps the table. -/ +theorem dropBelow_zero (table : List (Nat × Link)) : dropBelow 0 table = table := by + cases table with + | nil => rfl + | cons head rest => + obtain ⟨key, link⟩ := head + simp [dropBelow] + +#print axioms dropBelow_zero + +/-- Over an ascending table, the first entry left after dropping below a query answers it as a + scan of the table would. -/ +theorem readHead_dropBelow (query : Nat) (table : List (Nat × Link)) (lower : Nat) + (ascending : keysAscend lower table = true) : + readHead query (dropBelow query table) = linkLookup table query := by + rw [← linkLookup_dropBelow query table query (Nat.le_refl query)] + have rest_ascends := dropBelow_ascend query table lower ascending + cases remaining : dropBelow query table with + | nil => rfl + | cons entry rest => + obtain ⟨key, link⟩ := entry + rw [remaining] at rest_ascends + simp only [keysAscend, Bool.and_eq_true, decide_eq_true_eq] at rest_ascends + have bounded := keysAscend_bound rest (key + 1) rest_ascends.2 + by_cases hit : key = query + · subst hit + have first : (((key, link) :: rest).find? fun entry => entry.1 == key) = + some (key, link) := List.find?_cons_of_pos (Nat.beq_refl key) + simp only [readHead, linkLookup, ite_true] + rw [first] + rfl + · have different : ¬ (key == query) = true := by + simp only [beq_iff_eq] + exact hit + have none_after : (rest.find? fun entry => entry.1 == query) = none := by + rw [List.find?_eq_none] + intro other member + have := bounded other member + simp only [beq_iff_eq] + omega + have skip : (((key, link) :: rest).find? fun entry => entry.1 == query) = + rest.find? fun entry => entry.1 == query := List.find?_cons_of_neg different + simp only [readHead, linkLookup, hit, ite_false] + rw [skip, none_after] + rfl + +#print axioms readHead_dropBelow + +/-- In a table ascending from `lower`, every entry up to position `last` has a key at most that + entry's. -/ +theorem keysAscend_take_le {α : Type} : ∀ (entries : List (Nat × α)) (lower last : Nat) + (bound : Nat × α), keysAscend lower entries = true → entries[last]? = some bound → + ∀ entry ∈ entries.take (last + 1), entry.1 ≤ bound.1 + | [], _, _, _, _, found => by simp at found + | (key, value) :: rest, lower, last, bound, ascending, found => by + simp only [keysAscend, Bool.and_eq_true, decide_eq_true_eq] at ascending + cases last with + | zero => + simp only [List.getElem?_cons_zero, Option.some.injEq] at found + subst found + simp + | succ last => + simp only [List.getElem?_cons_succ] at found + have later := keysAscend_bound rest (key + 1) ascending.2 bound (List.mem_of_getElem? found) + intro entry member + simp only [List.take_succ_cons, List.mem_cons] at member + rcases member with rfl | member + · simp only + omega + · exact keysAscend_take_le rest (key + 1) last bound ascending.2 found entry member + +#print axioms keysAscend_take_le + +/-- What is left of an ascending table after any prefix ascends from some bound. -/ +theorem keysAscend_drop {α : Type} : ∀ (entries : List (Nat × α)) (lower count : Nat), + keysAscend lower entries = true → ∃ bound, keysAscend bound (entries.drop count) = true + | [], _, _, _ => ⟨0, by simp [keysAscend]⟩ + | _ :: _, lower, 0, ascending => ⟨lower, ascending⟩ + | (key, _) :: rest, _, count + 1, ascending => by + simp only [keysAscend, Bool.and_eq_true] at ascending + exact keysAscend_drop rest (key + 1) count ascending.2 + +#print axioms keysAscend_drop + +/-- Over an ascending table, the link of `query` is read from the head of the table, so a pc + before its first block needs no other entry. -/ +theorem linkAt_eq_readHead {body : Method} (ascending : keysAscend 0 body.links = true) + (query : Nat) : body.linkAt query = readHead query (dropBelow query body.links) := + (readHead_dropBelow query body.links 0 ascending).symm + +#print axioms linkAt_eq_readHead + +/-- Over an ascending table, when the entry at `previous` has a key below `query`, the link of + `query` is read from `suffix`, the table after that entry, so it walks no earlier entry. -/ +theorem linkAt_eq_readHead_suffix {body : Method} (ascending : keysAscend 0 body.links = true) + {previous query : Nat} {last : Nat × Link} {suffix : List (Nat × Link)} + (placed : body.links.drop (previous + 1) = suffix) (found : body.links[previous]? = some last) + (below : last.1 < query) : body.linkAt query = readHead query (dropBelow query suffix) := by + obtain ⟨bound, rest⟩ := keysAscend_drop body.links 0 (previous + 1) ascending + rw [placed] at rest + rw [readHead_dropBelow query suffix bound rest, Method.linkAt, ← placed] + have earlier := keysAscend_take_le body.links 0 previous last ascending found + conv => lhs; rw [← List.take_append_drop (previous + 1) body.links] + simp only [linkLookup, List.find?_append] + rw [show (List.take (previous + 1) body.links).find? (fun entry => entry.1 == query) = none from + List.find?_eq_none.mpr fun entry member => by + have := earlier entry member + simp only [beq_iff_eq] + omega] + rfl + +#print axioms linkAt_eq_readHead_suffix + +/-- While `table` is what is left of an ascending `full` after dropping below `previous`, the + walk reads each query as a scan of `full` would, in whatever order the queries come. -/ +theorem linkWalk_eq (full : List (Nat × Link)) (ascending : keysAscend 0 full = true) + (queries : List Nat) : ∀ (table : List (Nat × Link)) (previous : Nat), + table = dropBelow previous full → + linkWalk full table previous queries = queries.map (linkLookup full) := by + induction queries with + | nil => intros; rfl + | cons query queries ih => + intro table previous suffix + have restarted : dropBelow query (if query < previous then full else table) = + dropBelow query full := by + split + · rfl + · rw [suffix] + exact dropBelow_dropBelow previous query full (by omega) + simp only [linkWalk, List.map_cons, restarted] + rw [readHead_dropBelow query full 0 ascending, ih (dropBelow query full) query rfl] + +#print axioms linkWalk_eq + +/-- The links an embedding reads for the queries: the walk when the table keys ascend, the scan + otherwise. -/ +def linkReads (links : List (Nat × Link)) (queries : List Nat) : List (Option Link) := + if keysAscend 0 links then linkWalk links links 0 queries + else queries.map (linkLookup links) + +/-- However the table and queries are ordered, the reads are the scans' answers. -/ +theorem linkReads_eq (links : List (Nat × Link)) (queries : List Nat) : + linkReads links queries = queries.map (linkLookup links) := by + unfold linkReads + split + · rename_i ascending + exact linkWalk_eq links ascending queries links 0 (dropBelow_zero links).symm + · rfl + +#print axioms linkReads_eq + +/-- The link of each pc of `body`'s code, read by one walk over the table and indexed once, so + a check that asks every pc pays the table and the code once and a logarithm per query; a pc + past the code takes the scan. -/ +def linkIndex (body : Method) (pc : Nat) : Option Link := + match (FlowIndex.ofList (linkReads body.links (List.range body.code.length))).get? pc with + | some link => link + | none => body.linkAt pc + +/-- The index gives every pc the link a scan of the table gives it. -/ +theorem linkIndex_eq (body : Method) : linkIndex body = body.linkAt := by + funext pc + rw [linkIndex, FlowIndex.get?_ofList, linkReads_eq] + by_cases inside : pc < body.code.length <;> simp [inside, Method.linkAt] + +#print axioms linkIndex_eq + +end CIL.Extraction diff --git a/CIL/Extraction/Lockstep.lean b/CIL/Extraction/Lockstep.lean index 8a299ed13..a4ad05d36 100644 --- a/CIL/Extraction/Lockstep.lean +++ b/CIL/Extraction/Lockstep.lean @@ -16,6 +16,20 @@ def all3 (check : α → β → γ → Bool) : List α → List β → List γ | [], [], [] => true | _, _, _ => false +/-- A passing lockstep check consumes all three lists together. -/ +theorem all3_lengths {check : α → β → γ → Bool} : + ∀ {first : List α} {second : List β} {third : List γ}, all3 check first second third = true → + first.length = second.length ∧ first.length = third.length + | [], [], [], _ => ⟨rfl, rfl⟩ + | _ :: rest, _ :: states, _ :: code, checked => by + have lengths := all3_lengths (Bool.and_eq_true_iff.mp checked).2 + exact ⟨congrArg Nat.succ lengths.1, congrArg Nat.succ lengths.2⟩ + | [], _ :: _, _, checked | _ :: _, [], _, checked | [], [], _ :: _, checked => by + simp [all3] at checked + | _ :: _, _ :: _, [], checked => by simp [all3] at checked + +#print axioms all3_lengths + /-- A passing lockstep check holds at every index present in all three lists. -/ theorem all3_at {check : α → β → γ → Bool} : ∀ {as : List α} {bs : List β} {cs : List γ} {k : Nat} {a : α} {b : β} {c : γ}, @@ -36,4 +50,68 @@ theorem all3_at {check : α → β → γ → Bool} : #print axioms all3_at +/-- Two lockstep checks agree if they agree on every triple at a common index. -/ +theorem all3_congr_getElem? {left right : α → β → γ → Bool} : + ∀ (as : List α) (bs : List β) (cs : List γ), + (∀ (pc : Nat) a b c, as[pc]? = some a → bs[pc]? = some b → cs[pc]? = some c → + left a b c = right a b c) → all3 left as bs cs = all3 right as bs cs + | a :: as, b :: bs, c :: cs, agree => by + simp only [all3, agree 0 a b c rfl rfl rfl] + rw [all3_congr_getElem? as bs cs fun pc x y z => agree (pc + 1) x y z] + | [], bs, cs, _ => by cases bs <;> cases cs <;> rfl + | _ :: _, [], _, _ => rfl + | _ :: _, _ :: _, [], _ => rfl + +#print axioms all3_congr_getElem? + +/-- Equal-length lists pass a lockstep check when the triple at every index passes. -/ +theorem all3_of_getElem? {check : α → β → γ → Bool} {as : List α} {bs : List β} {cs : List γ} + (lenB : as.length = bs.length) (lenC : as.length = cs.length) + (each : ∀ (pc : Nat) a b c, as[pc]? = some a → bs[pc]? = some b → + cs[pc]? = some c → check a b c = true) : all3 check as bs cs = true := by + induction as generalizing bs cs with + | nil => cases bs <;> cases cs <;> simp_all [all3] + | cons a rest ih => + cases bs with + | nil => simp at lenB + | cons b bs => + cases cs with + | nil => simp at lenC + | cons c cs => + exact Bool.and_eq_true_iff.mpr ⟨each 0 a b c rfl rfl rfl, + ih (Nat.succ.inj lenB) (Nat.succ.inj lenC) (fun pc => each (pc + 1))⟩ + +#print axioms all3_of_getElem? +/-- Mapping the middle list changes nothing when the two checks agree on each of its entries. -/ +theorem all3_map {check : α → β → γ → Bool} {mapped : α → δ → γ → Bool} (f : β → δ) : + ∀ (as : List α) (bs : List β) (cs : List γ), + (∀ a b c, b ∈ bs → check a b c = mapped a (f b) c) → + all3 check as bs cs = all3 mapped as (bs.map f) cs + | a :: as, b :: bs, c :: cs, agree => by + simp only [all3, List.map_cons] + rw [agree a b c (List.mem_cons_self ..), + all3_map f as bs cs fun a b c member => agree a b c (List.mem_cons_of_mem _ member)] + | [], [], [], _ => rfl + | [], _ :: _, _, _ | _ :: _, [], _, _ | [], [], _ :: _, _ => rfl + | _ :: _, _ :: _, [], _ => rfl + +#print axioms all3_map + +/-- A passing lockstep check holds for every entry of the middle list. -/ +theorem all3_mem {check : α → β → γ → Bool} : + ∀ {as : List α} {bs : List β} {cs : List γ} {b : β}, + all3 check as bs cs = true → b ∈ bs → ∃ a c, check a b c = true + | a :: as, b :: bs, c :: cs, entry, valid, member => by + simp only [all3, Bool.and_eq_true] at valid + rcases List.mem_cons.mp member with same | later + · subst same + exact ⟨a, c, valid.1⟩ + · exact all3_mem valid.2 later + | [], [], [], _, _, member => by simp at member + | [], _ :: _, _, _, valid, _ | _ :: _, [], _, _, valid, _ | [], [], _ :: _, _, valid, _ => by + simp [all3] at valid + | _ :: _, _ :: _, [], _, valid, _ => by simp [all3] at valid + +#print axioms all3_mem + end CIL.Extraction diff --git a/CIL/Extraction/MarshalCalls.lean b/CIL/Extraction/MarshalCalls.lean deleted file mode 100644 index bb88b444a..000000000 --- a/CIL/Extraction/MarshalCalls.lean +++ /dev/null @@ -1,81 +0,0 @@ -import CIL.Extraction.UnsafeTransfers - -/-! -# Exact byte-span value transfers - -MemoryMarshal calls retain their complete generic signature and transfer representation. -Aggregate widths additionally require the existing complete layout-use certificate. -`Vector128.Create` and `Vector256.Create` over a byte span are the same checked -read of the vector, including the `ArgumentOutOfRangeException` for a short span. --/ - -namespace CIL.Extraction - -/-- An exact runtime generic call agrees with its lowered byte-span operation. -/ -private def marshalCallMatches (layouts : List RawLayout) (raw : RawInstruction) - (kind : TransferKind) (read trying : Bool) : Bool := Id.run do - if raw.opcode != 40 then return false - let .call site := raw.operand | return false - let [type] := site.typeArguments | return false - if site.declaringType != .atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal" || - site.hasThis || !transferKindMatches layouts type kind then return false - let name := if read then (if trying then "TryRead" else "Read") - else if trying then "TryWrite" else "Write" - let spanName := if read then "System.ReadOnlySpan`1" else "System.Span`1" - let span := TypeExpr.apply (.constructor runtimeScope spanName) - (.atom runtimeScope "System.Byte") - let parameters := if read && !trying then [span] else [span, .byref type] - let result := if trying then TypeExpr.atom runtimeScope "System.Boolean" - else if read then type else .void - let resultName := if trying then "System.Boolean" else if read then "!!0" else "System.Void" - let parameterNames := spanName ++ "" ++ (if read && !trying then "" else ",!!0&") - return site.name == name && site.parameterCount == parameters.length && - site.parameterTypes == parameters && site.returnType == result && - site.returnsValue == (read || trying) && - site.target == .external (runtimeScope ++ "::" ++ resultName ++ - " System.Runtime.InteropServices.MemoryMarshal::" ++ name ++ "(" ++ parameterNames ++ ")") - -/-- The vector type a byte-span vector construction produces, by transfer shape. -/ -private def spanVectorOwner : TransferKind → Option String - | .snapshot .vector128 => some "System.Runtime.Intrinsics.Vector128" - | .snapshot .vector256 => some "System.Runtime.Intrinsics.Vector256" - | _ => none - -/-- A call to `Vector128.Create` or `Vector256.Create` taking one read-only span, - by owner, name, type argument and parameter shape alone. -/ -private def byteSpanCreateFamily (site : CallSite) : Bool := - site.name == "Create" && site.typeArguments == [.atom runtimeScope "System.Byte"] && - (site.declaringType == .atom intrinsicsScope "System.Runtime.Intrinsics.Vector128" || - site.declaringType == .atom intrinsicsScope "System.Runtime.Intrinsics.Vector256") && - match site.parameterTypes with - | [.apply (.constructor _ "System.ReadOnlySpan`1") _] => true - | _ => false - -/-- An exact byte-span vector construction agrees with the checked read of its vector. -/ -private def byteSpanCreateMatches (raw : RawInstruction) (kind : TransferKind) : Bool := - Id.run do - if raw.opcode != 40 then return false - let .call site := raw.operand | return false - let some owner := spanVectorOwner kind | return false - let byte := TypeExpr.atom runtimeScope "System.Byte" - let span := TypeExpr.apply (.constructor runtimeScope "System.ReadOnlySpan`1") byte - let vector := TypeExpr.apply (.constructor intrinsicsScope (owner ++ "`1")) byte - return site.declaringType == .atom intrinsicsScope owner && site.name == "Create" && - !site.hasThis && site.typeArguments == [byte] && site.parameterCount == 1 && - site.parameterTypes == [span] && site.returnType == vector && site.returnsValue && - site.target == .external (intrinsicsScope ++ "::" ++ owner ++ "`1 " ++ owner ++ - "::Create(System.ReadOnlySpan`1)") - -/-- No other raw instruction may manufacture a byte-span transfer operation, and a byte-span - vector construction emits only the exact read of its vector. -/ -def marshalTransferMatches (layouts : List RawLayout) (raw : RawInstruction) : Op → Bool - | .memory (.marshalRead kind trying) => marshalCallMatches layouts raw kind true trying || - !trying && byteSpanCreateMatches raw kind - | .memory (.marshalWrite kind trying) => marshalCallMatches layouts raw kind false trying - | _ => match raw.operand with - | .call site => !(site.declaringType == - .atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal" && - site.name ∈ ["Read", "Write", "TryRead", "TryWrite"]) && !byteSpanCreateFamily site - | _ => true - -end CIL.Extraction diff --git a/CIL/Extraction/MarshalModels.lean b/CIL/Extraction/MarshalModels.lean new file mode 100644 index 000000000..843a673c0 --- /dev/null +++ b/CIL/Extraction/MarshalModels.lean @@ -0,0 +1,108 @@ +import CIL.Extraction.ModelTable +import CIL.Extraction.TransferKinds + +/-! +# MemoryMarshal runtime models + +GetReference, AsBytes, Cast, CreateSpan and CreateReadOnlySpan take primitive integers, +Boolean, Char, enums with integer storage, supported integer vectors and checked structs. +GetArrayDataReference also requires an array element. Read, TryRead, Write and TryWrite +transfer these elements as scalars or snapshots. Each size comes from the checked layouts. +-/ + +namespace CIL.Extraction + +/-- A static member of the exact runtime MemoryMarshal type. -/ +private inductive Member where + | getReference | getArrayDataReference | asBytes | cast | createSpan | createReadOnlySpan + | read | tryRead | write | tryWrite + deriving DecidableEq + +/-- The type declaring the marshal operations. -/ +private def owner := TypeExpr.atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal" + +/-- The writable or readonly runtime span constructor applied to its element. -/ +private def spanOf (writable : Bool) (element : TypeExpr) : TypeExpr := + let name := if writable then "System.Span`1" else "System.ReadOnlySpan`1" + .apply (.constructor runtimeScope name) element + +/-- The member's parameter and result types at the given instantiation. -/ +private def Member.signature (member : Member) (source target : TypeExpr) (writable : Bool) : + OpenSignature := + let view := spanOf writable + let byte := TypeExpr.atom runtimeScope "System.Byte" + let integer := TypeExpr.atom runtimeScope "System.Int32" + let boolean := TypeExpr.atom runtimeScope "System.Boolean" + let sig (result : TypeExpr) (parameters : List TypeExpr) : OpenSignature := + { convention := 0x10, returnType := result, parameters } + match member with + | .getReference => sig (.byref source) [view source] + | .getArrayDataReference => sig (.byref source) [.array source] + | .asBytes => sig (view byte) [view source] + | .cast => sig (view target) [view source] + | .createSpan => sig (spanOf true source) [.byref source, integer] + | .createReadOnlySpan => sig (spanOf false source) [.byref source, integer] + | .read => sig source [spanOf false byte] + | .tryRead => sig boolean [spanOf false byte, .byref source] + | .write => sig .void [spanOf true byte, .byref source] + | .tryWrite => sig boolean [spanOf true byte, .byref source] + +/-- Whether the declaration has a writable span as its first parameter. -/ +private def writableInput (parameters : List TypeExpr) : Bool := + match parameters with + | .apply (.constructor _ "System.Span`1") _ :: _ => true + | _ => false + +/-- The closed operation, including the element widths and signedness from the layouts. -/ +private def Member.operation (member : Member) (reader : CallLayouts) + (method : MethodRef) : Option MemoryOp := do + guard (!method.hasThis && method.owner == owner) + let (source, target) ← match member, method.methodArguments with + | .cast, [source, target] => some (source, target) + | .cast, _ => none + | _, [source] => some (source, .void) + | _, _ => none + guard (method.genericArity == method.methodArguments.length) + let writable := writableInput method.parameters + guard (method.signature 0x10 == member.signature source target writable) + guard ((arrayElementType source).isSome) + let kind ← transferKind? reader source + match member with + | .getReference => return .spanReference + | .getArrayDataReference => return .observeView .arrayData + | .asBytes => return .spanReinterpret kind.width 1 writable + | .cast => + guard ((arrayElementType target).isSome) + return .spanReinterpret kind.width (← transferKind? reader target).width writable + | .createSpan | .createReadOnlySpan => + return .spanFromReference kind.width (member == .createSpan) + | .read | .tryRead => return .marshalRead kind (member == .tryRead) + | .write | .tryWrite => + return .marshalWrite kind (member == .tryWrite) + +/-- The call entry binds the uninstantiated declaration as well as the closed reference. -/ +private def entry (name : MetadataName) (member : Member) : ModelEntry := + .single .call owner name (.declaration fun sig => + sig == member.signature (.parameter 0) (.parameter 1) (writableInput sig.parameters)) + (fun reader method => RuntimeModel.memory <$> member.operation reader method) + +/-- The runtime MemoryMarshal entries, one for each member name. -/ +def marshalEntries : List ModelEntry := + [entry name!"GetReference" .getReference, + entry name!"GetArrayDataReference" .getArrayDataReference, + entry name!"AsBytes" .asBytes, entry name!"Cast" .cast, entry name!"CreateSpan" .createSpan, + entry name!"CreateReadOnlySpan" .createReadOnlySpan, entry name!"Read" .read, + entry name!"TryRead" .tryRead, entry name!"Write" .write, entry name!"TryWrite" .tryWrite] + +/-- Every marshal entry resolves to a memory operation. -/ +theorem marshalEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : + ∀ candidate ∈ marshalEntries, candidate.Yields property := by + intro candidate listed environment method model resolved + simp only [marshalEntries, List.mem_cons, List.not_mem_nil, or_false] at listed + rcases listed with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + obtain ⟨_, _, rfl⟩ := Option.map_eq_some_iff.mp resolved <;> exact memory _ + +#print axioms marshalEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/MetadataBinding.lean b/CIL/Extraction/MetadataBinding.lean new file mode 100644 index 000000000..ce6d18fa5 --- /dev/null +++ b/CIL/Extraction/MetadataBinding.lean @@ -0,0 +1,1050 @@ +import CIL.Extraction.MetadataResolution +import CIL.Extraction.Assemblies +import CIL.Extraction.MetadataContractIdentity + +/-! +# Binding token operands and method definitions to metadata + +Each token of a method body names a type, a field, a method, a signature or a string. The +extractor states what each means as a `RawOperand`; `checkEntry` recomputes everything the +bytes decide: the scope and full name of every type, the value or class kind of types the +carried slices define, field indices and static flags, the arguments of generic instances and +method specifications, the definition a member reference resolves to, the owner and name an +external call target spells, and the text of a string. The extractor's constructor choice +(`atom`, `object`, `aggregate`) is accepted when it names the same scoped type with a form the +kind allows. `checkLocals` does the same for local variable types and `methodIdentityValid` +for the method definition itself. + +Not checked, as `docs/SOUNDNESS-EVIDENCE.md` states: that the carried rows are the image's, +definitions in platform assemblies, imported implementation definitions and public key tokens. +-/ + +namespace CIL.Extraction + +/-- What a token says that depends on the instruction using it: whether a `Field` row is + static, when the row is carried. -/ +structure TokenFacts where + staticField : Option Bool := none + deriving DecidableEq, Repr + +/-- The frame of the primitive assembly and the method and owner generic arities a method's + types are read in. -/ +def MetadataEnv.frame (env : MetadataEnv) (arity ownerArity : Nat) : + Except MetadataError Frame := do + let index ← env.slice 0 0 + let corlib ← index.corlibIdentity + .ok { corlib, anchor := corlib.getD [], arity, ownerArity } + +/-- The type of a signature blob read in the slice at `position`. -/ +def MetadataEnv.resolveBlob (env : MetadataEnv) (position token blobOffset : Nat) : + Except MetadataError ResolvedType := do + let index ← env.slice position token + env.resolve position (← decodeFieldSig (← index.blob blobOffset)) + +/-- Resolve each signature type of a list. -/ +def MetadataEnv.resolveAll (env : MetadataEnv) (position : Nat) : + List SigType → Except MetadataError (List ResolvedType) + | [] => .ok [] + | type :: rest => do + let first ← env.resolve position type + .ok (first :: (← env.resolveAll position rest)) + +/-! ## Where a member is declared -/ + +/-- The `TypeDef` that declares a `Field` or `MethodDef` token, from the slice's link, with the + check that the token lies in the type's range of members. -/ +def MetadataEnv.declaringType (env : MetadataEnv) (position token : Nat) : + Except MetadataError Nat := do + let index ← env.slice position token + let some (linked, owner) := index.link linkDeclaring token + | throw (.unresolvedMember token) + if linked != position then throw (.unresolvedMember token) + let row ← index.typeDef owner + let table := metadataTableOf token + let (start, stop) ← + if table == Table.field then do .ok (row.fieldList, ← index.fieldEnd owner row) + else do + let end_ ← + if tokenRow owner == index.header.rows Table.typeDef then + pure (index.header.rows Table.methodDef + 1) + else do + let next ← checkedTokenOf Table.typeDef (tokenRow owner + 1) .rowOverflow + .ok (← index.typeDef next).methodList + .ok (row.methodList, end_) + if tokenRow token < start || stop ≤ tokenRow token then throw (.unresolvedMember token) + .ok owner + +/-- The type a definition token's owner names, with its generic parameters left open. -/ +def MetadataEnv.ownerOf (env : MetadataEnv) (position token : Nat) : + Except MetadataError NamedType := do + typeDefNamed env position resolveFuel (← env.declaringType position token) + +/-- The type a `Field` or `MethodDef` token used as an operand is declared on, which takes no + generic arguments: the token names no instance to supply them. -/ +def MetadataEnv.plainOwnerOf (env : MetadataEnv) (token : Nat) : + Except MetadataError NamedType := do + let declaring ← env.declaringType 0 token + let owner ← typeDefNamed env 0 resolveFuel declaring + env.requireArity 0 declaring owner 0 + .ok owner + +/-- The type a resolved owner names: its head, with generic arguments aside. -/ +def ResolvedType.head : ResolvedType → Option NamedType + | .named type => some type + | .genericInst head _ => some head + | _ => none + +/-- The generic arguments of a resolved owner. -/ +def ResolvedType.arguments : ResolvedType → Option (List ResolvedType) + | .genericInst _ arguments => some arguments.toList + | _ => none + +/-! ## Strings -/ + +/-- Whether the user string of a token has the code units of the claimed text. -/ +def userStringMatches (index : MetadataIndex) (token : Nat) (text : String) : + Except MetadataError Bool := do + let units ← index.userString (tokenRow token) + let bytes := bytesOf text + .ok (utf16Units bytes.length bytes == some units) + +/-! ## Types -/ + +/-- The separate assembly name on a metadata type head. Flat owners carry it inside the name. -/ +def TypeExpr.explicitHeadScope : TypeExpr → Option String + | .atom scope _ | .object scope _ | .constructor scope _ | .objectConstructor scope _ => + some scope + | .apply head _ => head.explicitHeadScope + | _ => none + +/-- The assembly of the declaring type, including a scope embedded in an aggregate or enum name. -/ +def TypeExpr.headScope : TypeExpr → Option String + | .aggregate identity _ | .enum identity _ => flatOwnerScope? identity + | .apply head _ => head.headScope + | type => type.explicitHeadScope + +/-- Check a `.type` operand: the token's type is the one the extractor spells. -/ +def checkTypeOperand (env : MetadataEnv) (frame : Frame) (token : Nat) (expr : TypeExpr) : + Except MetadataError TokenFacts := do + let resolved ← env.resolveToken 0 token + if frame.admits resolved expr then .ok {} else .error (.operandMismatch token) + +/-! ## Fields -/ + +/-- A field token read from the slice: its declaring type as the operand names it, its name, + its declared type, and the definition when a carried slice defines it. -/ +structure FieldSite where + owner : ResolvedType + name : List Nat + declared : ResolvedType + definition : Option (Nat × Nat) + /-- The head token of a generic owner marked VALUETYPE in the field reference's slice. -/ + valueHead : Option Nat := none + +/-- Where a field's `Field` row lives, with the checks that tie a `MemberRef` to it: the + definition lies in the declaring type's range and has the member's name and signature. -/ +def MetadataEnv.fieldDefinition (env : MetadataEnv) (token : Nat) (owner : NamedType) + (name : List Nat) (declared : ResolvedType) : Except MetadataError (Option (Nat × Nat)) := do + if !owner.pinned then return none + let own ← env.slice 0 token + let some (position, definition) := own.link linkDefinition token + | throw (.unresolvedMember token) + let index ← env.slice position token + let row ← index.field definition + let declaring ← env.ownerOf position definition + if declaring.scope != owner.scope || declaring.parts != owner.parts then + throw (.unresolvedMember token) + let found ← env.resolveBlob position definition row.signature + if (← index.string row.name) != name || found != declared then throw (.unresolvedMember token) + .ok (some (position, definition)) + +/-- Read a `Field` or `MemberRef` token as a field. -/ +def MetadataEnv.fieldSite (env : MetadataEnv) (token : Nat) : Except MetadataError FieldSite := do + let index ← env.slice 0 token + if metadataTableOf token == Table.field then + let row ← index.field token + .ok { owner := .named (← env.plainOwnerOf token), name := ← index.string row.name, + declared := ← env.resolveBlob 0 token row.signature, definition := some (0, token) } + else + let row ← index.memberRef token + let (owner, valueHead) ← if metadataTableOf row.parent == Table.typeSpec then do + let signature ← index.typeSpec row.parent + let head := match signature with + | .genericInst true token _ => some token + | _ => none + pure (← env.resolve 0 signature, head) + else pure (← env.resolveToken 0 row.parent, none) + let name ← index.string row.name + let declared ← env.resolveBlob 0 token row.signature + let definition ← match owner.head with + | some head => env.fieldDefinition token head name declared + | none => .error (.unresolvedMember token) + .ok { owner, name, declared, definition, valueHead } + +/-- The end of a class chain at a base no carried slice defines: `System.Object`, named through + a reference to an assembly of a core-library name, whose platform definition states that it + is the root class. One lookup reads the definition, refusing as `MetadataEnv.externalNamed` + refuses a class before the name and the root kind are checked. -/ +def MetadataEnv.chainRoot (env : MetadataEnv) (token base : Nat) (scope : List Nat) + (parts : List (List Nat)) (core : Bool) : Except MetadataError (List (Nat × Nat)) := do + if env.pinned.matches scope then throw (.missingSlice base) + let kind ← env.platform.require base scope parts (PlatformKind.fitsHint (some false)) + if !(core && parts == [systemNamespace, objectName]) then throw (.unsupportedBase token) + if kind == .objectRoot then .ok [] else .error (.platformKindMismatch base) + +/-- A class chain ends outside the carried slices only at a type whose platform definition is + the root class `System.Object`. -/ +theorem MetadataEnv.chainRoot_ok {env : MetadataEnv} {token base : Nat} {scope : List Nat} + {parts : List (List Nat)} {core : Bool} {chain : List (Nat × Nat)} + (ended : env.chainRoot token base scope parts core = .ok chain) : + { scope, parts, kind := .objectRoot } ∈ env.platform.definitions ∧ + parts = [systemNamespace, objectName] ∧ core = true := by + unfold MetadataEnv.chainRoot at ended + by_cases pinned : env.pinned.matches scope = true + · simp [pinned, throw, throwThe, MonadExceptOf.throw, bind, Except.bind] at ended + cases required : env.platform.require base scope parts (PlatformKind.fitsHint (some false)) with + | error error => simp [pinned, required, bind, Except.bind] at ended + | ok kind => + have member := (PlatformTable.require_ok required).1 + by_cases named : (core && parts == [systemNamespace, objectName]) = true + · simp only [Bool.and_eq_true, beq_iff_eq] at named + by_cases root : kind = .objectRoot + · cases root + exact ⟨member, named.2, named.1⟩ + · simp only [pinned, required, bind, Except.bind] at ended + simp [named, root] at ended + · simp [pinned, required, named, throw, throwThe, MonadExceptOf.throw, bind, Except.bind] + at ended + +#print axioms MetadataEnv.chainRoot_ok + +/-- A carried root has the public, nongeneric, fieldless System.Object definition in CoreLib. -/ +def MetadataEnv.carriedObjectRoot (env : MetadataEnv) (index : MetadataIndex) + (position token : Nat) (row : TypeDefRow) : Except MetadataError Bool := do + unless index.identity == bytesOf coreLibraryScope && + row.flags &&& 0x27 == 1 && index.instanceTotal.find? token == some 0 do return false + let namespaceBytes ← index.string row.namespace_ + let name ← index.string row.name + return namespaceBytes == systemNamespace && name == objectName && + (← env.definitionArity position token) == 0 + +/-- The base classes of a class definition, nearest first, as slice positions and `TypeDef` + tokens. Traversal includes each base except System.Object. -/ +def MetadataEnv.classChain (env : MetadataEnv) (fuel position token : Nat) + (includeOwner : Bool := false) : Except MetadataError (List (Nat × Nat)) := + match fuel with + | 0 => .error (.unsupportedBase token) + | fuel + 1 => do + let index ← env.slice position token + let row ← index.typeDef token + let lineage (bases : List (Nat × Nat)) := + if includeOwner then (position, token) :: bases else bases + match metadataTableOf row.extends_ with + | 0x02 => + if tokenRow row.extends_ == 0 then + if ← env.carriedObjectRoot index position token row then pure [] + else throw (.unsupportedBase token) + else if (← env.definitionArity position row.extends_) != 0 then + .error (.unsupportedBase token) + else lineage <$> env.classChain fuel position row.extends_ true + | 0x01 => + match ← typeRefTarget env position resolveFuel row.extends_ with + | .inl _ => + let some (linked, definition) := index.link linkDefinition row.extends_ + | throw (.unsupportedBase token) + if (← env.definitionArity position row.extends_) != 0 then throw (.unsupportedBase token) + lineage <$> env.classChain fuel linked definition true + | .inr (scope, _, parts) => + lineage <$> env.chainRoot token row.extends_ scope parts + (← index.coreLibraryReference row.extends_) + | _ => .error (.unsupportedBase token) + +/-- The number of instance fields of the types of a chain. -/ +def MetadataEnv.chainFieldCount (env : MetadataEnv) : List (Nat × Nat) → Except MetadataError Nat + | [] => .ok 0 + | (position, token) :: rest => do + let index ← env.slice position token + let some own := index.instanceTotal.find? token | throw (.missingRow token) + .ok (own + (← env.chainFieldCount rest)) + +/-- The model index of an instance field: counting the fields of the bases of a class first, + then the instance fields of the owner before the field. -/ +def MetadataEnv.instanceIndex (env : MetadataEnv) (position field : Nat) (classOwner : Bool) : + Except MetadataError Nat := do + let index ← env.slice position field + let some before := index.instanceBefore.find? field | throw (.unresolvedMember field) + if classOwner then + let owner ← env.declaringType position field + let inherited ← env.chainFieldCount (← env.classChain resolveFuel position owner) + .ok (inherited + before) + else .ok before + +/-- Storage width of a primitive field signature on the supported 64-bit profiles. -/ +private def primitiveWidth : Nat → Option Nat + | 0x02 | 0x04 | 0x05 => some 1 + | 0x03 | 0x06 | 0x07 => some 2 + | 0x08 | 0x09 => some 4 + | 0x0A | 0x0B | 0x18 | 0x19 => some 8 + | _ => none + +/-- The `platform` constant field, the one static field of an owner no carried slice defines + that a body may name: its index is 0, and its owner's platform definition must state the byte + order. -/ +def MetadataEnv.platformConstant (env : MetadataEnv) (token : Nat) (site : FieldSite) + (held : ResolvedType) (isStatic platform : Bool) : + Except MetadataError (Nat × ResolvedType × Option Bool) := + if !isStatic then .error (.unboundInstanceField token) + else if !platform then .error (.unboundStaticField token) + else match site.owner.head with + | none => .error (.unboundStaticField token) + | some head => + (fun _ => (0, held, none)) <$> + env.platform.require token head.scope head.parts (· == .byteOrder) + +/-- The constant field's owner has a platform definition that states the byte order. -/ +theorem MetadataEnv.platformConstant_ok {env : MetadataEnv} {token : Nat} {site : FieldSite} + {held : ResolvedType} {isStatic platform : Bool} {result : Nat × ResolvedType × Option Bool} + (found : env.platformConstant token site held isStatic platform = .ok result) : + result = (0, held, none) ∧ ∃ head, site.owner.head = some head ∧ + { scope := head.scope, parts := head.parts, kind := .byteOrder } ∈ + env.platform.definitions := by + unfold MetadataEnv.platformConstant at found + split at found + · cases found + · split at found + · cases found + · split at found + · cases found + · rename_i head owned + cases required : env.platform.require token head.scope head.parts (· == .byteOrder) with + | error error => simp [required, Functor.map, Except.map] at found + | ok kind => + simp only [required, Functor.map, Except.map, Except.ok.injEq] at found + have ⟨member, fits⟩ := PlatformTable.require_ok required + simp only [beq_iff_eq] at fits + cases fits + exact ⟨found.symm, head, owned, member⟩ + +#print axioms MetadataEnv.platformConstant_ok + +/-- The index and held type of a field whose owner no carried slice defines: an item of the + platform pair, whose owner's platform definition must state the pair, read as an instance + field, or the `platform` constant field (`MetadataEnv.platformConstant`). -/ +def MetadataEnv.platformField (env : MetadataEnv) (token : Nat) (site : FieldSite) + (held : ResolvedType) (isStatic platform : Bool) : + Except MetadataError (Nat × ResolvedType × Option Bool) := + -- The pair is known by its own reference, never by the slice's `corlib` number. + match site.owner with + | .genericInst head arguments => + match arguments.toList with + | [first, second] => + if isCorlibScope head.scope && head.parts == [systemNamespace, valueTuplePairName] then + match env.platform.require token head.scope head.parts (· == .valuePair) with + | .error error => .error error + | .ok _ => + if site.name == firstItemName && site.declared == .var 0 then .ok (0, first, some false) + else if site.name == secondItemName && site.declared == .var 1 then + .ok (1, second, some false) + else if site.name == firstItemName || site.name == secondItemName then + .error (.operandMismatch token) + else .error (.unboundInstanceField token) + else env.platformConstant token site held isStatic platform + | _ => env.platformConstant token site held isStatic platform + | _ => env.platformConstant token site held isStatic platform + +/-- A platform pair field reads the argument selected by both its name and declared signature. -/ +theorem MetadataEnv.platformField_pair {env : MetadataEnv} {token : Nat} {site : FieldSite} + {held type : ResolvedType} {isStatic platform : Bool} {index : Nat} + (found : env.platformField token site held isStatic platform = .ok (index, type, some false)) : + ∃ head arguments, site.owner = .genericInst head arguments ∧ + { scope := head.scope, parts := head.parts, kind := .valuePair } ∈ + env.platform.definitions ∧ site.declared = .var index ∧ + arguments.toList[index]? = some type ∧ + ((index = 0 ∧ site.name = firstItemName) ∨ (index = 1 ∧ site.name = secondItemName)) := by + unfold MetadataEnv.platformField at found + split at found + · rename_i head arguments owner + split at found + · split at found + · split at found + · cases found + · rename_i required + have ⟨member, fits⟩ := PlatformTable.require_ok required + simp only [beq_iff_eq] at fits + cases fits + refine ⟨head, arguments, owner, member, ?_⟩ + split at found + · rename_i selected + simp only [Bool.and_eq_true, beq_iff_eq] at selected + cases found + simp_all + · split at found + · rename_i selected + simp only [Bool.and_eq_true, beq_iff_eq] at selected + cases found + simp_all + · split at found <;> cases found + · cases (MetadataEnv.platformConstant_ok found).1 + · cases (MetadataEnv.platformConstant_ok found).1 + · cases (MetadataEnv.platformConstant_ok found).1 + +#print axioms MetadataEnv.platformField_pair + +/-- The index a field operand must carry, and the type it must hold. A field signature's `!n` + names the declaring type's parameter, so the held type reads it as the declaring instance's + argument, which may itself be a parameter of the reading method or its owner. No carried + row gives a field of an owner no carried slice defines, so such a field is refused except + as an item of the platform pair, read as an instance field whose reference declares it as + the pair's own parameter (`!0` for `Item1`, `!1` for `Item2`), and as the `platform` + constant field; any other member of the pair is refused too. -/ +def MetadataEnv.expectedField (env : MetadataEnv) (token : Nat) (site : FieldSite) + (classOwner isStatic platform : Bool) : + Except MetadataError (Nat × ResolvedType × Option Bool) := do + let some held := substituteResolved none site.owner.arguments resolveFuel site.declared + | throw (.operandMismatch token) + match site.definition with + | some (position, definition) => + let index ← env.slice position definition + let row ← index.field definition + let declaredStatic := row.flags / 0x10 % 2 == 1 + if declaredStatic then + let owner ← env.declaringType position definition + let (start, _) ← index.fieldRange owner + .ok (tokenRow definition - start, held, some true) + else .ok (← env.instanceIndex position definition classOwner, held, some false) + | none => + -- The pair is known by its own reference, never by the slice's `corlib` number. + let pair ← match site.owner, site.valueHead with + | .genericInst head (.cons (.primitive first) (.cons (.primitive second) .nil)), + some reference => do + if metadataTableOf reference != Table.typeRef || + head.parts != [systemNamespace, valueTuplePairName] || + !(primitiveWidth first).any (fun width => primitiveWidth second == some width) then + pure false + else + let index ← env.slice 0 token + let scope := (← index.typeRef reference).scope + if metadataTableOf scope == Table.assemblyRef then + pure ((← index.assemblyRefName scope runtimeAssembly) == some runtimeAssembly) + else pure false + | _, _ => pure false + if pair || (isStatic && platform) then env.platformField token site held isStatic platform + else if isStatic then throw (.unboundStaticField token) + else throw (.unboundInstanceField token) + +/-- Check a `.field` operand: the owner, the field's model index, the type it holds and the + name, which must spell the name of the row the token names. -/ +def checkFieldOperand (env : MetadataEnv) (frame : Frame) (token : Nat) (owner : TypeExpr) + (index : Nat) (fieldType : TypeExpr) (name : MetadataName) (isStatic : Bool) : + Except MetadataError TokenFacts := do + if metadataTableOf token != Table.field && metadataTableOf token != Table.memberRef then + throw (.unsupportedTable token) + let site ← env.fieldSite token + if !name.spells site.name then throw (.operandMismatch token) + if isStatic && site.owner.arguments.isSome then throw (.instantiatedStaticField token) + let classOwner := match owner with + | .object .. => true + | _ => false + let platform := isStatic && ({ owner, index, type := fieldType, name } : FieldRef) == + littleEndianField + let (expected, held, isStaticFact) ← env.expectedField token site classOwner isStatic platform + if isStaticFact.any (· != isStatic) then throw (.operandMismatch token) + if index != expected then throw (.fieldIndexMismatch token index expected) + if !(frame.admits site.owner owner) then throw (.operandMismatch token) + let heldAdmitted := if classOwner && !isStatic then frame.admitsHeld held fieldType + else frame.admits held fieldType + if !heldAdmitted then throw (.operandMismatch token) + .ok { staticField := isStaticFact } + +/-! ## Methods and calls -/ + +/-- A method token read from the slice: its declaring type as the operand names it, its name, + its signature resolved, the definition when a carried slice defines it and the method + arguments of a specification. -/ +structure MethodSite where + owner : ResolvedType + name : List Nat + convention : Nat + genericArity : Nat + result : ResolvedType + parameters : List ResolvedType + definition : Option (Nat × Nat) + methodArguments : Option (List ResolvedType) + +/-- The convention, generic arity, result and parameters of a method blob, resolved in the + slice at `position`. -/ +def MetadataEnv.resolveMethod (env : MetadataEnv) (position : Nat) (blob : List Nat) : + Except MetadataError (Nat × Nat × ResolvedType × List ResolvedType) := do + let signature ← decodeMethodSig blob + let result ← env.resolve position signature.result + .ok (signature.convention, signature.genericArity, result, + ← env.resolveAll position signature.parameters) + +/-- The `MethodDef` of a carried slice a `MemberRef` resolves to: it lies in the owner's + methods and has the member's name, convention, arity and resolved signature. -/ +def MetadataEnv.methodDefinition (env : MetadataEnv) (token : Nat) (owner : NamedType) + (name : List Nat) (signature : Nat × Nat × ResolvedType × List ResolvedType) : + Except MetadataError (Option (Nat × Nat)) := do + if !owner.pinned then return none + let own ← env.slice 0 token + let some (position, definition) := own.link linkDefinition token + | throw (.unresolvedMember token) + let index ← env.slice position token + let row ← index.methodDef definition + let declaring ← env.ownerOf position definition + if declaring.scope != owner.scope || declaring.parts != owner.parts then + throw (.unresolvedMember token) + let found ← env.resolveMethod position (← index.blob row.signature) + if (← index.string row.name) != name || found != signature then + throw (.unresolvedMember token) + .ok (some (position, definition)) + +/-- Only a missing external TypeRef at the declaring position can name an opaque owner. -/ +private def MetadataEnv.callOwner (env : MetadataEnv) (token : Nat) (linked : Bool) : + Except MetadataError ResolvedType := do + if !linked then return <- { env with publishedLayouts := #[] }.resolveToken 0 token + if metadataTableOf token != Table.typeRef then throw (.unresolvedMember token) + let (scope, name, parts) <- typeRefQualified env 0 resolveFuel token + if env.identities.contains scope || env.pinned.matches scope || + (env.platform.kindOf scope parts).isSome then throw (.unresolvedMember token) + return .named { scope, name, parts, kind := .importedOwner, pinned := false } + +/-- Read a definition or member reference, resolving an opaque owner only for a linked call. -/ +private def MetadataEnv.readMemberSite (env : MetadataEnv) (token : Nat) (linked : Bool) : + Except MetadataError MethodSite := do + let index ← env.slice 0 token + match metadataTableOf token with + | 0x06 => + let row ← index.methodDef token + let signature ← env.resolveMethod 0 (← index.blob row.signature) + .ok { owner := .named (← env.plainOwnerOf token), name := ← index.string row.name, + convention := signature.1, genericArity := signature.2.1, result := signature.2.2.1, + parameters := signature.2.2.2, definition := some (0, token), methodArguments := none } + | 0x0A => + let row ← index.memberRef token + let owner <- env.callOwner row.parent linked + let name ← index.string row.name + let signature ← env.resolveMethod 0 (← index.blob row.signature) + let definition ← match owner.head with + | some head => env.methodDefinition token head name signature + | none => .error (.unresolvedMember token) + .ok { owner := owner, name := name, convention := signature.1, genericArity := signature.2.1, + result := signature.2.2.1, parameters := signature.2.2.2, definition := definition, + methodArguments := none } + | _ => .error (.unsupportedTable token) + +/-- Read a `MethodDef`, `MemberRef` or `MethodSpec` token as a method; a specification adds the + method arguments of its instantiation blob. -/ +private def MetadataEnv.readMethodSite (env : MetadataEnv) (token : Nat) (linked : Bool) : + Except MetadataError MethodSite := do + if metadataTableOf token == Table.methodSpec then + let index ← env.slice 0 token + let row ← index.methodSpec token + let site <- env.readMemberSite row.method linked + let arguments ← decodeMethodSpec (← index.blob row.instantiation) + if arguments.isEmpty || arguments.length != site.genericArity then + throw (.genericMismatch token) + .ok { site with methodArguments := some (← env.resolveAll 0 arguments) } + else env.readMemberSite token linked + +/-- A method reference with a declaring type resolved from carried or platform metadata. -/ +def MetadataEnv.methodSite (env : MetadataEnv) (token : Nat) : Except MetadataError MethodSite := + env.readMethodSite token false + +/-- Cecil's full name of a method, `result Owner::name(parameters)`, from the full names of its + parts. -/ +def methodFullName (result owner name : List Nat) (parameters : List (List Nat)) : List Nat := + result ++ [32] ++ owner ++ [58, 58] ++ name ++ [40] ++ (parameters.intersperse [44]).flatten ++ + [41] + +/-- The identity an external call target spells: the declaring type's scope, then Cecil's full + name of the method. -/ +def MethodSite.externalName (site : MethodSite) : Option (List Nat) := do + let head ← site.owner.head + let result ← cecilName false resolveFuel site.result + let owner ← cecilName false resolveFuel site.owner + let parameters ← site.parameters.mapM (cecilName false resolveFuel) + some (head.scope ++ [58, 58] ++ methodFullName result owner site.name parameters) + +/-- The exact open definition fields of a generic imported contract. -/ +def MethodSite.contractDefinitionKey (site : MethodSite) (frame : Frame) : + Option (List Nat) := do + let owner ← site.owner.head + let result ← contractTypeKey frame resolveFuel site.result + let parameters ← site.parameters.mapM (contractTypeKey frame resolveFuel) + let fields := [contractPart owner.scope ++ contractPart owner.name, site.name, + bytesOf (toString site.genericArity), bytesOf (toString (site.convention % 0x20)), + bytesOf (if site.convention / hasThisFlag % 2 == 1 then "1" else "0"), + bytesOf (if site.convention / explicitThisFlag % 2 == 1 then "1" else "0"), result, + bytesOf (toString parameters.length)] ++ parameters + pure (bytesOf "contract-definition-v1:" ++ (fields.map contractPart).flatten) + +/-- The exact closed contract identity, retaining its open definition and scoped arguments. -/ +def MethodSite.contractName (site : MethodSite) (frame : Frame) : Option (List Nat) := do + let .named owner := site.owner | none + let some arguments := site.methodArguments | none + if site.definition.isSome || arguments.isEmpty || arguments.length != site.genericArity || + site.convention != 0x10 || frame.corlib != some runtimeScopeBytes || + !arguments.all contractInteger then none else do + let definition ← site.contractDefinitionKey frame + let keys ← arguments.mapM (contractTypeKey frame resolveFuel) + let argumentNames ← arguments.mapM (cecilName false resolveFuel) + let indexedNames := SearchTree.ofSorted + (argumentNames.zipIdx.map fun (name, index) => (index, name)) + let result ← contractClosedName indexedNames resolveFuel site.result + let parameters ← site.parameters.mapM (contractClosedName indexedNames resolveFuel) + let name := site.name ++ [60] ++ (argumentNames.intersperse [44]).flatten ++ [62] ++ + bytesOf "|definition|" ++ contractHex definition ++ bytesOf "|arguments|" ++ + (keys.map contractPart).flatten + pure (owner.scope ++ [58, 58] ++ methodFullName result owner.name name parameters) + +/-- Whether the external target exactly spells its member or its closed generic contract. -/ +def MethodSite.externalMatches (site : MethodSite) (frame : Frame) (identity : String) : Bool := + site.externalName == some (bytesOf identity) || + site.contractName frame == some (bytesOf identity) + +/-- A reconstructed generic contract identity passes the exact target comparison. -/ +theorem MethodSite.externalMatches_contract (site : MethodSite) (frame : Frame) + (identity : String) (matched : site.contractName frame = some (bytesOf identity)) : + site.externalMatches frame identity = true := by + simp [MethodSite.externalMatches, matched] +#print axioms MethodSite.externalMatches_contract + +/-- A static import resolves only under the exact ordinary or closed generic export identity. -/ +def MetadataEnv.importMethodSite (env : MetadataEnv) (token : Nat) (frame : Frame) + (identity : String) : Except MetadataError MethodSite := do + let site <- env.readMethodSite token true + let .named owner := site.owner | throw (.unresolvedMember token) + if owner.kind != .importedOwner || site.definition.isSome then throw (.unresolvedMember token) + let expected := if site.methodArguments.isSome then site.contractName frame + else if site.convention == 0 && site.genericArity == 0 then site.externalName else none + if expected != some (bytesOf identity) then throw (.operandMismatch token) + return site + +/-- The claimed parameter list names every parameter of the member, in order. -/ +private def parametersAdmitted (frame : Frame) : List ResolvedType → List TypeExpr → Bool + | [], [] => true + | type :: types, claim :: claims => + frame.admitsParameter type claim && parametersAdmitted frame types claims + | _, _ => false + +/-- The reference and open declaration describe the same metadata member in its type frame. -/ +def checkCallReference (env : MetadataEnv) (frame : Frame) (token : Nat) (site : MethodSite) + (call : CallSite) : Except MetadataError Unit := do + if site.genericArity != 0 && site.methodArguments.isNone then throw (.genericMismatch token) + let extracted := site.definition.isSome + let ownerArguments := site.owner.arguments + let ownerList := ownerArguments.getD [] + let head ← match site.owner.head with + | some head => pure head + | none => throw (.unresolvedType token) + if bytesOf call.name != site.name then throw (.operandMismatch token) + if !call.method.name.spells site.name then throw (.operandMismatch token) + if call.staticVirtualInterfaceSlot then + let some (position, definition) := site.definition | throw (.operandMismatch token) + let index ← env.slice position definition + let row ← index.methodDef definition + -- MethodAttributes.Static and Virtual, on a type whose metadata declares an interface. + if call.hasThis || head.kind != .interfaceType || row.flags &&& 0x50 != 0x50 then + throw (.operandMismatch token) + let expected := site.methodArguments.getD [] ++ (if extracted then ownerList else []) + if call.typeArguments.length != expected.length || !(expected.zip call.typeArguments).all + (fun (type, claimed) => frame.admits type claimed) then + throw (.genericMismatch token) + if call.ownerArgumentCount != (if extracted then ownerList.length else 0) then + throw (.genericMismatch token) + if call.method.genericArity != site.genericArity || + call.method.methodArguments != siteMethodArguments call then + throw (.genericMismatch token) + -- The full flat identity is checked by located.admits below. + if call.declaringType.explicitHeadScope.any (bytesOf · != head.scope) then + throw (.scopeMismatch token) + let located := { frame with anchor := head.scope } + if head.kind == .importedOwner then + let .atom scope name := call.declaringType | throw (.operandMismatch token) + if bytesOf scope != head.scope || bytesOf name != head.name then throw (.operandMismatch token) + else if !(located.admits site.owner call.declaringType) then throw (.operandMismatch token) + let declared := call.declared + if declared.convention != site.convention || + call.hasThis != (declared.convention / hasThisFlag % 2 == 1) || + call.parameterCount != site.parameters.length then throw (.operandMismatch token) + let member := { located with + arity := site.genericArity, ownerArity := ownerList.length, rawIndices := true } + if !member.admits site.result declared.returnType || + !parametersAdmitted member site.parameters declared.parameters then + throw (.operandMismatch token) + -- A nongeneric MethodDef supplies the closed signature in its own module's primitive scope. + let closed ← match site.definition with + | some (position, definition) => + if position == 0 || site.genericArity != 0 || !ownerList.isEmpty then pure located + else do + let index ← env.slice position definition + pure { located with corlib := ← index.corlibIdentity } + | none => pure located + let some returned := siteReturn site.methodArguments ownerArguments site.result + | throw (.operandMismatch token) + if call.returnsValue != (returned != .primitive 1) || !(closed.admits returned call.returnType) + then throw (.operandMismatch token) + let some parameters := site.parameters.mapM (siteParameter site.methodArguments ownerArguments) + | throw (.operandMismatch token) + if !parametersAdmitted closed parameters call.parameterTypes then throw (.operandMismatch token) + return () + +/-- Resolve the token through the selected import, or through the ordinary metadata environment. -/ +def resolveCallMember (env : MetadataEnv) (frame : Frame) (token : Nat) (call : CallSite) + (slot : Option ImportSlot) : Except MetadataError MethodSite := + match slot, call.target with + | some binding, .external identity => do + if binding.hasThis || binding.identity != identity || !importSignatureMatches binding call || + binding.parameterCount != call.parameterCount || binding.returnsValue != call.returnsValue then + throw (.operandMismatch token) + env.importMethodSite token frame identity + | some _, _ => throw (.unresolvedMember token) + | none, _ => env.methodSite token + +/-- The operand target and typed reference both name the method at the token. -/ +def checkCallOperand (env : MetadataEnv) (frame : Frame) (token : Nat) (call : CallSite) + (slot : Option ImportSlot := none) : Except MetadataError TokenFacts := do + let site ← resolveCallMember env frame token call slot + checkCallReference env frame token site call + match call.target with + | .managed target => + match site.definition with + | some (_, definition) => + if target == definition then .ok {} else .error (.operandMismatch token) + | none => .error (.operandMismatch token) + | .external identity => + if site.definition.isSome || !(site.externalMatches frame identity) then + .error (.operandMismatch token) + else .ok {} + +/-- A checked managed operand resolves to a definition in a carried slice. The MethodDef token + in that slice is the target of the call, and its signature checks the same typed reference. -/ +theorem checkCallOperand_managed {env : MetadataEnv} {frame : Frame} {token target : Nat} + {call : CallSite} {facts : TokenFacts} + (checked : checkCallOperand env frame token call = .ok facts) + (managed : call.target = .managed target) : + ∃ site position, env.methodSite token = .ok site ∧ + checkCallReference env frame token site call = .ok () ∧ + site.definition = some (position, target) := by + unfold checkCallOperand at checked + simp only [resolveCallMember] at checked + cases resolved : env.methodSite token with + | error fault => simp [resolved, bind, Except.bind] at checked + | ok site => + cases reference : checkCallReference env frame token site call with + | error fault => simp [resolved, reference, bind, Except.bind] at checked + | ok result => + cases result + cases definition : site.definition with + | none => simp [resolved, reference, managed, definition, bind, Except.bind] at checked + | some pair => + obtain ⟨position, method⟩ := pair + by_cases same : target = method + · exact ⟨site, position, rfl, reference, same ▸ definition⟩ + · simp [resolved, reference, managed, definition, same, bind, Except.bind] at checked + +#print axioms checkCallOperand_managed + +/-- The same member binds the typed reference and both permitted external identity spellings. -/ +theorem checkCallOperand_external {env : MetadataEnv} {frame : Frame} {token : Nat} + {call : CallSite} {identity : String} {facts : TokenFacts} {slot : Option ImportSlot} + (checked : checkCallOperand env frame token call slot = .ok facts) + (externalTarget : call.target = .external identity) : + ∃ site, resolveCallMember env frame token call slot = .ok site ∧ + checkCallReference env frame token site call = .ok () ∧ site.definition = none ∧ + (site.externalName = some (bytesOf identity) ∨ + site.contractName frame = some (bytesOf identity)) := by + unfold checkCallOperand at checked + simp only [externalTarget, bind, Except.bind] at checked + cases resolved : resolveCallMember env frame token call slot with + | error fault => simp [resolved] at checked + | ok site => + refine ⟨site, rfl, ?_⟩ + cases reference : checkCallReference env frame token site call with + | error fault => simp [resolved, reference, bind, Except.bind] at checked + | ok result => + cases result + simp [resolved, reference, bind, Except.bind] at checked + split at checked + · contradiction + · rename_i accepted + by_cases named : site.externalName = some (bytesOf identity) <;> + simpa [MethodSite.externalMatches, named] using accepted + +#print axioms checkCallOperand_external + +/-! ## Indirect calls and strings -/ + +/-- Check an `.indirectCall` operand against its standalone signature. -/ +def checkIndirectOperand (env : MetadataEnv) (frame : Frame) (token : Nat) (result : TypeExpr) + (parameters : List TypeExpr) (hasThis explicitThis : Bool) (convention : Nat) : + Except MetadataError TokenFacts := do + let index ← env.slice 0 token + let blob ← index.blob (← index.signatureOf Table.standAloneSig token) + let signature ← env.resolveMethod 0 blob + let (stated, arity, returned, types) := signature + if arity != 0 || stated / genericFlag % 2 == 1 then throw (.unsupportedSignature 0 stated) + if convention != stated % 0x20 || hasThis != (stated / hasThisFlag % 2 == 1) || + explicitThis != (stated / explicitThisFlag % 2 == 1) || + !(frame.admits returned result) || parameters.length != types.length || + !(types.zip parameters).all (fun (type, claimed) => frame.admits type claimed) then + throw (.operandMismatch token) + .ok {} + +/-- Check one token-table entry, read in a frame built once for the method: what the token names + must be what the operand says. -/ +def checkEntryIn (env : MetadataEnv) (frame : Frame) (token : Nat) (operand : RawOperand) : + Except MetadataError TokenFacts := do + match operand with + | .type expr => + if metadataTableOf token == Table.typeRef || metadataTableOf token == Table.typeDef || + metadataTableOf token == Table.typeSpec then checkTypeOperand env frame token expr + else .error (.unsupportedTable token) + | .field owner index fieldType name isStatic => + checkFieldOperand env frame token owner index fieldType name isStatic + | .call site => checkCallOperand env frame token site + | .indirectCall result parameters hasThis explicitThis convention => + checkIndirectOperand env frame token result parameters hasThis explicitThis convention + | .text text => + if metadataTableOf token != Table.userString then throw (.unsupportedTable token) + let index ← env.slice 0 token + if (← userStringMatches index token text) then .ok {} else .error (.operandMismatch token) + | _ => .error (.operandMismatch token) + +/-- Check one token-table entry of a method with `arity` generic parameters whose owner has + `ownerArity`. -/ +def checkEntry (env : MetadataEnv) (arity : Nat) (token : Nat) (operand : RawOperand) + (ownerArity : Nat := 0) : Except MetadataError TokenFacts := do + checkEntryIn env (← env.frame arity ownerArity) token operand + +/-- The facts that depend on the opcode using a token: a static field access names a static + field, an instance access an instance field, `ldtoken` of a field takes the static form, and + the platform constant field is only loaded. -/ +def checkSite (opcode : Nat) (facts : TokenFacts) : RawOperand → Bool + | .field owner index type name isStatic => + if ({ owner, index, type, name } : FieldRef) == littleEndianField && opcode != 0x7E then false + else if opcode == 0x7E || opcode == 0x7F || opcode == 0x80 then + isStatic && facts.staticField != some false + else if opcode == 0x7B || opcode == 0x7C || opcode == 0x7D then + !isStatic && facts.staticField != some true + else if opcode == 0xD0 then isStatic + else false + | _ => true + +/-! ## Locals -/ + +/-- Check the local variable types against the standalone signature the header names, for a + method with `arity` generic parameters whose owner has `ownerArity`. -/ +def checkLocals (env : MetadataEnv) (arity signature : Nat) (locals : List TypeExpr) + (ownerArity : Nat := 0) : Except MetadataError Unit := do + if signature == 0 then + if locals.isEmpty then return () else throw (.localMismatch 0) + if metadataTableOf signature != Table.standAloneSig then throw (.unsupportedTable signature) + let frame ← env.frame arity ownerArity + let index ← env.slice 0 signature + let blob ← index.blob (← index.signatureOf Table.standAloneSig signature) + let types ← env.resolveAll 0 (← decodeLocalSig blob) + if types.length != locals.length then throw (.localMismatch (min types.length locals.length)) + let rec compare : Nat → List ResolvedType → List TypeExpr → Except MetadataError Unit + | _, [], _ | _, _, [] => .ok () + | position, type :: types, claimed :: locals => + if frame.admits type claimed then compare (position + 1) types locals + else .error (.localMismatch position) + compare 0 types locals + +/-! ## The method definition -/ + +/-- Whether generic parameter rows have the attribute flags the extractor states. -/ +def flagsAgree (rows : List GenericParamRow) (claimed : List GenericConstraint) : Bool := + rows.length == claimed.length && (rows.zip claimed).all fun (row, constraint) => + row.flags == constraint.flags + +/-- The names of a method's definition as its metadata spells them: the scoped name of its owner + type, `scope::name`, the scope and name of each base class of an object receiver's type, from + the nearest, and the signature a proof selects the method by. The extractor states them as + strings, which `MethodNames.spelling` and `MethodNames.Selects` read from the bytes. -/ +structure MethodNames where + owner : List Nat := [] + bases : List (List Nat × List Nat) := [] + /-- Cecil's full name of the extracted method: the instance's, for a generic instance. -/ + signature : List Nat := [] + deriving DecidableEq, Repr + +/-- The owner string and base class types these names spell, or `none` when a name is not + well-formed UTF-8. -/ +def MethodNames.spelling (names : MethodNames) : Option (String × List TypeExpr) := do + let owner ← spelled? names.owner + let bases ← names.bases.mapM fun (scope, name) => do + pure (TypeExpr.object (← spelled? scope) (← spelled? name)) + pure (owner, bases) + +/-- The names are the owner and base class types the method states. An equation between strings + and bytes, so an extraction proves it with `cil_kernel_rfl`. -/ +abbrev MethodNames.Spell (names : MethodNames) (method : RawMethod) : Prop := + names.spelling = some (method.owner, method.objectBases) + +/-- Whether each base class name is the bytes of the object type at its position. -/ +def basesSpelled : List (List Nat × List Nat) → List TypeExpr → Bool + | [], [] => true + | (scope, name) :: bases, .object scopeText nameText :: types => + scope == bytesOf scopeText && name == bytesOf nameText && basesSpelled bases types + | _, _ => false + +/-- Whether the names are the bytes of the owner and base classes the method states. A decision + over bytes, so a method whose strings are stored as bytes proves `Spell` without the kernel + encoding them. -/ +def MethodNames.spelledBy (names : MethodNames) (method : RawMethod) : Bool := + names.owner == bytesOf method.owner && basesSpelled names.bases method.objectBases + +private theorem bases_of_basesSpelled : ∀ (bases : List (List Nat × List Nat)) + (types : List TypeExpr), basesSpelled bases types = true → + bases.mapM (fun entry => (spelled? entry.1).bind fun scope => + (spelled? entry.2).bind fun name => pure (TypeExpr.object scope name)) = some types + | [], [], _ => rfl + | (scope, name) :: bases, .object scopeText nameText :: types, spelled => by + simp only [basesSpelled, Bool.and_eq_true, beq_iff_eq] at spelled + obtain ⟨⟨rfl, rfl⟩, rest⟩ := spelled + simp only [List.mapM_cons, spelled?_bytesOf, Option.bind_some] + rw [bases_of_basesSpelled bases types rest] + rfl + +/-- Names whose bytes are those of the method's owner and base classes spell them. -/ +theorem MethodNames.spell_of_spelledBy {names : MethodNames} {method : RawMethod} + (spelled : names.spelledBy method = true) : names.Spell method := by + simp only [MethodNames.spelledBy, Bool.and_eq_true, beq_iff_eq] at spelled + obtain ⟨owner, bases⟩ := spelled + simp only [MethodNames.Spell, MethodNames.spelling, owner, spelled?_bytesOf] + simp only [bind] + rw [bases_of_basesSpelled _ _ bases] + rfl +#print axioms MethodNames.spell_of_spelledBy + +/-- The names spell `signature`, the string a proof selects the method by. An equation between a + string and bytes, so an extraction proves it with `cil_kernel_rfl`. -/ +abbrev MethodNames.Selects (names : MethodNames) (signature : String) : Prop := + spelled? names.signature = some signature + +/-- The `MethodDef` flag of an abstract method, which has no body. -/ +def abstractMethodFlag : Nat := 0x0400 + +/-- The `MethodDef` flag of a platform invoke, whose body is native code outside the image. -/ +def platformInvokeFlag : Nat := 0x2000 + +/-- The `MethodDef` implementation flag of a method the runtime implements itself. -/ +def internalCallFlag : Nat := 0x1000 + +/-- The `MethodDef` implementation flag of a runtime-async method, whose body returns the result + its signature states as a task. -/ +def runtimeAsyncFlag : Nat := 0x2000 + +/-- Whether a `MethodDef` row's flags say the method has no IL body in the image. -/ +def hasNoBody (flags implFlags : Nat) : Bool := + implFlags % 4 != 0 || flags / abstractMethodFlag % 2 == 1 || + flags / platformInvokeFlag % 2 == 1 || implFlags / internalCallFlag % 2 == 1 + +/-- `.ctor` and `.cctor` as bytes. -/ +def constructorName : List Nat := [46, 99, 116, 111, 114] +def classConstructorName : List Nat := [46, 99, 99, 116, 111, 114] + +/-- Whether a `MethodDef` row is a constructor as Cecil reads one: its special-name and + runtime-special-name flags are set and it is named `.ctor` or `.cctor`. -/ +def isConstructorRow (flags : Nat) (name : List Nat) : Bool := + flags / 0x800 % 2 == 1 && flags / 0x1000 % 2 == 1 && + (name == constructorName || name == classConstructorName) + +/-- Cecil's full name of the method a `MethodDef` row defines, instantiated at `instantiation`, + method arguments first: the result and parameters substituted as Cecil substitutes them, and + an instantiated method other than a constructor named with its arguments. An empty + instantiation leaves the declared types, which then must name no generic parameter. -/ +def instanceSignature (owner name : List Nat) (constructor : Bool) (arity : Nat) + (instantiation : List ResolvedType) (returned : ResolvedType) + (parameters : List ResolvedType) : Option (List Nat) := do + let methodArguments := instantiation.take arity + let ownerArguments := instantiation.drop arity + let spell (type : ResolvedType) : Option (List Nat) := + instanceType methodArguments ownerArguments type >>= cecilName true resolveFuel + let result ← spell returned + let spelledParameters ← parameters.mapM spell + let named ← + if instantiation.isEmpty || constructor then pure name + else (fun spelled => name ++ [60] ++ spelled ++ [62]) <$> + cecilNames true resolveFuel (ResolvedList.ofList instantiation) + pure (methodFullName result owner named spelledParameters) + +/-- Check the method definition against its `MethodDef` row: flags, name, signature, generic + parameters, owner and class layout facts, and return the names it resolves, which the caller + ties to the extractor's strings. Each closed argument resolves from `sources` and must + admit the corresponding type in `arguments`. The row's relative virtual + address, which the extractor zeroes in the rows it carries, and its parameter list are not + read: where a body sits in the image is the strict audit's to confirm. A modified return or + parameter is refused, except an `init` accessor's void and an `in` reference. -/ +def methodIdentityValid (env : MetadataEnv) (method : RawMethod) + (sources : List InstanceArgument) (arguments : List TypeExpr) : + Except MetadataError MethodNames := do + let index ← env.slice 0 method.token + if metadataTableOf method.token != Table.methodDef then + throw (.unsupportedTable method.token) + let row ← index.methodDef method.token + if row.flags != method.attributes || row.implFlags != method.implementation then + throw (.methodMismatch method.token) + -- A row with native, runtime or no code has no IL body for the decoded bytes to be. + if hasNoBody row.flags row.implFlags then throw (.bodyLocation method.token) + -- A runtime-async body returns its task's result, which the model would read as the task itself. + if row.implFlags / runtimeAsyncFlag % 2 == 1 then throw (.runtimeAsync method.token) + let signature ← decodeMethodSig (← index.blob row.signature) + let convention := signature.convention + -- EXPLICITTHIS puts the receiver in the parameter list, outside the implicit receiver model. + if method.explicitThis || method.hasThis != (convention / hasThisFlag % 2 == 1) || + method.explicitThis != (convention / explicitThisFlag % 2 == 1) || + method.callingConvention != convention % 0x20 then throw (.methodMismatch method.token) + let ownerToken ← env.declaringType 0 method.token + let owner ← typeDefNamed env 0 resolveFuel ownerToken + let methodParameters ← index.methodGenericParameters 0 method.token signature.genericArity + let ownerParameters ← index.genericParameters 0 ownerToken + if methodParameters.length != signature.genericArity || + !flagsAgree methodParameters method.genericParameters || + !flagsAgree ownerParameters method.ownerParameters || method.ownerArgumentCount != 0 then + throw (.methodMismatch method.token) + let frame ← env.frame method.genericParameters.length method.ownerParameters.length + let returned ← env.resolve 0 signature.result + let normalizedReturn := declaredReturn returned + if hasModifier resolveFuel normalizedReturn then throw (.methodMismatch method.token) + let parameters ← env.resolveAll 0 signature.parameters + -- An argument read narrows by the declared type, so no parameter may hide a modifier in an atom. + if parameters.any (hasModifier resolveFuel ∘ declaredParameter) then + throw (.methodMismatch method.token) + if !(frame.admits normalizedReturn method.returnType) || + !parametersAdmitted frame (parameters.map declaredParameter) method.parameters then + throw (.methodMismatch method.token) + -- A row with generic parameters is extracted only as an instance naming an argument for each. + let instantiation ← sources.mapM (InstanceArgument.resolve env method.token resolveFuel) + if instantiation.length != arguments.length || + instantiation.length != methodParameters.length + ownerParameters.length || + !(instantiation.zip arguments).all (fun (type, claimed) => frame.admits type claimed) then + throw (.genericMismatch method.token) + let name ← index.string row.name + let some selected := instanceSignature owner.name name (isConstructorRow row.flags name) + signature.genericArity instantiation returned parameters + | throw (.methodMismatch method.token) + let ownerName := owner.scope ++ [58, 58] ++ owner.name + let receiverMatches := + if method.hasThis then + match owner.kind with + | .classType => method.objectReceiver + | .valueType | .enumType _ => !method.objectReceiver + | .interfaceType | .importedOwner | .external _ => false + else !method.objectReceiver + if !receiverMatches then throw (.methodMismatch method.token) + if method.objectReceiver then + let chain ← env.classChain resolveFuel 0 ownerToken + let some own := index.instanceTotal.find? ownerToken | throw (.missingRow ownerToken) + let bases ← chain.mapM fun (position, token) => typeDefNamed env position resolveFuel token + if method.objectFieldCount != (← env.chainFieldCount chain) + own then + throw (.methodMismatch method.token) + .ok { owner := ownerName, bases := bases.map fun base => (base.scope, base.name), + signature := selected } + else if method.objectFieldCount != 0 then throw (.methodMismatch method.token) + else .ok { owner := ownerName, signature := selected } + +end CIL.Extraction diff --git a/CIL/Extraction/MetadataBytes.lean b/CIL/Extraction/MetadataBytes.lean new file mode 100644 index 000000000..d76923685 --- /dev/null +++ b/CIL/Extraction/MetadataBytes.lean @@ -0,0 +1,1220 @@ +import CIL.Extraction.KernelEquation +import CIL.Extraction.ByteLiterals +import CIL.Extraction.MetadataToken + +/-! +# Metadata tables and heaps + +An extraction carries the bytes of the metadata rows and heap entries a method uses, with one +small header block per pinned assembly. This file reads those bytes by ECMA-335 II.22 and II.24: +the table stream header, the widths it fixes for every column, the rows of the tables the +checker decodes, and the entries of the `#Strings`, `#Blob` and `#US` heaps. Each slice builds +one balanced search tree per kind, so a token costs one lookup. + +Only the carried bytes are decoded. Whether they are the image's bytes at those positions is the +extractor's claim: every row must still have the length the header's widths give, every blob its +length prefix and every string one terminating zero. +-/ + +namespace CIL.Extraction + +/-- A heap entry or table row: its key (a heap offset, or a token) and its bytes. -/ +abbrev ByteEntry := Nat × List Nat + +/-- Why metadata bytes, or a claim about them, are refused. -/ +inductive MetadataError where + | invalidByte + | duplicateEntry (key : Nat) + | unsupportedTableStream (detail : Nat) + | malformedTableHeader + | tokenOutOfScope (token : Nat) + | missingRow (token : Nat) + | unsupportedTable (token : Nat) + | rowLength (token expected actual : Nat) + | badCodedIndex (value : Nat) + | rowOverflow (table row : Nat) + /-- An enclosing TypeDef is nil or does not precede its nested TypeDef (II.22). -/ + | invalidNesting (nested enclosing : Nat) + | missingHeap (heap offset : Nat) + | malformedHeap (heap offset : Nat) + | malformedUserString (offset : Nat) + | truncatedSignature (offset : Nat) + | unsupportedSignature (offset element : Nat) + | unsupportedAssemblyRef (token : Nat) + | unsupportedScope (token : Nat) + | unsupportedBase (token : Nat) + | unresolvedType (token : Nat) + | unresolvedMember (token : Nat) + | operandMismatch (token : Nat) + | scopeMismatch (token : Nat) + | genericMismatch (token : Nat) + | fieldIndexMismatch (token stated computed : Nat) + /-- An instance field whose owner no carried slice defines, other than an item of the platform + pair: no carried metadata gives its position among the owner's fields. -/ + | unboundInstanceField (token : Nat) + /-- A static field whose owner no carried slice defines, other than the platform constant + field: no carried row is the field the static site at its use stands for. -/ + | unboundStaticField (token : Nat) + /-- A static field named through a generic instantiation of its owner: each closed + instantiation has its own storage, and the model keeps one per field. -/ + | instantiatedStaticField (token : Nat) + | localMismatch (index : Nat) + | methodMismatch (token : Nat) + | identityMismatch + /-- A reference into a pinned assembly whose slice the evidence does not carry. -/ + | missingSlice (token : Nat) + | bodyLocation (token : Nat) + /-- A runtime-async method, whose body the model does not read. -/ + | runtimeAsync (token : Nat) + | tablesMismatch + /-- A type no carried slice defines whose platform definition the checks do not hold. -/ + | missingPlatformDefinition (token : Nat) + /-- A platform definition of another kind than the reading of the token relies on. -/ + | platformKindMismatch (token : Nat) + deriving DecidableEq, Repr + + + +/-- The heap number `MetadataError.missingHeap` reports for `#Strings`. -/ +def stringsHeap : Nat := 0 + +/-- The heap number `MetadataError.missingHeap` reports for `#Blob`. -/ +def blobHeap : Nat := 1 + +/-- The heap number `MetadataError.missingHeap` reports for `#US`. -/ +def userStringHeap : Nat := 2 + +/-! The metadata table numbers of II.22 that the checker reads; a user string is table `0x70` + and a standalone signature is table `0x11`. -/ +namespace Table +def module : Nat := 0x00 +def typeRef : Nat := 0x01 +def typeDef : Nat := 0x02 +def field : Nat := 0x04 +def methodDef : Nat := 0x06 +def memberRef : Nat := 0x0A +def standAloneSig : Nat := 0x11 +def moduleRef : Nat := 0x1A +def typeSpec : Nat := 0x1B +def assembly : Nat := 0x20 +def assemblyRef : Nat := 0x23 +def exportedType : Nat := 0x27 +def nestedClass : Nat := 0x29 +def genericParam : Nat := 0x2A +def methodSpec : Nat := 0x2B +def userString : Nat := 0x70 +end Table + +/-! ## Compressed integers and little-endian values -/ + +/-- The unsigned compressed integer at the front of the bytes (II.23.2) and the bytes after + it; a first byte of `0xE0` or more is not one. -/ +def decodeCompressed : List Nat → Option (Nat × List Nat) + | [] => none + | first :: rest => + if first < 0x80 then some (first, rest) + else if first < 0xC0 then + match rest with + | second :: tail => some ((first - 0x80) * 0x100 + second, tail) + | [] => none + else if first < 0xE0 then + match rest with + | second :: third :: fourth :: tail => + some ((((first - 0xC0) * 0x100 + second) * 0x100 + third) * 0x100 + fourth, tail) + | _ => none + else none + +/-- The little-endian value of the next `width` bytes, and the bytes after them. -/ +def readLittle : Nat → List Nat → Option (Nat × List Nat) + | 0, rest => some (0, rest) + | width + 1, byte :: rest => do + let (high, tail) ← readLittle width rest + some (byte + 0x100 * high, tail) + | _ + 1, [] => none + +/-- Split bytes into little-endian columns of the given widths; the bytes must be used up. -/ +def splitColumns : List Nat → List Nat → Option (List Nat) + | [], [] => some [] + | [], _ :: _ => none + | width :: widths, bytes => do + let (value, rest) ← readLittle width bytes + let values ← splitColumns widths rest + some (value :: values) + +/-! ## The table stream header -/ + +/-- How many bytes each kind of column takes, fixed by the row counts and heap sizes. -/ +structure ColumnWidths where + string : Nat + blob : Nat + field : Nat + methodDef : Nat + param : Nat + typeDef : Nat + genericParam : Nat + typeDefOrRef : Nat + resolutionScope : Nat + implementation : Nat + memberRefParent : Nat + methodDefOrRef : Nat + typeOrMethodDef : Nat + deriving DecidableEq, Repr + +/-- The decoded table stream header: heap sizes, the row count of each present table and the + column widths they imply. -/ +structure TableHeader where + heapSizes : Nat + /-- The table number and row count of each present table, in table order. -/ + rowCounts : List (Nat × Nat) + widths : ColumnWidths + deriving DecidableEq, Repr + +/-- The row count of a table; an absent table has none. -/ +def TableHeader.rows (header : TableHeader) (table : Nat) : Nat := + (header.rowCounts.lookup table).getD 0 + +/-- The tables whose presence makes the header unsupported: the pointer tables of an + uncompressed stream and the edit-and-continue tables. -/ +def unsupportedTables : List Nat := [0x03, 0x05, 0x07, 0x13, 0x16, 0x1E, 0x1F] + +/-- The width of an index into a table with `rows` rows. -/ +def simpleWidth (rows : Nat) : Nat := if rows < 0x10000 then 2 else 4 + +/-- The width of a coded index of `bits` tag bits over tables with at most `rows` rows. -/ +def codedWidth (bits rows : Nat) : Nat := if rows < 2 ^ (16 - bits) then 2 else 4 + +/-- The column widths of a header, as II.24.2.6 and II.22 define them. -/ +def columnWidthsOf (heapSizes : Nat) (rows : Nat → Nat) : ColumnWidths := + { string := if heapSizes % 2 == 1 then 4 else 2, + blob := if heapSizes / 4 % 2 == 1 then 4 else 2, + field := simpleWidth (rows 0x04), methodDef := simpleWidth (rows 0x06), + param := simpleWidth (rows 0x08), typeDef := simpleWidth (rows 0x02), + genericParam := simpleWidth (rows 0x2A), + typeDefOrRef := codedWidth 2 (max (rows 0x02) (max (rows 0x01) (rows 0x1B))), + implementation := codedWidth 2 (max (rows 0x26) (max (rows 0x23) (rows 0x27))), + resolutionScope := codedWidth 2 + (max (rows 0x00) (max (rows 0x1A) (max (rows 0x23) (rows 0x01)))), + memberRefParent := codedWidth 3 + (max (rows 0x02) (max (rows 0x01) (max (rows 0x1A) (max (rows 0x06) (rows 0x1B))))), + methodDefOrRef := codedWidth 1 (max (rows 0x06) (rows 0x0A)), + typeOrMethodDef := codedWidth 1 (max (rows 0x02) (rows 0x06)) } + +/-- The row counts of the tables flagged in `valid`, read in table order from `bytes`. The + count of tables left to examine is `fuel`; every byte must be used. -/ +private def readRowCounts (valid : Nat) : + Nat → Nat → List Nat → Except MetadataError (List (Nat × Nat)) + | 0, _, [] => .ok [] + | 0, _, _ :: _ => .error .malformedTableHeader + | fuel + 1, table, bytes => + if valid / 2 ^ table % 2 == 1 then + match readLittle 4 bytes with + | some (count, rest) => do + let counts ← readRowCounts valid fuel (table + 1) rest + .ok ((table, count) :: counts) + | none => .error .malformedTableHeader + else readRowCounts valid fuel (table + 1) bytes + +/-- The header with these heap sizes and present tables' row counts, and the widths they fix. -/ +def TableHeader.ofCounts (heapSizes : Nat) (rowCounts : List (Nat × Nat)) : TableHeader := + { heapSizes := heapSizes, rowCounts := rowCounts, + widths := columnWidthsOf heapSizes fun table => (rowCounts.lookup table).getD 0 } + +/-- The number of tables in a 64-bit validity mask. -/ +private def tableSlots : Nat := 64 + +/-- The `HeapSizes` byte, the `Valid` mask and the first row count sit at fixed offsets of the + table stream: 6, 8 and 24 (II.24.2.6). -/ +private def heapSizesOffset : Nat := 6 +private def validOffset : Nat := 8 +private def tableHeaderFixedSize : Nat := 24 + +/-- Decode the table stream header (II.24.2.6): the bytes from the reserved word through the + last row count. Heap-size bits outside the three index flags, and pointer or + edit-and-continue tables, are unsupported. -/ +def decodeTableHeader (bytes : List Nat) : Except MetadataError TableHeader := + if !bytes.all (· < 256) then .error .invalidByte + else if bytes.length < tableHeaderFixedSize then .error .malformedTableHeader + else + let heapSizes := bytes.getD heapSizesOffset 0 + match readLittle 8 (bytes.drop validOffset) with + | some (valid, _) => + if heapSizes ≥ 8 then .error (.unsupportedTableStream heapSizes) + else if unsupportedTables.any (fun table => valid / 2 ^ table % 2 == 1) then + .error (.unsupportedTableStream 0x100) + else do + let present ← readRowCounts valid tableSlots 0 (bytes.drop tableHeaderFixedSize) + .ok (TableHeader.ofCounts heapSizes present) + | none => .error .malformedTableHeader + +/-- The metadata an assembly's own identity is read from, shared by every method of the + assembly: its table stream header, its `Assembly` row and the heap entries the row names. -/ +structure AssemblyMetadata where + /-- The pin identity the bytes must spell, with the public key token as the pin writes it. -/ + identity : String := "" + /-- The public key token the pin writes, as sixteen lowercase hexadecimal digits, when the + `Assembly` row has a public key; empty otherwise. -/ + keyToken : List Nat := [] + /-- The table stream bytes from the reserved word through the last row count. -/ + tableHeader : List Nat := [] + /-- The header `tableHeader` decodes to, which every method of the assembly reads its rows + by; `AssemblyIdentified` proves it once per assembly. -/ + header : TableHeader := (decodeTableHeader tableHeader).toOption.getD (.ofCounts 0 []) + /-- Row 1 of the `Assembly` table. -/ + assemblyRow : List Nat := [] + strings : List ByteEntry := [] + blobs : List ByteEntry := [] + deriving DecidableEq, Repr + +/-- A fact the extractor states about a token of a slice, which the checker verifies against + the carried rows: `kind` says what `target` is, in the slice with index `slice`. -/ +structure MetadataLink where + kind : Nat + source : Nat + slice : Nat + target : Nat + deriving DecidableEq, Repr + +/-- A link names the definition a `TypeRef` or `MemberRef` token resolves to. -/ +def linkDefinition : Nat := 0 + +/-- A link names the `TypeDef` that declares a `Field` or `MethodDef` token. -/ +def linkDeclaring : Nat := 1 + +/-- A link names the `NestedClass` row of a nested `TypeDef` token. -/ +def linkNesting : Nat := 2 + +/-- A link names the first `GenericParam` row of a `TypeDef` or `MethodDef` token. -/ +def linkGenericParams : Nat := 3 + +/-- A TypeRef crosses assemblies through this ExportedType row in its named facade. -/ +def linkForwarder : Nat := 4 + +/-- The rows and heap entries one method's tokens, definition and locals reach in one + assembly, with the links that locate definitions among them. -/ +structure MetadataSlice where + assembly : AssemblyMetadata := {} + /-- Table rows keyed by token: the table number times `2 ^ 24` plus the row number. -/ + rows : List ByteEntry := [] + /-- `#Strings` entries keyed by offset, through the terminating zero. -/ + strings : List ByteEntry := [] + /-- `#Blob` entries keyed by offset, including the compressed length. -/ + blobs : List ByteEntry := [] + /-- `#US` entries keyed by offset, including the length and the terminal byte. -/ + userStrings : List ByteEntry := [] + /-- The `AssemblyRef` row of the assembly that defines the primitive types, or 0. -/ + corlib : Nat := 0 + links : List MetadataLink := [] + deriving DecidableEq, Repr + +/-- The metadata of one method body: the slice of its own assembly, which comes first, and the + slices of the other pinned assemblies its tokens resolve into. -/ +structure MethodMetadata where + own : MetadataSlice := {} + others : List MetadataSlice := [] + deriving DecidableEq, Repr + +/-! ## Rows -/ + +/-- The widths of the columns of a decoded table's row, or `none` for a table the checker does + not read. The `Module` table is not read: its columns are not needed. -/ +def columnWidths (widths : ColumnWidths) : Nat → Option (List Nat) + | 0x01 => some [widths.resolutionScope, widths.string, widths.string] + | 0x02 => some [4, widths.string, widths.string, widths.typeDefOrRef, widths.field, + widths.methodDef] + | 0x04 => some [2, widths.string, widths.blob] + | 0x06 => some [4, 2, 2, widths.string, widths.blob, widths.param] + | 0x0A => some [widths.memberRefParent, widths.string, widths.blob] + | 0x11 => some [widths.blob] + | 0x1B => some [widths.blob] + | 0x20 => some [4, 2, 2, 2, 2, 4, widths.blob, widths.string, widths.string] + | 0x23 => some [2, 2, 2, 2, 4, widths.blob, widths.string, widths.string, widths.blob] + | 0x27 => some [4, 4, widths.string, widths.string, widths.implementation] + | 0x29 => some [widths.typeDef, widths.typeDef] + | 0x2A => some [2, 2, widths.typeOrMethodDef, widths.string] + | 0x2B => some [widths.methodDefOrRef, widths.blob] + | _ => none + +/-- The byte length of a row of a table the checker reads. -/ +def rowSize (widths : ColumnWidths) (table : Nat) : Option Nat := + (columnWidths widths table).map List.sum + +/-- A coded index as a token: the tag picks the table, the rest is the row. -/ +def decodeCoded (tags : List Nat) (bits value : Nat) : Except MetadataError Nat := + match tags[value % 2 ^ bits]? with + | some table => checkedTokenOf table (value / 2 ^ bits) fun _ _ => .badCodedIndex value + | none => .error (.badCodedIndex value) + +/-- The tables a `TypeDefOrRef` coded index names, by tag. -/ +def typeDefOrRefTags : List Nat := [0x02, 0x01, 0x1B] +/-- The tables a `ResolutionScope` coded index names, by tag. -/ +def resolutionScopeTags : List Nat := [0x00, 0x1A, 0x23, 0x01] +/-- The tables a `MemberRefParent` coded index names, by tag. -/ +def memberRefParentTags : List Nat := [0x02, 0x01, 0x1A, 0x06, 0x1B] +/-- The tables a `MethodDefOrRef` coded index names, by tag. -/ +def methodDefOrRefTags : List Nat := [0x06, 0x0A] +/-- The tables a `TypeOrMethodDef` coded index names, by tag. -/ +def typeOrMethodDefTags : List Nat := [0x02, 0x06] + +/-- A `TypeRef` row: where its type lives, its name and its namespace. -/ +structure TypeRefRow where + scope : Nat + name : Nat + namespace_ : Nat + deriving DecidableEq, Repr + +/-- A `TypeDef` row. `extends_` is a token, or the row-0 token of table `0x02` for no base. -/ +structure TypeDefRow where + flags : Nat + name : Nat + namespace_ : Nat + extends_ : Nat + fieldList : Nat + methodList : Nat + deriving DecidableEq, Repr + +/-- A `Field` row. -/ +structure FieldRow where + flags : Nat + name : Nat + signature : Nat + deriving DecidableEq, Repr + +/-- A `MethodDef` row. -/ +structure MethodDefRow where + rva : Nat + implFlags : Nat + flags : Nat + name : Nat + signature : Nat + paramList : Nat + deriving DecidableEq, Repr + +/-- A `MemberRef` row: its parent as a token, its name and its signature blob. -/ +structure MemberRefRow where + parent : Nat + name : Nat + signature : Nat + deriving DecidableEq, Repr + +/-- An `AssemblyRef` row. -/ +structure AssemblyRefRow where + major : Nat + minor : Nat + build : Nat + revision : Nat + flags : Nat + publicKeyOrToken : Nat + name : Nat + culture : Nat + deriving DecidableEq, Repr + +/-- An `Assembly` row. -/ +structure AssemblyRow where + major : Nat + minor : Nat + build : Nat + revision : Nat + flags : Nat + publicKey : Nat + name : Nat + culture : Nat + deriving DecidableEq, Repr + +/-- A `NestedClass` row. -/ +structure NestedClassRow where + nested : Nat + enclosing : Nat + deriving DecidableEq, Repr + +/-- A `GenericParam` row: its position, flags and owner token. -/ +structure GenericParamRow where + number : Nat + flags : Nat + owner : Nat + name : Nat + deriving DecidableEq, Repr + +/-- A `MethodSpec` row: the instantiated method and its instantiation blob. -/ +structure MethodSpecRow where + method : Nat + instantiation : Nat + deriving DecidableEq, Repr + +/-! ## Search trees -/ + +/-- A binary search tree keyed by number, built balanced from entries in ascending key order, so a + lookup costs the logarithm of the entry count and the tree can be written out as a literal. -/ +inductive SearchTree (α : Type) where + | leaf + | node (left : SearchTree α) (key : Nat) (value : α) (right : SearchTree α) + deriving DecidableEq, Repr + +namespace SearchTree + +variable {α : Type} + +/-- The value stored under `wanted`. -/ +def find? : SearchTree α → Nat → Option α + | .leaf, _ => none + | .node left key value right, wanted => + match compare wanted key with + | .lt => find? left wanted + | .gt => find? right wanted + | .eq => some value + +/-- The entry with the least key above `wanted`. -/ +def above? : SearchTree α → Nat → Option (Nat × α) + | .leaf, _ => none + | .node left key value right, wanted => + if wanted < key then + match above? left wanted with + | some entry => some entry + | none => some (key, value) + else above? right wanted + +/-- The entries in ascending key order. -/ +def toList : SearchTree α → List (Nat × α) + | .leaf => [] + | .node left key value right => left.toList ++ (key, value) :: right.toList + +/-- The tree of the first `count` entries, taking `count / 2` of them to the left of the root, and + the entries left over. `fuel` bounds the depth. -/ +private def build : Nat → Nat → List (Nat × α) → SearchTree α × List (Nat × α) + | 0, _, rest => (.leaf, rest) + | fuel + 1, count, rest => + if count == 0 then (.leaf, rest) + else + let leftCount := count / 2 + let (left, afterLeft) := build fuel leftCount rest + match afterLeft with + | [] => (left, []) + | (key, value) :: afterRoot => + let (right, remaining) := build fuel (count - 1 - leftCount) afterRoot + (.node left key value right, remaining) + +/-- The balanced tree of entries in ascending key order. -/ +def ofSorted (entries : List (Nat × α)) : SearchTree α := + (build entries.length entries.length entries).1 + +/-- The first key that does not ascend strictly from its predecessor. -/ +def firstOutOfOrder : Option Nat → List (Nat × α) → Option Nat + | _, [] => none + | none, (key, _) :: rest => firstOutOfOrder (some key) rest + | some previous, (key, _) :: rest => + if previous < key then firstOutOfOrder (some key) rest else some key + +/-- The balanced tree of the entries, which must have strictly ascending keys. -/ +def ofEntries (entries : List (Nat × α)) : Except MetadataError (SearchTree α) := + match firstOutOfOrder none entries with + | some key => .error (.duplicateEntry key) + | none => .ok (ofSorted entries) + +end SearchTree + +/-- The lookup structures of one slice: one tree per kind of entry, and the instance field counts + derived from the rows. They cost the size of the slice to build, so an extraction carries them + as literals that one theorem equates with `MetadataSlice.tables`. -/ +structure IndexTables where + rows : SearchTree (List Nat) + strings : SearchTree (List Nat) + blobs : SearchTree (List Nat) + userStrings : SearchTree (List Nat) + /-- Links keyed by `kind * 2 ^ 32 + source`, giving the slice index and target token. -/ + links : SearchTree (Nat × Nat) + /-- For each Field row of a carried type, the instance fields before it in its type. -/ + instanceBefore : SearchTree Nat + /-- For each carried TypeDef whose fields are all carried, its number of instance fields. -/ + instanceTotal : SearchTree Nat + /-- Each owner's first carried generic parameter token and number of carried parameters, keyed + by the owner's coded value (`genericOwnerCode`), the order of the `GenericParam` table. -/ + genericOwners : SearchTree (Nat × Nat) + deriving DecidableEq, Repr + +/-! ## One slice, indexed -/ +/-- One slice with a tree per kind of entry, so a token or offset is a single lookup. -/ +structure MetadataIndex extends IndexTables where + header : TableHeader + corlib : Nat + /-- The identity of the assembly the slice is cut from, as the `Assembly` row spells it. -/ + identity : List Nat + +/-- The entries as a tree keyed by offset or token; they must be bytes with ascending keys. -/ +def indexEntries (entries : List ByteEntry) : Except MetadataError (SearchTree (List Nat)) := + if !entries.all (fun entry => entry.2.all (· < 256)) then .error .invalidByte + else SearchTree.ofEntries entries +/-- The bytes of a string literal, for comparison with heap entries. -/ +def bytesOf (text : String) : List Nat := text.toByteArray.data.toList.map UInt8.toNat + +/-- A name tied to bytes by `spelled?` has exactly those bytes, so an equation + `spelled? bytes = some name` accepts the same names as comparing `bytesOf name` with them. -/ +theorem bytesOf_of_spelled {bytes : List Nat} {text : String} + (spelling : spelled? bytes = some text) : bytesOf text = bytes := by + obtain ⟨characters, decoded, rfl⟩ := Option.map_eq_some_iff.mp spelling + simp [bytesOf, String.toByteArray_ofList, List.utf8Encode, List.map_flatMap] + exact utf8Characters_encode _ _ _ decoded +#print axioms bytesOf_of_spelled + +/-- Every string spells itself: a `String` holds only scalar values, so its bytes read back as the + string, whatever its characters. -/ +theorem spelled?_bytesOf (text : String) : spelled? (bytesOf text) = some text := by + obtain ⟨characters, rfl⟩ : ∃ characters, text = String.ofList characters := + ⟨text.toList, String.ofList_toList.symm⟩ + have bytes : bytesOf (String.ofList characters) = + characters.flatMap fun character => (String.utf8EncodeChar character).map UInt8.toNat := by + simp [bytesOf, String.toByteArray_ofList, List.utf8Encode, List.map_flatMap] + rw [spelled?, bytes, utf8Characters_decode _ _ (Nat.le_refl _)] + rfl +#print axioms spelled?_bytesOf + +/-- Bytes that spell a string are that string's byte array. -/ +theorem byteArray_of_spelled {bytes : List Nat} {text : String} + (spelling : spelled? bytes = some text) : + (⟨(bytes.map UInt8.ofNat).toArray⟩ : ByteArray) = text.toByteArray := by + have stored := bytesOf_of_spelled spelling + subst stored + show _ = (⟨text.toByteArray.data⟩ : ByteArray) + congr 1 + simp [bytesOf, List.map_map, Function.comp_def] +#print axioms byteArray_of_spelled + +/-- The string `bytes` spell, stored as those bytes. `bytesOf` of it reads the stored bytes and + never encodes `text`; the kernel decides `spelling` once, where the string is defined. -/ +def byteString (bytes : List Nat) (text : String) (spelling : spelled? bytes = some text) : + String := + { toByteArray := ⟨(bytes.map UInt8.ofNat).toArray⟩, + isValidUTF8 := byteArray_of_spelled spelling ▸ text.isValidUTF8 } + +/-- A byte-backed string is the string its bytes spell. -/ +theorem byteString_eq {bytes : List Nat} {text : String} (spelling : spelled? bytes = some text) : + byteString bytes text spelling = text := by + have stored := byteArray_of_spelled spelling + unfold byteString + revert stored spelling + cases text with + | ofByteArray data valid => + intro spelling stored + simp only at stored + subst stored + rfl +#print axioms byteString_eq + +/-- The entry of a heap map: the bytes, or `missingHeap`. -/ +def heapEntry (heap : Nat) (entries : SearchTree (List Nat)) (offset : Nat) : + Except MetadataError (List Nat) := + match entries.find? offset with + | some bytes => .ok bytes + | none => .error (.missingHeap heap offset) + +/-- A `#Strings` entry without its terminating zero; the entry must have exactly one zero, + at its end. -/ +def stringBytes (entries : SearchTree (List Nat)) (offset : Nat) : + Except MetadataError (List Nat) := do + let bytes ← heapEntry stringsHeap entries offset + match bytes.reverse with + | 0 :: reversed => + if reversed.contains 0 then .error (.malformedHeap stringsHeap offset) + else .ok reversed.reverse + | _ => .error (.malformedHeap stringsHeap offset) + +/-- A `#Blob` entry without its length prefix; the prefix must give the remaining length. -/ +def blobBytes (entries : SearchTree (List Nat)) (offset : Nat) : + Except MetadataError (List Nat) := do + let bytes ← heapEntry blobHeap entries offset + match decodeCompressed bytes with + | some (length, rest) => + if rest.length == length then .ok rest else .error (.malformedHeap blobHeap offset) + | none => .error (.malformedHeap blobHeap offset) + +/-- The bytes of the UTF-16 code units of a string from its UTF-8 bytes. -/ +def utf16Units : Nat → List Nat → Option (List Nat) + | _, [] => some [] + | 0, _ :: _ => none + | fuel + 1, first :: rest => + if first < 0x80 then (first :: ·) <$> utf16Units fuel rest + else if first < 0xC0 then none + else if first < 0xE0 then + match rest with + | second :: tail => + if second / 0x40 != 2 then none + else ((first % 0x20 * 0x40 + second % 0x40) :: ·) <$> utf16Units fuel tail + | [] => none + else if first < 0xF0 then + match rest with + | second :: third :: tail => + let point := (first % 0x10 * 0x40 + second % 0x40) * 0x40 + third % 0x40 + if second / 0x40 != 2 || third / 0x40 != 2 || point < 0x800 then none + else ((point :: ·) <$> utf16Units fuel tail) + | _ => none + else + match rest with + | second :: third :: fourth :: tail => + let point := ((first % 0x08 * 0x40 + second % 0x40) * 0x40 + third % 0x40) * 0x40 + + fourth % 0x40 + if second / 0x40 != 2 || third / 0x40 != 2 || fourth / 0x40 != 2 || + point < 0x10000 || point > 0x10FFFF then none + else + let offset := point - 0x10000 + (fun units => (0xD800 + offset / 0x400) :: (0xDC00 + offset % 0x400) :: units) <$> + utf16Units fuel tail + | _ => none + +/-- Whether a UTF-16 code unit sets the terminal flag of a `#US` entry (II.24.2.4). -/ +def userStringFlag (unit : Nat) : Bool := + unit ≥ 0x100 || (0x01 ≤ unit && unit ≤ 0x08) || (0x0E ≤ unit && unit ≤ 0x1F) || + unit == 0x27 || unit == 0x2D || unit == 0x7F + +/-- The little-endian 16-bit units of an even number of bytes. -/ +private def littleUnits : Nat → List Nat → Option (List Nat) + | _, [] => some [] + | 0, _ :: _ => none + | fuel + 1, low :: high :: tail => ((low + 0x100 * high) :: ·) <$> littleUnits fuel tail + | _ + 1, [_] => none + +/-- The UTF-16 code units of a `#US` entry. Its length counts the units' bytes and the + terminal byte, which is 1 exactly when a unit needs the flag. -/ +def userStringUnits (entries : SearchTree (List Nat)) (offset : Nat) : + Except MetadataError (List Nat) := do + let bytes ← heapEntry userStringHeap entries offset + let some (length, rest) := decodeCompressed bytes + | throw (.malformedHeap userStringHeap offset) + if rest.length != length then throw (.malformedHeap userStringHeap offset) + let payload := rest.dropLast + let terminal := rest.getLast?.getD 2 + let some units := littleUnits payload.length payload + | throw (.malformedUserString offset) + let flagged := units.any userStringFlag + if terminal != (if flagged then 1 else 0) then throw (.malformedUserString offset) + .ok units + +/-! ## Rows by token -/ + +/-- The row bytes of a token: the token must be in scope of the header, the slice must carry + its row, the table must be one the checker reads and the row must have the length the + header's widths give. -/ +def MetadataIndex.row (index : MetadataIndex) (token : Nat) : Except MetadataError (List Nat) := + let table := metadataTableOf token + if tokenRow token == 0 || index.header.rows table < tokenRow token then + .error (.tokenOutOfScope token) + else match columnWidths index.header.widths table, index.rows.find? token with + | none, _ => .error (.unsupportedTable token) + | some widths, some bytes => + if bytes.length == widths.sum then .ok bytes + else .error (.rowLength token widths.sum bytes.length) + | some _, none => .error (.missingRow token) + +/-- The columns of a row as numbers. -/ +def MetadataIndex.columns (index : MetadataIndex) (token : Nat) : + Except MetadataError (List Nat) := do + let bytes ← index.row token + match columnWidths index.header.widths (metadataTableOf token) with + | some widths => + match splitColumns widths bytes with + | some values => .ok values + | none => .error (.rowLength token widths.sum bytes.length) + | none => .error (.unsupportedTable token) + +/-- A row of a given table; a token of another table is out of scope. -/ +private def MetadataIndex.columnsOf (index : MetadataIndex) (table token : Nat) : + Except MetadataError (List Nat) := + if metadataTableOf token == table then index.columns token else .error (.tokenOutOfScope token) + +/-- The `TypeRef` row of a token. -/ +def MetadataIndex.typeRef (index : MetadataIndex) (token : Nat) : + Except MetadataError TypeRefRow := do + match ← index.columnsOf Table.typeRef token with + | [scope, name, namespace_] => + .ok { scope := ← decodeCoded resolutionScopeTags 2 scope, name, namespace_ } + | _ => .error (.unsupportedTable token) + +/-- The `TypeDef` row of a token. -/ +def MetadataIndex.typeDef (index : MetadataIndex) (token : Nat) : + Except MetadataError TypeDefRow := do + match ← index.columnsOf Table.typeDef token with + | [flags, name, namespace_, extends_, fieldList, methodList] => + .ok { flags, name, namespace_, extends_ := ← decodeCoded typeDefOrRefTags 2 extends_, + fieldList, methodList } + | _ => .error (.unsupportedTable token) + +/-- A decoded TypeDef used bytes at its exact token in the carried row index. -/ +theorem MetadataIndex.typeDef_bytes {index : MetadataIndex} {token : Nat} {row : TypeDefRow} + (decoded : index.typeDef token = .ok row) : + metadataTableOf token = Table.typeDef ∧ ∃ bytes, index.rows.find? token = some bytes := by + cases cols : index.columnsOf Table.typeDef token with + | error fault => simp [MetadataIndex.typeDef, cols, bind, Except.bind] at decoded + | ok values => + unfold MetadataIndex.columnsOf at cols + split at cols <;> try contradiction + rename_i typed + refine ⟨by simpa using typed, ?_⟩ + cases read : index.row token with + | error fault => simp [MetadataIndex.columns, read, bind, Except.bind] at cols + | ok bytes => + unfold MetadataIndex.row at read + cases stored : index.rows.find? token with + | some value => exact ⟨value, rfl⟩ + | none => + simp only [stored] at read + split at read <;> try contradiction + split at read <;> contradiction +#print axioms MetadataIndex.typeDef_bytes + +/-- The `Field` row of a token. -/ +def MetadataIndex.field (index : MetadataIndex) (token : Nat) : + Except MetadataError FieldRow := do + match ← index.columnsOf Table.field token with + | [flags, name, signature] => .ok { flags, name, signature } + | _ => .error (.unsupportedTable token) + +/-- The `MethodDef` row of a token. -/ +def MetadataIndex.methodDef (index : MetadataIndex) (token : Nat) : + Except MetadataError MethodDefRow := do + match ← index.columnsOf Table.methodDef token with + | [rva, implFlags, flags, name, signature, paramList] => + .ok { rva, implFlags, flags, name, signature, paramList } + | _ => .error (.unsupportedTable token) + +/-- The `MemberRef` row of a token. -/ +def MetadataIndex.memberRef (index : MetadataIndex) (token : Nat) : + Except MetadataError MemberRefRow := do + match ← index.columnsOf Table.memberRef token with + | [parent, name, signature] => + .ok { parent := ← decodeCoded memberRefParentTags 3 parent, name, signature } + | _ => .error (.unsupportedTable token) + +/-- The signature blob offset of a `StandAloneSig` or `TypeSpec` row. -/ +def MetadataIndex.signatureOf (index : MetadataIndex) (table token : Nat) : + Except MetadataError Nat := do + match ← index.columnsOf table token with + | [signature] => .ok signature + | _ => .error (.unsupportedTable token) + +/-- The `AssemblyRef` row of a token. -/ +def MetadataIndex.assemblyRef (index : MetadataIndex) (token : Nat) : + Except MetadataError AssemblyRefRow := do + match ← index.columnsOf Table.assemblyRef token with + | [major, minor, build, revision, flags, publicKeyOrToken, name, culture, _] => + .ok { major, minor, build, revision, flags, publicKeyOrToken, name, culture } + | _ => .error (.unsupportedTable token) + +/-- A `NestedClass` row names a TypeDef and an earlier, nonzero enclosing TypeDef. -/ +def MetadataIndex.nestedClass (index : MetadataIndex) (token : Nat) : + Except MetadataError NestedClassRow := do + match ← index.columnsOf Table.nestedClass token with + | [nested, enclosing] => + let nestedToken ← checkedTokenOf Table.typeDef nested .rowOverflow + let enclosingToken ← checkedTokenOf Table.typeDef enclosing .rowOverflow + if nested > index.header.rows Table.typeDef then throw (.tokenOutOfScope nestedToken) + if enclosing == 0 || enclosing ≥ nested then throw (.invalidNesting nestedToken enclosingToken) + .ok { nested := nestedToken, enclosing := enclosingToken } + | _ => .error (.unsupportedTable token) + +/-- The `GenericParam` row of a token. -/ +def MetadataIndex.genericParam (index : MetadataIndex) (token : Nat) : + Except MetadataError GenericParamRow := do + match ← index.columnsOf Table.genericParam token with + | [number, flags, owner, name] => + .ok { number, flags, owner := ← decodeCoded typeOrMethodDefTags 1 owner, name } + | _ => .error (.unsupportedTable token) + +/-- The `MethodSpec` row of a token. -/ +def MetadataIndex.methodSpec (index : MetadataIndex) (token : Nat) : + Except MetadataError MethodSpecRow := do + match ← index.columnsOf Table.methodSpec token with + | [method, instantiation] => + .ok { method := ← decodeCoded methodDefOrRefTags 1 method, instantiation } + | _ => .error (.unsupportedTable token) + +/-- A `#Strings` entry of the slice. -/ +def MetadataIndex.string (index : MetadataIndex) (offset : Nat) : + Except MetadataError (List Nat) := + stringBytes index.strings offset + +/-- A `#Blob` entry of the slice, without its length prefix. -/ +def MetadataIndex.blob (index : MetadataIndex) (offset : Nat) : + Except MetadataError (List Nat) := + blobBytes index.blobs offset + +/-- A `#US` entry of the slice as UTF-16 code units. -/ +def MetadataIndex.userString (index : MetadataIndex) (offset : Nat) : + Except MetadataError (List Nat) := + userStringUnits index.userStrings offset + +/-! ## The identity of an assembly -/ + +/-- The decimal ASCII digits of a number below `10 ^ fuel`. -/ +def decimalDigits : Nat → Nat → List Nat + | 0, _ => [] + | fuel + 1, value => + if value < 10 then [48 + value] else decimalDigits fuel (value / 10) ++ [48 + value % 10] + +/-- The 16 lowercase hexadecimal digits of the eight bytes of a public key token. -/ +def hexDigits : List Nat → List Nat + | [] => [] + | byte :: rest => + let digit (value : Nat) : Nat := if value < 10 then 48 + value else 87 + value + digit (byte / 16) :: digit (byte % 16) :: hexDigits rest + +/-- The fixed words of an assembly identity as bytes: `, Version=`, `, Culture=`, `neutral`, + `, PublicKeyToken=` and `null`. They are byte lists, not strings, so the kernel never + evaluates a string literal to read an identity. -/ +def versionMarker : List Nat := [44, 32, 86, 101, 114, 115, 105, 111, 110, 61] +def cultureMarker : List Nat := [44, 32, 67, 117, 108, 116, 117, 114, 101, 61] +def neutralCulture : List Nat := [110, 101, 117, 116, 114, 97, 108] +def tokenMarker : List Nat := + [44, 32, 80, 117, 98, 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61] +def nullToken : List Nat := [110, 117, 108, 108] + +/-- The text of an assembly name as Cecil writes it: + `Name, Version=a.b.c.d, Culture=culture, PublicKeyToken=token`. -/ +def assemblySpelling (name : List Nat) (major minor build revision : Nat) + (culture token : List Nat) : + List Nat := + name ++ versionMarker ++ decimalDigits 5 major ++ [46] ++ decimalDigits 5 minor ++ [46] ++ + decimalDigits 5 build ++ [46] ++ decimalDigits 5 revision ++ cultureMarker ++ + (if culture.isEmpty then neutralCulture else culture) ++ tokenMarker ++ token + +/-- Decoded identity columns shared by the typed and display forms of an assembly reference. -/ +private structure AssemblyRefParts where + name : List Nat + version : Version + culture : List Nat + publicKeyToken : List Nat + +/-- Supported assembly references carry an eight-byte token or no token, and zero flags. -/ +private def MetadataIndex.assemblyRefParts (index : MetadataIndex) (token : Nat) : + Except MetadataError AssemblyRefParts := do + let row ← index.assemblyRef token + if row.flags != 0 then throw (.unsupportedAssemblyRef token) + let name ← index.string row.name + let culture ← index.string row.culture + let publicToken ← index.blob row.publicKeyOrToken + if !publicToken.isEmpty && publicToken.length != 8 then throw (.unsupportedAssemblyRef token) + let version : Version := { major := row.major, minor := row.minor, build := row.build, + revision := row.revision } + return { name, version, culture, publicKeyToken := publicToken } + +/-- The publisher token of each assembly family used for CLI primitive and base types. -/ +def coreLibraryKey? (name : MetadataName) : Option Nat := + if name == name!"System.Runtime" then some 0xb03f5f7f11d50a3a + else if name == name!"System.Private.CoreLib" then some 0x7cec85d7bea7798e + else if name == name!"mscorlib" then some 0xb77a5c561934e089 + else if name == name!"netstandard" then some 0xcc7b13ffcd2ddd51 + else none + +/-- A supported core-library AssemblyRef has a neutral culture and its publisher's token. + Version is kept in the full identity used for binding a type or method. -/ +def MetadataIndex.coreAssemblyRef (index : MetadataIndex) (token : Nat) : + Except MetadataError Bool := do + let parts ← index.assemblyRefParts token + let some name := MetadataName.ofBytes? parts.name | return false + let some publisher := coreLibraryKey? name | return false + return parts.culture.isEmpty && parts.publicKeyToken.length == 8 && + radixCode? 256 parts.publicKeyToken 0 == some publisher + +/-- The scoped assembly identity in the byte spelling used for slice lookup. -/ +def MetadataIndex.assemblyRefIdentity (index : MetadataIndex) (token : Nat) : + Except MetadataError (List Nat) := do + let parts ← index.assemblyRefParts token + let spelled := if parts.publicKeyToken.isEmpty then nullToken else hexDigits parts.publicKeyToken + let version := parts.version + return assemblySpelling parts.name version.major version.minor version.build version.revision + parts.culture spelled + +/-- Read a top-level type forwarder and its destination AssemblyRef from the facade bytes. + A file export or a nested export does not identify a destination assembly. -/ +def MetadataIndex.forwardedAssembly (index : MetadataIndex) (token : Nat) + (parts : List (List Nat)) : Except MetadataError (List Nat) := do + let [flags, typeDefId, name, namespace_, implementation] ← index.columnsOf Table.exportedType token + | throw (.operandMismatch token) + unless flags == 0x00200000 && typeDefId == 0 do throw (.operandMismatch token) + let destination ← decodeCoded [0x26, Table.assemblyRef, Table.exportedType] 2 implementation + unless metadataTableOf destination == Table.assemblyRef && tokenRow destination != 0 do + throw (.operandMismatch token) + unless [← index.string namespace_, ← index.string name] == parts do throw (.operandMismatch token) + index.assemblyRefIdentity destination + +/-- The assembly identity when its name and culture fit the expected identity's byte lengths. -/ +def MetadataIndex.assemblyRefName (index : MetadataIndex) (token : Nat) (expected : AssemblyName) : + Except MetadataError (Option AssemblyName) := do + let parts ← index.assemblyRefParts token + if parts.name.length != expected.name.code.log2 / 8 || + parts.culture.length != expected.culture.code.log2 / 8 then return none + let some name := MetadataName.ofBytes? parts.name | throw (.unsupportedAssemblyRef token) + let some culture := MetadataName.ofBytes? parts.culture | throw (.unsupportedAssemblyRef token) + let some key := radixCode? 256 parts.publicKeyToken 0 | throw (.unsupportedAssemblyRef token) + let publicKeyToken := if parts.publicKeyToken.isEmpty then none else some key + return some { name, version := parts.version, culture, publicKeyToken } + +/-- The identity an `Assembly` row spells, with the key token the assembly states. A key token is + the SHA-1 of the public key, which the kernel does not compute: it is the stated token when + the row has a key, and the stated token must then be sixteen lowercase hexadecimal digits; + with no key the identity says `null` and no token is stated. -/ +def AssemblyMetadata.spelling (assembly : AssemblyMetadata) : + Except MetadataError (List Nat) := do + let header := assembly.header + let strings ← indexEntries assembly.strings + let blobs ← indexEntries assembly.blobs + let widths ← match columnWidths header.widths Table.assembly with + | some widths => .ok widths + | none => .error (.unsupportedTable (tokenOf Table.assembly 1)) + if assembly.assemblyRow.length != widths.sum then + throw (.rowLength (tokenOf Table.assembly 1) widths.sum assembly.assemblyRow.length) + let some columns := splitColumns widths assembly.assemblyRow + | throw (.unsupportedTable (tokenOf Table.assembly 1)) + let [_, major, minor, build, revision, _, key, name, culture] := columns + | throw (.unsupportedTable (tokenOf Table.assembly 1)) + let publicKey ← blobBytes blobs key + let tokenBytes := assembly.keyToken + let spelled ← + if publicKey.isEmpty then + if tokenBytes.isEmpty then pure nullToken else throw .identityMismatch + else if tokenBytes.length == 16 && tokenBytes.all (fun digit => + (48 ≤ digit && digit ≤ 57) || (97 ≤ digit && digit ≤ 102)) then pure tokenBytes + else throw .identityMismatch + .ok (assemblySpelling (← stringBytes strings name) major minor build revision + (← stringBytes strings culture) spelled) + +/-- The assembly's header is what its table header bytes decode to, `identity` is the text its + `Assembly` row spells, and it is the identity the assembly states. It costs the same for + every method of the assembly, so an extraction proves it once and each method's evidence + names the bytes. The last part is an equation between a string and bytes, so it is proved + with `cil_kernel_rfl` and never evaluates the string. -/ +abbrev AssemblyIdentified (assembly : AssemblyMetadata) (identity : List Nat) : Prop := + (decodeTableHeader assembly.tableHeader).toOption = some assembly.header ∧ + assembly.spelling.toOption = some identity ∧ spelled? identity = some assembly.identity + +/-- The running instance field count before each of up to `count` consecutive `Field` rows from + `row`, as far as the rows are carried, with the total and whether every row was carried. A + field is static when bit 4 of the first flag byte is set. -/ +private def fieldCounts (index : MetadataIndex) : + Nat → Nat → Nat → List (Nat × Nat) → Except MetadataError (List (Nat × Nat) × Nat × Bool) + | 0, _, instances, found => .ok (found, instances, true) + | count + 1, row, instances, found => do + let token ← checkedTokenOf Table.field row .rowOverflow + match index.rows.find? token with + | none => .ok (found, instances, false) + | some bytes => + let isStatic := bytes.getD 0 0 / 0x10 % 2 == 1 + fieldCounts index count (row + 1) (if isStatic then instances else instances + 1) + ((token, instances) :: found) + +/-- The carried prefix of a type's field counts, with a total when all its rows are carried. -/ +private def typeCounts (index : MetadataIndex) (token : Nat) : + Except MetadataError (Option (List (Nat × Nat) × Option Nat)) := do + let .ok row := index.typeDef token | return none + let stop ← if tokenRow token == index.header.rows Table.typeDef then + pure (some (index.header.rows Table.field + 1)) + else do + let next ← checkedTokenOf Table.typeDef (tokenRow token + 1) .rowOverflow + pure ((index.typeDef next).toOption.map (·.fieldList)) + let some stop := stop | return none + let (found, instances, complete) ← fieldCounts index (stop - row.fieldList) row.fieldList 0 [] + return some (found, if complete then some instances else none) + +/-- The instance field counts of every carried type, as entries in ascending key order. -/ +private def fieldCountEntries (index : MetadataIndex) : + Except MetadataError (SearchTree Nat × SearchTree Nat) := do + let (before, total) ← index.rows.toList.foldlM (fun (before, total) (token, _) => do + if metadataTableOf token != Table.typeDef then return (before, total) + match ← typeCounts index token with + | some (found, some count) => return (found ++ before, (token, count) :: total) + | some (found, none) => return (found ++ before, total) + | none => return (before, total)) ([], []) + return (← SearchTree.ofEntries before.reverse, ← SearchTree.ofEntries total.reverse) + +/-- Insert links into a list of entries keyed by kind and source, refusing a second link for one + key. -/ +private def linkEntries : List MetadataLink → List (Nat × (Nat × Nat)) + | [] => [] + | link :: rest => + (link.kind * 0x100000000 + link.source, (link.slice, link.target)) :: linkEntries rest + +/-- Group owner-sorted parameter tokens into first-token and count entries. -/ +private def genericOwnerGroups : List (Nat × Nat) → List (Nat × (Nat × Nat)) + | [] => [] + | (owner, first) :: rest => collect owner first 1 rest +where + /-- Finish each owner's group before starting the next owner. -/ + collect (owner first count : Nat) : List (Nat × Nat) → List (Nat × (Nat × Nat)) + | [] => [(owner, (first, count))] + | (nextOwner, token) :: rest => + if nextOwner == owner then collect owner first (count + 1) rest + else (owner, (first, count)) :: collect nextOwner token 1 rest + +/-- Merge two lists ordered by `before`, within a bound of their combined lengths. -/ +def mergeBy {α : Type} (before : α → α → Bool) : Nat → List α → List α → List α + | _, [], right => right + | _, left, [] => left + | 0, _, _ => [] + | fuel + 1, left :: leftRest, right :: rightRest => + if before left right then left :: mergeBy before fuel leftRest (right :: rightRest) + else right :: mergeBy before fuel (left :: leftRest) rightRest + +/-- The entries ordered by `before` with structurally bounded O(n log n) recursion; given a fuel of + at least their length they are sorted, and with any fuel they are a permutation. -/ +def sortBy {α : Type} (before : α → α → Bool) : Nat → List α → List α + | 0, entries => entries + | _ + 1, [] => [] + | _ + 1, [entry] => [entry] + | fuel + 1, entries => + let count := entries.length + let halves := entries.splitAt (count / 2) + mergeBy before count (sortBy before fuel halves.1) (sortBy before fuel halves.2) + +/-- Pairs of numbers in lexicographic order. -/ +def pairBefore (first second : Nat × Nat) : Bool := + first.1 < second.1 || (first.1 == second.1 && first.2 ≤ second.2) + +/-- The pairs in lexicographic order, in O(n log n) comparisons. -/ +def sortPairs (pairs : List (Nat × Nat)) : List (Nat × Nat) := + sortBy pairBefore pairs.length pairs + +/-- Merging with fuel for both lists keeps exactly their elements. -/ +theorem mergeBy_perm {α : Type} (before : α → α → Bool) : + ∀ (fuel : Nat) (left right : List α), left.length + right.length ≤ fuel → + (mergeBy before fuel left right).Perm (left ++ right) + | _, [], right, _ => by simp [mergeBy] + | _, _ :: _, [], _ => by simp [mergeBy] + | 0, _ :: _, _ :: _, enough => by simp at enough + | fuel + 1, left :: leftRest, right :: rightRest, enough => by + simp only [List.length_cons] at enough + simp only [mergeBy] + split + · exact (mergeBy_perm before fuel leftRest (right :: rightRest) (by simp; omega)).cons left + · have rest := mergeBy_perm before fuel (left :: leftRest) rightRest (by simp; omega) + exact (rest.cons right).trans List.perm_middle.symm + +#print axioms mergeBy_perm + +/-- Sorting keeps exactly the entries, whatever the fuel. -/ +theorem sortBy_perm {α : Type} (before : α → α → Bool) : + ∀ (fuel : Nat) (entries : List α), (sortBy before fuel entries).Perm entries + | 0, entries => by simp [sortBy] + | _ + 1, [] => by simp [sortBy] + | _ + 1, [_] => by simp [sortBy] + | fuel + 1, first :: second :: rest => by + rw [sortBy.eq_4 _ _ _ (by simp) (by simp)] + have left := sortBy_perm before fuel + ((first :: second :: rest).splitAt ((first :: second :: rest).length / 2)).1 + have right := sortBy_perm before fuel + ((first :: second :: rest).splitAt ((first :: second :: rest).length / 2)).2 + refine (mergeBy_perm before _ _ _ ?_).trans ?_ + · rw [left.length_eq, right.length_eq] + simp [List.splitAt_eq] + omega + · refine (left.append right).trans ?_ + simp [List.splitAt_eq] + +#print axioms sortBy_perm + +/-- The `TypeOrMethodDef` coded value of a `TypeDef` or `MethodDef` owner token, the key + ECMA-335 requires the `GenericParam` table to be sorted by. -/ +def genericOwnerCode (owner : Nat) : Nat := + tokenRow owner * 2 + if metadataTableOf owner == Table.methodDef then 1 else 0 + +/-- Index every carried generic parameter by the coded value of its owner in O(n log n) time. -/ +private def genericOwnerEntries (index : MetadataIndex) : + Except MetadataError (SearchTree (Nat × Nat)) := do + let entries ← index.rows.toList.foldlM (fun found (token, _) => do + if metadataTableOf token != Table.genericParam then return found + let parameter ← index.genericParam token + return (genericOwnerCode parameter.owner, token) :: found) [] + let ordered := sortPairs entries + SearchTree.ofEntries (genericOwnerGroups ordered) + +/-- The lookup structures of a slice, built from its bytes: its entries as trees, and the instance + field counts of its carried types. -/ +def MetadataSlice.tables (slice : MetadataSlice) : Except MetadataError IndexTables := do + let header := slice.assembly.header + let rows ← indexEntries slice.rows + let strings ← indexEntries slice.strings + let blobs ← indexEntries slice.blobs + let userStrings ← indexEntries slice.userStrings + let links ← SearchTree.ofEntries (linkEntries slice.links) + let bare : IndexTables := + { rows := rows + strings := strings + blobs := blobs + userStrings := userStrings + links := links + instanceBefore := .leaf + instanceTotal := .leaf + genericOwners := .leaf } + let index : MetadataIndex := + { toIndexTables := bare, header := header, corlib := slice.corlib, identity := [] } + let (before, total) ← fieldCountEntries index + let genericOwners ← genericOwnerEntries index + .ok { bare with + instanceBefore := before + instanceTotal := total + genericOwners := genericOwners } + +/-- The index of a slice given its tables and the identity its assembly spells, which an + `AssemblyIdentified` fact ties, with the assembly's header, to the assembly's bytes and to + the string it states. Cheap: independent of the number of rows. -/ +def MetadataSlice.indexWith (slice : MetadataSlice) (tables : IndexTables) + (identity : List Nat) : Except MetadataError MetadataIndex := + .ok { toIndexTables := tables, header := slice.assembly.header, corlib := slice.corlib, + identity := identity } + +/-- Build the index of a slice from its bytes alone, refusing a header the table header bytes do + not decode to and an identity the `Assembly` row does not spell as the assembly states it. -/ +def MetadataSlice.index (slice : MetadataSlice) : Except MetadataError MetadataIndex := do + if (← decodeTableHeader slice.assembly.tableHeader) != slice.assembly.header then + throw .malformedTableHeader + let identity ← slice.assembly.spelling + if spelled? identity != some slice.assembly.identity then throw .identityMismatch + slice.indexWith (← slice.tables) identity + +/-- The slices of one method's metadata, its own first. -/ +def MethodMetadata.slices (metadata : MethodMetadata) : List MetadataSlice := + metadata.own :: metadata.others + +/-- Each assembly spells the identity listed at its position. -/ +def AllIdentified : List AssemblyMetadata → List (List Nat) → Prop + | [], [] => True + | assembly :: assemblies, identity :: identities => + AssemblyIdentified assembly identity ∧ AllIdentified assemblies identities + | _, _ => False + +instance (assembly : AssemblyMetadata) (identity : List Nat) : + Decidable (AssemblyIdentified assembly identity) := + inferInstanceAs (Decidable (_ ∧ _)) + +instance decideAllIdentified : (assemblies : List AssemblyMetadata) → + (identities : List (List Nat)) → Decidable (AllIdentified assemblies identities) + | [], [] => isTrue trivial + | [], _ :: _ => isFalse fun impossible => impossible + | _ :: _, [] => isFalse fun impossible => impossible + | assembly :: assemblies, identity :: identities => + haveI := decideAllIdentified assemblies identities + inferInstanceAs (Decidable (AssemblyIdentified assembly identity ∧ + AllIdentified assemblies identities)) + +theorem AllIdentified.nil : AllIdentified [] [] := trivial + +#print axioms AllIdentified.nil + +theorem AllIdentified.cons {assembly : AssemblyMetadata} {identity : List Nat} + {assemblies : List AssemblyMetadata} {identities : List (List Nat)} + (first : AssemblyIdentified assembly identity) (rest : AllIdentified assemblies identities) : + AllIdentified (assembly :: assemblies) (identity :: identities) := ⟨first, rest⟩ + +#print axioms AllIdentified.cons + +/-- Each slice's assembly spells the identity listed for it, in the slices' order. -/ +abbrev AssembliesIdentified (metadata : MethodMetadata) (identities : List (List Nat)) : Prop := + AllIdentified (metadata.slices.map (·.assembly)) identities + +/-- The tables of the slices of one method's metadata, its own first. -/ +def MethodMetadata.tables (metadata : MethodMetadata) : Except MetadataError (List IndexTables) := + metadata.slices.mapM MetadataSlice.tables + +/-- Whether `tables` are what the decoder builds from the metadata's bytes. Only the kernel + decides it, like `bodyMatches`. -/ +@[irreducible] def tablesMatch (metadata : MethodMetadata) (tables : List IndexTables) : Bool := + match metadata.tables with + | .ok built => built == tables + | .error _ => false + +end CIL.Extraction diff --git a/CIL/Extraction/MetadataContractIdentity.lean b/CIL/Extraction/MetadataContractIdentity.lean new file mode 100644 index 000000000..edfea8395 --- /dev/null +++ b/CIL/Extraction/MetadataContractIdentity.lean @@ -0,0 +1,69 @@ +import CIL.Extraction.MetadataResolution + +/-! +# Generic contract identity components + +Exact, length-framed UTF-8 components bind a generic imported contract to its open metadata +signature and its closed arguments. Definition bytes use hexadecimal inside the outer signature. +-/ + +namespace CIL.Extraction + +/-- A UTF-8 component framed by its byte count. -/ +def contractPart (bytes : List Nat) : List Nat := + bytesOf (toString bytes.length) ++ [58] ++ bytes + +/-- Uppercase hexadecimal preserves definition bytes without signature punctuation. -/ +def contractHex (bytes : List Nat) : List Nat := + let digit (value : Nat) := if value < 10 then 48 + value else 55 + value + bytes.flatMap fun byte => [digit (byte / 16), digit (byte % 16)] + +/-- The class/value marker carried by a named signature type, when known. -/ +def contractValueKind : TypeKind → Option Bool + | .valueType | .enumType _ => some true + | .classType | .interfaceType => some false + | .importedOwner => none + | .external hint => hint + +/-- The scoped named component and its signature class/value marker. -/ +def contractAtom (scope name : List Nat) (valueType : Bool) : List Nat := + bytesOf "atom:" ++ contractPart scope ++ contractPart name ++ + [if valueType then 118 else 114] + +/-- The exact supported open type key; an unknown kind or unsupported wrapper has no key. -/ +def contractTypeKey (frame : Frame) : Nat → ResolvedType → Option (List Nat) + | 0, _ => none + | _ + 1, .primitive code => do + let scope ← frame.corlib + let name ← primitiveName code + pure (contractAtom scope name (code != 1 && code != 0x0E && code != 0x1C)) + | _ + 1, .named named => do + pure (contractAtom named.scope named.name (← contractValueKind named.kind)) + | _ + 1, .var index => some (bytesOf ("type:" ++ toString index)) + | _ + 1, .mvar index => some (bytesOf ("method:" ++ toString index)) + | fuel + 1, .byref element => do + pure (bytesOf "byref:" ++ contractPart (← contractTypeKey frame fuel element)) + | _, _ => none + +/-- The closed name of a supported contract type, with indexed method argument names. -/ +def contractClosedName (argumentNames : SearchTree (List Nat)) : + Nat → ResolvedType → Option (List Nat) + | 0, _ => none + | fuel + 1, type@(.primitive _) | fuel + 1, type@(.named _) => + cecilName false (fuel + 1) type + | _ + 1, .mvar index => argumentNames.find? index + | fuel + 1, .byref element => + (· ++ [38]) <$> contractClosedName argumentNames fuel element + | _, _ => none + +/-- The primitive integer codes admitted as closed imported method arguments. -/ +def contractInteger (type : ResolvedType) : Bool := + match type with + | .primitive code => (2 ≤ code && code ≤ 11) || code == 0x18 || code == 0x19 + | .named named => named.scope == runtimeScopeBytes && + contractValueKind named.kind == some true && + ([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x18, 0x19].any fun code => + primitiveName code == some named.name) + | _ => false + +end CIL.Extraction diff --git a/CIL/Extraction/MetadataNames.lean b/CIL/Extraction/MetadataNames.lean index f47c714b0..ec37b6884 100644 --- a/CIL/Extraction/MetadataNames.lean +++ b/CIL/Extraction/MetadataNames.lean @@ -1,4 +1,5 @@ import CIL.TypeExpr +import CIL.Extraction.KernelEquation /-! # Exact named metadata identities @@ -30,10 +31,10 @@ private def leaf (scope name : List Char) : Option (List Char × List Char) := if plainScope scope && plainName name then some (scope, name) else none /-- Decode ASCII without repeated indexed UTF-8 reads during kernel reduction. -/ -def asciiChars : List UInt8 → Option (List Char) - | [] => some [] - | byte :: bytes => - if byte.toNat < 128 then (Char.ofNat byte.toNat :: ·) <$> asciiChars bytes else none +def asciiChars (bytes : List UInt8) : Option (List Char) := + if bytes.all (fun byte => byte.toNat < 128) then + some (bytes.map fun byte => Char.ofNat byte.toNat) + else none /-- An ASCII byte is the complete UTF-8 encoding of its character. -/ private theorem asciiByte (byte : UInt8) (small : byte.toNat < 128) : @@ -48,19 +49,17 @@ private theorem asciiByte (byte : UInt8) (small : byte.toNat < 128) : private theorem asciiChars_encode (bytes : List UInt8) (chars : List Char) (decoded : asciiChars bytes = some chars) : chars.utf8Encode.data.toList = bytes := by - induction bytes generalizing chars with - | nil => simp [asciiChars] at decoded; subst chars; rfl - | cons byte bytes ih => - simp only [asciiChars] at decoded - split at decoded - next small => - cases tail : asciiChars bytes with - | none => simp [tail] at decoded - | some rest => - simp [tail] at decoded - subst chars - simpa [List.utf8Encode, List.flatMap_cons, asciiByte byte small] using ih rest tail - next large => simp at decoded + unfold asciiChars at decoded + split at decoded + next small => + cases decoded + simp only [List.all_eq_true, decide_eq_true_eq] at small + induction bytes with + | nil => rfl + | cons byte bytes ih => + simp only [List.mem_cons, forall_eq_or_imp] at small + simpa [List.utf8Encode, List.flatMap_cons, asciiByte byte small.1] using ih small.2 + next => cases decoded #print axioms asciiChars_encode @@ -68,13 +67,24 @@ private theorem asciiChars_encode (bytes : List UInt8) (chars : List Char) def metadataChars (value : String) : List Char := match asciiChars value.toByteArray.data.toList with | some chars => chars - | none => value.toList + | none => + let bytes := value.toByteArray.data.toList.map UInt8.toNat + (utf8Characters bytes.length bytes).getD [] /-- Metadata decoding preserves the original character sequence for every string. -/ theorem metadataChars_eq (value : String) : metadataChars value = value.toList := by unfold metadataChars cases decoded : asciiChars value.toByteArray.data.toList with - | none => rfl + | none => + obtain ⟨chars, rfl⟩ : ∃ chars, value = String.ofList chars := + ⟨value.toList, String.ofList_toList.symm⟩ + have bytes : (String.ofList chars).toByteArray.data.toList.map UInt8.toNat = + chars.flatMap fun char => (String.utf8EncodeChar char).map UInt8.toNat := by + simp [String.toByteArray_ofList, List.utf8Encode, List.map_flatMap] + rw [bytes] + dsimp only + rw [utf8Characters_decode chars _ (Nat.le_refl _), String.toList_ofList] + rfl | some chars => have encoded := asciiChars_encode _ _ decoded have same : String.ofList chars = value := by @@ -84,9 +94,45 @@ theorem metadataChars_eq (value : String) : metadataChars value = value.toList : #print axioms metadataChars_eq +/-- Splitting a metadata identity consumes exactly the first double colon. -/ +private theorem splitIdentity_parts {reversed input scope name : List Char} + (split : splitIdentity reversed input = some (scope, name)) : + reversed.reverse ++ input = scope ++ ':' :: ':' :: name := by + fun_induction splitIdentity reversed input <;> simp_all [List.append_assoc] + +#print axioms splitIdentity_parts + +/-- The assembly prefix of a flat metadata identity. Generic arguments may carry further scopes. -/ +def flatOwnerScope? (identity : String) : Option String := do + let (scope, name) ← splitIdentity [] (metadataChars identity) + if plainScope scope && !name.isEmpty then return String.ofList scope else none + +/-- Every scope returned by the parser is followed by the metadata ownership separator. -/ +theorem flatOwnerScope?_prefix {identity scope : String} + (found : flatOwnerScope? identity = some scope) : + (metadataChars scope ++ [':', ':']).isPrefixOf (metadataChars identity) = true := by + unfold flatOwnerScope? at found + cases split : splitIdentity [] (metadataChars identity) with + | none => simp [split] at found + | some pair => + obtain ⟨assembly, suffix⟩ := pair + simp only [split] at found + change (if plainScope assembly && !suffix.isEmpty then some (String.ofList assembly) + else none) = some scope at found + split at found + · cases found + have parts := splitIdentity_parts split + simp only [List.reverse_nil, List.nil_append] at parts + rw [parts, metadataChars_eq] + simp [List.isPrefixOf_iff_prefix, List.IsPrefix, List.append_assoc] + · contradiction +#print axioms flatOwnerScope?_prefix + + /-- A named type as its referencing scope and full name. -/ def namedType : TypeExpr → Option (List Char × List Char) - | .atom scope name | .constructor scope name => leaf (metadataChars scope) (metadataChars name) + | .atom scope name | .constructor scope name | .objectConstructor scope name => + leaf (metadataChars scope) (metadataChars name) | .aggregate identity _ | .enum identity _ => do let (scope, name) ← splitIdentity [] (metadataChars identity) leaf scope name @@ -95,7 +141,8 @@ def namedType : TypeExpr → Option (List Char × List Char) /-- The fast metadata decoder retains the original named-type rule, including Unicode. -/ theorem namedType_eq_original (type : TypeExpr) : namedType type = match type with - | .atom scope name | .constructor scope name => leaf scope.toList name.toList + | .atom scope name | .constructor scope name | .objectConstructor scope name => + leaf scope.toList name.toList | .aggregate identity _ | .enum identity _ => do let (scope, name) ← splitIdentity [] identity.toList leaf scope name @@ -104,9 +151,9 @@ theorem namedType_eq_original (type : TypeExpr) : namedType type = #print axioms namedType_eq_original -/-- A named type's plain full name; an atom's or constructor's scope is left undecoded. -/ +/-- A named type's plain full name; an atom's or generic definition's scope is left undecoded. -/ def plainTypeName : TypeExpr → Option (List Char) - | .atom _ name | .constructor _ name => + | .atom _ name | .constructor _ name | .objectConstructor _ name => let name := metadataChars name if plainName name then some name else none | type => Prod.snd <$> namedType type @@ -154,8 +201,42 @@ private def unscopedArguments : Nat → List Char → List Char /-- The runtime signature spelling of a scoped aggregate identity, nested arguments included. -/ def aggregateName (identity : String) : Option (List Char) := do - let name ← afterArgumentScope identity.toList + let name ← afterArgumentScope (metadataChars identity) let name := unscopedArguments name.length name if name.isEmpty then none else return name +/-- Pointer, byref, array and generic instance types, which are never equal to a named type. -/ +private def constructed : TypeExpr → Bool + | .pointer _ | .byref _ | .array _ | .apply .. => true + | _ => false + +/-- Conjunction of two three-valued answers: false wins over unknown. -/ +private def both : Option Bool → Option Bool → Option Bool + | some false, _ | _, some false => some false + | some true, some true => some true + | _, _ => none + +/-- Runtime identity of two closed metadata types, or `none` when metadata cannot decide it. + Full names are never forwarded, but one name in two scopes may denote one type. -/ +def sameRuntimeType : TypeExpr → TypeExpr → Option Bool + | .pointer x, .pointer y | .byref x, .byref y | .array x, .array y => sameRuntimeType x y + | .apply f x, .apply g y => both (sameRuntimeType f g) (sameRuntimeType x y) + | a, b => + -- `void` is serialized without its scope, so equal spellings do not establish identity. + if a == b then (if a == .void then none else some true) else + match namedType a, namedType b with + | some (scope, name), some (scope', name') => + if name != name' then some false else if scope == scope' then some true else none + | _, _ => + if (constructed a || (namedType a).isSome) && (constructed b || (namedType b).isSome) then + some false + else none + +/-- A repeated aggregate operand with the same scoped name and layout has the same runtime type. -/ +theorem sameRuntimeType_aggregate_self (identity : String) (layout : Nat) : + sameRuntimeType (.aggregate identity layout) (.aggregate identity layout) = some true := by + simp [sameRuntimeType] + +#print axioms sameRuntimeType_aggregate_self + end CIL.Extraction diff --git a/CIL/Extraction/MetadataResolution.lean b/CIL/Extraction/MetadataResolution.lean new file mode 100644 index 000000000..4c6742187 --- /dev/null +++ b/CIL/Extraction/MetadataResolution.lean @@ -0,0 +1,1447 @@ +import CIL.Extraction.Signatures +import CIL.Extraction.Generics +import CIL.Extraction.PlatformDefinitions +import Std.Data.TreeMap.Raw.Basic + +/-! +# Resolving metadata names and types + +A signature type names its classes and value types by token of the slice the blob comes from. +This file gives a token a scoped name and a kind, following `TypeRef` scopes and nested types, +and finds the `TypeDef` of a type the carried slices define. `ResolvedType` is independent of +the slice a type was read in, so a call site's generic arguments can be substituted into a +callee's signature from another slice. `typeAdmits` states when an extractor's `TypeExpr` +names a resolved type; it is a relation because the extractor picks the `TypeExpr` +constructor (`atom`, `object`, `aggregate`, `enum`) among those the resolved kind allows. + +Pinned types take their kinds from their carried metadata. For a platform type the extractor +supplies a scoped table entry, including the storage code of an enum. Lean checks each lookup. +Agreement with the platform image bytes remains trusted. +-/ + +namespace CIL.Extraction + +/-- The storage kind of a named type. A resolved enum retains its integer element code. + Other platform types record their value-or-reference marker. A modifier read by name alone + has no marker. -/ +inductive TypeKind where + | valueType + | enumType (underlying : Nat) + | classType + | interfaceType + /-- The declaring name of a linked static import has no storage classification. -/ + | importedOwner + | external (valueType : Option Bool) + deriving DecidableEq, Repr + +/-- A type by the identity of the assembly that defines it and its Cecil full name + (`Namespace.Name`, nested types as `Outer/Inner`), as bytes. `pinned` is set for a type + found in a carried slice. -/ +structure NamedType where + scope : List Nat + name : List Nat + /-- The namespace column and the simple name of each enclosing type and of the type itself, + kept apart: `name` joins them with dots and slashes and leaves out a nested type's + namespace, so types it spells alike stay different. -/ + parts : List (List Nat) + kind : TypeKind + pinned : Bool + platformArity : Nat := 0 + deriving DecidableEq, Repr + +mutual + +/-- A signature type with every token resolved. -/ +inductive ResolvedType where + | primitive (code : Nat) + | named (type : NamedType) + | var (index : Nat) + | mvar (index : Nat) + | byref (element : ResolvedType) + | pointer (element : ResolvedType) + | szarray (element : ResolvedType) + | genericInst (head : NamedType) (arguments : ResolvedList) + | functionPointer (convention : Nat) (result : ResolvedType) (parameters : ResolvedList) + | modified (required : Bool) (modifier : NamedType) (element : ResolvedType) + deriving DecidableEq, Repr + +/-- A list of resolved types, as a chain so that the types derive decidable equality. -/ +inductive ResolvedList where + | nil + | cons (head : ResolvedType) (tail : ResolvedList) + deriving DecidableEq, Repr + +end + +/-- The types of a chain, in order. -/ +def ResolvedList.toList : ResolvedList → List ResolvedType + | .nil => [] + | .cons head tail => head :: tail.toList + +/-- The number of arguments supplied to a constructed head. -/ +def ResolvedList.length : ResolvedList -> Nat + | .nil => 0 + | .cons _ tail => tail.length + 1 + +/-- The chain of a list of types. -/ +def ResolvedList.ofList : List ResolvedType → ResolvedList + | [] => .nil + | head :: tail => .cons head (ofList tail) + +/-- A prefix trie with indexed byte children and exact prefix membership at each node. -/ +inductive AssemblyPrefixes where + | node (terminal : Bool) (children : Std.TreeMap.Raw Nat AssemblyPrefixes) + +/-- An empty trie contains no prefixes. -/ +instance : EmptyCollection AssemblyPrefixes := ⟨.node false {}⟩ + +/-- Insert one prefix, retaining shared nodes. -/ +def AssemblyPrefixes.insert : AssemblyPrefixes → List Nat → AssemblyPrefixes + | .node _ children, [] => .node true children + | .node terminal children, byte :: rest => + let child := (children[byte]?).getD {} + .node terminal (children.insert byte (child.insert rest)) + +/-- Whether the supplied identity begins with any stored prefix. -/ +def AssemblyPrefixes.matches : AssemblyPrefixes → List Nat → Bool + | .node true _, _ => true + | .node false _, [] => false + | .node false children, byte :: rest => + match children[byte]? with + | some child => child.matches rest + | none => false + +/-- The package's complete assembly identities, indexed by their exact bytes. -/ +abbrev ExactPins := Std.TreeMap.Raw (List Nat) Unit + +/-- Index each full pin once per package. -/ +def indexPins (names : List (List Nat)) : ExactPins := + names.foldl (fun entries name => entries.insert name ()) {} + +/-- Binary search of a materialized identity index, with structural fuel for kernel reduction. -/ +def containsName (names : Array (List Nat)) (key : List Nat) : Bool := + search names.size 0 names.size +where + /-- A half-open interval contains the key only if one of its two halves or its midpoint does. -/ + search : Nat -> Nat -> Nat -> Bool + | 0, _, _ => false + | fuel + 1, low, high => Id.run do + if low >= high then return false + let middle := (low + high) / 2 + let some atMiddle := names[middle]? | return false + return match compare key atMiddle with + | .eq => true + | .lt => search fuel low middle + | .gt => search fuel (middle + 1) high + +/-- Indexed metadata and assembly identities derived together from checked metadata. -/ +structure MetadataEnv where + slices : SearchTree MetadataIndex + identities : Std.TreeMap.Raw (List Nat) Nat + pinned : AssemblyPrefixes + exactPins : ExactPins + /-- What the checks rely on about the types no carried slice defines. -/ + platform : PlatformTable + /-- Exact value-layout identities published by the selected import slots. -/ + publishedLayouts : Array (List Nat) := #[] + +/-- Build all assembly lookups once, keeping the first occurrence of each identity. -/ +def MetadataEnv.ofSlices (slices : List MetadataIndex) (pinned : AssemblyPrefixes := {}) + (platform : PlatformTable := {}) (exactPins : ExactPins := {}) : MetadataEnv := Id.run do + let mut identities : Std.TreeMap.Raw (List Nat) Nat := {} + let mut position := 0 + for index in slices do + if !identities.contains index.identity then + identities := identities.insert index.identity position + position := position + 1 + return { + slices := SearchTree.ofSorted (slices.zipIdx.map fun (index, position) => (position, index)) + identities := identities + pinned := pinned + exactPins := exactPins + platform := platform } + +/-- The slice at a position, or `unresolvedType` for a token that points outside the list. -/ +def MetadataEnv.slice (env : MetadataEnv) (position token : Nat) : + Except MetadataError MetadataIndex := + match env.slices.find? position with + | some index => .ok index + | none => .error (.unresolvedType token) + +/-- The slice, link-resolved: the link of a kind from a token, if the slice states one. -/ +def IndexTables.link (index : IndexTables) (kind source : Nat) : Option (Nat × Nat) := + index.links.find? (kind * 0x100000000 + source) + +/-- How deeply names, enclosing types and signature types may nest. -/ +def resolveFuel : Nat := 32 + +/-- The simple name of an assembly identity followed by `versionMarker`: every identity of an + assembly of that name, at any version, culture or key, starts with it. -/ +def assemblyStem : List Nat → List Nat + | [] => [] + | byte :: rest => + if versionMarker.isPrefixOf (byte :: rest) then versionMarker else byte :: assemblyStem rest + +/-- The package's complete pin identities and their checked prefix and exact lookup tables. -/ +structure PinnedIndex where + identities : List (List Nat) + prefixes : AssemblyPrefixes + indexed : prefixes = (identities.map assemblyStem).foldl AssemblyPrefixes.insert {} + exact : ExactPins + exactIndexed : exact = indexPins identities + +/-- Construct a checked index directly from its complete identity list. -/ +def PinnedIndex.ofIdentities (identities : List (List Nat)) : PinnedIndex := + { identities, prefixes := (identities.map assemblyStem).foldl AssemblyPrefixes.insert {}, + indexed := rfl, exact := indexPins identities, exactIndexed := rfl } + +/-- Complete identities determine every component of a checked pin index. -/ +theorem PinnedIndex.ext {first second : PinnedIndex} + (identities : first.identities = second.identities) : first = second := by + cases first with + | mk first firstPrefixes firstIndexed firstExact firstBound => + cases second with + | mk second secondPrefixes secondIndexed secondExact secondBound => + dsimp at identities + subst second + cases firstIndexed + cases secondIndexed + cases firstBound + cases secondBound + rfl +#print axioms PinnedIndex.ext + +/-- Checked pin indexes are equal exactly when their complete identities are equal. -/ +instance : DecidableEq PinnedIndex := fun first second => + if same : first.identities = second.identities then .isTrue (PinnedIndex.ext same) + else .isFalse (fun equal => same (congrArg PinnedIndex.identities equal)) + +/-- The complete identities determine the displayed pin index. -/ +instance : Repr PinnedIndex where + reprPrec index precedence := reprPrec index.identities precedence + +/-- `Namespace.Name`, or the bare name for the global namespace. -/ +def qualifiedName (namespace_ name : List Nat) : List Nat := + if namespace_.isEmpty then name else namespace_ ++ 46 :: name + +/-- The name of a type nested in an enclosing one. -/ +def nestedName (enclosing name : List Nat) : List Nat := enclosing ++ 47 :: name + +/-- The full name a type's name parts spell, as `typeRefQualified` spells it: the outermost + type's namespace and name, then each nested type's name; `none` when the parts are no pairs. -/ +def partsName : List (List Nat) → Option (List Nat) + | space :: simple :: nested => nestedParts (qualifiedName space simple) nested + | _ => none +where + /-- The name of the types nested in `name` that the remaining parts give. -/ + nestedParts (name : List Nat) : List (List Nat) → Option (List Nat) + | [] => some name + | _ :: simple :: rest => nestedParts (nestedName name simple) rest + | [_] => none + +/-- Read one of the four unsigned 16-bit version components in a metadata assembly identity. -/ +private def versionPart? (input : List Nat) : Option (List Nat) := do + let digits := input.takeWhile fun b => 48 ≤ b && b ≤ 57 + let count := digits.length + guard (0 < count && count ≤ 5 && (count == 1 || digits.head? != some 48)) + guard (digits.foldl (fun n b => 10 * n + (b - 48)) 0 ≤ 65535) + return input.drop count + +/-- The suffix after the remaining dotted version components. -/ +private def versionSuffix? : Nat → List Nat → Option (List Nat) + | 0, input => versionPart? input + | n + 1, input => do + let 46 :: next ← versionPart? input | none + versionSuffix? n next +/-- Check the complete neutral core assembly identity, including its publisher and version. -/ +def isCorlibScope (scope : List Nat) : Bool := Id.run do + let name := scope.takeWhile (· != 44) + let some metadataName := MetadataName.ofBytes? name | return false + let some publisher := coreLibraryKey? metadataName | return false + let rest := scope.drop name.length + unless versionMarker.isPrefixOf rest do return false + let some suffix := versionSuffix? 3 (rest.drop versionMarker.length) | return false + return suffix == cultureMarker ++ neutralCulture ++ tokenMarker ++ + bytesOf (publicKeyTokenText (some publisher)) + +/-- A base TypeRef counts as a core-library type when its scope names the platform publisher. -/ +def MetadataIndex.coreLibraryReference (index : MetadataIndex) (token : Nat) : + Except MetadataError Bool := do + let row ← index.typeRef token + if metadataTableOf row.scope != Table.assemblyRef then return false + index.coreAssemblyRef row.scope + +/-- CoreLib defines its own primitives. Other slices identify their primitive assembly by its + AssemblyRef row, if present. -/ +def MetadataIndex.corlibIdentity (index : MetadataIndex) : + Except MetadataError (Option (List Nat)) := + if index.corlib == 0 then + .ok (if index.identity == bytesOf coreLibraryScope then some index.identity else none) + else do + let token ← checkedTokenOf Table.assemblyRef index.corlib .rowOverflow + if !(← index.coreAssemblyRef token) then throw (.unsupportedAssemblyRef token) + .ok (some (← index.assemblyRefIdentity token)) + +/-- The scope, full name and name parts a `TypeRef` spells: its assembly, or the outermost + enclosing type's. -/ +def typeRefQualified (env : MetadataEnv) (position : Nat) : + Nat → Nat → Except MetadataError (List Nat × List Nat × List (List Nat)) + | 0, token => .error (.unresolvedType token) + | fuel + 1, token => do + let index ← env.slice position token + let row ← index.typeRef token + let namespace_ ← index.string row.namespace_ + let simple ← index.string row.name + let name := qualifiedName namespace_ simple + match metadataTableOf row.scope with + | 0x23 => .ok (← index.assemblyRefIdentity row.scope, name, [namespace_, simple]) + | 0x00 => .ok (index.identity, name, [namespace_, simple]) + | 0x01 => + let (scope, enclosing, parts) ← typeRefQualified env position fuel row.scope + .ok (scope, nestedName enclosing simple, parts ++ [namespace_, simple]) + | _ => .error (.unsupportedScope token) + +/-- The first instance field among `count` consecutive `Field` rows from `row`: a scan that + stops at the first instance field, which an enum declares before its constants. -/ +def firstInstanceField (index : MetadataIndex) : Nat → Nat → Except MetadataError (Option Nat) + | 0, _ => .ok none + | count + 1, row => do + let token ← checkedTokenOf Table.field row .rowOverflow + let field ← index.field token + if field.flags / 0x10 % 2 == 1 then firstInstanceField index count (row + 1) + else .ok (some token) +/-- The first `Field` row after a type's own: the next `TypeDef`'s, or one past the table. -/ +def MetadataIndex.fieldEnd (index : MetadataIndex) (token : Nat) (row : TypeDefRow) : + Except MetadataError Nat := + if tokenRow token == index.header.rows Table.typeDef then + .ok (index.header.rows Table.field + 1) + else do + let next ← index.typeDef (← checkedTokenOf Table.typeDef (tokenRow token + 1) .rowOverflow) + if next.fieldList < row.fieldList then throw (.unresolvedType token) + .ok next.fieldList + +/-- The fields of a type, in table order. -/ +def MetadataIndex.fieldRange (index : MetadataIndex) (token : Nat) : + Except MetadataError (Nat × Nat) := do + let row ← index.typeDef token + .ok (row.fieldList, ← index.fieldEnd token row) + +/-- Whether an element type code is one of the eight integer types an enum may have. -/ +def integerElement (code : Nat) : Bool := 0x04 ≤ code && code ≤ 0x0B + +/-- The integer element type of the single instance field of an enum. -/ +def enumUnderlying (index : MetadataIndex) (token : Nat) : Except MetadataError Nat := do + let (start, stop) ← index.fieldRange token + if index.instanceTotal.find? token != some 1 then throw (.unresolvedType token) + match ← firstInstanceField index (stop - start) start with + | some field => + let row ← index.field field + match ← decodeFieldSig (← index.blob row.signature) with + | .primitive code => + if integerElement code then .ok code else .error (.unresolvedType token) + | _ => .error (.unresolvedType token) + | none => .error (.unresolvedType token) +/-- The namespace `System` as bytes. -/ +def systemNamespace : List Nat := [83, 121, 115, 116, 101, 109] + +/-- `Enum`, the simple name of `System.Enum`, as bytes. -/ +def enumName : List Nat := [69, 110, 117, 109] + +/-- `ValueType`, the simple name of `System.ValueType`, as bytes. -/ +def valueTypeName : List Nat := [86, 97, 108, 117, 101, 84, 121, 112, 101] + +/-- `Object`, the simple name of `System.Object`, as bytes. -/ +def objectName : List Nat := [79, 98, 106, 101, 99, 116] + +/-- The core-library base names spell the texts they stand for; a wrong byte would turn every + struct into a class. -/ +theorem base_names_spell : + spelled? systemNamespace = some "System" ∧ spelled? enumName = some "Enum" ∧ + spelled? valueTypeName = some "ValueType" ∧ spelled? objectName = some "Object" := + ⟨by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl⟩ +#print axioms base_names_spell + +/-- An external storage root must have its exact core name and matching platform definition. -/ +def baseFits (core : Bool) (parts : List (List Nat)) : PlatformKind → Bool + | .enumRoot => core && parts == [systemNamespace, enumName] + | .valueTypeRoot => core && parts == [systemNamespace, valueTypeName] + | _ => false + +/-- The kind of a type whose base `base` no carried slice defines, from the base's platform + definition: an enum with the underlying type of its instance field, a value type or a + class. -/ +def MetadataEnv.externalBaseKind (env : MetadataEnv) (index : MetadataIndex) (token base : Nat) + (scope : List Nat) (parts : List (List Nat)) (core : Bool) : Except MetadataError TypeKind := + if env.pinned.matches scope then .error (.missingSlice base) + else if !core || (parts != [systemNamespace, enumName] && + parts != [systemNamespace, valueTypeName]) then .ok .classType + else match env.platform.require base scope parts (baseFits core parts) with + | .error error => .error error + | .ok .enumRoot => .enumType <$> enumUnderlying index token + | .ok .valueTypeRoot => .ok .valueType + | .ok _ => .ok .classType + +/-- A type is an enum or a value type through an external base only when the base's platform + definition is `System.Enum` or `System.ValueType`, named so in a core-library scope. -/ +theorem MetadataEnv.externalBaseKind_value {env : MetadataEnv} {index : MetadataIndex} + {token base : Nat} {scope : List Nat} {parts : List (List Nat)} {core : Bool} + {kind : TypeKind} (found : env.externalBaseKind index token base scope parts core = .ok kind) + (value : kind = .valueType ∨ ∃ underlying, kind = .enumType underlying) : + core = true ∧ + ({ scope, parts, kind := .enumRoot } ∈ env.platform.definitions ∧ + parts = [systemNamespace, enumName] ∨ + { scope, parts, kind := .valueTypeRoot } ∈ env.platform.definitions ∧ + parts = [systemNamespace, valueTypeName]) := by + unfold MetadataEnv.externalBaseKind at found + split at found + · cases found + split at found + case isTrue => + cases found + simp at value + split at found + · cases found + · rename_i required + have ⟨member, fits⟩ := PlatformTable.require_ok required + simp only [baseFits, Bool.and_eq_true, beq_iff_eq] at fits + exact ⟨fits.1, .inl ⟨member, fits.2⟩⟩ + · rename_i required + have ⟨member, fits⟩ := PlatformTable.require_ok required + simp only [baseFits, Bool.and_eq_true, beq_iff_eq] at fits + exact ⟨fits.1, .inr ⟨member, fits.2⟩⟩ + · cases found + rcases value with value | ⟨_, value⟩ <;> cases value + +#print axioms MetadataEnv.externalBaseKind_value + +/-! ## Generic parameter runs -/ + +/-- The `GenericParam` rows that name `owner`, from the row its link gives while they continue. -/ +private def genericRun (index : MetadataIndex) (owner : Nat) : + Nat → Nat → Except MetadataError (List GenericParamRow) + | 0, _ => .ok [] + | fuel + 1, row => + if index.header.rows Table.genericParam < row then .ok [] + else do + let entry ← index.genericParam (← checkedTokenOf Table.genericParam row .rowOverflow) + if entry.owner != owner then .ok [] + else do .ok (entry :: (← genericRun index owner fuel (row + 1))) + +/-- No `GenericParam` row names `owner`, given the table is sorted by coded owner as ECMA-335 + requires: the first carried row above the owner's code is row 1 or follows a carried row + below it, or the table's last row lies below it, or the table has no rows. -/ +def MetadataIndex.noGenericParameters (index : MetadataIndex) (owner : Nat) : + Except MetadataError Unit := do + let code := genericOwnerCode owner + let codeAt (row : Nat) : Except MetadataError Nat := do + let token ← checkedTokenOf Table.genericParam row .rowOverflow + .ok (genericOwnerCode (← index.genericParam token).owner) + let last := index.header.rows Table.genericParam + let bracketed : Bool ← match index.genericOwners.above? code with + | some (_, (first, _)) => do + let above := decide (code < (← codeAt (tokenRow first))) + if tokenRow first == 1 then pure above + else do pure (above && decide ((← codeAt (tokenRow first - 1)) < code)) + | none => if last == 0 then pure true else do pure (decide ((← codeAt last) < code)) + if !bracketed then throw (.unresolvedMember owner) + +/-- The generic parameters of a `TypeDef` or `MethodDef` owner: every carried parameter of + the owner belongs to its contiguous zero-based run, and an owner with no carried parameter + has neighbours that show it has none. An optional link must name the run's first row in the + current metadata position. -/ +def MetadataIndex.genericParameters (index : MetadataIndex) (position owner : Nat) : + Except MetadataError (List GenericParamRow) := do + let table := metadataTableOf owner + if table != Table.typeDef && table != Table.methodDef then throw (.unsupportedTable owner) + match index.genericOwners.find? (genericOwnerCode owner) with + | none => + if (index.link linkGenericParams owner).isSome then throw (.unresolvedMember owner) + index.noGenericParameters owner + .ok [] + | some (first, count) => do + if (index.link linkGenericParams owner).any (· != (position, first)) then + throw (.unresolvedMember owner) + if metadataTableOf first != Table.genericParam || tokenRow first == 0 then + throw (.tokenOutOfScope first) + if 1 < tokenRow first then + let previous ← checkedTokenOf Table.genericParam (tokenRow first - 1) .rowOverflow + if (← index.genericParam previous).owner == owner then throw (.unresolvedMember owner) + let run ← genericRun index owner (index.header.rows Table.genericParam) (tokenRow first) + if run.length == count && + (run.zipIdx.all fun (entry, number) => entry.number == number) then .ok run + else .error (.unresolvedMember owner) + +/-- The generic parameters of a `MethodDef` whose signature declares `declared` of them. A + signature without the GENERIC flag declares none, as ECMA-335 requires of the method's rows, + so a method with no carried run and no link has none without neighbours; carried rows are + checked as for any owner. -/ +def MetadataIndex.methodGenericParameters (index : MetadataIndex) + (position method declared : Nat) : Except MetadataError (List GenericParamRow) := + if declared == 0 && (index.genericOwners.find? (genericOwnerCode method)).isNone && + (index.link linkGenericParams method).isNone then .ok [] + else index.genericParameters position method + +/-- Resolve a definition link in the referenced assembly or through a top-level forwarder. + The facade's AssemblyRef names the implementation slice and its exported name matches + the original TypeRef. -/ +private def checkedDefinitionLink (env : MetadataEnv) (index : MetadataIndex) + (token target : Nat) (scope : List Nat) (parts : List (List Nat)) : + Except MetadataError (Nat × Nat × List Nat) := do + let some (linked, definition) := index.link linkDefinition token | throw (.unresolvedType token) + if linked == target then return (linked, definition, scope) + let some (facade, exported) := index.link linkForwarder token | throw (.unresolvedType token) + if facade != target then throw (.unresolvedType token) + let source ← env.slice facade exported + if source.identity != scope then throw (.operandMismatch token) + let destination ← source.forwardedAssembly exported parts + if env.identities[destination]? != some linked then throw (.operandMismatch token) + let implementation ← env.slice linked definition + let row ← implementation.typeDef definition + if implementation.identity != destination || row.flags % 8 != 1 then throw (.operandMismatch token) + return (linked, definition, destination) + +/-- A carried reference binds the qualified name in the checked defining assembly. -/ +private def linkedType (env : MetadataEnv) (position token target : Nat) (scope : List Nat) + (parts : List (List Nat)) (resolve : Nat → Nat → Except MetadataError NamedType) : + Except MetadataError NamedType := do + let index ← env.slice position token + let (linked, definition, identity) ← checkedDefinitionLink env index token target scope parts + let named ← resolve linked definition + if named.scope != identity || named.parts != parts then throw (.operandMismatch token) + return named + +/-- A modifier needs the qualified name of each enclosing local definition. -/ +private def modifierDefinition (env : MetadataEnv) (position : Nat) : + Nat → Nat → Except MetadataError NamedType + | 0, token => .error (.unresolvedType token) + | fuel + 1, token => do + let index ← env.slice position token + let row ← index.typeDef token + let simple ← index.string row.name + let namespace_ ← index.string row.namespace_ + let (name, parts) ← + if row.flags % 8 < 2 then pure (qualifiedName namespace_ simple, [namespace_, simple]) + else do + let some (linked, nesting) := index.link linkNesting token + | throw (.unresolvedType token) + if linked != position then throw (.unresolvedType token) + let nested ← index.nestedClass nesting + if nested.nested != token then throw (.unresolvedType token) + let enclosing ← modifierDefinition env position fuel nested.enclosing + pure (nestedName enclosing.name simple, enclosing.parts ++ [namespace_, simple]) + .ok { scope := index.identity, name, parts, kind := .external none, pinned := false } + +/-- The storage kind of a resolved platform type, including an enum's integer element code. -/ +def PlatformKind.resolvedKind : PlatformKind → TypeKind + | .enumType code _ => .enumType code + | kind => .external (some kind.isValue) + +/-- The named type of a reference to a type no carried slice defines: a scope of a pinned name + is refused, and the type's platform definition must agree with the value-or-class hint and + gives its kind. -/ +def MetadataEnv.externalNamed (env : MetadataEnv) (token : Nat) (scope name : List Nat) + (parts : List (List Nat)) (hint : Option Bool) : Except MetadataError NamedType := + if env.pinned.matches scope then .error (.missingSlice token) + else match env.platform.require token scope parts (PlatformKind.fitsHint hint) with + | .error error => .error error + | .ok kind => + .ok { scope, name, parts, kind := kind.resolvedKind, pinned := false, + platformArity := kind.arity } + +/-- An external type has a platform definition of its scope and name parts that agrees with the + hint it was read with and determines its storage kind. -/ +theorem MetadataEnv.externalNamed_ok {env : MetadataEnv} {token : Nat} {scope name : List Nat} + {parts : List (List Nat)} {hint : Option Bool} {named : NamedType} + (resolved : env.externalNamed token scope name parts hint = .ok named) : + ∃ kind, { scope := named.scope, parts := named.parts, kind } ∈ env.platform.definitions ∧ + kind.fitsHint hint = true /\ named.kind = kind.resolvedKind /\ + named.platformArity = kind.arity /\ named.pinned = false := by + unfold MetadataEnv.externalNamed at resolved + split at resolved + · cases resolved + · split at resolved + · cases resolved + · rename_i kind required + cases resolved + exact Exists.intro kind (And.intro (PlatformTable.require_ok required).1 + (And.intro (PlatformTable.require_ok required).2 (And.intro rfl (And.intro rfl rfl)))) + +#print axioms MetadataEnv.externalNamed_ok + +/-- Cecil qualifies each nested segment with its own namespace in a modifier's full name. -/ +private def modifierFullName : List (List Nat) → List Nat + | namespace_ :: simple :: rest => + let name := qualifiedName namespace_ simple + if rest.isEmpty then name else name ++ 47 :: modifierFullName rest + | _ => [] + +/-- A custom modifier names its assembly and enclosing types without requiring the definition + of an externally referenced modifier or the base type of a local one. -/ +def modifierNamed (env : MetadataEnv) (position token : Nat) : Except MetadataError NamedType := do + let (scope, _, parts) ← match metadataTableOf token with + | 0x01 => typeRefQualified env position resolveFuel token + | 0x02 => do + let named ← modifierDefinition env position resolveFuel token + pure (named.scope, named.name, named.parts) + | _ => throw (.unsupportedTable token) + pure { scope, name := modifierFullName parts, parts, kind := .external none, pinned := false } + +/-- A named type in a closed argument comes from a definition row or a scoped platform entry. -/ +inductive ArgumentHead where + | definition (scope : List Nat) (token : Nat) + | platform (scope : List Nat) (parts : List (List Nat)) + deriving DecidableEq, Repr + +mutual + +/-- The primitive, named, vector-array and constructed types admitted as closed arguments. -/ +inductive InstanceArgument where + | primitive (code : Nat) + | named (head : ArgumentHead) + | szarray (element : InstanceArgument) + | genericInst (head : ArgumentHead) (arguments : InstanceArguments) + deriving DecidableEq, Repr + +/-- The ordered arguments of a constructed type. -/ +inductive InstanceArguments where + | nil + | cons (head : InstanceArgument) (tail : InstanceArguments) + deriving DecidableEq, Repr + +end + +/-- Whether a signature's class-or-value-type byte agrees with a type found in a carried + slice. -/ +def hintAgrees : TypeKind → Option Bool → Bool + | .importedOwner, _ => false + | .valueType, some valueType | .enumType _, some valueType => valueType + | .classType, some valueType | .interfaceType, some valueType => !valueType + | _, _ => true + +/-- The number of generic parameters of the definition a `TypeDef` token, or a `TypeRef` linked + to a carried slice, names. -/ +def MetadataEnv.definitionArity (env : MetadataEnv) (position token : Nat) : + Except MetadataError Nat := do + let index ← env.slice position token + let (linked, definition) ← match metadataTableOf token with + | 0x02 => pure (position, token) + | 0x01 => + match index.link linkDefinition token with + | some target => pure target + | none => throw (.unresolvedType token) + | _ => throw (.unsupportedTable token) + .ok (← (← env.slice linked definition).genericParameters linked definition).length + +/-- A named head takes the arity of its carried rows or its resolved platform definition. -/ +def MetadataEnv.requireArity (env : MetadataEnv) (position token : Nat) (named : NamedType) + (arity : Nat) : Except MetadataError Unit := + let check (count : Nat) : Except MetadataError Unit := + if count == arity then .ok () else .error (.genericMismatch token) + if named.pinned then do check (<- env.definitionArity position token) + else check named.platformArity + +mutual + +/-- Resolve every token of a signature type read in the slice at `position`. -/ +def resolveType (env : MetadataEnv) (position : Nat) + (readNamed : Nat -> Option Bool -> Except MetadataError NamedType) : + Nat -> SigType -> Except MetadataError ResolvedType + | 0, _ => .error (.unresolvedType 0) + | _ + 1, .primitive code => .ok (.primitive code) + | _ + 1, .named valueType token => do + let named <- readNamed token (some valueType) + env.requireArity position token named 0 + .ok (.named named) + | _ + 1, .var index => .ok (.var index) + | _ + 1, .mvar index => .ok (.mvar index) + | fuel + 1, .byref element => do + .ok (.byref (<- resolveType env position readNamed fuel element)) + | fuel + 1, .pointer element => do + .ok (.pointer (<- resolveType env position readNamed fuel element)) + | fuel + 1, .szarray element => do + .ok (.szarray (<- resolveType env position readNamed fuel element)) + | fuel + 1, .genericInst valueType token arguments => do + if let .nil := arguments then throw (.genericMismatch token) + let head <- readNamed token (some valueType) + let resolved <- resolveList env position readNamed fuel arguments + env.requireArity position token head resolved.length + .ok (.genericInst head resolved) + | fuel + 1, .functionPointer convention result parameters => do + let resolved <- resolveType env position readNamed fuel result + let types <- resolveList env position readNamed fuel parameters + .ok (.functionPointer convention resolved types) + | fuel + 1, .modified required modifier element => do + let named <- modifierNamed env position modifier + .ok (.modified required named (<- resolveType env position readNamed fuel element)) + +/-- Resolve a chain of signature types. -/ +def resolveList (env : MetadataEnv) (position : Nat) + (readNamed : Nat -> Option Bool -> Except MetadataError NamedType) : + Nat -> SigList -> Except MetadataError ResolvedList + | 0, _ => .error (.unresolvedType 0) + | _ + 1, .nil => .ok .nil + | fuel + 1, .cons head tail => do + let type <- resolveType env position readNamed fuel head + .ok (.cons type (<- resolveList env position readNamed fuel tail)) + +end + +/-- The definition of a resolved reference, or its external scope, spelling and name parts. -/ +abbrev ReferenceTarget := Sum NamedType (Prod (List Nat) (Prod (List Nat) (List (List Nat)))) + +/-- A reference links to its scoped definition or identifies an external name. -/ +private def referenceTarget (env : MetadataEnv) (position token : Nat) + (resolve : Nat -> Nat -> Except MetadataError NamedType) : + Except MetadataError ReferenceTarget := do + let (scope, name, parts) <- typeRefQualified env position resolveFuel token + match env.identities[scope]? with + | none => return .inr (scope, name, parts) + | some target => .inl <$> linkedType env position token target scope parts resolve + +/-- A signature head resolves through its definition or its scoped platform entry. -/ +private def signatureHead (env : MetadataEnv) (position : Nat) + (resolve : Nat -> Nat -> Except MetadataError NamedType) (token : Nat) (hint : Option Bool) : + Except MetadataError NamedType := do + let named <- match metadataTableOf token with + | 0x02 => resolve position token + | 0x01 => do + match <- referenceTarget env position token resolve with + | .inl named => pure named + | .inr (scope, name, parts) => + match env.externalNamed token scope name parts hint with + | .error (.missingPlatformDefinition missing) => + let published := containsName env.publishedLayouts (scope ++ [58, 58] ++ name) + if hint != some false && published then + pure { scope, name, parts, kind := .valueType, pinned := false } + else throw (.missingPlatformDefinition missing) + | result => result + | _ => throw (.unsupportedTable token) + if hintAgrees named.kind hint then return named else throw (.operandMismatch token) + +/-- The namespace and simple names of a definition, from the outermost type inwards. -/ +private def definitionParts (index : MetadataIndex) (position fuel token : Nat) (row : TypeDefRow) + (inner : List (List Nat) := []) : Except MetadataError (List (List Nat)) := do + match fuel with + | 0 => throw (.unresolvedType token) + | rest + 1 => + let parts := (<- index.string row.namespace_) :: (<- index.string row.name) :: inner + if row.flags % 8 < 2 then return parts + let some (linked, nesting) := index.link linkNesting token | throw (.unresolvedType token) + if linked != position then throw (.unresolvedType token) + let nested <- index.nestedClass nesting + if nested.nested != token then throw (.unresolvedType token) + definitionParts index position rest nested.enclosing (<- index.typeDef nested.enclosing) parts + +/-- A resolved nesting chain yields the same name parts with extra lookup fuel. -/ +private theorem definitionParts_add (index : MetadataIndex) (position fuel extra token : Nat) + (row : TypeDefRow) (inner result : List (List Nat)) + (h : definitionParts index position fuel token row inner = .ok result) : + definitionParts index position (fuel + extra) token row inner = .ok result := by + induction fuel generalizing token row inner with + | zero => simp [definitionParts, throw] at h + | succ fuel ih => + rw [Nat.succ_add] + simp only [definitionParts] at h ⊢ + cases hn : index.string row.namespace_ <;> simp [hn, Bind.bind, Except.bind] at h ⊢ + cases hs : index.string row.name <;> simp [hs] at h ⊢ + split at h <;> rename_i branch + · simpa [branch] using h + · simp [branch] + cases hl : index.link linkNesting token with + | none => simp [hl, throw] at h + | some pair => + obtain ⟨linked, nesting⟩ := pair + simp [hl] at h ⊢ + by_cases scope : linked = position + · simp [scope] at h ⊢ + cases hr : index.nestedClass nesting with + | error err => simp [hr] at h + | ok nested => + simp [hr] at h ⊢ + by_cases same : nested.nested = token + · simp [same] at h ⊢ + cases ht : index.typeDef nested.enclosing <;> simp [ht] at h ⊢ + exact ih _ _ _ h + · simp [same, throw] at h + · simp [scope, throw] at h +#print axioms definitionParts_add + +/-- A carried root gives a storage kind only under its own core assembly and exact name. -/ +private def carriedRootKind (index : MetadataIndex) (token : Nat) (scope : List Nat) + (parts : List (List Nat)) : Except MetadataError TypeKind := do + if !isCorlibScope scope then return .classType + if parts == [systemNamespace, enumName] then return .enumType (<- enumUnderlying index token) + if parts == [systemNamespace, valueTypeName] then return .valueType + return .classType + +/-- Flags and the exact identity of an immediate named base determine the storage kind. -/ +def typeDefKind (env : MetadataEnv) (index : MetadataIndex) (position token : Nat) + (row : TypeDefRow) (ownParts : List (List Nat)) : Except MetadataError TypeKind := do + if row.flags &&& 0x20 != 0 then return .interfaceType + let kind <- match metadataTableOf row.extends_ with + | 0x02 => do + if tokenRow row.extends_ == 0 || !isCorlibScope index.identity then pure .classType + else + let base <- index.typeDef row.extends_ + carriedRootKind index token index.identity + (<- definitionParts index position resolveFuel row.extends_ base) + | 0x01 => do + let (scope, _, parts) <- typeRefQualified env position resolveFuel row.extends_ + match env.identities[scope]? with + | none => do + let core <- index.coreLibraryReference row.extends_ + env.externalBaseKind index token row.extends_ scope parts core + | some target => do + let (linked, definition, identity) ← checkedDefinitionLink env index row.extends_ target scope parts + let base <- env.slice linked definition + let baseParts <- definitionParts base linked resolveFuel definition + (<- base.typeDef definition) + if base.identity != identity || baseParts != parts then throw (.operandMismatch row.extends_) + carriedRootKind index token base.identity baseParts + | _ => pure .classType + if kind == .valueType && ownParts == [systemNamespace, enumName] && + isCorlibScope index.identity then return .classType + return kind + +/-- A carried definition's scoped name and kind, independent of its base's layout. -/ +def typeDefNamed (env : MetadataEnv) (position fuel token : Nat) : + Except MetadataError NamedType := do + let index <- env.slice position token + let row <- index.typeDef token + let parts <- definitionParts index position fuel token row + let some name := partsName parts | throw (.unresolvedType token) + let kind <- typeDefKind env index position token row parts + return { scope := index.identity, name, parts, kind, pinned := true } + +/-- Once the enclosing names resolve, any larger nesting budget recovers the same definition. -/ +theorem typeDefNamed_of_le {env : MetadataEnv} {position fuel budget token : Nat} {named : NamedType} + (bound : fuel ≤ budget) (resolved : typeDefNamed env position fuel token = .ok named) : + typeDefNamed env position budget token = .ok named := by + obtain ⟨extra, rfl⟩ := Nat.exists_eq_add_of_le bound + unfold typeDefNamed at resolved ⊢ + cases hi : env.slice position token with + | error err => simp [hi, Bind.bind, Except.bind] at resolved + | ok index => + simp [hi, Bind.bind, Except.bind] at resolved ⊢ + cases hr : index.typeDef token with + | error err => simp [hr] at resolved + | ok row => + simp [hr] at resolved ⊢ + cases hp : definitionParts index position fuel token row with + | error err => simp [hp] at resolved + | ok parts => + rw [definitionParts_add index position fuel extra token row [] parts hp] + simpa only [hp] using resolved +#print axioms typeDefNamed_of_le + +/-- What a `TypeRef` names before any platform lookup: for a scope that names a carried slice, + the `TypeDef` the slice's definition link reaches, whose name must be the one the reference + spells; for any other scope, the scope, name and name parts of a type no carried slice + defines. -/ +def typeRefTarget (env : MetadataEnv) (position : Nat) : + Nat -> Nat -> Except MetadataError ReferenceTarget + | 0, token => .error (.unresolvedType token) + | fuel + 1, token => do + referenceTarget env position token (fun linked => typeDefNamed env linked fuel) + +/-- The named type of a `TypeRef` (`typeRefTarget`); a type no carried slice defines is external + (`MetadataEnv.externalNamed`). -/ +def typeRefNamed (env : MetadataEnv) (position fuel token : Nat) (hint : Option Bool) : + Except MetadataError NamedType := do + match <- typeRefTarget env position fuel token with + | .inl named => .ok named + | .inr (scope, name, parts) => env.externalNamed token scope name parts hint + +/-- The named type of a `TypeDef` or `TypeRef` token read with a value-or-class byte. -/ +def namedOf (env : MetadataEnv) (position token : Nat) (hint : Option Bool) : + Except MetadataError NamedType := + signatureHead env position (fun linked => typeDefNamed env linked resolveFuel) token hint + +/-- Resolve a closed argument's head and check the arity of a carried definition. -/ +def ArgumentHead.resolve (env : MetadataEnv) (method arity : Nat) : + ArgumentHead → Except MetadataError NamedType + | .definition scope token => do + if !env.exactPins.contains scope then throw (.missingSlice token) + let some position := env.identities[scope]? | throw (.missingSlice token) + let named ← typeDefNamed env position resolveFuel token + env.requireArity position token named arity + return named + | .platform scope parts => do + if env.identities.contains scope then throw (.unresolvedType method) + let some name := partsName parts | throw (.unresolvedType method) + let named <- env.externalNamed method scope name parts none + if named.platformArity != arity then throw (.genericMismatch method) + return named + +mutual + +/-- Resolve every named component of a closed argument before comparing its storage or name. -/ +def InstanceArgument.resolve (env : MetadataEnv) (method : Nat) : + Nat → InstanceArgument → Except MetadataError ResolvedType + | 0, _ => .error (.unresolvedType method) + | _ + 1, .primitive code => .ok (.primitive code) + | _ + 1, .named head => .named <$> head.resolve env method 0 + | fuel + 1, .szarray element => .szarray <$> element.resolve env method fuel + | fuel + 1, .genericInst head arguments => do + if let .nil := arguments then throw (.genericMismatch method) + let types <- InstanceArguments.resolve env method fuel arguments + let named <- head.resolve env method types.length + return .genericInst named types + +/-- Resolve the arguments of a constructed type in declaration order. -/ +def InstanceArguments.resolve (env : MetadataEnv) (method : Nat) : + Nat → InstanceArguments → Except MetadataError ResolvedList + | _, .nil => .ok .nil + | 0, _ => .error (.unresolvedType method) + | fuel + 1, .cons head tail => do + return .cons (← head.resolve env method fuel) (← tail.resolve env method fuel) + +end + +/-- Resolve a signature type read in the slice at `position`. -/ +def MetadataEnv.resolve (env : MetadataEnv) (position : Nat) (type : SigType) : + Except MetadataError ResolvedType := + resolveType env position (namedOf env position) (resolveFuel + 2) type + +/-- The signature of a TypeSpec, including the marker and token of a generic head. -/ +def MetadataIndex.typeSpec (index : MetadataIndex) (token : Nat) : + Except MetadataError SigType := do + decodeTypeSpec (← index.blob (← index.signatureOf Table.typeSpec token)) + +/-- The type a `TypeDef`, `TypeRef` or `TypeSpec` token names. -/ +def MetadataEnv.resolveToken (env : MetadataEnv) (position token : Nat) : + Except MetadataError ResolvedType := + match metadataTableOf token with + | 0x02 | 0x01 => do + let named ← namedOf env position token none + env.requireArity position token named 0 + .ok (.named named) + | 0x1B => do + let index ← env.slice position token + env.resolve position (← index.typeSpec token) + | _ => .error (.unsupportedTable token) + +/-! ## Spelling -/ + +/-- `System.`, the namespace of every primitive type with its separator, as bytes. -/ +def systemPrefix : List Nat := systemNamespace ++ [46] + +/-- `System.String` as bytes. -/ +def systemStringName : List Nat := systemPrefix ++ [83, 116, 114, 105, 110, 103] + +/-- `System.Object` as bytes. -/ +def systemObjectName : List Nat := systemPrefix ++ objectName + +/-- `ValueTuple\``, the stem of every value tuple's simple name, as bytes. -/ +def valueTupleSimpleStem : List Nat := [86, 97, 108, 117, 101, 84, 117, 112, 108, 101, 96] + +/-- `System.ValueTuple\``, the stem of every value tuple's name, as bytes. -/ +def valueTupleStem : List Nat := systemPrefix ++ valueTupleSimpleStem + +/-- `ValueTuple\`2`, the simple name of the pair, as bytes. -/ +def valueTuplePairName : List Nat := valueTupleSimpleStem ++ [50] + +/-- `Item1`, the pair's first field, as bytes. -/ +def firstItemName : List Nat := [73, 116, 101, 109, 49] + +/-- `Item2`, the pair's second field, as bytes. -/ +def secondItemName : List Nat := [73, 116, 101, 109, 50] + +/-- The value tuple names spell the texts they stand for. -/ +theorem value_tuple_names_spell : + spelled? valueTupleStem = some "System.ValueTuple`" ∧ + spelled? valueTuplePairName = some "ValueTuple`2" ∧ + spelled? firstItemName = some "Item1" ∧ spelled? secondItemName = some "Item2" := + ⟨by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl⟩ +#print axioms value_tuple_names_spell + +/-- The `System` type name of an element type byte, as bytes, so that no decision evaluates a + string; `primitiveName_spells` gives the text of each. -/ +def primitiveName : Nat → Option (List Nat) + | 0x01 => some (systemPrefix ++ [86, 111, 105, 100]) + | 0x02 => some (systemPrefix ++ [66, 111, 111, 108, 101, 97, 110]) + | 0x03 => some (systemPrefix ++ [67, 104, 97, 114]) + | 0x04 => some (systemPrefix ++ [83, 66, 121, 116, 101]) + | 0x05 => some (systemPrefix ++ [66, 121, 116, 101]) + | 0x06 => some (systemPrefix ++ [73, 110, 116, 49, 54]) + | 0x07 => some (systemPrefix ++ [85, 73, 110, 116, 49, 54]) + | 0x08 => some (systemPrefix ++ [73, 110, 116, 51, 50]) + | 0x09 => some (systemPrefix ++ [85, 73, 110, 116, 51, 50]) + | 0x0A => some (systemPrefix ++ [73, 110, 116, 54, 52]) + | 0x0B => some (systemPrefix ++ [85, 73, 110, 116, 54, 52]) + | 0x0C => some (systemPrefix ++ [83, 105, 110, 103, 108, 101]) + | 0x0D => some (systemPrefix ++ [68, 111, 117, 98, 108, 101]) + | 0x0E => some systemStringName + | 0x18 => some (systemPrefix ++ [73, 110, 116, 80, 116, 114]) + | 0x19 => some (systemPrefix ++ [85, 73, 110, 116, 80, 116, 114]) + | 0x1C => some systemObjectName + | _ => none + +/-- Each element type byte with a name spells the `System` type it stands for. -/ +theorem primitiveName_spells : + [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x18, + 0x19, 0x1C].map (fun code => (primitiveName code).bind spelled?) = + ["System.Void", "System.Boolean", "System.Char", "System.SByte", "System.Byte", + "System.Int16", "System.UInt16", "System.Int32", "System.UInt32", "System.Int64", + "System.UInt64", "System.Single", "System.Double", "System.String", "System.IntPtr", + "System.UIntPtr", "System.Object"].map some := by + cil_kernel_rfl +#print axioms primitiveName_spells + +mutual + +/-- Cecil's `FullName` of a type: nested types as `Outer/Inner`, instances as `Head`, + references as `T&`, pointers as `T*`, vectors as `T[]`, parameters as `!n` and `!!n` and + modifiers as `T modreq(M)`. With `pointers`, a function pointer is `method R *(P,Q)`; its + calling convention is not spelled, so an identity compared across methods leaves it out. -/ +def cecilName (pointers : Bool) : Nat → ResolvedType → Option (List Nat) + | 0, _ => none + | _ + 1, .primitive code => primitiveName code + | _ + 1, .named named => some named.name + -- `!` and `!!` + | _ + 1, .var index => some (33 :: decimalDigits 10 index) + | _ + 1, .mvar index => some (33 :: 33 :: decimalDigits 10 index) + | fuel + 1, .byref element => (· ++ [38]) <$> cecilName pointers fuel element + | fuel + 1, .pointer element => (· ++ [42]) <$> cecilName pointers fuel element + | fuel + 1, .szarray element => (· ++ [91, 93]) <$> cecilName pointers fuel element + | fuel + 1, .genericInst head arguments => do + let inner ← cecilNames pointers fuel arguments + some (head.name ++ [60] ++ inner ++ [62]) + | fuel + 1, .modified required modifier element => do + let spelled ← cecilName pointers fuel element + -- ` modreq(` and ` modopt(` + let marker := if required then [32, 109, 111, 100, 114, 101, 113, 40] + else [32, 109, 111, 100, 111, 112, 116, 40] + some (spelled ++ marker ++ modifier.name ++ [41]) + | fuel + 1, .functionPointer _ result parameters => + if pointers then do + let returned ← cecilName pointers fuel result + let spelled ← cecilNames pointers fuel parameters + -- `method ` and ` *(` + some ([109, 101, 116, 104, 111, 100, 32] ++ returned ++ [32, 42, 40] ++ spelled ++ [41]) + else none + +/-- The names of a chain of types, separated by commas. -/ +def cecilNames (pointers : Bool) : Nat → ResolvedList → Option (List Nat) + | 0, _ => none + | _ + 1, .nil => some [] + | fuel + 1, .cons head .nil => cecilName pointers fuel head + | fuel + 1, .cons head tail => do + let first ← cecilName pointers fuel head + let rest ← cecilNames pointers fuel tail + some (first ++ [44] ++ rest) + +end + +mutual + +/-- An aggregate identity scopes its head and each argument. A primitive argument uses the + primitive assembly of the signature that contains it. -/ +def aggregateSpelling (corlib : Option (List Nat)) : Nat -> ResolvedType -> Option (List Nat) + | 0, _ => none + | _ + 1, .primitive code => do + let name ← primitiveName code + some ((<- corlib) ++ [58, 58] ++ name) + | _ + 1, .named named => some (named.scope ++ [58, 58] ++ named.name) + | fuel + 1, .genericInst head arguments => do + let inner <- aggregateSpellings corlib fuel arguments + some (head.scope ++ [58, 58] ++ head.name ++ [60] ++ inner ++ [62]) + | _ + 1, _ => none + +/-- The identities of a chain of types, separated by commas. -/ +def aggregateSpellings (corlib : Option (List Nat)) : Nat -> ResolvedList -> Option (List Nat) + | 0, _ => none + | _ + 1, .nil => some [] + | fuel + 1, .cons head .nil => aggregateSpelling corlib fuel head + | fuel + 1, .cons head tail => do + let first ← aggregateSpelling corlib fuel head + let rest ← aggregateSpellings corlib fuel tail + some (first ++ [44] ++ rest) + +end + +/-! ## Substitution and the modifiers a call site drops -/ + +mutual + +/-- Substitute call-site generic arguments for the parameters of a signature, as Cecil does: + a parameter with no arguments stays, an argument list too short for a parameter fails, and + modifiers and function pointers are left as written. -/ +def substituteResolved (methodArguments ownerArguments : Option (List ResolvedType)) : + Nat → ResolvedType → Option ResolvedType + | 0, _ => none + | _ + 1, .var index => + match ownerArguments with + | none => some (.var index) + | some arguments => arguments[index]? + | _ + 1, .mvar index => + match methodArguments with + | none => some (.mvar index) + | some arguments => arguments[index]? + | fuel + 1, .byref element => + .byref <$> substituteResolved methodArguments ownerArguments fuel element + | fuel + 1, .pointer element => + .pointer <$> substituteResolved methodArguments ownerArguments fuel element + | fuel + 1, .szarray element => + .szarray <$> substituteResolved methodArguments ownerArguments fuel element + | fuel + 1, .genericInst head arguments => + .genericInst head <$> substituteResolvedList methodArguments ownerArguments fuel arguments + | _ + 1, type => some type + +/-- Substitute through a chain of types. -/ +def substituteResolvedList (methodArguments ownerArguments : Option (List ResolvedType)) : + Nat → ResolvedList → Option ResolvedList + | 0, _ => none + | _ + 1, .nil => some .nil + | fuel + 1, .cons head tail => do + some (.cons (← substituteResolved methodArguments ownerArguments fuel head) + (← substituteResolvedList methodArguments ownerArguments fuel tail)) + +end + +mutual + +/-- Whether a type has a custom modifier anywhere inside it. -/ +def hasModifier : Nat → ResolvedType → Bool + | 0, _ => true + | _ + 1, .modified .. => true + | fuel + 1, .byref element => hasModifier fuel element + | fuel + 1, .pointer element => hasModifier fuel element + | fuel + 1, .szarray element => hasModifier fuel element + | fuel + 1, .genericInst _ arguments => listHasModifier fuel arguments + | fuel + 1, .functionPointer _ result parameters => + hasModifier fuel result || listHasModifier fuel parameters + | _ + 1, _ => false + +/-- Whether a chain of types has a custom modifier anywhere inside it. -/ +def listHasModifier : Nat → ResolvedList → Bool + | 0, _ => true + | _ + 1, .nil => false + | fuel + 1, .cons head tail => hasModifier fuel head || listHasModifier fuel tail +end + +/-- The namespace `System.Runtime.InteropServices` as bytes. -/ +def interopServicesNamespace : List Nat := + systemPrefix ++ [82, 117, 110, 116, 105, 109, 101, 46, 73, 110, 116, 101, 114, 111, 112, 83, + 101, 114, 118, 105, 99, 101, 115] + +/-- `InAttribute`, the simple name of the modifier of `in` parameters, as bytes. -/ +def inAttributeName : List Nat := [73, 110, 65, 116, 116, 114, 105, 98, 117, 116, 101] + +/-- The namespace `System.Runtime.CompilerServices` as bytes. -/ +def compilerServicesNamespace : List Nat := + systemPrefix ++ [82, 117, 110, 116, 105, 109, 101, 46, 67, 111, 109, 112, 105, 108, 101, 114, + 83, 101, 114, 118, 105, 99, 101, 115] + +/-- `IsExternalInit`, the simple name of the modifier of `init` accessors, as bytes. -/ +def externalInitName : List Nat := + [73, 115, 69, 120, 116, 101, 114, 110, 97, 108, 73, 110, 105, 116] + +/-- `runtimeScope` as bytes. -/ +def runtimeScopeBytes : List Nat := + [83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 44, 32, + 86, 101, 114, 115, 105, 111, 110, 61, 49, 49, 46, 48, 46, 48, 46, 48, + 44, 32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, + 108, 44, 32, 80, 117, 98, 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, + 110, 61, 98, 48, 51, 102, 53, 102, 55, 102, 49, 49, 100, 53, 48, 97, + 51, 97] + +/-- The modifier names and the runtime scope spell the texts they stand for. -/ +theorem modifier_names_spell : + spelled? interopServicesNamespace = some "System.Runtime.InteropServices" ∧ + spelled? inAttributeName = some "InAttribute" ∧ + spelled? compilerServicesNamespace = some "System.Runtime.CompilerServices" ∧ + spelled? externalInitName = some "IsExternalInit" ∧ + spelled? runtimeScopeBytes = some runtimeScope := + ⟨by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl⟩ +#print axioms modifier_names_spell + +/-- `System.Runtime`'s `System.Runtime.InteropServices.InAttribute`, by namespace and simple + name, the modifier Roslyn writes on `in` parameters. -/ +def isInModifier (modifier : NamedType) : Bool := + modifier.parts == [interopServicesNamespace, inAttributeName] && + modifier.scope == runtimeScopeBytes + +/-- `System.Runtime.CompilerServices.IsExternalInit` from any assembly, by namespace and simple + name, the marker Roslyn writes on `init` accessors. -/ +def isInitModifier (modifier : NamedType) : Bool := + modifier.parts == [compilerServicesNamespace, externalInitName] + +/-- The reference an `in` parameter's `modreq(InAttribute)` wraps: modified directly, once. -/ +def inReference : ResolvedType → Option ResolvedType + | .modified true modifier (.byref element) => + if isInModifier modifier && !hasModifier resolveFuel element then some element else none + | _ => none + +/-- The type of a parameter at run time: an `in` reference loses its modifier. -/ +def declaredParameter (type : ResolvedType) : ResolvedType := + match inReference type with + | some element => .byref element + | none => type + +/-- The type a method returns at run time: an `init` accessor's `void` loses its modifier. -/ +def declaredReturn : ResolvedType → ResolvedType + | .modified true modifier (.primitive 1) => + if isInitModifier modifier then .primitive 1 else .modified true modifier (.primitive 1) + | type => type + +/-- A call site's parameter type: substituted inside an `in` reference, which then loses its + modifier, and substituted whole otherwise. -/ +def siteParameter (methodArguments ownerArguments : Option (List ResolvedType)) + (type : ResolvedType) : Option ResolvedType := + match inReference type with + | some element => + .byref <$> substituteResolved methodArguments ownerArguments resolveFuel element + | none => substituteResolved methodArguments ownerArguments resolveFuel type + +/-- A call site's return type: substituted, then an `init` accessor's `void` loses its + modifier. -/ +def siteReturn (methodArguments ownerArguments : Option (List ResolvedType)) + (type : ResolvedType) : Option ResolvedType := + declaredReturn <$> substituteResolved methodArguments ownerArguments resolveFuel type + +/-- A declared type of a method instantiated at the given arguments, as Cecil writes the + instance: an `in` reference is substituted inside and keeps its modifier, any other type is + substituted whole, and a parameter with no argument fails. -/ +def instanceType (methodArguments ownerArguments : List ResolvedType) (type : ResolvedType) : + Option ResolvedType := + match type with + | .modified true modifier (.byref element) => + if isInModifier modifier && !hasModifier resolveFuel element then + (fun inner => .modified true modifier (.byref inner)) <$> + substituteResolved (some methodArguments) (some ownerArguments) resolveFuel element + else substituteResolved (some methodArguments) (some ownerArguments) resolveFuel type + | _ => substituteResolved (some methodArguments) (some ownerArguments) resolveFuel type + +/-! ## The relation between resolved types and the extractor's types -/ + +/-- How the extractor's types read the generic parameters and the primitive types of one + signature: the identity of the primitive assembly, the scope Cecil gives a modified type + whose element is a generic parameter, the number of method generic parameters that precede + the owner's, and the number of the owner's. -/ +structure Frame where + corlib : Option (List Nat) + anchor : List Nat + arity : Nat + ownerArity : Nat + /-- A member declaration numbers each owner's variables from zero in its own signature. -/ + rawIndices : Bool := false + + +/-- A class, interface or external type that may be a class. -/ +def classLike : TypeKind → Bool + | .classType | .interfaceType | .external (some false) | .external none => true + | _ => false + +/-- A named type an extractor `object` names outside a class field: a class of a carried slice, + whose instances record their lineage, or `System.String` of the runtime assembly. An + interface or external class records no lineage a type test or array store could decide. -/ +def objectNamed (named : NamedType) : Bool := + named.pinned && named.kind == .classType || + named.scope == runtimeScopeBytes && named.name == systemStringName && + (named.kind == .external (some false) || named.kind == .classType) + +/-- Whether a generic definition of this kind is a reference type, when the metadata decides it: + a class or interface of a carried slice or a reference the signature marks as a class is one, + a struct, an enum or a reference the signature marks as a value type is not. -/ +def TypeKind.reference? : TypeKind → Option Bool + | .classType | .interfaceType | .external (some false) => some true + | .valueType | .enumType _ | .external (some true) => some false + | .external none | .importedOwner => none + +/-- A value type, or an external type that may be one. -/ +def valueLike : TypeKind → Bool + | .valueType | .external (some true) | .external none => true + | _ => false + +/-- Whether the resolved definition gives the named type enum storage. -/ +def NamedType.isEnum (named : NamedType) : Bool := + match named.kind with + | .enumType _ => true + | _ => false + +mutual + +/-- A value argument of a closed aggregate may itself be a closed value instance. -/ +private def aggregateValueArgument : Nat -> ResolvedType -> Bool + | 0, _ => false + | _ + 1, .primitive code => code != 0x01 && code != 0x0E && code != 0x1C + | _ + 1, .named named => valueLike named.kind || named.isEnum + | fuel + 1, .genericInst head arguments => + (valueLike head.kind || head.isEnum) && aggregateValueArgs fuel arguments + | _ + 1, _ => false + +/-- Every argument of a preclosed aggregate must have a value kind throughout its type. -/ +private def aggregateValueArgs : Nat -> ResolvedList -> Bool + | 0, _ => false + | _ + 1, .nil => true + | fuel + 1, .cons head tail => aggregateValueArgument fuel head && aggregateValueArgs fuel tail + +end + +/-- Whether a name is that of a primitive type to which `typeStorage` gives value storage as an + atom of the runtime scope, or a floating-point primitive. -/ +def valueAtomName (name : List Nat) : Bool := + [0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x18, 0x19].any + fun code => + primitiveName code == some name + +/-- The longest primitive names are System.Boolean and System.UIntPtr, each fourteen bytes. -/ +private def longestPrimitiveName : Nat := 14 + +/-- A named runtime primitive has fixed storage and cannot use a supplied layout. -/ +def NamedType.fixedStorage (named : NamedType) : Bool := + (named.name.drop longestPrimitiveName).isEmpty && named.scope == runtimeScopeBytes && + (named.name == systemStringName || named.name == systemObjectName || valueAtomName named.name) + +mutual + +/-- Whether the extractor's `TypeExpr` names the resolved type. The scope, the name and the kind + facts come from the metadata; the constructor is the extractor's choice among those the kind + allows. `fuel` bounds the nesting. -/ +def typeAdmits (frame : Frame) : Nat → ResolvedType → TypeExpr → Bool + | 0, _, _ => false + | fuel + 1, type, expr => + match type, expr with + | .primitive code, .void => code == 0x01 + | .primitive code, .atom scope name => + code != 0x01 && frame.corlib == some (bytesOf scope) && + primitiveName code == some (bytesOf name) + | .primitive 0x0E, .object scope name => + frame.corlib == some (bytesOf scope) && bytesOf name == systemStringName + -- A type that cannot be a value type is never the runtime's primitive with value storage. + | .named named, .atom scope name => + named.scope == bytesOf scope && named.name == bytesOf name && !named.isEnum && + named.kind != .importedOwner && + (!named.fixedStorage || named.kind.reference? == + some (named.name == systemStringName || named.name == systemObjectName)) + | .named named, .object scope name => + named.scope == bytesOf scope && named.name == bytesOf name && objectNamed named && + (!named.fixedStorage || named.name == systemStringName) + | .named named, .aggregate identity _ => + bytesOf identity == named.scope ++ [58, 58] ++ named.name && valueLike named.kind && + !named.fixedStorage + | .named named, .enum identity underlying => + bytesOf identity == named.scope ++ [58, 58] ++ named.name && + match named.kind with + | .enumType code => + (named.pinned || partsName named.parts == some named.name) && !named.fixedStorage && + integerElement code && typeAdmits frame fuel (.primitive code) underlying + | _ => false + | .var index, .ownerParameter number => + frame.rawIndices && index < frame.ownerArity && number == index + | .var index, .parameter number => + !frame.rawIndices && index < frame.ownerArity && number == frame.arity + index + -- Past the arity, a method parameter's index would be an owner parameter's. + | .mvar index, .parameter number => index < frame.arity && number == index + | .byref element, .byref inner => typeAdmits frame fuel element inner + | .pointer element, .pointer inner => typeAdmits frame fuel element inner + | .szarray element, .array inner => typeAdmits frame fuel element inner + | .genericInst head arguments, .aggregate identity _ => + valueLike head.kind && aggregateValueArgs fuel arguments && + (if head.pinned || (head.parts == [systemNamespace, valueTuplePairName] && + head.scope == bytesOf runtimeScope) then + match frame.corlib with + | some corlib => aggregateSpelling (some corlib) fuel (.genericInst head arguments) + | none => none + else (fun spelled => head.scope ++ [58, 58] ++ spelled) <$> + cecilName false fuel (.genericInst head arguments)) == some (bytesOf identity) + | .genericInst head arguments, expr => appliedAdmits frame fuel head arguments expr + | .functionPointer convention result parameters, + .functionPointer expectedResult expectedParameters expectedConvention hasThis + explicitThis => + expectedConvention == convention % 0x20 && hasThis == (convention / hasThisFlag % 2 == 1) && + explicitThis == (convention / explicitThisFlag % 2 == 1) && + typeAdmits frame fuel result expectedResult && + chainAdmits frame fuel parameters expectedParameters + | .modified required modifier element, .modified claimed scope name inner => + frame.rawIndices && required == claimed && modifier.scope == bytesOf scope && + modifier.name == bytesOf name && typeAdmits frame fuel element inner + | .modified required modifier element, .atom scope name => + !frame.rawIndices && bytesOf scope == frame.anchor && + cecilName false fuel (.modified required modifier element) == some (bytesOf name) + | _, _ => false + +/-- A generic instance binds the constructor's scope, name and value or reference kind. -/ +def appliedAdmits (frame : Frame) : Nat → NamedType → ResolvedList → TypeExpr → Bool + | 0, _, _, _ => false + | fuel + 1, head, arguments, expr => + argumentsAdmit frame fuel arguments (fun + | .constructor scope name => + head.kind.reference? == some false && bytesOf scope == head.scope && + bytesOf name == head.name + | .objectConstructor scope name => + head.kind.reference? == some true && bytesOf scope == head.scope && + bytesOf name == head.name + | _ => false) expr + +/-- The last decoded argument matches the outermost application. Each match consumes one + constructor, stopping on the first mismatch and preserving the fuel of each decoded type. -/ +def argumentsAdmit (frame : Frame) : Nat → ResolvedList → (TypeExpr → Bool) → TypeExpr → Bool + | 0, _, _, _ => false + | _ + 1, .nil, prefixMatches, expr => prefixMatches expr + | fuel + 1, .cons argument rest, prefixMatches, expr => + argumentsAdmit frame fuel rest (fun + | .apply constructor last => typeAdmits frame fuel argument last && prefixMatches constructor + | _ => false) expr + +/-- Whether the parameters of a function pointer are the `signatureParameter` chain, which + ends in `void`. -/ +def chainAdmits (frame : Frame) : Nat → ResolvedList → TypeExpr → Bool + | 0, _, _ => false + | _ + 1, .nil, .void => true + | fuel + 1, .cons head tail, .signatureParameter expected rest => + typeAdmits frame fuel head expected && chainAdmits frame fuel tail rest + | _ + 1, _, _ => false + +end + +/-- Whether the resolved type is named by the extractor's type, with the usual nesting bound. -/ +def Frame.admits (frame : Frame) (type : ResolvedType) (expr : TypeExpr) : Bool := + typeAdmits frame (resolveFuel + 2) type expr + +/-- A method parameter can carry a root object reference when its signature spells + ELEMENT_TYPE_OBJECT in the frame's core library. -/ +def Frame.admitsParameter (frame : Frame) (type : ResolvedType) (expr : TypeExpr) : Bool := + match type, expr with + | .primitive 0x1C, .object scope name => + frame.corlib == some (bytesOf scope) && bytesOf name == systemObjectName + | _, _ => frame.admits type expr + +/-- Whether the extractor's type names the type a class instance field holds: as `admits` does, + or as an object for any class or interface, since the slot keeps a reference never read + through. -/ +def Frame.admitsHeld (frame : Frame) (type : ResolvedType) (expr : TypeExpr) : Bool := + frame.admitsParameter type expr || + match type, expr with + | .named named, .object scope name => + named.scope == bytesOf scope && named.name == bytesOf name && classLike named.kind && + (!named.fixedStorage || (named.kind.reference? == some true && + (named.name == systemStringName || named.name == systemObjectName))) + | _, _ => false + +/-- An admitted platform enum has the name and integer storage of its scoped definition. -/ +theorem typeAdmits_external_enum {env : MetadataEnv} {token : Nat} {scope name : List Nat} + {parts : List (List Nat)} {hint : Option Bool} {frame : Frame} {fuel : Nat} + {named : NamedType} {identity : String} {underlying : TypeExpr} + (resolved : env.externalNamed token scope name parts hint = .ok named) + (admitted : typeAdmits frame (fuel + 1) (.named named) (.enum identity underlying) = true) : + partsName named.parts = some named.name ∧ + Exists fun code => Membership.mem env.platform.definitions + { scope := named.scope, parts := named.parts, kind := .enumType code named.platformArity } + /\ integerElement code = true /\ + typeAdmits frame fuel (.primitive code) underlying = true := by + cases env.externalNamed_ok resolved with + | intro kind facts => + have known := facts.2.2.1 + have outside := facts.2.2.2.2 + have arity := facts.2.2.2.1 + cases kind <;> simp [typeAdmits, known, PlatformKind.resolvedKind, outside] at admitted + rename_i code count + refine And.intro admitted.2.1.1.1 (Exists.intro code (And.intro ?_ ?_)) + . simpa only [arity, PlatformKind.arity] using facts.1 + . exact And.intro admitted.2.1.2 admitted.2.2 + +#print axioms typeAdmits_external_enum + +end CIL.Extraction diff --git a/CIL/Extraction/MetadataText.lean b/CIL/Extraction/MetadataText.lean new file mode 100644 index 000000000..a1c33216d --- /dev/null +++ b/CIL/Extraction/MetadataText.lean @@ -0,0 +1,18 @@ +import Lean + +/-! +# UTF-8 metadata literals + +Checked matchers compare long assembly and type names by byte. A literal keeps its bytes in +the definition, and the source string supplies the kernel's UTF-8 proof for those exact bytes. +-/ + +namespace CIL.Extraction + +/-- A byte-backed metadata spelling definitionally equal to the source string. -/ +macro "metadata_text% " spelling:str : term => do + let bytes : Array Lean.Term := spelling.getString.toByteArray.data.map fun byte => + ⟨Lean.Syntax.mkNumLit (toString byte.toNat)⟩ + `(({ toByteArray := ⟨#[$[$bytes],*]⟩, isValidUTF8 := ($spelling).isValidUTF8 } : String)) + +end CIL.Extraction diff --git a/CIL/Extraction/MetadataToken.lean b/CIL/Extraction/MetadataToken.lean new file mode 100644 index 000000000..447f3cfef --- /dev/null +++ b/CIL/Extraction/MetadataToken.lean @@ -0,0 +1,22 @@ +/-! +# Metadata tokens + +The extraction checker and image auditor use the low three bytes of a token for the row. +-/ + +namespace CIL.Extraction + +/-- The table number of a token: its top byte. -/ +def metadataTableOf (token : Nat) : Nat := token / 0x1000000 + +/-- The row number of a token: its low three bytes. -/ +def tokenRow (token : Nat) : Nat := token % 0x1000000 + +/-- The token of a table and a row already known to fit in 24 bits. -/ +def tokenOf (table row : Nat) : Nat := table * 0x1000000 + row + +/-- A row must fit below the table byte. Each caller interprets row zero. -/ +def checkedTokenOf (table row : Nat) (overflow : Nat → Nat → ε) : Except ε Nat := + if row < 0x1000000 then .ok (tokenOf table row) else .error (overflow table row) + +end CIL.Extraction diff --git a/CIL/Extraction/ModelTable.lean b/CIL/Extraction/ModelTable.lean new file mode 100644 index 000000000..b84a33461 --- /dev/null +++ b/CIL/Extraction/ModelTable.lean @@ -0,0 +1,242 @@ +import CIL.RuntimeModel +import CIL.Extraction.CallLayouts +import CIL.Extraction.Generics +import CIL.Extraction.Lockstep +import CIL.Extraction.FlowIndex +import CIL.Extraction.SearchTreeLiteral + +/-! +# Runtime model tables + +A model table gives the runtime model a `call` or `newobj` of a typed `MethodRef` stands for, +from model entries searched by the method's name code. At the leaf the entry's name, opcode and +owner head are compared, and the entry resolves the whole reference. +`model_table%` writes a table as a literal balanced tree, so a lookup costs the depth of the tree +and the comparison of one reference. Modules that define entries import this one. +`CIL.Extraction.RuntimeModels` gathers the entries into the runtime table. +-/ + +namespace CIL.Extraction + +/-- The type constructor of `type` with its type arguments removed, so every instance of a + generic owner such as `Span` has one head. -/ +def TypeExpr.genericHead : TypeExpr → TypeExpr + | .apply head _ => head.genericHead + | type => type + +/-- The raw operand a closed overload accepts. An external member binds its name, exact target + and open declaration, including aggregate indices from the selected method. -/ +inductive ModelOperand where + | declaration (accepts : OpenSignature → Bool) + | external (declared : MethodRef → OpenSignature) (name : String) + (target : MethodRef → Option String) + +/-- Whether the raw declaration and redundant fields describe this operand. -/ +def ModelOperand.bindsSite (operand : ModelOperand) (site : CallSite) : Bool := + match operand with + | .declaration accepts => accepts site.declared + | .external declared name target => + site.declared == declared site.method && site.name == name && + (target site.method).any (fun id => site.target == .external id) && + site.ownerArgumentCount == 0 && site.typeArguments == site.method.methodArguments && + site.parameterCount == site.method.parameters.length && + site.returnsValue == (site.returnType != .void) + +/-- One overload's raw operand and runtime semantics. The closed member selects the overload + before either the raw fields or the layouts are checked. -/ +structure ModelOverload where + operand : ModelOperand + resolve : CallLayouts → MethodRef → Option RuntimeModel + accepts : MethodRef → Bool := fun _ => true + +/-- The overloads filed under one opcode, owner and name. A reserved name claims even a + closed reference for which none of its overloads resolves. -/ +structure ModelEntry where + opcode : CallOpcode + owner : TypeExpr + name : MetadataName + overloads : List ModelOverload + ownsName : Bool := false + +/-- A closed reference must identify exactly one overload for binding and execution. -/ +def ModelEntry.select? (entry : ModelEntry) (method : MethodRef) : Option ModelOverload := + select entry.overloads +where + /-- Once a match is found, a second match makes the closed reference ambiguous. -/ + select : List ModelOverload → Option ModelOverload + | [] => none + | overload :: rest => + if overload.accepts method then + if rest.any (fun other => other.accepts method) then none else some overload + else select rest + +/-- A successful selection always names a row of this entry. -/ +theorem ModelEntry.select?_mem {entry : ModelEntry} {method : MethodRef} {row : ModelOverload} + (selected : entry.select? method = some row) : row ∈ entry.overloads := by + change ModelEntry.select?.select method entry.overloads = some row at selected + generalize entry.overloads = rows at * + induction rows with + | nil => cases selected + | cons first rest ih => + simp only [ModelEntry.select?.select] at selected + split at selected + · split at selected + · contradiction + · cases selected + exact List.mem_cons_self + · exact List.mem_cons_of_mem _ (ih selected) + +#print axioms ModelEntry.select?_mem + +/-- The selected overload binds the site's declaration and raw operand. -/ +def ModelEntry.bindsSite (entry : ModelEntry) (site : CallSite) : Bool := + (entry.select? site.method).any (fun overload => overload.operand.bindsSite site) + +/-- The model of the selected overload under the checked layouts. -/ +def ModelEntry.resolve (entry : ModelEntry) (reader : CallLayouts) (method : MethodRef) : + Option RuntimeModel := + (entry.select? method).bind (fun overload => overload.resolve reader method) + +/-- A slot with a single binding needs no closed discriminator beyond its resolver. -/ +def ModelEntry.single (opcode : CallOpcode) (owner : TypeExpr) (name : MetadataName) + (operand : ModelOperand) (resolve : CallLayouts → MethodRef → Option RuntimeModel) + (ownsName := false) : ModelEntry := + { opcode, owner, name, ownsName, overloads := [{ operand, resolve }] } + +/-- A single binding passes the closed reference directly to its resolver. -/ +@[simp] theorem ModelEntry.resolve_single (opcode owner name operand resolve ownsName) + (reader : CallLayouts) (method : MethodRef) : + (ModelEntry.single opcode owner name operand resolve ownsName).resolve reader method = + resolve reader method := rfl + +#print axioms ModelEntry.resolve_single + +/-- An entry for one declaration and closed method reference under `opcode`. -/ +def ModelEntry.exact (opcode : CallOpcode) (method : MethodRef) (declared : OpenSignature) + (model : RuntimeModel) : ModelEntry := + .single opcode method.owner.genericHead method.name (.declaration (· == declared)) + (fun _ reference => if reference = method then some model else none) + +/-- The search key of an entry: the code of its method name. -/ +def ModelEntry.key (entry : ModelEntry) : Nat := entry.name.code + +/-- Whether two entries claim the same opcode, owner head and name, so one would hide the other. -/ +def ModelEntry.sameSlot (left right : ModelEntry) : Bool := + left.key == right.key && left.opcode == right.opcode && left.owner == right.owner + +/-- Model entries by name code; the entries under one code are tried in order. -/ +abbrev ModelTable := SearchTree (List ModelEntry) + +/-- The entry for the method's name, opcode and owner head. -/ +def ModelTable.entry? (table : ModelTable) (opcode : CallOpcode) (method : MethodRef) : + Option ModelEntry := do + let entries ← table.find? method.name.code + entries.find? fun entry => + entry.name == method.name && entry.opcode == opcode && + entry.owner == (if entry.ownsName && !(entry.owner matches .constructor ..) then method.owner + else method.owner.genericHead) + +/-- The model of a call under `opcode` to the closed reference `method`. -/ +def ModelTable.find (table : ModelTable) (environment : LayoutEnvironment) + (opcode : CallOpcode) (method : MethodRef) : Option RuntimeModel := do + let entry ← table.entry? opcode method + entry.resolve environment.callLayouts method + +/-- A family claims its name before overload resolution. Other entries share overlapping names + with lowerings and claim the closed overloads they resolve. -/ +def ModelTable.claims (table : ModelTable) (environment : LayoutEnvironment) + (opcode : CallOpcode) (method : MethodRef) : Bool := + (table.entry? opcode method).any fun entry => + entry.ownsName || (entry.resolve environment.callLayouts method).isSome + +/-- `groups` with `entry` added at the end of the group of its key. -/ +def ModelTable.insertGroup (entry : ModelEntry) : + List (Nat × List ModelEntry) → List (Nat × List ModelEntry) + | [] => [(entry.key, [entry])] + | (key, group) :: rest => + if entry.key < key then (entry.key, [entry]) :: (key, group) :: rest + else if entry.key = key then (key, group ++ [entry]) :: rest + else (key, group) :: insertGroup entry rest + +/-- The entries grouped by key in ascending key order, each group in list order. Structural + recursion keeps the grouping reducible, so a literal table is equal to it by `rfl`. -/ +def ModelTable.groups (entries : List ModelEntry) : List (Nat × List ModelEntry) := + entries.foldl (fun groups entry => ModelTable.insertGroup entry groups) [] + +/-- The balanced table of `entries`, which `model_table%` writes out as a literal. -/ +def ModelTable.ofEntries (entries : List ModelEntry) : ModelTable := + SearchTree.ofSorted (ModelTable.groups entries) + +/-- Whether no two entries of one group claim the same slot. -/ +def ModelTable.slotsDistinct (entries : List ModelEntry) : Bool := + (ModelTable.groups entries).all fun (_, group) => distinct group +where + /-- No entry of the list shares a slot with a later one. -/ + distinct : List ModelEntry → Bool + | [] => true + | entry :: rest => !rest.any (entry.sameSlot ·) && distinct rest + +open Lean Meta Elab Term in +/-- `model_table% entries` is `ModelTable.ofEntries entries` written out as a literal tree whose + leaves hold the entry terms, so the kernel never sorts or splits the list. Entries that share a + slot are an error. -/ +elab "model_table% " entries:term : term => do + let listType := mkApp (mkConst ``List [0]) (mkConst ``ModelEntry) + let list ← elabTermEnsuringType entries listType + synthesizeSyntheticMVarsNoPostponing + let list ← instantiateMVars list + let distinct ← unsafe evalExpr Bool (mkConst ``Bool) + (mkApp (mkConst ``ModelTable.slotsDistinct) list) + unless distinct do throwError "model table: two entries share an opcode, owner and name" + let cells ← literalCells list + let mut keys : Array Nat := #[] + for cell in cells do + let key := mkApp (mkConst ``ModelEntry.key) cell + keys := keys.push (← unsafe evalExpr Nat (mkConst ``Nat) key) + SearchTree.groupedLiteral (mkConst ``ModelEntry) cells keys + +/-- Every model `entry` resolves has `property`. -/ +def ModelEntry.Yields (entry : ModelEntry) (property : RuntimeModel → Prop) : Prop := + ∀ environment method model, entry.resolve environment method = some model → property model + +/-- An exact entry resolves only its own model. -/ +@[simp] theorem ModelEntry.exact_yields {opcode : CallOpcode} {method : MethodRef} + {declared : OpenSignature} {model : RuntimeModel} {property : RuntimeModel → Prop} : + (ModelEntry.exact opcode method declared model).Yields property ↔ property model := by + constructor + · intro yields + exact yields (LayoutEnvironment.callLayouts {}) method model (by simp [ModelEntry.exact]) + · intro holds environment reference resolved found + change (if reference = method then some model else none) = some resolved at found + split at found + · cases found + exact holds + · cases found + +#print axioms ModelEntry.exact_yields + +/-- A model a table gives has every property each of its entries' models has, so a proof about + the models of a table covers an added entry once that entry's models have the property. -/ +theorem ModelTable.find_yields {table : ModelTable} {property : RuntimeModel → Prop} + (every : ∀ group ∈ table.toList, ∀ entry ∈ group.2, entry.Yields property) + {environment : LayoutEnvironment} {opcode : CallOpcode} {method : MethodRef} + {model : RuntimeModel} (found : table.find environment opcode method = some model) : + property model := by + obtain ⟨entry, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + obtain ⟨entries, filed, member⟩ := Option.bind_eq_some_iff.mp selected + exact every _ (SearchTree.find?_mem_toList _ _ _ filed) entry + (List.mem_of_find?_eq_some member) environment.callLayouts method model resolved + +#print axioms ModelTable.find_yields + +/-- The table of runtime models checked at call sites. -/ +structure ModelCatalog where + table : ModelTable + +/-- The call opcodes a model can stand for, by raw opcode. -/ +def callOpcode? : Nat → Option CallOpcode + | 40 => some .call + | 115 => some .newobj + | _ => none + +end CIL.Extraction diff --git a/CIL/Extraction/ObjectConstruction.lean b/CIL/Extraction/ObjectConstruction.lean index 3ec0cc90a..37b1d3713 100644 --- a/CIL/Extraction/ObjectConstruction.lean +++ b/CIL/Extraction/ObjectConstruction.lean @@ -1,31 +1,33 @@ -import CIL.Extraction.Generics +import CIL.Extraction.ModelTable /-! -# The `System.Object` constructor call +# The `System.Object` constructor An admitted class derives directly from `System.Object`, so its constructor calls -`System.Object::.ctor()` on its receiver. That constructor has no effect on an instance, and the -extractor translates the call to `pop`. `objectConstructorCall` recognizes the exact call from raw -metadata alone: `CIL/Extraction/Scalars.lean` types its receiver as an object reference, and -`CIL/Extraction/UnsafeCalls.lean` requires it, and only it among calls, to emit `pop`. +`System.Object::.ctor()` on its receiver. That constructor has no effect on an instance, and its +runtime model consumes the receiver. The model entry stands for exactly that method reference +under `call`, so a constructor of another owner, scope or signature has no model. The call keeps +its call, and `callEmitsNoPop`, which `CIL/Extraction/UnsafeCalls.lean` requires, refuses `pop` +at every call site. -/ namespace CIL.Extraction -/-- Exactly `call instance void [System.Runtime]System.Object::.ctor()`. -/ -def objectConstructorCall (instruction : RawInstruction) : Bool := - match instruction.opcode, instruction.operand with - | 40, .call site => - site.name == ".ctor" && site.hasThis && !site.returnsValue && site.returnType == .void && - site.parameterCount == 0 && site.parameterTypes.isEmpty && site.typeArguments.isEmpty && - site.declaringType == .atom runtimeScope "System.Object" && - site.target == .external (runtimeScope ++ "::System.Void System.Object::.ctor()") - | _, _ => false - -/-- The `System.Object` constructor call emits `pop`, and no other `call` does. -/ -def objectConstructorMatches (instruction : RawInstruction) (emitted : Op) : Bool := - match emitted with - | .pop => instruction.opcode != 40 || objectConstructorCall instruction - | _ => !objectConstructorCall instruction +/-- `instance void [System.Runtime]System.Object::.ctor()`. -/ +def objectConstructorMethod : MethodRef := + { owner := .atom runtimeScope "System.Object", name := name!".ctor", hasThis := true, + genericArity := 0, returnType := .void, parameters := [] } + +/-- A `call` of the `System.Object` constructor only consumes its receiver. -/ +def objectConstructorEntry : ModelEntry := + .exact .call objectConstructorMethod { convention := 0x20 } .objectConstructor + +/-- The raw opcodes of `call`, `calli`, `callvirt` and `newobj`. -/ +def callFamilyOpcodes : List Nat := [40, 41, 111, 115] + +/-- No call site emits `pop`: the `System.Object` constructor call keeps its call and runs its + model, and `pop` in place of any other call would drop that call's effects. -/ +def callEmitsNoPop (instruction : RawInstruction) (emitted : Instruction) : Bool := + !(emitted matches .pop) || !callFamilyOpcodes.contains instruction.opcode end CIL.Extraction diff --git a/CIL/Extraction/ObjectOperands.lean b/CIL/Extraction/ObjectOperands.lean new file mode 100644 index 000000000..2c376fcb2 --- /dev/null +++ b/CIL/Extraction/ObjectOperands.lean @@ -0,0 +1,161 @@ +import CIL.Extraction.ValueDecoding +import CIL.Extraction.Scalars +import CIL.Extraction.CallLayouts + +/-! +# Object operand admission + +Live object instructions use the storage shape of their closed type operand. A span load or reset +requires a direct address of the same view, and a span constructor call requires the address of +its constructed type before its argument pushes. +-/ + +namespace CIL.Extraction + +/-- The storage shapes that an object opcode admits. -/ +def objectShapeAdmitted (opcode : Nat) (type : TypeExpr) (shape : ValueShape) : Bool := + objectApplicationAdmitted type && match shape with + | .span => opcode == 0x71 + | .unsupported _ => false + | _ => objectOpcode opcode + +/-- The declared type of the local or parameter whose home `instruction` addresses. -/ +private def addressedType (parameter localAt : Nat → Option TypeExpr) + (instruction : RawInstruction) : Option TypeExpr := + match argumentOperation instruction with + | some (.address index) => parameter index + | _ => if instruction.opcode ∉ [18, 65037] then none else (encodedLocal instruction).bind localAt + +/-- The type of the span home loaded or addressed immediately before a span copy. -/ +private def spanCopySource (parameter localAt : Nat → Option TypeExpr) + (previous : RawInstruction) : Option TypeExpr := do + match argumentOperation previous with + | some (.load index) => + let .byref type ← parameter index | none + return type + | _ => addressedType parameter localAt previous + +/-- Raw pushes of one value that read no memory: argument and local loads, `ldnull` and int32 + constants. -/ +private def purePush (opcode : Nat) : Bool := + opcode ∈ [2, 3, 4, 5, 14, 65033, 6, 7, 8, 9, 17, 65036, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] + +/-- A span constructor call at `pc` runs on the home its raw window names: one pure push per + parameter, at most three, after the address of a local or parameter of the constructed type, + and no branch target after that address. `recent` holds the raw instructions before the + call, nearest first. -/ +private def spanConstructorWindow (parameter localAt : Nat → Option TypeExpr) + (targeted : Nat → Bool) (pc : Nat) (recent : List RawInstruction) (site : CallSite) : Bool := + let count := site.parameterCount + let pushes := recent.take count + count ≤ 3 && pushes.length == count && pushes.all (purePush ·.opcode) && + (recent[count]?).bind (addressedType parameter localAt) == some site.declaringType && + (List.range (count + 1)).all fun offset => !targeted (pc - offset) + +/-- The live opcode and type pairs in instruction order, after checking span copies, resets and + constructor calls. -/ +def objectSites (raw : RawMethod) (reachable : List Bool) (method : Method) : + Option (List (Nat × TypeExpr)) := do + let targets := Std.TreeSet.ofList (branchTargets raw.code) compare + let parameters := FlowIndex.ofList raw.parameters + let locals := FlowIndex.ofList raw.locals + let parameter := fun index => if raw.hasThis && index == 0 then none else + parameters.get? (index - raw.hasThis.toNat) + let mut operands := [] + let mut previous := none + let mut recent := [] + let mut pc := 0 + for (current, live, _) in raw.code.zip (reachable.zip method.code) do + if live && current.opcode == 40 then + if let .call site := current.operand then + if site.name == ".ctor" && spanLocal site.declaringType then + unless spanConstructorWindow parameter locals.get? targets.contains pc recent site do + return ← none + if live && objectOpcode current.opcode then + let .type type := current.operand | return ← none + if current.opcode == 0xfe15 && spanLocal type then + unless !targets.contains pc && + previous.bind (addressedType parameter locals.get?) == some type do + return ← none + else + if current.opcode == 0x71 && spanLocal type then + let source := previous.bind (spanCopySource parameter locals.get?) + unless !targets.contains pc && source == some type do + return ← none + operands := (current.opcode, type) :: operands + previous := some current + recent := (current :: recent).take 4 + pc := pc + 1 + return operands.reverse + +/-- Collect first occurrences in reverse order, checking full types within a bucket. -/ +private def distinctOperands (seen : Std.TreeMap (List (List UInt8)) (List TypeExpr) compare) : + List (Nat × TypeExpr) → List (Nat × TypeExpr) → List (Nat × TypeExpr) + | kept, [] => kept + | kept, (opcode, type) :: rest => + let key := keyBytes (toString opcode) :: typeKeyInto type [] + let bucket := seen.getD key [] + if bucket.contains type then distinctOperands seen kept rest + else distinctOperands (seen.insert key (type :: bucket)) ((opcode, type) :: kept) rest + +/-- Every selected operand was already collected or occurs among the remaining live sites. -/ +private theorem distinctOperands_mem (seen kept sites) {pair : Nat × TypeExpr} + (member : pair ∈ distinctOperands seen kept sites) : pair ∈ kept ∨ pair ∈ sites := by + induction sites generalizing seen kept with + | nil => exact Or.inl member + | cons site rest ih => + obtain ⟨opcode, type⟩ := site + simp only [distinctOperands] at member + split at member + · exact (ih _ _ member).imp_right (List.mem_cons_of_mem _) + · rcases ih _ _ member with earlier | later + · rcases List.mem_cons.mp earlier with rfl | kept + · exact Or.inr (.head _) + · exact Or.inl kept + · exact Or.inr (.tail _ later) +#print axioms distinctOperands_mem + +/-- The distinct live pairs in reverse order of first occurrence. -/ +def objectOperands (raw : RawMethod) (reachable : List Bool) (method : Method) : + Option (List (Nat × TypeExpr)) := + (objectSites raw reachable method).map (distinctOperands ∅ []) + +/-- The method admits each distinct live object operand and every span copy source. -/ +def ObjectOperandsValid (raw : RawMethod) (reachable : List Bool) (method : Method) : Prop := + let indexed := method.environment.callLayouts + method.profile.Valid ∧ raw.code.length = reachable.length ∧ + raw.code.length = method.code.length ∧ + (objectOperands raw reachable method).any (fun operands => + operands.all fun (opcode, type) => + objectShapeAdmitted opcode type (indexed.valueShape type)) = true + +/-- A live object instruction and the checked shape of its named type operand. -/ +structure ObjectSiteShape (method : Method) where + opcode : Nat + type : TypeExpr + shape : ValueShape + resolved : method.valueShape type = shape + +/-- Shape facts for the live instruction sequence suffice for all its distinct operands. -/ +theorem ObjectOperandsValid.of_sites {raw : RawMethod} {reachable : List Bool} {method : Method} + (operands : List (ObjectSiteShape method)) + (sites : objectSites raw reachable method = some (operands.map fun s => (s.opcode, s.type))) + (lengths : method.profile.Valid ∧ raw.code.length = reachable.length ∧ + raw.code.length = method.code.length) + (shapes : operands.all (fun s => objectShapeAdmitted s.opcode s.type s.shape) = true) : + ObjectOperandsValid raw reachable method := by + refine ⟨lengths.1, lengths.2.1, lengths.2.2, ?_⟩ + simp only [objectOperands, sites, Option.map_some, Option.any_some, List.all_eq_true] + intro pair member + have live := (distinctOperands_mem _ _ _ member).resolve_left List.not_mem_nil + obtain ⟨site, atSite, rfl⟩ := List.mem_map.mp live + rw [CallLayouts.valueShape_eq] + simpa only [← site.resolved, Method.valueShape] using List.all_eq_true.mp shapes site atSite +#print axioms ObjectOperandsValid.of_sites + +instance (raw : RawMethod) (reachable : List Bool) (method : Method) : + Decidable (ObjectOperandsValid raw reachable method) := + inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _)) + +end CIL.Extraction diff --git a/CIL/Extraction/OpaqueLocals.lean b/CIL/Extraction/OpaqueLocals.lean new file mode 100644 index 000000000..7f768196d --- /dev/null +++ b/CIL/Extraction/OpaqueLocals.lean @@ -0,0 +1,106 @@ +import CIL.Extraction.Features +import CIL.Extraction.RuntimeModels +import Std.Data.TreeSet.Lemmas +import Lean.Elab.Tactic.BuiltinTactic + +/-! Whole-vector local storage preserves the unspecified lanes of scalar-unsafe vectors. -/ + +namespace CIL.Extraction + +/-- Whether a raw local is the whole-vector type eligible for opaque storage. -/ +private def opaqueLocalType : Option TypeExpr → Bool + | some (.apply (.constructor scope "System.Runtime.Intrinsics.Vector128`1") + (.atom elementScope "System.UInt64")) => + scope == intrinsicsScope && elementScope == runtimeScope + | _ => false + +/-- Every eligible local, in index order; addresses are taken by `ldloca` (18 or 0xFE0D). -/ +def opaqueLocalIndices (closed : RawMethod) : List Nat := + let addressed := Std.TreeSet.ofList (closed.code.filterMap fun instruction => + if instruction.opcode == 18 || instruction.opcode == 65037 then localIndex instruction else none) + ((closed.locals.zipIdx).filter fun (type, index) => + opaqueLocalType (some type) && !addressed.contains index).map Prod.snd + +/-- Indexed opaque-local discovery agrees with direct local lookup and address membership. -/ +theorem opaqueLocalIndices_eq_scan (closed : RawMethod) : + opaqueLocalIndices closed = (List.range closed.locals.length).filter (fun index => + (match (closed.locals[index]? : Option TypeExpr) with + | some (.apply (.constructor scope "System.Runtime.Intrinsics.Vector128`1") + (.atom elementScope "System.UInt64")) => + scope == intrinsicsScope && elementScope == runtimeScope + | _ => false) && !(closed.code.filterMap (fun instruction => + if instruction.opcode == 18 || instruction.opcode == 65037 then localIndex instruction + else none)).contains index) := by + simp only [opaqueLocalIndices, Std.TreeSet.contains_ofList] + rw [List.range_eq_range', ← List.zipIdx_map_snd 0 closed.locals, List.filter_map] + congr 1 + apply List.filter_congr + intro entry present + have found := List.mem_zipIdx_iff_getElem?.mp present + simp only [Function.comp_def, found] + rfl + +#print axioms opaqueLocalIndices_eq_scan + +/-- A live direct call to the exact scalar-unsafe creator needs whole-vector local storage. -/ +private def hasRawScalarUnsafe : List RawInstruction → List Bool → Bool + | raw :: rest, live :: mask => + (live && raw.opcode == 40 && (match raw.operand with + | .call site => site.method == createScalarUnsafeMethod && + site.declared == createScalarUnsafeMethod.signature 0 + | _ => false)) || hasRawScalarUnsafe rest mask + | _, _ => false + +/-- The closed body and its live mask determine the opaque vector slots in its frame. -/ +def rawOpaqueLocals (closed : RawMethod) (reachable : List Bool) : List Nat := + if closed.locals.any (fun type => opaqueLocalType (some type)) && + hasRawScalarUnsafe closed.code reachable then opaqueLocalIndices closed else [] + +/-- Every claimed opaque local has the emitted whole-vector slot kind. -/ +private def opaqueKindsValid (method : Method) : Bool := + let kinds := FlowIndex.ofList method.localKinds + method.opaqueVectorLocals.all fun index => kinds.get? index == some .vector128 + +/-- No emitted address operation exposes an opaque local's unspecified bytes. -/ +private def opaqueAddressesValid (method : Method) : Bool := + let slots := Std.TreeSet.ofList method.opaqueVectorLocals + method.code.all fun operation => match operation with + | .ldloca index => !slots.contains index + | _ => true +/-- Whether `operation` calls a method whose runtime model in `environment` is + `CreateScalarUnsafe`. -/ +def createsScalarUnsafe (environment : LayoutEnvironment) : Instruction → Bool + | .call method _ => + runtimeModel environment .call method == some (.intrinsic (.vector .createScalarUnsafe64) 1) + | _ => false + +/-- A live scalar-unsafe creation requires opaque storage for each eligible vector local. -/ +def expectedOpaqueLocals (environment : LayoutEnvironment) (closed : RawMethod) + (code : List Instruction) : List Nat := + if code.any (createsScalarUnsafe environment) then opaqueLocalIndices closed else [] + +/-- The opaque locals are exactly the eligible ones when the method calls + `CreateScalarUnsafe` and none otherwise; each is a 128-bit vector slot whose address the + emitted code never takes. -/ +def OpaqueLocalsValid (closed : RawMethod) (method : Method) : Prop := + method.opaqueVectorLocals = expectedOpaqueLocals method.environment closed method.code ∧ + opaqueKindsValid method = true ∧ opaqueAddressesValid method = true + +/-- Indexed opaque-local checks preserve the original kind and no-address obligations. -/ +theorem OpaqueLocalsValid_iff_scan (closed : RawMethod) (method : Method) : + OpaqueLocalsValid closed method ↔ + method.opaqueVectorLocals = (if method.code.any (createsScalarUnsafe method.environment) + then opaqueLocalIndices closed else []) ∧ + method.opaqueVectorLocals.all (fun index => method.localKinds[index]? == some .vector128) = + true ∧ + method.code.all (fun operation => match operation with + | .ldloca index => !(index ∈ method.opaqueVectorLocals) + | _ => true) = true := by + simp only [OpaqueLocalsValid, expectedOpaqueLocals, opaqueKindsValid, opaqueAddressesValid, + FlowIndex.get?_ofList, Std.TreeSet.contains_ofList, List.contains_eq_mem] + +#print axioms OpaqueLocalsValid_iff_scan +instance (closed : RawMethod) (method : Method) : Decidable (OpaqueLocalsValid closed method) := + inferInstanceAs (Decidable (_ ∧ _ ∧ _)) + +end CIL.Extraction diff --git a/CIL/Extraction/Parameters.lean b/CIL/Extraction/Parameters.lean index 9ef780d5a..3c595f198 100644 --- a/CIL/Extraction/Parameters.lean +++ b/CIL/Extraction/Parameters.lean @@ -67,8 +67,8 @@ def storageKind (layouts : Array ComputedLayout) : StorageType → Option LocalK | .vector128 => some .vector128 | .vector256 => some .vector256 | .vector512 => some .vector512 - | .field .vector128 => some .vector128 - | .field .vector256 => some .vector256 + | .field (.vector128 _) => some .vector128 + | .field (.vector256 _) => some .vector256 | .field .signed8 => some (.narrow .signed8) | .field .unsigned8 | .field .boolean => some (.narrow .unsigned8) | .field .signed16 => some (.narrow .signed16) @@ -142,8 +142,9 @@ theorem expectedHomes_eq_scan (layouts : Array ComputedLayout) (raw : RawArgumen simp only [expectedHomes, FlowIndex.get?_ofList] #print axioms expectedHomes_eq_scan -/-- The narrowing a load of a small-integer argument applies. -/ -private def argumentNarrow : StorageType → Option NarrowKind +/-- The narrowing a value held in this storage takes: a small-integer field's own width and + signedness, and `none` for every other storage. -/ +def StorageType.narrow : StorageType → Option NarrowKind | .field .signed8 => some .signed8 | .field .unsigned8 | .field .boolean => some .unsigned8 | .field .signed16 => some .signed16 @@ -174,50 +175,47 @@ theorem argumentHomeIndex_get (homes : List Nat) (offset index : Nat) : #print axioms argumentHomeIndex_get -/-- Check one argument instruction against a precomputed home set; hidden accesses - to appended home slots are rejected. -/ +/-- Check one argument instruction against a precomputed home set. An argument instruction + names its own argument, and a store or address needs a home; a local instruction cannot + reach the home slots appended after the locals. -/ def validateArgumentOpAt (localCount : Nat) (argumentAt : Nat → Option StorageType) (homeAt : Nat → Option Nat) - (operation : ArgumentOp) (live : Bool) (emitted : Op) : Bool := - if !live then match emitted with | .unsupported _ => true | _ => false + (operation : ArgumentOp) (live : Bool) (emitted : Instruction) : Bool := + if !live then match emitted with | .unadmitted _ => true | _ => false else match operation with | .load index => (do - let type ← argumentAt index - match homeAt index, emitted with - | some slot, .local actual narrow => return actual == slot && narrow.isNone - | none, .arg actual narrow => return actual == index && narrow == argumentNarrow type - | _, _ => return false).getD false + let _ ← argumentAt index + match emitted with + | .ldarg actual => return actual == index + | _ => return false).getD false | .store index => (do let _ ← argumentAt index - let slot ← homeAt index + let _ ← homeAt index match emitted with - | .setLocal actual narrow => return actual == slot && narrow.isNone + | .starg actual => return actual == index | _ => return false).getD false | .address index => (do let type ← argumentAt index let rootOnly := match type with | .array _ | .object => true | _ => false if type == StorageType.reference || rootOnly then return false - let slot ← homeAt index + let _ ← homeAt index match emitted with - | .localAddr actual => return actual == slot - | .local actual none => return actual == slot && match type with - | .span .. | .aggregateSpan .. => true | _ => false + | .ldarga actual => return actual == index | _ => return false).getD false | .other => match emitted with - | .arg .. | .vectorArg .. | .vectorArgAddr .. => false - | .local index _ | .setLocal index _ | .localAddr index | .initializeSpanLocal index _ - | .vectorLocal index | .vectorLocalAddr index | .setVectorLocal index => + | .ldarg _ | .starg _ | .ldarga _ => false + | .ldloc index | .stloc index | .ldloca index => index < localCount | _ => true /-- The per-instruction rule against the original argument and home lists. -/ def validateArgumentOpIn (raw : RawArguments) (homes : List Nat) : - ArgumentOp → Bool → Op → Bool := + ArgumentOp → Bool → Instruction → Bool := validateArgumentOpAt raw.locals.length (raw.arguments[·]?) (homeSlot raw homes) /-- Check the complete instruction stream with shared argument-type and home-slot indexes. -/ def argumentInstructionsValid (raw : RawArguments) (homes : List Nat) - (reachable : List Bool) (code : List Op) : Bool := + (reachable : List Bool) (code : List Instruction) : Bool := let arguments := FlowIndex.ofList raw.arguments let slots := argumentHomeIndex homes raw.locals.length all3 (validateArgumentOpAt raw.locals.length arguments.get? (slots[·]?)) @@ -225,7 +223,7 @@ def argumentInstructionsValid (raw : RawArguments) (homes : List Nat) /-- Indexed access validation preserves every original instruction check and length failure. -/ theorem argumentInstructionsValid_eq_scan (raw : RawArguments) (homes : List Nat) - (reachable : List Bool) (code : List Op) : + (reachable : List Bool) (code : List Instruction) : argumentInstructionsValid raw homes reachable code = all3 (validateArgumentOpIn raw homes) raw.code reachable code := by have arguments : (FlowIndex.ofList raw.arguments).get? = (raw.arguments[·]?) := by @@ -240,7 +238,7 @@ theorem argumentInstructionsValid_eq_scan (raw : RawArguments) (homes : List Nat /-- `validateArgumentOpIn` with the home set derived from the body. -/ def validateArgumentOp (raw : RawArguments) (reachable : List Bool) : - ArgumentOp → Bool → Op → Bool := + ArgumentOp → Bool → Instruction → Bool := validateArgumentOpIn raw (requiredHomeIndices raw reachable) /-- A complete mapping check, not a list of optional argument-use claims. -/ @@ -277,7 +275,7 @@ theorem validated_parameter_homes {layouts : Array ComputedLayout} {raw : RawArg theorem validated_argument_access {layouts : Array ComputedLayout} {raw : RawArguments} {reachable : List Bool} {method : Method} (valid : ParameterHomesValid layouts raw reachable method) - (pc : Fin raw.code.length) (operation : ArgumentOp) (live : Bool) (emitted : Op) + (pc : Fin raw.code.length) (operation : ArgumentOp) (live : Bool) (emitted : Instruction) (rawAt : raw.code[pc.val]? = some operation) (reachableAt : reachable[pc.val]? = some live) (emittedAt : method.code[pc.val]? = some emitted) : validateArgumentOp raw reachable operation live emitted = true := by @@ -285,12 +283,16 @@ theorem validated_argument_access {layouts : Array ComputedLayout} {raw : RawArg rw [argumentInstructionsValid_eq_scan] at checked exact all3_at checked rawAt reachableAt emittedAt -/-- Once a parameter has a home, loading its original argument slot is rejected. -/ -theorem home_load_rejects_original_arg (raw : RawArguments) (reachable : List Bool) - (index actual : Nat) (narrow : Option NarrowKind) - (home : index ∈ requiredHomeIndices raw reachable) : - validateArgumentOp raw reachable (.load index) true (.arg actual narrow) = false := by - cases found : raw.arguments[index]? <;> - simp [validateArgumentOp, validateArgumentOpIn, validateArgumentOpAt, homeSlot, home, found] +/-- A local instruction the check accepts outside an argument access names a declared local, + so it never reaches the home slots appended after the locals. -/ +theorem ldloc_rejects_home_slot (raw : RawArguments) (reachable : List Bool) + (op : Instruction) (index : Nat) + (checked : validateArgumentOp raw reachable .other true op = true) + (named : op = .ldloc index ∨ op = .stloc index ∨ op = .ldloca index) : + index < raw.locals.length := by + rcases named with rfl | rfl | rfl <;> + simpa [validateArgumentOp, validateArgumentOpIn, validateArgumentOpAt] using checked + +#print axioms ldloc_rejects_home_slot end CIL.Extraction diff --git a/CIL/Extraction/PinnedIndex.lean b/CIL/Extraction/PinnedIndex.lean new file mode 100644 index 000000000..c32da17d1 --- /dev/null +++ b/CIL/Extraction/PinnedIndex.lean @@ -0,0 +1,50 @@ +import CIL.Extraction.MetadataResolution +import Lean + +/-! Materialized package pin indexes whose complete identity binding the kernel checks. -/ + +namespace CIL.Extraction + +open Lean Meta Elab Term + +/-- Quote the shape, keys and values of a closed map so kernel lookups traverse its branches. -/ +private def quoteMap {Key Value : Type} [Ord Key] (keyType valueType : Expr) + (keyExpr : Key -> Expr) (valueExpr : Value -> MetaM Expr) + (entries : Std.TreeMap.Raw Key Value) : MetaM Expr := do + let family := mkLambda `key .default keyType valueType + let rec nodes : Std.DTreeMap.Internal.Impl Key (fun _ => Value) -> MetaM Expr + | .leaf => pure (mkApp2 (mkConst ``Std.DTreeMap.Internal.Impl.leaf [.zero, .zero]) + keyType family) + | .inner size key value left right => do + return mkAppN (mkConst ``Std.DTreeMap.Internal.Impl.inner [.zero, .zero]) + #[keyType, family, toExpr size, keyExpr key, <- valueExpr value, <- nodes left, + <- nodes right] + let order <- synthInstance (mkApp (mkConst ``Ord [.zero]) keyType) + let comparator := mkApp2 (mkConst ``compare [.zero]) keyType order + let tree := mkAppN (mkConst ``Std.DTreeMap.Raw.mk [.zero, .zero]) + #[keyType, family, comparator, <- nodes entries.inner.inner] + return mkAppN (mkConst ``Std.TreeMap.Raw.mk [.zero, .zero]) + #[keyType, valueType, comparator, tree] + +/-- Quote every prefix node with its literal map of byte children. -/ +private partial def quotePrefixes : AssemblyPrefixes -> MetaM Expr + | .node terminal children => do + let map <- quoteMap (mkConst ``Nat) (mkConst ``AssemblyPrefixes) toExpr quotePrefixes children + return mkApp2 (mkConst ``AssemblyPrefixes.node) (toExpr terminal) map + +/-- Materialize a closed package pin index; its constructor checks the complete derivation. -/ +elab "cil_pin_index " identities:term : term => do + let listType := mkApp (mkConst ``List [.zero]) + (mkApp (mkConst ``List [.zero]) (mkConst ``Nat)) + let expression ← elabTermEnsuringType identities listType + synthesizeSyntheticMVarsNoPostponing + let expression ← instantiateMVars expression + let values ← unsafe evalExpr (List (List Nat)) listType expression + let prefixes ← quotePrefixes ((values.map assemblyStem).foldl AssemblyPrefixes.insert {}) + let proof ← mkEqRefl prefixes + let bytes := mkApp (mkConst ``List [.zero]) (mkConst ``Nat) + let exact <- quoteMap bytes (mkConst ``Unit) toExpr (fun _ => pure (mkConst ``Unit.unit)) + (indexPins values) + return mkAppN (mkConst ``PinnedIndex.mk) #[expression, prefixes, proof, exact, <- mkEqRefl exact] + +end CIL.Extraction diff --git a/CIL/Extraction/PlatformDefinitions.lean b/CIL/Extraction/PlatformDefinitions.lean new file mode 100644 index 000000000..7a8d0afa9 --- /dev/null +++ b/CIL/Extraction/PlatformDefinitions.lean @@ -0,0 +1,271 @@ +import CIL.Extraction.SearchTreeLiteral +import Std.Data.HashSet + +/-! +# Platform definitions + +The carried slices hold the rows of the pinned assemblies only; a type they name but do not +define belongs to a platform assembly. What the metadata checks rely on about such a type is read +from a `PlatformTable`, the list of platform definitions the extraction states: the type's scope, +name parts and kind. Each check that relies on a type outside the carried slices looks its entry +up with `PlatformTable.require` and refuses the type when the entry is missing or states another +kind. The extractor supplies each kind, including enum storage. The kernel checks the lookup +and table equality across bodies. Agreement with the platform image bytes remains trusted. + +`platform_table%` emits a balanced tree. When several scopes use identical name parts, the +search key includes the scope. Every lookup compares the exact scope and name parts. +-/ + +namespace CIL.Extraction + +/-- What the metadata checks rely on about a type outside the carried slices. -/ +inductive PlatformKind where + /-- A class the checks rely on for nothing past being a reference type. -/ + | classType + | interfaceType + | genericClass (arity : Nat) + | genericInterface (arity : Nat) + | genericValue (arity : Nat) + /-- A value type that is not an enum. -/ + | valueType + /-- An enum whose one instance field has the integer element type `underlying`. -/ + | enumType (underlying : Nat) (parameters : Nat := 0) + /-- `System.Object`, which ends every class chain and declares no instance field. -/ + | objectRoot + /-- `System.ValueType`: a type that extends it is a value type. -/ + | valueTypeRoot + /-- `System.Enum`: a type that extends it is an enum. -/ + | enumRoot + /-- The value pair whose instance fields `Item1` and `Item2` are its first and its second. -/ + | valuePair + /-- The class whose static `IsLittleEndian` field is the profile's byte order. -/ + | byteOrder + deriving DecidableEq, Repr + +/-- Whether a type of this kind is a value type. -/ +def PlatformKind.isValue : PlatformKind → Bool + | .valueType | .genericValue _ | .enumType _ _ | .valuePair => true + | _ => false + +/-- The number of generic parameters declared by the platform definition. -/ +def PlatformKind.arity : PlatformKind -> Nat + | .genericClass count | .genericInterface count | .genericValue count => count + | .enumType _ count => count + | .valuePair => 2 + | _ => 0 + +/-- Whether a signature's value-or-class byte, when it has one, agrees with the kind. -/ +def PlatformKind.fitsHint (hint : Option Bool) (kind : PlatformKind) : Bool := + hint.all (· == kind.isValue) + +/-- One platform type: the identity of the assembly a reference scopes it to, the namespace + column and simple name of each enclosing type and of the type itself, as bytes, and its + kind. -/ +structure PlatformDefinition where + scope : List Nat + parts : List (List Nat) + kind : PlatformKind + deriving DecidableEq, Repr + +/-- A digit past every byte, which ends each name part in a key. -/ +def partEnd : Nat := 256 + +/-- The base-512 encoding of a sequence of byte strings with a distinct end digit. -/ +def platformKey (parts : List (List Nat)) : Nat := + parts.foldl (init := 1) fun code part => + part.foldl (fun code byte => code * 512 + byte) code * 512 + partEnd + +/-- A balanced index of scoped definitions. Shared names require scope bytes in the key. -/ +structure PlatformTable where + includesScope : Bool + entries : SearchTree (List PlatformDefinition) + deriving DecidableEq, Repr + +/-- The last group at or below the key, or the inherited candidate when the subtree is empty. + Exact identity comparison discards the candidate when the requested key is absent. -/ +private def scopedCandidates (tree : SearchTree (List PlatformDefinition)) (key : Nat) + (previous : List PlatformDefinition) : List PlatformDefinition := + match tree with + | .leaf => previous + | .node left limit group right => + if Nat.blt key limit then scopedCandidates left key previous + else scopedCandidates right key group + +/-- Each candidate comes from the inherited group or a group in the subtree. -/ +private theorem scopedCandidates_mem (tree : SearchTree (List PlatformDefinition)) (key : Nat) + (previous : List PlatformDefinition) (entry : PlatformDefinition) + (member : entry ∈ scopedCandidates tree key previous) : + entry ∈ previous ∨ ∃ pair ∈ tree.toList, entry ∈ pair.2 := by + induction tree generalizing previous with + | leaf => exact .inl member + | node left limit group right leftIH rightIH => + simp only [scopedCandidates] at member + split at member + · rcases leftIH previous member with inherited | ⟨pair, inside, found⟩ + · exact .inl inherited + · exact .inr ⟨pair, List.mem_append_left _ inside, found⟩ + · rcases rightIH group member with localMember | ⟨pair, inside, found⟩ + · exact .inr ⟨(limit, group), by simp [SearchTree.toList], localMember⟩ + · exact .inr ⟨pair, List.mem_append_right _ (List.mem_cons_of_mem _ inside), found⟩ +#print axioms scopedCandidates_mem + +/-- Candidate definitions for the reference. Shared names use a single comparison per node. -/ +private def PlatformTable.group? (table : PlatformTable) (scope : List Nat) + (parts : List (List Nat)) : Option (List PlatformDefinition) := + if table.includesScope then + some (scopedCandidates table.entries (platformKey (scope :: parts)) []) + else table.entries.find? (platformKey parts) + +/-- Every definition of the table, group by group. -/ +def PlatformTable.definitions (table : PlatformTable) : List PlatformDefinition := + table.entries.toList.flatMap (·.2) + +/-- The kind the table states for the type of this scope and these name parts. -/ +def PlatformTable.kindOf (table : PlatformTable) (scope : List Nat) + (parts : List (List Nat)) : Option PlatformKind := do + let group ← table.group? scope parts + let entry ← group.find? fun entry => entry.scope == scope && entry.parts == parts + pure entry.kind + +/-- A kind the table states is a definition of the table for exactly that scope and those + parts, whatever order the tree holds its groups in. -/ +theorem PlatformTable.mem_of_kindOf {table : PlatformTable} {scope : List Nat} + {parts : List (List Nat)} {kind : PlatformKind} + (found : table.kindOf scope parts = some kind) : + { scope, parts, kind } ∈ table.definitions := by + simp only [PlatformTable.kindOf, bind, Option.bind] at found + split at found + · cases found + · rename_i group filed + dsimp only at found + split at found + · cases found + · rename_i entry selected + cases found + have member := List.mem_of_find?_eq_some selected + have same := List.find?_some selected + simp only [Bool.and_eq_true, beq_iff_eq] at same + obtain ⟨sameScope, sameParts⟩ := same + have included : entry ∈ table.definitions := by + simp only [PlatformTable.definitions, List.mem_flatMap] + unfold PlatformTable.group? at filed + split at filed + · cases filed + exact (scopedCandidates_mem _ _ [] entry member).resolve_left (by simp) + · exact ⟨(_, group), SearchTree.find?_mem_toList _ _ _ filed, member⟩ + cases entry + cases sameScope + cases sameParts + exact included + +#print axioms PlatformTable.mem_of_kindOf + +/-- The kind of the type of this scope and these parts, which `fits` must accept: a missing + entry is `missingPlatformDefinition` and another kind `platformKindMismatch`, both for + `token`. -/ +def PlatformTable.require (table : PlatformTable) (token : Nat) (scope : List Nat) + (parts : List (List Nat)) (fits : PlatformKind → Bool) : + Except MetadataError PlatformKind := + match table.kindOf scope parts with + | none => .error (.missingPlatformDefinition token) + | some kind => if fits kind then .ok kind else .error (.platformKindMismatch token) + +/-- A required kind is the kind of a definition of the table for that scope and those parts, + and `fits` accepts it. -/ +theorem PlatformTable.require_ok {table : PlatformTable} {token : Nat} {scope : List Nat} + {parts : List (List Nat)} {fits : PlatformKind → Bool} {kind : PlatformKind} + (required : table.require token scope parts fits = .ok kind) : + { scope, parts, kind } ∈ table.definitions ∧ fits kind = true := by + unfold PlatformTable.require at required + split at required + · cases required + · rename_i stated found + split at required + · cases required + exact ⟨PlatformTable.mem_of_kindOf found, by assumption⟩ + · cases required + +#print axioms PlatformTable.require_ok + +/-- The table with no definitions, under which every reliance on a platform type fails. -/ +instance : EmptyCollection PlatformTable := ⟨⟨false, .leaf⟩⟩ + +open Lean Meta in +/-- The number a code term spells when it is written as an extraction writes one: a numeral, or + codes joined by `Nat.lor` and `Nat.shiftLeft`; `none` for any other term. -/ +private partial def literalCode? (term : Expr) : MetaM (Option Nat) := do + match_expr term with + | Nat.lor high low => + match ← literalCode? high, ← literalCode? low with + | some high, some low => return some (high ||| low) + | _, _ => return none + | Nat.shiftLeft code shift => + match ← literalCode? code, ← getNatValue? shift with + | some code, some shift => return some (code <<< shift) + | _, _ => return none + | _ => getNatValue? term + +open Lean Meta in +/-- The bytes a byte-list term spells when it is written as an extraction writes one: `[]`, a + list of numerals or `fixedBytes count code`; `none` for any other term. -/ +private partial def literalBytes? (term : Expr) : MetaM (Option (List Nat)) := do + match_expr term with + | fixedBytes count code => + match ← getNatValue? count, ← literalCode? code with + | some count, some code => return some (fixedBytes count code) + | _, _ => return none + | List.nil _ => return some [] + | List.cons _ head tail => + match ← getNatValue? head, ← literalBytes? tail with + | some head, some tail => return some (head :: tail) + | _, _ => return none + | _ => return none + +open Lean Meta in +/-- The byte lists of a list term of byte-list terms, each read by `literalBytes?`. -/ +private partial def literalParts? (term : Expr) : MetaM (Option (List (List Nat))) := do + match_expr term with + | List.nil _ => return some [] + | List.cons _ head tail => + match ← literalBytes? head, ← literalParts? tail with + | some head, some tail => return some (head :: tail) + | _, _ => return none + | _ => return none + +open Lean Meta in +/-- The scope and name parts of a definition term: read from its literals when an extraction + wrote them, otherwise evaluated, which compiles the term. -/ +private def definitionType (cell : Expr) : MetaM (List Nat × List (List Nat)) := do + if let some (scope, parts, _) := cell.app3? ``PlatformDefinition.mk then + if let (some scope, some parts) := (← literalBytes? scope, ← literalParts? parts) then + return (scope, parts) + let definition ← unsafe evalExpr PlatformDefinition (Lean.mkConst ``PlatformDefinition) cell + return (definition.scope, definition.parts) + +open Lean Meta Elab Term in +/-- `platform_table% [definitions]` is the list written out as a literal search tree whose leaves + hold the definition terms. Each term is elaborated on its own and its scope and name parts are + read from its literals, so a long list costs no evaluation and no recursion per definition. Two + definitions of one scope and name parts are an error, since a lookup would read only the + first. -/ +elab "platform_table% " "[" definitions:term,* "]" : term => do + let entryType := Lean.mkConst ``PlatformDefinition + let mut elaborated := #[] + for definition in definitions.getElems do + elaborated := elaborated.push (← elabTermEnsuringType definition entryType) + synthesizeSyntheticMVarsNoPostponing + let cells ← elaborated.mapM instantiateMVars + let mut named : Std.HashSet (List Nat × List (List Nat)) := {} + let mut names : Std.HashSet (List (List Nat)) := {} + let types ← cells.mapM fun cell => definitionType cell + for type in types do + if named.contains type then throwError "platform table: two definitions name one type" + named := named.insert type + names := names.insert type.2 + let includesScope := names.size != types.size + let keys := types.map fun (scope, parts) => + platformKey (if includesScope then scope :: parts else parts) + return mkApp2 (mkConst ``PlatformTable.mk) (toExpr includesScope) + (← SearchTree.groupedLiteral entryType cells keys) + +end CIL.Extraction diff --git a/CIL/Extraction/PlatformModels.lean b/CIL/Extraction/PlatformModels.lean new file mode 100644 index 000000000..ccf5e4424 --- /dev/null +++ b/CIL/Extraction/PlatformModels.lean @@ -0,0 +1,100 @@ +import CIL.Extraction.ModelTable + +/-! Runtime models for hardware feature getters and the 64-bit platform constants. -/ + +namespace CIL.Extraction + +/-- A parameterless getter reads a feature of the profile or a fixed platform word. -/ +private inductive Getter where + | supported (owner : String) (feature : Feature) + | accelerated (owner : String) (feature : Feature) + | size (owner : String) + | processWidth + +/-- The platform declaration, raw member and exact external identity agree on one getter. -/ +private def getterEntry (scope owner : String) (key : MetadataName) (name result : String) + (model : RuntimeModel) : ModelEntry := + let resultType := TypeExpr.atom runtimeScope result + let declared : OpenSignature := { convention := 0, returnType := resultType } + let target := s!"{scope}::{result} {owner}::{name}()" + let accepts (method : MethodRef) := !method.hasThis && method.genericArity == 0 && + method.methodArguments.isEmpty && method.parameters.isEmpty && method.returnType == resultType + let row : ModelOverload := + { operand := .external (fun _ => declared) name (fun _ => some target) + accepts + resolve := fun _ _ => some model } + { opcode := .call, owner := .atom scope owner, name := key, ownsName := true, overloads := [row] } + +/-- The exact member that returns the profile value or target width. -/ +private def Getter.entry : Getter → ModelEntry + | .supported owner feature => + getterEntry intrinsicsScope ("System.Runtime.Intrinsics." ++ owner) name!"get_IsSupported" + "get_IsSupported" "System.Boolean" (.feature feature) + | .accelerated owner feature => + getterEntry intrinsicsScope ("System.Runtime.Intrinsics." ++ owner) + name!"get_IsHardwareAccelerated" "get_IsHardwareAccelerated" "System.Boolean" + (.feature feature) + | .size owner => + getterEntry runtimeScope owner name!"get_Size" "get_Size" "System.Int32" (.constant 8) + | .processWidth => + getterEntry runtimeScope "System.Environment" name!"get_Is64BitProcess" "get_Is64BitProcess" + "System.Boolean" (.constant 1) + +/-- Each nested hardware owner names its own external target even when two return one flag. -/ +private def getters : List Getter := [ + .supported "Arm.AdvSimd" .advSimd, + .supported "Arm.AdvSimd/Arm64" .advSimd, + .supported "Arm.ArmBase/Arm64" .armBase64, + .supported "X86.Sse2" .sse2, + .supported "X86.Ssse3" .ssse3, + .supported "X86.Sse41" .sse41, + .supported "X86.Sse42" .sse42, + .supported "X86.Avx" .avx, + .supported "X86.Avx2" .avx2, + .supported "X86.Avx512F" .avx512F, + .supported "X86.Avx512F/VL" .avx512FVL, + .supported "X86.Avx512DQ" .avx512DQ, + .supported "X86.Avx512DQ/VL" .avx512DQVL, + .supported "X86.Avx512Vbmi" .avx512Vbmi, + .supported "X86.Avx512Vbmi/VL" .avx512Vbmi, + .supported "X86.Aes" .aes, + .supported "X86.Aes/X64" .aes, + .supported "Arm.Aes" .armAes, + .supported "Arm.Aes/Arm64" .armAes, + .supported "X86.Bmi1" .bmi1, + .supported "X86.Bmi2/X64" .bmi2, + .supported "X86.X86Base/X64" .x86Base64, + .accelerated "Vector128" .vector128Accelerated, + .accelerated "Vector256" .vector256Accelerated, + .size "System.IntPtr", .size "System.UIntPtr", .processWidth] + +/-- All hardware and platform getter slots of the runtime catalog. -/ +def platformEntries := getters.map Getter.entry + +/-- A getter row can only yield its specified model. -/ +private theorem getterEntry_yields {scope owner key name result model} + {property : RuntimeModel → Prop} (holds : property model) : + (getterEntry scope owner key name result model).Yields property := by + intro reader method actual found + obtain ⟨row, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + simp only [getterEntry, ModelEntry.select?, ModelEntry.select?.select] at selected + split at selected + · cases selected + cases resolved + exact holds + · cases selected + +#print axioms getterEntry_yields + +/-- Getter entries return only feature or constant models. -/ +theorem platformEntries_yields {property : RuntimeModel → Prop} + (feature : ∀ id, property (.feature id)) (constant : ∀ value, property (.constant value)) : + ∀ entry ∈ platformEntries, entry.Yields property := by + intro entry listed + obtain ⟨getter, _, rfl⟩ := List.mem_map.mp listed + cases getter <;> unfold Getter.entry <;> apply getterEntry_yields <;> + first | apply feature | apply constant + +#print axioms platformEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/Prefixes.lean b/CIL/Extraction/Prefixes.lean new file mode 100644 index 000000000..fd56c7e96 --- /dev/null +++ b/CIL/Extraction/Prefixes.lean @@ -0,0 +1,227 @@ +import CIL.Extraction.Generics + +/-! +# Instruction prefixes and branch entry + +A prefix modifies the instruction after it, so ECMA-335 forbids a branch into that instruction. +`PrefixEntryFree` states the rule for every pc of raw code, live or not. Body decoding refuses +such a branch, so the code of every method with body evidence satisfies it +(`checkBody_prefixEntryFree`, `MethodEvidenceValid.prefixEntryFree`) and the evidence check +decides nothing more. Raw construction checks branch destinations and prefix structure in the +same walk with `rawPrefixesValid`. Shape evidence checks the prefix operand and taker. A tail +transfer also requires the enclosing return type and the adjacent return. +-/ + +namespace CIL.Extraction + +/-- The address alignment of an aligned block instruction or runtime helper, in bytes. -/ +def defaultBlockAlignment : Nat := 8 + +/-- The opcodes that prefix the instruction after them: `unaligned.`, `volatile.`, `tail.`, + `constrained.`, `no.` and `readonly.`, all between `0xFE12` and `0xFE1E`. -/ +def isPrefixOpcode (opcode : Nat) : Bool := + 0xFE12 <= opcode && opcode <= 0xFE1E && + (opcode == 0xFE12 || opcode == 0xFE13 || opcode == 0xFE14 || opcode == 0xFE16 || + opcode == 0xFE19 || opcode == 0xFE1E) + +/-- A modeled unaligned access with the operand its opcode encodes. -/ +def unalignedTaker (raw : RawInstruction) : Bool := + match raw.operand with + | .none => (70 ≤ raw.opcode && raw.opcode ≤ 77) || (82 ≤ raw.opcode && raw.opcode ≤ 85) || + raw.opcode == 223 || raw.opcode == 65047 || raw.opcode == 65048 + | .field _ _ _ _ false => raw.opcode == 123 || raw.opcode == 125 + | .type _ => raw.opcode == 113 || raw.opcode == 129 + | _ => false + +/-- The alignment in bytes specified by a well-formed `unaligned.` operand. -/ +def prefixAlignment : RawOperand → Option Nat + | .integer 1 => some 1 + | .integer 2 => some 2 + | .integer 4 => some 4 + | _ => none + +/-- The alignment of a `cpblk` or `initblk` after `previous`: the operand of an `unaligned.` + before it when `prefixAlignment` admits it, none for any other `unaligned.` operand, and + `defaultBlockAlignment` after any other instruction or at the first pc. -/ +def blockAlignment : Option RawInstruction → Option Nat + | some ⟨_, 0xFE12, operand⟩ => prefixAlignment operand + | _ => some defaultBlockAlignment + +/-- The call opcode for a constrained instance or static slot. -/ +def constrainedCallOpcode (site : CallSite) : Nat := if site.hasThis then 111 else 40 + +/-- Whether the model admits the prefix operand and the immediately following instruction. + Volatile accesses and suppressed runtime checks have no modeled form. -/ +def prefixTakes (modifier next : RawInstruction) : Bool := + match modifier.opcode, modifier.operand with + | 0xFE12, operand => (prefixAlignment operand).isSome && unalignedTaker next + | 0xFE13, _ | 0xFE19, _ => false + | 0xFE14, .none => match next.opcode, next.operand with + | 40, .call _ | 41, .indirectCall .. => true + | 111, .call site => site.hasThis + | _, _ => false + | 0xFE16, .type _ => match next.operand with + | .call site => next.opcode == constrainedCallOpcode site && + (site.hasThis || site.staticVirtualInterfaceSlot) + | _ => false + | 0xFE1E, .none => next.opcode == 143 && (next.operand matches .type _) + | _, _ => false + +/-- The preceding instruction carries a modifier that the current instruction cannot take. -/ +def invalidPredecessor (previous : Option RawInstruction) (raw : RawInstruction) : Bool := + previous.any fun before => isPrefixOpcode before.opcode && !prefixTakes before raw + +/-- A tail transfer returns the method's result and ends at an adjacent operandless return. -/ +private def tailResultValid (result : TypeExpr) (rest : List RawInstruction) : Bool := + match rest with + | call :: ⟨_, 42, .none⟩ :: _ => match call.operand with + | .call site => site.returnType == result + | .indirectCall returned .. => returned == result + | _ => false + | _ => false + +/-- Each modifier takes the next instruction, and a tail transfer returns the enclosing result. + Raw construction also checks each instruction's branch operands during this walk. -/ +def prefixSequenceValid (result : TypeExpr) (code : List RawInstruction) + (entriesValid : RawInstruction → Bool := fun _ => true) : Bool := + match code with + | [] => true + | instruction :: rest => + entriesValid instruction && (!isPrefixOpcode instruction.opcode || + (rest.head?.any (prefixTakes instruction) && + (instruction.opcode != 0xFE14 || tailResultValid result rest))) && + prefixSequenceValid result rest entriesValid + +/-- No branch or switch of the code enters the instruction after a prefix; a branch to the + prefix itself stays allowed. -/ +def PrefixEntryFree (code : List RawInstruction) : Prop := + ∀ pc instruction, code[pc]? = some instruction → isPrefixOpcode instruction.opcode = true → + code.any (branchesTo (pc + 1)) = false + +/-- A branch may enter the first pc or any pc without a prefix immediately before it. -/ +private def entryAllowed (code : Array RawInstruction) : Nat → Bool + | 0 => true + | pc + 1 => !(indexedRead? code pc).any (isPrefixOpcode ·.opcode) + +/-- Every destination of the instruction satisfies the given entry rule. -/ +private def branchEntriesValid (allowed : Nat → Bool) (instruction : RawInstruction) : Bool := + match instruction.operand with + | .target target => allowed target + | .targets targets => targets.all allowed + | _ => true + +/-- The combined walk checks prefix structure and all branch and switch operands. -/ +theorem prefixSequenceValid_eq (result : TypeExpr) (code : List RawInstruction) + (allowed : Nat → Bool) : + prefixSequenceValid result code (branchEntriesValid allowed) = + (prefixSequenceValid result code && (branchTargets code).all allowed) := by + induction code with + | nil => rfl + | cons first rest ih => + have entries : (branchTargets (first :: rest)).all allowed = + (branchEntriesValid allowed first && (branchTargets rest).all allowed) := by + cases first with + | mk offset opcode operand => + cases operand <;> simp [branchTargets, branchEntriesValid] + simp only [prefixSequenceValid, ih, entries, Bool.true_and] + ac_rfl +#print axioms prefixSequenceValid_eq + +/-- Looking up each destination's preceding instruction decides prefix-entry freedom. -/ +theorem prefixEntryFree_iff (code : List RawInstruction) : + (branchTargets code).all (entryAllowed code.toArray) = true ↔ PrefixEntryFree code := by + rw [List.all_eq_true] + constructor + · intro clear pc instruction found isPrefix + rw [← branchTargets_contains, Bool.eq_false_iff] + intro contains + have blocked := clear _ (List.contains_iff_mem.mp contains) + simp [entryAllowed, indexedRead?_eq, found, isPrefix] at blocked + · intro free target member + cases target with + | zero => rfl + | succ pc => + simp only [entryAllowed, indexedRead?_eq, List.getElem?_toArray] + cases found : code[pc]? with + | none => rfl + | some instruction => + simp only [Option.any_some, Bool.not_eq_true', Bool.eq_false_iff] + intro isPrefix + have clear := free pc instruction found isPrefix + rw [← branchTargets_contains] at clear + exact Bool.noConfusion ((List.contains_iff_mem.mpr member).symm.trans clear) +#print axioms prefixEntryFree_iff + +instance (code : List RawInstruction) : Decidable (PrefixEntryFree code) := + decidable_of_iff _ (prefixEntryFree_iff code) + +/-- Without prefixes every instruction satisfies its prefix window and every entry is free. -/ +private theorem noPrefixes {code : List RawInstruction} (result : TypeExpr) + (absent : code.any (fun raw => isPrefixOpcode raw.opcode) = false) : + prefixSequenceValid result code = true ∧ PrefixEntryFree code := by + constructor + · induction code with + | nil => rfl + | cons raw rest ih => + simp only [List.any_cons, Bool.or_eq_false_iff] at absent + simp [prefixSequenceValid, absent.1, ih absent.2] + · intro pc raw found prefixAt + have present : code.any (fun instruction => isPrefixOpcode instruction.opcode) = true := + List.any_eq_true.mpr ⟨raw, List.mem_of_getElem? found, prefixAt⟩ + exact Bool.noConfusion (absent.symm.trans present) +#print axioms noPrefixes + +/-- Only bodies with a prefix need to read predecessors at branch and switch destinations. -/ +def rawPrefixesValid (result : TypeExpr) (code : List RawInstruction) : Bool := + if code.any (fun raw => isPrefixOpcode raw.opcode) then + prefixSequenceValid result code (branchEntriesValid (entryAllowed code.toArray)) + else true + +/-- Raw method validation checks prefix structure and every branch into a prefix's follower. -/ +theorem rawPrefixesValid_iff (result : TypeExpr) (code : List RawInstruction) : + rawPrefixesValid result code = true ↔ + prefixSequenceValid result code = true ∧ PrefixEntryFree code := by + unfold rawPrefixesValid + split + · rw [prefixSequenceValid_eq, Bool.and_eq_true, prefixEntryFree_iff] + · rename_i absent + simp [noPrefixes result (Bool.eq_false_iff.mpr absent)] +#print axioms rawPrefixesValid_iff + +/-- Every suffix of prefix-valid code still pairs each prefix with its legal follower. -/ +private theorem prefixSequenceValid_drop {result : TypeExpr} {code : List RawInstruction} + (valid : prefixSequenceValid result code = true) (count : Nat) : + prefixSequenceValid result (code.drop count) = true := by + induction count generalizing code with + | zero => simpa using valid + | succ count ih => + cases code with + | nil => rfl + | cons first rest => + exact ih (Bool.and_eq_true_iff.mp valid).2 +#print axioms prefixSequenceValid_drop + +/-- An instruction in a prefix-valid body can take the modifier at the preceding pc. -/ +theorem rawPrefixesValid_predecessor {result : TypeExpr} {code : List RawInstruction} + {pc : Nat} {modifier instruction : RawInstruction} + (valid : rawPrefixesValid result code = true) + (before : code[pc]? = some modifier) (here : code[pc+1]? = some instruction) : + invalidPredecessor (some modifier) instruction = false := by + have suffix := prefixSequenceValid_drop ((rawPrefixesValid_iff _ _).mp valid).1 pc + have first := List.drop_eq_getElem_cons (List.getElem?_eq_some_iff.mp before).choose + have second := List.drop_eq_getElem_cons (List.getElem?_eq_some_iff.mp here).choose + rw [first, second] at suffix + simp only [List.getElem?_eq_some_iff] at before here + obtain ⟨_, rfl⟩ := before + obtain ⟨_, rfl⟩ := here + have window := (Bool.and_eq_true_iff.mp (Bool.and_eq_true_iff.mp suffix).1).2 + simp only [invalidPredecessor, Option.any_some] + cases prefixed : isPrefixOpcode _ with + | false => rfl + | true => + simp only [prefixed, Bool.not_true, Bool.false_or, List.head?_cons, + Option.any_some, Bool.and_eq_true] at window + simp only [window.1, Bool.not_true, Bool.and_false] +#print axioms rawPrefixesValid_predecessor + +end CIL.Extraction diff --git a/CIL/Extraction/ReferenceContentModels.lean b/CIL/Extraction/ReferenceContentModels.lean new file mode 100644 index 000000000..750225914 --- /dev/null +++ b/CIL/Extraction/ReferenceContentModels.lean @@ -0,0 +1,64 @@ +import CIL.Extraction.ModelTable + +/-! The reference-content query for types with checked reference-free storage. -/ + +namespace CIL.Extraction + +/-- The generic RuntimeHelpers member with no parameters and a Boolean result. -/ +private def queryOwner := + TypeExpr.atom runtimeScope "System.Runtime.CompilerServices.RuntimeHelpers" + +/-- The open declaration has one method parameter and no value parameters. -/ +private def queryDeclaration : OpenSignature := + { convention := 0x10, returnType := .atom runtimeScope "System.Boolean" } + +/-- The exact open MemberRef whose closed type argument supplies the queried storage. -/ +private def queryOperand : ModelOperand := + let name := "IsReferenceOrContainsReferences" + let owner := "System.Runtime.CompilerServices.RuntimeHelpers" + let target := s!"{runtimeScope}::System.Boolean {owner}::{name}()" + .external (fun _ => queryDeclaration) name (fun _ => some target) + +/-- Only the static query closed over one method argument has this result. -/ +private def querySignature (method : MethodRef) : Bool := + !method.hasThis && method.genericArity == 1 && method.methodArguments.length == 1 && + method.signature 0x10 == queryDeclaration + +/-- Field storage binds a primitive, enum or aggregate. Vectors must have integer lanes. -/ +private def queryValue (storage : TypeExpr → Option StorageType) (method : MethodRef) : + Option W32 := do + let [element] := method.methodArguments | none + guard ((storage element matches some (.field _)) || (integerVectorWidth element).isSome) + return 0 + +/-- The query's single reserved overload reads storage through the checked layout index. -/ +private def queryEntry : ModelEntry := + let row : ModelOverload := + { operand := queryOperand, accepts := querySignature + resolve := fun reader method => RuntimeModel.constant <$> queryValue reader.storage method } + { opcode := .call, owner := queryOwner, name := name!"IsReferenceOrContainsReferences", + overloads := [row], ownsName := true } + +/-- The reference-content model family. -/ +def referenceContentEntries := [queryEntry] + +/-- Reachability reads the same operand, closed signature and indexed storage as the model. -/ +def referenceContentFact (layouts : Array RawLayout) (site : CallSite) : Option W32 := do + guard (site.method.name == queryEntry.name && site.method.owner == queryOwner && + querySignature site.method && queryOperand.bindsSite site) + queryValue (typeStorageAt (layouts[·]?)) site.method + +/-- The reference-content query resolves only to a constant model. -/ +theorem referenceContentEntries_yields {property : RuntimeModel → Prop} + (constant : ∀ value, property (.constant value)) : + ∀ entry ∈ referenceContentEntries, entry.Yields property := by + intro entry listed reader method model found + obtain rfl := List.mem_singleton.mp listed + simp only [ModelEntry.resolve, queryEntry, ModelEntry.select?, ModelEntry.select?.select] at found + split at found <;> simp_all + obtain ⟨_, _, rfl⟩ := found + exact constant _ + +#print axioms referenceContentEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/RuntimeModels.lean b/CIL/Extraction/RuntimeModels.lean new file mode 100644 index 000000000..98c8a3ff3 --- /dev/null +++ b/CIL/Extraction/RuntimeModels.lean @@ -0,0 +1,168 @@ +import CIL.Extraction.ModelTable +import CIL.Extraction.ObjectConstruction +import CIL.Extraction.WideArithmetic +import CIL.Extraction.VectorModels +import CIL.Extraction.HardwareModels +import CIL.Extraction.VectorHelpers +import CIL.Extraction.PlatformModels +import CIL.Extraction.ReferenceContentModels +import CIL.Extraction.GuardModels +import CIL.Extraction.MarshalModels +import CIL.Extraction.BinaryModels +import CIL.Extraction.BitModels +import CIL.Extraction.UnsafeReinterpretations +import CIL.Extraction.UnsafeOffsets +import CIL.Extraction.UnsafeTransferModels +import CIL.Extraction.UnsafeBlocks +import CIL.Extraction.AsSpanModels +import CIL.Extraction.StaticSpanModel +import CIL.Extraction.SpanBulkModels +import CIL.Extraction.SpanConstructorModels +import CIL.Extraction.RuntimeTypeModels + +/-! +# Runtime model resolution + +`runtimeModel` resolves calls and constructors by opcode and method. A modelled site keeps +its operand and has no link. The checked interpreter runs the model for that operand. +An unlinked method-token call that no model claims is refused. +-/ + +namespace CIL.Extraction + +/-- The families registered in the runtime model table. -/ +def modelFamilies : List (List ModelEntry) := [ + [objectConstructorEntry], + runtimeTypeEntries, + vectorEntries, + vectorHelperEntries, + hardwareEntries, + integerMathEntries, + wideArithmeticEntries, + binaryEntries, + bitEntries, + unsafeEntries, + platformEntries, + referenceContentEntries, + guardEntries, + marshalEntries, + unsafeReinterpretEntries, + unsafeReferenceEntries, + unsafeOffsetEntries, + unsafeTransferEntries, + unsafeBlockEntries, + spanEntries, + asSpanEntries, + [staticSpanEntry], + spanBulkEntries, + spanConstructorEntries, +] + +/-- The runtime methods with a model. -/ +def modelEntries : List ModelEntry := + modelFamilies.flatten + +/-- The table of `modelEntries`. -/ +def runtimeModels : ModelTable := model_table% modelEntries + +/-- The runtime model a call under `opcode` to `method` stands for, if any. -/ +def runtimeModel (environment : LayoutEnvironment) (opcode : CallOpcode) (method : MethodRef) : + Option RuntimeModel := + runtimeModels.find environment opcode method + +/-- Every hardware slot, exact or not, selects its own entry of the runtime table. -/ +theorem runtimeModels_hardwareRegistered : HardwareRegistered runtimeModels := by + hardware_registrations + +#print axioms runtimeModels_hardwareRegistered + +/-- Every hardware reference, DivRem among them, resolves through the rows of its own slot. -/ +theorem runtimeModels_hardware (environment : LayoutEnvironment) (method : MethodRef) + (member : hardwareCallFamily method = true) : + runtimeModel environment .call method = hardwareModel environment method := + runtimeModels_hardwareRegistered.resolve environment method member + +#print axioms runtimeModels_hardware + +/-- Every exact hardware reference selects the operation of its own closed signature. -/ +theorem runtimeModels_exactHardware (environment : LayoutEnvironment) (method : MethodRef) + (member : exactCallFamily method = true) : + runtimeModel environment .call method = exactIntrinsic method := + runtimeModels_hardwareRegistered.resolveExact environment method member + +#print axioms runtimeModels_exactHardware + +/-- The mechanism responsible for a method-token call site. -/ +inductive SiteClaim where + | link + | model + | refused + deriving DecidableEq, Repr + +/-- A link claims its pc first, followed by a model family or resolving overload. -/ +def siteClaim (environment : LayoutEnvironment) (link : Option Link) (raw : RawInstruction) + (claims : LayoutEnvironment → CallOpcode → MethodRef → Bool := runtimeModels.claims) : + SiteClaim := + if !methodTokenCall raw.opcode then .refused else + match raw.operand with + | .call site => + if link.isSome then .link + else if (callOpcode? raw.opcode).any (fun op => claims environment op site.method) then .model + else .refused + | _ => .refused + +/-- A modelled opcode and method use the body's runtime environment and require no link. -/ +def callModelled (body : Method) (site : CallOpcode × MethodRef) : Bool := + (runtimeModel body.environment site.1 site.2).isSome + +open Lean Meta Elab Tactic in +/-- Each literal table entry occurs at its recorded index in the registered list. -/ +private def proveEntryMembership : TacticM Unit := do + let cells ← literalCells (mkConst ``modelEntries) + let key (entry : Expr) := unsafe evalExpr Nat (mkConst ``Nat) + (mkApp (mkConst ``ModelEntry.key) entry) + let mut positions : Std.HashMap Expr Nat := {} + let mut byName : Std.HashMap Nat (List Nat) := {} + for index in [:cells.size] do + positions := positions.insert cells[index]!.consumeMData index + let name ← key cells[index]! + byName := byName.insert name (index :: byName.getD name []) + for goal in ← getGoals do + let target := (← instantiateMVars (← goal.getType)).consumeMData + unless target.isApp do throwError "expected entry membership: {target}" + let entry := target.appArg!.consumeMData + let found ← match positions.get? entry with + | some index => pure (some index) + | none => (byName.getD (← key entry) []).findM? (fun i => isDefEq cells[i]! entry) + let some index := found | throwError "unregistered model entry: {entry}" + let type := mkConst ``ModelEntry + let value := mkApp2 (mkConst ``Option.some [0]) type entry + goal.assign (mkApp5 (mkConst ``List.mem_of_getElem? [0]) type (mkConst ``modelEntries) + (mkNatLit index) entry (← mkEqRefl value)) + setGoals [] + +/-- Every entry of the runtime table is one of `modelEntries`. -/ +theorem runtimeModels_entries : ∀ group ∈ runtimeModels.toList, ∀ entry ∈ group.2, + entry ∈ modelEntries := by + simp only [runtimeModels, SearchTree.toList, List.forall_mem_append, List.forall_mem_cons, + List.not_mem_nil, false_implies, implies_true, and_true, true_and] + repeat' first | apply And.intro | exact True.intro + run_tac proveEntryMembership + +#print axioms runtimeModels_entries + +/-- A model the runtime table gives has every property the models of each entry of + `modelEntries` have, so adding an entry adds one case to such a proof. -/ +theorem runtimeModel_yields {property : RuntimeModel → Prop} + (every : ∀ entry ∈ modelEntries, entry.Yields property) {environment : LayoutEnvironment} + {opcode : CallOpcode} {method : MethodRef} {model : RuntimeModel} + (found : runtimeModel environment opcode method = some model) : property model := + ModelTable.find_yields (fun group listed entry member => + every entry (runtimeModels_entries group listed entry member)) found + +#print axioms runtimeModel_yields + +/-- The catalog used by extracted methods and the checked interpreter. -/ +def runtimeCatalog : ModelCatalog := ⟨runtimeModels⟩ + +end CIL.Extraction diff --git a/CIL/Extraction/RuntimeTypeModels.lean b/CIL/Extraction/RuntimeTypeModels.lean new file mode 100644 index 000000000..19c849927 --- /dev/null +++ b/CIL/Extraction/RuntimeTypeModels.lean @@ -0,0 +1,58 @@ +import CIL.Extraction.ModelTable + +/-! Closed method operands for System.Type handles and equality. -/ + +namespace CIL.Extraction + +/-- The exact call site of a static `System.Type` method with `parameters` parameters; + `methodName` is the metadata name spelling `name`. -/ +private def typeMethod (name : String) (methodName : MetadataName) (signature : String) + (parameters : Nat) : CallSite := + let type := TypeExpr.atom runtimeScope (metadata_text% "System.Type") + let fromHandle := methodName == name!"GetTypeFromHandle" + let method : MethodRef := { + owner := type, name := methodName, hasThis := false, + genericArity := 0, + returnType := if fromHandle then type else + .atom runtimeScope (metadata_text% "System.Boolean"), + parameters := if fromHandle then + [.atom runtimeScope (metadata_text% "System.RuntimeTypeHandle")] + else [type, type] } + { method, declared := method.signature 0, name, + target := .external (runtimeScope ++ "::" ++ signature), typeArguments := [], + parameterCount := parameters, returnsValue := true } + +/-- The exact System.Type.GetTypeFromHandle call. -/ +def getTypeFromHandle : CallSite := + typeMethod (metadata_text% "GetTypeFromHandle") name!"GetTypeFromHandle" + (metadata_text% "System.Type System.Type::GetTypeFromHandle(System.RuntimeTypeHandle)") 1 + +/-- The exact System.Type equality operator call selected by its polarity. -/ +def runtimeTypeComparison (equal : Bool) : CallSite := + if equal then typeMethod (metadata_text% "op_Equality") name!"op_Equality" + (metadata_text% "System.Boolean System.Type::op_Equality(System.Type,System.Type)") 2 + else typeMethod (metadata_text% "op_Inequality") name!"op_Inequality" + (metadata_text% "System.Boolean System.Type::op_Inequality(System.Type,System.Type)") 2 + +/-- Closed metadata identities for the three System.Type runtime calls. -/ +def runtimeTypeEntries : List ModelEntry := + [ModelEntry.exact .call getTypeFromHandle.method getTypeFromHandle.declared .typeFromHandle, + ModelEntry.exact .call (runtimeTypeComparison true).method (runtimeTypeComparison true).declared + (.typeEquality true), + ModelEntry.exact .call (runtimeTypeComparison false).method (runtimeTypeComparison false).declared + (.typeEquality false)] + +/-- Each closed System.Type entry resolves to its corresponding handle or comparison model. -/ +theorem runtimeTypeEntries_yields {property : RuntimeModel → Prop} + (handle : property .typeFromHandle) (comparison : ∀ equal, property (.typeEquality equal)) : + ∀ entry ∈ runtimeTypeEntries, entry.Yields property := by + intro entry member + simp only [runtimeTypeEntries, List.mem_cons, List.not_mem_nil, or_false] at member + rcases member with rfl | rfl | rfl + · exact ModelEntry.exact_yields.mpr handle + · exact ModelEntry.exact_yields.mpr (comparison true) + · exact ModelEntry.exact_yields.mpr (comparison false) + +#print axioms runtimeTypeEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/Scalars.lean b/CIL/Extraction/Scalars.lean index 354b622b5..a971df9ec 100644 --- a/CIL/Extraction/Scalars.lean +++ b/CIL/Extraction/Scalars.lean @@ -1,10 +1,11 @@ import CIL.Extraction.Features -import CIL.Extraction.UnsafeReferences -import CIL.Extraction.UnsafeTransfers -import CIL.Extraction.SpanCalls -import CIL.Extraction.ViewObservations -import CIL.Extraction.ObjectConstruction -import CIL.Extraction.GuardCalls +import CIL.Extraction.UnsafeOffsets +import CIL.Extraction.UnsafeTransferModels +import CIL.Extraction.SpanModels +import CIL.Extraction.GuardModels +import CIL.Extraction.Prefixes +import CIL.Extraction.RuntimeModels +import CIL.Resolution /-! # Scalar flow @@ -27,6 +28,9 @@ inductive ScalarKind where | word32 | word64 | native | reference | nullObject | other /-- A class instance reference of an admitted object type. -/ | object + /-- A class reference loaded from a field of an `opaqueFieldType`, of a class the model does + not name: only a store into such a field, a type test or a null test takes it. -/ + | opaque | array (element : ArrayElement) | enumSpan (element : ArrayElement) (writable : Bool) (reference : Bool) | methodPointer (signature : ManagedSignature) @@ -37,10 +41,13 @@ inductive ScalarKind where /-- The claimed stack kinds (top first) at each raw pc; `none` where the pc is unreachable. -/ abbrev ScalarCertificate := List (Option (List ScalarKind)) -/-- The stack kind of a metadata type: small integers widen to `word32`, enums use their - underlying type. -/ +/-- Small integers widen to `word32`. Enums take the stack kind of their immediate integer + atom, and a malformed enum has kind `other`. -/ def scalarType : TypeExpr → ScalarKind - | .enum _ underlying => scalarType underlying + | .enum _ underlying => match enumUnderlyingField underlying with + | some .signed64 | some .unsigned64 => .word64 + | some _ => .word32 + | none => .other | .pointer .void => .other | .byref element => match scalarType element with | .enumSpan identity writable _ => .enumSpan identity writable true @@ -57,7 +64,7 @@ def scalarType : TypeExpr → ScalarKind | some (.span _ writable), some identity => .enumSpan identity writable false | _, _ => .other | .atom scope name => - if scope != runtimeScope then .other + if scope != runtimeScope && scope != coreLibraryScope then .other else if name ∈ ["System.IntPtr", "System.UIntPtr"] then .native else if name ∈ ["System.Int64", "System.UInt64"] then .word64 else if name ∈ ["System.Boolean", "System.Char", "System.SByte", "System.Byte", "System.Int16", @@ -65,11 +72,12 @@ def scalarType : TypeExpr → ScalarKind | _ => .other /-- The stack kind of the receiver of an instance method declared by `owner`: an object - reference for a class, a byref for a value type. -/ + reference for a class or `System.Object`, a byref for a value type. -/ def receiverKind (owner : TypeExpr) : ScalarKind := match owner with | .object .. => .object - | _ => scalarType (.byref owner) + | _ => if owner == .atom runtimeScope "System.Object" then .object + else scalarType (.byref owner) private def numeric (kind : ScalarKind) : Bool := kind ∈ [.word32, .word64, .native] @@ -115,32 +123,31 @@ private def arrayOperand : ScalarKind → Option (Option ArrayElement) | .nullObject => some none | _ => none -/-- `ldlen`, `ldelem.ref` and `stelem.ref` on an array of class references, which is an object; - a null array reaches the same operations, which throw for it. -/ +/-- The stack after `ldlen`, `ldelem.ref` and `stelem.ref` on an array of class references, which + is an object; a null array reaches the same operations, which throw for it. -/ private def referenceArrayStep (instruction : RawInstruction) (stack : List ScalarKind) : - Option (MemoryOp × List ScalarKind) := + Option (List ScalarKind) := if instruction.operand != .none then none else match instruction.opcode, stack with - | 142, .object :: tail => some (.referenceArrayLength, .native :: tail) + | 142, .object :: tail => some (.native :: tail) | 154, index :: array :: tail => - if index ∈ [.word32, .native] && array ∈ [.object, .nullObject] then - some (.referenceArrayLoad, .object :: tail) + if index ∈ [.word32, .native] && array ∈ [.object, .nullObject] then some (.object :: tail) else none | 162, value :: index :: array :: tail => if value ∈ [.object, .nullObject] && index ∈ [.word32, .native] && - array ∈ [.object, .nullObject] then some (.referenceArrayStore, tail) + array ∈ [.object, .nullObject] then some tail else none | _, _ => none -/-- Exact array instruction and stack transition, including primitive tokens and element types. -/ +/-- Exact array stack transition, including primitive tokens and element types. -/ private def arrayStep (instruction : RawInstruction) (stack : List ScalarKind) : - Option (MemoryOp × List ScalarKind) := do - if let some step := referenceArrayStep instruction stack then return step + Option (List ScalarKind) := do + if let some after := referenceArrayStep instruction stack then return after let opcode := instruction.opcode if opcode == 142 then let array :: tail := stack | none let _ ← arrayOperand array - if instruction.operand == .none then return (.arrayLength, .native :: tail) else none + if instruction.operand == .none then return .native :: tail else none let store := (155 ≤ opcode && opcode ≤ 159) || opcode == 164 let (value, input) := if store then (stack.head?, stack.drop 1) else (none, stack) let index :: array :: tail := input | none @@ -150,120 +157,95 @@ private def arrayStep (instruction : RawInstruction) (stack : List ScalarKind) : let .type token := instruction.operand | none let expected ← arrayElementType token if element.isSome && element != some expected then none else - return (.arrayAddress expected, .reference :: tail) + return .reference :: tail if opcode ∈ [163, 164] then let .type token := instruction.operand | none let expected ← arrayElementType token if element.isSome && element != some expected then none else pure () - match expected with - | .aggregate .. | .vector .. => + if expected.composite then if store then - if value == some .other then return (.arrayTransfer expected true, tail) else none - else return (.arrayTransfer expected false, .other :: tail) - | _ => pure () - let (width, signed, native) : Nat × Bool × Bool ← + if value == some .other then return tail else none + else return .other :: tail + let (width, native) : Nat × Bool ← if opcode ∈ [163, 164] then do let .type token := instruction.operand | none let expected ← arrayElementType token if element.isSome && element != some expected then none else - pure (expected.width, expected.signed, expected.native) + pure (expected.width, expected.native) else do if instruction.operand != .none then none else pure () - let (width, signed, native) ← match opcode with - | 144 => some (1, true, false) | 145 => some (1, false, false) - | 146 => some (2, true, false) | 147 => some (2, false, false) - | 148 | 149 => some (4, false, false) - | 150 => some (8, false, false) | 151 | 155 => some (8, false, true) - | 156 => some (1, false, false) | 157 => some (2, false, false) - | 158 => some (4, false, false) | 159 => some (8, false, false) + let (width, native) ← match opcode with + | 144 | 145 | 156 => some (1, false) | 146 | 147 | 157 => some (2, false) + | 148 | 149 | 158 => some (4, false) | 150 | 159 => some (8, false) + | 151 | 155 => some (8, true) | _ => none if let some actual := element then - if (match actual with | .aggregate .. | .vector .. => true | _ => false) then none else pure () + if actual.composite then none else pure () if actual.width != width || actual.native != native then none else pure () - pure (width, signed, native) + pure (width, native) let kind := if native then ScalarKind.native else if width == 8 then .word64 else .word32 if store then - if value == some kind then return (.arrayStore width, tail) else none - else return (.arrayLoad width signed, kind :: tail) + if value == some kind then return tail else none + else return kind :: tail /-- Array operations cannot be introduced through an unrelated stack-effect-only encoding. -/ -private def arrayEmission : Op → Bool - | .newArray _ | .newReferenceArray _ => true - | .memory .arrayLength | .memory (.arrayLoad ..) | .memory (.arrayStore _) - | .memory (.arrayAddress ..) => true - | .memory (.arrayTransfer ..) => true - | .memory .referenceArrayLength | .memory .referenceArrayLoad - | .memory .referenceArrayStore => true +private def arrayEmission : Instruction → Bool + | .newarr _ => true + | .ldlen | .ldelem _ | .ldelem_ref | .stelem _ | .stelem_ref | .ldelem_any _ | .stelem_any _ + | .ldelema .. => true | _ => false -/-- The emitted array operation agrees exactly with its independently decoded raw opcode. -/ -private def arrayMatches (instruction : RawInstruction) (stack : List ScalarKind) - (emitted : Op) : Bool := +/-- The emitted array operation agrees exactly with its independently decoded raw opcode: a + decoded opcode emits its decoding after `previous`, the raw instruction before it, and a + typed one the operation its element gives. -/ +private def arrayMatches (previous : Option RawInstruction) (instruction : RawInstruction) + (stack : List ScalarKind) (emitted : Instruction) (skipEncoding : Bool) : Bool := if instruction.opcode == 141 then match instruction.operand, stack, emitted with - | .type token, length :: _, .newArray element => - length ∈ [.word32, .native] && element.allocationSupported && - arrayElementType token == some element.metadata - | .type token@(.object ..), length :: _, .newReferenceArray element => - length ∈ [.word32, .native] && token == element + | .type token, length :: _, .newarr element => length ∈ [.word32, .native] && token == element | _, _, _ => false else - match arrayStep instruction stack, emitted with - | some (.arrayLength, _), .memory .arrayLength => true - | some (.arrayLoad width signed, _), .memory (.arrayLoad actual polarity) => - width == actual && signed == polarity - | some (.arrayStore width, _), .memory (.arrayStore actual) => width == actual - | some (.arrayAddress element _, _), .memory (.arrayAddress actual _) => - element == actual.metadata - | some (.arrayTransfer element store, _), .memory (.arrayTransfer actual actualStore) => - element == actual.metadata && store == actualStore - | some (.referenceArrayLength, _), .memory .referenceArrayLength - | some (.referenceArrayLoad, _), .memory .referenceArrayLoad - | some (.referenceArrayStore, _), .memory .referenceArrayStore => true - | _, _ => false + Instruction.valueOpcode instruction.opcode && + (skipEncoding || valueEncodingMatches previous instruction emitted) /-- Opcodes whose emitted encoding the scalar check fixes: integer constants, arithmetic, shifts, `neg`, `not`, conversions and compare-branches. -/ def scalarCovered (opcode : Nat) : Bool := opcode == 141 || arrayCovered opcode || (21 <= opcode && opcode <= 33) || scalarBinary opcode || conversion opcode || - compareBranch opcode || opcode ∈ [20, 44, 45, 57, 58, 98, 99, 100, 101, 102, 65054] + compareBranch opcode || opcode ∈ [20, 44, 45, 57, 58, 98, 99, 100, 101, 102] /-- The emitted operation each covered non-branch opcode must produce. -/ -private def scalarEncodingMatches (opcode : Nat) (emitted : Op) : Bool := +private def scalarEncodingMatches (opcode : Nat) (emitted : Instruction) : Bool := match opcode, emitted with -- Arithmetic, division and remainder. | 88, .add | 89, .sub | 90, .mul => true - | 91, .divide true false | 92, .divide false false - | 93, .divide true true | 94, .divide false true => true + | 91, .div | 92, .div_un | 93, .rem | 94, .rem_un => true -- Bitwise, shifts, `neg` and `not`. - | 95, .band | 96, .bor | 97, .bxor | 98, .shl | 99, .shr | 100, .shrUn - | 101, .neg | 102, .bnot => true - -- Conversions; `checkedConvert width signed unsignedInput`. - | 103, .convI1 | 104, .convI2 | 105, .convI4 | 106, .convI8 | 109, .convU4 | 110, .convU8 => true - | 130, .checkedConvert 8 true true | 131, .checkedConvert 16 true true - | 132, .checkedConvert 32 true true | 133, .checkedConvert 64 true true - | 134, .checkedConvert 8 false true | 135, .checkedConvert 16 false true - | 136, .checkedConvert 32 false true | 137, .checkedConvert 64 false true - | 138, .checkedConvert 64 true true | 139, .checkedConvert 64 false true - | 179, .checkedConvert 8 true false | 180, .checkedConvert 8 false false - | 181, .checkedConvert 16 true false | 182, .checkedConvert 16 false false - | 183, .checkedConvert 32 true false | 184, .checkedConvert 32 false false - | 185, .checkedConvert 64 true false | 186, .checkedConvert 64 false false => true - | 209, .convU2 | 210, .convU1 | 211, .convI8 | 224, .convU => true - | 212, .checkedConvert 64 true false | 213, .checkedConvert 64 false false => true + | 95, .and | 96, .or | 97, .xor | 98, .shl | 99, .shr | 100, .shr_un + | 101, .neg | 102, .not => true + -- Conversions, wrapping and overflow-checked. + | 103, .conv_i1 | 104, .conv_i2 | 105, .conv_i4 | 106, .conv_i8 | 109, .conv_u4 + | 110, .conv_u8 => true + | 130, .conv_ovf_un .i1 | 131, .conv_ovf_un .i2 | 132, .conv_ovf_un .i4 + | 133, .conv_ovf_un .i8 | 134, .conv_ovf_un .u1 | 135, .conv_ovf_un .u2 + | 136, .conv_ovf_un .u4 | 137, .conv_ovf_un .u8 | 138, .conv_ovf_un .i + | 139, .conv_ovf_un .u => true + | 179, .conv_ovf .i1 | 180, .conv_ovf .u1 | 181, .conv_ovf .i2 | 182, .conv_ovf .u2 + | 183, .conv_ovf .i4 | 184, .conv_ovf .u4 | 185, .conv_ovf .i8 | 186, .conv_ovf .u8 => true + | 209, .conv_u2 | 210, .conv_u1 | 211, .conv_i | 224, .conv_u => true + | 212, .conv_ovf .i | 213, .conv_ovf .u => true -- Overflow-checked arithmetic and comparisons. - | 214, .checkedArithmetic .add true | 215, .checkedArithmetic .add false - | 216, .checkedArithmetic .mul true | 217, .checkedArithmetic .mul false - | 218, .checkedArithmetic .sub true | 219, .checkedArithmetic .sub false => true - | 65025, .eq | 65026, .gt | 65027, .gtu | 65028, .lt | 65029, .ltu => true + | 214, .add_ovf | 215, .add_ovf_un | 216, .mul_ovf | 217, .mul_ovf_un + | 218, .sub_ovf | 219, .sub_ovf_un => true + | 65025, .ceq | 65026, .cgt | 65027, .cgt_un | 65028, .clt | 65029, .clt_un => true | _, _ => false /-- The emitted branch each compare-branch opcode must produce, keeping its target. -/ -private def branchEncodingMatches (opcode target : Nat) (emitted : Op) : Bool := +private def branchEncodingMatches (opcode target : Nat) (emitted : Instruction) : Bool := (comparisonBranchIndex opcode).any fun index => match index, emitted with | 0, .beq actual | 1, .bge actual | 2, .bgt actual | 3, .ble actual | 4, .blt actual - | 5, .bne actual | 6, .bgeu actual | 7, .bgtu actual | 8, .bleu actual - | 9, .bltu actual => actual == target + | 5, .bne_un actual | 6, .bge_un actual | 7, .bgt_un actual | 8, .ble_un actual + | 9, .blt_un actual => actual == target | _, _ => false /-- One operand is a native int and the other an `int32`. -/ @@ -284,24 +266,18 @@ private def referenceComparison (opcode : Nat) (left right : ScalarKind) : Bool right ∈ [.object, .nullObject]) || (opcode == 65027 && left == .object && right == .nullObject) -/-- Mixed native/int32 operands require exactly one `nativeBinary` wrapper; any other pair - forbids it. -/ -private def nativeWrapped (left right : ScalarKind) (encoding : Op → Bool) : Op → Bool - | .nativeBinary operation => mixedNative left right && encoding operation - | operation => !mixedNative left right && encoding operation - /-- The constant an `ldc` instruction must emit; an out-of-range operand has none. -/ -private def literal (instruction : RawInstruction) : Option Op := +private def literal (instruction : RawInstruction) : Option Instruction := match instruction.opcode, instruction.operand with - | 21, .none => some (.const32 (BitVec.ofInt 32 (-1))) + | 21, .none => some (.ldc_i4 (BitVec.ofInt 32 (-1))) | 31, .integer value => - if -128 <= value && value < 128 then some (.const32 (BitVec.ofInt 32 value)) else none + if -128 <= value && value < 128 then some (.ldc_i4 (BitVec.ofInt 32 value)) else none | 32, .integer value => - if -(2^31) <= value && value < 2^31 then some (.const32 (BitVec.ofInt 32 value)) else none + if -(2^31) <= value && value < 2^31 then some (.ldc_i4 (BitVec.ofInt 32 value)) else none | 33, .integer value => - if -(2^63) <= value && value < 2^63 then some (.const64 (BitVec.ofInt 64 value)) else none + if -(2^63) <= value && value < 2^63 then some (.ldc_i8 (BitVec.ofInt 64 value)) else none | opcode, .none => - if 22 <= opcode && opcode <= 30 then some (.const32 (BitVec.ofNat 32 (opcode - 22))) else none + if 22 <= opcode && opcode <= 30 then some (.ldc_i4 (BitVec.ofNat 32 (opcode - 22))) else none | _, _ => none /-- Pointer addition admits an int32 or native byte offset on either side of the reference. -/ @@ -309,40 +285,39 @@ private def referenceAddition : ScalarKind → ScalarKind → Bool | .reference, offset | offset, .reference => offset ∈ [.word32, .native] | _, _ => false -/-- A covered opcode emits exactly the encoding its operand kinds imply. -/ -def scalarOpMatches (instruction : RawInstruction) (stack : List ScalarKind) (emitted : Op) : - Bool := +/-- A covered opcode after `previous`, the raw instruction before it, emits exactly the + encoding its operand kinds imply. -/ +def scalarOpMatches (previous : Option RawInstruction) (instruction : RawInstruction) + (stack : List ScalarKind) (emitted : Instruction) (skipEncoding := false) : Bool := if instruction.opcode == 141 || arrayCovered instruction.opcode || arrayEmission emitted then - arrayMatches instruction stack emitted - else if instruction.opcode == 65054 then - instruction.operand == .none && match emitted with | .nop => true | _ => false + arrayMatches previous instruction stack emitted skipEncoding else if instruction.opcode == 20 then - instruction.operand == .none && match emitted with | .memory .nullObject => true | _ => false + instruction.operand == .none && match emitted with | .ldnull => true | _ => false else if instruction.opcode ∈ [44, 45, 57, 58] then match instruction.operand, stack, emitted with - | .target target, value :: _, .brzero actual => + | .target target, value :: _, .brfalse actual => instruction.opcode ∈ [44, 57] && target == actual && (numeric value || value ∈ [.reference, .nullObject, .object]) - | .target target, value :: _, .brnonzero actual => + | .target target, value :: _, .brtrue actual => instruction.opcode ∈ [45, 58] && target == actual && (numeric value || value ∈ [.reference, .nullObject, .object]) | _, _, _ => false else if 21 <= instruction.opcode && instruction.opcode <= 33 then match literal instruction, emitted with - | some (.const32 expected), .const32 actual => expected == actual - | some (.const64 expected), .const64 actual => expected == actual + | some (.ldc_i4 expected), .ldc_i4 actual => expected == actual + | some (.ldc_i8 expected), .ldc_i8 actual => expected == actual | _, _ => false else if compareBranch instruction.opcode then match instruction.operand, stack with | .target target, right :: left :: _ => (compatibleBinary left right || referenceComparison instruction.opcode left right) && !(mixedNative left right && unsignedBranch instruction.opcode) && - nativeWrapped left right (branchEncodingMatches instruction.opcode target) emitted + branchEncodingMatches instruction.opcode target emitted | _, _ => false else if instruction.operand != .none then false else if instruction.opcode == 88 && (stack[0]? == some .reference || stack[1]? == some .reference) then match stack, emitted with - | right :: left :: _, .memory (.add 1 true) => referenceAddition left right + | right :: left :: _, .add => referenceAddition left right | _, _ => false else if instruction.opcode == 89 && (stack.drop 1).head? == some .reference then match stack.head?, emitted with @@ -351,7 +326,7 @@ def scalarOpMatches (instruction : RawInstruction) (stack : List ScalarKind) (em else if scalarBinary instruction.opcode then match stack with | right :: left :: _ => (compatibleBinary left right || referenceComparison instruction.opcode left right) && - nativeWrapped left right (scalarEncodingMatches instruction.opcode) emitted + scalarEncodingMatches instruction.opcode emitted | _ => false else if instruction.opcode ∈ [98, 99, 100] then match stack with | right :: left :: _ => @@ -364,193 +339,130 @@ def scalarOpMatches (instruction : RawInstruction) (stack : List ScalarKind) (em /-- Operations computing a scalar from operands or a literal; only a covered encoding may emit one. -/ -private def scalarEmission : Op → Bool - | .const32 _ | .const64 _ | .add | .sub | .mul | .divide .. | .band | .bor | .bxor | .shl - | .shr | .shrUn | .neg | .bnot | .eq | .lt | .ltu | .gt | .gtu | .convI1 | .convI2 | .convI4 - | .convI8 | .convU1 | .convU2 | .convU4 | .convU8 | .convU | .checkedConvert .. - | .checkedArithmetic .. | .nativeBinary _ - | .beq _ | .bge _ | .bgt _ | .ble _ | .blt _ | .bne _ | .bgeu _ | .bgtu _ | .bleu _ - | .bltu _ => true +private def scalarEmission : Instruction → Bool + | .ldc_i4 _ | .ldc_i8 _ | .add | .sub | .mul | .and | .or | .xor | .shl + | .shr | .shr_un | .neg | .not | .ceq | .clt | .clt_un | .cgt | .cgt_un | .conv_i1 + | .conv_i2 | .conv_i4 | .conv_i8 | .conv_u1 | .conv_u2 | .conv_u4 | .conv_u8 | .conv_u + | .conv_i | .conv_ovf _ | .conv_ovf_un _ + | .div | .div_un | .rem | .rem_un | .add_ovf | .add_ovf_un | .sub_ovf | .sub_ovf_un + | .mul_ovf | .mul_ovf_un + | .beq _ | .bge _ | .bgt _ | .ble _ | .blt _ | .bne_un _ | .bge_un _ | .bgt_un _ + | .ble_un _ | .blt_un _ => true | _ => false -/-- Byte width of a non-aggregate `sizeof` operand on the admitted 64-bit target. -/ -private def primitiveSize (type : TypeExpr) : Option Nat := - match typeStorage [] type with - | some (.field kind) => primitiveFieldWidth kind - | some .vector128 => some 16 - | some .vector256 => some 32 - | some .vector512 => some 64 - | _ => none - -/-- Exactly `call Unsafe.SizeOf()`. -/ -private def unsafeSizeOf (site : CallSite) : Bool := - site.declaringType == .atom runtimeScope "System.Runtime.CompilerServices.Unsafe" && - site.name == "SizeOf" && !site.hasThis && site.returnsValue && site.parameterCount == 0 && - site.parameterTypes.isEmpty && site.returnType == .atom runtimeScope "System.Int32" && - site.target == .external (runtimeScope ++ - "::System.Int32 System.Runtime.CompilerServices.Unsafe::SizeOf()") - -/-- A size constant has its primitive width; an aggregate size is bound by its layout use - instead. -/ -private def sizeConstant (instruction : RawInstruction) (bits : W32) : Bool := - let sized : TypeExpr → Bool - | .aggregate .. => true - | type => primitiveSize type == some bits.toNat - match instruction.opcode, instruction.operand with - | 65052, .type type => sized type - | 40, .call site => unsafeSizeOf site && match site.typeArguments with - | [type] => sized type - | _ => false - | _, _ => false - -/-- The width and sign extension of a class instance field of `type`: integer, Boolean, Char - and enum fields only, each fitting one `objectFieldSlot`. -/ -private def objectFieldShape (type : TypeExpr) : Option (Nat × Bool) := - match typeStorage [] type with - | some (.field kind) => (primitiveFieldWidth kind).map fun width => - (width, kind == .signed8 || kind == .signed16) - | _ => none - -/-- An instance field instruction on a class, or an emitted class field operation. -/ -private def objectFieldSite (instruction : RawInstruction) (emitted : Op) : Bool := +/-- An instance field instruction, or an emitted field instruction. -/ +private def instanceFieldSite (instruction : RawInstruction) (emitted : Instruction) : Bool := (instruction.opcode ∈ [123, 124, 125] && match instruction.operand with - | .field (.object _ _) _ _ false => true + | .field _ _ _ _ false => true | _ => false) || match emitted with - | .memory (.objectField ..) | .memory (.setObjectField ..) - | .memory (.objectReferenceField ..) | .memory (.setObjectReferenceField ..) - | .memory (.arrayField ..) | .memory (.setArrayField ..) => true + | .ldfld _ | .ldflda _ | .stfld _ => true | _ => false -/-- A class instance field whose slot holds an object reference: one typed as an admitted class - or as an array of one, which is itself an object on the heap. -/ -private def referenceFieldType : TypeExpr → Bool - | .object _ _ | .array (.object _ _) => true +/-- Whether the raw operand names an instance field of a class or a generic class instance. -/ +private def classOwned (instruction : RawInstruction) : Bool := + match instruction.operand with + | .field owner _ _ _ false => owner matches .object .. || owner.objectApplication | _ => false -/-- The element of a class instance field typed as an array of a value type, as the evaluation - stack records it: a struct element by its identity alone. -/ -private def valueArrayFieldElement (type : TypeExpr) : Option ArrayElement := - match type with - | .array element => arrayElementType element - | _ => none +/-- The kinds a class instance field holding references stores: an opaque reference or null in + a generic class instance, also an object in `System.Object`, an object or null otherwise. -/ +private def storedReferences (type : TypeExpr) : List ScalarKind := + if type.objectApplication then [.opaque, .nullObject] + else if opaqueFieldType type then [.opaque, .object, .nullObject] + else [.object, .nullObject] + +/-- `ldfld` and `stfld` of a class instance field emit the instruction the raw one + decodes to after `previous`, naming the field its operand names, on an object or null + receiver. Class field addresses have no storage model. A stored class or class array must be + an object or null, a stored value array one + of the field's element, and a stored integer of the field's stack kind (`word32` up to 4 + bytes, `word64` or `native` for 8), so `classFieldStore` never meets a word of the other + width; the step resolves the slot and faults on a field type without one. -/ +private def classFieldMatches (previous : Option RawInstruction) (instruction : RawInstruction) + (stack : List ScalarKind) (emitted : Instruction) (skipEncoding : Bool) : Bool := + (skipEncoding || valueEncodingMatches previous instruction emitted) && match emitted, stack with + | .ldfld _, receiver :: _ => receiver ∈ [.object, .nullObject] + | .stfld field, value :: receiver :: _ => + receiver ∈ [.object, .nullObject] && + (!referenceFieldType field.type || value ∈ storedReferences field.type) && + (valueArrayFieldElement field.type).all (fun declared => value == .array declared) && + ((objectFieldShape field.type).isNone || storageCompatible value (scalarType field.type)) + | _, _ => false -/-- An emitted field element is the declared one; a struct element's width is the layout's, - which `MethodLayoutsValid` checks at the layout use naming the field access. -/ -private def fieldElementMatches (declared emitted : ArrayElement) : Bool := - match declared, emitted with - | .aggregate identity _, .aggregate actual _ => identity == actual - | declared, emitted => declared == emitted - -/-- `ldfld` and `stfld` of a class instance field emit the checked field operation for that - owner, at the field's model offset: with its width and extension for a scalar field, as a - reference slot for a class-typed or class-array-typed one, whose stored value must be an - object or null, and as an array slot for a value-array-typed one, whose stored value must - be an array of that element. `ldflda` never does. -/ -private def objectFieldMatches (instruction : RawInstruction) (stack : List ScalarKind) - (emitted : Op) : Bool := - match instruction.opcode, instruction.operand, stack, emitted with - | 123, .field (.object scope name) index type false, receiver :: _, - .memory (.objectField owner offset width signed) => - receiver ∈ [.object, .nullObject] && owner == .object scope name && - offset == objectFieldOffset index && objectFieldShape type == some (width, signed) - | 125, .field (.object scope name) index type false, _ :: receiver :: _, - .memory (.setObjectField owner offset width) => - receiver ∈ [.object, .nullObject] && owner == .object scope name && - offset == objectFieldOffset index && (objectFieldShape type).map (·.1) == some width - | 123, .field (.object scope name) index type false, receiver :: _, - .memory (.objectReferenceField owner offset) => - referenceFieldType type && receiver ∈ [.object, .nullObject] && - owner == .object scope name && offset == objectFieldOffset index - | 125, .field (.object scope name) index type false, value :: receiver :: _, - .memory (.setObjectReferenceField owner offset) => - referenceFieldType type && value ∈ [.object, .nullObject] && - receiver ∈ [.object, .nullObject] && owner == .object scope name && - offset == objectFieldOffset index - | 123, .field (.object scope name) index type false, receiver :: _, - .memory (.arrayField owner offset element) => - (valueArrayFieldElement type).any (fieldElementMatches · element) && - receiver ∈ [.object, .nullObject] && - owner == .object scope name && offset == objectFieldOffset index - | 125, .field (.object scope name) index type false, value :: receiver :: _, - .memory (.setArrayField owner offset element) => - (valueArrayFieldElement type).any (fun declared => - fieldElementMatches declared element && value == .array declared) && - receiver ∈ [.object, .nullObject] && owner == .object scope name && - offset == objectFieldOffset index - | _, _, _, _ => false +/-- `ldfld`, `ldflda` and `stfld` of a struct instance field emit the instruction the raw one + decodes to after `previous`, naming the field its operand names. A load reads through a + reference or from a struct value, an address and a store go through a reference, and a stored + value has the field's stack kind; the step takes the offset and width from the layouts. -/ +private def structFieldMatches (previous : Option RawInstruction) (instruction : RawInstruction) + (stack : List ScalarKind) (emitted : Instruction) (skipEncoding : Bool) : Bool := + (skipEncoding || valueEncodingMatches previous instruction emitted) && match emitted, stack with + | .ldfld _, receiver :: _ => receiver ∈ [.reference, .other] + | .ldflda _, receiver :: _ => receiver == .reference + | .stfld field, value :: receiver :: _ => + receiver == .reference && storageCompatible value (scalarType field.type) + | _, _ => false /-- Only `ldstr` loads a literal, and only its own text; inside a folded throw it emits nothing. -/ private def stringLiteralMatches (role : Option FoldRole) (instruction : RawInstruction) - (emitted : Op) : Bool := + (emitted : Instruction) : Bool := match instruction.opcode, instruction.operand, emitted with | 114, .text _, .nop => role ∈ [some .head, some .interior] - | 114, .text text, .memory (.stringLiteral actual) => role == none && text == actual + | 114, .text text, .ldstr actual => role == none && text == actual | _, _, _ => false -/-- Covered opcodes emit exactly their encoding, and a class type test only from `castclass` - (throwing) or `isinst` on an object. Elsewhere only a decided type test's word, - a size constant or an exact runtime query may emit a scalar operation. -/ -def scalarEmissionMatches (layouts : Array RawLayout) (role : Option FoldRole) - (instruction : RawInstruction) - (stack : List ScalarKind) (emitted : Op) : Bool := - if let some (.typeBranch taken) := role then typeBranchMatches instruction taken emitted - else if let some (.result value) := role then - match emitted with | .const32 bits => bits == if value then 1 else 0 | _ => false - else if let .objectTest type throwing := emitted then +/-- The decoded `readonly.` rule at one site after `previous`: `readonly.` and `ldelema` emit + their decoding, so an `ldelema` is read-only exactly when `readonly.` precedes it. -/ +def readOnlyDecodedMatches (previous : Option RawInstruction) (instruction : RawInstruction) + (emitted : Instruction) (skipEncoding := false) : Bool := + !(instruction.opcode == 143 || instruction.opcode == 65054) || + (skipEncoding || valueEncodingMatches previous instruction emitted) + +/-- Covered opcodes emit exactly their encoding, and a class type test only from the raw + `castclass` or `isinst` of the same class on an object. Elsewhere only a decided type test's + word or the exact endianness field may emit a scalar operation. `previous` is + the raw instruction before `instruction`, `none` at the first pc. A `no.` prefix has no + scalar emission, and `readonly.` must agree with the decoded address. -/ +def scalarEmissionMatches (role : Option FoldRole) + (previous : Option RawInstruction) (instruction : RawInstruction) + (stack : List ScalarKind) (emitted : Instruction) (skipEncoding := false) : Bool := + if instruction.opcode == 0xFE19 || invalidPredecessor previous instruction then false + else if (emitted matches .ldobj _ | .stobj _ | .initobj _ | .cpobj _ | .sizeof _) || + objectOpcode instruction.opcode then + skipEncoding || valueEncodingMatches previous instruction emitted + else if !readOnlyDecodedMatches previous instruction emitted skipEncoding then false + else if role matches some .typeHead | some .typeOperand | some (.typeBranch _) | some (.result _) then + valueEncodingMatches previous instruction emitted + else if let some (opcode, type) := typeTestSource emitted then match instruction.operand, stack with | .type target, value :: _ => - instruction.opcode ∈ [116, 117] && throwing == (instruction.opcode == 116) && - type == target && value ∈ [.object, .nullObject] - | _, _ => false - else if (match instruction.operand with - | .call site => referenceContentQuery site | _ => false) then - match instruction.operand, emitted with - | .call site, .const32 bits => instruction.opcode == 40 && - referenceContentConstant layouts site == some bits + instruction.opcode == opcode && type == target && value ∈ [.object, .opaque, .nullObject] | _, _ => false else if instruction.opcode == 77 && (stack.head?).any (fun kind => match kind with | .nativeMethodTable _ => true | _ => false) then match stack, emitted with - | .nativeMethodTable signature :: _, .managed (.tableLoad actual) => signature == actual + | .nativeMethodTable _ :: _, .ldind .i => true | _, _ => false else if instruction.opcode == 88 && (match stack with | .nativeMethodTable _ :: _ | _ :: .nativeMethodTable _ :: _ => true | _ => false) then - match stack, emitted with - | offset :: .nativeMethodTable _ :: _, .managed .tableOffset - | .nativeMethodTable _ :: offset :: _, .managed .tableOffset => offset ∈ [.word32, .native] + instruction.operand == .none && match stack, emitted with + | offset :: .nativeMethodTable _ :: _, .add + | .nativeMethodTable _ :: offset :: _, .add => offset ∈ [.word32, .native] | _, _ => false - else if let .managed operation := emitted then match operation, stack with - | .address .., _ => instruction.opcode == 65030 - | .call .., _ => instruction.opcode == 41 - | .tableOffset, offset :: .nativeMethodTable _ :: _ - | .tableOffset, .nativeMethodTable _ :: offset :: _ => - instruction.opcode == 88 && offset ∈ [.word32, .native] - | .tableLoad signature, .nativeMethodTable actual :: _ => - instruction.opcode == 77 && signature == actual - | .tableAddress .., _ => false - | .tableLength, .methodTable _ :: _ => instruction.opcode == 142 - | .tableElement signature, index :: .methodTable actual :: _ => - index ∈ [.word32, .native] && signature == actual && - (instruction.opcode == 151 || (instruction.opcode == 163 && - (match instruction.operand with - | .type type => managedSignature type == some signature | _ => false))) - | _, _ => false - else if let some helper := unsafeReferenceHelper instruction then helper.matches emitted - else if arrayEmission emitted then arrayMatches instruction stack emitted + else if emitted matches .ldftn .. then instruction.opcode == 65030 + else if emitted matches .calli .. then instruction.opcode == 41 + else if arrayEmission emitted then arrayMatches previous instruction stack emitted skipEncoding else if instruction.opcode == 114 || - (match emitted with | .memory (.stringLiteral _) => true | _ => false) then + (match emitted with | .ldstr _ => true | _ => false) then stringLiteralMatches role instruction emitted - else if instruction.opcode == 20 && role ∈ [some .head, some .interior] then - instruction.operand == .none && match emitted with | .nop => true | _ => false - else if objectFieldSite instruction emitted then objectFieldMatches instruction stack emitted - else if scalarCovered instruction.opcode then scalarOpMatches instruction stack emitted + else if instanceFieldSite instruction emitted then + if classOwned instruction then + classFieldMatches previous instruction stack emitted skipEncoding + else structFieldMatches previous instruction stack emitted skipEncoding + else if scalarCovered instruction.opcode then + scalarOpMatches previous instruction stack emitted skipEncoding else !scalarEmission emitted || match role, emitted with - | _, .const32 bits => sizeConstant instruction bits || - platformField instruction == some bits || - (instruction.opcode == 40 && match instruction.operand with - | .call site => platformConstant site == some bits - | _ => false) + | _, .ldc_i4 bits => platformField instruction == some bits | _, _ => false /-- The result kind of a conversion opcode: `conv.i`/`conv.u` forms give native ints, @@ -596,12 +508,13 @@ private def joinBytes (separator : List UInt8) : List (List UInt8) → List UInt method type parameter, `&` and `*` suffixes, and `<...>` for generic arguments. -/ private def scalarTypeBytes : TypeExpr → Option (List UInt8) | .void => some (signatureBytes "System.Void") - | .atom _ name | .constructor _ name | .object _ name => some (signatureBytes name) + | .atom _ name | .constructor _ name | .objectConstructor _ name | .object _ name => + some (signatureBytes name) | type@(.aggregate ..) => signatureBytes <$> spanElementName type -- Enum names cannot contain generic owner arguments. | .enum identity _ => (splitBytes (signatureBytes identity) [58, 58])[1]? | .parameter index => some (signatureBytes ("!!" ++ toString index)) - | .functionPointer .. | .signatureParameter .. => none + | .functionPointer .. | .signatureParameter .. | .modified .. | .ownerParameter _ => none -- `&` and `*`. | .byref element => (· ++ [38]) <$> scalarTypeBytes element | .pointer element => (· ++ [42]) <$> scalarTypeBytes element @@ -715,54 +628,45 @@ private def externalResult (site : CallSite) (identity : String) : ScalarKind := /-- The kind a call pushes. -/ private def callResult (site : CallSite) : ScalarKind := - if unsafePointerOffset ⟨0, 40, .call site⟩ || spanCallReturnsReference site || - (viewObservation ⟨0, 40, .call site⟩).any - (fun operation => operation == .spanPinnable || operation == .arrayData) then .reference + if pointerDisplacementSite site || spanReferenceResult site then .reference else match site.target with | .managed _ => scalarType site.returnType | .external identity => externalResult site identity -/-- Exactly `newobj ReadOnlySpan(void*, int)`. -/ -private def byteSpanConstructor (opcode : Nat) (site : CallSite) : Bool := - opcode == 115 && site.name == ".ctor" && site.hasThis && !site.returnsValue && - site.typeArguments.isEmpty && site.parameterCount == 2 && site.returnType == .void && - site.declaringType == .apply (.constructor runtimeScope "System.ReadOnlySpan`1") - (.atom runtimeScope "System.Byte") && - site.parameterTypes == [.pointer .void, .atom runtimeScope "System.Int32"] && - site.target == .external (runtimeScope ++ - "::System.Void System.ReadOnlySpan`1::.ctor(System.Void*,System.Int32)") - -/-- Exact span pointer constructor, including owner scope and external signature. -/ -private def spanConstructor (opcode : Nat) (site : CallSite) : Option (Nat × Bool) := do - if opcode != 115 || site.name != ".ctor" || !site.hasThis || site.returnsValue || - !site.typeArguments.isEmpty || site.parameterCount != 2 || site.returnType != .void || - site.parameterTypes != [.pointer .void, .atom runtimeScope "System.Int32"] then none - else pure () - let .apply (.constructor _ owner) elementType := site.declaringType | none - let element ← spanElementName elementType - let .span width writable ← spanStorage site.declaringType | none - if site.target != .external (runtimeScope ++ "::System.Void " ++ owner ++ "<" ++ element ++ - ">::.ctor(System.Void*,System.Int32)") then none else some (width, writable) - -/-- Span constructors retain the exact element width and readonly capability. -/ -private def spanCreateMatches (instruction : RawInstruction) (emitted : Op) : Bool := - let modeled := match instruction.operand with - | .call site => byteSpanConstructor instruction.opcode site - | _ => false - if modeled then match emitted with | .memory .spanCreate => true | _ => false - else if let .call site := instruction.operand then - match spanConstructor instruction.opcode site with - | some (width, writable) => match emitted with - | .memory (.spanFromReference actual capability) => - spanConstructorMatches (.reference width writable) (.reference actual capability) - | _ => false - | none => match emitted with | .memory .spanCreate => false | _ => true - else match emitted with | .memory .spanCreate => false | _ => true - -/-- Contextual callable kinds retain the independently derived native home identity. -/ +/-- What a scalar site reads about the body's local slots: the declared type of each slot, and + whether a `calli` consumes the slot as a native method pointer. -/ +structure ScalarLocals where + typeAt : Nat → Option TypeExpr + pointer : Nat → Bool + +/-- The local slots as the raw metadata lists state them, scanned at each site. -/ +def ScalarLocals.ofLists (raw : RawMethod) (context : NativeDispatchContext) : ScalarLocals := + ⟨(raw.locals[·]?), context.pointerLocals.contains⟩ + +/-- The local slots read from a `FlowIndex` of the declared types and a `Std.TreeSet` of the + pointer locals, each built once per body. -/ +def ScalarLocals.indexed (raw : RawMethod) (context : NativeDispatchContext) : ScalarLocals := + ⟨(FlowIndex.ofList raw.locals).get?, (Std.TreeSet.ofList context.pointerLocals compare).contains⟩ + +/-- Indexes built once per body answer every local slot query exactly as the metadata lists do. -/ +theorem ScalarLocals.indexed_eq (raw : RawMethod) (context : NativeDispatchContext) : + ScalarLocals.indexed raw context = ScalarLocals.ofLists raw context := by + have typeAt : (FlowIndex.ofList raw.locals).get? = (raw.locals[·]?) := + funext (FlowIndex.get?_ofList raw.locals) + have pointer : (Std.TreeSet.ofList context.pointerLocals compare).contains = + context.pointerLocals.contains := by + funext index + rw [Std.TreeSet.contains_ofList] + rw [ScalarLocals.indexed, ScalarLocals.ofLists, typeAt, pointer] + +#print axioms ScalarLocals.indexed_eq + +/-- Contextual callable kinds retain the independently derived native home identity; `pointer` + holds the locals a `calli` consumes. -/ private def dispatchScalar (context : NativeDispatchContext) (type : TypeExpr) - (index : Option Nat := none) : ScalarKind := - match nativeDispatchStorage context type index with + (index : Option Nat := none) (pointer : Nat → Bool := context.pointerLocals.contains) : + ScalarKind := + match nativeDispatchStorage context type index pointer with | some (.methodTable signature) => .nativeMethodTable signature | some (.methodPointer signature) => .methodPointer signature | _ => scalarType type @@ -773,9 +677,10 @@ private def argumentKind (raw : RawMethod) if raw.hasThis && index == 0 then some (if raw.objectReceiver then .object else .reference) else dispatchScalar context <$> raw.parameters[index - (if raw.hasThis then 1 else 0)]? -/-- The kind an opcode with a fixed one-value push leaves on the stack. -/ +/-- The kind an opcode with a fixed one-value push leaves on the stack, reading local slots + through `locals`. -/ private def pushedKind (raw : RawMethod) - (context : NativeDispatchContext) + (context : NativeDispatchContext) (locals : ScalarLocals) (instruction : RawInstruction) : Option ScalarKind := do match argumentOperation instruction with | some (.load index) => argumentKind raw context index @@ -783,14 +688,14 @@ private def pushedKind (raw : RawMethod) -- `ldloc`. if instruction.opcode ∈ [6, 7, 8, 9, 17, 65036] then let index ← localIndex instruction - return dispatchScalar context (← raw.locals[index]?) (some index) + return dispatchScalar context (← locals.typeAt index) (some index) locals.pointer -- `ldarga`, `ldloca`, `ldflda` and `ldsflda`. else if instruction.opcode ∈ [18, 65037] then let index ← localIndex instruction - let type ← raw.locals[index]? + let type ← locals.typeAt index -- A class instance's slot is never addressed, so no byref stands in for a receiver. if type matches .object .. then none else - if (nativeDispatchStorage context type (some index)).isSome then none else + if (nativeDispatchStorage context type (some index) locals.pointer).isSome then none else return scalarType (.byref type) else if instruction.opcode ∈ [15, 65034] then match argumentOperation instruction with @@ -804,12 +709,12 @@ private def pushedKind (raw : RawMethod) return scalarType (.byref type) | _ => none else if instruction.opcode ∈ [124, 127] then some .reference - -- `ldfld` and `ldsfld`, then `ldobj`. + -- `ldfld` and `ldsfld`. else if instruction.opcode == 123 || instruction.opcode == 126 then - let .field _ _ type _ := instruction.operand | none - return scalarType type - else if instruction.opcode == 113 then - let .type type := instruction.operand | none + let .field owner _ type _ static := instruction.operand | none + if instruction.opcode == 123 && !static && owner matches .object .. && + opaqueFieldType type then + return .opaque return scalarType type -- `ldind.i`, `ldind.i8`, then `ldind.i1` through `ldind.u4` and `sizeof`. else if instruction.opcode == 77 then some .native @@ -817,10 +722,12 @@ private def pushedKind (raw : RawMethod) else if instruction.opcode ∈ [70, 71, 72, 73, 74, 75, 65052] then some .word32 else some .other -/-- The stack kinds after `instruction`, or `none` if its operands have the wrong kinds. -/ +/-- The stack kinds after `instruction`, with local slots read through `locals`, or `none` if + its operands have the wrong kinds. -/ def scalarTransfer (raw : RawMethod) (instruction : RawInstruction) (stack : List ScalarKind) (role : Option FoldRole := none) - (context : NativeDispatchContext := nativeDispatchContext raw) : Option (List ScalarKind) := do + (context : NativeDispatchContext := nativeDispatchContext raw) + (locals : ScalarLocals := .ofLists raw context) : Option (List ScalarKind) := do let opcode := instruction.opcode if role matches some (.typeBranch _) then let _ :: _ :: tail := stack | none @@ -828,6 +735,16 @@ def scalarTransfer (raw : RawMethod) (instruction : RawInstruction) if role matches some (.result _) then let _ :: _ :: tail := stack | none return .word32 :: tail + if opcode == 0x71 || opcode == 0x81 then + let .type type := instruction.operand | none + let home := scalarType (.byref type) + let value := scalarType type + if opcode == 0x71 then + let address :: rest := stack | none + if address == home then return value :: rest else none + else + let stored :: address :: rest := stack | none + if address == home && storageCompatible stored value then return rest else none -- A literal outside a folded throw is an object; a folded throw's message is never a value. if opcode == 114 && role == none then let .text _ := instruction.operand | none @@ -871,18 +788,19 @@ def scalarTransfer (raw : RawMethod) (instruction : RawInstruction) if token matches .object .. then return .object :: tail else let element ← arrayElementType token return .array element :: tail - if arrayCovered opcode then return (← arrayStep instruction stack).2 + if arrayCovered opcode then return (← arrayStep instruction stack) -- `castclass` and `isinst` of a class leave an object reference. if opcode == 116 || opcode == 117 then match instruction.operand, stack with | .type (.object ..), value :: tail => - if value ∈ [.object, .nullObject] then return .object :: tail + if value ∈ [.object, .opaque, .nullObject] then return .object :: tail | _, _ => pure () if opcode == 20 then if instruction.operand == .none then return .nullObject :: stack else none if opcode ∈ [44, 45, 57, 58] then let value :: tail := stack | none - if numeric value || value ∈ [.reference, .nullObject, .object] then return tail else none + if numeric value || value ∈ [.reference, .nullObject, .object, .opaque] then return tail + else none if compareBranch opcode then let right :: left :: tail := stack | none if compatibleBinary left right || referenceComparison opcode left right then return tail else none @@ -915,9 +833,11 @@ def scalarTransfer (raw : RawMethod) (instruction : RawInstruction) -- Stores to locals and arguments must fit the slot. if opcode ∈ [10, 11, 12, 13, 19, 65038] then let index ← localIndex instruction - let type ← raw.locals[index]? + let type ← locals.typeAt index let value :: tail := stack | none - if storageCompatible value (dispatchScalar context type (some index)) then return tail else none + if storageCompatible value (dispatchScalar context type (some index) locals.pointer) then + return tail + else none if let some (.store index) := argumentOperation instruction then let expected ← argumentKind raw context index let value :: tail := stack | none @@ -929,15 +849,29 @@ def scalarTransfer (raw : RawMethod) (instruction : RawInstruction) -- `call` and `newobj`: the operands must have the signature's kinds. if opcode == 40 || opcode == 111 || opcode == 115 then let .call site := instruction.operand | none - let expected := if let some (copy, _) := unsafeBlockCall instruction then - [.word32, if copy then .reference else .word32, .reference] else - if let some (type, read, _) := unsafeValueCall instruction then - (if read then [] else [scalarType type]) ++ [.reference] else - if unsafeReferenceHelper instruction == some .asRef then [.reference] else - if objectConstructorCall instruction then [.object] else - if nullGuardCall instruction then [.object, .object] else - if unsafePointerOffset instruction then [.word32, .reference] else - if (opcode == 115 || opcode == 40) && (spanConstructor 115 site).isSome + -- Block helpers consume allocation-relative references in their void-pointer slots. + let pointerModel := if opcode == 40 && site.parameterTypes.head? == some (.pointer .void) + then runtimeModel {} .call site.method else none + let expected := if pointerModel matches some (.memory (.copyBlock _)) then + [.word32, .reference, .reference] else + if pointerModel matches some (.memory (.initBlock _)) then + [.word32, .word32, .reference] else + if opcode == 40 && site.parameterTypes.head? == some (.pointer .void) && + unsafeTransferEntries.any (fun entry => entry.name == site.method.name && + entry.bindsSite site) then + site.parameterTypes.tail.reverse.map scalarType ++ [.reference] + else + -- AsRef takes a formed reference even at its void-pointer overload. + if opcode == 40 && site.method.name == name!"AsRef" && + site.declaringType == .atom runtimeScope "System.Runtime.CompilerServices.Unsafe" && + site.parameterTypes == [.pointer .void] then [.reference] else + if opcode == 40 && site.method.name == nullGuardEntry.name && + site.method.owner == nullGuardEntry.owner && nullGuardEntry.bindsSite site + then [.object, .object] else + if opcode == 40 && pointerDisplacementSite site then [.word32, .reference] else + if (opcode == 115 || opcode == 40) && site.name == ".ctor" && + site.parameterTypes.head? == some (.pointer .void) && + spanConstructorEntries.any (·.bindsSite site) then [.word32, .reference] ++ (if opcode == 40 then [scalarType (.byref site.declaringType)] else []) else site.parameterTypes.reverse.map scalarType ++ @@ -968,65 +902,61 @@ def scalarTransfer (raw : RawMethod) (instruction : RawInstruction) let (pops, pushes) ← rawStackEffect opcode if pops > stack.length then none else if pushes == 0 then return stack.drop pops - else if pushes == 1 then return (← pushedKind raw context instruction) :: stack.drop pops + else if pushes == 1 then + return (← pushedKind raw context locals instruction) :: stack.drop pops else none /-- A `stloc` of a 64-bit or native value into a 32-bit local, emitted as a store to that - same local. The checked interpreter rejects that store, so the site needs no successor. -/ + same local, whose type `locals` gives. The checked interpreter rejects that store, so the + site needs no successor. -/ def scalarStoreFault (raw : RawMethod) (instruction : RawInstruction) - (stack : List ScalarKind) (emitted : Op) : Bool := + (stack : List ScalarKind) (emitted : Instruction) + (locals : ScalarLocals := .ofLists raw (nativeDispatchContext raw)) : Bool := ((do if instruction.opcode ∉ [10, 11, 12, 13, 19, 65038] then none else let index ← localIndex instruction - let type ← raw.locals[index]? + let type ← locals.typeAt index let value :: _ := stack | none return scalarType type == .word32 && value ∈ [.word64, .native] && match emitted with - | .setLocal actual _ => actual == index + | .stloc actual => actual == index | _ => false) : Option Bool).getD false -/-- Whether the immediately preceding raw instruction is a readonly prefix. -/ -private def followsReadOnly (raw : RawMethod) (pc : Nat) : Bool := - pc > 0 && (raw.code[pc - 1]?).any (fun instruction => instruction.opcode == 65054) +/-- A `readonly.` site requires the raw instruction after it, `next`, to be `ldelema` (143), so a + `readonly.` that ends the body is refused. This is the rule that only `ldelema` follows + `readonly.`: a reachable `readonly.` falls through to the instruction after it, which + `PrefixEntryFree` keeps every branch out of. Within the scalar check, `invalidPredecessor` + at the follower and the successor rule refuse the same sites. -/ +def readOnlyPrefixMatches (next : Option RawInstruction) (instruction : RawInstruction) : Bool := + instruction.opcode != 65054 || next.any (prefixTakes instruction) -/-- A readonly prefix is inseparable from its element address, including branch entry. -/ -private def readOnlySiteMatches (raw : RawMethod) (pc : Nat) (instruction : RawInstruction) - (emitted : Op) : Bool := - let entered (target : Nat) := raw.code.any fun source => match source.operand with - | .target destination => destination == target - | .targets destinations => destinations.contains target - | _ => false - if instruction.opcode == 65054 then - instruction.operand == .none && - (raw.code[pc + 1]?).any (fun next => next.opcode == 143) && !entered (pc + 1) - else if instruction.opcode == 143 then - match emitted with - | .memory (.arrayAddress _ writable) => - writable == !followsReadOnly raw pc && (!followsReadOnly raw pc || !entered pc) - | _ => false - else true +/-- Only ordinary, unfolded, non-hole sites can reuse the raw value decoder's comparison. -/ +private def decodedValueSite (raw : RawInstruction) (role : Option FoldRole) + (emitted : Instruction) : Bool := + !boundSite raw && role.isNone && !(emitted matches .unadmitted _) /-- One pc of the certificate: an unreachable pc must emit a hole; a reachable one must - agree with the feature stack depth, emit the right encoding, and give every successor, - which must be reachable, exactly the resulting stack kinds. -/ -private def checkScalarSite (layouts : Array RawLayout) (raw : RawMethod) - (context : NativeDispatchContext) + agree with the feature stack depth, satisfy the indirect access rule and the `readonly.` rule + (`readOnlyPrefixMatches`) over the raw instructions `rawAt` gives, emit the right encoding + after the raw instruction before it, and give every successor, which must be reachable, + exactly the resulting stack kinds, with local slots read through `locals`. An ordinary + value site skips the raw value comparison, which the decoded code equation implies. -/ +private def checkScalarSite (raw : RawMethod) + (context : NativeDispatchContext) (locals : ScalarLocals) (rawAt : Nat → Option RawInstruction) (successor : List ScalarKind → Nat → Bool) : (RawInstruction × Option FoldRole) × Nat → Option FeatureState → Option (List ScalarKind) → - Op → Bool - | _, none, none, .unsupported _ => true + Instruction → Bool + | _, none, none, .unadmitted _ => true | ((instruction, role), pc), some feature, some stack, emitted => + let skipEncoding := decodedValueSite instruction role emitted stack.length == feature.stack.length && stack.length <= raw.maxStack && - (match role with - | some (.spanConstructor index constructor) => match emitted with - | .initializeSpanLocal actual operation => index == actual && spanConstructorMatches constructor operation - | _ => false - | _ => spanCreateMatches instruction emitted && spanCallMatches instruction emitted) && - readOnlySiteMatches raw pc instruction emitted && - scalarEmissionMatches layouts role instruction stack emitted && - (scalarStoreFault raw instruction stack emitted || - match scalarTransfer raw instruction stack role context, - featureSuccessors role pc instruction feature with + indirectSiteWindow rawAt pc instruction emitted skipEncoding && + readOnlyPrefixMatches (rawAt (pc + 1)) instruction && + scalarEmissionMatches role (rawBefore rawAt pc) instruction stack emitted + skipEncoding && + (scalarStoreFault raw instruction stack emitted locals || + match scalarTransfer raw instruction stack role context locals, + featureSuccessors role pc instruction feature.stack with | some after, some successors => successors.all (successor after) | _, _ => false) @@ -1034,22 +964,26 @@ private def checkScalarSite (layouts : Array RawLayout) (raw : RawMethod) /-- The scalar certificate covers exactly the feature-reachable pcs, starts with an empty stack, and passes every site check. -/ -def ScalarFlowValid (layouts : Array RawLayout) (raw : RawMethod) (features : FeatureCertificate) - (certificate : ScalarCertificate) (code : List Op) : Prop := +def ScalarFlowValid (raw : RawMethod) (features : FeatureCertificate) + (certificate : ScalarCertificate) (code : List Instruction) : Prop := let context := nativeDispatchContext raw + let locals := ScalarLocals.indexed raw context + let rawAt := (FlowIndex.ofList raw.code).get? raw.code.length = features.length ∧ raw.code.length = certificate.length ∧ raw.code.length = code.length ∧ certificate[0]? = some (some []) ∧ all3Flow (fun lookup instruction (feature, stack) emitted => - checkScalarSite layouts raw context (fun after target => match lookup target with - | some (some _, incoming) => incoming == some after - | _ => false) instruction feature stack emitted) + checkScalarSite raw context locals rawAt + (fun after target => match lookup target with + | some (some _, incoming) => incoming == some after + | _ => false) instruction feature stack emitted) (.ofList (features.zip certificate)) 0 - (raw.code.zip (foldRoles raw.locals raw.code)).zipIdx (features.zip certificate) code = true + (raw.code.zip (foldRoles raw.code)).zipIdx (features.zip certificate) code = true -instance (layouts : Array RawLayout) (raw : RawMethod) (features : FeatureCertificate) +instance (raw : RawMethod) (features : FeatureCertificate) (certificate : ScalarCertificate) - (code : List Op) : Decidable (ScalarFlowValid layouts raw features certificate code) := + (code : List Instruction) : + Decidable (ScalarFlowValid raw features certificate code) := inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _)) /-- Pairing feature and scalar states preserves both the reachability and exact stack @@ -1073,23 +1007,29 @@ theorem scalarSuccessor_zip (features : FeatureCertificate) #print axioms scalarSuccessor_zip -/-- Streaming scalar validation preserves all list-indexed site checks and lengths. -/ -theorem ScalarFlowValid_iff_list (layouts : Array RawLayout) (raw : RawMethod) +/-- Streaming scalar validation preserves all list-indexed site checks and lengths, with local + slots read from the metadata lists at each site. -/ +theorem ScalarFlowValid_iff_list (raw : RawMethod) (features : FeatureCertificate) - (certificate : ScalarCertificate) (code : List Op) : - ScalarFlowValid layouts raw features certificate code ↔ + (certificate : ScalarCertificate) (code : List Instruction) : + ScalarFlowValid raw features certificate code ↔ raw.code.length = features.length ∧ raw.code.length = certificate.length ∧ raw.code.length = code.length ∧ certificate[0]? = some (some []) ∧ all3 (fun (instruction, feature) stack emitted => - checkScalarSite layouts raw (nativeDispatchContext raw) + checkScalarSite raw (nativeDispatchContext raw) + (ScalarLocals.ofLists raw (nativeDispatchContext raw)) + (fun target => raw.code[target]?) (fun after target => match features[target]? with | some (some _) => certificate[target]? == some (some after) | _ => false) instruction feature stack emitted) - ((raw.code.zip (foldRoles raw.locals raw.code)).zipIdx.zip features) certificate code = + ((raw.code.zip (foldRoles raw.code)).zipIdx.zip features) certificate code = true := by dsimp only [ScalarFlowValid] - rw [all3Flow_eq _ _ _ _ _ _ List.drop_zero.symm] + have rawAt : (FlowIndex.ofList raw.code).get? = (fun target => raw.code[target]?) := + funext (FlowIndex.get?_ofList raw.code) + rw [ScalarLocals.indexed_eq, rawAt, + all3Flow_eq _ _ _ _ _ _ List.drop_zero.symm] simp only [scalarSuccessor_zip] by_cases featureLength : raw.code.length = features.length · by_cases scalarLength : raw.code.length = certificate.length @@ -1101,19 +1041,90 @@ theorem ScalarFlowValid_iff_list (layouts : Array RawLayout) (raw : RawMethod) #print axioms ScalarFlowValid_iff_list -/-- At every reachable pc the emitted instruction is the scalar encoding the raw one implies. -/ -theorem ScalarFlowValid.instruction_checked {layouts : Array RawLayout} {raw : RawMethod} - {features : FeatureCertificate} - {certificate : ScalarCertificate} {code : List Op} - (valid : ScalarFlowValid layouts raw features certificate code) +/-- At an ordinary site whose emitted instruction is its decoding, the skipped raw value + comparison would succeed, so the scalar check equals the one that compares it. -/ +private theorem decodedEmission_eq (role : Option FoldRole) (previous : Option RawInstruction) + (instruction : RawInstruction) (stack : List ScalarKind) (emitted : Instruction) + (same : boundSite instruction = false → role = none → + emitted = Instruction.ofRaw previous instruction ∨ + emitted = .unadmitted instruction.opcode) : + scalarEmissionMatches role previous instruction stack emitted + (decodedValueSite instruction role emitted) = + scalarEmissionMatches role previous instruction stack emitted := by + have redundant : + (decodedValueSite instruction role emitted || + valueEncodingMatches previous instruction emitted) = + valueEncodingMatches previous instruction emitted := by + by_cases called : boundSite instruction = true + · simp [decodedValueSite, called] + by_cases unfolded : role = none + case neg => cases role <;> simp_all [decodedValueSite] + obtain same | hole := same (Bool.eq_false_iff.mpr called) unfolded + case inr => simp [hole, decodedValueSite] + cases partialDecode : Instruction.ofRaw? previous instruction with + | none => + simp [Instruction.ofRaw, partialDecode] at same + subst emitted + simp [decodedValueSite] + | some value => + rw [Instruction.ofRaw_of_ofRaw? partialDecode] at same + subst emitted + simp [valueEncodingMatches, partialDecode, + Instruction.sameValue_self_of_ofRaw? partialDecode] + delta scalarEmissionMatches scalarOpMatches arrayMatches classFieldMatches structFieldMatches + readOnlyDecodedMatches + simp only [redundant, Bool.false_or] + +#print axioms decodedEmission_eq + +/-- Code that is the decoding of the raw body emits, at each ordinary live site, the value + decoding of the raw instruction after its predecessor, and a hole at a dead one. -/ +private theorem decodedSite_same {raw : RawMethod} {code : List Instruction} + {live : Nat → Bool} {decodeSite : SiteDecoder} + (same : code = Instruction.ofRawCode none raw.code + (Instruction.withSiteDecoder (foldRoles raw.code).toArray live decodeSite)) + {pc : Nat} {instruction : RawInstruction} {role : Option FoldRole} {emitted : Instruction} + (rawAt : raw.code[pc]? = some instruction) + (roleAt : (foldRoles raw.code)[pc]? = some role) (emittedAt : code[pc]? = some emitted) : + boundSite instruction = false → role = none → + emitted = Instruction.ofRaw (rawBefore (raw.code[·]?) pc) instruction ∨ + emitted = .unadmitted instruction.opcode := by + intro ordinary unfolded + rw [same, Instruction.ofRawCode_getElem? raw.code pc _, Instruction.ofRawAt, rawAt] + at emittedAt + rw [unfolded] at roleAt + cases active : live pc with + | false => + exact Or.inr (by simpa [Instruction.withSiteDecoder, active] using + (Option.some.inj emittedAt).symm) + | true => + have closed : emitted = Instruction.ofClosedValue + (rawBefore (FlowIndex.ofList raw.code).get? pc) instruction := by + simpa [Instruction.withSiteDecoder, active, ordinary, roleAt, rawBefore, + FlowIndex.get?_ofList] using (Option.some.inj emittedAt).symm + have flow : (FlowIndex.ofList raw.code).get? = (fun target => raw.code[target]?) := + funext (FlowIndex.get?_ofList raw.code) + rw [flow] at closed + unfold Instruction.ofClosedValue at closed + repeat' split at closed + all_goals first | exact Or.inl closed | exact Or.inr closed + +#print axioms decodedSite_same + +/-- At every reachable pc the certificate passes the scalar emission rule, with the raw value + comparison skipped at an ordinary site. -/ +private theorem ScalarFlowValid.emission_at {raw : RawMethod} {features : FeatureCertificate} + {certificate : ScalarCertificate} {code : List Instruction} + (valid : ScalarFlowValid raw features certificate code) {pc : Nat} {instruction : RawInstruction} {role : Option FoldRole} {feature : FeatureState} - {stack : List ScalarKind} {emitted : Op} + {stack : List ScalarKind} {emitted : Instruction} (rawAt : raw.code[pc]? = some instruction) - (roleAt : (foldRoles raw.locals raw.code)[pc]? = some role) + (roleAt : (foldRoles raw.code)[pc]? = some role) (featureAt : features[pc]? = some (some feature)) (stackAt : certificate[pc]? = some (some stack)) (emittedAt : code[pc]? = some emitted) : - scalarEmissionMatches layouts role instruction stack emitted = true := by - have pairAt : (raw.code.zip (foldRoles raw.locals raw.code)).zipIdx[pc]? = + scalarEmissionMatches role (rawBefore (raw.code[·]?) pc) instruction stack emitted + (decodedValueSite instruction role emitted) = true := by + have pairAt : (raw.code.zip (foldRoles raw.code)).zipIdx[pc]? = some ((instruction, role), pc) := by simp [List.getElem?_zipIdx, List.getElem?_zip_eq_some, rawAt, roleAt] have statesAt : (features.zip certificate)[pc]? = some (some feature, some stack) := @@ -1122,8 +1133,152 @@ theorem ScalarFlowValid.instruction_checked {layouts : Array RawLayout} {raw : R rw [all3Flow_eq _ _ _ _ _ _ List.drop_zero.symm] at sites have checked := all3_at sites pairAt statesAt emittedAt simp only [checkScalarSite, Bool.and_eq_true] at checked - exact checked.1.2 + simpa only [rawBefore, FlowIndex.get?_ofList] using checked.1.2 + +#print axioms ScalarFlowValid.emission_at + +/-- At every reachable pc of code that is the decoding of the raw body, the emitted + instruction is the scalar encoding the raw one implies after the raw instruction before it. -/ +theorem ScalarFlowValid.instruction_checked {raw : RawMethod} + {features : FeatureCertificate} + {certificate : ScalarCertificate} {code : List Instruction} + (valid : ScalarFlowValid raw features certificate code) + {live : Nat → Bool} {decodeSite : SiteDecoder} + (codeDecodes : code = Instruction.ofRawCode none raw.code + (Instruction.withSiteDecoder (foldRoles raw.code).toArray live decodeSite)) + {pc : Nat} {instruction : RawInstruction} {role : Option FoldRole} {feature : FeatureState} + {stack : List ScalarKind} {emitted : Instruction} + (rawAt : raw.code[pc]? = some instruction) + (roleAt : (foldRoles raw.code)[pc]? = some role) + (featureAt : features[pc]? = some (some feature)) + (stackAt : certificate[pc]? = some (some stack)) (emittedAt : code[pc]? = some emitted) : + scalarEmissionMatches role (rawBefore (raw.code[·]?) pc) instruction stack emitted = + true := by + rw [← decodedEmission_eq _ _ _ _ _ (decodedSite_same codeDecodes rawAt roleAt emittedAt)] + exact valid.emission_at rawAt roleAt featureAt stackAt emittedAt #print axioms ScalarFlowValid.instruction_checked +/-- At every reachable folded pc the emitted instruction is the scalar encoding the raw one + implies after the raw instruction before it; a folded site never skips the comparison. -/ +theorem ScalarFlowValid.folded_checked {raw : RawMethod} + {features : FeatureCertificate} + {certificate : ScalarCertificate} {code : List Instruction} + (valid : ScalarFlowValid raw features certificate code) + {pc : Nat} {instruction : RawInstruction} {role : FoldRole} {feature : FeatureState} + {stack : List ScalarKind} {emitted : Instruction} + (rawAt : raw.code[pc]? = some instruction) + (roleAt : (foldRoles raw.code)[pc]? = some (some role)) + (featureAt : features[pc]? = some (some feature)) + (stackAt : certificate[pc]? = some (some stack)) (emittedAt : code[pc]? = some emitted) : + scalarEmissionMatches (some role) (rawBefore (raw.code[·]?) pc) instruction stack emitted = + true := by + have checked := valid.emission_at rawAt roleAt featureAt stackAt emittedAt + simpa [decodedValueSite] using checked + +#print axioms ScalarFlowValid.folded_checked + +/-- At a reachable pc the certificate's stack passes the raw instruction's scalar transfer, or + the site is a narrowing store the interpreter rejects, and every successor's certificate + entry is exactly the transferred stack. -/ +theorem ScalarFlowValid.transfer_at {raw : RawMethod} {features : FeatureCertificate} + {certificate : ScalarCertificate} {code : List Instruction} + (valid : ScalarFlowValid raw features certificate code) + {pc : Nat} {instruction : RawInstruction} {role : Option FoldRole} + {feature : Option FeatureState} {stack : List ScalarKind} {emitted : Instruction} + (rawAt : raw.code[pc]? = some instruction) + (roleAt : (foldRoles raw.code)[pc]? = some role) + (featureAt : features[pc]? = some feature) + (stackAt : certificate[pc]? = some (some stack)) (emittedAt : code[pc]? = some emitted) : + ∃ state, feature = some state ∧ (scalarStoreFault raw instruction stack emitted = true ∨ + ∃ after successors, scalarTransfer raw instruction stack role = some after ∧ + featureSuccessors role pc instruction state.stack = some successors ∧ + ∀ target ∈ successors, certificate[target]? = some (some after)) := by + have sites := ((ScalarFlowValid_iff_list raw features certificate code).mp valid).2.2.2.2 + have pairAt : ((raw.code.zip (foldRoles raw.code)).zipIdx.zip features)[pc]? = + some (((instruction, role), pc), feature) := by + simp [List.getElem?_zipIdx, List.getElem?_zip_eq_some, rawAt, roleAt, featureAt] + have checked := all3_at sites pairAt stackAt emittedAt + cases feature with + | none => simp [checkScalarSite] at checked + | some state => + refine ⟨state, rfl, ?_⟩ + simp only [checkScalarSite, Bool.and_eq_true, Bool.or_eq_true] at checked + rcases checked.2 with fault | transfer + · exact Or.inl fault + · right + split at transfer + · rename_i after successors transferred successorsAt + refine ⟨after, successors, transferred, successorsAt, fun target member => ?_⟩ + have reached := List.all_eq_true.mp transfer target member + split at reached + · exact beq_iff_eq.mp reached + · cases reached + · cases transfer + +#print axioms ScalarFlowValid.transfer_at + +/-- At a live folded pc in a prefix-valid literal, the decoder selects its certified + instruction. The decoded operation passes the feature and scalar checks at that pc. + The managed and constrained call certificates bind a direct tail call to its raw operand. -/ +theorem fold_decoding_agreement {layouts : Array RawLayout} {raw : RawMethod} + {constants : Nat → Option W32} {linkAt : Nat → Option Link} {profile : FeatureProfile} + {features : FeatureCertificate} {reachable : List Bool} {body : Method} + {scalars : ScalarCertificate} {types : List DispatchType} {methods : List SpecializedMethod} + {imports : List ImportSlot} {modelled : CallOpcode × MethodRef → Bool} + (prefixes : rawPrefixesValid raw.returnType raw.code = true) + (featureFlow : FeatureFlowValid layouts constants linkAt raw profile features reachable + body.code) + (scalarFlow : ScalarFlowValid raw features scalars body.code) + (calls : ManagedCallsValid methods imports modelled (lowerConstrainedRaw types raw) + reachable body) + (constrained : ConstrainedCallsValid types methods raw reachable body) + {pc : Nat} {instruction : RawInstruction} {role : FoldRole} {state : FeatureState} + {stack : List ScalarKind} {emitted : Instruction} + (rawAt : raw.code[pc]? = some instruction) + (roleAt : (foldRoles raw.code)[pc]? = some (some role)) + (featureAt : features[pc]? = some (some state)) (stackAt : scalars[pc]? = some (some stack)) + (codeAt : body.code[pc]? = some emitted) + (supported : match role with + | .blocked => False + | .tailCall => ∃ prefixOffset callOffset site token target, + rawBefore (raw.code[·]?) pc = some ⟨prefixOffset, 0xFE14, .none⟩ ∧ + instruction = ⟨callOffset, 40, .call site⟩ ∧ site.target = .managed token ∧ + body.linkAt pc = some (.direct target) + | _ => True) : + let previous := rawBefore (raw.code[·]?) pc + invalidPredecessor previous instruction = false ∧ + Instruction.ofRole? role previous instruction = some emitted ∧ + ∀ decoded, Instruction.ofRole? role previous instruction = some decoded → + featureOpMatchesAt layouts raw (some role) instruction decoded previous = true ∧ + scalarEmissionMatches (some role) previous instruction stack decoded = true := by + have feature := featureFlow.instruction_checked rawAt roleAt featureAt codeAt + have scalar := scalarFlow.folded_checked rawAt roleAt featureAt stackAt codeAt + have bound : Instruction.ofRole? role (rawBefore (raw.code[·]?) pc) instruction = + some emitted := by + cases role with + | argument => exact argument_of_featureOpMatchesAt feature + | tailCall => + obtain ⟨_, _, _, _, _, tailAt, rfl, managed, linked⟩ := supported + have live : reachable[pc]? = some true := by + rw [featureFlow.2.2.1] + simp [List.getElem?_map, featureAt] + exact (calls.tailCall_target constrained rawAt tailAt codeAt live feature managed linked).1 + | blocked => exact supported.elim + | head | interior | typeHead | typeOperand | result | raise | typeBranch => + exact (fixedRole_matches _ _ _ _ _ _ trivial).mp feature + refine ⟨?_, bound, ?_⟩ + · cases pc with + | zero => rfl + | succ index => + simp only [rawBefore, Nat.add_one_sub_one, beq_iff_eq, Nat.succ_ne_zero, if_false] + cases before : raw.code[index]? with + | none => rfl + | some modifier => exact rawPrefixesValid_predecessor prefixes before rawAt + · intro decoded found + cases Option.some.inj (bound.symm.trans found) + exact ⟨feature, scalar⟩ + +#print axioms fold_decoding_agreement + end CIL.Extraction diff --git a/CIL/Extraction/SearchTreeLiteral.lean b/CIL/Extraction/SearchTreeLiteral.lean new file mode 100644 index 000000000..e1a98af3f --- /dev/null +++ b/CIL/Extraction/SearchTreeLiteral.lean @@ -0,0 +1,62 @@ +import CIL.Extraction.MetadataBytes + +/-! +# Literal search trees + +A table read by many kernel decisions is written out once as a literal `SearchTree` whose +leaves hold its entry terms, grouped by a number key, so a lookup costs the depth of the tree and +the comparison within one group, never the size of the table, and the kernel never sorts or +splits the entries. `SearchTree.groupedLiteral` builds that literal for an elaborator, and +`SearchTree.find?_mem_toList` lets a proof read a found group as one of the tree's entries +whatever order the tree holds. +-/ + +namespace CIL.Extraction + +open Lean Meta in +/-- The literal balanced tree of key-ordered groups of `entryType` terms, split as + `SearchTree.ofSorted` splits. -/ +private partial def literalTree (entryType : Expr) (groups : Array (Nat × Array Expr)) : + MetaM Expr := do + let valueType := mkApp (mkConst ``List [0]) entryType + if groups.isEmpty then return mkApp (mkConst ``SearchTree.leaf) valueType + let leftCount := groups.size / 2 + let (key, cells) := groups[leftCount]! + return mkApp5 (mkConst ``SearchTree.node) valueType + (← literalTree entryType (groups.extract 0 leftCount)) (mkNatLit key) + (← mkListLit entryType cells.toList) + (← literalTree entryType (groups.extract (leftCount + 1) groups.size)) + +open Lean Meta in +/-- The literal `SearchTree (List entryType)` of `cells` grouped under `keys`, one key per cell: + groups in ascending key order, each group's cells in list order. -/ +def SearchTree.groupedLiteral (entryType : Expr) (cells : Array Expr) (keys : Array Nat) : + MetaM Expr := do + unless cells.size == keys.size do throwError "search tree: one key is needed per entry" + let mut groups : Array (Nat × Array Expr) := #[] + for (key, cell) in (keys.zip cells).toList.mergeSort fun left right => left.1 ≤ right.1 do + match groups.back? with + | some (last, group) => + groups := if last = key then groups.pop.push (key, group.push cell) + else groups.push (key, #[cell]) + | none => groups := groups.push (key, #[cell]) + literalTree entryType groups + +/-- A value `find?` gives is one of the tree's entries. -/ +theorem SearchTree.find?_mem_toList {α : Type} : + ∀ (tree : SearchTree α) (key : Nat) (value : α), tree.find? key = some value → + (key, value) ∈ tree.toList + | .leaf, _, _, found => by simp [SearchTree.find?] at found + | .node left root stored right, key, value, found => by + simp only [SearchTree.find?] at found + simp only [SearchTree.toList, List.mem_append, List.mem_cons] + split at found + · exact .inl (SearchTree.find?_mem_toList left key value found) + · exact .inr (.inr (SearchTree.find?_mem_toList right key value found)) + · rename_i same + cases found + exact .inr (.inl (by rw [Nat.compare_eq_eq.mp same])) + +#print axioms SearchTree.find?_mem_toList + +end CIL.Extraction diff --git a/CIL/Extraction/Signatures.lean b/CIL/Extraction/Signatures.lean new file mode 100644 index 000000000..ba6a54762 --- /dev/null +++ b/CIL/Extraction/Signatures.lean @@ -0,0 +1,214 @@ +import CIL.Extraction.MetadataBytes + +/-! +# Signature blobs + +Method, field, local-variable, type and method-instantiation signatures of ECMA-335 II.23.2, +decoded from the bytes of a `#Blob` entry. Type tokens stay tokens of the slice the blob +comes from; `MetadataResolution` gives them names. Forms the model has no place for are refused +with the offset and the element byte: general arrays, `typedref`, the vararg sentinel, +pinned locals, vararg calling conventions and trailing bytes. + +Recursion is on a fuel equal to the blob length, which bounds the node count since every node +takes at least one byte. +-/ + +namespace CIL.Extraction + +mutual + +/-- A signature type, with type tokens as the blob writes them. -/ +inductive SigType where + /-- An element type of II.23.1.16 that names no token: `void`, the integers, `string`, + `object` and the native integers. -/ + | primitive (code : Nat) + /-- A `CLASS` or `VALUETYPE` element with the token of its `TypeDefOrRef` encoding. -/ + | named (valueType : Bool) (token : Nat) + | var (index : Nat) + | mvar (index : Nat) + | byref (element : SigType) + | pointer (element : SigType) + | szarray (element : SigType) + | genericInst (valueType : Bool) (token : Nat) (arguments : SigList) + | functionPointer (convention : Nat) (result : SigType) (parameters : SigList) + | modified (required : Bool) (modifier : Nat) (element : SigType) + deriving DecidableEq, Repr + +/-- A list of signature types, as a chain so that the types derive decidable equality. -/ +inductive SigList where + | nil + | cons (head : SigType) (tail : SigList) + deriving DecidableEq, Repr + +end + +/-- The types of a chain, in order. -/ +def SigList.toList : SigList → List SigType + | .nil => [] + | .cons head tail => head :: tail.toList + +/-- A method signature: calling convention byte, generic arity, result and parameters. -/ +structure MethodSig where + convention : Nat + genericArity : Nat + result : SigType + parameters : List SigType + deriving DecidableEq, Repr + +/-- The element type bytes that name a type without a token. -/ +def primitiveCodes : List Nat := + [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x18, 0x19, + 0x1C] + +/-- The calling-convention flag of a method with a `this` argument. -/ +def hasThisFlag : Nat := 0x20 + +/-- The calling-convention flag of a method whose `this` argument is explicit. -/ +def explicitThisFlag : Nat := 0x40 + +/-- The calling-convention flag of a generic method, which is followed by its arity. -/ +def genericFlag : Nat := 0x10 + +/-- The offset in the blob of the byte at the front of the remaining bytes. -/ +private def offsetOf (total : Nat) (rest : List Nat) : Nat := total - rest.length + +/-- A compressed unsigned integer, or a truncated-signature error. -/ +private def compressed (total : Nat) (bytes : List Nat) : + Except MetadataError (Nat × List Nat) := + match decodeCompressed bytes with + | some result => .ok result + | none => .error (.truncatedSignature (offsetOf total bytes)) + +/-- The token of a `TypeDefOrRef` coded index at the front of the bytes. -/ +private def typeToken (total : Nat) (bytes : List Nat) : + Except MetadataError (Nat × List Nat) := do + let (value, rest) ← compressed total bytes + .ok (← decodeCoded typeDefOrRefTags 2 value, rest) + +/-- The calling-convention byte of a method signature. Its kind must be a managed or unmanaged + default (0 to 4), and the generic flag is allowed only where `generic` says so. -/ +def callingConvention (total : Nat) (generic : Bool) : + List Nat → Except MetadataError (Nat × List Nat) + | [] => .error (.truncatedSignature total) + | convention :: rest => + if convention % 0x10 > 4 || convention ≥ 0x80 || + (!generic && convention / genericFlag % 2 == 1) then + .error (.unsupportedSignature (offsetOf total rest - 1) convention) + else .ok (convention, rest) + +mutual + +/-- Decode one type at the front of the bytes, with the bytes after it. -/ +def decodeType (total : Nat) : Nat → List Nat → Except MetadataError (SigType × List Nat) + | 0, rest => .error (.unsupportedSignature (offsetOf total rest) 0) + | _ + 1, [] => .error (.truncatedSignature total) + | fuel + 1, first :: rest => + if primitiveCodes.contains first then .ok (.primitive first, rest) + else if first == 0x0F then do + let (element, tail) ← decodeType total fuel rest + .ok (.pointer element, tail) + else if first == 0x10 then do + let (element, tail) ← decodeType total fuel rest + .ok (.byref element, tail) + else if first == 0x1D then do + let (element, tail) ← decodeType total fuel rest + .ok (.szarray element, tail) + else if first == 0x11 || first == 0x12 then do + let (token, tail) ← typeToken total rest + .ok (.named (first == 0x11) token, tail) + else if first == 0x13 || first == 0x1E then do + let (index, tail) ← compressed total rest + .ok (if first == 0x13 then .var index else .mvar index, tail) + else if first == 0x1F || first == 0x20 then do + let (modifier, afterModifier) ← typeToken total rest + let (element, tail) ← decodeType total fuel afterModifier + .ok (.modified (first == 0x1F) modifier element, tail) + else if first == 0x15 then + match rest with + | kind :: afterKind => + if kind != 0x11 && kind != 0x12 then + .error (.unsupportedSignature (offsetOf total rest) kind) + else do + let (token, afterToken) ← typeToken total afterKind + let (count, afterCount) ← compressed total afterToken + let (arguments, tail) ← decodeTypes total fuel count afterCount + .ok (.genericInst (kind == 0x11) token arguments, tail) + | [] => .error (.truncatedSignature total) + else if first == 0x1B then do + let (convention, afterConvention) ← callingConvention total false rest + let (count, afterCount) ← compressed total afterConvention + let (result, afterResult) ← decodeType total fuel afterCount + let (parameters, tail) ← decodeTypes total fuel count afterResult + .ok (.functionPointer convention result parameters, tail) + else .error (.unsupportedSignature (offsetOf total (first :: rest)) first) + +/-- Decode `count` types in a row. -/ +def decodeTypes (total : Nat) : Nat → Nat → List Nat → + Except MetadataError (SigList × List Nat) + | _, 0, rest => .ok (.nil, rest) + | 0, _ + 1, rest => .error (.unsupportedSignature (offsetOf total rest) 0) + | fuel + 1, count + 1, rest => do + let (head, afterHead) ← decodeType total fuel rest + let (tail, remaining) ← decodeTypes total fuel count afterHead + .ok (.cons head tail, remaining) + +end + +/-- A method signature: the convention, the generic arity when the generic flag is set, the + parameter count, the result and the parameters. Nothing may follow the last parameter. -/ +def decodeMethodSig (blob : List Nat) : Except MetadataError MethodSig := do + let total := blob.length + let (convention, afterConvention) ← callingConvention total true blob + let (arity, afterArity) ← + if convention / genericFlag % 2 == 1 then compressed total afterConvention + else pure (0, afterConvention) + let (count, afterCount) ← compressed total afterArity + let (result, afterResult) ← decodeType total total afterCount + let (parameters, rest) ← decodeTypes total total count afterResult + if rest.isEmpty then + .ok { convention, genericArity := arity, result, parameters := parameters.toList } + else .error (.unsupportedSignature (offsetOf total rest) 0x100) + +/-- Nothing may follow a decoded signature. -/ +private def finished {α : Type} (total : Nat) (value : α) (rest : List Nat) : + Except MetadataError α := + if rest.isEmpty then .ok value else .error (.unsupportedSignature (offsetOf total rest) 0x100) + +/-- A field signature: `FIELD` and then the field's type. -/ +def decodeFieldSig (blob : List Nat) : Except MetadataError SigType := + match blob with + | 0x06 :: rest => do + let (type, tail) ← decodeType blob.length blob.length rest + finished blob.length type tail + | first :: _ => .error (.unsupportedSignature 0 first) + | [] => .error (.truncatedSignature 0) + +/-- A local-variable signature: `LOCAL_SIG`, the count and one type per local. A pinned local + (`0x45`) is refused as an unsupported element. -/ +def decodeLocalSig (blob : List Nat) : Except MetadataError (List SigType) := + match blob with + | 0x07 :: rest => do + let total := blob.length + let (count, afterCount) ← compressed total rest + let (types, tail) ← decodeTypes total total count afterCount + finished total types.toList tail + | first :: _ => .error (.unsupportedSignature 0 first) + | [] => .error (.truncatedSignature 0) + +/-- A `TypeSpec` blob: one type. -/ +def decodeTypeSpec (blob : List Nat) : Except MetadataError SigType := do + let (type, tail) ← decodeType blob.length blob.length blob + finished blob.length type tail + +/-- A `MethodSpec` blob: `GENERICINST`, the count and the type arguments. -/ +def decodeMethodSpec (blob : List Nat) : Except MetadataError (List SigType) := + match blob with + | 0x0A :: rest => do + let total := blob.length + let (count, afterCount) ← compressed total rest + let (types, tail) ← decodeTypes total total count afterCount + finished total types.toList tail + | first :: _ => .error (.unsupportedSignature 0 first) + | [] => .error (.truncatedSignature 0) + +end CIL.Extraction diff --git a/CIL/Extraction/SpanBulkModels.lean b/CIL/Extraction/SpanBulkModels.lean new file mode 100644 index 000000000..a8d583003 --- /dev/null +++ b/CIL/Extraction/SpanBulkModels.lean @@ -0,0 +1,118 @@ +import CIL.Extraction.SpanModels + +/-! Bulk span and array calls use checked memory operations on the original evaluation stack. -/ + +namespace CIL.Extraction + +/-- Each form identifies one declaration of a bulk memory member. -/ +private inductive BulkMember where + | fill | clear | copy (trying : Bool) | arrayFill (counted : Bool) + +/-- The table key and raw metadata spelling of the member name. -/ +private def BulkMember.names : BulkMember → MetadataName × String + | .fill | .arrayFill _ => (name!"Fill", "Fill") + | .clear => (name!"Clear", "Clear") + | .copy false => (name!"CopyTo", "CopyTo") + | .copy true => (name!"TryCopyTo", "TryCopyTo") + +/-- The signature uses the owner's element for spans and the method's element for arrays. -/ +private def BulkMember.signature (member : BulkMember) (element : TypeExpr) : OpenSignature := + let int := TypeExpr.atom runtimeScope "System.Int32" + match member with + | .fill => ⟨0x20, .void, [element]⟩ + | .clear => ⟨0x20, .void, []⟩ + | .copy trying => ⟨0x20, if trying then .atom runtimeScope "System.Boolean" else .void, + [.apply (spanHead true) element]⟩ + | .arrayFill counted => ⟨0x10, .void, + if counted then [.array element, element, int, int] else [.array element, element]⟩ + +/-- The open declaration and exact external target bound to this overload. -/ +private def BulkMember.operand (member : BulkMember) : ModelOperand := + let name := member.names.2 + match member with + | .arrayFill counted => + let parameters := if counted then "!!0[],!!0,System.Int32,System.Int32" else "!!0[],!!0" + .external (fun _ => member.signature (.parameter 0)) name fun _ => + some s!"{runtimeScope}::System.Void System.Array::Fill({parameters})" + | _ => + .external (fun _ => member.signature (.ownerParameter 0)) name fun method => do + let .apply (.constructor _ owner) element := method.owner | none + let elementName ← spanElementName element + let result := if member matches .copy true then "System.Boolean" else "System.Void" + let parameters := match member with + | .copy _ => "System.Span`1" | .fill => "!0" | _ => "" + return s!"{runtimeScope}::{result} {owner}<{elementName}>::{name}({parameters})" + +/-- The closed receiver or generic argument supplies the element of this overload. -/ +private def BulkMember.element (member : BulkMember) (owner : TypeExpr) (method : MethodRef) : + Option TypeExpr := do + match member with + | .arrayFill _ => + let [element] := method.methodArguments | none + _root_.guard (method.owner == owner && !method.hasThis && method.genericArity == 1) + return element + | _ => + let .apply head element := method.owner | none + _root_.guard (head == owner && method.hasThis && method.genericArity == 0 && + method.methodArguments.isEmpty) + return element + +/-- A closed call selects exactly its declared result and parameter list. -/ +private def BulkMember.accepts (member : BulkMember) (owner : TypeExpr) (method : MethodRef) : + Bool := (member.element owner method).any fun element => + let signature := member.signature element + method.name == member.names.1 && method.returnType == signature.returnType && + method.parameters == signature.parameters + +/-- Array fills require scalar storage. Span operations also admit checked structs and vectors. -/ +private def BulkMember.operation (member : BulkMember) (owner : TypeExpr) (reader : CallLayouts) + (method : MethodRef) : Option MemoryOp := do + let element ← member.element owner method + if let .arrayFill counted := member then + let storage ← arrayElementType element + if storage matches .aggregate .. | .vector .. then none + return .arrayFill storage counted + let width ← match element with + | .aggregate .. => (reader.valueShape element).size? + | _ => spanElementWidth element + let _ ← spanElementName element + match member with + | .copy trying => return .spanCopy width trying + | .fill => return .spanFill width false + | .clear => return .spanFill width true + | .arrayFill _ => none + +/-- Each overload resolves only to its checked memory operation. -/ +private def bulkEntry (owner : TypeExpr) (first : BulkMember) (others : List BulkMember := []) : + ModelEntry := + { opcode := .call, owner, name := first.names.1, ownsName := true, + overloads := (first :: others).map fun member => + { operand := member.operand, accepts := member.accepts owner, + resolve := fun reader method => + RuntimeModel.memory <$> member.operation owner reader method } } + +/-- The seven owner and name pairs cover both copy receivers and both array fill overloads. -/ +def spanBulkEntries : List ModelEntry := + [bulkEntry (spanHead true) .fill, bulkEntry (spanHead true) .clear, + bulkEntry (spanHead true) (.copy false), bulkEntry (spanHead false) (.copy false), + bulkEntry (spanHead true) (.copy true), bulkEntry (spanHead false) (.copy true), + bulkEntry (.atom runtimeScope "System.Array") (.arrayFill false) [.arrayFill true]] + +/-- Every resolved bulk call executes a memory model. -/ +theorem spanBulkEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : + ∀ entry ∈ spanBulkEntries, entry.Yields property := by + have yields (owner first others) : (bulkEntry owner first others).Yields property := by + intro reader method model found + obtain ⟨row, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + have listed := ModelEntry.select?_mem (entry := bulkEntry owner first others) selected + obtain ⟨_, _, rfl⟩ := List.mem_map.mp listed + obtain ⟨operation, _, rfl⟩ := Option.map_eq_some_iff.mp resolved + exact memory operation + intro entry member + simp only [spanBulkEntries, List.mem_cons, List.not_mem_nil, or_false] at member + rcases member with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> apply yields + +#print axioms spanBulkEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/SpanCalls.lean b/CIL/Extraction/SpanCalls.lean index dd404ae19..4f7b11284 100644 --- a/CIL/Extraction/SpanCalls.lean +++ b/CIL/Extraction/SpanCalls.lean @@ -1,48 +1,17 @@ import CIL.Extraction.Generics /-! -# Exact reference-free span calls +# Span call names -Runtime span members and array-view factories retain their element identity, argument arity, -width and write capability in translation evidence. Aggregate widths use checked layouts. -Array factories require the exact -System.Memory identity; the span types and primitive elements require System.Runtime. +Span element names as runtime signatures spell them and the assembly defining array extensions. -/ namespace CIL.Extraction /-- The exact assembly defining the supported array extension methods. -/ def spanMemoryScope : String := - "System.Memory, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" - -/-- The observable choice made by a supported span call. -/ -inductive SpanCall where - | empty (width : Nat) (writable : Bool) - | reinterpret (sourceWidth targetWidth : Nat) (writable : Bool) - | copy (width : Nat) (tryCopy : Bool) - | reference (width : Nat) (writable : Bool) - | singleReference (width : Nat) (writable : Bool) - | fill (width : Nat) (clear : Bool) - | arrayFill (element : ArrayElement) (counted : Bool) - | item (width : Nat) | length | slice (counted : Bool) | readOnly - | array (element : ArrayElement) (writable : Bool) (range : ArraySpanRange) - deriving DecidableEq, Repr - -/-- A span operation's exact semantic parameters, independent of other memory operations. -/ -private def emittedSpanCall : Op → Option SpanCall - | .memory (.spanEmpty width writable) => some (.empty width writable) - | .memory (.spanReinterpret source target writable) => some (.reinterpret source target writable) - | .memory (.spanCopy width tryCopy) => some (.copy width tryCopy) - | .memory (.spanFromReference width writable) => some (.reference width writable) - | .memory (.spanSingleReference width writable) => some (.singleReference width writable) - | .memory (.spanFill width clear) => some (.fill width clear) - | .memory (.arrayFill element counted) => some (.arrayFill element counted) - | .memory (.spanItem width) => some (.item width) - | .memory .spanLength => some .length - | .memory (.spanSlice counted) => some (.slice counted) - | .memory .spanReadOnly => some .readOnly - | .memory (.arraySpan element writable range) => some (.array element writable range) - | _ => none + metadata_text% + "System.Memory, Version=11.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" /-- Closed span element names retain nested type arguments in runtime signatures. -/ def spanElementName (type : TypeExpr) : Option String := do @@ -53,218 +22,4 @@ def spanElementName (type : TypeExpr) : Option String := do let .apply (.constructor _ name) (.atom _ element) := type | none if (integerVectorWidth type).isSome then some (name ++ "<" ++ element ++ ">") else none -/-- A span member or array constructor with a fully specified owner and signature. -/ -private def spanMemberCall (opcode : Nat) (site : CallSite) - (aggregateWidth : Nat := 0) : Option SpanCall := do - let .apply (.constructor _ owner) elementType := site.declaringType - | none - let elementName ← spanElementName elementType - let .span width writable ← spanStorage site.declaringType aggregateWidth | none - let arrayElement := arrayElementType elementType aggregateWidth - if !site.typeArguments.isEmpty || site.parameterCount != site.parameterTypes.length then none - else pure () - let intType := TypeExpr.atom runtimeScope "System.Int32" - let ownerName := owner ++ "<" ++ elementName ++ ">" - let target (result parameters : String) := MethodId.external - (runtimeScope ++ "::" ++ result ++ " " ++ ownerName ++ "::" ++ site.name ++ - "(" ++ parameters ++ ")") - if opcode == 115 && site.name == ".ctor" && site.hasThis && - !site.returnsValue && site.returnType == .void then - if site.parameterTypes == [.byref elementType] && - site.target == target "System.Void" "!0&" then - some (.singleReference width writable) - else if site.parameterTypes == [.pointer .void, intType] && - site.target == target "System.Void" "System.Void*,System.Int32" then - if !writable && elementType == .atom runtimeScope "System.Byte" then none - else some (.reference width writable) - else do - let element ← arrayElement - if site.parameterTypes == [.array elementType] && - site.target == target "System.Void" "!0[]" then - some (.array (← arrayElement) writable .whole) - else if site.parameterTypes == [.array elementType, intType, intType] && - site.target == target "System.Void" "!0[],System.Int32,System.Int32" then - some (.array element writable .counted) - else none - else if opcode == 40 && site.hasThis && writable && !site.returnsValue && - site.returnType == .void && site.name == "Clear" && site.parameterTypes.isEmpty && - site.target == target "System.Void" "" then some (.fill width true) - else if opcode == 40 && site.hasThis && writable && !site.returnsValue && - site.returnType == .void && site.name == "Fill" && site.parameterTypes == [elementType] && - site.target == target "System.Void" "!0" then some (.fill width false) - else if opcode == 40 && site.hasThis && - site.parameterTypes == [.apply (.constructor runtimeScope "System.Span`1") elementType] && - site.name == "CopyTo" && !site.returnsValue && site.returnType == .void && - site.target == target "System.Void" "System.Span`1" then some (.copy width false) - else if opcode == 40 && site.hasThis && - site.parameterTypes == [.apply (.constructor runtimeScope "System.Span`1") elementType] && - site.name == "TryCopyTo" && site.returnsValue && - site.returnType == .atom runtimeScope "System.Boolean" && - site.target == target "System.Boolean" "System.Span`1" then some (.copy width true) - else if opcode != 40 || !site.returnsValue then none - else if !site.hasThis && site.name == "op_Implicit" then - if site.returnType == site.declaringType && site.parameterTypes == [.array elementType] && - site.target == target (owner ++ "") "!0[]" then - some (.array (← arrayElement) writable .whole) - else if writable && site.returnType == - .apply (.constructor runtimeScope "System.ReadOnlySpan`1") elementType && - site.parameterTypes == [site.declaringType] && - site.target == target "System.ReadOnlySpan`1" "System.Span`1" then - some .readOnly - else none - else if !site.hasThis && site.name == "get_Empty" && site.parameterTypes.isEmpty && - site.returnType == site.declaringType && - site.target == target (owner ++ "") "" then some (.empty width writable) - else if !site.hasThis then none - else if site.name == "get_Length" && site.returnType == intType && - site.parameterTypes.isEmpty && site.target == target "System.Int32" "" then - some .length - else if site.name == "get_Item" && site.parameterTypes == [intType] then - let result := if writable then "!0&" else - "!0& modreq(System.Runtime.InteropServices.InAttribute)" - let returnType := if writable then TypeExpr.byref elementType else - .atom runtimeScope result - if site.returnType == returnType && site.target == target result "System.Int32" then - some (.item width) else none - else if site.name == "Slice" && site.returnType == site.declaringType then - if site.parameterTypes == [intType] && - site.target == target (owner ++ "") "System.Int32" then some (.slice false) - else if site.parameterTypes == [intType, intType] && - site.target == target (owner ++ "") "System.Int32,System.Int32" then - some (.slice true) - else none - else none - -/-- An exact span constructor, independent of whether CIL allocates or initializes its receiver. -/ -def spanLocalConstructor (site : CallSite) (aggregateWidth : Nat := 0) : Option SpanConstructor := do - if let some (.array element writable range) := spanMemberCall 115 site aggregateWidth then - return .array element writable range - if let some (.singleReference width writable) := spanMemberCall 115 site aggregateWidth then - return .singleReference width writable - if site.name != ".ctor" || !site.hasThis || site.returnsValue || - !site.typeArguments.isEmpty || site.parameterCount != 2 || site.returnType != .void || - site.parameterTypes != [.pointer .void, .atom runtimeScope "System.Int32"] then none - else pure () - let .apply (.constructor _ owner) elementType := site.declaringType | none - let element ← spanElementName elementType - let .span width writable ← spanStorage site.declaringType aggregateWidth | none - if site.target != .external (runtimeScope ++ "::System.Void " ++ owner ++ "<" ++ element ++ - ">::.ctor(System.Void*,System.Int32)") then none else some (.reference width writable) - -/-- Aggregate constructor widths are bound separately by mandatory layout-use evidence. - Only aggregate metadata yields the zero-width placeholder in a folded descriptor. -/ -def spanConstructorMatches : SpanConstructor → SpanConstructor → Bool - | .empty expected writable, .empty actual capability - | .reference expected writable, .reference actual capability - | .singleReference expected writable, .singleReference actual capability => - (expected == 0 || expected == actual) && writable == capability - | .array expected writable range, .array actual capability actualRange => - expected.metadata == actual.metadata && writable == capability && range == actualRange - | expected, actual => expected == actual - -/-- Array fill binds the exact primitive or enum element and optional int32 range. -/ -private def arrayFillCall (opcode : Nat) (site : CallSite) : Option SpanCall := do - if opcode != 40 || site.hasThis || site.returnsValue || site.returnType != .void || - site.name != "Fill" || site.declaringType != .atom runtimeScope "System.Array" || - site.parameterCount != site.parameterTypes.length || site.ownerArgumentCount != 0 then none - else pure () - let [elementType] := site.typeArguments | none - let element ← arrayElementType elementType - match element with - | .aggregate .. | .vector .. => none - | _ => - let intType := TypeExpr.atom runtimeScope "System.Int32" - let (counted, suffix) ← if site.parameterTypes == [.array elementType, elementType] then - some (false, "") - else if site.parameterTypes == [.array elementType, elementType, intType, intType] then - some (true, ",System.Int32,System.Int32") else none - if site.target != .external - (runtimeScope ++ "::System.Void System.Array::Fill(!!0[],!!0" ++ suffix ++ ")") - then none else some (.arrayFill element counted) - -/-- The array overloads of AsSpan accept an array and zero, one or two int32 arguments. -/ -private def arrayExtensionCall (opcode : Nat) (site : CallSite) - (aggregateWidth : Nat := 0) : Option SpanCall := do - if opcode != 40 || site.hasThis || !site.returnsValue || site.name != "AsSpan" || - site.declaringType != .atom spanMemoryScope "System.MemoryExtensions" || - site.parameterCount != site.parameterTypes.length then none else pure () - let [elementType] := site.typeArguments | none - let arrayElement := arrayElementType elementType aggregateWidth - if site.returnType != .apply (.constructor runtimeScope "System.Span`1") elementType then none - else pure () - let intType := TypeExpr.atom runtimeScope "System.Int32" - let (range, parameters) ← - if site.parameterTypes == [.array elementType] then some (.whole, "!!0[]") - else if site.parameterTypes == [.array elementType, intType] then - some (.fromStart, "!!0[],System.Int32") - else if site.parameterTypes == [.array elementType, intType, intType] then - some (.counted, "!!0[],System.Int32,System.Int32") else none - if site.target != .external (spanMemoryScope ++ - "::System.Span`1 System.MemoryExtensions::AsSpan(" ++ - parameters ++ ")") then none else some (.array (← arrayElement) true range) - -/-- Reinterpretation retains readonly status and both primitive or enum element types. -/ -private def reinterpretCall (opcode : Nat) (site : CallSite) - (sourceAggregateWidth targetAggregateWidth : Nat) : Option SpanCall := do - if opcode != 40 || site.hasThis || !site.returnsValue || site.parameterCount != 1 || - site.declaringType != .atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal" then - none else pure () - let (source, target, resultName) ← match site.name, site.typeArguments with - | "AsBytes", [source] => some (source, .atom runtimeScope "System.Byte", "System.Byte") - | "Cast", [source, target] => some (source, target, "!!1") - | _, _ => none - let sourceWidth ← spanElementWidth source sourceAggregateWidth - let targetWidth ← spanElementWidth target targetAggregateWidth - let [input] := site.parameterTypes | none - let .span _ writable ← spanStorage input | none - let owner := if writable then "System.Span`1" else "System.ReadOnlySpan`1" - if input != .apply (.constructor runtimeScope owner) source || - site.returnType != .apply (.constructor runtimeScope owner) target || - site.target != .external (runtimeScope ++ "::" ++ owner ++ "<" ++ resultName ++ - "> System.Runtime.InteropServices.MemoryMarshal::" ++ site.name ++ "(" ++ owner ++ - ")") then none - else some (.reinterpret sourceWidth targetWidth writable) - -/-- Reference factories bind the exact element, output capability and generic signature. -/ -private def referenceFactory (opcode : Nat) (site : CallSite) - (aggregateWidth : Nat) : Option SpanCall := do - if opcode != 40 || site.hasThis || !site.returnsValue || site.parameterCount != 2 || - site.declaringType != .atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal" then - none else pure () - let [element] := site.typeArguments | none - let writable ← if site.name == "CreateSpan" then some true - else if site.name == "CreateReadOnlySpan" then some false else none - let owner := if writable then "System.Span`1" else "System.ReadOnlySpan`1" - if site.parameterTypes != [.byref element, .atom runtimeScope "System.Int32"] || - site.returnType != .apply (.constructor runtimeScope owner) element || - site.target != .external (runtimeScope ++ "::" ++ owner ++ - " System.Runtime.InteropServices.MemoryMarshal::" ++ site.name ++ - "(!!0&,System.Int32)") then none - else some (.reference (← spanElementWidth element aggregateWidth) writable) - -/-- An exactly checked span indexer returns a reference, including readonly metadata modifiers. -/ -def spanCallReturnsReference (site : CallSite) : Bool := - (spanMemberCall 40 site).any fun operation => match operation with - | .item _ => true - | _ => false - -/-- Exact span signatures and primitive/vector widths. Aggregate widths are separately bound - by mandatory LayoutUsesComplete and MethodLayoutsValid evidence. -/ -def spanCallMatches (instruction : RawInstruction) (emitted : Op) : Bool := - let (sourceWidth, targetWidth) := match emittedSpanCall emitted with - | some (.reinterpret source target _) => (source, target) - | some (.copy width _) | some (.fill width _) | some (.item width) - | some (.reference width _) | some (.singleReference width _) - | some (.empty width _) => (width, width) - | some (.array element _ _) => (element.width, element.width) - | _ => (0, 0) - let expected := match instruction.operand with - | .call site => (arrayFillCall instruction.opcode site).orElse (fun _ => - (spanMemberCall instruction.opcode site sourceWidth).orElse - (fun _ => (arrayExtensionCall instruction.opcode site sourceWidth).orElse - (fun _ => (referenceFactory instruction.opcode site sourceWidth).orElse - (fun _ => reinterpretCall instruction.opcode site sourceWidth targetWidth)))) - | _ => none - expected == emittedSpanCall emitted - end CIL.Extraction diff --git a/CIL/Extraction/SpanConstructorModels.lean b/CIL/Extraction/SpanConstructorModels.lean new file mode 100644 index 000000000..225d77e44 --- /dev/null +++ b/CIL/Extraction/SpanConstructorModels.lean @@ -0,0 +1,99 @@ +import CIL.Extraction.SpanModels + +/-! +# Span constructors + +Each overload binds an external MemberRef to the checked construction for its closed element. +`newobj` pushes the constructed span; `call` on the home of a span local or homed parameter +stores it in the slot the home names. `newobj` of `ReadOnlySpan(void*, int)` views +extracted static data, while the in-place call views any readable bytes, as a reference span. +-/ + +namespace CIL.Extraction + +/-- The explicit arguments determine how the constructor locates its storage. -/ +private inductive Source where + | array | range | pointer | reference + +/-- Open and closed signatures differ only in the owner's element type. -/ +private def Source.signature (source : Source) (element : TypeExpr) : OpenSignature := + let int := TypeExpr.atom runtimeScope "System.Int32" + ⟨0x20, .void, match source with + | .array => [.array element] + | .range => [.array element, int, int] + | .pointer => [.pointer .void, int] + | .reference => [.byref element]⟩ + +/-- The target names the closed owner and the parameter types of its open declaration. -/ +private def Source.operand (source : Source) : ModelOperand := + .external (fun _ => source.signature (.ownerParameter 0)) ".ctor" fun method => do + let .apply (.constructor _ owner) element := method.owner | none + let name ← spanElementName element + let parameters := match source with + | .array => "!0[]" | .range => "!0[],System.Int32,System.Int32" + | .pointer => "System.Void*,System.Int32" | .reference => "!0&" + return s!"{runtimeScope}::System.Void {owner}<{name}>::.ctor({parameters})" + +/-- Array views require admissible array storage. Aggregate widths use the checked layout. With + `staticData` a read-only byte span over a pointer must lie in extracted static data. -/ +private def Source.constructor (source : Source) (writable staticData : Bool) + (reader : CallLayouts) (method : MethodRef) : Option SpanConstructor := do + let .apply _ element := method.owner | none + let _ ← spanElementName element + let width ← match element with + | .aggregate .. => (reader.valueShape element).size? + | _ => spanElementWidth element + match source with + | .reference => return .singleReference width writable + | .pointer => + if staticData && !writable && element == .atom runtimeScope "System.Byte" then + return .staticBytes + return .reference width writable + | .array | .range => + let _ ← reader.storage (.array element) + let range := match source with | .range => ArraySpanRange.counted | _ => .whole + return .array (← arrayElementType element width) writable range + +/-- The closed instance constructor must agree with the declaration on its result and arguments; + `model` says what the opcode does with the constructed span. -/ +private def Source.overload (source : Source) (writable staticData : Bool) + (model : SpanConstructor → RuntimeModel) : ModelOverload := + { operand := source.operand + accepts := fun method => match method.owner with + | .apply head element => head == spanHead writable && method.name == name!".ctor" && + method.hasThis && method.genericArity == 0 && method.methodArguments.isEmpty && + method.signature 0x20 == source.signature element + | _ => false + resolve := fun reader method => + model <$> source.constructor writable staticData reader method } + +/-- The four overloads, in the order their rows are tried. -/ +private def sources : List Source := [.array, .range, .pointer, .reference] + +/-- Both span owners reserve their constructor name under `newobj` and under `call`. -/ +def spanConstructorEntries : List ModelEntry := [true, false].flatMap fun writable => + [{ opcode := .newobj, owner := spanHead writable, name := name!".ctor", ownsName := true, + overloads := sources.map (·.overload writable true fun constructor => + .memory constructor.operation) }, + { opcode := .call, owner := spanHead writable, name := name!".ctor", ownsName := true, + overloads := sources.map (·.overload writable false .spanInPlace) }] + +/-- A resolved span constructor executes its checked memory operation, or runs in place. -/ +theorem spanConstructorEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) + (inPlace : ∀ constructor, property (.spanInPlace constructor)) : + ∀ entry ∈ spanConstructorEntries, entry.Yields property := by + intro entry member reader method model found + obtain ⟨writable, _, member⟩ := List.mem_flatMap.mp member + obtain ⟨row, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + simp only [List.mem_cons, List.not_mem_nil, or_false] at member + rcases member with rfl | rfl + all_goals + obtain ⟨source, _, rfl⟩ := List.mem_map.mp (ModelEntry.select?_mem selected) + obtain ⟨constructor, _, rfl⟩ := Option.map_eq_some_iff.mp resolved + · exact memory _ + · exact inPlace constructor + +#print axioms spanConstructorEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/SpanModels.lean b/CIL/Extraction/SpanModels.lean new file mode 100644 index 000000000..4eb302f81 --- /dev/null +++ b/CIL/Extraction/SpanModels.lean @@ -0,0 +1,154 @@ +import CIL.Extraction.ModelTable +import CIL.Extraction.SpanCalls + +/-! +# Core span member models + +A kept span call checks the member's open declaration and its closed receiver, result and +parameters. Indexing and empty span construction read the checked element width. +-/ + +namespace CIL.Extraction + +/-- The operations available on an existing span, including the two static members. -/ +inductive SpanMember where + | item | length | slice (counted : Bool) | isEmpty | pinnable | readOnly | array | empty + deriving DecidableEq + +/-- The member name used to index calls in the model table. -/ +def SpanMember.name : SpanMember → MetadataName + | .item => name!"get_Item" + | .length => name!"get_Length" + | .slice _ => name!"Slice" + | .isEmpty => name!"get_IsEmpty" + | .pinnable => name!"GetPinnableReference" + | .readOnly | .array => name!"op_Implicit" + | .empty => name!"get_Empty" + +/-- The generic definition that declares members of a writable or read-only span. -/ +def spanHead (writable : Bool) : TypeExpr := + .constructor runtimeScope (if writable then "System.Span`1" else "System.ReadOnlySpan`1") + +/-- The accessor signature for an element. Open readonly returns carry the required modifier; + the closed operand carries Cecil's scoped spelling of that return. -/ +private def SpanMember.signature (member : SpanMember) (writable openType : Bool) + (element : TypeExpr) : OpenSignature := Id.run do + let owner := TypeExpr.apply (spanHead writable) element + let int := TypeExpr.atom runtimeScope "System.Int32" + let reference := if writable then TypeExpr.byref element else if openType then + .modified true runtimeScope "System.Runtime.InteropServices.InAttribute" (.byref element) + else .atom runtimeScope "!0& modreq(System.Runtime.InteropServices.InAttribute)" + return match member with + | .item => ⟨0x20, reference, [int]⟩ + | .pinnable => ⟨0x20, reference, []⟩ + | .length => ⟨0x20, int, []⟩ + | .isEmpty => ⟨0x20, .atom runtimeScope "System.Boolean", []⟩ + | .slice counted => ⟨0x20, owner, if counted then [int, int] else [int]⟩ + | .readOnly => ⟨0, .apply (spanHead false) element, [owner]⟩ + | .array => ⟨0, owner, [.array element]⟩ + | .empty => ⟨0, owner, []⟩ + +/-- The declaration and exact external target of a span overload. -/ +private def SpanMember.operand (member : SpanMember) (writable : Bool) : ModelOperand := + let memberName := match member with + | .item => "get_Item" | .length => "get_Length" | .slice _ => "Slice" + | .isEmpty => "get_IsEmpty" | .pinnable => "GetPinnableReference" | .empty => "get_Empty" + | .array | .readOnly => "op_Implicit" + let declared := member.signature writable true (.ownerParameter 0) + .external (fun _ => declared) memberName fun method => do + let .apply (.constructor _ owner) element := method.owner | none + let name ← spanElementName element + let reference := if writable then "!0&" else + "!0& modreq(System.Runtime.InteropServices.InAttribute)" + let (result, parameters) := match member with + | .item => (reference, "System.Int32") + | .pinnable => (reference, "") + | .length => ("System.Int32", "") + | .isEmpty => ("System.Boolean", "") + | .array => (owner ++ "", "!0[]") + | .empty => (owner ++ "", "") + | .slice counted => (owner ++ "", if counted then "System.Int32,System.Int32" + else "System.Int32") + | .readOnly => ("System.ReadOnlySpan`1", "System.Span`1") + return s!"{runtimeScope}::{result} {owner}<{name}>::{memberName}({parameters})" + +/-- The closed receiver, element and signature select this overload. -/ +private def SpanMember.accepts (member : SpanMember) (writable : Bool) (method : MethodRef) : + Bool := match method.owner with + | .apply head element => + head == spanHead writable && method.name == member.name && method.genericArity == 0 && + method.methodArguments.isEmpty && method.signature (if method.hasThis then 0x20 else 0) == + member.signature writable false element + | _ => false + +/-- Scalar flow recognizes an exact element reference before it has a computed layout table. -/ +def spanReferenceResult (site : CallSite) : Bool := (do + let member ← match site.name with + | "get_Item" => some SpanMember.item + | "GetPinnableReference" => some SpanMember.pinnable + | _ => none + let .span _ writable ← spanStorage site.declaringType | none + return member.accepts writable site.method && (member.operand writable).bindsSite site + ).getD false + +/-- The operation uses the indexed layout of a closed aggregate element. -/ +private def SpanMember.operation (member : SpanMember) (writable : Bool) + (reader : CallLayouts) (method : MethodRef) : Option MemoryOp := do + let .apply _ element := method.owner | none + let width ← match element with + | .aggregate .. => (reader.valueShape element).size? + | _ => spanElementWidth element + let _ ← spanElementName element + match member with + | .item => return .spanItem width + | .length => return .spanLength + | .slice counted => return .spanSlice counted + | .isEmpty => return .observeView .spanEmpty + | .pinnable => return .observeView .spanPinnable + | .readOnly => return .spanReadOnly + | .empty => return .spanEmpty width writable + | .array => + let _ ← reader.storage (.array element) + return .arraySpan (← arrayElementType element width) writable .whole + +/-- A span overload binds its own declaration and produces only memory models. -/ +private def SpanMember.overload (member : SpanMember) (writable : Bool) : ModelOverload := + { operand := member.operand writable, accepts := member.accepts writable, + resolve := fun reader method => + RuntimeModel.memory <$> member.operation writable reader method } + +/-- All overloads of one span member share its reserved table slot. -/ +def spanEntry (writable : Bool) (member : SpanMember) (others : List SpanMember := []) : + ModelEntry := + { opcode := .call, owner := spanHead writable, name := member.name, ownsName := true, + overloads := (member :: others).map (·.overload writable) } + +/-- Both span owners have array conversions. Only a writable span converts to a readonly span. -/ +def spanEntries : List ModelEntry := [true, false].flatMap fun writable => + [spanEntry writable .array (if writable then [.readOnly] else []), + spanEntry writable (.slice false) [.slice true]] ++ + [.item, .length, .isEmpty, .pinnable, .empty].map (spanEntry writable · []) + +/-- Every resolved core span member executes a checked memory operation. -/ +theorem spanEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : + ∀ entry ∈ spanEntries, entry.Yields property := by + have yields (writable : Bool) (member : SpanMember) (others : List SpanMember) : + (spanEntry writable member others).Yields property := by + intro reader method model found + obtain ⟨row, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + have listed := ModelEntry.select?_mem (entry := spanEntry writable member others) selected + obtain ⟨form, _, rfl⟩ := List.mem_map.mp listed + obtain ⟨operation, _, rfl⟩ := Option.map_eq_some_iff.mp resolved + exact memory operation + intro entry member + simp only [spanEntries, List.mem_flatMap, List.mem_append, List.mem_cons, + List.not_mem_nil, or_false, List.mem_map] at member + obtain ⟨writable, _, (rfl | rfl) | ⟨_, _, rfl⟩⟩ := member <;> apply yields + +/-- info: 'CIL.Extraction.spanEntries_yields' depends on axioms: [propext, Quot.sound] -/ +#guard_msgs in #print axioms spanEntries_yields +/-- info: 'CIL.Extraction.spanEntry' depends on axioms: [propext] -/ +#guard_msgs in #print axioms spanEntry + +end CIL.Extraction diff --git a/CIL/Extraction/StaticIdentities.lean b/CIL/Extraction/StaticIdentities.lean new file mode 100644 index 000000000..a4776cb56 --- /dev/null +++ b/CIL/Extraction/StaticIdentities.lean @@ -0,0 +1,179 @@ +import CIL.Extraction.BodyDecoding + +/-! +# Static identities + +The model keys static storage by `StaticDescriptor.identity`, so an identity must stand for +exactly one field. `StaticIdentitiesValid` holds when, over every static site of the program, two +sites share an identity exactly when the tokens of their instructions resolve to the same +`Field` row of the same pinned assembly. A site's token is the one the body decoder records at +its pc (`BodyEvidence.statics`), and only for a field whose binding found its carried row. + +Each body's recorded accesses are walked beside its sites once, and two sorts of the S pairs of +identity and field decide the rest: O(S log S) comparisons of numbers. +-/ + +namespace CIL.Extraction + +/-- Metadata tokens are 32-bit, so an assembly's code and a token below this bound pack into one + number, distinct for distinct pairs. -/ +def tokenSpace : Nat := 2 ^ 32 + +/-- An assembly identity as one number: its bytes in base 256 after a leading 1, so distinct byte + strings give distinct numbers; `none` when an entry is not a byte. -/ +def identityCode (identity : List Nat) : Option Nat := + if identity.all (· < 256) then some (identity.foldl (fun code byte => code * 256 + byte) 1) + else none + +/-- The code of the identity of each slice a body carries, in the slices' order. -/ +def BodyEvidence.sliceCodes (body : BodyEvidence) : List (Option Nat) := + body.identities.map identityCode + +/-- The metadata slice and Field token a recorded static operand resolves to. -/ +def BodyEvidence.staticFieldRow (body : BodyEvidence) (token : Nat) : Option (Nat × Nat) := do + if metadataTableOf token == Table.field then return (0, token) + (← body.tables.head?).link linkDefinition token + +/-- The field a recorded static token names, packed as its assembly's code times `tokenSpace` + plus its `Field` row token: a `Field` token is a row of the body's own assembly, and a + `MemberRef` names the row its own slice links it to, the link the token's binding checked. -/ +def BodyEvidence.staticField (body : BodyEvidence) (codes : List (Option Nat)) (token : Nat) : + Option Nat := do + let (position, row) ← body.staticFieldRow token + let code ← (← codes[position]?) + if row < tokenSpace then some (code * tokenSpace + row) else none + +/-- The recorded token at each of the ascending `pcs`, walking them beside the recorded accesses + in ascending index order; `none` when a pc records no access. -/ +def siteTokens : List (Nat × Nat) → List Nat → Option (List Nat) + | _, [] => some [] + | [], _ :: _ => none + | (index, token) :: statics, pc :: pcs => + if pc < index then none + else + let (here, later) := (pc :: pcs).span (· == index) + (siteTokens statics later).map (List.replicate here.length token ++ ·) + +/-- Each static site of the method as its identity beside the field its pc's recorded token + names; `none` when a site's pc records no bound static token. -/ +def methodStaticPairs (method : Method) (body : BodyEvidence) : Option (List (Nat × Nat)) := do + let tokens ← siteTokens body.statics (method.staticSites.map Prod.fst) + let codes := body.sliceCodes + (method.staticSites.zip tokens).mapM fun ((_, descriptor), token) => do + pure (descriptor.identity, ← body.staticField codes token) + +/-- The static sites of every method, each method read against the body at its position. -/ +def programStaticPairs : Program → List BodyEvidence → Option (List (Nat × Nat)) + | method :: program, body :: bodies => do + pure ((← methodStaticPairs method body) ++ (← programStaticPairs program bodies)) + | _, _ => some [] + +/-- Whether each pair after the first has a greater first number than the pair before it, or is + that pair again. -/ +def ascendingRuns : List (Nat × Nat) → Bool + | first :: second :: rest => + (first.1 < second.1 || first == second) && ascendingRuns (second :: rest) + | _ => true + +/-- In ascending runs, every later pair has a greater first number than the head or equals it. -/ +theorem ascendingRuns_head : ∀ {first : Nat × Nat} {rest : List (Nat × Nat)}, + ascendingRuns (first :: rest) = true → ∀ pair ∈ rest, first.1 < pair.1 ∨ first = pair + | _, [], _ => by simp + | first, second :: rest, valid => by + simp only [ascendingRuns, Bool.and_eq_true, Bool.or_eq_true, decide_eq_true_eq, + beq_iff_eq] at valid + obtain ⟨step, tail⟩ := valid + have later := ascendingRuns_head tail + intro pair member + simp only [List.mem_cons] at member + rcases member with same | member + · subst same + exact step + · rcases step with less | same + · rcases later pair member with further | equal + · exact Or.inl (Nat.lt_trans less further) + · subst equal + exact Or.inl less + · subst same + exact later pair member + +/-- In ascending runs, two pairs with the same first number are the same pair. -/ +theorem ascendingRuns_unique : ∀ {pairs : List (Nat × Nat)}, ascendingRuns pairs = true → + ∀ left ∈ pairs, ∀ right ∈ pairs, left.1 = right.1 → left = right + | [], _ => by simp + | first :: rest, valid => by + have head := ascendingRuns_head valid + have tail : ascendingRuns rest = true := by + cases rest with + | nil => rfl + | cons second rest => + simp only [ascendingRuns, Bool.and_eq_true] at valid + exact valid.2 + intro left leftMember right rightMember same + simp only [List.mem_cons] at leftMember rightMember + rcases leftMember with leftHead | leftMember <;> + rcases rightMember with rightHead | rightMember + · rw [leftHead, rightHead] + · subst leftHead + rcases head right rightMember with less | equal + · omega + · exact equal + · subst rightHead + rcases head left leftMember with less | equal + · omega + · exact equal.symm + · exact ascendingRuns_unique tail left leftMember right rightMember same + +/-- Whether no first number of the pairs comes with two second numbers. Sorted, the pairs must + rise in their first number except where a pair repeats; a sort that failed to order them + refuses them, so only the sort's keeping every pair is relied on. -/ +def functional (pairs : List (Nat × Nat)) : Bool := + ascendingRuns (sortPairs pairs) + +/-- Pairs that pass `functional` with the same first number have the same second number. -/ +theorem functional_spec {pairs : List (Nat × Nat)} (valid : functional pairs = true) : + ∀ left ∈ pairs, ∀ right ∈ pairs, left.1 = right.1 → left.2 = right.2 := by + intro left leftMember right rightMember same + have kept := sortBy_perm pairBefore pairs.length pairs + rw [ascendingRuns_unique (pairs := sortPairs pairs) valid left (kept.mem_iff.mpr leftMember) + right (kept.mem_iff.mpr rightMember) same] + +/-- Whether the pairs relate identities and fields one to one: no identity comes with two fields + and no field with two identities. Two sorts of the S pairs decide it in O(S log S) + comparisons. -/ +def oneToOne (pairs : List (Nat × Nat)) : Bool := + functional pairs && functional (pairs.map fun (identity, field) => (field, identity)) + +/-- Two pairs that pass `oneToOne` share an identity exactly when they share a field. -/ +theorem oneToOne_spec {pairs : List (Nat × Nat)} (valid : oneToOne pairs = true) : + ∀ left ∈ pairs, ∀ right ∈ pairs, (left.1 = right.1 ↔ left.2 = right.2) := by + simp only [oneToOne, Bool.and_eq_true] at valid + intro left leftMember right rightMember + have swapped {pair : Nat × Nat} (member : pair ∈ pairs) : + (pair.2, pair.1) ∈ pairs.map fun (identity, field) => (field, identity) := + List.mem_map.mpr ⟨pair, member, rfl⟩ + exact ⟨functional_spec valid.1 left leftMember right rightMember, + functional_spec valid.2 _ (swapped leftMember) _ (swapped rightMember)⟩ + +#print axioms oneToOne_spec + +/-- Over the whole program, two static sites share an identity exactly when their recorded tokens + name one field, read through the body of each method's evidence. -/ +abbrev StaticIdentitiesValid (program : Program) (bodies : List BodyEvidence) : Prop := + (programStaticPairs program bodies).any oneToOne = true + +/-- A program that passes the check pairs its sites one to one with the fields they name. -/ +theorem StaticIdentitiesValid.pairs {program : Program} {bodies : List BodyEvidence} + (valid : StaticIdentitiesValid program bodies) : + ∃ pairs, programStaticPairs program bodies = some pairs ∧ + ∀ left ∈ pairs, ∀ right ∈ pairs, (left.1 = right.1 ↔ left.2 = right.2) := by + unfold StaticIdentitiesValid at valid + cases found : programStaticPairs program bodies with + | none => simp [found] at valid + | some pairs => + rw [found] at valid + exact ⟨pairs, rfl, oneToOne_spec (by simpa using valid)⟩ + +#print axioms StaticIdentitiesValid.pairs + +end CIL.Extraction diff --git a/CIL/Extraction/StaticSpanModel.lean b/CIL/Extraction/StaticSpanModel.lean new file mode 100644 index 000000000..fbc657ae2 --- /dev/null +++ b/CIL/Extraction/StaticSpanModel.lean @@ -0,0 +1,40 @@ +import CIL.Extraction.ModelTable + +/-! The RuntimeHelpers field-handle model binds the scalar element of a readonly span. -/ + +namespace CIL.Extraction + +/-- The field handle is the sole parameter of the generic static member. -/ +private def staticSpanSignature (element : TypeExpr) : OpenSignature := + { convention := 0x10, parameters := [.atom runtimeScope "System.RuntimeFieldHandle"], + returnType := .apply (.constructor runtimeScope "System.ReadOnlySpan`1") element } + +/-- The closed result must have the primitive or enum element supplied to the generic method. -/ +private def staticSpanOperation (reader : CallLayouts) (method : MethodRef) : Option MemoryOp := do + let [element] := method.methodArguments | none + guard (!method.hasThis && method.genericArity == 1 && + method.signature 0x10 == staticSpanSignature element) + let .primitive width _ := reader.valueShape element | none + return .createSpan width + +/-- Both the open declaration and the closed member bind the field handle and the span result. -/ +def staticSpanEntry : ModelEntry := + let owner := "System.Runtime.CompilerServices.RuntimeHelpers" + let name := "CreateSpan" + let handle := "System.RuntimeFieldHandle" + let target := s!"{runtimeScope}::System.ReadOnlySpan`1 {owner}::{name}({handle})" + let operand := ModelOperand.external (fun _ => staticSpanSignature (.parameter 0)) name + (fun _ => some target) + .single .call (.atom runtimeScope owner) name!"CreateSpan" operand + (fun reader method => RuntimeModel.memory <$> staticSpanOperation reader method) true + +/-- The field handle model executes a memory operation under the caller's storage checks. -/ +theorem staticSpanEntry_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : staticSpanEntry.Yields property := by + intro reader method model found + obtain ⟨_, _, rfl⟩ := Option.map_eq_some_iff.mp found + exact memory _ + +#print axioms staticSpanEntry_yields + +end CIL.Extraction diff --git a/CIL/Extraction/StaticValues.lean b/CIL/Extraction/StaticValues.lean index f5ad22726..c41673e7f 100644 --- a/CIL/Extraction/StaticValues.lean +++ b/CIL/Extraction/StaticValues.lean @@ -40,6 +40,12 @@ def staticStoredBytes (descriptor : StaticDescriptor) (value : Value) : Option ( if descriptor.bytes.length == 8 then some (Safety.numberBytes word.toNat 8) else none | _, _ => none +/-- Scalar storage has the width and extension of its field. An aggregate has no scalar flag. -/ +def staticStorageMatches (type : TypeExpr) (descriptor : StaticDescriptor) : Bool := + match objectFieldShape type with + | some (width, signed) => descriptor.bytes.length == width && descriptor.scalar == some signed + | none => !(type matches .enum ..) && descriptor.scalar.isNone + /-- Run constructors through the ordinary checked aggregate-construction operation. -/ def constructorYields (program : Program) (descriptor : StaticDescriptor) (init : StaticInitializer) : Bool := Id.run do @@ -48,9 +54,10 @@ def constructorYields (program : Program) (descriptor : StaticDescriptor) if size : descriptor.bytes.length < 2^64 then let wrapper : Method := { profile := body.profile - code := (List.range init.arguments.length).map (fun index => .arg index) ++ - [.newAggregate init.method init.arguments.length ⟨descriptor.bytes.length, size⟩, .ret none] + code := (List.range init.arguments.length).map (fun index => .ldarg index) ++ + [.newobj (.arity init.arguments.length), .ret] locals := [] + links := [(init.arguments.length, .aggregate init.method ⟨descriptor.bytes.length, size⟩)] returnsValue := true } match Safety.invoke (program ++ [wrapper]) initializerFuel program.length (init.arguments.map .scalar) initializerMemory with diff --git a/CIL/Extraction/TemplateCode.lean b/CIL/Extraction/TemplateCode.lean new file mode 100644 index 000000000..9bc6a66ac --- /dev/null +++ b/CIL/Extraction/TemplateCode.lean @@ -0,0 +1,69 @@ +import CIL.InstructionEquations +import CIL.Extraction.DecodedDefinition +import CIL.Safety.Linking + +/-! +# Code selection for decoded profile templates + +A shared template takes its instructions from closed decoded methods. A flag selects an +instruction at each pc so the template has a fixed length even when the flags are variables. +-/ + +namespace CIL.Extraction +open Lean Meta Elab + +/-- Give every method index in a template link table its caller's numbering, including the + definition named by `ldftn`. Taking its address enters no callee during execution. -/ +def _root_.CIL.Method.reindexLinks (body : Method) (index : Nat → Nat) : Method := + { body with links := body.links.map fun (pc, link) => (pc, match link with + | .address target => .address (index target) + | other => other.relocate index) } + +/-- An unselected flag combination keeps the raw opcode at every position. -/ +def templateHoles (sites : List RawInstruction) : List Instruction := + sites.map fun site => .unadmitted site.opcode + +/-- Each conditional instruction compiles independently of the choices at later pcs. -/ +@[noinline] private def templateCons (head : Unit → Instruction) (tail : List Instruction) : + List Instruction := head () :: tail + +/-- The instructions of a choice and whether it contains any selected input. -/ +private partial def templateInstructions (choice : Expr) : MetaM (Array Expr × Bool) := do + let (fn, args) := choice.getAppFnArgs + if fn == ``ite then + let #[_, condition, decision, enabled, disabled] := args + | throwError "expected a complete conditional in a template" + let (yes, selectedYes) ← templateInstructions enabled + let (no, selectedNo) ← templateInstructions disabled + unless yes.size == no.size do throwError "template alternatives have different lengths" + let type := mkConst ``CIL.Instruction + let instructions ← yes.zipWithM (fun left right => do + if ← isDefEq left right then return if selectedYes then left else right + return mkApp5 (mkConst ``ite [.succ .zero]) type condition decision left right) no + return (instructions, selectedYes || selectedNo) + let holes := fn == ``templateHoles + let list := if holes then choice.appArg! else choice + let (elements, complete) ← InstructionEquations.listElements list + unless complete do throwError "a template alternative must have a closed instruction list" + unless holes do return (elements, true) + let instructions ← elements.mapM fun site => do + let opcode ← DecodedDefinition.natValue (mkApp (mkConst ``RawInstruction.opcode) site) + return mkApp (mkConst ``Instruction.unadmitted) (mkNatLit opcode) + return (instructions, false) + +/-- A fixed-length list with each position selected from the decoded alternatives. -/ +elab "cil_template_code% " choice:term : term => do + let type := mkConst ``CIL.Instruction + let term ← Term.elabTermEnsuringType choice (mkApp (mkConst ``List [.zero]) type) + Term.synthesizeSyntheticMVarsNoPostponing + let term ← instantiateMVars term + let (instructions, selected) ← templateInstructions term + if selected && !term.isAppOf ``ite then return term + let empty := mkApp (mkConst ``List.nil [.zero]) type + let cons := mkApp (mkConst ``List.cons [.zero]) type + return instructions.foldr (init := empty) fun instruction tail => + if instruction.isAppOf ``ite then + mkApp2 (mkConst ``templateCons) (mkLambda `unit .default (mkConst ``Unit) instruction) tail + else mkApp2 cons instruction tail + +end CIL.Extraction diff --git a/CIL/Extraction/TransferKinds.lean b/CIL/Extraction/TransferKinds.lean new file mode 100644 index 000000000..233bef85c --- /dev/null +++ b/CIL/Extraction/TransferKinds.lean @@ -0,0 +1,29 @@ +import CIL.Extraction.CallLayouts + +/-! # Transfer kinds of runtime model elements + +The memory transfer a runtime model performs for a value type, read through the layout reader of +the checked layouts. +Vectors are admitted only in the integer element family the base's transfer matchers admit. +-/ + +namespace CIL + +/-- How a value of `type` moves through memory under the layout reader `reader`: a primitive as a + scalar of its width and signedness, an integer-element vector or a struct in the layouts as a + snapshot of its checked size, and `none` for every other type. -/ +def transferKind? (reader : Extraction.CallLayouts) (type : Extraction.TypeExpr) : + Option TransferKind := + match reader.valueShape type with + | .primitive width signed => some (.scalar width signed) + | .vector bytes => + if Extraction.integerVectorWidth type != some bytes then none + else match bytes with + | 16 => some (.snapshot .vector128) + | 32 => some (.snapshot .vector256) + | 64 => some (.snapshot .vector512) + | _ => none + | .aggregate size => some (.snapshot (.aggregate size)) + | _ => none + +end CIL diff --git a/CIL/Extraction/Translation.lean b/CIL/Extraction/Translation.lean index 7a4439166..3199f20fa 100644 --- a/CIL/Extraction/Translation.lean +++ b/CIL/Extraction/Translation.lean @@ -1,23 +1,33 @@ import CIL.Extraction.Scalars import CIL.Extraction.LayoutCoverage import CIL.Extraction.UnsafeCalls -import CIL.Extraction.ExactIntrinsics -import CIL.Extraction.VectorInitialization +import CIL.Extraction.OpaqueLocals +import CIL.Extraction.ClosedMethod +import CIL.Extraction.ObjectOperands import CIL.Extraction.StaticValues +import CIL.Extraction.StaticIdentities import CIL.Extraction.ImportLayouts import CIL.Extraction.Constrained import CIL.Extraction.Assemblies +import CIL.Extraction.BodyDecoding +import CIL.Extraction.BodyDiagnostics +import CIL.EntryIndex +import CIL.Extraction.LinkIndex import Std.Data.TreeMap.Lemmas +import CIL.Resolution /-! # Translation validity The top of the extraction checks. `TranslationValid` conjoins the kernel-checked validators for one extraction: layouts recompute, every emitted method passes the per-method validators -against its evidence record, static values are reproduced by their initializers, import -layouts agree, and every body is owned by the pinned assembly its evidence names. It is not a -simulation proof for every raw opcode; general instruction translation and metadata decoding -stay trusted, as `docs/SOUNDNESS.md` describes. +against its evidence record, static values are reproduced by their initializers, each static +identity stands for exactly one field, import +layouts agree, every body is owned by the pinned assembly its evidence names, and every method +definition is what its body bytes and carried metadata say. It is not a simulation proof for +every raw opcode; general instruction translation, that the carried metadata rows are the image's, +that the `platform` definitions every body is checked against are the platform's, and the PE +container stay trusted, as `docs/SOUNDNESS.md` describes. Generated modules prove each method's evidence separately and assemble it with `translation_of_methods`, so no validator is reduced twice. @@ -25,9 +35,20 @@ Generated modules prove each method's evidence separately and assemble it with namespace CIL.Extraction +deriving instance DecidableEq for CIL.Instruction +deriving instance DecidableEq for CIL.Method + +private instance (result : Except MethodDecodeError Method) (method : Method) : + Decidable (result = .ok method) := by + cases result with + | error failure => exact isFalse (by intro same; cases same) + | ok candidate => exact decidable_of_iff (candidate = method) ⟨congrArg Except.ok, Except.ok.inj⟩ + /-- Every extracted method has exactly one record, in emitted program order. -/ structure MethodEvidence where original : RawMethod + /-- The bytes `original` was decoded from, and the metadata its tokens resolve to. -/ + body : BodyEvidence arguments : List TypeExpr closed : RawMethod parameterInput : RawArguments @@ -38,6 +59,12 @@ structure MethodEvidence where dispatchTypes : List DispatchType := [] /-- The pinned assembly whose metadata supplied this body. -/ assembly : AssemblyPin := {} + /-- The string a proof selects the method by: an extraction's `signatures` table lists these, + and `BodyDecodes` equates each with Cecil's full name of the method its row defines. -/ + signature : String := "" + /-- The link table the extraction states for the method's call sites and constructions; the + call check ties each entry to its raw site. -/ + links : List (Nat × Link) := [] /-- The pin and owner identity of every original and closed body, for attribution checks. -/ def bodyOwners (evidence : List MethodEvidence) : List (AssemblyPin × String) := @@ -53,62 +80,6 @@ def specializationTable (evidence : List MethodEvidence) : List SpecializedMetho def evidenceConstants (evidence : List MethodEvidence) (index : Nat) : Option W32 := evidence[index]?.bind fun item => constantReturn item.closed item.featureStates -/-- The zero value of each local kind, used when the method zero-initializes its locals. -/ -private def initialLocal : LocalKind → Value - | .reference | .span .. | .array _ | .methodPointer _ | .methodTable _ | .object => .nullRef - | .byte => .i8 0 - | .narrow _ | .word32 => .i32 0 - | .word64 => .i64 0 - | .vector128 => .v128 0 - | .vector256 => .v256 0 - | .vector512 => .v512 0 - | .aggregate size => .aggregate (List.replicate size.val 0) - -/-- Narrow return conversion is part of the closed metadata signature. -/ -def returnNarrow (layouts : List RawLayout) (type : TypeExpr) : Option NarrowKind := do - let .field field ← typeStorage layouts type | none - match field with - | .signed8 => some .signed8 - | .unsigned8 | .boolean => some .unsigned8 - | .signed16 => some .signed16 - | .unsigned16 | .character => some .unsigned16 - | _ => none - -/-- Integer static-field storage and its narrow-load signedness. -/ -private def staticScalarType : TypeExpr → Option (Nat × Bool) - | .enum _ underlying => staticScalarType underlying - | .atom scope name => - if scope != runtimeScope then none else - match name with - | "System.SByte" => some (1, true) - | "System.Byte" | "System.Boolean" => some (1, false) - | "System.Int16" => some (2, true) - | "System.UInt16" | "System.Char" => some (2, false) - | "System.Int32" | "System.UInt32" => some (4, false) - | "System.Int64" | "System.UInt64" | "System.IntPtr" | "System.UIntPtr" => some (8, false) - | _ => none - | _ => none - -/-- Index static sites with the same first-match behavior as the metadata list. -/ -private def staticSiteIndex : List (Nat × StaticDescriptor) → Std.TreeMap Nat StaticDescriptor - | [] => ∅ - | (pc, descriptor) :: sites => (staticSiteIndex sites).insert pc descriptor - -/-- Duplicate sites retain the first descriptor, including malformed evidence. -/ -private theorem staticSiteIndex_get (sites : List (Nat × StaticDescriptor)) (pc : Nat) : - (staticSiteIndex sites)[pc]? = (sites.find? (fun site => site.1 == pc)).map Prod.snd := by - induction sites with - | nil => simp [staticSiteIndex] - | cons site sites ih => - rcases site with ⟨index, descriptor⟩ - simp only [staticSiteIndex, Std.TreeMap.getElem?_insert, List.find?_cons] - by_cases same : index = pc - · subst index; simp - · have different : (index == pc) = false := beq_eq_false_iff_ne.mpr same - simp [same, ih, different] - -#print axioms staticSiteIndex_get - /-- A readonly scalar load has exactly the width and extension of its raw field type. -/ private def staticLoadMatches (opcode : Nat) (instruction : Option RawInstruction) (descriptor : Option StaticDescriptor) : Bool := @@ -116,93 +87,418 @@ private def staticLoadMatches (opcode : Nat) (instruction : Option RawInstructio | some instruction, some descriptor => if instruction.opcode != opcode then false else match instruction.operand with - | .field _ _ type _ => - match staticScalarType type with - | some (width, signed) => descriptor.bytes.length == width && descriptor.scalar == some signed - | none => descriptor.scalar.isNone + | .field _ _ type _ _ => staticStorageMatches type descriptor | _ => false | _, _ => false -/-- Check return conversions and static loads using precomputed instruction and site indexes. -/ -def methodShapeInstructions (layouts : List RawLayout) (closed : RawMethod) +/-- Raw opcode of `ldsfld`. -/ +private def ldsfldOpcode : Nat := 126 +/-- Raw opcode of `ldsflda`. -/ +private def ldsfldaOpcode : Nat := 127 +/-- Raw opcode of `stsfld`. -/ +private def stsfldOpcode : Nat := 128 +/-- Raw opcode of `ldtoken`. -/ +private def ldtokenOpcode : Nat := 208 + +/-- The raw instruction has `opcode` and names `field` as a static field, and the site at its pc, + if any, carries the descriptor of `field`. -/ +private def staticFieldMatches (opcode : Nat) (field : FieldRef) + (instruction : Option RawInstruction) (descriptor : Option StaticDescriptor) : Bool := + match instruction with + | some instruction => instruction.opcode == opcode && + instruction.operand == .field field.owner field.index field.type field.name true && + descriptor.all (·.field == field) + | none => false + +/-- Static field instructions agree with the raw field + and its site descriptor, and each static load fits that descriptor. -/ +def methodShapeInstructions (closed : RawMethod) (method : Method) : Bool := let raw := FlowIndex.ofList closed.code - let sites := staticSiteIndex method.staticSites + let sites := firstEntryIndex method.staticSites method.code.zipIdx.all fun (op, pc) => match op with - | .ret narrow => narrow == returnNarrow layouts closed.returnType - | .memory (.staticValue _) => staticLoadMatches 126 (raw.get? pc) sites[pc]? - | .memory (.staticAddress _) => - if (sites[pc]?.bind (·.initializer)).isSome then - staticLoadMatches 127 (raw.get? pc) sites[pc]? else true + | .ldsfld field => staticFieldMatches ldsfldOpcode field (raw.get? pc) sites[pc]? && + staticLoadMatches ldsfldOpcode (raw.get? pc) sites[pc]? + | .ldsflda field => staticFieldMatches ldsfldaOpcode field (raw.get? pc) sites[pc]? && + if (sites[pc]?.bind (·.initializer)).isSome then + staticLoadMatches ldsfldaOpcode (raw.get? pc) sites[pc]? else true + | .stsfld field => staticFieldMatches stsfldOpcode field (raw.get? pc) sites[pc]? + | .ldtoken (.field field) => staticFieldMatches ldtokenOpcode field (raw.get? pc) sites[pc]? | _ => true /-- Indexed instruction-shape checks agree with direct list lookups at every emitted pc. -/ -theorem methodShapeInstructions_eq_scan (layouts : List RawLayout) (closed : RawMethod) +theorem methodShapeInstructions_eq_scan (closed : RawMethod) (method : Method) : - methodShapeInstructions layouts closed method = method.code.zipIdx.all (fun (op, pc) => + methodShapeInstructions closed method = method.code.zipIdx.all (fun (op, pc) => match op with - | .ret narrow => narrow == returnNarrow layouts closed.returnType - | .memory (.staticValue _) => staticLoadMatches 126 closed.code[pc]? - ((method.staticSites.find? (fun site => site.1 == pc)).map Prod.snd) - | .memory (.staticAddress _) => - let descriptor := (method.staticSites.find? (fun site => site.1 == pc)).map Prod.snd - if (descriptor.bind (·.initializer)).isSome then - staticLoadMatches 127 closed.code[pc]? descriptor else true + | .ldsfld field => + staticFieldMatches ldsfldOpcode field closed.code[pc]? (method.staticAt pc) && + staticLoadMatches ldsfldOpcode closed.code[pc]? (method.staticAt pc) + | .ldsflda field => + staticFieldMatches ldsfldaOpcode field closed.code[pc]? (method.staticAt pc) && + if ((method.staticAt pc).bind (·.initializer)).isSome then + staticLoadMatches ldsfldaOpcode closed.code[pc]? (method.staticAt pc) else true + | .stsfld field => + staticFieldMatches stsfldOpcode field closed.code[pc]? (method.staticAt pc) + | .ldtoken (.field field) => + staticFieldMatches ldtokenOpcode field closed.code[pc]? (method.staticAt pc) | _ => true) := by - simp only [methodShapeInstructions, FlowIndex.get?_ofList, staticSiteIndex_get] + simp only [methodShapeInstructions, FlowIndex.get?_ofList, firstEntryIndex_get] + rfl #print axioms methodShapeInstructions_eq_scan -/-- Bind non-instruction method fields that affect frame setup and return behavior. -/ -def MethodShapeValid (layouts : List RawLayout) (closed : RawMethod) (method : Method) : Prop := +/-- Bind frame and return metadata, static-field instructions and all raw prefix sequences. -/ +def MethodShapeValid (closed : RawMethod) (method : Method) : Prop := method.profile.Valid ∧ method.returnsValue = (closed.returnType != .void) ∧ - method.locals = method.localKinds.map (fun kind => - if closed.initLocals then initialLocal kind else .unmodeled) ∧ - method.vectorLocals = [] ∧ method.vectorArgs = [] ∧ - methodShapeInstructions layouts closed method = true + method.locals = method.localKinds.map (LocalKind.initialValue closed.initLocals) ∧ + methodShapeInstructions closed method = true ∧ + prefixSequenceValid closed.returnType closed.code = true + +instance (closed : RawMethod) (method : Method) : + Decidable (MethodShapeValid closed method) := + inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _)) + +/-- The method's declared signature is the one its closed metadata implies. -/ +def DeclaredValid (closed : RawMethod) (method : Method) : Prop := + method.declared = DeclaredSignature.ofRaw closed + +-- Generated proofs use `rfl`, which compares the two signature literals once per method. +instance (closed : RawMethod) (method : Method) : Decidable (DeclaredValid closed method) := + inferInstanceAs (Decidable (_ = _)) + +/-- The method's environment is the program's layout tables, so every method of one extraction + shares it. -/ +def MethodEnvironmentValid (rawLayouts : List RawLayout) (layouts : Array ComputedLayout) + (method : Method) : Prop := + method.environment = { rawLayouts := rawLayouts, layouts := layouts } + +/-- Valid signature and environment metadata determine return narrowing independently of code. -/ +theorem returnNarrow_of_shape {rawLayouts : List RawLayout} + {layouts : Array ComputedLayout} {closed : RawMethod} {method : Method} + (shape : MethodShapeValid closed method) (declared : DeclaredValid closed method) + (environment : MethodEnvironmentValid rawLayouts layouts method) : + method.returnNarrow = declaredNarrow rawLayouts closed.returnType := by + have returns := shape.2.1 + change method.declared = _ at declared + change method.environment = _ at environment + by_cases void : closed.returnType = .void + · simp [Method.returnNarrow, returns, void, declaredNarrow, typeStorage, typeStorageAt] + · simp [Method.returnNarrow, returns, void, declared, DeclaredSignature.ofRaw, environment] + +#print axioms returnNarrow_of_shape + +/-- A list an option-valued function maps entirely holds at each index the image of the input + there. -/ +private theorem getElem?_of_mapM {α β : Type} {make : α → Option β} : + ∀ {inputs : List α} {outputs : List β}, inputs.mapM make = some outputs → + ∀ index : Nat, outputs[index]? = inputs[index]?.bind make + | [], outputs, made, index => by + simp only [List.mapM_nil, Option.pure_def, Option.some.injEq] at made + subst made + simp + | input :: rest, outputs, made, index => by + simp only [List.mapM_cons, Option.bind_eq_bind, Option.bind_eq_some_iff, Option.pure_def, + Option.some.injEq] at made + obtain ⟨output, first, others, remaining, rfl⟩ := made + cases index with + | zero => simp [first] + | succ index => simp [getElem?_of_mapM remaining index] + +/-- Substituting type arguments into an operand keeps every branch destination. -/ +theorem substituteOperand_branchesTo {arguments : List TypeExpr} {layouts : List RawLayout} + {arity : Nat} {instruction : RawInstruction} {operand : RawOperand} + (substituted : substituteOperand arguments layouts arity instruction.operand = some operand) + (target : Nat) : + branchesTo target { instruction with operand } = branchesTo target instruction := by + rcases instruction with ⟨offset, opcode, original⟩ + cases original + case target destination => + simp only [substituteOperand, Option.some.injEq] at substituted + subst substituted; rfl + case targets destinations => + simp only [substituteOperand, Option.some.injEq] at substituted + subst substituted; rfl + all_goals + simp only [branchesTo] + cases operand <;> try rfl + all_goals (try simp [substituteOperand, Option.bind_eq_some_iff] at substituted) + all_goals + obtain ⟨_, _, _, _, _, _, _, _, rest⟩ := substituted + split at rest <;> simp [Option.bind_eq_some_iff] at rest + +#print axioms substituteOperand_branchesTo + +/-- Instantiation keeps every opcode and branch destination, so it keeps `PrefixEntryFree`. -/ +theorem instantiateMethod_prefixEntryFree {layouts : List RawLayout} {original closed : RawMethod} + {arguments : List TypeExpr} + (instantiated : instantiateMethod layouts original arguments = some closed) + (free : PrefixEntryFree original.code) : PrefixEntryFree closed.code := by + unfold instantiateMethod at instantiated + split at instantiated + · cases instantiated + · simp only [Option.bind_eq_bind, Option.bind_eq_some_iff, Option.pure_def, + Option.some.injEq] at instantiated + obtain ⟨_, _, _, _, _, _, code, substituted, rfl⟩ := instantiated + have entries := getElem?_of_mapM substituted + have closedAt : ∀ (index : Nat) (instruction : RawInstruction), + code[index]? = some instruction → + ∃ source operand, original.code[index]? = some source ∧ + substituteOperand arguments layouts original.genericParameters.length source.operand = + some operand ∧ instruction = { source with operand } := by + intro index instruction found + rw [entries index] at found + cases source : original.code[index]? with + | none => simp [source] at found + | some source' => + simp only [source, Option.bind_some, Option.bind_eq_some_iff, Option.some.injEq] at found + obtain ⟨operand, substitutedHere, rfl⟩ := found + exact ⟨source', operand, rfl, substitutedHere, rfl⟩ + intro pc instruction fetched isPrefix + obtain ⟨source, _, sourceAt, _, rfl⟩ := closedAt pc instruction fetched + have clear := free pc source sourceAt isPrefix + rw [List.any_eq_false] at clear ⊢ + intro other member branches + obtain ⟨position, otherAt⟩ := List.mem_iff_getElem?.mp member + obtain ⟨otherSource, operand, otherSourceAt, otherSubstituted, rfl⟩ := + closedAt position other otherAt + rw [substituteOperand_branchesTo otherSubstituted] at branches + exact clear otherSource (List.mem_of_getElem? otherSourceAt) branches + +#print axioms instantiateMethod_prefixEntryFree + +/-- A storage native dispatch chooses is a callable that never narrows, for a pointer type, + which never narrows either. -/ +private theorem nativeDispatchStorage_narrow (context : NativeDispatchContext) (type : TypeExpr) + (storage : StorageType) (chosen : nativeDispatchStorage context type = some storage) : + storage.narrow = none ∧ type.narrowCandidate = false := by + unfold nativeDispatchStorage at chosen + cases signature : context.signature with + | none => simp [signature] at chosen + | some value => + simp only [signature, Option.bind_eq_bind, Option.bind_some] at chosen + cases type <;> simp_all [Option.any, TypeExpr.narrowCandidate] <;> + (try split at chosen) <;> simp_all [StorageType.narrow] + all_goals (obtain ⟨_, rfl⟩ := chosen; rfl) + +/-- The storage a specialization gives a parameter narrows exactly as its declared type does. -/ +theorem parameterStorage_narrow (layouts : List RawLayout) (context : NativeDispatchContext) + (type : TypeExpr) (storage : StorageType) + (stored : (nativeDispatchStorage context type <|> typeStorage layouts type) = some storage) : + storage.narrow = declaredNarrow layouts type := by + cases native : nativeDispatchStorage context type with + | some chosen => + rw [native] at stored + simp only [Option.some.injEq, HOrElse.hOrElse, OrElse.orElse, Option.orElse] at stored + subst stored + obtain ⟨wide, shape⟩ := nativeDispatchStorage_narrow context type chosen native + rw [wide, declaredNarrow_of_not_candidate _ _ shape] + | none => + rw [native] at stored + simp only [HOrElse.hOrElse, OrElse.orElse, Option.orElse] at stored + rw [declaredNarrow_eq_storage, stored] + rfl -instance (layouts : List RawLayout) (closed : RawMethod) (method : Method) : - Decidable (MethodShapeValid layouts closed method) := - inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _)) +#print axioms parameterStorage_narrow + +/-- Under validated evidence an argument without a home is read narrowed exactly as its raw + storage narrows, so `ldarg` reads it as the evidence check's storage says. -/ +theorem argumentAccess_of_evidence {rawLayouts : List RawLayout} {layouts : Array ComputedLayout} + {original closed : RawMethod} {typeArguments : List TypeExpr} {raw : RawArguments} + {reachable : List Bool} {method : Method} {index : Nat} {type : StorageType} + (specialized : SpecializationValid rawLayouts original typeArguments closed raw) + (declared : DeclaredValid closed method) + (environment : MethodEnvironmentValid rawLayouts layouts method) + (homes : ParameterHomesValid layouts raw reachable method) + (unhomed : index ∉ requiredHomeIndices raw reachable) + (found : raw.arguments[index]? = some type) : + method.argumentAccess index = .value type.narrow := by + have absent : method.parameterHomes.findIdx? (fun home => home.1 == index) = none := by + rw [homeKeys_findIdx? homes.2.2.2.2.1, List.findIdx?_eq_none_iff] + intro required member + rw [beq_eq_false_iff_ne] + intro same + exact unhomed (same ▸ member) + change method.declared = _ at declared + change method.environment = _ at environment + have closedEq := specialized.2 + rw [closedArguments_eq_list] at closedEq + simp only [closedArgumentsWith, Option.bind_eq_some_iff, bind, pure, Option.some.injEq] + at closedEq + obtain ⟨parameters, mapped, _, _, _, _, rfl⟩ := closedEq + simp only [Method.argumentAccess, absent, declared, environment, DeclaredSignature.ofRaw] + congr 1 + have entry := getElem?_of_mapM mapped + by_cases receiver : closed.hasThis + · simp only [receiver, ite_true] at found ⊢ + cases index with + | zero => + simp only [List.cons_append, List.getElem?_cons_zero, Option.some.injEq] at found + subst found + split <;> rfl + | succ position => + simp only [List.cons_append, List.getElem?_cons_succ, List.nil_append] at found + rw [entry position] at found + simp only [DeclaredSignature.argumentType] + cases parameter : closed.parameters[position]? with + | none => simp [parameter] at found + | some declaredType => + simp only [parameter, Option.bind_some] at found ⊢ + exact (parameterStorage_narrow _ _ _ _ found).symm + · simp only [receiver, Bool.false_eq_true, ite_false, List.nil_append] at found ⊢ + rw [entry index] at found + simp only [DeclaredSignature.argumentType] + cases parameter : closed.parameters[index]? with + | none => simp [parameter] at found + | some declaredType => + simp only [parameter, Option.bind_some] at found ⊢ + exact (parameterStorage_narrow _ _ _ _ found).symm + +#print axioms argumentAccess_of_evidence + +-- Generated proofs use `rfl`, which unfolds the two shared constants; deciding this instance +-- compares the layout tables element by element, once per method. +instance (rawLayouts : List RawLayout) (layouts : Array ComputedLayout) (method : Method) : + Decidable (MethodEnvironmentValid rawLayouts layouts method) := + inferInstanceAs (Decidable (_ = _)) /-- One emitted method against its evidence record: every per-method validator, in the - order generated modules supply their proofs. -/ + order generated modules supply their proofs. `pins` are the assemblies the extraction pins, + shared by every method. -/ def MethodEvidenceValid (rawLayouts : List RawLayout) (layouts : Array ComputedLayout) - (all : List MethodEvidence) (imports : List ImportSlot) (item : MethodEvidence) - (method : Method) : Prop := + (pins : List AssemblyPin) (platform : PlatformTable) (all : List MethodEvidence) + (imports : List ImportSlot) + (item : MethodEvidence) (method : Method) : Prop := SpecializationValid rawLayouts item.original item.arguments item.closed item.parameterInput ∧ ParameterHomesValid layouts item.parameterInput item.reachable method ∧ MethodLayoutsValid rawLayouts layouts method item.layoutUses ∧ - FeatureFlowValid rawLayouts.toArray (evidenceConstants all) item.closed method.profile - item.featureStates item.reachable method.code ∧ - ManagedCallsValid (specializationTable all) imports + FeatureFlowValid rawLayouts.toArray (evidenceConstants all) (linkIndex method) item.closed + method.profile item.featureStates item.reachable method.code ∧ + ManagedCallsValid (specializationTable all) imports (callModelled method) (lowerConstrainedRaw item.dispatchTypes item.closed) item.reachable method ∧ LayoutUsesComplete rawLayouts item.closed item.parameterInput item.reachable item.layoutUses ∧ - MethodShapeValid rawLayouts item.closed method ∧ - UnsafeCallsValid rawLayouts item.closed item.reachable method.code ∧ - ExactIntrinsicsValid item.closed item.reachable method ∧ + MethodShapeValid item.closed method ∧ + UnsafeCallsValid item.closed item.reachable method.code ∧ + Method.ofClosed? method.profile method.environment item.links item.reachable item.closed + item.parameterInput = .ok method ∧ OpaqueLocalsValid item.closed method ∧ - VectorInitializationsValid item.closed item.reachable method ∧ - ScalarFlowValid rawLayouts.toArray item.closed item.featureStates item.scalarStates method.code ∧ - ConstrainedCallsValid item.dispatchTypes (specializationTable all) item.closed item.reachable method + ObjectOperandsValid item.closed item.reachable method ∧ + (ScalarFlowValid item.closed item.featureStates item.scalarStates method.code ∧ + method.code = + methodCode method.profile method.environment method.links item.reachable item.closed) ∧ + ConstrainedCallsValid item.dispatchTypes (specializationTable all) item.closed item.reachable + method ∧ + BodyDecodes pins platform item.assembly item.body item.original item.arguments item.signature + ((ImportSlotIndex.ofList imports).forLinks all.length method.links) /\ + MethodEnvironmentValid rawLayouts layouts method ∧ + DeclaredValid item.closed method ∧ item.links = method.links instance (rawLayouts : List RawLayout) (layouts : Array ComputedLayout) - (all : List MethodEvidence) (imports : List ImportSlot) (item : MethodEvidence) - (method : Method) : - Decidable (MethodEvidenceValid rawLayouts layouts all imports item method) := - inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _)) + (pins : List AssemblyPin) (platform : PlatformTable) (all : List MethodEvidence) + (imports : List ImportSlot) + (item : MethodEvidence) (method : Method) : + Decidable (MethodEvidenceValid rawLayouts layouts pins platform all imports item method) := + inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ + _)) + +/-- The record supplies exactly the executable links checked for its method. -/ +theorem MethodEvidenceValid.links_eq {rawLayouts layouts pins platform all imports item method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) : + item.links = method.links := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, links⟩ := valid + exact links +#print axioms MethodEvidenceValid.links_eq + +/-- Validated evidence decodes at its own links and mask to the complete executable method. -/ +theorem MethodEvidenceValid.decoded {rawLayouts layouts pins platform all imports item method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) : + Method.ofClosed? method.profile method.environment item.links item.reachable item.closed + item.parameterInput = .ok method := by + obtain ⟨-, -, -, -, -, -, -, -, decoded, -⟩ := valid + exact decoded + +#print axioms MethodEvidenceValid.decoded + +/-- A validated method retains the identities, bytes and initializers of its checked static + sites. -/ +theorem MethodEvidenceValid.staticSites_eq {rawLayouts layouts pins platform all imports item method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) : + method.staticSites = item.closed.staticDescriptors := by + exact (Method.ofClosed?_fields valid.decoded).2 + +#print axioms MethodEvidenceValid.staticSites_eq + +/-- Validated evidence runs under a valid feature profile, which its shape check states. -/ +theorem MethodEvidenceValid.profile_valid {rawLayouts layouts pins platform all imports item method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) : + method.profile.Valid := by + obtain ⟨-, -, -, -, -, -, shape, -⟩ := valid + exact shape.1 +#print axioms MethodEvidenceValid.profile_valid + +/-- A validated record binds every emitted instruction to its decoding. -/ +theorem MethodEvidenceValid.code_eq {rawLayouts : List RawLayout} + {layouts : Array ComputedLayout} {pins : List AssemblyPin} {all : List MethodEvidence} + {imports : List ImportSlot} {item : MethodEvidence} {method : Method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) : method.code = + methodCode method.profile method.environment method.links item.reachable item.closed := by + obtain ⟨_, _, _, _, _, _, _, _, _, _, _, ⟨_, same⟩, _⟩ := valid + exact same + +#print axioms MethodEvidenceValid.code_eq + +/-- The runtime catalog agrees with every call of a validated method. -/ +theorem MethodEvidenceValid.modelsAgree {rawLayouts layouts pins platform all imports item method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) : + RuntimeModelsAgree item.closed item.reachable method := by + have decoded := valid.decoded + rw [valid.links_eq] at decoded + obtain ⟨-, -, -, features, -⟩ := valid + exact RuntimeModelsAgree.of_closed decoded features + +#print axioms MethodEvidenceValid.modelsAgree + +/-- Every reachable method-token call outside a fold in validated evidence has a raw claim. -/ +theorem MethodEvidenceValid.siteClaimTotal {rawLayouts : List RawLayout} + {layouts : Array ComputedLayout} {pins : List AssemblyPin} {all : List MethodEvidence} + {imports : List ImportSlot} {item : MethodEvidence} {method : Method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) + {pc : Nat} {raw : RawInstruction} (atRaw : item.closed.code[pc]? = some raw) + (atLive : item.reachable[pc]? = some true) : + SiteClaimTotal method.environment (method.linkAt pc) raw true + ((foldRoles item.closed.code)[pc]?.join) := by + exact valid.modelsAgree.siteClaimTotal atRaw atLive + +#print axioms MethodEvidenceValid.siteClaimTotal + +/-- At a reachable pc that a fold owns, validated evidence emits the instruction its role fixes. -/ +theorem MethodEvidenceValid.foldOwnedSite {rawLayouts : List RawLayout} + {layouts : Array ComputedLayout} {pins : List AssemblyPin} {all : List MethodEvidence} + {imports : List ImportSlot} {item : MethodEvidence} {method : Method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) + {pc : Nat} {raw : RawInstruction} {emitted : Instruction} + (atRaw : item.closed.code[pc]? = some raw) (atLive : item.reachable[pc]? = some true) + (atCode : method.code[pc]? = some emitted) + (owned : foldOwnsSite ((foldRoles item.closed.code)[pc]?.join) = true) : + ((foldRoles item.closed.code)[pc]?.join).bind + (Instruction.ofRole? · none raw) = some emitted := by + exact valid.modelsAgree.foldOwnedSite atRaw atLive atCode owned + +#print axioms MethodEvidenceValid.foldOwnedSite /-- `MethodEvidenceValid` for a program lookup; a missing method fails. -/ private def MethodAtValid (rawLayouts : List RawLayout) (layouts : Array ComputedLayout) - (all : List MethodEvidence) (imports : List ImportSlot) (item : MethodEvidence) - (method : Option Method) : Prop := + (pins : List AssemblyPin) (platform : PlatformTable) (all : List MethodEvidence) + (imports : List ImportSlot) + (item : MethodEvidence) (method : Option Method) : Prop := match method with | none => False - | some value => MethodEvidenceValid rawLayouts layouts all imports item value + | some value => MethodEvidenceValid rawLayouts layouts pins platform all imports item value private instance (rawLayouts : List RawLayout) (layouts : Array ComputedLayout) - (all : List MethodEvidence) (imports : List ImportSlot) (item : MethodEvidence) - (method : Option Method) : - Decidable (MethodAtValid rawLayouts layouts all imports item method) := by + (pins : List AssemblyPin) (platform : PlatformTable) (all : List MethodEvidence) + (imports : List ImportSlot) + (item : MethodEvidence) (method : Option Method) : + Decidable (MethodAtValid rawLayouts layouts pins platform all imports item method) := by cases method <;> dsimp [MethodAtValid] <;> infer_instance /-- The architecture layouts are computed for: the first method's profile, or `.scalar` for @@ -211,47 +507,58 @@ abbrev programArchitecture (program : Program) : Architecture := ((program.head?).map fun method => method.profile.architecture).getD .scalar /-- The certificate covers the entire emitted program, with no optional method list; calls - past it must match `imports`. -/ + past it must match `imports`, and every body is checked against the one list of `pins` and + the one list of `platform` definitions. -/ def TranslationValid (rawLayouts : List RawLayout) (layouts : Array ComputedLayout) - (evidence : List MethodEvidence) (imports : List ImportSlot) (program : Program) : Prop := + (pins : List AssemblyPin) (platform : PlatformTable) (evidence : List MethodEvidence) + (imports : List ImportSlot) (program : Program) : Prop := LayoutsValid rawLayouts layouts (programArchitecture program) ∧ evidence.length = program.length ∧ (∀ index : Fin evidence.length, - MethodAtValid rawLayouts layouts evidence imports evidence[index] program[index.val]?) ∧ + MethodAtValid rawLayouts layouts pins platform evidence imports evidence[index] + program[index.val]?) ∧ StaticValuesValid program ∧ + StaticIdentitiesValid program (evidence.map (·.body)) ∧ ImportedLayoutsValid imports (programArchitecture program) ∧ ImportedLocalLayoutsBound rawLayouts layouts imports ∧ AttributionValid (bodyOwners evidence) = true instance (rawLayouts : List RawLayout) (layouts : Array ComputedLayout) - (evidence : List MethodEvidence) (imports : List ImportSlot) (program : Program) : - Decidable (TranslationValid rawLayouts layouts evidence imports program) := - inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _)) + (pins : List AssemblyPin) (platform : PlatformTable) (evidence : List MethodEvidence) + (imports : List ImportSlot) (program : Program) : + Decidable (TranslationValid rawLayouts layouts pins platform evidence imports program) := + inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _)) /-- Compose independently checked methods without reducing their validators again. -/ inductive MethodsChecked (rawLayouts : List RawLayout) (layouts : Array ComputedLayout) - (all : List MethodEvidence) (imports : List ImportSlot) : + (pins : List AssemblyPin) (platform : PlatformTable) (all : List MethodEvidence) + (imports : List ImportSlot) : List MethodEvidence → Program → Prop where - | nil : MethodsChecked rawLayouts layouts all imports [] [] + | nil : MethodsChecked rawLayouts layouts pins platform all imports [] [] | cons {item rest method program} : - MethodEvidenceValid rawLayouts layouts all imports item method → - MethodsChecked rawLayouts layouts all imports rest program → - MethodsChecked rawLayouts layouts all imports (item :: rest) (method :: program) + MethodEvidenceValid rawLayouts layouts pins platform all imports item method → + MethodsChecked rawLayouts layouts pins platform all imports rest program → + MethodsChecked rawLayouts layouts pins platform all imports (item :: rest) + (method :: program) /-- Assembles `TranslationValid` from the program-wide checks and per-method proofs. -/ theorem translation_of_methods {rawLayouts : List RawLayout} {layouts : Array ComputedLayout} - {evidence : List MethodEvidence} {imports : List ImportSlot} {program : Program} + {pins : List AssemblyPin} {platform : PlatformTable} {evidence : List MethodEvidence} + {imports : List ImportSlot} + {program : Program} (layout : LayoutsValid rawLayouts layouts (programArchitecture program)) (statics : StaticValuesValid program) + (identities : StaticIdentitiesValid program (evidence.map (·.body))) (importLayouts : ImportedLayoutsValid imports (programArchitecture program)) (importedLocals : ImportedLocalLayoutsBound rawLayouts layouts imports) (attribution : AttributionValid (bodyOwners evidence) = true) - (methods : MethodsChecked rawLayouts layouts evidence imports evidence program) : - TranslationValid rawLayouts layouts evidence imports program := by + (methods : MethodsChecked rawLayouts layouts pins platform evidence imports evidence program) : + TranslationValid rawLayouts layouts pins platform evidence imports program := by have compose {items code} - (checked : MethodsChecked rawLayouts layouts evidence imports items code) : + (checked : MethodsChecked rawLayouts layouts pins platform evidence imports items code) : items.length = code.length ∧ ∀ index : Fin items.length, - MethodAtValid rawLayouts layouts evidence imports items[index] code[index.val]? := by + MethodAtValid rawLayouts layouts pins platform evidence imports items[index] + code[index.val]? := by induction checked with | nil => exact ⟨rfl, fun index => Fin.elim0 index⟩ | @cons item rest method code valid tail ih => @@ -260,15 +567,18 @@ theorem translation_of_methods {rawLayouts : List RawLayout} {layouts : Array Co cases index with | zero => exact valid | succ index => exact ih.2 ⟨index, Nat.lt_of_succ_lt_succ bound⟩ - exact ⟨layout, (compose methods).1, (compose methods).2, statics, importLayouts, + exact ⟨layout, (compose methods).1, (compose methods).2, statics, identities, importLayouts, importedLocals, attribution⟩ /-- Each method of a valid translation passes every per-method validator. -/ theorem translation_method_checked {rawLayouts : List RawLayout} {layouts : Array ComputedLayout} - {evidence : List MethodEvidence} {imports : List ImportSlot} {program : Program} - (valid : TranslationValid rawLayouts layouts evidence imports program) + {pins : List AssemblyPin} {platform : PlatformTable} {evidence : List MethodEvidence} + {imports : List ImportSlot} + {program : Program} + (valid : TranslationValid rawLayouts layouts pins platform evidence imports program) (index : Fin evidence.length) (method : Method) (found : program[index.val]? = some method) : - MethodEvidenceValid rawLayouts layouts evidence imports evidence[index] method := by + MethodEvidenceValid rawLayouts layouts pins platform evidence imports evidence[index] + method := by have checked := valid.2.2.1 index simpa [MethodAtValid, found] using checked diff --git a/CIL/Extraction/TypeStorage.lean b/CIL/Extraction/TypeStorage.lean new file mode 100644 index 000000000..40caf2c17 --- /dev/null +++ b/CIL/Extraction/TypeStorage.lean @@ -0,0 +1,226 @@ +import CIL.Extraction.Parameters +import CIL.Extraction.MetadataText + +/-! +# Storage from metadata types + +Storage and callable representations of resolved metadata types used by extraction and execution. +-/ + +namespace CIL.Extraction + +/-- The exact `System.Runtime.Intrinsics` assembly identity; `runtimeScope` is in `TypeExpr`. -/ +def intrinsicsScope : String := + metadata_text% + "System.Runtime.Intrinsics, Version=11.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" + +/-- Byte width of an admitted integer vector, bound to both exact runtime type identities. -/ +def integerVectorWidth : TypeExpr → Option Nat + | .apply (.constructor scope name) (.atom elementScope element) => + if scope != intrinsicsScope || elementScope != runtimeScope || + element ∉ ["System.Byte", "System.Int32", "System.UInt32", "System.Int64", + "System.UInt64"] then none + else if name == "System.Runtime.Intrinsics.Vector128`1" then some 16 + else if name == "System.Runtime.Intrinsics.Vector256`1" then some 32 + else if name == "System.Runtime.Intrinsics.Vector512`1" then some 64 + else none + | _ => none + +/-- A closed object operand may retain a type application for a runtime span or an integer + vector. Other admitted generic structs are represented by their layout index. -/ +def objectApplicationAdmitted : TypeExpr → Bool + | type@(.apply ..) => type.isRuntimeSpan || (integerVectorWidth type).isSome + | _ => true + +/-- Primitive array element identity is derived from the exact runtime metadata scope. -/ +def arrayElementType (type : TypeExpr) (aggregateWidth : Nat := 0) : Option ArrayElement := + match type with + | .aggregate identity _ => some (.aggregate identity aggregateWidth) + | type@(.apply (.constructor _ name) (.atom _ element)) => do + let width ← integerVectorWidth type + return .vector (name ++ "<" ++ element ++ ">") width + | .enum identity (.atom scope name) => do + if scope != runtimeScope then none else pure () + let storage ← match name with + | "System.SByte" => some EnumStorage.signed8 | "System.Byte" => some .unsigned8 + | "System.Int16" => some .signed16 | "System.UInt16" => some .unsigned16 + | "System.Int32" => some .signed32 | "System.UInt32" => some .unsigned32 + | "System.Int64" => some .signed64 | "System.UInt64" => some .unsigned64 + | _ => none + return .enumeration identity storage + | .atom scope name => + if scope != runtimeScope then none else match name with + | "System.SByte" => some .signed8 | "System.Byte" => some .unsigned8 + | "System.Int16" => some .signed16 | "System.UInt16" => some .unsigned16 + | "System.Int32" => some .signed32 | "System.UInt32" => some .unsigned32 + | "System.Int64" => some .signed64 | "System.UInt64" => some .unsigned64 + | "System.IntPtr" => some .nativeInt | "System.UIntPtr" => some .nativeUInt + | "System.Boolean" => some .boolean | "System.Char" => some .character + | _ => none + | _ => none +/-- Primitive/vector widths are intrinsic; aggregate widths come from checked layout uses. -/ +def spanElementWidth (type : TypeExpr) (aggregateWidth : Nat := 0) : Option Nat := + if let .aggregate .. := type then some aggregateWidth else + ((arrayElementType type).map ArrayElement.width).orElse (fun _ => integerVectorWidth type) + +/-- Exact span metadata, including element width and write capability. -/ +def spanStorage (type : TypeExpr) (aggregateWidth : Nat := 0) : Option StorageType := + match type with + | .apply (.constructor scope name) elementType => do + if scope != runtimeScope || + name ∉ ["System.Span`1", "System.ReadOnlySpan`1"] then none else + let width ← spanElementWidth elementType aggregateWidth + return .span width (name == "System.Span`1") + | _ => none + +/-- Whether a local has a supported span metadata shape; layouts are validated separately. -/ +def spanLocal (type : TypeExpr) : Bool := (spanStorage type).isSome + +/-- Callable identities retain scoped aggregate names, independent of layout-table ordering. + Argument storage still validates the original index against the checked layout metadata. -/ +def callableType : TypeExpr → TypeExpr + | .aggregate identity _ => .aggregate identity 0 + | .byref element => .byref (callableType element) + | .apply constructor argument => .apply (callableType constructor) (callableType argument) + | .functionPointer result parameters convention hasThis explicitThis => + .functionPointer (callableType result) (callableType parameters) convention hasThis + explicitThis + | .signatureParameter head tail => .signatureParameter (callableType head) (callableType tail) + | type => type + +/-- Function pointers admit checked aggregates, integer vectors and integer/reference types. -/ +def managedParameter : TypeExpr → Bool + | .atom scope name => scope == runtimeScope && (primitiveField name).isSome + | .enum _ underlying => (enumUnderlyingField underlying).isSome + | .byref element => match element with + | .atom .. | .enum .. | .aggregate .. | .apply .. => managedParameter element + | _ => false + | .aggregate .. => true + | type => (integerVectorWidth type).isSome + +/-- Decode the ordered parameter chain without accepting another metadata constructor. -/ +def managedParameters : TypeExpr → Option (List TypeExpr) + | .void => some [] + | .signatureParameter head tail => do + if !managedParameter head then none else pure () + return callableType head :: (← managedParameters tail) + | _ => none + +/-- Only the exact static managed convention gives callable provenance. -/ +def managedSignature : TypeExpr → Option ManagedSignature + | .functionPointer result parameters 0 false false => do + if result != .void && !managedParameter result then none else pure () + return ⟨callableType result, ← managedParameters parameters⟩ + | _ => none + +/-- Raw callable types must bind every aggregate index before their identity is canonicalized. -/ +def callableLayoutsAt (layoutAt : Nat → Option RawLayout) : TypeExpr → Bool + | .aggregate identity index => (layoutAt index).any (·.identity == identity) + | .byref element => callableLayoutsAt layoutAt element + | .functionPointer result parameters _ _ _ => + callableLayoutsAt layoutAt result && callableLayoutsAt layoutAt parameters + | .signatureParameter head tail => + callableLayoutsAt layoutAt head && callableLayoutsAt layoutAt tail + | _ => true + +/-- Callable metadata binds every aggregate index in the declared layout list. -/ +def callableLayoutsValid (layouts : List RawLayout) : TypeExpr → Bool := + callableLayoutsAt (layouts[·]?) + +/-- Four-byte groups bound the nesting of equality decisions on long metadata identities. -/ +private def equalBytes : List UInt8 → List UInt8 → Bool + | a :: b :: c :: d :: left, w :: x :: y :: z :: right => + bif decide (a = w) && decide (b = x) && decide (c = y) && decide (d = z) + then equalBytes left right else false + | left, right => @decide (left = right) (@List.hasDecEq UInt8 UInt8.decEq left right) + +/-- The grouped comparison accepts exactly equal byte sequences. -/ +private theorem equalBytes_iff (left right : List UInt8) : + equalBytes left right = true ↔ left = right := by + fun_induction equalBytes <;> simp_all [List.cons.injEq, and_assoc] + +#print axioms equalBytes_iff + +/-- Two metadata identities are equal exactly when their UTF-8 bytes agree. -/ +private def identityEq (left right : String) : Decidable (left = right) := + decidable_of_iff (equalBytes left.toByteArray.data.toList right.toByteArray.data.toList = true) + (by rw [equalBytes_iff, Array.toList_inj, ← ByteArray.ext_iff, String.toByteArray_inj]) + +/-- Compiled storage checks use the native string comparison. -/ +@[csimp] private theorem identityEq_eq : identityEq = String.decEq := by + funext left right + apply Subsingleton.elim + +#print axioms identityEq_eq + +/-- Decode storage independently from the exact metadata type identity. -/ +def typeStorageAt (layoutAt : Nat → Option RawLayout) : TypeExpr → Option StorageType + | .atom scope name => + if scope = runtimeScope ∨ scope = coreLibraryScope then .field <$> primitiveField name else none + | .object _ _ => some .object + | .aggregate identity index => do + let metadata ← layoutAt index + letI := identityEq metadata.identity identity + if metadata.identity = identity then return .field (.aggregate index) else none + | .enum _ underlying => StorageType.field <$> enumUnderlyingField underlying + | .byref _ => some .reference + -- An array of class references is itself an object on the heap. + | .array (.object _ _) => some .object + | .array (.aggregate identity index) => do + let metadata ← layoutAt index + if metadata.identity == identity && !metadata.byRefLike then + some (.aggregateArray identity index) else none + | .array element => match managedSignature element with + | some signature => if callableLayoutsAt layoutAt element then some (.methodTable signature) + else none + | none => StorageType.array <$> arrayElementType element + | type@(.functionPointer ..) => if callableLayoutsAt layoutAt type then + StorageType.methodPointer <$> managedSignature type else none + | .pointer (.atom scope name) => + if scope == runtimeScope && name ∈ ["System.SByte", "System.Byte", "System.Int16", + "System.UInt16", "System.Char", "System.Int32", "System.UInt32", "System.Int64", + "System.UInt64", "System.IntPtr", "System.UIntPtr"] then some .reference else none + | .pointer (.enum _ underlying) => + if (enumUnderlyingField underlying).isSome then some .reference else none + | .pointer (.aggregate identity index) => do + let metadata ← layoutAt index + if metadata.identity == identity then some .reference else none + | .pointer type => if (integerVectorWidth type).isSome then some .reference else none + | type@(.apply (.constructor scope name) elementType) => do + if let some storage := spanStorage type then + if let .aggregate identity index := elementType then + let metadata ← layoutAt index + if metadata.identity != identity then none else + return .aggregateSpan index (name == "System.Span`1") + else return storage + let .atom elementScope element := elementType | none + if scope != intrinsicsScope || elementScope != runtimeScope || + element ∉ ["System.UInt64", "System.Int64", "System.UInt32", "System.Int32", + "System.Byte", "System.Double", "System.Single"] then none + else if name = "System.Runtime.Intrinsics.Vector128`1" then some .vector128 + else if name = "System.Runtime.Intrinsics.Vector256`1" then some .vector256 + else if name = "System.Runtime.Intrinsics.Vector512`1" then some .vector512 else none + | _ => none + +/-- Storage of a type under the supplied raw metadata. -/ +def typeStorage (layouts : List RawLayout) : TypeExpr → Option StorageType := + typeStorageAt (layouts[·]?) + +/-- The struct field tag a field holding `type` is declared with: a primitive's kind, an enum's + underlying kind, a checked struct's index, or an integer vector's width and element lane. -/ +def fieldKind? (layouts : List RawLayout) (type : TypeExpr) : Option FieldType := + match typeStorage layouts type, type with + | some (.field kind), _ => some kind + -- `typeStorage` has decided both scopes; the element's name decides the lane. + | some .vector128, .apply _ (.atom _ element) => + FieldType.vector128 <$> (primitiveField element).bind VectorLane.ofField? + | some .vector256, .apply _ (.atom _ element) => + FieldType.vector256 <$> (primitiveField element).bind VectorLane.ofField? + | _, _ => none + +/-- Storage accepted by Unsafe reference casts and element offsets. -/ +def StorageType.isFieldOrVector : StorageType → Bool + | .field _ | .vector128 | .vector256 | .vector512 => true + | _ => false + +end CIL.Extraction diff --git a/CIL/Extraction/UnsafeBlocks.lean b/CIL/Extraction/UnsafeBlocks.lean new file mode 100644 index 000000000..c63c11351 --- /dev/null +++ b/CIL/Extraction/UnsafeBlocks.lean @@ -0,0 +1,58 @@ +import CIL.Extraction.UnsafeModels +import CIL.Extraction.Prefixes + +/-! Runtime models for Unsafe byte-block copies and fills on references and pointers. -/ + +namespace CIL.Extraction + +/-- One static, non-generic overload with its exact address type and external target. -/ +private def blockRow (name : String) (copy pointer : Bool) (operation : MemoryOp) : + ModelOverload := + let byte := TypeExpr.atom runtimeScope "System.Byte" + let address := if pointer then TypeExpr.pointer .void else .byref byte + let addressName := if pointer then "System.Void*" else "System.Byte&" + let sourceName := if copy then addressName else "System.Byte" + let declared : OpenSignature := { convention := 0, parameters := + [address, if copy then address else byte, .atom runtimeScope "System.UInt32"] } + { operand := unsafeOperand name "System.Void" s!"{addressName},{sourceName},System.UInt32" + declared + accepts := fun method => method.owner == unsafeOwner && !method.hasThis && + method.genericArity == 0 && method.methodArguments.isEmpty && method.signature 0 == declared + resolve := fun _ _ => some (.memory operation) } + +/-- The by-reference and pointer overloads of one reserved block helper. -/ +private def blockEntry (key : MetadataName) (name : String) (copy : Bool) (alignment : Nat) : + ModelEntry := + let operation := if copy then MemoryOp.copyBlock alignment else .initBlock alignment + { opcode := .call, owner := unsafeOwner, name := key, ownsName := true, + overloads := [blockRow name copy false operation, blockRow name copy true operation] } + +/-- Aligned block calls require eight-byte alignment and unaligned calls require one byte. -/ +def unsafeBlockEntries : List ModelEntry := + [blockEntry name!"CopyBlock" "CopyBlock" true defaultBlockAlignment, + blockEntry name!"CopyBlockUnaligned" "CopyBlockUnaligned" true 1, + blockEntry name!"InitBlock" "InitBlock" false defaultBlockAlignment, + blockEntry name!"InitBlockUnaligned" "InitBlockUnaligned" false 1] + +/-- Both overloads of each block helper run its checked memory operation. -/ +theorem unsafeBlockEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : + ∀ entry ∈ unsafeBlockEntries, entry.Yields property := by + have yields (key name copy alignment) : + (blockEntry key name copy alignment).Yields property := by + intro reader method model found + let operation := if copy then MemoryOp.copyBlock alignment else .initBlock alignment + let byref := blockRow name copy false operation + let pointer := blockRow name copy true operation + change ((ModelEntry.select?.select method [byref, pointer]).bind + (fun row => row.resolve reader method)) = some model at found + cases href : byref.accepts method <;> cases hptr : pointer.accepts method <;> + simp [ModelEntry.select?.select, href, hptr] at found + all_goals cases found <;> exact memory operation + simp only [unsafeBlockEntries, List.mem_cons, List.not_mem_nil, or_false, forall_eq_or_imp, + forall_eq] + exact ⟨yields _ _ _ _, yields _ _ _ _, yields _ _ _ _, yields _ _ _ _⟩ + +#print axioms unsafeBlockEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/UnsafeCalls.lean b/CIL/Extraction/UnsafeCalls.lean index 90579a5cf..daa595e87 100644 --- a/CIL/Extraction/UnsafeCalls.lean +++ b/CIL/Extraction/UnsafeCalls.lean @@ -1,128 +1,24 @@ -import CIL.Extraction.UnsafeReferences -import CIL.Extraction.UnsafeTransfers -import CIL.Extraction.MarshalCalls -import CIL.Extraction.ViewObservations -import CIL.Extraction.BinaryTransfers +import CIL.Extraction.SpanCalls import CIL.Extraction.ObjectConstruction -import CIL.Extraction.GuardCalls /-! # Unsafe helper calls -`Unsafe.SkipInit`, `Unsafe.NullRef` and `Unsafe.IsNullRef` are translated to dedicated -operations instead of calls. `UnsafeCallsValid` recognizes these calls from raw metadata -alone and requires that each live one emits its operation and that no other instruction -emits any of them. +Live calls retain their operands, including calls whose checked model consumes one value. -/ namespace CIL.Extraction -/-- Unsafe helpers that translate to one dedicated emitted operation each. -/ -inductive UnsafeHelper where - | skipInit | nullRef | isNullRef - deriving DecidableEq, Repr - -/-- The external identity of an `Unsafe` method with the given signature. -/ -private def unsafeTarget (signature : String) : MethodId := - .external (runtimeScope ++ "::" ++ signature) - -/-- One admitted non-reference type argument. -/ -private def admittedArgument (layouts : List RawLayout) : List TypeExpr → Bool - | [type] => match typeStorage layouts type with - | some .reference | none => false - | some _ => true - | _ => false - -/-- Reference helpers use supported stored values, excluding array and span descriptors. -/ -private def admittedReferenceArgument (layouts : List RawLayout) : List TypeExpr → Bool - | [type] => match typeStorage layouts type with - | some (.field _) | some .vector128 | some .vector256 | some .vector512 => true - | _ => false - | _ => false - -/-- Element offsets bind signedness and native stride; aggregate sizes use layout evidence. -/ -def unsafeElementOffsetMatches (layouts : List RawLayout) (raw : RawInstruction) - (emitted : Op) : Bool := - match raw.opcode, raw.operand with - | 40, .call site => - if site.name ∉ ["Add", "Subtract"] || - site.declaringType != .atom runtimeScope "System.Runtime.CompilerServices.Unsafe" then true - else if !unsafeElementOffset raw || !admittedReferenceArgument layouts site.typeArguments then - false - else match site.typeArguments, emitted with - | [type], .memory (.add stride signed) => - let expected := fun width => if site.name == "Subtract" then subtractionStride width - else width - signed == (site.parameterTypes[1]? != some (.atom runtimeScope "System.UIntPtr")) && - match typeStorage layouts type with - | some (.field (.aggregate _)) => true - | some (.field kind) => (primitiveFieldWidth kind).any fun width => - stride == expected width - | some .vector128 => stride == expected 16 - | some .vector256 => stride == expected 32 - | some .vector512 => stride == expected 64 - | _ => false - | _, _ => false - | _, _ => true - -/-- `helper` when the call has its exact target and an admitted type argument. -/ -private def bound (layouts : List RawLayout) (site : CallSite) (signature : String) - (helper : UnsafeHelper) : Option UnsafeHelper := - if site.target == unsafeTarget signature && admittedArgument layouts site.typeArguments then - some helper - else none - -/-- Recognize the external helper from raw metadata, independently of translation. -/ -def unsafeHelper (layouts : List RawLayout) (instruction : RawInstruction) : Option UnsafeHelper := - match instruction.opcode, instruction.operand with - | 0x28, .call site => - if !(site.declaringType == .atom runtimeScope "System.Runtime.CompilerServices.Unsafe" && - !site.hasThis) then none - else if site.name == "SkipInit" && site.parameterCount == 1 && !site.returnsValue then - bound layouts site "System.Void System.Runtime.CompilerServices.Unsafe::SkipInit(!!0&)" - .skipInit - else if site.name == "NullRef" && site.parameterCount == 0 && site.returnsValue then - bound layouts site "!!0& System.Runtime.CompilerServices.Unsafe::NullRef()" .nullRef - else if site.name == "IsNullRef" && site.parameterCount == 1 && site.returnsValue then - bound layouts site - "System.Boolean System.Runtime.CompilerServices.Unsafe::IsNullRef(!!0&)" .isNullRef - else none - | _, _ => none - -/-- The helper an emitted operation implements, if any. -/ -def emittedUnsafeHelper : Op → Option UnsafeHelper - | .skipInit => some .skipInit - | .memory .nullRef => some .nullRef - | .memory .isNullRef => some .isNullRef - | _ => none - -/-- A recognized call emits exactly its helper operation, which no other instruction may emit. -/ -def UnsafeCallsValid (layouts : List RawLayout) (closed : RawMethod) - (reachable : List Bool) (code : List Op) : Prop := +/-- The reachability and code lists cover each site, and no live call is replaced by a pop. -/ +def UnsafeCallsValid (closed : RawMethod) + (reachable : List Bool) (code : List Instruction) : Prop := closed.code.length = reachable.length ∧ closed.code.length = code.length ∧ - all3 (fun instruction live emitted => !live || - unsafeTransferMatches layouts instruction emitted && - unsafeBlockMatches instruction emitted && - vector128UnsafeMatches instruction emitted && - marshalTransferMatches layouts instruction emitted && - viewObservationMatches instruction emitted && - binaryTransferMatches instruction emitted && - unsafeElementOffsetMatches layouts instruction emitted && - objectConstructorMatches instruction emitted && - guardCallMatches instruction emitted && - if unsafeReferenceRequired instruction emitted then - match unsafeReferenceHelper instruction, instruction.operand with - | some helper, .call site => - admittedReferenceArgument layouts site.typeArguments && helper.matches emitted - | _, _ => false - else if instruction.opcode == 20 && instruction.operand == .none && - emittedUnsafeHelper emitted == some .nullRef then true - else unsafeHelper layouts instruction == emittedUnsafeHelper emitted) + all3 (fun instruction live emitted => !live || callEmitsNoPop instruction emitted) closed.code reachable code = true -instance (layouts : List RawLayout) (closed : RawMethod) - (reachable : List Bool) (code : List Op) : - Decidable (UnsafeCallsValid layouts closed reachable code) := +instance (closed : RawMethod) + (reachable : List Bool) (code : List Instruction) : + Decidable (UnsafeCallsValid closed reachable code) := inferInstanceAs (Decidable (_ ∧ _ ∧ _)) end CIL.Extraction diff --git a/CIL/Extraction/UnsafeModels.lean b/CIL/Extraction/UnsafeModels.lean new file mode 100644 index 000000000..52266671e --- /dev/null +++ b/CIL/Extraction/UnsafeModels.lean @@ -0,0 +1,140 @@ +import CIL.Extraction.ModelTable +import CIL.Resolution + +/-! +# Unsafe helper models + +The runtime models of the generic `System.Runtime.CompilerServices.Unsafe` helpers a call keeps. +Each entry covers one name of the exact `System.Runtime` owner, so a helper of another owner or +assembly, or of the owner applied to type arguments, has no model. Its resolve selects the +overload from the closed reference: a static reference of generic arity one whose parameters and +result are the overload's, rebuilt from its type argument. The element is read through +`LayoutEnvironment.valueShape`, the reader of `sizeof`, so a size comes from the checked layouts +and never from the extraction. Every entry requires an element with a size, including those that +read none (the reference comparisons and `SkipInit`): an element without one (a span, a generic +parameter, a pointer, a class, a floating-point type or a struct outside the layouts) has no +model and decodes to a hole. +-/ + +namespace CIL.Extraction + +/-- `System.Runtime.CompilerServices.Unsafe` of the exact `System.Runtime` assembly. -/ +def unsafeOwner : TypeExpr := .atom runtimeScope "System.Runtime.CompilerServices.Unsafe" + +/-- The type argument of a static reference of generic arity one whose owner is exactly + `unsafeOwner`; an owner with type arguments of its own names no helper. -/ +def unsafeArgument (method : MethodRef) : Option TypeExpr := + if method.owner != unsafeOwner || method.hasThis || method.genericArity != 1 then none + else match method.methodArguments with + | [element] => some element + | _ => none + +/-- Whether `method` takes `parameters element` and returns `result element` for an element with a + checked size. -/ +def unsafeSignature (reader : CallLayouts) (method : MethodRef) + (parameters : TypeExpr → List TypeExpr) (result : TypeExpr → TypeExpr) : Bool := + match unsafeArgument method with + | some element => (reader.valueShape element).size?.isSome && + method.parameters == parameters element && method.returnType == result element + | none => false + +/-- `model` when `admitted` holds. -/ +def unsafeGuarded (admitted : Bool) (model : RuntimeModel) : Option RuntimeModel := + if admitted then some model else none + +/-- A guarded model is the model it guards. -/ +theorem unsafeGuarded_eq {admitted : Bool} {model resolved : RuntimeModel} + (found : unsafeGuarded admitted model = some resolved) : resolved = model := by + unfold unsafeGuarded at found + split at found <;> cases found + rfl + +#print axioms unsafeGuarded_eq + +/-- The size of the element of `int Unsafe.SizeOf()`. -/ +def unsafeSizeOf (reader : CallLayouts) (method : MethodRef) : Option Nat := do + let element ← unsafeArgument method + if !method.parameters.isEmpty || method.returnType != .atom runtimeScope "System.Int32" then + none + (reader.valueShape element).size? + +/-- `Unsafe.SizeOf` pushes the size of `T`, as `sizeof T` does. -/ +def unsafeSizeOfModel (reader : CallLayouts) (method : MethodRef) : + Option RuntimeModel := + (unsafeSizeOf reader method).map fun size => .constant (BitVec.ofNat 32 size) + +/-- `bool Unsafe.AreSame(ref T, ref T)` and the two address orders compare their references by + `relation`. -/ +def unsafeCompareModel (relation : ReferenceRelation) (reader : CallLayouts) + (method : MethodRef) : Option RuntimeModel := + let boolean := TypeExpr.atom runtimeScope "System.Boolean" + unsafeGuarded (unsafeSignature reader method + (fun element => [.byref element, .byref element]) (fun _ => boolean)) + (.referenceCompare relation) + +/-- `void Unsafe.SkipInit(out T)` forms its out reference and writes nothing. -/ +def unsafeSkipInitModel (reader : CallLayouts) (method : MethodRef) : + Option RuntimeModel := + unsafeGuarded (unsafeSignature reader method (fun element => [.byref element]) + (fun _ => .void)) .skipInit + +/-- The open declaration of a generic Unsafe helper of arity one: the generic convention, and the + helper's parameters and result over its method type parameter `!!0`. -/ +def unsafeDeclaration (parameters : TypeExpr → List TypeExpr) (result : TypeExpr → TypeExpr) : + OpenSignature := + { convention := 0x10, returnType := result (.parameter 0), + parameters := parameters (.parameter 0) } + +/-- The exact external member and open declaration for one Unsafe overload. -/ +def unsafeOperand (name result parameters : String) (declared : OpenSignature) : ModelOperand := + let owner := "System.Runtime.CompilerServices.Unsafe" + let target := s!"{runtimeScope}::{result} {owner}::{name}({parameters})" + .external (fun _ => declared) name (fun _ => some target) + +/-- The reserved `call` entry for a single Unsafe overload. -/ +def unsafeEntry (key : MetadataName) (name result parameters : String) (declared : OpenSignature) + (resolve : CallLayouts → MethodRef → Option RuntimeModel) : ModelEntry := + .single .call unsafeOwner key (unsafeOperand name result parameters declared) resolve true + +/-- The open declaration `bool (ref !!0, ref !!0)` of the reference comparisons. -/ +def unsafeComparisonDeclaration : OpenSignature := + unsafeDeclaration (fun element => [.byref element, .byref element]) + (fun _ => .atom runtimeScope "System.Boolean") + +/-- The Unsafe helper entries. -/ +def unsafeEntries : List ModelEntry := + [unsafeEntry name!"SizeOf" "SizeOf" "System.Int32" "" + (unsafeDeclaration (fun _ => []) (fun _ => .atom runtimeScope "System.Int32")) + unsafeSizeOfModel, + unsafeEntry name!"AreSame" "AreSame" "System.Boolean" "!!0&,!!0&" + unsafeComparisonDeclaration (unsafeCompareModel .same), + unsafeEntry name!"IsAddressLessThan" "IsAddressLessThan" "System.Boolean" "!!0&,!!0&" + unsafeComparisonDeclaration + (unsafeCompareModel .less), + unsafeEntry name!"IsAddressGreaterThan" "IsAddressGreaterThan" "System.Boolean" "!!0&,!!0&" + unsafeComparisonDeclaration + (unsafeCompareModel .greater), + unsafeEntry name!"SkipInit" "SkipInit" "System.Void" "!!0&" + (unsafeDeclaration (fun element => [.byref element]) (fun _ => .void)) unsafeSkipInitModel] + +/-- Every model an Unsafe entry resolves is a constant, a reference comparison or `SkipInit`, so a + property those models have holds of them. -/ +theorem unsafeEntries_yields {property : RuntimeModel → Prop} + (constant : ∀ value, property (.constant value)) + (compare : ∀ relation, property (.referenceCompare relation)) + (skipInit : property .skipInit) : + ∀ entry ∈ unsafeEntries, entry.Yields property := by + simp only [unsafeEntries, List.mem_cons, List.not_mem_nil, or_false, forall_eq_or_imp, + forall_eq] + refine ⟨?_, ?_, ?_, ?_, ?_⟩ <;> intro environment method model found <;> + simp only [unsafeEntry, ModelEntry.resolve_single, unsafeSizeOfModel, unsafeCompareModel, + unsafeSkipInitModel, Option.map_eq_some_iff] at found + · rcases found with ⟨_, _, rfl⟩ + exact constant _ + all_goals + rw [unsafeGuarded_eq found] + first | exact compare _ | exact skipInit + +#print axioms unsafeEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/UnsafeOffsets.lean b/CIL/Extraction/UnsafeOffsets.lean new file mode 100644 index 000000000..6e6db662d --- /dev/null +++ b/CIL/Extraction/UnsafeOffsets.lean @@ -0,0 +1,140 @@ +import CIL.Extraction.UnsafeModels +import CIL.Extraction.LayoutUses + +/-! Runtime models for allocation-relative offsets of checked Unsafe references. -/ + +namespace CIL.Extraction + +/-- The two references whose byte displacement is measured. -/ +private def offsetPair (element : TypeExpr) : List TypeExpr := [.byref element, .byref element] + +/-- Byte displacements have native signed integer type. -/ +private def displacement (_ : TypeExpr) : TypeExpr := .atom runtimeScope "System.IntPtr" + +/-- Unsafe.ByteOffset computes a displacement between references to a sized element. -/ +def unsafeByteOffsetModel (reader : CallLayouts) (method : MethodRef) : + Option RuntimeModel := + unsafeGuarded (unsafeSignature reader method offsetPair displacement) (.memory .byteOffset) + +/-- The offset unit and direction of an Unsafe displacement. -/ +private inductive Displacement where + | addElements | subtractElements | addBytes | subtractBytes + +/-- The four closed parameter shapes of the displacement overloads. -/ +private inductive OffsetForm where + | int32 | native | unsigned | pointer + deriving DecidableEq + +/-- The runtime type of the displacement parameter. -/ +private def OffsetForm.offsetName : OffsetForm → String + | .int32 | .pointer => "System.Int32" + | .native => "System.IntPtr" + | .unsigned => "System.UIntPtr" + +/-- The input and result address type of the overload. -/ +private def OffsetForm.address (form : OffsetForm) (element : TypeExpr) : TypeExpr := + if form == .pointer then .pointer .void else .byref element + +/-- The parameter types of the overload at its method argument. -/ +private def OffsetForm.parameters (form : OffsetForm) (element : TypeExpr) : List TypeExpr := + [form.address element, .atom runtimeScope form.offsetName] + +/-- The method has this overload's static generic signature on the exact Unsafe owner. -/ +private def OffsetForm.accepts (form : OffsetForm) (method : MethodRef) : Bool := + (unsafeArgument method).any fun element => + method.parameters == form.parameters element && method.returnType == form.address element + +/-- The checked operation for the element size and signedness of the displacement. -/ +private def Displacement.model (kind : Displacement) (signed : Bool) (width : Nat) : + RuntimeModel := + match kind with + | .addElements => .memory (.add width signed) + | .subtractElements => .memory (.add (subtractionStride width) signed) + | .addBytes => .memory (.add 1 signed) + | .subtractBytes => .referenceSubtract + +/-- One selected overload binds its open MemberRef and reads the checked size of its element. -/ +private def offsetOverload (name : String) (kind : Displacement) (form : OffsetForm) : + ModelOverload := + let address := if form == .pointer then "System.Void*" else "!!0&" + { operand := unsafeOperand name address s!"{address},{form.offsetName}" + (unsafeDeclaration form.parameters form.address) + accepts := form.accepts + resolve := fun reader method => do + let element ← unsafeArgument method + let width ← (reader.valueShape element).size? + return kind.model (form != .unsigned) width } + +/-- An offset family reserves every closed reference of its name, including unresolved members. -/ +private def offsetEntry (key : MetadataName) (name : String) (kind : Displacement) : ModelEntry := + let forms : List OffsetForm := match kind with + | .addElements | .subtractElements => [.int32, .native, .unsigned, .pointer] + | .addBytes | .subtractBytes => [.native, .unsigned] + { opcode := .call, owner := unsafeOwner, name := key, ownsName := true, + overloads := forms.map (offsetOverload name kind) } + +/-- Whether scalar flow may use the managed-reference stack kind for a void-pointer offset. -/ +def pointerDisplacementSite (site : CallSite) : Bool := + site.method.returnType == .pointer .void && + ((site.method.name == name!"Add" && + (offsetEntry name!"Add" "Add" .addElements).bindsSite site) || + (site.method.name == name!"Subtract" && + (offsetEntry name!"Subtract" "Subtract" .subtractElements).bindsSite site)) + +/-- The checked offset operations that keep their call. -/ +def unsafeOffsetEntries : List ModelEntry := + [unsafeEntry name!"ByteOffset" "ByteOffset" "System.IntPtr" "!!0&,!!0&" + (unsafeDeclaration offsetPair displacement) unsafeByteOffsetModel, + offsetEntry name!"Add" "Add" .addElements, + offsetEntry name!"Subtract" "Subtract" .subtractElements, + offsetEntry name!"AddByteOffset" "AddByteOffset" .addBytes, + offsetEntry name!"SubtractByteOffset" "SubtractByteOffset" .subtractBytes] + +/-- A unique overload selected from a family's rows belongs to that family. -/ +private theorem selected_mem (rows : List ModelOverload) (method : MethodRef) + (row : ModelOverload) (found : ModelEntry.select?.select method rows = some row) : + row ∈ rows := by + induction rows with + | nil => cases found + | cons first rest ih => + simp only [ModelEntry.select?.select] at found + split at found + · split at found + · contradiction + · cases found + exact List.mem_cons_self + · exact List.mem_cons_of_mem _ (ih found) + +#print axioms selected_mem + +/-- Every displacement overload executes a memory offset or reference subtraction. -/ +private theorem offsetEntry_yields {property : RuntimeModel → Prop} + (memory : ∀ op, property (.memory op)) (subtract : property .referenceSubtract) + (key name kind) : (offsetEntry key name kind).Yields property := by + intro reader method model found + obtain ⟨row, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + have member := selected_mem _ _ _ selected + change row ∈ List.map _ _ at member + obtain ⟨form, _, rfl⟩ := List.mem_map.mp member + simp only [offsetOverload, bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at resolved + obtain ⟨_, _, _, _, rfl⟩ := resolved + cases kind <;> first | exact memory _ | exact subtract + +#print axioms offsetEntry_yields + +/-- The offset entries execute checked memory operations or reference subtraction. -/ +theorem unsafeOffsetEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ op, property (.memory op)) (subtract : property .referenceSubtract) : + ∀ entry ∈ unsafeOffsetEntries, entry.Yields property := by + simp only [unsafeOffsetEntries, List.mem_cons, List.not_mem_nil, or_false, forall_eq_or_imp, + forall_eq] + refine ⟨?_, offsetEntry_yields memory subtract _ _ _, offsetEntry_yields memory subtract _ _ _, + offsetEntry_yields memory subtract _ _ _, offsetEntry_yields memory subtract _ _ _⟩ + intro environment method model found + rw [unsafeGuarded_eq (admitted := unsafeSignature environment method offsetPair displacement) + (model := .memory .byteOffset) found] + exact memory _ + +#print axioms unsafeOffsetEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/UnsafeReferences.lean b/CIL/Extraction/UnsafeReferences.lean deleted file mode 100644 index 8b7858d9d..000000000 --- a/CIL/Extraction/UnsafeReferences.lean +++ /dev/null @@ -1,109 +0,0 @@ -import CIL.Extraction.Generics - -/-! -# Exact Unsafe reference helpers - -Runtime reference observations and byte offsets reuse checked allocation-relative operations. -The call identity, closed signature and offset signedness are bound independently of emission. --/ - -namespace CIL.Extraction - -/-- Reference helpers whose runtime signatures have checked allocation-relative meanings. -/ -inductive UnsafeReferenceHelper where - | areSame | less | greater | byteOffset | addBytes (signed : Bool) | subtractBytes | asRef - deriving DecidableEq, Repr - -/-- Exact runtime identity and closed types for a generic Unsafe reference helper. -/ -private def referenceSignature (site : CallSite) (result : TypeExpr) - (parameters : List TypeExpr) (signature : String) : Bool := - site.declaringType == .atom runtimeScope "System.Runtime.CompilerServices.Unsafe" && - !site.hasThis && site.parameterCount == parameters.length && site.returnsValue && - site.returnType == result && site.parameterTypes == parameters && - site.target == .external (runtimeScope ++ "::" ++ signature) - -/-- Recognize exact helper metadata before checking its admitted storage type argument. -/ -def unsafeReferenceHelper (instruction : RawInstruction) : Option UnsafeReferenceHelper := do - if instruction.opcode != 0x28 then none else - let .call site := instruction.operand | none - let [type] := site.typeArguments | none - let reference := TypeExpr.byref type - let native := TypeExpr.atom runtimeScope "System.IntPtr" - let unsigned := TypeExpr.atom runtimeScope "System.UIntPtr" - let boolean := TypeExpr.atom runtimeScope "System.Boolean" - let pair := [reference, reference] - if site.name == "AsRef" && - (referenceSignature site reference [.pointer .void] - "!!0& System.Runtime.CompilerServices.Unsafe::AsRef(System.Void*)" || - referenceSignature site reference [reference] - "!!0& System.Runtime.CompilerServices.Unsafe::AsRef(!!0&)") then some .asRef - else if site.name == "AreSame" && referenceSignature site boolean pair - "System.Boolean System.Runtime.CompilerServices.Unsafe::AreSame(!!0&,!!0&)" then - some .areSame - else if site.name == "IsAddressLessThan" && referenceSignature site boolean pair - "System.Boolean System.Runtime.CompilerServices.Unsafe::IsAddressLessThan(!!0&,!!0&)" then - some .less - else if site.name == "IsAddressGreaterThan" && referenceSignature site boolean pair - "System.Boolean System.Runtime.CompilerServices.Unsafe::IsAddressGreaterThan(!!0&,!!0&)" then - some .greater - else if site.name == "ByteOffset" && referenceSignature site native pair - "System.IntPtr System.Runtime.CompilerServices.Unsafe::ByteOffset(!!0&,!!0&)" then - some .byteOffset - else if site.name == "AddByteOffset" && referenceSignature site reference [reference, native] - "!!0& System.Runtime.CompilerServices.Unsafe::AddByteOffset(!!0&,System.IntPtr)" then - some (.addBytes true) - else if site.name == "AddByteOffset" && referenceSignature site reference [reference, unsigned] - "!!0& System.Runtime.CompilerServices.Unsafe::AddByteOffset(!!0&,System.UIntPtr)" then - some (.addBytes false) - else if site.name == "SubtractByteOffset" && referenceSignature site reference [reference, native] - "!!0& System.Runtime.CompilerServices.Unsafe::SubtractByteOffset(!!0&,System.IntPtr)" then - some .subtractBytes - else if site.name == "SubtractByteOffset" && - referenceSignature site reference [reference, unsigned] - "!!0& System.Runtime.CompilerServices.Unsafe::SubtractByteOffset(!!0&,System.UIntPtr)" then - some .subtractBytes - else none - -/-- The exact existing operation implementing each reference helper. -/ -def UnsafeReferenceHelper.matches : UnsafeReferenceHelper → Op → Bool - | .areSame, .eq | .less, .ltu | .greater, .gtu | .byteOffset, .memory .byteOffset - | .subtractBytes, .sub | .asRef, .memory .asRef => true - | .addBytes expected, .memory (.add width signed) => width == 1 && signed == expected - | _, _ => false - -/-- Pointer offsets use int32 element displacements and retain checked reference provenance. -/ -def unsafePointerOffset (instruction : RawInstruction) : Bool := Id.run do - if instruction.opcode != 0x28 then return false - let .call site := instruction.operand | return false - if site.typeArguments.length != 1 || site.name ∉ ["Add", "Subtract"] then return false - return referenceSignature site (.pointer .void) - [.pointer .void, .atom runtimeScope "System.Int32"] - ("System.Void* System.Runtime.CompilerServices.Unsafe::" ++ site.name ++ - "(System.Void*,System.Int32)") - -/-- Element offsets require exact helper metadata and separately checked layout evidence. -/ -def unsafeElementOffset (instruction : RawInstruction) : Bool := Id.run do - if unsafePointerOffset instruction then return true - if instruction.opcode != 0x28 then return false - let .call site := instruction.operand | return false - let [type] := site.typeArguments | return false - if site.name != "Add" && site.name != "Subtract" then return false - return ["System.Int32", "System.IntPtr", "System.UIntPtr"].any fun offset => - referenceSignature site (.byref type) [.byref type, .atom runtimeScope offset] - ("!!0& System.Runtime.CompilerServices.Unsafe::" ++ site.name ++ "(!!0&," ++ offset ++ ")") - -/-- New helper names and byte-add calls cannot escape exact matching through stack effects. -/ -def unsafeReferenceRequired (instruction : RawInstruction) (emitted : Op) : Bool := - match emitted with - | .memory .byteOffset => true - | .call .. => (unsafeReferenceHelper instruction).isSome - | _ => match instruction.opcode, instruction.operand with - | 0x28, .call site => - (match site.declaringType with - | .atom _ "System.Runtime.CompilerServices.Unsafe" => true - | _ => false) && site.name ∈ ["AreSame", "IsAddressLessThan", "IsAddressGreaterThan", "ByteOffset", - "AddByteOffset", "SubtractByteOffset", "AsRef"] || - match emitted with | .memory (.add ..) => !unsafeElementOffset instruction | _ => false - | _, _ => false - -end CIL.Extraction diff --git a/CIL/Extraction/UnsafeReinterpretations.lean b/CIL/Extraction/UnsafeReinterpretations.lean new file mode 100644 index 000000000..6c6ff1232 --- /dev/null +++ b/CIL/Extraction/UnsafeReinterpretations.lean @@ -0,0 +1,115 @@ +import CIL.Extraction.UnsafeModels +import CIL.Extraction.TransferKinds + +/-! # Unsafe reference and value reinterpretations + +The closed member reference selects the overload. A byref cast between field or vector storage +preserves a formed address. +A value cast moves a snapshot of equal width, or a byte into a Boolean. +-/ + +namespace CIL.Extraction + +/-- The two method arguments of a static generic member on the exact Unsafe owner. -/ +private def unsafeTypePair (method : MethodRef) : Option (TypeExpr × TypeExpr) := + if method.owner != unsafeOwner || method.hasThis || method.genericArity != 2 then none + else match method.methodArguments with + | [source, target] => some (source, target) + | _ => none + +/-- The reference cast between two closed types of field or vector storage. A span, array, + class, byref, pointer or type variable has none, and its reference is not the address of + bytes the target can name, so such a site decodes to the hole of its opcode. -/ +private def asOperation (reader : CallLayouts) (method : MethodRef) : Option MemoryOp := do + let (source, target) ← unsafeTypePair method + _root_.guard ([source, target].all (fun type => + (reader.storage type).any StorageType.isFieldOrVector) && + method.parameters == [.byref source] && method.returnType == .byref target) + return .asRef + +/-- BitCast reinterprets equally wide snapshots or the byte representation of a Boolean. -/ +private def bitCastOperation (reader : CallLayouts) (method : MethodRef) : + Option MemoryOp := do + let (source, target) ← unsafeTypePair method + _root_.guard (method.parameters == [source] && method.returnType == target) + if source == .atom runtimeScope "System.Byte" && target == .atom runtimeScope "System.Boolean" + then return .bitcastByteBool + let .snapshot fromShape ← transferKind? reader source | none + let .snapshot toShape ← transferKind? reader target | none + _root_.guard (fromShape.width == toShape.width) + return .bitcastValue fromShape toShape + +/-- One generic declaration of the reinterpretation family over its method variables. -/ +private def reinterpretDeclaration (parameter result : TypeExpr) : OpenSignature := + { convention := 0x10, parameters := [parameter], returnType := result } + +/-- The two-argument reference and value casts. Each resolves its own checked memory operation. -/ +def unsafeReinterpretEntries : List ModelEntry := + let source := TypeExpr.parameter 0 + let target := TypeExpr.parameter 1 + [unsafeEntry name!"As" "As" "!!1&" "!!0&" + (reinterpretDeclaration (.byref source) (.byref target)) + (fun reader method => (asOperation reader method).map .memory), + unsafeEntry name!"BitCast" "BitCast" "!!1" "!!0" (reinterpretDeclaration source target) + (fun reader method => (bitCastOperation reader method).map .memory)] + +/-- Each reinterpretation runs a checked memory operation. -/ +theorem unsafeReinterpretEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : + ∀ entry ∈ unsafeReinterpretEntries, entry.Yields property := by + simp only [unsafeReinterpretEntries, List.mem_cons, List.not_mem_nil, or_false, forall_eq_or_imp, + forall_eq] + refine ⟨?_, ?_⟩ <;> intro environment method model found <;> + simp only [unsafeEntry, ModelEntry.resolve_single, Option.map_eq_some_iff] at found <;> + obtain ⟨operation, _, rfl⟩ := found <;> exact memory operation + +#print axioms unsafeReinterpretEntries_yields + +/-- A stored reference helper binds the same signature at the open and closed type arguments. -/ +private def referenceOverload (name result parameters : String) + (signature : TypeExpr → OpenSignature) (admitted : StorageType → Bool) + (operation : MemoryOp) : ModelOverload := + { operand := unsafeOperand name result parameters (signature (.parameter 0)) + accepts := fun method => (unsafeArgument method).any fun element => + method.signature 0x10 == signature element + resolve := fun reader method => unsafeGuarded + (method.methodArguments.any fun type => (reader.storage type).any admitted) + (.memory operation) } + +/-- Both AsRef overloads form the reference supplied by the caller. -/ +private def asRefOverload (pointer : Bool) : ModelOverload := + referenceOverload "AsRef" "!!0&" (if pointer then "System.Void*" else "!!0&") + (fun element => reinterpretDeclaration (if pointer then .pointer .void else .byref element) + (.byref element)) StorageType.isFieldOrVector .asRef + +/-- The reserved reference helpers and their exact external overloads. -/ +def unsafeReferenceEntries : List ModelEntry := + let nonReference := fun (storage : StorageType) => storage != .reference + let boolean := TypeExpr.atom runtimeScope "System.Boolean" + [{ opcode := .call, owner := unsafeOwner, name := name!"AsRef", ownsName := true, + overloads := [asRefOverload false, asRefOverload true] }, + { opcode := .call, owner := unsafeOwner, name := name!"NullRef", ownsName := true, + overloads := [referenceOverload "NullRef" "!!0&" "" (fun element => + { convention := 0x10, parameters := [], returnType := .byref element }) + nonReference .nullRef] }, + { opcode := .call, owner := unsafeOwner, name := name!"IsNullRef", ownsName := true, + overloads := [referenceOverload "IsNullRef" "System.Boolean" "!!0&" (fun element => + reinterpretDeclaration (.byref element) boolean) nonReference .isNullRef] }] + +/-- Every selected reference overload runs its checked memory operation. -/ +theorem unsafeReferenceEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) : + ∀ entry ∈ unsafeReferenceEntries, entry.Yields property := by + intro entry listed reader method model found + simp only [unsafeReferenceEntries, List.mem_cons, List.not_mem_nil, or_false] at listed + rcases listed with rfl | rfl | rfl <;> + simp only [ModelEntry.resolve, ModelEntry.select?, ModelEntry.select?.select, + List.any_cons, List.any_nil, Bool.or_false, Bool.false_eq_true, ite_false] at found <;> + repeat' split at found + all_goals first + | contradiction + | rw [unsafeGuarded_eq found]; exact memory _ + +#print axioms unsafeReferenceEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/UnsafeTransferModels.lean b/CIL/Extraction/UnsafeTransferModels.lean new file mode 100644 index 000000000..51eb70f57 --- /dev/null +++ b/CIL/Extraction/UnsafeTransferModels.lean @@ -0,0 +1,82 @@ +import CIL.Extraction.UnsafeModels +import CIL.Extraction.TransferKinds + +/-! # Unsafe memory transfer models + +Each closed address overload binds its own external member and open declaration. +The layout reader supplies the representation of the transferred element and its alignment. +-/ + +namespace CIL.Extraction + +/-- A transfer takes an address and, for a write, the value to store. -/ +private def parameters (store : Bool) (address element : TypeExpr) : List TypeExpr := + if store then [address, element] else [address] + +/-- A read returns the transferred element. A write returns void. -/ +private def result (store : Bool) (element : TypeExpr) : TypeExpr := + if store then .void else element + +/-- The closed reference identifies the address overload independently of the element layout. -/ +private def accepts (store : Bool) (address : TypeExpr) (method : MethodRef) : Bool := + (unsafeArgument method).any fun element => + method.parameters == parameters store address element && + method.returnType == result store element + +/-- The required alignment is the aggregate's checked alignment or the scalar or vector width. -/ +private def alignment (reader : CallLayouts) (element : TypeExpr) (kind : TransferKind) : + Option Nat := + match kind with + | .snapshot (.aggregate _) => reader.alignment? element + | _ => some kind.width + +/-- Aligned pointer reads and writes check natural alignment before transferring the value. -/ +private def transfer (aligned store : Bool) (reader : CallLayouts) (method : MethodRef) : + Option RuntimeModel := do + let element ← unsafeArgument method + let kind ← transferKind? reader element + if aligned then + let bytes ← alignment reader element kind + return .memory (.pointerTransfer kind bytes store) + else return .typedTransfer kind store + +/-- One external member with its open declaration and corresponding closed address type. -/ +private def overload (name : String) (aligned store byref : Bool) : ModelOverload := + let address := if byref then TypeExpr.byref (.atom runtimeScope "System.Byte") + else .pointer .void + let spelling := if byref then "System.Byte&" else "System.Void*" + { operand := unsafeOperand name (if store then "System.Void" else "!!0") + (if store then s!"{spelling},!!0" else spelling) + (unsafeDeclaration (parameters store address) (result store)), + accepts := accepts store address, resolve := transfer aligned store } + +/-- An aligned member has one pointer overload. An unaligned member also takes a byte reference. -/ +private def entry (key : MetadataName) (name : String) (aligned store : Bool) : ModelEntry := + { opcode := .call, owner := unsafeOwner, name := key, ownsName := true, + overloads := overload name aligned store false :: + (if aligned then [] else [overload name aligned store true]) } + +/-- The four Unsafe calls that transfer a value between an address and the stack. -/ +def unsafeTransferEntries : List ModelEntry := + [entry name!"Read" "Read" true false, entry name!"Write" "Write" true true, + entry name!"ReadUnaligned" "ReadUnaligned" false false, + entry name!"WriteUnaligned" "WriteUnaligned" false true] + +/-- Each selected overload runs an aligned memory operation or an unaligned typed transfer. -/ +theorem unsafeTransferEntries_yields {property : RuntimeModel → Prop} + (memory : ∀ operation, property (.memory operation)) + (typed : ∀ kind store, property (.typedTransfer kind store)) : + ∀ candidate ∈ unsafeTransferEntries, candidate.Yields property := by + intro candidate listed reader method model found + simp only [unsafeTransferEntries, List.mem_cons, List.not_mem_nil, or_false] at listed + rcases listed with rfl | rfl | rfl | rfl <;> + simp only [entry, ModelEntry.resolve, ModelEntry.select?, ModelEntry.select?.select, + Bool.false_eq_true, ite_false, ite_true, List.any_cons, List.any_nil, Bool.or_false] at found + all_goals + repeat' split at found + all_goals simp_all [overload, transfer, Option.bind_eq_some_iff, Option.pure_def] + all_goals grind + +#print axioms unsafeTransferEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/UnsafeTransfers.lean b/CIL/Extraction/UnsafeTransfers.lean deleted file mode 100644 index 3e85147d2..000000000 --- a/CIL/Extraction/UnsafeTransfers.lean +++ /dev/null @@ -1,170 +0,0 @@ -import CIL.Extraction.Generics - -/-! -# Checked Unsafe transfers - -Unsafe reads and writes reuse checked typed transfers. Scalar and vector widths and alignment -are bound here; aggregate width and alignment are additionally bound by complete layout uses. -`Vector128.LoadUnsafe` and `StoreUnsafe`, with or without a `nuint` offset, are -bound to the one operation that displaces and transfers like `Unsafe.Add` and `load128` or -`store128`. --/ - -namespace CIL.Extraction - -/-- Exact byte-reference and pointer block helpers share the existing checked block operations. -/ -def unsafeBlockCall (raw : RawInstruction) : Option (Bool × Nat) := do - if raw.opcode != 40 then none else pure () - let .call site := raw.operand | none - if site.declaringType != .atom runtimeScope "System.Runtime.CompilerServices.Unsafe" || - site.hasThis || site.returnsValue || site.returnType != .void || - !site.typeArguments.isEmpty || site.parameterCount != 3 then none else pure () - let (copy, alignment) ← match site.name with - | "CopyBlock" => some (true, 8) | "CopyBlockUnaligned" => some (true, 1) - | "InitBlock" => some (false, 8) | "InitBlockUnaligned" => some (false, 1) - | _ => none - let byte := TypeExpr.atom runtimeScope "System.Byte" - let pointer := site.parameterTypes.head? == some (.pointer .void) - let address := if pointer then .pointer .void else .byref byte - if site.parameterTypes != [address, if copy then address else byte, - .atom runtimeScope "System.UInt32"] then none else pure () - let addressName := if pointer then "System.Void*" else "System.Byte&" - if site.target != .external (runtimeScope ++ - "::System.Void System.Runtime.CompilerServices.Unsafe::" ++ site.name ++ "(" ++ - addressName ++ "," ++ (if copy then addressName else "System.Byte") ++ - ",System.UInt32)") then none else some (copy, alignment) - -/-- Block helper calls bind operation, alignment and signature independently of the extractor. -/ -def unsafeBlockMatches (raw : RawInstruction) (emitted : Op) : Bool := - match unsafeBlockCall raw with - | some (copy, alignment) => match copy, emitted with - | true, .memory (.copyBlock actual) | false, .memory (.initBlock actual) => actual == alignment - | _, _ => false - | none => match raw.opcode, emitted with - | 40, .memory (.copyBlock _) | 40, .memory (.initBlock _) => false - | _, _ => true - -/-- The exact load or store representation of an admitted reference-free value type. -/ -def typedTransferMatches (layouts : List RawLayout) (type : TypeExpr) (read : Bool) - (emitted : Op) : Bool := - if let some width := integerVectorWidth type then - match read, width, emitted with - | true, 16, .memory .load128 | true, 32, .memory .load256 - | true, 64, .memory .load512 | false, 16, .memory .store128 - | false, 32, .memory .store256 | false, 64, .memory .store512 => true - | _, _, _ => false - else match typeStorage layouts type with - | some (.field (.aggregate _)) => match read, emitted with - | true, .loadAggregate _ | false, .storeAggregate _ => true - | _, _ => false - | some (.field kind) => match primitiveFieldWidth kind with - | none => false - | some width => match read, emitted with - | true, .loadScalar actual signed => actual == width && - signed == (kind == .signed8 || kind == .signed16) - | false, .storeScalar actual => actual == width - | _, _ => false - | _ => false - -/-- The transfer representation agrees with the validated value type. -/ -def transferKindMatches (layouts : List RawLayout) (type : TypeExpr) - (kind : TransferKind) : Bool := - match kind with - | .scalar width signed => typedTransferMatches layouts type true (.loadScalar width signed) - | .snapshot (.aggregate width) => typedTransferMatches layouts type true (.loadAggregate width) - | .snapshot .vector128 => typedTransferMatches layouts type true (.memory .load128) - | .snapshot .vector256 => typedTransferMatches layouts type true (.memory .load256) - | .snapshot .vector512 => typedTransferMatches layouts type true (.memory .load512) - -/-- Exact static generic Unsafe identity, including both closed parameter and result types. -/ -def unsafeValueCall (raw : RawInstruction) : Option (TypeExpr × Bool × Bool) := do - if raw.opcode != 40 then none else pure () - let .call site := raw.operand | none - let [type] := site.typeArguments | none - if site.declaringType != .atom runtimeScope "System.Runtime.CompilerServices.Unsafe" || - site.hasThis then none else pure () - let byte := TypeExpr.atom runtimeScope "System.Byte" - let pointer := site.parameterTypes.head? == some (.pointer .void) - let aligned := site.name == "Read" || site.name == "Write" - if aligned && !pointer then none else pure () - let address := if pointer then .pointer .void else .byref byte - let addressName := if pointer then "System.Void*" else "System.Byte&" - if (site.name == "ReadUnaligned" || site.name == "Read") && - site.parameterCount == 1 && site.returnsValue && - site.parameterTypes == [address] && site.returnType == type && - site.target == .external (runtimeScope ++ - "::!!0 System.Runtime.CompilerServices.Unsafe::" ++ - site.name ++ "(" ++ addressName ++ ")") then - some (type, true, aligned) - else if (site.name == "WriteUnaligned" || site.name == "Write") && - site.parameterCount == 2 && !site.returnsValue && - site.parameterTypes == [address, type] && site.returnType == .void && - site.target == .external (runtimeScope ++ - "::System.Void System.Runtime.CompilerServices.Unsafe::" ++ - site.name ++ "(" ++ addressName ++ ",!!0)") then - some (type, false, aligned) - else none - -/-- Primitive and vector alignment is fixed here; aggregate alignment uses complete layout uses. -/ -private def transferAlignmentMatches (layouts : List RawLayout) (type : TypeExpr) - (alignment : Nat) : Bool := - if let some width := integerVectorWidth type then alignment == width else - match typeStorage layouts type with - | some (.field (.aggregate _)) => true - | some (.field kind) => primitiveFieldWidth kind == some alignment - | _ => false - -/-- Typed memory operations from calls are reserved for exact admitted Unsafe transfer helpers. -/ -def unsafeTransferMatches (layouts : List RawLayout) (raw : RawInstruction) (emitted : Op) : Bool := - match unsafeValueCall raw with - | some (type, read, false) => typedTransferMatches layouts type read emitted - | some (type, read, true) => match emitted with - | .memory (.pointerTransfer kind alignment store) => store == !read && - transferKindMatches layouts type kind && transferAlignmentMatches layouts type alignment - | _ => false - | none => match raw.opcode, emitted with - | 40, .loadScalar .. | 40, .storeScalar _ | 40, .loadAggregate _ | 40, .storeAggregate _ - | 40, .memory .load128 | 40, .memory .load256 | 40, .memory .load512 - | 40, .memory .store128 | 40, .memory .store256 | 40, .memory .store512 => false - | _, .memory (.pointerTransfer ..) => false - | _, _ => true - -/-- Whether `Vector128.LoadUnsafe` or `StoreUnsafe` is named, whatever the remaining metadata. -/ -private def vector128UnsafeFamily (site : CallSite) : Bool := - (site.name == "LoadUnsafe" || site.name == "StoreUnsafe") && - match site.declaringType with - | .atom _ owner => owner == "System.Runtime.Intrinsics.Vector128" - | _ => false - -/-- `(store, offset)` of an exact `Vector128.LoadUnsafe` or `StoreUnsafe` call, - checked against its closed types and its open generic identity. -/ -def vector128UnsafeCall (raw : RawInstruction) : Option (Bool × Bool) := do - if raw.opcode != 40 then none else pure () - let .call site := raw.operand | none - if !vector128UnsafeFamily site || site.hasThis then none else pure () - let byte := TypeExpr.atom runtimeScope "System.Byte" - let vectorName := "System.Runtime.Intrinsics.Vector128`1" - let vector := TypeExpr.apply (.constructor intrinsicsScope vectorName) byte - let store := site.name == "StoreUnsafe" - let offset := site.parameterCount == (if store then 3 else 2) - let offsetType := if offset then [TypeExpr.atom runtimeScope "System.UIntPtr"] else [] - let offsetName := if offset then ",System.UIntPtr" else "" - let parameters := (if store then [vector] else []) ++ [.byref byte] ++ offsetType - let parameterNames := (if store then vectorName ++ "," else "") ++ "!!0&" ++ offsetName - let resultName := if store then "System.Void" else vectorName ++ "" - if site.typeArguments == [byte] && site.parameterTypes == parameters && - site.parameterCount == parameters.length && site.returnsValue == !store && - site.returnType == (if store then .void else vector) && - site.target == .external (intrinsicsScope ++ "::" ++ resultName ++ - " System.Runtime.Intrinsics.Vector128::" ++ site.name ++ "(" ++ parameterNames ++ ")") - then some (store, offset) else none - -/-- An exact `Vector128` byte load or store emits its operation; no other instruction may emit - it, and no other call named like one is admitted. -/ -def vector128UnsafeMatches (raw : RawInstruction) : Op → Bool - | .memory (.vector128Unsafe store offset) => vector128UnsafeCall raw == some (store, offset) - | _ => match raw.operand with - | .call site => !vector128UnsafeFamily site - | _ => true - -end CIL.Extraction diff --git a/CIL/Extraction/ValueDecoding.lean b/CIL/Extraction/ValueDecoding.lean new file mode 100644 index 000000000..b63937908 --- /dev/null +++ b/CIL/Extraction/ValueDecoding.lean @@ -0,0 +1,999 @@ +import CIL.Extraction.Prefixes + +/-! +# Value opcodes decoded from raw instructions + +`Instruction.ofRaw?` gives the one instruction a raw value-family instruction denotes: stack +shuffling, return, constants, `ldnull`, arguments, locals, branches, integer arithmetic, +conversions, comparisons, integer indirect loads and stores, block copies and fills, array +allocations, lengths and element loads and stores, dynamic frame storage, the `readonly.` and +`unaligned.` prefixes, the class type tests `castclass` and `isinst`, the +instance field accesses `ldfld`, `ldflda` and `stfld` of a class or a struct, direct and +constrained calls, and construction by `newobj`, with every short and macro form folded into its +constructor. `valueEncodingMatches` accepts an emitted instruction exactly when it is that decoded +instruction, and `Instruction.valueOpcode` is the set of opcodes `ofRaw?` decodes for some +operand. Instantiation changes a decoded instruction only in the type a type operand names, +the method a call or construction names, or through a field operand +(`Instruction.ofRaw?_substitute`), so a closed generic body decodes like its original with each +type test and typed array instruction at the instantiated type, each call and construction of +the instantiated method, and each instance field access at its instantiated owner and held type +(`Instruction.ofRaw?_substitute_instance_field`). +`Instruction.ofRaw` is the total decoding: it writes a hole, `.unadmitted` of the raw opcode, +exactly where `ofRaw?` admits nothing (`Instruction.ofRaw_eq_unadmitted_iff`). Both take the raw +instruction before the decoded one, where a prefix would stand, and `Instruction.ofRawCode` +decodes code in one pass that carries it (`Instruction.ofRawCode_getElem?`). +-/ + +namespace CIL.Extraction + +/-- One-byte opcodes are below this value; two-byte opcodes `0xFE nn` are `0xFE00 + nn`. -/ +def oneByteOpcodes : Nat := 256 + +/-- The local a `ldloc`, `stloc` or `ldloca` instruction names, in any of its forms. -/ +def localIndex (instruction : RawInstruction) : Option Nat := + match instruction.opcode, instruction.operand with + -- `ldloc.0` to `ldloc.3` and `stloc.0` to `stloc.3`. + | 6, .none | 10, .none => some 0 + | 7, .none | 11, .none => some 1 + | 8, .none | 12, .none => some 2 + | 9, .none | 13, .none => some 3 + -- `ldloc`, `ldloca` and `stloc`, short and long forms. + | 17, .local n | 18, .local n | 19, .local n + | 65036, .local n | 65037, .local n | 65038, .local n => some n + | _, _ => none + +/-- The indices an index operand of `opcode` can encode: an unsigned int8 after a one-byte + opcode and an unsigned int16 after a two-byte one (ECMA-335 III.1.2). -/ +def operandIndexBound (opcode : Nat) : Nat := + if opcode < oneByteOpcodes then 2 ^ 8 else 2 ^ 16 + +/-- The local a local-variable instruction names, when the instruction's encoding can carry it. -/ +def encodedLocal (instruction : RawInstruction) : Option Nat := + (localIndex instruction).filter (· < operandIndexBound instruction.opcode) + +/-- Operand kinds that instantiation rewrites; of these, value opcodes take only a type or a + field. -/ +def RawOperand.mentionsType : RawOperand → Bool + | .type _ | .field .. | .call _ | .indirectCall .. => true + | _ => false + +/-- Whether an operand is a type, the operand of the class type tests. -/ +def RawOperand.isType : RawOperand → Bool + | .type _ => true + | _ => false + +/-- Whether an operand is a call site, the operand of `call` and `newobj`. -/ +def RawOperand.isCall : RawOperand → Bool + | .call _ => true + | _ => false + +/-- Substitution keeps an operand's kind, a type, call or field operand included, and leaves an + operand that mentions no type unchanged. -/ +theorem substituteOperand_mentionsType {arguments : List TypeExpr} {layouts : List RawLayout} + {arity : Nat} {original operand : RawOperand} + (substituted : substituteOperand arguments layouts arity original = some operand) : + operand.mentionsType = original.mentionsType ∧ operand.isType = original.isType ∧ + operand.isCall = original.isCall ∧ + (FieldRef.ofOperand? operand).isSome = (FieldRef.ofOperand? original).isSome ∧ + (original.mentionsType = false → operand = original) := by + cases original with + | type type => + simp only [substituteOperand, Option.map_eq_map, Option.map_eq_some_iff] at substituted + obtain ⟨_, _, rfl⟩ := substituted + exact ⟨rfl, rfl, rfl, rfl, nofun⟩ + | field owner index type name isStatic => + simp only [substituteOperand, Option.bind_eq_bind, Option.bind_eq_some_iff, pure, + Option.some.injEq] at substituted + obtain ⟨_, _, _, _, rfl⟩ := substituted + exact ⟨rfl, rfl, rfl, rfl, nofun⟩ + | call site => + simp only [substituteOperand, Option.bind_eq_bind, Option.bind_eq_some_iff, pure] + at substituted + obtain ⟨_, _, substituted⟩ := substituted + split at substituted <;> + simp only [Option.bind_eq_some_iff, Option.some.injEq] at substituted <;> + obtain ⟨_, _, _, _, _, _, _, _, _, _, rfl⟩ := substituted <;> + exact ⟨rfl, rfl, rfl, rfl, nofun⟩ + | indirectCall returnType parameters hasThis explicitThis convention => + simp only [substituteOperand, Option.bind_eq_bind, Option.bind_eq_some_iff, pure, + Option.some.injEq] at substituted + obtain ⟨_, _, _, _, rfl⟩ := substituted + exact ⟨rfl, rfl, rfl, rfl, nofun⟩ + | _ => + simp only [substituteOperand, Option.some.injEq] at substituted + exact ⟨by rw [substituted], by rw [substituted], by rw [substituted], by rw [substituted], + fun _ => substituted.symm⟩ + +#print axioms substituteOperand_mentionsType + +/-- The five raw opcodes that access a value through a type token. -/ +def objectOpcode (opcode : Nat) : Bool := opcode ∈ [0x70, 0x71, 0x81, 0xfe15, 0xfe1c] + +end CIL.Extraction + +namespace CIL + +open Extraction + +/-- The opcodes `Instruction.ofRaw?` decodes for some operand. -/ +def Instruction.valueOpcode (opcode : Nat) : Bool := + opcode == 0 || (2 <= opcode && opcode <= 33) || opcode == 37 || opcode == 38 || + (40 <= opcode && opcode <= 41) || + (42 <= opcode && opcode <= 77) || (82 <= opcode && opcode <= 85) || + (88 <= opcode && opcode <= 106) || (109 <= opcode && opcode <= 117) || + (123 <= opcode && opcode <= 125) || (129 <= opcode && opcode <= 139) || + opcode == 141 || (142 <= opcode && opcode <= 151) || + (154 <= opcode && opcode <= 159) || (162 <= opcode && opcode <= 164) || + (179 <= opcode && opcode <= 186) || (208 <= opcode && opcode <= 219) || opcode == 223 || + opcode == 224 || (65025 <= opcode && opcode <= 65030) || + (65033 <= opcode && opcode <= 65039) || opcode == 0xfe15 || opcode == 0xfe16 || + opcode == 0xfe1c || opcode == 0xfe1e || opcode == 0xfe12 || opcode == 0xfe17 || + opcode == 0xfe18 + +/-- `ldelema` (143) of the element type `type` after `previous`: a read-only address after + `readonly.`, which it takes, and nothing after any other prefix. -/ +def Instruction.ldelemaAfter? (previous : Option RawInstruction) (type : TypeExpr) : + Option Instruction := + if previous.any fun before => before.opcode == 65054 then some (.ldelema type true) + else if previous.any fun before => isPrefixOpcode before.opcode then none + else some (.ldelema type false) + +/-- The object, class or array operation a type operand denotes after its prefix. -/ +def Instruction.ofType? (previous : Option RawInstruction) (opcode : Nat) (type : TypeExpr) : + Option Instruction := + match opcode with + | 0x70 => some (.cpobj type) + | 0x71 => some (.ldobj type) + | 0x74 => some (.castclass type) + | 0x75 => some (.isinst type) + | 0x81 => some (.stobj type) + | 0x8d => some (.newarr type) + | 0x8f => Instruction.ldelemaAfter? previous type + | 0xa3 => some (.ldelem_any type) + | 0xa4 => some (.stelem_any type) + | 0xd0 => some (.ldtoken (.type type)) + | 0xfe15 => some (.initobj type) + | 0xfe16 => some .nop + | 0xfe1c => some (.sizeof type) + | _ => none + +/-- Every successful type-operand decoding names one of the supported opcodes. -/ +theorem Instruction.ofType?_opcode {previous : Option RawInstruction} {opcode : Nat} + {type : TypeExpr} {instruction : Instruction} + (named : Instruction.ofType? previous opcode type = some instruction) : + opcode = 116 ∨ opcode = 117 ∨ opcode = 141 ∨ opcode = 163 ∨ opcode = 164 ∨ + opcode = 143 ∨ opcode = 0x70 ∨ opcode = 0x71 ∨ opcode = 0x81 ∨ + opcode = 0xFE15 ∨ opcode = 0xFE1C ∨ opcode = 0xFE16 ∨ opcode = 0xD0 := by + unfold Instruction.ofType? at named + repeat' split at named + all_goals first | contradiction | simp + +#print axioms Instruction.ofType?_opcode + +/-- The transfer or dispatch modifier carried by a direct call. -/ +private def callPrefixes : Option RawInstruction → CallPrefixes + | some ⟨_, 0xFE14, .none⟩ => { tail := true } + | some ⟨_, 0xFE16, .type receiver⟩ => { constrained := some receiver } + | _ => {} + +/-- The closed signature shared by a managed address and an indirect call. -/ +def ManagedSignature.ofTypes (result : TypeExpr) (parameters : List TypeExpr) : ManagedSignature := + ⟨callableType result, parameters.map callableType⟩ + +/-- A method-token instruction names its raw site and carries its supported prefix. -/ +def Instruction.ofCallSite? (previous : Option RawInstruction) (opcode : Nat) + (site : CallSite) : Option Instruction := + if opcode == 40 then some (.call site.method (callPrefixes previous)) + else if opcode == 115 then some (.newobj site.method) + else if opcode == 111 && (callPrefixes previous).constrained.isSome then + some (.call site.method (callPrefixes previous)) + else if opcode == 111 && site.hasThis && !(previous.any (isPrefixOpcode ·.opcode)) then + some (.callvirt site.method) + else if opcode == 65030 then + if site.hasThis then none else + match site.target, managedOwner site.declaringType with + | .managed token, some (owner, _) => + some (.ldftn ⟨owner, token, site.typeArguments.map callableType⟩ + (ManagedSignature.ofTypes site.returnType site.parameterTypes)) + | _, _ => none + else none + +/-- Call sites decode at `call` (40), `callvirt` (111), `newobj` (115) and `ldftn` (65030). -/ +theorem Instruction.ofCallSite?_opcode {previous : Option RawInstruction} {opcode : Nat} + {site : CallSite} {instruction : Instruction} + (named : Instruction.ofCallSite? previous opcode site = some instruction) : + opcode = 40 ∨ opcode = 115 ∨ opcode = 111 ∨ opcode = 65030 := by + unfold Instruction.ofCallSite? at named + repeat' split at named + all_goals first | contradiction | simp_all + +#print axioms Instruction.ofCallSite?_opcode + +/-- The instance field instruction `opcode` denotes on `field`: `ldfld` (123), `ldflda` (124) + or `stfld` (125). -/ +def Instruction.instanceField? (opcode : Nat) (field : FieldRef) : Option Instruction := + if opcode == 123 then some (.ldfld field) else if opcode == 124 then some (.ldflda field) + else if opcode == 125 then some (.stfld field) else none + +/-- Only `ldfld` (123), `ldflda` (124) and `stfld` (125) denote an instance field instruction. -/ +theorem Instruction.instanceField?_opcode {opcode : Nat} {field : FieldRef} + {instruction : Instruction} + (accessed : Instruction.instanceField? opcode field = some instruction) : + opcode = 123 ∨ opcode = 124 ∨ opcode = 125 := by + unfold Instruction.instanceField? at accessed + split at accessed + · exact .inl (beq_iff_eq.mp ‹_›) + · split at accessed + · exact .inr (.inl (beq_iff_eq.mp ‹_›)) + · split at accessed + · exact .inr (.inr (beq_iff_eq.mp ‹_›)) + · cases accessed + +#print axioms Instruction.instanceField?_opcode + +/-- Whether `emitted` is the instruction `decoded`, operands included. Defined only for the + value constructors `ofRaw?` produces; every other `decoded` gives `false`. -/ +def Instruction.sameValue (decoded emitted : Instruction) : Bool := + match decoded with + -- Stack shuffling and return. + | .nop => (emitted matches .nop) + | .dup => (emitted matches .dup) + | .pop => (emitted matches .pop) + | .ret => (emitted matches .ret) + -- Constants, arguments and locals compare their operand. + | .ldc_i4 expected => match emitted with | .ldc_i4 actual => expected == actual | _ => false + | .ldc_i8 expected => match emitted with | .ldc_i8 actual => expected == actual | _ => false + | .ldnull => (emitted matches .ldnull) + | .ldtoken (.type expected) => match emitted with + | .ldtoken (.type actual) => expected == actual + | _ => false + | .ldstr text => match emitted with + | .ldstr actual => decide (text = actual) + | _ => false + | .cpblk expected => match emitted with + | .cpblk actual => expected == actual + | _ => false + | .initblk expected => match emitted with + | .initblk actual => expected == actual + | _ => false + | .ldarg expected => match emitted with | .ldarg actual => expected == actual | _ => false + | .ldarga expected => match emitted with | .ldarga actual => expected == actual | _ => false + | .starg expected => match emitted with | .starg actual => expected == actual | _ => false + | .ldloc expected => match emitted with | .ldloc actual => expected == actual | _ => false + | .stloc expected => match emitted with | .stloc actual => expected == actual | _ => false + | .ldloca expected => match emitted with | .ldloca actual => expected == actual | _ => false + -- Branches compare their target. + | .br expected => match emitted with | .br actual => expected == actual | _ => false + | .brfalse expected => match emitted with | .brfalse actual => expected == actual | _ => false + | .brtrue expected => match emitted with | .brtrue actual => expected == actual | _ => false + | .beq expected => match emitted with | .beq actual => expected == actual | _ => false + | .bge expected => match emitted with | .bge actual => expected == actual | _ => false + | .bgt expected => match emitted with | .bgt actual => expected == actual | _ => false + | .ble expected => match emitted with | .ble actual => expected == actual | _ => false + | .blt expected => match emitted with | .blt actual => expected == actual | _ => false + | .bne_un expected => match emitted with | .bne_un actual => expected == actual | _ => false + | .bge_un expected => match emitted with | .bge_un actual => expected == actual | _ => false + | .bgt_un expected => match emitted with | .bgt_un actual => expected == actual | _ => false + | .ble_un expected => match emitted with | .ble_un actual => expected == actual | _ => false + | .blt_un expected => match emitted with | .blt_un actual => expected == actual | _ => false + | .switch expected => match emitted with | .switch actual => expected == actual | _ => false + -- Indirect loads and stores compare their kind. + | .ldind expected => match emitted with | .ldind actual => expected == actual | _ => false + | .stind expected => match emitted with | .stind actual => expected == actual | _ => false + -- Array lengths and elements compare their kind. + | .ldlen => (emitted matches .ldlen) + | .ldelem expected => match emitted with | .ldelem actual => expected == actual | _ => false + | .ldelem_ref => (emitted matches .ldelem_ref) + | .stelem expected => match emitted with | .stelem actual => expected == actual | _ => false + | .stelem_ref => (emitted matches .stelem_ref) + -- Arithmetic, bitwise operations and shifts. + | .add => (emitted matches .add) + | .sub => (emitted matches .sub) + | .mul => (emitted matches .mul) + | .div => (emitted matches .div) + | .div_un => (emitted matches .div_un) + | .rem => (emitted matches .rem) + | .rem_un => (emitted matches .rem_un) + | .and => (emitted matches .and) + | .or => (emitted matches .or) + | .xor => (emitted matches .xor) + | .shl => (emitted matches .shl) + | .shr => (emitted matches .shr) + | .shr_un => (emitted matches .shr_un) + | .neg => (emitted matches .neg) + | .not => (emitted matches .not) + -- Conversions. + | .conv_i1 => (emitted matches .conv_i1) + | .conv_i2 => (emitted matches .conv_i2) + | .conv_i4 => (emitted matches .conv_i4) + | .conv_i8 => (emitted matches .conv_i8) + | .conv_u1 => (emitted matches .conv_u1) + | .conv_u2 => (emitted matches .conv_u2) + | .conv_u4 => (emitted matches .conv_u4) + | .conv_u8 => (emitted matches .conv_u8) + | .conv_i => (emitted matches .conv_i) + | .conv_u => (emitted matches .conv_u) + | .conv_ovf expected => match emitted with | .conv_ovf actual => expected == actual | _ => false + | .conv_ovf_un expected => + match emitted with | .conv_ovf_un actual => expected == actual | _ => false + -- Overflow-checked arithmetic and comparisons. + | .add_ovf => (emitted matches .add_ovf) + | .add_ovf_un => (emitted matches .add_ovf_un) + | .sub_ovf => (emitted matches .sub_ovf) + | .sub_ovf_un => (emitted matches .sub_ovf_un) + | .mul_ovf => (emitted matches .mul_ovf) + | .mul_ovf_un => (emitted matches .mul_ovf_un) + | .ceq => (emitted matches .ceq) + | .cgt => (emitted matches .cgt) + | .cgt_un => (emitted matches .cgt_un) + | .clt => (emitted matches .clt) + | .clt_un => (emitted matches .clt_un) + -- Each object instruction names its closed storage type. + | .ldobj expected => match emitted with | .ldobj actual => expected == actual | _ => false + | .stobj expected => match emitted with | .stobj actual => expected == actual | _ => false + | .cpobj expected => match emitted with | .cpobj actual => expected == actual | _ => false + | .initobj expected => match emitted with | .initobj actual => expected == actual | _ => false + | .sizeof expected => match emitted with | .sizeof actual => expected == actual | _ => false + -- Type tests compare the class they test. + | .castclass expected => + match emitted with | .castclass actual => expected == actual | _ => false + | .isinst expected => match emitted with | .isinst actual => expected == actual | _ => false + -- Typed array instructions compare the element type they name. + | .newarr expected => match emitted with | .newarr actual => expected == actual | _ => false + | .ldelem_any expected => + match emitted with | .ldelem_any actual => expected == actual | _ => false + | .stelem_any expected => + match emitted with | .stelem_any actual => expected == actual | _ => false + | .ldelema expected readOnly => match emitted with + | .ldelema actual actualReadOnly => expected == actual && readOnly == actualReadOnly + | _ => false + -- Instance field instructions compare the field they name. + | .ldfld expected => match emitted with | .ldfld actual => expected == actual | _ => false + | .ldflda expected => match emitted with | .ldflda actual => expected == actual | _ => false + | .stfld expected => match emitted with | .stfld actual => expected == actual | _ => false + -- A call compares the method it names and its prefixes, a construction its constructor. + | .callvirt expected => match emitted with + | .callvirt actual => expected == actual + | _ => false + | .ldftn identity signature => match emitted with + | .ldftn target sig => identity == target && signature == sig + | _ => false + | .calli signature tail => match emitted with + | .calli sig isTail => signature == sig && tail == isTail + | _ => false + | .call expected prefixes => match emitted with + | .call actual actualPrefixes => expected == actual && prefixes == actualPrefixes + | _ => false + | .newobj expected => match emitted with | .newobj actual => expected == actual | _ => false + -- Dynamic frame storage. + | .localloc => (emitted matches .localloc) + -- Constructors outside the value family are never a decoded value. + | _ => false + +/-- An opcode result together with its reflexive value-encoding comparison. -/ +private abbrev DecodedValue := { value : Instruction // value.sameValue value = true } + +/-- The value emitted by an admitted opcode, with its encoding checked at the constructor. -/ +private def emit (value : Instruction) + (valid : value.sameValue value = true := by + first + | rfl + | exact Nat.beq_refl _ + | exact decide_eq_true (Eq.refl _) + | exact @List.instReflBEq Nat _ ⟨Nat.beq_refl _⟩ |>.rfl) : + Option DecodedValue := some ⟨value, valid⟩ + +/-- A bare literal admits BMP text except backspace and form feed. -/ +private def literalTextSupported (text : String) : Bool := + text.toUTF8.data.toList.all fun byte => + let value := byte.toNat + value != 0x08 && value != 0x0C && Nat.blt value 0xF0 + +/-- The value of an instruction with an immediate, branch, slot or absent operand. -/ +private def plainValue? (previous : Option RawInstruction) (raw : RawInstruction) : + Option DecodedValue := + match raw.opcode, raw.operand with + -- Stack shuffling and return. + | 0, .none => emit .nop + | 37, .none => emit .dup + | 38, .none => emit .pop + | 42, .none => emit .ret + -- Constants, with the operand ranges ECMA-335 gives each form. + | 20, .none => emit .ldnull + | 114, .text text => + if literalTextSupported text then emit (.ldstr text) else none + | 21, .none => emit (.ldc_i4 (BitVec.ofInt 32 (-1))) + | 31, .integer value => + if -128 <= value && value < 128 then emit (.ldc_i4 (BitVec.ofInt 32 value)) else none + | 32, .integer value => + if -(2^31) <= value && value < 2^31 then emit (.ldc_i4 (BitVec.ofInt 32 value)) else none + | 33, .integer value => + if -(2^63) <= value && value < 2^63 then emit (.ldc_i8 (BitVec.ofInt 64 value)) else none + | 22, .none | 23, .none | 24, .none | 25, .none | 26, .none | 27, .none | 28, .none + | 29, .none | 30, .none => emit (.ldc_i4 (BitVec.ofNat 32 (raw.opcode - 22))) + -- Argument loads, addresses and stores; an index wider than its form encodes gives `none`. + | 2, .none => emit (.ldarg 0) | 3, .none => emit (.ldarg 1) + | 4, .none => emit (.ldarg 2) | 5, .none => emit (.ldarg 3) + | 14, .argument index | 65033, .argument index => + if index < operandIndexBound raw.opcode then emit (.ldarg index) else none + | 15, .argument index | 65034, .argument index => + if index < operandIndexBound raw.opcode then emit (.ldarga index) else none + | 16, .argument index | 65035, .argument index => + if index < operandIndexBound raw.opcode then emit (.starg index) else none + -- Locals; `encodedLocal` validates the operand of every form and its width. + | 6, _ | 7, _ | 8, _ | 9, _ | 17, _ | 65036, _ => + (encodedLocal raw).bind fun index => emit (.ldloc index) + | 10, _ | 11, _ | 12, _ | 13, _ | 19, _ | 65038, _ => + (encodedLocal raw).bind fun index => emit (.stloc index) + | 18, _ | 65037, _ => (encodedLocal raw).bind fun index => emit (.ldloca index) + -- Branches, short and long forms, keep their resolved target. + | 43, .target target | 56, .target target => emit (.br target) + | 44, .target target | 57, .target target => emit (.brfalse target) + | 45, .target target | 58, .target target => emit (.brtrue target) + | 46, .target target | 59, .target target => emit (.beq target) + | 47, .target target | 60, .target target => emit (.bge target) + | 48, .target target | 61, .target target => emit (.bgt target) + | 49, .target target | 62, .target target => emit (.ble target) + | 50, .target target | 63, .target target => emit (.blt target) + | 51, .target target | 64, .target target => emit (.bne_un target) + | 52, .target target | 65, .target target => emit (.bge_un target) + | 53, .target target | 66, .target target => emit (.bgt_un target) + | 54, .target target | 67, .target target => emit (.ble_un target) + | 55, .target target | 68, .target target => emit (.blt_un target) + | 69, .targets targets => emit (.switch targets) + -- Integer indirect loads and stores; the floating and reference kinds decode to nothing. + | 70, .none => emit (.ldind .i1) | 71, .none => emit (.ldind .u1) + | 72, .none => emit (.ldind .i2) | 73, .none => emit (.ldind .u2) + | 74, .none => emit (.ldind .i4) | 75, .none => emit (.ldind .u4) + | 76, .none => emit (.ldind .i8) | 77, .none => emit (.ldind .i) + | 82, .none => emit (.stind .i1) | 83, .none => emit (.stind .i2) + | 84, .none => emit (.stind .i4) | 85, .none => emit (.stind .i8) + | 223, .none => emit (.stind .i) + -- Array lengths and elements; the floating kinds decode to nothing. + | 142, .none => emit .ldlen + | 144, .none => emit (.ldelem .i1) | 145, .none => emit (.ldelem .u1) + | 146, .none => emit (.ldelem .i2) | 147, .none => emit (.ldelem .u2) + | 148, .none => emit (.ldelem .i4) | 149, .none => emit (.ldelem .u4) + | 150, .none => emit (.ldelem .i8) | 151, .none => emit (.ldelem .i) + | 154, .none => emit .ldelem_ref + | 155, .none => emit (.stelem .i) | 156, .none => emit (.stelem .i1) + | 157, .none => emit (.stelem .i2) | 158, .none => emit (.stelem .i4) + | 159, .none => emit (.stelem .i8) + | 162, .none => emit .stelem_ref + -- Arithmetic, bitwise operations and shifts. + | 88, .none => emit .add | 89, .none => emit .sub | 90, .none => emit .mul + | 91, .none => emit .div | 92, .none => emit .div_un + | 93, .none => emit .rem | 94, .none => emit .rem_un + | 95, .none => emit .and | 96, .none => emit .or | 97, .none => emit .xor + | 98, .none => emit .shl | 99, .none => emit .shr | 100, .none => emit .shr_un + | 101, .none => emit .neg | 102, .none => emit .not + -- Conversions. + | 103, .none => emit .conv_i1 | 104, .none => emit .conv_i2 + | 105, .none => emit .conv_i4 | 106, .none => emit .conv_i8 + | 109, .none => emit .conv_u4 | 110, .none => emit .conv_u8 + | 209, .none => emit .conv_u2 | 210, .none => emit .conv_u1 + | 211, .none => emit .conv_i | 224, .none => emit .conv_u + -- Overflow-checked conversions from an unsigned source. + | 130, .none => emit (.conv_ovf_un .i1) | 131, .none => emit (.conv_ovf_un .i2) + | 132, .none => emit (.conv_ovf_un .i4) | 133, .none => emit (.conv_ovf_un .i8) + | 134, .none => emit (.conv_ovf_un .u1) | 135, .none => emit (.conv_ovf_un .u2) + | 136, .none => emit (.conv_ovf_un .u4) | 137, .none => emit (.conv_ovf_un .u8) + | 138, .none => emit (.conv_ovf_un .i) | 139, .none => emit (.conv_ovf_un .u) + -- Overflow-checked conversions from a signed source. + | 179, .none => emit (.conv_ovf .i1) | 180, .none => emit (.conv_ovf .u1) + | 181, .none => emit (.conv_ovf .i2) | 182, .none => emit (.conv_ovf .u2) + | 183, .none => emit (.conv_ovf .i4) | 184, .none => emit (.conv_ovf .u4) + | 185, .none => emit (.conv_ovf .i8) | 186, .none => emit (.conv_ovf .u8) + | 212, .none => emit (.conv_ovf .i) | 213, .none => emit (.conv_ovf .u) + -- Overflow-checked arithmetic. + | 214, .none => emit .add_ovf | 215, .none => emit .add_ovf_un + | 216, .none => emit .mul_ovf | 217, .none => emit .mul_ovf_un + | 218, .none => emit .sub_ovf | 219, .none => emit .sub_ovf_un + -- Comparisons. + | 65025, .none => emit .ceq | 65026, .none => emit .cgt | 65027, .none => emit .cgt_un + | 65028, .none => emit .clt | 65029, .none => emit .clt_un + -- Dynamic frame storage; whether it is zeroed is the method's localsinit flag, not an operand. + | 65039, .none => emit .localloc + -- Block copies and fills, at the alignment an `unaligned.` before them gives. + | 65047, .none => (blockAlignment previous).bind fun alignment => + emit (.cpblk alignment) + | 65048, .none => (blockAlignment previous).bind fun alignment => + emit (.initblk alignment) + -- The prefixes their consumers record; `invalidPredecessor` refuses a consumer that takes + -- neither, and `rawPrefixesValid` a prefix with no consumer. + | 65054, .none => if previous.any fun before => isPrefixOpcode before.opcode then none + else emit .nop + | 65042, operand => if (prefixAlignment operand).isSome then emit .nop else none + | _, _ => none + + +/-- The instruction a value-family raw instruction denotes: one constructor per ECMA opcode, + with short and macro forms folded into it. A type operand gives the type test or typed array + instruction its opcode names, a struct or nongeneric class field the instruction its opcode + names and a call site the `call` of the method it names, whose target the method's link table + gives, at `call` or constrained `callvirt`, and its construction at `newobj`. Any other + opcode, another operand kind that instantiation rewrites, or a malformed or out-of-range + operand gives `none`. A hole is never a decoding, so `Instruction.ofRaw` can write one where + this admits nothing. + + `previous` is the raw instruction before `raw`, `none` at the first pc. An instruction + after a prefix it does not take does not decode. `ldelema` takes `readonly.` and records + it, so after `readonly.` only `ldelema` decodes, and `ldelema` after any other prefix does + not. `readonly.` decodes to `nop`, and not after any prefix. `unaligned.` with an operand of + 1, 2 or 4 decodes to `nop`, and only the takers `prefixTakes` names decode after it. The + integer `ldind` and `stind` forms decode as without it, since the model accesses memory at + alignment 1; `cpblk` and `initblk` decode to the block operation at the alignment it gives, + or `defaultBlockAlignment` without it (`blockAlignment`). A constrained prefix decodes to + `nop` and its call records the receiver. -/ +def Instruction.ofRaw? (previous : Option RawInstruction) (raw : RawInstruction) : + Option Instruction := + Option.filter (fun decoded => !(decoded matches .unadmitted _)) <| + if invalidPredecessor previous raw then none else + match raw.operand with + | .type type => Instruction.ofType? previous raw.opcode type + | .field owner _ _ _ false => + if owner.objectApplication then none else + (FieldRef.ofOperand? raw.operand).bind (Instruction.instanceField? raw.opcode) + | .call site => Instruction.ofCallSite? previous raw.opcode site + | .indirectCall result parameters hasThis explicitThis convention => + if raw.opcode == 41 && !hasThis && !explicitThis && convention == 0 then + some (.calli (ManagedSignature.ofTypes result parameters) + (callPrefixes previous).tail) + else none + | _ => + if raw.operand.mentionsType || !Instruction.valueOpcode raw.opcode then none else + (plainValue? previous raw).map Subtype.val + +/-- `ofRaw?` decodes only the opcodes of `Instruction.valueOpcode`. -/ +theorem Instruction.valueOpcode_of_ofRaw? {previous : Option RawInstruction} {raw : RawInstruction} + {instruction : Instruction} (decoded : Instruction.ofRaw? previous raw = some instruction) : + Instruction.valueOpcode raw.opcode = true := by + cases opcodeValue : Instruction.valueOpcode raw.opcode + · have found := (Option.filter_eq_some_iff.mp decoded).1 + split at found + · cases found + · split at found + · rcases Instruction.ofType?_opcode found + with opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs | + opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs <;> + rw [opcodeIs] at opcodeValue <;> exact absurd opcodeValue (by decide) + · split at found + · cases found + obtain ⟨_, _, accessed⟩ := Option.bind_eq_some_iff.mp found + rcases Instruction.instanceField?_opcode accessed with opcodeIs | opcodeIs | opcodeIs <;> + rw [opcodeIs] at opcodeValue <;> exact absurd opcodeValue (by decide) + · rcases Instruction.ofCallSite?_opcode found with + opcodeIs | opcodeIs | opcodeIs | opcodeIs <;> + rw [opcodeIs] at opcodeValue <;> exact absurd opcodeValue (by decide) + · split at found + · simp_all [Instruction.valueOpcode] + · cases found + · rw [opcodeValue, Bool.not_false, Bool.or_true, + ite_eq_left_of_eq_true _ _ (eq_true rfl)] at found + cases found + · rfl + +#print axioms Instruction.valueOpcode_of_ofRaw? + +/-- No raw instruction decodes to a hole: a hole is what `Instruction.ofRaw` writes where + `ofRaw?` admits nothing. -/ +theorem Instruction.ofRaw?_ne_unadmitted (previous : Option RawInstruction) (raw : RawInstruction) + (opcode : Nat) : Instruction.ofRaw? previous raw ≠ some (.unadmitted opcode) := fun decoded => + Bool.false_ne_true (Option.filter_eq_some_iff.mp decoded).2 + +#print axioms Instruction.ofRaw?_ne_unadmitted + +/-- `readonly.` decodes only without an operand, and to `nop`. -/ +theorem Instruction.ofRaw?_readOnly {previous : Option RawInstruction} {raw : RawInstruction} + {decoded : Instruction} (readOnly : raw.opcode = 65054) + (decodes : Instruction.ofRaw? previous raw = some decoded) : + raw.operand = .none ∧ decoded = .nop := by + obtain ⟨offset, opcode, operand⟩ := raw + dsimp only at readOnly + subst readOnly + have found := (Option.filter_eq_some_iff.mp decodes).1 + split at found + · cases found + · split at found + · rcases Instruction.ofType?_opcode found + with opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs | + opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs | opcodeIs <;> + cases opcodeIs + · split at found + · cases found + obtain ⟨_, _, accessed⟩ := Option.bind_eq_some_iff.mp found + rcases Instruction.instanceField?_opcode accessed with opcodeIs | opcodeIs | opcodeIs <;> + cases opcodeIs + · rcases Instruction.ofCallSite?_opcode found with + opcodeIs | opcodeIs | opcodeIs | opcodeIs <;> + cases opcodeIs + · cases found + · cases operand + case none => + change (if previous.any (fun before => isPrefixOpcode before.opcode) then none + else emit .nop).map Subtype.val = some decoded at found + split at found + · cases found + · cases found + exact ⟨rfl, rfl⟩ + all_goals cases found + +#print axioms Instruction.ofRaw?_readOnly + +/-- `ldelema` decodes to an element address that is read-only exactly when `previous` is + `readonly.`. -/ +theorem Instruction.ofRaw?_ldelema {previous : Option RawInstruction} {raw : RawInstruction} + {decoded : Instruction} (address : raw.opcode = 143) + (decodes : Instruction.ofRaw? previous raw = some decoded) : + ∃ type, decoded = .ldelema type (previous.any fun before => before.opcode == 65054) := by + obtain ⟨offset, opcode, operand⟩ := raw + dsimp only at address + subst address + have found := (Option.filter_eq_some_iff.mp decodes).1 + split at found + · cases found + · split at found + · rename_i type _ + have after : Instruction.ldelemaAfter? previous type = some decoded := found + unfold Instruction.ldelemaAfter? at after + refine ⟨type, ?_⟩ + split at after + · rename_i prefixed + cases after + simp [prefixed] + · rename_i unprefixed + split at after + · cases after + · cases after + simp [unprefixed] + · split at found + · cases found + obtain ⟨_, _, accessed⟩ := Option.bind_eq_some_iff.mp found + rcases Instruction.instanceField?_opcode accessed with opcodeIs | opcodeIs | opcodeIs <;> + cases opcodeIs + · rcases Instruction.ofCallSite?_opcode found with + opcodeIs | opcodeIs | opcodeIs | opcodeIs <;> + cases opcodeIs + · cases found + · cases operand <;> cases found + +#print axioms Instruction.ofRaw?_ldelema + +/-- The instruction a raw instruction after `previous` denotes: its value decoding where `ofRaw?` + admits one, and otherwise a hole carrying the raw opcode, which faults when stepped. -/ +def Instruction.ofRaw (previous : Option RawInstruction) (raw : RawInstruction) : Instruction := + (Instruction.ofRaw? previous raw).getD (.unadmitted raw.opcode) + +/-- Decode an ordinary closed site. Type handles require a recognized comparison, and object + transfers and sizes require an admitted closed operand. -/ +def Instruction.ofClosedValue (previous : Option RawInstruction) (raw : RawInstruction) : + Instruction := + if (match raw.operand with + | .type type => raw.opcode == 208 || objectOpcode raw.opcode && !objectApplicationAdmitted type + | _ => false) then .unadmitted raw.opcode + else Instruction.ofRaw previous raw + +/-- Decoding gives a hole exactly when `ofRaw?` admits nothing, and the hole carries the raw + opcode; an instruction `ofRaw?` admits is never a hole. -/ +theorem Instruction.ofRaw_eq_unadmitted_iff (previous : Option RawInstruction) + (raw : RawInstruction) (opcode : Nat) : + Instruction.ofRaw previous raw = .unadmitted opcode ↔ + Instruction.ofRaw? previous raw = none ∧ opcode = raw.opcode := by + unfold Instruction.ofRaw + cases decoded : Instruction.ofRaw? previous raw with + | none => + simp only [Option.getD_none, Instruction.unadmitted.injEq, true_and] + exact eq_comm + | some instruction => + simp only [Option.getD_some, reduceCtorEq, false_and, iff_false] + intro hole + subst hole + exact Instruction.ofRaw?_ne_unadmitted previous raw opcode decoded + +#print axioms Instruction.ofRaw_eq_unadmitted_iff + +/-- An instruction `ofRaw?` admits decodes to itself. -/ +theorem Instruction.ofRaw_of_ofRaw? {previous : Option RawInstruction} {raw : RawInstruction} + {instruction : Instruction} (decoded : Instruction.ofRaw? previous raw = some instruction) : + Instruction.ofRaw previous raw = instruction := by + rw [Instruction.ofRaw, decoded, Option.getD_some] + +#print axioms Instruction.ofRaw_of_ofRaw? + +/-- An opcode outside the value opcodes decodes to a hole carrying it. -/ +theorem Instruction.ofRaw_of_not_valueOpcode {previous : Option RawInstruction} + {raw : RawInstruction} (outside : Instruction.valueOpcode raw.opcode = false) : + Instruction.ofRaw previous raw = .unadmitted raw.opcode := by + cases decoded : Instruction.ofRaw? previous raw with + | none => rw [Instruction.ofRaw, decoded, Option.getD_none] + | some _ => + rw [Instruction.valueOpcode_of_ofRaw? decoded] at outside + cases outside + +#print axioms Instruction.ofRaw_of_not_valueOpcode + +/-- The raw instruction before `pc` that `rawAt` gives; `none` at the first pc, which has no + instruction before it. -/ +def rawBefore (rawAt : Nat → Option RawInstruction) (pc : Nat) : Option RawInstruction := + if pc == 0 then none else rawAt (pc - 1) + +/-- A site can consult its enclosing method's bindings and profile. -/ +abbrev SiteDecoder := Nat → Option RawInstruction → RawInstruction → Instruction + +/-- The instruction at `pc`, with the preceding raw instruction available for prefixes. -/ +def Instruction.ofRawAt (code : List RawInstruction) (pc : Nat) + (decode : Nat → Option RawInstruction → RawInstruction → Instruction := + fun _ => Instruction.ofRaw) : Option Instruction := + code[pc]?.map (decode pc (rawBefore (code[·]?) pc)) + +/-- Decode each instruction at its pc, carrying the raw predecessor for prefix checks. -/ +def Instruction.ofRawCode (previous : Option RawInstruction) (code : List RawInstruction) + (decode : Nat → Option RawInstruction → RawInstruction → Instruction := + fun _ => Instruction.ofRaw) (start : Nat := 0) : List Instruction := + match code with + | [] => [] + | raw :: rest => decode start previous raw :: ofRawCode (some raw) rest decode (start + 1) + +/-- The sequential decoder and indexed decoder see the same pc and raw predecessor. -/ +theorem Instruction.ofRawCode_getElem? (code : List RawInstruction) (pc : Nat) + (decode : Nat → Option RawInstruction → RawInstruction → Instruction := + fun _ => Instruction.ofRaw) : + (Instruction.ofRawCode none code decode)[pc]? = Instruction.ofRawAt code pc decode := by + suffices walked : ∀ (code : List RawInstruction) (previous : Option RawInstruction) + (start pc : Nat), (Instruction.ofRawCode previous code decode start)[pc]? = + code[pc]?.map (decode (start + pc) (if pc == 0 then previous else code[pc - 1]?)) by + simpa [Instruction.ofRawAt, rawBefore] using walked code none 0 pc + intro code + induction code with + | nil => intro previous start pc; simp [Instruction.ofRawCode] + | cons raw rest walkedRest => + intro previous start pc + cases pc with + | zero => simp [Instruction.ofRawCode] + | succ pc => + rw [Instruction.ofRawCode, List.getElem?_cons_succ, walkedRest, List.getElem?_cons_succ] + rw [Nat.add_right_comm start 1 pc] + cases pc <;> rfl + +#print axioms Instruction.ofRawCode_getElem? + +/-- A decoded value is the emitted instruction exactly when `sameValue` accepts the pair. -/ +theorem Instruction.sameValue_eq {decoded emitted : Instruction} + (same : decoded.sameValue emitted = true) : emitted = decoded := by + cases decoded <;> simp only [Instruction.sameValue] at same + all_goals first + | exact absurd same Bool.false_ne_true + | (repeat' split at same) <;> simp_all + +#print axioms Instruction.sameValue_eq + +/-- Every instruction produced by the partial decoder matches its own value encoding. -/ +theorem Instruction.sameValue_self_of_ofRaw? {previous : Option RawInstruction} + {raw : RawInstruction} {emitted : Instruction} + (decoded : Instruction.ofRaw? previous raw = some emitted) : + emitted.sameValue emitted = true := by + have found := (Option.filter_eq_some_iff.mp decoded).1 + split at found + · cases found + · split at found + · unfold Instruction.ofType? at found + repeat' split at found + all_goals try unfold Instruction.ldelemaAfter? at found + all_goals repeat' split at found + all_goals cases found <;> simp [Instruction.sameValue] + · split at found + · cases found + obtain ⟨_, _, found⟩ := Option.bind_eq_some_iff.mp found + unfold Instruction.instanceField? at found + repeat' split at found + all_goals cases found <;> simp [Instruction.sameValue] + · unfold Instruction.ofCallSite? at found + repeat' split at found + all_goals cases found <;> simp [Instruction.sameValue] + · split at found <;> cases found <;> simp [Instruction.sameValue] + · split at found + · cases found + · obtain ⟨value, _, rfl⟩ := Option.map_eq_some_iff.mp found + exact value.property + +#print axioms Instruction.sameValue_self_of_ofRaw? + +/-- Instantiation preserves the operand information that a preceding modifier checks. -/ +private theorem invalidPredecessor_substitute {previous : Option RawInstruction} + {raw : RawInstruction} {operand : RawOperand} {arguments : List TypeExpr} + {layouts : List RawLayout} {arity : Nat} + (substituted : substituteOperand arguments layouts arity raw.operand = some operand) : + invalidPredecessor previous { raw with operand } = invalidPredecessor previous raw := by + cases previous with + | none => rfl + | some modifier => + simp only [invalidPredecessor, Option.any_some] + congr 2 + obtain ⟨offset, opcode, original⟩ := raw + cases original <;> simp only [substituteOperand, Option.map_eq_map, Option.map_eq_some_iff, + Option.bind_eq_bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at substituted + case' type => + obtain ⟨_, _, rfl⟩ := substituted + case' field owner index type name isStatic => + obtain ⟨_, _, _, _, rfl⟩ := substituted + cases isStatic + case' call => + obtain ⟨_, _, substituted⟩ := substituted + split at substituted <;> + simp only [Option.bind_eq_some_iff, Option.some.injEq] at substituted + all_goals obtain ⟨_, _, _, _, _, _, _, _, _, _, rfl⟩ := substituted + case' indirectCall => + obtain ⟨_, _, _, _, rfl⟩ := substituted + all_goals try (subst operand; rfl) + all_goals unfold prefixTakes + all_goals dsimp only + all_goals split <;> first | rfl | (split <;> simp_all <;> subst_vars <;> rfl) +#print axioms invalidPredecessor_substitute + +/-- Substitution decodes managed addresses and indirect signatures from their closed operands. + Other non-field instructions preserve admission and instantiate their type or method. -/ +theorem Instruction.ofRaw?_substitute {previous : Option RawInstruction} {raw : RawInstruction} + {operand : RawOperand} {arguments : List TypeExpr} {layouts : List RawLayout} {arity : Nat} + (substituted : substituteOperand arguments layouts arity raw.operand = some operand) + (unnamed : FieldRef.ofOperand? raw.operand = none) : + Instruction.ofRaw? previous { raw with operand } = + match raw.operand, operand with + | .type _, .type type => + (Instruction.ofRaw? previous raw).bind fun _ => + Instruction.ofType? previous raw.opcode type + | .call _, .call site => + if raw.opcode == 65030 then + if invalidPredecessor previous raw then none else + Instruction.ofCallSite? previous raw.opcode site + else (Instruction.ofRaw? previous raw).bind fun _ => + Instruction.ofCallSite? previous raw.opcode site + | .indirectCall .., .indirectCall result parameters hasThis explicitThis convention => + if !invalidPredecessor previous raw && raw.opcode == 41 && !hasThis && + !explicitThis && convention == 0 then + some (.calli (ManagedSignature.ofTypes result parameters) + (callPrefixes previous).tail) + else none + | _, _ => Instruction.ofRaw? previous raw := by + have sameGuard := invalidPredecessor_substitute (previous := previous) (raw := raw) substituted + obtain ⟨sameKind, sameType, sameCall, sameField, unchanged⟩ := + substituteOperand_mentionsType substituted + obtain ⟨offset, opcode, original⟩ := raw + cases original + case field => cases unnamed + case type before => + cases operand + case type type => + have staysSpan (span : before.isRuntimeSpan = true) : type.isRuntimeSpan = true := by + obtain ⟨closed, storage, same⟩ := Option.map_eq_some_iff.mp substituted + cases same + exact substituteStorageType_span span storage + show Option.filter _ (if _ then none else Instruction.ofType? previous opcode type) = + (Option.filter _ (if _ then none else Instruction.ofType? previous opcode before)).bind + fun _ => Instruction.ofType? previous opcode type + rw [sameGuard] + split + · rfl + · unfold Instruction.ofType? Instruction.ldelemaAfter? + split <;> repeat' first | rfl | split + all_goals exact False.elim (by solve | simp_all) + all_goals exact absurd sameType Bool.false_ne_true + case call before => + cases operand + case call site => + have receiver : site.hasThis = before.hasThis := by + simp only [substituteOperand, Option.bind_eq_bind, Option.bind_eq_some_iff, pure] + at substituted + obtain ⟨_, _, substituted⟩ := substituted + split at substituted <;> + simp only [Option.bind_eq_some_iff, Option.some.injEq, RawOperand.call.injEq] + at substituted <;> + obtain ⟨_, _, _, _, _, _, _, _, _, _, rfl⟩ := substituted <;> rfl + simp only [Instruction.ofRaw?, sameGuard] + by_cases address : opcode = 65030 + · subst opcode + simp only [beq_self_eq_true, ↓reduceIte] + split + · rfl + · unfold Instruction.ofCallSite? + simp only [Nat.reduceBEq, Bool.false_and, Bool.false_eq_true, ↓reduceIte, receiver] + repeat' first | rfl | split + · simp only [beq_iff_eq, address, ↓reduceIte] + split + · rfl + · unfold Instruction.ofCallSite? + simp only [receiver, beq_iff_eq, address, ↓reduceIte] + repeat' first | rfl | split + all_goals exact absurd sameCall Bool.false_ne_true + case indirectCall => + simp only [substituteOperand, Option.bind_eq_bind, Option.bind_eq_some_iff, pure, + Option.some.injEq] at substituted + obtain ⟨_, _, _, _, rfl⟩ := substituted + simp only [Instruction.ofRaw?, sameGuard] + repeat' split <;> simp_all + -- Any other original either is unchanged or, like its substitute, is refused by the guard. + all_goals first + | (cases unchanged rfl <;> rfl) + | (cases operand <;> first + | exact absurd sameKind Bool.false_ne_true + | exact absurd sameType.symm Bool.false_ne_true + | exact absurd sameCall.symm Bool.false_ne_true + | exact absurd sameField Bool.false_ne_true + | exact absurd sameField.symm Bool.false_ne_true + | rfl) + +#print axioms Instruction.ofRaw?_substitute + +/-- Instantiation closes an instance field operand's owner and held type, and the instantiated + instruction, after no prefix it does not take, decodes to the field instruction of the same + opcode over the closed reference unless the closed owner is a generic class instance. -/ +theorem Instruction.ofRaw?_substitute_instance_field {previous : Option RawInstruction} + {raw : RawInstruction} {operand : RawOperand} + {arguments : List TypeExpr} {layouts : List RawLayout} {arity : Nat} + {owner : TypeExpr} {index : Nat} {held : TypeExpr} {fieldName : MetadataName} + (substituted : substituteOperand arguments layouts arity raw.operand = some operand) + (named : raw.operand = .field owner index held fieldName false) + (accepted : invalidPredecessor previous { raw with operand } = false) : + ∃ closedOwner closed, substituteStorageType layouts arguments owner = some closedOwner ∧ + substituteStorageType layouts arguments held = some closed ∧ + operand = .field closedOwner index closed fieldName false ∧ + Instruction.ofRaw? previous { raw with operand } = + if closedOwner.objectApplication then none + else Instruction.instanceField? raw.opcode + { owner := closedOwner, index, type := closed, name := fieldName } := by + obtain ⟨offset, opcode, original⟩ := raw + dsimp only at named + subst named + simp only [substituteOperand, Option.bind_eq_bind, Option.bind_eq_some_iff, pure, + Option.some.injEq] at substituted + obtain ⟨closedOwner, holdsOwner, closed, holdsClosed, rfl⟩ := substituted + refine ⟨closedOwner, closed, holdsOwner, holdsClosed, rfl, ?_⟩ + show Option.filter _ (if _ then none else + if closedOwner.objectApplication then none + else Instruction.instanceField? opcode _) = _ + simp only [accepted, Bool.false_eq_true, ite_false] + cases closedOwner.objectApplication + · simp only [Bool.false_eq_true, ite_false] + unfold Instruction.instanceField? + split + · rfl + · split + · rfl + · split <;> rfl + · rfl + +#print axioms Instruction.ofRaw?_substitute_instance_field + +end CIL + +namespace CIL.Extraction + +/-- Whether `emitted` is exactly what `raw` decodes to after `previous`, the raw instruction + before it. -/ +def valueEncodingMatches (previous : Option RawInstruction) (raw : RawInstruction) + (emitted : Instruction) : Bool := + match Instruction.ofRaw? previous raw with + | some decoded => decoded.sameValue emitted + | none => false + +/-- A matching value encoding is exactly the decoded instruction. -/ +theorem valueEncodingMatches_sound {previous : Option RawInstruction} {raw : RawInstruction} + {emitted : Instruction} (matched : valueEncodingMatches previous raw emitted = true) : + Instruction.ofRaw? previous raw = some emitted := by + unfold valueEncodingMatches at matched + split at matched + · next decoded decodes => rw [decodes, Instruction.sameValue_eq matched] + · exact absurd matched Bool.false_ne_true + +#print axioms valueEncodingMatches_sound + +/-- A value-encoding check accepts exactly the decoded instruction at this site. -/ +theorem valueEncodingMatches_iff {previous : Option RawInstruction} {raw : RawInstruction} + {emitted : Instruction} : + valueEncodingMatches previous raw emitted = true ↔ + Instruction.ofRaw? previous raw = some emitted := by + refine ⟨valueEncodingMatches_sound, fun decoded => ?_⟩ + simpa only [valueEncodingMatches, decoded] using Instruction.sameValue_self_of_ofRaw? decoded + +#print axioms valueEncodingMatches_iff + +end CIL.Extraction diff --git a/CIL/Extraction/VectorHelpers.lean b/CIL/Extraction/VectorHelpers.lean new file mode 100644 index 000000000..69d0dd821 --- /dev/null +++ b/CIL/Extraction/VectorHelpers.lean @@ -0,0 +1,153 @@ +import CIL.Extraction.ModelTable +import CIL.Extraction.TransferKinds + +/-! Runtime models for portable vector construction, whole-vector equality and byte transfers. -/ + +namespace CIL.Extraction + +/-- One vector declaration and the operation its admitted instantiations execute. -/ +private inductive VectorMember where + | create (width bits count : Nat) + | combine + | span (width : Nat) + | equal (width : Nat) + | transfer (store offset : Bool) + +/-- The helper class or vector constructor, and its unscoped metadata spelling. -/ +private def vectorOwnerName (width : Nat) := s!"System.Runtime.Intrinsics.Vector{width}" + +/-- A signature component and the text the external member reference spells for it. -/ +private abbrev VectorOperand := TypeExpr × String + +/-- The named runtime primitive. -/ +private def primitiveOperand (name : String) : VectorOperand := + (.atom runtimeScope name, name) + +/-- The vector constructor applied to an open parameter or concrete element. -/ +private def vectorOperand (width : Nat) (element : VectorOperand) : VectorOperand := + let head := vectorOwnerName width ++ "`1" + (.apply (.constructor intrinsicsScope head) element.1, s!"{head}<{element.2}>") + +/-- The single-word broadcast used by the AVX2 execution lemmas. -/ +def broadcast256Method : MethodRef := + let word := primitiveOperand "System.UInt64" + { owner := .atom intrinsicsScope (vectorOwnerName 256), name := name!"Create", hasThis := false, + genericArity := 0, returnType := (vectorOperand 256 word).1, parameters := [word.1] } + +/-- The result and parameters of a declaration at the supplied type argument. -/ +private def VectorMember.signature (member : VectorMember) (element : VectorOperand) : + VectorOperand × List VectorOperand := + match member with + | .create width bits count => + let word := primitiveOperand (if bits == 32 then "System.UInt32" else "System.UInt64") + (vectorOperand width word, List.replicate count word) + | .combine => + let word := primitiveOperand "System.UInt64" + (vectorOperand 512 word, List.replicate 2 (vectorOperand 256 word)) + | .span width => + (vectorOperand width element, [(.apply (.constructor runtimeScope "System.ReadOnlySpan`1") + element.1, s!"System.ReadOnlySpan`1<{element.2}>")]) + | .equal width => + (primitiveOperand "System.Boolean", List.replicate 2 (vectorOperand width element)) + | .transfer store offset => + let vector := vectorOperand 128 element + let address := (TypeExpr.byref element.1, element.2 ++ "&") + let tail := if offset then [primitiveOperand "System.UIntPtr"] else [] + (if store then (.void, "System.Void") else vector, + (if store then [vector, address] else [address]) ++ tail) + +/-- The existing checked operation for this overload. -/ +private def VectorMember.model (reader : CallLayouts) (method : MethodRef) : + VectorMember → Option RuntimeModel + | .create width bits count => + some (.intrinsic (.vector (if bits == 32 then .create32 width else .create64 width)) count) + | .combine => some (.intrinsic (.vector .combine256) 2) + | .span _ => (transferKind? reader method.returnType).map fun kind => + .memory (.marshalRead kind false) + | .equal width => some (.intrinsic (.vector (.equalsAll width)) 2) + | .transfer store offset => some (.memory (.vector128Unsafe store offset)) + +/-- The element of a concrete vector equality reference, or the byte of a generic helper. -/ +private def VectorMember.element (member : VectorMember) (method : MethodRef) : + Option VectorOperand := + match member with + | .equal _ => do + guard (integerVectorWidth method.owner).isSome + let .apply _ (.atom _ name) := method.owner | none + return primitiveOperand name + | _ => some (primitiveOperand "System.Byte") + +/-- The open operand and disjoint closed signature for one declaration in a reserved slot. -/ +private def VectorMember.row (member : VectorMember) (width : Nat) (key : MetadataName) + (name : String) : ModelOverload := + let generic := member matches .span _ | .transfer .. + let equal := member matches .equal _ + let argument : VectorOperand := + if equal then (.ownerParameter 0, "!0") else (.parameter 0, "!!0") + let (result, parameters) := member.signature argument + let decl : OpenSignature := + { convention := if generic then 16 else 0, returnType := result.1, + parameters := parameters.map Prod.fst } + let owner := vectorOwnerName width + let target (method : MethodRef) := do + let element ← member.element method + let declaring := if equal then (vectorOperand width element).2 else owner + let names := ",".intercalate (parameters.map Prod.snd) + return s!"{intrinsicsScope}::{result.2} {declaring}::{name}({names})" + let accepts (method : MethodRef) := (member.element method).any fun element => + let (closedResult, closedParams) := member.signature element + let declaring := if equal then (vectorOperand width element).1 else .atom intrinsicsScope owner + method.owner == declaring && method.name == key && !method.hasThis && + method.genericArity == (if generic then 1 else 0) && + method.methodArguments == (if generic then [element.1] else []) && + method.returnType == closedResult.1 && method.parameters == closedParams.map Prod.fst + { operand := .external (fun _ => decl) name target, accepts, + resolve := fun reader method => member.model reader method } + +/-- A vector name owns all its overloads at the given owner head. -/ +private def vectorSlot (width : Nat) (key : MetadataName) (name : String) + (members : List VectorMember) (genericOwner := false) : ModelEntry := + let owner := vectorOwnerName width + { opcode := .call, name := key, ownsName := true, + owner := if genericOwner then .constructor intrinsicsScope (owner ++ "`1") + else .atom intrinsicsScope owner, + overloads := members.map (fun member => member.row width key name) } + +/-- The four scalar and span constructions of a 128- or 256-bit vector. -/ +private def createSlot (width : Nat) : ModelEntry := + vectorSlot width name!"Create" "Create" + [.create width 64 1, .create width 64 (width / 64), .create width 32 (width / 32), .span width] + +/-- The declarations for portable construction, equality and byte-reference transfer. -/ +def vectorHelperEntries : List ModelEntry := + [createSlot 128, createSlot 256, vectorSlot 512 name!"Create" "Create" [.combine], + vectorSlot 128 name!"LoadUnsafe" "LoadUnsafe" [.transfer false false, .transfer false true], + vectorSlot 128 name!"StoreUnsafe" "StoreUnsafe" [.transfer true false, .transfer true true]] ++ + [128, 256, 512].map (fun width => + vectorSlot width name!"op_Equality" "op_Equality" [.equal width] true) + +/-- Every model of a vector overload runs the declared intrinsic or checked transfer. -/ +theorem vectorHelperEntries_yields {property : RuntimeModel → Prop} + (intrinsic : ∀ operation count, property (.intrinsic operation count)) + (memory : ∀ operation, property (.memory operation)) : + ∀ entry ∈ vectorHelperEntries, entry.Yields property := by + have slot (width key name members genericOwner) : + (vectorSlot width key name members genericOwner).Yields property := by + intro reader method model found + obtain ⟨row, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + obtain ⟨member, _, rfl⟩ := List.mem_map.mp (ModelEntry.select?_mem selected) + change member.model reader method = some model at resolved + cases member <;> simp only [VectorMember.model, Option.map_eq_some_iff, Option.some.injEq] + at resolved + all_goals first + | subst model; apply intrinsic + | subst model; apply memory + | obtain ⟨_, _, rfl⟩ := resolved; apply memory + intro entry listed + simp only [vectorHelperEntries, List.mem_append, List.mem_cons, List.not_mem_nil, or_false, + List.mem_map] at listed + rcases listed with (rfl | rfl | rfl | rfl | rfl) | ⟨_, _, rfl⟩ <;> apply slot + +#print axioms vectorHelperEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/VectorInitialization.lean b/CIL/Extraction/VectorInitialization.lean deleted file mode 100644 index c2e5b56eb..000000000 --- a/CIL/Extraction/VectorInitialization.lean +++ /dev/null @@ -1,93 +0,0 @@ -import CIL.Extraction.Generics - -/-! -# Vector initialization - -`initobj` (0xFE15) zeroes its target, so its emitted `initAggregate` must zero exactly the -type's width. `VectorInitializationsValid` checks that an `initAggregate` emitted for a live -`initobj` of a struct, an admitted integer vector or a primitive has the right width. -Vector object loads, stores, copies and sizes are also bound to their exact type token and -width. Loads and stores retain initialization masks; typed copies require initialized bytes. -Aggregate widths themselves are bound by the layout checks. --/ - -namespace CIL.Extraction - -/-- `Vector128`, `Vector256` or `Vector512` for any `T` and scope. -/ -private def vectorFamily : TypeExpr → Bool - | .apply (.constructor _ name) _ => name ∈ ["System.Runtime.Intrinsics.Vector128`1", - "System.Runtime.Intrinsics.Vector256`1", "System.Runtime.Intrinsics.Vector512`1"] - | _ => false - -/-- The byte width of a primitive integer, `bool` or `char` type. -/ -private def primitiveWidth (type : TypeExpr) : Option Nat := do - let .field kind ← typeStorage [] type | none - primitiveFieldWidth kind - -/-- Whether an emitted memory operation transfers a vector snapshot. -/ -private def vectorMemory : Op → Bool - | .memory .load128 | .memory .load256 | .memory .load512 - | .memory .store128 | .memory .store256 | .memory .store512 - | .memory (.vector128Unsafe ..) => true - | _ => false - -/-- Vector object operations preserve the exact token's storage width and opcode semantics. -/ -def vectorObjectMatches (raw : RawInstruction) (emitted : Op) : Bool := - match raw.operand with - | .type type => - if !vectorFamily type || raw.opcode ∉ [0x70, 0x71, 0x81, 0xfe15, 0xfe1c] then - if raw.opcode ∈ [0x71, 0x81] && vectorMemory emitted then false else - match raw.opcode, type, emitted with - | 0x70, .aggregate .., _ => true - | 0x70, _, .copyObject width => primitiveWidth type == some width - | _, _, _ => true - else match integerVectorWidth type with - | none => false - | some width => match raw.opcode, emitted with - | 0x70, .copyObject bytes | 0xfe15, .initAggregate bytes => bytes == width - | 0xfe1c, .const32 bits => bits.toNat == width - | 0x71, .memory .load128 | 0x81, .memory .store128 => width == 16 - | 0x71, .memory .load256 | 0x81, .memory .store256 => width == 32 - | 0x71, .memory .load512 | 0x81, .memory .store512 => width == 64 - | _, _ => false - | _ => raw.opcode ∉ [0x70, 0x71, 0x81, 0xfe15, 0xfe1c] - -/-- An emitted `initAggregate` comes from an `initobj` of a struct, whose width the layout - check binds, or of a vector or primitive of exactly that width; no other emission may - come from an `initobj` of a vector type. -/ -def vectorInitializationMatches (raw : RawInstruction) (emitted : Op) : Bool := - vectorObjectMatches raw emitted && match emitted with - | .initAggregate width => - raw.opcode == 65045 && match raw.operand with - | .type (.aggregate _ _) => true - | .type type => (integerVectorWidth type).orElse (fun _ => primitiveWidth type) == some width - | _ => false - | _ => !(raw.opcode == 65045 && match raw.operand with - | .type type => vectorFamily type - | _ => false) - -/-- The profile is valid and every live instruction passes `vectorInitializationMatches`. -/ -def VectorInitializationsValid (closed : RawMethod) (reachable : List Bool) (method : Method) : - Prop := - method.profile.Valid ∧ closed.code.length = reachable.length ∧ - closed.code.length = method.code.length ∧ - all3 (fun raw live emitted => !live || vectorInitializationMatches raw emitted) - closed.code reachable method.code = true - -instance (closed : RawMethod) (reachable : List Bool) (method : Method) : - Decidable (VectorInitializationsValid closed reachable method) := - inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _)) - -theorem vector_initialization_checked {closed : RawMethod} {reachable : List Bool} - {method : Method} - (valid : VectorInitializationsValid closed reachable method) (pc : Nat) - (instruction : RawInstruction) (emitted : Op) - (rawAt : closed.code[pc]? = some instruction) (liveAt : reachable[pc]? = some true) - (emittedAt : method.code[pc]? = some emitted) : - vectorInitializationMatches instruction emitted = true := by - have checked := all3_at valid.2.2.2 rawAt liveAt emittedAt - simpa using checked - -#print axioms vector_initialization_checked - -end CIL.Extraction diff --git a/CIL/Extraction/VectorModels.lean b/CIL/Extraction/VectorModels.lean new file mode 100644 index 000000000..1c6e87c9e --- /dev/null +++ b/CIL/Extraction/VectorModels.lean @@ -0,0 +1,69 @@ +import CIL.Extraction.ModelTable +import CIL.Extraction.TypeStorage + +/-! +# Vector helper models + +The runtime models of the vector helpers a call keeps: `Vector128.CreateScalarUnsafe` of a +`ulong`, which sets the low lane and leaves the upper lane unspecified, and `GetElement` of a +`ulong` lane of a 128- or 256-bit vector. Each entry stands for exactly one instantiated +reference of the exact `System.Runtime.Intrinsics` assembly under `call`, so a helper of +another owner, assembly, element type or signature has no model. +-/ + +namespace CIL.Extraction + +/-- `vector` for the generic vector type named `vector`, with exact scopes. The names + are literals, so stepping never evaluates a string to compare a call's method. -/ +def ulongVectorOf (vector : String) : TypeExpr := + .apply (.constructor intrinsicsScope vector) (.atom runtimeScope "System.UInt64") + +/-- `Vector128 Vector128.CreateScalarUnsafe(ulong)`. -/ +def createScalarUnsafeMethod : MethodRef := + { owner := .atom intrinsicsScope "System.Runtime.Intrinsics.Vector128", + name := name!"CreateScalarUnsafe", hasThis := false, genericArity := 0, + returnType := ulongVectorOf "System.Runtime.Intrinsics.Vector128`1", + parameters := [.atom runtimeScope "System.UInt64"] } + +/-- `ulong owner.GetElement(vector, int)`, as a call site instantiates it. -/ +def getElementMethod (owner vector : String) : MethodRef := + { owner := .atom intrinsicsScope owner, name := name!"GetElement", hasThis := false, + genericArity := 1, returnType := .atom runtimeScope "System.UInt64", + parameters := [ulongVectorOf vector, .atom runtimeScope "System.Int32"], + methodArguments := [.atom runtimeScope "System.UInt64"] } + +/-- `GetElement` of a `Vector128` lane. -/ +def getElement128Method : MethodRef := + getElementMethod "System.Runtime.Intrinsics.Vector128" "System.Runtime.Intrinsics.Vector128`1" + +/-- `GetElement` of a `Vector256` lane. -/ +def getElement256Method : MethodRef := + getElementMethod "System.Runtime.Intrinsics.Vector256" "System.Runtime.Intrinsics.Vector256`1" + +/-- The declaration of a generic lane read from the named vector type. -/ +def getElementSignature (vector : String) : OpenSignature := + { convention := 0x10, returnType := .parameter 0, + parameters := [.apply (.constructor intrinsicsScope vector) (.parameter 0), + .atom runtimeScope "System.Int32"] } + +/-- The vector helper entries: each stands for one exact reference and runs as its intrinsic. -/ +def vectorEntries : List ModelEntry := + [.exact .call createScalarUnsafeMethod (createScalarUnsafeMethod.signature 0) + (.intrinsic (.vector .createScalarUnsafe64) 1), + .exact .call getElement128Method (getElementSignature "System.Runtime.Intrinsics.Vector128`1") + (.intrinsic (.vector (.extract64 128)) 2), + .exact .call getElement256Method (getElementSignature "System.Runtime.Intrinsics.Vector256`1") + (.intrinsic (.vector (.extract64 256)) 2)] + +/-- Every model a vector helper entry resolves is an intrinsic, so a property every intrinsic + model has holds of them. -/ +theorem vectorEntries_yields {property : RuntimeModel → Prop} + (intrinsic : ∀ operation argc, property (.intrinsic operation argc)) : + ∀ entry ∈ vectorEntries, entry.Yields property := by + simp only [vectorEntries, List.mem_cons, List.not_mem_nil, or_false, forall_eq_or_imp, + forall_eq, ModelEntry.exact_yields] + exact ⟨intrinsic _ _, intrinsic _ _, intrinsic _ _⟩ + +#print axioms vectorEntries_yields + +end CIL.Extraction diff --git a/CIL/Extraction/ViewObservations.lean b/CIL/Extraction/ViewObservations.lean deleted file mode 100644 index 23490f1c0..000000000 --- a/CIL/Extraction/ViewObservations.lean +++ /dev/null @@ -1,51 +0,0 @@ -import CIL.Extraction.SpanCalls - -/-! -# Exact view observations - -Span emptiness and pinnable references, and array data references, preserve the identity of -their admitted view types. Their evidence cannot be replaced by another unary operation. --/ - -namespace CIL.Extraction - -/-- The exact accessor signature fixes the observation without consulting element contents. -/ -def viewObservation (raw : RawInstruction) : Option ViewObservation := do - if raw.opcode != 40 then none else pure () - let .call site := raw.operand | none - if !site.returnsValue || site.parameterCount != site.parameterTypes.length then none else pure () - if site.name == "GetArrayDataReference" then - let [element] := site.typeArguments | none - let _ ← arrayElementType element - if site.hasThis || site.parameterTypes != [.array element] || - site.returnType != .byref element || site.declaringType != - .atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal" || - site.target != .external (runtimeScope ++ "::!!0& " ++ - "System.Runtime.InteropServices.MemoryMarshal::GetArrayDataReference(!!0[])") then none - else some .arrayData - else - let .apply (.constructor _ owner) element := site.declaringType | none - let .span _ writable ← spanStorage site.declaringType | none - let elementName ← spanElementName element - if !site.hasThis || !site.typeArguments.isEmpty || !site.parameterTypes.isEmpty then none - else pure () - let target (result : String) := MethodId.external (runtimeScope ++ "::" ++ result ++ - " " ++ owner ++ "<" ++ elementName ++ ">::" ++ site.name ++ "()") - if site.name == "get_IsEmpty" && site.returnType == .atom runtimeScope "System.Boolean" && - site.target == target "System.Boolean" then some .spanEmpty - else if site.name == "GetPinnableReference" then - let result := if writable then "!0&" else - "!0& modreq(System.Runtime.InteropServices.InAttribute)" - let returnType := if writable then TypeExpr.byref element else .atom runtimeScope result - if site.returnType == returnType && site.target == target result then some .spanPinnable - else none - else none - -/-- Accessor calls and observation instructions agree in both directions. -/ -def viewObservationMatches (raw : RawInstruction) (emitted : Op) : Bool := - let actual := match emitted with - | .memory (.observeView observation) => some observation - | _ => none - viewObservation raw == actual - -end CIL.Extraction diff --git a/CIL/Extraction/WideArithmetic.lean b/CIL/Extraction/WideArithmetic.lean new file mode 100644 index 000000000..8bdee5004 --- /dev/null +++ b/CIL/Extraction/WideArithmetic.lean @@ -0,0 +1,108 @@ +import CIL.Extraction.IntegerMath + +/-! Runtime models for System.Math division with remainder and double-width multiplication. -/ + +namespace CIL.Extraction + +/-- The layout identity of a pair of runtime integers, including both argument scopes. -/ +def integerPairIdentity (element : String) : String := + let argument := s!"{runtimeScope}::{element}" + s!"{runtimeScope}::System.ValueTuple`2<{argument},{argument}>" + +/-- The closed tuple resulting from DivRem, with its fields at offsets zero and one word. -/ +def integerPairResult (environment : CallLayouts) (type : TypeExpr) + (name : String) (width : Nat) : Bool := + match type with + | .aggregate identity index => + let validRaw := (layoutAt? environment.indexedRaw index).any fun raw => + match raw.fields with + | [first, second] => raw.identity == identity && raw.packing <= 0 && + (primitiveField name).any fun kind => first.type == kind && second.type == kind + | _ => false + identity == integerPairIdentity name && validRaw && + (layoutAt? environment.layouts index).any fun entry => + entry.alignment == width && entry.layout.size == 2 * width && + match entry.layout.fields with + | [first, second] => first.offset == 0 && first.width == width && + second.offset == width && second.width == width && + !first.aggregate && !second.aggregate + | _ => false + | _ => false + +/-- Each form determines the result representation and whether the last parameter is byref. -/ +private inductive Form where + | tuple | remainderOut | product | productOut + deriving DecidableEq + +/-- The scalar intrinsic or checked out-parameter write for a declared overload. -/ +private def Form.model (form : Form) (width : Nat) (signed : Bool) : RuntimeModel := + match form with + | .tuple => .intrinsic (.scalar (.divRem width signed)) 2 + | .remainderOut => .memory (.divRemOut width) + | .product => .intrinsic (.scalar (.bigMul32 signed)) 2 + | .productOut => .memory (.bigMul64 signed) + +/-- One concrete declaration, including the environment-local index of a tuple result. -/ +private def arithmeticRow (name : String) (form : Form) (element : String) (width : Nat) + (signed : Bool) : ModelOverload := + let word := TypeExpr.atom runtimeScope element + let out := form == .remainderOut || form == .productOut + let parameters := if out then [word, word, .byref word] else [word, word] + let result := match form with + | .tuple => s!"System.ValueTuple`2<{element},{element}>" + | .product => if signed then "System.Int64" else "System.UInt64" + | _ => element + let argumentNames := if out then s!"{element},{element},{element}&" else s!"{element},{element}" + let target := s!"{runtimeScope}::{result} System.Math::{name}({argumentNames})" + let returns := fun (type : TypeExpr) => match form, type with + | .tuple, .aggregate identity _ => identity == integerPairIdentity element + | .tuple, _ => false + | _, _ => type == .atom runtimeScope result + -- These declarations are non-generic, so their raw types equal the selected closed types. + { operand := .external (fun method => method.signature 0) name (fun _ => some target) + accepts := fun method => !method.hasThis && method.genericArity == 0 && + method.methodArguments.isEmpty && method.parameters == parameters && + returns method.returnType + resolve := fun reader method => + if form != .tuple || integerPairResult reader method.returnType element (width / 8) + then some (form.model width signed) else none } + +/-- The twelve division and four multiplication declarations on the runtime Math owner. -/ +def wideArithmeticEntries : List ModelEntry := + let owner := TypeExpr.atom runtimeScope "System.Math" + let division := arithmeticRow "DivRem" + let multiply := arithmeticRow "BigMul" + [{ opcode := .call, owner, name := name!"DivRem", ownsName := true + overloads := (mathIntegers.map fun (name, width, signed) => division .tuple name width signed) + ++ [division .remainderOut "System.Int32" 32 true, + division .remainderOut "System.Int64" 64 true] }, + { opcode := .call, owner, name := name!"BigMul", ownsName := true + overloads := [multiply .product "System.Int32" 32 true, + multiply .product "System.UInt32" 32 false, multiply .productOut "System.Int64" 64 true, + multiply .productOut "System.UInt64" 64 false] }] + +/-- Every admitted arithmetic overload runs an intrinsic or a checked memory operation. -/ +theorem wideArithmeticEntries_yields {property : RuntimeModel → Prop} + (intrinsic : ∀ operation argc, property (.intrinsic operation argc)) + (memory : ∀ operation, property (.memory operation)) : + ∀ entry ∈ wideArithmeticEntries, entry.Yields property := by + intro entry listed reader method model found + obtain ⟨row, selected, resolved⟩ := Option.bind_eq_some_iff.mp found + have member := ModelEntry.select?_mem selected + have holds (name form element width signed) + (produced : (arithmeticRow name form element width signed).resolve reader method = + some model) : property model := by + simp only [arithmeticRow] at produced + split at produced + · cases produced + cases form <;> first | exact intrinsic _ _ | exact memory _ + · contradiction + simp only [wideArithmeticEntries, List.mem_cons, List.not_mem_nil, or_false] at listed + rcases listed with rfl | rfl <;> + simp only [List.mem_append, List.mem_map, List.mem_cons, List.not_mem_nil, or_false] at member + · rcases member with ⟨⟨_, _, _⟩, _, rfl⟩ | rfl | rfl <;> apply holds _ _ _ _ _ resolved + · rcases member with rfl | rfl | rfl | rfl <;> apply holds _ _ _ _ _ resolved + +#print axioms wideArithmeticEntries_yields + +end CIL.Extraction diff --git a/CIL/Features.lean b/CIL/Features.lean index 5f7a446ee..0995cc75d 100644 --- a/CIL/Features.lean +++ b/CIL/Features.lean @@ -10,7 +10,9 @@ it for feature tests and intrinsic availability. Supported deployment policy: .NET 10 CoreCLR on 64-bit little-endian hosts. IsSupported includes OS support and runtime disabling, not just CPU capability. -The runtime enables SSE4.2, AVX2/BMI and AVX-512 capabilities in whole groups; +On x64 the .NET 11 runtime requires x86-64-v2: its X86Base group holds SSE through SSE4.2 and +POPCNT, so SSE2 is never reported without SSSE3, SSE4.1 and SSE4.2. +The runtime enables AVX2/BMI and AVX-512 capabilities in whole groups; AVX-512 VBMI is its own group, which requires AVX-512 and lies outside x86-64-v4. AES is its own group on each architecture, requiring only X86Base or ArmBase; no x86-64 level includes it. @@ -21,6 +23,7 @@ Pinned runtime sources, measured x64/ARM64 versions, and migration guidance: docs/RUNTIME-PROFILES.md https://github.com/dotnet/runtime/blob/v10.0.13/src/coreclr/vm/codeman.cpp https://github.com/dotnet/runtime/blob/v10.0.13/src/coreclr/tools/Common/JitInterface/ThunkGenerator/InstructionSetDesc.txt +https://github.com/dotnet/runtime/blob/v11.0.0/src/native/minipal/cpufeatures.c `FeatureProfile.valid_iff_prerequisites` spells out every operation prerequisite `Valid` implies. -/ @@ -116,8 +119,9 @@ def FeatureProfile.Valid (p : FeatureProfile) : Prop := (p.avx512Vbmi = true → p.avx512F = true) ∧ -- AES is a separate group on each architecture, but some hosts and switches disable it. (p.aes = true → p.x86Base64 = true) ∧ (p.armAes = true → p.armBase64 = true) ∧ - -- .NET 10 CoreCLR runtime groups; see docs/RUNTIME-PROFILES.md. - p.sse2 = p.x86Base64 ∧ p.ssse3 = p.sse41 ∧ p.sse41 = p.sse42 ∧ + -- The .NET 11 X86Base group: a process has all of SSE2 through SSE4.2 or none of them. + p.sse2 = p.x86Base64 ∧ p.ssse3 = p.x86Base64 ∧ p.sse41 = p.x86Base64 ∧ p.sse42 = p.x86Base64 ∧ + -- CoreCLR runtime groups; see docs/RUNTIME-PROFILES.md. p.avx2 = p.bmi1 ∧ p.avx2 = p.bmi2 ∧ p.avx512F = p.avx512FVL ∧ p.avx512F = p.avx512DQ ∧ p.avx512F = p.avx512DQVL ∧ p.advSimd = p.armBase64 ∧ p.vector128Accelerated = (p.sse2 || p.advSimd) ∧ @@ -153,8 +157,8 @@ theorem FeatureProfile.valid_iff_prerequisites (p : FeatureProfile) : p.Valid (p.armBase64 = true → p.architecture = .arm64) ∧ (p.advSimd = true → p.armBase64 = true) ∧ (p.armAes = true → p.armBase64 = true) ∧ - -- .NET 10 CoreCLR runtime groups; see docs/RUNTIME-PROFILES.md. - p.sse2 = p.x86Base64 ∧ p.ssse3 = p.sse41 ∧ p.sse41 = p.sse42 ∧ + -- The .NET 11 X86Base group and the other CoreCLR runtime groups. + p.sse2 = p.x86Base64 ∧ p.ssse3 = p.x86Base64 ∧ p.sse41 = p.x86Base64 ∧ p.sse42 = p.x86Base64 ∧ p.avx2 = p.bmi1 ∧ p.avx2 = p.bmi2 ∧ p.avx512F = p.avx512FVL ∧ p.avx512F = p.avx512DQ ∧ p.avx512F = p.avx512DQVL ∧ p.advSimd = p.armBase64 ∧ p.vector128Accelerated = (p.sse2 || p.advSimd) ∧ diff --git a/CIL/Identity.lean b/CIL/Identity.lean new file mode 100644 index 000000000..d23373efa --- /dev/null +++ b/CIL/Identity.lean @@ -0,0 +1,1192 @@ +import CIL.LiteralDisplay + +/-! +# Typed identities + +Values that name metadata entities: a metadata name (`MetadataName`), a user string literal +(`UserString`), an assembly (`AssemblyName` with its `Version`), a named type (`TypeIdentity`), a +primitive type (`PrimitiveType`) and what metadata says a named type is (`TypeKind`). Each is a +function of the entity it names and holds no row number, heap offset or per-extraction counter. + +A name or literal is one natural number: its UTF-8 bytes or UTF-16 code units read as a +big-endian number after a leading 1, so the number fixes the length and every digit. Equality +of every identity is the derived `DecidableEq`, which the kernel decides with `Nat.beq` on codes +and constructor matches. Code comparison reads numeric limbs rather than reducing a digit list; +runtime comparison still depends on the number of limbs. + +Text appears only in the `name!` and `str!` elaborators, which turn a literal into a code when +the term is elaborated, and in the display functions and delaborators, which turn a value back +into text for a reader. Printed text determines the value: where text would be ambiguous, the +value prints as its structure. `type!"..."` is printed for types whose components are all +literals and refused as input, so no text is parsed back into an identity. +-/ + +namespace CIL + +/-! ## Codes of digit lists -/ + +/-- The number of values one UTF-8 byte can take. -/ +def byteBase : Nat := 256 + +/-- The number of values one UTF-16 code unit can take. -/ +def codeUnitBase : Nat := 65536 + +/-- Read `digits` in `base` into `code`, most significant first; reject digits not below `base`. + Each step scales a growing code. -/ +def radixCode? (base : Nat) : List Nat → Nat → Option Nat + | [], code => some code + | digit :: rest, code => + if digit < base then radixCode? base rest (code * base + digit) else none + +/-- The digits of `code` in `base` below its leading digit 1, followed by `found`; `none` when + the leading digit is not 1 or `fuel` runs out. A code of `n` digits after the leading 1 + takes `n + 1` steps, and the code itself always suffices as fuel. -/ +def radixDigits? (base : Nat) : Nat → Nat → List Nat → Option (List Nat) + | 0, _, _ => none + | fuel + 1, code, found => + if code == 1 then some found + else if code < base then none + else radixDigits? base fuel (code / base) (code % base :: found) + +/-- A code read from a leading 1 is at least that start plus the number of digits read. -/ +theorem radixCode?_bound {base : Nat} (wide : 2 ≤ base) (digits : List Nat) : + ∀ (start code : Nat), 1 ≤ start → radixCode? base digits start = some code → + start + digits.length ≤ code := by + induction digits with + | nil => + intro start code _ read + simp only [radixCode?, Option.some.injEq] at read + subst read + simp + | cons digit rest ih => + intro start code positive read + simp only [radixCode?] at read + split at read + · have scaled : start * 2 ≤ start * base := Nat.mul_le_mul_left start wide + have restBound := ih (start * base + digit) code (by omega) read + simp only [List.length_cons] + omega + · simp at read + +#print axioms radixCode?_bound + +/-- Reading back one digit of a code whose leading part is at least 1 removes that digit. -/ +theorem radixDigits?_peel {base : Nat} (wide : 2 ≤ base) {start digit : Nat} + (positive : 1 ≤ start) (small : digit < base) (fuel : Nat) (found : List Nat) : + radixDigits? base (fuel + 1) (start * base + digit) found = + radixDigits? base fuel start (digit :: found) := by + have scaled : 1 * base ≤ start * base := Nat.mul_le_mul_right base positive + have notOne : start * base + digit ≠ 1 := by omega + have notSmall : ¬ start * base + digit < base := by omega + have quotient : (start * base + digit) / base = start := by + rw [Nat.add_comm, Nat.add_mul_div_right _ _ (by omega), Nat.div_eq_of_lt small, + Nat.zero_add] + have remainder : (start * base + digit) % base = digit := by + rw [Nat.add_comm, Nat.add_mul_mod_self_right, Nat.mod_eq_of_lt small] + simp [radixDigits?, notOne, notSmall, quotient, remainder] + +#print axioms radixDigits?_peel + +/-- Reading the digits of a code back, one step per digit, ends at the start of the code. -/ +theorem radixDigits?_radixCode? {base : Nat} (wide : 2 ≤ base) (digits : List Nat) : + ∀ (start code fuel : Nat) (found : List Nat), 1 ≤ start → + radixCode? base digits start = some code → + radixDigits? base (fuel + digits.length) code found = + radixDigits? base fuel start (digits ++ found) := by + induction digits with + | nil => intro start code fuel found _ read; simp [radixCode?] at read; simp [read] + | cons digit rest ih => + intro start code fuel found positive read + simp only [radixCode?] at read + split at read + next small => + have scaled : start * 2 ≤ start * base := Nat.mul_le_mul_left start wide + have step := ih (start * base + digit) code (fuel + 1) found (by omega) read + rw [List.length_cons, show fuel + (rest.length + 1) = fuel + 1 + rest.length by omega, + step, radixDigits?_peel wide positive small] + rfl + next => simp at read + +#print axioms radixDigits?_radixCode? + +/-- The digits of a code read from a leading 1 come back from the code alone. -/ +theorem radixDigits?_of_radixCode? {base : Nat} (wide : 2 ≤ base) {digits : List Nat} + {code : Nat} (read : radixCode? base digits 1 = some code) : + radixDigits? base code code [] = some digits := by + have bound := radixCode?_bound wide digits 1 code (Nat.le_refl 1) read + have back := radixDigits?_radixCode? wide digits 1 code (code - digits.length) [] + (Nat.le_refl 1) read + rw [Nat.sub_add_cancel (by omega), List.append_nil] at back + rw [back, show code - digits.length = code - digits.length - 1 + 1 by omega] + simp [radixDigits?] + +#print axioms radixDigits?_of_radixCode? + +/-- Two digit lists read from a leading 1 into the same code are equal. -/ +theorem radixCode?_injective {base : Nat} (wide : 2 ≤ base) {left right : List Nat} + {code : Nat} (fromLeft : radixCode? base left 1 = some code) + (fromRight : radixCode? base right 1 = some code) : left = right := + Option.some.inj ((radixDigits?_of_radixCode? wide fromLeft).symm.trans + (radixDigits?_of_radixCode? wide fromRight)) + +#print axioms radixCode?_injective + +/-- Reading digits after a list of digits continues from the code the list reached. -/ +theorem radixCode?_append (base : Nat) (front back : List Nat) : + ∀ start, radixCode? base (front ++ back) start = + (radixCode? base front start).bind (radixCode? base back) := by + induction front with + | nil => intro start; rfl + | cons digit rest ih => + intro start + simp only [List.cons_append, radixCode?] + split + · exact ih _ + · rfl + +#print axioms radixCode?_append + +/-- Digits read back from a code, followed by `found`, are digits that build the code from a + leading 1, followed by `found`. -/ +theorem radixCode?_of_radixDigits? {base : Nat} (wide : 2 ≤ base) : + ∀ (fuel code : Nat) (found digits : List Nat), + radixDigits? base fuel code found = some digits → + ∃ front, digits = front ++ found ∧ radixCode? base front 1 = some code := by + intro fuel + induction fuel with + | zero => intro code found digits read; simp [radixDigits?] at read + | succ fuel ih => + intro code found digits read + simp only [radixDigits?] at read + split at read + next isOne => + simp only [Option.some.injEq] at read + exact ⟨[], by simp [read], by simp [radixCode?, (beq_iff_eq.mp isOne)]⟩ + next => + split at read + next => simp at read + next => + obtain ⟨front, split, upper⟩ := ih (code / base) (code % base :: found) digits read + refine ⟨front ++ [code % base], by simp [split], ?_⟩ + have lowDigit : code % base < base := Nat.mod_lt _ (by omega) + rw [radixCode?_append, upper] + simp [radixCode?, lowDigit, Nat.div_add_mod'] + +#print axioms radixCode?_of_radixDigits? + +/-- The digits read back from a code build that code from a leading 1. -/ +theorem radixCode?_of_radixDigits?_self {base : Nat} (wide : 2 ≤ base) {code : Nat} + {digits : List Nat} (read : radixDigits? base code code [] = some digits) : + radixCode? base digits 1 = some code := by + obtain ⟨front, split, built⟩ := radixCode?_of_radixDigits? wide code code [] digits read + simpa [split] using built + +#print axioms radixCode?_of_radixDigits?_self + +/-- Exactly `count` base-2^bits digits when the code fits, most significant first, then `tail`. + Balanced splits keep bit operations within O(payload bits * log count). -/ +def fixedDigits (bits count code : Nat) (tail : List Nat) : List Nat := + match count with + | 0 => tail + | 1 => code :: tail + | count + 2 => + let lowCount := (count + 2) / 2 + let highCount := count + 2 - lowCount + let lowBits := bits * lowCount + let upper := code >>> lowBits + fixedDigits bits highCount upper + (fixedDigits bits lowCount (code - (upper <<< lowBits)) tail) +termination_by count +decreasing_by all_goals omega + +/-- The digit count is independent of the code and includes every leading zero. -/ +theorem fixedDigits_length (bits count code : Nat) (tail : List Nat) : + (fixedDigits bits count code tail).length = count + tail.length := by + induction count using Nat.strongRecOn generalizing code tail with + | ind count ih => + match count with + | 0 => simp [fixedDigits] + | 1 => simp [fixedDigits, Nat.add_comm] + | count + 2 => + rw [fixedDigits, ih _ (by omega), ih _ (by omega)] + omega + +#print axioms fixedDigits_length + +/-- Reading bounded fixed-width digits continues any initial code with their exact value. -/ +theorem fixedDigits_spec (bits count code : Nat) (bounded : code < (2^bits)^count) + (tail : List Nat) (start : Nat) : + radixCode? (2^bits) (fixedDigits bits count code tail) start = + radixCode? (2^bits) tail (start * (2^bits)^count + code) := by + induction count using Nat.strongRecOn generalizing code tail start with + | ind count ih => + match count with + | 0 => simp_all [fixedDigits] + | 1 => simp_all [fixedDigits, radixCode?] + | count + 2 => + let low := (count + 2) / 2 + let high := count + 2 - low + have splitCount : high + low = count + 2 := by dsimp [high, low]; omega + have lowSmall : low < count + 2 := by dsimp [low]; omega + have highSmall : high < count + 2 := by dsimp [high, low]; omega + have power : 2^(bits*low) = (2^bits)^low := by rw [Nat.pow_mul] + have positive : 0 < (2^bits)^low := Nat.pow_pos (Nat.pow_pos (by decide)) + have upper : code / (2^bits)^low < (2^bits)^high := by + apply (Nat.div_lt_iff_lt_mul positive).mpr + rw [← Nat.pow_add, splitCount] + exact bounded + have lower : code % (2^bits)^low < (2^bits)^low := Nat.mod_lt _ positive + rw [fixedDigits] + change radixCode? (2^bits) + (fixedDigits bits high (code >>> (bits*low)) + (fixedDigits bits low (code - ((code >>> (bits*low)) <<< (bits*low))) tail)) + start = _ + simp only [Nat.shiftRight_eq_div_pow, Nat.shiftLeft_eq] + rw [← Nat.mod_eq_sub_div_mul, power] + rw [ih high highSmall _ upper, ih low lowSmall _ lower] + congr 1 + rw [Nat.add_mul, Nat.mul_assoc, ← Nat.pow_add, splitCount] + have joined := Nat.div_add_mod code ((2^bits)^low) + rw [Nat.mul_comm] at joined + omega + + +#print axioms fixedDigits_spec + +/-- Every digit accepted by the radix reader is below its base. -/ +theorem radixCode?_digits {base : Nat} {digits : List Nat} {start code : Nat} + (read : radixCode? base digits start = some code) : + ∀ digit ∈ digits, digit < base := by + induction digits generalizing start with + | nil => simp + | cons head rest ih => + simp only [radixCode?] at read + split at read + next small => + intro digit member + rcases List.mem_cons.mp member with same | member + · simpa [same] using small + · exact ih read digit member + next => simp at read + +#print axioms radixCode?_digits + +/-- Every digit of a bounded code fits the requested digit width, including zero-width digits. -/ +theorem fixedDigits_bound (bits count code : Nat) (bounded : code < (2^bits)^count) : + ∀ digit ∈ fixedDigits bits count code [], digit < 2^bits := by + apply radixCode?_digits (start := 0) (code := code) + simpa [radixCode?] using fixedDigits_spec bits count code bounded [] 0 + +#print axioms fixedDigits_bound + +/-- The value of a bounded array range, with O(n log n) bit work and logarithmic recursion. -/ +def encodeRange (bits : Nat) (digits : Array Nat) (offset count : Nat) : Nat := + match count with + | 0 => 0 + | 1 => digits[offset]! + | count + 2 => + let lowCount := (count + 2) / 2 + let highCount := count + 2 - lowCount + (encodeRange bits digits offset highCount <<< (bits * lowCount)) + + encodeRange bits digits (offset + highCount) lowCount +termination_by count +decreasing_by all_goals omega + +/-- Reading an array range appends its exact value to any initial code. -/ +theorem encodeRange_spec (bits : Nat) (digits : Array Nat) (offset count : Nat) + (within : offset + count ≤ digits.size) + (bounded : ∀ digit ∈ digits.toList, digit < 2^bits) (start : Nat) : + radixCode? (2^bits) ((digits.toList.drop offset).take count) start = + some (start * (2^bits)^count + encodeRange bits digits offset count) := by + induction count using Nat.strongRecOn generalizing offset start with + | ind count ih => + match count with + | 0 => simp [encodeRange, radixCode?] + | 1 => + have index : offset < digits.size := by omega + have small := bounded digits[offset] (Array.getElem_mem_toList index) + rw [List.drop_eq_getElem_cons (by simpa using index)] + simp [encodeRange, radixCode?, small, getElem!_pos, index] + | count + 2 => + let low := (count + 2) / 2 + let high := count + 2 - low + have splitCount : high + low = count + 2 := by dsimp [high, low]; omega + have lowSmall : low < count + 2 := by dsimp [low]; omega + have highSmall : high < count + 2 := by dsimp [high, low]; omega + have splitList : (digits.toList.drop offset).take (count + 2) = + (digits.toList.drop offset).take high ++ + (digits.toList.drop (offset + high)).take low := by + have splitTake (entries : List Nat) (first second : Nat) : + entries.take (first + second) = + entries.take first ++ (entries.drop first).take second := by + induction first generalizing entries with + | zero => simp only [Nat.zero_add, List.take_zero, List.drop_zero, List.nil_append] + | succ first ih => + cases entries with + | nil => simp only [List.take_nil, List.drop_nil, List.nil_append] + | cons head rest => + simp only [Nat.succ_add, List.take_succ_cons, List.drop_succ_cons, + List.cons_append, ih] + rw [← splitCount, splitTake, List.drop_drop] + rw [splitList, radixCode?_append, ih high highSmall offset (by omega) start] + simp only [Option.bind_some] + rw [ih low lowSmall (offset + high) (by omega)] + simp only [encodeRange, Nat.shiftLeft_eq, Nat.pow_mul] + change some ((start * (2^bits)^high + encodeRange bits digits offset high) * + (2^bits)^low + encodeRange bits digits (offset + high) low) = _ + rw [Nat.add_mul, Nat.mul_assoc, ← Nat.pow_add, splitCount] + simp only [Nat.add_assoc] + rfl + +#print axioms encodeRange_spec + +/-- A leading one followed by bounded fixed-width digits, read one digit at a time, so the kernel + reduces it in steps linear in the digits. Compiled code runs `balancedBitCode?`. -/ +def bitCode? (bits : Nat) (digits : List Nat) : Option Nat := + radixCode? (2^bits) digits 1 + +/-- The code `bitCode?` gives, from one array conversion and a balanced split whose bit work grows + as n log n. The kernel holds an array as a list and reads entry i in i steps, so only compiled + code runs this form. -/ +def balancedBitCode? (bits : Nat) (digits : List Nat) : Option Nat := + if digits.all (· < 2^bits) then + let entries := digits.toArray + some ((1 <<< (bits * entries.size)) + encodeRange bits entries 0 entries.size) + else none + +/-- The balanced encoder has exactly the acceptance and value of the digit-by-digit reading, so + compiled code may run it in place of `bitCode?`. -/ +@[csimp] theorem bitCode?_eq_balancedBitCode? : @bitCode? = @balancedBitCode? := by + funext bits digits + unfold bitCode? balancedBitCode? + split + next valid => + have bounded : ∀ digit ∈ digits, digit < 2^bits := by simpa using valid + have spec := encodeRange_spec bits digits.toArray 0 digits.length + (by simp) (by simpa using bounded) 1 + simpa [Nat.shiftLeft_eq, Nat.pow_mul] using spec + next invalid => + cases read : radixCode? (2^bits) digits 1 with + | none => rfl + | some code => + have bounded := radixCode?_digits read + have valid : digits.all (· < 2^bits) = true := by simpa using bounded + exact False.elim (invalid valid) + +#print axioms bitCode?_eq_balancedBitCode? + +/-- `bitCode?` reads its digits as `radixCode?` does from a leading one. -/ +theorem bitCode?_eq_radixCode? (bits : Nat) (digits : List Nat) : + bitCode? bits digits = radixCode? (2^bits) digits 1 := rfl + +#print axioms bitCode?_eq_radixCode? + +/-- Reading bounded digits preserves the interval fixed by the initial code and digit count. -/ +theorem radixCode?_interval {base : Nat} (digits : List Nat) (start code : Nat) + (read : radixCode? base digits start = some code) : + start * base^digits.length ≤ code ∧ code < (start + 1) * base^digits.length := by + induction digits generalizing start with + | nil => simp [radixCode?] at read; subst code; simp + | cons digit rest ih => + simp only [radixCode?] at read + split at read + next small => + obtain ⟨lower, upper⟩ := ih (start * base + digit) read + have lowerScale := Nat.mul_le_mul_right (base^rest.length) + (Nat.le_add_right (start * base) digit) + have upperScale := Nat.mul_le_mul_right (base^rest.length) + (show start * base + digit + 1 ≤ (start + 1) * base by + rw [Nat.add_mul, Nat.one_mul]; omega) + simp only [List.length_cons, Nat.pow_succ', ← Nat.mul_assoc] + exact ⟨Nat.le_trans lowerScale lower, Nat.lt_of_lt_of_le upper upperScale⟩ + next => simp at read + +#print axioms radixCode?_interval + +/-- Fixed-width payload digits exist exactly when the highest set bit is a digit boundary. -/ +def bitDigits? (bits code : Nat) : Option (List Nat) := + if code = 0 then none + else + let top := code.log2 + if top % bits = 0 then + some (fixedDigits bits (top / bits) (code - (1 <<< top)) []) + else none + +/-- Every accepted digit list encodes the exact positive input code. -/ +theorem bitDigits?_sound (bits code : Nat) (_width : 0 < bits) (digits : List Nat) + (read : bitDigits? bits code = some digits) : + radixCode? (2^bits) digits 1 = some code := by + unfold bitDigits? at read + split at read + next zero => simp at read + next positive => + dsimp only at read + split at read + next aligned => + simp only [Option.some.injEq] at read + subst digits + have count : bits * (code.log2 / bits) = code.log2 := + Nat.mul_div_cancel' (Nat.dvd_of_mod_eq_zero aligned) + have power : (2^bits)^(code.log2 / bits) = 2^code.log2 := by + rw [← Nat.pow_mul, count] + have lower := Nat.log2_self_le positive + have upper : code < 2^code.log2 * 2 := by + simpa [Nat.pow_succ] using (Nat.lt_log2_self (n := code)) + have quotient : code / 2^code.log2 = 1 := by + apply Nat.div_eq_of_lt_le (by simpa using lower) + simpa [Nat.mul_comm] using upper + have remainder : code - 2^code.log2 = code % 2^code.log2 := by + rw [Nat.mod_eq_sub_div_mul, quotient, Nat.one_mul] + simp only [Nat.shiftLeft_eq, Nat.one_mul, remainder] + rw [fixedDigits_spec bits (code.log2 / bits) _ + (by rw [power]; exact Nat.mod_lt _ (Nat.pow_pos (by decide)))] + simp only [radixCode?, Nat.one_mul, power] + have joined := Nat.div_add_mod code (2^code.log2) + rw [quotient, Nat.mul_one] at joined + rw [joined] + next => simp at read + +#print axioms bitDigits?_sound + +/-- Every code built from a leading one is accepted and yields every original payload digit. -/ +theorem bitDigits?_complete (bits : Nat) (width : 0 < bits) (digits : List Nat) (code : Nat) + (read : radixCode? (2^bits) digits 1 = some code) : + bitDigits? bits code = some digits := by + obtain ⟨lower, upper⟩ := radixCode?_interval digits 1 code read + simp only [Nat.one_mul, ← Nat.pow_mul] at lower upper + have positive : code ≠ 0 := by + have := Nat.pow_pos (a := 2) (n := bits * digits.length) (by decide) + omega + have top : code.log2 = bits * digits.length := by + apply (Nat.log2_eq_iff positive).mpr + constructor + · exact lower + · simpa [Nat.pow_succ, Nat.mul_comm] using upper + have accepted : ∃ found, bitDigits? bits code = some found := by + simp [bitDigits?, positive, top] + obtain ⟨found, foundRead⟩ := accepted + have same := radixCode?_injective + (base := 2^bits) (by exact Nat.le_self_pow (by omega) 2) + (bitDigits?_sound bits code width found foundRead) read + simpa [same] using foundRead + +#print axioms bitDigits?_complete + +/-- A fixed-width digit suffix follows the same digits with an empty suffix. -/ +theorem fixedDigits_tail (bits count code : Nat) (tail : List Nat) : + fixedDigits bits count code tail = fixedDigits bits count code [] ++ tail := by + induction count using Nat.strongRecOn generalizing code tail with + | ind count ih => + match count with + | 0 => simp [fixedDigits] + | 1 => simp [fixedDigits] + | count + 2 => + rw [fixedDigits, ih (count + 2 - (count + 2) / 2) (by omega), + ih ((count + 2) / 2) (by omega)] + conv => rhs; rw [fixedDigits, ih _ (by omega)] + rw [List.append_assoc] + +#print axioms fixedDigits_tail + +/-- Removing the least significant fixed-width digit preserves every higher digit. -/ +theorem fixedDigits_peel (bits count code : Nat) (width : 0 < bits) + (bounded : code < (2^bits)^(count + 1)) : + fixedDigits bits (count + 1) code [] = + fixedDigits bits count (code / 2^bits) [] ++ [code % 2^bits] := by + have positive : 0 < 2^bits := Nat.pow_pos (by decide) + have upper : code / 2^bits < (2^bits)^count := by + apply (Nat.div_lt_iff_lt_mul positive).mpr + simpa [Nat.pow_succ] using bounded + have left := fixedDigits_spec bits (count + 1) code bounded [] 1 + have right := fixedDigits_spec bits count (code / 2^bits) upper [code % 2^bits] 1 + rw [fixedDigits_tail] at right + simp only [radixCode?, Nat.mod_lt _ positive, ↓reduceIte, Nat.one_mul] at left right + have joined := Nat.div_add_mod code (2^bits) + rw [Nat.mul_comm] at joined + simp only [Nat.add_mul, ← Nat.pow_succ, Nat.add_assoc, joined] at right + exact radixCode?_injective (Nat.le_self_pow (by omega) 2) left right + +#print axioms fixedDigits_peel + +/-! ## Names and literals -/ + +/-- One metadata name, such as `Int32` or `System.Collections`: its UTF-8 bytes read as a + big-endian base-256 number after a leading 1, so the value fixes the length and every + byte. A code that is zero or whose leading digit is not 1 names nothing. -/ +structure MetadataName where + /-- The bytes of the name after a leading 1, as one number. -/ + code : Nat + deriving DecidableEq, Ord + +/-- The number of values one 64-bit limb of a code takes. -/ +def hashLimbBase : Nat := 2 ^ 64 + +/-- Mathematical specification of least-significant-first hashing, with no zero padding. -/ +def mixCodeLimbsSpec (seed : UInt64) (code : Nat) : UInt64 := + if code = 0 then seed + else mixCodeLimbsSpec (mixHash seed (UInt64.ofNat code)) (code / hashLimbBase) +termination_by code +decreasing_by exact Nat.div_lt_self (by omega) (by unfold hashLimbBase; decide) + +/-- `seed` mixed with every 64-bit limb, least significant first; O(n log n) bit work. -/ +def mixCodeLimbs (seed : UInt64) (code : Nat) : UInt64 := + if code = 0 then seed + else + let limbs := fixedDigits 64 (code.log2 / 64 + 1) code [] + limbs.reverse.foldl (fun hash limb => mixHash hash (UInt64.ofNat limb)) seed + +/-- A fixed-width limb list with no zero padding hashes exactly the specified limb sequence. -/ +theorem fixedDigits_hash (count code : Nat) (seed : UInt64) + (bounded : code < hashLimbBase^count) + (minimal : count = 0 ∨ hashLimbBase^(count - 1) ≤ code) : + (fixedDigits 64 count code []).reverse.foldl + (fun hash limb => mixHash hash (UInt64.ofNat limb)) seed = mixCodeLimbsSpec seed code := by + induction count generalizing code seed with + | zero => + have zero : code = 0 := by simpa using bounded + simp [zero, fixedDigits, mixCodeLimbsSpec] + | succ count ih => + have lower : hashLimbBase^count ≤ code := by + rcases minimal with impossible | lower + · omega + · simpa using lower + have positive : 0 < hashLimbBase := by unfold hashLimbBase; decide + have nonzero : code ≠ 0 := by + have := Nat.pow_pos (n := count) positive + omega + rw [fixedDigits_peel 64 count code (by decide) bounded, List.reverse_append] + simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append, + List.foldl_cons] + have remainder : UInt64.ofNat (code % 2^64) = UInt64.ofNat code := by + exact UInt64.ofNat_mod_size + rw [remainder, ih] + · conv => rhs; rw [mixCodeLimbsSpec] + simp only [nonzero, ↓reduceIte] + rfl + · apply (Nat.div_lt_iff_lt_mul positive).mpr + simpa [hashLimbBase, Nat.pow_succ] using bounded + · cases count with + | zero => exact Or.inl rfl + | succ count => + apply Or.inr + apply (Nat.le_div_iff_mul_le positive).mpr + simpa [hashLimbBase, Nat.pow_succ] using lower + +#print axioms fixedDigits_hash + +/-- Balanced extraction preserves the seed, every limb, and the exact hash mixing order. -/ +theorem mixCodeLimbs_eq (seed : UInt64) (code : Nat) : + mixCodeLimbs seed code = mixCodeLimbsSpec seed code := by + unfold mixCodeLimbs + split + next zero => simp [mixCodeLimbsSpec, zero] + next positive => + apply fixedDigits_hash + · unfold hashLimbBase + rw [← Nat.pow_mul] + apply Nat.lt_of_lt_of_le Nat.lt_log2_self + apply Nat.pow_le_pow_right (by decide) + omega + · apply Or.inr + simp only [Nat.add_sub_cancel, hashLimbBase, ← Nat.pow_mul] + apply Nat.le_trans _ (Nat.log2_self_le positive) + apply Nat.pow_le_pow_right (by decide) + exact Nat.mul_div_le _ _ + +#print axioms mixCodeLimbs_eq + +instance : Hashable MetadataName := ⟨fun name => mixCodeLimbs 0 name.code⟩ + +/-- The name whose UTF-8 bytes are `bytes`, or `none` when an entry is not a byte. -/ +def MetadataName.ofBytes? (bytes : List Nat) : Option MetadataName := + (bitCode? 8 bytes).map MetadataName.mk + +/-- The UTF-8 bytes a name holds, or `none` when its code is zero or its leading base-256 digit + is not 1. -/ +def MetadataName.bytes? (name : MetadataName) : Option (List Nat) := + bitDigits? 8 name.code + +/-- The bytes a name was built from come back from the name. -/ +theorem MetadataName.bytes?_ofBytes? {bytes : List Nat} {name : MetadataName} + (encoded : MetadataName.ofBytes? bytes = some name) : name.bytes? = some bytes := by + obtain ⟨code, read, rfl⟩ := Option.map_eq_some_iff.mp encoded + rw [bitCode?_eq_radixCode?] at read + exact bitDigits?_complete 8 (by decide) bytes code read + +#print axioms MetadataName.bytes?_ofBytes? + +/-- A name whose bytes read back is the name of those bytes, so a name is malformed exactly + when no byte list gives it. -/ +theorem MetadataName.ofBytes?_bytes? {bytes : List Nat} {name : MetadataName} + (decoded : name.bytes? = some bytes) : MetadataName.ofBytes? bytes = some name := by + simp [ofBytes?, bitCode?_eq_radixCode?, + bitDigits?_sound 8 name.code (by decide) bytes decoded] + +#print axioms MetadataName.ofBytes?_bytes? + +/-- Two byte lists that give the same name are equal. -/ +theorem MetadataName.ofBytes?_injective {left right : List Nat} {name : MetadataName} + (fromLeft : MetadataName.ofBytes? left = some name) + (fromRight : MetadataName.ofBytes? right = some name) : left = right := by + exact Option.some.inj ((bytes?_ofBytes? fromLeft).symm.trans (bytes?_ofBytes? fromRight)) + +#print axioms MetadataName.ofBytes?_injective + +/-- Whether `name` is the metadata name of `bytes`, decided by reading the name back, which costs + time linear in the bytes in the kernel. -/ +def MetadataName.spells (name : MetadataName) (bytes : List Nat) : Bool := + name.bytes? == some bytes + +/-- A name spells exactly the bytes it is the metadata name of. -/ +theorem MetadataName.spells_iff {name : MetadataName} {bytes : List Nat} : + name.spells bytes = true ↔ MetadataName.ofBytes? bytes = some name := by + unfold MetadataName.spells + constructor + · intro spelled + exact MetadataName.ofBytes?_bytes? (by simpa using spelled) + · intro encoded + simpa using MetadataName.bytes?_ofBytes? encoded + +#print axioms MetadataName.spells_iff + +/-- A `#US` literal: its UTF-16 code units read as a big-endian base-65536 number after a + leading 1. -/ +structure UserString where + /-- The code units of the literal after a leading 1, as one number. -/ + code : Nat + deriving DecidableEq, Ord + +instance : Hashable UserString := ⟨fun literal => mixCodeLimbs 0 literal.code⟩ + +/-- The literal whose UTF-16 code units are `units`, or `none` when an entry is not a code + unit. -/ +def UserString.ofUnits? (units : List Nat) : Option UserString := + (bitCode? 16 units).map UserString.mk + +/-- The UTF-16 code units a literal holds, or `none` when its code is zero or its leading + base-65536 digit is not 1. -/ +def UserString.units? (literal : UserString) : Option (List Nat) := + bitDigits? 16 literal.code + +/-- The code units a literal was built from come back from the literal. -/ +theorem UserString.units?_ofUnits? {units : List Nat} {literal : UserString} + (encoded : UserString.ofUnits? units = some literal) : literal.units? = some units := by + obtain ⟨code, read, rfl⟩ := Option.map_eq_some_iff.mp encoded + rw [bitCode?_eq_radixCode?] at read + exact bitDigits?_complete 16 (by decide) units code read + +#print axioms UserString.units?_ofUnits? + +/-- A literal whose code units read back is the literal of those units, so a literal is + malformed exactly when no unit list gives it. -/ +theorem UserString.ofUnits?_units? {units : List Nat} {literal : UserString} + (decoded : literal.units? = some units) : UserString.ofUnits? units = some literal := by + simp [ofUnits?, bitCode?_eq_radixCode?, + bitDigits?_sound 16 literal.code (by decide) units decoded] + +#print axioms UserString.ofUnits?_units? + +/-- Two code unit lists that give the same literal are equal. -/ +theorem UserString.ofUnits?_injective {left right : List Nat} {literal : UserString} + (fromLeft : UserString.ofUnits? left = some literal) + (fromRight : UserString.ofUnits? right = some literal) : left = right := by + exact Option.some.inj ((units?_ofUnits? fromLeft).symm.trans (units?_ofUnits? fromRight)) + +#print axioms UserString.ofUnits?_injective + +/-! ## Assemblies, types and kinds -/ + +/-- The four version numbers of an `Assembly` or `AssemblyRef` row. -/ +structure Version where + major : Nat + minor : Nat + build : Nat + revision : Nat + deriving DecidableEq, Hashable, Ord, Repr + +/-- An assembly by its name, version, culture and public key token. Name, version and culture + are columns of its `Assembly` or `AssemblyRef` row; the culture is the empty name when + neutral. An `Assembly` row holds the public key, whose token is the last eight bytes of its + SHA-1 digest in reverse order; an `AssemblyRef` row holds the key or the token, as its + `PublicKey` flag (0x0001) says. `publicKeyToken` is the number whose big-endian base-256 + digits are the eight token bytes in the order an `AssemblyRef` row stores them and display + text writes them, and `none` for an assembly without a key. -/ +structure AssemblyName where + name : MetadataName + version : Version + culture : MetadataName + /-- The strong-name token as a big-endian number, or `none` for a null token. -/ + publicKeyToken : Option Nat + deriving DecidableEq, Hashable + +section AssemblyOrder + +attribute [local instance] lexOrd + +/-- Raw identity components in lexicographic comparison order. -/ +private def AssemblyName.orderKey (assembly : AssemblyName) : + Nat × Nat × Nat × Nat × Nat × Nat × Option Nat := + (assembly.name.code, assembly.version.major, assembly.version.minor, + assembly.version.build, assembly.version.revision, assembly.culture.code, + assembly.publicKeyToken) + +/-- Assembly order compares raw name codes, version components, culture and optional token. -/ +instance : Ord AssemblyName where + compare := compareOn AssemblyName.orderKey + +/-- Assembly comparison is transitive and reverses when its arguments are exchanged. -/ +instance : Std.TransOrd AssemblyName := + inferInstanceAs (Std.TransCmp (compareOn AssemblyName.orderKey)) + +/-- Assembly comparison identifies exactly equal raw identity components. -/ +instance : Std.LawfulEqOrd AssemblyName where + compare_self := Std.ReflCmp.compare_self (cmp := compareOn AssemblyName.orderKey) + eq_of_compare {left right} equal := by + have keys : left.orderKey = right.orderKey := Std.LawfulEqOrd.eq_of_compare equal + cases left with | mk leftName leftVersion leftCulture leftToken => + cases right with | mk rightName rightVersion rightCulture rightToken => + cases leftName; cases rightName; cases leftVersion; cases rightVersion + cases leftCulture; cases rightCulture + simpa only [AssemblyName.orderKey, Prod.mk.injEq, AssemblyName.mk.injEq, + MetadataName.mk.injEq, Version.mk.injEq, and_assoc] using keys + +/-- Equal assembly comparison results preserve every raw identity component. -/ +theorem AssemblyName.compare_eq_iff (left right : AssemblyName) : + compare left right = .eq ↔ left = right := + Std.LawfulEqOrd.compare_eq_iff_eq + +/-- info: 'CIL.AssemblyName.compare_eq_iff' depends on axioms: +[propext, Classical.choice, Quot.sound] -/ +#guard_msgs in +#print axioms AssemblyName.compare_eq_iff + +end AssemblyOrder + +/-- One row of a type's nesting chain: its namespace column and its name column. -/ +structure TypeLevel where + typeNamespace : MetadataName + name : MetadataName + deriving DecidableEq, Hashable + +/-- A type by the assembly that defines it, its namespace and name columns, and the namespace + and name columns of each enclosing type from the outermost. The assembly is the pinned + assembly that holds the `TypeDef` row, reached by following `ExportedType` forwarders, so + every reference to one type gives one value; producing it from a reference is the binding's + job. Every column is a separate component, so no two distinct types share a value. -/ +structure TypeIdentity where + assembly : AssemblyName + typeNamespace : MetadataName + enclosing : List TypeLevel := [] + name : MetadataName + deriving DecidableEq, Hashable + +/-- The value types ECMA-335 encodes by element type (II.23.1.16), without an assembly. -/ +inductive PrimitiveType where + | boolean | char | sbyte | byte | int16 | uint16 | int32 | uint32 | int64 | uint64 + | single | double | intPtr | uintPtr + deriving DecidableEq, Hashable, Repr + +/-- The element type code of a primitive (0x02 to 0x0D, 0x18, 0x19). -/ +def PrimitiveType.elementType : PrimitiveType → Nat + | .boolean => 0x02 | .char => 0x03 | .sbyte => 0x04 | .byte => 0x05 | .int16 => 0x06 + | .uint16 => 0x07 | .int32 => 0x08 | .uint32 => 0x09 | .int64 => 0x0A | .uint64 => 0x0B + | .single => 0x0C | .double => 0x0D | .intPtr => 0x18 | .uintPtr => 0x19 + +/-- What metadata says a named type is. -/ +inductive TypeKind where + | valueType + | enumType (underlying : PrimitiveType) + | classType + | interfaceType + deriving DecidableEq, Hashable, Repr + +/-! ## Display -/ + +/-- The first UTF-16 code unit of a surrogate pair. -/ +def highSurrogateStart : Nat := 0xD800 + +/-- The second UTF-16 code unit of a surrogate pair. -/ +def lowSurrogateStart : Nat := 0xDC00 + +/-- The first UTF-16 code unit after the surrogates. -/ +def surrogateEnd : Nat := 0xE000 + +/-- The first code point that UTF-16 writes as a surrogate pair. -/ +def supplementaryStart : Nat := 0x10000 + +/-- The number of values each half of a surrogate pair carries. -/ +def surrogateSpan : Nat := 0x400 + +/-- The UTF-16 code units of `text`. -/ +def utf16Units (text : String) : List Nat := + text.toList.flatMap fun char => + if char.toNat < supplementaryStart then [char.toNat] + else + let offset := char.toNat - supplementaryStart + [highSurrogateStart + offset / surrogateSpan, lowSurrogateStart + offset % surrogateSpan] + +/-- The characters that UTF-16 `units` encode, or `none` at an unpaired surrogate. -/ +def utf16Chars? : List Nat → Option (List Char) + | [] => some [] + | [unit] => if unit < highSurrogateStart || surrogateEnd ≤ unit then some [Char.ofNat unit] + else none + | unit :: next :: rest => + if unit < highSurrogateStart || surrogateEnd ≤ unit then + (Char.ofNat unit :: ·) <$> utf16Chars? (next :: rest) + else if unit < lowSurrogateStart && lowSurrogateStart ≤ next && next < surrogateEnd then + let point := supplementaryStart + (unit - highSurrogateStart) * surrogateSpan + + (next - lowSurrogateStart) + (Char.ofNat point :: ·) <$> utf16Chars? rest + else none + +/-- Inclusive code point ranges of the characters in Unicode categories Cc, Cf, Zl and Zp and + of the non-ASCII characters in Zs, as the .NET 10 runtime classifies them. -/ +def invisibleRanges : List (Nat × Nat) := + [(0x0000, 0x001F), (0x007F, 0x00A0), (0x00AD, 0x00AD), (0x0600, 0x0605), (0x061C, 0x061C), + (0x06DD, 0x06DD), (0x070F, 0x070F), (0x0890, 0x0891), (0x08E2, 0x08E2), (0x1680, 0x1680), + (0x180E, 0x180E), (0x2000, 0x200F), (0x2028, 0x202F), (0x205F, 0x2064), (0x2066, 0x206F), + (0x3000, 0x3000), (0xFEFF, 0xFEFF), (0xFFF9, 0xFFFB), (0x110BD, 0x110BD), + (0x110CD, 0x110CD), (0x13430, 0x1343F), (0x1BCA0, 0x1BCA3), (0x1D173, 0x1D17A), + (0xE0001, 0xE0001), (0xE0020, 0xE007F)] + +/-- Whether a character is a control, format, separator or non-ASCII space character, which + shows as nothing or as a space, or reorders the text around it. -/ +def isInvisible (char : Char) : Bool := + invisibleRanges.any fun (low, high) => low ≤ char.toNat && char.toNat ≤ high + +/-- The number of hexadecimal digits of a `\uHHHH` escape. -/ +def unicodeEscapeDigits : Nat := 4 + +/-- The escape `\uHHHH` of a character below U+10000. -/ +def unicodeEscape (char : Char) : String := + let digits := upperHexDigits char.toNat + "\\u" ++ String.ofList (List.replicate (unicodeEscapeDigits - digits.length) '0') ++ digits + +/-- `text` with `\` written `\\`, `"` written `\"` when `quoted`, and each invisible character + written `\uHHHH`; `none` when an invisible character lies above U+FFFF, which a Lean string + literal cannot escape. Every `\` of the result starts an escape, so it determines `text`. -/ +def escapedText? (quoted : Bool) (text : String) : Option String := + if text.any fun char => supplementaryStart ≤ char.toNat && isInvisible char then none + else some (String.join (text.toList.map fun char => + if char == '\\' then "\\\\" + else if quoted && char == '"' then "\\\"" + else if isInvisible char then unicodeEscape char + else char.toString)) + +/-- `text` as `escapedText?` writes it without quotes, with a leading `{` written `\{` so that + no text reads as the raw structure of a code. -/ +def readableText? (text : String) : Option String := do + let escaped ← escapedText? false text + return if text.startsWith "{" then "\\" ++ escaped else escaped + +/-- A Lean string literal that elaborates to `text`, with invisible characters escaped. -/ +def quotedText? (text : String) : Option String := + (fun escaped => "\"" ++ escaped ++ "\"") <$> escapedText? true text + +/-- The hexadecimal digit count obtained from the highest set bit contains the complete code. -/ +theorem hexCode_bound (code : Nat) : code < (2 ^ 4) ^ (code.log2 / 4 + 1) := by + rw [← Nat.pow_mul] + apply Nat.lt_of_lt_of_le Nat.lt_log2_self + apply Nat.pow_le_pow_right (by decide) + omega + +#print axioms hexCode_bound + +/-- The digits used by raw hexadecimal display retain the exact natural code. -/ +theorem fixedDigits_hex_exact (code : Nat) : + radixCode? 16 (fixedDigits 4 (code.log2 / 4 + 1) code []) 0 = some code := by + simpa [radixCode?] using + fixedDigits_spec 4 (code.log2 / 4 + 1) code (hexCode_bound code) [] 0 + +#print axioms fixedDigits_hex_exact + +/-- Minimal lowercase hexadecimal digits of a natural code, including one digit for zero. -/ +def rawCodeHex (code : Nat) : String := + let count := code.log2 / 4 + 1 + let digits := fixedDigits 4 count code [] + String.ofList (digits.map Nat.digitChar) + +/-- The raw structure of a code, for a value that no text shows faithfully. Its leading `{` + sets it apart from every `readableText?`. -/ +def rawCodeDisplay (code : Nat) : String := + "{ code := 0x" ++ rawCodeHex code ++ " }" + +/-- The text of a name, or `none` when its code is malformed or its bytes are not UTF-8. -/ +def MetadataName.text? (name : MetadataName) : Option String := do + let bytes ← name.bytes? + String.fromUTF8? ⟨(bytes.map Nat.toUInt8).toArray⟩ + +/-- The text of a name as `readableText?` writes it, or `none` when there is none. -/ +def MetadataName.readable? (name : MetadataName) : Option String := + name.text? >>= readableText? + +/-- The text of a name as `readableText?` writes it, or the raw structure of its code; distinct + names never display alike. -/ +def MetadataName.display (name : MetadataName) : String := + match name.readable? with + | some text => text + | none => rawCodeDisplay name.code + +/-- The `name!` literal of a name, or `none` when it has no text, holds a NUL, which no + `#Strings` entry can, or holds an invisible character above U+FFFF. -/ +def MetadataName.literal? (name : MetadataName) : Option String := do + let text ← name.text? + guard (!text.contains '\x00') + quotedText? text + +/-- The text of a literal, or `none` when its code is malformed or holds an unpaired + surrogate. -/ +def UserString.text? (literal : UserString) : Option String := do + let units ← literal.units? + String.ofList <$> utf16Chars? units + +/-- The text of a literal as `readableText?` writes it, or the raw structure of its code; + distinct literals never display alike. -/ +def UserString.display (literal : UserString) : String := + match literal.text? >>= readableText? with + | some text => text + | none => rawCodeDisplay literal.code + +/-- The `str!` literal of a literal, or `none` when it has no text or holds an invisible + character above U+FFFF. -/ +def UserString.literal? (literal : UserString) : Option String := + literal.text? >>= quotedText? + +instance : Repr MetadataName where + reprPrec name _ := match name.literal? with + | some quoted => "name!" ++ quoted + | none => rawCodeDisplay name.code + +instance : Repr UserString where + reprPrec literal _ := match literal.literal? with + | some quoted => "str!" ++ quoted + | none => rawCodeDisplay literal.code + +deriving instance Repr for AssemblyName +deriving instance Repr for TypeLevel +deriving instance Repr for TypeIdentity + +/-- Dotted version numbers, such as `11.0.0.0`. -/ +def Version.display (version : Version) : String := + s!"{version.major}.{version.minor}.{version.build}.{version.revision}" + +/-- The number of hexadecimal digits of a public key token. -/ +def publicKeyTokenDigits : Nat := 16 + +/-- A public key token as sixteen lowercase hexadecimal digits, or `null`. -/ +def publicKeyTokenText : Option Nat → String + | none => "null" + | some token => + let digits := Nat.toDigits 16 token + String.ofList (List.replicate (publicKeyTokenDigits - digits.length) '0' ++ digits) + +/-- The readable name of an assembly and its readable culture, `neutral` for the empty culture, + when both have text free of `,` and `]` and the culture is not spelled `neutral`. Text built + from these parts then splits back at its separators into exactly this assembly. -/ +def AssemblyName.parts? (assembly : AssemblyName) : Option (String × String) := do + let name ← assembly.name.readable? + let culture ← assembly.culture.readable? + guard ([name, culture].all fun text => !text.contains ',' && !text.contains ']') + guard (culture != "neutral") + return (name, if culture.isEmpty then "neutral" else culture) + +/-- The display name of an assembly, such as + `System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a`, when + `AssemblyName.parts?` reads it. -/ +def AssemblyName.text? (assembly : AssemblyName) : Option String := do + let (name, culture) ← assembly.parts? + return s!"{name}, Version={assembly.version.display}, Culture={culture}, " ++ + s!"PublicKeyToken={publicKeyTokenText assembly.publicKeyToken}" + +/-- The display name of an assembly, or its `Repr` structure when no display name determines + it; distinct assemblies never display alike. -/ +def AssemblyName.display (assembly : AssemblyName) : String := + assembly.text?.getD (reprStr assembly) + +/-- An assembly as the scope of a type: `[Name,Version]` when its culture is neutral and its + token null, otherwise `[Name,Version,Culture,Token]`; see `AssemblyName.parts?`. -/ +def AssemblyName.scopeText? (assembly : AssemblyName) : Option String := do + let (name, culture) ← assembly.parts? + let version := assembly.version.display + if assembly.culture.code == 1 && assembly.publicKeyToken.isNone then + return s!"[{name},{version}]" + return s!"[{name},{version},{culture},{publicKeyTokenText assembly.publicKeyToken}]" + +/-- One level of a nesting chain as `Namespace.Name` of readable texts, or `Name` in the empty + namespace, when both parts have text, the namespace holds no `/` and the name no `.` or `/`. + The level then splits back at its last `.`. -/ +def TypeLevel.text? (level : TypeLevel) : Option String := do + let typeNamespace ← level.typeNamespace.readable? + let name ← level.name.readable? + guard (!typeNamespace.contains '/' && !name.contains '.' && !name.contains '/') + return if typeNamespace.isEmpty then name else typeNamespace ++ "." ++ name + +/-- The levels of a type from the outermost, its own level last. -/ +def TypeIdentity.levels (type : TypeIdentity) : List TypeLevel := + type.enclosing ++ [⟨type.typeNamespace, type.name⟩] + +/-- A type in ILAsm form with its full assembly scope, `[Name,Version]Namespace.Outer/Name`, + when its scope and every level read as text; such text determines the type. -/ +def TypeIdentity.text? (type : TypeIdentity) : Option String := do + let scope ← type.assembly.scopeText? + let levels ← type.levels.mapM TypeLevel.text? + return scope ++ "/".intercalate levels + +/-- A type as `TypeIdentity.text?` writes it, such as `[Examples,1.0.0.0]Examples.Point`, or + its `Repr` structure; distinct types never display alike. -/ +def TypeIdentity.display (type : TypeIdentity) : String := + type.text?.getD (reprStr type) + +/-- The full name Cecil and method signatures give a primitive, such as `System.Int32`. -/ +def PrimitiveType.display : PrimitiveType → String + | .boolean => "System.Boolean" | .char => "System.Char" | .sbyte => "System.SByte" + | .byte => "System.Byte" | .int16 => "System.Int16" | .uint16 => "System.UInt16" + | .int32 => "System.Int32" | .uint32 => "System.UInt32" | .int64 => "System.Int64" + | .uint64 => "System.UInt64" | .single => "System.Single" | .double => "System.Double" + | .intPtr => "System.IntPtr" | .uintPtr => "System.UIntPtr" + +/-! ## Notation -/ + +section Notation + +open Lean Elab Term Meta PrettyPrinter Delaborator SubExpr + +/-- `name!"Int32"` is the `MetadataName` of the UTF-8 bytes of the literal, computed when the + term is elaborated. -/ +syntax:max (name := metadataNameLiteral) "name!" str : term + +/-- `str!"text"` is the `UserString` of the UTF-16 code units of the literal, computed when the + term is elaborated. -/ +syntax:max (name := userStringLiteral) "str!" str : term + +/-- Display-only notation for a closed `TypeIdentity`; refused as input, so that no text is + parsed into a type. -/ +syntax:max (name := typeIdentityDisplay) "type!" str : term + +/-- Elaborates `name!"..."` to `MetadataName.mk` of the literal's code; refuses a NUL, since a + `#Strings` entry ends at its first NUL and no metadata name holds one. -/ +@[term_elab metadataNameLiteral] def elabMetadataName : TermElab := fun syntaxTree _ => do + let some text := syntaxTree[1].isStrLit? | throwUnsupportedSyntax + if text.contains '\x00' then throwError "name!: a metadata name cannot hold a NUL character" + let some name := MetadataName.ofBytes? (text.toUTF8.toList.map UInt8.toNat) + | throwError "name!: a UTF-8 byte is out of range" + return mkApp (mkConst ``MetadataName.mk) (mkNatLit name.code) + +/-- Elaborates `str!"..."` to `UserString.mk` of the literal's code. -/ +@[term_elab userStringLiteral] def elabUserString : TermElab := fun syntaxTree _ => do + let some text := syntaxTree[1].isStrLit? | throwUnsupportedSyntax + let some literal := UserString.ofUnits? (utf16Units text) + | throwError "str!: a UTF-16 code unit is out of range" + return mkApp (mkConst ``UserString.mk) (mkNatLit literal.code) + +/-- Refuses `type!"..."` as input. -/ +@[term_elab typeIdentityDisplay] def elabTypeIdentityDisplay : TermElab := fun _ _ => + throwError "type!\"...\" is display only; write the TypeIdentity structure" + +/-- How many constants the type delaborator unfolds to reach one component's literal. -/ +def identityUnfoldLimit : Nat := 8 + +/-- `term` with at most `identityUnfoldLimit` constants at its head unfolded. -/ +def unfoldIdentityConstants (term : Expr) : MetaM Expr := do + let mut current := term.consumeMData + for _ in [0:identityUnfoldLimit] do + unless current.isConst do break + let some unfolded ← unfoldDefinition? current | break + current := unfolded.consumeMData + return current + +/-- The name a term spells as `MetadataName.mk` of a literal, through named constants. -/ +def metadataNameLiteral? (term : Expr) : MetaM (Option MetadataName) := do + let term ← unfoldIdentityConstants term + unless term.isAppOfArity ``MetadataName.mk 1 do return none + return MetadataName.mk <$> natLiteral? term.appArg! + +/-- The level a term spells as `TypeLevel.mk` of two name literals, through named constants. -/ +def typeLevelLiteral? (term : Expr) : MetaM (Option TypeLevel) := do + let term ← unfoldIdentityConstants term + let #[typeNamespace, name] := term.getAppArgs | return none + unless term.isAppOf ``TypeLevel.mk do return none + let some typeNamespace ← metadataNameLiteral? typeNamespace | return none + let some name ← metadataNameLiteral? name | return none + return some ⟨typeNamespace, name⟩ + +/-- The levels a list literal of `TypeLevel.mk` literals spells. -/ +partial def typeLevelList? (term : Expr) : MetaM (Option (List TypeLevel)) := do + let term ← unfoldIdentityConstants term + if term.isAppOfArity ``List.nil 1 then return some [] + unless term.isAppOfArity ``List.cons 3 do return none + let some head ← typeLevelLiteral? (term.getArg! 1) | return none + let some tail ← typeLevelList? (term.getArg! 2) | return none + return some (head :: tail) + +/-- The version a term spells as `Version.mk` of four literals, through named constants. -/ +def versionLiteral? (term : Expr) : MetaM (Option Version) := do + let term ← unfoldIdentityConstants term + let #[major, minor, build, revision] := term.getAppArgs | return none + unless term.isAppOf ``Version.mk do return none + return return ⟨← natLiteral? major, ← natLiteral? minor, ← natLiteral? build, + ← natLiteral? revision⟩ + +/-- The token a term spells as `none` or `some` of a literal, through named constants. -/ +def publicKeyTokenLiteral? (term : Expr) : MetaM (Option (Option Nat)) := do + let term ← unfoldIdentityConstants term + if term.isAppOfArity ``Option.none 1 then return some none + unless term.isAppOfArity ``Option.some 2 do return none + return some <$> natLiteral? term.appArg! + +/-- The assembly a term spells as `AssemblyName.mk` of literals, through named constants. -/ +def assemblyNameLiteral? (term : Expr) : MetaM (Option AssemblyName) := do + let term ← unfoldIdentityConstants term + let #[name, version, culture, token] := term.getAppArgs | return none + unless term.isAppOf ``AssemblyName.mk do return none + let some name ← metadataNameLiteral? name | return none + let some version ← versionLiteral? version | return none + let some culture ← metadataNameLiteral? culture | return none + let some token ← publicKeyTokenLiteral? token | return none + return some ⟨name, version, culture, token⟩ + +/-- The type a `TypeIdentity.mk` term spells when every component is a literal, reading + literals and unfolding named constants but evaluating nothing. Linear in the term's size. -/ +def typeIdentityLiteral? (term : Expr) : MetaM (Option TypeIdentity) := do + let #[assembly, typeNamespace, enclosing, name] := term.getAppArgs | return none + let some assembly ← assemblyNameLiteral? assembly | return none + let some typeNamespace ← metadataNameLiteral? typeNamespace | return none + let some enclosing ← typeLevelList? enclosing | return none + let some name ← metadataNameLiteral? name | return none + return some ⟨assembly, typeNamespace, enclosing, name⟩ + +/-- Prints a name as its `MetadataName.literal?`, which elaborates back to it; any other name + keeps its raw structure. -/ +@[delab app.CIL.MetadataName.mk] def delabMetadataName : Delab := do + if ← getPPOption getPPAll then failure + let #[codeTerm] := (← getExpr).getAppArgs | failure + let some code := natLiteral? codeTerm | failure + if let some quoted := (MetadataName.mk code).literal? then + `(name! $(⟨Syntax.mkLit strLitKind quoted⟩)) + else + let field ← `(Parser.Term.structInstLVal| $(mkIdent `code):ident) + `({ $field := $(Syntax.mkNumLit ("0x" ++ rawCodeHex code)) }) + +/-- Prints a literal as its `UserString.literal?`, which elaborates back to it; any other + literal keeps its raw structure. -/ +@[delab app.CIL.UserString.mk] def delabUserString : Delab := do + if ← getPPOption getPPAll then failure + let #[codeTerm] := (← getExpr).getAppArgs | failure + let some code := natLiteral? codeTerm | failure + if let some quoted := (UserString.mk code).literal? then + `(str! $(⟨Syntax.mkLit strLitKind quoted⟩)) + else + let field ← `(Parser.Term.structInstLVal| $(mkIdent `code):ident) + `({ $field := $(Syntax.mkNumLit ("0x" ++ rawCodeHex code)) }) + +/-- Prints a type whose components are all literals as `type!"..."` when `TypeIdentity.text?` + reads it, so the printed text determines the type; any other type keeps its structure. -/ +@[delab app.CIL.TypeIdentity.mk] def delabTypeIdentity : Delab := do + if ← getPPOption getPPAll then failure + let some type ← typeIdentityLiteral? (← getExpr) | failure + let some text := type.text? | failure + `(type! $(Syntax.mkStrLit text)) + +end Notation + +end CIL diff --git a/CIL/ImportMetadata.lean b/CIL/ImportMetadata.lean index 4e5e70a24..783574de7 100644 --- a/CIL/ImportMetadata.lean +++ b/CIL/ImportMetadata.lean @@ -29,7 +29,7 @@ structure ImportLayoutCatalog where computed : Array ComputedLayout := #[] deriving DecidableEq, Repr -/-- Whether a type is one of the .NET 10 `System.Runtime` integer primitives (including +/-- Whether a type is one of the .NET 11 `System.Runtime` integer primitives (including `bool`, `char` and the native integers). -/ def importPrimitive : TypeExpr → Bool | .atom scope name => @@ -60,12 +60,11 @@ def ImportSignature.supported (signature : ImportSignature) (layouts : List RawL def ImportLayoutCatalog.Valid (catalog : ImportLayoutCatalog) (architecture : Architecture) : Prop := catalog.published = catalog.actual ∧ LayoutsValid catalog.actual catalog.computed architecture ∧ - (catalog.actual.map RawLayout.identity).Nodup ∧ catalog.actual.all (fun layout => !layout.identity.isEmpty) = true instance (catalog : ImportLayoutCatalog) (architecture : Architecture) : Decidable (catalog.Valid architecture) := - inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _)) + inferInstanceAs (Decidable (_ ∧ _ ∧ _)) theorem ImportLayoutCatalog.Valid.metadata {catalog : ImportLayoutCatalog} {architecture : Architecture} (valid : catalog.Valid architecture) : diff --git a/CIL/InstructionEquations.lean b/CIL/InstructionEquations.lean index 97390a3a6..dacdd15f0 100644 --- a/CIL/InstructionEquations.lean +++ b/CIL/InstructionEquations.lean @@ -1,6 +1,7 @@ import Lean import CIL.Instructions import CIL.SymbolicExecution +import CIL.Extraction.LinkIndex /-! # Instruction equations @@ -12,7 +13,7 @@ after each generated template, and a hand-written body calls it itself, so `cil_ from an extracted body and from a parametric one, such as `def body (sse42 avx2 : Bool) (callee : Nat) : CIL.Method`, by one rewrite instead of unfolding the body and walking its code list. An instruction that depends on a parameter is stated as -written, for example `if avx2 then op else .unsupported ...`, and `cil_lookup` decides it from +written, for example `if avx2 then op else .unadmitted opcode`, and `cil_lookup` decides it from the caller's facts or literal flags. The proofs follow `CIL.CodeSuffix`: the command names the suffix of the code at every @@ -21,6 +22,12 @@ suffix before it, and proves each equation from the suffix that starts its block walks more than one block, so the equations of a body cost time linear in its length. The kernel checks every declaration, so the command proves nothing about the body beyond what its definition says. + +The command also states `body_link{pc} : (body xs).linkAt pc = some link`, tagged +`@[cil_code ↓]`, at every call site; a call reads its target from `linkAt`, so stepping one +uses the named fact. For ascending literal keys, the proof uses `body_links_ascend` and the +entry in the named suffix `body_linkSuffix{start}`. The command skips tables with symbolic +cells or keys. -/ namespace CIL.InstructionEquations @@ -31,22 +38,31 @@ open Lean Meta Elab Term Command body's code than this. -/ def suffixBlock : Nat := 16 -/-- The instructions of `code`, one per list cell and each as written in the body. Fails when - a cell cannot be reached by reduction, as when a parameter decides the code's length or - the body is irreducible or opaque. -/ -def instructions (body : Name) (code : Expr) : MetaM (Array Expr) := do - let mut cell := code +/-- The elements of `list`, one per cell and each as written, and whether the cells reached + end in `[]`. The walk stops early at a cell that does not reduce to `::` or `[]`, as + when a parameter decides the list's length or the list is irreducible or opaque. -/ +def listElements (list : Expr) : MetaM (Array Expr × Bool) := do + let mut cell := list let mut found := #[] repeat match (← whnf cell).getAppFnArgs with - | (``List.cons, #[_, op, tail]) => - found := found.push (← instantiateMVars op).headBeta + | (``List.cons, #[_, element, tail]) => + found := found.push (← instantiateMVars element).headBeta cell := tail - | (``List.nil, _) => break - | _ => throwError "cil_instruction_equations: the instruction at pc {found.size} of \ - {body} is not determined by its parameters, or the body does not unfold; state each \ - feature-dependent instruction as an `if` on the instruction, padding the shorter side \ - with `.unsupported`, so every profile has the same pcs" + | (``List.nil, _) => return (found, true) + | _ => return (found, false) + return (found, false) + +/-- The instructions of `code`, one per list cell and each as written in the body. Fails when + a cell cannot be reached by reduction, as when a parameter decides the code's length or + the body is irreducible or opaque. -/ +def instructions (body : Name) (code : Expr) : MetaM (Array Expr) := do + let (found, complete) ← listElements code + unless complete do + throwError "cil_instruction_equations: the instruction at pc {found.size} of \ + {body} is not determined by its parameters, or the body does not unfold; state each \ + feature-dependent instruction as an `if` on the instruction, padding the shorter side \ + with `.unadmitted`, so every profile has the same pcs" return found /-- A body whose equations are being declared: its name, universe parameters, parameters and @@ -85,46 +101,141 @@ def Body.prove (body : Body) (tail doc : String) (statement proof : Expr) : Term type := ← mkForallFVars body.parameters statement value := ← mkLambdaFVars body.parameters proof } -/-- `[Op]`, the type of a code list. -/ -def codeType : Expr := mkApp (mkConst ``List [.zero]) (mkConst ``CIL.Op) +/-- The literal list of `elements` of type `elementType` followed by `rest`. -/ +def listLiteral (elementType : Expr) (elements : Array Expr) (rest : Expr) : Expr := + elements.foldr (init := rest) (mkApp3 (mkConst ``List.cons [.zero]) elementType) + +/-- The positions at which `declareSuffixes` names a suffix of a list of `length` elements. -/ +def suffixStarts (length : Nat) : List Nat := + (List.range ((length + suffixBlock - 1) / suffixBlock)).map (· * suffixBlock) -/-- `Eq.refl list` for a code list. -/ -def codeRefl (list : Expr) : Expr := mkApp2 (mkConst ``Eq.refl [.one]) codeType list +/-- The suffix of a list of `elements` from `position` on as a literal: the rest of its block, + then the next named suffix `body{stem}{start}` or `[]`. -/ +def suffixFrom (body : Body) (stem : String) (elementType : Expr) (elements : Array Expr) + (position : Nat) : Expr := + let stop := min (position - position % suffixBlock + suffixBlock) elements.size + let rest := if stop < elements.size then body.constant s!"{stem}{stop}" else + mkApp (mkConst ``List.nil [.zero]) elementType + listLiteral elementType (elements.extract position stop) rest -/-- Declares `body_suffix{start}`, the code of `body` from each block boundary on, and - `body_suffix{start}_placed : (body xs).code.drop start = body_suffix{start} xs`, each - proved from the suffix one block earlier. -/ -def declareSuffixes (body : Body) (code : Expr) (ops : Array Expr) : TermElabM Unit := do - let suffix (start : Nat) := body.constant s!"_suffix{start}" - let starts := (List.range ((ops.size + suffixBlock - 1) / suffixBlock)).map (· * suffixBlock) +/-- A proof of `list[position]? = some element` from the named suffix that starts the + position's block; it walks at most one block. -/ +def foundAt (body : Body) (stem : String) (elementType list : Expr) (elements : Array Expr) + (position : Nat) : MetaM Expr := do + let offset := position % suffixBlock + let start := position - offset + let element := elements[position]! + return mkApp8 (mkConst ``CIL.CodeSuffix.lookup) elementType list + (body.constant s!"{stem}{start}") (mkNatLit start) element (mkNatLit offset) + (body.constant s!"{stem}{start}_placed") (← mkEqRefl (← mkAppM ``Option.some #[element])) + +/-- Declares `body{stem}{start}`, the elements of `list` from each block boundary on, and + `body{stem}{start}_placed : list.drop start = body{stem}{start} xs`, each proved from the + suffix one block earlier; `describe start` names what the suffix at `start` holds. -/ +def declareSuffixes (body : Body) (elementType : Expr) (stem : String) + (describe : Nat → String) (list : Expr) (elements : Array Expr) : TermElabM Unit := do + let suffix (start : Nat) := body.constant s!"{stem}{start}" + let listType := mkApp (mkConst ``List [.zero]) elementType + let refl (value : Expr) := mkApp2 (mkConst ``Eq.refl [.one]) listType value + let starts := suffixStarts elements.size -- Each suffix is its block followed by the next suffix, so the last one is declared first. for start in starts.reverse do - let stop := min (start + suffixBlock) ops.size - let rest := if stop < ops.size then suffix stop else - mkApp (mkConst ``List.nil [.zero]) (mkConst ``CIL.Op) - let value ← mkLambdaFVars body.parameters <| (ops.extract start stop).foldr (init := rest) - (mkApp3 (mkConst ``List.cons [.zero]) (mkConst ``CIL.Op)) - let name := body.name.appendAfter s!"_suffix{start}" - body.declare name s!"The code of `{body.name}` from pc {start} on." <| .defnDecl { + let value ← mkLambdaFVars body.parameters (suffixFrom body stem elementType elements start) + let name := body.name.appendAfter s!"{stem}{start}" + body.declare name s!"{describe start}." <| .defnDecl { name, levelParams := body.levelParams, value, safety := .safe - type := ← mkForallFVars body.parameters codeType + type := ← mkForallFVars body.parameters listType hints := .regular (getMaxHeight (← getEnv) value + 1) } -- Proofs alone read the suffixes, so they have no compiled code. modifyEnv (addNoncomputable · name) for start in starts do let previous := start - suffixBlock - let dropped := mkApp3 (mkConst ``List.drop [.zero]) (mkConst ``CIL.Op) (mkNatLit start) code - body.prove s!"_suffix{start}_placed" - s!"The code of `{body.name}` from pc {start} on is `{body.name}_suffix{start}`." - (mkApp3 (mkConst ``Eq [.one]) codeType dropped (suffix start)) - (if start = 0 then codeRefl (suffix start) else - mkApp7 (mkConst ``CIL.CodeSuffix.advance) code (suffix previous) (suffix start) - (mkNatLit previous) (mkNatLit suffixBlock) - (body.constant s!"_suffix{previous}_placed") (codeRefl (suffix start))) + let dropped := mkApp3 (mkConst ``List.drop [.zero]) elementType (mkNatLit start) list + body.prove s!"{stem}{start}_placed" + s!"{describe start} is `{body.name}{stem}{start}`." + (mkApp3 (mkConst ``Eq [.one]) listType dropped (suffix start)) + (if start = 0 then refl (suffix start) else + mkApp8 (mkConst ``CIL.CodeSuffix.advance) elementType list (suffix previous) + (suffix start) (mkNatLit previous) (mkNatLit suffixBlock) + (body.constant s!"{stem}{previous}_placed") (refl (suffix start))) + +/-- The entries of an explicit link table, each as written with its literal key, when every + cell reduces and the keys ascend from 0; `none` otherwise. -/ +def explicitLinks (method : Expr) : MetaM (Option (Array Expr × Array (Nat × Expr))) := do + let (entries, complete) ← listElements (mkApp (mkConst ``CIL.Method.links) method) + unless complete do return none + let mut keyed := #[] + let mut lowest := 0 + for entry in entries do + let some (_, _, key, link) := (← whnfR entry).app4? ``Prod.mk | return none + let some pc ← (evalNat (← instantiateMVars key)).run | return none + unless lowest ≤ pc do return none + lowest := pc + 1 + keyed := keyed.push (pc, link) + return some (entries, keyed) + +/-- Declares `body_links_ascend`, the suffixes `body_linkSuffix{start}` of the table and, at each + entry, `body_link{pc} : ∀ xs, (body xs).linkAt pc = some link` from the entry's position. At + each `call` of `ops` the table gives no key it declares `body_link{pc} : ∀ xs, + (body xs).linkAt pc = none`, read from the suffix of the block where `pc` would sit, so the + kernel walks at most one block of the table. -/ +def declareExplicitLinks (body : Body) (simpSet : SimpExtension) (method : Expr) + (ops : Array Expr) (entries : Array Expr) (keyed : Array (Nat × Expr)) : + TermElabM Unit := do + let links := mkApp (mkConst ``CIL.Method.links) method + let entryType := mkApp2 (mkConst ``Prod [.zero, .zero]) (mkConst ``Nat) (mkConst ``CIL.Link) + declareSuffixes body entryType "_linkSuffix" + (fun start => s!"The link table of `{body.name}` from entry {start} on") links entries + let ascending ← mkEq (mkApp3 (mkConst ``CIL.Extraction.keysAscend) (mkConst ``CIL.Link) + (mkNatLit 0) links) (mkConst ``Bool.true) + body.prove "_links_ascend" s!"The keys of the link table of `{body.name}` ascend." + ascending (← mkEqRefl (mkConst ``Bool.true)) + for inRange : position in [0:keyed.size] do + let (pc, link) := keyed[position] + withCurrHeartbeats do + let statement ← mkEq (mkApp2 (mkConst ``CIL.Method.linkAt) method (mkNatLit pc)) + (← mkAppM ``Option.some #[link]) + let proof ← mkAppM ``CIL.Extraction.linkAt_of_position #[body.constant "_links_ascend", + ← foundAt body "_linkSuffix" entryType links entries position] + body.prove s!"_link{pc}" s!"The link of the call site at pc {pc} of `{body.name}`." + statement proof + addSimpTheorem simpSet (body.name.appendAfter s!"_link{pc}") (post := false) + (inv := false) .global (eval_prio default) + let mut position := 0 + for inRange : pc in [0:ops.size] do + if let some (key, _) := keyed[position]? then + if key == pc then + position := position + 1 + continue + unless (← whnfR ops[pc]).isAppOf ``CIL.Instruction.call do continue + withCurrHeartbeats do + let read := mkApp2 (mkConst ``CIL.Method.linkAt) method (mkNatLit pc) + let absent := mkApp (mkConst ``Option.none [.zero]) (mkConst ``CIL.Link) + -- A pc past the last key of a full last block starts that block, as no suffix follows it. + let block := position - position % suffixBlock + let start := if block < entries.size then block else block - suffixBlock + let ascending := body.constant "_links_ascend" + let readFrom ← if start = 0 then + mkAppM ``CIL.Extraction.linkAt_eq_readHead #[ascending, mkNatLit pc] + else + let previous := entries[start - 1]! + let below ← mkDecideProof (← mkAppM ``LT.lt + #[← mkAppM ``Prod.fst #[previous], mkNatLit pc]) + mkAppM ``CIL.Extraction.linkAt_eq_readHead_suffix #[ascending, + body.constant s!"_linkSuffix{start}_placed", + ← foundAt body "_linkSuffix" entryType links entries (start - 1), below] + let walked := (← inferType readFrom).eq?.map (·.2.2) |>.getD absent + let proof ← mkEqTrans readFrom + (← mkExpectedTypeHint (← mkEqRefl absent) (← mkEq walked absent)) + body.prove s!"_link{pc}" s!"The call at pc {pc} of `{body.name}` states no link." + (← mkEq read absent) proof + addSimpTheorem simpSet (body.name.appendAfter s!"_link{pc}") (post := false) + (inv := false) .global (eval_prio default) /-- `cil_instruction_equations body` declares `body_instruction{pc}`, the `@[cil_code ↓]` equation `∀ xs, (body xs).code[pc]? = some op`, for every pc of the method-valued - definition `body`, with the named suffixes `body_suffix{start}` its proofs go through. -/ + definition `body`, with the named suffixes `body_suffix{start}` its proofs go through, and + the link fact `body_link{pc}` at each call site its table gives a link. -/ syntax (name := instructionEquations) "cil_instruction_equations " ident : command elab_rules : command @@ -145,20 +256,19 @@ elab_rules : command let method := body.constant "" let code := mkApp (mkConst ``CIL.Method.code) method let ops ← instructions name code - declareSuffixes body code ops + declareSuffixes body (mkConst instructionTypeName) "_suffix" + (fun start => s!"The code of `{name}` from pc {start} on") code ops for inRange : pc in [0:ops.size] do - let index := pc % suffixBlock - let start := pc - index -- A window per equation, so a long body's statements do not exhaust the command's. withCurrHeartbeats do let found ← mkAppM ``Option.some #[ops[pc]] body.prove s!"_instruction{pc}" s!"The instruction at pc {pc} of `{name}`." (← mkEq (lookup.beta #[method, mkNatLit pc]) found) - (mkApp7 (mkConst ``CIL.CodeSuffix.lookup) code (body.constant s!"_suffix{start}") - (mkNatLit start) ops[pc] (mkNatLit index) - (body.constant s!"_suffix{start}_placed") (← mkEqRefl found)) + (← foundAt body "_suffix" (mkConst instructionTypeName) code ops pc) -- Pre-order, as `@[cil_code ↓]`: simp rewrites the lookup before unfolding the body. addSimpTheorem simpSet (name.appendAfter s!"_instruction{pc}") (post := false) (inv := false) .global (eval_prio default) + if let some (entries, keyed) ← explicitLinks method then + declareExplicitLinks body simpSet method ops entries keyed end CIL.InstructionEquations diff --git a/CIL/Instructions.lean b/CIL/Instructions.lean index 0521ff287..521fa1f71 100644 --- a/CIL/Instructions.lean +++ b/CIL/Instructions.lean @@ -1,19 +1,40 @@ import CIL.Types +import CIL.ExceptionType import CIL.IntegerArithmetic import CIL.Features import CIL.SIMD.Availability import CIL.VectorMemory import CIL.MethodPointers +import CIL.CallSite +import CIL.Extraction.Layout /-! # Instructions and methods -`Op` is the instruction set the extractor emits; `Method` is one extracted method body +`Instruction` is the instruction set the extractor emits; `Method` is one extracted method body and `Program` the list a call index refers into. -/ namespace CIL +/-- The admitted integer suffixes of `ldind.*`, `stind.*`, `ldelem.*` and `stelem.*`. -/ +inductive NumericKind where + | i1 | u1 | i2 | u2 | i4 | u4 | i8 | i + deriving DecidableEq, Repr + +/-- Bytes the access moves; native int is eight bytes on every admitted profile. -/ +def NumericKind.width : NumericKind → Nat + | .i1 | .u1 => 1 + | .i2 | .u2 => 2 + | .i4 | .u4 => 4 + | .i8 | .i => 8 + +/-- Whether a load sign-extends to the stack width: only the one- and two-byte signed kinds, + since a four-byte load gives the same i32 bits either way. -/ +def NumericKind.signed : NumericKind → Bool + | .i1 | .i2 => true + | _ => false + /-- Small integer storage types; all extend to the 32-bit evaluation-stack type. -/ inductive NarrowKind where | signed8 | unsigned8 | signed16 | unsigned16 @@ -82,120 +103,220 @@ inductive LocalKind where | byte | word32 | word64 | vector128 | vector256 | vector512 | reference deriving DecidableEq, Repr -/-- Constructors that initialize a typed span local without exposing its storage address. -/ +/-- The span constructor overloads, each building a checked view from its explicit arguments + without changing memory. -/ inductive SpanConstructor where - | empty (width : Nat) (writable : Bool) | array (element : ArrayElement) (writable : Bool) (range : ArraySpanRange) | reference (width : Nat) (writable : Bool) | singleReference (width : Nat) (writable : Bool) + /-- `ReadOnlySpan(void*, int)`, which must lie in extracted static data. -/ + | staticBytes deriving DecidableEq, Repr /-- The checked memory operation underlying a span constructor. -/ def SpanConstructor.operation : SpanConstructor → MemoryOp - | .empty width writable => .spanEmpty width writable | .array element writable range => .arraySpan element writable range | .reference width writable => .spanFromReference width writable | .singleReference width writable => .spanSingleReference width writable + | .staticBytes => .spanCreate + +/-- The element width and writability of every span the constructor builds. -/ +def SpanConstructor.shape : SpanConstructor → Nat × Bool + | .array element writable _ => (element.width, writable) + | .reference width writable | .singleReference width writable => (width, writable) + | .staticBytes => (1, false) + +/-- Target of an overflow-checked conversion; `i` and `u` are native-sized. -/ +inductive ConvTarget where + | i1 | u1 | i2 | u2 | i4 | u4 | i8 | u8 | i | u + deriving DecidableEq, Repr + +/-- Result width in bits; native int is 64 bits on every admitted profile. -/ +def ConvTarget.width : ConvTarget → Nat + | .i1 | .u1 => 8 + | .i2 | .u2 => 16 + | .i4 | .u4 => 32 + | .i8 | .u8 | .i | .u => 64 + +/-- Whether the target range is signed. -/ +def ConvTarget.signed : ConvTarget → Bool + | .i1 | .i2 | .i4 | .i8 | .i => true + | .u1 | .u2 | .u4 | .u8 | .u => false + +/-- The metadata item an `ldtoken` names. A field token's runtime handle is modelled by the + reference to the field's read-only storage, which `createSpan` consumes. -/ +inductive TokenOperand where + /-- A metadata type whose runtime handle `ldtoken` loads. -/ + | type (type : Extraction.TypeExpr) + | field (field : Extraction.FieldRef) + deriving DecidableEq, Repr + +/-- The prefixes of a `call`: `tail.` retires the caller's frame before the transfer, and + `constrained.` names the type on which the called member is resolved to an implementation. -/ +structure CallPrefixes where + tail : Bool := false + constrained : Option Extraction.TypeExpr := none + deriving DecidableEq, Repr /-- One instruction. Branch targets and call indices are positions in the method's code and in the `Program`. Constructors that `CIL.step` does not execute are handled by the checked interpreter in `CIL.Safety`. -/ -inductive Op where - /-- Widen the int32 operand of a metadata-validated mixed native operation. -/ - | nativeBinary (operation : Op) - | divide (signed remainder : Bool) - | checkedArithmetic (kind : CheckedBinaryKind) (signed : Bool) - | checkedConvert (width : Nat) (signed unsignedInput : Bool) - /-- Dynamic frame storage; the flag is checked against the raw method's localsinit bit. -/ - | stackAlloc (initLocals : Bool) - /-- Fresh zero-initialized primitive heap storage, assuming runtime allocation availability. -/ - | newArray (element : ArrayElement) - /-- `newarr` of class `element`: a fresh array of null references on the heap budget. -/ - | newReferenceArray (element : Extraction.TypeExpr) - | nop | neg | gt +inductive Instruction where + -- Division and remainder; `divideKind?` gives their flags. + | div | div_un | rem | rem_un + -- Overflow-checked arithmetic; `checkedKind?` gives their flags. + | add_ovf | add_ovf_un | sub_ovf | sub_ovf_un | mul_ovf | mul_ovf_un + -- Overflow-checked conversion to `target`, reading the operand as signed or as unsigned. + | conv_ovf (target : ConvTarget) | conv_ovf_un (target : ConvTarget) + /-- Fresh frame storage of the popped byte count, zeroed when `declared.initLocals` is set. -/ + | localloc + /-- `newarr` of the declared element type `element`: a fresh zeroed array whose storage the + method's layouts give that type (`CIL.Method.elementShape`). -/ + | newarr (element : Extraction.TypeExpr) + | nop | neg | cgt -- Signed and unsigned compare-and-branch; more follow `switch` below. | bgt (target : Nat) | ble (target : Nat) - | bleu (target : Nat) + | ble_un (target : Nat) -- Value-type aggregates and fields, by byte offset and width. - | loadAggregate (size : Nat) - | copyObject (size : Nat) - | storeAggregate (size : Nat) - | initAggregate (size : Nat) - | loadScalar (width : Nat) (signed : Bool := false) - | storeScalar (width : Nat) - | fieldAt (offset width : Nat) (signed : Bool := false) - | fieldAddress (offset : Nat) - | setFieldAt (offset width : Nat) - | aggregateFieldAt (offset size : Nat) - | setAggregateFieldAt (offset size : Nat) - -- Arguments, locals and their vector homes. - | arg (index : Nat) (narrow : Option NarrowKind := none) - | vectorArg (index : Nat) - | vectorArgAddr (index : Nat) - | local (index : Nat) (narrow : Option NarrowKind := none) - | vectorLocal (index : Nat) - | vectorLocalAddr (index : Nat) - | setVectorLocal (index : Nat) - | localAddr (index : Nat) - | setLocal (index : Nat) (narrow : Option NarrowKind := none) - | initializeSpanLocal (index : Nat) (constructor : SpanConstructor) - /-- Transfer to a managed method after retiring the current activation. -/ - | tailCall (callee argc : Nat) - /-- Authenticated managed pointers, immutable tables and exact-signature indirect calls. -/ - | managed (operation : ManagedOp) - -- 64-bit lanes of a 256-bit vector. - | lane (index : Fin 4) - | laneAddress (index : Fin 4) - | setLane (index : Fin 4) - -- Constants, conversions, arithmetic, 64-bit memory access and stack shuffling. - | const32 (word : W32) - | const64 (word : W64) - | convI1 | convI2 | convU2 - | convI8 | convI4 | convU1 | convU4 | convU8 | convU | bnot | add | sub | mul | band | bor | bxor - | shl | shr | shrUn | lt | ltu | gtu | eq | load64 | store64 | dup | pop + /-- Read the value of the addressed type. -/ + | ldobj (type : Extraction.TypeExpr) + /-- Store a value of the addressed type. -/ + | stobj (type : Extraction.TypeExpr) + /-- Zero the storage of the addressed type. -/ + | initobj (type : Extraction.TypeExpr) + /-- Push the storage width of the type in bytes. -/ + | sizeof (type : Extraction.TypeExpr) + /-- Copy the initialized bytes of the type from source to destination. -/ + | cpobj (type : Extraction.TypeExpr) + /-- `ldind` of `kind`: a load through a reference, or of the entry a native method table + cursor names. -/ + | ldind (kind : NumericKind) + /-- `stind` of `kind`: a store through a writable reference. -/ + | stind (kind : NumericKind) + /-- `ldlen`: the element count of an array or of a managed handler table, as a native int. -/ + | ldlen + /-- `ldelem` of `kind`: an element of a value array, or with `.i` the entry of a managed + handler table. -/ + | ldelem (kind : NumericKind) + /-- `ldelem.ref`: the reference an element slot of an array of class references holds. -/ + | ldelem_ref + /-- `stelem` of `kind`: a store into an element of a value array. -/ + | stelem (kind : NumericKind) + /-- `stelem.ref`: a store into an element slot of an array of class references, after the + covariance check. -/ + | stelem_ref + /-- `ldelem` of the declared element type `element`: an element of the storage the method's + layouts give that type (`CIL.Method.elementShape`), or the entry of a managed handler + table of that signature. -/ + | ldelem_any (element : Extraction.TypeExpr) + /-- `stelem` of the declared element type `element`: a store into an element of the storage + the method's layouts give that type. -/ + | stelem_any (element : Extraction.TypeExpr) + /-- `ldelema` of the declared element type `element`: the address of an element of the + storage the method's layouts give that type, read-only when `readOnly` records a + `readonly.` prefix. -/ + | ldelema (element : Extraction.TypeExpr) (readOnly : Bool) + -- Arguments and locals. + /-- Pushes argument `index`, read as `Method.argumentAccess` says. -/ + | ldarg (index : Nat) + /-- Stores the top of the stack into argument `index`'s home, as `storeArgument` says. -/ + | starg (index : Nat) + /-- Pushes the address of argument `index`'s home, as `addressArgument` says. -/ + | ldarga (index : Nat) + | ldloc (index : Nat) + | ldloca (index : Nat) + | stloc (index : Nat) + /-- Push the managed method pointer authenticated by the method token and closed signature. -/ + | ldftn (identity : ManagedIdentity) (signature : ManagedSignature) + /-- Call a managed function pointer with the standalone signature and optional tail prefix. -/ + | calli (signature : ManagedSignature) (tail : Bool) + -- Constants, conversions, arithmetic and stack shuffling. + | ldc_i4 (value : W32) + | ldc_i8 (value : W64) + /-- `ldnull`: the null object reference. -/ + | ldnull + /-- `ldstr` pushes the interned object bound to the decoded user-string operand. -/ + | ldstr (text : String) + /-- `cpblk` and `initblk` carry the alignment established by their preceding prefix. -/ + | cpblk (alignment : Nat) | initblk (alignment : Nat) + | conv_i1 | conv_i2 | conv_u2 + | conv_i8 | conv_i4 | conv_u1 | conv_u4 | conv_u8 | conv_u | conv_i | not | add | sub | mul + | and | or + | xor | shl | shr | shr_un | clt | clt_un | cgt_un | ceq | dup | pop -- Control flow. - | branch (target : Nat) + | br (target : Nat) | switch (targets : List Nat) - | brzero (target : Nat) - | brnonzero (target : Nat) - | bltu (target : Nat) - | bgeu (target : Nat) - | bgtu (target : Nat) + | brfalse (target : Nat) + | brtrue (target : Nat) + | blt_un (target : Nat) + | bge_un (target : Nat) + | bgt_un (target : Nat) | bge (target : Nat) | blt (target : Nat) | beq (target : Nat) - | bne (target : Nat) - -- Calls and constructors; `argc` values are popped as arguments. - | call (method : Nat) (argc : Nat) - /-- `callvirt` of a non-virtual instance method of a class: a null receiver, the first of the - `argc` arguments, throws `System.NullReferenceException` before the call. -/ - | callInstance (method : Nat) (argc : Nat) - /-- `callvirt` of a virtual method of a class: the receiver, the first of the `argc` arguments, - enters the body `targets` lists for its exact runtime class. A null receiver throws - `System.NullReferenceException`; a class `targets` omits is a model fault, not a call. -/ - | callVirtual (targets : List (Extraction.TypeExpr × Nat)) (argc : Nat) - | newVector256 (constructor : Nat) (argc : Nat) - | newAggregate (constructor : Nat) (argc : Nat) (size : Fin (2^64)) - /-- A fresh zeroed instance of class `owner` with `size` bytes of fields, passed to - `constructor` as its receiver ahead of `argc` arguments and pushed once it returns. -/ - | newObject (constructor : Nat) (argc : Nat) (owner : Extraction.TypeExpr) - (bases : List Extraction.TypeExpr) (size : Nat) - /-- `castclass` (`throwing`) or `isinst` of class `type`: an object whose runtime class or one - of its bases is `type` stays on the stack, as does null; any other object becomes null, - or throws `System.InvalidCastException` for `castclass`. -/ - | objectTest (type : Extraction.TypeExpr) (throwing : Bool) - | skipInit | asRef - /-- Pushes `1` when the method's feature profile has the feature, else `0`. -/ - | feature (id : Feature) - | intrinsic (operation : Intrinsic) (argc : Nat) - | memory (operation : MemoryOp) - | ret (narrow : Option NarrowKind := none) + | bne_un (target : Nat) + /-- `call` of `method`: it enters the body its pc links to with `method.argumentCount` + popped values as arguments. Under `tail.` those are the whole stack and the caller's + frame retires before the transfer. -/ + | call (method : Extraction.MethodRef) (prefixes : CallPrefixes := {}) + /-- A receiver call enters the direct body or dispatch table at its pc. Null receivers throw. -/ + | callvirt (method : Extraction.MethodRef) + /-- `newobj` of `constructor`: a fresh zeroed instance of its owner, passed to the constructor + the link at its pc names as its receiver ahead of the constructor's parameters, and pushed + once it returns. The link states what is allocated: a value-type temporary for + `.aggregate`, a class instance with its bases for `.object`. -/ + | newobj (constructor : Extraction.MethodRef) + /-- `castclass`: the object unchanged when it is null or an instance of `type`; + `InvalidCastException` otherwise. -/ + | castclass (type : Extraction.TypeExpr) + /-- `isinst`: the object when it is null or an instance of `type`; null otherwise. -/ + | isinst (type : Extraction.TypeExpr) + -- Instance fields, reached as `Method.fieldShape` resolves the field for the body. + /-- `ldfld`: replaces the receiver on top of the stack with the value its field holds. -/ + | ldfld (field : Extraction.FieldRef) + /-- `ldflda`: replaces the receiver with its field's address; a class field has none. -/ + | ldflda (field : Extraction.FieldRef) + /-- `stfld`: pops a value and a receiver below it and stores the value into the field. -/ + | stfld (field : Extraction.FieldRef) + -- Static fields; the descriptor extraction bound to the instruction's pc holds the storage. + /-- `ldsfld`: the value of the static field `field`. -/ + | ldsfld (field : Extraction.FieldRef) + /-- `ldsflda`: the address of the static field `field`, in read-only storage. -/ + | ldsflda (field : Extraction.FieldRef) + /-- `stsfld`: a store to the static field `field`; no static is writable, so it faults. -/ + | stsfld (field : Extraction.FieldRef) + /-- `ldtoken`: the runtime handle of the token `operand` names. -/ + | ldtoken (operand : TokenOperand) + | ret /-- Raises an exception of the named type; there are no handlers. -/ - | throw (exception : String) - | unsupported (description : String) + | throw (exception : ExceptionType) + /-- A hole carrying the raw `opcode` at its pc: an opcode no instruction family admits, or one + outside the code the extractor reaches under the selected profile. It faults when stepped. -/ + | unadmitted (opcode : Nat) deriving Repr +/-- The name of the instruction type, for tactics that recognise its constructors. -/ +def instructionTypeName : Lean.Name := ``CIL.Instruction + +/-- `(signed, remainder)` of a division or remainder instruction. -/ +def Instruction.divideKind? : Instruction → Option (Bool × Bool) + | .div => some (true, false) + | .div_un => some (false, false) + | .rem => some (true, true) + | .rem_un => some (false, true) + | _ => none + +/-- `(kind, signed)` of an overflow-checked arithmetic instruction. -/ +def Instruction.checkedKind? : Instruction → Option (CheckedBinaryKind × Bool) + | .add_ovf => some (.add, true) + | .add_ovf_un => some (.add, false) + | .sub_ovf => some (.sub, true) + | .sub_ovf_un => some (.sub, false) + | .mul_ovf => some (.mul, true) + | .mul_ovf_un => some (.mul, false) + | _ => none + /-- A literal, call or construction whose value a static constructor stores in a readonly field. -/ structure StaticInitializer where method : Nat @@ -206,16 +327,17 @@ structure StaticInitializer where construct : Bool := false deriving DecidableEq, Repr -/-- A static field the method reads: its bytes as extracted, and where they came from. -/ +/-- A static field the method reads: its bytes as extracted, and where they came from. `field` is + the field as the instruction at the site names it, which metadata binding ties to its row; + `identity` keys the field's storage. -/ structure StaticDescriptor where identity : Nat - fieldName : String + field : Extraction.FieldRef bytes : List (BitVec 8) /-- `some` for a value fixed by its static constructor, `none` for RVA data. -/ initializer : Option StaticInitializer := none /-- Integer fields use their byte width and this signed-load flag; aggregates use `none`. -/ scalar : Option Bool := none - assemblyIdentity : String := "" /-- Guaranteed alignment of the field's storage: the declared packing of RVA data, which the extractor checked divides the field's RVA. `1` promises nothing beyond byte alignment. -/ alignment : Nat := 1 @@ -224,24 +346,164 @@ structure StaticDescriptor where opaqueValue : Bool := false deriving DecidableEq, Repr +/-- How `this` reaches an instance method: an object reference for a class method, a managed + reference to the instance for a value-type method. -/ +inductive Receiver where + | object + | reference + deriving DecidableEq, Repr + +/-- A method's declared signature after its type arguments are closed. Aggregate types refer + to `LayoutEnvironment.rawLayouts`. Only implicit receiver signatures are supported. -/ +structure DeclaredSignature where + /-- `some` exactly for an instance method. -/ + receiver : Option Receiver := none + /-- Declared parameter types in order, `this` excluded. -/ + parameters : List Extraction.TypeExpr := [] + returnType : Extraction.TypeExpr := .void + /-- The body's localsinit flag: `localloc` storage and locals start zeroed when set. -/ + initLocals : Bool := true + deriving DecidableEq, Repr + +/-- The declared type of argument `index`, counting `this` as argument 0; `none` for `this`. -/ +def DeclaredSignature.argumentType (signature : DeclaredSignature) (index : Nat) : + Option Extraction.TypeExpr := + match signature.receiver, index with + | none, _ => signature.parameters[index]? + | some _, 0 => none + | some _, index + 1 => signature.parameters[index]? + +/-- Program-wide struct layouts the semantics may read, shared by every method of one + extraction. -/ +structure LayoutEnvironment where + /-- Raw struct metadata, the input that `LayoutsValid` recomputes layouts from. -/ + rawLayouts : List Extraction.RawLayout := [] + /-- The checked layout table, indexed like `rawLayouts`. -/ + layouts : Array Extraction.ComputedLayout := #[] + /-- The raw table in positional order for model lookups during execution and validation. -/ + indexedRaw : Array Extraction.RawLayout := rawLayouts.toArray + index_eq : indexedRaw = rawLayouts.toArray := by rfl + deriving DecidableEq, Repr + +/-- Where the call-family instruction at one pc goes. Every entry is checked against the call + site's metadata; method indices are positions in the `Program`. -/ +inductive Link where + /-- One body: a program method or import slot for `call`, a non-virtual `callvirt`, or the + implementation a `constrained.` call resolves to. -/ + | direct (method : Nat) + /-- A virtual `callvirt` of a class slot: the body each exact runtime class enters. -/ + | virtual (targets : List (Extraction.TypeExpr × Nat)) + /-- `newobj` of a value type: its constructor and the instance size from the layout table. -/ + | aggregate (constructor : Nat) (size : Fin (2^64)) + /-- `newobj` of a class: its constructor, admitted bases nearest first, and field bytes. -/ + | object (constructor : Nat) (bases : List Extraction.TypeExpr) (size : Nat) + /-- The body whose managed identity an `ldftn` pushes. Taking its address enters no body. -/ + | address (method : Nat) + /-- `calli`: the bodies a method pointer of the site's signature may name. -/ + | indirect (catalog : List ManagedTarget) + deriving DecidableEq, Repr + +/-- Every method index a link may enter. -/ +def Link.callees : Link → List Nat + | .direct method | .aggregate method _ | .object method _ _ => [method] + | .virtual targets => targets.map Prod.snd + | .address _ => [] + | .indirect catalog => catalog.map ManagedTarget.method + +/-- The constructor a `newobj` names; `none` for every other instruction. Its link is not implied + by the operand: the link table states the constructor's index and the allocation. -/ +def Instruction.constructor? (op : Instruction) : Option Extraction.MethodRef := by + cases op + case newobj constructor => exact some constructor + all_goals exact none + +/-- The instruction is a `newobj`. -/ +def Instruction.constructs (op : Instruction) : Bool := op.constructor?.isSome + +/-- An instruction that names a constructor is the `newobj` of it. -/ +theorem Instruction.eq_newobj_of_constructor? {op : Instruction} + {constructor : Extraction.MethodRef} (named : op.constructor? = some constructor) : + op = .newobj constructor := by + cases op <;> cases named <;> rfl + +#print axioms Instruction.eq_newobj_of_constructor? + +/-- Instructions whose metadata binding belongs to the method's link table. -/ +def Instruction.statesLink (op : Instruction) : Bool := by + cases op + case call | callvirt | newobj | ldftn | calli => exact true + all_goals exact false + +/-- Instructions whose execution can consult the link at their pc. -/ +def Instruction.readsLink (op : Instruction) : Bool := by + cases op + case call | callvirt | newobj | calli => exact true + all_goals exact false + /-- An extracted method body with the frame layout the interpreter needs. -/ structure Method where /-- The hardware features the method was compiled against. -/ profile : FeatureProfile := FeatureProfile.scalar - code : List Op + code : List Instruction /-- Initial values of the scalar locals. -/ locals : List Value localKinds : List LocalKind := [] staticSites : List (Nat × StaticDescriptor) := [] - /-- Locals kept in 256-bit vector homes rather than scalar slots. -/ - vectorLocals : List Nat := [] opaqueVectorLocals : List Nat := [] - /-- Arguments kept in 256-bit vector homes rather than scalar slots. -/ - vectorArgs : List Nat := [] parameterHomes : List (Nat × LocalKind) := [] + /-- Declared argument and return types; see `DeclaredSignature`. -/ + declared : DeclaredSignature := {} + /-- Program-wide layouts, the same value for every method of an extraction. -/ + environment : LayoutEnvironment := {} + /-- Call-family pc to link; the first entry for a pc wins. -/ + links : List (Nat × Link) := [] returnsValue : Bool deriving Repr +/-- The link the first entry of `links` for `query` gives, `none` without one. -/ +def linkLookup (links : List (Nat × Link)) (query : Nat) : Option Link := + (links.find? fun entry => entry.1 == query).map Prod.snd + +/-- The link of the call-family instruction at `pc`, if the table has one; the first entry wins. + An extracted method states each call site's link as a generated fact, so a proof need not + evaluate this scan; for any other body the scan runs over its table, which for a body without + a written `links` is empty. -/ +def Method.linkAt (body : Method) (pc : Nat) : Option Link := + linkLookup body.links pc + +/-- The static descriptor extraction bound to the static field instruction at `pc`; the first + site with that pc wins. -/ +def Method.staticAt (body : Method) (pc : Nat) : Option StaticDescriptor := + (body.staticSites.find? (fun site => site.1 == pc)).map Prod.snd + +/-- Replace the code and clear the links and static sites keyed by its pcs. -/ +def Method.withCode (method : Method) (code : List Instruction) : Method := + { method with code := code, links := [], staticSites := [] } + +/-- Replacing the code clears its link table. -/ +theorem Method.withCode_linkAt (method : Method) (code : List Instruction) (pc : Nat) : + (method.withCode code).linkAt pc = none := rfl + +#print axioms Method.withCode_linkAt + +/-- A table that lists no key `pc` gives it no link. -/ +theorem linkLookup_of_unlisted {links : List (Nat × Link)} {pc : Nat} + (unlisted : ∀ entry ∈ links, entry.1 ≠ pc) : linkLookup links pc = none := by + have none_found : (links.find? fun entry => entry.1 == pc) = none := by + rw [List.find?_eq_none] + intro entry member + simpa using unlisted entry member + simp [linkLookup, none_found] + +#print axioms linkLookup_of_unlisted + +/-- A body's table has no entry at a pc the table lists no key for. -/ +theorem Method.linkAt_of_unlisted {body : Method} {pc : Nat} + (unlisted : ∀ entry ∈ body.links, entry.1 ≠ pc) : body.linkAt pc = none := + linkLookup_of_unlisted unlisted + +#print axioms Method.linkAt_of_unlisted + /-- The methods a call refers to, by index. -/ abbrev Program := List Method diff --git a/CIL/IntegerArithmetic.lean b/CIL/IntegerArithmetic.lean index 2bc461738..5b35334f0 100644 --- a/CIL/IntegerArithmetic.lean +++ b/CIL/IntegerArithmetic.lean @@ -100,12 +100,18 @@ theorem checkedBits_mul_signed {width : Nat} {x y : BitVec width} rw [BitVec.ofInt_mul, BitVec.ofInt_toInt, BitVec.ofInt_toInt] #print axioms checkedBits_mul_signed -/-- An overflow-checked binary instruction on two `i32` or two `i64` stack values. -/ +/-- The int32 operand of a mixed operation, extended with the operation's signedness. -/ +def widenNative (signed : Bool) (bits : BitVec 32) : BitVec 64 := + if signed then bits.signExtend 64 else bits.zeroExtend 64 + +/-- Overflow-checked arithmetic on equal widths or a native integer paired with int32. -/ def checkedValues (kind : CheckedBinaryKind) (signed : Bool) (x y : Value) : Except ArithmeticFault Value := match x, y with | .i32 x, .i32 y => (checkedBits kind signed x y).map Value.i32 | .i64 x, .i64 y => (checkedBits kind signed x y).map Value.i64 + | .i32 x, .i64 y => (checkedBits kind signed (widenNative signed x) y).map Value.i64 + | .i64 x, .i32 y => (checkedBits kind signed x (widenNative signed y)).map Value.i64 | _, _ => .error .invalidOperands /-- An unsigned checked `add` of two `i32` values: a sum that fits is the wrapping sum. -/ @@ -219,11 +225,13 @@ def divideBits (signed remainder : Bool) (x y : BitVec width) : else return BitVec.ofNat width (if remainder then x.toNat % y.toNat else x.toNat / y.toNat) -/-- `div`, `div.un`, `rem` or `rem.un` on two `i32` or two `i64` stack values. -/ +/-- Division or remainder on equal widths or a mixed pair with int32 sign-extended. -/ def divideValues (signed remainder : Bool) (x y : Value) : Except ArithmeticFault Value := match x, y with | .i32 x, .i32 y => (divideBits signed remainder x y).map Value.i32 | .i64 x, .i64 y => (divideBits signed remainder x y).map Value.i64 + | .i32 x, .i64 y => (divideBits signed remainder (x.signExtend 64) y).map Value.i64 + | .i64 x, .i32 y => (divideBits signed remainder x (y.signExtend 64)).map Value.i64 | _, _ => .error .invalidOperands /-- `Math.DivRem` on `width`-bit operands, mirroring the runtime source: the quotient is the diff --git a/CIL/LiteralDisplay.lean b/CIL/LiteralDisplay.lean index 2fad88837..dd48bbea4 100644 --- a/CIL/LiteralDisplay.lean +++ b/CIL/LiteralDisplay.lean @@ -15,14 +15,22 @@ open Lean PrettyPrinter Delaborator SubExpr /-- The smallest bit-vector literal value printed in hexadecimal. -/ def hexLiteralMinimum : Nat := 2 ^ 16 +/-- The uppercase hexadecimal digits of `value`, such as `80000000`. -/ +def upperHexDigits (value : Nat) : String := + String.ofList ((Nat.toDigits 16 value).map Char.toUpper) + /-- `value` as an uppercase hexadecimal numeral, such as `0x80000000`. -/ def hexLiteral (value : Nat) : String := - "0x" ++ String.ofList ((Nat.toDigits 16 value).map Char.toUpper) + "0x" ++ upperHexDigits value + +/-- The value of a natural number literal, written as a numeral or as a raw literal. -/ +def natLiteral? (term : Expr) : Option Nat := + term.nat? <|> term.rawNatLit? /-- The literal `value` of a bit-vector numeral, when it is shown in hexadecimal. -/ private def hexDisplayed? (value : Expr) : DelabM (Option Nat) := do if ← getPPOption getPPAll then return none - return (value.nat?.orElse fun _ => value.rawNatLit?).filter (· ≥ hexLiteralMinimum) + return (natLiteral? value).filter (· ≥ hexLiteralMinimum) /-- Print `BitVec.ofNat w n` of a wide literal `n` as `0x...#w`. -/ @[delab app.BitVec.ofNat] diff --git a/CIL/Memory.lean b/CIL/Memory.lean index e8305776e..bcda90b92 100644 --- a/CIL/Memory.lean +++ b/CIL/Memory.lean @@ -39,23 +39,7 @@ def readStaticBytes (bytes : List (BitVec 8)) (offset : Nat) : (width : Nat) → let high ← readStaticBytes bytes (offset + 1) width return low.toNat + 256 * high -/-- The little-endian number at `offset` in a private vector home. -/ -def readHomeBytes (memory : Memory) (frame kind index offset : Nat) : (width : Nat) → Option Nat - | 0 => some 0 - | width + 1 => do - let .i8 low ← memory (.home frame kind index offset) | none - let high ← readHomeBytes memory frame kind index (offset + 1) width - return low.toNat + 256 * high - -/-- Stores `value` little-endian at `offset` in a private vector home. -/ -def writeHomeBytes (memory : Memory) (frame kind index offset value : Nat) : (width : Nat) → Memory - | 0 => memory - | width + 1 => - writeHomeBytes (write memory (.home frame kind index offset) (.i8 (BitVec.ofNat 8 value))) - frame kind index (offset + 1) (value / 256) width - -/-- Reads a 64-bit word. A local must already hold an `i64`; a home read must stay - within its 32 bytes. -/ +/-- Reads a 64-bit word. A local must already hold an `i64`. -/ def read64 (memory : Memory) : Address → Option Value | .byte base => do return .i64 (BitVec.ofNat 64 (← readBytes memory base 8)) | .local frame index => do @@ -63,19 +47,12 @@ def read64 (memory : Memory) : Address → Option Value return .i64 w | .static bytes offset => do return .i64 (BitVec.ofNat 64 (← readStaticBytes bytes offset 8)) - | .home frame kind index offset => do - if offset + 8 ≤ 32 then - return .i64 (BitVec.ofNat 64 (← readHomeBytes memory frame kind index offset 8)) - else none -/-- Writes a 64-bit word. Read-only data cannot be written; a home write must stay - within its 32 bytes. -/ +/-- Writes a 64-bit word. Read-only data cannot be written. -/ def write64 (memory : Memory) (address : Address) (word : W64) : Option Memory := match address with | .byte base => some (writeBytes memory base word.toNat 8) | .local frame index => some (write memory (.local frame index) (.i64 word)) | .static _ _ => none - | .home frame kind index offset => - if offset + 8 ≤ 32 then some (writeHomeBytes memory frame kind index offset word.toNat 8) else none end CIL diff --git a/CIL/MemoryLemmas.lean b/CIL/MemoryLemmas.lean index b183a6516..4ce2f236b 100644 --- a/CIL/MemoryLemmas.lean +++ b/CIL/MemoryLemmas.lean @@ -3,8 +3,8 @@ import CIL.Semantics /-! # Scalar memory lemmas -Frame facts for the reference interpreter's memory: caller bytes, scalar locals and vector -homes are separate address spaces, so a write to one leaves reads of the others unchanged. +Frame facts for the reference interpreter's memory: caller bytes and scalar locals +are separate address spaces, so a write to one leaves reads of the others unchanged. Stepping proofs use these to carry facts about caller memory across local updates. -/ @@ -14,14 +14,6 @@ set_option maxHeartbeats 2000000 namespace CIL -@[simp] theorem writeBytes_home (m : Memory) - (base word count frame kind index offset : Nat) : - writeBytes m base word count (.home frame kind index offset) = - m (.home frame kind index offset) := by - induction count generalizing m base word with - | zero => rfl - | succ count ih => rw [writeBytes, ih]; simp [write] - /-- A caller-byte read depends only on caller bytes. -/ theorem readBytes_congr (m n : Memory) (h : ∀ address, m (.byte address) = n (.byte address)) (base count : Nat) : diff --git a/CIL/MethodBinding.lean b/CIL/MethodBinding.lean index 35ac468cd..10c028abd 100644 --- a/CIL/MethodBinding.lean +++ b/CIL/MethodBinding.lean @@ -17,9 +17,8 @@ namespace CIL.MethodBinding open Lean Meta Elab Tactic --- Diagnostic comparison only; proofs never depend on these instances. -deriving instance BEq for MemoryOp -deriving instance BEq for Op +-- Diagnostic comparison only; proofs never depend on this instance. +deriving instance BEq for Instruction /-- The first index where two lists differ, with the element of each side there. -/ def firstDifference [BEq T] : @@ -30,6 +29,21 @@ def firstDifference [BEq T] : | a :: _, [], index => some (index, some a, none) | [], b :: _, index => some (index, none, some b) +/-- First program counter where decoded instructions or their ordered call links differ. + A caller comparing profiles relocates both link tables to the same callee numbering. -/ +def firstCodeOrLinkDifference (left right : Method) : Option Nat := + let code := (firstDifference left.code right.code 0).map Prod.fst + let link := (firstDifference left.links right.links 0).bind fun (_, lhs, rhs) => + match lhs, rhs with + | some a, some b => some (min a.1 b.1) + | some a, none => some a.1 + | none, some b => some b.1 + | none, none => none + match code, link with + | some pc, some site => some (min pc site) + | _, none => code + | none, _ => link + /-- An element for a diagnostic, or a marker when one list has ended. -/ private def element [Repr T] : Option T -> String | some value => reprStr value @@ -47,16 +61,36 @@ def valueField [BEq T] [Repr T] (field : String) (left right : T) : Option Strin if left == right then none else some s!"field {field}:\n left: {reprStr left}\n right: {reprStr right}" +/-- The environment difference, without printing the layout tables. -/ +def environmentField (left right : LayoutEnvironment) : Option String := + if left == right then none + else some (s!"field environment: the layout tables differ\n" ++ + s!" left: {left.rawLayouts.length} raw layouts, {left.layouts.size} computed\n" ++ + s!" right: {right.rawLayouts.length} raw layouts, {right.layouts.size} computed") + +/-- How to bind the pc-indexed links of a replacement body. -/ +def linksHint : String := + "\n a body written as `{ method with code := ... }` keeps the links of `method`, keyed by its \ + own pcs. `Method.withCode` clears the table. Supply the links at the replacement body's pcs \ + in ascending order with `{ body with links := [(pc, link), ...] }`" + +/-- What to do when a body's static sites differ: `Method.withCode` resets them. -/ +def staticSitesHint : String := + "\n static sites are keyed by pc; `Method.withCode` resets them, so a body that reads statics \ + supplies its own after it: `{ body with staticSites := [(pc, descriptor), ...] }`, one entry \ + per static access at the body's pc, with the descriptor the extraction has for that access" + /-- One line per differing `Method` field, in declaration order. -/ def methodDifferences (left right : Method) : List String := [ valueField "profile" left.profile right.profile, listField "code" left.code right.code, listField "locals" left.locals right.locals, listField "localKinds" left.localKinds right.localKinds, - listField "staticSites" left.staticSites right.staticSites, - listField "vectorLocals" left.vectorLocals right.vectorLocals, - listField "vectorArgs" left.vectorArgs right.vectorArgs, + (listField "staticSites" left.staticSites right.staticSites).map (· ++ staticSitesHint), listField "parameterHomes" left.parameterHomes right.parameterHomes, + valueField "declared" left.declared right.declared, + environmentField left.environment right.environment, + (listField "links" left.links right.links).map (· ++ linksHint), valueField "returnsValue" left.returnsValue right.returnsValue ].filterMap id /-- The type `CIL.Method` as an expression, for evaluating a side. -/ diff --git a/CIL/MethodIdentity.lean b/CIL/MethodIdentity.lean index 055a8f0a9..106316642 100644 --- a/CIL/MethodIdentity.lean +++ b/CIL/MethodIdentity.lean @@ -77,36 +77,78 @@ syntax (name := methodNamed) "method_named " str : term let some signature := stx[1].isStrLit? | throwUnsupportedSyntax return mkNatLit (← resolve signature) -/-- A call instruction found in a method body: `CIL.Op.call` or the receiver-checked - `CIL.Op.callInstance`, with its pc, callee index and argument count. -/ +/-- Elaborates `fact`, a `code[pc]? = ...` fact proved by `rfl`; unfolding the index +needs recursion depth linear in `pc`, beyond the default for long extracted bodies. -/ +def elabAtPc (pc : Nat) (fact : Command) : CommandElabM Unit := do + elabCommand + (← `(command| set_option maxRecDepth $(Syntax.mkNumLit (toString (512 + 4 * pc))) in $fact)) + +/-- A call instruction found in a method body: `CIL.Instruction.call` or the receiver-checked + `CIL.Instruction.callvirt`, with its pc, callee index, argument count and the + instruction term as the body writes it. -/ structure CallAt where pc : Nat callee : Nat argc : Nat - opcode : Name + instruction : Expr deriving Inhabited -/-- The instruction a call site holds, as syntax with `index` for its callee. -/ -def CallAt.instruction (site : CallAt) (index : Term) : MacroM Term := - `($(mkCIdent site.opcode) $index $(quote site.argc)) +/-- Declares `name : parent.code[pc]? = some op` for the call `site` of the closed body + `parent`, with the method reference its raw site names. -/ +def emitCallFact (name : Ident) (parent : Term) (site : CallAt) : CommandElabM Unit := do + liftTermElabM do + let body ← instantiateMVars (← elabTermEnsuringType parent (some (mkConst ``CIL.Method))) + let lookup ← mkAppM ``GetElem?.getElem? + #[mkApp (mkConst ``CIL.Method.code) body, mkNatLit site.pc] + let type ← mkEq lookup (← mkAppM ``Option.some #[site.instruction]) + let declaration := (← getCurrNamespace) ++ name.getId + let value ← mkEqRefl lookup + addDecl (.thmDecl { name := declaration, levelParams := [], type, value }) + elabCommand (← `(attribute [cil_code] $name)) + +/-- The callee of each direct link in a method's table, keyed by pc; the first entry for a pc +wins, as in `Method.linkAt`. One cell is reduced at a time, so the cost is linear in the table. -/ +def directLinks (method : Expr) : MetaM (Std.HashMap Nat Nat) := do + let mut cell := mkApp (mkConst ``CIL.Method.links) method + let mut found : Std.HashMap Nat Nat := {} + let mut seen : Std.HashSet Nat := {} + repeat + match (← whnf cell).getAppFnArgs with + | (``List.cons, #[_, entry, tail]) => + if let (``Prod.mk, #[_, _, pc, link]) := (← whnf entry).getAppFnArgs then + if let some pc ← (evalNat (← whnf pc)).run then + unless seen.contains pc do + seen := seen.insert pc + if let (``CIL.Link.direct, #[callee]) := (← whnf link).getAppFnArgs then + if let some callee ← (evalNat (← whnf callee)).run then + found := found.insert pc callee + cell := tail + | _ => break + return found -/-- Every call in a method body, reduced one cell at a time so the cost is linear in the body -and the body is never normalized. -/ +/-- Every call in a method body, with the callee its link table gives the call's pc. Code and +table are reduced one cell at a time, so the cost is linear in the body and the body is never +normalized; a call the table does not link to a method is no call site. -/ def calls (method : Expr) : MetaM (Array CallAt) := do let mut cell := mkApp (mkConst ``CIL.Method.code) method let mut pc := 0 - let mut found := #[] + let mut sites := #[] repeat match (← whnf cell).getAppFnArgs with | (``List.cons, #[_, op, tail]) => - if let (opcode, #[callee, argc]) := (← whnf op).getAppFnArgs then - if opcode == ``CIL.Op.call || opcode == ``CIL.Op.callInstance then - if let (some callee, some argc) := (← (evalNat callee).run, ← (evalNat argc).run) then - found := found.push { pc, callee, argc, opcode } + let instruction ← whnf op + match instruction.getAppFnArgs with + | (``CIL.Instruction.call, #[method, _]) | (``CIL.Instruction.callvirt, #[method]) => + let count ← reduce (mkApp (mkConst ``CIL.Extraction.MethodRef.argumentCount) method) + if let some argc ← (evalNat count).run then + sites := sites.push (pc, argc, instruction) + | _ => pure () cell := tail pc := pc + 1 | _ => break - return found + let linked ← directLinks method + return sites.filterMap fun (pc, argc, instruction) => + linked[pc]?.map fun callee => { pc, callee, argc, instruction } /-- The candidate selected by `[occurrence]`, or the only one; never the first of several. -/ def choose (subject : MessageData) (candidates : Array String) (occurrence : Option Nat) : @@ -120,12 +162,6 @@ def choose (subject : MessageData) (candidates : Array String) (occurrence : Opt if candidates.size == 1 then return 0 else throwError "{subject}; select one with [i]:\n{listing}" -/-- Elaborates `fact`, a `code[pc]? = ...` fact proved by `rfl`; unfolding the index -needs recursion depth linear in `pc`, beyond the default for long extracted bodies. -/ -def elabAtPc (pc : Nat) (fact : Command) : CommandElabM Unit := do - elabCommand - (← `(command| set_option maxRecDepth $(Syntax.mkNumLit (toString (512 + 4 * pc))) in $fact)) - /-- Defines `index` and `body` for extraction method `method` and proves by `rfl` that the signature table and the program agree with them. -/ def emit (index body : Ident) (signature : StrLit) (method : Nat) : CommandElabM Unit := do @@ -139,6 +175,11 @@ def emit (index body : Ident) (signature : StrLit) (method : Nat) : CommandElabM elabCommand (← `(@[cil_code] theorem $(mkIdent (index.getId ++ `found)) : $program[$index]? = some $body := rfl)) +/-- Declares `name : parent.linkAt pc = some (.direct callee)`, the link a call site enters by. -/ +def emitLink (name : Ident) (parent : Term) (pc : Nat) (callee : Term) : CommandElabM Unit := do + elabAtPc pc (← `(@[cil_code] theorem $name : + ($parent).linkAt $(quote pc) = some (.direct $callee) := rfl)) + /-- `bind_method index body := "signature"` defines `index` as the extraction index of the method with that exact signature and `body` as its extracted body, with `index.signature` and `index.found` checked by `rfl`. -/ @@ -172,9 +213,9 @@ def callSite (parent : Term) (method : Nat) (callee : String) (occurrence : Opti entryAt "callSite" sites chosen /-- `bind_callee index body := "signature" from parent` binds as `bind_method` does and -also requires `parent` to call that method; `index.call` states the instruction at its -call site, and `[k]` selects one of several. Expected bodies take `index` as a parameter, -`.call index argc`, so a renumbered extraction never changes them. -/ +also requires `parent` to call that method, by the link of the call site; `index.call` states +the instruction at the site, `index.link` its link, and `[k]` selects one of several. The link +names `index`, so a renumbered extraction never changes a proof that reads the callee from it. -/ syntax (name := bindCallee) (docComment)? "bind_callee " ident ppSpace ident " := " str " from " term:max (" [" num "]")? : command @@ -187,9 +228,8 @@ elab_rules : command return (method, ← callSite parent method signature.getString (occurrence.map (·.getNat))) emit index body signature method documentBinding doc index - let instruction ← liftMacroM (site.instruction index) - elabAtPc site.pc (← `(@[cil_code] theorem $(mkIdent (index.getId ++ `call)) : - ($parent).code[$(quote site.pc)]? = some $instruction := rfl)) + emitCallFact (mkIdent (index.getId ++ `call)) parent site + emitLink (mkIdent (index.getId ++ `link)) parent site.pc index /-- `bind_callee index body := call argc from parent` binds the method `parent` calls with `argc` arguments, for helpers whose names are not stable; `[k]` selects one of several such @@ -219,9 +259,8 @@ elab_rules : command return (site, table[site.callee]?.getD "") emit index body (Syntax.mkStrLit signature) site.callee documentBinding doc index - let instruction ← liftMacroM (site.instruction index) - elabAtPc site.pc (← `(@[cil_code] theorem $(mkIdent (index.getId ++ `call)) : - ($parent).code[$(quote site.pc)]? = some $instruction := rfl)) + emitCallFact (mkIdent (index.getId ++ `call)) parent site + emitLink (mkIdent (index.getId ++ `link)) parent site.pc index /-- The number of cells in a closed list, reduced one cell at a time without normalizing its elements. -/ diff --git a/CIL/MethodPointers.lean b/CIL/MethodPointers.lean index 1507d5c5b..457dbc98f 100644 --- a/CIL/MethodPointers.lean +++ b/CIL/MethodPointers.lean @@ -46,22 +46,16 @@ structure MethodTableBacking where deriving DecidableEq, Repr /-- Supplied immutable handler entries sharing one closed signature; `none` is a null table. - Native cursors additionally retain the backing allocation and table origin. -/ + Native cursors additionally retain the backing allocation and table origin. The entry count + is carried beside the entries, so a native table step reads it without walking them. -/ structure MethodTable where signature : ManagedSignature targets : Option (Array ManagedIdentity) backing : Option MethodTableBacking := none - deriving DecidableEq, Repr - -/-- Managed pointer operations preserve metadata provenance instead of encoding addresses. -/ -inductive ManagedOp where - | address (target : ManagedTarget) (signature : ManagedSignature) - | tableLength - | tableElement (signature : ManagedSignature) - | tableAddress (signature : ManagedSignature) - | tableOffset - | tableLoad (signature : ManagedSignature) - | call (signature : ManagedSignature) (catalog : List ManagedTarget) (tail : Bool) + /-- The number of supplied entries. -/ + entryCount : Nat + /-- The supplied entries number exactly `entryCount`, proved once where the table is built. -/ + counted : ∀ entries ∈ targets, entries.size = entryCount := by decide deriving DecidableEq, Repr end CIL diff --git a/CIL/NativeEvaluation.lean b/CIL/NativeEvaluation.lean index fd0ac2547..a5624fe6a 100644 --- a/CIL/NativeEvaluation.lean +++ b/CIL/NativeEvaluation.lean @@ -1,50 +1,31 @@ -import CIL.Semantics +import CIL.Instructions /-! # Mixed-width native operands -Binary instructions whose operands are an `i32` and a native-width `i64`, as CoreCLR -accepts them. The `i32` operand is widened the way the runtime widens it, then the -instruction runs through the ordinary `step`, `divideValues` or `checkedValues`, so the -mixed path adds no arithmetic of its own. The checked interpreter dispatches here. +Binary instructions whose operands are an `i32` and a native-width `i64`, as ECMA-335 III.1.5 +admits them. `CIL.step` widens the `i32` operand the way the runtime widens it before it +selects an arm, so a mixed pair runs the same-width arithmetic and adds none of its own. +Division, remainder and overflow-checked arithmetic widen inside `divideValues` and +`checkedValues` instead. -/ namespace CIL -/-- Only mixed integer arithmetic, comparisons and branches may use this path. - It cannot dispatch memory, calls, features or another native wrapper. -/ -def nativePureBinary : Op → Bool - | .add | .sub | .mul | .band | .bor | .bxor | .lt | .ltu | .gt | .gtu | .eq +/-- The instructions whose mixed operands `step` widens: wrapping arithmetic, bitwise + operations, comparisons and signed compare-and-branch. Memory operations, calls, feature + queries and unsigned branches keep their operands. -/ +def nativePureBinary : Instruction → Bool + | .add | .sub | .mul | .and | .or | .xor | .clt | .clt_un | .cgt | .cgt_un | .ceq | .bgt _ | .ble _ | .bge _ | .blt _ | .beq _ => true | _ => false -/-- The admitted .NET runtime zero-extends for unsigned checked arithmetic. - Other admitted mixed operations sign-extend, including scalar clt.un/cgt.un. - Mixed unsigned branches are rejected: CoreCLR's widening depends on whether - its importer sees a constant, which can change through inlining. -/ -def widenNativeOperand (op : Op) (bits : W32) : W64 := - match op with - | .checkedArithmetic _ false => bits.zeroExtend 64 - | _ => bits.signExtend 64 - -/-- Runs a mixed `i32`/`i64` binary instruction at `pc`: the next pc and the values it - pushes, or the arithmetic fault. Any other operand pair is `invalidOperands`. -/ -def nativeBinaryValues (op : Op) (pc : Nat) (x y : Value) : - Except ArithmeticFault (Nat × List Value) := do - let (x, y) ← match x, y with - | .i32 a, .i64 b => .ok (Value.i64 (widenNativeOperand op a), Value.i64 b) - | .i64 a, .i32 b => .ok (Value.i64 a, Value.i64 (widenNativeOperand op b)) - | _, _ => .error .invalidOperands - match op with - | .divide signed remainder => - return (pc + 1, [← divideValues signed remainder x y]) - | .checkedArithmetic kind signed => - return (pc + 1, [← checkedValues kind signed x y]) - | _ => - if nativePureBinary op then - match step op false pc [] 0 [y, x] (fun _ => none) with - | some (.next target values _) => .ok (target, values) - | _ => .error .invalidOperands - else .error .invalidOperands +/-- The stack with the int32 operand of a mixed int32 and native pair on top sign-extended to + 64 bits, for an instruction `nativePureBinary` admits; any other stack is unchanged. -/ +def widenMixed (operation : Instruction) (stack : List Value) : List Value := + match nativePureBinary operation, stack with + | true, .i64 right :: .i32 left :: rest => .i64 right :: .i64 (left.signExtend 64) :: rest + | true, .i32 right :: .i64 left :: rest => .i64 (right.signExtend 64) :: .i64 left :: rest + | _, stack => stack end CIL diff --git a/CIL/Observe.lean b/CIL/Observe.lean index 15d45c0e8..f2f9141b4 100644 --- a/CIL/Observe.lean +++ b/CIL/Observe.lean @@ -154,6 +154,35 @@ def residualOf (step : MVarId) : MetaM (Option (Array Expr)) := step.withContext result.set (← stepResidual step (← facts.mapM fun fact => Term.exprToSyntax fact)) result.get +/-- The step goal `step` of a call or construction that runs its runtime model, rewritten to + that model's step; nothing for one that enters a callee. -/ +def modelledStepOf (step : MVarId) : MetaM (Option MVarId) := step.withContext do + let result ← IO.mkRef none + discard <| Term.TermElabM.run' <| Tactic.run step do result.set (← modelledCallStep? step) + result.get + +/-- The step goal `step` of a memory operation with each home among its view operands read as + the view its literal frame slot holds, as `viewedStack` reads them for operation facts; + `step` itself when no operand is such a home. -/ +def viewedStepGoal (step : MVarId) : MetaM MVarId := step.withContext do + let some (_, action, _) := (← instantiateMVars (← step.getType)).eq? | return step + let some #[model, body, pc, frame, stack, memory] := + argumentsOf? action `CIL.Safety.modelStep 6 | return step + let some #[_, value] := argumentsOf? model `Option.some 2 | return step + let some #[operation] := argumentsOf? value `CIL.RuntimeModel.memory 1 | return step + let viewed ← viewedStack frame operation stack + if viewed == stack then return step + let stepTerm ← mkAppM `CIL.Safety.modelStep #[model, body, pc, frame, viewed, memory] + let goal ← mkFreshExprMVar (← mkEq stepTerm (← mkFreshExprMVar (← inferType stepTerm))) + return goal.mvarId! + +/-- Whether the step goal `step` is of a call or construction with neither a link nor a + runtime model, which no step crosses. -/ +def unmodelledStepOf (step : MVarId) : MetaM Bool := step.withContext do + let result ← IO.mkRef false + discard <| Term.TermElabM.run' <| Tactic.run step do result.set (← unmodelledCall step) + result.get + /-! ## Failure records -/ /-- The non-null object references among the literal arguments `args`. -/ @@ -236,26 +265,45 @@ def analyze (tactic : String) (goal : MVarId) (handed : Option MVarId) (error : let record := { record with instruction := op } let calling := tactic.endsWith "_call" -- A receiver-checked call crosses into its callee as a direct call does. - let isCall := (← whnfR op).isAppOf `CIL.Op.call || (← whnfR op).isAppOf `CIL.Op.callInstance - if isCall && !calling then - return { record with - missing := some "calleeContract", - suggestions := #[m!"cross the call with the call tactic of its stepping family and the \ - callee's contract"] } + let isCall := (← whnfR op).isAppOf ``CIL.Instruction.call || + (← whnfR op).isAppOf ``CIL.Instruction.callvirt if calling && !isCall then return { record with missing := some "callSite", suggestions := #[m!"step to the call first"] } - let some frame := stepping.parts.find? (·.1 == "frame") | return record - let some stack := stepping.parts.find? (·.1 == "stack") | return record - let some memory := stepping.parts.find? (·.1 == "memory") | return record + let callee := { record with + missing := some "calleeContract", + suggestions := #[m!"cross the call with the call tactic of its stepping family and the \ + callee's contract"] } + let unmodelled := { record with + missing := some "linkOrModel", + suggestions := #[m!"the site has neither a link nor a runtime model, so no step crosses it"] } + let entersCallee := isCall && !calling + let constructs := (← whnfR op).isAppOf ``CIL.Instruction.newobj + let unread := if entersCallee then callee else record + let some frame := stepping.parts.find? (·.1 == "frame") | return unread + let some stack := stepping.parts.find? (·.1 == "stack") | return unread + let some memory := stepping.parts.find? (·.1 == "memory") | return unread let stepTerm ← mkAppM `CIL.Safety.step #[body, op, pcTerm, stepping.args, frame.2, stack.2, memory.2] let resultType ← inferType stepTerm let step ← mkFreshExprMVar (← mkEq stepTerm (← mkFreshExprMVar resultType)) let .mvar stepGoal := step | throwError "analyze: expected a fresh metavariable, but found{indentExpr step}" - let residual := (← residualOf stepGoal).getD #[← instantiateMVars (← inferType step)] + -- A call or construction without a link runs its runtime model in this method, so its residual + -- is the model's; with no model either, nothing steps it. + let stepGoal ← if entersCallee || constructs then + match ← modelledStepOf stepGoal with + | some modelled => pure modelled + | none => + if ← unmodelledStepOf stepGoal then return unmodelled + if entersCallee then return callee + pure stepGoal + else pure stepGoal + -- Reading a home's slot needs only the view's validity, which the stepping facts give, so the + -- operation's own missing fact is read from the step on the view the slot holds. + let stepGoal ← viewedStepGoal stepGoal + let residual := (← residualOf stepGoal).getD #[← instantiateMVars (← stepGoal.getType)] let missing := missingFactOf residual return { record with missing := missing.name, residual, diff --git a/CIL/OleanAudit.lean b/CIL/OleanAudit.lean index a72a530e1..21c256822 100644 --- a/CIL/OleanAudit.lean +++ b/CIL/OleanAudit.lean @@ -12,6 +12,7 @@ import CIL.Interface.Headers module parts with only persisted linter diagnostics removed. Neither mode loads extensions. `--replay INPUT` kernel-checks an exact JSON array of target modules against shared imports. `--replay-separately INPUT` reports isolated toolchain replay results for each target. +`--diagnose-replay INPUT` reports typed isolated checking failures without accepting a proof. An audit request with `replayTargets` checks those modules first and reuses their checked kernel environment when declaration names are unique and the audit needs no target module metadata. @@ -23,10 +24,13 @@ REQUEST (JSON): { "modules": ["Proof", ...], "theorems": [{ "name": N, "type": E?, "levelParams": [u, ...]?, "axioms": [N, ...], "statement": bool?, "refutation": bool?, "term": bool?, - "expected": { "term": T, "negated": bool? }?, "allowedModules": [M, ...]? }], + "expected": { "term": T, "refuted": bool }?, "allowedModules": [M, ...]? }], "constants": [{ "name": N, "value": E, "type": E?, "axioms": [N, ...]? }], "interfaces": [M, ...]?, "closures": [{ "name": N }]?, "replayTargets": [M, ...]?, "namespaces": { "prefixes": [P, ...], "axioms": [N, ...] }? } +Keys must be unique within each object, including keys spelled with JSON escapes. +Each `expected` object must contain exactly `term` and the boolean `refuted`. +A duplicate key rejects the request. An invalid comparison field gives that theorem an `error`. A theorem must be a `theorem` whose type matches `E` (when given) and whose axiom closure is in `axioms`. An `allowedModules` scope requires its declaring module and every reachable replay-target declaration's module to belong to that set; @@ -41,6 +45,7 @@ Expected expressions `E`: "_" any expression {"const": N, "levels": [L, ...]} constant; `levels` defaults to [] {"app": [E, E1, ..., En]} `E` applied to exactly the trailing n arguments + {"list": {"type": E, "level": L, "items": [E, ...]}} exact `List.cons`/`List.nil` spine {"nat": n} the canonical numeral `@OfNat.ofNat Nat n (instOfNatNat n)` {"lit": n} / {"str": s} raw literals {"sort": L} / {"bvar": i} @@ -49,13 +54,19 @@ Levels L: n (the n-th successor of zero), {"param": u}, {"succ": L}, {"max": [L, {"imax": [L, L]}. Comparison is structural after removing metadata: no unfolding, instances or elaboration. `term` reports the theorem's type as a `statementTerm` T, for another audit's `expected`: there the -theorem `matches` when the kernel accepts it at type T (or `¬ T` when `negated`), structurally or by -definitional unfolding within `statementHeartbeats`; otherwise `mismatch` says why. +theorem `matches` when the kernel accepts it at type T, or, when `refuted`, at the counterexample +predicate of the contract whose `Holds` heads T applied to T's arguments, structurally or by +definitional unfolding within `statementHeartbeats`; otherwise `mismatch` says why. `refutation` +reports a theorem stating a library contract's counterexample predicate as `refutes`, and fails +one stating `Not` of a library contract's `Holds`, each read through the definitions at the +statement's head as the kernel comparison unfolds them. A theorem that `matches` when `refuted` +reports as `refutes` the subject of the counterexample predicate it was compared with. RESULT (JSON): { "modules": { N: olean path }, "theorems": [{ "name": N, "error": string?, "axioms": [N, ...], "statement": string?, - "refutes": { "contract": N, "line": n, "column": n }?, + "refutes": { "contract": N, "program": N?, "method": n?, "line": n, + "column": n }?, "term": T?, "termError": string?, "matches": bool?, "mismatch": string? }], "constants": [{ "name": N, "error": string?, "axioms": [N, ...] }], "interfaces": { "error": string? }, @@ -69,6 +80,40 @@ Per-item failures are reported in `error`; a missing module or malformed request namespace LeanCILAudit open Lean +/-- A decoded object key and its hash. Equal hashes still require equal text. -/ +private structure RequestKey where + hashCode : UInt64 + text : String + deriving BEq + +/-- Resizing the set of request keys reads the stored hash in constant time. -/ +private instance : Hashable RequestKey where + hash key := key.hashCode + +open Std.Internal.Parsec Std.Internal.Parsec.String in +/-- Checks decoded keys in each open object before JSON parsing can overwrite a duplicate. -/ +private partial def checkUniqueKeys [Hashable String] (objects : List (Std.HashSet RequestKey)) : + Parser Unit := do + if <- isEof then return + match <- any with + | '{' => checkUniqueKeys ({} :: objects) + | '}' => checkUniqueKeys (objects.drop 1) + | '"' => + let key <- Json.Parser.str + ws + -- Only object keys are followed by a colon outside a string. + if (<- peek?) == some ':' then + if let keys :: parents := objects then + let (duplicate, keys) := keys.containsThenInsert { hashCode := hash key, text := key } + if duplicate then fail s!"Duplicate JSON key: {key}" + return <- checkUniqueKeys (keys :: parents) + checkUniqueKeys objects + | _ => checkUniqueKeys objects + +/-- Refuses a repeated decoded key within any object of the request text. -/ +def checkRequestKeys [Hashable String] (source : String) : Except String Unit := + (checkUniqueKeys []).run source + /-- The declaration name written as `s`. -/ def name (s : String) : Name := s.toName @@ -107,6 +152,20 @@ partial def fits (j : Json) (e : Expr) : Except String Bool := do for spec in args, arg in actual.reverse do unless ← fits spec arg do return false return true + if let .ok spec := j.getObjVal? "list" then + let elementType ← spec.getObjVal? "type" + let expectedLevel ← level (← spec.getObjVal? "level") + let items ← spec.getObjValAs? (Array Json) "items" + let mut tail := e + for item in items do + let .app (.app (.app (.const `List.cons [actualLevel]) actualType) head) rest := tail + | return false + unless actualLevel == expectedLevel do return false + unless ← fits elementType actualType do return false + unless ← fits item head do return false + tail := rest + let .app (.const `List.nil [actualLevel]) actualType := tail | return false + return actualLevel == expectedLevel && (← fits elementType actualType) if let .ok n := j.getObjValAs? Nat "nat" then return e == canonicalNat n if let .ok n := j.getObjValAs? Nat "lit" then return e == .lit (.natVal n) if let .ok s := j.getObjValAs? String "str" then return e == .lit (.strVal s) @@ -180,10 +239,61 @@ partial def stripMData (e : Expr) : Expr := e.replace fun | .mdata _ b => some (stripMData b) | _ => none -/-- The contract predicates whose negation a refutation theorem may state. -/ -def contracts : List Name := - [`CIL.Safety.MethodContract.Holds, `CIL.Safety.OutcomeContract.Holds, - `CIL.Safety.OutcomeContract.HoldsCLR] +/-- A contract predicate, the counterexample predicate that refutes it with the same arguments, + and the module that declares both. -/ +structure Contract where + holds : Name + refuted : Name + module : Name + +/-- The contracts a refutation theorem may refute, matched by name and declaring module. -/ +def contracts : List Contract := + [⟨`CIL.Safety.MethodContract.Holds, `CIL.Safety.MethodContract.Refuted, + `CIL.Safety.MethodContract⟩, + ⟨`CIL.Safety.OutcomeContract.Holds, `CIL.Safety.OutcomeContract.Refuted, + `CIL.Safety.OutcomeContract⟩, + ⟨`CIL.Safety.OutcomeContract.HoldsCLR, `CIL.Safety.OutcomeContract.RefutedCLR, + `CIL.Safety.RuntimeExceptions⟩] + +/-- How many definitions `unfoldAliases` unfolds at the head of a statement. -/ +def aliasDepth : Nat := 16 + +/-- `type` with the definition at its head unfolded, at most `depth` times, until the head is + `Not`, a contract predicate's name or no definition. Classification reads statements through + it, as the kernel comparison of statements unfolds any definition, so an alias of a contract + claim is classified as the claim it stands for. -/ +def unfoldAliases (find : Name → Option ConstantInfo) : Nat → Expr → Expr + | 0, type => type + | depth + 1, type => + match type.getAppFn with + | .const name levels => + if name == `Not || contracts.any (fun c => c.holds == name || c.refuted == name) then type + else match find name with + | some (.defnInfo info) => unfoldAliases find depth <| stripMData <| + (info.value.instantiateLevelParams info.levelParams levels).beta type.getAppArgs + | _ => type + | _ => type + +/-- `type` and, under a `Not`, its claim, read through `unfoldAliases`. -/ +def contractClaim (find : Name → Option ConstantInfo) (type : Expr) : Expr := + match unfoldAliases find aliasDepth type with + | .app (.const `Not levels) claim => + .app (.const `Not levels) (unfoldAliases find aliasDepth claim) + | unfolded => unfolded + +/-- Whether `type`, read through `contractClaim`, names a contract predicate by name alone: a + `Refuted` application, or `Not` applied to a `Holds` one. -/ +def namesContract (find : Name → Option ConstantInfo) (type : Expr) : Bool := + match contractClaim find type with + | .app (.const `Not _) claim => contracts.any (claim.getAppFn.constName? == some ·.holds) + | claim => contracts.any (claim.getAppFn.constName? == some ·.refuted) + +/-- The library contract whose predicate `select` picks as the head of `claim`, checked against + the module `ownerOf` gives. -/ +def contractAt (ownerOf : Name → Option Name) (select : Contract → Name) (claim : Expr) : + Option Contract := + claim.getAppFn.constName?.bind fun head => + contracts.find? fun contract => select contract == head && ownerOf head == some contract.module /-- The context for pretty-printing and source ranges; no elaboration runs in it. With no open namespaces or aliases only full names resolve, so `pp.fullNames` prints the same text @@ -209,18 +319,66 @@ def statement (env : Environment) (thm : Name) (type : Expr) : IO String := catch _ => pure (toString type) return s!"{thm} : {text}" -/-- When `type` negates a contract predicate applied to arguments, that contract and the - source position of `thm`; otherwise `none`. -/ -def refutation (env : Environment) (thm : Name) (type : Expr) : IO (Option Json) := do - let .app (.const `Not _) claim := type | return none - let .const contract _ := claim.getAppFn | return none - unless contracts.contains contract do return none +/-- How many definitions `methodIndex?` unfolds to reach a numeral, as `bind_method` defines + one. -/ +def methodIndexDepth : Nat := 8 + +/-- The `Nat` that `e` denotes when it is a canonical numeral, a raw literal, `Nat.zero`, or a + definition without universe parameters whose value is one, unfolding at most `depth` + definitions. -/ +def methodIndex? (env : Environment) : Nat → Expr → Option Nat + | _, .lit (.natVal n) => some n + | _, .const `Nat.zero [] => some 0 + | _, e@(.app (.app (.app _ _) (.lit (.natVal n))) _) => + if e == canonicalNat n then some n else none + | depth + 1, .const name [] => match env.find? name with + | some (.defnInfo info) => methodIndex? env depth (stripMData info.value) + | _ => none + | _, _ => none + +/-- The constant `e` names, read through definitions without universe parameters whose value is + a bare constant, at most `depth` of them, so an alias of a program names that program. -/ +def programConstant? (env : Environment) : Nat → Expr → Option Name + | depth + 1, .const name [] => match env.find? name with + | some (.defnInfo info) => match stripMData info.value with + | target@(.const _ []) => programConstant? env depth target + | _ => some name + | _ => some name + | _, e => e.constName? + +/-- When `type` is a library contract's counterexample predicate applied to arguments, the + contract it refutes, the program and method it names, and the source position of `thm`; + otherwise `none`. The program is reported only as the constant `programConstant?` reads and + the method only as the number `methodIndex?` reads, so a caller can bind the refutation to + an extraction. A statement `Not` applied to a library contract's `Holds` is an error: a run + the model does not cover proves it, so it is no evidence of a counterexample. -/ +def refutation (env : Environment) (ownerOf : Name → Option Name) (thm : Name) (type : Expr) : + IO (Except String (Option Json)) := do + let type := contractClaim env.find? type + if let .app (.const `Not _) claim := type then + if let some contract := contractAt ownerOf (·.holds) claim then + return .error s!"{thm} states Not ({contract.holds} ...), which a run the model does not \ + cover can prove, so it is no counterexample; state {contract.refuted} instead" + let some contract := contractAt ownerOf (·.refuted) type | return .ok none let some ranges ← run env (findDeclarationRanges? thm) | throw (IO.userError s!"Missing refutation location for {thm}") - return some <| Json.mkObj [("contract", toJson contract.toString), + let arguments := type.getAppArgs + let program := arguments[3]?.bind (programConstant? env aliasDepth ∘ stripMData) + let method := arguments[4]?.bind (methodIndex? env methodIndexDepth ∘ stripMData) + return .ok <| some <| Json.mkObj [("contract", toJson contract.holds.toString), + ("program", toJson (program.map toString)), ("method", toJson method), ("line", toJson ranges.selectionRange.pos.line), ("column", toJson ranges.selectionRange.pos.column)] +/-- `holds`, a library contract's `Holds` applied to arguments, as that contract's counterexample + predicate applied to the same arguments. -/ +def refutedAt (ownerOf : Name → Option Name) (holds : Expr) : Except String Expr := + match contractAt ownerOf (·.holds) holds, holds.getAppFn with + | some contract, .const _ levels => + pure (mkAppN (.const contract.refuted levels) holds.getAppArgs) + | _, _ => throw "Production's statement is not a contract's Holds, so no refutation of it \ + can be compared" + /-- `n` as JSON: its components from the root, strings as strings and numbers as numbers. -/ def nameJson (n : Name) : Json := .arr (n.components.toArray.map fun @@ -410,7 +568,10 @@ def theorem? (env : Environment) (req : Json) if req.getObjValAs? Bool "statement" matches .ok true then fields := fields ++ [("statement", toJson (← statement env thm info.type))] if req.getObjValAs? Bool "refutation" matches .ok true then - if let some r ← refutation env thm type then fields := fields ++ [("refutes", r)] + match ← refutation env ownerOf thm type with + | .ok (some refutes) => fields := fields ++ [("refutes", refutes)] + | .ok none => pure () + | .error error => return result (fields ++ [("error", toJson error)]) if req.getObjValAs? Bool "term" matches .ok true then let term := (ownerOf thm).map (statementTerm kernel ownerOf · info.levelParams type) fields := fields ++ match term with @@ -418,14 +579,28 @@ def theorem? (env : Environment) (req : Json) | some (.error error) => [("termError", toJson error)] | none => [("termError", toJson s!"Theorem has no declaring module: {thm}")] if let .ok expected := req.getObjVal? "expected" then - let negated := (expected.getObjValAs? Bool "negated").toOption.getD false - let target := (expected.getObjVal? "term").bind - (decodeStatement kernel ownerOf info.levelParams) - fields := fields ++ match target.map (if negated then mkApp (.const ``Not []) else id) with - | .ok target => match acceptsAt kernel thm info.levelParams type target with - | none => [("matches", toJson true)] - | some error => [("matches", toJson false), ("mismatch", toJson error)] - | .error error => [("matches", toJson false), ("mismatch", toJson error)] + if let .obj members := expected then + for (key, _) in members do + unless key == "term" || key == "refuted" do + return result (fields ++ [("error", toJson s!"Unknown comparison field: {key}")]) + let .ok refuted := expected.getObjValAs? Bool "refuted" + | return result (fields ++ + [("error", toJson "A statement comparison states expected.refuted as a boolean")]) + let .ok term := expected.getObjVal? "term" + | return result (fields ++ + [("error", toJson "A statement comparison requires expected.term")]) + let target := decodeStatement kernel ownerOf info.levelParams term + match target.bind (if refuted then refutedAt ownerOf else pure) with + | .ok target => match acceptsAt kernel thm info.levelParams type target with + | none => + fields := fields ++ [("matches", toJson true)] + -- The kernel accepted the theorem at production's counterexample predicate, so it refutes + -- that predicate's subject, whatever form the theorem states it in. + if refuted then + if let .ok (some refutes) ← refutation env ownerOf thm target then + fields := fields.filter (·.1 != "refutes") ++ [("refutes", refutes)] + | some error => fields := fields ++ [("matches", toJson false), ("mismatch", toJson error)] + | .error error => fields := fields ++ [("matches", toJson false), ("mismatch", toJson error)] if let .ok spec := req.getObjVal? "type" then unless info.levelParams == (names req "levelParams").toList && (← IO.ofExcept (fits spec type)) do @@ -571,9 +746,7 @@ def needsTargetMetadata (env : Kernel.Environment) (req : Json) : Bool := Id.run if entry.getObjValAs? Bool "refutation" matches .ok true then let some thm := (entry.getObjValAs? String "name").toOption | return true if let some (.thmInfo info) := env.find? (name thm) then - if let .app (.const `Not _) claim := stripMData info.type then - if let .const contract _ := claim.getAppFn then - if contracts.contains contract then return true + if namesContract env.find? (stripMData info.type) then return true return false /-- Audits a supplied immutable environment and records the exact resolved module paths. -/ @@ -616,12 +789,17 @@ unsafe def main (args : List String) : IO UInt32 := do | ["--replay-separately", input] => let targets ← IO.ofExcept <| (Json.parse (← IO.FS.readFile input)).bind fromJson? return ← CIL.Replay.replayModulesSeparately targets + | ["--diagnose-replay", input] => + let targets ← IO.ofExcept <| (Json.parse (← IO.FS.readFile input)).bind fromJson? + return ← CIL.Replay.diagnoseModules targets | ["--headers", input] => CIL.Interface.writeSourceHeaders input; return 0 | ["--imports", input, output] => writeImports input output; return 0 | ["--fingerprints", input] => writeFingerprints input; return 0 | _ => pure () let [request, output] := args | throw (IO.userError "Usage: leancil-audit REQUEST RESULT") - let req ← IO.ofExcept (Json.parse (← IO.FS.readFile request)) + let source <- IO.FS.readFile request + IO.ofExcept (checkRequestKeys source) + let req <- IO.ofExcept (Json.parse source) let sysroot ← findSysroot -- Toolchain modules first: LEAN_PATH may add modules but never replace core ones. searchPathRef.set ((← getBuiltinSearchPath sysroot) ++ (← addSearchPathFromEnv {})) @@ -648,7 +826,9 @@ unsafe def main (args : List String) : IO UInt32 := do else none try writeAudit req output env [("replayed", toJson targets), ("fused", toJson fused)] - ownerOf closureOwnerOf replayedInterfaces + (if fused then ownerOf else fun name => + (env.getModuleIdxFor? name).bind (env.allImportedModuleNames[·.toNat]?)) + closureOwnerOf replayedInterfaces finally unless fused do env.freeRegions return 0 let env ← importModules (modules.map ({ module := · })) {} (loadExts := false) diff --git a/CIL/ProfileEquivalence.lean b/CIL/ProfileEquivalence.lean index 10837de83..068b1258f 100644 --- a/CIL/ProfileEquivalence.lean +++ b/CIL/ProfileEquivalence.lean @@ -1,12 +1,13 @@ import CIL.ExecutionLemmas +import CIL.Extraction.RuntimeModels /-! # Profile equivalence Shows when a method's result does not depend on the hardware it runs on. Two feature -profiles agree on a program when every feature test and intrinsic availability check in it -gives the same answer under both. Checked execution equivalence is proved in -`CIL.Safety.ProfileEquivalence`. +profiles agree on a program when every feature test and intrinsic availability check in it, +including those of the runtime models its calls run, gives the same answer under both. Checked +execution equivalence is proved in `CIL.Safety.ProfileEquivalence`. -/ namespace CIL @@ -16,52 +17,38 @@ layout, static data and return conventions remain exactly the same. -/ def reprofile (program : Program) (profile : FeatureProfile) : Program := program.map fun body => { body with profile := profile } -/-- `p` and `q` answer the profile query `op` makes, if any, the same way: a feature test or - an intrinsic's availability. -/ -def Op.ProfileAgreement (op : Op) (p q : FeatureProfile) : Prop := - match op with +/-- `p` and `q` answer the profile query `model` makes, if any, the same way: a feature test + or an intrinsic's availability. -/ +def RuntimeModel.ProfileAgreement (model : RuntimeModel) (p q : FeatureProfile) : Prop := + match model with | .feature featureId => p.evaluate featureId = q.evaluate featureId | .intrinsic operation _ => operation.available p = operation.available q | _ => True -/-- `p` and `q` agree on every instruction of every method. -/ -def Program.ProfileAgreement (program : Program) (p q : FeatureProfile) : Prop := - ∀ body ∈ program, ∀ op ∈ body.code, op.ProfileAgreement p q - -/-- An intrinsic step observes the profile only through the intrinsic's availability. -/ -theorem step_intrinsic (operation : Intrinsic) (argc : Nat) (returns : Bool) (pc : Nat) - (args : List Value) (frame : Nat) (stack : List Value) (memory : Memory) - (profile : FeatureProfile) : - step (.intrinsic operation argc) returns pc args frame stack memory profile = - if stack.length < argc || !operation.available profile then none else do - return .next (pc + 1) - ((← evalIntrinsic operation (stack.take argc).reverse) :: stack.drop argc) memory := - rfl +/-- Profiles agree on an instruction when the runtime model of its resolved call or constructor + has the same feature result or intrinsic availability in both profiles. -/ +def Instruction.ProfileAgreement (environment : LayoutEnvironment) (op : Instruction) + (p q : FeatureProfile) : Prop := + match op with + | .call method _ => ∀ model, Extraction.runtimeModel environment .call method = some model → + model.ProfileAgreement p q + | .newobj method => ∀ model, Extraction.runtimeModel environment .newobj method = some model → + model.ProfileAgreement p q + | _ => True -#print axioms step_intrinsic +/-- `p` and `q` agree on every instruction of every method, in that method's environment. -/ +def Program.ProfileAgreement (program : Program) (p q : FeatureProfile) : Prop := + ∀ body ∈ program, ∀ op ∈ body.code, op.ProfileAgreement body.environment p q /-- A step under agreeing profiles has the same outcome. -/ -theorem step_profile_eq (op : Op) (p q : FeatureProfile) - (h : op.ProfileAgreement p q) (returns : Bool) (pc : Nat) (args : List Value) - (frame : Nat) (stack : List Value) (memory : Memory) : - step op returns pc args frame stack memory p = - step op returns pc args frame stack memory q := by - cases op <;> try rfl - case feature id => - change p.evaluate id = q.evaluate id at h - rw [step_feature, step_feature, h] - case intrinsic operation argc => - change operation.available p = operation.available q at h - rw [step_intrinsic, step_intrinsic, h] - all_goals - cases stack with - | nil => rfl - | cons a rest => - first - | rfl - | cases rest with - | nil => first | rfl | (cases a <;> rfl) - | cons b rest => first | rfl | (cases a <;> first | rfl | (cases b <;> rfl)) +theorem step_profile_eq (environment : LayoutEnvironment) (op : Instruction) + (p q : FeatureProfile) (h : op.ProfileAgreement environment p q) (returns : Bool) + (narrow : Option NarrowKind) (pc : Nat) + (stack : List Value) (memory : Memory) : + step op returns narrow pc stack memory p = + step op returns narrow pc stack memory q := by + clear h + rfl @[simp] theorem reprofile_lookup (program : Program) (profile : FeatureProfile) (index : Nat) : (reprofile program profile)[index]? = @@ -122,15 +109,29 @@ theorem Intrinsic.available_of_required (operation : Intrinsic) (p : FeatureProf simp_all [Intrinsic.requiredFeatures, Intrinsic.available, FeatureProfile.evaluate] case avx512DQ operation => cases operation <;> simp_all -theorem Op.ProfileAgreement.symm (op : Op) (p q : FeatureProfile) - (h : op.ProfileAgreement p q) : op.ProfileAgreement q p := by +theorem RuntimeModel.ProfileAgreement.symm (model : RuntimeModel) (p q : FeatureProfile) + (h : model.ProfileAgreement p q) : model.ProfileAgreement q p := by + cases model <;> try trivial + all_goals exact Eq.symm h + +theorem RuntimeModel.ProfileAgreement.trans (model : RuntimeModel) (p q r : FeatureProfile) + (hpq : model.ProfileAgreement p q) (hqr : model.ProfileAgreement q r) : + model.ProfileAgreement p r := by + cases model <;> try trivial + all_goals exact Eq.trans hpq hqr + +theorem Instruction.ProfileAgreement.symm (environment : LayoutEnvironment) (op : Instruction) + (p q : FeatureProfile) (h : op.ProfileAgreement environment p q) : + op.ProfileAgreement environment q p := by cases op <;> try trivial + case call | newobj => exact fun model found => (h model found).symm all_goals exact Eq.symm h -theorem Op.ProfileAgreement.trans (op : Op) (p q r : FeatureProfile) - (hpq : op.ProfileAgreement p q) (hqr : op.ProfileAgreement q r) : - op.ProfileAgreement p r := by +theorem Instruction.ProfileAgreement.trans (environment : LayoutEnvironment) (op : Instruction) + (p q r : FeatureProfile) (hpq : op.ProfileAgreement environment p q) + (hqr : op.ProfileAgreement environment q r) : op.ProfileAgreement environment p r := by cases op <;> try trivial + case call | newobj => exact fun model found => (hpq model found).trans _ _ _ _ (hqr model found) all_goals exact Eq.trans hpq hqr /-- An intrinsic's availability is fixed by the features it needs: two profiles that agree on @@ -145,10 +146,20 @@ theorem Intrinsic.available_eq_of_required (operation : Intrinsic) (p q : Featur case avx512 operation => cases operation <;> simp_all case avx512DQ operation => cases operation <;> simp_all -/-- The profile flags `op` reads: the feature it tests, or every feature its intrinsic needs. -/ -def Op.profileReads : Op → List Feature +/-- The profile flags `model` reads: the feature it tests, or every feature its intrinsic + needs. -/ +def RuntimeModel.profileReads : RuntimeModel → List Feature | .feature featureId => [featureId] | .intrinsic operation _ => operation.requiredFeatures + | _ => [] + +/-- The profile flags `op` reads: the feature it tests, every feature its intrinsic needs, or + those of the runtime model a call runs, resolved in `environment`. -/ +def Instruction.profileReads (environment : LayoutEnvironment) : Instruction → List Feature + | .call method _ => + ((Extraction.runtimeModel environment .call method).map RuntimeModel.profileReads).getD [] + | .newobj method => + ((Extraction.runtimeModel environment .newobj method).map RuntimeModel.profileReads).getD [] -- A new profile-reading op breaks `step_profile_eq`; covering it in `ProfileAgreement` -- then breaks `ProfileAgreement.of_flags` until it is listed here. | _ => [] @@ -156,7 +167,16 @@ def Op.profileReads : Op → List Feature /-- Every profile flag an instruction of any method in the program reads, the same scope `Program.ProfileAgreement` quantifies over. -/ def Program.profileReads (program : Program) : List Feature := - program.flatMap fun body => body.code.flatMap Op.profileReads + program.flatMap fun body => body.code.flatMap (Instruction.profileReads body.environment) + +/-- Two profiles that agree on every flag a model reads agree on the model. -/ +theorem RuntimeModel.ProfileAgreement.of_flags (model : RuntimeModel) (p q : FeatureProfile) + (agree : ∀ featureId ∈ model.profileReads, p.evaluate featureId = q.evaluate featureId) : + model.ProfileAgreement p q := by + cases model with + | feature featureId => exact agree featureId (List.mem_singleton_self featureId) + | intrinsic operation _ => exact Intrinsic.available_eq_of_required operation p q agree + | _ => trivial /-- Two profiles that agree on every flag the program reads agree on the whole program. The hypothesis is decidable, so `decide` closes it for concrete profiles. -/ @@ -164,15 +184,19 @@ theorem Program.ProfileAgreement.of_flags (program : Program) (p q : FeatureProf (agree : ∀ featureId ∈ program.profileReads, p.evaluate featureId = q.evaluate featureId) : program.ProfileAgreement p q := by intro body member op instruction - have reads : ∀ featureId ∈ op.profileReads, p.evaluate featureId = q.evaluate featureId := + have reads : ∀ featureId ∈ op.profileReads body.environment, + p.evaluate featureId = q.evaluate featureId := fun featureId read => agree featureId (List.mem_flatMap.mpr ⟨body, member, List.mem_flatMap.mpr ⟨op, instruction, read⟩⟩) cases op with - | feature featureId => exact reads featureId (List.mem_singleton_self featureId) - | intrinsic operation _ => exact Intrinsic.available_eq_of_required operation p q reads + | call method _ | newobj method => + intro model found + exact RuntimeModel.ProfileAgreement.of_flags model p q fun featureId read => + reads featureId (by simp [Instruction.profileReads, found, read]) | _ => trivial #print axioms Intrinsic.available_eq_of_required #print axioms Program.ProfileAgreement.of_flags +#print axioms RuntimeModel.ProfileAgreement.of_flags end CIL diff --git a/CIL/Replay.lean b/CIL/Replay.lean index 533748a99..8d9858939 100644 --- a/CIL/Replay.lean +++ b/CIL/Replay.lean @@ -31,17 +31,52 @@ Each definition, theorem, opaque and axiom is added unchecked in dependency orde checked against the environment that preceded it, which is exactly what sequential replay checks. Inductives, the quotient and postponed constructor/recursor comparisons stay sequential. -/ +/-- Checking-boundary failures that can identify a rejected owned declaration. -/ +inductive RejectionKind where + | unknownConstant | alreadyDeclared | declTypeMismatch | declHasMVars | declHasFVars + | funExpected | typeExpected | letTypeMismatch | exprTypeMismatch | appTypeMismatch + | invalidProj | thmTypeIsNotProp | constructorMismatch | recursorMismatch + deriving BEq, Repr + +/-- Classifies kernel failures without interpreting diagnostic text. -/ +def rejectionKind : Lean.Kernel.Exception → Option RejectionKind + | .unknownConstant .. => some .unknownConstant + | .alreadyDeclared .. => some .alreadyDeclared + | .declTypeMismatch .. => some .declTypeMismatch + | .declHasMVars .. => some .declHasMVars + | .declHasFVars .. => some .declHasFVars + | .funExpected .. => some .funExpected + | .typeExpected .. => some .typeExpected + | .letTypeMismatch .. => some .letTypeMismatch + | .exprTypeMismatch .. => some .exprTypeMismatch + | .appTypeMismatch .. => some .appTypeMismatch + | .invalidProj .. => some .invalidProj + | .thmTypeIsNotProp .. => some .thmTypeIsNotProp + | .other .. | .deterministicTimeout | .excessiveMemory | .deepRecursion | .interrupted => none + +/-- A declaration rejected at a kernel checking boundary. -/ +structure ReplayRejection where + declaration : Lean.Name + kind : RejectionKind + +/-- Diagnostic replay never establishes verification success. -/ +inductive ReplayDiagnostic where + | accepted + | rejected (rejection : ReplayRejection) + | unavailable (message : String) + /-- Kernel verdict for one deferred declaration check. -/ inductive CheckOutcome where | accepted | cancelled - | rejected (message : String) + | rejected (message : String) (kind : Option RejectionKind) /-- Constants being replayed and the deferred checks issued so far. -/ structure ReplayContext where newConstants : Std.HashMap Lean.Name Lean.ConstantInfo cancel : IO.CancelToken checks : IO.Ref (Array (Lean.Name × Task (Except IO.Error CheckOutcome))) + rejection : IO.Ref (Option ReplayRejection) /-- The environment built so far and the names still to replay. -/ structure ReplayState where @@ -73,7 +108,7 @@ Written inline in `IO.asTask`, the check would run eagerly on the calling thread | .error .interrupted => return .cancelled | .error ex => cancel.set - return .rejected (← kernelMessage ex) + return .rejected (← kernelMessage ex) (rejectionKind ex) /-- Adds a declaration; a deferred check sees only the declarations added before it. -/ def addDecl (decl : Lean.Declaration) : ReplayM Unit := do @@ -91,7 +126,11 @@ def addDecl (decl : Lean.Declaration) : ReplayM Unit := do | _ => match before.addDeclCore 0 0 decl (cancelTk? := none) with | .ok env => modify fun s => { s with env } - | .error ex => throw <| .userError (← kernelMessage ex) + | .error ex => + let message ← kernelMessage ex + if let some kind := rejectionKind ex then + (← read).rejection.set (some ⟨decl.getNames.headD .anonymous, kind⟩) + throw <| .userError message /-- The constant `name` among those being replayed; a name the replay reaches but the replayed constants lack is an error naming it. -/ @@ -160,43 +199,61 @@ def checkPostponed : ReplayM Unit := do for ctor in (← get).postponedConstructors do match (← get).env.find? ctor, (← read).newConstants[ctor]? with | some (.ctorInfo info), some (.ctorInfo info') => - if !(info == info') then throw <| .userError s!"Invalid constructor {ctor}" + if !(info == info') then + (← read).rejection.set (some ⟨ctor, .constructorMismatch⟩) + throw <| .userError s!"Invalid constructor {ctor}" | _, _ => throw <| .userError s!"No such constructor {ctor}" for recursor in (← get).postponedRecursors do match (← get).env.find? recursor, (← read).newConstants[recursor]? with | some (.recInfo info), some (.recInfo info') => - if !(info == info') then throw <| .userError s!"Invalid recursor {recursor}" + if !(info == info') then + (← read).rejection.set (some ⟨recursor, .recursorMismatch⟩) + throw <| .userError s!"Invalid recursor {recursor}" | _, _ => throw <| .userError s!"No such recursor {recursor}" +/-- Joins every deferred check and retains only the first terminal failure's classification. -/ +def awaitChecks (checks : IO.Ref (Array (Lean.Name × Task (Except IO.Error CheckOutcome)))) + (rejection : IO.Ref (Option ReplayRejection)) : IO (Option String) := do + let mut failure := none + for (name, check) in ← checks.get do + match ← IO.wait check with + | .ok (.rejected message kind) => + if failure.isNone then rejection.set (kind.map fun kind => ⟨name, kind⟩) + failure := failure <|> some s!"while replaying declaration '{name}':\n{message}" + | .ok _ => pure () + | .error ex => + if failure.isNone then rejection.set none + failure := failure <|> some s!"while replaying declaration '{name}':\n{ex}" + return failure + /-- Kernel-checks every safe, total constant into `env`, accepting exactly what sequential replay accepts. Every deferred check has finished when this returns or throws, so imported regions may be freed. -/ def replay (newConstants : Std.HashMap Lean.Name Lean.ConstantInfo) - (env : Lean.Kernel.Environment) : IO Lean.Kernel.Environment := do + (env : Lean.Kernel.Environment) + (diagnostic : Option (IO.Ref (Option ReplayRejection)) := none) : + IO Lean.Kernel.Environment := do let remaining := newConstants.fold (init := {}) fun remaining name ci => if !ci.isUnsafe && !ci.isPartial then remaining.insert name else remaining let cancel ← IO.CancelToken.new let checks ← IO.mkRef #[] - let awaitChecks : IO (Option String) := do - let mut failure := none - for (name, check) in ← checks.get do - match ← IO.wait check with - | .ok (.rejected message) => - failure := failure <|> some s!"while replaying declaration '{name}':\n{message}" - | .ok _ => pure () - | .error ex => failure := failure <|> some s!"while replaying declaration '{name}':\n{ex}" - return failure + let rejection ← IO.mkRef none + let awaitChecks := awaitChecks checks rejection let replayed ← try let (_, s) ← (do for n in remaining do replayConstant n - checkPostponed : ReplayM Unit).run { newConstants, cancel, checks } + checkPostponed : ReplayM Unit).run { newConstants, cancel, checks, rejection } |>.run { env, remaining } pure s.env catch ex => cancel.set + let sequential ← rejection.get discard awaitChecks + if let some diagnostic := diagnostic then diagnostic.set sequential throw ex - if let some failure ← awaitChecks then throw <| .userError failure + if let some failure ← awaitChecks then + if let some diagnostic := diagnostic then diagnostic.set (← rejection.get) + throw <| .userError failure return replayed /-- Replays every target and exposes the checked environment while its imported regions live. @@ -304,4 +361,97 @@ unsafe def replayModulesSeparately (args : List String) : IO UInt32 := do failed := true return if failed then 1 else 0 +/-- Stable protocol spellings for checking-boundary rejection kinds. -/ +def RejectionKind.protocolName : RejectionKind → String + | .unknownConstant => "unknownConstant" + | .alreadyDeclared => "alreadyDeclared" + | .declTypeMismatch => "declTypeMismatch" + | .declHasMVars => "declHasMVars" + | .declHasFVars => "declHasFVars" + | .funExpected => "funExpected" + | .typeExpected => "typeExpected" + | .letTypeMismatch => "letTypeMismatch" + | .exprTypeMismatch => "exprTypeMismatch" + | .appTypeMismatch => "appTypeMismatch" + | .invalidProj => "invalidProj" + | .thmTypeIsNotProp => "thmTypeIsNotProp" + | .constructorMismatch => "constructorMismatch" + | .recursorMismatch => "recursorMismatch" + +/-- Requires every complete declaration and mutual member to belong uniquely to this module. -/ +unsafe def diagnosticConstants (complete : Lean.ModuleData) (env : Lean.Environment) : + IO (Std.HashMap Lean.Name Lean.ConstantInfo) := do + unless complete.constNames.size == complete.constants.size do + throw <| IO.userError "Mismatched declaration inventory" + let mut constants : Std.HashMap Lean.Name Lean.ConstantInfo := {} + for name in complete.constNames, info in complete.constants do + if name.isAnonymous || name != info.name || constants.contains name || + (env.find? name).isSome then + throw <| IO.userError s!"Ambiguous declaration ownership: {name}" + constants := constants.insert name info + let mut extra : Std.HashSet Lean.Name := {} + for name in complete.extraConstNames do + if extra.contains name || (env.find? name).isSome then + throw <| IO.userError s!"Ambiguous auxiliary ownership: {name}" + extra := extra.insert name + -- Shared physical tails retain their validated ownership for this module inventory. + let mut checked : Lean.PtrSet (List Lean.Name) := Lean.mkPtrSet + for (_, info) in constants do + let mut members := info.all + while !members.isEmpty do + if checked.contains members then break + let member := members.head! + unless constants.contains member do + throw <| IO.userError s!"Unowned mutual declaration: {member}" + checked := checked.insert members + members := members.tail! + if let .inductInfo inductiveInfo := info then + for ctor in inductiveInfo.ctors do + unless constants.contains ctor do + throw <| IO.userError s!"Unowned constructor: {ctor}" + return constants + +/-- Diagnoses one module against precisely its exported imports and complete declaration part. -/ +unsafe def diagnoseModule (name : String) : IO ReplayDiagnostic := do + let operation : IO ReplayDiagnostic := do + let (exported, complete) ← readReplayParts name.toName + let (_, state) ← Lean.importModulesCore exported.imports |>.run + let env ← Lean.finalizeImport state exported.imports {} 0 false false (isModule := true) + let check : IO ReplayDiagnostic := do + let constants ← diagnosticConstants complete env + let rejection ← IO.mkRef none + try + discard <| replay constants env.toKernelEnv (some rejection) + return .accepted + catch error => + if let some rejected ← rejection.get then + if constants.contains rejected.declaration then return .rejected rejected + return .unavailable (toString error) + try check + finally env.freeRegions + try operation + catch error => return .unavailable (toString error) + +/-- Emits one strict diagnostic record for each unique target and a terminal completion record. -/ +unsafe def diagnoseModules (targets : List String) : IO UInt32 := do + Lean.initSearchPath (← Lean.findSysroot) + let mut seen : Std.HashSet String := {} + for target in targets do + if target.isEmpty || seen.contains target then + throw <| IO.userError "Diagnostic targets must be unique nonempty names" + seen := seen.insert target + for target in targets do + let outcome ← diagnoseModule target + let fields := [("module", Lean.toJson target)] + let result := match outcome with + | .accepted => [("outcome", Lean.toJson "accepted")] + | .unavailable message => [("outcome", Lean.toJson "unavailable"), + ("error", Lean.toJson message)] + | .rejected rejection => [("outcome", Lean.toJson "rejected"), + ("declaration", Lean.toJson rejection.declaration.toString), + ("kind", Lean.toJson rejection.kind.protocolName)] + IO.println (Lean.Json.mkObj (fields ++ result)).compress + IO.println (Lean.Json.mkObj [("complete", Lean.toJson true)]).compress + return 0 + end CIL.Replay diff --git a/CIL/Resolution.lean b/CIL/Resolution.lean new file mode 100644 index 000000000..cdadd114d --- /dev/null +++ b/CIL/Resolution.lean @@ -0,0 +1,1024 @@ +import CIL.Extraction.TypeStorage +import CIL.Extraction.LinkIndex +import CIL.Identity + +/-! +# Narrowing from declared types + +`declaredNarrow` is the small-integer narrowing a value of a declared metadata type takes: +the storage of a primitive or of an enum's underlying type. `CIL.Method.returnNarrow` applies it +to a method's declared return type. The lemmas here state the narrowing of every primitive, of +every enum over a primitive and of every other type head, so a generated fact names a method's +narrowing by lemma application and no generated proof evaluates a type name. + +`CIL.Method.argumentAccess` says how a method reads each argument: from the frame slot of its +parameter home, which follows the declared locals, or as the incoming value narrowed as its +declared type requires. Under `ParameterHomesValid` the home slot is the one the argument check +expects. The key lemmas over ascending homes let a generated fact name an argument's home +position without searching the homes before it. + +`CIL.Method.elementShape` says how an array of a declared element type holds its elements: inline +values of a primitive, an enum, a vector or a struct of the size its checked layout gives, class +references, or method-table entries of one managed signature; a by-ref-like struct has no shape. +The primitive, enum and class lemmas state a shape without evaluating a type name, and the struct +lemmas take the layout entries as premises, so a generated fact reads them through named suffixes. + +`CIL.LayoutEnvironment.fieldShape` says how `ldfld`, `stfld` and `ldflda` of a field operand +reach memory: a value-type field through its checked layout entry, a class instance field through +its model slot. `CIL.LayoutEnvironment.valueShape` says the same of a type operand of `ldobj`, +`stobj`, `initobj`, `cpobj` and `sizeof`. Both read only the layouts every method of an +extraction shares, so one fact per distinct operand serves every method. +-/ + +namespace CIL + +/-- The type expression the extractor writes for a primitive: its full name in the runtime + scope. -/ +def PrimitiveType.typeExpr (type : PrimitiveType) : Extraction.TypeExpr := + .atom Extraction.runtimeScope type.display + +/-- The narrowing a value of the primitive takes; `none` for every type wider than 16 bits. -/ +def PrimitiveType.narrow : PrimitiveType → Option NarrowKind + | .sbyte => some .signed8 + | .byte | .boolean => some .unsigned8 + | .int16 => some .signed16 + | .uint16 | .char => some .unsigned16 + | .int32 | .uint32 | .int64 | .uint64 | .single | .double | .intPtr | .uintPtr => none + +/-- Whether `typeStorage` admits the primitive as an enum's underlying type. -/ +def PrimitiveType.enumUnderlying : PrimitiveType → Bool + | .sbyte | .byte | .int16 | .uint16 | .int32 | .uint32 | .int64 | .uint64 => true + | .boolean | .char | .single | .double | .intPtr | .uintPtr => false + +namespace Extraction + +/-- Whether `declaredNarrow` can be `some` for the type's head: an atom or an enum. -/ +def TypeExpr.narrowCandidate : TypeExpr → Bool + | .atom .. | .enum .. => true + | _ => false + +/-- The narrowing a value of declared type `type` takes: the small-integer storage of a primitive + or of an enum's underlying type, and `none` for every other type. Any other head is `none` + without decoding its storage, so evaluating it never compares a struct identity. -/ +def declaredNarrow (layouts : List RawLayout) (type : TypeExpr) : Option NarrowKind := + if type.narrowCandidate then (typeStorage layouts type).bind StorageType.narrow else none + +/-- A primitive narrows as its own kind, whatever the layout table. -/ +theorem declaredNarrow_primitive (layouts : List RawLayout) (type : PrimitiveType) : + declaredNarrow layouts type.typeExpr = type.narrow := by + cases type <;> rfl + +#print axioms declaredNarrow_primitive + +/-- An enum narrows as its underlying primitive when `typeStorage` admits that primitive as an + enum's underlying type, and not at all otherwise. -/ +theorem declaredNarrow_enum (layouts : List RawLayout) (identity : String) + (type : PrimitiveType) : + declaredNarrow layouts (.enum identity type.typeExpr) = + if type.enumUnderlying then type.narrow else none := by + cases type <;> rfl + +#print axioms declaredNarrow_enum + +/-- The narrowing of a storage that is never a small-integer field is `none`. -/ +theorem storage_narrow_of_wide {storage : Option StorageType} + (wide : ∀ field, storage = some (.field field) → ∃ index, field = .aggregate index) : + storage.bind StorageType.narrow = none := by + cases storage with + | none => rfl + | some storage => + cases storage with + | field field => + obtain ⟨index, rfl⟩ := wide field rfl + rfl + | _ => rfl + +#print axioms storage_narrow_of_wide + +/-- Span metadata is never stored as a field. -/ +theorem spanStorage_ne_field (type : TypeExpr) (width : Nat) (field : FieldType) : + spanStorage type width ≠ some (.field field) := by + unfold spanStorage + split + · intro stored + revert stored + split + · simp + · simp [Option.bind_eq_bind, Option.bind_eq_some_iff] + · simp + +#print axioms spanStorage_ne_field + +/-- Every head other than an atom or an enum decodes to a struct field, a reference, a + vector, a span, an array, a callable or an object, none of which narrows. -/ +theorem typeStorage_narrow_of_not_candidate (layouts : List RawLayout) (type : TypeExpr) + (shape : type.narrowCandidate = false) : + (typeStorage layouts type).bind StorageType.narrow = none := by + apply storage_narrow_of_wide + intro field stored + cases type with + | atom => simp [TypeExpr.narrowCandidate] at shape + | enum => simp [TypeExpr.narrowCandidate] at shape + | aggregate identity index => + simp only [typeStorage, typeStorageAt] at stored + revert stored + simp only [Option.bind_eq_bind, Option.bind_eq_some_iff] + rintro ⟨metadata, -, chosen⟩ + split at chosen + · simp only [pure, Option.some.injEq, StorageType.field.injEq] at chosen + exact ⟨index, chosen.symm⟩ + · cases chosen + | array element => + cases element <;> simp only [typeStorage, typeStorageAt] at stored <;> revert stored <;> + (repeat' split) <;> + simp_all [Option.map_eq_map, Option.bind_eq_bind, Option.bind_eq_some_iff] + | pointer element => + cases element <;> simp only [typeStorage, typeStorageAt] at stored <;> revert stored <;> + (repeat' split) <;> + simp_all [Option.bind_eq_bind, Option.bind_eq_some_iff] + | apply constructor argument => + cases constructor <;> simp only [typeStorage, typeStorageAt] at stored <;> revert stored <;> + (repeat' split) <;> simp_all [Option.bind_eq_bind, Option.bind_eq_some_iff] + -- A span: its storage is the one `spanStorage` gives. + rintro rfl + exact absurd ‹spanStorage _ _ = _› (spanStorage_ne_field _ _ _) + | _ => simp [typeStorage, typeStorageAt] at stored + +#print axioms typeStorage_narrow_of_not_candidate + +/-- `declaredNarrow` is the narrowing of the type's decoded storage, for every type. -/ +theorem declaredNarrow_eq_storage (layouts : List RawLayout) (type : TypeExpr) : + declaredNarrow layouts type = (typeStorage layouts type).bind StorageType.narrow := by + unfold declaredNarrow + split + · rfl + · next other => + exact (typeStorage_narrow_of_not_candidate layouts type (by simpa using other)).symm + +#print axioms declaredNarrow_eq_storage + +/-- A type whose storage is never a small-integer field does not narrow. -/ +theorem declaredNarrow_of_storage {layouts : List RawLayout} {type : TypeExpr} + (wide : ∀ field, typeStorage layouts type = some (.field field) → + ∃ index, field = .aggregate index) : + declaredNarrow layouts type = none := by + rw [declaredNarrow_eq_storage] + exact storage_narrow_of_wide wide + +#print axioms declaredNarrow_of_storage + +/-- Only an atom or an enum narrows; any other head is `none` without decoding its storage. -/ +theorem declaredNarrow_of_not_candidate (layouts : List RawLayout) (type : TypeExpr) + (shape : type.narrowCandidate = false) : declaredNarrow layouts type = none := by + simp [declaredNarrow, shape] + +#print axioms declaredNarrow_of_not_candidate + +/-- An enum cannot introduce narrowing when its underlying storage does not narrow. -/ +theorem declaredNarrow_enum_of_none (layouts : List RawLayout) (identity : String) + (type : TypeExpr) (narrow : declaredNarrow layouts type = none) : + declaredNarrow layouts (.enum identity type) = none := by + cases type <;> try rfl + case atom scope name => + by_cases runtime : scope = runtimeScope + . subst scope + by_cases longName : name.utf8ByteSize > "System.UInt64".utf8ByteSize + . simp [declaredNarrow, TypeExpr.narrowCandidate, typeStorage, typeStorageAt, + enumUnderlyingField, longName] + . cases stored : primitiveField name with + | none => simp [declaredNarrow, TypeExpr.narrowCandidate, typeStorage, typeStorageAt, + enumUnderlyingField, longName, stored] + | some kind => + cases kind <;> simp_all [declaredNarrow, TypeExpr.narrowCandidate, typeStorage, + typeStorageAt, enumUnderlyingField, StorageType.narrow] + . simp [declaredNarrow, TypeExpr.narrowCandidate, typeStorage, typeStorageAt, + enumUnderlyingField, runtime] + +#print axioms declaredNarrow_enum_of_none + +end Extraction + +/-- The narrowing `ret` applies: the declared return type's small-integer storage, and `none` + for a method that returns nothing. -/ +def Method.returnNarrow (body : Method) : Option NarrowKind := + if body.returnsValue then + Extraction.declaredNarrow body.environment.rawLayouts body.declared.returnType + else none + +/-- A method that returns nothing does not narrow. -/ +theorem Method.returnNarrow_of_void {body : Method} (returns : body.returnsValue = false) : + body.returnNarrow = none := by + simp [Method.returnNarrow, returns] + +#print axioms Method.returnNarrow_of_void + +/-- A method that returns a value narrows as its declared return type. -/ +theorem Method.returnNarrow_of_declared {body : Method} {type : Extraction.TypeExpr} + {result : Option NarrowKind} (returns : body.returnsValue = true) + (declared : body.declared.returnType = type) + (resolved : Extraction.declaredNarrow body.environment.rawLayouts type = result) : + body.returnNarrow = result := by + simp [Method.returnNarrow, returns, declared, resolved] + +#print axioms Method.returnNarrow_of_declared + +/-- A narrowed method returns a value. -/ +theorem Method.returnsValue_of_returnNarrow {body : Method} {kind : NarrowKind} + (narrowed : body.returnNarrow = some kind) : body.returnsValue = true := by + unfold Method.returnNarrow at narrowed + split at narrowed <;> simp_all + +#print axioms Method.returnsValue_of_returnNarrow + +/-! ## Argument access -/ + +namespace Extraction + +/-- Below an ascending table, every entry has a larger key than `key`, so none has `key`. -/ +theorem findIdx?_none_of_keysAscend {α : Type} {entries : List (Nat × α)} {lower key : Nat} + (ascending : keysAscend lower entries = true) (below : key < lower) : + entries.findIdx? (fun entry => entry.1 == key) = none := by + rw [List.findIdx?_eq_none_iff] + intro entry member + have := keysAscend_bound entries lower ascending entry member + simp only [beq_eq_false_iff_ne, ne_eq] + omega + +#print axioms findIdx?_none_of_keysAscend + +/-- The entry at `position` of an ascending table is the first with its key. -/ +theorem findIdx?_of_keysAscend {α : Type} {entries rest : List (Nat × α)} + {lower position key : Nat} {value : α} (ascending : keysAscend lower entries = true) + (placed : entries.drop position = (key, value) :: rest) : + entries.findIdx? (fun entry => entry.1 == key) = some position := by + induction entries generalizing lower position with + | nil => simp at placed + | cons head tail ih => + obtain ⟨first, other⟩ := head + simp only [keysAscend, Bool.and_eq_true, decide_eq_true_eq] at ascending + cases position with + | zero => + simp only [List.drop_zero, List.cons.injEq, Prod.mk.injEq] at placed + obtain ⟨⟨rfl, -⟩, -⟩ := placed + -- `simp` closes this through Nat's LawfulBEq instance, which depends on Classical.choice. + rw [List.findIdx?_cons, show ∀ same : Nat, (same == same) = true from Nat.beq_refl] + rfl + | succ position => + simp only [List.drop_succ_cons] at placed + have later := keysAscend_bound tail (first + 1) ascending.2 (key, value) + (List.mem_of_mem_drop (placed ▸ List.mem_cons_self)) + rw [List.findIdx?_cons, ih ascending.2 placed] + simp only [beq_iff_eq] + split + · omega + · rfl + +#print axioms findIdx?_of_keysAscend + +/-- The entry at `position` of an ascending table is the one `find?` returns for its key. -/ +theorem find?_of_keysAscend {α : Type} {entries rest : List (Nat × α)} + {lower position key : Nat} {value : α} (ascending : keysAscend lower entries = true) + (placed : entries.drop position = (key, value) :: rest) : + entries.find? (fun entry => entry.1 == key) = some (key, value) := by + induction entries generalizing lower position with + | nil => rw [List.drop_nil] at placed; cases placed + | cons head tail ih => + obtain ⟨first, other⟩ := head + simp only [keysAscend, Bool.and_eq_true, decide_eq_true_eq] at ascending + cases position with + | zero => + simp only [List.drop_zero, List.cons.injEq, Prod.mk.injEq] at placed + obtain ⟨⟨rfl, rfl⟩, -⟩ := placed + exact List.find?_cons_of_pos (decide_eq_true rfl) + | succ position => + simp only [List.drop_succ_cons] at placed + have later := keysAscend_bound tail (first + 1) ascending.2 (key, value) + (List.mem_of_mem_drop (placed ▸ List.mem_cons_self)) + exact (List.find?_cons_of_neg (p := fun (entry : Nat × α) => entry.1 == key) + fun found => Nat.ne_of_lt later (of_decide_eq_true found)).trans (ih ascending.2 placed) + +#print axioms find?_of_keysAscend + +/-- In a site table with ascending pcs, the site at `position` is the descriptor + `Method.staticAt` resolves for its pc. -/ +theorem _root_.CIL.Method.staticAt_of_keysAscend {body : CIL.Method} + {rest : List (Nat × CIL.StaticDescriptor)} {position pc : Nat} + {descriptor : CIL.StaticDescriptor} + (ascending : keysAscend 0 body.staticSites = true) + (placed : body.staticSites.drop position = (pc, descriptor) :: rest) : + body.staticAt pc = some descriptor := by + simp only [CIL.Method.staticAt, find?_of_keysAscend ascending placed, Option.map_some] + +#print axioms CIL.Method.staticAt_of_keysAscend + +/-- No entry of an ascending table has a key strictly between the key at `position` and the + next one. -/ +theorem findIdx?_none_after {α : Type} {entries rest : List (Nat × α)} + {lower position previous key : Nat} {value : α} + (ascending : keysAscend lower entries = true) + (placed : entries.drop position = (previous, value) :: rest) (below : previous < key) + (above : (rest.head?.all fun entry => decide (key < entry.1)) = true) : + entries.findIdx? (fun entry => entry.1 == key) = none := by + induction entries generalizing lower position with + | nil => simp at placed + | cons head tail ih => + obtain ⟨first, other⟩ := head + simp only [keysAscend, Bool.and_eq_true, decide_eq_true_eq] at ascending + cases position with + | zero => + simp only [List.drop_zero, List.cons.injEq, Prod.mk.injEq] at placed + obtain ⟨⟨rfl, -⟩, rfl⟩ := placed + have beyond := keysAscend_raise (higher := key + 1) ascending.2 (by + cases tail with + | nil => rfl + | cons next _ => + simp only [List.head?_cons, Option.all_some, decide_eq_true_eq] at above ⊢ + omega) + rw [List.findIdx?_cons, findIdx?_none_of_keysAscend beyond (Nat.lt_succ_self key)] + simp only [beq_iff_eq] + split + · omega + · rfl + | succ position => + simp only [List.drop_succ_cons] at placed + have later := keysAscend_bound tail (first + 1) ascending.2 (previous, value) + (List.mem_of_mem_drop (placed ▸ List.mem_cons_self)) + rw [List.findIdx?_cons, ih ascending.2 placed] + simp only [beq_iff_eq] + split + · omega + · rfl + +#print axioms findIdx?_none_after + +end Extraction + +/-- How a method reads argument `index`: the incoming value narrowed as its declared type + requires, or the frame slot of the argument's parameter home. -/ +inductive ArgumentAccess where + | value (narrow : Option NarrowKind) + | home (slot : Nat) + deriving DecidableEq, Repr + +/-- Homes follow the declared local kinds in `parameterHomes` order; an argument without a home + is read from the incoming arguments, narrowed by its declared type (none for `this`). -/ +def Method.argumentAccess (body : Method) (index : Nat) : ArgumentAccess := + match body.parameterHomes.findIdx? (fun home => home.1 == index) with + | some position => .home (body.localKinds.length + position) + | none => .value ((body.declared.argumentType index).bind + (Extraction.declaredNarrow body.environment.rawLayouts)) + +/-- An argument without a home is read narrowed as its declared type requires. -/ +theorem Method.argumentAccess_value {body : Method} {index : Nat} {type : Extraction.TypeExpr} + {narrow : Option NarrowKind} + (unhomed : body.parameterHomes.findIdx? (fun home => home.1 == index) = none) + (declared : body.declared.argumentType index = some type) + (narrowed : Extraction.declaredNarrow body.environment.rawLayouts type = narrow) : + body.argumentAccess index = .value narrow := by + simp [Method.argumentAccess, unhomed, declared, narrowed] + +#print axioms Method.argumentAccess_value + +/-- An argument without a home or a declared type, such as `this`, is read as it is. -/ +theorem Method.argumentAccess_undeclared {body : Method} {index : Nat} + (unhomed : body.parameterHomes.findIdx? (fun home => home.1 == index) = none) + (declared : body.declared.argumentType index = none) : + body.argumentAccess index = .value none := by + simp [Method.argumentAccess, unhomed, declared] + +#print axioms Method.argumentAccess_undeclared + +/-- A method with no parameter homes and no declared receiver or parameters reads every argument + as it is; one proof serves every index. -/ +theorem Method.argumentAccess_plain {body : Method} (index : Nat) + (homes : body.parameterHomes = []) (parameters : body.declared.parameters = []) + (receiver : body.declared.receiver = none) : body.argumentAccess index = .value none := by + simp [Method.argumentAccess, homes, DeclaredSignature.argumentType, parameters, receiver] + +#print axioms Method.argumentAccess_plain + +/-- A homed argument is read from its home's slot, after the declared locals. -/ +theorem Method.argumentAccess_home {body : Method} {index position slot : Nat} + (homed : body.parameterHomes.findIdx? (fun home => home.1 == index) = some position) + (placed : body.localKinds.length + position = slot) : + body.argumentAccess index = .home slot := by + simp [Method.argumentAccess, homed, placed] + +#print axioms Method.argumentAccess_home + +/-- Two methods with the same homes, local count, signature and layouts read every argument + alike. -/ +theorem Method.argumentAccess_congr {expected actual : Method} + (homes : actual.parameterHomes = expected.parameterHomes) + (localCount : actual.localKinds.length = expected.localKinds.length) + (declared : actual.declared = expected.declared) + (environment : actual.environment = expected.environment) : + actual.argumentAccess = expected.argumentAccess := by + funext index + simp only [Method.argumentAccess, homes, localCount, declared, environment] + +#print axioms Method.argumentAccess_congr + +/-- Parameter `index` of a static method is argument `index`. -/ +theorem DeclaredSignature.argumentType_static {signature : DeclaredSignature} {index : Nat} + {type : Extraction.TypeExpr} (static : signature.receiver = none) + (found : signature.parameters[index]? = some type) : + signature.argumentType index = some type := by + simp [DeclaredSignature.argumentType, static, found] + +#print axioms DeclaredSignature.argumentType_static + +/-- Parameter `index` of an instance method is argument `index + 1`. -/ +theorem DeclaredSignature.argumentType_instance {signature : DeclaredSignature} {index : Nat} + {receiver : Receiver} {type : Extraction.TypeExpr} + (instanceMethod : signature.receiver = some receiver) + (found : signature.parameters[index]? = some type) : + signature.argumentType (index + 1) = some type := by + simp [DeclaredSignature.argumentType, instanceMethod, found] + +#print axioms DeclaredSignature.argumentType_instance + +/-- Argument 0 of an instance method, `this`, has no declared parameter type. -/ +theorem DeclaredSignature.argumentType_this {signature : DeclaredSignature} {receiver : Receiver} + (instanceMethod : signature.receiver = some receiver) : signature.argumentType 0 = none := by + simp [DeclaredSignature.argumentType, instanceMethod] + +#print axioms DeclaredSignature.argumentType_this + +namespace Extraction + +/-- A list an option-valued function maps entirely has as many results as inputs. -/ +private theorem length_of_mapM {α β : Type} {inputs : List α} {outputs : List β} + {make : α → Option β} (made : inputs.mapM make = some outputs) : + outputs.length = inputs.length := by + induction inputs generalizing outputs with + | nil => simp_all + | cons input rest ih => + simp only [List.mapM_cons, Option.bind_eq_bind, Option.bind_eq_some_iff, + Option.pure_def, Option.some.injEq] at made + obtain ⟨_, -, others, remaining, rfl⟩ := made + simp [ih remaining] + +#print axioms length_of_mapM + +/-- Searching the expected homes for an argument searches the required home indices. -/ +theorem homeKeys_findIdx? {layouts : Array ComputedLayout} {raw : RawArguments} + {reachable : List Bool} {homes : List (Nat × LocalKind)} {index : Nat} + (expected : expectedHomes layouts raw reachable = some homes) : + homes.findIdx? (fun home => home.1 == index) = + (requiredHomeIndices raw reachable).findIdx? (· == index) := by + unfold expectedHomes at expected + generalize requiredHomeIndices raw reachable = indices at expected ⊢ + induction indices generalizing homes with + | nil => simp_all + | cons key rest ih => + simp only [List.mapM_cons, Option.bind_eq_some_iff, bind, pure, Option.some.injEq] + at expected + obtain ⟨entry, made, others, remaining, rfl⟩ := expected + obtain ⟨_, -, _, -, rfl⟩ := made + simp [List.findIdx?_cons, ih remaining] + +#print axioms homeKeys_findIdx? + +/-- Under `ParameterHomesValid`, a homed argument is read from the slot the argument check + expects of it: the raw locals' count plus its position among the required homes. -/ +theorem argumentAccess_of_homed {layouts : Array ComputedLayout} {raw : RawArguments} + {reachable : List Bool} {method : Method} {index : Nat} + (valid : ParameterHomesValid layouts raw reachable method) + (homed : index ∈ requiredHomeIndices raw reachable) : + method.argumentAccess index = + .home (raw.locals.length + (requiredHomeIndices raw reachable).idxOf index) := by + obtain ⟨-, -, kinds, -, homes, -⟩ := valid + apply Method.argumentAccess_home (position := (requiredHomeIndices raw reachable).idxOf index) + · rw [homeKeys_findIdx? homes] + cases found : (requiredHomeIndices raw reachable).findIdx? (· == index) with + | none => + rw [List.findIdx?_eq_none_iff] at found + simp at found + exact absurd rfl (found index homed) + | some position => + show some position = some (List.findIdx (· == index) (requiredHomeIndices raw reachable)) + rw [List.findIdx_eq_getD_findIdx?, found] + rfl + · rw [length_of_mapM kinds] + +#print axioms argumentAccess_of_homed + +end Extraction + +/-! ## Array element shapes -/ + +/-- How an array of one element type holds its elements: reference-free values of an exact + element identity and width, class references, or method-table entries of one managed + signature. Every other element type is `unsupported`, with the reason. -/ +inductive ElementShape where + /-- Inline values: a primitive, an enum over its underlying integer type, an integer vector or + a struct of the size its checked layout gives. -/ + | value (element : ArrayElement) + /-- References to instances of the class `element`. -/ + | reference (element : Extraction.TypeExpr) + /-- Method-table entries: managed method pointers of `signature`. -/ + | method (signature : ManagedSignature) + /-- An element type no array of the model holds. -/ + | unsupported (reason : String) + deriving DecidableEq, Repr + +namespace ElementShape + +/-- Why a struct element without an entry in the checked layouts has no shape. -/ +def outsideLayouts : String := "Array element outside the checked layouts" + +/-- Why a by-ref-like struct element has no shape: no heap array holds one. -/ +def byRefLikeElement : String := "By-ref-like array element" + +/-- Why any other element type without array storage has no shape. -/ +def unsupportedType : String := "Array element type" + +/-- The full name of the root class, whose arrays have no shape. -/ +def rootObjectName : String := "System.Object" + +/-- Why an array of `System.Object` has no shape: its elements may be interface or value-type + instances, whose covariance the model cannot decide. -/ +def rootObjectElement : String := "System.Object array element" + +/-- Why `newarr` allocates no method-table array: the model builds tables only from bindings. -/ +def methodAllocation : String := "Allocation of a method pointer array" + +end ElementShape + +/-- The shape of an array element of declared type `type`: a class reference for a class other + than `System.Object`, the size the checked layout gives for a struct that the raw layouts + hold under its identity and that is not by-ref-like, the managed signature of a function + pointer whose struct parameters the raw layouts bind, and the storage `arrayElementType` + gives for every other type. -/ +def LayoutEnvironment.elementShape (environment : LayoutEnvironment) + (type : Extraction.TypeExpr) : ElementShape := + match type with + | .object _ name => + if name = ElementShape.rootObjectName then .unsupported ElementShape.rootObjectElement + else .reference type + | .aggregate identity index => + match environment.rawLayouts[index]? with + | none => .unsupported ElementShape.outsideLayouts + | some metadata => + if metadata.identity ≠ identity then .unsupported ElementShape.outsideLayouts + else if metadata.byRefLike then .unsupported ElementShape.byRefLikeElement + else match environment.layouts[index]? with + | some layout => .value (.aggregate identity layout.layout.size) + | none => .unsupported ElementShape.outsideLayouts + | _ => match Extraction.managedSignature type with + | some signature => + if Extraction.callableLayoutsValid environment.rawLayouts type then .method signature + else .unsupported ElementShape.outsideLayouts + | none => match Extraction.arrayElementType type with + | some element => .value element + | none => .unsupported ElementShape.unsupportedType + +/-- The shape of an array element of declared type `type` in the method's layouts. -/ +def Method.elementShape (body : Method) (type : Extraction.TypeExpr) : ElementShape := + body.environment.elementShape type + +/-- The shape of an array of the primitive: its own storage, and none for floating point. -/ +def PrimitiveType.elementShape : PrimitiveType → ElementShape + | .sbyte => .value .signed8 | .byte => .value .unsigned8 | .boolean => .value .boolean + | .int16 => .value .signed16 | .uint16 => .value .unsigned16 | .char => .value .character + | .int32 => .value .signed32 | .uint32 => .value .unsigned32 + | .int64 => .value .signed64 | .uint64 => .value .unsigned64 + | .intPtr => .value .nativeInt | .uintPtr => .value .nativeUInt + | .single | .double => .unsupported ElementShape.unsupportedType + +/-- The storage of an enum over the primitive; `none` where `typeStorage` admits no enum. -/ +def PrimitiveType.enumStorage : PrimitiveType → Option EnumStorage + | .sbyte => some .signed8 | .byte => some .unsigned8 + | .int16 => some .signed16 | .uint16 => some .unsigned16 + | .int32 => some .signed32 | .uint32 => some .unsigned32 + | .int64 => some .signed64 | .uint64 => some .unsigned64 + | .boolean | .char | .single | .double | .intPtr | .uintPtr => none + +/-- The shape of an array of the enum `identity` over the primitive. -/ +def PrimitiveType.enumElementShape (identity : String) (type : PrimitiveType) : ElementShape := + match type.enumStorage with + | some storage => .value (.enumeration identity storage) + | none => .unsupported ElementShape.unsupportedType + +/-- An array of a primitive holds the primitive's storage, whatever the layouts. -/ +theorem Method.elementShape_primitive (body : Method) (type : PrimitiveType) : + body.elementShape type.typeExpr = type.elementShape := by + cases type <;> rfl + +#print axioms Method.elementShape_primitive + +/-- An array of an enum over a primitive holds the enum's storage where `typeStorage` admits + the primitive as an enum's underlying type, whatever the layouts. -/ +theorem Method.elementShape_enum (body : Method) (identity : String) (type : PrimitiveType) : + body.elementShape (.enum identity type.typeExpr) = type.enumElementShape identity := by + cases type <;> rfl + +#print axioms Method.elementShape_enum + +/-- An array of a class other than `System.Object` holds references to its instances. -/ +theorem Method.elementShape_object (body : Method) {scope name : String} + (derived : name ≠ ElementShape.rootObjectName) : + body.elementShape (.object scope name) = .reference (.object scope name) := by + simp [Method.elementShape, LayoutEnvironment.elementShape, derived] + +#print axioms Method.elementShape_object + +/-- An array of a struct the raw layouts hold under its identity, and that is not by-ref-like, + holds values of the size its checked layout gives. -/ +theorem Method.elementShape_aggregate {body : Method} {identity : String} {index : Nat} + {metadata : Extraction.RawLayout} {layout : Extraction.ComputedLayout} + (declared : body.environment.rawLayouts[index]? = some metadata) + (identified : metadata.identity = identity) (inline : metadata.byRefLike = false) + (computed : body.environment.layouts.toList[index]? = some layout) : + body.elementShape (.aggregate identity index) = + .value (.aggregate identity layout.layout.size) := by + rw [Array.getElem?_toList] at computed + simp [Method.elementShape, LayoutEnvironment.elementShape, declared, identified, inline, + computed] + +#print axioms Method.elementShape_aggregate + +/-- An array of a by-ref-like struct has no shape: no heap array holds one. -/ +theorem Method.elementShape_byRefLike {body : Method} {identity : String} {index : Nat} + {metadata : Extraction.RawLayout} + (declared : body.environment.rawLayouts[index]? = some metadata) + (identified : metadata.identity = identity) (byRefLike : metadata.byRefLike = true) : + body.elementShape (.aggregate identity index) = + .unsupported ElementShape.byRefLikeElement := by + simp [Method.elementShape, LayoutEnvironment.elementShape, declared, identified, byRefLike] + +#print axioms Method.elementShape_byRefLike + +/-! ## Field and value shapes -/ + +namespace Extraction + +/-- The width and sign extension of a class instance field of `type`: integer, Boolean, Char + and enum fields only, each fitting one `objectFieldSlot`. -/ +def objectFieldShape (type : TypeExpr) : Option (Nat × Bool) := + match typeStorage [] type with + | some (.field kind) => (primitiveFieldWidth kind).map fun width => (width, signedField kind) + | _ => none + +/-- A class instance field whose slot holds an object reference: one typed as a class or as an + array of one, which is itself an object on the heap, or as a generic class instance. -/ +def referenceFieldType : TypeExpr → Bool + | .object _ _ | .array (.object _ _) => true + | type => type.objectApplication + +/-- A class instance field type whose references the model never reads through: a generic class + instance or `System.Object`, which name no class the model lays out. -/ +def opaqueFieldType : TypeExpr → Bool + | .object _ name => name == ElementShape.rootObjectName + | type => type.objectApplication + +/-- The element of a class instance field typed as an array of a value type, as the evaluation + stack records it: a struct element by its identity alone. -/ +def valueArrayFieldElement (type : TypeExpr) : Option ArrayElement := + match type with + | .array element => arrayElementType element + | _ => none + +end Extraction + +/-- What the model keeps in a class instance field's 8-byte slot. -/ +inductive ClassSlot where + /-- An integer, Boolean, Char or enum of `width` bytes, sign-extended when `signed`. -/ + | word (width : Nat) (signed : Bool) + /-- An object reference: a class or an array of a class. -/ + | reference + /-- A reference to an array of the value type `element`. -/ + | array (element : ArrayElement) + deriving DecidableEq, Repr + +/-- How `ldfld`, `stfld` and `ldflda` of one field operand reach memory. -/ +inductive FieldShape where + /-- A primitive or vector field of a value type: `width` bytes at byte `offset` of the + owner, sign-extended on load when `signed`. -/ + | value (offset width : Nat) (signed : Bool) + /-- A nested struct field of a value type: `size` bytes at byte `offset` of the owner. -/ + | aggregate (offset size : Nat) + /-- A class instance field at model offset `offset`; its address is never formed. -/ + | classSlot (offset : Nat) (slot : ClassSlot) + /-- A field outside the checked layouts and class slots; an access faults with `reason`. -/ + | unsupported (reason : String) + deriving DecidableEq, Repr + +/-- The shape of a value-type field with checked layout entry `layout`, sign-extended on load + when `signed`. -/ +def FieldShape.ofLayout (layout : FieldLayout) (signed : Bool) : FieldShape := + if layout.aggregate then .aggregate layout.offset layout.width + else .value layout.offset layout.width signed + +/-- The byte offset `ldflda` adds to a struct receiver's address; `none` for a class slot, + whose address is never formed, and for an unsupported field. -/ +def FieldShape.structOffset? : FieldShape → Option Nat + | .value offset .. | .aggregate offset _ => some offset + | _ => none + +/-- The slot of a class instance field typed `type`, apart from the size of a struct array + element, which only the environment's layouts know. -/ +def classSlotOf (type : Extraction.TypeExpr) : Option ClassSlot := + match Extraction.objectFieldShape type with + | some (width, signed) => some (.word width signed) + | none => + if Extraction.referenceFieldType type then some .reference + else (Extraction.valueArrayFieldElement type).map .array + +/-- The slot of a class instance field typed `type`. A struct array element is an admitted + struct that is not byref-like, sized by its checked layout. -/ +def LayoutEnvironment.classSlot (environment : LayoutEnvironment) (type : Extraction.TypeExpr) : + Option ClassSlot := + match type with + | .array (.aggregate identity index) => do + let .aggregateArray .. ← Extraction.typeStorage environment.rawLayouts type | none + let layout ← environment.layouts[index]? + return .array (.aggregate identity layout.layout.size) + | _ => classSlotOf type + +/-- The checked layout entry of field `index` of the struct at layout `parent`, with whether a + load sign-extends it; `none` unless the field is declared with the tag `held` has, a + vector's element lane included. -/ +def LayoutEnvironment.structField? (environment : LayoutEnvironment) (parent index : Nat) + (held : Extraction.TypeExpr) : Option (FieldLayout × Bool) := do + let metadata ← environment.rawLayouts[parent]? + let declared ← metadata.fields[index]? + if Extraction.fieldKind? environment.rawLayouts held != some declared.type then none + else pure () + let layout ← environment.layouts[parent]? + let computed ← layout.layout.fields[index]? + return (computed, Extraction.signedField declared.type) + +/-- How an access to `field` reaches memory. A class owner's field is the slot at the model + offset of its index; a value-type owner's field is its checked layout entry when the operand + holds the field's declared type; a field of any other owner is unsupported. -/ +def LayoutEnvironment.fieldShape (environment : LayoutEnvironment) + (field : Extraction.FieldRef) : FieldShape := + match field.owner with + | .object _ _ => match environment.classSlot field.type with + | some slot => .classSlot (objectFieldOffset field.index) slot + | none => .unsupported "Class instance field type" + | owner => match Extraction.typeStorage environment.rawLayouts owner with + | some (.field (.aggregate parent)) => + match environment.structField? parent field.index field.type with + | some (computed, signed) => FieldShape.ofLayout computed signed + | none => .unsupported "Field outside the checked layouts" + | _ => .unsupported "Field owner is not a value type" + +/-- How `ldobj`, `stobj`, `initobj`, `cpobj` and `sizeof` of one type operand reach memory. -/ +inductive ValueShape where + /-- A primitive integer, Boolean, Char or enum of `width` bytes, sign-extended on load when + `signed`. -/ + | primitive (width : Nat) (signed : Bool) + /-- A struct of `size` bytes. -/ + | aggregate (size : Nat) + /-- A vector of `width` bytes. -/ + | vector (width : Nat) + /-- A span. The address a load reads it through is the view an `in` argument passed; a local's + home names a slot instead, which the load refuses. -/ + | span + /-- A type outside the checked layouts and primitives; an access faults with `reason`. -/ + | unsupported (reason : String) + deriving DecidableEq, Repr + +/-- A layout read stops at its row or the end of the list. Each tail supplies a lookup function + so reducing a closed index does not nest one match per preceding row. -/ +noncomputable def layoutAt? (rows : Array α) (index : Nat) : Option α := + rows.toList.rec (fun _ => none) (fun row _ next position => + match position with + | 0 => some row + | index + 1 => next index) index + +/-- Layout reads agree with optional indexing at every position. -/ +@[simp] theorem layoutAt?_eq (rows : Array α) (index : Nat) : + layoutAt? rows index = rows[index]? := by + rw [← Array.getElem?_toList] + cases rows with + | mk rows => + induction rows generalizing index with + | nil => cases index <;> rfl + | cons row rest ih => + cases index with + | zero => rfl + | succ index => exact ih index + +#print axioms layoutAt?_eq + +/-- Native code reads the same position directly from the array. -/ +private def nativeLayoutAt? (rows : Array α) (index : Nat) : Option α := rows[index]? + +/-- Compiled layout reads use the array's native bounds check and index. -/ +@[csimp] private theorem layoutAt?_compiled : @layoutAt? = @nativeLayoutAt? := by + funext α rows index + exact layoutAt?_eq rows index + +#print axioms layoutAt?_compiled + +/-- How a value of `type` reaches memory: as a span, a vector of its width, a struct sized by its + checked layout or a primitive of its width. -/ +def valueShapeAt (layouts : Array Extraction.ComputedLayout) + (storage : Extraction.TypeExpr → Option Extraction.StorageType) + (type : Extraction.TypeExpr) : ValueShape := + if Extraction.spanLocal type then .span else + match Extraction.integerVectorWidth type with + | some width => .vector width + | none => match storage type with + | some (.field (.aggregate index)) => match layoutAt? layouts index with + | some layout => .aggregate layout.layout.size + | none => .unsupported "Value type outside the checked layouts" + | some (.field kind) => match Extraction.primitiveFieldWidth kind with + | some width => .primitive width (Extraction.signedField kind) + | none => .unsupported "Value type without a width" + | some .vector128 => .vector 16 + | some .vector256 => .vector 32 + | some .vector512 => .vector 64 + | _ => .unsupported "Not a value type" + +/-- The value representation given by the method's checked layout environment. -/ +def LayoutEnvironment.valueShape (environment : LayoutEnvironment) + (type : Extraction.TypeExpr) : ValueShape := + valueShapeAt environment.layouts (Extraction.typeStorage environment.rawLayouts) type + +/-- The bytes a value of the shape occupies; `none` for a span and an unsupported type. -/ +def ValueShape.size? : ValueShape → Option Nat + | .primitive width _ | .vector width => some width + | .aggregate size => some size + | _ => none + +/-- How an access to `field` in `body` reaches memory, from the program-wide layouts. -/ +def Method.fieldShape (body : Method) (field : Extraction.FieldRef) : FieldShape := + body.environment.fieldShape field + +/-- How a value of `type` in `body` reaches memory, from the program-wide layouts. -/ +def Method.valueShape (body : Method) (type : Extraction.TypeExpr) : ValueShape := + body.environment.valueShape type + +/-- A class slot sits at the model offset of the field's index, and only a class owner has one, + so a view naming a field by owner and offset agrees with the step that resolves it. -/ +theorem LayoutEnvironment.fieldShape_class_offset {environment : LayoutEnvironment} + {field : Extraction.FieldRef} {offset : Nat} {slot : ClassSlot} + (shape : environment.fieldShape field = .classSlot offset slot) : + offset = objectFieldOffset field.index ∧ ∃ scope name, field.owner = .object scope name := by + unfold LayoutEnvironment.fieldShape at shape + split at shape + · next scope name owned => + split at shape + · simp only [FieldShape.classSlot.injEq] at shape + exact ⟨shape.1.symm, scope, name, owned⟩ + · cases shape + · split at shape + · split at shape + · unfold FieldShape.ofLayout at shape + split at shape <;> cases shape + · cases shape + · cases shape + +#print axioms LayoutEnvironment.fieldShape_class_offset + +/-- A field of a class owner whose type has a slot resolves to that slot at the model offset of + its index. -/ +theorem LayoutEnvironment.fieldShape_class {environment : LayoutEnvironment} + {field : Extraction.FieldRef} {scope name : String} {slot : ClassSlot} + (owner : field.owner = .object scope name) + (typed : environment.classSlot field.type = some slot) : + environment.fieldShape field = .classSlot (objectFieldOffset field.index) slot := by + simp [LayoutEnvironment.fieldShape, owner, typed] + +#print axioms LayoutEnvironment.fieldShape_class + +/-- A field of a struct owner resolves to its checked layout entry. -/ +theorem LayoutEnvironment.fieldShape_struct {environment : LayoutEnvironment} + {field : Extraction.FieldRef} {parent : Nat} {computed : FieldLayout} {signed : Bool} + (owner : Extraction.typeStorage environment.rawLayouts field.owner = + some (.field (.aggregate parent))) + (found : environment.structField? parent field.index field.type = some (computed, signed)) : + environment.fieldShape field = FieldShape.ofLayout computed signed := by + unfold LayoutEnvironment.fieldShape + split + · next scope name owned => + simp [owned, Extraction.typeStorage, Extraction.typeStorageAt] at owner + · simp [owner, found] + +#print axioms LayoutEnvironment.fieldShape_struct + +/-- A method resolves a field as the environment it shares does. -/ +theorem Method.fieldShape_of_environment {body : Method} {environment : LayoutEnvironment} + {field : Extraction.FieldRef} {shape : FieldShape} (shared : body.environment = environment) + (resolved : environment.fieldShape field = shape) : body.fieldShape field = shape := by + rw [Method.fieldShape, shared, resolved] + +#print axioms Method.fieldShape_of_environment + +/-- A method resolves a value type as the environment it shares does. -/ +theorem Method.valueShape_of_environment {body : Method} {environment : LayoutEnvironment} + {type : Extraction.TypeExpr} {shape : ValueShape} (shared : body.environment = environment) + (resolved : environment.valueShape type = shape) : body.valueShape type = shape := by + rw [Method.valueShape, shared, resolved] + +#print axioms Method.valueShape_of_environment + +/-! ### Shapes from one layout entry + +Each lemma reads the layouts only through the entries it names, so a fact built from them never +has the kernel walk the layout lists to an index; the entries come from named suffixes. -/ + +/-- A struct type decodes to the field storage of its index when the layout there carries its + identity. -/ +theorem Extraction.typeStorage_aggregate {layouts : List Extraction.RawLayout} + {metadata : Extraction.RawLayout} {identity : String} {index : Nat} + (found : layouts[index]? = some metadata) (named : metadata.identity = identity) : + Extraction.typeStorage layouts (.aggregate identity index) = + some (.field (.aggregate index)) := by + simp only [Extraction.typeStorage, Extraction.typeStorageAt] + rw [found] + dsimp only [bind, Option.bind] + simp only [named, ite_true, eq_self, pure] + +#print axioms Extraction.typeStorage_aggregate + +/-- A struct type is held by a field declared with its index when the layout there carries its + identity. -/ +theorem Extraction.fieldKind?_aggregate {layouts : List Extraction.RawLayout} + {metadata : Extraction.RawLayout} {identity : String} {index : Nat} + (found : layouts[index]? = some metadata) (named : metadata.identity = identity) : + Extraction.fieldKind? layouts (.aggregate identity index) = some (.aggregate index) := by + unfold Extraction.fieldKind? + rw [Extraction.typeStorage_aggregate found named] + +#print axioms Extraction.fieldKind?_aggregate + +/-- An array of a struct decodes to an array of that struct's layout when the layout at its + index carries its identity and is not byref-like. -/ +theorem Extraction.typeStorage_aggregateArray {layouts : List Extraction.RawLayout} + {metadata : Extraction.RawLayout} {identity : String} {index : Nat} + (found : layouts[index]? = some metadata) + (admitted : (metadata.identity == identity && !metadata.byRefLike) = true) : + Extraction.typeStorage layouts (.array (.aggregate identity index)) = + some (.aggregateArray identity index) := by + simp only [Extraction.typeStorage, Extraction.typeStorageAt] + rw [found] + dsimp only [bind, Option.bind] + simp only [admitted, ite_true] + +#print axioms Extraction.typeStorage_aggregateArray + +/-- Field `index` of the struct at `parent` is its checked entry when it is declared with the + tag `held` has. -/ +theorem LayoutEnvironment.structField?_eq {environment : LayoutEnvironment} + {parent index : Nat} {held : Extraction.TypeExpr} {metadata : Extraction.RawLayout} + {declared : Extraction.RawField} {layout : Extraction.ComputedLayout} {computed : FieldLayout} + (raw : environment.rawLayouts[parent]? = some metadata) + (field : metadata.fields[index]? = some declared) + (kind : Extraction.fieldKind? environment.rawLayouts held = some declared.type) + (checked : environment.layouts.toList[parent]? = some layout) + (entry : layout.layout.fields[index]? = some computed) : + environment.structField? parent index held = + some (computed, Extraction.signedField declared.type) := by + rw [Array.getElem?_toList] at checked + unfold LayoutEnvironment.structField? + rw [raw] + dsimp only [bind, Option.bind] + rw [field] + dsimp only [bind, Option.bind] + rw [kind, bne_self_eq_false] + dsimp only [bind, Option.bind, Bool.false_eq_true, ↓reduceIte, pure] + rw [checked] + dsimp only [bind, Option.bind] + rw [entry] + rfl + +#print axioms LayoutEnvironment.structField?_eq + +/-- A class field holding an array of an admitted struct has that struct's checked size. -/ +theorem LayoutEnvironment.classSlot_aggregateArray {environment : LayoutEnvironment} + {identity : String} {index : Nat} {layout : Extraction.ComputedLayout} + (storage : Extraction.typeStorage environment.rawLayouts (.array (.aggregate identity index)) = + some (.aggregateArray identity index)) + (checked : environment.layouts.toList[index]? = some layout) : + environment.classSlot (.array (.aggregate identity index)) = + some (.array (.aggregate identity layout.layout.size)) := by + rw [Array.getElem?_toList] at checked + unfold LayoutEnvironment.classSlot + rw [storage] + dsimp only [bind, Option.bind] + rw [checked] + rfl + +#print axioms LayoutEnvironment.classSlot_aggregateArray + +/-- A struct type that is neither a span nor a vector has its checked size. -/ +theorem LayoutEnvironment.valueShape_aggregate {environment : LayoutEnvironment} + {type : Extraction.TypeExpr} {index : Nat} {layout : Extraction.ComputedLayout} + (notSpan : Extraction.spanLocal type = false) + (notVector : Extraction.integerVectorWidth type = none) + (storage : Extraction.typeStorage environment.rawLayouts type = + some (.field (.aggregate index))) + (checked : environment.layouts.toList[index]? = some layout) : + environment.valueShape type = .aggregate layout.layout.size := by + rw [Array.getElem?_toList] at checked + simp [LayoutEnvironment.valueShape, valueShapeAt, notSpan, notVector, storage, checked] + +#print axioms LayoutEnvironment.valueShape_aggregate + +end CIL diff --git a/CIL/RuntimeModel.lean b/CIL/RuntimeModel.lean new file mode 100644 index 000000000..636d48124 --- /dev/null +++ b/CIL/RuntimeModel.lean @@ -0,0 +1,92 @@ +import CIL.Instructions + +/-! +# Runtime models + +A runtime model is the meaning the checked interpreter gives a call to a runtime library method +whose body is not extracted: an intrinsic, a checked memory helper, a hardware feature test, a +constant, a reference comparison or subtraction, an unaligned typed transfer, `Unsafe.SkipInit`, +the `System.Object` constructor or a span constructor run in place on a span local. +`CIL.Safety.runModel` executes a model, and `MemoryOp.callReached` names the memory operations +no opcode emits. +-/ + +namespace CIL + +/-- The opcode of a call site whose target a runtime model can stand for. -/ +inductive CallOpcode where + | call | newobj + deriving DecidableEq, Repr + +/-- The order of two references that `Unsafe.AreSame` (`same`), `IsAddressLessThan` + (`less`: the first below the second) and `IsAddressGreaterThan` (`greater`) test. -/ +inductive ReferenceRelation where + | same | less | greater + deriving DecidableEq, Repr + +/-- What a call to a modelled runtime method does. -/ +inductive RuntimeModel where + /-- An intrinsic taking `argc` operands, available only where the profile has it. -/ + | intrinsic (operation : Intrinsic) (argc : Nat) + /-- A checked memory helper, run as `memoryInstruction` runs `operation`. -/ + | memory (operation : MemoryOp) + /-- A hardware feature test, answered by the method's profile. -/ + | feature (feature : Feature) + /-- A constant of the target, such as `IntPtr.Size` or `Unsafe.SizeOf`. -/ + | constant (value : W32) + /-- `Unsafe.AreSame`, `IsAddressLessThan` or `IsAddressGreaterThan`. -/ + | referenceCompare (relation : ReferenceRelation) + /-- `Unsafe.SubtractByteOffset`: the reference moved back by a byte offset. -/ + | referenceSubtract + /-- `Unsafe.ReadUnaligned` (`store` false) or `WriteUnaligned` of a value of `kind`. -/ + | typedTransfer (kind : TransferKind) (store : Bool) + /-- `Unsafe.SkipInit`: its out reference must form, and nothing is written. -/ + | skipInit + /-- The `System.Object` constructor, which only consumes its receiver. -/ + | objectConstructor + /-- `System.Type.GetTypeFromHandle` on a closed metadata type. -/ + | typeFromHandle + /-- `System.Type.op_Equality` or `op_Inequality` on two runtime types. -/ + | typeEquality (equal : Bool) + /-- A span constructor called on the home of a span local or homed parameter: `constructor` + builds the span from the explicit arguments and the slot the home names then holds it. -/ + | spanInPlace (constructor : SpanConstructor) + deriving DecidableEq, Repr + +/-- Operands a model observes; the checked interpreter rejects the call when any of them has + unspecified bits. Intrinsics count none, since they decide themselves which bits they read. -/ +def RuntimeModel.observerCount : RuntimeModel → Nat + | .intrinsic .. | .feature _ | .constant _ | .objectConstructor => 0 + | .memory operation => operation.argumentCount + | .spanInPlace constructor => constructor.operation.argumentCount + | .referenceCompare _ | .referenceSubtract => 2 + | .typedTransfer _ store => if store then 2 else 1 + | .skipInit | .typeFromHandle => 1 + | .typeEquality _ => 2 + +/-- Whether `profile` may run `model`: an intrinsic must be available in it, and every other + model runs under any profile. -/ +def RuntimeModel.available (profile : FeatureProfile) : RuntimeModel → Bool + | .intrinsic operation _ => operation.available profile + | _ => true + +/-- Whether only a call to a runtime method reaches `operation`, so that no opcode emits it. + `cpblk` and `initblk` emit the block operations, which calls also reach, so they count as + opcode meanings. The match has no default, so each new operation is classified. -/ +def MemoryOp.callReached : MemoryOp → Bool + -- Views, spans and their observations + | .observeView _ | .spanReinterpret .. | .spanCopy .. | .spanFill .. | .arrayFill .. + | .spanCreate | .spanReference | .createSpan _ | .spanItem _ | .spanLength | .spanEmpty .. + | .spanFromReference .. | .spanSingleReference .. | .spanSlice _ | .spanReadOnly + | .arraySpan .. => true + -- Transfers and wide arithmetic + | .binaryRead .. | .binaryWrite .. | .marshalRead .. | .marshalWrite .. | .pointerTransfer .. + | .vector128Unsafe .. | .bigMul64 _ | .divRemOut _ => true + -- References, bit casts and guards + | .asRef | .add .. | .byteOffset | .bitcastByteBool | .bitcastValue .. | .nullRef | .isNullRef + | .throwIfNull | .zeroGuard .. | .comparisonGuard .. => true + -- Opcode meanings: blocks, vector loads and stores, arrays + | .copyBlock _ | .initBlock _ + | .load128 | .load256 | .load512 | .store128 | .store256 | .store512 => false + +end CIL diff --git a/CIL/SIMD/Intrinsics.lean b/CIL/SIMD/Intrinsics.lean index 71a3ed944..44581312f 100644 --- a/CIL/SIMD/Intrinsics.lean +++ b/CIL/SIMD/Intrinsics.lean @@ -33,10 +33,9 @@ inductive VectorIntrinsic where | reinterpret (width : Nat) | zero (width : Nat) | ones (width : Nat) | create64 (width : Nat) | extract64 (width : Nat) | ashr64 (width : Nat) | create32 (width : Nat) | createScalar32 (width : Nat) | createScalar64 (width : Nat) - | extractMSB64 (width : Nat) | sum64 (width : Nat) | mul64 (width : Nat) + | extractMSB64 (width : Nat) | sum64 (width : Nat) | createScalarUnsafe64 | combine256 - | byteToBool deriving DecidableEq, Repr /-- ARM AdvSimd intrinsics. -/ @@ -151,7 +150,6 @@ def evalIntrinsic (op : Intrinsic) (args : List Value) : Option Value := do -- Portable vectors at any width: lanewise arithmetic and comparisons, bitwise operations. | .vector (.add64 w), xs => binaryLanes w (· + ·) xs | .vector (.sub64 w), xs => binaryLanes w (· - ·) xs - | .vector (.mul64 w), xs => binaryLanes w (· * ·) xs | .vector (.ltu64 w), xs => binaryLanes w (fun a b => Vector.mask64 (a.ult b)) xs | .vector (.lt64 w), xs => binaryLanes w (fun a b => Vector.mask64 (a.slt b)) xs | .vector (.eq64 w), xs => binaryLanes w (fun a b => Vector.mask64 (a == b)) xs @@ -209,11 +207,8 @@ def evalIntrinsic (op : Intrinsic) (args : List Value) : Option Value := do | .vector (.reinterpret 128), [.v128Low64 low] => some (.v128Low64 low) | .vector (.extract64 128), [.v128Low64 low, .i32 index] => if index.toNat == 0 then some (.i64 low) else none - -- Joining two 256-bit vectors, and reading a byte as a `bool`. + -- Joining two 256-bit vectors. | .vector .combine256, [.v256 lower, .v256 upper] => some (.v512 (upper ++ lower)) - | .vector .byteToBool, [.i32 a] => - if a.toNat < 256 then some (.i32 a) else none - | .vector .byteToBool, [.i8 a] => some (.i32 (a.setWidth 32)) -- 128-bit byte alignment and shuffles: ARM AdvSimd and SSE/SSSE3. | .advSimd .extract64, [.v128 a, .v128 b, i] => let c ← immediate i diff --git a/CIL/SIMD/PackedLanes.lean b/CIL/SIMD/PackedLanes.lean index ef415483e..da5ee699a 100644 --- a/CIL/SIMD/PackedLanes.lean +++ b/CIL/SIMD/PackedLanes.lean @@ -837,12 +837,6 @@ theorem intrinsic_avx512dq_mul256 (x y : V256) : rfl #print axioms intrinsic_avx512dq_mul256 -/-- Portable `Vector256` multiplication keeps the low 64 bits of each lane product. -/ -theorem intrinsic_mul256 (x y : V256) : - evalIntrinsic (.vector (.mul64 256)) [.v256 x, .v256 y] = - some (.v256 (zip256 (· * ·) x y)) := rfl -#print axioms intrinsic_mul256 - /-- AVX-512DQ `MoveMask` gives the lane sign bits, like the AVX form. -/ theorem intrinsic_avx512dq_movemask256 (x : V256) : evalIntrinsic (.avx512DQ .moveMask64) [.v256 x] = some (.i32 (moveMask64 x)) := rfl diff --git a/CIL/SIMD/UnspecifiedLanes.lean b/CIL/SIMD/UnspecifiedLanes.lean index e223bec5d..7fd60ed68 100644 --- a/CIL/SIMD/UnspecifiedLanes.lean +++ b/CIL/SIMD/UnspecifiedLanes.lean @@ -172,7 +172,7 @@ theorem opaque128_store_memory_identity (memory result : Memory) (before : Optio | reference _ => cases stored | span _ _ => cases stored | bufferSpan _ | readOnlyReference _ | array _ | methodPointer _ | methodTable _ - | object _ => cases stored + | typeHandle _ | runtimeType _ | object _ | spanHome _ => cases stored theorem opaque128_init_zero (memory : Memory) : makeOpaque128 (.v128 0) memory = .ok (.opaque128 (some (.known 0)), [], memory) := rfl diff --git a/CIL/Safety/ActionRenaming.lean b/CIL/Safety/ActionRenaming.lean index 3e288c250..bbfa981a2 100644 --- a/CIL/Safety/ActionRenaming.lean +++ b/CIL/Safety/ActionRenaming.lean @@ -17,11 +17,12 @@ inductive FrameAction.Renamed (indices : LocalIndexMap) (mapping : AllocationRen (memory : Memory.Renamed mapping scopes sourceMemory targetMemory) (frame : Frame.Renamed indices mapping scopes sourceFrame targetFrame) : Renamed indices mapping scopes (.next pc stack sourceFrame sourceMemory) - (.next pc (stack.map mapping.value) targetFrame targetMemory) + (.next pc (stack.map (indices.value mapping)) targetFrame targetMemory) | call {sourceMemory targetMemory callee arguments rest} (memory : Memory.Renamed mapping scopes sourceMemory targetMemory) : Renamed indices mapping scopes (.call callee arguments rest sourceMemory) - (.call callee (arguments.map mapping.value) (rest.map mapping.value) targetMemory) + (.call callee (arguments.map mapping.value) (rest.map (indices.value mapping)) + targetMemory) | tailCall {sourceMemory targetMemory callee arguments} (memory : Memory.Renamed mapping scopes sourceMemory targetMemory) : Renamed indices mapping scopes (.tailCall callee arguments sourceMemory) @@ -33,8 +34,8 @@ inductive FrameAction.Renamed (indices : LocalIndexMap) (mapping : AllocationRen (temporaryBound : temporary.allocation < mapping.sourceLimit) : Renamed indices mapping scopes (.construct callee arguments rest sourceFrame temporary kind sourceMemory) - (.construct callee (arguments.map mapping.value) (rest.map mapping.value) targetFrame - (mapping.reference temporary) kind targetMemory) + (.construct callee (arguments.map mapping.value) (rest.map (indices.value mapping)) + targetFrame (mapping.reference temporary) kind targetMemory) | returned {sourceMemory targetMemory values} (memory : Memory.Renamed mapping scopes sourceMemory targetMemory) : Renamed indices mapping scopes (.returned values sourceMemory) diff --git a/CIL/Safety/AggregateFields.lean b/CIL/Safety/AggregateFields.lean index 9ed6b5f07..4c5eaab4c 100644 --- a/CIL/Safety/AggregateFields.lean +++ b/CIL/Safety/AggregateFields.lean @@ -265,53 +265,57 @@ theorem writeSnapshot_initialized (memory : Memory) (reference : Reference) cases access memory reference bytes.length alignment true with | error => rfl | ok => - simp only [Bind.bind, Except.bind, Pure.pure, Except.pure, List.getElem?_map] + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure, + snapshotCellAt_eq, List.getElem?_map] congr 2 funext allocation position split <;> try rfl cases bytes[position - reference.offset]? <;> rfl -/-- Closes a step equation through its generic `instructionStep` equation, as the stepping - equations in `InstructionSteps` are closed. -/ -local macro "field_step_proof " generic:ident : tactic => `(tactic| ( - rw [$generic:ident] - simp only [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, +/-- Reduce a vector store at the offset and width of its checked field. -/ +local macro "field_step_proof " shape:ident : tactic => `(tactic| ( + rw [step_stfld_value_eq _ _ _ _ _ _ _ _ _ $shape:ident] + simp only [storeValueField, cil_guard, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, bind_assoc, pure_bind, referenceAt, valueSnapshot, ite_true, writeSnapshot_initialized] all_goals first | rfl | (split <;> (try simp only [bind_assoc, pure_bind]) <;> rfl))) /-- A 16-byte vector field store writes the vector's bytes at the field address. -/ -theorem step_setFieldAt_v128_eq (body : CIL.Method) (pc : Nat) (args : List Value) - (frame : Frame) (memory : Memory) (offset : Nat) (bits : BitVec 128) (reference : Reference) - (rest : List Value) : - step body (.setFieldAt offset 16) pc args frame +theorem step_stfld_value_v128_eq (body : CIL.Method) (pc : Nat) (args : List Value) + (frame : Frame) (memory : Memory) (operand : Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape operand = .value offset 16 signed) (bits : BitVec 128) + (reference : Reference) (rest : List Value) : + step body (.stfld operand) pc args frame (.scalar (.v128 bits) :: .reference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) let stored ← checkedAt (.address address) 16 (write memory address (numberBytes bits.toNat 16) ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := by - field_step_proof step_setFieldAt_eq + field_step_proof shape /-- A 32-byte vector field store writes the vector's bytes at the field address. -/ -theorem step_setFieldAt_v256_eq (body : CIL.Method) (pc : Nat) (args : List Value) - (frame : Frame) (memory : Memory) (offset : Nat) (bits : BitVec 256) (reference : Reference) - (rest : List Value) : - step body (.setFieldAt offset 32) pc args frame +theorem step_stfld_value_v256_eq (body : CIL.Method) (pc : Nat) (args : List Value) + (frame : Frame) (memory : Memory) (operand : Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape operand = .value offset 32 signed) (bits : BitVec 256) + (reference : Reference) (rest : List Value) : + step body (.stfld operand) pc args frame (.scalar (.v256 bits) :: .reference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) let stored ← checkedAt (.address address) 32 (write memory address (numberBytes bits.toNat 32) ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := by - field_step_proof step_setFieldAt_eq + field_step_proof shape /-- A nested struct field store writes the struct's bytes at the field address once their count matches the field's. -/ -theorem step_setAggregateFieldAt_reference_eq (body : CIL.Method) (pc : Nat) - (args : List Value) (frame : Frame) (memory : Memory) (offset size : Nat) +theorem step_stfld_aggregate_reference_eq (body : CIL.Method) (pc : Nat) + (args : List Value) (frame : Frame) (memory : Memory) (operand : Extraction.FieldRef) + (offset size : Nat) + (shape : body.fieldShape operand = .aggregate offset size) (bytes : List (BitVec 8)) (reference : Reference) (rest : List Value) : - step body (.setAggregateFieldAt offset size) pc args frame + step body (.stfld operand) pc args frame (.scalar (.aggregate bytes) :: .reference (.address reference) :: rest) memory = if bytes.length != size then .error .invalidState else do let address ← checkedAt (.address reference) 0 @@ -319,20 +323,61 @@ theorem step_setAggregateFieldAt_reference_eq (body : CIL.Method) (pc : Nat) let stored ← checkedAt (.address address) size (write memory address bytes ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := by - field_step_proof step_setAggregateFieldAt_eq - -/-- A nested struct field load reads the field's bytes at the field address. -/ -theorem step_aggregateFieldAt_reference_eq (body : CIL.Method) (pc : Nat) (args : List Value) - (frame : Frame) (memory : Memory) (offset size : Nat) (reference : Reference) + rw [step_stfld_aggregate_eq body pc args frame memory operand offset size shape] + simp only [storeAggregateField, cil_guard, unspecifiedOperands, List.take, List.any, + Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, referenceAt] + all_goals first | rfl | (split <;> (try simp only [bind_assoc, pure_bind]) <;> rfl) + +/-- A nested struct field store of a snapshot writes its bytes and initialization flags at the + field address once its width and byte count match the field's. -/ +theorem step_stfld_aggregate_snapshot_eq (body : CIL.Method) (pc : Nat) + (args : List Value) (frame : Frame) (memory : Memory) (operand : Extraction.FieldRef) + (offset size width : Nat) + (shape : body.fieldShape operand = .aggregate offset size) + (bytes : List (BitVec 8 × Bool)) (reference : Reference) (rest : List Value) : + step body (.stfld operand) pc args frame + (.scalar (.snapshot (.aggregate width) bytes) :: .reference (.address reference) :: rest) + memory = + if bytes.length != size || width != size then .error .invalidState else do + let address ← checkedAt (.address reference) 0 + (add memory reference 1 (BitVec.ofNat 64 offset)) + let stored ← checkedAt (.address address) size + (writeSnapshot memory address bytes ordinaryAccessAlignment) + return .next (pc + 1) rest frame stored := by + rw [step_stfld_aggregate_eq body pc args frame memory operand offset size shape] + simp only [storeAggregateField, cil_guard, unspecifiedOperands, List.take, List.any, + Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, referenceAt] + all_goals first | rfl | (split <;> (try simp only [bind_assoc, pure_bind]) <;> rfl) + +/-- A nested struct field load copies the field's bytes at the field address, as a snapshot + keeping their initialization flags when some of them are uninitialized. -/ +theorem step_ldfld_aggregate_reference_eq (body : CIL.Method) (pc : Nat) (args : List Value) + (frame : Frame) (memory : Memory) (field : Extraction.FieldRef) (offset size : Nat) + (shape : body.fieldShape field = .aggregate offset size) (reference : Reference) (rest : List Value) : - step body (.aggregateFieldAt offset size) pc args frame + step body (.ldfld field) pc args frame (.reference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) + if let .error .uninitialized := read memory address size ordinaryAccessAlignment then + let copied ← loadSnapshot memory (.address address) (.aggregate size) + return .next (pc + 1) (.scalar copied :: rest) frame memory let bytes ← checkedAt (.address address) size (read memory address size ordinaryAccessAlignment) return .next (pc + 1) (.scalar (.aggregate bytes) :: rest) frame memory := by - field_step_proof step_aggregateFieldAt_eq + rw [step_ldfld_aggregate_eq body pc args frame memory field offset size shape] + simp only [loadAggregateField, cil_guard, unspecifiedOperands, List.take, List.any, + Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, referenceAt, Bind.bind, + Except.bind, Pure.pure, Except.pure] + cases checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) with + | error => rfl + | ok address => + dsimp only + cases read memory address size ordinaryAccessAlignment with + | ok => rfl + | error fault => + cases fault <;> try rfl + cases loadSnapshot memory (.address address) (.aggregate size) <;> rfl /-- A field store into readable struct bytes leaves them reading as the stored field bytes. -/ theorem read_after_field_write {memory after : Memory} {home : Reference} @@ -379,15 +424,16 @@ theorem field_address {memory : Memory} {home : Reference} {length offset width /-- `stfld` into a struct home readable as `bytes` writes only the field's bytes, after which the home reads as `bytes` with that field stored. The step holds in any method and frame. -/ -theorem step_setFieldAt_home {memory : Memory} {home : Reference} {bytes : List (BitVec 8)} +theorem step_stfld_value_home {memory : Memory} {home : Reference} {bytes : List (BitVec 8)} {field : FieldLayout} (formed : memory.WellFormed) (loaded : read memory home bytes.length 1 = .ok bytes) (writable : access memory home bytes.length 1 true = .ok ()) (within : field.offset + field.width ≤ bytes.length) {value : CIL.Value} {number : Nat} (encoded : fieldStoreNumber field.width value = some number) : ∃ after, - (∀ body pc args frame rest, - step body (.setFieldAt field.offset field.width) pc args frame + (∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value field.offset field.width signed → ∀ pc args frame rest, + step body (.stfld operand) pc args frame (.scalar value :: .reference (.address home) :: rest) memory = .ok (.next (pc + 1) rest frame after)) ∧ write memory { home with offset := home.offset + field.offset } @@ -402,32 +448,33 @@ theorem step_setFieldAt_home {memory : Memory} {home : Reference} {bytes : List (alignment := 1) (by simpa only [numberBytes_length] using slice.access) have address := field_address formed writable positive within refine ⟨after, ?_, written, read_after_field_write loaded within written⟩ - intro body pc args frame rest + intro body operand signed shape pc args frame rest unfold fieldStoreNumber at encoded split at encoded <;> cases encoded <;> rename_i sized <;> - simp only [sized] at written address ⊢ + simp only [sized] at written address shape ⊢ all_goals first - | rw [step_setFieldAt_i32_1_eq, ← numberBytes_mod _ 1] - | rw [step_setFieldAt_i32_2_eq, ← numberBytes_mod _ 2] - | rw [step_setFieldAt_i32_4_eq] - | rw [step_setFieldAt_i64_eq] - | rw [step_setFieldAt_v128_eq] - | rw [step_setFieldAt_v256_eq] + | rw [step_stfld_value_i32_1_eq _ _ _ _ _ _ _ _ shape, ← numberBytes_mod _ 1] + | rw [step_stfld_value_i32_2_eq _ _ _ _ _ _ _ _ shape, ← numberBytes_mod _ 2] + | rw [step_stfld_value_i32_4_eq _ _ _ _ _ _ _ _ shape] + | rw [step_stfld_value_i64_eq _ _ _ _ _ _ _ _ shape] + | rw [step_stfld_value_v128_eq _ _ _ _ _ _ _ _ shape] + | rw [step_stfld_value_v256_eq _ _ _ _ _ _ _ _ shape] all_goals simp [address, written, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] /-- A nested struct store of `fieldBytes` into a struct home readable as `bytes` writes only the field's bytes, after which the home reads as `bytes` with that field stored. The step holds in any method and frame. -/ -theorem step_setAggregateFieldAt_home {memory : Memory} {home : Reference} +theorem step_stfld_aggregate_home {memory : Memory} {home : Reference} {bytes : List (BitVec 8)} {field : FieldLayout} (formed : memory.WellFormed) (loaded : read memory home bytes.length 1 = .ok bytes) (writable : access memory home bytes.length 1 true = .ok ()) (within : field.offset + field.width ≤ bytes.length) (positive : 0 < field.width) {fieldBytes : List (BitVec 8)} (fieldSized : fieldBytes.length = field.width) : ∃ after, - (∀ body pc args frame rest, - step body (.setAggregateFieldAt field.offset field.width) pc args frame + (∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .aggregate field.offset field.width → ∀ pc args frame rest, + step body (.stfld operand) pc args frame (.scalar (.aggregate fieldBytes) :: .reference (.address home) :: rest) memory = .ok (.next (pc + 1) rest frame after)) ∧ write memory { home with offset := home.offset + field.offset } fieldBytes 1 = @@ -442,100 +489,105 @@ theorem step_setAggregateFieldAt_home {memory : Memory} {home : Reference} have address := field_address formed writable positive within refine ⟨after, ?_, written, read_after_field_write loaded within (by rw [encoding]; exact written)⟩ - intro body pc args frame rest - rw [step_setAggregateFieldAt_reference_eq] + intro body operand shape pc args frame rest + rw [step_stfld_aggregate_reference_eq _ _ _ _ _ _ _ _ shape] simp [fieldSized, address, written, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] #print axioms writeSnapshot_initialized #print axioms loadValue_signed_of_read -#print axioms step_setFieldAt_v128_eq -#print axioms step_setFieldAt_v256_eq -#print axioms step_setAggregateFieldAt_reference_eq -#print axioms step_aggregateFieldAt_reference_eq +#print axioms step_stfld_value_v128_eq +#print axioms step_stfld_value_v256_eq +#print axioms step_stfld_aggregate_reference_eq +#print axioms step_ldfld_aggregate_reference_eq +#print axioms step_stfld_aggregate_snapshot_eq #print axioms field_address -#print axioms step_setFieldAt_home -#print axioms step_setAggregateFieldAt_home +#print axioms step_stfld_value_home +#print axioms step_stfld_aggregate_home /-- `ldfld` through the address of a struct home readable as `bytes` pushes the field's number at the field's width and changes nothing. The step holds in any method and frame. -/ -theorem step_fieldAt_home {memory : Memory} {home : Reference} {bytes : List (BitVec 8)} +theorem step_ldfld_unsigned_home {memory : Memory} {home : Reference} {bytes : List (BitVec 8)} {field : FieldLayout} (formed : memory.WellFormed) (loaded : read memory home bytes.length 1 = .ok bytes) (within : field.offset + field.width ≤ bytes.length) (scalar : ScalarWidth field.width) : - ∀ body pc args frame rest, - step body (.fieldAt field.offset field.width false) pc args frame + ∀ (body : Method) (operand : Extraction.FieldRef), + body.fieldShape operand = .value field.offset field.width false → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) memory = .ok (.next (pc + 1) (.scalar (numericOfWidth field.width (field.numberIn bytes)) :: rest) frame memory) := by - intro body pc args frame rest + intro body operand shape pc args frame rest have positive : 0 < field.width := by rcases scalar with h | h | h | h | h | h | h <;> omega have address := field_address formed (read_access loaded) positive within have slice := read_slice_bytes formed loaded field.offset field.width positive within - rw [step_fieldAt_reference_eq] + rw [step_ldfld_value_reference_eq _ _ _ _ _ _ _ _ _ shape] simp [address, loadValue_of_read scalar slice, FieldLayout.numberIn, FieldLayout.bytesIn, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] /-- A signed `ldfld` through the address of a struct home readable as `bytes` pushes the field's number sign extended from a 1- or 2-byte field, and changes nothing. The step holds in any method and frame. -/ -theorem step_fieldAt_signed_home {memory : Memory} {home : Reference} +theorem step_ldfld_signed_home {memory : Memory} {home : Reference} {bytes : List (BitVec 8)} {field : FieldLayout} (formed : memory.WellFormed) (loaded : read memory home bytes.length 1 = .ok bytes) (within : field.offset + field.width ≤ bytes.length) (scalar : ScalarWidth field.width) : - ∀ body pc args frame rest, - step body (.fieldAt field.offset field.width true) pc args frame + ∀ (body : Method) (operand : Extraction.FieldRef), + body.fieldShape operand = .value field.offset field.width true → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) memory = .ok (.next (pc + 1) (.scalar (signedOfWidth field.width (field.numberIn bytes)) :: rest) frame memory) := by - intro body pc args frame rest + intro body operand shape pc args frame rest have positive : 0 < field.width := by rcases scalar with h | h | h | h | h | h | h <;> omega have address := field_address formed (read_access loaded) positive within have slice := read_slice_bytes formed loaded field.offset field.width positive within - rw [step_fieldAt_reference_eq] + rw [step_ldfld_value_reference_eq _ _ _ _ _ _ _ _ _ shape] simp [address, loadValue_signed_of_read scalar slice, FieldLayout.numberIn, FieldLayout.bytesIn, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] /-- A nested struct load through the address of a struct home readable as `bytes` pushes the field's bytes and changes nothing. The step holds in any method and frame. -/ -theorem step_aggregateFieldAt_home {memory : Memory} {home : Reference} +theorem step_ldfld_aggregate_home {memory : Memory} {home : Reference} {bytes : List (BitVec 8)} {field : FieldLayout} (formed : memory.WellFormed) (loaded : read memory home bytes.length 1 = .ok bytes) (within : field.offset + field.width ≤ bytes.length) (positive : 0 < field.width) : - ∀ body pc args frame rest, - step body (.aggregateFieldAt field.offset field.width) pc args frame + ∀ (body : Method) (operand : Extraction.FieldRef), + body.fieldShape operand = .aggregate field.offset field.width → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) memory = .ok (.next (pc + 1) (.scalar (.aggregate (field.bytesIn bytes)) :: rest) frame memory) := by - intro body pc args frame rest + intro body operand shape pc args frame rest have address := field_address formed (read_access loaded) positive within have slice := read_slice_bytes formed loaded field.offset field.width positive within - rw [step_aggregateFieldAt_reference_eq] + rw [step_ldfld_aggregate_reference_eq _ _ _ _ _ _ _ _ shape] simp [address, slice, FieldLayout.bytesIn, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] /-- `initobj` on a writable home zeroes its `size` bytes, which then read back. The step holds in any method and frame. -/ -theorem step_initAggregate_home {memory : Memory} {home : Reference} {size : Nat} +theorem step_initobj_home {memory : Memory} {home : Reference} {size : Nat} (writable : access memory home size 1 true = .ok ()) : ∃ after, - (∀ body pc args frame rest, - step body (.initAggregate size) pc args frame (.reference (.address home) :: rest) - memory = .ok (.next (pc + 1) rest frame after)) ∧ + (∀ (body : Method) (type : Extraction.TypeExpr), (body.valueShape type).size? = some size → + ∀ pc args frame rest, + step body (.initobj type) pc args frame (.reference (.address home) :: rest) memory = + .ok (.next (pc + 1) rest frame after)) ∧ write memory home (List.replicate size 0) 1 = .ok after ∧ read after home size 1 = .ok (List.replicate size 0) := by obtain ⟨after, written⟩ := write_succeeds (bytes := List.replicate size 0) (alignment := 1) (by simpa using writable) refine ⟨after, ?_, written, by simpa using write_readback _ _ _ _ _ written⟩ - intro body pc args frame rest - rw [step_initAggregate_reference_eq] + intro body type shape pc args frame rest + rw [step_initobj_reference_eq _ _ _ _ _ shape] simp only [referenceAt, ordinaryAccessAlignment, written, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] -#print axioms step_fieldAt_home -#print axioms step_fieldAt_signed_home -#print axioms step_aggregateFieldAt_home -#print axioms step_initAggregate_home +#print axioms step_ldfld_unsigned_home +#print axioms step_ldfld_signed_home +#print axioms step_ldfld_aggregate_home +#print axioms step_initobj_home /-- A remembered aggregate local reads as exactly its remembered bytes, which fill its home. -/ theorem LocalSnapshots.aggregate {memory : Memory} {slots : List LocalSlot} @@ -572,16 +624,17 @@ theorem LocalSnapshots.zero_aggregate {entered memory : Memory} {boundary : Nat} (slot : slots[index]? = some (.bytes (.aggregate ⟨width, bounded⟩) home)) (writable : access memory home width 1 true = .ok ()) : ∃ after, - (∀ body pc args frame rest, - step body (.initAggregate width) pc args frame (.reference (.address home) :: rest) - memory = .ok (.next (pc + 1) rest frame after)) ∧ + (∀ (body : Method) (type : Extraction.TypeExpr), (body.valueShape type).size? = some width → + ∀ pc args frame rest, + step body (.initobj type) pc args frame (.reference (.address home) :: rest) memory = + .ok (.next (pc + 1) rest frame after)) ∧ LocalSnapshots after slots (rememberLocal known index (.aggregate (List.replicate width 0))) ∧ after.WellFormed ∧ MemoryBelow boundary memory after ∧ access after home width 1 true = .ok () ∧ form after home = .ok home ∧ write memory home (List.replicate width 0) 1 = .ok after := by let size : Fin (2^64) := ⟨width, bounded⟩ - obtain ⟨after, stepped, written, _⟩ := step_initAggregate_home writable + obtain ⟨after, stepped, written, _⟩ := step_initobj_home writable obtain ⟨_, ready⟩ := access_requirements writable have encoded : localNumber (.aggregate size) (.aggregate (List.replicate size 0)) = .ok (byteNumber (List.replicate size 0)) := by @@ -678,8 +731,9 @@ theorem LocalSnapshots.store_field {entered memory : Memory} {boundary : Nat} {k (found : layout.fields[fieldIndex]? = some ⟨offset, width, aggregate⟩) (value : CIL.Value) {number : Nat} (encoded : fieldStoreNumber width value = some number) : ∃ after, - (∀ body pc args frame rest, - step body (.setFieldAt offset width) pc args frame + (∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args frame rest, + step body (.stfld operand) pc args frame (.scalar value :: .reference (.address home) :: rest) memory = .ok (.next (pc + 1) rest frame after)) ∧ LocalSnapshots after slots (rememberLocal known index @@ -694,7 +748,7 @@ theorem LocalSnapshots.store_field {entered memory : Memory} {boundary : Nat} {k obtain ⟨length, loaded⟩ := snapshots.aggregate slot specified have within : offset + width ≤ bytes.length := (length.trans sized.symm) ▸ field_within_layout layout valid _ (List.mem_of_getElem? found) - obtain ⟨after, stepped, written, -⟩ := step_setFieldAt_home + obtain ⟨after, stepped, written, -⟩ := step_stfld_value_home (field := ⟨offset, width, aggregate⟩) formed loaded (length ▸ writable) within encoded obtain ⟨remembered, formedAfter, below, kept, formsAfter, changed⟩ := snapshots.write_field homes formed slot specified writable valid disjoint sized decoded found @@ -719,8 +773,9 @@ theorem LocalSnapshots.store_aggregate_field {entered memory : Memory} {boundary (found : layout.fields[fieldIndex]? = some ⟨offset, width, aggregate⟩) (fieldBytes : List (BitVec 8)) (fieldSized : fieldBytes.length = width) : ∃ after, - (∀ body pc args frame rest, - step body (.setAggregateFieldAt offset width) pc args frame + (∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .aggregate offset width → ∀ pc args frame rest, + step body (.stfld operand) pc args frame (.scalar (.aggregate fieldBytes) :: .reference (.address home) :: rest) memory = .ok (.next (pc + 1) rest frame after)) ∧ LocalSnapshots after slots (rememberLocal known index (.aggregate @@ -736,7 +791,7 @@ theorem LocalSnapshots.store_aggregate_field {entered memory : Memory} {boundary have within : offset + width ≤ bytes.length := (length.trans sized.symm) ▸ field_within_layout layout valid _ member have positive := valid.positive member - obtain ⟨after, stepped, written, -⟩ := step_setAggregateFieldAt_home + obtain ⟨after, stepped, written, -⟩ := step_stfld_aggregate_home (field := ⟨offset, width, aggregate⟩) formed loaded (length ▸ writable) within positive fieldSized have fits : byteNumber fieldBytes < 256 ^ width := fieldSized ▸ byteNumber_lt fieldBytes @@ -778,14 +833,15 @@ theorem LocalSnapshots.load_field {memory : Memory} {slots : List LocalSlot} (decoded : layout.values bytes = values) {fieldIndex offset width : Nat} (found : layout.fields[fieldIndex]? = some ⟨offset, width, false⟩) {value : Nat} (present : values[fieldIndex]? = some value) : - ∀ body pc args frame rest, - step body (.fieldAt offset width false) pc args frame (.reference (.address home) :: rest) + ∀ (body : Method) (operand : Extraction.FieldRef), + body.fieldShape operand = .value offset width false → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) memory = .ok (.next (pc + 1) (.scalar (numericOfWidth width value) :: rest) frame memory) := by obtain ⟨loaded, within, number⟩ := snapshots.field_value slot specified valid sized decoded found present rw [← number] - exact step_fieldAt_home (field := ⟨offset, width, false⟩) formed loaded within + exact step_ldfld_unsigned_home (field := ⟨offset, width, false⟩) formed loaded within (valid.scalar (List.mem_of_getElem? found)) /-- A signed `ldfld` of field `fieldIndex` through the address of a remembered aggregate local @@ -800,14 +856,15 @@ theorem LocalSnapshots.load_signed_field {memory : Memory} {slots : List LocalSl (decoded : layout.values bytes = values) {fieldIndex offset width : Nat} (found : layout.fields[fieldIndex]? = some ⟨offset, width, false⟩) {value : Nat} (present : values[fieldIndex]? = some value) : - ∀ body pc args frame rest, - step body (.fieldAt offset width true) pc args frame (.reference (.address home) :: rest) + ∀ (body : Method) (operand : Extraction.FieldRef), + body.fieldShape operand = .value offset width true → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) memory = .ok (.next (pc + 1) (.scalar (signedOfWidth width value) :: rest) frame memory) := by obtain ⟨loaded, within, number⟩ := snapshots.field_value slot specified valid sized decoded found present rw [← number] - exact step_fieldAt_signed_home (field := ⟨offset, width, false⟩) formed loaded within + exact step_ldfld_signed_home (field := ⟨offset, width, false⟩) formed loaded within (valid.scalar (List.mem_of_getElem? found)) /-- A nested struct load of field `fieldIndex` through the address of a remembered aggregate @@ -822,8 +879,9 @@ theorem LocalSnapshots.load_aggregate_field {memory : Memory} {slots : List Loca (decoded : layout.values bytes = values) {fieldIndex offset width : Nat} {aggregate : Bool} (found : layout.fields[fieldIndex]? = some ⟨offset, width, aggregate⟩) {value : Nat} (present : values[fieldIndex]? = some value) : - ∀ body pc args frame rest, - step body (.aggregateFieldAt offset width) pc args frame + ∀ (body : Method) (operand : Extraction.FieldRef), + body.fieldShape operand = .aggregate offset width → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) memory = .ok (.next (pc + 1) (.scalar (.aggregate (numberBytes value width)) :: rest) frame memory) := by @@ -842,7 +900,7 @@ theorem LocalSnapshots.load_aggregate_field {memory : Memory} {slots : List Loca rw [← number] exact roundTrip rw [encoding] - exact step_aggregateFieldAt_home (field := ⟨offset, width, aggregate⟩) formed loaded within + exact step_ldfld_aggregate_home (field := ⟨offset, width, aggregate⟩) formed loaded within positive #print axioms LocalSnapshots.aggregate diff --git a/CIL/Safety/AggregateHomeValues.lean b/CIL/Safety/AggregateHomeValues.lean index 8a5515df7..ec1e552c7 100644 --- a/CIL/Safety/AggregateHomeValues.lean +++ b/CIL/Safety/AggregateHomeValues.lean @@ -1,4 +1,6 @@ -import CIL.Safety.ArgumentHomeValues +import CIL.Safety.HomeProgress +import CIL.Safety.FrameMemoryBelow +import CIL.Safety.WriteEffects import CIL.Safety.ByteRoundTrip /-! @@ -70,7 +72,7 @@ theorem allocate_initialized_aggregate (memory : Memory) (activation : Nat) (allocateHome_fresh _ _ _ _ _ home).2.1⟩ /-- By-value aggregate parameters get private initialized copies of their actual - bytes, using the emitted parameterHomes path rather than vector arguments. -/ + bytes, through the parameterHomes path. -/ theorem make_parameter_home_aggregate (memory : Memory) (activation index : Nat) (args : List Value) (size : Fin (2^64)) (bytes : List (BitVec 8)) (length : bytes.length = size) (wellFormed : memory.WellFormed) diff --git a/CIL/Safety/AggregateLocals.lean b/CIL/Safety/AggregateLocals.lean index b6050cdcf..3470e0b7b 100644 --- a/CIL/Safety/AggregateLocals.lean +++ b/CIL/Safety/AggregateLocals.lean @@ -20,7 +20,7 @@ theorem step_store_aggregate {body : CIL.Method} {pc index watermark : Nat} (ready : access memory reference size 1 true = .ok ()) (privateHome : watermark ≤ reference.allocation) : ∃ result, - step body (.setLocal index) pc args frame (.scalar (.aggregate bytes) :: rest) memory = + step body (.stloc index) pc args frame (.scalar (.aggregate bytes) :: rest) memory = .ok (.next (pc + 1) rest frame result) ∧ write memory reference bytes 1 = .ok result ∧ read result reference size 1 = .ok bytes ∧ @@ -40,7 +40,7 @@ theorem step_store_aggregate {body : CIL.Method} {pc index watermark : Nat} refine ⟨result, ?_, written, loaded, value, allocations, write_preserves_memory_below _ _ _ _ _ _ privateHome written⟩ simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, - Bool.false_or, Bool.false_eq_true, ite_false, slot, stored, unchanged, Bind.bind, + Bool.false_eq_true, ite_false, slot, stored, unchanged, Bind.bind, Except.bind, Pure.pure, Except.pure] /-- Loading an aggregate local pushes the bytes its home currently reads. -/ @@ -49,10 +49,10 @@ theorem step_load_aggregate {body : CIL.Method} {pc index : Nat} {size : Fin (2^64)} {bytes : List (BitVec 8)} (slot : frame.locals[index]? = some (.bytes (.aggregate size) reference)) (loaded : read memory reference size 1 = .ok bytes) : - step body (.local index) pc args frame stack memory = + step body (.ldloc index) pc args frame stack memory = .ok (.next (pc + 1) (.scalar (.aggregate bytes) :: stack) frame memory) := by simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, - Bool.false_or, Bool.false_eq_true, ite_false, slot, load_local_aggregate_of_read loaded, + Bool.false_eq_true, ite_false, slot, load_local_aggregate_of_read loaded, Bind.bind, Except.bind, Pure.pure, Except.pure] #print axioms step_store_aggregate diff --git a/CIL/Safety/AggregateMemorySteps.lean b/CIL/Safety/AggregateMemorySteps.lean index 74a21aee5..9de0ca037 100644 --- a/CIL/Safety/AggregateMemorySteps.lean +++ b/CIL/Safety/AggregateMemorySteps.lean @@ -37,81 +37,109 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem /-- Loading an aggregate copies its snapshot without requiring initialized bytes. -/ @[cil_instruction_step high] -theorem step_loadAggregate_address_eq (width : Nat) (reference : Reference) +theorem step_ldobj_aggregate_address_eq {type : CIL.Extraction.TypeExpr} (width : Nat) + (shape : body.valueShape type = .aggregate width) (reference : Reference) (rest : List Value) : - step body (.loadAggregate width) pc args frame (.reference (.address reference) :: rest) + step body (.ldobj type) pc args frame (.reference (.address reference) :: rest) memory = do let value ← loadSnapshot memory (.address reference) (.aggregate width) return .next (pc + 1) (.scalar value :: rest) frame memory := by - rw [step_loadAggregate_eq, ← aggregate_load_snapshot memory reference width] - simp only [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, + rw [step_ldobj_aggregate body pc args frame memory shape, + ← aggregate_load_snapshot memory reference width] + simp only [typedTransferCall, observedTransfer, transferValues, cil_guard, + unspecifiedOperands, List.take, List.any, Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false] split <;> first | rfl | simp_all only [ordinaryAccessAlignment, bind_assoc, pure_bind] -#print axioms step_loadAggregate_address_eq +#print axioms step_ldobj_aggregate_address_eq /-- A readonly reference permits the same aggregate snapshot without write authority. -/ @[cil_instruction_step high] -theorem step_loadAggregate_readOnlyAddress_eq (width : Nat) (reference : Reference) +theorem step_ldobj_aggregate_readOnlyAddress_eq {type : CIL.Extraction.TypeExpr} (width : Nat) + (shape : body.valueShape type = .aggregate width) (reference : Reference) (rest : List Value) : - step body (.loadAggregate width) pc args frame (.readOnlyReference (.address reference) :: rest) + step body (.ldobj type) pc args frame (.readOnlyReference (.address reference) :: rest) memory = do let value ← loadSnapshot memory (.address reference) (.aggregate width) return .next (pc + 1) (.scalar value :: rest) frame memory := by - rw [step_loadAggregate_eq, ← aggregate_load_snapshot memory reference width] - simp only [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, + rw [step_ldobj_aggregate body pc args frame memory shape, + ← aggregate_load_snapshot memory reference width] + simp only [typedTransferCall, observedTransfer, transferValues, cil_guard, + unspecifiedOperands, List.take, List.any, Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false] split <;> first | rfl | simp_all only [ordinaryAccessAlignment, bind_assoc, pure_bind] -#print axioms step_loadAggregate_readOnlyAddress_eq +#print axioms step_ldobj_aggregate_readOnlyAddress_eq + +/-- A checked snapshot determines the continuation of an aggregate load through either + kind of managed reference. -/ +theorem step_ldobj_aggregate_of_snapshot {type : CIL.Extraction.TypeExpr} {width : Nat} + {reference : Reference} {value : CIL.Value} (readonly : Bool) (rest : List Value) + (shape : body.valueShape type = .aggregate width) + (loaded : loadSnapshot memory (.address reference) (.aggregate width) = .ok value) : + step body (.ldobj type) pc args frame + ((if readonly then .readOnlyReference (.address reference) + else .reference (.address reference)) :: rest) memory = + .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := by + cases readonly <;> + simp only [Bool.false_eq_true, ite_false, ite_true, + step_ldobj_aggregate_address_eq body pc args frame memory width shape, + step_ldobj_aggregate_readOnlyAddress_eq body pc args frame memory width shape, loaded] <;> rfl +#print axioms step_ldobj_aggregate_of_snapshot /-- A null aggregate source retains the requested width in its fault diagnostic. -/ @[cil_instruction_step high] -theorem step_loadAggregate_null_eq (width : Nat) (rest : List Value) : - step body (.loadAggregate width) pc args frame (.reference .null :: rest) memory = +theorem step_ldobj_aggregate_null_eq {type : CIL.Extraction.TypeExpr} (width : Nat) + (shape : body.valueShape type = .aggregate width) (rest : List Value) : + step body (.ldobj type) pc args frame (.reference .null :: rest) memory = .error (.memory .nullDereference .null width) := by - rw [step_loadAggregate_eq] + rw [step_ldobj_aggregate body pc args frame memory shape] rfl -#print axioms step_loadAggregate_null_eq +#print axioms step_ldobj_aggregate_null_eq /-- A readonly null source reports the same requested aggregate width. -/ @[cil_instruction_step high] -theorem step_loadAggregate_readOnlyNull_eq (width : Nat) (rest : List Value) : - step body (.loadAggregate width) pc args frame (.readOnlyReference .null :: rest) memory = +theorem step_ldobj_aggregate_readOnlyNull_eq {type : CIL.Extraction.TypeExpr} (width : Nat) + (shape : body.valueShape type = .aggregate width) (rest : List Value) : + step body (.ldobj type) pc args frame (.readOnlyReference .null :: rest) memory = .error (.memory .nullDereference .null width) := by - rw [step_loadAggregate_eq] + rw [step_ldobj_aggregate body pc args frame memory shape] rfl -#print axioms step_loadAggregate_readOnlyNull_eq +#print axioms step_ldobj_aggregate_readOnlyNull_eq /-- An initialized aggregate store checks its width and writes its bytes. -/ @[cil_instruction_step high] -theorem step_storeAggregate_reference_eq (width : Nat) (bytes : List (BitVec 8)) +theorem step_stobj_initialized_eq {type : CIL.Extraction.TypeExpr} (width : Nat) + (shape : body.valueShape type = .aggregate width) (bytes : List (BitVec 8)) (reference : ManagedReference) (rest : List Value) : - step body (.storeAggregate width) pc args frame + step body (.stobj type) pc args frame (.scalar (.aggregate bytes) :: .reference reference :: rest) memory = if bytes.length != width then .error .invalidState else do let address ← referenceAt reference let stored ← checkedAt reference width (write memory address bytes 1) return .next (pc + 1) rest frame stored := by - rw [step_storeAggregate_eq] - simp only [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, + rw [step_stobj_aggregate body pc args frame memory shape] + simp only [typedTransferCall, observedTransfer, ite_true, transferValues, cil_guard, + unspecifiedOperands, List.take, List.any, Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false] split <;> first | rfl | simp_all only [ordinaryAccessAlignment, bind_assoc, pure_bind] -#print axioms step_storeAggregate_reference_eq +#print axioms step_stobj_initialized_eq /-- A snapshot store checks both widths and preserves its initialization flags. -/ @[cil_instruction_step high] -theorem step_storeAggregate_snapshot_eq (width actual : Nat) (bytes : List (BitVec 8 × Bool)) +theorem step_stobj_snapshot_eq {type : CIL.Extraction.TypeExpr} (width actual : Nat) + (shape : body.valueShape type = .aggregate width) (bytes : List (BitVec 8 × Bool)) (reference : ManagedReference) (rest : List Value) : - step body (.storeAggregate width) pc args frame + step body (.stobj type) pc args frame (.scalar (.snapshot (.aggregate actual) bytes) :: .reference reference :: rest) memory = if bytes.length != width || actual != width then .error .invalidState else do let address ← referenceAt reference let stored ← checkedAt reference width (writeSnapshot memory address bytes 1) return .next (pc + 1) rest frame stored := by - rw [step_storeAggregate_eq] - simp only [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, + rw [step_stobj_aggregate body pc args frame memory shape] + simp only [typedTransferCall, observedTransfer, ite_true, transferValues, cil_guard, + unspecifiedOperands, List.take, List.any, Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false] split <;> first | rfl | simp_all only [ordinaryAccessAlignment, bind_assoc, pure_bind] -#print axioms step_storeAggregate_snapshot_eq +#print axioms step_stobj_snapshot_eq end Equations /-- A checked snapshot read gives the aggregate value with the same initialization mask. -/ diff --git a/CIL/Safety/AllocatingStepRenaming.lean b/CIL/Safety/AllocatingStepRenaming.lean index 0bd631088..371168b41 100644 --- a/CIL/Safety/AllocatingStepRenaming.lean +++ b/CIL/Safety/AllocatingStepRenaming.lean @@ -3,6 +3,8 @@ import CIL.Safety.StepComposition import CIL.Safety.StaticInstructionRenaming import CIL.Safety.StackAllocationRenaming import CIL.Safety.ArrayAllocationRenaming +import CIL.Safety.InstructionRenaming +import CIL.Safety.SpanHomeRenaming /-! # Checked allocating instruction steps @@ -14,42 +16,42 @@ The memory case reuses the checked step equation instead of simplifying the full namespace CIL.Safety -/-- Static dispatch depends on extraction-bound descriptors, not unrelated method metadata. -/ -theorem staticInstruction_metadata {sourceBody targetBody : CIL.Method} - (metadata : StepMetadata sourceBody targetBody) (pc : Nat) (operation : CIL.MemoryOp) - (stack : List Value) (memory : Memory) : +-- Stack lists are renamed by the local map of the frame that holds them. +variable {indices : LocalIndexMap} + +/-- Memory dispatch reads neither the method nor the pc, so two bodies agree on it. -/ +theorem staticInstruction_body (sourceBody targetBody : CIL.Method) (pc : Nat) + (operation : CIL.MemoryOp) (stack : List Value) (memory : Memory) : staticInstruction targetBody pc operation stack memory = - staticInstruction sourceBody pc operation stack memory := by - unfold staticInstruction - rw [metadata.staticSites] -#print axioms staticInstruction_metadata + staticInstruction sourceBody pc operation stack memory := rfl +#print axioms staticInstruction_body -/-- Checked static-memory steps extend the map only when their actual operation allocates. -/ -theorem Memory.Renamed.stepMemory {indices : LocalIndexMap} {mapping : AllocationRenaming} +/-- Checked memory steps, span constructors over static storage included, extend the map only + when their actual operation allocates. -/ +theorem Memory.Renamed.memoryModelStep {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) - (staticBounds : ∀ binding ∈ source.staticBindings, - binding.2.allocation < mapping.sourceLimit) - (sourceBody targetBody : CIL.Method) (metadata : StepMetadata sourceBody targetBody) - (operation : CIL.MemoryOp) (pc : Nat) (arguments stack : List Value) + (sourceFormed : source.WellFormed) (sourceBody targetBody : CIL.Method) + (operation : CIL.MemoryOp) (pc : Nat) (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (observable : SuccessOrThrow - (step sourceBody (.memory operation) pc arguments sourceFrame stack source)) : + (modelStep (some (.memory operation)) sourceBody pc sourceFrame stack source)) : ∃ later, mapping.Extends later ∧ ResultRenamed later (FrameAction.Renamed indices later scopes) - (step sourceBody (.memory operation) pc arguments sourceFrame stack source) - (step targetBody (.memory operation) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step_memory_eq, mapping.unspecifiedOperands] at observable ⊢ + (modelStep (some (.memory operation)) sourceBody pc sourceFrame stack source) + (modelStep (some (.memory operation)) targetBody pc targetFrame (stack.map (indices.value mapping)) target) := by + simp only [modelStep_memory, indices.unspecifiedOperands mapping] at observable ⊢ split at observable · cases observable · rename_i guard simp only [guard, Bool.false_eq_true, ite_false, - staticInstruction_metadata metadata] at observable ⊢ + staticInstruction_body sourceBody targetBody] at observable ⊢ + apply (frames.viewOperands related (viewOperandCount operation) stack bounds).bindExtending + intro viewed otherViewed sourceViewed operandsRelated + obtain ⟨rfl, viewedBounds⟩ := operandsRelated obtain ⟨later, extension, operations⟩ := related.staticInstructionResult sourceFormed - targetFormed staticBounds sourceBody pc operation stack bounds observable.bind_left + sourceBody pc operation viewed viewedBounds (observable.bind_right sourceViewed).bind_left refine ⟨later, extension, ?_⟩ apply operations.bind intro first second result @@ -57,11 +59,338 @@ theorem Memory.Renamed.stepMemory {indices : LocalIndexMap} {mapping : Allocatio rcases second with ⟨targetAfter, targetValues⟩ rcases result with ⟨memories, rfl⟩ exact .ok (.next memories (frames.extend extension (fun _ _ scope => scope))) -#print axioms Memory.Renamed.stepMemory +#print axioms Memory.Renamed.memoryModelStep + +/-- A typed element access after its operand check touches the mapped element and keeps the + allocation map: both bodies give the element the same shape. -/ +private theorem Memory.Renamed.typedElementStep {indices : LocalIndexMap} + {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} + {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (sourceFormed : source.WellFormed) (sourceBody targetBody : CIL.Method) + (metadata : StepMetadata sourceBody targetBody) (element : CIL.Extraction.TypeExpr) + (store : Bool) (pc : Nat) (stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (do + let (memory, values) ← + typedElementInstruction (sourceBody.elementShape element) store stack source + return FrameAction.next (pc + 1) values sourceFrame memory)) : + ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (do + let (memory, values) ← + typedElementInstruction (sourceBody.elementShape element) store stack source + return FrameAction.next (pc + 1) values sourceFrame memory) + (do + let (memory, values) ← typedElementInstruction (targetBody.elementShape element) store + (stack.map (indices.value mapping)) target + return FrameAction.next (pc + 1) values targetFrame memory) := by + have shaped : targetBody.elementShape element = sourceBody.elementShape element := by + simp only [CIL.Method.elementShape, metadata.environment] + rw [shaped] + apply (related.typedElementInstructionResult _ store sourceFormed stack bounds + observable.bind_left).bind + intro first second result + rcases first with ⟨sourceAfter, values⟩ + rcases second with ⟨targetAfter, targetValues⟩ + rcases result with ⟨memories, rfl⟩ + exact .ok (.next memories frames) +#print axioms Memory.Renamed.typedElementStep + +/-- A checked typed `ldelem` step keeps the allocation map. -/ +theorem Memory.Renamed.stepLdelemAny {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} + (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (sourceFormed : source.WellFormed) (sourceBody targetBody : CIL.Method) + (metadata : StepMetadata sourceBody targetBody) (element : CIL.Extraction.TypeExpr) + (pc : Nat) (arguments stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + (step sourceBody (.ldelem_any element) pc arguments sourceFrame stack source)) : + ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (step sourceBody (.ldelem_any element) pc arguments sourceFrame stack source) + (step targetBody (.ldelem_any element) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + simp only [step_ldelem_any_eq, indices.unspecifiedOperands mapping] at observable ⊢ + split at observable + · cases observable + · rename_i guard + simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ + exact related.typedElementStep frames sourceFormed sourceBody targetBody metadata element + false pc stack bounds observable +#print axioms Memory.Renamed.stepLdelemAny + +/-- A checked typed `stelem` step keeps the allocation map. -/ +theorem Memory.Renamed.stepStelemAny {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} + (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (sourceFormed : source.WellFormed) (sourceBody targetBody : CIL.Method) + (metadata : StepMetadata sourceBody targetBody) (element : CIL.Extraction.TypeExpr) + (pc : Nat) (arguments stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + (step sourceBody (.stelem_any element) pc arguments sourceFrame stack source)) : + ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (step sourceBody (.stelem_any element) pc arguments sourceFrame stack source) + (step targetBody (.stelem_any element) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + simp only [step_stelem_any_eq, indices.unspecifiedOperands mapping] at observable ⊢ + split at observable + · cases observable + · rename_i guard + simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ + exact related.typedElementStep frames sourceFormed sourceBody targetBody metadata element + true pc stack bounds observable +#print axioms Memory.Renamed.stepStelemAny + +/-- A checked `ldelema` step keeps the allocation map: both bodies give the element the same + shape. -/ +theorem Memory.Renamed.stepLdelema {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} + (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (sourceFormed : source.WellFormed) (sourceBody targetBody : CIL.Method) + (metadata : StepMetadata sourceBody targetBody) (element : CIL.Extraction.TypeExpr) + (readOnly : Bool) (pc : Nat) (arguments stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + (step sourceBody (.ldelema element readOnly) pc arguments sourceFrame stack source)) : + ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (step sourceBody (.ldelema element readOnly) pc arguments sourceFrame stack source) + (step targetBody (.ldelema element readOnly) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + simp only [step_ldelema_eq, indices.unspecifiedOperands mapping] at observable ⊢ + split at observable + · cases observable + · rename_i guard + simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ + have shaped : targetBody.elementShape element = sourceBody.elementShape element := by + simp only [CIL.Method.elementShape, metadata.environment] + rw [shaped] + apply (related.elementAddressInstructionResult _ readOnly sourceFormed stack bounds + observable.bind_left).bind + intro first second result + rcases first with ⟨sourceAfter, values⟩ + rcases second with ⟨targetAfter, targetValues⟩ + rcases result with ⟨memories, rfl⟩ + exact .ok (.next memories frames) +#print axioms Memory.Renamed.stepLdelema + +/-- `ldsfld` reads the descriptor bound to its pc, which a step renaming keeps. -/ +theorem staticLoad_metadata {sourceBody targetBody : CIL.Method} + (metadata : StepMetadata sourceBody targetBody) (pc : Nat) (memory : Memory) : + staticLoad targetBody pc memory = staticLoad sourceBody pc memory := by + unfold staticLoad CIL.Method.staticAt + rw [metadata.staticSites] +#print axioms staticLoad_metadata + +/-- `ldsflda` reads the descriptor bound to its pc, which a step renaming keeps. -/ +theorem staticAddress_metadata {sourceBody targetBody : CIL.Method} + (metadata : StepMetadata sourceBody targetBody) (pc : Nat) (memory : Memory) : + staticAddress targetBody pc memory = staticAddress sourceBody pc memory := by + unfold staticAddress CIL.Method.staticAt + rw [metadata.staticSites] +#print axioms staticAddress_metadata + +/-- A checked `ldsfld` step keeps the allocation map. -/ +theorem Memory.Renamed.stepLdsfld {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} + (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (staticBounds : ∀ binding ∈ source.staticBindings, + binding.2.allocation < mapping.sourceLimit) + (sourceBody targetBody : CIL.Method) (metadata : StepMetadata sourceBody targetBody) + (field : CIL.Extraction.FieldRef) (pc : Nat) (arguments stack : List Value) : + ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (step sourceBody (.ldsfld field) pc arguments sourceFrame stack source) + (step targetBody (.ldsfld field) pc (arguments.map mapping.value) targetFrame + (stack.map (indices.value mapping)) target) := by + show ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (do + let (memory, value) ← staticLoad sourceBody pc source + return FrameAction.next (pc + 1) (value :: stack) sourceFrame memory) + (do + let (memory, value) ← staticLoad targetBody pc target + return FrameAction.next (pc + 1) (value :: stack.map (indices.value mapping)) targetFrame + memory) + rw [staticLoad_metadata metadata] + apply (related.staticLoadResult staticBounds sourceBody pc).bindWithSuccess + intro first second loaded result + rcases first with ⟨sourceAfter, value⟩ + rcases second with ⟨targetAfter, targetValue⟩ + rcases result with ⟨memories, rfl⟩ + dsimp only + rw [indices.push_of_not_home mapping stack (staticLoad_not_spanHome loaded)] + exact .ok (.next memories frames) +#print axioms Memory.Renamed.stepLdsfld + +/-- A checked `ldsflda` or field `ldtoken` step extends the map only when the field's storage + is allocated. -/ +theorem Memory.Renamed.stepStaticAddress {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} + (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) + (staticBounds : ∀ binding ∈ source.staticBindings, + binding.2.allocation < mapping.sourceLimit) + (sourceBody targetBody : CIL.Method) (metadata : StepMetadata sourceBody targetBody) + (operation : CIL.Instruction) + (addressed : ∀ body pc args frame stack memory, + step body operation pc args frame stack memory = do + let (memory, value) ← staticAddress body pc memory + return FrameAction.next (pc + 1) (value :: stack) frame memory) + (pc : Nat) (arguments stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + (step sourceBody operation pc arguments sourceFrame stack source)) : + ∃ later, mapping.Extends later ∧ + ResultRenamed later (FrameAction.Renamed indices later scopes) + (step sourceBody operation pc arguments sourceFrame stack source) + (step targetBody operation pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + simp only [addressed, staticAddress_metadata metadata] at observable ⊢ + obtain ⟨later, extension, operations⟩ := related.staticAddressResult sourceFormed + targetFormed staticBounds sourceBody pc observable.bind_left + refine ⟨later, extension, ?_⟩ + apply operations.bindWithSuccess + intro first second addressed result + rcases first with ⟨sourceAfter, value⟩ + rcases second with ⟨targetAfter, targetValue⟩ + rcases result with ⟨memories, rfl⟩ + dsimp only + rw [← indices.values_extends extension stack bounds, + indices.push_of_not_home later stack (staticAddress_not_spanHome addressed)] + exact .ok (.next memories (frames.extend extension (fun _ _ scope => scope))) +#print axioms Memory.Renamed.stepStaticAddress + +/-- An instance field load reads the related field, which both bodies resolve alike. -/ +theorem Memory.Renamed.stepFieldLoad {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} + (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (sourceFormed : source.WellFormed) + (sourceBody targetBody : CIL.Method) (metadata : StepMetadata sourceBody targetBody) + (field : CIL.Extraction.FieldRef) (pc : Nat) (arguments stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + (step sourceBody (.ldfld field) pc arguments sourceFrame stack source)) : + ∃ later, mapping.Extends later ∧ + ResultRenamed later (FrameAction.Renamed indices later scopes) + (step sourceBody (.ldfld field) pc arguments sourceFrame stack source) + (step targetBody (.ldfld field) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + refine ⟨mapping, .refl mapping, ?_⟩ + have shape : targetBody.fieldShape field = sourceBody.fieldShape field := by + simp only [CIL.Method.fieldShape, metadata.environment] + simp only [step_ldfld_eq, indices.unspecifiedOperands mapping] at observable ⊢ + split at observable + · cases observable + · rename_i guard + simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ + unfold fieldLoad at observable ⊢ + rw [shape] + cases resolved : sourceBody.fieldShape field + all_goals simp only [resolved] at observable ⊢ + all_goals first + | exact .error _ + | apply (related.classFieldLoadResult _ _ _ sourceFormed stack bounds + observable.bind_left).bind + | apply (related.loadAggregateFieldResult sourceFormed _ _ stack bounds + observable.bind_left).bind + | apply (related.loadValueFieldResult sourceFormed _ _ _ stack bounds observable.bind_left).bind + all_goals + intro first second result + rcases first with ⟨sourceAfter, values⟩ + rcases second with ⟨targetAfter, targetValues⟩ + rcases result with ⟨memories, rfl⟩ + exact .ok (.next memories frames) +#print axioms Memory.Renamed.stepFieldLoad + +/-- An instance field store writes the related field, which both bodies resolve alike. -/ +theorem Memory.Renamed.stepFieldStore {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} + (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (sourceFormed : source.WellFormed) + (sourceBody targetBody : CIL.Method) (metadata : StepMetadata sourceBody targetBody) + (field : CIL.Extraction.FieldRef) (pc : Nat) (arguments stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + (step sourceBody (.stfld field) pc arguments sourceFrame stack source)) : + ∃ later, mapping.Extends later ∧ + ResultRenamed later (FrameAction.Renamed indices later scopes) + (step sourceBody (.stfld field) pc arguments sourceFrame stack source) + (step targetBody (.stfld field) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + refine ⟨mapping, .refl mapping, ?_⟩ + have shape : targetBody.fieldShape field = sourceBody.fieldShape field := by + simp only [CIL.Method.fieldShape, metadata.environment] + simp only [step_stfld_eq, indices.unspecifiedOperands mapping] at observable ⊢ + split at observable + · cases observable + · rename_i guard + simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ + unfold fieldStore at observable ⊢ + rw [shape] + cases resolved : sourceBody.fieldShape field + all_goals simp only [resolved] at observable ⊢ + all_goals first + | exact .error _ + | apply (related.classFieldStoreResult _ _ _ sourceFormed stack bounds + observable.bind_left).bind + | apply (related.storeAggregateFieldResult sourceFormed _ _ stack bounds + observable.bind_left).bind + | apply (related.storeValueFieldResult sourceFormed _ _ stack bounds observable.bind_left).bind + all_goals + intro first second result + rcases first with ⟨sourceAfter, values⟩ + rcases second with ⟨targetAfter, targetValues⟩ + rcases result with ⟨memories, rfl⟩ + exact .ok (.next memories frames) +#print axioms Memory.Renamed.stepFieldStore + +/-- An instance field address forms the related address, which both bodies resolve alike. -/ +theorem Memory.Renamed.stepFieldAddress {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} + (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (sourceFormed : source.WellFormed) + (sourceBody targetBody : CIL.Method) (metadata : StepMetadata sourceBody targetBody) + (field : CIL.Extraction.FieldRef) (pc : Nat) (arguments stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + (step sourceBody (.ldflda field) pc arguments sourceFrame stack source)) : + ∃ later, mapping.Extends later ∧ + ResultRenamed later (FrameAction.Renamed indices later scopes) + (step sourceBody (.ldflda field) pc arguments sourceFrame stack source) + (step targetBody (.ldflda field) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + refine ⟨mapping, .refl mapping, ?_⟩ + have shape : targetBody.fieldShape field = sourceBody.fieldShape field := by + simp only [CIL.Method.fieldShape, metadata.environment] + simp only [step_ldflda_eq, indices.unspecifiedOperands mapping] at observable ⊢ + split at observable + · cases observable + · rename_i guard + simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ + unfold fieldReference at observable ⊢ + rw [shape] + cases resolved : sourceBody.fieldShape field + all_goals simp only [resolved] at observable ⊢ + all_goals first + | exact .error _ + | (apply (related.structFieldAddressResult sourceFormed _ stack bounds observable.bind_left).bind + intro first second result + rcases first with ⟨sourceAfter, values⟩ + rcases second with ⟨targetAfter, targetValues⟩ + rcases result with ⟨memories, rfl⟩ + exact .ok (.next memories frames)) +#print axioms Memory.Renamed.stepFieldAddress /-- Stack allocation faults are resource or memory failures, never named CLR exceptions. -/ theorem allocateStack_not_thrown (memory : Memory) (frame : Frame) (size : Nat) - (initLocals : Bool) (exception : String) : + (initLocals : Bool) (exception : ExceptionType) : allocateStack memory frame size initLocals ≠ .error (.thrown exception) := by intro raised unfold allocateStack allocateHome checkedAt at raised @@ -77,58 +406,58 @@ theorem allocateStack_not_thrown (memory : Memory) (frame : Frame) (size : Nat) /-- Actual dynamic stack steps extend ownership and preserve exact quota and initialization rules. -/ -theorem Memory.Renamed.stepStackAlloc {indices : LocalIndexMap} {mapping : AllocationRenaming} +theorem Memory.Renamed.stepLocalloc {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) (staticBounds : ∀ binding ∈ source.staticBindings, binding.2.allocation < mapping.sourceLimit) - (sourceBody targetBody : CIL.Method) (initLocals : Bool) (pc : Nat) - (arguments stack : List Value) + (sourceBody targetBody : CIL.Method) (declared : targetBody.declared = sourceBody.declared) + (pc : Nat) (arguments stack : List Value) (observable : SuccessOrThrow - (step sourceBody (.stackAlloc initLocals) pc arguments sourceFrame stack source)) : + (step sourceBody .localloc pc arguments sourceFrame stack source)) : ∃ later, mapping.Extends later ∧ ResultRenamed later (FrameAction.Renamed indices later scopes) - (step sourceBody (.stackAlloc initLocals) pc arguments sourceFrame stack source) - (step targetBody (.stackAlloc initLocals) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step, Bool.or_false, mapping.unspecifiedOperands] at observable ⊢ + (step sourceBody .localloc pc arguments sourceFrame stack source) + (step targetBody .localloc pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + simp only [step, declared, indices.unspecifiedOperands mapping] at observable ⊢ split at observable · cases observable · rename_i guard - simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ - split at observable <;> try contradiction - · rename_i size same - cases same - simp only [List.map_cons, List.map_nil, AllocationRenaming.value] + simp only [guard, Bool.false_eq_true, ite_false, localAllocation] at observable ⊢ + split at observable + · rename_i size + simp only [List.map_cons, List.map_nil, LocalIndexMap.value] cases size <;> simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at observable ⊢ all_goals try cases observable all_goals rename_i bits all_goals - cases allocated : CIL.Safety.allocateStack source sourceFrame bits.toNat initLocals with + cases allocated : CIL.Safety.allocateStack source sourceFrame bits.toNat + sourceBody.declared.initLocals with | error fault => cases fault <;> simp only [allocated, SuccessOrThrow] at observable rename_i exception - exact False.elim (allocateStack_not_thrown source sourceFrame _ initLocals + exact False.elim (allocateStack_not_thrown source sourceFrame _ _ exception allocated) | ok result => obtain ⟨reference, finalFrame, finalMemory⟩ := result obtain ⟨later, targetReference, targetFinal, targetAfter, extension, targetAllocated, referenceEqual, finalFrames, memories⟩ := - related.allocateStack frames sourceFormed targetFormed staticBounds _ initLocals + related.allocateStack frames sourceFormed targetFormed staticBounds _ _ reference allocated subst targetReference refine ⟨later, extension, ?_⟩ simp only [targetAllocated] exact .ok (.next memories finalFrames) - · simp only [pureArity, instruction] at observable - split at observable <;> cases observable -#print axioms Memory.Renamed.stepStackAlloc + · cases observable +#print axioms Memory.Renamed.stepLocalloc /-- Heap-resource and allocator faults cannot discharge a named exception obligation. -/ theorem allocateArray_not_thrown (memory : Memory) (element : CIL.ArrayElement) (length : Nat) - (exception : String) : allocateArray memory element length ≠ .error (.thrown exception) := by + (exception : ExceptionType) : + allocateArray memory element length ≠ .error (.thrown exception) := by intro raised unfold allocateArray checkedAt at raised repeat' first @@ -139,67 +468,11 @@ theorem allocateArray_not_thrown (memory : Memory) (element : CIL.ArrayElement) all_goals split at found <;> cases found #print axioms allocateArray_not_thrown -/-- Array allocation transports its fresh heap identity and retains the current frame. -/ -theorem Memory.Renamed.stepNewArray {indices : LocalIndexMap} {mapping : AllocationRenaming} - {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} - (related : Memory.Renamed mapping scopes source target) - (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) - (staticBounds : ∀ binding ∈ source.staticBindings, - binding.2.allocation < mapping.sourceLimit) - (sourceBody targetBody : CIL.Method) (element : CIL.ArrayElement) (pc : Nat) - (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) - (observable : SuccessOrThrow - (step sourceBody (.newArray element) pc arguments sourceFrame stack source)) : - ∃ later, mapping.Extends later ∧ - ResultRenamed later (FrameAction.Renamed indices later scopes) - (step sourceBody (.newArray element) pc arguments sourceFrame stack source) - (step targetBody (.newArray element) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step, Bool.or_false, mapping.unspecifiedOperands] at observable ⊢ - split at observable - · cases observable - · rename_i guard - simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ - split at observable <;> try contradiction - · rename_i length rest same - cases same - simp only [List.map_cons, AllocationRenaming.value] - have restBounds : ∀ value ∈ rest, value.Below mapping.sourceLimit := - fun value member => bounds value (List.mem_cons_of_mem _ member) - cases length <;> simp only [newArray, Bind.bind, Except.bind, Pure.pure, Except.pure] - at observable ⊢ - all_goals cases supported : element.allocationSupported - all_goals simp only [supported, Bool.not_true, Bool.not_false, Bool.true_eq, - Bool.false_eq_true, ite_true, ite_false] at observable ⊢ - all_goals try cases observable - all_goals rename_i bits - all_goals by_cases negative : bits.toInt < 0 - all_goals simp only [negative, ite_true, ite_false] at observable ⊢ - all_goals first - | exact ⟨mapping, .refl mapping, .error _⟩ - | (cases allocated : CIL.Safety.allocateArray source element bits.toInt.toNat with - | error fault => - cases fault <;> simp only [allocated, SuccessOrThrow] at observable - rename_i exception - exact False.elim (allocateArray_not_thrown source element _ exception allocated) - | ok result => - obtain ⟨view, sourceAfter⟩ := result - obtain ⟨targetAfter, targetAllocated, memories⟩ := related.allocateArray sourceFormed - targetFormed staticBounds element _ view sourceAfter allocated - refine ⟨mapping.allocate, mapping.allocate_extends, ?_⟩ - simp only [allocated, targetAllocated, Bind.bind, Except.bind] - rw [← mapping.allocate_extends.values rest restBounds] - exact .ok (.next memories - (frames.extend mapping.allocate_extends (fun _ _ scope => scope)))) - · simp only [pureArity, instruction] at observable - split at observable <;> cases observable - -#print axioms Memory.Renamed.stepNewArray /-- Heap-budget and allocator faults cannot discharge a named exception obligation. -/ theorem allocateObject_not_thrown (memory : Memory) (type : RuntimeType) (size : Nat) - (exception : String) : allocateObject memory type size ≠ .error (.thrown exception) := by + (exception : ExceptionType) : + allocateObject memory type size ≠ .error (.thrown exception) := by intro raised unfold allocateObject checkedAt at raised repeat' first @@ -210,99 +483,96 @@ theorem allocateObject_not_thrown (memory : Memory) (type : RuntimeType) (size : all_goals split at found <;> cases found #print axioms allocateObject_not_thrown -/-- `newobj` transports its fresh instance and checked arguments into the constructor call, and - leaves the instance under the same mapped caller stack. -/ -theorem Memory.Renamed.stepNewObject {indices : LocalIndexMap} {mapping : AllocationRenaming} +/-- A class construction transports its fresh instance and checked arguments into the + constructor call, and leaves the instance under the same mapped caller stack. -/ +theorem Memory.Renamed.constructObject {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) (staticBounds : ∀ binding ∈ source.staticBindings, binding.2.allocation < mapping.sourceLimit) - (sourceBody targetBody : CIL.Method) (callee argc : Nat) (owner : CIL.Extraction.TypeExpr) - (bases : List CIL.Extraction.TypeExpr) - (size pc : Nat) (arguments stack : List Value) - (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (callee argc : Nat) (type : RuntimeType) (size : Nat) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (observable : SuccessOrThrow - (step sourceBody (.newObject callee argc owner bases size) pc arguments sourceFrame stack - source)) : + (CIL.Safety.constructObject callee argc type size sourceFrame stack source)) : ∃ later, mapping.Extends later ∧ ResultRenamed later (FrameAction.Renamed indices later scopes) - (step sourceBody (.newObject callee argc owner bases size) pc arguments sourceFrame stack - source) - (step targetBody (.newObject callee argc owner bases size) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step, mapping.unspecifiedOperands, Bool.or_false, List.length_map, - ← List.map_take, ← List.map_drop, ← List.map_reverse] at observable ⊢ - by_cases guard : - unspecifiedOperands (observerCount (.newObject callee argc owner bases size)) stack = true - · simp only [guard, ite_true, SuccessOrThrow] at observable - · simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ - by_cases short : stack.length < argc - · simp only [short, ite_true] - exact ⟨mapping, .refl mapping, .error _⟩ - · simp only [short, ite_false] at observable ⊢ - have checks := related.checkedValues (stack.take argc).reverse (by + (CIL.Safety.constructObject callee argc type size sourceFrame stack source) + (CIL.Safety.constructObject callee argc type size targetFrame + (stack.map (indices.value mapping)) target) := by + unfold CIL.Safety.constructObject at observable ⊢ + simp only [List.length_map, ← List.map_take, ← List.map_drop, ← List.map_reverse] + by_cases short : stack.length < argc + · simp only [short, ite_true] + exact ⟨mapping, .refl mapping, .error _⟩ + · simp only [short, ite_false] at observable ⊢ + have checks := frames.callArguments related (stack.take argc).reverse (by + intro value member + exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member))) + cases checked : + (stack.take argc).reverse.mapM (CIL.Safety.callArgument sourceFrame source) with + | error fault => + rw [checked] at checks + generalize ((stack.take argc).reverse.map (indices.value mapping)).mapM + (CIL.Safety.callArgument targetFrame target) = targetChecked at checks ⊢ + cases checks + exact ⟨mapping, .refl mapping, .error fault⟩ + | ok operands => + have operandBounds : ∀ value ∈ operands, value.Below mapping.sourceLimit := by intro value member - exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member))) - cases checked : (stack.take argc).reverse.mapM (CIL.Safety.checkedValue source) with + rw [related.sourceLimit] + exact (callArguments_valid checked value member).below sourceFormed + rw [checked] at checks + obtain ⟨targetOperands, targetChecked, rfl⟩ := checks.success + simp only [checked, Bind.bind, Except.bind] at observable + cases allocated : CIL.Safety.allocateObject source type size with | error fault => - rw [checked] at checks - generalize ((stack.take argc).reverse.map mapping.value).mapM - (CIL.Safety.checkedValue target) = targetChecked at checks ⊢ - cases checks - exact ⟨mapping, .refl mapping, .error fault⟩ - | ok operands => - have operandBounds : ∀ value ∈ operands, value.Below mapping.sourceLimit := by + rw [allocated] at observable + cases fault <;> simp only [SuccessOrThrow] at observable + rename_i exception + exact False.elim (allocateObject_not_thrown source _ _ exception allocated) + | ok result => + obtain ⟨object, sourceAfter⟩ := result + obtain ⟨targetAfter, targetAllocated, memories⟩ := related.allocateObject sourceFormed + targetFormed staticBounds _ size object sourceAfter allocated + refine ⟨mapping.allocate, mapping.allocate_extends, ?_⟩ + simp only [targetChecked, targetAllocated, Bind.bind, Except.bind] + have operandsSame := mapping.allocate_extends.values operands operandBounds + have restSame := indices.values_extends mapping.allocate_extends (stack.drop argc) (by intro value member - rw [related.sourceLimit] - exact ((checkedValues_valid _ _ _ checked).2 value member).below sourceFormed - rw [checked] at checks - obtain ⟨targetOperands, targetChecked, rfl⟩ := checks.success - simp only [checked, Bind.bind, Except.bind] at observable - cases allocated : CIL.Safety.allocateObject source ⟨owner, bases⟩ size with - | error fault => - rw [allocated] at observable - cases fault <;> simp only [SuccessOrThrow] at observable - rename_i exception - exact False.elim (allocateObject_not_thrown source _ _ exception allocated) - | ok result => - obtain ⟨object, sourceAfter⟩ := result - obtain ⟨targetAfter, targetAllocated, memories⟩ := related.allocateObject sourceFormed - targetFormed staticBounds _ size object sourceAfter allocated - refine ⟨mapping.allocate, mapping.allocate_extends, ?_⟩ - simp only [checked, targetChecked, allocated, targetAllocated, Bind.bind, Except.bind] - have operandsSame := mapping.allocate_extends.values operands operandBounds - have restSame := mapping.allocate_extends.values (stack.drop argc) (by - intro value member - exact bounds value (List.mem_of_mem_drop member)) - rw [← operandsSame, ← restSame] - simpa only [List.map_cons, AllocationRenaming.value, - AllocationRenaming.managedReference, Pure.pure, Except.pure] using - ResultRenamed.ok (FrameAction.Renamed.call (indices := indices) (callee := callee) - (arguments := .object (.address object) :: operands) - (rest := .object (.address object) :: stack.drop argc) memories) + exact bounds value (List.mem_of_mem_drop member)) + rw [← operandsSame, ← restSame] + simpa only [List.map_cons, AllocationRenaming.value, LocalIndexMap.value, + AllocationRenaming.managedReference, Pure.pure, Except.pure] using + ResultRenamed.ok (FrameAction.Renamed.call (indices := indices) (callee := callee) + (arguments := .object (.address object) :: operands) + (rest := .object (.address object) :: stack.drop argc) memories) -#print axioms Memory.Renamed.stepNewObject +#print axioms Memory.Renamed.constructObject -/-- `newarr` of a class transports its fresh array identity and retains the current frame. -/ -theorem Memory.Renamed.stepNewReferenceArray {indices : LocalIndexMap} - {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} - {sourceFrame targetFrame : Frame} +/-- `newarr` transports its fresh heap identity and retains the current frame: both bodies give + the element the same shape, since their layout environments are equal. -/ +theorem Memory.Renamed.stepNewarr {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) (staticBounds : ∀ binding ∈ source.staticBindings, binding.2.allocation < mapping.sourceLimit) - (sourceBody targetBody : CIL.Method) (element : CIL.Extraction.TypeExpr) (pc : Nat) + (sourceBody targetBody : CIL.Method) (metadata : StepMetadata sourceBody targetBody) + (element : CIL.Extraction.TypeExpr) (pc : Nat) (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (observable : SuccessOrThrow - (step sourceBody (.newReferenceArray element) pc arguments sourceFrame stack source)) : + (step sourceBody (.newarr element) pc arguments sourceFrame stack source)) : ∃ later, mapping.Extends later ∧ ResultRenamed later (FrameAction.Renamed indices later scopes) - (step sourceBody (.newReferenceArray element) pc arguments sourceFrame stack source) - (step targetBody (.newReferenceArray element) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step, Bool.or_false, mapping.unspecifiedOperands] at observable ⊢ + (step sourceBody (.newarr element) pc arguments sourceFrame stack source) + (step targetBody (.newarr element) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + have shaped : targetBody.elementShape element = sourceBody.elementShape element := by + simp only [CIL.Method.elementShape, metadata.environment] + simp only [step, indices.unspecifiedOperands mapping] at observable ⊢ split at observable · cases observable · rename_i guard @@ -310,40 +580,72 @@ theorem Memory.Renamed.stepNewReferenceArray {indices : LocalIndexMap} split at observable <;> try contradiction · rename_i length rest same cases same - simp only [List.map_cons, AllocationRenaming.value] + simp only [List.map_cons, LocalIndexMap.value, shaped] have restBounds : ∀ value ∈ rest, value.Below mapping.sourceLimit := fun value member => bounds value (List.mem_cons_of_mem _ member) - cases length <;> simp only [newReferenceArray, Bind.bind, Except.bind, Pure.pure, - Except.pure] at observable ⊢ - all_goals try cases observable - all_goals rename_i bits - all_goals by_cases negative : bits.toInt < 0 - all_goals simp only [negative, ite_true, ite_false] at observable ⊢ - all_goals by_cases large : maxArrayLength < bits.toInt.toNat - all_goals try simp only [gt_iff_lt, large, ite_true, ite_false] at observable ⊢ - all_goals first - | exact ⟨mapping, .refl mapping, .error _⟩ - | (cases allocated : CIL.Safety.allocateObject source (referenceArrayType element) - (objectFieldSlot * bits.toInt.toNat) with - | error fault => - cases fault <;> simp only [allocated, SuccessOrThrow] at observable - rename_i exception - exact False.elim (allocateObject_not_thrown source _ _ exception allocated) - | ok result => - obtain ⟨array, sourceAfter⟩ := result - obtain ⟨targetAfter, targetAllocated, memories⟩ := related.allocateObject - sourceFormed targetFormed staticBounds _ _ array sourceAfter allocated - refine ⟨mapping.allocate, mapping.allocate_extends, ?_⟩ - simp only [allocated, targetAllocated, Bind.bind, Except.bind] - rw [← mapping.allocate_extends.values rest restBounds] - simpa only [List.map_cons, AllocationRenaming.value, - AllocationRenaming.managedReference, Pure.pure, Except.pure] using - ResultRenamed.ok (FrameAction.Renamed.next (pc := pc + 1) - (stack := .object (.address array) :: rest) memories + generalize sourceBody.elementShape element = shape at observable ⊢ + cases shape with + | value inline => + cases length <;> simp only [allocateElements, newArray, Bind.bind, Except.bind, Pure.pure, + Except.pure] at observable ⊢ + all_goals cases supported : inline.allocationSupported + all_goals simp only [supported, Bool.not_true, Bool.not_false, Bool.false_eq_true, + ite_true, ite_false] at observable ⊢ + all_goals try cases observable + all_goals rename_i bits + all_goals by_cases negative : bits.toInt < 0 + all_goals simp only [negative, ite_true, ite_false] at observable ⊢ + all_goals first + | exact ⟨mapping, .refl mapping, .error _⟩ + | (cases allocated : CIL.Safety.allocateArray source inline bits.toInt.toNat with + | error fault => + cases fault <;> simp only [allocated, SuccessOrThrow] at observable + rename_i exception + exact False.elim (allocateArray_not_thrown source inline _ exception allocated) + | ok result => + obtain ⟨view, sourceAfter⟩ := result + obtain ⟨targetAfter, targetAllocated, memories⟩ := related.allocateArray + sourceFormed targetFormed staticBounds inline _ view sourceAfter allocated + refine ⟨mapping.allocate, mapping.allocate_extends, ?_⟩ + simp only [targetAllocated] + rw [← indices.values_extends mapping.allocate_extends rest restBounds] + exact .ok (.next memories (frames.extend mapping.allocate_extends (fun _ _ scope => scope)))) - · simp only [pureArity, instruction] at observable + | reference classElement => + cases length <;> simp only [allocateElements, newReferenceArray, Bind.bind, Except.bind, + Pure.pure, Except.pure] at observable ⊢ + all_goals try cases observable + all_goals rename_i bits + all_goals by_cases negative : bits.toInt < 0 + all_goals simp only [negative, ite_true, ite_false] at observable ⊢ + all_goals by_cases large : maxArrayLength < bits.toInt.toNat + all_goals try simp only [gt_iff_lt, large, ite_true, ite_false] at observable ⊢ + all_goals first + | exact ⟨mapping, .refl mapping, .error _⟩ + | (cases allocated : CIL.Safety.allocateObject source (referenceArrayType classElement) + (objectFieldSlot * bits.toInt.toNat) with + | error fault => + cases fault <;> simp only [allocated, SuccessOrThrow] at observable + rename_i exception + exact False.elim (allocateObject_not_thrown source _ _ exception allocated) + | ok result => + obtain ⟨array, sourceAfter⟩ := result + obtain ⟨targetAfter, targetAllocated, memories⟩ := related.allocateObject + sourceFormed targetFormed staticBounds _ _ array sourceAfter allocated + refine ⟨mapping.allocate, mapping.allocate_extends, ?_⟩ + simp only [targetAllocated] + rw [← indices.values_extends mapping.allocate_extends rest restBounds] + simpa only [List.map_cons, AllocationRenaming.value, LocalIndexMap.value, + AllocationRenaming.managedReference, Pure.pure, Except.pure] using + ResultRenamed.ok (FrameAction.Renamed.next (pc := pc + 1) + (stack := .object (.address array) :: rest) memories + (frames.extend mapping.allocate_extends (fun _ _ scope => scope)))) + | method signature => exact ⟨mapping, .refl mapping, .error _⟩ + | unsupported reason => exact ⟨mapping, .refl mapping, .error _⟩ + · rw [show pureArity (.newarr element) = none from rfl] at observable + simp only [instruction] at observable split at observable <;> cases observable -#print axioms Memory.Renamed.stepNewReferenceArray +#print axioms Memory.Renamed.stepNewarr end CIL.Safety diff --git a/CIL/Safety/AllocationRefinement.lean b/CIL/Safety/AllocationRefinement.lean index b24bbeb33..178f7d087 100644 --- a/CIL/Safety/AllocationRefinement.lean +++ b/CIL/Safety/AllocationRefinement.lean @@ -53,7 +53,8 @@ theorem ProgramRenaming.raises {initial : Memory} (formed : initial.WellFormed) (statics : initial.StaticBindingsBounded) (literals : initial.LiteralsBounded) {method : Nat} {arguments : List Value} (argumentValues : ValuesValid initial arguments) - {exception : String} (raised : Raises sourceProgram method arguments initial exception) : + {exception : ExceptionType} + (raised : Raises sourceProgram method arguments initial exception) : Raises targetProgram method arguments initial exception := raised.rename StepRenaming.stepTransport programs formed statics literals argumentValues #print axioms ProgramRenaming.raises diff --git a/CIL/Safety/ArgumentEquivalence.lean b/CIL/Safety/ArgumentEquivalence.lean index 8a6169af8..6e5ede8b3 100644 --- a/CIL/Safety/ArgumentEquivalence.lean +++ b/CIL/Safety/ArgumentEquivalence.lean @@ -11,34 +11,18 @@ Two argument lists that agree on every index the body loads therefore give the s namespace CIL.Safety /-- Only argument loads observe the argument list after frame setup. -/ -theorem step_arguments_eq (body : CIL.Method) (op : CIL.Op) (pc : Nat) +theorem step_arguments_eq (body : CIL.Method) (op : CIL.Instruction) (pc : Nat) (args other : List Value) (frame : Frame) (stack : List Value) (memory : Memory) - (agreement : ∀ index narrow, op = .arg index narrow → args[index]? = other[index]?) : + (agreement : ∀ index, op = .ldarg index → args[index]? = other[index]?) : step body op pc args frame stack memory = step body op pc other frame stack memory := by cases op <;> try rfl - case arg index narrow => - cases narrow with - | none => - exact congrArg (fun selected => do - let some value := selected | .error .invalidState - return FrameAction.next (pc + 1) ((← checkedValue memory value) :: stack) frame memory) - (agreement index none rfl) - | some kind => - exact congrArg (fun (selected : Option Value) => show ExecutionResult FrameAction from do - if (selected.map Value.hasUnspecified).getD false then - .error (.unsupported "Instruction observes unspecified vector bits") else - let some (Value.scalar (.i32 bits)) := selected | .error .invalidState - return FrameAction.next (pc + 1) - ((← checkedValue memory (.scalar (.i32 (kind.extend bits)))) :: stack) frame memory) - (agreement index (some kind) rfl) - case ret narrow => - cases narrow <;> try rfl - cases stack with - | nil => rfl - | cons value rest => - cases value <;> try rfl - rename_i scalar - cases scalar <;> cases rest <;> rfl + case ldarg index => + have same : loadArgument body index args frame memory = + loadArgument body index other frame memory := by + simp only [loadArgument, agreement index rfl] + exact congrArg (fun (loaded : ExecutionResult Value) => + show ExecutionResult FrameAction from do + return FrameAction.next (pc + 1) ((← loaded) :: stack) frame memory) same all_goals cases stack with | nil => rfl @@ -57,8 +41,7 @@ theorem step_arguments_eq (body : CIL.Method) (op : CIL.Op) (pc : Nat) Child calls receive the same evaluated stack arguments on both sides. -/ theorem run_arguments_eq (program : CIL.Program) (method : Nat) (body : CIL.Method) (lookup : program[method]? = some body) (args other : List Value) - (agreement : ∀ index narrow, - CIL.Op.arg index narrow ∈ body.code → args[index]? = other[index]?) + (agreement : ∀ index, CIL.Instruction.ldarg index ∈ body.code → args[index]? = other[index]?) (fuel pc : Nat) (frame : Frame) (stack : List Value) (memory : Memory) : run program fuel method pc args frame stack memory = run program fuel method pc other frame stack memory := by @@ -66,13 +49,13 @@ theorem run_arguments_eq (program : CIL.Program) (method : Nat) (body : CIL.Meth | zero => rfl | succ fuel ih => cases fetched : body.code[pc]? with - | none => simp [run, lookup, fetched] + | none => simp [run_succ, lookup, fetched] | some op => have same := step_arguments_eq body op pc args other frame stack memory (by - intro index narrow equal + intro index equal subst op - exact agreement index narrow (List.mem_of_getElem? fetched)) - simp only [run, lookup, fetched] + exact agreement index (List.mem_of_getElem? fetched)) + simp only [run_succ, lookup, fetched] rw [same] cases haction : step body op pc other frame stack memory with | error fault => rfl @@ -91,14 +74,13 @@ theorem run_arguments_eq (program : CIL.Program) (method : Nat) (body : CIL.Meth them, and set up the same frame. -/ theorem invoke_arguments_eq (program : CIL.Program) (method : Nat) (body : CIL.Method) (lookup : program[method]? = some body) (args other : List Value) - (agreement : ∀ index narrow, - CIL.Op.arg index narrow ∈ body.code → args[index]? = other[index]?) + (agreement : ∀ index, CIL.Instruction.ldarg index ∈ body.code → args[index]? = other[index]?) (fuel : Nat) (memory : Memory) (checkedArgs : args.mapM (checkedValue memory) = .ok args) (checkedOther : other.mapM (checkedValue memory) = .ok other) (setup : enterFrame body args memory = enterFrame body other memory) : invoke program fuel method args memory = invoke program fuel method other memory := by - simp only [invoke, lookup, checkedArgs, checkedOther, Except.mapError, Bind.bind, Except.bind] + simp only [invoke_eq, lookup, checkedArgs, checkedOther, Except.mapError, Bind.bind, Except.bind] rw [setup] cases setupResult : enterFrame body other memory with | error fault => rfl diff --git a/CIL/Safety/ArgumentHomeValues.lean b/CIL/Safety/ArgumentHomeValues.lean deleted file mode 100644 index 1c7cbeab3..000000000 --- a/CIL/Safety/ArgumentHomeValues.lean +++ /dev/null @@ -1,71 +0,0 @@ -import CIL.Safety.HomeProgress -import CIL.Safety.FrameMemoryBelow -import CIL.Safety.WriteEffects - -/-! -# 256-bit argument homes - -Frame setup copies each 256-bit vector argument into a fresh private home before the body -can take its address. These lemmas give the exact home, its readable contents and write -access, and show that older memory is unchanged. --/ - -namespace CIL.Safety - -/-- Allocate and initialize a private 256-bit home using the checked memory operations. -/ -theorem allocate_initialized256 (memory : Memory) (activation : Nat) (bits : BitVec 256) - (wellFormed : memory.WellFormed) : - ∃ reference allocated result, - allocateHome memory activation 32 = .ok (reference, allocated) ∧ - storeValue allocated (.address reference) (.v256 bits) = .ok result ∧ - storeLocal allocated (.bytes .vector256 reference) (.scalar (.v256 bits)) = - .ok (.bytes .vector256 reference, result) ∧ - read result reference 32 1 = .ok (numberBytes bits.toNat 32) ∧ - access result reference 32 1 true = .ok () ∧ - MemoryBelow memory.nextIdentity memory result := by - obtain ⟨reference, allocated, home⟩ := - allocateHome_succeeds (alignment := 1) memory activation 32 wellFormed (by decide) (by decide) - have ready := allocateHome_write_access _ _ _ _ _ home - have length : (numberBytes bits.toNat 32).length = 32 := by simp [numberBytes] - obtain ⟨result, written⟩ := write_succeeds (bytes := numberBytes bits.toNat 32) - (by simpa only [length] using ready.access) - have readback := write_readback _ _ _ _ _ written - rw [length] at readback - have stored : storeLocal allocated (.bytes .vector256 reference) (.scalar (.v256 bits)) = - .ok (.bytes .vector256 reference, result) := by - simp [storeLocal, Value.hasUnspecified, CIL.Value.hasUnspecified, localNumber, localWidth, - written, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] - have created : makeLocal activation .vector256 (.v256 bits) memory = - .ok (.bytes .vector256 reference, [reference.allocation], result) := by - simp [makeLocal, localWidth, localAlignment, home, stored, Bind.bind, Except.bind, - Pure.pure, Except.pure] - refine ⟨reference, allocated, result, home, ?_, stored, readback, ?_, - makeLocal_preserves_caller_memory _ _ _ _ _ _ _ created⟩ - · simp [storeValue, CIL.Value.hasUnspecified, referenceAt, written, checkedAt, - Except.mapError, Bind.bind, Except.bind] - · simpa only [length] using (ready.after_write written).access - -/-- The actual vector-argument setup initializes a private copy before its - address is exposed to the method body. -/ -theorem make_argument_home256 (memory : Memory) (activation index : Nat) - (args : List Value) (bits : BitVec 256) - (wellFormed : memory.WellFormed) - (argument : args[index]? = some (.scalar (.v256 bits))) : - ∃ reference result, - makeArgumentHomes activation [index] args memory = - .ok ([(index, .bytes .vector256 reference)], [reference.allocation], result) ∧ - read result reference 32 1 = .ok (numberBytes bits.toNat 32) ∧ - access result reference 32 1 true = .ok () ∧ - MemoryBelow memory.nextIdentity memory result := by - obtain ⟨reference, allocated, result, home, _, stored, readback, writable, preserved⟩ := - allocate_initialized256 memory activation bits wellFormed - have made : makeArgumentHomes activation [index] args memory = - .ok ([(index, .bytes .vector256 reference)], [reference.allocation], result) := by - simp [makeArgumentHomes, argument, Value.hasUnspecified, CIL.Value.hasUnspecified, - localWidth, home, stored, Bind.bind, Except.bind, Pure.pure, Except.pure] - exact ⟨reference, result, made, readback, writable, preserved⟩ - -#print axioms allocate_initialized256 -#print axioms make_argument_home256 - -end CIL.Safety diff --git a/CIL/Safety/ArrayAllocation.lean b/CIL/Safety/ArrayAllocation.lean index 4a8cded7b..d496f26b8 100644 --- a/CIL/Safety/ArrayAllocation.lean +++ b/CIL/Safety/ArrayAllocation.lean @@ -43,7 +43,7 @@ def newArray (memory : Memory) (element : CIL.ArrayElement) (value : CIL.Value) | .i32 bits => pure bits.toInt | .i64 bits => pure bits.toInt | _ => .error .invalidState - if length < 0 then .error (.thrown "System.OverflowException") else + if length < 0 then .error (.thrown .overflow) else allocateArray memory element length.toNat end CIL.Safety diff --git a/CIL/Safety/ArrayContents.lean b/CIL/Safety/ArrayContents.lean index 29f00c844..57cb2d936 100644 --- a/CIL/Safety/ArrayContents.lean +++ b/CIL/Safety/ArrayContents.lean @@ -9,12 +9,18 @@ import CIL.Safety.BufferContents An array argument is an `ArrayView` whose bytes are the span `view.storage`, so the facts a method needs of its elements are `SpanContents` over that span. `SpanContents.array_load` turns them into the element reference and loaded value of an in-bound index; a null array -has no elements, so the bound alone rules it out. `step_arrayLoad_of_element` and -`step_arrayAddress_of_element` step `ldelem` and `ldelema` from those facts, and +has no elements, so the bound alone rules it out. `step_ldelem_of_element` steps `ldelem`, +`step_ldelem_any_of_element` a typed `ldelem` and `step_ldelema_of_element` an `ldelema` of +the element's shape from those facts, and `step_ldelem_any_of_transfer` a typed `ldelem` of a +struct or vector element from its transfer. `arrayLoad_of_element` and +`arrayAddress_of_element` read a typed `ldelem` and an `ldelema` once they step to +`arrayLoad` and `arrayAddress`; `arrayElement_formed` lets a method return the element address. A loop indexing the array names the element it stores to with `arrayElement_of_access` and steps `stelem` with -`step_arrayStore_of_write`; `SpanView.length_bounded` of the storage span lets its signed bound -test read as unsigned. +`step_stelem_narrow_of_write` or `step_stelem_wide_of_write`, or a typed `stelem` with +`step_stelem_any_of_write`, `step_stelem_any_of_wide_write` or `step_stelem_any_of_transfer`, +and reads one that steps to `arrayStore` with `arrayStore_of_write`; `SpanView.length_bounded` of +the storage span lets its signed bound test read as unsigned. -/ namespace CIL.Safety @@ -86,44 +92,121 @@ theorem SpanContents.array_load {memory : Memory} {view : ArrayView} {width : Na #print axioms SpanContents.array_load -/-- `ldelem` of an element whose address and load succeed pushes the loaded value. -/ -theorem step_arrayLoad_of_element {memory : Memory} {view : ArrayView} {index : CIL.Value} +/-- A numeric element load whose address and load succeed pushes the loaded value; a typed + `ldelem` of a numeric element steps to this load. -/ +theorem arrayLoad_of_element {memory : Memory} {view : ArrayView} {index : CIL.Value} {position width : Nat} {signed : Bool} {reference : Reference} {value : CIL.Value} (indexed : arrayIndex index = some position) (found : arrayElement memory view position width = .ok reference) (loaded : loadValue memory (.address reference) width signed = .ok value) : + ∀ rest, arrayLoad width signed (.scalar index :: .array view :: rest) memory = + .ok (memory, .scalar value :: rest) := by + intro rest + cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> + subst indexed <;> + simp [arrayLoad, arrayIndex, found, loaded, Bind.bind, Except.bind, Pure.pure, Except.pure] + +#print axioms arrayLoad_of_element + +/-- `ldelem` of `kind` from an element whose address and load succeed pushes the loaded value. -/ +theorem step_ldelem_of_element (kind : CIL.NumericKind) {memory : Memory} {view : ArrayView} + {index : CIL.Value} {position : Nat} {reference : Reference} {value : CIL.Value} + (indexed : arrayIndex index = some position) + (found : arrayElement memory view position kind.width = .ok reference) + (loaded : loadValue memory (.address reference) kind.width kind.signed = .ok value) : ∀ body pc args frame rest, - step body (.memory (.arrayLoad width signed)) pc args frame - (.scalar index :: .array view :: rest) memory = + step body (.ldelem kind) pc args frame (.scalar index :: .array view :: rest) memory = .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := by intro body pc args frame rest - rw [step_memory_eq] + rw [step_ldelem_eq] cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> - subst indexed <;> - simp [unspecifiedOperands, CIL.MemoryOp.argumentCount, Value.hasUnspecified, - CIL.Value.hasUnspecified, staticInstruction, memoryInstruction, otherMemoryInstruction, - arrayIndex, found, loaded, Bind.bind, Except.bind, Pure.pure, Except.pure] + subst indexed <;> cases kind <;> + simp [instructionStep, instruction, elementInstruction, observerCount, unspecifiedOperands, + Value.hasUnspecified, CIL.Value.hasUnspecified, arrayLoad, arrayIndex, found, loaded, + Bind.bind, Except.bind, Pure.pure, Except.pure] -#print axioms step_arrayLoad_of_element +#print axioms step_ldelem_of_element -/-- `ldelema` of an element of the array's own element type pushes the element's address. -/ -theorem step_arrayAddress_of_element {memory : Memory} {view : ArrayView} {index : CIL.Value} - {element : CIL.ArrayElement} {position : Nat} {reference : Reference} +/-- `ldelema` of an element of the array's own element type gives the element's address, + read-only unless `writable`. -/ +theorem arrayAddress_of_element {memory : Memory} {view : ArrayView} {index : CIL.Value} + {element : CIL.ArrayElement} {position : Nat} {reference : Reference} (writable : Bool) (typed : view.elementType = element) (indexed : arrayIndex index = some position) (found : arrayElement memory view position element.width = .ok reference) : - ∀ body pc args frame rest, - step body (.memory (.arrayAddress element)) pc args frame + ∀ rest, arrayAddress element writable (.scalar index :: .array view :: rest) memory = + .ok (memory, (if writable then .reference (.address reference) + else .readOnlyReference (.address reference)) :: rest) := by + intro rest + cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> + subst indexed <;> + simp [arrayAddress, arrayIndex, typed, found, Bind.bind, Except.bind, Pure.pure, Except.pure] + +#print axioms arrayAddress_of_element + +/-- `ldelema` of a type the method's layouts give the array's own element shape pushes the + element's address, read-only exactly when the instruction is. -/ +theorem step_ldelema_of_element {body : CIL.Method} {type : CIL.Extraction.TypeExpr} + {memory : Memory} {view : ArrayView} {index : CIL.Value} {element : CIL.ArrayElement} + {position : Nat} {reference : Reference} (readOnly : Bool) + (shape : body.elementShape type = .value element) (typed : view.elementType = element) + (indexed : arrayIndex index = some position) + (found : arrayElement memory view position element.width = .ok reference) : + ∀ pc args frame rest, step body (.ldelema type readOnly) pc args frame (.scalar index :: .array view :: rest) memory = - .ok (.next (pc + 1) (.reference (.address reference) :: rest) frame memory) := by - intro body pc args frame rest - rw [step_memory_eq] + .ok (.next (pc + 1) ((if readOnly then .readOnlyReference (.address reference) + else .reference (.address reference)) :: rest) frame memory) := by + intro pc args frame rest + rw [step_ldelema_eq, shape] + cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> + subst indexed <;> cases readOnly <;> + simp [unspecifiedOperands, Value.hasUnspecified, CIL.Value.hasUnspecified, + elementAddressInstruction, arrayAddress, arrayIndex, typed, found, Bind.bind, Except.bind, + Pure.pure, Except.pure] + +#print axioms step_ldelema_of_element + +/-- A typed `ldelem` of a type the method's layouts give a numeric element shape pushes the + value the element's load reads. -/ +theorem step_ldelem_any_of_element {body : CIL.Method} {type : CIL.Extraction.TypeExpr} + {memory : Memory} {view : ArrayView} {index : CIL.Value} {element : CIL.ArrayElement} + {position : Nat} {reference : Reference} {value : CIL.Value} + (shape : body.elementShape type = .value element) (numeric : element.composite = false) + (indexed : arrayIndex index = some position) + (found : arrayElement memory view position element.width = .ok reference) + (loaded : loadValue memory (.address reference) element.width element.signed = .ok value) : + ∀ pc args frame rest, step body (.ldelem_any type) pc args frame + (.scalar index :: .array view :: rest) memory = + .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := by + intro pc args frame rest + rw [step_ldelem_any_eq, shape] + cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> + subst indexed <;> + simp [typedElementInstruction, numeric, unspecifiedOperands, Value.hasUnspecified, + CIL.Value.hasUnspecified, arrayLoad, arrayIndex, found, loaded, Bind.bind, Except.bind, + Pure.pure, Except.pure] + +#print axioms step_ldelem_any_of_element + +/-- A typed `ldelem` of a type the method's layouts give a struct or vector element shape pushes + the snapshot the element's transfer reads. -/ +theorem step_ldelem_any_of_transfer {body : CIL.Method} {type : CIL.Extraction.TypeExpr} + {memory : Memory} {view : ArrayView} {index : CIL.Value} {element : CIL.ArrayElement} + {position : Nat} {value : CIL.Value} + (shape : body.elementShape type = .value element) (composite : element.composite = true) + (indexed : arrayIndex index = some position) + (transferred : ∀ rest, arrayTransferInstruction element false + (.scalar index :: .array view :: rest) memory = .ok (memory, .scalar value :: rest)) : + ∀ pc args frame rest, step body (.ldelem_any type) pc args frame + (.scalar index :: .array view :: rest) memory = + .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := by + intro pc args frame rest + rw [step_ldelem_any_eq, shape] cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> subst indexed <;> - simp [unspecifiedOperands, CIL.MemoryOp.argumentCount, Value.hasUnspecified, - CIL.Value.hasUnspecified, staticInstruction, memoryInstruction, otherMemoryInstruction, - arrayIndex, typed, found, Bind.bind, Except.bind, Pure.pure, Except.pure] + simp [typedElementInstruction, composite, unspecifiedOperands, Value.hasUnspecified, + CIL.Value.hasUnspecified, transferred, Bind.bind, Except.bind, Pure.pure, Except.pure] -#print axioms step_arrayAddress_of_element +#print axioms step_ldelem_any_of_transfer /-- An element address an array access returns forms in the same memory, so a method may return it. -/ @@ -164,51 +247,160 @@ theorem arrayElement_of_access {memory : Memory} {view : ArrayView} {base : Refe #print axioms arrayElement_of_access -/-- `stelem` of a 1-, 2- or 4-byte word into an element whose write succeeds leaves that - write's memory. -/ -theorem step_arrayStore_of_write {memory after : Memory} {view : ArrayView} {index : CIL.Value} +/-- A numeric element store of a 1-, 2- or 4-byte word whose write succeeds leaves that write's + memory; a typed `stelem` of a numeric element steps to this store. -/ +theorem arrayStore_of_write {memory after : Memory} {view : ArrayView} {index : CIL.Value} {position width : Nat} {reference : Reference} {bits : CIL.W32} (indexed : arrayIndex index = some position) (found : arrayElement memory view position width = .ok reference) (written : write memory reference (numberBytes bits.toNat width) 1 = .ok after) (narrow : width = 1 ∨ width = 2 ∨ width = 4 := by decide) : + ∀ rest, arrayStore width (.scalar (.i32 bits) :: .scalar index :: .array view :: rest) memory = + .ok (after, rest) := by + intro rest + rcases narrow with rfl | rfl | rfl <;> + cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> + subst indexed <;> + simp [arrayStore, arrayIndex, found, written, checkedAt, Except.mapError, Bind.bind, + Except.bind, Pure.pure, Except.pure] + +#print axioms arrayStore_of_write + +/-- A numeric element store of an 8-byte word whose write succeeds leaves that write's memory; + a typed `stelem` of an 8-byte element steps to this store. -/ +theorem arrayStore_i64_of_write {memory after : Memory} {view : ArrayView} + {index : CIL.Value} {position : Nat} {reference : Reference} {bits : CIL.W64} + (indexed : arrayIndex index = some position) + (found : arrayElement memory view position 8 = .ok reference) + (written : write memory reference (numberBytes bits.toNat 8) 1 = .ok after) : + ∀ rest, arrayStore 8 (.scalar (.i64 bits) :: .scalar index :: .array view :: rest) memory = + .ok (after, rest) := by + intro rest + cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> + subst indexed <;> + simp [arrayStore, arrayIndex, found, written, checkedAt, Except.mapError, Bind.bind, + Except.bind, Pure.pure, Except.pure] + +#print axioms arrayStore_i64_of_write + +/-- `stelem` of a 1-, 2- or 4-byte `kind` into an element whose write succeeds leaves that + write's memory. -/ +theorem step_stelem_narrow_of_write (kind : CIL.NumericKind) {memory after : Memory} + {view : ArrayView} {index : CIL.Value} {position : Nat} {reference : Reference} + {bits : CIL.W32} + (indexed : arrayIndex index = some position) + (found : arrayElement memory view position kind.width = .ok reference) + (written : write memory reference (numberBytes bits.toNat kind.width) 1 = .ok after) + (narrow : kind.width = 1 ∨ kind.width = 2 ∨ kind.width = 4 := by decide) : ∀ body pc args frame rest, - step body (.memory (.arrayStore width)) pc args frame + step body (.stelem kind) pc args frame (.scalar (.i32 bits) :: .scalar index :: .array view :: rest) memory = .ok (.next (pc + 1) rest frame after) := by intro body pc args frame rest - rw [step_memory_eq] - rcases narrow with rfl | rfl | rfl <;> + rw [step_stelem_eq] + cases kind <;> simp only [CIL.NumericKind.width] at narrow found written <;> + (try (exfalso; revert narrow; decide)) <;> cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> subst indexed <;> - simp [unspecifiedOperands, CIL.MemoryOp.argumentCount, Value.hasUnspecified, - CIL.Value.hasUnspecified, staticInstruction, memoryInstruction, otherMemoryInstruction, + simp [instructionStep, instruction, elementInstruction, observerCount, unspecifiedOperands, + Value.hasUnspecified, CIL.Value.hasUnspecified, CIL.NumericKind.width, arrayStore, arrayIndex, found, written, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] -#print axioms step_arrayStore_of_write +#print axioms step_stelem_narrow_of_write -/-- `stelem` of an 8-byte word into an element whose write succeeds leaves that write's - memory. -/ -theorem step_arrayStore_i64_of_write {memory after : Memory} {view : ArrayView} - {index : CIL.Value} {position : Nat} {reference : Reference} {bits : CIL.W64} +/-- `stelem.i8` or `stelem.i` of an 8-byte word into an element whose write succeeds leaves that + write's memory. -/ +theorem step_stelem_wide_of_write (kind : CIL.NumericKind) {memory after : Memory} + {view : ArrayView} {index : CIL.Value} {position : Nat} {reference : Reference} + {bits : CIL.W64} (indexed : arrayIndex index = some position) (found : arrayElement memory view position 8 = .ok reference) - (written : write memory reference (numberBytes bits.toNat 8) 1 = .ok after) : + (written : write memory reference (numberBytes bits.toNat 8) 1 = .ok after) + (wide : kind = .i8 ∨ kind = .i := by decide) : ∀ body pc args frame rest, - step body (.memory (.arrayStore 8)) pc args frame + step body (.stelem kind) pc args frame (.scalar (.i64 bits) :: .scalar index :: .array view :: rest) memory = .ok (.next (pc + 1) rest frame after) := by intro body pc args frame rest - rw [step_memory_eq] + rw [step_stelem_eq] + rcases wide with rfl | rfl <;> cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> subst indexed <;> - simp [unspecifiedOperands, CIL.MemoryOp.argumentCount, Value.hasUnspecified, - CIL.Value.hasUnspecified, staticInstruction, memoryInstruction, otherMemoryInstruction, + simp [instructionStep, instruction, elementInstruction, observerCount, unspecifiedOperands, + Value.hasUnspecified, CIL.Value.hasUnspecified, CIL.NumericKind.width, arrayStore, arrayIndex, found, written, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] -#print axioms step_arrayStore_i64_of_write +#print axioms step_stelem_wide_of_write + +/-- A typed `stelem` of a 1-, 2- or 4-byte word into a numeric element whose write succeeds + leaves that write's memory. -/ +theorem step_stelem_any_of_write {body : CIL.Method} {type : CIL.Extraction.TypeExpr} + {memory after : Memory} {view : ArrayView} {index : CIL.Value} {element : CIL.ArrayElement} + {position : Nat} {reference : Reference} {bits : CIL.W32} + (shape : body.elementShape type = .value element) (numeric : element.composite = false) + (indexed : arrayIndex index = some position) + (found : arrayElement memory view position element.width = .ok reference) + (written : write memory reference (numberBytes bits.toNat element.width) 1 = .ok after) + (narrow : element.width = 1 ∨ element.width = 2 ∨ element.width = 4 := by decide) : + ∀ pc args frame rest, step body (.stelem_any type) pc args frame + (.scalar (.i32 bits) :: .scalar index :: .array view :: rest) memory = + .ok (.next (pc + 1) rest frame after) := by + intro pc args frame rest + have stored := arrayStore_of_write indexed found written narrow rest + rw [step_stelem_any_eq, shape] + cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> + subst indexed <;> + simp [typedElementInstruction, numeric, unspecifiedOperands, Value.hasUnspecified, + CIL.Value.hasUnspecified, stored, Bind.bind, Except.bind, Pure.pure, Except.pure] + +#print axioms step_stelem_any_of_write + +/-- A typed `stelem` of an 8-byte word into a numeric element whose write succeeds leaves that + write's memory. -/ +theorem step_stelem_any_of_wide_write {body : CIL.Method} {type : CIL.Extraction.TypeExpr} + {memory after : Memory} {view : ArrayView} {index : CIL.Value} {element : CIL.ArrayElement} + {position : Nat} {reference : Reference} {bits : CIL.W64} + (shape : body.elementShape type = .value element) (numeric : element.composite = false) + (indexed : arrayIndex index = some position) + (found : arrayElement memory view position 8 = .ok reference) + (written : write memory reference (numberBytes bits.toNat 8) 1 = .ok after) + (wide : element.width = 8 := by decide) : + ∀ pc args frame rest, step body (.stelem_any type) pc args frame + (.scalar (.i64 bits) :: .scalar index :: .array view :: rest) memory = + .ok (.next (pc + 1) rest frame after) := by + intro pc args frame rest + have stored := arrayStore_i64_of_write indexed found written rest + rw [step_stelem_any_eq, shape] + cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> + subst indexed <;> + simp [typedElementInstruction, numeric, wide, unspecifiedOperands, Value.hasUnspecified, + CIL.Value.hasUnspecified, stored, Bind.bind, Except.bind, Pure.pure, Except.pure] + +#print axioms step_stelem_any_of_wide_write + +/-- A typed `stelem` of a specified value into a struct or vector element whose transfer succeeds + leaves that transfer's memory. -/ +theorem step_stelem_any_of_transfer {body : CIL.Method} {type : CIL.Extraction.TypeExpr} + {memory after : Memory} {view : ArrayView} {index : CIL.Value} {element : CIL.ArrayElement} + {position : Nat} {value : CIL.Value} + (shape : body.elementShape type = .value element) (composite : element.composite = true) + (indexed : arrayIndex index = some position) (specified : value.hasUnspecified = false) + (transferred : ∀ rest, arrayTransferInstruction element true + (.scalar value :: .scalar index :: .array view :: rest) memory = .ok (after, rest)) : + ∀ pc args frame rest, step body (.stelem_any type) pc args frame + (.scalar value :: .scalar index :: .array view :: rest) memory = + .ok (.next (pc + 1) rest frame after) := by + intro pc args frame rest + rw [step_stelem_any_eq, shape] + cases index <;> simp only [arrayIndex, reduceCtorEq, Option.some.injEq] at indexed <;> + subst indexed <;> + simp [typedElementInstruction, composite, unspecifiedOperands, Value.hasUnspecified, + CIL.Value.hasUnspecified, transferred, Bind.bind, Except.bind, Pure.pure, Except.pure] <;> + exact specified + +#print axioms step_stelem_any_of_transfer /-- `values` are the elements of a span over the whole array in either access mode, which shares its storage extent. -/ @@ -232,18 +424,19 @@ theorem CheckedSpan.of_array {memory : Memory} {view : ArrayView} {width : Nat} theorem step_arraySpan_whole {memory : Memory} {view : ArrayView} {element : CIL.ArrayElement} {writable : Bool} (typed : view.elementType = element) (valid : view.validate memory = .ok ()) : - ∀ body pc args frame rest, - step body (.memory (.arraySpan element writable .whole)) pc args frame + ∀ body pc frame rest, + modelStep (some (.memory (.arraySpan element writable .whole))) body pc frame (.array view :: rest) memory = .ok (.next (pc + 1) (.bufferSpan (view.span writable) :: rest) frame memory) := by - intro body pc args frame rest + intro body pc frame rest subst typed have checked : (view.span writable).validate memory = .ok () := ArrayView.span_valid writable valid simp only [ArrayView.span] at checked - rw [step_memory_eq] + rw [modelStep_memory] simp [unspecifiedOperands, CIL.MemoryOp.argumentCount, CIL.ArraySpanRange.argumentCount, Value.hasUnspecified, staticInstruction, memoryInstruction, otherMemoryInstruction, checked, + viewOperandCount, viewOperands, spanView, ArrayView.span, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] #print axioms step_arraySpan_whole diff --git a/CIL/Safety/BoundaryStepRenaming.lean b/CIL/Safety/BoundaryStepRenaming.lean index 55dd792f2..56f288080 100644 --- a/CIL/Safety/BoundaryStepRenaming.lean +++ b/CIL/Safety/BoundaryStepRenaming.lean @@ -1,10 +1,15 @@ import CIL.Safety.FixedStepHelpers import CIL.Safety.NativeMethodTableRenaming +import CIL.Safety.SpanHomeRenaming +import CIL.Safety.SpanConstructionLemmas /-! Argument loads, calls and returns preserve checked values and control flow. -/ namespace CIL.Safety +-- Stack lists are renamed by the local map of the frame that holds them. +variable {indices : LocalIndexMap} + /-- Allocation renaming preserves exactly the null managed reference. -/ private theorem managedReference_null_iff (mapping : AllocationRenaming) (reference : ManagedReference) : @@ -12,40 +17,21 @@ private theorem managedReference_null_iff (mapping : AllocationRenaming) cases reference <;> simp [AllocationRenaming.managedReference] #print axioms managedReference_null_iff -/-- Observable execution through a native table requires a successful checked memory step. -/ -private theorem checkedNative_bind {mapping : AllocationRenaming} {relation : β → γ → Prop} - (source target : Checked α) (sourceNext : α → ExecutionResult β) - (targetNext : α → ExecutionResult γ) - (preserved : ∀ value, source = .ok value → target = .ok value) - (observable : SuccessOrThrow (checkedAt .null nativeMethodWidth source >>= sourceNext)) - (next : ∀ value, source = .ok value → SuccessOrThrow (sourceNext value) → - ResultRenamed mapping relation (sourceNext value) (targetNext value)) : - ResultRenamed mapping relation (checkedAt .null nativeMethodWidth source >>= sourceNext) - (checkedAt .null nativeMethodWidth target >>= targetNext) := by - cases checked : source with - | error fault => - simp [checked, checkedAt, Except.mapError, Bind.bind, Except.bind, SuccessOrThrow] at observable - | ok value => - simp only [checked, checkedAt, Except.mapError, Bind.bind, Except.bind] at observable ⊢ - rw [preserved value checked] - exact next value checked observable -#print axioms checkedNative_bind - /-- Managed metadata identities are stable while checked argument references are renamed. -/ -theorem Memory.Renamed.managedStep {indices : LocalIndexMap} {mapping : AllocationRenaming} +theorem Memory.Renamed.calliStep {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (formed : source.WellFormed) - (operation : CIL.ManagedOp) (pc : Nat) (stack : List Value) + (signature : CIL.ManagedSignature) (tail : Bool) (link : Option CIL.Link) (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) - (observable : SuccessOrThrow (managedStep operation pc sourceFrame stack source)) : + (observable : SuccessOrThrow (calliStep signature tail link sourceFrame stack source)) : ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (managedStep operation pc sourceFrame stack source) - (managedStep operation pc targetFrame (stack.map mapping.value) target) := by - unfold CIL.Safety.managedStep at observable ⊢ + (calliStep signature tail link sourceFrame stack source) + (calliStep signature tail link targetFrame (stack.map (indices.value mapping)) target) := by + unfold CIL.Safety.calliStep at observable ⊢ split - all_goals try simp only [*, List.map_cons, AllocationRenaming.value] at observable ⊢ + all_goals try simp only [*, List.map_cons, LocalIndexMap.value] + at observable ⊢ all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds all_goals repeat' rcases bounds with ⟨_, bounds⟩ all_goals try simp only [List.length_map, ← List.map_take, ← List.map_reverse, ← List.map_drop] @@ -56,81 +42,26 @@ theorem Memory.Renamed.managedStep {indices : LocalIndexMap} {mapping : Allocati | exact .ok (.tailCall related) | (simp only [Bool.false_eq_true, Bool.true_eq, ite_false, ite_true] at observable ⊢) | contradiction - | (apply checkedNative_bind _ _ _ _ - (fun value proof => related.addNativeTable_success formed _ value _ proof) observable - intro value found observable) - | (apply checkedNative_bind _ _ _ _ - (fun value proof => related.loadNativeTable_success formed _ value proof) observable - intro value found observable) - | (apply (related.checkedValues _ (by + | (apply (frames.callArguments related _ (by intro value member exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member)))).bind intro first second same subst second) - | (split <;> try simp only [*, Bool.false_eq_true, Bool.true_eq, ite_false, ite_true, - List.map_cons, AllocationRenaming.value, Bind.bind, Except.bind, Pure.pure, - Except.pure] at observable ⊢) + | (split <;> try simp only [*, Bool.false_eq_true, ite_false, ite_true, Bind.bind, + Except.bind, Pure.pure, Except.pure] at observable ⊢) all_goals try simp_all only [reduceCtorEq, forall_const, forall_eq', false_implies, true_implies] all_goals cases stack with - | nil => simp_all [AllocationRenaming.value, managedReference_null_iff] + | nil => simp_all [AllocationRenaming.value, LocalIndexMap.value, managedReference_null_iff] | cons head rest => - cases head <;> simp_all [AllocationRenaming.value, managedReference_null_iff] + cases head <;> + simp_all [AllocationRenaming.value, LocalIndexMap.value, managedReference_null_iff] all_goals cases rest with - | nil => simp_all [AllocationRenaming.value, managedReference_null_iff] + | nil => simp_all [AllocationRenaming.value, LocalIndexMap.value, managedReference_null_iff] | cons head rest => - cases head <;> simp_all [AllocationRenaming.value, managedReference_null_iff] <;> + cases head <;> + simp_all [AllocationRenaming.value, LocalIndexMap.value, managedReference_null_iff] <;> (exfalso; solve_by_elim) -#print axioms Memory.Renamed.managedStep - -/-- A receiver-checked call transports as a call: renaming keeps a receiver null exactly when it - was, so both runs throw at the `callvirt` or both enter the callee. -/ -private theorem Memory.Renamed.stepCallInstance {indices : LocalIndexMap} - {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} - {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) - (formed : source.WellFormed) (sourceBody targetBody : CIL.Method) (callee argc pc : Nat) - (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) : - ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.callInstance callee argc) pc arguments sourceFrame stack source) - (step targetBody (.callInstance callee argc) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step, mapping.unspecifiedOperands, Bool.or_false, List.length_map, - ← List.map_take, ← List.map_drop, ← List.map_reverse] - by_cases guard : unspecifiedOperands (observerCount (.callInstance callee argc)) stack = true - · simp only [guard, ite_true] - exact .error _ - · simp only [guard, Bool.false_eq_true, ite_false] - by_cases short : stack.length < argc - · simp only [short, ite_true] - exact .error _ - · simp only [short, ite_false] - have checks := related.checkedValues (stack.take argc).reverse (by - intro value member - exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member))) - cases checked : (stack.take argc).reverse.mapM (CIL.Safety.checkedValue source) with - | error fault => - rw [checked] at checks - generalize ((stack.take argc).reverse.map mapping.value).mapM - (CIL.Safety.checkedValue target) = targetChecked at checks ⊢ - cases checks - exact .error fault - | ok operands => - rw [checked] at checks - obtain ⟨targetOperands, targetChecked, rfl⟩ := checks.success - simp only [targetChecked, Bind.bind, Except.bind] - have called : ∀ receiver tail, ResultRenamed mapping - (FrameAction.Renamed indices mapping scopes) - (.ok (.call callee (.object (.address receiver) :: tail) (stack.drop argc) source)) - (.ok (.call callee ((Value.object (.address receiver) :: tail).map mapping.value) - ((stack.drop argc).map mapping.value) target)) := fun receiver tail => - .ok (FrameAction.Renamed.call related) - match operands with - | [] => exact .error _ - | .object (.address receiver) :: tail => exact called receiver tail - | .object .null :: _ => exact .error _ - | .scalar _ :: _ | .methodPointer _ :: _ | .methodTable _ :: _ | .reference _ :: _ - | .readOnlyReference _ :: _ | .span .. :: _ | .bufferSpan _ :: _ | .array _ :: _ => - exact .error _ -#print axioms Memory.Renamed.stepCallInstance +#print axioms Memory.Renamed.calliStep /-- Renaming keeps a live object's runtime type, and maps the fault of any other reference. -/ private theorem Memory.Renamed.runtimeTypeOf {mapping : AllocationRenaming} @@ -214,103 +145,635 @@ private theorem Memory.Renamed.objectTest {mapping : AllocationRenaming} · exact .ok rfl #print axioms Memory.Renamed.objectTest -/-- A type test transports as a step to the next pc: renaming keeps null, the tested runtime - class and the cast's exception. -/ -private theorem Memory.Renamed.stepObjectTest {indices : LocalIndexMap} +/-- A `castclass` (`throwing`) or `isinst` transports as a step to the next pc: renaming keeps + null, the tested runtime class and the cast's exception. -/ +private theorem Memory.Renamed.stepTypeTest {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (sourceBody targetBody : CIL.Method) (type : CIL.Extraction.TypeExpr) (throwing : Bool) + (sourceBody targetBody : CIL.Method) (operation : CIL.Instruction) + (type : CIL.Extraction.TypeExpr) (throwing : Bool) + (tests : operation = .castclass type ∧ throwing = true ∨ + operation = .isinst type ∧ throwing = false) (pc : Nat) (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) : ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.objectTest type throwing) pc arguments sourceFrame stack source) - (step targetBody (.objectTest type throwing) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step, mapping.unspecifiedOperands, Bool.or_false] - by_cases guard : unspecifiedOperands (observerCount (.objectTest type throwing)) stack = true - · simp only [guard, ite_true] - exact .error _ - · simp only [guard, Bool.false_eq_true, ite_false] - match stack, bounds with - | .object reference :: rest, bounds => - have below : reference.Below mapping.sourceLimit := bounds _ List.mem_cons_self - simp only [List.map_cons, AllocationRenaming.value] - refine (related.objectTest reference below type throwing).bind _ _ ?_ - intro left right same - subst same - exact .ok (.next related frames) - | [], _ => exact .error _ - | .scalar _ :: _, _ | .methodPointer _ :: _, _ | .methodTable _ :: _, _ - | .reference _ :: _, _ | .readOnlyReference _ :: _, _ | .span .. :: _, _ - | .bufferSpan _ :: _, _ | .array _ :: _, _ => - dsimp only [pureArity, CIL.Safety.instruction] - simp only [mapping.unspecifiedOperands] - split <;> exact .error _ -#print axioms Memory.Renamed.stepObjectTest + (step sourceBody operation pc arguments sourceFrame stack source) + (step targetBody operation pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + rcases tests with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ <;> ( + simp only [step, observerCount, indices.unspecifiedOperands mapping] + by_cases guard : unspecifiedOperands 1 stack = true + · simp only [guard, ite_true] + exact .error _ + · simp only [guard, Bool.false_eq_true, ite_false] + match stack, bounds with + | .object reference :: rest, bounds => + have below : reference.Below mapping.sourceLimit := bounds _ List.mem_cons_self + simp only [List.map_cons, LocalIndexMap.value] + refine (related.objectTest reference below type _).bind _ _ ?_ + intro left right same + subst same + exact .ok (.next related frames) + | [], _ => exact .error _ + | .scalar _ :: _, _ | .methodPointer _ :: _, _ | .methodTable _ :: _, _ + | .reference _ :: _, _ | .readOnlyReference _ :: _, _ | .span .. :: _, _ + | .typeHandle _ :: _, _ | .runtimeType _ :: _, _ + | .bufferSpan _ :: _, _ | .array _ :: _, _ | .spanHome _ :: _, _ => + dsimp only [pureArity, CIL.Safety.instruction] + simp only [indices.unspecifiedOperands mapping] + split <;> exact .error _) +#print axioms Memory.Renamed.stepTypeTest /-- A virtual call transports as a call: renaming keeps a receiver null exactly when it was and keeps a live receiver's class, so both runs throw, fault alike or enter the same body. -/ -private theorem Memory.Renamed.stepCallVirtual {indices : LocalIndexMap} +private theorem Memory.Renamed.stepCallvirt {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) - (sourceBody targetBody : CIL.Method) (targets : List (CIL.Extraction.TypeExpr × Nat)) - (argc pc : Nat) (arguments stack : List Value) - (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) : + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (formed : source.WellFormed) + (sourceBody targetBody : CIL.Method) (method : CIL.Extraction.MethodRef) (pc : Nat) + (linked : targetBody.linkAt pc = sourceBody.linkAt pc) + (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) : ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.callVirtual targets argc) pc arguments sourceFrame stack source) - (step targetBody (.callVirtual targets argc) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step, mapping.unspecifiedOperands, Bool.or_false, List.length_map, + (step sourceBody (.callvirt method) pc arguments sourceFrame stack source) + (step targetBody (.callvirt method) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + simp only [step, indices.unspecifiedOperands mapping, List.length_map, linked, ← List.map_take, ← List.map_drop, ← List.map_reverse] - by_cases guard : unspecifiedOperands (observerCount (.callVirtual targets argc)) stack = true + by_cases guard : unspecifiedOperands (observerCount (.callvirt method)) stack = true · simp only [guard, ite_true] exact .error _ · simp only [guard, Bool.false_eq_true, ite_false] - by_cases short : stack.length < argc - · simp only [short, ite_true] - exact .error _ - · simp only [short, ite_false] - have checks := related.checkedValues (stack.take argc).reverse (by - intro value member - exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member))) - cases checked : (stack.take argc).reverse.mapM (CIL.Safety.checkedValue source) with - | error fault => - rw [checked] at checks - generalize ((stack.take argc).reverse.map mapping.value).mapM - (CIL.Safety.checkedValue target) = targetChecked at checks ⊢ - cases checks - exact .error fault - | ok operands => - rw [checked] at checks - obtain ⟨targetOperands, targetChecked, rfl⟩ := checks.success - have unchanged := (checkedValues_valid _ _ _ checked).1 - simp only [targetChecked, Bind.bind, Except.bind] - match operands, unchanged with - | [], _ => exact .error _ - | .object (.address receiver) :: tail, unchanged => - have below := bounds _ (List.mem_of_mem_take (List.mem_reverse.mp - (unchanged ▸ List.mem_cons_self ..))) - simp only [Value.Below, ManagedReference.Below] at below - simp only [List.map_cons, AllocationRenaming.value, AllocationRenaming.managedReference] - refine (related.virtualTarget receiver below targets).bind _ _ ?_ - intro left right same - subst same - exact .ok (FrameAction.Renamed.call related) - | .object .null :: _, _ => exact .error _ - | .scalar _ :: _, _ | .methodPointer _ :: _, _ | .methodTable _ :: _, _ - | .reference _ :: _, _ | .readOnlyReference _ :: _, _ | .span .. :: _, _ - | .bufferSpan _ :: _, _ | .array _ :: _, _ => + -- The dispatch table is the link's; both bodies read the same one. + generalize method.argumentCount = argc + generalize sourceBody.linkAt pc = link + rcases link with _ | link + · exact .error _ + cases link <;> try exact .error _ + case direct callee => + try dsimp only + by_cases short : stack.length < argc + · simp only [short, ite_true] + exact .error _ + · simp only [short, ite_false] + have checks := frames.callArguments related (stack.take argc).reverse (by + intro value member + exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member))) + cases checked : + (stack.take argc).reverse.mapM (CIL.Safety.callArgument sourceFrame source) with + | error fault => + rw [checked] at checks + generalize ((stack.take argc).reverse.map (indices.value mapping)).mapM + (CIL.Safety.callArgument targetFrame target) = targetChecked at checks ⊢ + cases checks + exact .error fault + | ok operands => + rw [checked] at checks + obtain ⟨targetOperands, targetChecked, rfl⟩ := checks.success + simp only [targetChecked, Bind.bind, Except.bind] + have called : ∀ receiver tail, ResultRenamed mapping + (FrameAction.Renamed indices mapping scopes) + (.ok (.call callee (.object (.address receiver) :: tail) (stack.drop argc) source)) + (.ok (.call callee ((Value.object (.address receiver) :: tail).map mapping.value) + ((stack.drop argc).map (indices.value mapping)) target)) := fun receiver tail => + .ok (FrameAction.Renamed.call related) + match operands with + | [] => exact .error _ + | .object (.address receiver) :: tail => exact called receiver tail + | .object .null :: _ => exact .error _ + | .scalar _ :: _ | .methodPointer _ :: _ | .methodTable _ :: _ | .reference _ :: _ + | .readOnlyReference _ :: _ | .span .. :: _ | .bufferSpan _ :: _ | .array _ :: _ + | .typeHandle _ :: _ | .runtimeType _ :: _ | .spanHome _ :: _ => + exact .error _ + case virtual targets => + try dsimp only + by_cases short : stack.length < argc + · simp only [short, ite_true] + exact .error _ + · simp only [short, ite_false] + have checks := frames.callArguments related (stack.take argc).reverse (by + intro value member + exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member))) + cases checked : + (stack.take argc).reverse.mapM (CIL.Safety.callArgument sourceFrame source) with + | error fault => + rw [checked] at checks + generalize ((stack.take argc).reverse.map (indices.value mapping)).mapM + (CIL.Safety.callArgument targetFrame target) = targetChecked at checks ⊢ + cases checks + exact .error fault + | ok operands => + rw [checked] at checks + obtain ⟨targetOperands, targetChecked, rfl⟩ := checks.success + have operandsValid := callArguments_valid checked + simp only [targetChecked, Bind.bind, Except.bind] + match operands, operandsValid with + | [], _ => exact .error _ + | .object (.address receiver) :: tail, operandsValid => + have below : (Value.object (.address receiver)).Below mapping.sourceLimit := by + rw [related.sourceLimit] + exact (operandsValid _ (List.mem_cons_self ..)).below formed + simp only [Value.Below, ManagedReference.Below] at below + simp only [List.map_cons, AllocationRenaming.value, + AllocationRenaming.managedReference] + refine (related.virtualTarget receiver below targets).bind _ _ ?_ + intro left right same + subst same + exact .ok (FrameAction.Renamed.call related) + | .object .null :: _, _ => exact .error _ + | .scalar _ :: _, _ | .methodPointer _ :: _, _ | .methodTable _ :: _, _ + | .reference _ :: _, _ | .readOnlyReference _ :: _, _ | .span .. :: _, _ + | .typeHandle _ :: _, _ | .runtimeType _ :: _, _ + | .bufferSpan _ :: _, _ | .array _ :: _, _ | .spanHome _ :: _, _ => + exact .error _ +#print axioms Memory.Renamed.stepCallvirt + +/-- Running `model` for a call without a link transports under every allocation renaming: + related bodies, well-formed source memories, frames and stacks whose source run succeeds or + throws step to related results. -/ +def _root_.CIL.RuntimeModel.StepRenamed (model : CIL.RuntimeModel) : Prop := + ∀ {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} + {source target : Memory} {sourceFrame targetFrame : Frame}, + Memory.Renamed mapping scopes source target → + Frame.Renamed indices mapping scopes sourceFrame targetFrame → source.WellFormed → + ∀ (sourceBody targetBody : CIL.Method), StepMetadata sourceBody targetBody → + ∀ (pc : Nat) (stack : List Value), (∀ value ∈ stack, value.Below mapping.sourceLimit) → + SuccessOrThrow (modelStep (some model) sourceBody pc sourceFrame stack source) → + ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (modelStep (some model) sourceBody pc sourceFrame stack source) + (modelStep (some model) targetBody pc targetFrame (stack.map (indices.value mapping)) target) + +/-- The `System.Object` constructor's model drops the receiver alike on both sides. -/ +theorem objectConstructor_stepRenamed : CIL.RuntimeModel.objectConstructor.StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames _ _ _ _ pc + stack _ _ + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel] + split + · exact .error _ + cases stack with + | nil => exact .error _ + | cons value rest => exact .ok (.next related frames) + +#print axioms objectConstructor_stepRenamed + +/-- An intrinsic's model reads only scalar operands and the profile, which related bodies share, + so both sides fault alike or push the same value. -/ +theorem intrinsic_stepRenamed (operation : CIL.Intrinsic) (argc : Nat) : + (CIL.RuntimeModel.intrinsic operation argc).StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames _ sourceBody + targetBody metadata pc stack _ _ + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel, intrinsicCall, + metadata.profile, List.length_map, ← List.map_take, ← List.map_drop, ← List.map_reverse] + split + · exact .error _ + split + · exact .error _ + split + · exact .error _ + apply (indices.scalarOperands mapping _).bind + intro values otherValues same + subst otherValues + apply (mapping.checkedIntrinsic _ _).bind + intro value otherValue same + subst otherValue + apply (related.checkedValue _ (by trivial)).bindWithSuccess + intro checked otherChecked checkedStep same + subst otherChecked + rw [indices.push_of_not_home mapping _ (checkedValue_not_spanHome checkedStep)] + exact .ok (.next related frames) + +#print axioms intrinsic_stepRenamed + +/-- A memory operation's model runs as that operation, which transports its successful effect or + named throw on the corresponding allocations. -/ +theorem memory_stepRenamed (operation : CIL.MemoryOp) : + (CIL.RuntimeModel.memory operation).StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames formed _ _ _ + pc stack bounds observable + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel, + CIL.RuntimeModel.observerCount] at observable ⊢ + by_cases observed : unspecifiedOperands operation.argumentCount stack = true + · simp only [observed, ite_true] + exact .error _ + simp only [observed, Bool.false_eq_true, ite_false] at observable ⊢ + apply (frames.viewOperands related (viewOperandCount operation) stack bounds).bindWithSuccess + intro viewed otherViewed sourceViewed operandsRelated + obtain ⟨rfl, viewedBounds⟩ := operandsRelated + apply (related.memoryInstructionResult operation formed viewed viewedBounds + (observable.bind_right sourceViewed).bind_left).bind + intro left right ⟨memories, values⟩ + rw [values] + exact .ok (.next memories frames) + +#print axioms memory_stepRenamed + +/-- A typed transfer reads or writes the corresponding bytes in the renamed allocations. -/ +theorem typedTransfer_stepRenamed (kind : CIL.TransferKind) (store : Bool) : + (CIL.RuntimeModel.typedTransfer kind store).StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames formed _ _ _ + pc stack bounds observable + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel, + typedTransferCall, CIL.RuntimeModel.observerCount] at observable ⊢ + by_cases observed : unspecifiedOperands (if store then 2 else 1) stack = true + · simp only [observed, ite_true] + exact .error _ + simp only [observed, Bool.false_eq_true, ite_false] at observable ⊢ + apply (related.observedTransferResult kind store formed stack bounds observable.bind_left).bind + intro left right ⟨memories, values⟩ + rw [values] + exact .ok (.next memories frames) + +#print axioms typedTransfer_stepRenamed + +/-- A constant's model pushes the same scalar on both sides and touches no memory. -/ +theorem constant_stepRenamed (value : CIL.W32) : + (CIL.RuntimeModel.constant value).StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames _ _ _ _ pc + stack _ _ + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel] + split + · exact .error _ + exact .ok (.next related frames) + +#print axioms constant_stepRenamed + +/-- GetTypeFromHandle preserves the handle's type across allocation renaming. -/ +theorem typeFromHandle_stepRenamed : CIL.RuntimeModel.typeFromHandle.StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames _ _ _ _ pc stack _ _ + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel] + split + · exact .error _ + cases stack with + | nil => exact .error _ + | cons head rest => cases head <;> first | exact .error _ | exact .ok (.next related frames) + +#print axioms typeFromHandle_stepRenamed + +/-- Runtime type equality reads only the scoped type identities, which are invariant under renaming. -/ +theorem typeEquality_stepRenamed (equal : Bool) : (CIL.RuntimeModel.typeEquality equal).StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames _ _ _ _ pc stack _ _ + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel] + split + · exact .error _ + rcases stack with _ | ⟨right, _ | ⟨left, rest⟩⟩ + · exact .error _ + · cases right <;> exact .error _ + cases right <;> try exact .error _ + cases left <;> try exact .error _ + simp only [List.map_cons, LocalIndexMap.value] + apply (mapping.runtimeTypeEqual _ _).bind + intro first second same + subst second + exact .ok (.next related frames) + +#print axioms typeEquality_stepRenamed + +/-- A feature getter reads the common profile and preserves both frames and memories. -/ +theorem feature_stepRenamed (feature : CIL.Feature) : + (CIL.RuntimeModel.feature feature).StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames _ sourceBody + targetBody metadata pc stack _ _ + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel, featureCall, + metadata.profile] + split + · exact .error _ + exact .ok (.next related frames) + +#print axioms feature_stepRenamed + +/-- A reference comparison reads the renamed references of the same allocations, so both sides + fault alike or push the same flag. -/ +theorem referenceCompare_stepRenamed (relation : CIL.ReferenceRelation) : + (CIL.RuntimeModel.referenceCompare relation).StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames _ _ _ _ pc + stack bounds _ + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel, + CIL.RuntimeModel.observerCount] + by_cases observed : unspecifiedOperands 2 stack = true + · simp only [observed, ite_true] + exact .error _ + simp only [observed, Bool.false_eq_true, ite_false] + rcases stack with _ | ⟨right, _ | ⟨left, rest⟩⟩ + · exact .error _ + · cases right <;> exact .error _ + have rightBound := bounds right (by simp) + have leftBound := bounds left (by simp) + cases right <;> cases left <;> + simp only [List.map_cons, LocalIndexMap.value, + referenceCompareCall] <;> + first + | exact .error _ + | (simp only [Value.Below] at rightBound leftBound + cases relation + · apply (related.referenceEqual _ _ leftBound rightBound).bind + intro holds other same; cases same; exact .ok (.next related frames) + · apply (related.referenceLess _ _ leftBound rightBound).bind + intro holds other same; cases same; exact .ok (.next related frames) + · apply (related.referenceLess _ _ rightBound leftBound).bind + intro holds other same; cases same; exact .ok (.next related frames)) + +#print axioms referenceCompare_stepRenamed + +/-- Reference subtraction preserves the mapped address and its readonly restriction. -/ +theorem referenceSubtract_stepRenamed : CIL.RuntimeModel.referenceSubtract.StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames _ _ _ _ pc + stack bounds _ + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel, + CIL.RuntimeModel.observerCount] + by_cases observed : unspecifiedOperands 2 stack = true + · simp only [observed, ite_true] + exact .error _ + simp only [observed, Bool.false_eq_true, ite_false] + rcases stack with _ | ⟨offset, _ | ⟨reference, rest⟩⟩ + · exact .error _ + · cases offset <;> exact .error _ + have bound := bounds reference (by simp) + cases offset <;> cases reference <;> + simp only [List.map_cons, LocalIndexMap.value, + referenceSubtractCall] <;> + first + | exact .error _ + | (apply (related.referenceSubtract _ bound _).bind + intro value other same + cases same + exact .ok (.next related frames)) + +#print axioms referenceSubtract_stepRenamed + +/-- `SkipInit` forms its renamed out reference on both sides and writes nothing. -/ +theorem skipInit_stepRenamed : CIL.RuntimeModel.skipInit.StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames _ _ _ _ pc + stack bounds _ + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel, + CIL.RuntimeModel.observerCount] + by_cases observed : unspecifiedOperands 1 stack = true + · simp only [observed, ite_true] + exact .error _ + simp only [observed, Bool.false_eq_true, ite_false] + rcases stack with _ | ⟨top, rest⟩ + · exact .error _ + have bound := bounds top (by simp) + cases top <;> + simp only [List.map_cons, LocalIndexMap.value, skipInitCall] <;> + first + | exact .error _ + | (simp only [Value.Below] at bound + apply (related.formValue _ bound).bind + intro formed other same + exact .ok (.next related frames)) + +#print axioms skipInit_stepRenamed + +/-- A span constructor run in place builds related views and stores them in the slot the renamed + home names, or faults alike on both sides. -/ +theorem spanInPlace_stepRenamed (constructor : CIL.SpanConstructor) : + (CIL.RuntimeModel.spanInPlace constructor).StepRenamed := by + intro indices mapping scopes source target sourceFrame targetFrame related frames formed _ _ _ + pc stack bounds observable + simp only [modelStep, observedCall, indices.unspecifiedOperands mapping, runModel, + CIL.RuntimeModel.observerCount] at observable ⊢ + by_cases observed : unspecifiedOperands constructor.operation.argumentCount stack = true + · simp only [observed, ite_true] + exact .error _ + simp only [observed, Bool.false_eq_true, ite_false, spanInPlaceCall] at observable ⊢ + apply (related.memoryInstructionResult _ formed stack bounds + observable.bind_left).bindWithSuccess + intro result otherResult constructed results + obtain ⟨after, values⟩ := result + obtain ⟨otherAfter, otherValues⟩ := otherResult + obtain ⟨memories, rfl⟩ := results + rcases values with _ | ⟨value, _ | ⟨home, rest⟩⟩ + · exact .error .invalidState + · exact .error .invalidState + cases home + case spanHome position => + simp only [List.map_cons, LocalIndexMap.value] + obtain ⟨rfl, valid⟩ := + spanConstructor_result constructor source after stack value _ constructed + have valueBound : value.Below mapping.sourceLimit := by + rw [related.sourceLimit] + exact valid.below formed + have slots := frames.locals.getElem?_all position + generalize sourceFound : sourceFrame.locals[position]? = sourceSlot at slots ⊢ + generalize targetFound : targetFrame.locals[indices.index position]? = targetSlot at slots ⊢ + cases slots with + | none => exact .error .invalidState + | @some slot otherSlot slots => + rcases slots with ⟨bounded, rfl⟩ + cases slot with + | span width writable view => + by_cases shaped : (constructor.shape != (width, writable)) = true + · simp only [AllocationRenaming.localSlot, shaped, ite_true] exact .error _ -#print axioms Memory.Renamed.stepCallVirtual + simp only [AllocationRenaming.localSlot, shaped, Bool.false_eq_true, ite_false] + apply (memories.storeLocal (indices := indices) (.span width writable view) value bounded + valueBound).bindWithSuccess + intro result otherResult stored results + obtain ⟨slotAfter, sourceAfter⟩ := result + obtain ⟨otherSlotAfter, targetAfter⟩ := otherResult + rcases results with ⟨slotsEqual, storedMemories⟩ + change otherSlotAfter = mapping.localSlot slotAfter at slotsEqual + subst otherSlotAfter + have positionBound : position < indices.sourceLength := by + rw [← frames.locals.lengths.1] + exact (List.getElem?_eq_some_iff.mp sourceFound).1 + exact .ok (.next storedMemories (frames.setLocal position positionBound slotAfter + (storeLocal_below bounded valueBound stored))) + | _ => exact .error .invalidState + all_goals exact .error .invalidState + +#print axioms spanInPlace_stepRenamed + +open CIL.Extraction in +/-- Every model of a registered family transports under an allocation renaming. -/ +theorem runtimeModel_stepRenamed {environment : CIL.LayoutEnvironment} + {opcode : CIL.CallOpcode} {method : CIL.Extraction.MethodRef} {model : CIL.RuntimeModel} + (found : CIL.Extraction.runtimeModel environment opcode method = some model) : + model.StepRenamed := by + refine runtimeModel_yields (property := CIL.RuntimeModel.StepRenamed) ?_ found + intro entry member + obtain ⟨family, listed, member⟩ := List.mem_flatten.mp member + simp only [modelFamilies, List.mem_cons, List.not_mem_nil] at listed + repeat' rcases listed with rfl | listed + all_goals first + | contradiction + | obtain rfl := List.mem_singleton.mp member + exact ModelEntry.exact_yields.mpr objectConstructor_stepRenamed + | obtain rfl := List.mem_singleton.mp member + exact staticSpanEntry_yields memory_stepRenamed + | exact runtimeTypeEntries_yields (property := CIL.RuntimeModel.StepRenamed) + typeFromHandle_stepRenamed typeEquality_stepRenamed entry member + | exact platformEntries_yields feature_stepRenamed constant_stepRenamed entry member + | exact referenceContentEntries_yields constant_stepRenamed entry member + | exact guardEntries_yields memory_stepRenamed entry member + | exact hardwareEntries_yields intrinsic_stepRenamed entry member + | exact vectorHelperEntries_yields intrinsic_stepRenamed memory_stepRenamed entry member + | exact vectorEntries_yields intrinsic_stepRenamed entry member + | exact integerMathEntries_yields intrinsic_stepRenamed entry member + | exact wideArithmeticEntries_yields intrinsic_stepRenamed memory_stepRenamed entry member + | exact binaryEntries_yields memory_stepRenamed entry member + | exact bitEntries_yields intrinsic_stepRenamed entry member + | exact unsafeEntries_yields constant_stepRenamed referenceCompare_stepRenamed + skipInit_stepRenamed entry member + | exact unsafeTransferEntries_yields memory_stepRenamed typedTransfer_stepRenamed entry member + | exact marshalEntries_yields memory_stepRenamed entry member + | exact unsafeReferenceEntries_yields memory_stepRenamed entry member + | exact unsafeReinterpretEntries_yields memory_stepRenamed entry member + | exact unsafeOffsetEntries_yields (property := CIL.RuntimeModel.StepRenamed) + memory_stepRenamed referenceSubtract_stepRenamed entry member + | exact unsafeBlockEntries_yields memory_stepRenamed entry member + | exact spanEntries_yields memory_stepRenamed entry member + | exact asSpanEntries_yields memory_stepRenamed entry member + | exact spanBulkEntries_yields memory_stepRenamed entry member + | exact spanConstructorEntries_yields memory_stepRenamed spanInPlace_stepRenamed entry member + +#print axioms runtimeModel_stepRenamed + +/-- A direct call transports as a call or, under `tail.`, as a tail call: both bodies link its + pc to the same callee, so both runs enter it with related arguments or fault alike. Without a + link both run the model one environment gives its method, which continues at the next pc. -/ +private theorem Memory.Renamed.stepCall {indices : LocalIndexMap} + {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} + {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (formed : source.WellFormed) (sourceBody targetBody : CIL.Method) + (method : CIL.Extraction.MethodRef) (prefixes : CIL.CallPrefixes) (pc : Nat) + (metadata : StepMetadata sourceBody targetBody) + (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + (step sourceBody (.call method prefixes) pc arguments sourceFrame stack source)) : + ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (step sourceBody (.call method prefixes) pc arguments sourceFrame stack source) + (step targetBody (.call method prefixes) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by + have unobserved : unspecifiedOperands 0 stack = false := rfl + simp only [step, observerCount, unobserved, Bool.false_eq_true, ite_false, + observedCall, indices.unspecifiedOperands mapping, List.length_map, metadata.linkAt pc, + metadata.environment, ← List.map_take, ← List.map_drop, ← List.map_reverse] at observable ⊢ + generalize sourceBody.linkAt pc = link at observable ⊢ + rcases link with _ | link + · dsimp only at observable ⊢ + by_cases tail : prefixes.tail = true + · simp only [tail, ite_true] + exact .error _ + simp only [tail, Bool.false_eq_true, ite_false] at observable ⊢ + cases found : CIL.Extraction.runtimeModel sourceBody.environment .call method with + | none => exact .error _ + | some model => + rw [found] at observable + exact runtimeModel_stepRenamed found related frames formed sourceBody targetBody metadata + pc stack bounds observable + · by_cases guard : unspecifiedOperands method.argumentCount stack = true + · simp only [guard, ite_true] + exact .error _ + simp only [guard, Bool.false_eq_true, ite_false] + cases link <;> try exact .error _ + rename_i callee + try dsimp only + by_cases tail : prefixes.tail = true + · simp only [tail, ite_true] + split + · exact .error _ + · apply (frames.callArguments related _ (by + intro value member + exact bounds value (List.mem_reverse.mp member))).bind + intro first second same + subst second + exact .ok (.tailCall related) + · simp only [tail, Bool.false_eq_true, ite_false] + by_cases short : stack.length < method.argumentCount + · simp only [short, ite_true] + exact .error _ + · simp only [short, ite_false] + apply (frames.callArguments related _ (by + intro value member + exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member)))).bind + intro first second same + subst second + exact .ok (.call related) +#print axioms Memory.Renamed.stepCall + +/-- Bodies that read an argument from the incoming list, narrowed alike, load related values. -/ +theorem Memory.Renamed.loadArgument {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} + (related : Memory.Renamed mapping scopes source target) {sourceBody targetBody : CIL.Method} + {index : Nat} {narrow : Option NarrowKind} + (sourceAccess : sourceBody.argumentAccess index = .value narrow) + (targetAccess : targetBody.argumentAccess index = .value narrow) (arguments : List Value) + (argumentBounds : ∀ value ∈ arguments, value.Below mapping.sourceLimit) : + ResultRenamed mapping (fun left right => right = mapping.value left) + (loadArgument sourceBody index arguments sourceFrame source) + (loadArgument targetBody index (arguments.map mapping.value) targetFrame target) := by + unfold CIL.Safety.loadArgument + rw [sourceAccess, targetAccess, List.getElem?_map] + cases found : arguments[index]? with + | none => cases narrow <;> exact .error .invalidState + | some value => + have bound := argumentBounds value (List.mem_of_getElem? found) + cases narrow with + | none => exact related.checkedValue value bound + | some kind => + dsimp only [Option.map] + rw [mapping.hasUnspecified] + by_cases unspecified : value.hasUnspecified = true + · simp only [unspecified, ↓reduceIte] + exact .error _ + · simp only [unspecified, Bool.false_eq_true, ↓reduceIte] + cases value <;> try exact .error .invalidState + rename_i scalar + cases scalar <;> try exact .error .invalidState + exact related.checkedValue _ trivial +#print axioms Memory.Renamed.loadArgument + +/-- The source body reads every argument as it arrives, so `starg` has no home to write. -/ +theorem StepMetadata.storeArgument_source {sourceBody targetBody : CIL.Method} + (metadata : StepMetadata sourceBody targetBody) (index : Nat) (value : Value) (frame : Frame) + (memory : Memory) : + storeArgument sourceBody index value frame memory = + .error (.unsupported "Store to an argument without a home") := by + obtain ⟨_, sourceAccess, -⟩ := metadata.arguments index + simp only [storeArgument, sourceAccess] +#print axioms StepMetadata.storeArgument_source + +/-- The source body reads every argument as it arrives, so `ldarga` has no home to address. -/ +theorem StepMetadata.addressArgument_source {sourceBody targetBody : CIL.Method} + (metadata : StepMetadata sourceBody targetBody) (index : Nat) (frame : Frame) + (memory : Memory) : + addressArgument sourceBody index frame memory = + .error (.unsupported "Address of an argument without a home") := by + obtain ⟨_, sourceAccess, -⟩ := metadata.arguments index + simp only [addressArgument, sourceAccess] +#print axioms StepMetadata.addressArgument_source + +/-- The target body reads every argument as it arrives, so `starg` has no home to write. -/ +theorem StepMetadata.storeArgument_target {sourceBody targetBody : CIL.Method} + (metadata : StepMetadata sourceBody targetBody) (index : Nat) (value : Value) (frame : Frame) + (memory : Memory) : + storeArgument targetBody index value frame memory = + .error (.unsupported "Store to an argument without a home") := by + obtain ⟨_, -, targetAccess⟩ := metadata.arguments index + simp only [storeArgument, targetAccess] +#print axioms StepMetadata.storeArgument_target -/-- Argument loads, calls and returns preserve their checked values and control flow. -/ +/-- The target body reads every argument as it arrives, so `ldarga` has no home to address. -/ +theorem StepMetadata.addressArgument_target {sourceBody targetBody : CIL.Method} + (metadata : StepMetadata sourceBody targetBody) (index : Nat) (frame : Frame) + (memory : Memory) : + addressArgument targetBody index frame memory = + .error (.unsupported "Address of an argument without a home") := by + obtain ⟨_, -, targetAccess⟩ := metadata.arguments index + simp only [addressArgument, targetAccess] +#print axioms StepMetadata.addressArgument_target + +/-- Argument loads, calls and returns preserve their checked values and control flow; argument + stores and addresses cannot succeed or throw without a home. -/ theorem Memory.Renamed.stepBoundary {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (formed : source.WellFormed) (sourceBody targetBody : CIL.Method) - (metadata : StepMetadata sourceBody targetBody) (operation : CIL.Op) + (metadata : StepMetadata sourceBody targetBody) (operation : CIL.Instruction) (family : fixedStepFamily operation = .boundary) (pc : Nat) (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (argumentBounds : ∀ value ∈ arguments, value.Below mapping.sourceLimit) @@ -319,96 +782,108 @@ theorem Memory.Renamed.stepBoundary {indices : LocalIndexMap} {mapping : Allocat ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) (step sourceBody operation pc arguments sourceFrame stack source) (step targetBody operation pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - by_cases isManaged : ∃ native, operation = .managed native - · rcases isManaged with ⟨native, rfl⟩ + targetFrame (stack.map (indices.value mapping)) target) := by + by_cases isAddress : ∃ identity signature, operation = .ldftn identity signature + · rcases isAddress with ⟨identity, signature, rfl⟩ + exact .ok (.next related frames) + by_cases indirect : ∃ signature tail, operation = .calli signature tail + · rcases indirect with ⟨signature, tail, rfl⟩ unfold CIL.Safety.step at observable ⊢ - simp only [Bool.or_false] at observable ⊢ - rw [mapping.unspecifiedOperands] - by_cases guarded : unspecifiedOperands (observerCount (.managed native)) stack = true + rw [indices.unspecifiedOperands mapping] + by_cases guarded : unspecifiedOperands (observerCount (.calli signature tail)) stack = true · simp only [guarded, ite_true] exact .error (.unsupported "Instruction observes unspecified vector bits") · simp only [guarded, Bool.false_eq_true, ite_false] at observable ⊢ - exact related.managedStep frames formed native pc stack bounds observable - by_cases receiverChecked : ∃ callee argc, operation = .callInstance callee argc - · rcases receiverChecked with ⟨callee, argc, rfl⟩ - exact related.stepCallInstance formed sourceBody targetBody callee argc pc arguments stack - bounds - by_cases dispatched : ∃ targets argc, operation = .callVirtual targets argc - · rcases dispatched with ⟨targets, argc, rfl⟩ - exact related.stepCallVirtual sourceBody targetBody targets argc pc arguments stack bounds - by_cases tested : ∃ type throwing, operation = .objectTest type throwing - · rcases tested with ⟨type, throwing, rfl⟩ - exact related.stepObjectTest frames sourceBody targetBody type throwing pc arguments stack - bounds + rw [metadata.linkAt pc] + exact related.calliStep frames signature tail _ stack bounds observable + by_cases direct : ∃ method prefixes, operation = .call method prefixes + · rcases direct with ⟨method, prefixes, rfl⟩ + exact related.stepCall frames formed sourceBody targetBody method prefixes pc metadata + arguments stack bounds observable + by_cases receiverChecked : ∃ method, operation = .callvirt method + · rcases receiverChecked with ⟨method, rfl⟩ + exact related.stepCallvirt frames formed sourceBody targetBody method pc (metadata.linkAt pc) + arguments stack bounds + by_cases casting : ∃ type, operation = .castclass type + · rcases casting with ⟨type, rfl⟩ + exact related.stepTypeTest frames sourceBody targetBody _ type true (.inl ⟨rfl, rfl⟩) pc + arguments stack bounds + by_cases testing : ∃ type, operation = .isinst type + · rcases testing with ⟨type, rfl⟩ + exact related.stepTypeTest frames sourceBody targetBody _ type false (.inr ⟨rfl, rfl⟩) pc + arguments stack bounds · allocation_fixed_step ( first - | exact False.elim (isManaged ⟨_, rfl⟩) - | exact False.elim (receiverChecked ⟨_, _, rfl⟩) - | exact False.elim (dispatched ⟨_, _, rfl⟩) - | exact False.elim (tested ⟨_, _, rfl⟩) - | (case h_61 => - rename_i _ _ position - rw [List.getElem?_map] - cases found : arguments[position]? with - | none => exact .error .invalidState - | some value => - apply (related.checkedValue value - (argumentBounds value (List.mem_of_getElem? found))).bind - intro first second same - subst second - exact .ok (.next related frames)) - | (case h_62 => - rename_i _ _ position kind - rw [List.getElem?_map] - cases found : arguments[position]? with - | none => exact .error .invalidState - | some value => - cases value <;> try exact .error .invalidState - rename_i scalar - cases scalar <;> try exact .error .invalidState - apply (related.checkedValue _ (by trivial)).bind - intro first second same - subst second - exact .ok (.next related frames)) - | (apply (frames.argumentHome _).bind - intro slot otherSlot slots - rcases slots with ⟨bounded, rfl⟩ - first - | (apply (related.loadLocal slot bounded).bind - intro first second same - subst second - exact .ok (.next related frames)) - | (apply (related.localAddress slot bounded).bind - intro first second same - subst second - exact .ok (.next related frames))) - | (try simp only [List.map_nil, List.length_map, ← List.map_take, ← List.map_drop, - ← List.map_reverse, metadata.returnsValue] + | exact False.elim (isAddress ⟨_, _, rfl⟩) + | exact False.elim (indirect ⟨_, _, rfl⟩) + | exact False.elim (direct ⟨_, _, rfl⟩) + | exact False.elim (receiverChecked ⟨_, rfl⟩) + | exact False.elim (casting ⟨_, rfl⟩) + | exact False.elim (testing ⟨_, rfl⟩) + -- Both bodies read every argument without a home, so a store or an address faults alike. + | (simp only [metadata.storeArgument_source, metadata.storeArgument_target, + metadata.addressArgument_source, metadata.addressArgument_target, Bind.bind, + Except.bind] + exact .error _) + | (rename_i position + obtain ⟨_, sourceAccess, targetAccess⟩ := metadata.arguments position + apply (related.loadArgument sourceAccess targetAccess arguments + argumentBounds).bindWithSuccess + intro first second loaded same + subst second + rw [indices.push_of_not_home mapping stack (loadArgument_not_spanHome loaded)] + exact .ok (.next related frames)) + | (try simp only [List.length_map, ← List.map_take, ← List.map_drop, ← List.map_reverse] repeat first | exact .error _ | exact .ok (.returned related) | exact .ok (.call related) | exact .ok (.tailCall related) - | (apply (related.checkedValues _ (by assumption)).bind + | (apply (related.checkedStack _ (by assumption)).bind intro first second same subst second) - | (apply (related.checkedValues _ (by + | (apply (frames.callArguments related _ (by assumption)).bind + intro first second same + subst second) + | (apply (frames.callArguments related _ (by intro value member exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member)))).bind intro first second same subst second) - | (apply (related.checkedValues _ (by + | (apply (frames.callArguments related _ (by intro value member exact bounds value (List.mem_reverse.mp member))).bind intro first second same subst second) - | (apply (related.scalarChecks _).bind - intro first second same - subst second) + | (apply (related.scalarChecks _).bindWithSuccess + intro first second checkedStep same + subst second + try rw [← indices.values_of_not_home mapping (checkedValues_not_spanHome checkedStep)]) | (split <;> try simp only [*, Bool.false_eq_true, ite_false]) - skip)) + skip) + all_goals + cases narrowing : sourceBody.returnNarrow with + | none => + simp only [narrowing, returnFrom, List.length_map] + by_cases sized : stack.length = (if sourceBody.returnsValue then 1 else 0) + · simp only [sized, ↓reduceIte] + apply (related.checkedStack stack bounds).bind + intro first second same + subst second + exact .ok (.returned related) + · simp only [sized, ↓reduceIte] + exact .error .invalidState + | some kind => + cases stack with + | nil => exact .error .invalidState + | cons value rest => + cases value <;> try exact .error .invalidState + rename_i scalar + cases scalar <;> cases rest <;> try exact .error .invalidState + simp only [returnFrom, List.map_cons, List.map_nil, + LocalIndexMap.value] + split <;> first | exact .ok (.returned related) | exact .error .invalidState) #print axioms Memory.Renamed.stepBoundary end CIL.Safety diff --git a/CIL/Safety/CallComposition.lean b/CIL/Safety/CallComposition.lean index 107518330..fddc68d3a 100644 --- a/CIL/Safety/CallComposition.lean +++ b/CIL/Safety/CallComposition.lean @@ -15,7 +15,7 @@ namespace CIL.Safety The invocation includes child setup and teardown; no helper body is assumed, and the caller's later outcome, normal or faulting, carries over unchanged. -/ theorem run_call_of_invoke {program : CIL.Program} {fuel method pc callee : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack arguments rest returned : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest returned : List Value} {frame : Frame} {memory updated childResult : Memory} {outcome : Except LocatedFault (Memory × List Value)} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) @@ -25,24 +25,26 @@ theorem run_call_of_invoke {program : CIL.Program} {fuel method pc callee : Nat} run program fuel method (pc + 1) args frame (returned ++ rest) childResult = outcome) : run program (fuel + 1) method pc args frame stack memory = outcome := by cases hb : program[callee]? with - | none => simp [invoke, hb] at child + | none => simp [invoke_eq, hb] at child | some childBody => cases ha : arguments.mapM (checkedValue updated) with - | error fault => simp [invoke, hb, ha, Except.mapError, Bind.bind, Except.bind] at child + | error fault => simp [invoke_eq, hb, ha, Except.mapError, Bind.bind, Except.bind] at child | ok checked => have same := (checkedValues_valid _ _ _ ha).1 subst checked cases he : enterFrame childBody arguments updated with - | error fault => simp [invoke, hb, ha, he, Except.mapError, Bind.bind, Except.bind] at child + | error fault => simp [invoke_eq, hb, ha, he, Except.mapError, Bind.bind, + Except.bind] at child | ok entered => obtain ⟨childFrame, childMemory⟩ := entered - simp only [invoke, hb, ha, he, Except.mapError, Bind.bind, Except.bind] at child - simp only [run, methodFound, instructionFound, stepped, hb, he, child, + simp only [invoke_eq, hb, ha, he, Except.mapError, Bind.bind, Except.bind] at child + simp only [run_succ, methodFound, instructionFound, stepped, hb, he, child, continuation, Except.mapError, Bind.bind, Except.bind] /-- Independently sufficient budgets combine without requiring equal proof fuel. -/ theorem run_call_exists {program : CIL.Program} {method pc callee : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack arguments rest returned values : List Value} + {body : CIL.Method} {op : CIL.Instruction} + {args stack arguments rest returned values : List Value} {frame : Frame} {memory updated childResult result : Memory} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .ok (.call callee arguments rest updated)) @@ -65,7 +67,7 @@ theorem run_call_exists {program : CIL.Program} {method pc callee : Nat} /-- Compose a separately proved child invocation with a caller postcondition. -/ theorem run_call_post {program : CIL.Program} {method pc callee : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack arguments rest values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest values : List Value} {frame : Frame} {memory updated final : Memory} {post : Memory → List Value → Prop} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = diff --git a/CIL/Safety/CallSegments.lean b/CIL/Safety/CallSegments.lean index bebd0c86d..f3827c4f6 100644 --- a/CIL/Safety/CallSegments.lean +++ b/CIL/Safety/CallSegments.lean @@ -12,18 +12,25 @@ the helper's complete contract and the actual checked write reach the continuati namespace CIL.Safety /-- Load two input words, call a binary scalar helper, and store its result to the output. -/ -def loadCallStoreCode (callee : Nat) : List CIL.Op := - [.arg 2, .arg 0, .fieldAt 0 8, .arg 1, .fieldAt 0 8, .call callee 2, .store64] +def loadCallStoreCode (helper : Extraction.MethodRef) (left right : Extraction.FieldRef) : + List CIL.Instruction := + [.ldarg 2, .ldarg 0, .ldfld left, .ldarg 1, .ldfld right, .call helper {}, .stind .i8] /-- Compose a load/call/store segment once for any proved helper with scalar encodings. The continuation receives the real call result and checked write, not an assumed summary. -/ theorem WP.load_call_store {Input Output : Type} {contract : MethodContract Input Output} {program : CIL.Program} {method start callee : Nat} {body : CIL.Method} + {helper : Extraction.MethodRef} {leftField rightField : Extraction.FieldRef} {frame : Frame} {memory : Memory} {post : (Memory × List Value) → Prop} (holds : contract.Holds program callee) (input : Input) (decode : Output → CIL.W64) (left right output : Reference) (found : program[method]? = some body) - (placed : CodeAt body start (loadCallStoreCode callee)) + (placed : CodeAt body start (loadCallStoreCode helper leftField rightField)) + (leftShape : body.fieldShape leftField = .value 0 8 false) + (rightShape : body.fieldShape rightField = .value 0 8 false) + (arity : helper.argumentCount = 2) + (linked : body.linkAt (start + 5) = some (.direct callee)) + (plainArguments : ∀ index, index < 3 → body.argumentAccess index = .value none) (call : ValidCall memory [⟨left, 8⟩, ⟨right, 8⟩] [⟨output, 8⟩]) (encoded : contract.args input = [.scalar (.i64 ((⟨left, 8⟩ : ArgumentView).word memory 0)), @@ -39,16 +46,21 @@ theorem WP.load_call_store {Input Output : Type} {contract : MethodContract Inpu WP (Execution (fun fuel => run program fuel method start [.reference (.address left), .reference (.address right), .reference (.address output)] frame [] memory)) post := by - cil_contract_steps [found, placed] + have outputAccess := plainArguments 2 (by decide) + have leftAccess := plainArguments 0 (by decide) + have rightAccess := plainArguments 1 (by decide) + have leftRead := call.input_word_step ⟨left, 8⟩ (by simp) ⟨0, by change 0 < 8 / 8; decide⟩ body leftField leftShape + have rightRead := call.input_word_step ⟨right, 8⟩ (by simp) ⟨0, by change 0 < 8 / 8; decide⟩ body rightField rightShape + cil_contract_steps [found, placed, outputAccess, leftAccess, rightAccess, leftRead, rightRead] apply run_call_of_contract holds input found - (by cil_lookup [placed]) (by cil_checked_step []) encoded.symm permitted + (by cil_lookup [placed]) (by cil_checked_step [linked, arity]) encoded.symm permitted intro middle value result have writable : access middle output 8 1 true = .ok () := (result.framed.access_write output (call.output_bound (output := ⟨output, 8⟩) (by simp)) 8 1).trans (call.writable ⟨output, 8⟩ (by simp)) obtain ⟨after, written, _, _⟩ := store_word64 (decode value) writable - have stored := step_store64_of_write written + have stored := step_stind_i8_of_write written rw [results] cil_contract_steps [found, placed, stored] until suffix middle value after result written diff --git a/CIL/Safety/CallingFields.lean b/CIL/Safety/CallingFields.lean index cdfb68e5e..82f4ab49e 100644 --- a/CIL/Safety/CallingFields.lean +++ b/CIL/Safety/CallingFields.lean @@ -98,18 +98,19 @@ theorem ValidCall.store_word {memory : Memory} {inputs outputs : List ArgumentVi the read are checked; no input/output separation is assumed. -/ theorem ValidCall.input_word_step {memory : Memory} {inputs outputs : List ArgumentView} (call : ValidCall memory inputs outputs) (input : ArgumentView) (member : input ∈ inputs) - (index : Fin (input.width / 8)) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.fieldAt (8 * index.val) 8 false) pc args frame + (index : Fin (input.width / 8)) (body : CIL.Method) (operand : Extraction.FieldRef) + (shape : body.fieldShape operand = .value (8 * index.val) 8 false) + (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.ldfld operand) pc args frame (.reference (.address input.reference) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i64 (input.word memory (8 * index.val))) :: rest) frame memory) := by have length : (input.bytes memory).length = input.width := by simp [ArgumentView.bytes] have loaded := call.input_snapshot input member rw [← length] at loaded - exact step_fieldAt_home (field := { offset := 8 * index.val, width := 8 }) + exact step_ldfld_unsigned_home (field := { offset := 8 * index.val, width := 8 }) call.wellFormed loaded (by have := index.isLt; simp only [length]; omega) - (show ScalarWidth 8 by decide) body pc args frame rest + (show ScalarWidth 8 by decide) body operand shape pc args frame rest #print axioms ValidCall.input_word_step diff --git a/CIL/Safety/Certificate.lean b/CIL/Safety/Certificate.lean index 05f1c7557..be259e142 100644 --- a/CIL/Safety/Certificate.lean +++ b/CIL/Safety/Certificate.lean @@ -43,7 +43,7 @@ theorem certify_invocation (program : CIL.Program) (method : Nat) (body : CIL.Me (finished : run program fuel method 0 args frame [] entered = .ok (final, values)) : InvocationCertificate program method args initial fuel final values := by refine ⟨?_, body, frame, entered, lookup, checked, setup, finished, live, ?_, ?_⟩ - · simpa only [invoke, lookup, checked, setup, Except.mapError, Bind.bind, Except.bind] + · simpa only [invoke_eq, lookup, checked, setup, Except.mapError, Bind.bind, Except.bind] using finished · intro observedArgs observedFrame observedStack observed visit exact runningVisits_live_state visit live @@ -78,19 +78,20 @@ theorem InvocationCertificate.of_invoke {program : CIL.Program} {method : Nat} (invoked : invoke program fuel method args initial = .ok (final, values)) : InvocationCertificate program method args initial fuel final values := by cases lookup : program[method]? with - | none => simp [invoke, lookup] at invoked + | none => simp [invoke_eq, lookup] at invoked | some body => cases checking : args.mapM (checkedValue initial) with | error fault => - simp [invoke, lookup, checking, Except.mapError, Bind.bind, Except.bind] at invoked + simp [invoke_eq, lookup, checking, Except.mapError, Bind.bind, Except.bind] at invoked | ok checked => obtain ⟨rfl, -⟩ := checkedValues_valid _ _ _ checking cases setup : enterFrame body checked initial with | error fault => - simp [invoke, lookup, checking, setup, Except.mapError, Bind.bind, Except.bind] at invoked + simp [invoke_eq, lookup, checking, setup, Except.mapError, Bind.bind, + Except.bind] at invoked | ok entry => obtain ⟨frame, entered⟩ := entry - simp only [invoke, lookup, checking, setup, Except.mapError, Bind.bind, Except.bind] + simp only [invoke_eq, lookup, checking, setup, Except.mapError, Bind.bind, Except.bind] at invoked exact .of_run formed world lookup checking setup invoked diff --git a/CIL/Safety/CertifiedContract.lean b/CIL/Safety/CertifiedContract.lean index 53f8c41f4..82fa0094a 100644 --- a/CIL/Safety/CertifiedContract.lean +++ b/CIL/Safety/CertifiedContract.lean @@ -64,7 +64,7 @@ theorem MethodContract.Certified.weaken {Input Output : Type} callee's `InvocationCertificate`, with its entered frame, live entry and visited states and returned state, besides the footprint and the typed output. -/ theorem run_call_of_certified {Input Output : Type} {c : MethodContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (certified : c.Certified program callee) (input : Input) @@ -98,7 +98,7 @@ theorem run_call_of_certified {Input Output : Type} {c : MethodContract Input Ou callee's `InvocationCertificate`, so beyond `run_call_of_contract_formed` it learns the callee's entered frame and that its entry and every visited state are live. -/ theorem run_call_of_contract_certified {Input Output : Type} {c : MethodContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (input : Input) diff --git a/CIL/Safety/CheckedHomes.lean b/CIL/Safety/CheckedHomes.lean index c3c2253aa..62b3aea62 100644 --- a/CIL/Safety/CheckedHomes.lean +++ b/CIL/Safety/CheckedHomes.lean @@ -28,6 +28,64 @@ namespace CIL.Safety Pure.pure, Except.pure] #print axioms checkedValue_span +/-- A call argument that is a scalar is checked as a pushed value is. -/ +@[cil_checked] theorem callArgument_scalar (frame : Frame) (memory : Memory) (value : CIL.Value) : + callArgument frame memory (.scalar value) = checkedValue memory (.scalar value) := rfl +#print axioms callArgument_scalar + +/-- A call argument that is a managed reference is checked as a pushed value is. -/ +@[cil_checked] theorem callArgument_reference (frame : Frame) (memory : Memory) + (reference : ManagedReference) : + callArgument frame memory (.reference reference) = + checkedValue memory (.reference reference) := rfl +#print axioms callArgument_reference + +/-- A call argument that is a readonly reference is checked as a pushed value is. -/ +@[cil_checked] theorem callArgument_readOnlyReference (frame : Frame) (memory : Memory) + (reference : ManagedReference) : + callArgument frame memory (.readOnlyReference reference) = + checkedValue memory (.readOnlyReference reference) := rfl +#print axioms callArgument_readOnlyReference + +/-- A call argument that is a reference span is checked as a pushed value is. -/ +@[cil_checked] theorem callArgument_span (frame : Frame) (memory : Memory) + (reference : ManagedReference) (length : Nat) : + callArgument frame memory (.span reference length) = + checkedValue memory (.span reference length) := rfl +#print axioms callArgument_span + +/-- A call argument that is a span view is checked as a pushed value is. -/ +@[cil_checked] theorem callArgument_bufferSpan (frame : Frame) (memory : Memory) + (view : SpanView) : + callArgument frame memory (.bufferSpan view) = + checkedValue memory (.bufferSpan view) := rfl +#print axioms callArgument_bufferSpan + +/-- A call argument that is an array view is checked as a pushed value is. -/ +@[cil_checked] theorem callArgument_array (frame : Frame) (memory : Memory) (view : ArrayView) : + callArgument frame memory (.array view) = checkedValue memory (.array view) := rfl +#print axioms callArgument_array + +/-- A call argument that is a method pointer is checked as a pushed value is. -/ +@[cil_checked] theorem callArgument_methodPointer (frame : Frame) (memory : Memory) + (pointer : CIL.MethodPointer) : + callArgument frame memory (.methodPointer pointer) = + checkedValue memory (.methodPointer pointer) := rfl +#print axioms callArgument_methodPointer + +/-- A call argument that is a method table is checked as a pushed value is. -/ +@[cil_checked] theorem callArgument_methodTable (frame : Frame) (memory : Memory) + (table : CIL.MethodTable) : + callArgument frame memory (.methodTable table) = + checkedValue memory (.methodTable table) := rfl +#print axioms callArgument_methodTable + +/-- A call argument that is an object reference is checked as a pushed value is. -/ +@[cil_checked] theorem callArgument_object (frame : Frame) (memory : Memory) + (reference : ManagedReference) : + callArgument frame memory (.object reference) = checkedValue memory (.object reference) := rfl +#print axioms callArgument_object + /-- Validating an array stack value, as a call does with each array argument, keeps the view. -/ @[cil_checked] theorem checkedValue_array (memory : Memory) (view : ArrayView) (valid : view.validate memory = .ok ()) : diff --git a/CIL/Safety/CheckedMemoryFacts.lean b/CIL/Safety/CheckedMemoryFacts.lean index 098070a84..96b98891d 100644 --- a/CIL/Safety/CheckedMemoryFacts.lean +++ b/CIL/Safety/CheckedMemoryFacts.lean @@ -46,11 +46,11 @@ attribute [cil_checked] load_word64_of_read load_vector128_of_read load_vector25 contents. -/ theorem step_store128_of_write {memory after : Memory} {target : Reference} {bits : BitVec 128} (written : write memory target (numberBytes bits.toNat 16) 1 = .ok after) : - ∀ body pc args frame rest, - step body (.memory .store128) pc args frame + ∀ body pc frame rest, + modelStep (some (.memory .store128)) body pc frame (.scalar (.v128 bits) :: .reference (.address target) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by - intro body pc args frame rest + intro body pc frame rest rw [step_memory_store128_eq, storeValue_vector128_of_write written] rfl #print axioms step_store128_of_write @@ -59,11 +59,11 @@ theorem step_store128_of_write {memory after : Memory} {target : Reference} {bit contents. -/ theorem step_store256_of_write {memory after : Memory} {target : Reference} {bits : BitVec 256} (written : write memory target (numberBytes bits.toNat 32) 1 = .ok after) : - ∀ body pc args frame rest, - step body (.memory .store256) pc args frame + ∀ body pc frame rest, + modelStep (some (.memory .store256)) body pc frame (.scalar (.v256 bits) :: .reference (.address target) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by - intro body pc args frame rest + intro body pc frame rest rw [step_memory_store256_eq, storeValue_vector256_of_write written] rfl #print axioms step_store256_of_write @@ -72,55 +72,85 @@ theorem step_store256_of_write {memory after : Memory} {target : Reference} {bit contents. -/ theorem step_store512_of_write {memory after : Memory} {target : Reference} {bits : BitVec 512} (written : write memory target (numberBytes bits.toNat 64) 1 = .ok after) : - ∀ body pc args frame rest, - step body (.memory .store512) pc args frame + ∀ body pc frame rest, + modelStep (some (.memory .store512)) body pc frame (.scalar (.v512 bits) :: .reference (.address target) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by - intro body pc args frame rest + intro body pc frame rest rw [step_memory_store512_eq, storeValue_vector512_of_write written] rfl #print axioms step_store512_of_write -/-- `store64` through an address whose eight-byte write succeeds steps to that write. The step +/-- `stind.i8` through an address whose eight-byte write succeeds steps to that write. The step holds in any method and frame. -/ -theorem step_store64_of_write {memory after : Memory} {target : Reference} {word : CIL.W64} +theorem step_stind_i8_of_write {memory after : Memory} {target : Reference} {word : CIL.W64} (written : write memory target (numberBytes word.toNat 8) 1 = .ok after) : ∀ body pc args frame rest, - step body .store64 pc args frame + step body (.stind .i8) pc args frame (.scalar (.i64 word) :: .reference (.address target) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by intro body pc args frame rest - rw [step_store64_reference_eq, storeValue_word64_of_write written] + rw [step_stind_i8_eq, storeValue_word64_of_write written] rfl -/-- A 1-, 2- or 4-byte scalar store of an int through an address whose write succeeds steps to +/-- `stind.i` through an address whose eight-byte write succeeds steps to that write, as + `stind.i8` does. -/ +theorem step_stind_i_of_write {memory after : Memory} {target : Reference} {word : CIL.W64} + (written : write memory target (numberBytes word.toNat 8) 1 = .ok after) : + ∀ body pc args frame rest, + step body (.stind .i) pc args frame + (.scalar (.i64 word) :: .reference (.address target) :: rest) memory = + .ok (.next (pc + 1) rest frame after) := by + intro body pc args frame rest + rw [step_stind_i_eq, storeValue_word64_of_write written] + rfl + +/-- A one-, two- or four-byte `stind` of an int through an address whose write succeeds steps to that write. The step holds in any method and frame. -/ -theorem step_storeScalar_of_write {memory after : Memory} {target : Reference} {width : Nat} - {bits : CIL.W32} (written : write memory target (numberBytes bits.toNat width) 1 = .ok after) - (narrow : width = 1 ∨ width = 2 ∨ width = 4 := by decide) : +theorem step_stind_narrow_of_write (kind : CIL.NumericKind) {memory after : Memory} + {target : Reference} {bits : CIL.W32} + (written : write memory target (numberBytes bits.toNat kind.width) 1 = .ok after) + (narrow : kind.width ≠ 8 := by decide) : ∀ body pc args frame rest, - step body (.storeScalar width) pc args frame + step body (.stind kind) pc args frame (.scalar (.i32 bits) :: .reference (.address target) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by intro body pc args frame rest - rw [step_storeScalar_i32_eq body pc args frame memory width narrow] + rw [step_stind_i32_eq body pc args frame memory kind narrow] simp [referenceAt, checkedAt, ordinaryAccessAlignment, written, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] +/-- A 1-, 2- or 4-byte scalar store of an int through an address whose write succeeds steps to + that write. The step holds in any method and frame. -/ +theorem transfer_i32_of_write {memory after : Memory} {target : Reference} {width : Nat} + {bits : CIL.W32} (written : write memory target (numberBytes bits.toNat width) 1 = .ok after) + (narrow : width = 1 ∨ width = 2 ∨ width = 4 := by decide) : + ∀ pc frame rest, + typedTransferCall (.scalar width false) true pc frame + (.scalar (.i32 bits) :: .reference (.address target) :: rest) memory = + .ok (.next (pc + 1) rest frame after) := by + intro pc frame rest + rcases narrow with rfl | rfl | rfl <;> + simp [typedTransferCall, observedTransfer, transferValues, unspecifiedOperands, + Value.hasUnspecified, CIL.Value.hasUnspecified, referenceAt, checkedAt, ordinaryAccessAlignment, written, Except.mapError, Bind.bind, + Except.bind, Pure.pure, Except.pure] + /-- An eight-byte scalar store of a long through an address whose write succeeds steps to that write. The step holds in any method and frame. -/ -theorem step_storeScalar_i64_of_write {memory after : Memory} {target : Reference} +theorem transfer_i64_of_write {memory after : Memory} {target : Reference} {bits : CIL.W64} (written : write memory target (numberBytes bits.toNat 8) 1 = .ok after) : - ∀ body pc args frame rest, - step body (.storeScalar 8) pc args frame + ∀ pc frame rest, + typedTransferCall (.scalar 8 false) true pc frame (.scalar (.i64 bits) :: .reference (.address target) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by - intro body pc args frame rest - rw [step_storeScalar_i64_eq] - simp [referenceAt, checkedAt, ordinaryAccessAlignment, written, Except.mapError, Bind.bind, + intro pc frame rest + simp [typedTransferCall, observedTransfer, transferValues, unspecifiedOperands, + Value.hasUnspecified, CIL.Value.hasUnspecified, referenceAt, checkedAt, ordinaryAccessAlignment, written, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] -#print axioms step_store64_of_write -#print axioms step_storeScalar_of_write -#print axioms step_storeScalar_i64_of_write +#print axioms step_stind_i8_of_write +#print axioms step_stind_i_of_write +#print axioms step_stind_narrow_of_write +#print axioms transfer_i32_of_write +#print axioms transfer_i64_of_write end CIL.Safety diff --git a/CIL/Safety/CheckedReferenceBounds.lean b/CIL/Safety/CheckedReferenceBounds.lean index df11d518b..c2ff93d5c 100644 --- a/CIL/Safety/CheckedReferenceBounds.lean +++ b/CIL/Safety/CheckedReferenceBounds.lean @@ -110,7 +110,7 @@ theorem Memory.WellFormed.span_below {memory : Memory} (formed : memory.WellForm theorem Value.Valid.below {memory : Memory} (formed : memory.WellFormed) {value : Value} (valid : value.Valid memory) : value.Below memory.nextIdentity := by cases value with - | scalar value | methodPointer _ | methodTable _ => trivial + | typeHandle _ | runtimeType _ | scalar value | methodPointer _ | methodTable _ | spanHome _ => trivial | bufferSpan view => exact formed.span_below view valid | array view => exact formed.span_below view.storage valid | reference reference | readOnlyReference reference | span reference length => diff --git a/CIL/Safety/CheckedValueRenaming.lean b/CIL/Safety/CheckedValueRenaming.lean index df74763a6..22d29b08d 100644 --- a/CIL/Safety/CheckedValueRenaming.lean +++ b/CIL/Safety/CheckedValueRenaming.lean @@ -30,23 +30,15 @@ theorem AllocationRenaming.bitcastValue (mapping : AllocationRenaming) repeat first | exact .ok rfl | exact .error _ | split #print axioms AllocationRenaming.bitcastValue -/-- The unspecified-operand guard sees the same scalar operands after reference renaming. -/ -theorem AllocationRenaming.unspecifiedOperands (mapping : AllocationRenaming) - (count : Nat) (values : List Value) : - unspecifiedOperands count (values.map mapping.value) = unspecifiedOperands count values := by - simp only [CIL.Safety.unspecifiedOperands, ← List.map_take, List.any_map, - Function.comp_def, mapping.hasUnspecified] -#print axioms AllocationRenaming.unspecifiedOperands - -/-- Scalar-only decoding rejects the same non-scalar shapes after allocation renaming. -/ -theorem AllocationRenaming.scalars (mapping : AllocationRenaming) (values : List Value) : - scalars (values.map mapping.value) = scalars values := by +/-- Scalar-only decoding rejects the same non-scalar shapes, homes included, after renaming. -/ +theorem LocalIndexMap.scalars (indices : LocalIndexMap) (mapping : AllocationRenaming) + (values : List Value) : scalars (values.map (indices.value mapping)) = scalars values := by induction values with | nil => rfl | cons value rest induction => - cases value <;> simp only [List.map_cons, AllocationRenaming.value, CIL.Safety.scalars, + cases value <;> simp only [List.map_cons, LocalIndexMap.value, CIL.Safety.scalars, induction] -#print axioms AllocationRenaming.scalars +#print axioms LocalIndexMap.scalars /-- Managed reference extraction maps the successful address and preserves null faults. -/ theorem AllocationRenaming.referenceAt (mapping : AllocationRenaming) @@ -66,7 +58,8 @@ theorem Memory.Renamed.checkedValue {mapping : AllocationRenaming} ResultRenamed mapping (fun left right => right = mapping.value left) (checkedValue source value) (checkedValue target (mapping.value value)) := by cases value with - | methodPointer _ | methodTable _ => exact .ok rfl + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ => exact .ok rfl + | spanHome _ => exact .error .invalidState | scalar value => dsimp only [AllocationRenaming.value, CIL.Safety.checkedValue] split <;> first | exact .ok rfl | exact .error .invalidState diff --git a/CIL/Safety/CodeAt.lean b/CIL/Safety/CodeAt.lean index 0a8907a2a..6599a154d 100644 --- a/CIL/Safety/CodeAt.lean +++ b/CIL/Safety/CodeAt.lean @@ -15,12 +15,12 @@ namespace CIL.Safety /-- `segment` occupies `body.code` from position `start`: the instructions at `start`, `start + 1`, ... are exactly those of `segment`, in order. -/ -def CodeAt (body : CIL.Method) (start : Nat) (segment : List CIL.Op) : Prop := +def CodeAt (body : CIL.Method) (start : Nat) (segment : List CIL.Instruction) : Prop := (body.code.drop start).take segment.length = segment namespace CodeAt -variable {body : CIL.Method} {start : Nat} {segment : List CIL.Op} +variable {body : CIL.Method} {start : Nat} {segment : List CIL.Instruction} /-- A placement is exactly agreement of `body.code` with `segment` at each position of the segment, shifted by `start`. -/ @@ -42,14 +42,14 @@ theorem iff_lookup : CodeAt body start segment ↔ #print axioms iff_lookup /-- The instruction at each position of a placed segment is the segment's instruction there. -/ -theorem lookup {op : CIL.Op} (placed : CodeAt body start segment) (index : Nat) +theorem lookup {op : CIL.Instruction} (placed : CodeAt body start segment) (index : Nat) (found : segment[index]? = some op) : body.code[start + index]? = some op := by rw [iff_lookup.mp placed index (List.getElem?_eq_some_iff.mp found).1, found] #print axioms lookup /-- The positional form a placement replaces: `segment` follows `start` instructions. -/ -theorem of_eq_append {leading tail : List CIL.Op} +theorem of_eq_append {leading tail : List CIL.Instruction} (placed : body.code = leading ++ segment ++ tail) (offset : leading.length = start) : CodeAt body start segment := by unfold CodeAt @@ -89,7 +89,7 @@ theorem nil : CodeAt body start [] := by #print axioms nil /-- A segment is placed at `start` exactly when its two parts are placed one after the other. -/ -theorem append_iff {left right : List CIL.Op} : +theorem append_iff {left right : List CIL.Instruction} : CodeAt body start (left ++ right) ↔ CodeAt body start left ∧ CodeAt body (start + left.length) right := by simp only [iff_lookup, List.length_append] @@ -109,28 +109,30 @@ theorem append_iff {left right : List CIL.Op} : #print axioms append_iff /-- The first part of a placed segment is placed at the same start. -/ -theorem append_left {left right : List CIL.Op} (placed : CodeAt body start (left ++ right)) : +theorem append_left {left right : List CIL.Instruction} + (placed : CodeAt body start (left ++ right)) : CodeAt body start left := (append_iff.mp placed).1 #print axioms append_left /-- The second part of a placed segment is placed after the first part. -/ -theorem append_right {left right : List CIL.Op} (placed : CodeAt body start (left ++ right)) : +theorem append_right {left right : List CIL.Instruction} + (placed : CodeAt body start (left ++ right)) : CodeAt body (start + left.length) right := (append_iff.mp placed).2 #print axioms append_right /-- Adjacent placed segments compose into one placed segment. -/ -theorem append {left right : List CIL.Op} (first : CodeAt body start left) +theorem append {left right : List CIL.Instruction} (first : CodeAt body start left) (second : CodeAt body (start + left.length) right) : CodeAt body start (left ++ right) := append_iff.mpr ⟨first, second⟩ #print axioms append /-- A placed segment's first instruction is at `start` and the rest follows it. -/ -theorem cons_iff {op : CIL.Op} {rest : List CIL.Op} : +theorem cons_iff {op : CIL.Instruction} {rest : List CIL.Instruction} : CodeAt body start (op :: rest) ↔ body.code[start]? = some op ∧ CodeAt body (start + 1) rest := by rw [← List.singleton_append, append_iff, iff_lookup (segment := [op])] @@ -159,7 +161,7 @@ theorem take (placed : CodeAt body start segment) (count : Nat) : #print axioms take /-- A segment inside a placed segment is placed after the instructions that precede it. -/ -theorem middle {before inner after : List CIL.Op} +theorem middle {before inner after : List CIL.Instruction} (placed : CodeAt body start (before ++ inner ++ after)) : CodeAt body (start + before.length) inner := append_left (append_right (List.append_assoc _ _ _ ▸ placed)) diff --git a/CIL/Safety/ConstructComposition.lean b/CIL/Safety/ConstructComposition.lean index 740664bba..8298f4aac 100644 --- a/CIL/Safety/ConstructComposition.lean +++ b/CIL/Safety/ConstructComposition.lean @@ -14,7 +14,7 @@ namespace CIL.Safety /-- A constructor must return void, and its full initialized value must be loaded successfully before the caller resumes with that value. -/ theorem run_construct_of_invoke {program : CIL.Program} {fuel method pc callee : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack arguments rest values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest values : List Value} {frame constructedFrame : Frame} {temporary : Reference} {kind : CIL.LocalKind} {value : Value} {memory updated childResult result : Memory} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) @@ -26,25 +26,26 @@ theorem run_construct_of_invoke {program : CIL.Program} {fuel method pc callee : childResult = .ok (result, values)) : run program (fuel + 1) method pc args frame stack memory = .ok (result, values) := by cases hb : program[callee]? with - | none => simp [invoke, hb] at child + | none => simp [invoke_eq, hb] at child | some childBody => cases ha : arguments.mapM (checkedValue updated) with - | error fault => simp [invoke, hb, ha, Except.mapError, Bind.bind, Except.bind] at child + | error fault => simp [invoke_eq, hb, ha, Except.mapError, Bind.bind, Except.bind] at child | ok checked => have same := (checkedValues_valid _ _ _ ha).1 subst checked cases he : enterFrame childBody arguments updated with - | error fault => simp [invoke, hb, ha, he, Except.mapError, Bind.bind, Except.bind] at child + | error fault => simp [invoke_eq, hb, ha, he, Except.mapError, Bind.bind, + Except.bind] at child | ok entered => obtain ⟨childFrame, childMemory⟩ := entered - simp only [invoke, hb, ha, he, Except.mapError, Bind.bind, Except.bind] at child - simp only [run, methodFound, instructionFound, stepped, hb, he, child, + simp only [invoke_eq, hb, ha, he, Except.mapError, Bind.bind, Except.bind] at child + simp only [run_succ, methodFound, instructionFound, stepped, hb, he, child, loaded, List.isEmpty_nil, Bool.not_true, Bool.false_eq_true, ite_false, continuation, Except.mapError, Bind.bind, Except.bind] /-- Independently sufficient budgets combine without requiring equal proof fuel. -/ theorem run_construct_exists {program : CIL.Program} {method pc callee : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack arguments rest values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest values : List Value} {frame constructedFrame : Frame} {temporary : Reference} {kind : CIL.LocalKind} {value : Value} {memory updated childResult result : Memory} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) diff --git a/CIL/Safety/ConstructContract.lean b/CIL/Safety/ConstructContract.lean index 70ad95079..e8756440c 100644 --- a/CIL/Safety/ConstructContract.lean +++ b/CIL/Safety/ConstructContract.lean @@ -8,7 +8,7 @@ import CIL.Safety.ConstructComposition Applies a constructor's `MethodContract` at a fetched `newobj` without stepping through the constructor body. `MethodContract.Constructs` says what the contract must promise about the constructed bytes, `ConstructStorage` is what `newobj` guarantees about the storage it hands -over, and the `cil_contract_construct` tactic applies `run_newAggregate_of_contract` in a +over, and the `cil_contract_construct` tactic applies `run_newobj_aggregate_of_contract` in a consumer proof. -/ @@ -48,8 +48,9 @@ theorem ConstructStorage.confined {memory updated final : Memory} {temporary : R resumes on the read-back value. -/ theorem run_construct_of_contract {Input Output : Type} {c : MethodContract Input Output} {program : CIL.Program} {method pc callee : Nat} {size : Fin (2^64)} {body : CIL.Method} - {op : CIL.Op} {args stack arguments rest : List Value} {frame constructedFrame : Frame} - {temporary : Reference} {memory updated : Memory} {this : Input → Reference} + {op : CIL.Instruction} {args stack arguments rest : List Value} + {frame constructedFrame : Frame} {temporary : Reference} {memory updated : Memory} + {this : Input → Reference} {value : Input → Output → List (BitVec 8)} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (constructs : c.Constructs size this value) (input : Input) (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) @@ -81,7 +82,7 @@ theorem run_construct_of_contract {Input Output : Type} {c : MethodContract Inpu /-- A successful construction from a well-formed memory returns a well-formed memory and keeps the static world and catalog agreement. -/ theorem construct_frame_invariants {program : CIL.Program} {method pc callee fuel : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack arguments rest values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest values : List Value} {frame constructedFrame : Frame} {temporary : Reference} {kind : CIL.LocalKind} {memory updated final : Memory} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) @@ -106,8 +107,9 @@ theorem construct_frame_invariants {program : CIL.Program} {method pc callee fue frame invariants. -/ theorem run_construct_of_contract_formed {Input Output : Type} {c : MethodContract Input Output} {program : CIL.Program} {method pc callee : Nat} {size : Fin (2^64)} {body : CIL.Method} - {op : CIL.Op} {args stack arguments rest : List Value} {frame constructedFrame : Frame} - {temporary : Reference} {memory updated : Memory} {this : Input → Reference} + {op : CIL.Instruction} {args stack arguments rest : List Value} + {frame constructedFrame : Frame} {temporary : Reference} {memory updated : Memory} + {this : Input → Reference} {value : Input → Output → List (BitVec 8)} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (constructs : c.Constructs size this value) (input : Input) (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) @@ -132,19 +134,23 @@ theorem run_construct_of_contract_formed {Input Output : Type} {c : MethodContra continuation final out call (construct_frame_invariants methodFound instructionFound stepped formed invoked) -/-- Construct at a fetched `newobj` from its constructor's contract alone; `input` names the - fresh storage `newobj` zeroes. -/ -theorem run_newAggregate_of_contract {Input Output : Type} {c : MethodContract Input Output} - {program : CIL.Program} {method pc callee argc : Nat} {size : Fin (2^64)} {body : CIL.Method} - {args stack arguments : List Value} {frame : Frame} {memory : Memory} +/-- Construct at a fetched `newobj` under an `.aggregate` link from its constructor's contract + alone; `input` names the fresh storage `newobj` zeroes. -/ +theorem run_newobj_aggregate_of_contract {Input Output : Type} {c : MethodContract Input Output} + {program : CIL.Program} {method pc callee : Nat} {constructor : CIL.Extraction.MethodRef} + {size : Fin (2^64)} + {body : CIL.Method} {args stack arguments : List Value} {frame : Frame} {memory : Memory} {this : Input → Reference} {value : Input → Output → List (BitVec 8)} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (constructs : c.Constructs size this value) (input : Reference → Input) (methodFound : program[method]? = some body) - (instructionFound : body.code[pc]? = some (.newAggregate callee argc size)) - (positive : 0 < size.val) (formed : memory.WellFormed) (enough : argc ≤ stack.length) - (checked : (stack.take argc).reverse.mapM (checkedValue memory) = .ok arguments) + (instructionFound : body.code[pc]? = some (.newobj constructor)) + (linked : body.linkAt pc = some (.aggregate callee size)) + (positive : 0 < size.val) (formed : memory.WellFormed) + (enough : constructor.parameters.length ≤ stack.length) + (checked : (stack.take constructor.parameters.length).reverse.mapM (checkedValue memory) = + .ok arguments) (located : ∀ temporary, this (input temporary) = temporary) (encoded : ∀ temporary, c.args (input temporary) = .reference (.address temporary) :: arguments) @@ -156,7 +162,8 @@ theorem run_newAggregate_of_contract {Input Output : Type} {c : MethodContract I ∃ fuel result returned, run program fuel method (pc + 1) args { frame with owned := temporary.allocation :: frame.owned } - (.scalar (.aggregate (value (input temporary) out)) :: stack.drop argc) final = + (.scalar (.aggregate (value (input temporary) out)) :: + stack.drop constructor.parameters.length) final = .ok (result, returned) ∧ post result returned) (known : arguments.any Value.hasUnspecified = false := by cil_known_arguments) : @@ -164,15 +171,16 @@ theorem run_newAggregate_of_contract {Input Output : Type} {c : MethodContract I run program fuel method pc args frame stack memory = .ok (result, returned) ∧ post result returned := by have same := (checkedValues_valid _ _ _ checked).1 - have length : arguments.length = argc := by + have length : arguments.length = constructor.parameters.length := by rw [same, List.length_reverse, List.length_take, Nat.min_eq_left enough] - have split : arguments.reverse ++ stack.drop argc = stack := by + have split : arguments.reverse ++ stack.drop constructor.parameters.length = stack := by rw [same, List.reverse_reverse, List.take_append_drop] rw [same] at checked obtain ⟨temporary, updated, stepped, zeroed, writable, preserved, fresh⟩ := - step_newAggregate (body := body) (pc := pc) (callee := callee) size positive args arguments - (stack.drop argc) frame memory formed (by rw [same]; exact checked) known - rw [length, split] at stepped + step_newobj_aggregate (body := body) (pc := pc) (constructor := constructor) size positive + linked args arguments (stack.drop constructor.parameters.length) frame memory formed + length.symm (by rw [same]; exact checked) known + rw [split] at stepped have entered : updated.WellFormed := programStep_preserves_wellFormed _ _ _ _ _ _ _ _ formed ⟨body, _, methodFound, instructionFound, stepped⟩ have storage : ConstructStorage memory updated temporary size := @@ -186,7 +194,7 @@ theorem run_newAggregate_of_contract {Input Output : Type} {c : MethodContract I #print axioms run_construct_of_contract #print axioms construct_frame_invariants #print axioms run_construct_of_contract_formed -#print axioms run_newAggregate_of_contract +#print axioms run_newobj_aggregate_of_contract /-- Apply a constructor contract at the fetched `newobj`, leaving `wellFormed`, the `precondition` and the continuation. -/ @@ -196,9 +204,10 @@ macro_rules | `(tactic| cil_contract_construct $holds $constructs $input [$facts:term,*]) => `(tactic| ( try apply CIL.Safety.WP.adequate.mpr - refine CIL.Safety.run_newAggregate_of_contract $holds $constructs $input + refine CIL.Safety.run_newobj_aggregate_of_contract $holds $constructs $input (by cil_lookup [$[$facts:term],*]) (by cil_lookup [$[$facts:term],*]) + (by first | rfl | simp only [cil_code, cil_step, $[$facts:term],*]) (by decide) ?wellFormed (by simp) (by first | rfl | (simp [CIL.Safety.checkedValue, CIL.Safety.numericValue, $[$facts:term],*] <;> diff --git a/CIL/Safety/ConstructSetup.lean b/CIL/Safety/ConstructSetup.lean index f8f68e10a..03de77e25 100644 --- a/CIL/Safety/ConstructSetup.lean +++ b/CIL/Safety/ConstructSetup.lean @@ -1,11 +1,11 @@ -import CIL.Safety.ArgumentHomeValues import CIL.Safety.AggregateHomeValues import CIL.Safety.Execution /-! # Constructor storage setup -The exact checked `step` for `newobj` on a 256-bit vector or an unmanaged struct: it allocates +The exact checked `step` for `newobj` under an `.aggregate` link, a constructor of an unmanaged +struct: it allocates fresh zeroed, writable, caller-owned storage and passes its address as the constructor's first argument, leaving all older memory unchanged. -/ @@ -17,43 +17,19 @@ namespace CIL.Safety macro "cil_known_arguments" : tactic => `(tactic| simp [CIL.Safety.Value.hasUnspecified, CIL.Value.hasUnspecified]) -/-- The actual new-value step allocates initialized caller-owned storage and - passes its checked address to the constructor. -/ -theorem step_newVector256 {body : CIL.Method} {pc callee : Nat} +/-- Unmanaged constructors receive fresh initialized aggregate storage of the positive + representable width the call site's link states; the constructor is the one the link names, and + it takes the constructor's parameters. -/ +theorem step_newobj_aggregate {body : CIL.Method} {pc callee : Nat} + {constructor : CIL.Extraction.MethodRef} (size : Fin (2^64)) (positive : 0 < size.val) + (linked : body.linkAt pc = some (.aggregate callee size)) (args arguments rest : List Value) (frame : Frame) (memory : Memory) (wellFormed : memory.WellFormed) + (count : constructor.parameters.length = arguments.length) (checked : arguments.mapM (checkedValue memory) = .ok arguments) (known : arguments.any Value.hasUnspecified = false := by cil_known_arguments) : ∃ temporary updated, - step body (.newVector256 callee arguments.length) pc args frame (arguments.reverse ++ rest) - memory = - .ok (.construct callee (.reference (.address temporary) :: arguments) rest - { frame with owned := temporary.allocation :: frame.owned } temporary .vector256 - updated) ∧ - read updated temporary 32 1 = .ok (numberBytes 0 32) ∧ - access updated temporary 32 1 true = .ok () ∧ - MemoryBelow memory.nextIdentity memory updated ∧ - temporary.allocation = memory.nextIdentity := by - obtain ⟨temporary, allocated, updated, home, stored, _, loaded, writable, preserved⟩ := - allocate_initialized256 memory frame.activation (BitVec.ofNat 256 0) wellFormed - refine ⟨temporary, updated, ?_, loaded, writable, preserved, - (allocateHome_fresh _ _ _ _ _ home).2.1⟩ - simp [step, observerCount, unspecifiedOperands, known, checked, localWidth, home, stored, - Bind.bind, Except.bind, Pure.pure, Except.pure] - -#print axioms step_newVector256 - -/-- Unmanaged constructors receive fresh initialized aggregate storage of any - positive representable width, without requiring a vector representation. -/ -theorem step_newAggregate {body : CIL.Method} {pc callee : Nat} - (size : Fin (2^64)) (positive : 0 < size.val) - (args arguments rest : List Value) (frame : Frame) (memory : Memory) - (wellFormed : memory.WellFormed) - (checked : arguments.mapM (checkedValue memory) = .ok arguments) - (known : arguments.any Value.hasUnspecified = false := by cil_known_arguments) : - ∃ temporary updated, - step body (.newAggregate callee arguments.length size) pc args frame - (arguments.reverse ++ rest) memory = + step body (.newobj constructor) pc args frame (arguments.reverse ++ rest) memory = .ok (.construct callee (.reference (.address temporary) :: arguments) rest { frame with owned := temporary.allocation :: frame.owned } temporary (.aggregate size) updated) ∧ @@ -66,13 +42,15 @@ theorem step_newAggregate {body : CIL.Method} {pc callee : Nat} (by simp) wellFormed refine ⟨temporary, updated, ?_, loaded, writable, preserved, fresh⟩ have nonzero : size ≠ 0 := by intro h; have := congrArg Fin.val h; simp at this; omega - simp [step, observerCount, unspecifiedOperands, known, nonzero, checked, home, + have called := callArguments_of_checked (frame := frame) checked + simp [step, observerCount, newobjStep, constructAggregate, unspecifiedOperands, linked, count, + known, nonzero, called, home, Nat.not_lt_of_ge (Nat.le_add_right arguments.length rest.length), Bind.bind, Except.bind, Pure.pure, Except.pure] simp only [Nat.reducePow, BitVec.ofNat_eq_ofNat] at stored rw [stored] -#print axioms step_newAggregate +#print axioms step_newobj_aggregate end CIL.Safety diff --git a/CIL/Safety/ConstructorStepRenaming.lean b/CIL/Safety/ConstructorStepRenaming.lean index 4ed3e012a..50c26eb53 100644 --- a/CIL/Safety/ConstructorStepRenaming.lean +++ b/CIL/Safety/ConstructorStepRenaming.lean @@ -1,18 +1,24 @@ +import CIL.Safety.BoundaryStepRenaming import CIL.Safety.StepTransport import CIL.Safety.CheckedValueRenaming import CIL.Safety.CheckedReferenceBounds import CIL.Safety.ValueMemoryRenaming import CIL.Safety.FrameHomeRenaming import CIL.Safety.LocalStoreRenaming +import CIL.Safety.AllocatingStepRenaming -/-!+# Constructor temporary storage under allocation renaming +/-!+# Constructions under allocation renaming -Constructor storage extends the current correspondence before the child invocation. The caller -retains ownership and both initial argument lists refer to the same mapped older allocations. +A construction's storage, a caller-owned temporary or a fresh instance, extends the current +correspondence before the child invocation, and both initial argument lists refer to the same +mapped older allocations. -/ namespace CIL.Safety +-- Stack lists are renamed by the local map of the frame that holds them. +variable {indices : LocalIndexMap} + /-- A failed checked prefix reports the corresponding fault before any allocation occurs. -/ private theorem renamed_failure {mapping : AllocationRenaming} {relation : α → β → Prop} {fault : ExecutionFault} {target : ExecutionResult β} @@ -20,146 +26,120 @@ private theorem renamed_failure {mapping : AllocationRenaming} {relation : α target = .error (mapping.fault fault) := by cases related; rfl #print axioms renamed_failure -/-- A vector constructor reserves corresponding initialized storage owned by the caller. -/ -theorem Memory.Renamed.stepNewVector256 {indices : LocalIndexMap} {mapping : AllocationRenaming} +/-- A value-type construction reserves corresponding initialized storage owned by the caller. -/ +theorem Memory.Renamed.constructAggregate {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) (staticBounds : ∀ binding ∈ source.staticBindings, binding.2.allocation < mapping.sourceLimit) - (sourceBody targetBody : CIL.Method) (callee argc pc : Nat) (arguments stack : List Value) + (callee argc : Nat) (size : Fin (2 ^ 64)) (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) : ∃ later, mapping.Extends later ∧ ResultRenamed later (FrameAction.Renamed indices later scopes) - (step sourceBody (.newVector256 callee argc) pc arguments sourceFrame stack source) - (step targetBody (.newVector256 callee argc) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step, mapping.unspecifiedOperands, Bool.or_false, List.length_map, - ← List.map_take, ← List.map_drop, ← List.map_reverse] + (CIL.Safety.constructAggregate callee argc size sourceFrame stack source) + (CIL.Safety.constructAggregate callee argc size targetFrame + (stack.map (indices.value mapping)) + target) := by + unfold CIL.Safety.constructAggregate + simp only [List.length_map, ← List.map_take, ← List.map_drop, ← List.map_reverse] split · exact ⟨mapping, .refl mapping, .error _⟩ - · split - · exact ⟨mapping, .refl mapping, .error _⟩ - · have checks := related.checkedValues (stack.take argc).reverse (by + · have checks := frames.callArguments related (stack.take argc).reverse (by + intro value member + exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member))) + cases checked : + (stack.take argc).reverse.mapM (CIL.Safety.callArgument sourceFrame source) with + | error fault => + rw [checked] at checks + rw [renamed_failure checks] + exact ⟨mapping, .refl mapping, .error fault⟩ + | ok operands => + have operandBounds : ∀ value ∈ operands, value.Below mapping.sourceLimit := by + intro value member + rw [related.sourceLimit] + exact (callArguments_valid checked value member).below sourceFormed + rw [checked] at checks + obtain ⟨targetOperands, targetChecked, rfl⟩ := checks.success + obtain ⟨sourceHome, targetHome, sourceMade, targetMade, homes⟩ := + related.allocateHome sourceFormed targetFormed staticBounds sourceFrame.activation + targetFrame.activation size 1 frames.activation size.isLt (by decide) + have freshBound : source.nextIdentity < mapping.allocate.sourceLimit := by + rw [← related.sourceLimit] + exact Nat.lt_succ_self _ + have freshImage : mapping.allocate.image source.nextIdentity = target.nextIdentity := by + rw [← related.sourceLimit, mapping.allocate_fresh, related.targetLimit] + have owned := (frames.extend mapping.allocate_extends (fun _ _ scope => scope)).addOwned + source.nextIdentity freshBound 0 + simp only [Nat.add_zero, freshImage] at owned + have stored := homes.storeLocal (indices := .nil) + (.bytes (.aggregate size) ⟨source.nextIdentity, 0⟩) + (.scalar (.aggregate (List.replicate size 0))) freshBound (by trivial) + simp only [AllocationRenaming.localSlot, LocalIndexMap.value, + AllocationRenaming.reference, freshImage] at stored + refine ⟨mapping.allocate, mapping.allocate_extends, ?_⟩ + simp only [targetChecked, sourceMade, targetMade, Bind.bind, Except.bind] + apply stored.bind + intro sourceResult targetResult results + rcases sourceResult with ⟨sourceSlot, sourceStored⟩ + rcases targetResult with ⟨targetSlot, targetStored⟩ + have memories := results.2 + have operandsSame := mapping.allocate_extends.values operands operandBounds + have restSame := indices.values_extends mapping.allocate_extends (stack.drop argc) (by intro value member - exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member))) - cases checked : (stack.take argc).reverse.mapM (CIL.Safety.checkedValue source) with - | error fault => - rw [checked] at checks - rw [renamed_failure checks] - exact ⟨mapping, .refl mapping, .error fault⟩ - | ok operands => - have operandBounds : ∀ value ∈ operands, value.Below mapping.sourceLimit := by - intro value member - rw [related.sourceLimit] - exact ((checkedValues_valid _ _ _ checked).2 value member).below sourceFormed - rw [checked] at checks - obtain ⟨targetOperands, targetChecked, rfl⟩ := checks.success - obtain ⟨sourceHome, targetHome, sourceMade, targetMade, homes⟩ := - related.allocateHome sourceFormed targetFormed staticBounds sourceFrame.activation - targetFrame.activation (localWidth .vector256) 1 frames.activation - (by decide) (by decide) - have freshBound : source.nextIdentity < mapping.allocate.sourceLimit := by - rw [← related.sourceLimit] - exact Nat.lt_succ_self _ - have freshImage : mapping.allocate.image source.nextIdentity = target.nextIdentity := by - rw [← related.sourceLimit, mapping.allocate_fresh, related.targetLimit] - have owned := (frames.extend mapping.allocate_extends (fun _ _ scope => scope)).addOwned - source.nextIdentity freshBound 0 - simp only [Nat.add_zero, freshImage] at owned - have stored := homes.storeValue (.address ⟨source.nextIdentity, 0⟩) freshBound (.v256 0) - simp only [AllocationRenaming.managedReference, AllocationRenaming.reference, - freshImage] at stored - refine ⟨mapping.allocate, mapping.allocate_extends, ?_⟩ - simp only [targetChecked, sourceMade, targetMade, Bind.bind, Except.bind] - apply stored.bind - intro sourceStored targetStored memories - have operandsSame := mapping.allocate_extends.values operands operandBounds - have restSame := mapping.allocate_extends.values (stack.drop argc) (by - intro value member - exact bounds value (List.mem_of_mem_drop member)) - rw [← operandsSame, ← restSame] - simpa only [List.map_cons, AllocationRenaming.value, - AllocationRenaming.managedReference, AllocationRenaming.reference, freshImage, - Pure.pure, Except.pure] using - ResultRenamed.ok (FrameAction.Renamed.construct (callee := callee) - (arguments := .reference (.address ⟨source.nextIdentity, 0⟩) :: operands) - (rest := stack.drop argc) (temporary := ⟨source.nextIdentity, 0⟩) - (kind := .vector256) memories owned freshBound) -#print axioms Memory.Renamed.stepNewVector256 + exact bounds value (List.mem_of_mem_drop member)) + rw [← operandsSame, ← restSame] + simpa only [List.map_cons, AllocationRenaming.value, LocalIndexMap.value, + AllocationRenaming.managedReference, AllocationRenaming.reference, freshImage, + Pure.pure, Except.pure] using + ResultRenamed.ok (FrameAction.Renamed.construct (callee := callee) + (arguments := .reference (.address ⟨source.nextIdentity, 0⟩) :: operands) + (rest := stack.drop argc) (temporary := ⟨source.nextIdentity, 0⟩) + (kind := .aggregate size) memories owned freshBound) +#print axioms Memory.Renamed.constructAggregate -/-- An aggregate constructor reserves corresponding initialized storage owned by the caller. -/ -theorem Memory.Renamed.stepNewAggregate {indices : LocalIndexMap} {mapping : AllocationRenaming} +/-- Related bodies construct with the same link or runtime model. Only a linked construction + can extend the allocation renaming. -/ +theorem Memory.Renamed.stepNewobj {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) (staticBounds : ∀ binding ∈ source.staticBindings, binding.2.allocation < mapping.sourceLimit) - (sourceBody targetBody : CIL.Method) (callee argc pc : Nat) (size : Fin (2 ^ 64)) - (arguments stack : List Value) - (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) : + (sourceBody targetBody : CIL.Method) (constructor : CIL.Extraction.MethodRef) (pc : Nat) + (metadata : StepMetadata sourceBody targetBody) (arguments stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + (step sourceBody (.newobj constructor) pc arguments sourceFrame stack source)) : ∃ later, mapping.Extends later ∧ ResultRenamed later (FrameAction.Renamed indices later scopes) - (step sourceBody (.newAggregate callee argc size) pc arguments sourceFrame stack source) - (step targetBody (.newAggregate callee argc size) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by - simp only [step, mapping.unspecifiedOperands, Bool.or_false, List.length_map, - ← List.map_take, ← List.map_drop, ← List.map_reverse] - split - · exact ⟨mapping, .refl mapping, .error _⟩ - · split - · exact ⟨mapping, .refl mapping, .error _⟩ - · have checks := related.checkedValues (stack.take argc).reverse (by - intro value member - exact bounds value (List.mem_of_mem_take (List.mem_reverse.mp member))) - cases checked : (stack.take argc).reverse.mapM (CIL.Safety.checkedValue source) with - | error fault => - rw [checked] at checks - rw [renamed_failure checks] - exact ⟨mapping, .refl mapping, .error fault⟩ - | ok operands => - have operandBounds : ∀ value ∈ operands, value.Below mapping.sourceLimit := by - intro value member - rw [related.sourceLimit] - exact ((checkedValues_valid _ _ _ checked).2 value member).below sourceFormed - rw [checked] at checks - obtain ⟨targetOperands, targetChecked, rfl⟩ := checks.success - obtain ⟨sourceHome, targetHome, sourceMade, targetMade, homes⟩ := - related.allocateHome sourceFormed targetFormed staticBounds sourceFrame.activation - targetFrame.activation size 1 frames.activation size.isLt (by decide) - have freshBound : source.nextIdentity < mapping.allocate.sourceLimit := by - rw [← related.sourceLimit] - exact Nat.lt_succ_self _ - have freshImage : mapping.allocate.image source.nextIdentity = target.nextIdentity := by - rw [← related.sourceLimit, mapping.allocate_fresh, related.targetLimit] - have owned := (frames.extend mapping.allocate_extends (fun _ _ scope => scope)).addOwned - source.nextIdentity freshBound 0 - simp only [Nat.add_zero, freshImage] at owned - have stored := homes.storeLocal (.bytes (.aggregate size) ⟨source.nextIdentity, 0⟩) - (.scalar (.aggregate (List.replicate size 0))) freshBound (by trivial) - simp only [AllocationRenaming.localSlot, AllocationRenaming.value, - AllocationRenaming.reference, freshImage] at stored - refine ⟨mapping.allocate, mapping.allocate_extends, ?_⟩ - simp only [targetChecked, sourceMade, targetMade, Bind.bind, Except.bind] - apply stored.bind - intro sourceResult targetResult results - rcases sourceResult with ⟨sourceSlot, sourceStored⟩ - rcases targetResult with ⟨targetSlot, targetStored⟩ - have memories := results.2 - have operandsSame := mapping.allocate_extends.values operands operandBounds - have restSame := mapping.allocate_extends.values (stack.drop argc) (by - intro value member - exact bounds value (List.mem_of_mem_drop member)) - rw [← operandsSame, ← restSame] - simpa only [List.map_cons, AllocationRenaming.value, - AllocationRenaming.managedReference, AllocationRenaming.reference, freshImage, - Pure.pure, Except.pure] using - ResultRenamed.ok (FrameAction.Renamed.construct (callee := callee) - (arguments := .reference (.address ⟨source.nextIdentity, 0⟩) :: operands) - (rest := stack.drop argc) (temporary := ⟨source.nextIdentity, 0⟩) - (kind := .aggregate size) memories owned freshBound) -#print axioms Memory.Renamed.stepNewAggregate + (step sourceBody (.newobj constructor) pc arguments sourceFrame stack source) + (step targetBody (.newobj constructor) pc (arguments.map mapping.value) targetFrame + (stack.map (indices.value mapping)) target) := by + simp only [step, indices.unspecifiedOperands mapping, metadata.linkAt pc] at observable ⊢ + by_cases guard : unspecifiedOperands (observerCount (.newobj constructor)) stack = true + · simp only [guard, ite_true, SuccessOrThrow] at observable + · simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ + unfold newobjStep at observable ⊢ + generalize sourceBody.linkAt pc = link at observable ⊢ + rcases link with _ | link + · dsimp only at observable ⊢ + rw [metadata.environment] + refine ⟨mapping, .refl mapping, ?_⟩ + cases found : CIL.Extraction.runtimeModel sourceBody.environment .newobj constructor with + | none => exact .error _ + | some model => + rw [found] at observable + exact runtimeModel_stepRenamed found related frames sourceFormed sourceBody targetBody + metadata pc stack bounds observable + cases link <;> try exact ⟨mapping, .refl mapping, .error _⟩ + · exact related.constructAggregate frames sourceFormed targetFormed staticBounds _ _ _ stack + bounds + · exact related.constructObject frames sourceFormed targetFormed staticBounds _ _ _ _ stack + bounds observable + +#print axioms Memory.Renamed.stepNewobj end CIL.Safety diff --git a/CIL/Safety/DecodedSegment.lean b/CIL/Safety/DecodedSegment.lean new file mode 100644 index 000000000..96c7996fb --- /dev/null +++ b/CIL/Safety/DecodedSegment.lean @@ -0,0 +1,121 @@ +import CIL.Safety.DecodedSummary +import CIL.Safety.SegmentContract + +/-! +# Contracts for decoded instruction regions + +A region summary binds a nonempty instruction sequence at a program counter to the decoded +method that contains it. Reuse in another extraction requires a binding for the destination +and agreement on a closed region covering the sequence and its possible continuations. +-/ + +namespace CIL.Safety + +open CIL.Extraction + +/-- A segment contract retains its exits and footprint in a program that agrees on a closed + region containing the entry and every continuation permitted by the contract. -/ +theorem SegmentContract.Holds.congr_region {contract : SegmentContract} {program other : Program} + {region : Nat → Nat → Prop} {method start : Nat} (holds : contract.Holds program method start) + (closed : RegionClosed program region) (agrees : AgreesOn program other region) + (entry : region method start) + (continues : ∀ args before pc after, contract.pre args before → + contract.post args before (.reaches pc after) → region method pc) : + contract.Holds other method start := by + intro args before permitted + obtain ⟨exit, ran, framed, post⟩ := holds args before permitted + refine ⟨exit, ?_, framed, post⟩ + have same := fun fuel => run_congr_region closed agrees fuel method start args before.frame + before.stack before.memory entry + cases exit with + | reaches pc after => + obtain ⟨cost, positive, reached⟩ := ran + exact ⟨cost, positive, reached.congr_region closed agrees entry + (continues args before pc after permitted post)⟩ + | returns memory values => + obtain ⟨fuel, returned⟩ := ran + exact ⟨fuel, (same fuel).trans returned⟩ + | faults fault => + obtain ⟨definite, fuel, faulted⟩ := ran + exact ⟨definite, fuel, (same fuel).trans faulted⟩ + | diverges => + intro fuel + obtain ⟨fault, exhausted, faulted⟩ := ran fuel + exact ⟨fault, exhausted, (same fuel).trans faulted⟩ + +#print axioms SegmentContract.Holds.congr_region + +/-- A checked contract for the instructions starting with `first` at `start` in the named + method. The evidence fixes its defining assembly, type arguments, body bytes and layout. -/ +structure DecodedSegmentSummary (program : Program) (evidence : List MethodEvidence) + (profile : FeatureProfile) (environment : LayoutEnvironment) (method start : Nat) + (pin : AssemblyPin) (token : Nat) (arguments : List TypeExpr) + (first : Instruction) (rest : List Instruction) (contract : SegmentContract) : Prop where + bound : DecodedAt program evidence profile environment method pin token arguments + placed : ∃ body, program[method]? = some body ∧ CodeAt body start (first :: rest) + holds : contract.Holds program method start + +/-- An independently bound destination can reuse the proof when the sequence, the reachable + instructions and their method metadata agree, including any callees in the region. -/ +theorem DecodedSegmentSummary.congr_region {program other : Program} {method start : Nat} + {evidence otherEvidence : List MethodEvidence} {profile otherProfile : FeatureProfile} + {environment otherEnvironment : LayoutEnvironment} {pin otherPin : AssemblyPin} + {token otherToken : Nat} {arguments otherArguments : List TypeExpr} {first : Instruction} + {rest : List Instruction} {contract : SegmentContract} {region : Nat → Nat → Prop} + (summary : DecodedSegmentSummary program evidence profile environment method start + pin token arguments first rest contract) + (target : DecodedAt other otherEvidence otherProfile otherEnvironment method + otherPin otherToken otherArguments) + (closed : RegionClosed program region) (agrees : AgreesOn program other region) + (inside : ∀ i, i < (first :: rest).length → region method (start + i)) + (continues : ∀ args before pc after, contract.pre args before → + contract.post args before (.reaches pc after) → region method pc) : + DecodedSegmentSummary other otherEvidence otherProfile otherEnvironment method start + otherPin otherToken otherArguments first rest contract := by + have entry : region method start := by simpa using inside 0 (by simp) + obtain ⟨body, found, placed⟩ := summary.placed + obtain ⟨destination, present, _, _⟩ := agrees.2 method start body entry found + refine ⟨target, ⟨destination, present, CodeAt.iff_lookup.mpr ?_⟩, + summary.holds.congr_region closed agrees entry continues⟩ + intro i bound + obtain ⟨same, lookup, _, instruction⟩ := agrees.2 method (start + i) body (inside i bound) found + cases present.symm.trans lookup + exact instruction.trans (CodeAt.iff_lookup.mp placed i bound) + +#print axioms DecodedSegmentSummary.congr_region + +/-- Adjacent decoded regions compose when the first reaches the second's precondition and + their permitted writes fit the combined footprint. The final state satisfies both + postconditions through the same intermediate state. -/ +theorem DecodedSegmentSummary.then {program : Program} {evidence : List MethodEvidence} + {profile : FeatureProfile} {environment : LayoutEnvironment} {method start finish : Nat} + {pin : AssemblyPin} {token : Nat} {arguments : List TypeExpr} + {first second : Instruction} {left right : List Instruction} + {firstPre secondPre : List Value → SegmentState → Prop} + {firstPost secondPost : List Value → SegmentState → SegmentState → Prop} + {firstFoot secondFoot footprint : List Value → SegmentState → List (Reference × Nat)} + (head : DecodedSegmentSummary program evidence profile environment method start pin token + arguments first left (.continuing (start + (first :: left).length) firstPre firstPost + firstFoot)) + (tail : DecodedSegmentSummary program evidence profile environment method + (start + (first :: left).length) pin token arguments second right + (.continuing finish secondPre secondPost secondFoot)) + (links : ∀ args before after, firstPre args before → firstPost args before after → + secondPre args after) + (covers : ∀ args before after, firstPre args before → firstPost args before after → + (∀ entry ∈ firstFoot args before, entry ∈ footprint args before) ∧ + (∀ entry ∈ secondFoot args after, entry ∈ footprint args before)) : + DecodedSegmentSummary program evidence profile environment method start pin token arguments + first (left ++ second :: right) (.continuing finish firstPre + (fun args before final => + ∃ after, firstPost args before after ∧ secondPost args after final) + footprint) := by + obtain ⟨body, found, leftPlaced⟩ := head.placed + obtain ⟨other, same, rightPlaced⟩ := tail.placed + cases found.symm.trans same + exact ⟨head.bound, ⟨body, found, leftPlaced.append rightPlaced⟩, + head.holds.then tail.holds links covers⟩ + +#print axioms DecodedSegmentSummary.then + +end CIL.Safety diff --git a/CIL/Safety/DecodedSummary.lean b/CIL/Safety/DecodedSummary.lean new file mode 100644 index 000000000..5301cef2c --- /dev/null +++ b/CIL/Safety/DecodedSummary.lean @@ -0,0 +1,323 @@ +import CIL.Extraction.DecodedMethod +import CIL.Safety.OutcomeContract + +/-! +# Contracts bound to decoded methods + +A `DecodedSummary` is a method contract proved for one index of a program together with the +evidence record that names the method there: the pinned assembly, the metadata token and the +type arguments, and the body bytes, links and certified reachability mask used to decode it. +`decodedProgram?_eq_some` combines each record's body equation in `BodiesDecode` with the +program's mask and call-binding checks to identify the program the summary ranges over. At a call +site, `DecodedSummary.site` identifies the callee's token and type arguments. For a declaring type +with an explicit scope, `DecodedAt.direct_site` also identifies the callee's assembly pin. +-/ + +namespace CIL.Safety +open CIL.Extraction + +/-- Each record's pinned body bytes, links and reachability mask decode to the method at the + same index of `program`, and the two lists have the same length. -/ +inductive BodiesDecode (profile : FeatureProfile) (environment : LayoutEnvironment) : + List MethodEvidence → CIL.Program → Prop + /-- No record and no method. -/ + | nil : BodiesDecode profile environment [] [] + /-- The first record decodes to the first method, and the rest to the rest. -/ + | cons {item : MethodEvidence} {method : CIL.Method} {items : List MethodEvidence} + {methods : CIL.Program} + (decodes : Method.ofBody? profile environment item.links item.reachable item.body + item.original item.arguments = some method) + (rest : BodiesDecode profile environment items methods) : + BodiesDecode profile environment (item :: items) (method :: methods) + +/-- Certified masks, call bindings and per-record body equations identify the decoded program. -/ +theorem decodedProgram?_eq_some {profile : FeatureProfile} {environment : LayoutEnvironment} + {evidence : List MethodEvidence} {program : CIL.Program} + {imports : List ImportSlot} (bindings : programBindingsValid evidence program imports = true) + (bodies : BodiesDecode profile environment evidence program) : + decodedProgram? profile environment evidence imports = some program := by + apply Option.filter_eq_some_iff.mpr + refine ⟨?_, bindings⟩ + clear bindings + induction bodies with + | nil => rfl + | cons decodes _ tail => + simp [List.mapM_cons, decodes, tail] + +#print axioms decodedProgram?_eq_some + +/-- The method at each index of a decoded program is the one that index's record decodes to. -/ +private theorem bodyMap_getElem {profile : FeatureProfile} {environment : LayoutEnvironment} : + ∀ {evidence : List MethodEvidence} {program : CIL.Program} {index : Nat} + {item : MethodEvidence}, + (evidence.mapM fun entry => Method.ofBody? profile environment entry.links entry.reachable + entry.body entry.original entry.arguments) = some program → + evidence[index]? = some item → + ∃ method, program[index]? = some method ∧ + Method.ofBody? profile environment item.links item.reachable item.body item.original + item.arguments = some method + | [], _, _, _, _, found => by simp at found + | head :: items, program, index, item, decoded, found => by + simp only [List.mapM_cons, Option.bind_eq_bind, Option.bind_eq_some_iff, Option.pure_def, + Option.some.injEq] at decoded + obtain ⟨method, decodes, methods, rest, rfl⟩ := decoded + cases index with + | zero => + simp only [List.getElem?_cons_zero, Option.some.injEq] at found + subst found + exact ⟨method, rfl, decodes⟩ + | succ index => + simp only [List.getElem?_cons_succ] at found ⊢ + exact bodyMap_getElem rest found + +#print axioms bodyMap_getElem + +/-- The method at a decoded index is the result of decoding its record's pinned body. -/ +theorem decodedProgram?_getElem {profile : FeatureProfile} {environment : LayoutEnvironment} + {evidence : List MethodEvidence} {program : CIL.Program} {index : Nat} {item : MethodEvidence} + {imports : List ImportSlot} (decoded : decodedProgram? profile environment evidence imports = + some program) + (found : evidence[index]? = some item) : + Method.ofBody? profile environment item.links item.reachable item.body item.original + item.arguments = program[index]? := by + obtain ⟨method, present, decodes⟩ := + bodyMap_getElem (Option.filter_eq_some_iff.mp decoded).1 found + exact decodes.trans present.symm + +#print axioms decodedProgram?_getElem + +/-- The feature certificate at each decoded index validates its record's mask and method. -/ +theorem decodedProgram?_maskValid {profile : FeatureProfile} {environment : LayoutEnvironment} + {evidence : List MethodEvidence} {program : CIL.Program} {index : Nat} + {item : MethodEvidence} {method : Method} + {imports : List ImportSlot} (decoded : decodedProgram? profile environment evidence imports = + some program) + (found : evidence[index]? = some item) (present : program[index]? = some method) : + item.MaskValid evidence method := by + have paired : (item, method) ∈ evidence.zip program := + List.mem_of_getElem? (List.getElem?_zip_eq_some.mpr ⟨found, present⟩) + have bindings : programBindingsValid evidence program imports = true := + (Option.filter_eq_some_iff.mp decoded).2 + unfold programBindingsValid at bindings + have methods := (Bool.and_eq_true_iff.mp bindings).2 + exact (of_decide_eq_true (List.all_eq_true.mp methods (item, method) paired)).1 + +#print axioms decodedProgram?_maskValid + +/-- The body's metadata and bytes certify this record's assembly, raw method and signature. -/ +def _root_.CIL.Extraction.MethodEvidence.BodyBound (item : MethodEvidence) : Prop := + ∃ pins, BodyDecodes pins item.body.platform item.assembly item.body item.original item.arguments + item.signature item.body.imports + +/-- Record `index` of `evidence` is the method with metadata token `token` of assembly `pin` + at type arguments `arguments`, its closed body instantiates that definition, and its pinned + body bytes decode to the method at `index` of `program` under its certified mask. -/ +def DecodedAt (program : CIL.Program) (evidence : List MethodEvidence) + (profile : FeatureProfile) (environment : LayoutEnvironment) (index : Nat) + (pin : AssemblyPin) (token : Nat) (arguments : List TypeExpr) : Prop := + ∃ item, evidence[index]? = some item ∧ item.assembly = pin ∧ item.original.token = token ∧ + item.arguments = arguments ∧ + instantiateMethod environment.rawLayouts item.original arguments = some item.closed ∧ + item.BodyBound ∧ ∃ method, program[index]? = some method ∧ + Method.ofBody? profile environment item.links item.reachable item.body item.original + item.arguments = some method ∧ item.MaskValid evidence method + +/-- An entire decoded program binds each recorded method to its pinned body and valid mask. -/ +theorem DecodedAt.of_decoded {program : CIL.Program} {evidence : List MethodEvidence} + {profile : FeatureProfile} {environment : LayoutEnvironment} {imports : List ImportSlot} + {index : Nat} {item : MethodEvidence} + (decoded : decodedProgram? profile environment evidence imports = some program) + (found : evidence[index]? = some item) (body : item.BodyBound) + (instantiated : + instantiateMethod environment.rawLayouts item.original item.arguments = some item.closed) : + DecodedAt program evidence profile environment index item.assembly item.original.token + item.arguments := by + obtain ⟨method, present, decodes⟩ := + bodyMap_getElem (Option.filter_eq_some_iff.mp decoded).1 found + exact ⟨item, found, rfl, rfl, rfl, instantiated, body, method, present, decodes, + decodedProgram?_maskValid decoded found present⟩ + +#print axioms DecodedAt.of_decoded + +/-- A validated translation binds the method at the selected index to its pinned record. -/ +theorem DecodedAt.of_translation {program : CIL.Program} {evidence : List MethodEvidence} + {rawLayouts layouts pins platform imports} (valid : TranslationValid rawLayouts layouts pins platform evidence + imports program) (index : Fin evidence.length) {method : Method} + (present : program[index.val]? = some method) : + DecodedAt program evidence method.profile method.environment index evidence[index].assembly + evidence[index].original.token evidence[index].arguments := by + have checked := translation_method_checked valid index method present + have mask := checked.maskValid + have body : evidence[index].BodyBound := by + obtain ⟨_, _, _, _, _, _, _, _, _, _, _, _, _, certified, _⟩ := checked + obtain ⟨assemblies, tables, identity, pinned, _, binding⟩ := certified + exact ⟨pins, assemblies, tables, identity, pinned, rfl, binding⟩ + exact ⟨evidence[index], List.getElem?_eq_getElem index.isLt, rfl, rfl, rfl, mask.2.1, body, + method, present, checked.ofBody?, mask⟩ + +#print axioms DecodedAt.of_translation + +/-- `contract` holds for the method at `index`, and `bound` names that method by pinned + assembly, metadata token and type arguments and ties it to the bytes it decodes from. + `MethodContract.Certified` names only an index, which another extraction can give another + body; an `ImportSlot` names a method with no body. A caller holding `bound` knows which + metadata method and which bytes the contract was proved for. -/ +structure DecodedSummary {Input Output : Type} (program : CIL.Program) + (evidence : List MethodEvidence) (profile : FeatureProfile) (environment : LayoutEnvironment) + (index : Nat) (pin : AssemblyPin) (token : Nat) (arguments : List TypeExpr) + (contract : MethodContract Input Output) : Prop where + /-- The contract of the method at `index`. -/ + holds : contract.Holds program index + /-- The identity and bytes of the method at `index`. -/ + bound : DecodedAt program evidence profile environment index pin token arguments + +/-- A contract proved for an index of the decoded program is a summary of the record there. -/ +theorem DecodedSummary.of_decoded {Input Output : Type} {contract : MethodContract Input Output} + {program : CIL.Program} {evidence : List MethodEvidence} {profile : FeatureProfile} + {environment : LayoutEnvironment} {index : Nat} {item : MethodEvidence} + {imports : List ImportSlot} (decoded : decodedProgram? profile environment evidence imports = + some program) + (found : evidence[index]? = some item) (body : item.BodyBound) + (instantiated : + instantiateMethod environment.rawLayouts item.original item.arguments = some item.closed) + (holds : contract.Holds program index) : + DecodedSummary program evidence profile environment index item.assembly item.original.token + item.arguments contract := + ⟨holds, DecodedAt.of_decoded decoded found body instantiated⟩ + +#print axioms DecodedSummary.of_decoded + +/-- A contract proved over a validated translation is a summary of its method's pinned record. -/ +theorem DecodedSummary.of_translation {Input Output : Type} {contract : MethodContract Input Output} + {program : CIL.Program} {evidence : List MethodEvidence} {rawLayouts layouts pins platform imports} + (valid : TranslationValid rawLayouts layouts pins platform evidence imports program) + (index : Fin evidence.length) {method : Method} (present : program[index.val]? = some method) + (holds : contract.Holds program index) : + DecodedSummary program evidence method.profile method.environment index evidence[index].assembly + evidence[index].original.token evidence[index].arguments contract := + ⟨holds, DecodedAt.of_translation valid index present⟩ + +#print axioms DecodedSummary.of_translation + +/-- A method's normal returns and named exceptions, proved for its pinned decoded body. -/ +structure DecodedOutcomeSummary {Input Output : Type} (program : CIL.Program) + (evidence : List MethodEvidence) (profile : FeatureProfile) (environment : LayoutEnvironment) + (index : Nat) (pin : AssemblyPin) (token : Nat) (arguments : List TypeExpr) + (contract : OutcomeContract Input Output) : Prop where + /-- Each permitted input returns with the contract's frame and postcondition or raises + the exception specified for that input and initial memory. -/ + holds : contract.Holds program index + /-- The pinned bytes, metadata and checked reachability mask of the method. -/ + bound : DecodedAt program evidence profile environment index pin token arguments + +/-- The translation certificate binds an outcome proof to its method's decoded record. -/ +theorem DecodedOutcomeSummary.of_translation {Input Output : Type} + {contract : OutcomeContract Input Output} {program : CIL.Program} + {evidence : List MethodEvidence} + {rawLayouts layouts pins platform imports} (valid : TranslationValid rawLayouts layouts pins platform evidence + imports program) (index : Fin evidence.length) {method : Method} + (present : program[index.val]? = some method) (holds : contract.Holds program index) : + DecodedOutcomeSummary program evidence method.profile method.environment index + evidence[index].assembly evidence[index].original.token evidence[index].arguments contract := + ⟨holds, DecodedAt.of_translation valid index present⟩ + +#print axioms DecodedOutcomeSummary.of_translation + +/-- At a live `call` whose link enters `index`, a summary of `index` is for the method the + raw site names: the site's managed token and type arguments are the summary's. -/ +theorem DecodedSummary.site {Input Output : Type} {contract : MethodContract Input Output} + {program : CIL.Program} {evidence : List MethodEvidence} {profile : FeatureProfile} + {environment : LayoutEnvironment} {index token : Nat} {pin : AssemblyPin} + {arguments : List TypeExpr} + (summary : DecodedSummary program evidence profile environment index pin token arguments + contract) + {imports : List ImportSlot} {modelled : CallOpcode × MethodRef → Bool} {raw : RawMethod} + {reachable : List Bool} {caller : CIL.Method} + {pc siteToken : Nat} {method : MethodRef} {prefixes : CallPrefixes} + {instruction : RawInstruction} {site : CallSite} + (calls : + ManagedCallsValid (specializationTable evidence) imports modelled raw reachable caller) + (found : caller.code[pc]? = some (.call method prefixes)) + (live : reachable[pc]? = some true) (rawFound : raw.code[pc]? = some instruction) + (operand : instruction.operand = .call site) (managed : site.target = .managed siteToken) + (linked : caller.linkAt pc = some (.direct index)) : + token = siteToken ∧ arguments = site.typeArguments := by + obtain ⟨item, recorded, -, sameToken, sameArguments, instantiated, -⟩ := summary.bound + obtain ⟨callee, selected, calleeToken, calleeArguments⟩ := + (calls.direct_operand found live rawFound operand managed linked).2 + simp only [specializationTable, List.getElem?_map, recorded, Option.map_some, + Option.some.injEq] at selected + subst selected + subst sameArguments + refine ⟨?_, calleeArguments⟩ + rw [← sameToken, ← instantiateMethod_token instantiated] + exact calleeToken + +#print axioms DecodedSummary.site + +/-- A declaring type with an explicit assembly scope keeps that scope in the closed owner + identity, including when the type is instantiated. -/ +private theorem managedOwner_scoped {owner : TypeExpr} {scope identity : String} + {arguments : List TypeExpr} (hasScope : owner.headScope = some scope) + (named : managedOwner owner = some (identity, arguments)) : ownedBy scope identity = true := by + induction owner generalizing identity arguments with + | apply head argument ih _ => + cases headOwner : managedOwner head with + | none => simp [managedOwner, headOwner] at named + | some pair => + obtain ⟨base, parameters⟩ := pair + simp [managedOwner, headOwner] at named + exact named.1 ▸ ih hasScope headOwner + | aggregate flat _ => + change flatOwnerScope? flat = some scope at hasScope + change some (flat, []) = some (identity, arguments) at named + cases named + exact flatOwnerScope?_prefix hasScope + | _ => + simp_all [TypeExpr.headScope, TypeExpr.explicitHeadScope, managedOwner] + all_goals + rw [← named.1] + simp [ownedBy, metadataChars_eq, List.isPrefixOf_iff_prefix, List.IsPrefix] + +#print axioms managedOwner_scoped + +/-- At a live direct managed call whose declaring type carries an assembly scope, the method + entered by its link has that assembly identity, definition token and type arguments. -/ +theorem DecodedAt.direct_site {program : Program} {evidence : List MethodEvidence} + {profile : FeatureProfile} {environment : LayoutEnvironment} {index token : Nat} + {pin : AssemblyPin} {arguments : List TypeExpr} + (bound : DecodedAt program evidence profile environment index pin token arguments) + (attributed : AttributionValid (bodyOwners evidence) = true) {imports : List ImportSlot} + {modelled : CallOpcode × MethodRef → Bool} {raw : RawMethod} {reachable : List Bool} + {caller : Method} {pc siteToken : Nat} {method : MethodRef} {prefixes : CallPrefixes} + {instruction : RawInstruction} {site : CallSite} {scope : String} + (calls : ManagedCallsValid (specializationTable evidence) imports modelled raw reachable caller) + (found : caller.code[pc]? = some (.call method prefixes)) + (live : reachable[pc]? = some true) (rawFound : raw.code[pc]? = some instruction) + (operand : instruction.operand = .call site) (managed : site.target = .managed siteToken) + (linked : caller.linkAt pc = some (.direct index)) + (hasScope : site.declaringType.headScope = some scope) (validScope : pinIdentityValid scope) : + pin.identity = scope ∧ token = siteToken ∧ arguments = site.typeArguments := by + obtain ⟨item, recorded, rfl, rfl, rfl, instantiated, _⟩ := bound + obtain ⟨_, callee, selected, fits⟩ := + calls.direct_callee found live rawFound operand managed linked + simp only [specializationTable, List.getElem?_map, recorded, Option.map_some, + Option.some.injEq] at selected + subst selected + simp only [calleeMatchesSite, Bool.and_eq_true, beq_iff_eq, and_assoc] at fits + obtain ⟨owner, sameToken, sameArguments, _⟩ := fits + refine ⟨?_, (instantiateMethod_token instantiated).symm.trans sameToken, sameArguments⟩ + have listed : (item.assembly, item.closed.owner) ∈ bodyOwners evidence := by + exact List.mem_flatMap.mpr ⟨item, List.mem_of_getElem? recorded, by simp⟩ + have assigned := Bool.and_eq_true_iff.mp (List.all_eq_true.mp attributed _ listed) + cases named : managedOwner site.declaringType with + | none => simp [managedOwnerMatches, named] at owner + | some pair => + obtain ⟨identity, params⟩ := pair + simp only [managedOwnerMatches, named, Bool.and_eq_true, beq_iff_eq, and_assoc] at owner + exact ownedBy_unique assigned.1 validScope assigned.2 + (owner.1 ▸ managedOwner_scoped hasScope named) + +#print axioms DecodedAt.direct_site + +end CIL.Safety diff --git a/CIL/Safety/Embedding/AppendLocals.lean b/CIL/Safety/Embedding/AppendLocals.lean index f26d21b24..673b5af31 100644 --- a/CIL/Safety/Embedding/AppendLocals.lean +++ b/CIL/Safety/Embedding/AppendLocals.lean @@ -90,14 +90,14 @@ local macro "frame_natural" : tactic => `(tactic| ( | simp only [map_bind])) /-- The shared dispatcher tail carries the frame unchanged through pure and memory operations. -/ -theorem fallback_appendLocals (profile : FeatureProfile) (op : Op) (pc : Nat) +theorem fallback_appendLocals (profile : FeatureProfile) (op : Instruction) (pc : Nat) (stack : List Value) (frame : Frame) (extra : List LocalSlot) (memory : Memory) : (match pureArity op with | some argc => if stack.length < argc then Except.error ExecutionFault.invalidState else do let values ← scalars (stack.take argc) let some (.next target result _) := - CIL.step op false pc [] 0 values (fun _ => none) profile | + CIL.step op false none pc values (fun _ => none) profile | Except.error ExecutionFault.invalidState let result ← (result.map Value.scalar).mapM (checkedValue memory) pure (FrameAction.next target (result ++ stack.drop argc) (frame.appendLocals extra) memory) @@ -110,7 +110,7 @@ theorem fallback_appendLocals (profile : FeatureProfile) (op : Op) (pc : Nat) if stack.length < argc then Except.error ExecutionFault.invalidState else do let values ← scalars (stack.take argc) let some (.next target result _) := - CIL.step op false pc [] 0 values (fun _ => none) profile | + CIL.step op false none pc values (fun _ => none) profile | Except.error ExecutionFault.invalidState let result ← (result.map Value.scalar).mapM (checkedValue memory) pure (FrameAction.next target (result ++ stack.drop argc) frame memory) @@ -132,8 +132,232 @@ theorem observed_appendLocals (observed : Bool) (extra : List LocalSlot) cases observed <;> simp [same, Functor.map, Except.map] #print axioms observed_appendLocals +/-- `padded` is `original` resumed in padded frames, or `original` is no kept outcome. A home + past the original frame's slots reads a padded slot, so only such a home's fault differs. -/ +def PaddedAgrees (extra : List LocalSlot) (padded original : ExecutionResult FrameAction) : + Prop := + padded = FrameAction.appendLocals extra <$> original ∨ ¬ StepKept original + +/-- A read in the padded frame that gives the original result, or the original read failed + without throwing. -/ +def ReadAgrees {α : Type} (padded original : ExecutionResult α) : Prop := + padded = original ∨ ∃ fault, original = .error fault ∧ ∀ exception, fault ≠ .thrown exception + +/-- A kept original outcome is the padded one, with padded frames. -/ +theorem PaddedAgrees.kept {extra : List LocalSlot} {padded original : ExecutionResult FrameAction} + (agrees : PaddedAgrees extra padded original) (kept : StepKept original) : + padded = FrameAction.appendLocals extra <$> original := + agrees.resolve_right fun lost => lost kept + +/-- Equal results with padded frames agree. -/ +theorem PaddedAgrees.of_eq {extra : List LocalSlot} {padded original : ExecutionResult FrameAction} + (same : padded = FrameAction.appendLocals extra <$> original) : + PaddedAgrees extra padded original := + Or.inl same + +#print axioms PaddedAgrees.of_eq + +/-- A step continues alike after a read that agrees, since a failed original read throws + nothing. -/ +theorem PaddedAgrees.bind {α : Type} {extra : List LocalSlot} {padded original : ExecutionResult α} + {next other : α → ExecutionResult FrameAction} (read : ReadAgrees padded original) + (rest : ∀ value, PaddedAgrees extra (next value) (other value)) : + PaddedAgrees extra (padded >>= next) (original >>= other) := by + rcases read with same | ⟨fault, failed, unthrown⟩ + · rw [same] + cases original with + | error fault => exact Or.inl rfl + | ok value => exact rest value + · subst failed + right + intro kept + obtain ⟨exception, thrown⟩ := kept + exact unthrown exception thrown + +/-- A step continues alike after the same computation in both frames. -/ +theorem PaddedAgrees.bind_same {α : Type} {extra : List LocalSlot} {first : ExecutionResult α} + {next other : α → ExecutionResult FrameAction} + (rest : ∀ value, PaddedAgrees extra (next value) (other value)) : + PaddedAgrees extra (first >>= next) (first >>= other) := + PaddedAgrees.bind (Or.inl rfl) rest + +#print axioms PaddedAgrees.kept +#print axioms PaddedAgrees.bind +#print axioms PaddedAgrees.bind_same + +/-- A home read in the padded frame reads the same slot, unless the original frame has none. -/ +theorem spanView_appendLocals (frame : Frame) (extra : List LocalSlot) (memory : Memory) + (value : Value) : + ReadAgrees (spanView (frame.appendLocals extra) memory value) + (spanView frame memory value) := by + cases value with + | spanHome index => + cases found : frame.locals[index]? with + | some slot => + left + simp only [spanView, Frame.appendLocals_getElem? found, found] + | none => + right + refine ⟨.invalidState, by simp only [spanView, found], fun _ thrown => ?_⟩ + cases thrown + | _ => exact Or.inl rfl + +/-- A call argument reads alike in the padded frame, unless the original read fails. -/ +theorem callArgument_appendLocals (frame : Frame) (extra : List LocalSlot) (memory : Memory) + (value : Value) : + ReadAgrees (callArgument (frame.appendLocals extra) memory value) + (callArgument frame memory value) := by + unfold callArgument + rcases spanView_appendLocals frame extra memory value with same | ⟨fault, failed, unthrown⟩ + · exact Or.inl (by rw [same]) + · exact Or.inr ⟨fault, by rw [failed]; rfl, unthrown⟩ + +/-- A list read one value at a time agrees when each read agrees. -/ +theorem ReadAgrees.mapM {α β : Type} {padded original : α → ExecutionResult β} + (each : ∀ value, ReadAgrees (padded value) (original value)) : + ∀ values : List α, ReadAgrees (values.mapM padded) (values.mapM original) + | [] => Or.inl rfl + | value :: rest => by + simp only [List.mapM_cons] + rcases each value with same | ⟨fault, failed, unthrown⟩ + · rw [same] + cases original value with + | error fault => exact Or.inl rfl + | ok head => + rcases ReadAgrees.mapM each rest with tail | ⟨fault, failed, unthrown⟩ + · exact Or.inl (by rw [tail]) + · exact Or.inr ⟨fault, by simp only [failed]; rfl, unthrown⟩ + · exact Or.inr ⟨fault, by simp only [failed]; rfl, unthrown⟩ + +/-- A call's arguments read alike in the padded frame, unless an original read fails. -/ +theorem callArguments_appendLocals (frame : Frame) (extra : List LocalSlot) (memory : Memory) + (values : List Value) : + ReadAgrees (values.mapM (callArgument (frame.appendLocals extra) memory)) + (values.mapM (callArgument frame memory)) := + ReadAgrees.mapM (callArgument_appendLocals frame extra memory) values + +/-- A span operation's operands read alike in the padded frame, unless an original read + fails. -/ +theorem viewOperands_appendLocals (frame : Frame) (extra : List LocalSlot) (memory : Memory) : + ∀ (count : Nat) (stack : List Value), + ReadAgrees (viewOperands (frame.appendLocals extra) memory count stack) + (viewOperands frame memory count stack) + | 0, _ => Or.inl rfl + | _ + 1, [] => Or.inl rfl + | count + 1, value :: rest => by + simp only [viewOperands] + rcases spanView_appendLocals frame extra memory value with same | ⟨fault, failed, unthrown⟩ + · rw [same] + cases spanView frame memory value with + | error fault => exact Or.inl rfl + | ok head => + rcases viewOperands_appendLocals frame extra memory count rest with + tail | ⟨fault, failed, unthrown⟩ + · exact Or.inl (by rw [tail]) + · exact Or.inr ⟨fault, by simp only [failed]; rfl, unthrown⟩ + · exact Or.inr ⟨fault, by simp only [failed]; rfl, unthrown⟩ + +/-- `initobj` through a home writes the same slot in the padded frame, unless the original frame + has no such slot. -/ +theorem initializeSpanHome_appendLocals (pc : Nat) (frame : Frame) (extra : List LocalSlot) + (stack : List Value) (memory : Memory) : + PaddedAgrees extra (initializeSpanHome pc (frame.appendLocals extra) stack memory) + (initializeSpanHome pc frame stack memory) := by + unfold initializeSpanHome + split + · rename_i index rest + cases found : frame.locals[index]? with + | none => + right + simp only [StepKept] + intro ⟨_, thrown⟩ + cases thrown + | some slot => + left + have bound := (List.getElem?_eq_some_iff.mp found).1 + cases slot with + | span width writable view => + simp only [Frame.appendLocals_getElem? found] + cases stored : storeLocal memory (.span width writable view) + (.bufferSpan ⟨.null, 0, width, writable⟩) with + | error fault => + simp only [Bind.bind, Except.bind, Functor.map, Except.map] + | ok result => + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure, Functor.map, + Except.map, FrameAction.appendLocals, Frame.appendLocals, + List.set_append_left _ _ bound] + | _ => simp only [Frame.appendLocals_getElem? found]; rfl + · exact Or.inl rfl + +/-- A span constructor run in place writes the same slot in the padded frame, unless the original + frame has no such slot. -/ +theorem spanInPlaceCall_appendLocals (constructor : CIL.SpanConstructor) (pc : Nat) + (frame : Frame) (extra : List LocalSlot) (stack : List Value) (memory : Memory) : + PaddedAgrees extra (spanInPlaceCall constructor pc (frame.appendLocals extra) stack memory) + (spanInPlaceCall constructor pc frame stack memory) := by + unfold spanInPlaceCall + apply PaddedAgrees.bind_same + intro constructed + obtain ⟨afterMemory, values⟩ := constructed + dsimp only + rcases values with _ | ⟨value, _ | ⟨home, rest⟩⟩ + · exact Or.inl rfl + · exact Or.inl rfl + cases home + case spanHome index => + dsimp only + cases found : frame.locals[index]? with + | none => + right + simp only [StepKept] + intro ⟨_, thrown⟩ + cases thrown + | some slot => + left + have bound := (List.getElem?_eq_some_iff.mp found).1 + cases slot with + | span width writable view => + simp only [Frame.appendLocals_getElem? found] + cases stored : storeLocal afterMemory (.span width writable view) value with + | error fault => + simp only [Bind.bind, Except.bind, Functor.map, Except.map] + split <;> rfl + | ok result => + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure, Functor.map, + Except.map, FrameAction.appendLocals, Frame.appendLocals, + List.set_append_left _ _ bound] + split <;> rfl + | _ => simp only [Frame.appendLocals_getElem? found]; rfl + all_goals exact Or.inl rfl + +#print axioms spanView_appendLocals +#print axioms callArgument_appendLocals +#print axioms ReadAgrees.mapM +#print axioms callArguments_appendLocals +#print axioms viewOperands_appendLocals +#print axioms initializeSpanHome_appendLocals +#print axioms spanInPlaceCall_appendLocals + +set_option hygiene false in +/-- Closes a padded step whose reads may see padded slots: each home read agrees or fails without + throwing, and every other computation is the same in both frames. -/ +local macro "padded_natural" : tactic => `(tactic| ( + repeat' first + | contradiction + | (apply PaddedAgrees.of_eq; rfl) + | dsimp only + | (apply PaddedAgrees.of_eq + simp only [map_bind, map_pure, FrameAction.appendLocals, Frame.appendLocals] + done) + | exact initializeSpanHome_appendLocals _ _ _ _ _ + | exact spanInPlaceCall_appendLocals _ _ _ _ _ _ + | (apply PaddedAgrees.bind (callArguments_appendLocals _ _ _ _); intro) + | (apply PaddedAgrees.bind (viewOperands_appendLocals _ _ _ _ _); intro) + | (apply PaddedAgrees.bind_same; intro) + | split)) + /-- An instruction preserves successful and throwing steps under trailing local slots. -/ -def StepAppendLocals (op : Op) : Prop := +def StepAppendLocals (op : Instruction) : Prop := ∀ (body : Method) (pc : Nat) (args : List Value) (frame : Frame) (extra : List LocalSlot) (stack : List Value) (memory : Memory) (outcome : ExecutionResult FrameAction), @@ -150,92 +374,85 @@ local macro "append_stack_cases" " using " fallback:term : tactic => `(tactic| ( | (rcases rest with _ | ⟨second, rest⟩ <;> (try cases second) all_goals first | exact $fallback | frame_natural))) -/-- `nativeBinary` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_nativeBinary (operation : Op) : - StepAppendLocals (.nativeBinary operation) := by +set_option hygiene false in +/-- Proves `StepAppendLocals op` for one faulting arithmetic constructor `op`; the fallback + names `op`, since inferring it from the dispatcher does not terminate in budget. -/ +local macro "faulting_appendLocals " op:term : tactic => `(tactic| ( intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.nativeBinary operation) pc _ frame extra memory - -#print axioms step_appendLocals_nativeBinary + fallback_appendLocals body.profile $op pc _ frame extra memory)) -/-- `divide` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_divide (signed remainder : Bool) : - StepAppendLocals (.divide signed remainder) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - append_stack_cases using - fallback_appendLocals body.profile (.divide signed remainder) pc _ frame extra memory +/-- Division, remainder and overflow-checked arithmetic preserve outcomes under trailing local + slots. -/ +theorem step_appendLocals_faulting (op : Instruction) + (faulting : op.divideKind?.isSome || op.checkedKind?.isSome) : StepAppendLocals op := by + cases op <;> simp only [Instruction.divideKind?, Instruction.checkedKind?, Option.isSome_none, + Option.isSome_some, Bool.or_false, Bool.false_or, Bool.false_eq_true] at faulting + case div => faulting_appendLocals .div + case div_un => faulting_appendLocals .div_un + case rem => faulting_appendLocals .rem + case rem_un => faulting_appendLocals .rem_un + case add_ovf => faulting_appendLocals .add_ovf + case add_ovf_un => faulting_appendLocals .add_ovf_un + case sub_ovf => faulting_appendLocals .sub_ovf + case sub_ovf_un => faulting_appendLocals .sub_ovf_un + case mul_ovf => faulting_appendLocals .mul_ovf + case mul_ovf_un => faulting_appendLocals .mul_ovf_un -#print axioms step_appendLocals_divide +#print axioms step_appendLocals_faulting -/-- `checkedArithmetic` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_checkedArithmetic (kind : CheckedBinaryKind) (signed : Bool) : - StepAppendLocals (.checkedArithmetic kind signed) := by +/-- `conv_ovf` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_ovf (target : ConvTarget) : + StepAppendLocals (.conv_ovf target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.checkedArithmetic kind signed) - pc _ frame extra memory + fallback_appendLocals body.profile (.conv_ovf target) pc _ frame extra memory -#print axioms step_appendLocals_checkedArithmetic +#print axioms step_appendLocals_conv_ovf -/-- `checkedConvert` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_checkedConvert (width : Nat) (signed unsignedInput : Bool) : - StepAppendLocals (.checkedConvert width signed unsignedInput) := by +/-- `conv_ovf_un` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_ovf_un (target : ConvTarget) : + StepAppendLocals (.conv_ovf_un target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.checkedConvert width signed unsignedInput) - pc _ frame extra memory + fallback_appendLocals body.profile (.conv_ovf_un target) pc _ frame extra memory -#print axioms step_appendLocals_checkedConvert +#print axioms step_appendLocals_conv_ovf_un -/-- `stackAlloc` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_stackAlloc (initLocals : Bool) : - StepAppendLocals (.stackAlloc initLocals) := by +/-- `localloc` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_localloc : StepAppendLocals .localloc := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept - simp only [step, allocateStack_appendLocals, bind_map_left] + simp only [step, localAllocation, allocateStack_appendLocals, bind_map_left] frame_natural -#print axioms step_appendLocals_stackAlloc +#print axioms step_appendLocals_localloc /-- Heap array allocation does not inspect or change a frame's local slots. -/ -theorem step_appendLocals_newArray (element : CIL.ArrayElement) : - StepAppendLocals (.newArray element) := by +theorem step_appendLocals_newarr (element : CIL.Extraction.TypeExpr) : + StepAppendLocals (.newarr element) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept - simp only [step, bind_map_left] - frame_natural - -#print axioms step_appendLocals_newArray - -/-- Reference array allocation does not inspect or change a frame's local slots. -/ -theorem step_appendLocals_newReferenceArray (element : CIL.Extraction.TypeExpr) : - StepAppendLocals (.newReferenceArray element) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - simp only [step, bind_map_left] - frame_natural + unfold step + apply observed_appendLocals + append_stack_cases using + fallback_appendLocals body.profile (.newarr element) pc _ frame extra memory -#print axioms step_appendLocals_newReferenceArray +#print axioms step_appendLocals_newarr /-- `nop` preserves outcomes under trailing local slots. -/ theorem step_appendLocals_nop : @@ -261,17 +478,17 @@ theorem step_appendLocals_neg : #print axioms step_appendLocals_neg -/-- `gt` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_gt : - StepAppendLocals .gt := by +/-- `cgt` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_cgt : + StepAppendLocals .cgt := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .gt pc stack frame extra memory + exact fallback_appendLocals body.profile .cgt pc stack frame extra memory -#print axioms step_appendLocals_gt +#print axioms step_appendLocals_cgt /-- `bgt` preserves outcomes under trailing local slots. -/ theorem step_appendLocals_bgt (target : Nat) : @@ -297,224 +514,116 @@ theorem step_appendLocals_ble (target : Nat) : #print axioms step_appendLocals_ble -/-- `bleu` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_bleu (target : Nat) : - StepAppendLocals (.bleu target) := by +/-- `ble_un` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ble_un (target : Nat) : + StepAppendLocals (.ble_un target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.bleu target) pc _ frame extra memory + fallback_appendLocals body.profile (.ble_un target) pc _ frame extra memory -#print axioms step_appendLocals_bleu +#print axioms step_appendLocals_ble_un -/-- `loadAggregate` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_loadAggregate (size : Nat) : - StepAppendLocals (.loadAggregate size) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.loadAggregate size) pc stack frame extra memory -#print axioms step_appendLocals_loadAggregate -/-- `copyObject` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_copyObject (size : Nat) : - StepAppendLocals (.copyObject size) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.copyObject size) pc stack frame extra memory -#print axioms step_appendLocals_copyObject -/-- `storeAggregate` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_storeAggregate (size : Nat) : - StepAppendLocals (.storeAggregate size) := by +/-- `ldarg` preserves outcomes under trailing local slots: a homed argument's slot precedes + them, and an argument value does not involve the frame. -/ +theorem step_appendLocals_ldarg (index : Nat) : StepAppendLocals (.ldarg index) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.storeAggregate size) pc stack frame extra memory - -#print axioms step_appendLocals_storeAggregate - -/-- `initAggregate` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_initAggregate (size : Nat) : - StepAppendLocals (.initAggregate size) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.initAggregate size) pc stack frame extra memory - -#print axioms step_appendLocals_initAggregate - -/-- `loadScalar` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_loadScalar (width : Nat) (signed : Bool) : - StepAppendLocals (.loadScalar width signed) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.loadScalar width signed) pc stack frame extra memory - -#print axioms step_appendLocals_loadScalar - -/-- `storeScalar` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_storeScalar (width : Nat) : - StepAppendLocals (.storeScalar width) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.storeScalar width) pc stack frame extra memory - -#print axioms step_appendLocals_storeScalar - -/-- `fieldAt` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_fieldAt (offset width : Nat) (signed : Bool) : - StepAppendLocals (.fieldAt offset width signed) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.fieldAt offset width signed) - pc stack frame extra memory - -#print axioms step_appendLocals_fieldAt - -/-- `fieldAddress` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_fieldAddress (offset : Nat) : - StepAppendLocals (.fieldAddress offset) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.fieldAddress offset) pc stack frame extra memory - -#print axioms step_appendLocals_fieldAddress - -/-- `setFieldAt` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_setFieldAt (offset width : Nat) : - StepAppendLocals (.setFieldAt offset width) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.setFieldAt offset width) pc stack frame extra memory - -#print axioms step_appendLocals_setFieldAt - -/-- `aggregateFieldAt` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_aggregateFieldAt (offset size : Nat) : - StepAppendLocals (.aggregateFieldAt offset size) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.aggregateFieldAt offset size) - pc stack frame extra memory - -#print axioms step_appendLocals_aggregateFieldAt - -/-- `setAggregateFieldAt` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_setAggregateFieldAt (offset size : Nat) : - StepAppendLocals (.setAggregateFieldAt offset size) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.setAggregateFieldAt offset size) - pc stack frame extra memory - -#print axioms step_appendLocals_setAggregateFieldAt - -/-- `arg` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_arg (index : Nat) (narrow : Option NarrowKind) : - StepAppendLocals (.arg index narrow) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - frame_natural - -#print axioms step_appendLocals_arg - -/-- `vectorArg` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_vectorArg (index : Nat) : - StepAppendLocals (.vectorArg index) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - frame_natural - -#print axioms step_appendLocals_vectorArg - -/-- `vectorArgAddr` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_vectorArgAddr (index : Nat) : - StepAppendLocals (.vectorArgAddr index) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - frame_natural + have loaded : loadArgument body index args (frame.appendLocals extra) memory = + loadArgument body index args frame memory ∨ + loadArgument body index args frame memory = .error .invalidState := by + unfold loadArgument + split + · rename_i slot _ + cases found : frame.locals[slot]? with + | none => exact Or.inr rfl + | some home => exact Or.inl (by simp only [Frame.appendLocals_getElem? found]) + all_goals exact Or.inl rfl + have unfolded : ∀ current : Frame, step body (.ldarg index) pc args current stack memory = + (do + let value ← loadArgument body index args current memory + return FrameAction.next (pc + 1) (value :: stack) current memory) := fun _ => rfl + revert kept + rw [unfolded, unfolded] + rcases loaded with same | missing + · rw [same] + intro + frame_natural + · rw [missing] + simp [StepKept, Functor.map, Except.map] -#print axioms step_appendLocals_vectorArgAddr +#print axioms step_appendLocals_ldarg -/-- `local` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_local (index : Nat) (narrow : Option NarrowKind) : - StepAppendLocals (.local index narrow) := by +/-- `starg` preserves outcomes under trailing local slots: a homed argument's slot precedes + them, and an argument without a home faults alike. -/ +theorem step_appendLocals_starg (index : Nat) : StepAppendLocals (.starg index) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped - cases found : frame.locals[index]? with - | some slot => - cases stack <;> - simp only [step, found, Frame.appendLocals_getElem? found] <;> - frame_natural - | none => + cases stack with + | nil => revert kept - cases stack <;> simp only [step, found] <;> repeat' split + simp only [step] + repeat' split all_goals first | (simp [StepKept]; done) | (intro; frame_natural; done) - -#print axioms step_appendLocals_local - -/-- `vectorLocal` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_vectorLocal (index : Nat) : - StepAppendLocals (.vectorLocal index) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - cases found : frame.locals[index]? with - | some slot => - cases stack <;> - simp only [step, found, Frame.appendLocals_getElem? found] <;> - frame_natural - | none => + | cons value rest => + have unfolded : ∀ current : Frame, + step body (.starg index) pc args current (value :: rest) memory = (do + let (current, memory) ← storeArgument body index value current memory + return FrameAction.next (pc + 1) rest current memory) := fun _ => rfl revert kept - cases stack <;> simp only [step, found] <;> repeat' split - all_goals first | (simp [StepKept]; done) | (intro; frame_natural; done) + rw [unfolded, unfolded] + unfold storeArgument + split + · rename_i slot _ + cases found : frame.locals[slot]? with + | some home => + simp only [Frame.appendLocals_getElem? found, Frame.appendLocals_set found] + intro + cases storeLocal memory home value <;> rfl + | none => simp [StepKept, Bind.bind, Except.bind] + · simp [StepKept, Bind.bind, Except.bind] + +#print axioms step_appendLocals_starg + +/-- `ldarga` preserves outcomes under trailing local slots: a homed argument's slot precedes + them, and an argument without a home faults alike. -/ +theorem step_appendLocals_ldarga (index : Nat) : StepAppendLocals (.ldarga index) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + have addressed : addressArgument body index (frame.appendLocals extra) memory = + addressArgument body index frame memory ∨ + addressArgument body index frame memory = .error .invalidState := by + unfold addressArgument + split + · rename_i slot _ + cases found : frame.locals[slot]? with + | none => exact Or.inr rfl + | some home => exact Or.inl (by simp only [Frame.appendLocals_getElem? found]) + · exact Or.inl rfl + have unfolded : ∀ current : Frame, step body (.ldarga index) pc args current stack memory = + (do + let value ← addressArgument body index current memory + return FrameAction.next (pc + 1) (value :: stack) current memory) := fun _ => rfl + revert kept + rw [unfolded, unfolded] + rcases addressed with same | missing + · rw [same] + intro + frame_natural + · rw [missing] + simp [StepKept, Functor.map, Except.map] -#print axioms step_appendLocals_vectorLocal +#print axioms step_appendLocals_ldarga -/-- `vectorLocalAddr` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_vectorLocalAddr (index : Nat) : - StepAppendLocals (.vectorLocalAddr index) := by +/-- `ldloc` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldloc (index : Nat) : StepAppendLocals (.ldloc index) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped cases found : frame.locals[index]? with @@ -527,29 +636,11 @@ theorem step_appendLocals_vectorLocalAddr (index : Nat) : cases stack <;> simp only [step, found] <;> repeat' split all_goals first | (simp [StepKept]; done) | (intro; frame_natural; done) -#print axioms step_appendLocals_vectorLocalAddr +#print axioms step_appendLocals_ldloc -/-- `setVectorLocal` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_setVectorLocal (index : Nat) : - StepAppendLocals (.setVectorLocal index) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - cases found : frame.locals[index]? with - | some slot => - cases stack <;> - simp only [step, found, Frame.appendLocals_getElem? found, - Frame.appendLocals_set found] <;> - frame_natural - | none => - revert kept - cases stack <;> simp only [step, found] <;> repeat' split - all_goals first | (simp [StepKept]; done) | (intro; frame_natural; done) - -#print axioms step_appendLocals_setVectorLocal - -/-- `localAddr` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_localAddr (index : Nat) : - StepAppendLocals (.localAddr index) := by +/-- `ldloca` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldloca (index : Nat) : + StepAppendLocals (.ldloca index) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped cases found : frame.locals[index]? with @@ -562,11 +653,10 @@ theorem step_appendLocals_localAddr (index : Nat) : cases stack <;> simp only [step, found] <;> repeat' split all_goals first | (simp [StepKept]; done) | (intro; frame_natural; done) -#print axioms step_appendLocals_localAddr +#print axioms step_appendLocals_ldloca -/-- `setLocal` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_setLocal (index : Nat) (narrow : Option NarrowKind) : - StepAppendLocals (.setLocal index narrow) := by +/-- `stloc` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_stloc (index : Nat) : StepAppendLocals (.stloc index) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped cases found : frame.locals[index]? with @@ -580,204 +670,175 @@ theorem step_appendLocals_setLocal (index : Nat) (narrow : Option NarrowKind) : cases stack <;> simp only [step, found] <;> repeat' split all_goals first | (simp [StepKept]; done) | (intro; frame_natural; done) -#print axioms step_appendLocals_setLocal - -/-- Span construction updates only its existing typed local slot. -/ -theorem step_appendLocals_initializeSpanLocal (index : Nat) (constructor : CIL.SpanConstructor) : - StepAppendLocals (.initializeSpanLocal index constructor) := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - cases found : frame.locals[index]? with - | some slot => - simp only [step, found, Frame.appendLocals_getElem? found, Frame.appendLocals_set found] - frame_natural - | none => - revert kept - simp only [step, found] - repeat' split - all_goals first | (simp [StepKept]; done) | (intro; frame_natural; done) - -#print axioms step_appendLocals_initializeSpanLocal +#print axioms step_appendLocals_stloc -/-- `lane` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_lane (index : Fin 4) : - StepAppendLocals (.lane index) := by +/-- `ldc_i4` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldc_i4 (value : W32) : + StepAppendLocals (.ldc_i4 value) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile (.lane index) pc stack frame extra memory + exact fallback_appendLocals body.profile (.ldc_i4 value) pc stack frame extra memory -#print axioms step_appendLocals_lane +#print axioms step_appendLocals_ldc_i4 -/-- `laneAddress` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_laneAddress (index : Fin 4) : - StepAppendLocals (.laneAddress index) := by +/-- `ldc_i8` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldc_i8 (value : W64) : + StepAppendLocals (.ldc_i8 value) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile (.laneAddress index) pc stack frame extra memory + exact fallback_appendLocals body.profile (.ldc_i8 value) pc stack frame extra memory -#print axioms step_appendLocals_laneAddress +#print axioms step_appendLocals_ldc_i8 -/-- `setLane` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_setLane (index : Fin 4) : - StepAppendLocals (.setLane index) := by +/-- `ldnull` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldnull : + StepAppendLocals .ldnull := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile (.setLane index) pc stack frame extra memory + exact fallback_appendLocals body.profile .ldnull pc stack frame extra memory -#print axioms step_appendLocals_setLane +#print axioms step_appendLocals_ldnull -/-- `const32` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_const32 (word : W32) : - StepAppendLocals (.const32 word) := by +/-- `conv_i1` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_i1 : + StepAppendLocals .conv_i1 := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile (.const32 word) pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_i1 pc stack frame extra memory -#print axioms step_appendLocals_const32 +#print axioms step_appendLocals_conv_i1 -/-- `const64` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_const64 (word : W64) : - StepAppendLocals (.const64 word) := by +/-- `conv_i2` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_i2 : + StepAppendLocals .conv_i2 := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile (.const64 word) pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_i2 pc stack frame extra memory -#print axioms step_appendLocals_const64 +#print axioms step_appendLocals_conv_i2 -/-- `convI1` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_convI1 : - StepAppendLocals .convI1 := by +/-- `conv_u2` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_u2 : + StepAppendLocals .conv_u2 := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .convI1 pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_u2 pc stack frame extra memory -#print axioms step_appendLocals_convI1 +#print axioms step_appendLocals_conv_u2 -/-- `convI2` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_convI2 : - StepAppendLocals .convI2 := by +/-- `conv_i8` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_i8 : + StepAppendLocals .conv_i8 := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .convI2 pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_i8 pc stack frame extra memory -#print axioms step_appendLocals_convI2 +#print axioms step_appendLocals_conv_i8 -/-- `convU2` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_convU2 : - StepAppendLocals .convU2 := by +/-- `conv_i4` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_i4 : + StepAppendLocals .conv_i4 := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .convU2 pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_i4 pc stack frame extra memory -#print axioms step_appendLocals_convU2 +#print axioms step_appendLocals_conv_i4 -/-- `convI8` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_convI8 : - StepAppendLocals .convI8 := by +/-- `conv_u1` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_u1 : + StepAppendLocals .conv_u1 := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .convI8 pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_u1 pc stack frame extra memory -#print axioms step_appendLocals_convI8 +#print axioms step_appendLocals_conv_u1 -/-- `convI4` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_convI4 : - StepAppendLocals .convI4 := by +/-- `conv_u4` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_u4 : + StepAppendLocals .conv_u4 := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .convI4 pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_u4 pc stack frame extra memory -#print axioms step_appendLocals_convI4 +#print axioms step_appendLocals_conv_u4 -/-- `convU1` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_convU1 : - StepAppendLocals .convU1 := by +/-- `conv_u8` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_u8 : + StepAppendLocals .conv_u8 := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .convU1 pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_u8 pc stack frame extra memory -#print axioms step_appendLocals_convU1 +#print axioms step_appendLocals_conv_u8 -/-- `convU4` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_convU4 : - StepAppendLocals .convU4 := by +/-- `conv_u` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_u : + StepAppendLocals .conv_u := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .convU4 pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_u pc stack frame extra memory -#print axioms step_appendLocals_convU4 +#print axioms step_appendLocals_conv_u -/-- `convU8` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_convU8 : - StepAppendLocals .convU8 := by +/-- `conv_i` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_conv_i : + StepAppendLocals .conv_i := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .convU8 pc stack frame extra memory + exact fallback_appendLocals body.profile .conv_i pc stack frame extra memory -#print axioms step_appendLocals_convU8 +#print axioms step_appendLocals_conv_i -/-- `convU` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_convU : - StepAppendLocals .convU := by +/-- `not` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_not : + StepAppendLocals .not := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .convU pc stack frame extra memory + exact fallback_appendLocals body.profile .not pc stack frame extra memory -#print axioms step_appendLocals_convU - -/-- `bnot` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_bnot : - StepAppendLocals .bnot := by - intro body pc args frame extra stack memory outcome stepped kept - subst stepped - clear kept - unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile .bnot pc stack frame extra memory - -#print axioms step_appendLocals_bnot +#print axioms step_appendLocals_not /-- `add` preserves outcomes under trailing local slots. -/ theorem step_appendLocals_add : @@ -787,7 +848,8 @@ theorem step_appendLocals_add : clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .add pc stack frame extra memory + append_stack_cases using + fallback_appendLocals body.profile .add pc _ frame extra memory #print axioms step_appendLocals_add @@ -797,7 +859,7 @@ theorem step_appendLocals_sub : intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept - unfold step + unfold step referenceCompareCall referenceSubtractCall apply observed_appendLocals append_stack_cases using fallback_appendLocals body.profile .sub pc _ frame extra memory @@ -816,41 +878,41 @@ theorem step_appendLocals_mul : #print axioms step_appendLocals_mul -/-- `band` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_band : - StepAppendLocals .band := by +/-- `and` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_and : + StepAppendLocals .and := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .band pc stack frame extra memory + exact fallback_appendLocals body.profile .and pc stack frame extra memory -#print axioms step_appendLocals_band +#print axioms step_appendLocals_and -/-- `bor` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_bor : - StepAppendLocals .bor := by +/-- `or` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_or : + StepAppendLocals .or := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .bor pc stack frame extra memory + exact fallback_appendLocals body.profile .or pc stack frame extra memory -#print axioms step_appendLocals_bor +#print axioms step_appendLocals_or -/-- `bxor` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_bxor : - StepAppendLocals .bxor := by +/-- `xor` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_xor : + StepAppendLocals .xor := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .bxor pc stack frame extra memory + exact fallback_appendLocals body.profile .xor pc stack frame extra memory -#print axioms step_appendLocals_bxor +#print axioms step_appendLocals_xor /-- `shl` preserves outcomes under trailing local slots. -/ theorem step_appendLocals_shl : @@ -876,92 +938,152 @@ theorem step_appendLocals_shr : #print axioms step_appendLocals_shr -/-- `shrUn` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_shrUn : - StepAppendLocals .shrUn := by +/-- `shr_un` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_shr_un : + StepAppendLocals .shr_un := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .shrUn pc stack frame extra memory + exact fallback_appendLocals body.profile .shr_un pc stack frame extra memory -#print axioms step_appendLocals_shrUn +#print axioms step_appendLocals_shr_un -/-- `lt` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_lt : - StepAppendLocals .lt := by +/-- `clt` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_clt : + StepAppendLocals .clt := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .lt pc stack frame extra memory + exact fallback_appendLocals body.profile .clt pc stack frame extra memory -#print axioms step_appendLocals_lt +#print axioms step_appendLocals_clt -/-- `ltu` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_ltu : - StepAppendLocals .ltu := by +/-- `clt_un` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_clt_un : + StepAppendLocals .clt_un := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept - unfold step + unfold step referenceCompareCall referenceSubtractCall apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile .ltu pc _ frame extra memory + fallback_appendLocals body.profile .clt_un pc _ frame extra memory -#print axioms step_appendLocals_ltu +#print axioms step_appendLocals_clt_un -/-- `gtu` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_gtu : - StepAppendLocals .gtu := by +/-- `cgt_un` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_cgt_un : + StepAppendLocals .cgt_un := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept - unfold step + unfold step referenceCompareCall referenceSubtractCall apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile .gtu pc _ frame extra memory + fallback_appendLocals body.profile .cgt_un pc _ frame extra memory -#print axioms step_appendLocals_gtu +#print axioms step_appendLocals_cgt_un -/-- `eq` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_eq : - StepAppendLocals .eq := by +/-- `ceq` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ceq : + StepAppendLocals .ceq := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept - unfold step + unfold step referenceCompareCall referenceSubtractCall apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile .eq pc _ frame extra memory + fallback_appendLocals body.profile .ceq pc _ frame extra memory + +#print axioms step_appendLocals_ceq + +/-- `ldind` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldind (kind : CIL.NumericKind) : + StepAppendLocals (.ldind kind) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + clear kept + unfold step + apply observed_appendLocals + exact fallback_appendLocals body.profile (.ldind kind) pc stack frame extra memory + +#print axioms step_appendLocals_ldind + +/-- `stind` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_stind (kind : CIL.NumericKind) : + StepAppendLocals (.stind kind) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + clear kept + unfold step + apply observed_appendLocals + exact fallback_appendLocals body.profile (.stind kind) pc stack frame extra memory + +#print axioms step_appendLocals_stind + +/-- `ldlen` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldlen : + StepAppendLocals .ldlen := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + clear kept + unfold step + apply observed_appendLocals + exact fallback_appendLocals body.profile .ldlen pc stack frame extra memory + +#print axioms step_appendLocals_ldlen + +/-- `ldelem` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldelem (kind : CIL.NumericKind) : + StepAppendLocals (.ldelem kind) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + clear kept + unfold step + apply observed_appendLocals + exact fallback_appendLocals body.profile (.ldelem kind) pc stack frame extra memory + +#print axioms step_appendLocals_ldelem + +/-- `ldelem.ref` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldelem_ref : + StepAppendLocals .ldelem_ref := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + clear kept + unfold step + apply observed_appendLocals + exact fallback_appendLocals body.profile .ldelem_ref pc stack frame extra memory -#print axioms step_appendLocals_eq +#print axioms step_appendLocals_ldelem_ref -/-- `load64` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_load64 : - StepAppendLocals .load64 := by +/-- `stelem` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_stelem (kind : CIL.NumericKind) : + StepAppendLocals (.stelem kind) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .load64 pc stack frame extra memory + exact fallback_appendLocals body.profile (.stelem kind) pc stack frame extra memory -#print axioms step_appendLocals_load64 +#print axioms step_appendLocals_stelem -/-- `store64` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_store64 : - StepAppendLocals .store64 := by +/-- `stelem.ref` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_stelem_ref : + StepAppendLocals .stelem_ref := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile .store64 pc stack frame extra memory + exact fallback_appendLocals body.profile .stelem_ref pc stack frame extra memory -#print axioms step_appendLocals_store64 +#print axioms step_appendLocals_stelem_ref /-- `dup` preserves outcomes under trailing local slots. -/ theorem step_appendLocals_dup : @@ -987,17 +1109,17 @@ theorem step_appendLocals_pop : #print axioms step_appendLocals_pop -/-- `branch` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_branch (target : Nat) : - StepAppendLocals (.branch target) := by +/-- `br` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_br (target : Nat) : + StepAppendLocals (.br target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals - exact fallback_appendLocals body.profile (.branch target) pc stack frame extra memory + exact fallback_appendLocals body.profile (.br target) pc stack frame extra memory -#print axioms step_appendLocals_branch +#print axioms step_appendLocals_br /-- `switch` preserves outcomes under trailing local slots. -/ theorem step_appendLocals_switch (targets : List Nat) : @@ -1011,83 +1133,129 @@ theorem step_appendLocals_switch (targets : List Nat) : #print axioms step_appendLocals_switch -/-- `brzero` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_brzero (target : Nat) : - StepAppendLocals (.brzero target) := by +/-- `brfalse` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_brfalse (target : Nat) : + StepAppendLocals (.brfalse target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.brzero target) pc _ frame extra memory + fallback_appendLocals body.profile (.brfalse target) pc _ frame extra memory -#print axioms step_appendLocals_brzero +#print axioms step_appendLocals_brfalse -/-- `brnonzero` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_brnonzero (target : Nat) : - StepAppendLocals (.brnonzero target) := by +/-- `brtrue` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_brtrue (target : Nat) : + StepAppendLocals (.brtrue target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.brnonzero target) pc _ frame extra memory + fallback_appendLocals body.profile (.brtrue target) pc _ frame extra memory -#print axioms step_appendLocals_brnonzero +#print axioms step_appendLocals_brtrue -/-- `objectTest` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_objectTest (type : CIL.Extraction.TypeExpr) (throwing : Bool) : - StepAppendLocals (.objectTest type throwing) := by +/-- `castclass` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_castclass (type : CIL.Extraction.TypeExpr) : + StepAppendLocals (.castclass type) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.objectTest type throwing) pc _ frame extra memory + fallback_appendLocals body.profile (.castclass type) pc _ frame extra memory + +#print axioms step_appendLocals_castclass + +/-- `isinst` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_isinst (type : CIL.Extraction.TypeExpr) : + StepAppendLocals (.isinst type) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + clear kept + unfold step + apply observed_appendLocals + append_stack_cases using + fallback_appendLocals body.profile (.isinst type) pc _ frame extra memory + +#print axioms step_appendLocals_isinst + +/-- Instance field loads do not inspect or change a frame's local slots. -/ +theorem step_appendLocals_ldfld (field : CIL.Extraction.FieldRef) : + StepAppendLocals (.ldfld field) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + clear kept + simp only [step] + frame_natural -#print axioms step_appendLocals_objectTest +#print axioms step_appendLocals_ldfld -/-- `bltu` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_bltu (target : Nat) : - StepAppendLocals (.bltu target) := by +/-- Instance field stores do not inspect or change a frame's local slots. -/ +theorem step_appendLocals_stfld (field : CIL.Extraction.FieldRef) : + StepAppendLocals (.stfld field) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + clear kept + simp only [step] + frame_natural + +#print axioms step_appendLocals_stfld + +/-- Instance field addresses do not inspect or change a frame's local slots. -/ +theorem step_appendLocals_ldflda (field : CIL.Extraction.FieldRef) : + StepAppendLocals (.ldflda field) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + clear kept + simp only [step] + frame_natural + +#print axioms step_appendLocals_ldflda + +/-- `blt_un` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_blt_un (target : Nat) : + StepAppendLocals (.blt_un target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.bltu target) pc _ frame extra memory + fallback_appendLocals body.profile (.blt_un target) pc _ frame extra memory -#print axioms step_appendLocals_bltu +#print axioms step_appendLocals_blt_un -/-- `bgeu` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_bgeu (target : Nat) : - StepAppendLocals (.bgeu target) := by +/-- `bge_un` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_bge_un (target : Nat) : + StepAppendLocals (.bge_un target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.bgeu target) pc _ frame extra memory + fallback_appendLocals body.profile (.bge_un target) pc _ frame extra memory -#print axioms step_appendLocals_bgeu +#print axioms step_appendLocals_bge_un -/-- `bgtu` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_bgtu (target : Nat) : - StepAppendLocals (.bgtu target) := by +/-- `bgt_un` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_bgt_un (target : Nat) : + StepAppendLocals (.bgt_un target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.bgtu target) pc _ frame extra memory + fallback_appendLocals body.profile (.bgt_un target) pc _ frame extra memory -#print axioms step_appendLocals_bgtu +#print axioms step_appendLocals_bgt_un /-- `bge` preserves outcomes under trailing local slots. -/ theorem step_appendLocals_bge (target : Nat) : @@ -1126,168 +1294,201 @@ theorem step_appendLocals_beq (target : Nat) : #print axioms step_appendLocals_beq -/-- `bne` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_bne (target : Nat) : - StepAppendLocals (.bne target) := by +/-- `bne_un` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_bne_un (target : Nat) : + StepAppendLocals (.bne_un target) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step apply observed_appendLocals append_stack_cases using - fallback_appendLocals body.profile (.bne target) pc _ frame extra memory + fallback_appendLocals body.profile (.bne_un target) pc _ frame extra memory -#print axioms step_appendLocals_bne +#print axioms step_appendLocals_bne_un /-- `call` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_call (method : Nat) (argc : Nat) : - StepAppendLocals (.call method argc) := by +theorem step_appendLocals_call (method : CIL.Extraction.MethodRef) (prefixes : CallPrefixes) : + StepAppendLocals (.call method prefixes) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped - clear kept - unfold step - frame_natural + refine PaddedAgrees.kept ?_ kept + unfold step modelStep observedCall runModel intrinsicCall referenceCompareCall + referenceSubtractCall featureCall typedTransferCall skipInitCall + padded_natural #print axioms step_appendLocals_call -/-- `callInstance` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_callInstance (method : Nat) (argc : Nat) : - StepAppendLocals (.callInstance method argc) := by +/-- Receiver calls preserve outcomes under trailing local slots. -/ +theorem step_appendLocals_callvirt (method : CIL.Extraction.MethodRef) : + StepAppendLocals (.callvirt method) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped - clear kept + refine PaddedAgrees.kept ?_ kept unfold step - frame_natural + padded_natural -#print axioms step_appendLocals_callInstance +#print axioms step_appendLocals_callvirt -/-- `callVirtual` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_callVirtual (targets : List (CIL.Extraction.TypeExpr × Nat)) - (argc : Nat) : StepAppendLocals (.callVirtual targets argc) := by +/-- `newobj` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_newobj (constructor : Extraction.MethodRef) : + StepAppendLocals (.newobj constructor) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped - clear kept - unfold step - frame_natural + refine PaddedAgrees.kept ?_ kept + unfold step newobjStep constructAggregate constructObject modelStep observedCall runModel + intrinsicCall referenceCompareCall referenceSubtractCall featureCall typedTransferCall + skipInitCall + padded_natural -#print axioms step_appendLocals_callVirtual +#print axioms step_appendLocals_newobj -/-- `tailCall` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_tailCall (method : Nat) (argc : Nat) : - StepAppendLocals (.tailCall method argc) := by +/-- `ldsfld` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldsfld (field : CIL.Extraction.FieldRef) : + StepAppendLocals (.ldsfld field) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step frame_natural -#print axioms step_appendLocals_tailCall +#print axioms step_appendLocals_ldsfld -/-- `newVector256` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_newVector256 (constructor : Nat) (argc : Nat) : - StepAppendLocals (.newVector256 constructor argc) := by +/-- `ldsflda` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldsflda (field : CIL.Extraction.FieldRef) : + StepAppendLocals (.ldsflda field) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step frame_natural -#print axioms step_appendLocals_newVector256 +#print axioms step_appendLocals_ldsflda -/-- `newAggregate` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_newAggregate (constructor : Nat) (argc : Nat) (size : Fin (2^64)) : - StepAppendLocals (.newAggregate constructor argc size) := by +/-- A typed ldobj preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldobj (type : CIL.Extraction.TypeExpr) : + StepAppendLocals (.ldobj type) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + refine PaddedAgrees.kept ?_ kept + unfold step + padded_natural +#print axioms step_appendLocals_ldobj + +/-- A typed stobj preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_stobj (type : CIL.Extraction.TypeExpr) : + StepAppendLocals (.stobj type) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step frame_natural +#print axioms step_appendLocals_stobj -#print axioms step_appendLocals_newAggregate +/-- A typed initobj preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_initobj (type : CIL.Extraction.TypeExpr) : + StepAppendLocals (.initobj type) := by + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + refine PaddedAgrees.kept ?_ kept + unfold step + padded_natural +#print axioms step_appendLocals_initobj -/-- `newObject` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_newObject (constructor argc : Nat) (owner : Extraction.TypeExpr) - (bases : List Extraction.TypeExpr) (size : Nat) : - StepAppendLocals (.newObject constructor argc owner bases size) := by +/-- A typed sizeof preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_sizeof (type : CIL.Extraction.TypeExpr) : + StepAppendLocals (.sizeof type) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step frame_natural +#print axioms step_appendLocals_sizeof -#print axioms step_appendLocals_newObject - -/-- `skipInit` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_skipInit : - StepAppendLocals .skipInit := by +/-- A typed cpobj preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_cpobj (type : CIL.Extraction.TypeExpr) : + StepAppendLocals (.cpobj type) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile .skipInit pc stack frame extra memory - -#print axioms step_appendLocals_skipInit + frame_natural +#print axioms step_appendLocals_cpobj -/-- `asRef` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_asRef : - StepAppendLocals .asRef := by +/-- A typed `ldelem` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldelem_any (element : CIL.Extraction.TypeExpr) : + StepAppendLocals (.ldelem_any element) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile .asRef pc stack frame extra memory + frame_natural -#print axioms step_appendLocals_asRef +#print axioms step_appendLocals_ldelem_any -/-- `feature` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_feature (id : Feature) : - StepAppendLocals (.feature id) := by +/-- A typed `stelem` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_stelem_any (element : CIL.Extraction.TypeExpr) : + StepAppendLocals (.stelem_any element) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step - apply observed_appendLocals - exact fallback_appendLocals body.profile (.feature id) pc stack frame extra memory + frame_natural -#print axioms step_appendLocals_feature +#print axioms step_appendLocals_stelem_any -/-- `intrinsic` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_intrinsic (operation : Intrinsic) (argc : Nat) : - StepAppendLocals (.intrinsic operation argc) := by +/-- An `ldelema` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldelema (element : CIL.Extraction.TypeExpr) (readOnly : Bool) : + StepAppendLocals (.ldelema element readOnly) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step frame_natural -#print axioms step_appendLocals_intrinsic +#print axioms step_appendLocals_ldelema + +/-- `ldtoken` preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_ldtoken (operand : CIL.TokenOperand) : + StepAppendLocals (.ldtoken operand) := by + cases operand <;> intro body pc args frame extra stack memory outcome stepped kept <;> + subst stepped <;> clear kept <;> unfold step <;> frame_natural -/-- `memory` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_memory (operation : MemoryOp) : - StepAppendLocals (.memory operation) := by +#print axioms step_appendLocals_ldtoken + +/-- `stsfld` faults whatever the frame holds. -/ +theorem step_appendLocals_stsfld (field : CIL.Extraction.FieldRef) : + StepAppendLocals (.stsfld field) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step frame_natural -#print axioms step_appendLocals_memory +#print axioms step_appendLocals_stsfld /-- `ret` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_ret (narrow : Option NarrowKind) : - StepAppendLocals (.ret narrow) := by +theorem step_appendLocals_ret : + StepAppendLocals .ret := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept - unfold step - frame_natural + simp only [step, observerCount] + simp only [returnFrom] + by_cases unknown : unspecifiedOperands 1 stack = true + · simp only [unknown, ite_true] + rfl + simp only [unknown, Bool.false_eq_true, ite_false] + repeat' first + | rfl + | (simp only [map_bind, map_pure, FrameAction.appendLocals]; done) + | (apply bind_congr; intro) + | split #print axioms step_appendLocals_ret /-- `throw` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_throw (exception : String) : +theorem step_appendLocals_throw (exception : ExceptionType) : StepAppendLocals (.throw exception) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped @@ -1297,38 +1498,29 @@ theorem step_appendLocals_throw (exception : String) : #print axioms step_appendLocals_throw -/-- `unsupported` preserves outcomes under trailing local slots. -/ -theorem step_appendLocals_unsupported (description : String) : - StepAppendLocals (.unsupported description) := by +/-- A hole preserves outcomes under trailing local slots. -/ +theorem step_appendLocals_unadmitted (opcode : Nat) : + StepAppendLocals (.unadmitted opcode) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped clear kept unfold step frame_natural -#print axioms step_appendLocals_unsupported +#print axioms step_appendLocals_unadmitted -/-- Managed pointers and tables never inspect the unused trailing frame slots. -/ -theorem step_appendLocals_managed (operation : ManagedOp) : - StepAppendLocals (.managed operation) := by +/-- Managed pointers never inspect the unused trailing frame slots. -/ +theorem step_appendLocals_calli (signature : ManagedSignature) (tail : Bool) : + StepAppendLocals (.calli signature tail) := by intro body pc args frame extra stack memory outcome stepped kept subst stepped - clear kept - unfold step - apply observed_appendLocals - unfold managedStep - split - all_goals repeat' first - | rfl - | contradiction - | (simp only [map_bind, map_pure, FrameAction.appendLocals, Frame.appendLocals]; done) - | (apply bind_congr; intro) - | split - | simp only [map_bind] -#print axioms step_appendLocals_managed + refine PaddedAgrees.kept ?_ kept + unfold step calliStep + padded_natural +#print axioms step_appendLocals_calli /-- Every instruction preserves successful and throwing steps under trailing local slots. -/ -theorem step_appendLocals (body : Method) (op : Op) (pc : Nat) (args : List Value) +theorem step_appendLocals (body : Method) (op : Instruction) (pc : Nat) (args : List Value) (frame : Frame) (extra : List LocalSlot) (stack : List Value) (memory : Memory) (outcome : ExecutionResult FrameAction) (stepped : step body op pc args frame stack memory = outcome) (kept : StepKept outcome) : @@ -1336,191 +1528,170 @@ theorem step_appendLocals (body : Method) (op : Op) (pc : Nat) (args : List Valu FrameAction.appendLocals extra <$> outcome := by have padded : StepAppendLocals op := by cases op - case managed operation => exact step_appendLocals_managed operation - case nativeBinary operation => - exact step_appendLocals_nativeBinary operation - case divide signed remainder => - exact step_appendLocals_divide signed remainder - case checkedArithmetic kind signed => - exact step_appendLocals_checkedArithmetic kind signed - case checkedConvert width signed unsignedInput => - exact step_appendLocals_checkedConvert width signed unsignedInput - case stackAlloc initLocals => - exact step_appendLocals_stackAlloc initLocals - case newArray element => exact step_appendLocals_newArray element - case newReferenceArray element => exact step_appendLocals_newReferenceArray element + case calli signature tail => exact step_appendLocals_calli signature tail + case div | div_un | rem | rem_un | add_ovf | add_ovf_un | sub_ovf | sub_ovf_un | mul_ovf + | mul_ovf_un => exact step_appendLocals_faulting _ rfl + case conv_ovf target => + exact step_appendLocals_conv_ovf target + case conv_ovf_un target => + exact step_appendLocals_conv_ovf_un target + case localloc => + exact step_appendLocals_localloc + case newarr element => exact step_appendLocals_newarr element + case ldobj type => exact step_appendLocals_ldobj type + case stobj type => exact step_appendLocals_stobj type + case initobj type => exact step_appendLocals_initobj type + case sizeof type => exact step_appendLocals_sizeof type + case cpobj type => exact step_appendLocals_cpobj type + case ldelem_any element => exact step_appendLocals_ldelem_any element + case stelem_any element => exact step_appendLocals_stelem_any element + case ldelema element readOnly => exact step_appendLocals_ldelema element readOnly case nop => exact step_appendLocals_nop case neg => exact step_appendLocals_neg - case gt => - exact step_appendLocals_gt + case cgt => + exact step_appendLocals_cgt case bgt target => exact step_appendLocals_bgt target case ble target => exact step_appendLocals_ble target - case bleu target => - exact step_appendLocals_bleu target - case loadAggregate size => - exact step_appendLocals_loadAggregate size - case copyObject size => - exact step_appendLocals_copyObject size - case storeAggregate size => - exact step_appendLocals_storeAggregate size - case initAggregate size => - exact step_appendLocals_initAggregate size - case loadScalar width signed => - exact step_appendLocals_loadScalar width signed - case storeScalar width => - exact step_appendLocals_storeScalar width - case fieldAt offset width signed => - exact step_appendLocals_fieldAt offset width signed - case fieldAddress offset => - exact step_appendLocals_fieldAddress offset - case setFieldAt offset width => - exact step_appendLocals_setFieldAt offset width - case aggregateFieldAt offset size => - exact step_appendLocals_aggregateFieldAt offset size - case setAggregateFieldAt offset size => - exact step_appendLocals_setAggregateFieldAt offset size - case arg index narrow => - exact step_appendLocals_arg index narrow - case vectorArg index => - exact step_appendLocals_vectorArg index - case vectorArgAddr index => - exact step_appendLocals_vectorArgAddr index - case «local» index narrow => - exact step_appendLocals_local index narrow - case vectorLocal index => - exact step_appendLocals_vectorLocal index - case vectorLocalAddr index => - exact step_appendLocals_vectorLocalAddr index - case setVectorLocal index => - exact step_appendLocals_setVectorLocal index - case initializeSpanLocal index constructor => - exact step_appendLocals_initializeSpanLocal index constructor - case localAddr index => - exact step_appendLocals_localAddr index - case setLocal index narrow => - exact step_appendLocals_setLocal index narrow - case lane index => - exact step_appendLocals_lane index - case laneAddress index => - exact step_appendLocals_laneAddress index - case setLane index => - exact step_appendLocals_setLane index - case const32 word => - exact step_appendLocals_const32 word - case const64 word => - exact step_appendLocals_const64 word - case convI1 => - exact step_appendLocals_convI1 - case convI2 => - exact step_appendLocals_convI2 - case convU2 => - exact step_appendLocals_convU2 - case convI8 => - exact step_appendLocals_convI8 - case convI4 => - exact step_appendLocals_convI4 - case convU1 => - exact step_appendLocals_convU1 - case convU4 => - exact step_appendLocals_convU4 - case convU8 => - exact step_appendLocals_convU8 - case convU => - exact step_appendLocals_convU - case bnot => - exact step_appendLocals_bnot + case ble_un target => + exact step_appendLocals_ble_un target + case ldarg index => + exact step_appendLocals_ldarg index + case starg index => + exact step_appendLocals_starg index + case ldarga index => + exact step_appendLocals_ldarga index + case ldloc index => + exact step_appendLocals_ldloc index + case ldloca index => + exact step_appendLocals_ldloca index + case stloc index => + exact step_appendLocals_stloc index + case ldc_i4 value => + exact step_appendLocals_ldc_i4 value + case ldc_i8 value => + exact step_appendLocals_ldc_i8 value + case ldnull => + exact step_appendLocals_ldnull + case conv_i1 => + exact step_appendLocals_conv_i1 + case conv_i2 => + exact step_appendLocals_conv_i2 + case conv_u2 => + exact step_appendLocals_conv_u2 + case conv_i8 => + exact step_appendLocals_conv_i8 + case conv_i4 => + exact step_appendLocals_conv_i4 + case conv_u1 => + exact step_appendLocals_conv_u1 + case conv_u4 => + exact step_appendLocals_conv_u4 + case conv_u8 => + exact step_appendLocals_conv_u8 + case conv_u => + exact step_appendLocals_conv_u + case conv_i => + exact step_appendLocals_conv_i + case not => + exact step_appendLocals_not case add => exact step_appendLocals_add case sub => exact step_appendLocals_sub case mul => exact step_appendLocals_mul - case band => - exact step_appendLocals_band - case bor => - exact step_appendLocals_bor - case bxor => - exact step_appendLocals_bxor + case and => + exact step_appendLocals_and + case or => + exact step_appendLocals_or + case xor => + exact step_appendLocals_xor case shl => exact step_appendLocals_shl case shr => exact step_appendLocals_shr - case shrUn => - exact step_appendLocals_shrUn - case lt => - exact step_appendLocals_lt - case ltu => - exact step_appendLocals_ltu - case gtu => - exact step_appendLocals_gtu - case eq => - exact step_appendLocals_eq - case load64 => - exact step_appendLocals_load64 - case store64 => - exact step_appendLocals_store64 + case shr_un => + exact step_appendLocals_shr_un + case clt => + exact step_appendLocals_clt + case clt_un => + exact step_appendLocals_clt_un + case cgt_un => + exact step_appendLocals_cgt_un + case ceq => + exact step_appendLocals_ceq + case ldind kind => + exact step_appendLocals_ldind kind + case stind kind => + exact step_appendLocals_stind kind + case ldlen => + exact step_appendLocals_ldlen + case ldelem kind => + exact step_appendLocals_ldelem kind + case ldelem_ref => + exact step_appendLocals_ldelem_ref + case stelem kind => + exact step_appendLocals_stelem kind + case stelem_ref => + exact step_appendLocals_stelem_ref case dup => exact step_appendLocals_dup case pop => exact step_appendLocals_pop - case branch target => - exact step_appendLocals_branch target + case br target => + exact step_appendLocals_br target case switch targets => exact step_appendLocals_switch targets - case brzero target => - exact step_appendLocals_brzero target - case brnonzero target => - exact step_appendLocals_brnonzero target - case objectTest type throwing => - exact step_appendLocals_objectTest type throwing - case bltu target => - exact step_appendLocals_bltu target - case bgeu target => - exact step_appendLocals_bgeu target - case bgtu target => - exact step_appendLocals_bgtu target + case brfalse target => + exact step_appendLocals_brfalse target + case brtrue target => + exact step_appendLocals_brtrue target + case ldfld field => exact step_appendLocals_ldfld field + case ldflda field => exact step_appendLocals_ldflda field + case stfld field => exact step_appendLocals_stfld field + case castclass type => + exact step_appendLocals_castclass type + case isinst type => + exact step_appendLocals_isinst type + case blt_un target => + exact step_appendLocals_blt_un target + case bge_un target => + exact step_appendLocals_bge_un target + case bgt_un target => + exact step_appendLocals_bgt_un target case bge target => exact step_appendLocals_bge target case blt target => exact step_appendLocals_blt target case beq target => exact step_appendLocals_beq target - case bne target => - exact step_appendLocals_bne target - case call method argc => - exact step_appendLocals_call method argc - case callInstance method argc => - exact step_appendLocals_callInstance method argc - case callVirtual targets argc => - exact step_appendLocals_callVirtual targets argc - case tailCall method argc => - exact step_appendLocals_tailCall method argc - case newVector256 constructor argc => - exact step_appendLocals_newVector256 constructor argc - case newAggregate constructor argc size => - exact step_appendLocals_newAggregate constructor argc size - case newObject constructor argc owner bases size => - exact step_appendLocals_newObject constructor argc owner bases size - case skipInit => - exact step_appendLocals_skipInit - case asRef => - exact step_appendLocals_asRef - case feature id => - exact step_appendLocals_feature id - case intrinsic operation argc => - exact step_appendLocals_intrinsic operation argc - case memory operation => - exact step_appendLocals_memory operation - case ret narrow => - exact step_appendLocals_ret narrow + case bne_un target => + exact step_appendLocals_bne_un target + case call method prefixes => + exact step_appendLocals_call method prefixes + case callvirt method => exact step_appendLocals_callvirt method + case newobj constructor => + exact step_appendLocals_newobj constructor + case cpblk | initblk | ldftn | ldstr => + intro body pc args frame extra stack memory outcome stepped kept + subst stepped + refine PaddedAgrees.kept ?_ kept + unfold step + padded_natural + case ldsfld field => exact step_appendLocals_ldsfld field + case ldsflda field => exact step_appendLocals_ldsflda field + case ldtoken operand => exact step_appendLocals_ldtoken operand + case stsfld field => exact step_appendLocals_stsfld field + case ret => + exact step_appendLocals_ret case throw exception => exact step_appendLocals_throw exception - case unsupported description => - exact step_appendLocals_unsupported description + case unadmitted opcode => + exact step_appendLocals_unadmitted opcode exact padded body pc args frame extra stack memory outcome stepped kept #print axioms step_appendLocals diff --git a/CIL/Safety/Embedding/DeadLocals.lean b/CIL/Safety/Embedding/DeadLocals.lean index 1fac1524b..57fb3825e 100644 --- a/CIL/Safety/Embedding/DeadLocals.lean +++ b/CIL/Safety/Embedding/DeadLocals.lean @@ -21,27 +21,31 @@ namespace CIL.Safety open CIL.Embedding -/-- Replacing a body's locals keeps every step that succeeds or throws: steps read the - profile, return arity and static sites, never the local declarations. -/ -theorem step_withLocalsOf (actual expected : CIL.Method) (op : CIL.Op) (pc : Nat) +/-- Replacing the locals of a body without parameter homes keeps every step that succeeds or + throws: steps read the profile, return arity, signature and static sites, and only a home's + slot depends on the local declarations. -/ +theorem step_withLocalsOf (actual expected : CIL.Method) (op : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (memory : Memory) - (outcome : ExecutionResult FrameAction) + (outcome : ExecutionResult FrameAction) (homes : actual.parameterHomes = []) (stepped : step (actual.withLocalsOf expected) op pc args frame stack memory = outcome) (kept : StepKept outcome) : step actual op pc args frame stack memory = outcome := by have context : StepContext (actual.withLocalsOf expected) actual id op pc pc := - ⟨rfl, rfl, rfl, fun _ => rfl⟩ + ⟨rfl, rfl, rfl, rfl, by + funext index + simp only [Method.argumentAccess, Method.withLocalsOf, homes, List.findIdx?_nil], + rfl, rfl, fun _ => rfl⟩ cases outcome with | ok action => have same := step_relabel op _ actual id pc pc args frame stack memory action context stepped - rw [Op.relabel_id] at same + rw [Instruction.relabel_id] at same rw [same] cases action <;> rfl | error fault => obtain ⟨exception, thrown⟩ := kept subst thrown have same := step_raise op _ actual id pc pc args frame stack memory exception context stepped - rwa [Op.relabel_id] at same + rwa [Instruction.relabel_id] at same #print axioms step_withLocalsOf @@ -186,7 +190,6 @@ theorem enterFrame_deadLocals {expected actual : CIL.Method} {map : List Nat} simp only [enterFrame, Method.withLocalsOf, selected, embeds.parameterHomes, bind_map_left, map_bind] refine bind_congr fun ⟨slots, owned, entered⟩ => ?_ - refine bind_congr fun ⟨arguments, argumentIds, entered⟩ => ?_ simp [makeParameterHomes, padSlots, deadSlots, Frame.appendLocals] #print axioms enterFrame_deadLocals @@ -213,14 +216,21 @@ theorem returnsValue_of_steps (body target : CIL.Method) (frame : Frame) (memory target.returnsValue = body.returnsValue := by cases original : body.returnsValue <;> cases actual : target.returnsValue · rfl - · have kept := steps (.ret none) 0 [] frame [] memory (.ok (.returned [] memory)) - (by simp [step, original, observerCount, unspecifiedOperands, Pure.pure, Except.pure]) trivial - simp [step, actual, observerCount, unspecifiedOperands] at kept - · have kept := steps (.ret none) 0 [] frame [.scalar (.i32 0)] memory + · have kept := steps .ret 0 [] frame [] memory (.ok (.returned [] memory)) + (by simp [step, returnFrom, CIL.Method.returnNarrow, original, + observerCount, unspecifiedOperands, + Pure.pure, Except.pure]) trivial + cases narrow : target.returnNarrow <;> + simp [step, returnFrom, narrow, actual, observerCount, unspecifiedOperands] at kept + · have kept := steps .ret 0 [] frame [.scalar (.i32 0)] memory (.ok (.returned [.scalar (.i32 0)] memory)) - (by simp [step, original, observerCount, unspecifiedOperands, Value.hasUnspecified, - CIL.Value.hasUnspecified, checkedValue, numericValue, Pure.pure, Except.pure]) trivial - simp [step, actual, observerCount, unspecifiedOperands, Value.hasUnspecified, + (by + cases narrow : body.returnNarrow <;> + simp [step, returnFrom, narrow, original, observerCount, unspecifiedOperands, + Value.hasUnspecified, CIL.Value.hasUnspecified, checkedValue, numericValue, + CIL.NarrowKind.extend_zero, Pure.pure, Except.pure]) trivial + simp [step, returnFrom, CIL.Method.returnNarrow, actual, observerCount, + unspecifiedOperands, Value.hasUnspecified, CIL.Value.hasUnspecified] at kept · rfl #print axioms returnsValue_of_steps @@ -242,7 +252,7 @@ theorem run_padded {unpadded program : CIL.Program} {extra : Nat → List LocalS cases thrown | succ fuel ih => intro method pc args frame stack memory outcome ran transported - rw [run] at ran ⊢ + rw [run_succ] at ran ⊢ cases fetched : unpadded[method]? with | none => simp only [fetched] at ran @@ -395,7 +405,7 @@ theorem invoke_padded {unpadded program : CIL.Program} {extra : Nat → List Loc {memory : Memory} {outcome : Except LocatedFault (Memory × List Value)} (invoked : invoke unpadded fuel method args memory = outcome) (transported : Transported outcome) : invoke program fuel method args memory = outcome := by - unfold invoke at invoked ⊢ + rw [invoke_eq] at invoked ⊢ cases fetched : unpadded[method]? with | none => simp only [fetched] at invoked @@ -433,7 +443,8 @@ theorem _root_.CIL.EmbedsDeadLocals.padded {program : CIL.Program} {index : Nat} simp only [bound, ite_true, Option.some.injEq] at fetched subst fetched refine ⟨actual, found, rfl, fun op pc args frame stack memory outcome stepped kept => - step_withLocalsOf actual expected op pc args frame stack memory outcome stepped kept, + step_withLocalsOf actual expected op pc args frame stack memory outcome + embeds.parameterHomes stepped kept, fun args memory => ?_⟩ simp only [ite_true] exact enterFrame_deadLocals embeds args memory @@ -490,7 +501,7 @@ theorem _root_.CIL.MethodEmbedsDeadLocals.invoke {program : CIL.Program} {index theorem _root_.CIL.EmbedsDeadLocals.invoke_raise {program : CIL.Program} {index : Nat} {expected actual : CIL.Method} {map : List Nat} (found : program[index]? = some actual) (embeds : EmbedsDeadLocals expected actual map) {fuel method : Nat} {args : List Value} - {memory : Memory} {fault : LocatedFault} {exception : String} + {memory : Memory} {fault : LocatedFault} {exception : ExceptionType} (invoked : invoke (program.set index expected) fuel method args memory = .error fault) (thrown : fault.fault = .thrown exception) : invoke program fuel method args memory = @@ -503,7 +514,7 @@ theorem _root_.CIL.EmbedsDeadLocals.invoke_raise {program : CIL.Program} {index `program`. -/ theorem _root_.CIL.MethodEmbedsDeadLocals.raises {program : CIL.Program} {index : Nat} {expected : CIL.Method} (embeds : MethodEmbedsDeadLocals program index expected) - {method : Nat} {args : List Value} {memory : Memory} {exception : String} + {method : Nat} {args : List Value} {memory : Memory} {exception : ExceptionType} (raised : Raises (program.set index expected) method args memory exception) : Raises program method args memory exception := by obtain ⟨_, _, found, embedded⟩ := embeds diff --git a/CIL/Safety/Embedding/Outcomes.lean b/CIL/Safety/Embedding/Outcomes.lean index a964ddf1e..8619de463 100644 --- a/CIL/Safety/Embedding/Outcomes.lean +++ b/CIL/Safety/Embedding/Outcomes.lean @@ -18,7 +18,8 @@ open CIL.Embedding /-- A named exception raised by an invocation of `expected` is raised by `actual`. -/ theorem Raises.of_refines {expected actual : CIL.Program} {maps : Nat → Nat → Nat} (refines : ProgramRefines expected actual maps) {method : Nat} {args : List Value} - {memory : Memory} {exception : String} (raised : Raises expected method args memory exception) : + {memory : Memory} {exception : ExceptionType} + (raised : Raises expected method args memory exception) : Raises actual method args memory exception := by obtain ⟨fuel, fault, invoked, thrown⟩ := raised exact ⟨fuel, _, invoke_raise_refines refines invoked thrown, thrown⟩ @@ -27,7 +28,7 @@ theorem Raises.of_refines {expected actual : CIL.Program} {maps : Nat → Nat the renamed pc. -/ theorem RunRaises.of_refines {expected actual : CIL.Program} {maps : Nat → Nat → Nat} (refines : ProgramRefines expected actual maps) {method pc : Nat} {args : List Value} - {frame : Frame} {stack : List Value} {memory : Memory} {exception : String} + {frame : Frame} {stack : List Value} {memory : Memory} {exception : ExceptionType} (raised : RunRaises expected method pc args frame stack memory exception) : RunRaises actual method (maps method pc) args frame stack memory exception := by obtain ⟨fuel, fault, ran, thrown⟩ := raised @@ -74,7 +75,7 @@ theorem _root_.CIL.MethodEmbeds.method_holds {Input Output : Type} `program`. -/ theorem _root_.CIL.MethodEmbeds.raises {program : CIL.Program} {index : Nat} {expected : CIL.Method} (embeds : MethodEmbeds program index expected) {method : Nat} - {args : List Value} {memory : Memory} {exception : String} + {args : List Value} {memory : Memory} {exception : ExceptionType} (raised : Raises (program.set index expected) method args memory exception) : Raises program method args memory exception := by obtain ⟨_, refines⟩ := embeds.refines diff --git a/CIL/Safety/Embedding/Simulation.lean b/CIL/Safety/Embedding/Simulation.lean index 4fecbdfb5..f789cdab4 100644 --- a/CIL/Safety/Embedding/Simulation.lean +++ b/CIL/Safety/Embedding/Simulation.lean @@ -19,8 +19,8 @@ namespace CIL.Embedding open CIL -theorem Op.relabel_id (op : Op) : op.relabel id = op := by - induction op <;> simp only [Op.relabel, id] <;> simp_all +theorem Instruction.relabel_id (op : Instruction) : op.relabel id = op := by + induction op <;> simp only [Instruction.relabel, id] <;> simp_all /-- The facts about one method that the simulation consumes. -/ structure BodyRefines (expected actual : Method) (r : Nat → Nat) : Prop where @@ -29,23 +29,25 @@ structure BodyRefines (expected actual : Method) (r : Nat → Nat) : Prop where locals : actual.locals = expected.locals localKinds : actual.localKinds = expected.localKinds opaqueVectorLocals : actual.opaqueVectorLocals = expected.opaqueVectorLocals - vectorArgs : actual.vectorArgs = expected.vectorArgs parameterHomes : actual.parameterHomes = expected.parameterHomes + declared : actual.declared = expected.declared + environment : actual.environment = expected.environment descriptors : actual.staticSites.map Prod.snd = expected.staticSites.map Prod.snd entry : r 0 = 0 /-- Each live expected instruction sits relabeled at its renamed pc with the same static - descriptor, and an instruction that falls through keeps its successor adjacent. -/ + descriptor and link, and an instruction that falls through keeps its successor adjacent. -/ code : ∀ p op, expected.code[p]? = some op → op.live = true → - actual.code[r p]? = some (op.relabel r) ∧ staticAt actual (r p) = staticAt expected p ∧ - (op.continues = true → r (p + 1) = r p + 1) + actual.code[r p]? = some (op.relabel r) ∧ actual.staticAt (r p) = expected.staticAt p ∧ + actual.linkAt (r p) = expected.linkAt p ∧ (op.continues = true → r (p + 1) = r p + 1) /-- Every body refines itself under the identity renaming. -/ theorem BodyRefines.refl (body : Method) : BodyRefines body body id := - ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, - fun _ op found _ => ⟨by simp [found, Op.relabel_id], rfl, fun _ => rfl⟩⟩ + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun _ op found _ => ⟨by simp [found, Instruction.relabel_id], rfl, rfl, fun _ => rfl⟩⟩ /-- A live instruction at `p` appears in `sites` with its mapped pc and mapped successor. -/ -theorem mem_sites {limit : Nat} {code : List Op} {map : List Nat} {start p : Nat} {op : Op} +theorem mem_sites {limit : Nat} {code : List Instruction} {map : List Nat} {start p : Nat} + {op : Instruction} (found : code[p]? = some op) (live : op.live = true) : (⟨start + p, map.getD p limit, map.getD (p + 1) limit, op⟩ : Site) ∈ sites limit start code map := by @@ -71,26 +73,25 @@ theorem mem_sites {limit : Nat} {code : List Op} {map : List Nat} {start p : Nat · exact member /-- `pick` agrees with pointwise lookup whenever its cursor is `code.drop start`. -/ -theorem pick_eq (code : List Op) (qs : List Nat) (start : Nat) : +theorem pick_eq (code : List Instruction) (qs : List Nat) (start : Nat) : pick code (code.drop start) start qs = qs.map (fun q => code[q]?) := by induction qs generalizing start with | nil => rfl | cons q qs ih => - unfold pick - split - · rename_i ordered - have dropped : (code.drop start).drop (q - start) = code.drop q := by - rw [List.drop_drop]; congr 1; omega - simp only [dropped, List.head?_drop, ih, List.map_cons] - · simp only [ih, List.map_cons] + have dropped : (if start ≤ q then (code.drop start).drop (q - start) else code.drop q) = + code.drop q := by + split + · rw [List.drop_drop]; congr 1; omega + · rfl + simp only [pick, dropped, List.head?_drop, ih, List.map_cons] /-- The decidable `Embeds` check yields the per-method facts the simulation consumes. -/ theorem _root_.CIL.Embeds.refines {expected actual : Method} {map : List Nat} (embeds : Embeds expected actual map) : BodyRefines expected actual (rename map actual.code.length) := by refine ⟨embeds.profile, embeds.returnsValue, embeds.locals, embeds.localKinds, - embeds.opaqueVectorLocals, embeds.vectorArgs, embeds.parameterHomes, embeds.descriptors, - embeds.entry, ?_⟩ + embeds.opaqueVectorLocals, embeds.parameterHomes, embeds.declared, + embeds.environment, embeds.descriptors, embeds.entry, ?_⟩ intro p op found live have member := mem_sites (limit := actual.code.length) (map := map) (start := 0) found live rw [Nat.zero_add] at member @@ -101,9 +102,14 @@ theorem _root_.CIL.Embeds.refines {expected actual : Method} {map : List Nat} rw [dropped, List.map_map] at picked have code := List.map_inj_left.mp picked _ member have statics := List.map_inj_left.mp embeds.statics _ member + -- The reads are the first entry of each table, as `linkAt` gives. + have reads := embeds.links + rw [Extraction.linkReads_eq, Extraction.linkReads_eq, List.map_map, List.map_map] at reads + have link : actual.linkAt (rename map actual.code.length p) = expected.linkAt p := + List.map_inj_left.mp reads _ member have flow := List.all_eq_true.mp embeds.flow _ member simp only [Bool.or_eq_true, Bool.not_eq_true', beq_iff_eq] at flow - refine ⟨code, statics, fun continues => ?_⟩ + refine ⟨code, statics, link, fun continues => ?_⟩ rcases flow with stops | next · rw [continues] at stops; cases stops · exact next @@ -119,92 +125,70 @@ theorem enterFrame_congr {expected actual : CIL.Method} {r : Nat → Nat} (refines : BodyRefines expected actual r) (args : List Value) (m : Memory) : enterFrame actual args m = enterFrame expected args m := by simp only [enterFrame, refines.locals, refines.localKinds, refines.opaqueVectorLocals, - refines.vectorArgs, refines.parameterHomes] + refines.parameterHomes] -/-- `.unsupported` never steps successfully. -/ -theorem step_unsupported {body : CIL.Method} {description : String} {pc : Nat} {args : List Value} +/-- A hole never steps successfully. -/ +theorem step_unadmitted_ne_ok {body : CIL.Method} {opcode : Nat} {pc : Nat} {args : List Value} {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} - (h : step body (.unsupported description) pc args frame stack m = .ok action) : False := by - change Except.error - (ExecutionFault.unsupported "Not a memory instruction") = - .ok action at h + (h : step body (.unadmitted opcode) pc args frame stack m = .ok action) : False := by cases h +#print axioms step_unadmitted_ne_ok + /-- `.throw` never steps successfully. -/ -theorem step_throw {body : CIL.Method} {exception : String} {pc : Nat} {args : List Value} +theorem step_throw {body : CIL.Method} {exception : ExceptionType} {pc : Nat} {args : List Value} {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} (h : step body (.throw exception) pc args frame stack m = .ok action) : False := by cases h /-- A successful `.ret` step always returns. -/ -theorem step_ret_returns {body : CIL.Method} {narrow : Option NarrowKind} {pc : Nat} +theorem step_ret_returns {body : CIL.Method} {pc : Nat} {args : List Value} {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} - (h : step body (.ret narrow) pc args frame stack m = .ok action) : + (h : step body .ret pc args frame stack m = .ok action) : ∃ values m', action = .returned values m' := by - cases narrow - · have known : unspecifiedOperands 1 stack = false := by - cases observed : unspecifiedOperands 1 stack with - | false => rfl - | true => simp [step, observerCount, observed] at h - simp only [step, observerCount, known, Bool.false_or, Bool.false_eq_true, ↓reduceIte] at h - change (if stack.length = (if body.returnsValue then 1 else 0) then - (do return FrameAction.returned (← stack.mapM (checkedValue m)) m) - else .error .invalidState) = .ok action at h - by_cases sized : stack.length = (if body.returnsValue then 1 else 0) - · rw [ite_eq_left sized] at h - obtain ⟨_, _, h⟩ := except_bind_ok h - cases h - exact ⟨_, _, rfl⟩ - · rw [ite_eq_right sized] at h - cases h - · rename_i kind - cases stack with - | nil => cases h - | cons value rest => - cases value <;> try (solve | cases rest <;> cases h) - rename_i value - cases value <;> cases rest <;> try (solve | cases h) - rename_i bits - change (if body.returnsValue then - (do return FrameAction.returned - (← [Value.scalar (.i32 (kind.extend bits))].mapM (checkedValue m)) m) - else .error .invalidState) = .ok action at h - split at h - · obtain ⟨_, _, h⟩ := except_bind_ok h - cases h - exact ⟨_, _, rfl⟩ - · cases h + unfold step at h + dsimp only at h + split at h + · cases h + · unfold returnFrom at h + repeat' first + | (split at h) + | (obtain ⟨_, _, h⟩ := except_bind_ok h) + | (cases h <;> exact ⟨_, _, rfl⟩) + +#print axioms step_ret_returns /-- Calls and constructions come only from instructions that resume at the next pc. -/ -theorem continues_of_invocation {body : CIL.Method} {op : CIL.Op} {pc : Nat} {args : List Value} +theorem continues_of_invocation {body : CIL.Method} {op : CIL.Instruction} {pc : Nat} + {args : List Value} {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} (h : step body op pc args frame stack m = .ok action) (resumes : match action with | .call .. | .construct .. => True | _ => False) : op.continues = true := by cases op case throw => exact (step_throw h).elim - case unsupported => exact (step_unsupported h).elim - case branch target => + case unadmitted => exact (step_unadmitted_ne_ok h).elim + case br target => change Except.ok (.next target stack frame m) = .ok action at h cases h exact resumes.elim - case ret narrow => + case ret => obtain ⟨_, _, rfl⟩ := step_ret_returns h exact resumes.elim - case tailCall callee argc => - simp only [step] at h - repeat' first - | contradiction - | exact resumes.elim - | cases h - | split at h - | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h - case managed operation => - cases operation <;> try rfl - rename_i signature catalog tail + case call method prefixes => + cases tail : prefixes.tail + · simp [CIL.Instruction.continues, tail] + · simp only [step, observedCall, tail, ite_true] at h + repeat' first + | contradiction + | exact resumes.elim + | cases h + | split at h + | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h + case calli signature tail => cases tail · rfl - · simp only [step, managedStep] at h + · simp only [step, calliStep] at h repeat' first | contradiction | exact resumes.elim @@ -303,7 +287,7 @@ theorem run_outcome_refines {expected actual : CIL.Program} {maps : Nat → Nat cases thrown | succ fuel ih => intro method pc args frame stack m outcome h transported - rw [run] at h ⊢ + rw [run_succ] at h ⊢ cases fetched : expected[method]? with | none => simp only [fetched] at h @@ -332,18 +316,24 @@ theorem run_outcome_refines {expected actual : CIL.Program} {maps : Nat → Nat cases op <;> first | rfl | (change Except.error (ExecutionFault.unsupported _) = _ at stepped; cases stepped) - obtain ⟨code, statics, next⟩ := refined.code pc op decoded live + obtain ⟨code, statics, link, next⟩ := refined.code pc op decoded live + have arguments := Method.argumentAccess_congr refined.parameterHomes + (congrArg List.length refined.localKinds) refined.declared refined.environment have actualStep := step_raise op body target (maps method) pc (maps method pc) args - frame stack m exception ⟨refined.profile, refined.returnsValue, statics, next⟩ stepped + frame stack m exception ⟨refined.profile, refined.returnsValue, refined.declared, + refined.environment, arguments, statics, link, next⟩ stepped simp only [found, code, actualStep, Except.mapError, bind, Except.bind] rfl | ok action => simp only [stepped, Except.mapError] at h have live : op.live = true := by - cases op <;> first | rfl | exact (step_unsupported stepped).elim - obtain ⟨code, statics, next⟩ := refined.code pc op decoded live + cases op <;> first | rfl | exact (step_unadmitted_ne_ok stepped).elim + obtain ⟨code, statics, link, next⟩ := refined.code pc op decoded live + have arguments := Method.argumentAccess_congr refined.parameterHomes + (congrArg List.length refined.localKinds) refined.declared refined.environment have actualStep := step_relabel op body target (maps method) pc (maps method pc) args - frame stack m action ⟨refined.profile, refined.returnsValue, statics, next⟩ stepped + frame stack m action ⟨refined.profile, refined.returnsValue, refined.declared, + refined.environment, arguments, statics, link, next⟩ stepped simp only [found, code, actualStep, Except.mapError, bind, Except.bind] cases action with | next target' stack' frame' m' => @@ -399,7 +389,7 @@ theorem run_outcome_refines {expected actual : CIL.Program} {maps : Nat → Nat split at h · subst h; obtain ⟨_, thrown⟩ := transported; cases thrown · rename_i returns - simp only [returns, ↓reduceIte] + simp only [returns] cases checked : arguments.mapM (checkedValue (leaveFrame frame m')) with | error fault => simp only [checked] at h ⊢; subst h; rfl | ok values => @@ -473,7 +463,8 @@ theorem run_refines {expected actual : CIL.Program} {maps : Nat → Nat → Nat} /-- An expected run that throws `exception` throws it in the actual run, at the relocated pc. -/ theorem run_raise_refines {expected actual : CIL.Program} {maps : Nat → Nat → Nat} (refines : ProgramRefines expected actual maps) {fuel method pc : Nat} {args : List Value} - {frame : Frame} {stack : List Value} {m : Memory} {fault : LocatedFault} {exception : String} + {frame : Frame} {stack : List Value} {m : Memory} {fault : LocatedFault} + {exception : ExceptionType} (h : run expected fuel method pc args frame stack m = .error fault) (thrown : fault.fault = .thrown exception) : run actual fuel method (maps method pc) args frame stack m = .error (fault.relocate maps) := @@ -486,7 +477,7 @@ theorem invoke_outcome_refines {expected actual : CIL.Program} {maps : Nat → N {m : Memory} {outcome : Except LocatedFault (Memory × List Value)} (h : invoke expected fuel method args m = outcome) (transported : Transported outcome) : invoke actual fuel method args m = outcome.mapError (·.relocate maps) := by - unfold invoke at h ⊢ + rw [invoke_eq] at h ⊢ cases fetched : expected[method]? with | none => simp only [fetched] at h @@ -528,7 +519,7 @@ theorem invoke_refines {expected actual : CIL.Program} {maps : Nat → Nat → N relocated pc. -/ theorem invoke_raise_refines {expected actual : CIL.Program} {maps : Nat → Nat → Nat} (refines : ProgramRefines expected actual maps) {fuel method : Nat} {args : List Value} - {m : Memory} {fault : LocatedFault} {exception : String} + {m : Memory} {fault : LocatedFault} {exception : ExceptionType} (h : invoke expected fuel method args m = .error fault) (thrown : fault.fault = .thrown exception) : invoke actual fuel method args m = .error (fault.relocate maps) := @@ -598,7 +589,8 @@ theorem _root_.CIL.Embeds.run {program : CIL.Program} {index : Nat} theorem _root_.CIL.Embeds.invoke_raise {program : CIL.Program} {index : Nat} {expected target : CIL.Method} {map : List Nat} (found : program[index]? = some target) (embeds : Embeds expected target map) - {fuel method : Nat} {args : List Value} {m : Memory} {fault : LocatedFault} {exception : String} + {fuel method : Nat} {args : List Value} {m : Memory} {fault : LocatedFault} + {exception : ExceptionType} (h : CIL.Safety.invoke (program.set index expected) fuel method args m = .error fault) (thrown : fault.fault = .thrown exception) : CIL.Safety.invoke program fuel method args m = diff --git a/CIL/Safety/Embedding/Step.lean b/CIL/Safety/Embedding/Step.lean index bab41c6e5..929eb1ae9 100644 --- a/CIL/Safety/Embedding/Step.lean +++ b/CIL/Safety/Embedding/Step.lean @@ -15,41 +15,6 @@ instructions reuse the base-step lemmas of `CIL.Embedding.BaseStep`, and the res both steps in lockstep with the tactics defined here. -/ -namespace CIL.Embedding - -open CIL - -/-- A mixed native operation resumes at the relabeled successor. -/ -theorem nativeBinaryValues_relabel (r : Nat → Nat) (op : Op) (pc q : Nat) (x y : Value) - (next : r (pc + 1) = q + 1) (target : Nat) (values : List Value) - (h : nativeBinaryValues op pc x y = .ok (target, values)) : - nativeBinaryValues (op.relabel r) q x y = .ok (r target, values) := by - unfold nativeBinaryValues at h ⊢ - simp only [bind, Except.bind] at h ⊢ - rw [widenNativeOperand_relabel] - split at h - rotate_right - · cases h - all_goals - try simp only at h ⊢ - cases op <;> simp only [Op.relabel, nativePureBinary, Bool.false_eq_true, ↓reduceIte, - reduceCtorEq] at h ⊢ - all_goals (split at h <;> (try (cases h; done))) - all_goals rename_i found - all_goals first - | ((try simp only [found]) - simp only [pure, Except.pure] at h ⊢ - cases h - rw [next] - done) - | (have stepped := base_relabel_of_nativePure _ rfl r pc q _ _ _ _ _ (fun _ => next) found - simp only [Op.relabel] at stepped - rw [stepped] - cases h - rfl) - -end CIL.Embedding - namespace CIL.Safety open CIL.Embedding @@ -59,16 +24,70 @@ def FrameAction.relabel (r : Nat → Nat) : FrameAction → FrameAction | .next target stack frame memory => .next (r target) stack frame memory | action => action +/-- A call's runtime model reads its pc only to continue at the next one. -/ +theorem modelStep_at (model : Option CIL.RuntimeModel) (body : CIL.Method) (pc : Nat) + (frame : Frame) (stack : List Value) (memory : Memory) : + modelStep model body pc frame stack memory = + FrameAction.relabel (fun _ => pc + 1) <$> modelStep model body 0 frame stack memory := by + unfold modelStep observedCall runModel intrinsicCall featureCall referenceCompareCall + referenceSubtractCall typedTransferCall skipInitCall spanInPlaceCall + repeat' first + | rfl + | (simp only [map_bind, map_pure, FrameAction.relabel]; done) + | (apply bind_congr; intro) + | split + | simp only [map_bind] + +#print axioms modelStep_at + +/-- A runtime model that continues at one pc continues alike at another, in a body of the same + profile. -/ +theorem modelStep_move {model : Option CIL.RuntimeModel} {moved body : CIL.Method} {pc q : Nat} + {frame changed : Frame} {stack values : List Value} {memory final : Memory} + (profile : moved.profile = body.profile) + (executed : modelStep model body pc frame stack memory = + .ok (.next (pc + 1) values changed final)) : + modelStep model moved q frame stack memory = .ok (.next (q + 1) values changed final) := by + rw [modelStep_profile_congr profile, modelStep_at (pc := q)] + rw [modelStep_at (pc := pc)] at executed + cases zero : modelStep model body 0 frame stack memory with + | error fault => simp [zero, Functor.map, Except.map] at executed + | ok action => + simp only [zero, Functor.map, Except.map, Except.ok.injEq] at executed ⊢ + cases action <;> simp_all [FrameAction.relabel] + +#print axioms modelStep_move + +/-- A runtime model that faults at one pc faults alike at another, in a body of the same + profile. -/ +theorem modelStep_move_error {model : Option CIL.RuntimeModel} {moved body : CIL.Method} + {pc q : Nat} {frame : Frame} {stack : List Value} {memory : Memory} {fault : ExecutionFault} + (profile : moved.profile = body.profile) + (executed : modelStep model body pc frame stack memory = .error fault) : + modelStep model moved q frame stack memory = .error fault := by + rw [modelStep_profile_congr profile, modelStep_at (pc := q)] + rw [modelStep_at (pc := pc)] at executed + cases zero : modelStep model body 0 frame stack memory with + | error other => simp_all [Functor.map, Except.map] + | ok action => simp [zero, Functor.map, Except.map] at executed + +#print axioms modelStep_move_error + /-- What one expected step at `pc` and its actual counterpart at `q` must share. -/ -structure StepContext (expected actual : CIL.Method) (r : Nat → Nat) (op : CIL.Op) (pc q : Nat) : +structure StepContext (expected actual : CIL.Method) (r : Nat → Nat) (op : CIL.Instruction) + (pc q : Nat) : Prop where profile : actual.profile = expected.profile returnsValue : actual.returnsValue = expected.returnsValue - statics : staticAt actual q = staticAt expected pc + declared : actual.declared = expected.declared + environment : actual.environment = expected.environment + arguments : actual.argumentAccess = expected.argumentAccess + statics : actual.staticAt q = expected.staticAt pc + link : actual.linkAt q = expected.linkAt pc next : op.continues = true → r (pc + 1) = q + 1 /-- A successful expected step is matched by the relabeled step at the renamed pc. -/ -def StepRelabel (op : CIL.Op) : Prop := +def StepRelabel (op : CIL.Instruction) : Prop := ∀ (expected actual : CIL.Method) (r : Nat → Nat) (pc q : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction), StepContext expected actual r op pc q → @@ -77,24 +96,32 @@ def StepRelabel (op : CIL.Op) : Prop := /-- Static-memory instructions at two pcs that resolve the same descriptor behave alike. -/ theorem staticInstruction_congr {expected actual : CIL.Method} {pc q : Nat} - (same : staticAt actual q = staticAt expected pc) (operation : CIL.MemoryOp) + (_same : actual.staticAt q = expected.staticAt pc) (operation : CIL.MemoryOp) (stack : List Value) (m : Memory) : staticInstruction actual q operation stack m = - staticInstruction expected pc operation stack m := by - unfold staticAt at same - cases operation - case staticAddress bytes | staticValue bytes => - simp only [staticInstruction] - cases h1 : actual.staticSites.find? (fun site => site.1 == q) <;> - cases h2 : expected.staticSites.find? (fun site => site.1 == pc) <;> simp_all - case spanCreate | createSpan => - unfold staticInstruction - split <;> first | contradiction | rfl - all_goals unfold staticInstruction; rfl + staticInstruction expected pc operation stack m := rfl + +/-- `ldsfld` at two pcs that resolve the same descriptor loads the same value. -/ +theorem staticLoad_congr {expected actual : CIL.Method} {pc q : Nat} + (same : actual.staticAt q = expected.staticAt pc) (memory : Memory) : + staticLoad actual q memory = staticLoad expected pc memory := by + unfold staticLoad + rw [same] + +#print axioms staticLoad_congr + +/-- `ldsflda` at two pcs that resolve the same descriptor gives the same storage. -/ +theorem staticAddress_congr {expected actual : CIL.Method} {pc q : Nat} + (same : actual.staticAt q = expected.staticAt pc) (memory : Memory) : + staticAddress actual q memory = staticAddress expected pc memory := by + unfold staticAddress + rw [same] + +#print axioms staticAddress_congr /-- The shared tail of `step` for instructions without a dedicated case. -/ theorem fallback_relabel {r : Nat → Nat} {pc q : Nat} {profile : FeatureProfile} - {stack : List Value} {frame : Frame} {m : Memory} {action : FrameAction} (op : CIL.Op) + {stack : List Value} {frame : Frame} {m : Memory} {action : FrameAction} (op : CIL.Instruction) (arity : pureArity (op.relabel r) = pureArity op) (base : pureArity op ≠ none → BaseRelabel op) (plain : pureArity op = none → ∀ result, instruction op stack m = .ok result → @@ -105,7 +132,7 @@ theorem fallback_relabel {r : Nat → Nat} {pc q : Nat} {profile : FeatureProfil if stack.length < argc then Except.error ExecutionFault.invalidState else do let values ← scalars (List.take argc stack) - match CIL.step op false pc [] 0 values (fun _ => none) profile with + match CIL.step op false none pc values (fun _ => none) profile with | some (CIL.Action.next target result _) => do let result ← List.mapM (checkedValue m) (List.map Value.scalar result) Pure.pure (FrameAction.next target (result ++ List.drop argc stack) frame m) @@ -118,7 +145,7 @@ theorem fallback_relabel {r : Nat → Nat} {pc q : Nat} {profile : FeatureProfil if stack.length < argc then Except.error ExecutionFault.invalidState else do let values ← scalars (List.take argc stack) - match CIL.step (op.relabel r) false q [] 0 values (fun _ => none) profile with + match CIL.step (op.relabel r) false none q values (fun _ => none) profile with | some (CIL.Action.next target result _) => do let result ← List.mapM (checkedValue m) (List.map Value.scalar result) Pure.pure (FrameAction.next target (result ++ List.drop argc stack) frame m) @@ -174,7 +201,7 @@ private theorem observer_ok {observed : Bool} {result : ExecutionResult FrameAct /-- A step that throws passed the unspecified-bits guard. -/ private theorem observer_thrown {observed : Bool} {result : ExecutionResult FrameAction} - {exception : String} + {exception : ExceptionType} (h : (if observed then .error (.unsupported "Instruction observes unspecified vector bits") else result) = .error (.thrown exception)) : observed = false ∧ result = .error (.thrown exception) := by @@ -200,7 +227,7 @@ set_option hygiene false in macro "step_relabel_fallback" base:term:max plain:term:max : tactic => `(tactic| ( intro expected actual r pc q args frame stack m action context h unfold step at h ⊢ - dsimp only [CIL.Op.relabel] at h ⊢ + dsimp only [CIL.Instruction.relabel] at h ⊢ step_observers rw [context.profile] refine fallback_relabel (profile := expected.profile) _ rfl ?_ ?_ context.next h @@ -229,8 +256,8 @@ set_option hygiene false in /-- Introduces a `StepRelabel` or `StepRaise` goal, unfolds both steps and clears the guard. -/ macro "explicit_intro" : tactic => `(tactic| ( intro expected actual r pc q args frame stack m action context h - unfold step at h ⊢ - dsimp only [CIL.Op.relabel] at h ⊢ + unfold step referenceCompareCall referenceSubtractCall at h ⊢ + dsimp only [CIL.Instruction.relabel] at h ⊢ step_observers)) theorem mapError_ok {x : Except ε α} {f : ε → ε'} {a : α} (h : x.mapError f = .ok a) : @@ -244,16 +271,16 @@ set_option hygiene false in before examining the second operand. -/ local macro "stack_cases" " => " close:tacticSeq : tactic => `(tactic| ( intro expected actual r pc q args frame stack m action context h - unfold step at h ⊢ - dsimp only [CIL.Op.relabel] at h ⊢ + unfold step referenceCompareCall referenceSubtractCall at h ⊢ + dsimp only [CIL.Instruction.relabel] at h ⊢ step_observers rcases stack with _ | ⟨v1, rest⟩ <;> (try cases v1) - all_goals (try dsimp only [CIL.Op.relabel] at h ⊢) + all_goals (try dsimp only [CIL.Instruction.relabel] at h ⊢) all_goals (try rw [context.profile]) all_goals first | (($close); done) | (rcases rest with _ | ⟨v2, rest⟩ <;> (try cases v2) - all_goals (try dsimp only [CIL.Op.relabel] at h ⊢) + all_goals (try dsimp only [CIL.Instruction.relabel] at h ⊢) all_goals (try rw [context.profile]) all_goals ($close)))) @@ -269,11 +296,21 @@ set_option hygiene false in macro "plain_cases" : tactic => `(tactic| ( intro expected actual r pc q args frame stack m action context h unfold step at h ⊢ - dsimp only [CIL.Op.relabel] at h ⊢ + dsimp only [CIL.Instruction.relabel] at h ⊢ step_observers (try rw [context.profile]) (try rw [context.returnsValue]))) +set_option hygiene false in +/-- Reads the call site's link in both steps through the context and splits on it: a link of + another kind faults alike in both, and the matching one is substituted in the goal. -/ +macro "link_cases" : tactic => `(tactic| ( + rw [context.link] + split at h + rotate_left + · first | cases h | (simp [unlinkedCall] at h) + try simp only [‹expected.linkAt pc = _›])) + set_option hygiene false in /-- Splits an optional lookup shared by both steps. -/ macro "lookup_lockstep" : tactic => `(tactic| ( @@ -285,218 +322,255 @@ macro "lookup_lockstep" : tactic => `(tactic| ( -- Instructions with a dedicated case in `step`. -theorem step_relabel_nativeBinary (operation : Op) : StepRelabel (.nativeBinary operation) := by - stack_cases => - first - | fallback_vacuous - | (obtain ⟨⟨target, values⟩, found, h⟩ := except_bind_ok h - have native := nativeBinaryValues_relabel r operation pc q _ _ (context.next rfl) target - values (mapError_ok found) - simp only [native, Except.mapError, ok_bind_eq] at h ⊢ - lockstep) +/-- Division, remainder and overflow-checked arithmetic step in lockstep with their relabeling. -/ +theorem step_relabel_faulting (op : Instruction) + (faulting : op.divideKind?.isSome || op.checkedKind?.isSome) : StepRelabel op := by + cases op <;> simp only [Instruction.divideKind?, Instruction.checkedKind?, Option.isSome_none, + Option.isSome_some, Bool.or_false, Bool.false_or, Bool.false_eq_true] at faulting + -- One goal per arm of the dispatcher's first match, so its cost does not grow with the values. + all_goals + explicit_intro + (try rw [context.profile]) + split at h + all_goals first | contradiction | lockstep | fallback_vacuous -theorem step_relabel_divide (signed remainder : Bool) : StepRelabel (.divide signed remainder) := by - stack_cases => - first | fallback_vacuous | lockstep +#print axioms step_relabel_faulting -theorem step_relabel_checkedArithmetic (kind : CheckedBinaryKind) (signed : Bool) : - StepRelabel (.checkedArithmetic kind signed) := by +/-- A successful checked conversion of a signed operand is matched by the relabeled step. -/ +theorem step_relabel_conv_ovf (target : ConvTarget) : StepRelabel (.conv_ovf target) := by stack_cases => first | fallback_vacuous | lockstep -theorem step_relabel_checkedConvert (width : Nat) (signed unsignedInput : Bool) : - StepRelabel (.checkedConvert width signed unsignedInput) := by +#print axioms step_relabel_conv_ovf + +/-- A successful checked conversion of an unsigned operand is matched by the relabeled step. -/ +theorem step_relabel_conv_ovf_un (target : ConvTarget) : StepRelabel (.conv_ovf_un target) := by stack_cases => first | fallback_vacuous | lockstep -theorem step_relabel_arg (index : Nat) (narrow : Option NarrowKind) : - StepRelabel (.arg index narrow) := by - plain_cases - cases narrow <;> dsimp only at h ⊢ <;> lookup_lockstep +#print axioms step_relabel_conv_ovf_un -theorem step_relabel_vectorArg (index : Nat) : StepRelabel (.vectorArg index) := by - explicit_intro +/-- An argument load reads as `Method.argumentAccess` says, which both methods share. -/ +theorem step_relabel_ldarg (index : Nat) : StepRelabel (.ldarg index) := by + plain_cases + rw [show loadArgument actual index args frame m = loadArgument expected index args frame m by + simp only [loadArgument, context.arguments]] lockstep -theorem step_relabel_vectorArgAddr (index : Nat) : StepRelabel (.vectorArgAddr index) := by - explicit_intro - lockstep +#print axioms step_relabel_ldarg -theorem step_relabel_local (index : Nat) (narrow : Option NarrowKind) : - StepRelabel (.local index narrow) := by +/-- An argument store writes the home `Method.argumentAccess` gives, which both methods share. -/ +theorem step_relabel_starg (index : Nat) : StepRelabel (.starg index) := by + stack_cases => + first + | fallback_vacuous + | (rw [show ∀ value, storeArgument actual index value frame m = + storeArgument expected index value frame m by + intro value + simp only [storeArgument, context.arguments]] + lockstep) +#print axioms step_relabel_starg + +/-- An argument address is the home `Method.argumentAccess` gives, which both methods share. -/ +theorem step_relabel_ldarga (index : Nat) : StepRelabel (.ldarga index) := by plain_cases - lookup_lockstep + rw [show addressArgument actual index frame m = addressArgument expected index frame m by + simp only [addressArgument, context.arguments]] + lockstep +#print axioms step_relabel_ldarga -theorem step_relabel_vectorLocal (index : Nat) : StepRelabel (.vectorLocal index) := by +theorem step_relabel_ldloc (index : Nat) : StepRelabel (.ldloc index) := by plain_cases lookup_lockstep -theorem step_relabel_vectorLocalAddr (index : Nat) : StepRelabel (.vectorLocalAddr index) := by +theorem step_relabel_ldloca (index : Nat) : StepRelabel (.ldloca index) := by plain_cases lookup_lockstep -theorem step_relabel_setVectorLocal (index : Nat) : StepRelabel (.setVectorLocal index) := by +theorem step_relabel_stloc (index : Nat) : StepRelabel (.stloc index) := by stack_cases => first | fallback_vacuous | lookup_lockstep -theorem step_relabel_localAddr (index : Nat) : StepRelabel (.localAddr index) := by +/-- A call relocates no pc; a tail call has no continuation and an ordinary one returns to the + pc after it. -/ +theorem step_relabel_call (method : CIL.Extraction.MethodRef) (prefixes : CallPrefixes) : + StepRelabel (.call method prefixes) := by plain_cases - lookup_lockstep + rw [context.link] + split at h + · rename_i unlinked + try simp only [unlinked] + by_cases tail : prefixes.tail = true + · simp [tail] at h + · simp only [tail, Bool.false_eq_true, ↓reduceIte] at h ⊢ + rw [context.environment] + obtain ⟨values, final, changed, rfl, _⟩ := modelStep_frame h + simp only [FrameAction.relabel] + rw [context.next (by simp [CIL.Instruction.continues, tail])] + exact modelStep_move context.profile h + · rename_i link linked + try simp only [linked] + dsimp only [observedCall] at h ⊢ + obtain ⟨observed, h⟩ := observer_ok h + simp only [observed, Bool.false_eq_true, ↓reduceIte] + cases link <;> try cases h + by_cases tail : prefixes.tail = true + · by_cases short : (stack.length != method.argumentCount) = true + · simp [tail, short] at h + · simp only [tail, short, ↓reduceIte] at h ⊢ + lockstep + · by_cases short : stack.length < method.argumentCount + · simp [tail, short] at h + · simp only [tail, short, ↓reduceIte, Bool.false_eq_true] at h ⊢ + lockstep + +#print axioms step_relabel_call + +/-- A receiver call uses the same direct body or virtual table at the relocated pc. -/ +theorem step_relabel_callvirt (method : CIL.Extraction.MethodRef) : + StepRelabel (.callvirt method) := by + plain_cases + rw [context.link] + split at h <;> try cases h + all_goals try simp only [‹expected.linkAt pc = _›] + all_goals + by_cases short : stack.length < method.argumentCount + · simp [short] at h + · simp only [short, ↓reduceIte] at h ⊢ + obtain ⟨arguments, found, h⟩ := except_bind_ok h + simp only [found, ok_bind_eq] + split at h + all_goals first + | (obtain ⟨callee, selected, h⟩ := except_bind_ok h + simp only [selected, ok_bind_eq] + cases h + rfl) + | (cases h <;> rfl) -theorem step_relabel_setLocal (index : Nat) (narrow : Option NarrowKind) : - StepRelabel (.setLocal index narrow) := by - stack_cases => - first | fallback_vacuous | lookup_lockstep +#print axioms step_relabel_callvirt + +/-- Both constructions enter the same linked body or continue after the same runtime model. -/ +theorem step_relabel_newobj (constructor : Extraction.MethodRef) : + StepRelabel (.newobj constructor) := by -/-- Span construction and its typed local store retain the original instruction boundary. -/ -theorem step_relabel_initializeSpanLocal (index : Nat) (constructor : SpanConstructor) : - StepRelabel (.initializeSpanLocal index constructor) := by plain_cases - split at h - · rename_i found - try simp only [found] - obtain ⟨⟨memory, values⟩, constructed, h⟩ := except_bind_ok h - simp only [constructed, ok_bind_eq] - cases values with - | nil => cases h - | cons value rest => lockstep - · cases h + unfold newobjStep constructAggregate constructObject at h ⊢ + rw [context.link] + rcases linked : expected.linkAt pc with _ | link + · simp only [linked] at h + rw [context.environment] + obtain ⟨values, final, changed, rfl, _⟩ := modelStep_frame h + simp only [FrameAction.relabel] + rw [context.next (by simp [CIL.Instruction.continues])] + exact modelStep_move context.profile h + simp only [linked] at h ⊢ + cases link + case aggregate callee size => + by_cases short : (size = 0 || stack.length < constructor.parameters.length) = true + · simp only [short, ↓reduceIte] at h; cases h + · simp only [short, ↓reduceIte, Bool.false_eq_true] at h ⊢ + lockstep + case object callee bases size => + by_cases short : stack.length < constructor.parameters.length + · simp [short] at h + · simp only [short, ↓reduceIte] at h ⊢ + lockstep + all_goals first | cases h | (simp [unlinkedCall] at h) -#print axioms step_relabel_initializeSpanLocal +#print axioms step_relabel_newobj -theorem step_relabel_call (callee argc : Nat) : StepRelabel (.call callee argc) := by +theorem step_relabel_ldsfld (field : Extraction.FieldRef) : StepRelabel (.ldsfld field) := by plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - lockstep + rw [staticLoad_congr context.statics] + lockstep -/-- A receiver-checked call relocates no pc; its null receiver raises rather than calls. -/ -theorem step_relabel_callInstance (callee argc : Nat) : - StepRelabel (.callInstance callee argc) := by - plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - obtain ⟨arguments, found, h⟩ := except_bind_ok h - simp only [found, ok_bind_eq] - split at h <;> cases h - rfl - -#print axioms step_relabel_callInstance - -/-- A virtual call relocates no pc; its body is selected from memory, which relabeling keeps. -/ -theorem step_relabel_callVirtual (targets : List (CIL.Extraction.TypeExpr × Nat)) (argc : Nat) : - StepRelabel (.callVirtual targets argc) := by +#print axioms step_relabel_ldsfld + +theorem step_relabel_ldsflda (field : Extraction.FieldRef) : StepRelabel (.ldsflda field) := by plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - obtain ⟨arguments, found, h⟩ := except_bind_ok h - simp only [found, ok_bind_eq] - split at h - · obtain ⟨callee, selected, h⟩ := except_bind_ok h - simp only [selected, ok_bind_eq] - cases h - rfl - all_goals cases h + rw [staticAddress_congr context.statics] + lockstep -#print axioms step_relabel_callVirtual +#print axioms step_relabel_ldsflda -/-- A tail transfer has no continuation pc to relocate. -/ -theorem step_relabel_tailCall (callee argc : Nat) : StepRelabel (.tailCall callee argc) := by +/-- A typed element access reads its element's shape from the layouts, which relabeling keeps. -/ +theorem step_relabel_ldelem_any (element : Extraction.TypeExpr) : + StepRelabel (.ldelem_any element) := by plain_cases - by_cases short : (stack.length != argc) = true - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - lockstep + simp only [CIL.Method.elementShape, context.environment] at h ⊢ + dsimp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢ + repeat (obtain ⟨_, found, h⟩ := except_bind_ok h; simp only [found]) + cases h + simp only [FrameAction.relabel] + rw [context.next rfl] -#print axioms step_relabel_tailCall +#print axioms step_relabel_ldelem_any -theorem step_relabel_newVector256 (callee argc : Nat) : - StepRelabel (.newVector256 callee argc) := by +/-- A typed element store reads its element's shape from the layouts, which relabeling keeps. -/ +theorem step_relabel_stelem_any (element : Extraction.TypeExpr) : + StepRelabel (.stelem_any element) := by plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - lockstep + simp only [CIL.Method.elementShape, context.environment] at h ⊢ + dsimp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢ + repeat (obtain ⟨_, found, h⟩ := except_bind_ok h; simp only [found]) + cases h + simp only [FrameAction.relabel] + rw [context.next rfl] -theorem step_relabel_newAggregate (callee argc : Nat) (size : Fin (2^64)) : - StepRelabel (.newAggregate callee argc size) := by - plain_cases - by_cases short : (size = 0 || stack.length < argc) = true - · simp only [short, ↓reduceIte] at h; cases h - · simp only [short, ↓reduceIte, Bool.false_eq_true] at h ⊢ - lockstep +#print axioms step_relabel_stelem_any -theorem step_relabel_newObject (callee argc : Nat) (owner : Extraction.TypeExpr) - (bases : List Extraction.TypeExpr) (size : Nat) : - StepRelabel (.newObject callee argc owner bases size) := by +/-- An element address reads its element's shape from the layouts, which relabeling keeps. -/ +theorem step_relabel_ldelema (element : Extraction.TypeExpr) (readOnly : Bool) : + StepRelabel (.ldelema element readOnly) := by plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - lockstep + simp only [CIL.Method.elementShape, context.environment] at h ⊢ + dsimp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢ + repeat (obtain ⟨_, found, h⟩ := except_bind_ok h; simp only [found]) + cases h + simp only [FrameAction.relabel] + rw [context.next rfl] -theorem step_relabel_intrinsic (operation : Intrinsic) (argc : Nat) : - StepRelabel (.intrinsic operation argc) := by - explicit_intro - rw [context.profile] - by_cases avail : (!operation.available expected.profile) = true - · simp [avail] at h - · simp only [avail, ↓reduceIte, Bool.false_eq_true] at h ⊢ - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - lockstep +#print axioms step_relabel_ldelema + +theorem step_relabel_ldtoken (operand : TokenOperand) : StepRelabel (.ldtoken operand) := by + cases operand <;> plain_cases + case type => + cases h + simp only [FrameAction.relabel, context.next rfl] + case field => rw [staticAddress_congr context.statics]; lockstep + +#print axioms step_relabel_ldtoken -theorem step_relabel_memory (operation : MemoryOp) : StepRelabel (.memory operation) := by +theorem step_relabel_stsfld (field : Extraction.FieldRef) : StepRelabel (.stsfld field) := by plain_cases - rw [staticInstruction_congr context.statics] - lockstep + cases h + +#print axioms step_relabel_stsfld -theorem step_relabel_ret (narrow : Option NarrowKind) : StepRelabel (.ret narrow) := by +theorem step_relabel_ret : StepRelabel .ret := by intro expected actual r pc q args frame stack m action context h unfold step at h ⊢ - dsimp only [CIL.Op.relabel] at h ⊢ + dsimp only [CIL.Instruction.relabel] at h ⊢ step_observers - cases narrow - · dsimp only [CIL.Op.relabel] at h ⊢ - rw [context.returnsValue] - by_cases sized : stack.length = (if expected.returnsValue then 1 else 0) - · rw [ite_eq_left sized] at h ⊢ - lockstep - · rw [ite_eq_right sized] at h - cases h - · rcases stack with _ | ⟨v, rest⟩ - · dsimp only [CIL.Op.relabel] at h ⊢ - fallback_vacuous - · cases v with - | scalar value => - cases value <;> cases rest - all_goals (try dsimp only [CIL.Op.relabel] at h ⊢) - all_goals (try rw [context.returnsValue]) - all_goals first - | fallback_vacuous - | (by_cases returns : expected.returnsValue = true - · simp only [returns, ↓reduceIte] at h ⊢ - lockstep - · simp only [returns, ↓reduceIte, Bool.false_eq_true] at h - cases h) - | reference reference => - cases rest <;> fallback_vacuous - | span reference length => - cases rest <;> fallback_vacuous - | bufferSpan _ | readOnlyReference _ | array _ | methodPointer _ | methodTable _ - | object _ => - cases rest <;> fallback_vacuous - -theorem step_relabel_unsupported (description : String) : - StepRelabel (.unsupported description) := by - plain_cases - fallback_vacuous + rw [context.returnsValue, show actual.returnNarrow = expected.returnNarrow from by + simp only [CIL.Method.returnNarrow, context.returnsValue, context.declared, + context.environment]] + have returned : ∃ values memory, action = .returned values memory := by + unfold returnFrom at h + repeat' first + | split at h + | (obtain ⟨_, _, h⟩ := except_bind_ok h) + | (cases h <;> exact ⟨_, _, rfl⟩) + obtain ⟨values, memory, rfl⟩ := returned + exact h + +#print axioms step_relabel_ret -theorem step_relabel_throw (exception : String) : StepRelabel (.throw exception) := by +theorem step_relabel_unadmitted (opcode : Nat) : StepRelabel (.unadmitted opcode) := by + explicit_intro + cases h + +theorem step_relabel_throw (exception : ExceptionType) : StepRelabel (.throw exception) := by explicit_intro cases h @@ -517,7 +591,7 @@ elab "base_relabels" : tactic => withMainContext do replaceMainGoal [] /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_eq : StepRelabel .eq := by +theorem step_relabel_ceq : StepRelabel .ceq := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -526,10 +600,10 @@ theorem step_relabel_eq : StepRelabel .eq := by cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_eq +#print axioms step_relabel_ceq /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_brzero (target : Nat) : StepRelabel (.brzero target) := by +theorem step_relabel_brfalse (target : Nat) : StepRelabel (.brfalse target) := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -538,10 +612,10 @@ theorem step_relabel_brzero (target : Nat) : StepRelabel (.brzero target) := by cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_brzero +#print axioms step_relabel_brfalse /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_brnonzero (target : Nat) : StepRelabel (.brnonzero target) := by +theorem step_relabel_brtrue (target : Nat) : StepRelabel (.brtrue target) := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -550,11 +624,25 @@ theorem step_relabel_brnonzero (target : Nat) : StepRelabel (.brnonzero target) cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_brnonzero +#print axioms step_relabel_brtrue + +/-- A cast continues at the next pc with the tested reference. -/ +theorem step_relabel_castclass (type : CIL.Extraction.TypeExpr) : + StepRelabel (.castclass type) := by + stack_cases => + first + | exact fallback_relabel (profile := expected.profile) _ rfl + (fun absent => absurd rfl absent) (fun _ _ ran => ⟨ran, context.next rfl⟩) + context.next h + | (repeat (obtain ⟨_, found, h⟩ := except_bind_ok h; simp only [found, ok_bind_eq]) + cases h + simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) + +#print axioms step_relabel_castclass /-- A type test continues at the next pc with the tested reference or null. -/ -theorem step_relabel_objectTest (type : CIL.Extraction.TypeExpr) (throwing : Bool) : - StepRelabel (.objectTest type throwing) := by +theorem step_relabel_isinst (type : CIL.Extraction.TypeExpr) : + StepRelabel (.isinst type) := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -564,7 +652,35 @@ theorem step_relabel_objectTest (type : CIL.Extraction.TypeExpr) (throwing : Boo cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_objectTest +#print axioms step_relabel_isinst + +/-- An instance field load resolves its field in the layout environment both methods share. -/ +theorem step_relabel_ldfld (field : CIL.Extraction.FieldRef) : StepRelabel (.ldfld field) := by + plain_cases + rw [show fieldLoad actual field stack m = fieldLoad expected field stack m by + simp only [fieldLoad, CIL.Method.fieldShape, context.environment]] + lockstep + +#print axioms step_relabel_ldfld + +/-- An instance field store resolves its field in the layout environment both methods share. -/ +theorem step_relabel_stfld (field : CIL.Extraction.FieldRef) : StepRelabel (.stfld field) := by + plain_cases + rw [show fieldStore actual field stack m = fieldStore expected field stack m by + simp only [fieldStore, CIL.Method.fieldShape, context.environment]] + lockstep + +#print axioms step_relabel_stfld + +/-- An instance field address resolves its field in the layout environment both methods + share. -/ +theorem step_relabel_ldflda (field : CIL.Extraction.FieldRef) : StepRelabel (.ldflda field) := by + plain_cases + rw [show fieldReference actual field stack m = fieldReference expected field stack m by + simp only [fieldReference, CIL.Method.fieldShape, context.environment]] + lockstep + +#print axioms step_relabel_ldflda /-- Relabel scalar or reference observations while preserving their result. -/ theorem step_relabel_beq (target : Nat) : StepRelabel (.beq target) := by @@ -579,7 +695,7 @@ theorem step_relabel_beq (target : Nat) : StepRelabel (.beq target) := by #print axioms step_relabel_beq /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_bne (target : Nat) : StepRelabel (.bne target) := by +theorem step_relabel_bne_un (target : Nat) : StepRelabel (.bne_un target) := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -588,7 +704,24 @@ theorem step_relabel_bne (target : Nat) : StepRelabel (.bne target) := by cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_bne +#print axioms step_relabel_bne_un + +/-- Relabel scalar, reference or native table operands while preserving their result. -/ +theorem step_relabel_add : StepRelabel .add := by + stack_cases => + first + | exact fallback_relabel (profile := expected.profile) _ rfl + (fun _ => by base_relabels) (by intro absent; contradiction) context.next h + | (repeat (obtain ⟨_, found, h⟩ := except_bind_ok h; simp only [found, ok_bind_eq]) + cases h + simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) + | (split at h + all_goals repeat + (obtain ⟨_, found, h⟩ := except_bind_ok h; simp only [found, ok_bind_eq]) + all_goals cases h + all_goals simp only [FrameAction.relabel, context.next rfl, Pure.pure, Except.pure]) + +#print axioms step_relabel_add /-- Relabel scalar or reference observations while preserving their result. -/ theorem step_relabel_sub : StepRelabel .sub := by @@ -603,7 +736,7 @@ theorem step_relabel_sub : StepRelabel .sub := by #print axioms step_relabel_sub /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_ltu : StepRelabel .ltu := by +theorem step_relabel_clt_un : StepRelabel .clt_un := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -612,10 +745,10 @@ theorem step_relabel_ltu : StepRelabel .ltu := by cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_ltu +#print axioms step_relabel_clt_un /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_gtu : StepRelabel .gtu := by +theorem step_relabel_cgt_un : StepRelabel .cgt_un := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -624,10 +757,10 @@ theorem step_relabel_gtu : StepRelabel .gtu := by cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_gtu +#print axioms step_relabel_cgt_un /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_bltu (target : Nat) : StepRelabel (.bltu target) := by +theorem step_relabel_blt_un (target : Nat) : StepRelabel (.blt_un target) := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -636,10 +769,10 @@ theorem step_relabel_bltu (target : Nat) : StepRelabel (.bltu target) := by cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_bltu +#print axioms step_relabel_blt_un /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_bgeu (target : Nat) : StepRelabel (.bgeu target) := by +theorem step_relabel_bge_un (target : Nat) : StepRelabel (.bge_un target) := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -648,10 +781,10 @@ theorem step_relabel_bgeu (target : Nat) : StepRelabel (.bgeu target) := by cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_bgeu +#print axioms step_relabel_bge_un /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_bgtu (target : Nat) : StepRelabel (.bgtu target) := by +theorem step_relabel_bgt_un (target : Nat) : StepRelabel (.bgt_un target) := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -660,10 +793,10 @@ theorem step_relabel_bgtu (target : Nat) : StepRelabel (.bgtu target) := by cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_bgtu +#print axioms step_relabel_bgt_un /-- Relabel scalar or reference observations while preserving their result. -/ -theorem step_relabel_bleu (target : Nat) : StepRelabel (.bleu target) := by +theorem step_relabel_ble_un (target : Nat) : StepRelabel (.ble_un target) := by stack_cases => first | exact fallback_relabel (profile := expected.profile) _ rfl @@ -672,158 +805,159 @@ theorem step_relabel_bleu (target : Nat) : StepRelabel (.bleu target) := by cases h simp only [FrameAction.relabel, apply_ite, context.next rfl, Pure.pure, Except.pure]) -#print axioms step_relabel_bleu +#print axioms step_relabel_ble_un set_option maxHeartbeats 300000 in /-- Every instruction steps in lockstep with its relabeled form. Pure operations share the base-step contract; ordinary memory operations share an unchanged instruction result. Dedicated dispatcher cases retain their own proofs. -/ -theorem step_relabel (op : CIL.Op) : StepRelabel op := by +theorem step_relabel (op : CIL.Instruction) : StepRelabel op := by cases op - case managed operation => - cases operation + case calli => all_goals plain_cases - all_goals simp only [managedStep] at h ⊢ + all_goals rw [context.link] + all_goals simp only [calliStep] at h ⊢ all_goals repeat' first | contradiction - | solve | simp only [FrameAction.relabel, context.next rfl] + | solve | simp only [FrameAction.relabel] | solve | lockstep | cases h - | (split at h <;> try simp only [*, Bool.false_eq_true, Bool.true_eq, ite_true, ite_false, - Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢) - case stackAlloc | newArray | newReferenceArray => + | (split at h <;> try simp only [*, Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢) + case localloc => + plain_cases + rw [context.declared] + unfold localAllocation at h ⊢ + split at h + · split at h <;> first | lockstep | cases h + · cases h + case newarr => stack_cases => first | fallback_vacuous - | (cases ‹CIL.Value› - all_goals dsimp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢ - all_goals try cases h - all_goals repeat (obtain ⟨_, found, h⟩ := except_bind_ok h; simp only [found]) - all_goals cases h - all_goals simp only [FrameAction.relabel] - all_goals rw [context.next rfl]) - case nativeBinary operation => exact step_relabel_nativeBinary operation - case divide signed remainder => exact step_relabel_divide signed remainder - case checkedArithmetic kind signed => exact step_relabel_checkedArithmetic kind signed - case checkedConvert width signed unsignedInput => - exact step_relabel_checkedConvert width signed unsignedInput - case arg index narrow => exact step_relabel_arg index narrow - case vectorArg index => exact step_relabel_vectorArg index - case vectorArgAddr index => exact step_relabel_vectorArgAddr index - case «local» index narrow => exact step_relabel_local index narrow - case vectorLocal index => exact step_relabel_vectorLocal index - case vectorLocalAddr index => exact step_relabel_vectorLocalAddr index - case setVectorLocal index => exact step_relabel_setVectorLocal index - case initializeSpanLocal index constructor => - exact step_relabel_initializeSpanLocal index constructor - case localAddr index => exact step_relabel_localAddr index - case setLocal index narrow => exact step_relabel_setLocal index narrow - case call method argc => exact step_relabel_call method argc - case callInstance method argc => exact step_relabel_callInstance method argc - case callVirtual targets argc => exact step_relabel_callVirtual targets argc - case tailCall method argc => exact step_relabel_tailCall method argc - case newVector256 constructor argc => exact step_relabel_newVector256 constructor argc - case newAggregate constructor argc size => exact step_relabel_newAggregate constructor argc size - case newObject constructor argc owner bases size => - exact step_relabel_newObject constructor argc owner bases size - case intrinsic operation argc => exact step_relabel_intrinsic operation argc - case memory operation => exact step_relabel_memory operation - case ret narrow => exact step_relabel_ret narrow + | (simp only [CIL.Method.elementShape, context.environment] at h ⊢ + dsimp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢ + repeat (obtain ⟨_, found, h⟩ := except_bind_ok h; simp only [found]) + cases h + simp only [FrameAction.relabel] + rw [context.next rfl]) + case div | div_un | rem | rem_un | add_ovf | add_ovf_un | sub_ovf | sub_ovf_un | mul_ovf + | mul_ovf_un => exact step_relabel_faulting _ rfl + case conv_ovf target => exact step_relabel_conv_ovf target + case conv_ovf_un target => exact step_relabel_conv_ovf_un target + case ldarg index => exact step_relabel_ldarg index + case starg index => exact step_relabel_starg index + case ldarga index => exact step_relabel_ldarga index + case ldloc index => exact step_relabel_ldloc index + case ldloca index => exact step_relabel_ldloca index + case stloc index => exact step_relabel_stloc index + case call method prefixes => exact step_relabel_call method prefixes + case callvirt method => exact step_relabel_callvirt method + case newobj constructor => exact step_relabel_newobj constructor + case cpblk | initblk | ldftn | ldstr => plain_cases; lockstep + case ldsfld field => exact step_relabel_ldsfld field + case ldsflda field => exact step_relabel_ldsflda field + case ldobj | stobj | cpobj | sizeof => + plain_cases + simp only [Method.valueShape, context.environment] at h ⊢ + first | lockstep | lookup_lockstep + case initobj type => + plain_cases + simp only [Method.valueShape, context.environment] at h ⊢ + generalize expected.environment.valueShape type = shape at h ⊢ + cases shape <;> simp only [ValueShape.size?] at h ⊢ + case span => + unfold initializeSpanHome at h ⊢ + split at h + · lookup_lockstep + · cases h + all_goals first | lockstep | cases h + case ldelem_any element => exact step_relabel_ldelem_any element + case stelem_any element => exact step_relabel_stelem_any element + case ldelema element readOnly => exact step_relabel_ldelema element readOnly + case ldtoken operand => exact step_relabel_ldtoken operand + case stsfld field => exact step_relabel_stsfld field + case ret => exact step_relabel_ret case throw exception => exact step_relabel_throw exception - case unsupported description => exact step_relabel_unsupported description + case unadmitted opcode => exact step_relabel_unadmitted opcode + case add => exact step_relabel_add case sub => exact step_relabel_sub - case ltu => exact step_relabel_ltu - case gtu => exact step_relabel_gtu - case bltu target => exact step_relabel_bltu target - case bgeu target => exact step_relabel_bgeu target - case bgtu target => exact step_relabel_bgtu target - case bleu target => exact step_relabel_bleu target - case eq => exact step_relabel_eq - case brzero target => exact step_relabel_brzero target - case brnonzero target => exact step_relabel_brnonzero target - case objectTest type throwing => exact step_relabel_objectTest type throwing + case clt_un => exact step_relabel_clt_un + case cgt_un => exact step_relabel_cgt_un + case blt_un target => exact step_relabel_blt_un target + case bge_un target => exact step_relabel_bge_un target + case bgt_un target => exact step_relabel_bgt_un target + case ble_un target => exact step_relabel_ble_un target + case ceq => exact step_relabel_ceq + case brfalse target => exact step_relabel_brfalse target + case brtrue target => exact step_relabel_brtrue target + case castclass type => exact step_relabel_castclass type + case isinst type => exact step_relabel_isinst type + case ldfld field => exact step_relabel_ldfld field + case ldflda field => exact step_relabel_ldflda field + case stfld field => exact step_relabel_stfld field case beq target => exact step_relabel_beq target - case bne target => exact step_relabel_bne target - case nop | neg | gt | bgt | ble | convI1 | convI2 | convU2 | convI8 | convI4 | convU1 - | convU4 | convU8 | convU | bnot | add | mul | band | bor | bxor | shl | shr | shrUn - | lt | branch | switch | bge | blt | feature => + case bne_un target => exact step_relabel_bne_un target + case nop | neg | cgt | bgt | ble | conv_i1 | conv_i2 | conv_u2 | conv_i8 | conv_i4 | conv_u1 + | conv_u4 | conv_u8 | conv_u | conv_i | not | mul | and | or | xor | shl | shr | shr_un + | clt | br | switch | bge | blt => step_relabel_fallback (fun _ => by base_relabels) (fun absent => nomatch absent) - case loadAggregate | copyObject | storeAggregate | initAggregate | loadScalar | storeScalar - | fieldAt | fieldAddress | setFieldAt | aggregateFieldAt | setAggregateFieldAt | lane - | laneAddress | setLane | const32 | const64 | load64 | store64 | dup | pop | skipInit | asRef => + case ldc_i4 + | ldc_i8 | ldnull | ldind | stind | ldlen | ldelem | ldelem_ref | stelem | stelem_ref | dup + | pop => step_relabel_fallback (fun absent => absurd rfl absent) (fun _ _ ran => ⟨ran, context.next rfl⟩) -- Preserve the named specializations for callers of the existing API. theorem step_relabel_nop : StepRelabel .nop := step_relabel _ theorem step_relabel_neg : StepRelabel .neg := step_relabel _ -theorem step_relabel_gt : StepRelabel .gt := step_relabel _ +theorem step_relabel_cgt : StepRelabel .cgt := step_relabel _ theorem step_relabel_bgt (target : Nat) : StepRelabel (.bgt target) := step_relabel _ theorem step_relabel_ble (target : Nat) : StepRelabel (.ble target) := step_relabel _ -theorem step_relabel_loadAggregate (size : Nat) : StepRelabel (.loadAggregate size) := - step_relabel _ -theorem step_relabel_copyObject (size : Nat) : StepRelabel (.copyObject size) := step_relabel _ -theorem step_relabel_storeAggregate (size : Nat) : StepRelabel (.storeAggregate size) := - step_relabel _ -theorem step_relabel_initAggregate (size : Nat) : StepRelabel (.initAggregate size) := - step_relabel _ -theorem step_relabel_loadScalar (width : Nat) (signed : Bool) : - StepRelabel (.loadScalar width signed) := step_relabel _ -theorem step_relabel_storeScalar (width : Nat) : StepRelabel (.storeScalar width) := step_relabel _ -theorem step_relabel_fieldAt (offset : Nat) (width : Nat) (signed : Bool) : - StepRelabel (.fieldAt offset width signed) := step_relabel _ -theorem step_relabel_fieldAddress (offset : Nat) : StepRelabel (.fieldAddress offset) := - step_relabel _ -theorem step_relabel_setFieldAt (offset : Nat) (width : Nat) : - StepRelabel (.setFieldAt offset width) := step_relabel _ -theorem step_relabel_aggregateFieldAt (offset : Nat) (size : Nat) : - StepRelabel (.aggregateFieldAt offset size) := step_relabel _ -theorem step_relabel_setAggregateFieldAt (offset : Nat) (size : Nat) : - StepRelabel (.setAggregateFieldAt offset size) := step_relabel _ -theorem step_relabel_lane (index : Fin 4) : StepRelabel (.lane index) := step_relabel _ -theorem step_relabel_laneAddress (index : Fin 4) : StepRelabel (.laneAddress index) := - step_relabel _ -theorem step_relabel_setLane (index : Fin 4) : StepRelabel (.setLane index) := step_relabel _ -theorem step_relabel_const32 (word : W32) : StepRelabel (.const32 word) := step_relabel _ -theorem step_relabel_const64 (word : W64) : StepRelabel (.const64 word) := step_relabel _ -theorem step_relabel_convI1 : StepRelabel .convI1 := step_relabel _ -theorem step_relabel_convI2 : StepRelabel .convI2 := step_relabel _ -theorem step_relabel_convU2 : StepRelabel .convU2 := step_relabel _ -theorem step_relabel_convI8 : StepRelabel .convI8 := step_relabel _ -theorem step_relabel_convI4 : StepRelabel .convI4 := step_relabel _ -theorem step_relabel_convU1 : StepRelabel .convU1 := step_relabel _ -theorem step_relabel_convU4 : StepRelabel .convU4 := step_relabel _ -theorem step_relabel_convU8 : StepRelabel .convU8 := step_relabel _ -theorem step_relabel_convU : StepRelabel .convU := step_relabel _ -theorem step_relabel_bnot : StepRelabel .bnot := step_relabel _ -theorem step_relabel_add : StepRelabel .add := step_relabel _ + +theorem step_relabel_ldc_i4 (value : W32) : StepRelabel (.ldc_i4 value) := step_relabel _ +theorem step_relabel_ldc_i8 (value : W64) : StepRelabel (.ldc_i8 value) := step_relabel _ +theorem step_relabel_conv_i1 : StepRelabel .conv_i1 := step_relabel _ +theorem step_relabel_conv_i2 : StepRelabel .conv_i2 := step_relabel _ +theorem step_relabel_conv_u2 : StepRelabel .conv_u2 := step_relabel _ +theorem step_relabel_conv_i8 : StepRelabel .conv_i8 := step_relabel _ +theorem step_relabel_conv_i4 : StepRelabel .conv_i4 := step_relabel _ +theorem step_relabel_conv_u1 : StepRelabel .conv_u1 := step_relabel _ +theorem step_relabel_conv_u4 : StepRelabel .conv_u4 := step_relabel _ +theorem step_relabel_conv_u8 : StepRelabel .conv_u8 := step_relabel _ +theorem step_relabel_conv_u : StepRelabel .conv_u := step_relabel _ +/-- A successful `conv_i` step is matched by the relabeled step at the renamed pc. -/ +theorem step_relabel_conv_i : StepRelabel .conv_i := step_relabel _ +#print axioms step_relabel_conv_i +theorem step_relabel_not : StepRelabel .not := step_relabel _ theorem step_relabel_mul : StepRelabel .mul := step_relabel _ -theorem step_relabel_band : StepRelabel .band := step_relabel _ -theorem step_relabel_bor : StepRelabel .bor := step_relabel _ -theorem step_relabel_bxor : StepRelabel .bxor := step_relabel _ +theorem step_relabel_and : StepRelabel .and := step_relabel _ +theorem step_relabel_or : StepRelabel .or := step_relabel _ +theorem step_relabel_xor : StepRelabel .xor := step_relabel _ theorem step_relabel_shl : StepRelabel .shl := step_relabel _ theorem step_relabel_shr : StepRelabel .shr := step_relabel _ -theorem step_relabel_shrUn : StepRelabel .shrUn := step_relabel _ -theorem step_relabel_lt : StepRelabel .lt := step_relabel _ -theorem step_relabel_load64 : StepRelabel .load64 := step_relabel _ -theorem step_relabel_store64 : StepRelabel .store64 := step_relabel _ +theorem step_relabel_shr_un : StepRelabel .shr_un := step_relabel _ +theorem step_relabel_clt : StepRelabel .clt := step_relabel _ +theorem step_relabel_ldind (kind : CIL.NumericKind) : StepRelabel (.ldind kind) := step_relabel _ +theorem step_relabel_stind (kind : CIL.NumericKind) : StepRelabel (.stind kind) := step_relabel _ +theorem step_relabel_ldlen : StepRelabel .ldlen := step_relabel _ +theorem step_relabel_ldelem (kind : CIL.NumericKind) : StepRelabel (.ldelem kind) := step_relabel _ +theorem step_relabel_ldelem_ref : StepRelabel .ldelem_ref := step_relabel _ +theorem step_relabel_stelem (kind : CIL.NumericKind) : StepRelabel (.stelem kind) := step_relabel _ +theorem step_relabel_stelem_ref : StepRelabel .stelem_ref := step_relabel _ theorem step_relabel_dup : StepRelabel .dup := step_relabel _ theorem step_relabel_pop : StepRelabel .pop := step_relabel _ -theorem step_relabel_branch (target : Nat) : StepRelabel (.branch target) := step_relabel _ +theorem step_relabel_br (target : Nat) : StepRelabel (.br target) := step_relabel _ theorem step_relabel_switch (targets : List Nat) : StepRelabel (.switch targets) := step_relabel _ theorem step_relabel_bge (target : Nat) : StepRelabel (.bge target) := step_relabel _ theorem step_relabel_blt (target : Nat) : StepRelabel (.blt target) := step_relabel _ -theorem step_relabel_skipInit : StepRelabel .skipInit := step_relabel _ -theorem step_relabel_asRef : StepRelabel .asRef := step_relabel _ -theorem step_relabel_feature (id : Feature) : StepRelabel (.feature id) := step_relabel _ #print axioms step_relabel /-- An expected step that throws is matched by the same throw from the relabeled step at the renamed pc. -/ -def StepRaise (op : CIL.Op) : Prop := +def StepRaise (op : CIL.Instruction) : Prop := ∀ (expected actual : CIL.Method) (r : Nat → Nat) (pc q : Nat) (args : List Value) - (frame : Frame) (stack : List Value) (m : Memory) (exception : String), + (frame : Frame) (stack : List Value) (m : Memory) (exception : ExceptionType), StepContext expected actual r op pc q → step expected op pc args frame stack m = .error (.thrown exception) → step actual (op.relabel r) q args frame stack m = .error (.thrown exception) @@ -839,13 +973,15 @@ theorem except_bind_error {x : Except ε α} {f : α → Except ε β} {e : ε} (Except.error e >>= f) = .error e := rfl /-- An arithmetic fault is never a thrown exception. -/ -theorem mapError_arithmetic_ne_thrown {x : Except CIL.ArithmeticFault α} {exception : String} : +theorem mapError_arithmetic_ne_thrown {x : Except CIL.ArithmeticFault α} + {exception : ExceptionType} : x.mapError ExecutionFault.arithmetic ≠ .error (.thrown exception) := by cases x <;> simp [Except.mapError] /-- The shared tail of `step` throws only from `instruction`, which both steps share. -/ theorem fallback_raise {r : Nat → Nat} {pc q : Nat} {profile : FeatureProfile} - {stack : List Value} {frame : Frame} {m : Memory} {exception : String} (op : CIL.Op) + {stack : List Value} {frame : Frame} {m : Memory} {exception : ExceptionType} + (op : CIL.Instruction) (arity : pureArity (op.relabel r) = pureArity op) (base : pureArity op ≠ none → BaseRelabel op) (plain : pureArity op = none → instruction op stack m = .error (.thrown exception) → @@ -856,7 +992,7 @@ theorem fallback_raise {r : Nat → Nat} {pc q : Nat} {profile : FeatureProfile} if stack.length < argc then Except.error ExecutionFault.invalidState else do let values ← scalars (List.take argc stack) - match CIL.step op false pc [] 0 values (fun _ => none) profile with + match CIL.step op false none pc values (fun _ => none) profile with | some (CIL.Action.next target result _) => do let result ← List.mapM (checkedValue m) (List.map Value.scalar result) Pure.pure (FrameAction.next target (result ++ List.drop argc stack) frame m) @@ -870,7 +1006,7 @@ theorem fallback_raise {r : Nat → Nat} {pc q : Nat} {profile : FeatureProfile} if stack.length < argc then Except.error ExecutionFault.invalidState else do let values ← scalars (List.take argc stack) - match CIL.step (op.relabel r) false q [] 0 values (fun _ => none) profile with + match CIL.step (op.relabel r) false none q values (fun _ => none) profile with | some (CIL.Action.next target result _) => do let result ← List.mapM (checkedValue m) (List.map Value.scalar result) Pure.pure (FrameAction.next target (result ++ List.drop argc stack) frame m) @@ -917,7 +1053,7 @@ set_option hygiene false in macro "step_raise_fallback" base:term:max plain:term:max : tactic => `(tactic| ( intro expected actual r pc q args frame stack m action context h unfold step at h ⊢ - dsimp only [CIL.Op.relabel] at h ⊢ + dsimp only [CIL.Instruction.relabel] at h ⊢ step_observers rw [context.profile] refine fallback_raise (profile := expected.profile) _ rfl ?_ ?_ context.next h @@ -950,178 +1086,196 @@ macro "lookup_raise" : tactic => `(tactic| ( -- Instructions with a dedicated case in `step`. -theorem step_raise_nativeBinary (operation : Op) : StepRaise (.nativeBinary operation) := by - stack_cases => - first - | fallback_raise_vacuous - | (rcases except_bind_error h with found | ⟨⟨target, values⟩, found, h⟩ - · exact absurd found mapError_arithmetic_ne_thrown - · have native := nativeBinaryValues_relabel r operation pc q _ _ (context.next rfl) target - values (mapError_ok found) - simp only [native, Except.mapError, ok_bind_eq] at h ⊢ - raise_lockstep) - -theorem step_raise_divide (signed remainder : Bool) : StepRaise (.divide signed remainder) := by - stack_cases => +set_option maxHeartbeats 300000 in +/-- Division, remainder and overflow-checked arithmetic raise in lockstep with their + relabeling. -/ +theorem step_raise_faulting (op : Instruction) + (faulting : op.divideKind?.isSome || op.checkedKind?.isSome) : StepRaise op := by + cases op <;> simp only [Instruction.divideKind?, Instruction.checkedKind?, Option.isSome_none, + Option.isSome_some, Bool.or_false, Bool.false_or, Bool.false_eq_true] at faulting + all_goals stack_cases => first | fallback_raise_vacuous | raise_lockstep -theorem step_raise_checkedArithmetic (kind : CheckedBinaryKind) (signed : Bool) : - StepRaise (.checkedArithmetic kind signed) := by - stack_cases => - first | fallback_raise_vacuous | raise_lockstep +#print axioms step_raise_faulting -theorem step_raise_checkedConvert (width : Nat) (signed unsignedInput : Bool) : - StepRaise (.checkedConvert width signed unsignedInput) := by +/-- A throwing checked conversion of a signed operand throws the same from the relabeled step. -/ +theorem step_raise_conv_ovf (target : ConvTarget) : StepRaise (.conv_ovf target) := by stack_cases => first | fallback_raise_vacuous | raise_lockstep -theorem step_raise_call (callee argc : Nat) : StepRaise (.call callee argc) := by - plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - raise_lockstep +#print axioms step_raise_conv_ovf -/-- A null receiver raises the same exception at the relocated pc. -/ -theorem step_raise_callInstance (callee argc : Nat) : StepRaise (.callInstance callee argc) := by - plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - raise_lockstep +/-- A throwing checked conversion of an unsigned operand throws the same from the relabeled + step. -/ +theorem step_raise_conv_ovf_un (target : ConvTarget) : StepRaise (.conv_ovf_un target) := by + stack_cases => + first | fallback_raise_vacuous | raise_lockstep -#print axioms step_raise_callInstance +#print axioms step_raise_conv_ovf_un -/-- A null receiver raises the same exception at the relocated pc. -/ -theorem step_raise_callVirtual (targets : List (CIL.Extraction.TypeExpr × Nat)) (argc : Nat) : - StepRaise (.callVirtual targets argc) := by +/-- A call raises nothing of its own; its faults keep their location under pc relocation. -/ +theorem step_raise_call (method : CIL.Extraction.MethodRef) (prefixes : CallPrefixes) : + StepRaise (.call method prefixes) := by plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - raise_lockstep - -#print axioms step_raise_callVirtual - -/-- Tail-transfer faults retain their original location under pc relocation. -/ -theorem step_raise_tailCall (callee argc : Nat) : StepRaise (.tailCall callee argc) := by + rw [context.link] + split at h + · rename_i unlinked + try simp only [unlinked] + by_cases tail : prefixes.tail = true + · simp [tail, unlinkedCall] at h + · simp only [tail, Bool.false_eq_true, ↓reduceIte] at h ⊢ + rw [context.environment] + exact modelStep_move_error context.profile h + · rename_i link linked + try simp only [linked] + dsimp only [observedCall] at h ⊢ + obtain ⟨observed, h⟩ := observer_thrown h + simp only [observed, Bool.false_eq_true, ↓reduceIte] + cases link <;> try (simp [unlinkedCall] at h; done) + by_cases tail : prefixes.tail = true + · simp only [tail, ↓reduceIte] at h ⊢ + exact h + · by_cases short : stack.length < method.argumentCount + · simp [tail, short] at h + · simp only [tail, short, ↓reduceIte, Bool.false_eq_true] at h ⊢ + raise_lockstep + +#print axioms step_raise_call + +/-- A null receiver raises the same exception at the relocated pc under either link kind. -/ +theorem step_raise_callvirt (method : CIL.Extraction.MethodRef) : + StepRaise (.callvirt method) := by plain_cases - exact h - -#print axioms step_raise_tailCall + rw [context.link] + split at h <;> try cases h + all_goals try simp only [‹expected.linkAt pc = _›] + all_goals + by_cases short : stack.length < method.argumentCount + · simp [short] at h + · simp only [short, ↓reduceIte] at h ⊢ + raise_lockstep -theorem step_raise_newVector256 (callee argc : Nat) : StepRaise (.newVector256 callee argc) := by - plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - raise_lockstep +#print axioms step_raise_callvirt -theorem step_raise_newAggregate (callee argc : Nat) (size : Fin (2^64)) : - StepRaise (.newAggregate callee argc size) := by - plain_cases - by_cases short : (size = 0 || stack.length < argc) = true - · simp only [short, ↓reduceIte] at h; cases h - · simp only [short, ↓reduceIte, Bool.false_eq_true] at h ⊢ - raise_lockstep +/-- Relocation preserves the fault from a constructor body or its runtime model. -/ +theorem step_raise_newobj (constructor : Extraction.MethodRef) : + StepRaise (.newobj constructor) := by -theorem step_raise_newObject (callee argc : Nat) (owner : Extraction.TypeExpr) - (bases : List Extraction.TypeExpr) (size : Nat) : - StepRaise (.newObject callee argc owner bases size) := by plain_cases - by_cases short : stack.length < argc - · simp [short] at h - · simp only [short, ↓reduceIte] at h ⊢ - raise_lockstep - -theorem step_raise_intrinsic (operation : Intrinsic) (argc : Nat) : - StepRaise (.intrinsic operation argc) := by - explicit_intro - rw [context.profile] - by_cases avail : (!operation.available expected.profile) = true - · simp [avail] at h - · simp only [avail, ↓reduceIte, Bool.false_eq_true] at h ⊢ - by_cases short : stack.length < argc + unfold newobjStep constructAggregate constructObject at h ⊢ + rw [context.link] + rcases linked : expected.linkAt pc with _ | link + · simp only [linked] at h + rw [context.environment] + exact modelStep_move_error context.profile h + simp only [linked] at h ⊢ + cases link + case aggregate callee size => + by_cases short : (size = 0 || stack.length < constructor.parameters.length) = true + · simp only [short, ↓reduceIte] at h; cases h + · simp only [short, ↓reduceIte, Bool.false_eq_true] at h ⊢ + raise_lockstep + case object callee bases size => + by_cases short : stack.length < constructor.parameters.length · simp [short] at h · simp only [short, ↓reduceIte] at h ⊢ raise_lockstep + all_goals first | cases h | (simp [unlinkedCall] at h) -theorem step_raise_ret (narrow : Option NarrowKind) : StepRaise (.ret narrow) := by +#print axioms step_raise_newobj + +theorem step_raise_ret : StepRaise .ret := by intro expected actual r pc q args frame stack m action context h unfold step at h ⊢ - dsimp only [CIL.Op.relabel] at h ⊢ + dsimp only [CIL.Instruction.relabel] at h ⊢ step_observers - cases narrow - · dsimp only [CIL.Op.relabel] at h ⊢ - rw [context.returnsValue] - by_cases sized : stack.length = (if expected.returnsValue then 1 else 0) - · rw [ite_eq_left sized] at h ⊢ - raise_lockstep - · rw [ite_eq_right sized] at h - cases h - · rcases stack with _ | ⟨v, rest⟩ - · dsimp only [CIL.Op.relabel] at h ⊢ - fallback_raise_vacuous - · cases v with - | scalar value => - cases value <;> cases rest - all_goals (try dsimp only [CIL.Op.relabel] at h ⊢) - all_goals (try rw [context.returnsValue]) - all_goals first - | fallback_raise_vacuous - | (by_cases returns : expected.returnsValue = true - · simp only [returns, ↓reduceIte] at h ⊢ - raise_lockstep - · simp only [returns, ↓reduceIte, Bool.false_eq_true] at h - cases h) - | reference reference => - cases rest <;> fallback_raise_vacuous - | span reference length => - cases rest <;> fallback_raise_vacuous - | bufferSpan _ | readOnlyReference _ | array _ | methodPointer _ | methodTable _ - | object _ => - cases rest <;> fallback_raise_vacuous + rw [context.returnsValue, show actual.returnNarrow = expected.returnNarrow from by + simp only [CIL.Method.returnNarrow, context.returnsValue, context.declared, + context.environment]] + unfold returnFrom at h ⊢ + repeat' first + | (split at h <;> simp_all only) + | raise_lockstep + | cases h + +#print axioms step_raise_ret /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_eq : StepRaise .eq := by +theorem step_raise_ceq : StepRaise .ceq := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep -#print axioms step_raise_eq +#print axioms step_raise_ceq /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_brzero (target : Nat) : StepRaise (.brzero target) := by +theorem step_raise_brfalse (target : Nat) : StepRaise (.brfalse target) := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep -#print axioms step_raise_brzero +#print axioms step_raise_brfalse /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_brnonzero (target : Nat) : StepRaise (.brnonzero target) := by +theorem step_raise_brtrue (target : Nat) : StepRaise (.brtrue target) := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep -#print axioms step_raise_brnonzero +#print axioms step_raise_brtrue /-- A failed cast raises `InvalidCastException` at the same relabelled location. -/ -theorem step_raise_objectTest (type : CIL.Extraction.TypeExpr) (throwing : Bool) : - StepRaise (.objectTest type throwing) := by +theorem step_raise_castclass (type : CIL.Extraction.TypeExpr) : StepRaise (.castclass type) := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun absent => absurd rfl absent) (fun _ ran => ran) context.next h | raise_lockstep -#print axioms step_raise_objectTest +#print axioms step_raise_castclass + +/-- A throwing `isinst` step throws the same at the relabelled location. -/ +theorem step_raise_isinst (type : CIL.Extraction.TypeExpr) : StepRaise (.isinst type) := by + stack_cases => + first + | exact fallback_raise (profile := expected.profile) _ rfl + (fun absent => absurd rfl absent) (fun _ ran => ran) context.next h + | raise_lockstep + +#print axioms step_raise_isinst + +/-- A thrown instance field load, such as a null receiver, is thrown by the relabeled load. -/ +theorem step_raise_ldfld (field : CIL.Extraction.FieldRef) : StepRaise (.ldfld field) := by + plain_cases + rw [show fieldLoad actual field stack m = fieldLoad expected field stack m by + simp only [fieldLoad, CIL.Method.fieldShape, context.environment]] + raise_lockstep + +#print axioms step_raise_ldfld + +/-- A thrown instance field store is thrown by the relabeled store. -/ +theorem step_raise_stfld (field : CIL.Extraction.FieldRef) : StepRaise (.stfld field) := by + plain_cases + rw [show fieldStore actual field stack m = fieldStore expected field stack m by + simp only [fieldStore, CIL.Method.fieldShape, context.environment]] + raise_lockstep + +#print axioms step_raise_stfld + +/-- A thrown field address is thrown by the relabeled address, which resolves the field alike. -/ +theorem step_raise_ldflda (field : CIL.Extraction.FieldRef) : StepRaise (.ldflda field) := by + plain_cases + rw [show fieldReference actual field stack m = fieldReference expected field stack m by + simp only [fieldReference, CIL.Method.fieldShape, context.environment]] + raise_lockstep + +#print axioms step_raise_ldflda /-- Reference observations preserve exceptional outcomes under relabeling. -/ theorem step_raise_beq (target : Nat) : StepRaise (.beq target) := by @@ -1134,14 +1288,25 @@ theorem step_raise_beq (target : Nat) : StepRaise (.beq target) := by #print axioms step_raise_beq /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_bne (target : Nat) : StepRaise (.bne target) := by +theorem step_raise_bne_un (target : Nat) : StepRaise (.bne_un target) := by + stack_cases => + first + | exact fallback_raise (profile := expected.profile) _ rfl + (fun _ => by base_relabels) (by intro absent; contradiction) context.next h + | raise_lockstep + +#print axioms step_raise_bne_un + +/-- Reference and native table additions preserve exceptional outcomes under relabeling. -/ +theorem step_raise_add : StepRaise .add := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep + | (split at h <;> raise_lockstep) -#print axioms step_raise_bne +#print axioms step_raise_add /-- Reference observations preserve exceptional outcomes under relabeling. -/ theorem step_raise_sub : StepRaise .sub := by @@ -1154,71 +1319,71 @@ theorem step_raise_sub : StepRaise .sub := by #print axioms step_raise_sub /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_ltu : StepRaise .ltu := by +theorem step_raise_clt_un : StepRaise .clt_un := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep -#print axioms step_raise_ltu +#print axioms step_raise_clt_un /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_gtu : StepRaise .gtu := by +theorem step_raise_cgt_un : StepRaise .cgt_un := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep -#print axioms step_raise_gtu +#print axioms step_raise_cgt_un /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_bltu (target : Nat) : StepRaise (.bltu target) := by +theorem step_raise_blt_un (target : Nat) : StepRaise (.blt_un target) := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep -#print axioms step_raise_bltu +#print axioms step_raise_blt_un /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_bgeu (target : Nat) : StepRaise (.bgeu target) := by +theorem step_raise_bge_un (target : Nat) : StepRaise (.bge_un target) := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep -#print axioms step_raise_bgeu +#print axioms step_raise_bge_un /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_bgtu (target : Nat) : StepRaise (.bgtu target) := by +theorem step_raise_bgt_un (target : Nat) : StepRaise (.bgt_un target) := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep -#print axioms step_raise_bgtu +#print axioms step_raise_bgt_un /-- Reference observations preserve exceptional outcomes under relabeling. -/ -theorem step_raise_bleu (target : Nat) : StepRaise (.bleu target) := by +theorem step_raise_ble_un (target : Nat) : StepRaise (.ble_un target) := by stack_cases => first | exact fallback_raise (profile := expected.profile) _ rfl (fun _ => by base_relabels) (by intro absent; contradiction) context.next h | raise_lockstep -#print axioms step_raise_bleu +#print axioms step_raise_ble_un /-- Instructions dispatched directly to the memory evaluator preserve its faults. -/ -theorem step_raise_instruction (op : CIL.Op) +theorem step_raise_instruction (op : CIL.Instruction) (relabels : ∀ r, op.relabel r = op) (dispatch : ∀ body pc args frame stack memory, step body op pc args frame stack memory = - if unspecifiedOperands (observerCount op) stack || false then + if unspecifiedOperands (observerCount op) stack then .error (.unsupported "Instruction observes unspecified vector bits") else do let (result, values) ← instruction op stack memory @@ -1240,92 +1405,131 @@ theorem step_raise_instruction (op : CIL.Op) set_option maxHeartbeats 400000 in /-- Only `throw` raises by itself; every other throw comes from a computation both steps share. -/ -theorem step_raise (op : CIL.Op) : StepRaise op := by +theorem step_raise (op : CIL.Instruction) : StepRaise op := by cases op - case managed operation => + case calli => plain_cases - simp only [managedStep] at h ⊢ + rw [context.link] + simp only [calliStep] at h ⊢ repeat' first | solve | raise_lockstep + | solve | simp [unlinkedCall] at h | cases h - | (split at h <;> try simp only [*, Bool.false_eq_true, Bool.true_eq, ite_true, ite_false, - Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢) - case stackAlloc | newArray | newReferenceArray => + | (split at h <;> try simp only [*, Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢) + case localloc => + plain_cases + rw [context.declared] + unfold localAllocation at h ⊢ + split at h + · split at h <;> first | raise_lockstep | cases h + · cases h + case newarr => stack_cases => first | fallback_raise_vacuous - | (cases ‹CIL.Value› <;> - dsimp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢ <;> - first | cases h | raise_lockstep) - case nativeBinary operation => exact step_raise_nativeBinary operation - case divide signed remainder => exact step_raise_divide signed remainder - case checkedArithmetic kind signed => exact step_raise_checkedArithmetic kind signed - case checkedConvert width signed unsignedInput => - exact step_raise_checkedConvert width signed unsignedInput - case arg index narrow => + | (simp only [CIL.Method.elementShape, context.environment] at h ⊢ + dsimp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢ + first | cases h | raise_lockstep) + case div | div_un | rem | rem_un | add_ovf | add_ovf_un | sub_ovf | sub_ovf_un | mul_ovf + | mul_ovf_un => exact step_raise_faulting _ rfl + case conv_ovf target => exact step_raise_conv_ovf target + case conv_ovf_un target => exact step_raise_conv_ovf_un target + case ldarg index => plain_cases - cases narrow <;> dsimp only at h ⊢ <;> lookup_raise - case vectorArg | vectorArgAddr => - explicit_intro + rw [show loadArgument actual index args frame m = loadArgument expected index args frame m by + simp only [loadArgument, context.arguments]] raise_lockstep - case «local» | vectorLocal | vectorLocalAddr | localAddr => + case starg index => + stack_cases => + first + | fallback_raise_vacuous + | (rw [show ∀ value, storeArgument actual index value frame m = + storeArgument expected index value frame m by + intro value + simp only [storeArgument, context.arguments]] + raise_lockstep) + case ldarga index => + plain_cases + rw [show addressArgument actual index frame m = addressArgument expected index frame m by + simp only [addressArgument, context.arguments]] + raise_lockstep + case ldloc | ldloca => plain_cases lookup_raise - case setVectorLocal | setLocal => + case stloc => stack_cases => first | fallback_raise_vacuous | lookup_raise - case initializeSpanLocal => + case call method prefixes => exact step_raise_call method prefixes + case callvirt method => exact step_raise_callvirt method + case newobj constructor => exact step_raise_newobj constructor + case cpblk | initblk | ldftn | ldstr => plain_cases; raise_lockstep + case ldsfld => plain_cases - split at h - · rename_i found - try simp only [found] - rcases except_bind_error h with constructed | ⟨⟨memory, values⟩, constructed, h⟩ - · simp only [constructed, error_bind_eq] - · simp only [constructed, ok_bind_eq] - cases values with - | nil => cases h - | cons value rest => raise_lockstep - · cases h - case call callee argc => exact step_raise_call callee argc - case callInstance callee argc => exact step_raise_callInstance callee argc - case callVirtual targets argc => exact step_raise_callVirtual targets argc - case tailCall callee argc => exact step_raise_tailCall callee argc - case newVector256 callee argc => exact step_raise_newVector256 callee argc - case newAggregate callee argc size => exact step_raise_newAggregate callee argc size - case newObject callee argc owner bases size => - exact step_raise_newObject callee argc owner bases size - case intrinsic operation argc => exact step_raise_intrinsic operation argc - case memory operation => + rw [staticLoad_congr context.statics] + raise_lockstep + case ldsflda => plain_cases - rw [staticInstruction_congr context.statics] + rw [staticAddress_congr context.statics] raise_lockstep - case ret narrow => exact step_raise_ret narrow + case ldobj | stobj | cpobj | sizeof => + plain_cases + simp only [Method.valueShape, context.environment] at h ⊢ + first | solve | raise_lockstep | lookup_raise + case initobj type => + plain_cases + simp only [Method.valueShape, context.environment] at h ⊢ + generalize expected.environment.valueShape type = shape at h ⊢ + cases shape <;> simp only [ValueShape.size?] at h ⊢ + case span => + unfold initializeSpanHome at h ⊢ + split at h + · lookup_raise + · cases h + all_goals first | solve | raise_lockstep | cases h + case ldelem_any | stelem_any | ldelema => + plain_cases + simp only [CIL.Method.elementShape, context.environment] at h ⊢ + dsimp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h ⊢ + first | cases h | raise_lockstep + case ldtoken operand => + cases operand <;> plain_cases + case type => raise_lockstep + case field => rw [staticAddress_congr context.statics]; raise_lockstep + case stsfld => + plain_cases + cases h + case ret => exact step_raise_ret case throw => explicit_intro exact h - case unsupported => - plain_cases - fallback_raise_vacuous + case unadmitted => + explicit_intro + exact h + case add => exact step_raise_add case sub => exact step_raise_sub - case ltu => exact step_raise_ltu - case gtu => exact step_raise_gtu - case bltu target => exact step_raise_bltu target - case bgeu target => exact step_raise_bgeu target - case bgtu target => exact step_raise_bgtu target - case bleu target => exact step_raise_bleu target - case eq => exact step_raise_eq - case brzero target => exact step_raise_brzero target - case brnonzero target => exact step_raise_brnonzero target - case objectTest type throwing => exact step_raise_objectTest type throwing + case clt_un => exact step_raise_clt_un + case cgt_un => exact step_raise_cgt_un + case blt_un target => exact step_raise_blt_un target + case bge_un target => exact step_raise_bge_un target + case bgt_un target => exact step_raise_bgt_un target + case ble_un target => exact step_raise_ble_un target + case ceq => exact step_raise_ceq + case brfalse target => exact step_raise_brfalse target + case brtrue target => exact step_raise_brtrue target + case castclass type => exact step_raise_castclass type + case isinst type => exact step_raise_isinst type + case ldfld field => exact step_raise_ldfld field + case ldflda field => exact step_raise_ldflda field + case stfld field => exact step_raise_stfld field case beq target => exact step_raise_beq target - case bne target => exact step_raise_bne target - case nop | neg | gt | bgt | ble | convI1 | convI2 | convU2 | convI8 | convI4 | convU1 - | convU4 | convU8 | convU | bnot | add | mul | band | bor | bxor | shl | shr | shrUn - | lt | branch | switch | bge | blt | feature => + case bne_un target => exact step_raise_bne_un target + case nop | neg | cgt | bgt | ble | conv_i1 | conv_i2 | conv_u2 | conv_i8 | conv_i4 | conv_u1 + | conv_u4 | conv_u8 | conv_u | conv_i | not | mul | and | or | xor | shl | shr | shr_un + | clt | br | switch | bge | blt => step_raise_fallback (fun _ => by base_relabels) (fun absent => nomatch absent) - case loadAggregate | copyObject | storeAggregate | initAggregate | loadScalar | storeScalar - | fieldAt | fieldAddress | setFieldAt | aggregateFieldAt | setAggregateFieldAt | lane - | laneAddress | setLane | const32 | const64 | load64 | store64 | dup | pop | skipInit | asRef => + case ldc_i4 + | ldc_i8 | ldnull | ldind | stind | ldlen | ldelem | ldelem_ref | stelem | stelem_ref | dup + | pop => exact step_raise_instruction _ (fun _ => rfl) (fun _ _ _ _ _ _ => rfl) #print axioms step_raise diff --git a/CIL/Safety/Embedding/Suffix.lean b/CIL/Safety/Embedding/Suffix.lean index c0811023d..7a8cc4f75 100644 --- a/CIL/Safety/Embedding/Suffix.lean +++ b/CIL/Safety/Embedding/Suffix.lean @@ -3,10 +3,10 @@ import CIL.Safety.Embedding.Simulation /-! # Relocated suffixes -When the expected body is dead (`Op.live = false`) before pc `lower` and its code from +When the expected body is dead (`Instruction.live = false`) before pc `lower` and its code from `lower` on appears relabeled at pc `start` of the actual body, the actual body refines it under `suffixRename`. Branch targets must stay inside the suffix. A proof about a region, -with the code outside it masked as `.unsupported`, then transfers to the real method +with the code outside it masked as holes (`.unadmitted`), then transfers to the real method through `Embedding.Simulation`. -/ @@ -20,30 +20,98 @@ def suffixRename (lower start pc : Nat) : Nat := if pc < lower then 0 else pc - lower + start /-- Relabeling depends only on the renaming's values at the instruction's own targets. -/ -theorem _root_.CIL.Op.relabel_congr {left right : Nat → Nat} (op : Op) +theorem _root_.CIL.Instruction.relabel_congr {left right : Nat → Nat} (op : Instruction) (same : ∀ target ∈ op.targets, left target = right target) : op.relabel left = op.relabel right := by - induction op <;> simp_all [Op.relabel, Op.targets] + induction op <;> simp_all [Instruction.relabel, Instruction.targets] + +/-- A table whose keys all lie below `query` gives it no link, so a lookup skips it. -/ +theorem linkLookup_append_of_below {before after : List (Nat × Link)} {query : Nat} + (below : ∀ entry ∈ before, entry.1 < query) : + linkLookup (before ++ after) query = linkLookup after query := by + induction before with + | nil => rfl + | cons head rest ih => + have differs : (head.1 == query) = false := by + have := below head (List.mem_cons_self ..) + simp only [beq_eq_false_iff_ne] + omega + have restBelow : ∀ entry ∈ rest, entry.1 < query := + fun entry member => below entry (List.mem_cons_of_mem _ member) + simpa only [linkLookup, List.cons_append, List.find?_cons, differs] using ih restBelow + +#print axioms linkLookup_append_of_below + +/-- Moving every key of a table at or after `lower` to `key - lower + start` moves each lookup + at or after `lower` the same way. -/ +theorem linkLookup_shift {links : List (Nat × Link)} {lower start query : Nat} + (above : ∀ entry ∈ links, lower ≤ entry.1) (after : lower ≤ query) : + linkLookup (links.map fun entry => (entry.1 - lower + start, entry.2)) + (query - lower + start) = linkLookup links query := by + induction links with + | nil => rfl + | cons head rest ih => + obtain ⟨key, link⟩ := head + have keyAbove := above (key, link) (List.mem_cons_self ..) + have restAbove : ∀ entry ∈ rest, lower ≤ entry.1 := + fun entry member => above entry (List.mem_cons_of_mem _ member) + by_cases same : key = query + · subst same + simp [linkLookup] + · have shiftedDiffers : key - lower + start ≠ query - lower + start := by + simp only at keyAbove + omega + have differs : (key == query) = false := by simpa using same + have movedDiffers : (key - lower + start == query - lower + start) = false := by + simpa using shiftedDiffers + simpa only [linkLookup, List.map_cons, List.find?_cons, movedDiffers, differs] + using ih restAbove + +#print axioms linkLookup_shift + +/-- Explicit tables, as a body with `call` sites has: the expected table's entries from `lower` on, + moved to `start`, follow entries of the actual table below `start`, and the expected table's + other entries lie below `lower`; then each moved pc has the link of its original. -/ +theorem links_of_shifted_table {expected actual : Method} {lower start : Nat} + {expectedBefore expectedAfter actualBefore : List (Nat × Link)} + (expectedLinks : expected.links = expectedBefore ++ expectedAfter) + (actualLinks : actual.links = + actualBefore ++ expectedAfter.map fun entry => (entry.1 - lower + start, entry.2)) + (expectedBelow : ∀ entry ∈ expectedBefore, entry.1 < lower) + (actualBelow : ∀ entry ∈ actualBefore, entry.1 < start) + (above : ∀ entry ∈ expectedAfter, lower ≤ entry.1) : + ∀ pc, lower ≤ pc → actual.linkAt (pc - lower + start) = expected.linkAt pc := by + intro pc after + simp only [Method.linkAt, expectedLinks, actualLinks] + rw [linkLookup_append_of_below (fun entry member => by have := actualBelow entry member; omega), + linkLookup_append_of_below (fun entry member => by have := expectedBelow entry member; omega)] + exact linkLookup_shift above after + +#print axioms links_of_shifted_table /-- The expected code from `lower` on, relabeled and placed at `start` in `actual`, makes - `actual` refine `expected` under `suffixRename lower start`. -/ + `actual` refine `expected` under `suffixRename lower start`. Static sites and link tables + may differ, as long as each moved pc resolves the same descriptor and link. -/ theorem BodyRefines.of_relocated_suffix {expected actual : Method} {lower start : Nat} - (metadata : actual = { expected with code := actual.code, staticSites := actual.staticSites }) + (metadata : actual = { expected with + code := actual.code, staticSites := actual.staticSites, links := actual.links }) (descriptors : actual.staticSites.map Prod.snd = expected.staticSites.map Prod.snd) (entry : lower = 0 → start = 0) (dead : ∀ pc op, pc < lower → expected.code[pc]? = some op → op.live = false) (suffix : actual.code.drop start = - (expected.code.drop lower).map (Op.relabel (fun pc => pc - lower + start))) + (expected.code.drop lower).map (Instruction.relabel (fun pc => pc - lower + start))) (targets : ∀ op ∈ expected.code.drop lower, ∀ target ∈ op.targets, lower ≤ target) - (statics : ∀ pc, lower ≤ pc → staticAt actual (pc - lower + start) = staticAt expected pc) : + (statics : ∀ pc, lower ≤ pc → actual.staticAt (pc - lower + start) = expected.staticAt pc) + (links : ∀ pc, lower ≤ pc → actual.linkAt (pc - lower + start) = expected.linkAt pc) : BodyRefines expected actual (suffixRename lower start) := by refine ⟨by simpa only using congrArg Method.profile metadata, by simpa only using congrArg Method.returnsValue metadata, by simpa only using congrArg Method.locals metadata, by simpa only using congrArg Method.localKinds metadata, by simpa only using congrArg Method.opaqueVectorLocals metadata, - by simpa only using congrArg Method.vectorArgs metadata, - by simpa only using congrArg Method.parameterHomes metadata, descriptors, ?_, ?_⟩ + by simpa only using congrArg Method.parameterHomes metadata, + by simpa only using congrArg Method.declared metadata, + by simpa only using congrArg Method.environment metadata, descriptors, ?_, ?_⟩ · by_cases zero : lower = 0 · simp [suffixRename, zero, entry zero] · simp [suffixRename, show 0 < lower by omega] @@ -63,9 +131,10 @@ theorem BodyRefines.of_relocated_suffix {expected actual : Method} {lower start have code := congrArg (fun code => code[pc - lower]?) suffix rw [List.getElem?_drop, List.getElem?_map, fetched, Option.map_some, Nat.add_comm start (pc - lower), relabeled] at code - refine ⟨?_, ?_, fun _ => ?_⟩ + refine ⟨?_, ?_, ?_, fun _ => ?_⟩ · simpa only [suffixRename, ite_eq_right (Nat.not_lt.mpr after)] using code · simpa only [suffixRename, ite_eq_right (Nat.not_lt.mpr after)] using statics pc after + · simpa only [suffixRename, ite_eq_right (Nat.not_lt.mpr after)] using links pc after · simp only [suffixRename, ite_eq_right (Nat.not_lt.mpr after), ite_eq_right (show ¬pc + 1 < lower by omega)] omega diff --git a/CIL/Safety/Execution.lean b/CIL/Safety/Execution.lean index 861524d63..bb532221c 100644 --- a/CIL/Safety/Execution.lean +++ b/CIL/Safety/Execution.lean @@ -5,16 +5,19 @@ import CIL.Safety.StaticMemory import CIL.Safety.ReferenceObservations import CIL.Safety.HeapObjects import CIL.Safety.NativeMethodTables -import CIL.NativeEvaluation +import CIL.Resolution +import CIL.RuntimeModel +import CIL.Extraction.RuntimeModels /-! # Checked interpreter -The interpreter that safety proofs reason about. It runs the same instructions as -`CIL.step`, but over allocation-aware memory and checked values, and it reports why it -stopped: every fault is an `ExecutionFault` located at a method and pc instead of `none`. -Value-only instructions are delegated to `CIL.step` through `pureArity`, so the two -interpreters cannot disagree on arithmetic. +The interpreter that safety proofs reason about. It runs over allocation-aware memory and +checked values, and it reports why it stopped: every fault is an `ExecutionFault` located at a +method and pc instead of `none`. Value-only instructions are delegated to `CIL.step` through +`pureArity`, so the two interpreters cannot disagree on arithmetic. Locals have only the meaning +given here, through the kinds of their slots. `runModel` runs a runtime model through the same +named functions as the instructions emitted for its call. -/ namespace CIL.Safety @@ -33,6 +36,17 @@ theorem numericValue_snapshotValue (shape : CIL.SnapshotShape) (bytes : List (Bi · cases shape <;> rfl · simp [numericValue, length] +/-- A slice of a snapshot's bytes that lies within them is a well-formed value of its width. -/ +theorem numericValue_snapshotSlice {bytes : List (BitVec 8 × Bool)} {offset size total : Nat} + (within : ¬(bytes.length != total || decide (offset + size > total)) = true) : + numericValue (snapshotValue (.aggregate size) ((bytes.drop offset).take size)) = true := by + apply numericValue_snapshotValue + simp only [Bool.or_eq_true, bne_iff_ne, ne_eq, decide_eq_true_eq, not_or, Decidable.not_not] + at within + simp only [List.length_take, List.length_drop, CIL.SnapshotShape.width] + omega +#print axioms numericValue_snapshotSlice + theorem loadSnapshot_numeric {m : Memory} {r : ManagedReference} {shape : CIL.SnapshotShape} {value : CIL.Value} (h : loadSnapshot m r shape = .ok value) : numericValue value = true := by unfold loadSnapshot at h @@ -67,6 +81,8 @@ theorem bitcastValue_numeric {source target : CIL.SnapshotShape} {input value : def checkedValue (m : Memory) : Value → ExecutionResult Value | .methodPointer pointer => .ok (.methodPointer pointer) | .methodTable table => .ok (.methodTable table) + | .typeHandle type => .ok (.typeHandle type) + | .runtimeType type => .ok (.runtimeType type) | .object r => formObject m r | .reference r => formValue m r | .readOnlyReference r => do @@ -82,21 +98,100 @@ def checkedValue (m : Memory) : Value → ExecutionResult Value let _ ← formValue m r return .span r length | .scalar value => if numericValue value then .ok (.scalar value) else .error .invalidState + -- A home names a slot of one frame, so no value that crosses a frame boundary is one. + | .spanHome _ => .error .invalidState + +/-- What a call passes for `value`: for a span home, the view its slot of `frame` holds when the + call runs; every other value unchanged. A home whose slot holds no view faults. -/ +def spanView (frame : Frame) (memory : Memory) : Value → ExecutionResult Value + | .spanHome index => match frame.locals[index]? with + | some (.span width writable view) => loadLocal memory (.span width writable view) + | _ => .error .invalidState + | .typeHandle type => pure (.typeHandle type) + | .runtimeType type => pure (.runtimeType type) + | .scalar value => pure (.scalar value) + | .reference reference => pure (.reference reference) + | .span reference length => pure (.span reference length) + | .bufferSpan view => pure (.bufferSpan view) + | .array view => pure (.array view) + | .readOnlyReference reference => pure (.readOnlyReference reference) + | .methodPointer pointer => pure (.methodPointer pointer) + | .methodTable table => pure (.methodTable table) + | .object reference => pure (.object reference) + +/-- A call argument of `frame`: `value`, a home read as `spanView` reads it, checked as + `checkedValue` checks a pushed value. -/ +def callArgument (frame : Frame) (memory : Memory) (value : Value) : ExecutionResult Value := do + checkedValue memory (← spanView frame memory value) + +/-- How many operands of `operation` `viewOperands` reads: all of a span or view operation's, + where a home stands for the view its slot holds, and none of any other. The match has no + default, so each new operation is classified. -/ +def viewOperandCount (operation : CIL.MemoryOp) : Nat := + match operation with + | .observeView _ | .spanReinterpret .. | .spanCopy .. | .spanFill .. | .arrayFill .. + | .spanCreate | .spanReference | .createSpan _ | .spanItem _ | .spanLength | .spanEmpty .. + | .spanFromReference .. | .spanSingleReference .. | .spanSlice _ | .spanReadOnly + | .arraySpan .. => operation.argumentCount + | .binaryRead .. | .binaryWrite .. | .marshalRead .. | .marshalWrite .. | .pointerTransfer .. + | .vector128Unsafe .. | .bigMul64 _ | .divRemOut _ + | .asRef | .add .. | .byteOffset | .bitcastByteBool | .bitcastValue .. | .nullRef | .isNullRef + | .throwIfNull | .zeroGuard .. | .comparisonGuard .. + | .copyBlock _ | .initBlock _ + | .load128 | .load256 | .load512 | .store128 | .store256 | .store512 => 0 + +/-- A value `checkedValue` accepts is passed as that same checked value, since it is no home. -/ +theorem callArgument_of_checked {frame : Frame} {memory : Memory} {value result : Value} + (checked : checkedValue memory value = .ok result) : + callArgument frame memory value = .ok result := by + cases value <;> first | (cases checked; done) | exact checked | (cases checked; rfl) + +#print axioms callArgument_of_checked + +/-- Values `checkedValue` accepts one by one pass as call arguments unchanged. -/ +theorem callArguments_of_checked {frame : Frame} {memory : Memory} : + ∀ {values results : List Value}, values.mapM (checkedValue memory) = .ok results → + values.mapM (callArgument frame memory) = .ok results + | [], _, checked => checked + | value :: rest, results, checked => by + simp only [List.mapM_cons, Bind.bind, Except.bind] at checked ⊢ + cases head : checkedValue memory value with + | error _ => simp [head] at checked + | ok checkedHead => + rw [callArgument_of_checked head] + cases tail : rest.mapM (checkedValue memory) with + | error _ => simp [head, tail] at checked + | ok others => + rw [callArguments_of_checked tail] + simpa [head, tail] using checked + +#print axioms callArguments_of_checked + +/-- The stack with each of its top `count` entries read as `spanView` reads it: the operands a + span operation consumes, a home among them standing for the view its slot holds. -/ +def viewOperands (frame : Frame) (memory : Memory) : + Nat → List Value → ExecutionResult (List Value) + | 0, stack => pure stack + | _ + 1, [] => pure [] + | count + 1, value :: rest => do + return (← spanView frame memory value) :: (← viewOperands frame memory count rest) /-- Only the value-only instruction whitelist may use the original pure arithmetic semantics. No memory/reference operation can enter this path. -/ -def pureArity : CIL.Op → Option Nat - | .convI1 | .convI2 | .convU2 => some 1 +def pureArity : CIL.Instruction → Option Nat + | .conv_i1 | .conv_i2 | .conv_u2 => some 1 | .nop => some 0 | .neg => some 1 - | .gt | .bgt _ | .ble _ | .bleu _ => some 2 - | .convI8 | .convI4 | .convU1 | .convU4 | .convU8 | .convU | .bnot => some 1 - | .add | .sub | .mul | .band | .bor | .bxor | .shl | .shr | .shrUn - | .lt | .ltu | .gtu | .eq => some 2 - | .brzero _ | .brnonzero _ | .switch _ => some 1 - | .bltu _ | .bgeu _ | .bgtu _ | .bge _ | .blt _ | .beq _ | .bne _ => some 2 - | .branch _ | .feature _ => some 0 - | .intrinsic _ argc => some argc + | .cgt | .bgt _ | .ble _ | .ble_un _ => some 2 + | .conv_i8 | .conv_i4 | .conv_u1 | .conv_u4 | .conv_u8 | .conv_u | .conv_i | .not => some 1 + | .add | .sub | .mul | .and | .or | .xor | .shl | .shr | .shr_un + | .clt | .clt_un | .cgt_un | .ceq => some 2 + | .brfalse _ | .brtrue _ | .switch _ => some 1 + | .blt_un _ | .bge_un _ | .bgt_un _ | .bge _ | .blt _ | .beq _ | .bne_un _ => some 2 + | .br _ => some 0 + -- Faulting arithmetic has its own arms in `step`; `CIL.step` would drop the fault. + | .div | .div_un | .rem | .rem_un | .add_ovf | .add_ovf_un | .sub_ovf | .sub_ovf_un + | .mul_ovf | .mul_ovf_un | .conv_ovf _ | .conv_ovf_un _ => none | _ => none /-- The operands of a value-only instruction, all of which must be numeric scalars. -/ @@ -116,65 +211,52 @@ inductive FrameAction where (temporary : Reference) (kind : CIL.LocalKind) (memory : Memory) | returned (result : List Value) (memory : Memory) -/-- Managed dispatch checks stable pointer identity and signature before selecting a body. - Tail calls use the same retirement action as direct tail calls. -/ -def managedStep (operation : CIL.ManagedOp) (pc : Nat) (frame : Frame) +/-- How a call-family instruction faults when its method's link table gives its pc no target, or + a target of another kind than the instruction enters. -/ +def unlinkedCall : ExecutionFault := .unsupported "Call site has no program target" + +/-- The catalog body an indirect call through `pointer` enters: the entry for the pointer's + target, when the pointer has the call site's signature; `none` for any other pointer. A + step proof rewrites this with a fact the kernel checks, so the elaborator never compares the + identities, whose strings it compares with recursion as deep as they are long. -/ +def indirectTarget (signature : CIL.ManagedSignature) (catalog : List CIL.ManagedTarget) + (pointer : CIL.MethodPointer) : Option Nat := + if pointer.signature != signature then none + else pointer.target.bind fun identity => + (catalog.find? (·.identity == identity)).map (·.method) + +/-- A selected body is one the catalog lists. -/ +theorem indirectTarget_mem {signature : CIL.ManagedSignature} {catalog : List CIL.ManagedTarget} + {pointer : CIL.MethodPointer} {method : Nat} + (selected : indirectTarget signature catalog pointer = some method) : + method ∈ catalog.map CIL.ManagedTarget.method := by + unfold indirectTarget at selected + split at selected + · cases selected + · cases target : pointer.target with + | none => simp [target] at selected + | some identity => + simp only [target, Option.bind] at selected + obtain ⟨entry, found, rfl⟩ := Option.map_eq_some_iff.mp selected + exact List.mem_map.mpr ⟨entry, List.mem_of_find?_eq_some found, rfl⟩ +#print axioms indirectTarget_mem + +/-- Managed dispatch checks stable pointer identity and signature before selecting a body from + the catalog `link`, the call site's entry in its method's link table. Tail calls use the same + retirement action as direct tail calls. -/ +def calliStep (signature : CIL.ManagedSignature) (tail : Bool) (link : Option CIL.Link) (frame : Frame) (stack : List Value) (memory : Memory) : ExecutionResult FrameAction := do - match operation, stack with - | .tableLength, .object .null :: _ - | .tableElement _, .scalar _ :: .object .null :: _ => - .error (.thrown "System.NullReferenceException") - | .address target signature, _ => - return .next (pc + 1) (.methodPointer ⟨signature, some target.identity⟩ :: stack) frame memory - | .tableLength, .methodTable table :: rest => - let some targets := table.targets | .error (.thrown "System.NullReferenceException") - if targets.size >= 2^31 then .error .invalidState else pure () - return .next (pc + 1) (.scalar (.i64 (BitVec.ofNat 64 targets.size)) :: rest) frame memory - | .tableElement signature, .scalar index :: .methodTable table :: rest => - if table.signature != signature then .error .invalidState else pure () - let some index := arrayIndex index | .error .invalidState - let some targets := table.targets | .error (.thrown "System.NullReferenceException") - if targets.size >= 2^31 then .error .invalidState else pure () - let some target := targets[index]? | .error (.thrown "System.IndexOutOfRangeException") - if let some backing := table.backing then - let selected := { table with backing := some { backing with - byteOffset := nativeMethodWidth * index } } - let pointer ← checkedAt .null nativeMethodWidth (loadNativeTable selected memory) - return .next (pc + 1) (.methodPointer pointer :: rest) frame memory - return .next (pc + 1) (.methodPointer ⟨signature, some target⟩ :: rest) frame memory - | .tableAddress signature, .scalar index :: .methodTable table :: rest => - if table.signature != signature then .error .invalidState else pure () - let some index := arrayIndex index | .error .invalidState - let some targets := table.targets | .error (.thrown "System.NullReferenceException") - if index >= targets.size then .error (.thrown "System.IndexOutOfRangeException") else pure () - let some backing := table.backing | .error .invalidState - let base := { table with backing := some { backing with byteOffset := 0 } } - let selected ← checkedAt .null nativeMethodWidth - (addNativeTable base memory (BitVec.ofNat 64 (nativeMethodWidth * index))) - return .next (pc + 1) (.methodTable selected :: rest) frame memory - | .tableOffset, .scalar offset :: .methodTable table :: rest - | .tableOffset, .methodTable table :: .scalar offset :: rest => - let offset ← match offset with - | .i32 bits => .ok (bits.signExtend 64) - | .i64 bits => .ok bits - | _ => .error .invalidState - let selected ← checkedAt .null nativeMethodWidth (addNativeTable table memory offset) - return .next (pc + 1) (.methodTable selected :: rest) frame memory - | .tableLoad signature, .methodTable table :: rest => - if table.signature != signature then .error .invalidState else pure () - let pointer ← checkedAt .null nativeMethodWidth (loadNativeTable table memory) - return .next (pc + 1) (.methodPointer pointer :: rest) frame memory - | .call signature catalog tail, .methodPointer pointer :: values => - if pointer.signature != signature then .error .invalidState else pure () - let some identity := pointer.target | .error .invalidState - let some target := catalog.find? (·.identity == identity) | .error .invalidState + match stack with + | .methodPointer pointer :: values => + let some (.indirect catalog) := link | .error unlinkedCall + let some method := indirectTarget signature catalog pointer | .error .invalidState let count := signature.parameters.length if (if tail then values.length != count else values.length < count) then .error .invalidState else pure () - let arguments ← (values.take count).reverse.mapM (checkedValue memory) - if tail then return .tailCall target.method arguments memory - else return .call target.method arguments (values.drop count) memory - | _, _ => .error .invalidState + let arguments ← (values.take count).reverse.mapM (callArgument frame memory) + if tail then return .tailCall method arguments memory + else return .call method arguments (values.drop count) memory + | _ => .error .invalidState /-- Evaluates an intrinsic. Operands with unspecified bits are allowed only where the result does not observe them; arithmetic faults are reported, not swallowed. -/ @@ -193,293 +275,831 @@ def checkedIntrinsic (operation : CIL.Intrinsic) (operands : List CIL.Value) : E | some value => .ok value | none => .error .invalidState +/-- A narrowed return extends one i32 from a value-returning method; a plain return checks + the declared arity and validates each returned value. Malformed stacks fault. -/ +def returnFrom (narrow : Option CIL.NarrowKind) (returnsValue : Bool) (stack : List Value) + (memory : Memory) : ExecutionResult FrameAction := + match narrow, stack with + | some kind, [.scalar (.i32 bits)] => + if returnsValue then .ok (.returned [.scalar (.i32 (kind.extend bits))] memory) + else .error .invalidState + | some _, _ => .error .invalidState + | none, _ => + if stack.length = (if returnsValue then 1 else 0) then do + return .returned (← stack.mapM (checkedValue memory)) memory + else .error .invalidState + +/-- The value `ldarg index` pushes: a homed argument's frame slot, or the incoming argument + narrowed as its declared type requires. An incoming value with unspecified vector bits is + never narrowed. -/ +def loadArgument (body : CIL.Method) (index : Nat) (args : List Value) (frame : Frame) + (memory : Memory) : ExecutionResult Value := + match body.argumentAccess index with + | .home slot => do + let some home := frame.locals[slot]? | .error .invalidState + loadLocal memory home + | .value none => do + let some value := args[index]? | .error .invalidState + checkedValue memory value + | .value (some kind) => do + let some value := args[index]? | .error .invalidState + if value.hasUnspecified then + .error (.unsupported "Instruction observes unspecified vector bits") else + let .scalar (.i32 bits) := value | .error .invalidState + checkedValue memory (.scalar (.i32 (kind.extend bits))) + +/-- The frame and memory after `starg index` stores `value`: a homed argument's frame slot is + written as `stloc` of that slot writes it. An argument without a home has no slot to write. -/ +def storeArgument (body : CIL.Method) (index : Nat) (value : Value) (frame : Frame) + (memory : Memory) : ExecutionResult (Frame × Memory) := + match body.argumentAccess index with + | .home slot => do + let some home := frame.locals[slot]? | .error .invalidState + let (home, memory) ← storeLocal memory home value + return ({ frame with locals := frame.locals.set slot home }, memory) + | .value _ => .error (.unsupported "Store to an argument without a home") + +/-- The address `ldarga index` pushes: a homed argument's frame slot, as `ldloca` of that slot + takes it. An argument without a home has no address. -/ +def addressArgument (body : CIL.Method) (index : Nat) (frame : Frame) (memory : Memory) : + ExecutionResult Value := + match body.argumentAccess index with + | .home slot => do + let some home := frame.locals[slot]? | .error .invalidState + localAddress memory slot home + | .value _ => .error (.unsupported "Address of an argument without a home") + +/-- Runs `next` unless one of the top `count` operands has unspecified bits, which `step` rejects + with the same fault before every instruction. -/ +def observedCall (count : Nat) (stack : List Value) (next : Unit → ExecutionResult FrameAction) : + ExecutionResult FrameAction := + if unspecifiedOperands count stack then + .error (.unsupported "Instruction observes unspecified vector bits") else next () + +/-- An intrinsic must be available in `profile`; its `argc` operands are taken deepest first. -/ +def intrinsicCall (profile : CIL.FeatureProfile) (operation : CIL.Intrinsic) (argc pc : Nat) + (frame : Frame) (stack : List Value) (memory : Memory) : ExecutionResult FrameAction := + if !operation.available profile then + .error (.unsupported "Intrinsic unavailable in target profile") + else if stack.length < argc then .error .invalidState else do + let operands ← scalars (stack.take argc).reverse + let value ← checkedIntrinsic operation operands + return .next (pc + 1) ((← checkedValue memory (.scalar value)) :: stack.drop argc) frame memory + +/-- A feature test pushes 1 when `profile` has `feature`, and 0 otherwise. -/ +def featureCall (profile : CIL.FeatureProfile) (feature : CIL.Feature) (pc : Nat) (frame : Frame) + (stack : List Value) (memory : Memory) : ExecutionResult FrameAction := + .ok (.next (pc + 1) (.scalar (.i32 (if profile.evaluate feature then 1 else 0)) :: stack) frame + memory) + +/-- Two closed types compare when their scoped metadata establishes their runtime identity. -/ +def runtimeTypeEqual (left right : CIL.Extraction.TypeExpr) : ExecutionResult Bool := + match CIL.Extraction.sameRuntimeType left right with + | some equal => .ok equal + | none => .error (.unsupported "Type comparison needs a resolved type identity") + +/-- A reference comparison pushes 1 when the two references, deepest first, stand in + `relation`, and 0 otherwise; a readonly reference compares as its address. -/ +def referenceCompareCall (relation : CIL.ReferenceRelation) (pc : Nat) (frame : Frame) + (stack : List Value) (memory : Memory) : ExecutionResult FrameAction := do + match stack with + | .reference right :: .reference left :: rest + | .reference right :: .readOnlyReference left :: rest + | .readOnlyReference right :: .reference left :: rest + | .readOnlyReference right :: .readOnlyReference left :: rest => + let holds ← match relation with + | .same => referenceEqual memory left right + | .less => referenceLess memory left right + | .greater => referenceLess memory right left + return .next (pc + 1) (.scalar (.i32 (if holds then 1 else 0)) :: rest) frame memory + | _ => .error .invalidState + +/-- A reference below a byte offset moves back by that offset and keeps its readonly + restriction. -/ +def referenceSubtractCall (pc : Nat) (frame : Frame) (stack : List Value) (memory : Memory) : + ExecutionResult FrameAction := do + match stack with + | .scalar offset :: .reference value :: rest => + let result ← referenceSubtract memory value offset + return .next (pc + 1) (.reference result :: rest) frame memory + | .scalar offset :: .readOnlyReference value :: rest => + let result ← referenceSubtract memory value offset + return .next (pc + 1) (.readOnlyReference result :: rest) frame memory + | _ => .error .invalidState + +/-- An unaligned transfer of `kind` runs as its typed load or store. -/ +def typedTransferCall (kind : CIL.TransferKind) (store : Bool) (pc : Nat) (frame : Frame) + (stack : List Value) (memory : Memory) : ExecutionResult FrameAction := do + let (memory, values) ← observedTransfer kind store stack memory + return .next (pc + 1) values frame memory + +/-- `Unsafe.SkipInit` pops its out reference once it forms; nothing is written. A non-reference operand + gives an unsupported result. -/ +def skipInitCall (pc : Nat) (frame : Frame) (stack : List Value) (memory : Memory) : + ExecutionResult FrameAction := do + match stack with + | .reference reference :: rest => + let _ ← formValue memory reference + return .next (pc + 1) rest frame memory + | _ => .error (.unsupported "Not a memory instruction") + +/-- A span constructor run in place: `constructor` builds the span from the arguments, and the + slot named by the home beneath them then holds it. The constructor must build spans of the + slot's width and access. A view or any other receiver names no slot, so the call faults. -/ +def spanInPlaceCall (constructor : CIL.SpanConstructor) (pc : Nat) (frame : Frame) + (stack : List Value) (memory : Memory) : ExecutionResult FrameAction := do + let (memory, values) ← memoryInstruction constructor.operation stack memory + match values with + | value :: .spanHome index :: rest => + let some (.span width writable view) := frame.locals[index]? | .error .invalidState + if constructor.shape != (width, writable) then + .error (.unsupported "Span constructor does not build the span of its home") else pure () + let (slot, memory) ← storeLocal memory (.span width writable view) value + return .next (pc + 1) rest { frame with locals := frame.locals.set index slot } memory + | _ => .error .invalidState + +/-- Executes `model` as the call at `pc` of `body`. The caller applies `observedCall` with + `model.observerCount` first. On the stacks a model admits this equals `step` on the + instruction emitted for the call, and it refuses every other stack. -/ +def runModel (body : CIL.Method) (model : CIL.RuntimeModel) (pc : Nat) (frame : Frame) + (stack : List Value) (memory : Memory) : ExecutionResult FrameAction := + match model with + | .intrinsic operation argc => intrinsicCall body.profile operation argc pc frame stack memory + | .memory operation => do + let operands ← viewOperands frame memory (viewOperandCount operation) stack + let (memory, values) ← memoryInstruction operation operands memory + return .next (pc + 1) values frame memory + | .feature feature => featureCall body.profile feature pc frame stack memory + | .constant value => .ok (.next (pc + 1) (.scalar (.i32 value) :: stack) frame memory) + | .referenceCompare relation => referenceCompareCall relation pc frame stack memory + | .referenceSubtract => referenceSubtractCall pc frame stack memory + | .typedTransfer kind store => typedTransferCall kind store pc frame stack memory + | .skipInit => skipInitCall pc frame stack memory + | .typeFromHandle => match stack with + | .typeHandle type :: rest => .ok (.next (pc + 1) (.runtimeType type :: rest) frame memory) + | _ => .error (.unsupported "GetTypeFromHandle requires a type handle") + | .typeEquality equal => match stack with + | .runtimeType right :: .runtimeType left :: rest => do + let same ← runtimeTypeEqual left right + return .next (pc + 1) (.scalar (.i32 (if same == equal then 1 else 0)) :: rest) frame memory + | _ => .error (.unsupported "Type comparison requires two runtime types") + | .objectConstructor => match stack with + | _ :: rest => .ok (.next (pc + 1) rest frame memory) + | [] => .error (.unsupported "Not a memory instruction") + | .spanInPlace constructor => spanInPlaceCall constructor pc frame stack memory + +/-- A call whose pc states no link runs `model`, the runtime model its method stands for, after + checking the operands the model observes; without one it faults with `unlinkedCall`. -/ +def modelStep (model : Option CIL.RuntimeModel) (body : CIL.Method) (pc : Nat) (frame : Frame) + (stack : List Value) (memory : Memory) : ExecutionResult FrameAction := + match model with + | some model => + observedCall model.observerCount stack fun _ => runModel body model pc frame stack memory + | none => .error unlinkedCall + +/-- A store into a span slot leaves a span slot of the same width and access. -/ +theorem storeLocal_span_slot {memory after : Memory} {width : Nat} {writable : Bool} + {view : Option SpanView} {value : Value} {slot : LocalSlot} + (stored : storeLocal memory (.span width writable view) value = .ok (slot, after)) : + ∃ newView, slot = .span width writable (some newView) := by + unfold storeLocal at stored + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at stored + repeat' (first | contradiction | split at stored) + all_goals (cases stored; simp_all) + +#print axioms storeLocal_span_slot + +/-- A span constructor run in place continues at the next pc with only the span slot its home + names changed, to a span of the slot's width and access, which the constructor builds. -/ +theorem spanInPlaceCall_frame {constructor : CIL.SpanConstructor} {pc : Nat} {frame : Frame} + {stack : List Value} {memory : Memory} {action : FrameAction} + (executed : spanInPlaceCall constructor pc frame stack memory = .ok action) : + ∃ values final index width writable view newView, + constructor.shape = (width, writable) ∧ + frame.locals[index]? = some (.span width writable view) ∧ + action = .next (pc + 1) values + { frame with locals := frame.locals.set index (.span width writable (some newView)) } + final := by + unfold spanInPlaceCall at executed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' (first | contradiction | split at executed) + all_goals + cases executed + obtain ⟨newView, slotIs⟩ := storeLocal_span_slot ‹storeLocal _ _ _ = _› + rw [slotIs] + refine ⟨_, _, _, _, _, _, newView, ?_, ‹frame.locals[_]? = some _›, rfl⟩ + simp_all + +#print axioms spanInPlaceCall_frame + +/-- A call's runtime model continues at the next pc. The frame stays the same, except that a span + constructor run in place sets the span slot its home names to a span of the slot's width and + access, which the constructor builds. -/ +theorem modelStep_frame {model : Option CIL.RuntimeModel} {body : CIL.Method} {pc : Nat} + {frame : Frame} {stack : List Value} {memory : Memory} {action : FrameAction} + (executed : modelStep model body pc frame stack memory = .ok action) : + ∃ values final changed, action = .next (pc + 1) values changed final ∧ + (changed = frame ∨ ∃ constructor index width writable view newView, + model = some (.spanInPlace constructor) ∧ constructor.shape = (width, writable) ∧ + frame.locals[index]? = some (.span width writable view) ∧ + changed = { frame with + locals := frame.locals.set index (.span width writable (some newView)) }) := by + unfold modelStep observedCall runModel intrinsicCall featureCall referenceCompareCall + referenceSubtractCall typedTransferCall skipInitCall at executed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' first + | exact ⟨_, _, _, (Except.ok.inj executed).symm, .inl rfl⟩ + | (obtain ⟨_, _, _, _, _, _, _, shaped, held, rfl⟩ := spanInPlaceCall_frame executed + exact ⟨_, _, _, rfl, .inr ⟨_, _, _, _, _, _, by first | rfl | simp_all, shaped, held, rfl⟩⟩) + | cases executed + | split at executed + +#print axioms modelStep_frame + +/-- A call's runtime model reads its body only through the profile. -/ +theorem modelStep_profile_congr {moved body : CIL.Method} (profile : moved.profile = body.profile) + (model : Option CIL.RuntimeModel) (pc : Nat) (frame : Frame) (stack : List Value) + (memory : Memory) : + modelStep model moved pc frame stack memory = modelStep model body pc frame stack memory := by + simp only [modelStep, runModel, profile] + +#print axioms modelStep_profile_congr + +/-- `ldfld` of `field` as `body` resolves it: a class slot through the receiver, or a struct + field at its checked offset and width through a reference or out of a struct value. A field + outside the checked layouts and class slots faults with its reason. -/ +def fieldLoad (body : CIL.Method) (field : CIL.Extraction.FieldRef) (stack : List Value) + (memory : Memory) : ExecutionResult (Memory × List Value) := + match body.fieldShape field with + | .classSlot offset slot => classFieldLoad field.owner offset slot stack memory + | .value offset width signed => loadValueField offset width signed stack memory + | .aggregate offset size => loadAggregateField offset size stack memory + | .unsupported reason => .error (.unsupported reason) + +/-- `stfld` of `field` as `body` resolves it: a class slot through the receiver, or a struct + field at its checked offset and width through a writable reference; faults as `fieldLoad`. -/ +def fieldStore (body : CIL.Method) (field : CIL.Extraction.FieldRef) (stack : List Value) + (memory : Memory) : ExecutionResult (Memory × List Value) := + match body.fieldShape field with + | .classSlot offset slot => classFieldStore field.owner offset slot stack memory + | .value offset width _ => storeValueField offset width stack memory + | .aggregate offset size => storeAggregateField offset size stack memory + | .unsupported reason => .error (.unsupported reason) + +/-- `ldflda` of `field` as `body` resolves it: the address of a struct field at its checked + offset, read-only through a read-only reference. A class field's address is never formed; the + extractor refuses one at admission, so only a hand-written body steps one. -/ +def fieldReference (body : CIL.Method) (field : CIL.Extraction.FieldRef) (stack : List Value) + (memory : Memory) : ExecutionResult (Memory × List Value) := + match body.fieldShape field with + | .value offset .. | .aggregate offset _ => structFieldAddress offset stack memory + | .classSlot .. => .error (.unsupported "Class field address") + | .unsupported reason => .error (.unsupported reason) + +/-- `localloc` of the byte count alone on the stack: fresh storage owned by `frame`, zeroed when + `initLocals` is set. Any other stack is an invalid state. -/ +def localAllocation (initLocals : Bool) (pc : Nat) (frame : Frame) (stack : List Value) + (memory : Memory) : ExecutionResult FrameAction := do + let [.scalar size] := stack | .error .invalidState + let bytes ← match size with + | .i32 bits => pure bits.toNat + | .i64 bits => pure bits.toNat + | _ => .error .invalidState + let (reference, owner, memory) ← allocateStack memory frame bytes initLocals + return .next (pc + 1) [.reference reference] owner memory + +/-- `newarr` of `length` elements of `shape`: an inline value array or an array of null class + references; a method-table or unsupported shape allocates nothing. -/ +def allocateElements (shape : ElementShape) (length : CIL.Value) (memory : Memory) : + ExecutionResult (Value × Memory) := do + match shape with + | .value element => + let (view, memory) ← newArray memory element length + return (.array view, memory) + | .reference element => + let (array, memory) ← newReferenceArray memory element length + return (.object (.address array), memory) + | .method _ => .error (.unsupported ElementShape.methodAllocation) + | .unsupported reason => .error (.unsupported reason) + +/-- `initobj` of a span through the home on top of the stack: its slot then holds the empty view + of the slot's width and access. Any other operand has no width to initialize. -/ +def initializeSpanHome (pc : Nat) (frame : Frame) (stack : List Value) (memory : Memory) : + ExecutionResult FrameAction := + match stack with + | .spanHome index :: rest => do + let some (.span width writable view) := frame.locals[index]? | .error .invalidState + let (slot, memory) ← storeLocal memory (.span width writable view) + (.bufferSpan ⟨.null, 0, width, writable⟩) + return .next (pc + 1) rest { frame with locals := frame.locals.set index slot } memory + | _ => .error (.unsupported "Initialization width") + +/-- `newobj` of a value type: a zeroed temporary of `size` bytes owned by `frame`, passed by + address to `callee` ahead of the `argc` arguments on the stack; the driver pushes the + temporary's value once the constructor returns. -/ +def constructAggregate (callee argc : Nat) (size : Fin (2^64)) (frame : Frame) + (stack : List Value) (m : Memory) : ExecutionResult FrameAction := do + if size = 0 || stack.length < argc then .error .invalidState else + let arguments ← (stack.take argc).reverse.mapM (callArgument frame m) + let (temporary, memory) ← allocateHome m frame.activation size + let (_, memory) ← storeLocal memory (.bytes (.aggregate size) temporary) + (.scalar (.aggregate (List.replicate size 0))) + let frame := { frame with owned := temporary.allocation :: frame.owned } + return .construct callee (.reference (.address temporary) :: arguments) + (stack.drop argc) frame temporary (.aggregate size) memory + +/-- `newobj` of a class: a fresh zeroed instance of `type` with `size` bytes of fields, passed + to `constructor` as its receiver ahead of the `argc` arguments on the stack, and left on the + caller's stack for when the constructor returns. -/ +def constructObject (constructor argc : Nat) (type : RuntimeType) (size : Nat) (frame : Frame) + (stack : List Value) (m : Memory) : ExecutionResult FrameAction := do + if stack.length < argc then .error .invalidState else + let arguments ← (stack.take argc).reverse.mapM (callArgument frame m) + let (object, memory) ← allocateObject m type size + let created := Value.object (.address object) + return .call constructor (created :: arguments) (created :: stack.drop argc) memory + +/-- Construction uses the linked body or the runtime model for the explicit arguments. -/ +def newobjStep (link : Option CIL.Link) (method : CIL.Extraction.MethodRef) + (body : CIL.Method) (pc : Nat) (frame : Frame) (stack : List Value) (m : Memory) : + ExecutionResult FrameAction := + let argc := method.parameters.length + let owner := method.owner + match link with + | some (.aggregate callee size) => constructAggregate callee argc size frame stack m + | some (.object constructor bases size) => + constructObject constructor argc ⟨owner, bases⟩ size frame stack m + | none => modelStep (CIL.Extraction.runtimeModel body.environment .newobj method) body pc + frame stack m + | _ => .error unlinkedCall + /-- Executes one instruction of `body`. Instructions that would observe unspecified vector - bits are rejected before dispatch. -/ -def step (body : CIL.Method) (op : CIL.Op) (pc : Nat) (args : List Value) + bits are rejected before dispatch, and a call in the arm of its link. -/ +def step (body : CIL.Method) (op : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) : ExecutionResult FrameAction := do - let unknownArgument := match op with - | .arg index (some _) => (args[index]?.map Value.hasUnspecified).getD false - | _ => false - if unspecifiedOperands (observerCount op) stack || unknownArgument then + if unspecifiedOperands (observerCount op) stack then .error (.unsupported "Instruction observes unspecified vector bits") else + -- Native table offsets, faulting arithmetic, object identity, type tests, instance fields, + -- argument homes, `localloc`, `newarr` and `newobj` are matched apart, so neither match exceeds + -- the 79 alternatives `split` handles. match op, stack with - | .managed operation, _ => managedStep operation pc frame stack m - | .sub, .scalar offset :: .reference value :: rest => - let result ← referenceSubtract m value offset - return .next (pc + 1) (.reference result :: rest) frame m - | .sub, .scalar offset :: .readOnlyReference value :: rest => - let result ← referenceSubtract m value offset - return .next (pc + 1) (.readOnlyReference result :: rest) frame m + -- A native table offset is a byte count; the cursor keeps the table's identities. + | .add, .scalar offset :: .methodTable table :: rest + | .add, .methodTable table :: .scalar offset :: rest => + let offset ← match offset with + | .i32 bits => .ok (bits.signExtend 64) + | .i64 bits => .ok bits + | _ => .error .invalidState + let selected ← checkedAt .null nativeMethodWidth (addNativeTable table m offset) + return .next (pc + 1) (.methodTable selected :: rest) frame m + -- One arm per overflow-checked arithmetic constructor, flags as `checkedKind?` gives them. + | .add_ovf, .scalar y :: .scalar x :: rest => + let value ← (CIL.checkedValues .add true x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .add_ovf_un, .scalar y :: .scalar x :: rest => + let value ← (CIL.checkedValues .add false x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .sub_ovf, .scalar y :: .scalar x :: rest => + let value ← (CIL.checkedValues .sub true x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .sub_ovf_un, .scalar y :: .scalar x :: rest => + let value ← (CIL.checkedValues .sub false x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .mul_ovf, .scalar y :: .scalar x :: rest => + let value ← (CIL.checkedValues .mul true x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .mul_ovf_un, .scalar y :: .scalar x :: rest => + let value ← (CIL.checkedValues .mul false x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + -- One arm per division constructor, flags as `divideKind?` gives them. + | .div, .scalar y :: .scalar x :: rest => + let value ← (CIL.divideValues true false x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .div_un, .scalar y :: .scalar x :: rest => + let value ← (CIL.divideValues false false x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .rem, .scalar y :: .scalar x :: rest => + let value ← (CIL.divideValues true true x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .rem_un, .scalar y :: .scalar x :: rest => + let value ← (CIL.divideValues false true x y).mapError ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + -- Overflow-checked conversions, reading the operand as signed or as unsigned. + | .conv_ovf target, .scalar x :: rest => + let value ← (CIL.convertInteger target.width target.signed false x).mapError + ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .conv_ovf_un target, .scalar x :: rest => + let value ← (CIL.convertInteger target.width target.signed true x).mapError + ExecutionFault.arithmetic + return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m + | .ldtoken (.type type), _ => .ok (.next (pc + 1) (.typeHandle type :: stack) frame m) + | .ceq, .runtimeType right :: .runtimeType left :: rest => do + let same ← runtimeTypeEqual left right + return .next (pc + 1) (.scalar (.i32 (if same then 1 else 0)) :: rest) frame m + | .beq target, .runtimeType right :: .runtimeType left :: rest => do + let same ← runtimeTypeEqual left right + return .next (if same then target else pc + 1) rest frame m + | .bne_un target, .runtimeType right :: .runtimeType left :: rest => do + let same ← runtimeTypeEqual left right + return .next (if same then pc + 1 else target) rest frame m + -- Object identity and null tests; `x != null` compiles to `cgt.un` against null. + | .ceq, .object right :: .object left :: rest => + let equal ← objectEqual m left right + return .next (pc + 1) (.scalar (.i32 (if equal then 1 else 0)) :: rest) frame m + | .beq target, .object right :: .object left :: rest => + let equal ← objectEqual m left right + return .next (if equal then target else pc + 1) rest frame m + | .bne_un target, .object right :: .object left :: rest => + let equal ← objectEqual m left right + return .next (if equal then pc + 1 else target) rest frame m + | .cgt_un, .object right :: .object left :: rest => + let present ← objectAboveNull m left right + return .next (pc + 1) (.scalar (.i32 (if present then 1 else 0)) :: rest) frame m + | .brfalse target, .object value :: rest => + let present ← objectNonNull m value + return .next (if present then pc + 1 else target) rest frame m + | .brtrue target, .object value :: rest => + let present ← objectNonNull m value + return .next (if present then target else pc + 1) rest frame m + -- Class type tests: null and an instance of `type` pass; another object fails the test. + | .castclass type, .object value :: rest => + let result ← objectTest m type true value + return .next (pc + 1) (.object result :: rest) frame m + | .isinst type, .object value :: rest => + let result ← objectTest m type false value + return .next (pc + 1) (.object result :: rest) frame m + -- Instance fields, as the body's layout environment resolves them. + | .ldfld field, _ => + let (memory, stack) ← fieldLoad body field stack m + return .next (pc + 1) stack frame memory + | .ldflda field, _ => + let (memory, stack) ← fieldReference body field stack m + return .next (pc + 1) stack frame memory + | .stfld field, _ => + let (memory, stack) ← fieldStore body field stack m + return .next (pc + 1) stack frame memory + -- An argument's home is written and addressed as the local slot it is. + | .starg index, value :: rest => + let (frame, memory) ← storeArgument body index value frame m + return .next (pc + 1) rest frame memory + | .ldarga index, _ => + return .next (pc + 1) ((← addressArgument body index frame m) :: stack) frame m + + -- Dynamic frame storage, zeroed as the body's declared localsinit flag says. + | .localloc, _ => localAllocation body.declared.initLocals pc frame stack m + -- Static fields resolve the descriptor extraction bound to their pc. + | .ldsfld _, _ => + let (memory, value) ← staticLoad body pc m + return .next (pc + 1) (value :: stack) frame memory + | .ldsflda _, _ | .ldtoken (.field _), _ => + let (memory, value) ← staticAddress body pc m + return .next (pc + 1) (value :: stack) frame memory + | .stsfld _, _ => .error (.unsupported "Static field store") + -- A fresh array of the storage the layouts give its declared element type. + | .newarr element, .scalar length :: rest => + let (array, memory) ← allocateElements (body.elementShape element) length m + return .next (pc + 1) (array :: rest) frame memory + -- Object transfers use the checked shape of the instruction's closed type operand. A span load + -- reads a home as the view its slot holds when the load runs. + | .ldobj type, _ => + let operands ← viewOperands frame m (if Extraction.spanLocal type then 1 else 0) stack + let (memory, values) ← transferObject (body.valueShape type) false operands m + return .next (pc + 1) values frame memory + | .stobj type, _ => + let (memory, values) ← transferObject (body.valueShape type) true stack m + return .next (pc + 1) values frame memory + | .cpobj type, _ => + let some size := (body.valueShape type).size? | .error (.unsupported "Typed copy width") + let (memory, values) ← copyInitializedBytes size stack m + return .next (pc + 1) values frame memory + | .initobj type, _ => + let some size := (body.valueShape type).size? | match body.valueShape type with + | .span => initializeSpanHome pc frame stack m + | _ => .error (.unsupported "Initialization width") + let (memory, values) ← initializeBytes size stack m + return .next (pc + 1) values frame memory + | .sizeof type, _ => + let some size := (body.environment.callLayouts.valueShape type).size? + | .error (.unsupported "Object size") + return .next (pc + 1) (.scalar (.i32 (BitVec.ofNat 32 size)) :: stack) frame m + -- Typed element accesses take the storage the layouts give their declared element type. + | .ldelem_any element, _ => + let (memory, values) ← typedElementInstruction (body.elementShape element) false stack m + return .next (pc + 1) values frame memory + | .stelem_any element, _ => + let (memory, values) ← typedElementInstruction (body.elementShape element) true stack m + return .next (pc + 1) values frame memory + | .ldelema element readOnly, _ => + let (memory, values) ← elementAddressInstruction (body.elementShape element) readOnly stack m + return .next (pc + 1) values frame memory + -- A call enters the body its pc links to once its method's arguments are specified; a pc with + -- no link runs the runtime model its operand names, which checks the operands it observes and + -- leaves no frame for a `tail.` call to retire. + | .call method prefixes, _ => + match body.linkAt pc with + | none => + if prefixes.tail then .error unlinkedCall else + modelStep (CIL.Extraction.runtimeModel body.environment .call method) body pc frame stack m + | some link => observedCall method.argumentCount stack fun _ => do + match link with + | .direct callee => + if prefixes.tail then + if stack.length != method.argumentCount then .error .invalidState else + return .tailCall callee (← stack.reverse.mapM (callArgument frame m)) m + else if stack.length < method.argumentCount then .error .invalidState else + let arguments ← (stack.take method.argumentCount).reverse.mapM (callArgument frame m) + return .call callee arguments (stack.drop method.argumentCount) m + | _ => .error unlinkedCall + -- A construction allocates what the link at its pc states and enters the constructor it names. + | .newobj constructor, _ => + newobjStep (body.linkAt pc) constructor body pc frame stack m + -- A hole faults on every stack. + | .unadmitted _, _ => .error (.unsupported "Opcode not admitted") + | _, _ => + match op, stack with + | .ldftn identity signature, _ => + return .next (pc + 1) (.methodPointer ⟨signature, some identity⟩ :: stack) frame m + | .calli signature tail, _ => calliStep signature tail (body.linkAt pc) frame stack m + | .add, .scalar _ :: .reference _ :: _ + | .add, .reference _ :: .scalar _ :: _ + | .add, .scalar _ :: .readOnlyReference _ :: _ + | .add, .readOnlyReference _ :: .scalar _ :: _ => + let (memory, values) ← referenceAddInstruction 1 true stack m + return .next (pc + 1) values frame memory + | .sub, .scalar _ :: .reference _ :: _ + | .sub, .scalar _ :: .readOnlyReference _ :: _ => referenceSubtractCall pc frame stack m | .sub, .reference right :: .reference left :: rest | .sub, .reference right :: .readOnlyReference left :: rest | .sub, .readOnlyReference right :: .reference left :: rest | .sub, .readOnlyReference right :: .readOnlyReference left :: rest => let result ← referenceDifference m left right return .next (pc + 1) (.scalar (.i64 result) :: rest) frame m - | .ltu, .reference right :: .reference left :: rest - | .ltu, .reference right :: .readOnlyReference left :: rest - | .ltu, .readOnlyReference right :: .reference left :: rest - | .ltu, .readOnlyReference right :: .readOnlyReference left :: rest => - let less ← referenceLess m left right - return .next (pc + 1) (.scalar (.i32 (if less then 1 else 0)) :: rest) frame m - | .gtu, .reference right :: .reference left :: rest - | .gtu, .reference right :: .readOnlyReference left :: rest - | .gtu, .readOnlyReference right :: .reference left :: rest - | .gtu, .readOnlyReference right :: .readOnlyReference left :: rest => - let less ← referenceLess m right left - return .next (pc + 1) (.scalar (.i32 (if less then 1 else 0)) :: rest) frame m - | .bltu target, .reference right :: .reference left :: rest - | .bltu target, .reference right :: .readOnlyReference left :: rest - | .bltu target, .readOnlyReference right :: .reference left :: rest - | .bltu target, .readOnlyReference right :: .readOnlyReference left :: rest => + | .clt_un, .reference _ :: .reference _ :: _ + | .clt_un, .reference _ :: .readOnlyReference _ :: _ + | .clt_un, .readOnlyReference _ :: .reference _ :: _ + | .clt_un, .readOnlyReference _ :: .readOnlyReference _ :: _ => + referenceCompareCall .less pc frame stack m + | .cgt_un, .reference _ :: .reference _ :: _ + | .cgt_un, .reference _ :: .readOnlyReference _ :: _ + | .cgt_un, .readOnlyReference _ :: .reference _ :: _ + | .cgt_un, .readOnlyReference _ :: .readOnlyReference _ :: _ => + referenceCompareCall .greater pc frame stack m + | .blt_un target, .reference right :: .reference left :: rest + | .blt_un target, .reference right :: .readOnlyReference left :: rest + | .blt_un target, .readOnlyReference right :: .reference left :: rest + | .blt_un target, .readOnlyReference right :: .readOnlyReference left :: rest => let less ← referenceLess m left right return .next (if less then target else pc + 1) rest frame m - | .bgeu target, .reference right :: .reference left :: rest - | .bgeu target, .reference right :: .readOnlyReference left :: rest - | .bgeu target, .readOnlyReference right :: .reference left :: rest - | .bgeu target, .readOnlyReference right :: .readOnlyReference left :: rest => + | .bge_un target, .reference right :: .reference left :: rest + | .bge_un target, .reference right :: .readOnlyReference left :: rest + | .bge_un target, .readOnlyReference right :: .reference left :: rest + | .bge_un target, .readOnlyReference right :: .readOnlyReference left :: rest => let less ← referenceLess m left right return .next (if less then pc + 1 else target) rest frame m - | .bgtu target, .reference right :: .reference left :: rest - | .bgtu target, .reference right :: .readOnlyReference left :: rest - | .bgtu target, .readOnlyReference right :: .reference left :: rest - | .bgtu target, .readOnlyReference right :: .readOnlyReference left :: rest => + | .bgt_un target, .reference right :: .reference left :: rest + | .bgt_un target, .reference right :: .readOnlyReference left :: rest + | .bgt_un target, .readOnlyReference right :: .reference left :: rest + | .bgt_un target, .readOnlyReference right :: .readOnlyReference left :: rest => let less ← referenceLess m right left return .next (if less then target else pc + 1) rest frame m - | .bleu target, .reference right :: .reference left :: rest - | .bleu target, .reference right :: .readOnlyReference left :: rest - | .bleu target, .readOnlyReference right :: .reference left :: rest - | .bleu target, .readOnlyReference right :: .readOnlyReference left :: rest => + | .ble_un target, .reference right :: .reference left :: rest + | .ble_un target, .reference right :: .readOnlyReference left :: rest + | .ble_un target, .readOnlyReference right :: .reference left :: rest + | .ble_un target, .readOnlyReference right :: .readOnlyReference left :: rest => let less ← referenceLess m right left return .next (if less then pc + 1 else target) rest frame m -- Reference observations preserve memory and do not grant byte access authority. - | .brzero target, .reference value :: rest - | .brzero target, .readOnlyReference value :: rest => + | .brfalse target, .reference value :: rest + | .brfalse target, .readOnlyReference value :: rest => let present ← referenceNonNull m value return .next (if present then pc + 1 else target) rest frame m - | .brnonzero target, .reference value :: rest - | .brnonzero target, .readOnlyReference value :: rest => + | .brtrue target, .reference value :: rest + | .brtrue target, .readOnlyReference value :: rest => let present ← referenceNonNull m value return .next (if present then target else pc + 1) rest frame m - | .eq, .reference right :: .reference left :: rest - | .eq, .reference right :: .readOnlyReference left :: rest - | .eq, .readOnlyReference right :: .reference left :: rest - | .eq, .readOnlyReference right :: .readOnlyReference left :: rest => - let equal ← referenceEqual m left right - return .next (pc + 1) (.scalar (.i32 (if equal then 1 else 0)) :: rest) frame m + | .ceq, .reference _ :: .reference _ :: _ + | .ceq, .reference _ :: .readOnlyReference _ :: _ + | .ceq, .readOnlyReference _ :: .reference _ :: _ + | .ceq, .readOnlyReference _ :: .readOnlyReference _ :: _ => + referenceCompareCall .same pc frame stack m | .beq target, .reference right :: .reference left :: rest | .beq target, .reference right :: .readOnlyReference left :: rest | .beq target, .readOnlyReference right :: .reference left :: rest | .beq target, .readOnlyReference right :: .readOnlyReference left :: rest => let equal ← referenceEqual m left right return .next (if equal then target else pc + 1) rest frame m - | .bne target, .reference right :: .reference left :: rest - | .bne target, .reference right :: .readOnlyReference left :: rest - | .bne target, .readOnlyReference right :: .reference left :: rest - | .bne target, .readOnlyReference right :: .readOnlyReference left :: rest => + | .bne_un target, .reference right :: .reference left :: rest + | .bne_un target, .reference right :: .readOnlyReference left :: rest + | .bne_un target, .readOnlyReference right :: .reference left :: rest + | .bne_un target, .readOnlyReference right :: .readOnlyReference left :: rest => let equal ← referenceEqual m left right return .next (if equal then pc + 1 else target) rest frame m - -- Object identity and null tests; `x != null` compiles to `cgt.un` against null. - | .eq, .object right :: .object left :: rest => - let equal ← objectEqual m left right - return .next (pc + 1) (.scalar (.i32 (if equal then 1 else 0)) :: rest) frame m - | .beq target, .object right :: .object left :: rest => - let equal ← objectEqual m left right - return .next (if equal then target else pc + 1) rest frame m - | .bne target, .object right :: .object left :: rest => - let equal ← objectEqual m left right - return .next (if equal then pc + 1 else target) rest frame m - | .gtu, .object right :: .object left :: rest => - let present ← objectAboveNull m left right - return .next (pc + 1) (.scalar (.i32 (if present then 1 else 0)) :: rest) frame m - | .brzero target, .object value :: rest => - let present ← objectNonNull m value - return .next (if present then pc + 1 else target) rest frame m - | .brnonzero target, .object value :: rest => - let present ← objectNonNull m value - return .next (if present then target else pc + 1) rest frame m - | .objectTest type throwing, .object value :: rest => - let result ← objectTest m type throwing value - return .next (pc + 1) (.object result :: rest) frame m - | .stackAlloc initLocals, [.scalar size] => - let bytes ← match size with - | .i32 bits => pure bits.toNat - | .i64 bits => pure bits.toNat - | _ => .error .invalidState - let (reference, owner, memory) ← allocateStack m frame bytes initLocals - return .next (pc + 1) [.reference reference] owner memory - | .intrinsic operation argc, _ => - if !operation.available body.profile then .error (.unsupported "Intrinsic unavailable in target profile") else - if stack.length < argc then .error .invalidState else - let operands ← scalars (stack.take argc).reverse - let value ← checkedIntrinsic operation operands - return .next (pc + 1) ((← checkedValue m (.scalar value)) :: stack.drop argc) frame m - | .nativeBinary operation, .scalar y :: .scalar x :: rest => - let (target, values) ← (CIL.nativeBinaryValues operation pc x y).mapError ExecutionFault.arithmetic - let values ← (values.map Value.scalar).mapM (checkedValue m) - return .next target (values ++ rest) frame m - | .checkedConvert width signed unsignedInput, .scalar x :: rest => - let value ← (CIL.convertInteger width signed unsignedInput x).mapError ExecutionFault.arithmetic - return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m - | .checkedArithmetic kind signed, .scalar y :: .scalar x :: rest => - let value ← (CIL.checkedValues kind signed x y).mapError ExecutionFault.arithmetic - return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m - | .divide signed remainder, .scalar y :: .scalar x :: rest => - let value ← (CIL.divideValues signed remainder x y).mapError ExecutionFault.arithmetic - return .next (pc + 1) ((← checkedValue m (.scalar value)) :: rest) frame m - | .arg i none, _ => - let some value := args[i]? | .error .invalidState - return .next (pc + 1) ((← checkedValue m value) :: stack) frame m - | .arg i (some kind), _ => - let some (.scalar (.i32 bits)) := args[i]? | .error .invalidState - return .next (pc + 1) ((← checkedValue m (.scalar (.i32 (kind.extend bits)))) :: stack) frame m - | .vectorArg i, _ => - let home ← argumentHome frame i - return .next (pc + 1) ((← loadLocal m home) :: stack) frame m - | .vectorArgAddr i, _ => - let home ← argumentHome frame i - return .next (pc + 1) ((← localAddress m home) :: stack) frame m - | .local i _, _ | .vectorLocal i, _ => + | .ldarg index, _ => + return .next (pc + 1) ((← loadArgument body index args frame m) :: stack) frame m + | .ldloc i, _ => let some slot := frame.locals[i]? | .error .invalidState return .next (pc + 1) ((← loadLocal m slot) :: stack) frame m - | .localAddr i, _ | .vectorLocalAddr i, _ => + | .ldloca i, _ => let some slot := frame.locals[i]? | .error .invalidState - return .next (pc + 1) ((← localAddress m slot) :: stack) frame m - | .setLocal i _, value :: rest | .setVectorLocal i, value :: rest => + return .next (pc + 1) ((← localAddress m i slot) :: stack) frame m + | .stloc i, value :: rest => let some slot := frame.locals[i]? | .error .invalidState let (slot, memory) ← storeLocal m slot value return .next (pc + 1) rest { frame with locals := frame.locals.set i slot } memory - | .initializeSpanLocal index constructor, _ => - let some slot := frame.locals[index]? | .error .invalidState - let (memory, values) ← memoryInstruction constructor.operation stack m - let value :: rest := values | .error .invalidState - let (slot, memory) ← storeLocal memory slot value - return .next (pc + 1) rest { frame with locals := frame.locals.set index slot } memory - | .tailCall callee argc, _ => - if stack.length != argc then .error .invalidState else - return .tailCall callee (← stack.reverse.mapM (checkedValue m)) m - | .call callee argc, _ => - if stack.length < argc then .error .invalidState else - let arguments ← (stack.take argc).reverse.mapM (checkedValue m) - return .call callee arguments (stack.drop argc) m - | .callInstance callee argc, _ => - if stack.length < argc then .error .invalidState else - let arguments ← (stack.take argc).reverse.mapM (checkedValue m) - -- The receiver is the deepest argument; anything but an object reference is ill-typed. - match arguments with - | .object (.address _) :: _ => return .call callee arguments (stack.drop argc) m - | .object .null :: _ => .error (.thrown "System.NullReferenceException") - | _ => .error .invalidState - | .callVirtual targets argc, _ => - if stack.length < argc then .error .invalidState else - let arguments ← (stack.take argc).reverse.mapM (checkedValue m) - match arguments with - | .object (.address receiver) :: _ => - return .call (← virtualTarget m targets receiver) arguments (stack.drop argc) m - | .object .null :: _ => .error (.thrown "System.NullReferenceException") - | _ => .error .invalidState - | .newVector256 callee argc, _ => - if stack.length < argc then .error .invalidState else - let arguments ← (stack.take argc).reverse.mapM (checkedValue m) - let (temporary, memory) ← allocateHome m frame.activation (localWidth .vector256) - let memory ← storeValue memory (.address temporary) (.v256 0) - let frame := { frame with owned := temporary.allocation :: frame.owned } - return .construct callee (.reference (.address temporary) :: arguments) - (stack.drop argc) frame temporary .vector256 memory - | .newAggregate callee argc size, _ => - if size = 0 || stack.length < argc then .error .invalidState else - let arguments ← (stack.take argc).reverse.mapM (checkedValue m) - let (temporary, memory) ← allocateHome m frame.activation size - let (_, memory) ← storeLocal memory (.bytes (.aggregate size) temporary) - (.scalar (.aggregate (List.replicate size 0))) - let frame := { frame with owned := temporary.allocation :: frame.owned } - return .construct callee (.reference (.address temporary) :: arguments) - (stack.drop argc) frame temporary (.aggregate size) memory - | .newObject constructor argc owner bases size, _ => - if stack.length < argc then .error .invalidState else - let arguments ← (stack.take argc).reverse.mapM (checkedValue m) - let (object, memory) ← allocateObject m ⟨owner, bases⟩ size - let created := Value.object (.address object) - return .call constructor (created :: arguments) (created :: stack.drop argc) memory - | .memory operation, _ => - let (memory, values) ← staticInstruction body pc operation stack m + -- Each receiver call and construction reads the method it enters from the link of its pc. + | .callvirt method, _ => + let argc := method.argumentCount + match body.linkAt pc with + | some (.direct callee) => + if stack.length < argc then .error .invalidState else + let arguments ← (stack.take argc).reverse.mapM (callArgument frame m) + -- The receiver is the deepest argument; anything but an object reference is ill-typed. + match arguments with + | .object (.address _) :: _ => return .call callee arguments (stack.drop argc) m + | .object .null :: _ => .error (.thrown .nullReference) + | _ => .error .invalidState + | some (.virtual targets) => + if stack.length < argc then .error .invalidState else + let arguments ← (stack.take argc).reverse.mapM (callArgument frame m) + match arguments with + | .object (.address receiver) :: _ => + return .call (← virtualTarget m targets receiver) arguments (stack.drop argc) m + | .object .null :: _ => .error (.thrown .nullReference) + | _ => .error .invalidState + | _ => .error unlinkedCall + | .cpblk alignment, _ => do + let (memory, values) ← copyBlockBytes alignment stack m + return .next (pc + 1) values frame memory + | .initblk alignment, _ => do + let (memory, values) ← initBlockBytes alignment stack m + return .next (pc + 1) values frame memory + | .ldstr text, _ => do + let (memory, values) ← stringLiteralInstruction text stack m return .next (pc + 1) values frame memory | .throw exception, _ => .error (.thrown exception) - | .ret (some kind), [.scalar (.i32 bits)] => - if body.returnsValue then - return .returned (← [.scalar (.i32 (kind.extend bits))].mapM (checkedValue m)) m - else .error .invalidState - | .ret none, _ => - if stack.length = (if body.returnsValue then 1 else 0) then - return .returned (← stack.mapM (checkedValue m)) m - else .error .invalidState - | .newArray element, .scalar length :: rest => - let (view, memory) ← newArray m element length - return .next (pc + 1) (.array view :: rest) frame memory - | .newReferenceArray element, .scalar length :: rest => - let (array, memory) ← newReferenceArray m element length - return .next (pc + 1) (.object (.address array) :: rest) frame memory + | .ret, _ => returnFrom body.returnNarrow body.returnsValue stack m | _, _ => match pureArity op with | some argc => if stack.length < argc then .error .invalidState else let values ← scalars (stack.take argc) let some (.next target result _) := - CIL.step op false pc [] 0 values (fun _ => none) body.profile | .error .invalidState + CIL.step op false none pc values (fun _ => none) body.profile + | .error .invalidState let result ← (result.map Value.scalar).mapM (checkedValue m) return .next target (result ++ stack.drop argc) frame m | none => let (memory, values) ← instruction op stack m return .next (pc + 1) values frame memory -/-- Every executed instruction goes through the checked dispatcher. Fuel has +/-- The interpreter over the methods `lookup` gives. Every executed instruction goes through the + checked dispatcher. Fuel has the same recursive call structure as the original interpreter. Constructor storage belongs to its caller, so expiration of the constructor's private frame does not destroy the value being constructed. Static instructions use extraction-bound field identities and readonly allocation storage. -/ -def run (program : CIL.Program) : Nat → Nat → Nat → List Value → Frame → +def runWith (lookup : Nat → Option CIL.Method) : Nat → Nat → Nat → List Value → Frame → List Value → Memory → Except LocatedFault (Memory × List Value) | 0, method, pc, _, _, _, _ => .error ⟨method, pc, .fuelExhausted⟩ | fuel + 1, method, pc, args, frame, stack, memory => do - let some body := program[method]? | .error ⟨method, pc, .invalidState⟩ + let some body := lookup method | .error ⟨method, pc, .invalidState⟩ let some op := body.code[pc]? | .error ⟨method, pc, .invalidState⟩ - let action ← (step body op pc args frame stack memory).mapError fun fault => ⟨method, pc, fault⟩ + let action ← (step body op pc args frame stack memory).mapError + fun fault => ⟨method, pc, fault⟩ match action with - | .next target stack frame memory => run program fuel method target args frame stack memory + | .next target stack frame memory => runWith lookup fuel method target args frame stack memory | .call callee arguments rest memory => - let some child := program[callee]? | .error ⟨method, pc, .invalidState⟩ - let (childFrame, memory) ← (enterFrame child arguments memory).mapError fun fault => ⟨method, pc, fault⟩ - let (memory, result) ← run program fuel callee 0 arguments childFrame [] memory - run program fuel method (pc + 1) args frame (result ++ rest) memory + let some child := lookup callee | .error ⟨method, pc, .invalidState⟩ + let (childFrame, memory) ← (enterFrame child arguments memory).mapError + fun fault => ⟨method, pc, fault⟩ + let (memory, result) ← runWith lookup fuel callee 0 arguments childFrame [] memory + runWith lookup fuel method (pc + 1) args frame (result ++ rest) memory | .tailCall callee arguments memory => - let some child := program[callee]? | .error ⟨method, pc, .invalidState⟩ + let some child := lookup callee | .error ⟨method, pc, .invalidState⟩ if child.returnsValue != body.returnsValue then .error ⟨method, pc, .invalidState⟩ else let memory := leaveFrame frame memory let arguments ← (arguments.mapM (checkedValue memory)).mapError fun fault => ⟨method, pc, fault⟩ let (childFrame, memory) ← (enterFrame child arguments memory).mapError fun fault => ⟨method, pc, fault⟩ - run program fuel callee 0 arguments childFrame [] memory + runWith lookup fuel callee 0 arguments childFrame [] memory | .construct callee arguments rest frame temporary kind memory => - let some child := program[callee]? | .error ⟨method, pc, .invalidState⟩ - let (childFrame, memory) ← (enterFrame child arguments memory).mapError fun fault => ⟨method, pc, fault⟩ - let (memory, result) ← run program fuel callee 0 arguments childFrame [] memory + let some child := lookup callee | .error ⟨method, pc, .invalidState⟩ + let (childFrame, memory) ← (enterFrame child arguments memory).mapError + fun fault => ⟨method, pc, fault⟩ + let (memory, result) ← runWith lookup fuel callee 0 arguments childFrame [] memory if !result.isEmpty then .error ⟨method, pc, .invalidState⟩ else let value ← (loadLocal memory (.bytes kind temporary)).mapError fun fault => ⟨method, pc, fault⟩ - run program fuel method (pc + 1) args frame (value :: rest) memory + runWith lookup fuel method (pc + 1) args frame (value :: rest) memory | .returned result memory => let memory := leaveFrame frame memory let result ← (result.mapM (checkedValue memory)).mapError fun fault => ⟨method, pc, fault⟩ return (memory, result) -/-- Calls `method` with checked `args` in a fresh frame. -/ -def invoke (program : CIL.Program) (fuel method : Nat) (args : List Value) (m : Memory) : - Except LocatedFault (Memory × List Value) := do - let some body := program[method]? | .error ⟨method, 0, .invalidState⟩ +/-- Calls the method `lookup` gives at `method` with checked `args` in a fresh frame. -/ +def invokeWith (lookup : Nat → Option CIL.Method) (fuel method : Nat) (args : List Value) + (m : Memory) : Except LocatedFault (Memory × List Value) := do + let some body := lookup method | .error ⟨method, 0, .invalidState⟩ let args ← (args.mapM (checkedValue m)).mapError fun fault => ⟨method, 0, fault⟩ let (frame, memory) ← (enterFrame body args m).mapError fun fault => ⟨method, 0, fault⟩ - run program fuel method 0 args frame [] memory + runWith lookup fuel method 0 args frame [] memory + +/-- The interpreter over `program`, reading each method by its index in the list. A proof that + evaluates it rewrites by `run_eq_runWith` to read an index of the program instead. -/ +def run (program : CIL.Program) : Nat → Nat → Nat → List Value → Frame → List Value → Memory → + Except LocatedFault (Memory × List Value) := + runWith (program[·]?) + +/-- Calls `method` of `program` with checked `args` in a fresh frame. -/ +def invoke (program : CIL.Program) (fuel method : Nat) (args : List Value) (m : Memory) : + Except LocatedFault (Memory × List Value) := + invokeWith (program[·]?) fuel method args m + +/-- A checked execution with a decidable result can be compared by its fault or return value. -/ +instance {α : Type} [DecidableEq α] : DecidableEq (Except LocatedFault α) + | .ok left, .ok right => decidable_of_iff (left = right) ⟨congrArg _, Except.ok.inj⟩ + | .error left, .error right => + decidable_of_iff (left = right) ⟨congrArg _, Except.error.inj⟩ + | .ok _, .error _ | .error _, .ok _ => isFalse (by intro equal; cases equal) + +/-- Out of fuel, `run` stops at the method and pc it would execute. -/ +theorem run_zero (program : CIL.Program) (method pc : Nat) (args : List Value) (frame : Frame) + (stack : List Value) (memory : Memory) : + run program 0 method pc args frame stack memory = .error ⟨method, pc, .fuelExhausted⟩ := rfl + +#print axioms run_zero + +/-- `run` takes one step as the interpreter over the list reads it. -/ +theorem run_succ (program : CIL.Program) (fuel method pc : Nat) (args : List Value) + (frame : Frame) (stack : List Value) (memory : Memory) : + run program (fuel + 1) method pc args frame stack memory = (do + let some body := program[method]? | .error ⟨method, pc, .invalidState⟩ + let some op := body.code[pc]? | .error ⟨method, pc, .invalidState⟩ + let action ← (step body op pc args frame stack memory).mapError + fun fault => ⟨method, pc, fault⟩ + match action with + | .next target stack frame memory => run program fuel method target args frame stack memory + | .call callee arguments rest memory => + let some child := program[callee]? | .error ⟨method, pc, .invalidState⟩ + let (childFrame, memory) ← (enterFrame child arguments memory).mapError + fun fault => ⟨method, pc, fault⟩ + let (memory, result) ← run program fuel callee 0 arguments childFrame [] memory + run program fuel method (pc + 1) args frame (result ++ rest) memory + | .tailCall callee arguments memory => + let some child := program[callee]? | .error ⟨method, pc, .invalidState⟩ + if child.returnsValue != body.returnsValue then .error ⟨method, pc, .invalidState⟩ else + let memory := leaveFrame frame memory + let arguments ← (arguments.mapM (checkedValue memory)).mapError + fun fault => ⟨method, pc, fault⟩ + let (childFrame, memory) ← (enterFrame child arguments memory).mapError + fun fault => ⟨method, pc, fault⟩ + run program fuel callee 0 arguments childFrame [] memory + | .construct callee arguments rest frame temporary kind memory => + let some child := program[callee]? | .error ⟨method, pc, .invalidState⟩ + let (childFrame, memory) ← (enterFrame child arguments memory).mapError + fun fault => ⟨method, pc, fault⟩ + let (memory, result) ← run program fuel callee 0 arguments childFrame [] memory + if !result.isEmpty then .error ⟨method, pc, .invalidState⟩ else + let value ← (loadLocal memory (.bytes kind temporary)).mapError + fun fault => ⟨method, pc, fault⟩ + run program fuel method (pc + 1) args frame (value :: rest) memory + | .returned result memory => + let memory := leaveFrame frame memory + let result ← (result.mapM (checkedValue memory)).mapError + fun fault => ⟨method, pc, fault⟩ + return (memory, result)) := rfl + +#print axioms run_succ + +/-- `invoke` enters the method it reads from the list and runs it. -/ +theorem invoke_eq (program : CIL.Program) (fuel method : Nat) (args : List Value) (m : Memory) : + invoke program fuel method args m = (do + let some body := program[method]? | .error ⟨method, 0, .invalidState⟩ + let args ← (args.mapM (checkedValue m)).mapError fun fault => ⟨method, 0, fault⟩ + let (frame, memory) ← (enterFrame body args m).mapError fun fault => ⟨method, 0, fault⟩ + run program fuel method 0 args frame [] memory) := rfl + +#print axioms invoke_eq + +/-- A lookup that gives each method of `program` at its index runs as the list does. -/ +theorem run_eq_runWith {program : CIL.Program} {lookup : Nat → Option CIL.Method} + (agrees : ∀ index, lookup index = program[index]?) : run program = runWith lookup := by + have same : lookup = (program[·]?) := funext agrees + subst same + rfl + +#print axioms run_eq_runWith + +/-- A lookup that gives each method of `program` at its index invokes as the list does. -/ +theorem invoke_eq_invokeWith {program : CIL.Program} {lookup : Nat → Option CIL.Method} + (agrees : ∀ index, lookup index = program[index]?) : invoke program = invokeWith lookup := by + have same : lookup = (program[·]?) := funext agrees + subst same + rfl + +#print axioms invoke_eq_invokeWith /-- Consumer macros name the dispatchers' `eq_def`, which a quotation cannot generate itself. -/ private theorem dispatch_eq_def_generated : True := by diff --git a/CIL/Safety/ExecutionAllocations.lean b/CIL/Safety/ExecutionAllocations.lean index 38c64639e..e23e28ed4 100644 --- a/CIL/Safety/ExecutionAllocations.lean +++ b/CIL/Safety/ExecutionAllocations.lean @@ -32,7 +32,7 @@ theorem memoryInstruction_extends_allocations (operation : CIL.MemoryOp) AllocationExtension m result := by exact (memoryInstruction_effect _ _ _ _ _ h).allocations -theorem instruction_extends_allocations (operation : CIL.Op) (stack values : List Value) +theorem instruction_extends_allocations (operation : CIL.Instruction) (stack values : List Value) (m result : Memory) (h : instruction operation stack m = .ok (result, values)) : AllocationExtension m result := by exact (instruction_effect _ _ _ _ _ h).allocations @@ -43,7 +43,7 @@ theorem staticInstruction_extends_allocations (body : CIL.Method) (pc : Nat) AllocationExtension m result := by exact (staticInstruction_effect _ _ _ _ _ _ _ h).allocations -theorem step_extends_allocations (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +theorem step_extends_allocations (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) (h : step body operation pc args frame stack m = .ok action) : AllocationExtension m action.memory := by @@ -70,7 +70,7 @@ def FrameAction.OwnedAbove (action : FrameAction) (watermark : Nat) : Prop := /-- A step keeps the current frame above a watermark that memory has already passed: any home it allocates gets a fresh identity. -/ -theorem step_ownedAbove (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +theorem step_ownedAbove (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) (watermark : Nat) (old : frame.OwnedAbove watermark) (bound : watermark ≤ m.nextIdentity) diff --git a/CIL/Safety/ExecutionClasses.lean b/CIL/Safety/ExecutionClasses.lean index 5fc48719f..208dd39e8 100644 --- a/CIL/Safety/ExecutionClasses.lean +++ b/CIL/Safety/ExecutionClasses.lean @@ -7,8 +7,8 @@ Memory a caller passes may record any runtime type, so a proof that tells instan classes apart (`CIL/Safety/ClassCatalog.lean`) needs every object's lineage to agree with the class chains the extraction declares: `Memory.ClassesConsistent catalog memory`. The caller establishes it once, at entry. The program keeps it when each of its `newobj` and class `newarr` -sites records a type that agrees with the catalog (`ObjectSitesConsistent`), a property of the -program text alone that `decide` checks once per extraction. Under that premise, +sites creates a type that agrees with the catalog (`ObjectSitesConsistent`), a property of the +program text and link tables alone that `decide` checks once per extraction. Under that premise, `step_preserves_classes` up to `invoke_preserves_classes` carry the agreement through every step, nested call and frame exit, following `StepEffect`: no other instruction creates an instance. -/ @@ -30,24 +30,27 @@ def Memory.ClassesConsistent (catalog : List RuntimeType) (memory : Memory) : Pr ∀ id allocation type, memory.allocations id = some allocation → allocation.kind = .object type → type.ConsistentWith catalog -/-- An instruction creates no instance, or creates one whose type agrees with the catalog. -/ -def SiteConsistent (catalog : List RuntimeType) (operation : CIL.Op) : Prop := - match objectSiteType operation with +/-- An instruction at `pc` of `body` creates no instance, or creates one whose type agrees with + the catalog. -/ +def SiteConsistent (catalog : List RuntimeType) (body : CIL.Method) (pc : Nat) + (operation : CIL.Instruction) : Prop := + match objectSiteType body pc operation with | some type => type.ConsistentWith catalog | none => True -instance (catalog : List RuntimeType) (operation : CIL.Op) : - Decidable (SiteConsistent catalog operation) := by +instance (catalog : List RuntimeType) (body : CIL.Method) (pc : Nat) + (operation : CIL.Instruction) : Decidable (SiteConsistent catalog body pc operation) := by unfold SiteConsistent split <;> infer_instance -/-- Every allocating site of `program` records a runtime type that agrees with the catalog. -/ +/-- Every allocating site of `program` creates a runtime type that agrees with the catalog. -/ def ObjectSitesConsistent (catalog : List RuntimeType) (program : CIL.Program) : Prop := - ∀ body ∈ program, ∀ operation ∈ body.code, SiteConsistent catalog operation + ∀ body ∈ program, ∀ pc (inside : pc < body.code.length), + SiteConsistent catalog body pc body.code[pc] instance (catalog : List RuntimeType) (program : CIL.Program) : Decidable (ObjectSitesConsistent catalog program) := - inferInstanceAs (Decidable (∀ body ∈ program, ∀ operation ∈ body.code, _)) + inferInstanceAs (Decidable (∀ body ∈ program, ∀ pc (_ : pc < body.code.length), _)) /-- A step that creates no instance keeps the catalog's agreement. -/ theorem Memory.ClassesConsistent.kept {catalog : List RuntimeType} {before after : Memory} @@ -138,9 +141,10 @@ theorem ObjectAllocationEffect.classesConsistent {catalog : List RuntimeType} /-- Every checked dispatcher effect keeps the catalog's agreement when the executed instruction's object site does. -/ theorem StepEffect.classesConsistent {catalog : List RuntimeType} {body : CIL.Method} - {operation : CIL.Op} {frame final : Frame} {before after : Memory} - (effect : StepEffect body operation frame before final after) - (site : SiteConsistent catalog operation) (consistent : before.ClassesConsistent catalog) : + {pc : Nat} {operation : CIL.Instruction} {frame final : Frame} {before after : Memory} + (effect : StepEffect body pc operation frame before final after) + (site : SiteConsistent catalog body pc operation) + (consistent : before.ClassesConsistent catalog) : after.ClassesConsistent catalog := by cases effect with | arrayAllocation effect => exact consistent.kept effect.objectsKept @@ -161,8 +165,9 @@ theorem StepEffect.classesConsistent {catalog : List RuntimeType} {body : CIL.Me #print axioms StepEffect.classesConsistent theorem step_preserves_classes {catalog : List RuntimeType} (body : CIL.Method) - (operation : CIL.Op) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) - (m : Memory) (action : FrameAction) (site : SiteConsistent catalog operation) + (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) + (stack : List Value) + (m : Memory) (action : FrameAction) (site : SiteConsistent catalog body pc operation) (consistent : m.ClassesConsistent catalog) (h : step body operation pc args frame stack m = .ok action) : action.memory.ClassesConsistent catalog := @@ -177,9 +182,9 @@ theorem programStep_preserves_classes {catalog : List RuntimeType} {program : CI (hs : ProgramStep program method pc args frame stack m action) : action.memory.ClassesConsistent catalog := by obtain ⟨body, operation, found, fetched, stepped⟩ := hs + obtain ⟨inside, rfl⟩ := List.getElem?_eq_some_iff.mp fetched exact step_preserves_classes _ _ _ _ _ _ _ _ - (sites body (List.mem_of_getElem? found) operation (List.mem_of_getElem? fetched)) - consistent stepped + (sites body (List.mem_of_getElem? found) pc inside) consistent stepped /-- A frame exit only expires storage, so it keeps the catalog's agreement. -/ theorem leaveFrame_preserves_classes {catalog : List RuntimeType} (frame : Frame) {m : Memory} diff --git a/CIL/Safety/ExecutionEquivalence.lean b/CIL/Safety/ExecutionEquivalence.lean index 7405bcebd..20001ec5f 100644 --- a/CIL/Safety/ExecutionEquivalence.lean +++ b/CIL/Safety/ExecutionEquivalence.lean @@ -71,6 +71,39 @@ theorem Memory.Equivalent.checkedValues {m n : Memory} (same : m.Equivalent n) have equality : CIL.Safety.checkedValue m = CIL.Safety.checkedValue n := funext same.checkedValue rw [equality] +/-- A home reads the same view from equivalent memories, and other values are unchanged. -/ +theorem Memory.Equivalent.spanView {m n : Memory} (same : m.Equivalent n) (frame : Frame) + (value : Value) : spanView frame m value = spanView frame n value := by + cases value <;> simp only [CIL.Safety.spanView, same.loadLocal] + +/-- A call argument reads and checks the same in equivalent memories. -/ +theorem Memory.Equivalent.callArgument {m n : Memory} (same : m.Equivalent n) (frame : Frame) + (value : Value) : callArgument frame m value = callArgument frame n value := by + simp only [CIL.Safety.callArgument, same.spanView, same.checkedValue] + +/-- A call's arguments read and check the same in equivalent memories. -/ +theorem Memory.Equivalent.callArguments {m n : Memory} (same : m.Equivalent n) (frame : Frame) + (values : List Value) : + values.mapM (CIL.Safety.callArgument frame m) = + values.mapM (CIL.Safety.callArgument frame n) := by + have equality : CIL.Safety.callArgument frame m = CIL.Safety.callArgument frame n := + funext (same.callArgument frame) + rw [equality] + +/-- A span operation's operands read the same views in equivalent memories. -/ +theorem Memory.Equivalent.viewOperands {m n : Memory} (same : m.Equivalent n) (frame : Frame) : + ∀ (count : Nat) (stack : List Value), + viewOperands frame m count stack = viewOperands frame n count stack + | 0, _ => rfl + | _ + 1, [] => rfl + | count + 1, value :: rest => by + simp only [CIL.Safety.viewOperands, same.spanView, same.viewOperands frame count rest] + +#print axioms Memory.Equivalent.spanView +#print axioms Memory.Equivalent.callArgument +#print axioms Memory.Equivalent.callArguments +#print axioms Memory.Equivalent.viewOperands + /-- Control, values and frames agree exactly; only the representation of memory views may differ. -/ inductive FrameActionEquivalent : FrameAction → FrameAction → Prop where @@ -87,12 +120,13 @@ inductive FrameActionEquivalent : FrameAction → FrameAction → Prop where FrameActionEquivalent (.returned values m) (.returned values n) /-- Managed dispatch observes only stable identities and equivalent checked arguments. -/ -theorem Memory.Equivalent.managedStep {m n : Memory} (same : m.Equivalent n) - (operation : CIL.ManagedOp) (pc : Nat) (frame : Frame) (stack : List Value) : - ResultEquivalent FrameActionEquivalent (managedStep operation pc frame stack m) - (managedStep operation pc frame stack n) := by - unfold CIL.Safety.managedStep - split <;> try simp only [same.checkedValues, same.addNativeTable, same.loadNativeTable] +theorem Memory.Equivalent.calliStep {m n : Memory} (same : m.Equivalent n) + (signature : CIL.ManagedSignature) (tail : Bool) (link : Option CIL.Link) (frame : Frame) + (stack : List Value) : + ResultEquivalent FrameActionEquivalent (calliStep signature tail link frame stack m) + (calliStep signature tail link frame stack n) := by + unfold CIL.Safety.calliStep + split <;> try simp only [same.callArguments] all_goals repeat' first | exact .error _ | exact .ok (.next same _ _ _) @@ -100,7 +134,7 @@ theorem Memory.Equivalent.managedStep {m n : Memory} (same : m.Equivalent n) | exact .ok (.tailCall same _ _) | (apply ResultEquivalent.bind_same; intro) | split -#print axioms Memory.Equivalent.managedStep +#print axioms Memory.Equivalent.calliStep /-- Fresh array bytes and authority do not depend on the old view-list representation. -/ theorem Memory.Equivalent.allocateArray {m n : Memory} (same : m.Equivalent n) @@ -213,26 +247,196 @@ theorem Memory.Equivalent.newReferenceArray {m n : Memory} (same : m.Equivalent | split #print axioms Memory.Equivalent.newReferenceArray +/-- `newarr` of either array shape preserves exact outcomes over equivalent memories. -/ +theorem Memory.Equivalent.allocateElements {m n : Memory} (same : m.Equivalent n) + (shape : ElementShape) (length : CIL.Value) : + ResultEquivalent MemoryLastEquivalent (allocateElements shape length m) + (allocateElements shape length n) := by + cases shape with + | value element => + apply ResultEquivalent.bind (same.newArray element length) + intro left right related + obtain ⟨view, memory⟩ := left + obtain ⟨otherView, otherMemory⟩ := right + obtain ⟨rfl, equivalent⟩ := related + exact .ok ⟨rfl, equivalent⟩ + | reference element => + apply ResultEquivalent.bind (same.newReferenceArray element length) + intro left right related + obtain ⟨array, memory⟩ := left + obtain ⟨otherArray, otherMemory⟩ := right + obtain ⟨rfl, equivalent⟩ := related + exact .ok ⟨rfl, equivalent⟩ + | method signature => exact .error _ + | unsupported reason => exact .error _ +#print axioms Memory.Equivalent.allocateElements + +/-- Equivalent memories load every argument alike, from its home or the incoming list. -/ +theorem Memory.Equivalent.loadArgument {m n : Memory} (same : m.Equivalent n) + (body : CIL.Method) (index : Nat) (args : List Value) (frame : Frame) : + loadArgument body index args frame m = loadArgument body index args frame n := by + simp only [CIL.Safety.loadArgument, same.checkedValue, same.loadLocal] +#print axioms Memory.Equivalent.loadArgument + +/-- Equivalent memories store every argument into the same home, leaving equivalent memories. -/ +theorem Memory.Equivalent.storeArgument {m n : Memory} (same : m.Equivalent n) + (body : CIL.Method) (index : Nat) (value : Value) (frame : Frame) : + ResultEquivalent MemoryLastEquivalent (storeArgument body index value frame m) + (storeArgument body index value frame n) := by + unfold CIL.Safety.storeArgument + split + · split + · apply ResultEquivalent.bind (same.storeLocal _ _) + intro left right related + obtain ⟨home, memory⟩ := left + obtain ⟨otherHome, otherMemory⟩ := right + obtain ⟨rfl, equivalent⟩ := related + exact .ok ⟨rfl, equivalent⟩ + · exact .error _ + · exact .error _ +#print axioms Memory.Equivalent.storeArgument + +/-- Equivalent memories give every argument the same address. -/ +theorem Memory.Equivalent.addressArgument {m n : Memory} (same : m.Equivalent n) + (body : CIL.Method) (index : Nat) (frame : Frame) : + addressArgument body index frame m = addressArgument body index frame n := by + simp only [CIL.Safety.addressArgument, same.localAddress] +#print axioms Memory.Equivalent.addressArgument + +/-- Equivalent memories give an instance field load equivalent results. -/ +theorem Memory.Equivalent.fieldLoad {m n : Memory} (same : m.Equivalent n) (body : CIL.Method) + (field : CIL.Extraction.FieldRef) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (fieldLoad body field stack m) + (fieldLoad body field stack n) := by + unfold CIL.Safety.fieldLoad + split + · exact same.classFieldLoad _ _ _ _ + · exact same.loadValueField _ _ _ _ + · exact same.loadAggregateField _ _ _ + · exact .error _ +#print axioms Memory.Equivalent.fieldLoad + +/-- Equivalent memories give an instance field store equivalent results. -/ +theorem Memory.Equivalent.fieldStore {m n : Memory} (same : m.Equivalent n) (body : CIL.Method) + (field : CIL.Extraction.FieldRef) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (fieldStore body field stack m) + (fieldStore body field stack n) := by + unfold CIL.Safety.fieldStore + split + · exact same.classFieldStore _ _ _ _ + · exact same.storeValueField _ _ _ + · exact same.storeAggregateField _ _ _ + · exact .error _ +#print axioms Memory.Equivalent.fieldStore + +/-- Equivalent memories validate the same field displacement and return the same reference. -/ +theorem Memory.Equivalent.structFieldAddress {m n : Memory} (same : m.Equivalent n) + (offset : Nat) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (structFieldAddress offset stack m) + (structFieldAddress offset stack n) := by + unfold CIL.Safety.structFieldAddress + split + · exact .error _ + · split <;> first + | exact .error _ + | (rw [same.add] + apply ResultEquivalent.bind_same + intro address + exact .ok ⟨same, rfl⟩) +#print axioms Memory.Equivalent.structFieldAddress + +/-- Equivalent memories give an instance field address equivalent results. -/ +theorem Memory.Equivalent.fieldReference {m n : Memory} (same : m.Equivalent n) + (body : CIL.Method) (field : CIL.Extraction.FieldRef) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (fieldReference body field stack m) + (fieldReference body field stack n) := by + unfold CIL.Safety.fieldReference + split + · exact same.structFieldAddress _ _ + · exact same.structFieldAddress _ _ + · exact .error _ + · exact .error _ +#print axioms Memory.Equivalent.fieldReference + +/-- Equivalent memories give a span constructor run in place equivalent results. -/ +theorem Memory.Equivalent.spanInPlaceCall {m n : Memory} (same : m.Equivalent n) + (constructor : CIL.SpanConstructor) (pc : Nat) (frame : Frame) (stack : List Value) : + ResultEquivalent FrameActionEquivalent (spanInPlaceCall constructor pc frame stack m) + (spanInPlaceCall constructor pc frame stack n) := by + unfold CIL.Safety.spanInPlaceCall + apply ResultEquivalent.bind (same.memoryInstruction _ _) + intro left right related + obtain ⟨memory, values⟩ := left + obtain ⟨otherMemory, otherValues⟩ := right + obtain ⟨equivalent, rfl⟩ := related + dsimp only + split + · split + · split + · exact .error _ + · apply ResultEquivalent.bind (equivalent.storeLocal _ _) + intro left right related + obtain ⟨slot, memory⟩ := left + obtain ⟨otherSlot, otherMemory⟩ := right + obtain ⟨rfl, stored⟩ := related + exact .ok (.next stored _ _ _) + · exact .error _ + · exact .error _ +#print axioms Memory.Equivalent.spanInPlaceCall + +/-- Equivalent memories give a call's runtime model equivalent results. -/ +theorem Memory.Equivalent.modelStep {m n : Memory} (same : m.Equivalent n) + (model : Option CIL.RuntimeModel) (body : CIL.Method) (pc : Nat) (frame : Frame) + (stack : List Value) : + ResultEquivalent FrameActionEquivalent (modelStep model body pc frame stack m) + (modelStep model body pc frame stack n) := by + unfold CIL.Safety.modelStep observedCall runModel intrinsicCall referenceCompareCall + referenceSubtractCall featureCall typedTransferCall skipInitCall + split + all_goals try split + all_goals try simp only [same.checkedValue, same.referenceEqual, same.referenceLess, + same.referenceSubtract, same.formValue, same.viewOperands] + all_goals repeat' first + | exact .error _ + | exact .ok (.next same _ _ _) + | exact same.spanInPlaceCall _ _ _ _ + | (apply ResultEquivalent.bind_same; intro) + | (apply ResultEquivalent.bind (same.memoryInstruction _ _) + intro left right related + obtain ⟨memory, values⟩ := left + obtain ⟨otherMemory, otherValues⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) + | (apply ResultEquivalent.bind (same.observedTransfer _ _ _) + intro left right related + obtain ⟨memory, values⟩ := left + obtain ⟨otherMemory, otherValues⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) + | split +#print axioms Memory.Equivalent.modelStep + theorem Memory.Equivalent.step {m n : Memory} (same : m.Equivalent n) - (body : CIL.Method) (op : CIL.Op) (pc : Nat) (args : List Value) + (body : CIL.Method) (op : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) : ResultEquivalent FrameActionEquivalent (step body op pc args frame stack m) (step body op pc args frame stack n) := by - unfold CIL.Safety.step + unfold CIL.Safety.step observedCall referenceCompareCall referenceSubtractCall + newobjStep constructAggregate constructObject dsimp only split <;> try simp only [same.checkedValue, same.checkedValues, same.loadLocal, same.localAddress, + same.loadArgument, same.addressArgument, same.callArguments, same.viewOperands, same.referenceNonNull, same.referenceEqual, same.referenceDifference, same.referenceLess, same.referenceSubtract, same.objectNonNull, same.objectEqual, same.objectAboveNull, same.virtualTarget, same.objectTest] all_goals repeat' first - | exact same.managedStep _ _ _ _ - | (apply ResultEquivalent.bind (same.newArray _ _) - intro left right related - obtain ⟨view, memory⟩ := left - obtain ⟨otherView, otherMemory⟩ := right - obtain ⟨rfl, equivalent⟩ := related - exact .ok (.next equivalent _ _ _)) - | (apply ResultEquivalent.bind (same.newReferenceArray _ _) + | exact same.calliStep _ _ _ _ _ + | unfold returnFrom + | unfold localAllocation + | unfold initializeSpanHome + | exact same.modelStep _ _ _ _ _ + | simp only [same.checkedValues] + | (apply ResultEquivalent.bind (same.allocateElements _ _) intro left right related obtain ⟨array, memory⟩ := left obtain ⟨otherArray, otherMemory⟩ := right @@ -255,6 +459,7 @@ theorem Memory.Equivalent.step {m n : Memory} (same : m.Equivalent n) | exact .ok (.call same _ _ _) | exact .ok (.tailCall same _ _) | exact .ok (.returned same _) + | rw [same.addNativeTable] | (apply ResultEquivalent.bind_same; intro) | (apply ResultEquivalent.bind (same.storeLocal _ _) intro left right related @@ -262,6 +467,12 @@ theorem Memory.Equivalent.step {m n : Memory} (same : m.Equivalent n) obtain ⟨otherSlot, otherMemory⟩ := right obtain ⟨rfl, equivalent⟩ := related exact .ok (.next equivalent _ _ _)) + | (apply ResultEquivalent.bind (same.storeArgument _ _ _ _) + intro left right related + obtain ⟨frame, memory⟩ := left + obtain ⟨otherFrame, otherMemory⟩ := right + obtain ⟨rfl, equivalent⟩ := related + exact .ok (.next equivalent _ _ _)) | (apply ResultEquivalent.bind (same.memoryInstruction _ _) intro left right related obtain ⟨memory, values⟩ := left @@ -282,12 +493,67 @@ theorem Memory.Equivalent.step {m n : Memory} (same : m.Equivalent n) obtain ⟨otherMemory, otherValues⟩ := right obtain ⟨equivalent, rfl⟩ := related exact .ok (.next equivalent _ _ _)) + | (apply ResultEquivalent.bind (same.staticLoad _ _) + intro left right related + obtain ⟨memory, value⟩ := left + obtain ⟨otherMemory, otherValue⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) + | (apply ResultEquivalent.bind (same.staticAddress _ _) + intro left right related + obtain ⟨memory, value⟩ := left + obtain ⟨otherMemory, otherValue⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) + | (apply ResultEquivalent.bind (same.referenceAddInstruction _ _ _) + intro left right related + obtain ⟨memory, values⟩ := left + obtain ⟨otherMemory, otherValues⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) | (apply ResultEquivalent.bind (same.instruction _ _) intro left right related obtain ⟨memory, values⟩ := left obtain ⟨otherMemory, otherValues⟩ := right obtain ⟨equivalent, rfl⟩ := related exact .ok (.next equivalent _ _ _)) + | (first + | apply ResultEquivalent.bind (same.otherMemoryInstruction (.copyBlock _) _) + | apply ResultEquivalent.bind (same.otherMemoryInstruction (.initBlock _) _) + | apply ResultEquivalent.bind (same.stringLiteralInstruction _ _) + | apply ResultEquivalent.bind (same.copyInitializedBytes _ _) + | apply ResultEquivalent.bind (same.initializeBytes _ _) + | apply ResultEquivalent.bind (same.transferObject _ _ _) + | apply ResultEquivalent.bind (same.typedElementInstruction _ _ _) + intro left right related + obtain ⟨memory, values⟩ := left + obtain ⟨otherMemory, otherValues⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) + | (apply ResultEquivalent.bind (same.elementAddressInstruction _ _ _) + intro left right related + obtain ⟨memory, values⟩ := left + obtain ⟨otherMemory, otherValues⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) + | (apply ResultEquivalent.bind (same.fieldLoad _ _ _) + intro left right related + obtain ⟨memory, values⟩ := left + obtain ⟨otherMemory, otherValues⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) + | (apply ResultEquivalent.bind (same.fieldStore _ _ _) + intro left right related + obtain ⟨memory, values⟩ := left + obtain ⟨otherMemory, otherValues⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) + | (apply ResultEquivalent.bind (same.fieldReference _ _ _) + intro left right related + obtain ⟨memory, values⟩ := left + obtain ⟨otherMemory, otherValues⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok (.next equivalent _ _ _)) | split all_goals apply ResultEquivalent.bind (same.allocateHome _ _ _) @@ -316,7 +582,7 @@ theorem Memory.Equivalent.run {m n : Memory} (same : m.Equivalent n) induction fuel generalizing m n method pc args frame stack with | zero => exact .error _ | succ fuel ih => - simp only [CIL.Safety.run] + simp only [CIL.Safety.run_succ] split <;> try exact .error _ split <;> try exact .error _ apply ResultEquivalent.bind ((same.step _ _ _ _ _ _).mapError _) @@ -381,7 +647,7 @@ theorem Memory.Equivalent.invoke {m n : Memory} (same : m.Equivalent n) (program : CIL.Program) (fuel method : Nat) (args : List Value) : ResultEquivalent MemoryFirstEquivalent (invoke program fuel method args m) (invoke program fuel method args n) := by - unfold CIL.Safety.invoke + rw [CIL.Safety.invoke_eq, CIL.Safety.invoke_eq] split <;> try exact .error _ simp only [same.checkedValues] apply ResultEquivalent.bind_same diff --git a/CIL/Safety/ExecutionFrameOwnership.lean b/CIL/Safety/ExecutionFrameOwnership.lean index 3f7e80c90..da109bea0 100644 --- a/CIL/Safety/ExecutionFrameOwnership.lean +++ b/CIL/Safety/ExecutionFrameOwnership.lean @@ -13,7 +13,8 @@ namespace CIL.Safety /-- A step keeps every allocation the resulting frame owns live and tagged with its activation, including a home the step just allocated. -/ -theorem step_preserves_frame_allocations (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +theorem step_preserves_frame_allocations (body : CIL.Method) (operation : CIL.Instruction) + (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) (hm : m.WellFormed) (owns : FrameAllocations m frame.activation frame.owned) diff --git a/CIL/Safety/ExecutionInvariants.lean b/CIL/Safety/ExecutionInvariants.lean index 80a8543d4..1d8392bf8 100644 --- a/CIL/Safety/ExecutionInvariants.lean +++ b/CIL/Safety/ExecutionInvariants.lean @@ -30,47 +30,55 @@ def FrameAction.frameAfter (action : FrameAction) (current : Frame) : Frame := | .next _ _ frame _ | .construct _ _ _ frame _ _ _ => frame | .call _ _ _ _ | .tailCall _ _ _ | .returned _ _ => current -/-- The runtime type of the instance an instruction creates: `newobj` records its class and - bases in the instruction itself, `newarr` of a class the array type of its element; no - other instruction creates an instance. -/ -def objectSiteType : CIL.Op → Option RuntimeType - | .newObject _ _ owner bases _ => some ⟨owner, bases⟩ - | .newReferenceArray element => some (referenceArrayType element) +/-- The runtime type of the instance an instruction at `pc` of `body` creates: `newobj` an + instance of its constructor's owner with the bases an `.object` link at `pc` states, and + `newarr` of an element the body's layouts give a class reference shape the array type of that + class; no other instruction, and `newobj` under no `.object` link, creates an instance. -/ +def objectSiteType (body : CIL.Method) (pc : Nat) : CIL.Instruction → Option RuntimeType + | .newobj constructor => match body.linkAt pc with + | some (.object _ bases _) => some ⟨constructor.owner, bases⟩ + | _ => none + | .newarr element => match body.elementShape element with + | .reference element => some (referenceArrayType element) + | _ => none | _ => none /-- A step's memory and frame changes, with successful primitive operations as evidence. Value-stack validity is independent of this effect classification. Only the - executed `operation` may create an instance, and only of the type it records. -/ -inductive StepEffect (body : CIL.Method) (operation : CIL.Op) (frame : Frame) (m : Memory) : + `operation` executed at `site` may create an instance, and only of the type + `objectSiteType` gives it there. -/ +inductive StepEffect (body : CIL.Method) (site : Nat) (operation : CIL.Instruction) (frame : Frame) + (m : Memory) : Frame → Memory → Prop where | arrayAllocation {view result} (effect : ArrayAllocationEffect m view result) : - StepEffect body operation frame m frame result + StepEffect body site operation frame m frame result | objectAllocation {type object result} (effect : ObjectAllocationEffect m type object result) - (site : objectSiteType operation = some type) : - StepEffect body operation frame m frame result + (created : objectSiteType body site operation = some type) : + StepEffect body site operation frame m frame result | stackAllocation {reference final result} (effect : StackAllocationEffect m frame reference final result) : - StepEffect body operation frame m final result + StepEffect body site operation frame m final result | memory {result} (effect : StaticInstructionEffect body m result) : - StepEffect body operation frame m frame result + StepEffect body site operation frame m frame result | store {index slot final value result} (found : frame.locals[index]? = some slot) (stored : storeLocal m slot value = .ok (final, result)) : - StepEffect body operation frame m { frame with locals := frame.locals.set index final } + StepEffect body site operation frame m { frame with locals := frame.locals.set index final } result | valueHome {home result r width value} (allocated : allocateHome m frame.activation width = .ok (r, home)) (stored : storeValue home (.address r) value = .ok result) : - StepEffect body operation frame m { frame with owned := r.allocation :: frame.owned } + StepEffect body site operation frame m { frame with owned := r.allocation :: frame.owned } result | localHome {home result r width slot final value} (allocated : allocateHome m frame.activation width = .ok (r, home)) (stored : storeLocal home slot value = .ok (final, result)) : - StepEffect body operation frame m { frame with owned := r.allocation :: frame.owned } + StepEffect body site operation frame m { frame with owned := r.allocation :: frame.owned } result -theorem StepEffect.wellFormed {body : CIL.Method} {operation : CIL.Op} {frame final : Frame} - {m result : Memory} (effect : StepEffect body operation frame m final result) +theorem StepEffect.wellFormed {body : CIL.Method} {site : Nat} {operation : CIL.Instruction} + {frame final : Frame} + {m result : Memory} (effect : StepEffect body site operation frame m final result) (hm : m.WellFormed) : result.WellFormed := by cases effect with | arrayAllocation effect => exact effect.wellFormed hm @@ -85,8 +93,9 @@ theorem StepEffect.wellFormed {body : CIL.Method} {operation : CIL.Op} {frame fi exact storeLocal_preserves_wellFormed _ _ _ _ _ (allocateHome_preserves_wellFormed _ _ _ _ _ hm allocated) stored -theorem StepEffect.allocations {body : CIL.Method} {operation : CIL.Op} {frame final : Frame} - {m result : Memory} (effect : StepEffect body operation frame m final result) : +theorem StepEffect.allocations {body : CIL.Method} {site : Nat} {operation : CIL.Instruction} + {frame final : Frame} + {m result : Memory} (effect : StepEffect body site operation frame m final result) : AllocationExtension m result := by cases effect with | arrayAllocation effect => exact effect.allocations @@ -119,17 +128,66 @@ theorem loadLocal_result_valid (m : Memory) (slot : LocalSlot) (value : Value) | cases h | split at h -theorem localAddress_result_valid (m : Memory) (slot : LocalSlot) (value : Value) - (h : localAddress m slot = .ok value) : value.Valid m := by +/-- An argument load pushes a value valid in the memory it was read from, from its home or + checked from the incoming arguments. -/ +theorem loadArgument_result_valid (body : CIL.Method) (index : Nat) (args : List Value) + (frame : Frame) (m : Memory) (value : Value) + (h : loadArgument body index args frame m = .ok value) : value.Valid m := by + unfold loadArgument at h + split at h <;> try simp only [Bind.bind, Except.bind] at h + all_goals repeat' first + | solve | exact (checkedValue_valid _ _ _ h).2 + | solve | exact loadLocal_result_valid _ _ _ h + | cases h + | split at h +#print axioms loadArgument_result_valid + +/-- A successful argument store writes its home's frame slot, as `stloc` of that slot does. -/ +theorem storeArgument_stores {body : CIL.Method} {index : Nat} {value : Value} {frame : Frame} + {m : Memory} {stored : Frame × Memory} + (h : storeArgument body index value frame m = .ok stored) : + ∃ slot home final, frame.locals[slot]? = some home ∧ + storeLocal m home value = .ok (final, stored.2) ∧ + stored.1 = { frame with locals := frame.locals.set slot final } := by + unfold storeArgument at h + split at h + · rename_i slot _ + cases found : frame.locals[slot]? with + | none => simp only [found] at h; cases h + | some home => + simp only [found, Bind.bind, Except.bind, Pure.pure, Except.pure] at h + cases written : storeLocal m home value with + | error _ => simp only [written] at h; cases h + | ok result => + simp only [written, Except.ok.injEq] at h + subst h + exact ⟨slot, home, result.1, found, by rw [written], rfl⟩ + · cases h +#print axioms storeArgument_stores + +theorem localAddress_result_valid (m : Memory) (index : Nat) (slot : LocalSlot) (value : Value) + (h : localAddress m index slot = .ok value) : value.Valid m := by cases slot with | root root => cases h | readOnlyRoot _ => cases h | opaque128 _ => cases h - | span _ _ _ => cases h + | span _ _ _ => cases h; trivial | array _ _ => cases h | methodPointer _ _ | methodTable _ _ | object _ => cases h | bytes kind r => exact formValue_result_valid m (.address r) value h +/-- An argument address is valid in the memory it was taken in, as its home's address is. -/ +theorem addressArgument_result_valid (body : CIL.Method) (index : Nat) (frame : Frame) + (m : Memory) (value : Value) (h : addressArgument body index frame m = .ok value) : + value.Valid m := by + unfold addressArgument at h + split at h + · split at h + · exact localAddress_result_valid _ _ _ _ h + · cases h + · cases h +#print axioms addressArgument_result_valid + theorem ValuesValid.drop {m : Memory} {values : List Value} (valid : ValuesValid m values) (count : Nat) : ValuesValid m (values.drop count) := fun _ member => valid _ (List.mem_of_mem_drop member) @@ -139,6 +197,109 @@ theorem checkedValues_preserved (m result : Memory) (values checked : List Value (h : values.mapM (checkedValue m) = .ok checked) : ValuesValid result checked := (checkedValues_valid _ _ _ h).2.preserve hm ext +/-- A call argument is a checked value: valid in the memory it was read in, and never a home. -/ +theorem callArgument_checked {frame : Frame} {m : Memory} {value result : Value} + (h : callArgument frame m value = .ok result) : result.Valid m ∧ result.isHome = false := by + unfold callArgument at h + cases viewed : spanView frame m value with + | error _ => simp [viewed, Bind.bind, Except.bind] at h + | ok projected => + simp only [viewed, Bind.bind, Except.bind] at h + exact ⟨(checkedValue_valid _ _ _ h).2, checkedValue_not_spanHome h⟩ + +#print axioms callArgument_checked + +/-- A call's arguments, read from `frame`, are valid in the memory they were read in. -/ +theorem callArguments_valid {frame : Frame} {m : Memory} {values checked : List Value} + (h : values.mapM (callArgument frame m) = .ok checked) : ValuesValid m checked := + mapM_ok_forall (fun _ _ read => (callArgument_checked read).1) h + +#print axioms callArguments_valid + +/-- A call's arguments, read from `frame`, stay valid as memory only gains allocations. -/ +theorem callArguments_preserved (m result : Memory) (frame : Frame) (values checked : List Value) + (hm : m.WellFormed) (ext : AllocationExtension m result) + (h : values.mapM (callArgument frame m) = .ok checked) : ValuesValid result checked := + (callArguments_valid h).preserve hm ext + +#print axioms callArguments_preserved + +/-- A call argument has unspecified bits exactly when the value it was read from has them; a + home stands for a view, which has none. -/ +theorem callArgument_hasUnspecified {frame : Frame} {m : Memory} {value result : Value} + (h : callArgument frame m value = .ok result) : + result.hasUnspecified = value.hasUnspecified := by + unfold callArgument at h + cases viewed : spanView frame m value with + | error _ => simp [viewed, Bind.bind, Except.bind] at h + | ok projected => + simp only [viewed, Bind.bind, Except.bind] at h + rw [(checkedValue_valid _ _ _ h).1] + cases value <;> simp only [spanView, Pure.pure, Except.pure, Except.ok.injEq] at viewed + all_goals first + | (subst viewed; rfl) + | (split at viewed + · simp only [loadLocal, Bind.bind, Except.bind, Pure.pure, Except.pure] at viewed + repeat' (first | (cases viewed; rfl) | contradiction | split at viewed) + · cases viewed) + +#print axioms callArgument_hasUnspecified + +/-- A call's arguments have unspecified bits exactly when the values they were read from have. -/ +theorem callArguments_hasUnspecified {frame : Frame} {m : Memory} : + ∀ {values checked : List Value}, values.mapM (callArgument frame m) = .ok checked → + checked.any Value.hasUnspecified = values.any Value.hasUnspecified + | [], checked, mapped => by cases mapped; rfl + | value :: rest, checked, mapped => by + simp only [List.mapM_cons, Bind.bind, Except.bind, Pure.pure, Except.pure] at mapped + cases head : callArgument frame m value with + | error _ => simp [head] at mapped + | ok produced => + cases tail : rest.mapM (callArgument frame m) with + | error _ => simp [head, tail] at mapped + | ok others => + simp only [head, tail, Except.ok.injEq] at mapped + subst mapped + simp only [List.any_cons, callArgument_hasUnspecified head, + callArguments_hasUnspecified tail] + +#print axioms callArguments_hasUnspecified + +/-- The view a home stands for is valid where it was read, and every other value is unchanged. -/ +theorem spanView_valid {frame : Frame} {m : Memory} {value result : Value} + (h : spanView frame m value = .ok result) (valid : value.Valid m) : result.Valid m := by + cases value with + | spanHome index => + simp only [spanView] at h + split at h + · exact loadLocal_result_valid _ _ _ h + · cases h + | _ => cases h; exact valid + +#print axioms spanView_valid + +/-- Reading the homes among a stack's top operands keeps every value valid. -/ +theorem viewOperands_valid {frame : Frame} {m : Memory} : + ∀ {count : Nat} {stack operands : List Value}, + viewOperands frame m count stack = .ok operands → ValuesValid m stack → + ValuesValid m operands + | 0, _, _, h, valid => by cases h; exact valid + | _ + 1, [], _, h, _ => by cases h; intro _ member; cases member + | count + 1, value :: rest, operands, h, valid => by + simp only [viewOperands, Bind.bind, Except.bind, Pure.pure, Except.pure] at h + cases viewed : spanView frame m value with + | error _ => simp [viewed] at h + | ok produced => + cases others : viewOperands frame m count rest with + | error _ => simp [viewed, others] at h + | ok tail => + simp only [viewed, others, Except.ok.injEq] at h + subst h + exact ValuesValid.cons (spanView_valid viewed (valid value (List.mem_cons_self ..))) + (viewOperands_valid others valid.tail) + +#print axioms viewOperands_valid + theorem allocated_store_reference_valid (m home result : Memory) (activation width : Nat) (r : Reference) (value : CIL.Value) (hm : m.WellFormed) (allocation : allocateHome m activation width = .ok (r, home)) @@ -165,38 +326,67 @@ def FrameAction.LiveValues (action : FrameAction) : Prop := ValuesValid memory args ∧ ValuesValid memory rest /-- One dispatcher proof supplies memory/frame effects and conditional value validity. -/ -structure StepContract (body : CIL.Method) (operation : CIL.Op) (frame : Frame) +structure StepContract (body : CIL.Method) (site : Nat) (operation : CIL.Instruction) + (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) : Prop where - effect : StepEffect body operation frame m (action.frameAfter frame) action.memory + effect : StepEffect body site operation frame m (action.frameAfter frame) action.memory values : m.WellFormed → ValuesValid m stack → action.LiveValues -theorem StepContract.of_effect {body : CIL.Method} {operation : CIL.Op} {frame : Frame} +theorem StepContract.of_effect {body : CIL.Method} {site : Nat} {operation : CIL.Instruction} + {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} - (effect : StepEffect body operation frame m (action.frameAfter frame) action.memory) + (effect : StepEffect body site operation frame m (action.frameAfter frame) action.memory) (preserve : m.WellFormed → AllocationExtension m action.memory → ValuesValid action.memory stack → action.LiveValues) : - StepContract body operation frame stack m action := + StepContract body site operation frame stack m action := ⟨effect, fun hm valid => preserve hm effect.allocations (valid.preserve hm effect.allocations)⟩ -theorem StepContract.instruction {body : CIL.Method} {operation : CIL.Op} {frame : Frame} +/-- A successful argument store has the effect of a local store to the argument's home. -/ +theorem StepContract.storeArgument {body : CIL.Method} {site : Nat} {index : Nat} {frame : Frame} + {value : Value} {rest : List Value} {m : Memory} {stored : Frame × Memory} {pc : Nat} + (h : storeArgument body index value frame m = .ok stored) : + StepContract body site (.starg index) frame (value :: rest) m + (.next pc rest stored.1 stored.2) := by + obtain ⟨slot, home, final, found, written, same⟩ := storeArgument_stores h + rw [same] + refine StepContract.of_effect (.store found written) ?_ + intro _ _ retained + exact retained.tail +#print axioms StepContract.storeArgument + +theorem StepContract.instruction {body : CIL.Method} {site : Nat} {operation : CIL.Instruction} + {frame : Frame} {stack values : List Value} {m result : Memory} {pc : Nat} (contract : InstructionContract m result stack values) : - StepContract body operation frame stack m (.next pc values frame result) := + StepContract body site operation frame stack m (.next pc values frame result) := ⟨.memory (.ordinary contract.effect), contract.values⟩ -theorem StepContract.staticInstruction {body : CIL.Method} {operation : CIL.Op} {frame : Frame} +/-- A step that runs on the stack with its homes read as views has that step's contract. -/ +theorem StepContract.viewed {body : CIL.Method} {site : Nat} {operation : CIL.Instruction} + {frame : Frame} {stack operands : List Value} {m : Memory} {action : FrameAction} + {count : Nat} (viewed : viewOperands frame m count stack = .ok operands) + (contract : StepContract body site operation frame operands m action) : + StepContract body site operation frame stack m action := + ⟨contract.effect, fun hm valid => contract.values hm (viewOperands_valid viewed valid)⟩ + +#print axioms StepContract.viewed + +theorem StepContract.staticInstruction {body : CIL.Method} {site : Nat} + {operation : CIL.Instruction} + {frame : Frame} {stack values : List Value} {m result : Memory} {pc : Nat} (contract : StaticInstructionContract body m result stack values) : - StepContract body operation frame stack m (.next pc values frame result) := + StepContract body site operation frame stack m (.next pc values frame result) := ⟨.memory contract.effect, contract.values⟩ /-- Dynamic allocation supplies a live reference and preserves existing memory. -/ -theorem StepContract.stackAllocation {body : CIL.Method} {operation : CIL.Op} {frame final : Frame} +theorem StepContract.stackAllocation {body : CIL.Method} {site : Nat} {operation : CIL.Instruction} + {frame final : Frame} {stack : List Value} {memory result : Memory} {reference : ManagedReference} {size pc : Nat} {initLocals : Bool} (success : allocateStack memory frame size initLocals = .ok (reference, final, result)) : - StepContract body operation frame stack memory + StepContract body site operation frame stack memory (.next pc [.reference reference] final result) := by have effect := allocateStack_effect success refine ⟨.stackAllocation effect, ?_⟩ @@ -207,11 +397,12 @@ theorem StepContract.stackAllocation {body : CIL.Method} {operation : CIL.Op} {f #print axioms StepContract.stackAllocation /-- Heap arrays stay live independently of frame-owned storage. -/ -theorem StepContract.arrayAllocation {body : CIL.Method} {operation : CIL.Op} {frame : Frame} +theorem StepContract.arrayAllocation {body : CIL.Method} {site : Nat} {operation : CIL.Instruction} + {frame : Frame} {stack : List Value} {memory result : Memory} {view : ArrayView} {element : CIL.ArrayElement} {length : CIL.Value} {pc : Nat} (success : newArray memory element length = .ok (view, result)) : - StepContract body operation frame (.scalar length :: stack) memory + StepContract body site operation frame (.scalar length :: stack) memory (.next pc (.array view :: stack) frame result) := by have effect := newArray_effect success apply StepContract.of_effect (.arrayAllocation effect) @@ -220,40 +411,62 @@ theorem StepContract.arrayAllocation {body : CIL.Method} {operation : CIL.Op} {f #print axioms StepContract.arrayAllocation -/-- `newobj` hands the constructor a fresh instance ahead of arguments checked before the - allocation, and leaves the same instance under the caller's remaining stack. -/ -theorem StepContract.objectAllocation {body : CIL.Method} {frame : Frame} +/-- `newobj` under an `.object` link hands the constructor a fresh instance of its owner with the + link's bases ahead of arguments checked before the allocation, and leaves the same instance + under the caller's remaining stack. -/ +theorem StepContract.objectAllocation {body : CIL.Method} {site : Nat} {frame : Frame} {stack arguments : List Value} {memory result : Memory} {object : Reference} - {owner : CIL.Extraction.TypeExpr} {bases : List CIL.Extraction.TypeExpr} - {size callee argc : Nat} - (checked : (stack.take argc).reverse.mapM (checkedValue memory) = .ok arguments) - (success : allocateObject memory ⟨owner, bases⟩ size = .ok (object, result)) : - StepContract body (.newObject callee argc owner bases size) frame stack memory + {constructor : CIL.Extraction.MethodRef} {bases : List CIL.Extraction.TypeExpr} + {size linkedSize index callee argc : Nat} + (linked : body.linkAt site = some (.object index bases linkedSize)) + (checked : (stack.take argc).reverse.mapM (callArgument frame memory) = .ok arguments) + (success : allocateObject memory ⟨constructor.owner, bases⟩ size = .ok (object, result)) : + StepContract body site (.newobj constructor) frame stack memory (.call callee (.object (.address object) :: arguments) (.object (.address object) :: stack.drop argc) result) := by have effect := allocateObject_effect success - apply StepContract.of_effect (.objectAllocation effect rfl) + apply StepContract.of_effect (.objectAllocation effect (by simp only [objectSiteType, linked])) intro hm extension retained exact ⟨ValuesValid.cons effect.object_valid - (checkedValues_preserved _ _ _ _ hm extension checked), + (callArguments_preserved _ _ _ _ _ hm extension checked), ValuesValid.cons effect.object_valid (retained.drop _)⟩ #print axioms StepContract.objectAllocation -/-- `newarr` of a class pushes a fresh array, allocated as an instance of its array type. -/ -theorem StepContract.referenceArrayAllocation {body : CIL.Method} {frame : Frame} - {stack : List Value} {memory result : Memory} {array : Reference} +/-- `newarr` pushes a fresh array of the shape the body's layouts give its element: inline + storage, or an instance of the array type of a class element. -/ +theorem StepContract.newarr {body : CIL.Method} {site : Nat} {frame : Frame} + {stack : List Value} {memory result : Memory} {array : Value} {element : CIL.Extraction.TypeExpr} {length : CIL.Value} {pc : Nat} - (success : newReferenceArray memory element length = .ok (array, result)) : - StepContract body (.newReferenceArray element) frame (.scalar length :: stack) memory - (.next pc (.object (.address array) :: stack) frame result) := by - obtain ⟨size, allocated⟩ := newReferenceArray_allocation success - have effect := allocateObject_effect allocated - apply StepContract.of_effect (.objectAllocation effect rfl) - intro _ _ retained - exact ValuesValid.cons effect.object_valid retained.tail - -#print axioms StepContract.referenceArrayAllocation + (success : allocateElements (body.elementShape element) length memory = .ok (array, result)) : + StepContract body site (.newarr element) frame (.scalar length :: stack) memory + (.next pc (array :: stack) frame result) := by + cases shaped : body.elementShape element with + | value inline => + simp only [shaped, allocateElements, Bind.bind, Except.bind, Pure.pure, Except.pure] + at success + split at success + · cases success + · rename_i allocated + cases success + exact StepContract.arrayAllocation allocated + | reference classElement => + simp only [shaped, allocateElements, Bind.bind, Except.bind, Pure.pure, Except.pure] + at success + split at success + · cases success + · rename_i allocated + cases success + obtain ⟨size, created⟩ := newReferenceArray_allocation allocated + have effect := allocateObject_effect created + apply StepContract.of_effect (.objectAllocation effect + (by simp only [objectSiteType, shaped])) + intro _ _ retained + exact ValuesValid.cons effect.object_valid retained.tail + | method signature => simp [shaped, allocateElements] at success + | unsupported reason => simp [shaped, allocateElements] at success + +#print axioms StepContract.newarr /-- A successful pointer subtraction forms a valid result without changing its capability. -/ theorem referenceSubtract_valid (memory : Memory) (reference result : ManagedReference) @@ -264,38 +477,38 @@ theorem referenceSubtract_valid (memory : Memory) (reference result : ManagedRef · exact displaceReference_valid success · cases success #print axioms referenceSubtract_valid -/-- Constructing a view and storing it changes only the destination local. -/ -theorem StepContract.initializeSpanLocal {body : CIL.Method} {operation : CIL.Op} {frame : Frame} - {stack rest : List Value} {memory result : Memory} {construction : Memory × List Value} - {index pc : Nat} {slot final : LocalSlot} {value : Value} - {constructor : CIL.SpanConstructor} +/-- Constructing a span and storing it through the home beneath its arguments changes only the + slot the home names. -/ +theorem StepContract.spanInPlace {body : CIL.Method} {site : Nat} {operation : CIL.Instruction} + {frame : Frame} {stack rest : List Value} {memory result : Memory} + {construction : Memory × List Value} {index pc : Nat} {slot final : LocalSlot} + {value home : Value} {constructor : CIL.SpanConstructor} (found : frame.locals[index]? = some slot) - (constructed : memoryInstruction constructor.operation stack memory = - .ok construction) - (values : construction.2 = value :: rest) + (constructed : memoryInstruction constructor.operation stack memory = .ok construction) + (values : construction.2 = value :: home :: rest) (stored : storeLocal construction.1 slot value = .ok (final, result)) : - StepContract body operation frame stack memory + StepContract body site operation frame stack memory (.next pc rest { frame with locals := frame.locals.set index final } result) := by obtain ⟨constructedMemory, constructedValues⟩ := construction dsimp only at values stored subst constructedValues obtain ⟨same, _⟩ := spanConstructor_result constructor memory constructedMemory stack - value rest constructed + value (home :: rest) constructed subst constructedMemory refine ⟨.store found stored, ?_⟩ intro formed valid - exact ((memoryInstruction_contract _ _ _ _ _ constructed).values formed valid).tail.preserve + exact ((memoryInstruction_contract _ _ _ _ _ constructed).values formed valid).tail.tail.preserve formed (storeLocal_extends_allocations _ _ _ _ _ stored) -#print axioms StepContract.initializeSpanLocal +#print axioms StepContract.spanInPlace /-- Managed identities change no memory; dispatched arguments are checked before use. -/ -theorem managedStep_contract {dispatched : CIL.Op} (body : CIL.Method) - (operation : CIL.ManagedOp) (pc : Nat) +theorem calliStep_contract {dispatched : CIL.Instruction} (body : CIL.Method) + (signature : CIL.ManagedSignature) (tail : Bool) (link : Option CIL.Link) (pc : Nat) (frame : Frame) (stack : List Value) (memory : Memory) (action : FrameAction) - (executed : managedStep operation pc frame stack memory = .ok action) : - StepContract body dispatched frame stack memory action := by - unfold managedStep at executed + (executed : calliStep signature tail link frame stack memory = .ok action) : + StepContract body pc dispatched frame stack memory action := by + unfold calliStep at executed simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed repeat' first | solve | @@ -307,37 +520,156 @@ theorem managedStep_contract {dispatched : CIL.Op} (body : CIL.Method) | exact retained.tail | exact retained.tail.tail | exact retained.tail.drop _ - | exact (checkedValues_valid _ _ _ (by assumption)).2 + | exact callArguments_valid (by assumption) | (apply ValuesValid.cons; first | trivial | exact retained | exact retained.tail.tail) | constructor | cases executed | split at executed -#print axioms managedStep_contract - +#print axioms calliStep_contract + +/-- An instance field load that succeeds preserves checked effects and stack validity. -/ +theorem fieldLoad_contract (body : CIL.Method) (field : CIL.Extraction.FieldRef) + (stack values : List Value) (memory result : Memory) + (executed : fieldLoad body field stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold fieldLoad at executed + split at executed + · exact classFieldLoad_contract _ _ _ _ _ _ _ executed + · exact loadValueField_contract _ _ _ _ _ _ _ executed + · exact loadAggregateField_contract _ _ _ _ _ _ executed + · cases executed +#print axioms fieldLoad_contract + +/-- An instance field store that succeeds preserves checked effects and stack validity. -/ +theorem fieldStore_contract (body : CIL.Method) (field : CIL.Extraction.FieldRef) + (stack values : List Value) (memory result : Memory) + (executed : fieldStore body field stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold fieldStore at executed + split at executed + · exact classFieldStore_contract _ _ _ _ _ _ _ executed + · exact storeValueField_contract _ _ _ _ _ _ executed + · exact storeAggregateField_contract _ _ _ _ _ _ executed + · cases executed +#print axioms fieldStore_contract + +/-- An instance field address that succeeds preserves checked effects and stack validity. -/ +theorem fieldReference_contract (body : CIL.Method) (field : CIL.Extraction.FieldRef) + (stack values : List Value) (memory result : Memory) + (executed : fieldReference body field stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold fieldReference at executed + split at executed + · exact structFieldAddress_contract _ _ _ _ _ executed + · exact structFieldAddress_contract _ _ _ _ _ executed + · cases executed + · cases executed +#print axioms fieldReference_contract + +/-- A runtime model preserves the caller's frame and has the checked effect of its operation. -/ +theorem modelStep_contract {body : CIL.Method} {model : Option CIL.RuntimeModel} + {pc : Nat} {frame : Frame} {stack : List Value} {memory : Memory} {action : FrameAction} + {operation : CIL.Instruction} + (executed : modelStep model body pc frame stack memory = .ok action) : + StepContract body pc operation frame stack memory action := by + unfold modelStep observedCall runModel intrinsicCall featureCall referenceCompareCall + referenceSubtractCall typedTransferCall skipInitCall spanInPlaceCall at executed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' first | split at executed | cases executed + all_goals first + | (apply StepContract.spanInPlace <;> first | assumption | rfl) + | exact StepContract.instruction (memoryInstruction_contract _ _ _ _ _ (by assumption)) + | exact StepContract.instruction (observedTransfer_contract _ _ _ _ _ _ (by assumption)) + | exact StepContract.viewed (by assumption) + (StepContract.instruction (memoryInstruction_contract _ _ _ _ _ (by assumption))) + | refine StepContract.of_effect (.memory (.ordinary (.unchanged _))) ?_ + all_goals + intro formed extended retained + first + | exact retained.tail + | apply ValuesValid.cons + · first + | trivial + | exact (checkedValue_valid _ _ _ (by assumption)).2 + | exact (referenceSubtract_valid _ _ _ _ (by assumption)).1 + | exact (referenceSubtract_valid _ _ _ _ (by assumption)).2 + · first | exact retained | exact retained.tail | exact retained.tail.tail | exact retained.drop _ + +#print axioms modelStep_contract + +-- The walk splits every arm of both dispatcher matches, argument homes included. +set_option maxHeartbeats 400000 in /-- Every successful checked step has a `StepContract`. The proof walks every dispatcher arm, so it is the place that breaks when an instruction is added without an effect. -/ -theorem step_contract (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +theorem step_contract (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) (h : step body operation pc args frame stack m = .ok action) : - StepContract body operation frame stack m action := by - unfold step at h + StepContract body pc operation frame stack m action := by + unfold step referenceCompareCall referenceSubtractCall newobjStep + constructAggregate constructObject observedCall initializeSpanHome at h split at h <;> try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h all_goals repeat' first - | solve | apply managedStep_contract; assumption - | solve | - apply StepContract.initializeSpanLocal - all_goals assumption + | solve | exact modelStep_contract (by assumption) + | solve | apply calliStep_contract; assumption + | solve | exact StepContract.storeArgument (by assumption) + | unfold returnFrom at h + | unfold localAllocation at h + | unfold initializeSpanHome at h + | unfold intrinsicCall at h + | unfold referenceCompareCall at h + | unfold referenceSubtractCall at h + | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h | solve | exact .stackAllocation (by assumption) | solve | exact .arrayAllocation (by assumption) - | solve | exact .referenceArrayAllocation (by assumption) - | solve | exact .objectAllocation (by assumption) (by assumption) + | solve | exact .newarr (by assumption) + | solve | exact .objectAllocation (by assumption) (by assumption) (by assumption) | solve | + apply StepContract.viewed (by assumption) apply StepContract.staticInstruction apply staticInstruction_contract assumption | solve | + apply StepContract.staticInstruction + first + | (apply staticLoad_contract; assumption) + | (apply staticAddress_contract; assumption) + | solve | + apply StepContract.instruction + first + | exact otherMemoryInstruction_contract (.copyBlock _) _ _ _ _ (by assumption) + | exact otherMemoryInstruction_contract (.initBlock _) _ _ _ _ (by assumption) + | exact stringLiteralInstruction_contract _ _ _ _ _ (by assumption) + | exact instruction_contract _ _ _ _ _ (by assumption) + | solve | + apply StepContract.viewed (by assumption) apply StepContract.instruction - apply instruction_contract + apply memoryInstruction_contract + assumption + | solve | + apply StepContract.instruction + apply referenceAddInstruction_contract + assumption + | solve | + apply StepContract.instruction + apply typedElementInstruction_contract + assumption + | solve | + apply StepContract.instruction + apply elementAddressInstruction_contract + assumption + | solve | + apply StepContract.instruction + first + | (apply copyInitializedBytes_contract; assumption) + | (apply initializeBytes_contract; assumption) + | (apply transferObject_contract; assumption) + | (apply fieldLoad_contract; assumption) + | (apply fieldStore_contract; assumption) + | (apply fieldReference_contract; assumption) + | solve | + apply StepContract.viewed (by assumption) + apply StepContract.instruction + apply transferObject_contract assumption | solve | refine StepContract.of_effect ?_ ?_ @@ -357,6 +689,7 @@ theorem step_contract (body : CIL.Method) (operation : CIL.Op) (pc : Nat) | exact retained.tail.tail | solve | exact retained.drop _ | solve | apply checkedValues_preserved _ _ _ _ hm ext; assumption + | solve | apply callArguments_preserved _ _ _ _ _ hm ext; assumption | solve | apply ValuesValid.append · apply checkedValues_preserved _ _ _ _ hm ext; assumption @@ -365,11 +698,14 @@ theorem step_contract (body : CIL.Method) (operation : CIL.Op) (pc : Nat) apply ValuesValid.cons · first | rfl + | trivial | exact (checkedValue_valid _ _ _ (by assumption)).2 | exact objectTest_valid (by assumption) | exact (referenceSubtract_valid _ _ _ _ (by assumption)).1 | exact (referenceSubtract_valid _ _ _ _ (by assumption)).2 | apply loadLocal_result_valid; assumption + | apply loadArgument_result_valid; assumption + | apply addressArgument_result_valid; assumption | apply localAddress_result_valid; assumption | apply allocated_store_reference_valid; exact hm; assumption; assumption | apply allocated_storeLocal_reference_valid; exact hm; assumption; assumption @@ -379,19 +715,115 @@ theorem step_contract (body : CIL.Method) (operation : CIL.Op) (pc : Nat) | exact retained.tail.tail | exact retained.tail | apply checkedValues_preserved _ _ _ _ hm ext; assumption + | apply callArguments_preserved _ _ _ _ _ hm ext; assumption | constructor | cases h | split at h #print axioms step_contract -theorem step_effect (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +/-- No value `action` hands to another frame is a home: the arguments of a call, tail call or + construction and the returned values. A `.next` keeps its stack in the same frame. -/ +def FrameAction.HomeFree : FrameAction → Prop + | .next .. => True + | .call _ arguments _ _ | .tailCall _ arguments _ | .construct _ arguments _ _ _ _ _ + | .returned arguments _ => ∀ value ∈ arguments, value.isHome = false + +/-- Call arguments read from a frame contain no home. -/ +theorem callArguments_not_spanHome {frame : Frame} {m : Memory} {values checked : List Value} + (h : values.mapM (callArgument frame m) = .ok checked) : + ∀ value ∈ checked, value.isHome = false := + mapM_ok_forall (fun _ _ read => (callArgument_checked read).2) h + +#print axioms callArguments_not_spanHome + +/-- Closes the home-freedom of an action built from checked lists and fixed values. -/ +local macro "home_free_close" : tactic => `(tactic| ( + dsimp only [FrameAction.HomeFree] + all_goals first + | trivial + | exact callArguments_not_spanHome (by assumption) + | exact checkedValues_not_spanHome (by assumption) + | exact List.forall_mem_cons.2 ⟨rfl, callArguments_not_spanHome (by assumption)⟩ + | exact List.forall_mem_cons.2 ⟨rfl, fun _ member => nomatch member⟩)) + + +/-- A managed pointer call hands its callee checked arguments, none of them a home. -/ +theorem calliStep_homeFree {signature : CIL.ManagedSignature} {tail : Bool} {link : Option CIL.Link} + {frame : Frame} {stack : List Value} {memory : Memory} {action : FrameAction} + (executed : calliStep signature tail link frame stack memory = .ok action) : + action.HomeFree := by + unfold calliStep at executed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' first + | (cases executed; home_free_close; done) + | (cases executed; done) + | split at executed + +#print axioms calliStep_homeFree + +/-- No successful step hands a home to another frame, so a home never leaves the frame whose + slot it names. -/ +theorem step_homeFree {body : CIL.Method} {operation : CIL.Instruction} {pc : Nat} + {args : List Value} {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} + (h : step body operation pc args frame stack m = .ok action) : action.HomeFree := by + unfold step referenceCompareCall referenceSubtractCall newobjStep + constructAggregate constructObject modelStep observedCall runModel featureCall + typedTransferCall skipInitCall initializeSpanHome spanInPlaceCall at h + split at h <;> try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h + all_goals repeat' first + | (cases h; home_free_close; done) + | (cases h; done) + | exact calliStep_homeFree h + | unfold returnFrom at h + | unfold localAllocation at h + | unfold initializeSpanHome at h + | unfold intrinsicCall at h + | unfold referenceCompareCall at h + | unfold referenceSubtractCall at h + | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h + | split at h + +#print axioms step_homeFree + +/-- A list holding an element that `apply` refuses is refused by `mapM`. -/ +theorem mapM_error_of_mem {α β ε : Type} {apply : α → Except ε β} {value : α} {fault : ε} + (refused : apply value = .error fault) : + ∀ {values : List α}, value ∈ values → ∃ fault, values.mapM apply = .error fault + | head :: rest, member => by + rw [List.mapM_cons] + cases headResult : apply head with + | error other => exact ⟨other, by simp only [Bind.bind, Except.bind]⟩ + | ok result => + cases member with + | head => rw [refused] at headResult; cases headResult + | tail _ inRest => + obtain ⟨other, failed⟩ := mapM_error_of_mem refused inRest + exact ⟨other, by simp only [failed, Bind.bind, Except.bind]⟩ + +#print axioms mapM_error_of_mem + +/-- A home among the entry arguments stops `invokeWith` before any frame starts, so no frame + begins holding a home that names a slot of a frame that does not exist. -/ +theorem invokeWith_spanHome (lookup : Nat → Option CIL.Method) (fuel method : Nat) + {args : List Value} {m : Memory} {slot : Nat} (forged : Value.spanHome slot ∈ args) : + ∃ fault, invokeWith lookup fuel method args m = .error ⟨method, 0, fault⟩ := by + obtain ⟨fault, refused⟩ := mapM_error_of_mem (apply := checkedValue m) rfl forged + unfold invokeWith + cases lookup method with + | none => exact ⟨.invalidState, rfl⟩ + | some body => + exact ⟨fault, by simp only [refused, Except.mapError, Bind.bind, Except.bind]⟩ + +#print axioms invokeWith_spanHome + +theorem step_effect (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) (h : step body operation pc args frame stack m = .ok action) : - StepEffect body operation frame m (action.frameAfter frame) action.memory := + StepEffect body pc operation frame m (action.frameAfter frame) action.memory := (step_contract _ _ _ _ _ _ _ _ h).effect -theorem step_preserves_wellFormed (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +theorem step_preserves_wellFormed (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) (hm : m.WellFormed) (h : step body operation pc args frame stack m = .ok action) : action.memory.WellFormed := by diff --git a/CIL/Safety/ExecutionLemmas.lean b/CIL/Safety/ExecutionLemmas.lean index f9fd23c8f..ef5c41dd8 100644 --- a/CIL/Safety/ExecutionLemmas.lean +++ b/CIL/Safety/ExecutionLemmas.lean @@ -18,14 +18,14 @@ theorem run_success_step (program : CIL.Program) (fuel method pc : Nat) ∃ body op action, program[method]? = some body ∧ body.code[pc]? = some op ∧ step body op pc args frame stack m = .ok action := by cases hb : program[method]? with - | none => simp [run, hb] at success + | none => simp [run_succ, hb] at success | some body => cases ho : body.code[pc]? with - | none => simp [run, hb, ho] at success + | none => simp [run_succ, hb, ho] at success | some op => cases hs : step body op pc args frame stack m with | error fault => - simp [run, hb, ho, hs, Bind.bind, Except.bind, Except.mapError] at success + simp [run_succ, hb, ho, hs, Bind.bind, Except.bind, Except.mapError] at success | ok action => exact ⟨body, op, action, rfl, ho, hs⟩ #print axioms run_success_step diff --git a/CIL/Safety/ExecutionRenaming.lean b/CIL/Safety/ExecutionRenaming.lean index 4e4a89f5c..e7fde3825 100644 --- a/CIL/Safety/ExecutionRenaming.lean +++ b/CIL/Safety/ExecutionRenaming.lean @@ -25,11 +25,11 @@ theorem MethodRenaming.step_success (transport : StepTransport) (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) (statics : source.StaticBindingsBounded) {arguments stack : List Value} (argumentValues : ValuesValid source arguments) (stackValues : ValuesValid source stack) - {operation : CIL.Op} {pc : Nat} {action : FrameAction} + {operation : CIL.Instruction} {pc : Nat} {action : FrameAction} (stepped : step sourceBody operation pc arguments sourceFrame stack source = .ok action) : ∃ later targetAction, mapping.Extends later ∧ step targetBody (methods.indices.operation operation) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target = .ok targetAction ∧ + targetFrame (stack.map (methods.indices.value mapping)) target = .ok targetAction ∧ FrameAction.Renamed methods.indices later scopes action targetAction := by obtain ⟨later, extension, actions⟩ := transport methods.indices mapping scopes sourceBody targetBody operation pc arguments stack sourceFrame targetFrame source target @@ -83,7 +83,7 @@ theorem RunTrace.rename (transport : StepTransport) (argumentValues : ValuesValid source arguments) (stackValues : ValuesValid source stack) : SuccessfulRenaming mapping scopes final values (run targetProgram fuel method pc (arguments.map mapping.value) targetFrame - (stack.map mapping.value) target) := by + (stack.map (methods.indices.value mapping)) target) := by induction trace generalizing sourceBody targetBody mapping scopes target targetFrame with | @next fuel method pc arguments sourceFrame stack source nextPc nextStack nextFrame updated final values actual following induction => @@ -104,7 +104,7 @@ theorem RunTrace.rename (transport : StepTransport) have argumentBounds : ∀ value ∈ arguments, value.Below mapping.sourceLimit := by rw [memory.sourceLimit] exact argumentValues.below sourceFormed - simp only [run, targetFound, methods.instruction fetched, targetStepped, + simp only [run_succ, targetFound, methods.instruction fetched, targetStepped, Except.mapError, Bind.bind, Except.bind] rw [← extension.values arguments argumentBounds] exact continued.prepend extension (fun _ _ scope => scope) @@ -128,7 +128,7 @@ theorem RunTrace.rename (transport : StepTransport) obtain ⟨result, targetChecked, equal⟩ := checks.success subst result refine ⟨middle, scopes, _, extension, fun _ _ scope => scope, departed, ?_⟩ - simp only [run, targetFound, methods.instruction fetched, targetStepped, targetChecked, + simp only [run_succ, targetFound, methods.instruction fetched, targetStepped, targetChecked, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] | @call fuel method pc arguments sourceFrame stack source callee childArguments rest updated child childFrame childMemory returned result final values actual childFound entered @@ -196,10 +196,12 @@ theorem RunTrace.rename (transport : StepTransport) have restBounds : ∀ value ∈ rest, value.Below middle.sourceLimit := by rw [renamed.sourceLimit] exact live.2.below sourceReady - simp only [run, targetFound, methods.instruction fetched, targetStepped, + simp only [run_succ, targetFound, methods.instruction fetched, targetStepped, targetChildFound, targetEntry, targetFinished, Except.mapError, Bind.bind, Except.bind] + have resultFree := run_result_not_spanHome _ _ _ _ _ _ _ _ _ _ childFinished rw [← fullExtension.values arguments argumentBounds, - ← middleExtension.values rest restBounds, ← List.map_append] + ← methods.indices.values_extends middleExtension rest restBounds, + ← methods.indices.values_of_not_home later resultFree, ← List.map_append] exact continued.prepend fullExtension scopeExtension | @tailCall fuel method pc arguments sourceFrame stack source callee childArguments updated body child checked childFrame childMemory final values actual bodyLookup childFound @@ -250,9 +252,9 @@ theorem RunTrace.rename (transport : StepTransport) (by intro value member; cases member) simp only [List.map_nil] at continued rw [entryExtension.values checked checkedBounds] at continued - simp only [run, targetFound, methods.instruction fetched, targetStepped, targetChildFound, - childMethods.metadata.returnsValue, methods.metadata.returnsValue, returns, - Bool.false_eq_true, ite_false, targetCheck, targetEntry, Except.mapError, + simp only [run_succ, targetFound, methods.instruction fetched, targetStepped, + targetChildFound, childMethods.metadata.returnsValue, methods.metadata.returnsValue, + returns, Bool.false_eq_true, ite_false, targetCheck, targetEntry, Except.mapError, Bind.bind, Except.bind] exact continued.prepend (extension.trans entryExtension) (fun _ _ scope => Or.inl scope) | @construct fuel method pc arguments sourceFrame stack source callee childArguments rest @@ -331,12 +333,13 @@ theorem RunTrace.rename (transport : StepTransport) have restBounds : ∀ value ∈ rest, value.Below middle.sourceLimit := by rw [renamed.sourceLimit] exact live.2.below sourceReady - simp only [run, targetFound, methods.instruction fetched, targetStepped, + simp only [run_succ, targetFound, methods.instruction fetched, targetStepped, targetChildFound, targetEntry, targetFinished, List.isEmpty_nil, Bool.not_true, Bool.false_eq_true, ite_false, targetLoaded, Except.mapError, Bind.bind, Except.bind] rw [← fullExtension.values arguments argumentBounds, - ← middleExtension.values rest restBounds, ← List.map_cons] + ← methods.indices.values_extends middleExtension rest restBounds, + ← methods.indices.value_of_not_home later (loadLocal_not_spanHome loaded), ← List.map_cons] exact continued.prepend fullExtension scopeExtension #print axioms RunTrace.rename @@ -380,7 +383,7 @@ theorem invoke_rename (transport : StepTransport) (by intro value member; cases member) simp only [List.map_nil] at renamed rw [extension.values arguments bounds] at renamed - simp only [invoke, targetFound, targetChecked, targetEntry, + simp only [invoke_eq, targetFound, targetChecked, targetEntry, Except.mapError, Bind.bind, Except.bind] exact renamed.prepend extension (fun _ _ scope => Or.inl scope) #print axioms invoke_rename @@ -431,11 +434,12 @@ theorem MethodRenaming.step_throw (transport : StepTransport) (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) (statics : source.StaticBindingsBounded) {arguments stack : List Value} (argumentValues : ValuesValid source arguments) (stackValues : ValuesValid source stack) - {operation : CIL.Op} {pc : Nat} {exception : String} + {operation : CIL.Instruction} {pc : Nat} {exception : ExceptionType} (stepped : step sourceBody operation pc arguments sourceFrame stack source = .error (.thrown exception)) : step targetBody (methods.indices.operation operation) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target = .error (.thrown exception) := by + targetFrame (stack.map (methods.indices.value mapping)) target = + .error (.thrown exception) := by obtain ⟨later, _, actions⟩ := transport methods.indices mapping scopes sourceBody targetBody operation pc arguments stack sourceFrame targetFrame source target methods.metadata memory frames sourceFormed targetFormed @@ -451,7 +455,7 @@ theorem MethodRenaming.step_throw (transport : StepTransport) theorem ResultRenamed.located_throw {mapping : AllocationRenaming} {relation : α → β → Prop} {source : ExecutionResult α} {target : ExecutionResult β} (related : ResultRenamed mapping relation source target) {method pc : Nat} - {fault : LocatedFault} {exception : String} + {fault : LocatedFault} {exception : ExceptionType} (failed : source.mapError (fun fault => ⟨method, pc, fault⟩) = .error fault) (thrown : fault.fault = .thrown exception) : target.mapError (fun fault => ⟨method, pc, fault⟩) = .error fault := by @@ -469,7 +473,7 @@ theorem run_throw_rename (transport : StepTransport) {sourceProgram targetProgram : CIL.Program} (programs : ProgramRenaming sourceProgram targetProgram) {fuel method pc : Nat} {arguments stack : List Value} {sourceFrame : Frame} - {source : Memory} {fault : LocatedFault} {exception : String} + {source : Memory} {fault : LocatedFault} {exception : ExceptionType} (failed : run sourceProgram fuel method pc arguments sourceFrame stack source = .error fault) (thrown : fault.fault = .thrown exception) {sourceBody targetBody : CIL.Method} @@ -483,27 +487,27 @@ theorem run_throw_rename (transport : StepTransport) (statics : source.StaticBindingsBounded) (argumentValues : ValuesValid source arguments) (stackValues : ValuesValid source stack) : run targetProgram fuel method pc (arguments.map mapping.value) targetFrame - (stack.map mapping.value) target = .error fault := by + (stack.map (methods.indices.value mapping)) target = .error fault := by induction fuel generalizing method pc arguments stack sourceFrame source sourceBody targetBody mapping scopes target targetFrame with | zero => cases failed; cases thrown | succ fuel induction => cases fetched : sourceBody.code[pc]? with | none => - simp only [run, sourceFound, fetched] at failed + simp only [run_succ, sourceFound, fetched] at failed cases failed cases thrown | some operation => cases stepped : step sourceBody operation pc arguments sourceFrame stack source with | error reason => - simp only [run, sourceFound, fetched, stepped, Except.mapError, Bind.bind, + simp only [run_succ, sourceFound, fetched, stepped, Except.mapError, Bind.bind, Except.bind] at failed cases failed dsimp only at thrown subst reason have targetStepped := methods.step_throw transport memory frames sourceFormed targetFormed statics argumentValues stackValues stepped - simp only [run, targetFound, methods.instruction fetched, targetStepped, + simp only [run_succ, targetFound, methods.instruction fetched, targetStepped, Except.mapError, Bind.bind, Except.bind] | ok action => obtain ⟨middle, targetAction, extension, targetStepped, actions⟩ := @@ -516,9 +520,9 @@ theorem run_throw_rename (transport : StepTransport) have argumentBounds : ∀ value ∈ arguments, value.Below mapping.sourceLimit := by rw [memory.sourceLimit] exact argumentValues.below sourceFormed - simp only [run, sourceFound, fetched, stepped, Except.mapError, Bind.bind, + simp only [run_succ, sourceFound, fetched, stepped, Except.mapError, Bind.bind, Except.bind] at failed - simp only [run, targetFound, methods.instruction fetched, targetStepped, + simp only [run_succ, targetFound, methods.instruction fetched, targetStepped, Except.mapError, Bind.bind, Except.bind] cases actions with | next renamed nextFrames => @@ -625,10 +629,12 @@ theorem run_throw_rename (transport : StepTransport) have restBounds : ∀ value ∈ rest, value.Below middle.sourceLimit := by rw [renamed.sourceLimit] exact live.2.below sourceReady + have resultFree := run_result_not_spanHome _ _ _ _ _ _ _ _ _ _ childFinished rw [targetFinished] simp only [] rw [← fullExtension.values arguments argumentBounds, - ← middleExtension.values rest restBounds, ← List.map_append] + ← methods.indices.values_extends middleExtension rest restBounds, + ← methods.indices.values_of_not_home later resultFree, ← List.map_append] exact continued | @tailCall updated targetUpdated callee childArguments renamed => have live : ValuesValid updated childArguments := contract.values sourceFormed stackValues @@ -822,7 +828,9 @@ theorem run_throw_rename (transport : StepTransport) rw [targetLoaded] simp only [] rw [← fullExtension.values arguments argumentBounds, - ← middleExtension.values rest restBounds, ← List.map_cons] + ← methods.indices.values_extends middleExtension rest restBounds, + ← methods.indices.value_of_not_home later (loadLocal_not_spanHome loaded), + ← List.map_cons] exact continued #print axioms run_throw_rename @@ -835,17 +843,22 @@ theorem invoke_throw_rename (transport : StepTransport) (memory : Memory.Renamed mapping scopes source target) (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) (statics : source.StaticBindingsBounded) {fuel method : Nat} {arguments : List Value} - (argumentValues : ValuesValid source arguments) {fault : LocatedFault} {exception : String} + (argumentValues : ValuesValid source arguments) {fault : LocatedFault} + {exception : ExceptionType} (failed : invoke sourceProgram fuel method arguments source = .error fault) (thrown : fault.fault = .thrown exception) : invoke targetProgram fuel method (arguments.map mapping.value) target = .error fault := by cases found : sourceProgram[method]? with | none => - simp only [invoke, found] at failed + simp only [invoke_eq, found] at failed cases failed cases thrown | some body => - have checked := checkedValues_of_valid argumentValues + rcases checkedValues_of_valid_or_home argumentValues with checked | refused + case inr => + simp only [invoke_eq, found, refused, Except.mapError, Bind.bind, Except.bind] at failed + cases failed + cases thrown have bounds : ∀ value ∈ arguments, value.Below mapping.sourceLimit := by rw [memory.sourceLimit] exact argumentValues.below sourceFormed @@ -859,7 +872,8 @@ theorem invoke_throw_rename (transport : StepTransport) cases entry : enterFrame body arguments source with | ok result => exact ⟨result.1, result.2, rfl⟩ | error reason => - simp only [invoke, found, checked, entry, Except.mapError, Bind.bind, Except.bind] at failed + simp only [invoke_eq, found, checked, entry, Except.mapError, Bind.bind, + Except.bind] at failed cases failed exact False.elim (enterFrame_not_thrown body methods.sourceReady arguments source exception (by simpa only [show reason = .thrown exception from thrown] using entry)) @@ -868,7 +882,7 @@ theorem invoke_throw_rename (transport : StepTransport) memory.enterFrameInitialized sourceFormed targetFormed (by rw [memory.sourceLimit]; exact statics) methods.sourceReady methods.targetReady methods.indices methods.declarations arguments (arguments.map mapping.value) entry - simp only [invoke, found, checked, sourceEntry, Except.mapError, Bind.bind, + simp only [invoke_eq, found, checked, sourceEntry, Except.mapError, Bind.bind, Except.bind] at failed have renamed := run_throw_rename transport programs failed thrown found targetFound methods enteredMemory enteredFrames @@ -879,7 +893,7 @@ theorem invoke_throw_rename (transport : StepTransport) (by intro value member; cases member) simp only [List.map_nil] at renamed rw [extension.values arguments bounds] at renamed - simpa only [invoke, targetFound, targetChecked, targetEntry, + simpa only [invoke_eq, targetFound, targetChecked, targetEntry, Except.mapError, Bind.bind, Except.bind] using renamed #print axioms invoke_throw_rename @@ -930,7 +944,8 @@ theorem Raises.rename (transport : StepTransport) {initial : Memory} (formed : initial.WellFormed) (statics : initial.StaticBindingsBounded) (literals : initial.LiteralsBounded) {method : Nat} {arguments : List Value} (argumentValues : ValuesValid initial arguments) - {exception : String} (raised : Raises sourceProgram method arguments initial exception) : + {exception : ExceptionType} + (raised : Raises sourceProgram method arguments initial exception) : Raises targetProgram method arguments initial exception := by obtain ⟨fuel, fault, failed, thrown⟩ := raised have targetFailed := invoke_throw_rename transport programs diff --git a/CIL/Safety/ExecutionStaticWorld.lean b/CIL/Safety/ExecutionStaticWorld.lean index c04b17a9b..9a0efe03e 100644 --- a/CIL/Safety/ExecutionStaticWorld.lean +++ b/CIL/Safety/ExecutionStaticWorld.lean @@ -26,7 +26,7 @@ theorem program_static_sites_valid (program : CIL.Program) (method : Nat) (body exact List.mem_map.mpr ⟨site, member, rfl⟩ theorem step_preserves_static_world (descriptors : List CIL.StaticDescriptor) - (body : CIL.Method) (operation : CIL.Op) (pc : Nat) (args : List Value) + (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) (hm : m.WellFormed) (sites : StaticSitesValid descriptors body) (world : StaticWorldValid descriptors m) diff --git a/CIL/Safety/ExecutionTrace.lean b/CIL/Safety/ExecutionTrace.lean index d34d088b2..707218c48 100644 --- a/CIL/Safety/ExecutionTrace.lean +++ b/CIL/Safety/ExecutionTrace.lean @@ -84,85 +84,88 @@ theorem run_success_trace (program : CIL.Program) (fuel method pc : Nat) | zero => cases h | succ fuel ih => cases hb : program[method]? with - | none => simp [run, hb] at h + | none => simp [run_succ, hb] at h | some body => cases ho : body.code[pc]? with - | none => simp [run, hb, ho] at h + | none => simp [run_succ, hb, ho] at h | some op => cases hs : step body op pc args frame stack m with | error fault => - simp [run, hb, ho, hs, Except.mapError, Bind.bind, Except.bind] at h + simp [run_succ, hb, ho, hs, Except.mapError, Bind.bind, Except.bind] at h | ok action => have actual : ProgramStep program method pc args frame stack m action := ⟨body, op, hb, ho, hs⟩ cases action with | next target nextStack nextFrame memory => - simp only [run, hb, ho, hs, Except.mapError, Bind.bind, Except.bind] at h + simp only [run_succ, hb, ho, hs, Except.mapError, Bind.bind, Except.bind] at h exact .next actual (ih method target args nextFrame nextStack memory final values h) | returned result memory => cases hr : result.mapM (checkedValue (leaveFrame frame memory)) <;> - simp only [run, hb, ho, hs, hr, Except.mapError, Bind.bind, Except.bind, + simp only [run_succ, hb, ho, hs, hr, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] at h · cases h · cases h exact .returned actual hr | call callee arguments rest memory => cases hc : program[callee]? with - | none => simp [run, hb, ho, hs, hc, Except.mapError, Bind.bind, Except.bind] at h + | none => simp [run_succ, hb, ho, hs, hc, Except.mapError, Bind.bind, Except.bind] at h | some child => cases he : enterFrame child arguments memory with | error fault => - simp [run, hb, ho, hs, hc, he, Except.mapError, Bind.bind, Except.bind] at h + simp [run_succ, hb, ho, hs, hc, he, Except.mapError, Bind.bind, Except.bind] at h | ok entered => obtain ⟨childFrame, childMemory⟩ := entered cases hr : run program fuel callee 0 arguments childFrame [] childMemory with | error fault => - simp [run, hb, ho, hs, hc, he, hr, Except.mapError, Bind.bind, Except.bind] at h + simp [run_succ, hb, ho, hs, hc, he, hr, Except.mapError, Bind.bind, + Except.bind] at h | ok returned => obtain ⟨childFinal, result⟩ := returned - simp only [run, hb, ho, hs, hc, he, hr, Except.mapError, Bind.bind, + simp only [run_succ, hb, ho, hs, hc, he, hr, Except.mapError, Bind.bind, Except.bind] at h exact .call actual hc he hr (ih callee 0 arguments childFrame [] childMemory childFinal result hr) (ih method (pc + 1) args frame (result ++ rest) childFinal final values h) | tailCall callee arguments memory => cases hc : program[callee]? with - | none => simp [run, hb, ho, hs, hc, Except.mapError, Bind.bind, Except.bind] at h + | none => simp [run_succ, hb, ho, hs, hc, Except.mapError, Bind.bind, Except.bind] at h | some child => cases returns : child.returnsValue != body.returnsValue with | true => - simp [run, hb, ho, hs, hc, returns, Except.mapError, Bind.bind, Except.bind] at h + simp [run_succ, hb, ho, hs, hc, returns, Except.mapError, Bind.bind, + Except.bind] at h | false => cases check : arguments.mapM (checkedValue (leaveFrame frame memory)) with | error fault => - simp [run, hb, ho, hs, hc, returns, check, Except.mapError, + simp [run_succ, hb, ho, hs, hc, returns, check, Except.mapError, Bind.bind, Except.bind] at h | ok checked => cases enter : enterFrame child checked (leaveFrame frame memory) with | error fault => - simp [run, hb, ho, hs, hc, returns, check, enter, Except.mapError, + simp [run_succ, hb, ho, hs, hc, returns, check, enter, Except.mapError, Bind.bind, Except.bind] at h | ok entered => obtain ⟨childFrame, childMemory⟩ := entered - simp only [run, hb, ho, hs, hc, returns, check, enter, Except.mapError, + simp only [run_succ, hb, ho, hs, hc, returns, check, enter, Except.mapError, Bind.bind, Except.bind, Bool.false_eq_true, ite_false] at h exact .tailCall actual hb hc returns check enter h (ih callee 0 checked childFrame [] childMemory final values h) | construct callee arguments rest parentFrame temporary kind memory => cases hc : program[callee]? with - | none => simp [run, hb, ho, hs, hc, Except.mapError, Bind.bind, Except.bind] at h + | none => simp [run_succ, hb, ho, hs, hc, Except.mapError, Bind.bind, Except.bind] at h | some child => cases he : enterFrame child arguments memory with | error fault => - simp [run, hb, ho, hs, hc, he, Except.mapError, Bind.bind, Except.bind] at h + simp [run_succ, hb, ho, hs, hc, he, Except.mapError, Bind.bind, Except.bind] at h | ok entered => obtain ⟨childFrame, childMemory⟩ := entered cases hr : run program fuel callee 0 arguments childFrame [] childMemory with | error fault => - simp [run, hb, ho, hs, hc, he, hr, Except.mapError, Bind.bind, Except.bind] at h + simp [run_succ, hb, ho, hs, hc, he, hr, Except.mapError, Bind.bind, + Except.bind] at h | ok returned => obtain ⟨childFinal, result⟩ := returned - simp only [run, hb, ho, hs, hc, he, hr, Except.mapError, Bind.bind, + simp only [run_succ, hb, ho, hs, hc, he, hr, Except.mapError, Bind.bind, Except.bind] at h cases result with | cons value rest' => simp at h @@ -187,25 +190,26 @@ theorem RunTrace.success {program : CIL.Program} {fuel method pc : Nat} induction trace with | next step _ ih => obtain ⟨body, op, hb, ho, hs⟩ := step - simpa only [run, hb, ho, hs, Except.mapError, Bind.bind, Except.bind] using ih + simpa only [run_zero, run_succ, hb, ho, hs, Except.mapError, Bind.bind, Except.bind] using ih | returned step checked => obtain ⟨body, op, hb, ho, hs⟩ := step - simp only [run, hb, ho, hs, checked, Except.mapError, Bind.bind, Except.bind, + simp only [run_succ, hb, ho, hs, checked, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] | call step lookup enter finish _ _ _ ih => obtain ⟨body, op, hb, ho, hs⟩ := step - simpa only [run, hb, ho, hs, lookup, enter, finish, Except.mapError, Bind.bind, + simpa only [run_zero, run_succ, hb, ho, hs, lookup, enter, finish, Except.mapError, Bind.bind, Except.bind] using ih | tailCall step bodyLookup lookup returns check enter finish _ ih => obtain ⟨body, op, hb, ho, hs⟩ := step have same := Option.some.inj (hb.symm.trans bodyLookup) subst same - simpa only [run, hb, ho, hs, lookup, returns, check, enter, Except.mapError, + simpa only [run_zero, run_succ, hb, ho, hs, lookup, returns, check, enter, Except.mapError, Bind.bind, Except.bind, Bool.false_eq_true, ite_false] using ih | construct step lookup enter finish _ load _ _ ih => obtain ⟨body, op, hb, ho, hs⟩ := step - simpa only [run, hb, ho, hs, lookup, enter, finish, load, Except.mapError, Bind.bind, - Except.bind, List.isEmpty_nil, Bool.not_true, Bool.false_eq_true, ite_false] using ih + simpa only [run_zero, run_succ, hb, ho, hs, lookup, enter, finish, load, Except.mapError, + Bind.bind, Except.bind, List.isEmpty_nil, Bool.not_true, Bool.false_eq_true, + ite_false] using ih /-- Separate invocation setup from execution once, including checked arguments. -/ theorem invoke_success_setup {program : CIL.Program} {fuel method : Nat} @@ -215,7 +219,7 @@ theorem invoke_success_setup {program : CIL.Program} {fuel method : Nat} program[method]? = some body ∧ args.mapM (checkedValue m) = .ok arguments ∧ enterFrame body arguments m = .ok (frame, entered) ∧ run program fuel method 0 arguments frame [] entered = .ok (final, values) := by - unfold invoke at h + rw [invoke_eq] at h cases hb : program[method]? with | none => simp [hb] at h | some body => diff --git a/CIL/Safety/FailureRecords.lean b/CIL/Safety/FailureRecords.lean index 5e0c843cc..2b78fffc6 100644 --- a/CIL/Safety/FailureRecords.lean +++ b/CIL/Safety/FailureRecords.lean @@ -57,7 +57,8 @@ structure FailureRecord where /-- `record` as one line of JSON, with its expressions printed in the current context. -/ def FailureRecord.render (record : FailureRecord) : MetaM String := do - let text (data : MessageData) : MetaM Json := + -- An unassigned part prints as `?_`, so a record does not depend on metavariable numbering. + let text (data : MessageData) : MetaM Json := withOptions (pp.mvars.set · false) do return Json.str (← (← addMessageContext data).toString) let parts (entries : Array (String × Expr)) : MetaM Json := do return Json.mkObj (← entries.toList.mapM fun (name, value) => do diff --git a/CIL/Safety/FieldValues.lean b/CIL/Safety/FieldValues.lean index a9a10a0c8..e8bca533e 100644 --- a/CIL/Safety/FieldValues.lean +++ b/CIL/Safety/FieldValues.lean @@ -8,10 +8,10 @@ after each field stores its value, as `initobj` followed by one `stfld` per fiel proof that tracks a struct local this way names its field values, such as `[.i32 count, .i32 sum]`, instead of its bytes, offsets and widths. On a valid, disjoint layout a field store replaces exactly that value (`AggregateLayout.setIn_encode`), and a field holding an int, word or -vector of exactly its width reads back as that value, by value (`step_fieldAt_encode`) or through -the struct's address (`fieldAt_encode_home`). A 1- or 2-byte field stores a truncated int, which -reads back as `narrowLoad`, its low bytes zero or sign extended (`step_fieldAt_encode_narrow`, -`fieldAt_encode_home_narrow`). +vector of exactly its width reads back as that value, by value (`step_ldfld_encode`) or through +the struct's address (`ldfld_encode_home`). A 1- or 2-byte field stores a truncated int, which +reads back as `narrowLoad`, its low bytes zero or sign extended (`step_ldfld_encode_narrow`, +`ldfld_encode_home_narrow`). `AggregateLayout.zeros` are the field values of a zeroed struct, each the value its kind loads from zero bytes (`FieldLayout.zero_loaded`, `FieldLayout.zero_nested`): an int for a 1-, 2- or @@ -284,53 +284,56 @@ namespace CIL.Safety /-- A by-value `ldfld` of a field holding `number` in struct bytes that contain it pushes the value an exact field `value` with that number decodes to, signed or not. The step holds in any method and frame. -/ -theorem step_fieldAt_value {bytes : List (BitVec 8)} {offset width : Nat} {value : CIL.Value} +theorem step_ldfld_value {bytes : List (BitVec 8)} {offset width : Nat} {value : CIL.Value} (within : offset + width ≤ bytes.length) (exact : FieldExact width value) (decoded : byteNumber ((bytes.drop offset).take width) = fieldNumber width value) : - ∀ body pc args frame memory rest signed, - step body (.fieldAt offset width signed) pc args frame (.scalar (.aggregate bytes) :: rest) + ∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args frame memory rest, + step body (.ldfld operand) pc args frame (.scalar (.aggregate bytes) :: rest) memory = .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := by - intro body pc args frame memory rest signed + intro body operand signed shape pc args frame memory rest have pushed := exact.decoded false unfold FieldExact at exact - rw [step_fieldAt_eq] + rw [step_ldfld_value_eq _ _ _ _ _ _ _ _ _ shape] split at exact <;> (try cases exact) <;> - simp_all [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, + simp_all [loadValueField, cil_guard, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, numericOfWidth, Bind.bind, Except.bind, Pure.pure, Except.pure, Nat.not_lt.2 within] /-- A by-value `ldfld` of field `index` of a struct of a valid, disjoint layout whose fields hold `values` pushes the exact value at `index`. -/ -theorem step_fieldAt_encode {layout : AggregateLayout} (valid : layout.Valid) (index : Nat) +theorem step_ldfld_encode {layout : AggregateLayout} (valid : layout.Valid) (index : Nat) (values : List CIL.Value) {offset width : Nat} {aggregate : Bool} (found : layout.fields[index]? = some ⟨offset, width, aggregate⟩ := by rfl) {value : CIL.Value} (listed : values[index]? = some value := by rfl) (exact : FieldExact width value := by trivial) (disjoint : layout.Disjoint := by decide) : - ∀ body pc args frame memory rest signed, - step body (.fieldAt offset width signed) pc args frame + ∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args frame memory rest, + step body (.ldfld operand) pc args frame (.scalar (.aggregate (layout.encode values)) :: rest) memory = .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := - step_fieldAt_value (layout.within_encode valid values _ (List.mem_of_getElem? found)) exact + step_ldfld_value (layout.within_encode valid values _ (List.mem_of_getElem? found)) exact (layout.numberIn_encode valid disjoint found listed) -#print axioms step_fieldAt_value -#print axioms step_fieldAt_encode +#print axioms step_ldfld_value +#print axioms step_ldfld_encode /-- `ldfld` of field `index` through the address of a struct home holding a struct of a valid, disjoint layout whose fields hold `values` pushes the exact value at `index`. The step holds in any method and frame. -/ -theorem fieldAt_encode_home {memory : Memory} {home : Reference} {layout : AggregateLayout} +theorem ldfld_encode_home {memory : Memory} {home : Reference} {layout : AggregateLayout} (formed : memory.WellFormed) (valid : layout.Valid) (disjoint : layout.Disjoint) {values : List CIL.Value} (loaded : read memory home (layout.encode values).length 1 = .ok (layout.encode values)) {index offset width : Nat} {aggregate : Bool} (found : layout.fields[index]? = some ⟨offset, width, aggregate⟩) {value : CIL.Value} (listed : values[index]? = some value) (exact : FieldExact width value) : - ∀ body pc args frame rest signed, - step body (.fieldAt offset width signed) pc args frame + ∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) memory = .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := by - intro body pc args frame rest signed + intro body operand signed shape pc args frame rest have within := layout.within_encode valid values _ (List.mem_of_getElem? found) have number := layout.numberIn_encode valid disjoint found listed have scalar : ScalarWidth width := by @@ -339,13 +342,13 @@ theorem fieldAt_encode_home {memory : Memory} {home : Reference} {layout : Aggre rw [← exact.decoded signed] cases signed with | false => - simpa [number] using step_fieldAt_home (field := ⟨offset, width, aggregate⟩) formed loaded - within scalar body pc args frame rest + simpa [number] using step_ldfld_unsigned_home (field := ⟨offset, width, aggregate⟩) formed loaded + within scalar body operand shape pc args frame rest | true => - simpa [number] using step_fieldAt_signed_home (field := ⟨offset, width, aggregate⟩) formed - loaded within scalar body pc args frame rest + simpa [number] using step_ldfld_signed_home (field := ⟨offset, width, aggregate⟩) formed + loaded within scalar body operand shape pc args frame rest -#print axioms fieldAt_encode_home +#print axioms ldfld_encode_home end CIL.Safety @@ -470,68 +473,71 @@ theorem narrowLoad_decoded {width : Nat} (narrow : width = 1 ∨ width = 2) (sig /-- A by-value `ldfld` of a 1- or 2-byte field holding the number an int `bits` stores pushes `narrowLoad` of it. The step holds in any method and frame. -/ -theorem step_fieldAt_narrow {bytes : List (BitVec 8)} {offset width : Nat} {bits : BitVec 32} +theorem step_ldfld_narrow {bytes : List (BitVec 8)} {offset width : Nat} {bits : BitVec 32} (within : offset + width ≤ bytes.length) (narrow : width = 1 ∨ width = 2) (decoded : byteNumber ((bytes.drop offset).take width) = fieldNumber width (.i32 bits)) : - ∀ body pc args frame memory rest signed, - step body (.fieldAt offset width signed) pc args frame (.scalar (.aggregate bytes) :: rest) + ∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args frame memory rest, + step body (.ldfld operand) pc args frame (.scalar (.aggregate bytes) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 (narrowLoad width signed bits)) :: rest) frame memory) := by - intro body pc args frame memory rest signed + intro body operand signed shape pc args frame memory rest have pushed := narrowLoad_decoded narrow signed bits - rw [step_fieldAt_eq] + rw [step_ldfld_value_eq _ _ _ _ _ _ _ _ _ shape] rcases narrow with rfl | rfl <;> cases signed <;> - simp_all [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, + simp_all [loadValueField, cil_guard, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, signedOfWidth, numericOfWidth, Bind.bind, Except.bind, Pure.pure, Except.pure, Nat.not_lt.2 within] /-- A by-value `ldfld` of 1- or 2-byte field `index` of a struct of a valid, disjoint layout whose field values hold the int `bits` there pushes `narrowLoad` of it. -/ -theorem step_fieldAt_encode_narrow {layout : AggregateLayout} (valid : layout.Valid) +theorem step_ldfld_encode_narrow {layout : AggregateLayout} (valid : layout.Valid) (index : Nat) (values : List CIL.Value) {offset width : Nat} {aggregate : Bool} (found : layout.fields[index]? = some ⟨offset, width, aggregate⟩ := by rfl) {bits : BitVec 32} (listed : values[index]? = some (.i32 bits) := by rfl) (narrow : width = 1 ∨ width = 2 := by decide) (disjoint : layout.Disjoint := by decide) : - ∀ body pc args frame memory rest signed, - step body (.fieldAt offset width signed) pc args frame + ∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args frame memory rest, + step body (.ldfld operand) pc args frame (.scalar (.aggregate (layout.encode values)) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 (narrowLoad width signed bits)) :: rest) frame memory) := - step_fieldAt_narrow (layout.within_encode valid values _ (List.mem_of_getElem? found)) narrow + step_ldfld_narrow (layout.within_encode valid values _ (List.mem_of_getElem? found)) narrow (layout.numberIn_encode valid disjoint found listed) /-- `ldfld` of 1- or 2-byte field `index` through the address of a struct home holding a struct of a valid, disjoint layout whose field values hold the int `bits` there pushes `narrowLoad` of it. The step holds in any method and frame. -/ -theorem fieldAt_encode_home_narrow {memory : Memory} {home : Reference} +theorem ldfld_encode_home_narrow {memory : Memory} {home : Reference} {layout : AggregateLayout} (formed : memory.WellFormed) (valid : layout.Valid) (disjoint : layout.Disjoint) {values : List CIL.Value} (loaded : read memory home (layout.encode values).length 1 = .ok (layout.encode values)) {index offset width : Nat} {aggregate : Bool} (found : layout.fields[index]? = some ⟨offset, width, aggregate⟩) {bits : BitVec 32} (listed : values[index]? = some (.i32 bits)) (narrow : width = 1 ∨ width = 2) : - ∀ body pc args frame rest signed, - step body (.fieldAt offset width signed) pc args frame + ∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 (narrowLoad width signed bits)) :: rest) frame memory) := by - intro body pc args frame rest signed + intro body operand signed shape pc args frame rest have within := layout.within_encode valid values _ (List.mem_of_getElem? found) have number := layout.numberIn_encode valid disjoint found listed have scalar : ScalarWidth width := by rcases narrow with rfl | rfl <;> simp [ScalarWidth] rw [← narrowLoad_decoded narrow signed bits] cases signed with | false => - simpa [number] using step_fieldAt_home (field := ⟨offset, width, aggregate⟩) formed loaded - within scalar body pc args frame rest + simpa [number] using step_ldfld_unsigned_home (field := ⟨offset, width, aggregate⟩) formed loaded + within scalar body operand shape pc args frame rest | true => - simpa [number] using step_fieldAt_signed_home (field := ⟨offset, width, aggregate⟩) formed - loaded within scalar body pc args frame rest + simpa [number] using step_ldfld_signed_home (field := ⟨offset, width, aggregate⟩) formed + loaded within scalar body operand shape pc args frame rest #print axioms narrowLoad_decoded -#print axioms step_fieldAt_narrow -#print axioms step_fieldAt_encode_narrow -#print axioms fieldAt_encode_home_narrow +#print axioms step_ldfld_narrow +#print axioms step_ldfld_encode_narrow +#print axioms ldfld_encode_home_narrow end CIL.Safety diff --git a/CIL/Safety/FixedStepHelpers.lean b/CIL/Safety/FixedStepHelpers.lean index 86431ffd7..e47e6529f 100644 --- a/CIL/Safety/FixedStepHelpers.lean +++ b/CIL/Safety/FixedStepHelpers.lean @@ -1,6 +1,7 @@ import CIL.Safety.StepTransport import CIL.Safety.PureStepRenaming import CIL.Safety.InstructionRenaming +import CIL.Safety.NativeMethodTableRenaming /-!+# Fixed-allocation checked steps @@ -10,12 +11,16 @@ Fresh allocation instructions are handled separately by the extending step simul namespace CIL.Safety +-- Stack lists are renamed by the local map of the frame that holds them. +variable {indices : LocalIndexMap} + /-- The ordinary dispatcher tail preserves corresponding frames and checked result stacks. -/ theorem Memory.Renamed.stepFallback {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (formed : source.WellFormed) (operation : CIL.Op) (pc : Nat) (profile : CIL.FeatureProfile) + (formed : source.WellFormed) (operation : CIL.Instruction) (pc : Nat) + (profile : CIL.FeatureProfile) (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (observable : SuccessOrThrow (do match pureArity operation with @@ -23,7 +28,7 @@ theorem Memory.Renamed.stepFallback {indices : LocalIndexMap} {mapping : Allocat if stack.length < argc then .error .invalidState else let values ← CIL.Safety.scalars (stack.take argc) let some (.next destination result _) := - CIL.step operation false pc [] 0 values (fun _ => none) profile | + CIL.step operation false none pc values (fun _ => none) profile | .error .invalidState let result ← (result.map Value.scalar).mapM (CIL.Safety.checkedValue source) return FrameAction.next destination (result ++ stack.drop argc) sourceFrame source @@ -37,7 +42,7 @@ theorem Memory.Renamed.stepFallback {indices : LocalIndexMap} {mapping : Allocat if stack.length < argc then .error .invalidState else let values ← CIL.Safety.scalars (stack.take argc) let some (.next destination result _) := - CIL.step operation false pc [] 0 values (fun _ => none) profile | + CIL.step operation false none pc values (fun _ => none) profile | .error .invalidState let result ← (result.map Value.scalar).mapM (CIL.Safety.checkedValue source) return FrameAction.next destination (result ++ stack.drop argc) sourceFrame source @@ -47,16 +52,18 @@ theorem Memory.Renamed.stepFallback {indices : LocalIndexMap} {mapping : Allocat (do match pureArity operation with | some argc => - if (stack.map mapping.value).length < argc then .error .invalidState else - let values ← CIL.Safety.scalars ((stack.map mapping.value).take argc) + if (stack.map (indices.value mapping)).length < argc then .error .invalidState else + let values ← CIL.Safety.scalars ((stack.map (indices.value mapping)).take argc) let some (.next destination result _) := - CIL.step operation false pc [] 0 values (fun _ => none) profile | + CIL.step operation false none pc values (fun _ => none) profile | .error .invalidState let result ← (result.map Value.scalar).mapM (CIL.Safety.checkedValue target) - return FrameAction.next destination (result ++ (stack.map mapping.value).drop argc) + return FrameAction.next destination + (result ++ (stack.map (indices.value mapping)).drop argc) targetFrame target | none => - let (memory, values) ← CIL.Safety.instruction operation (stack.map mapping.value) target + let (memory, values) ← + CIL.Safety.instruction operation (stack.map (indices.value mapping)) target return FrameAction.next (pc + 1) values targetFrame memory) := by cases arity : pureArity operation with | none => @@ -71,74 +78,101 @@ theorem Memory.Renamed.stepFallback {indices : LocalIndexMap} {mapping : Allocat simp only [arity, List.length_map, ← List.map_take, ← List.map_drop] split · exact .error .invalidState - · apply (mapping.scalarOperands (stack.take argc)).bind + · apply (indices.scalarOperands mapping (stack.take argc)).bind intro values otherValues same subst otherValues split · rename_i destination result ignored _ - apply (related.scalarChecks result).bind - intro checked otherChecked same + apply (related.scalarChecks result).bindWithSuccess + intro checked otherChecked checkedStep same subst otherChecked + rw [← indices.values_of_not_home mapping (checkedValues_not_spanHome checkedStep)] simpa only [List.map_append, pure, Except.pure] using ResultRenamed.ok (FrameAction.Renamed.next (pc := destination) (stack := checked ++ stack.drop argc) related frames) · exact .error .invalidState #print axioms Memory.Renamed.stepFallback +/-- An observable native table addition selects the same cursor in the renamed memory: renaming + keeps the table's stable binding identity and resolves it to the renamed allocation. -/ +theorem Memory.Renamed.nativeTableAddition {indices : LocalIndexMap} + {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} + {sourceFrame targetFrame : Frame} (related : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) + (formed : source.WellFormed) (pc : Nat) (offset : CIL.Value) (table : CIL.MethodTable) + (rest : List Value) + (observable : SuccessOrThrow (do + let offset ← match offset with + | .i32 bits => .ok (bits.signExtend 64) + | .i64 bits => .ok bits + | _ => .error .invalidState + let selected ← checkedAt .null nativeMethodWidth (addNativeTable table source offset) + return FrameAction.next (pc + 1) (.methodTable selected :: rest) sourceFrame source)) : + ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (do + let offset ← match offset with + | .i32 bits => .ok (bits.signExtend 64) + | .i64 bits => .ok bits + | _ => .error .invalidState + let selected ← checkedAt .null nativeMethodWidth (addNativeTable table source offset) + return FrameAction.next (pc + 1) (.methodTable selected :: rest) sourceFrame source) + (do + let offset ← match offset with + | .i32 bits => .ok (bits.signExtend 64) + | .i64 bits => .ok bits + | _ => .error .invalidState + let selected ← checkedAt .null nativeMethodWidth (addNativeTable table target offset) + return FrameAction.next (pc + 1) + (.methodTable selected :: rest.map (indices.value mapping)) targetFrame target) := by + cases offset + all_goals simp only [Bind.bind, Except.bind] at observable ⊢ + all_goals try exact .error _ + all_goals + apply checkedNative_bind _ _ _ _ + (fun value proof => related.addNativeTable_success formed _ value _ proof) observable + intro value found observable + exact .ok (.next related frames) +#print axioms Memory.Renamed.nativeTableAddition + /-- Nonallocating families separate reference observations, numeric and boundary steps. -/ inductive FixedStepFamily where | reference | numeric | boundary | other | excluded /-- Allocating and relocated-local operations have separate concrete step proofs. -/ -def fixedStepFamily : CIL.Op → FixedStepFamily - | .local .. | .localAddr _ | .setLocal .. | .vectorLocal _ | .vectorLocalAddr _ - | .setVectorLocal _ | .initializeSpanLocal .. | .stackAlloc _ | .newArray _ | .newVector256 .. - | .newAggregate .. | .newObject .. | .newReferenceArray _ | .memory _ => .excluded - | .sub => .reference - | .ltu | .gtu | .eq => .reference - | .bltu _ | .bgeu _ | .bgtu _ | .bleu _ => .reference - | .brzero _ | .brnonzero _ | .beq _ | .bne _ => .reference - | .intrinsic .. | .nativeBinary _ | .checkedConvert .. | .checkedArithmetic .. - | .divide .. => .numeric - | .managed _ | .arg .. | .vectorArg _ | .vectorArgAddr _ | .call .. | .callInstance .. - | .callVirtual .. | .objectTest .. | .tailCall .. - | .throw _ | .ret _ => .boundary +def fixedStepFamily : CIL.Instruction → FixedStepFamily + | .ldloc .. | .ldloca _ | .stloc .. | .localloc | .newarr _ + | .ldobj _ | .stobj _ | .cpobj _ | .initobj _ | .sizeof _ + | .ldelem_any _ | .stelem_any _ | .ldelema .. + | .newobj _ | .ldfld _ | .ldflda _ | .stfld _ => .excluded + | .cpblk _ | .initblk _ | .ldstr _ | .ldsfld _ | .ldsflda _ | .stsfld _ | .ldtoken _ => .excluded + | .add | .sub => .reference + | .clt_un | .cgt_un | .ceq => .reference + | .blt_un _ | .bge_un _ | .bgt_un _ | .ble_un _ => .reference + | .brfalse _ | .brtrue _ | .beq _ | .bne_un _ => .reference + | .conv_ovf _ | .conv_ovf_un _ | .div | .div_un | .rem + | .rem_un | .add_ovf | .add_ovf_un | .sub_ovf | .sub_ovf_un | .mul_ovf | .mul_ovf_un => .numeric + | .ldftn .. | .calli .. | .ldarg _ | .starg _ | .ldarga _ | .call .. | .callvirt .. + | .castclass _ | .isinst _ | .throw _ | .ret => .boundary | _ => .other -/-- The dispatcher rejects a narrow argument whose source value contains unspecified bits. -/ -def argumentUnspecified (operation : CIL.Op) (arguments : List Value) : Bool := - match operation with - | .arg position (some _) => (arguments[position]?.map Value.hasUnspecified).getD false - | _ => false - -/-- Allocation renaming cannot change the narrow argument observation. -/ -theorem argumentUnspecified_map (mapping : AllocationRenaming) - (operation : CIL.Op) (arguments : List Value) : - argumentUnspecified operation (arguments.map mapping.value) = - argumentUnspecified operation arguments := by - cases operation <;> try rfl - rename_i position narrow - cases narrow <;> simp only [argumentUnspecified, List.getElem?_map, Option.map_map, - Function.comp_def, mapping.hasUnspecified] -#print axioms argumentUnspecified_map - /-- The finite reference-operation family exposes only the cases that can observe references. -/ -inductive ReferenceStepOperation : CIL.Op → Prop where +inductive ReferenceStepOperation : CIL.Instruction → Prop where + | add : ReferenceStepOperation .add | sub : ReferenceStepOperation .sub - | ltu : ReferenceStepOperation .ltu - | gtu : ReferenceStepOperation .gtu - | eq : ReferenceStepOperation .eq - | bltu (destination : Nat) : ReferenceStepOperation (.bltu destination) - | bgeu (destination : Nat) : ReferenceStepOperation (.bgeu destination) - | bgtu (destination : Nat) : ReferenceStepOperation (.bgtu destination) - | bleu (destination : Nat) : ReferenceStepOperation (.bleu destination) - | brzero (destination : Nat) : ReferenceStepOperation (.brzero destination) - | brnonzero (destination : Nat) : ReferenceStepOperation (.brnonzero destination) + | clt_un : ReferenceStepOperation .clt_un + | cgt_un : ReferenceStepOperation .cgt_un + | ceq : ReferenceStepOperation .ceq + | blt_un (destination : Nat) : ReferenceStepOperation (.blt_un destination) + | bge_un (destination : Nat) : ReferenceStepOperation (.bge_un destination) + | bgt_un (destination : Nat) : ReferenceStepOperation (.bgt_un destination) + | ble_un (destination : Nat) : ReferenceStepOperation (.ble_un destination) + | brfalse (destination : Nat) : ReferenceStepOperation (.brfalse destination) + | brtrue (destination : Nat) : ReferenceStepOperation (.brtrue destination) | beq (destination : Nat) : ReferenceStepOperation (.beq destination) - | bne (destination : Nat) : ReferenceStepOperation (.bne destination) + | bne_un (destination : Nat) : ReferenceStepOperation (.bne_un destination) /-- Reference-family classification supplies its finite case analysis independently of step goals. -/ -theorem referenceStepOperation (operation : CIL.Op) +theorem referenceStepOperation (operation : CIL.Instruction) (family : fixedStepFamily operation = .reference) : ReferenceStepOperation operation := by cases operation <;> try cases family all_goals constructor @@ -155,6 +189,8 @@ macro "fixed_fallback" : tactic => `(tactic| ( | (have referenceCases := referenceStepOperation operation family cases referenceCases) | (cases operation <;> try cases family) + -- A hole has its own faulting arm, so the fallback arm excludes it. + all_goals try exact False.elim (unadmitted _ rfl) all_goals try exact fallback all_goals cases stack with | nil => @@ -162,29 +198,68 @@ macro "fixed_fallback" : tactic => `(tactic| ( exact fallback | cons first rest => cases first - all_goals try dsimp only [List.map_cons, AllocationRenaming.value] + all_goals try dsimp only [List.map_cons, AllocationRenaming.value, LocalIndexMap.value] all_goals try exact fallback all_goals cases rest with | nil => - try dsimp only [List.map_nil, List.map_cons, AllocationRenaming.value] + try dsimp only [List.map_nil, List.map_cons, AllocationRenaming.value, LocalIndexMap.value] first | contradiction | exact fallback | cons second rest => cases second - all_goals try dsimp only [List.map_nil, List.map_cons, AllocationRenaming.value] + all_goals try dsimp only [List.map_nil, List.map_cons, AllocationRenaming.value, + LocalIndexMap.value] all_goals first | contradiction | exact fallback | (exfalso have excluded : ∀ (offset : CIL.Value) (reference : ManagedReference) (tail : List Value), - CIL.Op.sub = CIL.Op.sub → + CIL.Instruction.add = CIL.Instruction.add → originalStack = Value.scalar offset :: Value.reference reference :: tail → False := by assumption exact excluded _ _ _ rfl rfl) + | (exfalso + have excluded : ∀ (reference : ManagedReference) (offset : CIL.Value) (tail : List Value), + CIL.Instruction.add = CIL.Instruction.add → + originalStack = Value.reference reference :: Value.scalar offset :: tail → False := + by assumption + exact excluded _ _ _ rfl rfl) | (exfalso have excluded : ∀ (offset : CIL.Value) (reference : ManagedReference) (tail : List Value), - CIL.Op.sub = CIL.Op.sub → + CIL.Instruction.add = CIL.Instruction.add → + originalStack = + Value.scalar offset :: Value.readOnlyReference reference :: tail → False := + by assumption + exact excluded _ _ _ rfl rfl) + | (exfalso + have excluded : ∀ (reference : ManagedReference) (offset : CIL.Value) (tail : List Value), + CIL.Instruction.add = CIL.Instruction.add → + originalStack = + Value.readOnlyReference reference :: Value.scalar offset :: tail → False := + by assumption + exact excluded _ _ _ rfl rfl) + | (exfalso + have excluded : ∀ (offset : CIL.Value) (table : CIL.MethodTable) (tail : List Value), + CIL.Instruction.add = CIL.Instruction.add → + originalStack = Value.scalar offset :: Value.methodTable table :: tail → False := + by assumption + exact excluded _ _ _ rfl rfl) + | (exfalso + have excluded : ∀ (table : CIL.MethodTable) (offset : CIL.Value) (tail : List Value), + CIL.Instruction.add = CIL.Instruction.add → + originalStack = Value.methodTable table :: Value.scalar offset :: tail → False := + by assumption + exact excluded _ _ _ rfl rfl) + | (exfalso + have excluded : ∀ (offset : CIL.Value) (reference : ManagedReference) (tail : List Value), + CIL.Instruction.sub = CIL.Instruction.sub → + originalStack = Value.scalar offset :: Value.reference reference :: tail → False := + by assumption + exact excluded _ _ _ rfl rfl) + | (exfalso + have excluded : ∀ (offset : CIL.Value) (reference : ManagedReference) (tail : List Value), + CIL.Instruction.sub = CIL.Instruction.sub → originalStack = Value.scalar offset :: Value.readOnlyReference reference :: tail → False := by assumption exact excluded _ _ _ rfl rfl) @@ -193,70 +268,124 @@ macro "fixed_fallback" : tactic => `(tactic| ( set_option hygiene false in /-- Compose fixed-map primitives after selecting one checked dispatcher family. -/ macro "allocation_fixed_step" following:tactic : tactic => `(tactic| ( - unfold CIL.Safety.step at observable ⊢ - change SuccessOrThrow (if (unspecifiedOperands (observerCount operation) stack || - argumentUnspecified operation arguments) then _ else _) at observable + unfold CIL.Safety.step referenceCompareCall referenceSubtractCall at observable ⊢ + change SuccessOrThrow (if unspecifiedOperands (observerCount operation) stack then _ else _) + at observable change ResultRenamed mapping _ - (if (unspecifiedOperands (observerCount operation) stack || - argumentUnspecified operation arguments) then _ else _) - (if (unspecifiedOperands (observerCount operation) (stack.map mapping.value) || - argumentUnspecified operation (arguments.map mapping.value)) then _ else _) - rw [mapping.unspecifiedOperands, argumentUnspecified_map] - by_cases guard : (unspecifiedOperands (observerCount operation) stack || - argumentUnspecified operation arguments) = true + (if unspecifiedOperands (observerCount operation) stack then _ else _) + (if unspecifiedOperands (observerCount operation) (stack.map (indices.value mapping)) + then _ else _) + rw [indices.unspecifiedOperands mapping] + by_cases guard : unspecifiedOperands (observerCount operation) stack = true · simp only [guard, ite_true] at observable cases observable · simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ split at observable + -- The catch-all's exclusions are named so the arms' own names stay the newest inaccessible + -- ones; `fixed_fallback` reads the hole and table-offset exclusions and the boundary arm the + -- call exclusion, and the others are cleared. + -- `split` states the exclusions of the arms that ignore the stack first. + all_goals try ( + rename_i typeToken loadsField addressesField storesField addressHome localAllocation loadsStatic + addressesStatic tokensStatic storesStatic loadsObject storesObject copiesObject + resetsObject objectSize loadsTyped storesTyped addressesTyped calls constructs + unadmitted offsetBeforeTable + tableBeforeOffset addOvf addOvfUn subOvf subOvfUn mulOvf mulOvfUn div divUn rem remUn + convOvf convOvfUn typesEqual typesBranchEqual typesBranchUnequal + objectsEqual objectsBranchEqual objectsBranchUnequal objectAbove objectBranchFalse + objectBranchTrue castClass isInst storeHome allocatedArray + guard_hyp loadsField : ∀ (field : CIL.Extraction.FieldRef), operation = .ldfld field → False + guard_hyp addressesField : ∀ (field : CIL.Extraction.FieldRef), operation = .ldflda field → + False + guard_hyp storesField : ∀ (field : CIL.Extraction.FieldRef), operation = .stfld field → False + guard_hyp addressHome : ∀ (index : Nat), operation = .ldarga index → False + guard_hyp localAllocation : operation = .localloc → False + guard_hyp loadsStatic : ∀ field, operation = .ldsfld field → False + guard_hyp addressesStatic : ∀ field, operation = .ldsflda field → False + guard_hyp tokensStatic : ∀ field, operation = .ldtoken (.field field) → False + guard_hyp storesStatic : ∀ field, operation = .stsfld field → False + guard_hyp loadsTyped : ∀ element, operation = .ldelem_any element → False + guard_hyp storesTyped : ∀ element, operation = .stelem_any element → False + guard_hyp addressesTyped : ∀ element readOnly, operation = .ldelema element readOnly → False + guard_hyp calls : ∀ method prefixes, operation = .call method prefixes → False + guard_hyp constructs : ∀ constructor, operation = .newobj constructor → False + guard_hyp unadmitted : ∀ (opcode : Nat), operation = .unadmitted opcode → False + guard_hyp storeHome : ∀ (index : Nat) (value : Value) (rest : List Value), + operation = .starg index → stack = value :: rest → False + guard_hyp allocatedArray : ∀ (element : CIL.Extraction.TypeExpr) (length : CIL.Value) + (rest : List Value), operation = .newarr element → stack = Value.scalar length :: rest → + False + guard_hyp tableBeforeOffset : ∀ (table : CIL.MethodTable) (offset : CIL.Value) + (rest : List Value), operation = .add → + stack = Value.methodTable table :: Value.scalar offset :: rest → False + guard_hyp div : ∀ (y x : CIL.Value) (rest : List Value), operation = .div → + stack = Value.scalar y :: Value.scalar x :: rest → False + guard_hyp objectsEqual : ∀ (right left : ManagedReference) (rest : List Value), + operation = .ceq → stack = Value.object right :: Value.object left :: rest → False + guard_hyp isInst : ∀ (type : CIL.Extraction.TypeExpr) (value : ManagedReference) + (rest : List Value), operation = .isinst type → stack = Value.object value :: rest → False + clear addOvf addOvfUn subOvf subOvfUn mulOvf mulOvfUn div divUn rem remUn convOvf + convOvfUn typeToken typesEqual typesBranchEqual typesBranchUnequal objectsEqual objectsBranchEqual objectsBranchUnequal objectAbove + objectBranchFalse objectBranchTrue castClass isInst storeHome allocatedArray addressHome + localAllocation loadsField addressesField storesField loadsStatic addressesStatic + tokensStatic storesStatic loadsObject storesObject copiesObject resetsObject objectSize + loadsTyped storesTyped addressesTyped constructs + split at observable) all_goals try solve | cases family - all_goals try simp only [List.map_cons, AllocationRenaming.value, metadata.profile, - metadata.returnsValue] + all_goals try simp only [List.map_cons, AllocationRenaming.value, LocalIndexMap.value, + metadata.profile, metadata.returnsValue, metadata.returnNarrow] all_goals try exact (related.stepFallback frames formed operation pc sourceBody.profile stack bounds observable) - case h_85 => + -- Only the dispatcher's final arm excludes `calli`, so the guard skips every other arm + -- and fails if no arm is left to select. + any_goals + first + | (have excludesCalli : ∀ signature tail, operation = .calli signature tail → False := by assumption + clear excludesCalli) + | fail "not the dispatcher's fallback arm" first | (have boundary : fixedStepFamily operation = .boundary := family - have noManaged : ∀ op, operation = .managed op → False := by assumption - have noArg : ∀ position, operation = .arg position none → False := by assumption - have noNarrowArg : ∀ position kind, operation = .arg position (some kind) → False := - by assumption - have noVectorArg : ∀ position, operation = .vectorArg position → False := by assumption - have noVectorAddress : ∀ position, operation = .vectorArgAddr position → False := - by assumption - have noCall : ∀ callee argc, operation = .call callee argc → False := by assumption - have noTailCall : ∀ callee argc, operation = .tailCall callee argc → False := by assumption + have noCalli : ∀ sig tail, operation = .calli sig tail → False := by assumption + have noLdftn : ∀ id sig, operation = .ldftn id sig → False := by assumption + have noArgument : ∀ position, operation = .ldarg position → False := by assumption + have noCall : ∀ method prefixes, operation = .call method prefixes → False := by + assumption have noThrow : ∀ exception, operation = .throw exception → False := by assumption - have noReturn : operation = .ret none → False := by assumption - have noArgument : ∀ position narrow, operation = .arg position narrow → False := by - intro position narrow - cases narrow - · exact noArg position - · exact noNarrowArg position _ + have noReturn : operation = .ret → False := by assumption cases operation <;> try cases boundary - all_goals try exact False.elim (noManaged _ rfl) - all_goals try exact False.elim (noArgument _ _ rfl) - all_goals try exact False.elim (noVectorArg _ rfl) - all_goals try exact False.elim (noVectorAddress _ rfl) + all_goals try exact False.elim (noCalli _ _ rfl) + all_goals try exact False.elim (noLdftn _ _ rfl) + all_goals try exact False.elim (noArgument _ rfl) all_goals try exact False.elim (noCall _ _ rfl) - all_goals try exact False.elim (noTailCall _ _ rfl) all_goals try exact False.elim (noThrow _ rfl) + all_goals try exact False.elim (noReturn rfl) all_goals simp only [pureArity, CIL.Safety.instruction] at observable all_goals repeat first | cases observable | split at observable) | (have numeric : fixedStepFamily operation = .numeric := family - have noIntrinsic : ∀ intrinsic argc, operation = .intrinsic intrinsic argc → False := - by assumption cases operation <;> try cases numeric - all_goals try exact False.elim (noIntrinsic _ _ rfl) all_goals simp only [pureArity, CIL.Safety.instruction] at observable all_goals repeat first | cases observable | split at observable) | fixed_fallback all_goals try solve | repeat first | contradiction | cases observable | split at observable + all_goals try solve | + apply (related.referenceAddInstructionResult 1 true _ bounds + (SuccessOrThrow.bind_left observable)).bind + intro first second same + obtain ⟨memory, values⟩ := first + obtain ⟨otherMemory, otherValues⟩ := second + obtain ⟨memories, rfl⟩ := same + exact .ok (.next memories frames) + all_goals try solve | exact related.nativeTableAddition frames formed _ _ _ _ observable all_goals clear observable all_goals try simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds all_goals repeat' rcases bounds with ⟨_, bounds⟩ all_goals repeat first | exact .ok (.next related frames) | exact .error _ + | (apply (mapping.runtimeTypeEqual _ _).bind + intro first second same + subst second + exact .ok (.next related frames)) | (apply (related.referenceSubtract _ (by assumption) _).bind intro first second same subst second diff --git a/CIL/Safety/FixedStepRenaming.lean b/CIL/Safety/FixedStepRenaming.lean index dec91392b..b8f40951e 100644 --- a/CIL/Safety/FixedStepRenaming.lean +++ b/CIL/Safety/FixedStepRenaming.lean @@ -11,7 +11,7 @@ theorem Memory.Renamed.stepReference {indices : LocalIndexMap} {mapping : Alloca (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (formed : source.WellFormed) (sourceBody targetBody : CIL.Method) - (metadata : StepMetadata sourceBody targetBody) (operation : CIL.Op) + (metadata : StepMetadata sourceBody targetBody) (operation : CIL.Instruction) (family : fixedStepFamily operation = .reference) (pc : Nat) (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (observable : SuccessOrThrow @@ -19,7 +19,7 @@ theorem Memory.Renamed.stepReference {indices : LocalIndexMap} {mapping : Alloca ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) (step sourceBody operation pc arguments sourceFrame stack source) (step targetBody operation pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by + targetFrame (stack.map (indices.value mapping)) target) := by allocation_fixed_step skip #print axioms Memory.Renamed.stepReference diff --git a/CIL/Safety/Forwarders.lean b/CIL/Safety/Forwarders.lean index ed2458e63..3c142a996 100644 --- a/CIL/Safety/Forwarders.lean +++ b/CIL/Safety/Forwarders.lean @@ -15,8 +15,8 @@ method's result: overload forwarders and instance-to-static wrappers compile to `ldarg*; call callee; ret`. `MethodContract.Holds.forward` gives such a method the callee's contract with the forwarder's own argument encoding, from the callee's `Holds` and premises that the arguments and the returned value pass through unchanged; the loads are data -(`loads`: an argument index and the narrowing the extractor attaches to small integer -parameters), so no forwarder body is stepped by hand. Its frame allocates nothing, so the +(`loads`: the argument indices, each read as the forwarder's declared signature says), so no +forwarder body is stepped by hand. Its frame allocates nothing, so the callee's precondition, footprint and postcondition carry over verbatim. `MethodContract.Holds.forward_out` covers `ldarg*; ldloca 0; call callee; ret`, which also @@ -28,50 +28,64 @@ forwarder's contract is related to the callee's by premises over those two memor namespace CIL.Safety -/-- What `ret narrow` returns unchanged: any values for a plain `ret`, and a single i32 - already in the narrow range for a narrowed one. -/ +/-- Values preserved by the method's return storage: any values without narrowing, + and a single i32 already in range for narrow storage. -/ def ReturnsUnchanged : Option CIL.NarrowKind → List Value → Prop | none, _ => True | some kind, values => ∃ bits, values = [.scalar (.i32 bits)] ∧ kind.extend bits = bits -/-- What `.arg index narrow` pushes unchanged: any argument for a plain load, and an i32 - already in the narrow range for a narrowed one. -/ -def LoadsUnchanged : Option CIL.NarrowKind → Value → Prop - | none, _ => True - | some kind, value => ∃ bits, value = .scalar (.i32 bits) ∧ kind.extend bits = bits - -/-- Plain loads of the arguments at `indices`, as emitted for parameters wider than 16 bits. -/ -def plainLoads (indices : List Nat) : List (Nat × Option CIL.NarrowKind) := - indices.map fun index => (index, none) - -/-- Plain loads push every argument unchanged. -/ -theorem plainLoads_unchanged (indices : List Nat) (values : List Value) : - ∀ pair ∈ (plainLoads indices).zip values, LoadsUnchanged pair.1.2 pair.2 := by - intro ⟨⟨index, narrow⟩, value⟩ member - have loaded := (List.of_mem_zip member).1 - simp only [plainLoads, List.mem_map, Prod.mk.injEq] at loaded - obtain ⟨_, _, _, rfl⟩ := loaded - trivial +/-- `body`'s `.ldarg index` pushes `value` unchanged: the argument is read without a home, + and either without narrowing or as an i32 already in the declared narrow range. -/ +def LoadsUnchanged (body : CIL.Method) (index : Nat) (value : Value) : Prop := + match body.argumentAccess index with + | .value none => True + | .value (some kind) => ∃ bits, value = .scalar (.i32 bits) ∧ kind.extend bits = bits + | .home _ => False + +/-- An argument read without a home or narrowing pushes any value unchanged. -/ +theorem LoadsUnchanged.plain {body : CIL.Method} {index : Nat} {value : Value} + (access : body.argumentAccess index = .value none) : LoadsUnchanged body index value := by + simp only [LoadsUnchanged, access] + +#print axioms LoadsUnchanged.plain + +/-- An argument read narrowed pushes an i32 already in the narrow range unchanged. -/ +theorem LoadsUnchanged.narrow {body : CIL.Method} {index : Nat} {kind : CIL.NarrowKind} + {bits : CIL.W32} (access : body.argumentAccess index = .value (some kind)) + (same : kind.extend bits = bits) : LoadsUnchanged body index (.scalar (.i32 bits)) := by + simp only [LoadsUnchanged, access] + exact ⟨bits, rfl, same⟩ + +#print axioms LoadsUnchanged.narrow + +/-- Arguments that `body` reads without a home or narrowing, as for parameters wider than 16 + bits, push every value unchanged. -/ +theorem plainLoads_unchanged {body : CIL.Method} {indices : List Nat} + (plain : ∀ index ∈ indices, body.argumentAccess index = .value none) (values : List Value) : + ∀ pair ∈ indices.zip values, LoadsUnchanged body pair.1 pair.2 := + fun pair member => .plain (plain pair.1 (List.of_mem_zip member).1) #print axioms plainLoads_unchanged -/-- The body that performs `loads` in order, calls `callee` with the loaded values and - returns its result through `ret narrow`. -/ -def forwarderCode (loads : List (Nat × Option CIL.NarrowKind)) (callee : Nat) - (narrow : Option CIL.NarrowKind) : List CIL.Op := - loads.map (fun load => .arg load.1 load.2) ++ [.call callee loads.length, .ret narrow] - -/-- `body` forwards the arguments it loads by `loads` to `callee`, whose body is - `calleeBody`, from a frame that allocates nothing, and declares the callee's return - arity. -/ -structure Forwards (body calleeBody : CIL.Method) (loads : List (Nat × Option CIL.NarrowKind)) - (callee : Nat) (narrow : Option CIL.NarrowKind) : Prop where - code : body.code = forwarderCode loads callee narrow +/-- The body that loads the arguments at `loads` in order, calls `method` with them and + returns its result through `ret`. -/ +def forwarderCode (loads : List Nat) (method : CIL.Extraction.MethodRef) : List CIL.Instruction := + loads.map .ldarg ++ [.call method {}, .ret] + +/-- `body` forwards the arguments it loads by `loads` to `method`, linked to `callee`, whose + body is `calleeBody`, from a frame that allocates nothing, and declares the callee's return + arity and return storage `narrow`. -/ +structure Forwards (body calleeBody : CIL.Method) (loads : List Nat) + (method : CIL.Extraction.MethodRef) (callee : Nat) (narrow : Option CIL.NarrowKind) : + Prop where + code : body.code = forwarderCode loads method + arity : method.argumentCount = loads.length + linked : body.linkAt loads.length = some (.direct callee) locals : body.locals = [] localKinds : body.localKinds = [] - vectorArgs : body.vectorArgs = [] parameterHomes : body.parameterHomes = [] returnsValue : body.returnsValue = calleeBody.returnsValue + returnNarrow : body.returnNarrow = narrow /-- `contract` with the arguments encoded by `args` instead: what a forwarder to a method proving `contract` satisfies. -/ @@ -81,40 +95,43 @@ def MethodContract.withArgs {Input Output : Type} (contract : MethodContract Inp /-- A frame that allocates nothing: entry keeps memory and the frame owns no allocation. -/ theorem Forwards.enterFrame {body calleeBody : CIL.Method} - {loads : List (Nat × Option CIL.NarrowKind)} {callee : Nat} - {narrow : Option CIL.NarrowKind} (forwards : Forwards body calleeBody loads callee narrow) + {loads : List Nat} {method : CIL.Extraction.MethodRef} {callee : Nat} + {narrow : Option CIL.NarrowKind} + (forwards : Forwards body calleeBody loads method callee narrow) (args : List Value) (memory : Memory) : CIL.Safety.enterFrame body args memory = - .ok (⟨memory.nextIdentity, [], [], [], 0⟩, memory) := by + .ok (⟨memory.nextIdentity, [], [], 0⟩, memory) := by have noLocals : makeSelectedLocals memory.nextIdentity body.opaqueVectorLocals [] [] memory = .ok ([], [], memory) := by unfold makeSelectedLocals split · rfl · simp only [makeSelectedLocalsFrom] - simp only [CIL.Safety.enterFrame, forwards.localKinds, forwards.locals, forwards.vectorArgs, - forwards.parameterHomes, noLocals, makeArgumentHomes, makeParameterHomes, Bind.bind, + simp only [CIL.Safety.enterFrame, forwards.localKinds, forwards.locals, + forwards.parameterHomes, noLocals, makeParameterHomes, Bind.bind, Except.bind, Pure.pure, Except.pure, List.map_nil, List.append_nil] #print axioms Forwards.enterFrame -/-- A load of a checked argument that its narrowing keeps pushes that argument. -/ +/-- A load of a checked argument that the body's reading keeps pushes that argument. -/ theorem step_load_unchanged {body : CIL.Method} {index pc : Nat} - {narrow : Option CIL.NarrowKind} {args stack : List Value} {frame : Frame} + {args stack : List Value} {frame : Frame} {memory : Memory} {value : Value} (present : args[index]? = some value) - (checked : checkedValue memory value = .ok value) (unchanged : LoadsUnchanged narrow value) : - step body (.arg index narrow) pc args frame stack memory = + (checked : checkedValue memory value = .ok value) + (unchanged : LoadsUnchanged body index value) : + step body (.ldarg index) pc args frame stack memory = .ok (.next (pc + 1) (value :: stack) frame memory) := by - cases narrow with - | none => - simp only [step_argument_eq, present, checked, Bind.bind, Except.bind, Pure.pure, - Except.pure] - | some kind => - obtain ⟨bits, rfl, same⟩ := unchanged - simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, present, - Option.map_some, Option.getD_some, Value.hasUnspecified, CIL.Value.hasUnspecified, - Bool.false_or, Bool.false_eq_true, ↓reduceIte, same, checked, Bind.bind, Except.bind, + unfold LoadsUnchanged at unchanged + split at unchanged + · rename_i access + simp only [step_ldarg_eq, loadArgument, access, present, checked, Bind.bind, Except.bind, Pure.pure, Except.pure] + · rename_i kind access + obtain ⟨bits, rfl, same⟩ := unchanged + simp only [step_ldarg_eq, loadArgument, access, present, Value.hasUnspecified, + CIL.Value.hasUnspecified, Bool.false_eq_true, ↓reduceIte, same, checked, Bind.bind, + Except.bind, Pure.pure, Except.pure] + · exact unchanged.elim #print axioms step_load_unchanged @@ -122,12 +139,13 @@ theorem step_load_unchanged {body : CIL.Method} {index pc : Nat} argument; any continuation from the following pc composes. -/ theorem run_load_arguments {program : CIL.Program} {method : Nat} {body : CIL.Method} {args : List Value} {frame : Frame} {memory : Memory} {post : Memory → List Value → Prop} - (found : program[method]? = some body) (valid : ValuesValid memory args) : - ∀ (loads : List (Nat × Option CIL.NarrowKind)) (pc : Nat) (stack loaded : List Value), - (∀ position load, loads[position]? = some load → - body.code[pc + position]? = some (.arg load.1 load.2)) → - loads.map (fun load => args[load.1]?) = loaded.map some → - (∀ pair ∈ loads.zip loaded, LoadsUnchanged pair.1.2 pair.2) → + (found : program[method]? = some body) (valid : ValuesValid memory args) + (free : ∀ value ∈ args, value.isHome = false) : + ∀ (loads : List Nat) (pc : Nat) (stack loaded : List Value), + (∀ position index, loads[position]? = some index → + body.code[pc + position]? = some (.ldarg index)) → + loads.map (fun index => args[index]?) = loaded.map some → + (∀ pair ∈ loads.zip loaded, LoadsUnchanged body pair.1 pair.2) → (∃ fuel result returned, run program fuel method (pc + loads.length) args frame (loaded.reverse ++ stack) memory = .ok (result, returned) ∧ post result returned) → @@ -149,7 +167,7 @@ theorem run_load_arguments {program : CIL.Program} {method : Nat} {body : CIL.Me simp only [List.map_cons, List.cons.injEq] at selected obtain ⟨present, selected⟩ := selected have checked := checkedValue_of_valid memory value - (valid value (List.mem_of_getElem? present)) + (valid value (List.mem_of_getElem? present)) (free value (List.mem_of_getElem? present)) have stepped := step_load_unchanged (body := body) (pc := pc) (stack := stack) (frame := frame) present checked (narrowed (load, value) (by simp)) refine run_next_exists post found (by simpa using fetched 0 load rfl) stepped ?_ @@ -162,61 +180,75 @@ theorem run_load_arguments {program : CIL.Program} {method : Nat} {body : CIL.Me #print axioms run_load_arguments -/-- A call whose arguments are exactly the stack, valid and fully specified, passes them to - the callee in push order and leaves an empty stack. -/ -theorem step_call_loaded {body : CIL.Method} {callee pc : Nat} {args : List Value} +/-- A call without `tail.` whose arguments are exactly the stack, valid and fully specified, + passes them to the callee in push order and leaves an empty stack. -/ +theorem step_call_loaded {body : CIL.Method} {method : CIL.Extraction.MethodRef} + {prefixes : CIL.CallPrefixes} {callee pc : Nat} {args : List Value} {frame : Frame} {memory : Memory} {arguments : List Value} - (valid : ValuesValid memory arguments) (known : arguments.any Value.hasUnspecified = false) : - step body (.call callee arguments.length) pc args frame arguments.reverse memory = + (plain : prefixes.tail = false) (arity : method.argumentCount = arguments.length) + (linked : body.linkAt pc = some (.direct callee)) + (valid : ValuesValid memory arguments) (free : ∀ value ∈ arguments, value.isHome = false) + (known : arguments.any Value.hasUnspecified = false) : + step body (.call method prefixes) pc args frame arguments.reverse memory = .ok (.call callee arguments [] memory) := by have taken : arguments.reverse.take arguments.length = arguments.reverse := List.take_of_length_le (by simp) have exposed : unspecifiedOperands arguments.length arguments.reverse = false := by rw [unspecifiedOperands, taken, List.any_reverse, known] - simp only [step, observerCount, exposed, Bool.false_or, Bool.false_eq_true, ↓reduceIte, - List.length_reverse, Nat.lt_irrefl, taken, List.reverse_reverse, - checkedValues_of_valid valid, Bind.bind, Except.bind, Pure.pure, Except.pure] + have unobserved : unspecifiedOperands 0 arguments.reverse = false := rfl + simp only [step, observerCount, observedCall, unobserved, arity, exposed, Bool.false_eq_true, + ↓reduceIte, linked, plain, List.length_reverse, Nat.lt_irrefl, taken, List.reverse_reverse, + callArguments_of_checked (frame := frame) (checkedValues_of_valid valid free), Bind.bind, + Except.bind, Pure.pure, Except.pure] rw [List.drop_of_length_le (by simp)] #print axioms step_call_loaded -/-- `ret narrow` returns a callee's checked, fully specified result of the declared arity +/-- `ret` returns a callee's checked, fully specified result of the declared arity unchanged. -/ theorem step_return_unchanged {body : CIL.Method} {narrow : Option CIL.NarrowKind} {pc : Nat} {args : List Value} {frame : Frame} {memory : Memory} {values : List Value} + (returnNarrow : body.returnNarrow = narrow) (arity : values.length = if body.returnsValue then 1 else 0) (known : values.any Value.hasUnspecified = false) (checked : values.mapM (checkedValue memory) = .ok values) (unchanged : ReturnsUnchanged narrow values) : - step body (.ret narrow) pc args frame values memory = .ok (.returned values memory) := by + step body (.ret) pc args frame values memory = .ok (.returned values memory) := by cases narrow with | none => have hidden : unspecifiedOperands 1 values = false := by simp only [unspecifiedOperands, List.any_eq_false] at known ⊢ exact fun value member => known value (List.mem_of_mem_take member) - simp only [step_return_eq, hidden, Bool.false_eq_true, ↓reduceIte, arity, checked, + simp only [step_ret_eq, returnNarrow, returnFrom_none_eq, hidden, Bool.false_eq_true, + ↓reduceIte, + arity, checked, Bind.bind, Except.bind, Pure.pure, Except.pure] | some kind => obtain ⟨bits, rfl, same⟩ := unchanged have returns : body.returnsValue = true := by cases returning : body.returnsValue <;> simp_all - rw [step_return_narrow_eq, returns, same] - rfl + simp only [step_ret_eq, returnNarrow, unspecifiedOperands, List.take_succ_cons, List.take_zero, + List.any_cons, List.any_nil, Value.hasUnspecified, CIL.Value.hasUnspecified, + Bool.false_or, Bool.false_eq_true, ↓reduceIte, returnFrom_narrow_eq, returns, same] #print axioms step_return_unchanged -/-- From a stack holding a callee's arguments in push order, `call callee; ret narrow` +/-- From a stack holding a callee's arguments in push order, `call callee; ret` returns what the callee's contract promises from the current memory, in the memory left once the frame expires. -/ theorem run_forward_call {Input Output : Type} {contract : MethodContract Input Output} {program : CIL.Program} {method callee pc : Nat} {body calleeBody : CIL.Method} + {called : CIL.Extraction.MethodRef} {prefixes : CIL.CallPrefixes} {narrow : Option CIL.NarrowKind} {args : List Value} {frame : Frame} {memory : Memory} {input : Input} {post : Memory → List Value → Prop} (holds : contract.Holds program callee) (found : program[method]? = some body) (calleeFound : program[callee]? = some calleeBody) - (callFetched : body.code[pc]? = some (.call callee (contract.args input).length)) - (returnFetched : body.code[pc + 1]? = some (.ret narrow)) + (callFetched : body.code[pc]? = some (.call called prefixes)) + (plain : prefixes.tail = false) (arity : called.argumentCount = (contract.args input).length) + (callLinked : body.linkAt pc = some (.direct callee)) + (returnFetched : body.code[pc + 1]? = some (.ret)) (returnsValue : body.returnsValue = calleeBody.returnsValue) + (returnNarrow : body.returnNarrow = narrow) (unchanged : ∀ final out, contract.post input memory final out → ReturnsUnchanged narrow (contract.results out)) (permitted : contract.pre input memory) @@ -233,8 +265,12 @@ theorem run_forward_call {Input Output : Type} {contract : MethodContract Input have calleeValid : ValuesValid memory (contract.args input) := by obtain ⟨same, valid⟩ := checkedValues_valid _ _ _ calleeChecked exact same ▸ valid + have calleeFree : ∀ value ∈ contract.args input, value.isHome = false := by + have free := checkedValues_not_spanHome calleeChecked + obtain ⟨same, _⟩ := checkedValues_valid _ _ _ calleeChecked + rwa [same] at free refine run_call_of_contract holds input found callFetched - (step_call_loaded calleeValid known) rfl permitted ?_ + (step_call_loaded plain arity callLinked calleeValid calleeFree known) rfl permitted ?_ intro final out call obtain ⟨_, called⟩ := call.invoked have returns : contract.Returns input memory final (contract.results out) := @@ -244,35 +280,37 @@ theorem run_forward_call {Input Output : Type} {contract : MethodContract Input rw [returnsValue] exact run_return_length _ _ _ _ _ _ _ _ _ _ _ calleeFound ran have valid := invoke_result_valid _ _ _ _ _ _ _ called + have free := invoke_result_not_spanHome _ _ _ _ _ _ _ called refine run_return_exists found returnFetched ?_ - (checkedValues_of_valid (survives final _ valid returns)) + (checkedValues_of_valid (survives final _ valid returns) free) (continuation final _ call.framed returns) rw [List.append_nil] - exact step_return_unchanged arity (invoke_return_known called) (checkedValues_of_valid valid) + exact step_return_unchanged returnNarrow arity (invoke_return_known called) + (checkedValues_of_valid valid free) (unchanged final out call.post) #print axioms run_forward_call /-- A forwarder inherits its callee's contract: it meets `pre`, `footprint` and `post` unchanged, with arguments encoded by `forwarded`. The callee's arguments must be the - forwarder's selected by `loads`, each kept by its load's narrowing; each forwarder - argument must reach the callee or be valid under `pre`, since the forwarder checks them - all; the call must expose no unspecified vector bits; and a narrowed return must not - change the callee's result. -/ + forwarder's selected by `loads`, each kept as the forwarder reads it; each forwarder + argument must reach the callee or be valid and not a span home under `pre`, since the + forwarder checks them all; the call must expose no unspecified vector bits; and a narrowed + return must not change the callee's result. -/ theorem MethodContract.Holds.forward {Input Output : Type} {contract : MethodContract Input Output} {program : CIL.Program} {method callee : Nat} {body calleeBody : CIL.Method} - {loads : List (Nat × Option CIL.NarrowKind)} {narrow : Option CIL.NarrowKind} + {loads : List Nat} {called : CIL.Extraction.MethodRef} {narrow : Option CIL.NarrowKind} {forwarded : Input → List Value} (holds : contract.Holds program callee) (found : program[method]? = some body) (calleeFound : program[callee]? = some calleeBody) - (forwards : Forwards body calleeBody loads callee narrow) + (forwards : Forwards body calleeBody loads called callee narrow) (selected : ∀ input, - loads.map (fun load => (forwarded input)[load.1]?) = (contract.args input).map some) + loads.map (fun index => (forwarded input)[index]?) = (contract.args input).map some) (narrowed : ∀ input initial, contract.pre input initial → - ∀ pair ∈ loads.zip (contract.args input), LoadsUnchanged pair.1.2 pair.2) + ∀ pair ∈ loads.zip (contract.args input), LoadsUnchanged body pair.1 pair.2) (covered : ∀ input initial, contract.pre input initial → ∀ value ∈ forwarded input, - value ∈ contract.args input ∨ value.Valid initial) + value ∈ contract.args input ∨ (value.Valid initial ∧ value.isHome = false)) (known : ∀ input initial, contract.pre input initial → (contract.args input).any Value.hasUnspecified = false) (unchanged : ∀ input initial final out, contract.post input initial final out → @@ -285,15 +323,21 @@ theorem MethodContract.Holds.forward {Input Output : Type} obtain ⟨same, valid⟩ := checkedValues_valid _ _ _ calleeChecked subst same exact fun value member => - (covered input initial permitted value member).elim (valid value) id + (covered input initial permitted value member).elim (valid value) (·.1) + have forwardedFree : ∀ value ∈ forwarded input, value.isHome = false := by + have calleeFree := checkedValues_not_spanHome calleeChecked + obtain ⟨same, _⟩ := checkedValues_valid _ _ _ calleeChecked + rw [same] at calleeFree + exact fun value member => + (covered input initial permitted value member).elim (calleeFree value) (·.2) have length : (contract.args input).length = loads.length := by simpa using (congrArg List.length (selected input)).symm - have fetched : ∀ pc : Nat, body.code[pc]? = (forwarderCode loads callee narrow)[pc]? := by + have fetched : ∀ pc : Nat, body.code[pc]? = (forwarderCode loads called)[pc]? := by rw [forwards.code]; exact fun _ => rfl apply WP.adequate.mpr - refine invoke_exists_of_run found (checkedValues_of_valid forwardedValid) + refine invoke_exists_of_run found (checkedValues_of_valid forwardedValid forwardedFree) (forwards.enterFrame _ _) ?_ - refine run_load_arguments found forwardedValid loads 0 [] (contract.args input) + refine run_load_arguments found forwardedValid forwardedFree loads 0 [] (contract.args input) (fun position load located => ?_) (selected input) (narrowed input initial permitted) ?_ · have bound : position < loads.length := (List.getElem?_eq_some_iff.mp located).1 rw [Nat.zero_add, fetched, forwarderCode, @@ -301,30 +345,34 @@ theorem MethodContract.Holds.forward {Input Output : Type} rfl rw [Nat.zero_add, List.append_nil] -- The frame owns nothing, so leaving it keeps the callee's final memory. - exact run_forward_call holds found calleeFound (by simp [fetched, forwarderCode, length]) - (by simp [fetched, forwarderCode]) forwards.returnsValue (unchanged input initial) permitted + exact run_forward_call (called := called) (prefixes := {}) holds found calleeFound + (by simp [fetched, forwarderCode]) rfl (by rw [forwards.arity, length]) + forwards.linked (by simp [fetched, forwarderCode]) forwards.returnsValue forwards.returnNarrow + (unchanged input initial) permitted (known input initial permitted) (fun _ _ valid _ => valid) (fun _ _ framed returns => ⟨framed, returns⟩) #print axioms MethodContract.Holds.forward -/-- The body that performs `loads` in order, loads the address of local 0, calls `callee` - with them and returns its result through `ret narrow`. -/ -def outForwarderCode (loads : List (Nat × Option CIL.NarrowKind)) (callee : Nat) - (narrow : Option CIL.NarrowKind) : List CIL.Op := - loads.map (fun load => .arg load.1 load.2) ++ - [.localAddr 0, .call callee (loads.length + 1), .ret narrow] +/-- The body that loads the arguments at `loads` in order, loads the address of local 0, + calls `method` with them and returns its result through `ret`. -/ +def outForwarderCode (loads : List Nat) (method : CIL.Extraction.MethodRef) : + List CIL.Instruction := + loads.map .ldarg ++ [.ldloca 0, .call method {}, .ret] -/-- `body` forwards the arguments it loads by `loads` to `callee`, whose body is - `calleeBody`, with the address of its only local, a byte home of `kind` created from +/-- `body` forwards the arguments it loads by `loads` to `method`, linked to `callee`, whose + body is `calleeBody`, with the address of its only local, a byte home of `kind` created from `initial`, as a last out argument whose contents it drops; it declares the callee's - return arity. References, spans, arrays and managed identities allocate no byte home, so + return arity and has return storage `narrow`. References, spans, arrays and managed + identities allocate no byte home, so `kind` excludes them. Any fitting initializer is accepted; the extractor emits `.unmodeled` without `InitLocals` and the kind's zero with it. -/ -structure ForwardsOut (body calleeBody : CIL.Method) (loads : List (Nat × Option CIL.NarrowKind)) - (callee : Nat) (narrow : Option CIL.NarrowKind) (kind : CIL.LocalKind) (initial : CIL.Value) : - Prop where - code : body.code = outForwarderCode loads callee narrow +structure ForwardsOut (body calleeBody : CIL.Method) (loads : List Nat) + (method : CIL.Extraction.MethodRef) (callee : Nat) (narrow : Option CIL.NarrowKind) + (kind : CIL.LocalKind) (initial : CIL.Value) : Prop where + code : body.code = outForwarderCode loads method + arity : method.argumentCount = loads.length + 1 + linked : body.linkAt (loads.length + 1) = some (.direct callee) locals : body.locals = [initial] localKinds : body.localKinds = [kind] numeric : kind ≠ .reference @@ -335,9 +383,9 @@ structure ForwardsOut (body calleeBody : CIL.Method) (loads : List (Nat × Optio notObject : kind ≠ .object fits : InitializerFits kind initial opaqueVectorLocals : body.opaqueVectorLocals = [] - vectorArgs : body.vectorArgs = [] parameterHomes : body.parameterHomes = [] returnsValue : body.returnsValue = calleeBody.returnsValue + returnNarrow : body.returnNarrow = narrow /-- What a fresh numeric home created from `initial` holds: nothing known when `initial` is `.unmodeled`, and otherwise the bytes of `initial`'s number at the home's width. -/ @@ -378,13 +426,13 @@ theorem make_fitting_local (memory : Memory) (activation : Nat) (kind : CIL.Loca /-- Entering a `ForwardsOut` body creates its local as one fresh writable home holding its initializer, keeps the caller's memory below `nextIdentity` and stays well formed. -/ theorem ForwardsOut.enterFrame {body calleeBody : CIL.Method} - {loads : List (Nat × Option CIL.NarrowKind)} {callee : Nat} {narrow : Option CIL.NarrowKind} - {kind : CIL.LocalKind} {initial : CIL.Value} - (forwards : ForwardsOut body calleeBody loads callee narrow kind initial) + {loads : List Nat} {method : CIL.Extraction.MethodRef} {callee : Nat} + {narrow : Option CIL.NarrowKind} {kind : CIL.LocalKind} {initial : CIL.Value} + (forwards : ForwardsOut body calleeBody loads method callee narrow kind initial) (args : List Value) (memory : Memory) (wellFormed : memory.WellFormed) : ∃ home result, CIL.Safety.enterFrame body args memory = - .ok (⟨memory.nextIdentity, [.bytes kind home], [home.allocation], [], 0⟩, result) ∧ + .ok (⟨memory.nextIdentity, [.bytes kind home], [home.allocation], 0⟩, result) ∧ memory.nextIdentity ≤ home.allocation ∧ access result home (localWidth kind) 1 true = .ok () ∧ LocalHolds kind initial result home ∧ @@ -399,38 +447,39 @@ theorem ForwardsOut.enterFrame {body calleeBody : CIL.Method} makeLocal_preserves_wellFormed _ _ _ _ _ _ _ wellFormed made⟩ simp only [CIL.Safety.enterFrame, forwards.opaqueVectorLocals, makeSelectedLocals, List.isEmpty_nil, ite_true, forwards.localKinds, forwards.locals, makeLocals, made, - forwards.vectorArgs, makeArgumentHomes, forwards.parameterHomes, makeParameterHomes, + forwards.parameterHomes, makeParameterHomes, List.map_nil, Bind.bind, Except.bind, Pure.pure, Except.pure, List.append_nil] #print axioms ForwardsOut.enterFrame /-- A forwarder that passes a fresh local as a last out argument and drops it meets `outer` when its callee meets `inner` at `extend input home`, `home` being that local. Each - argument must be valid under `outer.pre` (`checked`), since the forwarder checks them - all. The callee's arguments must be the forwarder's selected by `loads`, each kept by its - load's narrowing, and then `home`; its precondition must follow from `outer.pre` in the - memory after frame entry, where `home` is fresh, writable over the local's width and - holds its initializer; its footprint may name fresh storage or lie inside - `outer.footprint`; its postcondition must give `outer.post` once the frame has expired; - and its results must be scalars, which survive the expiry of `home`. The home's + argument must be valid and not a span home under `outer.pre` (`checked`), since the + forwarder checks them all. The callee's arguments must be the forwarder's selected by + `loads`, each kept as the forwarder reads it, and then `home`; its precondition must + follow from `outer.pre` in the memory after frame entry, where `home` is fresh, writable + over the local's width and holds its initializer; its footprint may name fresh storage or lie + inside `outer.footprint`; its postcondition must give `outer.post` once the frame has + expired; and its results must be scalars, which survive the expiry of `home`. The home's alignment is not surfaced. -/ theorem MethodContract.Holds.forward_out {Outer Inner Output : Type} {outer : MethodContract Outer Output} {inner : MethodContract Inner Output} {program : CIL.Program} {method callee : Nat} {body calleeBody : CIL.Method} - {loads : List (Nat × Option CIL.NarrowKind)} {narrow : Option CIL.NarrowKind} + {loads : List Nat} {called : CIL.Extraction.MethodRef} {narrow : Option CIL.NarrowKind} {kind : CIL.LocalKind} {initial : CIL.Value} (extend : Outer → Reference → Inner) (holds : inner.Holds program callee) (found : program[method]? = some body) (calleeFound : program[callee]? = some calleeBody) - (forwards : ForwardsOut body calleeBody loads callee narrow kind initial) + (forwards : ForwardsOut body calleeBody loads called callee narrow kind initial) (selected : ∀ input home, - loads.map (fun load => (outer.args input)[load.1]?) ++ + loads.map (fun index => (outer.args input)[index]?) ++ [some (.reference (.address home))] = (inner.args (extend input home)).map some) (checked : ∀ input memory, outer.pre input memory → - memory.WellFormed ∧ ValuesValid memory (outer.args input)) + memory.WellFormed ∧ ValuesValid memory (outer.args input) ∧ + ∀ value ∈ outer.args input, value.isHome = false) (narrowed : ∀ input memory home, outer.pre input memory → - ∀ pair ∈ loads.zip (inner.args (extend input home)), LoadsUnchanged pair.1.2 pair.2) + ∀ pair ∈ loads.zip (inner.args (extend input home)), LoadsUnchanged body pair.1 pair.2) (known : ∀ input memory home, outer.pre input memory → (inner.args (extend input home)).any Value.hasUnspecified = false) (unchanged : ∀ input memory home entered final out, outer.pre input memory → @@ -459,7 +508,7 @@ theorem MethodContract.Holds.forward_out {Outer Inner Output : Type} inner.post (extend input home) entered final out → outer.post input memory left out) : outer.Holds program method := by intro input memory permitted - obtain ⟨formed, outerValid⟩ := checked input memory permitted + obtain ⟨formed, outerValid, outerFree⟩ := checked input memory permitted obtain ⟨home, entered, setup, fresh, writable, initialized, entryBelow, enteredFormed⟩ := forwards.enterFrame (outer.args input) memory formed have owned := enterFrame_fresh _ _ _ _ _ setup @@ -478,9 +527,9 @@ theorem MethodContract.Holds.forward_out {Outer Inner Output : Type} have length : (inner.args (extend input home)).length = loads.length + 1 := by rw [split]; simpa using (congrArg List.length loadedSome) have fetched : ∀ pc : Nat, - body.code[pc]? = (outForwarderCode loads callee narrow)[pc]? := by + body.code[pc]? = (outForwarderCode loads called)[pc]? := by rw [forwards.code]; exact fun _ => rfl - have loadedNarrowed : ∀ pair ∈ loads.zip loaded, LoadsUnchanged pair.1.2 pair.2 := by + have loadedNarrowed : ∀ pair ∈ loads.zip loaded, LoadsUnchanged body pair.1 pair.2 := by intro pair member refine narrowed input memory home permitted pair ?_ have same : loads.length = loaded.length := by @@ -491,22 +540,22 @@ theorem MethodContract.Holds.forward_out {Outer Inner Output : Type} exact List.mem_append_left _ member have enteredValid := outerValid.preserve formed owned.1 apply WP.adequate.mpr - refine invoke_exists_of_run found (checkedValues_of_valid outerValid) setup ?_ - refine run_load_arguments found enteredValid loads 0 [] loaded + refine invoke_exists_of_run found (checkedValues_of_valid outerValid outerFree) setup ?_ + refine run_load_arguments found enteredValid outerFree loads 0 [] loaded (fun position load located => ?_) loadedSome.symm loadedNarrowed ?_ · have bound : position < loads.length := (List.getElem?_eq_some_iff.mp located).1 rw [Nat.zero_add, fetched, outForwarderCode, List.getElem?_append_left (by simpa using bound), List.getElem?_map, located] rfl rw [Nat.zero_add, List.append_nil] - let frame : Frame := ⟨memory.nextIdentity, [.bytes kind home], [home.allocation], [], 0⟩ - have addressed : step body (.localAddr 0) loads.length (outer.args input) frame + let frame : Frame := ⟨memory.nextIdentity, [.bytes kind home], [home.allocation], 0⟩ + have addressed : step body (.ldloca 0) loads.length (outer.args input) frame loaded.reverse entered = .ok (.next (loads.length + 1) (.reference (.address home) :: loaded.reverse) frame entered) := by have formedHome := access_reference_valid _ _ _ _ _ writable simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, - Bool.false_or, Bool.false_eq_true, ↓reduceIte, frame, List.getElem?_cons_zero, + Bool.false_eq_true, ↓reduceIte, frame, List.getElem?_cons_zero, localAddress, formValue, checkedAt, formedHome, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] refine run_next_exists _ found (by simp [fetched, outForwarderCode]) addressed ?_ @@ -516,12 +565,12 @@ theorem MethodContract.Holds.forward_out {Outer Inner Output : Type} rw [stack] have exitFresh : ∀ id ∈ frame.owned, memory.nextIdentity ≤ id := fun id member => (owned.2 id member).1 - refine run_forward_call holds found calleeFound - (by rw [length]; simp [fetched, outForwarderCode]) + refine run_forward_call (called := called) (prefixes := {}) holds found calleeFound + (by simp [fetched, outForwarderCode]) rfl (by rw [forwards.arity, length]) forwards.linked (by rw [fetched, outForwarderCode, List.getElem?_append_right (by simp; omega)] simp [show loads.length + 1 + 1 - loads.length = 2 by omega]) - forwards.returnsValue + forwards.returnsValue forwards.returnNarrow (fun final out satisfied => unchanged input memory home entered final out permitted satisfied) calleePre (known input memory home permitted) (fun final values valid returns => ?_) diff --git a/CIL/Safety/FrameAllocations.lean b/CIL/Safety/FrameAllocations.lean index fa7ffcf57..8e70ade1c 100644 --- a/CIL/Safety/FrameAllocations.lean +++ b/CIL/Safety/FrameAllocations.lean @@ -24,12 +24,6 @@ theorem makeLocals_owned (m result : Memory) (activation : Nat) (kinds : List CI #print axioms makeLocals_owned -theorem makeArgumentHomes_owned (m result : Memory) (activation : Nat) (indices : List Nat) - (args : List Value) (homes : List (Nat × LocalSlot)) (owned : List AllocationId) - (h : makeArgumentHomes activation indices args m = .ok (homes, owned, result)) : - FrameAllocations result activation owned := by - exact (makeArgumentHomes_setup _ _ _ _ _ _ _ h).allocations - theorem makeParameterHomes_owned (m result : Memory) (activation : Nat) (parameters : List (Nat × CIL.LocalKind)) (args : List Value) (homes : List (Nat × LocalSlot)) (owned : List AllocationId) diff --git a/CIL/Safety/FrameEntryRenaming.lean b/CIL/Safety/FrameEntryRenaming.lean index 011b4baef..b48079c80 100644 --- a/CIL/Safety/FrameEntryRenaming.lean +++ b/CIL/Safety/FrameEntryRenaming.lean @@ -5,16 +5,15 @@ import CIL.Safety.FrameRenaming Ordinary declarations preserve retained initializers while allocating corresponding homes. Entry transport derives target success from the actual successful source entry. -Opaque slots and argument or parameter homes have separate setup semantics and are +Opaque slots and parameter homes have separate setup semantics and are explicitly excluded by this entry theorem. -/ namespace CIL.Safety -/-- Ordinary declarations use explicit local metadata without auxiliary argument homes. -/ +/-- Ordinary declarations use explicit local metadata and no parameter homes. -/ structure OrdinaryFrame (body : CIL.Method) : Prop where noOpaque : body.opaqueVectorLocals = [] - vectorArguments : body.vectorArgs = [] parameterHomes : body.parameterHomes = [] layouts : ∀ kind ∈ body.localKinds, localWidth kind < nativeLimit ∧ ∀ width writable, kind = .span width writable → (0 < width ∧ width < 2^64) @@ -23,7 +22,6 @@ structure OrdinaryFrame (body : CIL.Method) : Prop where structure UnmodeledFrame (body : CIL.Method) : Prop where initializers : body.locals = body.localKinds.map (fun _ => .unmodeled) noOpaque : body.opaqueVectorLocals = [] - vectorArguments : body.vectorArgs = [] parameterHomes : body.parameterHomes = [] layouts : ∀ kind ∈ body.localKinds, localWidth kind < nativeLimit ∧ ∀ width writable, kind = .span width writable → (0 < width ∧ width < 2^64) @@ -68,21 +66,19 @@ theorem Memory.Renamed.enterFrameUnmodeled {mapping : AllocationRenaming} extension, sourceMade, targetMade, remains, slots, owned⟩ := extended.makeLocalsUnmodeled sourceFormed targetFormed staticBounds source.nextIdentity target.nextIdentity (enterScopes_new _ _ _) selected targetReady.layouts - let sourceFrame : Frame := ⟨source.nextIdentity, sourceSlots, sourceOwned, [], 0⟩ - let targetFrame : Frame := ⟨target.nextIdentity, targetSlots, targetOwned, [], 0⟩ + let sourceFrame : Frame := ⟨source.nextIdentity, sourceSlots, sourceOwned, 0⟩ + let targetFrame : Frame := ⟨target.nextIdentity, targetSlots, targetOwned, 0⟩ refine ⟨later, sourceFrame, targetFrame, sourceAfter, targetAfter, extension, ?_, ?_, remains, ?_⟩ · simp only [enterFrame_of_no_opaque _ _ _ sourceReady.noOpaque, sourceReady.initializers, - sourceMade, sourceReady.vectorArguments, sourceReady.parameterHomes] - simp only [makeArgumentHomes, makeParameterHomes, Bind.bind, Except.bind, + sourceMade, sourceReady.parameterHomes] + simp only [makeParameterHomes, Bind.bind, Except.bind, Pure.pure, Except.pure, List.map_nil, List.append_nil, sourceFrame] · simp only [enterFrame_of_no_opaque _ _ _ targetReady.noOpaque, targetReady.initializers, - targetMade, targetReady.vectorArguments, targetReady.parameterHomes] - simp only [makeArgumentHomes, makeParameterHomes, Bind.bind, Except.bind, + targetMade, targetReady.parameterHomes] + simp only [makeParameterHomes, Bind.bind, Except.bind, Pure.pure, Except.pure, List.map_nil, List.append_nil, targetFrame] - refine ⟨enterScopes_new _ _ _, slots, owned, rfl, ?_, rfl⟩ - intro home member - cases member + exact ⟨enterScopes_new _ _ _, slots, owned, rfl⟩ #print axioms Memory.Renamed.enterFrameUnmodeled /-- Successful frame entry preserves retained initializers and inserts only private dead homes. -/ @@ -108,12 +104,12 @@ theorem Memory.Renamed.enterFrameInitialized {mapping : AllocationRenaming} sourceFrame targetFrame := by have originalEntry := entered simp only [enterFrame_of_no_opaque _ _ _ sourceReady.noOpaque, - sourceReady.vectorArguments, sourceReady.parameterHomes, Bind.bind, Except.bind] at entered + sourceReady.parameterHomes, Bind.bind, Except.bind] at entered split at entered · cases entered rename_i locals made rcases locals with ⟨sourceSlots, sourceOwned, sourceAfter⟩ - simp only [makeArgumentHomes, makeParameterHomes, + simp only [makeParameterHomes, Pure.pure, Except.pure, List.map_nil, List.append_nil, Except.ok.injEq, Prod.mk.injEq] at entered obtain ⟨rfl, rfl⟩ := entered @@ -124,21 +120,19 @@ theorem Memory.Renamed.enterFrameInitialized {mapping : AllocationRenaming} remains, slots, owned⟩ := extended.makeLocalsInitialized sourceFormed targetFormed staticBounds source.nextIdentity target.nextIdentity (enterScopes_new _ _ _) selected targetReady.layouts made - let targetFrame : Frame := ⟨target.nextIdentity, targetSlots, targetOwned, [], 0⟩ + let targetFrame : Frame := ⟨target.nextIdentity, targetSlots, targetOwned, 0⟩ refine ⟨later, _, targetFrame, sourceAfter, targetAfter, extension, originalEntry, ?_, remains, ?_⟩ · simp only [enterFrame_of_no_opaque _ _ _ targetReady.noOpaque, - targetMade, targetReady.vectorArguments, targetReady.parameterHomes] - simp only [makeArgumentHomes, makeParameterHomes, Bind.bind, Except.bind, + targetMade, targetReady.parameterHomes] + simp only [makeParameterHomes, Bind.bind, Except.bind, Pure.pure, Except.pure, List.map_nil, List.append_nil, targetFrame] - refine ⟨enterScopes_new _ _ _, slots, owned, rfl, ?_, rfl⟩ - intro home member - cases member + exact ⟨enterScopes_new _ _ _, slots, owned, rfl⟩ #print axioms Memory.Renamed.enterFrameInitialized /-- A sequential computation cannot introduce a throw when neither component can throw. -/ private theorem bind_not_thrown (first : ExecutionResult α) (next : α → ExecutionResult β) - (exception : String) (firstSafe : first ≠ .error (.thrown exception)) + (exception : ExceptionType) (firstSafe : first ≠ .error (.thrown exception)) (nextSafe : ∀ value, next value ≠ .error (.thrown exception)) : (first >>= next) ≠ .error (.thrown exception) := by cases first <;> simp_all [Bind.bind, Except.bind] @@ -146,14 +140,14 @@ private theorem bind_not_thrown (first : ExecutionResult α) (next : α → Exec /-- Checked memory operations report memory faults, not named CLR exceptions. -/ private theorem checkedAt_not_thrown (reference : ManagedReference) (width : Nat) - (result : Checked α) (exception : String) : + (result : Checked α) (exception : ExceptionType) : checkedAt reference width result ≠ .error (.thrown exception) := by cases result <;> simp [checkedAt, Except.mapError] #print axioms checkedAt_not_thrown /-- Scalar encoding reports kind mismatches without raising CLR exceptions. -/ theorem localNumber_not_thrown (kind : CIL.LocalKind) (initial : CIL.Value) - (exception : String) : localNumber kind initial ≠ .error (.thrown exception) := by + (exception : ExceptionType) : localNumber kind initial ≠ .error (.thrown exception) := by intro failed unfold localNumber at failed repeat' first | (solve | cases failed) | split at failed @@ -161,7 +155,7 @@ theorem localNumber_not_thrown (kind : CIL.LocalKind) (initial : CIL.Value) /-- Initializing a byte home cannot raise a named CLR exception. -/ theorem storeLocal_scalar_not_thrown (memory : Memory) (kind : CIL.LocalKind) - (reference : Reference) (initial : CIL.Value) (exception : String) : + (reference : Reference) (initial : CIL.Value) (exception : ExceptionType) : storeLocal memory (.bytes kind reference) (.scalar initial) ≠ .error (.thrown exception) := by dsimp only [storeLocal] split @@ -185,7 +179,7 @@ theorem storeLocal_scalar_not_thrown (memory : Memory) (kind : CIL.LocalKind) /-- Fresh home allocation reports checked memory faults rather than CLR exceptions. -/ theorem allocateHome_not_thrown (memory : Memory) (activation width alignment : Nat) - (exception : String) : + (exception : ExceptionType) : allocateHome memory activation width alignment ≠ .error (.thrown exception) := by intro failed unfold allocateHome checkedAt at failed @@ -198,7 +192,7 @@ theorem allocateHome_not_thrown (memory : Memory) (activation width alignment : /-- Local declaration setup reports model faults, never a named CLR exception. -/ theorem makeLocal_not_thrown (activation : Nat) (kind : CIL.LocalKind) (initial : CIL.Value) - (memory : Memory) (exception : String) : + (memory : Memory) (exception : ExceptionType) : makeLocal activation kind initial memory ≠ .error (.thrown exception) := by intro failed have homes := allocateHome_not_thrown memory activation (localWidth kind) @@ -216,7 +210,7 @@ theorem makeLocal_not_thrown (activation : Nat) (kind : CIL.LocalKind) (initial /-- A list of local initializers cannot synthesize a named CLR exception. -/ theorem makeLocals_not_thrown (activation : Nat) (kinds : List CIL.LocalKind) - (initials : List CIL.Value) (memory : Memory) (exception : String) : + (initials : List CIL.Value) (memory : Memory) (exception : ExceptionType) : makeLocals activation kinds initials memory ≠ .error (.thrown exception) := by induction kinds generalizing initials memory with | nil => cases initials <;> simp [makeLocals, Pure.pure, Except.pure] @@ -240,16 +234,16 @@ theorem makeLocals_not_thrown (activation : Nat) (kinds : List CIL.LocalKind) /-- Ordinary frame entry cannot manufacture a named CLR exception. -/ theorem enterFrame_not_thrown (body : CIL.Method) (ready : OrdinaryFrame body) - (arguments : List Value) (memory : Memory) (exception : String) : + (arguments : List Value) (memory : Memory) (exception : ExceptionType) : enterFrame body arguments memory ≠ .error (.thrown exception) := by intro failed simp only [enterFrame_of_no_opaque _ _ _ ready.noOpaque, - ready.vectorArguments, ready.parameterHomes, Bind.bind, Except.bind] at failed + ready.parameterHomes, Bind.bind, Except.bind] at failed split at failed · rename_i reason localsFailed cases failed exact makeLocals_not_thrown _ _ _ _ _ localsFailed - · simp only [makeArgumentHomes, makeParameterHomes, + · simp only [makeParameterHomes, Pure.pure, Except.pure] at failed cases failed #print axioms enterFrame_not_thrown diff --git a/CIL/Safety/FrameEquivalence.lean b/CIL/Safety/FrameEquivalence.lean index a235e0d54..65dafa1a0 100644 --- a/CIL/Safety/FrameEquivalence.lean +++ b/CIL/Safety/FrameEquivalence.lean @@ -97,8 +97,8 @@ theorem Memory.Equivalent.loadLocal {m n : Memory} (same : m.Equivalent n) cases value <;> simp only [CIL.Safety.loadLocal, same.arrayValidate] theorem Memory.Equivalent.localAddress {m n : Memory} (same : m.Equivalent n) - (slot : LocalSlot) : - CIL.Safety.localAddress m slot = CIL.Safety.localAddress n slot := by + (index : Nat) (slot : LocalSlot) : + CIL.Safety.localAddress m index slot = CIL.Safety.localAddress n index slot := by cases slot <;> simp only [CIL.Safety.localAddress, same.formValue] theorem Memory.Equivalent.allocateHome {m n : Memory} (same : m.Equivalent n) @@ -136,7 +136,7 @@ theorem Memory.Equivalent.storeLocal {m n : Memory} (same : m.Equivalent n) | reference _ => exact .error _ | span _ _ => exact .error _ | bufferSpan _ | readOnlyReference _ | array _ - | methodPointer _ | methodTable _ | object _ => exact .error _ + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ | object _ | spanHome _ => exact .error _ | root root | readOnlyRoot root => cases value with | reference reference | readOnlyReference reference => @@ -147,7 +147,8 @@ theorem Memory.Equivalent.storeLocal {m n : Memory} (same : m.Equivalent n) exact .ok ⟨rfl, same⟩ | scalar scalar => cases scalar <;> exact .error _ | span reference length => exact .error _ - | bufferSpan _ | array _ | methodPointer _ | methodTable _ | object _ => exact .error _ + | typeHandle _ | runtimeType _ | bufferSpan _ | array _ | methodPointer _ | methodTable _ | object _ | spanHome _ => + exact .error _ | bytes kind reference => cases value with | scalar value => @@ -170,7 +171,7 @@ theorem Memory.Equivalent.storeLocal {m n : Memory} (same : m.Equivalent n) | reference reference => exact .error _ | span reference length => exact .error _ | bufferSpan _ | readOnlyReference _ | array _ - | methodPointer _ | methodTable _ | object _ => exact .error _ + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ | object _ | spanHome _ => exact .error _ | span width writable stored => cases value case scalar scalar => cases scalar <;> exact .error _ @@ -323,44 +324,6 @@ theorem Memory.Equivalent.makeSelectedLocals {m n : Memory} (same : m.Equivalent · exact same.makeLocals _ _ _ · exact same.makeSelectedLocalsFrom _ _ _ _ _ -theorem Memory.Equivalent.makeArgumentHomes {m n : Memory} (same : m.Equivalent n) - (activation : Nat) (indices : List Nat) (args : List Value) : - ResultEquivalent OwnedEquivalent (CIL.Safety.makeArgumentHomes activation indices args m) - (CIL.Safety.makeArgumentHomes activation indices args n) := by - induction indices generalizing m n with - | nil => exact .ok ⟨rfl, rfl, same⟩ - | cons index indices ih => - simp only [CIL.Safety.makeArgumentHomes] - cases found : args[index]? with - | none => - simp only [Option.map_none, Option.getD_none, Bool.false_eq_true, ite_false] - exact .error _ - | some value => - simp only [Option.map_some, Option.getD_some] - cases value with - | reference reference => exact .error _ - | span reference length => exact .error _ - | bufferSpan _ | readOnlyReference _ | array _ - | methodPointer _ | methodTable _ | object _ => exact .error _ - | scalar value => - cases value <;> first | exact .error _ | skip - apply ResultEquivalent.bind (same.allocateHome _ _ _) - intro left right related - obtain ⟨reference, memory⟩ := left - obtain ⟨other, otherMemory⟩ := right - obtain ⟨rfl, equivalent⟩ := related - apply ResultEquivalent.bind (equivalent.storeLocal _ _) - intro left right related - obtain ⟨slot, stored⟩ := left - obtain ⟨otherSlot, otherStored⟩ := right - obtain ⟨rfl, storedEquivalent⟩ := related - apply ResultEquivalent.bind (ih storedEquivalent) - intro left right related - obtain ⟨homes, owned, result⟩ := left - obtain ⟨otherHomes, otherOwned, otherResult⟩ := right - obtain ⟨rfl, rfl, resultEquivalent⟩ := related - exact .ok ⟨rfl, rfl, resultEquivalent⟩ - theorem Memory.Equivalent.makeParameterHomes {m n : Memory} (same : m.Equivalent n) (activation : Nat) (parameters : List (Nat × CIL.LocalKind)) (args : List Value) : ResultEquivalent OwnedEquivalent (CIL.Safety.makeParameterHomes activation parameters args m) @@ -400,12 +363,7 @@ theorem Memory.Equivalent.enterFrame {m n : Memory} (same : m.Equivalent n) obtain ⟨locals, owned, memory⟩ := left obtain ⟨otherLocals, otherOwned, otherMemory⟩ := right obtain ⟨rfl, rfl, equivalent⟩ := related - apply ResultEquivalent.bind (equivalent.makeArgumentHomes _ _ _) - intro left right related - obtain ⟨arguments, argumentIds, stored⟩ := left - obtain ⟨otherArguments, otherIds, otherStored⟩ := right - obtain ⟨rfl, rfl, storedEquivalent⟩ := related - apply ResultEquivalent.bind (storedEquivalent.makeParameterHomes _ _ _) + apply ResultEquivalent.bind (equivalent.makeParameterHomes _ _ _) intro left right related obtain ⟨parameters, parameterIds, result⟩ := left obtain ⟨otherParameters, otherParameterIds, otherResult⟩ := right @@ -422,7 +380,6 @@ theorem Memory.Equivalent.leaveFrame {m n : Memory} (same : m.Equivalent n) | cons id ids ih => exact ih (same.expire id) #print axioms Memory.Equivalent.makeLocals -#print axioms Memory.Equivalent.makeArgumentHomes #print axioms Memory.Equivalent.makeParameterHomes #print axioms Memory.Equivalent.enterFrame #print axioms Memory.Equivalent.leaveFrame diff --git a/CIL/Safety/FrameMemoryBelow.lean b/CIL/Safety/FrameMemoryBelow.lean index 4ad9e4330..6f38df35c 100644 --- a/CIL/Safety/FrameMemoryBelow.lean +++ b/CIL/Safety/FrameMemoryBelow.lean @@ -31,13 +31,6 @@ theorem makeLocals_preserves_caller_memory (m result : Memory) (activation : Nat MemoryBelow m.nextIdentity m result := by exact (makeLocals_setup _ _ _ _ _ _ _ h).below -theorem makeArgumentHomes_preserves_caller_memory (m result : Memory) (activation : Nat) - (indices : List Nat) (args : List Value) (homes : List (Nat × LocalSlot)) - (owned : List AllocationId) - (h : makeArgumentHomes activation indices args m = .ok (homes, owned, result)) : - MemoryBelow m.nextIdentity m result := by - exact (makeArgumentHomes_setup _ _ _ _ _ _ _ h).below - theorem initialize_parameter_preserves_caller_memory (m home result : Memory) (activation : Nat) (kind : CIL.LocalKind) (slot final : LocalSlot) (owned : List AllocationId) (value : Value) (made : makeLocal activation kind .unmodeled m = .ok (slot, owned, home)) @@ -60,7 +53,6 @@ theorem enterFrame_preserves_caller_memory (body : CIL.Method) (args : List Valu #print axioms allocateHome_preserves_caller_memory #print axioms makeLocal_preserves_caller_memory #print axioms makeLocals_preserves_caller_memory -#print axioms makeArgumentHomes_preserves_caller_memory #print axioms enterFrame_preserves_caller_memory diff --git a/CIL/Safety/FrameOwnership.lean b/CIL/Safety/FrameOwnership.lean index a40a85376..f9365e6b1 100644 --- a/CIL/Safety/FrameOwnership.lean +++ b/CIL/Safety/FrameOwnership.lean @@ -5,7 +5,7 @@ import CIL.Safety.AllocationExtension # Frame ownership A frame owns only allocations created after it was entered, during setup or by temporaries -that `newVector256` and `newAggregate` allocate, so leaving it expires nothing the caller +that a value-type `newobj` allocates, so leaving it expires nothing the caller already had. The headline results are `frame_entry_exit_preserves_caller` and `frame_entry_exit_preserves_reference`: entering and then leaving a frame keeps every older allocation, and every reference the caller could form, unchanged. @@ -27,12 +27,6 @@ theorem makeLocals_fresh (m result : Memory) (activation : Nat) (kinds : List CI FreshOwned m result owned := by exact (makeLocals_setup _ _ _ _ _ _ _ h).fresh -theorem makeArgumentHomes_fresh (m result : Memory) (activation : Nat) (indices : List Nat) - (args : List Value) (homes : List (Nat × LocalSlot)) (owned : List AllocationId) - (h : makeArgumentHomes activation indices args m = .ok (homes, owned, result)) : - FreshOwned m result owned := by - exact (makeArgumentHomes_setup _ _ _ _ _ _ _ h).fresh - theorem makeParameterHomes_fresh (m result : Memory) (activation : Nat) (parameters : List (Nat × CIL.LocalKind)) (args : List Value) (homes : List (Nat × LocalSlot)) (owned : List AllocationId) diff --git a/CIL/Safety/FrameProgress.lean b/CIL/Safety/FrameProgress.lean index c291d1a79..03b7165d7 100644 --- a/CIL/Safety/FrameProgress.lean +++ b/CIL/Safety/FrameProgress.lean @@ -108,33 +108,6 @@ theorem makeLocals_succeeds (m : Memory) (activation : Nat) exact ⟨slot :: slots, ids ++ other, result, by simp [makeLocals, prepared, rest, Bind.bind, Except.bind, Pure.pure, Except.pure]⟩ -/-- Aggregate-home indices must designate actual by-value aggregate arguments. - This is signature/type compatibility, not an access or future-run condition. -/ -def AggregateArgumentsFit (indices : List Nat) (args : List Value) : Prop := - ∀ index ∈ indices, ∃ bits, args[index]? = some (.scalar (.v256 bits)) - -theorem makeArgumentHomes_succeeds (m : Memory) (activation : Nat) (indices : List Nat) - (args : List Value) (wellFormed : m.WellFormed) (fits : AggregateArgumentsFit indices args) : - ∃ homes owned result, - makeArgumentHomes activation indices args m = .ok (homes, owned, result) := by - induction indices generalizing m with - | nil => exact ⟨[], [], m, rfl⟩ - | cons index indices ih => - obtain ⟨bits, argument⟩ := fits index (by simp) - obtain ⟨reference, memory, home⟩ := - allocateHome_succeeds (alignment := 1) m activation (localWidth .vector256) wellFormed - (localWidth_bounded _) (by decide) - obtain ⟨slot, stored, store⟩ := fresh_home_storeLocal_succeeds m memory activation .vector256 - reference (.v256 bits) bits.toNat home rfl - have valid := storeLocal_preserves_wellFormed _ _ _ _ _ - (allocateHome_preserves_wellFormed _ _ _ _ _ wellFormed home) store - obtain ⟨homes, owned, result, rest⟩ := - ih stored valid (fun i member => fits i (by simp [member])) - exact ⟨(index, slot) :: homes, reference.allocation :: owned, result, by - simp [makeArgumentHomes, argument, home, store, rest, - Value.hasUnspecified, CIL.Value.hasUnspecified, - Bind.bind, Except.bind, Pure.pure, Except.pure]⟩ - /-- An argument matches its numeric home, reference root, or typed span view. -/ def ParameterFits (kind : CIL.LocalKind) : Value → Prop | .methodPointer pointer => kind = .methodPointer pointer.signature @@ -142,10 +115,11 @@ def ParameterFits (kind : CIL.LocalKind) : Value → Prop | .scalar value => kind ≠ .reference ∧ ∃ number, localNumber kind value = .ok number | .reference _ => kind = .reference | .object _ => kind = .object - | .span _ _ => False + | .typeHandle _ | .runtimeType _ | .span _ _ => False | .readOnlyReference _ => kind = .reference | .bufferSpan view => kind = .span view.width view.writable ∧ (0 < view.width ∧ view.width < 2^64) | .array view => kind = .array view.elementType + | .spanHome _ => False /-- Every parameter home names an existing argument that fits its kind. -/ def ParameterArgumentsFit (parameters : List (Nat × CIL.LocalKind)) (args : List Value) : @@ -159,6 +133,7 @@ theorem initializeParameter_succeeds (m : Memory) (activation : Nat) (kind : CIL makeLocal activation kind .unmodeled m = .ok (slot, owned, home) ∧ storeLocal home slot value = .ok (final, result) := by cases value with + | typeHandle _ | runtimeType _ | spanHome _ => exact fits.elim | methodPointer pointer => change kind = .methodPointer pointer.signature at fits subst kind @@ -175,7 +150,7 @@ theorem initializeParameter_succeeds (m : Memory) (activation : Nat) (kind : CIL | readOnlyReference reference => change kind = .reference at fits subst kind - have formed := checkedValue_of_valid m (.readOnlyReference reference) valid + have formed := checkedValue_of_valid m (.readOnlyReference reference) valid rfl refine ⟨.root none, [], m, .readOnlyRoot reference, m, rfl, ?_⟩ cases reference <;> simp_all [checkedValue, formValue, Value.Valid, checkedAt, Except.mapError, storeLocal, Value.hasUnspecified, Bind.bind, Except.bind, @@ -200,7 +175,7 @@ theorem initializeParameter_succeeds (m : Memory) (activation : Nat) (kind : CIL | reference r => change kind = .reference at fits subst kind - have formed := checkedValue_of_valid m (.reference r) valid + have formed := checkedValue_of_valid m (.reference r) valid rfl exact ⟨.root none, [], m, .root (some r), m, rfl, by simp only [checkedValue] at formed simp [storeLocal, formed, Value.hasUnspecified, @@ -208,7 +183,7 @@ theorem initializeParameter_succeeds (m : Memory) (activation : Nat) (kind : CIL | object reference => change kind = .object at fits subst kind - have formed := checkedValue_of_valid m (.object reference) valid + have formed := checkedValue_of_valid m (.object reference) valid rfl exact ⟨.object none, [], m, .object (some reference), m, rfl, by simp only [checkedValue] at formed simp [storeLocal, formed, Value.hasUnspecified, @@ -323,8 +298,7 @@ theorem makeSelectedLocals_succeeds (m : Memory) (activation : Nat) (indices : L /-- A method's frame metadata fits its arguments, so `enterFrame` cannot fault on a well-formed memory with valid arguments (`enterFrame_succeeds`). -/ def FrameSetupFits (body : CIL.Method) (args : List Value) : Prop := - InitializersFit body.localKinds body.locals ∧ AggregateArgumentsFit body.vectorArgs args ∧ - ParameterArgumentsFit body.parameterHomes args ∧ + InitializersFit body.localKinds body.locals ∧ ParameterArgumentsFit body.parameterHomes args ∧ SelectedKindsFit body.opaqueVectorLocals 0 body.localKinds @[simp] theorem selectedKindsFit_nil (index : Nat) (kinds : List CIL.LocalKind) : @@ -337,7 +311,7 @@ def FrameSetupFits (body : CIL.Method) (args : List Value) : Prop := theorem frameSetupFits_of_no_opaque (body : CIL.Method) (args : List Value) (none : body.opaqueVectorLocals = []) : FrameSetupFits body args ↔ - InitializersFit body.localKinds body.locals ∧ AggregateArgumentsFit body.vectorArgs args ∧ + InitializersFit body.localKinds body.locals ∧ ParameterArgumentsFit body.parameterHomes args := by simp only [FrameSetupFits, none, selectedKindsFit_nil, and_true] #print axioms frameSetupFits_of_no_opaque @@ -348,20 +322,14 @@ theorem enterFrame_succeeds (body : CIL.Method) (args : List Value) (m : Memory) ∃ frame memory, enterFrame body args m = .ok (frame, memory) := by obtain ⟨locals, owned, memory, prepared⟩ := makeSelectedLocals_succeeds m m.nextIdentity body.opaqueVectorLocals body.localKinds - body.locals wellFormed fits.1 fits.2.2.2 + body.locals wellFormed fits.1 fits.2.2 have setup := makeSelectedLocals_setup _ _ _ _ _ _ _ _ prepared have valid := setup.wellFormed wellFormed - obtain ⟨arguments, ids, result, homes⟩ := - makeArgumentHomes_succeeds memory m.nextIdentity body.vectorArgs args valid fits.2.1 - have resultWF := makeArgumentHomes_preserves_wellFormed _ _ _ _ _ _ _ valid homes - have ext := setup.fresh.1.trans - (makeArgumentHomes_fresh _ _ _ _ _ _ _ homes).1 - obtain ⟨parameters, other, final, parametersMade⟩ := makeParameterHomes_succeeds result - m.nextIdentity body.parameterHomes args resultWF (validArgs.preserve wellFormed ext) - fits.2.2.1 - exact ⟨⟨m.nextIdentity, locals ++ parameters.map Prod.snd, (owned ++ ids) ++ other, - arguments, 0⟩, final, by - simp [enterFrame, prepared, homes, parametersMade, Bind.bind, Except.bind, Pure.pure, + obtain ⟨parameters, other, final, parametersMade⟩ := makeParameterHomes_succeeds memory + m.nextIdentity body.parameterHomes args valid (validArgs.preserve wellFormed setup.fresh.1) + fits.2.1 + exact ⟨⟨m.nextIdentity, locals ++ parameters.map Prod.snd, owned ++ other, 0⟩, final, by + simp [enterFrame, prepared, parametersMade, Bind.bind, Except.bind, Pure.pure, Except.pure]⟩ /-- Entering a frame whose metadata fits its arguments succeeds and starts the callee in a @@ -377,7 +345,6 @@ theorem enterFrame_live_succeeds (program : CIL.Program) (body : CIL.Method) #print axioms makeLocal_succeeds #print axioms makeLocals_succeeds -#print axioms makeArgumentHomes_succeeds #print axioms enterFrame_succeeds #print axioms enterFrame_live_succeeds diff --git a/CIL/Safety/FrameRenaming.lean b/CIL/Safety/FrameRenaming.lean index c6f008255..54a4d99e1 100644 --- a/CIL/Safety/FrameRenaming.lean +++ b/CIL/Safety/FrameRenaming.lean @@ -9,15 +9,12 @@ Suspended frames retain their correspondence when nested calls add mappings and namespace CIL.Safety -/-- A retained activation, including private dead homes and vector argument homes. -/ +/-- A retained activation, including private dead homes. -/ structure Frame.Renamed (indices : LocalIndexMap) (mapping : AllocationRenaming) (scopes : Nat → Nat → Prop) (source target : Frame) : Prop where activation : scopes source.activation target.activation locals : indices.Selects (LocalSlot.Renamed mapping) source.locals target.locals owned : OwnedAllocations mapping source.owned target.owned - arguments : target.arguments = source.arguments.map - (fun home => (home.1, mapping.localSlot home.2)) - argumentBounds : ∀ home ∈ source.arguments, home.2.Below mapping.sourceLimit stackBytes : target.stackBytes = source.stackBytes /-- Nested calls can extend identity and scope correspondence without rewriting suspended frames. -/ @@ -27,16 +24,9 @@ theorem Frame.Renamed.extend {indices : LocalIndexMap} {earlier later : Allocati (extension : earlier.Extends later) (scopeExtension : ∀ left right, before left right → after left right) : Frame.Renamed indices later after source target := by - refine ⟨scopeExtension _ _ related.activation, + exact ⟨scopeExtension _ _ related.activation, related.locals.imp (fun _ _ slots => slots.extend extension), - related.owned.extend extension, ?_, ?_, related.stackBytes⟩ - · rw [related.arguments] - apply List.map_congr_left - intro home member - exact congrArg (fun slot => (home.1, slot)) - (extension.localSlot home.2 (related.argumentBounds home member)).symm - · intro home member - exact (related.argumentBounds home member).mono extension.source home.2 + related.owned.extend extension, related.stackBytes⟩ #print axioms Frame.Renamed.extend /-- Assignment to a retained local updates its relocated target operand. -/ @@ -50,7 +40,7 @@ theorem Frame.Renamed.setLocal {indices : LocalIndexMap} {mapping : AllocationRe { target with locals := target.locals.set (indices.index position) (mapping.localSlot slot) } := ⟨related.activation, related.locals.set position bound slot _ ⟨slotBound, rfl⟩, - related.owned, related.arguments, related.argumentBounds, related.stackBytes⟩ + related.owned, related.stackBytes⟩ #print axioms Frame.Renamed.setLocal /-- A paired dynamic allocation adds corresponding ownership and the same quota charge. -/ @@ -66,7 +56,7 @@ theorem Frame.Renamed.addOwned {indices : LocalIndexMap} {mapping : AllocationRe owned := mapping.image identity :: target.owned stackBytes := target.stackBytes + reserved } := ⟨related.activation, related.locals, .keep identity bound related.owned, - related.arguments, related.argumentBounds, congrArg (· + reserved) related.stackBytes⟩ + congrArg (· + reserved) related.stackBytes⟩ #print axioms Frame.Renamed.addOwned end CIL.Safety diff --git a/CIL/Safety/FrameSetupLemmas.lean b/CIL/Safety/FrameSetupLemmas.lean index 1f712dae3..2154bf8c5 100644 --- a/CIL/Safety/FrameSetupLemmas.lean +++ b/CIL/Safety/FrameSetupLemmas.lean @@ -11,7 +11,7 @@ import CIL.Safety.ObjectsKept What `enterFrame` (in `CIL/Safety/Frames.lean`) does to memory, collected in one `FrameSetup` record: the frame owns only fresh allocations of its own activation, caller memory below the entry watermark is untouched, the static registry is unchanged, and every -slot refers to live storage. Each builder (`makeLocal`, `makeArgumentHomes`, ...) proves a +slot refers to live storage. Each builder (`makeLocal`, `makeParameterHomes`, ...) proves a `FrameSetup`, and `FrameSetup.append` composes them; the other `Frame*` files project it. -/ @@ -732,41 +732,6 @@ theorem makeSelectedLocals_setup (m result : Memory) (activation : Nat) (indices · exact makeLocals_setup _ _ _ _ _ _ _ h · exact makeSelectedLocalsFrom_setup _ _ _ _ _ _ _ _ _ h -theorem makeArgumentHomes_setup (m result : Memory) (activation : Nat) (indices : List Nat) - (args : List Value) (homes : List (Nat × LocalSlot)) (owned : List AllocationId) - (h : makeArgumentHomes activation indices args m = .ok (homes, owned, result)) : - FrameSetup activation m result (homes.map Prod.snd) owned := by - induction indices generalizing m homes owned result with - | nil => cases h; exact .empty _ _ - | cons index indices ih => - simp only [makeArgumentHomes] at h - have known : (args[index]?.map Value.hasUnspecified).getD false = false := by - cases hu : (args[index]?.map Value.hasUnspecified).getD false with - | false => rfl - | true => simp only [hu, ite_true] at h; cases h - simp only [known, Bool.false_eq_true, ite_false] at h - split at h - · rename_i bits hv - cases ha : allocateHome m activation (localWidth .vector256) <;> - simp only [ha, Bind.bind, Except.bind] at h - · cases h - · rename_i home - obtain ⟨reference, memory⟩ := home - cases hs : storeLocal memory (.bytes .vector256 reference) (.scalar (.v256 bits)) <;> - simp only [hs] at h - · cases h - · rename_i stored - obtain ⟨slot, memory'⟩ := stored - have first := (allocateHome_setup _ _ _ _ _ .vector256 ha).store hs - cases hr : makeArgumentHomes activation indices args memory' <;> - simp only [hr, Pure.pure, Except.pure] at h - · cases h - · rename_i rest - obtain ⟨rest, other, final⟩ := rest - cases h - exact first.append (ih memory' final rest other hr) - · cases h - theorem makeParameterHomes_setup (m result : Memory) (activation : Nat) (parameters : List (Nat × CIL.LocalKind)) (args : List Value) (homes : List (Nat × LocalSlot)) (owned : List AllocationId) @@ -798,20 +763,10 @@ theorem makeParameterHomes_setup (m result : Memory) (activation : Nat) exact first.append (ih memory' final rest other hr) · cases h -theorem FrameSetup.select {activation : Nat} {before after : Memory} - {slots selected : List LocalSlot} {owned : List AllocationId} - (setup : FrameSetup activation before after slots owned) - (subset : ∀ slot ∈ selected, slot ∈ slots) : - FrameSetup activation before after selected owned := - { setup with - references := fun hm slot member => setup.references hm slot (subset slot member) - above := fun slot member => setup.above slot (subset slot member) } - /-- Everything `enterFrame` guarantees about the memory it returns and the frame's slots. -/ theorem enterFrame_setup (body : CIL.Method) (args : List Value) (m result : Memory) (frame : Frame) (h : enterFrame body args m = .ok (frame, result)) : - FrameSetup frame.activation m result (frame.locals ++ frame.arguments.map Prod.snd) - frame.owned := by + FrameSetup frame.activation m result frame.locals frame.owned := by simp only [enterFrame] at h cases hl : makeSelectedLocals m.nextIdentity body.opaqueVectorLocals body.localKinds body.locals m <;> @@ -819,26 +774,14 @@ theorem enterFrame_setup (body : CIL.Method) (args : List Value) (m result : Mem · cases h · rename_i locals obtain ⟨slots, owned, memory⟩ := locals - cases ha : makeArgumentHomes m.nextIdentity body.vectorArgs args memory <;> - simp only [ha, Pure.pure, Except.pure] at h + cases hp : makeParameterHomes m.nextIdentity body.parameterHomes args memory <;> + simp only [hp, Pure.pure, Except.pure] at h · cases h - · rename_i homes - obtain ⟨arguments, other, final⟩ := homes - cases hp : makeParameterHomes m.nextIdentity body.parameterHomes args final <;> - simp only [hp] at h - · cases h - · rename_i parameters - obtain ⟨parameters, ids, final'⟩ := parameters - cases h - apply ((makeSelectedLocals_setup _ _ _ _ _ _ _ _ hl).append - (makeArgumentHomes_setup _ _ _ _ _ _ _ ha) |>.append - (makeParameterHomes_setup _ _ _ _ _ _ _ hp)).select - intro slot member - simp only [List.mem_append] at member ⊢ - rcases member with (left | right) | middle - · exact Or.inl (Or.inl left) - · exact Or.inr right - · exact Or.inl (Or.inr middle) + · rename_i parameters + obtain ⟨parameters, ids, final⟩ := parameters + cases h + exact (makeSelectedLocals_setup _ _ _ _ _ _ _ _ hl).append + (makeParameterHomes_setup _ _ _ _ _ _ _ hp) /-- Entering a frame charges nothing to the object heap budget, so a caller's grant reaches the callee's first `newobj` whole. -/ @@ -856,7 +799,6 @@ theorem enterFrame_objectsKept {body : CIL.Method} {args : List Value} {m result #print axioms enterFrame_objectsKept #print axioms makeLocal_setup #print axioms makeLocals_setup -#print axioms makeArgumentHomes_setup #print axioms makeParameterHomes_setup #print axioms enterFrame_setup @@ -876,13 +818,6 @@ theorem makeLocals_preserves_wellFormed (m result : Memory) (activation : Nat) #print axioms makeLocal_preserves_wellFormed #print axioms makeLocals_preserves_wellFormed -theorem makeArgumentHomes_preserves_wellFormed (m result : Memory) (activation : Nat) - (indices : List Nat) (args : List Value) (homes : List (Nat × LocalSlot)) - (owned : List AllocationId) (hm : m.WellFormed) - (h : makeArgumentHomes activation indices args m = .ok (homes, owned, result)) : - result.WellFormed := by - exact (makeArgumentHomes_setup _ _ _ _ _ _ _ h).wellFormed hm - theorem makeParameterHomes_preserves_wellFormed (m result : Memory) (activation : Nat) (parameters : List (Nat × CIL.LocalKind)) (args : List Value) (homes : List (Nat × LocalSlot)) (owned : List AllocationId) (hm : m.WellFormed) @@ -895,7 +830,6 @@ theorem enterFrame_preserves_wellFormed (body : CIL.Method) (args : List Value) (h : enterFrame body args m = .ok (frame, result)) : result.WellFormed := by exact (enterFrame_setup _ _ _ _ _ h).wellFormed hm -#print axioms makeArgumentHomes_preserves_wellFormed #print axioms enterFrame_preserves_wellFormed end CIL.Safety diff --git a/CIL/Safety/FrameStaticWorld.lean b/CIL/Safety/FrameStaticWorld.lean index a4de570d3..eb9b5f71c 100644 --- a/CIL/Safety/FrameStaticWorld.lean +++ b/CIL/Safety/FrameStaticWorld.lean @@ -64,14 +64,6 @@ theorem makeLocals_preserves_static_world (descriptors : List CIL.StaticDescript StaticWorldValid descriptors result := by exact (makeLocals_setup _ _ _ _ _ _ _ h).staticWorld descriptors hm world -theorem makeArgumentHomes_preserves_static_world (descriptors : List CIL.StaticDescriptor) - (m result : Memory) (activation : Nat) (indices : List Nat) (args : List Value) - (homes : List (Nat × LocalSlot)) (owned : List AllocationId) (hm : m.WellFormed) - (world : StaticWorldValid descriptors m) - (h : makeArgumentHomes activation indices args m = .ok (homes, owned, result)) : - StaticWorldValid descriptors result := by - exact (makeArgumentHomes_setup _ _ _ _ _ _ _ h).staticWorld descriptors hm world - theorem makeParameterHomes_preserves_static_world (descriptors : List CIL.StaticDescriptor) (m result : Memory) (activation : Nat) (parameters : List (Nat × CIL.LocalKind)) (args : List Value) (homes : List (Nat × LocalSlot)) (owned : List AllocationId) diff --git a/CIL/Safety/Frames.lean b/CIL/Safety/Frames.lean index 396dc2361..9bb545725 100644 --- a/CIL/Safety/Frames.lean +++ b/CIL/Safety/Frames.lean @@ -4,7 +4,7 @@ import CIL.Safety.InstructionMemory # Activation frames How the checked interpreter (`CIL/Safety/Execution.lean`) lays out a method activation. -Each numeric local, vector argument and written parameter gets its own frame-owned +Each numeric local and written parameter gets its own frame-owned allocation of bytes, so unsafe code can address it; managed references live in root slots that hold a reference, not bytes. `enterFrame` builds a frame on call and `leaveFrame` expires its allocations on return. The `Frame*` lemma files describe these functions. @@ -53,13 +53,12 @@ inductive LocalSlot where | object (value : Option ManagedReference) deriving DecidableEq, Repr -/-- A method activation: its identity, local slots, the allocations it owns (expired by - `leaveFrame`), and the private homes of vector arguments, keyed by argument index. -/ +/-- A method activation: its identity, local slots and the allocations it owns (expired by + `leaveFrame`). -/ structure Frame where activation : Nat locals : List LocalSlot owned : List AllocationId - arguments : List (Nat × LocalSlot) := [] /-- Rounded bytes reserved by this activation's dynamic stack allocations. -/ stackBytes : Nat := 0 deriving Repr @@ -133,7 +132,7 @@ def storeLocal (m : Memory) (slot : LocalSlot) (value : Value) : if table.signature != signature then .error .invalidState else return (.methodTable signature (some table), m) | .methodTable signature _, .object .null => - return (.methodTable signature (some ⟨signature, none, none⟩), m) + return (.methodTable signature (some { signature, targets := none, entryCount := 0 }), m) | .object _, .object reference => let _ ← formObject m reference return (.object (some reference), m) @@ -218,15 +217,16 @@ def loadLocal (m : Memory) (slot : LocalSlot) : ExecutionResult Value := do | .reference | .span .. | .array _ | .methodPointer _ | .methodTable _ | .object => .error .invalidState -/-- The address of a local's byte home. Root and opaque vector slots have no bytes and - cannot be addressed. -/ -def localAddress (m : Memory) (slot : LocalSlot) : ExecutionResult Value := +/-- What taking the address of local `index`, held in `slot`, pushes: the address of its byte + home, or for a span local its home, which reads nothing until a call passes it. Root, + array, opaque vector, callable and object slots have no address the model forms. -/ +def localAddress (m : Memory) (index : Nat) (slot : LocalSlot) : ExecutionResult Value := match slot with | .bytes _ reference => formValue m (.address reference) + | .span .. => .ok (.spanHome index) | .root _ | .readOnlyRoot _ => .error (.unsupported "Address of managed-reference local") | .array .. => .error (.unsupported "Address of array root local") | .opaque128 _ => .error (.unsupported "Address of opaque vector local") - | .span .. => .error (.unsupported "Address of span local outside a validated receiver") | .methodPointer .. | .methodTable .. => .error (.unsupported "Address of callable provenance root") | .object _ => .error (.unsupported "Address of object reference local") @@ -259,7 +259,8 @@ def makeLocal (activation : Nat) (kind : CIL.LocalKind) (initial : CIL.Value) (m | .unmodeled => .ok (.methodPointer signature none, [], m) | _ => .error .invalidState | .methodTable signature => match initial with - | .nullRef => .ok (.methodTable signature (some ⟨signature, none, none⟩), [], m) + | .nullRef => + .ok (.methodTable signature (some { signature, targets := none, entryCount := 0 }), [], m) | .unmodeled => .ok (.methodTable signature none, [], m) | _ => .error .invalidState | .array element => match initial with @@ -340,27 +341,6 @@ def makeSelectedLocals (activation : Nat) (indices : List Nat) (kinds : List CIL makeLocals activation kinds initializers m := rfl #print axioms makeSelectedLocals_nil -/-- Aggregate argument instructions operate on a private snapshot home, never - directly on the caller's by-value source. Their extracted indices identify - the 256-bit vector representation supported by those CIL operations. -/ -def makeArgumentHomes (activation : Nat) (indices : List Nat) (args : List Value) - (m : Memory) : ExecutionResult (List (Nat × LocalSlot) × List AllocationId × Memory) := do - match indices with - | [] => return ([], [], m) - | index :: rest => - if (args[index]?.map Value.hasUnspecified).getD false then - .error (.unsupported "Argument home observes unspecified vector bits") else - let some (.scalar (.v256 bits)) := args[index]? | .error .invalidState - let (reference, memory) ← allocateHome m activation (localWidth .vector256) - let (slot, memory) ← storeLocal memory (.bytes .vector256 reference) (.scalar (.v256 bits)) - let (homes, owned, memory) ← makeArgumentHomes activation rest args memory - return ((index, slot) :: homes, reference.allocation :: owned, memory) - -/-- The private home of vector argument `index`. -/ -def argumentHome (frame : Frame) (index : Nat) : ExecutionResult LocalSlot := do - let some (_, slot) := frame.arguments.find? (fun home => home.1 == index) | .error .invalidState - return slot - /-- Materialize only parameters that are assigned or addressed. By-value homes are private to the activation; byref parameters retain their reference value. -/ def makeParameterHomes (activation : Nat) (parameters : List (Nat × CIL.LocalKind)) @@ -375,18 +355,16 @@ def makeParameterHomes (activation : Nat) (parameters : List (Nat × CIL.LocalKi let (homes, other, memory) ← makeParameterHomes activation rest args memory return ((index, slot) :: homes, owned ++ other, memory) -/-- Sets up a callee activation under a fresh identity: its locals, vector argument homes - and parameter homes. Parameter homes are appended to the locals. -/ +/-- Sets up a callee activation under a fresh identity: its locals and parameter homes. + Parameter homes are appended to the locals. -/ def enterFrame (body : CIL.Method) (args : List Value) (m : Memory) : ExecutionResult (Frame × Memory) := do let activation := m.nextIdentity let (locals, owned, memory) ← makeSelectedLocals activation body.opaqueVectorLocals body.localKinds body.locals m - let (arguments, argumentIds, memory) ← makeArgumentHomes activation body.vectorArgs args memory let (parameters, parameterIds, memory) ← makeParameterHomes activation body.parameterHomes args memory - return (⟨activation, locals ++ parameters.map Prod.snd, (owned ++ argumentIds) ++ parameterIds, - arguments, 0⟩, memory) + return (⟨activation, locals ++ parameters.map Prod.snd, owned ++ parameterIds, 0⟩, memory) /-- Ends an activation by expiring every allocation it owns. -/ def leaveFrame (frame : Frame) (m : Memory) : Memory := @@ -398,12 +376,10 @@ theorem enterFrame_of_no_opaque (body : CIL.Method) (args : List Value) (m : Mem enterFrame body args m = (do let activation := m.nextIdentity let (locals, owned, memory) ← makeLocals activation body.localKinds body.locals m - let (arguments, argumentIds, memory) ← - makeArgumentHomes activation body.vectorArgs args memory let (parameters, parameterIds, memory) ← makeParameterHomes activation body.parameterHomes args memory - return (⟨activation, locals ++ parameters.map Prod.snd, - (owned ++ argumentIds) ++ parameterIds, arguments, 0⟩, memory)) := by + return (⟨activation, locals ++ parameters.map Prod.snd, owned ++ parameterIds, 0⟩, + memory)) := by simp only [enterFrame, noOpaque, makeSelectedLocals_nil] #print axioms enterFrame_of_no_opaque diff --git a/CIL/Safety/FuelLemmas.lean b/CIL/Safety/FuelLemmas.lean index 2494df5a2..9eb6bf189 100644 --- a/CIL/Safety/FuelLemmas.lean +++ b/CIL/Safety/FuelLemmas.lean @@ -1,178 +1,268 @@ import CIL.Safety.Execution /-! -# More fuel never changes a settled outcome +# More fuel never changes a stable outcome -Fuel only bounds how long the checked interpreter may run. Once a run succeeds or fails with -any fault other than `fuelExhausted`, giving it more fuel yields the same outcome -(`run_mono`, `invoke_mono`). Consequently a semantic fault found at one fuel budget refutes -success at every budget (`invoke_fault_refutes_success`). +Fuel only bounds how long the checked interpreter may run. Once a run succeeds or fails with any +fault other than `fuelExhausted`, giving it more fuel yields the same outcome (`run_mono`, +`invoke_mono`). Consequently a definite fault found at one fuel budget refutes success at every +budget (`invoke_fault_refutes_success`). Fuel exhaustion and `unsupported` faults are not +definite: the first stops a run early and the second marks an operation the model does not cover, +so neither is evidence about the method and no refutation lemma takes one (`Settled`). +`Counterexample` is the evidence a contract refutation carries: a settled outcome at some budget, +or exhaustion at every budget (`Diverges`). -/ namespace CIL.Safety +/-- Whether a fault is an outcome of the method itself. Running out of fuel and an operation the + model does not cover (`unsupported`) are not: a longer or a more complete run could go on. -/ +@[simp] def ExecutionFault.definite : ExecutionFault → Bool + | .arithmetic _ => true + | .memory .. => true + | .invalidState => true + | .unsupported .. => false + | .thrown _ => true + | .fuelExhausted => false + | .stackAllocationFailure .. => true + | .allocationFailure .. => true + /-- An outcome that more fuel cannot change: a success, or any fault except running out of - fuel. -/ -def Settled (outcome : Except LocatedFault α) : Prop := + fuel. An `unsupported` fault is stable but not settled. -/ +def Stable (outcome : Except LocatedFault α) : Prop := match outcome with | .ok _ => True | .error fault => fault.fault ≠ .fuelExhausted +/-- An outcome that stands as evidence about the method: a success, or a definite fault. Every + refutation lemma takes it, so none of them refutes anything from a run that reaches an + `unsupported` operation. -/ +def Settled (outcome : Except LocatedFault α) : Prop := + match outcome with + | .ok _ => True + | .error fault => fault.fault.definite ≠ false + +instance (outcome : Except LocatedFault α) : Decidable (Settled outcome) := + match outcome with + | .ok _ => isTrue trivial + | .error fault => inferInstanceAs (Decidable (fault.fault.definite ≠ false)) + +/-- A settled outcome is stable: a definite fault is never fuel exhaustion. -/ +theorem Settled.stable {outcome : Except LocatedFault α} (settled : Settled outcome) : + Stable outcome := by + cases outcome with + | ok _ => trivial + | error fault => exact fun exhausted => settled (by rw [exhausted]; rfl) + section -- Every `run` case unfolds through these monad steps. attribute [local simp] Bind.bind Except.bind Except.mapError -/-- Adding fuel to a run with a settled outcome gives the same outcome. -/ +/-- Adding fuel to a run with a stable outcome gives the same outcome. -/ theorem run_mono (program : CIL.Program) (fuel extra method pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (memory : Memory) (outcome : Except LocatedFault (Memory × List Value)) - (ready : Settled outcome) + (ready : Stable outcome) (h : run program fuel method pc args frame stack memory = outcome) : run program (fuel + extra) method pc args frame stack memory = outcome := by induction fuel generalizing method pc args frame stack memory outcome with | zero => - simp only [run] at h + simp only [run_zero] at h subst outcome - simp [Settled] at ready + simp [Stable] at ready | succ fuel ih => cases hb : program[method]? with - | none => simpa [Nat.succ_add, run, hb] using h + | none => simpa [Nat.succ_add, run_zero, run_succ, hb] using h | some body => cases ho : body.code[pc]? with - | none => simpa [Nat.succ_add, run, hb, ho] using h + | none => simpa [Nat.succ_add, run_zero, run_succ, hb, ho] using h | some op => cases hs : step body op pc args frame stack memory with | error fault => - simpa [Nat.succ_add, run, hb, ho, hs] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs] using h | ok action => cases action with | next target values nextFrame updated => - simp [run, hb, ho, hs] at h - simpa [Nat.succ_add, run, hb, ho, hs] using + simp [run_succ, hb, ho, hs] at h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs] using ih method target args nextFrame values updated outcome ready h | returned values updated => - simpa [Nat.succ_add, run, hb, ho, hs] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs] using h | tailCall callee arguments updated => cases hc : program[callee]? with - | none => simpa [Nat.succ_add, run, hb, ho, hs, hc] using h + | none => simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc] using h | some child => cases returns : child.returnsValue != body.returnsValue with - | true => simpa [Nat.succ_add, run, hb, ho, hs, hc, returns] using h + | true => simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, returns] using h | false => cases check : arguments.mapM (checkedValue (leaveFrame frame updated)) with | error fault => - simpa [Nat.succ_add, run, hb, ho, hs, hc, returns, check] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, returns, check] using h | ok checked => cases enter : enterFrame child checked (leaveFrame frame updated) with | error fault => - simpa [Nat.succ_add, run, hb, ho, hs, hc, returns, check, enter] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, returns, check, + enter] using h | ok entered => obtain ⟨childFrame, childMemory⟩ := entered - simp [run, hb, ho, hs, hc, returns, check, enter] at h - simpa [Nat.succ_add, run, hb, ho, hs, hc, returns, check, enter] using + simp [run_succ, hb, ho, hs, hc, returns, check, enter] at h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, returns, check, + enter] using ih callee 0 checked childFrame [] childMemory outcome ready h | call callee arguments rest updated => cases hc : program[callee]? with | none => - simpa [Nat.succ_add, run, hb, ho, hs, hc] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc] using h | some child => cases he : enterFrame child arguments updated with | error fault => - simpa [Nat.succ_add, run, hb, ho, hs, hc, he] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, he] using h | ok entered => obtain ⟨childFrame, childMemory⟩ := entered cases hr : run program fuel callee 0 arguments childFrame [] childMemory with | error fault => - simp [run, hb, ho, hs, hc, he, hr] at h + simp [run_succ, hb, ho, hs, hc, he, hr] at h have settled : - Settled (.error fault : Except LocatedFault (Memory × List Value)) := by + Stable (.error fault : Except LocatedFault (Memory × List Value)) := by simpa [← h] using ready have more := ih callee 0 arguments childFrame [] childMemory (.error fault) settled hr - simpa [Nat.succ_add, run, hb, ho, hs, hc, he, more] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, he, more] using h | ok childResult => obtain ⟨final, values⟩ := childResult have more := ih callee 0 arguments childFrame [] childMemory (.ok (final, values)) trivial hr - simp [run, hb, ho, hs, hc, he, hr] at h + simp [run_succ, hb, ho, hs, hc, he, hr] at h have parent := ih method (pc + 1) args frame (values ++ rest) final outcome ready h - simpa [Nat.succ_add, run, hb, ho, hs, hc, he, more] using parent + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, he, more] using parent | construct callee arguments rest parentFrame temporary kind updated => cases hc : program[callee]? with | none => - simpa [Nat.succ_add, run, hb, ho, hs, hc] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc] using h | some child => cases he : enterFrame child arguments updated with | error fault => - simpa [Nat.succ_add, run, hb, ho, hs, hc, he] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, he] using h | ok entered => obtain ⟨childFrame, childMemory⟩ := entered cases hr : run program fuel callee 0 arguments childFrame [] childMemory with | error fault => - simp [run, hb, ho, hs, hc, he, hr] at h + simp [run_succ, hb, ho, hs, hc, he, hr] at h have settled : - Settled (.error fault : Except LocatedFault (Memory × List Value)) := by + Stable (.error fault : Except LocatedFault (Memory × List Value)) := by simpa [← h] using ready have more := ih callee 0 arguments childFrame [] childMemory (.error fault) settled hr - simpa [Nat.succ_add, run, hb, ho, hs, hc, he, more] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, he, more] using h | ok childResult => obtain ⟨final, values⟩ := childResult have more := ih callee 0 arguments childFrame [] childMemory (.ok (final, values)) trivial hr cases values with | cons value tail => - simpa [Nat.succ_add, run, hb, ho, hs, hc, he, hr, more] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, he, hr, more] using h | nil => cases hv : loadLocal final (.bytes kind temporary) with | error fault => - simpa [Nat.succ_add, run, hb, ho, hs, hc, he, hr, more, hv] using h + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, he, hr, more, + hv] using h | ok value => - simp [run, hb, ho, hs, hc, he, hr, hv] at h + simp [run_succ, hb, ho, hs, hc, he, hr, hv] at h have parent := ih method (pc + 1) args parentFrame (value :: rest) final outcome ready h - simpa [Nat.succ_add, run, hb, ho, hs, hc, he, more, hv] using parent + simpa [Nat.succ_add, run_zero, run_succ, hb, ho, hs, hc, he, more, + hv] using parent #print axioms run_mono end theorem invoke_mono (program : CIL.Program) (fuel extra method : Nat) (args : List Value) (memory : Memory) (outcome : Except LocatedFault (Memory × List Value)) - (ready : Settled outcome) (h : invoke program fuel method args memory = outcome) : + (ready : Stable outcome) (h : invoke program fuel method args memory = outcome) : invoke program (fuel + extra) method args memory = outcome := by cases hb : program[method]? with - | none => simpa [invoke, hb] using h + | none => simpa [invoke_eq, hb] using h | some body => cases ha : args.mapM (checkedValue memory) with - | error fault => simpa [invoke, hb, ha, Bind.bind, Except.bind, Except.mapError] using h + | error fault => simpa [invoke_eq, hb, ha, Bind.bind, Except.bind, Except.mapError] using h | ok arguments => cases he : enterFrame body arguments memory with - | error fault => simpa [invoke, hb, ha, he, Bind.bind, Except.bind, Except.mapError] using h + | error fault => simpa [invoke_eq, hb, ha, he, Bind.bind, Except.bind, + Except.mapError] using h | ok entered => obtain ⟨frame, updated⟩ := entered - simp [invoke, hb, ha, he, Bind.bind, Except.bind, Except.mapError] at h - simpa [invoke, hb, ha, he, Bind.bind, Except.bind, Except.mapError] using + simp [invoke_eq, hb, ha, he, Bind.bind, Except.bind, Except.mapError] at h + simpa [invoke_eq, hb, ha, he, Bind.bind, Except.bind, Except.mapError] using run_mono program fuel extra method 0 arguments frame [] updated outcome ready h -/-- A classified semantic fault refutes successful execution for every fuel, - not merely the concrete budget used to exhibit the fault. -/ +/-- A definite fault refutes successful execution for every fuel, not merely the concrete + budget used to exhibit the fault. -/ theorem invoke_fault_refutes_success (program : CIL.Program) (observed method : Nat) (args : List Value) (memory : Memory) (fault : LocatedFault) - (semantic : fault.fault ≠ .fuelExhausted) + (definite : fault.fault.definite ≠ false) (witness : invoke program observed method args memory = .error fault) (fuel : Nat) (result : Memory × List Value) : invoke program fuel method args memory ≠ .ok result := by intro success have failure := - invoke_mono program observed fuel method args memory (.error fault) semantic witness + invoke_mono program observed fuel method args memory (.error fault) (Settled.stable definite) + witness have returned := invoke_mono program fuel observed method args memory (.ok result) trivial success rw [Nat.add_comm] at returned rw [failure] at returned cases returned +/-- Two stable outcomes of the same invocation agree at any fuel budgets. This includes the + interpreter stopping at an unsupported operation. -/ +theorem invoke_unique {program : CIL.Program} {method left right : Nat} {args : List Value} + {memory : Memory} {first second : Except LocatedFault (Memory × List Value)} + (firstStable : Stable first) (secondStable : Stable second) + (one : invoke program left method args memory = first) + (other : invoke program right method args memory = second) : + first = second := by + have up := invoke_mono program left right method args memory first firstStable one + have down := invoke_mono program right left method args memory second secondStable other + rw [Nat.add_comm] at down + exact up.symm.trans down + +/-- Every fuel budget ends the run in fuel exhaustion: the method never terminates. One exhausted + budget is no evidence of this, and a run that reaches an `unsupported` operation does not + diverge, since that fault stays at every larger budget. -/ +def Diverges (program : CIL.Program) (method : Nat) (args : List Value) (memory : Memory) : + Prop := + ∀ fuel, ∃ fault, invoke program fuel method args memory = .error fault ∧ + fault.fault = .fuelExhausted + +/-- Evidence that the run of `method` on `args` from `memory` ends as `violates` describes: a + settled outcome at some fuel that `violates` holds of, or divergence. A run that reaches an + operation the model does not cover, or one exhausted budget, is neither. -/ +def Counterexample (program : CIL.Program) (method : Nat) (args : List Value) (memory : Memory) + (violates : Except LocatedFault (Memory × List Value) → Prop) : Prop := + (∃ fuel outcome, invoke program fuel method args memory = outcome ∧ Settled outcome ∧ + violates outcome) ∨ Diverges program method args memory + +/-- Every settled outcome of a counterexample's run is one `violates` holds of. -/ +theorem Counterexample.violates {program : CIL.Program} {method fuel : Nat} + {args : List Value} {memory : Memory} + {violates : Except LocatedFault (Memory × List Value) → Prop} + {outcome : Except LocatedFault (Memory × List Value)} + (counter : Counterexample program method args memory violates) + (settled : Settled outcome) (ran : invoke program fuel method args memory = outcome) : + violates outcome := by + rcases counter with ⟨observed, witness, invoked, witnessSettled, bad⟩ | diverges + · rw [← invoke_unique witnessSettled.stable settled.stable invoked ran] + exact bad + · obtain ⟨fault, exhausted, kind⟩ := diverges fuel + rw [ran] at exhausted + subst exhausted + simp [Settled, kind] at settled + +#print axioms Settled.stable #print axioms invoke_mono #print axioms invoke_fault_refutes_success +#print axioms invoke_unique +#print axioms Counterexample.violates end CIL.Safety diff --git a/CIL/Safety/GuardNormalization.lean b/CIL/Safety/GuardNormalization.lean index f31686c12..14085c4c0 100644 --- a/CIL/Safety/GuardNormalization.lean +++ b/CIL/Safety/GuardNormalization.lean @@ -25,6 +25,10 @@ namespace CIL.Safety @[cil_guard] theorem guard_span (reference : ManagedReference) (length : Nat) : Value.hasUnspecified (.span reference length) = false := rfl +/-- A span local's home carries no unspecified vector bits. -/ +@[cil_guard] theorem guard_spanHome (slot : Nat) : + Value.hasUnspecified (.spanHome slot) = false := rfl +#print axioms guard_spanHome @[cil_guard] theorem guard_snapshot (shape : CIL.SnapshotShape) (bytes : List (BitVec 8 × Bool)) : CIL.Value.hasUnspecified (.snapshot shape bytes) = false := rfl @@ -53,6 +57,13 @@ namespace CIL.Safety @[cil_guard] theorem guard_nullRef : CIL.Value.hasUnspecified .nullRef = false := rfl @[cil_guard] theorem guard_unmodeled : CIL.Value.hasUnspecified .unmodeled = false := rfl +-- A memory operation that reads no span view takes its operands from the stack unchanged. +attribute [cil_guard] viewOperandCount +/-- With no view operands to read, the stack is the operation's operand list. -/ +@[cil_guard] theorem viewOperands_zero (frame : Frame) (memory : Memory) (stack : List Value) : + viewOperands frame memory 0 stack = .ok stack := rfl +#print axioms viewOperands_zero + -- The guard inspects only the top `count` stack operands. @[cil_guard] theorem guard_operands_zero (stack : List Value) : unspecifiedOperands 0 stack = false := rfl @@ -63,57 +74,122 @@ namespace CIL.Safety (value.hasUnspecified || unspecifiedOperands count rest) := rfl -- How many stack operands each instruction observes. -@[cil_guard] theorem guard_nativeBinary (op : CIL.Op) : observerCount (.nativeBinary op) = 2 := rfl -@[cil_guard] theorem guard_checkedConvert (width : Nat) (signed unsigned : Bool) : - observerCount (.checkedConvert width signed unsigned) = 1 := rfl -@[cil_guard] theorem guard_checkedArithmetic (kind : CIL.CheckedBinaryKind) (signed : Bool) : - observerCount (.checkedArithmetic kind signed) = 2 := rfl -@[cil_guard] theorem guard_divide (signed remainder : Bool) : - observerCount (.divide signed remainder) = 2 := rfl -@[cil_guard] theorem guard_arg (index : Nat) (narrow : Option CIL.NarrowKind) : - observerCount (.arg index narrow) = 0 := rfl -@[cil_guard] theorem guard_call (callee argc : Nat) : - observerCount (.call callee argc) = argc := rfl -@[cil_guard] theorem guard_newVector256 (callee argc : Nat) : - observerCount (.newVector256 callee argc) = argc := rfl -@[cil_guard] theorem guard_newAggregate (callee argc : Nat) (size : Fin (2^64)) : - observerCount (.newAggregate callee argc size) = argc := rfl -@[cil_guard] theorem guard_memory (op : CIL.MemoryOp) : - observerCount (.memory op) = op.argumentCount := rfl -@[cil_guard] theorem guard_throw (exception : String) : observerCount (.throw exception) = 0 := rfl -@[cil_guard] theorem guard_unsupported (description : String) : - observerCount (.unsupported description) = 0 := rfl -@[cil_guard] theorem guard_ret : observerCount (.ret none) = 1 := rfl -@[cil_guard] theorem guard_ret_narrow (kind : CIL.NarrowKind) : - observerCount (.ret (some kind)) = 1 := rfl -@[cil_guard] theorem guard_loadAggregate (size : Nat) : - observerCount (.loadAggregate size) = 1 := rfl -@[cil_guard] theorem guard_storeAggregate (size : Nat) : - observerCount (.storeAggregate size) = 2 := rfl -@[cil_guard] theorem guard_copyObject (size : Nat) : observerCount (.copyObject size) = 2 := rfl -@[cil_guard] theorem guard_initAggregate (size : Nat) : - observerCount (.initAggregate size) = 1 := rfl -@[cil_guard] theorem guard_loadScalar (width : Nat) (signed : Bool) : - observerCount (.loadScalar width signed) = 1 := rfl -@[cil_guard] theorem guard_storeScalar (width : Nat) : observerCount (.storeScalar width) = 2 := rfl -@[cil_guard] theorem guard_fieldAt (offset width : Nat) (signed : Bool) : - observerCount (.fieldAt offset width signed) = 1 := rfl -@[cil_guard] theorem guard_fieldAddress (offset : Nat) : - observerCount (.fieldAddress offset) = 1 := rfl -@[cil_guard] theorem guard_setFieldAt (offset width : Nat) : - observerCount (.setFieldAt offset width) = 2 := rfl -@[cil_guard] theorem guard_aggregateFieldAt (offset size : Nat) : - observerCount (.aggregateFieldAt offset size) = 1 := rfl -@[cil_guard] theorem guard_setAggregateFieldAt (offset size : Nat) : - observerCount (.setAggregateFieldAt offset size) = 2 := rfl -@[cil_guard] theorem guard_lane (index : Fin 4) : observerCount (.lane index) = 1 := rfl -@[cil_guard] theorem guard_laneAddress (index : Fin 4) : - observerCount (.laneAddress index) = 1 := rfl -@[cil_guard] theorem guard_setLane (index : Fin 4) : observerCount (.setLane index) = 2 := rfl -@[cil_guard] theorem guard_load64 : observerCount .load64 = 1 := rfl -@[cil_guard] theorem guard_store64 : observerCount .store64 = 2 := rfl -@[cil_guard] theorem guard_skipInit : observerCount .skipInit = 1 := rfl -@[cil_guard] theorem guard_asRef : observerCount .asRef = 1 := rfl +/-- A signed division observes both of its operands. -/ +@[cil_guard] theorem guard_div : observerCount .div = 2 := rfl +#print axioms guard_div +/-- An unsigned division observes both of its operands. -/ +@[cil_guard] theorem guard_div_un : observerCount .div_un = 2 := rfl +#print axioms guard_div_un +/-- A signed remainder observes both of its operands. -/ +@[cil_guard] theorem guard_rem : observerCount .rem = 2 := rfl +#print axioms guard_rem +/-- An unsigned remainder observes both of its operands. -/ +@[cil_guard] theorem guard_rem_un : observerCount .rem_un = 2 := rfl +#print axioms guard_rem_un +/-- A signed checked addition observes both of its operands. -/ +@[cil_guard] theorem guard_add_ovf : observerCount .add_ovf = 2 := rfl +#print axioms guard_add_ovf +/-- An unsigned checked addition observes both of its operands. -/ +@[cil_guard] theorem guard_add_ovf_un : observerCount .add_ovf_un = 2 := rfl +#print axioms guard_add_ovf_un +/-- A signed checked subtraction observes both of its operands. -/ +@[cil_guard] theorem guard_sub_ovf : observerCount .sub_ovf = 2 := rfl +#print axioms guard_sub_ovf +/-- An unsigned checked subtraction observes both of its operands. -/ +@[cil_guard] theorem guard_sub_ovf_un : observerCount .sub_ovf_un = 2 := rfl +#print axioms guard_sub_ovf_un +/-- A signed checked multiplication observes both of its operands. -/ +@[cil_guard] theorem guard_mul_ovf : observerCount .mul_ovf = 2 := rfl +#print axioms guard_mul_ovf +/-- An unsigned checked multiplication observes both of its operands. -/ +@[cil_guard] theorem guard_mul_ovf_un : observerCount .mul_ovf_un = 2 := rfl +#print axioms guard_mul_ovf_un +/-- A checked conversion of a signed operand observes its one operand. -/ +@[cil_guard] theorem guard_conv_ovf (target : CIL.ConvTarget) : + observerCount (.conv_ovf target) = 1 := rfl +#print axioms guard_conv_ovf +/-- A checked conversion of an unsigned operand observes its one operand. -/ +@[cil_guard] theorem guard_conv_ovf_un (target : CIL.ConvTarget) : + observerCount (.conv_ovf_un target) = 1 := rfl +#print axioms guard_conv_ovf_un +@[cil_guard] theorem guard_ldarg (index : Nat) : observerCount (.ldarg index) = 0 := rfl +#print axioms guard_ldarg +@[cil_guard] theorem guard_starg (index : Nat) : observerCount (.starg index) = 0 := rfl +#print axioms guard_starg +@[cil_guard] theorem guard_ldarga (index : Nat) : observerCount (.ldarga index) = 0 := rfl +#print axioms guard_ldarga +/-- A call observes no operand before dispatch; the arm of its link checks them. -/ +@[cil_guard] theorem guard_call (method : Extraction.MethodRef) (prefixes : CallPrefixes) : + observerCount (.call method prefixes) = 0 := rfl +#print axioms guard_call +/-- A linked call checks its method's arguments. -/ +@[cil_guard] theorem guard_call_linked (link : CIL.Link) (observed : Bool) : + ((some link).isSome && observed) = observed := rfl +#print axioms guard_call_linked +/-- A call without a link leaves its operands to the model it runs. -/ +@[cil_guard] theorem guard_call_unlinked (observed : Bool) : + ((none : Option CIL.Link).isSome && observed) = false := rfl +#print axioms guard_call_unlinked +@[cil_guard] theorem guard_newobj (constructor : CIL.Extraction.MethodRef) : + observerCount (.newobj constructor) = constructor.parameters.length := rfl +#print axioms guard_newobj + +open Lean Meta in +/-- The argument count of a call's method as a numeral, when a closed term fixes its parameter + list and receiver flag; a method a variable decides is left as it is. -/ +dsimproc [cil_step, cil_guard] reduceArgumentCount (CIL.Extraction.MethodRef.argumentCount _) := + fun expression => do + let_expr CIL.Extraction.MethodRef.argumentCount _ := expression | return .continue + let count ← withDefault <| whnf expression + match count.rawNatLit? <|> count.nat? with + | some value => return .done (mkNatLit value) + | none => return .continue +@[cil_guard] theorem guard_throw (exception : ExceptionType) : + observerCount (.throw exception) = 0 := rfl +@[cil_guard] theorem guard_unadmitted (opcode : Nat) : + observerCount (.unadmitted opcode) = 0 := rfl +/-- Return observes at most the first stack value, regardless of narrowing. -/ +@[cil_guard] theorem guard_ret : + observerCount .ret = 1 := rfl + +#print axioms guard_ret +/-- The cpobj guard checks the operands observed by the storage operation. -/ +@[cil_guard] theorem guard_cpobj (type : Extraction.TypeExpr) : + observerCount (.cpobj type) = 2 := rfl +#print axioms guard_cpobj +/-- The initobj guard checks the operands observed by the storage operation. -/ +@[cil_guard] theorem guard_initobj (type : Extraction.TypeExpr) : + observerCount (.initobj type) = 1 := rfl +#print axioms guard_initobj +/-- The ldobj guard checks the operands observed by the storage operation. -/ +@[cil_guard] theorem guard_ldobj (type : Extraction.TypeExpr) : + observerCount (.ldobj type) = (if Extraction.spanLocal type then 0 else 1) := rfl +#print axioms guard_ldobj +/-- The stobj guard checks the operands observed by the storage operation. -/ +@[cil_guard] theorem guard_stobj (type : Extraction.TypeExpr) : + observerCount (.stobj type) = 2 := rfl +#print axioms guard_stobj +/-- The sizeof guard checks the operands observed by the storage operation. -/ +@[cil_guard] theorem guard_sizeof (type : Extraction.TypeExpr) : + observerCount (.sizeof type) = 0 := rfl +#print axioms guard_sizeof +@[cil_guard] theorem guard_ldind (kind : CIL.NumericKind) : observerCount (.ldind kind) = 1 := rfl +@[cil_guard] theorem guard_stind (kind : CIL.NumericKind) : observerCount (.stind kind) = 2 := rfl +@[cil_guard] theorem guard_ldlen : observerCount .ldlen = 1 := rfl +@[cil_guard] theorem guard_ldelem (kind : CIL.NumericKind) : observerCount (.ldelem kind) = 2 := + rfl +@[cil_guard] theorem guard_ldelem_ref : observerCount .ldelem_ref = 2 := rfl +@[cil_guard] theorem guard_stelem (kind : CIL.NumericKind) : observerCount (.stelem kind) = 3 := + rfl +@[cil_guard] theorem guard_stelem_ref : observerCount .stelem_ref = 3 := rfl +@[cil_guard] theorem guard_ldsfld (field : CIL.Extraction.FieldRef) : + observerCount (.ldsfld field) = 0 := rfl +@[cil_guard] theorem guard_ldsflda (field : CIL.Extraction.FieldRef) : + observerCount (.ldsflda field) = 0 := rfl +@[cil_guard] theorem guard_ldtoken (operand : CIL.TokenOperand) : + observerCount (.ldtoken operand) = 0 := rfl +@[cil_guard] theorem guard_stsfld (field : CIL.Extraction.FieldRef) : + observerCount (.stsfld field) = 1 := rfl -- Operand counts of memory instructions, which `observerCount` defers to. @[cil_guard] theorem guard_count_bigMul64 (signed : Bool) : @@ -130,7 +206,6 @@ namespace CIL.Safety @[cil_guard] theorem guard_count_store128 : CIL.MemoryOp.store128.argumentCount = 2 := rfl @[cil_guard] theorem guard_count_store256 : CIL.MemoryOp.store256.argumentCount = 2 := rfl @[cil_guard] theorem guard_count_store512 : CIL.MemoryOp.store512.argumentCount = 2 := rfl -@[cil_guard] theorem guard_count_init256 : CIL.MemoryOp.init256.argumentCount = 1 := rfl @[cil_guard] theorem guard_count_vector128Load : (CIL.MemoryOp.vector128Unsafe false false).argumentCount = 1 := rfl @[cil_guard] theorem guard_count_vector128LoadAt : @@ -142,16 +217,10 @@ namespace CIL.Safety @[cil_guard] theorem guard_count_asRef : CIL.MemoryOp.asRef.argumentCount = 1 := rfl @[cil_guard] theorem guard_count_add (size : Nat) (signed : Bool) : (CIL.MemoryOp.add size signed).argumentCount = 2 := rfl -@[cil_guard] theorem guard_count_bitcast256 : CIL.MemoryOp.bitcast256.argumentCount = 1 := rfl @[cil_guard] theorem guard_count_bitcastByteBool : CIL.MemoryOp.bitcastByteBool.argumentCount = 1 := rfl @[cil_guard] theorem guard_count_bitcastValue (source target : CIL.SnapshotShape) : (CIL.MemoryOp.bitcastValue source target).argumentCount = 1 := rfl -@[cil_guard] theorem guard_count_skipInit : CIL.MemoryOp.skipInit.argumentCount = 1 := rfl -@[cil_guard] theorem guard_count_staticAddress (bytes : List (BitVec 8)) : - (CIL.MemoryOp.staticAddress bytes).argumentCount = 0 := rfl -@[cil_guard] theorem guard_count_staticValue (bytes : List (BitVec 8)) : - (CIL.MemoryOp.staticValue bytes).argumentCount = 0 := rfl @[cil_guard] theorem guard_count_createSpan (width : Nat) : (CIL.MemoryOp.createSpan width).argumentCount = 1 := rfl @[cil_guard] theorem guard_count_spanCreate : CIL.MemoryOp.spanCreate.argumentCount = 2 := rfl @@ -159,6 +228,39 @@ namespace CIL.Safety @[cil_guard] theorem guard_count_spanItem (width : Nat) : (CIL.MemoryOp.spanItem width).argumentCount = 2 := rfl @[cil_guard] theorem guard_count_spanLength : CIL.MemoryOp.spanLength.argumentCount = 1 := rfl +-- The remaining span and view operations, whose counts `viewOperandCount` also reads. +@[cil_guard] theorem guard_count_observeView (observation : CIL.ViewObservation) : + (CIL.MemoryOp.observeView observation).argumentCount = 1 := rfl +@[cil_guard] theorem guard_count_spanReinterpret (source target : Nat) (writable : Bool) : + (CIL.MemoryOp.spanReinterpret source target writable).argumentCount = 1 := rfl +@[cil_guard] theorem guard_count_spanCopy (width : Nat) (tryCopy : Bool) : + (CIL.MemoryOp.spanCopy width tryCopy).argumentCount = 2 := rfl +@[cil_guard] theorem guard_count_spanClear (width : Nat) : + (CIL.MemoryOp.spanFill width true).argumentCount = 1 := rfl +@[cil_guard] theorem guard_count_spanFill (width : Nat) : + (CIL.MemoryOp.spanFill width false).argumentCount = 2 := rfl +@[cil_guard] theorem guard_count_arrayFill (element : CIL.ArrayElement) : + (CIL.MemoryOp.arrayFill element false).argumentCount = 2 := rfl +@[cil_guard] theorem guard_count_arrayFillCounted (element : CIL.ArrayElement) : + (CIL.MemoryOp.arrayFill element true).argumentCount = 4 := rfl +@[cil_guard] theorem guard_count_spanEmpty (width : Nat) (writable : Bool) : + (CIL.MemoryOp.spanEmpty width writable).argumentCount = 0 := rfl +@[cil_guard] theorem guard_count_spanFromReference (width : Nat) (writable : Bool) : + (CIL.MemoryOp.spanFromReference width writable).argumentCount = 2 := rfl +@[cil_guard] theorem guard_count_spanSingleReference (width : Nat) (writable : Bool) : + (CIL.MemoryOp.spanSingleReference width writable).argumentCount = 1 := rfl +@[cil_guard] theorem guard_count_spanSlice : (CIL.MemoryOp.spanSlice false).argumentCount = 2 := + rfl +@[cil_guard] theorem guard_count_spanSliceCount : + (CIL.MemoryOp.spanSlice true).argumentCount = 3 := rfl +@[cil_guard] theorem guard_count_spanReadOnly : CIL.MemoryOp.spanReadOnly.argumentCount = 1 := + rfl +@[cil_guard] theorem guard_count_arraySpan (element : CIL.ArrayElement) (writable : Bool) : + (CIL.MemoryOp.arraySpan element writable .whole).argumentCount = 1 := rfl +@[cil_guard] theorem guard_count_arraySpanFrom (element : CIL.ArrayElement) (writable : Bool) : + (CIL.MemoryOp.arraySpan element writable .fromStart).argumentCount = 2 := rfl +@[cil_guard] theorem guard_count_arraySpanCounted (element : CIL.ArrayElement) (writable : Bool) : + (CIL.MemoryOp.arraySpan element writable .counted).argumentCount = 3 := rfl @[cil_guard] theorem guard_count_nullRef : CIL.MemoryOp.nullRef.argumentCount = 0 := rfl @[cil_guard] theorem guard_count_isNullRef : CIL.MemoryOp.isNullRef.argumentCount = 1 := rfl diff --git a/CIL/Safety/HeapObjects.lean b/CIL/Safety/HeapObjects.lean index eb01e41ea..66a1afb5e 100644 --- a/CIL/Safety/HeapObjects.lean +++ b/CIL/Safety/HeapObjects.lean @@ -7,7 +7,7 @@ Class instances on the checked heap. An object reference is null or offset 0 of allocation whose kind records the object's runtime type (`formObject`). It carries no byte position, so code can neither observe nor forge one, and since allocation identities are never reused, reference equality compares identities alone. `CIL/Safety/Execution.lean` uses these -observations for `ceq`, `beq`, `bne`, `brtrue`, `brfalse` and `cgt.un` against null, and +observations for `ceq`, `beq`, `bne.un`, `brtrue`, `brfalse` and `cgt.un` against null, and `allocateObject` for `newobj`, which charges each instance to `Memory.heapBudget`. -/ @@ -90,7 +90,7 @@ def objectTest (memory : Memory) (type : CIL.Extraction.TypeExpr) (throwing : Bo | .address object => let runtime ← runtimeTypeOf memory object if type ∈ runtime.lineage then pure reference - else if throwing then .error (.thrown "System.InvalidCastException") + else if throwing then .error (.thrown .invalidCast) else pure .null /-- Object storage is aligned to the 8-byte field slots of the admitted 64-bit targets. -/ @@ -137,7 +137,7 @@ def newReferenceArray (memory : Memory) (element : CIL.Extraction.TypeExpr) | .i32 bits => pure bits.toInt | .i64 bits => pure bits.toInt | _ => .error .invalidState - if length < 0 then .error (.thrown "System.OverflowException") else + if length < 0 then .error (.thrown .overflow) else if length.toNat > maxArrayLength then .error (.unsupported "Array length outside supported extent") else allocateObject memory (referenceArrayType element) (objectFieldSlot * length.toNat) diff --git a/CIL/Safety/Imports.lean b/CIL/Safety/Imports.lean index 499eb0ada..a664e2b0c 100644 --- a/CIL/Safety/Imports.lean +++ b/CIL/Safety/Imports.lean @@ -48,118 +48,118 @@ theorem run_extend {base linked : CIL.Program} (links : Links base linked) (fuel (h : run base fuel method pc args frame stack memory = outcome) : run linked fuel method pc args frame stack memory = outcome := by induction fuel generalizing method pc args frame stack memory outcome with - | zero => simpa only [run] using h + | zero => simpa only [run_zero, run_succ] using h | succ fuel ih => cases hb : base[method]? with | none => - simp only [run, hb] at h + simp only [run_succ, hb] at h exact absurd rfl (resolved _ h.symm) | some body => have hl := links.found hb cases ho : body.code[pc]? with | none => - simp only [run, hb, ho] at h + simp only [run_succ, hb, ho] at h exact absurd rfl (resolved _ h.symm) | some op => cases hs : step body op pc args frame stack memory with | error fault => - simp only [run, hb, hl, ho, hs, Bind.bind, Except.bind, Except.mapError] at h ⊢ + simp only [run_succ, hb, hl, ho, hs, Bind.bind, Except.bind, Except.mapError] at h ⊢ exact h | ok action => cases action with | next target values nextFrame updated => - simp only [run, hb, hl, ho, hs, Bind.bind, Except.bind, Except.mapError] at h ⊢ + simp only [run_succ, hb, hl, ho, hs, Bind.bind, Except.bind, Except.mapError] at h ⊢ exact ih method target args nextFrame values updated outcome resolved h | returned values updated => - simp only [run, hb, hl, ho, hs, Bind.bind, Except.bind, Except.mapError] at h ⊢ + simp only [run_succ, hb, hl, ho, hs, Bind.bind, Except.bind, Except.mapError] at h ⊢ exact h | call callee arguments rest updated => cases hc : base[callee]? with | none => - simp only [run, hb, ho, hs, hc, Bind.bind, Except.bind, Except.mapError] at h + simp only [run_succ, hb, ho, hs, hc, Bind.bind, Except.bind, Except.mapError] at h exact absurd rfl (resolved _ h.symm) | some child => have hlc := links.found hc cases he : enterFrame child arguments updated with | error fault => - simp only [run, hb, hl, ho, hs, hc, hlc, he, Bind.bind, Except.bind, + simp only [run_succ, hb, hl, ho, hs, hc, hlc, he, Bind.bind, Except.bind, Except.mapError] at h ⊢ exact h | ok entered => obtain ⟨childFrame, childMemory⟩ := entered cases hr : run base fuel callee 0 arguments childFrame [] childMemory with | error fault => - simp only [run, hb, ho, hs, hc, he, hr, Bind.bind, Except.bind, + simp only [run_succ, hb, ho, hs, hc, he, hr, Bind.bind, Except.bind, Except.mapError] at h subst h have more := ih callee 0 arguments childFrame [] childMemory _ resolved hr - simp only [run, hl, ho, hs, hlc, he, more, Bind.bind, Except.bind, + simp only [run_succ, hl, ho, hs, hlc, he, more, Bind.bind, Except.bind, Except.mapError] | ok childResult => obtain ⟨final, values⟩ := childResult have more := ih callee 0 arguments childFrame [] childMemory (.ok (final, values)) (by intro _ h; cases h) hr - simp only [run, hb, ho, hs, hc, he, hr, Bind.bind, Except.bind, + simp only [run_succ, hb, ho, hs, hc, he, hr, Bind.bind, Except.bind, Except.mapError] at h - simp only [run, hl, ho, hs, hlc, he, more, Bind.bind, Except.bind, + simp only [run_succ, hl, ho, hs, hlc, he, more, Bind.bind, Except.bind, Except.mapError] exact ih method (pc + 1) args frame (values ++ rest) final outcome resolved h | tailCall callee arguments updated => cases hc : base[callee]? with | none => - simp only [run, hb, ho, hs, hc, Bind.bind, Except.bind, Except.mapError] at h + simp only [run_succ, hb, ho, hs, hc, Bind.bind, Except.bind, Except.mapError] at h exact absurd rfl (resolved _ h.symm) | some child => have hlc := links.found hc cases returns : child.returnsValue != body.returnsValue with | true => - simpa [run, hb, hl, ho, hs, hc, hlc, returns, Bind.bind, Except.bind, - Except.mapError] using h + simpa [run_zero, run_succ, hb, hl, ho, hs, hc, hlc, returns, Bind.bind, + Except.bind, Except.mapError] using h | false => cases check : arguments.mapM (checkedValue (leaveFrame frame updated)) with | error fault => - simpa [run, hb, hl, ho, hs, hc, hlc, returns, check, Bind.bind, + simpa [run_zero, run_succ, hb, hl, ho, hs, hc, hlc, returns, check, Bind.bind, Except.bind, Except.mapError] using h | ok checked => cases enter : enterFrame child checked (leaveFrame frame updated) with | error fault => - simpa [run, hb, hl, ho, hs, hc, hlc, returns, check, enter, Bind.bind, - Except.bind, Except.mapError] using h + simpa [run_zero, run_succ, hb, hl, ho, hs, hc, hlc, returns, check, enter, + Bind.bind, Except.bind, Except.mapError] using h | ok entered => obtain ⟨childFrame, childMemory⟩ := entered - simp only [run, hb, hl, ho, hs, hc, hlc, returns, check, enter, + simp only [run_succ, hb, hl, ho, hs, hc, hlc, returns, check, enter, Bind.bind, Except.bind, Except.mapError, Bool.false_eq_true, ite_false] at h ⊢ exact ih callee 0 checked childFrame [] childMemory outcome resolved h | construct callee arguments rest parentFrame temporary kind updated => cases hc : base[callee]? with | none => - simp only [run, hb, ho, hs, hc, Bind.bind, Except.bind, Except.mapError] at h + simp only [run_succ, hb, ho, hs, hc, Bind.bind, Except.bind, Except.mapError] at h exact absurd rfl (resolved _ h.symm) | some child => have hlc := links.found hc cases he : enterFrame child arguments updated with | error fault => - simp only [run, hb, hl, ho, hs, hc, hlc, he, Bind.bind, Except.bind, + simp only [run_succ, hb, hl, ho, hs, hc, hlc, he, Bind.bind, Except.bind, Except.mapError] at h ⊢ exact h | ok entered => obtain ⟨childFrame, childMemory⟩ := entered cases hr : run base fuel callee 0 arguments childFrame [] childMemory with | error fault => - simp only [run, hb, ho, hs, hc, he, hr, Bind.bind, Except.bind, + simp only [run_succ, hb, ho, hs, hc, he, hr, Bind.bind, Except.bind, Except.mapError] at h subst h have more := ih callee 0 arguments childFrame [] childMemory _ resolved hr - simp only [run, hl, ho, hs, hlc, he, more, Bind.bind, Except.bind, + simp only [run_succ, hl, ho, hs, hlc, he, more, Bind.bind, Except.bind, Except.mapError] | ok childResult => obtain ⟨final, values⟩ := childResult have more := ih callee 0 arguments childFrame [] childMemory (.ok (final, values)) (by intro _ h; cases h) hr - simp only [run, hb, ho, hs, hc, he, hr, Bind.bind, Except.bind, + simp only [run_succ, hb, ho, hs, hc, he, hr, Bind.bind, Except.bind, Except.mapError] at h - simp only [run, hl, ho, hs, hlc, he, more, Bind.bind, Except.bind, + simp only [run_succ, hl, ho, hs, hlc, he, more, Bind.bind, Except.bind, Except.mapError] cases hn : !values.isEmpty with | true => @@ -183,29 +183,30 @@ theorem invoke_extend {base linked : CIL.Program} (links : Links base linked) (f invoke linked fuel method args memory = outcome := by cases hb : base[method]? with | none => - simp only [invoke, hb] at h + simp only [invoke_eq, hb] at h exact absurd rfl (resolved _ h.symm) | some body => have hl := links.found hb cases hc : (args.mapM (checkedValue memory)).mapError (fun fault => (⟨method, 0, fault⟩ : LocatedFault)) with | error fault => - simp only [invoke, hb, hl, hc, Bind.bind, Except.bind] at h ⊢ + simp only [invoke_eq, hb, hl, hc, Bind.bind, Except.bind] at h ⊢ exact h | ok checked => cases he : (enterFrame body checked memory).mapError (fun fault => (⟨method, 0, fault⟩ : LocatedFault)) with | error fault => - simp only [invoke, hb, hl, hc, he, Bind.bind, Except.bind] at h ⊢ + simp only [invoke_eq, hb, hl, hc, he, Bind.bind, Except.bind] at h ⊢ exact h | ok entered => obtain ⟨frame, entered⟩ := entered - simp only [invoke, hb, hl, hc, he, Bind.bind, Except.bind] at h ⊢ + simp only [invoke_eq, hb, hl, hc, he, Bind.bind, Except.bind] at h ⊢ exact run_extend links fuel method 0 checked frame [] entered outcome resolved h /-- A raised exception is not `invalidState`, so it is raised in every linked program. -/ theorem Raises.extend {base linked : CIL.Program} {method : Nat} {args : List Value} - {memory : Memory} {exception : String} (raised : Raises base method args memory exception) + {memory : Memory} {exception : ExceptionType} + (raised : Raises base method args memory exception) (links : Links base linked) : Raises linked method args memory exception := by obtain ⟨fuel, fault, invoked, thrown⟩ := raised @@ -242,14 +243,14 @@ theorem OutcomeContract.Holds.extend {Input Output : Type} {c : OutcomeContract /-- A call to a method the program lacks, such as an unlinked import, faults `invalidState` at the call. -/ theorem run_call_unlinked {program : CIL.Program} {fuel method pc callee : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack arguments rest : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .ok (.call callee arguments rest updated)) (missing : program[callee]? = none) : run program (fuel + 1) method pc args frame stack memory = .error ⟨method, pc, .invalidState⟩ := by - simp only [run, methodFound, instructionFound, stepped, missing, Except.mapError, Bind.bind, + simp only [run_succ, methodFound, instructionFound, stepped, missing, Except.mapError, Bind.bind, Except.bind] /-- Invoking a method the program lacks faults `invalidState` at its entry. -/ @@ -257,7 +258,7 @@ theorem invoke_unlinked {program : CIL.Program} {fuel method : Nat} {args : List {memory : Memory} (missing : program[method]? = none) : invoke program fuel method args memory = .error ⟨method, 0, .invalidState⟩ := by - simp only [invoke, missing] + simp only [invoke_eq, missing] /-- `claim` holds in every program that links `base` and whose imports meet `imports`. -/ def HoldsGiven (base : CIL.Program) (imports claim : CIL.Program → Prop) : Prop := diff --git a/CIL/Safety/InstructionMemory.lean b/CIL/Safety/InstructionMemory.lean index ca980a94d..0e97395e3 100644 --- a/CIL/Safety/InstructionMemory.lean +++ b/CIL/Safety/InstructionMemory.lean @@ -1,7 +1,9 @@ import CIL.Safety.SnapshotMemory import CIL.Safety.SpanViews import CIL.Safety.ArrayViews +import CIL.Safety.NativeMethodTables import CIL.Semantics +import CIL.Resolution /-! # Checked memory instructions @@ -37,6 +39,13 @@ inductive Value where /-- A class instance reference: null or offset 0 of an object allocation. It has no byte position, so arithmetic, byte loads and byref operations never accept it. -/ | object (reference : ManagedReference) + /-- The runtime handle loaded by `ldtoken` with a type operand. -/ + | typeHandle (type : CIL.Extraction.TypeExpr) + /-- A `System.Type` obtained from a checked metadata type handle. -/ + | runtimeType (type : CIL.Extraction.TypeExpr) + /-- The address of span local `slot` of the running frame, as `ldloca` or `ldarga` takes it. + Only a call reads the slot through it, when it passes the home; it never leaves the frame. -/ + | spanHome (slot : Nat) deriving DecidableEq, Repr /-- Why the checked interpreter stopped. -/ @@ -51,7 +60,7 @@ inductive ExecutionFault where unspecified vector bits or an overlapping block copy. -/ | unsupported (description : String) /-- The method raised the CLR exception named `exception`. -/ - | thrown (exception : String) + | thrown (exception : ExceptionType) /-- `run` ran out of fuel before the method returned. -/ | fuelExhausted /-- Dynamic stack demand exceeded the caller-supplied per-activation quota. @@ -65,49 +74,60 @@ inductive ExecutionFault where /-- A checked result: a value, or the fault that stopped execution. -/ abbrev ExecutionResult := Except ExecutionFault +/-- A checked memory operation transforms the evaluation stack and memory together. -/ +abbrev MemoryStep := List Value -> Memory -> ExecutionResult (Prod Memory (List Value)) + /-- Whether a stack value has bits that must not be observed; only a scalar can. -/ def Value.hasUnspecified : Value → Bool | .scalar value => value.hasUnspecified | _ => false +/-- Whether a stack value is a span local's home, which names a slot of the running frame. -/ +def Value.isHome : Value → Bool + | .spanHome _ => true + | _ => false + /-- Whether any of the top `count` stack entries has unspecified bits. -/ def unspecifiedOperands (count : Nat) (stack : List Value) : Bool := (stack.take count).any Value.hasUnspecified /-- Operands an op observes or exposes across a method boundary; `step` rejects the op when - any of them has unspecified bits. -/ -def observerCount : CIL.Op → Nat + any of them has unspecified bits. A call counts none, since the arm of its link checks. -/ +def observerCount : CIL.Instruction → Nat -- Arithmetic and comparisons - | .nativeBinary _ | .divide .. | .checkedArithmetic .. => 2 - | .checkedConvert .. | .neg | .bnot | .convI1 | .convI2 | .convU2 - | .convI8 | .convI4 | .convU1 | .convU4 | .convU8 | .convU | .stackAlloc _ | .newArray _ - | .newReferenceArray _ => 1 - | .add | .sub | .mul | .band | .bor | .bxor | .shl | .shr | .shrUn - | .lt | .ltu | .gt | .gtu | .eq | .bgt _ | .ble _ | .bleu _ - | .bltu _ | .bgeu _ | .bgtu _ | .bge _ | .blt _ | .beq _ | .bne _ => 2 - | .brzero _ | .brnonzero _ | .switch _ | .objectTest .. => 1 - -- Memory, fields and lanes - | .loadAggregate _ | .loadScalar .. | .fieldAt .. | .fieldAddress _ | .aggregateFieldAt .. - | .lane _ | .laneAddress _ | .load64 | .skipInit | .asRef | .initAggregate _ => 1 - | .storeAggregate _ | .storeScalar _ | .setFieldAt .. | .setAggregateFieldAt .. - | .setLane _ | .store64 | .copyObject _ => 2 + | .div | .div_un | .rem | .rem_un | .add_ovf | .add_ovf_un | .sub_ovf + | .sub_ovf_un | .mul_ovf | .mul_ovf_un => 2 + | .conv_ovf _ | .conv_ovf_un _ | .neg | .not | .conv_i1 | .conv_i2 | .conv_u2 + | .conv_i8 | .conv_i4 | .conv_u1 | .conv_u4 | .conv_u8 | .conv_u | .conv_i | .localloc + | .newarr _ => 1 + | .add | .sub | .mul | .and | .or | .xor | .shl | .shr | .shr_un + | .clt | .clt_un | .cgt | .cgt_un | .ceq | .bgt _ | .ble _ | .ble_un _ + | .blt_un _ | .bge_un _ | .bgt_un _ | .bge _ | .blt _ | .beq _ | .bne_un _ => 2 + | .brfalse _ | .brtrue _ | .switch _ | .castclass _ | .isinst _ => 1 + | .ldfld _ | .ldflda _ => 1 + | .stfld _ => 2 + -- Memory and fields + | .ldind _ | .initobj _ => 1 + | .ldobj type => if Extraction.spanLocal type then 0 else 1 + | .stind _ | .cpobj _ | .stobj _ => 2 + | .ldlen => 1 + | .ldelem _ | .ldelem_ref | .ldelem_any _ | .ldelema .. => 2 + | .stelem _ | .stelem_ref | .stelem_any _ | .cpblk _ | .initblk _ => 3 + | .ldsfld _ | .ldsflda _ | .ldtoken _ => 0 + | .sizeof _ => 0 + | .stsfld _ => 1 -- Method boundaries - | .ret _ => 1 - | .call _ argc | .callInstance _ argc | .callVirtual _ argc | .tailCall _ argc - | .newVector256 _ argc - | .newAggregate _ argc _ | .newObject _ argc .. => argc - | .managed (.call signature _ _) => signature.parameters.length + 1 - | .managed (.tableElement _) | .managed (.tableAddress _) | .managed .tableOffset => 2 - | .managed .tableLength | .managed (.tableLoad _) => 1 - | .managed (.address ..) => 0 - | .memory operation => operation.argumentCount - | .initializeSpanLocal _ constructor => constructor.operation.argumentCount + | .ret => 1 + | .callvirt method => method.argumentCount + | .newobj constructor => constructor.parameters.length + | .calli signature _ => signature.parameters.length + 1 + | .ldftn .. => 0 -- Internal forwarders preserve unspecified bits; stores and intrinsic consumers guard them. - | .nop | .arg .. | .vectorArg _ | .vectorArgAddr _ | .local .. | .vectorLocal _ - | .vectorLocalAddr _ | .setVectorLocal _ | .localAddr _ | .setLocal .. | .const32 _ - | .const64 _ | .dup | .pop - | .branch _ | .feature _ | .intrinsic .. - | .throw _ | .unsupported _ => 0 + | .nop | .ldarg _ | .starg _ | .ldarga _ | .ldloc .. | .ldloca _ | .stloc .. | .ldc_i4 _ + | .ldc_i8 _ | .ldnull | .ldstr _ | .dup | .pop | .br _ + | .throw _ | .unadmitted _ => 0 + -- A linked call checks its method's arguments, and a call's model the operands it observes. + | .call .. => 0 /-- A memory check as an execution result; a fault is attributed to `reference` and `width`. -/ def checkedAt (reference : ManagedReference) (width : Nat) (result : Checked α) : @@ -306,7 +326,7 @@ def formObject (memory : Memory) (reference : ManagedReference) : ExecutionResul def objectFieldAddress (memory : Memory) (reference : ManagedReference) (owner : CIL.Extraction.TypeExpr) (offset : Nat) : ExecutionResult Reference := match reference with - | .null => .error (.thrown "System.NullReferenceException") + | .null => .error (.thrown .nullReference) | .address address => do let allocation ← checkedAt reference 0 (liveAllocation memory address.allocation) match allocation.kind with @@ -345,8 +365,8 @@ def arrayElement (memory : Memory) (view : ArrayView) (index width : Nat) : ExecutionResult Reference := do if width != view.elementType.width then .error .invalidState else pure () let _ ← checkedAt view.reference 0 (view.validate memory) - if view.reference == .null then .error (.thrown "System.NullReferenceException") else pure () - if index < view.length then pure () else .error (.thrown "System.IndexOutOfRangeException") + if view.reference == .null then .error (.thrown .nullReference) else pure () + if index < view.length then pure () else .error (.thrown .indexOutOfRange) checkedAt view.reference width (view.element memory index) /-- Span copying snapshots every source byte before writing, including for overlapping views. A short destination returns false or throws without reading or modifying either range. -/ @@ -359,7 +379,7 @@ def spanCopyInstruction (width : Nat) (tryCopy : Bool) (stack : List Value) (mem if !destination.writable then .error .invalidState else if source.length > destination.length then if tryCopy then return (memory, .scalar (.i32 0) :: rest) - else .error (.thrown "System.ArgumentException") + else .error (.thrown .argument) else let values := if tryCopy then .scalar (.i32 1) :: rest else rest if source.length == 0 then return (memory, values) else @@ -421,16 +441,16 @@ def arrayFillInstruction (element : CIL.ArrayElement) (counted : Bool) | .array view => if view.elementType != element then .error .invalidState else do let _ ← checkedAt view.reference 0 (view.validate memory) - if view.reference == .null then .error (.thrown "System.ArgumentNullException") else do + if view.reference == .null then .error (.thrown .argumentNull) else do let storage ← match range with | none => pure view.storage | some (start, count) => do if start ≤ view.length && count ≤ view.length - start then pure () - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) checkedAt view.reference 0 (view.storage.slice memory start count) spanFillInstruction element.width false (.scalar value :: .bufferSpan storage :: rest) memory - | .object .null => .error (.thrown "System.ArgumentNullException") + | .object .null => .error (.thrown .argumentNull) | _ => .error .invalidState /-- A fixed-width integer in the requested byte order, with the required narrow extension. -/ @@ -453,7 +473,7 @@ def binaryTransferInstruction (width : Nat) (signed bigEndian read trying : Bool | true, false, .bufferSpan view :: rest => let _ ← checkedAt view.reference 0 (view.validate memory) if view.width != 1 then .error .invalidState else - if view.length < width then .error (.thrown "System.ArgumentOutOfRangeException") else + if view.length < width then .error (.thrown .argumentOutOfRange) else let value ← loadTransfer memory view.reference (.scalar width false) return (memory, .scalar (← endianValue width signed bigEndian value) :: rest) | true, true, .reference destination :: .bufferSpan view :: rest => @@ -471,7 +491,7 @@ def binaryTransferInstruction (width : Nat) (signed bigEndian read trying : Bool if !view.writable then .error (.memory .unwritable view.reference width) else if view.length < width then if trying then return (memory, .scalar (.i32 0) :: rest) - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) else let ordered ← endianValue width false bigEndian value let result ← storeTransfer memory view.reference (.scalar width false) ordered @@ -493,10 +513,10 @@ def viewObservationInstruction (observation : CIL.ViewObservation) (stack : List else .readOnlyReference reference) :: rest) | .arrayData, .array view :: rest => let _ ← checkedAt view.reference 0 (view.validate memory) - if view.reference == .null then .error (.thrown "System.NullReferenceException") else + if view.reference == .null then .error (.thrown .nullReference) else return (memory, (← formValue memory view.reference) :: rest) | .arrayData, .object .null :: _ => - .error (.thrown "System.NullReferenceException") + .error (.thrown .nullReference) | _, _ => .error .invalidState /-- Typed array transfers share the checked snapshot reads and writes used by byref transfers. -/ @@ -510,7 +530,7 @@ def arrayTransferInstruction (element : CIL.ArrayElement) (store : Bool) let some index := arrayIndex index | .error .invalidState let view ← match array with | .array view => pure view - | .object .null => .error (.thrown "System.NullReferenceException") + | .object .null => .error (.thrown .nullReference) | _ => .error .invalidState if view.elementType != element then .error .invalidState else pure () let shape ← match element with @@ -626,6 +646,87 @@ def wideArithmeticInstruction (operation : CIL.MemoryOp) (stack : List Value) (m .scalar (.i64 quotient) :: rest) | _, _ => .error .invalidState +/-- `ldelem` of a numeric value-array element of `width` bytes, extended as `signed` says; a null + array throws `NullReferenceException` once the index is well formed. -/ +def arrayLoad (width : Nat) (signed : Bool) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := do + match stack with + | .scalar index :: .array view :: rest => + let some index := arrayIndex index | .error .invalidState + let reference ← arrayElement memory view index width + let value ← loadValue memory (.address reference) width signed + return (memory, .scalar value :: rest) + | .scalar index :: .object .null :: _ => + let some _ := arrayIndex index | .error .invalidState + .error (.thrown .nullReference) + | _ => .error .invalidState + +/-- `stelem` of a numeric value-array element of `width` bytes; a null array throws + `NullReferenceException` once the index is well formed. -/ +def arrayStore (width : Nat) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := do + match stack with + | .scalar value :: .scalar index :: .array view :: rest => + let some index := arrayIndex index | .error .invalidState + let reference ← arrayElement memory view index width + let number ← match width, value with + | 1, .i32 bits | 2, .i32 bits | 4, .i32 bits => .ok bits.toNat + | 8, .i64 bits => .ok bits.toNat + | _, _ => .error .invalidState + let updated ← checkedAt (.address reference) width + (write memory reference (numberBytes number width) 1) + return (updated, rest) + | .scalar _ :: .scalar index :: .object .null :: _ => + let some _ := arrayIndex index | .error .invalidState + .error (.thrown .nullReference) + | _ => .error .invalidState + +/-- `ldelema` of a value-array element of type `element`: a writable or read-only reference to + it; a null array throws `NullReferenceException` once the index is well formed. -/ +def arrayAddress (element : CIL.ArrayElement) (writable : Bool) (stack : List Value) + (memory : Memory) : ExecutionResult (Memory × List Value) := do + match stack with + | .scalar index :: .array view :: rest => + if view.elementType != element then .error .invalidState else pure () + let some index := arrayIndex index | .error .invalidState + let reference ← arrayElement memory view index element.width + return (memory, (if writable then .reference (.address reference) + else .readOnlyReference (.address reference)) :: rest) + | .scalar index :: .object .null :: _ => + let some _ := arrayIndex index | .error .invalidState + .error (.thrown .nullReference) + | _ => .error .invalidState + +/-- Copies non-overlapping bytes at the admitted alignment. A zero count touches no memory. -/ +def copyBlockBytes (alignment : Nat) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := do + match stack with + | .scalar (.i32 count) :: .reference source :: .reference destination :: rest => + let width := count.toNat + if width = 0 then return (memory, rest) else + let src ← referenceAt source + let dst ← referenceAt destination + if src.allocation = dst.allocation && src.offset < dst.offset + width && + dst.offset < src.offset + width then + .error (.unsupported "Overlapping block copy") + else + let bytes ← checkedAt source width (read memory src width alignment) + return (← checkedAt destination width (write memory dst bytes alignment), rest) + | _ => .error .invalidState + +/-- Fills the destination with the low byte of the value. A zero count touches no memory. -/ +def initBlockBytes (alignment : Nat) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := do + match stack with + | .scalar (.i32 count) :: .scalar (.i32 value) :: .reference destination :: rest => + let width := count.toNat + if width = 0 then return (memory, rest) else + let dst ← referenceAt destination + let _ ← checkedAt destination width (access memory dst width alignment true) + return (← checkedAt destination width + (write memory dst (List.replicate width (value.setWidth 8)) alignment), rest) + | _ => .error .invalidState + /-- Executes the remaining memory operations after the shared operand guard. -/ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memory : Memory) : ExecutionResult (Memory × List Value) := do @@ -634,7 +735,7 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo | .marshalRead kind false, .bufferSpan view :: rest => let _ ← checkedAt view.reference 0 (view.validate memory) if view.width != 1 then .error .invalidState else - if view.length < kind.width then .error (.thrown "System.ArgumentOutOfRangeException") else + if view.length < kind.width then .error (.thrown .argumentOutOfRange) else return (memory, .scalar (← loadTransfer memory view.reference kind) :: rest) | .marshalRead kind true, .reference destination :: .bufferSpan view :: rest => let _ ← checkedAt view.reference 0 (view.validate memory) @@ -650,32 +751,13 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo if !view.writable then .error (.memory .unwritable view.reference kind.width) else if view.length < kind.width then if trying then return (memory, .scalar (.i32 0) :: rest) - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) else let value ← loadTransfer memory source kind let result ← storeTransfer memory view.reference kind value if trying then return (result, .scalar (.i32 1) :: rest) else return (result, rest) - -- Block copy and block initialization - | .copyBlock alignment, - .scalar (.i32 count) :: .reference source :: .reference destination :: rest => - let width := count.toNat - if width = 0 then return (memory, rest) else - let src ← referenceAt source - let dst ← referenceAt destination - if src.allocation = dst.allocation && src.offset < dst.offset + width && - dst.offset < src.offset + width then - .error (.unsupported "Overlapping block copy") - else - let bytes ← checkedAt source width (read memory src width alignment) - return (← checkedAt destination width (write memory dst bytes alignment), rest) - | .initBlock alignment, - .scalar (.i32 count) :: .scalar (.i32 value) :: .reference destination :: rest => - let width := count.toNat - if width = 0 then return (memory, rest) else - let dst ← referenceAt destination - let _ ← checkedAt destination width (access memory dst width alignment true) - return (← checkedAt destination width - (write memory dst (List.replicate width (value.setWidth 8)) alignment), rest) + | .copyBlock alignment, values => copyBlockBytes alignment values memory + | .initBlock alignment, values => initBlockBytes alignment values memory -- Vector loads and stores | .load128, .reference r :: rest | .load128, .readOnlyReference r :: rest => return (memory, .scalar (← loadValue memory r 16) :: rest) @@ -695,7 +777,6 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo return (← storeValue memory r (.snapshot .vector128 bytes), rest) | .store256, .scalar (.snapshot .vector256 bytes) :: .reference r :: rest => return (← storeValue memory r (.snapshot .vector256 bytes), rest) - | .init256, .reference r :: rest => return (← storeValue memory r (.v256 0), rest) -- References, pointer arithmetic and bit casts | .byteOffset, .reference target :: .reference origin :: rest | .byteOffset, .readOnlyReference target :: .reference origin :: rest @@ -704,7 +785,6 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo let offset ← referenceDifference memory target origin return (memory, .scalar (.i64 offset) :: rest) | .asRef, .reference r :: rest => return (memory, (← formValue memory r) :: rest) - | .bitcast256, .scalar (.v256 bits) :: rest => return (memory, .scalar (.v256 bits) :: rest) | .bitcastValue source target, .scalar value :: rest => return (memory, .scalar (← bitcastValue source target value) :: rest) | .bitcastByteBool, .scalar (.i32 bits) :: rest => @@ -713,9 +793,6 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo | .bitcastByteBool, .scalar (.i8 bits) :: rest => if bits.toNat ≤ 1 then return (memory, .scalar (.i32 (bits.zeroExtend 32)) :: rest) else .error (.unsupported "Noncanonical Boolean representation") - | .skipInit, .reference r :: rest => - let _ ← formValue memory r - return (memory, rest) -- Arrays retain the supplied length; reads and writes use checked element storage. | .arraySpan element writable .whole, .array array :: rest => if array.elementType != element then .error .invalidState else pure () @@ -728,7 +805,7 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo let view : SpanView := ⟨array.reference, array.length, element.width, writable⟩ let _ ← checkedAt view.reference 0 (view.validate memory) if start.toNat ≤ view.length then pure () - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) let sliced ← checkedAt view.reference 0 (view.slice memory start.toNat (view.length - start.toNat)) return (memory, .bufferSpan sliced :: rest) @@ -738,7 +815,7 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo let view : SpanView := ⟨array.reference, array.length, element.width, writable⟩ let _ ← checkedAt view.reference 0 (view.validate memory) if start.toNat ≤ view.length && length.toNat ≤ view.length - start.toNat then pure () - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) let sliced ← checkedAt view.reference 0 (view.slice memory start.toNat length.toNat) return (memory, .bufferSpan sliced :: rest) | .arraySpan element writable .whole, .object .null :: rest => @@ -746,50 +823,17 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo let _ ← checkedAt view.reference 0 (view.validate memory) return (memory, .bufferSpan view :: rest) | .arraySpan element writable .fromStart, .scalar (.i32 start) :: .object .null :: rest => - if start == 0 then pure () else .error (.thrown "System.ArgumentOutOfRangeException") + if start == 0 then pure () else .error (.thrown .argumentOutOfRange) let view : SpanView := ⟨.null, 0, element.width, writable⟩ let _ ← checkedAt view.reference 0 (view.validate memory) return (memory, .bufferSpan view :: rest) | .arraySpan element writable .counted, .scalar (.i32 length) :: .scalar (.i32 start) :: .object .null :: rest => if start == 0 && length == 0 then pure () - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) let view : SpanView := ⟨.null, 0, element.width, writable⟩ let _ ← checkedAt view.reference 0 (view.validate memory) return (memory, .bufferSpan view :: rest) - | .arrayAddress element writable, .scalar index :: .array view :: rest => - if view.elementType != element then .error .invalidState else pure () - let some index := arrayIndex index | .error .invalidState - let reference ← arrayElement memory view index element.width - return (memory, (if writable then .reference (.address reference) - else .readOnlyReference (.address reference)) :: rest) - | .arrayLength, .array view :: rest => - let _ ← checkedAt view.reference 0 (view.validate memory) - if view.reference == .null then .error (.thrown "System.NullReferenceException") else - return (memory, .scalar (.i64 (BitVec.ofNat 64 view.length)) :: rest) - | .arrayLoad width signed, .scalar index :: .array view :: rest => - let some index := arrayIndex index | .error .invalidState - let reference ← arrayElement memory view index width - let value ← loadValue memory (.address reference) width signed - return (memory, .scalar value :: rest) - | .arrayStore width, .scalar value :: .scalar index :: .array view :: rest => - let some index := arrayIndex index | .error .invalidState - let reference ← arrayElement memory view index width - let number ← match width, value with - | 1, .i32 bits | 2, .i32 bits | 4, .i32 bits => .ok bits.toNat - | 8, .i64 bits => .ok bits.toNat - | _, _ => .error .invalidState - let updated ← checkedAt (.address reference) width - (write memory reference (numberBytes number width) 1) - return (updated, rest) - | .arrayLength, .object .null :: _ => .error (.thrown "System.NullReferenceException") - | .arrayLoad .., .scalar index :: .object .null :: _ - | .arrayAddress .., .scalar index :: .object .null :: _ => - let some _ := arrayIndex index | .error .invalidState - .error (.thrown "System.NullReferenceException") - | .arrayStore _, .scalar _ :: .scalar index :: .object .null :: _ => - let some _ := arrayIndex index | .error .invalidState - .error (.thrown "System.NullReferenceException") -- Spans | .spanEmpty width writable, rest => let view : SpanView := ⟨.null, 0, width, writable⟩ @@ -800,18 +844,18 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo (0 < targetWidth ∧ targetWidth < 2^64) then pure () else .error .invalidState let _ ← checkedAt view.reference 0 (view.validate memory) let length := view.length * sourceWidth / targetWidth - if length < 2^31 then pure () else .error (.thrown "System.OverflowException") + if length < 2^31 then pure () else .error (.thrown .overflow) let result : SpanView := { view with width := targetWidth, length } let _ ← checkedAt result.reference 0 (result.validate memory) return (memory, .bufferSpan result :: rest) | .spanFromReference width writable, .scalar (.i32 length) :: .reference reference :: rest => - if length.toInt < 0 then .error (.thrown "System.ArgumentOutOfRangeException") else pure () + if length.toInt < 0 then .error (.thrown .argumentOutOfRange) else pure () let view : SpanView := ⟨reference, length.toNat, width, writable⟩ let _ ← checkedAt reference (length.toNat * width) (view.validate memory) return (memory, .bufferSpan view :: rest) | .spanFromReference width false, .scalar (.i32 length) :: .readOnlyReference reference :: rest => - if length.toInt < 0 then .error (.thrown "System.ArgumentOutOfRangeException") else pure () + if length.toInt < 0 then .error (.thrown .argumentOutOfRange) else pure () let view : SpanView := ⟨reference, length.toNat, width, false⟩ let _ ← checkedAt reference (length.toNat * width) (view.validate memory) return (memory, .bufferSpan view :: rest) @@ -820,18 +864,18 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo return (memory, .bufferSpan view.asReadOnly :: rest) | .spanSlice true, .scalar (.i32 length) :: .scalar (.i32 start) :: .bufferSpan view :: rest => if start.toNat ≤ view.length && length.toNat ≤ view.length - start.toNat then pure () - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) let sliced ← checkedAt view.reference 0 (view.slice memory start.toNat length.toNat) return (memory, .bufferSpan sliced :: rest) | .spanSlice false, .scalar (.i32 start) :: .bufferSpan view :: rest => if start.toNat ≤ view.length then pure () - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) let sliced ← checkedAt view.reference 0 (view.slice memory start.toNat (view.length - start.toNat)) return (memory, .bufferSpan sliced :: rest) | .spanItem width, .scalar (.i32 index) :: .bufferSpan view :: rest => if width == view.width then pure () else .error .invalidState if index.toNat < view.length then pure () - else .error (.thrown "System.IndexOutOfRangeException") + else .error (.thrown .indexOutOfRange) let reference ← checkedAt view.reference width (view.element memory index.toNat) return (memory, (if view.writable then .reference (.address reference) else .readOnlyReference (.address reference)) :: rest) @@ -853,17 +897,14 @@ def otherMemoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo let address ← referenceAt r let result ← checkedAt r 0 (add memory address width (BitVec.ofNat 64 index.toNat)) return (memory, .reference (.address result) :: rest) - else .error (.thrown "System.IndexOutOfRangeException") + else .error (.thrown .indexOutOfRange) | .spanLength, .span _ length :: rest => return (memory, .scalar (.i32 (BitVec.ofNat 32 length)) :: rest) -- Null references | .nullRef, _ => return (memory, .reference .null :: stack) - | .nullObject, _ => return (memory, .object .null :: stack) -- A pure comparison: no access, so a live, expired or end reference all answer false. | .isNullRef, .reference r :: rest | .isNullRef, .readOnlyReference r :: rest => return (memory, .scalar (.i32 (match r with | .null => 1 | .address _ => 0)) :: rest) - | .staticAddress _, _ | .staticValue _, _ | .createSpan _, _ | .spanCreate, _ => - .error (.unsupported "Extraction-bound static/span allocation metadata") | _, _ => .error .invalidState /-- Constructs a one-element view over checked existing storage without reading its bytes. -/ @@ -902,7 +943,7 @@ def storeSlot (memory : Memory) (field : Reference) (value : ManagedReference) : def referenceArrayOf (memory : Memory) (array : ManagedReference) : ExecutionResult (Reference × CIL.Extraction.TypeExpr × Nat) := match array with - | .null => .error (.thrown "System.NullReferenceException") + | .null => .error (.thrown .nullReference) | .address address => do let allocation ← checkedAt array 0 (liveAllocation memory address.allocation) match allocation.kind with @@ -918,7 +959,7 @@ def referenceArraySlot (memory : Memory) (array : ManagedReference) (index : Nat ExecutionResult (Reference × CIL.Extraction.TypeExpr) := do let (origin, element, length) ← referenceArrayOf memory array if index < length then return (⟨origin.allocation, objectFieldSlot * index⟩, element) - else .error (.thrown "System.IndexOutOfRangeException") + else .error (.thrown .indexOutOfRange) /-- The covariance check of `stelem.ref` storing `value` into an array of class `element`. Null is always storable; an instance of an admitted class is storable exactly when `element` is in @@ -935,7 +976,7 @@ def storableIn (memory : Memory) (element : CIL.Extraction.TypeExpr) (value : Ma match type.identity, element with | .object .., .object .. => if element ∈ type.lineage then .ok () - else .error (.thrown "System.ArrayTypeMismatchException") + else .error (.thrown .arrayTypeMismatch) | _, _ => .error (.unsupported "Array store outside the admitted classes") | _ => .error .invalidState @@ -988,18 +1029,120 @@ def storeArraySlot (memory : Memory) (field : Reference) (view : ArrayView) let held ← arraySlotValue memory view element return setSlot memory field held -/-- `ldfld` and `stfld` of a field of a class instance, at the field's model offset: a scalar - field moves bytes, a class-typed field moves the object reference its slot holds. `ldlen`, - `ldelem.ref` and `stelem.ref` reach the slots of an array of class references the same way, - checking the null array, then the index, then a stored value's class, as the CLR does. -/ -def objectFieldInstruction (operation : CIL.MemoryOp) (stack : List Value) (memory : Memory) : - ExecutionResult (Memory × List Value) := do - match operation, stack with - | .objectField owner offset width signed, .object reference :: rest => +/-- `ldlen` of a method table: its entry count; a table without entries throws + `NullReferenceException`. -/ +def methodTableLength (table : CIL.MethodTable) : ExecutionResult CIL.Value := do + let some targets := table.targets | .error (.thrown .nullReference) + if targets.size >= 2^31 then .error .invalidState else pure () + return .i64 (BitVec.ofNat 64 targets.size) + +/-- `ldelem` of a method table: the pointer at `index`, read from the native backing when the + table has one; a table without entries throws before an index outside them. -/ +def methodTableElement (table : CIL.MethodTable) (index : CIL.Value) (memory : Memory) : + ExecutionResult CIL.MethodPointer := do + let some index := arrayIndex index | .error .invalidState + let some targets := table.targets | .error (.thrown .nullReference) + if targets.size >= 2^31 then .error .invalidState else pure () + let some target := targets[index]? | .error (.thrown .indexOutOfRange) + match table.backing with + | some backing => + let selected := { table with backing := some { backing with + byteOffset := nativeMethodWidth * index } } + checkedAt .null nativeMethodWidth (loadNativeTable selected memory) + | none => return ⟨table.signature, some target⟩ + +/-- `ldlen`, `ldelem`, `stelem`, `ldelem.ref` and `stelem.ref` on a value array or an + array of class references; `ldlen` and `ldelem.i` of a managed handler table read the + table. A null array throws `NullReferenceException` after a well-formed index; any other + operand is an ill-typed state. -/ +def elementInstruction (op : CIL.Instruction) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := + -- Arms of one opcode differ in a stack constructor, so a renamed stack selects the same arm. + match op, stack with + -- Lengths: a value array's view is validated before its null check. + | .ldlen, .array view :: rest => do + let _ ← checkedAt view.reference 0 (view.validate memory) + if view.reference == .null then .error (.thrown .nullReference) else + return (memory, .scalar (.i64 (BitVec.ofNat 64 view.length)) :: rest) + | .ldlen, .object array :: rest => do + let (_, _, length) ← referenceArrayOf memory array + return (memory, .scalar (.i64 (BitVec.ofNat 64 length)) :: rest) + | .ldlen, .methodTable table :: rest => do + return (memory, .scalar (← methodTableLength table) :: rest) + -- Numeric elements. + | .ldelem .i, .scalar index :: .methodTable table :: rest => do + return (memory, .methodPointer (← methodTableElement table index memory) :: rest) + | .ldelem kind, .scalar _ :: .array _ :: _ | .ldelem kind, .scalar _ :: .object .null :: _ => + arrayLoad kind.width kind.signed stack memory + | .stelem kind, .scalar _ :: .scalar _ :: .array _ :: _ + | .stelem kind, .scalar _ :: .scalar _ :: .object .null :: _ => + arrayStore kind.width stack memory + -- Class reference elements: the index, then the null array, then the bounds are checked. + | .ldelem_ref, .scalar index :: .object array :: rest => do + let some index := arrayIndex index | .error .invalidState + let (slot, _) ← referenceArraySlot memory array index + return (memory, (← loadSlot memory slot) :: rest) + | .stelem_ref, .object value :: .scalar index :: .object array :: rest => do + let some index := arrayIndex index | .error .invalidState + let (slot, element) ← referenceArraySlot memory array index + let _ ← storableIn memory element value + return (← storeSlot memory slot value, rest) + | _, _ => .error .invalidState + +/-- Typed `ldelem` (`store` false) or `stelem` (`store` true) of an element of `shape`: a + struct or vector element by checked snapshot, any other value element by its width and sign, + or with `ldelem` the entry of a managed handler table of the shape's signature, a null table + throwing `NullReferenceException`. Class and unsupported shapes and table stores fault. -/ +def typedElementInstruction (shape : ElementShape) (store : Bool) (stack : List Value) + (memory : Memory) : ExecutionResult (Memory × List Value) := + match shape, store, stack with + | .value element, store, _ => + if element.composite then arrayTransferInstruction element store stack memory + else if store then arrayStore element.width stack memory + else arrayLoad element.width element.signed stack memory + | .method signature, false, .scalar index :: .methodTable table :: rest => do + if table.signature != signature then .error .invalidState else pure () + return (memory, .methodPointer (← methodTableElement table index memory) :: rest) + | .method _, false, .scalar _ :: .object .null :: _ => .error (.thrown .nullReference) + | .unsupported reason, _, _ => .error (.unsupported reason) + | _, _, _ => .error .invalidState + +/-- `ldelema` of an element of `shape`: the reference `arrayAddress` gives for a value + element, read-only when `readOnly`. Class, method-table and unsupported shapes fault. -/ +def elementAddressInstruction (shape : ElementShape) (readOnly : Bool) (stack : List Value) + (memory : Memory) : ExecutionResult (Memory × List Value) := + match shape with + | .value element => arrayAddress element (!readOnly) stack memory + | .unsupported reason => .error (.unsupported reason) + | _ => .error .invalidState + +/-- `ldfld` of a class instance field whose slot at model offset `offset` holds `slot`: the + receiver gives way to the word, object reference or array reference the slot keeps. A null + receiver throws `NullReferenceException` and a receiver of another class lineage faults. -/ +def classFieldLoad (owner : CIL.Extraction.TypeExpr) (offset : Nat) (slot : CIL.ClassSlot) + (stack : List Value) (memory : Memory) : ExecutionResult (Memory × List Value) := do + match slot, stack with + | .word width signed, .object reference :: rest => if width ∉ [1, 2, 4, 8] then .error (.unsupported "Object field width") else let address ← objectFieldAddress memory reference owner offset return (memory, .scalar (← loadValue memory (.address address) width signed) :: rest) - | .setObjectField owner offset width, .scalar value :: .object reference :: rest => + | .reference, .object reference :: rest => + let address ← objectFieldAddress memory reference owner offset + return (memory, (← loadSlot memory address) :: rest) + | .array element, .object reference :: rest => + let address ← objectFieldAddress memory reference owner offset + return (memory, (← loadArraySlot memory address element) :: rest) + | _, _ => .error .invalidState + +/-- `stfld` of a class instance field whose slot at model offset `offset` holds `slot`: the + value on top of the stack, an integer of the slot's width, an object or an array of the + slot's element, replaces what the receiver below it keeps there. An `int32` into an 8-byte + slot or an `int64` into a narrower one is an invalid state, which no admitted method reaches: + the scalar check requires the field's stack kind for a stored integer. -/ +def classFieldStore (owner : CIL.Extraction.TypeExpr) (offset : Nat) (slot : CIL.ClassSlot) + (stack : List Value) (memory : Memory) : ExecutionResult (Memory × List Value) := do + match slot, stack with + | .word width _, .scalar value :: .object reference :: rest => let address ← objectFieldAddress memory reference owner offset let number ← match width, value with | 1, .i32 bits | 2, .i32 bits | 4, .i32 bits => .ok bits.toNat @@ -1007,28 +1150,10 @@ def objectFieldInstruction (operation : CIL.MemoryOp) (stack : List Value) (memo | _, _ => .error .invalidState return (← checkedAt (.address address) width (write memory address (numberBytes number width) ordinaryAccessAlignment), rest) - | .objectReferenceField owner offset, .object reference :: rest => - let address ← objectFieldAddress memory reference owner offset - return (memory, (← loadSlot memory address) :: rest) - | .setObjectReferenceField owner offset, .object value :: .object reference :: rest => + | .reference, .object value :: .object reference :: rest => let address ← objectFieldAddress memory reference owner offset return (← storeSlot memory address value, rest) - | .referenceArrayLength, .object array :: rest => - let (_, _, length) ← referenceArrayOf memory array - return (memory, .scalar (.i64 (BitVec.ofNat 64 length)) :: rest) - | .referenceArrayLoad, .scalar index :: .object array :: rest => - let some index := arrayIndex index | .error .invalidState - let (slot, _) ← referenceArraySlot memory array index - return (memory, (← loadSlot memory slot) :: rest) - | .referenceArrayStore, .object value :: .scalar index :: .object array :: rest => - let some index := arrayIndex index | .error .invalidState - let (slot, element) ← referenceArraySlot memory array index - let _ ← storableIn memory element value - return (← storeSlot memory slot value, rest) - | .arrayField owner offset element, .object reference :: rest => - let address ← objectFieldAddress memory reference owner offset - return (memory, (← loadArraySlot memory address element) :: rest) - | .setArrayField owner offset element, .array view :: .object reference :: rest => + | .array element, .array view :: .object reference :: rest => let address ← objectFieldAddress memory reference owner offset return (← storeArraySlot memory address view element, rest) | _, _ => .error .invalidState @@ -1051,7 +1176,7 @@ def stringLiteralInstruction (text : String) (stack : List Value) (memory : Memo def throwIfNullInstruction (stack : List Value) (memory : Memory) : ExecutionResult (Memory × List Value) := match stack with - | .object _ :: .object .null :: _ => .error (.thrown "System.ArgumentNullException") + | .object _ :: .object .null :: _ => .error (.thrown .argumentNull) | .object _ :: .object (.address _) :: rest => .ok (memory, rest) | _ => .error .invalidState @@ -1065,7 +1190,7 @@ def zeroGuardInstruction (guard : ZeroGuard) (width : Nat) (signed : Bool) match guardOperand width signed value with | some number => match guard.throws number with - | true => .error (.thrown "System.ArgumentOutOfRangeException") + | true => .error (.thrown .argumentOutOfRange) | false => .ok (memory, rest) | none => .error .invalidState | _ => .error .invalidState @@ -1080,15 +1205,49 @@ def comparisonGuardInstruction (guard : ComparisonGuard) (width : Nat) (signed : match guardOperand width signed value, guardOperand width signed other with | some number, some bound => match guard.throws number bound with - | true => .error (.thrown "System.ArgumentOutOfRangeException") + | true => .error (.thrown .argumentOutOfRange) | false => .ok (memory, rest) | _, _ => .error .invalidState | _ => .error .invalidState +/-- CoreCLR's `CreateSpan` throws unless the field data is aligned to `sizeof(T)`, so a typed + span needs `width` to divide the allocation's guaranteed alignment. -/ +def staticSpanAligned (allocation : Allocation) (width : Nat) : Bool := + 0 < width && allocation.layout.alignment % width == 0 + +/-- Spans over extracted static data. A byte span from a pointer must lie in a live immutable + static allocation, or be empty over null; a typed span covers a whole static field whose + alignment its element width divides. Memory is never changed. -/ +def staticSpanInstruction (operation : CIL.MemoryOp) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := do + match operation, stack with + | .spanCreate, .scalar (.i32 length) :: .reference (.address reference) :: rest => + if length.toInt < 0 then .error .invalidState else pure () + let allocation ← checkedAt (.address reference) length.toNat + (liveAllocation memory reference.allocation) + if allocation.kind == .immutableStatic then pure () else + .error (.unsupported "Span pointer constructor outside extracted static data") + let _ ← formValue memory (.address reference) + if reference.offset + length.toNat ≤ allocation.layout.size then pure () else + .error (.memory .outsideAllocation (.address reference) length.toNat) + return (memory, .span (.address reference) length.toNat :: rest) + | .createSpan width, .reference (.address reference) :: rest => + let allocation ← checkedAt (.address reference) 0 + (liveAllocation memory reference.allocation) + if allocation.kind == .immutableStatic && reference.offset == 0 && 0 < width && + allocation.layout.size % width == 0 then + if staticSpanAligned allocation width then + return (memory, .span (.address reference) (allocation.layout.size / width) :: rest) + else .error (.unsupported "Typed span element width does not divide the field alignment") + else .error (.unsupported "Typed span outside a whole extracted static field") + | .spanCreate, .scalar (.i32 length) :: .reference .null :: rest => + if length == 0 then return (memory, .span .null 0 :: rest) + else .error (.memory .nullDereference .null length.toNat) + | _, _ => .error .invalidState + /-- Runs a `.memory` operation on the stack (top first), giving the new memory and stack. - Operands with unspecified bits are rejected first. The static-field and span-construction - operations need extracted metadata, so `staticInstruction` runs them and they are - unsupported here; any other operand shape is `invalidState`. -/ + Operands with unspecified bits are rejected first. The span constructors over static storage + run in `staticSpanInstruction`; any other operand shape is `invalidState`. -/ def memoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memory : Memory) : ExecutionResult (Memory × List Value) := do if unspecifiedOperands operation.argumentCount stack then @@ -1099,7 +1258,6 @@ def memoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memory : | .pointerTransfer kind alignment store => pointerTransferInstruction kind alignment store stack memory | .vector128Unsafe store offset => vector128UnsafeInstruction store offset stack memory - | .arrayTransfer element store => arrayTransferInstruction element store stack memory | .binaryRead width signed bigEndian trying => binaryTransferInstruction width signed bigEndian true trying stack memory | .binaryWrite width bigEndian trying => @@ -1110,43 +1268,127 @@ def memoryInstruction (operation : CIL.MemoryOp) (stack : List Value) (memory : | .spanFill width clear => spanFillInstruction width clear stack memory | .arrayFill element counted => arrayFillInstruction element counted stack memory | .bigMul64 _ | .divRemOut _ => wideArithmeticInstruction operation stack memory - | .objectField .. | .setObjectField .. - | .objectReferenceField .. | .setObjectReferenceField .. - | .referenceArrayLength | .referenceArrayLoad | .referenceArrayStore - | .arrayField .. | .setArrayField .. => - objectFieldInstruction operation stack memory - | .stringLiteral text => stringLiteralInstruction text stack memory + | .spanCreate | .createSpan _ => staticSpanInstruction operation stack memory | .throwIfNull => throwIfNullInstruction stack memory | .zeroGuard guard width signed => zeroGuardInstruction guard width signed stack memory | .comparisonGuard guard width signed => comparisonGuardInstruction guard width signed stack memory | _ => otherMemoryInstruction operation stack memory -/-- Runs an op on the stack (top first) and memory: loads, stores, typed copies, aggregate - fields, vector lanes, constants and stack shuffles. `step` falls back to it for ops with - neither their own arm nor a value-only meaning; a `.memory` op goes to `memoryInstruction` - without static-field metadata, and any other op is unsupported here. -/ -def instruction (op : CIL.Op) (stack : List Value) (m : Memory) : +/-- The external copy helper performs the same checked transfer as `cpblk`. -/ +theorem blockInstruction_copy (alignment : Nat) (stack : List Value) (memory : Memory) : + memoryInstruction (.copyBlock alignment) stack memory = + (if unspecifiedOperands 3 stack then .error (.unsupported "Memory operation observes unspecified vector bits") + else copyBlockBytes alignment stack memory) := by rfl +#print axioms blockInstruction_copy + +/-- The external initialization helper performs the same checked transfer as `initblk`. -/ +theorem blockInstruction_init (alignment : Nat) (stack : List Value) (memory : Memory) : + memoryInstruction (.initBlock alignment) stack memory = + (if unspecifiedOperands 3 stack then .error (.unsupported "Memory operation observes unspecified vector bits") + else initBlockBytes alignment stack memory) := by rfl +#print axioms blockInstruction_init + +/-- Copies initialized bytes between writable-reference stack operands without partial overlap. -/ +def copyInitializedBytes (size : Nat) (stack : List Value) (memory : Memory) : ExecutionResult (Memory × List Value) := do - if unspecifiedOperands (observerCount op) stack then - .error (.unsupported "Instruction observes unspecified vector bits") else - match op, stack with - -- Typed copies and scalar and aggregate memory - | .copyObject size, .reference source :: .reference destination :: rest => + match stack with + | .reference source :: .reference destination :: rest => let src ← referenceAt source let dst ← referenceAt destination if src.allocation = dst.allocation && src.offset != dst.offset && src.offset < dst.offset + size && dst.offset < src.offset + size then .error (.unsupported "Partially overlapping typed copy") else - let bytes ← checkedAt source size (read m src size ordinaryAccessAlignment) - return (← checkedAt destination size (write m dst bytes ordinaryAccessAlignment), rest) - | .loadScalar width signed, .reference r :: rest - | .loadScalar width signed, .readOnlyReference r :: rest => + let bytes ← checkedAt source size (read memory src size ordinaryAccessAlignment) + return (← checkedAt destination size (write memory dst bytes ordinaryAccessAlignment), rest) + | _ => .error (.unsupported "Not a memory instruction") + +/-- Writes initialized zeros through the reference on top of the stack. -/ +def initializeBytes (size : Nat) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := do + match stack with + | .reference r :: rest => + let address ← referenceAt r + return (← checkedAt r size + (write memory address (List.replicate size 0) ordinaryAccessAlignment), rest) + | _ => .error (.unsupported "Not a memory instruction") + +/-- Form a struct field address at its checked offset, preserving the receiver's writability. + A null receiver faults before pointer arithmetic. -/ +def structFieldAddress (offset : Nat) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := do + if unspecifiedOperands 1 stack then + .error (.unsupported "Instruction observes unspecified vector bits") else + match stack with + | .reference (.address reference) :: rest => + let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) + return (memory, .reference (.address address) :: rest) + | .readOnlyReference (.address reference) :: rest => + let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) + return (memory, .readOnlyReference (.address address) :: rest) + | .reference .null :: _ | .readOnlyReference .null :: _ => + .error (.memory .nullDereference .null 0) + | _ => .error (.unsupported "Not a memory instruction") + +/-- Read the nested field of a struct value, snapshot or reference at the checked offset. + A copy preserves the initialization flags of padding bytes. -/ +def loadAggregateField (offset size : Nat) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := do + if unspecifiedOperands 1 stack then + .error (.unsupported "Instruction observes unspecified vector bits") else + match stack with + | .scalar (.aggregate bytes) :: rest => + if offset + size > bytes.length then .error .invalidState else + return (memory, .scalar (.aggregate ((bytes.drop offset).take size)) :: rest) + | .scalar (.snapshot (.aggregate total) bytes) :: rest => + if bytes.length != total || offset + size > total then .error .invalidState else + return (memory, .scalar (snapshotValue (.aggregate size) ((bytes.drop offset).take size)) :: rest) + | .reference reference :: rest + | .readOnlyReference reference :: rest => + let r ← referenceAt reference + let address ← checkedAt (.address r) 0 (add memory r 1 (BitVec.ofNat 64 offset)) + -- The aggregate snapshot preserves the initialization flags of padding bytes. + if let .error .uninitialized := read memory address size ordinaryAccessAlignment then + return (memory, .scalar (← loadSnapshot memory (.address address) (.aggregate size)) :: rest) + let bytes ← checkedAt (.address address) size (read memory address size ordinaryAccessAlignment) + return (memory, .scalar (.aggregate bytes) :: rest) + | _ => .error (.unsupported "Not a memory instruction") + +/-- Write a nested struct or snapshot at the checked field displacement. + The copied bytes retain their initialization flags and the receiver must be writable. -/ +def storeAggregateField (offset size : Nat) (stack : List Value) (m : Memory) : + ExecutionResult (Memory × List Value) := do + if unspecifiedOperands 2 stack then + .error (.unsupported "Instruction observes unspecified vector bits") else + match stack with + | .scalar (.aggregate bytes) :: .reference reference :: rest => + if bytes.length != size then .error .invalidState else + let r ← referenceAt reference + let address ← checkedAt (.address r) 0 (add m r 1 (BitVec.ofNat 64 offset)) + return (← checkedAt (.address address) size + (write m address bytes ordinaryAccessAlignment), rest) + | .scalar (.snapshot (.aggregate width) bytes) :: .reference reference :: rest => + if bytes.length != size || width != size then .error .invalidState else + let r ← referenceAt reference + let address ← checkedAt (.address r) 0 (add m r 1 (BitVec.ofNat 64 offset)) + return (← checkedAt (.address address) size + (writeSnapshot m address bytes ordinaryAccessAlignment), rest) + | _ :: .readOnlyReference r :: _ => + .error (.memory .unwritable r size) + | _ => .error (.unsupported "Not a memory instruction") + +/-- Apply a scalar, aggregate or vector transfer to its evaluated stack operands. Callers + check observation of unspecified operands before executing the transfer. -/ +def transferValues (kind : TransferKind) (store : Bool) (stack : List Value) (m : Memory) : + ExecutionResult (Memory × List Value) := do + match kind, store, stack with + | .scalar width signed, false, .reference r :: rest + | .scalar width signed, false, .readOnlyReference r :: rest => if width = 1 || width = 2 || width = 4 || width = 8 then return (m, .scalar (← loadValue m r width signed) :: rest) else .error (.unsupported "Scalar memory width") - | .storeScalar width, .scalar value :: .reference r :: rest => + | .scalar width _, true, .scalar value :: .reference r :: rest => let number ← match width, value with | 1, .i32 bits | 2, .i32 bits | 4, .i32 bits => .ok bits.toNat | 8, .i64 bits => .ok bits.toNat @@ -1154,34 +1396,43 @@ def instruction (op : CIL.Op) (stack : List Value) (m : Memory) : let address ← referenceAt r return (← checkedAt r width (write m address (numberBytes number width) ordinaryAccessAlignment), rest) - | .storeScalar width, _ :: .readOnlyReference r :: _ => + | .scalar width _, true, _ :: .readOnlyReference r :: _ => .error (.memory .unwritable r width) - | .loadAggregate size, .reference r :: rest | .loadAggregate size, .readOnlyReference r :: rest => + | .snapshot (.aggregate size), false, .reference r :: rest | .snapshot (.aggregate size), false, .readOnlyReference r :: rest => if let .error .uninitialized := dereference m r size ordinaryAccessAlignment then return (m, .scalar (← loadSnapshot m r (.aggregate size)) :: rest) let bytes ← checkedAt r size (dereference m r size ordinaryAccessAlignment) return (m, .scalar (.aggregate bytes) :: rest) - | .storeAggregate size, .scalar (.aggregate bytes) :: .reference r :: rest => + | .snapshot (.aggregate size), true, .scalar (.aggregate bytes) :: .reference r :: rest => if bytes.length != size then .error .invalidState else let address ← referenceAt r return (← checkedAt r size (write m address bytes ordinaryAccessAlignment), rest) - | .storeAggregate size, .scalar (.snapshot (.aggregate width) bytes) :: .reference r :: rest => + | .snapshot (.aggregate size), true, .scalar (.snapshot (.aggregate width) bytes) :: .reference r :: rest => if bytes.length != size || width != size then .error .invalidState else let address ← referenceAt r return (← checkedAt r size (writeSnapshot m address bytes ordinaryAccessAlignment), rest) - | .initAggregate size, .reference r :: rest => - let address ← referenceAt r - return (← checkedAt r size - (write m address (List.replicate size 0) ordinaryAccessAlignment), rest) - -- Fields - | .fieldAddress offset, .reference (.address r) :: rest => - let address ← checkedAt (.address r) 0 (add m r 1 (BitVec.ofNat 64 offset)) - return (m, .reference (.address address) :: rest) - -- A field of a readonly reference keeps the restriction, as `readonly. ldelema` results do. - | .fieldAddress offset, .readOnlyReference (.address r) :: rest => - let address ← checkedAt (.address r) 0 (add m r 1 (BitVec.ofNat 64 offset)) - return (m, .readOnlyReference (.address address) :: rest) - | .fieldAt offset width signed, .scalar (.aggregate bytes) :: rest => + | .snapshot .vector128, false, _ => memoryInstruction .load128 stack m + | .snapshot .vector128, true, _ => memoryInstruction .store128 stack m + | .snapshot .vector256, false, _ => memoryInstruction .load256 stack m + | .snapshot .vector256, true, _ => memoryInstruction .store256 stack m + | .snapshot .vector512, false, _ => memoryInstruction .load512 stack m + | .snapshot .vector512, true, _ => memoryInstruction .store512 stack m + | _, _, _ => .error (.unsupported "Not a memory instruction") + +/-- A typed transfer observes the reference and, for a store, its value. -/ +def observedTransfer (kind : TransferKind) (store : Bool) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := + if unspecifiedOperands (if store then 2 else 1) stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else transferValues kind store stack memory + +/-- Reads a primitive or vector struct field at its checked offset from a value or reference. -/ +def loadValueField (offset width : Nat) (signed : Bool) (stack : List Value) (m : Memory) : + ExecutionResult (Memory × List Value) := do + if unspecifiedOperands 1 stack then + .error (.unsupported "Instruction observes unspecified vector bits") else + match stack with + | .scalar (.aggregate bytes) :: rest => if offset + width > bytes.length then .error .invalidState else let number := byteNumber ((bytes.drop offset).take width) let value ← match width with @@ -1195,43 +1446,50 @@ def instruction (op : CIL.Op) (stack : List Value) (m : Memory) : | 32 => .ok (.v256 (BitVec.ofNat 256 number)) | _ => .error (.unsupported "Aggregate field width") return (m, .scalar value :: rest) - | .fieldAt offset width _, .scalar (.snapshot (.aggregate size) bytes) :: rest => + | .scalar (.snapshot (.aggregate size) bytes) :: rest => if bytes.length != size || offset + width > size then .error .invalidState else if width = 16 || width = 32 then let value ← bitcastValue (.aggregate width) (if width = 16 then .vector128 else .vector256) (.snapshot (.aggregate width) ((bytes.drop offset).take width)) return (m, .scalar value :: rest) - else .error (.unsupported "Snapshot field width") - | .aggregateFieldAt offset size, .scalar (.aggregate bytes) :: rest => - if offset + size > bytes.length then .error .invalidState else - return (m, .scalar (.aggregate ((bytes.drop offset).take size)) :: rest) - | .aggregateFieldAt offset size, .reference reference :: rest - | .aggregateFieldAt offset size, .readOnlyReference reference :: rest => - let r ← referenceAt reference - let address ← checkedAt (.address r) 0 (add m r 1 (BitVec.ofNat 64 offset)) - let bytes ← checkedAt (.address address) size (read m address size ordinaryAccessAlignment) - return (m, .scalar (.aggregate bytes) :: rest) - | .setAggregateFieldAt offset size, .scalar (.aggregate bytes) :: .reference reference :: rest => - if bytes.length != size then .error .invalidState else - let r ← referenceAt reference - let address ← checkedAt (.address r) 0 (add m r 1 (BitVec.ofNat 64 offset)) - return (← checkedAt (.address address) size - (write m address bytes ordinaryAccessAlignment), rest) - | .fieldAt offset width signed, .reference (.address r) :: rest - | .fieldAt offset width signed, .readOnlyReference (.address r) :: rest => + -- A scalar field is read only when its own bytes are initialized; padding need not be. + else if ((bytes.drop offset).take width).all Prod.snd then + let number := byteNumber (((bytes.drop offset).take width).map Prod.fst) + let value ← match width with + | 1 => .ok (.i32 (if signed then (BitVec.ofNat 8 number).signExtend 32 + else BitVec.ofNat 32 number)) + | 2 => .ok (.i32 (if signed then (BitVec.ofNat 16 number).signExtend 32 + else BitVec.ofNat 32 number)) + | 4 => .ok (.i32 (BitVec.ofNat 32 number)) + | 8 => .ok (.i64 (BitVec.ofNat 64 number)) + | _ => .error (.unsupported "Snapshot field width") + return (m, .scalar value :: rest) + else .error (.unsupported "Uninitialized snapshot field") + | .reference (.address r) :: rest + | .readOnlyReference (.address r) :: rest => let address ← checkedAt (.address r) 0 (add m r 1 (BitVec.ofNat 64 offset)) return (m, .scalar (← loadValue m (.address address) width signed) :: rest) - | .setFieldAt offset 16, .scalar value :: .reference (.address r) :: rest => + | .reference .null :: _ | .readOnlyReference .null :: _ => + .error (.memory .nullDereference .null 0) + | _ => .error (.unsupported "Not a memory instruction") + +/-- Writes a primitive or vector struct field at its checked offset through a writable reference. -/ +def storeValueField (offset width : Nat) (stack : List Value) (m : Memory) : + ExecutionResult (Memory × List Value) := do + if unspecifiedOperands 2 stack then + .error (.unsupported "Instruction observes unspecified vector bits") else + match width, stack with + | 16, .scalar value :: .reference (.address r) :: rest => let some bytes := valueSnapshot .vector128 value | .error .invalidState let address ← checkedAt (.address r) 0 (add m r 1 (BitVec.ofNat 64 offset)) return (← checkedAt (.address address) 16 (writeSnapshot m address bytes ordinaryAccessAlignment), rest) - | .setFieldAt offset 32, .scalar value :: .reference (.address r) :: rest => + | 32, .scalar value :: .reference (.address r) :: rest => let some bytes := valueSnapshot .vector256 value | .error .invalidState let address ← checkedAt (.address r) 0 (add m r 1 (BitVec.ofNat 64 offset)) return (← checkedAt (.address address) 32 (writeSnapshot m address bytes ordinaryAccessAlignment), rest) - | .setFieldAt offset width, .scalar value :: .reference (.address r) :: rest => + | width, .scalar value :: .reference (.address r) :: rest => let number ← match width, value with | 1, .i32 bits => .ok bits.toNat | 2, .i32 bits => .ok bits.toNat @@ -1242,49 +1500,74 @@ def instruction (op : CIL.Op) (stack : List Value) (m : Memory) : return (← checkedAt (.address address) width (write m address (numberBytes number width) ordinaryAccessAlignment), rest) -- Field stores need write permission, however the backing allocation may be written. - | .setFieldAt _ width, _ :: .readOnlyReference r :: _ => .error (.memory .unwritable r width) - | .setAggregateFieldAt _ size, _ :: .readOnlyReference r :: _ => - .error (.memory .unwritable r size) - -- Field and lane accesses through null fault like `referenceAt`, before any offset is formed. - | .fieldAt .., .reference .null :: _ | .fieldAddress _, .reference .null :: _ - | .fieldAt .., .readOnlyReference .null :: _ | .fieldAddress _, .readOnlyReference .null :: _ - | .lane _, .reference .null :: _ | .laneAddress _, .reference .null :: _ - | .setFieldAt .., .scalar _ :: .reference .null :: _ - | .setLane _, .scalar _ :: .reference .null :: _ => - .error (.memory .nullDereference .null 0) - -- Memory operations, constants and 64-bit words - | .memory operation, _ => memoryInstruction operation stack m - | .const32 bits, _ => return (m, .scalar (.i32 bits) :: stack) - | .const64 bits, _ => return (m, .scalar (.i64 bits) :: stack) - | .load64, .reference r :: rest | .load64, .readOnlyReference r :: rest => - return (m, .scalar (← loadValue m r 8) :: rest) - | .store64, .scalar (.i64 bits) :: .reference r :: rest => + | width, _ :: .readOnlyReference r :: _ => .error (.memory .unwritable r width) + | _, .scalar _ :: .reference .null :: _ => .error (.memory .nullDereference .null 0) + | _, _ => .error (.unsupported "Not a memory instruction") + +/-- Runs loads, stores, vector lanes, constants and stack shuffles on the stack and memory. `step` falls back to it for ops with + neither their own arm nor a value-only meaning; a `.memory` op goes to `memoryInstruction` + without static-field metadata, and any other op is unsupported here. -/ +def instruction (op : CIL.Instruction) (stack : List Value) (m : Memory) : + ExecutionResult (Memory × List Value) := do + if unspecifiedOperands (observerCount op) stack then + .error (.unsupported "Instruction observes unspecified vector bits") else + match op, stack with + -- Memory operations and constants + | .ldc_i4 bits, _ => return (m, .scalar (.i32 bits) :: stack) + | .ldc_i8 bits, _ => return (m, .scalar (.i64 bits) :: stack) + | .ldnull, _ => return (m, .object .null :: stack) + -- Indirect integer loads and stores; a native load from a method table reads its entry. + | .ldind .i, .methodTable table :: rest => + let pointer ← checkedAt .null nativeMethodWidth (loadNativeTable table m) + return (m, .methodPointer pointer :: rest) + | .ldind kind, .reference r :: rest | .ldind kind, .readOnlyReference r :: rest => + return (m, .scalar (← loadValue m r kind.width kind.signed) :: rest) + | .stind .i8, .scalar (.i64 bits) :: .reference r :: rest + | .stind .i, .scalar (.i64 bits) :: .reference r :: rest => return (← storeValue m r (.i64 bits), rest) - | .store64, _ :: .readOnlyReference r :: _ => .error (.memory .unwritable r 8) - -- Vector lanes - | .lane index, .reference (.address r) :: rest => - let address ← checkedAt (.address r) 0 (add m r 8 (BitVec.ofNat 64 index.val)) - return (m, .scalar (← loadValue m (.address address) 8) :: rest) - | .lane index, .scalar (.v256 bits) :: rest => - return (m, .scalar (.i64 (CIL.Vector.lane64 bits index.val)) :: rest) - | .laneAddress index, .reference (.address r) :: rest => - let address ← checkedAt (.address r) 0 (add m r 8 (BitVec.ofNat 64 index.val)) - return (m, .reference (.address address) :: rest) - | .setLane index, .scalar (.i64 bits) :: .reference (.address r) :: rest => - let address ← checkedAt (.address r) 0 (add m r 8 (BitVec.ofNat 64 index.val)) - return (← storeValue m (.address address) (.i64 bits), rest) + -- An eight-byte store takes an i64, never the bytes of an i32. + | .stind .i8, .scalar (.i32 _) :: .reference _ :: _ + | .stind .i, .scalar (.i32 _) :: .reference _ :: _ => .error .invalidState + | .stind kind, .scalar (.i32 bits) :: .reference r :: rest => + let address ← referenceAt r + return (← checkedAt r kind.width + (write m address (numberBytes bits.toNat kind.width) ordinaryAccessAlignment), rest) + | .stind kind, _ :: .readOnlyReference r :: _ => .error (.memory .unwritable r kind.width) + -- Array lengths and elements, and the two reads of a managed handler table. + | .ldlen, _ | .ldelem _, _ | .ldelem_ref, _ | .stelem _, _ | .stelem_ref, _ => + elementInstruction op stack m -- References and stack shuffles - | .skipInit, .reference r :: rest => - let _ ← formValue m r - return (m, rest) - | .asRef, .reference r :: rest => return (m, (← formValue m r) :: rest) | .dup, value :: rest => return (m, value :: value :: rest) | .pop, _ :: rest => return (m, rest) | _, _ => .error (.unsupported "Not a memory instruction") +/-- Loads or stores a typed primitive, struct or vector using its checked storage shape. + A span load accepts only a span value, which stays in place (`step` has already read a home + as its slot's view); a null reference faults as a null dereference and any other operand is + invalid. -/ +def transferObject (shape : ValueShape) (store : Bool) (stack : List Value) (memory : Memory) : + ExecutionResult (Memory × List Value) := + match shape, store with + | .primitive width signed, false => observedTransfer (.scalar width signed) false stack memory + | .primitive width _, true => observedTransfer (.scalar width false) true stack memory + | .aggregate size, store => observedTransfer (.snapshot (.aggregate size)) store stack memory + | .vector 16, false => memoryInstruction .load128 stack memory + | .vector 32, false => memoryInstruction .load256 stack memory + | .vector 64, false => memoryInstruction .load512 stack memory + | .vector 16, true => memoryInstruction .store128 stack memory + | .vector 32, true => memoryInstruction .store256 stack memory + | .vector 64, true => memoryInstruction .store512 stack memory + | .span, false => match stack with + | .bufferSpan _ :: _ | .span .. :: _ => .ok (memory, stack) + | .reference .null :: _ | .readOnlyReference .null :: _ => + .error (.memory .nullDereference .null 0) + | _ => .error .invalidState + | .unsupported reason, _ => .error (.unsupported reason) + | _, _ => .error (.unsupported "Object transfer shape") + /-- Runs `ops` in order with `instruction`, stopping at the first fault. For straight-line examples only: it has no branches, calls or frames, which `run` handles. -/ -def instructions (ops : List CIL.Op) (stack : List Value) (m : Memory) : +def instructions (ops : List CIL.Instruction) (stack : List Value) (m : Memory) : ExecutionResult (Memory × List Value) := do match ops with | [] => return (m, stack) @@ -1300,47 +1583,8 @@ structure LocatedFault where deriving DecidableEq, Repr /-- `instruction`, with a fault located at `method` and `pc`. -/ -def instructionAt (method pc : Nat) (op : CIL.Op) (stack : List Value) (memory : Memory) : +def instructionAt (method pc : Nat) (op : CIL.Instruction) (stack : List Value) (memory : Memory) : Except LocatedFault (Memory × List Value) := (instruction op stack memory).mapError fun fault => ⟨method, pc, fault⟩ -/-- Runs a straight-line body. A plain `arg` pushes the checked argument, and `ret` returns - the stack when it holds exactly the declared number of results; running off the end is - `invalidState`. Every other op goes through `instruction`, so branches and calls are - unsupported here (`run` handles them). -/ -def block (returns : Bool) (args : List Value) (ops : List CIL.Op) - (stack : List Value) (m : Memory) : ExecutionResult (Memory × List Value) := do - match ops with - | [] => .error .invalidState - | .arg index none :: rest => - let some value := args[index]? | .error .invalidState - let value ← match value with - | .reference reference => formValue m reference - | .scalar value => if value.initialized then .ok (.scalar value) else .error .invalidState - | .span reference length => .ok (.span reference length) - | .methodPointer pointer => .ok (.methodPointer pointer) - | .methodTable table => .ok (.methodTable table) - | .object reference => formObject m reference - | .array view => do - let _ ← checkedAt view.reference 0 (view.validate m) - pure (.array view) - | .bufferSpan view => do - let _ ← checkedAt view.reference 0 (view.validate m) - pure (.bufferSpan view) - | .readOnlyReference reference => do - let _ ← formValue m reference - pure (.readOnlyReference reference) - block returns args rest (value :: stack) m - | .ret none :: _ => - if stack.length = (if returns then 1 else 0) then return (m, stack) - else .error .invalidState - | op :: rest => - let (memory, values) ← instruction op stack m - block returns args rest values memory - -/-- `block` on a method's code, from an empty stack. -/ -def methodBlock (body : CIL.Method) (args : List Value) (memory : Memory) : - ExecutionResult (Memory × List Value) := - block body.returnsValue args body.code [] memory - end CIL.Safety diff --git a/CIL/Safety/InstructionMemoryEquivalence.lean b/CIL/Safety/InstructionMemoryEquivalence.lean index 06fee12e2..c2a2c2b07 100644 --- a/CIL/Safety/InstructionMemoryEquivalence.lean +++ b/CIL/Safety/InstructionMemoryEquivalence.lean @@ -1,4 +1,5 @@ import CIL.Safety.FrameEquivalence +import CIL.Safety.NativeMethodTableEquivalence /-! # Equivalent memories run instructions alike @@ -232,14 +233,15 @@ theorem Memory.Equivalent.storeArraySlot {m n : Memory} (same : m.Equivalent n) #print axioms Memory.Equivalent.storeArraySlot /-- Non-fill memory operations preserve equivalent memories and identical values. -/ -private theorem Memory.Equivalent.otherMemoryInstruction {m n : Memory} (same : m.Equivalent n) +theorem Memory.Equivalent.otherMemoryInstruction {m n : Memory} (same : m.Equivalent n) (operation : CIL.MemoryOp) (stack : List Value) : ResultEquivalent MemoryFirstEquivalent (otherMemoryInstruction operation stack m) (otherMemoryInstruction operation stack n) := by unfold CIL.Safety.otherMemoryInstruction + simp only [copyBlockBytes, initBlockBytes] split <;> try simp only [same.read, same.access, same.loadValue, same.formValue, same.add, same.referenceDifference, same.spanValidate, same.spanSlice, same.spanElement, - same.arrayValidate, same.arrayElement, same.loadTransfer] + same.loadTransfer] all_goals repeat first | exact .error _ | exact .ok ⟨same, rfl⟩ @@ -283,14 +285,53 @@ private theorem Memory.Equivalent.wideArithmeticInstruction {m n : Memory} (same | split #print axioms Memory.Equivalent.wideArithmeticInstruction -/-- Object field accesses locate the same field and perform equivalent checked accesses. -/ -private theorem Memory.Equivalent.objectFieldInstruction {m n : Memory} (same : m.Equivalent n) - (operation : CIL.MemoryOp) (stack : List Value) : - ResultEquivalent MemoryFirstEquivalent (objectFieldInstruction operation stack m) - (objectFieldInstruction operation stack n) := by - unfold CIL.Safety.objectFieldInstruction +/-- Array lengths and element accesses, and the two reads of a handler table, locate the same + element and perform equivalent checked accesses. -/ +private theorem Memory.Equivalent.elementInstruction {m n : Memory} + (same : m.Equivalent n) (op : CIL.Instruction) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (elementInstruction op stack m) + (elementInstruction op stack n) := by + unfold CIL.Safety.elementInstruction CIL.Safety.arrayLoad CIL.Safety.arrayStore + CIL.Safety.methodTableElement + split <;> try simp only [same.loadSlot, same.referenceArrayOf, same.referenceArraySlot, + same.storableIn, same.loadValue, same.arrayValidate, same.arrayElement, same.loadNativeTable] + all_goals repeat first + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | (apply ResultEquivalent.bind (same.storeSlot _ _) + intro left right related + exact .ok ⟨related, rfl⟩) + | (apply ResultEquivalent.bind ((same.write _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | split +#print axioms Memory.Equivalent.elementInstruction + +/-- Class field loads locate the same field and read equivalent memories alike. -/ +theorem Memory.Equivalent.classFieldLoad {m n : Memory} (same : m.Equivalent n) + (owner : CIL.Extraction.TypeExpr) (offset : Nat) (slot : CIL.ClassSlot) + (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (classFieldLoad owner offset slot stack m) + (classFieldLoad owner offset slot stack n) := by + unfold CIL.Safety.classFieldLoad split <;> try simp only [same.objectFieldAddress, same.loadValue, same.loadSlot, - same.referenceArrayOf, same.referenceArraySlot, same.storableIn, same.loadArraySlot] + same.loadArraySlot] + all_goals repeat first + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | split +#print axioms Memory.Equivalent.classFieldLoad + +/-- Class field stores locate the same field and perform equivalent checked writes. -/ +theorem Memory.Equivalent.classFieldStore {m n : Memory} (same : m.Equivalent n) + (owner : CIL.Extraction.TypeExpr) (offset : Nat) (slot : CIL.ClassSlot) + (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (classFieldStore owner offset slot stack m) + (classFieldStore owner offset slot stack n) := by + unfold CIL.Safety.classFieldStore + split <;> try simp only [same.objectFieldAddress] all_goals repeat first | exact .error _ | exact .ok ⟨same, rfl⟩ @@ -305,10 +346,10 @@ private theorem Memory.Equivalent.objectFieldInstruction {m n : Memory} (same : intro left right related exact .ok ⟨related, rfl⟩) | split -#print axioms Memory.Equivalent.objectFieldInstruction +#print axioms Memory.Equivalent.classFieldStore /-- A string literal loads the same bound object from equivalent memories. -/ -private theorem Memory.Equivalent.stringLiteralInstruction {m n : Memory} (same : m.Equivalent n) +theorem Memory.Equivalent.stringLiteralInstruction {m n : Memory} (same : m.Equivalent n) (text : String) (stack : List Value) : ResultEquivalent MemoryFirstEquivalent (stringLiteralInstruction text stack m) (stringLiteralInstruction text stack n) := by @@ -417,6 +458,46 @@ theorem Memory.Equivalent.arrayTransferInstruction {m n : Memory} (same : m.Equi | split #print axioms Memory.Equivalent.arrayTransferInstruction +/-- A typed element access takes the same access from its element's shape and performs it alike + over equivalent memories. -/ +theorem Memory.Equivalent.typedElementInstruction {m n : Memory} (same : m.Equivalent n) + (shape : CIL.ElementShape) (store : Bool) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (typedElementInstruction shape store stack m) + (typedElementInstruction shape store stack n) := by + unfold CIL.Safety.typedElementInstruction + split + any_goals split + any_goals exact same.arrayTransferInstruction _ _ _ + all_goals (try unfold CIL.Safety.arrayLoad); (try unfold CIL.Safety.arrayStore) + all_goals try unfold CIL.Safety.methodTableElement + all_goals try simp only [same.loadValue, same.arrayElement, same.loadNativeTable] + all_goals repeat first + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | (apply ResultEquivalent.bind ((same.write _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | split +#print axioms Memory.Equivalent.typedElementInstruction + +/-- An element address takes its element from the shape and gives the same reference over + equivalent memories. -/ +theorem Memory.Equivalent.elementAddressInstruction {m n : Memory} (same : m.Equivalent n) + (shape : CIL.ElementShape) (readOnly : Bool) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (elementAddressInstruction shape readOnly stack m) + (elementAddressInstruction shape readOnly stack n) := by + unfold CIL.Safety.elementAddressInstruction + split + all_goals try unfold CIL.Safety.arrayAddress + all_goals try simp only [same.arrayElement] + all_goals repeat first + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | split +#print axioms Memory.Equivalent.elementAddressInstruction + /-- Aligned transfers retain exact access failures and the existing typed transfer equivalence. -/ theorem Memory.Equivalent.pointerTransferInstruction {m n : Memory} (same : m.Equivalent n) (kind : CIL.TransferKind) (alignment : Nat) (store : Bool) (stack : List Value) : @@ -452,6 +533,20 @@ theorem Memory.Equivalent.vector128UnsafeInstruction {m n : Memory} (same : m.Eq | split #print axioms Memory.Equivalent.vector128UnsafeInstruction +/-- A static span reads only equivalent allocation metadata and leaves memory unchanged. -/ +theorem Memory.Equivalent.staticSpanInstruction {m n : Memory} (same : m.Equivalent n) + (operation : CIL.MemoryOp) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (staticSpanInstruction operation stack m) + (staticSpanInstruction operation stack n) := by + unfold CIL.Safety.staticSpanInstruction + split <;> try simp only [checkedAt, liveAllocation, same.allocations, same.formValue] + all_goals repeat first + | exact .ok ⟨same, rfl⟩ + | exact .error _ + | (apply ResultEquivalent.bind_same; intro) + | split +#print axioms Memory.Equivalent.staticSpanInstruction + /-- Opcode dispatch preserves the memory equivalence of each checked family. -/ theorem Memory.Equivalent.memoryInstruction {m n : Memory} (same : m.Equivalent n) (operation : CIL.MemoryOp) (stack : List Value) : @@ -464,7 +559,6 @@ theorem Memory.Equivalent.memoryInstruction {m n : Memory} (same : m.Equivalent · exact same.referenceAddInstruction _ _ _ · exact same.pointerTransferInstruction _ _ _ _ · exact same.vector128UnsafeInstruction _ _ _ - · exact same.arrayTransferInstruction _ _ _ · exact same.binaryTransferInstruction _ _ _ _ _ _ · exact same.binaryTransferInstruction _ _ _ _ _ _ · exact same.singleSpanInstruction _ _ _ @@ -474,30 +568,126 @@ theorem Memory.Equivalent.memoryInstruction {m n : Memory} (same : m.Equivalent · exact same.arrayFillInstruction _ _ _ · exact same.wideArithmeticInstruction _ _ · exact same.wideArithmeticInstruction _ _ - · exact same.objectFieldInstruction _ _ - · exact same.objectFieldInstruction _ _ - · exact same.objectFieldInstruction _ _ - · exact same.objectFieldInstruction _ _ - · exact same.objectFieldInstruction _ _ - · exact same.objectFieldInstruction _ _ - · exact same.objectFieldInstruction _ _ - · exact same.objectFieldInstruction _ _ - · exact same.objectFieldInstruction _ _ - · exact same.stringLiteralInstruction _ _ + · exact same.staticSpanInstruction _ _ + · exact same.staticSpanInstruction _ _ · exact same.throwIfNullInstruction _ · exact same.zeroGuardInstruction _ _ _ _ · exact same.comparisonGuardInstruction _ _ _ _ · exact same.otherMemoryInstruction _ _ +/-- Equivalent memories return the same value from a checked value field. -/ +theorem Memory.Equivalent.loadValueField {m n : Memory} (same : m.Equivalent n) + (offset width : Nat) (signed : Bool) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (loadValueField offset width signed stack m) + (loadValueField offset width signed stack n) := by + unfold CIL.Safety.loadValueField + simp only [same.add, same.loadValue] + repeat first + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | split +#print axioms Memory.Equivalent.loadValueField + +/-- Equivalent memories preserve equivalence when writing a checked value field. -/ +theorem Memory.Equivalent.storeValueField {m n : Memory} (same : m.Equivalent n) + (offset width : Nat) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (storeValueField offset width stack m) + (storeValueField offset width stack n) := by + unfold CIL.Safety.storeValueField + simp only [same.add] + repeat first + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | (apply ResultEquivalent.bind ((same.write _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | (apply ResultEquivalent.bind ((same.writeSnapshot _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | split +#print axioms Memory.Equivalent.storeValueField + +/-- Equivalent memories read the same nested field from a struct or a reference. -/ +theorem Memory.Equivalent.loadAggregateField {m n : Memory} (same : m.Equivalent n) + (offset size : Nat) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (loadAggregateField offset size stack m) + (loadAggregateField offset size stack n) := by + unfold CIL.Safety.loadAggregateField + simp only [same.add, same.read, same.loadSnapshot] + repeat first + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | split +#print axioms Memory.Equivalent.loadAggregateField + +/-- A nested struct write produces equivalent memories for initialized bytes and snapshots. -/ +theorem Memory.Equivalent.storeAggregateField {m n : Memory} (same : m.Equivalent n) + (offset size : Nat) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (storeAggregateField offset size stack m) + (storeAggregateField offset size stack n) := by + unfold CIL.Safety.storeAggregateField + simp only [same.add] + repeat first + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | (apply ResultEquivalent.bind ((same.write _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | (apply ResultEquivalent.bind ((same.writeSnapshot _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | split +#print axioms Memory.Equivalent.storeAggregateField + +/-- Equivalent memories give the same typed load and equivalent scalar, snapshot and vector stores. -/ +theorem Memory.Equivalent.transferValues {m n : Memory} (same : m.Equivalent n) + (kind : CIL.TransferKind) (store : Bool) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (transferValues kind store stack m) + (transferValues kind store stack n) := by + unfold CIL.Safety.transferValues + split <;> try simp only [same.dereference, same.loadValue, same.loadSnapshot] + all_goals repeat first + | exact same.memoryInstruction _ _ + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | (apply ResultEquivalent.bind ((same.write _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | (apply ResultEquivalent.bind ((same.writeSnapshot _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | split +#print axioms Memory.Equivalent.transferValues + +/-- Equivalent memories share every observable transfer outcome and its operand guard. -/ +theorem Memory.Equivalent.observedTransfer {m n : Memory} (same : m.Equivalent n) + (kind : CIL.TransferKind) (store : Bool) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (observedTransfer kind store stack m) + (observedTransfer kind store stack n) := by + unfold CIL.Safety.observedTransfer + by_cases observed : unspecifiedOperands (if store then 2 else 1) stack = true + · simp only [observed, ite_true] + exact .error _ + · simp only [observed, ite_false] + exact same.transferValues _ _ _ +#print axioms Memory.Equivalent.observedTransfer + theorem Memory.Equivalent.instruction {m n : Memory} (same : m.Equivalent n) - (operation : CIL.Op) (stack : List Value) : + (operation : CIL.Instruction) (stack : List Value) : ResultEquivalent MemoryFirstEquivalent (instruction operation stack m) (instruction operation stack n) := by unfold CIL.Safety.instruction split <;> try simp only [same.read, same.dereference, same.loadValue, same.loadSnapshot, - same.formValue, same.add] + same.formValue, same.add, same.loadNativeTable] all_goals repeat first + | exact same.transferValues _ _ _ | exact same.memoryInstruction _ _ + | exact same.elementInstruction _ _ | exact .error _ | exact .ok ⟨same, rfl⟩ | (apply ResultEquivalent.bind_same; intro) @@ -517,4 +707,59 @@ theorem Memory.Equivalent.instruction {m n : Memory} (same : m.Equivalent n) #print axioms Memory.Equivalent.loadValue #print axioms Memory.Equivalent.storeValue +/-- Equivalent memories give equivalent results for a typed copy. -/ +theorem Memory.Equivalent.copyInitializedBytes {m n : Memory} (same : m.Equivalent n) + (size : Nat) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (copyInitializedBytes size stack m) + (copyInitializedBytes size stack n) := by + unfold CIL.Safety.copyInitializedBytes + split <;> try simp only [same.read] + all_goals repeat first + | exact same.memoryInstruction _ _ + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | (apply ResultEquivalent.bind ((same.write _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | split +#print axioms Memory.Equivalent.copyInitializedBytes + +/-- Equivalent memories give equivalent results for a zero initialization. -/ +theorem Memory.Equivalent.initializeBytes {m n : Memory} (same : m.Equivalent n) + (size : Nat) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (initializeBytes size stack m) + (initializeBytes size stack n) := by + unfold CIL.Safety.initializeBytes + split <;> try simp only [same.read] + all_goals repeat first + | exact same.memoryInstruction _ _ + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | (apply ResultEquivalent.bind ((same.write _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | split +#print axioms Memory.Equivalent.initializeBytes + +/-- Equivalent memories give equivalent results for a typed object transfer. -/ +theorem Memory.Equivalent.transferObject {m n : Memory} (same : m.Equivalent n) + (shape : ValueShape) (store : Bool) (stack : List Value) : + ResultEquivalent MemoryFirstEquivalent (transferObject shape store stack m) + (transferObject shape store stack n) := by + unfold CIL.Safety.transferObject + split <;> try simp only [same.read] + all_goals repeat first + | exact same.memoryInstruction _ _ + | exact same.observedTransfer _ _ _ + | exact .error _ + | exact .ok ⟨same, rfl⟩ + | (apply ResultEquivalent.bind_same; intro) + | (apply ResultEquivalent.bind ((same.write _ _ _).mapError _) + intro left right related + exact .ok ⟨related, rfl⟩) + | split +#print axioms Memory.Equivalent.transferObject + end CIL.Safety diff --git a/CIL/Safety/InstructionMemoryLemmas.lean b/CIL/Safety/InstructionMemoryLemmas.lean index 900d4d9c7..466dfdc63 100644 --- a/CIL/Safety/InstructionMemoryLemmas.lean +++ b/CIL/Safety/InstructionMemoryLemmas.lean @@ -24,7 +24,8 @@ theorem memoryInstruction_emptySpan (memory : Memory) (stack : List Value) rfl #print axioms memoryInstruction_emptySpan -theorem instructions_append (first second : List CIL.Op) (stack : List Value) (m : Memory) : +theorem instructions_append (first second : List CIL.Instruction) (stack : List Value) + (m : Memory) : instructions (first ++ second) stack m = (instructions first stack m).bind (fun (memory, values) => instructions second values memory) := by @@ -39,13 +40,14 @@ theorem instructions_append (first second : List CIL.Op) (stack : List Value) (m exact ih values memory /-- A fault in a prefix of straight-line code is the fault of the whole sequence. -/ -theorem instruction_fault_cannot_be_erased (first second : List CIL.Op) (stack : List Value) +theorem instruction_fault_cannot_be_erased (first second : List CIL.Instruction) + (stack : List Value) (m : Memory) (fault : ExecutionFault) (h : instructions first stack m = .error fault) : instructions (first ++ second) stack m = .error fault := by rw [instructions_append, h] rfl -theorem successful_instruction_prefix (first second : List CIL.Op) (stack : List Value) +theorem successful_instruction_prefix (first second : List CIL.Instruction) (stack : List Value) (m final : Memory) (values : List Value) (h : instructions (first ++ second) stack m = .ok (final, values)) : ∃ intermediate stack', instructions first stack m = .ok (intermediate, stack') := by @@ -59,7 +61,7 @@ theorem successful_instruction_prefix (first second : List CIL.Op) (stack : List /-- Reference validity is independent of readable initialization and access width. A span's accessible bytes are checked by the memory operation. -/ def Value.Valid (m : Memory) : Value → Prop - | .methodPointer _ | .methodTable _ | .object .null => True + | .typeHandle _ | .runtimeType _ | .methodPointer _ | .methodTable _ | .object .null => True | .object (.address r) => formObject m (.address r) = .ok (.object (.address r)) | .scalar value => numericValue value = true | .bufferSpan view => view.validate m = .ok () @@ -68,6 +70,8 @@ def Value.Valid (m : Memory) : Value → Prop | .readOnlyReference (.address r) => form m r = .ok r | .reference .null | .span .null _ => True | .reference (.address r) | .span (.address r) _ => form m r = .ok r + -- A home names a frame slot and addresses no memory; a call checks the view it reads. + | .spanHome _ => True /-- Every value in the list is valid in `m`. -/ def ValuesValid (m : Memory) (values : List Value) : Prop := @@ -350,7 +354,8 @@ theorem formValue_readOnly_valid (m : Memory) (reference : ManagedReference) (va theorem checkedValue_valid (m : Memory) (value result : Value) (h : checkedValue m value = .ok result) : result = value ∧ result.Valid m := by cases value with - | methodPointer _ | methodTable _ => cases h; exact ⟨rfl, trivial⟩ + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ => cases h; exact ⟨rfl, trivial⟩ + | spanHome _ => cases h | object r => exact ⟨formObject_returns_input h, formObject_result_valid h⟩ | scalar value => @@ -390,10 +395,11 @@ theorem checkedValue_valid (m : Memory) (value result : Value) rw [same] at valid cases r <;> exact ⟨rfl, valid⟩ -theorem checkedValue_of_valid (m : Memory) (value : Value) (valid : value.Valid m) : - checkedValue m value = .ok value := by +theorem checkedValue_of_valid (m : Memory) (value : Value) (valid : value.Valid m) + (free : value.isHome = false) : checkedValue m value = .ok value := by cases value with - | methodPointer _ | methodTable _ => rfl + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ => rfl + | spanHome _ => cases free | object r => cases r with | null => rfl @@ -420,7 +426,7 @@ theorem ValuesValid.preserve {m result : Memory} {values : List Value} intro value member have hv := valid value member cases value with - | scalar value | methodPointer _ | methodTable _ => exact hv + | typeHandle _ | runtimeType _ | scalar value | methodPointer _ | methodTable _ | spanHome _ => exact hv | object r => cases r with | null => exact True.intro @@ -467,18 +473,175 @@ theorem checkedValues_valid (m : Memory) (values result : List Value) #print axioms ValuesValid.preserve #print axioms checkedValues_valid -/-- Values valid in `memory` pass the argument and result checks unchanged. -/ +/-- Values valid in `memory`, none of them a home, pass the argument and result checks + unchanged. -/ theorem checkedValues_of_valid {memory : Memory} {values : List Value} - (valid : ValuesValid memory values) : values.mapM (checkedValue memory) = .ok values := by + (valid : ValuesValid memory values) (free : ∀ value ∈ values, value.isHome = false) : + values.mapM (checkedValue memory) = .ok values := by induction values with | nil => rfl | cons value rest restChecked => have head := checkedValue_of_valid memory value (valid value (List.mem_cons_self ..)) - have tail := restChecked fun other member => valid other (List.mem_cons_of_mem _ member) + (free value (List.mem_cons_self ..)) + have tail := restChecked (fun other member => valid other (List.mem_cons_of_mem _ member)) + (fun other member => free other (List.mem_cons_of_mem _ member)) simp only [List.mapM_cons, head, tail, Bind.bind, Except.bind, Pure.pure, Except.pure] #print axioms checkedValues_of_valid +/-- Valid values pass the argument and result checks unchanged, or one of them is a home and + the check faults with `.invalidState`. -/ +theorem checkedValues_of_valid_or_home {memory : Memory} : + ∀ {values : List Value}, ValuesValid memory values → + values.mapM (checkedValue memory) = .ok values ∨ + values.mapM (checkedValue memory) = .error .invalidState + | [], _ => .inl rfl + | value :: rest, valid => by + have restValid : ValuesValid memory rest := + fun other member => valid other (List.mem_cons_of_mem _ member) + cases value with + | spanHome _ => + exact .inr (by simp [List.mapM_cons, checkedValue, Bind.bind, Except.bind]) + | _ => + have head := checkedValue_of_valid memory _ (valid _ (List.mem_cons_self ..)) rfl + rcases checkedValues_of_valid_or_home restValid with tail | tail <;> + simp [List.mapM_cons, head, tail, Bind.bind, Except.bind, Pure.pure, Except.pure] + +#print axioms checkedValues_of_valid_or_home + +/-- A value `checkedValue` accepts is never a home, so no checked value names a frame slot. -/ +theorem checkedValue_not_spanHome {m : Memory} {value result : Value} + (checked : checkedValue m value = .ok result) : result.isHome = false := by + obtain ⟨same, _⟩ := checkedValue_valid m value result checked + subst same + cases result <;> first | rfl | cases checked + +#print axioms checkedValue_not_spanHome + +/-- A formed managed pointer is a reference, never a home. -/ +theorem formValue_not_spanHome {memory : Memory} {reference : ManagedReference} {result : Value} + (formed : formValue memory reference = .ok result) : result.isHome = false := by + unfold formValue at formed + repeat' first + | (cases formed <;> rfl) + | split at formed + | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at formed + +#print axioms formValue_not_spanHome + +/-- A formed object reference is an object, never a home. -/ +theorem formObject_not_spanHome {memory : Memory} {reference : ManagedReference} {result : Value} + (formed : formObject memory reference = .ok result) : result.isHome = false := by + unfold formObject at formed + repeat' first + | (cases formed <;> rfl) + | split at formed + | simp only [Bind.bind, Except.bind] at formed + +#print axioms formObject_not_spanHome + +/-- A reference loaded from an object field is an object, never a home. -/ +theorem loadSlot_not_spanHome {memory : Memory} {field : Reference} {result : Value} + (loaded : loadSlot memory field = .ok result) : result.isHome = false := by + unfold loadSlot at loaded + simp only [Bind.bind, Except.bind] at loaded + split at loaded + · cases loaded + · exact formObject_not_spanHome loaded + +#print axioms loadSlot_not_spanHome + +/-- A value array loaded from an object field is an array view, never a home. -/ +theorem loadArraySlot_not_spanHome {memory : Memory} {field : Reference} + {element : CIL.ArrayElement} {result : Value} + (loaded : loadArraySlot memory field element = .ok result) : result.isHome = false := by + unfold loadArraySlot arrayOfSlot at loaded + repeat' first + | (cases loaded <;> rfl) + | split at loaded + | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at loaded + +#print axioms loadArraySlot_not_spanHome + +/-- A loaded local is a scalar, reference, view or callable, never a home. -/ +theorem loadLocal_not_spanHome {memory : Memory} {slot : LocalSlot} {result : Value} + (loaded : loadLocal memory slot = .ok result) : result.isHome = false := by + unfold loadLocal at loaded + split at loaded <;> try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at loaded + all_goals + repeat' first + | (cases loaded <;> rfl) + | exact formValue_not_spanHome loaded + | exact formObject_not_spanHome loaded + | split at loaded + +#print axioms loadLocal_not_spanHome + +/-- The view a home stands for, and every other value passed through, is never a home. -/ +theorem spanView_not_spanHome {frame : Frame} {memory : Memory} {value result : Value} + (viewed : spanView frame memory value = .ok result) : result.isHome = false := by + cases value with + | spanHome slot => + simp only [spanView] at viewed + split at viewed + · exact loadLocal_not_spanHome viewed + · cases viewed + | _ => + simp only [spanView, Pure.pure, Except.pure, Except.ok.injEq] at viewed + subst viewed + rfl + +#print axioms spanView_not_spanHome + +/-- A loaded argument is a checked value or a loaded local, never a home. -/ +theorem loadArgument_not_spanHome {body : CIL.Method} {index : Nat} {args : List Value} + {frame : Frame} {memory : Memory} {result : Value} + (loaded : loadArgument body index args frame memory = .ok result) : result.isHome = false := by + unfold loadArgument at loaded + repeat' first + | exact loadLocal_not_spanHome loaded + | exact checkedValue_not_spanHome loaded + | (cases loaded; done) + | split at loaded + | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at loaded + +#print axioms loadArgument_not_spanHome + +/-- Every result of a successful `mapM` has a property each successful application gives. -/ +theorem mapM_ok_forall {α β ε : Type} {apply : α → Except ε β} {property : β → Prop} + (each : ∀ input output, apply input = .ok output → property output) : + ∀ {inputs : List α} {outputs : List β}, inputs.mapM apply = .ok outputs → + ∀ output ∈ outputs, property output + | [], outputs, mapped => by + cases mapped + intro _ member + cases member + | input :: rest, outputs, mapped => by + simp only [List.mapM_cons, Bind.bind, Except.bind, Pure.pure, Except.pure] at mapped + cases head : apply input with + | error _ => simp [head] at mapped + | ok produced => + cases tail : rest.mapM apply with + | error _ => simp [head, tail] at mapped + | ok others => + simp only [head, tail, Except.ok.injEq] at mapped + subst mapped + intro output member + rcases List.mem_cons.mp member with same | inner + · subst same + exact each input _ head + · exact mapM_ok_forall each tail output inner + +#print axioms mapM_ok_forall + +/-- Values `checkedValue` accepts one by one contain no home. -/ +theorem checkedValues_not_spanHome {m : Memory} {values results : List Value} + (checked : values.mapM (checkedValue m) = .ok results) : + ∀ result ∈ results, result.isHome = false := + mapM_ok_forall (fun _ _ accepted => checkedValue_not_spanHome accepted) checked + +#print axioms checkedValues_not_spanHome + theorem loadValue_scalar_valid (m : Memory) (r : ManagedReference) (width : Nat) {signed : Bool} (value : CIL.Value) (h : loadValue m r width signed = .ok value) : (Value.scalar value).Valid m := by @@ -719,6 +882,7 @@ macro "instruction_contract_close" : tactic => `(tactic| ( | exact True.intro | exact bitcastValue_numeric (by assumption) | exact loadSnapshot_numeric (by assumption) + | exact numericValue_snapshotSlice (by assumption) | exact loadTransfer_numeric _ _ _ _ (by assumption) | exact endianValue_numeric _ _ _ _ _ (by assumption) | exact retained.head @@ -839,18 +1003,61 @@ theorem wideArithmeticInstruction_contract (operation : CIL.MemoryOp) | split at executed #print axioms wideArithmeticInstruction_contract -/-- Object field loads and stores preserve checked effects and stack validity. -/ -theorem objectFieldInstruction_contract (operation : CIL.MemoryOp) +/-- The length a method table answers is a well-formed native integer. -/ +theorem methodTableLength_numeric {table : CIL.MethodTable} {length : CIL.Value} + (counted : methodTableLength table = .ok length) : numericValue length = true := by + unfold methodTableLength at counted + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at counted + repeat' first + | (cases counted <;> rfl) + | split at counted +#print axioms methodTableLength_numeric + +set_option maxHeartbeats 800000 in +/-- Array lengths, element loads and stores, and the two reads of a handler table preserve + checked effects and stack validity. -/ +theorem elementInstruction_contract (op : CIL.Instruction) (stack values : List Value) (memory result : Memory) - (executed : objectFieldInstruction operation stack memory = .ok (result, values)) : + (executed : elementInstruction op stack memory = .ok (result, values)) : InstructionContract memory result stack values := by - unfold objectFieldInstruction at executed + unfold elementInstruction at executed + split at executed <;> try simp only [arrayLoad, arrayStore, Bind.bind, Except.bind, Pure.pure, + Except.pure] at executed + repeat' first + | solve | cases executed; instruction_contract_close + | solve + | cases executed + exact InstructionContract.of_effect (.unchanged _) (fun retained => + ValuesValid.cons (methodTableLength_numeric (by assumption)) retained.tail) + | cases executed + | split at executed +#print axioms elementInstruction_contract + +/-- Class instance field loads preserve checked effects and stack validity. -/ +theorem classFieldLoad_contract (owner : CIL.Extraction.TypeExpr) (offset : Nat) + (slot : CIL.ClassSlot) (stack values : List Value) (memory result : Memory) + (executed : classFieldLoad owner offset slot stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold classFieldLoad at executed simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed repeat' first | solve | cases executed; instruction_contract_close | cases executed | split at executed -#print axioms objectFieldInstruction_contract +#print axioms classFieldLoad_contract + +/-- Class instance field stores preserve checked effects and stack validity. -/ +theorem classFieldStore_contract (owner : CIL.Extraction.TypeExpr) (offset : Nat) + (slot : CIL.ClassSlot) (stack values : List Value) (memory result : Memory) + (executed : classFieldStore owner offset slot stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold classFieldStore at executed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' first + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms classFieldStore_contract /-- A string literal leaves memory unchanged and pushes a formed object reference. -/ theorem stringLiteralInstruction_contract (text : String) @@ -906,12 +1113,12 @@ theorem comparisonGuardInstruction_contract (guard : ComparisonGuard) (width : N set_option maxHeartbeats 800000 in /-- The remaining memory operations preserve their checked effects and stack validity. -/ -private theorem otherMemoryInstruction_contract (operation : CIL.MemoryOp) +theorem otherMemoryInstruction_contract (operation : CIL.MemoryOp) (stack values : List Value) (memory result : Memory) (executed : otherMemoryInstruction operation stack memory = .ok (result, values)) : InstructionContract memory result stack values := by unfold otherMemoryInstruction at executed - simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + simp only [copyBlockBytes, initBlockBytes, Bind.bind, Except.bind, Pure.pure, Except.pure] at executed repeat' first | solve | cases executed; instruction_contract_close | cases executed @@ -970,6 +1177,40 @@ theorem arrayTransferInstruction_contract (element : CIL.ArrayElement) (store : | split at transferred #print axioms arrayTransferInstruction_contract +set_option maxHeartbeats 800000 in +/-- Typed element loads and stores, and the typed read of a handler table, preserve checked + effects and stack validity. -/ +theorem typedElementInstruction_contract (shape : CIL.ElementShape) (store : Bool) + (stack values : List Value) (memory result : Memory) + (executed : typedElementInstruction shape store stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold typedElementInstruction at executed + split at executed + any_goals split at executed + any_goals exact arrayTransferInstruction_contract _ _ _ _ _ _ executed + all_goals try simp only [arrayLoad, arrayStore, Bind.bind, Except.bind, Pure.pure, + Except.pure] at executed + repeat' first + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms typedElementInstruction_contract + +/-- An element address writes nothing and returns only a valid reference. -/ +theorem elementAddressInstruction_contract (shape : CIL.ElementShape) (readOnly : Bool) + (stack values : List Value) (memory result : Memory) + (executed : elementAddressInstruction shape readOnly stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold elementAddressInstruction at executed + split at executed + all_goals try unfold arrayAddress at executed + all_goals try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' first + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms elementAddressInstruction_contract + /-- Aligned transfers preserve the existing typed write effect and return only valid scalars. -/ theorem pointerTransferInstruction_contract (kind : CIL.TransferKind) (alignment : Nat) (store : Bool) (stack values : List Value) (memory result : Memory) @@ -1061,6 +1302,37 @@ theorem vector128StoreAt_eq_add_store128 (offset : CIL.W64) (bits : BitVec 128) Except.bind, Pure.pure, Except.pure] #print axioms vector128StoreAt_eq_add_store128 +/-- A reference that forms is a valid stack value in that memory. -/ +theorem formValue_reference_valid (m : Memory) (r : ManagedReference) (value : Value) + (h : formValue m r = .ok value) : (Value.reference r).Valid m := by + have valid := formValue_result_valid _ _ _ h + rw [formValue_returns_input _ _ _ h] at valid + exact valid + +/-- A static span leaves memory unchanged and pushes a span whose base is valid. -/ +theorem staticSpanInstruction_contract (operation : CIL.MemoryOp) (stack values : List Value) + (memory result : Memory) + (constructed : staticSpanInstruction operation stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold staticSpanInstruction at constructed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at constructed + repeat' first + | solve | + cases constructed + refine InstructionContract.of_effect (.unchanged _) ?_ + intro retained + apply ValuesValid.cons + · first + | exact True.intro + | rfl + | exact retained.tail.head + | exact retained.head + | apply formValue_reference_valid; assumption + · first | exact retained | exact retained.tail | exact retained.tail.tail + | cases constructed + | split at constructed +#print axioms staticSpanInstruction_contract + /-- Checked memory dispatch preserves the effects and stack validity of its selected family. -/ theorem memoryInstruction_contract (operation : CIL.MemoryOp) (stack values : List Value) (m result : Memory) (h : memoryInstruction operation stack m = .ok (result, values)) : @@ -1072,7 +1344,6 @@ theorem memoryInstruction_contract (operation : CIL.MemoryOp) (stack values : Li · exact referenceAddInstruction_contract _ _ _ _ _ _ h · exact pointerTransferInstruction_contract _ _ _ _ _ _ _ h · exact vector128UnsafeInstruction_contract _ _ _ _ _ _ h - · exact arrayTransferInstruction_contract _ _ _ _ _ _ h · exact binaryTransferInstruction_contract _ _ _ _ _ _ _ _ _ h · exact binaryTransferInstruction_contract _ _ _ _ _ _ _ _ _ h · exact singleSpanInstruction_contract _ _ _ _ _ _ h @@ -1082,38 +1353,165 @@ theorem memoryInstruction_contract (operation : CIL.MemoryOp) (stack values : Li · exact arrayFillInstruction_contract _ _ _ _ _ _ h · exact wideArithmeticInstruction_contract _ _ _ _ _ h · exact wideArithmeticInstruction_contract _ _ _ _ _ h - · exact objectFieldInstruction_contract _ _ _ _ _ h - · exact objectFieldInstruction_contract _ _ _ _ _ h - · exact objectFieldInstruction_contract _ _ _ _ _ h - · exact objectFieldInstruction_contract _ _ _ _ _ h - · exact objectFieldInstruction_contract _ _ _ _ _ h - · exact objectFieldInstruction_contract _ _ _ _ _ h - · exact objectFieldInstruction_contract _ _ _ _ _ h - · exact objectFieldInstruction_contract _ _ _ _ _ h - · exact objectFieldInstruction_contract _ _ _ _ _ h - · exact stringLiteralInstruction_contract _ _ _ _ _ h + · exact staticSpanInstruction_contract _ _ _ _ _ h + · exact staticSpanInstruction_contract _ _ _ _ _ h · exact throwIfNullInstruction_contract _ _ _ _ h · exact zeroGuardInstruction_contract _ _ _ _ _ _ _ h · exact comparisonGuardInstruction_contract _ _ _ _ _ _ _ h · exact otherMemoryInstruction_contract _ _ _ _ _ h +/-- A successful nested struct read preserves memory and returns a valid field value. -/ +theorem loadAggregateField_contract (offset size : Nat) (stack values : List Value) + (memory result : Memory) + (executed : loadAggregateField offset size stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold loadAggregateField at executed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' first + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms loadAggregateField_contract + +/-- A nested struct write preserves checked memory effects and stack validity. -/ +theorem storeAggregateField_contract (offset size : Nat) (stack values : List Value) + (memory result : Memory) + (executed : storeAggregateField offset size stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold storeAggregateField at executed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' first + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms storeAggregateField_contract + +/-- A successful field displacement preserves memory and returns a valid reference. -/ +theorem structFieldAddress_contract (offset : Nat) (stack values : List Value) (m result : Memory) + (h : structFieldAddress offset stack m = .ok (result, values)) : + InstructionContract m result stack values := by + unfold structFieldAddress at h + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h + repeat' first + | solve | cases h; instruction_contract_close + | cases h + | split at h +#print axioms structFieldAddress_contract + +/-- Scalar, aggregate and vector transfers preserve memory authority and the validity of stack values. -/ +theorem transferValues_contract (kind : CIL.TransferKind) (store : Bool) (stack values : List Value) + (m result : Memory) (h : transferValues kind store stack m = .ok (result, values)) : + InstructionContract m result stack values := by + unfold transferValues at h + split at h <;> try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h + all_goals repeat' first + | solve | apply memoryInstruction_contract; assumption + | solve | cases h; instruction_contract_close + | cases h + | split at h +#print axioms transferValues_contract + +/-- Observing the transfer operands preserves the same contract on every successful result. -/ +theorem observedTransfer_contract (kind : CIL.TransferKind) (store : Bool) (stack values : List Value) + (m result : Memory) (h : observedTransfer kind store stack m = .ok (result, values)) : + InstructionContract m result stack values := by + by_cases observed : unspecifiedOperands (if store then 2 else 1) stack = true + · simp [observedTransfer, observed] at h + · exact transferValues_contract _ _ _ _ _ _ (by simpa [observedTransfer, observed] using h) +#print axioms observedTransfer_contract + +/-- Reading a primitive or vector field preserves memory and the validity of the stack. -/ +theorem loadValueField_contract (offset width : Nat) (signed : Bool) (stack values : List Value) + (memory result : Memory) + (executed : loadValueField offset width signed stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold loadValueField at executed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' first + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms loadValueField_contract + +/-- Writing a primitive or vector field preserves memory authority and the validity of the stack. -/ +theorem storeValueField_contract (offset width : Nat) (stack values : List Value) + (memory result : Memory) + (executed : storeValueField offset width stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold storeValueField at executed + simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + repeat' first + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms storeValueField_contract + set_option maxHeartbeats 800000 in -theorem instruction_contract (operation : CIL.Op) (stack values : List Value) (m result : Memory) +theorem instruction_contract (operation : CIL.Instruction) (stack values : List Value) + (m result : Memory) (h : instruction operation stack m = .ok (result, values)) : InstructionContract m result stack values := by unfold instruction at h split at h <;> try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h all_goals repeat' first + | solve | apply transferValues_contract; assumption | solve | apply memoryInstruction_contract; assumption + | solve | apply elementInstruction_contract; assumption | solve | cases h; instruction_contract_close | cases h | split at h +/-- A successful typed copy preserves the checked memory and stack invariants. -/ +theorem copyInitializedBytes_contract (size : Nat) (stack values : List Value) + (memory result : Memory) + (executed : copyInitializedBytes size stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold copyInitializedBytes at executed + split at executed <;> try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + all_goals repeat' first + | solve | apply instruction_contract; assumption + | solve | apply memoryInstruction_contract; assumption + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms copyInitializedBytes_contract + +/-- A successful initialization preserves the checked memory and stack invariants. -/ +theorem initializeBytes_contract (size : Nat) (stack values : List Value) + (memory result : Memory) + (executed : initializeBytes size stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold initializeBytes at executed + split at executed <;> try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + all_goals repeat' first + | solve | apply instruction_contract; assumption + | solve | apply memoryInstruction_contract; assumption + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms initializeBytes_contract + +/-- A successful object transfer preserves the checked memory and stack invariants. -/ +theorem transferObject_contract (shape : ValueShape) (store : Bool) (stack values : List Value) + (memory result : Memory) + (executed : transferObject shape store stack memory = .ok (result, values)) : + InstructionContract memory result stack values := by + unfold transferObject at executed + split at executed <;> try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + all_goals repeat' first + | solve | apply observedTransfer_contract; assumption + | solve | apply memoryInstruction_contract; assumption + | solve | cases executed; instruction_contract_close + | cases executed + | split at executed +#print axioms transferObject_contract + theorem memoryInstruction_effect (operation : CIL.MemoryOp) (stack values : List Value) (m result : Memory) (h : memoryInstruction operation stack m = .ok (result, values)) : InstructionEffect m result := (memoryInstruction_contract _ _ _ _ _ h).effect -theorem instruction_effect (operation : CIL.Op) (stack values : List Value) (m result : Memory) +theorem instruction_effect (operation : CIL.Instruction) (stack values : List Value) + (m result : Memory) (h : instruction operation stack m = .ok (result, values)) : InstructionEffect m result := (instruction_contract _ _ _ _ _ h).effect @@ -1141,7 +1539,7 @@ theorem memoryInstruction_preserves_wellFormed (operation : CIL.MemoryOp) #print axioms memoryInstruction_preserves_wellFormed set_option maxHeartbeats 800000 in -theorem instruction_preserves_wellFormed (operation : CIL.Op) (stack values : List Value) +theorem instruction_preserves_wellFormed (operation : CIL.Instruction) (stack values : List Value) (m result : Memory) (hm : m.WellFormed) (h : instruction operation stack m = .ok (result, values)) : result.WellFormed := by exact (instruction_effect _ _ _ _ _ h).wellFormed hm diff --git a/CIL/Safety/InstructionReferences.lean b/CIL/Safety/InstructionReferences.lean index 8867bed65..17fef5c98 100644 --- a/CIL/Safety/InstructionReferences.lean +++ b/CIL/Safety/InstructionReferences.lean @@ -14,7 +14,7 @@ theorem memoryInstruction_values_valid (operation : CIL.MemoryOp) (stack values (h : memoryInstruction operation stack m = .ok (result, values)) : ValuesValid result values := (memoryInstruction_contract _ _ _ _ _ h).values hm valid -theorem instruction_values_valid (operation : CIL.Op) (stack values : List Value) +theorem instruction_values_valid (operation : CIL.Instruction) (stack values : List Value) (m result : Memory) (hm : m.WellFormed) (valid : ValuesValid m stack) (h : instruction operation stack m = .ok (result, values)) : ValuesValid result values := (instruction_contract _ _ _ _ _ h).values hm valid diff --git a/CIL/Safety/InstructionRenaming.lean b/CIL/Safety/InstructionRenaming.lean index 8ce591135..6a083690d 100644 --- a/CIL/Safety/InstructionRenaming.lean +++ b/CIL/Safety/InstructionRenaming.lean @@ -1,39 +1,252 @@ import CIL.Safety.MemoryInstructionRenaming +import CIL.Safety.NativeMethodTableRenaming /-! # Ordinary instruction simulation under allocation renaming -Typed fields, aggregates, lanes and stack operations preserve successful values and named -exceptions while accessing corresponding allocation identities. +Typed fields, aggregates, lanes, native table loads and stack operations preserve successful +values and named exceptions while accessing corresponding allocation identities. Primitive matching tries writes before reads to avoid reducing a write while rejecting a read lemma. -/ namespace CIL.Safety -set_option maxHeartbeats 600000 in -/-- Ordinary instructions preserve mapped successful values and named CLR exceptions. -/ -theorem Memory.Renamed.instructionResult {mapping : AllocationRenaming} +-- Stack lists are renamed by the local map of the frame that holds them. +variable {indices : LocalIndexMap} + +/-- Observable execution through a native table requires a successful checked memory step. -/ +theorem checkedNative_bind {mapping : AllocationRenaming} {relation : β → γ → Prop} + (source target : Checked α) (sourceNext : α → ExecutionResult β) + (targetNext : α → ExecutionResult γ) + (preserved : ∀ value, source = .ok value → target = .ok value) + (observable : SuccessOrThrow (checkedAt .null nativeMethodWidth source >>= sourceNext)) + (next : ∀ value, source = .ok value → SuccessOrThrow (sourceNext value) → + ResultRenamed mapping relation (sourceNext value) (targetNext value)) : + ResultRenamed mapping relation (checkedAt .null nativeMethodWidth source >>= sourceNext) + (checkedAt .null nativeMethodWidth target >>= targetNext) := by + cases checked : source with + | error fault => + simp [checked, checkedAt, Except.mapError, Bind.bind, Except.bind, SuccessOrThrow] + at observable + | ok value => + simp only [checked, checkedAt, Except.mapError, Bind.bind, Except.bind] at observable ⊢ + rw [preserved value checked] + exact next value checked observable +#print axioms checkedNative_bind + +/-- A method table's length reads no memory, so renaming leaves it and its faults unchanged. -/ +theorem methodTableLength_renamed (mapping : AllocationRenaming) + (table : CIL.MethodTable) : + ResultRenamed mapping Eq (methodTableLength table) (methodTableLength table) := by + unfold methodTableLength + cases table.targets with + | none => exact .error _ + | some targets => + by_cases large : targets.size ≥ 2 ^ 31 + · simp only [large, ite_true, Bind.bind, Except.bind]; exact .error _ + · simp only [large, ite_false, Bind.bind, Except.bind, Pure.pure, Except.pure]; exact .ok rfl +#print axioms methodTableLength_renamed + +/-- An observable method table element read selects the same pointer in renamed memory: a + native backing resolves its stable binding to the renamed allocation. -/ +theorem Memory.Renamed.methodTableElement_bind {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} {relation : β → γ → Prop} + (related : Memory.Renamed mapping scopes source target) (formed : source.WellFormed) + (table : CIL.MethodTable) (index : CIL.Value) + (sourceNext : CIL.MethodPointer → ExecutionResult β) + (targetNext : CIL.MethodPointer → ExecutionResult γ) + (observable : SuccessOrThrow (methodTableElement table index source >>= sourceNext)) + (next : ∀ pointer, SuccessOrThrow (sourceNext pointer) → + ResultRenamed mapping relation (sourceNext pointer) (targetNext pointer)) : + ResultRenamed mapping relation (methodTableElement table index source >>= sourceNext) + (methodTableElement table index target >>= targetNext) := by + unfold methodTableElement at observable ⊢ + obtain ⟨signature, targets, backing, entryCount, counted⟩ := table + cases selected : arrayIndex index with + | none => simp only [Bind.bind, Except.bind]; exact .error _ + | some position => + simp only [selected] at observable ⊢ + cases targets with + | none => exact .error _ + | some targets => + by_cases large : targets.size ≥ 2 ^ 31 + · simp only [large, ite_true, Bind.bind, Except.bind]; exact .error _ + · simp only [large, ite_false, Bind.bind, Except.bind, Pure.pure, Except.pure] + at observable ⊢ + cases found : targets[position]? with + | none => simp only; exact .error _ + | some entry => + simp only [found] at observable ⊢ + cases backing with + | none => simp only at observable ⊢; exact next _ observable + | some backing => + simp only at observable ⊢ + exact checkedNative_bind _ _ _ _ + (fun value proof => related.loadNativeTable_success formed _ value proof) observable + (fun value _ observed => next value observed) +#print axioms Memory.Renamed.methodTableElement_bind + +set_option hygiene false in +/-- Closes the renaming of one arm of an element access once its stack is split: each checked + read, write and table lookup touches the mapped element and relates the memories. -/ +local macro "element_result" : tactic => `(tactic| ( + all_goals try simp only [List.map_cons, AllocationRenaming.value, LocalIndexMap.value, + AllocationRenaming.arrayView, arrayLoad, arrayStore] at observable ⊢ + all_goals try dsimp +instances only [AllocationRenaming.arrayView] + all_goals try solve | repeat first | contradiction | cases observable | split at observable + all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below, + ManagedReference.Below] at bounds + all_goals repeat' rcases bounds with ⟨_, bounds⟩ + all_goals have imageEquality : ∀ left right, left < mapping.sourceLimit → + right < mapping.sourceLimit → (mapping.image left = mapping.image right ↔ left = right) := + fun left right leftBound rightBound => + ⟨mapping.injective left right leftBound rightBound, congrArg mapping.image⟩ + all_goals repeat first + | exact .ok ⟨related, rfl⟩ + | exact .error _ + | (apply (methodTableLength_renamed mapping _).bind + intro first second same + subst second + exact .ok ⟨related, rfl⟩) + | (apply related.methodTableElement_bind formed _ _ _ _ observable + intro pointer observable + exact .ok ⟨related, rfl⟩) + | (apply (related.loadSlot formed _ (by assumption)).bindWithSuccess + intro first second sourceStep same + subst second + push_renamed related) + | (apply (related.referenceArrayOf _ (by assumption)).bind + intro first second same + subst second + exact .ok ⟨related, rfl⟩) + | (apply (related.referenceArraySlot _ (by assumption) _).bindWithSuccess + intro first second sourceStep same + have slotBound := referenceArraySlot_below (by assumption) sourceStep + subst second) + | (apply (related.storableIn _ _ (by assumption)).bind + intro first second same + subst second) + | (apply (related.storeSlot _ (by assumption) _ (by assumption)).bind + intro first second memoryRelated + exact .ok ⟨memoryRelated, rfl⟩) + | (apply (related.loadValue _ (by assumption) _ _).bindWithSuccess + intro first second sourceStep same + subst second + exact .ok ⟨related, rfl⟩) + | (apply (related.checkedWrite _ (by assumption) _ _ _ _).bindWithSuccess + intro first second sourceStep memoryRelated + exact .ok ⟨memoryRelated, rfl⟩) + | (apply (related.checkedArray _ (by assumption) _ _).bindWithSuccess + intro first second sourceStep same + subst second) + | (apply (related.arrayElement _ (by assumption) _ _).bindWithSuccess + intro first second sourceStep same + have addressBound : first.allocation < mapping.sourceLimit := by + rw [related.sourceLimit] + exact (checkedArrayElement_valid _ _ _ _ _ sourceStep).below formed + subst second) + | dsimp +instances only [AllocationRenaming.reference] + | simp only [mapping.isNull, imageEquality] + | (split <;> try simp only [*, mapping.isNull, AllocationRenaming.managedReference, + Bind.bind, Except.bind]))) + +/-- Array lengths, element loads and stores, and handler table reads touch the mapped element + and relate their memories. -/ +theorem Memory.Renamed.elementInstructionResult {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} - (related : Memory.Renamed mapping scopes source target) (operation : CIL.Op) + (related : Memory.Renamed mapping scopes source target) (op : CIL.Instruction) (formed : source.WellFormed) (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) - (observable : SuccessOrThrow (instruction operation stack source)) : + (observable : SuccessOrThrow (elementInstruction op stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) - (instruction operation stack source) - (instruction operation (stack.map mapping.value) target) := by - unfold CIL.Safety.instruction at observable ⊢ - rw [mapping.unspecifiedOperands] + right.2 = left.2.map (indices.value mapping)) + (elementInstruction op stack source) + (elementInstruction op (stack.map (indices.value mapping)) target) := by + unfold elementInstruction at observable ⊢ split at observable - · cases observable - · rename_i guard - rw [ite_eq_right guard, ite_eq_right guard] + -- Every arm names its stack constructors, so the renamed stack reduces to the same arm. + element_result +#print axioms Memory.Renamed.elementInstructionResult + +/-- A typed element access touches the mapped element by its shape and relates the memories. -/ +theorem Memory.Renamed.typedElementInstructionResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (shape : CIL.ElementShape) + (store : Bool) (formed : source.WellFormed) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (typedElementInstruction shape store stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (typedElementInstruction shape store stack source) + (typedElementInstruction shape store (stack.map (indices.value mapping)) target) := by + cases shape with + | value element => + simp only [typedElementInstruction] at observable ⊢ + by_cases composite : element.composite = true + · simp only [composite, ite_true] at observable ⊢ + exact related.arrayTransferInstructionResult formed element store stack bounds observable + · simp only [composite, Bool.false_eq_true, ite_false] at observable ⊢ + cases store + all_goals simp only [Bool.false_eq_true, ite_false, ite_true] at observable ⊢ + · unfold arrayLoad at observable ⊢ + split at observable + element_result + · unfold arrayStore at observable ⊢ + split at observable + element_result + | method signature => + cases store + all_goals unfold typedElementInstruction at observable ⊢ + all_goals split at observable + all_goals try simp only [CIL.ElementShape.method.injEq] at * + all_goals try subst_vars + all_goals try simp only [List.map_cons, LocalIndexMap.value] + -- The table's signature test reads no allocation, so both sides take the same branch. + all_goals try (split at observable <;> + simp only [*, ite_true, ite_false, Bool.false_eq_true, Bind.bind, Except.bind]) + all_goals element_result + | reference element => cases store <;> exact .error _ + | unsupported reason => cases store <;> exact .error _ +#print axioms Memory.Renamed.typedElementInstructionResult + +/-- An element address is the address of the mapped element and relates the memories. -/ +theorem Memory.Renamed.elementAddressInstructionResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (shape : CIL.ElementShape) + (readOnly : Bool) (formed : source.WellFormed) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (elementAddressInstruction shape readOnly stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (elementAddressInstruction shape readOnly stack source) + (elementAddressInstruction shape readOnly (stack.map (indices.value mapping)) target) := by + cases shape with + | value element => + simp only [elementAddressInstruction] at observable ⊢ + unfold arrayAddress at observable ⊢ split at observable + element_result + | method signature => exact .error _ + | reference element => exact .error _ + | unsupported reason => exact .error _ +#print axioms Memory.Renamed.elementAddressInstructionResult + +set_option hygiene false in +/-- Moves checked memory accesses in a matched stack arm through the allocation map. -/ +local macro "rename_memory_arm" : tactic => `(tactic| ( all_goals try simp (config := { singlePass := true }) only - [List.map_cons, AllocationRenaming.value] + [List.map_cons, AllocationRenaming.value, LocalIndexMap.value] all_goals try dsimp +instances only [AllocationRenaming.arrayView, AllocationRenaming.spanView] all_goals try exact related.memoryInstructionResult _ formed _ bounds observable + all_goals try exact related.elementInstructionResult _ formed _ bounds observable + all_goals try solve + | (apply checkedNative_bind _ _ _ _ + (fun value proof => related.loadNativeTable_success formed _ value proof) observable + intro value found observable + exact .ok ⟨related, rfl⟩) all_goals try solve | repeat first | contradiction | cases observable | split at observable all_goals clear observable all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below, @@ -96,10 +309,239 @@ theorem Memory.Renamed.instructionResult {mapping : AllocationRenaming} intro first second sourceStep same subst second exact .ok ⟨related, rfl⟩) + | simp only [related.read _ (by assumption)] | dsimp +instances only [AllocationRenaming.reference] | simp only [mapping.isNull, imageEquality] | (split <;> try simp only [*, AllocationRenaming.managedReference, - Bind.bind, Except.bind]) + Bind.bind, Except.bind]))) + +/-- Forming a field address at a checked offset respects allocation renaming. -/ +theorem Memory.Renamed.structFieldAddressResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (formed : source.WellFormed) + (offset : Nat) (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (structFieldAddress offset stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (structFieldAddress offset stack source) + (structFieldAddress offset (stack.map (indices.value mapping)) target) := by + unfold structFieldAddress at observable ⊢ + rw [indices.unspecifiedOperands mapping] + split at observable + · cases observable + · rename_i guard + rw [ite_eq_right guard, ite_eq_right guard] + split at observable + rename_memory_arm +#print axioms Memory.Renamed.structFieldAddressResult + +/-- Reading from a checked value field respects allocation renaming. -/ +theorem Memory.Renamed.loadValueFieldResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (formed : source.WellFormed) + (offset width : Nat) (signed : Bool) (stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (loadValueField offset width signed stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (loadValueField offset width signed stack source) + (loadValueField offset width signed (stack.map (indices.value mapping)) target) := by + unfold loadValueField at observable ⊢ + rw [indices.unspecifiedOperands mapping] + split at observable + · cases observable + · rename_i guard + rw [ite_eq_right guard, ite_eq_right guard] + split at observable + rename_memory_arm +#print axioms Memory.Renamed.loadValueFieldResult + +/-- Writing to a checked value field respects allocation renaming. -/ +theorem Memory.Renamed.storeValueFieldResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (formed : source.WellFormed) + (offset width : Nat) (stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (storeValueField offset width stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (storeValueField offset width stack source) + (storeValueField offset width (stack.map (indices.value mapping)) target) := by + unfold storeValueField at observable ⊢ + rw [indices.unspecifiedOperands mapping] + split at observable + · cases observable + · rename_i guard + rw [ite_eq_right guard, ite_eq_right guard] + split at observable + rename_memory_arm +#print axioms Memory.Renamed.storeValueFieldResult + +/-- Reading a nested struct retains its byte contents under an allocation renaming. -/ +theorem Memory.Renamed.loadAggregateFieldResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (formed : source.WellFormed) + (offset size : Nat) (stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (loadAggregateField offset size stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (loadAggregateField offset size stack source) + (loadAggregateField offset size (stack.map (indices.value mapping)) target) := by + unfold loadAggregateField at observable ⊢ + rw [indices.unspecifiedOperands mapping] + split at observable + · cases observable + · rename_i guard + rw [ite_eq_right guard, ite_eq_right guard] + split at observable + rename_memory_arm +#print axioms Memory.Renamed.loadAggregateFieldResult + +/-- Writing a nested struct preserves the allocation correspondence of the resulting memories. -/ +theorem Memory.Renamed.storeAggregateFieldResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (formed : source.WellFormed) + (offset size : Nat) (stack : List Value) + (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (storeAggregateField offset size stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (storeAggregateField offset size stack source) + (storeAggregateField offset size (stack.map (indices.value mapping)) target) := by + unfold storeAggregateField at observable ⊢ + rw [indices.unspecifiedOperands mapping] + split at observable + · cases observable + · rename_i guard + rw [ite_eq_right guard, ite_eq_right guard] + split at observable + rename_memory_arm +#print axioms Memory.Renamed.storeAggregateFieldResult + +/-- A typed transfer maps the references in its operands and preserves initialized and snapshot bytes. -/ +theorem Memory.Renamed.transferValuesResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (kind : CIL.TransferKind) (store : Bool) + (formed : source.WellFormed) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (transferValues kind store stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (transferValues kind store stack source) + (transferValues kind store (stack.map (indices.value mapping)) target) := by + unfold transferValues at observable ⊢ + split at observable + rename_memory_arm +#print axioms Memory.Renamed.transferValuesResult + +/-- Renaming preserves the observation guard and the result of a typed transfer. -/ +theorem Memory.Renamed.observedTransferResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (kind : CIL.TransferKind) (store : Bool) + (formed : source.WellFormed) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (observedTransfer kind store stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (observedTransfer kind store stack source) + (observedTransfer kind store (stack.map (indices.value mapping)) target) := by + by_cases seen : unspecifiedOperands (if store then 2 else 1) stack = true + · simp only [observedTransfer, seen, ite_true] at observable + cases observable + · simp only [observedTransfer, indices.unspecifiedOperands mapping, seen, ite_false] at observable ⊢ + exact related.transferValuesResult kind store formed stack bounds observable +#print axioms Memory.Renamed.observedTransferResult + +set_option maxHeartbeats 600000 in +/-- Ordinary instructions preserve mapped successful values and named CLR exceptions. -/ +theorem Memory.Renamed.instructionResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (operation : CIL.Instruction) + (formed : source.WellFormed) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (instruction operation stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (instruction operation stack source) + (instruction operation (stack.map (indices.value mapping)) target) := by + unfold CIL.Safety.instruction at observable ⊢ + rw [indices.unspecifiedOperands mapping] + split at observable + · cases observable + · rename_i guard + rw [ite_eq_right guard, ite_eq_right guard] + split at observable + all_goals try exact related.transferValuesResult _ _ formed _ bounds observable + rename_memory_arm #print axioms Memory.Renamed.instructionResult + +/-- Typed copying preserves the allocation map on every observable result. -/ +theorem Memory.Renamed.copyInitializedBytesResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (size : Nat) + (formed : source.WellFormed) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (copyInitializedBytes size stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (copyInitializedBytes size stack source) + (copyInitializedBytes size (stack.map (indices.value mapping)) target) := by + unfold copyInitializedBytes at observable ⊢ + split at observable + rename_memory_arm +#print axioms Memory.Renamed.copyInitializedBytesResult + +/-- Zero initialization preserves the allocation map on every observable result. -/ +theorem Memory.Renamed.initializeBytesResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (size : Nat) + (formed : source.WellFormed) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (initializeBytes size stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (initializeBytes size stack source) + (initializeBytes size (stack.map (indices.value mapping)) target) := by + unfold initializeBytes at observable ⊢ + split at observable + rename_memory_arm +#print axioms Memory.Renamed.initializeBytesResult + +/-- An object transfer preserves the allocation map on every observable result. -/ +theorem Memory.Renamed.transferObjectResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (shape : CIL.ValueShape) (store : Bool) + (formed : source.WellFormed) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (transferObject shape store stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (transferObject shape store stack source) + (transferObject shape store (stack.map (indices.value mapping)) target) := by + unfold transferObject at observable ⊢ + split at observable + all_goals first + | exact related.observedTransferResult _ _ formed _ bounds observable + | exact related.memoryInstructionResult _ formed _ bounds observable + | exact .ok ⟨related, rfl⟩ + | cases observable + | (split at observable + all_goals first + | exact .ok ⟨related, rfl⟩ + | cases observable) +#print axioms Memory.Renamed.transferObjectResult + end CIL.Safety diff --git a/CIL/Safety/InstructionSteps.lean b/CIL/Safety/InstructionSteps.lean index 1bcd497b2..087b60968 100644 --- a/CIL/Safety/InstructionSteps.lean +++ b/CIL/Safety/InstructionSteps.lean @@ -1,16 +1,15 @@ -import CIL.Safety.StepComposition -import CIL.Safety.GuardNormalization +import CIL.Safety.ValueSteps /-! # Instruction step equations -One `@[cil_instruction_step]` equation per `instruction` arm and operand shape for the 64-bit -word, scalar-store, field, reference and aggregate-initialization opcodes, so a step rewrites -to that arm's body at the level of `loadValue`, `storeValue`, `add`, `write` and `formValue`, -where callers state their memory facts, and never unfolds `instruction`. Other operand shapes -keep the generic `instructionStep` equations in `StepComposition`, which these outrank. The -stepping tactics use this set alongside `cil_step`, and `cil_instruction_step_goal` lists its -opcodes; a hand-written `simp only [cil_step]` does not use it. +One `@[cil_instruction_step]` equation per `instruction` arm and operand shape for the indirect +integer, handler-table, scalar-store, field, reference and aggregate-initialization opcodes, so a +step rewrites to that arm's body at the level of `loadValue`, `storeValue`, `add`, `write` and +`formValue`, where callers state their memory facts, and never unfolds `instruction`. Other +operand shapes keep the generic `instructionStep` equations in `StepComposition`, which these +outrank. The stepping tactics use this set alongside `cil_step`, and `cil_instruction_step_goal` +lists its opcodes; a hand-written `simp only [cil_step]` does not use it. -/ namespace CIL.Safety @@ -19,7 +18,9 @@ namespace CIL.Safety decided guard leaves the single matching arm. -/ local macro "instruction_step_proof " generic:ident : tactic => `(tactic| ( rw [$generic:ident] - simp only [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, + simp only [instructionStep, instruction, loadValueField, storeValueField, transferValues, + elementInstruction, cil_guard, unspecifiedOperands, + List.take, List.any, Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, bind_assoc, pure_bind] all_goals first | rfl | (split <;> rfl))) @@ -27,296 +28,313 @@ section InstructionEquations variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (memory : Memory) --- 64-bit words through a reference. -/-- `load64` reads the word behind a writable reference. -/ +-- Indirect integer loads and stores. +/-- `ldind` reads its kind's width behind a writable reference, sign-extending a narrow signed + kind. -/ @[cil_instruction_step high] -theorem step_load64_reference_eq (reference : ManagedReference) +theorem step_ldind_reference_eq (kind : CIL.NumericKind) (reference : ManagedReference) (rest : List Value) : - step body .load64 pc args frame (.reference reference :: rest) memory = do - let value ← loadValue memory reference 8 + step body (.ldind kind) pc args frame (.reference reference :: rest) memory = do + let value ← loadValue memory reference kind.width kind.signed return .next (pc + 1) (.scalar value :: rest) frame memory := by - instruction_step_proof step_load64_eq + instruction_step_proof step_ldind_eq -/-- `load64` reads the word behind a readonly reference without write authority. -/ +/-- `ldind` reads behind a readonly reference exactly as behind a writable one. -/ @[cil_instruction_step high] -theorem step_load64_readOnlyReference_eq (reference : ManagedReference) +theorem step_ldind_readOnlyReference_eq (kind : CIL.NumericKind) (reference : ManagedReference) (rest : List Value) : - step body .load64 pc args frame (.readOnlyReference reference :: rest) memory = do - let value ← loadValue memory reference 8 + step body (.ldind kind) pc args frame (.readOnlyReference reference :: rest) memory = do + let value ← loadValue memory reference kind.width kind.signed return .next (pc + 1) (.scalar value :: rest) frame memory := by - instruction_step_proof step_load64_eq + instruction_step_proof step_ldind_eq -/-- `store64` writes an i64 through a writable reference. -/ +/-- A native `ldind` on a method table cursor loads the authenticated entry it names. -/ @[cil_instruction_step high] -theorem step_store64_reference_eq (bits : CIL.W64) (reference : ManagedReference) - (rest : List Value) : - step body .store64 pc args frame (.scalar (.i64 bits) :: .reference reference :: rest) - memory = do - let stored ← storeValue memory reference (.i64 bits) - return .next (pc + 1) rest frame stored := by - instruction_step_proof step_store64_eq - --- Scalar stores through a reference. -/-- An i32 store of width 1, 2 or 4 writes the value's low bytes through the reference. -/ -theorem step_storeScalar_i32_eq (width : Nat) (narrow : width = 1 ∨ width = 2 ∨ width = 4) - (bits : CIL.W32) (reference : ManagedReference) (rest : List Value) : - step body (.storeScalar width) pc args frame - (.scalar (.i32 bits) :: .reference reference :: rest) memory = do - let address ← referenceAt reference - let stored ← checkedAt reference width - (write memory address (numberBytes bits.toNat width) ordinaryAccessAlignment) - return .next (pc + 1) rest frame stored := by - rcases narrow with rfl | rfl | rfl <;> instruction_step_proof step_storeScalar_eq - -/-- A one-byte scalar store. -/ +theorem step_ldind_table_eq (table : CIL.MethodTable) (rest : List Value) : + step body (.ldind .i) pc args frame (.methodTable table :: rest) memory = do + let pointer ← checkedAt .null nativeMethodWidth (loadNativeTable table memory) + return .next (pc + 1) (.methodPointer pointer :: rest) frame memory := by + instruction_step_proof step_ldind_eq + +-- Handler table reads. +/-- `ldlen` of a managed handler table pushes its entry count. -/ @[cil_instruction_step high] -theorem step_storeScalar_i32_1_eq (bits : CIL.W32) - (reference : ManagedReference) (rest : List Value) : - step body (.storeScalar 1) pc args frame - (.scalar (.i32 bits) :: .reference reference :: rest) memory = do - let address ← referenceAt reference - let stored ← checkedAt reference 1 - (write memory address (numberBytes bits.toNat 1) ordinaryAccessAlignment) - return .next (pc + 1) rest frame stored := - step_storeScalar_i32_eq body pc args frame memory 1 (.inl rfl) bits reference rest +theorem step_ldlen_table_eq (table : CIL.MethodTable) (rest : List Value) : + step body .ldlen pc args frame (.methodTable table :: rest) memory = do + let length ← methodTableLength table + return .next (pc + 1) (.scalar length :: rest) frame memory := by + instruction_step_proof step_ldlen_eq -/-- A two-byte scalar store. -/ +/-- `ldelem.i` of a managed handler table at an int32 index pushes the method pointer there. -/ @[cil_instruction_step high] -theorem step_storeScalar_i32_2_eq (bits : CIL.W32) - (reference : ManagedReference) (rest : List Value) : - step body (.storeScalar 2) pc args frame - (.scalar (.i32 bits) :: .reference reference :: rest) memory = do - let address ← referenceAt reference - let stored ← checkedAt reference 2 - (write memory address (numberBytes bits.toNat 2) ordinaryAccessAlignment) - return .next (pc + 1) rest frame stored := - step_storeScalar_i32_eq body pc args frame memory 2 (.inr (.inl rfl)) bits reference rest +theorem step_ldelem_i_table_eq (index : CIL.W32) (table : CIL.MethodTable) + (rest : List Value) : + step body (.ldelem .i) pc args frame (.scalar (.i32 index) :: .methodTable table :: rest) + memory = do + let pointer ← methodTableElement table (.i32 index) memory + return .next (pc + 1) (.methodPointer pointer :: rest) frame memory := by + instruction_step_proof step_ldelem_eq -/-- A four-byte scalar store. -/ +/-- `stind.i8` writes an i64 through a writable reference. -/ @[cil_instruction_step high] -theorem step_storeScalar_i32_4_eq (bits : CIL.W32) - (reference : ManagedReference) (rest : List Value) : - step body (.storeScalar 4) pc args frame - (.scalar (.i32 bits) :: .reference reference :: rest) memory = do - let address ← referenceAt reference - let stored ← checkedAt reference 4 - (write memory address (numberBytes bits.toNat 4) ordinaryAccessAlignment) - return .next (pc + 1) rest frame stored := - step_storeScalar_i32_eq body pc args frame memory 4 (.inr (.inr rfl)) bits reference rest +theorem step_stind_i8_eq (bits : CIL.W64) (reference : ManagedReference) (rest : List Value) : + step body (.stind .i8) pc args frame (.scalar (.i64 bits) :: .reference reference :: rest) + memory = do + let stored ← storeValue memory reference (.i64 bits) + return .next (pc + 1) rest frame stored := by + instruction_step_proof step_stind_eq -/-- An eight-byte scalar store of an i64. -/ +/-- `stind.i` writes a native int, an i64 on every admitted profile, as `stind.i8` does. -/ @[cil_instruction_step high] -theorem step_storeScalar_i64_eq (bits : CIL.W64) +theorem step_stind_i_eq (bits : CIL.W64) (reference : ManagedReference) (rest : List Value) : + step body (.stind .i) pc args frame (.scalar (.i64 bits) :: .reference reference :: rest) + memory = do + let stored ← storeValue memory reference (.i64 bits) + return .next (pc + 1) rest frame stored := by + instruction_step_proof step_stind_eq + +/-- A one-, two- or four-byte `stind` writes the low bytes of an i32 through the reference. -/ +theorem step_stind_i32_eq (kind : CIL.NumericKind) (narrow : kind.width ≠ 8) (bits : CIL.W32) (reference : ManagedReference) (rest : List Value) : - step body (.storeScalar 8) pc args frame - (.scalar (.i64 bits) :: .reference reference :: rest) memory = do + step body (.stind kind) pc args frame (.scalar (.i32 bits) :: .reference reference :: rest) + memory = do let address ← referenceAt reference - let stored ← checkedAt reference 8 - (write memory address (numberBytes bits.toNat 8) ordinaryAccessAlignment) + let stored ← checkedAt reference kind.width + (write memory address (numberBytes bits.toNat kind.width) ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := by - instruction_step_proof step_storeScalar_eq + cases kind <;> first | exact absurd rfl narrow | instruction_step_proof step_stind_eq + +/-- Rewrite a checked field access using its shape, then reduce the guard on known operands. -/ +local macro "field_value_step_proof " equation:ident access:ident shape:ident : tactic => `(tactic| ( + rw [$equation:ident _ _ _ _ _ _ _ _ _ $shape:ident] + simp only [$access:ident, cil_guard, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + Bool.or_false, Bool.false_eq_true, ite_false, bind_assoc, pure_bind] + all_goals first | rfl | (split <;> rfl))) -- Fields of a referenced struct. /-- A field load forms the field address, then reads it at the field's width. -/ @[cil_instruction_step high] -theorem step_fieldAt_reference_eq (offset width : Nat) (signed : Bool) +theorem step_ldfld_value_reference_eq (field : Extraction.FieldRef) (offset width : Nat) (signed : Bool) + (shape : body.fieldShape field = .value offset width signed) (reference : Reference) (rest : List Value) : - step body (.fieldAt offset width signed) pc args frame + step body (.ldfld field) pc args frame (.reference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) let value ← loadValue memory (.address address) width signed return .next (pc + 1) (.scalar value :: rest) frame memory := by - instruction_step_proof step_fieldAt_eq + field_value_step_proof step_ldfld_value_eq loadValueField shape /-- A field load through a readonly reference reads exactly as through a writable one. -/ @[cil_instruction_step high] -theorem step_fieldAt_readOnlyReference_eq (offset width : Nat) (signed : Bool) +theorem step_ldfld_value_readOnlyReference_eq (field : Extraction.FieldRef) (offset width : Nat) (signed : Bool) + (shape : body.fieldShape field = .value offset width signed) (reference : Reference) (rest : List Value) : - step body (.fieldAt offset width signed) pc args frame + step body (.ldfld field) pc args frame (.readOnlyReference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) let value ← loadValue memory (.address address) width signed return .next (pc + 1) (.scalar value :: rest) frame memory := by - instruction_step_proof step_fieldAt_eq - -/-- A field address keeps a writable reference writable. -/ -@[cil_instruction_step high] -theorem step_fieldAddress_reference_eq (offset : Nat) (reference : Reference) - (rest : List Value) : - step body (.fieldAddress offset) pc args frame - (.reference (.address reference) :: rest) memory = do - let address ← checkedAt (.address reference) 0 - (add memory reference 1 (BitVec.ofNat 64 offset)) - return .next (pc + 1) (.reference (.address address) :: rest) frame memory := by - instruction_step_proof step_fieldAddress_eq - -/-- A field address of a readonly reference stays readonly. -/ -@[cil_instruction_step high] -theorem step_fieldAddress_readOnlyReference_eq (offset : Nat) - (reference : Reference) (rest : List Value) : - step body (.fieldAddress offset) pc args frame - (.readOnlyReference (.address reference) :: rest) memory = do - let address ← checkedAt (.address reference) 0 - (add memory reference 1 (BitVec.ofNat 64 offset)) - return .next (pc + 1) (.readOnlyReference (.address address) :: rest) frame memory := by - instruction_step_proof step_fieldAddress_eq + field_value_step_proof step_ldfld_value_eq loadValueField shape /-- An i32 field store of width 1, 2 or 4 writes the value's low bytes at the field address. -/ -theorem step_setFieldAt_i32_eq (offset width : Nat) (narrow : width = 1 ∨ width = 2 ∨ width = 4) +theorem step_stfld_value_i32_eq (field : Extraction.FieldRef) (offset width : Nat) (signed : Bool) + (shape : body.fieldShape field = .value offset width signed) (narrow : width = 1 ∨ width = 2 ∨ width = 4) (bits : CIL.W32) (reference : Reference) (rest : List Value) : - step body (.setFieldAt offset width) pc args frame + step body (.stfld field) pc args frame (.scalar (.i32 bits) :: .reference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) let stored ← checkedAt (.address address) width (write memory address (numberBytes bits.toNat width) ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := by - rcases narrow with rfl | rfl | rfl <;> instruction_step_proof step_setFieldAt_eq + rcases narrow with rfl | rfl | rfl <;> field_value_step_proof step_stfld_value_eq storeValueField shape /-- A one-byte field store. -/ @[cil_instruction_step high] -theorem step_setFieldAt_i32_1_eq (offset : Nat) (bits : CIL.W32) +theorem step_stfld_value_i32_1_eq (field : Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .value offset 1 signed) (bits : CIL.W32) (reference : Reference) (rest : List Value) : - step body (.setFieldAt offset 1) pc args frame + step body (.stfld field) pc args frame (.scalar (.i32 bits) :: .reference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) let stored ← checkedAt (.address address) 1 (write memory address (numberBytes bits.toNat 1) ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := - step_setFieldAt_i32_eq body pc args frame memory offset 1 (.inl rfl) bits reference rest + step_stfld_value_i32_eq body pc args frame memory field offset 1 signed shape (.inl rfl) bits reference rest /-- A two-byte field store. -/ @[cil_instruction_step high] -theorem step_setFieldAt_i32_2_eq (offset : Nat) (bits : CIL.W32) +theorem step_stfld_value_i32_2_eq (field : Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .value offset 2 signed) (bits : CIL.W32) (reference : Reference) (rest : List Value) : - step body (.setFieldAt offset 2) pc args frame + step body (.stfld field) pc args frame (.scalar (.i32 bits) :: .reference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) let stored ← checkedAt (.address address) 2 (write memory address (numberBytes bits.toNat 2) ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := - step_setFieldAt_i32_eq body pc args frame memory offset 2 (.inr (.inl rfl)) bits reference rest + step_stfld_value_i32_eq body pc args frame memory field offset 2 signed shape (.inr (.inl rfl)) bits reference rest /-- A four-byte field store. -/ @[cil_instruction_step high] -theorem step_setFieldAt_i32_4_eq (offset : Nat) (bits : CIL.W32) +theorem step_stfld_value_i32_4_eq (field : Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .value offset 4 signed) (bits : CIL.W32) (reference : Reference) (rest : List Value) : - step body (.setFieldAt offset 4) pc args frame + step body (.stfld field) pc args frame (.scalar (.i32 bits) :: .reference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) let stored ← checkedAt (.address address) 4 (write memory address (numberBytes bits.toNat 4) ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := - step_setFieldAt_i32_eq body pc args frame memory offset 4 (.inr (.inr rfl)) bits reference rest + step_stfld_value_i32_eq body pc args frame memory field offset 4 signed shape (.inr (.inr rfl)) bits reference rest /-- An eight-byte field store of an i64. -/ @[cil_instruction_step high] -theorem step_setFieldAt_i64_eq (offset : Nat) (bits : CIL.W64) +theorem step_stfld_value_i64_eq (field : Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .value offset 8 signed) (bits : CIL.W64) (reference : Reference) (rest : List Value) : - step body (.setFieldAt offset 8) pc args frame + step body (.stfld field) pc args frame (.scalar (.i64 bits) :: .reference (.address reference) :: rest) memory = do let address ← checkedAt (.address reference) 0 (add memory reference 1 (BitVec.ofNat 64 offset)) let stored ← checkedAt (.address address) 8 (write memory address (numberBytes bits.toNat 8) ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := by - instruction_step_proof step_setFieldAt_eq + field_value_step_proof step_stfld_value_eq storeValueField shape -- Fields of a struct value on the stack. /-- A four-byte field of a struct value is read from its bytes, after a bounds check. -/ @[cil_instruction_step high] -theorem step_fieldAt_aggregate_4_eq (offset : Nat) (signed : Bool) +theorem step_ldfld_value_aggregate_4_eq (field : Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .value offset 4 signed) (bytes : List (BitVec 8)) (rest : List Value) : - step body (.fieldAt offset 4 signed) pc args frame (.scalar (.aggregate bytes) :: rest) + step body (.ldfld field) pc args frame (.scalar (.aggregate bytes) :: rest) memory = if offset + 4 > bytes.length then .error .invalidState else .ok (.next (pc + 1) (.scalar (.i32 (BitVec.ofNat 32 (byteNumber ((bytes.drop offset).take 4)))) :: rest) frame memory) := by - instruction_step_proof step_fieldAt_eq + field_value_step_proof step_ldfld_value_eq loadValueField shape /-- An eight-byte field of a struct value is read from its bytes, after a bounds check. -/ @[cil_instruction_step high] -theorem step_fieldAt_aggregate_8_eq (offset : Nat) (signed : Bool) +theorem step_ldfld_value_aggregate_8_eq (field : Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .value offset 8 signed) (bytes : List (BitVec 8)) (rest : List Value) : - step body (.fieldAt offset 8 signed) pc args frame (.scalar (.aggregate bytes) :: rest) + step body (.ldfld field) pc args frame (.scalar (.aggregate bytes) :: rest) memory = if offset + 8 > bytes.length then .error .invalidState else .ok (.next (pc + 1) (.scalar (.i64 (BitVec.ofNat 64 (byteNumber ((bytes.drop offset).take 8)))) :: rest) frame memory) := by - instruction_step_proof step_fieldAt_eq + field_value_step_proof step_ldfld_value_eq loadValueField shape /-- A nested struct field of a struct value is the field's bytes, after a bounds check. -/ @[cil_instruction_step high] -theorem step_aggregateFieldAt_aggregate_eq (offset size : Nat) +theorem step_ldfld_aggregate_value_eq (field : Extraction.FieldRef) (offset size : Nat) + (shape : body.fieldShape field = .aggregate offset size) (bytes : List (BitVec 8)) (rest : List Value) : - step body (.aggregateFieldAt offset size) pc args frame (.scalar (.aggregate bytes) :: rest) + step body (.ldfld field) pc args frame (.scalar (.aggregate bytes) :: rest) memory = if offset + size > bytes.length then .error .invalidState else .ok (.next (pc + 1) (.scalar (.aggregate ((bytes.drop offset).take size)) :: rest) frame memory) := by - instruction_step_proof step_aggregateFieldAt_eq + rw [step_ldfld_aggregate_eq body pc args frame memory field offset size shape] + simp only [loadAggregateField, cil_guard, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + Bool.or_false, Bool.false_eq_true, ite_false] + split <;> rfl --- Reference checks and struct initialization. -/-- `asRef` pushes the reference once it forms. -/ +/-- A nested struct field of a struct snapshot is the slice of its bytes and flags, a plain struct + value when all of them are initialized, after a bounds check. -/ @[cil_instruction_step high] -theorem step_asRef_reference_eq (reference : ManagedReference) - (rest : List Value) : - step body .asRef pc args frame (.reference reference :: rest) memory = do - let value ← formValue memory reference - return .next (pc + 1) (value :: rest) frame memory := by - instruction_step_proof step_asRef_eq - -/-- `skipInit` checks that the reference forms and pops it. -/ -@[cil_instruction_step high] -theorem step_skipInit_reference_eq (reference : ManagedReference) - (rest : List Value) : - step body .skipInit pc args frame (.reference reference :: rest) memory = do - let _ ← formValue memory reference - return .next (pc + 1) rest frame memory := by - instruction_step_proof step_skipInit_eq +theorem step_ldfld_aggregate_snapshot_eq (field : Extraction.FieldRef) (offset size : Nat) + (shape : body.fieldShape field = .aggregate offset size) (total : Nat) + (bytes : List (BitVec 8 × Bool)) (rest : List Value) : + step body (.ldfld field) pc args frame + (.scalar (.snapshot (.aggregate total) bytes) :: rest) memory = + if bytes.length != total || offset + size > total then .error .invalidState + else .ok (.next (pc + 1) + (.scalar (snapshotValue (.aggregate size) ((bytes.drop offset).take size)) :: rest) + frame memory) := by + rw [step_ldfld_aggregate_eq body pc args frame memory field offset size shape] + simp only [loadAggregateField, cil_guard, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + Bool.or_false, Bool.false_eq_true, ite_false] + split <;> rfl +-- Reference checks and struct initialization. /-- `initobj` zeroes `size` bytes behind a writable reference. -/ @[cil_instruction_step high] -theorem step_initAggregate_reference_eq (size : Nat) +theorem step_initobj_reference_eq {type : Extraction.TypeExpr} {size : Nat} + (width : (body.valueShape type).size? = some size) (reference : ManagedReference) (rest : List Value) : - step body (.initAggregate size) pc args frame (.reference reference :: rest) memory = do + step body (.initobj type) pc args frame (.reference reference :: rest) memory = do let address ← referenceAt reference let stored ← checkedAt reference size (write memory address (List.replicate size 0) ordinaryAccessAlignment) return .next (pc + 1) rest frame stored := by - instruction_step_proof step_initAggregate_eq + rw [step_initobj_eq body pc args frame memory width] + simp [initializeBytes, unspecifiedOperands, Value.hasUnspecified, bind_assoc] end InstructionEquations -#print axioms step_load64_reference_eq -#print axioms step_load64_readOnlyReference_eq -#print axioms step_store64_reference_eq -#print axioms step_storeScalar_i32_eq -#print axioms step_storeScalar_i32_1_eq -#print axioms step_storeScalar_i32_2_eq -#print axioms step_storeScalar_i32_4_eq -#print axioms step_storeScalar_i64_eq -#print axioms step_fieldAt_reference_eq -#print axioms step_fieldAt_readOnlyReference_eq -#print axioms step_fieldAddress_reference_eq -#print axioms step_fieldAddress_readOnlyReference_eq -#print axioms step_setFieldAt_i32_eq -#print axioms step_setFieldAt_i32_1_eq -#print axioms step_setFieldAt_i32_2_eq -#print axioms step_setFieldAt_i32_4_eq -#print axioms step_setFieldAt_i64_eq -#print axioms step_fieldAt_aggregate_4_eq -#print axioms step_fieldAt_aggregate_8_eq -#print axioms step_aggregateFieldAt_aggregate_eq -#print axioms step_asRef_reference_eq -#print axioms step_skipInit_reference_eq -#print axioms step_initAggregate_reference_eq +open Lean Meta in +/-- The storage equation at a named operand whose checked shape supplies the byte width. -/ +private def objectSizeStep (expression : Expr) : SimpM Simp.Step := do + let_expr CIL.Safety.step body operation pc args frame stack memory := expression + | return .continue + let (opcode, #[type]) := operation.getAppFnArgs | return .continue + let read := mkApp2 (mkConst ``CIL.Method.valueShape) body type + let some (.done shared) ← ResolutionFacts.sharedShape? ``CIL.Method.valueShape read + | return .continue + let some proof := shared.proof? | return .continue + let size ← match shared.expr.getAppFnArgs with + | (``CIL.ValueShape.primitive, #[width, _]) | (``CIL.ValueShape.aggregate, #[width]) + | (``CIL.ValueShape.vector, #[width]) => pure width + | _ => return .continue + let width ← mkCongrArg (mkConst ``CIL.ValueShape.size?) proof + let leading := #[body, pc, args, frame, memory, type, size, width] + if opcode == ``CIL.Instruction.initobj then + if let some (_, head, rest) := stack.app3? ``List.cons then + if let some reference := head.app1? ``Value.reference then + return ← equationStep (mkAppN (mkConst ``step_initobj_reference_eq) + (leading ++ #[reference, rest])) + let equation := if opcode == ``CIL.Instruction.initobj then ``step_initobj_eq + else if opcode == ``CIL.Instruction.cpobj then ``step_cpobj_eq else ``step_sizeof_eq + equationStep (mkAppN (mkConst equation) (leading.push stack)) + +/-- A typed initialization steps at the byte width of its named operand. -/ +simproc [cil_instruction_step] reduceObjectInit (step _ (.initobj _) _ _ _ _ _) := objectSizeStep + +/-- A typed copy steps at the byte width of its named operand. -/ +simproc [cil_instruction_step] reduceObjectCopy (step _ (.cpobj _) _ _ _ _ _) := objectSizeStep + +/-- A size instruction pushes the byte width of its named operand. -/ +simproc [cil_instruction_step] reduceObjectSize (step _ (.sizeof _) _ _ _ _ _) := objectSizeStep + +#print axioms step_ldind_reference_eq +#print axioms step_ldind_readOnlyReference_eq +#print axioms step_ldind_table_eq +#print axioms step_ldlen_table_eq +#print axioms step_ldelem_i_table_eq +#print axioms step_stind_i8_eq +#print axioms step_stind_i_eq +#print axioms step_stind_i32_eq +#print axioms step_ldfld_value_reference_eq +#print axioms step_ldfld_value_readOnlyReference_eq +#print axioms step_stfld_value_i32_eq +#print axioms step_stfld_value_i32_1_eq +#print axioms step_stfld_value_i32_2_eq +#print axioms step_stfld_value_i32_4_eq +#print axioms step_stfld_value_i64_eq +#print axioms step_ldfld_value_aggregate_4_eq +#print axioms step_ldfld_value_aggregate_8_eq +#print axioms step_ldfld_aggregate_value_eq +#print axioms step_ldfld_aggregate_snapshot_eq +#print axioms step_initobj_reference_eq end CIL.Safety diff --git a/CIL/Safety/IntrinsicExecution.lean b/CIL/Safety/IntrinsicExecution.lean index 5125a3410..2a3cb40ce 100644 --- a/CIL/Safety/IntrinsicExecution.lean +++ b/CIL/Safety/IntrinsicExecution.lean @@ -5,20 +5,25 @@ import CIL.SIMD.Evaluation256Lemmas # Reusable intrinsic executions Execution facts for broadcast arithmetic separate instruction dispatch from vector evaluation. -Concrete runtime comparisons instantiate these facts without reducing the interpreter again. +Concrete runtime comparisons instantiate these facts without reducing the interpreter again. Each +method takes the declared signature and layout environment an extraction records. The execution +facts require argument and return storage that preserve complete 64-bit values. -/ namespace CIL.Safety set_option maxRecDepth 4096 -/-- A binary operation applied to broadcast operands, followed by a lane extraction. -/ -@[cil_code] def broadcastBinaryMethod (profile : FeatureProfile) (operation : AVX2Intrinsic) - (lane : W32) : Method := { - profile := profile, returnsValue := true, locals := [] - code := [.arg 0, .intrinsic (.vector (.create64 256)) 1, - .arg 1, .intrinsic (.vector (.create64 256)) 1, .intrinsic (.avx2 operation) 2, - .const32 lane, .intrinsic (.vector (.extract64 256)) 2, .ret] } +/-- A binary operation applied to broadcast operands, followed by a call that extracts a lane. -/ +@[cil_code] def broadcastBinaryMethod (profile : FeatureProfile) (reference : Extraction.MethodRef) + (lane : W32) (declared : DeclaredSignature) (environment : LayoutEnvironment) : Method := { + profile := profile, returnsValue := true, locals := [], declared := declared, + environment := environment + code := [.ldarg 0, .call Extraction.broadcast256Method {}, + .ldarg 1, .call Extraction.broadcast256Method {}, .call reference {}, + .ldc_i4 lane, .call Extraction.getElement256Method {}, .ret] } + +cil_runtime_models broadcastBinaryMethod /-- Broadcasting a word does not observe any unspecified bits. -/ theorem checked_broadcast256 (word : W64) : @@ -39,17 +44,35 @@ theorem checked_extract256 (bits : BitVec 256) (lane : W32) (bound : lane.toNat #print axioms checked_extract256 /-- A broadcast binary method returns the selected lane of its checked intrinsic result. -/ -theorem invoke_broadcastBinary (profile : FeatureProfile) (operation : AVX2Intrinsic) +theorem invoke_broadcastBinary {declared : DeclaredSignature} {environment : LayoutEnvironment} + (profile : FeatureProfile) (operation : AVX2Intrinsic) {reference : Extraction.MethodRef} (lane : W32) (left right : W64) (bits : BitVec 256) (memory : Memory) + (returnStorage : Extraction.declaredNarrow environment.rawLayouts declared.returnType = none) + (leftStorage : (declared.argumentType 0).bind + (Extraction.declaredNarrow environment.rawLayouts) = none) + (rightStorage : (declared.argumentType 1).bind + (Extraction.declaredNarrow environment.rawLayouts) = none) + (resolved : Extraction.runtimeModel environment .call reference = + some (.intrinsic (.avx2 operation) 2)) (available : profile.avx2 = true) (bound : lane.toNat < 4) (evaluated : checkedIntrinsic (.avx2 operation) [.v256 (Vector.pack256 left left left left), .v256 (Vector.pack256 right right right right)] = .ok (.v256 bits)) : - invoke [broadcastBinaryMethod profile operation lane] 256 0 + invoke [broadcastBinaryMethod profile reference lane declared environment] 256 0 [.scalar (.i64 left), .scalar (.i64 right)] memory = .ok (memory, [.scalar (.i64 (Vector.lane64 bits lane.toNat))]) := by - cil_checked_steps [available, evaluated, checked_broadcast256, + have stepped (args : List Value) (frame : Frame) : + step (broadcastBinaryMethod profile reference lane declared environment) (.call reference {}) + 4 args frame [.scalar (.v256 (Vector.pack256 right right right right)), + .scalar (.v256 (Vector.pack256 left left left left))] memory = + .ok (.next 5 [.scalar (.v256 bits)] frame memory) := by + rw [step_call_intrinsic _ _ _ _ _ rfl resolved rfl] + cil_checked_intrinsic [available, evaluated, broadcastBinaryMethod] + cil_checked_steps [returnStorage, Method.returnNarrow, Method.argumentAccess, List.findIdx?_nil, + leftStorage, rightStorage, available, evaluated, checked_broadcast256, checked_extract256 bits lane bound] + rw [run_next (by rfl) (by rfl) (stepped _ _)] + cil_checked_steps [returnStorage, Method.returnNarrow, checked_extract256 bits lane bound] #print axioms invoke_broadcastBinary @@ -62,18 +85,28 @@ theorem lane_broadcast256 (word : W64) (lane : Nat) (bound : lane < 4) : #print axioms lane_broadcast256 /-- A broadcast result permits every valid lane selection without vector computation. -/ -theorem invoke_broadcastBinary_word (profile : FeatureProfile) (operation : AVX2Intrinsic) +theorem invoke_broadcastBinary_word {declared : DeclaredSignature} + {environment : LayoutEnvironment} (profile : FeatureProfile) (operation : AVX2Intrinsic) + {reference : Extraction.MethodRef} (lane : W32) (left right word : W64) (memory : Memory) + (returnStorage : Extraction.declaredNarrow environment.rawLayouts declared.returnType = none) + (leftStorage : (declared.argumentType 0).bind + (Extraction.declaredNarrow environment.rawLayouts) = none) + (rightStorage : (declared.argumentType 1).bind + (Extraction.declaredNarrow environment.rawLayouts) = none) + (resolved : Extraction.runtimeModel environment .call reference = + some (.intrinsic (.avx2 operation) 2)) (available : profile.avx2 = true) (bound : lane.toNat < 4) (evaluated : checkedIntrinsic (.avx2 operation) [.v256 (Vector.pack256 left left left left), .v256 (Vector.pack256 right right right right)] = .ok (.v256 (Vector.pack256 word word word word))) : - invoke [broadcastBinaryMethod profile operation lane] 256 0 + invoke [broadcastBinaryMethod profile reference lane declared environment] 256 0 [.scalar (.i64 left), .scalar (.i64 right)] memory = .ok (memory, [.scalar (.i64 word)]) := by simpa only [lane_broadcast256 word lane.toNat bound] using - invoke_broadcastBinary profile operation lane left right _ memory available bound evaluated + invoke_broadcastBinary profile operation lane left right _ memory returnStorage leftStorage + rightStorage resolved available bound evaluated #print axioms invoke_broadcastBinary_word @@ -126,14 +159,17 @@ theorem checked_broadcast_andNot (left right : W64) : #print axioms checked_broadcast_andNot /-- A full-width shift count is held in an opaque local so its high bits stay unspecified. -/ -@[cil_code] def broadcastShiftMethod (profile : FeatureProfile) (operation : AVX2Intrinsic) - (lane : W32) : Method := { - profile := profile, returnsValue := true, locals := [.v128 0] +@[cil_code] def broadcastShiftMethod (profile : FeatureProfile) (reference : Extraction.MethodRef) + (lane : W32) (declared : DeclaredSignature) (environment : LayoutEnvironment) : Method := { + profile := profile, returnsValue := true, locals := [.v128 0], declared := declared, + environment := environment localKinds := [.vector128], opaqueVectorLocals := [0] - code := [.arg 1, .intrinsic (.vector .createScalarUnsafe64) 1, .setLocal 0, - .arg 0, .intrinsic (.vector (.create64 256)) 1, .local 0, - .intrinsic (.avx2 operation) 2, .const32 lane, - .intrinsic (.vector (.extract64 256)) 2, .ret] } + code := [.ldarg 1, .call Extraction.createScalarUnsafeMethod {}, .stloc 0, + .ldarg 0, .call Extraction.broadcast256Method {}, .ldloc 0, + .call reference {}, .ldc_i4 lane, + .call Extraction.getElement256Method {}, .ret] } + +cil_runtime_models broadcastShiftMethod /-- Creating an unsafe scalar vector records only the low lane. -/ theorem checked_scalarUnsafe (word : W64) : @@ -143,17 +179,39 @@ theorem checked_scalarUnsafe (word : W64) : #print axioms checked_scalarUnsafe /-- Shifting a broadcast returns the selected lane of the checked shift result. -/ -theorem invoke_broadcastShift (profile : FeatureProfile) (operation : AVX2Intrinsic) +theorem invoke_broadcastShift {declared : DeclaredSignature} {environment : LayoutEnvironment} + (profile : FeatureProfile) (operation : AVX2Intrinsic) {reference : Extraction.MethodRef} (lane : W32) (value count word : W64) (memory : Memory) + (returnStorage : Extraction.declaredNarrow environment.rawLayouts declared.returnType = none) + (valueStorage : (declared.argumentType 0).bind + (Extraction.declaredNarrow environment.rawLayouts) = none) + (countStorage : (declared.argumentType 1).bind + (Extraction.declaredNarrow environment.rawLayouts) = none) + (resolved : Extraction.runtimeModel environment .call reference = + some (.intrinsic (.avx2 operation) 2)) (available : profile.avx2 = true) (bound : lane.toNat < 4) (evaluated : checkedIntrinsic (.avx2 operation) [.v256 (Vector.pack256 value value value value), .v128Low64 count] = .ok (.v256 (Vector.pack256 word word word word))) : - invoke [broadcastShiftMethod profile operation lane] 256 0 + invoke [broadcastShiftMethod profile reference lane declared environment] 256 0 [.scalar (.i64 value), .scalar (.i64 count)] memory = .ok (memory, [.scalar (.i64 word)]) := by - cil_checked_steps [available, evaluated, checked_broadcast256, checked_scalarUnsafe, + have stepped (args : List Value) (frame : Frame) : + step (broadcastShiftMethod profile reference lane declared environment) (.call reference {}) + 6 args frame [.scalar (Opaque128Value.low64 count).value, + .scalar (.v256 (Vector.pack256 value value value value))] + memory = .ok (.next 7 [.scalar (.v256 (Vector.pack256 word word word word))] + frame memory) := by + rw [step_call_intrinsic _ _ _ _ _ rfl resolved rfl] + cil_checked_intrinsic [available, evaluated, broadcastShiftMethod, Opaque128Value.value] + cil_checked_steps [returnStorage, Method.returnNarrow, Method.argumentAccess, List.findIdx?_nil, + valueStorage, countStorage, available, evaluated, checked_broadcast256, checked_scalarUnsafe, checked_extract256, lane_broadcast256, bound, Opaque128Value.value] + rw [run_next (fuel := 249) (method := 0) + (program := [broadcastShiftMethod profile reference lane declared environment]) + (by rfl) (by rfl) (stepped _ _)] + cil_checked_steps [returnStorage, Method.returnNarrow, checked_extract256, + lane_broadcast256, bound] #print axioms invoke_broadcastShift diff --git a/CIL/Safety/Linking.lean b/CIL/Safety/Linking.lean index 5c2fe389e..bb62eaed4 100644 --- a/CIL/Safety/Linking.lean +++ b/CIL/Safety/Linking.lean @@ -30,58 +30,53 @@ theorem ManagedTarget.find?_relocate (f : Nat → Nat) (catalog : List ManagedTa #print axioms ManagedTarget.find?_relocate -/-- Renames the methods an instruction calls or constructs with; `.nativeBinary` never - dispatches its operand. -/ -def Op.relocate (f : Nat → Nat) : Op → Op - | .call method argc => .call (f method) argc - | .callInstance method argc => .callInstance (f method) argc - | .callVirtual targets argc => - .callVirtual (targets.map fun (type, method) => (type, f method)) argc - | .tailCall method argc => .tailCall (f method) argc - | .newVector256 method argc => .newVector256 (f method) argc - | .newAggregate method argc size => .newAggregate (f method) argc size - | .newObject method argc owner bases size => .newObject (f method) argc owner bases size - | .managed (.call signature catalog tail) => - .managed (.call signature (catalog.map (ManagedTarget.relocate f)) tail) - | op => op - --- Together these fail if a method-index constructor is added to only one of `relocate` --- and `callees`. -theorem Op.callees_relocate (f : Nat → Nat) (op : Op) : - (op.relocate f).callees = op.callees.map f := by - cases op <;> simp [Op.relocate, Op.callees] - case managed operation => - cases operation <;> simp [List.map_map, - Function.comp_def, ManagedTarget.relocate] - -theorem Op.relocate_of_callees_nil (f : Nat → Nat) (op : Op) (none : op.callees = []) : - op.relocate f = op := by - cases op <;> simp_all [Op.relocate, Op.callees] - case managed operation => - cases operation <;> simp_all - -/-- Renames the method indices in a body; pcs, locals, profile and static sites are unchanged. -/ +/-- Renames every method index a link enters. -/ +def Link.relocate (f : Nat → Nat) : Link → Link + | .direct method => .direct (f method) + | .address method => .address method + | .virtual targets => .virtual (targets.map fun (type, method) => (type, f method)) + | .aggregate constructor size => .aggregate (f constructor) size + | .object constructor bases size => .object (f constructor) bases size + | .indirect catalog => .indirect (catalog.map (ManagedTarget.relocate f)) + +/-- A relocated link enters exactly the renamed methods of the original. -/ +theorem Link.callees_relocate (f : Nat → Nat) (link : Link) : + (link.relocate f).callees = link.callees.map f := by + cases link <;> simp [Link.relocate, Link.callees, List.map_map, Function.comp_def, + ManagedTarget.relocate] + +#print axioms Link.callees_relocate + +/-- Renames the method indices in a body's link table; its code, pcs, locals, profile and static + sites are unchanged, since a call reads the method it enters from the table. -/ def Method.relocate (f : Nat → Nat) (body : Method) : Method := - { body with code := body.code.map (Op.relocate f) } + { body with links := body.links.map fun entry => (entry.1, entry.2.relocate f) } + +/-- A relocated body gives each pc the relocated link of the original. -/ +theorem Method.linkAt_relocate (f : Nat → Nat) (body : Method) (pc : Nat) : + (body.relocate f).linkAt pc = (body.linkAt pc).map (Link.relocate f) := by + simp only [Method.linkAt, linkLookup, Method.relocate, List.find?_map, Option.map_map] + rfl + +#print axioms Method.linkAt_relocate end CIL namespace CIL.Safety -/-- Every method index that `lib`'s code names is one of its own methods. -/ +/-- Every method index the link tables of `lib`'s bodies enter is one of its own methods. -/ def Closed (lib : CIL.Program) : Prop := ∀ (i : Nat) (body : CIL.Method), lib[i]? = some body → - ∀ (pc : Nat) (op : CIL.Op), body.code[pc]? = some op → - ∀ callee ∈ op.callees, callee < lib.length + ∀ entry ∈ body.links, ∀ callee ∈ entry.2.callees, callee < lib.length /-- Decides `Closed` for a concrete library. -/ def closedCheck (lib : CIL.Program) : Bool := - lib.all fun body => body.code.all fun op => op.callees.all (· < lib.length) + lib.all fun body => body.links.all fun entry => entry.2.callees.all (· < lib.length) theorem Closed.of_check {lib : CIL.Program} (checked : closedCheck lib = true) : Closed lib := by - intro i body found pc op fetched callee member + intro i body found entry listed callee member simp only [closedCheck, List.all_eq_true, decide_eq_true_eq] at checked - exact checked body (List.mem_of_getElem? found) op (List.mem_of_getElem? fetched) callee member + exact checked body (List.mem_of_getElem? found) entry listed callee member /-- Method `i` of `lib` sits at `f i` in `linked`, relocated by `f`. -/ def RelocatesInto (lib linked : CIL.Program) (f : Nat → Nat) : Prop := @@ -99,6 +94,21 @@ def FrameAction.relocate (f : Nat → Nat) : FrameAction → FrameAction .construct (f callee) arguments rest frame temporary kind memory | action => action +/-- A call's runtime model enters no callee, so relocating the body's links changes nothing it + does. -/ +theorem modelStep_relocate (f : Nat → Nat) (model : Option CIL.RuntimeModel) (body : Method) + (pc : Nat) (frame : Frame) (stack : List Value) (memory : Memory) : + modelStep model (body.relocate f) pc frame stack memory = + (modelStep model body pc frame stack memory).map (FrameAction.relocate f) := by + rw [modelStep_profile_congr (moved := body.relocate f) (body := body) rfl] + cases executed : modelStep model body pc frame stack memory with + | error => rfl + | ok action => + obtain ⟨_, _, _, rfl, _⟩ := modelStep_frame executed + rfl + +#print axioms modelStep_relocate + theorem FrameAction.relocate_of_callee_none (f : Nat → Nat) (action : FrameAction) (none : action.callee = .none) : action.relocate f = action := by cases action <;> simp_all [FrameAction.relocate, FrameAction.callee] @@ -106,42 +116,57 @@ theorem FrameAction.relocate_of_callee_none (f : Nat → Nat) (action : FrameAct theorem enterFrame_relocate (f : Nat → Nat) (body : CIL.Method) (args : List Value) (m : Memory) : enterFrame (body.relocate f) args m = enterFrame body args m := rfl -/-- Relocating the body does not change how an unrelocated instruction steps. -/ -theorem step_body_relocate (f : Nat → Nat) (body : CIL.Method) (op : CIL.Op) (pc : Nat) - (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) : - step (body.relocate f) op pc args frame stack m = step body op pc args frame stack m := rfl - -/-- Relocating an indirect catalog changes only the body index of its selected action. -/ -private theorem managedStep_relocate (f : Nat → Nat) (signature : CIL.ManagedSignature) - (catalog : List CIL.ManagedTarget) (tail : Bool) (pc : Nat) (frame : Frame) - (stack : List Value) (memory : Memory) : - managedStep (.call signature (catalog.map (CIL.ManagedTarget.relocate f)) tail) - pc frame stack memory = - (managedStep (.call signature catalog tail) pc frame stack memory).map +/-- Relocating a catalog renames the body an indirect call selects and selects no other. -/ +theorem indirectTarget_relocate (f : Nat → Nat) (signature : CIL.ManagedSignature) + (catalog : List CIL.ManagedTarget) (pointer : CIL.MethodPointer) : + indirectTarget signature (catalog.map (CIL.ManagedTarget.relocate f)) pointer = + (indirectTarget signature catalog pointer).map f := by + unfold indirectTarget + cases pointer.signature != signature with + | true => rfl + | false => + cases pointer.target with + | none => rfl + | some identity => + simp only [Bool.false_eq_true, ite_false, Option.bind, CIL.ManagedTarget.find?_relocate, + Option.map_map] + rfl + +#print axioms indirectTarget_relocate + +/-- Relocating the catalog an indirect call reads from its link changes only the body index of + its selected action. -/ +private theorem calliStep_relocate (f : Nat → Nat) (signature : CIL.ManagedSignature) + (tail : Bool) (link : Option CIL.Link) + (frame : Frame) (stack : List Value) (memory : Memory) : + calliStep signature tail (link.map (CIL.Link.relocate f)) frame stack + memory = + (calliStep signature tail link frame stack memory).map (FrameAction.relocate f) := by cases stack with | nil => rfl | cons value values => cases value <;> try rfl rename_i pointer - cases target : pointer.target with - | none => simp [managedStep, target, Bind.bind, Except.bind, Except.map] - | some identity => - cases found : catalog.find? (·.identity == identity) with - | none => - simp only [managedStep, target, CIL.ManagedTarget.find?_relocate, found, - Bind.bind, Except.bind, Except.map, Option.map_none] - split <;> rfl - | some selected => - simp only [managedStep, target, CIL.ManagedTarget.find?_relocate, found, - Option.map_some] - repeat' first - | rfl - | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure, - Except.map, CIL.ManagedTarget.relocate, FrameAction.relocate] - | split - -#print axioms managedStep_relocate + rcases link with _ | link + · rfl + cases link <;> try rfl + rename_i catalog + cases selected : indirectTarget signature catalog pointer with + | none => + simp only [calliStep, Option.map_some, CIL.Link.relocate, indirectTarget_relocate, + selected, Option.map_none] + rfl + | some method => + simp only [calliStep, Option.map_some, CIL.Link.relocate, indirectTarget_relocate, + selected] + repeat' first + | rfl + | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure, Except.map, + FrameAction.relocate] + | split + +#print axioms calliStep_relocate /-- Relocating a dispatch table renames the selected body and keeps every fault. -/ private theorem virtualTarget_relocate (f : Nat → Nat) (memory : Memory) @@ -166,69 +191,104 @@ private theorem virtualTarget_relocate (f : Nat → Nat) (memory : Memory) #print axioms virtualTarget_relocate -/-- A relocated instruction steps exactly as the original, entering the renamed method. -/ -theorem step_relocate (f : Nat → Nat) (body : CIL.Method) (op : CIL.Op) (pc : Nat) +set_option maxHeartbeats 400000 in +/-- An instruction that reads no link steps alike whatever link table its body carries. -/ +private theorem step_relinked {body : CIL.Method} (links : List (Nat × CIL.Link)) + {op : CIL.Instruction} (unlinked : op.readsLink = false) (pc : Nat) (args : List Value) + (frame : Frame) (stack : List Value) (m : Memory) : + step { body with links := links } op pc args frame stack m = + step body op pc args frame stack m := by + -- A matcher stuck on the stack carries the call arms, which read the table; split the + -- stack as deep as the patterns look so the matcher selects this instruction's arm. + cases op <;> simp only [CIL.Instruction.readsLink, reduceCtorEq] at unlinked + case ldtoken operand => cases operand <;> rfl + all_goals first + | rfl + | (rcases stack with _ | ⟨first, _ | ⟨second, rest⟩⟩ + all_goals (try cases first) <;> (try cases second) <;> rfl) + +#print axioms step_relinked + +/-- A relocated body steps each instruction exactly as the original, entering the renamed + method. -/ +theorem step_relocate (f : Nat → Nat) (body : CIL.Method) (op : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) : - step (body.relocate f) (op.relocate f) pc args frame stack m = + step (body.relocate f) op pc args frame stack m = (step body op pc args frame stack m).map (FrameAction.relocate f) := by - rw [step_body_relocate] - cases hc : op.callees with - | nil => - rw [CIL.Op.relocate_of_callees_nil f op hc] - cases hs : step body op pc args frame stack m with + cases linked : op.readsLink with + | false => + rw [CIL.Method.relocate, step_relinked _ linked] + cases stepped : step body op pc args frame stack m with | error => rfl | ok action => have none : action.callee = .none := by cases enters : action.callee with | none => rfl - | some callee => simpa [hc] using step_callee hs enters + | some callee => simpa [linked] using (step_callee stepped enters).1 simp only [Except.map, FrameAction.relocate_of_callee_none f action none] - | cons callee rest => - cases op <;> simp only [CIL.Op.callees, reduceCtorEq] at hc - case managed operation => - cases operation <;> simp only [CIL.Op.callees, reduceCtorEq] at hc - case call signature catalog tail => - simp only [step, CIL.Op.relocate, observerCount, Bool.or_false] - split - · rfl - · exact managedStep_relocate f signature catalog tail pc frame stack m - case callVirtual targets argc => - simp only [step, CIL.Op.relocate, observerCount, Bool.or_false, virtualTarget_relocate] - split + | true => + cases op <;> simp only [CIL.Instruction.readsLink, Bool.false_eq_true] at linked + case calli => + simp only [step, CIL.Method.linkAt_relocate] + rw [apply_ite (Except.map (FrameAction.relocate f))] + refine ite_congr rfl (fun _ => rfl) (fun _ => ?_) + exact calliStep_relocate f _ _ (body.linkAt pc) frame stack m + case callvirt method => + simp only [step, observerCount, CIL.Method.linkAt_relocate] + by_cases unknown : unspecifiedOperands method.argumentCount stack = true + · simp only [unknown, ite_true] + rfl + simp only [unknown, Bool.false_eq_true, ite_false] + rcases body.linkAt pc with _ | link · rfl + cases link <;> try rfl + case direct index => + simp only [Option.map_some, CIL.Link.relocate] + repeat' first + | rfl + | (split <;> try simp only [*, Bind.bind, Except.bind, Pure.pure, Except.pure, + Except.map, FrameAction.relocate]) + rename_i table + simp only [Option.map_some, CIL.Link.relocate, virtualTarget_relocate] split · rfl - cases (stack.take argc).reverse.mapM (checkedValue m) with + cases (stack.take method.argumentCount).reverse.mapM (callArgument frame m) with | error => rfl | ok arguments => simp only [Bind.bind, Except.bind] split · rename_i receiver _ - cases virtualTarget m targets receiver <;> rfl + cases virtualTarget m table receiver <;> rfl all_goals rfl all_goals - unfold step - simp only [CIL.Op.relocate, observerCount, Bool.or_false, - Bind.bind, Except.bind, Pure.pure, Except.pure, Except.map] - repeat' (split <;> simp_all [FrameAction.relocate]) - all_goals - split <;> try rfl - split <;> rfl - -/-- A method that `step` enters is a callee its instruction names, so it is in a closed library. -/ + simp only [step, observedCall, CIL.Method.linkAt_relocate] + rw [apply_ite (Except.map (FrameAction.relocate f))] + refine ite_congr rfl (fun _ => rfl) (fun _ => ?_) + rcases body.linkAt pc with _ | link + · first + | rfl + | exact modelStep_relocate .. + | (dsimp only + rw [apply_ite (Except.map (FrameAction.relocate f))] + exact ite_congr rfl (fun _ => rfl) (fun _ => modelStep_relocate ..)) + cases link <;> try rfl + all_goals simp only [Option.map_some, CIL.Link.relocate, newobjStep, constructAggregate, + constructObject] + all_goals repeat' first + | rfl + | (split <;> try simp only [*, Bind.bind, Except.bind, Pure.pure, Except.pure, Except.map, + FrameAction.relocate]) + +/-- A method that `step` enters is one its link table lists, so it is in a closed library. -/ theorem Closed.callee {lib : CIL.Program} (closed : Closed lib) {method pc : Nat} - {body : CIL.Method} {op : CIL.Op} {args : List Value} {frame : Frame} {stack : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args : List Value} {frame : Frame} + {stack : List Value} {m : Memory} {action : FrameAction} {callee : Nat} (found : lib[method]? = some body) - (fetched : body.code[pc]? = some op) (stepped : step body op pc args frame stack m = .ok action) (enters : action.callee = some callee) : - callee < lib.length := - closed method body found pc op fetched callee - (step_callee stepped enters) - -theorem Method.relocate_code {f : Nat → Nat} {body : CIL.Method} {pc : Nat} : - (body.relocate f).code[pc]? = (body.code[pc]?).map (CIL.Op.relocate f) := by - simp [CIL.Method.relocate] + callee < lib.length := by + obtain ⟨entry, listed, _, member⟩ := CIL.Method.calleesAt_mem (step_callee stepped enters).2 + exact closed method body found entry listed callee member /-- A closed library runs in the linked program exactly as alone; only a fault's method index is renamed. -/ @@ -239,40 +299,40 @@ theorem run_relocate {lib linked : CIL.Program} {f : Nat → Nat} (closed : Clos (run lib fuel method pc args frame stack memory).mapError (LocatedFault.relocateMethod f) := by induction fuel generalizing method pc args frame stack memory with - | zero => simp only [run, Except.mapError, LocatedFault.relocateMethod] + | zero => simp only [run_zero, Except.mapError, LocatedFault.relocateMethod] | succ fuel ih => have found : lib[method]? = some lib[method] := List.getElem?_eq_getElem bound have moved := relocates method bound generalize lib[method] = body at found moved cases ho : body.code[pc]? with | none => - have lo : (body.relocate f).code[pc]? = none := by rw [Method.relocate_code, ho]; rfl - simp only [run, found, moved, ho, lo, Except.mapError, LocatedFault.relocateMethod] + have lo : (body.relocate f).code[pc]? = none := ho + simp only [run_succ, found, moved, ho, lo, Except.mapError, LocatedFault.relocateMethod] | some op => - have lo : (body.relocate f).code[pc]? = some (op.relocate f) := by - rw [Method.relocate_code, ho]; rfl + have lo : (body.relocate f).code[pc]? = some op := ho have ls := step_relocate f body op pc args frame stack memory cases hs : step body op pc args frame stack memory with | error fault => - simp only [run, found, moved, ho, lo, ls, hs, Except.map, Bind.bind, Except.bind, + simp only [run_succ, found, moved, ho, lo, ls, hs, Except.map, Bind.bind, Except.bind, Except.mapError, LocatedFault.relocateMethod] | ok action => cases action with | next target values nextFrame updated => - simp only [run, found, moved, ho, lo, ls, hs, Except.map, FrameAction.relocate, Bind.bind, - Except.bind, Except.mapError] + simp only [run_succ, found, moved, ho, lo, ls, hs, Except.map, FrameAction.relocate, + Bind.bind, Except.bind, Except.mapError] exact ih method target args nextFrame values updated bound | returned values updated => - simp only [run, found, moved, ho, lo, ls, hs, Except.map, FrameAction.relocate, Bind.bind, - Except.bind, Except.mapError] + simp only [run_succ, found, moved, ho, lo, ls, hs, Except.map, FrameAction.relocate, + Bind.bind, Except.bind, Except.mapError] cases values.mapM (checkedValue (leaveFrame frame updated)) <;> rfl | call callee arguments rest updated => - have inside := closed.callee found ho hs rfl + have inside := closed.callee found hs rfl have child : lib[callee]? = some lib[callee] := List.getElem?_eq_getElem inside have childMoved := relocates callee inside generalize lib[callee] = callBody at child childMoved - simp only [run, found, moved, ho, lo, ls, hs, child, childMoved, enterFrame_relocate, - Except.map, FrameAction.relocate, Bind.bind, Except.bind, Except.mapError] + simp only [run_succ, found, moved, ho, lo, ls, hs, child, childMoved, + enterFrame_relocate, Except.map, FrameAction.relocate, Bind.bind, Except.bind, + Except.mapError] cases he : enterFrame callBody arguments updated with | error fault => rfl | ok entered => @@ -285,11 +345,11 @@ theorem run_relocate {lib linked : CIL.Program} {f : Nat → Nat} (closed : Clos obtain ⟨final, values⟩ := result exact ih method (pc + 1) args frame (values ++ rest) final bound | tailCall callee arguments updated => - have inside := closed.callee found ho hs rfl + have inside := closed.callee found hs rfl have child : lib[callee]? = some lib[callee] := List.getElem?_eq_getElem inside have childMoved := relocates callee inside generalize lib[callee] = callBody at child childMoved - simp only [run, found, moved, ho, lo, ls, hs, child, childMoved, + simp only [run_succ, found, moved, ho, lo, ls, hs, child, childMoved, Except.map, FrameAction.relocate, Bind.bind, Except.bind, Except.mapError] change (if (callBody.returnsValue != body.returnsValue) then _ else _) = _ cases returns : callBody.returnsValue != body.returnsValue with @@ -304,12 +364,13 @@ theorem run_relocate {lib linked : CIL.Program} {f : Nat → Nat} (closed : Clos | error fault => rfl | ok setup => exact ih callee 0 values setup.1 [] setup.2 inside | construct callee arguments rest nextFrame temporary kind updated => - have inside := closed.callee found ho hs rfl + have inside := closed.callee found hs rfl have child : lib[callee]? = some lib[callee] := List.getElem?_eq_getElem inside have childMoved := relocates callee inside generalize lib[callee] = callBody at child childMoved - simp only [run, found, moved, ho, lo, ls, hs, child, childMoved, enterFrame_relocate, - Except.map, FrameAction.relocate, Bind.bind, Except.bind, Except.mapError] + simp only [run_succ, found, moved, ho, lo, ls, hs, child, childMoved, + enterFrame_relocate, Except.map, FrameAction.relocate, Bind.bind, Except.bind, + Except.mapError] cases he : enterFrame callBody arguments updated with | error fault => rfl | ok entered => @@ -338,7 +399,7 @@ theorem invoke_relocate {lib linked : CIL.Program} {f : Nat → Nat} (closed : C have found : lib[method]? = some lib[method] := List.getElem?_eq_getElem bound have moved := relocates method bound generalize lib[method] = body at found moved - simp only [invoke, found, moved, enterFrame_relocate, Bind.bind, Except.bind, Except.mapError] + simp only [invoke_eq, found, moved, enterFrame_relocate, Bind.bind, Except.bind, Except.mapError] cases args.mapM (checkedValue memory) with | error fault => rfl | ok checked => @@ -360,7 +421,7 @@ theorem invoke_bound {lib : CIL.Program} {fuel method : Nat} {args : List Value} /-- A raised exception is raised by the relocated method. -/ theorem Raises.relocate {lib linked : CIL.Program} {f : Nat → Nat} {method : Nat} - {args : List Value} {memory : Memory} {exception : String} + {args : List Value} {memory : Memory} {exception : ExceptionType} (raised : Raises lib method args memory exception) (closed : Closed lib) (relocates : RelocatesInto lib linked f) : Raises linked (f method) args memory exception := by diff --git a/CIL/Safety/LiteralBindings.lean b/CIL/Safety/LiteralBindings.lean index 2fde4cd61..fc227694b 100644 --- a/CIL/Safety/LiteralBindings.lean +++ b/CIL/Safety/LiteralBindings.lean @@ -61,8 +61,9 @@ theorem staticReference_literals {source target : Memory} {descriptor : CIL.Stat #print axioms staticReference_literals /-- Every checked dispatcher effect leaves the literal bindings as they were. -/ -theorem StepEffect.literals {body : CIL.Method} {operation : CIL.Op} {source target : Memory} - {frame final : Frame} (effect : StepEffect body operation frame source final target) : +theorem StepEffect.literals {body : CIL.Method} {site : Nat} {operation : CIL.Instruction} + {source target : Memory} + {frame final : Frame} (effect : StepEffect body site operation frame source final target) : target.literals = source.literals := by cases effect with | arrayAllocation effect => diff --git a/CIL/Safety/LocalCalls.lean b/CIL/Safety/LocalCalls.lean index 4dadace45..02ae23a1f 100644 --- a/CIL/Safety/LocalCalls.lean +++ b/CIL/Safety/LocalCalls.lean @@ -106,7 +106,7 @@ theorem LoopFrame.after_local_write {footprint : List (Reference × Nat)} preservation evidence into the caller proof. The helper is actually invoked. -/ theorem WP.local_call {Input Output : Type} {contract : MethodContract Input Output} {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} - {operation : CIL.Op} {args stack rest : List Value} {frame : Frame} + {operation : CIL.Instruction} {args stack rest : List Value} {frame : Frame} {footprint : List (Reference × Nat)} {caller current : Memory} {kinds : List CIL.LocalKind} {known : Nat → Option CIL.Value} {post : (Memory × List Value) → Prop} diff --git a/CIL/Safety/LocalLoadRenaming.lean b/CIL/Safety/LocalLoadRenaming.lean index a626e4def..f19055fb7 100644 --- a/CIL/Safety/LocalLoadRenaming.lean +++ b/CIL/Safety/LocalLoadRenaming.lean @@ -88,18 +88,6 @@ theorem Memory.Renamed.loadSnapshot {mapping : AllocationRenaming} | ok bytes => exact .ok rfl #print axioms Memory.Renamed.loadSnapshot -/-- Taking a retained byte home's address preserves its mapped pointer and all checks. -/ -theorem Memory.Renamed.localAddress {mapping : AllocationRenaming} - {scopes : Nat → Nat → Prop} {source target : Memory} - (related : Memory.Renamed mapping scopes source target) (slot : LocalSlot) - (bound : slot.Below mapping.sourceLimit) : - ResultRenamed mapping (fun source target => target = mapping.value source) - (localAddress source slot) (localAddress target (mapping.localSlot slot)) := by - cases slot with - | bytes kind reference => exact related.formValue (.address reference) bound - | root _ | readOnlyRoot _ | opaque128 _ | span _ _ _ | array _ _ - | methodPointer _ _ | methodTable _ _ | object _ => exact .error _ -#print axioms Memory.Renamed.localAddress /-- Reading a retained local preserves its value, including restricted references and snapshots. -/ theorem Memory.Renamed.loadLocal {mapping : AllocationRenaming} @@ -177,4 +165,25 @@ theorem Memory.Renamed.loadLocal {mapping : AllocationRenaming} | ok bytes => cases kind <;> first | exact .ok rfl | exact .error .invalidState #print axioms Memory.Renamed.loadLocal +/-- Taking a retained local's address preserves its mapped byte home pointer and all checks, and + a span local's home names the relocated slot. -/ +theorem Memory.Renamed.localAddress {indices : LocalIndexMap} {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (index : Nat) (slot : LocalSlot) + (bound : slot.Below mapping.sourceLimit) : + ResultRenamed mapping (fun source target => target = indices.value mapping source) + (localAddress source index slot) + (localAddress target (indices.index index) (mapping.localSlot slot)) := by + cases slot with + | bytes kind reference => + exact (related.formValue (.address reference) bound).imp fun left right formed same => by + simp only [CIL.Safety.formValue, Bind.bind, Except.bind, checkedAt, Except.mapError] + at formed + split at formed <;> cases formed + exact same + | span width writable contents => exact .ok rfl + | root _ | readOnlyRoot _ | opaque128 _ | array _ _ + | methodPointer _ _ | methodTable _ _ | object _ => exact .error _ +#print axioms Memory.Renamed.localAddress + end CIL.Safety diff --git a/CIL/Safety/LocalOperandRenaming.lean b/CIL/Safety/LocalOperandRenaming.lean index c16c43e0f..5cc0214fa 100644 --- a/CIL/Safety/LocalOperandRenaming.lean +++ b/CIL/Safety/LocalOperandRenaming.lean @@ -12,25 +12,20 @@ indices, arithmetic operations and static sites retain their original meaning. namespace CIL.Safety /-- Relocate each local load, address or store through the retained declaration map. -/ -def LocalIndexMap.operation (indices : LocalIndexMap) : CIL.Op → CIL.Op - | .local position narrow => .local (indices.index position) narrow - | .localAddr position => .localAddr (indices.index position) - | .setLocal position narrow => .setLocal (indices.index position) narrow - | .initializeSpanLocal position constructor => - .initializeSpanLocal (indices.index position) constructor - | .vectorLocal position => .vectorLocal (indices.index position) - | .vectorLocalAddr position => .vectorLocalAddr (indices.index position) - | .setVectorLocal position => .setVectorLocal (indices.index position) +def LocalIndexMap.operation (indices : LocalIndexMap) : CIL.Instruction → CIL.Instruction + | .ldloc position => .ldloc (indices.index position) + | .ldloca position => .ldloca (indices.index position) + | .stloc position => .stloc (indices.index position) | operation => operation /-- Relocation leaves every instruction's unspecified-operand guard unchanged. -/ -theorem LocalIndexMap.operation_observers (indices : LocalIndexMap) (operation : CIL.Op) : +theorem LocalIndexMap.operation_observers (indices : LocalIndexMap) (operation : CIL.Instruction) : observerCount (indices.operation operation) = observerCount operation := by cases operation <;> rfl #print axioms LocalIndexMap.operation_observers /-- Local operands never enter the pure arithmetic fallback, whose arity remains exact. -/ -theorem LocalIndexMap.operation_pureArity (indices : LocalIndexMap) (operation : CIL.Op) : +theorem LocalIndexMap.operation_pureArity (indices : LocalIndexMap) (operation : CIL.Instruction) : pureArity (indices.operation operation) = pureArity operation := by cases operation <;> rfl #print axioms LocalIndexMap.operation_pureArity @@ -53,21 +48,6 @@ theorem Frame.Renamed.localFound {indices : LocalIndexMap} {mapping : Allocation exact ⟨bound, slots.1, congrArg some slots.2⟩ #print axioms Frame.Renamed.localFound -/-- Argument-home lookup retains its argument key and maps the selected bounded slot. -/ -theorem Frame.Renamed.argumentHome {indices : LocalIndexMap} {mapping : AllocationRenaming} - {scopes : Nat → Nat → Prop} {source target : Frame} - (related : Frame.Renamed indices mapping scopes source target) (position : Nat) : - ResultRenamed mapping (LocalSlot.Renamed mapping) - (argumentHome source position) (argumentHome target position) := by - unfold CIL.Safety.argumentHome - rw [related.arguments] - simp only [List.find?_map, Function.comp_def] - cases found : source.arguments.find? (fun home => home.1 == position) with - | none => exact .error .invalidState - | some binding => - exact .ok ⟨related.argumentBounds binding (List.mem_of_find?_eq_some found), rfl⟩ -#print axioms Frame.Renamed.argumentHome - /-- Local lookup preserves missing-slot rejection as well as corresponding retained slots. -/ theorem Frame.Renamed.localLookup {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Frame} diff --git a/CIL/Safety/LocalReferences.lean b/CIL/Safety/LocalReferences.lean index a79e92689..544decf01 100644 --- a/CIL/Safety/LocalReferences.lean +++ b/CIL/Safety/LocalReferences.lean @@ -4,7 +4,7 @@ import CIL.Safety.FrameAllocations /-! # Frame references are valid -Frame setup (locals, argument homes and parameter homes) yields slots whose references are +Frame setup (locals and parameter homes) yields slots whose references are valid in the resulting memory, and `Frame.ReferencesValid` survives any allocation extension. `LiveState` uses this to keep a frame's references valid across calls. -/ @@ -24,15 +24,6 @@ theorem makeLocals_references_valid (m result : Memory) (activation : Nat) SlotsReferencesValid result slots := by exact (makeLocals_setup _ _ _ _ _ _ _ h).references hm -theorem makeArgumentHomes_references_valid (m result : Memory) (activation : Nat) - (indices : List Nat) (args : List Value) (homes : List (Nat × LocalSlot)) - (owned : List AllocationId) (hm : m.WellFormed) - (h : makeArgumentHomes activation indices args m = .ok (homes, owned, result)) : - ∀ home ∈ homes, home.2.ReferencesValid result := by - intro home member - exact (makeArgumentHomes_setup _ _ _ _ _ _ _ h).references hm home.2 - (List.mem_map.mpr ⟨home, member, rfl⟩) - theorem makeParameterHomes_references_valid (m result : Memory) (activation : Nat) (parameters : List (Nat × CIL.LocalKind)) (args : List Value) (homes : List (Nat × LocalSlot)) (owned : List AllocationId) (hm : m.WellFormed) @@ -42,23 +33,19 @@ theorem makeParameterHomes_references_valid (m result : Memory) (activation : Na exact (makeParameterHomes_setup _ _ _ _ _ _ _ h).references hm home.2 (List.mem_map.mpr ⟨home, member, rfl⟩) -/-- Every reference held by the frame's local slots and argument homes is valid in `m`. -/ +/-- Every reference held by the frame's local slots is valid in `m`. -/ def Frame.ReferencesValid (m : Memory) (frame : Frame) : Prop := - SlotsReferencesValid m frame.locals ∧ ∀ home ∈ frame.arguments, home.2.ReferencesValid m + SlotsReferencesValid m frame.locals theorem enterFrame_references_valid (body : CIL.Method) (args : List Value) (m result : Memory) (frame : Frame) (hm : m.WellFormed) (h : enterFrame body args m = .ok (frame, result)) : frame.ReferencesValid result := by - have valid := (enterFrame_setup _ _ _ _ _ h).references hm - refine ⟨fun slot member => valid slot (List.mem_append_left _ member), ?_⟩ - intro home member - exact valid home.2 (List.mem_append_right _ (List.mem_map.mpr ⟨home, member, rfl⟩)) + exact (enterFrame_setup _ _ _ _ _ h).references hm theorem Frame.ReferencesValid.preserve {m result : Memory} {frame : Frame} (valid : frame.ReferencesValid m) (hm : m.WellFormed) (ext : AllocationExtension m result) : frame.ReferencesValid result := - ⟨fun slot member => (valid.1 slot member).preserve hm ext, - fun home member => (valid.2 home member).preserve hm ext⟩ + fun slot member => (valid slot member).preserve hm ext #print axioms allocateHome_reference_valid #print axioms storeLocal_preserves_reference_validity diff --git a/CIL/Safety/LocalSetupRenaming.lean b/CIL/Safety/LocalSetupRenaming.lean index c619e946d..e93fa52c9 100644 --- a/CIL/Safety/LocalSetupRenaming.lean +++ b/CIL/Safety/LocalSetupRenaming.lean @@ -162,7 +162,8 @@ theorem Memory.Renamed.makeLocalInitialized {mapping : AllocationRenaming} | mk storedSlot storedMemory => simp only [Pure.pure, Except.pure, Except.ok.injEq, Prod.mk.injEq] at made obtain ⟨rfl, rfl, rfl⟩ := made - have stores := homes.storeLocal (.bytes retainedKind ⟨source.nextIdentity, 0⟩) + have stores := homes.storeLocal (indices := .nil) + (.bytes retainedKind ⟨source.nextIdentity, 0⟩) (.scalar initial) freshBound (by trivial) rw [sourceStored] at stores obtain ⟨⟨targetSlot, targetMemory⟩, targetStored, slots, remains⟩ := stores.success @@ -174,7 +175,7 @@ theorem Memory.Renamed.makeLocalInitialized {mapping : AllocationRenaming} · simp only [CIL.Safety.makeLocal, targetAllocated, unmodeled, ite_false, Bind.bind, Except.bind] simp only [AllocationRenaming.localSlot, AllocationRenaming.reference, - AllocationRenaming.value, freshImage, retainedKind] at targetStored + LocalIndexMap.value, freshImage, retainedKind] at targetStored simp only [targetStored, List.map_cons, List.map_nil, freshImage, Pure.pure, Except.pure, AllocationRenaming.localSlot, AllocationRenaming.reference] · intro identity member @@ -295,8 +296,8 @@ theorem Memory.Renamed.skipLocalInitialized {mapping : AllocationRenaming} .refl mapping, rfl, related, by simp⟩ | methodTable signature => cases initial <;> simp [InitializerFits] at fits unknown - exact ⟨mapping, .methodTable signature (some ⟨signature, none, none⟩), [], target, - .refl mapping, rfl, related, by simp⟩ + exact ⟨mapping, .methodTable signature (some { signature, targets := none, entryCount := 0 }), + [], target, .refl mapping, rfl, related, by simp⟩ | reference => cases initial <;> simp [InitializerFits] at fits unknown refine ⟨mapping, .root (some .null), [], target, .refl mapping, rfl, related, ?_⟩ diff --git a/CIL/Safety/LocalSnapshotSteps.lean b/CIL/Safety/LocalSnapshotSteps.lean index 39b3c3f00..253fe8196 100644 --- a/CIL/Safety/LocalSnapshotSteps.lean +++ b/CIL/Safety/LocalSnapshotSteps.lean @@ -82,7 +82,7 @@ theorem LocalSnapshots.store_at {body : CIL.Method} {pc index target : Nat} (fits : localNumber kind value = .ok number) (slot : frame.locals[index]? = some (.bytes kind home)) (ready : access memory home (localWidth kind) 1 true = .ok ()) - (stored : step body (.setLocal index) pc args frame (.scalar value :: stack) memory = + (stored : step body (.stloc index) pc args frame (.scalar value :: stack) memory = .ok (.next target values nextFrame after)) : LocalSnapshots after frame.locals (rememberLocal known index (localResult kind value)) ∧ after.WellFormed ∧ MemoryBelow boundary memory after ∧ @@ -104,44 +104,139 @@ initialize Lean.registerTraceClass `CIL.snapshots /-- The reader the snapshot forms name when a term does not have the shape they read. -/ private def snapshotReader : String := "snapshot stepping (cil_checked_steps, cil_contract_steps)" +/-- What one store keeps while it restates facts at the memory it produces: the hypotheses the + restated facts supersede, cleared together by one pass at the end of the store. -/ +private structure SnapshotScope where + /-- The hypotheses to clear once the store is done. -/ + superseded : IO.Ref (Array FVarId) + +/-- The scope of a store that starts now. -/ +private def SnapshotScope.start : TacticM SnapshotScope := + return { superseded := ← IO.mkRef #[] } + +/-- Whether `left` and `right` may still be made equal by assigning metavariables: false when + they are statements headed by different predicates once their reducible heads are unfolded, + or applications of one constant to arguments that differ at two distinct memory variables. + Unification would settle these only after unfolding what mentions them, which costs more + with every memory a walk has passed. -/ +private partial def mayUnify (left right : Expr) (statement := false) : MetaM Bool := do + if left == right then return true + match left, right with + | .fvar _, .fvar _ => + -- Variables of other types, such as two proofs of one statement, may be equal. + if (← instantiateMVars (← inferType left)).isConstOf ``Memory then memoryMatches left right + else return true + | .app .., .app .. => + let function := left.getAppFn + if function.isConst && function == right.getAppFn && + left.getAppNumArgs == right.getAppNumArgs then + -- Reading the arguments in place allocates nothing, and the check runs on every hypothesis. + for position in [:left.getAppNumArgs] do + unless ← mayUnify (left.getArg! position) (right.getArg! position) do return false + return true + else + -- Two terms with different heads, such as `localWidth kind` and `16`, may be equal. + unless statement && function.isConst && right.getAppFn.isConst do return true + -- A reducible head, as `>` is `<` swapped and `≠` is `¬ =`, may state the other one; the + -- check reads two attributes, so a hypothesis with neither head reducible costs nothing. + let reducible (head : Expr) : MetaM Bool := do + return (← getReducibilityStatus head.constName!) == .reducible + unless (← reducible function) || (← reducible right.getAppFn) do return false + let leftUnfolded ← whnfR left + let rightUnfolded ← whnfR right + if leftUnfolded == left && rightUnfolded == right then return false + mayUnify leftUnfolded rightUnfolded (statement := true) + | _, _ => return true + +/-- `assumption` that tries only the hypotheses `mayUnify` leaves, since unifying a statement + about one memory with a hypothesis about another unfolds the accesses and writes in both, + and the context holds a hypothesis for each store. -/ +elab "cil_cheap_assumption" : tactic => withMainContext do + let goal ← getMainGoal + let type ← instantiateMVars (← goal.getType) + -- The goal with its reducible head unfolded once, so no hypothesis unfolds it again. + let shape ← whnfR type + let context ← getLCtx + for step in [:context.numIndices] do + let some declaration := context.decls[context.numIndices - 1 - step]! | continue + if declaration.isImplementationDetail then continue + unless ← mayUnify shape (← instantiateMVars declaration.type) (statement := true) do continue + if ← isDefEq type declaration.type then + goal.assign declaration.toExpr + replaceMainGoal [] + return + throwError "cil_cheap_assumption: no hypothesis states the goal" + /-- Close an existing snapshot obligation using checked facts and concrete local encodings. -/ private def solveSnapshotPremise (goal : MVarId) (facts : Array Term) : TacticM Unit := do - let remaining ← Tactic.run goal <| evalTactic (← `(tactic| cil_strict + let shown ← instantiateMVars (← goal.getType) + -- The encoding of a stored value holds by `rfl`; looking for a hypothesis that states it would + -- unify it with each equation in scope, which costs more with every store. + let encoding := (shown.eq?.map (·.2.1)).any (·.isAppOf ``localNumber) + let general ← `(tactic| cil_strict (first - | assumption - | (apply CompleteWritableHomes.writable; assumption) - | (apply NumericHomes.writable; assumption) + | cil_cheap_assumption + | (apply CompleteWritableHomes.writable; cil_cheap_assumption) + | (apply NumericHomes.writable; cil_cheap_assumption) | rfl | cil_lookup [$facts,*] - | (simp [localNumber, localResult, rememberLocal, $[$facts:term],*]; done)))) + | (simp [localNumber, localResult, rememberLocal, $[$facts:term],*]; done))) + let attempt ← if encoding then `(tactic| first | rfl | ($general:tactic)) else pure general + let remaining ← Tactic.run goal (evalTactic attempt) unless remaining.isEmpty do throwError "A local snapshot premise was not established" /-- Assert a proved fact in the current continuation and return its local name: fresh, or with `named` the accessible name `base` itself, which then refers to this fact. -/ -private def assertSnapshotFact (base : Name) (proof : Expr) (named := false) : - TacticM Term := withMainContext do +private def assertSnapshotFact (scope : SnapshotScope) (base : Name) (proof : Expr) + (named := false) : TacticM Term := withMainContext do let type ← inferType proof let named := named && !base.hasMacroScopes unless named do - for declaration in ← getLCtx do - if declaration.type == type then return ← Term.exprToSyntax declaration.toExpr + -- A scan that compares types allocates nothing, so it costs no heartbeats however long it is. + let present? := (← getLCtx).findDeclRev? fun declaration => + if !declaration.isImplementationDetail && declaration.type == type then some declaration + else none + if let some present := present? then return ← Term.exprToSyntax present.toExpr let name ← if named then pure base else mkFreshUserName base - let (_, goal) ← (← (← getMainGoal).assert name type proof).intro1P - replaceMainGoal [goal] + replaceMainGoal [(← addFact (← getMainGoal) name type proof).2] return mkIdent name +/-- Whether a hypothesis of this name is one a store made for itself, so that no supplied fact + names it and it can be cleared once a later memory supersedes it. -/ +private def isSnapshotOwnName (name : Name) : Bool := + [`snapshotAccess, `snapshotRetained, `snapshotHomes, `snapshotSlot, `snapshotBound, + `snapshotReady].contains name.eraseMacroScopes + +/-- The memory a proved write `write memory reference bytes alignment = .ok after` starts from. -/ +private def snapshotWrittenMemory (writeProof : Expr) : TacticM Expr := do + let some (_, operation, _) := (← instantiateMVars (← inferType writeProof)).eq? + | throwError "{snapshotReader}: expected a write equation" + argumentAt snapshotReader operation 0 + /-- Carry access facts through a proved write; byte-value facts are deliberately not carried. -/ -private def preserveSnapshotAccess (written : Term) : TacticM Unit := withMainContext do +private def preserveSnapshotAccess (scope : SnapshotScope) (written : Term) : + TacticM Unit := withMainContext do let writeProof ← Term.elabTerm written none - let declarations := (← getLCtx).decls.toArray.filterMap id - for declaration in declarations do + let before ← snapshotWrittenMemory writeProof + for declaration in declarationsFrom (← getLCtx) (← memoryWindowStart before) do let some (_, accessTerm, _) := (← instantiateMVars declaration.type).consumeMData.eq? | continue unless accessTerm.isAppOf ``access do continue + -- Only a fact about the memory the write starts from can be restated. + unless ← memoryMatches (← argumentAt snapshotReader accessTerm 0) before do continue + -- The access a store found for its own home, a variable the walk introduced, is read by that + -- store alone, so it is dropped instead of restated, which would add one fact per store. + if ← producedByWalk (← argumentAt snapshotReader accessTerm 1) then + scope.superseded.modify (·.push declaration.fvarId) + continue let saved ← saveState try let proof ← mkAppM ``snapshot_access_after_write #[declaration.toExpr, writeProof] - discard <| assertSnapshotFact `snapshotAccess proof + discard <| assertSnapshotFact scope `snapshotAccess proof + -- A restatement of ours that the new one supersedes is cleared, so the context does not + -- grow with each store. + if isSnapshotOwnName declaration.userName then + scope.superseded.modify (·.push declaration.fvarId) catch _ => saved.restore /-- The names of the memory a store produces, its step and the evidence about that memory. -/ @@ -155,30 +250,68 @@ private def isSnapshotStoreName : Name → Bool snapshotStoreBases.any fun base => text == base || text.startsWith (base ++ "_") | _ => false +/-- Whether the hypothesis `name` may be cleared when a restatement supersedes it: a fact the + store made for itself, or, when it is stated at a memory the walk produced, a named fact the + restatement shadows, except a store's evidence. A fact at the caller's memory stays. -/ +private def isSupersededBy (producedMemory : Bool) (name : Name) : Bool := + isSnapshotOwnName name || + (producedMemory && !name.hasMacroScopes && !isSnapshotStoreName name) + /-- Retain only generic facts whose exact current memory is the source of this write, each restated after the write under the name it has. One store's own evidence keeps its name for that store, and reaches later memories under a fresh name. -/ -private def preserveSnapshotFacts (written below : Term) : TacticM Unit := withMainContext do +private def preserveSnapshotFacts (scope : SnapshotScope) (written below : Term) : + TacticM Unit := withMainContext do let writeProof ← Term.elabTerm written none let belowProof ← Term.elabTerm below none - let declarations := (← getLCtx).decls.toArray.filterMap id - for declaration in declarations do + let before ← snapshotWrittenMemory writeProof + let producedMemory ← producedByWalk before + -- The memories homes are stated at, whose chains to the current memory a later store reads. + -- Looking for them allocates only for a hit. + let homeMemories := (← getLCtx).foldl (init := #[]) fun found declaration => + let type := declaration.type + if !declaration.isImplementationDetail && + (type.isAppOfArity ``CompleteWritableHomes 4 || type.isAppOfArity ``NumericHomes 4) then + found.push type.appFn!.appFn!.appFn!.appArg! + else found + for declaration in declarationsFrom (← getLCtx) (← memoryWindowStart before) do + let type := (← instantiateMVars declaration.type).consumeMData + -- The position of the memory a fact is about; only a fact about the memory the write starts + -- from is restated. + let aboutMemory? : Option Nat := + if type.isAppOfArity ``CompleteWritableHomes 4 || type.isAppOfArity ``ValidCall 3 then + some 0 + else if type.isAppOfArity ``MemoryBelow 3 || type.isAppOfArity ``AccessBelow 3 then + some 2 + else none + let some position := aboutMemory? | continue let saved ← saveState try - let type := (← instantiateMVars declaration.type).consumeMData + unless ← memoryMatches (← argumentAt snapshotReader type position) before do continue + -- A chain from a memory an earlier store produced adds one fact per store, and a store reads + -- it only when homes are stated at that memory; the others are not carried. + if position == 2 then + let source ← argumentAt snapshotReader type 1 + if (← producedByWalk source) && !homeMemories.contains source then + if isSupersededBy producedMemory declaration.userName then + scope.superseded.modify (·.push declaration.fvarId) + continue let proof ← if type.isAppOfArity ``CompleteWritableHomes 4 then mkAppM ``CompleteWritableHomes.after_write #[declaration.toExpr, writeProof] else if type.isAppOfArity ``ValidCall 3 then mkAppM ``ValidCall.after_write #[declaration.toExpr, writeProof] else if type.isAppOfArity ``MemoryBelow 3 then mkAppM ``MemoryBelow.trans #[declaration.toExpr, belowProof] - else if type.isAppOfArity ``AccessBelow 3 then do + else do let watermark ← argumentAt snapshotReader type 0 let next ← mkAppM ``write_preserves_access_below #[writeProof, watermark] mkAppM ``AccessBelow.trans #[declaration.toExpr, next] - else continue - discard <| assertSnapshotFact declaration.userName proof + discard <| assertSnapshotFact scope declaration.userName proof (named := !isSnapshotStoreName declaration.userName) + -- The fact now stated at the new memory supersedes this one, except a store's own evidence, + -- which keeps its name; clearing the rest keeps the context from growing with each store. + if isSupersededBy producedMemory declaration.userName then + scope.superseded.modify (·.push declaration.fvarId) catch error => saved.restore trace[CIL.snapshots] "{declaration.userName}: {error.toMessageData}" @@ -191,7 +324,7 @@ private def snapshotCarried? (memory : Expr) (below : Ident) (fact : Term) (type let reads := operation.isAppOfArity ``read 4 unless reads || operation.isAppOfArity ``form 2 do return none let some stated := argument? operation 0 | return none - unless ← withNewMCtxDepth (isDefEq stated memory) do return none + unless ← memoryMatches stated memory do return none if reads then return some (← `((MemoryBelow.read $below _ (by cil_caller_allocation) _ _).trans $fact)) return some (← `((MemoryBelow.form $below _ (by cil_caller_allocation)).trans $fact)) @@ -199,22 +332,26 @@ private def snapshotCarried? (memory : Expr) (below : Ident) (fact : Term) (type /-- Restate each named read or formed reference of `memory` below the watermark of `below`, a store that keeps that memory, at the memory after it and under the same name. A fact whose reference is not shown older than the watermark stays as it is. -/ -private def carrySnapshotReads (memory : Expr) (below : Ident) : TacticM Unit := do +private def carrySnapshotReads (scope : SnapshotScope) (memory : Expr) (below : Ident) : + TacticM Unit := do let carried ← withMainContext do - let context ← getLCtx - let mut found : Array (Ident × Term) := #[] - for declaration in context do - if declaration.isImplementationDetail || declaration.userName.hasMacroScopes then continue - unless (context.findFromUserName? declaration.userName).any - (·.fvarId == declaration.fvarId) do continue + let mut found : Array (LocalDecl × Term) := #[] + for declaration in accessibleDeclarations (← getLCtx) (← memoryWindowStart memory) do + if declaration.userName.hasMacroScopes then continue let fact := mkIdent declaration.userName let type ← instantiateMVars declaration.type if let some transport ← snapshotCarried? memory below fact type then - found := found.push (fact, transport) + found := found.push (declaration, transport) return found - for (fact, transport) in carried do + let produced ← producedByWalk memory + for (earlier, transport) in carried do let saved ← saveState - try evalTactic (← `(tactic| cil_strict have $fact:ident := $transport)) + try + evalTactic (← `(tactic| cil_strict cil_have_by $(mkIdent earlier.userName):ident := + $transport)) + -- The restated fact has the name, so one at a memory the walk made is out of reach and is + -- cleared; one at the caller's memory stays. + if produced then scope.superseded.modify (·.push earlier.fvarId) catch _ => saved.restore /-- The slots a frame's locals list before `slots`, with the layout equation that states @@ -241,21 +378,53 @@ private def snapshotLeadingSlots (locals slots : Expr) : throwError "The homes cover no suffix of the frame's locals" /-- Evidence in scope that `current` retains the access of `entered`, as `AccessBelow`. -/ -private def snapshotRetainedAccess (entered current : Expr) : TacticM Term := withMainContext do - let declarations := (← getLCtx).decls.toArray.filterMap id +private def snapshotRetainedAccess (scope : SnapshotScope) (entered current : Expr) : + TacticM Term := withMainContext do + let declarations := declarationsFrom (← getLCtx) (← memoryWindowStart entered) let linked (head : Name) : MetaM (Option Expr) := do for declaration in declarations.reverse do let type := (← instantiateMVars declaration.type).consumeMData let some #[_, source, target] := argumentsOf? type head 3 | continue - unless ← withNewMCtxDepth (isDefEq source entered) do continue - unless ← withNewMCtxDepth (isDefEq target current) do continue + unless ← memoryMatches source entered do continue + unless ← memoryMatches target current do continue return some declaration.toExpr return none if let some retained ← linked ``AccessBelow then return ← Term.exprToSyntax retained let some preserved ← linked ``MemoryBelow | throwError "No evidence that the current memory retains the homes' access" -- Stated as `AccessBelow`, the link follows each later store under one watermark. - assertSnapshotFact `snapshotRetained (← mkAppM ``MemoryBelow.accessBelow #[preserved]) + assertSnapshotFact scope `snapshotRetained (← mkAppM ``MemoryBelow.accessBelow #[preserved]) + +/-- Introduces the conjuncts of `proof`, a chain `a ∧ b ∧ c`, as hypotheses under `names` by + projection. `obtain` splits each conjunct by cases, which searches the whole context for + the hypotheses that depend on it. -/ +private def introConjuncts (proof : Expr) (names : Array Ident) : TacticM Unit := + withMainContext do + let mut rest := proof + for position in [:names.size] do + let last := position + 1 == names.size + let component ← if last then pure rest else mkAppM ``And.left #[rest] + let name := names[position]! + replaceMainGoal [(← addFact (← getMainGoal) name.getId (← inferType component) component).2] + unless last do rest ← mkAppM ``And.right #[rest] + +/-- Introduces the witness and the conjuncts of the hypothesis `located : ∃ witness, a ∧ b ∧ c`, + as `obtain` does, without its search of the whole context for hypotheses that depend on the + case split. The hypotheses that only carry the split are left for `scope` to clear. -/ +private def introWitness (scope : SnapshotScope) (located witness : Ident) + (conjuncts : Array Ident) : TacticM Unit := do + let rest := mkIdent (← mkFreshUserName `snapshotRest) + evalTactic (← `(tactic| refine Exists.elim $located (fun $witness $rest => ?_))) + let context ← withMainContext getLCtx + let some carried := context.findFromUserName? rest.getId + | throwError "{snapshotReader}: the store's evidence was not introduced" + if let some introduced := context.findFromUserName? witness.getId then + recordWalkVariable introduced.fvarId + introConjuncts carried.toExpr conjuncts + scope.superseded.modify (·.push carried.fvarId) + withMainContext do + if let some source := (← getLCtx).findFromUserName? located.getId then + scope.superseded.modify (·.push source.fvarId) /-- Close a home's kind lookup `kinds[index]? = some _` with a supplied fact that states it, or that states the numeric spec at that index. -/ @@ -278,11 +447,23 @@ elab_rules : tactic context shows that the current memory retains their access. A kind lookup that does not compute, over opaque kinds or at a symbolic index, is closed from `facts`; a symbolic index is taken only when no leading slot is skipped. -/ -private def prepareSnapshotHome (memory frame index : Expr) (facts : Array Term) : - TacticM Unit := withMainContext do +private def prepareSnapshotHome (scope : SnapshotScope) (memory frame index : Expr) + (facts : Array Term) : TacticM Unit := withMainContext do + -- The facts about the home that only this store reads are cleared when the store is done. + let supersede (names : Array Ident) : TacticM Unit := withMainContext do + for name in names do + if let some declaration := (← getLCtx).findFromUserName? name.getId then + scope.superseded.modify (·.push declaration.fvarId) let locals ← mkAppM ``Frame.locals #[frame] - let declarations := (← getLCtx).decls.toArray.filterMap id - for declaration in declarations.reverse do + let context ← getLCtx + for step in [:context.numIndices] do + let some declaration := context.decls[context.numIndices - 1 - step]! | continue + if declaration.isImplementationDetail then continue + -- A hypothesis that is not about homes, and has no metavariable to instantiate, is skipped + -- without allocating, since the context holds one for each store. + let stated := declaration.type.consumeMData + unless declaration.type.hasMVar || stated.isAppOfArity ``NumericHomes 4 || + stated.isAppOfArity ``CompleteWritableHomes 4 do continue let saved ← saveState try let type := (← instantiateMVars declaration.type).consumeMData @@ -299,13 +480,15 @@ private def prepareSnapshotHome (memory frame index : Expr) (facts : Array Term) let slot := mkIdent (← mkFreshUserName `snapshotSlot) let bound := mkIdent (← mkFreshUserName `snapshotBound) let ready := mkIdent (← mkFreshUserName `snapshotReady) - let current ← withNewMCtxDepth (isDefEq entered memory) + let current ← memoryMatches entered memory if current && (← withNewMCtxDepth (isDefEq slots locals)) then - let homes ← assertSnapshotFact `snapshotHomes proof + let homes ← assertSnapshotFact scope `snapshotHomes proof let index ← Term.exprToSyntax index - evalTactic (← `(tactic| obtain ⟨$home, $slot, $bound, $ready⟩ := - CompleteWritableHomes.home_at $homes $index _ - (by first | rfl | cil_snapshot_kind [$facts,*]))) + let located := mkIdent (← mkFreshUserName `snapshotLocated) + evalTactic (← `(tactic| cil_have_by $located := CompleteWritableHomes.home_at $homes + $index _ (by first | rfl | cil_snapshot_kind [$facts,*]))) + introWitness scope located home #[slot, bound, ready] + supersede #[slot, bound, ready] return let (skipped, layout) ← snapshotLeadingSlots locals slots let position ← getNatValue? (← whnfR index) @@ -323,7 +506,7 @@ private def prepareSnapshotHome (memory frame index : Expr) (facts : Array Term) let retained ← if current then `(term| (MemoryBelow.refl (Memory.nextIdentity $(← Term.exprToSyntax entered)) $(← Term.exprToSyntax entered)).accessBelow) - else snapshotRetainedAccess entered memory + else snapshotRetainedAccess scope entered memory let homes ← Term.exprToSyntax proof let skippedTerms ← skipped.mapM (Term.exprToSyntax ·) let layoutTerm ← layout.mapM (Term.exprToSyntax ·) @@ -339,17 +522,19 @@ private def prepareSnapshotHome (memory frame index : Expr) (facts : Array Term) | none => pure stated let localsTerm ← Term.exprToSyntax locals let raw := mkIdent (← mkFreshUserName `snapshotSlot) - evalTactic (← `(tactic| cil_strict obtain ⟨$home, $raw, $bound, $ready⟩ := - CompleteWritableHomes.home_retained $homes (by assumption) $retained - (by first | exact Nat.le_refl _ | assumption | omega) [$skippedTerms,*] $inner _ - (by first | rfl | cil_snapshot_kind [$facts,*]))) + let located := mkIdent (← mkFreshUserName `snapshotLocated) + evalTactic (← `(tactic| cil_strict cil_have_by $located := + CompleteWritableHomes.home_retained $homes (by cil_cheap_assumption) $retained + (by first | exact Nat.le_refl _ | cil_cheap_assumption | omega) [$skippedTerms,*] + $inner _ (by first | rfl | cil_snapshot_kind [$facts,*]))) + introWitness scope located home #[raw, bound, ready] let kind ← withMainContext do let found ← instantiateMVars (← inferType (← Term.elabTerm raw none)) let some (_, _, selected) := found.eq? | throwError "Unexpected home lookup" let some kind := (lastArgument? selected).bind (argument? · 0) | throwError "{snapshotReader}: expected a byte slot, but found{indentExpr selected}" Term.exprToSyntax (← whnf kind) - evalTactic (← `(tactic| cil_strict have $slot : + evalTactic (← `(tactic| cil_strict cil_have_proved $slot : ($localsTerm)[$outer]? = some (LocalSlot.bytes $kind $home) := by $(← restate raw):tactic)) unless skipped.isEmpty do @@ -364,8 +549,10 @@ private def prepareSnapshotHome (memory frame index : Expr) (facts : Array Term) pure (← Term.exprToSyntax covered, ← Term.exprToSyntax (mkAppN stated.getAppFn (stated.getAppArgs.set! 3 locals))) let writable := mkIdent (← mkFreshUserName `snapshotHomes) - evalTactic (← `(tactic| cil_strict have $writable : $statement := by + evalTactic (← `(tactic| cil_strict cil_have_proved $writable : $statement := by $(← restate covered):tactic)) + supersede #[writable] + supersede #[raw, slot, bound, ready] return catch error => saved.restore @@ -376,13 +563,8 @@ private def prepareSnapshotHome (memory frame index : Expr) (facts : Array Term) `localSnapshots`, `localFormed`, `localBelow`, `localAccess` and `localWrite`, or all seven with the first suffix `_1`, `_2` that leaves each unused. -/ private def snapshotStoreNames : TacticM (Array Ident) := withMainContext do - let context ← getLCtx - let named (index : Nat) (base : String) : Name := - Name.mkSimple (if index == 0 then base else s!"{base}_{index}") - let mut index := 0 - while snapshotStoreBases.any fun base => context.usesUserName (named index base) do - index := index + 1 - return snapshotStoreBases.map fun base => mkIdent (named index base) + let index := firstUnusedSuffix (← getLCtx) snapshotStoreBases + return snapshotStoreBases.map fun base => mkIdent (suffixedName index base) /-- What a numeric local store leaves for the walk that took it. -/ private structure SnapshotStored where @@ -395,6 +577,43 @@ private structure SnapshotStored where /-- The name of the store's `MemoryBelow` evidence from `source`. -/ below : Ident +/-- The local index the snapshots track for `operation` of `method`: a `stloc` (`ldloc` unless + `store`) by its index, or a homed `starg` (`ldarg`) at its home slot with the proof of that + home. None for any other instruction, including an argument without a home. -/ +private def snapshotLocal? (method operation : Expr) (store : Bool) : + MetaM (Option (Expr × Option Expr)) := do + let (localOp, argumentOp) := if store then (``CIL.Instruction.stloc, ``CIL.Instruction.starg) + else (``CIL.Instruction.ldloc, ``CIL.Instruction.ldarg) + let some index := argument? operation 0 | return none + if operation.isAppOfArity localOp 1 then return some (index, none) + unless operation.isAppOfArity argumentOp 1 do return none + let some (slot, access) ← ResolutionFacts.argumentHome? method index | return none + return some (slot, some access) + +/-- The method `methodFound` finds and the instruction `instructionFound` fetches from it. -/ +private def snapshotFetched (methodFound instructionFound : Term) : TacticM (Expr × Expr) := + withMainContext do + let methodProof ← Term.elabTerm methodFound none + let some (_, _, found) := (← instantiateMVars (← inferType methodProof)).eq? + | throwError "Expected a method lookup" + let instructionProof ← Term.elabTerm instructionFound none + let some (_, _, instruction) := (← instantiateMVars (← inferType instructionProof)).eq? + | throwError "Expected an instruction lookup" + let some operation ← cilReducedOp? (← lastArgument snapshotReader instruction) + | throwError "The local instruction is not concrete" + return (← lastArgument snapshotReader found, operation) + +/-- Whether the snapshots track the fetched instruction named `operation` as a numeric local + store, or as a load unless `store`; an argument instruction counts only with a home. -/ +private def snapshotTracks (operation : Name) (methodFound instructionFound : Term) + (store : Bool) : TacticM Bool := do + if operation == (if store then ``CIL.Instruction.stloc else ``CIL.Instruction.ldloc) then + return true + unless operation == (if store then ``CIL.Instruction.starg else ``CIL.Instruction.ldarg) do + return false + let (method, fetched) ← snapshotFetched methodFound instructionFound + return (← snapshotLocal? method fetched store).isSome + /-- Store one numeric local and return its updated snapshot collection with the step taken. With `supplied`, the step is the one `facts` prove, to the memory they state, and the snapshots are about that memory. -/ @@ -405,10 +624,8 @@ private def snapshotStore (snapshot methodFound instructionFound : Term) | throwError "Expected a checked run equality" let some #[_, _, _, runPc, runArgs, runFrame, runStack, memory] := argumentsOf? runTerm ``run 8 | throwError "Expected a checked run" - let methodProof ← Term.elabTerm methodFound none - let some (_, _, found) := (← instantiateMVars (← inferType methodProof)).eq? - | throwError "Expected a method lookup" - let body ← Term.exprToSyntax (← lastArgument snapshotReader found) + let (method, operation) ← snapshotFetched methodFound instructionFound + let body ← Term.exprToSyntax method let pc ← Term.exprToSyntax runPc let args ← Term.exprToSyntax runArgs let frame ← Term.exprToSyntax runFrame @@ -420,16 +637,17 @@ private def snapshotStore (snapshot methodFound instructionFound : Term) | throwError "The local store is not numeric" let value ← Term.exprToSyntax scalar let rest ← Term.exprToSyntax remaining - let instructionProof ← Term.elabTerm instructionFound none - let some (_, _, instruction) := (← instantiateMVars (← inferType instructionProof)).eq? - | throwError "Expected an instruction lookup" - let some operation ← cilReducedOp? (← lastArgument snapshotReader instruction) - | throwError "The local store instruction is not concrete" - let storedIndex ← argumentAt snapshotReader operation 0 + let some (storedIndex, home) ← snapshotLocal? method operation true + | throwError "The instruction is not a numeric local store" + let memoryTerm ← Term.exprToSyntax memory + -- A homed argument's store is the store to its home slot, which the snapshot lemmas state. + let bridge ← home.mapM fun access => do + let access ← Term.exprToSyntax access + `(term| step_starg_home_stloc $body $pc $args $frame $memoryTerm _ (.scalar $value) $rest _ + $access) let known ← if supplied then let operationTerm ← Term.exprToSyntax operation let stackTerm ← Term.exprToSyntax runStack - let memoryTerm ← Term.exprToSyntax memory let known ← Term.withoutErrToSorry <| Term.withSynthesize <| Term.elabTerm (← `(term| (by cil_strict cil_checked_step_normalized [$facts,*] : step $body $operationTerm $pc $args $frame $stackTerm $memoryTerm = @@ -438,9 +656,13 @@ private def snapshotStore (snapshot methodFound instructionFound : Term) if known.hasMVar then throwError "The supplied store does not determine its result" pure (some (← Term.exprToSyntax known)) else pure none - if automatic then prepareSnapshotHome memory runFrame storedIndex facts + let scope ← SnapshotScope.start + if automatic then prepareSnapshotHome scope memory runFrame storedIndex facts let index ← Term.exprToSyntax storedIndex - let evidence ← match known with + let localKnown ← known.mapM fun known => match bridge with + | some bridge => `(term| ($bridge).symm.trans $known) + | none => pure known + let evidence ← match localKnown with | some known => `(term| LocalSnapshots.store_at (body := $body) (pc := $pc) (args := $args) (frame := $frame) (stack := $rest) (index := $index) @@ -462,59 +684,88 @@ private def snapshotStore (snapshot methodFound instructionFound : Term) if ← isProp (← pendingGoal.getType) then solveSnapshotPremise pendingGoal facts let proof ← instantiateMVars proof if proof.hasMVar then throwError "The local store has unresolved snapshot premises" - let proofTerm ← Term.exprToSyntax proof let names ← snapshotStoreNames let #[after, taken, updated, formed, below, retained, written] := names | throwError "{snapshotReader}: expected seven store names, but there are {names.size}" let stepped ← match known with | some known => - evalTactic (← `(tactic| obtain - ⟨$updated, $formed, $below, $retained, $written⟩ := $proofTerm)) + introConjuncts proof #[updated, formed, below, retained, written] pure known | none => - evalTactic (← `(tactic| obtain - ⟨$after, $taken, $updated, $formed, $below, $retained, $written⟩ := $proofTerm)) - pure (taken : Term) - -- The remembered value is stated as the value a load pushes, not as its `localResult`. + -- The conjuncts follow the memory the store produced. + let located := mkIdent (← mkFreshUserName `snapshotStored) + evalTactic (← `(tactic| cil_have_by $located := $(← Term.exprToSyntax proof))) + introWitness scope located after #[taken, updated, formed, below, retained, written] + match bridge with + | some bridge => `(term| ($bridge).trans $taken) + | none => pure (taken : Term) + -- The remembered value is stated as the value a load pushes, not as its `localResult`. Only + -- this store's value is restated: rebuilding the earlier updates would copy each of them again. withMainContext do if let some declaration := (← getLCtx).findFromUserName? updated.getId then let stated ← instantiateMVars declaration.type - let reduced ← reduceLocalResults stated - unless reduced == stated do - replaceMainGoal [← (← getMainGoal).replaceLocalDeclDefEq declaration.fvarId reduced] - preserveSnapshotAccess written + if let some #[produced, slots, chain] := argumentsOf? stated ``LocalSnapshots 3 then + if let some #[known, slot, remembered] := argumentsOf? chain ``rememberLocal 3 then + let reduced ← reduceLocalResults remembered + unless reduced == remembered do + let restated := mkApp3 stated.getAppFn produced slots + (mkApp3 chain.getAppFn known slot reduced) + replaceMainGoal [← (← getMainGoal).replaceLocalDeclDefEq declaration.fvarId restated] + preserveSnapshotAccess scope written if automatic then - preserveSnapshotFacts written below + preserveSnapshotFacts scope written below -- Caller buffers and reads below the homes' watermark hold unchanged after the store. - loopCarryBelow memory below - carrySnapshotReads memory below + loopCarryBelow memory below scope.superseded + carrySnapshotReads scope memory below + clearSuperseded (← scope.superseded.get) return { snapshots := updated, stepped, source := memory, below } +/-- The value the chain of updates `known` remembers at `index`: the result of the latest + `rememberLocal` at that index, when `known` is a chain of them over a closed index. -/ +private partial def rememberedAt? (known : Expr) (index : Nat) : MetaM (Option Expr) := do + unless known.isAppOfArity ``rememberLocal 3 do return none + let some updated ← getNatValue? (known.getArg! 1) | return none + if updated == index then + -- `rememberLocal known index result` is `fun slot => if slot = index then some result ...`. + return some (known.getArg! 2) + rememberedAt? (known.getArg! 0) index + /-- Read the remembered value at the current run's exact frame and stack. -/ private def snapshotLoad (snapshot methodFound instructionFound : Term) (facts : Array Term) : TacticM Term := withMainContext do let some runTerm := (← getMainTarget).find? (·.isAppOf ``run) | throwError "Expected a checked run" - let some #[_, _, _, runPc, runArgs, runFrame, runStack, _] := argumentsOf? runTerm ``run 8 + let some #[_, _, _, runPc, runArgs, runFrame, runStack, memory] := argumentsOf? runTerm ``run 8 | throwError "Expected a checked run" - let methodProof ← Term.elabTerm methodFound none - let some (_, _, found) := (← instantiateMVars (← inferType methodProof)).eq? - | throwError "Expected a method lookup" - let body ← Term.exprToSyntax (← lastArgument snapshotReader found) + let (method, operation) ← snapshotFetched methodFound instructionFound + let body ← Term.exprToSyntax method let pc ← Term.exprToSyntax runPc let args ← Term.exprToSyntax runArgs let frame ← Term.exprToSyntax runFrame let stack ← Term.exprToSyntax runStack - let instructionProof ← Term.elabTerm instructionFound none - let some (_, _, instruction) := (← instantiateMVars (← inferType instructionProof)).eq? - | throwError "Expected an instruction lookup" - let some operation ← cilReducedOp? (← lastArgument snapshotReader instruction) - | throwError "The local load instruction is not concrete" - let loadedIndex ← argumentAt snapshotReader operation 0 + let some (loadedIndex, home) ← snapshotLocal? method operation false + | throwError "The instruction is not a numeric local load" let snapshotProof ← Term.withoutErrToSorry <| Term.elabTerm snapshot none let snapshotType ← instantiateMVars (← inferType snapshotProof) let some #[_, _, known] := argumentsOf? snapshotType ``LocalSnapshots 3 | throwError "Expected local snapshots at the current memory" + -- A homed argument's load is the load of its home slot, which the snapshot lemma states. + let bridged (loaded : Term) : TacticM Term := do + let some access := home | return loaded + let access ← Term.exprToSyntax access + let memory ← Term.exprToSyntax memory + `(term| (step_ldarg_home_ldloc $body $pc $args $frame $memory _ $stack _ $access).trans + $loaded) + -- A value remembered by a store is read from the chain of updates itself, which a simp over the + -- whole chain would unfold at each load. + if let some position ← getNatValue? (← whnfR loadedIndex) then + if let some stored ← rememberedAt? known position then + if (stored.find? (·.isConstOf ``localResult)).isNone then + let value ← Term.exprToSyntax stored + let index ← Term.exprToSyntax loadedIndex + let load ← `(term| LocalSnapshots.load (body := $body) (pc := $pc) (args := $args) + (frame := $frame) (stack := $stack) (index := $index) $snapshot $value rfl) + return ← bridged load let selected := mkApp known loadedIndex if (← whnfD selected).isAppOf ``Option.none then throwError "The snapshots do not establish a value at this local index" @@ -532,8 +783,9 @@ private def snapshotLoad (snapshot methodFound instructionFound : Term) let value ← Term.exprToSyntax loaded let checked ← Term.exprToSyntax (← remembered.getProof) let index ← Term.exprToSyntax loadedIndex - `(term| LocalSnapshots.load (body := $body) (pc := $pc) (args := $args) (frame := $frame) - (stack := $stack) (index := $index) $snapshot $value $checked) + let loaded ← `(term| LocalSnapshots.load (body := $body) (pc := $pc) (args := $args) + (frame := $frame) (stack := $stack) (index := $index) $snapshot $value $checked) + bridged loaded /-- The memory of the goal's run, when the goal has one. -/ private def snapshotRunMemory : TacticM (Option Expr) := do @@ -564,42 +816,44 @@ syntax "cil_checked_steps" "(" &"snapshots" ":=" term ")" "[" term,* "]" : tacti elab_rules : tactic | `(tactic| cil_checked_steps (snapshots := $snapshot) [$facts:term,*]) => focus do let prepared ← prepareStepFacts facts.getElems - let mut snapshot := snapshot - let mut progressed := false - repeat - if (← getGoals).isEmpty then break - let saved ← saveState - try - let updated ← withMainContext do - let (methodFound, instructionFound, operation) ← - cilFetchedOp ``run_next (fun target => target.find? (·.isAppOf ``run)) 3 prepared - if operation == ``CIL.Op.setLocal then - let stored ← snapshotStore snapshot methodFound instructionFound prepared - evalTactic (← `(tactic| cil_strict rw - [run_next $methodFound $instructionFound $(stored.stepped)])) - pure stored.snapshots - else if operation == ``CIL.Op.local then - let loaded ← snapshotLoad snapshot methodFound instructionFound prepared - evalTactic (← `(tactic| cil_strict rw - [run_next $methodFound $instructionFound $loaded])) - pure snapshot - else if operation == ``CIL.Op.ret then - evalTactic (← `(tactic| cil_strict - (rw [run_return $methodFound $instructionFound - (by cil_checked_step_normalized [$[$prepared:term],*]) - (by first | rfl | (simp [$[$prepared:term],*]; rfl))] - try exact ⟨_, rfl⟩))) - pure snapshot - else - evalTactic (← `(tactic| cil_strict rw [run_next $methodFound $instructionFound - (by cil_checked_step_normalized [$[$prepared:term],*])])) - pure snapshot - snapshot := updated - progressed := true - catch error => - saved.restore - trace[CIL.snapshots] "{error.toMessageData}" - break + let progressed ← withNormalRun do + let mut snapshot := snapshot + let mut progressed := false + repeat + if (← getGoals).isEmpty then break + let saved ← saveState + try + let updated ← withNormalNodes prepared <| withMainContext do + let (methodFound, instructionFound, operation) ← + cilFetchedOp ``run_next (fun target => target.find? (·.isAppOf ``run)) 3 prepared + if ← snapshotTracks operation methodFound instructionFound true then + let stored ← snapshotStore snapshot methodFound instructionFound prepared + evalTactic (← `(tactic| cil_strict rw + [run_next $methodFound $instructionFound $(stored.stepped)])) + pure stored.snapshots + else if ← snapshotTracks operation methodFound instructionFound false then + let loaded ← snapshotLoad snapshot methodFound instructionFound prepared + evalTactic (← `(tactic| cil_strict rw + [run_next $methodFound $instructionFound $loaded])) + pure snapshot + else if operation == ``CIL.Instruction.ret then + evalTactic (← `(tactic| cil_strict + (rw [run_return $methodFound $instructionFound + (by cil_checked_step_normalized [$[$prepared:term],*]) + (by first | rfl | (simp [$[$prepared:term],*]; rfl))] + try exact ⟨_, rfl⟩))) + pure snapshot + else + evalTactic (← `(tactic| cil_strict rw [run_next $methodFound $instructionFound + (by cil_checked_step_normalized [$[$prepared:term],*])])) + pure snapshot + snapshot := updated + progressed := true + catch error => + saved.restore + trace[CIL.snapshots] "{error.toMessageData}" + break + return progressed unless progressed do throwError "Snapshot stepping took no step; justify the local premises" /-- Restate each supplied read or formed reference of `memory` that its name did not carry, @@ -621,7 +875,7 @@ private def carrySuppliedReads (memory : Expr) (below : Ident) (facts : Array Te let name := mkIdent (← mkFreshUserName `snapshotRead) let saved ← saveState try - evalTactic (← `(tactic| cil_strict have $name:ident := $transport)) + evalTactic (← `(tactic| cil_strict cil_have_by $name:ident := $transport)) carried := carried.push name catch _ => saved.restore @@ -634,8 +888,7 @@ private def snapshotEvidenceAt (memory : Expr) (facts : Array Term) : TacticM Bo withMainContext do let mentions (type : Expr) : Bool := (type.find? (· == memory)).isSome let context ← getLCtx - for declaration in context do - if declaration.isImplementationDetail then continue + for declaration in declarationsFrom context (← memoryWindowStart memory) do let type := (← instantiateMVars declaration.type).consumeMData unless mentions type do continue if type.isAppOfArity ``ValidCall 3 || !(objectEvidenceIn type).isEmpty then return true @@ -651,7 +904,7 @@ private def snapshotEvidenceAt (memory : Expr) (facts : Array Term) : TacticM Bo that fails. After each store it crosses, the step facts are prepared again from `facts` at the store's memory, where the store restated the caller facts they name. Returns whether a step was taken. -/ -private def snapshotContractWalk (initial : Term) (facts : Array Term) +private def snapshotContractWalkSteps (initial : Term) (facts : Array Term) (continuation : Option Term) (limit : Option Nat) : TacticM Bool := do let mut facts := facts let mut prepared ← prepareStepFacts facts @@ -675,19 +928,19 @@ private def snapshotContractWalk (initial : Term) (facts : Array Term) let stepFacts := prepared -- A numeric local's step through the snapshots; a store's step may be `supplied`. let remembered (supplied : Bool) : TacticM (Term × Option SnapshotStored) := - withMainContext do + withNormalNodes stepFacts <| withMainContext do let (found, instruction, operation) ← cilFetchedOp ``run_next_exists some 4 stepFacts - let (updated, checked, stored) ← if operation == ``CIL.Op.setLocal || - operation == ``CIL.Op.setVectorLocal then do + let tracksStore ← snapshotTracks operation found instruction true + let (updated, checked, stored) ← if tracksStore then do let stored ← snapshotStore snapshot found instruction stepFacts true supplied pure (stored.snapshots, stored.stepped, some stored) else if supplied then throwError "The instruction is not a numeric store" - else if operation == ``CIL.Op.local || operation == ``CIL.Op.vectorLocal then do + else if ← snapshotTracks operation found instruction false then do pure (snapshot, ← snapshotLoad snapshot found instruction stepFacts, none) else throwError "The instruction is not a numeric local" evalTactic (← `(tactic| ( - cil_strict refine run_next_exists (post := _) $found $instruction $checked ?_ + cil_strict cil_refine_by run_next_exists (post := _) $found $instruction $checked ?_ cil_stack_cleanup))) foldRunPc return (updated, stored) @@ -732,6 +985,11 @@ private def snapshotContractWalk (initial : Term) (facts : Array Term) prepared ← prepareStepFacts facts return progressed +/-- `snapshotContractWalkSteps` on the record of normal-form nodes of this walk. -/ +private def snapshotContractWalk (initial : Term) (facts : Array Term) + (continuation : Option Term) (limit : Option Nat) : TacticM Bool := + withNormalRun (snapshotContractWalkSteps initial facts continuation limit) + /-- Compose a finite checked execution while retaining numeric local snapshots. Supplied step facts take priority over generated stores, and the snapshots follow a supplied store to its memory when the store's premises hold. Homes stated at an earlier memory, or after @@ -756,4 +1014,4 @@ elab_rules : tactic focus do discard <| snapshotContractWalk initial facts.getElems none (some count.getNat) -end CIL.Safety \ No newline at end of file +end CIL.Safety diff --git a/CIL/Safety/LocalSnapshots.lean b/CIL/Safety/LocalSnapshots.lean index 17aa344a3..1dfbfbee1 100644 --- a/CIL/Safety/LocalSnapshots.lean +++ b/CIL/Safety/LocalSnapshots.lean @@ -134,7 +134,7 @@ theorem LocalSnapshots.load {body : CIL.Method} {pc index : Nat} {args stack : L {frame : Frame} {memory : Memory} {known : Nat → Option CIL.Value} (snapshots : LocalSnapshots memory frame.locals known) (value : CIL.Value) (specified : known index = some value) : - step body (.local index) pc args frame stack memory = + step body (.ldloc index) pc args frame stack memory = .ok (.next (pc + 1) (.scalar value :: stack) frame memory) := by obtain ⟨kind, home, stored, number, slot, fits, result, loaded⟩ := snapshots index value specified @@ -156,6 +156,16 @@ theorem LocalSnapshots.read_at {memory : Memory} {slots : List LocalSlot} cases located exact ⟨stored, number, fits, result, loaded⟩ +/-- A remembered 64-bit word reads back as its eight bytes. -/ +theorem LocalSnapshots.read_word64 {memory : Memory} {slots : List LocalSlot} + {known : Nat → Option CIL.Value} (snapshots : LocalSnapshots memory slots known) + {index : Nat} {home : Reference} {word : CIL.W64} + (specified : known index = some (.i64 word)) + (slot : slots[index]? = some (.bytes .word64 home)) : + read memory home 8 1 = .ok (numberBytes word.toNat 8) := by + obtain ⟨stored, number, fits, result, loaded⟩ := snapshots.read_at specified slot + cases stored <;> simp_all [localResult, localNumber, CIL.Value.hasUnspecified, localWidth] + /-- A remembered 128-bit vector reads back as its sixteen bytes. -/ theorem LocalSnapshots.read_vector128 {memory : Memory} {slots : List LocalSlot} {known : Nat → Option CIL.Value} (snapshots : LocalSnapshots memory slots known) @@ -199,7 +209,7 @@ theorem LocalSnapshots.store {body : CIL.Method} {pc index : Nat} {args stack : (slot : frame.locals[index]? = some (.bytes kind home)) (ready : access memory home (localWidth kind) 1 true = .ok ()) : ∃ after, - step body (.setLocal index) pc args frame (.scalar value :: stack) memory = + step body (.stloc index) pc args frame (.scalar value :: stack) memory = .ok (.next (pc + 1) stack frame after) ∧ LocalSnapshots after frame.locals (rememberLocal known index (localResult kind value)) ∧ after.WellFormed ∧ MemoryBelow boundary memory after ∧ @@ -328,6 +338,7 @@ theorem NumericHomes.snapshots {memory : Memory} {lower : Nat} {specs slots} #print axioms LocalSnapshots.after_write #print axioms LocalSnapshots.load #print axioms LocalSnapshots.read_at +#print axioms LocalSnapshots.read_word64 #print axioms LocalSnapshots.read_vector128 #print axioms LocalSnapshots.read_vector256 #print axioms LocalSnapshots.read_vector512 diff --git a/CIL/Safety/LocalStepRenaming.lean b/CIL/Safety/LocalStepRenaming.lean index e36b70d56..2b47a0ecd 100644 --- a/CIL/Safety/LocalStepRenaming.lean +++ b/CIL/Safety/LocalStepRenaming.lean @@ -13,77 +13,18 @@ frame correspondence, including missing-slot rejection. namespace CIL.Safety -/-- Initializing a span local preserves its relocated slot and backing allocation identity. -/ -theorem Frame.Renamed.stepInitializeSpanLocal {indices : LocalIndexMap} - {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} - {source target : Memory} {sourceFrame targetFrame : Frame} - (memories : Memory.Renamed mapping scopes source target) - (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (formed : source.WellFormed) (sourceBody targetBody : CIL.Method) - (position pc : Nat) (constructor : CIL.SpanConstructor) (arguments stack : List Value) - (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) - (observable : SuccessOrThrow - (step sourceBody (.initializeSpanLocal position constructor) pc arguments - sourceFrame stack source)) : - ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.initializeSpanLocal position constructor) pc arguments - sourceFrame stack source) - (step targetBody (.initializeSpanLocal (indices.index position) constructor) pc - (arguments.map mapping.value) targetFrame (stack.map mapping.value) target) := by - simp only [step, observerCount, Bool.or_false, mapping.unspecifiedOperands] at observable ⊢ - split at observable - · cases observable - · rename_i guard - simp only [guard, Bool.false_eq_true, ite_false] at observable ⊢ - have slots := frames.locals.getElem?_all position - generalize sourceFound : sourceFrame.locals[position]? = sourceSlot at slots observable ⊢ - generalize targetFound : targetFrame.locals[indices.index position]? = targetSlot at slots ⊢ - cases slots with - | none => exact .error .invalidState - | @some slot otherSlot slots => - rcases slots with ⟨bounded, rfl⟩ - apply (memories.memoryInstructionResult _ formed stack bounds observable.bind_left).bindWithSuccess - intro result otherResult constructed results - obtain ⟨after, values⟩ := result - obtain ⟨otherAfter, otherValues⟩ := otherResult - obtain ⟨related, rfl⟩ := results - cases values with - | nil => exact .error .invalidState - | cons value rest => - obtain ⟨sameMemory, valid⟩ := - spanConstructor_result constructor source after stack value rest constructed - subst after - have valueBound : value.Below mapping.sourceLimit := by - rw [memories.sourceLimit] - exact valid.below formed - apply (related.storeLocal slot value bounded valueBound).bindWithSuccess - intro result otherResult stored results - obtain ⟨slotAfter, sourceAfter⟩ := result - obtain ⟨otherSlotAfter, targetAfter⟩ := otherResult - rcases results with ⟨slotsEqual, related⟩ - change otherSlotAfter = mapping.localSlot slotAfter at slotsEqual - subst otherSlotAfter - have positionBound : position < indices.sourceLength := by - rw [← frames.locals.lengths.1] - exact (List.getElem?_eq_some_iff.mp sourceFound).1 - exact .ok (.next related (frames.setLocal position positionBound slotAfter - (storeLocal_below bounded valueBound stored))) - -#print axioms Frame.Renamed.stepInitializeSpanLocal - /-- A local load reads the same bounded slot through its relocated operand. -/ -theorem Frame.Renamed.stepLocal {indices : LocalIndexMap} {mapping : AllocationRenaming} +theorem Frame.Renamed.stepLdloc {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (memories : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (sourceBody targetBody : CIL.Method) (position pc : Nat) (narrow : Option CIL.NarrowKind) - (arguments stack : List Value) : + (sourceBody targetBody : CIL.Method) (position pc : Nat) (arguments stack : List Value) : ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.local position narrow) pc arguments sourceFrame stack source) - (step targetBody (.local (indices.index position) narrow) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by + (step sourceBody (.ldloc position) pc arguments sourceFrame stack source) + (step targetBody (.ldloc (indices.index position)) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, - Bool.false_or, Bool.false_eq_true, ite_false] + Bool.false_eq_true, ite_false] have slots := frames.locals.getElem?_all position generalize sourceFound : sourceFrame.locals[position]? = sourceSlot at slots ⊢ generalize targetFound : targetFrame.locals[indices.index position]? = targetSlot at slots ⊢ @@ -94,22 +35,23 @@ theorem Frame.Renamed.stepLocal {indices : LocalIndexMap} {mapping : AllocationR apply (memories.loadLocal slot bounded).bindWithSuccess intro value otherValue loaded same subst otherValue + rw [indices.push_of_not_home mapping stack (loadLocal_not_spanHome loaded)] exact .ok (.next memories frames) -#print axioms Frame.Renamed.stepLocal +#print axioms Frame.Renamed.stepLdloc -/-- A local address uses its retained byte home at the relocated operand. -/ -theorem Frame.Renamed.stepLocalAddress {indices : LocalIndexMap} {mapping : AllocationRenaming} +/-- A local address uses its retained byte home or span view at the relocated operand. -/ +theorem Frame.Renamed.stepLdloca {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (memories : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (sourceBody targetBody : CIL.Method) (position pc : Nat) (arguments stack : List Value) : ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.localAddr position) pc arguments sourceFrame stack source) - (step targetBody (.localAddr (indices.index position)) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by + (step sourceBody (.ldloca position) pc arguments sourceFrame stack source) + (step targetBody (.ldloca (indices.index position)) pc (arguments.map mapping.value) + targetFrame (stack.map (indices.value mapping)) target) := by simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, - Bool.false_or, Bool.false_eq_true, ite_false] + Bool.false_eq_true, ite_false] have slots := frames.locals.getElem?_all position generalize sourceFound : sourceFrame.locals[position]? = sourceSlot at slots ⊢ generalize targetFound : targetFrame.locals[indices.index position]? = targetSlot at slots ⊢ @@ -117,25 +59,26 @@ theorem Frame.Renamed.stepLocalAddress {indices : LocalIndexMap} {mapping : Allo | none => exact .error .invalidState | @some slot otherSlot slots => rcases slots with ⟨bounded, rfl⟩ - apply (memories.localAddress slot bounded).bindWithSuccess + apply (memories.localAddress position slot bounded).bindWithSuccess intro value otherValue loaded same subst otherValue exact .ok (.next memories frames) -#print axioms Frame.Renamed.stepLocalAddress +#print axioms Frame.Renamed.stepLdloca /-- A local store updates the corresponding slot without exposing private dead homes. -/ -theorem Frame.Renamed.stepSetLocal {indices : LocalIndexMap} {mapping : AllocationRenaming} +theorem Frame.Renamed.stepStloc {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} (memories : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (sourceBody targetBody : CIL.Method) (position pc : Nat) (narrow : Option CIL.NarrowKind) + (sourceBody targetBody : CIL.Method) (position pc : Nat) (arguments rest : List Value) (value : Value) (valueBound : value.Below mapping.sourceLimit) : ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.setLocal position narrow) pc arguments sourceFrame (value :: rest) source) - (step targetBody (.setLocal (indices.index position) narrow) pc - (arguments.map mapping.value) targetFrame ((value :: rest).map mapping.value) target) := by + (step sourceBody (.stloc position) pc arguments sourceFrame (value :: rest) source) + (step targetBody (.stloc (indices.index position)) pc + (arguments.map mapping.value) targetFrame ((value :: rest).map (indices.value mapping)) + target) := by simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, - Bool.false_or, Bool.false_eq_true, ite_false, List.map_cons] + Bool.false_eq_true, ite_false, List.map_cons] have slots := frames.locals.getElem?_all position generalize sourceFound : sourceFrame.locals[position]? = sourceSlot at slots ⊢ generalize targetFound : targetFrame.locals[indices.index position]? = targetSlot at slots ⊢ @@ -156,48 +99,6 @@ theorem Frame.Renamed.stepSetLocal {indices : LocalIndexMap} {mapping : Allocati have updated := frames.setLocal position positionBound slotAfter (storeLocal_below bounded valueBound stored) exact .ok (.next memories updated) -#print axioms Frame.Renamed.stepSetLocal - -/-- Vector local loads use the same relocation as ordinary local loads. -/ -theorem Frame.Renamed.stepVectorLocal {indices : LocalIndexMap} {mapping : AllocationRenaming} - {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} - (memories : Memory.Renamed mapping scopes source target) - (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (sourceBody targetBody : CIL.Method) (position pc : Nat) (arguments stack : List Value) : - ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.vectorLocal position) pc arguments sourceFrame stack source) - (step targetBody (.vectorLocal (indices.index position)) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := - frames.stepLocal memories sourceBody targetBody position pc none arguments stack -#print axioms Frame.Renamed.stepVectorLocal - -/-- Vector local addresses retain the same relocated byte-home address. -/ -theorem Frame.Renamed.stepVectorLocalAddress {indices : LocalIndexMap} - {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} - {source target : Memory} {sourceFrame targetFrame : Frame} - (memories : Memory.Renamed mapping scopes source target) - (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (sourceBody targetBody : CIL.Method) (position pc : Nat) (arguments stack : List Value) : - ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.vectorLocalAddr position) pc arguments sourceFrame stack source) - (step targetBody (.vectorLocalAddr (indices.index position)) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := - frames.stepLocalAddress memories sourceBody targetBody position pc arguments stack -#print axioms Frame.Renamed.stepVectorLocalAddress - -/-- Vector local stores update the retained slot through the same index map. -/ -theorem Frame.Renamed.stepSetVectorLocal {indices : LocalIndexMap} {mapping : AllocationRenaming} - {scopes : Nat → Nat → Prop} {source target : Memory} {sourceFrame targetFrame : Frame} - (memories : Memory.Renamed mapping scopes source target) - (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) - (sourceBody targetBody : CIL.Method) (position pc : Nat) (arguments rest : List Value) - (value : Value) (valueBound : value.Below mapping.sourceLimit) : - ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) - (step sourceBody (.setVectorLocal position) pc arguments sourceFrame (value :: rest) source) - (step targetBody (.setVectorLocal (indices.index position)) pc - (arguments.map mapping.value) targetFrame ((value :: rest).map mapping.value) target) := - frames.stepSetLocal memories sourceBody targetBody position pc none arguments rest value - valueBound -#print axioms Frame.Renamed.stepSetVectorLocal +#print axioms Frame.Renamed.stepStloc end CIL.Safety diff --git a/CIL/Safety/LocalStoreRenaming.lean b/CIL/Safety/LocalStoreRenaming.lean index e4784ad63..dc5a01c14 100644 --- a/CIL/Safety/LocalStoreRenaming.lean +++ b/CIL/Safety/LocalStoreRenaming.lean @@ -25,7 +25,7 @@ theorem AllocationRenaming.localNumber (mapping : AllocationRenaming) #print axioms AllocationRenaming.localNumber /-- A slot store preserves corresponding metadata, contents and memory authority. -/ -theorem Memory.Renamed.storeLocal {mapping : AllocationRenaming} +theorem Memory.Renamed.storeLocal {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} (related : Memory.Renamed mapping scopes source target) (slot : LocalSlot) (value : Value) (slotBound : slot.Below mapping.sourceLimit) (valueBound : value.Below mapping.sourceLimit) : @@ -33,7 +33,7 @@ theorem Memory.Renamed.storeLocal {mapping : AllocationRenaming} target.1 = mapping.localSlot source.1 ∧ Memory.Renamed mapping scopes source.2 target.2) (storeLocal source slot value) - (storeLocal target (mapping.localSlot slot) (mapping.value value)) := by + (storeLocal target (mapping.localSlot slot) (indices.value mapping value)) := by cases slot with | methodPointer signature stored | methodTable signature stored => cases value @@ -45,7 +45,7 @@ theorem Memory.Renamed.storeLocal {mapping : AllocationRenaming} | exact .error _ | exact .ok ⟨rfl, related⟩ all_goals simp only [CIL.Safety.storeLocal, AllocationRenaming.localSlot, - AllocationRenaming.value, Value.hasUnspecified, Bool.false_and, Bool.false_eq_true, + LocalIndexMap.value, Value.hasUnspecified, Bool.false_and, Bool.false_eq_true, ↓reduceIte] all_goals repeat' first | contradiction @@ -57,34 +57,34 @@ theorem Memory.Renamed.storeLocal {mapping : AllocationRenaming} | scalar scalar => cases scalar <;> first | exact .error _ | exact .ok ⟨rfl, related⟩ | reference _ | span _ _ | bufferSpan _ | readOnlyReference _ | array _ - | methodPointer _ | methodTable _ | object _ => exact .error _ + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ | object _ | spanHome _ => exact .error _ | root stored | readOnlyRoot stored => cases value with | reference reference | readOnlyReference reference => dsimp only [CIL.Safety.storeLocal, AllocationRenaming.localSlot, - AllocationRenaming.value, Value.hasUnspecified, Bool.and] + LocalIndexMap.value, Value.hasUnspecified, Bool.and] apply (related.formValue reference valueBound).bind intro left right values exact .ok ⟨rfl, related⟩ | scalar scalar => cases scalar <;> exact .error _ - | span _ _ | bufferSpan _ | array _ | methodPointer _ | methodTable _ | object _ => - exact .error _ + | span _ _ | bufferSpan _ | array _ | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ | object _ + | spanHome _ => exact .error _ | object stored => cases value with | object reference => dsimp only [CIL.Safety.storeLocal, AllocationRenaming.localSlot, - AllocationRenaming.value, Value.hasUnspecified, Bool.and] + LocalIndexMap.value, Value.hasUnspecified, Bool.and] apply (related.formObject reference valueBound).bind intro left right values exact .ok ⟨rfl, related⟩ | scalar scalar => cases scalar <;> exact .error _ | reference _ | readOnlyReference _ | span _ _ | bufferSpan _ | array _ - | methodPointer _ | methodTable _ => exact .error _ + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ | spanHome _ => exact .error _ | bytes kind reference => cases value with | scalar scalar => cases scalar <;> simp only [CIL.Safety.storeLocal, AllocationRenaming.localSlot, - AllocationRenaming.value, Value.hasUnspecified, CIL.Value.hasUnspecified, + LocalIndexMap.value, Value.hasUnspecified, CIL.Value.hasUnspecified, Bool.not_false, Bool.and_true, Bool.false_eq_true, ↓reduceIte] all_goals first | exact .error _ @@ -102,18 +102,18 @@ theorem Memory.Renamed.storeLocal {mapping : AllocationRenaming} intro left right stored exact .ok ⟨rfl, stored⟩) | reference _ | span _ _ | bufferSpan _ | readOnlyReference _ | array _ - | methodPointer _ | methodTable _ | object _ => exact .error _ + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ | object _ | spanHome _ => exact .error _ | span width writable stored => cases value with | scalar scalar => cases scalar <;> exact .error _ | reference _ | readOnlyReference _ | array _ - | methodPointer _ | methodTable _ | object _ => exact .error _ + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ | object _ | spanHome _ => exact .error _ | span reference length => cases writable with | true => exact .error .invalidState | false => dsimp only [CIL.Safety.storeLocal, AllocationRenaming.localSlot, - AllocationRenaming.value, Value.hasUnspecified, Bool.and] + LocalIndexMap.value, Value.hasUnspecified, Bool.and] have checked := related.spanValidate ⟨reference, length, width, false⟩ valueBound dsimp only [AllocationRenaming.spanView] at checked rw [checked] @@ -122,7 +122,7 @@ theorem Memory.Renamed.storeLocal {mapping : AllocationRenaming} | ok _ => exact .ok ⟨rfl, related⟩ | bufferSpan view => dsimp only [CIL.Safety.storeLocal, AllocationRenaming.localSlot, - AllocationRenaming.value, AllocationRenaming.spanView, Value.hasUnspecified, Bool.and] + LocalIndexMap.value, AllocationRenaming.spanView, Value.hasUnspecified, Bool.and] have checked := related.spanValidate view valueBound dsimp only [AllocationRenaming.spanView] at checked rw [checked] @@ -140,7 +140,7 @@ theorem Memory.Renamed.storeLocal {mapping : AllocationRenaming} cases reference with | address _ => exact .error .invalidState | null => - simp only [CIL.Safety.storeLocal, AllocationRenaming.localSlot, AllocationRenaming.value, + simp only [CIL.Safety.storeLocal, AllocationRenaming.localSlot, LocalIndexMap.value, Value.hasUnspecified, Bool.and, Bool.false_eq_true, ite_false, AllocationRenaming.managedReference, ArrayView.validate, ArrayView.storage, SpanView.validate] by_cases width : 0 < element.width ∧ element.width < 2^64 @@ -149,10 +149,10 @@ theorem Memory.Renamed.storeLocal {mapping : AllocationRenaming} · exact .ok ⟨rfl, related⟩ · exact .error _ | span _ _ | readOnlyReference _ | bufferSpan _ - | methodPointer _ | methodTable _ | reference _ => exact .error _ + | typeHandle _ | runtimeType _ | methodPointer _ | methodTable _ | reference _ | spanHome _ => exact .error _ | array view => dsimp only [CIL.Safety.storeLocal, AllocationRenaming.localSlot, - AllocationRenaming.value, AllocationRenaming.arrayView, Value.hasUnspecified, Bool.and] + LocalIndexMap.value, AllocationRenaming.arrayView, Value.hasUnspecified, Bool.and] have checked := related.arrayValidate view valueBound dsimp only [AllocationRenaming.arrayView] at checked rw [checked] diff --git a/CIL/Safety/LoopAutomation.lean b/CIL/Safety/LoopAutomation.lean index cfa0be4e9..bb72e4257 100644 --- a/CIL/Safety/LoopAutomation.lean +++ b/CIL/Safety/LoopAutomation.lean @@ -21,7 +21,7 @@ namespace CIL.Safety /-- Locate an invariant cutpoint by instruction shape. Missing matches point past the body and cannot satisfy the checked fetch obligations below. -/ -def instructionLocation (body : CIL.Method) (predicate : CIL.Op → Bool) : Nat := +def instructionLocation (body : CIL.Method) (predicate : CIL.Instruction → Bool) : Nat := body.code.findIdx predicate /-- A finite checked path whose cost is inferred while composing its instructions. -/ @@ -36,7 +36,7 @@ theorem loop_reaches_refl {program : CIL.Program} {method : Nat} {args : List Va /-- Prepends one fetched non-branching step to a loop path. -/ theorem loop_reaches_next {program : CIL.Program} {method pc next : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} {frame nextFrame : Frame} {memory updated : Memory} {target : LoopState} (found : program[method]? = some body) (instruction : body.code[pc]? = some op) @@ -52,7 +52,7 @@ theorem loop_reaches_next {program : CIL.Program} {method pc next : Nat} /-- A state terminates if its fetched instruction steps to a state that terminates. -/ theorem loop_terminates_next {program : CIL.Program} {method pc next : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} {frame nextFrame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (found : program[method]? = some body) (instruction : body.code[pc]? = some op) @@ -63,7 +63,7 @@ theorem loop_terminates_next {program : CIL.Program} {method pc next : Nat} /-- A fetched `ret` whose checked results satisfy `post` terminates. -/ theorem loop_terminates_return {program : CIL.Program} {method pc : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (found : program[method]? = some body) (instruction : body.code[pc]? = some op) @@ -86,7 +86,7 @@ theorem loop_reaches_terminates {program : CIL.Program} {method : Nat} {args : L #print axioms loop_reaches_terminates /-- Where a loop stepping tactic takes its goal: to the target state of a `LoopReaches`, or along - a `LoopTerminates` through its return, only up to a call or return, or up to the pc `target` + a `LoopTerminates` through its return, up to a linked call or return, or up to the pc `target` and any struct store before it. -/ inductive LoopStepping where | reach @@ -151,11 +151,15 @@ def loopStateParts? (state : Expr) : MetaM (Option (Array Expr)) := do /-- The instruction a loop goal's source state fetches, the proofs of its method and instruction lookups, and the step obligation taking it leaves. -/ structure LoopFetched where - /-- The fetched instruction. -/ + /-- The instruction used to select loop facts. -/ op : Lean.Expr + /-- An unlinked call runs its model or faults in the current frame. -/ + unlinkedCall : Bool + /-- The offset and checked shape equation for a primitive or vector struct field. -/ + valueField : Option (Lean.Expr × Lean.Expr) /-- A proof that the program holds the goal's method. -/ found : Lean.Expr - /-- A proof that the method's code holds `op` at the goal's pc. -/ + /-- A proof of the original instruction lookup at the goal's pc. -/ instruction : Lean.Expr /-- The obligation that `op` steps to a next state. -/ step : Lean.MVarId @@ -185,80 +189,172 @@ def loopFetch (facts : Array Term) : TacticM LoopFetched := withMainContext do let found ← instantiateMVars methodLookup let instruction ← instantiateMVars instructionLookup let op ← lastArgument "loopFetch" op + let code? := fetched.find? (·.isAppOfArity ``CIL.Method.code 1) + let resolvedField : MetaM (Option (Expr × Expr)) := do + let some code := code? | return none + let (name, #[field]) := (← whnfR op).getAppFnArgs | pure none + unless name == ``CIL.Instruction.ldfld || name == ``CIL.Instruction.stfld do return none + let some (shape, proof) ← fieldShapeKnown? code.appArg! field | pure none + let_expr CIL.FieldShape.value offset _ _ := shape | pure none + return some (offset, proof) + let valueField ← resolvedField let .mvar step := stepPremise | throwError "loopFetch: expected a step premise, but found{indentExpr stepPremise}" - return { op, found, instruction, step } + let unlinkedCall ← if (← whnfR op).isAppOf ``CIL.Instruction.call then do + let some (_, action, _) := (← instantiateMVars (← step.getType)).eq? | pure false + let some #[body, _, pc, _, _, _, _] := argumentsOf? action ``CIL.Safety.step 7 | pure false + unlinkedPc body pc + else pure false + return { op, unlinkedCall, valueField, found, instruction, step } /-- Instructions that only move values, which no case split decides. -/ def loopMovesOnly : List Lean.Name := - [``CIL.Op.nop, ``CIL.Op.arg, ``CIL.Op.vectorArg, ``CIL.Op.vectorArgAddr, ``CIL.Op.local, - ``CIL.Op.vectorLocal, ``CIL.Op.vectorLocalAddr, ``CIL.Op.setVectorLocal, ``CIL.Op.localAddr, - ``CIL.Op.setLocal, ``CIL.Op.const32, ``CIL.Op.const64, ``CIL.Op.branch, ``CIL.Op.skipInit, - ``CIL.Op.initAggregate, ``CIL.Op.fieldAddress] + [``CIL.Instruction.nop, ``CIL.Instruction.ldarg, ``CIL.Instruction.starg, + ``CIL.Instruction.ldarga, ``CIL.Instruction.ldloc, ``CIL.Instruction.ldloca, + ``CIL.Instruction.stloc, ``CIL.Instruction.ldc_i4, + ``CIL.Instruction.ldc_i8, ``CIL.Instruction.br, + ``CIL.Instruction.initobj, ``CIL.Instruction.ldflda] /-- Instructions that read no reference: they compare or compute on scalars, or move a value that `loopReadsReferences` shows is a scalar. -/ def loopScalarOnly : List Lean.Name := - [``CIL.Op.nop, ``CIL.Op.arg, ``CIL.Op.local, ``CIL.Op.localAddr, ``CIL.Op.setLocal, - ``CIL.Op.const32, ``CIL.Op.const64, ``CIL.Op.branch, ``CIL.Op.nativeBinary, ``CIL.Op.divide, - ``CIL.Op.checkedArithmetic, ``CIL.Op.checkedConvert, ``CIL.Op.convU2, ``CIL.Op.bxor, - ``CIL.Op.pop, ``CIL.Op.gt, ``CIL.Op.bgt, ``CIL.Op.ble, ``CIL.Op.bleu, ``CIL.Op.switch, - ``CIL.Op.brzero, ``CIL.Op.brnonzero, ``CIL.Op.bltu, ``CIL.Op.bgeu, ``CIL.Op.bgtu, - ``CIL.Op.bge, ``CIL.Op.blt, ``CIL.Op.beq, ``CIL.Op.bne] + [``CIL.Instruction.nop, ``CIL.Instruction.ldarg, ``CIL.Instruction.starg, + ``CIL.Instruction.ldarga, ``CIL.Instruction.ldloc, ``CIL.Instruction.ldloca, + ``CIL.Instruction.stloc, ``CIL.Instruction.ldc_i4, ``CIL.Instruction.ldc_i8, + ``CIL.Instruction.br, + ``CIL.Instruction.div, ``CIL.Instruction.div_un, ``CIL.Instruction.rem, + ``CIL.Instruction.rem_un, ``CIL.Instruction.add_ovf, ``CIL.Instruction.add_ovf_un, + ``CIL.Instruction.sub_ovf, ``CIL.Instruction.sub_ovf_un, ``CIL.Instruction.mul_ovf, + ``CIL.Instruction.mul_ovf_un, ``CIL.Instruction.conv_ovf, ``CIL.Instruction.conv_ovf_un, + ``CIL.Instruction.conv_u2, ``CIL.Instruction.xor, + ``CIL.Instruction.pop, ``CIL.Instruction.cgt, ``CIL.Instruction.bgt, ``CIL.Instruction.ble, + ``CIL.Instruction.ble_un, ``CIL.Instruction.switch, ``CIL.Instruction.brfalse, + ``CIL.Instruction.brtrue, ``CIL.Instruction.blt_un, ``CIL.Instruction.bge_un, + ``CIL.Instruction.bgt_un, ``CIL.Instruction.bge, ``CIL.Instruction.blt, + ``CIL.Instruction.beq, ``CIL.Instruction.bne_un] + +open Lean Meta in +/-- Constructor-spine entries of the last local list inspected by a loop walk. Slot expressions + are reduced afresh in the current context; no context-dependent reduction is retained. -/ +structure LoopLocalIndex where + /-- The exact list expression whose constructor prefix is indexed. -/ + source : Option Expr := none + /-- The syntactically present slots, in list order. -/ + entries : Array Expr := #[] + /-- The tail after those slots; a literal nil proves every later position absent. -/ + tail : Option Expr := none + deriving Inhabited + +open Lean Meta in +/-- Read a local slot, indexing an unchanged constructor spine once per walk. A nonconstructor + tail is reduced in the current context on each access beyond the indexed prefix. -/ +def loopLocalAt (locals : Expr) (index : Nat) : StateT LoopLocalIndex MetaM (Option Expr) := do + let locals ← instantiateMVars locals + let cached ← get + let (entries, tail) ← if !locals.hasMVar && cached.source == some locals then + pure (cached.entries, cached.tail.getD locals) + else do + let mut entries := #[] + let mut tail := locals + while let some #[_, slot, rest] := argumentsOf? tail.consumeMData ``List.cons 3 do + entries := entries.push slot + tail := rest + if !locals.hasMVar then + set ({ source := some locals, entries, tail := some tail } : LoopLocalIndex) + pure (entries, tail) + if let some slot := entries[index]? then return some slot + let mut tail := tail + let mut position := entries.size + repeat + let some #[_, slot, rest] := argumentsOf? (← whnf tail) ``List.cons 3 | return none + if position == index then return some slot + position := position + 1 + tail := rest + return none + +open Lean Meta in +/-- The frame slot of the home of argument `indexTerm` of `body`, when the body reads it from a + home. -/ +def loopHomeSlot? (body indexTerm : Expr) : MetaM (Option Nat) := do + let some (slot, _) ← ResolutionFacts.argumentHome? body indexTerm | return none + literalNat? slot open Lean Meta in /-- Whether the step obligation `step` of the fetched `op` can check a reference or span: any - instruction but `loopScalarOnly`, an argument load or local store of a non-scalar, and a load - of a local that is not a byte home. -/ -def loopReadsReferences (op : Expr) (step : MVarId) : MetaM Bool := do + instruction but `loopScalarOnly`, an argument load or local store of a non-scalar, a load of a + local that is not a byte home or holds a reference, and the address of a local that is not a + byte home. A homed argument is loaded, stored and addressed as its home's local. -/ +def loopReadsReferences (op : Expr) (step : MVarId) : StateT LoopLocalIndex MetaM Bool := do let op ← whnfR op let some name := op.getAppFn.constName? | return true unless loopScalarOnly.contains name do return true let scalar (value : Expr) : MetaM Bool := return (← whnfR value).isAppOf ``Value.scalar let some (_, lhs, _) := (← instantiateMVars (← step.getType)).consumeMData.eq? | return true - let some #[_, _, _, args, frame, stack, _] := argumentsOf? lhs ``step 7 | return true - if name == ``CIL.Op.arg then - -- An argument is checked as it is loaded, so a reference argument's `form` is read. + let some #[body, _, _, args, frame, stack, _] := argumentsOf? lhs ``step 7 | return true + let argumentOp := name == ``CIL.Instruction.ldarg || name == ``CIL.Instruction.starg || + name == ``CIL.Instruction.ldarga + let home ← if argumentOp then + match argument? op 0 with + | some indexTerm => loopHomeSlot? body indexTerm + | none => pure none + else pure none + if argumentOp && home.isNone then + -- An argument is checked as it is loaded, so a reference argument's `form` is read; a store + -- or address of an argument without a home faults. + unless name == ``CIL.Instruction.ldarg do return true let some indexTerm := argument? op 0 | return true let some index ← literalNat? indexTerm | return true + let some access ← ResolutionFacts.argumentAccessProof? body indexTerm | return true + let some (_, _, access) := (← inferType access).eq? | return true + unless (← whnfD access).isAppOf ``CIL.ArgumentAccess.value do return true let entries ← listEntries args let some (_, value) := entries.find? (·.1 == index) | return true return !(← scalar value) - if name == ``CIL.Op.setLocal then + if name == ``CIL.Instruction.stloc || name == ``CIL.Instruction.starg then let stack ← whnfR stack let some #[_, top, _] := argumentsOf? stack ``List.cons 3 | return true return !(← scalar top) - if name == ``CIL.Op.local then - -- Loading a span or reference local checks it. - let some indexTerm := argument? op 0 | return true - let some index ← literalNat? indexTerm | return true + let loads := name == ``CIL.Instruction.ldloc || name == ``CIL.Instruction.ldarg + if loads || name == ``CIL.Instruction.ldloca || name == ``CIL.Instruction.ldarga then + -- Loading a span or reference local checks it, and so does taking a span local's address. + let mut index := 0 + if let some slot := home then index := slot + else + let some indexTerm := argument? op 0 | return true + let some literal ← literalNat? indexTerm | return true + index := literal let frame ← whnfR frame - let some #[_, locals, _, _, _] := argumentsOf? frame ``Frame.mk 5 | return true - let some (_, slot) := (← listEntries locals).find? (·.1 == index) | return true + let some #[_, locals, _, _] := argumentsOf? frame ``Frame.mk 4 | return true + let some slot ← loopLocalAt locals index | return true let slot ← whnfR slot let some #[kind, _] := argumentsOf? slot ``LocalSlot.bytes 2 | return true - return (← whnfR kind).isConstOf ``CIL.LocalKind.reference + return loads && (← whnfR kind).isConstOf ``CIL.LocalKind.reference return false open Lean Meta in /-- Whether the store `op` with obligation `step` can step without the frame lemmas: it is not a byte home's local store or a struct field store or zeroing, or a hypothesis or one of `known` states a store or step at its memory, such as the frame lemmas produce. -/ -def loopStoreKnown (op : Expr) (step : MVarId) (known : Array Expr) : MetaM Bool := +def loopStoreKnown (op : Expr) (step : MVarId) (known : Array Expr) : + StateT LoopLocalIndex MetaM Bool := step.withContext do let op ← whnfR op let some (_, lhs, _) := (← instantiateMVars (← step.getType)).consumeMData.eq? | return true - let some #[_, _, _, _, frame, _, memory] := argumentsOf? lhs ``CIL.Safety.step 7 + let some #[body, _, _, _, frame, _, memory] := argumentsOf? lhs ``CIL.Safety.step 7 | return true - if op.isAppOf ``CIL.Op.setLocal then - -- A span or reference local's store reduces without a fact. + let homed := op.isAppOf ``CIL.Instruction.starg + if op.isAppOf ``CIL.Instruction.stloc || homed then + -- A span or reference local's store reduces without a fact; a homed argument's store is the + -- store to its home's local. let some indexTerm := argument? op 0 | return true - let some index ← literalNat? indexTerm | return true + let some index ← (if homed then loopHomeSlot? body indexTerm else literalNat? indexTerm) + | return true let frame ← whnfR frame - let some #[_, locals, _, _, _] := argumentsOf? frame ``Frame.mk 5 | return true - let some (_, slot) := (← listEntries locals).find? (·.1 == index) | return true + let some #[_, locals, _, _] := argumentsOf? frame ``Frame.mk 4 | return true + let some slot ← loopLocalAt locals index | return true unless (← whnfR slot).isAppOf ``LocalSlot.bytes do return true - else unless op.isAppOf ``CIL.Op.setFieldAt || op.isAppOf ``CIL.Op.initAggregate do + else unless + op.isAppOf ``CIL.Instruction.stfld || op.isAppOf ``CIL.Instruction.initobj do return true let heads := [``storeLocal, ``storeValue, ``CIL.Safety.step] let states (type : Expr) : Bool := (type.find? fun part => @@ -271,12 +367,13 @@ def loopStoreKnown (op : Expr) (step : MVarId) (known : Array Expr) : MetaM Bool /-- The groups of `facts` the instruction `op` reads besides its core: a return reads them all, since its postcondition may speak of anything the loop proved, and the reference facts only an instruction that `references` says can check a reference or span. -/ -def LoopFacts.readBy (facts : LoopFacts) (op : Lean.Name) (references : Bool) : +def LoopFacts.readBy (facts : LoopFacts) (op : Lean.Name) (references valueField : Bool) : Array (Array Lean.Syntax.Term) := - if op == ``CIL.Op.ret then + if op == ``CIL.Instruction.ret then #[facts.addresses, facts.fields, facts.cases, facts.references] else - #[if op == ``CIL.Op.localAddr || op == ``CIL.Op.vectorLocalAddr then facts.addresses else #[], - if op == ``CIL.Op.fieldAt then facts.fields else #[], + #[if op == ``CIL.Instruction.ldloca || op == ``CIL.Instruction.ldarga then facts.addresses + else #[], + if op == ``CIL.Instruction.ldfld && valueField then facts.fields else #[], if loopMovesOnly.contains op then #[] else facts.cases, if references then facts.references else #[]] @@ -286,7 +383,9 @@ def loopMissingHint (residual : Array Lean.Expr) : Lean.MetaM Lean.MessageData : open Lean Elab Tactic Meta in /-- Fail, as `tactic`, naming why the loop goal cannot be stepped: an undecided branch, an - instruction that cannot be fetched, a call, or the residual of the step at its pc. Returns + instruction that cannot be fetched, a call into a callee, a call with neither a link nor a + model, or the residual of the step at its pc, which for a call or construction without a link + is the step of its runtime model. Returns instead when `stops` accepts the fetched opcode, as a call or return a later tactic takes. -/ def loopStuck (tactic : String) (facts prepared : Array Term) (stops : Lean.Name → Bool) : TacticM Unit := withMainContext do @@ -303,10 +402,19 @@ def loopStuck (tactic : String) (facts prepared : Array Term) saved.restore throwError "{tactic}: cannot fetch the instruction at pc {position}: {error.toMessageData}" let op := fetched.op - let stepGoal := fetched.step let opName := ((← whnfR op).getAppFn.constName?).getD Name.anonymous - if stops opName then saved.restore; return - if callsCallee opName then + let frameStore := opName != ``CIL.Instruction.stfld || fetched.valueField.isSome + if !fetched.unlinkedCall && frameStore && stops opName then saved.restore; return + -- A call without a link runs its model in this method, so its step is shown like any other. + let modelled ← if opName == ``CIL.Instruction.call || opName == ``CIL.Instruction.newobj then + modelledCallStep? fetched.step + else pure none + if modelled.isNone && (← unmodelledCall fetched.step) then + saved.restore + throwError "{tactic}: pc {position} ({op}) has neither a link nor a runtime model, so no step \ + crosses it" + let stepGoal := modelled.getD fetched.step + if !fetched.unlinkedCall && callsCallee opName then saved.restore throwError "{tactic}: pc {position} is a call ({op}); cross it with `cil_loop_call` and the \ callee's contract" @@ -318,10 +426,12 @@ def loopStuck (tactic : String) (facts prepared : Array Term) | some goals => MessageData.joinSep (goals.toList.map (m!"{loopLiteralSums ·}")) "\n" | none => m!"no step equation or fact rewrites it" let hint ← loopMissingHint (residual.getD #[obligation]) - throwError "{tactic}: cannot step pc {position} ({op}) at memory{indentExpr memory}\n\ - The step is{indentExpr obligation}\n\ - With the step equations and facts it reduces to{indentD shown}\n\ - Supply {hint}." + -- Unassigned parts print as `?_`, so the text does not depend on metavariable numbering. + withOptions (pp.mvars.set · false) do + throwError "{tactic}: cannot step pc {position} ({op}) at memory{indentExpr memory}\n\ + The step is{indentExpr obligation}\n\ + With the step equations and facts it reduces to{indentD shown}\n\ + Supply {hint}." open Lean Elab Tactic Meta in /-- Fail, as `tactic`, for a `LoopReaches` goal that has stepped back to its target's pc in @@ -344,8 +454,17 @@ open Lean Elab Tactic Meta in /-- Whether a loop goal mentions a list operation that `loopStackLemmas` puts in literal form. -/ def loopMentionsStackOperations : TacticM Bool := withMainContext do let operations := [``List.drop, ``List.take, ``List.reverse, ``HAppend.hAppend] - return ((← instantiateMVars (← getMainTarget)).find? fun term => - term.constName?.any operations.contains).isSome + -- Once the walk uses the record, the terms below which none occurs are remembered, so a long + -- walk reads only the terms each step produced. + if (← usableNormalNodes).isNone then + return ((← instantiateMVars (← getMainTarget)).find? fun term => + term.constName?.any operations.contains).isSome + let name := "loopMentionsStackOperations" + let known ← normalNodesFor name + let (found, (_, clean)) := (cleanWalk (·.constName?.any operations.contains) known + (← instantiateMVars (← getMainTarget))).run ({}, #[]) + recordNormalNodes name clean + return found open Lean Elab Tactic Meta in /-- Step a loop goal through fetched instructions with the `facts` as `mode` directs, failing @@ -356,15 +475,15 @@ open Lean Elab Tactic Meta in fact steps. A finishing walk takes its return through `finishReturn`, given the fetched return and the facts it reads, or else leaves a return the facts do not close as the goal. Returns the opcode the walk stopped at before stepping it, if any. -/ -def loopSteps (tactic : String) (mode : LoopStepping) (facts : LoopFacts) (prepared : Array Term) - (stepped : Bool := false) - (finishReturn : Option (LoopFetched → Array Term → TacticM Unit) := none) : +private def loopStepsRun (tactic : String) (mode : LoopStepping) (facts : LoopFacts) + (prepared : Array Term) (stepped : Bool) + (finishReturn : Option (LoopFetched → Array Term → TacticM Unit)) : TacticM (Option Lean.Name) := do let stackLemmas : Array Term := loopStackLemmas.map fun name => mkIdent (`_root_ ++ name) let core := facts.core let withStack := core ++ stackLemmas - let rewrite ← `(tactic| simp only [$[$core:term],*]) - let rewriteStack ← `(tactic| simp only [$[$withStack:term],*]) + let rewrite ← `(tactic| cil_simp_only [$[$core:term],*]) + let rewriteStack ← `(tactic| cil_simp_only [$[$withStack:term],*]) let close ← `(tactic| exact CIL.Safety.loop_reaches_refl) -- Each group of `LoopFacts.readBy`, once normalised. let mut normalised : Array (Option (Array Term)) := #[none, none, none, none] @@ -387,17 +506,27 @@ def loopSteps (tactic : String) (mode : LoopStepping) (facts : LoopFacts) (prepa literalNat? pc let stops : Lean.Name → Bool := match mode with | .reach => fun _ => false - | .finish => (· == ``CIL.Op.ret) - | .advance => fun op => callsCallee op || op == ``CIL.Op.ret - | .resume _ => fun op => op == ``CIL.Op.setFieldAt || op == ``CIL.Op.initAggregate + | .finish => (· == ``CIL.Instruction.ret) + | .advance => fun op => callsCallee op || op == ``CIL.Instruction.ret + | .resume _ => fun op => + op == ``CIL.Instruction.stfld || op == ``CIL.Instruction.initobj -- A struct store no fact steps ends any walk but `.reach`, for the frame lemmas to take. let yields : Lean.Name → Bool := fun op => stops op || (mode != .reach && - (op == ``CIL.Op.setFieldAt || op == ``CIL.Op.initAggregate || op == ``CIL.Op.setLocal)) + (op == ``CIL.Instruction.stfld || op == ``CIL.Instruction.initobj || + op == ``CIL.Instruction.stloc || op == ``CIL.Instruction.starg)) let mut taken := 0 + let mut localIndex : LoopLocalIndex := {} repeat if (← getGoals).isEmpty then return none - let rewriting := if ← loopMentionsStackOperations then rewriteStack else rewrite - if ← attempt rewriting then continue + let rewriting := if ← withNormalNodes prepared loopMentionsStackOperations then rewriteStack + else rewrite + let rewrote ← withNormalNodes prepared do + let saved ← saveState + -- As for `attempt`: a fact that is no simp lemma fails the rewrite quietly instead of + -- logging an error and admitting. + try Term.withoutErrToSorry <| withoutRecover <| cilSimpProgress rewriting + catch _ => saved.restore; pure false + if rewrote then continue if let .resume target := mode then if taken > 0 || stepped then if (← currentPc) == some target then return none @@ -407,28 +536,39 @@ def loopSteps (tactic : String) (mode : LoopStepping) (facts : LoopFacts) (prepa if let some (position, source, target) ← reachedPc then loopTargetMismatch tactic position source target -- Fetching unifies the goal with a step lemma; only its closed lookup proofs are kept. - let saved ← saveState - let fetched? ← observing? do - let fetched ← loopFetch core - let stores := [``CIL.Op.setLocal, ``CIL.Op.setFieldAt, ``CIL.Op.initAggregate] - let unknownStore ← if stores.any (← whnfR fetched.op).isAppOf then - pure !(← loopStoreKnown fetched.op fetched.step facts.known) - else pure false - return (fetched, ← loopReadsReferences fetched.op fetched.step, unknownStore) - saved.restore - let some (fetched, references, unknownStore) := fetched? + -- The fetch runs on the record of the facts, so its lookups read the equation hypotheses the + -- record holds; the restore inside the step undoes the fetch and keeps the record. + let fetched? ← withNormalNodes prepared do + let saved ← saveState + let fetched? ← observing? do + let fetched ← loopFetch core + let stores := [``CIL.Instruction.stloc, ``CIL.Instruction.starg, + ``CIL.Instruction.stfld, ``CIL.Instruction.initobj] + let frameStore := !(← whnfR fetched.op).isAppOf ``CIL.Instruction.stfld || fetched.valueField.isSome + let classify : StateT LoopLocalIndex MetaM (Bool × Bool) := do + let unknownStore ← if frameStore && stores.any (← whnfR fetched.op).isAppOf then + pure !(← loopStoreKnown fetched.op fetched.step facts.known) + else pure false + return (← loopReadsReferences fetched.op fetched.step, unknownStore) + let ((references, unknownStore), indexed) ← classify.run localIndex + return (fetched, references, unknownStore, indexed) + saved.restore + pure fetched? + let some (fetched, references, unknownStore, indexed) := fetched? | loopStuck tactic facts.all prepared stops; return none + localIndex := indexed let op := ((← whnfR fetched.op).getAppFn.constName?).getD Name.anonymous - let finishing := mode == .finish && op == ``CIL.Op.ret + let finishing := mode == .finish && op == ``CIL.Instruction.ret -- A call or return ends an advance before any step; a resumed walk tries the store first, -- since a store fact the frame lemmas just produced may step it. let resuming := match mode with | .resume _ => true | _ => false - if stops op && !finishing && !resuming then return some op + let frameStore := op != ``CIL.Instruction.stfld || fetched.valueField.isSome + if !fetched.unlinkedCall && frameStore && stops op && !finishing && !resuming then return some op -- A store no fact states steps only through the frame lemmas. if mode != .reach && unknownStore then return some op -- The groups this instruction reads, normalised in the goal the first time one is read. let mut reading := prepared - let read := facts.readBy op references + let read := facts.readBy op references fetched.valueField.isSome for h : index in [:read.size] do if read[index].isEmpty then continue let cached ← entryAt tactic normalised index @@ -463,14 +603,24 @@ def loopSteps (tactic : String) (mode : LoopStepping) (facts : LoopFacts) (prepa | _ => `(tactic| ( apply CIL.Safety.loop_terminates_next $found $instruction · cil_checked_step_normalized [$[$reading:term],*])) - if ← attempt next then + if ← withNormalNodes reading (attempt next) then taken := taken + 1 continue - if yields op then return some op + if !fetched.unlinkedCall && frameStore && yields op then return some op loopStuck tactic facts.all reading yields return none return none +open Lean Elab Tactic Meta in +/-- `loopStepsRun` on the record of normal-form nodes of this walk, so that the whole-goal + rewrite and the step simplifications leave alone what earlier steps visited and left + unchanged. -/ +def loopSteps (tactic : String) (mode : LoopStepping) (facts : LoopFacts) (prepared : Array Term) + (stepped : Bool := false) + (finishReturn : Option (LoopFetched → Array Term → TacticM Unit) := none) : + TacticM (Option Lean.Name) := + withNormalRun (loopStepsRun tactic mode facts prepared stepped finishReturn) + /-- Compose actual fetched instructions until the requested invariant state. Fuel is inferred from the path, including NOPs and unconditional branches. The facts are the optional list and those `gatherLoopFacts` finds. Fails naming the pc, the instruction diff --git a/CIL/Safety/LoopContract.lean b/CIL/Safety/LoopContract.lean index 0e93cd820..64dd98ca7 100644 --- a/CIL/Safety/LoopContract.lean +++ b/CIL/Safety/LoopContract.lean @@ -34,7 +34,7 @@ theorem loop_terminates_induction {α : Type} (program : CIL.Program) (method : /-- A loop path crosses a fetched call through the callee's contract, never its body or fuel. -/ theorem loop_terminates_call {Input Output : Type} {c : MethodContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (input : Input) @@ -53,7 +53,7 @@ theorem loop_terminates_call {Input Output : Type} {c : MethodContract Input Out /-- `loop_terminates_call` that also hands the continuation the callee's `WellFormed`, static world and catalog agreement. -/ theorem loop_terminates_call_formed {Input Output : Type} {c : MethodContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (input : Input) diff --git a/CIL/Safety/LoopFacts.lean b/CIL/Safety/LoopFacts.lean index af1a52eab..81ee3914f 100644 --- a/CIL/Safety/LoopFacts.lean +++ b/CIL/Safety/LoopFacts.lean @@ -34,12 +34,13 @@ def entryEvidenceFields (proof type : Expr) : MetaM (Array (Name × Expr × Expr let selected ← mkAppM (name ++ field) #[proof] return (field, selected, (← instantiateMVars (← inferType selected)).consumeMData) -/-- Heads of the equations the loop tactics take from the context: checked steps, local and - memory loads and stores, span element, slice, validity, width and mode checks, array validity, - formed references and `Unsafe.Add` results. -/ +/-- Heads of the equations the loop tactics take from the context: checked steps, local, memory + and static field loads and stores, span element, slice, validity, width and mode checks, array + validity, formed references and `Unsafe.Add` results. -/ def loopFactHeads : List Lean.Name := [``CIL.Safety.step, ``CIL.Safety.instruction, ``CIL.Safety.memoryInstruction, - ``CIL.Safety.staticInstruction, ``CIL.Safety.loadLocal, ``CIL.Safety.storeLocal, + ``CIL.Safety.staticInstruction, ``CIL.Safety.staticLoad, ``CIL.Safety.staticAddress, + ``CIL.Safety.loadLocal, ``CIL.Safety.storeLocal, ``CIL.Safety.loadValue, ``CIL.Safety.loadSlot, ``CIL.Safety.storeValue, ``CIL.Safety.SpanView.element, ``CIL.Safety.SpanView.slice, ``CIL.Safety.SpanView.validate, ``CIL.Safety.SpanView.width, @@ -221,8 +222,7 @@ def assertLoopFact (base : Name) (fact : Term) (known : Array Expr) : saved.restore return none let name ← mkFreshUserName base - let (_, goal) ← (← (← getMainGoal).assert name type proof).intro1P - replaceMainGoal [goal] + replaceMainGoal [(← addFact (← getMainGoal) name type proof).2] return some (mkIdent name, type) catch _ => saved.restore @@ -243,7 +243,7 @@ def readsArguments (program : Expr) : MetaM Bool := do seen := seen.insert name let some value := ((← getEnv).find? name).bind (·.value?) | continue for used in value.getUsedConstants do - if used == ``CIL.Op.arg then return true + if used == ``CIL.Instruction.ldarg then return true if seen.contains used then continue let follow := used.getRoot == root.getRoot || (((← getEnv).find? used).any fun info => code info.type) @@ -307,24 +307,32 @@ def confinedToNone (type : Lean.Expr) : Bool := type.isAppOfArity `CIL.Safety.LoopFrame 6 && (argument? type 0).any (·.isAppOfArity ``List.nil 1) -open Lean Meta in -/-- The positions of the `some` entries of `list` up to reduction, reading cons cells until one is - not: the locals a `knownLocals` list remembers. -/ -partial def rememberedIndices (list : Lean.Expr) (index : Nat := 0) : MetaM (Array Nat) := do - let cell ← whnf list - let some #[_, head, tail] := argumentsOf? cell ``List.cons 3 | return #[] - let rest ← rememberedIndices tail (index + 1) - if (← whnf head).isAppOfArity ``Option.some 2 then return #[index] ++ rest - return rest - open Lean Meta in /-- The entries of `list` up to reduction with their positions, reading cons cells until one is not. -/ -partial def listEntries (list : Lean.Expr) (index : Nat := 0) : +def listEntries (list : Lean.Expr) (index : Nat := 0) : MetaM (Array (Nat × Lean.Expr)) := do - let cell ← whnf list - let some #[_, head, tail] := argumentsOf? cell ``List.cons 3 | return #[] - return #[(index, head)] ++ (← listEntries tail (index + 1)) + let mut remaining := list + let mut position := index + let mut entries := #[] + repeat + let cell ← whnf remaining + let some #[_, head, tail] := argumentsOf? cell ``List.cons 3 | return entries + entries := entries.push (position, head) + remaining := tail + position := position + 1 + +open Lean Meta in +/-- The positions of the `some` entries of `list` up to reduction, reading cons cells until one is + not: the locals a `knownLocals` list remembers. -/ +def rememberedIndices (list : Lean.Expr) (index : Nat := 0) : MetaM (Array Nat) := do + let entries ← listEntries list index + let mut positions := #[] + -- Head reductions follow the complete spine traversal, from last to first. + for (position, head) in entries.reverse do + if (← whnf head).isAppOfArity ``Option.some 2 then + positions := positions.push position + return positions.reverse open Lean Elab Tactic Meta in /-- Checked argument facts with their source views, shared by entry gathering and write @@ -383,7 +391,7 @@ structure LoopFacts where /-- Each byte home's `form` with `localAddress`, which only taking a local's address reads; derived facts are proof terms, asserted by `assertLoopFacts` when first read. -/ addresses : Array Lean.Syntax.Term := #[] - /-- The field reads of remembered structs, which only `fieldAt` reads, as proof terms. -/ + /-- The field reads of remembered structs, used by `ldfld`, as proof terms. -/ fields : Array Lean.Syntax.Term := #[] /-- The negative sides of case splits, which only instructions testing or computing on data read. -/ @@ -643,7 +651,7 @@ def gatherLoopFactGroups (facts : Array Term) (evidenceOnly : Bool := false) : let memory := #[frameMemory] let subject := variables #[slots] let numeral (index : Nat) : Term := ⟨Syntax.mkNumLit (toString index)⟩ - -- Each byte home forms, so `localAddr` of it steps. + -- Each byte home forms, so `ldloca` of it steps. for (index, slot) in ← listEntries slots do if (← whnf slot).isAppOfArity ``LocalSlot.bytes 2 then candidates := candidates.push (`homeForm, @@ -669,8 +677,8 @@ def gatherLoopFactGroups (facts : Array Term) (evidenceOnly : Bool := false) : let width ← whnf (mkApp (mkConst `CIL.FieldLayout.width) fieldLayout) let narrow := (← getNatValue? width) matches some 1 | some 2 let (base, read, load) := if narrow then - (`narrowRead, `step_fieldAt_encode_narrow, `LoopFrame.load_narrow_field) - else (`fieldRead, `step_fieldAt_encode, `LoopFrame.load_field) + (`narrowRead, `step_ldfld_encode_narrow, `LoopFrame.load_narrow_field) + else (`fieldRead, `step_ldfld_encode, `LoopFrame.load_field) candidates := candidates.push (base, applied read #[valid, numeral field, valuesTerm], subject, memory, #[]) candidates := candidates.push (base, applied load diff --git a/CIL/Safety/LoopFrameFields.lean b/CIL/Safety/LoopFrameFields.lean index 9a2c01a23..5518e8807 100644 --- a/CIL/Safety/LoopFrameFields.lean +++ b/CIL/Safety/LoopFrameFields.lean @@ -11,7 +11,7 @@ The `LoopFrame` steps of a struct local remembered by its field values, as `LoopFrame.load_narrow_field` for a 1- or 2-byte field). Each continues the frame facts with the struct's new field values, from the `LocalSnapshots` laws of `CIL.Safety.AggregateFields`, so a loop state names the fields rather than the struct's bytes. A by-value field read, `ldloc` then -`ldfld`, is `step_fieldAt_encode` (or `step_fieldAt_encode_narrow`) on the value `LoopFrame.load` +`ldfld`, is `step_ldfld_encode` (or `step_ldfld_encode_narrow`) on the value `LoopFrame.load` pushes. -/ @@ -33,9 +33,11 @@ theorem LoopFrame.zero_aggregate {values : List (Option CIL.Value)} {stored : List (Option CIL.Value)} (listed : values.set index (some (.aggregate (layout.encode fields))) = stored := by cil_listed_locals) : - ∃ after, (∀ body pc args stacked rest, - step body (.initAggregate width) pc args stacked (.reference (.address home) :: rest) - current = .ok (.next (pc + 1) rest stacked after)) ∧ + ∃ after, (∀ (body : Method) (type : Extraction.TypeExpr), + (body.valueShape type).size? = some width → + ∀ pc args stacked rest, + step body (.initobj type) pc args stacked (.reference (.address home) :: rest) current = + .ok (.next (pc + 1) rest stacked after)) ∧ LoopFrame footprint caller after kinds slots (knownLocals stored) ∧ MemoryBelow caller.nextIdentity current after := by subst listed @@ -64,8 +66,9 @@ theorem LoopFrame.store_field {values : List (Option CIL.Value)} (storedFields : values.set index (some (.aggregate (layout.encode (fields.set fieldIndex value)))) = stored := by cil_listed_locals) : - ∃ after, (∀ body pc args stacked rest, - step body (.setFieldAt offset width) pc args stacked + ∃ after, (∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args stacked rest, + step body (.stfld operand) pc args stacked (.scalar value :: .reference (.address home) :: rest) current = .ok (.next (pc + 1) rest stacked after)) ∧ LoopFrame footprint caller after kinds slots (knownLocals stored) ∧ @@ -99,13 +102,14 @@ theorem LoopFrame.load_field {values : List (Option CIL.Value)} (found : layout.fields[fieldIndex]? = some ⟨offset, width, aggregate⟩ := by rfl) {value : CIL.Value} (listed : fields[fieldIndex]? = some value := by rfl) (exact : FieldExact width value := by trivial) : - ∀ body pc args stacked rest signed, - step body (.fieldAt offset width signed) pc args stacked + ∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args stacked rest, + step body (.ldfld operand) pc args stacked (.reference (.address home) :: rest) current = .ok (.next (pc + 1) (.scalar value :: rest) stacked current) := by obtain ⟨-, loaded⟩ := frame.snapshots.aggregate (bytes := layout.encode fields) slot (by simp [knownLocals, present]) - exact fieldAt_encode_home frame.wellFormed valid disjoint loaded found listed exact + exact ldfld_encode_home frame.wellFormed valid disjoint loaded found listed exact /-- `ldfld` of 1- or 2-byte field `fieldIndex` through the address of a listed struct of a valid, disjoint layout whose field holds the int `bits` pushes `narrowLoad` of it, and @@ -120,14 +124,15 @@ theorem LoopFrame.load_narrow_field {values : List (Option CIL.Value)} (found : layout.fields[fieldIndex]? = some ⟨offset, width, aggregate⟩ := by rfl) {bits : BitVec 32} (listed : fields[fieldIndex]? = some (.i32 bits) := by rfl) (narrow : width = 1 ∨ width = 2 := by decide) : - ∀ body pc args stacked rest signed, - step body (.fieldAt offset width signed) pc args stacked + ∀ (body : Method) (operand : Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value offset width signed → ∀ pc args stacked rest, + step body (.ldfld operand) pc args stacked (.reference (.address home) :: rest) current = .ok (.next (pc + 1) (.scalar (.i32 (narrowLoad width signed bits)) :: rest) stacked current) := by obtain ⟨-, loaded⟩ := frame.snapshots.aggregate (bytes := layout.encode fields) slot (by simp [knownLocals, present]) - exact fieldAt_encode_home_narrow frame.wellFormed valid disjoint loaded found listed narrow + exact ldfld_encode_home_narrow frame.wellFormed valid disjoint loaded found listed narrow #print axioms LoopFrame.zero_aggregate #print axioms LoopFrame.store_field diff --git a/CIL/Safety/LoopFrameSteps.lean b/CIL/Safety/LoopFrameSteps.lean index c1018839f..1aedd81af 100644 --- a/CIL/Safety/LoopFrameSteps.lean +++ b/CIL/Safety/LoopFrameSteps.lean @@ -8,7 +8,7 @@ import CIL.TermAccess # Loop tactics that carry frame facts The tactics that walk a loop's `LoopTerminates` goals while keeping its `LoopFrame` current: -`cil_loop_advance` steps to a call or return, `cil_loop_finish` through a return, +`cil_loop_advance` steps to a linked call or return, `cil_loop_finish` through a return, `cil_loop_call` crosses a call through the callee's contract and continues with the frame after it, and `cil_loop_recurse` steps to a loop head and applies the head's hypothesis there. Each walk takes the struct zeroings, field stores and numeric local stores on its way through the frame's @@ -132,37 +132,137 @@ macro "cil_caller_grows" : tactic => `(tactic| first | (apply CIL.Safety.Confined.grows; apply CIL.Safety.LoopFrame.confined with_reducible assumption)) +/-- Whether `left` and `right` can be the same memory. Two different local variables never are, + and deciding that by unification unfolds the reads and writes that mention them, which costs + more with every memory a walk has passed, so they are told apart syntactically. -/ +def memoryMatches (left right : Expr) : MetaM Bool := do + if left == right then return true + if left.isFVar && right.isFVar then + let leftDeclaration ← left.fvarId!.getDecl + let rightDeclaration ← right.fvarId!.getDecl + if !leftDeclaration.isLet && !rightDeclaration.isLet then return false + withNewMCtxDepth (isDefEq left right) + +/-- The variables stepping walks introduced: the memory each store or call produced and the + home each snapshot store found. It lives in the environment, so a backtracked step forgets + the variables it introduced. -/ +initialize walkVariablesExtension : EnvExtension (PersistentHashSet FVarId) ← + registerEnvExtension (pure {}) (asyncMode := .local) + +/-- Record `introduced` as a variable a stepping walk introduced. -/ +def recordWalkVariable (introduced : FVarId) : CoreM Unit := + modifyEnv fun env => walkVariablesExtension.modifyState env (·.insert introduced) + +/-- Whether `term` is a variable a stepping walk introduced, such as the memory a store + produced. A fact at any other memory belongs to the caller, whatever its name. -/ +def producedByWalk (term : Expr) : CoreM Bool := do + let .fvar introduced := term | return false + return (walkVariablesExtension.getState (← getEnv)).contains introduced + +/-- Where the facts about `memory` can start in the context: at the variable itself when it has + no value, since every fact that mentions it follows it, and at the start otherwise, since a + fact may state a let variable's value or another term equal to `memory`. A store that reads + only the facts from there reads a number that does not grow with the walk. -/ +def memoryWindowStart (memory : Expr) : MetaM Nat := do + let .fvar memoryVariable := memory | return 0 + let declaration ← memoryVariable.getDecl + return if declaration.isLet then 0 else declaration.index + +/-- The declarations of `context` from position `start` on, without the implementation details. -/ +def declarationsFrom (context : LocalContext) (start : Nat) : Array LocalDecl := Id.run do + let mut found := #[] + for position in [start:context.numIndices] do + if let some declaration := context.decls[position]! then + unless declaration.isImplementationDetail do found := found.push declaration + return found + +/-- The declarations of `context` from position `start` that a user name reaches: the last + declaration of each name, in context order, found in one pass. -/ +def accessibleDeclarations (context : LocalContext) (start : Nat := 0) : Array LocalDecl := + Id.run do + let declarations := declarationsFrom context start + let mut latest : Std.HashMap Name FVarId := {} + for declaration in declarations do + latest := latest.insert declaration.userName declaration.fvarId + return declarations.filter fun declaration => + latest[declaration.userName]? == some declaration.fvarId + +/-- Clear from the main goal each hypothesis of `superseded` that neither the goal nor a remaining + hypothesis mentions, by its type or its value. Only a hypothesis after the earliest of them can + mention one, so the pass reads the context from there and not the whole walk's. -/ +def clearSuperseded (superseded : Array FVarId) : TacticM Unit := withMainContext do + let context ← getLCtx + let mut doomed : Std.HashSet FVarId := {} + let mut earliest := context.numIndices + for free in superseded do + if let some declaration := context.find? free then + doomed := doomed.insert free + earliest := min earliest declaration.index + if doomed.isEmpty then return + let goal ← getMainGoal + let target ← instantiateMVars (← goal.getType) + let mentions (expression : Expr) : Array FVarId := (CollectFVars.main expression {}).fvarIds + for free in mentions target do doomed := doomed.erase free + -- Latest first, so a hypothesis that stays keeps what it mentions, which may itself be doomed. + for step in [:context.numIndices - earliest] do + let some declaration := context.decls[context.numIndices - 1 - step]! | continue + if declaration.isImplementationDetail || doomed.contains declaration.fvarId then continue + -- Looking for a doomed variable allocates nothing; the variables are collected only on a hit. + let type ← instantiateMVars declaration.type + if type.hasAnyFVar doomed.contains then + for free in mentions type do doomed := doomed.erase free + if let some value := declaration.value? then + let value ← instantiateMVars value + if value.hasAnyFVar doomed.contains then + for free in mentions value do doomed := doomed.erase free + if doomed.isEmpty then return + let declaration ← goal.getDecl + let replacement ← mkFreshExprMVarAt (doomed.fold (·.erase ·) declaration.lctx) + declaration.localInstances target MetavarKind.syntheticOpaque declaration.userName + goal.assign replacement + replaceMainGoal [replacement.mvarId!] + /-- Re-state each named hypothesis about a caller buffer or class instance at `memory` through `carry`, a term from its predicate and name, under the same name; a fact `carry` gives nothing - for, or whose term does not elaborate, stays as it is. -/ -def loopCarryFacts (memory : Expr) (carry : LoopCarried → Ident → TacticM (Option Term)) : - TacticM Unit := do + for, or whose term does not elaborate, stays as it is. A restated fact at a memory the walk + introduced supersedes the earlier one, which is added to `superseded` for the caller to clear, + or cleared here when no list is given. -/ +def loopCarryFacts (memory : Expr) (carry : LoopCarried → Ident → TacticM (Option Term)) + (superseded : Option (IO.Ref (Array FVarId)) := none) : TacticM Unit := do let carried ← withMainContext do - let context ← getLCtx - let mut found : Array (LoopCarried × Name) := #[] - for declaration in context do - if declaration.isImplementationDetail || declaration.userName.hasMacroScopes then continue - unless (context.findFromUserName? declaration.userName).any - (·.fvarId == declaration.fvarId) do continue + let mut found : Array (LoopCarried × LocalDecl) := #[] + for declaration in accessibleDeclarations (← getLCtx) (← memoryWindowStart memory) do + if declaration.userName.hasMacroScopes then continue let type := (← instantiateMVars declaration.type).consumeMData let some head := type.getAppFn.constName? | continue let some carried := loopCarriedHeads.find? fun carried => carried.head == head && carried.arity == type.getAppNumArgs | continue let some carriedMemory := argument? type carried.memory | continue - unless ← withNewMCtxDepth (isDefEq carriedMemory memory) do continue - found := found.push (carried, declaration.userName) + unless ← memoryMatches carriedMemory memory do continue + found := found.push (carried, declaration) return found - for (head, name) in carried do - let some term ← carry head (mkIdent name) | continue + let produced ← producedByWalk memory + let pending ← match superseded with + | some pending => pure pending + | none => IO.mkRef #[] + for (head, earlier) in carried do + let some term ← carry head (mkIdent earlier.userName) | continue let saved ← saveState -- A failed side proof inside `term` is logged, not thrown; `cil_strict` makes it fail here. - try evalTactic (← `(tactic| cil_strict have $(mkIdent name):ident := $term)) + try + evalTactic (← `(tactic| cil_strict cil_have_by $(mkIdent earlier.userName):ident := $term)) + -- A fact at a memory the walk made is out of reach once restated, and clearing it keeps the + -- context from growing with each store. A fact at the caller's memory stays, since the + -- restatement does not give it back. + if produced then pending.modify (·.push earlier.fvarId) catch _ => saved.restore + if superseded.isNone then clearSuperseded (← pending.get) /-- Carry the caller-buffer facts at `memory` across `below`, a store that keeps everything below the caller's watermark. -/ -def loopCarryBelow (memory : Expr) (below : Ident) : TacticM Unit := - loopCarryFacts memory fun carried fact => do +def loopCarryBelow (memory : Expr) (below : Ident) + (superseded : Option (IO.Ref (Array FVarId)) := none) : TacticM Unit := + loopCarryFacts memory (superseded := superseded) fun carried fact => do if carried.grows then return some (← `(($fact).after_below $below (by cil_caller_allocation) (by cil_caller_grows))) @@ -200,7 +300,7 @@ def loopFrameStore (facts : Array Term) (layouts : Array Expr) (footprint : Expr let some parts ← loopStateParts? source | return false let fetched ← observing? (loopFetch facts) saved.restore - let some { op, found, instruction, .. } := fetched | return false + let some { op, valueField, found, instruction, .. } := fetched | return false let op ← whnfR op let #[_, _, stackList, memory] := parts | return false let stack ← listEntries stackList @@ -210,8 +310,8 @@ def loopFrameStore (facts : Array Term) (layouts : Array Expr) (footprint : Expr let below ← loopFreshName `storedBelow -- A local store is a `storeLocal` fact the next step reads; a struct store is a whole step. let mut takesStep := true - let store ← if op.isAppOfArity ``CIL.Op.setFieldAt 2 then do - let some offsetTerm := argument? op 0 | return false + let store ← if op.isAppOfArity ``CIL.Instruction.stfld 1 then do + let some (offsetTerm, _) := valueField | return false let some offset ← literalNat? offsetTerm | return false let some (_, top) := stack[0]? | return false let some (_, address) := stack[1]? | return false @@ -228,8 +328,17 @@ def loopFrameStore (facts : Array Term) (layouts : Array Expr) (footprint : Expr let value ← Term.exprToSyntax payload `(tactic| obtain ⟨$after, $stepped, $framed, $below⟩ := CIL.Safety.LoopFrame.store_field $frame $(quote index) $valid $(quote field) $value) - else if op.isAppOfArity ``CIL.Op.initAggregate 1 then do - let some widthTerm := lastArgument? op | return false + else if op.isAppOfArity ``CIL.Instruction.initobj 1 then do + let some operand := lastArgument? op | return false + let some (_, _, result) := (← instantiateMVars (← inferType found)).consumeMData.eq? + | return false + let some body := lastArgument? result | return false + let shape := mkApp2 (mkConst ``CIL.Method.valueShape) body operand + let resolved ← match ← ResolutionFacts.sharedShape? ``CIL.Method.valueShape shape with + | some (.done result) => pure result.expr + | _ => whnfD shape + let widthTerm := mkApp (mkConst ``CIL.ValueShape.size?) resolved + let .app (.app (.const ``Option.some _) _) widthTerm ← whnfD widthTerm | return false let some width ← literalNat? widthTerm | return false let some (_, top) := stack[0]? | return false let some home ← loopAddressOf? top | return false @@ -243,10 +352,8 @@ def loopFrameStore (facts : Array Term) (layouts : Array Expr) (footprint : Expr let layoutTerm ← Term.exprToSyntax layout `(tactic| obtain ⟨$after, $stepped, $framed, $below⟩ := CIL.Safety.LoopFrame.zero_aggregate $frame $(quote index) $layoutTerm [$zeros,*] (valid := $valid)) - else if op.isAppOfArity ``CIL.Op.setLocal 2 then do - -- A narrowing store changes the value, which `LoopFrame.store` does not model. - let #[slotIndex, narrowing] := op.getAppArgs | return false - unless (← whnf narrowing).isAppOf ``Option.none do return false + else if op.isAppOfArity ``CIL.Instruction.stloc 1 then do + let #[slotIndex] := op.getAppArgs | return false let some index ← literalNat? slotIndex | return false let some (_, top) := stack[0]? | return false let top ← whnf top @@ -270,6 +377,9 @@ def loopFrameStore (facts : Array Term) (layouts : Array Expr) (footprint : Expr catch _ => saved.restore return false + withMainContext do + if let some produced := (← getLCtx).findFromUserName? after.getId then + recordWalkVariable produced.fvarId -- The slots stay literal, as the next step's frame states them, so the next gathering finds it. evalTactic (← `(tactic| try dsimp only at $framed:ident)) loopCarryBelow memory below @@ -285,8 +395,20 @@ def loopFrameStore (facts : Array Term) (layouts : Array Expr) (footprint : Expr try let found ← Term.exprToSyntax found let instruction ← Term.exprToSyntax instruction + let storing ← match valueField with + | some (_, proof) => do + let shape ← Term.exprToSyntax proof + `($stepped _ _ _ $shape _ _ _ _) + | none => + if op.isAppOfArity ``CIL.Instruction.initobj 1 then + `($stepped _ _ (by + first + | cil_step_value_size + | rfl + | simp only [cil_code, CIL.ValueShape.size?]) _ _ _ _) + else `($stepped _ _ _ _ _) Term.withoutErrToSorry <| withoutRecover <| evalTactic (← `(tactic| - refine CIL.Safety.loop_terminates_next $found $instruction ($stepped _ _ _ _ _) ?_)) + refine CIL.Safety.loop_terminates_next $found $instruction $storing ?_)) catch _ => taken.restore return true @@ -451,14 +573,14 @@ partial def loopWalk (tactic : String) (mode : LoopStepping) (explicit : Array T if let .resume target := mode then if position == some target then return -- A call or return an advance stops at is left for the next tactic. - if mode == .advance && stopped.any fun op => callsCallee op || op == ``CIL.Op.ret then + if mode == .advance && stopped.any fun op => callsCallee op || op == ``CIL.Instruction.ret then return if ← loopFrameStore facts.core layouts footprint then return ← loopWalk tactic mode explicit layouts footprint true -- Where the walk stopped is the mode's stop, or a store no frame lemma takes. let stops : Lean.Name → Bool := match mode with - | .finish => (· == ``CIL.Op.ret) - | .advance => fun op => callsCallee op || op == ``CIL.Op.ret + | .finish => (· == ``CIL.Instruction.ret) + | .advance => fun op => callsCallee op || op == ``CIL.Instruction.ret | _ => fun _ => false loopStuck tactic facts.all prepared stops @@ -466,7 +588,7 @@ partial def loopWalk (tactic : String) (mode : LoopStepping) (explicit : Array T def noFootprint : MetaM Expr := do mkAppOptM ``List.nil #[← mkAppM ``Prod #[mkConst ``Reference, mkConst ``Nat]] -/-- Step a `LoopTerminates` goal through fetched instructions up to the next call or return, +/-- Step a `LoopTerminates` goal through fetched instructions up to the next linked call or return, with the optional facts and those `gatherLoopFacts` finds, taking each struct zeroing, field store and numeric local store on the way through the frame lemmas. Any other instruction it cannot step fails, naming the pc, the instruction, the residual step and the fact missing. -/ @@ -537,6 +659,9 @@ def loopCallContinue (contract : Expr) (memory : Expr) (facts : Array Term) have $confined:ident := CIL.Safety.CallResult.framed call have $returned:ident := CIL.Safety.CallResult.post call clear call))) + withMainContext do + if let some produced := (← getLCtx).findFromUserName? called.getId then + recordWalkVariable produced.fvarId let unfold : Array Ident := match contract.getAppFn.constName? with | some name => #[mkIdent (`_root_ ++ name)] | none => #[] @@ -586,7 +711,8 @@ syntax "cil_loop_call" (&" keeping")? term:max term:max (" at " ident)? (" [" te /-- The contract a `MethodContract.Holds` proof's type names, and the tag of its callee's precondition (`preconditionTag`). -/ def loopHeldContract (holds : Term) : TacticM (Expr × Name) := withMainContext do - let type ← whnfR (← inferType (← Term.elabTerm holds none)) + -- A projection such as `summary.holds` types its contract by an assigned metavariable. + let type ← whnfR (← instantiateMVars (← inferType (← Term.elabTerm holds none))) let contract ← if type.isAppOf ``MethodContract.Holds then argumentAt "cil_loop_call" type 2 else pure type return (contract, preconditionTag type) @@ -601,9 +727,12 @@ def loopCallFailure (holds input : Term) (facts : Array Term) (position : Nat) let prepared ← normaliseStepFacts (← `(Lean.Parser.Tactic.optConfig|)) (#[← `(CIL.truth)] ++ facts) let fetched ← observing? (loopFetch facts) - let some { op, step, .. } := fetched + let some { op, unlinkedCall, step, .. } := fetched | saved.restore throwError "cil_loop_call: cannot fetch the instruction at pc {position}" + if unlinkedCall then + saved.restore + throwError "cil_loop_call: pc {position} has no callee link; step it with `cil_loop_advance`" unless ((← whnfR op).getAppFn.constName?.map callsCallee).getD false do saved.restore throwError "cil_loop_call: pc {position} is {op}, not a call; step to the call first with \ diff --git a/CIL/Safety/LoopOutcome.lean b/CIL/Safety/LoopOutcome.lean index 9254d5a99..b68fefa1a 100644 --- a/CIL/Safety/LoopOutcome.lean +++ b/CIL/Safety/LoopOutcome.lean @@ -16,7 +16,8 @@ namespace CIL.Safety /-- The loop run from `state` returns satisfying `post` when `raises` is `none`, and throws exactly the named exception otherwise. -/ def LoopOutcome (program : CIL.Program) (method : Nat) (args : List Value) - (post : Memory → List Value → Prop) (raises : Option String) (state : LoopState) : Prop := + (post : Memory → List Value → Prop) (raises : Option ExceptionType) (state : LoopState) : + Prop := match raises with | some exception => RunRaises program method state.pc args state.frame state.stack state.memory exception @@ -28,7 +29,8 @@ theorem loop_outcome_none {program : CIL.Program} {method : Nat} {args : List Va LoopTerminates program method args post state := rfl theorem loop_outcome_some {program : CIL.Program} {method pc : Nat} {args stack : List Value} - {post : Memory → List Value → Prop} {frame : Frame} {memory : Memory} {exception : String} : + {post : Memory → List Value → Prop} {frame : Frame} {memory : Memory} + {exception : ExceptionType} : LoopOutcome program method args post (some exception) ⟨pc, frame, stack, memory⟩ = RunRaises program method pc args frame stack memory exception := rfl @@ -36,7 +38,7 @@ theorem loop_outcome_some {program : CIL.Program} {method pc : Nat} {args stack proves it directly. -/ theorem loop_outcome_induction {α : Type} (program : CIL.Program) (method : Nat) (args : List Value) (configuration : α → LoopState) (invariant : α → Prop) (measure : α → Nat) - (post : Memory → List Value → Prop) (raises : Option String) + (post : Memory → List Value → Prop) (raises : Option ExceptionType) (progress : ∀ state, invariant state → (∀ next, invariant next → measure next < measure state → LoopOutcome program method args post raises (configuration next)) → @@ -51,9 +53,9 @@ theorem loop_outcome_induction {α : Type} (program : CIL.Program) (method : Nat /-- A state has the outcome if its fetched instruction steps to a state that has it. -/ theorem loop_outcome_next {program : CIL.Program} {method pc next : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} {frame nextFrame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} - {raises : Option String} + {raises : Option ExceptionType} (found : program[method]? = some body) (instruction : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .ok (.next next values nextFrame updated)) @@ -65,7 +67,7 @@ theorem loop_outcome_next {program : CIL.Program} {method pc next : Nat} /-- A loop path to a state with the outcome gives the outcome at its source. -/ theorem loop_reaches_outcome {program : CIL.Program} {method : Nat} {args : List Value} - {source target : LoopState} {post : Memory → List Value → Prop} {raises : Option String} + {source target : LoopState} {post : Memory → List Value → Prop} {raises : Option ExceptionType} (path : LoopReaches program method args source target) (finished : LoopOutcome program method args post raises target) : LoopOutcome program method args post raises source := by @@ -79,9 +81,9 @@ theorem loop_reaches_outcome {program : CIL.Program} {method : Nat} {args : List /-- Continue a loop path past a callee whose outcome contract names no exception for this input. -/ theorem loop_outcome_call {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} - {post : Memory → List Value → Prop} {raises : Option String} + {post : Memory → List Value → Prop} {raises : Option ExceptionType} (holds : c.Holds program callee) (input : Input) (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : @@ -105,9 +107,9 @@ theorem loop_outcome_call {Input Output : Type} {c : OutcomeContract Input Outpu /-- `loop_outcome_call` that also hands the continuation the callee's `WellFormed`, static world and catalog agreement. -/ theorem loop_outcome_call_formed {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} - {post : Memory → List Value → Prop} {raises : Option String} + {post : Memory → List Value → Prop} {raises : Option ExceptionType} (holds : c.Holds program callee) (input : Input) (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : @@ -128,9 +130,9 @@ theorem loop_outcome_call_formed {Input Output : Type} {c : OutcomeContract Inpu /-- Exit a loop with the exception a callee's contract names; no invariant or measure is needed. -/ theorem loop_outcome_call_raises {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} - {post : Memory → List Value → Prop} {exception : String} + {post : Memory → List Value → Prop} {exception : ExceptionType} (holds : c.Holds program callee) (input : Input) (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : @@ -144,26 +146,29 @@ theorem loop_outcome_call_raises {Input Output : Type} {c : OutcomeContract Inpu /-- A fetched `throw` of the named exception exits the loop with that outcome. -/ theorem loop_outcome_throw {program : CIL.Program} {method pc : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack : List Value} {frame : Frame} {memory : Memory} - {post : Memory → List Value → Prop} {exception : String} + {body : CIL.Method} {op : CIL.Instruction} {args stack : List Value} {frame : Frame} + {memory : Memory} + {post : Memory → List Value → Prop} {exception : ExceptionType} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .error (.thrown exception)) : LoopOutcome program method args post (some exception) ⟨pc, frame, stack, memory⟩ := run_throw_raises methodFound instructionFound stepped /-- A run that settles at any fuel without throwing `exception` refutes that outcome; - exhaustion is not settled. -/ + neither exhaustion nor an `unsupported` fault is settled. -/ theorem loop_outcome_some_refuted {program : CIL.Program} {fuel method pc : Nat} {args stack : List Value} - {frame : Frame} {memory : Memory} {post : Memory → List Value → Prop} {exception : String} + {frame : Frame} {memory : Memory} {post : Memory → List Value → Prop} + {exception : ExceptionType} {outcome : Except LocatedFault (Memory × List Value)} (ran : run program fuel method pc args frame stack memory = outcome) (settled : Settled outcome) (other : ∀ fault, outcome = .error fault → fault.fault ≠ .thrown exception) : ¬ LoopOutcome program method args post (some exception) ⟨pc, frame, stack, memory⟩ := by rintro ⟨later, fault, raised, thrown⟩ - have up := run_mono program fuel later method pc args frame stack memory outcome settled ran + have up := + run_mono program fuel later method pc args frame stack memory outcome settled.stable ran have down := run_mono program later fuel method pc args frame stack memory (.error fault) - (by simp [Settled, thrown]) raised + (by simp [Stable, thrown]) raised rw [Nat.add_comm] at down exact other fault (up.symm.trans down) thrown @@ -172,7 +177,7 @@ theorem loop_outcome_some_refuted {program : CIL.Program} {fuel method pc : Nat} def EnsuresOutcome (program : CIL.Program) (method : Nat) (pre : List Value → Memory → Prop) (post : List Value → Memory → Memory → List Value → Prop) - (raises : List Value → Memory → Option String) : Prop := + (raises : List Value → Memory → Option ExceptionType) : Prop := ∀ args initial, pre args initial → match raises args initial with | some exception => Raises program method args initial exception @@ -189,7 +194,7 @@ theorem ensures_outcome_none {program : CIL.Program} {method : Nat} theorem ensures_outcome_of_loop (program : CIL.Program) (method : Nat) (pre : List Value → Memory → Prop) (post : List Value → Memory → Memory → List Value → Prop) - (raises : List Value → Memory → Option String) + (raises : List Value → Memory → Option ExceptionType) (verified : ∀ args initial, pre args initial → ∃ body frame entered, program[method]? = some body ∧ @@ -221,15 +226,31 @@ theorem ensures_outcome_of_loop (program : CIL.Program) (method : Nat) apply; the goal keeps its tag. -/ syntax "cil_loop_outcome_advance" "[" term,* "]" : tactic +open Lean Elab Tactic Meta in elab_rules : tactic - | `(tactic| cil_loop_outcome_advance [$facts:term,*]) => keepingTag <| - cilWithNormalisedFacts facts.getElems fun facts prepared => - `(tactic| repeat first - | (simp only [$[$facts:term],*]) - | (apply CIL.Safety.loop_outcome_next - · cil_lookup [$[$facts:term],*] - · cil_lookup [$[$facts:term],*] - · cil_checked_step_normalized [$[$prepared:term],*])) + | `(tactic| cil_loop_outcome_advance [$facts:term,*]) => keepingTag do + let facts := facts.getElems + let prepared ← cilNormalisedFacts facts + let rewrite ← `(tactic| cil_simp_only [$[$facts:term],*]) + let next ← `(tactic| ( + apply CIL.Safety.loop_outcome_next + · cil_lookup [$[$facts:term],*] + · cil_lookup [$[$facts:term],*] + · cil_checked_step_normalized [$[$prepared:term],*])) + -- Rewrite the whole goal while that makes progress, else take one step; the record keeps + -- each instruction's rewrite to the terms it produced. As under `repeat first`, a failure + -- throws instead of logging an error and admitting the goal. + withNormalRun <| withoutRecover do + repeat + let saved ← saveState + let rewrote ← withNormalNodes facts do + try cilSimpProgress rewrite catch _ => saved.restore; pure false + if rewrote then continue + let stepped ← withNormalNodes prepared do + let beforeStep ← saveState + try withMainContext (evalTactic next); pure true + catch _ => beforeStep.restore; pure false + unless stepped do break /-- Continue past a returning callee at a `bind_call` site; leaves the `precondition`, the `raises` equation, then the continuation. -/ diff --git a/CIL/Safety/LoopScaffold.lean b/CIL/Safety/LoopScaffold.lean index 1a9b43ba9..f077a47fe 100644 --- a/CIL/Safety/LoopScaffold.lean +++ b/CIL/Safety/LoopScaffold.lean @@ -74,7 +74,7 @@ private def entrySimplifier {m : Type → Type} [Monad m] [MonadQuotation m] (definitions facts : Array (TSyntax `term)) : m (TSyntax `tactic) := do let facts := definitions ++ facts `(tactic| (simp [enterFrame, makeSelectedLocals, makeLocals, - makeArgumentHomes, makeParameterHomes, makeLocal, storeLocal, Value.hasUnspecified, + makeParameterHomes, makeLocal, storeLocal, Value.hasUnspecified, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, $[$facts:term],*]; try rfl)) diff --git a/CIL/Safety/MemoryInstructionRenaming.lean b/CIL/Safety/MemoryInstructionRenaming.lean index 59364d042..dd50ceb12 100644 --- a/CIL/Safety/MemoryInstructionRenaming.lean +++ b/CIL/Safety/MemoryInstructionRenaming.lean @@ -13,12 +13,33 @@ an allocation renaming. Local-home insertion does not change scalar computation namespace CIL.Safety +-- Stack lists are renamed by the local map of the frame that holds them. +variable {indices : LocalIndexMap} + +/-- Closes a renamed push whose pushed value came from `formValue`, `formObject`, + `loadSlot` or `loadArraySlot`, none of which yields a home. -/ +macro "push_renamed" related:term : tactic => `(tactic| first + | exact .ok ⟨$related, rfl⟩ + | exact .ok ⟨$related, LocalIndexMap.push_of_not_home _ _ _ (by first + | exact formValue_not_spanHome (by assumption) + | exact formObject_not_spanHome (by assumption) + | exact loadSlot_not_spanHome (by assumption) + | exact loadArraySlot_not_spanHome (by assumption))⟩) + /-- An observable checked outcome is successful or raises a named CLR exception. -/ def SuccessOrThrow : ExecutionResult α → Prop | .ok _ => True | .error (.thrown _) => True | .error _ => False +/-- A successful or throwing sequence continues observably after a successful first step. -/ +theorem SuccessOrThrow.bind_right {first : ExecutionResult α} {next : α → ExecutionResult β} + {value : α} (observable : SuccessOrThrow (first >>= next)) (success : first = .ok value) : + SuccessOrThrow (next value) := by + subst success + exact observable +#print axioms SuccessOrThrow.bind_right + /-- A successful or throwing sequence cannot start with an unobservable failure. -/ theorem SuccessOrThrow.bind_left {first : ExecutionResult α} {next : α → ExecutionResult β} (observable : SuccessOrThrow (first >>= next)) : @@ -48,13 +69,13 @@ private theorem Memory.Renamed.spanCopyInstructionResult {mapping : AllocationRe (observable : SuccessOrThrow (spanCopyInstruction width tryCopy stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (spanCopyInstruction width tryCopy stack source) - (spanCopyInstruction width tryCopy (stack.map mapping.value) target) := by + (spanCopyInstruction width tryCopy (stack.map (indices.value mapping)) target) := by unfold CIL.Safety.spanCopyInstruction at observable ⊢ split at observable · rename_i destinationView sourceView rest - simp only [List.map_cons, AllocationRenaming.value] + simp only [List.map_cons, LocalIndexMap.value] dsimp only [AllocationRenaming.spanView] simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds obtain ⟨destinationBound, sourceBound, restBound⟩ := bounds @@ -116,12 +137,12 @@ private theorem Memory.Renamed.spanFillInstructionResult {mapping : AllocationRe (observable : SuccessOrThrow (spanFillInstruction width clear stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (spanFillInstruction width clear stack source) - (spanFillInstruction width clear (stack.map mapping.value) target) := by + (spanFillInstruction width clear (stack.map (indices.value mapping)) target) := by cases clear <;> simp only [CIL.Safety.spanFillInstruction] at observable ⊢ all_goals split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals try dsimp only [AllocationRenaming.spanView] all_goals try solve | repeat first | contradiction | cases observable | split at observable all_goals clear observable @@ -147,14 +168,14 @@ private theorem Memory.Renamed.arrayFillInstructionResult {mapping : AllocationR (observable : SuccessOrThrow (arrayFillInstruction element counted stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (arrayFillInstruction element counted stack source) - (arrayFillInstruction element counted (stack.map mapping.value) target) := by + (arrayFillInstruction element counted (stack.map (indices.value mapping)) target) := by unfold arrayFillInstruction at observable ⊢ split at observable · rename_i value input rest - simp only [List.map_cons, AllocationRenaming.value, Bind.bind, Except.bind, - Pure.pure, Except.pure] at observable ⊢ + simp only [List.map_cons, LocalIndexMap.value, Bind.bind, + Except.bind, Pure.pure, Except.pure] at observable ⊢ split at observable · rename_i view dsimp +instances only [AllocationRenaming.arrayView] @@ -173,18 +194,19 @@ private theorem Memory.Renamed.arrayFillInstructionResult {mapping : AllocationR split at observable · simp only [*, ite_true]; exact .error _ · simp only [*, Bool.false_eq_true, ite_false] - have renamed := related.spanFillInstructionResult element.width false - (.scalar value :: .bufferSpan view.storage :: rest) - (by simpa only [List.mem_cons, forall_eq_or_imp, Value.Below, ArrayView.storage] - using bounds) observable - simpa only [List.map_cons, AllocationRenaming.value, AllocationRenaming.spanView, - ArrayView.storage] using renamed - · simp only [AllocationRenaming.value, AllocationRenaming.managedReference] + have renamed := + related.spanFillInstructionResult (indices := indices) element.width false + (.scalar value :: .bufferSpan view.storage :: rest) + (by simpa only [List.mem_cons, forall_eq_or_imp, Value.Below, ArrayView.storage] + using bounds) observable + simpa only [List.map_cons, AllocationRenaming.value, LocalIndexMap.value, + AllocationRenaming.spanView, ArrayView.storage] using renamed + · simp only [AllocationRenaming.managedReference] exact .error _ · cases observable · rename_i count start value input rest - simp only [List.map_cons, AllocationRenaming.value, Bind.bind, Except.bind, - Pure.pure, Except.pure] at observable ⊢ + simp only [List.map_cons, LocalIndexMap.value, Bind.bind, + Except.bind, Pure.pure, Except.pure] at observable ⊢ split at observable · rename_i view dsimp +instances only [AllocationRenaming.arrayView] @@ -213,16 +235,17 @@ private theorem Memory.Renamed.arrayFillInstructionResult {mapping : AllocationR have slicedBound : first.reference.Below mapping.sourceLimit := by rw [related.sourceLimit] exact formed.span_below first (checkedSpanSlice_valid _ _ _ _ _ sourceStep) - have renamed := related.spanFillInstructionResult element.width false + have renamed := + related.spanFillInstructionResult (indices := indices) element.width false (.scalar value :: .bufferSpan first :: rest) (by simpa only [List.mem_cons, forall_eq_or_imp, Value.Below] using And.intro trivial (And.intro slicedBound bounds.2.2.2.2)) (by simpa only [sourceStep, Bind.bind, Except.bind] using observable) - simpa only [List.map_cons, AllocationRenaming.value] using renamed + simpa only [List.map_cons, AllocationRenaming.value, LocalIndexMap.value] using renamed · rename_i invalidRange simp only [invalidRange, ite_false, Bind.bind, Except.bind] exact .error _ - · simp only [AllocationRenaming.value, AllocationRenaming.managedReference] + · simp only [AllocationRenaming.managedReference] exact .error _ · cases observable · simp only [Bind.bind, Except.bind] at observable @@ -230,7 +253,7 @@ private theorem Memory.Renamed.arrayFillInstructionResult {mapping : AllocationR #print axioms Memory.Renamed.arrayFillInstructionResult /-- Typed array loads and stores transport the exact snapshot mask and checked authority. -/ -private theorem Memory.Renamed.arrayTransferInstructionResult {mapping : AllocationRenaming} +theorem Memory.Renamed.arrayTransferInstructionResult {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} (related : Memory.Renamed mapping scopes source target) (formed : source.WellFormed) (element : CIL.ArrayElement) (store : Bool) (stack : List Value) @@ -238,9 +261,9 @@ private theorem Memory.Renamed.arrayTransferInstructionResult {mapping : Allocat (observable : SuccessOrThrow (arrayTransferInstruction element store stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (arrayTransferInstruction element store stack source) - (arrayTransferInstruction element store (stack.map mapping.value) target) := by + (arrayTransferInstruction element store (stack.map (indices.value mapping)) target) := by cases store <;> simp only [arrayTransferInstruction, Bool.false_eq_true, ite_false, ite_true] at observable ⊢ all_goals simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at observable ⊢ @@ -264,24 +287,24 @@ private theorem Memory.Renamed.arrayTransferInstructionResult {mapping : Allocat | (apply (related.storeTransfer _ (by assumption) _ _).bindWithSuccess intro first second sourceStep memoryRelated) | (split at observable <;> try simp only [*, List.map_cons, AllocationRenaming.value, - AllocationRenaming.arrayView, AllocationRenaming.managedReference, + LocalIndexMap.value, AllocationRenaming.arrayView, AllocationRenaming.managedReference, Bool.false_eq_true, ite_false, ite_true, Bind.bind, Except.bind, Pure.pure, Except.pure] at observable ⊢) #print axioms Memory.Renamed.arrayTransferInstructionResult /-- Checked reference addition transports either operand order to the same allocation. -/ -private theorem Memory.Renamed.referenceAddInstructionResult {mapping : AllocationRenaming} +theorem Memory.Renamed.referenceAddInstructionResult {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} (related : Memory.Renamed mapping scopes source target) (size : Nat) (signed : Bool) (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (observable : SuccessOrThrow (referenceAddInstruction size signed stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (referenceAddInstruction size signed stack source) - (referenceAddInstruction size signed (stack.map mapping.value) target) := by + (referenceAddInstruction size signed (stack.map (indices.value mapping)) target) := by unfold CIL.Safety.referenceAddInstruction at observable ⊢ split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals try solve | cases observable all_goals clear observable all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds @@ -304,12 +327,12 @@ private theorem Memory.Renamed.wideArithmeticInstructionResult {mapping : Alloca (observable : SuccessOrThrow (wideArithmeticInstruction operation stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (wideArithmeticInstruction operation stack source) - (wideArithmeticInstruction operation (stack.map mapping.value) target) := by + (wideArithmeticInstruction operation (stack.map (indices.value mapping)) target) := by unfold wideArithmeticInstruction at observable ⊢ split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals try solve | repeat first | contradiction | cases observable | split at observable all_goals clear observable all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds @@ -680,21 +703,21 @@ theorem Memory.Renamed.storeArraySlot {mapping : AllocationRenaming} exact .ok (related.setSlot field bound left) #print axioms Memory.Renamed.storeArraySlot -/-- Object field loads and stores touch the mapped field and relate their memories. -/ -private theorem Memory.Renamed.objectFieldInstructionResult {mapping : AllocationRenaming} +/-- Class field loads read the mapped field and leave the related memories as they were. -/ +theorem Memory.Renamed.classFieldLoadResult {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} - (related : Memory.Renamed mapping scopes source target) (operation : CIL.MemoryOp) - (formed : source.WellFormed) + (related : Memory.Renamed mapping scopes source target) (owner : CIL.Extraction.TypeExpr) + (offset : Nat) (slot : CIL.ClassSlot) (formed : source.WellFormed) (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) - (observable : SuccessOrThrow (objectFieldInstruction operation stack source)) : + (observable : SuccessOrThrow (classFieldLoad owner offset slot stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) - (objectFieldInstruction operation stack source) - (objectFieldInstruction operation (stack.map mapping.value) target) := by - unfold objectFieldInstruction at observable ⊢ + right.2 = left.2.map (indices.value mapping)) + (classFieldLoad owner offset slot stack source) + (classFieldLoad owner offset slot (stack.map (indices.value mapping)) target) := by + unfold classFieldLoad at observable ⊢ split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals try solve | repeat first | contradiction | cases observable | split at observable all_goals clear observable all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds @@ -713,10 +736,10 @@ private theorem Memory.Renamed.objectFieldInstructionResult {mapping : Allocatio | (apply (related.checkedWrite _ (by assumption) _ _ _ _).bindWithSuccess intro first second sourceStep memoryRelated exact .ok ⟨memoryRelated, rfl⟩) - | (apply (related.loadSlot formed _ (by assumption)).bind - intro first second same + | (apply (related.loadSlot formed _ (by assumption)).bindWithSuccess + intro first second sourceStep same subst second - exact .ok ⟨related, rfl⟩) + push_renamed related) | (apply (related.referenceArrayOf _ (by assumption)).bind intro first second same subst second @@ -731,15 +754,77 @@ private theorem Memory.Renamed.objectFieldInstructionResult {mapping : Allocatio | (apply (related.storeSlot _ (by assumption) _ (by assumption)).bind intro first second memoryRelated exact .ok ⟨memoryRelated, rfl⟩) - | (apply (related.loadArraySlot formed _ (by assumption) _).bind + | (apply (related.loadArraySlot formed _ (by assumption) _).bindWithSuccess + intro first second sourceStep same + subst second + push_renamed related) + | (apply (related.storeArraySlot _ (by assumption) _ (by assumption) _).bind + intro first second memoryRelated + exact .ok ⟨memoryRelated, rfl⟩) + | (split <;> try simp only [*, AllocationRenaming.managedReference, Bind.bind, Except.bind]) +#print axioms Memory.Renamed.classFieldLoadResult + +/-- Class field stores write the mapped field and relate the memories after the write. -/ +theorem Memory.Renamed.classFieldStoreResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (owner : CIL.Extraction.TypeExpr) + (offset : Nat) (slot : CIL.ClassSlot) (formed : source.WellFormed) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (classFieldStore owner offset slot stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (classFieldStore owner offset slot stack source) + (classFieldStore owner offset slot (stack.map (indices.value mapping)) target) := by + unfold classFieldStore at observable ⊢ + split at observable + all_goals try simp only [List.map_cons, LocalIndexMap.value] + all_goals try solve | repeat first | contradiction | cases observable | split at observable + all_goals clear observable + all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds + all_goals repeat' rcases bounds with ⟨_, bounds⟩ + all_goals repeat first + | exact .ok ⟨related, rfl⟩ + | exact .error _ + | (apply (related.objectFieldAddress _ (by assumption) _ _).bindWithSuccess + intro first second sourceStep same + have addressBound := objectFieldAddress_below (by assumption) sourceStep + subst second) + | (apply (related.loadValue (.address _) (by assumption) _ _).bindWithSuccess + intro first second sourceStep same + subst second + exact .ok ⟨related, rfl⟩) + | (apply (related.checkedWrite _ (by assumption) _ _ _ _).bindWithSuccess + intro first second sourceStep memoryRelated + exact .ok ⟨memoryRelated, rfl⟩) + | (apply (related.loadSlot formed _ (by assumption)).bindWithSuccess + intro first second sourceStep same + subst second + push_renamed related) + | (apply (related.referenceArrayOf _ (by assumption)).bind intro first second same subst second exact .ok ⟨related, rfl⟩) + | (apply (related.referenceArraySlot _ (by assumption) _).bindWithSuccess + intro first second sourceStep same + have slotBound := referenceArraySlot_below (by assumption) sourceStep + subst second) + | (apply (related.storableIn _ _ (by assumption)).bind + intro first second same + subst second) + | (apply (related.storeSlot _ (by assumption) _ (by assumption)).bind + intro first second memoryRelated + exact .ok ⟨memoryRelated, rfl⟩) + | (apply (related.loadArraySlot formed _ (by assumption) _).bindWithSuccess + intro first second sourceStep same + subst second + push_renamed related) | (apply (related.storeArraySlot _ (by assumption) _ (by assumption) _).bind intro first second memoryRelated exact .ok ⟨memoryRelated, rfl⟩) | (split <;> try simp only [*, AllocationRenaming.managedReference, Bind.bind, Except.bind]) -#print axioms Memory.Renamed.objectFieldInstructionResult +#print axioms Memory.Renamed.classFieldStoreResult + /-- Looking up a renamed literal table gives the renamed binding. -/ theorem lookup_renamedLiterals (bindings : List (String × Reference)) @@ -771,16 +856,16 @@ private theorem formObject_observable {memory : Memory} {reference : Reference} #print axioms formObject_observable /-- A string literal loads the renamed binding of its text, formed alike. -/ -private theorem Memory.Renamed.stringLiteralInstructionResult {mapping : AllocationRenaming} +theorem Memory.Renamed.stringLiteralInstructionResult {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} (related : Memory.Renamed mapping scopes source target) (formed : source.WellFormed) (text : String) (stack : List Value) (observable : SuccessOrThrow (stringLiteralInstruction text stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (stringLiteralInstruction text stack source) - (stringLiteralInstruction text (stack.map mapping.value) target) := by + (stringLiteralInstruction text (stack.map (indices.value mapping)) target) := by unfold stringLiteralInstruction literalObject at observable ⊢ rw [related.literals, lookup_renamedLiterals] cases bound : source.literals.lookup text with @@ -791,10 +876,10 @@ private theorem Memory.Renamed.stringLiteralInstructionResult {mapping : Allocat have issued : reference.allocation < mapping.sourceLimit := by rw [related.sourceLimit] exact formObject_issued sourceFormed formed - apply (related.formObject (.address reference) issued).bind - intro first second same + apply (related.formObject (.address reference) issued).bindWithSuccess + intro first second sourceStep same subst second - exact .ok ⟨related, rfl⟩ + push_renamed related #print axioms Memory.Renamed.stringLiteralInstructionResult /-- Renaming keeps null and non-null objects apart, so a null guard decides alike. -/ @@ -804,12 +889,13 @@ private theorem Memory.Renamed.throwIfNullInstructionResult {mapping : Allocatio (observable : SuccessOrThrow (throwIfNullInstruction stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (throwIfNullInstruction stack source) - (throwIfNullInstruction (stack.map mapping.value) target) := by + (throwIfNullInstruction (stack.map (indices.value mapping)) target) := by unfold CIL.Safety.throwIfNullInstruction at observable ⊢ split at observable - all_goals try simp only [List.map, AllocationRenaming.value, AllocationRenaming.managedReference] + all_goals try simp only [List.map, LocalIndexMap.value, + AllocationRenaming.managedReference] · exact .error _ · exact .ok ⟨related, rfl⟩ · cases observable @@ -823,12 +909,12 @@ private theorem Memory.Renamed.zeroGuardInstructionResult {mapping : AllocationR (observable : SuccessOrThrow (zeroGuardInstruction guard width signed stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (zeroGuardInstruction guard width signed stack source) - (zeroGuardInstruction guard width signed (stack.map mapping.value) target) := by + (zeroGuardInstruction guard width signed (stack.map (indices.value mapping)) target) := by unfold CIL.Safety.zeroGuardInstruction at observable ⊢ split at observable - all_goals try simp only [List.map, AllocationRenaming.value] + all_goals try simp only [List.map, LocalIndexMap.value] all_goals repeat first | exact .error _ | exact .ok ⟨related, rfl⟩ @@ -844,18 +930,66 @@ private theorem Memory.Renamed.comparisonGuardInstructionResult {mapping : Alloc (observable : SuccessOrThrow (comparisonGuardInstruction guard width signed stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (comparisonGuardInstruction guard width signed stack source) - (comparisonGuardInstruction guard width signed (stack.map mapping.value) target) := by + (comparisonGuardInstruction guard width signed (stack.map (indices.value mapping)) + target) := by unfold CIL.Safety.comparisonGuardInstruction at observable ⊢ split at observable - all_goals try simp only [List.map, AllocationRenaming.value] + all_goals try simp only [List.map, LocalIndexMap.value] all_goals repeat first | exact .error _ | exact .ok ⟨related, rfl⟩ | cases observable | split #print axioms Memory.Renamed.comparisonGuardInstructionResult +/-- Copying or initializing bytes commutes with allocation renaming, including the overlap + check on two bounded references. -/ +theorem Memory.Renamed.blockBytesResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (copy : Bool) (alignment : Nat) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow + ((if copy then copyBlockBytes else initBlockBytes) alignment stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + ((if copy then copyBlockBytes else initBlockBytes) alignment stack source) + ((if copy then copyBlockBytes else initBlockBytes) alignment + (stack.map (indices.value mapping)) target) := by + cases copy <;> simp only [Bool.false_eq_true, ↓reduceIte, copyBlockBytes, initBlockBytes] + at observable ⊢ + all_goals split at observable + all_goals try solve | cases observable + all_goals simp only [List.map_cons, LocalIndexMap.value] + all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds + all_goals repeat' rcases bounds with ⟨_, bounds⟩ + all_goals have imageEquality : ∀ left right, left < mapping.sourceLimit → + right < mapping.sourceLimit → (mapping.image left = mapping.image right ↔ left = right) := + fun left right leftBound rightBound => + ⟨mapping.injective left right leftBound rightBound, congrArg mapping.image⟩ + all_goals repeat first + | exact .ok ⟨related, rfl⟩ + | exact .error _ + | (apply (mapping.referenceAt _).bindWithSuccess + intro first second sourceStep same + have addressBound := referenceAt_below (by assumption) sourceStep + subst second) + | (apply (related.checkedRead _ (by assumption) _ _ _ _).bindWithSuccess + intro first second sourceStep same + subst second) + | (apply (related.checkedAccess _ (by assumption) _ _ _ _ _).bindWithSuccess + intro first second sourceStep same + subst second) + | (apply (related.checkedWrite _ (by assumption) _ _ _ _).bindWithSuccess + intro first second sourceStep memoryRelated + exact .ok ⟨memoryRelated, rfl⟩) + | dsimp +instances only [AllocationRenaming.reference] + | simp only [mapping.isNull, imageEquality] + | (split <;> try simp only [*, mapping.isNull, AllocationRenaming.managedReference, + Bind.bind, Except.bind]) +#print axioms Memory.Renamed.blockBytesResult + /-- Ordinary memory effects transport independently of the whole-span dispatch families. -/ private theorem Memory.Renamed.otherMemoryInstructionResult {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} @@ -865,12 +999,16 @@ private theorem Memory.Renamed.otherMemoryInstructionResult {mapping : Allocatio (observable : SuccessOrThrow (otherMemoryInstruction operation stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (otherMemoryInstruction operation stack source) - (otherMemoryInstruction operation (stack.map mapping.value) target) := by + (otherMemoryInstruction operation (stack.map (indices.value mapping)) target) := by unfold otherMemoryInstruction at observable ⊢ split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals first + | exact related.blockBytesResult true _ _ bounds observable + | exact related.blockBytesResult false _ _ bounds observable + | skip + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals try dsimp +instances only [AllocationRenaming.arrayView, AllocationRenaming.spanView] all_goals try solve | repeat first | contradiction | cases observable | split at observable all_goals clear observable @@ -906,7 +1044,7 @@ private theorem Memory.Renamed.otherMemoryInstructionResult {mapping : Allocatio | (apply (related.formValue _ (by assumption)).bindWithSuccess intro first second sourceStep same subst second - first | (split <;> exact .ok ⟨related, rfl⟩) | exact .ok ⟨related, rfl⟩) + first | (split <;> push_renamed related) | push_renamed related) | (apply (related.storeValue _ (by assumption) _).bindWithSuccess intro first second sourceStep memoryRelated exact .ok ⟨memoryRelated, rfl⟩) @@ -968,19 +1106,19 @@ private theorem Memory.Renamed.viewObservationInstructionResult {mapping : Alloc (observable : SuccessOrThrow (viewObservationInstruction observation stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (viewObservationInstruction observation stack source) - (viewObservationInstruction observation (stack.map mapping.value) target) := by + (viewObservationInstruction observation (stack.map (indices.value mapping)) target) := by unfold viewObservationInstruction at observable ⊢ split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals try dsimp only [AllocationRenaming.arrayView, AllocationRenaming.spanView] all_goals try solve | repeat first | contradiction | cases observable | split at observable all_goals clear observable all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds all_goals repeat' rcases bounds with ⟨_, bounds⟩ all_goals repeat first - | exact .ok ⟨related, rfl⟩ + | push_renamed related | exact .error _ | (apply (related.checkedSpanValidate _ (by assumption)).bindWithSuccess intro first second sourceStep same @@ -996,12 +1134,12 @@ private theorem Memory.Renamed.viewObservationInstructionResult {mapping : Alloc AllocationRenaming.managedReference, Bind.bind, Except.bind]) all_goals try simp_all [CIL.Safety.formValue, Pure.pure, Except.pure, Bind.bind, Except.bind] all_goals subst_vars - all_goals try solve | split <;> exact .ok ⟨related, rfl⟩ + all_goals try solve | split <;> push_renamed related all_goals apply (related.formValue _ (by assumption)).bindWithSuccess intro first second sourceStep same subst second - split <;> exact .ok ⟨related, rfl⟩ + split <;> push_renamed related #print axioms Memory.Renamed.viewObservationInstructionResult /-- Byte reordering cannot introduce a fault containing allocation identities. -/ @@ -1023,12 +1161,13 @@ private theorem Memory.Renamed.binaryTransferInstructionResult {mapping : Alloca (binaryTransferInstruction width signed bigEndian read trying stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (binaryTransferInstruction width signed bigEndian read trying stack source) - (binaryTransferInstruction width signed bigEndian read trying (stack.map mapping.value) target) := by + (binaryTransferInstruction width signed bigEndian read trying + (stack.map (indices.value mapping)) target) := by unfold binaryTransferInstruction at observable ⊢ split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals try dsimp only [AllocationRenaming.spanView] all_goals try solve | repeat first | contradiction | cases observable | split at observable all_goals clear observable @@ -1060,12 +1199,12 @@ private theorem Memory.Renamed.singleSpanInstructionResult {mapping : Allocation (observable : SuccessOrThrow (singleSpanInstruction width writable stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (singleSpanInstruction width writable stack source) - (singleSpanInstruction width writable (stack.map mapping.value) target) := by + (singleSpanInstruction width writable (stack.map (indices.value mapping)) target) := by unfold singleSpanInstruction at observable ⊢ split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds all_goals repeat' rcases bounds with ⟨_, bounds⟩ all_goals repeat first @@ -1087,12 +1226,13 @@ private theorem Memory.Renamed.pointerTransferInstructionResult {mapping : Alloc (observable : SuccessOrThrow (pointerTransferInstruction kind alignment store stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (pointerTransferInstruction kind alignment store stack source) - (pointerTransferInstruction kind alignment store (stack.map mapping.value) target) := by + (pointerTransferInstruction kind alignment store (stack.map (indices.value mapping)) + target) := by unfold pointerTransferInstruction at observable ⊢ split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals try solve | repeat first | contradiction | cases observable | split at observable all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds all_goals repeat' rcases bounds with ⟨_, bounds⟩ @@ -1143,12 +1283,12 @@ private theorem Memory.Renamed.vector128UnsafeInstructionResult {mapping : Alloc (observable : SuccessOrThrow (vector128UnsafeInstruction store offset stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (vector128UnsafeInstruction store offset stack source) - (vector128UnsafeInstruction store offset (stack.map mapping.value) target) := by + (vector128UnsafeInstruction store offset (stack.map (indices.value mapping)) target) := by unfold vector128UnsafeInstruction at observable ⊢ split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value] + all_goals try simp only [List.map_cons, LocalIndexMap.value] all_goals try solve | repeat first | contradiction | cases observable | split at observable all_goals clear observable all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below] at bounds @@ -1169,6 +1309,43 @@ private theorem Memory.Renamed.vector128UnsafeInstructionResult {mapping : Alloc exact .ok ⟨memoryRelated, rfl⟩) #print axioms Memory.Renamed.vector128UnsafeInstructionResult +/-- A static span keeps the allocation identity of its base under renaming. -/ +private theorem Memory.Renamed.staticSpanInstructionResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) (operation : CIL.MemoryOp) + (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) + (observable : SuccessOrThrow (staticSpanInstruction operation stack source)) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = left.2.map (indices.value mapping)) + (staticSpanInstruction operation stack source) + (staticSpanInstruction operation (stack.map (indices.value mapping)) target) := by + unfold staticSpanInstruction at observable ⊢ + split at observable + all_goals try simp only [List.map_cons, LocalIndexMap.value, + AllocationRenaming.managedReference] + all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below, + ManagedReference.Below] at bounds + all_goals repeat' rcases bounds with ⟨_, bounds⟩ + all_goals repeat first + | exact .ok ⟨related, rfl⟩ + | exact .error _ + | (apply ((related.liveAllocation _ (by assumption)).checkedAt _ _).bindWithSuccess + intro first second sourceStep descriptors + rw [← descriptors.layout, ← descriptors.kind.immutable] + try (unfold staticSpanAligned; rw [← descriptors.layout])) + | (apply (related.formValue _ (by assumption)).bindWithSuccess + intro first second sourceStep same + subst second) + | dsimp +instances only [AllocationRenaming.reference] + | (split <;> try simp only [*, Bind.bind, Except.bind]) + all_goals repeat first + | contradiction + | cases observable + | split at observable + | simp only [SuccessOrThrow] at observable +#print axioms Memory.Renamed.staticSpanInstructionResult + /-- A memory instruction's successful effect is preserved on the corresponding allocations. -/ theorem Memory.Renamed.memoryInstructionResult {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} @@ -1178,43 +1355,32 @@ theorem Memory.Renamed.memoryInstructionResult {mapping : AllocationRenaming} (observable : SuccessOrThrow (memoryInstruction operation stack source)) : ResultRenamed mapping (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ - right.2 = left.2.map mapping.value) + right.2 = left.2.map (indices.value mapping)) (memoryInstruction operation stack source) - (memoryInstruction operation (stack.map mapping.value) target) := by + (memoryInstruction operation (stack.map (indices.value mapping)) target) := by unfold CIL.Safety.memoryInstruction at observable ⊢ - rw [mapping.unspecifiedOperands] + rw [indices.unspecifiedOperands mapping] split at observable · cases observable · rename_i observed simp only [observed, Bool.false_eq_true, ite_false] - split at observable - · exact related.referenceAddInstructionResult _ _ _ bounds observable - · exact related.pointerTransferInstructionResult _ _ _ _ bounds observable - · exact related.vector128UnsafeInstructionResult _ _ _ bounds observable - · exact related.arrayTransferInstructionResult formed _ _ _ bounds observable - · exact related.binaryTransferInstructionResult _ _ _ _ _ _ bounds observable - · exact related.binaryTransferInstructionResult _ _ _ _ _ _ bounds observable - · exact related.singleSpanInstructionResult _ _ _ bounds observable - · exact related.viewObservationInstructionResult _ _ bounds observable - · exact related.spanCopyInstructionResult _ _ _ bounds observable - · exact related.spanFillInstructionResult _ _ _ bounds observable - · exact related.arrayFillInstructionResult formed _ _ _ bounds observable - · exact related.wideArithmeticInstructionResult _ _ bounds observable - · exact related.wideArithmeticInstructionResult _ _ bounds observable - · exact related.objectFieldInstructionResult _ formed _ bounds observable - · exact related.objectFieldInstructionResult _ formed _ bounds observable - · exact related.objectFieldInstructionResult _ formed _ bounds observable - · exact related.objectFieldInstructionResult _ formed _ bounds observable - · exact related.objectFieldInstructionResult _ formed _ bounds observable - · exact related.objectFieldInstructionResult _ formed _ bounds observable - · exact related.objectFieldInstructionResult _ formed _ bounds observable - · exact related.objectFieldInstructionResult _ formed _ bounds observable - · exact related.objectFieldInstructionResult _ formed _ bounds observable - · exact related.stringLiteralInstructionResult formed _ _ observable - · exact related.throwIfNullInstructionResult _ observable - · exact related.zeroGuardInstructionResult _ _ _ _ observable - · exact related.comparisonGuardInstructionResult _ _ _ _ observable - · exact related.otherMemoryInstructionResult _ formed _ bounds observable + -- Each arm is routed by the helper it calls, so arm order does not matter. + split at observable <;> first + | with_reducible exact related.referenceAddInstructionResult _ _ _ bounds observable + | with_reducible exact related.pointerTransferInstructionResult _ _ _ _ bounds observable + | with_reducible exact related.vector128UnsafeInstructionResult _ _ _ bounds observable + | with_reducible exact related.binaryTransferInstructionResult _ _ _ _ _ _ bounds observable + | with_reducible exact related.singleSpanInstructionResult _ _ _ bounds observable + | with_reducible exact related.viewObservationInstructionResult _ _ bounds observable + | with_reducible exact related.spanCopyInstructionResult _ _ _ bounds observable + | with_reducible exact related.spanFillInstructionResult _ _ _ bounds observable + | with_reducible exact related.arrayFillInstructionResult formed _ _ _ bounds observable + | with_reducible exact related.wideArithmeticInstructionResult _ _ bounds observable + | with_reducible exact related.staticSpanInstructionResult _ _ bounds observable + | with_reducible exact related.throwIfNullInstructionResult _ observable + | with_reducible exact related.zeroGuardInstructionResult _ _ _ _ observable + | with_reducible exact related.comparisonGuardInstructionResult _ _ _ _ observable + | with_reducible exact related.otherMemoryInstructionResult _ formed _ bounds observable #print axioms Memory.Renamed.memoryInstructionResult diff --git a/CIL/Safety/MemorySteps.lean b/CIL/Safety/MemorySteps.lean index 926b96b4b..6eecb7c2d 100644 --- a/CIL/Safety/MemorySteps.lean +++ b/CIL/Safety/MemorySteps.lean @@ -4,9 +4,10 @@ import CIL.Safety.GuardNormalization /-! # Memory step equations -One `@[cil_step]` equation per `memoryInstruction` arm and operand shape, so a `.memory` step +One `@[cil_step]` equation per memory model and operand shape, so a bound call rewrites to that arm's body and never unfolds `staticInstruction` or `memoryInstruction`. -The static-field arms keep `step_memory_eq`, which these outrank. +The span constructors over static storage keep `modelStep_memory`, which these outrank. `ldlen` of a +value array has its equations here beside the view checks it shares with them. -/ namespace CIL.Safety @@ -18,13 +19,12 @@ private theorem bind_ite {α β : Type} (c : Prop) [Decidable c] (a b : Executio /-- Closes one memory step equation through `step` and both memory dispatchers. Both dispatchers check the same operands, so a decided guard leaves one arm and no `if`. -/ local macro "memory_step_proof" : tactic => `(tactic| ( - simp only [step, staticInstruction, memoryInstruction, spanFillInstruction, spanCopyInstruction, - otherMemoryInstruction, wideArithmeticInstruction, referenceAddInstruction, - vector128UnsafeInstruction, - displaceReference, observerCount, - CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, - Bool.or_false, Bool.false_eq_true, ite_false, ite_true, CIL.offsetValue, cil_guard, bind_ite, - bind_assoc, pure_bind] + simp only [modelStep_memory, step, staticInstruction, memoryInstruction, spanFillInstruction, spanCopyInstruction, + copyBlockBytes, initBlockBytes, otherMemoryInstruction, wideArithmeticInstruction, referenceAddInstruction, + vector128UnsafeInstruction, displaceReference, observerCount, viewOperandCount, viewOperands, + spanView, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, List.any, + Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, ite_true, CIL.offsetValue, + cil_guard, bind_ite, bind_assoc, pure_bind] all_goals (repeat' split) <;> (try simp only [*, Bool.false_eq_true, ite_false, ite_true, bind_assoc, pure_bind]) <;> rfl)) @@ -33,40 +33,40 @@ section MemoryEquations variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (memory : Memory) /-- Loading through a writable reference uses the existing width check and checked scalar read. -/ -@[cil_step high] theorem step_loadScalar_reference_eq (width : Nat) (signed : Bool) +@[cil_step high] theorem transfer_scalar_reference_eq (width : Nat) (signed : Bool) (reference : ManagedReference) (rest : List Value) : - step body (.loadScalar width signed) pc args frame (.reference reference :: rest) memory = + typedTransferCall (.scalar width signed) false pc frame (.reference reference :: rest) memory = if width = 1 || width = 2 || width = 4 || width = 8 then do let value ← loadValue memory reference width signed return .next (pc + 1) (.scalar value :: rest) frame memory else .error (.unsupported "Scalar memory width") := by - rw [step_loadScalar_eq] - simp only [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, + simp only [typedTransferCall, observedTransfer, transferValues, cil_guard, unspecifiedOperands, + List.take, List.any, Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, bind_ite, bind_assoc, pure_bind] rfl -#print axioms step_loadScalar_reference_eq +#print axioms transfer_scalar_reference_eq /-- A readonly reference permits the same checked read without acquiring write authority. -/ -@[cil_step high] theorem step_loadScalar_readOnlyReference_eq (width : Nat) (signed : Bool) +@[cil_step high] theorem transfer_scalar_readOnlyReference_eq (width : Nat) (signed : Bool) (reference : ManagedReference) (rest : List Value) : - step body (.loadScalar width signed) pc args frame + typedTransferCall (.scalar width signed) false pc frame (.readOnlyReference reference :: rest) memory = if width = 1 || width = 2 || width = 4 || width = 8 then do let value ← loadValue memory reference width signed return .next (pc + 1) (.scalar value :: rest) frame memory else .error (.unsupported "Scalar memory width") := by - rw [step_loadScalar_eq] - simp only [instructionStep, instruction, cil_guard, unspecifiedOperands, List.take, List.any, + simp only [typedTransferCall, observedTransfer, transferValues, cil_guard, unspecifiedOperands, + List.take, List.any, Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, bind_ite, bind_assoc, pure_bind] rfl -#print axioms step_loadScalar_readOnlyReference_eq +#print axioms transfer_scalar_readOnlyReference_eq /-- `bigMul64` stores the low 64 bits of the product through `low` and pushes the high 64. -/ @[cil_step high] theorem step_memory_bigMul64_eq (low : ManagedReference) (right left : CIL.W64) (rest : List Value) : - step body (.memory (.bigMul64 false)) pc args frame + modelStep (some (.memory (.bigMul64 false))) body pc frame (.reference low :: .scalar (.i64 right) :: .scalar (.i64 left) :: rest) memory = do let stored ← storeValue memory low (.i64 (left * right)) return .next (pc + 1) @@ -76,7 +76,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem /-- Signed `bigMul64` stores the same low word and pushes the signed high 64 bits. -/ @[cil_step high] theorem step_memory_bigMul64_signed_eq (low : ManagedReference) (right left : CIL.W64) (rest : List Value) : - step body (.memory (.bigMul64 true)) pc args frame + modelStep (some (.memory (.bigMul64 true))) body pc frame (.reference low :: .scalar (.i64 right) :: .scalar (.i64 left) :: rest) memory = do let stored ← storeValue memory low (.i64 (left * right)) return .next (pc + 1) @@ -86,7 +86,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem /-- `Math.DivRem(int, int, out int)` divides first, then stores the remainder's four bytes. -/ @[cil_step high] theorem step_memory_divRemOut32_eq (destination : ManagedReference) (right left : CIL.W32) (rest : List Value) : - step body (.memory (.divRemOut 32)) pc args frame + modelStep (some (.memory (.divRemOut 32))) body pc frame (.reference destination :: .scalar (.i32 right) :: .scalar (.i32 left) :: rest) memory = do match CIL.divRemBits true left right with | .error fault => .error (.arithmetic fault) @@ -96,15 +96,16 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem (write memory address (numberBytes remainder.toNat 4) ordinaryAccessAlignment) return .next (pc + 1) (.scalar (.i32 quotient) :: rest) frame stored := by cases outcome : CIL.divRemBits true left right <;> - simp only [step, staticInstruction, memoryInstruction, wideArithmeticInstruction, observerCount, + simp only [modelStep_memory, step, staticInstruction, memoryInstruction, wideArithmeticInstruction, observerCount, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, - Bool.or_false, Bool.false_eq_true, ite_false, cil_guard, outcome, bind_assoc, pure_bind] <;> + viewOperandCount, viewOperands, Bool.or_false, Bool.false_eq_true, ite_false, cil_guard, + outcome, bind_assoc, pure_bind] <;> rfl /-- `Math.DivRem(long, long, out long)` divides first, then stores the remainder's eight bytes. -/ @[cil_step high] theorem step_memory_divRemOut64_eq (destination : ManagedReference) (right left : CIL.W64) (rest : List Value) : - step body (.memory (.divRemOut 64)) pc args frame + modelStep (some (.memory (.divRemOut 64))) body pc frame (.reference destination :: .scalar (.i64 right) :: .scalar (.i64 left) :: rest) memory = do match CIL.divRemBits true left right with | .error fault => .error (.arithmetic fault) @@ -114,16 +115,17 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem (write memory address (numberBytes remainder.toNat 8) ordinaryAccessAlignment) return .next (pc + 1) (.scalar (.i64 quotient) :: rest) frame stored := by cases outcome : CIL.divRemBits true left right <;> - simp only [step, staticInstruction, memoryInstruction, wideArithmeticInstruction, observerCount, + simp only [modelStep_memory, step, staticInstruction, memoryInstruction, wideArithmeticInstruction, observerCount, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, - Bool.or_false, Bool.false_eq_true, ite_false, cil_guard, outcome, bind_assoc, pure_bind] <;> + viewOperandCount, viewOperands, Bool.or_false, Bool.false_eq_true, ite_false, cil_guard, + outcome, bind_assoc, pure_bind] <;> rfl /-- A zero-byte copy touches no memory, even through null references; otherwise overlapping source and destination regions are rejected. -/ @[cil_step high] theorem step_memory_copyBlock_eq (alignment : Nat) (count : CIL.W32) (source destination : ManagedReference) (rest : List Value) : - step body (.memory (.copyBlock alignment)) pc args frame + step body (.cpblk alignment) pc args frame (.scalar (.i32 count) :: .reference source :: .reference destination :: rest) memory = if count.toNat = 0 then .ok (.next (pc + 1) rest frame memory) else do let src ← referenceAt source @@ -141,7 +143,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem byte becomes the low byte of `value`. -/ @[cil_step high] theorem step_memory_initBlock_eq (alignment : Nat) (count value : CIL.W32) (destination : ManagedReference) (rest : List Value) : - step body (.memory (.initBlock alignment)) pc args frame + step body (.initblk alignment) pc args frame (.scalar (.i32 count) :: .scalar (.i32 value) :: .reference destination :: rest) memory = if count.toNat = 0 then .ok (.next (pc + 1) rest frame memory) else do let dst ← referenceAt destination @@ -153,26 +155,26 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem -- Vector loads and stores, one equation per stored value shape. @[cil_step high] theorem step_memory_load128_eq (r : ManagedReference) (rest : List Value) : - step body (.memory .load128) pc args frame (.reference r :: rest) memory = do + modelStep (some (.memory .load128)) body pc frame (.reference r :: rest) memory = do let value ← loadValue memory r 16 return .next (pc + 1) (.scalar value :: rest) frame memory := by memory_step_proof @[cil_step high] theorem step_memory_load256_eq (r : ManagedReference) (rest : List Value) : - step body (.memory .load256) pc args frame (.reference r :: rest) memory = do + modelStep (some (.memory .load256)) body pc frame (.reference r :: rest) memory = do let value ← loadValue memory r 32 return .next (pc + 1) (.scalar value :: rest) frame memory := by memory_step_proof @[cil_step high] theorem step_memory_load512_eq (r : ManagedReference) (rest : List Value) : - step body (.memory .load512) pc args frame (.reference r :: rest) memory = do + modelStep (some (.memory .load512)) body pc frame (.reference r :: rest) memory = do let value ← loadValue memory r 64 return .next (pc + 1) (.scalar value :: rest) frame memory := by memory_step_proof @[cil_step high] theorem step_memory_store128_eq (bits : BitVec 128) (r : ManagedReference) (rest : List Value) : - step body (.memory .store128) pc args frame + modelStep (some (.memory .store128)) body pc frame (.scalar (.v128 bits) :: .reference r :: rest) memory = do let stored ← storeValue memory r (.v128 bits) return .next (pc + 1) rest frame stored := by @@ -180,7 +182,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem @[cil_step high] theorem step_memory_store128_snapshot_eq (bytes : List (BitVec 8 × Bool)) (r : ManagedReference) (rest : List Value) : - step body (.memory .store128) pc args frame + modelStep (some (.memory .store128)) body pc frame (.scalar (.snapshot .vector128 bytes) :: .reference r :: rest) memory = do let stored ← storeValue memory r (.snapshot .vector128 bytes) return .next (pc + 1) rest frame stored := by @@ -188,7 +190,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem @[cil_step high] theorem step_memory_store256_eq (bits : BitVec 256) (r : ManagedReference) (rest : List Value) : - step body (.memory .store256) pc args frame + modelStep (some (.memory .store256)) body pc frame (.scalar (.v256 bits) :: .reference r :: rest) memory = do let stored ← storeValue memory r (.v256 bits) return .next (pc + 1) rest frame stored := by @@ -196,7 +198,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem @[cil_step high] theorem step_memory_store256_snapshot_eq (bytes : List (BitVec 8 × Bool)) (r : ManagedReference) (rest : List Value) : - step body (.memory .store256) pc args frame + modelStep (some (.memory .store256)) body pc frame (.scalar (.snapshot .vector256 bytes) :: .reference r :: rest) memory = do let stored ← storeValue memory r (.snapshot .vector256 bytes) return .next (pc + 1) rest frame stored := by @@ -204,7 +206,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem @[cil_step high] theorem step_memory_store512_eq (bits : BitVec 512) (r : ManagedReference) (rest : List Value) : - step body (.memory .store512) pc args frame + modelStep (some (.memory .store512)) body pc frame (.scalar (.v512 bits) :: .reference r :: rest) memory = do let stored ← storeValue memory r (.v512 bits) return .next (pc + 1) rest frame stored := by @@ -212,37 +214,69 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem @[cil_step high] theorem step_memory_store512_snapshot_eq (bytes : List (BitVec 8 × Bool)) (r : ManagedReference) (rest : List Value) : - step body (.memory .store512) pc args frame + modelStep (some (.memory .store512)) body pc frame (.scalar (.snapshot .vector512 bytes) :: .reference r :: rest) memory = do let stored ← storeValue memory r (.snapshot .vector512 bytes) return .next (pc + 1) rest frame stored := by memory_step_proof -@[cil_step high] theorem step_memory_init256_eq (r : ManagedReference) (rest : List Value) : - step body (.memory .init256) pc args frame (.reference r :: rest) memory = do - let stored ← storeValue memory r (.v256 0) - return .next (pc + 1) rest frame stored := by +-- Vector transfer equations normalize the memory operation after unlinked call resolution. +@[cil_step high] private theorem step_memory_vector128Load_eq + (r : ManagedReference) (rest : List Value) : + modelStep (some (.memory (.vector128Unsafe false false))) body pc frame (.reference r :: rest) + memory = do + let value ← loadValue memory r 16 + return .next (pc + 1) (.scalar value :: rest) frame memory := by memory_step_proof --- `Vector128.LoadUnsafe` and `StoreUnsafe` on bytes, without and with a `nuint` byte offset. -@[cil_step high] theorem step_memory_vector128Load_eq (r : ManagedReference) (rest : List Value) : - step body (.memory (.vector128Unsafe false false)) pc args frame (.reference r :: rest) +#print axioms step_memory_vector128Load_eq + +/-- The load call on a writable reference uses the checked transfer. -/ +theorem step_call_vector128Load_eq {method : Extraction.MethodRef} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.memory (.vector128Unsafe false false))) + (r : ManagedReference) (rest : List Value) : + step body (.call method {}) pc args frame (.reference r :: rest) memory = do let value ← loadValue memory r 16 return .next (pc + 1) (.scalar value :: rest) frame memory := by - memory_step_proof + rw [step_call_model body pc args frame memory link found rfl] + apply step_memory_vector128Load_eq + +#print axioms step_call_vector128Load_eq -@[cil_step high] theorem step_memory_vector128Load_readOnly_eq (r : ManagedReference) +@[cil_step high] private theorem step_memory_vector128Load_readOnly_eq + (r : ManagedReference) (rest : List Value) : - step body (.memory (.vector128Unsafe false false)) pc args frame + modelStep (some (.memory (.vector128Unsafe false false))) body pc frame (.readOnlyReference r :: rest) memory = do let value ← loadValue memory r 16 return .next (pc + 1) (.scalar value :: rest) frame memory := by memory_step_proof -@[cil_step high] theorem step_memory_vector128LoadAt_eq (offset : CIL.W64) (r : ManagedReference) +#print axioms step_memory_vector128Load_readOnly_eq + +/-- The load call on a readonly reference uses the checked transfer. -/ +theorem step_call_vector128Load_readOnly_eq {method : Extraction.MethodRef} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.memory (.vector128Unsafe false false))) + (r : ManagedReference) + (rest : List Value) : + step body (.call method {}) pc args frame + (.readOnlyReference r :: rest) memory = do + let value ← loadValue memory r 16 + return .next (pc + 1) (.scalar value :: rest) frame memory := by + rw [step_call_model body pc args frame memory link found rfl] + apply step_memory_vector128Load_readOnly_eq + +#print axioms step_call_vector128Load_readOnly_eq + +@[cil_step high] private theorem step_memory_vector128LoadAt_eq + (offset : CIL.W64) (r : ManagedReference) (rest : List Value) : - step body (.memory (.vector128Unsafe false true)) pc args frame + modelStep (some (.memory (.vector128Unsafe false true))) body pc frame (.scalar (.i64 offset) :: .reference r :: rest) memory = do let address ← referenceAt r let moved ← checkedAt r 0 (add memory address 1 (BitVec.ofInt 64 (offset.toNat : Int))) @@ -250,9 +284,30 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem return .next (pc + 1) (.scalar value :: rest) frame memory := by memory_step_proof -@[cil_step high] theorem step_memory_vector128LoadAt_readOnly_eq (offset : CIL.W64) +#print axioms step_memory_vector128LoadAt_eq + +/-- The offset load call on a writable reference uses the checked transfer. -/ +theorem step_call_vector128LoadAt_eq {method : Extraction.MethodRef} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.memory (.vector128Unsafe false true))) + (offset : CIL.W64) (r : ManagedReference) + (rest : List Value) : + step body (.call method {}) pc args frame + (.scalar (.i64 offset) :: .reference r :: rest) memory = do + let address ← referenceAt r + let moved ← checkedAt r 0 (add memory address 1 (BitVec.ofInt 64 (offset.toNat : Int))) + let value ← loadValue memory (.address moved) 16 + return .next (pc + 1) (.scalar value :: rest) frame memory := by + rw [step_call_model body pc args frame memory link found rfl] + apply step_memory_vector128LoadAt_eq + +#print axioms step_call_vector128LoadAt_eq + +@[cil_step high] private theorem step_memory_vector128LoadAt_readOnly_eq + (offset : CIL.W64) (r : ManagedReference) (rest : List Value) : - step body (.memory (.vector128Unsafe false true)) pc args frame + modelStep (some (.memory (.vector128Unsafe false true))) body pc frame (.scalar (.i64 offset) :: .readOnlyReference r :: rest) memory = do let address ← referenceAt r let moved ← checkedAt r 0 (add memory address 1 (BitVec.ofInt 64 (offset.toNat : Int))) @@ -260,25 +315,84 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem return .next (pc + 1) (.scalar value :: rest) frame memory := by memory_step_proof -@[cil_step high] theorem step_memory_vector128Store_eq (bits : BitVec 128) (r : ManagedReference) +#print axioms step_memory_vector128LoadAt_readOnly_eq + +/-- The offset load call on a readonly reference uses the checked transfer. -/ +theorem step_call_vector128LoadAt_readOnly_eq {method : Extraction.MethodRef} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.memory (.vector128Unsafe false true))) + (offset : CIL.W64) + (r : ManagedReference) (rest : List Value) : + step body (.call method {}) pc args frame + (.scalar (.i64 offset) :: .readOnlyReference r :: rest) memory = do + let address ← referenceAt r + let moved ← checkedAt r 0 (add memory address 1 (BitVec.ofInt 64 (offset.toNat : Int))) + let value ← loadValue memory (.address moved) 16 + return .next (pc + 1) (.scalar value :: rest) frame memory := by + rw [step_call_model body pc args frame memory link found rfl] + apply step_memory_vector128LoadAt_readOnly_eq + +#print axioms step_call_vector128LoadAt_readOnly_eq + +@[cil_step high] private theorem step_memory_vector128Store_eq + (bits : BitVec 128) (r : ManagedReference) (rest : List Value) : - step body (.memory (.vector128Unsafe true false)) pc args frame + modelStep (some (.memory (.vector128Unsafe true false))) body pc frame (.reference r :: .scalar (.v128 bits) :: rest) memory = do let stored ← storeValue memory r (.v128 bits) return .next (pc + 1) rest frame stored := by memory_step_proof -@[cil_step high] theorem step_memory_vector128Store_snapshot_eq (bytes : List (BitVec 8 × Bool)) +#print axioms step_memory_vector128Store_eq + +/-- The store call on defined vector bits uses the checked transfer. -/ +theorem step_call_vector128Store_eq {method : Extraction.MethodRef} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.memory (.vector128Unsafe true false))) + (bits : BitVec 128) (r : ManagedReference) + (rest : List Value) : + step body (.call method {}) pc args frame + (.reference r :: .scalar (.v128 bits) :: rest) memory = do + let stored ← storeValue memory r (.v128 bits) + return .next (pc + 1) rest frame stored := by + rw [step_call_model body pc args frame memory link found rfl] + apply step_memory_vector128Store_eq + +#print axioms step_call_vector128Store_eq + +@[cil_step high] private theorem step_memory_vector128Store_snapshot_eq + (bytes : List (BitVec 8 × Bool)) (r : ManagedReference) (rest : List Value) : - step body (.memory (.vector128Unsafe true false)) pc args frame + modelStep (some (.memory (.vector128Unsafe true false))) body pc frame (.reference r :: .scalar (.snapshot .vector128 bytes) :: rest) memory = do let stored ← storeValue memory r (.snapshot .vector128 bytes) return .next (pc + 1) rest frame stored := by memory_step_proof -@[cil_step high] theorem step_memory_vector128StoreAt_eq (offset : CIL.W64) (bits : BitVec 128) +#print axioms step_memory_vector128Store_snapshot_eq + +/-- The store call on a vector snapshot uses the checked transfer. -/ +theorem step_call_vector128Store_snapshot_eq {method : Extraction.MethodRef} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.memory (.vector128Unsafe true false))) + (bytes : List (BitVec 8 × Bool)) + (r : ManagedReference) (rest : List Value) : + step body (.call method {}) pc args frame + (.reference r :: .scalar (.snapshot .vector128 bytes) :: rest) memory = do + let stored ← storeValue memory r (.snapshot .vector128 bytes) + return .next (pc + 1) rest frame stored := by + rw [step_call_model body pc args frame memory link found rfl] + apply step_memory_vector128Store_snapshot_eq + +#print axioms step_call_vector128Store_snapshot_eq + +@[cil_step high] private theorem step_memory_vector128StoreAt_eq + (offset : CIL.W64) (bits : BitVec 128) (r : ManagedReference) (rest : List Value) : - step body (.memory (.vector128Unsafe true true)) pc args frame + modelStep (some (.memory (.vector128Unsafe true true))) body pc frame (.scalar (.i64 offset) :: .reference r :: .scalar (.v128 bits) :: rest) memory = do let address ← referenceAt r let moved ← checkedAt r 0 (add memory address 1 (BitVec.ofInt 64 (offset.toNat : Int))) @@ -286,9 +400,30 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem return .next (pc + 1) rest frame stored := by memory_step_proof -@[cil_step high] theorem step_memory_vector128StoreAt_snapshot_eq (offset : CIL.W64) +#print axioms step_memory_vector128StoreAt_eq + +/-- The offset store call on defined vector bits uses the checked transfer. -/ +theorem step_call_vector128StoreAt_eq {method : Extraction.MethodRef} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.memory (.vector128Unsafe true true))) + (offset : CIL.W64) (bits : BitVec 128) + (r : ManagedReference) (rest : List Value) : + step body (.call method {}) pc args frame + (.scalar (.i64 offset) :: .reference r :: .scalar (.v128 bits) :: rest) memory = do + let address ← referenceAt r + let moved ← checkedAt r 0 (add memory address 1 (BitVec.ofInt 64 (offset.toNat : Int))) + let stored ← storeValue memory (.address moved) (.v128 bits) + return .next (pc + 1) rest frame stored := by + rw [step_call_model body pc args frame memory link found rfl] + apply step_memory_vector128StoreAt_eq + +#print axioms step_call_vector128StoreAt_eq + +@[cil_step high] private theorem step_memory_vector128StoreAt_snapshot_eq + (offset : CIL.W64) (bytes : List (BitVec 8 × Bool)) (r : ManagedReference) (rest : List Value) : - step body (.memory (.vector128Unsafe true true)) pc args frame + modelStep (some (.memory (.vector128Unsafe true true))) body pc frame (.scalar (.i64 offset) :: .reference r :: .scalar (.snapshot .vector128 bytes) :: rest) memory = do let address ← referenceAt r @@ -297,27 +432,143 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem return .next (pc + 1) rest frame stored := by memory_step_proof +#print axioms step_memory_vector128StoreAt_snapshot_eq + +/-- The offset store call on a vector snapshot uses the checked transfer. -/ +theorem step_call_vector128StoreAt_snapshot_eq {method : Extraction.MethodRef} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.memory (.vector128Unsafe true true))) + (offset : CIL.W64) + (bytes : List (BitVec 8 × Bool)) (r : ManagedReference) (rest : List Value) : + step body (.call method {}) pc args frame + (.scalar (.i64 offset) :: .reference r :: .scalar (.snapshot .vector128 bytes) :: rest) + memory = do + let address ← referenceAt r + let moved ← checkedAt r 0 (add memory address 1 (BitVec.ofInt 64 (offset.toNat : Int))) + let stored ← storeValue memory (.address moved) (.snapshot .vector128 bytes) + return .next (pc + 1) rest frame stored := by + rw [step_call_model body pc args frame memory link found rfl] + apply step_memory_vector128StoreAt_snapshot_eq + +#print axioms step_call_vector128StoreAt_snapshot_eq + -- References, pointer arithmetic and bit casts. @[cil_step high] theorem step_memory_asRef_eq (r : ManagedReference) (rest : List Value) : - step body (.memory .asRef) pc args frame (.reference r :: rest) memory = do + modelStep (some (.memory .asRef)) body pc frame (.reference r :: rest) memory = do let value ← formValue memory r return .next (pc + 1) (value :: rest) frame memory := by memory_step_proof +/-- Adding a signed 32-bit byte offset preserves the reference checks. -/ +@[cil_step high] theorem step_add_reference_i32_eq (offset : CIL.W32) + (reference : ManagedReference) (rest : List Value) : + step body .add pc args frame + (.scalar (.i32 offset) :: .reference reference :: rest) memory = do + let address ← referenceAt reference + let result ← checkedAt reference 0 + (add memory address 1 (BitVec.ofInt 64 offset.toInt)) + return .next (pc + 1) (.reference (.address result) :: rest) frame memory := by + memory_step_proof +#print axioms step_add_reference_i32_eq + +/-- Adding a signed 64-bit byte offset preserves the reference checks. -/ +@[cil_step high] theorem step_add_reference_i64_eq (offset : CIL.W64) + (reference : ManagedReference) (rest : List Value) : + step body .add pc args frame + (.scalar (.i64 offset) :: .reference reference :: rest) memory = do + let address ← referenceAt reference + let result ← checkedAt reference 0 + (add memory address 1 (BitVec.ofInt 64 offset.toInt)) + return .next (pc + 1) (.reference (.address result) :: rest) frame memory := by + memory_step_proof +#print axioms step_add_reference_i64_eq + +/-- Commuting the scalar and reference preserves byte-offset execution. -/ +@[cil_step high] theorem step_add_commuted_eq (offset : CIL.Value) + (reference : ManagedReference) (rest : List Value) : + step body .add pc args frame (.reference reference :: .scalar offset :: rest) memory = + step body .add pc args frame (.scalar offset :: .reference reference :: rest) memory := + id rfl +#print axioms step_add_commuted_eq + +/-- Adding a signed 32-bit byte offset preserves the reference checks and readonly capability. -/ +@[cil_step high] theorem step_add_readOnly_i32_eq (offset : CIL.W32) + (reference : ManagedReference) (rest : List Value) : + step body .add pc args frame + (.scalar (.i32 offset) :: .readOnlyReference reference :: rest) memory = do + let address ← referenceAt reference + let result ← checkedAt reference 0 + (add memory address 1 (BitVec.ofInt 64 offset.toInt)) + return .next (pc + 1) (.readOnlyReference (.address result) :: rest) frame memory := by + memory_step_proof +#print axioms step_add_readOnly_i32_eq + +/-- Adding a signed 64-bit byte offset preserves the reference checks and readonly capability. -/ +@[cil_step high] theorem step_add_readOnly_i64_eq (offset : CIL.W64) + (reference : ManagedReference) (rest : List Value) : + step body .add pc args frame + (.scalar (.i64 offset) :: .readOnlyReference reference :: rest) memory = do + let address ← referenceAt reference + let result ← checkedAt reference 0 + (add memory address 1 (BitVec.ofInt 64 offset.toInt)) + return .next (pc + 1) (.readOnlyReference (.address result) :: rest) frame memory := by + memory_step_proof +#print axioms step_add_readOnly_i64_eq + +/-- Commuting the scalar and reference preserves byte-offset execution. -/ +@[cil_step high] theorem step_add_readOnly_commuted_eq (offset : CIL.Value) + (reference : ManagedReference) (rest : List Value) : + step body .add pc args frame + (.readOnlyReference reference :: .scalar offset :: rest) memory = + step body .add pc args frame + (.scalar offset :: .readOnlyReference reference :: rest) memory := + id rfl +#print axioms step_add_readOnly_commuted_eq + +/-- Adding a sign-extended 32-bit byte offset moves a native table cursor through its checked + backing storage and keeps the table's identities. -/ +@[cil_step high] theorem step_add_table_i32_eq (offset : CIL.W32) (table : CIL.MethodTable) + (rest : List Value) : + step body .add pc args frame (.scalar (.i32 offset) :: .methodTable table :: rest) memory = do + let selected ← checkedAt .null nativeMethodWidth + (addNativeTable table memory (offset.signExtend 64)) + return .next (pc + 1) (.methodTable selected :: rest) frame memory := by + memory_step_proof +#print axioms step_add_table_i32_eq + +/-- Adding a 64-bit byte offset moves a native table cursor through its checked backing storage + and keeps the table's identities. -/ +@[cil_step high] theorem step_add_table_i64_eq (offset : CIL.W64) (table : CIL.MethodTable) + (rest : List Value) : + step body .add pc args frame (.scalar (.i64 offset) :: .methodTable table :: rest) memory = do + let selected ← checkedAt .null nativeMethodWidth (addNativeTable table memory offset) + return .next (pc + 1) (.methodTable selected :: rest) frame memory := by + memory_step_proof +#print axioms step_add_table_i64_eq + +/-- Commuting the scalar and the native table preserves execution for every offset value. -/ +@[cil_step high] theorem step_add_table_commuted_eq (offset : CIL.Value) + (table : CIL.MethodTable) (rest : List Value) : + step body .add pc args frame (.methodTable table :: .scalar offset :: rest) memory = + step body .add pc args frame (.scalar offset :: .methodTable table :: rest) memory := + id rfl +#print axioms step_add_table_commuted_eq + /-- Commuted reference offsets reuse the scalar-first memory step equations. -/ @[cil_step high] theorem step_memory_add_commuted_eq (size : Nat) (signed : Bool) (reference : ManagedReference) (offset : CIL.Value) (rest : List Value) : - step body (.memory (.add size signed)) pc args frame + modelStep (some (.memory (.add size signed))) body pc frame (.reference reference :: .scalar offset :: rest) memory = - step body (.memory (.add size signed)) pc args frame - (.scalar offset :: .reference reference :: rest) memory := by - rfl + modelStep (some (.memory (.add size signed))) body pc frame + (.scalar offset :: .reference reference :: rest) memory := + id rfl #print axioms step_memory_add_commuted_eq -- One equation per `offsetValue` shape, so the offset is a term and needs no side condition. @[cil_step high] theorem step_memory_add_signed_i32_eq (size : Nat) (offset : CIL.W32) (r : ManagedReference) (rest : List Value) : - step body (.memory (.add size true)) pc args frame + modelStep (some (.memory (.add size true))) body pc frame (.scalar (.i32 offset) :: .reference r :: rest) memory = do let address ← referenceAt r let result ← checkedAt r 0 (add memory address size (BitVec.ofInt 64 offset.toInt)) @@ -326,7 +577,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem @[cil_step high] theorem step_memory_add_signed_i64_eq (size : Nat) (offset : CIL.W64) (r : ManagedReference) (rest : List Value) : - step body (.memory (.add size true)) pc args frame + modelStep (some (.memory (.add size true))) body pc frame (.scalar (.i64 offset) :: .reference r :: rest) memory = do let address ← referenceAt r let result ← checkedAt r 0 (add memory address size (BitVec.ofInt 64 offset.toInt)) @@ -335,7 +586,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem @[cil_step high] theorem step_memory_add_unsigned_i64_eq (size : Nat) (offset : CIL.W64) (r : ManagedReference) (rest : List Value) : - step body (.memory (.add size false)) pc args frame + modelStep (some (.memory (.add size false))) body pc frame (.scalar (.i64 offset) :: .reference r :: rest) memory = do let address ← referenceAt r let result ← checkedAt r 0 (add memory address size (BitVec.ofInt 64 (offset.toNat : Int))) @@ -345,16 +596,16 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem /-- Offsetting a readonly reference keeps the restriction and the writable form's checks. -/ @[cil_step high] theorem step_memory_add_readOnly_commuted_eq (size : Nat) (signed : Bool) (reference : ManagedReference) (offset : CIL.Value) (rest : List Value) : - step body (.memory (.add size signed)) pc args frame + modelStep (some (.memory (.add size signed))) body pc frame (.readOnlyReference reference :: .scalar offset :: rest) memory = - step body (.memory (.add size signed)) pc args frame - (.scalar offset :: .readOnlyReference reference :: rest) memory := by - rfl + modelStep (some (.memory (.add size signed))) body pc frame + (.scalar offset :: .readOnlyReference reference :: rest) memory := + id rfl #print axioms step_memory_add_readOnly_commuted_eq @[cil_step high] theorem step_memory_add_readOnly_signed_i32_eq (size : Nat) (offset : CIL.W32) (r : ManagedReference) (rest : List Value) : - step body (.memory (.add size true)) pc args frame + modelStep (some (.memory (.add size true))) body pc frame (.scalar (.i32 offset) :: .readOnlyReference r :: rest) memory = do let address ← referenceAt r let result ← checkedAt r 0 (add memory address size (BitVec.ofInt 64 offset.toInt)) @@ -363,7 +614,7 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem @[cil_step high] theorem step_memory_add_readOnly_signed_i64_eq (size : Nat) (offset : CIL.W64) (r : ManagedReference) (rest : List Value) : - step body (.memory (.add size true)) pc args frame + modelStep (some (.memory (.add size true))) body pc frame (.scalar (.i64 offset) :: .readOnlyReference r :: rest) memory = do let address ← referenceAt r let result ← checkedAt r 0 (add memory address size (BitVec.ofInt 64 offset.toInt)) @@ -372,22 +623,17 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem @[cil_step high] theorem step_memory_add_readOnly_unsigned_i64_eq (size : Nat) (offset : CIL.W64) (r : ManagedReference) (rest : List Value) : - step body (.memory (.add size false)) pc args frame + modelStep (some (.memory (.add size false))) body pc frame (.scalar (.i64 offset) :: .readOnlyReference r :: rest) memory = do let address ← referenceAt r let result ← checkedAt r 0 (add memory address size (BitVec.ofInt 64 (offset.toNat : Int))) return .next (pc + 1) (.readOnlyReference (.address result) :: rest) frame memory := by memory_step_proof -@[cil_step high] theorem step_memory_bitcast256_eq (bits : BitVec 256) (rest : List Value) : - step body (.memory .bitcast256) pc args frame (.scalar (.v256 bits) :: rest) memory = - .ok (.next (pc + 1) (.scalar (.v256 bits) :: rest) frame memory) := by - memory_step_proof - -- The operand is any scalar, so the guard stays until the caller's value decides it. @[cil_step high] theorem step_memory_bitcastValue_eq (source target : CIL.SnapshotShape) (value : CIL.Value) (rest : List Value) : - step body (.memory (.bitcastValue source target)) pc args frame (.scalar value :: rest) memory = + modelStep (some (.memory (.bitcastValue source target))) body pc frame (.scalar value :: rest) memory = if value.hasUnspecified then .error (.unsupported "Instruction observes unspecified vector bits") else do @@ -397,28 +643,22 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem -- A byte reinterpreted as a Boolean must be 0 or 1. @[cil_step high] theorem step_memory_bitcastByteBool_i32_eq (bits : CIL.W32) (rest : List Value) : - step body (.memory .bitcastByteBool) pc args frame (.scalar (.i32 bits) :: rest) memory = + modelStep (some (.memory .bitcastByteBool)) body pc frame (.scalar (.i32 bits) :: rest) memory = if bits.toNat ≤ 1 then .ok (.next (pc + 1) (.scalar (.i32 bits) :: rest) frame memory) else .error (.unsupported "Noncanonical Boolean representation") := by memory_step_proof @[cil_step high] theorem step_memory_bitcastByteBool_i8_eq (bits : BitVec 8) (rest : List Value) : - step body (.memory .bitcastByteBool) pc args frame (.scalar (.i8 bits) :: rest) memory = + modelStep (some (.memory .bitcastByteBool)) body pc frame (.scalar (.i8 bits) :: rest) memory = if bits.toNat ≤ 1 then .ok (.next (pc + 1) (.scalar (.i32 (bits.zeroExtend 32)) :: rest) frame memory) else .error (.unsupported "Noncanonical Boolean representation") := by memory_step_proof -@[cil_step high] theorem step_memory_skipInit_eq (r : ManagedReference) (rest : List Value) : - step body (.memory .skipInit) pc args frame (.reference r :: rest) memory = do - let _ ← formValue memory r - return .next (pc + 1) rest frame memory := by - memory_step_proof - -- Spans. @[cil_step high] theorem step_memory_spanReference_eq (r : ManagedReference) (length : Nat) (rest : List Value) : - step body (.memory .spanReference) pc args frame (.span r length :: rest) memory = do + modelStep (some (.memory .spanReference)) body pc frame (.span r length :: rest) memory = do let value ← formValue memory r return .next (pc + 1) (value :: rest) frame memory := by memory_step_proof @@ -427,13 +667,14 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem the view is empty; it validates the view's extent but makes no access. -/ @[cil_step high] theorem step_memory_spanReference_view_eq (reference : ManagedReference) (length width : Nat) (rest : List Value) : - step body (.memory .spanReference) pc args frame + modelStep (some (.memory .spanReference)) body pc frame (.bufferSpan ⟨reference, length, width, true⟩ :: rest) memory = do let _ ← checkedAt reference 0 (SpanView.validate ⟨reference, length, width, true⟩ memory) let value ← formValue memory reference return .next (pc + 1) (value :: rest) frame memory := by - simp only [step, staticInstruction, memoryInstruction, otherMemoryInstruction, observerCount, + simp only [modelStep_memory, step, staticInstruction, memoryInstruction, otherMemoryInstruction, observerCount, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + viewOperandCount, viewOperands, spanView, Bool.or_false, Bool.false_eq_true, ite_false, cil_guard, bind_ite, bind_assoc, pure_bind] all_goals (repeat' split) <;> first | exact absurd trivial (by assumption) @@ -443,13 +684,14 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem to reads, even when the view is empty; it validates the view's extent but makes no access. -/ @[cil_step high] theorem step_memory_spanReference_readOnlyView_eq (reference : ManagedReference) (length width : Nat) (rest : List Value) : - step body (.memory .spanReference) pc args frame + modelStep (some (.memory .spanReference)) body pc frame (.bufferSpan ⟨reference, length, width, false⟩ :: rest) memory = do let _ ← checkedAt reference 0 (SpanView.validate ⟨reference, length, width, false⟩ memory) let _ ← formValue memory reference return .next (pc + 1) (.readOnlyReference reference :: rest) frame memory := by - simp only [step, staticInstruction, memoryInstruction, otherMemoryInstruction, observerCount, + simp only [modelStep_memory, step, staticInstruction, memoryInstruction, otherMemoryInstruction, observerCount, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + viewOperandCount, viewOperands, spanView, Bool.or_false, Bool.false_eq_true, ite_false, cil_guard, bind_ite, bind_assoc, pure_bind] all_goals (repeat' split) <;> first | exact absurd trivial (by assumption) @@ -459,24 +701,24 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem `IndexOutOfRangeException`. -/ @[cil_step high] theorem step_memory_spanItem_eq (width : Nat) (index : CIL.W32) (r : ManagedReference) (length : Nat) (rest : List Value) : - step body (.memory (.spanItem width)) pc args frame + modelStep (some (.memory (.spanItem width))) body pc frame (.scalar (.i32 index) :: .span r length :: rest) memory = if index.toNat < length then do let address ← referenceAt r let result ← checkedAt r 0 (add memory address width (BitVec.ofNat 64 index.toNat)) return .next (pc + 1) (.reference (.address result) :: rest) frame memory - else .error (.thrown "System.IndexOutOfRangeException") := by + else .error (.thrown .indexOutOfRange) := by memory_step_proof @[cil_step high] theorem step_memory_spanLength_eq (r : ManagedReference) (length : Nat) (rest : List Value) : - step body (.memory .spanLength) pc args frame (.span r length :: rest) memory = + modelStep (some (.memory .spanLength)) body pc frame (.span r length :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 (BitVec.ofNat 32 length)) :: rest) frame memory) := by memory_step_proof /-- A buffer span's length retains its validation obligation and does not read its bytes. -/ @[cil_step high] theorem step_memory_bufferSpanLength_eq (view : SpanView) (rest : List Value) : - step body (.memory .spanLength) pc args frame (.bufferSpan view :: rest) memory = do + modelStep (some (.memory .spanLength)) body pc frame (.bufferSpan view :: rest) memory = do let _ ← checkedAt view.reference 0 (view.validate memory) return .next (pc + 1) (.scalar (.i32 (BitVec.ofNat 32 view.length)) :: rest) frame memory := by @@ -486,10 +728,10 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem /-- A suffix slice checks the starting index and preserves the span's storage and capability. -/ @[cil_step high] theorem step_memory_bufferSpanSlice_eq (view : SpanView) (start : CIL.W32) (rest : List Value) : - step body (.memory (.spanSlice false)) pc args frame + modelStep (some (.memory (.spanSlice false))) body pc frame (.scalar (.i32 start) :: .bufferSpan view :: rest) memory = do if start.toNat ≤ view.length then pure () - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) let sliced ← checkedAt view.reference 0 (view.slice memory start.toNat (view.length - start.toNat)) return .next (pc + 1) (.bufferSpan sliced :: rest) frame memory := by @@ -499,10 +741,10 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem /-- A counted slice retains both range checks and the checked view-construction result. -/ @[cil_step high] theorem step_memory_bufferSpanSliceCount_eq (view : SpanView) (start length : CIL.W32) (rest : List Value) : - step body (.memory (.spanSlice true)) pc args frame + modelStep (some (.memory (.spanSlice true))) body pc frame (.scalar (.i32 length) :: .scalar (.i32 start) :: .bufferSpan view :: rest) memory = do if start.toNat ≤ view.length && length.toNat ≤ view.length - start.toNat then pure () - else .error (.thrown "System.ArgumentOutOfRangeException") + else .error (.thrown .argumentOutOfRange) let sliced ← checkedAt view.reference 0 (view.slice memory start.toNat length.toNat) return .next (pc + 1) (.bufferSpan sliced :: rest) frame memory := by memory_step_proof @@ -511,11 +753,11 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem /-- Buffer indexing keeps width, bounds and memory checks before returning a restricted address. -/ @[cil_step high] theorem step_memory_bufferSpanItem_eq (view : SpanView) (width : Nat) (index : CIL.W32) (rest : List Value) : - step body (.memory (.spanItem width)) pc args frame + modelStep (some (.memory (.spanItem width))) body pc frame (.scalar (.i32 index) :: .bufferSpan view :: rest) memory = do if width == view.width then pure () else .error .invalidState if index.toNat < view.length then pure () - else .error (.thrown "System.IndexOutOfRangeException") + else .error (.thrown .indexOutOfRange) let reference ← checkedAt view.reference width (view.element memory index.toNat) return .next (pc + 1) ((if view.writable then .reference (.address reference) @@ -523,61 +765,138 @@ variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (mem memory_step_proof #print axioms step_memory_bufferSpanItem_eq +set_option hygiene false in +/-- Closes a span operation's step through a home: a failed read of the slot faults both sides + alike, and a read view leaves the step on that view. -/ +local macro "home_step_proof" : tactic => `(tactic| ( + cases viewed : spanView frame memory (.spanHome slot) with + | error fault => + simp only [modelStep_memory, viewOperandCount, viewOperands, cil_guard, viewed, + CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + CIL.Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, ite_true, + Bind.bind, Except.bind] + try simp only [spanView, Pure.pure, Except.pure] + | ok result => + obtain ⟨view, rfl⟩ := spanView_home_view viewed + simp only [modelStep_memory, viewOperandCount, viewOperands, cil_guard, viewed, + CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + CIL.Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, ite_true, + Bind.bind, Except.bind] + try simp only [spanView, Pure.pure, Except.pure])) + +/-- `get_Length` through a home steps on the view its slot holds; any other slot faults. -/ +@[cil_step high] theorem step_memory_spanLength_home_eq (slot : Nat) (rest : List Value) : + modelStep (some (.memory .spanLength)) body pc frame (.spanHome slot :: rest) memory = + match spanView frame memory (.spanHome slot) with + | .ok (.bufferSpan view) => + modelStep (some (.memory .spanLength)) body pc frame (.bufferSpan view :: rest) memory + | .ok _ => .error .invalidState + | .error fault => .error fault := by + home_step_proof +#print axioms step_memory_spanLength_home_eq + +/-- `GetReference` through a home steps on the view its slot holds; any other slot faults. -/ +@[cil_step high] theorem step_memory_spanReference_home_eq (slot : Nat) (rest : List Value) : + modelStep (some (.memory .spanReference)) body pc frame (.spanHome slot :: rest) memory = + match spanView frame memory (.spanHome slot) with + | .ok (.bufferSpan view) => + modelStep (some (.memory .spanReference)) body pc frame (.bufferSpan view :: rest) memory + | .ok _ => .error .invalidState + | .error fault => .error fault := by + home_step_proof +#print axioms step_memory_spanReference_home_eq + +/-- An indexed element read through a home steps on the view its slot holds; any other slot + faults. -/ +@[cil_step high] theorem step_memory_spanItem_home_eq (width : Nat) (index : CIL.W32) + (slot : Nat) (rest : List Value) : + modelStep (some (.memory (.spanItem width))) body pc frame + (.scalar (.i32 index) :: .spanHome slot :: rest) memory = + match spanView frame memory (.spanHome slot) with + | .ok (.bufferSpan view) => modelStep (some (.memory (.spanItem width))) body pc frame + (.scalar (.i32 index) :: .bufferSpan view :: rest) memory + | .ok _ => .error .invalidState + | .error fault => .error fault := by + home_step_proof +#print axioms step_memory_spanItem_home_eq + +/-- A slice from a start through a home steps on the view its slot holds; any other slot + faults. -/ +@[cil_step high] theorem step_memory_spanSlice_home_eq (start : CIL.W32) (slot : Nat) + (rest : List Value) : + modelStep (some (.memory (.spanSlice false))) body pc frame + (.scalar (.i32 start) :: .spanHome slot :: rest) memory = + match spanView frame memory (.spanHome slot) with + | .ok (.bufferSpan view) => modelStep (some (.memory (.spanSlice false))) body pc frame + (.scalar (.i32 start) :: .bufferSpan view :: rest) memory + | .ok _ => .error .invalidState + | .error fault => .error fault := by + home_step_proof +#print axioms step_memory_spanSlice_home_eq + +/-- A counted slice through a home steps on the view its slot holds; any other slot faults. -/ +@[cil_step high] theorem step_memory_spanSliceCount_home_eq (start length : CIL.W32) + (slot : Nat) (rest : List Value) : + modelStep (some (.memory (.spanSlice true))) body pc frame + (.scalar (.i32 length) :: .scalar (.i32 start) :: .spanHome slot :: rest) memory = + match spanView frame memory (.spanHome slot) with + | .ok (.bufferSpan view) => modelStep (some (.memory (.spanSlice true))) body pc frame + (.scalar (.i32 length) :: .scalar (.i32 start) :: .bufferSpan view :: rest) memory + | .ok _ => .error .invalidState + | .error fault => .error fault := by + home_step_proof +#print axioms step_memory_spanSliceCount_home_eq + -- Array length: `ldlen` validates the array's view first, then faults on the null array. /-- `ldlen` of an array validates its view, then throws on the null array and pushes the length of any other as a native integer. -/ -theorem step_memory_arrayLength_eq (view : ArrayView) (rest : List Value) : - step body (.memory .arrayLength) pc args frame (.array view :: rest) memory = do +theorem step_ldlen_array_eq (view : ArrayView) (rest : List Value) : + step body .ldlen pc args frame (.array view :: rest) memory = do let _ ← checkedAt view.reference 0 (view.validate memory) - if view.reference == .null then .error (.thrown "System.NullReferenceException") else + if view.reference == .null then .error (.thrown .nullReference) else return .next (pc + 1) (.scalar (.i64 (BitVec.ofNat 64 view.length)) :: rest) frame memory := by - memory_step_proof -#print axioms step_memory_arrayLength_eq + rw [step_ldlen_eq] + simp only [instructionStep, instruction, elementInstruction, cil_guard, unspecifiedOperands, + List.take, List.any, Value.hasUnspecified, Bool.or_false, Bool.false_eq_true, ite_false, + bind_assoc, bind_ite, pure_bind] + all_goals first | rfl | (split <;> rfl) +#print axioms step_ldlen_array_eq /-- `ldlen` of a non-null array pushes its length as a native integer once its view validates. -/ -@[cil_step high] theorem step_memory_arrayLength_address_eq (array : Reference) (length : Nat) +@[cil_step high] theorem step_ldlen_address_eq (array : Reference) (length : Nat) (element : CIL.ArrayElement) (rest : List Value) : - step body (.memory .arrayLength) pc args frame - (.array ⟨.address array, length, element⟩ :: rest) memory = do + step body .ldlen pc args frame (.array ⟨.address array, length, element⟩ :: rest) memory = do let _ ← checkedAt (.address array) 0 (ArrayView.validate ⟨.address array, length, element⟩ memory) return .next (pc + 1) (.scalar (.i64 (BitVec.ofNat 64 length)) :: rest) frame memory := by - rw [step_memory_arrayLength_eq] + rw [step_ldlen_array_eq] rfl -#print axioms step_memory_arrayLength_address_eq +#print axioms step_ldlen_address_eq /-- `ldlen` of the null array throws `NullReferenceException` once its view validates. -/ -@[cil_step high] theorem step_memory_arrayLength_null_eq (length : Nat) - (element : CIL.ArrayElement) (rest : List Value) : - step body (.memory .arrayLength) pc args frame - (.array ⟨.null, length, element⟩ :: rest) memory = +@[cil_step high] theorem step_ldlen_null_eq (length : Nat) (element : CIL.ArrayElement) + (rest : List Value) : + step body .ldlen pc args frame (.array ⟨.null, length, element⟩ :: rest) memory = checkedAt .null 0 (ArrayView.validate ⟨.null, length, element⟩ memory) >>= fun _ => - .error (.thrown "System.NullReferenceException") := by - rw [step_memory_arrayLength_eq] + .error (.thrown .nullReference) := by + rw [step_ldlen_array_eq] rfl -#print axioms step_memory_arrayLength_null_eq - -/-- `ldlen` of a null object reference, as `ldnull` pushes it, throws `NullReferenceException`. -/ -@[cil_step high] theorem step_memory_arrayLength_object_null_eq (rest : List Value) : - step body (.memory .arrayLength) pc args frame (.object .null :: rest) memory = - .error (.thrown "System.NullReferenceException") := by - memory_step_proof -#print axioms step_memory_arrayLength_object_null_eq +#print axioms step_ldlen_null_eq -- Null references; `isNullRef` makes no access, so it answers for expired references too. @[cil_step high] theorem step_memory_nullRef_eq (stack : List Value) : - step body (.memory .nullRef) pc args frame stack memory = + modelStep (some (.memory .nullRef)) body pc frame stack memory = .ok (.next (pc + 1) (.reference .null :: stack) frame memory) := by memory_step_proof @[cil_step high] theorem step_memory_isNullRef_null_eq (rest : List Value) : - step body (.memory .isNullRef) pc args frame (.reference .null :: rest) memory = + modelStep (some (.memory .isNullRef)) body pc frame (.reference .null :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 1) :: rest) frame memory) := by memory_step_proof @[cil_step high] theorem step_memory_isNullRef_address_eq (r : Reference) (rest : List Value) : - step body (.memory .isNullRef) pc args frame (.reference (.address r) :: rest) memory = + modelStep (some (.memory .isNullRef)) body pc frame (.reference (.address r) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 0) :: rest) frame memory) := by memory_step_proof @@ -598,7 +917,6 @@ end MemoryEquations #print axioms step_memory_store256_snapshot_eq #print axioms step_memory_store512_eq #print axioms step_memory_store512_snapshot_eq -#print axioms step_memory_init256_eq #print axioms step_memory_vector128Load_eq #print axioms step_memory_vector128Load_readOnly_eq #print axioms step_memory_vector128LoadAt_eq @@ -614,11 +932,9 @@ end MemoryEquations #print axioms step_memory_add_readOnly_signed_i32_eq #print axioms step_memory_add_readOnly_signed_i64_eq #print axioms step_memory_add_readOnly_unsigned_i64_eq -#print axioms step_memory_bitcast256_eq #print axioms step_memory_bitcastValue_eq #print axioms step_memory_bitcastByteBool_i32_eq #print axioms step_memory_bitcastByteBool_i8_eq -#print axioms step_memory_skipInit_eq #print axioms step_memory_spanReference_eq #print axioms step_memory_spanReference_view_eq #print axioms step_memory_spanReference_readOnlyView_eq diff --git a/CIL/Safety/MethodContract.lean b/CIL/Safety/MethodContract.lean index 503a4f0a6..24961f452 100644 --- a/CIL/Safety/MethodContract.lean +++ b/CIL/Safety/MethodContract.lean @@ -20,7 +20,8 @@ A `MethodContract` states what a method needs (`pre`), which bytes of pre-existi may change (`footprint`, enforced by `Confined`) and what it returns (`post`). `Holds` means every checked invocation meeting `pre` returns normally within the footprint and satisfies `post`. Callers compose through `run_call_of_contract` instead of re-executing the callee, -and `Refines` derives a weaker exported contract from a proved internal one. +and `Refines` derives a weaker exported contract from a proved internal one. `Refuted` is a +counterexample: an input meeting `pre` whose run settles outside the contract or never ends. The tactics at the end drive proofs: `cil_contract_steps` steps the checked interpreter (`CIL/Safety/Execution.lean`) through a body, and `cil_contract_call` / `cil_formed_call` @@ -95,6 +96,19 @@ theorem Confined.ofMemoryBelow {footprint : List (Reference × Nat)} {before aft fun id bound offset _ => preserved.permissions id (old bound) offset false, fun id bound offset _ => preserved.slots id (old bound) offset⟩ +/-- A method that uses only fresh storage during execution leaves every caller allocation, + byte, object slot and access permission unchanged when its frame retires. -/ +theorem Confined.frame_round_trip {body : CIL.Method} {args : List Value} + {before entered updated : Memory} {frame : Frame} + (setup : enterFrame body args before = .ok (frame, entered)) + (preserved : MemoryBelow before.nextIdentity entered updated) + (grows : entered.nextIdentity ≤ updated.nextIdentity) : + Confined [] before (leaveFrame frame updated) := by + apply Confined.ofMemoryBelow (MemoryBelow.frame_round_trip setup preserved) (Nat.le_refl _) + rw [leaveFrame_nextIdentity] + exact Nat.le_trans (enterFrame_fresh _ _ _ _ _ setup).1.1 grows +#print axioms Confined.frame_round_trip + /-- A change confined to no storage keeps all memory that existed before it. -/ theorem Confined.nil_memoryBelow {before after : Memory} (framed : Confined [] before after) : MemoryBelow before.nextIdentity before after := @@ -362,20 +376,66 @@ def MethodContract.Holds {Input Output : Type} (c : MethodContract Input Output) (fun (final, values) => Confined (c.footprint input) initial final ∧ c.Returns input initial final values) -/-- A located semantic fault on an input meeting the precondition refutes the contract at - every fuel budget. Exhaustion alone does not refute termination. -/ -theorem MethodContract.not_holds_of_fault {Input Output : Type} - (contract : MethodContract Input Output) {program : CIL.Program} {method observed : Nat} - {input : Input} {initial : Memory} {fault : LocatedFault} - (permitted : contract.pre input initial) (semantic : fault.fault ≠ .fuelExhausted) - (failed : invoke program observed method (contract.args input) initial = .error fault) : - ¬ contract.Holds program method := by +/-- How a settled outcome of an invocation at `input` from `initial` fails `c`: any fault, or a + return outside the footprint or `post`. -/ +def MethodContract.Violates {Input Output : Type} (c : MethodContract Input Output) + (input : Input) (initial : Memory) : Except LocatedFault (Memory × List Value) → Prop + | .ok (final, values) => + ¬ (Confined (c.footprint input) initial final ∧ c.Returns input initial final values) + | .error _ => True + +/-- A counterexample to `c`: an input and memory meeting `pre` from which the method settles + outside the contract (a definite fault or a wrong return) or never terminates. It is evidence + that the method violates the contract in the model. A run that reaches an operation the model + does not cover (`unsupported`), or that exhausts one fuel budget, is no evidence and gives + no `Refuted`, although it can make `¬ c.Holds` provable. -/ +def MethodContract.Refuted {Input Output : Type} (c : MethodContract Input Output) + (program : CIL.Program) (method : Nat) : Prop := + ∃ input initial, c.pre input initial ∧ + Counterexample program method (c.args input) initial (c.Violates input initial) + +/-- A refuted contract does not hold. -/ +theorem MethodContract.Refuted.not_holds {Input Output : Type} + {c : MethodContract Input Output} {program : CIL.Program} {method : Nat} + (refuted : c.Refuted program method) : ¬ c.Holds program method := by intro holds - obtain ⟨fuel, ⟨final, values⟩, invoked, _⟩ := holds input initial permitted - exact invoke_fault_refutes_success program observed method (contract.args input) initial - fault semantic failed fuel (final, values) invoked + obtain ⟨input, initial, permitted, counter⟩ := refuted + obtain ⟨fuel, ⟨final, values⟩, ran, returned⟩ := holds input initial permitted + exact counter.violates (outcome := .ok (final, values)) trivial ran returned -#print axioms MethodContract.not_holds_of_fault +/-- A located definite fault on an input meeting the precondition refutes the contract. -/ +theorem MethodContract.Refuted.of_fault {Input Output : Type} + {c : MethodContract Input Output} {program : CIL.Program} {method observed : Nat} + {input : Input} {initial : Memory} {fault : LocatedFault} + (permitted : c.pre input initial) (definite : fault.fault.definite ≠ false) + (failed : invoke program observed method (c.args input) initial = .error fault) : + c.Refuted program method := + ⟨input, initial, permitted, .inl ⟨observed, _, failed, definite, trivial⟩⟩ + +/-- A return outside the footprint or the postcondition refutes the contract. -/ +theorem MethodContract.Refuted.of_return {Input Output : Type} + {c : MethodContract Input Output} {program : CIL.Program} {method fuel : Nat} + {input : Input} {initial final : Memory} {values : List Value} + (permitted : c.pre input initial) + (ran : invoke program fuel method (c.args input) initial = .ok (final, values)) + (wrong : ¬ (Confined (c.footprint input) initial final ∧ + c.Returns input initial final values)) : + c.Refuted program method := + ⟨input, initial, permitted, .inl ⟨fuel, _, ran, trivial, wrong⟩⟩ + +/-- A run that exhausts every fuel budget on an input meeting the precondition refutes the + contract, which promises a return. -/ +theorem MethodContract.Refuted.of_diverges {Input Output : Type} + {c : MethodContract Input Output} {program : CIL.Program} {method : Nat} + {input : Input} {initial : Memory} (permitted : c.pre input initial) + (diverges : Diverges program method (c.args input) initial) : + c.Refuted program method := + ⟨input, initial, permitted, .inr diverges⟩ + +#print axioms MethodContract.Refuted.not_holds +#print axioms MethodContract.Refuted.of_fault +#print axioms MethodContract.Refuted.of_return +#print axioms MethodContract.Refuted.of_diverges /-- A contract is `Ensures` at each input, with the footprint as part of the postcondition. -/ theorem MethodContract.Holds.ensures {Input Output : Type} {c : MethodContract Input Output} @@ -452,7 +512,7 @@ theorem MethodContract.Holds.result {Input Output : Type} {c : MethodContract In /-- Compose a fetched call through the callee's contract instead of its body. The continuation receives the callee's final memory and typed output with their `CallResult`. -/ theorem run_call_of_contract {Input Output : Type} {c : MethodContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (input : Input) @@ -477,7 +537,7 @@ theorem run_call_of_contract {Input Output : Type} {c : MethodContract Input Out /-- A successful call from a well-formed memory returns a well-formed memory and keeps the static world and catalog agreement, whatever the callee's contract states. -/ theorem call_frame_invariants {program : CIL.Program} {method pc callee fuel : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack arguments rest values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest values : List Value} {frame : Frame} {memory updated final : Memory} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : @@ -500,7 +560,7 @@ theorem call_frame_invariants {program : CIL.Program} {method pc callee fuel : N /-- Compose an arbitrary checked helper postcondition with the frame and allocation evidence proved by its invocation. Cell preservation alone does not establish confinement. -/ theorem WP.call_framed {program : CIL.Program} {method pc callee : Nat} - {body : CIL.Method} {operation : CIL.Op} {args stack arguments rest : List Value} + {body : CIL.Method} {operation : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {helperPost post : (Memory × List Value) → Prop} (found : program[method]? = some body) (fetched : body.code[pc]? = some operation) @@ -527,7 +587,7 @@ theorem WP.call_framed {program : CIL.Program} {method pc callee : Nat} /-- The `FrameInvariants` a call composed through a contract keeps, from its `CallResult` and the step that made the call. -/ theorem CallResult.frame_invariants {Input Output : Type} {c : MethodContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack rest : List Value} {frame : Frame} {memory updated final : Memory} {input : Input} {output : Output} (call : CallResult c program callee input updated final output) @@ -543,7 +603,7 @@ theorem CallResult.frame_invariants {Input Output : Type} {c : MethodContract In /-- `run_call_of_contract` from a well-formed memory: the continuation also receives the `FrameInvariants` the call keeps from the caller's memory to the callee's final memory. -/ theorem run_call_of_contract_formed {Input Output : Type} {c : MethodContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (input : Input) @@ -767,14 +827,38 @@ def cilInvocationEvidence (type : Expr) : Bool := | some (.lam _ _ body _) => invocation body | _ => false) +open Lean Meta in +/-- The memory an invocation fact starts from, when its type states it. -/ +def cilInvocationMemory? (type : Expr) : Option Expr := + let type := type.consumeMData + if type.isAppOf `CIL.Safety.InvocationCertificate then type.getAppArgs[3]? + else if type.isAppOf ``CIL.Safety.CallResult then type.getAppArgs[type.getAppNumArgs - 3]? + else (type.find? (·.isAppOfArity ``CIL.Safety.invoke 5)).bind (·.getAppArgs[4]?) + +open Lean Meta in +/-- Whether invocation evidence that starts from `start` can match a call stepped from `current`. + A call passes on the memory it steps from or a term built from it, never another variable. -/ +def cilStartsAt (current start : Expr) : MetaM Bool := do + let (.fvar current, .fvar start) := (current.consumeMData, start.consumeMData) | return true + if current == start then return true + return (← current.isLetVar) || (← start.isLetVar) + open Lean Elab Tactic Meta in -/-- Use supplied or local successful invocation evidence at the fetched call. -/ +/-- Use supplied or local successful invocation evidence at the fetched call. A local fact that + starts from another memory variable than the call is not tried, so the results of earlier + calls in scope cost no step each. -/ def cilContractChild (methodFound instructionFound : Term) (facts : Array Term) : TacticM Unit := do let mut candidates := facts + let current := ((← getMainTarget).find? (·.isAppOfArity ``CIL.Safety.run 8)).bind + (·.getAppArgs[7]?) for declaration in ← getLCtx do unless declaration.isImplementationDetail do if cilInvocationEvidence declaration.type then - candidates := candidates.push (← Term.exprToSyntax declaration.toExpr) + let type ← instantiateMVars declaration.type + let starts ← match current, cilInvocationMemory? type with + | some current, some start => cilStartsAt current start + | _, _ => pure true + if starts then candidates := candidates.push (← Term.exprToSyntax declaration.toExpr) for candidate in candidates do let saved ← saveState try @@ -832,14 +916,9 @@ open Lean Meta in `called`, `output` and `call`, or with the first suffix `_1`, `_2` that leaves all three unused, so one call's names share a suffix. -/ def cilCallNames : MetaM (Ident × Ident × Ident) := do - let context ← getLCtx - let named (index : Nat) (base : String) : Name := - Name.mkSimple (if index == 0 then base else s!"{base}_{index}") - let mut index := 0 - while ["called", "output", "call"].any fun base => context.usesUserName (named index base) do - index := index + 1 - return (mkIdent (named index "called"), mkIdent (named index "output"), - mkIdent (named index "call")) + let index := firstUnusedSuffix (← getLCtx) #["called", "output", "call"] + return (mkIdent (suffixedName index "called"), mkIdent (suffixedName index "output"), + mkIdent (suffixedName index "call")) open Lean Elab Tactic Meta in /-- Apply a matching contract and expose its `CallResult` to the caller continuation. -/ @@ -872,7 +951,7 @@ def cilContractApply (methodFound instructionFound : Term) (facts : Array Term) let siblings := (← getGoals).tail let tag ← (← getMainGoal).getTag evalTactic (← `(tactic| ( - cil_strict refine CIL.Safety.run_call_of_contract $proofSyntax $inputSyntax + cil_strict cil_refine_by CIL.Safety.run_call_of_contract $proofSyntax $inputSyntax $methodFound $instructionFound (by cil_checked_step_normalized [$facts,*]) (by rfl) ?_ ?_))) for parameter in parameters do @@ -916,19 +995,21 @@ elab_rules : tactic withMainContext <| evalTactic (← `(tactic| cil_contract_step_normalized [$[$prepared:term],*])) | `(tactic| cil_contract_step_normalized [$facts:term,*]) => withMainContext do - let (methodFound, instructionFound, operation) ← + -- The lookups read the supplied facts, so they run on the record of those facts. + let (methodFound, instructionFound, operation) ← withNormalNodes facts.getElems <| cilFetchedOp ``CIL.Safety.run_next_exists some 4 facts.getElems let currentFacts := facts.getElems ++ (← cilOutputFacts operation) let facts := currentFacts - if callsCallee operation then + withNormalNodes currentFacts do + if callsCallee operation && !(← unlinkedModelSite operation instructionFound facts) then let saved ← saveState try cilContractApply methodFound instructionFound facts catch _ => saved.restore cilContractChild methodFound instructionFound facts return - if operation == ``CIL.Op.ret then - evalTactic (← `(tactic| cil_strict refine CIL.Safety.run_return_exists + if operation == ``CIL.Instruction.ret then + evalTactic (← `(tactic| cil_strict cil_refine_by CIL.Safety.run_return_exists $methodFound $instructionFound (by cil_checked_step_normalized [$[$facts:term],*]) (by cil_checked_side [$[$facts:term],*]) ?_)) @@ -939,11 +1020,12 @@ elab_rules : tactic return let advance (facts : Array Term) := do evalTactic (← `(tactic| ( - cil_strict refine CIL.Safety.run_next_exists (post := _) + cil_strict cil_refine_by CIL.Safety.run_next_exists (post := _) $methodFound $instructionFound (by cil_checked_step_normalized [$[$facts:term],*]) ?_ cil_stack_cleanup))) - unless [``CIL.Op.store64, ``CIL.Op.storeScalar, ``CIL.Op.setFieldAt].contains operation do + unless [``CIL.Instruction.stind, ``CIL.Instruction.stobj, + ``CIL.Instruction.stfld].contains operation do advance facts return let saved ← saveState @@ -961,13 +1043,8 @@ elab_rules : tactic if prepared.size == facts.size then throw error advance prepared -/-- Step a goal `∃ fuel result returned, run ... ∧ post` through checked instructions, - composing calls with registered contracts or child evidence. Each composed call names its - final memory `called`, its typed output `output` and its `CallResult` `call`, all three - with the suffix `_1`, `_2` once a name is taken. Unmatched calls and the postcondition at - each return remain explicit goals, a callee's precondition tagged `precondition` and the - postcondition `postcondition`. With `CallingFields`, an output-word store can obtain its - write and current-memory argument facts from `ValidCall`, after trying supplied facts. -/ +/-- Contract stepping executes runtime models in the caller and composes managed calls with + contracts or child invocations. Unmatched calls and unproved conditions remain goals. -/ syntax "cil_contract_steps" "[" term,* "]" : tactic /-- As `cil_contract_steps`, naming the first call it composes: its final memory, typed output and `CallResult` take the three names given. Fails when it composes no call. -/ @@ -1039,20 +1116,22 @@ elab_rules : tactic let prepared ← prepareStepFacts facts.getElems withMainContext <| evalTactic (← `(tactic| ( cil_contract_prepare [$[$prepared:term],*] - cil_steps_from "cil_contract_steps took no step: the method or instruction lookup, or the \ - first step, did not close; pass the lookup hypothesis, feature facts or step facts" - cil_contract_step_normalized [$[$prepared:term],*]))) + cil_normal_run + cil_steps_from "cil_contract_steps took no step: the method or instruction lookup, or \ + the first step, did not close; pass the lookup hypothesis, feature facts or step facts" + cil_contract_step_normalized [$[$prepared:term],*]))) | `(tactic| cil_contract_steps $count:num [$facts:term,*]) => do let prepared ← prepareStepFacts facts.getElems withMainContext <| evalTactic (← `(tactic| cil_contract_prepare [$[$prepared:term],*])) let next ← `(tactic| cil_contract_step_normalized [$[$prepared:term],*]) - for _ in [:count.getNat] do - let saved ← saveState - try - evalTactic next - catch _ => - saved.restore - break + withNormalRun do + for _ in [:count.getNat] do + let saved ← saveState + try + evalTactic next + catch _ => + saved.restore + break | `(tactic| cil_contract_steps [$facts:term,*] until $continuation) => do let prepared ← prepareStepFacts facts.getElems let (direct, finish) ← cilContractContinuation continuation @@ -1060,9 +1139,10 @@ elab_rules : tactic | (cil_strict exact $direct) | ( cil_contract_prepare [$[$prepared:term],*] - repeat first - | (cil_strict $finish) - | cil_contract_step_normalized [$[$prepared:term],*])))) + cil_normal_run + repeat first + | (cil_strict $finish) + | cil_contract_step_normalized [$[$prepared:term],*])))) /-- Apply a proved contract at the fetched call, optionally checked against a `bind_call` site: leaves the `precondition`, then the continuation over the callee's final memory, diff --git a/CIL/Safety/MethodRenaming.lean b/CIL/Safety/MethodRenaming.lean index 5352b2e91..d80600043 100644 --- a/CIL/Safety/MethodRenaming.lean +++ b/CIL/Safety/MethodRenaming.lean @@ -22,7 +22,7 @@ structure MethodRenaming (source target : CIL.Method) where /-- Fetching an actual instruction selects its relocated target instruction at the same pc. -/ theorem MethodRenaming.instruction {source target : CIL.Method} - (related : MethodRenaming source target) {pc : Nat} {operation : CIL.Op} + (related : MethodRenaming source target) {pc : Nat} {operation : CIL.Instruction} (found : source.code[pc]? = some operation) : target.code[pc]? = some (related.indices.operation operation) := by rw [related.code, List.getElem?_map, found] diff --git a/CIL/Safety/MissingFacts.lean b/CIL/Safety/MissingFacts.lean index cc5da6f9d..125a2357d 100644 --- a/CIL/Safety/MissingFacts.lean +++ b/CIL/Safety/MissingFacts.lean @@ -146,6 +146,55 @@ def missingFactHint (residual : Array Expr) : MessageData := Id.run do return m!"{fact.hint}. The unresolved operation is{indentExpr operation}" return fact.hint +open Lean Elab Tactic Meta in +/-- The body, method, opcode and link fact of the step goal `step` when it steps a `call` or + `newobj` whose pc a fact shows has no link. Nothing for any other step. -/ +private def unlinkedSite? (step : MVarId) : TacticM (Option (Expr × Expr × Name × Expr)) := do + let some (_, action, _) := (← instantiateMVars (← step.getType)).eq? | return none + let some #[body, op, pc, _, _, _, _] := argumentsOf? action ``CIL.Safety.step 7 | return none + let some (method, opcode) := (match argumentsOf? op ``CIL.Instruction.call 2, + argumentsOf? op ``CIL.Instruction.newobj 1 with + | some #[method, _], _ => some (method, ``CIL.CallOpcode.call) + | _, some #[method] => some (method, ``CIL.CallOpcode.newobj) + | _, _ => none) | return none + let some link ← stepLinkFact? body pc | return none + unless ← statesNone link do return none + return some (body, method, opcode, link) + +open Lean Elab Tactic Meta in +/-- Whether the step goal `step` steps a call or construction that has neither a link nor a + runtime model, so no step crosses it. -/ +def unmodelledCall (step : MVarId) : TacticM Bool := do + let some (body, method, opcode, _) ← unlinkedSite? step | return false + let some model ← stepModelFact? body method opcode | return false + statesNone model + +open Lean Elab Tactic Meta in +/-- The step goal `step` of a `call` or `newobj` whose pc has no link, rewritten to the step of + its method's runtime model, as a memory operation where the model is one, so its residual + shows what the model reads. Nothing when the site is linked or no fact decides its link or + model. -/ +def modelledCallStep? (step : MVarId) : TacticM (Option MVarId) := do + let some (body, method, opcode, link) ← unlinkedSite? step | return none + let some model ← stepModelFact? body method opcode | return none + let saved ← saveState + try + let remaining ← step.withContext do + let link ← Term.exprToSyntax link + let model ← Term.exprToSyntax model + let rewrite ← if opcode == ``CIL.CallOpcode.call then + `(tactic| rewrite [CIL.Safety.step_call_model _ _ _ _ _ $link $model rfl]) + else + `(tactic| first + | rewrite [CIL.Safety.step_newobj_memory _ _ _ _ _ $link $model rfl] + | rewrite [CIL.Safety.step_newobj_model _ _ _ _ _ $link $model]) + Tactic.run step <| evalTactic rewrite + let [goal] := remaining | saved.restore; return none + return some goal + catch _ => + saved.restore + return none + set_option hygiene false in open Lean Elab Tactic Meta in /-- The step goal `step` rewritten as far as the step equations and `prepared` go: the goals left, diff --git a/CIL/Safety/NativeMethodTableRenaming.lean b/CIL/Safety/NativeMethodTableRenaming.lean index aa14ea7f6..9f5956134 100644 --- a/CIL/Safety/NativeMethodTableRenaming.lean +++ b/CIL/Safety/NativeMethodTableRenaming.lean @@ -19,7 +19,8 @@ theorem Memory.Renamed.nativeTableCheck {mapping : AllocationRenaming} (bound : table.origin.allocation < mapping.sourceLimit) (sameAllocation : table.cursor.allocation = table.origin.allocation) : (mapping.nativeTable table).check target = table.check source := by - rcases table with ⟨signature, entries, ⟨originId, originOffset⟩, ⟨cursorId, cursorOffset⟩⟩ + rcases table with ⟨signature, entries, ⟨originId, originOffset⟩, ⟨cursorId, cursorOffset⟩, count, + counted⟩ dsimp only at bound sameAllocation subst cursorId have allocations := related.allocations originId bound @@ -95,14 +96,12 @@ theorem Memory.Renamed.nativeTableView_success {mapping : AllocationRenaming} (viewed : nativeTableView table source = .ok view) : nativeTableView table target = .ok (mapping.nativeTable view) := by unfold nativeTableView at viewed ⊢ - cases targets : table.targets with - | none => simp [targets] at viewed - | some entries => - simp only [targets] at viewed ⊢ - cases backingFound : table.backing with - | none => simp [backingFound] at viewed + rcases table with ⟨signature, _ | entries, storage, count, counted⟩ + · simp at viewed + · cases storage with + | none => simp at viewed | some backing => - simp only [backingFound] at viewed ⊢ + dsimp only at viewed ⊢ cases found : source.staticBindings.find? (·.1 == backing.identity) with | none => simp [found] at viewed | some binding => @@ -112,8 +111,8 @@ theorem Memory.Renamed.nativeTableView_success {mapping : AllocationRenaming} rw [related.staticBindings] simpa only [List.find?_map, Function.comp_def, found, Option.map_some] simp only [found, targetFound] at viewed ⊢ - let original : NativeMethodTable := ⟨table.signature, entries, origin, - { origin with offset := origin.offset + backing.byteOffset }⟩ + let original : NativeMethodTable := ⟨signature, entries, origin, + { origin with offset := origin.offset + backing.byteOffset }, count, counted entries rfl⟩ change (original.check source >>= fun _ => pure original) = .ok view at viewed cases checked : original.check source with | error fault => simp [checked, Bind.bind, Except.bind] at viewed diff --git a/CIL/Safety/NativeMethodTables.lean b/CIL/Safety/NativeMethodTables.lean index 050b2719e..e80ebedce 100644 --- a/CIL/Safety/NativeMethodTables.lean +++ b/CIL/Safety/NativeMethodTables.lean @@ -18,6 +18,11 @@ structure NativeMethodTable where targets : Array CIL.ManagedIdentity origin : Reference cursor : Reference + /-- The number of entries, which the geometry checks and loads read instead of walking + `targets`. -/ + entryCount : Nat + /-- The entries number exactly `entryCount`. -/ + counted : targets.size = entryCount deriving DecidableEq, Repr /-- Every admitted runtime profile uses eight-byte native method addresses. -/ @@ -28,14 +33,14 @@ def nativeMethodWidth : Nat := 8 def NativeMethodTable.check (table : NativeMethodTable) (memory : Memory) : Checked Unit := do let allocation ← liveAllocation memory table.origin.allocation if allocation.kind != .immutableStatic then .error .unsupportedLayout else pure () - if table.targets.size >= 2^31 || - table.origin.offset + nativeMethodWidth * table.targets.size > allocation.layout.size then + if table.entryCount >= 2^31 || + table.origin.offset + nativeMethodWidth * table.entryCount > allocation.layout.size then .error .outsideAllocation else pure () if allocation.layout.alignment % nativeMethodWidth != 0 || table.origin.offset % nativeMethodWidth != 0 then .error .alignment else pure () if table.cursor.allocation != table.origin.allocation || table.cursor.offset < table.origin.offset || - table.cursor.offset > table.origin.offset + nativeMethodWidth * table.targets.size then + table.cursor.offset > table.origin.offset + nativeMethodWidth * table.entryCount then .error .invalidReference else pure () /-- Native byte addition preserves the supplied table and checks the resulting cursor; @@ -57,9 +62,11 @@ def NativeMethodTable.load (table : NativeMethodTable) (memory : Memory) : let relative := table.cursor.offset - table.origin.offset if relative % nativeMethodWidth != 0 then .error .alignment else pure () let index := relative / nativeMethodWidth - let some identity := table.targets[index]? | .error .outsideAllocation - let _ ← read memory table.cursor nativeMethodWidth nativeMethodWidth - return ⟨table.signature, some identity⟩ + if inside : index < table.entryCount then + let identity := table.targets[index]'(by rw [table.counted]; exact inside) + let _ ← read memory table.cursor nativeMethodWidth nativeMethodWidth + return ⟨table.signature, some identity⟩ + else .error .outsideAllocation /-- Moving the cursor never changes the admitted target identities or their signature. -/ theorem NativeMethodTable.addBytes_provenance (table result : NativeMethodTable) @@ -88,8 +95,7 @@ theorem NativeMethodTable.load_provenance (table : NativeMethodTable) (memory : split at loaded <;> (try simp_all) split at loaded <;> (try simp_all) subst pointer - refine ⟨rfl, ⟨_, rfl, ?_⟩⟩ - exact Array.mem_of_getElem? (by assumption) + exact ⟨rfl, ⟨_, rfl, Array.getElem_mem _⟩⟩ #print axioms NativeMethodTable.load_provenance @@ -97,14 +103,17 @@ theorem NativeMethodTable.load_provenance (table : NativeMethodTable) (memory : Bindings are transported by the existing memory renaming relation. -/ def nativeTableView (table : CIL.MethodTable) (memory : Memory) : Checked NativeMethodTable := do - let some targets := table.targets | .error .nullDereference - let some backing := table.backing | .error .invalidReference - let some (_, origin) := memory.staticBindings.find? (·.1 == backing.identity) | - .error .missingAllocation - let view : NativeMethodTable := ⟨table.signature, targets, origin, - { origin with offset := origin.offset + backing.byteOffset }⟩ - view.check memory - return view + match found : table.targets with + | none => .error .nullDereference + | some targets => + let some backing := table.backing | .error .invalidReference + let some (_, origin) := memory.staticBindings.find? (·.1 == backing.identity) | + .error .missingAllocation + let view : NativeMethodTable := ⟨table.signature, targets, origin, + { origin with offset := origin.offset + backing.byteOffset }, table.entryCount, + table.counted targets found⟩ + view.check memory + return view /-- Move an opaque native table cursor without changing its identities or storage binding. -/ def addNativeTable (table : CIL.MethodTable) (memory : Memory) (offset : BitVec 64) : diff --git a/CIL/Safety/NegationReturn.lean b/CIL/Safety/NegationReturn.lean index d1812daf3..74677afa4 100644 --- a/CIL/Safety/NegationReturn.lean +++ b/CIL/Safety/NegationReturn.lean @@ -12,8 +12,8 @@ namespace CIL.Safety /-- Execute Boolean negation from its actual constant/equality/return suffix. -/ theorem run_negation_return (program : CIL.Program) (method pc : Nat) (body : CIL.Method) (lookup : program[method]? = some body) - (constant : body.code[pc]? = some (.const32 0)) - (equality : body.code[pc + 1]? = some .eq) + (constant : body.code[pc]? = some (.ldc_i4 0)) + (equality : body.code[pc + 1]? = some .ceq) (returned : body.code[pc + 1 + 1]? = some .ret) (returns : body.returnsValue = true) (args : List Value) (frame : Frame) (memory : Memory) (flag : BitVec 32) : @@ -21,9 +21,11 @@ theorem run_negation_return (program : CIL.Program) (method pc : Nat) (body : CI .ok (leaveFrame frame memory, [.scalar (.i32 (if flag = 0 then 1 else 0))]) := by rw [run_next lookup constant (by rfl), run_next lookup equality (by rfl)] apply run_return lookup returned - · change (if 1 = (if body.returnsValue then 1 else 0) then _ else _) = _ - rw [returns] - rfl + · cases narrow : body.returnNarrow <;> + simp [step_ret_eq, narrow, returnFrom, returns, unspecifiedOperands, + Value.hasUnspecified, CIL.Value.hasUnspecified, + CIL.NarrowKind.extend_ite_ofNat_one_zero, + checkedValue, numericValue, Pure.pure, Except.pure, Bind.bind, Except.bind] · rfl #print axioms run_negation_return end CIL.Safety diff --git a/CIL/Safety/NormalNodes.lean b/CIL/Safety/NormalNodes.lean new file mode 100644 index 000000000..e11b3300a --- /dev/null +++ b/CIL/Safety/NormalNodes.lean @@ -0,0 +1,691 @@ +import Lean + +/-! +# Normal-form record for stepping runs + +A stepping run simplifies its goal after every instruction, and the goal holds every value the +run has built so far. Simplifying the whole goal each time makes the cost of a step grow with the +program counter. This module keeps, for each simplification that a step runs, the terms that +simp visited and left unchanged (the entries of its cache whose result is the key) in earlier +steps of the same run, and `cil_simp_only` and `cil_try_simp_only` return such a term without +visiting it again. The literal-operand and local-result passes record the terms below which +their detectors find nothing, which depends on the term alone. The record also keeps the +simplification contexts of the run, which every step would otherwise rebuild, and a +discrimination tree of the equation hypotheses by their left sides, which the lookups of every +step read. + +Invariant for a tactic that reads the record: it runs inside `withNormalNodes`, on a goal whose +local context is the one the record was made for (`NormalNodes.usable` checks this), and the +result it records for a term depends only on the term, the simp set named by the call, the +configuration, and the facts that `withNormalNodes` keys the run on. The simplifications use +`steppingDischarge?`, which proves a side condition from the simp set and never from a +hypothesis, so the hypotheses outside the facts cannot change a result. A call with another +discharger, `contextual`, `+arith` or `at h`, and a call that follows a tactic of the same step +that adds, clears or reverts a hypothesis, must not use it. Every fact spliced into a +`cil_simp_only` call inside a step must be one of the facts `withNormalNodes` was given, because +the contexts the record caches are named by the call's text with macro scopes erased. The +options that change what simp does must stay fixed during a driver for the same reason. + +At the start of each step `withNormalNodes` keeps each shelf whose local context the step's +context extends without changing a hypothesis, and drops the others, so a driver that rewrites, +renames or clears a hypothesis, as a snapshot or loop-frame store that clears the facts it +supersedes does, cannot reach a stale record while a step that only adds hypotheses, as a call +does, keeps it. The equation index outlives a cleared hypothesis and skips its entries. A step +in a longer context elaborates its facts again, since a name may now refer to a new hypothesis. +The record lives in the environment, so a backtracked step forgets what it recorded. +`withNormalRun` opens it for one driver and restores the earlier record when the driver ends, +however it ends, `withNormalNodes` drops it when a step fails, and a step outside a driver keeps +none. `cil.stepping.record` turns the record off; a lookup without an equation index scans the +local context instead. +-/ + +namespace CIL.Safety + +open Lean Elab Tactic Meta + +/-- The identity of a local context: its size and its last declaration. A step that adds, + clears or replaces a hypothesis changes it. -/ +def contextId (context : LocalContext) : Nat × Option FVarId := + (context.numIndices, context.lastDecl.map (·.fvarId)) + +/-- Whether two declarations state the same hypothesis: name, user name, type and value. -/ +def sameDeclaration (left right : LocalDecl) : Bool := + left.fvarId == right.fvarId && left.userName == right.userName && left.type == right.type && + left.value? == right.value? + +/-- Whether `earlier` and `current` are one object in memory, which makes them equal. -/ +private unsafe def sameObjectUnsafe {α : Type} (earlier current : α) : Bool := + ptrAddrUnsafe earlier == ptrAddrUnsafe current + +/-- Whether `earlier` and `current` are one object; `false` only means they must be compared. -/ +@[implemented_by sameObjectUnsafe] +def sameObject {α : Type} (_earlier _current : α) : Bool := false + +/-- How many context positions the comparisons of `contextExtendedBy` and `contextKeeps` have + read. Tests read it to bound the whole-context comparisons of a step, which cost time but no + heartbeats. It lives in the environment, so a backtracked attempt's reads are forgotten. -/ +initialize contextReadsExtension : EnvExtension Nat ← + registerEnvExtension (pure 0) (asyncMode := .local) + +/-- The context positions compared so far in this environment. -/ +def contextReads : CoreM Nat := + return contextReadsExtension.getState (← getEnv) + +/-- Whether every position of `earlier` holds the same hypothesis in `current`, where `cleared` + says whether a hypothesis `current` no longer holds is allowed. Two contexts that are one + object are equal and read no position; other comparisons are counted in `contextReads`. -/ +def compareContexts (cleared : Bool) (earlier current : LocalContext) : CoreM Bool := do + if sameObject earlier current then return true + if current.numIndices < earlier.numIndices then return false + modifyEnv (contextReadsExtension.modifyState · (· + earlier.numIndices)) + for position in [:earlier.numIndices] do + match earlier.decls[position]!, current.decls[position]! with + | none, none => pure () + | some _, none => unless cleared do return false + | some old, some new => unless sameObject old new || sameDeclaration old new do return false + | none, some _ => return false + return true + +/-- Whether `current` holds every hypothesis of `earlier`, unchanged and in the same positions, + followed by any number of others. A step that rewrites a hypothesis in place, renames it or + clears it makes this false; a step that only adds hypotheses keeps it true. -/ +def contextExtendedBy (earlier current : LocalContext) : CoreM Bool := + compareContexts false earlier current + +/-- Whether every hypothesis of `earlier` that `current` still holds is unchanged and in the same + position, so that an index of `earlier`'s equations is right for `current` once the entries of + cleared hypotheses are skipped. A store that clears the facts it supersedes keeps it true. -/ +def contextKeeps (earlier current : LocalContext) : CoreM Bool := + compareContexts true earlier current + +/-- The left side of the equation `type` states, under any quantifiers, whose variables stay + loose. -/ +def equationLeft? : Expr → Option Expr + | .forallE _ _ body _ => equationLeft? body + | .mdata _ body => equationLeft? body + | type => type.eq?.map fun (_, lhs, _) => lhs.consumeMData + +/-- The head of the left side of the equation `type` states, under any quantifiers. -/ +def equationHead? (type : Expr) : Option Expr := + (equationLeft? type).map (·.getAppFn) + +/-- `equationHead?` of `type` with its metavariables instantiated, instantiating only those on + the way to the head, so a lookup that reads every hypothesis does not rebuild their types. -/ +partial def instantiatedEquationHead? (type : Expr) : MetaM (Option Expr) := do + if !type.hasMVar then return equationHead? type + match type with + | .forallE _ _ body _ | .mdata _ body => instantiatedEquationHead? body + | _ => + if type.getAppFn.isMVar then return equationHead? (← instantiateMVars type) + let some (_, left, _) := type.eq? | return none + let head := left.consumeMData.getAppFn + if head.isMVar then return some (← instantiateMVars left).consumeMData.getAppFn + return some head + +/-- Whether `head` is a definition that reducible unfolding replaces at the head of a term. -/ +def headUnfolds (head : Expr) : CoreM Bool := do + let .const name _ := head | return false + if ← isReducible name then return !(← isProjectionFn name) + return false + +/-- Whether a hypothesis whose left side has head `found` may state a lookup whose left side has + head `wanted`, where `wantedUnfolds` is `headUnfolds wanted`: unless both are constants, + other ones, and neither is a definition that reducible unfolding replaces. -/ +def headsMayMeet (wanted : Expr) (wantedUnfolds : Bool) (found : Expr) : CoreM Bool := do + let (.const wantedName _, .const foundName _) := (wanted, found) | return true + if wantedName == foundName || wantedUnfolds then return true + headUnfolds found + +/-- The functions on natural numbers that unification evaluates on literals. -/ +def naturalEvaluations : List Name := + [``Nat.add, ``Nat.sub, ``Nat.mul, ``Nat.div, ``Nat.mod, ``Nat.pow, ``Nat.succ, ``Nat.pred] + +/-- The operator classes whose instances on natural numbers unification evaluates on literals. -/ +def naturalOperators : List Name := + [``HAdd.hAdd, ``HSub.hSub, ``HMul.hMul, ``HDiv.hDiv, ``HMod.hMod, ``HPow.hPow, ``Add.add, + ``Sub.sub, ``Mul.mul, ``Div.div, ``Mod.mod, ``Pow.pow, ``NatPow.pow, ``HomogeneousPow.pow] + +/-- Whether `type` is `Nat` once reduced, as the tree decides it for offsets. -/ +def naturalType (type : Expr) : MetaM Bool := + return (← whnf type).isConstOf ``Nat + +/-- Whether `term` is headed by an arithmetic function or operator that unification may + evaluate. -/ +def arithmeticHead (term : Expr) : Bool := + match term.getAppFn with + | .const name _ => naturalEvaluations.contains name || naturalOperators.contains name + | _ => false + +/-- `term` with each natural-number arithmetic on literals replaced by its value, which + unification equates it with, so that the tree keys it as that literal. -/ +def evaluateNaturals (term : Expr) : MetaM Expr := do + unless (term.find? arithmeticHead).isSome do return term + Meta.transform term (pre := fun part => do + unless arithmeticHead part do return .continue + let .const name _ := part.getAppFn | return .continue + let onNatural ← if naturalOperators.contains name && part.getAppNumArgs > 0 then + naturalType (part.getArg! 0) + else pure true + unless onNatural do return .continue + match ← (evalNat part).run with + | some value => return .done (mkNatLit value) + | none => return .continue) + +/-- Whether the tree keys `term` as a natural-number literal. -/ +partial def treeNumeral (term : Expr) : Bool := + term.isRawNatLit || term.isConstOf ``Nat.zero || + (term.isAppOfArity ``Nat.succ 1 && treeNumeral term.appArg!) || + (term.isAppOfArity ``OfNat.ofNat 3 && treeNumeral (term.getArg! 1)) + +/-- Whether the tree keys `term`, below the root, as a wildcard: a successor, or a sum on natural + numbers whose last operand is a numeral. -/ +def treeOffset (term : Expr) : MetaM Bool := do + if term.isAppOfArity ``Nat.succ 1 then return true + if term.isAppOfArity ``Nat.add 2 then return treeNumeral term.appArg! + if term.isAppOfArity ``Add.add 4 then + return treeNumeral term.appArg! && (← naturalType (term.getArg! 0)) + if term.isAppOfArity ``HAdd.hAdd 6 then + return treeNumeral term.appArg! && (← naturalType (term.getArg! 1)) + return false + +/-- Whether unification at reducible transparency, which `cil_lookup_assumption` uses, can equate + `term` only with terms whose discrimination-tree keys match its own, where `indexed` says + whether the tree keys `term` as an entry or as a lookup. It reads `term` as the tree does, + after the same reducible unfolding of let variables and definitions, and fails at what + unification decides beyond the keys: natural-number arithmetic it evaluates, except a sum the + tree keys as a wildcard in an entry, a structure constructor it matches against projections + (structure eta), and a value of a type with one value. Instance arguments and proofs, which + the tree does not key, are not read. -/ +partial def keyedExactly (indexed root : Bool) (term : Expr) : MetaM Bool := withReducible do + let term ← DiscrTree.reduceDT term root + unless root do + if treeNumeral term then return true + if indexed then if ← treeOffset term then return true + let type ← inferType term + -- The tree does not key a proof below the root, and unification does not compare proofs. + if !root then if ← isProp type then return true + let oneValue ← matchConstNonRecStructure (← whnf type).getAppFn + (fun _ => pure false) fun induct _ constructor => + return constructor.numFields == 0 && (← useEtaStruct induct.name) + if oneValue then return false + let function := term.getAppFn + if let .const name _ := function then + if naturalEvaluations.contains name then return false + if naturalOperators.contains name && term.getAppNumArgs > 0 then + if ← naturalType (term.getArg! 0) then return false + if let some (.ctorInfo constructor) := (← getEnv).find? name then + if isNonRecStructure (← getEnv) constructor.induct then return false + match function with + | .proj _ _ projected => unless ← keyedExactly indexed false projected do return false + | .forallE _ domain _ _ => return ← keyedExactly indexed false domain + | _ => pure () + let arguments := term.getAppArgs + let parameters := (← getFunInfoNArgs function arguments.size).paramInfo + for position in [:arguments.size] do + let argument := arguments[position]! + if parameters[position]?.any (·.isInstance) then continue + unless ← keyedExactly indexed false argument do return false + return true + +/-- An equation hypothesis: its position in the context, its name, and the head of its left + side. -/ +structure EquationEntry where + /-- Its position in the local context. -/ + position : Nat + /-- The hypothesis. -/ + hypothesis : FVarId + /-- The head of its left side, which `headsMayMeet` compares. -/ + head : Expr + deriving Inhabited + +/-- The hypotheses of a local context that state an equation, indexed by their left sides so that + a lookup reads only those that may unify with it at reducible transparency, as + `cil_lookup_assumption` unifies. Extending it for a longer context reads only the added + hypotheses. -/ +structure EquationIndex where + /-- How many declarations of the context it covers. -/ + covered : Nat := 0 + /-- Every equation hypothesis, in context order. -/ + all : Array EquationEntry := #[] + /-- The equation hypotheses whose left sides `keyedExactly` accepts, by their discrimination-tree + keys. -/ + tree : DiscrTree (Nat × FVarId) := {} + /-- The other equation hypotheses, which a lookup reads by the heads of their left sides. -/ + unkeyed : Array EquationEntry := #[] + +instance : Inhabited EquationIndex := ⟨{}⟩ + +/-- The index extended to the hypotheses of `context` that it does not cover. A quantified + variable of a hypothesis becomes a metavariable, which the tree keys as matching anything; a + metavariable the hypothesis itself holds may be assigned later, so that hypothesis is + unkeyed. -/ +def EquationIndex.extend (index : EquationIndex) (context : LocalContext) : + MetaM EquationIndex := do + if index.covered == context.numIndices then return index + withLCtx context (← getLocalInstances) do + let mut index := index + for position in [index.covered:context.numIndices] do + let some declaration := context.decls[position]! | continue + if declaration.isImplementationDetail then continue + let type ← instantiateMVars declaration.type + let some left := equationLeft? type | continue + let entry : EquationEntry := ⟨declaration.index, declaration.fvarId, left.getAppFn⟩ + index := { index with all := index.all.push entry } + let keyed? ← if left.hasExprMVar then pure none else withoutModifyingState do + let (_, _, body) ← forallMetaTelescope type + let some (_, left, _) := body.consumeMData.eq? | return none + try + let left ← evaluateNaturals left + unless ← keyedExactly true true left do return none + return some (← index.tree.insert left (entry.position, entry.hypothesis)) + catch _ => return none + index := match keyed? with + | some tree => { index with tree } + | none => { index with unkeyed := index.unkeyed.push entry } + return { index with covered := context.numIndices } + +/-- The index rebuilt for `context` once more than half of its entries name hypotheses that + `context` no longer holds, as after stores that clear the facts they supersede; the tree + cannot drop an entry, so a walk would otherwise read every cleared one at each lookup. -/ +def EquationIndex.prune (index : EquationIndex) (context : LocalContext) : + MetaM EquationIndex := do + let live := index.all.foldl (init := 0) fun count entry => + if context.contains entry.hypothesis then count + 1 else count + if 2 * live >= index.all.size then return index + ({} : EquationIndex).extend context + +/-- The equation hypotheses that may state the goal `goal`, in context order: those the tree finds + for its left side with the unkeyed ones whose head `headsMayMeet` the goal's, or every one + whose head does when `keyedExactly` rejects the left side or the goal is quantified. -/ +def EquationIndex.candidates (index : EquationIndex) (goal : Expr) : MetaM (Array FVarId) := do + let some wanted := equationHead? goal | return #[] + let wantedUnfolds ← headUnfolds wanted + let meeting (entries : Array EquationEntry) : MetaM (Array (Nat × FVarId)) := + entries.filterMapM fun entry => do + if ← headsMayMeet wanted wantedUnfolds entry.head then + return some (entry.position, entry.hypothesis) + return none + let keyed? ← withoutModifyingState do + let some (_, lookup, _) := goal.consumeMData.eq? | return none + try + let lookup ← evaluateNaturals lookup + unless ← keyedExactly false true lookup do return none + return some (← index.tree.getUnify lookup) + catch _ => return none + let some keyed := keyed? | return (← meeting index.all).map (·.2) + return ((keyed ++ (← meeting index.unkeyed)).qsort (·.1 < ·.1)).map (·.2) + +/-- What a stepping run has established for one local context and one set of facts. -/ +structure NormalShelf where + /-- The facts of the step that made the shelf, as elaborated; see `factsKey`. -/ + key : Option (Array Expr) := none + /-- The identity of the local context the shelf was last used in. -/ + context : Nat × Option FVarId := (0, none) + /-- The local context the shelf was last used in. -/ + locals : LocalContext := {} + /-- The syntax of the facts that `key` was built from. -/ + factsSeen : Array Syntax := #[] + /-- How many nodes are recorded. -/ + count : Nat := 0 + /-- For each simplification, by name, the terms it visited and left unchanged. -/ + nodes : PersistentHashMap String (PersistentHashSet Expr) := {} + /-- The simplification contexts the run built, by the arguments that name them. -/ + contexts : Array (String × Simp.Context × Simp.SimprocsArray) := #[] + +instance : Inhabited NormalShelf := ⟨{}⟩ + +/-- What a stepping driver has established: the shelf its current step reads and writes, and the + shelves of other facts it used in the same local context, so a walk whose steps read + different groups of facts keeps one shelf for each. -/ +structure NormalNodes extends NormalShelf where + /-- Whether a driver opened this record with `withNormalRun`; a step outside a driver keeps + none, so a record never outlives its driver. -/ + driven : Bool := false + /-- Whether a step is running, so the simplifications consult the record. -/ + active : Bool := false + /-- How many steps the driver has run, counting those before the record is used. -/ + steps : Nat := 0 + /-- The shelves that are not current. -/ + others : Array NormalShelf := #[] + /-- The equation hypotheses of `equationLocals`, which every shelf shares. -/ + equations : EquationIndex := {} + /-- The local context `equations` was built for; none until a step of the driver builds it. -/ + equationLocals : Option LocalContext := none + +instance : Inhabited NormalNodes := ⟨{}⟩ + +/-- Most shelves a driver keeps. -/ +def normalShelfLimit : Nat := 8 + +/-- Holds the record of the running stepping driver. -/ +initialize normalNodesExtension : EnvExtension NormalNodes ← + registerEnvExtension (pure {}) (asyncMode := .local) + +/-- Most nodes the record keeps; a longer run starts a new record. -/ +def normalNodeLimit : Nat := 2000000 + +/-- The record of the running stepping driver. -/ +def getNormalNodes : CoreM NormalNodes := + return normalNodesExtension.getState (← getEnv) + +/-- Replace the record of the running stepping driver. -/ +def setNormalNodes (record : NormalNodes) : CoreM Unit := + modifyEnv fun env => normalNodesExtension.setState env record + +/-- Whether `record` may be consulted for a goal with local context `context`: a step is running + and the record was made for this context. A record from another goal or declaration is + never used. -/ +def NormalNodes.usable (record : NormalNodes) (context : LocalContext) : CoreM Bool := do + if !record.active || record.context != contextId context then return false + contextExtendedBy record.locals context + +/-- The equation index of `record` for `context`: the one it holds, extended, when `context` still + has every hypothesis it indexed unchanged, and built again otherwise. -/ +def NormalNodes.equationsFor (record : NormalNodes) (context : LocalContext) : + MetaM EquationIndex := do + let known ← match record.equationLocals with + | some locals => pure (if ← contextKeeps locals context then record.equations else {}) + | none => pure {} + (← known.prune context).extend context + +/-- Whether stepping drivers use the record. Turning it off makes every simplification visit the + whole goal, which leaves the same goals and costs more per step the longer the run. -/ +register_option cil.stepping.record : Bool := { + defValue := true + descr := "Let stepping tactics skip terms that earlier steps of the run left unchanged." +} + +/-- How many steps of a driver run before the record is used. The goal of a short run is small, so + the whole-goal simplification costs less than keeping the record. -/ +register_option cil.stepping.warmup : Nat := { + defValue := 12 + descr := "Steps of a stepping driver that run before it starts to use the record." +} + +/-- The record, when it may be consulted for the current local context. -/ +def usableNormalNodes : MetaM (Option NormalNodes) := do + unless cil.stepping.record.get (← getOptions) do return none + let record ← getNormalNodes + return if ← record.usable (← getLCtx) then some record else none + +/-- The terms the simplification `name` visited and left unchanged in earlier steps. -/ +def NormalNodes.normalFor (record : NormalNodes) (name : String) : PersistentHashSet Expr := + (record.nodes.find? name).getD {} + +/-- The terms the simplification `name` may skip now; none when the record is not usable. -/ +def normalNodesFor (name : String) : MetaM (PersistentHashSet Expr) := do + return (← usableNormalNodes).map (·.normalFor name) |>.getD {} + +/-- Add `fresh` to the terms the simplification `name` leaves alone, when the record is usable. + Terms with metavariables or loose bound variables are not recorded: a later assignment or + binder instance would change what they mean. Neither are terms with a free variable outside + the local context, such as the temporary variables simp opens under a binder, which can + never occur again. -/ +def recordNormalNodes (name : String) (fresh : Array Expr) : MetaM Unit := do + let some record ← usableNormalNodes | return + let context ← getLCtx + let fresh := fresh.filter fun expr => !expr.hasMVar && !expr.hasLooseBVars && + !(expr.hasFVar && expr.hasAnyFVar fun free => !context.contains free) + if fresh.isEmpty then return + let known := fresh.foldl (fun nodes expr => nodes.insert expr) (record.normalFor name) + let nodes := record.nodes.insert name known + setNormalNodes { record with nodes, count := record.count + fresh.size } + +/-- Walks `expr` as a DAG, each term once. Returns whether some term satisfies `flagged`, and + records in the state every term below which none does. Terms in `known` are clean and are + not entered. -/ +partial def cleanWalk (flagged : Expr → Bool) (known : PersistentHashSet Expr) (expr : Expr) : + StateM (Std.HashMap Expr Bool × Array Expr) Bool := do + if known.contains expr then return false + if let some seen := (← get).1[expr]? then return seen + let children : List Expr := match expr with + | .app function argument => [function, argument] + | .mdata _ body | .proj _ _ body => [body] + | .lam _ domain body _ | .forallE _ domain body _ => [domain, body] + | .letE _ type value body _ => [type, value, body] + | _ => [] + let mut found := flagged expr + for child in children do + if ← cleanWalk flagged known child then found := true + modify fun (seen, clean) => + (seen.insert expr found, if found || expr.hasLooseBVars then clean else clean.push expr) + return found + +initialize registerTraceClass `CIL.record + +/-- The key of the facts a step is given: what the facts state and prove, not how they are + written, so two runs whose prepared facts print alike but differ are told apart. A fact + that elaborates with metavariables, as a lemma with implicit arguments or universe + parameters does, is keyed by its abstraction over them, which is the same term each time. + `none` when a fact does not elaborate; the step then runs without the record and + `trace.CIL.record` says which fact. -/ +def factsKey (facts : Array Term) : TacticM (Option (Array Expr)) := do + let mut key : Array Expr := #[] + for fact in facts do + -- A hypothesis named in the context is keyed by itself and its type, with no elaboration. + if fact.raw.isIdent then + if let some declaration := (← getLCtx).findFromUserName? fact.raw.getId then + key := key.push declaration.toExpr |>.push (← abstractMVars declaration.type).expr + continue + let saved ← saveState + let element? : Except MessageData Expr ← try + let proof ← Term.withoutErrToSorry <| Term.elabTerm fact none + Term.synthesizeSyntheticMVarsNoPostponing + pure (.ok (← abstractMVars (← instantiateMVars proof)).expr) + catch error => pure (.error error.toMessageData) + saved.restore + match element? with + | .ok element => key := key.push element + | .error message => + trace[CIL.record] "the step runs without the record, since fact {fact} does not \ + elaborate: {message}" + return none + return some key + +/-- Starts a driver's record, empty, and returns the earlier record for `leaveNormalRun`. A driver + that mutates its own variables calls these around a `try ... finally`; any other uses + `withNormalRun`. -/ +def enterNormalRun : TacticM NormalNodes := do + let outer ← getNormalNodes + setNormalNodes { driven := true } + return outer + +/-- Restores the record that was current before `enterNormalRun`. -/ +def leaveNormalRun (outer : NormalNodes) : TacticM Unit := + setNormalNodes outer + +/-- Runs a driver with an empty record and restores the earlier record when the driver ends, + whether it succeeds or fails. -/ +def withNormalRun {α : Type} (driver : TacticM α) : TacticM α := do + let outer ← enterNormalRun + try driver finally leaveNormalRun outer + +/-- Runs the tactics with `withNormalRun`, so a driver written as a macro scopes the record. -/ +elab "cil_normal_run " steps:tacticSeq : tactic => withNormalRun (evalTactic steps) + +/-- Runs one stepping step so that its simplifications leave alone what earlier steps of the + same driver visited and left unchanged. The record is kept while the facts and the local + context stay the same and dropped when they change, and a driver that alternates between + several facts keeps a shelf for each; a step that fails drops them, and the record is never + left active. A step nested in another with different facts runs on its own shelf, and the + outer record is restored after it. A step outside a driver, which `withNormalRun` has not + opened, runs plainly. -/ +def withNormalNodes {α : Type} (facts : Array Term) (step : TacticM α) : TacticM α := do + let outer ← getNormalNodes + if !outer.driven || !cil.stepping.record.get (← getOptions) then return ← step + let noGoals := (← getGoals).isEmpty + let localContext ← if noGoals then getLCtx else withMainContext getLCtx + -- Looking the equation hypotheses up is a failed attempt's work, which a backtrack discards, + -- so every step of a driver starts with them, those before the record is used included. + let equations ← outer.equationsFor localContext + if outer.steps < cil.stepping.warmup.get (← getOptions) then + setNormalNodes { outer with + steps := outer.steps + 1, equations, equationLocals := some localContext } + return ← step + let context := contextId localContext + -- A shelf stays valid while the hypotheses it was made for are unchanged, so a step that only + -- adds hypotheses keeps it; one that rewrites, renames or clears a hypothesis does not. + let kept (shelf : NormalShelf) : TacticM Bool := contextExtendedBy shelf.locals localContext + let currentKept ← kept outer.toNormalShelf + let shelves := (if currentKept then #[outer.toNormalShelf] else #[]) ++ + (← outer.others.filterM kept) + -- The same syntax in the same context states the same facts, so a walk elaborates them once; + -- in a longer context a name may refer to a new hypothesis, so the facts are elaborated again. + let raw := facts.map (·.raw) + let mut index := shelves.findIdx? fun shelf => + shelf.key.isSome && shelf.factsSeen == raw && shelf.context == context + let mut key := index.bind fun position => shelves[position]!.key + if key.isNone then + key ← if noGoals then factsKey facts else withMainContext (factsKey facts) + let some elaborated := key | return ← step + index := shelves.findIdx? (·.key == some elaborated) + let fresh : NormalShelf := { key, context, locals := localContext, factsSeen := raw } + let selected := match index with + | some position => if shelves[position]!.count < normalNodeLimit then shelves[position]! + else fresh + | none => fresh + let remaining := (shelves.eraseIdxIfInBounds (index.getD shelves.size)).extract 0 + (normalShelfLimit - 1) + let shared := currentKept && index == some 0 && outer.count < normalNodeLimit + let current : NormalNodes := + { selected with + factsSeen := raw, driven := true, steps := outer.steps, others := remaining, + context := context, locals := localContext, equations := equations, + equationLocals := some localContext } + setNormalNodes { current with active := true } + -- The finalizer also runs for heartbeat and interrupt exceptions, which `catch` does not see. + let (result, _) ← tryFinally' step fun result? => do + if result?.isSome then + let after ← getNormalNodes + setNormalNodes (if outer.active && !shared then outer + else { after with active := outer.active }) + else + -- The equation index depends on the hypotheses alone, so it outlives a failed step. + setNormalNodes (if outer.active then outer else + { driven := true, steps := outer.steps, equations, + equationLocals := some localContext }) + return result + +/-- `simp only [arguments]` on the goal that leaves alone what the running stepping driver + already visited and left unchanged. It discharges side conditions with `steppingDischarge?`, + so a hypothesis outside `arguments` never proves one. -/ +syntax (name := cilSimpOnly) "cil_simp_only " " [" + withoutPosition((Lean.Parser.Tactic.simpStar <|> Lean.Parser.Tactic.simpErase <|> + Lean.Parser.Tactic.simpLemma),*,?) "]" : tactic + +/-- Simp's default discharger without its search of the hypotheses for a side condition of an + equation theorem. Each hypothesis tried costs a unification, so the default cost grows with + the hypotheses in scope; this one decides a side condition by the simp set alone, and the + result of a simplification does not depend on the hypotheses outside it. -/ +def steppingDischarge? (condition : Expr) : Simp.SimpM (Option Expr) := do + let condition := condition.cleanupAnnotations + if Simp.isEqnThmHypothesis condition then + if let some proof ← Simp.dischargeEqnThmHypothesis? condition then return some proof + let simplified ← Simp.simp condition + if let some proof ← Simp.dischargeRfl simplified.expr then + return some (mkApp4 (mkConst ``Eq.mpr [Level.zero]) condition simplified.expr + (← simplified.getProof) proof) + else if simplified.expr.isTrue then + return some (← mkOfEqTrue (← simplified.getProof)) + else + return none + +/-- Simplify `target` with `ctx`. When the record is usable, a term the simplification `name` + left unchanged in an earlier step is returned as it is, and the terms this call visited and + left unchanged are recorded. -/ +def simpLeavingAlone (name : String) (target : Expr) (ctx : Simp.Context) + (simprocs : Simp.SimprocsArray) : MetaM Simp.Result := do + let defaults := { Simp.mkDefaultMethodsCore simprocs with discharge? := steppingDischarge? } + let some record ← usableNormalNodes + | return (← Simp.mainCore target ctx {} defaults).1 + let known := record.normalFor name + let methods : Simp.Methods := { defaults with + pre := fun expr => + if known.contains expr then return .done { expr } else defaults.pre expr } + let (result, state) ← Simp.mainCore target ctx {} methods + let unchanged := state.cache.fold (init := #[]) fun found expr visit => + if visit.proof?.isNone && visit.cache && visit.expr == expr && !known.contains expr then + found.push expr + else found + recordNormalNodes name unchanged + return result + +/-- The arguments of a simp call as text, ignoring the macro scopes that each expansion adds. -/ +partial def simpArgumentsKey : Syntax → String + | .atom _ value => value + | .ident _ _ name _ => name.eraseMacroScopes.toString + | .node _ kind arguments => + s!"({kind}{arguments.foldl (fun text part => text ++ " " ++ simpArgumentsKey part) ""})" + | .missing => "_" + +/-- The simplification context for `simp only [arguments]`, built once per record: the lemmas + and facts are elaborated from syntax, which a step would otherwise repeat. -/ +def simpOnlyContext (key : String) + (arguments : Syntax.TSepArray [`Lean.Parser.Tactic.simpStar, `Lean.Parser.Tactic.simpErase, + `Lean.Parser.Tactic.simpLemma] ",") : TacticM (Simp.Context × Simp.SimprocsArray) := do + let usable ← usableNormalNodes + if let some record := usable then + if let some (_, ctx, simprocs) := record.contexts.find? (·.1 == key) then + return (ctx, simprocs) + let { ctx, simprocs, .. } ← + mkSimpContext (← `(tactic| simp only [$arguments,*])) (eraseLocal := false) + if usable.isSome then + let current ← getNormalNodes + setNormalNodes { current with contexts := current.contexts.push (key, ctx, simprocs) } + return (ctx, simprocs) + +/-- Simplify the goal as `simp only [arguments]` does, leaving alone what the running stepping + driver already visited and left unchanged. A simplification that changes nothing fails when + `required` and otherwise succeeds, so the terms it visited are still recorded. Returns + whether the goal changed. -/ +def cilSimp (required : Bool) + (arguments : Syntax.TSepArray [`Lean.Parser.Tactic.simpStar, `Lean.Parser.Tactic.simpErase, + `Lean.Parser.Tactic.simpLemma] ",") : TacticM Bool := withMainContext do + -- The key names the call for the record; a plain call has no use for it. + let key := if (← usableNormalNodes).isSome then + simpArgumentsKey (mkNullNode arguments.elemsAndSeps) + else "" + let (ctx, simprocs) ← simpOnlyContext key arguments + let goal ← getMainGoal + let target ← instantiateMVars (← goal.getType) + let result ← profileitM Exception "simp" (← getOptions) <| + simpLeavingAlone key target ctx simprocs + if result.expr.isTrue then + goal.assign (← match result.proof? with + | some proof => mkOfEqTrue proof + | none => pure (mkConst ``True.intro)) + replaceMainGoal [] + return true + else if result.proof?.isNone && result.expr == target then + if required then throwError "`simp` made no progress" + return false + else + replaceMainGoal [← applySimpResultToTarget goal target result] + return true + +/-- `cil_simp_only [arguments]` written as syntax, run so that it reports whether the goal + changed instead of failing when it did not. A failure would restore the state and drop what + the simplification visited, which the next step would visit again. -/ +def cilSimpProgress (call : TSyntax `tactic) : TacticM Bool := do + let `(tactic| cil_simp_only [$arguments,*]) := call | throwUnsupportedSyntax + cilSimp false arguments + +/-- Elaborates `cil_simp_only`. -/ +@[tactic cilSimpOnly] +def evalCilSimpOnly : Tactic := fun stx => do + let `(tactic| cil_simp_only [$arguments,*]) := stx | throwUnsupportedSyntax + discard <| cilSimp true arguments + +/-- `try cil_simp_only [arguments]`, which keeps what the simplification visited. -/ +syntax (name := cilTrySimpOnly) "cil_try_simp_only " " [" + withoutPosition((Lean.Parser.Tactic.simpStar <|> Lean.Parser.Tactic.simpErase <|> + Lean.Parser.Tactic.simpLemma),*,?) "]" : tactic + +/-- Elaborates `cil_try_simp_only`. -/ +@[tactic cilTrySimpOnly] +def evalCilTrySimpOnly : Tactic := fun stx => do + let `(tactic| cil_try_simp_only [$arguments,*]) := stx | throwUnsupportedSyntax + let saved ← saveState + try discard <| cilSimp false arguments catch _ => saved.restore + +end CIL.Safety diff --git a/CIL/Safety/NumericHomes.lean b/CIL/Safety/NumericHomes.lean index 82430709c..68f48b136 100644 --- a/CIL/Safety/NumericHomes.lean +++ b/CIL/Safety/NumericHomes.lean @@ -154,7 +154,7 @@ def numericSpecs (body : CIL.Method) : List NumericLocalSpec := theorem numeric_frame_setup (body : CIL.Method) (specs : List NumericLocalSpec) (kinds : body.localKinds = specs.map NumericLocalSpec.kind) (initializers : body.locals = specs.map NumericLocalSpec.value) - (arguments : body.vectorArgs = []) (parameters : body.parameterHomes = []) + (parameters : body.parameterHomes = []) (opaqueLocals : body.opaqueVectorLocals = []) (memory : Memory) (args : List Value) (wellFormed : memory.WellFormed) : ∃ frame result, @@ -164,10 +164,10 @@ theorem numeric_frame_setup (body : CIL.Method) (specs : List NumericLocalSpec) obtain ⟨slots, owned, result, made, homes⟩ := make_numeric_locals memory memory.nextIdentity specs wellFormed simp only [numericKinds, numericInitializers] at made - let frame : Frame := ⟨memory.nextIdentity, slots, owned, [], 0⟩ + let frame : Frame := ⟨memory.nextIdentity, slots, owned, 0⟩ have setup : enterFrame body args memory = .ok (frame, result) := by simp only [enterFrame, opaqueLocals, makeSelectedLocals, List.isEmpty_nil, ite_true, - kinds, initializers, made, arguments, makeArgumentHomes, parameters, makeParameterHomes, + kinds, initializers, made, parameters, makeParameterHomes, List.map_nil, Bind.bind, Except.bind, Pure.pure, Except.pure, List.append_nil] rfl diff --git a/CIL/Safety/NumericLocals.lean b/CIL/Safety/NumericLocals.lean index 6a7dcabc1..f3adb7c59 100644 --- a/CIL/Safety/NumericLocals.lean +++ b/CIL/Safety/NumericLocals.lean @@ -60,10 +60,10 @@ theorem step_load_numeric_local {body : CIL.Method} {pc index : Nat} {args stack (slot : frame.locals[index]? = some (.bytes kind reference)) (loaded : read memory reference (localWidth kind) 1 = .ok (numberBytes number (localWidth kind))) : - step body (.local index) pc args frame stack memory = + step body (.ldloc index) pc args frame stack memory = .ok (.next (pc + 1) (.scalar (localResult kind value) :: stack) frame memory) := by simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, - Bool.false_or, Bool.false_eq_true, ite_false, slot, + Bool.false_eq_true, ite_false, slot, load_numeric_local kind value number fits loaded, Bind.bind, Except.bind, Pure.pure, Except.pure] @@ -76,7 +76,7 @@ theorem step_store_numeric_local {body : CIL.Method} {pc index : Nat} {args rest (slot : frame.locals[index]? = some (.bytes kind reference)) (ready : access memory reference (localWidth kind) 1 true = .ok ()) : ∃ result, - step body (.setLocal index) pc args frame (.scalar value :: rest) memory = + step body (.stloc index) pc args frame (.scalar value :: rest) memory = .ok (.next (pc + 1) rest frame result) ∧ write memory reference (numberBytes number (localWidth kind)) 1 = .ok result ∧ read result reference (localWidth kind) 1 = .ok (numberBytes number (localWidth kind)) := by @@ -91,7 +91,7 @@ theorem step_store_numeric_local {body : CIL.Method} {pc index : Nat} {args rest · simp only [List.getElem?_set_ne same] refine ⟨result, ?_, written, readback⟩ simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, - Bool.false_or, Bool.false_eq_true, ite_false, slot, stored, unchanged, + Bool.false_eq_true, ite_false, slot, stored, unchanged, Bind.bind, Except.bind, Pure.pure, Except.pure] #print axioms make_numeric_local diff --git a/CIL/Safety/NumericStepRenaming.lean b/CIL/Safety/NumericStepRenaming.lean index 3995d6420..e1508b0f2 100644 --- a/CIL/Safety/NumericStepRenaming.lean +++ b/CIL/Safety/NumericStepRenaming.lean @@ -10,7 +10,7 @@ theorem Memory.Renamed.stepNumeric {indices : LocalIndexMap} {mapping : Allocati (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (formed : source.WellFormed) (sourceBody targetBody : CIL.Method) - (metadata : StepMetadata sourceBody targetBody) (operation : CIL.Op) + (metadata : StepMetadata sourceBody targetBody) (operation : CIL.Instruction) (family : fixedStepFamily operation = .numeric) (pc : Nat) (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (observable : SuccessOrThrow @@ -18,30 +18,32 @@ theorem Memory.Renamed.stepNumeric {indices : LocalIndexMap} {mapping : Allocati ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) (step sourceBody operation pc arguments sourceFrame stack source) (step targetBody operation pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by + targetFrame (stack.map (indices.value mapping)) target) := by allocation_fixed_step ( all_goals try simp only [List.length_map, ← List.map_take, ← List.map_drop, - ← List.map_reverse, List.map_cons, AllocationRenaming.value] + ← List.map_reverse] all_goals repeat first | exact .error _ | exact .ok (.next related frames) | (apply (ResultRenamed.arithmetic mapping _).bind intro first second same subst second) - | (apply (mapping.scalarOperands _).bind + | (apply (indices.scalarOperands mapping _).bind intro first second same subst second) | (apply (mapping.checkedIntrinsic _ _).bind intro first second same subst second) - | (apply (related.checkedValue _ (by trivial)).bind - intro first second same + | (apply (related.checkedValue _ (by trivial)).bindWithSuccess + intro first second checkedStep same subst second + rw [indices.push_of_not_home mapping _ (checkedValue_not_spanHome checkedStep)] exact .ok (.next related frames)) - | (apply (related.scalarChecks _).bind - intro first second same + | (apply (related.scalarChecks _).bindWithSuccess + intro first second checkedStep same subst second - simp only [← List.map_append] + rw [← indices.values_of_not_home mapping (checkedValues_not_spanHome checkedStep), + ← List.map_append] exact .ok (.next related frames)) | (split <;> try simp only [*, Bool.false_eq_true, ite_false]) skip) diff --git a/CIL/Safety/ObjectAllocationLemmas.lean b/CIL/Safety/ObjectAllocationLemmas.lean index 15d88a87d..6c65fe287 100644 --- a/CIL/Safety/ObjectAllocationLemmas.lean +++ b/CIL/Safety/ObjectAllocationLemmas.lean @@ -239,7 +239,7 @@ theorem newReferenceArray_insufficient (memory : Memory) (element : CIL.Extracti theorem newReferenceArray_negative (memory : Memory) (element : CIL.Extraction.TypeExpr) (bits : BitVec 32) (negative : bits.toInt < 0) : newReferenceArray memory element (.i32 bits) = - .error (.thrown "System.OverflowException") := by + .error (.thrown .overflow) := by simp [newReferenceArray, negative, Bind.bind, Except.bind, Pure.pure, Except.pure] #print axioms newReferenceArray_negative diff --git a/CIL/Safety/ObjectFields.lean b/CIL/Safety/ObjectFields.lean index ca01c2fb0..c2098f931 100644 --- a/CIL/Safety/ObjectFields.lean +++ b/CIL/Safety/ObjectFields.lean @@ -269,11 +269,15 @@ def address (field : WordField width) (object : Reference) : Reference := def entry (field : WordField width) (object : Reference) : Reference × Nat := (field.address object, width) -/-- `op` loads or stores this field: a scalar field operation of its class, offset and width. - An extraction checks each field it names against an operation of its own code with this. -/ -def accessedBy (field : WordField width) : CIL.Op → Bool - | .memory (.objectField owner offset size _) | .memory (.setObjectField owner offset size) => - owner == field.owner && offset == field.offset && size == width +/-- `op` loads or stores this field: `ldfld` or `stfld` of a field of its class that `body` + resolves to a word slot of its width at its offset. An extraction checks each field it names + against an instruction of its own code with this. -/ +def accessedBy (field : WordField width) (body : CIL.Method) : CIL.Instruction → Bool + | .ldfld reference | .stfld reference => + reference.owner == field.owner && + match body.fieldShape reference with + | .classSlot offset (.word size _) => offset == field.offset && size == width + | _ => false | _ => false @[simp] theorem address_allocation (field : WordField width) (object : Reference) : @@ -640,12 +644,15 @@ def address (field : ReferenceField) (object : Reference) : Reference := def entry (field : ReferenceField) (object : Reference) : Reference × Nat := (field.address object, objectFieldSlot) -/-- `op` loads or stores this field: a reference field operation of its class and offset. An - extraction checks each field it names against an operation of its own code with this. -/ -def accessedBy (field : ReferenceField) : CIL.Op → Bool - | .memory (.objectReferenceField owner offset) - | .memory (.setObjectReferenceField owner offset) => - owner == field.owner && offset == field.offset +/-- `op` loads or stores this field: `ldfld` or `stfld` of a field of its class that `body` + resolves to a reference slot at its offset. An extraction checks each field it names against + an instruction of its own code with this. -/ +def accessedBy (field : ReferenceField) (body : CIL.Method) : CIL.Instruction → Bool + | .ldfld reference | .stfld reference => + reference.owner == field.owner && + match body.fieldShape reference with + | .classSlot offset .reference => offset == field.offset + | _ => false | _ => false @[simp] theorem address_allocation (field : ReferenceField) (object : Reference) : @@ -987,11 +994,16 @@ def address (field : ArrayField element) (object : Reference) : Reference := def entry (field : ArrayField element) (object : Reference) : Reference × Nat := (field.address object, objectFieldSlot) -/-- `op` loads or stores this field: a value-array field operation of its class, offset and - element. An extraction checks each field it names against an operation of its own code. -/ -def accessedBy (field : ArrayField element) : CIL.Op → Bool - | .memory (.arrayField owner offset held) | .memory (.setArrayField owner offset held) => - owner == field.owner && offset == field.offset && held == element +/-- `op` loads or stores this field: `ldfld` or `stfld` of a field of its class that `body` + resolves to a slot at its offset holding an array of its element, a struct element sized by + the checked layouts. An extraction checks each field it names against an instruction of its + own code. -/ +def accessedBy (field : ArrayField element) (body : CIL.Method) : CIL.Instruction → Bool + | .ldfld reference | .stfld reference => + reference.owner == field.owner && + match body.fieldShape reference with + | .classSlot offset (.array held) => offset == field.offset && held == element + | _ => false | _ => false /-- The element of the arrays the field holds, so a statement can name it through the field. -/ diff --git a/CIL/Safety/ObjectSteps.lean b/CIL/Safety/ObjectSteps.lean index 34532f641..1e3eef148 100644 --- a/CIL/Safety/ObjectSteps.lean +++ b/CIL/Safety/ObjectSteps.lean @@ -1,15 +1,17 @@ import CIL.Safety.StepComposition import CIL.Safety.ObjectAllocationLemmas +import CIL.Safety.InstructionSteps /-! # Object step equations `cil_step` equations for the instructions that observe class instance references: identity comparison (`ceq`, `beq`, `bne.un`), presence (`cgt.un` against null, `brtrue`, `brfalse`), -`isinst` and `castclass` (`objectTest`, decided by a `runtimeTypeOf` fact), `ldnull`, +`isinst` and `castclass` (through `objectTest`, decided by a `runtimeTypeOf` fact), `ldnull`, `ldstr` of a caller-bound literal (`literalObject_of_lookup`), `ldfld`/`stfld` of scalar class -fields, and `newobj` of a class, which calls the constructor with a fresh instance that -`allocateObject_ok` or `allocateObject_insufficient` decides, and `callvirt` of a non-virtual +fields, and `newobj`, whose link fact selects a class construction, which calls the constructor +with a fresh instance that `allocateObject_ok` or `allocateObject_insufficient` decides, or a +value-type construction, and `callvirt` of a non-virtual method, which calls with a live receiver and throws `NullReferenceException` on a null one (`receiverCall`), and `callvirt` of a virtual method, which enters the body its dispatch table lists for the receiver's runtime class (`virtualCall`, decided by a `runtimeTypeOf` fact), @@ -67,48 +69,54 @@ section ObjectEquations variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (memory : Memory) -/-- `newobj` of a class checks its `argc` arguments, allocates the instance and calls the - constructor with it as the receiver; the instance waits under the rest of the stack. -/ -@[cil_step] theorem step_newObject_eq (callee argc : Nat) (owner : CIL.Extraction.TypeExpr) - (bases : List CIL.Extraction.TypeExpr) (size : Nat) (stack : List Value) : - step body (.newObject callee argc owner bases size) pc args frame stack memory = - if unspecifiedOperands argc stack then +/-- `newobj` checks its constructor's arguments for unspecified bits and then builds what the + link at its pc states: a zeroed value-type temporary owned by the frame and passed by address, + or an instance of the constructor's class with the link's bases, passed as the receiver and + left under the rest of the stack. An unlinked constructor runs its runtime model. -/ +@[cil_step] theorem step_newobj_eq (constructor : CIL.Extraction.MethodRef) + (stack : List Value) : + step body (.newobj constructor) pc args frame stack memory = + if unspecifiedOperands constructor.parameters.length stack then .error (.unsupported "Instruction observes unspecified vector bits") - else if stack.length < argc then .error .invalidState - else do - let arguments ← (stack.take argc).reverse.mapM (checkedValue memory) - let (object, allocated) ← allocateObject memory ⟨owner, bases⟩ size - return FrameAction.call callee (.object (.address object) :: arguments) - (.object (.address object) :: stack.drop argc) allocated := by - simp only [step, observerCount, Bool.or_false] + else match body.linkAt pc with + | some (.aggregate callee size) => + if size = 0 || stack.length < constructor.parameters.length then .error .invalidState + else do + let arguments ← + (stack.take constructor.parameters.length).reverse.mapM (callArgument frame memory) + let (temporary, allocated) ← allocateHome memory frame.activation size + let (_, stored) ← storeLocal allocated (.bytes (.aggregate size) temporary) + (.scalar (.aggregate (List.replicate size 0))) + return FrameAction.construct callee (.reference (.address temporary) :: arguments) + (stack.drop constructor.parameters.length) + { frame with owned := temporary.allocation :: frame.owned } temporary + (.aggregate size) stored + | some (.object callee bases size) => + if stack.length < constructor.parameters.length then .error .invalidState + else do + let arguments ← + (stack.take constructor.parameters.length).reverse.mapM (callArgument frame memory) + let (object, allocated) ← allocateObject memory ⟨constructor.owner, bases⟩ size + return FrameAction.call callee (.object (.address object) :: arguments) + (.object (.address object) :: stack.drop constructor.parameters.length) allocated + | none => modelStep (CIL.Extraction.runtimeModel body.environment .newobj constructor) + body pc frame stack memory + | _ => .error unlinkedCall := by + simp only [step, observerCount] + rfl -#print axioms step_newObject_eq +#print axioms step_newobj_eq /-- The call a receiver-checked `callvirt` makes with checked `arguments`, receiver first: a null receiver throws `NullReferenceException`, and a receiver that is no object reference is - ill-typed. This is the `callInstance` arm of `step` after its argument checks. -/ + ill-typed. -/ def receiverCall (callee : Nat) (arguments rest : List Value) (memory : Memory) : ExecutionResult FrameAction := match arguments with | .object (.address _) :: _ => .ok (.call callee arguments rest memory) - | .object .null :: _ => .error (.thrown "System.NullReferenceException") + | .object .null :: _ => .error (.thrown .nullReference) | _ => .error .invalidState -/-- `callvirt` of a non-virtual method checks its `argc` arguments and calls with a live receiver; - a null receiver throws `NullReferenceException` before the call. -/ -@[cil_step] theorem step_callInstance_eq (callee argc : Nat) (stack : List Value) : - step body (.callInstance callee argc) pc args frame stack memory = - if unspecifiedOperands argc stack then - .error (.unsupported "Instruction observes unspecified vector bits") - else if stack.length < argc then .error .invalidState - else (do - let arguments ← (stack.take argc).reverse.mapM (checkedValue memory) - receiverCall callee arguments (stack.drop argc) memory) := by - simp only [step, observerCount, Bool.or_false] - rfl - -#print axioms step_callInstance_eq - /-- A live receiver makes the call with the checked arguments as they stand. -/ @[cil_checked] theorem receiverCall_address (callee : Nat) (receiver : Reference) (arguments rest : List Value) : @@ -120,36 +128,43 @@ def receiverCall (callee : Nat) (arguments rest : List Value) (memory : Memory) /-- A null receiver throws before the call. -/ @[cil_checked] theorem receiverCall_null (callee : Nat) (arguments rest : List Value) : receiverCall callee (.object .null :: arguments) rest memory = - .error (.thrown "System.NullReferenceException") := rfl + .error (.thrown .nullReference) := rfl #print axioms receiverCall_null /-- The call a virtual `callvirt` makes with checked `arguments`, receiver first: into the body `targets` lists for a live receiver's runtime class, `NullReferenceException` for a null - receiver. This is the `callVirtual` arm of `step` after its argument checks. -/ + receiver. -/ def virtualCall (targets : List (CIL.Extraction.TypeExpr × Nat)) (arguments rest : List Value) (memory : Memory) : ExecutionResult FrameAction := match arguments with | .object (.address receiver) :: _ => do return .call (← virtualTarget memory targets receiver) arguments rest memory - | .object .null :: _ => .error (.thrown "System.NullReferenceException") + | .object .null :: _ => .error (.thrown .nullReference) | _ => .error .invalidState -/-- `callvirt` of a virtual method checks its `argc` arguments and enters the body its table - selects for the receiver; a null receiver throws `NullReferenceException` before the call. -/ -@[cil_step] theorem step_callVirtual_eq (targets : List (CIL.Extraction.TypeExpr × Nat)) - (argc : Nat) (stack : List Value) : - step body (.callVirtual targets argc) pc args frame stack memory = +/-- Receiver calls check their operands before reading the direct body or dispatch table. -/ +@[cil_step] theorem step_callvirt_eq (method : CIL.Extraction.MethodRef) (stack : List Value) : + step body (.callvirt method) pc args frame stack memory = + let argc := method.argumentCount if unspecifiedOperands argc stack then .error (.unsupported "Instruction observes unspecified vector bits") - else if stack.length < argc then .error .invalidState - else (do - let arguments ← (stack.take argc).reverse.mapM (checkedValue memory) - virtualCall targets arguments (stack.drop argc) memory) := by - simp only [step, observerCount, Bool.or_false] + else match body.linkAt pc with + | some (.direct callee) => + if stack.length < argc then .error .invalidState + else (do + let arguments ← (stack.take argc).reverse.mapM (callArgument frame memory) + receiverCall callee arguments (stack.drop argc) memory) + | some (.virtual targets) => + if stack.length < argc then .error .invalidState + else (do + let arguments ← (stack.take argc).reverse.mapM (callArgument frame memory) + virtualCall targets arguments (stack.drop argc) memory) + | _ => .error unlinkedCall := by + simp only [step, observerCount] rfl -#print axioms step_callVirtual_eq +#print axioms step_callvirt_eq /-- A live receiver enters the body the table lists for its runtime class; a step fact for `runtimeTypeOf` then decides the lookup. -/ @@ -196,16 +211,18 @@ theorem virtualCall_outside (targets : List (CIL.Extraction.TypeExpr × Nat)) @[cil_checked] theorem virtualCall_null (targets : List (CIL.Extraction.TypeExpr × Nat)) (arguments rest : List Value) : virtualCall targets (.object .null :: arguments) rest memory = - .error (.thrown "System.NullReferenceException") := rfl + .error (.thrown .nullReference) := rfl #print axioms virtualCall_null +-- These equations are closed by `id rfl` because simp applies a lemma proved by `rfl` without a +-- proof term, and the kernel would then reduce `step` again at every use. /-- `ceq` on two object references pushes their identity comparison. -/ -@[cil_step high] theorem step_eq_object_eq (left right : ManagedReference) (rest : List Value) : - step body .eq pc args frame (.object right :: .object left :: rest) memory = do +@[cil_step high] theorem step_ceq_object_eq (left right : ManagedReference) (rest : List Value) : + step body .ceq pc args frame (.object right :: .object left :: rest) memory = do let equal ← objectEqual memory left right return .next (pc + 1) (.scalar (.i32 (if equal then 1 else 0)) :: rest) frame memory := - rfl + id rfl /-- `beq` on two object references branches when they are the same instance. -/ @[cil_step high] theorem step_beq_object_eq (target : Nat) (left right : ManagedReference) @@ -213,88 +230,106 @@ theorem virtualCall_outside (targets : List (CIL.Extraction.TypeExpr × Nat)) step body (.beq target) pc args frame (.object right :: .object left :: rest) memory = do let equal ← objectEqual memory left right return .next (if equal then target else pc + 1) rest frame memory := - rfl + id rfl /-- `bne.un` on two object references branches when they are different instances. -/ -@[cil_step high] theorem step_bne_object_eq (target : Nat) (left right : ManagedReference) +@[cil_step high] theorem step_bne_un_object_eq (target : Nat) (left right : ManagedReference) (rest : List Value) : - step body (.bne target) pc args frame (.object right :: .object left :: rest) memory = do + step body (.bne_un target) pc args frame (.object right :: .object left :: rest) memory = do let equal ← objectEqual memory left right return .next (if equal then pc + 1 else target) rest frame memory := - rfl + id rfl /-- `cgt.un` of an object reference against null pushes whether the reference is present. -/ -@[cil_step high] theorem step_gtu_object_eq (left right : ManagedReference) (rest : List Value) : - step body .gtu pc args frame (.object right :: .object left :: rest) memory = do +@[cil_step high] theorem step_cgt_un_object_eq (left right : ManagedReference) + (rest : List Value) : + step body .cgt_un pc args frame (.object right :: .object left :: rest) memory = do let present ← objectAboveNull memory left right return .next (pc + 1) (.scalar (.i32 (if present then 1 else 0)) :: rest) frame memory := - rfl + id rfl /-- `brfalse` on an object reference branches when it is null. -/ -@[cil_step high] theorem step_brzero_object_eq (target : Nat) (value : ManagedReference) +@[cil_step high] theorem step_brfalse_object_eq (target : Nat) (value : ManagedReference) (rest : List Value) : - step body (.brzero target) pc args frame (.object value :: rest) memory = do + step body (.brfalse target) pc args frame (.object value :: rest) memory = do let present ← objectNonNull memory value return .next (if present then pc + 1 else target) rest frame memory := - rfl + id rfl /-- `brtrue` on an object reference branches when it is present. -/ -@[cil_step high] theorem step_brnonzero_object_eq (target : Nat) (value : ManagedReference) +@[cil_step high] theorem step_brtrue_object_eq (target : Nat) (value : ManagedReference) (rest : List Value) : - step body (.brnonzero target) pc args frame (.object value :: rest) memory = do + step body (.brtrue target) pc args frame (.object value :: rest) memory = do let present ← objectNonNull memory value return .next (if present then target else pc + 1) rest frame memory := - rfl + id rfl -/-- `isinst` and `castclass` replace an object reference by its tested form: itself when its - runtime lineage holds the class, else null or `InvalidCastException`. -/ -@[cil_step high] theorem step_objectTest_eq (type : CIL.Extraction.TypeExpr) (throwing : Bool) +/-- `castclass` keeps an object reference whose runtime lineage holds the class, and null; + any other object throws `InvalidCastException`. -/ +@[cil_step high] theorem step_castclass_eq (type : CIL.Extraction.TypeExpr) (value : ManagedReference) (rest : List Value) : - step body (.objectTest type throwing) pc args frame (.object value :: rest) memory = do - let result ← objectTest memory type throwing value + step body (.castclass type) pc args frame (.object value :: rest) memory = do + let result ← objectTest memory type true value return .next (pc + 1) (.object result :: rest) frame memory := - rfl + id rfl -/-- `newarr` of a class with an int32 length allocates the array and pushes it. -/ -@[cil_step high] theorem step_newReferenceArray_i32_eq (element : CIL.Extraction.TypeExpr) +/-- `isinst` keeps an object reference whose runtime lineage holds the class, and null; any + other object becomes null. -/ +@[cil_step high] theorem step_isinst_eq (type : CIL.Extraction.TypeExpr) + (value : ManagedReference) (rest : List Value) : + step body (.isinst type) pc args frame (.object value :: rest) memory = do + let result ← objectTest memory type false value + return .next (pc + 1) (.object result :: rest) frame memory := + id rfl + +/-- `newarr` with an int32 length allocates an array of its element's shape and pushes it. -/ +@[cil_step high] theorem step_newarr_i32_eq (element : CIL.Extraction.TypeExpr) (bits : BitVec 32) (rest : List Value) : - step body (.newReferenceArray element) pc args frame (.scalar (.i32 bits) :: rest) memory = do - let (array, allocated) ← newReferenceArray memory element (.i32 bits) - return .next (pc + 1) (.object (.address array) :: rest) frame allocated := rfl + step body (.newarr element) pc args frame (.scalar (.i32 bits) :: rest) memory = do + let (array, allocated) ← allocateElements (body.elementShape element) (.i32 bits) memory + return .next (pc + 1) (array :: rest) frame allocated := id rfl -/-- `newarr` of a class with a native length allocates the array and pushes it. -/ -@[cil_step high] theorem step_newReferenceArray_i64_eq (element : CIL.Extraction.TypeExpr) +/-- `newarr` with a native length allocates an array of its element's shape and pushes it. -/ +@[cil_step high] theorem step_newarr_i64_eq (element : CIL.Extraction.TypeExpr) (bits : BitVec 64) (rest : List Value) : - step body (.newReferenceArray element) pc args frame (.scalar (.i64 bits) :: rest) memory = do - let (array, allocated) ← newReferenceArray memory element (.i64 bits) - return .next (pc + 1) (.object (.address array) :: rest) frame allocated := rfl + step body (.newarr element) pc args frame (.scalar (.i64 bits) :: rest) memory = do + let (array, allocated) ← allocateElements (body.elementShape element) (.i64 bits) memory + return .next (pc + 1) (array :: rest) frame allocated := id rfl + +/-- An inline element shape allocates a value array. -/ +@[cil_step] theorem allocateElements_value (element : CIL.ArrayElement) (length : CIL.Value) : + allocateElements (.value element) length memory = do + let (view, allocated) ← newArray memory element length + return (.array view, allocated) := rfl + +/-- A class element shape allocates an array of null references. -/ +@[cil_step] theorem allocateElements_reference (element : CIL.Extraction.TypeExpr) + (length : CIL.Value) : + allocateElements (.reference element) length memory = do + let (array, allocated) ← newReferenceArray memory element length + return (.object (.address array), allocated) := rfl /-- `ldnull` pushes the null object reference and leaves memory unchanged. -/ -@[cil_step high] theorem step_nullObject_eq (stack : List Value) : - step body (.memory .nullObject) pc args frame stack memory = - .ok (.next (pc + 1) (.object .null :: stack) frame memory) := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, List.any] - rfl +@[cil_step high] theorem step_ldnull_eq (stack : List Value) : + step body .ldnull pc args frame stack memory = + .ok (.next (pc + 1) (.object .null :: stack) frame memory) := id rfl /-- `ldstr` pushes the object bound to its text and leaves memory unchanged. -/ -@[cil_step high] theorem step_stringLiteral_eq (text : String) (stack : List Value) : - step body (.memory (.stringLiteral text)) pc args frame stack memory = do +@[cil_step high] theorem step_ldstr_eq (text : String) (stack : List Value) : + step body (.ldstr text) pc args frame stack memory = do let value ← literalObject memory text return .next (pc + 1) (value :: stack) frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - stringLiteralInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Bool.false_eq_true, ite_false, Bind.bind, Except.bind, Pure.pure, Except.pure] + simp only [step, stringLiteralInstruction, Bind.bind, Except.bind, Pure.pure, Except.pure] cases literalObject memory text <;> rfl /-- `ThrowIfNull` throws `ArgumentNullException` for a null argument; otherwise it pops the argument and the parameter name and leaves memory unchanged. -/ @[cil_step high] theorem step_throwIfNull_eq (name argument : ManagedReference) (rest : List Value) : - step body (.memory .throwIfNull) pc args frame (.object name :: .object argument :: rest) + modelStep (some (.memory .throwIfNull)) body pc frame (.object name :: .object argument :: rest) memory = match argument with - | .null => .error (.thrown "System.ArgumentNullException") + | .null => .error (.thrown .argumentNull) | .address _ => .ok (.next (pc + 1) rest frame memory) := by cases argument <;> rfl @@ -304,7 +339,7 @@ def zeroGuardOutcome (guard : ZeroGuard) (width : Nat) (signed : Bool) (value : match guardOperand width signed value with | some number => match guard.throws number with - | true => .error (.thrown "System.ArgumentOutOfRangeException") + | true => .error (.thrown .argumentOutOfRange) | false => .ok () | none => .error .invalidState @@ -314,18 +349,19 @@ def comparisonGuardOutcome (guard : ComparisonGuard) (width : Nat) (signed : Boo match guardOperand width signed value, guardOperand width signed other with | some number, some bound => match guard.throws number bound with - | true => .error (.thrown "System.ArgumentOutOfRangeException") + | true => .error (.thrown .argumentOutOfRange) | false => .ok () | _, _ => .error .invalidState /-- A zero guard on a 32-bit stack value throws, or pops it with the parameter name. -/ @[cil_step high] theorem step_zeroGuard_i32_eq (guard : ZeroGuard) (width : Nat) (signed : Bool) (name : ManagedReference) (bits : CIL.W32) (rest : List Value) : - step body (.memory (.zeroGuard guard width signed)) pc args frame + modelStep (some (.memory (.zeroGuard guard width signed))) body pc frame (.object name :: .scalar (.i32 bits) :: rest) memory = do zeroGuardOutcome guard width signed (.i32 bits) return .next (pc + 1) rest frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, + simp only [modelStep_memory, viewOperandCount, viewOperands, pure_bind, Bool.or_false, + staticInstruction, memoryInstruction, zeroGuardInstruction, zeroGuardOutcome, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take_succ_cons, List.take_zero, List.any_cons, List.any_nil, Value.hasUnspecified, CIL.Value.hasUnspecified, Bool.false_eq_true, ite_false] @@ -336,11 +372,12 @@ def comparisonGuardOutcome (guard : ComparisonGuard) (width : Nat) (signed : Boo /-- A zero guard on a 64-bit stack value throws, or pops it with the parameter name. -/ @[cil_step high] theorem step_zeroGuard_i64_eq (guard : ZeroGuard) (width : Nat) (signed : Bool) (name : ManagedReference) (bits : CIL.W64) (rest : List Value) : - step body (.memory (.zeroGuard guard width signed)) pc args frame + modelStep (some (.memory (.zeroGuard guard width signed))) body pc frame (.object name :: .scalar (.i64 bits) :: rest) memory = do zeroGuardOutcome guard width signed (.i64 bits) return .next (pc + 1) rest frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, + simp only [modelStep_memory, viewOperandCount, viewOperands, pure_bind, Bool.or_false, + staticInstruction, memoryInstruction, zeroGuardInstruction, zeroGuardOutcome, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take_succ_cons, List.take_zero, List.any_cons, List.any_nil, Value.hasUnspecified, CIL.Value.hasUnspecified, Bool.false_eq_true, ite_false] @@ -351,11 +388,12 @@ def comparisonGuardOutcome (guard : ComparisonGuard) (width : Nat) (signed : Boo /-- A comparison guard on 32-bit stack values throws, or pops both with the parameter name. -/ @[cil_step high] theorem step_comparisonGuard_i32_eq (guard : ComparisonGuard) (width : Nat) (signed : Bool) (name : ManagedReference) (other value : CIL.W32) (rest : List Value) : - step body (.memory (.comparisonGuard guard width signed)) pc args frame + modelStep (some (.memory (.comparisonGuard guard width signed))) body pc frame (.object name :: .scalar (.i32 other) :: .scalar (.i32 value) :: rest) memory = do comparisonGuardOutcome guard width signed (.i32 value) (.i32 other) return .next (pc + 1) rest frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, + simp only [modelStep_memory, viewOperandCount, viewOperands, pure_bind, Bool.or_false, + staticInstruction, memoryInstruction, comparisonGuardInstruction, comparisonGuardOutcome, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take_succ_cons, List.take_zero, List.any_cons, List.any_nil, Value.hasUnspecified, CIL.Value.hasUnspecified, Bool.false_eq_true, ite_false] @@ -367,11 +405,12 @@ def comparisonGuardOutcome (guard : ComparisonGuard) (width : Nat) (signed : Boo /-- A comparison guard on 64-bit stack values throws, or pops both with the parameter name. -/ @[cil_step high] theorem step_comparisonGuard_i64_eq (guard : ComparisonGuard) (width : Nat) (signed : Bool) (name : ManagedReference) (other value : CIL.W64) (rest : List Value) : - step body (.memory (.comparisonGuard guard width signed)) pc args frame + modelStep (some (.memory (.comparisonGuard guard width signed))) body pc frame (.object name :: .scalar (.i64 other) :: .scalar (.i64 value) :: rest) memory = do comparisonGuardOutcome guard width signed (.i64 value) (.i64 other) return .next (pc + 1) rest frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, + simp only [modelStep_memory, viewOperandCount, viewOperands, pure_bind, Bool.or_false, + staticInstruction, memoryInstruction, comparisonGuardInstruction, comparisonGuardOutcome, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take_succ_cons, List.take_zero, List.any_cons, List.any_nil, Value.hasUnspecified, CIL.Value.hasUnspecified, Bool.false_eq_true, ite_false] @@ -386,125 +425,33 @@ def comparisonGuardOutcome (guard : ComparisonGuard) (width : Nat) (signed : Boo #print axioms step_comparisonGuard_i32_eq #print axioms step_comparisonGuard_i64_eq -/-- `ldfld` of a scalar field of an object loads it from the field's located address. -/ -theorem step_objectField_eq (owner : CIL.Extraction.TypeExpr) - (offset width : Nat) (signed : Bool) (fits : width ∈ [1, 2, 4, 8]) - (reference : ManagedReference) (rest : List Value) : - step body (.memory (.objectField owner offset width signed)) pc args frame - (.object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset - let value ← loadValue memory (.address address) width signed - return .next (pc + 1) (.scalar value :: rest) frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, fits, not_true_eq_false, ite_false, Value.hasUnspecified, Bool.false_eq_true, - Bind.bind, Except.bind, Pure.pure, Except.pure] - cases objectFieldAddress memory reference owner offset <;> try rfl - rename_i address - dsimp only - cases loadValue memory (.address address) width signed <;> rfl - -/-- `stfld` of a 64-bit object field writes the value's bytes at the located address. -/ -@[cil_step high] theorem step_setObjectField_i64_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (bits : BitVec 64) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.setObjectField owner offset 8)) pc args frame - (.scalar (.i64 bits) :: .object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset - let after ← checkedAt (.address address) 8 - (write memory address (numberBytes bits.toNat 8) ordinaryAccessAlignment) - return .next (pc + 1) rest frame after := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Value.hasUnspecified, CIL.Value.hasUnspecified, Bool.false_eq_true, ite_false, - Bind.bind, Except.bind, Pure.pure, Except.pure] - cases objectFieldAddress memory reference owner offset <;> try rfl - rename_i address - dsimp only - cases checkedAt (.address address) 8 - (write memory address (numberBytes bits.toNat 8) ordinaryAccessAlignment) <;> rfl - -/-- `stfld` of a 1-, 2- or 4-byte object field writes the low bytes of the int32 value. -/ -theorem step_setObjectField_i32_eq (owner : CIL.Extraction.TypeExpr) - (offset width : Nat) (narrow : width ∈ [1, 2, 4]) (bits : BitVec 32) - (reference : ManagedReference) (rest : List Value) : - step body (.memory (.setObjectField owner offset width)) pc args frame - (.scalar (.i32 bits) :: .object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset - let after ← checkedAt (.address address) width - (write memory address (numberBytes bits.toNat width) ordinaryAccessAlignment) - return .next (pc + 1) rest frame after := by - simp only [List.mem_cons, List.mem_nil_iff, or_false] at narrow - rcases narrow with rfl | rfl | rfl - all_goals - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Value.hasUnspecified, CIL.Value.hasUnspecified, Bool.false_eq_true, - ite_false, Bind.bind, Except.bind, Pure.pure, Except.pure] - cases objectFieldAddress memory reference owner offset <;> try rfl - rename_i address - dsimp only - cases checkedAt (.address address) _ - (write memory address (numberBytes bits.toNat _) ordinaryAccessAlignment) <;> rfl - - -/-- `ldfld` of a class-typed object field pushes the reference its located slot holds. -/ -@[cil_step high] theorem step_objectReferenceField_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.objectReferenceField owner offset)) pc args frame - (.object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset - let value ← loadSlot memory address - return .next (pc + 1) (value :: rest) frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Value.hasUnspecified, Bool.false_eq_true, ite_false, Bind.bind, Except.bind, - Pure.pure, Except.pure] - cases objectFieldAddress memory reference owner offset <;> try rfl - rename_i address - dsimp only - cases loadSlot memory address <;> rfl - -/-- `stfld` of a class-typed object field stores the reference into its located slot. -/ -@[cil_step high] theorem step_setObjectReferenceField_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (value reference : ManagedReference) (rest : List Value) : - step body (.memory (.setObjectReferenceField owner offset)) pc args frame - (.object value :: .object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset - let after ← storeSlot memory address value - return .next (pc + 1) rest frame after := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Value.hasUnspecified, Bool.false_eq_true, ite_false, Bind.bind, Except.bind, - Pure.pure, Except.pure] - cases objectFieldAddress memory reference owner offset <;> try rfl - rename_i address - dsimp only - cases storeSlot memory address value <;> rfl - /-- `ldlen` of an array of class references pushes its length as a native integer. -/ -@[cil_step high] theorem step_referenceArrayLength_eq (array : ManagedReference) - (rest : List Value) : - step body (.memory .referenceArrayLength) pc args frame (.object array :: rest) memory = do +@[cil_step high] theorem step_ldlen_object_eq (array : ManagedReference) (rest : List Value) : + step body .ldlen pc args frame (.object array :: rest) memory = do let found ← referenceArrayOf memory array return .next (pc + 1) (.scalar (.i64 (BitVec.ofNat 64 found.2.2)) :: rest) frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Value.hasUnspecified, Bool.false_eq_true, ite_false, Bind.bind, Except.bind, - Pure.pure, Except.pure] + rw [step_ldlen_eq] + simp only [instructionStep, instruction, elementInstruction, Bind.bind, Except.bind, Pure.pure, + Except.pure] cases referenceArrayOf memory array <;> rfl +/-- `ldlen` of a null object reference, as `ldnull` pushes it, throws `NullReferenceException`. -/ +@[cil_step high] theorem step_ldlen_object_null_eq (rest : List Value) : + step body .ldlen pc args frame (.object .null :: rest) memory = + .error (.thrown .nullReference) := by + rw [step_ldlen_object_eq] + rfl + /-- `ldelem.ref` with an int32 index pushes the reference its element's slot holds. -/ -@[cil_step high] theorem step_referenceArrayLoad_eq (index : BitVec 32) +@[cil_step high] theorem step_ldelem_ref_i32_eq (index : BitVec 32) (array : ManagedReference) (rest : List Value) : - step body (.memory .referenceArrayLoad) pc args frame - (.scalar (.i32 index) :: .object array :: rest) memory = do + step body .ldelem_ref pc args frame (.scalar (.i32 index) :: .object array :: rest) memory = do let slot ← referenceArraySlot memory array index.toNat let value ← loadSlot memory slot.1 return .next (pc + 1) (value :: rest) frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Value.hasUnspecified, CIL.Value.hasUnspecified, Bool.false_eq_true, ite_false, - arrayIndex, Bind.bind, Except.bind, Pure.pure, Except.pure] + rw [step_ldelem_ref_eq] + simp only [instructionStep, instruction, elementInstruction, arrayIndex, Bind.bind, Except.bind, + Pure.pure, Except.pure] cases referenceArraySlot memory array index.toNat <;> try rfl rename_i slot dsimp only @@ -512,18 +459,17 @@ theorem step_setObjectField_i32_eq (owner : CIL.Extraction.TypeExpr) /-- `stelem.ref` with an int32 index checks the stored object's class against the element class, then stores it into the element's slot. -/ -@[cil_step high] theorem step_referenceArrayStore_eq (index : BitVec 32) +@[cil_step high] theorem step_stelem_ref_i32_eq (index : BitVec 32) (value array : ManagedReference) (rest : List Value) : - step body (.memory .referenceArrayStore) pc args frame + step body .stelem_ref pc args frame (.object value :: .scalar (.i32 index) :: .object array :: rest) memory = do let slot ← referenceArraySlot memory array index.toNat let _ ← storableIn memory slot.2 value let after ← storeSlot memory slot.1 value return .next (pc + 1) rest frame after := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Value.hasUnspecified, CIL.Value.hasUnspecified, Bool.false_eq_true, ite_false, - arrayIndex, Bind.bind, Except.bind, Pure.pure, Except.pure] + rw [step_stelem_ref_eq] + simp only [instructionStep, instruction, elementInstruction, arrayIndex, Bind.bind, Except.bind, + Pure.pure, Except.pure] cases referenceArraySlot memory array index.toNat <;> try rfl rename_i slot dsimp only @@ -531,41 +477,6 @@ theorem step_setObjectField_i32_eq (owner : CIL.Extraction.TypeExpr) dsimp only cases storeSlot memory slot.1 value <;> rfl -/-- `ldfld` of a value-array field pushes the whole array its located slot names. -/ -@[cil_step high] theorem step_arrayField_eq (owner : CIL.Extraction.TypeExpr) (offset : Nat) - (element : CIL.ArrayElement) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.arrayField owner offset element)) pc args frame - (.object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset - let value ← loadArraySlot memory address element - return .next (pc + 1) (value :: rest) frame memory := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Value.hasUnspecified, Bool.false_eq_true, ite_false, Bind.bind, Except.bind, - Pure.pure, Except.pure] - cases objectFieldAddress memory reference owner offset <;> try rfl - rename_i address - dsimp only - cases loadArraySlot memory address element <;> rfl - -/-- `stfld` of a value-array field keeps the stored array's origin in its located slot. -/ -@[cil_step high] theorem step_setArrayField_eq (owner : CIL.Extraction.TypeExpr) (offset : Nat) - (element : CIL.ArrayElement) (view : ArrayView) (reference : ManagedReference) - (rest : List Value) : - step body (.memory (.setArrayField owner offset element)) pc args frame - (.array view :: .object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset - let after ← storeArraySlot memory address view element - return .next (pc + 1) rest frame after := by - simp only [step, observerCount, Bool.or_false, staticInstruction, memoryInstruction, - objectFieldInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, List.take, - List.any, Value.hasUnspecified, Bool.false_eq_true, ite_false, Bind.bind, Except.bind, - Pure.pure, Except.pure] - cases objectFieldAddress memory reference owner offset <;> try rfl - rename_i address - dsimp only - cases storeArraySlot memory address view element <;> rfl - /-- A readable slot loads the reference it holds, which must still form. -/ theorem loadSlot_of_access {memory : Memory} {field : Reference} (readable : access memory field objectFieldSlot ordinaryAccessAlignment false = .ok ()) : @@ -614,113 +525,525 @@ theorem storeArraySlot_whole {memory : Memory} {field array : Reference} simp only [storeArraySlot, arraySlotValue, writable, found, heap, origin, whole, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, and_self, ↓reduceIte] -/-- `ldfld` of a 1-byte object field, as `step_objectField_eq` states it. -/ -@[cil_step high] theorem step_objectField_1_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (signed : Bool) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.objectField owner offset 1 signed)) pc args frame - (.object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset +/-- `ldfld` of a class field whose slot holds a `width`-byte integer loads it from the field's + located address. -/ +theorem step_ldfld_word_eq (field : CIL.Extraction.FieldRef) (offset width : Nat) (signed : Bool) + (fits : width ∈ [1, 2, 4, 8]) + (shape : body.fieldShape field = .classSlot offset (.word width signed)) + (reference : ManagedReference) (rest : List Value) : + step body (.ldfld field) pc args frame (.object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset + let value ← loadValue memory (.address address) width signed + return .next (pc + 1) (.scalar value :: rest) frame memory := by + simp only [step_ldfld_eq, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + ite_false, fieldLoad, shape, classFieldLoad, fits, not_true_eq_false, + Bind.bind, Except.bind, Pure.pure, Except.pure] + cases objectFieldAddress memory reference field.owner offset <;> try rfl + rename_i address + dsimp only + cases loadValue memory (.address address) width signed <;> rfl + +/-- `stfld` of a class field whose slot holds an 8-byte integer writes the value's bytes at the + located address. -/ +theorem step_stfld_word_i64_eq (field : CIL.Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .classSlot offset (.word 8 signed)) + (bits : BitVec 64) (reference : ManagedReference) (rest : List Value) : + step body (.stfld field) pc args frame + (.scalar (.i64 bits) :: .object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset + let after ← checkedAt (.address address) 8 + (write memory address (numberBytes bits.toNat 8) ordinaryAccessAlignment) + return .next (pc + 1) rest frame after := by + simp only [step_stfld_eq, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + CIL.Value.hasUnspecified, fieldStore, shape, classFieldStore, + Bind.bind, Except.bind, Pure.pure, Except.pure] + cases objectFieldAddress memory reference field.owner offset <;> try rfl + rename_i address + dsimp only + cases checkedAt (.address address) 8 + (write memory address (numberBytes bits.toNat 8) ordinaryAccessAlignment) <;> rfl + +/-- `stfld` of a class field whose slot holds a 1-, 2- or 4-byte integer writes the low bytes of + the int32 value. -/ +theorem step_stfld_word_i32_eq (field : CIL.Extraction.FieldRef) (offset width : Nat) + (signed : Bool) (narrow : width ∈ [1, 2, 4]) + (shape : body.fieldShape field = .classSlot offset (.word width signed)) + (bits : BitVec 32) (reference : ManagedReference) (rest : List Value) : + step body (.stfld field) pc args frame + (.scalar (.i32 bits) :: .object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset + let after ← checkedAt (.address address) width + (write memory address (numberBytes bits.toNat width) ordinaryAccessAlignment) + return .next (pc + 1) rest frame after := by + simp only [List.mem_cons, List.mem_nil_iff, or_false] at narrow + rcases narrow with rfl | rfl | rfl + all_goals + simp only [step_stfld_eq, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + CIL.Value.hasUnspecified, fieldStore, shape, + classFieldStore, Bind.bind, Except.bind, Pure.pure, Except.pure] + cases objectFieldAddress memory reference field.owner offset <;> try rfl + rename_i address + dsimp only + cases checkedAt (.address address) _ + (write memory address (numberBytes bits.toNat _) ordinaryAccessAlignment) <;> rfl + +/-- `ldfld` of a class field holding an object reference pushes the reference its located slot + holds. -/ +theorem step_ldfld_reference_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (shape : body.fieldShape field = .classSlot offset .reference) + (reference : ManagedReference) (rest : List Value) : + step body (.ldfld field) pc args frame (.object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset + let value ← loadSlot memory address + return .next (pc + 1) (value :: rest) frame memory := by + simp only [step_ldfld_eq, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + fieldLoad, shape, classFieldLoad, Bind.bind, Except.bind, + Pure.pure, Except.pure] + cases objectFieldAddress memory reference field.owner offset <;> try rfl + rename_i address + dsimp only + cases loadSlot memory address <;> rfl + +/-- `stfld` of a class field holding an object reference stores the reference into its located + slot. -/ +theorem step_stfld_reference_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (shape : body.fieldShape field = .classSlot offset .reference) + (value reference : ManagedReference) (rest : List Value) : + step body (.stfld field) pc args frame (.object value :: .object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset + let after ← storeSlot memory address value + return .next (pc + 1) rest frame after := by + simp only [step_stfld_eq, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + fieldStore, shape, classFieldStore, Bind.bind, Except.bind, + Pure.pure, Except.pure] + cases objectFieldAddress memory reference field.owner offset <;> try rfl + rename_i address + dsimp only + cases storeSlot memory address value <;> rfl + +/-- `ldfld` of a class field holding a value array pushes the whole array its located slot + names. -/ +theorem step_ldfld_array_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (element : CIL.ArrayElement) + (shape : body.fieldShape field = .classSlot offset (.array element)) + (reference : ManagedReference) (rest : List Value) : + step body (.ldfld field) pc args frame (.object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset + let value ← loadArraySlot memory address element + return .next (pc + 1) (value :: rest) frame memory := by + simp only [step_ldfld_eq, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + fieldLoad, shape, classFieldLoad, Bind.bind, Except.bind, + Pure.pure, Except.pure] + cases objectFieldAddress memory reference field.owner offset <;> try rfl + rename_i address + dsimp only + cases loadArraySlot memory address element <;> rfl + +/-- `stfld` of a class field holding a value array keeps the stored array's origin in its + located slot. -/ +theorem step_stfld_array_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (element : CIL.ArrayElement) + (shape : body.fieldShape field = .classSlot offset (.array element)) + (view : ArrayView) (reference : ManagedReference) (rest : List Value) : + step body (.stfld field) pc args frame (.array view :: .object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset + let after ← storeArraySlot memory address view element + return .next (pc + 1) rest frame after := by + simp only [step_stfld_eq, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + fieldStore, shape, classFieldStore, Bind.bind, Except.bind, + Pure.pure, Except.pure] + cases objectFieldAddress memory reference field.owner offset <;> try rfl + rename_i address + dsimp only + cases storeArraySlot memory address view element <;> rfl + +/-- `ldflda` of a class field faults: the model gives no address into an object. -/ +theorem step_ldflda_class_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (slot : CIL.ClassSlot) (shape : body.fieldShape field = .classSlot offset slot) + (reference : ManagedReference) (rest : List Value) : + step body (.ldflda field) pc args frame (.object reference :: rest) memory = + .error (.unsupported "Class field address") := by + simp only [step_ldflda_eq, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + fieldReference, shape] + rfl + +/-- `ldfld` of a class field whose slot holds a 1-byte integer, as `step_ldfld_word_eq` states + it. -/ +theorem step_ldfld_word_1_eq (field : CIL.Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .classSlot offset (.word 1 signed)) + (reference : ManagedReference) (rest : List Value) : + step body (.ldfld field) pc args frame (.object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset let value ← loadValue memory (.address address) 1 signed return .next (pc + 1) (.scalar value :: rest) frame memory := - by apply step_objectField_eq; decide - -/-- `ldfld` of a 2-byte object field, as `step_objectField_eq` states it. -/ -@[cil_step high] theorem step_objectField_2_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (signed : Bool) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.objectField owner offset 2 signed)) pc args frame - (.object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset + step_ldfld_word_eq body pc args frame memory field offset 1 signed (by decide) shape reference + rest + +/-- `ldfld` of a class field whose slot holds a 2-byte integer, as `step_ldfld_word_eq` states + it. -/ +theorem step_ldfld_word_2_eq (field : CIL.Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .classSlot offset (.word 2 signed)) + (reference : ManagedReference) (rest : List Value) : + step body (.ldfld field) pc args frame (.object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset let value ← loadValue memory (.address address) 2 signed return .next (pc + 1) (.scalar value :: rest) frame memory := - by apply step_objectField_eq; decide - -/-- `ldfld` of a 4-byte object field, as `step_objectField_eq` states it. -/ -@[cil_step high] theorem step_objectField_4_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (signed : Bool) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.objectField owner offset 4 signed)) pc args frame - (.object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset + step_ldfld_word_eq body pc args frame memory field offset 2 signed (by decide) shape reference + rest + +/-- `ldfld` of a class field whose slot holds a 4-byte integer, as `step_ldfld_word_eq` states + it. -/ +theorem step_ldfld_word_4_eq (field : CIL.Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .classSlot offset (.word 4 signed)) + (reference : ManagedReference) (rest : List Value) : + step body (.ldfld field) pc args frame (.object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset let value ← loadValue memory (.address address) 4 signed return .next (pc + 1) (.scalar value :: rest) frame memory := - by apply step_objectField_eq; decide - -/-- `ldfld` of a 8-byte object field, as `step_objectField_eq` states it. -/ -@[cil_step high] theorem step_objectField_8_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (signed : Bool) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.objectField owner offset 8 signed)) pc args frame - (.object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset + step_ldfld_word_eq body pc args frame memory field offset 4 signed (by decide) shape reference + rest + +/-- `ldfld` of a class field whose slot holds a 8-byte integer, as `step_ldfld_word_eq` states + it. -/ +theorem step_ldfld_word_8_eq (field : CIL.Extraction.FieldRef) (offset : Nat) (signed : Bool) + (shape : body.fieldShape field = .classSlot offset (.word 8 signed)) + (reference : ManagedReference) (rest : List Value) : + step body (.ldfld field) pc args frame (.object reference :: rest) memory = do + let address ← objectFieldAddress memory reference field.owner offset let value ← loadValue memory (.address address) 8 signed return .next (pc + 1) (.scalar value :: rest) frame memory := - by apply step_objectField_eq; decide - -/-- `stfld` of a 1-byte object field, as `step_setObjectField_i32_eq` states it. -/ -@[cil_step high] theorem step_setObjectField_i32_1_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (bits : BitVec 32) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.setObjectField owner offset 1)) pc args frame + step_ldfld_word_eq body pc args frame memory field offset 8 signed (by decide) shape reference + rest + +/-- `stfld` of a class field whose slot holds a 1-byte integer, as `step_stfld_word_i32_eq` + states it. -/ +theorem step_stfld_word_i32_1_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (signed : Bool) (shape : body.fieldShape field = .classSlot offset (.word 1 signed)) + (bits : BitVec 32) (reference : ManagedReference) (rest : List Value) : + step body (.stfld field) pc args frame (.scalar (.i32 bits) :: .object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset + let address ← objectFieldAddress memory reference field.owner offset let after ← checkedAt (.address address) 1 (write memory address (numberBytes bits.toNat 1) ordinaryAccessAlignment) return .next (pc + 1) rest frame after := - by apply step_setObjectField_i32_eq; decide - -/-- `stfld` of a 2-byte object field, as `step_setObjectField_i32_eq` states it. -/ -@[cil_step high] theorem step_setObjectField_i32_2_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (bits : BitVec 32) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.setObjectField owner offset 2)) pc args frame + step_stfld_word_i32_eq body pc args frame memory field offset 1 signed (by decide) shape bits + reference rest + +/-- `stfld` of a class field whose slot holds a 2-byte integer, as `step_stfld_word_i32_eq` + states it. -/ +theorem step_stfld_word_i32_2_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (signed : Bool) (shape : body.fieldShape field = .classSlot offset (.word 2 signed)) + (bits : BitVec 32) (reference : ManagedReference) (rest : List Value) : + step body (.stfld field) pc args frame (.scalar (.i32 bits) :: .object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset + let address ← objectFieldAddress memory reference field.owner offset let after ← checkedAt (.address address) 2 (write memory address (numberBytes bits.toNat 2) ordinaryAccessAlignment) return .next (pc + 1) rest frame after := - by apply step_setObjectField_i32_eq; decide - -/-- `stfld` of a 4-byte object field, as `step_setObjectField_i32_eq` states it. -/ -@[cil_step high] theorem step_setObjectField_i32_4_eq (owner : CIL.Extraction.TypeExpr) - (offset : Nat) (bits : BitVec 32) (reference : ManagedReference) (rest : List Value) : - step body (.memory (.setObjectField owner offset 4)) pc args frame + step_stfld_word_i32_eq body pc args frame memory field offset 2 signed (by decide) shape bits + reference rest + +/-- `stfld` of a class field whose slot holds a 4-byte integer, as `step_stfld_word_i32_eq` + states it. -/ +theorem step_stfld_word_i32_4_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (signed : Bool) (shape : body.fieldShape field = .classSlot offset (.word 4 signed)) + (bits : BitVec 32) (reference : ManagedReference) (rest : List Value) : + step body (.stfld field) pc args frame (.scalar (.i32 bits) :: .object reference :: rest) memory = do - let address ← objectFieldAddress memory reference owner offset + let address ← objectFieldAddress memory reference field.owner offset let after ← checkedAt (.address address) 4 (write memory address (numberBytes bits.toNat 4) ordinaryAccessAlignment) return .next (pc + 1) rest frame after := - by apply step_setObjectField_i32_eq; decide + step_stfld_word_i32_eq body pc args frame memory field offset 4 signed (by decide) shape bits + reference rest end ObjectEquations -#print axioms step_eq_object_eq +/-- When set, a closed body's field instruction steps only by the operand's shape fact, and + evaluating the shape instead is an error, so a test can require that a step reads the fact. -/ +register_option cil.requireFieldShapeFacts : Bool := { + defValue := false + descr := "step a closed body's field instruction only by the operand's shape fact" +} + +open Lean Meta in +/-- How `body` resolves `field` as a `FieldShape` literal, with its proof: from the operand's + shared shape fact, or by compiled evaluation for a closed body and operand, which the kernel + checks again. An evaluated shape is the literal a shape fact states, so both give one term. -/ +def fieldShapeKnown? (body field : Expr) : MetaM (Option (Expr × Expr)) := do + let resolver := mkApp2 (mkConst ``CIL.Method.fieldShape) body field + if let some (.done shared) ← ResolutionFacts.sharedShape? ``CIL.Method.fieldShape resolver then + if let some proof := shared.proof? then return some (shared.expr, proof) + if resolver.hasFVar || resolver.hasMVar then return none + if cil.requireFieldShapeFacts.get (← getOptions) then + throwError "field {field} of a closed body has no shape fact" + -- A body the compiler cannot run, such as one over an axiom, is left to simp. + let evaluated? ← try + pure (some (toExpr (← unsafe evalExpr CIL.FieldShape (mkConst ``CIL.FieldShape) resolver))) + catch _ => pure none + evaluated?.mapM fun evaluated => do + return (evaluated, ← mkExpectedTypeHint (← mkEqRefl evaluated) (← mkEq resolver evaluated)) + +open Lean Meta in +/-- How `body` resolves `field`, as `fieldShapeKnown?` finds it, or else as simp rewrites the + shape with the facts in scope. -/ +def fieldShapeIn (body field : Expr) : SimpM (Expr × Expr) := do + if let some known ← fieldShapeKnown? body field then return known + let resolver := mkApp2 (mkConst ``CIL.Method.fieldShape) body field + let shape ← Simp.simp resolver + return (← whnfR (← instantiateMVars shape.expr), ← shape.getProof) + +open Lean Meta in +/-- A vector object access steps as its checked vector operation. A span load steps as a no-op + on a known span value. Each result supplies a proof at the requested stack. -/ +def typedObjectStep? (body op pc args frame stack memory : Expr) : MetaM (Option Expr) := do + let (name, #[type]) := (← whnfR op).getAppFnArgs | return none + let load := name == ``CIL.Instruction.ldobj + unless load || name == ``CIL.Instruction.stobj do return none + let resolver := mkApp2 (mkConst ``CIL.Method.valueShape) body type + let some (.done shared) ← ResolutionFacts.sharedShape? ``CIL.Method.valueShape resolver + | return none + let some proof := shared.proof? | return none + let premises := #[body, pc, args, frame, memory, type, proof] + match shared.expr.getAppFnArgs with + | (``CIL.ValueShape.vector, #[width]) => + let some equation := (match ← getNatValue? width, load with + | some 16, true => some ``step_ldobj_vector16 + | some 32, true => some ``step_ldobj_vector32 + | some 64, true => some ``step_ldobj_vector64 + | some 16, false => some ``step_stobj_vector16 + | some 32, false => some ``step_stobj_vector32 + | some 64, false => some ``step_stobj_vector64 + | _, _ => none) | return none + return some (mkAppN (mkConst equation) (premises.push stack)) + | (``CIL.ValueShape.span, #[]) => + unless load do return none + let (``List.cons, #[_, head, rest]) := (← whnfR stack).getAppFnArgs | return none + match (← whnfR head).getAppFnArgs with + | (``CIL.Safety.Value.bufferSpan, #[view]) => + return some (mkAppN (mkConst ``step_ldobj_span) (premises ++ #[view, rest])) + | (``CIL.Safety.Value.span, #[reference, length]) => + return some (mkAppN (mkConst ``step_ldobj_span_value) (premises ++ #[reference, length, rest])) + | _ => return none + | _ => return none + +open Lean Meta in +/-- The step a class field instruction takes by `equation`, showing the field's owner as the + operand writes it, so facts stated over that owner still match the stepped goal. -/ +def fieldStep (field equation : Expr) : MetaM Simp.Step := do + let some (_, lhs, stepped) := (← inferType equation).eq? | return .continue + let_expr CIL.Extraction.FieldRef.mk owner _ _ _ := (← withDefault <| whnf field).consumeMData + | return .visit { expr := stepped, proof? := some equation } + let projected := mkApp (mkConst ``CIL.Extraction.FieldRef.owner) field + let shown := stepped.replace fun term => if term == projected then some owner else none + let proof ← mkExpectedTypeHint equation (← mkEq lhs shown) + return .visit { expr := shown, proof? := some proof } + +open Lean Meta in +/-- A field load steps by the equation for the field's shape, as `fieldShapeIn` finds it: a + struct field uses its checked byte offset and width, and a class field uses its slot and + the receiver on the stack. A load whose shape stays unknown steps to `fieldLoad`, + whose match a later fact can decide. -/ +simproc [cil_step] reduceFieldLoad (step _ (.ldfld _) _ _ _ _ _) := fun expression => do + let_expr CIL.Safety.step body operation pc args frame stack memory := expression + | return .continue + let_expr CIL.Instruction.ldfld field := operation | return .continue + let leading := #[body, pc, args, frame, memory, field] + let generic := mkAppN (mkConst ``step_ldfld_eq) (leading.push stack) + let (shape, proof) ← fieldShapeIn body field + match shape.getAppFnArgs with + | (``CIL.FieldShape.value, #[offset, width, signed]) => + let fallback := mkAppN (mkConst ``step_ldfld_value_eq) + (leading ++ #[offset, width, signed, proof, stack]) + let_expr List.cons _ receiver rest := stack | return ← equationStep fallback + match (← whnfR receiver).getAppFnArgs with + | (kind, #[managed]) => + if kind == ``Value.reference || kind == ``Value.readOnlyReference then + let_expr ManagedReference.address address := managed | return ← equationStep fallback + let rule := if kind == ``Value.reference then ``step_ldfld_value_reference_eq + else ``step_ldfld_value_readOnlyReference_eq + return ← equationStep (mkAppN (mkConst rule) + (leading ++ #[offset, width, signed, proof, address, rest])) + if kind == ``Value.scalar then + let_expr CIL.Value.aggregate bytes := managed | return ← equationStep fallback + let some rule := (match ← getNatValue? width with + | some 4 => some ``step_ldfld_value_aggregate_4_eq + | some 8 => some ``step_ldfld_value_aggregate_8_eq + | _ => none) | return ← equationStep fallback + return ← equationStep (mkAppN (mkConst rule) + (leading ++ #[offset, signed, proof, bytes, rest])) + return ← equationStep fallback + | _ => return ← equationStep fallback + | (``CIL.FieldShape.aggregate, #[offset, size]) => + let resolved := leading ++ #[offset, size, proof] + let fallback := mkAppN (mkConst ``step_ldfld_aggregate_eq) (resolved.push stack) + let_expr List.cons _ receiver rest := stack | return ← equationStep fallback + let_expr Value.scalar value := receiver | return ← equationStep fallback + match_expr value with + | CIL.Value.aggregate bytes => + return ← equationStep (mkAppN (mkConst ``step_ldfld_aggregate_value_eq) + (resolved ++ #[bytes, rest])) + | CIL.Value.snapshot cell bytes => + let_expr CIL.SnapshotShape.aggregate total := cell | return ← equationStep fallback + return ← equationStep (mkAppN (mkConst ``step_ldfld_aggregate_snapshot_eq) + (resolved ++ #[total, bytes, rest])) + | _ => return ← equationStep fallback + | _ => pure () + let_expr CIL.FieldShape.classSlot offset slot := shape | equationStep generic + let_expr List.cons _ receiver rest := stack | equationStep generic + let_expr Value.object reference := receiver | equationStep generic + let known? ← match (← whnfR slot).getAppFnArgs with + | (``CIL.ClassSlot.word, #[width, signed]) => + let equation? := match ← getNatValue? width with + | some 1 => some ``step_ldfld_word_1_eq + | some 2 => some ``step_ldfld_word_2_eq + | some 4 => some ``step_ldfld_word_4_eq + | some 8 => some ``step_ldfld_word_8_eq + | _ => none + pure (equation?.map fun name => + mkAppN (mkConst name) (leading ++ #[offset, signed, proof, reference, rest])) + | (``CIL.ClassSlot.reference, _) => + pure (some (mkAppN (mkConst ``step_ldfld_reference_eq) + (leading ++ #[offset, proof, reference, rest]))) + | (``CIL.ClassSlot.array, #[element]) => + pure (some (mkAppN (mkConst ``step_ldfld_array_eq) + (leading ++ #[offset, element, proof, reference, rest]))) + | _ => pure none + match known? with + | some equation => fieldStep field equation + | none => equationStep generic + +open Lean Meta in +/-- Step a field store using its checked shape. Nested structs write at the field displacement. + A class slot uses the stored value and receiver on the stack. -/ +simproc [cil_step] reduceFieldStore (step _ (.stfld _) _ _ _ _ _) := fun expression => do + let_expr CIL.Safety.step body operation pc args frame stack memory := expression + | return .continue + let_expr CIL.Instruction.stfld field := operation | return .continue + let leading := #[body, pc, args, frame, memory, field] + let generic := mkAppN (mkConst ``step_stfld_eq) (leading.push stack) + let (shape, proof) ← fieldShapeIn body field + match shape.getAppFnArgs with + | (``CIL.FieldShape.value, #[offset, width, signed]) => + let fallback := mkAppN (mkConst ``step_stfld_value_eq) + (leading ++ #[offset, width, signed, proof, stack]) + let_expr List.cons _ stored below := stack | return ← equationStep fallback + let_expr List.cons _ receiver rest := below | return ← equationStep fallback + let_expr Value.reference reference := receiver | return ← equationStep fallback + let_expr ManagedReference.address address := reference | return ← equationStep fallback + let_expr Value.scalar number := stored | return ← equationStep fallback + let some (rule, bits) := (match ← getNatValue? (← whnfR width), (← whnfR number).getAppFnArgs with + | some 1, (``CIL.Value.i32, #[bits]) => some (``step_stfld_value_i32_1_eq, bits) + | some 2, (``CIL.Value.i32, #[bits]) => some (``step_stfld_value_i32_2_eq, bits) + | some 4, (``CIL.Value.i32, #[bits]) => some (``step_stfld_value_i32_4_eq, bits) + | some 8, (``CIL.Value.i64, #[bits]) => some (``step_stfld_value_i64_eq, bits) + | _, _ => none) | return ← equationStep fallback + return ← equationStep (mkAppN (mkConst rule) + (leading ++ #[offset, signed, proof, bits, address, rest])) + | (``CIL.FieldShape.aggregate, #[offset, size]) => + return ← equationStep (mkAppN (mkConst ``step_stfld_aggregate_eq) + (leading ++ #[offset, size, proof, stack])) + | _ => pure () + let_expr CIL.FieldShape.classSlot offset slot := shape | equationStep generic + let_expr List.cons _ stored below := stack | equationStep generic + let_expr List.cons _ receiver rest := below | equationStep generic + let_expr Value.object reference := receiver | equationStep generic + let known? ← match (← whnfR slot).getAppFnArgs, stored.getAppFnArgs with + | (``CIL.ClassSlot.word, #[width, signed]), (``Value.scalar, #[number]) => + let equation? := match ← getNatValue? width, number.getAppFnArgs with + | some 1, (``CIL.Value.i32, #[bits]) => some (``step_stfld_word_i32_1_eq, bits) + | some 2, (``CIL.Value.i32, #[bits]) => some (``step_stfld_word_i32_2_eq, bits) + | some 4, (``CIL.Value.i32, #[bits]) => some (``step_stfld_word_i32_4_eq, bits) + | some 8, (``CIL.Value.i64, #[bits]) => some (``step_stfld_word_i64_eq, bits) + | _, _ => none + pure (equation?.map fun (name, bits) => + mkAppN (mkConst name) (leading ++ #[offset, signed, proof, bits, reference, rest])) + | (``CIL.ClassSlot.reference, _), (``Value.object, #[value]) => + pure (some (mkAppN (mkConst ``step_stfld_reference_eq) + (leading ++ #[offset, proof, value, reference, rest]))) + | (``CIL.ClassSlot.array, #[element]), (``Value.array, #[view]) => + pure (some (mkAppN (mkConst ``step_stfld_array_eq) + (leading ++ #[offset, element, proof, view, reference, rest]))) + | _, _ => pure none + match known? with + | some equation => fieldStep field equation + | none => equationStep generic + +open Lean Meta in +/-- A struct field address steps at its checked offset, and a class field + address to its fault, as `fieldShapeIn` finds the shape. An address whose shape stays + unknown steps to `fieldReference`. -/ +simproc [cil_step] reduceFieldAddress (step _ (.ldflda _) _ _ _ _ _) := fun expression => do + let_expr CIL.Safety.step body operation pc args frame stack memory := expression + | return .continue + let_expr CIL.Instruction.ldflda field := operation | return .continue + let leading := #[body, pc, args, frame, memory, field] + let generic := mkAppN (mkConst ``step_ldflda_eq) (leading.push stack) + let (shape, proof) ← fieldShapeIn body field + match shape.getAppFnArgs with + | (``CIL.FieldShape.value, #[offset, _, _]) | (``CIL.FieldShape.aggregate, #[offset, _]) => + -- The offset fact reduces to `some offset` once the shape fact rewrites the resolution. + let addressed ← mkCongrArg (mkConst ``CIL.FieldShape.structOffset?) proof + let resolved := mkApp (mkConst ``CIL.FieldShape.structOffset?) + (mkApp2 (mkConst ``CIL.Method.fieldShape) body field) + let expected ← mkEq resolved (← mkAppM ``Option.some #[offset]) + let addressed ← mkExpectedTypeHint addressed expected + if let (``List.cons, #[_, receiver, rest]) := (← whnfR stack).getAppFnArgs then + let (head, operands) := (← whnfR receiver).getAppFnArgs + if let #[pointer] := operands then + if let (``ManagedReference.address, #[reference]) := (← whnfR pointer).getAppFnArgs then + let equation := if head == ``Value.reference then some ``step_ldflda_reference_eq + else if head == ``Value.readOnlyReference then some ``step_ldflda_readOnlyReference_eq + else none + if let some name := equation then + return ← equationStep (mkAppN (mkConst name) + (leading ++ #[offset, addressed, reference, rest])) + return ← equationStep (mkAppN (mkConst ``step_ldflda_struct_eq) + (leading ++ #[offset, addressed, stack])) + | _ => pure () + let_expr CIL.FieldShape.classSlot offset slot := shape | equationStep generic + let_expr List.cons _ receiver rest := stack | equationStep generic + let_expr Value.object reference := receiver | equationStep generic + equationStep (mkAppN (mkConst ``step_ldflda_class_eq) + (leading ++ #[offset, slot, proof, reference, rest])) + +#print axioms step_ceq_object_eq #print axioms step_beq_object_eq -#print axioms step_bne_object_eq -#print axioms step_gtu_object_eq -#print axioms step_brzero_object_eq -#print axioms step_brnonzero_object_eq -#print axioms step_objectTest_eq -#print axioms step_nullObject_eq -#print axioms step_newReferenceArray_i32_eq -#print axioms step_newReferenceArray_i64_eq -#print axioms step_stringLiteral_eq -#print axioms step_objectField_eq -#print axioms step_setObjectField_i64_eq -#print axioms step_setObjectField_i32_eq -#print axioms step_objectField_1_eq -#print axioms step_objectField_2_eq -#print axioms step_objectField_4_eq -#print axioms step_objectField_8_eq -#print axioms step_setObjectField_i32_1_eq -#print axioms step_setObjectField_i32_2_eq -#print axioms step_setObjectField_i32_4_eq -#print axioms step_objectReferenceField_eq -#print axioms step_setObjectReferenceField_eq -#print axioms step_referenceArrayLength_eq -#print axioms step_referenceArrayLoad_eq -#print axioms step_referenceArrayStore_eq -#print axioms step_arrayField_eq -#print axioms step_setArrayField_eq +#print axioms step_bne_un_object_eq +#print axioms step_cgt_un_object_eq +#print axioms step_brfalse_object_eq +#print axioms step_brtrue_object_eq +#print axioms step_castclass_eq +#print axioms step_isinst_eq +#print axioms step_ldnull_eq +#print axioms step_newarr_i32_eq +#print axioms step_newarr_i64_eq +#print axioms allocateElements_value +#print axioms allocateElements_reference +#print axioms step_ldstr_eq +#print axioms step_ldlen_object_eq +#print axioms step_ldlen_object_null_eq +#print axioms step_ldelem_ref_i32_eq +#print axioms step_stelem_ref_i32_eq #print axioms loadArraySlot_of_access #print axioms arrayOfSlot_whole #print axioms storeArraySlot_whole #print axioms loadSlot_of_access #print axioms storeSlot_of_access +#print axioms step_ldfld_word_eq +#print axioms step_stfld_word_i64_eq +#print axioms step_stfld_word_i32_eq +#print axioms step_ldfld_reference_eq +#print axioms step_stfld_reference_eq +#print axioms step_ldfld_array_eq +#print axioms step_stfld_array_eq +#print axioms step_ldflda_class_eq +#print axioms step_ldfld_word_1_eq +#print axioms step_ldfld_word_2_eq +#print axioms step_ldfld_word_4_eq +#print axioms step_ldfld_word_8_eq +#print axioms step_stfld_word_i32_1_eq +#print axioms step_stfld_word_i32_2_eq +#print axioms step_stfld_word_i32_4_eq end CIL.Safety diff --git a/CIL/Safety/OtherStepRenaming.lean b/CIL/Safety/OtherStepRenaming.lean index 19d2292f0..b29a1eeb1 100644 --- a/CIL/Safety/OtherStepRenaming.lean +++ b/CIL/Safety/OtherStepRenaming.lean @@ -10,7 +10,7 @@ theorem Memory.Renamed.stepOther {indices : LocalIndexMap} {mapping : Allocation (related : Memory.Renamed mapping scopes source target) (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (formed : source.WellFormed) (sourceBody targetBody : CIL.Method) - (metadata : StepMetadata sourceBody targetBody) (operation : CIL.Op) + (metadata : StepMetadata sourceBody targetBody) (operation : CIL.Instruction) (family : fixedStepFamily operation = .other) (pc : Nat) (arguments stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (observable : SuccessOrThrow @@ -18,7 +18,7 @@ theorem Memory.Renamed.stepOther {indices : LocalIndexMap} {mapping : Allocation ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) (step sourceBody operation pc arguments sourceFrame stack source) (step targetBody operation pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) := by + targetFrame (stack.map (indices.value mapping)) target) := by allocation_fixed_step skip #print axioms Memory.Renamed.stepOther diff --git a/CIL/Safety/OutcomeContract.lean b/CIL/Safety/OutcomeContract.lean index 41a48bf07..be72c5095 100644 --- a/CIL/Safety/OutcomeContract.lean +++ b/CIL/Safety/OutcomeContract.lean @@ -15,13 +15,13 @@ namespace CIL.Safety /-- Exactly a thrown `exception`: fuel exhaustion, unsupported instructions and other faults never satisfy it. With no handlers, memory after a throw is unobservable. -/ def Raises (program : CIL.Program) (method : Nat) (args : List Value) (memory : Memory) - (exception : String) : Prop := + (exception : ExceptionType) : Prop := ∃ fuel fault, invoke program fuel method args memory = .error fault ∧ fault.fault = .thrown exception /-- `Raises` for a run already in progress at `pc`, used while stepping a method body. -/ def RunRaises (program : CIL.Program) (method pc : Nat) (args : List Value) (frame : Frame) - (stack : List Value) (memory : Memory) (exception : String) : Prop := + (stack : List Value) (memory : Memory) (exception : ExceptionType) : Prop := ∃ fuel fault, run program fuel method pc args frame stack memory = .error fault ∧ fault.fault = .thrown exception @@ -29,7 +29,7 @@ def RunRaises (program : CIL.Program) (method pc : Nat) (args : List Value) (fra return. -/ structure OutcomeContract (Input : Type) (Output : Type := List Value) extends MethodContract Input Output where - raises : Input → Memory → Option String + raises : Input → Memory → Option ExceptionType /-- For every input meeting the precondition, the method raises exactly the named exception, or, when none is named, returns normally as the underlying `MethodContract` requires. -/ @@ -44,7 +44,8 @@ def OutcomeContract.Holds {Input Output : Type} (c : OutcomeContract Input Outpu Confined (c.footprint input) initial final ∧ c.Returns input initial final values theorem OutcomeContract.Holds.raising {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method : Nat} {input : Input} {initial : Memory} {exception : String} + {program : CIL.Program} {method : Nat} {input : Input} {initial : Memory} + {exception : ExceptionType} (holds : c.Holds program method) (permitted : c.pre input initial) (raised : c.raises input initial = some exception) : Raises program method (c.args input) initial exception := by @@ -87,22 +88,80 @@ theorem OutcomeContract.Holds.normal {Input Output : Type} {c : OutcomeContract fun input initial permitted => WP.adequate.mpr (holds.returning permitted (never input initial permitted)) -/-- Successful or raised invocations agree at every fuel, so a computed run refutes a contract. -/ -theorem invoke_unique {program : CIL.Program} {method left right : Nat} {args : List Value} - {memory : Memory} {first second : Except LocatedFault (Memory × List Value)} - (firstSettled : Settled first) (secondSettled : Settled second) - (one : invoke program left method args memory = first) - (other : invoke program right method args memory = second) : - first = second := by - have up := invoke_mono program left right method args memory first firstSettled one - have down := invoke_mono program right left method args memory second secondSettled other - rw [Nat.add_comm] at down - exact up.symm.trans down +/-- How a settled outcome at `input` from `initial` fails `c`: where `c` names an exception, + anything but a throw of it; otherwise what `MethodContract.Violates` says. -/ +def OutcomeContract.Violates {Input Output : Type} (c : OutcomeContract Input Output) + (input : Input) (initial : Memory) (outcome : Except LocatedFault (Memory × List Value)) : + Prop := + match c.raises input initial, outcome with + | some exception, .error fault => fault.fault ≠ .thrown exception + | some _, .ok _ => True + | none, outcome => c.toMethodContract.Violates input initial outcome + +/-- A counterexample to `c`: an input and memory meeting `pre` from which the method settles + outside the contract or never terminates. It is evidence that the method violates the + contract in the model; a run that reaches an operation the model does not cover + (`unsupported`), or that exhausts one fuel budget, is no evidence and gives no `Refuted`. -/ +def OutcomeContract.Refuted {Input Output : Type} (c : OutcomeContract Input Output) + (program : CIL.Program) (method : Nat) : Prop := + ∃ input initial, c.pre input initial ∧ + Counterexample program method (c.args input) initial (c.Violates input initial) + +/-- A refuted outcome contract does not hold. -/ +theorem OutcomeContract.Refuted.not_holds {Input Output : Type} + {c : OutcomeContract Input Output} {program : CIL.Program} {method : Nat} + (refuted : c.Refuted program method) : ¬ c.Holds program method := by + intro holds + obtain ⟨input, initial, permitted, counter⟩ := refuted + cases named : c.raises input initial with + | none => + obtain ⟨fuel, final, values, ran, framed, returned⟩ := holds.returning permitted named + have wrong := counter.violates (outcome := .ok (final, values)) trivial ran + simp only [OutcomeContract.Violates, MethodContract.Violates, named] at wrong + exact wrong ⟨framed, returned⟩ + | some exception => + obtain ⟨fuel, fault, ran, thrown⟩ := holds.raising permitted named + have wrong := counter.violates (by simp [Settled, thrown]) ran + simp only [OutcomeContract.Violates, named] at wrong + exact wrong thrown + +/-- A located definite fault that `c` does not name, on an input meeting the precondition, + refutes the contract. -/ +theorem OutcomeContract.Refuted.of_fault {Input Output : Type} + {c : OutcomeContract Input Output} {program : CIL.Program} {method observed : Nat} + {input : Input} {initial : Memory} {fault : LocatedFault} + (permitted : c.pre input initial) (definite : fault.fault.definite ≠ false) + (failed : invoke program observed method (c.args input) initial = .error fault) + (wrong : c.Violates input initial (.error fault)) : c.Refuted program method := + ⟨input, initial, permitted, .inl ⟨observed, _, failed, definite, wrong⟩⟩ + +/-- A return that `c` does not allow, on an input meeting the precondition, refutes it. -/ +theorem OutcomeContract.Refuted.of_return {Input Output : Type} + {c : OutcomeContract Input Output} {program : CIL.Program} {method fuel : Nat} + {input : Input} {initial final : Memory} {values : List Value} + (permitted : c.pre input initial) + (ran : invoke program fuel method (c.args input) initial = .ok (final, values)) + (wrong : c.Violates input initial (.ok (final, values))) : c.Refuted program method := + ⟨input, initial, permitted, .inl ⟨fuel, _, ran, trivial, wrong⟩⟩ + +/-- A run that exhausts every fuel budget on an input meeting the precondition refutes the + contract, which promises a return or a throw. -/ +theorem OutcomeContract.Refuted.of_diverges {Input Output : Type} + {c : OutcomeContract Input Output} {program : CIL.Program} {method : Nat} + {input : Input} {initial : Memory} (permitted : c.pre input initial) + (diverges : Diverges program method (c.args input) initial) : + c.Refuted program method := + ⟨input, initial, permitted, .inr diverges⟩ + +#print axioms OutcomeContract.Refuted.not_holds +#print axioms OutcomeContract.Refuted.of_fault +#print axioms OutcomeContract.Refuted.of_return +#print axioms OutcomeContract.Refuted.of_diverges /-- A callee fault reaches the caller with the same fault kind; its location may be relocated to the call. -/ theorem run_call_error {program : CIL.Program} {fuel method pc callee : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack arguments rest : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {fault : LocatedFault} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .ok (.call callee arguments rest updated)) @@ -112,28 +171,28 @@ theorem run_call_error {program : CIL.Program} {fuel method pc callee : Nat} located.fault = fault.fault := by cases hb : program[callee]? with | none => - simp only [invoke, hb] at child + simp only [invoke_eq, hb] at child cases child exact ⟨⟨method, pc, .invalidState⟩, by - simp only [run, methodFound, instructionFound, stepped, hb, Except.mapError, Bind.bind, + simp only [run_succ, methodFound, instructionFound, stepped, hb, Except.mapError, Bind.bind, Except.bind], rfl⟩ | some childBody => cases he : enterFrame childBody arguments updated with | error entry => - simp only [invoke, hb, checked, he, Except.mapError, Bind.bind, Except.bind] at child + simp only [invoke_eq, hb, checked, he, Except.mapError, Bind.bind, Except.bind] at child cases child exact ⟨⟨method, pc, entry⟩, by - simp only [run, methodFound, instructionFound, stepped, hb, he, Except.mapError, + simp only [run_succ, methodFound, instructionFound, stepped, hb, he, Except.mapError, Bind.bind, Except.bind], rfl⟩ | ok entered => obtain ⟨childFrame, childMemory⟩ := entered - simp only [invoke, hb, checked, he, Except.mapError, Bind.bind, Except.bind] at child - exact ⟨fault, by simp only [run, methodFound, instructionFound, stepped, hb, he, child, + simp only [invoke_eq, hb, checked, he, Except.mapError, Bind.bind, Except.bind] at child + exact ⟨fault, by simp only [run_succ, methodFound, instructionFound, stepped, hb, he, child, Except.mapError, Bind.bind, Except.bind], rfl⟩ /-- A method raises when its body, started in the frame `invoke` would set up, raises. -/ theorem raises_of_run {program : CIL.Program} {method : Nat} {body : CIL.Method} - {args : List Value} {frame : Frame} {memory entered : Memory} {exception : String} + {args : List Value} {frame : Frame} {memory entered : Memory} {exception : ExceptionType} (found : program[method]? = some body) (checked : args.mapM (checkedValue memory) = .ok args) (setup : enterFrame body args memory = .ok (frame, entered)) @@ -141,13 +200,13 @@ theorem raises_of_run {program : CIL.Program} {method : Nat} {body : CIL.Method} Raises program method args memory exception := by obtain ⟨fuel, fault, ran, thrown⟩ := raised refine ⟨fuel, fault, ?_, thrown⟩ - simp only [invoke, found, checked, setup, Except.mapError, Bind.bind, Except.bind] + simp only [invoke_eq, found, checked, setup, Except.mapError, Bind.bind, Except.bind] exact ran /-- A run raises if it raises after one ordinary step. -/ theorem run_next_raises {program : CIL.Program} {method pc target : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} - {frame nextFrame : Frame} {memory updated : Memory} {exception : String} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} + {frame nextFrame : Frame} {memory updated : Memory} {exception : ExceptionType} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .ok (.next target values nextFrame updated)) @@ -158,20 +217,21 @@ theorem run_next_raises {program : CIL.Program} {method pc target : Nat} /-- A run raises at a step that throws. -/ theorem run_throw_raises {program : CIL.Program} {method pc : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack : List Value} {frame : Frame} {memory : Memory} - {exception : String} + {body : CIL.Method} {op : CIL.Instruction} {args stack : List Value} {frame : Frame} + {memory : Memory} + {exception : ExceptionType} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .error (.thrown exception)) : RunRaises program method pc args frame stack memory exception := ⟨1, ⟨method, pc, .thrown exception⟩, by - simp only [run, methodFound, instructionFound, stepped, Except.mapError, Bind.bind, + simp only [run_succ, methodFound, instructionFound, stepped, Except.mapError, Bind.bind, Except.bind], rfl⟩ /-- Propagate the exception a callee's contract names for this input. -/ theorem run_call_raises_of_contract {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} - {exception : String} + {exception : ExceptionType} (holds : c.Holds program callee) (input : Input) (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = @@ -188,7 +248,7 @@ theorem run_call_raises_of_contract {Input Output : Type} {c : OutcomeContract I /-- Continue after a callee whose contract names no exception for this input. -/ theorem run_call_of_outcome {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (input : Input) @@ -216,9 +276,9 @@ theorem run_call_of_outcome {Input Output : Type} {c : OutcomeContract Input Out /-- Continue after a callee whose contract names no exception into a caller path that raises. -/ theorem run_call_raises_after_outcome {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} - {exception : String} + {exception : ExceptionType} (holds : c.Holds program callee) (input : Input) (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = @@ -237,14 +297,14 @@ theorem run_call_raises_after_outcome {Input Output : Type} {c : OutcomeContract (.ok (final, c.results output)) trivial invoked have parent := run_mono program parentFuel childFuel method (pc + 1) args frame _ final (.error fault) - (by simp [Settled, thrown]) resumed + (by simp [Stable, thrown]) resumed rw [Nat.add_comm parentFuel childFuel] at parent exact ⟨childFuel + parentFuel + 1, fault, run_call_of_invoke methodFound instructionFound stepped child parent, thrown⟩ /-- `run_call_of_outcome` from a well-formed memory, with the frame invariants supplied. -/ theorem run_call_of_outcome_formed {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (input : Input) @@ -273,9 +333,9 @@ theorem run_call_of_outcome_formed {Input Output : Type} {c : OutcomeContract In supplied. -/ theorem run_call_raises_after_outcome_formed {Input Output : Type} {c : OutcomeContract Input Output} {program : CIL.Program} - {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} - {exception : String} + {exception : ExceptionType} (holds : c.Holds program callee) (input : Input) (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = @@ -380,7 +440,6 @@ theorem OutcomeContract.Holds.weaken {Input Output : Type} #print axioms run_call_raises_after_outcome #print axioms run_call_raises_of_contract #print axioms run_call_of_outcome -#print axioms invoke_unique /-- Step a `Raises` or `RunRaises` goal through checked instructions to a `throw`, leaving each `.call` for `cil_outcome_raise`. -/ @@ -392,15 +451,16 @@ syntax "cil_raise_run_step" "[" term,* "]" : tactic open Lean Elab Tactic in elab_rules : tactic - | `(tactic| cil_raise_run_step [$facts:term,*]) => withMainContext do + | `(tactic| cil_raise_run_step [$facts:term,*]) => withNormalNodes facts.getElems do + withMainContext do let (methodSyntax, instructionSyntax, operation) ← cilFetchedOp ``CIL.Safety.run_next_raises some 4 facts.getElems - evalTactic (← if operation == ``CIL.Op.throw then + evalTactic (← if operation == ``CIL.Instruction.throw then `(tactic| cil_strict exact CIL.Safety.run_throw_raises $methodSyntax $instructionSyntax rfl) else `(tactic| ( - cil_strict refine CIL.Safety.run_next_raises $methodSyntax $instructionSyntax + cil_strict cil_refine_by CIL.Safety.run_next_raises $methodSyntax $instructionSyntax (by cil_checked_step_normalized [$facts,*]) ?_ cil_stack_cleanup))) @@ -413,9 +473,10 @@ macro_rules (by cil_lookup [$[$facts:term],*]) (by cil_checked_side [$[$facts:term],*]) (by cil_checked_side [$[$facts:term],*]) ?_) - cil_steps_from "cil_raise_steps took no step: the method or instruction lookup, or the \ - first step, did not close; pass the lookup hypothesis, feature facts or step facts" - cil_raise_run_step [$[$facts:term],*])) + cil_normal_run + cil_steps_from "cil_raise_steps took no step: the method or instruction lookup, or the \ + first step, did not close; pass the lookup hypothesis, feature facts or step facts" + cil_raise_run_step [$[$facts:term],*])) /-- Step a `Raises` or `RunRaises` goal to a `throw`, after normalising the step facts. Use on a raising path; each `.call` on the way is left for `cil_outcome_raise` or diff --git a/CIL/Safety/ProfileContract.lean b/CIL/Safety/ProfileContract.lean index b0d3969e2..f907ee542 100644 --- a/CIL/Safety/ProfileContract.lean +++ b/CIL/Safety/ProfileContract.lean @@ -1,3 +1,4 @@ +import CIL.Safety.DecodedSummary import CIL.Safety.ProfileCertificate import CIL.Safety.RuntimeExceptions @@ -34,6 +35,40 @@ theorem MethodContract.Holds.reprofile {Input Output : Type} {c : MethodContract rw [invoke_uniform_reprofile_fun program p q uniform agreement] exact holds +open CIL.Extraction in +/-- Decoding the same evidence at a second profile preserves the method's pinned identity. -/ +theorem DecodedAt.reprofile {program : CIL.Program} {evidence : List MethodEvidence} + {source target : FeatureProfile} {environment : LayoutEnvironment} {index token : Nat} + {pin : AssemblyPin} {arguments : List TypeExpr} {imports : List ImportSlot} + (bound : DecodedAt program evidence source environment index pin token arguments) + (decoded : decodedProgram? target environment evidence imports = + some (CIL.reprofile program target)) : + DecodedAt (CIL.reprofile program target) evidence target environment index pin token + arguments := by + obtain ⟨item, found, rfl, rfl, rfl, instantiated, body, _⟩ := bound + exact .of_decoded decoded found body instantiated + +#print axioms DecodedAt.reprofile + +open CIL.Extraction in +/-- A decoded summary carries to an agreeing profile when the same evidence decodes to the + reprofiled program. The target summary retains the pinned assembly, token and arguments. -/ +theorem DecodedSummary.reprofile {Input Output : Type} {contract : MethodContract Input Output} + {program : CIL.Program} {evidence : List MethodEvidence} {source target : CIL.FeatureProfile} + {environment : LayoutEnvironment} {index token : Nat} {pin : AssemblyPin} + {arguments : List TypeExpr} {imports : List CIL.ImportSlot} + (summary : DecodedSummary program evidence source environment index pin token arguments + contract) + (uniform : ∀ body ∈ program, body.profile = source) + (agreement : program.ProfileAgreement source target) + (decoded : decodedProgram? target environment evidence imports = + some (CIL.reprofile program target)) : + DecodedSummary (CIL.reprofile program target) evidence target environment index pin token + arguments contract := + ⟨summary.holds.reprofile source uniform agreement, summary.bound.reprofile decoded⟩ + +#print axioms DecodedSummary.reprofile + /-- `MethodContract.Holds.reprofile` for a contract that reads the program's static descriptors: reprofiling keeps them, so the same contract certifies the target. `contract` is explicit because unification cannot recover it from its applications. -/ @@ -57,6 +92,25 @@ theorem OutcomeContract.Holds.reprofile {Input Output : Type} {c : OutcomeContra rw [invoke_uniform_reprofile_fun program p q uniform agreement] exact holds +open CIL.Extraction in +/-- Both normal and exceptional outcomes carry to an agreeing profile when the target + program decodes from the same pinned evidence. -/ +theorem DecodedOutcomeSummary.reprofile {Input Output : Type} + {contract : OutcomeContract Input Output} + {program : CIL.Program} {evidence : List MethodEvidence} {source target : FeatureProfile} + {environment : LayoutEnvironment} {index token : Nat} {pin : AssemblyPin} + {arguments : List TypeExpr} {imports : List ImportSlot} + (summary : DecodedOutcomeSummary program evidence source environment index pin token arguments + contract) (uniform : ∀ body ∈ program, body.profile = source) + (agreement : program.ProfileAgreement source target) + (decoded : decodedProgram? target environment evidence imports = + some (CIL.reprofile program target)) : + DecodedOutcomeSummary (CIL.reprofile program target) evidence target environment index pin token + arguments contract := + ⟨summary.holds.reprofile source uniform agreement, summary.bound.reprofile decoded⟩ + +#print axioms DecodedOutcomeSummary.reprofile + /-- `OutcomeContract.Holds.reprofile` for a contract that reads the program's static descriptors, with `contract` explicit as in `MethodContract.Holds.reprofile_statics`. -/ theorem OutcomeContract.Holds.reprofile_statics {Input Output : Type} diff --git a/CIL/Safety/ProfileEquivalence.lean b/CIL/Safety/ProfileEquivalence.lean index 870fccf39..46fde215b 100644 --- a/CIL/Safety/ProfileEquivalence.lean +++ b/CIL/Safety/ProfileEquivalence.lean @@ -5,7 +5,7 @@ import CIL.Safety.Execution # Checked execution across feature profiles The checked interpreter gives the same result for a program under two feature profiles -whenever every instruction agrees on them (`CIL.Op.ProfileAgreement`): only feature tests +whenever every instruction agrees on them (`CIL.Instruction.ProfileAgreement`): only feature tests and intrinsics consult the profile. `CIL.Safety.ProfileCertificate` lifts this to invocation certificates and `CIL.Safety.ProfileContract` to method and outcome contracts. -/ @@ -16,19 +16,54 @@ theorem enterFrame_profile (body : CIL.Method) (profile : CIL.FeatureProfile) (args : List Value) (memory : Memory) : enterFrame { body with profile } args memory = enterFrame body args memory := rfl +/-- A call without a link runs the same under two profiles that agree on the query of the + model its method has, if any. -/ +theorem modelStep_profile_eq {opcode : CIL.CallOpcode} (body : CIL.Method) + (method : Extraction.MethodRef) (p q : CIL.FeatureProfile) + (agreement : ∀ model, Extraction.runtimeModel body.environment opcode method = some model → + model.ProfileAgreement p q) + (pc : Nat) (frame : Frame) (stack : List Value) (memory : Memory) : + modelStep (Extraction.runtimeModel body.environment opcode method) { body with profile := p } + pc frame stack memory = + modelStep (Extraction.runtimeModel body.environment opcode method) + { body with profile := q } pc frame stack memory := by + cases found : Extraction.runtimeModel body.environment opcode method with + | none => rfl + | some model => + have agrees := agreement model found + cases model <;> try rfl + all_goals simp only [CIL.RuntimeModel.ProfileAgreement] at agrees + all_goals simp only [modelStep, runModel, intrinsicCall, featureCall, agrees] + /-- A checked step does not depend on the method's profile when the instruction agrees on both profiles. -/ -theorem step_profile_eq (body : CIL.Method) (op : CIL.Op) (p q : CIL.FeatureProfile) - (agreement : op.ProfileAgreement p q) (pc : Nat) (args : List Value) +theorem step_profile_eq (body : CIL.Method) (op : CIL.Instruction) (p q : CIL.FeatureProfile) + (agreement : op.ProfileAgreement body.environment p q) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (memory : Memory) : step { body with profile := p } op pc args frame stack memory = step { body with profile := q } op pc args frame stack memory := by - unfold step + have reprofiled : ∀ profile, ({ body with profile } : CIL.Method).linkAt pc = body.linkAt pc := + fun _ => rfl + have construction (constructor : Extraction.MethodRef) + (agrees : (CIL.Instruction.newobj constructor).ProfileAgreement body.environment p q) : + newobjStep (body.linkAt pc) constructor { body with profile := p } pc frame stack memory = + newobjStep (body.linkAt pc) constructor { body with profile := q } pc frame stack + memory := by + cases body.linkAt pc with + | none => exact modelStep_profile_eq (opcode := .newobj) body constructor p q agrees .. + | some link => cases link <;> rfl + have baseStep := CIL.step_profile_eq body.environment op p q agreement + unfold step referenceCompareCall referenceSubtractCall + simp only [reprofiled] split <;> try rfl all_goals split <;> try rfl - all_goals simp only [CIL.Op.ProfileAgreement] at agreement + all_goals try exact construction _ agreement + all_goals split <;> try rfl + all_goals try exact modelStep_profile_eq _ _ _ _ agreement .. + all_goals try (split <;> first | rfl | exact modelStep_profile_eq _ _ _ _ agreement ..) + all_goals simp only [CIL.Instruction.ProfileAgreement] at agreement all_goals try simp only [agreement] - all_goals simp only [CIL.step_profile_eq op p q agreement] + all_goals simp only [baseStep] /-- Running a reprofiled program gives the same result, fault or success, under any two profiles the program agrees on. -/ @@ -41,15 +76,15 @@ theorem run_reprofile_eq (program : CIL.Program) (p q : CIL.FeatureProfile) | zero => rfl | succ fuel ih => cases hb : program[method]? with - | none => simp [run, hb] + | none => simp [run_succ, hb] | some body => cases ho : body.code[pc]? with - | none => simp [run, hb, ho] + | none => simp [run_succ, hb, ho] | some op => have same := step_profile_eq body op p q (agreement body (List.mem_of_getElem? hb) op (List.mem_of_getElem? ho)) pc args frame stack memory - simp only [run, CIL.reprofile_lookup, hb, Option.map_some, ho] + simp only [run_succ, CIL.reprofile_lookup, hb, Option.map_some, ho] rw [same] cases haction : step { body with profile := q } op pc args frame stack memory with | error fault => rfl @@ -86,9 +121,9 @@ theorem invoke_reprofile_eq (program : CIL.Program) (p q : CIL.FeatureProfile) invoke (CIL.reprofile program p) fuel method args memory = invoke (CIL.reprofile program q) fuel method args memory := by cases hb : program[method]? with - | none => simp [invoke, hb] + | none => simp [invoke_eq, hb] | some body => - simp only [invoke, CIL.reprofile_lookup, hb, Option.map_some, enterFrame_profile] + simp only [invoke_eq, CIL.reprofile_lookup, hb, Option.map_some, enterFrame_profile] simp only [run_reprofile_eq program p q agreement] /-- A program whose methods all use profile `p` behaves exactly as its reprofiling to `q`. -/ @@ -107,4 +142,5 @@ theorem invoke_uniform_reprofile_eq (program : CIL.Program) (p q : CIL.FeaturePr #print axioms run_reprofile_eq #print axioms enterFrame_profile #print axioms step_profile_eq +#print axioms modelStep_profile_eq end CIL.Safety diff --git a/CIL/Safety/ProgramClosure.lean b/CIL/Safety/ProgramClosure.lean new file mode 100644 index 000000000..90d299f3e --- /dev/null +++ b/CIL/Safety/ProgramClosure.lean @@ -0,0 +1,545 @@ +import CIL.Safety.Linking +import CIL.Safety.DecodedSummary + +/-! +# Closures of a method and their transfer between programs + +`Program.closure` takes out of a program the methods a root reaches through link tables, renumbered +from the root at 0, and keeps them only when every link of the renumbered methods enters one of +them. The result is closed (`Program.closure_closed`) and relocates back into its program +(`Program.closure_relocates`), and `Program.closureEvidence` gives the evidence records that decode +to it. `Program.closure` reads each method from the list; `closureWith` takes any lookup, and +`Program.closure_eq_closureWith` lets a proof decide a closure over an index of the program, such as +`Extracted.programIndex`, instead. A contract proved for a program carries to its closure +(`MethodContract.Holds.restrict`), and `DecodedSummary.relocate` carries a summary proved over a +closure to any program that holds the closure's methods at relocated indices +(`RelocatesInto.of_decoded`). +-/ + +namespace CIL +open CIL.Extraction + +/-- Relocating twice renames by the composition. -/ +theorem Link.relocate_relocate (f g : Nat → Nat) (link : Link) : + (link.relocate g).relocate f = link.relocate (f ∘ g) := by + cases link <;> simp [Link.relocate, List.map_map, Function.comp_def, ManagedTarget.relocate] + +#print axioms Link.relocate_relocate + +/-- A renaming that fixes every method a link enters leaves the link unchanged. -/ +theorem Link.relocate_of_fixes {f : Nat → Nat} {link : Link} + (fixes : ∀ callee ∈ link.callees, f callee = callee) : link.relocate f = link := by + cases link with + | address method => rfl + | direct method => simp_all [Link.relocate, Link.callees] + | aggregate constructor size => simp_all [Link.relocate, Link.callees] + | object constructor bases size => simp_all [Link.relocate, Link.callees] + | virtual targets => + simp only [Link.callees, List.mem_map] at fixes + simp only [Link.relocate, Link.virtual.injEq] + conv => rhs; rw [← List.map_id targets] + refine List.map_congr_left fun target member => ?_ + simp [fixes target.2 ⟨target, member, rfl⟩] + | indirect catalog => + simp only [Link.callees, List.mem_map] at fixes + simp only [Link.relocate, Link.indirect.injEq] + conv => rhs; rw [← List.map_id catalog] + refine List.map_congr_left fun target member => ?_ + simp [ManagedTarget.relocate, fixes target.method ⟨target, member, rfl⟩] + +#print axioms Link.relocate_of_fixes + +/-- Relocating a body twice renames its links by the composition. -/ +theorem Method.relocate_relocate (f g : Nat → Nat) (body : Method) : + (body.relocate g).relocate f = body.relocate (f ∘ g) := by + simp [Method.relocate, List.map_map, Function.comp_def, Link.relocate_relocate] + +#print axioms Method.relocate_relocate + +/-- The method indices the link table of `body` enters, in table order. -/ +def Method.callees (body : Method) : List Nat := + body.links.flatMap fun entry => entry.2.callees + +/-- A renaming that fixes every method a body's links enter leaves the body unchanged. -/ +theorem Method.relocate_of_fixes {f : Nat → Nat} {body : Method} + (fixes : ∀ callee ∈ body.callees, f callee = callee) : body.relocate f = body := by + have same : body.links.map (fun entry => (entry.1, entry.2.relocate f)) = body.links := by + conv => rhs; rw [← List.map_id body.links] + refine List.map_congr_left fun entry member => ?_ + rw [Link.relocate_of_fixes fun callee listed => + fixes callee (List.mem_flatMap.mpr ⟨entry, member, listed⟩)] + rfl + simp only [Method.relocate, same] + +#print axioms Method.relocate_of_fixes + +/-- A relocated body enters the renamed callees of the original. -/ +theorem Method.callees_relocate (f : Nat → Nat) (body : Method) : + (body.relocate f).callees = body.callees.map f := by + simp [Method.callees, Method.relocate, List.flatMap_map, Link.callees_relocate, + List.map_flatMap] + +#print axioms Method.callees_relocate + +/-- Relocating the links preserves their occupied pcs, so raw decoding relocates the method. -/ +theorem Method.ofRaw?_relocate (f : Nat → Nat) (profile : FeatureProfile) + (environment : LayoutEnvironment) (links : List (Nat × Link)) (reachable : List Bool) + (source : RawMethod) (arguments : List TypeExpr) : + Method.ofRaw? profile environment (links.map fun entry => (entry.1, entry.2.relocate f)) + reachable source arguments = + (Method.ofRaw? profile environment links reachable source arguments).map + (Method.relocate f) := by + have occupied : ((links.map fun entry => (entry.1, entry.2.relocate f)).map Prod.fst) = + links.map Prod.fst := by simp [List.map_map, Function.comp_def] + have sameCode (closed : RawMethod) : + methodCode profile environment (links.map fun e => (e.1, e.2.relocate f)) reachable closed = + methodCode profile environment links reachable closed := by + unfold methodCode + rw [occupied] + simp only [Method.ofRaw?, bind, Option.map_bind] + refine congrArg _ (funext fun closed => ?_) + simp only [Function.comp_apply, Option.map_bind] + refine congrArg _ (funext fun storage => ?_) + simp only [Function.comp_apply, Method.ofClosed?] + rw [sameCode] + generalize methodCode profile environment links reachable closed = code + cases firstLiveHole 0 reachable code <;> simp only + all_goals repeat' split + all_goals rfl + +#print axioms Method.ofRaw?_relocate + +/-- Bytes decoded with a relocated link table give the relocated method. -/ +theorem Method.ofBody?_relocate (f : Nat → Nat) (profile : FeatureProfile) + (environment : LayoutEnvironment) (links : List (Nat × Link)) (reachable : List Bool) + (body : BodyEvidence) (original : RawMethod) (arguments : List TypeExpr) : + Method.ofBody? profile environment (links.map fun entry => (entry.1, entry.2.relocate f)) + reachable body original arguments = + (Method.ofBody? profile environment links reachable body original arguments).map + (Method.relocate f) := by + simp only [Method.ofBody?, bind] + cases (checkBody body original arguments).toOption with + | none => rfl + | some decoded => + simp only [Option.bind_some] + exact Method.ofRaw?_relocate f profile environment links reachable _ arguments + +#print axioms Method.ofBody?_relocate + +/-- The record with its executable targets renamed by `f` and its source address bindings. -/ +def Extraction.MethodEvidence.relocateLinks (f : Nat → Nat) (item : MethodEvidence) : + MethodEvidence := + { item with links := item.links.map fun entry => (entry.1, entry.2.relocate f) } + +/-- A record with relocated links decodes to the relocated method of the record. -/ +theorem Extraction.MethodEvidence.ofBody?_relocateLinks (f : Nat → Nat) (profile : FeatureProfile) + (environment : LayoutEnvironment) (item : MethodEvidence) : + Method.ofBody? profile environment (item.relocateLinks f).links + (item.relocateLinks f).reachable (item.relocateLinks f).body + (item.relocateLinks f).original (item.relocateLinks f).arguments = + (Method.ofBody? profile environment item.links item.reachable item.body item.original + item.arguments).map (Method.relocate f) := + Method.ofBody?_relocate f profile environment item.links item.reachable item.body item.original + item.arguments + +#print axioms Extraction.MethodEvidence.ofBody?_relocateLinks + +/-- Slot `slot` of a table packing `width`-bit entries into one number. -/ +def packedAt (width table slot : Nat) : Nat := + (table >>> (slot * width)) % 2 ^ width + +/-- `table` with `value` written into slot `slot`, which must be zero and `value` below + `2 ^ width`. -/ +def packedSet (width table slot value : Nat) : Nat := + table ||| (value <<< (slot * width)) + +/-- A breadth-first search of the methods a root reaches. `slots` packs, per program index, one + more than its position in the closure (zero when unreached); `indices` packs, per position, + the program index; `found` lists the visited methods, last visited first; `front` and the + reversed `back` queue the methods given a position and not yet visited. The closure checks + every packed entry it relies on, so a wrong entry gives `none` rather than a wrong closure. -/ +structure ClosureSearch where + /-- Bits per packed entry. -/ + width : Nat + /-- Position plus one of each reached program index. -/ + slots : Nat + /-- Program index at each position. -/ + indices : Nat + /-- Positions given out. -/ + count : Nat + /-- Visited program indices and their methods, last visited first. -/ + found : List (Nat × Method) + /-- Queued indices, next first. -/ + front : List Nat + /-- Queued indices after `front`, last queued first. -/ + back : List Nat + +/-- `search` with each of `callees` in turn given the next position and queued, unless it + already has one. -/ +-- A fold over the structure costs the kernel time quadratic in the callees; matching the fields +-- at each callee keeps it linear. +def ClosureSearch.reachAll : ClosureSearch → List Nat → ClosureSearch + | search, [] => search + | ⟨width, slots, indices, count, found, front, back⟩, callee :: rest => + if packedAt width slots callee != 0 then + reachAll ⟨width, slots, indices, count, found, front, back⟩ rest + else + reachAll ⟨width, packedSet width slots callee (count + 1), + packedSet width indices count callee, count + 1, found, front, callee :: back⟩ rest + +/-- `search` after visiting the method `lookup` gives at `index`, with `front` left in the + queue; with nothing queued when `lookup` gives no method there. -/ +def ClosureSearch.visit (lookup : Nat → Option Method) (search : ClosureSearch) (index : Nat) + (front : List Nat) : ClosureSearch := + match lookup index with + | some method => + ClosureSearch.reachAll { search with front := front, found := (index, method) :: search.found } + method.callees + | none => { search with front := [], back := [] } + +/-- `search` after visiting at most `fuel` queued methods `lookup` gives. -/ +def ClosureSearch.run (lookup : Nat → Option Method) : Nat → ClosureSearch → ClosureSearch + | 0, search => search + | fuel + 1, search => + match search.front with + | index :: front => (search.visit lookup index front).run lookup fuel + | [] => + match search.back.reverse with + | index :: front => ({ search with back := [] }.visit lookup index front).run lookup fuel + | [] => search + +/-- The search from `root`, which holds position 0, of the `length` methods `lookup` gives. Each + visit takes a new position, so `length` visits reach every method. -/ +def closureSearchWith (lookup : Nat → Option Method) (length root : Nat) : ClosureSearch := + let width := length.log2 + 1 + ClosureSearch.run lookup length + { width, slots := packedSet width 0 root 1, indices := root, count := 1, found := [], + front := [root], back := [] } + +/-- The search of `program` from `root`, reading each method by its index in the list. -/ +def Program.closureSearch (program : Program) (root : Nat) : ClosureSearch := + closureSearchWith (program[·]?) program.length root + +/-- The position `search` gave program index `callee`. -/ +def ClosureSearch.position (search : ClosureSearch) (callee : Nat) : Nat := + packedAt search.width search.slots callee - 1 + +/-- The program index `search` placed at position `slot`. -/ +def ClosureSearch.indexAt (search : ClosureSearch) (slot : Nat) : Nat := + packedAt search.width search.indices slot + +/-- `search` visited one method per position, `root` first, places each at its own position, + and gives every method a visited method's links enter a position that holds it. -/ +def ClosureSearch.consistent (search : ClosureSearch) (root : Nat) : Bool := + let visits := search.found.reverse + visits.length == search.count && visits.head?.map Prod.fst == some root && + (visits.zipIdx.all fun (visited, slot) => search.indexAt slot == visited.1) && + visits.all fun visited => visited.2.callees.all fun callee => + search.position callee < search.count && search.indexAt (search.position callee) == callee + +/-- The methods `root` reaches through link tables among the `length` methods `lookup` gives, + renumbered from `root` at 0, with the index of each; `none` unless every link of the + renumbered methods enters one of them. A search over `n` methods with `e` link callees reads + each method once through `lookup` and gives each callee one packed table operation. -/ +def closureWith (lookup : Nat → Option Method) (length root : Nat) : + Option (Program × List Nat) := + let search := closureSearchWith lookup length root + if search.consistent root then + some (search.found.reverse.map fun visited => visited.2.relocate search.position, + search.found.reverse.map Prod.fst) + else none + +/-- The methods `root` reaches in `program`, as `closureWith` takes them. Read through the list, + a closure of `n` methods costs `O(n * P)` list steps in a program of `P` methods; through + an index of the program, `closure_eq_closureWith` brings that to `O(n * log P)`. -/ +def Program.closure (program : Program) (root : Nat) : Option (Program × List Nat) := + closureWith (program[·]?) program.length root + +/-- A lookup that gives each method of `program` at its index, with the program's length, takes + the same closure as the list does. -/ +theorem Program.closure_eq_closureWith {program : Program} {lookup : Nat → Option Method} + {length : Nat} (agrees : ∀ index, lookup index = program[index]?) + (counted : length = program.length) (root : Nat) : + program.closure root = closureWith lookup length root := by + have same : lookup = (program[·]?) := funext agrees + subst same counted + rfl + +#print axioms Program.closure_eq_closureWith + +/-- The closure records in method order, with their links renumbered and their feature + certificates and call bindings checked against the closure. -/ +def Program.closureEvidence (program : Program) (evidence : List MethodEvidence) (root : Nat) : + Option (List MethodEvidence) := + let search := program.closureSearch root + let visits := search.found.reverse + (visits.mapM fun visited => + evidence[visited.1]?.map (MethodEvidence.relocateLinks search.position)).filter fun records => + programBindingsValid records (visits.map fun visit => visit.2.relocate search.position) + +/-- Every method `search` visited is the method at its index of `program`. -/ +def ClosureSearch.Sound (program : Program) (search : ClosureSearch) : Prop := + ∀ visited ∈ search.found, program[visited.1]? = some visited.2 + +/-- Giving callees positions leaves the visited methods alone. -/ +theorem ClosureSearch.reachAll_found : + ∀ (search : ClosureSearch) (callees : List Nat), (search.reachAll callees).found = search.found + | _, [] => rfl + | ⟨_, _, _, _, _, _, _⟩, _ :: rest => by + unfold ClosureSearch.reachAll + split + · exact reachAll_found _ rest + · exact reachAll_found _ rest + +#print axioms ClosureSearch.reachAll_found + +/-- A visit records only the program's own method at the visited index. -/ +theorem ClosureSearch.visit_sound {program : Program} {search : ClosureSearch} + (sound : search.Sound program) (index : Nat) (front : List Nat) : + (search.visit (program[·]?) index front).Sound program := by + unfold ClosureSearch.visit + split + · next method found => + intro visited member + rw [ClosureSearch.reachAll_found] at member + rcases List.mem_cons.mp member with rfl | member + · exact found + · exact sound visited member + · exact sound + +#print axioms ClosureSearch.visit_sound + +/-- A search of `program` from a sound start visits only the program's own methods. -/ +theorem ClosureSearch.run_sound (program : Program) : + ∀ (fuel : Nat) (search : ClosureSearch), search.Sound program → + (search.run (program[·]?) fuel).Sound program + | 0, _, sound => sound + | fuel + 1, search, sound => by + unfold ClosureSearch.run + split + · exact run_sound program fuel _ (visit_sound sound _ _) + · split + · exact run_sound program fuel _ + (visit_sound (search := { search with back := [] }) sound _ _) + · exact sound + +#print axioms ClosureSearch.run_sound + +/-- A closure is read off a sound search of its program that `consistent` accepts. -/ +theorem Program.closure_eq_some {program lib : Program} {root : Nat} {indices : List Nat} + (closure : program.closure root = some (lib, indices)) : + (program.closureSearch root).Sound program ∧ + (program.closureSearch root).consistent root = true ∧ + lib = (program.closureSearch root).found.reverse.map + (fun visited => visited.2.relocate (program.closureSearch root).position) ∧ + indices = (program.closureSearch root).found.reverse.map Prod.fst := by + unfold Program.closure closureWith at closure + dsimp only at closure + split at closure + · next consistent => + obtain ⟨rfl, rfl⟩ := Prod.mk.inj (Option.some.inj closure) + exact ⟨ClosureSearch.run_sound program _ _ (fun _ member => by simp at member), consistent, + rfl, rfl⟩ + · cases closure + +#print axioms Program.closure_eq_some + +/-- What `consistent` checks, as propositions over the visits in order. -/ +theorem ClosureSearch.consistent_spec {search : ClosureSearch} {root : Nat} + (consistent : search.consistent root = true) : + search.found.reverse.length = search.count ∧ + (search.found.reverse.head?.map Prod.fst) = some root ∧ + (∀ slot (bound : slot < search.found.reverse.length), + search.indexAt slot = search.found.reverse[slot].1) ∧ + ∀ visited ∈ search.found.reverse, ∀ callee ∈ visited.2.callees, + search.position callee < search.count ∧ + search.indexAt (search.position callee) = callee := by + simp only [ClosureSearch.consistent, Bool.and_eq_true, beq_iff_eq, List.all_eq_true, + decide_eq_true_eq] at consistent + obtain ⟨⟨⟨length, first⟩, placed⟩, closed⟩ := consistent + refine ⟨length, first, fun slot bound => ?_, fun visited member callee listed => ?_⟩ + · exact placed (search.found.reverse[slot], slot) + (List.mem_zipIdx_iff_getElem?.mpr (List.getElem?_eq_getElem bound)) + · simpa using closed visited member callee listed + +#print axioms ClosureSearch.consistent_spec + +/-- Results whose items each give them through `f` give, through `g`, the images of the items. -/ +private theorem mapM_map_of {α β γ : Type} {f : α → Option β} {g : β → Option γ} + {image : α → γ} : + ∀ {items : List α} {results : List β}, items.mapM f = some results → + (∀ item ∈ items, ∀ result, f item = some result → g result = some (image item)) → + results.mapM g = some (items.map image) + | [], results, mapped, _ => by + simp only [List.mapM_nil, pure, Option.some.injEq] at mapped + subst mapped + rfl + | item :: items, results, mapped, step => by + simp only [List.mapM_cons, bind, pure, Option.bind_eq_some_iff, Option.some.injEq] at mapped + obtain ⟨first, given, rest, remaining, rfl⟩ := mapped + have tail := mapM_map_of remaining fun other member => + step other (List.mem_cons_of_mem _ member) + simp [List.mapM_cons, step item (List.mem_cons_self ..) first given, tail] + +/-- `mapM` into `Option` gives one result per item. -/ +private theorem mapM_length {α β : Type} {f : α → Option β} : + ∀ {items : List α} {results : List β}, items.mapM f = some results → + results.length = items.length + | [], results, mapped => by + simp only [List.mapM_nil, pure, Option.some.injEq] at mapped + subst mapped + rfl + | item :: items, results, mapped => by + simp only [List.mapM_cons, bind, pure, Option.bind_eq_some_iff, Option.some.injEq] at mapped + obtain ⟨first, -, rest, remaining, rfl⟩ := mapped + simp [mapM_length remaining] + +/-- The methods a closure takes out of its program enter only one another. -/ +theorem Program.closure_closed {program lib : Program} {root : Nat} {indices : List Nat} + (closure : program.closure root = some (lib, indices)) : Safety.Closed lib := by + have facts := Program.closure_eq_some closure + generalize program.closureSearch root = search at facts + obtain ⟨-, consistent, rfl, -⟩ := facts + obtain ⟨length, -, -, closed⟩ := ClosureSearch.consistent_spec consistent + intro i body found entry listed callee member + simp only [List.getElem?_map, Option.map_eq_some_iff] at found + obtain ⟨visited, visitedAt, rfl⟩ := found + have entered : callee ∈ (visited.2.relocate search.position).callees := + List.mem_flatMap.mpr ⟨entry, listed, member⟩ + rw [Method.callees_relocate, List.mem_map] at entered + obtain ⟨original, originalListed, rfl⟩ := entered + rw [List.length_map, length] + exact (closed visited (List.mem_of_getElem? visitedAt) original originalListed).1 + +#print axioms Program.closure_closed + +/-- A closure relocates back into its program, method `slot` of the closure to index + `indices[slot]`, and its first method is the root. -/ +theorem Program.closure_relocates {program lib : Program} {root : Nat} {indices : List Nat} + (closure : program.closure root = some (lib, indices)) : + Safety.RelocatesInto lib program (fun slot => indices.getD slot 0) ∧ + indices[0]? = some root := by + have facts := Program.closure_eq_some closure + generalize program.closureSearch root = search at facts + obtain ⟨sound, consistent, rfl, rfl⟩ := facts + obtain ⟨length, first, placed, closed⟩ := ClosureSearch.consistent_spec consistent + have indexed : ∀ slot (bound : slot < search.found.reverse.length), + (search.found.reverse.map Prod.fst).getD slot 0 = search.found.reverse[slot].1 := by + intro slot bound + rw [List.getD_eq_getElem?_getD, List.getElem?_map, List.getElem?_eq_getElem bound] + rfl + refine ⟨fun i bound => ?_, ?_⟩ + · rw [List.length_map] at bound + dsimp only + rw [indexed i bound, List.getElem_map, Method.relocate_relocate, + Method.relocate_of_fixes fun callee listed => ?_] + · exact sound _ (List.mem_reverse.mp (List.getElem_mem bound)) + · obtain ⟨below, lands⟩ := closed _ (List.getElem_mem bound) callee listed + rw [Function.comp_apply, indexed _ (length ▸ below), ← placed _ (length ▸ below), lands] + · rw [List.getElem?_map, ← List.head?_eq_getElem?] + exact first + +#print axioms Program.closure_relocates + +/-- The records of a closure, read from the records that decode to its program, decode to the + closure. -/ +theorem Program.closureEvidence_decodes {profile : FeatureProfile} + {environment : LayoutEnvironment} {evidence libEvidence : List MethodEvidence} + {program lib : Program} {root : Nat} {indices : List Nat} + (decoded : decodedProgram? profile environment evidence = some program) + (closure : program.closure root = some (lib, indices)) + (records : program.closureEvidence evidence root = some libEvidence) : + decodedProgram? profile environment libEvidence = some lib := by + have facts := Program.closure_eq_some closure + unfold Program.closureEvidence at records + dsimp only at records + generalize program.closureSearch root = search at facts records + obtain ⟨sound, -, rfl, -⟩ := facts + obtain ⟨mapped, masks⟩ := Option.filter_eq_some_iff.mp records + apply Option.filter_eq_some_iff.mpr + refine ⟨?_, masks⟩ + refine mapM_map_of mapped fun visited member item relocated => ?_ + obtain ⟨original, recorded, rfl⟩ := Option.map_eq_some_iff.mp relocated + rw [MethodEvidence.ofBody?_relocateLinks, Safety.decodedProgram?_getElem decoded recorded, + sound visited (List.mem_reverse.mp member)] + rfl + +#print axioms Program.closureEvidence_decodes + +end CIL + +namespace CIL.Safety +open CIL.Extraction + +/-- A contract that holds where a closed library relocates into a program holds in the library: + the program runs the library's method exactly as the library does. -/ +theorem MethodContract.Holds.restrict {Input Output : Type} {c : MethodContract Input Output} + {lib linked : CIL.Program} {f : Nat → Nat} {method : Nat} (holds : c.Holds linked (f method)) + (closed : Closed lib) (relocates : RelocatesInto lib linked f) (bound : method < lib.length) : + c.Holds lib method := by + intro input initial permitted + obtain ⟨fuel, ⟨final, values⟩, invoked, framed, returns⟩ := holds input initial permitted + refine ⟨fuel, (final, values), ?_, framed, returns⟩ + dsimp only [Execution] at invoked ⊢ + rw [invoke_relocate closed relocates fuel method _ initial bound] at invoked + cases ran : invoke lib fuel method (c.args input) initial <;> + simp_all [Except.mapError] + +#print axioms MethodContract.Holds.restrict + +/-- Records whose methods decode, at the indices `f` gives, to the relocated methods of a + library's records place the library in `target`. A map sending two library methods to one + index is accepted when both decode to the method there, which is sound: a call into either + runs that method, the one both denote. -/ +theorem RelocatesInto.of_decoded {profile : FeatureProfile} {environment : LayoutEnvironment} + {lib target : CIL.Program} {libEvidence targetEvidence : List MethodEvidence} {f : Nat → Nat} + (libDecoded : decodedProgram? profile environment libEvidence = some lib) + (targetDecoded : decodedProgram? profile environment targetEvidence = some target) + (same : ∀ i, i < libEvidence.length → ∃ item moved, libEvidence[i]? = some item ∧ + targetEvidence[f i]? = some moved ∧ + Method.ofBody? profile environment moved.links moved.reachable moved.body moved.original + moved.arguments = + (Method.ofBody? profile environment item.links item.reachable item.body item.original + item.arguments).map (Method.relocate f)) : + RelocatesInto lib target f := by + intro i bound + have length : libEvidence.length = lib.length := by + exact (mapM_length (Option.filter_eq_some_iff.mp libDecoded).1).symm + obtain ⟨item, moved, recorded, placed, decodes⟩ := same i (length ▸ bound) + rw [← decodedProgram?_getElem targetDecoded placed, decodes, + decodedProgram?_getElem libDecoded recorded, List.getElem?_eq_getElem bound] + rfl + +#print axioms RelocatesInto.of_decoded + +/-- A summary proved over a closed library is a summary of the method `f` places in `target`, + bound to `target`'s record there, given that record names the same pinned method at the same + type arguments. The bytes of that record need not be the library's: `relocates` already + states that they decode to the relocated method. `f` may send two library methods to one + index when both decode to the method there, which is sound because a call into either runs + the method both denote. -/ +theorem DecodedSummary.relocate {Input Output : Type} {contract : MethodContract Input Output} + {lib target : CIL.Program} {libEvidence targetEvidence : List MethodEvidence} + {profile : FeatureProfile} {environment : LayoutEnvironment} {f : Nat → Nat} + {index token : Nat} {pin : AssemblyPin} {arguments : List TypeExpr} + (summary : DecodedSummary lib libEvidence profile environment index pin token arguments + contract) + (closed : Closed lib) (relocates : RelocatesInto lib target f) + (targetDecoded : decodedProgram? profile environment targetEvidence = some target) + (record : ∀ item, libEvidence[index]? = some item → ∃ moved, + targetEvidence[f index]? = some moved ∧ moved.assembly = item.assembly ∧ + moved.original = item.original ∧ moved.arguments = item.arguments ∧ + moved.closed = item.closed ∧ moved.BodyBound) : + DecodedSummary target targetEvidence profile environment (f index) pin token arguments + contract := by + obtain ⟨item, recorded, assembly, sameToken, sameArguments, instantiated, -⟩ := summary.bound + obtain ⟨moved, placed, movedAssembly, movedOriginal, movedArguments, movedClosed, movedBody⟩ := + record item recorded + rw [← movedAssembly.trans assembly, ← movedArguments.trans sameArguments, + ← sameToken, ← movedOriginal] + apply DecodedSummary.of_decoded targetDecoded placed movedBody _ (summary.holds.relocate closed relocates) + simpa only [movedOriginal, movedArguments, sameArguments, movedClosed] using instantiated + +#print axioms DecodedSummary.relocate + +end CIL.Safety diff --git a/CIL/Safety/PureStepRenaming.lean b/CIL/Safety/PureStepRenaming.lean index 76a7aa0c4..3de33b09d 100644 --- a/CIL/Safety/PureStepRenaming.lean +++ b/CIL/Safety/PureStepRenaming.lean @@ -4,33 +4,20 @@ import CIL.Safety.LocalOperandRenaming /-! # Pure step computations under allocation renaming -Argument guards, scalar decoding and arithmetic diagnostics contain no allocation identities. +Scalar decoding and arithmetic diagnostics contain no allocation identities. Their results can therefore be reused exactly while checked references are mapped separately. -/ namespace CIL.Safety +-- Stack lists are renamed by the local map of the frame that holds them. +variable {indices : LocalIndexMap} + /-- The empty relocation leaves every operand unchanged. -/ -theorem LocalIndexMap.operation_nil (operation : CIL.Op) : +theorem LocalIndexMap.operation_nil (operation : CIL.Instruction) : LocalIndexMap.nil.operation operation = operation := by cases operation <;> rfl #print axioms LocalIndexMap.operation_nil -/-- Relocating locals and references preserves the narrow-argument observation guard. -/ -theorem LocalIndexMap.argumentGuard (indices : LocalIndexMap) (mapping : AllocationRenaming) - (operation : CIL.Op) (arguments : List Value) : - (match indices.operation operation with - | .arg position (some _) => - ((arguments.map mapping.value)[position]?.map Value.hasUnspecified).getD false - | _ => false) = - (match operation with - | .arg position (some _) => (arguments[position]?.map Value.hasUnspecified).getD false - | _ => false) := by - cases operation <;> try rfl - rename_i position narrow - cases narrow <;> simp only [LocalIndexMap.operation, List.getElem?_map, - Option.map_map, Function.comp_def, mapping.hasUnspecified] -#print axioms LocalIndexMap.argumentGuard - /-- Arithmetic faults and scalar results are independent of allocation identities. -/ theorem ResultRenamed.arithmetic (mapping : AllocationRenaming) (result : Except CIL.ArithmeticFault α) : @@ -54,11 +41,22 @@ theorem AllocationRenaming.checkedIntrinsic (mapping : AllocationRenaming) | split #print axioms AllocationRenaming.checkedIntrinsic +/-- Scoped type identity has the same result or unsupported fault under every allocation map. -/ +theorem AllocationRenaming.runtimeTypeEqual (mapping : AllocationRenaming) + (left right : CIL.Extraction.TypeExpr) : + ResultRenamed mapping Eq (CIL.Safety.runtimeTypeEqual left right) + (CIL.Safety.runtimeTypeEqual left right) := by + unfold CIL.Safety.runtimeTypeEqual + split <;> first | exact .ok rfl | exact .error _ + +#print axioms AllocationRenaming.runtimeTypeEqual + /-- Scalar decoding has the same numeric checks and rejects the same reference shapes. -/ -theorem AllocationRenaming.scalarOperands (mapping : AllocationRenaming) (values : List Value) : +theorem LocalIndexMap.scalarOperands (indices : LocalIndexMap) (mapping : AllocationRenaming) + (values : List Value) : ResultRenamed mapping Eq (CIL.Safety.scalars values) - (CIL.Safety.scalars (values.map mapping.value)) := by - rw [mapping.scalars] + (CIL.Safety.scalars (values.map (indices.value mapping))) := by + rw [indices.scalars mapping] induction values with | nil => exact .ok rfl | cons value rest induction => @@ -71,7 +69,7 @@ theorem AllocationRenaming.scalarOperands (mapping : AllocationRenaming) (values subst second exact .ok rfl · exact .error .invalidState -#print axioms AllocationRenaming.scalarOperands +#print axioms LocalIndexMap.scalarOperands /-- A list consisting only of scalars has no allocation identities to bound. -/ theorem scalarValues_below (limit : Nat) (values : List CIL.Value) : @@ -88,7 +86,7 @@ theorem Memory.Renamed.scalarChecks {mapping : AllocationRenaming} ResultRenamed mapping (fun left right => right = left.map mapping.value) ((values.map Value.scalar).mapM (CIL.Safety.checkedValue source)) ((values.map Value.scalar).mapM (CIL.Safety.checkedValue target)) := by - simpa only [List.map_map, Function.comp_def, AllocationRenaming.value] using + simpa only [List.map_map, Function.comp_def, AllocationRenaming.value, LocalIndexMap.value] using related.checkedValues (values.map Value.scalar) (scalarValues_below _ values) #print axioms Memory.Renamed.scalarChecks diff --git a/CIL/Safety/ReferenceArrayElements.lean b/CIL/Safety/ReferenceArrayElements.lean index 77a0be30c..cdeef82ce 100644 --- a/CIL/Safety/ReferenceArrayElements.lean +++ b/CIL/Safety/ReferenceArrayElements.lean @@ -75,7 +75,7 @@ theorem Live.slot_outside {memory : Memory} {array : Reference} {element : CIL.Extraction.TypeExpr} {length index : Nat} (live : Live memory array element length) (outside : length ≤ index) : referenceArraySlot memory (.address array) index = - .error (.thrown "System.IndexOutOfRangeException") := by + .error (.thrown .indexOutOfRange) := by simp only [referenceArraySlot, live.located, Nat.not_lt.mpr outside, Bind.bind, Except.bind, ite_false] #print axioms Live.slot_outside @@ -255,7 +255,7 @@ theorem storableIn_mismatch {memory : Memory} {target : Reference} (elementClass : element = .object elementScope elementName) (outside : element ∉ type.lineage) : storableIn memory element (.address target) = - .error (.thrown "System.ArrayTypeMismatchException") := by + .error (.thrown .arrayTypeMismatch) := by subst elementClass simp only [storableIn, found, kind, identity, outside, checkedAt, Except.mapError, Bind.bind, Except.bind, ite_false] diff --git a/CIL/Safety/RegionRun.lean b/CIL/Safety/RegionRun.lean index 915d9704d..72122e350 100644 --- a/CIL/Safety/RegionRun.lean +++ b/CIL/Safety/RegionRun.lean @@ -11,14 +11,14 @@ two programs that agree on that region run identically from it (`run_congr_regio namespace CIL.Safety -/-- Fallthroughs, code targets and callee entries of every region instruction stay in the - region. -/ +/-- Fallthroughs and code targets of every region instruction stay in the region, and every + method a region pc's link table entry may enter is in the region from its entry. -/ def RegionClosed (program : CIL.Program) (region : Nat → Nat → Prop) : Prop := - ∀ method pc body op, region method pc → program[method]? = some body → + (∀ method pc body op, region method pc → program[method]? = some body → body.code[pc]? = some op → - (op.continues = true → region method (pc + 1)) ∧ - (∀ target ∈ op.targets, region method target) ∧ - ∀ callee ∈ op.callees, region callee 0 + (op.continues = true → region method (pc + 1)) ∧ ∀ target ∈ op.targets, region method target) ∧ + ∀ method body, program[method]? = some body → ∀ entry ∈ body.links, region method entry.1 → + ∀ callee ∈ entry.2.callees, region callee 0 /-- `other` has `program`'s methods with the same metadata and the same instruction at every region pc. -/ @@ -28,15 +28,15 @@ def AgreesOn (program other : CIL.Program) (region : Nat → Nat → Prop) : Pro ∃ body', other[method]? = some body' ∧ body' = { body with code := body'.code } ∧ body'.code[pc]? = body.code[pc]? -/-- A non-tail instruction that calls a method falls through to the next pc. -/ -theorem Op.continues_of_callee {op : CIL.Op} {callee : Nat} - (h : callee ∈ op.callees) - (notTail : ∀ argc, op ≠ .tailCall callee argc) - (notManagedTail : ∀ signature catalog, op ≠ .managed (.call signature catalog true)) : +/-- An instruction that reads its link, other than a tail call, falls through to the next pc. -/ +theorem Instruction.continues_of_linked {op : CIL.Instruction} + (linked : op.readsLink = true) + (notTail : ∀ method prefixes, op = .call method prefixes → prefixes.tail = false) + (notManagedTail : ∀ signature, op ≠ .calli signature true) : op.continues = true := by - cases op <;> simp_all [CIL.Op.callees, CIL.Op.continues] + cases op <;> simp_all [CIL.Instruction.readsLink, CIL.Instruction.continues] -#print axioms Op.continues_of_callee +#print axioms Instruction.continues_of_linked private theorem AgreesOn.found {program other : CIL.Program} {region : Nat → Nat → Prop} (agrees : AgreesOn program other region) {method pc : Nat} (inside : region method pc) : @@ -65,13 +65,17 @@ theorem run_congr_region {program other : CIL.Program} {region : Nat → Nat → | zero => rfl | succ fuel ih => rcases agrees.found inside with ⟨hb, hb'⟩ | ⟨body, body', hb, hb', same, code⟩ - · simp only [run, hb, hb'] + · simp only [run_succ, hb, hb'] rw [same] at hb' cases ho : body.code[pc]? with - | none => simp only [run, hb, hb', ← same, code, ho] + | none => simp only [run_succ, hb, hb', ← same, code, ho] | some op => - obtain ⟨continues, targets, callees⟩ := closed method pc body op inside hb ho - simp only [run, hb, hb', ← same, code, ho] + obtain ⟨continues, targets⟩ := closed.1 method pc body op inside hb ho + -- A method the step enters is listed by a link entry at this region pc. + have callees : ∀ callee ∈ body.calleesAt pc, region callee 0 := fun callee member => by + obtain ⟨entry, listed, located, member⟩ := CIL.Method.calleesAt_mem member + exact closed.2 method body hb entry listed (located ▸ inside) callee member + simp only [run_succ, hb, hb', ← same, code, ho] rw [same, show step { body with code := body'.code } op pc args frame stack m = step body op pc args frame stack m from rfl] cases hs : step body op pc args frame stack m with @@ -85,14 +89,19 @@ theorem run_congr_region {program other : CIL.Program} {region : Nat → Nat → · exact ih method _ args frame' stack' m' (targets target member) | returned result m' => rfl | call callee arguments rest m' => - have entered := step_callee hs rfl + obtain ⟨linked, entered⟩ := step_callee hs rfl have child := callees callee entered - have resume := continues (Op.continues_of_callee entered (by - intro argc equal + have resume := continues (Instruction.continues_of_linked linked (by + intro method prefixes equal subst op - obtain ⟨_, _, impossible⟩ := step_tailCall_action hs - cases impossible) (by - intro signature catalog equal + rcases step_call_action hs with ⟨_, _, chosen⟩ | ⟨_, _, _, _, _, impossible, _⟩ + · cases tail : prefixes.tail + · rfl + · simp only [tail, ite_true] at chosen + obtain ⟨_, _, impossible⟩ := chosen + cases impossible + · cases impossible) (by + intro signature equal subst op obtain ⟨_, _, _, impossible⟩ := step_managed_tail_action hs cases impossible)) @@ -109,7 +118,7 @@ theorem run_congr_region {program other : CIL.Program} {region : Nat → Nat → | error fault => rfl | ok result => exact ih method (pc + 1) args frame _ _ resume | tailCall callee arguments updated => - have entered := step_callee hs rfl + have entered := (step_callee hs rfl).2 have child := callees callee entered rcases agrees.found child with ⟨cb, cb'⟩ | ⟨cbody, cbody', cb, cb', csame, _⟩ · simp only [cb, cb'] @@ -128,14 +137,19 @@ theorem run_congr_region {program other : CIL.Program} {region : Nat → Nat → | error fault => rfl | ok result => exact ih callee 0 values result.1 [] result.2 child | construct callee arguments rest frame' temporary kind m' => - have entered := step_callee hs rfl + obtain ⟨linked, entered⟩ := step_callee hs rfl have child := callees callee entered - have resume := continues (Op.continues_of_callee entered (by - intro argc equal + have resume := continues (Instruction.continues_of_linked linked (by + intro method prefixes equal subst op - obtain ⟨_, _, impossible⟩ := step_tailCall_action hs - cases impossible) (by - intro signature catalog equal + rcases step_call_action hs with ⟨_, _, chosen⟩ | ⟨_, _, _, _, _, impossible, _⟩ + · cases tail : prefixes.tail + · rfl + · simp only [tail, ite_true] at chosen + obtain ⟨_, _, impossible⟩ := chosen + cases impossible + · cases impossible) (by + intro signature equal subst op obtain ⟨_, _, _, impossible⟩ := step_managed_tail_action hs cases impossible)) @@ -161,7 +175,7 @@ theorem run_congr_region {program other : CIL.Program} {region : Nat → Nat → /-- Replacing one method's code keeps agreement where the new code matches the old. -/ theorem AgreesOn.set {program : CIL.Program} {region : Nat → Nat → Prop} {index : Nat} {body : CIL.Method} - (found : program[index]? = some body) (code : List CIL.Op) + (found : program[index]? = some body) (code : List CIL.Instruction) (agree : ∀ pc, region index pc → code[pc]? = body.code[pc]?) : AgreesOn program (program.set index { body with code := code }) region := by refine ⟨List.length_set .., fun method pc candidate inside fetched => ?_⟩ @@ -174,7 +188,7 @@ theorem AgreesOn.set {program : CIL.Program} {region : Nat → Nat → Prop} {in · exact ⟨candidate, by rw [List.getElem?_set_ne (Ne.symm same), fetched], rfl, rfl⟩ /-- Code that agrees from `lower` on agrees at every pc at or after it. -/ -theorem code_agrees_of_drop {a b : List CIL.Op} {lower pc : Nat} +theorem code_agrees_of_drop {a b : List CIL.Instruction} {lower pc : Nat} (same : a.drop lower = b.drop lower) (after : lower ≤ pc) : a[pc]? = b[pc]? := by have := congrArg (·[pc - lower]?) same simpa only [List.getElem?_drop, Nat.add_sub_cancel' after] using this @@ -224,31 +238,40 @@ theorem indexedInStarts_eq (starts : List (Nat × Nat)) (method pc : Nat) : #print axioms indexedInStarts_eq -/-- Skip absent methods and check each instruction using the earliest included pc. -/ -def regionBodyCheck (index : Std.TreeMap Nat Nat) (method : Nat) (code : List CIL.Op) : Bool := +/-- Skip absent methods; check each instruction and each link entry against the earliest included + pc. -/ +def regionBodyCheck (index : Std.TreeMap Nat Nat) (method : Nat) (body : CIL.Method) : Bool := match index[method]? with | none => true - | some lower => code.zipIdx.all fun (op, pc) => - !(lower ≤ pc) || (op.targets.all (indexedInStarts index method) && - op.callees.all (indexedInStarts index · 0)) + | some lower => + (body.code.zipIdx.all fun (op, pc) => + !(lower ≤ pc) || op.targets.all (indexedInStarts index method)) && + body.links.all fun (pc, link) => + !(lower ≤ pc) || link.callees.all (indexedInStarts index · 0) -/-- Skipping absent methods agrees with testing membership at each instruction. -/ -theorem regionBodyCheck_eq (index : Std.TreeMap Nat Nat) (method : Nat) (code : List CIL.Op) : - regionBodyCheck index method code = (code.zipIdx.all fun (op, pc) => - !indexedInStarts index method pc || - ((!op.continues || indexedInStarts index method (pc + 1)) && - op.targets.all (indexedInStarts index method) && - op.callees.all (indexedInStarts index · 0))) := by +/-- Skipping absent methods agrees with testing membership at each instruction and entry. -/ +theorem regionBodyCheck_eq (index : Std.TreeMap Nat Nat) (method : Nat) (body : CIL.Method) : + regionBodyCheck index method body = + ((body.code.zipIdx.all fun (op, pc) => + !indexedInStarts index method pc || + ((!op.continues || indexedInStarts index method (pc + 1)) && + op.targets.all (indexedInStarts index method))) && + body.links.all fun (pc, link) => + !indexedInStarts index method pc || link.callees.all (indexedInStarts index · 0)) := by cases found : index[method]? with | none => simp [regionBodyCheck, indexedInStarts, found] | some lower => simp only [regionBodyCheck, found] congr 1 - funext (op, pc) - by_cases inside : lower ≤ pc - · have next : lower ≤ pc + 1 := by omega - simp [indexedInStarts, found, inside, next] - · simp [indexedInStarts, found, inside] + · congr 1 + funext (op, pc) + by_cases inside : lower ≤ pc + · have next : lower ≤ pc + 1 := by omega + simp [indexedInStarts, found, inside, next] + · simp [indexedInStarts, found, inside] + · congr 1 + funext (pc, link) + simp [indexedInStarts, found] #print axioms regionBodyCheck_eq @@ -256,7 +279,7 @@ theorem regionBodyCheck_eq (index : Std.TreeMap Nat Nat) (method : Nat) (code : def regionClosedCheck (program : CIL.Program) (starts : List (Nat × Nat)) : Bool := if starts.isEmpty then true else let index := regionStartsIndex starts - program.zipIdx.all fun (body, method) => regionBodyCheck index method body.code + program.zipIdx.all fun (body, method) => regionBodyCheck index method body /-- The executable check is exactly closure of the union of supplied method suffixes. -/ theorem regionClosedCheck_iff (program : CIL.Program) (starts : List (Nat × Nat)) : regionClosedCheck program starts = true ↔ @@ -268,81 +291,116 @@ theorem regionClosedCheck_iff (program : CIL.Program) (starts : List (Nat × Nat simp only [regionClosedCheck, nonempty, Bool.false_eq_true, ↓reduceIte, regionBodyCheck_eq, indexedInStarts_eq, List.all_eq_true] constructor - · intro checked method pc body op inside found fetched - have bodyCheck := checked (body, method) (List.mk_mem_zipIdx_iff_getElem?.mpr found) - have opCheck := bodyCheck (op, pc) (List.mk_mem_zipIdx_iff_getElem?.mpr fetched) - simp only [inside, Bool.not_true, Bool.false_or, Bool.and_eq_true, - Bool.or_eq_true, Bool.not_eq_true', List.all_eq_true, indexedInStarts_eq] at opCheck - refine ⟨?_, opCheck.1.2, opCheck.2⟩ - intro continues - rcases opCheck.1.1 with stopped | next - · simp_all - · exact next - · intro closed entry present instruction fetched + · intro checked + refine ⟨fun method pc body op inside found fetched => ?_, + fun method body found entry listed inside => ?_⟩ + · have bodyCheck := checked (body, method) (List.mk_mem_zipIdx_iff_getElem?.mpr found) + simp only [Bool.and_eq_true, List.all_eq_true] at bodyCheck + have opCheck := bodyCheck.1 (op, pc) (List.mk_mem_zipIdx_iff_getElem?.mpr fetched) + simp only [inside, Bool.not_true, Bool.false_or, Bool.and_eq_true, + Bool.or_eq_true, Bool.not_eq_true', List.all_eq_true, indexedInStarts_eq] at opCheck + refine ⟨?_, opCheck.2⟩ + intro continues + rcases opCheck.1 with stopped | next + · simp_all + · exact next + · have bodyCheck := checked (body, method) (List.mk_mem_zipIdx_iff_getElem?.mpr found) + simp only [Bool.and_eq_true, List.all_eq_true] at bodyCheck + rcases entry with ⟨pc, link⟩ + have entryCheck := bodyCheck.2 (pc, link) listed + simp only [inside, Bool.not_true, Bool.false_or, List.all_eq_true, + indexedInStarts_eq] at entryCheck + exact entryCheck + · intro closed entry present rcases entry with ⟨body, method⟩ - rcases instruction with ⟨op, pc⟩ have found := List.mk_mem_zipIdx_iff_getElem?.mp present - have code := List.mk_mem_zipIdx_iff_getElem?.mp fetched - by_cases inside : inStarts starts method pc = true - · obtain ⟨next, targets, callees⟩ := closed method pc body op inside found code - simp only [inside, Bool.not_true, Bool.false_or, Bool.and_eq_true, - Bool.or_eq_true, Bool.not_eq_true', List.all_eq_true, indexedInStarts_eq] - exact ⟨⟨by cases continues : op.continues <;> simp_all, targets⟩, callees⟩ - · simp [inside] + simp only [Bool.and_eq_true, List.all_eq_true] + refine ⟨fun instruction fetched => ?_, fun entry listed => ?_⟩ + · rcases instruction with ⟨op, pc⟩ + have code := List.mk_mem_zipIdx_iff_getElem?.mp fetched + by_cases inside : inStarts starts method pc = true + · obtain ⟨next, targets⟩ := closed.1 method pc body op inside found code + simp only [inside, Bool.not_true, Bool.false_or, Bool.and_eq_true, + Bool.or_eq_true, Bool.not_eq_true', List.all_eq_true, indexedInStarts_eq] + exact ⟨by cases continues : op.continues <;> simp_all, targets⟩ + · simp [inside] + · rcases entry with ⟨pc, link⟩ + by_cases inside : inStarts starts method pc = true + · simp only [inside, Bool.not_true, Bool.false_or, List.all_eq_true, indexedInStarts_eq] + exact closed.2 method body found (pc, link) listed inside + · simp [inside] #print axioms regionClosedCheck_iff -/-- Indexed closure accepts exactly the former per-start, per-position check. -/ +/-- Indexed closure accepts exactly the per-start check that scans every position and every link + entry. -/ theorem regionClosedCheck_eq_original (program : CIL.Program) (starts : List (Nat × Nat)) : regionClosedCheck program starts = (starts.all fun start => match program[start.1]? with | none => true - | some body => (List.range body.code.length).all fun pc => + | some body => ((List.range body.code.length).all fun pc => !(start.2 ≤ pc) || match body.code[pc]? with | none => true | some op => (!op.continues || inStarts starts start.1 (pc + 1)) && - op.targets.all (inStarts starts start.1) && op.callees.all (inStarts starts · 0)) := by + op.targets.all (inStarts starts start.1)) && + body.links.all fun entry => + !(start.2 ≤ entry.1) || entry.2.callees.all (inStarts starts · 0)) := by apply Bool.eq_iff_iff.mpr rw [regionClosedCheck_iff] constructor · intro closed simp only [List.all_eq_true] intro start member + have startInside : ∀ pc, start.2 ≤ pc → inStarts starts start.1 pc = true := fun pc below => by + simp only [inStarts, List.any_eq_true] + exact ⟨start, member, by simp [below]⟩ cases found : program[start.1]? with | none => rfl | some body => - simp only [List.all_eq_true] - intro pc bound - by_cases below : start.2 ≤ pc - · cases fetched : body.code[pc]? with - | none => simp - | some op => - have inside : inStarts starts start.1 pc = true := by - simp only [inStarts, List.any_eq_true] - exact ⟨start, member, by simp [below]⟩ - obtain ⟨next, targets, callees⟩ := closed _ _ _ _ inside found fetched - simp only [Bool.and_eq_true, Bool.or_eq_true, Bool.not_eq_true', - List.all_eq_true] - right - exact ⟨⟨by cases continues : op.continues <;> simp_all, targets⟩, callees⟩ - · simp [below] - · intro checked method pc body op inside found fetched - simp only [inStarts, List.any_eq_true, Bool.and_eq_true, beq_iff_eq, - decide_eq_true_eq] at inside - obtain ⟨⟨start, lower⟩, member, rfl, below⟩ := inside + simp only [Bool.and_eq_true, List.all_eq_true] + refine ⟨fun pc bound => ?_, fun entry listed => ?_⟩ + · by_cases below : start.2 ≤ pc + · cases fetched : body.code[pc]? with + | none => simp + | some op => + obtain ⟨next, targets⟩ := closed.1 _ _ _ _ (startInside pc below) found fetched + simp only [Bool.and_eq_true, Bool.or_eq_true, Bool.not_eq_true', + List.all_eq_true] + right + exact ⟨by cases continues : op.continues <;> simp_all, targets⟩ + · simp [below] + · by_cases below : start.2 ≤ entry.1 + · simp only [Bool.or_eq_true, Bool.not_eq_true', List.all_eq_true] + exact .inr (closed.2 _ _ found entry listed (startInside entry.1 below)) + · simp [below] + · intro checked simp only [List.all_eq_true] at checked - have bound : pc < body.code.length := (List.getElem?_eq_some_iff.mp fetched).1 - have scanned := checked _ member - simp only [found, List.all_eq_true, List.mem_range, Bool.or_eq_true, - Bool.not_eq_true', decide_eq_false_iff_not, Nat.not_le] at scanned - rcases scanned pc bound with early | late - · omega - · simp only [fetched, Bool.and_eq_true, Bool.or_eq_true, Bool.not_eq_true', - List.all_eq_true] at late - obtain ⟨⟨go, targets⟩, callees⟩ := late - refine ⟨fun continues => ?_, targets, callees⟩ - rcases go with stop | next - · simp_all - · exact next + refine ⟨fun method pc body op inside found fetched => ?_, + fun method body found entry listed inside => ?_⟩ + · simp only [inStarts, List.any_eq_true, Bool.and_eq_true, beq_iff_eq, + decide_eq_true_eq] at inside + obtain ⟨⟨start, lower⟩, member, rfl, below⟩ := inside + have bound : pc < body.code.length := (List.getElem?_eq_some_iff.mp fetched).1 + have scanned := checked _ member + simp only [found, Bool.and_eq_true, List.all_eq_true, List.mem_range, Bool.or_eq_true, + Bool.not_eq_true', decide_eq_false_iff_not, Nat.not_le] at scanned + rcases scanned.1 pc bound with early | late + · omega + · simp only [fetched, Bool.and_eq_true, Bool.or_eq_true, Bool.not_eq_true', + List.all_eq_true] at late + obtain ⟨go, targets⟩ := late + refine ⟨fun continues => ?_, targets⟩ + rcases go with stop | next + · simp_all + · exact next + · simp only [inStarts, List.any_eq_true, Bool.and_eq_true, beq_iff_eq, + decide_eq_true_eq] at inside + obtain ⟨⟨start, lower⟩, member, rfl, below⟩ := inside + have scanned := checked _ member + simp only [found, Bool.and_eq_true, List.all_eq_true, Bool.or_eq_true, + Bool.not_eq_true', decide_eq_false_iff_not, Nat.not_le] at scanned + rcases scanned.2 entry listed with early | late + · omega + · exact late #print axioms regionClosedCheck_eq_original /-- `regionClosedCheck` is sound for `RegionClosed`. -/ diff --git a/CIL/Safety/RelocationExecution.lean b/CIL/Safety/RelocationExecution.lean index 78838c092..21d77ce55 100644 --- a/CIL/Safety/RelocationExecution.lean +++ b/CIL/Safety/RelocationExecution.lean @@ -14,7 +14,7 @@ future allocations is proved here. -/ theorem relocation_preserves_step (m : PlacedMemory) (after : Placement) - (body : CIL.Method) (op : CIL.Op) (pc : Nat) (args : List Value) + (body : CIL.Method) (op : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) : step body op pc args frame stack (relocate m after).memory = step body op pc args frame stack m.memory := rfl diff --git a/CIL/Safety/ResolutionFacts.lean b/CIL/Safety/ResolutionFacts.lean new file mode 100644 index 000000000..39cf9ff62 --- /dev/null +++ b/CIL/Safety/ResolutionFacts.lean @@ -0,0 +1,1028 @@ +import CIL.Resolution +import CIL.InstructionEquations +import CIL.Extraction.Generics +import CIL.Extraction.RuntimeModels + +/-! +# Return, argument, static site and array element metadata facts + +Generated facts and value stepping share a proof-producing chooser for declared narrowing. +Primitive and non-candidate heads use storage lemmas, without evaluating code or layout tables. +`cil_resolution_facts` declares a method's return narrowing and, for each declared argument, +how the method reads it (`CIL.Method.argumentAccess`). The argument facts walk the parameter +types and the parameter homes once through named suffixes, as `CIL.CodeSuffix` does for code, +so a method's facts cost time linear in its arguments and homes. It also states, for each +static site, the descriptor `CIL.Method.staticAt` resolves there, from one check that the site +pcs ascend. + +For a method `m` with a raw method `mClosed`, the command also declares the shape of each +distinct array element type the raw code names (`CIL.Method.elementShape`). A struct element +reads its layout entries through named suffixes of the layout lists, shared by every method of +an environment constant, so the facts cost time linear in the layouts and the distinct types. + +`cil_field_shape` and `cil_value_shape` declare how every method of an extraction resolves one +distinct field or type operand, against the environment those methods share, so an operand used +at many sites of many methods costs one kernel check. A struct shape is checked from one entry +of each layout list, read through suffixes of the lists named by the environment's first fact, +so an extraction's facts cost time linear in its layouts and distinct operands. The `cil_code` +simprocs `reduceFieldShape` and `reduceValueShape` read a method's shape of an operand from its +fact. + +`cil_runtime_models` declares the runtime model of each modelled call site of a method, read +from the operand alone, so stepping such a call rewrites with one fact per site. +-/ + +namespace CIL.Safety.ResolutionFacts + +open Lean Meta Elab Command +open CIL.InstructionEquations + (Body instructions listElements declareSuffixes suffixBlock suffixFrom foundAt) + +/-- A recognized runtime primitive, checked by its complete scope and type name. -/ +def primitive? (type : Expr) : Option Name := do + let (``CIL.Extraction.TypeExpr.atom, #[scope, name]) := type.getAppFnArgs | none + let scopeMatches := scope.isConstOf ``CIL.Extraction.runtimeScope || + match scope with + | .lit (.strVal value) => value == CIL.Extraction.runtimeScope + | _ => false + unless scopeMatches do none + let .lit (.strVal name) := name | none + let primitive ← #[(`sbyte, "System.SByte"), (`byte, "System.Byte"), + (`boolean, "System.Boolean"), (`int16, "System.Int16"), (`uint16, "System.UInt16"), + (`char, "System.Char"), (`int32, "System.Int32"), (`uint32, "System.UInt32"), + (`int64, "System.Int64"), (`uint64, "System.UInt64"), (`single, "System.Single"), + (`double, "System.Double"), (`intPtr, "System.IntPtr"), (`uintPtr, "System.UIntPtr")].find? + (fun entry => entry.2 == name) + return ``CIL.PrimitiveType ++ primitive.1 + +/-- A storage lemma for recognized heads, or one of five checked reflexivity results. -/ +def declaredProof? (layouts type : Expr) : MetaM (Option Expr) := withDefault do + let head ← withDefault <| whnf type + if let some primitive := primitive? head then + return some (← mkAppM ``CIL.Extraction.declaredNarrow_primitive + #[layouts, mkConst primitive]) + if let (``CIL.Extraction.TypeExpr.enum, #[identity, underlying]) := head.getAppFnArgs then + if let some primitive := primitive? (← withDefault <| whnf underlying) then + return some (← mkAppM ``CIL.Extraction.declaredNarrow_enum + #[layouts, identity, mkConst primitive]) + let mut terminal := head + let mut identities := #[] + repeat + let (``CIL.Extraction.TypeExpr.enum, #[identity, underlying]) := terminal.getAppFnArgs + | break + identities := identities.push identity + terminal ← withDefault <| whnf underlying + let candidate := mkApp (mkConst ``CIL.Extraction.TypeExpr.narrowCandidate) terminal + if (← withDefault <| whnf candidate).isConstOf ``Bool.false then + let mut proof ← mkAppM ``CIL.Extraction.declaredNarrow_of_not_candidate + #[layouts, terminal, ← mkEqRefl (mkConst ``Bool.false)] + for identity in identities.reverse do + proof ← mkAppM ``CIL.Extraction.declaredNarrow_enum_of_none + #[layouts, identity, terminal, proof] + terminal := mkApp2 (mkConst ``CIL.Extraction.TypeExpr.enum) identity terminal + return some proof + unless terminal.isAppOf ``CIL.Extraction.TypeExpr.atom do return none + let narrowType := mkConst ``CIL.NarrowKind + let choices := #[mkApp (mkConst ``Option.none [.zero]) narrowType] ++ + #[``CIL.NarrowKind.signed8, ``CIL.NarrowKind.unsigned8, + ``CIL.NarrowKind.signed16, ``CIL.NarrowKind.unsigned16].map + (fun kind => mkApp2 (mkConst ``Option.some [.zero]) narrowType (mkConst kind)) + let expression := mkApp2 (mkConst ``CIL.Extraction.declaredNarrow) layouts type + for choice in choices do + if ← withoutModifyingState <| isDefEq expression choice then + return some (← mkEqRefl choice) + return none + +/-- Return narrowing from signature facts; unresolved metadata has no selected result. -/ +def returnProof? (method returns declared : Expr) : MetaM (Option Expr) := withDefault do + let some (_, _, value) := (← inferType returns).eq? | return none + let value ← withDefault <| whnf value + if value.isConstOf ``Bool.false then + return some (← mkAppOptM ``CIL.Method.returnNarrow_of_void #[some method, some returns]) + unless value.isConstOf ``Bool.true do return none + let some (_, _, signature) := (← inferType declared).eq? | return none + let projection := mkConst ``CIL.DeclaredSignature.returnType + let type ← withDefault <| whnf (mkApp projection signature) + let declaredType ← mkCongrArg projection declared + let layouts := mkApp (mkConst ``CIL.LayoutEnvironment.rawLayouts) + (mkApp (mkConst ``CIL.Method.environment) method) + let some resolved ← declaredProof? layouts type | return none + return some (← mkAppOptM ``CIL.Method.returnNarrow_of_declared + #[some method, some type, none, some returns, some declaredType, some resolved]) + +/-- `Nat × LocalKind`, the type of a parameter home. -/ +def homeType : Expr := + mkApp2 (mkConst ``Prod [.zero, .zero]) (mkConst ``Nat) (mkConst ``CIL.LocalKind) + +/-- The literal `some value` or `none` of type `Option Nat`. -/ +def natOption : Option Nat → Expr + | some value => mkApp2 (mkConst ``Option.some [.zero]) (mkConst ``Nat) (mkNatLit value) + | none => mkApp (mkConst ``Option.none [.zero]) (mkConst ``Nat) + +/-- `Eq.refl value` at the type `Option Nat`. -/ +def natOptionRefl (value : Option Nat) : Expr := + mkApp2 (mkConst ``Eq.refl [.one]) (mkApp (mkConst ``Option [.zero]) (mkConst ``Nat)) + (natOption value) + +/-- The raw layouts a method's declared types refer to. -/ +def layoutsOf (method : Expr) : Expr := + mkApp (mkConst ``CIL.LayoutEnvironment.rawLayouts) + (mkApp (mkConst ``CIL.Method.environment) method) + +/-- Each home's argument index, key and slot kind, when every key reduces to a literal. -/ +def homeKeys? (homes : Array Expr) : MetaM (Option (Array (Nat × Expr × Expr))) := do + let mut keys := #[] + for home in homes do + let (``Prod.mk, #[_, _, key, kind]) := (← whnf home).getAppFnArgs | return none + let some value ← (Meta.evalNat key).run | return none + keys := keys.push (value, key, kind) + return some keys + +/-- How `method` reads argument `index`, proved from premises the kernel decides by walking the + homes, the local kinds and the parameters up to `index`; for a literal body in a proof. -/ +def argumentProof? (method : Expr) (index : Nat) : MetaM (Option Expr) := withDefault do + let argument := mkNatLit index + let (homes, complete) ← listElements (mkApp (mkConst ``CIL.Method.parameterHomes) method) + unless complete do return none + let some keys ← homeKeys? homes | return none + if let some position := keys.findIdx? (·.1 == index) then + let (kinds, complete) ← listElements (mkApp (mkConst ``CIL.Method.localKinds) method) + unless complete do return none + let slot := mkNatLit (kinds.size + position) + return some (mkAppN (mkConst ``CIL.Method.argumentAccess_home) + #[method, argument, mkNatLit position, slot, natOptionRefl (some position), + ← mkEqRefl slot]) + let declared := mkApp (mkConst ``CIL.Method.declared) method + let type ← whnf (mkApp2 (mkConst ``CIL.DeclaredSignature.argumentType) declared argument) + match type.getAppFnArgs with + | (``Option.none, _) => + return some (mkAppN (mkConst ``CIL.Method.argumentAccess_undeclared) + #[method, argument, natOptionRefl none, ← mkEqRefl type]) + | (``Option.some, #[_, parameter]) => + let some narrowed ← declaredProof? (layoutsOf method) parameter | return none + let some (_, _, result) := (← inferType narrowed).eq? | return none + return some (mkAppN (mkConst ``CIL.Method.argumentAccess_value) + #[method, argument, parameter, ← whnf result, natOptionRefl none, ← mkEqRefl type, + narrowed]) + | _ => return none + +/-- The fact `cil_resolution_facts` declared for how the method constant `method` reads argument + `index`, when its statement is about exactly that method and index. -/ +def argumentFact? (method index : Expr) : MetaM (Option Expr) := do + let .const name _ := method | return none + let some literal ← (Meta.evalNat index).run | return none + let fact := name.appendAfter s!"_argumentAccess{literal}" + let some info := (← getEnv).find? fact | return none + let some (_, lhs, _) := info.type.eq? | return none + let_expr CIL.Method.argumentAccess factMethod factIndex := lhs | return none + unless factMethod == method do return none + unless (← (Meta.evalNat factIndex).run) == some literal do return none + return some (mkConst fact) + +/-- That a method with no homes and no declared receiver or parameters reads argument `index` as + it is, from premises about the method alone, which the kernel checks once for every index. -/ +def plainArgumentProof? (method index : Expr) : MetaM (Option Expr) := withDefault do + let homes := mkApp (mkConst ``CIL.Method.parameterHomes) method + unless (← whnf homes).isAppOf ``List.nil do return none + let declared := mkApp (mkConst ``CIL.Method.declared) method + let parameters := mkApp (mkConst ``CIL.DeclaredSignature.parameters) declared + unless (← whnf parameters).isAppOf ``List.nil do return none + let receiver := mkApp (mkConst ``CIL.DeclaredSignature.receiver) declared + unless (← whnf receiver).isAppOf ``Option.none do return none + return some (mkAppN (mkConst ``CIL.Method.argumentAccess_plain) + #[method, index, ← mkEqRefl homes, ← mkEqRefl parameters, ← mkEqRefl receiver]) + +/-- How `method` reads argument `index`: its declared fact or the plain reading of a method + without a signature, which cost the kernel nothing per index, or else a proof from premises + that walk the signature up to `index`. -/ +def argumentAccessProof? (method index : Expr) : MetaM (Option Expr) := do + if let some fact ← argumentFact? method index then return some fact + if let some plain ← plainArgumentProof? method index then return some plain + let some literal ← (Meta.evalNat index).run | return none + argumentProof? method literal + +/-- How `method` reads argument `index`, reduced to an `ArgumentAccess` constructor, with the + proof `argumentAccessProof?` finds for it. -/ +def resolvedArgumentAccess? (method index : Expr) : MetaM (Option (Expr × Expr)) := do + let some proof ← argumentAccessProof? method index | return none + let some (_, _, access) := (← inferType proof).eq? | return none + return some (← whnfR (← instantiateMVars access), proof) + +/-- The frame slot of the home of argument `index` of `method`, with the proof that the method + reads the argument there; none for an argument without a home or an undecided access. -/ +def argumentHome? (method index : Expr) : MetaM (Option (Expr × Expr)) := do + let some (access, proof) ← resolvedArgumentAccess? method index | return none + let_expr CIL.ArgumentAccess.home slot := access | return none + return some (slot, proof) + +/-- What a method's argument facts read: its receiver, parameter types and homes. -/ +structure Signature where + /-- The receiver's value, for an instance method. -/ + receiver : Option Expr + /-- The declared parameter types, `this` excluded. -/ + parameters : Array Expr + /-- Each home's argument index, key and slot kind, ascending by index. -/ + homes : Array (Nat × Expr × Expr) + /-- The number of local kinds, read only when the method has homes. -/ + localCount : Nat + +/-- Reads the signature of the method `body` names. Fails on a part that does not reduce to a + literal and on homes whose argument indices do not ascend. -/ +def readSignature (body : Body) : MetaM Signature := withDefault do + let method := body.constant "" + let declared := mkApp (mkConst ``CIL.Method.declared) method + let receiverValue ← whnf (mkApp (mkConst ``CIL.DeclaredSignature.receiver) declared) + let receiver ← match receiverValue.getAppFnArgs with + | (``Option.none, _) => pure none + | (``Option.some, #[_, value]) => pure (some value) + | _ => throwError "cil_resolution_facts: the receiver of {body.name} is unresolved" + let (parameters, complete) ← + listElements (mkApp (mkConst ``CIL.DeclaredSignature.parameters) declared) + unless complete do + throwError "cil_resolution_facts: the parameter types of {body.name} are unresolved" + let (homes, complete) ← listElements (mkApp (mkConst ``CIL.Method.parameterHomes) method) + let some keys ← homeKeys? homes + | throwError "cil_resolution_facts: the parameter homes of {body.name} are unresolved" + unless complete do + throwError "cil_resolution_facts: the parameter homes of {body.name} are unresolved" + for position in [1:keys.size] do + unless keys[position - 1]!.1 < keys[position]!.1 do + throwError "cil_resolution_facts: the parameter homes of {body.name} do not ascend" + let mut localCount := 0 + unless keys.isEmpty do + let (kinds, complete) ← listElements (mkApp (mkConst ``CIL.Method.localKinds) method) + unless complete do + throwError "cil_resolution_facts: the local kinds of {body.name} are unresolved" + localCount := kinds.size + return { receiver, parameters, homes := keys, localCount } + +/-- `list.drop position = suffix`, from the named suffix that starts the position's block, with + the suffix `suffixFrom` gives; the proof walks at most one block. -/ +def placedAt (body : Body) (stem : String) (elementType list : Expr) (elements : Array Expr) + (position : Nat) : Expr × Expr := + let offset := position % suffixBlock + let start := position - offset + let next := suffixFrom body stem elementType elements position + (mkApp8 (mkConst ``CIL.CodeSuffix.advance) elementType list (body.constant s!"{stem}{start}") + next (mkNatLit start) (mkNatLit offset) (body.constant s!"{stem}{start}_placed") + (mkApp2 (mkConst ``Eq.refl [.one]) (mkApp (mkConst ``List [.zero]) elementType) next), next) + +/-- Declares `body_argumentAccess{index}` for every argument of the method `body` names and adds + each to `cil_code`. One pass walks the parameter types and the homes together; each fact + applies a `CIL.Method.argumentAccess` lemma to premises that read at most one block of a + named suffix, so the facts cost time linear in the arguments and homes. -/ +def declareArgumentFacts (body : Body) (simpSet : SimpExtension) : TermElabM Unit := do + let signature ← readSignature body + let shift := if signature.receiver.isSome then 1 else 0 + let count := signature.parameters.size + shift + if count = 0 then return + let method := body.constant "" + let declared := mkApp (mkConst ``CIL.Method.declared) method + let typeExpr := mkConst ``CIL.Extraction.TypeExpr + let localKind := mkConst ``CIL.LocalKind + let parameterList := mkApp (mkConst ``CIL.DeclaredSignature.parameters) declared + let homeList := mkApp (mkConst ``CIL.Method.parameterHomes) method + let homes := signature.homes + let homeElements := homes.map fun (_, key, kind) => + mkApp4 (mkConst ``Prod.mk [.zero, .zero]) (mkConst ``Nat) localKind key kind + declareSuffixes body typeExpr "_parameterSuffix" + (fun start => s!"The declared parameter types of `{body.name}` from parameter {start} on") + parameterList signature.parameters + let ascending := body.constant "_homesAscend" + let localCount := body.constant "_localCount" + let boolTrue ← mkEqRefl (mkConst ``Bool.true) + unless homes.isEmpty do + declareSuffixes body homeType "_homeSuffix" + (fun start => s!"The parameter homes of `{body.name}` from position {start} on") + homeList homeElements + let ascends := mkApp3 (mkConst ``CIL.Extraction.keysAscend) localKind (mkNatLit 0) homeList + body.prove "_homesAscend" s!"The parameter homes of `{body.name}` ascend by argument." + (← mkEq ascends (mkConst ``Bool.true)) boolTrue + let length := mkApp2 (mkConst ``List.length [.zero]) localKind + (mkApp (mkConst ``CIL.Method.localKinds) method) + body.prove "_localCount" s!"The number of declared locals of `{body.name}`." + (← mkEq length (mkNatLit signature.localCount)) + (← mkEqRefl (mkNatLit signature.localCount)) + let placedHome := placedAt body "_homeSuffix" homeType homeList homeElements + let mut passed := 0 + for index in [0:count] do + let argument := mkNatLit index + let homed := passed < homes.size && homes[passed]!.1 == index + -- A window per fact, so a long signature's facts do not exhaust the command's. + withCurrHeartbeats do + let (access, proof) ← if homed then do + let (_, key, kind) := homes[passed]! + let (placed, next) := placedHome passed + let found := mkAppN (mkConst ``CIL.Extraction.findIdx?_of_keysAscend) + #[localKind, homeList, next.appArg!, mkNatLit 0, mkNatLit passed, key, kind, + ascending, placed] + let shifted := mkLambda `count .default (mkConst ``Nat) + (mkNatAdd (mkBVar 0) (mkNatLit passed)) + let slot := mkNatLit (signature.localCount + passed) + pure (mkApp (mkConst ``CIL.ArgumentAccess.home) slot, + mkAppN (mkConst ``CIL.Method.argumentAccess_home) + #[method, argument, mkNatLit passed, slot, found, + ← mkCongrArg shifted localCount]) + else do + let unhomed ← if homes.isEmpty then pure (natOptionRefl none) + else if passed = 0 then do + let raised := mkAppN (mkConst ``CIL.Extraction.keysAscend_raise) + #[localKind, homeList, mkNatLit 0, mkNatLit (index + 1), ascending, boolTrue] + pure <| mkAppN (mkConst ``CIL.Extraction.findIdx?_none_of_keysAscend) + #[localKind, homeList, mkNatLit (index + 1), argument, raised, + mkApp (mkConst ``Nat.lt_add_one) argument] + else do + let (_, key, kind) := homes[passed - 1]! + let (placed, next) := placedHome (passed - 1) + let below ← mkDecideProof (← mkLt key argument) + pure <| mkAppN (mkConst ``CIL.Extraction.findIdx?_none_after) + #[localKind, homeList, next.appArg!, mkNatLit 0, mkNatLit (passed - 1), key, + argument, kind, ascending, placed, below, boolTrue] + let thisReceiver := if index = 0 then signature.receiver else none + if let some receiver := thisReceiver then + let this := mkApp3 (mkConst ``CIL.DeclaredSignature.argumentType_this) declared + receiver (← mkEqRefl (← mkAppM ``Option.some #[receiver])) + let narrowNone := mkApp (mkConst ``Option.none [.zero]) (mkConst ``CIL.NarrowKind) + pure (mkApp (mkConst ``CIL.ArgumentAccess.value) narrowNone, + mkAppN (mkConst ``CIL.Method.argumentAccess_undeclared) + #[method, argument, unhomed, this]) + else + let parameter := index - shift + let type := signature.parameters[parameter]! + let found ← foundAt body "_parameterSuffix" typeExpr parameterList + signature.parameters parameter + let typed ← match signature.receiver with + | none => do + let static ← + mkEqRefl (mkApp (mkConst ``Option.none [.zero]) (mkConst ``CIL.Receiver)) + pure (mkAppN (mkConst ``CIL.DeclaredSignature.argumentType_static) + #[declared, mkNatLit parameter, type, static, found]) + | some receiver => do + let instanceMethod ← mkEqRefl (← mkAppM ``Option.some #[receiver]) + pure (mkAppN (mkConst ``CIL.DeclaredSignature.argumentType_instance) + #[declared, mkNatLit parameter, receiver, type, instanceMethod, found]) + let some narrowed ← declaredProof? (layoutsOf method) type + | throwError "cil_resolution_facts: the narrowing of argument {index} of \ + {body.name} is unresolved" + let some (_, _, result) := (← inferType narrowed).eq? + | throwError "cil_resolution_facts: expected an argument narrowing equality" + let result ← withDefault <| whnf result + pure (mkApp (mkConst ``CIL.ArgumentAccess.value) result, + mkAppN (mkConst ``CIL.Method.argumentAccess_value) + #[method, argument, type, result, unhomed, typed, narrowed]) + body.prove s!"_argumentAccess{index}" s!"How `{body.name}` reads argument {index}." + (← mkEq (mkApp2 (mkConst ``CIL.Method.argumentAccess) method argument) access) proof + addSimpTheorem simpSet (body.name.appendAfter s!"_argumentAccess{index}") (post := false) + (inv := false) .global (eval_prio default) + if homed then passed := passed + 1 + +/-- Declares `body_staticAt{pc}` for every static site of the method `body` names and adds each + to `cil_code`. One check that the site pcs ascend lets each fact read at most one block of a + named suffix, so the facts cost time linear in the sites. -/ +def declareStaticFacts (body : Body) (simpSet : SimpExtension) : TermElabM Unit := do + let method := body.constant "" + let siteList := mkApp (mkConst ``CIL.Method.staticSites) method + let (sites, complete) ← withDefault <| listElements siteList + unless complete do + throwError "cil_resolution_facts: the static sites of {body.name} are unresolved" + if sites.isEmpty then return + let mut keyed := #[] + for site in sites do + let (``Prod.mk, #[_, _, key, descriptor]) := (← withDefault <| whnf site).getAppFnArgs + | throwError "cil_resolution_facts: a static site of {body.name} is unresolved" + let some pc ← (Meta.evalNat key).run + | throwError "cil_resolution_facts: a static site pc of {body.name} is unresolved" + keyed := keyed.push (pc, descriptor) + for position in [1:keyed.size] do + unless keyed[position - 1]!.1 < keyed[position]!.1 do + throwError "cil_resolution_facts: the static sites of {body.name} do not ascend" + let descriptorType := mkConst ``CIL.StaticDescriptor + let siteType := mkApp2 (mkConst ``Prod [.zero, .zero]) (mkConst ``Nat) descriptorType + declareSuffixes body siteType "_siteSuffix" + (fun start => s!"The static sites of `{body.name}` from position {start} on") siteList sites + let ascends := mkApp3 (mkConst ``CIL.Extraction.keysAscend) descriptorType (mkNatLit 0) siteList + body.prove "_sitesAscend" s!"The static sites of `{body.name}` ascend by pc." + (← mkEq ascends (mkConst ``Bool.true)) (← mkEqRefl (mkConst ``Bool.true)) + let ascending := body.constant "_sitesAscend" + for position in [0:keyed.size] do + let (pc, descriptor) := keyed[position]! + -- A window per fact, so a method with many sites does not exhaust the command's. + withCurrHeartbeats do + let (placed, next) := placedAt body "_siteSuffix" siteType siteList sites position + let proof := mkAppN (mkConst ``CIL.Method.staticAt_of_keysAscend) + #[method, next.appArg!, mkNatLit position, mkNatLit pc, descriptor, ascending, placed] + let resolved := mkApp2 (mkConst ``CIL.Method.staticAt) method (mkNatLit pc) + body.prove s!"_staticAt{pc}" + s!"The descriptor extraction bound to the static instruction at pc {pc} of `{body.name}`." + (← mkEq resolved (← mkAppM ``Option.some #[descriptor])) proof + addSimpTheorem simpSet (body.name.appendAfter s!"_staticAt{pc}") (post := false) + (inv := false) .global (eval_prio default) + +/-- `value` with every constructor field reduced: a natural number to a literal, and a string kept + as written when it is a literal or a named constant and evaluated otherwise; `none` when a + part reduces to no constructor or a string mentions a free variable. -/ +partial def constructorForm? (value : Expr) : MetaM (Option Expr) := withDefault do + let type ← whnf (← inferType value) + if type.isConstOf ``String then + let value := value.consumeMData + if value.isConst || value matches .lit (.strVal _) then return some value + if value.hasFVar || value.hasMVar then return none + return some (mkStrLit (← unsafe evalExpr String type value)) + if type.isConstOf ``Nat then + return (← (evalNat (← whnf value)).run).map mkNatLit + let head ← whnf value + let .const name _ := head.getAppFn | return none + let some (.ctorInfo info) := (← getEnv).find? name | return none + let arguments := head.getAppArgs + let mut parts := arguments.extract 0 info.numParams + for field in arguments.extract info.numParams arguments.size do + let some reduced ← constructorForm? field | return none + parts := parts.push reduced + return some (mkAppN head.getAppFn parts) + +/-- Reads of a method's layout tables at an index; `none` past the entries a read reaches. -/ +structure LayoutReads where + /-- The raw metadata at an index, with a proof of + `body.environment.rawLayouts[index]? = some metadata`. -/ + declared : Nat → MetaM (Option (Expr × Expr)) + /-- The computed layout at an index, with a proof of + `body.environment.layouts.toList[index]? = some layout`. -/ + computed : Nat → MetaM (Option (Expr × Expr)) + +/-- `body.elementShape (.aggregate identity index)` by the aggregate or the by-ref-like lemma, + when the raw layouts hold the index under the same literal identity. -/ +def aggregateProof? (method identity index : Expr) (reads : LayoutReads) : + MetaM (Option Expr) := withDefault do + let some position ← (evalNat index).run | return none + let some (metadata, declared) ← reads.declared position | return none + let .lit (.strVal actual) ← whnf (mkApp (mkConst ``CIL.Extraction.RawLayout.identity) metadata) + | return none + let .lit (.strVal expected) ← whnf identity | return none + unless actual == expected do return none + let identified ← mkEqRefl identity + let byRefLike ← whnf (mkApp (mkConst ``CIL.Extraction.RawLayout.byRefLike) metadata) + if byRefLike.isConstOf ``Bool.true then + return some (mkAppN (mkConst ``CIL.Method.elementShape_byRefLike) + #[method, identity, index, metadata, declared, identified, ← mkEqRefl byRefLike]) + unless byRefLike.isConstOf ``Bool.false do return none + let some (layout, computed) ← reads.computed position | return none + return some (mkAppN (mkConst ``CIL.Method.elementShape_aggregate) + #[method, identity, index, metadata, layout, declared, identified, ← mkEqRefl byRefLike, + computed]) + +/-- The shape of `method`'s array elements of type `type` in constructor form, with its proof: by + the primitive and enum lemmas, which evaluate no type name, by the class lemma, which compares + a literal class name with `System.Object`, by the aggregate lemmas over `reads`, and + otherwise by evaluation. -/ +def elementProof? (method type : Expr) (reads : LayoutReads) : + MetaM (Option (Expr × Expr)) := withDefault do + let head ← whnf type + let chosen ← match head.getAppFnArgs with + | (``CIL.Extraction.TypeExpr.atom, _) => + pure ((primitive? head).map fun primitive => + mkApp2 (mkConst ``CIL.Method.elementShape_primitive) method (mkConst primitive)) + | (``CIL.Extraction.TypeExpr.enum, #[identity, underlying]) => + pure ((primitive? (← whnf underlying)).map fun primitive => + mkApp3 (mkConst ``CIL.Method.elementShape_enum) method identity (mkConst primitive)) + | (``CIL.Extraction.TypeExpr.object, #[scope, name]) => do + let .lit (.strVal text) ← whnf name | pure none + if text == CIL.ElementShape.rootObjectName then pure none else + let derived ← mkDecideProof + (mkNot (← mkEq name (mkConst ``CIL.ElementShape.rootObjectName))) + pure (some (mkApp4 (mkConst ``CIL.Method.elementShape_object) method scope name derived)) + | (``CIL.Extraction.TypeExpr.aggregate, #[identity, index]) => + aggregateProof? method identity index reads + | _ => pure none + let shape := mkApp2 (mkConst ``CIL.Method.elementShape) method type + let some proof := chosen | do + let some value ← constructorForm? shape | return none + return some (value, ← mkEqRefl value) + let some (_, _, result) := (← inferType proof).eq? | return none + let some value ← constructorForm? result | return none + return some (value, proof) + +/-- `LayoutReads` that walk a literal body's layout lists, with premises the kernel decides by + walking them to the index; for a literal body in a proof. -/ +def literalReads (method : Expr) : LayoutReads := + let environment := mkApp (mkConst ``CIL.Method.environment) method + let read (list : Expr) (position : Nat) : MetaM (Option (Expr × Expr)) := do + let (elements, _) ← listElements list + let some element := elements[position]? | return none + return some (element, ← mkEqRefl (← mkAppM ``Option.some #[element])) + { declared := read (mkApp (mkConst ``CIL.LayoutEnvironment.rawLayouts) environment) + computed := read (mkApp2 (mkConst ``Array.toList [.zero]) + (mkConst ``CIL.Extraction.ComputedLayout) + (mkApp (mkConst ``CIL.LayoutEnvironment.layouts) environment)) } + +/-- The opcodes whose type operand is an array element type: `newarr`, `ldelema`, `ldelem` and + `stelem`. -/ +def elementOpcodes : List Nat := [141, 143, 163, 164] + +/-- The distinct element types the raw method `body{Closed}` names as the type operand of an + `elementOpcodes` instruction, in order of first appearance; none without that raw method. -/ +def elementTypes (body : Body) : MetaM (Array Expr) := withDefault do + let closed := body.name.appendAfter "Closed" + let some info := (← getEnv).find? closed | return #[] + unless info.type.isConstOf ``CIL.Extraction.RawMethod do return #[] + let code := mkApp (mkConst ``CIL.Extraction.RawMethod.code) + (mkConst closed (info.levelParams.map fun _ => .zero)) + let (instructions, complete) ← listElements code + unless complete do throwError "cil_resolution_facts: the raw code of {closed} is unresolved" + let mut seen : Std.HashSet Expr := {} + let mut types := #[] + for instruction in instructions do + let (``CIL.Extraction.RawInstruction.mk, #[_, opcode, operand]) := + (← whnf instruction).getAppFnArgs + | throwError "cil_resolution_facts: an instruction of {closed} is unresolved" + let some opcode ← (evalNat (← whnf opcode)).run + | throwError "cil_resolution_facts: an opcode of {closed} is unresolved" + unless elementOpcodes.contains opcode do continue + let (``CIL.Extraction.RawOperand.type, #[type]) := (← whnf operand).getAppFnArgs | continue + let type ← instantiateMVars type + unless seen.contains type do + seen := seen.insert type + types := types.push type + return types + +/-- `LayoutReads` through named suffixes of the layout lists of `body`'s environment. An + environment constant of this module names the suffixes once for all its methods; any other + environment names them for `body`. Each read walks at most one block. -/ +def suffixReads (body : Body) : TermElabM LayoutReads := withDefault do + let method := body.constant "" + let projected := mkApp (mkConst ``CIL.Method.environment) method + let named ← whnfR (mkApp (mkConst ``CIL.Method.environment) (← whnf method)) + let current ← getEnv + let shared := match named with + | .const name [] => (current.getModuleIdxFor? name).isNone + | _ => false + let (owner, environment) := if shared then + ({ body with name := named.constName!, levelParams := [], parameters := #[] }, named) + else (body, projected) + let rawType := mkConst ``CIL.Extraction.RawLayout + let computedType := mkConst ``CIL.Extraction.ComputedLayout + let rawList := mkApp (mkConst ``CIL.LayoutEnvironment.rawLayouts) environment + let computedList := mkApp2 (mkConst ``Array.toList [.zero]) computedType + (mkApp (mkConst ``CIL.LayoutEnvironment.layouts) environment) + let (raw, rawComplete) ← listElements rawList + let (computed, computedComplete) ← listElements computedList + unless rawComplete && computedComplete do + throwError "cil_resolution_facts: the layouts of {body.name} are unresolved" + unless (← getEnv).contains (owner.name.appendAfter "_rawLayoutSuffix0") do + declareSuffixes owner rawType "_rawLayoutSuffix" + (fun start => s!"The raw layouts of `{owner.name}` from entry {start} on") rawList raw + declareSuffixes owner computedType "_layoutSuffix" + (fun start => s!"The computed layouts of `{owner.name}` from entry {start} on") + computedList computed + let read (stem : String) (elementType list : Expr) (elements : Array Expr) (position : Nat) : + MetaM (Option (Expr × Expr)) := do + let some element := elements[position]? | return none + return some (element, ← foundAt owner stem elementType list elements position) + return { declared := read "_rawLayoutSuffix" rawType rawList raw + computed := read "_layoutSuffix" computedType computedList computed } + +/-- Declares `body_elementShape{k}` for the `k`-th distinct element type the raw method + `body{Closed}` names and adds each to `cil_code`. A struct element reads its layouts through + named suffixes, so the facts cost time linear in the layouts and the distinct types. -/ +def declareElementFacts (body : Body) (simpSet : SimpExtension) : TermElabM Unit := do + let types ← elementTypes body + if types.isEmpty then return + let method := body.constant "" + let aggregate ← types.anyM fun type => do + return (← whnf type).isAppOf ``CIL.Extraction.TypeExpr.aggregate + let reads ← if aggregate then suffixReads body else pure (literalReads method) + for h : position in [0:types.size] do + let type := types[position] + -- A window per fact, so the facts of many element types do not exhaust the command's. + withCurrHeartbeats do + let some (shape, proof) ← elementProof? method type reads + | throwError "cil_resolution_facts: the shape of element type {position} of \ + {body.name} is unresolved" + body.prove s!"_elementShape{position}" + s!"The shape of `{body.name}`'s array elements of type {position}." + (← mkEq (mkApp2 (mkConst ``CIL.Method.elementShape) method type) shape) proof + addSimpTheorem simpSet (body.name.appendAfter s!"_elementShape{position}") (post := false) + (inv := false) .global (eval_prio default) + +/-- The checked element shape of the decoded array instruction at `pc` in a named method. -/ +elab "cil_element_shape% " body:ident ppSpace pc:num : term => do + let method ← Term.elabTermEnsuringType body (mkConst ``CIL.Method) + let lookup ← Term.elabTermAndSynthesize (← `(term| (CIL.Method.code $body)[$pc]?)) none + let some ext ← getSimpExtension? `cil_code | throwError "Missing cil_code simp set" + let ctx ← Simp.mkContext (simpTheorems := #[← ext.getTheorems]) + (congrTheorems := ← getSimpCongrTheorems) + let (found, _) ← simp (← instantiateMVars lookup) ctx + let (``Option.some, #[_, op]) := found.expr.getAppFnArgs + | throwError "Expected an instruction at pc {pc}, found {found.expr}" + let (head, args) := op.getAppFnArgs + unless #[``CIL.Instruction.newarr, ``CIL.Instruction.ldelem_any, ``CIL.Instruction.stelem_any, + ``CIL.Instruction.ldelema].contains head do + throwError "The instruction at pc {pc} has no array element type" + let lhs := mkApp2 (mkConst ``CIL.Method.elementShape) method args[0]! + let (result, _) ← simp lhs ctx + let some proof := result.proof? | throwError "No checked element shape at pc {pc}" + return proof + +/-- Declares a method's kernel-checked return narrowing from its named signature facts, how it + reads each declared argument, the descriptor bound to each static site and the shape of each + array element type its raw method names. -/ +syntax "cil_resolution_facts " ident : command + +elab_rules : command + | `(cil_resolution_facts $methodSyntax:ident) => liftTermElabM do + let name ← realizeGlobalConstNoOverloadWithInfo methodSyntax + let info ← getConstInfo name + let some simpSet ← getSimpExtension? `cil_code + | throwError "cil_resolution_facts: cil_code is not registered" + forallTelescope info.type fun parameters result => do + unless ← isDefEq result (mkConst ``CIL.Method) do + throwError "cil_resolution_facts: {name} does not define a CIL.Method" + let body : Body := + { name, levelParams := info.levelParams, parameters, syntaxRef := methodSyntax } + let method := body.constant "" + let some proof ← returnProof? method (body.constant "_returnsValue") + (body.constant "_declared") + | throwError "cil_resolution_facts: return narrowing of {name} is unresolved" + let some (_, _, result) := (← inferType proof).eq? + | throwError "cil_resolution_facts: expected a return narrowing equality" + let result ← withDefault <| whnf result + body.prove "_returnNarrow" s!"The return narrowing of `{name}`." + (← mkEq (mkApp (mkConst ``CIL.Method.returnNarrow) method) result) proof + addSimpTheorem simpSet (name.appendAfter "_returnNarrow") (post := false) + (inv := false) .global (eval_prio default) + declareArgumentFacts body simpSet + declareStaticFacts body simpSet + declareElementFacts body simpSet + +/-! ## Runtime models -/ + +/-- The type operand loaded by the instruction at a comparison input. -/ +private def loadedType? (ops : Array Expr) (pc : Nat) : MetaM (Option Expr) := do + let some op := ops[pc]? | return none + let_expr CIL.Instruction.ldtoken operand := ← whnfR op | return none + let_expr CIL.TokenOperand.type type := ← whnfR operand | return none + return some type + +/-- The preceding model fact identifies a checked GetTypeFromHandle call. -/ +private def isTypeGetter (body : Body) (pc : Nat) : MetaM Bool := do + let some info := (← getEnv).find? (body.name.appendAfter s!"_model{pc}") | return false + forallTelescope info.type fun _ conclusion => do + let_expr Eq _ _ result := conclusion | return false + let_expr Option.some _ model := result | return false + return model.isConstOf ``CIL.RuntimeModel.typeFromHandle + +/-- A decided comparison of the two types at a pair of checked token/getter sites. + An unresolved comparison supplies no equality fact. -/ +private def declareTypeIdentity (body : Body) (ops : Array Expr) (pc : Nat) + (simpSet : SimpExtension) : TermElabM Unit := do + unless pc ≥ 4 && (← isTypeGetter body (pc - 3)) && (← isTypeGetter body (pc - 1)) do return + let some left ← loadedType? ops (pc - 4) | return + let some right ← loadedType? ops (pc - 2) | return + let read := mkApp2 (mkConst ``CIL.Extraction.sameRuntimeType) left right + let proof? ← match_expr (← whnfR left) with + | CIL.Extraction.TypeExpr.aggregate identity layout => + if ← isDefEq left right then + pure (some (mkApp2 (mkConst ``CIL.Extraction.sameRuntimeType_aggregate_self) identity layout)) + else pure none + | _ => pure none + let (value, proof) ← match proof? with + | some proof => pure (mkConst ``Bool.true, proof) + | none => do + let_expr Option.some _ value := ← withDefault <| whnf read | return + let value ← withDefault <| whnf value + unless value.isConstOf ``Bool.false || value.isConstOf ``Bool.true do return + pure (value, ← mkEqRefl read) + body.prove s!"_typeIdentity{pc}" s!"The scoped type comparison at pc {pc} of `{body.name}`." + (← mkEq read (← mkAppM ``Option.some #[value])) proof + addSimpTheorem simpSet (body.name.appendAfter s!"_typeIdentity{pc}") (post := false) + (inv := false) .global (eval_prio default) + +/-- Each modelled call or constructor has a lookup fact for its opcode, method and environment. -/ +syntax "cil_runtime_models " ident : command + +elab_rules : command + | `(cil_runtime_models $methodSyntax:ident) => liftTermElabM do + let name ← realizeGlobalConstNoOverloadWithInfo methodSyntax + let info ← getConstInfo name + let some simpSet ← getSimpExtension? `cil_code + | throwError "cil_runtime_models: cil_code is not registered" + forallTelescope info.type fun parameters result => do + unless ← isDefEq result (mkConst ``CIL.Method) do + throwError "cil_runtime_models: {name} does not define a CIL.Method" + let body : Body := + { name, levelParams := info.levelParams, parameters, syntaxRef := methodSyntax } + let method := body.constant "" + let environment := mkApp (mkConst ``CIL.Method.environment) method + let ops ← instructions name (mkApp (mkConst ``CIL.Method.code) method) + for h : pc in [0:ops.size] do + let op ← whnfR ops[pc] + let some (opcode, target) := (match_expr op with + | CIL.Instruction.call target _ => some (``CIL.CallOpcode.call, target) + | CIL.Instruction.newobj target => some (``CIL.CallOpcode.newobj, target) + | _ => none) | continue + let read := mkApp3 (mkConst ``CIL.Extraction.runtimeModel) environment + (mkConst opcode) target + let resolved ← withDefault <| whnf read + let_expr Option.some _ model := resolved | continue + let model ← withDefault <| reduce model + -- A constant is written as the numeral `ldc.i4` pushes, so a test of it simplifies alike. + let model ← match_expr model with + | CIL.RuntimeModel.constant value => do + let .lit (.natVal word) ← withDefault <| reduce + (mkApp2 (mkConst ``BitVec.toNat) (mkNatLit 32) value) + | throwError "cil_runtime_models: the constant at pc {pc} is not a literal" + let numeral ← mkNumeral (mkConst ``CIL.W32) word + pure (mkApp (mkConst ``CIL.RuntimeModel.constant) numeral) + | _ => pure model + -- A window per fact, so the facts of many model sites do not exhaust the command's. + withCurrHeartbeats do + body.prove s!"_model{pc}" s!"The runtime model of the call at pc {pc} of `{name}`." + (← mkEq read (← mkAppM ``Option.some #[model])) (← mkEqRefl read) + addSimpTheorem simpSet (name.appendAfter s!"_model{pc}") (post := false) + (inv := false) .global (eval_prio default) + if model.isAppOf ``CIL.RuntimeModel.typeEquality then + declareTypeIdentity body ops pc simpSet + +/-! ## Field and value shapes -/ + +deriving instance Lean.ToExpr for CIL.EnumStorage +deriving instance Lean.ToExpr for CIL.ArrayElement +deriving instance Lean.ToExpr for CIL.ClassSlot +deriving instance Lean.ToExpr for CIL.FieldShape +deriving instance Lean.ToExpr for CIL.ValueShape + +/-- The name of the shape fact `cil_field_shape` or `cil_value_shape` declares for `operand`. -/ +def shapeFactName (operand : Name) : Name := operand.appendAfter "_shape" + +/-- The stem of the named suffixes of an environment's `rawLayouts`. -/ +def rawLayoutStem : String := "_rawLayoutSuffix" + +/-- The stem of the named suffixes of an environment's `layouts.toList`. -/ +def computedLayoutStem : String := "_layoutSuffix" + +/-- The two layout lists of an environment and the type of their entries, by suffix stem. -/ +def layoutLists (environment : Expr) : Array (String × Expr × Expr) := + let computedType := mkConst ``CIL.Extraction.ComputedLayout + #[(rawLayoutStem, mkConst ``CIL.Extraction.RawLayout, + mkApp (mkConst ``CIL.LayoutEnvironment.rawLayouts) environment), + (computedLayoutStem, computedType, mkApp2 (mkConst ``Array.toList [.zero]) computedType + (mkApp (mkConst ``CIL.LayoutEnvironment.layouts) environment))] + +/-- Names the suffixes of both layout lists of `environment` once, so the facts of one + environment walk each list once between them. -/ +def declareLayoutSuffixes (owner : Body) : TermElabM Unit := do + for (stem, entryType, list) in layoutLists (mkConst owner.name) do + if (← getEnv).contains (owner.name.appendAfter s!"{stem}0") then continue + let (entries, complete) ← listElements list + unless complete do + throwError "cil_shape_facts: the layouts of {owner.name} do not reduce to a list" + declareSuffixes owner entryType stem + (fun start => s!"The layouts of `{owner.name}` from entry {start} on") list entries + +/-- The entry at `position` of the layout list with suffixes `stem`, and a proof that the list + has it there, which reads only the suffix that starts the position's block. -/ +def layoutEntry? (owner : Body) (stem : String) (position : Nat) : + MetaM (Option (Expr × Expr)) := do + let some (_, entryType, list) := (layoutLists (mkConst owner.name)).find? (·.1 == stem) + | return none + let offset := position % suffixBlock + let start := position - offset + let some (.defnInfo info) := (← getEnv).find? (owner.name.appendAfter s!"{stem}{start}") + | return none + let mut cell := info.value + for _ in [0:offset] do + let (``List.cons, #[_, _, tail]) := cell.getAppFnArgs | return none + cell := tail + let (``List.cons, #[_, entry, _]) := cell.getAppFnArgs | return none + let found ← mkEqRefl (mkApp2 (mkConst ``Option.some [.zero]) entryType entry) + return some (entry, mkApp8 (mkConst ``CIL.CodeSuffix.lookup) entryType list + (owner.constant s!"{stem}{start}") (mkNatLit start) entry (mkNatLit offset) + (owner.constant s!"{stem}{start}_placed") found) + +/-- The element at `index` of the list `list` reduces to, as written. -/ +def listEntry? (list : Expr) (index : Nat) : MetaM (Option Expr) := do + let (entries, _) ← listElements list + return entries[index]? + +/-- A proof that `environment.fieldShape field` is the checked entry of a struct field, read + through the layout suffixes; `none` for any other field. -/ +def structFieldProof? (owner : Body) (field : Expr) : MetaM (Option Expr) := do + let environment := mkConst owner.name + let (``CIL.Extraction.FieldRef.mk, #[ownerType, indexExpr, held, _]) := + (← whnf field).getAppFnArgs | return none + let (``CIL.Extraction.TypeExpr.aggregate, #[identity, parentExpr]) := + (← whnf ownerType).getAppFnArgs | return none + let some index ← (Meta.evalNat indexExpr).run | return none + let some parent ← (Meta.evalNat parentExpr).run | return none + let some (metadata, rawFound) ← layoutEntry? owner rawLayoutStem parent | return none + let some (layout, checked) ← layoutEntry? owner computedLayoutStem parent | return none + let raw := mkApp (mkConst ``CIL.LayoutEnvironment.rawLayouts) environment + let ownerStorage := mkAppN (mkConst ``CIL.Extraction.typeStorage_aggregate) + #[raw, metadata, identity, parentExpr, rawFound, ← mkEqRefl identity] + let some declared ← listEntry? (mkApp (mkConst ``CIL.Extraction.RawLayout.fields) metadata) index + | return none + let some computed ← listEntry? (mkApp (mkConst ``CIL.AggregateLayout.fields) + (mkApp (mkConst ``CIL.Extraction.ComputedLayout.layout) layout)) index | return none + let declaredType := mkApp (mkConst ``CIL.Extraction.RawField.type) declared + let declaredKind := mkApp2 (mkConst ``Option.some [.zero]) + (mkConst ``CIL.Extraction.FieldType) declaredType + -- A held struct is decoded through its own entry; other held types do not read the layouts. + let heldKind ← match (← whnf held).getAppFnArgs with + | (``CIL.Extraction.TypeExpr.aggregate, #[heldIdentity, heldIndexExpr]) => do + let some heldIndex ← (Meta.evalNat heldIndexExpr).run | return none + let some (heldMetadata, heldFound) ← layoutEntry? owner rawLayoutStem heldIndex + | return none + pure (mkAppN (mkConst ``CIL.Extraction.fieldKind?_aggregate) + #[raw, heldMetadata, heldIdentity, heldIndexExpr, heldFound, ← mkEqRefl heldIdentity]) + | _ => mkEqRefl declaredKind + let fieldFound ← mkEqRefl (mkApp2 (mkConst ``Option.some [.zero]) + (mkConst ``CIL.Extraction.RawField) declared) + let entryFound ← mkEqRefl (mkApp2 (mkConst ``Option.some [.zero]) + (mkConst ``CIL.FieldLayout) computed) + let structFound := mkAppN (mkConst ``CIL.LayoutEnvironment.structField?_eq) + #[environment, parentExpr, indexExpr, held, metadata, declared, layout, computed, rawFound, + fieldFound, heldKind, checked, entryFound] + let signed := mkApp (mkConst ``CIL.Extraction.signedField) declaredType + return mkAppN (mkConst ``CIL.LayoutEnvironment.fieldShape_struct) + #[environment, field, parentExpr, computed, signed, ownerStorage, structFound] + +/-- A proof that `environment.fieldShape field` is the slot of a class field holding an array + of an admitted struct, read through the layout suffixes; `none` for any other field. -/ +def classArrayProof? (owner : Body) (field : Expr) : MetaM (Option Expr) := do + let environment := mkConst owner.name + let (``CIL.Extraction.FieldRef.mk, #[ownerType, _, held, _]) := + (← whnf field).getAppFnArgs | return none + let ownerType ← whnf ownerType + let (``CIL.Extraction.TypeExpr.object, #[scope, name]) := ownerType.getAppFnArgs + | return none + let (``CIL.Extraction.TypeExpr.array, #[element]) := (← whnf held).getAppFnArgs | return none + let (``CIL.Extraction.TypeExpr.aggregate, #[identity, indexExpr]) := + (← whnf element).getAppFnArgs | return none + let some index ← (Meta.evalNat indexExpr).run | return none + let some (metadata, found) ← layoutEntry? owner rawLayoutStem index | return none + let some (layout, checked) ← layoutEntry? owner computedLayoutStem index | return none + let raw := mkApp (mkConst ``CIL.LayoutEnvironment.rawLayouts) environment + let storage := mkAppN (mkConst ``CIL.Extraction.typeStorage_aggregateArray) + #[raw, metadata, identity, indexExpr, found, ← mkEqRefl (mkConst ``Bool.true)] + let slotProof := mkAppN (mkConst ``CIL.LayoutEnvironment.classSlot_aggregateArray) + #[environment, identity, indexExpr, layout, storage, checked] + let size := mkApp (mkConst ``CIL.AggregateLayout.size) + (mkApp (mkConst ``CIL.Extraction.ComputedLayout.layout) layout) + let slot := mkApp (mkConst ``CIL.ClassSlot.array) + (mkApp2 (mkConst ``CIL.ArrayElement.aggregate) identity size) + return mkAppN (mkConst ``CIL.LayoutEnvironment.fieldShape_class) + #[environment, field, scope, name, slot, ← mkEqRefl ownerType, slotProof] + +/-- A proof that `environment.valueShape type` is the checked size of a struct type, read + through the layout suffixes; `none` for any other type. -/ +def aggregateValueProof? (owner : Body) (type : Expr) : MetaM (Option Expr) := do + let environment := mkConst owner.name + let (``CIL.Extraction.TypeExpr.aggregate, #[identity, indexExpr]) := + (← whnf type).getAppFnArgs | return none + let some index ← (Meta.evalNat indexExpr).run | return none + let some (metadata, found) ← layoutEntry? owner rawLayoutStem index | return none + let some (layout, checked) ← layoutEntry? owner computedLayoutStem index | return none + let raw := mkApp (mkConst ``CIL.LayoutEnvironment.rawLayouts) environment + let storage := mkAppN (mkConst ``CIL.Extraction.typeStorage_aggregate) + #[raw, metadata, identity, indexExpr, found, ← mkEqRefl identity] + let noWidth := mkApp (mkConst ``Option.none [.zero]) (mkConst ``Nat) + return mkAppN (mkConst ``CIL.LayoutEnvironment.valueShape_aggregate) + #[environment, type, indexExpr, layout, ← mkEqRefl (mkConst ``Bool.false), + ← mkEqRefl noWidth, storage, checked] + +/-- The operand kinds that have shape facts. -/ +inductive ShapeOperand where + /-- A field operand of `ldfld`, `ldflda` or `stfld`, resolved by `fieldShape`. -/ + | field + /-- A type operand of `ldobj`, `stobj`, `initobj`, `cpobj` or `sizeof`, resolved by + `valueShape`. -/ + | value + +/-- The method resolver, lemma lifting the environment's resolution to every method sharing it, + and shape type of an operand kind. -/ +def ShapeOperand.names : ShapeOperand → Name × Name × Name + | .field => (``CIL.Method.fieldShape, ``CIL.Method.fieldShape_of_environment, ``CIL.FieldShape) + | .value => (``CIL.Method.valueShape, ``CIL.Method.valueShape_of_environment, ``CIL.ValueShape) + +/-- Layout environments evaluated by the compiled code, by name, so the shape facts of one + environment build its value once rather than once per operand. -/ +initialize evaluatedEnvironments : EnvExtension (Std.HashMap Name LayoutEnvironment) ← + registerEnvExtension (pure {}) + +/-- The value of the layout environment `name`, evaluated on its first use in an elaboration. -/ +unsafe def evaluatedEnvironment (name : Name) : MetaM LayoutEnvironment := do + if let some environment := (evaluatedEnvironments.getState (← getEnv)).get? name then + return environment + let environment ← evalExpr LayoutEnvironment (mkConst ``CIL.LayoutEnvironment) (mkConst name) + modifyEnv (evaluatedEnvironments.modifyState · (·.insert name environment)) + return environment + +/-- The literal shape the compiled resolver gives `operand` under the environment + `environmentName`. The kernel still checks the fact that states it, so a wrong evaluation + fails the declaration. -/ +unsafe def evaluateShape (kind : ShapeOperand) (environmentName : Name) (operand : Expr) : + MetaM Expr := do + let environment ← evaluatedEnvironment environmentName + match kind with + | .field => return toExpr (environment.fieldShape + (← evalExpr Extraction.FieldRef (mkConst ``CIL.Extraction.FieldRef) operand)) + | .value => return toExpr (environment.valueShape + (← evalExpr Extraction.TypeExpr (mkConst ``CIL.Extraction.TypeExpr) operand)) + +/-- A proof of the environment's resolution of `operand` that reads one layout entry through + the suffixes, for the struct shapes that have one; `none` for the shapes the resolver gives + without reading the layouts and for refused operands. -/ +def ShapeOperand.entryProof? (kind : ShapeOperand) (owner : Body) (operand : Expr) : + MetaM (Option Expr) := + match kind with + | .field => do + if let some proof ← structFieldProof? owner operand then return some proof + classArrayProof? owner operand + | .value => aggregateValueProof? owner operand + +/-- Declares `{operand}_shape`: every method whose environment is `environment` resolves the + operand to the shape `evaluate` gives it. The kernel checks that shape: from one layout entry + for a struct shape, or by reducing the resolver otherwise. -/ +def declareShapeFact (kind : ShapeOperand) (environmentName operandName : Name) + (evaluate : Expr → MetaM Expr) : TermElabM Unit := do + let (methodResolver, sharedLemma, shapeType) := kind.names + let owner : Body := + { name := environmentName, levelParams := [], parameters := #[], syntaxRef := ← getRef } + declareLayoutSuffixes owner + let environment := mkConst environmentName + let operand := mkConst operandName + let shape ← evaluate operand + let resolved ← match ← kind.entryProof? owner operand with + | some proof => pure proof + | none => pure (mkApp2 (mkConst ``Eq.refl [.one]) (mkConst shapeType) shape) + let (statement, proof) ← withLocalDecl `body .implicit (mkConst ``CIL.Method) fun body => do + let environmentOf := mkApp (mkConst ``CIL.Method.environment) body + withLocalDeclD `shared (← mkEq environmentOf environment) fun shared => do + let lhs := mkApp2 (mkConst methodResolver) body operand + let proof := mkAppN (mkConst sharedLemma) #[body, environment, operand, shape, shared, + resolved] + return (← mkForallFVars #[body, shared] (← mkEq lhs shape), + ← mkLambdaFVars #[body, shared] proof) + let name := shapeFactName operandName + withCurrHeartbeats do + addDecl (.thmDecl { name, levelParams := [], type := statement, value := proof }) + let describe := match kind with | .field => "field" | .value => "type" + addDocStringCore name <| s!"How every method sharing `{environmentName}` resolves the " ++ + s!"{describe} operand `{operandName}`." + +/-- `cil_field_shape environment field` declares `field_shape`: how every method whose + environment is `environment` resolves the field operand `field` (`CIL.Method.fieldShape`). -/ +syntax "cil_field_shape " ident ident : command + +/-- `cil_value_shape environment type` declares `type_shape`: how every method whose + environment is `environment` resolves the type operand `type` (`CIL.Method.valueShape`). -/ +syntax "cil_value_shape " ident ident : command + +elab_rules : command + | `(cil_field_shape $environment:ident $field:ident) => liftTermElabM do + let environmentName ← realizeGlobalConstNoOverloadWithInfo environment + declareShapeFact .field environmentName (← realizeGlobalConstNoOverloadWithInfo field) + (unsafe evaluateShape .field environmentName) + | `(cil_value_shape $environment:ident $type:ident) => liftTermElabM do + let environmentName ← realizeGlobalConstNoOverloadWithInfo environment + declareShapeFact .value environmentName (← realizeGlobalConstNoOverloadWithInfo type) + (unsafe evaluateShape .value environmentName) + +/-- The shape of `resolver body operand` from `operand_shape`, when `operand` is a constant with + that fact and a hypothesis states `body`'s environment is the fact's, or `body` unfolds to a + method with that environment, which the kernel checks by unfolding `body` once. -/ +def sharedShape? (resolver : Name) (expression : Expr) : MetaM (Option Simp.Step) := do + let (name, #[body, operand]) := expression.getAppFnArgs | return none + unless name == resolver do return none + let .const operandName _ := operand | return none + let fact := shapeFactName operandName + let some info := (← getEnv).find? fact | return none + let .forallE _ _ (.forallE _ shared _ _) _ := info.type | return none + let some (_, _, environment) := shared.eq? | return none + if environment.hasLooseBVars then return none + let environmentOf := mkApp (mkConst ``CIL.Method.environment) body + let shared ← if ← withDefault (isDefEq environmentOf environment) then + mkEqRefl environment + else + let stated ← mkEq environmentOf environment + -- A candidate states equality of layout environments. + let hypothesis? ← (← getLCtx).findDeclRevM? fun decl => do + if decl.isImplementationDetail then return none + let some (type, _, _) := (← whnfR decl.type).eq? | return none + unless ← withReducible (isDefEq type (mkConst ``CIL.LayoutEnvironment)) do return none + return if ← withDefault (isDefEq decl.type stated) then some decl.fvarId else none + let some hypothesis := hypothesis? | return none + pure (mkFVar hypothesis) + let proof := mkApp2 (mkConst fact) body shared + let some (_, resolved, shape) := (← inferType proof).eq? | return none + unless resolved == expression do return none + return some (.done { expr := shape, proof? := some proof }) + +/-- A method's field shape, from the shared fact of a field operand that has one. -/ +simproc ↓ [cil_code] reduceFieldShape (CIL.Method.fieldShape _ _) := fun expression => do + return (← sharedShape? ``CIL.Method.fieldShape expression).getD .continue + +/-- A method's value shape, from the shared fact of a type operand that has one. -/ +simproc ↓ [cil_code] reduceValueShape (CIL.Method.valueShape _ _) := fun expression => do + return (← sharedShape? ``CIL.Method.valueShape expression).getD .continue + +end CIL.Safety.ResolutionFacts diff --git a/CIL/Safety/ResultRenaming.lean b/CIL/Safety/ResultRenaming.lean index 115a42672..00fd8f086 100644 --- a/CIL/Safety/ResultRenaming.lean +++ b/CIL/Safety/ResultRenaming.lean @@ -47,6 +47,21 @@ theorem ResultRenamed.bind {mapping : AllocationRenaming} {relation : α → β | ok relation => exact next _ _ relation #print axioms ResultRenamed.bind +/-- A first computation under a fixed map composes with a continuation that may extend it. -/ +theorem ResultRenamed.bindExtending {mapping : AllocationRenaming} {relation : α → β → Prop} + {following : AllocationRenaming → γ → δ → Prop} {source : ExecutionResult α} + {target : ExecutionResult β} (related : ResultRenamed mapping relation source target) + (sourceNext : α → ExecutionResult γ) (targetNext : β → ExecutionResult δ) + (next : ∀ left right, source = .ok left → relation left right → + ∃ later, mapping.Extends later ∧ + ResultRenamed later (following later) (sourceNext left) (targetNext right)) : + ∃ later, mapping.Extends later ∧ + ResultRenamed later (following later) (source >>= sourceNext) (target >>= targetNext) := by + cases related with + | error fault => exact ⟨mapping, .refl mapping, .error fault⟩ + | ok relation => exact next _ _ rfl relation +#print axioms ResultRenamed.bindExtending + /-- Sequential transport may use the source computation's successful equation. -/ theorem ResultRenamed.bindWithSuccess {mapping : AllocationRenaming} {relation : α → β → Prop} {following : γ → δ → Prop} @@ -72,6 +87,17 @@ theorem ResultRenamed.map {mapping : AllocationRenaming} {relation : α → β | ok relation => exact .ok (preserved _ _ relation) #print axioms ResultRenamed.map +/-- A success relation that follows from the related one, given the source succeeded, holds. -/ +theorem ResultRenamed.imp {mapping : AllocationRenaming} {relation following : α → β → Prop} + {source : ExecutionResult α} {target : ExecutionResult β} + (related : ResultRenamed mapping relation source target) + (implies : ∀ left right, source = .ok left → relation left right → following left right) : + ResultRenamed mapping following source target := by + cases related with + | error fault => exact .error fault + | ok related => exact .ok (implies _ _ rfl related) +#print axioms ResultRenamed.imp + /-- Equal checked-memory faults become execution faults with corresponding references. -/ theorem ResultEquivalent.checkedAt {mapping : AllocationRenaming} {relation : α → β → Prop} {source : Checked α} {target : Checked β} @@ -86,7 +112,7 @@ theorem ResultEquivalent.checkedAt {mapping : AllocationRenaming} {relation : α /-- A raised CLR exception is unchanged by reference renaming. -/ theorem ResultRenamed.thrown {mapping : AllocationRenaming} {relation : α → β → Prop} - {target : ExecutionResult β} {exception : String} + {target : ExecutionResult β} {exception : ExceptionType} (related : ResultRenamed mapping relation (.error (.thrown exception)) target) : target = .error (.thrown exception) := by cases related; rfl #print axioms ResultRenamed.thrown diff --git a/CIL/Safety/ReturnArity.lean b/CIL/Safety/ReturnArity.lean index 47c916de1..efde74cce 100644 --- a/CIL/Safety/ReturnArity.lean +++ b/CIL/Safety/ReturnArity.lean @@ -18,27 +18,32 @@ private theorem bind_success {source : Except ε α} {continuation : α → Exce cases source <;> simp [Except.bind] /-- Managed dispatch produces a next step or child invocation, never a method return. -/ -private theorem managedStep_not_returned (operation : CIL.ManagedOp) (pc : Nat) (frame : Frame) - (stack : List Value) (memory final : Memory) (values : List Value) : - managedStep operation pc frame stack memory ≠ .ok (.returned values final) := by +private theorem calliStep_not_returned (signature : CIL.ManagedSignature) (tail : Bool) (link : Option CIL.Link) + (frame : Frame) (stack : List Value) (memory final : Memory) (values : List Value) : + calliStep signature tail link frame stack memory ≠ .ok (.returned values final) := by intro executed - unfold managedStep at executed + unfold calliStep at executed simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed repeat' first | cases executed | split at executed -#print axioms managedStep_not_returned +#print axioms calliStep_not_returned set_option maxHeartbeats 400000 in /-- A successful return has its declared arity and exposes no unspecified SIMD lanes. -/ -theorem step_return_properties (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +theorem step_return_properties (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (memory final : Memory) (values : List Value) (executed : step body operation pc args frame stack memory = .ok (.returned values final)) : values.length = (if body.returnsValue then 1 else 0) ∧ values.any Value.hasUnspecified = false := by - unfold step at executed + unfold step observedCall referenceCompareCall referenceSubtractCall newobjStep + constructAggregate constructObject at executed dsimp only at executed + generalize narrowing : body.returnNarrow = narrow at executed repeat' first - | solve | exact False.elim (managedStep_not_returned _ _ _ _ _ _ _ executed) + | unfold returnFrom at executed + | unfold localAllocation at executed + | solve | exact False.elim (calliStep_not_returned _ _ _ _ _ _ _ _ executed) + | solve | (obtain ⟨_, _, _, returned, _⟩ := modelStep_frame executed; cases returned) | solve | simp only [Bind.bind, Pure.pure, Except.pure, bind_success, Except.ok.injEq, reduceCtorEq, and_false, exists_false] at executed @@ -49,18 +54,19 @@ theorem step_return_properties (body : CIL.Method) (operation : CIL.Op) (pc : Na simp_all [observerCount, unspecifiedOperands, List.take_of_length_le, Value.hasUnspecified, CIL.Value.hasUnspecified] | solve | - simp_all only [reduceCtorEq, implies_true, Bool.or_false, Bool.not_eq_true, + simp_all only [reduceCtorEq, implies_true, Bool.not_eq_true, List.reverse_cons, List.reverse_nil, List.nil_append, List.length_cons, List.length_nil, Nat.zero_add, ↓reduceIte, List.any_cons, Value.hasUnspecified, CIL.Value.hasUnspecified, List.any_nil, Bool.or_self, and_self] | cases executed + | unfold initializeSpanHome at executed | obtain ⟨_, _, executed⟩ := bind_success.mp executed | simp only [Bind.bind, Pure.pure, Except.pure] at executed #print axioms step_return_properties /-- A checked return has the arity declared by its actual method body. -/ -theorem step_return_length (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +theorem step_return_length (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (memory final : Memory) (values : List Value) (executed : step body operation pc args frame stack memory = .ok (.returned values final)) : @@ -95,7 +101,7 @@ theorem run_return_length (program : CIL.Program) (fuel method pc : Nat) (body : #print axioms run_return_length /-- Successful returns never expose unspecified SIMD lanes. -/ -theorem step_return_known (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +theorem step_return_known (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (memory final : Memory) (values : List Value) (executed : step body operation pc args frame stack memory = .ok (.returned values final)) : @@ -120,25 +126,43 @@ theorem invoke_return_known {program : CIL.Program} {fuel method : Nat} #print axioms invoke_return_known /-- A successful ordinary call exposes only fully specified arguments. -/ -theorem step_call_known {body : CIL.Method} {callee argc pc : Nat} +theorem step_call_known {body : CIL.Method} {method : Extraction.MethodRef} + {prefixes : CallPrefixes} {callee pc : Nat} {args stack arguments rest : List Value} {frame : Frame} {memory final : Memory} - (executed : step body (.call callee argc) pc args frame stack memory = + (executed : step body (.call method prefixes) pc args frame stack memory = .ok (.call callee arguments rest final)) : arguments.any Value.hasUnspecified = false := by - simp only [step, observerCount, Bool.or_false] at executed + simp only [step] at executed split at executed · cases executed - · rename_i known - split at executed + split at executed + · split at executed + · cases executed + · obtain ⟨_, _, _, called, _⟩ := modelStep_frame executed + cases called + rename_i link _ + unfold observedCall at executed + split at executed + · cases executed + rename_i known + cases link <;> try (first | (cases executed; done) | (simp at executed; done)) + dsimp only at executed + split at executed + · split at executed · cases executed - · cases checked : (stack.take argc).reverse.mapM (checkedValue memory) with - | error fault => simp [checked, Bind.bind, Except.bind] at executed - | ok checkedArgs => - simp only [checked, Bind.bind, Except.bind, Pure.pure, Except.pure, - Except.ok.injEq, FrameAction.call.injEq] at executed - obtain ⟨_, rfl, rfl, rfl⟩ := executed - rw [(checkedValues_valid _ _ _ checked).1] - simpa [unspecifiedOperands] using known + · cases checked : stack.reverse.mapM (callArgument frame memory) <;> + simp [checked, Bind.bind, Except.bind, Pure.pure, Except.pure] at executed + split at executed + · cases executed + · cases checked : + (stack.take method.argumentCount).reverse.mapM (callArgument frame memory) with + | error fault => simp [checked, Bind.bind, Except.bind] at executed + | ok checkedArgs => + simp only [checked, Bind.bind, Except.bind, Pure.pure, Except.pure, + Except.ok.injEq, FrameAction.call.injEq] at executed + obtain ⟨_, rfl, rfl, rfl⟩ := executed + rw [callArguments_hasUnspecified checked] + simpa [unspecifiedOperands] using known #print axioms step_call_known end CIL.Safety diff --git a/CIL/Safety/ReturnReferences.lean b/CIL/Safety/ReturnReferences.lean index 3c27e0ac4..a9fbb7e4b 100644 --- a/CIL/Safety/ReturnReferences.lean +++ b/CIL/Safety/ReturnReferences.lean @@ -5,7 +5,7 @@ import CIL.Safety.LiveValues Every value a successful run or invocation returns passed `checkedValue` against the final memory, after the returning frame was torn down, so the results are `ValuesValid` in the -memory the caller receives. +memory the caller receives and none of them is a home. -/ namespace CIL.Safety @@ -42,4 +42,29 @@ theorem invoke_result_valid (program : CIL.Program) (fuel method : Nat) #print axioms run_result_valid #print axioms invoke_result_valid +/-- No run returns a home, so no result names a slot of a frame that has expired. -/ +theorem run_result_not_spanHome (program : CIL.Program) (fuel method pc : Nat) + (args : List Value) (frame : Frame) (stack : List Value) (m final : Memory) + (values : List Value) + (h : run program fuel method pc args frame stack m = .ok (final, values)) : + ∀ value ∈ values, value.isHome = false := by + have trace := run_success_trace program fuel method pc args frame stack m final values h + clear h + induction trace with + | returned _ checked => exact checkedValues_not_spanHome checked + | next _ _ ih | call _ _ _ _ _ _ _ ih | construct _ _ _ _ _ _ _ _ ih + | tailCall _ _ _ _ _ _ _ _ ih => exact ih + +#print axioms run_result_not_spanHome + +/-- No invocation returns a home. -/ +theorem invoke_result_not_spanHome (program : CIL.Program) (fuel method : Nat) + (args values : List Value) (m final : Memory) + (h : invoke program fuel method args m = .ok (final, values)) : + ∀ value ∈ values, value.isHome = false := by + obtain ⟨_, _, _, _, _, _, _, executed⟩ := invoke_success_setup h + exact run_result_not_spanHome _ _ _ _ _ _ _ _ _ _ executed + +#print axioms invoke_result_not_spanHome + end CIL.Safety diff --git a/CIL/Safety/RuntimeExceptions.lean b/CIL/Safety/RuntimeExceptions.lean index a35670eec..caa31fd8a 100644 --- a/CIL/Safety/RuntimeExceptions.lean +++ b/CIL/Safety/RuntimeExceptions.lean @@ -12,26 +12,26 @@ mapping, and the lemmas mirror the call-composition lemmas of `CIL.Safety.Outcom namespace CIL.Safety /-- The CLR exception a fault surfaces as, if any; other faults are not exceptions. -/ -def ExecutionFault.clrException : ExecutionFault → Option String +def ExecutionFault.clrException : ExecutionFault → Option ExceptionType | .thrown exception => some exception - | .arithmetic .overflow => some "System.OverflowException" - | .arithmetic .divideByZero => some "System.DivideByZeroException" + | .arithmetic .overflow => some .overflow + | .arithmetic .divideByZero => some .divideByZero | _ => none /-- Some invocation fails with a fault that surfaces as exactly `exception`. -/ def RaisesCLR (program : CIL.Program) (method : Nat) (args : List Value) - (memory : Memory) (exception : String) : Prop := + (memory : Memory) (exception : ExceptionType) : Prop := ∃ fuel fault, invoke program fuel method args memory = .error fault ∧ fault.fault.clrException = some exception /-- `RaisesCLR` for a run already in progress at `pc`. -/ def RunRaisesCLR (program : CIL.Program) (method pc : Nat) (args : List Value) - (frame : Frame) (stack : List Value) (memory : Memory) (exception : String) : Prop := + (frame : Frame) (stack : List Value) (memory : Memory) (exception : ExceptionType) : Prop := ∃ fuel fault, run program fuel method pc args frame stack memory = .error fault ∧ fault.fault.clrException = some exception /-- A fault that names a CLR exception is not fuel exhaustion, so more fuel cannot change it. -/ -theorem settled_of_clrException {fault : LocatedFault} {exception : String} +theorem settled_of_clrException {fault : LocatedFault} {exception : ExceptionType} (named : fault.fault.clrException = some exception) : Settled (.error fault : Except LocatedFault α) := by cases fault with @@ -39,30 +39,30 @@ theorem settled_of_clrException {fault : LocatedFault} {exception : String} cases fault <;> simp_all [ExecutionFault.clrException, Settled] theorem RaisesCLR.of_raises {program : CIL.Program} {method : Nat} {args : List Value} - {memory : Memory} {exception : String} + {memory : Memory} {exception : ExceptionType} (raised : Raises program method args memory exception) : RaisesCLR program method args memory exception := by obtain ⟨fuel, fault, invoked, thrown⟩ := raised exact ⟨fuel, fault, invoked, by simp [thrown, ExecutionFault.clrException]⟩ theorem RaisesCLR.of_invoke {program : CIL.Program} {fuel method : Nat} {args : List Value} - {memory : Memory} {fault : LocatedFault} {exception : String} + {memory : Memory} {fault : LocatedFault} {exception : ExceptionType} (invoked : invoke program fuel method args memory = .error fault) (named : fault.fault.clrException = some exception) : RaisesCLR program method args memory exception := ⟨fuel, fault, invoked, named⟩ /-- A settled invocation result that names a different exception, or none, refutes - `RaisesCLR`. -/ + `RaisesCLR`. An `unsupported` fault is not settled, so it refutes no expected exception. -/ theorem RaisesCLR.not_of_invoke {program : CIL.Program} {fuel method : Nat} {args : List Value} - {memory : Memory} {fault : LocatedFault} {exception : String} + {memory : Memory} {fault : LocatedFault} {exception : ExceptionType} (settled : Settled (.error fault : Except LocatedFault (Memory × List Value))) (invoked : invoke program fuel method args memory = .error fault) (different : fault.fault.clrException ≠ some exception) : ¬ RaisesCLR program method args memory exception := by rintro ⟨otherFuel, otherFault, otherInvoked, named⟩ have same : fault = otherFault := Except.error.inj - (invoke_unique settled (settled_of_clrException named) invoked otherInvoked) + (invoke_unique settled.stable (settled_of_clrException named).stable invoked otherInvoked) subst otherFault exact different named @@ -79,7 +79,8 @@ def OutcomeContract.HoldsCLR {Input Output : Type} (c : OutcomeContract Input Ou Confined (c.footprint input) initial final ∧ c.Returns input initial final values theorem OutcomeContract.HoldsCLR.raising {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method : Nat} {input : Input} {initial : Memory} {exception : String} + {program : CIL.Program} {method : Nat} {input : Input} {initial : Memory} + {exception : ExceptionType} (holds : c.HoldsCLR program method) (permitted : c.pre input initial) (raised : c.raises input initial = some exception) : RaisesCLR program method (c.args input) initial exception := by @@ -98,6 +99,79 @@ theorem OutcomeContract.HoldsCLR.returning {Input Output : Type} {c : OutcomeCon rw [normal] at outcome exact outcome +/-- How a settled outcome at `input` from `initial` fails `c` in the CLR reading: where `c` names + an exception, anything but a fault that surfaces as it; otherwise what + `MethodContract.Violates` says. -/ +def OutcomeContract.ViolatesCLR {Input Output : Type} (c : OutcomeContract Input Output) + (input : Input) (initial : Memory) (outcome : Except LocatedFault (Memory × List Value)) : + Prop := + match c.raises input initial, outcome with + | some exception, .error fault => fault.fault.clrException ≠ some exception + | some _, .ok _ => True + | none, outcome => c.toMethodContract.Violates input initial outcome + +/-- A counterexample to `c` in the CLR reading: an input and memory meeting `pre` from which the + method settles outside the contract or never terminates. It is evidence that the method + violates the contract in the model; a run that reaches an operation the model does not cover + (`unsupported`), or that exhausts one fuel budget, is no evidence and gives no + `RefutedCLR`. -/ +def OutcomeContract.RefutedCLR {Input Output : Type} (c : OutcomeContract Input Output) + (program : CIL.Program) (method : Nat) : Prop := + ∃ input initial, c.pre input initial ∧ + Counterexample program method (c.args input) initial (c.ViolatesCLR input initial) + +/-- A contract refuted in the CLR reading does not hold in it. -/ +theorem OutcomeContract.RefutedCLR.not_holds {Input Output : Type} + {c : OutcomeContract Input Output} {program : CIL.Program} {method : Nat} + (refuted : c.RefutedCLR program method) : ¬ c.HoldsCLR program method := by + intro holds + obtain ⟨input, initial, permitted, counter⟩ := refuted + cases named : c.raises input initial with + | none => + obtain ⟨fuel, final, values, ran, framed, returned⟩ := holds.returning permitted named + have wrong := counter.violates (outcome := .ok (final, values)) trivial ran + simp only [OutcomeContract.ViolatesCLR, MethodContract.Violates, named] at wrong + exact wrong ⟨framed, returned⟩ + | some exception => + obtain ⟨fuel, fault, ran, surfaced⟩ := holds.raising permitted named + have wrong := counter.violates (settled_of_clrException surfaced) ran + simp only [OutcomeContract.ViolatesCLR, named] at wrong + exact wrong surfaced + +/-- A located definite fault that does not surface as the exception `c` names, on an input + meeting the precondition, refutes the contract in the CLR reading. -/ +theorem OutcomeContract.RefutedCLR.of_fault {Input Output : Type} + {c : OutcomeContract Input Output} {program : CIL.Program} {method observed : Nat} + {input : Input} {initial : Memory} {fault : LocatedFault} + (permitted : c.pre input initial) (definite : fault.fault.definite ≠ false) + (failed : invoke program observed method (c.args input) initial = .error fault) + (wrong : c.ViolatesCLR input initial (.error fault)) : c.RefutedCLR program method := + ⟨input, initial, permitted, .inl ⟨observed, _, failed, definite, wrong⟩⟩ + +/-- A return that `c` does not allow, on an input meeting the precondition, refutes it in the + CLR reading. -/ +theorem OutcomeContract.RefutedCLR.of_return {Input Output : Type} + {c : OutcomeContract Input Output} {program : CIL.Program} {method fuel : Nat} + {input : Input} {initial final : Memory} {values : List Value} + (permitted : c.pre input initial) + (ran : invoke program fuel method (c.args input) initial = .ok (final, values)) + (wrong : c.ViolatesCLR input initial (.ok (final, values))) : c.RefutedCLR program method := + ⟨input, initial, permitted, .inl ⟨fuel, _, ran, trivial, wrong⟩⟩ + +/-- A run that exhausts every fuel budget on an input meeting the precondition refutes the + contract in the CLR reading. -/ +theorem OutcomeContract.RefutedCLR.of_diverges {Input Output : Type} + {c : OutcomeContract Input Output} {program : CIL.Program} {method : Nat} + {input : Input} {initial : Memory} (permitted : c.pre input initial) + (diverges : Diverges program method (c.args input) initial) : + c.RefutedCLR program method := + ⟨input, initial, permitted, .inr diverges⟩ + +#print axioms OutcomeContract.RefutedCLR.not_holds +#print axioms OutcomeContract.RefutedCLR.of_fault +#print axioms OutcomeContract.RefutedCLR.of_return +#print axioms OutcomeContract.RefutedCLR.of_diverges + /-- Every proved `OutcomeContract.Holds` also holds in the CLR reading. -/ theorem OutcomeContract.Holds.clr {Input Output : Type} {c : OutcomeContract Input Output} {program : CIL.Program} {method : Nat} (holds : c.Holds program method) : @@ -108,7 +182,7 @@ theorem OutcomeContract.Holds.clr {Input Output : Type} {c : OutcomeContract Inp | some exception => exact RaisesCLR.of_raises (holds.raising permitted named) theorem raisesCLR_of_run {program : CIL.Program} {method : Nat} {body : CIL.Method} - {args : List Value} {frame : Frame} {memory entered : Memory} {exception : String} + {args : List Value} {frame : Frame} {memory entered : Memory} {exception : ExceptionType} (found : program[method]? = some body) (checked : args.mapM (checkedValue memory) = .ok args) (setup : enterFrame body args memory = .ok (frame, entered)) @@ -116,12 +190,12 @@ theorem raisesCLR_of_run {program : CIL.Program} {method : Nat} {body : CIL.Meth RaisesCLR program method args memory exception := by obtain ⟨fuel, fault, ran, named⟩ := raised refine ⟨fuel, fault, ?_, named⟩ - simp only [invoke, found, checked, setup, Except.mapError, Bind.bind, Except.bind] + simp only [invoke_eq, found, checked, setup, Except.mapError, Bind.bind, Except.bind] exact ran theorem run_next_raisesCLR {program : CIL.Program} {method pc target : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} - {frame nextFrame : Frame} {memory updated : Memory} {exception : String} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} + {frame nextFrame : Frame} {memory updated : Memory} {exception : ExceptionType} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .ok (.next target values nextFrame updated)) @@ -132,21 +206,22 @@ theorem run_next_raisesCLR {program : CIL.Program} {method pc target : Nat} /-- A run raises at a step whose fault surfaces as a CLR exception. -/ theorem run_fault_raisesCLR {program : CIL.Program} {method pc : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack : List Value} {frame : Frame} {memory : Memory} - {fault : ExecutionFault} {exception : String} + {body : CIL.Method} {op : CIL.Instruction} {args stack : List Value} {frame : Frame} + {memory : Memory} + {fault : ExecutionFault} {exception : ExceptionType} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .error fault) (named : fault.clrException = some exception) : RunRaisesCLR program method pc args frame stack memory exception := ⟨1, ⟨method, pc, fault⟩, by - simp only [run, methodFound, instructionFound, stepped, Except.mapError, Bind.bind, + simp only [run_succ, methodFound, instructionFound, stepped, Except.mapError, Bind.bind, Except.bind], named⟩ /-- Propagate the CLR exception a callee's contract names for this input. -/ theorem run_call_raisesCLR_of_contract {Input Output : Type} {c : OutcomeContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} - {exception : String} + {exception : ExceptionType} (holds : c.HoldsCLR program callee) (input : Input) (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = diff --git a/CIL/Safety/RuntimeTypeSegment.lean b/CIL/Safety/RuntimeTypeSegment.lean new file mode 100644 index 000000000..a683c3dea --- /dev/null +++ b/CIL/Safety/RuntimeTypeSegment.lean @@ -0,0 +1,78 @@ +import CIL.Safety.Segment + +/-! Run-to-run summaries for the original token loads and System.Type calls of a closed typeof comparison. -/ + +namespace CIL.Safety + +/-- The actual getter converts the handle at the top of the stack to its runtime type. -/ +theorem step_getTypeFromHandle {body : CIL.Method} {member : CIL.Extraction.MethodRef} + {pc : Nat} {args stack : List Value} {frame : Frame} {memory : Memory} + (link : body.linkAt pc = none) + (getter : CIL.Extraction.runtimeModel body.environment .call member = some .typeFromHandle) + (type : CIL.Extraction.TypeExpr) : + step body (.call member {}) pc args frame (.typeHandle type :: stack) memory = + .ok (.next (pc + 1) (.runtimeType type :: stack) frame memory) := by + rw [step_call_model (link := link) (found := getter) (direct := rfl)] + rfl +#print axioms step_getTypeFromHandle + +/-- An ldtoken and its checked getter preserve the continuation, frame and caller memory. -/ +theorem SegmentRun.loadRuntimeType {program : CIL.Program} {body : CIL.Method} + {method pc : Nat} {type : CIL.Extraction.TypeExpr} {member : CIL.Extraction.MethodRef} + (found : program[method]? = some body) + (token : body.code[pc]? = some (.ldtoken (.type type))) + (call : body.code[pc + 1]? = some (.call member {})) + (link : body.linkAt (pc + 1) = none) + (getter : CIL.Extraction.runtimeModel body.environment .call member = some .typeFromHandle) + (args : List Value) (frame : Frame) (stack : List Value) (memory : Memory) : + SegmentRun program method pc (pc + 2) 2 args ⟨frame, stack, memory⟩ + ⟨frame, .runtimeType type :: stack, memory⟩ := by + intro fuel + exact (run_next (fuel := fuel + 1) found token + (step_ldtoken_type_eq body pc args frame memory type stack)).trans + (run_next (fuel := fuel) found call (step_getTypeFromHandle link getter type)) +#print axioms SegmentRun.loadRuntimeType + +/-- A checked equality or inequality operator consumes the two types and pushes an i32. -/ +theorem step_runtimeTypeComparison {body : CIL.Method} {member : CIL.Extraction.MethodRef} + {pc : Nat} {args stack : List Value} {frame : Frame} {memory : Memory} + {left right : CIL.Extraction.TypeExpr} {equal same : Bool} + (link : body.linkAt pc = none) + (model : CIL.Extraction.runtimeModel body.environment .call member = some (.typeEquality equal)) + (identity : CIL.Extraction.sameRuntimeType left right = some same) : + step body (.call member {}) pc args frame (.runtimeType right :: .runtimeType left :: stack) memory = + .ok (.next (pc + 1) (.scalar (.i32 (if same == equal then 1 else 0)) :: stack) frame memory) := by + rw [step_call_model (link := link) (found := model) (direct := rfl)] + change (do + let answer ← runtimeTypeEqual left right + pure (FrameAction.next (pc + 1) (.scalar (.i32 (if answer == equal then 1 else 0)) :: stack) + frame memory)) = _ + simp only [runtimeTypeEqual, identity, Bind.bind, Except.bind, Pure.pure, Except.pure] +#print axioms step_runtimeTypeComparison + +/-- Two token/getter pairs and a comparison call preserve the remaining stack and memory. -/ +theorem SegmentRun.compareRuntimeTypes {program : CIL.Program} {body : CIL.Method} + {method pc : Nat} {left right : CIL.Extraction.TypeExpr} + {getter comparison : CIL.Extraction.MethodRef} {equal same : Bool} + (found : program[method]? = some body) + (leftToken : body.code[pc]? = some (.ldtoken (.type left))) + (leftCall : body.code[pc + 1]? = some (.call getter {})) + (rightToken : body.code[pc + 2]? = some (.ldtoken (.type right))) + (rightCall : body.code[pc + 3]? = some (.call getter {})) + (test : body.code[pc + 4]? = some (.call comparison {})) + (leftLink : body.linkAt (pc + 1) = none) (rightLink : body.linkAt (pc + 3) = none) + (testLink : body.linkAt (pc + 4) = none) + (handle : CIL.Extraction.runtimeModel body.environment .call getter = some .typeFromHandle) + (model : CIL.Extraction.runtimeModel body.environment .call comparison = some (.typeEquality equal)) + (identity : CIL.Extraction.sameRuntimeType left right = some same) + (args : List Value) (frame : Frame) (stack : List Value) (memory : Memory) : + SegmentRun program method pc (pc + 5) 5 args ⟨frame, stack, memory⟩ + ⟨frame, .scalar (.i32 (if same == equal then 1 else 0)) :: stack, memory⟩ := by + intro fuel + exact (SegmentRun.loadRuntimeType found leftToken leftCall leftLink handle args frame stack memory + (fuel + 3)).trans <| (SegmentRun.loadRuntimeType found rightToken rightCall rightLink handle + args frame (.runtimeType left :: stack) memory (fuel + 1)).trans <| + run_next (fuel := fuel) found test (step_runtimeTypeComparison testLink model identity) +#print axioms SegmentRun.compareRuntimeTypes + +end CIL.Safety diff --git a/CIL/Safety/ScalarBranch.lean b/CIL/Safety/ScalarBranch.lean index aff280cc3..b0a7798cd 100644 --- a/CIL/Safety/ScalarBranch.lean +++ b/CIL/Safety/ScalarBranch.lean @@ -3,7 +3,7 @@ import CIL.Safety.Execution /-! # Word branch step -The checked step for `brnonzero` on a 64-bit scalar, in the `if value = 0` form that loop and +The checked step for `brtrue` on a 64-bit scalar, in the `if value = 0` form that loop and branch proofs split on. -/ @@ -12,7 +12,7 @@ namespace CIL.Safety /-- A word-valued conditional branch checks the scalar operand and preserves memory. -/ theorem step_word_nonzero (body : CIL.Method) (pc target : Nat) (args : List Value) (frame : Frame) (value : BitVec 64) (memory : Memory) : - step body (.brnonzero target) pc args frame [.scalar (.i64 value)] memory = + step body (.brtrue target) pc args frame [.scalar (.i64 value)] memory = .ok (.next (if value = 0 then pc + 1 else target) [] frame memory) := by change Except.ok (FrameAction.next (if value != 0 then target else pc + 1) [] frame memory) = _ by_cases h : value = 0 <;> simp [h] diff --git a/CIL/Safety/SegmentContract.lean b/CIL/Safety/SegmentContract.lean new file mode 100644 index 000000000..beb15b3bc --- /dev/null +++ b/CIL/Safety/SegmentContract.lean @@ -0,0 +1,681 @@ +import CIL.Safety.SegmentLocals +import CIL.Safety.RuntimeExceptions +import CIL.Safety.RegionRun + +/-! Segment contracts retain checked interpreter outcomes and memory confinement. -/ + +namespace CIL.Safety + +/-- Stable outcomes agree across a prefix, which consumes at least `cost` fuel. -/ +structure ReachesRun (program : CIL.Program) (method start finish cost : Nat) + (arguments : List Value) (before after : SegmentState) : Prop where + spends : forall fuel outcome, Stable outcome -> + run program fuel method start arguments before.frame before.stack before.memory = outcome -> + And (cost <= fuel) + (run program (fuel - cost) method finish arguments after.frame after.stack after.memory = + outcome) + continues : forall fuel outcome, Stable outcome -> + run program fuel method finish arguments after.frame after.stack after.memory = outcome -> + Exists fun more => + run program more method start arguments before.frame before.stack before.memory = outcome + +/-- Every exact segment preserves stable outcomes with its exact fuel lower bound. -/ +theorem SegmentRun.reaches {program : CIL.Program} {method start finish cost : Nat} + {arguments : List Value} {before after : SegmentState} + (exactRun : SegmentRun program method start finish cost arguments before after) : + ReachesRun program method start finish cost arguments before after := by + constructor + next => + intro fuel outcome settled ran + have bound : cost <= fuel := by + apply Classical.byContradiction + intro short + have more := run_mono program fuel (cost - fuel) method start arguments + before.frame before.stack before.memory outcome settled ran + have zero := exactRun 0 + rw [Nat.zero_add] at zero + rw [show fuel + (cost - fuel) = cost by omega, zero] at more + simp only [run_zero] at more + rw [<- more] at settled + exact settled rfl + exact And.intro bound (by + have equation := exactRun (fuel - cost) + rw [Nat.sub_add_cancel bound, ran] at equation + exact equation.symm) + next => + intro fuel outcome _ ran + exact Exists.intro (fuel + cost) ((exactRun fuel).trans ran) + +#print axioms SegmentRun.reaches + +/-- Adjacent reaching prefixes add their fuel lower bounds. -/ +theorem ReachesRun.trans {program : CIL.Program} + {method start middle finish first second : Nat} + {arguments : List Value} {before intermediate after : SegmentState} + (head : ReachesRun program method start middle first arguments before intermediate) + (tail : ReachesRun program method middle finish second arguments intermediate after) : + ReachesRun program method start finish (first + second) arguments before after := by + constructor + next => + intro fuel outcome settled ran + cases head.spends fuel outcome settled ran + rename_i headBound headRun + cases tail.spends (fuel - first) outcome settled headRun + rename_i tailBound tailRun + exact And.intro (by omega) (by simpa [Nat.sub_sub] using tailRun) + next => + intro fuel outcome settled ran + cases tail.continues fuel outcome settled ran + rename_i middleFuel middleRun + exact head.continues middleFuel outcome settled middleRun + +#print axioms ReachesRun.trans + +/-- A successful continuation proves the same postcondition before the prefix. -/ +theorem ReachesRun.wp {program : CIL.Program} {method start finish cost : Nat} + {arguments : List Value} {before after : SegmentState} + {post : Prod Memory (List Value) -> Prop} + (reached : ReachesRun program method start finish cost arguments before after) + (rest : WP (Execution (fun fuel => + run program fuel method finish arguments after.frame after.stack after.memory)) post) : + WP (Execution (fun fuel => + run program fuel method start arguments before.frame before.stack before.memory)) post := by + cases rest + rename_i fuel resultWitness + cases resultWitness + rename_i result runPost + cases runPost + rename_i ran satisfied + cases reached.continues fuel (.ok result) trivial ran + rename_i more continued + exact Exists.intro more (Exists.intro result (And.intro continued satisfied)) + +#print axioms ReachesRun.wp + +/-- A returning callee reaches the caller continuation with its results pushed. + The checked step supplies the callee arguments independently of the caller stack. -/ +theorem ReachesRun.call {program : CIL.Program} {method pc callee : Nat} + {body child : CIL.Method} {op : CIL.Instruction} + {arguments stack values rest results : List Value} {frame childFrame : Frame} + {memory updated entered final : Memory} {calleeFuel : Nat} + (found : program[method]? = some body) (instruction : body.code[pc]? = some op) + (stepped : step body op pc arguments frame stack memory = + .ok (.call callee values rest updated)) + (calleeFound : program[callee]? = some child) + (enter : enterFrame child values updated = .ok (childFrame, entered)) + (returned : run program calleeFuel callee 0 values childFrame [] entered = + .ok (final, results)) : + ReachesRun program method pc (pc + 1) 1 arguments + { frame := frame, stack := stack, memory := memory } + { frame := frame, stack := results ++ rest, memory := final } := by + constructor + next => + intro fuel outcome settled ran + cases fuel with + | zero => + simp only [run_zero] at ran + subst outcome + exact False.elim (settled rfl) + | succ fuel => + simp only [run_succ, found, instruction, stepped, calleeFound, enter, + Bind.bind, Except.bind, Except.mapError] at ran + cases childRun : run program fuel callee 0 values childFrame [] entered with + | error fault => + simp only [childRun] at ran + have childSettled : Stable (.error fault : Except LocatedFault + (Prod Memory (List Value))) := by simpa [ran] using settled + have failed := run_mono program fuel calleeFuel callee 0 values childFrame [] entered + (.error fault) childSettled childRun + have succeeded := run_mono program calleeFuel fuel callee 0 values childFrame [] entered + (.ok (final, results)) trivial returned + rw [Nat.add_comm, failed] at succeeded + cases succeeded + | ok result => + have actual := run_mono program fuel calleeFuel callee 0 values childFrame [] entered + (.ok result) trivial childRun + have expected := run_mono program calleeFuel fuel callee 0 values childFrame [] entered + (.ok (final, results)) trivial returned + rw [Nat.add_comm, actual] at expected + cases Except.ok.inj expected + exact And.intro (by omega) (by simpa [childRun] using ran) + next => + intro fuel outcome settled ran + have childRun := run_mono program calleeFuel fuel callee 0 values childFrame [] entered + (.ok (final, results)) trivial returned + have continuation := run_mono program fuel calleeFuel method (pc + 1) arguments frame + (results ++ rest) final outcome settled ran + refine Exists.intro (calleeFuel + fuel + 1) ?_ + simpa only [run_zero, run_succ, found, instruction, stepped, calleeFound, enter, + Bind.bind, Except.bind, Except.mapError, childRun, Nat.add_comm fuel calleeFuel] + using continuation + +#print axioms ReachesRun.call + +/-- Agreement on a closed region transports reaching between two positions inside it. -/ +theorem ReachesRun.congr_region {program other : CIL.Program} + {region : Nat -> Nat -> Prop} {method start finish cost : Nat} + {arguments : List Value} {before after : SegmentState} + (closed : RegionClosed program region) (agrees : AgreesOn program other region) + (entry : region method start) (continuation : region method finish) + (reached : ReachesRun program method start finish cost arguments before after) : + ReachesRun other method start finish cost arguments before after := by + constructor + next => + intro fuel outcome settled ran + rw [run_congr_region closed agrees _ _ _ _ _ _ _ entry] at ran + cases reached.spends fuel outcome settled ran + rename_i bound continued + exact And.intro bound (by + rw [run_congr_region closed agrees _ _ _ _ _ _ _ continuation] + exact continued) + next => + intro fuel outcome settled ran + rw [run_congr_region closed agrees _ _ _ _ _ _ _ continuation] at ran + cases reached.continues fuel outcome settled ran + rename_i more continued + exact Exists.intro more (by + rw [run_congr_region closed agrees _ _ _ _ _ _ _ entry] + exact continued) + +#print axioms ReachesRun.congr_region + +/-- Control reaches a continuation, returns, faults, or exhausts every fuel budget. -/ +inductive SegmentExit where + | reaches (pc : Nat) (state : SegmentState) + | returns (memory : Memory) (values : List Value) + | faults (fault : LocatedFault) + | diverges + +/-- The exit's fault names this CLR exception, including arithmetic exceptions. -/ +def SegmentExit.Raises (exit : SegmentExit) (exception : CIL.ExceptionType) : Prop := + Exists fun fault => And (exit = .faults fault) (fault.fault.clrException = some exception) + +/-- An exit has interpreter evidence. A fault exit is definite, so an `unsupported` fault is no + exit; reaching also requires a positive fuel lower bound. -/ +def SegmentExit.Exhibited (program : CIL.Program) (method start : Nat) + (arguments : List Value) (before : SegmentState) : SegmentExit -> Prop + | .reaches pc after => Exists fun cost => And (0 < cost) + (ReachesRun program method start pc cost arguments before after) + | .returns memory values => Exists fun fuel => + run program fuel method start arguments before.frame before.stack before.memory = + .ok (memory, values) + | .faults fault => And (fault.fault.definite ≠ false) (Exists fun fuel => + run program fuel method start arguments before.frame before.stack before.memory = + .error fault) + | .diverges => forall fuel, Exists fun fault => And (fault.fault = .fuelExhausted) + (run program fuel method start arguments before.frame before.stack before.memory = + .error fault) + +/-- A loop exhausts every fuel budget if each state in its invariant reaches another such + state and consumes positive fuel. Both the program counter and memory can vary. -/ +theorem SegmentExit.diverges_of_recurrent {program : CIL.Program} {method : Nat} + {arguments : List Value} {invariant : Nat → SegmentState → Prop} + (advance : ∀ pc state, invariant pc state → ∃ next after cost, + 0 < cost ∧ ReachesRun program method pc next cost arguments state after ∧ + invariant next after) + {pc : Nat} {state : SegmentState} (valid : invariant pc state) : + SegmentExit.Exhibited program method pc arguments state .diverges := by + have unstable (fuel pc : Nat) (state : SegmentState) (valid : invariant pc state) + (outcome : Except LocatedFault (Memory × List Value)) (stable : Stable outcome) : + run program fuel method pc arguments state.frame state.stack state.memory ≠ outcome := by + induction fuel using Nat.strongRecOn generalizing pc state with + | ind fuel smaller => + intro ran + obtain ⟨next, after, cost, positive, reached, preserved⟩ := advance pc state valid + obtain ⟨bound, continued⟩ := reached.spends fuel outcome stable ran + exact smaller (fuel - cost) (by omega) next after preserved continued + intro fuel + cases observed : run program fuel method pc arguments state.frame state.stack state.memory with + | ok result => exact False.elim (unstable fuel pc state valid (.ok result) trivial observed) + | error fault => + refine ⟨fault, ?_, rfl⟩ + by_cases exhausted : fault.fault = .fuelExhausted + · exact exhausted + · exact False.elim (unstable fuel pc state valid (.error fault) exhausted observed) + +#print axioms SegmentExit.diverges_of_recurrent + +/-- Continuing and returned memories preserve existing storage outside the footprint. -/ +def SegmentExit.Confined (footprint : List (Prod Reference Nat)) (before : SegmentState) : + SegmentExit -> Prop + | .reaches _ after => CIL.Safety.Confined footprint before.memory after.memory + | .returns memory _ => CIL.Safety.Confined footprint before.memory memory + | .faults _ => True + | .diverges => True + +/-- The byte homes and widths of the frame slots a code segment of `body` stores to directly. -/ +def SegmentState.localFootprint (body : CIL.Method) (code : List CIL.Instruction) + (state : SegmentState) : List (Prod Reference Nat) := + let locals : Array LocalSlot := state.frame.locals.toArray + (segmentStores body code).filterMap fun index => match locals[index]? with + | some (LocalSlot.bytes kind home) => some (home, localWidth kind) + | _ => none + +/-- A boundary precondition, permitted memory writes and predicate on checked exits. -/ +structure SegmentContract where + pre : List Value -> SegmentState -> Prop + footprint : List Value -> SegmentState -> List (Prod Reference Nat) := fun _ _ => [] + post : List Value -> SegmentState -> SegmentExit -> Prop + +/-- Every permitted input exhibits an exit within the footprint satisfying the postcondition. -/ +def SegmentContract.Holds (contract : SegmentContract) (program : CIL.Program) + (method start : Nat) : Prop := + forall arguments before, contract.pre arguments before -> Exists fun exit => + And (exit.Exhibited program method start arguments before) + (And (exit.Confined (contract.footprint arguments before) before) + (contract.post arguments before exit)) + +/-- Named evidence retained by one application of a checked segment contract. -/ +structure SegmentResult (contract : SegmentContract) (program : CIL.Program) + (method start : Nat) (arguments : List Value) (before : SegmentState) + (exit : SegmentExit) : Prop where + ran : exit.Exhibited program method start arguments before + framed : exit.Confined (contract.footprint arguments before) before + post : contract.post arguments before exit + +/-- An impossible precondition describes unused successors. -/ +def SegmentContract.unreachable : SegmentContract where + pre _ _ := False + post _ _ _ := False + +/-- An unreachable contract holds at every instruction position. -/ +theorem SegmentContract.unreachable_holds (program : CIL.Program) (method start : Nat) : + unreachable.Holds program method start := by + intro arguments before impossible + exact False.elim impossible + +#print axioms SegmentContract.unreachable_holds + +/-- A contract that reaches one specified continuation with a relational state postcondition. -/ +def SegmentContract.continuing (finish : Nat) (pre : List Value -> SegmentState -> Prop) + (post : List Value -> SegmentState -> SegmentState -> Prop) + (footprint : List Value -> SegmentState -> List (Prod Reference Nat)) : SegmentContract where + pre := pre + footprint := footprint + post arguments before + | .reaches pc after => And (pc = finish) (post arguments before after) + | _ => False + +/-- An exact segment between increasing pcs supplies a positive reaching contract. -/ +theorem Segment.contract {program : CIL.Program} {method start finish : Nat} + {pre post : List Value -> SegmentState -> Prop} + {footprint : List Value -> SegmentState -> List (Prod Reference Nat)} + (segment : Segment program method start finish pre post) (distinct : start < finish) + (framed : forall arguments before after, pre arguments before -> post arguments after -> + Confined (footprint arguments before) before.memory after.memory) : + (SegmentContract.continuing finish pre (fun arguments _ after => post arguments after) + footprint).Holds program method start := by + intro arguments before permitted + cases segment arguments before permitted + rename_i cost stateWitness + cases stateWitness + rename_i after runPost + cases runPost + rename_i exactRun satisfied + have positive : 0 < cost := by + cases cost with + | zero => + have zero := exactRun 0 + simp only [Nat.add_zero, run_zero, Except.error.injEq, LocatedFault.mk.injEq] at zero + omega + | succ cost => omega + exact Exists.intro (.reaches finish after) + (And.intro (Exists.intro cost (And.intro positive exactRun.reaches)) + (And.intro (framed arguments before after permitted satisfied) (And.intro rfl satisfied))) + +#print axioms Segment.contract + +/-- A successful goal after each permitted exit establishes success before the segment. -/ +def SegmentExit.Continues (program : CIL.Program) (method : Nat) (arguments : List Value) + (post : Prod Memory (List Value) -> Prop) : SegmentExit -> Prop + | .reaches pc after => WP (Execution (fun fuel => + run program fuel method pc arguments after.frame after.stack after.memory)) post + | .returns memory values => post (memory, values) + | .faults _ => False + | .diverges => False + +/-- A checked summary and its exit obligations imply a successful interpreter run. -/ +theorem SegmentContract.Holds.wp {contract : SegmentContract} {program : CIL.Program} + {method start : Nat} {arguments : List Value} {before : SegmentState} + {post : Prod Memory (List Value) -> Prop} + (holds : contract.Holds program method start) (permitted : contract.pre arguments before) + (next : forall exit, SegmentResult contract program method start arguments before exit -> + exit.Continues program method arguments post) : + WP (Execution (fun fuel => + run program fuel method start arguments before.frame before.stack before.memory)) post := by + cases holds arguments before permitted + rename_i exit exitWitness + cases exitWitness + rename_i ran framedPost + cases framedPost + rename_i framed satisfied + have rest := next exit { ran := ran, framed := framed, post := satisfied } + cases exit with + | reaches pc after => + cases ran + rename_i cost progress + cases progress + rename_i _positive reached + exact reached.wp rest + | returns memory values => + cases ran + rename_i fuel returned + exact Exists.intro fuel (Exists.intro (memory, values) (And.intro returned rest)) + | faults fault => exact False.elim rest + | diverges => exact False.elim rest + +#print axioms SegmentContract.Holds.wp + +/-- Named exceptions at an exit or its continuation give the same exception at entry. -/ +theorem SegmentContract.Holds.raises {contract : SegmentContract} {program : CIL.Program} + {method start : Nat} {arguments : List Value} {before : SegmentState} + {exception : CIL.ExceptionType} + (holds : contract.Holds program method start) (permitted : contract.pre arguments before) + (next : forall exit, SegmentResult contract program method start arguments before exit -> + Or (exit.Raises exception) (Exists fun pc => Exists fun after => + And (exit = .reaches pc after) + (RunRaisesCLR program method pc arguments after.frame after.stack after.memory + exception))) : + RunRaisesCLR program method start arguments before.frame before.stack before.memory + exception := by + cases holds arguments before permitted + rename_i exit exitWitness + cases exitWitness + rename_i ran framedPost + cases framedPost + rename_i framed satisfied + rcases next exit { ran := ran, framed := framed, post := satisfied } with named | continued + next => + cases named + rename_i fault named + rcases named.1 with rfl + have named := named.2 + cases ran + rename_i _settled failure + cases failure + rename_i fuel failed + exact Exists.intro fuel (Exists.intro fault (And.intro failed named)) + next => + cases continued + rename_i pc continued + cases continued + rename_i after continued + rcases continued.1 with rfl + have raised := continued.2 + cases ran + rename_i cost progress + cases progress + rename_i _positive reached + cases raised + rename_i fuel faultWitness + cases faultWitness + rename_i fault failureName + cases failureName + rename_i failed named + cases reached.continues fuel (.error fault) (settled_of_clrException named).stable failed + rename_i more failed + exact Exists.intro more (Exists.intro fault (And.intro failed named)) + +#print axioms SegmentContract.Holds.raises + +/-- An exhibited exit after a reaching prefix is also exhibited before that prefix. -/ +theorem ReachesRun.exhibited {program : CIL.Program} {method start finish cost : Nat} + {arguments : List Value} {before after : SegmentState} {exit : SegmentExit} + (reached : ReachesRun program method start finish cost arguments before after) + (ran : exit.Exhibited program method finish arguments after) : + exit.Exhibited program method start arguments before := by + cases exit with + | reaches pc final => + cases ran + rename_i nextCost progress + cases progress + rename_i positive tail + exact Exists.intro (cost + nextCost) (And.intro (by omega) (reached.trans tail)) + | returns memory values => + cases ran + rename_i fuel returned + exact reached.continues fuel (.ok (memory, values)) trivial returned + | faults fault => + cases ran + rename_i settled failure + cases failure + rename_i fuel failed + exact And.intro settled (reached.continues fuel (.error fault) + (Settled.stable (α := Prod Memory (List Value)) (outcome := .error fault) settled) failed) + | diverges => + intro fuel + cases actual : run program fuel method start arguments + before.frame before.stack before.memory with + | ok result => + cases reached.spends fuel (.ok result) trivial actual + rename_i _bound continued + cases ran (fuel - cost) + rename_i fault failure + cases failure + rename_i _exhausted exhausted + rw [continued] at exhausted + cases exhausted + | error fault => + refine Exists.intro fault (And.intro ?_ rfl) + apply Classical.byContradiction + intro settled + cases reached.spends fuel (.error fault) settled actual + rename_i _bound continued + cases ran (fuel - cost) + rename_i exhaustedFault failure + cases failure + rename_i exhausted failed + rw [continued] at failed + exact settled ((congrArg LocatedFault.fault (Except.error.inj failed)).trans exhausted) + +#print axioms ReachesRun.exhibited + +/-- An entered method diverges if its prefix reaches a cutpoint that exhausts every budget. -/ +theorem Diverges.of_reaches {program : CIL.Program} {method pc cost : Nat} {body : CIL.Method} + {arguments : List Value} {memory : Memory} {frame : Frame} {entered : Memory} + {head : SegmentState} (found : program[method]? = some body) + (checked : arguments.mapM (checkedValue memory) = .ok arguments) + (setup : enterFrame body arguments memory = .ok (frame, entered)) + (reached : ReachesRun program method 0 pc cost arguments ⟨frame, [], entered⟩ head) + (recurrent : SegmentExit.Exhibited program method pc arguments head .diverges) : + Diverges program method arguments memory := by + intro fuel + obtain ⟨fault, exhausted, ran⟩ := reached.exhibited recurrent fuel + refine ⟨fault, ?_, exhausted⟩ + simpa only [invoke_eq, found, checked, setup, Except.mapError, Bind.bind, Except.bind] + using ran + +#print axioms Diverges.of_reaches + +/-- Joined contracts preserve every stopping exit and compose each reachable successor. -/ +theorem SegmentContract.Holds.seq {head : SegmentContract} {next : Nat -> SegmentContract} + {composed : SegmentContract} {program : CIL.Program} {method start : Nat} + (first : head.Holds program method start) + (rest : forall pc, (next pc).Holds program method pc) + (enters : forall arguments before, composed.pre arguments before -> head.pre arguments before) + (links : forall arguments before pc after, composed.pre arguments before -> + head.post arguments before (.reaches pc after) -> (next pc).pre arguments after) + (stops : forall arguments before exit, composed.pre arguments before -> + (forall pc after, Not (exit = .reaches pc after)) -> head.post arguments before exit -> + exit.Confined (head.footprint arguments before) before -> + And (exit.Confined (composed.footprint arguments before) before) + (composed.post arguments before exit)) + (joins : forall arguments before pc after exit, composed.pre arguments before -> + head.post arguments before (.reaches pc after) -> + Confined (head.footprint arguments before) before.memory after.memory -> + (next pc).post arguments after exit -> + exit.Confined ((next pc).footprint arguments after) after -> + And (exit.Confined (composed.footprint arguments before) before) + (composed.post arguments before exit)) : + composed.Holds program method start := by + intro arguments before permitted + cases first arguments before (enters arguments before permitted) + rename_i exit exitWitness + cases exitWitness + rename_i ran framedPost + cases framedPost + rename_i framed satisfied + cases exit with + | reaches pc after => + cases ran + rename_i cost progress + cases progress + rename_i _positive reached + cases rest pc arguments after (links arguments before pc after permitted satisfied) + rename_i final exitWitness + cases exitWitness + rename_i tail framedPost + cases framedPost + rename_i tailFramed tailPost + exact Exists.intro final (And.intro (reached.exhibited tail) + (joins arguments before pc after final permitted satisfied framed tailPost tailFramed)) + | returns memory values => + exact Exists.intro (.returns memory values) (And.intro ran + (stops arguments before _ permitted (by intro pc after impossible; cases impossible) + satisfied framed)) + | faults fault => + exact Exists.intro (.faults fault) (And.intro ran + (stops arguments before _ permitted (by intro pc after impossible; cases impossible) + satisfied framed)) + | diverges => + exact Exists.intro .diverges (And.intro ran + (stops arguments before _ permitted (by intro pc after impossible; cases impossible) + satisfied framed)) + +#print axioms SegmentContract.Holds.seq + +/-- Two continuing contracts compose when their boundary predicates and footprints agree. -/ +theorem SegmentContract.Holds.then {program : CIL.Program} {method start middle finish : Nat} + {firstPre secondPre : List Value -> SegmentState -> Prop} + {firstPost secondPost : List Value -> SegmentState -> SegmentState -> Prop} + {firstFoot secondFoot footprint : List Value -> SegmentState -> List (Prod Reference Nat)} + (first : (SegmentContract.continuing middle firstPre firstPost firstFoot).Holds + program method start) + (second : (SegmentContract.continuing finish secondPre secondPost secondFoot).Holds + program method middle) + (links : forall arguments before after, firstPre arguments before -> + firstPost arguments before after -> secondPre arguments after) + (covers : forall arguments before after, firstPre arguments before -> + firstPost arguments before after -> + And (forall entry, List.Mem entry (firstFoot arguments before) -> + List.Mem entry (footprint arguments before)) + (forall entry, List.Mem entry (secondFoot arguments after) -> + List.Mem entry (footprint arguments before))) : + (SegmentContract.continuing finish firstPre + (fun arguments before final => Exists fun after => + And (firstPost arguments before after) (secondPost arguments after final)) + footprint).Holds program method start := by + intro arguments before permitted + cases first arguments before permitted + rename_i firstExit exitWitness + cases exitWitness + rename_i firstRun framedPost + cases framedPost + rename_i firstFramed firstSatisfied + cases firstExit with + | reaches pc after => + cases firstSatisfied + rename_i destination firstPost + rcases destination with rfl + cases second arguments after (links arguments before after permitted firstPost) + rename_i secondExit exitWitness + cases exitWitness + rename_i secondRun framedPost + cases framedPost + rename_i secondFramed secondPost + cases secondExit with + | reaches pc final => + cases secondPost + rename_i destination secondPost + rcases destination with rfl + cases firstRun + rename_i cost progress + cases progress + rename_i _positive reached + cases covers arguments before after permitted firstPost + rename_i firstCover secondCover + exact Exists.intro (.reaches pc final) (And.intro (reached.exhibited secondRun) + (And.intro ((firstFramed.widen firstCover).trans (secondFramed.widen secondCover)) + (And.intro rfl (Exists.intro after (And.intro firstPost secondPost))))) + | returns memory values => exact False.elim secondPost + | faults fault => exact False.elim secondPost + | diverges => exact False.elim secondPost + | returns memory values => exact False.elim firstSatisfied + | faults fault => exact False.elim firstSatisfied + | diverges => exact False.elim firstSatisfied + +#print axioms SegmentContract.Holds.then + +/-- A validated branch exposes its selected successor and preserves the incoming memory. -/ +theorem SegmentContract.branch {program : CIL.Program} {method pc target : Nat} + {body : CIL.Method} {op : CIL.Instruction} + (pre taken : List Value -> SegmentState -> Prop) + [_decidable : forall arguments state, Decidable (taken arguments state)] + (popped : List Value -> SegmentState -> List Value) + (found : program[method]? = some body) (instruction : body.code[pc]? = some op) + (stepped : forall arguments state, pre arguments state -> + step body op pc arguments state.frame state.stack state.memory = + .ok (.next (if taken arguments state then target else pc + 1) (popped arguments state) + state.frame state.memory)) : + ({ pre := pre + post := fun arguments before exit => + exit = .reaches (if taken arguments before then target else pc + 1) + { frame := before.frame, stack := popped arguments before, + memory := before.memory } } : SegmentContract).Holds program method pc := by + intro arguments before permitted + have exactRun : SegmentRun program method pc + (if taken arguments before then target else pc + 1) 1 arguments before + { frame := before.frame, stack := popped arguments before, memory := before.memory } := by + intro fuel + simp only [run_succ, found, instruction, stepped arguments before permitted, + Bind.bind, Except.bind, Except.mapError] + exact Exists.intro (.reaches (if taken arguments before then target else pc + 1) + { frame := before.frame, stack := popped arguments before, memory := before.memory }) + (And.intro (Exists.intro 1 (And.intro (by decide) exactRun.reaches)) + (And.intro (Confined.refl [] before.memory) rfl)) + +#print axioms SegmentContract.branch + +/-- A validated branch composes with both successor summaries under explicit footprint coverage. -/ +theorem SegmentContract.branch_seq {program : CIL.Program} {method pc target : Nat} + {body : CIL.Method} {op : CIL.Instruction} {composed : SegmentContract} + (pre taken : List Value -> SegmentState -> Prop) + [_decidable : forall arguments state, Decidable (taken arguments state)] + (popped : List Value -> SegmentState -> List Value) + (found : program[method]? = some body) (instruction : body.code[pc]? = some op) + (stepped : forall arguments state, pre arguments state -> + step body op pc arguments state.frame state.stack state.memory = + .ok (.next (if taken arguments state then target else pc + 1) (popped arguments state) + state.frame state.memory)) + (next : Nat -> SegmentContract) (rest : forall pc, (next pc).Holds program method pc) + (enters : forall arguments before, composed.pre arguments before -> pre arguments before) + (links : forall arguments before, composed.pre arguments before -> + (next (if taken arguments before then target else pc + 1)).pre arguments + { frame := before.frame, stack := popped arguments before, memory := before.memory }) + (joins : forall arguments before exit, composed.pre arguments before -> + (next (if taken arguments before then target else pc + 1)).post arguments + { frame := before.frame, stack := popped arguments before, memory := before.memory } + exit -> + exit.Confined ((next (if taken arguments before then target else pc + 1)).footprint arguments + { frame := before.frame, stack := popped arguments before, memory := before.memory }) + { frame := before.frame, stack := popped arguments before, memory := before.memory } -> + And (exit.Confined (composed.footprint arguments before) before) + (composed.post arguments before exit)) : + composed.Holds program method pc := by + apply (SegmentContract.branch pre taken popped found instruction stepped).seq rest enters + next => + intro arguments before nextPc after permitted matched + cases matched + exact links arguments before permitted + next => + intro arguments before exit permitted stopped matched + cases matched + exact False.elim (stopped _ _ rfl) + next => + intro arguments before nextPc after exit permitted matched _ satisfied framed + cases matched + exact joins arguments before exit permitted satisfied framed + +#print axioms SegmentContract.branch_seq + +end CIL.Safety diff --git a/CIL/Safety/SegmentLocals.lean b/CIL/Safety/SegmentLocals.lean index 2eef31130..c3bb81069 100644 --- a/CIL/Safety/SegmentLocals.lean +++ b/CIL/Safety/SegmentLocals.lean @@ -12,39 +12,46 @@ Indirect writes and aliased homes cannot be justified by placement alone. namespace CIL.Safety -/-- Direct local destinations named by a code segment, including vector local stores. -/ -def segmentStores (code : List CIL.Op) : List Nat := code.filterMap fun operation => +/-- Direct destinations named by a code segment of `body`: the slots its local stores name, + and the home slots of the homed arguments it stores to. -/ +def segmentStores (body : CIL.Method) (code : List CIL.Instruction) : List Nat := + code.filterMap fun operation => match operation with - | .setLocal index _ | .setVectorLocal index | .initializeSpanLocal index _ => some index + | .stloc index => some index + | .starg index => + match body.argumentAccess index with + | .home slot => some slot + | .value _ => none | _ => none /-- The remembered values of locals outside a segment's direct-store set. -/ -def untouchedLocals (code : List CIL.Op) (known : Nat → Option CIL.Value) : - Nat → Option CIL.Value := fun index => - if index ∈ segmentStores code then none else known index +def untouchedLocals (body : CIL.Method) (code : List CIL.Instruction) + (known : Nat → Option CIL.Value) : Nat → Option CIL.Value := fun index => + if index ∈ segmentStores body code then none else known index /-- A memory effect consists of actual successful writes to local homes named by the code. Slot identities stay fixed. Merely omitting an indirect write from the code is not evidence for this relation: every memory transition requires its checked write equation. -/ -inductive SegmentLocalEffects (code : List CIL.Op) (slots : List LocalSlot) : - Memory → Memory → Prop where - | refl (memory) : SegmentLocalEffects code slots memory memory +inductive SegmentLocalEffects (body : CIL.Method) (code : List CIL.Instruction) + (slots : List LocalSlot) : Memory → Memory → Prop where + | refl (memory) : SegmentLocalEffects body code slots memory memory | store {before after} (index : Nat) (kind : CIL.LocalKind) (home : Reference) (bytes : List (BitVec 8)) (alignment : Nat) - (destination : index ∈ segmentStores code) + (destination : index ∈ segmentStores body code) (slot : slots[index]? = some (.bytes kind home)) (written : write before home bytes alignment = .ok after) : - SegmentLocalEffects code slots before after + SegmentLocalEffects body code slots before after | trans {before middle after} - (first : SegmentLocalEffects code slots before middle) - (second : SegmentLocalEffects code slots middle after) : - SegmentLocalEffects code slots before after + (first : SegmentLocalEffects body code slots before middle) + (second : SegmentLocalEffects body code slots middle after) : + SegmentLocalEffects body code slots before after /-- An effect remains valid when the containing placed region permits more destinations. -/ -theorem SegmentLocalEffects.mono {small large : List CIL.Op} {slots : List LocalSlot} - {before after : Memory} (effects : SegmentLocalEffects small slots before after) - (included : ∀ index ∈ segmentStores small, index ∈ segmentStores large) : - SegmentLocalEffects large slots before after := by +theorem SegmentLocalEffects.mono {body : CIL.Method} {small large : List CIL.Instruction} + {slots : List LocalSlot} {before after : Memory} + (effects : SegmentLocalEffects body small slots before after) + (included : ∀ index ∈ segmentStores body small, index ∈ segmentStores body large) : + SegmentLocalEffects body large slots before after := by induction effects with | refl memory => exact .refl memory | store index kind home bytes alignment destination slot written => @@ -54,11 +61,11 @@ theorem SegmentLocalEffects.mono {small large : List CIL.Op} {slots : List Local #print axioms SegmentLocalEffects.mono /-- Adjacent placed regions combine their checked memory effects. -/ -theorem SegmentLocalEffects.append {left right : List CIL.Op} {slots : List LocalSlot} - {before middle after : Memory} - (first : SegmentLocalEffects left slots before middle) - (second : SegmentLocalEffects right slots middle after) : - SegmentLocalEffects (left ++ right) slots before after := +theorem SegmentLocalEffects.append {body : CIL.Method} {left right : List CIL.Instruction} + {slots : List LocalSlot} {before middle after : Memory} + (first : SegmentLocalEffects body left slots before middle) + (second : SegmentLocalEffects body right slots middle after) : + SegmentLocalEffects body (left ++ right) slots before after := (first.mono (by intro index member simp only [segmentStores, List.filterMap_append, List.mem_append] at * @@ -70,19 +77,20 @@ theorem SegmentLocalEffects.append {left right : List CIL.Op} {slots : List Loca #print axioms SegmentLocalEffects.append /-- A checked local-write effect preserves every remembered value outside the placed stores. Distinct writable homes rule out aliasing between the stored and retained locals. -/ -theorem SegmentLocalEffects.frame {code : List CIL.Op} {slots : List LocalSlot} +theorem SegmentLocalEffects.frame {body : CIL.Method} {code : List CIL.Instruction} + {slots : List LocalSlot} {before after entered : Memory} {boundary : Nat} {kinds : List CIL.LocalKind} {known : Nat → Option CIL.Value} - (effects : SegmentLocalEffects code slots before after) + (effects : SegmentLocalEffects body code slots before after) (homes : WritableHomes entered boundary kinds slots) - (snapshots : LocalSnapshots before slots (untouchedLocals code known)) : - LocalSnapshots after slots (untouchedLocals code known) := by + (snapshots : LocalSnapshots before slots (untouchedLocals body code known)) : + LocalSnapshots after slots (untouchedLocals body code known) := by induction effects with | refl => exact snapshots | store index kind home bytes alignment destination slot written => apply snapshots.after_write_apart written intro other value otherKind otherHome specified otherSlot - have outside : other ∉ segmentStores code := by + have outside : other ∉ segmentStores body code := by intro stored simp [untouchedLocals, stored] at specified have different : other ≠ index := fun same => outside (same ▸ destination) @@ -92,12 +100,12 @@ theorem SegmentLocalEffects.frame {code : List CIL.Op} {slots : List LocalSlot} #print axioms SegmentLocalEffects.frame /-- Forgetting destinations from a snapshot map preserves all other snapshot evidence. -/ -theorem LocalSnapshots.untouched {code : List CIL.Op} {memory : Memory} - {slots : List LocalSlot} {known : Nat → Option CIL.Value} +theorem LocalSnapshots.untouched {body : CIL.Method} {code : List CIL.Instruction} + {memory : Memory} {slots : List LocalSlot} {known : Nat → Option CIL.Value} (snapshots : LocalSnapshots memory slots known) : - LocalSnapshots memory slots (untouchedLocals code known) := by + LocalSnapshots memory slots (untouchedLocals body code known) := by intro index value specified - by_cases stored : index ∈ segmentStores code + by_cases stored : index ∈ segmentStores body code · simp [untouchedLocals, stored] at specified · exact snapshots index value (by simpa [untouchedLocals, stored] using specified) @@ -107,7 +115,7 @@ theorem LocalSnapshots.untouched {code : List CIL.Op} {memory : Memory} at a segment boundary. No final snapshot or per-local disjointness proof is required. -/ theorem SegmentRun.frameLocals {program : CIL.Program} {method start finish cost : Nat} {arguments : List Value} {before after : SegmentState} {body : CIL.Method} - {code : List CIL.Op} {entered : Memory} {boundary : Nat} {kinds : List CIL.LocalKind} + {code : List CIL.Instruction} {entered : Memory} {boundary : Nat} {kinds : List CIL.LocalKind} {known : Nat → Option CIL.Value} (execution : SegmentRun program method start finish cost arguments before after) (_found : program[method]? = some body) @@ -115,9 +123,9 @@ theorem SegmentRun.frameLocals {program : CIL.Program} {method start finish cost (homes : WritableHomes entered boundary kinds before.frame.locals) (snapshots : LocalSnapshots before.memory before.frame.locals known) (slots : after.frame.locals = before.frame.locals) - (effects : SegmentLocalEffects code before.frame.locals before.memory after.memory) : + (effects : SegmentLocalEffects body code before.frame.locals before.memory after.memory) : SegmentRun program method start finish cost arguments before after ∧ - LocalSnapshots after.memory after.frame.locals (untouchedLocals code known) := by + LocalSnapshots after.memory after.frame.locals (untouchedLocals body code known) := by refine ⟨execution, ?_⟩ rw [slots] exact effects.frame homes snapshots.untouched @@ -127,7 +135,7 @@ theorem SegmentRun.frameLocals {program : CIL.Program} {method start finish cost /-- A segment with checked local-write effects inherits untouched input snapshots. The effect summary replaces repeated disjoint-read proofs at each of its numeric local stores. -/ theorem Segment.frameLocals {program : CIL.Program} {method start finish : Nat} - {body : CIL.Method} {code : List CIL.Op} {slots : List LocalSlot} + {body : CIL.Method} {code : List CIL.Instruction} {slots : List LocalSlot} {initial entered : Memory} {boundary : Nat} {kinds : List CIL.LocalKind} {known : Nat → Option CIL.Value} {pre post : List Value → SegmentState → Prop} (_found : program[method]? = some body) @@ -136,10 +144,10 @@ theorem Segment.frameLocals {program : CIL.Program} {method start finish : Nat} (snapshots : LocalSnapshots initial slots known) (contract : Segment program method start finish pre fun arguments state => post arguments state ∧ state.frame.locals = slots ∧ - SegmentLocalEffects code slots initial state.memory) : + SegmentLocalEffects body code slots initial state.memory) : Segment program method start finish pre fun arguments state => post arguments state ∧ LocalSnapshots state.memory state.frame.locals - (untouchedLocals code known) := by + (untouchedLocals body code known) := by apply contract.consequence (fun _ _ valid => valid) intro arguments state result refine ⟨result.1, ?_⟩ diff --git a/CIL/Safety/SnapshotMemory.lean b/CIL/Safety/SnapshotMemory.lean index 737a9c857..b2aa1bb6e 100644 --- a/CIL/Safety/SnapshotMemory.lean +++ b/CIL/Safety/SnapshotMemory.lean @@ -32,18 +32,141 @@ theorem readSnapshot_length {m : Memory} {r : Reference} {width alignment : Nat} · cases h simp +/-- The cells following a short snapshot prefix, indexed from its first byte. -/ +private def suffixCell {cell : Type} : List cell -> Nat -> Option cell + | [] => fun _ => none + | value :: bytes => + let tail := suffixCell bytes + fun index => match index with + | 0 => some value + | next + 1 => tail next + +/-- Reading a suffix at any index agrees with list lookup, including past the end. -/ +private theorem suffixCell_eq {cell : Type} (bytes : List cell) (index : Nat) : + suffixCell bytes index = bytes[index]? := by + induction bytes generalizing index with + | nil => cases index <;> rfl + | cons head tail ih => cases index with + | zero => rfl + | succ next => exact ih next +#print axioms suffixCell_eq + +/-- A decision tree indexes byte ranges. Each right branch shares the remaining list. -/ +private def rangeCell {cell : Type} : Nat -> Nat -> List cell -> Nat -> Option cell + | 0, _, bytes => fun index => bytes[index]? + | height + 1, stride, bytes => + let left := rangeCell height (stride / 2) bytes + let right := rangeCell height (stride / 2) (bytes.drop stride) + fun index => if index < stride then left index else right (index - stride) + +/-- Splitting a range preserves the cell at every index for every split size. -/ +private theorem rangeCell_eq {cell : Type} (height stride : Nat) (bytes : List cell) + (index : Nat) : rangeCell height stride bytes index = bytes[index]? := by + induction height generalizing stride bytes index with + | zero => rfl + | succ height ih => + simp only [rangeCell] + split + next => apply ih + next outside => + rw [ih, List.getElem?_drop] + congr 1 + omega +#print axioms rangeCell_eq + +/-- Four low offset bits select one of sixteen cells at a leaf. -/ +private def leafBits := 4 + +/-- Consecutive ranges double in width. The tail preserves lookup beyond the last range. -/ +private def indexedRanges {cell : Type} : Nat -> Nat -> Nat -> List cell -> Nat -> Option cell + | 0, _, _, bytes => fun index => bytes[index]? + | remaining + 1, height, width, bytes => + if bytes.isEmpty then fun _ => none else + let here := rangeCell height (width / 2) bytes + let later := indexedRanges remaining (height + 1) (width * 2) (bytes.drop width) + fun index => if index < width then here index else later (index - width) + +/-- Reading successive ranges agrees with list lookup for any choice of range widths. -/ +private theorem indexedRanges_eq {cell : Type} (remaining height width : Nat) (bytes : List cell) + (index : Nat) : indexedRanges remaining height width bytes index = bytes[index]? := by + induction remaining generalizing height width bytes index with + | zero => rfl + | succ remaining ih => + cases bytes with + | nil => rfl + | cons first tail => + simp only [indexedRanges, List.isEmpty_cons, Bool.false_eq_true, ite_false] + split + next => apply rangeCell_eq + next outside => + rw [ih, List.getElem?_drop] + congr 1 + omega +#print axioms indexedRanges_eq + +/-- The ranges cover every native offset, with at most sixteen byte probes in its leaf. -/ +def snapshotCellAt {cell : Type} (indexed : Array cell) : Nat -> Option cell := + let width := 2 ^ leafBits + let first := suffixCell indexed.toList + let later := indexedRanges (nativeLimit.log2 - leafBits) 1 (2 * width) + (indexed.toList.drop width) + fun index => if index < width then first index else later (index - width) + +/-- The snapshot index agrees with list lookup, including for offsets past the end. -/ +theorem snapshotCellAt_eq {cell : Type} (bytes : List cell) (index : Nat) : + snapshotCellAt bytes.toArray index = bytes[index]? := by + unfold snapshotCellAt + split + next => apply suffixCell_eq + next outside => + rw [indexedRanges_eq, List.getElem?_drop] + congr 1 + omega +#print axioms snapshotCellAt_eq + +/-- Compiled reads index an array captured at write time. -/ +private def arrayCellAt {cell : Type} (indexed : Array cell) (index : Nat) : Option cell := + indexed[index]? + +/-- The compiler can use direct array reads for every snapshot and offset. -/ +@[csimp] private theorem snapshotCellAt_eq_array : @snapshotCellAt = @arrayCellAt := by + funext cell indexed index + cases indexed with + | mk bytes => simp only [snapshotCellAt_eq, arrayCellAt, List.getElem?_toArray] +#print axioms snapshotCellAt_eq_array + /-- Copy bits and initialization flags together. Uninitialized input never becomes initialized just because it was assigned to another value or local. -/ def writeSnapshot (m : Memory) (r : Reference) (bytes : List (BitVec 8 × Bool)) (alignment : Nat) : Checked Memory := do let written ← write m r (bytes.map Prod.fst) alignment + let indexed := bytes.toArray return { written with cells := (fun id offset => if id = r.allocation ∧ r.offset ≤ offset then - match bytes[offset - r.offset]? with + match snapshotCellAt indexed (offset - r.offset) with | some cell => ⟨cell.1, cell.2⟩ | none => m.cells id offset else m.cells id offset) } +/-- A successful store overlays exactly the supplied cells and preserves every other cell. -/ +theorem writeSnapshot_cells_eq {m result : Memory} {r : Reference} + {bytes : List (Prod (BitVec 8) Bool)} {alignment : Nat} + (stored : writeSnapshot m r bytes alignment = .ok result) : + result.cells = fun id offset => + if (And (id = r.allocation) (r.offset <= offset)) then + match bytes[offset - r.offset]? with + | some cell => .mk cell.1 cell.2 + | none => m.cells id offset + else m.cells id offset := by + unfold writeSnapshot at stored + cases written : write m r (bytes.map Prod.fst) alignment <;> + simp only [written, Bind.bind, Except.bind, Pure.pure, Except.pure] at stored + next => cases stored + next => + cases stored + simp only [snapshotCellAt_eq] +#print axioms writeSnapshot_cells_eq + theorem writeSnapshot_preserves_wellFormed (m result : Memory) (r : Reference) (bytes : List (BitVec 8 × Bool)) (alignment : Nat) (hm : m.WellFormed) (h : writeSnapshot m r bytes alignment = .ok result) : result.WellFormed := by diff --git a/CIL/Safety/SpanConstructionLemmas.lean b/CIL/Safety/SpanConstructionLemmas.lean index da02f77bf..f5db3553c 100644 --- a/CIL/Safety/SpanConstructionLemmas.lean +++ b/CIL/Safety/SpanConstructionLemmas.lean @@ -3,7 +3,8 @@ import CIL.Safety.InstructionMemoryLemmas /-! # Span constructor results -Local span initialization reuses checked view construction without changing backing memory. +A span constructor builds a checked view without changing memory, so running it in place on a +home changes only the slot the home names. -/ namespace CIL.Safety @@ -16,7 +17,8 @@ theorem spanConstructor_result (constructor : CIL.SpanConstructor) after = memory ∧ value.Valid memory := by cases constructor <;> simp only [CIL.SpanConstructor.operation] at constructed all_goals unfold memoryInstruction at constructed - all_goals simp only [singleSpanInstruction, otherMemoryInstruction] at constructed + all_goals simp only [singleSpanInstruction, otherMemoryInstruction, staticSpanInstruction] + at constructed all_goals split at constructed all_goals repeat' first | contradiction @@ -24,6 +26,8 @@ theorem spanConstructor_result (constructor : CIL.SpanConstructor) | solve | cases constructed exact ⟨rfl, SpanView.slice_valid _ _ _ _ _ (checkedAt_ok (by assumption))⟩ + | solve | cases constructed; exact ⟨rfl, trivial⟩ + | solve | cases constructed; exact ⟨rfl, formValue_reference_valid _ _ _ (by assumption)⟩ | cases constructed | split at constructed | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at constructed diff --git a/CIL/Safety/SpanHomeRenaming.lean b/CIL/Safety/SpanHomeRenaming.lean new file mode 100644 index 000000000..5b2e93a12 --- /dev/null +++ b/CIL/Safety/SpanHomeRenaming.lean @@ -0,0 +1,190 @@ +import CIL.Safety.ActionRenaming +import CIL.Safety.MemoryInstructionRenaming + +/-! +# Span homes under allocation renaming + +A home names a slot of the frame that holds it, so renaming relocates it with the frame's +locals. Reading through a home reads the corresponding slot of the target frame, which relates +view projection, the operands of span operations and call argument checks. +-/ + +namespace CIL.Safety + +variable {indices : LocalIndexMap} {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} + {source target : Memory} {sourceFrame targetFrame : Frame} + +/-- A span local's view lies below every limit its slot lies below. -/ +theorem loadLocal_span_below {memory : Memory} {width : Nat} {writable : Bool} + {held : Option SpanView} {limit : Nat} {result : Value} + (bound : (LocalSlot.span width writable held).Below limit) + (loaded : loadLocal memory (.span width writable held) = .ok result) : result.Below limit := by + cases held with + | none => cases loaded + | some view => + simp only [loadLocal, Bind.bind, Except.bind, Pure.pure, Except.pure] at loaded + repeat' first + | (cases loaded <;> exact bound view rfl) + | split at loaded + +#print axioms loadLocal_span_below + +/-- A home reads the corresponding slot and any other value passes as renamed; the view is + bounded in the source. -/ +theorem Frame.Renamed.spanView (memories : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (value : Value) + (bound : value.Below mapping.sourceLimit) : + ResultRenamed mapping + (fun left right => right = mapping.value left ∧ left.Below mapping.sourceLimit) + (spanView sourceFrame source value) + (spanView targetFrame target (indices.value mapping value)) := by + cases value with + | spanHome slot => + have slots := frames.locals.getElem?_all slot + simp only [CIL.Safety.spanView, LocalIndexMap.value] + generalize sourceFound : sourceFrame.locals[slot]? = sourceSlot at slots ⊢ + generalize targetFound : targetFrame.locals[indices.index slot]? = targetSlot at slots ⊢ + cases slots with + | none => exact .error .invalidState + | @some held otherHeld slots => + rcases slots with ⟨bounded, rfl⟩ + cases held with + | span width writable view => + refine (memories.loadLocal (.span width writable view) bounded).imp ?_ + intro left right loaded same + exact ⟨same, loadLocal_span_below bounded loaded⟩ + | _ => exact .error .invalidState + | _ => exact .ok ⟨rfl, bound⟩ +#print axioms Frame.Renamed.spanView + +/-- Projecting the operands of a span operation reads corresponding slots, keeps the stack + below them renamed and leaves every projected operand bounded. -/ +theorem Frame.Renamed.viewOperands (memories : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) : + ∀ (count : Nat) (stack : List Value), (∀ value ∈ stack, value.Below mapping.sourceLimit) → + ResultRenamed mapping + (fun left right => right = left.map (indices.value mapping) ∧ + ∀ value ∈ left, value.Below mapping.sourceLimit) + (viewOperands sourceFrame source count stack) + (viewOperands targetFrame target count (stack.map (indices.value mapping))) + | 0, _, bounds => .ok ⟨rfl, bounds⟩ + | _ + 1, [], _ => .ok ⟨rfl, fun _ member => nomatch member⟩ + | count + 1, value :: rest, bounds => by + simp only [CIL.Safety.viewOperands, List.map_cons] + apply (frames.spanView memories value (bounds value (List.mem_cons_self ..))).bindWithSuccess + intro first second viewed related + obtain ⟨same, firstBound⟩ := related + subst same + apply (Frame.Renamed.viewOperands memories frames count rest + (fun other member => bounds other (List.mem_cons_of_mem _ member))).bind + intro others otherTarget related + obtain ⟨same, otherBounds⟩ := related + subst same + refine .ok ⟨indices.push_of_not_home mapping others (spanView_not_spanHome viewed), ?_⟩ + intro other member + rcases List.mem_cons.mp member with same | inner + · subst same + exact firstBound + · exact otherBounds other inner +#print axioms Frame.Renamed.viewOperands + +/-- A call argument reads a home's corresponding slot and checks the renamed value. -/ +theorem Frame.Renamed.callArgument (memories : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (value : Value) + (bound : value.Below mapping.sourceLimit) : + ResultRenamed mapping (fun left right => right = mapping.value left) + (callArgument sourceFrame source value) + (callArgument targetFrame target (indices.value mapping value)) := by + unfold CIL.Safety.callArgument + apply (frames.spanView memories value bound).bind + intro viewed otherViewed related + obtain ⟨same, viewedBound⟩ := related + subst same + exact memories.checkedValue viewed viewedBound +#print axioms Frame.Renamed.callArgument + +/-- Checking call arguments one by one keeps them renamed and reports the first fault. -/ +theorem Frame.Renamed.callArguments (memories : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (values : List Value) + (bounds : ∀ value ∈ values, value.Below mapping.sourceLimit) : + ResultRenamed mapping (fun left right => right = left.map mapping.value) + (values.mapM (CIL.Safety.callArgument sourceFrame source)) + ((values.map (indices.value mapping)).mapM + (CIL.Safety.callArgument targetFrame target)) := by + induction values with + | nil => exact .ok rfl + | cons value rest induction => + simp only [List.map_cons, List.mapM_cons] + apply (frames.callArgument memories value (bounds value (List.mem_cons_self ..))).bind + intro first otherFirst firstRelated + apply (induction (fun value member => bounds value (List.mem_cons_of_mem _ member))).bind + intro remaining otherRemaining remainingRelated + exact .ok (by simp only [List.map_cons, firstRelated, remainingRelated]) +#print axioms Frame.Renamed.callArguments + +/-- Checking a frame's stack refuses a home on both sides and otherwise checks each renamed + value, so a returned stack keeps its values renamed. -/ +theorem Memory.Renamed.checkedStack (related : Memory.Renamed mapping scopes source target) + (values : List Value) (bounds : ∀ value ∈ values, value.Below mapping.sourceLimit) : + ResultRenamed mapping (fun left right => right = left.map mapping.value) + (values.mapM (CIL.Safety.checkedValue source)) + ((values.map (indices.value mapping)).mapM (CIL.Safety.checkedValue target)) := by + induction values with + | nil => exact .ok rfl + | cons value rest induction => + simp only [List.map_cons, List.mapM_cons] + have head : ResultRenamed mapping (fun left right => right = mapping.value left) + (CIL.Safety.checkedValue source value) + (CIL.Safety.checkedValue target (indices.value mapping value)) := by + cases value with + | spanHome slot => exact .error .invalidState + | _ => exact related.checkedValue _ (bounds _ (List.mem_cons_self ..)) + apply head.bind + intro first otherFirst firstRelated + apply (induction (fun value member => bounds value (List.mem_cons_of_mem _ member))).bind + intro remaining otherRemaining remainingRelated + exact .ok (by simp only [List.map_cons, firstRelated, remainingRelated]) +#print axioms Memory.Renamed.checkedStack + +/-- Initializing a span through a home stores the empty view in the corresponding slot of the + target frame and keeps the stack below renamed. -/ +theorem Frame.Renamed.initializeSpanHome (memories : Memory.Renamed mapping scopes source target) + (frames : Frame.Renamed indices mapping scopes sourceFrame targetFrame) (pc : Nat) + (stack : List Value) : + ResultRenamed mapping (FrameAction.Renamed indices mapping scopes) + (initializeSpanHome pc sourceFrame stack source) + (initializeSpanHome pc targetFrame (stack.map (indices.value mapping)) target) := by + cases stack with + | nil => exact .error _ + | cons value rest => + cases value with + | spanHome position => + simp only [CIL.Safety.initializeSpanHome, List.map_cons, LocalIndexMap.value] + have slots := frames.locals.getElem?_all position + generalize sourceFound : sourceFrame.locals[position]? = sourceSlot at slots ⊢ + generalize targetFound : targetFrame.locals[indices.index position]? = targetSlot at slots ⊢ + cases slots with + | none => exact .error .invalidState + | @some slot otherSlot slots => + rcases slots with ⟨bounded, rfl⟩ + cases slot with + | span width writable view => + apply (memories.storeLocal (indices := indices) (.span width writable view) + (.bufferSpan ⟨.null, 0, width, writable⟩) bounded trivial).bindWithSuccess + intro result otherResult stored results + obtain ⟨slotAfter, sourceAfter⟩ := result + obtain ⟨otherSlotAfter, targetAfter⟩ := otherResult + rcases results with ⟨slotsEqual, related⟩ + change otherSlotAfter = mapping.localSlot slotAfter at slotsEqual + subst otherSlotAfter + have positionBound : position < indices.sourceLength := by + rw [← frames.locals.lengths.1] + exact (List.getElem?_eq_some_iff.mp sourceFound).1 + exact .ok (.next related (frames.setLocal position positionBound slotAfter + (storeLocal_below (value := .bufferSpan ⟨.null, 0, width, writable⟩) bounded trivial + stored))) + | _ => exact .error .invalidState + | _ => exact .error _ +#print axioms Frame.Renamed.initializeSpanHome + +end CIL.Safety diff --git a/CIL/Safety/StaticBindingBounds.lean b/CIL/Safety/StaticBindingBounds.lean index f20245ad8..277e6d679 100644 --- a/CIL/Safety/StaticBindingBounds.lean +++ b/CIL/Safety/StaticBindingBounds.lean @@ -139,9 +139,9 @@ theorem StackAllocationEffect.staticBindingsBounded {source target : Memory} #print axioms StackAllocationEffect.staticBindingsBounded /-- Every checked dispatcher effect preserves static allocation bounds. -/ -theorem StepEffect.staticBindingsBounded {body : CIL.Method} {operation : CIL.Op} - {source target : Memory} {frame final : Frame} - (effect : StepEffect body operation frame source final target) +theorem StepEffect.staticBindingsBounded {body : CIL.Method} {site : Nat} + {operation : CIL.Instruction} {source target : Memory} {frame final : Frame} + (effect : StepEffect body site operation frame source final target) (bounded : source.StaticBindingsBounded) : target.StaticBindingsBounded := by cases effect with | arrayAllocation effect => exact bounded.preserve effect.registry effect.allocations.next diff --git a/CIL/Safety/StaticInstructionRenaming.lean b/CIL/Safety/StaticInstructionRenaming.lean index 30ce3f1ba..c85395fd8 100644 --- a/CIL/Safety/StaticInstructionRenaming.lean +++ b/CIL/Safety/StaticInstructionRenaming.lean @@ -6,14 +6,17 @@ import CIL.Safety.StaticMemoryLemmas # Static instruction simulation under allocation renaming Extraction-bound static identities stay exact. A first lookup extends the allocation map, -while cached fields and span construction preserve their existing allocation correspondence. +while cached fields preserve their existing allocation correspondence. -/ namespace CIL.Safety +-- Stack lists are renamed by the local map of the frame that holds them. +variable {indices : LocalIndexMap} + /-- Static resolution reports model or memory faults, never a named CLR exception. -/ theorem staticReference_not_thrown (descriptor : CIL.StaticDescriptor) (memory : Memory) - (exception : String) : + (exception : ExceptionType) : staticReference descriptor memory ≠ .error (.thrown exception) := by intro raised unfold staticReference at raised @@ -30,115 +33,105 @@ theorem staticReference_not_thrown (descriptor : CIL.StaticDescriptor) (memory : theorem Memory.Renamed.staticInstructionResult {mapping : AllocationRenaming} {scopes : Nat → Nat → Prop} {source target : Memory} (related : Memory.Renamed mapping scopes source target) - (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) - (staticBounds : ∀ binding ∈ source.staticBindings, - binding.2.allocation < mapping.sourceLimit) + (sourceFormed : source.WellFormed) (body : CIL.Method) (pc : Nat) (operation : CIL.MemoryOp) (stack : List Value) (bounds : ∀ value ∈ stack, value.Below mapping.sourceLimit) (observable : SuccessOrThrow (staticInstruction body pc operation stack source)) : ∃ later, mapping.Extends later ∧ ResultRenamed later (fun left right => Memory.Renamed later scopes left.1 right.1 ∧ - right.2 = left.2.map later.value) + right.2 = left.2.map (indices.value later)) (staticInstruction body pc operation stack source) - (staticInstruction body pc operation (stack.map mapping.value) target) := by + (staticInstruction body pc operation (stack.map (indices.value mapping)) target) := by unfold CIL.Safety.staticInstruction at observable ⊢ - split at observable - all_goals try simp only [List.map_cons, AllocationRenaming.value, - AllocationRenaming.managedReference] - case h_1 => - rename_i bytes - cases found : body.staticSites.find? (fun site => site.1 == pc) with - | none => simp only [found, SuccessOrThrow] at observable - | some site => - obtain ⟨key, descriptor⟩ := site - simp only [found] at observable ⊢ - by_cases matched : (descriptor.bytes == bytes) = true - · simp only [matched, ite_true, Bind.bind, Except.bind] at observable ⊢ - cases resolved : CIL.Safety.staticReference descriptor source with - | error fault => - cases fault <;> simp only [resolved, SuccessOrThrow] at observable - rename_i exception - exact False.elim (staticReference_not_thrown descriptor source exception resolved) - | ok result => - obtain ⟨sourceAfter, reference⟩ := result - obtain ⟨later, targetAfter, extension, targetLoaded, memories⟩ := - related.staticReference sourceFormed targetFormed staticBounds - descriptor reference resolved - refine ⟨later, extension, ?_⟩ - simp only [resolved, targetLoaded, Bind.bind, Except.bind] - exact .ok ⟨memories, by - simp only [List.map_cons, AllocationRenaming.value, - AllocationRenaming.managedReference, extension.values stack bounds]⟩ - · simp only [matched, Bool.false_eq_true, ite_false, Bind.bind, Except.bind, - SuccessOrThrow] at observable - case h_2 => - rename_i bytes - refine ⟨mapping, .refl mapping, ?_⟩ - cases found : body.staticSites.find? (fun site => site.1 == pc) with - | none => exact .error _ - | some site => - obtain ⟨key, descriptor⟩ := site - simp only [found] at observable ⊢ - cases flagged : descriptor.opaqueValue - case false => - simp only [Bool.false_eq_true, ite_false] - repeat first - | exact .ok ⟨related, rfl⟩ - | exact .error _ - | (split <;> try simp only [Bind.bind, Except.bind]) - simp only [flagged, ite_true] at observable ⊢ - split - case isFalse => exact .error _ - -- An opaque value is only ever read through the caller's cached binding. - cases cached : source.staticBindings.find? (fun binding => binding.1 == descriptor.identity) - case none => - simp only [CIL.Safety.staticReference, related.staticLookup descriptor.identity, cached, - Option.map_none, flagged, ite_true, Bind.bind, Except.bind] - exact .error _ - rename_i binding - obtain ⟨name, address⟩ := binding - have bound := staticBounds (name, address) (List.mem_of_find?_eq_some cached) - apply (related.cachedStatic descriptor name address bound cached).bindWithSuccess - intro sourceResult targetResult sourceLoaded paired - obtain ⟨sourceAfter, sourceReference⟩ := sourceResult - obtain ⟨targetAfter, targetReference⟩ := targetResult - obtain ⟨memories, rfl⟩ := paired - obtain ⟨rfl, rfl, _⟩ := cached_static_reference_checked _ _ _ _ _ _ cached sourceLoaded - dsimp only - rw [memories.read sourceReference bound] - apply ((ResultEquivalent.eq_refl _).checkedAt (.address sourceReference) _).bind - intro loaded other same - subst same - split <;> first | exact .ok ⟨memories, rfl⟩ | exact .error _ - case h_6 => - refine ⟨mapping, .refl mapping, ?_⟩ - cases operation <;> first - | exact related.memoryInstructionResult _ sourceFormed _ bounds observable - | (unfold CIL.Safety.memoryInstruction at observable - repeat' first - | contradiction - | cases observable - | split at observable - | simp only [Bind.bind, Except.bind, SuccessOrThrow] at observable) - all_goals refine ⟨mapping, .refl mapping, ?_⟩ - all_goals try exact related.memoryInstructionResult _ sourceFormed _ bounds observable - all_goals simp only [List.mem_cons, forall_eq_or_imp, Value.Below, - ManagedReference.Below] at bounds - all_goals repeat' rcases bounds with ⟨_, bounds⟩ - all_goals repeat first - | exact .ok ⟨related, rfl⟩ - | exact .error _ - | (apply ((related.liveAllocation _ (by assumption)).checkedAt _ _).bindWithSuccess - intro first second sourceStep descriptors - rw [← descriptors.layout, ← descriptors.kind.immutable] - try (unfold staticSpanAligned; rw [← descriptors.layout])) - | (apply (related.formValue _ (by assumption)).bindWithSuccess - intro first second sourceStep same - subst second) - | dsimp +instances only [AllocationRenaming.reference] - | (split <;> try simp only [*, Bind.bind, Except.bind]) + exact ⟨mapping, .refl mapping, + related.memoryInstructionResult _ sourceFormed _ bounds observable⟩ #print axioms Memory.Renamed.staticInstructionResult +/-- `ldsfld` allocates nothing, so it keeps the allocation map and renames its value with it. -/ +theorem Memory.Renamed.staticLoadResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) + (staticBounds : ∀ binding ∈ source.staticBindings, + binding.2.allocation < mapping.sourceLimit) + (body : CIL.Method) (pc : Nat) : + ResultRenamed mapping + (fun left right => Memory.Renamed mapping scopes left.1 right.1 ∧ + right.2 = mapping.value left.2) + (staticLoad body pc source) (staticLoad body pc target) := by + unfold CIL.Safety.staticLoad + cases found : body.staticAt pc with + | none => exact .error _ + | some descriptor => + simp only + cases flagged : descriptor.opaqueValue + case false => + simp only [Bool.false_eq_true, ite_false] + repeat first + | exact .ok ⟨related, rfl⟩ + | exact .error _ + | (split <;> try simp only [Bind.bind, Except.bind]) + simp only [ite_true] + cases cached : source.staticBindings.find? + (fun binding => binding.1 == descriptor.identity) + case none => + simp only [CIL.Safety.staticReference, related.staticLookup descriptor.identity, cached, + Option.map_none, flagged, ite_true, Bind.bind, Except.bind] + exact .error _ + rename_i binding + obtain ⟨name, address⟩ := binding + have bound := staticBounds (name, address) (List.mem_of_find?_eq_some cached) + apply (related.cachedStatic descriptor name address bound cached).bindWithSuccess + intro sourceResult targetResult sourceLoaded paired + obtain ⟨sourceAfter, sourceReference⟩ := sourceResult + obtain ⟨targetAfter, targetReference⟩ := targetResult + obtain ⟨memories, rfl⟩ := paired + obtain ⟨rfl, rfl, _⟩ := cached_static_reference_checked _ _ _ _ _ _ cached sourceLoaded + dsimp only + rw [memories.read sourceReference bound] + apply ((ResultEquivalent.eq_refl _).checkedAt (.address sourceReference) _).bind + intro loaded other same + subst same + split <;> first | exact .ok ⟨memories, rfl⟩ | exact .error _ + +#print axioms Memory.Renamed.staticLoadResult + +/-- `ldsflda` and a field `ldtoken` extend the allocation map only when they allocate the + field's storage, and rename the reference they push with it. -/ +theorem Memory.Renamed.staticAddressResult {mapping : AllocationRenaming} + {scopes : Nat → Nat → Prop} {source target : Memory} + (related : Memory.Renamed mapping scopes source target) + (sourceFormed : source.WellFormed) (targetFormed : target.WellFormed) + (staticBounds : ∀ binding ∈ source.staticBindings, + binding.2.allocation < mapping.sourceLimit) + (body : CIL.Method) (pc : Nat) (observable : SuccessOrThrow (staticAddress body pc source)) : + ∃ later, mapping.Extends later ∧ + ResultRenamed later + (fun left right => Memory.Renamed later scopes left.1 right.1 ∧ + right.2 = later.value left.2) + (staticAddress body pc source) (staticAddress body pc target) := by + unfold CIL.Safety.staticAddress at observable ⊢ + cases found : body.staticAt pc with + | none => exact ⟨mapping, .refl mapping, .error _⟩ + | some descriptor => + simp only [found, Bind.bind, Except.bind] at observable ⊢ + cases resolved : CIL.Safety.staticReference descriptor source with + | error fault => + cases fault <;> simp only [resolved, SuccessOrThrow] at observable + rename_i exception + exact False.elim (staticReference_not_thrown descriptor source exception resolved) + | ok result => + obtain ⟨sourceAfter, reference⟩ := result + obtain ⟨later, targetAfter, extension, targetLoaded, memories⟩ := + related.staticReference sourceFormed targetFormed staticBounds descriptor reference + resolved + refine ⟨later, extension, ?_⟩ + simp only [targetLoaded] + exact .ok ⟨memories, by + simp only [AllocationRenaming.value, + AllocationRenaming.managedReference]⟩ + +#print axioms Memory.Renamed.staticAddressResult + end CIL.Safety diff --git a/CIL/Safety/StaticInstructionWorld.lean b/CIL/Safety/StaticInstructionWorld.lean index d8851b76e..97f48e3dd 100644 --- a/CIL/Safety/StaticInstructionWorld.lean +++ b/CIL/Safety/StaticInstructionWorld.lean @@ -70,7 +70,7 @@ theorem memoryInstruction_preserves_static_world (descriptors : List CIL.StaticD exact (memoryInstruction_effect _ _ _ _ _ h).staticWorld descriptors world theorem instruction_preserves_static_world (descriptors : List CIL.StaticDescriptor) - (operation : CIL.Op) (stack values : List Value) (m result : Memory) + (operation : CIL.Instruction) (stack values : List Value) (m result : Memory) (world : StaticWorldValid descriptors m) (h : instruction operation stack m = .ok (result, values)) : StaticWorldValid descriptors result := by @@ -107,6 +107,25 @@ theorem staticInstruction_preserves_static_world (descriptors : List CIL.StaticD StaticWorldValid descriptors result := by exact (staticInstruction_effect _ _ _ _ _ _ _ h).staticWorld descriptors hm sites world +/-- A successful `ldsfld` keeps the static world valid when the method's static sites all name + descriptors of that world. -/ +theorem staticLoad_preserves_static_world (descriptors : List CIL.StaticDescriptor) + (body : CIL.Method) (pc : Nat) (m result : Memory) (value : Value) (hm : m.WellFormed) + (sites : StaticSitesValid descriptors body) (world : StaticWorldValid descriptors m) + (h : staticLoad body pc m = .ok (result, value)) : StaticWorldValid descriptors result := + (staticLoad_contract body pc [] m result value h).effect.staticWorld descriptors hm sites world + +/-- A successful `ldsflda` or field `ldtoken` keeps the static world valid when the method's + static sites all name descriptors of that world. -/ +theorem staticAddress_preserves_static_world (descriptors : List CIL.StaticDescriptor) + (body : CIL.Method) (pc : Nat) (m result : Memory) (value : Value) (hm : m.WellFormed) + (sites : StaticSitesValid descriptors body) (world : StaticWorldValid descriptors m) + (h : staticAddress body pc m = .ok (result, value)) : StaticWorldValid descriptors result := + (staticAddress_contract body pc [] m result value h).effect.staticWorld descriptors hm sites + world + +#print axioms staticLoad_preserves_static_world +#print axioms staticAddress_preserves_static_world #print axioms write_preserves_static_world #print axioms memoryInstruction_preserves_static_world #print axioms instruction_preserves_static_world diff --git a/CIL/Safety/StaticLinking.lean b/CIL/Safety/StaticLinking.lean index 61092a9f5..3d1cfbaf8 100644 --- a/CIL/Safety/StaticLinking.lean +++ b/CIL/Safety/StaticLinking.lean @@ -6,7 +6,7 @@ import CIL.Safety.ExecutionStaticWorld `linkChecked` links an application with its libraries and accepts the result only if no two distinct static descriptors share an identity, so the linked program has one static world. -On a collision it reports the two conflicting fields. +On a collision it reports the first two conflicting descriptors as a `StaticCollision`. -/ namespace CIL.Safety @@ -19,48 +19,6 @@ instance (descriptors : List CIL.StaticDescriptor) : Decidable (StaticDescriptorsCompatible descriptors) := inferInstanceAs (Decidable (descriptors.Pairwise _)) -/-- The error message for incompatible descriptors, naming the first colliding pair found. -/ -def staticCollision (descriptors : List CIL.StaticDescriptor) : String := - let pairs := descriptors.flatMap fun left => descriptors.map fun right => (left, right) - match - pairs.find? (fun (left, right) => left.identity == right.identity && !(left == right)) - with - | none => "Static identity collision" - | some (left, right) => - s!"Static identity collision {left.identity}: {left.assemblyIdentity}::{left.fieldName}" ++ - s!" and {right.assemblyIdentity}::{right.fieldName}" - -/-- Links `app` with `libraries`, failing when two static descriptors collide. -/ -def linkChecked (app : CIL.Program) (libraries : List Library) : Except String CIL.Program := - let linked := link app libraries - let descriptors := programStaticDescriptors linked - if StaticDescriptorsCompatible descriptors then .ok linked - else .error (staticCollision descriptors) - -theorem linkChecked_sound {app : CIL.Program} {libraries : List Library} {linked : CIL.Program} - (checked : linkChecked app libraries = .ok linked) : - linked = link app libraries ∧ - StaticDescriptorsCompatible (programStaticDescriptors linked) := by - dsimp only [linkChecked] at checked - split at checked - next compatible => - cases checked - exact ⟨rfl, compatible⟩ - next incompatible => cases checked - -theorem linkChecked_of_compatible {app : CIL.Program} {libraries : List Library} - (compatible : StaticDescriptorsCompatible (programStaticDescriptors (link app libraries))) : - linkChecked app libraries = .ok (link app libraries) := by - dsimp only [linkChecked] - split - · rfl - · contradiction - -theorem linkChecked_links {app : CIL.Program} {libraries : List Library} {linked : CIL.Program} - (checked : linkChecked app libraries = .ok linked) : Links app linked := by - rw [(linkChecked_sound checked).1] - exact link_links app libraries - /-- Compatibility in the form `StaticWorldValid` asks for: any two members with the same identity are equal. -/ theorem StaticDescriptorsCompatible.identities {descriptors : List CIL.StaticDescriptor} @@ -83,9 +41,81 @@ theorem StaticDescriptorsCompatible.identities {descriptors : List CIL.StaticDes exact (head left leftMember same.symm).symm · exact ih tail left leftMember right rightMember same +/-- Two descriptors that share an identity and differ, as `linkChecked` reports them. -/ +structure StaticCollision where + left : CIL.StaticDescriptor + right : CIL.StaticDescriptor + deriving DecidableEq, Repr + +/-- The report of a collision, naming its identity and both fields. -/ +def StaticCollision.message (collision : StaticCollision) : String := + s!"Static identity collision {collision.left.identity}: {reprStr collision.left.field}" ++ + s!" and {reprStr collision.right.field}" + +/-- The first pair of descriptors, in list order, that share an identity and differ. -/ +def firstCollision (descriptors : List CIL.StaticDescriptor) : Option StaticCollision := + let pairs := descriptors.flatMap fun left => descriptors.map fun right => (left, right) + (pairs.find? fun (left, right) => left.identity == right.identity && !(left == right)).map + fun (left, right) => { left, right } + +/-- Descriptors with no collision are compatible, and compatible ones have none. -/ +theorem firstCollision_eq_none {descriptors : List CIL.StaticDescriptor} : + firstCollision descriptors = none ↔ StaticDescriptorsCompatible descriptors := by + simp only [firstCollision, Option.map_eq_none_iff, List.find?_eq_none, List.mem_flatMap, + List.mem_map, Bool.and_eq_true, beq_iff_eq, Bool.not_eq_true', beq_eq_false_iff_ne] + constructor + · intro none + refine List.pairwise_of_forall_mem_list fun left leftMember right rightMember same => ?_ + exact Classical.byContradiction fun different => + none (left, right) ⟨left, leftMember, right, rightMember, rfl⟩ ⟨same, different⟩ + · intro compatible pair member found + obtain ⟨left, leftMember, right, rightMember, rfl⟩ := member + exact found.2 (compatible.identities left leftMember right rightMember found.1) + +/-- Links `app` with `libraries`, failing with the first collision of two static + descriptors. -/ +def linkChecked (app : CIL.Program) (libraries : List Library) : + Except StaticCollision CIL.Program := + let linked := link app libraries + match firstCollision (programStaticDescriptors linked) with + | none => .ok linked + | some collision => .error collision + +theorem linkChecked_sound {app : CIL.Program} {libraries : List Library} {linked : CIL.Program} + (checked : linkChecked app libraries = .ok linked) : + linked = link app libraries ∧ + StaticDescriptorsCompatible (programStaticDescriptors linked) := by + dsimp only [linkChecked] at checked + split at checked + next none => + cases checked + exact ⟨rfl, firstCollision_eq_none.mp none⟩ + next => cases checked + +theorem linkChecked_of_compatible {app : CIL.Program} {libraries : List Library} + (compatible : StaticDescriptorsCompatible (programStaticDescriptors (link app libraries))) : + linkChecked app libraries = .ok (link app libraries) := by + dsimp only [linkChecked] + rw [firstCollision_eq_none.mpr compatible] + +/-- `linkChecked` fails with the first collision of the linked program's descriptors. -/ +theorem linkChecked_of_collision {app : CIL.Program} {libraries : List Library} + {collision : StaticCollision} + (found : firstCollision (programStaticDescriptors (link app libraries)) = some collision) : + linkChecked app libraries = .error collision := by + dsimp only [linkChecked] + rw [found] + +theorem linkChecked_links {app : CIL.Program} {libraries : List Library} {linked : CIL.Program} + (checked : linkChecked app libraries = .ok linked) : Links app linked := by + rw [(linkChecked_sound checked).1] + exact link_links app libraries + +#print axioms firstCollision_eq_none #print axioms linkChecked_sound #print axioms linkChecked_of_compatible #print axioms linkChecked_links +#print axioms linkChecked_of_collision #print axioms StaticDescriptorsCompatible.identities end CIL.Safety diff --git a/CIL/Safety/StaticMemory.lean b/CIL/Safety/StaticMemory.lean index 0b13701cb..e0b0b5b3b 100644 --- a/CIL/Safety/StaticMemory.lean +++ b/CIL/Safety/StaticMemory.lean @@ -5,10 +5,10 @@ import CIL.Safety.InstructionMemory `staticReference` resolves an extracted readonly static field to immutable storage, creating it on first use and revalidating a cached binding on every later use. An opaque field's value -is not extracted, so it must already be bound by the caller and is never created. -`staticInstruction` runs the static and span instructions on top of it and hands every other -memory instruction to `memoryInstruction`. `staticSpanAligned` is the element alignment a typed -static span needs. +is not extracted, so it must already be bound by the caller and is never created. `staticLoad` +and `staticAddress` run `ldsfld`, `ldsflda` and a field `ldtoken` through the descriptor +bound to their pc. `staticInstruction` hands every memory instruction, static spans included, to +`memoryInstruction`. -/ namespace CIL.Safety @@ -63,61 +63,36 @@ def staticReference (descriptor : CIL.StaticDescriptor) (m : Memory) : views := ⟨id, 0, descriptor.bytes.length, true, false⟩ :: memory.views staticBindings := (descriptor.identity, reference) :: memory.staticBindings }, reference) -/-- CoreCLR's `CreateSpan` throws unless the field data is aligned to `sizeof(T)`, so a typed - span needs `width` to divide the allocation's guaranteed alignment. -/ -def staticSpanAligned (allocation : Allocation) (width : Nat) : Bool := - 0 < width && allocation.layout.alignment % width == 0 +/-- `ldsfld` at `pc`: an opaque value read from the caller's binding, or the value an initializer + was checked to produce. RVA data has no extracted value to load. -/ +def staticLoad (body : CIL.Method) (pc : Nat) (memory : Memory) : + ExecutionResult (Memory × Value) := do + let some descriptor := body.staticAt pc | + .error (.unsupported "Missing extracted static field identity") + if descriptor.opaqueValue then + let (memory, reference) ← staticReference descriptor memory + let loaded ← checkedAt (.address reference) descriptor.bytes.length + (read memory reference descriptor.bytes.length 1) + let some value := staticLoadedValue { descriptor with bytes := loaded } | .error .invalidState + return (memory, .scalar value) + -- The translation certificate checks each initializer's result against these bytes. + else if descriptor.initializer.isSome then + let some value := staticLoadedValue descriptor | .error .invalidState + return (memory, .scalar value) + else .error (.unsupported "Static field value is not extracted") -/-- Runs a memory instruction at `pc`. Static loads use the field identity extracted for that - pc; span constructors over raw pointers are accepted only inside extracted static data. -/ -def staticInstruction (body : CIL.Method) (pc : Nat) (operation : CIL.MemoryOp) - (stack : List Value) (m : Memory) : ExecutionResult (Memory × List Value) := do - match operation, stack with - | .staticAddress bytes, _ => - let some (_, descriptor) := body.staticSites.find? (fun site => site.1 == pc) | - .error (.unsupported "Missing extracted static field identity") - if descriptor.bytes == bytes then pure () else .error .invalidState - let (memory, reference) ← staticReference descriptor m - return (memory, .reference (.address reference) :: stack) - | .staticValue bytes, _ => - let some (_, descriptor) := body.staticSites.find? (fun site => site.1 == pc) | - .error (.unsupported "Missing extracted static field identity") - -- An opaque value is whatever the caller's binding holds; `bytes` fixes only its width. - if descriptor.opaqueValue then - if descriptor.bytes == bytes then - let (memory, reference) ← staticReference descriptor m - let loaded ← checkedAt (.address reference) bytes.length - (read memory reference bytes.length 1) - let some value := staticLoadedValue { descriptor with bytes := loaded } | - .error .invalidState - return (memory, .scalar value :: stack) - else .error .invalidState - -- The translation certificate checks each initializer's result against these bytes. - else if descriptor.bytes == bytes && descriptor.initializer.isSome then - let some value := staticLoadedValue descriptor | .error .invalidState - return (m, .scalar value :: stack) - else .error .invalidState - | .spanCreate, .scalar (.i32 length) :: .reference (.address reference) :: rest => - if length.toInt < 0 then .error .invalidState else pure () - let allocation ← checkedAt (.address reference) length.toNat - (liveAllocation m reference.allocation) - if allocation.kind == .immutableStatic then pure () else - .error (.unsupported "Span pointer constructor outside extracted static data") - let _ ← formValue m (.address reference) - if reference.offset + length.toNat ≤ allocation.layout.size then pure () else - .error (.memory .outsideAllocation (.address reference) length.toNat) - return (m, .span (.address reference) length.toNat :: rest) - | .createSpan width, .reference (.address reference) :: rest => - let allocation ← checkedAt (.address reference) 0 (liveAllocation m reference.allocation) - if allocation.kind == .immutableStatic && reference.offset == 0 && 0 < width && - allocation.layout.size % width == 0 then - if staticSpanAligned allocation width then - return (m, .span (.address reference) (allocation.layout.size / width) :: rest) - else .error (.unsupported "Typed span element width does not divide the field alignment") - else .error (.unsupported "Typed span outside a whole extracted static field") - | .spanCreate, .scalar (.i32 length) :: .reference .null :: rest => - if length == 0 then return (m, .span .null 0 :: rest) - else .error (.memory .nullDereference .null length.toNat) - | _, _ => memoryInstruction operation stack m +/-- `ldsflda` or a field `ldtoken` at `pc`: the reference to the read-only storage of the + descriptor bound there. -/ +def staticAddress (body : CIL.Method) (pc : Nat) (memory : Memory) : + ExecutionResult (Memory × Value) := do + let some descriptor := body.staticAt pc | + .error (.unsupported "Missing extracted static field identity") + let (memory, reference) ← staticReference descriptor memory + return (memory, .reference (.address reference)) + +/-- Runs a memory instruction, reading neither the method nor the pc. -/ +def staticInstruction (_body : CIL.Method) (_pc : Nat) (operation : CIL.MemoryOp) + (stack : List Value) (m : Memory) : ExecutionResult (Memory × List Value) := + memoryInstruction operation stack m end CIL.Safety diff --git a/CIL/Safety/StaticMemoryEquivalence.lean b/CIL/Safety/StaticMemoryEquivalence.lean index 801d78ce5..5ce50a49a 100644 --- a/CIL/Safety/StaticMemoryEquivalence.lean +++ b/CIL/Safety/StaticMemoryEquivalence.lean @@ -7,7 +7,7 @@ import CIL.Safety.InstructionMemoryEquivalence Equivalent memories (same allocations, bytes, bindings and permissions, however the access views are listed) give equivalent results for static field resolution and for the static -and span instructions. +field instructions. -/ namespace CIL.Safety @@ -48,33 +48,56 @@ theorem Memory.Equivalent.staticReference {m n : Memory} (same : m.Equivalent n) · exact (equivalent.prependView _).permissions · simp only [equivalent.staticBindings] -/-- Equivalent memories give equivalent results for every static or span instruction. -/ +/-- Equivalent memories give equivalent results for every static or memory instruction. -/ theorem Memory.Equivalent.staticInstruction {m n : Memory} (same : m.Equivalent n) (body : CIL.Method) (pc : Nat) (operation : CIL.MemoryOp) (stack : List Value) : ResultEquivalent MemoryFirstEquivalent (CIL.Safety.staticInstruction body pc operation stack m) (CIL.Safety.staticInstruction body pc operation stack n) := by unfold CIL.Safety.staticInstruction - split <;> try simp only [checkedAt, liveAllocation, same.allocations, same.formValue] + exact same.memoryInstruction _ _ + +#print axioms Memory.Equivalent.staticInstruction + +/-- Equivalent memories give equivalent results for `ldsfld`. -/ +theorem Memory.Equivalent.staticLoad {m n : Memory} (same : m.Equivalent n) + (body : CIL.Method) (pc : Nat) : + ResultEquivalent MemoryFirstEquivalent (CIL.Safety.staticLoad body pc m) + (CIL.Safety.staticLoad body pc n) := by + unfold CIL.Safety.staticLoad + split all_goals repeat first - | exact same.memoryInstruction _ _ | exact .ok ⟨same, rfl⟩ | exact .error _ - | (apply ResultEquivalent.bind_same; intro) | (apply ResultEquivalent.bind (same.staticReference _) intro left right related obtain ⟨memory, reference⟩ := left obtain ⟨otherMemory, otherReference⟩ := right obtain ⟨equivalent, rfl⟩ := related - first + simp only [checkedAt, equivalent.read] + repeat first | exact .ok ⟨equivalent, rfl⟩ - | (simp only [equivalent.read] - repeat first - | exact .ok ⟨equivalent, rfl⟩ - | exact .error _ - | (apply ResultEquivalent.bind_same; intro) - | split)) + | exact .error _ + | (apply ResultEquivalent.bind_same; intro) + | split) | split -#print axioms Memory.Equivalent.staticInstruction +#print axioms Memory.Equivalent.staticLoad + +/-- Equivalent memories give equivalent results for `ldsflda` and a field `ldtoken`. -/ +theorem Memory.Equivalent.staticAddress {m n : Memory} (same : m.Equivalent n) + (body : CIL.Method) (pc : Nat) : + ResultEquivalent MemoryFirstEquivalent (CIL.Safety.staticAddress body pc m) + (CIL.Safety.staticAddress body pc n) := by + unfold CIL.Safety.staticAddress + split + · apply ResultEquivalent.bind (same.staticReference _) + intro left right related + obtain ⟨memory, reference⟩ := left + obtain ⟨otherMemory, otherReference⟩ := right + obtain ⟨equivalent, rfl⟩ := related + exact .ok ⟨equivalent, rfl⟩ + · exact .error _ + +#print axioms Memory.Equivalent.staticAddress #print axioms Memory.Equivalent.staticReference end CIL.Safety diff --git a/CIL/Safety/StaticMemoryLemmas.lean b/CIL/Safety/StaticMemoryLemmas.lean index 6df25ad96..f97d9a409 100644 --- a/CIL/Safety/StaticMemoryLemmas.lean +++ b/CIL/Safety/StaticMemoryLemmas.lean @@ -171,12 +171,6 @@ theorem staticReference_reference_valid (descriptor : CIL.StaticDescriptor) (m r simp [form, liveAllocation, lookup, validPosition, nativeLimit, position, Bind.bind, Except.bind, Pure.pure, Except.pure] -theorem formValue_reference_valid (m : Memory) (r : ManagedReference) (value : Value) - (h : formValue m r = .ok value) : (Value.reference r).Valid m := by - have valid := formValue_result_valid _ _ _ h - rw [formValue_returns_input _ _ _ h] at valid - exact valid - /-- What a successful static instruction guarantees: its memory effect is an allowed `StaticInstructionEffect`, and the values it leaves are valid whenever its input stack was. -/ structure StaticInstructionContract (body : CIL.Method) (m result : Memory) @@ -214,25 +208,7 @@ theorem staticInstruction_contract (body : CIL.Method) (pc : Nat) (operation : C (h : staticInstruction body pc operation stack m = .ok (result, values)) : StaticInstructionContract body m result stack values := by unfold staticInstruction at h - split at h <;> try simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h - all_goals repeat' first - | solve | exact .ordinary (memoryInstruction_contract _ _ _ _ _ (by assumption)) - | solve | - refine StaticInstructionContract.of_effect ?_ ?_ - · first | exact .ordinary (.unchanged _) | exact .reference (by assumption) (by assumption) - · intro retained - apply ValuesValid.cons - · first - | exact True.intro - | rfl - | exact retained.tail.head - | exact retained.head - | apply staticReference_reference_valid; assumption - | apply formValue_reference_valid; assumption - | apply staticLoadedValue_valid; assumption - · first | exact retained | exact retained.tail | exact retained.tail.tail - | cases h - | split at h + exact .ordinary (memoryInstruction_contract _ _ _ _ _ h) theorem staticInstruction_effect (body : CIL.Method) (pc : Nat) (operation : CIL.MemoryOp) (stack values : List Value) (m result : Memory) @@ -248,4 +224,124 @@ theorem staticInstruction_preserves_wellFormed (body : CIL.Method) (pc : Nat) #print axioms staticInstruction_preserves_wellFormed +/-- A descriptor `Method.staticAt` resolves is the one of the method's first site at that pc. -/ +theorem staticAt_site {body : CIL.Method} {pc : Nat} {descriptor : CIL.StaticDescriptor} + (resolved : body.staticAt pc = some descriptor) : + ∃ site, body.staticSites.find? (fun site => site.1 == pc) = some (site, descriptor) := by + unfold CIL.Method.staticAt at resolved + cases found : body.staticSites.find? (fun site => site.1 == pc) with + | none => simp [found] at resolved + | some entry => + obtain ⟨site, other⟩ := entry + simp only [found, Option.map_some, Option.some.injEq] at resolved + exact ⟨site, by rw [resolved]⟩ + +#print axioms staticAt_site + +/-- A successful `ldsfld` satisfies `StaticInstructionContract` for the value it pushes. -/ +theorem staticLoad_contract (body : CIL.Method) (pc : Nat) (stack : List Value) + (m result : Memory) (value : Value) (h : staticLoad body pc m = .ok (result, value)) : + StaticInstructionContract body m result stack (value :: stack) := by + unfold staticLoad at h + cases resolved : body.staticAt pc with + | none => simp only [resolved] at h; cases h + | some descriptor => + obtain ⟨site, selected⟩ := staticAt_site resolved + simp only [resolved, Bind.bind, Except.bind, Pure.pure, Except.pure] at h + repeat' first + | solve | + refine StaticInstructionContract.of_effect ?_ ?_ + · first | exact .ordinary (.unchanged _) | exact .reference selected (by assumption) + · intro retained + apply ValuesValid.cons + · apply staticLoadedValue_valid; assumption + · exact retained + | cases h + | split at h + +#print axioms staticLoad_contract + +/-- A successful `ldsflda` or field `ldtoken` satisfies `StaticInstructionContract` for the + reference it pushes. -/ +theorem staticAddress_contract (body : CIL.Method) (pc : Nat) (stack : List Value) + (m result : Memory) (value : Value) (h : staticAddress body pc m = .ok (result, value)) : + StaticInstructionContract body m result stack (value :: stack) := by + unfold staticAddress at h + cases resolved : body.staticAt pc with + | none => simp only [resolved] at h; cases h + | some descriptor => + obtain ⟨site, selected⟩ := staticAt_site resolved + simp only [resolved, Bind.bind, Except.bind, Pure.pure, Except.pure] at h + split at h + · cases h + · rename_i resolution referenced + obtain ⟨memory, reference⟩ := resolution + cases h + refine StaticInstructionContract.of_effect (.reference selected referenced) ?_ + intro retained + apply ValuesValid.cons + · apply staticReference_reference_valid; exact referenced + · exact retained + +#print axioms staticAddress_contract + +/-- `ldsfld` of a descriptor whose initializer was checked pushes the descriptor's value and + leaves memory unchanged. -/ +theorem staticLoad_initialized {body : CIL.Method} {pc : Nat} {descriptor : CIL.StaticDescriptor} + {memory : Memory} {value : CIL.Value} (site : body.staticAt pc = some descriptor) + (computed : descriptor.opaqueValue = false) + (initialized : descriptor.initializer.isSome = true) + (decoded : staticLoadedValue descriptor = some value) : + staticLoad body pc memory = .ok (memory, .scalar value) := by + simp only [staticLoad, site, computed, initialized, decoded, Bool.false_eq_true, ite_false, + ite_true] + rfl + +#print axioms staticLoad_initialized + +/-- `ldsflda` or a field `ldtoken` pushes the reference `staticReference` resolves for the + descriptor bound to its pc. -/ +theorem staticAddress_resolved {body : CIL.Method} {pc : Nat} {descriptor : CIL.StaticDescriptor} + {memory result : Memory} {reference : Reference} (site : body.staticAt pc = some descriptor) + (resolved : staticReference descriptor memory = .ok (result, reference)) : + staticAddress body pc memory = .ok (result, .reference (.address reference)) := by + simp only [staticAddress, site, resolved, Bind.bind, Except.bind, Pure.pure, Except.pure] + +#print axioms staticAddress_resolved + +/-- With a descriptor bound to its pc, `staticAddress` resolves that descriptor's storage, so a + step can rewrite the read without evaluating the site table. -/ +theorem staticAddress_site {body : CIL.Method} {pc : Nat} {descriptor : CIL.StaticDescriptor} + {memory : Memory} (site : body.staticAt pc = some descriptor) : + staticAddress body pc memory = (do + let (memory, reference) ← staticReference descriptor memory + return (memory, .reference (.address reference))) := by + rw [staticAddress, site] + +#print axioms staticAddress_site + +/-- A loaded static field value is a scalar, never a home. -/ +theorem staticLoad_not_spanHome {body : CIL.Method} {pc : Nat} {memory : Memory} + {result : Memory × Value} (loaded : staticLoad body pc memory = .ok result) : + result.2.isHome = false := by + unfold staticLoad at loaded + repeat' first + | (cases loaded <;> rfl) + | split at loaded + | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at loaded + +#print axioms staticLoad_not_spanHome + +/-- A static field address is a reference, never a home. -/ +theorem staticAddress_not_spanHome {body : CIL.Method} {pc : Nat} {memory : Memory} + {result : Memory × Value} (addressed : staticAddress body pc memory = .ok result) : + result.2.isHome = false := by + unfold staticAddress at addressed + repeat' first + | (cases addressed <;> rfl) + | split at addressed + | simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at addressed + +#print axioms staticAddress_not_spanHome + end CIL.Safety diff --git a/CIL/Safety/StaticSpanAlignment.lean b/CIL/Safety/StaticSpanAlignment.lean index 8ce77033c..02acbeeb8 100644 --- a/CIL/Safety/StaticSpanAlignment.lean +++ b/CIL/Safety/StaticSpanAlignment.lean @@ -22,16 +22,18 @@ theorem staticSpanAligned_divides {allocation : Allocation} {width : Nat} /-- A successful typed static span starts a live allocation whose guaranteed alignment is a multiple of the element width. -/ -theorem createSpan_aligned {body : CIL.Method} {pc width : Nat} {stack values : List Value} - {m result : Memory} - (created : staticInstruction body pc (.createSpan width) stack m = .ok (result, values)) : +theorem createSpan_aligned {width : Nat} {stack values : List Value} {m result : Memory} + (created : memoryInstruction (.createSpan width) stack m = .ok (result, values)) : ∃ (reference : Reference) (rest : List Value) (allocation : Allocation), stack = .reference (.address reference) :: rest ∧ reference.offset = 0 ∧ m.allocations reference.allocation = some allocation ∧ allocation.live = true ∧ 0 < width ∧ width ∣ allocation.layout.alignment := by - unfold staticInstruction at created + simp only [memoryInstruction] at created + split at created + · cases created + unfold staticSpanInstruction at created split at created <;> try contradiction - · rename_i reference rest same + · rename_i reference rest same _ cases same cases live : liveAllocation m reference.allocation with | error fault => simp [live, checkedAt, Except.mapError, Bind.bind, Except.bind] at created @@ -51,17 +53,12 @@ theorem createSpan_aligned {body : CIL.Method} {pc width : Nat} {stack values : staticSpanAligned_divides (by assumption)⟩ · cases created · cases created - · unfold memoryInstruction at created - split at created - · cases created - · unfold otherMemoryInstruction at created - split at created <;> simp_all /-- Under every valid placement, a successful typed static span's base address is a multiple of its element width, which is the condition CoreCLR's `CreateSpan` enforces. -/ -theorem createSpan_aligned_at_placement {body : CIL.Method} {pc width : Nat} - {stack values : List Value} {m result : Memory} - (created : staticInstruction body pc (.createSpan width) stack m = .ok (result, values)) +theorem createSpan_aligned_at_placement {width : Nat} {stack values : List Value} + {m result : Memory} + (created : memoryInstruction (.createSpan width) stack m = .ok (result, values)) (placement : Placement) (placed : placement.Valid m) : ∃ (reference : Reference) (rest : List Value), stack = .reference (.address reference) :: rest ∧ 0 < width ∧ diff --git a/CIL/Safety/StepComposition.lean b/CIL/Safety/StepComposition.lean index 9d1731d50..62d742125 100644 --- a/CIL/Safety/StepComposition.lean +++ b/CIL/Safety/StepComposition.lean @@ -15,278 +15,1141 @@ stepping. namespace CIL.Safety /-- Observing an unspecified operand faults before any instruction changes memory or state. -/ -theorem step_unspecified {body : CIL.Method} {operation : CIL.Op} {pc : Nat} +theorem step_unspecified {body : CIL.Method} {operation : CIL.Instruction} {pc : Nat} {args stack : List Value} {frame : Frame} {memory : Memory} (unknown : unspecifiedOperands (observerCount operation) stack = true) : step body operation pc args frame stack memory = .error (.unsupported "Instruction observes unspecified vector bits") := by - simp only [step, unknown, Bool.true_or, ite_true] + simp only [step, unknown, ite_true] #print axioms step_unspecified section InstructionEquations variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (memory : Memory) -/-- Expose an argument load without expanding the instruction dispatcher. -/ -@[cil_step] theorem step_argument_eq (index : Nat) (stack : List Value) : - step body (.arg index) pc args frame stack memory = do +-- Equations that hold by unfolding are closed by `id rfl`: simp applies a lemma proved by `rfl` +-- without a proof term, so the kernel would reduce `step` again at every use. + +/-- Expose an argument load without expanding the instruction dispatcher; the read itself is + `loadArgument`. Stepping uses this form only while the method's argument access is unknown, + and the three equations below once it is known. -/ +theorem step_ldarg_eq (index : Nat) (stack : List Value) : + step body (.ldarg index) pc args frame stack memory = do + return FrameAction.next (pc + 1) ((← loadArgument body index args frame memory) :: stack) + frame memory := + id rfl + +#print axioms step_ldarg_eq + +attribute [cil_step] loadArgument + +/-- An argument read without a home or narrowing pushes the checked incoming value. -/ +theorem step_ldarg_value_eq (index : Nat) (stack : List Value) + (access : body.argumentAccess index = .value none) : + step body (.ldarg index) pc args frame stack memory = do let some value := args[index]? | .error .invalidState - return FrameAction.next (pc + 1) ((← checkedValue memory value) :: stack) frame memory := - rfl + return FrameAction.next (pc + 1) ((← checkedValue memory value) :: stack) frame memory := by + simp only [step_ldarg_eq, loadArgument, access] + cases args[index]? <;> rfl -/-- A narrowed argument load checks that the selected argument has no unspecified bits, - then extends an i32 according to the parameter's declared width. -/ -@[cil_step] theorem step_argument_narrow_eq (index : Nat) (kind : CIL.NarrowKind) - (stack : List Value) : - step body (.arg index (some kind)) pc args frame stack memory = +#print axioms step_ldarg_value_eq + +/-- An argument of a narrow declared type faults on unspecified bits, and otherwise pushes its i32 + truncated and extended to that type. -/ +theorem step_ldarg_narrow_eq (index : Nat) (stack : List Value) (kind : CIL.NarrowKind) + (access : body.argumentAccess index = .value (some kind)) : + step body (.ldarg index) pc args frame stack memory = if (args[index]?.map Value.hasUnspecified).getD false then .error (.unsupported "Instruction observes unspecified vector bits") else do let some (.scalar (.i32 bits)) := args[index]? | .error .invalidState return FrameAction.next (pc + 1) ((← checkedValue memory (.scalar (.i32 (kind.extend bits)))) :: stack) frame memory := by - simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, - Bool.false_or] + simp only [step_ldarg_eq, loadArgument, access] + cases args[index]? with + | none => rfl + | some value => + cases unknown : value.hasUnspecified + · cases value + case scalar inner => cases inner <;> rfl + all_goals simp_all [Value.hasUnspecified] <;> rfl + · simp [unknown]; rfl + +#print axioms step_ldarg_narrow_eq + +/-- A homed argument reads its frame slot, as `ldloc` of that slot does. -/ +theorem step_ldarg_home_eq (index : Nat) (stack : List Value) (slot : Nat) + (access : body.argumentAccess index = .home slot) : + step body (.ldarg index) pc args frame stack memory = do + let some home := frame.locals[slot]? | .error .invalidState + return FrameAction.next (pc + 1) ((← loadLocal memory home) :: stack) frame memory := by + simp only [step_ldarg_eq, loadArgument, access] + cases frame.locals[slot]? <;> rfl + +#print axioms step_ldarg_home_eq + +/-- Expose an argument store without expanding the instruction dispatcher; the write itself is + `storeArgument`. Stepping uses this form only while the method's argument access is unknown, + and the two equations below once it is known. -/ +theorem step_starg_eq (index : Nat) (value : Value) (rest : List Value) : + step body (.starg index) pc args frame (value :: rest) memory = do + let (frame, memory) ← storeArgument body index value frame memory + return FrameAction.next (pc + 1) rest frame memory := + id rfl + +attribute [cil_step] storeArgument + +/-- A homed argument store writes its frame slot, as `stloc` of that slot does. -/ +theorem step_starg_home_eq (index : Nat) (value : Value) (rest : List Value) (slot : Nat) + (access : body.argumentAccess index = .home slot) : + step body (.starg index) pc args frame (value :: rest) memory = do + let some home := frame.locals[slot]? | .error .invalidState + let (home, memory) ← storeLocal memory home value + return FrameAction.next (pc + 1) rest { frame with locals := frame.locals.set slot home } + memory := by + simp only [step_starg_eq, storeArgument, access] + cases frame.locals[slot]? with + | none => rfl + | some home => + dsimp only + cases storeLocal memory home value <;> rfl + +#print axioms step_starg_home_eq + +/-- A store to an argument without a home faults as unsupported. -/ +theorem step_starg_value_eq (index : Nat) (value : Value) (rest : List Value) + (narrow : Option CIL.NarrowKind) (access : body.argumentAccess index = .value narrow) : + step body (.starg index) pc args frame (value :: rest) memory = + .error (.unsupported "Store to an argument without a home") := by + simp only [step_starg_eq, storeArgument, access] + rfl + +#print axioms step_starg_value_eq + +/-- Expose an argument address without expanding the instruction dispatcher; the address itself + is `addressArgument`. Stepping uses this form only while the method's argument access is + unknown, and the two equations below once it is known. -/ +theorem step_ldarga_eq (index : Nat) (stack : List Value) : + step body (.ldarga index) pc args frame stack memory = do + return FrameAction.next (pc + 1) ((← addressArgument body index frame memory) :: stack) + frame memory := + id rfl + +attribute [cil_step] addressArgument + +-- A home is read only where a call or span operation passes it; stepping evaluates each read. +attribute [cil_step] spanView callArgument viewOperandCount viewOperands + +/-- A homed argument's address is its frame slot's, as `ldloca` of that slot takes it. -/ +theorem step_ldarga_home_eq (index : Nat) (stack : List Value) (slot : Nat) + (access : body.argumentAccess index = .home slot) : + step body (.ldarga index) pc args frame stack memory = do + let some home := frame.locals[slot]? | .error .invalidState + return FrameAction.next (pc + 1) ((← localAddress memory slot home) :: stack) frame + memory := by + simp only [step_ldarga_eq, addressArgument, access] + cases frame.locals[slot]? <;> rfl + +#print axioms step_ldarga_home_eq + +/-- The address of an argument without a home faults as unsupported. -/ +theorem step_ldarga_value_eq (index : Nat) (stack : List Value) (narrow : Option CIL.NarrowKind) + (access : body.argumentAccess index = .value narrow) : + step body (.ldarga index) pc args frame stack memory = + .error (.unsupported "Address of an argument without a home") := by + simp only [step_ldarga_eq, addressArgument, access] rfl -#print axioms step_argument_narrow_eq +#print axioms step_ldarga_value_eq /-- Expose return validation without expanding the instruction dispatcher. -/ -@[cil_step] theorem step_return_eq (stack : List Value) : +@[cil_step] theorem step_ret_eq (stack : List Value) : step body .ret pc args frame stack memory = if unspecifiedOperands 1 stack then .error (.unsupported "Instruction observes unspecified vector bits") - else if stack.length = (if body.returnsValue then 1 else 0) then + else returnFrom body.returnNarrow body.returnsValue stack memory := by + simp only [step, observerCount] + rfl + +#print axioms step_ret_eq + +/-- A hole faults on every stack, before reading the frame or memory. -/ +@[cil_step] theorem step_unadmitted (opcode : Nat) (stack : List Value) : + step body (.unadmitted opcode) pc args frame stack memory = + .error (.unsupported "Opcode not admitted") := + id rfl + +#print axioms step_unadmitted + +/-- A plain return validates the declared number of result values. -/ +@[cil_step] theorem returnFrom_none_eq (returnsValue : Bool) (stack : List Value) : + returnFrom none returnsValue stack memory = + if stack.length = (if returnsValue then 1 else 0) then (do return FrameAction.returned (← stack.mapM (checkedValue memory)) memory) - else .error .invalidState := by - simp only [step, observerCount, Bool.or_false] + else .error .invalidState := rfl + +#print axioms returnFrom_none_eq + +end InstructionEquations + +/-- What a call taking `count` arguments does under the link `link` of its pc: enter a `.direct` + link's method, under `tail.` with the whole stack and retiring the frame; without a link run + `unlinked`, the call's runtime model, unless under `tail.`; any other link faults. A regular + definition, so the kernel checks a rewrite of its arguments by comparing them, where a `match` + on the link would make it evaluate the table's read. -/ +def callStep (link : Option CIL.Link) (count : Nat) (tail : Bool) + (unlinked : Unit → ExecutionResult FrameAction) (frame : Frame) (stack : List Value) + (memory : Memory) : ExecutionResult FrameAction := + match link with + | some (.direct callee) => + if tail then + if stack.length != count then .error .invalidState + else do + return FrameAction.tailCall callee + (← stack.reverse.mapM (callArgument frame memory)) memory + else if stack.length < count then .error .invalidState + else do + let arguments ← (stack.take count).reverse.mapM (callArgument frame memory) + return FrameAction.call callee arguments (stack.drop count) memory + | none => if tail then .error unlinkedCall else unlinked () + | some _ => .error unlinkedCall + +section InstructionEquations + +variable (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (memory : Memory) + +/-- A call enters the method its link names, after checking the arguments its method takes, + deepest first, and a call without a link runs its method's runtime model, which checks the + operands it observes, as `callStep` says. -/ +@[cil_step] theorem step_call_eq (method : Extraction.MethodRef) (prefixes : CallPrefixes) + (stack : List Value) : + step body (.call method prefixes) pc args frame stack memory = + if (body.linkAt pc).isSome && unspecifiedOperands method.argumentCount stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else callStep (body.linkAt pc) method.argumentCount prefixes.tail + (fun _ => modelStep (Extraction.runtimeModel body.environment .call method) body pc frame + stack memory) frame stack memory := by + simp only [step, observerCount, guard_operands_zero, Bool.false_eq_true, ite_false, callStep] + cases body.linkAt pc with + | none => rfl + | some link => cases link <;> rfl + +#print axioms step_call_eq + +/-- The step of a linked call is the one before a call left its operands to its model: the + same argument check, at the same point before the arm's other faults. -/ +theorem step_call_linked {method : Extraction.MethodRef} {prefixes : CallPrefixes} + {link : CIL.Link} (linked : body.linkAt pc = some link) (stack : List Value) : + step body (.call method prefixes) pc args frame stack memory = + if unspecifiedOperands method.argumentCount stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else callStep (some link) method.argumentCount prefixes.tail + (fun _ => .error unlinkedCall) frame stack memory := by + rw [step_call_eq, linked] + cases link <;> rfl + +#print axioms step_call_linked -/-- A call checks its `argc` arguments, deepest first, and leaves the rest of the stack for the +/-- A plain call checks its arguments, deepest first, and leaves the rest of the stack for the caller's continuation. -/ -@[cil_step] theorem step_call_eq (callee argc : Nat) (stack : List Value) : - step body (.call callee argc) pc args frame stack memory = - if unspecifiedOperands argc stack then +@[cil_step] theorem callStep_call (callee count : Nat) + (unlinked : Unit → ExecutionResult FrameAction) (stack : List Value) : + callStep (some (.direct callee)) count false unlinked frame stack memory = + if stack.length < count then .error .invalidState + else do + let arguments ← (stack.take count).reverse.mapM (callArgument frame memory) + return FrameAction.call callee arguments (stack.drop count) memory := rfl + +#print axioms callStep_call + +/-- A call under `tail.` takes the whole stack as its arguments and retires the frame. -/ +@[cil_step] theorem callStep_tailCall (callee count : Nat) + (unlinked : Unit → ExecutionResult FrameAction) (stack : List Value) : + callStep (some (.direct callee)) count true unlinked frame stack memory = + if stack.length != count then .error .invalidState + else do + return FrameAction.tailCall callee + (← stack.reverse.mapM (callArgument frame memory)) memory := + rfl + +#print axioms callStep_tailCall + +/-- A call whose pc has a link other than a `.direct` one faults. -/ +@[cil_step] theorem callStep_unlinked (link : CIL.Link) (count : Nat) (tail : Bool) + (unlinked : Unit → ExecutionResult FrameAction) (stack : List Value) + (undirected : ∀ callee, link ≠ .direct callee) : + callStep (some link) count tail unlinked frame stack memory = .error unlinkedCall := by + unfold callStep + split + · exact absurd (Option.some.inj ‹_›) (undirected _) + · contradiction + · rfl + +#print axioms callStep_unlinked + +/-- A call whose pc states no link runs its runtime model. -/ +@[cil_step] theorem callStep_none (count : Nat) + (unlinked : Unit → ExecutionResult FrameAction) (stack : List Value) : + callStep none count false unlinked frame stack memory = unlinked () := rfl + +#print axioms callStep_none + +/-- A `tail.` call whose pc states no link faults, since a model retires no frame. -/ +@[cil_step] theorem callStep_none_tail (count : Nat) + (unlinked : Unit → ExecutionResult FrameAction) (stack : List Value) : + callStep none count true unlinked frame stack memory = .error unlinkedCall := rfl + +#print axioms callStep_none_tail + +/-- A call whose pc states no link, outside `tail.`, runs the runtime model `found` gives its + method, which checks the operands it observes. Stepping selects it once the link and model + facts are known, so the kernel checks one application instead of rewriting `callStep`. -/ +theorem step_call_model {method : Extraction.MethodRef} {prefixes : CallPrefixes} + {model : CIL.RuntimeModel} (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = some model) + (direct : prefixes.tail = false) (stack : List Value) : + step body (.call method prefixes) pc args frame stack memory = + modelStep (some model) body pc frame stack memory := by + rw [step_call_eq, link, direct, found] + rfl + +#print axioms step_call_model + +/-- A call without a link whose method's model is an intrinsic steps as that intrinsic, which + decides itself which operand bits it reads. -/ +theorem step_call_intrinsic {method : Extraction.MethodRef} {prefixes : CallPrefixes} + {operation : CIL.Intrinsic} {argc : Nat} (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.intrinsic operation argc)) + (direct : prefixes.tail = false) : + step body (.call method prefixes) pc args frame stack memory = + intrinsicCall body.profile operation argc pc frame stack memory := by + rw [step_call_model (link := link) (found := found) (direct := direct)] + rfl + +#print axioms step_call_intrinsic + +/-- A decoded call of a feature getter pushes its profile flag and advances to the next PC. -/ +theorem step_call_feature {method : Extraction.MethodRef} {prefixes : CallPrefixes} + {feature : CIL.Feature} (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = some (.feature feature)) + (direct : prefixes.tail = false) : + step body (.call method prefixes) pc args frame stack memory = + .ok (.next (pc + 1) (.scalar (.i32 (if body.profile.evaluate feature then 1 else 0)) :: stack) + frame memory) := by + rw [step_call_model (link := link) (found := found) (direct := direct)] + rfl + +#print axioms step_call_feature + +/-- A memory model checks its operands, reads any span homes and performs the bound operation. -/ +@[cil_step low] theorem modelStep_memory (operation : CIL.MemoryOp) (stack : List Value) : + modelStep (some (.memory operation)) body pc frame stack memory = + if unspecifiedOperands operation.argumentCount stack then .error (.unsupported "Instruction observes unspecified vector bits") - else if stack.length < argc then .error .invalidState else do - let arguments ← (stack.take argc).reverse.mapM (checkedValue memory) - return FrameAction.call callee arguments (stack.drop argc) memory := by - simp only [step, observerCount, Bool.or_false] + let operands ← viewOperands frame memory (viewOperandCount operation) stack + let (result, values) ← staticInstruction body pc operation operands memory + return FrameAction.next (pc + 1) values frame result := id rfl -#print axioms step_call_eq +#print axioms modelStep_memory + +/-- An unlinked constructor dispatches its model after checking the explicit arguments. -/ +theorem step_newobj_model {method : Extraction.MethodRef} {model : CIL.RuntimeModel} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .newobj method = some model) + (stack : List Value) : + step body (.newobj method) pc args frame stack memory = + if unspecifiedOperands method.parameters.length stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else modelStep (some model) body pc frame stack memory := by + simp only [step, observerCount, newobjStep, link, found] + +#print axioms step_newobj_model + +/-- A memory constructor observes its explicit operands and pushes the operation's result. -/ +theorem step_newobj_memory {method : Extraction.MethodRef} {operation : CIL.MemoryOp} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .newobj method = some (.memory operation)) + (count : method.parameters.length = operation.argumentCount) (stack : List Value) : + step body (.newobj method) pc args frame stack memory = + modelStep (some (.memory operation)) body pc frame stack memory := by + rw [step_newobj_model (link := link) (found := found), count, modelStep_memory] + by_cases observed : unspecifiedOperands operation.argumentCount stack = true <;> + simp only [observed, Bool.false_eq_true, ite_true, ite_false] + +#print axioms step_newobj_memory + +/-- A call without a link whose method's model is `SkipInit` checks that its reference operand + forms, then pops it and preserves memory. -/ +theorem step_call_skipInit_reference {method : Extraction.MethodRef} {prefixes : CallPrefixes} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = some .skipInit) + (direct : prefixes.tail = false) (reference : ManagedReference) (rest : List Value) : + step body (.call method prefixes) pc args frame (.reference reference :: rest) memory = do + let _ ← formValue memory reference + return .next (pc + 1) rest frame memory := by + rw [step_call_model (link := link) (found := found) (direct := direct)] + simp only [modelStep, observedCall, runModel, skipInitCall, CIL.RuntimeModel.observerCount] + rfl + +#print axioms step_call_skipInit_reference + +/-- A checked SkipInit call on a formed address pops the reference without writing to it. -/ +theorem step_call_skipInit_address {method : Extraction.MethodRef} {prefixes : CallPrefixes} + (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = some .skipInit) + (direct : prefixes.tail = false) {address : Reference} {rest : List Value} + (formed : form memory address = .ok address) : + step body (.call method prefixes) pc args frame (.reference (.address address) :: rest) memory = + .ok (.next (pc + 1) rest frame memory) := by + rw [step_call_skipInit_reference (link := link) (found := found) (direct := direct)] + simp [formValue, checkedAt, formed, Except.mapError, Bind.bind, Except.bind, pure, Except.pure] + +#print axioms step_call_skipInit_address + +/-- A resolved unaligned transfer executes its checked stack and memory action at the call PC. -/ +theorem step_call_typedTransfer {method : Extraction.MethodRef} {prefixes : CallPrefixes} + {kind : CIL.TransferKind} {store : Bool} (link : body.linkAt pc = none) + (found : Extraction.runtimeModel body.environment .call method = + some (.typedTransfer kind store)) (direct : prefixes.tail = false) (stack : List Value) : + step body (.call method prefixes) pc args frame stack memory = + typedTransferCall kind store pc frame stack memory := by + rw [step_call_model (link := link) (found := found) (direct := direct)] + by_cases observed : unspecifiedOperands (if store then 2 else 1) stack = true <;> + simp [modelStep, observedCall, runModel, CIL.RuntimeModel.observerCount, observed, + typedTransferCall, observedTransfer] <;> rfl + +#print axioms step_call_typedTransfer + +/-- A call whose pc links `callee` directly enters it, as `callStep` says; the model it would + run without a link is never read. Stepping selects it once the link fact is known. -/ +theorem step_call_direct {method : Extraction.MethodRef} {prefixes : CallPrefixes} + {callee : Nat} (link : body.linkAt pc = some (.direct callee)) (stack : List Value) : + step body (.call method prefixes) pc args frame stack memory = + if unspecifiedOperands method.argumentCount stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else callStep (some (.direct callee)) method.argumentCount prefixes.tail + (fun _ => .error unlinkedCall) frame stack memory := by + rw [step_call_eq, link] + rfl + +#print axioms step_call_direct + +/-- A call without a link whose method has no runtime model faults. -/ +@[cil_step] theorem modelStep_none (stack : List Value) : + modelStep none body pc frame stack memory = .error unlinkedCall := rfl + +#print axioms modelStep_none + +/-- The `System.Object` constructor consumes its receiver and changes nothing else, as `pop` + does. -/ +@[cil_step] theorem modelStep_objectConstructor (receiver : Value) (rest : List Value) : + modelStep (some .objectConstructor) body pc frame (receiver :: rest) memory = + .ok (.next (pc + 1) rest frame memory) := rfl + +#print axioms modelStep_objectConstructor + +/-- `localloc` allocates frame storage, zeroed as the body's declared localsinit flag says. -/ +@[cil_step] theorem step_localloc_eq (stack : List Value) : + step body .localloc pc args frame stack memory = + if unspecifiedOperands 1 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else localAllocation body.declared.initLocals pc frame stack memory := by + simp only [step, observerCount] + rfl + +#print axioms step_localloc_eq + +/-- A typed `ldelem` loads by the shape the method's layouts give its element type. -/ +@[cil_step] theorem step_ldelem_any_eq (element : CIL.Extraction.TypeExpr) (stack : List Value) : + step body (.ldelem_any element) pc args frame stack memory = + if unspecifiedOperands 2 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else do + let (memory, values) ← + typedElementInstruction (body.elementShape element) false stack memory + return .next (pc + 1) values frame memory := by + simp only [step, observerCount] + rfl + +#print axioms step_ldelem_any_eq + +/-- A value element's typed access is a snapshot transfer for a struct or vector element and + otherwise a numeric load or store of the element's width. -/ +@[cil_step] theorem typedElementInstruction_value (element : CIL.ArrayElement) (store : Bool) + (stack : List Value) : + typedElementInstruction (.value element) store stack memory = + if element.composite then arrayTransferInstruction element store stack memory + else if store then arrayStore element.width stack memory + else arrayLoad element.width element.signed stack memory := rfl + +-- A stepped typed element access shows its element's width and sign as literals. +attribute [cil_step] CIL.ArrayElement.width CIL.ArrayElement.signed CIL.ArrayElement.composite + CIL.EnumStorage.width CIL.EnumStorage.signed + +/-- A typed `stelem` stores by the shape the method's layouts give its element type. -/ +@[cil_step] theorem step_stelem_any_eq (element : CIL.Extraction.TypeExpr) (stack : List Value) : + step body (.stelem_any element) pc args frame stack memory = + if unspecifiedOperands 3 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else do + let (memory, values) ← + typedElementInstruction (body.elementShape element) true stack memory + return .next (pc + 1) values frame memory := by + simp only [step, observerCount] + rfl + +#print axioms step_stelem_any_eq + +/-- `ldelema` addresses an element by the shape the method's layouts give its element type. -/ +@[cil_step] theorem step_ldelema_eq (element : CIL.Extraction.TypeExpr) (readOnly : Bool) + (stack : List Value) : + step body (.ldelema element readOnly) pc args frame stack memory = + if unspecifiedOperands 2 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else do + let (memory, values) ← + elementAddressInstruction (body.elementShape element) readOnly stack memory + return .next (pc + 1) values frame memory := by + simp only [step, observerCount] + rfl + +#print axioms step_ldelema_eq + +/-- A value element's address is the reference `arrayAddress` gives, read-only after the + prefix. -/ +@[cil_step] theorem elementAddressInstruction_value (element : CIL.ArrayElement) + (readOnly : Bool) (stack : List Value) : + elementAddressInstruction (.value element) readOnly stack memory = + arrayAddress element (!readOnly) stack memory := rfl + +/-- `ldfld` reads the field as the body resolves it, behind the unspecified-bits guard. + Stepping uses this form only while the field's shape is unknown, and the equations of + `ObjectSteps` once it is known. -/ +theorem step_ldfld_eq (field : CIL.Extraction.FieldRef) (stack : List Value) : + step body (.ldfld field) pc args frame stack memory = + if unspecifiedOperands 1 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else do + let (memory, stack) ← fieldLoad body field stack memory + return .next (pc + 1) stack frame memory := by + simp only [step, observerCount] + rfl + +#print axioms step_ldfld_eq + +/-- `stfld` writes the field as the body resolves it, behind the unspecified-bits guard. + Stepping uses this form only while the field's shape is unknown. -/ +theorem step_stfld_eq (field : CIL.Extraction.FieldRef) (stack : List Value) : + step body (.stfld field) pc args frame stack memory = + if unspecifiedOperands 2 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else do + let (memory, stack) ← fieldStore body field stack memory + return .next (pc + 1) stack frame memory := by + simp only [step, observerCount] + rfl + +#print axioms step_stfld_eq + +/-- `ldflda` forms the address the body resolves for the field, behind the guard. Stepping + uses this form only while the field's shape is unknown. -/ +theorem step_ldflda_eq (field : CIL.Extraction.FieldRef) (stack : List Value) : + step body (.ldflda field) pc args frame stack memory = + if unspecifiedOperands 1 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else do + let (memory, stack) ← fieldReference body field stack memory + return .next (pc + 1) stack frame memory := by + simp only [step, observerCount] + rfl + +#print axioms step_ldflda_eq + +attribute [cil_step] fieldLoad fieldStore fieldReference + +attribute [cil_step] localAllocation + +/-- Loading a function pointer pushes the authenticated identity and signature on any stack. -/ +@[cil_step] theorem step_ldftn_eq (identity : CIL.ManagedIdentity) (signature : CIL.ManagedSignature) + (stack : List Value) : + step body (.ldftn identity signature) pc args frame stack memory = + .ok (.next (pc + 1) (.methodPointer ⟨signature, some identity⟩ :: stack) frame memory) := id rfl + +#print axioms step_ldftn_eq + +/-- An indirect call through a method pointer enters the body `indirectTarget` selects from the + catalog its link gives; a fact for that selection, which the kernel checks, decides it without + comparing identities here. -/ +@[cil_step] theorem step_managed_call_eq (signature : CIL.ManagedSignature) + (tail : Bool) (pointer : CIL.MethodPointer) + (values : List Value) : + step body (.calli signature tail) pc args frame + (.methodPointer pointer :: values) memory = + if unspecifiedOperands (signature.parameters.length + 1) (.methodPointer pointer :: values) + then .error (.unsupported "Instruction observes unspecified vector bits") + else do + let some (.indirect catalog) := body.linkAt pc | .error unlinkedCall + let some method := indirectTarget signature catalog pointer | .error .invalidState + let count := signature.parameters.length + if (if tail then values.length != count else values.length < count) then + .error .invalidState else pure () + let arguments ← (values.take count).reverse.mapM (callArgument frame memory) + if tail then return .tailCall method arguments memory + else return .call method arguments (values.drop count) memory := by + simp only [step, observerCount, calliStep] + rfl + +#print axioms step_managed_call_eq /-- A narrowed return of one i32 extends it to the declared width. -/ -@[cil_step] theorem step_return_narrow_eq (kind : CIL.NarrowKind) (bits : CIL.W32) : - step body (.ret (some kind)) pc args frame [.scalar (.i32 bits)] memory = - if body.returnsValue then .ok (.returned [.scalar (.i32 (kind.extend bits))] memory) +@[cil_step] theorem returnFrom_narrow_eq (kind : CIL.NarrowKind) (returnsValue : Bool) + (bits : CIL.W32) : + returnFrom (some kind) returnsValue [.scalar (.i32 bits)] memory = + if returnsValue then .ok (.returned [.scalar (.i32 (kind.extend bits))] memory) else .error .invalidState := rfl +#print axioms returnFrom_narrow_eq + -- Conditional branches on an i32 or i64 operand. -@[cil_step] theorem step_brzero_i32_eq (target : Nat) (value : CIL.W32) (rest : List Value) : - step body (.brzero target) pc args frame (.scalar (.i32 value) :: rest) memory = do +@[cil_step] theorem step_brfalse_i32_eq (target : Nat) (value : CIL.W32) (rest : List Value) : + step body (.brfalse target) pc args frame (.scalar (.i32 value) :: rest) memory = do let some taken := CIL.truth (.i32 value) | .error .invalidState - return FrameAction.next (if taken then pc + 1 else target) rest frame memory := rfl + return FrameAction.next (if taken then pc + 1 else target) rest frame memory := id rfl -@[cil_step] theorem step_brzero_i64_eq (target : Nat) (value : CIL.W64) (rest : List Value) : - step body (.brzero target) pc args frame (.scalar (.i64 value) :: rest) memory = do +@[cil_step] theorem step_brfalse_i64_eq (target : Nat) (value : CIL.W64) (rest : List Value) : + step body (.brfalse target) pc args frame (.scalar (.i64 value) :: rest) memory = do let some taken := CIL.truth (.i64 value) | .error .invalidState - return FrameAction.next (if taken then pc + 1 else target) rest frame memory := rfl + return FrameAction.next (if taken then pc + 1 else target) rest frame memory := id rfl -@[cil_step] theorem step_brnonzero_i32_eq (target : Nat) (value : CIL.W32) (rest : List Value) : - step body (.brnonzero target) pc args frame (.scalar (.i32 value) :: rest) memory = do +@[cil_step] theorem step_brtrue_i32_eq (target : Nat) (value : CIL.W32) (rest : List Value) : + step body (.brtrue target) pc args frame (.scalar (.i32 value) :: rest) memory = do let some taken := CIL.truth (.i32 value) | .error .invalidState - return FrameAction.next (if taken then target else pc + 1) rest frame memory := rfl + return FrameAction.next (if taken then target else pc + 1) rest frame memory := id rfl -@[cil_step] theorem step_brnonzero_i64_eq (target : Nat) (value : CIL.W64) (rest : List Value) : - step body (.brnonzero target) pc args frame (.scalar (.i64 value) :: rest) memory = do +@[cil_step] theorem step_brtrue_i64_eq (target : Nat) (value : CIL.W64) (rest : List Value) : + step body (.brtrue target) pc args frame (.scalar (.i64 value) :: rest) memory = do let some taken := CIL.truth (.i64 value) | .error .invalidState - return FrameAction.next (if taken then target else pc + 1) rest frame memory := rfl + return FrameAction.next (if taken then target else pc + 1) rest frame memory := id rfl /-- The dispatcher arm for an op that only touches the stack and memory. -/ -def instructionStep (op : CIL.Op) (pc : Nat) (frame : Frame) (stack : List Value) +def instructionStep (op : CIL.Instruction) (pc : Nat) (frame : Frame) (stack : List Value) (memory : Memory) : ExecutionResult FrameAction := do let (memory, values) ← instruction op stack memory return .next (pc + 1) values frame memory --- One equation per op, so simp selects it by constructor without a side condition. -@[cil_step] theorem step_loadAggregate_eq {size} (stack : List Value) : - step body (.loadAggregate size) pc args frame stack memory = - instructionStep (.loadAggregate size) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_copyObject_eq {size} (stack : List Value) : - step body (.copyObject size) pc args frame stack memory = - instructionStep (.copyObject size) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 2 stack <;> rfl -@[cil_step] theorem step_storeAggregate_eq {size} (stack : List Value) : - step body (.storeAggregate size) pc args frame stack memory = - instructionStep (.storeAggregate size) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 2 stack <;> rfl -@[cil_step] theorem step_initAggregate_eq {size} (stack : List Value) : - step body (.initAggregate size) pc args frame stack memory = - instructionStep (.initAggregate size) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_loadScalar_eq {width signed} (stack : List Value) : - step body (.loadScalar width signed) pc args frame stack memory = - instructionStep (.loadScalar width signed) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_storeScalar_eq {width} (stack : List Value) : - step body (.storeScalar width) pc args frame stack memory = - instructionStep (.storeScalar width) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 2 stack <;> rfl -@[cil_step] theorem step_fieldAt_eq {offset width signed} (stack : List Value) : - step body (.fieldAt offset width signed) pc args frame stack memory = - instructionStep (.fieldAt offset width signed) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_fieldAddress_eq {offset} (stack : List Value) : - step body (.fieldAddress offset) pc args frame stack memory = - instructionStep (.fieldAddress offset) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_setFieldAt_eq {offset width} (stack : List Value) : - step body (.setFieldAt offset width) pc args frame stack memory = - instructionStep (.setFieldAt offset width) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 2 stack <;> rfl -@[cil_step] theorem step_aggregateFieldAt_eq {offset size} (stack : List Value) : - step body (.aggregateFieldAt offset size) pc args frame stack memory = - instructionStep (.aggregateFieldAt offset size) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_setAggregateFieldAt_eq {offset size} (stack : List Value) : - step body (.setAggregateFieldAt offset size) pc args frame stack memory = - instructionStep (.setAggregateFieldAt offset size) pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 2 stack <;> rfl -@[cil_step] theorem step_lane_eq {index} (stack : List Value) : - step body (.lane index) pc args frame stack memory = - instructionStep (.lane index) pc frame stack memory := by +/-- The span shape resolves exactly the span loads that forward their operand. -/ +private theorem valueShape_span_test (type : Extraction.TypeExpr) : + Extraction.spanLocal type = (body.valueShape type matches .span) := by + generalize resolved : body.valueShape type = shape + unfold Method.valueShape LayoutEnvironment.valueShape valueShapeAt at resolved + cases span : Extraction.spanLocal type <;> simp only [span, Bool.false_eq_true, ite_false, + ite_true] at resolved + all_goals repeat' split at resolved + all_goals subst shape <;> rfl +#print axioms valueShape_span_test + +/-- The decoded ldobj of primitive storage has the full result of its memory rule. -/ +@[cil_step] theorem step_ldobj_primitive {type : Extraction.TypeExpr} {width : Nat} {signed : Bool} + (shape : body.valueShape type = .primitive width signed) (stack : List Value) : + step body (.ldobj type) pc args frame stack memory = + typedTransferCall (.scalar width signed) false pc frame stack memory := by + by_cases observed : unspecifiedOperands 1 stack = true <;> + simp [step, observerCount, valueShape_span_test body type, shape, viewOperands, transferObject, + typedTransferCall, observedTransfer, observed, Bind.bind, Except.bind, Pure.pure, Except.pure] +#print axioms step_ldobj_primitive + +/-- The decoded ldobj of aggregate storage has the full result of its memory rule. -/ +@[cil_step] theorem step_ldobj_aggregate {type : Extraction.TypeExpr} {size : Nat} + (shape : body.valueShape type = .aggregate size) (stack : List Value) : + step body (.ldobj type) pc args frame stack memory = + typedTransferCall (.snapshot (.aggregate size)) false pc frame stack memory := by + by_cases observed : unspecifiedOperands 1 stack = true <;> + simp [step, observerCount, valueShape_span_test body type, shape, viewOperands, transferObject, + typedTransferCall, observedTransfer, observed, Bind.bind, Except.bind, Pure.pure, Except.pure] +#print axioms step_ldobj_aggregate + +/-- The decoded ldobj of vector storage has the full result of its memory rule. -/ +@[cil_step] theorem step_ldobj_vector16 {type : Extraction.TypeExpr} + (shape : body.valueShape type = .vector 16) (stack : List Value) : + step body (.ldobj type) pc args frame stack memory = + modelStep (some (.memory .load128)) body pc frame stack memory := by + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject, + pureArity, staticInstruction] + first | rfl | (cases unspecifiedOperands 1 stack <;> rfl) +#print axioms step_ldobj_vector16 + +/-- The decoded ldobj of vector storage has the full result of its memory rule. -/ +@[cil_step] theorem step_ldobj_vector32 {type : Extraction.TypeExpr} + (shape : body.valueShape type = .vector 32) (stack : List Value) : + step body (.ldobj type) pc args frame stack memory = + modelStep (some (.memory .load256)) body pc frame stack memory := by + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject, + pureArity, staticInstruction] + first | rfl | (cases unspecifiedOperands 1 stack <;> rfl) +#print axioms step_ldobj_vector32 + +/-- The decoded ldobj of vector storage has the full result of its memory rule. -/ +@[cil_step] theorem step_ldobj_vector64 {type : Extraction.TypeExpr} + (shape : body.valueShape type = .vector 64) (stack : List Value) : + step body (.ldobj type) pc args frame stack memory = + modelStep (some (.memory .load512)) body pc frame stack memory := by + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject, + pureArity, staticInstruction] + first | rfl | (cases unspecifiedOperands 1 stack <;> rfl) +#print axioms step_ldobj_vector64 + +/-- A decoded ldobj of span storage leaves a span view on the stack in place. -/ +@[cil_step] theorem step_ldobj_span {type : Extraction.TypeExpr} + (shape : body.valueShape type = .span) (view : SpanView) (rest : List Value) : + step body (.ldobj type) pc args frame (.bufferSpan view :: rest) memory = + step body .nop pc args frame (.bufferSpan view :: rest) memory := by + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject, + pureArity, staticInstruction] + rfl +#print axioms step_ldobj_span + +/-- A decoded ldobj of span storage leaves a span value formed from a reference in place. -/ +@[cil_step] theorem step_ldobj_span_value {type : Extraction.TypeExpr} + (shape : body.valueShape type = .span) (reference : ManagedReference) (length : Nat) + (rest : List Value) : + step body (.ldobj type) pc args frame (.span reference length :: rest) memory = + step body .nop pc args frame (.span reference length :: rest) memory := by + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject, + pureArity, staticInstruction] + rfl +#print axioms step_ldobj_span_value + +/-- A decoded ldobj of span storage faults on a null reference, as a load through it does. -/ +theorem step_ldobj_span_null {type : Extraction.TypeExpr} + (shape : body.valueShape type = .span) {operand : Value} + (null : operand = .reference .null ∨ operand = .readOnlyReference .null) (rest : List Value) : + step body (.ldobj type) pc args frame (operand :: rest) memory = + .error (.memory .nullDereference .null 0) := by + rcases null with rfl | rfl <;> + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject, + pureArity, staticInstruction] <;> rfl +#print axioms step_ldobj_span_null + +/-- A decoded ldobj of span storage faults on a plain address, which never names a span view. -/ +theorem step_ldobj_span_address {type : Extraction.TypeExpr} + (shape : body.valueShape type = .span) {operand : Value} {reference : Reference} + (address : operand = .reference (.address reference) ∨ + operand = .readOnlyReference (.address reference)) (rest : List Value) : + step body (.ldobj type) pc args frame (operand :: rest) memory = .error .invalidState := by + rcases address with rfl | rfl <;> + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject, + pureArity, staticInstruction] <;> rfl +#print axioms step_ldobj_span_address + +/-- A home a span operation reads stands for the view its slot holds. -/ +theorem spanView_home_view {frame : Frame} {memory : Memory} {slot : Nat} {result : Value} + (viewed : spanView frame memory (.spanHome slot) = .ok result) : + ∃ view, result = .bufferSpan view := by + simp only [spanView] at viewed + split at viewed + · simp only [loadLocal, Bind.bind, Except.bind, Pure.pure, Except.pure] at viewed + repeat' (first | (cases viewed; exact ⟨_, rfl⟩) | contradiction | split at viewed) + · cases viewed +#print axioms spanView_home_view + +/-- A decoded ldobj of span storage reads a home as the view its slot holds when the load runs. + A home whose slot holds no view faults. -/ +@[cil_step high] theorem step_ldobj_span_home {type : Extraction.TypeExpr} + (shape : body.valueShape type = .span) (slot : Nat) (rest : List Value) : + step body (.ldobj type) pc args frame (.spanHome slot :: rest) memory = + match spanView frame memory (.spanHome slot) with + | .ok (.bufferSpan view) => step body .nop pc args frame (.bufferSpan view :: rest) memory + | .ok _ => .error .invalidState + | .error fault => .error fault := by + cases viewed : spanView frame memory (.spanHome slot) with + | error fault => + simp only [step, observerCount, valueShape_span_test body type, shape, viewOperands, viewed, + unspecifiedOperands, List.take, List.any, Bool.false_eq_true, ite_false, ite_true, + Bind.bind, Except.bind] + | ok result => + obtain ⟨view, rfl⟩ := spanView_home_view viewed + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject, + viewOperands, viewed, unspecifiedOperands, List.take, List.any, Bool.false_eq_true, + ite_false, ite_true, Bind.bind, Except.bind, pureArity] + rfl +#print axioms step_ldobj_span_home + +/-- A decoded ldobj of span storage forwards a span view or span value, reads a home as the view + its slot holds, faults on a null reference as a null dereference, and raises an invalid state + on any other stack. -/ +theorem step_ldobj_span_result {type : Extraction.TypeExpr} + (shape : body.valueShape type = .span) (stack : List Value) : + step body (.ldobj type) pc args frame stack memory = match stack with + | .bufferSpan _ :: _ | .span .. :: _ => step body .nop pc args frame stack memory + | .spanHome slot :: rest => match spanView frame memory (.spanHome slot) with + | .ok (.bufferSpan view) => step body .nop pc args frame (.bufferSpan view :: rest) memory + | .ok _ => .error .invalidState + | .error fault => .error fault + | .reference .null :: _ | .readOnlyReference .null :: _ => + .error (.memory .nullDereference .null 0) + | _ => .error .invalidState := by + cases stack with + | nil => + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject] + rfl + | cons head rest => + cases head + case spanHome slot => + exact step_ldobj_span_home (body := body) (pc := pc) (args := args) (frame := frame) + (memory := memory) shape slot rest + all_goals + simp only [step, observerCount, valueShape_span_test body type, shape, transferObject, + pureArity] + first | rfl | (rename_i reference; cases reference <;> rfl) +#print axioms step_ldobj_span_result + +/-- The decoded stobj of primitive storage has the full result of its memory rule. -/ +@[cil_step] theorem step_stobj_primitive {type : Extraction.TypeExpr} {width : Nat} {signed : Bool} + (shape : body.valueShape type = .primitive width signed) (stack : List Value) : + step body (.stobj type) pc args frame stack memory = + typedTransferCall (.scalar width false) true pc frame stack memory := by + by_cases observed : unspecifiedOperands 2 stack = true <;> + simp [step, observerCount, shape, transferObject, typedTransferCall, observedTransfer, + observed] <;> rfl +#print axioms step_stobj_primitive + +/-- The decoded stobj of aggregate storage has the full result of its memory rule. -/ +@[cil_step] theorem step_stobj_aggregate {type : Extraction.TypeExpr} {size : Nat} + (shape : body.valueShape type = .aggregate size) (stack : List Value) : + step body (.stobj type) pc args frame stack memory = + typedTransferCall (.snapshot (.aggregate size)) true pc frame stack memory := by + by_cases observed : unspecifiedOperands 2 stack = true <;> + simp [step, observerCount, shape, transferObject, typedTransferCall, observedTransfer, + observed] <;> rfl +#print axioms step_stobj_aggregate + +/-- The decoded stobj of vector storage has the full result of its memory rule. -/ +@[cil_step] theorem step_stobj_vector16 {type : Extraction.TypeExpr} + (shape : body.valueShape type = .vector 16) (stack : List Value) : + step body (.stobj type) pc args frame stack memory = + modelStep (some (.memory .store128)) body pc frame stack memory := by + simp only [step, observerCount, shape, transferObject, + pureArity, staticInstruction] + first | rfl | (cases unspecifiedOperands 2 stack <;> rfl) +#print axioms step_stobj_vector16 + +/-- The decoded stobj of vector storage has the full result of its memory rule. -/ +@[cil_step] theorem step_stobj_vector32 {type : Extraction.TypeExpr} + (shape : body.valueShape type = .vector 32) (stack : List Value) : + step body (.stobj type) pc args frame stack memory = + modelStep (some (.memory .store256)) body pc frame stack memory := by + simp only [step, observerCount, shape, transferObject, + pureArity, staticInstruction] + first | rfl | (cases unspecifiedOperands 2 stack <;> rfl) +#print axioms step_stobj_vector32 + +/-- The decoded stobj of vector storage has the full result of its memory rule. -/ +@[cil_step] theorem step_stobj_vector64 {type : Extraction.TypeExpr} + (shape : body.valueShape type = .vector 64) (stack : List Value) : + step body (.stobj type) pc args frame stack memory = + modelStep (some (.memory .store512)) body pc frame stack memory := by + simp only [step, observerCount, shape, transferObject, + pureArity, staticInstruction] + first | rfl | (cases unspecifiedOperands 2 stack <;> rfl) +#print axioms step_stobj_vector64 + +/-- initobj checks its operands before accessing the resolved number of bytes. -/ +@[cil_step] theorem step_initobj_eq {type : Extraction.TypeExpr} {size : Nat} + (width : (body.valueShape type).size? = some size) (stack : List Value) : + step body (.initobj type) pc args frame stack memory = + if unspecifiedOperands 1 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else do + let (updated, values) ← initializeBytes size stack memory + return .next (pc + 1) values frame updated := by + simp only [step, observerCount, width] +#print axioms step_initobj_eq + +/-- cpobj checks its operands before accessing the resolved number of bytes. -/ +@[cil_step] theorem step_cpobj_eq {type : Extraction.TypeExpr} {size : Nat} + (width : (body.valueShape type).size? = some size) (stack : List Value) : + step body (.cpobj type) pc args frame stack memory = + if unspecifiedOperands 2 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else do + let (updated, values) ← copyInitializedBytes size stack memory + return .next (pc + 1) values frame updated := by + simp only [step, observerCount, width] +#print axioms step_cpobj_eq + +/-- The size of an admitted type pushes its resolved byte width as an i32. -/ +@[cil_step] theorem step_sizeof_eq {type : Extraction.TypeExpr} {size : Nat} + (width : (body.valueShape type).size? = some size) (stack : List Value) : + step body (.sizeof type) pc args frame stack memory = + step body (.ldc_i4 (BitVec.ofNat 32 size)) pc args frame stack memory := by + change (body.environment.valueShape type).size? = some size at width + simp only [step, observerCount, Extraction.CallLayouts.valueShape_eq, width, unspecifiedOperands, + List.take_zero, List.any_nil, pureArity, instruction] + rfl +#print axioms step_sizeof_eq + +/-- A primitive or vector struct field reads the offset, width and sign of its checked shape. -/ +theorem step_ldfld_value_eq (field : CIL.Extraction.FieldRef) (offset width : Nat) + (signed : Bool) (shape : body.fieldShape field = .value offset width signed) + (stack : List Value) : + step body (.ldfld field) pc args frame stack memory = (do + let (memory, stack) ← loadValueField offset width signed stack memory + return .next (pc + 1) stack frame memory) := by + rw [step_ldfld_eq] + simp only [fieldLoad, shape] + unfold loadValueField + split <;> rfl +#print axioms step_ldfld_value_eq + +/-- A nested struct field is read from its checked offset with its initialization flags. -/ +theorem step_ldfld_aggregate_eq (field : CIL.Extraction.FieldRef) (offset size : Nat) + (shape : body.fieldShape field = .aggregate offset size) (stack : List Value) : + step body (.ldfld field) pc args frame stack memory = (do + let (memory, stack) ← loadAggregateField offset size stack memory + return .next (pc + 1) stack frame memory) := by + rw [step_ldfld_eq] + simp only [fieldLoad, shape] + unfold loadAggregateField + split <;> rfl +#print axioms step_ldfld_aggregate_eq + +/-- A primitive or vector struct field writes the offset and width of its checked shape. -/ +theorem step_stfld_value_eq (field : CIL.Extraction.FieldRef) (offset width : Nat) + (signed : Bool) (shape : body.fieldShape field = .value offset width signed) + (stack : List Value) : + step body (.stfld field) pc args frame stack memory = (do + let (memory, stack) ← storeValueField offset width stack memory + return .next (pc + 1) stack frame memory) := by + rw [step_stfld_eq] + simp only [fieldStore, shape] + unfold storeValueField + split <;> rfl +#print axioms step_stfld_value_eq + +/-- A nested struct store writes at the offset and width from the checked field shape. -/ +theorem step_stfld_aggregate_eq (field : CIL.Extraction.FieldRef) (offset size : Nat) + (shape : body.fieldShape field = .aggregate offset size) (stack : List Value) : + step body (.stfld field) pc args frame stack memory = (do + let (memory, stack) ← storeAggregateField offset size stack memory + return .next (pc + 1) stack frame memory) := by + rw [step_stfld_eq] + simp only [fieldStore, shape] + unfold storeAggregateField + split <;> rfl +#print axioms step_stfld_aggregate_eq + +/-- A struct ldflda computes its checked field offset and preserves the receiver's writability. -/ +theorem step_ldflda_struct_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (addressed : (body.fieldShape field).structOffset? = some offset) (stack : List Value) : + step body (.ldflda field) pc args frame stack memory = (do + let (memory, stack) ← structFieldAddress offset stack memory + return .next (pc + 1) stack frame memory) := by + have reached : fieldReference body field stack memory = structFieldAddress offset stack memory := by + unfold fieldReference + split <;> simp_all [CIL.FieldShape.structOffset?] + rw [step_ldflda_eq, reached] + unfold structFieldAddress + split <;> rfl +#print axioms step_ldflda_struct_eq + +/-- A struct field address keeps a writable reference writable. -/ +theorem step_ldflda_reference_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (addressed : (body.fieldShape field).structOffset? = some offset) (reference : Reference) + (rest : List Value) : + step body (.ldflda field) pc args frame (.reference (.address reference) :: rest) + memory = do + let address ← checkedAt (.address reference) 0 + (add memory reference 1 (BitVec.ofNat 64 offset)) + return .next (pc + 1) (.reference (.address address) :: rest) frame memory := by + rw [step_ldflda_struct_eq _ _ _ _ _ field offset addressed] + simp only [structFieldAddress, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + Bool.false_or, Bool.false_eq_true, ite_false, bind_assoc, pure_bind] +#print axioms step_ldflda_reference_eq + +/-- A struct field address through a readonly receiver stays readonly. -/ +theorem step_ldflda_readOnlyReference_eq (field : CIL.Extraction.FieldRef) (offset : Nat) + (addressed : (body.fieldShape field).structOffset? = some offset) (reference : Reference) + (rest : List Value) : + step body (.ldflda field) pc args frame (.readOnlyReference (.address reference) :: rest) + memory = do + let address ← checkedAt (.address reference) 0 + (add memory reference 1 (BitVec.ofNat 64 offset)) + return .next (pc + 1) (.readOnlyReference (.address address) :: rest) frame memory := by + rw [step_ldflda_struct_eq _ _ _ _ _ field offset addressed] + simp only [structFieldAddress, unspecifiedOperands, List.take, List.any, Value.hasUnspecified, + Bool.false_or, Bool.false_eq_true, ite_false, bind_assoc, pure_bind] +#print axioms step_ldflda_readOnlyReference_eq + + +@[cil_step] theorem step_ldc_i4_eq {value} (stack : List Value) : + step body (.ldc_i4 value) pc args frame stack memory = + instructionStep (.ldc_i4 value) pc frame stack memory := id rfl +@[cil_step] theorem step_ldc_i8_eq {value} (stack : List Value) : + step body (.ldc_i8 value) pc args frame stack memory = + instructionStep (.ldc_i8 value) pc frame stack memory := id rfl +@[cil_step] theorem step_ldind_eq {kind} (stack : List Value) : + step body (.ldind kind) pc args frame stack memory = + instructionStep (.ldind kind) pc frame stack memory := by + by_cases observed : unspecifiedOperands 1 stack = true <;> + simp only [step, instructionStep, instruction, observerCount, pureArity, observed, ↓reduceIte, + Bool.false_eq_true] <;> rfl +@[cil_step] theorem step_stind_eq {kind} (stack : List Value) : + step body (.stind kind) pc args frame stack memory = + instructionStep (.stind kind) pc frame stack memory := by + by_cases observed : unspecifiedOperands 2 stack = true <;> + simp only [step, instructionStep, instruction, observerCount, pureArity, observed, ↓reduceIte, + Bool.false_eq_true] <;> rfl +@[cil_step] theorem step_ldlen_eq (stack : List Value) : + step body .ldlen pc args frame stack memory = + instructionStep .ldlen pc frame stack memory := by simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_laneAddress_eq {index} (stack : List Value) : - step body (.laneAddress index) pc args frame stack memory = - instructionStep (.laneAddress index) pc frame stack memory := by + by_cases observed : unspecifiedOperands 1 stack = true <;> + simp only [observed, ↓reduceIte, Bool.false_eq_true] <;> rfl +@[cil_step] theorem step_ldelem_eq {kind} (stack : List Value) : + step body (.ldelem kind) pc args frame stack memory = + instructionStep (.ldelem kind) pc frame stack memory := by simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_setLane_eq {index} (stack : List Value) : - step body (.setLane index) pc args frame stack memory = - instructionStep (.setLane index) pc frame stack memory := by + by_cases observed : unspecifiedOperands 2 stack = true <;> + simp only [observed, ↓reduceIte, Bool.false_eq_true] <;> rfl +@[cil_step] theorem step_ldelem_ref_eq (stack : List Value) : + step body .ldelem_ref pc args frame stack memory = + instructionStep .ldelem_ref pc frame stack memory := by simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 2 stack <;> rfl -@[cil_step] theorem step_const32_eq {word} (stack : List Value) : - step body (.const32 word) pc args frame stack memory = - instructionStep (.const32 word) pc frame stack memory := rfl -@[cil_step] theorem step_const64_eq {word} (stack : List Value) : - step body (.const64 word) pc args frame stack memory = - instructionStep (.const64 word) pc frame stack memory := rfl -@[cil_step] theorem step_load64_eq (stack : List Value) : - step body .load64 pc args frame stack memory = - instructionStep .load64 pc frame stack memory := by + by_cases observed : unspecifiedOperands 2 stack = true <;> + simp only [observed, ↓reduceIte, Bool.false_eq_true] <;> rfl +@[cil_step] theorem step_stelem_eq {kind} (stack : List Value) : + step body (.stelem kind) pc args frame stack memory = + instructionStep (.stelem kind) pc frame stack memory := by simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_store64_eq (stack : List Value) : - step body .store64 pc args frame stack memory = - instructionStep .store64 pc frame stack memory := by + by_cases observed : unspecifiedOperands 3 stack = true <;> + simp only [observed, ↓reduceIte, Bool.false_eq_true] <;> rfl +@[cil_step] theorem step_stelem_ref_eq (stack : List Value) : + step body .stelem_ref pc args frame stack memory = + instructionStep .stelem_ref pc frame stack memory := by simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 2 stack <;> rfl + by_cases observed : unspecifiedOperands 3 stack = true <;> + simp only [observed, ↓reduceIte, Bool.false_eq_true] <;> rfl +-- A stepped `ldind` or `stind` of a known kind shows its width and sign as literals. +attribute [cil_step, cil_instruction_step] CIL.NumericKind.width CIL.NumericKind.signed @[cil_step] theorem step_dup_eq (stack : List Value) : step body .dup pc args frame stack memory = - instructionStep .dup pc frame stack memory := rfl + instructionStep .dup pc frame stack memory := id rfl @[cil_step] theorem step_pop_eq (stack : List Value) : step body .pop pc args frame stack memory = - instructionStep .pop pc frame stack memory := rfl -@[cil_step] theorem step_skipInit_eq (stack : List Value) : - step body .skipInit pc args frame stack memory = - instructionStep .skipInit pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl -@[cil_step] theorem step_asRef_eq (stack : List Value) : - step body .asRef pc args frame stack memory = - instructionStep .asRef pc frame stack memory := by - simp only [step, instructionStep, instruction, observerCount, pureArity] - cases observed : unspecifiedOperands 1 stack <;> rfl - + instructionStep .pop pc frame stack memory := id rfl -- Locals go through the frame's slot for `index`. -@[cil_step] theorem step_local_eq (index : Nat) (narrow : Option CIL.NarrowKind) - (stack : List Value) : - step body (.local index narrow) pc args frame stack memory = do - let some slot := frame.locals[index]? | .error .invalidState - return FrameAction.next (pc + 1) ((← loadLocal memory slot) :: stack) frame memory := rfl - -@[cil_step] theorem step_vectorLocal_eq (index : Nat) (stack : List Value) : - step body (.vectorLocal index) pc args frame stack memory = do +@[cil_step] theorem step_ldloc_eq (index : Nat) (stack : List Value) : + step body (.ldloc index) pc args frame stack memory = do let some slot := frame.locals[index]? | .error .invalidState - return FrameAction.next (pc + 1) ((← loadLocal memory slot) :: stack) frame memory := rfl + return FrameAction.next (pc + 1) ((← loadLocal memory slot) :: stack) frame memory := id rfl -@[cil_step] theorem step_localAddr_eq (index : Nat) (stack : List Value) : - step body (.localAddr index) pc args frame stack memory = do +@[cil_step] theorem step_ldloca_eq (index : Nat) (stack : List Value) : + step body (.ldloca index) pc args frame stack memory = do let some slot := frame.locals[index]? | .error .invalidState - return FrameAction.next (pc + 1) ((← localAddress memory slot) :: stack) frame memory := rfl + return FrameAction.next (pc + 1) ((← localAddress memory index slot) :: stack) frame + memory := + id rfl -@[cil_step] theorem step_setLocal_eq (index : Nat) (narrow : Option CIL.NarrowKind) - (value : Value) (rest : List Value) : - step body (.setLocal index narrow) pc args frame (value :: rest) memory = do +@[cil_step] theorem step_stloc_eq (index : Nat) (value : Value) (rest : List Value) : + step body (.stloc index) pc args frame (value :: rest) memory = do let some slot := frame.locals[index]? | .error .invalidState let (slot, memory) ← storeLocal memory slot value return FrameAction.next (pc + 1) rest { frame with locals := frame.locals.set index slot } - memory := rfl + memory := id rfl -@[cil_step] theorem step_setVectorLocal_eq (index : Nat) (value : Value) (rest : List Value) : - step body (.setVectorLocal index) pc args frame (value :: rest) memory = do - let some slot := frame.locals[index]? | .error .invalidState - let (slot, memory) ← storeLocal memory slot value - return FrameAction.next (pc + 1) rest { frame with locals := frame.locals.set index slot } - memory := rfl +/-- A homed argument load steps as `ldloc` of its home slot. -/ +theorem step_ldarg_home_ldloc (index : Nat) (stack : List Value) (slot : Nat) + (access : body.argumentAccess index = .home slot) : + step body (.ldarg index) pc args frame stack memory = + step body (.ldloc slot) pc args frame stack memory := by + rw [step_ldarg_home_eq body pc args frame memory index stack slot access, step_ldloc_eq] -/-- A constructor checks its arguments and replaces its typed local at the call instruction. -/ -@[cil_step] theorem step_initializeSpanLocal_eq (index : Nat) - (constructor : CIL.SpanConstructor) (stack : List Value) : - step body (.initializeSpanLocal index constructor) pc args frame stack memory = - if unspecifiedOperands constructor.operation.argumentCount stack then - .error (.unsupported "Instruction observes unspecified vector bits") - else do - let some slot := frame.locals[index]? | .error .invalidState - let (memory, values) ← memoryInstruction constructor.operation stack memory - let value :: rest := values | .error .invalidState - let (slot, memory) ← storeLocal memory slot value - return FrameAction.next (pc + 1) rest - { frame with locals := frame.locals.set index slot } memory := by - simp only [step, observerCount, Bool.or_false] - rfl +#print axioms step_ldarg_home_ldloc -#print axioms step_initializeSpanLocal_eq +/-- A homed argument store steps as `stloc` of its home slot. -/ +theorem step_starg_home_stloc (index : Nat) (value : Value) (rest : List Value) (slot : Nat) + (access : body.argumentAccess index = .home slot) : + step body (.starg index) pc args frame (value :: rest) memory = + step body (.stloc slot) pc args frame (value :: rest) memory := by + rw [step_starg_home_eq body pc args frame memory index value rest slot access, step_stloc_eq] -/-- Memory operations fault on unspecified operand bits, then run `staticInstruction`. -/ -@[cil_step] theorem step_memory_eq (operation : CIL.MemoryOp) (stack : List Value) : - step body (.memory operation) pc args frame stack memory = - if unspecifiedOperands operation.argumentCount stack then - .error (.unsupported "Instruction observes unspecified vector bits") - else do - let (memory, values) ← staticInstruction body pc operation stack memory - return FrameAction.next (pc + 1) values frame memory := by - simp only [step, observerCount, Bool.or_false] +#print axioms step_starg_home_stloc -/-- An intrinsic must be available in the method's profile; its `argc` operands are taken - deepest first. -/ -@[cil_step] theorem step_intrinsic_eq (operation : CIL.Intrinsic) (argc : Nat) - (stack : List Value) : - step body (.intrinsic operation argc) pc args frame stack memory = - if !operation.available body.profile then - .error (.unsupported "Intrinsic unavailable in target profile") - else if stack.length < argc then .error .invalidState else do - let operands ← scalars (stack.take argc).reverse - let value ← checkedIntrinsic operation operands - return FrameAction.next (pc + 1) - ((← checkedValue memory (.scalar value)) :: stack.drop argc) frame memory := +/-- A homed argument's address steps as `ldloca` of its home slot. -/ +theorem step_ldarga_home_ldloca (index : Nat) (stack : List Value) (slot : Nat) + (access : body.argumentAccess index = .home slot) : + step body (.ldarga index) pc args frame stack memory = + step body (.ldloca slot) pc args frame stack memory := by + rw [step_ldarga_home_eq body pc args frame memory index stack slot access, step_ldloca_eq] + +#print axioms step_ldarga_home_ldloca + +/-- `ldsfld` pushes the value `staticLoad` reads through the descriptor bound to its pc. Stated + as the match a rewrite of the read leaves, so checking a stepped load never evaluates it. -/ +@[cil_step] theorem step_ldsfld_eq (field : CIL.Extraction.FieldRef) (stack : List Value) : + step body (.ldsfld field) pc args frame stack memory = + match staticLoad body pc memory with + | .ok result => .ok (.next (pc + 1) (result.2 :: stack) frame result.1) + | .error fault => .error fault := by + have unfolded : step body (.ldsfld field) pc args frame stack memory = (do + let (memory, value) ← staticLoad body pc memory + return FrameAction.next (pc + 1) (value :: stack) frame memory) := id rfl + rw [unfolded] + cases staticLoad body pc memory <;> rfl + +#print axioms step_ldsfld_eq + +/-- `ldsflda` pushes the reference to the storage of the descriptor bound to its pc. Stated as + the match a rewrite of the read leaves, so checking a stepped load never evaluates it. -/ +@[cil_step] theorem step_ldsflda_eq (field : CIL.Extraction.FieldRef) (stack : List Value) : + step body (.ldsflda field) pc args frame stack memory = + match staticAddress body pc memory with + | .ok result => .ok (.next (pc + 1) (result.2 :: stack) frame result.1) + | .error fault => .error fault := by + have unfolded : step body (.ldsflda field) pc args frame stack memory = (do + let (memory, value) ← staticAddress body pc memory + return FrameAction.next (pc + 1) (value :: stack) frame memory) := id rfl + rw [unfolded] + cases staticAddress body pc memory <;> rfl + +#print axioms step_ldsflda_eq + +/-- A type token pushes its scoped metadata handle without reading or allocating memory. -/ +@[cil_step] theorem step_ldtoken_type_eq (type : CIL.Extraction.TypeExpr) (stack : List Value) : + step body (.ldtoken (.type type)) pc args frame stack memory = + .ok (.next (pc + 1) (.typeHandle type :: stack) frame memory) := id rfl + +#print axioms step_ldtoken_type_eq + +/-- A field `ldtoken` pushes the same reference as `ldsflda` of that field. Stated as the match + a rewrite of the read leaves, so checking a stepped load never evaluates it. -/ +@[cil_step] theorem step_ldtoken_eq (field : CIL.Extraction.FieldRef) (stack : List Value) : + step body (.ldtoken (.field field)) pc args frame stack memory = + match staticAddress body pc memory with + | .ok result => .ok (.next (pc + 1) (result.2 :: stack) frame result.1) + | .error fault => .error fault := by + have unfolded : step body (.ldtoken (.field field)) pc args frame stack memory = (do + let (memory, value) ← staticAddress body pc memory + return FrameAction.next (pc + 1) (value :: stack) frame memory) := id rfl + rw [unfolded] + cases staticAddress body pc memory <;> rfl + +#print axioms step_ldtoken_eq + +/-- `stsfld` faults: no static field is writable. -/ +@[cil_step] theorem step_stsfld_eq (field : CIL.Extraction.FieldRef) (stack : List Value) : + step body (.stsfld field) pc args frame stack memory = + if unspecifiedOperands 1 stack then + .error (.unsupported "Instruction observes unspecified vector bits") + else .error (.unsupported "Static field store") := by + simp only [step, observerCount] rfl +#print axioms step_stsfld_eq + end InstructionEquations /-- Compose checked argument/frame setup with execution. Keeping this boundary @@ -298,12 +1161,12 @@ theorem invoke_of_run {program : CIL.Program} {fuel method : Nat} {body : CIL.Me (setup : enterFrame body args memory = .ok (frame, entered)) (executed : run program fuel method 0 args frame [] entered = .ok (final, result)) : invoke program fuel method args memory = .ok (final, result) := by - simp only [invoke, found, checked, setup, Except.mapError, Bind.bind, Except.bind] + simp only [invoke_eq, found, checked, setup, Except.mapError, Bind.bind, Except.bind] exact executed /-- A fetched step that continues at `target` costs one unit of fuel. -/ theorem run_next {program : CIL.Program} {fuel method pc target : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} {frame nextFrame : Frame} {memory updated : Memory} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) @@ -311,12 +1174,13 @@ theorem run_next {program : CIL.Program} {fuel method pc target : Nat} .ok (.next target values nextFrame updated)) : run program (fuel + 1) method pc args frame stack memory = run program fuel method target args nextFrame values updated := by - simp only [run, methodFound, instructionFound, stepped, Except.mapError, Bind.bind, Except.bind] + simp only [run_succ, methodFound, instructionFound, stepped, Except.mapError, Bind.bind, + Except.bind] #print axioms run_next /-- Return through frame expiration and the checked result validation. -/ theorem run_return {program : CIL.Program} {fuel method pc : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} {frame : Frame} {memory updated : Memory} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) @@ -324,20 +1188,21 @@ theorem run_return {program : CIL.Program} {fuel method pc : Nat} (checked : values.mapM (checkedValue (leaveFrame frame updated)) = .ok values) : run program (fuel + 1) method pc args frame stack memory = .ok (leaveFrame frame updated, values) := by - simp only [run, methodFound, instructionFound, stepped, checked, + simp only [run_succ, methodFound, instructionFound, stepped, checked, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] #print axioms run_return /-- A fetched step that faults ends the run with that fault, located at its method and pc. -/ theorem run_fault {program : CIL.Program} {fuel method pc : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack : List Value} {frame : Frame} {memory : Memory} {fault : ExecutionFault} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (failed : step body op pc args frame stack memory = .error fault) : run program (fuel + 1) method pc args frame stack memory = .error ⟨method, pc, fault⟩ := by - simp only [run, methodFound, instructionFound, failed, Except.mapError, Bind.bind, Except.bind] + simp only [run_succ, methodFound, instructionFound, failed, Except.mapError, Bind.bind, + Except.bind] #print axioms run_fault @@ -349,7 +1214,7 @@ theorem invoke_of_run_fault {program : CIL.Program} {fuel method : Nat} {body : (setup : enterFrame body args memory = .ok (frame, entered)) (executed : run program fuel method 0 args frame [] entered = .error fault) : invoke program fuel method args memory = .error fault := by - simp only [invoke, found, checked, setup, Except.mapError, Bind.bind, Except.bind] + simp only [invoke_eq, found, checked, setup, Except.mapError, Bind.bind, Except.bind] exact executed #print axioms invoke_of_run_fault @@ -357,7 +1222,7 @@ theorem invoke_of_run_fault {program : CIL.Program} {fuel method : Nat} {body : /-- Compose one checked fetched step with a terminating continuation without fixing the continuation's fuel in the caller proof. -/ theorem run_next_exists {program : CIL.Program} {method pc target : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} {frame nextFrame : Frame} {memory updated : Memory} (post : Memory → List Value → Prop) (methodFound : program[method]? = some body) @@ -378,7 +1243,7 @@ theorem run_next_exists {program : CIL.Program} {method pc target : Nat} /-- Some fuel runs to a successful return satisfying `post` when the fetched instruction returns checked values that satisfy it. -/ theorem run_return_exists {program : CIL.Program} {method pc : Nat} - {body : CIL.Method} {op : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {op : CIL.Instruction} {args stack values : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (methodFound : program[method]? = some body) (instructionFound : body.code[pc]? = some op) (stepped : step body op pc args frame stack memory = .ok (.returned values updated)) diff --git a/CIL/Safety/StepFlow.lean b/CIL/Safety/StepFlow.lean index 67ac180ec..a2cac346f 100644 --- a/CIL/Safety/StepFlow.lean +++ b/CIL/Safety/StepFlow.lean @@ -10,13 +10,23 @@ loop proofs can enumerate successors without stepping. namespace CIL -/-- Every method index an instruction calls or constructs with. -/ -def Op.callees : Op → List Nat - | .managed (.call _ catalog _) => catalog.map ManagedTarget.method - | .callVirtual targets _ => targets.map Prod.snd - | .call method _ | .callInstance method _ | .tailCall method _ | .newVector256 method _ - | .newAggregate method _ _ | .newObject method .. => [method] - | _ => [] +/-- Every method index the call site at `pc` may enter: the callees of its link, none when the + link table gives the pc no entry. -/ +def Method.calleesAt (body : Method) (pc : Nat) : List Nat := + ((body.linkAt pc).map Link.callees).getD [] + +/-- A method a call site may enter is a callee of an entry of the link table at that pc. -/ +theorem Method.calleesAt_mem {body : Method} {pc callee : Nat} + (member : callee ∈ body.calleesAt pc) : + ∃ entry ∈ body.links, entry.1 = pc ∧ callee ∈ entry.2.callees := by + simp only [Method.calleesAt, Method.linkAt, linkLookup] at member + cases found : body.links.find? (fun entry => entry.1 == pc) with + | none => simp [found] at member + | some entry => + simp only [found, Option.map_some, Option.getD_some] at member + exact ⟨entry, List.mem_of_find?_eq_some found, by simpa using List.find?_some found, member⟩ + +#print axioms Method.calleesAt_mem end CIL @@ -27,79 +37,70 @@ def FrameAction.callee : FrameAction → Option Nat | .call callee .. | .tailCall callee .. | .construct callee .. => some callee | _ => none -/-- An indirect call can enter only a body named in its authenticated catalog. -/ -theorem managedStep_callee {operation : CIL.ManagedOp} {pc : Nat} {frame : Frame} - {stack : List Value} {memory : Memory} {action : FrameAction} {callee : Nat} - (executed : managedStep operation pc frame stack memory = .ok action) - (enters : action.callee = some callee) : callee ∈ (CIL.Op.managed operation).callees := by - unfold managedStep at executed +/-- An indirect call can enter only a body named in the authenticated catalog of its link. -/ +theorem calliStep_callee {signature : CIL.ManagedSignature} {tail : Bool} {link : Option CIL.Link} + {frame : Frame} {stack : List Value} {memory : Memory} {action : FrameAction} {callee : Nat} + (executed : calliStep signature tail link frame stack memory = .ok action) + (enters : action.callee = some callee) : + (CIL.Instruction.calli signature tail).readsLink = true ∧ + callee ∈ (link.map CIL.Link.callees).getD [] := by + unfold calliStep at executed simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at executed repeat' first | solve | cases executed simp only [FrameAction.callee, Option.some.injEq] at enters subst callee - exact List.mem_map.mpr ⟨_, List.mem_of_find?_eq_some (by assumption), rfl⟩ + refine ⟨rfl, ?_⟩ + simp only [Option.map_some, Option.getD_some, CIL.Link.callees] + exact indirectTarget_mem (by assumption) | solve | cases executed; cases enters | cases executed | split at executed -#print axioms managedStep_callee - -/-- A virtual call enters only a body its table lists. -/ -theorem step_callVirtual_callee {body : CIL.Method} - {targets : List (CIL.Extraction.TypeExpr × Nat)} {argc pc : Nat} {args : List Value} - {frame : Frame} {stack : List Value} {memory : Memory} {action : FrameAction} {callee : Nat} - (executed : step body (.callVirtual targets argc) pc args frame stack memory = .ok action) - (enters : action.callee = some callee) : - callee ∈ (CIL.Op.callVirtual targets argc).callees := by - simp only [step, observerCount, Bool.or_false] at executed - split at executed - · cases executed - split at executed - · cases executed - obtain ⟨arguments, _, executed⟩ := except_bind_ok executed - split at executed - · obtain ⟨selectedCallee, selected, executed⟩ := except_bind_ok executed - cases executed - simp only [FrameAction.callee, Option.some.injEq] at enters - subst callee - exact virtualTarget_mem selected - all_goals cases executed -#print axioms step_callVirtual_callee +#print axioms calliStep_callee set_option maxHeartbeats 800000 in -/-- A successful call or construction enters a method listed by its instruction. -/ -theorem step_callee {body : CIL.Method} {op : CIL.Op} {pc : Nat} {args : List Value} +/-- A successful call or construction is a step of an instruction that reads its link, and enters + a method its link at the step's pc lists. -/ +theorem step_callee {body : CIL.Method} {op : CIL.Instruction} {pc : Nat} {args : List Value} {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} {callee : Nat} (h : step body op pc args frame stack m = .ok action) - (enters : action.callee = some callee) : callee ∈ op.callees := by - cases op - case callVirtual targets argc => exact step_callVirtual_callee h enters - all_goals unfold step at h - all_goals dsimp only at h - all_goals split at h + (enters : action.callee = some callee) : + op.readsLink = true ∧ callee ∈ body.calleesAt pc := by + unfold step observedCall referenceCompareCall referenceSubtractCall newobjStep + constructAggregate constructObject at h + dsimp only at h + split at h all_goals repeat' first - | exact managedStep_callee h enters + | exact calliStep_callee h enters + | unfold returnFrom at h + | unfold localAllocation at h + | unfold initializeSpanHome at h | solve | cases h; cases enters | solve | cases h simp only [FrameAction.callee, Option.some.injEq] at enters subst callee - simp only [Op.callees, List.mem_singleton] + refine ⟨rfl, ?_⟩ + rw [CIL.Method.calleesAt, ‹body.linkAt pc = _›] + first + | (simp only [Option.map_some, Option.getD_some, CIL.Link.callees, List.mem_singleton]; done) + | (simp only [Option.map_some, Option.getD_some, CIL.Link.callees] + exact virtualTarget_mem ‹_›) | contradiction + | solve | (obtain ⟨_, _, _, rfl, _⟩ := modelStep_frame h; cases enters) | (obtain ⟨_, _, h⟩ := except_bind_ok h) | split at h #print axioms step_callee /-- An indirect tail call cannot leave a caller continuation behind. -/ theorem step_managed_tail_action {body : CIL.Method} {signature : CIL.ManagedSignature} - {catalog : List CIL.ManagedTarget} {pc : Nat} {args : List Value} {frame : Frame} + {pc : Nat} {args : List Value} {frame : Frame} {stack : List Value} {memory : Memory} {action : FrameAction} - (executed : step body (.managed (.call signature catalog true)) pc args frame stack memory = + (executed : step body (.calli signature true) pc args frame stack memory = .ok action) : ∃ callee arguments result, action = .tailCall callee arguments result := by - simp only [step, managedStep, observerCount, Bool.or_false, - Bind.bind, Except.bind, Pure.pure, Except.pure, Bool.true_eq, ite_true] at executed + simp only [step, calliStep, Bind.bind, Except.bind, Pure.pure, Except.pure] at executed repeat' first | solve | cases executed; exact ⟨_, _, _, rfl⟩ | cases executed @@ -109,7 +110,7 @@ theorem step_managed_tail_action {body : CIL.Method} {signature : CIL.ManagedSig /-- A step that stays in the frame resumes at the fallthrough of a continuing instruction or at one of its targets. -/ -theorem step_next_target {body : CIL.Method} {op : CIL.Op} {pc : Nat} {args : List Value} +theorem step_next_target {body : CIL.Method} {op : CIL.Instruction} {pc : Nat} {args : List Value} {frame : Frame} {stack : List Value} {m : Memory} {target : Nat} {stack' : List Value} {frame' : Frame} {m' : Memory} (h : step body op pc args frame stack m = .ok (.next target stack' frame' m')) : @@ -121,54 +122,57 @@ theorem step_next_target {body : CIL.Method} {op : CIL.Op} {pc : Nat} {args : Li have same : op.relabel r = op := op.relabel_eq_self r fun t member => ite_eq_right fun e => outside (.inr (by rw [← e]; exact member)) have moved := step_relabel op body body r pc pc args frame stack m _ - ⟨rfl, rfl, rfl, fun continues => ite_eq_right fun e => outside (.inl ⟨continues, e.symm⟩)⟩ h + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, + fun continues => ite_eq_right fun e => outside (.inl ⟨continues, e.symm⟩)⟩ h rw [same, h] at moved simp only [FrameAction.relabel, Except.ok.injEq, FrameAction.next.injEq] at moved have : r target = target + 1 := ite_eq_left rfl omega -/-- A call instruction only ever enters the method it names as a call, never as a - construction. -/ -theorem step_call_action {body : CIL.Method} {callee argc pc : Nat} {args : List Value} - {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} - (h : step body (.call callee argc) pc args frame stack m = .ok action) : - ∃ arguments rest m', action = .call callee arguments rest m' := by - unfold step at h - simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h - repeat' (first | contradiction | split at h) - all_goals (cases h; exact ⟨_, _, _, rfl⟩) - -/-- A tail-call instruction requests frame retirement and exactly its named callee. -/ -theorem step_tailCall_action {body : CIL.Method} {callee argc pc : Nat} {args : List Value} +/-- A `call` only ever enters the method its link names, never as a construction: under a `tail.` + prefix it retires the frame, and otherwise it calls and continues. A call whose pc states no + link runs its runtime model, which continues at the next pc; only a span constructor run in + place changes the frame, and only in the span slot its home names. -/ +theorem step_call_action {body : CIL.Method} {method : CIL.Extraction.MethodRef} + {prefixes : CIL.CallPrefixes} {pc : Nat} {args : List Value} {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} - (h : step body (.tailCall callee argc) pc args frame stack m = .ok action) : - ∃ arguments updated, action = .tailCall callee arguments updated := by - unfold step at h + (h : step body (.call method prefixes) pc args frame stack m = .ok action) : + (∃ callee, body.linkAt pc = some (.direct callee) ∧ + if prefixes.tail then ∃ arguments updated, action = .tailCall callee arguments updated + else ∃ arguments rest m', action = .call callee arguments rest m') ∨ + (body.linkAt pc = none ∧ prefixes.tail = false ∧ + ∃ values final changed, action = .next (pc + 1) values changed final ∧ + (changed = frame ∨ ∃ constructor index width writable view newView, + Extraction.runtimeModel body.environment .call method = + some (.spanInPlace constructor) ∧ + constructor.shape = (width, writable) ∧ + frame.locals[index]? = some (.span width writable view) ∧ + changed = { frame with + locals := frame.locals.set index (.span width writable (some newView)) })) := by + unfold step observedCall at h simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h repeat' (first | contradiction | split at h) - all_goals (cases h; exact ⟨_, _, rfl⟩) -#print axioms step_tailCall_action - -/-- Vector and aggregate construction enter the constructor they name with a temporary, never - as a plain call. -/ -theorem step_newVector256_action {body : CIL.Method} {callee argc pc : Nat} {args : List Value} - {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} - (h : step body (.newVector256 callee argc) pc args frame stack m = .ok action) : + all_goals first + | (cases h; exact .inl ⟨_, by assumption, by simp_all⟩) + | (obtain ⟨_, _, _, rfl, framed⟩ := modelStep_frame (model := Extraction.runtimeModel + body.environment .call method) (body := body) (pc := pc) (frame := frame) + (stack := stack) (memory := m) h + exact .inr ⟨by assumption, by simp_all, _, _, _, rfl, framed⟩) +#print axioms step_call_action + +/-- A construction under an `.aggregate` link enters the constructor the link names with a + temporary, never as a plain call. -/ +theorem step_newobj_aggregate_action {body : CIL.Method} {constructor : CIL.Extraction.MethodRef} + {callee pc : Nat} {size : Fin (2^64)} {args : List Value} {frame : Frame} + {stack : List Value} {m : Memory} {action : FrameAction} + (linked : body.linkAt pc = some (.aggregate callee size)) + (h : step body (.newobj constructor) pc args frame stack m = .ok action) : ∃ arguments rest frame' temporary kind m', action = .construct callee arguments rest frame' temporary kind m' := by - unfold step at h - simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h - repeat' (first | contradiction | split at h) - all_goals (cases h; exact ⟨_, _, _, _, _, _, rfl⟩) - -theorem step_newAggregate_action {body : CIL.Method} {callee argc pc : Nat} {size} - {args : List Value} {frame : Frame} {stack : List Value} {m : Memory} {action : FrameAction} - (h : step body (.newAggregate callee argc size) pc args frame stack m = .ok action) : - ∃ arguments rest frame' temporary kind m', - action = .construct callee arguments rest frame' temporary kind m' := by - unfold step at h - simp only [Bind.bind, Except.bind, Pure.pure, Except.pure] at h + unfold step newobjStep constructAggregate at h + simp only [linked, Bind.bind, Except.bind, Pure.pure, Except.pure] at h repeat' (first | contradiction | split at h) all_goals (cases h; exact ⟨_, _, _, _, _, _, rfl⟩) +#print axioms step_newobj_aggregate_action end CIL.Safety diff --git a/CIL/Safety/StepLiveState.lean b/CIL/Safety/StepLiveState.lean index 59e2a86cb..a1a60c853 100644 --- a/CIL/Safety/StepLiveState.lean +++ b/CIL/Safety/StepLiveState.lean @@ -28,7 +28,8 @@ def FrameAction.StateValid (program : CIL.Program) (args : List Value) (current /-- A successful step from a live state yields a state valid for its action. -/ theorem step_live_state (program : CIL.Program) (method : Nat) (body : CIL.Method) - (operation : CIL.Op) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) + (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) + (stack : List Value) (m : Memory) (action : FrameAction) (state : LiveState program args frame stack m) (found : program[method]? = some body) (h : step body operation pc args frame stack m = .ok action) : diff --git a/CIL/Safety/StepLocalReferences.lean b/CIL/Safety/StepLocalReferences.lean index 84af05362..863bf195d 100644 --- a/CIL/Safety/StepLocalReferences.lean +++ b/CIL/Safety/StepLocalReferences.lean @@ -17,17 +17,16 @@ theorem stored_frame_references_valid (m result : Memory) (frame : Frame) (index Frame.ReferencesValid result { frame with locals := frame.locals.set index final } := by have retained := valid.preserve hm (storeLocal_extends_allocations _ _ _ _ _ h) have stored := storeLocal_preserves_reference_validity _ _ _ _ _ hm - (valid.1 slot (List.mem_of_getElem? present)) h - refine ⟨?_, retained.2⟩ + (valid slot (List.mem_of_getElem? present)) h intro other member rcases List.mem_or_eq_of_mem_set member with old | same - · exact retained.1 other old + · exact retained other old · subst other exact stored /-- A step keeps every frame slot reference valid in the memory it leaves behind. -/ -theorem step_preserves_frame_references (body : CIL.Method) (operation : CIL.Op) (pc : Nat) - (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) +theorem step_preserves_frame_references (body : CIL.Method) (operation : CIL.Instruction) + (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) (hm : m.WellFormed) (valid : frame.ReferencesValid m) (h : step body operation pc args frame stack m = .ok action) : (action.frameAfter frame).ReferencesValid action.memory := by diff --git a/CIL/Safety/StepRenaming.lean b/CIL/Safety/StepRenaming.lean index a6f549e19..a4a6f83f9 100644 --- a/CIL/Safety/StepRenaming.lean +++ b/CIL/Safety/StepRenaming.lean @@ -15,7 +15,7 @@ by recursive execution. No consumer supplies or assumes a step simulation obliga namespace CIL.Safety.StepRenaming /-- Nonlocal nonallocating instructions retain their exact opcode under local relocation. -/ -private theorem fixedOperation (indices : LocalIndexMap) (operation : CIL.Op) +private theorem fixedOperation (indices : LocalIndexMap) (operation : CIL.Instruction) (fixed : fixedStepFamily operation ≠ .excluded) : indices.operation operation = operation := by cases operation <;> try rfl all_goals exact False.elim (fixed rfl) @@ -46,49 +46,132 @@ theorem stepTransport : StepTransport := by metadata operation family pc arguments stack bounds observable⟩ | excluded => cases operation <;> try cases family - case newArray.refl element => - exact memories.stepNewArray frames sourceFormed targetFormed staticBounds - sourceBody targetBody element pc arguments stack bounds observable - case newReferenceArray.refl element => - exact memories.stepNewReferenceArray frames sourceFormed targetFormed staticBounds - sourceBody targetBody element pc arguments stack bounds observable - case newObject.refl callee argc owner bases size => - exact memories.stepNewObject sourceFormed targetFormed staticBounds sourceBody targetBody - callee argc owner bases size pc arguments stack bounds observable + case newarr.refl element => + exact memories.stepNewarr frames sourceFormed targetFormed staticBounds + sourceBody targetBody metadata element pc arguments stack bounds observable + case newobj.refl constructor => + exact memories.stepNewobj frames sourceFormed targetFormed staticBounds sourceBody + targetBody constructor pc metadata arguments stack bounds observable + case ldobj.refl | stobj.refl | cpobj.refl | initobj.refl | sizeof.refl => + refine ⟨mapping, .refl mapping, ?_⟩ + simp only [LocalIndexMap.operation, step, observerCount, indices.unspecifiedOperands mapping, + Extraction.CallLayouts.valueShape_eq, Method.valueShape, metadata.environment] + at observable ⊢ + rename_i type + first + | (let count := if Extraction.spanLocal type then 0 else 1 + change SuccessOrThrow (if unspecifiedOperands count stack then _ else _) at observable) + | (let count := 2 + change SuccessOrThrow (if unspecifiedOperands count stack then _ else _) at observable) + | (let count := 1 + change SuccessOrThrow (if unspecifiedOperands count stack then _ else _) at observable) + | (let count := 0 + change SuccessOrThrow (if unspecifiedOperands count stack then _ else _) at observable) + by_cases observed : unspecifiedOperands count stack = true <;> + simp only [count] at observed observable <;> + simp only [observed, Bool.false_eq_true, ite_true, ite_false] at observable ⊢ + all_goals try exact .error _ + all_goals try cases width : (sourceBody.environment.valueShape type).size? <;> + simp only [width] at observable ⊢ + all_goals try exact .error _ + all_goals first + | exact .ok (.next memories frames) + | (cases shape : sourceBody.environment.valueShape type <;> + simp only [shape] at observable ⊢ <;> + first + | exact .error _ + | exact frames.initializeSpanHome memories pc stack) + | (first + | apply (memories.copyInitializedBytesResult _ sourceFormed stack bounds + observable.bind_left).bind + | apply (memories.initializeBytesResult _ sourceFormed stack bounds + observable.bind_left).bind + | apply (memories.transferObjectResult _ _ sourceFormed stack bounds + observable.bind_left).bind + rintro ⟨_, _⟩ ⟨_, _⟩ ⟨related, rfl⟩ + exact .ok (.next related frames)) + | (apply (frames.viewOperands memories _ stack bounds).bindWithSuccess + intro viewed otherViewed sourceViewed operandsRelated + obtain ⟨rfl, viewedBounds⟩ := operandsRelated + apply (memories.transferObjectResult _ _ sourceFormed viewed viewedBounds + (observable.bind_right sourceViewed).bind_left).bind + rintro ⟨_, _⟩ ⟨_, _⟩ ⟨related, rfl⟩ + exact .ok (.next related frames)) + case ldelem_any.refl element => + exact ⟨mapping, .refl mapping, memories.stepLdelemAny frames sourceFormed sourceBody + targetBody metadata element pc arguments stack bounds observable⟩ + case stelem_any.refl element => + exact ⟨mapping, .refl mapping, memories.stepStelemAny frames sourceFormed sourceBody + targetBody metadata element pc arguments stack bounds observable⟩ + case ldelema.refl element readOnly => + exact ⟨mapping, .refl mapping, memories.stepLdelema frames sourceFormed sourceBody + targetBody metadata element readOnly pc arguments stack bounds observable⟩ + case cpblk.refl alignment | initblk.refl alignment => + refine ⟨mapping, .refl mapping, ?_⟩ + simp only [LocalIndexMap.operation, step, observerCount, indices.unspecifiedOperands mapping] + at observable ⊢ + by_cases observed : unspecifiedOperands 3 stack = true <;> + simp only [observed, Bool.false_eq_true, ite_true, ite_false] at observable ⊢ + · exact .error _ + first + | apply (memories.blockBytesResult true alignment stack bounds + observable.bind_left).bind + | apply (memories.blockBytesResult false alignment stack bounds + observable.bind_left).bind + rintro ⟨_, _⟩ ⟨_, _⟩ ⟨related, rfl⟩ + exact .ok (.next related frames) + case ldstr.refl text => + refine ⟨mapping, .refl mapping, ?_⟩ + change SuccessOrThrow (do + let (memory, values) ← stringLiteralInstruction text stack source + return FrameAction.next (pc + 1) values sourceFrame memory) at observable + apply (memories.stringLiteralInstructionResult sourceFormed text stack observable.bind_left).bind + rintro ⟨_, _⟩ ⟨_, _⟩ ⟨related, rfl⟩ + exact .ok (.next related frames) + case ldsfld.refl field => + exact ⟨mapping, .refl mapping, memories.stepLdsfld frames staticBounds sourceBody + targetBody metadata field pc arguments stack⟩ + case ldsflda.refl field => + exact memories.stepStaticAddress frames sourceFormed targetFormed staticBounds sourceBody + targetBody metadata _ (fun _ _ _ _ _ _ => rfl) pc arguments stack bounds + observable + case ldtoken.refl operand => + cases operand with + | type type => exact ⟨mapping, .refl mapping, .ok (.next memories frames)⟩ + | field field => + exact memories.stepStaticAddress frames sourceFormed targetFormed staticBounds sourceBody + targetBody metadata _ (fun _ _ _ _ _ _ => rfl) pc arguments stack bounds observable + case stsfld.refl field => + refine ⟨mapping, .refl mapping, ?_⟩ + dsimp only [LocalIndexMap.operation] + rw [step_stsfld_eq, step_stsfld_eq, indices.unspecifiedOperands mapping] + split <;> exact .error _ + case ldfld.refl field => + exact memories.stepFieldLoad frames sourceFormed sourceBody targetBody metadata field pc + arguments stack bounds observable + case stfld.refl field => + exact memories.stepFieldStore frames sourceFormed sourceBody targetBody metadata field pc + arguments stack bounds observable + case ldflda.refl field => + exact memories.stepFieldAddress frames sourceFormed sourceBody targetBody metadata field pc + arguments stack bounds observable all_goals dsimp only [LocalIndexMap.operation] all_goals first + | exact memories.stepLocalloc frames sourceFormed targetFormed staticBounds + sourceBody targetBody metadata.declared pc arguments stack observable | exact ⟨mapping, .refl mapping, - frames.stepInitializeSpanLocal memories sourceFormed sourceBody targetBody _ pc _ - arguments stack bounds observable⟩ - | exact memories.stepMemory frames sourceFormed targetFormed staticBounds sourceBody targetBody - metadata _ pc arguments stack bounds observable - | exact memories.stepStackAlloc frames sourceFormed targetFormed staticBounds - sourceBody targetBody _ pc arguments stack observable - | exact memories.stepNewVector256 frames sourceFormed targetFormed staticBounds - sourceBody targetBody _ _ pc arguments stack bounds - | exact memories.stepNewAggregate frames sourceFormed targetFormed staticBounds - sourceBody targetBody _ _ pc _ arguments stack bounds + frames.stepLdloc memories sourceBody targetBody _ pc arguments stack⟩ | exact ⟨mapping, .refl mapping, - frames.stepLocal memories sourceBody targetBody _ pc _ arguments stack⟩ - | exact ⟨mapping, .refl mapping, - frames.stepLocalAddress memories sourceBody targetBody _ pc arguments stack⟩ - | exact ⟨mapping, .refl mapping, - frames.stepVectorLocal memories sourceBody targetBody _ pc arguments stack⟩ - | exact ⟨mapping, .refl mapping, - frames.stepVectorLocalAddress memories sourceBody targetBody _ pc arguments stack⟩ + frames.stepLdloca memories sourceBody targetBody _ pc arguments stack⟩ | (cases stack with | nil => simp only [step, observerCount, unspecifiedOperands, List.take_nil, List.any_nil, - Bool.false_or, Bool.false_eq_true, ite_false, pureArity, instruction] at observable + Bool.false_eq_true, ite_false, pureArity, instruction] at observable cases observable | cons value rest => - first - | exact ⟨mapping, .refl mapping, - frames.stepSetLocal memories sourceBody targetBody _ pc _ arguments rest value - (bounds value (List.mem_cons_self ..))⟩ - | exact ⟨mapping, .refl mapping, - frames.stepSetVectorLocal memories sourceBody targetBody _ pc arguments rest value - (bounds value (List.mem_cons_self ..))⟩) + exact ⟨mapping, .refl mapping, + frames.stepStloc memories sourceBody targetBody _ pc arguments rest value + (bounds value (List.mem_cons_self ..))⟩) #print axioms stepTransport end CIL.Safety.StepRenaming diff --git a/CIL/Safety/StepTransport.lean b/CIL/Safety/StepTransport.lean index 1ce9e932e..0ec6dad6d 100644 --- a/CIL/Safety/StepTransport.lean +++ b/CIL/Safety/StepTransport.lean @@ -1,5 +1,6 @@ import CIL.Safety.ActionRenaming import CIL.Safety.MemoryInstructionRenaming +import CIL.Resolution /-! # Allocation-renaming obligation for a checked step @@ -15,11 +16,28 @@ structure StepMetadata (source target : CIL.Method) : Prop where profile : target.profile = source.profile returnsValue : target.returnsValue = source.returnsValue staticSites : target.staticSites = source.staticSites + returnNarrow : target.returnNarrow = source.returnNarrow + /-- Both bodies read every argument from the incoming list, narrowed alike. -/ + arguments : ∀ index, ∃ narrow, source.argumentAccess index = .value narrow ∧ + target.argumentAccess index = .value narrow + /-- Both bodies declare one signature, so `localloc` zeroes storage alike. -/ + declared : target.declared = source.declared + /-- Both bodies resolve types and layouts in the same environment. -/ + environment : target.environment = source.environment + /-- Both bodies enter the same methods, since each call reads its target from this table. -/ + links : target.links = source.links + +/-- Related bodies give every call site the same link. -/ +theorem StepMetadata.linkAt {source target : CIL.Method} (metadata : StepMetadata source target) + (pc : Nat) : target.linkAt pc = source.linkAt pc := by + unfold CIL.Method.linkAt + rw [metadata.links] +#print axioms StepMetadata.linkAt /-- One observable checked step transports frames, values and fresh allocation correspondence. -/ def StepTransport : Prop := ∀ (indices : LocalIndexMap) (mapping : AllocationRenaming) (scopes : Nat → Nat → Prop) - (sourceBody targetBody : CIL.Method) (operation : CIL.Op) (pc : Nat) + (sourceBody targetBody : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (arguments stack : List Value) (sourceFrame targetFrame : Frame) (source target : Memory), StepMetadata sourceBody targetBody → Memory.Renamed mapping scopes source target → @@ -33,6 +51,6 @@ def StepTransport : Prop := ResultRenamed later (FrameAction.Renamed indices later scopes) (step sourceBody operation pc arguments sourceFrame stack source) (step targetBody (indices.operation operation) pc (arguments.map mapping.value) - targetFrame (stack.map mapping.value) target) + targetFrame (stack.map (indices.value mapping)) target) end CIL.Safety diff --git a/CIL/Safety/StepValues.lean b/CIL/Safety/StepValues.lean index 5040cc044..e4ed23a87 100644 --- a/CIL/Safety/StepValues.lean +++ b/CIL/Safety/StepValues.lean @@ -10,7 +10,7 @@ Part of the per-step `LiveState` invariant. namespace CIL.Safety -theorem step_values_valid (body : CIL.Method) (operation : CIL.Op) (pc : Nat) +theorem step_values_valid (body : CIL.Method) (operation : CIL.Instruction) (pc : Nat) (args : List Value) (frame : Frame) (stack : List Value) (m : Memory) (action : FrameAction) (hm : m.WellFormed) (valid : ValuesValid m stack) (h : step body operation pc args frame stack m = .ok action) : action.LiveValues := diff --git a/CIL/Safety/SymbolicExecution.lean b/CIL/Safety/SymbolicExecution.lean index f748f3e6e..5cd2eb3bc 100644 --- a/CIL/Safety/SymbolicExecution.lean +++ b/CIL/Safety/SymbolicExecution.lean @@ -13,6 +13,7 @@ import CIL.Safety.CheckedMemoryFacts import CIL.Safety.AggregateMemorySteps import CIL.Safety.LoopFacts import CIL.TermAccess +import CIL.Safety.NormalNodes /-! # Checked symbolic execution @@ -28,30 +29,20 @@ and loop tactics, the automation tests and downstream proofs all build on these; namespace CIL.Safety -/-- Checked local loads use the slot's kind; the opcode annotation adds no second conversion. -/ -theorem step_local_annotation (body : CIL.Method) (index : Nat) - (narrow : Option CIL.NarrowKind) (pc : Nat) (args : List Value) (frame : Frame) - (stack : List Value) (memory : Memory) : - step body (.local index narrow) pc args frame stack memory = - step body (.local index) pc args frame stack memory := rfl - -#print axioms step_local_annotation - -/-- Checked local stores use the slot's kind independently of the opcode annotation. -/ -theorem step_setLocal_annotation (body : CIL.Method) (index : Nat) - (narrow : Option CIL.NarrowKind) (pc : Nat) (args : List Value) (frame : Frame) - (stack : List Value) (memory : Memory) : - step body (.setLocal index narrow) pc args frame stack memory = - step body (.setLocal index) pc args frame stack memory := by - cases stack <;> rfl - -#print axioms step_setLocal_annotation - open Lean Elab Tactic Conv in /-- Replaces the focused term by its full reduction; stepping applies it to the goal's fuel. -/ elab "cil_reduce_fuel" : conv => do changeLhs (← Meta.reduce (← getLhs)) +open Lean Elab Tactic Conv in +/-- Replaces the focused closed natural number by its literal. A call's continuation resumes at + `pc + 1`; without it the pc grows by one addition per call, and so does every lookup. -/ +elab "cil_literal_nat" : conv => do + let lhs ← instantiateMVars (← getLhs) + if lhs.hasFVar || lhs.hasMVar then return + let some value ← (Meta.evalNat lhs).run | return + changeLhs (mkNatLit value) + open Lean Elab Tactic in /-- Runs `tactic` so that a failed candidate proof fails and can backtrack, rather than be recovered as an admitted term. Only the candidate's own errors count, so an earlier failed @@ -65,6 +56,81 @@ elab "cil_strict " tactic:tactic : tactic => evalTactic tactic if errors (← Core.getMessageLog) > earlier then throwError "Checked step candidate failed" +open Lean Elab Tactic in +/-- `term` with each `by` block replaced by a named hole, and the blocks in order of + appearance. -/ +private partial def holesForByBlocks (term : Syntax) : + StateT (Array (Name × Syntax)) TacticM Syntax := do + if term.isOfKind ``Lean.Parser.Term.byTactic then + -- A named hole refers to an existing hole of that name, so each block's name is fresh. + let name := Name.mkSimple s!"cilBlock{← mkFreshId}" + modify (·.push (name, term[1])) + return (← `(term| ?$(mkIdent name))).raw + match term with + | .node info kind arguments => return .node info kind (← arguments.mapM holesForByBlocks) + | other => return other + +open Lean Elab Tactic Meta in +/-- Elaborates `term` against `expectedType?` and runs each `by` block of it as a tactic on its + own goal after the rest elaborates. A `by` block inside a term makes Lean rebuild the whole + local context before it runs, so its cost grows with the hypotheses in scope; a goal does + not. Returns the proof and the goals left, which are the holes `?_` and `?name` of `term`; + any other unassigned hole is an error, as in `refine`. -/ +def elabTermRunningBlocks (term : Syntax) (expectedType? : Option Expr) : + TacticM (Expr × List MVarId) := do + let (replaced, blocks) ← (holesForByBlocks term).run #[] + let holeCount := Id.run do + let mut count := 0 + let mut pending := #[replaced] + while let some next := pending.back? do + pending := pending.pop + -- A hole that refers to an existing metavariable has a hygienic name and is no new goal. + if next.isOfKind ``Lean.Parser.Term.syntheticHole && + !(next[1].isIdent && next[1].getId.hasMacroScopes) then count := count + 1 + pending := pending ++ next.getArgs + return count + let (proof, holes) ← elabTermWithHoles replaced expectedType? `refine + (allowNaturalHoles := true) + for (name, tactics) in blocks do + let some hole ← holes.findM? fun hole => return (← hole.getTag) == name + | throwError "a `by` block was not elaborated: {name}" + let unsolved ← Tactic.run hole (evalTactic tactics) + unless unsolved.isEmpty do + throwError "a `by` block left goals{goalsToMessageData unsolved}" + let pending ← holes.filterM fun hole => return !(← hole.isAssigned) + unless pending.length == holeCount - blocks.size do + throwError "expected {holeCount - blocks.size} goals, found{goalsToMessageData pending}" + return (proof, pending) + +open Lean Elab Tactic Meta in +/-- `refine term`, with each `by` block of `term` run as a tactic on its own goal + (`elabTermRunningBlocks`). -/ +elab "cil_refine_by " term:term : tactic => withMainContext do + let (proof, pending) ← elabTermRunningBlocks term.raw (← getMainTarget) + (← getMainGoal).assign (← instantiateMVars proof) + replaceMainGoal pending + +open Lean Elab Tactic Meta in +/-- `have name := term`, with each `by` block of `term` run as a tactic on its own goal + (`elabTermRunningBlocks`). -/ +elab "cil_have_by " name:ident " := " term:term : tactic => withMainContext do + let (proof, pending) ← elabTermRunningBlocks term.raw none + unless pending.isEmpty do throwError "cil_have_by: the term leaves goals" + let proof ← instantiateMVars proof + let type ← instantiateMVars (← inferType proof) + replaceMainGoal [(← addFact (← getMainGoal) name.getId type proof).2] + +open Lean Elab Tactic Meta in +/-- `have name : type := by steps`, with the steps run as a tactic on a goal of their own. -/ +elab "cil_have_proved " name:ident " : " type:term " := " "by " steps:tacticSeq : tactic => + withMainContext do + let statement ← Term.withSynthesize <| Term.elabType type + let hole ← mkFreshExprSyntheticOpaqueMVar statement + let unsolved ← Tactic.run hole.mvarId! (evalTactic steps) + unless unsolved.isEmpty do + throwError "cil_have_proved: the proof left goals{goalsToMessageData unsolved}" + replaceMainGoal [(← addFact (← getMainGoal) name.getId statement (← instantiateMVars hole)).2] + /-- The projections of a method contract that stepping unfolds in a goal: its argument and result encodings, precondition, footprint and postcondition. -/ def contractProjections : List Lean.Name := @@ -112,7 +178,7 @@ elab "cil_frame_exit" : tactic => withMainContext do unless leaves e do return .continue let #[frame, memory] := e.getAppArgs | return .continue let frame ← whnfR frame - let some #[_, _, owned, _, _] := argumentsOf? frame `CIL.Safety.Frame.mk 5 + let some #[_, _, owned, _] := argumentsOf? frame `CIL.Safety.Frame.mk 4 | return .continue unless (← whnfR owned).isAppOfArity ``List.nil 1 do return .continue return .visit memory) @@ -165,20 +231,41 @@ syntax "cil_fact_simp" Lean.Parser.Tactic.optConfig " [" term,* "]" " [" term,* initialize Lean.registerTraceClass `CIL.factNormalize open Lean in -/-- Whether an expression contains a literal operand the checked state normalizer evaluates. -/ -private def hasClosedLiteralOperand (expression : Expr) : Bool := +/-- The width and value of a numeral used as the right mask of a bitwise AND. -/ +private def numeralMask? (term : Expr) : Option (Expr × Nat) := do + guard (term.isAppOfArity ``HAnd.hAnd 6) + let mask ← lastArgument? term + let #[_, .lit (.natVal value), inst] ← argumentsOf? mask ``OfNat.ofNat 3 | none + let #[width, _] ← argumentsOf? inst ``BitVec.instOfNat 2 | none + return (width, value) + +open Lean in +/-- Whether `value` is a literal operand the checked state normalizer evaluates. -/ +def closedLiteralOperand (value : Expr) : Bool := let literal (value : Expr) := value.isRawNatLit || value.isAppOfArity ``OfNat.ofNat 3 let numeral (value : Expr) := literal value || (value.isAppOfArity ``BitVec.ofNat 2 && (lastArgument? value).any literal) || (value.isAppOfArity ``BitVec.ofFin 2 && (lastArgument? value).any fun finite => finite.isAppOfArity ``Fin.ofNat 3 && (lastArgument? finite).any literal) - (expression.find? fun value => - (value.isAppOfArity ``BitVec.ofFin 2 && numeral value) || + -- The simprocs need the new width as a literal too, and `ofInt` an integer literal. + let integer (value : Expr) := literal value || (value.isAppOfArity ``Int.ofNat 1 && + (lastArgument? value).any literal) || (value.isAppOfArity ``Neg.neg 3 && + (lastArgument? value).any literal) + (value.isAppOfArity ``BitVec.ofFin 2 && numeral value) || + (numeralMask? value).isSome || ((value.isAppOfArity ``BitVec.signExtend 3 || value.isAppOfArity ``BitVec.setWidth 3 || - value.isAppOfArity ``BitVec.zeroExtend 3 || value.isAppOfArity ``BitVec.toNat 2) && + value.isAppOfArity ``BitVec.zeroExtend 3) && (argument? value 1).any literal && (lastArgument? value).any numeral) || + (value.isAppOfArity ``BitVec.toNat 2 && (lastArgument? value).any numeral) || + (value.isAppOfArity ``BitVec.ofInt 2 && (argument? value 0).any literal && + (argument? value 1).any integer) || (value.isAppOfArity ``HMod.hMod 6 && (argument? value 5).any numeral && - (argument? value 4).any numeral)).isSome + (argument? value 4).any numeral) + +open Lean in +/-- Whether an expression contains a literal operand the checked state normalizer evaluates. -/ +private def hasClosedLiteralOperand (expression : Expr) : Bool := + (expression.find? closedLiteralOperand).isSome open Lean Meta in /-- `type` with each closed natural-number argument of a `step` opcode evaluated to a numeral, @@ -208,11 +295,17 @@ def evaluateOpcodeIndices (type : Expr) : MetaM Expr := do open Lean Meta in /-- `term` with each `localResult kind value` that its constructors decide replaced by the value a load of that kind pushes, so a loaded `localResult .vector128 (.v128 bits)` reads - `.v128 bits` as the next fact states it. The result is definitionally equal to `term`. -/ -def reduceLocalResults (term : Expr) : MetaM Expr := do + `.v128 bits` as the next fact states it. The result is definitionally equal to `term`, and + the nodes `leaveAlone` selects, which name no local result, are not entered. -/ +def reduceLocalResults (term : Expr) (leaveAlone : Expr → Bool := fun _ => false) : + MetaM Expr := do -- Most goals and facts name no local result, and are returned without being rebuilt. - if (term.find? (·.isConstOf ``CIL.Safety.localResult)).isNone then return term + let names (candidate : Expr) : Expr.FindStep := + if leaveAlone candidate then .done + else if candidate.isConstOf ``CIL.Safety.localResult then .found else .visit + if (term.findExt? names).isNone then return term Meta.transform term (pre := fun candidate => do + if leaveAlone candidate then return .done candidate unless candidate.isAppOfArity ``CIL.Safety.localResult 2 do return .continue let some unfolded ← unfoldDefinition? candidate | return .continue let reduced ← whnfCore unfolded @@ -307,8 +400,7 @@ def normaliseStepFacts (config : TSyntax `Lean.Parser.Tactic.optConfig) | some eq => mkEqMP eq instanceProof | none => mkExpectedTypeHint instanceProof result.expr let name ← mkFreshUserName base - let (_, goal) ← (← (← getMainGoal).assert name result.expr normalisedProof).intro1P - replaceMainGoal [goal] + replaceMainGoal [(← addFact (← getMainGoal) name result.expr normalisedProof).2] names := names.push (mkIdent name) return names if !locals.isEmpty then @@ -379,15 +471,56 @@ private def indexedLookup? (lookup : Expr) : Option (Expr × Expr) := else none open Lean Elab Tactic Meta in -/-- Reuse a generated equation directly for a named body and a closed instruction position. -/ -elab "cil_lookup_equation" : tactic => withMainContext do - let some (_, lookup, _) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? +/-- The lookup rules retain caller-supplied facts while keeping a supplied program named. -/ +private def methodLookupContext (program : Name) (facts : Array Term) (registered : SimpTheorems) : + TacticM (Simp.Context × Simp.SimprocsArray) := do + -- Even preparing a simp entry for the program definition costs work in its list length. + let lookupFacts ← facts.filterM fun (fact : Term) => do + if !fact.raw.isIdent then return true + return !(← Term.resolveId? fact).any (·.isConstOf program) + let (ctx, simprocs) ← if lookupFacts.isEmpty then do + let some procs ← Simp.getSimprocExtension? `cil_code | throwError "Missing cil_code simprocs" + let ctx ← Simp.mkContext (simpTheorems := #[registered]) + (congrTheorems := ← getSimpCongrTheorems) + pure (ctx, #[← procs.getSimprocs]) + else do + let tactic ← `(tactic| simp only [cil_code, Nat.reduceAdd, Nat.reduceMul, + $[$lookupFacts:term],*]) + let { ctx, simprocs, .. } ← mkSimpContext tactic (eraseLocal := false) + pure (ctx, simprocs) + if lookupFacts.size == facts.size then return (ctx, simprocs) + return (ctx.setSimpTheorems (ctx.simpTheorems.eraseTheorem (.decl program)), simprocs) + +open Lean Elab Tactic Meta in +/-- Close a named lookup using its registered equations and the caller's method facts. -/ +private def closeLookupEquation (facts : Array Term) : TacticM Unit := withMainContext do + let some (_, lookup, expected) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? | throwError "Expected an instruction lookup" let some (code, index) := indexedLookup? lookup | throwError "Expected an indexed lookup" - unless code.isAppOfArity ``CIL.Method.code 1 do throwError "Expected a named method body" + let some position ← getNatValue? index | throwError "Expected a closed position" + unless code.isAppOfArity ``CIL.Method.code 1 do + let some name := code.getAppFn.constName? | throwError "Expected a named program" + let equation := name.appendAfter s!"_method{position}" + unless (← getEnv).contains equation do throwError "No generated method equation" + let some rules ← getSimpExtension? `cil_code | throwError "Missing cil_code rules" + let registered ← rules.getTheorems + let (context, simprocs) ← methodLookupContext name facts registered + let (result, _) ← simp lookup context simprocs steppingDischarge? + let goal ← getMainGoal + if result.expr != lookup && (← withReducible (isDefEq expected result.expr)) then + goal.assign (← result.getProof) + else + -- A cil_code unfold can erase the program name before its lookup equation fires. + unless registered.isLemma (.decl equation) || registered.isLemma (.decl equation false) do + throwError "No registered method equation" + let proof := mkAppN (← mkConstWithFreshMVarLevels equation) code.getAppArgs + unless ← withReducible (isDefEq (← goal.getType) (← inferType proof)) do + throwError "The method equation does not prove this lookup" + goal.assign proof + replaceMainGoal [] + return let body ← lastArgument "cil_lookup_equation" code let some name := body.getAppFn.constName? | throwError "Expected a named method body" - let some position ← getNatValue? index | throwError "Expected a closed position" let equation := name.appendAfter s!"_instruction{position}" unless (← getEnv).contains equation do throwError "No generated equation at this position" let proof := mkAppN (← mkConstWithFreshMVarLevels equation) body.getAppArgs @@ -397,7 +530,8 @@ elab "cil_lookup_equation" : tactic => withMainContext do let operation ← whnfD (← lastArgument "cil_lookup_equation" found) let some (.ctorInfo constructor) := operation.getAppFn.constName?.bind (← getEnv).find? | throwError "Instruction needs profile facts" - unless constructor.induct == ``CIL.Op do throwError "Expected an instruction constructor" + unless constructor.induct == instructionTypeName do + throwError "Expected an instruction constructor" let some (_, _, pending) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? | throwError "Expected an instruction lookup" unless ← isDefEq pending (← mkAppM ``Option.some #[operation]) do @@ -406,28 +540,53 @@ elab "cil_lookup_equation" : tactic => withMainContext do unless goals.isEmpty do throwError "The instruction equation left premises" replaceMainGoal [] -/-- Close a lookup goal `lookup = result` with a hypothesis, up to reducible unfolding, trying - only hypotheses that are equations whose left side has the goal's head; fails otherwise. -/ +open Lean Elab Tactic in +/-- Fetch a named method by simplifying its lookup, or an instruction by its equation. -/ +elab "cil_lookup_equation" : tactic => closeLookupEquation #[] + +/-- Close a lookup goal `lookup = result`, possibly quantified, with a hypothesis, up to reducible + unfolding, trying only equation hypotheses whose left side may unify with `lookup`; fails + otherwise. -/ syntax "cil_lookup_assumption" : tactic +open Lean Elab Tactic Meta in +/-- The equation hypotheses that may state the goal `goal`, in context order. Inside a step of a + stepping driver they come from the index the driver keeps, which the step's start brought up + to date; any other call scans the context for hypotheses whose head `headsMayMeet` the + goal's, which costs less than building an index for one lookup. -/ +private def equationCandidates (goal : Expr) : TacticM (Array FVarId) := do + let record ← getNormalNodes + let context ← getLCtx + if record.driven && cil.stepping.record.get (← getOptions) then + if let some locals := record.equationLocals then + if ← contextKeeps locals context then + let index ← record.equations.extend context + -- The index may still name a hypothesis that a store cleared. + return (← index.candidates goal).filter context.contains + let some wanted := equationHead? goal | return #[] + let wantedUnfolds ← headUnfolds wanted + let mut found := #[] + -- Hypotheses in a row often share a head, so the last head's verdict is kept. + let mut seen := wanted + let mut seenMeets := true + for declaration in context do + if declaration.isImplementationDetail then continue + let some head ← instantiatedEquationHead? declaration.type | continue + unless head == seen do + seen := head + seenMeets ← headsMayMeet wanted wantedUnfolds head + if seenMeets then found := found.push declaration.fvarId + return found + open Lean Elab Tactic Meta in elab_rules : tactic | `(tactic| cil_lookup_assumption) => withMainContext do let goal ← getMainGoal - let rec head (type : Expr) : Option Expr := - match type with - | .forallE _ _ body _ => head body - | .mdata _ body => head body - | type => type.eq?.map fun (_, lhs, _) => lhs.consumeMData.getAppFn - let some wanted := head (← instantiateMVars (← goal.getType)) - | throwError "cil_lookup_assumption: the goal is not an equation" - -- A head that is not a constant may unify with anything, so every hypothesis is tried. - let candidate (type : Expr) : Bool := match head type with - | some found => !wanted.isConst || !found.isConst || found == wanted - | none => false - for declaration in ← getLCtx do - if declaration.isImplementationDetail then continue - unless candidate (← instantiateMVars declaration.type) do continue + let statement ← instantiateMVars (← goal.getType) + unless (equationHead? statement).isSome do + throwError "cil_lookup_assumption: the goal is not an equation" + for hypothesis in ← equationCandidates statement do + let declaration ← hypothesis.getDecl let saved ← saveState if declaration.type.isForall then try @@ -561,7 +720,7 @@ elab_rules : tactic let equal := mkIdent (← mkFreshUserName `positionCase) let close ← `(tactic| ( simp only [$equal:term, Nat.reduceMul, Nat.reduceAdd] - all_goals first | cil_lookup_equation | cil_lookup [$[$facts:term],*])) + all_goals cil_lookup [$[$facts:term],*])) match remaining with | 0 => `(tactic| ( have $equal : $value = $numeral := by omega @@ -597,11 +756,12 @@ elab_rules : tactic try evalTactic tactic; return true catch _ => saved.restore; return false if ← attempt (← `(tactic| cil_lookup_assumption)) then return - if ← attempt (← `(tactic| cil_lookup_equation)) then return + if (← observing? (closeLookupEquation facts.getElems)).isSome then return let some (_, lookup, _) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? | throwError "Expected a lookup equation" if !lookup.hasFVar && !lookup.hasMVar then - evalTactic (← `(tactic| try simp only [cil_code, Nat.reduceAdd, Nat.reduceMul])) + evalTactic (← `(tactic| cil_try_simp_only [cil_code, Nat.reduceAdd, Nat.reduceMul, + $[$facts:term],*])) pruneSolvedGoals if (← getGoals).isEmpty then return if ← attempt (← `(tactic| cil_lookup_rfl [$facts:term,*])) then return @@ -616,7 +776,7 @@ elab_rules : tactic finite := finite || (← whnf declaration.type).isAppOfArity ``Fin 1 if placed && (← attempt (← `(tactic| cil_lookup_placed [$facts:term,*]))) then return if finite && (← attempt (← `(tactic| cil_lookup_finite [$facts:term,*]))) then return - evalTactic (← `(tactic| try simp only [cil_code, Nat.reduceAdd, Nat.reduceMul, + evalTactic (← `(tactic| cil_try_simp_only [cil_code, Nat.reduceAdd, Nat.reduceMul, bne_iff_ne, ne_eq, not_false_eq_true, Bool.false_eq_true, ite_true, ite_false, ↓reduceIte, $[$facts:term],*])) pruneSolvedGoals @@ -645,6 +805,15 @@ dsimproc_decl closedBitVecConstructor (BitVec.ofFin _) := fun expression => do return .done (mkApp2 (mkConst ``BitVec.ofNat) (toExpr width) (toExpr value)) attribute [cil_literal_proc] closedBitVecConstructor + +open Lean Meta in +/-- State a literal bit mask as a width-qualified bit vector in the stepped expression. -/ +dsimproc_decl bitVecNumeralMask ((_ &&& _ : BitVec _)) := fun expression => do + let .app operation _ := expression | return .continue + let some (width, value) := numeralMask? expression | return .continue + return .done (mkApp operation (mkApp2 (mkConst ``BitVec.ofNat) width (toExpr value))) + +attribute [cil_literal_proc] bitVecNumeralMask attribute [cil_literal_proc] BitVec.reduceSignExtend BitVec.reduceSetWidth BitVec.reduceZeroExtend BitVec.reduceToNat BitVec.reduceOfInt Nat.reduceMod @@ -657,8 +826,16 @@ elab "cil_literal_operands" : tactic => do -- A step that closed its goal leaves nothing to normalise. if (← getGoals).isEmpty then return withMainContext do - if hasClosedLiteralOperand (← instantiateMVars (← getMainTarget)) then - evalTactic (← `(tactic| try simp only [cil_literal])) + let name := "cil_literal_operands" + let target ← instantiateMVars (← getMainTarget) + let found ← if (← usableNormalNodes).isNone then pure (hasClosedLiteralOperand target) + else do + let (found, (_, clean)) := + (cleanWalk closedLiteralOperand (← normalNodesFor name) target).run ({}, #[]) + -- The terms below which no operand occurs stay so, whatever the step does next. + recordNormalNodes name clean + pure found + if found then evalTactic (← `(tactic| cil_try_simp_only [cil_literal])) open Lean Elab Tactic Meta in /-- State the goal with each decided `localResult` as the value it loads as @@ -668,15 +845,24 @@ elab "cil_local_results" : tactic => do withMainContext do let goal ← getMainGoal let target ← instantiateMVars (← goal.getType) - let reduced ← reduceLocalResults target - unless reduced == target do replaceMainGoal [← goal.replaceTargetDefEq reduced] + let name := "cil_local_results" + let known ← normalNodesFor name + let isLocalResult (term : Expr) := term.isConstOf ``CIL.Safety.localResult + let found ← if (← usableNormalNodes).isNone then pure (target.find? isLocalResult).isSome + else do + let (found, (_, clean)) := (cleanWalk isLocalResult known target).run ({}, #[]) + recordNormalNodes name clean + pure found + if found then + let reduced ← reduceLocalResults target known.contains + unless reduced == target do replaceMainGoal [← goal.replaceTargetDefEq reduced] /-- Normalises the literal `take`, `drop`, `reverse` and `++` a step leaves on the stack, evaluates its closed literal operands (`cil_literal_operands`) and states each decided local result as its value (`cil_local_results`); never fails. -/ macro "cil_stack_cleanup" : tactic => `(tactic| ( - try simp only [List.drop_succ_cons, List.drop_zero, List.drop_nil, + cil_try_simp_only [List.drop_succ_cons, List.drop_zero, List.drop_nil, List.take_succ_cons, List.take_zero, List.take_nil, List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append, List.append_nil] cil_literal_operands @@ -687,8 +873,19 @@ macro "cil_stack_cleanup" : tactic => definitions and `facts`, constructors and `rfl`. -/ macro "cil_checked_step_by_equations " "[" facts:term,* "]" : tactic => `(tactic| ( - simp only [cil_instruction_step, cil_step, cil_guard, CIL.Safety.instructionStep] - try simp only [cil_guard, cil_checked, cil_side, $[$facts:term],*] + cil_simp_only [cil_instruction_step, cil_step, cil_guard, CIL.Safety.instructionStep] + cil_try_simp_only [cil_guard, cil_checked, cil_side, $[$facts:term],*] + all_goals ((repeat constructor) <;> rfl))) + +/-- `cil_checked_step_by_equations` for a call-family step, whose equation reads the link at + its pc: `links`, the link facts, rewrite with the step equations, so only the linked arm is + simplified; `facts` apply after them as they do for any step. -/ +macro "cil_checked_linked_step_by_equations " "[" links:term,* "]" "[" facts:term,* "]" : + tactic => + `(tactic| ( + cil_simp_only [cil_instruction_step, cil_step, cil_guard, CIL.Safety.instructionStep, + $[$links:term],*] + cil_try_simp_only [cil_guard, cil_checked, cil_side, $[$facts:term],*] all_goals ((repeat constructor) <;> rfl))) /-- A matching instruction fact closes a step through its generic `@[cil_step]` equation. @@ -696,8 +893,8 @@ macro "cil_checked_step_by_equations " "[" facts:term,* "]" : tactic => rewrites past it; the opcode need not have a per-shape `@[cil_instruction_step]` equation. -/ macro "cil_checked_instruction_from_fact " "[" facts:term,* "]" : tactic => `(tactic| ( - simp only [cil_step, cil_guard, CIL.Safety.instructionStep] - try simp only [cil_guard, cil_checked, cil_side, $[$facts:term],*] + cil_simp_only [cil_step, cil_guard, CIL.Safety.instructionStep] + cil_try_simp_only [cil_guard, cil_checked, cil_side, $[$facts:term],*] all_goals ((repeat constructor) <;> rfl))) /-- `cil_checked_step_by_equations` for a `.memory` step, also rewriting `referenceAt` on @@ -705,13 +902,13 @@ macro "cil_checked_instruction_from_fact " "[" facts:term,* "]" : tactic => generic list would slow every other step. -/ macro "cil_checked_memory_by_equations " "[" facts:term,* "]" : tactic => `(tactic| ( - simp only [cil_step, cil_guard, CIL.Safety.instructionStep] - try simp only [cil_guard, cil_checked, cil_side, $[$facts:term],*] + cil_simp_only [cil_step, cil_guard, CIL.Safety.instructionStep] + cil_try_simp_only [cil_guard, cil_checked, cil_side, $[$facts:term],*] all_goals ((repeat constructor) <;> rfl))) /-- Normalize checked conditions with the shared closed rules, retaining unresolved goals. -/ macro "cil_checked_normalize " "[" facts:term,* "]" : tactic => - `(tactic| try simp only [cil_checked, cil_guard, cil_side, $[$facts:term],*]) + `(tactic| cil_try_simp_only [cil_checked, cil_guard, cil_side, $[$facts:term],*]) /-- Closes frame-entry and return premises with the closed checked-value rules and supplied facts. Returned objects retain formation when the exiting frame does not own them. -/ @@ -743,12 +940,14 @@ macro "cil_checked_step_equations " "[" facts:term,* "]" : tactic => `(tactic| cil_strict (cil_checked_step_by_equations [$[$facts:term],*])) /-- Closes a memory step with a matching supplied operation fact. Memory-dispatch facts - select the static dispatch equation explicitly before this tactic runs. -/ + select the static dispatch equation explicitly before this tactic runs. A home among the + operands is read from its frame slot first, so the fact meets the view it stands for. -/ macro "cil_checked_memory_from_fact " "[" facts:term,* "]" : tactic => `(tactic| ( - rw [CIL.Safety.step_memory_eq] - try simp only [cil_guard] - try simp only [cil_guard, cil_checked, cil_side, $[$facts:term],*] + rw [CIL.Safety.modelStep_memory] + cil_try_simp_only [cil_guard, CIL.Safety.viewOperands, CIL.Safety.spanView, + List.getElem?_cons_zero, List.getElem?_cons_succ, pure_bind] + cil_try_simp_only [cil_guard, cil_checked, cil_side, pure_bind, $[$facts:term],*] all_goals ((repeat constructor) <;> rfl))) open Lean Elab Tactic Meta in @@ -798,7 +997,7 @@ elab_rules : tactic operands are respelled as numerals to match it. -/ macro "cil_checked_intrinsic " "[" facts:term,* "]" : tactic => `(tactic| ( - simp only [cil_step, cil_guard, CIL.Intrinsic.available, List.length_cons, List.length_nil, + cil_simp_only [CIL.Safety.intrinsicCall, cil_step, cil_guard, CIL.Intrinsic.available, List.length_cons, List.length_nil, Nat.reduceAdd, Nat.reduceLT, Bool.not_true, Bool.not_false, Bool.false_eq_true, ite_false, ite_true, List.take_succ_cons, List.take_zero, List.take_nil, List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append, List.append_nil, @@ -806,7 +1005,7 @@ macro "cil_checked_intrinsic " "[" facts:term,* "]" : tactic => CIL.Safety.numericValue, Bind.bind, Except.bind, Pure.pure, Except.pure, BitVec.ofInt_ofNat, cil_eval, $[$facts:term],*] -- A fact that makes the intrinsic fault leaves no result for this pass to check. - all_goals try simp only [CIL.Safety.checkedIntrinsic, cil_guard, cil_eval, + all_goals cil_try_simp_only [CIL.Safety.checkedIntrinsic, cil_guard, cil_eval, List.any_cons, List.any_nil, Bool.false_or, Bool.true_or, Bool.or_false, Bool.false_eq_true, ite_false, CIL.Safety.checkedValue, CIL.Safety.numericValue, ite_true, Bind.bind, Except.bind, Pure.pure, Except.pure, $[$facts:term],*] @@ -816,7 +1015,7 @@ macro "cil_checked_intrinsic " "[" facts:term,* "]" : tactic => all_goals try ( cil_numeral_eval_fact [$[$facts:term],*] cil_intrinsic_pending - simp only [bitVecOfNatLiteral, CIL.Safety.checkedIntrinsic, cil_guard, cil_eval, + cil_simp_only [bitVecOfNatLiteral, CIL.Safety.checkedIntrinsic, cil_guard, cil_eval, List.any_cons, List.any_nil, Bool.false_or, Bool.true_or, Bool.or_false, Bool.false_eq_true, ite_false, CIL.Safety.checkedValue, CIL.Safety.numericValue, ite_true, Bind.bind, Except.bind, Pure.pure, Except.pure, $[$facts:term],*] @@ -831,7 +1030,7 @@ def cilReducedOp? (op : Expr) : MetaM (Option Expr) := do let reduced ← whnfD (← instantiateMVars op) let some name := reduced.getAppFn.constName? | return none let some (.ctorInfo info) := (← getEnv).find? name | return none - return if info.induct == ``CIL.Op then some reduced else none + return if info.induct == instructionTypeName then some reduced else none /-- One checked `step` goal, with facts already normalised: a supplied step fact for the fetched op, as given or with its `@[cil_code]` bodies unfolded, or else a strategy chosen @@ -859,27 +1058,23 @@ def stepFacts (target : Expr) (facts : Array Term) : TacticM (Array Term) := do -- A tactic `have` leaves the target under `mdata`, which `Expr.eq?` does not see through. let target := (← instantiateMVars target).consumeMData let some (_, lhs, _) := target.eq? | return #[] - let some #[body, opcode, _, _, _, _, _] := argumentsOf? lhs ``CIL.Safety.step 7 | return #[] - let head := (← whnfR opcode).getAppFn.constName? + let (action, arity, bodyIndex, opcodeIndex) := + if lhs.isAppOfArity ``CIL.Safety.modelStep 6 then (``CIL.Safety.modelStep, 6, 1, 0) + else (``CIL.Safety.step, 7, 0, 1) + let some arguments := argumentsOf? lhs action arity | return #[] + let body := arguments[bodyIndex]! + let head := (← whnfR arguments[opcodeIndex]!).getAppFn.constName? let states (type : Expr) : MetaM Bool := withNewMCtxDepth do let openParameters := type.hasMVar let (_, _, conclusion) ← forallMetaTelescopeReducing type let some (_, factLhs, _) := conclusion.eq? | return false - let some #[_, factOpcode, _, _, _, _, _] := argumentsOf? factLhs ``CIL.Safety.step 7 - | return false - match (← whnfR factOpcode).getAppFn.constName?, head with + let some factArguments := argumentsOf? factLhs action arity | return false + match (← whnfR factArguments[opcodeIndex]!).getAppFn.constName?, head with | some factHead, some goalHead => unless factHead == goalHead do return false | _, _ => pure () if openParameters then return true -- A named body may need its generated equation unfolded before application. - let mut arguments := factLhs.getAppArgs |>.set! 0 body - let operation ← whnfR factOpcode - if operation.isAppOfArity ``CIL.Op.local 2 || - operation.isAppOfArity ``CIL.Op.setLocal 2 then - let some index := argument? operation 0 | return false - arguments := arguments.set! 1 - (mkApp2 operation.getAppFn index - (mkApp (mkConst ``Option.none [Level.zero]) (mkConst ``CIL.NarrowKind))) + let arguments := factArguments.set! bodyIndex body withTransparency .instances <| isDefEq (mkAppN factLhs.getAppFn arguments) lhs let mut matching := #[] for fact in facts do @@ -888,25 +1083,56 @@ def stepFacts (target : Expr) (facts : Array Term) : TacticM (Array Term) := do else if ← suppliedFactSatisfies fact (fun type => forallTelescope type fun _ conclusion => pure (conclusion.isAppOfArity ``And 2)) then let proof ← Term.withoutErrToSorry <| Term.withSynthesize <| Term.elabTerm fact none - for part in ← equationConjuncts (·.isAppOfArity ``CIL.Safety.step 7) proof do + for part in ← equationConjuncts (·.isAppOfArity action arity) proof do if ← states (← inferType part) then matching := matching.push (← Term.exprToSyntax part) return matching open Lean Elab Tactic Meta in -/-- Close the goal with `fact` after normalizing named bodies and local annotations in its type. +/-- The checked shape of a named type operand proves its byte width at a typed step. -/ +elab "cil_step_value_size" : tactic => withMainContext do + let target := (← instantiateMVars (← getMainTarget)).consumeMData + let some (_, lhs, _) := target.eq? | throwError "Expected an object size equation" + let some shape := lhs.app1? ``CIL.ValueShape.size? | throwError "Expected a value shape size" + let some (.done resolved) ← ResolutionFacts.sharedShape? ``CIL.Method.valueShape shape + | throwError "No checked shape for the step operand" + let some proof := resolved.proof? | throwError "Missing value shape proof" + let sized ← mkCongrArg (mkConst ``CIL.ValueShape.size?) proof + unless ← withDefault (isDefEq (← inferType sized) target) do + throwError "The checked shape has a different size" + (← getMainGoal).assign sized + replaceMainGoal [] + +open Lean Elab Tactic Meta in +/-- A field or size premise of an operation fact follows from the fetched method's checked shape. -/ +elab "cil_step_shape" : tactic => withMainContext do + let target ← instantiateMVars (← getMainTarget) + let some (_, lhs, _) := target.consumeMData.eq? | throwError "Expected a checked shape equation" + if let some #[body, field] := argumentsOf? lhs ``CIL.Method.fieldShape 2 then + let some (_, proof) ← fieldShapeKnown? body field | throwError "No checked shape for the field" + unless ← withDefault (isDefEq (← inferType proof) target) do + throwError "The field has a different checked shape" + (← getMainGoal).assign proof + replaceMainGoal [] + else evalTactic (← `(tactic| cil_step_value_size)) + +open Lean Elab Tactic Meta in +/-- Close the goal with `fact` after unfolding the named bodies in its type. Fails when the supplied normalization does not change the type. -/ def applyUnfoldedStepFact (unfold : Simp.Context) (simprocs : Simp.SimprocsArray) (fact : Term) : TacticM Unit := withMainContext do let proof ← Term.withoutErrToSorry <| Term.withSynthesize <| Term.elabTerm fact none let type ← instantiateMVars (← inferType proof) let (result, _) ← simp type unfold simprocs - if result.expr == type then throwError "The step fact needs no body or annotation normalization" + if result.expr == type then throwError "The step fact names no body to unfold" let proof ← match result.proof? with | some eq => mkEqMP eq proof | none => mkExpectedTypeHint proof result.expr - let goals ← withTransparency .instances <| (← getMainGoal).apply proof - unless goals.isEmpty do throwError "The step fact left goals" + -- Lookup may also unfold a supplied definition inside the body, such as a local initializer. + let goals ← withDefault <| (← getMainGoal).apply proof + for goal in goals do + let remaining ← Tactic.run goal <| evalTactic (← `(tactic| cil_step_shape)) + unless remaining.isEmpty do throwError "The step fact left a premise" replaceMainGoal [] open Lean Elab Tactic Meta in @@ -921,25 +1147,84 @@ private partial def dispatchFactMatches (wanted : Expr) (function : Name) (type unless lhs.isAppOf function do return false withTransparency .instances <| isDefEq lhs wanted +open Lean Meta in +/-- `stack` with each home among its top `count` entries replaced by the view its slot in + `locals` holds, as `viewOperands` reads it. A home whose slot is no literal view stays. -/ +private partial def viewedOperands (locals : Array (Nat × Expr)) (count : Nat) (stack : Expr) : + MetaM Expr := do + if count == 0 then return stack + let cell ← whnfR stack + let some #[type, head, tail] := argumentsOf? cell ``List.cons 3 | return stack + let rest ← viewedOperands locals (count - 1) tail + let viewed ← do + let some #[slot] := argumentsOf? (← whnfR head) ``CIL.Safety.Value.spanHome 1 | pure head + let some index ← evalNat slot | pure head + let some (_, home) := locals.find? (·.1 == index) | pure head + let some #[_, _, held] := argumentsOf? (← whnfR home) ``CIL.Safety.LocalSlot.span 3 + | pure head + let some #[_, view] := argumentsOf? (← whnfR held) ``Option.some 2 | pure head + pure (mkApp (mkConst ``CIL.Safety.Value.bufferSpan) view) + return mkApp3 cell.getAppFn type viewed rest + +open Lean Meta in +/-- The operands a memory step on `stack` hands `operation`: each home `viewOperands` reads + stands for the view its slot of the literal `frame` holds. A stack without a home is + returned unchanged. -/ +def viewedStack (frame operation stack : Expr) : MetaM Expr := do + if (stack.find? (·.isConstOf ``CIL.Safety.Value.spanHome)).isNone then return stack + let count := mkApp (mkConst ``CIL.Safety.viewOperandCount) operation + let some count ← evalNat (← whnfD count) | return stack + let some #[_, locals, _, _] := argumentsOf? (← whnfR frame) ``CIL.Safety.Frame.mk 4 + | return stack + viewedOperands (← listEntries locals) count stack + open Lean Elab Tactic Meta in /-- Supplied dispatch facts for this operation, stack and memory, including facts inside a conjunction. Matching fresh quantified variables never instantiates the caller's goal. -/ def dispatchFacts (step : Expr) (function : Name) (facts : Array Term) : TacticM (Array Term) := do - let arguments := step.getAppArgs - let (some body, some opcode, some pc, some stack, some memory) := - (arguments[0]?, arguments[1]?, arguments[2]?, arguments[5]?, arguments[6]?) - | return #[] - let operation ← whnfR opcode - let wanted ← if function == ``CIL.Safety.instruction then - mkAppM function #[operation, stack, memory] - else if let some operand := lastArgument? operation then + if facts.isEmpty then return #[] + let wanted ← if function == ``CIL.Safety.instruction then do + let some #[_, opcode, _, _, _, stack, memory] := argumentsOf? step ``CIL.Safety.step 7 + | return #[] + mkAppM function #[← whnfR opcode, stack, memory] + else do + let some #[model, body, pc, frame, stack, memory] := + argumentsOf? step ``CIL.Safety.modelStep 6 | return #[] + let some #[_, value] := argumentsOf? model ``Option.some 2 | return #[] + let some #[operand] := argumentsOf? value ``CIL.RuntimeModel.memory 1 | return #[] + let operands ← viewedStack frame operand stack if function == ``CIL.Safety.staticInstruction then - mkAppM function #[body, pc, operand, stack, memory] - else mkAppM function #[operand, stack, memory] - else return #[] + mkAppM function #[body, pc, operand, operands, memory] + else mkAppM function #[operand, operands, memory] facts.filterM (suppliedFactSatisfies · (dispatchFactMatches wanted function)) +open Lean Elab Tactic Meta in +/-- The result of a static read at `pc` of a named body, from the body's `@[cil_code]` site + fact: an initialized load's value, or an address's storage reference. Nothing for a literal + body, a pc without a site fact, or an opaque value, which a caller's fact must give. -/ +def stepStaticFact? (body pc memory : Expr) (load : Bool) : TacticM (Option Expr) := + withMainContext do + if (← instantiateMVars body).isAppOf ``CIL.Method.mk then return none + let read := mkApp2 (mkConst ``CIL.Method.staticAt) body pc + let normalize ← `(tactic| simp only [cil_code]) + let { ctx, simprocs, .. } ← mkSimpContext normalize (eraseLocal := false) + let (result, _) ← simp read ctx simprocs + unless result.expr.isAppOfArity ``Option.some 2 do return none + let some site := result.proof? | return none + let site ← Term.exprToSyntax site + let memory ← Term.exprToSyntax memory + let fact ← if load then + `(CIL.Safety.staticLoad_initialized (memory := $memory) $site rfl rfl rfl) + else `(CIL.Safety.staticAddress_site (memory := $memory) $site) + let saved ← saveState + try + let proof ← Term.withoutErrToSorry <| Term.withSynthesize <| Term.elabTerm fact none + let proof ← instantiateMVars proof + if proof.hasMVar then saved.restore; return none + return some proof + catch _ => saved.restore; return none + open Lean Meta in /-- Expose a concrete stack spine without reducing the values carried by its cells. -/ private partial def exposeStackSpine (stack : Expr) : MetaM Expr := withIncRecDepth do @@ -950,55 +1235,189 @@ private partial def exposeStackSpine (stack : Expr) : MetaM Expr := withIncRecDe return mkApp withHead (← exposeStackSpine tail) open Lean Elab Tactic Meta in -elab_rules : tactic - | `(tactic| cil_checked_step_normalized [$facts:term,*]) => withMainContext do - let initial := (← instantiateMVars (← getMainTarget)).consumeMData - let mut localOperation := false - if let some (_, step, _) := initial.eq? then - if step.isAppOfArity ``CIL.Safety.step 7 && - (argument? step 1).any (·.isConstOf ``CIL.Op.nop) then - evalTactic (← `(tactic| exact step_nop_eq ..)) - return - if let some #[_, opcode, _, _, _, _, _] := argumentsOf? step ``CIL.Safety.step 7 then - let operation ← whnfR opcode - localOperation := operation.isAppOfArity ``CIL.Op.local 2 || - operation.isAppOfArity ``CIL.Op.setLocal 2 - if localOperation then - let narrow ← lastArgument "cil_checked_step_normalized" operation - unless (← whnfR narrow).isAppOf ``Option.none do - evalTactic (← `(tactic| simp only [step_local_annotation, step_setLocal_annotation])) - if (← getGoals).isEmpty then return - -- A body reached through a lookup hypothesis stays named; one reached by unfolding a - -- `cil_code` program is already a literal, so a fact on the named body needs its own - -- `cil_code` unfolded to match it. - let candidates ← stepFacts (← getMainTarget) facts.getElems +/-- A caller's equation for the exact link or model lookup of the current instruction. -/ +private def suppliedReadFact? (read : Expr) (facts : Array Term) : TacticM (Option Expr) := do + for fact in facts do + if ← suppliedFactSatisfies fact fun type => do + let some (_, lhs, _) := type.consumeMData.eq? | return false + unless lhs.getAppFn == read.getAppFn do return false + withNewMCtxDepth <| withDefault <| isDefEq lhs read + then + return some (← Term.withoutErrToSorry (Term.elabTerm fact none)) + return none + +open Lean Elab Tactic Meta in +/-- A proof of `body.linkAt pc = link`, the link a call-family step reads, with `link` a + constructor: a literal body's by evaluation, a named body's by its `@[cil_code]` fact and link + definition, and a named body without one by evaluating its table. Nothing when none decides + it, so a caller's fact must. -/ +def stepLinkFact? (body pc : Expr) (facts : Array Term := #[]) : TacticM (Option Expr) := withMainContext do + let read := mkApp2 (mkConst ``CIL.Method.linkAt) body pc + let byEvaluation (link : Expr) : TacticM Expr := do + mkExpectedTypeHint (← mkEqRefl read) (← mkEq read link) + if let some link ← literalLink? read then return some (← byEvaluation link) + if (← instantiateMVars body).isAppOf ``CIL.Method.mk then return ← suppliedReadFact? read facts + let normalize ← `(tactic| simp only [cil_code]) + let { ctx, simprocs, .. } ← mkSimpContext normalize (eraseLocal := false) + let (result, _) ← simp read ctx simprocs + let link ← whnfR result.expr + if link.isAppOfArity ``Option.some 2 || link.isAppOfArity ``Option.none 1 then + -- A fact proved by `rfl` rewrites without a proof term; the hint restores the read it states. + return some (← mkExpectedTypeHint (← result.getProof) (← mkEq read result.expr)) + -- Facts cover every extracted call site, so only a hand-written table is evaluated here. + let link ← withDefault <| whnf read + match_expr link with + | Option.none _ => return some (← byEvaluation link) + | Option.some _ value => + let value ← withDefault <| whnf value + unless value.getAppFn.isConst do return none + return some (← byEvaluation (mkApp2 (mkConst ``Option.some [Level.zero]) + (mkConst ``CIL.Link) value)) + | _ => suppliedReadFact? read facts + +open Lean Meta in +/-- Whether `fact` is an equation whose right side is `none`: a link fact for a pc with no link, + or a model fact for a method with no runtime model. -/ +def statesNone (fact : Expr) : MetaM Bool := + return ((← inferType fact).eq?.map (·.2.2.isAppOf ``Option.none)).getD false + +open Lean Elab Tactic Meta in +/-- The runtime model of a call of `method` in `body` as a fact: the `cil_runtime_models` fact + of a named body, or the table's evaluation for a literal one, which may give no model. Nothing + when neither decides it. -/ +def stepModelFact? (body method : Expr) (opcode := ``CIL.CallOpcode.call) (facts : Array Term := #[]) : + TacticM (Option Expr) := withMainContext do + let read := mkApp3 (mkConst ``CIL.Extraction.runtimeModel) + (mkApp (mkConst ``CIL.Method.environment) body) (mkConst opcode) method + let normalize ← `(tactic| simp only [cil_code, CIL.Safety.reduceRuntimeModel]) + let { ctx, simprocs, .. } ← mkSimpContext normalize (eraseLocal := false) + let (result, _) ← simp read ctx simprocs + let model ← whnfR result.expr + unless model.isAppOfArity ``Option.some 2 || model.isAppOfArity ``Option.none 1 do + return ← suppliedReadFact? read facts + return some (← mkExpectedTypeHint (← result.getProof) (← mkEq read result.expr)) + +/-- Opcodes whose step reads the body's link at the step's pc. -/ +def linkedOpcodes : List Lean.Name := + [``CIL.Instruction.call, ``CIL.Instruction.callvirt, + ``CIL.Instruction.newobj, ``CIL.Instruction.calli] + +open Lean Elab Tactic Meta in +/-- Apply a supplied step fact at the current instruction, unfolding named bodies as needed. -/ +private def applyCheckedStepFact (facts : Array Term) : TacticM Bool := do + -- A lookup can expose either a named body or its literal. A fact about the named body + -- matches the literal after unfolding its cil_code definitions. + let candidates ← stepFacts (← getMainTarget) facts + for fact in candidates do + let saved ← saveState + try + evalTactic (← `(tactic| cil_strict + ((with_reducible_and_instances apply $fact) <;> cil_step_shape))) + return true + catch _ => saved.restore + if !candidates.isEmpty then + let normalize ← `(tactic| simp only [cil_code]) + let { ctx, simprocs, .. } ← mkSimpContext normalize (eraseLocal := false) for fact in candidates do let saved ← saveState try - evalTactic (← `(tactic| cil_strict ((with_reducible_and_instances apply $fact); done))) - return + applyUnfoldedStepFact ctx simprocs fact + return true catch _ => saved.restore - if !candidates.isEmpty then - let normalize ← if localOperation then - `(tactic| simp only [cil_code, step_local_annotation, step_setLocal_annotation]) - else `(tactic| simp only [cil_code]) - let { ctx, simprocs, .. } ← mkSimpContext normalize (eraseLocal := false) - for fact in candidates do - let saved ← saveState - try - applyUnfoldedStepFact ctx simprocs fact + return false + +open Lean Elab Tactic Meta in +elab_rules : tactic + | `(tactic| cil_checked_step_normalized [$facts:term,*]) => withMainContext do + let initial := (← instantiateMVars (← getMainTarget)).consumeMData + if let some (_, step, _) := initial.eq? then + if step.isAppOfArity ``CIL.Safety.step 7 then + let op := step.getArg! 1 + if op.isConstOf ``CIL.Instruction.nop then + evalTactic (← `(tactic| exact step_nop_eq ..)) + return + if !facts.getElems.isEmpty && (← applyCheckedStepFact facts.getElems) then return + if let some (_, action, _) := initial.eq? then + if let some #[model, _, _, _, _, _] := argumentsOf? action ``CIL.Safety.modelStep 6 then + if let some #[_, value] := argumentsOf? model ``Option.some 2 then + if value.isAppOf ``CIL.RuntimeModel.memory then + let tactic ← if !(← dispatchFacts action ``staticInstruction facts.getElems).isEmpty then + `(tactic| cil_checked_memory_from_fact [$facts:term,*]) + else if !(← dispatchFacts action ``memoryInstruction facts.getElems).isEmpty then + `(tactic| cil_checked_memory_from_fact [CIL.Safety.staticInstruction, $facts:term,*]) + else `(tactic| cil_checked_memory_by_equations [$facts:term,*]) + evalTactic tactic + return + if let some (_, step, result) := initial.eq? then + if let some #[body, op, pc, args, frame, stack, memory] := + argumentsOf? step ``CIL.Safety.step 7 then + -- Use the named body's field equation for an address after the caller's step facts. + -- A scalar aggregate can retain a symbolic value from its layout's encode equation. + if op.isAppOf ``CIL.Instruction.ldfld || op.isAppOf ``CIL.Instruction.stfld then + let receiver := (argumentsOf? stack ``List.cons 3).bind fun entries => + if op.isAppOf ``CIL.Instruction.ldfld then entries[1]? + else ((argumentsOf? entries[2]! ``List.cons 3).bind (·[1]?)) + if receiver.any (fun value => value.isAppOf ``Value.reference || value.isAppOf ``Value.readOnlyReference) then + let saved ← saveState + try + evalTactic (← `(tactic| cil_checked_step_equations [$facts,*])) + return + catch _ => saved.restore + if op.isAppOfArity ``CIL.Instruction.ldobj 1 then + let saved ← saveState + try + evalTactic (← `(tactic| ( + first + | (refine Eq.trans (step_ldobj_primitive _ _ _ _ _ (by first | assumption | rfl) _) ?_ + simp [transfer_scalar_reference_eq, transfer_scalar_readOnlyReference_eq, + cil_guard, cil_checked, $[$facts:term],*] <;> rfl) + | exact step_ldobj_aggregate_of_snapshot _ _ _ _ _ false _ + (by first | assumption | rfl) (by solve_by_elim only [$[$facts:term],*]) + | exact step_ldobj_aggregate_of_snapshot _ _ _ _ _ true _ + (by first | assumption | rfl) (by solve_by_elim only [$[$facts:term],*]) + | refine Eq.trans (step_ldobj_aggregate_address_eq _ _ _ _ _ _ + (by first | assumption | rfl) _ _) ?_ + | refine Eq.trans (step_ldobj_aggregate_readOnlyAddress_eq _ _ _ _ _ _ + (by first | assumption | rfl) _ _) ?_ + | exact step_ldobj_aggregate_null_eq _ _ _ _ _ _ (by first | assumption | rfl) _ + | exact step_ldobj_aggregate_readOnlyNull_eq _ _ _ _ _ _ + (by first | assumption | rfl) _ + all_goals simp [cil_guard, cil_checked, $[$facts:term],*] <;> rfl))) + return + catch _ => saved.restore + if op.isAppOfArity ``CIL.Instruction.stobj 1 then + let saved ← saveState + try + evalTactic (← `(tactic| ( + first + | (refine Eq.trans (step_stobj_primitive _ _ _ _ _ (by first | assumption | rfl) _) ?_ + solve_by_elim only [$[$facts:term],*]) + | refine Eq.trans (step_stobj_initialized_eq _ _ _ _ _ _ (by first | assumption | rfl) _ _ _) ?_ + | refine Eq.trans (step_stobj_snapshot_eq _ _ _ _ _ _ _ (by first | assumption | rfl) _ _ _) ?_ + all_goals simp [cil_guard, cil_checked, referenceAt, checkedAt, $[$facts:term],*] <;> rfl))) + return + catch _ => saved.restore + if let some equation ← typedObjectStep? body op pc args frame stack memory then + let some (_, _, resolved) := (← inferType equation).eq? | throwError "expected a step equation" + let stated ← withLocalDeclD `stepped (← inferType step) fun stepped => do + mkLambdaFVars #[stepped] (← mkEq stepped result) + let rewritten ← mkCongrArg stated equation + let target ← mkEq resolved result + replaceMainGoal [← (← getMainGoal).replaceTargetEq target rewritten] + evalTactic (← `(tactic| cil_checked_step_normalized [$facts:term,*])) return - catch _ => saved.restore if let some (_, pending, _) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? then let opcodeHead := (argument? pending 1).bind (·.getAppFn.constName?) if pending.isAppOfArity ``CIL.Safety.step 7 && opcodeHead.any - ([``CIL.Op.brzero, ``CIL.Op.brnonzero, ``CIL.Op.beq, ``CIL.Op.bne, - ``CIL.Op.blt, ``CIL.Op.bltu, ``CIL.Op.ble, ``CIL.Op.bleu, ``CIL.Op.bgt, - ``CIL.Op.bgtu, ``CIL.Op.bge, ``CIL.Op.bgeu].contains ·) then + ([``CIL.Instruction.brfalse, ``CIL.Instruction.brtrue, ``CIL.Instruction.beq, + ``CIL.Instruction.bne_un, ``CIL.Instruction.blt, ``CIL.Instruction.blt_un, + ``CIL.Instruction.ble, ``CIL.Instruction.ble_un, ``CIL.Instruction.bgt, + ``CIL.Instruction.bgt_un, ``CIL.Instruction.bge, + ``CIL.Instruction.bge_un].contains ·) then -- A matching truth equation must rewrite before its definition can erase that head. let mut truthDefinitions : Array Term := #[← `(term| CIL.truth)] - if opcodeHead.any ([``CIL.Op.brzero, ``CIL.Op.brnonzero].contains ·) then + if opcodeHead.any ([``CIL.Instruction.brfalse, ``CIL.Instruction.brtrue].contains ·) then let stackTerm ← argumentAt "cil_checked_step_normalized" pending 5 let stack ← whnfD stackTerm if let some #[_, value, _] := argumentsOf? stack ``List.cons 3 then @@ -1013,12 +1432,12 @@ elab_rules : tactic -- Numerals use the same spelling as the normalized branch facts, and a comparison of -- two numerals is decided, so a literal operand leaves a numeral pc. evalTactic (← `(tactic| cil_strict ( - simp only [cil_code, Nat.reduceAdd, cil_step, cil_guard, cil_checked, cil_side, + cil_simp_only [cil_code, Nat.reduceAdd, cil_step, cil_guard, cil_checked, cil_side, BitVec.ofNat_eq_ofNat, BitVec.setWidth_ofNat_of_le, Nat.reduceLeDiff, BitVec.reduceEq, ↓reduceIte, $[$branchFacts:term],*] all_goals ((repeat constructor) <;> rfl)))) return - evalTactic (← `(tactic| try simp only [cil_code, Nat.reduceAdd])) + evalTactic (← `(tactic| cil_try_simp_only [cil_code, Nat.reduceAdd])) if (← getGoals).isEmpty then return -- A tactic `have` leaves the target under `mdata`, which `Expr.eq?` does not see through. let mut target := (← instantiateMVars (← getMainTarget)).consumeMData @@ -1037,10 +1456,15 @@ elab_rules : tactic unless args == lhs.getAppArgs do target ← mkEq (mkAppN lhs.getAppFn args) rhs replaceMainGoal [← (← getMainGoal).replaceTargetDefEq target] - let checkedOperation := if operation == ``CIL.Op.checkedArithmetic then - some (``CIL.checkedValues, ``CIL.Safety.step_checkedArithmetic_result) - else if operation == ``CIL.Op.checkedConvert then - some (``CIL.convertInteger, ``CIL.Safety.step_checkedConvert_result) + let checkedArithmetic := [``CIL.Instruction.add_ovf, ``CIL.Instruction.add_ovf_un, + ``CIL.Instruction.sub_ovf, ``CIL.Instruction.sub_ovf_un, ``CIL.Instruction.mul_ovf, + ``CIL.Instruction.mul_ovf_un].contains operation + let checkedOperation := if checkedArithmetic then + some (``CIL.checkedValues, ``CIL.Safety.step_checked_result) + else if operation == ``CIL.Instruction.conv_ovf then + some (``CIL.convertInteger, ``CIL.Safety.step_conv_ovf_result) + else if operation == ``CIL.Instruction.conv_ovf_un then + some (``CIL.convertInteger, ``CIL.Safety.step_conv_ovf_un_result) else none if let some (function, bridge) := checkedOperation then for fact in facts.getElems do @@ -1050,17 +1474,26 @@ elab_rules : tactic let saved ← saveState try let lemma := mkIdent bridge + -- The goal fixes the constructor, so its flags are decided once `apply` unifies. A + -- conversion's fact is checked once `apply` fixes the target, so its width reduces. + let bridged ← if checkedArithmetic then + `(tactic| (apply $lemma (calculated := $fact); case selected => rfl)) + else `(tactic| (apply $lemma; case calculated => exact $fact)) evalTactic (← `(tactic| cil_strict ( - apply $lemma $fact + $bridged:tactic all_goals cil_checked_side []))) return catch _ => saved.restore - let scalar := #[``CIL.Op.neg, ``CIL.Op.gt, ``CIL.Op.convI1, ``CIL.Op.convI2, - ``CIL.Op.convU2, ``CIL.Op.convI8, ``CIL.Op.convI4, ``CIL.Op.convU1, - ``CIL.Op.convU4, ``CIL.Op.convU8, ``CIL.Op.convU, ``CIL.Op.bnot, - ``CIL.Op.add, ``CIL.Op.sub, ``CIL.Op.mul, ``CIL.Op.band, ``CIL.Op.bor, - ``CIL.Op.bxor, ``CIL.Op.shl, ``CIL.Op.shr, ``CIL.Op.shrUn, ``CIL.Op.lt, - ``CIL.Op.ltu, ``CIL.Op.gtu, ``CIL.Op.eq].contains operation + let scalar := #[``CIL.Instruction.neg, ``CIL.Instruction.cgt, ``CIL.Instruction.conv_i1, + ``CIL.Instruction.conv_i2, ``CIL.Instruction.conv_u2, ``CIL.Instruction.conv_i8, + ``CIL.Instruction.conv_i4, ``CIL.Instruction.conv_u1, ``CIL.Instruction.conv_u4, + ``CIL.Instruction.conv_u8, ``CIL.Instruction.conv_u, ``CIL.Instruction.conv_i, + ``CIL.Instruction.not, + ``CIL.Instruction.add, ``CIL.Instruction.sub, ``CIL.Instruction.mul, + ``CIL.Instruction.and, ``CIL.Instruction.or, ``CIL.Instruction.xor, + ``CIL.Instruction.shl, ``CIL.Instruction.shr, ``CIL.Instruction.shr_un, + ``CIL.Instruction.clt, ``CIL.Instruction.clt_un, ``CIL.Instruction.cgt_un, + ``CIL.Instruction.ceq].contains operation if scalar && lhs.isAppOfArity ``CIL.Safety.step 7 then -- Reuse the selected value equation without rewriting its symbolic arithmetic result. let mut widths := #[] @@ -1092,31 +1525,140 @@ elab_rules : tactic try evalTactic (← `(tactic| cil_strict ( apply step_unspecified - simp only [observerCount, cil_guard, cil_side, $[$facts:term],*] + cil_simp_only [observerCount, cil_guard, cil_side, $[$facts:term],*] done))) return catch _ => saved.restore - let strategy ← if operation == ``CIL.Op.intrinsic then - `(tactic| cil_checked_intrinsic [$facts:term,*]) - else if operation == ``CIL.Op.memory then - let matchingStatic := - !(← dispatchFacts lhs ``CIL.Safety.staticInstruction facts.getElems).isEmpty - let matchingMemory := - !(← dispatchFacts lhs ``CIL.Safety.memoryInstruction facts.getElems).isEmpty - -- A supplied operation fact retains its abstraction; otherwise select its equation. - if matchingStatic then - `(tactic| cil_strict (cil_checked_memory_from_fact [$facts:term,*])) - else if matchingMemory then - `(tactic| cil_strict ( - cil_checked_memory_from_fact [CIL.Safety.staticInstruction, $facts:term,*])) - else - `(tactic| cil_strict (cil_checked_memory_by_equations [$facts:term,*])) + -- A static read rewrites by a fact built from the site fact; evaluating the read instead walks + -- the site table at every load. A hypothesis keeps the run's record, keyed by fact text, out. + let staticRead := if operation == ``CIL.Instruction.ldsfld then some ``CIL.Safety.staticLoad + else if operation == ``CIL.Instruction.ldsflda || operation == ``CIL.Instruction.ldtoken then + some ``CIL.Safety.staticAddress + else none + let facts : Syntax.TSepArray `term "," ← match staticRead with + | none => pure facts + | some read => + let supplied ← facts.getElems.anyM (suppliedFactSatisfies · fun type => + forallTelescope type fun _ conclusion => do + let some (_, left, _) := conclusion.consumeMData.eq? | return false + return left.isAppOf read) + let body ← entryAt "cil_checked_step_normalized" args 0 + let pc ← entryAt "cil_checked_step_normalized" args 2 + let memory ← entryAt "cil_checked_step_normalized" args 6 + let found ← if supplied then pure none + else stepStaticFact? body pc memory (read == ``CIL.Safety.staticLoad) + match found with + | none => pure facts + | some proof => + let (fact, goal) ← (← (← getMainGoal).assert (← mkFreshUserName `static) + (← inferType proof) proof).intro1P + replaceMainGoal [goal] + let factTerm ← goal.withContext (Term.exprToSyntax (mkFVar fact)) + pure (.ofElems (facts.getElems.push factTerm)) + -- A call-family step's arm matches on its link; the read's fact lets that match reduce. It + -- enters as a hypothesis, so the run's record, whose simp contexts are keyed by fact text and + -- would hand a later pc this pc's fact, is not consulted for this step. + let linkFacts : Array Term ← if linkedOpcodes.contains operation then + match (← stepLinkFact? (← entryAt "cil_checked_step_normalized" args 0) + (← entryAt "cil_checked_step_normalized" args 2) facts.getElems) with + | some proof => + let (link, goal) ← (← (← getMainGoal).assert (← mkFreshUserName `link) + (← inferType proof) proof).intro1P + replaceMainGoal [goal] + let linkFact ← goal.withContext (Term.exprToSyntax (mkFVar link)) + -- A call whose pc states no link runs its method's model, which a fact states. + let unlinked ← statesNone proof + let body ← entryAt "cil_checked_step_normalized" args 0 + let model ← if !unlinked then pure none else + match argumentsOf? args[1]! ``CIL.Instruction.call 2, + argumentsOf? args[1]! ``CIL.Instruction.newobj 1 with + | some #[method, _], _ => stepModelFact? body method (facts := facts.getElems) + | _, some #[method] => stepModelFact? body method ``CIL.CallOpcode.newobj facts.getElems + | _, _ => pure none + match model with + | none => pure #[linkFact] + | some model => + let (fact, goal) ← (← (← getMainGoal).assert (← mkFreshUserName `model) + (← inferType model) model).intro1P + replaceMainGoal [goal] + pure #[linkFact, ← goal.withContext (Term.exprToSyntax (mkFVar fact))] + | none => pure #[] + else pure #[] + let stepFacts := facts.getElems ++ linkFacts + -- A checked field can also step from the byte-level read selected by its shape. + let strategy ← if operation == ``CIL.Instruction.ldfld then + let hasRead ← facts.getElems.anyM fun fact => suppliedFactSatisfies fact fun type => do + let some (_, lhs, _) := type.getForallBody.consumeMData.eq? | return false + return lhs.isAppOf ``CIL.Safety.loadValueField + let equation ← `(tactic| cil_checked_step_equations [$[$stepFacts:term],*]) + let byRead ← `(tactic| ( + refine Eq.trans (step_ldfld_value_eq _ _ _ _ _ _ _ _ _ + (by first | cil_step_shape | assumption | rfl) _) ?_ + simp only [$[$stepFacts:term],*] <;> rfl)) + if !hasRead then `(tactic| first | $equation:tactic | $byRead:tactic) + else `(tactic| first | $byRead:tactic | $equation:tactic) + else if operation == ``CIL.Instruction.stfld then + `(tactic| first + | cil_checked_step_equations [$[$stepFacts:term],*] + | (refine Eq.trans (step_stfld_value_eq _ _ _ _ _ _ _ _ _ + (by first | assumption | cil_step_shape) _) ?_ + simp only [storeValueField, cil_guard, cil_checked, checkedAt, ordinaryAccessAlignment, + Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, $[$stepFacts:term],*] <;> rfl)) -- A fact about `instruction` keeps its abstraction only if tried before a per-shape -- equation rewrites past it. else if !(← dispatchFacts lhs ``CIL.Safety.instruction facts.getElems).isEmpty then `(tactic| cil_strict (cil_checked_instruction_from_fact [$facts:term,*])) + else if let #[link, model] := linkFacts then + let evaluate ← `(tactic| ( + refine Eq.trans (CIL.Safety.step_call_model _ _ _ _ _ $link $model rfl _) ?_ + rfl)) + let intrinsic ← `(tactic| ( + refine Eq.trans (CIL.Safety.step_call_intrinsic _ _ _ _ _ $link $model rfl) ?_ + cil_checked_intrinsic [$facts:term,*])) + -- Scalar results can reduce directly. Vector steps normalize lanes, and supplied + -- evaluation facts must rewrite before reduction unfolds their symbolic operands. + let preferIntrinsic ← (← getMainGoal).withContext do + let type ← inferType (← Term.elabTerm model none) + let some (_, _, result) := type.consumeMData.eq? | return false + let some #[_, value] := argumentsOf? result ``Option.some 2 | return false + let some #[op, _] := argumentsOf? value ``CIL.RuntimeModel.intrinsic 2 | return false + return !op.isAppOf ``CIL.Intrinsic.scalar || !facts.getElems.isEmpty + let (firstStep, secondStep) := if preferIntrinsic then (intrinsic, evaluate) else (evaluate, intrinsic) + `(tactic| cil_strict (first + | $firstStep:tactic + | $secondStep:tactic + | (apply CIL.Safety.step_call_skipInit_address _ _ _ _ _ $link $model rfl + assumption) + | (refine Eq.trans (CIL.Safety.step_call_feature _ _ _ _ _ $link $model rfl) ?_ + cil_try_simp_only [cil_step, cil_guard, cil_checked, cil_side, $[$facts:term],*, + ite_true, ite_false, Bool.true_eq_false, Bool.false_eq_true] + all_goals rfl) + | (refine Eq.trans (CIL.Safety.step_call_typedTransfer _ _ _ _ _ $link $model rfl _) ?_ + cil_simp_only [CIL.Safety.typedTransferCall, CIL.Safety.observedTransfer, + CIL.Safety.transferValues, cil_guard, cil_checked, $[$facts:term],*] + all_goals cil_checked_side [$facts:term,*]) + | (refine Eq.trans (CIL.Safety.step_newobj_memory _ _ _ _ _ $link $model rfl _) ?_ + first + | cil_checked_memory_from_fact [$facts:term,*] + | cil_checked_step_normalized [$facts:term,*] + | cil_checked_memory_by_equations [$facts:term,*]) + | (refine Eq.trans (CIL.Safety.step_call_model _ _ _ _ _ $link $model rfl _) ?_ + first + | cil_checked_memory_from_fact [$facts:term,*] + | cil_checked_step_normalized [$facts:term,*] + | cil_checked_memory_by_equations [$facts:term,*]) + | cil_checked_linked_step_by_equations [$link, $model] [$[$stepFacts:term],*])) + else if let (true, #[link]) := (operation == ``CIL.Instruction.call, linkFacts) then + -- A directly linked call rewrites by one equation that never mentions the model. + `(tactic| cil_strict (first + | (refine Eq.trans (CIL.Safety.step_call_direct _ _ _ _ _ $link _) ?_ + cil_checked_linked_step_by_equations [] [$[$stepFacts:term],*]) + | cil_checked_linked_step_by_equations [$link] [$[$stepFacts:term],*])) + else if linkedOpcodes.contains operation then + `(tactic| cil_strict + (cil_checked_linked_step_by_equations [$[$linkFacts:term],*] [$[$stepFacts:term],*])) else - `(tactic| cil_checked_step_equations [$facts:term,*]) + `(tactic| cil_checked_step_equations [$[$stepFacts:term],*]) evalTactic strategy /-- One checked `step` goal: normalises `facts`, then runs `cil_checked_step_normalized`. -/ @@ -1157,11 +1699,67 @@ def numericValueFacts (facts : Array Term) : TacticM (Array Term) := withMainCon catch _ => saved.restore return restated +open Lean Meta in +/-- Whether a body the goal or its hypotheses reach may hold `stind.i`: some `stind` there names + `.i` or a kind that is not a literal, or a body is a variable or has no definition to read. + Reads each reached body once, so the cost is linear in the code it reaches. -/ +def mayStoreNativeInt (roots : Array Expr) : MetaM Bool := do + let bodyType (type : Expr) : MetaM Bool := forallTelescopeReducing type fun _ result => do + let result ← whnfR result + let element := if result.isAppOfArity ``List 1 then result.appArg! else result + return element.isConstOf ``CIL.Method || element.isConstOf ``CIL.Instruction + let narrowKinds := [``CIL.NumericKind.i1, ``CIL.NumericKind.u1, ``CIL.NumericKind.i2, + ``CIL.NumericKind.u2, ``CIL.NumericKind.i4, ``CIL.NumericKind.u4, ``CIL.NumericKind.i8] + let mut pending := roots + let mut seen : NameSet := {} + while !pending.isEmpty do + let term ← instantiateMVars pending.back! + pending := pending.pop + if term.hasMVar then return true + let masked := term.replace fun part => + if part.isAppOfArity ``CIL.Instruction.stind 1 && + narrowKinds.any part.appArg!.isConstOf then some (mkConst ``Unit.unit) else none + if (masked.find? (·.isConstOf ``CIL.Instruction.stind)).isSome then return true + for free in (Lean.collectFVars {} term).fvarIds do + if ← bodyType (← free.getType) then return true + for name in term.getUsedConstants do + if seen.contains name then continue + seen := seen.insert name + let info ← getConstInfo name + unless ← bodyType info.type do continue + match info with + | .ctorInfo _ | .recInfo _ | .inductInfo _ | .quotInfo _ => continue + | .defnInfo definition => pending := pending.push definition.value + | _ => return true + return false + open Lean Elab Tactic Meta in /-- Derive checked load and store facts from byte operations once, before stepping. The - byte width selects the existing bridge; quantified facts retain their binders. A numeric - value test of an opaque value is also stated in its unfolded form (`numericValueFacts`). -/ + byte width selects the existing bridge; quantified facts retain their binders. An eight-byte + write gives a `stind.i` step only when a reached body may hold one (`mayStoreNativeInt`). A + numeric value test of an opaque value is also stated in its unfolded form + (`numericValueFacts`). -/ def prepareOperationFacts (facts : Array Term) : TacticM (Array Term) := withMainContext do + let roots := (← getLCtx).foldl (init := #[← getMainTarget]) fun roots declaration => + if declaration.isImplementationDetail then roots else roots.push declaration.type + -- Only an eight-byte write needs the opcode scan. Its answer is shared by all such facts. + let nativeStoreCache ← IO.mkRef (none : Option Bool) + let nativeStores : MetaM Bool := do + if let some answer ← nativeStoreCache.get then return answer + let answer ← mayStoreNativeInt roots + nativeStoreCache.set (some answer) + return answer + -- `mkAppM` leaves an auto-parameter as a premise of the derived fact, so each width side + -- condition is decided here; a premise left in place blocks the step the fact states. + let narrowStores (width : Nat) (kind : Name) (written : Expr) : + MetaM (Array (Name × Array Expr)) := do + let byteWidth := mkNatLit width + let scalarWidth ← mkDecideProof (mkOr (← mkEq byteWidth (mkNatLit 1)) + (mkOr (← mkEq byteWidth (mkNatLit 2)) (← mkEq byteWidth (mkNatLit 4)))) + let kindWidth := mkApp (mkConst ``CIL.NumericKind.width) (mkConst kind) + let narrowKind ← mkDecideProof (mkNot (← mkEq kindWidth (mkNatLit 8))) + return #[(``transfer_i32_of_write, #[written, scalarWidth]), + (``step_stind_narrow_of_write, #[mkConst kind, written, narrowKind])] let operationEquation (operation : Expr) := operation.isAppOfArity ``CIL.Safety.read 4 || operation.isAppOfArity ``CIL.Safety.readSnapshot 4 || @@ -1210,18 +1808,29 @@ def prepareOperationFacts (facts : Array Term) : TacticM (Array Term) := withMai candidates := candidates ++ #[(``load_scalar_of_read, #[scalar, applied]), (``load_scalar_signed_of_read, #[scalar, applied])] else - let bridge := match reading, width with - | true, 8 => ``load_word64_of_read - | true, 16 => ``load_vector128_of_read - | true, 32 => ``load_vector256_of_read - | true, 64 => ``load_vector512_of_read - | false, 8 => ``step_store64_of_write - | false, 1 | false, 2 | false, 4 => ``step_storeScalar_of_write - | false, 16 => ``step_store128_of_write - | false, 32 => ``step_store256_of_write - | false, 64 => ``step_store512_of_write - | _, _ => Name.anonymous - unless bridge.isAnonymous do candidates := candidates.push (bridge, #[applied]) + -- A narrow write steps a runtime-model store or the one `stind` of its width. + let narrowKind? := match width with + | 1 => some ``CIL.NumericKind.i1 + | 2 => some ``CIL.NumericKind.i2 + | 4 => some ``CIL.NumericKind.i4 + | _ => none + let narrow ← match reading, narrowKind? with + | false, some kind => narrowStores width kind applied + | _, _ => pure #[] + let includeNative ← if !reading && width == 8 then nativeStores else pure false + let bridges : Array (Name × Array Expr) := match reading, width with + | true, 8 => #[(``load_word64_of_read, #[applied])] + | true, 16 => #[(``load_vector128_of_read, #[applied])] + | true, 32 => #[(``load_vector256_of_read, #[applied])] + | true, 64 => #[(``load_vector512_of_read, #[applied])] + | false, 8 => #[(``transfer_i64_of_write, #[applied]), + (``step_stind_i8_of_write, #[applied])] ++ + if includeNative then #[(``step_stind_i_of_write, #[applied])] else #[] + | false, 16 => #[(``step_store128_of_write, #[applied])] + | false, 32 => #[(``step_store256_of_write, #[applied])] + | false, 64 => #[(``step_store512_of_write, #[applied])] + | _, _ => #[] + candidates := candidates ++ narrow ++ bridges let mut results := #[] for (bridge, arguments) in candidates do let before ← getMCtx @@ -1229,7 +1838,11 @@ def prepareOperationFacts (facts : Array Term) : TacticM (Array Term) := withMai let result ← mkAppM bridge arguments let result ← instantiateMVars (← mkLambdaFVars variables result) if result.hasMVar then setMCtx before - else results := results.push result + else + -- Unification can match bytes of another width through a structure projection; + -- such a fact is ill-typed and the kernel rejects a proof that binds it. + check result + results := results.push result catch _ => setMCtx before return results for result in derived do @@ -1238,15 +1851,20 @@ def prepareOperationFacts (facts : Array Term) : TacticM (Array Term) := withMai return prepared ++ (← numericValueFacts facts) open Lean Elab Tactic in -/-- Normalise `CIL.truth` and `facts` once for a stepping loop, then build its tactic from the - raw and normalised facts; per-step normalisation would re-simp every local fact on every - instruction. -/ +/-- `CIL.truth` and `facts` normalised once for a stepping loop; per-step normalisation would + re-simp every local fact on every instruction. -/ +def cilNormalisedFacts (facts : Array Term) : TacticM (Array Term) := + withMainContext do + normaliseStepFacts (← `(Lean.Parser.Tactic.optConfig|)) + (← prepareOperationFacts (#[← `(CIL.truth)] ++ facts)) + +open Lean Elab Tactic in +/-- Normalise `facts` with `cilNormalisedFacts`, then build a stepping loop's tactic from the + raw and normalised facts. -/ def cilWithNormalisedFacts (facts : Array Term) (body : Array Term → Array Term → TacticM Syntax) : TacticM Unit := withMainContext do - let prepared ← - normaliseStepFacts (← `(Lean.Parser.Tactic.optConfig|)) - (← prepareOperationFacts (#[← `(CIL.truth)] ++ facts)) + let prepared ← cilNormalisedFacts facts withMainContext <| evalTactic (← body facts prepared) open Lean Elab Tactic in @@ -1274,7 +1892,7 @@ syntax "cil_checked_steps_normalized" "[" term,* "]" : tactic macro "cil_checked_return_at " methodFound:term:max instructionFound:term:max "[" facts:term,* "]" : tactic => `(tactic| ( - cil_strict refine Eq.trans (CIL.Safety.run_return $methodFound $instructionFound + cil_strict cil_refine_by Eq.trans (CIL.Safety.run_return $methodFound $instructionFound (by cil_checked_step_normalized [$[$facts:term],*]) (by cil_checked_side [$[$facts:term],*])) ?_ try simp only [CIL.Safety.leaveFrame, List.foldl_nil, List.nil_append, List.append_nil, @@ -1290,7 +1908,7 @@ macro "cil_checked_return_at " methodFound:term:max instructionFound:term:max macro "cil_checked_next_at " methodFound:term:max instructionFound:term:max "[" facts:term,* "]" : tactic => `(tactic| ( - cil_strict refine Eq.trans (CIL.Safety.run_next $methodFound $instructionFound + cil_strict cil_refine_by Eq.trans (CIL.Safety.run_next $methodFound $instructionFound (by cil_checked_step_normalized [$[$facts:term],*])) ?_ cil_stack_cleanup)) @@ -1298,7 +1916,7 @@ macro "cil_checked_next_at " methodFound:term:max instructionFound:term:max located fault fixes the fault the step must raise. -/ macro "cil_checked_fault_at " methodFound:term:max instructionFound:term:max "[" facts:term,* "]" : tactic => - `(tactic| cil_strict exact CIL.Safety.run_fault $methodFound $instructionFound + `(tactic| cil_strict cil_refine_by CIL.Safety.run_fault $methodFound $instructionFound (by cil_checked_step_normalized [$[$facts:term],*])) /-- Close a child invocation with supplied exact-run or invocation-certificate evidence. -/ @@ -1359,10 +1977,11 @@ elab_rules : tactic macro "cil_checked_call_open_at " methodFound:term:max instructionFound:term:max "[" facts:term,* "]" : tactic => `(tactic| ( - cil_strict refine CIL.Safety.run_call_of_invoke $methodFound $instructionFound + cil_strict cil_refine_by CIL.Safety.run_call_of_invoke $methodFound $instructionFound (by cil_checked_step_normalized [$[$facts:term],*]) (by first | cil_checked_child [$[$facts:term],*] | (cil_checked_steps_normalized [$[$facts:term],*]; done)) ?_ + try (conv => lhs; arg 4; cil_literal_nat) cil_stack_cleanup)) open Lean Elab Tactic Meta in @@ -1424,21 +2043,26 @@ def cilOutputFacts (operation : Name) : TacticM (Array Term) := withMainContext let some execution := (← instantiateMVars (← getMainTarget)).find? (·.isAppOfArity ``run 8) | return #[] let #[_, _, _, _, _, _, stack, memory] := execution.getAppArgs | return #[] - if operation == ``CIL.Op.ret && (← whnfD stack).isAppOf ``List.nil then return #[] - let currentFacts := withMainContext do + if operation == ``CIL.Instruction.ret && (← whnfD stack).isAppOf ``List.nil then return #[] + -- Facts remembered below join the context after `retained` was collected, so they are + -- found by a second scan. + let currentFacts (markers : Array LocalDecl) : TacticM (Array Term) := withMainContext do let mut facts := #[] - for declaration in ← getLCtx do - if declaration.userName.eraseMacroScopes == `cilOutputFact && - (← loopFactMemories declaration.type).contains memory then + for declaration in markers do + if (← loopFactMemories declaration.type).contains memory then facts := facts.push (← Term.exprToSyntax declaration.toExpr) return facts - let facts ← currentFacts + let facts ← currentFacts retained unless facts.isEmpty do return facts for declaration in calls do let type ← instantiateMVars declaration.type if type.isAppOfArity `CIL.Safety.ValidCall 3 && argument? type 0 == some memory then cilRememberOutputFacts (← Term.exprToSyntax declaration.toExpr) - return ← currentFacts + let remembered ← withMainContext do + return (← getLCtx).foldl (init := #[]) fun found declaration => + if declaration.userName.eraseMacroScopes == `cilOutputFact then found.push declaration + else found + return ← currentFacts remembered return #[] open Lean Elab Tactic Meta in @@ -1450,16 +2074,22 @@ def cilOnlyInputFacts (operation : Name) (facts : Array Term) : TacticM Bool := if left.isAppOf ``form || left.isAppOf ``read then return true let some #[_, op, _, _, _, _, _] := argumentsOf? left ``step 7 | return false -- Only known reads are excluded; a generic or custom step retains its priority. - return op.isAppOf ``CIL.Op.fieldAt && operation != ``CIL.Op.fieldAt + return op.isAppOf ``CIL.Instruction.ldfld && operation != ``CIL.Instruction.ldfld open Lean Elab Tactic Meta in /-- Materialize a checked output-word write in the outer continuation. Only the fetched store and a `ValidCall` at its current memory are considered; permissions remain real premises. -/ def cilPrepareOutputStore (instructionFound : Term) (operation : Name) (facts : Array Term) : TacticM (Array Term) := withMainContext do - unless [``CIL.Op.store64, ``CIL.Op.storeScalar, ``CIL.Op.setFieldAt].contains operation do + unless [``CIL.Instruction.stind, ``CIL.Instruction.stobj, + ``CIL.Instruction.stfld].contains operation do return facts unless (← getEnv).contains `CIL.Safety.ValidCall.store_word do return facts + let calls ← ((← getLCtx).decls.toArray.filterMap id).filterMapM fun declaration => do + let type ← instantiateMVars declaration.type + let some #[callMemory, _, outputs] := argumentsOf? type `CIL.Safety.ValidCall 3 | return none + return some (declaration, callMemory, outputs) + if calls.isEmpty then return facts let some execution := (← instantiateMVars (← getMainTarget)).find? (·.isAppOfArity ``run 8) | return facts let #[_, _, _, _, _, _, stackTerm, memory] := execution.getAppArgs | return facts @@ -1478,19 +2108,27 @@ def cilPrepareOutputStore (instructionFound : Term) (operation : Name) let some fetchedOp := lastArgument? found | return facts let opcode ← whnf fetchedOp let mut offset := mkNatLit 0 - if operation == ``CIL.Op.storeScalar then - let some width := lastArgument? opcode | return facts + if operation == ``CIL.Instruction.stobj then + let some code := instruction.find? (·.isAppOfArity ``CIL.Method.code 1) | return facts + let some type := lastArgument? opcode | return facts + let shape := mkApp2 (mkConst ``CIL.Method.valueShape) code.appArg! type + let some (.done resolved) ← ResolutionFacts.sharedShape? ``CIL.Method.valueShape shape + | return facts + let some #[width, _] := argumentsOf? resolved.expr ``CIL.ValueShape.primitive 2 | return facts unless ← isDefEq width (mkNatLit 8) do return facts - if operation == ``CIL.Op.setFieldAt then - let some width := lastArgument? opcode | return facts + if operation == ``CIL.Instruction.stind then + let some kind := lastArgument? opcode | return facts + unless (← isDefEq kind (mkConst ``CIL.NumericKind.i8)) || + (← isDefEq kind (mkConst ``CIL.NumericKind.i)) do return facts + if operation == ``CIL.Instruction.stfld then + let some code := instruction.find? (·.isAppOfArity ``CIL.Method.code 1) | return facts + let some field := lastArgument? opcode | return facts + let some (shape, _) ← fieldShapeKnown? code.appArg! field | return facts + let some #[fieldOffset, width, _] := argumentsOf? shape ``CIL.FieldShape.value 3 | return facts unless ← isDefEq width (mkNatLit 8) do return facts - let some fieldOffset := (appliedFunction? opcode).bind lastArgument? | return facts offset := fieldOffset let offsetTerm ← Term.exprToSyntax offset - let declarations := (← getLCtx).decls.toArray.filterMap id - for declaration in declarations do - let type ← instantiateMVars declaration.type - let some #[callMemory, _, outputs] := argumentsOf? type `CIL.Safety.ValidCall 3 | continue + for (declaration, callMemory, outputs) in calls do unless ← withNewMCtxDepth <| isDefEq callMemory memory do continue for (position, view) in ← listEntries outputs do let view ← whnf view @@ -1500,14 +2138,9 @@ def cilPrepareOutputStore (instructionFound : Term) (operation : Name) try let call ← Term.exprToSyntax declaration.toExpr let view ← Term.exprToSyntax view - let context ← getLCtx - let bases := ["stored", "addressed", "written", "readback", "storedCall"] - let named (index : Nat) (base : String) := - Name.mkSimple (if index == 0 then base else s!"{base}_{index}") - let mut index := 0 - while bases.any (fun base => context.usesUserName (named index base)) do - index := index + 1 - let names := bases.toArray.map (fun base => mkIdent (named index base)) + let bases := #["stored", "addressed", "written", "readback", "storedCall"] + let index := firstUnusedSuffix (← getLCtx) bases + let names := bases.map (fun base => mkIdent (suffixedName index base)) let #[after, addressed, written, readback, retained] := names | throwError "cilPrepareOutputStore: expected a name for each of {bases}" let store := Syntax.mkApp (mkCIdent `CIL.Safety.ValidCall.store_word) @@ -1553,16 +2186,34 @@ elab_rules : tactic · cil_checked_side [$facts,*] · cil_checked_side [$facts,*]))) -/-- Whether an opcode steps to a call action: `call`, or `callvirt` with a non-null receiver. -/ +/-- Whether an opcode steps to a call action: `call` without `tail.`, or `callvirt` with a + non-null receiver. The driver has no tail-transfer step, so a tail call fails here. -/ def callsCallee (operation : Lean.Name) : Bool := - operation == ``CIL.Op.call || operation == ``CIL.Op.callInstance || - operation == ``CIL.Op.callVirtual + operation == ``CIL.Instruction.call || operation == ``CIL.Instruction.callvirt + +open Lean Elab Tactic Meta in +/-- The link lookup for this instruction proves that it has no callee. -/ +def unlinkedPc (body pc : Expr) (facts : Array Term := #[]) : TacticM Bool := withMainContext do + let some fact ← stepLinkFact? body pc facts | return false + statesNone fact + +open Lean Elab Tactic Meta in +/-- A known empty link selects runtime model lookup for a call or constructor at the fetched pc. -/ +def unlinkedModelSite (operation : Name) (instructionFound : Term) (facts : Array Term) : TacticM Bool := + withMainContext do + unless operation == ``CIL.Instruction.call || operation == ``CIL.Instruction.newobj do + return false + let found ← instantiateMVars (← inferType (← Term.elabTerm instructionFound none)) + let some (_, lookup, _) := found.consumeMData.eq? | return false + let some (code, pc) := indexedLookup? lookup | return false + let some #[body] := argumentsOf? code ``CIL.Method.code 1 | return false + unlinkedPc body pc facts open Lean Elab Tactic Meta in /-- One `run` step: prove the method and instruction lookups once, then apply only the composition lemma for the fetched opcode. A call proves its callee and leaves the caller's continuation as the goal. Toward a stated fault, a step that raises it ends the run. -/ -def cilRunStep (facts : Array Term) : TacticM Unit := withMainContext do +def cilRunStepNodes (facts : Array Term) : TacticM Unit := withMainContext do -- `run_fault` can close only at the pc the stated fault names; elsewhere it only costs. let target := (← instantiateMVars (← getMainTarget)).consumeMData let faulting ← match target.eq? with @@ -1580,16 +2231,21 @@ def cilRunStep (facts : Array Term) : TacticM Unit := withMainContext do | none => pure false let (methodSyntax, instructionSyntax, operation) ← cilFetchedOp ``CIL.Safety.run_next (fun e => e.eq?.map (·.2.1)) 3 facts - -- An object construction runs its constructor as a call before continuing in the caller. - let constructs := operation == ``CIL.Op.newObject - -- These fault before their callee runs: a short heap budget, a null receiver, or a receiver - -- whose class a dispatch table omits. - let faultsFirst := constructs || operation == ``CIL.Op.callInstance || - operation == ``CIL.Op.callVirtual - evalTactic (← if operation == ``CIL.Op.ret then + -- A construction under an `.object` link runs its constructor as a call before continuing in + -- the caller; under an `.aggregate` link it steps on as any other instruction does. + let constructs := operation == ``CIL.Instruction.newobj + let modelled ← unlinkedModelSite operation instructionSyntax facts + -- These fault before their callee runs: a call site the link table omits, a short heap + -- budget, a null receiver, or a receiver whose class a dispatch table omits. + let faultsFirst := constructs || callsCallee operation + evalTactic (← if operation == ``CIL.Instruction.ret then `(tactic| cil_checked_return_at $methodSyntax $instructionSyntax [$facts,*]) - else if callsCallee operation || constructs then - let call ← `(tactic| cil_checked_call_open_at $methodSyntax $instructionSyntax [$facts,*]) + else if (callsCallee operation || constructs) && !modelled then + let call ← if constructs then + `(tactic| first + | cil_checked_call_open_at $methodSyntax $instructionSyntax [$facts,*] + | cil_checked_next_at $methodSyntax $instructionSyntax [$facts,*]) + else `(tactic| cil_checked_call_open_at $methodSyntax $instructionSyntax [$facts,*]) if faulting && faultsFirst then `(tactic| first | cil_checked_fault_at $methodSyntax $instructionSyntax [$facts,*] @@ -1602,6 +2258,12 @@ def cilRunStep (facts : Array Term) : TacticM Unit := withMainContext do else `(tactic| cil_checked_next_at $methodSyntax $instructionSyntax [$facts,*])) +open Lean Elab Tactic in +/-- `cilRunStepNodes` on the record of normal-form nodes of the running driver, so that a + simplification leaves alone what earlier steps visited and left unchanged. -/ +def cilRunStep (facts : Array Term) : TacticM Unit := + withNormalNodes facts (cilRunStepNodes facts) + /-- One `run` step of the goal, chosen by the fetched opcode; at a call it proves the callee and leaves the caller's continuation. -/ syntax "cil_checked_run_step" "[" term,* "]" : tactic @@ -1614,9 +2276,10 @@ macro_rules `(tactic| ( try (conv => lhs; arg 2; cil_reduce_fuel) cil_checked_entry [$[$facts:term],*] - cil_steps_from "cil_checked_steps took no step: the method or instruction lookup, or the \ - first step, did not close; pass the lookup hypothesis, feature facts or step facts" - cil_checked_run_step [$[$facts:term],*])) + cil_normal_run + cil_steps_from "cil_checked_steps took no step: the method or instruction lookup, or the \ + first step, did not close; pass the lookup hypothesis, feature facts or step facts" + cil_checked_run_step [$[$facts:term],*])) /-- Proves an `invoke` or `run` goal, returning or stating a located fault, by checked stepping with `facts` and the object evidence facts of the context, such as `live.formed` of @@ -1647,10 +2310,11 @@ elab_rules : tactic let prepared ← prepareStepFacts facts.getElems -- Focus the original goal: an early close must error, not step into an unrelated pending goal. focus do - for taken in [:count.getNat] do - if (← getGoals).isEmpty then - throwError "cil_checked_steps {count.getNat}: the goal closed after {taken} steps" - cilRunStep prepared + withNormalRun do + for taken in [:count.getNat] do + if (← getGoals).isEmpty then + throwError "cil_checked_steps {count.getNat}: the goal closed after {taken} steps" + cilRunStep prepared -- Fold the stepped pc (`2 + 1`) so the residual matches a target stated with a numeral. unless (← getGoals).isEmpty do evalTactic (← `(tactic| try (conv => lhs; arg 4; simp only [Nat.reduceAdd]))) diff --git a/CIL/Safety/TupleDivision.lean b/CIL/Safety/TupleDivision.lean index 5a9a80b15..80bdcada3 100644 --- a/CIL/Safety/TupleDivision.lean +++ b/CIL/Safety/TupleDivision.lean @@ -21,25 +21,25 @@ theorem byteNumber_numberBytes_word (word : CIL.W64) : /-- `ldfld Item1` on a returned `(ulong, ulong)` reads the first word. -/ theorem item1_of_tuple (first second : CIL.W64) (rest : List Value) (m : Memory) : - instruction (.fieldAt 0 8) + loadValueField 0 8 false (.scalar (.aggregate (numberBytes first.toNat 8 ++ numberBytes second.toNat 8)) :: rest) m = .ok (m, .scalar (.i64 first) :: rest) := by have taken : ((numberBytes first.toNat 8 ++ numberBytes second.toNat 8).drop 0).take 8 = numberBytes first.toNat 8 := by simp [numberBytes_length] - simp only [instruction, List.length_append, numberBytes_length, taken, + simp only [loadValueField, List.length_append, numberBytes_length, taken, byteNumber_numberBytes_word, BitVec.ofNat_toNat, BitVec.setWidth_eq] rfl /-- `ldfld Item2` on a returned `(ulong, ulong)` reads the second word. -/ theorem item2_of_tuple (first second : CIL.W64) (rest : List Value) (m : Memory) : - instruction (.fieldAt 8 8) + loadValueField 8 8 false (.scalar (.aggregate (numberBytes first.toNat 8 ++ numberBytes second.toNat 8)) :: rest) m = .ok (m, .scalar (.i64 second) :: rest) := by have taken : ((numberBytes first.toNat 8 ++ numberBytes second.toNat 8).drop 8).take 8 = numberBytes second.toNat 8 := by simp [numberBytes_length, List.take_of_length_le] - simp only [instruction, List.length_append, numberBytes_length, taken, + simp only [loadValueField, List.length_append, numberBytes_length, taken, byteNumber_numberBytes_word, BitVec.ofNat_toNat, BitVec.setWidth_eq] rfl @@ -50,11 +50,11 @@ theorem divRem_fields {lower upper divisor : CIL.W64} (fits : upper.toNat < divi ∃ quotient remainder : CIL.W64, checkedIntrinsic (.x86Base .divRem64) [.i64 lower, .i64 upper, .i64 divisor] = .ok (.aggregate (numberBytes quotient.toNat 8 ++ numberBytes remainder.toNat 8)) ∧ - instruction (.fieldAt 0 8) + loadValueField 0 8 false (.scalar (.aggregate (numberBytes quotient.toNat 8 ++ numberBytes remainder.toNat 8)) :: rest) m = .ok (m, .scalar (.i64 quotient) :: rest) ∧ - instruction (.fieldAt 8 8) + loadValueField 8 8 false (.scalar (.aggregate (numberBytes quotient.toNat 8 ++ numberBytes remainder.toNat 8)) :: rest) m = .ok (m, .scalar (.i64 remainder) :: rest) ∧ @@ -87,7 +87,7 @@ theorem mathDivRem32_eq (signed : Bool) (left right : CIL.W32) : /-- `ldfld Item1` on a returned `(int, int)` reads the quotient's four bytes back exactly. -/ theorem item1_of_int_tuple (first second : CIL.W32) (rest : List Value) (m : Memory) : - instruction (.fieldAt 0 4) + loadValueField 0 4 false (.scalar (.aggregate (numberBytes first.toNat 4 ++ numberBytes second.toNat 4)) :: rest) m = .ok (m, .scalar (.i32 first) :: rest) := by @@ -97,7 +97,7 @@ theorem item1_of_int_tuple (first second : CIL.W32) (rest : List Value) (m : Mem have number : byteNumber (numberBytes first.toNat 4) = first.toNat := by rw [byteNumber_numberBytes] exact Nat.mod_eq_of_lt first.isLt - simp only [instruction, List.length_append, numberBytes_length, taken, number, BitVec.ofNat_toNat, + simp only [loadValueField, List.length_append, numberBytes_length, taken, number, BitVec.ofNat_toNat, BitVec.setWidth_eq] rfl diff --git a/CIL/Safety/TypedCopy.lean b/CIL/Safety/TypedCopy.lean index 225bc1864..abee57d6b 100644 --- a/CIL/Safety/TypedCopy.lean +++ b/CIL/Safety/TypedCopy.lean @@ -1,10 +1,10 @@ -import CIL.Safety.InstructionMemory +import CIL.Safety.StepComposition import CIL.Safety.AccessRequirements /-! # Typed copies -What a successful `cpobj` (`copyObject`) leaves in memory: each destination byte holds the +What a successful `cpobj` leaves in memory: each destination byte holds the matching source byte, now marked initialized. Consumers use this to read copied structs back without unfolding the instruction. -/ @@ -13,13 +13,28 @@ namespace CIL.Safety /-- Every successful typed copy initializes each destination byte to the corresponding byte in the source snapshot, for arbitrary memories and sizes. -/ -theorem copyObject_writes_snapshot (m result : Memory) (source destination : Reference) +theorem cpobj_writes_snapshot (m result : Memory) (source destination : Reference) (size index : Nat) (rest : List Value) (within : index < size) - (copied : instruction (.copyObject size) + {body : Method} {type : Extraction.TypeExpr} {pc : Nat} {args : List Value} {frame : Frame} + (width : (body.valueShape type).size? = some size) + (stepped : step body (.cpobj type) pc args frame (.reference (.address source) :: .reference (.address destination) :: rest) m = - .ok (result, rest)) : + .ok (.next (pc + 1) rest frame result)) : result.cells destination.allocation (destination.offset + index) = ⟨(m.cells source.allocation (source.offset + index)).bits, true⟩ := by + rw [step_cpobj_eq body pc args frame m width] at stepped + let operands := .reference (.address source) :: .reference (.address destination) :: rest + have copied : copyInitializedBytes size operands m = .ok (result, rest) := by + change (do + let (updated, values) ← copyInitializedBytes size operands m + pure (FrameAction.next (pc + 1) values frame updated)) = .ok _ at stepped + cases executed : copyInitializedBytes size operands m with + | error _ => rw [executed] at stepped; cases stepped + | ok pair => + cases pair + rw [executed] at stepped + cases stepped + rfl change (if source.allocation = destination.allocation && source.offset != destination.offset && source.offset < destination.offset + size && destination.offset < source.offset + size then .error (.unsupported "Partially overlapping typed copy") else do @@ -44,5 +59,5 @@ theorem copyObject_writes_snapshot (m result : Memory) (source destination : Ref have initialized := write_initializes _ _ _ _ _ index hw (by simpa using within) simpa using initialized -#print axioms copyObject_writes_snapshot +#print axioms cpobj_writes_snapshot end CIL.Safety diff --git a/CIL/Safety/UnknownHomes.lean b/CIL/Safety/UnknownHomes.lean index 6380db883..b9e2a6a78 100644 --- a/CIL/Safety/UnknownHomes.lean +++ b/CIL/Safety/UnknownHomes.lean @@ -163,7 +163,7 @@ theorem CompleteWritableHomes.home_at {memory : Memory} {lower : Nat} {kinds slo obtain ⟨reference, slot⟩ := homes.complete index kind specified exact homes.writable.home_at index kind specified ⟨kind, reference, slot⟩ -/-- Entering a method with only numeric `.unmodeled` locals and no vector arguments, +/-- Entering a method with only numeric `.unmodeled` locals and no parameter homes or opaque vector locals succeeds, keeps the caller's memory below `nextIdentity` and gives writable homes for every local. -/ theorem unknown_frame_setup (body : CIL.Method) @@ -174,7 +174,7 @@ theorem unknown_frame_setup (body : CIL.Method) (notPointer : ∀ kind ∈ body.localKinds, ∀ signature, kind ≠ .methodPointer signature) (notTable : ∀ kind ∈ body.localKinds, ∀ signature, kind ≠ .methodTable signature) (notObject : ∀ kind ∈ body.localKinds, kind ≠ .object) - (arguments : body.vectorArgs = []) (parameters : body.parameterHomes = []) + (parameters : body.parameterHomes = []) (opaqueLocals : body.opaqueVectorLocals = []) (memory : Memory) (args : List Value) (wellFormed : memory.WellFormed) : ∃ frame result, @@ -184,10 +184,10 @@ theorem unknown_frame_setup (body : CIL.Method) obtain ⟨slots, owned, result, made, homes⟩ := make_unknown_locals memory memory.nextIdentity body.localKinds wellFormed numeric notSpan notArray notPointer notTable notObject - let frame : Frame := ⟨memory.nextIdentity, slots, owned, [], 0⟩ + let frame : Frame := ⟨memory.nextIdentity, slots, owned, 0⟩ have setup : enterFrame body args memory = .ok (frame, result) := by simp only [enterFrame, opaqueLocals, makeSelectedLocals, List.isEmpty_nil, ite_true, - initializers, made, arguments, makeArgumentHomes, parameters, makeParameterHomes, + initializers, made, parameters, makeParameterHomes, List.map_nil, Bind.bind, Except.bind, Pure.pure, Except.pure, List.append_nil] rfl diff --git a/CIL/Safety/ValueBitCasts.lean b/CIL/Safety/ValueBitCasts.lean index f8019c7aa..1ff2872e7 100644 --- a/CIL/Safety/ValueBitCasts.lean +++ b/CIL/Safety/ValueBitCasts.lean @@ -89,12 +89,8 @@ theorem writeSnapshot_cell (m result : Memory) (r : Reference) (bytes : List (BitVec 8 × Bool)) (alignment i : Nat) (written : writeSnapshot m r bytes alignment = .ok result) (bound : i < bytes.length) : result.cells r.allocation (r.offset + i) = ⟨bytes[i].1, bytes[i].2⟩ := by - unfold writeSnapshot at written - cases hw : write m r (bytes.map Prod.fst) alignment <;> - simp only [hw, Bind.bind, Except.bind, Pure.pure, Except.pure] at written - · cases written - · cases written - simp [List.getElem?_eq_getElem bound] + rw [writeSnapshot_cells_eq written] + simp [List.getElem?_eq_getElem bound] /-- Reading back a stored snapshot returns exactly its bytes and flags. -/ theorem writeSnapshot_readback (m result : Memory) (r : Reference) @@ -123,7 +119,7 @@ theorem writeSnapshot_readback (m result : Memory) (r : Reference) · simp · intro i left right simp only [List.getElem_map, List.getElem_range] - simp [List.getElem?_eq_getElem right] + simp [snapshotCellAt_eq, List.getElem?_eq_getElem right] #print axioms vector128_bytes_roundtrip #print axioms vector256_bytes_roundtrip diff --git a/CIL/Safety/ValueRenaming.lean b/CIL/Safety/ValueRenaming.lean index 547170494..b113ec267 100644 --- a/CIL/Safety/ValueRenaming.lean +++ b/CIL/Safety/ValueRenaming.lean @@ -1,5 +1,6 @@ import CIL.Safety.AllocationRenaming import CIL.Safety.Frames +import CIL.Safety.LocalIndexMap /-! # Values under allocation renaming @@ -18,7 +19,8 @@ def ManagedReference.Below (limit : Nat) : ManagedReference → Prop /-- Every reference carried by a checked value belongs to the allocated identity prefix. -/ def Value.Below (limit : Nat) : Value → Prop - | .scalar _ | .methodPointer _ | .methodTable _ => True + -- A home names a slot of the frame whose stack holds it, not an allocation. + | .typeHandle _ | .runtimeType _ | .scalar _ | .methodPointer _ | .methodTable _ | .spanHome _ => True | .reference address | .readOnlyReference address | .span address _ | .object address => address.Below limit | .bufferSpan view => view.reference.Below limit @@ -36,6 +38,8 @@ def arrayView (mapping : AllocationRenaming) (view : ArrayView) : ArrayView := /-- Rename every allocation-backed part of a checked value, leaving scalar bits unchanged. -/ def value (mapping : AllocationRenaming) : Value → Value + | .typeHandle type => .typeHandle type + | .runtimeType type => .runtimeType type | .methodPointer pointer => .methodPointer pointer | .methodTable table => .methodTable table | .scalar scalar => .scalar scalar @@ -45,6 +49,7 @@ def value (mapping : AllocationRenaming) : Value → Value | .span reference length => .span (mapping.managedReference reference) length | .bufferSpan view => .bufferSpan (mapping.spanView view) | .array view => .array (mapping.arrayView view) + | .spanHome slot => .spanHome slot /-- A bounded managed reference stays fixed when the allocation map extends. -/ theorem Extends.managedReference {earlier later : AllocationRenaming} @@ -81,7 +86,7 @@ theorem value_below (mapping : AllocationRenaming) (value : Value) (bound : value.Below mapping.sourceLimit) : (mapping.value value).Below mapping.targetLimit := by cases value with - | scalar _ | methodPointer _ | methodTable _ => trivial + | typeHandle _ | runtimeType _ | scalar _ | methodPointer _ | methodTable _ | spanHome _ => trivial | reference reference | readOnlyReference reference | span reference _ | object reference => exact mapping.managedReference_below reference bound | bufferSpan view => exact mapping.managedReference_below view.reference bound @@ -117,4 +122,86 @@ theorem AllocationRenaming.Extends.values {earlier later : AllocationRenaming} exact extension.value value (bounds value member) #print axioms AllocationRenaming.Extends.values +/-- A stack value of a frame whose locals `indices` relocates: a home names the relocated slot, + and every other value is renamed as `mapping.value` renames it. -/ +def LocalIndexMap.value (indices : LocalIndexMap) (mapping : AllocationRenaming) : Value → Value + | .spanHome slot => .spanHome (indices.index slot) + | .typeHandle type => .typeHandle type + | .runtimeType type => .runtimeType type + | .methodPointer pointer => .methodPointer pointer + | .methodTable table => .methodTable table + | .scalar scalar => .scalar scalar + | .reference reference => .reference (mapping.managedReference reference) + | .object reference => .object (mapping.managedReference reference) + | .readOnlyReference reference => .readOnlyReference (mapping.managedReference reference) + | .span reference length => .span (mapping.managedReference reference) length + | .bufferSpan view => .bufferSpan (mapping.spanView view) + | .array view => .array (mapping.arrayView view) + +/-- A value that is not a home is renamed as `mapping.value` renames it. -/ +theorem LocalIndexMap.value_of_not_home (indices : LocalIndexMap) (mapping : AllocationRenaming) + {value : Value} (free : value.isHome = false) : + indices.value mapping value = mapping.value value := by + cases value <;> first | rfl | cases free +#print axioms LocalIndexMap.value_of_not_home + +/-- A list without homes, such as call arguments or results, is renamed by `mapping.value`. -/ +theorem LocalIndexMap.values_of_not_home (indices : LocalIndexMap) (mapping : AllocationRenaming) + {values : List Value} (free : ∀ value ∈ values, value.isHome = false) : + values.map (indices.value mapping) = values.map mapping.value := + List.map_congr_left fun _ member => indices.value_of_not_home mapping (free _ member) +#print axioms LocalIndexMap.values_of_not_home + +/-- Pushing a value that is not a home onto a renamed stack renames the whole stack. -/ +theorem LocalIndexMap.push_of_not_home (indices : LocalIndexMap) (mapping : AllocationRenaming) + {value : Value} (rest : List Value) (free : value.isHome = false) : + mapping.value value :: rest.map (indices.value mapping) = + (value :: rest).map (indices.value mapping) := by + rw [List.map_cons, indices.value_of_not_home mapping free] +#print axioms LocalIndexMap.push_of_not_home + +/-- Renaming keeps whether a value is a home. -/ +theorem LocalIndexMap.value_isHome (indices : LocalIndexMap) (mapping : AllocationRenaming) + (value : Value) : (indices.value mapping value).isHome = value.isHome := by + cases value <;> rfl +#print axioms LocalIndexMap.value_isHome + +/-- Renaming keeps whether a value exposes unspecified scalar bits. -/ +theorem LocalIndexMap.hasUnspecified (indices : LocalIndexMap) (mapping : AllocationRenaming) + (value : Value) : (indices.value mapping value).hasUnspecified = value.hasUnspecified := by + cases value <;> rfl +#print axioms LocalIndexMap.hasUnspecified + +/-- The unspecified-operand guard sees the same scalar operands after renaming. -/ +theorem LocalIndexMap.unspecifiedOperands (indices : LocalIndexMap) (mapping : AllocationRenaming) + (count : Nat) (values : List Value) : + unspecifiedOperands count (values.map (indices.value mapping)) = + unspecifiedOperands count values := by + simp only [CIL.Safety.unspecifiedOperands, ← List.map_take, List.any_map, + Function.comp_def, indices.hasUnspecified] +#print axioms LocalIndexMap.unspecifiedOperands + +/-- A renamed value lies below the target allocation watermark. -/ +theorem LocalIndexMap.value_below (indices : LocalIndexMap) (mapping : AllocationRenaming) + (value : Value) (bound : value.Below mapping.sourceLimit) : + (indices.value mapping value).Below mapping.targetLimit := by + cases value <;> first | trivial | exact mapping.value_below _ bound +#print axioms LocalIndexMap.value_below + +/-- A value held across a nested call keeps its renamed representation. -/ +theorem LocalIndexMap.value_extends (indices : LocalIndexMap) {earlier later : AllocationRenaming} + (extension : earlier.Extends later) (value : Value) (bound : value.Below earlier.sourceLimit) : + indices.value later value = indices.value earlier value := by + cases value <;> first | rfl | exact extension.value _ bound +#print axioms LocalIndexMap.value_extends + +/-- Extending the allocation map leaves every bounded stack value of a suspended frame + unchanged. -/ +theorem LocalIndexMap.values_extends (indices : LocalIndexMap) + {earlier later : AllocationRenaming} (extension : earlier.Extends later) (values : List Value) + (bounds : ∀ value ∈ values, value.Below earlier.sourceLimit) : + values.map (indices.value later) = values.map (indices.value earlier) := + List.map_congr_left fun value member => indices.value_extends extension value (bounds _ member) +#print axioms LocalIndexMap.values_extends + end CIL.Safety diff --git a/CIL/Safety/ValueSteps.lean b/CIL/Safety/ValueSteps.lean index 48a5970f4..5c42e62fd 100644 --- a/CIL/Safety/ValueSteps.lean +++ b/CIL/Safety/ValueSteps.lean @@ -1,14 +1,14 @@ import CIL.Safety.StepComposition import CIL.Safety.GuardNormalization +import CIL.Safety.ResolutionFacts /-! # Value step equations `@[cil_step]` equations for the value-only ops (arithmetic, comparisons, conversions, -branches, `switch`, `nop`, explicit throws and feature tests), one per op and i32/i64 operand shape. +branches, `switch`, `nop` and explicit throws), one per op and i32/i64 operand shape. `cil_value_steps` generates them as the library builds by evaluating the dispatcher once, so -stepping rewrites by constructor instead of unfolding `CIL.step` in every proof. A feature -test on a known feature is then reduced to the profile field that a caller's fact names. +stepping rewrites by constructor instead of unfolding `CIL.step` in every proof. -/ namespace CIL.Safety @@ -29,8 +29,8 @@ elab_rules : command let widths := match arity.getNat with | 0 => [[]] | 1 => [[false], [true]] - -- Top of stack first: equal widths, and an i32 count over an i64 value for shifts. - | _ => [[false, false], [true, true], [false, true]] + -- Top of stack first: equal widths and both mixed operand orders. + | _ => [[false, false], [true, true], [false, true], [true, false]] let params ← (paramName.zip paramType).mapM fun (name, type) => `(Lean.Parser.Term.bracketedBinderF| ($name : $type)) let paramTerms : Array Term := paramName.map fun name => ⟨name.raw⟩ @@ -75,39 +75,75 @@ elab_rules : command ``Except.map, ``Bind.bind, ``Except.bind] (← getSimpTheorems)]) let mut declared := #[] for op in ops.getElems do - let checked := #[`checkedArithmetic, `checkedConvert, `divide].contains op.getId + let checked := #[`div, `div_un, `rem, `rem_un, `add_ovf, `add_ovf_un, `sub_ovf, + `sub_ovf_un, `mul_ovf, `mul_ovf_un, `conv_ovf, `conv_ovf_un].contains op.getId let select := if checked then checkedSelect else select let cleanup := if checked then checkedCleanup else cleanup for shape in widths do let names := (if shape.length = 1 then [`v] else [`b, `a]).take shape.length |>.map mkIdent - let operands ← (names.zip shape).toArray.mapM fun (name, wide) => - if wide then `(Lean.Parser.Term.bracketedBinderF| ($name : CIL.W64)) - else `(Lean.Parser.Term.bracketedBinderF| ($name : CIL.W32)) - let stack ← (names.zip shape).foldrM (init := (rest : Term)) fun (name, wide) tail => - if wide then `(Value.scalar (CIL.Value.i64 $name) :: $tail) - else `(Value.scalar (CIL.Value.i32 $name) :: $tail) - let opName := mkIdent (`CIL.Op ++ op.getId) + let opName := mkIdent (instructionTypeName ++ op.getId) let opTerm ← `(($opName $paramTerms*)) - let statement ← `(∀ (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) - (memory : Memory) ($rest : List Value) $params* $operands*, - step body $opTerm pc args frame $stack memory = - step body $opTerm pc args frame $stack memory) + -- The equation at this shape; `asValue` types every operand as an unknown `Value`. + let statementOver (asValue : Bool) : TermElabM Term := do + let operands ← (names.zip shape).toArray.mapM fun (name, wide) => + if asValue then `(Lean.Parser.Term.bracketedBinderF| ($name : CIL.Value)) + else if wide then `(Lean.Parser.Term.bracketedBinderF| ($name : CIL.W64)) + else `(Lean.Parser.Term.bracketedBinderF| ($name : CIL.W32)) + let stack ← (names.zip shape).foldrM (init := (rest : Term)) fun (name, wide) tail => + if asValue then `(Value.scalar $name :: $tail) + else if wide then `(Value.scalar (CIL.Value.i64 $name) :: $tail) + else `(Value.scalar (CIL.Value.i32 $name) :: $tail) + `(∀ (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) + (memory : Memory) ($rest : List Value) $params* $operands*, + step body $opTerm pc args frame $stack memory = + step body $opTerm pc args frame $stack memory) + -- Proves `lhs = rhs` for all `xs`, with `rhs` the arm the dispatcher selects, cleaned up. + let reduceEquation (statement : Expr) : TermElabM (Expr × Expr) := + forallTelescope statement fun xs equation => do + let some (_, lhs, _) := equation.eq? | throwError "cil_value_steps: not an equation" + let mut reduced := lhs + repeat + let next := (← dsimp (← arms reduced) select).1 + if next == reduced then break + reduced := next + let (result, _) ← simp reduced cleanup simprocs + -- The proof states `reduced = rhs`; the kernel checks it at `lhs`, definitionally + -- equal by `dsimp`. + return (← mkForallFVars xs (← mkEq lhs result.expr), + ← mkLambdaFVars xs (← result.getProof)) + let specific ← instantiateMVars (← Term.elabType (← statementOver false)) + -- On fixed-width operands `whnf` and the kernel evaluate an unsigned overflow bound + -- `2 ^ width` in unary, so faulting ops reduce over `Value`s and are instantiated. + let (type, value) ← if checked then + let (generalType, generalProof) ← + reduceEquation (← instantiateMVars (← Term.elabType (← statementOver true))) + forallTelescope specific fun xs _ => do + let fixed := xs.size - shape.length + let mut arguments := xs.extract 0 fixed + for (operand, wide) in (xs.extract fixed xs.size).zip shape.toArray do + let constructor := if wide then ``CIL.Value.i64 else ``CIL.Value.i32 + arguments := arguments.push (mkApp (mkConst constructor) operand) + let some (_, lhs, guarded) := (← instantiateForall generalType arguments).eq? + | throwError "cil_value_steps: not an equation" + -- The operand constructors decide the unspecified-operand guard; nothing else + -- is reduced. + let guard := guarded.getAppArgs + unless guarded.isAppOf ``Bool.rec && guard.size > 3 do + throwError "cil_value_steps: no unspecified-operand guard in {guarded}" + let decided ← whnfD guard[3]! + let some branch := if decided.isConstOf ``Bool.false then some guard[1]! + else if decided.isConstOf ``Bool.true then some guard[2]! else none + | throwError "cil_value_steps: unspecified-operand guard not decided: {decided}" + let selected := (mkAppN branch (guard.extract 4 guard.size)).headBeta + -- `id` gives the proof the general statement as its type, so the kernel does not + -- compare the dispatcher with the reduced form at fixed widths. + let general := mkApp2 (mkConst ``id [.zero]) generalType generalProof + return (← mkForallFVars xs (← mkEq lhs selected), + ← mkLambdaFVars xs (mkAppN general arguments)) + else reduceEquation specific let suffix := String.join (shape.map fun wide => if wide then "_i64" else "_i32") let name := Name.mkSimple s!"step_{op.getId}{suffix}_eq" - forallTelescope (← instantiateMVars (← Term.elabType statement)) fun xs equation => do - let some (_, lhs, _) := equation.eq? | throwError "cil_value_steps: not an equation" - let mut reduced := lhs - repeat - let next := (← dsimp (← arms reduced) select).1 - if next == reduced then break - reduced := next - let (result, _) ← simp reduced cleanup simprocs - -- The proof states `reduced = rhs`; the kernel checks it at `lhs`, definitionally - -- equal by `dsimp`. - addDecl <| .thmDecl { - name := ns ++ name, levelParams := [] - type := ← mkForallFVars xs (← mkEq lhs result.expr) - value := ← mkLambdaFVars xs (← result.getProof) } + addDecl <| .thmDecl { name := ns ++ name, levelParams := [], type, value } declared := declared.push name return declared for name in declared do @@ -115,61 +151,76 @@ elab_rules : command -- Results stay on the stack; compare-branches expose their condition as an `if` that a fact -- can decide. -cil_value_steps [neg, bnot, convI1, convI2, convU2, convI4, convI8, convU1, convU4, convU8, convU] 1 -cil_value_steps [gt, add, sub, mul, band, bor, bxor, shl, shr, shrUn, lt, ltu, gtu, eq] 2 -cil_value_steps (target : Nat) [bgt, ble, bleu, bltu, bgeu, bgtu, bge, blt, beq, bne] 2 +cil_value_steps [neg, not, conv_i1, conv_i2, conv_u2, conv_i4, conv_i8, conv_u1, conv_u4, + conv_u8, conv_u, conv_i] 1 +cil_value_steps [cgt, add, sub, mul, and, or, xor, shl, shr, shr_un, clt, clt_un, cgt_un, ceq] 2 +cil_value_steps (target : Nat) [bgt, ble, ble_un, blt_un, bge_un, bgt_un, bge, blt, beq, bne_un] 2 cil_value_steps (targets : List Nat) [switch] 1 -cil_value_steps (target : Nat) [branch] 0 +cil_value_steps (target : Nat) [br] 0 cil_value_steps [nop] 0 -cil_value_steps (exception : String) [throw] 0 +cil_value_steps (exception : ExceptionType) [throw] 0 #print axioms step_throw_eq -cil_value_steps (id : CIL.Feature) [feature] 0 -cil_value_steps (kind : CIL.CheckedBinaryKind) (signed : Bool) [checkedArithmetic] 2 -cil_value_steps (width : Nat) (signed : Bool) (unsignedInput : Bool) [checkedConvert] 1 -cil_value_steps (signed : Bool) (remainder : Bool) [divide] 2 - -#print axioms step_checkedArithmetic_i64_i64_eq -#print axioms step_checkedArithmetic_i32_i32_eq -#print axioms step_checkedArithmetic_i32_i64_eq -#print axioms step_checkedConvert_i64_eq -#print axioms step_checkedConvert_i32_eq -#print axioms step_divide_i64_i64_eq -#print axioms step_divide_i32_i32_eq -#print axioms step_divide_i32_i64_eq - -/-- A proved checked arithmetic result advances the instruction without unfolding its check. -/ -theorem step_checkedArithmetic_result {kind : CIL.CheckedBinaryKind} {signed : Bool} +cil_value_steps (target : CIL.ConvTarget) [conv_ovf, conv_ovf_un] 1 +cil_value_steps [div, div_un, rem, rem_un, add_ovf, add_ovf_un, sub_ovf, sub_ovf_un, mul_ovf, + mul_ovf_un] 2 +-- A conversion's generated equation names `target.width` and `target.signed`; a known target +-- reduces them to the literals a caller's `calculated` fact states. +attribute [cil_step] CIL.ConvTarget.width CIL.ConvTarget.signed + + +/-- A proved overflow-checked arithmetic result advances the instruction without unfolding its + check; `selected` names the constructor's flags. -/ +theorem step_checked_result {operation : CIL.Instruction} {kind : CIL.CheckedBinaryKind} + {signed : Bool} (selected : operation.checkedKind? = some (kind, signed)) {left right value : CIL.Value} {memory : Memory} (calculated : CIL.checkedValues kind signed left right = .ok value) (leftKnown : left.hasUnspecified = false) (rightKnown : right.hasUnspecified = false) (checked : checkedValue memory (.scalar value) = .ok (.scalar value)) : ∀ body pc args frame rest, - step body (.checkedArithmetic kind signed) pc args frame - (.scalar right :: .scalar left :: rest) memory = + step body operation pc args frame (.scalar right :: .scalar left :: rest) memory = .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := by intro body pc args frame rest - simp [step, observerCount, unspecifiedOperands, Value.hasUnspecified, + cases operation <;> simp only [CIL.Instruction.checkedKind?, reduceCtorEq, + Option.some.injEq, Prod.mk.injEq] at selected + all_goals obtain ⟨rfl, rfl⟩ := selected + all_goals simp [step, observerCount, unspecifiedOperands, Value.hasUnspecified, leftKnown, rightKnown, calculated, checked, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] -#print axioms step_checkedArithmetic_result +#print axioms step_checked_result -/-- A proved checked conversion advances the instruction without unfolding its range check. -/ -theorem step_checkedConvert_result {width : Nat} {signed unsignedInput : Bool} - {input value : CIL.Value} {memory : Memory} - (calculated : CIL.convertInteger width signed unsignedInput input = .ok value) +/-- A proved checked conversion of a signed operand advances the instruction without unfolding + its range check. -/ +theorem step_conv_ovf_result {target : CIL.ConvTarget} {input value : CIL.Value} + {memory : Memory} + (calculated : CIL.convertInteger target.width target.signed false input = .ok value) (inputKnown : input.hasUnspecified = false) (checked : checkedValue memory (.scalar value) = .ok (.scalar value)) : ∀ body pc args frame rest, - step body (.checkedConvert width signed unsignedInput) pc args frame - (.scalar input :: rest) memory = + step body (.conv_ovf target) pc args frame (.scalar input :: rest) memory = .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := by intro body pc args frame rest simp [step, observerCount, unspecifiedOperands, Value.hasUnspecified, inputKnown, calculated, checked, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] -#print axioms step_checkedConvert_result +#print axioms step_conv_ovf_result + +/-- A proved checked conversion of an unsigned operand advances the instruction without + unfolding its range check. -/ +theorem step_conv_ovf_un_result {target : CIL.ConvTarget} {input value : CIL.Value} + {memory : Memory} + (calculated : CIL.convertInteger target.width target.signed true input = .ok value) + (inputKnown : input.hasUnspecified = false) + (checked : checkedValue memory (.scalar value) = .ok (.scalar value)) : + ∀ body pc args frame rest, + step body (.conv_ovf_un target) pc args frame (.scalar input :: rest) memory = + .ok (.next (pc + 1) (.scalar value :: rest) frame memory) := by + intro body pc args frame rest + simp [step, observerCount, unspecifiedOperands, Value.hasUnspecified, + inputKnown, calculated, checked, Except.mapError, + Bind.bind, Except.bind, Pure.pure, Except.pure] +#print axioms step_conv_ovf_un_result open Lean Meta in /-- `(m ..).profile` for a method definition or literal, unfolding only to the structure head; @@ -180,4 +231,154 @@ dsimproc [cil_step] reduceMethodProfile (CIL.Method.profile _) := fun e => do unless head.isAppOf ``CIL.Method.mk do return .continue return .done (← withReducible <| whnf (mkApp (mkConst ``CIL.Method.profile) head)) +open Lean Meta in +/-- The link `m.linkAt pc` reads, as `none` or `some` of a link constructor, for a method literal + `m` and a pc that reduces to a numeral, by evaluating its table. A named or opaque body, or a table a + parameter decides, gives nothing. -/ +def literalLink? (read : Expr) : MetaM (Option Expr) := do + let_expr CIL.Method.linkAt body pc := read | return none + unless (← instantiateMVars body).isAppOf ``CIL.Method.mk do return none + let pc ← instantiateMVars pc + unless pc.nat?.isSome do + if pc.hasFVar || pc.hasMVar then return none + unless (← (evalNat pc).run).isSome do return none + let link ← withDefault <| whnf read + match_expr link with + | Option.none _ => return some link + | Option.some _ value => + let value ← withDefault <| whnf value + unless value.getAppFn.isConst do return none + return some (mkApp2 (mkConst ``Option.some [Level.zero]) (mkConst ``CIL.Link) value) + | _ => return none + +open Lean Meta in +/-- `m.linkAt pc` for a method literal and a literal pc, by `literalLink?`. -/ +dsimproc [cil_step] reduceMethodLinkAt (CIL.Method.linkAt _ _) := fun read => do + let some link ← literalLink? read | return .continue + return .done link + +open Lean Meta in +/-- The runtime model of a call of a closed method in a method literal or under an environment + that is not a named body's, by evaluating the table; a named body's call is left for its + `cil_runtime_models` fact. An environment that is a variable decides every entry whose + resolution does not read it, and leaves the read of any other unevaluated. -/ +dsimproc [cil_step] reduceRuntimeModel (CIL.Extraction.runtimeModel _ _ _) := fun read => do + let_expr CIL.Extraction.runtimeModel environment opcode method := read | return .continue + if let some body := environment.app1? ``CIL.Method.environment then + unless (← instantiateMVars body).isAppOf ``CIL.Method.mk do return .continue + if opcode.hasFVar || opcode.hasMVar || method.hasFVar || method.hasMVar then return .continue + let resolved ← withDefault <| whnf read + match_expr resolved with + | Option.none _ => return .done resolved + | Option.some _ model => + return .done (← mkAppM ``Option.some #[← withDefault <| reduce model]) + | _ => return .continue + +open Lean Meta in +/-- Known return signatures simplify by checked storage lemmas without traversing method code. -/ +simproc [cil_step] reduceReturnNarrow (CIL.Method.returnNarrow _) := fun expression => do + let_expr CIL.Method.returnNarrow method := expression | return .continue + let returns ← withDefault <| whnf (mkApp (mkConst ``CIL.Method.returnsValue) method) + unless returns.isConstOf ``Bool.true || returns.isConstOf ``Bool.false do return .continue + let some proof ← ResolutionFacts.returnProof? method + (← mkEqRefl (mkApp (mkConst ``CIL.Method.returnsValue) method)) + (← mkEqRefl (mkApp (mkConst ``CIL.Method.declared) method)) | return .continue + let some (_, _, result) := (← inferType proof).eq? | return .continue + return .done { expr := ← withDefault <| whnf result, proof? := some proof } + +open Lean Meta in +/-- How a literal body reads a literal argument index, by the storage lemmas and premises the + kernel decides over its homes, local kinds and parameters; a symbolic part is left for a + caller's fact. -/ +simproc [cil_step] reduceArgumentAccess (CIL.Method.argumentAccess _ _) := fun expression => do + let_expr CIL.Method.argumentAccess method index := expression | return .continue + let some proof ← ResolutionFacts.argumentAccessProof? method index | return .continue + let some (_, _, result) := (← inferType proof).eq? | return .continue + return .done { expr := ← withDefault <| whnf result, proof? := some proof } + +open Lean Meta in +/-- The shape of a literal type's array elements in a literal body, by the shape lemmas and + premises the kernel decides over the body's layout lists; a symbolic part is left for a + caller's fact. -/ +simproc [cil_step] reduceElementShape (CIL.Method.elementShape _ _) := fun expression => do + let_expr CIL.Method.elementShape method type := expression | return .continue + let some (shape, proof) ← ResolutionFacts.elementProof? method type + (ResolutionFacts.literalReads method) | return .continue + return .done { expr := shape, proof? := some proof } + +open Lean Meta in +/-- How `body` reads argument `index`, reduced to an `ArgumentAccess` constructor where it can + be, with its proof: from the declared fact or signature, or else as simp rewrites the access + with the facts in scope. -/ +def argumentAccessIn (body index : Expr) : SimpM (Option (Expr × Expr)) := do + if let some resolved ← ResolutionFacts.resolvedArgumentAccess? body index then + return some resolved + let access ← Simp.simp (mkApp2 (mkConst ``CIL.Method.argumentAccess) body index) + return some (← whnfR (← instantiateMVars access.expr), ← access.getProof) + +open Lean Meta in +/-- The step an instruction takes by `equation`, whose right side is the result. -/ +def equationStep (equation : Expr) : SimpM Simp.Step := do + let some (_, _, stepped) := (← inferType equation).eq? | return .continue + return .visit { expr := stepped, proof? := some equation } + +open Lean Meta in +/-- An argument load steps by the equation for how the body reads that argument, as + `argumentAccessIn` finds it. A load whose access stays unknown steps to `loadArgument`, + whose match a later fact can decide. -/ +simproc [cil_step] reduceArgumentLoad (step _ (.ldarg _) _ _ _ _ _) := fun expression => do + let_expr CIL.Safety.step body operation pc args frame stack memory := expression + | return .continue + let_expr CIL.Instruction.ldarg index := operation | return .continue + let leading := #[body, pc, args, frame, memory, index, stack] + let some (resolved, proof) ← argumentAccessIn body index | return .continue + let known? ← match resolved.getAppFnArgs with + | (``CIL.ArgumentAccess.value, #[narrow]) => + match (← whnfR narrow).getAppFnArgs with + | (``Option.none, _) => + pure (some (mkAppN (mkConst ``step_ldarg_value_eq) (leading.push proof))) + | (``Option.some, #[_, kind]) => + pure (some (mkAppN (mkConst ``step_ldarg_narrow_eq) (leading ++ #[kind, proof]))) + | _ => pure none + | (``CIL.ArgumentAccess.home, #[slot]) => + pure (some (mkAppN (mkConst ``step_ldarg_home_eq) (leading ++ #[slot, proof]))) + | _ => pure none + equationStep (known?.getD (mkAppN (mkConst ``step_ldarg_eq) leading)) + +open Lean Meta in +/-- An argument store steps as `stloc` of the argument's home, or faults without one, as + `argumentAccessIn` finds it. A store whose access stays unknown steps to `storeArgument`. -/ +simproc [cil_step] reduceArgumentStore (step _ (.starg _) _ _ _ (_ :: _) _) := fun expression => do + let_expr CIL.Safety.step body operation pc args frame stack memory := expression + | return .continue + let_expr CIL.Instruction.starg index := operation | return .continue + let_expr List.cons _ value rest := stack | return .continue + let leading := #[body, pc, args, frame, memory, index, value, rest] + let some (resolved, proof) ← argumentAccessIn body index | return .continue + let known? := match resolved.getAppFnArgs with + | (``CIL.ArgumentAccess.value, #[narrow]) => + some (mkAppN (mkConst ``step_starg_value_eq) (leading ++ #[narrow, proof])) + | (``CIL.ArgumentAccess.home, #[slot]) => + some (mkAppN (mkConst ``step_starg_home_eq) (leading ++ #[slot, proof])) + | _ => none + equationStep (known?.getD (mkAppN (mkConst ``step_starg_eq) leading)) + +open Lean Meta in +/-- An argument address steps as `ldloca` of the argument's home, or faults without one, as + `argumentAccessIn` finds it. An address whose access stays unknown steps to + `addressArgument`. -/ +simproc [cil_step] reduceArgumentAddress (step _ (.ldarga _) _ _ _ _ _) := fun expression => do + let_expr CIL.Safety.step body operation pc args frame stack memory := expression + | return .continue + let_expr CIL.Instruction.ldarga index := operation | return .continue + let leading := #[body, pc, args, frame, memory, index, stack] + let some (resolved, proof) ← argumentAccessIn body index | return .continue + let known? := match resolved.getAppFnArgs with + | (``CIL.ArgumentAccess.value, #[narrow]) => + some (mkAppN (mkConst ``step_ldarga_value_eq) (leading ++ #[narrow, proof])) + | (``CIL.ArgumentAccess.home, #[slot]) => + some (mkAppN (mkConst ``step_ldarga_home_eq) (leading ++ #[slot, proof])) + | _ => none + equationStep (known?.getD (mkAppN (mkConst ``step_ldarga_eq) leading)) + end CIL.Safety diff --git a/CIL/Safety/ViewPositionRenaming.lean b/CIL/Safety/ViewPositionRenaming.lean index bd268e84e..0927de916 100644 --- a/CIL/Safety/ViewPositionRenaming.lean +++ b/CIL/Safety/ViewPositionRenaming.lean @@ -157,7 +157,7 @@ theorem Memory.Renamed.arrayElement {mapping : AllocationRenaming} | null => simp only [AllocationRenaming.managedReference, reference, BEq.beq, decide_true, ite_true] - exact .error (.thrown "System.NullReferenceException") + exact .error (.thrown .nullReference) | address address => simp only [AllocationRenaming.managedReference, reference, BEq.beq, reduceCtorEq, decide_false, Bool.false_eq_true, ite_false] @@ -165,7 +165,7 @@ theorem Memory.Renamed.arrayElement {mapping : AllocationRenaming} · simpa only [inside, ite_true, Bind.bind, Except.bind, reference, AllocationRenaming.arrayView, AllocationRenaming.managedReference] using positions · simp only [inside, ite_false, Bind.bind, Except.bind] - exact .error (.thrown "System.IndexOutOfRangeException") + exact .error (.thrown .indexOutOfRange) #print axioms Memory.Renamed.arrayElement end CIL.Safety diff --git a/CIL/Safety/WeakestPrecondition.lean b/CIL/Safety/WeakestPrecondition.lean index 994461a9b..725b4b779 100644 --- a/CIL/Safety/WeakestPrecondition.lean +++ b/CIL/Safety/WeakestPrecondition.lean @@ -105,7 +105,7 @@ theorem of_prefix {before after : Nat → Except LocatedFault (Memory × List Va /-- One fetched instruction continues with the checked state it actually produces. -/ theorem step {program : CIL.Program} {method pc target : Nat} - {body : CIL.Method} {operation : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {operation : CIL.Instruction} {args stack values : List Value} {frame nextFrame : Frame} {memory updated : Memory} {post : (Memory × List Value) → Prop} (found : program[method]? = some body) (fetched : body.code[pc]? = some operation) (stepped : Safety.step body operation pc args frame stack memory = @@ -121,7 +121,7 @@ theorem step {program : CIL.Program} {method pc target : Nat} /-- A checked return expires the frame and validates every value before establishing `post`. -/ theorem returned {program : CIL.Program} {method pc : Nat} - {body : CIL.Method} {operation : CIL.Op} {args stack values : List Value} + {body : CIL.Method} {operation : CIL.Instruction} {args stack values : List Value} {frame : Frame} {memory updated : Memory} {post : (Memory × List Value) → Prop} (found : program[method]? = some body) (fetched : body.code[pc]? = some operation) (stepped : Safety.step body operation pc args frame stack memory = @@ -150,7 +150,7 @@ theorem enter {program : CIL.Program} {method : Nat} {body : CIL.Method} /-- Compose a checked call with its suffix, retaining the helper's complete postcondition. -/ theorem call {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} - {operation : CIL.Op} {args stack arguments rest : List Value} + {operation : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {helperPost post : (Memory × List Value) → Prop} (found : program[method]? = some body) (fetched : body.code[pc]? = some operation) diff --git a/CIL/Safety/WideMultiply.lean b/CIL/Safety/WideMultiply.lean index 9dc8b36a0..10dd5af1a 100644 --- a/CIL/Safety/WideMultiply.lean +++ b/CIL/Safety/WideMultiply.lean @@ -4,8 +4,8 @@ import CIL.Safety.MemorySteps /-! # Wide multiply -`Math.BigMul(ulong, ulong, out ulong)` and `Math.BigMul(long, long, out long)` as the -`bigMul64` memory instruction: each returns the unsigned or signed high word of the exact +`Math.BigMul(ulong, ulong, out ulong)` and `Math.BigMul(long, long, out long)` use the +checked `bigMul64` memory model. Each returns the unsigned or signed high word of the exact 128-bit product and writes the low word through the out reference, changing no other cell and no allocation. `CIL.WideMultiply.signedHigh64_decomposition` recomposes the signed product. -/ @@ -16,12 +16,16 @@ namespace CIL.Safety retains the exact high-word expression used by arithmetic proofs. -/ theorem step_bigMul64_of_write {body : CIL.Method} {pc : Nat} {args rest : List Value} {frame : Frame} {memory result : Memory} {reference : Reference} {left right : CIL.W64} + {method : CIL.Extraction.MethodRef} (unlinked : body.linkAt pc = none) + (model : CIL.Extraction.runtimeModel body.environment .call method = + some (.memory (.bigMul64 false))) (stored : write memory reference (numberBytes (left * right).toNat 8) 1 = .ok result) : - step body (.memory (.bigMul64 false)) pc args frame + step body (.call method {}) pc args frame (.reference (.address reference) :: .scalar (.i64 right) :: .scalar (.i64 left) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i64 (CIL.WideMultiply.high64 left right)) :: rest) frame result) := by - rw [step_memory_bigMul64_eq] + rw [step_call_model (link := unlinked) (found := model) (direct := rfl), + step_memory_bigMul64_eq] simp only [storeValue, CIL.Value.hasUnspecified, Bool.false_eq_true, ite_false, referenceAt, checkedAt, stored, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] @@ -30,12 +34,16 @@ theorem step_bigMul64_of_write {body : CIL.Method} {pc : Nat} {args rest : List /-- A checked low-word write steps signed wide multiplication with its exact signed high word. -/ theorem step_bigMul64_signed_of_write {body : CIL.Method} {pc : Nat} {args rest : List Value} {frame : Frame} {memory result : Memory} {reference : Reference} {left right : CIL.W64} + {method : CIL.Extraction.MethodRef} (unlinked : body.linkAt pc = none) + (model : CIL.Extraction.runtimeModel body.environment .call method = + some (.memory (.bigMul64 true))) (stored : write memory reference (numberBytes (left * right).toNat 8) 1 = .ok result) : - step body (.memory (.bigMul64 true)) pc args frame + step body (.call method {}) pc args frame (.reference (.address reference) :: .scalar (.i64 right) :: .scalar (.i64 left) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i64 (CIL.WideMultiply.signedHigh64 left right)) :: rest) frame result) := by - rw [step_memory_bigMul64_signed_eq] + rw [step_call_model (link := unlinked) (found := model) (direct := rfl), + step_memory_bigMul64_signed_eq] simp only [storeValue, CIL.Value.hasUnspecified, Bool.false_eq_true, ite_false, referenceAt, checkedAt, stored, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] diff --git a/CIL/Safety/WordFrameSetup.lean b/CIL/Safety/WordFrameSetup.lean index 2b05275fa..e6c9c5ad0 100644 --- a/CIL/Safety/WordFrameSetup.lean +++ b/CIL/Safety/WordFrameSetup.lean @@ -15,7 +15,7 @@ namespace CIL.Safety theorem word_frame_setup (body : CIL.Method) (specs : List (Option (BitVec 64))) (kinds : body.localKinds = wordKinds specs) (initializers : body.locals = wordInitializers specs) - (arguments : body.vectorArgs = []) (parameters : body.parameterHomes = []) + (parameters : body.parameterHomes = []) (opaqueLocals : body.opaqueVectorLocals = []) (memory : Memory) (args : List Value) (wellFormed : memory.WellFormed) : ∃ frame result, @@ -24,10 +24,10 @@ theorem word_frame_setup (body : CIL.Method) (specs : List (Option (BitVec 64))) MemoryBelow memory.nextIdentity memory result ∧ result.WellFormed := by obtain ⟨slots, owned, result, made, homes⟩ := make_word_locals memory memory.nextIdentity specs wellFormed - let frame : Frame := ⟨memory.nextIdentity, slots, owned, [], 0⟩ + let frame : Frame := ⟨memory.nextIdentity, slots, owned, 0⟩ have setup : enterFrame body args memory = .ok (frame, result) := by simp only [enterFrame, opaqueLocals, makeSelectedLocals, List.isEmpty_nil, ite_true, - kinds, initializers, made, arguments, makeArgumentHomes, parameters, makeParameterHomes, + kinds, initializers, made, parameters, makeParameterHomes, List.map_nil, Bind.bind, Except.bind, Pure.pure, Except.pure, List.append_nil] rfl diff --git a/CIL/Safety/WordLocals.lean b/CIL/Safety/WordLocals.lean index 38118400e..118c842f5 100644 --- a/CIL/Safety/WordLocals.lean +++ b/CIL/Safety/WordLocals.lean @@ -37,9 +37,9 @@ theorem step_load_word64 {body : CIL.Method} {pc index : Nat} {args stack : List {frame : Frame} {memory : Memory} {reference : Reference} {word : BitVec 64} (slot : frame.locals[index]? = some (.bytes .word64 reference)) (loaded : read memory reference 8 1 = .ok (numberBytes word.toNat 8)) : - step body (.local index) pc args frame stack memory = + step body (.ldloc index) pc args frame stack memory = .ok (.next (pc + 1) (.scalar (.i64 word) :: stack) frame memory) := by - simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, Bool.false_or, + simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, Bool.false_eq_true, ite_false, slot, load_local_word64_of_read loaded, Bind.bind, Except.bind, Pure.pure, Except.pure] @@ -49,14 +49,14 @@ theorem step_store_word64 {body : CIL.Method} {pc index : Nat} {args rest : List (slot : frame.locals[index]? = some (.bytes .word64 reference)) (ready : access memory reference 8 1 true = .ok ()) : ∃ result, - step body (.setLocal index) pc args frame (.scalar (.i64 word) :: rest) memory = + step body (.stloc index) pc args frame (.scalar (.i64 word) :: rest) memory = .ok (.next (pc + 1) rest { frame with locals := frame.locals.set index (.bytes .word64 reference) } result) ∧ write memory reference (numberBytes word.toNat 8) 1 = .ok result ∧ read result reference 8 1 = .ok (numberBytes word.toNat 8) := by obtain ⟨result, written, stored, readback, _⟩ := store_local_word64 word ready refine ⟨result, ?_, written, readback⟩ - simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, Bool.false_or, + simp only [step, observerCount, unspecifiedOperands, List.take_zero, List.any_nil, Bool.false_eq_true, ite_false, slot, stored, Bind.bind, Except.bind, Pure.pure, Except.pure] /-- Storing a numeric local changes its bytes, not the frame's slot identities. @@ -67,7 +67,7 @@ theorem step_store_word64_same_frame {body : CIL.Method} {pc index : Nat} (slot : frame.locals[index]? = some (.bytes .word64 reference)) (ready : access memory reference 8 1 true = .ok ()) : ∃ result, - step body (.setLocal index) pc args frame (.scalar (.i64 word) :: rest) memory = + step body (.stloc index) pc args frame (.scalar (.i64 word) :: rest) memory = .ok (.next (pc + 1) rest frame result) ∧ write memory reference (numberBytes word.toNat 8) 1 = .ok result ∧ read result reference 8 1 = .ok (numberBytes word.toNat 8) := by diff --git a/CIL/Safety/WordSnapshots.lean b/CIL/Safety/WordSnapshots.lean index 0b0e8cdb6..93694670a 100644 --- a/CIL/Safety/WordSnapshots.lean +++ b/CIL/Safety/WordSnapshots.lean @@ -94,7 +94,7 @@ theorem WordSnapshots.load {body : CIL.Method} {pc index : Nat} {args stack : Li {frame : Frame} {memory : Memory} {known : Nat → Option (BitVec 64)} (snapshots : WordSnapshots memory frame.locals known) (value : BitVec 64) (specified : known index = some value) : - step body (.local index) pc args frame stack memory = + step body (.ldloc index) pc args frame stack memory = .ok (.next (pc + 1) (.scalar (.i64 value) :: stack) frame memory) := by obtain ⟨reference, slot, loaded⟩ := snapshots index value specified exact step_load_numeric_local .word64 (.i64 value) value.toNat rfl slot loaded @@ -110,7 +110,7 @@ theorem WordSnapshots.store {body : CIL.Method} {pc index : Nat} {args stack : L (slot : frame.locals[index]? = some (.bytes .word64 reference)) (ready : access memory reference 8 1 true = .ok ()) : ∃ after, - step body (.setLocal index) pc args frame (.scalar (.i64 value) :: stack) memory = + step body (.stloc index) pc args frame (.scalar (.i64 value) :: stack) memory = .ok (.next (pc + 1) stack frame after) ∧ WordSnapshots after frame.locals (rememberWord known index value) ∧ after.WellFormed ∧ MemoryBelow boundary memory after ∧ AccessBelow memory.nextIdentity memory after ∧ diff --git a/CIL/Semantics.lean b/CIL/Semantics.lean index c6378a33e..73e8459f2 100644 --- a/CIL/Semantics.lean +++ b/CIL/Semantics.lean @@ -1,7 +1,7 @@ import CIL.Memory import CIL.Instructions -import CIL.AggregateMemory import CIL.IntegerArithmetic +import CIL.NativeEvaluation /-! # Instruction evaluation @@ -25,10 +25,19 @@ def truth : Value → Option Bool /-- Two-operand arithmetic, bitwise and comparison instructions. `a` is the deeper stack operand. Comparisons push `1` or `0` as an `i32`; shift counts are taken modulo the width. -/ -def binary (op : Op) (a b : Value) : Option Value := +def binary (op : Instruction) (a b : Value) : Option Value := match op, a, b with - | .divide signed remainder, x, y => (divideValues signed remainder x y).toOption - | .checkedArithmetic kind signed, x, y => (checkedValues kind signed x y).toOption + -- Division, remainder and overflow-checked arithmetic; a fault gives no value. + | .div, x, y => (divideValues true false x y).toOption + | .div_un, x, y => (divideValues false false x y).toOption + | .rem, x, y => (divideValues true true x y).toOption + | .rem_un, x, y => (divideValues false true x y).toOption + | .add_ovf, x, y => (checkedValues .add true x y).toOption + | .add_ovf_un, x, y => (checkedValues .add false x y).toOption + | .sub_ovf, x, y => (checkedValues .sub true x y).toOption + | .sub_ovf_un, x, y => (checkedValues .sub false x y).toOption + | .mul_ovf, x, y => (checkedValues .mul true x y).toOption + | .mul_ovf_un, x, y => (checkedValues .mul false x y).toOption -- Wrapping arithmetic and bitwise operations. | .add, .i64 x, .i64 y => some (.i64 (x + y)) | .add, .i32 x, .i32 y => some (.i32 (x + y)) @@ -36,42 +45,42 @@ def binary (op : Op) (a b : Value) : Option Value := | .sub, .i32 x, .i32 y => some (.i32 (x - y)) | .mul, .i64 x, .i64 y => some (.i64 (x * y)) | .mul, .i32 x, .i32 y => some (.i32 (x * y)) - | .band, .i64 x, .i64 y => some (.i64 (x &&& y)) - | .band, .i32 x, .i32 y => some (.i32 (x &&& y)) - | .bor, .i64 x, .i64 y => some (.i64 (x ||| y)) - | .bor, .i32 x, .i32 y => some (.i32 (x ||| y)) - | .bxor, .i64 x, .i64 y => some (.i64 (x ^^^ y)) - | .bxor, .i32 x, .i32 y => some (.i32 (x ^^^ y)) + | .and, .i64 x, .i64 y => some (.i64 (x &&& y)) + | .and, .i32 x, .i32 y => some (.i32 (x &&& y)) + | .or, .i64 x, .i64 y => some (.i64 (x ||| y)) + | .or, .i32 x, .i32 y => some (.i32 (x ||| y)) + | .xor, .i64 x, .i64 y => some (.i64 (x ^^^ y)) + | .xor, .i32 x, .i32 y => some (.i32 (x ^^^ y)) -- Shifts. | .shl, .i64 x, .i32 count => some (.i64 (x <<< (count.toNat % 64))) | .shl, .i32 x, .i32 count => some (.i32 (x <<< (count.toNat % 32))) - | .shrUn, .i64 x, .i32 count => some (.i64 (x >>> (count.toNat % 64))) - | .shrUn, .i32 x, .i32 count => some (.i32 (x >>> (count.toNat % 32))) + | .shr_un, .i64 x, .i32 count => some (.i64 (x >>> (count.toNat % 64))) + | .shr_un, .i32 x, .i32 count => some (.i32 (x >>> (count.toNat % 32))) | .shr, .i64 x, .i32 count => some (.i64 (x.sshiftRight (count.toNat % 64))) | .shr, .i32 x, .i32 count => some (.i32 (x.sshiftRight (count.toNat % 32))) -- Native counts use their low bits; extraction distinguishes native values from int64. | .shl, .i64 x, .i64 count => some (.i64 (x <<< ((count.setWidth 32).toNat % 64))) | .shl, .i32 x, .i64 count => some (.i32 (x <<< ((count.setWidth 32).toNat % 32))) - | .shrUn, .i64 x, .i64 count => some (.i64 (x >>> ((count.setWidth 32).toNat % 64))) - | .shrUn, .i32 x, .i64 count => some (.i32 (x >>> ((count.setWidth 32).toNat % 32))) + | .shr_un, .i64 x, .i64 count => some (.i64 (x >>> ((count.setWidth 32).toNat % 64))) + | .shr_un, .i32 x, .i64 count => some (.i32 (x >>> ((count.setWidth 32).toNat % 32))) | .shr, .i64 x, .i64 count => some (.i64 (x.sshiftRight ((count.setWidth 32).toNat % 64))) | .shr, .i32 x, .i64 count => some (.i32 (x.sshiftRight ((count.setWidth 32).toNat % 32))) -- Comparisons. - | .eq, .i64 x, .i64 y => some (.i32 (if x = y then 1 else 0)) - | .eq, .i32 x, .i32 y => some (.i32 (if x = y then 1 else 0)) - | .lt, .i64 x, .i64 y => some (.i32 (if x.toInt < y.toInt then 1 else 0)) - | .lt, .i32 x, .i32 y => some (.i32 (if x.toInt < y.toInt then 1 else 0)) - | .gt, .i64 x, .i64 y => some (.i32 (if y.toInt < x.toInt then 1 else 0)) - | .gt, .i32 x, .i32 y => some (.i32 (if y.toInt < x.toInt then 1 else 0)) - | .ltu, .i64 x, .i64 y => some (.i32 (if x < y then 1 else 0)) - | .ltu, .i32 x, .i32 y => some (.i32 (if x < y then 1 else 0)) - | .gtu, .i64 x, .i64 y => some (.i32 (if y < x then 1 else 0)) - | .gtu, .i32 x, .i32 y => some (.i32 (if y < x then 1 else 0)) + | .ceq, .i64 x, .i64 y => some (.i32 (if x = y then 1 else 0)) + | .ceq, .i32 x, .i32 y => some (.i32 (if x = y then 1 else 0)) + | .clt, .i64 x, .i64 y => some (.i32 (if x.toInt < y.toInt then 1 else 0)) + | .clt, .i32 x, .i32 y => some (.i32 (if x.toInt < y.toInt then 1 else 0)) + | .cgt, .i64 x, .i64 y => some (.i32 (if y.toInt < x.toInt then 1 else 0)) + | .cgt, .i32 x, .i32 y => some (.i32 (if y.toInt < x.toInt then 1 else 0)) + | .clt_un, .i64 x, .i64 y => some (.i32 (if x < y then 1 else 0)) + | .clt_un, .i32 x, .i32 y => some (.i32 (if x < y then 1 else 0)) + | .cgt_un, .i64 x, .i64 y => some (.i32 (if y < x then 1 else 0)) + | .cgt_un, .i32 x, .i32 y => some (.i32 (if y < x then 1 else 0)) | _ , _, _ => none /-- Native shift counts normalize to their low 32 bits under the admitted 64-bit runtime. -/ -theorem binary_native_shift_count (operation : Op) (value : Value) (count : W64) - (shift : operation = .shl ∨ operation = .shr ∨ operation = .shrUn) : +theorem binary_native_shift_count (operation : Instruction) (value : Value) (count : W64) + (shift : operation = .shl ∨ operation = .shr ∨ operation = .shr_un) : binary operation value (.i64 count) = binary operation value (.i32 (count.setWidth 32)) := by rcases shift with rfl | rfl | rfl <;> cases value <;> rfl @@ -79,44 +88,43 @@ theorem binary_native_shift_count (operation : Op) (value : Value) (count : W64) /-- One-operand instructions: negation, complement and integer conversions. A narrowing conversion truncates, then sign- or zero-extends back to `i32`. -/ -def unary (op : Op) (value : Value) : Option Value := +def unary (op : Instruction) (value : Value) : Option Value := match op, value with - | .checkedConvert width signed unsignedInput, value => - (convertInteger width signed unsignedInput value).toOption + | .conv_ovf target, value => (convertInteger target.width target.signed false value).toOption + | .conv_ovf_un target, value => + (convertInteger target.width target.signed true value).toOption | .neg, .i32 w => some (.i32 (-w)) | .neg, .i64 w => some (.i64 (-w)) - | .bnot, .i64 w => some (.i64 (~~~w)) - | .bnot, .i32 w => some (.i32 (~~~w)) + | .not, .i64 w => some (.i64 (~~~w)) + | .not, .i32 w => some (.i32 (~~~w)) -- To 8, 16 and 32 bits. - | .convI1, .i32 w => some (.i32 (NarrowKind.signed8.extend w)) - | .convI1, .i64 w => some (.i32 (NarrowKind.signed8.extend (w.setWidth 32))) - | .convU1, .i32 w => some (.i32 ((w.setWidth 8).zeroExtend 32)) - | .convU1, .i64 w => some (.i32 ((w.setWidth 8).zeroExtend 32)) - | .convI2, .i32 w => some (.i32 (NarrowKind.signed16.extend w)) - | .convI2, .i64 w => some (.i32 (NarrowKind.signed16.extend (w.setWidth 32))) - | .convU2, .i32 w => some (.i32 (NarrowKind.unsigned16.extend w)) - | .convU2, .i64 w => some (.i32 (NarrowKind.unsigned16.extend (w.setWidth 32))) - | .convI4, .i64 w => some (.i32 (w.setWidth 32)) - | .convI4, .i32 w => some (.i32 w) - | .convU4, .i64 w => some (.i32 (w.setWidth 32)) - | .convU4, .i32 w => some (.i32 w) + | .conv_i1, .i32 w => some (.i32 (NarrowKind.signed8.extend w)) + | .conv_i1, .i64 w => some (.i32 (NarrowKind.signed8.extend (w.setWidth 32))) + | .conv_u1, .i32 w => some (.i32 ((w.setWidth 8).zeroExtend 32)) + | .conv_u1, .i64 w => some (.i32 ((w.setWidth 8).zeroExtend 32)) + | .conv_i2, .i32 w => some (.i32 (NarrowKind.signed16.extend w)) + | .conv_i2, .i64 w => some (.i32 (NarrowKind.signed16.extend (w.setWidth 32))) + | .conv_u2, .i32 w => some (.i32 (NarrowKind.unsigned16.extend w)) + | .conv_u2, .i64 w => some (.i32 (NarrowKind.unsigned16.extend (w.setWidth 32))) + | .conv_i4, .i64 w => some (.i32 (w.setWidth 32)) + | .conv_i4, .i32 w => some (.i32 w) + | .conv_u4, .i64 w => some (.i32 (w.setWidth 32)) + | .conv_u4, .i32 w => some (.i32 w) -- To 64 bits. - | .convI8, .i32 w => some (.i64 (w.signExtend 64)) - | .convI8, .i64 w => some (.i64 w) - | .convU8, .i32 w => some (.i64 (w.zeroExtend 64)) - | .convU8, .i64 w => some (.i64 w) - | .convU, .i32 w => some (.i64 (w.zeroExtend 64)) - | .convU, .i64 w => some (.i64 w) + | .conv_i8, .i32 w => some (.i64 (w.signExtend 64)) + | .conv_i8, .i64 w => some (.i64 w) + | .conv_i, .i32 w => some (.i64 (w.signExtend 64)) + | .conv_i, .i64 w => some (.i64 w) + | .conv_u8, .i32 w => some (.i64 (w.zeroExtend 64)) + | .conv_u8, .i64 w => some (.i64 w) + | .conv_u, .i32 w => some (.i64 (w.zeroExtend 64)) + | .conv_u, .i64 w => some (.i64 w) | _, _ => none /-- What a step asks the driver to do next. -/ inductive Action where /-- Continue at `pc` with the new stack and memory. -/ | next (pc : Nat) (stack : List Value) (memory : Memory) - /-- Call `callee` with `args`; its results are pushed onto `rest`. -/ - | call (callee : Nat) (args rest : List Value) (memory : Memory) - /-- Run constructor `callee`, then push the vector it built in a kind-2 home onto `rest`. -/ - | construct (callee : Nat) (args rest : List Value) (memory : Memory) /-- The method returned `result`. -/ | returned (result : List Value) (memory : Memory) @@ -133,85 +141,60 @@ def returnFromStack (narrow : Option NarrowKind) (returns : Bool) else if narrow.isNone && stack.isEmpty then return .returned [] memory else none /-- Executes instruction `op` at `pc`. Binary operands are popped as `b :: a :: rest`, so - `a` was pushed first. `returns` says whether the method returns a value; `profile` - decides feature tests and intrinsic availability. -/ -def step (op : Op) (returns : Bool) (pc : Nat) (args : List Value) (frame : Nat) + `a` was pushed first; a mixed int32 and native pair is first widened by `widenMixed` + (ECMA-335 III.1.5). `returns` says whether the method returns a value; the checked call model decides feature tests and intrinsic availability. `narrow` is the method's return storage. -/ +def step (op : Instruction) (returns : Bool) + (narrow : Option NarrowKind) (pc : Nat) (stack : List Value) (memory : Memory) (profile : FeatureProfile := FeatureProfile.scalar) : Option Action := do - match op, stack with + let _ := profile + match op, widenMixed op stack with -- Constants and stack shuffling. | .nop, _ => return .next (pc + 1) stack memory - | .const32 w, _ => return .next (pc + 1) (.i32 w :: stack) memory - | .const64 w, _ => return .next (pc + 1) (.i64 w :: stack) memory + | .ldc_i4 w, _ => return .next (pc + 1) (.i32 w :: stack) memory + | .ldc_i8 w, _ => return .next (pc + 1) (.i64 w :: stack) memory | .dup, v :: rest => return .next (pc + 1) (v :: v :: rest) memory | .pop, _ :: rest => return .next (pc + 1) rest memory - -- Arguments and locals; a read must be initialized. - | .arg i narrow, _ => - let value ← args[i]? - let value ← narrowValue narrow value - if !value.initialized then none else return .next (pc + 1) (value :: stack) memory - | .local i narrow, _ => - let value ← memory (.local frame i) - let value ← narrowValue narrow value - if !value.initialized then none else return .next (pc + 1) (value :: stack) memory - | .localAddr i, _ => return .next (pc + 1) (.ref (.local frame i) :: stack) memory - | .setLocal i narrow, v :: rest => - return .next (pc + 1) rest (write memory (.local frame i) (← narrowValue narrow v)) - -- Vector homes: kind 1 for arguments, kind 0 for locals. - | .vectorArg i, _ => return .next (pc + 1) ((← readVectorHome memory frame 1 i) :: stack) memory - | .vectorArgAddr i, _ => return .next (pc + 1) (.ref (.home frame 1 i 0) :: stack) memory - | .vectorLocal i, _ => return .next (pc + 1) ((← readVectorHome memory frame 0 i) :: stack) memory - | .vectorLocalAddr i, _ => return .next (pc + 1) (.ref (.home frame 0 i 0) :: stack) memory - | .setVectorLocal i, .v256 bits :: rest => - return .next (pc + 1) rest (writeVectorHome memory frame 0 i bits) - -- 64-bit lanes of an object, a reference or a vector value. - | .lane i, .object id :: rest => - return .next (pc + 1) ((← read64 memory (.byte (id + 8 * i.val))) :: rest) memory - | .lane i, .ref address :: rest => - return .next (pc + 1) ((← read64 memory (← unsafeAdd 8 i.val address)) :: rest) memory - | .lane i, .v256 bits :: rest => - return .next (pc + 1) (.i64 (Vector.lane64 bits i.val) :: rest) memory - | .laneAddress i, .object id :: rest => - return .next (pc + 1) (.ref (.byte (id + 8 * i.val)) :: rest) memory - | .laneAddress i, .ref address :: rest => - return .next (pc + 1) (.ref (← unsafeAdd 8 i.val address) :: rest) memory - | .setLane i, .i64 word :: .object base :: rest => - return .next (pc + 1) rest (← write64 memory (.byte (base + 8 * i.val)) word) - | .setLane i, .i64 word :: .ref address :: rest => - return .next (pc + 1) rest (← write64 memory (← unsafeAdd 8 i.val address) word) -- Arithmetic, through `unary` and `binary`. - | .neg, v :: rest | .bnot, v :: rest - | .checkedConvert _ _ _, v :: rest - | .convI1, v :: rest | .convI2, v :: rest | .convU2, v :: rest - | .convI4, v :: rest | .convI8, v :: rest | .convU1, v :: rest - | .convU4, v :: rest | .convU8, v :: rest | .convU, v :: rest => + | .neg, v :: rest | .not, v :: rest + | .conv_ovf _, v :: rest | .conv_ovf_un _, v :: rest + | .conv_i1, v :: rest | .conv_i2, v :: rest | .conv_u2, v :: rest + | .conv_i4, v :: rest | .conv_i8, v :: rest | .conv_u1, v :: rest + | .conv_u4, v :: rest | .conv_u8, v :: rest | .conv_u, v :: rest | .conv_i, v :: rest => return .next (pc + 1) ((← unary op v) :: rest) memory | .add, b :: a :: rest | .sub, b :: a :: rest - | .divide _ _, b :: a :: rest - | .checkedArithmetic _ _, b :: a :: rest - | .band, b :: a :: rest | .bor, b :: a :: rest - | .mul, b :: a :: rest | .bxor, b :: a :: rest - | .shl, b :: a :: rest | .shr, b :: a :: rest | .shrUn, b :: a :: rest - | .lt, b :: a :: rest | .ltu, b :: a :: rest | .gt, b :: a :: rest | .gtu, b :: a :: rest - | .eq, b :: a :: rest => + | .div, b :: a :: rest | .div_un, b :: a :: rest + | .rem, b :: a :: rest | .rem_un, b :: a :: rest + | .add_ovf, b :: a :: rest | .add_ovf_un, b :: a :: rest + | .sub_ovf, b :: a :: rest | .sub_ovf_un, b :: a :: rest + | .mul_ovf, b :: a :: rest | .mul_ovf_un, b :: a :: rest + | .and, b :: a :: rest | .or, b :: a :: rest + | .mul, b :: a :: rest | .xor, b :: a :: rest + | .shl, b :: a :: rest | .shr, b :: a :: rest | .shr_un, b :: a :: rest + | .clt, b :: a :: rest | .clt_un, b :: a :: rest | .cgt, b :: a :: rest + | .cgt_un, b :: a :: rest + | .ceq, b :: a :: rest => return .next (pc + 1) ((← binary op a b) :: rest) memory - -- 64-bit loads and stores through a reference. - | .load64, .ref a :: rest => + -- Eight-byte integer loads and stores through a reference. + | .ldind .i8, .ref a :: rest | .ldind .i, .ref a :: rest => match ← read64 memory a with | .i64 w => return .next (pc + 1) (.i64 w :: rest) memory | _ => none - | .store64, .i64 w :: .ref a :: rest => return .next (pc + 1) rest (← write64 memory a w) + | .stind .i8, .i64 w :: .ref a :: rest | .stind .i, .i64 w :: .ref a :: rest => + return .next (pc + 1) rest (← write64 memory a w) -- Unconditional and truth-tested branches. - | .branch target, _ => return .next target stack memory + | .br target, _ => return .next target stack memory | .switch targets, .i32 index :: rest => return .next (targets[index.toNat]?.getD (pc + 1)) rest memory - | .brzero target, v :: rest => return .next (if ← truth v then pc + 1 else target) rest memory - | .brnonzero target, v :: rest => return .next (if ← truth v then target else pc + 1) rest memory + | .brfalse target, v :: rest => return .next (if ← truth v then pc + 1 else target) rest memory + | .brtrue target, v :: rest => return .next (if ← truth v then target else pc + 1) rest memory -- Compare-and-branch: `a` was pushed first, and every condition is stated with `<` or `≤`. | .beq target, .i32 b :: .i32 a :: rest => return .next (if a = b then target else pc + 1) rest memory | .beq target, .i64 b :: .i64 a :: rest => return .next (if a = b then target else pc + 1) rest memory - | .bne target, .i32 b :: .i32 a :: rest => return .next (if a ≠ b then target else pc + 1) rest memory - | .bne target, .i64 b :: .i64 a :: rest => return .next (if a ≠ b then target else pc + 1) rest memory + | .bne_un target, .i32 b :: .i32 a :: rest => + return .next (if a ≠ b then target else pc + 1) rest memory + | .bne_un target, .i64 b :: .i64 a :: rest => + return .next (if a ≠ b then target else pc + 1) rest memory | .blt target, .i32 b :: .i32 a :: rest => return .next (if a.toInt < b.toInt then target else pc + 1) rest memory | .blt target, .i64 b :: .i64 a :: rest => @@ -228,38 +211,24 @@ def step (op : Op) (returns : Bool) (pc : Nat) (args : List Value) (frame : Nat) return .next (if b.toInt ≤ a.toInt then target else pc + 1) rest memory | .bge target, .i64 b :: .i64 a :: rest => return .next (if b.toInt ≤ a.toInt then target else pc + 1) rest memory - | .bltu target, .i32 b :: .i32 a :: rest => return .next (if a < b then target else pc + 1) rest memory - | .bltu target, .i64 b :: .i64 a :: rest => return .next (if a < b then target else pc + 1) rest memory - | .bleu target, .i32 b :: .i32 a :: rest => return .next (if a ≤ b then target else pc + 1) rest memory - | .bleu target, .i64 b :: .i64 a :: rest => return .next (if a ≤ b then target else pc + 1) rest memory - | .bgtu target, .i32 b :: .i32 a :: rest => return .next (if b < a then target else pc + 1) rest memory - | .bgtu target, .i64 b :: .i64 a :: rest => return .next (if b < a then target else pc + 1) rest memory - | .bgeu target, .i32 b :: .i32 a :: rest => return .next (if b ≤ a then target else pc + 1) rest memory - | .bgeu target, .i64 b :: .i64 a :: rest => return .next (if b ≤ a then target else pc + 1) rest memory - -- Calls; every argument must be initialized. A vector constructor writes into a kind-2 home. - | .call callee argc, _ => - if stack.length < argc || (stack.take argc).any (! ·.initialized) then none else - return .call callee (stack.take argc).reverse (stack.drop argc) memory - | .newVector256 callee argc, _ => - if stack.length < argc || (stack.take argc).any (! ·.initialized) then none else - return .construct callee - (.ref (.home frame 2 pc 0) :: (stack.take argc).reverse) (stack.drop argc) - (writeVectorHome memory frame 2 pc 0) - -- Hardware features, intrinsics and memory operations. - | .feature id, _ => - return .next (pc + 1) (.i32 (if profile.evaluate id then 1 else 0) :: stack) memory - | .intrinsic operation argc, _ => - if stack.length < argc || !operation.available profile then none else - return .next (pc + 1) - ((← evalIntrinsic operation (stack.take argc).reverse) :: stack.drop argc) memory - | .memory operation, _ => - let (updated, values) ← evalMemory operation stack memory - return .next (pc + 1) values updated - | .skipInit, .object _ :: rest => return .next (pc + 1) rest memory - | .skipInit, .ref _ :: rest => return .next (pc + 1) rest memory - | .asRef, .ref a :: rest => return .next (pc + 1) (.ref a :: rest) memory + | .blt_un target, .i32 b :: .i32 a :: rest => + return .next (if a < b then target else pc + 1) rest memory + | .blt_un target, .i64 b :: .i64 a :: rest => + return .next (if a < b then target else pc + 1) rest memory + | .ble_un target, .i32 b :: .i32 a :: rest => + return .next (if a ≤ b then target else pc + 1) rest memory + | .ble_un target, .i64 b :: .i64 a :: rest => + return .next (if a ≤ b then target else pc + 1) rest memory + | .bgt_un target, .i32 b :: .i32 a :: rest => + return .next (if b < a then target else pc + 1) rest memory + | .bgt_un target, .i64 b :: .i64 a :: rest => + return .next (if b < a then target else pc + 1) rest memory + | .bge_un target, .i32 b :: .i32 a :: rest => + return .next (if b ≤ a then target else pc + 1) rest memory + | .bge_un target, .i64 b :: .i64 a :: rest => + return .next (if b ≤ a then target else pc + 1) rest memory -- Return. - | .ret narrow, _ => returnFromStack narrow returns stack memory + | .ret, _ => returnFromStack narrow returns stack memory | _, _ => none end CIL diff --git a/CIL/StructuralLabels.lean b/CIL/StructuralLabels.lean index d91b1d20b..3f83d5104 100644 --- a/CIL/StructuralLabels.lean +++ b/CIL/StructuralLabels.lean @@ -36,7 +36,7 @@ def loops (method : Expr) : MetaM (Array (Nat × Nat)) := do repeat match (← whnf cell).getAppFnArgs with | (``List.cons, #[_, op, tail]) => - for target in ← nats (mkApp (mkConst ``CIL.Op.targets) op) do + for target in ← nats (mkApp (mkConst ``CIL.Instruction.targets) op) do if target ≤ pc then latches := latches.insert target pc cell := tail pc := pc + 1 @@ -69,7 +69,8 @@ elab_rules : command ⟨rfl, by decide⟩)) /-- `bind_call site := callee from parent` names the only call from `parent` to the bound -method `callee`, and `[k]` the `k`-th in pc order; `site.call` states its instruction. -/ +method `callee`, and `[k]` the `k`-th in pc order; `site.call` states its instruction and +`site.link` its link. -/ syntax (name := bindCall) (docComment)? "bind_call " ident " := " term:max " from " term:max (" [" num "]")? : command @@ -83,8 +84,7 @@ elab_rules : command callSite parent method (table[method]?.getD s!"method {method}") (occurrence.map (·.getNat)) elabCommand (← `(@[cil_code] def $label : Nat := $(quote site.pc))) CIL.MethodIdentity.documentBinding doc label - let instruction ← liftMacroM (site.instruction callee) - elabAtPc site.pc (← `(@[cil_code] theorem $(mkIdent (label.getId ++ `call)) : - ($parent).code[$label]? = some $instruction := rfl)) + CIL.MethodIdentity.emitCallFact (mkIdent (label.getId ++ `call)) parent site + CIL.MethodIdentity.emitLink (mkIdent (label.getId ++ `link)) parent site.pc callee end CIL.StructuralLabels diff --git a/CIL/SymbolicExecution.lean b/CIL/SymbolicExecution.lean index da52580d3..e96d18deb 100644 --- a/CIL/SymbolicExecution.lean +++ b/CIL/SymbolicExecution.lean @@ -23,10 +23,10 @@ theorem fin_mk_ofNat {n k : Nat} [NeZero n] (bound : k < n) : simp [Fin.val_ofNat, Nat.mod_eq_of_lt bound] /-- A candidate fuel budget for the selected method and later callees: one more than their - supported instruction count. Each execution proof establishes that it suffices. -/ + instruction count, holes excluded. Each execution proof establishes that it suffices. -/ def executionBound (program : Program) (method : Nat) : Nat := (program.drop method).foldl (fun total body => total + - (body.code.filter fun op => match op with | .unsupported _ => false | _ => true).length) 0 + 1 + (body.code.filter fun op => match op with | .unadmitted _ => false | _ => true).length) 0 + 1 open Lean Elab Command in /-- `if_extracted name { commands }` elaborates a summary candidate when `name` exists. It is diff --git a/CIL/TermAccess.lean b/CIL/TermAccess.lean index 3098eb02a..08c395ef0 100644 --- a/CIL/TermAccess.lean +++ b/CIL/TermAccess.lean @@ -8,7 +8,8 @@ arrays through these accessors. Core's `xs[i]!`, `Expr.getArg!`, `Expr.appArg!` `Expr.appFn!` return a default value after printing a panic if the term has an unexpected shape; the caller then continues with that value. Each accessor here either reports the absent part as `none`, for code that probes a term's shape, or fails with an error naming -its reader and the term, for code that has committed to a shape. +its reader and the term, for code that has committed to a shape. `addFact` adds a proved +hypothesis to a goal for the tactics that restate facts at each step. -/ namespace CIL @@ -78,4 +79,45 @@ def entryAt {α : Type} (reader : String) (entries : Array α) (index : Nat) : m end Committed +/-- The name `base`, or `base_index` from the first suffix on, for the hypotheses a step + introduces. -/ +def suffixedName (index : Nat) (base : String) : Name := + Name.mkSimple (if index == 0 then base else s!"{base}_{index}") + +/-- The least suffix at which no name of `bases` is in use in `context`, found in one pass over + the context; probing each suffix in turn would scan the context once per suffix. -/ +def firstUnusedSuffix (context : LocalContext) (bases : Array String) : Nat := Id.run do + let mut used : Std.HashSet Nat := {} + for declaration in context do + let .str .anonymous text := declaration.userName | continue + for base in bases do + if text == base then + used := used.insert 0 + else if let some digits := text.dropPrefix? (base ++ "_") then + let digits := digits.toString + -- Only the spelling `suffixedName` produces counts, not `base_01`. + if let some suffix := digits.toNat? then + if suffix != 0 && toString suffix == digits then used := used.insert suffix + let mut index := 0 + while used.contains index do + index := index + 1 + return index + +open Meta in +/-- Adds the hypothesis `name : type`, proved by `proof`, to `goal`, and returns it with the goal + that follows. The proof term is `have name : type := proof; rest`, so `proof` occurs once. + `assert` followed by `intro` gives the same goal, but instantiating it substitutes `proof` for + each use of the hypothesis; a fact restated from the previous one at every step then carries + every earlier proof, under a different number of binders each time, so the term of a walk + grows with the square of its steps. -/ +def addFact (goal : MVarId) (name : Name) (type proof : Expr) : MetaM (FVarId × MVarId) := + goal.withContext do + let target ← goal.getType + withLocalDeclD name type fun fact => do + let rest ← mkFreshExprSyntheticOpaqueMVar target (← goal.getTag) + let .lam _ _ body _ ← mkLambdaFVars #[fact] rest + | throwError "addFact: abstracting {name} gave no binder" + goal.assign (.letE name type proof body (nondep := true)) + return (fact.fvarId!, rest.mvarId!) + end CIL diff --git a/CIL/TypeExpr.lean b/CIL/TypeExpr.lean index ecec3bd5a..5c65545fa 100644 --- a/CIL/TypeExpr.lean +++ b/CIL/TypeExpr.lean @@ -1,34 +1,47 @@ import Std +import CIL.Identity /-! # Metadata type expressions A scope-qualified form of ECMA-335 signature types, used by extraction and by typed import -slots to compare signatures as values. Custom modifiers, whose placement the ECMA-335 augments -widen, are not represented; see `docs/SOUNDNESS-SPEC.md` for the admitted placements. +slots to compare signatures as values. Open member declarations preserve custom modifiers. +See `docs/SOUNDNESS-SPEC.md` for the admitted placements. +Ordered type keys and candidate buckets let dispatch and layout tables find entries by type. -/ namespace CIL.Extraction -/-- The assembly scope of the .NET 10 reference assembly that defines the primitive types. -/ +/-- The assembly scope of the .NET 11 reference assembly that defines the primitive types. -/ def runtimeScope : String := { toByteArray := ⟨#[ 83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 44, 32, - 86, 101, 114, 115, 105, 111, 110, 61, 49, 48, 46, 48, 46, 48, 46, 48, + 86, 101, 114, 115, 105, 111, 110, 61, 49, 49, 46, 48, 46, 48, 46, 48, 44, 32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, 98, 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 98, 48, 51, 102, 53, 102, 55, 102, 49, 49, 100, 53, 48, 97, 51, 97]⟩, isValidUTF8 := - ("System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" + ("System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" ).isValidUTF8 } +/-- The defining assembly of the .NET 11 CoreLib implementation. -/ +def coreLibraryScope : String := + "System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" + /-- The byte-backed scope retains the exact original assembly identity. -/ theorem runtimeScope_eq : runtimeScope = - "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" := by rfl + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" := by + rfl #print axioms runtimeScope_eq +/-- The .NET 11 reference assembly that holds the `TypeDef` rows of the primitive types and of the + modelled exceptions; `runtimeScope` is its display name. -/ +def runtimeAssembly : AssemblyName := + { name := name!"System.Runtime", version := ⟨11, 0, 0, 0⟩, culture := name!"", + publicKeyToken := some 0xb03f5f7f11d50a3a } + /-- Scoped metadata types shared by extraction and typed import slots. -/ inductive TypeExpr where | void @@ -45,17 +58,38 @@ inductive TypeExpr where (hasThis explicitThis : Bool) /-- One parameter in a function-pointer signature; the parameter chain ends in `.void`. -/ | signatureParameter (head tail : TypeExpr) + /-- The generic definition of a value type, by its exact scope and name; an `apply` chain over + it is a value type instance. -/ | constructor (scope name : String) + /-- The generic definition of a class, interface or delegate, by its exact scope and name; an + `apply` chain over it is a reference type instance. -/ + | objectConstructor (scope name : String) | apply (constructor argument : TypeExpr) - /-- A class on the checked heap, by its exact scope and name: an admitted class, or, as the - type a class field holds, a class outside the model whose references are never read - through. -/ + /-- A class on the checked heap, by its exact scope and name: a class of a pinned assembly or + `System.String`, or, as the type a class instance field holds, any class or interface, + whose references the field never reads through. -/ | object (scope name : String) + /-- A custom modifier of an open member type, with the modifier's assembly and full name. -/ + | modified (required : Bool) (scope name : String) (element : TypeExpr) + /-- An owner variable (!n) in an open declaration. A method variable (!!n) uses `parameter`. -/ + | ownerParameter (index : Nat) deriving DecidableEq, Repr +/-- The exact runtime value-type constructor of a mutable or read-only span. -/ +def TypeExpr.isSpanHead : TypeExpr → Bool + | .constructor scope name => + scope == runtimeScope && (name == "System.Span`1" || name == "System.ReadOnlySpan`1") + | _ => false + +/-- A runtime span application retains its type when the element is specialized. -/ +def TypeExpr.isRuntimeSpan : TypeExpr → Bool + | .apply owner _ => owner.isSpanHead + | _ => false + /-- Structural substitution includes nested type constructors, not just signature strings. -/ def substituteType (arguments : List TypeExpr) : TypeExpr → Option TypeExpr | .parameter index => arguments[index]? + | .ownerParameter _ => none | .byref element => TypeExpr.byref <$> substituteType arguments element | .pointer element => TypeExpr.pointer <$> substituteType arguments element | .array element => TypeExpr.array <$> substituteType arguments element @@ -65,8 +99,87 @@ def substituteType (arguments : List TypeExpr) : TypeExpr → Option TypeExpr | .signatureParameter head tail => do return .signatureParameter (← substituteType arguments head) (← substituteType arguments tail) | .enum identity underlying => TypeExpr.enum identity <$> substituteType arguments underlying + | .modified required scope name element => + .modified required scope name <$> substituteType arguments element | .apply constructor argument => do return .apply (← substituteType arguments constructor) (← substituteType arguments argument) | type => some type +/-- Whether the type is an `apply` chain over the generic definition of a class, interface or + delegate, so that a value of it is an object reference. -/ +def TypeExpr.objectApplication : TypeExpr → Bool + | .apply (.objectConstructor _ _) _ => true + | .apply head _ => head.objectApplication + | _ => false + +/-- Ordered candidate buckets retain every matching metadata entry. -/ +def groupIndex {α κ : Type} [Ord κ] (key : α → κ) : + List α → Std.TreeMap κ (List α) + | [] => ∅ + | value :: rest => + let index := groupIndex key rest + index.insert (key value) (value :: index.getD (key value) []) + +/-- Each bucket is exactly the corresponding ordered filter of the input. -/ +theorem groupIndex_get {α κ : Type} [Ord κ] [Std.TransOrd κ] [Std.LawfulEqOrd κ] + [BEq κ] [LawfulBEq κ] (key : α → κ) (values : List α) (query : κ) : + (groupIndex key values).getD query [] = values.filter (fun value => key value == query) := by + induction values with + | nil => simp [groupIndex] + | cons value rest ih => + simp only [groupIndex, Std.TreeMap.getD_insert, Std.compare_eq_iff_eq] + by_cases same : key value = query + · simp [same, ih] + · simp [same, ih] + +#print axioms groupIndex_get + +/-- Linked UTF-8 bytes avoid indexed string decoding while comparing type keys. -/ +def keyBytes (text : String) : List UInt8 := text.toByteArray.data.toList + +/-- `runtimeScope` is the display name of `runtimeAssembly`, compared in bytes because display + text does not reduce in the kernel; a version or token changed in only one of them fails it. -/ +theorem runtimeScope_spells_runtimeAssembly : + runtimeAssembly.culture = name!"" ∧ + (runtimeAssembly.name.bytes?.getD []).map Nat.toUInt8 ++ + keyBytes (", Version=" ++ runtimeAssembly.version.display ++ + ", Culture=neutral, PublicKeyToken=" ++ + publicKeyTokenText runtimeAssembly.publicKeyToken) = + keyBytes runtimeScope := by + decide +kernel + +#print axioms runtimeScope_spells_runtimeAssembly + +/-- Constructor-delimited type buckets retain scopes and aggregate positions. Lookups through + these keys still compare full identities, in case two encodings were to share a bucket. -/ +def typeKeyInto : TypeExpr → List (List UInt8) → List (List UInt8) + | .modified required scope name element, rest => + keyBytes (if required then "modreq" else "modopt") :: keyBytes scope :: keyBytes name :: + typeKeyInto element rest + | .void, rest => keyBytes "void" :: rest + | .atom scope name, rest => keyBytes "atom" :: keyBytes scope :: keyBytes name :: rest + | .aggregate identity index, rest => + keyBytes "aggregate" :: keyBytes identity :: keyBytes (toString index) :: rest + | .enum identity underlying, rest => + keyBytes "enum" :: keyBytes identity :: typeKeyInto underlying rest + | .parameter index, rest => keyBytes "parameter" :: keyBytes (toString index) :: rest + | .ownerParameter index, rest => keyBytes "ownerParameter" :: keyBytes (toString index) :: rest + | .byref element, rest => keyBytes "byref" :: typeKeyInto element rest + | .pointer element, rest => keyBytes "pointer" :: typeKeyInto element rest + | .array element, rest => keyBytes "array" :: typeKeyInto element rest + | .functionPointer result parameters convention hasThis explicitThis, rest => + keyBytes "functionPointer" :: keyBytes (toString convention) :: + keyBytes (toString hasThis) :: keyBytes (toString explicitThis) :: + typeKeyInto result (typeKeyInto parameters rest) + | .signatureParameter head tail, rest => + keyBytes "signatureParameter" :: typeKeyInto head (typeKeyInto tail rest) + | .constructor scope name, rest => + keyBytes "constructor" :: keyBytes scope :: keyBytes name :: rest + | .objectConstructor scope name, rest => + keyBytes "objectConstructor" :: keyBytes scope :: keyBytes name :: rest + | .apply constructor argument, rest => + keyBytes "apply" :: typeKeyInto constructor (typeKeyInto argument rest) + | .object scope name, rest => + keyBytes "object" :: keyBytes scope :: keyBytes name :: rest + end CIL.Extraction diff --git a/CIL/Types.lean b/CIL/Types.lean index 410afa1c3..8cd4dcd21 100644 --- a/CIL/Types.lean +++ b/CIL/Types.lean @@ -26,7 +26,7 @@ def SnapshotShape.width : SnapshotShape → Nat | .vector256 => 32 | .vector512 => 64 -/-- A memory location. The four kinds are disjoint address spaces. -/ +/-- A memory location. The three kinds are disjoint address spaces. -/ inductive Address where /-- A byte of caller-provided storage; aggregate references may overlap. -/ | byte (address : Nat) @@ -34,9 +34,6 @@ inductive Address where | local (frame index : Nat) /-- Byte `offset` of read-only data extracted from the assembly. -/ | static (bytes : List (BitVec 8)) (offset : Nat) - /-- Byte `offset` of a private 32-byte vector home; `kind` is 0 for locals, 1 for - arguments and 2 for construction temporaries. -/ - | home (frame kind index offset : Nat) deriving DecidableEq, Repr /-- An evaluation-stack or memory value. -/ diff --git a/CIL/VectorMemory.lean b/CIL/VectorMemory.lean index 9545f95b3..b717d1d8b 100644 --- a/CIL/VectorMemory.lean +++ b/CIL/VectorMemory.lean @@ -7,8 +7,8 @@ import CIL.WideMultiply /-! # Vector memory and unsafe references -128-, 256- and 512-bit loads and stores, `Unsafe` reference arithmetic, and the memory -instructions (`MemoryOp`) of the reference interpreter. In caller storage a vector is +128-, 256- and 512-bit loads and stores, `Unsafe` reference arithmetic, and the operations +bound to modeled memory calls. In caller storage a vector is stored little-endian, byte by byte, like scalars in `CIL.Memory`, so vector and scalar accesses to the same bytes agree. A scalar local holds a whole vector value instead. @@ -18,8 +18,7 @@ Exact overload references (.NET 10): https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.memorymarshal.getreference?view=net-10.0 https://learn.microsoft.com/dotnet/api/system.readonlyspan-1.-ctor?view=net-10.0 Unsafe.Add scales by element size. BitCast preserves storage bits. - The shared value evaluator models static byte spans. Checked array-backed and - reference-backed primitive spans use the memory rules in CIL.Safety. + Checked static, array-backed and reference-backed spans use the memory rules in CIL.Safety. Static bytes come from the extracted RVA field. -/ @@ -33,67 +32,43 @@ def read128 (memory : Memory) : Address → Option Value let .v128 bits ← memory (.local frame index) | none return .v128 bits | .static bytes base => do return .v128 (BitVec.ofNat 128 (← readStaticBytes bytes base 16)) - | .home frame kind index offset => - if offset + 16 ≤ 32 then do - return .v128 (BitVec.ofNat 128 (← readHomeBytes memory frame kind index offset 16)) - else none -/-- Reads a 256-bit vector; a home read must cover the whole 32-byte home. -/ +/-- Reads a 256-bit vector. -/ def read256 (memory : Memory) : Address → Option Value | .byte base => do return .v256 (BitVec.ofNat 256 (← readBytes memory base 32)) | .local frame index => do let .v256 bits ← memory (.local frame index) | none return .v256 bits | .static bytes base => do return .v256 (BitVec.ofNat 256 (← readStaticBytes bytes base 32)) - | .home frame kind index offset => - if offset + 32 ≤ 32 then do - return .v256 (BitVec.ofNat 256 (← readHomeBytes memory frame kind index offset 32)) - else none -/-- Writes a 128-bit vector. Read-only data cannot be written; a home write must stay - within its 32 bytes. -/ +/-- Writes a 128-bit vector. Read-only data cannot be written. -/ def write128 (memory : Memory) (address : Address) (bits : BitVec 128) : Option Memory := match address with | .byte base => some (writeBytes memory base bits.toNat 16) | .local frame index => some (write memory (.local frame index) (.v128 bits)) | .static _ _ => none - | .home frame kind index offset => - if offset + 16 ≤ 32 then some (writeHomeBytes memory frame kind index offset bits.toNat 16) - else none -/-- Writes a 256-bit vector. Read-only data cannot be written; a home write must cover the - whole 32-byte home. -/ +/-- Writes a 256-bit vector. Read-only data cannot be written. -/ def write256 (memory : Memory) (address : Address) (bits : BitVec 256) : Option Memory := match address with | .byte base => some (writeBytes memory base bits.toNat 32) | .local frame index => some (write memory (.local frame index) (.v256 bits)) | .static _ _ => none - | .home frame kind index offset => - if offset + 32 ≤ 32 then some (writeHomeBytes memory frame kind index offset bits.toNat 32) - else none -/-- Reads a 512-bit vector; it does not fit in a home, so a home read is stuck. -/ +/-- Reads a 512-bit vector. -/ def read512 (memory : Memory) : Address → Option Value | .byte base => do return .v512 (BitVec.ofNat 512 (← readBytes memory base 64)) | .local frame index => do let .v512 bits ← memory (.local frame index) | none return .v512 bits | .static bytes base => do return .v512 (BitVec.ofNat 512 (← readStaticBytes bytes base 64)) - | .home .. => none -/-- Writes a 512-bit vector; read-only data cannot be written and a home is too small. -/ +/-- Writes a 512-bit vector; read-only data cannot be written. -/ def write512 (memory : Memory) (address : Address) (bits : BitVec 512) : Option Memory := match address with | .byte base => some (writeBytes memory base bits.toNat 64) | .local frame index => some (write memory (.local frame index) (.v512 bits)) - | .static .. | .home .. => none - -/-- Only actual pointer representations may be reinterpreted as managed refs. - In particular, a vector snapshot cannot become a pointer to caller storage. -/ -def unsafeAsRef : Value → Option Value - | .object base => some (.ref (.byte base)) - | .ref address => some (.ref address) - | _ => none + | .static .. => none /-- Byte-address arithmetic is unbounded, as in the existing scalar memory model. Negative offsets must remain nonnegative. Local slots are typed @@ -107,9 +82,6 @@ def unsafeAdd (sizeBytes : Nat) (offset : Int) : Address → Option Address | .static bytes base => let address := (base : Int) + (sizeBytes : Int) * offset if 0 ≤ address then some (.static bytes address.toNat) else none - | .home frame kind index base => - let address := (base : Int) + (sizeBytes : Int) * offset - if 0 ≤ address ∧ address ≤ 32 then some (.home frame kind index address.toNat) else none @[simp ↓] theorem unsafeAdd_byte_natural (size base offset : Nat) : unsafeAdd size (offset : Int) (.byte base) = some (.byte (base + size * offset)) := by @@ -161,7 +133,7 @@ inductive MemoryOp where /-- Signed `Math.DivRem(left, right, out remainder)` on `width`-bit integers. -/ | divRemOut (width : Nat) | copyBlock (alignment : Nat) | initBlock (alignment : Nat) - | load128 | load256 | load512 | store128 | store256 | store512 | init256 + | load128 | load256 | load512 | store128 | store256 | store512 /-- `Vector128.LoadUnsafe` or `StoreUnsafe` on bytes: an unsigned native byte offset, when present, displaces the reference as `Unsafe.Add` does, then sixteen bytes move as in `load128` or `store128`. The store takes its vector below the reference. -/ @@ -175,11 +147,9 @@ inductive MemoryOp where | marshalWrite (kind : TransferKind) (tryWrite : Bool) /-- A typed pointer transfer with independently validated alignment. -/ | pointerTransfer (kind : TransferKind) (alignment : Nat) (store : Bool) - | bitcast256 | bitcastByteBool | skipInit + | bitcastByteBool | bitcastValue (source target : SnapshotShape) - | staticAddress (bytes : List (BitVec 8)) | spanCreate | spanReference - /-- The value of a constructor-initialized readonly field; its address is never formed. -/ - | staticValue (bytes : List (BitVec 8)) + | spanCreate | spanReference /-- A span of `width`-byte elements over a whole static field; indexing is bounds checked. -/ | createSpan (width : Nat) | spanItem (width : Nat) | spanLength /-- Primitive spans over supplied storage; these operations never grant memory authority. -/ @@ -189,19 +159,8 @@ inductive MemoryOp where | spanSlice (explicitLength : Bool) | spanReadOnly /-- A span view over supplied reference-free array storage, without allocation or copying. -/ | arraySpan (element : ArrayElement) (writable : Bool) (range : ArraySpanRange) - /-- Integer operations over supplied array storage; allocation is not modelled. -/ - | arrayLength | arrayLoad (width : Nat) (signed : Bool) | arrayStore (width : Nat) - /-- A checked element address retains allocation provenance without granting authority. -/ - | arrayAddress (element : ArrayElement) (writable : Bool := true) - /-- A typed reference-free element transfer retains byte initialization masks. -/ - | arrayTransfer (element : ArrayElement) (store : Bool) /-- `Unsafe.NullRef` pushes the null reference; `Unsafe.IsNullRef` tests for it. -/ | nullRef | isNullRef - /-- `ldnull` pushes the null object reference, distinct from a null byref. -/ - | nullObject - /-- `ldstr`: the interned string object the caller bound to `text`. No operation reads its - characters. -/ - | stringLiteral (text : String) /-- `ArgumentNullException.ThrowIfNull(argument, paramName)`: throws for a null argument. The parameter name is an opaque string nothing reads. -/ | throwIfNull @@ -211,30 +170,10 @@ inductive MemoryOp where /-- An `ArgumentOutOfRangeException` guard comparing a `width`-bit integer with a bound. The parameter name is an opaque string nothing reads. -/ | comparisonGuard (guard : ComparisonGuard) (width : Nat) (signed : Bool) - /-- `ldfld` of a scalar field of a class instance of type `owner`, at its model offset. -/ - | objectField (owner : CIL.Extraction.TypeExpr) (offset width : Nat) (signed : Bool) - /-- `stfld` of a scalar field of a class instance of type `owner`, at its model offset. -/ - | setObjectField (owner : CIL.Extraction.TypeExpr) (offset width : Nat) - /-- `ldfld` of a class-typed field of a class instance of type `owner`, at its model offset: - the object reference the field holds, never bytes. -/ - | objectReferenceField (owner : CIL.Extraction.TypeExpr) (offset : Nat) - /-- `stfld` of a class-typed field of a class instance of type `owner`, at its model offset. -/ - | setObjectReferenceField (owner : CIL.Extraction.TypeExpr) (offset : Nat) - /-- `ldlen` of an array of class references: its element count, as a native integer. -/ - | referenceArrayLength - /-- `ldelem.ref` of an array of class references: the reference its element's slot holds. -/ - | referenceArrayLoad - /-- `stelem.ref` of an array of class references, after the CLR's covariance check. -/ - | referenceArrayStore - /-- `ldfld` of a field of a class instance of type `owner` holding an array of `element`: - the whole array the reference in its slot names. -/ - | arrayField (owner : CIL.Extraction.TypeExpr) (offset : Nat) (element : ArrayElement) - /-- `stfld` of a field of a class instance of type `owner` holding an array of `element`. -/ - | setArrayField (owner : CIL.Extraction.TypeExpr) (offset : Nat) (element : ArrayElement) - deriving Repr + deriving DecidableEq, Repr /-- Bytes the model reserves for each class instance field, in declaration order. The layout is - sound only while code cannot observe field offsets, so `ldflda` on a class is rejected. -/ + sound only while code cannot observe field offsets, so `ldflda` on a class faults. -/ def objectFieldSlot : Nat := 8 /-- The model offset of the class instance field at `index` among the instance fields. -/ @@ -259,18 +198,11 @@ def MemoryOp.argumentCount : MemoryOp → Nat | .spanFromReference .. | .byteOffset => 2 | .spanSingleReference .. => 1 | .arraySpan _ _ range => range.argumentCount - | .spanReinterpret .. | .spanReadOnly | .arrayLength => 1 - | .arrayLoad .. | .arrayAddress .. => 2 - | .arrayTransfer _ store => if store then 3 else 2 - | .arrayStore _ | .referenceArrayStore => 3 - | .referenceArrayLoad => 2 - | .store128 | .store256 | .store512 | .add .. | .spanCreate | .spanItem _ - | .setObjectField .. | .setObjectReferenceField .. | .setArrayField .. => 2 - | .staticAddress _ | .staticValue _ | .nullRef | .nullObject | .stringLiteral _ - | .spanEmpty .. => 0 - | .load128 | .load256 | .load512 | .init256 | .asRef | .bitcast256 | .bitcastByteBool - | .skipInit | .bitcastValue .. | .createSpan _ | .spanReference | .spanLength | .isNullRef - | .objectField .. | .objectReferenceField .. | .referenceArrayLength | .arrayField .. => 1 + | .spanReinterpret .. | .spanReadOnly => 1 + | .store128 | .store256 | .store512 | .add .. | .spanCreate | .spanItem _ => 2 + | .nullRef | .spanEmpty .. => 0 + | .load128 | .load256 | .load512 | .asRef | .bitcastByteBool | .bitcastValue .. | .createSpan _ + | .spanReference | .spanLength | .isNullRef => 1 /-- Signed Int32/native and unsigned native element offsets on the 64-bit target. -/ def offsetValue (signed : Bool) (value : Value) : Option Int := @@ -280,74 +212,4 @@ def offsetValue (signed : Bool) (value : Value) : Option Int := | false, .i64 bits => some (bits.toNat : Int) | _, _ => none -/-- Runs a memory instruction on the evaluation stack (top first), giving the new memory and - stack. Ill-typed operands, out-of-range accesses and writes to read-only data are stuck. -/ -def evalMemory (op : MemoryOp) (stack : List Value) (memory : Memory) : - Option (Memory × List Value) := do - match op, stack with - -- `Math.BigMul`: the low word is stored through the reference, the high word is pushed. - | .bigMul64 false, .ref low :: .i64 right :: .i64 left :: rest => - return (← write64 memory low (left * right), .i64 (WideMultiply.high64 left right) :: rest) - | .bigMul64 true, .ref low :: .i64 right :: .i64 left :: rest => - return (← write64 memory low (left * right), - .i64 (WideMultiply.signedHigh64 left right) :: rest) - -- Vector loads and stores, through a reference or an object; `init256` stores zero. - | .load128, .ref address :: rest => return (memory, (← read128 memory address) :: rest) - | .load256, .ref address :: rest => return (memory, (← read256 memory address) :: rest) - | .load256, .object base :: rest => return (memory, (← read256 memory (.byte base)) :: rest) - | .load512, .ref address :: rest => do return (memory, (← read512 memory address) :: rest) - | .store512, .v512 bits :: .ref address :: rest => - return (← write512 memory address bits, rest) - | .store128, .v128 bits :: .ref address :: rest => - return (← write128 memory address bits, rest) - | .store256, .v256 bits :: .ref address :: rest => - return (← write256 memory address bits, rest) - | .store256, .v256 bits :: .object base :: rest => - return (← write256 memory (.byte base) bits, rest) - | .init256, .ref address :: rest => return (← write256 memory address 0, rest) - -- `Vector128.LoadUnsafe` and `StoreUnsafe`, the offset in bytes above the reference. - | .vector128Unsafe false false, .ref address :: rest => - return (memory, (← read128 memory address) :: rest) - | .vector128Unsafe false true, offset :: .ref address :: rest => - let moved ← unsafeAdd 1 (← offsetValue false offset) address - return (memory, (← read128 memory moved) :: rest) - | .vector128Unsafe true false, .ref address :: .v128 bits :: rest => - return (← write128 memory address bits, rest) - | .vector128Unsafe true true, offset :: .ref address :: .v128 bits :: rest => - let moved ← unsafeAdd 1 (← offsetValue false offset) address - return (← write128 memory moved bits, rest) - | .init256, .object base :: rest => return (← write256 memory (.byte base) 0, rest) - -- `Unsafe` reinterpretation, offsets and skipped initialization. - | .asRef, v :: rest => return (memory, (← unsafeAsRef v) :: rest) - | .add size signed, offset :: .ref address :: rest => - return (memory, .ref (← unsafeAdd size (← offsetValue signed offset) address) :: rest) - | .bitcast256, .v256 bits :: rest => return (memory, .v256 bits :: rest) - | .bitcastByteBool, .i32 bits :: rest => - if bits.toNat < 256 then return (memory, .i32 bits :: rest) else none - | .bitcastByteBool, .i8 bits :: rest => - return (memory, .i32 (bits.zeroExtend 32) :: rest) - | .skipInit, .object _ :: rest | .skipInit, .ref _ :: rest => return (memory, rest) - -- Null references. - | .nullRef, _ => return (memory, .nullRef :: stack) - | .isNullRef, .nullRef :: rest => return (memory, .i32 1 :: rest) - | .isNullRef, .object _ :: rest | .isNullRef, .ref _ :: rest => return (memory, .i32 0 :: rest) - -- Read-only static data and spans over it; span indexing is bounds checked. - | .createSpan width, .ref (.static bytes 0) :: rest => - if 0 < width ∧ bytes.length % width = 0 then - return (memory, .span (.static bytes 0) (bytes.length / width) :: rest) - else none - | .spanItem width, .i32 index :: .span (.static bytes offset) length :: rest => - if index.toNat < length then - return (memory, .ref (.static bytes (offset + index.toNat * width)) :: rest) - else none - | .spanLength, .span _ length :: rest => return (memory, .i32 (BitVec.ofNat 32 length) :: rest) - | .staticAddress bytes, _ => return (memory, .ref (.static bytes 0) :: stack) - | .spanCreate, .i32 length :: .ref (.static bytes offset) :: rest => - if 0 ≤ length.toInt ∧ offset + length.toNat ≤ bytes.length then - return (memory, .span (.static bytes offset) length.toNat :: rest) - else none - | .spanReference, .span address length :: rest => - if length = 0 then none else return (memory, .ref address :: rest) - | _, _ => none - end CIL diff --git a/CIL/VectorMemoryLemmas.lean b/CIL/VectorMemoryLemmas.lean index f290cce05..08b712c1a 100644 --- a/CIL/VectorMemoryLemmas.lean +++ b/CIL/VectorMemoryLemmas.lean @@ -23,15 +23,6 @@ namespace CIL some (.static bytes (size * offset)) := by simp [CIL.unsafeAdd, ← Int.natCast_mul] -@[simp ↓] theorem eval_add_static_vector (m : CIL.Memory) (bytes : List (BitVec 8)) - (index : CIL.W32) (rest : List CIL.Value) : - CIL.evalMemory (.add 32 false) - (.i64 (index.zeroExtend 64) :: .ref (.static bytes 0) :: rest) m = - some (m, .ref (.static bytes (32 * index.toNat)) :: rest) := by - have bound : index.toNat < 2^64 := by have hi := index.isLt; omega - simp [CIL.evalMemory, CIL.offsetValue, BitVec.zeroExtend, BitVec.toNat_setWidth, - Nat.mod_eq_of_lt bound] - /-- Each byte inside a written range holds the matching little-endian byte of the value. -/ theorem writeBytes_inside (m : Memory) (base word count address : Nat) (h : base ≤ address ∧ address < base + count) : @@ -67,7 +58,6 @@ theorem writeBytes_overwrite_same (m : Memory) (base old new count : Nat) : writeBytes_outside _ _ _ _ _ outside, writeBytes_outside _ _ _ _ _ outside] | «local» frame index => simp | static bytes offset => simp - | home frame kind index offset => simp /-- A successful read of `count` bytes is a number below `256^count`. -/ theorem readBytes_bound (m : Memory) (base count number : Nat) diff --git a/Examples/Assembly/DivisionIntrinsics.cs b/Examples/Assembly/DivisionIntrinsics.cs index 5ed16968f..5315384cc 100644 --- a/Examples/Assembly/DivisionIntrinsics.cs +++ b/Examples/Assembly/DivisionIntrinsics.cs @@ -47,6 +47,8 @@ public static int Greater(ulong left, ulong right) return Avx512DQ.MoveMask(Avx512F.CompareGreaterThan(first, second)); } public static ulong UnsafeLow(ulong value) => Vector128.CreateScalarUnsafe(value).GetElement(0); + // Calls UnsafeLow, so a proof can apply a summary of a method whose body runs vector calls. + public static ulong UnsafeLowNext(ulong value) => UnsafeLow(value) + 1; public static Vector128 UnsafeReturn(ulong value) => Vector128.CreateScalarUnsafe(value); public static ulong UnsafeCall(ulong value) => ReadLow(Vector128.CreateScalarUnsafe(value)); private static ulong ReadLow(Vector128 value) => value.GetElement(0); @@ -54,11 +56,23 @@ public static void UnsafeStore(ref Vector128 destination, ulong value) => destination = Vector128.CreateScalarUnsafe(value); public static ulong UnsafeHigh(ulong value) => Vector128.CreateScalarUnsafe(value).GetElement(1); public static bool UnsafeEquals(ulong value) => Vector128.CreateScalarUnsafe(value) == Vector128.CreateScalarUnsafe(value); + public static bool EqualWords128(uint a, uint b, uint c, uint d, Vector128 expected) => + Vector128.Create(a, b, c, d) == expected; + public static bool EqualBytes256(Vector256 left, Vector256 right) => left == right; + public static bool EqualSigned512(Vector512 left, Vector512 right) => left == right; public static void Init128(ref Vector128 value) => value = default; public static void Init256(ref Vector256 value) => value = default; public static void Init512(ref Vector512 value) => value = default; public static Vector256 Shuffle256(ReadOnlySpan source, ReadOnlySpan control) => Avx2.Shuffle(Vector256.Create(source), Vector256.Create(control)); + // These compiled variants violate the lane and zero-mask rules of Shuffle256. + public static Vector256 ShuffleCrossHalf(ReadOnlySpan source, ReadOnlySpan control) => + Avx2.Shuffle(Avx2.Permute4x64(Vector256.Create(source).AsUInt64(), 0x4e).AsByte(), Vector256.Create(control)); + + public static Vector256 ShuffleIgnoreHighBit(ReadOnlySpan source, ReadOnlySpan control) => + Avx2.Shuffle(Vector256.Create(source), + Avx2.And(Vector256.Create(control).AsUInt64(), Vector256.Create(0x7f7f7f7f7f7f7f7fUL)).AsByte()); + public static Vector128 Shuffle128(ReadOnlySpan source, ReadOnlySpan control) => Ssse3.Shuffle(Vector128.Create(source), Vector128.Create(control)); // A portable AES hash round: x86 AESENC, else ARM64 AESE with a zero key, AESMC and the key. diff --git a/Examples/Assembly/Examples.csproj b/Examples/Assembly/Examples.csproj index ff456e1fb..c173228bc 100644 --- a/Examples/Assembly/Examples.csproj +++ b/Examples/Assembly/Examples.csproj @@ -1,6 +1,6 @@ - net10.0 + net11.0 Examples 1.0.0 true diff --git a/Examples/Assembly/GenericOwners.cs b/Examples/Assembly/GenericOwners.cs index 47e220c04..4c108cdad 100644 --- a/Examples/Assembly/GenericOwners.cs +++ b/Examples/Assembly/GenericOwners.cs @@ -10,7 +10,8 @@ public static class GenericOwners public static uint Nested(uint value) => OwnerOuter.Inner.Identity(value); - /// A non-generic class nested in a generic one: its owner is `OwnerOuter`1/Helper`. + /// A non-generic class nested in a generic one: + /// its owner is OwnerOuter`1/Helper<!0>. public static uint NestedHelper(uint value) => OwnerOuter.Helper.Identity(value); public static uint Repeated(uint value) => @@ -35,7 +36,7 @@ public static ulong NestedSpanFirst(ReadOnlySpan values) => OwnerOuter.Inner.First(values); } -/// Method-generic span members: the call owner is `ReadOnlySpan`. +/// Method-generic span members: the call owner is ReadOnlySpan<!!0>. public static class SpanHelpers { public static T First(in ReadOnlySpan values) where T : unmanaged => values[0]; diff --git a/Examples/Assembly/Loops.cs b/Examples/Assembly/Loops.cs index 66b65ba16..2ff16b721 100644 --- a/Examples/Assembly/Loops.cs +++ b/Examples/Assembly/Loops.cs @@ -230,4 +230,77 @@ public static void FillThroughSpan(uint[] values, uint value) rest = rest.Slice(1); } } + + // The local order differs from identity order, so layout numbers follow emission order. + [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] + struct Zulu + { + public uint Count; + public uint Spare; + } + + struct Alpha + { + public ulong Sum; + } + + public static ulong TwoStructs(uint value) + { + Zulu zulu = default; + Alpha alpha = default; + while (zulu.Count != value) + { + zulu.Count = unchecked(zulu.Count + 1); + alpha.Sum = unchecked(alpha.Sum + 2); + } + return alpha.Sum; + } + + public struct Inner + { + public uint Count; + } + + public struct Outer + { + public Inner Part; + public uint Other; + } + + // `outer` is named first in the extraction, and `Inner` nests in it, so the two orders of layouts disagree. + public static uint InnerCount(Outer outer, uint value) + { + Inner inner = default; + while (inner.Count != value) inner.Count = unchecked(inner.Count + 1); + return inner.Count; + } + + // Walks down from the reference: the offset of each store is a negative stride, which no loop step names. + public static void FillWordsDown(ref ulong destination, ulong value, int count) + { + for (int i = 0; i < count; i++) + System.Runtime.CompilerServices.Unsafe.Subtract(ref destination, i) = value; + } + + // Loop tests against a small constant: signed and unsigned order, and equality. + public static int CountToFive(int start) + { + var count = start; + while (count < 5) count = unchecked(count + 1); + return count; + } + + public static uint CountUpToFive(uint start) + { + var count = start; + while (count < 5u) count = unchecked(count + 1); + return count; + } + + public static uint ReachFive(uint start) + { + var count = start; + while (count != 5) count = unchecked(count + 1); + return count; + } } diff --git a/Examples/Assembly/Methods.cs b/Examples/Assembly/Methods.cs index 92251712e..7b2e0db0b 100644 --- a/Examples/Assembly/Methods.cs +++ b/Examples/Assembly/Methods.cs @@ -22,6 +22,10 @@ internal static class Arithmetic public static ulong Add(ulong x, ulong y) => unchecked(x + y); public static ulong WrongAdd(ulong x, ulong y) => unchecked(x - y); public static ulong Choose(bool flag, ulong x, ulong y) => flag ? x : y; + // An argument tested against a SizeOf result, which the JIT folds to the size of its element. + public static int WordAdjusted(int value) => value < Unsafe.SizeOf() ? value : unchecked(value + 1); + // A test of two literals once the SizeOf call steps, which an outline decides without a split. + public static int WordSized(int value) => Unsafe.SizeOf() == 8 ? value : unchecked(value + 1); [MethodImpl(MethodImplOptions.NoInlining)] private static ulong Increment(ulong x) => unchecked(x + 1); public static ulong Calls(ulong x) @@ -72,4 +76,11 @@ public static ref ulong Escape() public static ulong TypeCheck(ulong x) => ReferenceEquals(typeof(ulong), typeof(uint)) ? x : unchecked(x + 1); public static ulong Distance(ulong x, ulong y) => x < y ? y - x : x - y; public static int Larger(int x, int y) => x < y ? y : x; + public static int AtLeastFive(int x) => x < 5 ? 5 : x; + public static uint AtMostFive(uint x) => x > 5 ? 5 : x; + public static int FiveToOne(int x) + { + if (x == 5) return 1; + return x; + } } diff --git a/Examples/Assembly/Objects.cs b/Examples/Assembly/Objects.cs index 3468555fc..e7c6cfdeb 100644 --- a/Examples/Assembly/Objects.cs +++ b/Examples/Assembly/Objects.cs @@ -344,6 +344,9 @@ public static ulong Withdraw(Account account, ulong amount) return amount; } + /// The balance when the limit is below a small constant, otherwise zero: an unsigned test. + public static ulong UnderFive(Account account) => account.Limit < 5u ? account.Balance : 0; + /// Two instances of a class with no fields are still two distinct instances. public static bool TwoMarkers() { diff --git a/Examples/Assembly/OutlineHelpers.cs b/Examples/Assembly/OutlineHelpers.cs index 598e6a995..f42121088 100644 --- a/Examples/Assembly/OutlineHelpers.cs +++ b/Examples/Assembly/OutlineHelpers.cs @@ -108,4 +108,15 @@ static int Raise(Gauge gauge, int value, int low) /// The level raised to zero, through a helper that throws when its local passes the limit. public int RaisedLevel() => Raise(this, Level, 0); + + static int Floor(int value) => value < 5 ? 5 : value; + + /// A word raised to a small constant, through a helper that tests it against the constant. + public static int Floored(int value) => Floor(value); + + /// The limit when the level is below a small constant, otherwise the level: a signed test. + public int BelowFive() => Level < 5 ? Limit : Level; + + /// The level unless it equals a small constant, then the limit: an equality test. + public int IsFive() => Level == 5 ? Limit : Level; } diff --git a/Examples/Assembly/Sequential.cs b/Examples/Assembly/Sequential.cs index 3134fc7ac..29b70b8ec 100644 --- a/Examples/Assembly/Sequential.cs +++ b/Examples/Assembly/Sequential.cs @@ -2,6 +2,7 @@ namespace Examples; +[StructLayout(LayoutKind.Sequential)] internal struct Record { public byte Tag; diff --git a/Examples/Assembly/SpanConstructors.cs b/Examples/Assembly/SpanConstructors.cs index 5889a9dba..24420b0b7 100644 --- a/Examples/Assembly/SpanConstructors.cs +++ b/Examples/Assembly/SpanConstructors.cs @@ -51,6 +51,12 @@ public static int AggregateLocal(in AggregateSpans.Words value) return span.Length; } + public static unsafe int BytePointerLocal(byte* value, int length) + { + var span = new ReadOnlySpan(value, length); + return span.Length; + } + public static unsafe int LayoutLocal(AggregateSpans.VectorRecord* value, int length) { var span = new Span(value, length); diff --git a/Examples/Assembly/TupleDivision.cs b/Examples/Assembly/TupleDivision.cs index 195f3ee51..fb48644e8 100644 --- a/Examples/Assembly/TupleDivision.cs +++ b/Examples/Assembly/TupleDivision.cs @@ -1,4 +1,4 @@ -// DivRem is marked experimental in .NET 10; the fixture pins its current signature. +// DivRem is marked experimental in .NET 11; the fixture pins its current signature. #pragma warning disable SYSLIB5004 using System; using System.Runtime.Intrinsics.X86; diff --git a/Examples/Assembly/VectorFields.cs b/Examples/Assembly/VectorFields.cs index 6ca4fb90c..20bd69070 100644 --- a/Examples/Assembly/VectorFields.cs +++ b/Examples/Assembly/VectorFields.cs @@ -12,6 +12,18 @@ public struct Holder128 { public byte Prefix; public Vector128 Value; } [StructLayout(LayoutKind.Sequential)] public struct Holder256 { public byte Prefix; public Vector256 Value; } + [StructLayout(LayoutKind.Sequential)] + public struct Bytes128 { public byte Prefix; public Vector128 Value; } + + [StructLayout(LayoutKind.Sequential)] + public struct Ints128 { public byte Prefix; public Vector128 Value; } + + [StructLayout(LayoutKind.Sequential)] + public struct UInts256 { public byte Prefix; public Vector256 Value; } + + [StructLayout(LayoutKind.Sequential)] + public struct Longs256 { public byte Prefix; public Vector256 Value; } + [StructLayout(LayoutKind.Sequential)] public struct Nested256 { public byte Prefix; public Holder256 Value; } @@ -24,6 +36,10 @@ public struct Single256 { public Vector256 Value; } public static Vector128 Read128(Holder128 value) => value.Value; public static Vector256 Read256(Holder256 value) => value.Value; + public static Vector128 ReadBytes128(Bytes128 value) => value.Value; + public static Vector128 ReadInts128(Ints128 value) => value.Value; + public static Vector256 ReadUInts256(UInts256 value) => value.Value; + public static Vector256 ReadLongs256(Longs256 value) => value.Value; public static Vector128 Write128(ref Holder128 holder, Vector128 value) { holder.Value = value; diff --git a/Examples/FSharp/Examples.FSharp.fsproj b/Examples/FSharp/Examples.FSharp.fsproj index 668084274..7b33e160d 100644 --- a/Examples/FSharp/Examples.FSharp.fsproj +++ b/Examples/FSharp/Examples.FSharp.fsproj @@ -1,6 +1,6 @@ - net10.0 + net11.0 Examples.FSharp 1.0.0 true diff --git a/Examples/Proofs/AnimalLegs.lean b/Examples/Proofs/AnimalLegs.lean index 4c33014fe..c11254d9d 100644 --- a/Examples/Proofs/AnimalLegs.lean +++ b/Examples/Proofs/AnimalLegs.lean @@ -11,8 +11,8 @@ override. The call's dispatch table lists both classes with their overrides, and each entry against the extraction's catalog. The receiver's runtime class selects the body: a `Dog` has four legs and a `Bird` two. A null receiver throws `NullReferenceException` at the `callvirt`. An instance of any class the table omits, such as a -class from an assembly outside the extraction, is the model fault `outsideDispatchTable`: no -return and no exception is proved for it, so every theorem here names the receiver's class. +class from an assembly outside the extraction, stops the interpreter at `outsideDispatchTable`. +The CLR behavior of that receiver remains unknown. -/ open CIL CIL.Safety @@ -53,7 +53,7 @@ theorem bird_legs (memory : CIL.Safety.Memory) (animal : Reference) /-- A null receiver throws `NullReferenceException` at the `callvirt`, before any body runs. -/ theorem null_throws (memory : CIL.Safety.Memory) : ∃ fuel, invoke Extracted.program fuel 0 [.object .null] memory = - .error ⟨0, 1, .thrown "System.NullReferenceException"⟩ := by + .error ⟨0, 1, .thrown .nullReference⟩ := by cil_checked_steps #print axioms null_throws @@ -89,8 +89,8 @@ theorem outside_faults (memory : CIL.Safety.Memory) (animal : Reference) #print axioms outside_faults -/-- So for such an instance no fuel proves a return or a thrown exception: every outcome is the - model fault or running out of fuel. -/ +/-- At every fuel budget, the interpreter can neither return a value nor throw a named + exception for a receiver whose class is absent from the dispatch table. -/ theorem outside_neither_returns_nor_throws (memory : CIL.Safety.Memory) (animal : Reference) (formed : formObject memory (.address animal) = .ok (.object (.address animal))) (typed : runtimeTypeOf memory animal = .ok plugin) (fuel : Nat) : @@ -98,19 +98,28 @@ theorem outside_neither_returns_nor_throws (memory : CIL.Safety.Memory) (animal .ok result) ∧ (∀ fault, invoke Extracted.program fuel 0 [.object (.address animal)] memory = .error fault → ∀ exception, fault.fault ≠ .thrown exception) := by - obtain ⟨observed, witness⟩ := outside_faults memory animal formed typed - have semantic : outsideDispatchTable ≠ .fuelExhausted := by simp [outsideDispatchTable] - refine ⟨invoke_fault_refutes_success _ observed 0 _ _ _ semantic witness fuel, ?_⟩ - intro fault failed exception thrown - have settled : Settled - (Except.error fault : Except LocatedFault (CIL.Safety.Memory × List CIL.Safety.Value)) := by - simp [Settled, thrown] - have up := invoke_mono _ fuel observed 0 _ _ _ settled failed - have down := invoke_mono _ observed fuel 0 _ _ _ (by exact semantic) witness - rw [Nat.add_comm, up] at down - cases down - simp [outsideDispatchTable] at thrown + obtain ⟨_, stopped⟩ := outside_faults memory animal formed typed + have stable : Stable (.error ⟨0, 1, outsideDispatchTable⟩ : + Except LocatedFault (CIL.Safety.Memory × List CIL.Safety.Value)) := by + simp [Stable, outsideDispatchTable] + constructor + · intro result returned + cases invoke_unique stable (by trivial) stopped returned + · intro fault failed exception thrown + have same := Except.error.inj (invoke_unique stable (by simp [Stable, thrown]) stopped failed) + subst fault + cases thrown #print axioms outside_neither_returns_nor_throws +/-- That fault is not a throw, so it is no named exception, and it is not definite: the model + does not cover the run, so `invoke_fault_refutes_success` and the contract refutations + cannot use it. -/ +theorem outside_not_definite : + (∀ exception, outsideDispatchTable ≠ .thrown exception) ∧ + outsideDispatchTable.definite = false := + ⟨fun _ => by simp [outsideDispatchTable], rfl⟩ + +#print axioms outside_not_definite + end ExampleProof diff --git a/Examples/Proofs/ArrayCovariance.lean b/Examples/Proofs/ArrayCovariance.lean index a9f195266..5d372acf9 100644 --- a/Examples/Proofs/ArrayCovariance.lean +++ b/Examples/Proofs/ArrayCovariance.lean @@ -78,7 +78,7 @@ theorem square_mismatch (memory : Memory) (shapes target : Reference) (length : (kind : allocation.kind = .object square) : ∃ fuel, invoke Extracted.program fuel put [.object (.address shapes), .scalar (.i32 index), .object (.address target)] memory = - .error ⟨put, 3, .thrown "System.ArrayTypeMismatchException"⟩ := by + .error ⟨put, 3, .thrown .arrayTypeMismatch⟩ := by have mismatch := ReferenceArray.storableIn_mismatch (element := circleType) found kind rfl rfl (by decide) have located := owned.slot_located owned.inside @@ -94,7 +94,7 @@ theorem index_out_of_range (memory : Memory) (shapes : Reference) (length : Nat) (formed : formObject memory shape = .ok (.object shape)) : ∃ fuel, invoke Extracted.program fuel put [.object (.address shapes), .scalar (.i32 index), .object shape] memory = - .error ⟨put, 3, .thrown "System.IndexOutOfRangeException"⟩ := by + .error ⟨put, 3, .thrown .indexOutOfRange⟩ := by have past := live.slot_outside outside cil_checked_steps [formed, past] @@ -104,7 +104,7 @@ theorem index_out_of_range (memory : Memory) (shapes : Reference) (length : Nat) theorem null_array (memory : Memory) (index : BitVec 32) (shape : ManagedReference) (formed : formObject memory shape = .ok (.object shape)) : ∃ fuel, invoke Extracted.program fuel put [.object .null, .scalar (.i32 index), .object shape] - memory = .error ⟨put, 3, .thrown "System.NullReferenceException"⟩ := by + memory = .error ⟨put, 3, .thrown .nullReference⟩ := by cil_checked_steps [formed] #print axioms null_array diff --git a/Examples/Proofs/ArrayElement.lean b/Examples/Proofs/ArrayElement.lean index 858e29366..0a0ed45fc 100644 --- a/Examples/Proofs/ArrayElement.lean +++ b/Examples/Proofs/ArrayElement.lean @@ -55,7 +55,7 @@ theorem index_out_of_range (memory : Memory) (nodes : Reference) (length : Nat) (outside : length ≤ index.toNat) : ∃ fuel, invoke Extracted.program fuel elementAt [.object (.address nodes), .scalar (.i32 index)] memory = - .error ⟨elementAt, 2, .thrown "System.IndexOutOfRangeException"⟩ := by + .error ⟨elementAt, 2, .thrown .indexOutOfRange⟩ := by have past := live.slot_outside outside cil_checked_steps [past] @@ -67,7 +67,7 @@ theorem negative_out_of_range (memory : Memory) (nodes : Reference) (length : Na (small : length ≤ 2 ^ 31) (negative : index.toInt < 0) : ∃ fuel, invoke Extracted.program fuel elementAt [.object (.address nodes), .scalar (.i32 index)] memory = - .error ⟨elementAt, 2, .thrown "System.IndexOutOfRangeException"⟩ := + .error ⟨elementAt, 2, .thrown .indexOutOfRange⟩ := index_out_of_range memory nodes length index live (by rw [BitVec.toInt_eq_toNat_cond] at negative split at negative <;> omega) @@ -77,7 +77,7 @@ theorem negative_out_of_range (memory : Memory) (nodes : Reference) (length : Na /-- A null array throws `NullReferenceException` at pc 2. -/ theorem null_array (memory : Memory) (index : BitVec 32) : ∃ fuel, invoke Extracted.program fuel elementAt [.object .null, .scalar (.i32 index)] - memory = .error ⟨elementAt, 2, .thrown "System.NullReferenceException"⟩ := by + memory = .error ⟨elementAt, 2, .thrown .nullReference⟩ := by cil_checked_steps #print axioms null_array diff --git a/Examples/Proofs/ArrayLength.lean b/Examples/Proofs/ArrayLength.lean index 838499b12..1f07a1fce 100644 --- a/Examples/Proofs/ArrayLength.lean +++ b/Examples/Proofs/ArrayLength.lean @@ -31,7 +31,7 @@ theorem correctness (memory : Memory) (nodes : Reference) (length : Nat) /-- A null array throws `NullReferenceException` at `ldlen`, pc 1. -/ theorem null_array (memory : Memory) : ∃ fuel, invoke Extracted.program fuel count [.object .null] memory = - .error ⟨count, 1, .thrown "System.NullReferenceException"⟩ := by + .error ⟨count, 1, .thrown .nullReference⟩ := by cil_checked_steps #print axioms null_array diff --git a/Examples/Proofs/BalanceField.lean b/Examples/Proofs/BalanceField.lean index aedeb14eb..5207afb2b 100644 --- a/Examples/Proofs/BalanceField.lean +++ b/Examples/Proofs/BalanceField.lean @@ -25,7 +25,7 @@ theorem correctness (memory : CIL.Safety.Memory) (account : CIL.Safety.ManagedRe runtime does. -/ theorem null_throws (memory : CIL.Safety.Memory) : ∃ fuel, CIL.Safety.invoke Extracted.program fuel 0 [.object .null] memory = - .error ⟨0, 1, .thrown "System.NullReferenceException"⟩ := by + .error ⟨0, 1, .thrown .nullReference⟩ := by cil_checked_steps [CIL.Safety.objectFieldAddress] #print axioms null_throws diff --git a/Examples/Proofs/BaseField.lean b/Examples/Proofs/BaseField.lean index bc2402ddd..aaac3b0f8 100644 --- a/Examples/Proofs/BaseField.lean +++ b/Examples/Proofs/BaseField.lean @@ -56,7 +56,7 @@ theorem unrelated_rejected (memory : CIL.Safety.Memory) (type : RuntimeType) /-- Reading a field of the null reference throws `NullReferenceException` at the `ldfld`. -/ theorem null_throws (memory : CIL.Safety.Memory) : ∃ fuel, invoke Extracted.program fuel 0 [.object .null] memory = - .error ⟨0, 1, .thrown "System.NullReferenceException"⟩ := by + .error ⟨0, 1, .thrown .nullReference⟩ := by cil_checked_steps [objectFieldAddress] #print axioms null_throws diff --git a/Examples/Proofs/BigMul.lean b/Examples/Proofs/BigMul.lean index 87ec303b7..4a760087c 100644 --- a/Examples/Proofs/BigMul.lean +++ b/Examples/Proofs/BigMul.lean @@ -2,6 +2,8 @@ import Extracted import CIL.Safety.SymbolicExecution import CIL.Safety.WideMultiply +/-! The checked unsigned product writes its low word and returns its high word. -/ + namespace ExampleProof open CIL.Safety @@ -13,7 +15,8 @@ theorem correctness (memory result : Memory) (reference : Reference) (left right invoke Extracted.program 8 0 [.scalar (.i64 left), .scalar (.i64 right), .reference (.address reference)] memory = .ok (result, [.scalar (.i64 (CIL.WideMultiply.high64 left right))]) := by - cil_checked_steps [formed, step_bigMul64_of_write stored] + cil_checked_steps [formed, + step_bigMul64_of_write Extracted.method0_link3 Extracted.method0_model3 stored] #print axioms correctness end ExampleProof diff --git a/Examples/Proofs/BlockOverrun.lean b/Examples/Proofs/BlockOverrun.lean index f0b6bccb8..8f8e25dec 100644 --- a/Examples/Proofs/BlockOverrun.lean +++ b/Examples/Proofs/BlockOverrun.lean @@ -1,5 +1,8 @@ import Extracted import CIL.Safety.FuelLemmas +import CIL.Safety.StepComposition + +/-! The compiled unaligned fill faults on a nine-byte write to an eight-byte allocation. -/ open CIL.Safety namespace ExampleProof @@ -17,8 +20,13 @@ def callerReference : CIL.Safety.Value := .reference (.address ⟨0, 0⟩) def arguments : List Value := [callerReference, .scalar (.i32 0), .scalar (.i32 9)] def fault : LocatedFault := ⟨0, 3, .memory .outsideAllocation (.address ⟨0, 0⟩) 9⟩ +/-- The block call faults at pc 3 after loading its destination, byte and count. -/ theorem witness : invoke Extracted.program 16 0 arguments callerMemory = .error fault := by - rfl + change run Extracted.program 13 0 3 arguments ⟨1, [], [], 0⟩ + [.scalar (.i32 9), .scalar (.i32 0), callerReference] callerMemory = .error fault + apply run_fault Extracted.program_method0 Extracted.method0_instruction3 + rw [step_call_model (link := Extracted.method0_link3) (found := Extracted.method0_model3) + (direct := rfl)] <;> rfl /-- No fuel choice can make a nine-byte fill succeed in the eight-byte allocation. -/ theorem correctness (fuel : Nat) (result : Memory × List Value) : diff --git a/Examples/Proofs/BucketItems.lean b/Examples/Proofs/BucketItems.lean index 8295de9be..56bdd0bfc 100644 --- a/Examples/Proofs/BucketItems.lean +++ b/Examples/Proofs/BucketItems.lean @@ -45,7 +45,7 @@ theorem null_items (memory : Memory) (bucket : Reference) (readable : Bucket.Items.Readable memory bucket) (array : Bucket.Items.of memory bucket = .null) : ∃ fuel, invoke Extracted.program fuel first [.object (.address bucket)] memory = - .error ⟨first, 3, .thrown "System.NullReferenceException"⟩ := by + .error ⟨first, 3, .thrown .nullReference⟩ := by cil_checked_steps [readable.load_null array] #print axioms null_items @@ -56,7 +56,7 @@ theorem empty_items (memory : Memory) (bucket items : Reference) (array : Bucket.Items.of memory bucket = .address items) (live : ReferenceArray.Live memory items nodeType 0) : ∃ fuel, invoke Extracted.program fuel first [.object (.address bucket)] memory = - .error ⟨first, 3, .thrown "System.IndexOutOfRangeException"⟩ := by + .error ⟨first, 3, .thrown .indexOutOfRange⟩ := by cil_checked_steps [readable.facts array live.object, live.slot_outside (index := 0) (Nat.le_refl _)] diff --git a/Examples/Proofs/BucketRefill.lean b/Examples/Proofs/BucketRefill.lean index 58f4cc121..f0d50a950 100644 --- a/Examples/Proofs/BucketRefill.lean +++ b/Examples/Proofs/BucketRefill.lean @@ -44,7 +44,7 @@ theorem correctness (memory : Memory) (bucket items : Reference) (length : Nat) theorem null_bucket (memory : Memory) (items : Reference) (length : Nat) (live : ReferenceArray.Live memory items nodeType length) : ∃ fuel, invoke Extracted.program fuel refill [.object .null, .object (.address items)] - memory = .error ⟨refill, 2, .thrown "System.NullReferenceException"⟩ := by + memory = .error ⟨refill, 2, .thrown .nullReference⟩ := by cil_checked_steps #print axioms null_bucket diff --git a/Examples/Proofs/BumpBoth.lean b/Examples/Proofs/BumpBoth.lean index 1d018faf8..65fb8d65d 100644 --- a/Examples/Proofs/BumpBoth.lean +++ b/Examples/Proofs/BumpBoth.lean @@ -1,7 +1,7 @@ import Extracted import CIL.MethodIdentity +import CIL.Safety.DecodedSummary import CIL.Safety.ObjectFields -import CIL.Safety.OutcomeContract /-! # Two calls on class instances @@ -95,7 +95,7 @@ theorem correctness : bumpBothContract.Holds Extracted.program bumpBoth := /-- `Bump` on a null account throws `NullReferenceException` at its field load. -/ theorem bump_null_throws (memory : Memory) (amount : W64) : ∃ fuel, invoke Extracted.program fuel bump [.object .null, .scalar (.i64 amount)] memory = - .error ⟨bump, 2, .thrown "System.NullReferenceException"⟩ := by + .error ⟨bump, 2, .thrown .nullReference⟩ := by cil_checked_steps #print axioms bump_null_throws @@ -115,7 +115,7 @@ def bumpOutcome : OutcomeContract (ManagedReference × W64) where | .address account => Account.Balance.Holds final account (Account.Balance.of initial account + amount) raises := fun (account, _) _ => match account with - | .null => some "System.NullReferenceException" + | .null => some .nullReference | .address _ => none /-- `Bump` meets its outcome contract: the runtime exception for null, the update otherwise. -/ @@ -128,4 +128,14 @@ theorem bump_outcome : bumpOutcome.Holds Extracted.program bump := by | address account => exact WP.adequate.mp (bump_holds (account, amount) memory permitted) #print axioms bump_outcome + +/-- The balance update and null exception belong to the same pinned `Bump` body. -/ +theorem bump_summary : ∃ record, Extracted.methodEvidence[bump]? = some record ∧ + DecodedOutcomeSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment bump record.assembly record.original.token record.arguments + bumpOutcome := + ⟨_, rfl, .of_translation Extracted.translation_valid ⟨bump, by decide⟩ + (method := Extracted.method1) rfl bump_outcome⟩ + +#print axioms bump_summary end ExampleProof diff --git a/Examples/Proofs/CallsSummary.lean b/Examples/Proofs/CallsSummary.lean new file mode 100644 index 000000000..0cd6793a2 --- /dev/null +++ b/Examples/Proofs/CallsSummary.lean @@ -0,0 +1,145 @@ +import Extracted +import CIL +import CIL.MethodIdentity +import CIL.Safety.LoopScaffold + +/-! +# A call chain through decoded summaries + +`Examples.Arithmetic::Calls` stores `Increment(x)` in a local, passes the local by reference to +`Mutate`, which adds two through it, and returns the local. `correctness_stepped` steps the +caller and both callees instruction by instruction. `correctness` proves the same statement from +a `DecodedSummary` of each callee, bound to the record whose pinned bytes decode to it, without +stepping either body. +-/ + +namespace ExampleProof +open CIL CIL.Safety CIL.Extraction + +bind_entry entry (memory : Safety.Memory) (x : W64) (wellFormed : memory.WellFormed) := + Extracted.method0 with [.scalar (.i64 x)] from memory allocating wellFormed +bind_method increment incrementBody := + "System.UInt64 Examples.Arithmetic::Increment(System.UInt64)" +bind_method mutate mutateBody := "System.Void Examples.Arithmetic::Mutate(System.UInt64&)" + +/-- The caller returns `x + 3` and keeps all memory below its entry, stepping both callees. -/ +theorem correctness_stepped (memory : Safety.Memory) (x : W64) (wellFormed : memory.WellFormed) : + WP (Execution (fun fuel => invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory)) + (fun (final, values) => values = [.scalar (.i64 (x + 3))] ∧ + MemoryBelow memory.nextIdentity memory final) := by + obtain ⟨reference, entered, enter⟩ := entry.checked memory x wellFormed + have ready := enter.numeric0.writable + obtain ⟨first, firstWrite, firstStore, firstRead, -⟩ := store_local_word64 (x + 1) ready + have readyAfter := snapshot_access_after_write ready firstWrite + obtain ⟨second, secondWrite, secondStore, secondRead⟩ := store_word64 (x + 3) readyAfter + rw [← show x + 1 + 2#64 = x + 3 by bv_omega] at secondStore + have below := MemoryBelow.frame_round_trip enter.setup + ((write_preserves_memory_below _ _ _ _ _ _ enter.numeric0.fresh firstWrite).trans + (write_preserves_memory_below _ _ _ _ _ _ enter.numeric0.fresh secondWrite)) + suffices ran : ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory = + .ok (leaveFrame (entry.frame memory reference) second, [.scalar (.i64 (x + 3))]) by + obtain ⟨fuel, ran⟩ := ran + exact WP.adequate.mpr ⟨fuel, _, _, ran, rfl, below⟩ + cil_checked_steps [enter.setup, entry.frame, firstStore, firstRead, secondStore, + load_local_word64_of_read secondRead, localAddress, access_reference_valid _ _ _ _ _ ready, + access_reference_valid _ _ _ _ _ readyAfter, + checkedValue_address _ _ (access_reference_valid _ _ _ _ _ readyAfter), numericOfWidth] +#print axioms correctness_stepped + +/-- `Increment` returns its argument plus one and changes no memory. -/ +def incrementContract : MethodContract W64 W64 where + args x := [.scalar (.i64 x)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post x initial final value := value = x + 1 ∧ final = initial + +/-- `Mutate` adds two to the word its argument names and writes nothing else. -/ +def mutateContract : MethodContract (Reference × W64) Unit where + args := fun (place, _) => [.reference (.address place)] + results _ := [] + pre := fun (place, old) memory => access memory place 8 1 true = .ok () ∧ + read memory place 8 1 = .ok (numberBytes old.toNat 8) + footprint := fun (place, _) => [(place, 8)] + post := fun (place, old) _ final _ => + read final place 8 1 = .ok (numberBytes (old + 2).toNat 8) + +/-- `Increment` meets its contract. -/ +theorem increment_holds : incrementContract.Holds Extracted.program increment := by + rintro x memory - + refine MethodContract.Holds.intro memory (x + 1) ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + cil_checked_steps +#print axioms increment_holds + +/-- `Mutate` meets its contract. -/ +theorem mutate_holds : mutateContract.Holds Extracted.program mutate := by + rintro ⟨place, old⟩ memory ⟨ready, loaded⟩ + obtain ⟨final, written, stored, readback⟩ := store_word64 (old + 2) ready + refine MethodContract.Holds.intro final () ?_ + (Confined.write written (by simp [mutateContract])) readback + cil_checked_steps [loaded, stored, access_reference_valid _ _ _ _ _ ready, + checkedValue_address _ _ (access_reference_valid _ _ _ _ _ ready)] +#print axioms mutate_holds + +/-- Every record's pinned bytes decode to the method at its index of the extraction. -/ +theorem bodies_decode : + decodedProgram? Extracted.profile Extracted.environment Extracted.methodEvidence = + some Extracted.program := + decodedProgram?_eq_some Extracted.translation_valid.programBindingsValid + (.cons ((Method.ofBody?_of_decodes Extracted.method0_body_validated).trans + Extracted.method0_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method1_body_validated).trans + Extracted.method1_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method2_body_validated).trans + Extracted.method2_decoded) .nil))) +#print axioms bodies_decode + +/-- The contract of `Increment`, bound to its record and the bytes it decodes from. -/ +theorem increment_summary : ∃ record, Extracted.methodEvidence[increment]? = some record ∧ + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment increment record.assembly record.original.token record.arguments + incrementContract := + ⟨_, rfl, .of_translation Extracted.translation_valid ⟨increment, by decide⟩ + (method := Extracted.method2) rfl increment_holds⟩ +#print axioms increment_summary + +/-- The contract of `Mutate`, bound to its record and the bytes it decodes from. -/ +theorem mutate_summary : ∃ record, Extracted.methodEvidence[mutate]? = some record ∧ + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment mutate record.assembly record.original.token record.arguments + mutateContract := + ⟨_, rfl, .of_translation Extracted.translation_valid ⟨mutate, by decide⟩ + (method := Extracted.method1) rfl mutate_holds⟩ +#print axioms mutate_summary + +/-- The caller returns `x + 3` and keeps all memory below its entry, applying each callee's + summary at its call. -/ +theorem correctness (memory : Safety.Memory) (x : W64) (wellFormed : memory.WellFormed) : + WP (Execution (fun fuel => invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory)) + (fun (final, values) => values = [.scalar (.i64 (x + 3))] ∧ + MemoryBelow memory.nextIdentity memory final) := by + obtain ⟨incrementRecord, -, incrementSummary⟩ := increment_summary + obtain ⟨mutateRecord, -, mutateSummary⟩ := mutate_summary + obtain ⟨reference, entered, enter⟩ := entry.checked memory x wellFormed + apply WP.enter (by rfl) (by rfl) enter.setup + cil_contract_steps [incrementSummary.holds, incrementContract, entry.frame] + with middle value incremented + obtain ⟨rfl, rfl⟩ := incremented.post + obtain ⟨stored, store, frame, -⟩ := (enter.frame []).store 0 (.i64 (x + 1)) rfl + have permitted : mutateContract.pre (reference, x + 1) stored := + ⟨frame.homes.writable_at (kind := .word64) (home := reference) (index := 0) rfl, + frame.snapshots.read_word64 (index := 0) rfl rfl⟩ + cil_contract_steps [store, localAddress, frame.form_home 0] + apply WP.exists_execution + apply WP.local_call frame mutateSummary.holds (reference, x + 1) (by rfl) (by rfl) + (by cil_checked_step [localAddress, frame.form_home 0]) permitted 0 .word64 reference rfl rfl + (fun _ => .i64 (x + 3)) (fun _ => (x + 3).toNat) (fun _ => rfl) + (fun _ _ post => by rw [← show x + 1 + 2 = x + 3 by bv_omega]; exact post) + intro final values mutated updated + have loaded := updated.snapshots.load (index := 0) (.i64 (x + 3)) rfl + (body := Extracted.method0) (pc := 5) (args := [.scalar (.i64 x)]) (stack := []) + cil_contract_steps [loaded] + exact ⟨rfl, updated.leave_kept enter.setup _ rfl⟩ +#print axioms correctness + +end ExampleProof diff --git a/Examples/Proofs/CheckedAdd32.lean b/Examples/Proofs/CheckedAdd32.lean index 71bd4ba95..a3f1d3310 100644 --- a/Examples/Proofs/CheckedAdd32.lean +++ b/Examples/Proofs/CheckedAdd32.lean @@ -4,16 +4,22 @@ import CIL.Safety.SymbolicExecution open CIL CIL.Safety namespace ExampleProof +/-- A fitting checked sum preserves arbitrary caller memory and returns the natural-number sum. -/ +theorem correctness_framed (memory : CIL.Safety.Memory) (x y : W32) + (fits : x.toNat + y.toNat < 2^32) : + invoke Extracted.program 4 0 [.scalar (.i32 x), .scalar (.i32 y)] memory = + .ok (memory, [.scalar (.i32 (BitVec.ofNat 32 (x.toNat + y.toNat)))]) := by + cil_checked_steps [checkedValues_add_unsigned_i32 fits] + rfl + +#print axioms correctness_framed + /-- The checked sum equals natural-number addition whenever it fits. -/ theorem correctness (x y : W32) (fits : x.toNat + y.toNat < 2^32) : (CIL.Safety.invoke Extracted.program 4 0 [.scalar (.i32 x), .scalar (.i32 y)] Memory.empty).map Prod.snd = .ok [.scalar (.i32 (BitVec.ofNat 32 (x.toNat + y.toNat)))] := by - have ran : CIL.Safety.invoke Extracted.program 4 0 [.scalar (.i32 x), .scalar (.i32 y)] - Memory.empty = .ok (Memory.empty, [.scalar (.i32 (x + y))]) := by - cil_checked_steps [checkedValues_add_unsigned_i32 fits] - rw [ran] - rfl + exact congrArg (fun result => result.map Prod.snd) (correctness_framed Memory.empty x y fits) #print axioms correctness -end ExampleProof \ No newline at end of file +end ExampleProof diff --git a/Examples/Proofs/CopyUntilZero.lean b/Examples/Proofs/CopyUntilZero.lean index 3cf2b7664..fe3a6bc84 100644 --- a/Examples/Proofs/CopyUntilZero.lean +++ b/Examples/Proofs/CopyUntilZero.lean @@ -34,7 +34,7 @@ theorem stamp_holds : stampContract.Holds Extracted.program stamp := by rintro ⟨slot, value⟩ memory writable obtain ⟨after, wrote, formed, confined, filled⟩ := writable.write_one value refine MethodContract.Holds.intro after () ?_ confined filled - cil_checked_steps [step_storeScalar_of_write wrote, formed] + cil_checked_steps [step_stind_narrow_of_write .i4 wrote, formed] /-- The words copied: those before the first zero, or all of them. -/ def copied (values : List W32) : List W32 := values.takeWhile (· ≠ 0#32) @@ -70,7 +70,7 @@ private abbrev State (memory : Memory) (output : Reference) (room : Nat) -- The loop guard observes the remaining span length. bind_cutpoint header (memory : Memory) (output : Reference) (room : Nat) (home0 home1 : Reference) (state : State memory output room home0 home1) := - copyBody at loop.latch matching (· matches .brnonzero _) + copyBody at loop.latch matching (· matches .brtrue _) with entry.frame memory state.values.1.view home0 home1, [.scalar (.i32 (BitVec.ofNat 32 state.values.1.view.length))], state.memory diff --git a/Examples/Proofs/CountCalls.lean b/Examples/Proofs/CountCalls.lean index e64a5b1b0..a763bc7f8 100644 --- a/Examples/Proofs/CountCalls.lean +++ b/Examples/Proofs/CountCalls.lean @@ -37,7 +37,7 @@ private abbrev State (remaining counted : Reference) := /-- The loop test, with the remaining count on the stack. -/ bind_cutpoint header (remaining counted : Reference) (state : State remaining counted) := - countBody at loop.latch matching (· matches .brnonzero _) + countBody at loop.latch matching (· matches .brtrue _) with entry.frame remaining counted, [.scalar (.i32 state.values.1)], state.memory /-- `count + remaining` is the input. -/ diff --git a/Examples/Proofs/CountCallsSummary.lean b/Examples/Proofs/CountCallsSummary.lean new file mode 100644 index 000000000..c83fa5d2c --- /dev/null +++ b/Examples/Proofs/CountCallsSummary.lean @@ -0,0 +1,213 @@ +import Extracted +import CIL +import CIL.MethodIdentity +import CIL.StructuralLabels +import CIL.Safety.LoopScaffold + +/-! +# A counted loop through a decoded summary + +`Examples.Loops::CountCalls` counts down its argument, calling `Decrement` once per iteration, +and returns how many calls it made. Both methods decode from their pinned bytes. +`correctness_stepped` steps `Decrement`'s body at the call of every iteration; +`correctness` proves the same statement from a `DecodedSummary` of `Decrement`, applied at +the call with `cil_loop_call`, without stepping its body in the loop. +-/ + +namespace ExampleProof +open CIL CIL.Safety CIL.Extraction + +bind_method countCalls countBody := "System.UInt32 Examples.Loops::CountCalls(System.UInt32)" +bind_callee decrement decrementBody := + "System.UInt32 Examples.Loops::Decrement(System.UInt32)" from countBody +bind_call decrementSite := decrement from countBody + +bind_entry entry (x : W32) := countBody with [.scalar (.i32 x)] from Memory.empty + allocating Memory.empty_wellFormed +bind_loop loop := countBody + +/-- The remaining count and the calls made so far, held by the frame's two locals. -/ +private abbrev State (remaining counted : Reference) := + FrameState [] Memory.empty [.word32, .word32] (W32 × W32) + (fun _ => [.bytes .word32 remaining, .bytes .word32 counted]) + fun values => [some (.i32 values.1), some (.i32 values.2)] + +/-- The loop test, with the remaining count on the stack. -/ +bind_cutpoint header (remaining counted : Reference) (state : State remaining counted) := + countBody at loop.latch matching (· matches .brtrue _) + with entry.frame remaining counted, [.scalar (.i32 state.values.1)], state.memory + +/-- `count + remaining` is the input. -/ +private def invariant {remaining counted : Reference} (value : W32) + (state : State remaining counted) : Prop := + state.values.2 + state.values.1 = value + +/-- The remaining count falls by one in every iteration that does not exit. -/ +private theorem measure_falls {x : W32} (zero : ¬x = 0) : (x - 1).toNat < x.toNat := by + have positive : 0 < x.toNat := by + have : x.toNat ≠ 0 := fun h => zero (BitVec.eq_of_toNat_eq h) + omega + rw [BitVec.toNat_sub_of_le] + · simp; omega + · change 1 ≤ x.toNat + omega +#print axioms measure_falls + +/-- From every loop test, the loop returns its input, stepping `Decrement` at each call. -/ +theorem terminates_stepped (args : List CIL.Safety.Value) (value : W32) + {remaining counted : Reference} (state : State remaining counted) + (valid : invariant value state) : + LoopTerminates Extracted.program countCalls args + (fun _ values => values = [.scalar (.i32 value)]) (header remaining counted state) := by + apply loop_terminates_induction Extracted.program countCalls args (header remaining counted) + (invariant value) (fun s => s.values.1.toNat) _ ?_ state valid + rintro ⟨⟨x, count⟩, m, frame⟩ total recurse + dsimp only [invariant] at total recurse + by_cases zero : x = 0 + · subst x + have same : count = value := by simpa using total + subst same + cil_loop_finish + · cil_loop_advance + unfold LoopTerminates LoopState.run + -- Decrement's own body runs to its result, leaving the caller's memory as it was. + refine WP.call (helperPost := fun result => result = (m, [.scalar (.i32 (x - 1))])) + countCalls.found decrementSite.call (by cil_checked_step []) ?_ ?_ + · refine WP.exact_iff.mpr ?_ + dsimp only [Execution] + cil_checked_steps [] + intro final values called + obtain ⟨rfl, rfl⟩ := Prod.mk.inj called + change LoopTerminates Extracted.program countCalls args _ ⟨_, _, _, _⟩ + dsimp only [List.append_nil] + cil_loop_recurse recurse (x - 1, count + 1) + case invariant => + show count + 1 + (x - 1) = value + rw [← total] + bv_omega + case measure => exact measure_falls zero +#print axioms terminates_stepped + +/-- `CountCalls` returns its input for every `uint`, stepping `Decrement` at each call. -/ +theorem correctness_stepped : CIL.Safety.Ensures Extracted.program countCalls + (fun args initial => ∃ x : W32, args = [.scalar (.i32 x)] ∧ initial = Memory.empty) + (fun args _ _ values => values = args) := by + apply ensures_of_loop + intro args initial permitted + obtain ⟨x, rfl, rfl⟩ := permitted + obtain ⟨remaining, counted, entered, enter⟩ := entry.checked x + have started : + LoopTerminates Extracted.program countCalls [.scalar (.i32 x)] + (fun _ values => values = [.scalar (.i32 x)]) + ⟨0, entry.frame remaining counted, [], entered⟩ := by + cil_loop_recurse (terminates_stepped [.scalar (.i32 x)] x) (x, 0) + case invariant => simp [invariant] + exact ⟨countBody, entry.frame remaining counted, entered, countCalls.found, rfl, enter.setup, + started⟩ +#print axioms correctness_stepped + +/-- `Decrement` returns its argument less one and changes no memory. -/ +def decrementContract : MethodContract W32 W32 where + args x := [.scalar (.i32 x)] + results v := [.scalar (.i32 v)] + pre _ _ := True + footprint _ := [] + post x _ _ v := v = x - 1 + +/-- `Decrement` meets its contract. -/ +theorem decrement_holds : decrementContract.Holds Extracted.program decrement := by + rintro x m - + refine MethodContract.Holds.intro m _ ?_ (Confined.refl _ m) rfl + cil_checked_steps +#print axioms decrement_holds + +/-- Every record's pinned bytes decode to the method at its index of the extraction. -/ +theorem bodies_decode : + decodedProgram? Extracted.profile Extracted.environment Extracted.methodEvidence = + some Extracted.program := + decodedProgram?_eq_some Extracted.translation_valid.programBindingsValid + (.cons ((Method.ofBody?_of_decodes Extracted.method0_body_validated).trans + Extracted.method0_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method1_body_validated).trans + Extracted.method1_decoded) .nil)) +#print axioms bodies_decode + +/-- The contract of `Decrement`, bound to its record and the bytes it decodes from. -/ +theorem decrement_summary : ∃ record, Extracted.methodEvidence[decrement]? = some record ∧ + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment decrement record.assembly record.original.token record.arguments + decrementContract := + ⟨_, rfl, .of_decoded bodies_decode rfl ⟨_, Extracted.method1_body_validated⟩ + Extracted.method1_specialization_validated.1 decrement_holds⟩ +#print axioms decrement_summary + +/-- Every summary of the index the loop's call enters is for the method that call names: + `Decrement`'s metadata token, with no type arguments. -/ +theorem decrement_named {pin : AssemblyPin} {token : Nat} {arguments : List TypeExpr} + (summary : DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment decrement pin token arguments decrementContract) : + token = Extracted.method1Original.token ∧ arguments = [] := + summary.site Extracted.method0_calls_validated decrementSite.call rfl rfl rfl rfl + decrementSite.link +#print axioms decrement_named + +/-- A summary of the loop's callee index bound to `CountCalls`' own token is refuted at the + call. -/ +theorem caller_token_refused {pin : AssemblyPin} + (summary : DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment decrement pin Extracted.method0Original.token [] decrementContract) : + False := + absurd (decrement_named summary).1 (by decide) +#print axioms caller_token_refused + +/-- From every loop test, the loop returns its input, applying `summary` at each call. -/ +theorem terminates {pin : AssemblyPin} {token : Nat} {arguments : List TypeExpr} + (summary : DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment decrement pin token arguments decrementContract) + (args : List CIL.Safety.Value) (value : W32) {remaining counted : Reference} + (state : State remaining counted) (valid : invariant value state) : + LoopTerminates Extracted.program countCalls args + (fun _ values => values = [.scalar (.i32 value)]) (header remaining counted state) := by + apply loop_terminates_induction Extracted.program countCalls args (header remaining counted) + (invariant value) (fun s => s.values.1.toNat) _ ?_ state valid + rintro ⟨⟨x, count⟩, m, frame⟩ total recurse + dsimp only [invariant] at total recurse + by_cases zero : x = 0 + · subst x + have same : count = value := by simpa using total + subst same + cil_loop_finish + · cil_loop_advance + -- The callee may change only its empty footprint, so both locals survive the call. + cil_loop_call summary.holds x at decrementSite + -- The frame of the memory before the call is stale once `calledFrame` holds. + clear frame + cil_loop_recurse recurse (x - 1, count + 1) + case invariant => + show count + 1 + (x - 1) = value + rw [← total] + bv_omega + case measure => exact measure_falls zero +#print axioms terminates + +/-- `CountCalls` returns its input for every `uint`, given only `Decrement`'s decoded + summary. -/ +theorem correctness : CIL.Safety.Ensures Extracted.program countCalls + (fun args initial => ∃ x : W32, args = [.scalar (.i32 x)] ∧ initial = Memory.empty) + (fun args _ _ values => values = args) := by + obtain ⟨record, -, summary⟩ := decrement_summary + apply ensures_of_loop + intro args initial permitted + obtain ⟨x, rfl, rfl⟩ := permitted + obtain ⟨remaining, counted, entered, enter⟩ := entry.checked x + have started : + LoopTerminates Extracted.program countCalls [.scalar (.i32 x)] + (fun _ values => values = [.scalar (.i32 x)]) + ⟨0, entry.frame remaining counted, [], entered⟩ := by + cil_loop_recurse (terminates summary [.scalar (.i32 x)] x) (x, 0) + case invariant => simp [invariant] + exact ⟨countBody, entry.frame remaining counted, entered, countCalls.found, rfl, enter.setup, + started⟩ +#print axioms correctness + +end ExampleProof diff --git a/Examples/Proofs/CountUntil.lean b/Examples/Proofs/CountUntil.lean index 680429bdc..96d536fe1 100644 --- a/Examples/Proofs/CountUntil.lean +++ b/Examples/Proofs/CountUntil.lean @@ -7,7 +7,7 @@ import CIL.Safety.LoopOutcome import CIL.Safety.LoopScaffold open CIL.Safety -open CIL (W32) +open CIL (W32 ExceptionType) namespace ExampleProof bind_method countUntil untilBody := @@ -22,7 +22,7 @@ def bumpContract : OutcomeContract (W32 × W32) W32 where pre _ _ := True footprint _ := [] post input _ _ v := v = input.1 + 1 - raises input _ := if input.1 = input.2 then some "System.InvalidOperationException" else none + raises input _ := if input.1 = input.2 then some .invalidOperation else none theorem bump_holds : bumpContract.Holds Extracted.program bump := by rintro ⟨count, limit⟩ m - @@ -35,14 +35,14 @@ theorem bump_holds : bumpContract.Holds Extracted.program bump := by exact ⟨Confined.refl _ m, _, rfl, rfl⟩ /-- `CountUntil` raises exactly when the input exceeds the limit, after `limit` successful calls. -/ -def untilRaises (value limit : W32) : Option String := - if limit.toNat < value.toNat then some "System.InvalidOperationException" else none +def untilRaises (value limit : W32) : Option ExceptionType := + if limit.toNat < value.toNat then some .invalidOperation else none bind_entry entry (value limit : W32) := untilBody with [.scalar (.i32 value), .scalar (.i32 limit)] from Memory.empty allocating Memory.empty_wellFormed private def guard : Nat := instructionLocation untilBody fun op => - match op with | .brnonzero _ => true | _ => false + match op with | .brtrue _ => true | _ => false /-- The remaining count and the successful calls so far, held by the frame's two locals. -/ private abbrev State (remaining counted : Reference) := FrameState [] Memory.empty [.word32, .word32] (W32 × W32) @@ -125,7 +125,7 @@ theorem outcome (holds : bumpContract.Holds Extracted.program bump) (value limit /-- The outcome `CountUntil` reaches from its arguments: `untilRaises` of them, or a normal return. -/ -def untilOutcome : List Value → Memory → Option String +def untilOutcome : List Value → Memory → Option ExceptionType | [.scalar (.i32 value), .scalar (.i32 limit)], _ => untilRaises value limit | _, _ => none diff --git a/Examples/Proofs/Countdown.lean b/Examples/Proofs/Countdown.lean index 04943210e..7dd888a09 100644 --- a/Examples/Proofs/Countdown.lean +++ b/Examples/Proofs/Countdown.lean @@ -17,7 +17,7 @@ private abbrev State (home : Reference) := /-- The loop test, with the remaining count on the stack. It is found as the only conditional branch of the compiled code, so no instruction position or block order is assumed. -/ bind_cutpoint header (home : Reference) (state : State home) := - Extracted.method0 matching (fun op => match op with | .brnonzero _ => true | _ => false) + Extracted.method0 matching (fun op => match op with | .brtrue _ => true | _ => false) with entry.frame home, [.scalar (.i32 state.values)], state.memory theorem correctness : CIL.Safety.Ensures Extracted.program 0 @@ -25,7 +25,8 @@ theorem correctness : CIL.Safety.Ensures Extracted.program 0 (fun _ _ _ values => values = [.scalar (.i32 0)]) := by rintro args initial ⟨x, rfl, rfl⟩ obtain ⟨home, entered, enter⟩ := entry.checked x - refine invoke_loop_satisfies Extracted.lookup0 rfl enter.setup (header home) (fun _ => True) + refine invoke_loop_satisfies Extracted.program_method0 rfl enter.setup (header home) + (fun _ => True) (fun s => s.values.toNat) (fun start => ?_) ?_ · cil_loop_recurse start x case invariant => trivial diff --git a/Examples/Proofs/DecodedAdd.lean b/Examples/Proofs/DecodedAdd.lean new file mode 100644 index 000000000..d1c56f099 --- /dev/null +++ b/Examples/Proofs/DecodedAdd.lean @@ -0,0 +1,54 @@ +import Extracted +import CIL.Safety.DecodedSegment + +/-! +# Addition over the decoded program + +`correctness_decoded` states the addition contract of `Examples.Arithmetic::Add` for the program +that `Extraction.decodedProgram?` reads from the extraction's body bytes. The proof checks the +feature certificate and evaluates the method decoded from the pinned bytes. +-/ + +open CIL +namespace ExampleProof + +/-- The program the Add body bytes denote returns the sum of its two 64-bit arguments modulo + 2^64, in the specified empty initial memory. -/ +theorem correctness_decoded : ∃ program : CIL.Program, + Extraction.decodedProgram? Extracted.profile Extracted.environment + Extracted.methodEvidence = some program ∧ ∀ x y : W64, ∃ result : W64, + (CIL.Safety.invoke program 4 0 [.scalar (.i64 x), .scalar (.i64 y)] + Safety.Memory.empty).map Prod.snd = .ok [.scalar (.i64 result)] ∧ + result.toNat = (x.toNat + y.toNat) % 2^64 := by + simp only [Extraction.decodedProgram?, Extracted.methodEvidence, List.mapM_cons, + List.mapM_nil, Method.ofBody?_of_decodes Extracted.method0_body_validated, + Option.filter_eq_some_iff] + exact ⟨_, ⟨rfl, by decide +kernel⟩, fun x y => ⟨x + y, rfl, BitVec.toNat_add x y⟩⟩ + +#print axioms correctness_decoded +/-- At the add instruction, the stack contains the right and left operands. The continuation + keeps the frame and memory and replaces the operands with their sum. -/ +def arithmeticStep (left right : W64) : Safety.SegmentContract := + .continuing 3 (fun _ before => before.stack = [.scalar (.i64 right), .scalar (.i64 left)]) + (fun _ before after => after = { before with stack := [.scalar (.i64 (left + right))] }) + (fun _ _ => []) + +/-- The arithmetic step at pc 2 is bound to the selected Add method and its certified body. -/ +theorem arithmetic_region (left right : W64) : + Safety.DecodedSegmentSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment 0 2 Extracted.methodEvidence[0].assembly + Extracted.methodEvidence[0].original.token Extracted.methodEvidence[0].arguments + .add [] (arithmeticStep left right) := by + refine ⟨Safety.DecodedAt.of_translation Extracted.translation_valid ⟨0, by decide⟩ + Extracted.program_method0, ⟨_, Extracted.program_method0, rfl⟩, ?_⟩ + rintro args ⟨frame, stack, memory⟩ permitted + change stack = _ at permitted + subst stack + let after : Safety.SegmentState := ⟨frame, [.scalar (.i64 (left + right))], memory⟩ + refine ⟨.reaches 3 after, ⟨1, by decide, Safety.SegmentRun.reaches ?_⟩, + Safety.Confined.refl _ _, rfl, rfl⟩ + intro fuel + rfl + +#print axioms arithmetic_region +end ExampleProof diff --git a/Examples/Proofs/DepositBoth.lean b/Examples/Proofs/DepositBoth.lean index 08c3ee598..f21ab9ea3 100644 --- a/Examples/Proofs/DepositBoth.lean +++ b/Examples/Proofs/DepositBoth.lean @@ -102,7 +102,7 @@ theorem first_null_throws (memory : Memory) (second : Reference) (amount : W64) (secondLive : LiveObject memory second) : ∃ fuel, invoke Extracted.program fuel depositBoth [.object .null, .object (.address second), .scalar (.i64 amount)] memory = - .error ⟨depositBoth, 2, .thrown "System.NullReferenceException"⟩ := by + .error ⟨depositBoth, 2, .thrown .nullReference⟩ := by cil_checked_steps #print axioms first_null_throws @@ -121,7 +121,7 @@ theorem second_null_throws (memory : Memory) (first : Reference) (amount : W64) Account.Balance.Holds middle first (Account.Balance.of memory first + amount) ∧ invoke Extracted.program (fuel + 6) depositBoth [.object (.address first), .object .null, .scalar (.i64 amount)] memory = - .error ⟨depositBoth, 5, .thrown "System.NullReferenceException"⟩ := by + .error ⟨depositBoth, 5, .thrown .nullReference⟩ := by obtain ⟨middle, values, deposited⟩ := deposit_holds.result (first, amount) owned obtain ⟨rfl, held⟩ := deposited.post obtain ⟨childFuel, invoked⟩ := deposited.invoked diff --git a/Examples/Proofs/DepositTimes.lean b/Examples/Proofs/DepositTimes.lean index 9f0732524..17b5d08f1 100644 --- a/Examples/Proofs/DepositTimes.lean +++ b/Examples/Proofs/DepositTimes.lean @@ -59,7 +59,7 @@ private abbrev State (memory : Memory) (account home : Reference) := /-- The loop test, with the count made and `times` on the stack. -/ bind_cutpoint header (memory : Memory) (account : Reference) (times : W32) (home : Reference) (state : State memory account home) := - depositTimesBody at loop.latch matching (fun op => op matches .bltu _) with + depositTimesBody at loop.latch matching (fun op => op matches .blt_un _) with entry.frame memory home, [.scalar (.i32 times), .scalar (.i32 state.values)], state.memory /-- The balance after `count` deposits of `amount` onto `balance`. -/ diff --git a/Examples/Proofs/DivRemQuotient.lean b/Examples/Proofs/DivRemQuotient.lean index a6d1b89da..1a6adf96d 100644 --- a/Examples/Proofs/DivRemQuotient.lean +++ b/Examples/Proofs/DivRemQuotient.lean @@ -16,6 +16,8 @@ open CIL CIL.Safety def inputs (lower upper divisor : W64) : List CIL.Safety.Value := [.scalar (.i64 lower), .scalar (.i64 upper), .scalar (.i64 divisor)] +-- Comparing the two fully assembly-qualified tuple arguments needs additional reduction depth. +set_option maxRecDepth 2048 in /-- `DivRem(lower, upper, divisor).Quotient` is the natural quotient when it fits, and faults otherwise. -/ theorem correctness (lower upper divisor : W64) : diff --git a/Examples/Proofs/DogCast.lean b/Examples/Proofs/DogCast.lean index bc81fb033..75970fd4f 100644 --- a/Examples/Proofs/DogCast.lean +++ b/Examples/Proofs/DogCast.lean @@ -38,7 +38,7 @@ theorem bird_throws (memory : CIL.Safety.Memory) (animal : Reference) (formed : formObject memory (.address animal) = .ok (.object (.address animal))) (typed : runtimeTypeOf memory animal = .ok bird) : ∃ fuel, invoke Extracted.program fuel 0 [.object (.address animal)] memory = - .error ⟨0, 1, .thrown "System.InvalidCastException"⟩ := by + .error ⟨0, 1, .thrown .invalidCast⟩ := by cil_checked_steps [formed, typed, bird, scope, animalType, RuntimeType.lineage] #print axioms bird_throws @@ -59,11 +59,11 @@ theorem correctness (memory : CIL.Safety.Memory) (animal : Reference) .ok (memory, [.object (.address animal)])) ∧ (runtimeTypeOf memory animal = .ok bird → ∃ fuel, invoke Extracted.program fuel 0 [.object (.address animal)] memory = - .error ⟨0, 1, .thrown "System.InvalidCastException"⟩) ∧ + .error ⟨0, 1, .thrown .invalidCast⟩) ∧ ∃ fuel, invoke Extracted.program fuel 0 [.object .null] memory = .ok (memory, [.object .null]) := ⟨dog_casts memory animal formed, bird_throws memory animal formed, null_casts memory⟩ #print axioms correctness -end ExampleProof \ No newline at end of file +end ExampleProof diff --git a/Examples/Proofs/FillWords.lean b/Examples/Proofs/FillWords.lean index 82a310e38..6447b63ad 100644 --- a/Examples/Proofs/FillWords.lean +++ b/Examples/Proofs/FillWords.lean @@ -60,7 +60,7 @@ theorem fill_words (initial : Memory) (base : Reference) (v : W64) (n : W32) have baseForm := frame.form valid old have frame1 := frame.write_element stored (List.mem_singleton_self _) lower old cil_loop_recurse recurse (k + 1) [int_lt small lower, - add_int_element frame.wellFormed baseForm (by omega) target, step_store64_of_write stored] + add_int_element frame.wellFormed baseForm (by omega) target, step_stind_i8_of_write stored] all_goals simp only [invariant, toNat_succ lower] case invariant => rw [List.replicate_succ', show n.toNat - (k.toNat + 1) = n.toNat - k.toNat - 1 by omega] diff --git a/Examples/Proofs/GuardCount.lean b/Examples/Proofs/GuardCount.lean index fb41c4e28..5f199255b 100644 --- a/Examples/Proofs/GuardCount.lean +++ b/Examples/Proofs/GuardCount.lean @@ -18,15 +18,18 @@ theorem correctness (memory : Memory) (literal : Reference) (count : CIL.W32) (bound : memory.literals.lookup "count" = some literal) (formed : formObject memory (.address literal) = .ok (.object (.address literal))) : ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i32 count)] memory = - if count.toInt < 0 then .error ⟨0, 2, .thrown "System.ArgumentOutOfRangeException"⟩ + if count.toInt < 0 then .error ⟨0, 2, .thrown .argumentOutOfRange⟩ else .ok (memory, [.scalar (.i32 count)]) := by + have outcome : zeroGuardOutcome .negative 32 true (.i32 count) = + if count.toInt < 0 then .error (.thrown .argumentOutOfRange) + else .ok () := by + by_cases negative : count.toInt < 0 <;> + simp [zeroGuardOutcome, CIL.guardOperand, CIL.ZeroGuard.throws, negative] by_cases negative : count.toInt < 0 · simp only [negative, ite_true] - cil_checked_steps [literalObject_of_lookup bound, formed, zeroGuardOutcome, - CIL.guardOperand, CIL.ZeroGuard.throws, BitVec.setWidth_eq, negative] + cil_checked_steps [literalObject_of_lookup bound, formed, outcome, negative] · simp only [negative, ite_false] - cil_checked_steps [literalObject_of_lookup bound, formed, zeroGuardOutcome, - CIL.guardOperand, CIL.ZeroGuard.throws, BitVec.setWidth_eq, negative] + cil_checked_steps [literalObject_of_lookup bound, formed, outcome, negative] #print axioms correctness @@ -39,4 +42,4 @@ theorem zero_returned (memory : Memory) (literal : Reference) simpa using correctness memory literal 0 bound formed #print axioms zero_returned -end ExampleProof \ No newline at end of file +end ExampleProof diff --git a/Examples/Proofs/GuardIndex.lean b/Examples/Proofs/GuardIndex.lean index 90798f9ae..3efedc6a5 100644 --- a/Examples/Proofs/GuardIndex.lean +++ b/Examples/Proofs/GuardIndex.lean @@ -21,16 +21,18 @@ theorem correctness (memory : Memory) (literal : Reference) (index length : CIL. ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i32 index), .scalar (.i32 length)] memory = if length.toNat ≤ index.toNat then - .error ⟨0, 3, .thrown "System.ArgumentOutOfRangeException"⟩ + .error ⟨0, 3, .thrown .argumentOutOfRange⟩ else .ok (memory, [.scalar (.i32 index)]) := by + have outcome : comparisonGuardOutcome .greaterThanOrEqual 32 false (.i32 index) + (.i32 length) = if length.toNat <= index.toNat then + .error (.thrown .argumentOutOfRange) else .ok () := by + by_cases outside : length.toNat <= index.toNat <;> + simp [comparisonGuardOutcome, CIL.guardOperand, CIL.ComparisonGuard.throws, outside] by_cases outside : length.toNat ≤ index.toNat · simp only [outside, ite_true] - cil_checked_steps [literalObject_of_lookup bound, formed, comparisonGuardOutcome, - CIL.guardOperand, CIL.ComparisonGuard.throws, BitVec.setWidth_eq, outside] + cil_checked_steps [literalObject_of_lookup bound, formed, outcome, outside] · simp only [outside, ite_false] - cil_checked_steps [literalObject_of_lookup bound, formed, - comparisonGuardOutcome, CIL.guardOperand, CIL.ComparisonGuard.throws, BitVec.setWidth_eq, - outside] + cil_checked_steps [literalObject_of_lookup bound, formed, outcome, outside] #print axioms correctness @@ -50,8 +52,8 @@ theorem equal_raises (memory : Memory) (literal : Reference) (index : CIL.W32) (bound : memory.literals.lookup "index" = some literal) (formed : formObject memory (.address literal) = .ok (.object (.address literal))) : ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i32 index), .scalar (.i32 index)] memory = - .error ⟨0, 3, .thrown "System.ArgumentOutOfRangeException"⟩ := by + .error ⟨0, 3, .thrown .argumentOutOfRange⟩ := by simpa using correctness memory literal index index bound formed #print axioms equal_raises -end ExampleProof \ No newline at end of file +end ExampleProof diff --git a/Examples/Proofs/GuardRequired.lean b/Examples/Proofs/GuardRequired.lean index 29b2c111a..ba90f7b59 100644 --- a/Examples/Proofs/GuardRequired.lean +++ b/Examples/Proofs/GuardRequired.lean @@ -17,7 +17,7 @@ theorem null_raises (memory : Memory) (literal : Reference) (bound : memory.literals.lookup "label" = some literal) (formed : formObject memory (.address literal) = .ok (.object (.address literal))) : ∃ fuel, invoke Extracted.program fuel 0 [.object .null] memory = - .error ⟨0, 2, .thrown "System.ArgumentNullException"⟩ := by + .error ⟨0, 2, .thrown .argumentNull⟩ := by cil_checked_steps [literalObject_of_lookup bound, formed] #print axioms null_raises @@ -40,7 +40,7 @@ theorem correctness (memory : Memory) (label : ManagedReference) (literal : Refe (bound : memory.literals.lookup "label" = some literal) (formed : formObject memory (.address literal) = .ok (.object (.address literal))) : ∃ fuel, invoke Extracted.program fuel 0 [.object label] memory = - if label = .null then .error ⟨0, 2, .thrown "System.ArgumentNullException"⟩ + if label = .null then .error ⟨0, 2, .thrown .argumentNull⟩ else .ok (memory, [.object label]) := by cases label with | null => simpa using null_raises memory literal bound formed @@ -48,4 +48,4 @@ theorem correctness (memory : Memory) (label : ManagedReference) (literal : Refe simpa using keeps_label memory reference literal (labelFormed reference rfl) bound formed #print axioms correctness -end ExampleProof \ No newline at end of file +end ExampleProof diff --git a/Examples/Proofs/HeadroomReceiver.lean b/Examples/Proofs/HeadroomReceiver.lean index add1231da..b7dc746e4 100644 --- a/Examples/Proofs/HeadroomReceiver.lean +++ b/Examples/Proofs/HeadroomReceiver.lean @@ -29,7 +29,7 @@ theorem correctness (memory : CIL.Safety.Memory) (account : CIL.Safety.ManagedRe /-- Calling the getter on a null receiver throws `NullReferenceException` at its first `ldfld`. -/ theorem null_throws (memory : CIL.Safety.Memory) : ∃ fuel, CIL.Safety.invoke Extracted.program fuel 0 [.object .null] memory = - .error ⟨0, 1, .thrown "System.NullReferenceException"⟩ := by + .error ⟨0, 1, .thrown .nullReference⟩ := by cil_checked_steps [CIL.Safety.objectFieldAddress] #print axioms null_throws diff --git a/Examples/Proofs/IdentitySummary.lean b/Examples/Proofs/IdentitySummary.lean new file mode 100644 index 000000000..7ed9c71bb --- /dev/null +++ b/Examples/Proofs/IdentitySummary.lean @@ -0,0 +1,89 @@ +import Extracted +import CIL +import CIL.MethodIdentity + +/-! +# Identity through a decoded summary + +`Examples.GenericMethods::Identity64` calls `Identity` once. `correctness_stepped` steps +the caller and the specialization instruction by instruction. `correctness` proves the same +statement from a `DecodedSummary` of the specialization: its contract, bound to the record whose +pinned bytes decode to it, applied at the call without stepping its body. +-/ + +namespace ExampleProof +open CIL CIL.Safety CIL.Extraction + +bind_method identity identityBody := + "System.UInt64 Examples.GenericMethods::Identity(System.UInt64)" + +/-- The identity specialization returns its argument and changes no memory. -/ +def identityContract : MethodContract W64 W64 where + args x := [.scalar (.i64 x)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post x initial final value := value = x ∧ final = initial + +/-- The caller returns its argument and keeps the caller memory, stepping the specialization. -/ +theorem correctness_stepped (memory : Safety.Memory) (x : W64) : + ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory = + .ok (memory, [.scalar (.i64 x)]) := by + cil_checked_steps +#print axioms correctness_stepped + +/-- The specialization meets its contract. -/ +theorem identity_holds : identityContract.Holds Extracted.program identity := by + rintro x memory - + refine MethodContract.Holds.intro memory x ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + cil_checked_steps +#print axioms identity_holds + +/-- Every record's pinned bytes decode to the method at its index of the extraction. -/ +theorem bodies_decode : + decodedProgram? Extracted.profile Extracted.environment Extracted.methodEvidence = + some Extracted.program := + decodedProgram?_eq_some Extracted.translation_valid.programBindingsValid + (.cons ((Method.ofBody?_of_decodes Extracted.method0_body_validated).trans + Extracted.method0_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method1_body_validated).trans + Extracted.method1_decoded) .nil)) +#print axioms bodies_decode + +/-- The contract, bound to the record of the specialization and the bytes it decodes from. -/ +theorem identity_summary : ∃ record, Extracted.methodEvidence[identity]? = some record ∧ + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment identity record.assembly record.original.token record.arguments + identityContract := + ⟨_, rfl, .of_decoded bodies_decode rfl ⟨_, Extracted.method1_body_validated⟩ + Extracted.method1_specialization_validated.1 identity_holds⟩ +#print axioms identity_summary + +/-- The call of the caller names the metadata token and type arguments of any summary of the + index its link enters. -/ +theorem calls_name_summary {pin : AssemblyPin} {token : Nat} {arguments : List TypeExpr} + (summary : DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment identity pin token arguments identityContract) : + ∃ site, + (lowerConstrainedRaw Extracted.method0DispatchTypes Extracted.method0Closed).code[1]?.map + RawInstruction.operand = some (.call site) ∧ + site.target = .managed token ∧ site.typeArguments = arguments := by + obtain ⟨sameToken, sameArguments⟩ := + summary.site Extracted.method0_calls_validated (pc := 1) rfl rfl rfl rfl rfl rfl + exact ⟨_, rfl, by rw [sameToken], sameArguments.symm⟩ +#print axioms calls_name_summary + +/-- The caller returns its argument and keeps the caller memory, applying the summary at + the call. -/ +theorem correctness (memory : Safety.Memory) (x : W64) : + ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory = + .ok (memory, [.scalar (.i64 x)]) := by + obtain ⟨record, -, summary⟩ := identity_summary + refine (WP.exact_iff (execution := Execution fun fuel => + invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory)).mp ?_ + cil_contract_steps [summary.holds, identityContract] with middle value firstCall + obtain ⟨rfl, rfl⟩ := firstCall.post + rfl +#print axioms correctness + +end ExampleProof diff --git a/Examples/Proofs/LinkedNodes.lean b/Examples/Proofs/LinkedNodes.lean index 49145c87a..92476f17c 100644 --- a/Examples/Proofs/LinkedNodes.lean +++ b/Examples/Proofs/LinkedNodes.lean @@ -68,7 +68,7 @@ theorem secondValue_holds : secondValueContract.Holds Extracted.program secondVa /-- A null node throws `NullReferenceException` at `SecondValue`'s first field load, pc 1. -/ theorem secondValue_null_node_throws (memory : Memory) : ∃ fuel, invoke Extracted.program fuel secondValue [.object .null] memory = - .error ⟨secondValue, 1, .thrown "System.NullReferenceException"⟩ := by + .error ⟨secondValue, 1, .thrown .nullReference⟩ := by cil_checked_steps #print axioms secondValue_null_node_throws @@ -78,7 +78,7 @@ theorem secondValue_null_node_throws (memory : Memory) : theorem secondValue_null_next_throws (memory : Memory) (node : Reference) (readable : Node.Next.Readable memory node) (held : Node.Next.of memory node = .null) : ∃ fuel, invoke Extracted.program fuel secondValue [.object (.address node)] memory = - .error ⟨secondValue, 2, .thrown "System.NullReferenceException"⟩ := by + .error ⟨secondValue, 2, .thrown .nullReference⟩ := by cil_checked_steps [readable.load_null held] #print axioms secondValue_null_next_throws diff --git a/Examples/Proofs/MathDivRemQuotient.lean b/Examples/Proofs/MathDivRemQuotient.lean index b8ddab253..e16419fbc 100644 --- a/Examples/Proofs/MathDivRemQuotient.lean +++ b/Examples/Proofs/MathDivRemQuotient.lean @@ -16,6 +16,8 @@ open CIL CIL.Safety /-- The two `int` arguments as stack values. -/ def inputs (left right : W32) : List CIL.Safety.Value := [.scalar (.i32 left), .scalar (.i32 right)] +-- Comparing the two fully assembly-qualified tuple arguments needs additional reduction depth. +set_option maxRecDepth 2048 in /-- `Math.DivRem(left, right).Quotient` returns the exact truncated quotient whenever `div` succeeds, and otherwise raises its divide-by-zero or overflow fault. -/ theorem correctness (left right : W32) : diff --git a/Examples/Proofs/NativeAdd.lean b/Examples/Proofs/NativeAdd.lean index f7c10b96f..6b334e9a9 100644 --- a/Examples/Proofs/NativeAdd.lean +++ b/Examples/Proofs/NativeAdd.lean @@ -10,22 +10,23 @@ theorem correctness (x y : W64) : Memory.empty).map Prod.snd = .ok [.scalar (.i64 (x + y))] := by let args : List CIL.Safety.Value := [.scalar (.i64 x), .scalar (.i64 y)] - let frame : Frame := ⟨0, [], [], [], 0⟩ - have first : CIL.Safety.step Extracted.method0 (.arg 0) 0 args frame [] Memory.empty = + let frame : Frame := ⟨0, [], [], 0⟩ + have first : CIL.Safety.step Extracted.method0 (.ldarg 0) 0 args frame [] Memory.empty = .ok (.next 1 [.scalar (.i64 x)] frame Memory.empty) := rfl have second : - CIL.Safety.step Extracted.method0 (.arg 1) 1 args frame [.scalar (.i64 x)] Memory.empty = + CIL.Safety.step Extracted.method0 (.ldarg 1) 1 args frame [.scalar (.i64 x)] Memory.empty = .ok (.next 2 [.scalar (.i64 y), .scalar (.i64 x)] frame Memory.empty) := rfl have added : CIL.Safety.step Extracted.method0 .add 2 args frame [.scalar (.i64 y), .scalar (.i64 x)] Memory.empty = .ok (.next 3 [.scalar (.i64 (x + y))] frame Memory.empty) := rfl have ran : CIL.Safety.run Extracted.program 4 0 0 args frame [] Memory.empty = .ok (Memory.empty, [.scalar (.i64 (x + y))]) := by - rw [run_next Extracted.lookup0 Extracted.method0_instruction0 first] - rw [run_next Extracted.lookup0 Extracted.method0_instruction1 second] - rw [run_next Extracted.lookup0 Extracted.method0_instruction2 added] + rw [run_next Extracted.program_method0 Extracted.method0_instruction0 first] + rw [run_next Extracted.program_method0 Extracted.method0_instruction1 second] + rw [run_next Extracted.program_method0 Extracted.method0_instruction2 added] rfl - have invoked := invoke_of_run (memory := Memory.empty) Extracted.lookup0 (by rfl) (by rfl) ran + have invoked := invoke_of_run (memory := Memory.empty) + Extracted.program_method0 (by rfl) (by rfl) ran rw [invoked] rfl diff --git a/Examples/Proofs/OpaqueSeeds.lean b/Examples/Proofs/OpaqueSeeds.lean index 6bc3ed5cf..12db19b2b 100644 --- a/Examples/Proofs/OpaqueSeeds.lean +++ b/Examples/Proofs/OpaqueSeeds.lean @@ -6,14 +6,14 @@ open CIL CIL.Safety namespace ExampleProof /-- The identity extraction gave the static field with this metadata name. -/ -def fieldIdentity (name : String) : Nat := - ((Extracted.program.flatMap (·.staticSites.map Prod.snd)).find? (·.fieldName == name)).elim 0 +def fieldIdentity (name : MetadataName) : Nat := + ((Extracted.program.flatMap (·.staticSites.map Prod.snd)).find? (·.field.name == name)).elim 0 (·.identity) /-- Caller memory in which the two process-random seeds hold `seed0` and `seed1`. -/ def seeded (seed0 seed1 : W64) : Safety.Memory := Safety.Memory.boundStatics - [(fieldIdentity "System.UInt64 Examples.SeededHash::Seed0", numberBytes seed0.toNat 8), - (fieldIdentity "System.UInt64 Examples.SeededHash::Seed1", numberBytes seed1.toNat 8)] + [(fieldIdentity name!"Seed0", numberBytes seed0.toNat 8), + (fieldIdentity name!"Seed1", numberBytes seed1.toNat 8)] /-- The hash `SeededHash.Hash` computes from its seeds. -/ def mix (seed0 seed1 value : W64) : W64 := (value ^^^ seed0) * 0x9E3779B97F4A7C15#64 + seed1 diff --git a/Examples/Proofs/OpaqueTag.lean b/Examples/Proofs/OpaqueTag.lean index 4d7c09965..4d199559d 100644 --- a/Examples/Proofs/OpaqueTag.lean +++ b/Examples/Proofs/OpaqueTag.lean @@ -42,7 +42,7 @@ theorem correctness : copyTagContract.Holds Extracted.program copyTag := by theorem copyTag_null_source_throws (memory : Memory) (target : Reference) (live : LiveObject memory target) : ∃ fuel, invoke Extracted.program fuel copyTag [.object (.address target), .object .null] - memory = .error ⟨copyTag, 2, .thrown "System.NullReferenceException"⟩ := by + memory = .error ⟨copyTag, 2, .thrown .nullReference⟩ := by cil_checked_steps #print axioms copyTag_null_source_throws @@ -52,7 +52,7 @@ theorem copyTag_null_source_throws (memory : Memory) (target : Reference) theorem copyTag_null_target_throws (memory : Memory) (source : Reference) (readable : Tagged.Tag.Readable memory source) (forms : Tagged.Tag.Forms memory source) : ∃ fuel, invoke Extracted.program fuel copyTag [.object .null, .object (.address source)] - memory = .error ⟨copyTag, 3, .thrown "System.NullReferenceException"⟩ := by + memory = .error ⟨copyTag, 3, .thrown .nullReference⟩ := by cil_checked_steps [readable.load_forms forms] #print axioms copyTag_null_target_throws diff --git a/Examples/Proofs/PoolIdle.lean b/Examples/Proofs/PoolIdle.lean index 5895dab0e..d58c9d8a4 100644 --- a/Examples/Proofs/PoolIdle.lean +++ b/Examples/Proofs/PoolIdle.lean @@ -34,7 +34,7 @@ private abbrev State (memory : Memory) := /-- The loop test, with the node on the stack and in the local. -/ bind_cutpoint header (memory : Memory) (state : State memory) := - idleBody at loop.latch matching (fun op => op matches .brnonzero _) with + idleBody at loop.latch matching (fun op => op matches .brtrue _) with { entry.frame memory with locals := [.object (some state.values.1)] }, [.object state.values.1], state.memory diff --git a/Examples/Proofs/PoolRent.lean b/Examples/Proofs/PoolRent.lean index 954c8389d..9fd1f13f9 100644 --- a/Examples/Proofs/PoolRent.lean +++ b/Examples/Proofs/PoolRent.lean @@ -364,7 +364,7 @@ theorem rent_exhausted_throws (memory : Memory) (pool : Reference) (capacity : ItemPool._capacity.Readable memory pool) (full : ItemPool._live.of memory pool = ItemPool._capacity.of memory pool) : ∃ fuel, invoke Extracted.program fuel rent [.object (.address pool)] memory = - .error ⟨rent, 12, .thrown "System.InvalidOperationException"⟩ := by + .error ⟨rent, 12, .thrown .invalidOperation⟩ := by obtain ⟨home, entered, enter⟩ := entry.checked memory (.address pool) wellFormed have keep := enter.entryMemory.kept @@ -382,7 +382,7 @@ theorem rent_exhausted_throws (memory : Memory) (pool : Reference) /-- Renting from a null pool throws `NullReferenceException` at the first field load, pc 1. -/ theorem rent_null_pool_throws (memory : Memory) (wellFormed : memory.WellFormed) : ∃ fuel, invoke Extracted.program fuel rent [.object .null] memory = - .error ⟨rent, 1, .thrown "System.NullReferenceException"⟩ := by + .error ⟨rent, 1, .thrown .nullReference⟩ := by obtain ⟨home, entered, enter⟩ := entry.checked memory .null wellFormed cil_checked_steps [enter.setup] diff --git a/Examples/Proofs/RepeatedSummary.lean b/Examples/Proofs/RepeatedSummary.lean new file mode 100644 index 000000000..bab19a6d0 --- /dev/null +++ b/Examples/Proofs/RepeatedSummary.lean @@ -0,0 +1,96 @@ +import Extracted +import CIL +import CIL.MethodIdentity + +/-! +# Identity through a decoded summary + +`Examples.GenericMethods::Repeated` calls `Identity` twice. `correctness_stepped` steps +the caller and the specialization instruction by instruction. `correctness` proves the same +statement from a `DecodedSummary` of the specialization: its contract, bound to the record whose +pinned bytes decode to it, applied at each call without stepping its body. +-/ + +namespace ExampleProof +open CIL CIL.Safety CIL.Extraction + +bind_method identity identityBody := + "System.UInt64 Examples.GenericMethods::Identity(System.UInt64)" + +/-- The identity specialization returns its argument and changes no memory. -/ +def identityContract : MethodContract W64 W64 where + args x := [.scalar (.i64 x)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post x initial final value := value = x ∧ final = initial + +/-- The caller returns its argument and keeps the caller memory, stepping the specialization. -/ +theorem correctness_stepped (memory : Safety.Memory) (x : W64) : + ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory = + .ok (memory, [.scalar (.i64 x)]) := by + cil_checked_steps +#print axioms correctness_stepped + +/-- The specialization meets its contract. -/ +theorem identity_holds : identityContract.Holds Extracted.program identity := by + rintro x memory - + refine MethodContract.Holds.intro memory x ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + cil_checked_steps +#print axioms identity_holds + +/-- Every record's pinned bytes decode to the method at its index of the extraction. -/ +theorem bodies_decode : + decodedProgram? Extracted.profile Extracted.environment Extracted.methodEvidence = + some Extracted.program := + decodedProgram?_eq_some Extracted.translation_valid.programBindingsValid + (.cons ((Method.ofBody?_of_decodes Extracted.method0_body_validated).trans + Extracted.method0_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method1_body_validated).trans + Extracted.method1_decoded) .nil)) +#print axioms bodies_decode + +/-- The contract, bound to the record of the specialization and the bytes it decodes from. -/ +theorem identity_summary : ∃ record, Extracted.methodEvidence[identity]? = some record ∧ + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment identity record.assembly record.original.token record.arguments + identityContract := + ⟨_, rfl, .of_decoded bodies_decode rfl ⟨_, Extracted.method1_body_validated⟩ + Extracted.method1_specialization_validated.1 identity_holds⟩ +#print axioms identity_summary + +/-- Both calls of the caller name the metadata token and type arguments of any summary of the + index their links enter. -/ +theorem calls_name_summary {pin : AssemblyPin} {token : Nat} {arguments : List TypeExpr} + (summary : DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment identity pin token arguments identityContract) : + ∀ pc ∈ [1, 2], ∃ site, + (lowerConstrainedRaw Extracted.method0DispatchTypes Extracted.method0Closed).code[pc]?.map + RawInstruction.operand = some (.call site) ∧ + site.target = .managed token ∧ site.typeArguments = arguments := by + intro pc listed + simp only [List.mem_cons, List.not_mem_nil, or_false] at listed + rcases listed with rfl | rfl + · obtain ⟨sameToken, sameArguments⟩ := + summary.site Extracted.method0_calls_validated (pc := 1) rfl rfl rfl rfl rfl rfl + exact ⟨_, rfl, by rw [sameToken], sameArguments.symm⟩ + · obtain ⟨sameToken, sameArguments⟩ := + summary.site Extracted.method0_calls_validated (pc := 2) rfl rfl rfl rfl rfl rfl + exact ⟨_, rfl, by rw [sameToken], sameArguments.symm⟩ +#print axioms calls_name_summary + +/-- The caller returns its argument and keeps the caller memory, applying the summary at + each call. -/ +theorem correctness (memory : Safety.Memory) (x : W64) : + ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory = + .ok (memory, [.scalar (.i64 x)]) := by + obtain ⟨record, -, summary⟩ := identity_summary + refine (WP.exact_iff (execution := Execution fun fuel => + invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory)).mp ?_ + cil_contract_steps [summary.holds, identityContract] with middle value firstCall + obtain ⟨rfl, rfl⟩ := firstCall.post + obtain ⟨rfl, rfl⟩ := call_1.post + rfl +#print axioms correctness + +end ExampleProof diff --git a/Examples/Proofs/SampleCount.lean b/Examples/Proofs/SampleCount.lean index a71e29619..c1375ef03 100644 --- a/Examples/Proofs/SampleCount.lean +++ b/Examples/Proofs/SampleCount.lean @@ -34,7 +34,7 @@ theorem null_values (memory : Memory) (samples : Reference) (readable : Samples.Values.Readable memory samples) (held : Samples.Values.of memory samples = .null) : ∃ fuel, invoke Extracted.program fuel count [.object (.address samples)] memory = - .error ⟨count, 2, .thrown "System.NullReferenceException"⟩ := by + .error ⟨count, 2, .thrown .nullReference⟩ := by cil_checked_steps [readable.load_null held rfl] #print axioms null_values diff --git a/Examples/Proofs/SampleRefill.lean b/Examples/Proofs/SampleRefill.lean index 11df3f366..df7a756e6 100644 --- a/Examples/Proofs/SampleRefill.lean +++ b/Examples/Proofs/SampleRefill.lean @@ -44,7 +44,7 @@ theorem null_samples (memory : Memory) (values : Reference) (length : Nat) (valid : ArrayView.validate ⟨.address values, length, .signed32⟩ memory = .ok ()) : ∃ fuel, invoke Extracted.program fuel refill [.object .null, .array ⟨.address values, length, .signed32⟩] memory = - .error ⟨refill, 2, .thrown "System.NullReferenceException"⟩ := by + .error ⟨refill, 2, .thrown .nullReference⟩ := by cil_checked_steps [valid] #print axioms null_samples diff --git a/Examples/Proofs/SignedBigMul.lean b/Examples/Proofs/SignedBigMul.lean index c2bbea747..afdf90dd1 100644 --- a/Examples/Proofs/SignedBigMul.lean +++ b/Examples/Proofs/SignedBigMul.lean @@ -23,7 +23,8 @@ theorem correctness (memory result : Memory) (reference : Reference) (left right (CIL.WideMultiply.signedHigh64 left right).toInt * 2 ^ 64 + ((left * right).toNat : Int) = left.toInt * right.toInt := by refine ⟨?_, CIL.WideMultiply.signedHigh64_decomposition left right⟩ - cil_checked_steps [formed, step_bigMul64_signed_of_write stored] + cil_checked_steps [formed, + step_bigMul64_signed_of_write Extracted.method0_link3 Extracted.method0_model3 stored] #print axioms correctness end ExampleProof diff --git a/Examples/Proofs/SkipInitialization.lean b/Examples/Proofs/SkipInitialization.lean index df280828c..abf35be5a 100644 --- a/Examples/Proofs/SkipInitialization.lean +++ b/Examples/Proofs/SkipInitialization.lean @@ -1,5 +1,6 @@ import Extracted import CIL.Safety.SymbolicExecution +import CIL.Safety.StepComposition /-! Skipping initialization consumes a checked reference without initializing its storage. -/ @@ -28,16 +29,67 @@ theorem correctness : invoke Extracted.program 4 0 [callerReference] uninitialized = .ok (uninitialized, []) := by rfl -/-- The checked instruction preserves every byte and initialization flag of caller memory. -/ -theorem step_preserves (body : CIL.Method) (pc : Nat) (args : List Value) - (frame : Frame) (memory : Memory) (reference : ManagedReference) (stack : List Value) +/-- A `SkipInit` call the runtime table models preserves every byte and initialization flag of + caller memory. -/ +theorem step_preserves (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) + (memory : Memory) {method : CIL.Extraction.MethodRef} {prefixes : CIL.CallPrefixes} + (link : body.linkAt pc = none) + (found : CIL.Extraction.runtimeModel body.environment .call method = some .skipInit) + (direct : prefixes.tail = false) (reference : ManagedReference) (stack : List Value) (valid : ∃ value, formValue memory reference = .ok value) : - step body .skipInit pc args frame (.reference reference :: stack) memory = + step body (.call method prefixes) pc args frame (.reference reference :: stack) memory = .ok (.next (pc + 1) stack frame memory) := by obtain ⟨value, valid⟩ := valid - rw [step_skipInit_reference_eq, valid] + rw [step_call_skipInit_reference (link := link) (found := found) (direct := direct), valid] rfl #print axioms correctness #print axioms step_preserves + +/-- Both environments assign the call to the SkipInit family before checking its layout. -/ +theorem skip_family : + let raw := Extracted.method0Closed.code[1]'(by decide) + CIL.Extraction.siteClaim Extracted.environment none raw = + .model ∧ + CIL.Extraction.siteClaim {} none raw = + .model := by + decide +kernel + +#print axioms skip_family + +open CIL in +cil_decoded_method skipWithLayout := Method.ofRaw? Extracted.profile Extracted.environment + Extracted.method0Links Extracted.method0Reachable Extracted.method0Original + Extracted.method0TypeArgumentsRaw + +/-- The same decoding environment with no aggregate layouts. -/ +private def emptyLayouts : CIL.LayoutEnvironment := {} + +/-- The decoder retains the call with the method reference from the raw operand. -/ +theorem resolved_skip : + let raw := Extracted.method0Closed.code[1]'(by decide) + match raw.operand with + | .call site => skipWithLayout.code[1]? = some (.call site.method {}) + | _ => False := by rfl + +#print axioms resolved_skip + +/-- The extracted instruction pops the caller's reference and preserves its uninitialized bytes. -/ +theorem compiled_step (args : List Value) (frame : Frame) (stack : List Value) : + step Extracted.method0 (Extracted.method0.code[1]'(by decide)) 1 args frame + (callerReference :: stack) uninitialized = .ok (.next 2 stack frame uninitialized) := + step_preserves Extracted.method0 1 args frame uninitialized (prefixes := {}) + rfl Extracted.method0_model1 rfl (.address ⟨0, 0⟩) stack ⟨_, rfl⟩ + +#print axioms compiled_step + +/-- Without the value type's layout the call has no model, so the decoder refuses the method. -/ +theorem unresolved_skip : + (CIL.Method.ofRaw? Extracted.profile emptyLayouts Extracted.method0Links + Extracted.method0Reachable Extracted.method0Original + Extracted.method0TypeArgumentsRaw).isNone = true := by + decide +kernel + +#print axioms unresolved_skip + end ExampleProof diff --git a/Examples/Proofs/SpanAllZero.lean b/Examples/Proofs/SpanAllZero.lean index 64f619b0c..90a4bfe5d 100644 --- a/Examples/Proofs/SpanAllZero.lean +++ b/Examples/Proofs/SpanAllZero.lean @@ -20,7 +20,7 @@ bind_loop loop := Extracted.method0 -- The loop guard observes the remaining span length. bind_cutpoint header (memory : Memory) (state : SpanSuffix memory 4 false) := - Extracted.method0 at loop.latch matching (· matches .brnonzero _) + Extracted.method0 at loop.latch matching (· matches .brtrue _) with entry.frame memory state.view, [.scalar (.i32 (BitVec.ofNat 32 state.view.length))], memory /-- The compiled loop decides whether every mathematical element is zero, for any valid length. -/ @@ -29,7 +29,7 @@ theorem correctness (memory : Memory) (view : SpanView) (values : List CIL.W32) (contents : SpanContents memory view 4 values) : ∃ fuel, invoke Extracted.program fuel 0 [.bufferSpan view] memory = .ok (memory, [.scalar (.i32 (if values.all (· == 0) then 1 else 0))]) := by - refine invoke_loop_returns Extracted.lookup0 (by simp [cil_checked, valid]) + refine invoke_loop_returns Extracted.program_method0 (by simp [cil_checked, valid]) (entry.checked memory view valid contents.elementWidth readOnly) (header memory) (fun state => state.values.all (· == 0) = values.all (· == 0)) (·.values.length) ⟨view, values, valid, readOnly, contents⟩ (by cil_loop_steps) rfl ?_ diff --git a/Examples/Proofs/SpanDrain.lean b/Examples/Proofs/SpanDrain.lean index d3a87d879..13793ead5 100644 --- a/Examples/Proofs/SpanDrain.lean +++ b/Examples/Proofs/SpanDrain.lean @@ -24,7 +24,7 @@ private def State (memory : Memory) := -- The loop guard observes the current span length. bind_cutpoint header (memory : Memory) (state : State memory) := Extracted.method0 - at loop.latch matching (· matches .brnonzero _) + at loop.latch matching (· matches .brtrue _) with entry.frame memory state.val, [.scalar (.i32 (BitVec.ofNat 32 state.val.length))], memory /-- Every valid readonly integer span is consumed without changing any caller memory. -/ @@ -32,7 +32,7 @@ theorem drain (memory : Memory) (view : SpanView) (valid : view.validate memory (width : view.width = 4) (readOnly : view.writable = false) : ∃ fuel, invoke Extracted.program fuel 0 [.bufferSpan view] memory = .ok (memory, [.scalar (.i32 0)]) := by - refine invoke_loop_returns Extracted.lookup0 (by simp [cil_checked, valid]) + refine invoke_loop_returns Extracted.program_method0 (by simp [cil_checked, valid]) (entry.checked memory view valid width readOnly) (header memory) (fun _ => True) (·.val.length) ⟨view, valid, width, readOnly⟩ (by cil_loop_steps) trivial ?_ rintro ⟨view, valid, width, readOnly⟩ - diff --git a/Examples/Proofs/SpanSum.lean b/Examples/Proofs/SpanSum.lean index 49b3383f4..d097fc9cc 100644 --- a/Examples/Proofs/SpanSum.lean +++ b/Examples/Proofs/SpanSum.lean @@ -28,7 +28,7 @@ private abbrev State (memory : Memory) (home : Reference) := -- The loop guard observes the remaining span length. bind_cutpoint header (memory : Memory) (home : Reference) (state : State memory home) := - Extracted.method0 at loop.latch matching (· matches .brnonzero _) + Extracted.method0 at loop.latch matching (· matches .brtrue _) with entry.frame memory state.values.1.view home, [.scalar (.i32 (BitVec.ofNat 32 state.values.1.view.length))], state.memory @@ -43,7 +43,7 @@ theorem correctness (memory : Memory) (view : SpanView) (values : List W64) entry.checked memory view wellFormed valid contents.elementWidth readOnly obtain ⟨started, stored, frame, -⟩ := (enter.frame []).store 0 (.i64 0) rfl have span : CheckedSpan memory view 8 false values := ⟨valid, readOnly, contents⟩ - refine invoke_loop_results Extracted.lookup0 (by simp [cil_checked, valid]) enter.setup + refine invoke_loop_results Extracted.program_method0 (by simp [cil_checked, valid]) enter.setup (header memory home) (fun state => state.values.2 + state.values.1.values.sum = values.sum) (·.values.1.values.length) ⟨(⟨view, values, span⟩, 0), started, frame⟩ (by cil_loop_steps) (by simp) ?_ diff --git a/Examples/Proofs/StringLabel.lean b/Examples/Proofs/StringLabel.lean index 43c315181..1c32e9373 100644 --- a/Examples/Proofs/StringLabel.lean +++ b/Examples/Proofs/StringLabel.lean @@ -38,7 +38,7 @@ theorem correctness (memory : Memory) (item : Reference) (label : ManagedReferen theorem relabel_null_throws (memory : Memory) (label : ManagedReference) (labelFormed : formObject memory label = .ok (.object label)) : ∃ fuel, invoke Extracted.program fuel 0 [.object .null, .object label] memory = - .error ⟨0, 1, .thrown "System.NullReferenceException"⟩ := by + .error ⟨0, 1, .thrown .nullReference⟩ := by cil_checked_steps [labelFormed] #print axioms relabel_null_throws diff --git a/Examples/Proofs/TagLoad.lean b/Examples/Proofs/TagLoad.lean index 565a6c9db..5f53126f0 100644 --- a/Examples/Proofs/TagLoad.lean +++ b/Examples/Proofs/TagLoad.lean @@ -67,7 +67,7 @@ theorem index_out_of_range (memory : Memory) (tagged tags : Reference) (length : (live : ReferenceArray.Live memory tags textType length) (outside : length ≤ index.toNat) : ∃ fuel, invoke Extracted.program fuel take [.object (.address tagged), .object (.address tags), .scalar (.i32 index)] memory = - .error ⟨take, 3, .thrown "System.IndexOutOfRangeException"⟩ := by + .error ⟨take, 3, .thrown .indexOutOfRange⟩ := by cil_checked_steps [live.slot_outside outside] #print axioms index_out_of_range diff --git a/Examples/Proofs/TagStore.lean b/Examples/Proofs/TagStore.lean index 355e2fa01..17da38cf0 100644 --- a/Examples/Proofs/TagStore.lean +++ b/Examples/Proofs/TagStore.lean @@ -74,7 +74,7 @@ theorem index_out_of_range (memory : Memory) (tags tagged : Reference) (length : (forms : Tagged.Tag.Forms memory tagged) : ∃ fuel, invoke Extracted.program fuel put [.object (.address tags), .scalar (.i32 index), .object (.address tagged)] memory = - .error ⟨put, 4, .thrown "System.IndexOutOfRangeException"⟩ := by + .error ⟨put, 4, .thrown .indexOutOfRange⟩ := by cil_checked_steps [readable.load_forms forms, live.slot_outside outside] #print axioms index_out_of_range @@ -84,7 +84,7 @@ theorem null_array (memory : Memory) (tagged : Reference) (index : BitVec 32) (readable : Tagged.Tag.Readable memory tagged) (forms : Tagged.Tag.Forms memory tagged) : ∃ fuel, invoke Extracted.program fuel put [.object .null, .scalar (.i32 index), .object (.address tagged)] memory = - .error ⟨put, 4, .thrown "System.NullReferenceException"⟩ := by + .error ⟨put, 4, .thrown .nullReference⟩ := by cil_checked_steps [readable.load_forms forms] #print axioms null_array diff --git a/Examples/Proofs/TallyTwice.lean b/Examples/Proofs/TallyTwice.lean index 61bb5566a..f387c4367 100644 --- a/Examples/Proofs/TallyTwice.lean +++ b/Examples/Proofs/TallyTwice.lean @@ -43,7 +43,7 @@ private abbrev State (memory : Memory) (home : Reference) := /-- The loop test, with the input and `count` on the stack. -/ bind_cutpoint header (memory : Memory) (value : W32) (home : Reference) (state : State memory home) := - tallyBody at loop.latch matching (fun op => op matches .bne _) with entry.frame memory home, + tallyBody at loop.latch matching (fun op => op matches .bne_un _) with entry.frame memory home, [.scalar (.i32 value), .scalar (.i32 state.values.1)], state.memory /-- `TallyTwice` returns twice its input and allocates nothing the caller sees. -/ diff --git a/Examples/Proofs/TilePairCount.lean b/Examples/Proofs/TilePairCount.lean index 8c2b7a317..f5a1fe4d3 100644 --- a/Examples/Proofs/TilePairCount.lean +++ b/Examples/Proofs/TilePairCount.lean @@ -35,9 +35,9 @@ theorem null_pairs (memory : Memory) (tiles : Reference) (readable : Tiles.Pairs.Readable memory tiles) (held : Tiles.Pairs.of memory tiles = .null) : ∃ fuel, invoke Extracted.program fuel pairCount [.object (.address tiles)] memory = - .error ⟨pairCount, 2, .thrown "System.NullReferenceException"⟩ := by + .error ⟨pairCount, 2, .thrown .nullReference⟩ := by cil_checked_steps [readable.load_null held rfl] #print axioms null_pairs -end ExampleProof \ No newline at end of file +end ExampleProof diff --git a/Examples/Proofs/UnsafeReference.lean b/Examples/Proofs/UnsafeReference.lean index ee1a5e5e7..027808dfa 100644 --- a/Examples/Proofs/UnsafeReference.lean +++ b/Examples/Proofs/UnsafeReference.lean @@ -8,11 +8,15 @@ open CIL.Safety /-- Reinterpreting a live reference preserves the complete memory, including initialization flags, access views, allocation identities and lifetimes. -/ -theorem reinterpret_preserves (body : CIL.Method) (pc : Nat) (args : List Value) +theorem reinterpret_preserves (body : CIL.Method) (method : CIL.Extraction.MethodRef) + (pc : Nat) (args : List Value) (frame : Frame) (memory : Memory) (reference : Reference) (rest : List Value) + (unlinked : body.linkAt pc = none) + (model : CIL.Extraction.runtimeModel body.environment .call method = some (.memory .asRef)) (formed : form memory reference = .ok reference) : - step body (.memory .asRef) pc args frame (.reference (.address reference) :: rest) memory = + step body (.call method {}) pc args frame (.reference (.address reference) :: rest) memory = .ok (.next (pc + 1) (.reference (.address reference) :: rest) frame memory) := by + rw [step_call_model (link := unlinked) (found := model) (direct := rfl)] cil_checked_step [formed] /-- The compiled cast preserves arbitrary caller memory and the precise reference. -/ diff --git a/Examples/Proofs/VectorSummary.lean b/Examples/Proofs/VectorSummary.lean new file mode 100644 index 000000000..a70e71c00 --- /dev/null +++ b/Examples/Proofs/VectorSummary.lean @@ -0,0 +1,76 @@ +import Extracted +import CIL +import CIL.MethodIdentity + +/-! +# A vector callee through a decoded summary + +`Examples.DivisionIntrinsics::UnsafeLowNext` returns `UnsafeLow` of its argument plus one. +`UnsafeLow` puts its argument in the low lane of a `Vector128` with `CreateScalarUnsafe` and +reads that lane back with `GetElement`, two calls the extraction runs through their runtime +models. `correctness_stepped` steps the caller and `UnsafeLow` instruction by instruction. +`correctness` proves the same statement from a `DecodedSummary` of `UnsafeLow`: its contract, +bound to the record whose pinned bytes decode to it, applied at the call without stepping its body. +-/ + +namespace ExampleProof +open CIL CIL.Safety CIL.Extraction + +bind_method low lowBody := "System.UInt64 Examples.DivisionIntrinsics::UnsafeLow(System.UInt64)" + +/-- The caller returns its argument plus one and keeps the caller memory, stepping `UnsafeLow`. -/ +theorem correctness_stepped (memory : Safety.Memory) (x : W64) : + ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory = + .ok (memory, [.scalar (.i64 (x + 1))]) := by + cil_checked_steps +#print axioms correctness_stepped + +/-- `UnsafeLow` returns its argument and changes no memory. -/ +def lowContract : MethodContract W64 W64 where + args x := [.scalar (.i64 x)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post x initial final value := value = x ∧ final = initial + +/-- `UnsafeLow` meets its contract. -/ +theorem low_holds : lowContract.Holds Extracted.program low := by + rintro x memory - + refine MethodContract.Holds.intro memory x ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + cil_checked_steps +#print axioms low_holds + +/-- Every record's pinned bytes decode to the method at its index of the extraction. -/ +theorem bodies_decode : + decodedProgram? Extracted.profile Extracted.environment Extracted.methodEvidence = + some Extracted.program := + decodedProgram?_eq_some Extracted.translation_valid.programBindingsValid + (.cons ((Method.ofBody?_of_decodes Extracted.method0_body_validated).trans + Extracted.method0_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method1_body_validated).trans + Extracted.method1_decoded) .nil)) +#print axioms bodies_decode + +/-- The contract, bound to the record of `UnsafeLow` and the bytes it decodes from. -/ +theorem low_summary : ∃ record, Extracted.methodEvidence[low]? = some record ∧ + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment low record.assembly record.original.token record.arguments + lowContract := + ⟨_, rfl, .of_translation Extracted.translation_valid ⟨low, by decide⟩ + (method := Extracted.method1) rfl low_holds⟩ +#print axioms low_summary + +/-- The caller returns its argument plus one and keeps the caller memory, applying the summary + at the call. -/ +theorem correctness (memory : Safety.Memory) (x : W64) : + ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory = + .ok (memory, [.scalar (.i64 (x + 1))]) := by + obtain ⟨record, -, summary⟩ := low_summary + refine (WP.exact_iff (execution := Execution fun fuel => + invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory)).mp ?_ + cil_contract_steps [summary.holds, lowContract] with middle value lowered + obtain ⟨rfl, rfl⟩ := lowered.post + rfl +#print axioms correctness + +end ExampleProof diff --git a/Examples/Proofs/WrongAdd.lean b/Examples/Proofs/WrongAdd.lean index 148bae9a9..5f5fda915 100644 --- a/Examples/Proofs/WrongAdd.lean +++ b/Examples/Proofs/WrongAdd.lean @@ -1,22 +1,26 @@ import Extracted -import CIL.ExecutionLemmas -import CIL.Safety.FuelLemmas -open CIL +import CIL.Safety.MethodContract + +/-! A counterexample to the addition contract for the subtracting `WrongAdd`: at `(0, 1)` it + returns the wrapped difference, not the sum. -/ + +open CIL.Safety namespace ExampleProof -theorem correctness (fuel : Nat) (memory : CIL.Safety.Memory) : - CIL.Safety.invoke Extracted.program fuel 0 [.scalar (.i64 1), .scalar (.i64 1)] - Safety.Memory.empty ≠ - .ok (memory, [.scalar (.i64 2)]) := by - intro alleged - have observed : CIL.Safety.invoke Extracted.program 4 0 - [.scalar (.i64 1), .scalar (.i64 1)] Safety.Memory.empty = - .ok (Safety.Memory.empty, [.scalar (.i64 0)]) := rfl - have first := CIL.Safety.invoke_mono _ 4 fuel _ _ _ (.ok _) trivial observed - have second := CIL.Safety.invoke_mono _ fuel 4 _ _ _ (.ok _) trivial alleged - rw [Nat.add_comm] at second - rw [first] at second - simp at second +/-- The addition contract `Add` meets: every pair of words returns their wrapping sum and leaves + memory unchanged. -/ +def addContract : MethodContract (CIL.W64 × CIL.W64) where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + pre _ _ := True + footprint _ := [] + post input initial final values := + final = initial ∧ values = [.scalar (.i64 (input.1 + input.2))] + +/-- The extracted method returns `0 - 1` for `(0, 1)`, which is not the sum `1`. -/ +theorem refuted : addContract.Refuted Extracted.program 0 := + .of_return (input := (0, 1)) (initial := Memory.empty) trivial + (rfl : invoke Extracted.program 16 0 [.scalar (.i64 0), .scalar (.i64 1)] Memory.empty = .ok _) + (by rintro ⟨-, out, rfl, -, wrong⟩; revert wrong; decide) -#print axioms correctness +#print axioms refuted end ExampleProof diff --git a/Examples/Verify.cs b/Examples/Verify.cs index 2cffa67e7..2bbbf8f1e 100644 --- a/Examples/Verify.cs +++ b/Examples/Verify.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // Build the sample, extract its CIL and prove the addition contract. Run(["dotnet", "build", "Examples/Assembly", "-c", "Release"]); diff --git a/Extractor/ArgumentGuards.cs b/Extractor/ArgumentGuards.cs index b8a683f80..af0de3295 100644 --- a/Extractor/ArgumentGuards.cs +++ b/Extractor/ArgumentGuards.cs @@ -2,57 +2,46 @@ namespace Nethermind.LeanCIL; -// The System.Runtime argument guards modeled as checked runtime calls; CIL.Extraction.guardCallMatches -// rechecks each emission against the raw call. The parameter name is an opaque argument. +// The parameter name is an opaque argument to each checked guard. internal static class ArgumentGuards { private const string OutOfRange = "System.ArgumentOutOfRangeException"; private const string NullGuard = "System.Void System.ArgumentNullException::ThrowIfNull(System.Object,System.String)"; - private static readonly (string Type, int Width, bool Signed)[] Integers = - [ - ("System.SByte", 8, true), ("System.Byte", 8, false), - ("System.Int16", 16, true), ("System.UInt16", 16, false), - ("System.Int32", 32, true), ("System.UInt32", 32, false), - ("System.Int64", 64, true), ("System.UInt64", 64, false), - ("System.IntPtr", 64, true), ("System.UIntPtr", 64, false) - ]; - - private static readonly (string Method, string Guard)[] ZeroGuards = - [("ThrowIfZero", "zero"), ("ThrowIfNegative", "negative"), ("ThrowIfNegativeOrZero", "negativeOrZero")]; - - private static readonly (string Method, string Guard)[] ComparingGuards = - [ - ("ThrowIfEqual", "equal"), ("ThrowIfNotEqual", "notEqual"), ("ThrowIfGreaterThan", "greaterThan"), - ("ThrowIfGreaterThanOrEqual", "greaterThanOrEqual"), ("ThrowIfLessThan", "lessThan"), - ("ThrowIfLessThanOrEqual", "lessThanOrEqual") - ]; - - private static readonly Dictionary Operations = Build(); + private static readonly HashSet Signatures = BuildSignatures(); // Exact generic instances only: another type argument or overload is not a modeled guard. - internal static string? Translate(string signature) => Operations.GetValueOrDefault(signature); + internal static Operation? CallModel(MethodReference reference, string architecture) + { + if (!Signatures.Contains(reference.FullName)) return null; + if (!RuntimeModels.ExactScope(reference.DeclaringType, PlatformAssemblies.SystemRuntime) || + reference.HasThis || reference.ExplicitThis) return null; + var generic = reference is GenericInstanceMethod { GenericArguments.Count: 1 } instance && + instance.ElementMethod.GenericParameters.Count == 1; + var numeric = reference.DeclaringType.FullName == OutOfRange; + var convention = numeric ? MethodCallingConvention.Generic : MethodCallingConvention.Default; + if (generic != numeric || reference.CallingConvention != convention) return null; + RuntimeModels.ValidateSignature(reference, architecture); + return Unoutlined.Site; + } // The modeled ThrowIfNull, whose object argument must be a class instance or null on the stack. internal static bool IsNullGuard(MethodReference callee) => callee.FullName == NullGuard && callee.DeclaringType.Scope.Name == "System.Runtime"; - private static Dictionary Build() + private static HashSet BuildSignatures() { - var operations = new Dictionary(StringComparer.Ordinal) - { - [NullGuard] = ".memory .throwIfNull" - }; - foreach (var (type, width, signed) in Integers) + var signatures = new HashSet(StringComparer.Ordinal) { NullGuard }; + string[] unaryGuards = ["ThrowIfZero", "ThrowIfNegative", "ThrowIfNegativeOrZero"]; + string[] binaryGuards = ["ThrowIfEqual", "ThrowIfNotEqual", "ThrowIfGreaterThan", + "ThrowIfGreaterThanOrEqual", "ThrowIfLessThan", "ThrowIfLessThanOrEqual"]; + foreach (var (type, _, _) in RuntimeModels.MathIntegers) { - var bits = $"{width} {(signed ? "true" : "false")}"; - foreach (var (method, guard) in ZeroGuards) - operations.Add($"System.Void {OutOfRange}::{method}<{type}>(!!0,System.String)", - $".memory (.zeroGuard .{guard} {bits})"); - foreach (var (method, guard) in ComparingGuards) - operations.Add($"System.Void {OutOfRange}::{method}<{type}>(!!0,!!0,System.String)", - $".memory (.comparisonGuard .{guard} {bits})"); + foreach (var method in unaryGuards) + signatures.Add($"System.Void {OutOfRange}::{method}<{type}>(!!0,System.String)"); + foreach (var method in binaryGuards) + signatures.Add($"System.Void {OutOfRange}::{method}<{type}>(!!0,!!0,System.String)"); } - return operations; + return signatures; } } diff --git a/Extractor/Arrays.cs b/Extractor/Arrays.cs index 71e36b886..1ad896d74 100644 --- a/Extractor/Arrays.cs +++ b/Extractor/Arrays.cs @@ -1,16 +1,19 @@ using Mono.Cecil; using Mono.Cecil.Cil; -using System.Text.Json; namespace Nethermind.LeanCIL; // Array identity and physical element shape remain distinct throughout extraction. internal static class Arrays { - internal sealed record Element(string Lean, int Width, bool Signed, NativeOperands.Kind Kind) + /// + /// An array element shape. marks a vector, aggregate or enumeration element, whose + /// Lean term takes arguments and so is parenthesized where it is an argument itself. + /// + internal sealed record Element(string Lean, int Width, bool Signed, NativeOperands.Kind Kind, + bool Compound = false) { - internal string Expression => Lean.Contains(' ', StringComparison.Ordinal) - ? $"(.{Lean})" : $".{Lean}"; + internal string Expression => Compound ? $"(.{Lean})" : $".{Lean}"; } static readonly Dictionary elements = new(StringComparer.Ordinal) @@ -37,14 +40,14 @@ internal sealed record Element(string Lean, int Width, bool Signed, NativeOperan internal static Element? Storage(TypeReference type, string? architecture = null) { if (MetadataValidation.IntegerVectorWidth(type) is { } vectorWidth) - return new($"vector {JsonSerializer.Serialize(type.FullName)} {vectorWidth}", - vectorWidth, false, NativeOperands.Kind.ArrayComposite); + return new($"vector {IdentityText.Quote(type.FullName)} {vectorWidth}", + vectorWidth, false, NativeOperands.Kind.ArrayComposite, Compound: true); if (MetadataValidation.IsAggregate(type, type.Module)) { if (MetadataValidation.IsByRefLike(type)) return null; var width = architecture is null ? 0 : Spans.ElementWidth(type, architecture)!.Value; - return new($"aggregate {JsonSerializer.Serialize(LayoutEvidence.Identity(type))} {width}", - width, false, NativeOperands.Kind.ArrayComposite); + return new($"aggregate {IdentityText.Quote(LayoutEvidence.Identity(type))} {width}", + width, false, NativeOperands.Kind.ArrayComposite, Compound: true); } if (!EnumTypes.IsEnum(type)) return Primitive(type); return EnumTypes.Cached(type, EnumStorage); @@ -53,55 +56,129 @@ internal sealed record Element(string Lean, int Width, bool Signed, NativeOperan static Element EnumStorage(TypeReference type) { var underlying = Primitive(EnumTypes.Storage(type))!; - var identity = JsonSerializer.Serialize(LayoutEvidence.Identity(type)); - return underlying with { Lean = $"enumeration {identity} .{underlying.Lean}" }; + var identity = IdentityText.Quote(LayoutEvidence.Identity(type)); + return underlying with { Lean = $"enumeration {identity} .{underlying.Lean}", Compound = true }; + } + + /// Array.Fill accepts primitive and enum storage with an optional int32 range. + internal static Operation? Fill(MethodReference method, string architecture) + { + if (method.Name != "Fill" || method.DeclaringType.FullName != "System.Array" || method.HasThis || + method.ExplicitThis || method.CallingConvention != MethodCallingConvention.Generic || + method is not GenericInstanceMethod { GenericArguments.Count: 1 } generic || + generic.ElementMethod.GenericParameters.Count != 1 || method.ReturnType.MetadataType != MetadataType.Void || + !RuntimeModels.ExactScope(method.DeclaringType, PlatformAssemblies.SystemRuntime)) return null; + + var parameters = method.Parameters; + var counted = parameters.Count == 4; + if (parameters.Count is not (2 or 4) || + parameters[0].ParameterType is not ArrayType { IsVector: true } array || + !MethodElement(array.ElementType) || !MethodElement(parameters[1].ParameterType) || + counted && (parameters[2].ParameterType.MetadataType != MetadataType.Int32 || + parameters[3].ParameterType.MetadataType != MetadataType.Int32)) return null; + + var element = generic.GenericArguments[0]; + if (Primitive(element) is null && !EnumTypes.IsEnum(element)) return null; + RuntimeModels.ValidateSignature(method, architecture); + return Storage(element, architecture) is null ? null : Unoutlined.Site; + + static bool MethodElement(TypeReference type) => + type is GenericParameter { Type: GenericParameterType.Method, Position: 0 }; } internal static Element? Of(TypeReference type, string? architecture = null) => type is ArrayType { IsVector: true } array ? Storage(array.ElementType, architecture) : null; - internal static string? StackKind(NativeOperands.Kind kind) => + internal static LocalKind.Array? StackKind(NativeOperands.Kind kind) => elements.Values.FirstOrDefault(element => element.Kind == kind) is { } element - ? $".array .{element.Lean}" : null; + ? new LocalKind.Array(element) : null; - internal static string? Translate(Instruction instruction, string architecture) => instruction.OpCode.Code switch + internal static Operation? Translate(Instruction instruction, string architecture) { - Code.Newarr when Storage((TypeReference)instruction.Operand, architecture) is - { Width: > 0 and <= ushort.MaxValue } element => $".newArray {element.Expression}", - Code.Ldlen => ".memory .arrayLength", - Code.Ldelema => $".memory (.arrayAddress {(Storage((TypeReference)instruction.Operand, architecture) ?? - throw new InvalidDataException("Unsupported array element token: " + instruction.Operand)).Expression}" + - (instruction.Previous?.OpCode.Code == Code.Readonly ? " false)" : ")"), - Code.Ldelem_I1 => ".memory (.arrayLoad 1 true)", - Code.Ldelem_U1 => ".memory (.arrayLoad 1 false)", - Code.Ldelem_I2 => ".memory (.arrayLoad 2 true)", - Code.Ldelem_U2 => ".memory (.arrayLoad 2 false)", - Code.Ldelem_I4 or Code.Ldelem_U4 => ".memory (.arrayLoad 4 false)", - Code.Ldelem_I8 or Code.Ldelem_I => ".memory (.arrayLoad 8 false)", - Code.Stelem_I1 => ".memory (.arrayStore 1)", - Code.Stelem_I2 => ".memory (.arrayStore 2)", - Code.Stelem_I4 => ".memory (.arrayStore 4)", - Code.Stelem_I8 or Code.Stelem_I => ".memory (.arrayStore 8)", - Code.Ldelem_Any => Typed(instruction, false, architecture), - Code.Stelem_Any => Typed(instruction, true, architecture), - _ => null - }; + if (instruction.OpCode.Code is Code.Ldelema or Code.Ldelem_Any or Code.Stelem_Any) + { + ElementStorage(instruction, architecture); + return instruction.OpCode.Code == Code.Ldelema + ? Unoutlined.Site : new StackOperation(instruction.OpCode.Code); + } + return instruction.OpCode.Code switch + { + Code.Newarr when Storage((TypeReference)instruction.Operand, architecture) is + { Width: > 0 and <= ushort.MaxValue } => Unoutlined.Site, + Code.Ldlen => new StackOperation(instruction.OpCode.Code), + _ when NumericElement(instruction.OpCode.Code).HasValue => new StackOperation(instruction.OpCode.Code), + _ => null + }; + } - internal static void ValidateReadOnly(InstructionIndex instructions, Instruction prefix) + /// + /// A numeric element load or store: its width, whether a load sign-extends, and the step lemma a proof outline + /// cites for it. is the opcode's suffix, or null for a typed access naming its element + /// by token, which steps from , the shape fact of its element type. + /// + internal sealed record NumericAccess(int Width, bool Signed, bool Store, NumericKind? Kind, string? Shape = null) { - if (prefix.Next is not { OpCode.Code: Code.Ldelema } address || prefix.Operand is not null) - throw new InvalidDataException("Readonly prefix requires an immediate array element address"); - if (instructions.IsTarget(address)) - throw new InvalidDataException("Branch enters an array address after its readonly prefix"); + /// The lemma stepping a load of an element whose address and load succeed. + internal string LoadLemma => + Kind is { } kind ? $"step_ldelem_of_element {kind.Term()}" : $"step_ldelem_any_of_element {Typed} rfl"; + + /// The lemma stepping a store into an element whose write succeeds. + internal string StoreLemma => (Kind, Width) switch + { + ({ } kind, 8) => $"step_stelem_wide_of_write {kind.Term()}", + ({ } kind, _) => $"step_stelem_narrow_of_write {kind.Term()}", + (null, 8) => $"step_stelem_any_of_wide_write {Typed} rfl", + _ => $"step_stelem_any_of_write {Typed} rfl" + }; + + string Typed => Shape ?? throw new InvalidOperationException("A typed element access has no shape fact"); } - static string Typed(Instruction instruction, bool store, string architecture) + /// The integer kind and access direction encoded by a numeric array opcode. + static (NumericKind Kind, bool Store)? NumericElement(Code code) => code switch { - var element = Storage((TypeReference)instruction.Operand, architecture) ?? - throw new InvalidDataException("Unsupported array element token: " + instruction.Operand); - if (element.Kind == NativeOperands.Kind.ArrayComposite) - return $".memory (.arrayTransfer {element.Expression} {store.ToString().ToLowerInvariant()})"; - return store ? $".memory (.arrayStore {element.Width})" : - $".memory (.arrayLoad {element.Width} {element.Signed.ToString().ToLowerInvariant()})"; + Code.Ldelem_I1 => (NumericKind.I1, false), + Code.Ldelem_U1 => (NumericKind.U1, false), + Code.Ldelem_I2 => (NumericKind.I2, false), + Code.Ldelem_U2 => (NumericKind.U2, false), + Code.Ldelem_I4 => (NumericKind.I4, false), + Code.Ldelem_U4 => (NumericKind.U4, false), + Code.Ldelem_I8 => (NumericKind.I8, false), + Code.Ldelem_I => (NumericKind.I, false), + Code.Stelem_I1 => (NumericKind.I1, true), + Code.Stelem_I2 => (NumericKind.I2, true), + Code.Stelem_I4 => (NumericKind.I4, true), + Code.Stelem_I8 => (NumericKind.I8, true), + Code.Stelem_I => (NumericKind.I, true), + _ => null + }; + + /// A numeric access uses the method's checked shape for its closed element type. + internal static NumericAccess? Numeric(Operation op, MethodDefinition method, int pc, int methodIndex = 0) => + op switch + { + StackOperation raw when NumericElement(raw.Kind) is { } access => + new(access.Kind.Width(), access.Kind.Signed(), access.Store, access.Kind), + StackOperation { Kind: Code.Ldelem_Any or Code.Stelem_Any } when + method.Body.Instructions[pc] is { Operand: TypeReference element } site && + Storage(element) is { Kind: not NativeOperands.Kind.ArrayComposite } storage => + new(storage.Width, storage.Signed, site.OpCode.Code == Code.Stelem_Any, null, + $"(cil_element_shape% Extracted.method{methodIndex} {pc})"), + _ => null + }; + + internal static Element ElementStorage(Instruction instruction, string architecture) + { + var element = (TypeReference)instruction.Operand; + if (ObjectTypes.Candidate(element) || ReferenceArrays.Element(element)) + { + var operand = $"{instruction.OpCode.Name} {element.FullName}"; + throw new InvalidDataException($""" + Unsupported typed element access on an array of class references: {operand} + Element addresses would expose a reference slot; only ldelem.ref and stelem.ref are modelled. + """.ReplaceLineEndings("\n")); + } + return Storage(element, architecture) ?? + throw new InvalidDataException($"Unsupported array element token: {element}"); } } diff --git a/Extractor/BinaryModels.cs b/Extractor/BinaryModels.cs new file mode 100644 index 000000000..fe0fcb119 --- /dev/null +++ b/Extractor/BinaryModels.cs @@ -0,0 +1,41 @@ +using Mono.Cecil; + +namespace Nethermind.LeanCIL; + +// The checked runtime catalog supplies the semantics of each admitted binary primitive. +internal static class BinaryModels +{ + private static readonly HashSet Signatures = BuildSignatures(); + + internal static Operation? CallModel(MethodReference reference, string architecture) + { + if (reference.DeclaringType.FullName != "System.Buffers.Binary.BinaryPrimitives" || + !RuntimeModels.ExactScope(reference.DeclaringType, PlatformAssemblies.SystemMemory) || + reference.HasThis || reference.ExplicitThis || reference.HasGenericParameters || + reference is GenericInstanceMethod || reference.CallingConvention != MethodCallingConvention.Default) + return null; + if (!Signatures.Contains(reference.FullName)) return null; + RuntimeModels.ValidateSignature(reference, architecture); + return Unoutlined.Site; + } + + private static HashSet BuildSignatures() + { + var signatures = new HashSet(StringComparer.Ordinal); + foreach (var type in new[] { "Int16", "UInt16", "Int32", "UInt32", "Int64", "UInt64", "IntPtr", "UIntPtr" }) + foreach (var order in new[] { "BigEndian", "LittleEndian" }) + foreach (var read in new[] { false, true }) + foreach (var trying in new[] { false, true }) + { + var prefix = trying ? "Try" : ""; + var action = read ? "Read" : "Write"; + var name = $"{prefix}{action}{type}{order}"; + var result = trying ? "System.Boolean" : read ? $"System.{type}" : "System.Void"; + var span = read ? "System.ReadOnlySpan`1" : "System.Span`1"; + var suffix = read ? "&" : ""; + var parameters = read && !trying ? span : $"{span},System.{type}{suffix}"; + signatures.Add($"{result} System.Buffers.Binary.BinaryPrimitives::{name}({parameters})"); + } + return signatures; + } +} diff --git a/Extractor/BinaryTransfers.cs b/Extractor/BinaryTransfers.cs deleted file mode 100644 index bff105d3c..000000000 --- a/Extractor/BinaryTransfers.cs +++ /dev/null @@ -1,46 +0,0 @@ -using Mono.Cecil; - -namespace Nethermind.LeanCIL; - -// Exact fixed-width overloads share checked transfers and the scalar byte-reversal model. -internal static class BinaryTransfers -{ - private static readonly Dictionary Operations = BuildOperations(); - - internal static string? Translate(MethodReference reference) - { - if (reference.DeclaringType.FullName != "System.Buffers.Binary.BinaryPrimitives" || - !RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Memory") || - reference.HasThis || reference.ExplicitThis || reference.HasGenericParameters || - reference is GenericInstanceMethod || reference.CallingConvention != MethodCallingConvention.Default) - return null; - return Operations.GetValueOrDefault(reference.FullName); - } - - private static Dictionary BuildOperations() - { - var operations = new Dictionary(StringComparer.Ordinal); - foreach (var (type, width, signed) in new[] - { - ("Int16", 2, true), ("UInt16", 2, false), ("Int32", 4, true), ("UInt32", 4, false), - ("Int64", 8, true), ("UInt64", 8, false), ("IntPtr", 8, true), ("UIntPtr", 8, false) - }) - foreach (var bigEndian in new[] { false, true }) - foreach (var read in new[] { false, true }) - foreach (var trying in new[] { false, true }) - { - var name = (trying ? "Try" : "") + (read ? "Read" : "Write") + type + - (bigEndian ? "BigEndian" : "LittleEndian"); - var result = trying ? "System.Boolean" : read ? "System." + type : "System.Void"; - var span = (read ? "System.ReadOnlySpan`1" : "System.Span`1") + ""; - var parameters = span + (read && !trying ? "" : ",System." + type + (read ? "&" : "")); - var operation = read - ? $".binaryRead {width} {signed.ToString().ToLowerInvariant()}" - : $".binaryWrite {width}"; - operations.Add($"{result} System.Buffers.Binary.BinaryPrimitives::{name}({parameters})", - $".memory ({operation} {bigEndian.ToString().ToLowerInvariant()} " + - $"{trying.ToString().ToLowerInvariant()})"); - } - return operations; - } -} diff --git a/Extractor/BitModels.cs b/Extractor/BitModels.cs new file mode 100644 index 000000000..97b976a64 --- /dev/null +++ b/Extractor/BitModels.cs @@ -0,0 +1,59 @@ +using Mono.Cecil; + +namespace Nethermind.LeanCIL; + +/// The exact integer bit and byte-order calls admitted by CIL/Extraction/BitModels.lean. +internal static class BitModels +{ + const string BinaryPrimitives = "System.Buffers.Binary.BinaryPrimitives"; + static readonly string[] Counts = ["PopCount", "LeadingZeroCount", "TrailingZeroCount", "Log2"]; + static readonly HashSet Calls = Build(); + + /// Whether the static bit helper binds its platform declaration and exact signature. + internal static bool Admits(MethodReference reference, FeatureProfile profile) + { + if (reference.HasThis || reference.ExplicitThis || reference.HasGenericParameters || + reference is GenericInstanceMethod || reference.CallingConvention != MethodCallingConvention.Default || + !Calls.Contains(reference.FullName)) return false; + var scope = reference.DeclaringType.FullName == BinaryPrimitives + ? PlatformAssemblies.SystemMemory : PlatformAssemblies.SystemRuntime; + if (!RuntimeModels.ExactScope(reference.DeclaringType, scope)) return false; + RuntimeModels.ValidateSignature(reference, profile.Architecture); + return true; + } + + /// The stack width of a bit count's declared result, including native integers. + internal static int? CountWidth(MethodReference reference, FeatureProfile profile) + { + if (!Counts.Contains(reference.Name) || !Admits(reference, profile)) return null; + return reference.ReturnType.MetadataType is MetadataType.Int64 or MetadataType.UInt64 or + MetadataType.IntPtr or MetadataType.UIntPtr ? 64 : 32; + } + + // Signed Log2 can throw on a negative input. Signed narrow rotations have no model. + static HashSet Build() + { + var calls = new HashSet(StringComparer.Ordinal); + const string Bits = "System.Numerics.BitOperations"; + foreach (var (type, width, signed) in RuntimeModels.MathIntegers) + { + var wide = width is 32 or 64; + calls.Add($"System.Boolean {type}::IsPow2({type})"); + if (wide) calls.Add($"System.Boolean {Bits}::IsPow2({type})"); + calls.Add($"{type} {BinaryPrimitives}::ReverseEndianness({type})"); + foreach (var name in Counts) + { + if (name == "Log2" && signed) continue; + calls.Add($"{type} {type}::{name}({type})"); + if (wide && !signed) calls.Add($"System.Int32 {Bits}::{name}({type})"); + } + if (wide && !signed) calls.Add($"{type} {Bits}::RoundUpToPowerOf2({type})"); + foreach (var name in new[] { "RotateLeft", "RotateRight" }) + { + if (wide || !signed) calls.Add($"{type} {type}::{name}({type},System.Int32)"); + if (wide && !signed) calls.Add($"{type} {Bits}::{name}({type},System.Int32)"); + } + } + return calls; + } +} diff --git a/Extractor/Blockers.cs b/Extractor/Blockers.cs index 1eceae730..a7ef9f220 100644 --- a/Extractor/Blockers.cs +++ b/Extractor/Blockers.cs @@ -1,4 +1,3 @@ -using System.Security.Cryptography; using System.Text; using System.Text.Encodings.Web; using System.Text.Json; @@ -8,7 +7,7 @@ namespace Nethermind.LeanCIL; // Runs extraction and its validators over explicit method/profile pairs; it never checks a proof. -internal static class Blockers +internal static partial class Blockers { const string Usage = "Usage: leancil blockers assembly.dll --manifest manifest.json [--output blockers.json] " + "[--assembly 'assembly name or full identity'] [--reference Lib.dll]... [--import Lib.leancil.contracts]... " + @@ -21,7 +20,9 @@ static readonly (string Key, string Option)[] ImportKeys = ]; const string Disclaimer = "Admission only: admitted entries are extractable under their profile; nothing here is proved."; - sealed record Site(string Method, int? IlOffset, string? Instruction, string? Source, string Message); + /// One occurrence of a blocker: the method and instruction it names, its source line and its + /// diagnostic. + internal sealed record Site(string Method, int? IlOffset, string? Instruction, string? Source, string Message); sealed class ChainComparer : IEqualityComparer { @@ -74,10 +75,11 @@ sealed record Outcome(string? Signature, string? Profile, string Status, string? int? ReachableMethods, List Findings) { internal string[] TypeArguments { get; init; } = []; + // Names, not definitions: a definition holds its extraction's whole module alive across every later entry. + /// The full names of the extracted methods, each defined from its decoder. + internal string[] DecodedMethods { get; init; } = []; } - static string Hash(byte[] bytes) => Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant(); - static string Option(string[] args, ref int index) { var name = args[index]; @@ -138,15 +140,19 @@ internal static int Run(string[] args) var initializedTypes = root.TryGetProperty("initializedTypes", out var types) ? new SortedSet(types.Deserialize() ?? [], StringComparer.Ordinal) : new SortedSet(StringComparer.Ordinal); - var assembly = File.ReadAllBytes(assemblyPath); - VectorSignatures.Validate(assembly); + // Each admitted entry is extracted again; the batch reads and hashes the image, PDB and extractor once. + var batch = new Program.ExtractionBatch(); + var image = batch.ReadAssembly(assemblyPath); + image.ValidateSignatures(); + var assembly = image.Bytes; string identity, symbols; Guid mvid; using (var probe = ModuleDefinition.ReadModule(new MemoryStream(assembly))) { identity = probe.Assembly?.Name.FullName ?? throw new InvalidDataException("Input must contain assembly metadata."); mvid = probe.Mvid; - symbols = Symbols.Read(probe, assemblyPath, out _) is { } origin ? origin == "embedded" ? "embedded portable PDB" : "portable PDB " + origin : "none"; + symbols = Symbols.Read(probe, assemblyPath, out _, batch.ReadPdb) is { } origin + ? origin == "embedded" ? "embedded portable PDB" : "portable PDB " + origin : "none"; } var findings = new Dictionary(StringComparer.Ordinal); @@ -154,10 +160,10 @@ internal static int Run(string[] args) // One module serves every entry, so per-method analyses are computed once; each entry's // operand rewrites are undone before the next entry is validated. var references = referencePaths.Distinct(StringComparer.Ordinal).ToArray(); - using var pinned = new PinnedAssemblies(assembly, Hash(assembly), - references.Select(path => new PinnedAssemblies.Source(path))); + using var pinned = new PinnedAssemblies(image, + references.Select(path => new PinnedAssemblies.Source(path)), batch.ReadAssembly); var module = pinned.Selected; - Symbols.Read(module, assemblyPath, out _); + Symbols.Read(module, assemblyPath, out _, batch.ReadPdb); foreach (var entry in entries.EnumerateArray()) { var signature = entry.ValueKind == JsonValueKind.Object ? Text(entry, "signature") : null; @@ -224,27 +230,25 @@ internal static int Run(string[] args) rewrites[index].Instruction.Operand = rewrites[index].Operand; } // `admitted` means exactly that `extract` succeeds, so an entry without blockers is extracted. - if (blocked.Count == 0 && Extract(assemblyPath, signature, profileName, manifestDirectory, expectedAssembly, - typeArguments, initializedTypes, references, imports) is { } rejected) - blocked.Add(new(methods[0], null, rejected, [methods[0]])); + Program.MethodDefinitions? definitions = null; + if (blocked.Count == 0 && Extract(batch, assemblyPath, signature, profileName, manifestDirectory, + expectedAssembly, typeArguments, initializedTypes, references, imports, + extracted => definitions = extracted) is { } rejected) + blocked.Add(Rejection(methods[0], rejected)); if (blocked.Count == 0) { + string[] decoded = [.. definitions!.Methods.Select(method => method.FullName)]; outcomes.Add(new(signature, profileName, "admitted", methods[0].FullName, null, methods.Length, []) - { TypeArguments = typeArguments }); + { TypeArguments = typeArguments, DecodedMethods = decoded }); continue; } var entryFindings = new List(); var knownFindings = new HashSet(); foreach (var failure in blocked) { - var message = failure.Error.Message; - int? offset = failure.At?.Offset ?? (Regex.Match(message, @"\bIL_([0-9a-f]{4,})\b") is { Success: true } label - ? Convert.ToInt32(label.Groups[1].Value, 16) : null); - // The construct is the diagnostic without its site, so every occurrence shares one blocker. - var construct = Regex.Replace(message.Split('\n')[0], @"\bIL_[0-9a-f]{4,}: ", ""); + var construct = Construct(failure.Error.Message); if (!findings.TryGetValue(construct, out var finding)) findings[construct] = finding = new(construct); - var site = new Site(failure.Method.FullName, offset, failure.At?.ToString(), - offset is int at ? Symbols.Location(failure.Method, at, Path.GetFileName) : null, message); + var site = SiteOf(failure); var chain = failure.Chain.Select(method => method.FullName).ToArray(); finding.Add(site, chain[0], profileName, chain); if (knownFindings.Add(finding)) entryFindings.Add(finding); @@ -261,6 +265,7 @@ internal static int Run(string[] args) .ToDictionary(pair => pair.finding, pair => pair.index); foreach (var outcome in outcomes) outcome.Findings.Sort((left, right) => ranks[left].CompareTo(ranks[right])); + batch.RequireUnchanged(); var report = JsonSerializer.Serialize(new { schema = 1, @@ -268,8 +273,8 @@ internal static int Run(string[] args) assemblyPath = Path.GetFullPath(assemblyPath), assemblyIdentity = identity, mvid, - assemblySha256 = Hash(assembly), - extractorSha256 = Hash(File.ReadAllBytes(typeof(LeanEmitter).Assembly.Location)), + assemblySha256 = image.Hash, + extractorSha256 = batch.ExtractorHash, symbols, initializedTypes, counts = new @@ -282,7 +287,9 @@ internal static int Run(string[] args) { signature = o.Signature, typeArguments = o.TypeArguments, profile = o.Profile, status = o.Status, method = o.Method, reason = o.Reason, - reachableMethods = o.ReachableMethods, blockers = o.Findings.Select(f => f.Id) + reachableMethods = o.ReachableMethods, + decodedMethods = o.DecodedMethods, + blockers = o.Findings.Select(f => f.Id) }), blockers = ordered.Select(f => new { @@ -297,10 +304,40 @@ internal static int Run(string[] args) return 0; } - // Runs `extract` with the entry's settings into a scratch directory; returns its failure, if any. - static Exception? Extract(string assemblyPath, string signature, string profileName, string manifestDirectory, - string? expectedAssembly, string[] typeArguments, IEnumerable initializedTypes, - IEnumerable references, ImportOptions imports) + /// The blocker of an entry whose extraction failed with `error`: an undecoded instruction names its + /// method and instruction, and any other failure names the entry. + internal static MetadataValidation.Blocked Rejection(MethodDefinition entry, Exception error) => + error is GenericEvidence.UndecodedInstruction undecoded + ? new(undecoded.Method, undecoded.Refusal.At, error, [entry]) + : new(entry, null, error, [entry]); + + /// The construct a diagnostic reports: its first line without its site, so every occurrence of + /// one construct shares one blocker. + internal static string Construct(string message) => + SiteLabel.Replace(message.Split('\n')[0], ""); + + /// The report site of a blocker, at its instruction or at the IL offset its message names. + internal static Site SiteOf(MetadataValidation.Blocked failure) + { + var message = failure.Error.Message; + int? offset = failure.At?.Offset ?? (OffsetLabel.Match(message) is { Success: true } label + ? Convert.ToInt32(label.Groups[1].Value, 16) : null); + return new(failure.Method.FullName, offset, failure.At?.ToString(), + offset is int at ? Symbols.Location(failure.Method, at, Path.GetFileName) : null, message); + } + + [GeneratedRegex(@"\bIL_[0-9a-f]{4,}: ")] + private static partial Regex SiteLabel { get; } + + [GeneratedRegex(@"\bIL_([0-9a-f]{4,})\b")] + private static partial Regex OffsetLabel { get; } + + // Runs `extract` with the entry's settings into a scratch directory; returns its failure, if any, and passes + // the extracted methods to `definitions`. + static Exception? Extract(Program.ExtractionBatch batch, string assemblyPath, string signature, string profileName, + string manifestDirectory, string? expectedAssembly, string[] typeArguments, + IEnumerable initializedTypes, IEnumerable references, ImportOptions imports, + Action definitions) { var output = Path.Combine(Path.GetTempPath(), "leancil-blockers-" + Guid.NewGuid().ToString("N")); string profile = profileName.StartsWith('@') ? "@" + Path.GetFullPath(profileName[1..], manifestDirectory) : @@ -311,7 +348,8 @@ internal static int Run(string[] args) .. expectedAssembly is null ? Array.Empty() : ["--assembly", expectedAssembly], .. typeArguments.SelectMany(argument => new[] { "--type-argument", argument }), .. initializedTypes.SelectMany(type => new[] { "--assume-initialized", type }), - .. references.SelectMany(path => new[] { "--reference", path }), .. imports.Arguments()]); + .. references.SelectMany(path => new[] { "--reference", path }), .. imports.Arguments()], batch, + definitions); return null; } catch (Exception error) { return error; } diff --git a/Extractor/ByteLiterals.cs b/Extractor/ByteLiterals.cs new file mode 100644 index 000000000..dc8f73c47 --- /dev/null +++ b/Extractor/ByteLiterals.cs @@ -0,0 +1,21 @@ +using System.Text; + +namespace Nethermind.LeanCIL; + +/// Byte lists as the extractor writes them into Lean. +internal static class ByteLiterals +{ + /// + /// A Lean byte list as one CIL.Extraction.fixedBytes literal: its length and its big-endian code, as + /// writes it. The elaborator reads numerals where a list literal costs a numeral + /// and a constructor per byte; the qualified names resolve wherever the text is pasted. + /// + internal static string Of(IEnumerable bytes) + { + var data = bytes.ToArray(); + if (data.Length == 0) return "[]"; + var text = new StringBuilder($"(CIL.Extraction.fixedBytes {data.Length} "); + NumeralCode.Append(text, data); + return text.Append(')').ToString(); + } +} diff --git a/Extractor/ClassViews.cs b/Extractor/ClassViews.cs index ea2a8127a..eb094ad00 100644 --- a/Extractor/ClassViews.cs +++ b/Extractor/ClassViews.cs @@ -6,17 +6,17 @@ namespace Nethermind.LeanCIL; // Named fields of the admitted classes an extraction's methods access, so proofs say `Account.Balance` -// instead of an owner and offset. Each name is checked in Lean against an emitted field operation. +// instead of an owner and offset. Each name is checked in Lean against an emitted field access. internal static partial class ClassViews { /// What a class instance field holds: a word, an object reference, or the origin of a whole array of /// a value element. internal enum FieldKind { Word, Reference, Array } - /// A class instance field operation as emitted: a load or store at model offset `Offset` of an - /// instance of `Owner`, of a word of `Width` bytes, an object reference, or an array of `Element`. + /// A class instance field access: a load or store at model offset `Offset` of an instance of + /// `Owner`, of a word of `Width` bytes, an object reference, or an array of `Element`. internal sealed record FieldOperation(bool Store, string Owner, int Offset, FieldKind Kind, int? Width, - bool Signed, string? Element); + bool Signed, Arrays.Element? Element); /// One field access: the method and pc whose emitted operation loads or stores the field. internal sealed record Access(FieldDefinition Field, TypeReference Owner, int Method, int Pc, @@ -24,7 +24,8 @@ internal sealed record Access(FieldDefinition Field, TypeReference Owner, int Me /// A named field of a class: where it lives, what it holds, and which methods load and store it. /// - internal sealed record Field(string Name, int Offset, FieldKind Kind, int? Width, string? Element, int[] Loaders, + internal sealed record Field(string Name, int Offset, FieldKind Kind, int? Width, Arrays.Element? Element, + int[] Loaders, int[] Storers) { /// The name as a Lean identifier. @@ -34,32 +35,17 @@ internal sealed record Field(string Name, int Offset, FieldKind Kind, int? Width /// A class's `Extracted.Classes` namespace: its full name, Lean path and named fields. internal sealed record View(string FullName, string Path, string Type, Field[] Fields); - /// The field operation an emitted op performs, or none; every reader of field ops decodes here. - /// - internal static FieldOperation? Decode(string op) + /// The named class field accessed by a reachable load or store. + internal static Access? Of(Instruction instruction, int method, int pc, string architecture) { - var match = FieldOperationPattern().Match(op); - if (!match.Success) return null; - var name = match.Groups["kind"].Value; - var kind = name switch - { - "arrayField" or "setArrayField" => FieldKind.Array, - "objectReferenceField" or "setObjectReferenceField" => FieldKind.Reference, - _ => FieldKind.Word - }; - return new(name.StartsWith("set", StringComparison.Ordinal), match.Groups["owner"].Value, - int.Parse(match.Groups["offset"].Value), kind, - match.Groups["width"].Success ? int.Parse(match.Groups["width"].Value) : null, - match.Groups["signed"].Value == "true", - match.Groups["element"].Success ? match.Groups["element"].Value : null); + if (instruction is not { OpCode.Code: Code.Ldfld or Code.Stfld, Operand: FieldReference field } || + !ObjectTypes.Candidate(field.DeclaringType)) return null; + var resolved = field.Resolve() ?? throw new InvalidDataException("Unresolved field"); + var operation = ObjectTypes.FieldOperation(field, resolved, instruction.OpCode.Code == Code.Stfld, + address: false, architecture); + return new(resolved, field.DeclaringType, method, pc, operation); } - /// The access a ldfld or stfld on a class instance field emitted, or none. - internal static Access? Of(Instruction instruction, int method, int pc, string op) => - instruction is { OpCode.Code: Code.Ldfld or Code.Stfld, Operand: FieldReference field } && - Decode(op) is { } operation && field.Resolve() is { } resolved - ? new(resolved, field.DeclaringType, method, pc, operation) : null; - /// The views of the classes whose fields the accesses touch, each field named once per class. /// internal static View[] Views(IEnumerable accesses) @@ -115,14 +101,15 @@ internal static string Emit(IEnumerable accesses, View[] views) var descriptor = field.Kind switch { FieldKind.Word => $"Safety.WordField {field.Width}", - FieldKind.Array => $"Safety.ArrayField {field.Element}", + FieldKind.Array => $"Safety.ArrayField {field.Element!.Expression}", _ => "Safety.ReferenceField" }; lean.AppendLine($"/-- `{access.Field.FieldType.FullName} {view.FullName}::{access.Field.Name}`, " + $"at model offset {field.Offset}. -/"); lean.AppendLine($"@[reducible] def {field.Lean} : {descriptor} := ⟨type, {field.Offset}⟩"); lean.AppendLine($"theorem {Identifier(field.Name + "_accessed")} : " + - $"(method{access.Method}.code[{access.Pc}]?).any {field.Lean}.accessedBy = true := " + + $"(method{access.Method}.code[{access.Pc}]?).any " + + $"({field.Lean}.accessedBy method{access.Method}) = true := " + LeanEmitter.Decide); } lean.AppendLine($"end {view.Path}"); @@ -135,14 +122,14 @@ internal static string Emit(IEnumerable accesses, View[] views) /// The name C# source gives a field: an auto-property's backing field takes its property's. internal static string FieldName(FieldDefinition field) => - BackingField().Match(field.Name) is { Success: true } backing ? backing.Groups[1].Value : field.Name; + BackingField.Match(field.Name) is { Success: true } backing ? backing.Groups[1].Value : field.Name; [GeneratedRegex(@"\A<([A-Za-z_][A-Za-z0-9_]*)>k__BackingField\z")] - private static partial Regex BackingField(); + private static partial Regex BackingField { get; } // Metadata names may hold characters Lean identifiers cannot, such as a closure class's `<>c__DisplayClass1_0`. static string Identifier(string name) => - LeanIdentifier().IsMatch(name) && !Keywords.Contains(name) ? name : $"«{name}»"; + LeanIdentifier.IsMatch(name) && !Keywords.Contains(name) ? name : $"«{name}»"; static readonly HashSet Keywords = new(StringComparer.Ordinal) { @@ -154,11 +141,5 @@ static string Identifier(string name) => }; [GeneratedRegex(@"\A[A-Za-z_][A-Za-z0-9_']*\z")] - private static partial Regex LeanIdentifier(); - - // The owner is matched lazily, since an array element such as an enumeration is parenthesized too. - [GeneratedRegex(@"\A\.memory \(\.(?objectField|setObjectField|objectReferenceField|" + - @"setObjectReferenceField|arrayField|setArrayField) \((?.*?)\) (?\d+)" + - @"(?: (?\d+)(?: (?true|false))?| (?\.\w+|\(.*\)))?\)\z")] - private static partial Regex FieldOperationPattern(); + private static partial Regex LeanIdentifier { get; } } diff --git a/Extractor/CliDiagnostics.cs b/Extractor/CliDiagnostics.cs index 09613fe0e..9f76a374f 100644 --- a/Extractor/CliDiagnostics.cs +++ b/Extractor/CliDiagnostics.cs @@ -50,7 +50,7 @@ internal static string Printable(string text) => string.Concat(text.Select(chara ? $"\\u{(int)character:X4}" : character.ToString())); static bool IsBidiControl(char character) => - character is >= '‪' and <= '‮' or >= '⁦' and <= '⁩' or '‎' or '‏' or '؜'; + character is >= '\u202A' and <= '\u202E' or >= '\u2066' and <= '\u2069' or '\u200E' or '\u200F' or '\u061C'; internal static void Write(Exception error, string stage, bool invalid) { @@ -82,8 +82,8 @@ internal static void Write(Exception error, string stage, bool invalid) "program comparison" => "Fix the fixture case, or the variant's program if the change is intended.", "refutation template" or "proof template" => "Give every template placeholder a value and every value a placeholder.", - "statement identity" => "State the variant's theorem as production's statement, or its refutation as " + - "that statement's negation; name only declarations both proofs import.", + "statement identity" => "State the theorem as the statement it is compared with, or a refutation as " + + "that statement's counterexample predicate (Refuted); name only declarations both proofs import.", "binding generation" => "Correct the catalog entry the diagnostic names, then rerun leancil bindings.", _ => "Check the diagnostic and the input paths, then retry." }; diff --git a/Extractor/ComputedLayout.cs b/Extractor/ComputedLayout.cs new file mode 100644 index 000000000..f0f0ef49b --- /dev/null +++ b/Extractor/ComputedLayout.cs @@ -0,0 +1,16 @@ +namespace Nethermind.LeanCIL; + +/// A field of a computed layout: its byte offset, its size, and whether it is itself an aggregate. +internal sealed record LayoutField(int Offset, int Width, bool IsAggregate) +{ + internal string ToLean() => $"⟨{Offset}, {Width}, {LeanText.Bool(IsAggregate)}⟩"; +} + +/// +/// The size of the aggregate of identity and the placement of its fields, as the +/// extraction computed them. Its position among the emitted layouts is the number of its `layout{n}` definition. +/// +internal sealed record ComputedLayout(string Identity, int Size, ValueList Fields) +{ + internal string ToLean() => $"⟨{Size}, [{string.Join(", ", Fields.Select(field => field.ToLean()))}]⟩"; +} diff --git a/Extractor/ConstrainedCalls.cs b/Extractor/ConstrainedCalls.cs index 4b1d8a9bb..58423a65e 100644 --- a/Extractor/ConstrainedCalls.cs +++ b/Extractor/ConstrainedCalls.cs @@ -17,22 +17,25 @@ internal sealed record Resolution(TypeDefinition Receiver, MethodDefinition Slot internal sealed class TargetIndex(IEnumerable methods) { - readonly Dictionary<(MethodDefinition Definition, string Owner, string Arguments), MethodDefinition> targets = - methods.Where(method => MethodSpecialization.Source(method) is not null) - .ToDictionary(method => (MethodSpecialization.Definition(method), - MethodSpecialization.SignatureType(MethodSpecialization.DeclaringType(method)), - ArgumentKey(MethodSpecialization.Arguments(method) + readonly SortedDictionary targets = Build(methods); + + static SortedDictionary Build( + IEnumerable methods) + { + var result = new SortedDictionary(); + foreach (var method in methods.Where(method => MethodSpecialization.Source(method) is not null)) + result.Add(StructuralSignatures.Current.Specialization(method, + MethodSpecialization.DeclaringType(method), MethodSpecialization.Arguments(method) .Take(MethodSpecialization.Arguments(method).Count - - MethodSpecialization.OwnerArguments(method).Count)))); + MethodSpecialization.OwnerArguments(method).Count)), method); + return result; + } internal MethodDefinition Find(Resolution resolution) => !resolution.Specialized ? resolution.Target : - targets.GetValueOrDefault((resolution.Target, MethodSpecialization.SignatureType(resolution.Owner), - ArgumentKey(resolution.MethodArguments))) ?? + targets.GetValueOrDefault(StructuralSignatures.Current.Specialization( + resolution.Target, resolution.Owner, resolution.MethodArguments)) ?? throw new InvalidDataException("Unresolved constrained owner specialization"); - - static string ArgumentKey(IEnumerable arguments) => - string.Join(",", arguments.Select(MethodSpecialization.SignatureType)); } internal static MethodDefinition Specialize(Resolution resolution, MethodSpecialization specializations) @@ -76,9 +79,8 @@ internal static Resolution Resolve(MethodDefinition caller, Instruction call, if (member.Resolve() is not { } slot || !Extracted(slot.Module, caller.Module) || member.Name != slot.Name || !SameType(member.ReturnType, slot.ReturnType) || - !member.Parameters.Select(parameter => MethodSpecialization.SignatureType(parameter.ParameterType)) - .SequenceEqual(slot.Parameters.Select(parameter => - MethodSpecialization.SignatureType(parameter.ParameterType))) || + StructuralSignatures.Current.Ordered(member.Parameters.Select(parameter => parameter.ParameterType)) != + StructuralSignatures.Current.Ordered(slot.Parameters.Select(parameter => parameter.ParameterType)) || member.HasThis != slot.HasThis || slot.HasThis != instance || slot.IsStatic == instance || slot.IsConstructor || slot.GenericParameters.Count != methodArguments.Length || instance && methodArguments.Length != 0 || @@ -166,13 +168,13 @@ static Resolution InterfaceBody(TypeDefinition receiver, MethodReference slot, M { var listed = concrete.Interfaces.Select(implementation => GenericStructs.Substitute(implementation.InterfaceType, owner)).ToArray(); - var names = listed.Select(MethodSpecialization.SignatureType).ToHashSet(StringComparer.Ordinal); + var names = new SortedSet(listed.Select(StructuralSignatures.Current.Type)); List<(MethodDefinition Body, TypeReference Owner)> candidates = [(slot, closedSlotOwner)]; foreach (var candidate in listed) { if (candidate.Resolve() is not { } declared || !Extracted(declared.Module, caller) || !declared.IsInterface || !BaseInterfaces(candidate).All(parent => - names.Contains(MethodSpecialization.SignatureType(parent)))) + names.Contains(StructuralSignatures.Current.Type(parent)))) throw new InvalidDataException("A static virtual slot's interface-level body requires every " + "interface of the receiver, including inherited ones, to be listed and declared in an " + "extracted assembly"); @@ -235,5 +237,5 @@ static TypeReference Close(TypeReference type, GenericInstanceType? owner, TypeR } static bool SameType(TypeReference left, TypeReference right) => - MethodSpecialization.SignatureType(left) == MethodSpecialization.SignatureType(right); + StructuralSignatures.Current.Type(left) == StructuralSignatures.Current.Type(right); } diff --git a/Extractor/ContractMethodMetadata.cs b/Extractor/ContractMethodMetadata.cs index ac20ac73a..a978f4737 100644 --- a/Extractor/ContractMethodMetadata.cs +++ b/Extractor/ContractMethodMetadata.cs @@ -1,6 +1,5 @@ using System.Globalization; using System.Text; -using System.Text.Json; using Mono.Cecil; namespace Nethermind.LeanCIL; @@ -21,7 +20,7 @@ internal static ContractType Read(TypeReference type) internal string Lean => Kind switch { "void" => ".void", - "atom" => ".atom " + JsonSerializer.Serialize(Scope) + " " + JsonSerializer.Serialize(Name), + "atom" => $".atom {IdentityText.Quote(Scope)} {IdentityText.Quote(Name)}", "byref" when Element is not null => ".byref (" + Element.Lean + ")", _ => throw new InvalidDataException("Invalid imported type metadata.") }; @@ -36,10 +35,17 @@ internal static ContractMethodSignature Of(MethodReference reference, MethodDefi CallReturnType.Parameters(reference).Select(ContractType.Read).ToArray(), definition.Parameters.Select(parameter => (int)parameter.Attributes).ToArray()); - internal string Lean => "{ owner := " + Owner.Lean + ", name := " + JsonSerializer.Serialize(Name) + - ", arguments := [" + string.Join(", ", Arguments.Select(type => type.Lean)) + "]" + - ", returnType := " + ReturnType.Lean + ", parameters := [" + - string.Join(", ", Parameters.Select(type => type.Lean)) + "] }"; + internal string Lean + { + get + { + var owner = $"owner := {Owner.Lean}, name := {IdentityText.Quote(Name)}"; + var arguments = string.Join(", ", Arguments.Select(type => type.Lean)); + var parameters = string.Join(", ", Parameters.Select(type => type.Lean)); + var signature = $"arguments := [{arguments}], returnType := {ReturnType.Lean}"; + return $$"""{ {{owner}}, {{signature}}, parameters := [{{parameters}}] }"""; + } + } } internal static class ContractMethodMetadata @@ -127,7 +133,8 @@ static void RejectModifiers(MethodReference method, bool selected) } internal static bool Primitive(TypeReference type) => type is not TypeSpecification && - RuntimeModels.ExactScope(type.Scope, "System.Runtime") && type.IsValueType && PrimitiveName(type.FullName); + RuntimeModels.ExactScope(type, PlatformAssemblies.SystemRuntime) && type.IsValueType && + PrimitiveName(type.FullName); static bool PrimitiveName(string name) => name is "System.SByte" or "System.Byte" or "System.Boolean" or "System.Int16" or "System.UInt16" or @@ -153,7 +160,7 @@ static TypeReference EntryArgument(ModuleDefinition module, string name) { IsValueType: true, HasGenericParameters: false } referenced) throw new InvalidDataException("Closed entry type argument is not a non-generic value type of " + owner[0].Assembly.Name.FullName + ": " + name); - return module.ImportReference(referenced); + return referenced; } return module.GetType(name) is { IsValueType: true, HasGenericParameters: false } local ? local : throw new InvalidDataException( @@ -210,13 +217,13 @@ static Selection Instantiate(MethodDefinition definition, TypeReference[] argume MethodDefinition closed = new MethodSpecialization(definition.Module, architecture).Instantiate(reference); // Scalar certificates read the closed return and parameter types from the identity. // Keep definition and scoped argument keys before the parameter list. - string definitionHash = ContractInterface.Hash(Encoding.UTF8.GetBytes(DefinitionKey(definition, entry))); + string definitionKey = Convert.ToHexString(Encoding.UTF8.GetBytes(DefinitionKey(definition, entry))); StringBuilder identity = new(definition.Module.Assembly.Name.FullName + "::" + closed.ReturnType.FullName + " " + - closed.DeclaringType.FullName + "::" + closed.Name + "|definition|" + definitionHash + "|arguments|"); + closed.DeclaringType.FullName + "::" + closed.Name + "|definition|" + definitionKey + "|arguments|"); foreach (TypeReference argument in arguments) { string scoped = OpenType(argument); - identity.Append(scoped.Length.ToString(CultureInfo.InvariantCulture)).Append(':').Append(scoped); + identity.Append(Part(scoped)); } identity.Append('(').AppendJoin(',', closed.Parameters.Select(parameter => parameter.ParameterType.FullName)).Append(')'); return new(definition, closed, reference, arguments, identity.ToString()); @@ -280,7 +287,8 @@ static string DefinitionKey(MethodDefinition definition, bool entry) { Func parameterKey = entry ? EntryParameter : OpenParameter; StringBuilder key = new("contract-definition-v1:"); - key.Append(Part(OpenType(definition.DeclaringType))).Append(Part(definition.Name)) + string owner = Part(LayoutEvidence.Scope(definition.DeclaringType)) + Part(definition.DeclaringType.FullName); + key.Append(Part(owner)).Append(Part(definition.Name)) .Append(Part(definition.GenericParameters.Count.ToString(CultureInfo.InvariantCulture))) .Append(Part(((byte)definition.CallingConvention).ToString(CultureInfo.InvariantCulture))) .Append(Part(definition.HasThis ? "1" : "0")).Append(Part(definition.ExplicitThis ? "1" : "0")) @@ -291,7 +299,8 @@ static string DefinitionKey(MethodDefinition definition, bool entry) return key.ToString(); } - static string Part(string value) => value.Length.ToString(CultureInfo.InvariantCulture) + ":" + value; + static string Part(string value) => + Encoding.UTF8.GetByteCount(value).ToString(CultureInfo.InvariantCulture) + ":" + value; // The `in:` tag keeps an `in` reference distinct from a plain one; plain parameters keep their encoding. static string OpenParameter(TypeReference type) => diff --git a/Extractor/EnumTypes.cs b/Extractor/EnumTypes.cs index fe168689f..ceffe9a44 100644 --- a/Extractor/EnumTypes.cs +++ b/Extractor/EnumTypes.cs @@ -59,14 +59,15 @@ static TypeReference ValidateStorage(TypeReference reference) if (!reference.IsValueType || type.HasGenericParameters || !type.IsSealed || type.HasInterfaces || !type.IsAutoLayout || type.BaseType is not { FullName: "System.Enum" } parent || - !RuntimeModels.ExactScope(parent.Scope, "System.Runtime") || + !RuntimeModels.ExactScope(parent, PlatformAssemblies.SystemRuntime) || type.Module != reference.Module || type.Methods.Count != 0 || fields.Length != 1 || fields[0].Name != "value__" || !fields[0].IsSpecialName || !fields[0].IsRuntimeSpecialName || (activeScope?.Count(type) ?? type.Module.GetTypes().Count(t => t.FullName == type.FullName)) != 1) throw new InvalidDataException($"Unsupported enum metadata: {reference.FullName}"); var storage = fields[0].FieldType; - if (storage is TypeSpecification || !RuntimeModels.ExactScope(storage.Scope, "System.Runtime") || + if (storage is TypeSpecification || + !RuntimeModels.ExactScope(storage, PlatformAssemblies.SystemRuntime) || storage.FullName is not ("System.SByte" or "System.Byte" or "System.Int16" or "System.UInt16" or "System.Int32" or "System.UInt32" or "System.Int64" or "System.UInt64")) throw new InvalidDataException($"Unsupported enum underlying type: {reference.FullName}"); diff --git a/Extractor/ExceptionLint.cs b/Extractor/ExceptionLint.cs index b37532c6c..c582da694 100644 --- a/Extractor/ExceptionLint.cs +++ b/Extractor/ExceptionLint.cs @@ -9,11 +9,11 @@ namespace Nethermind.LeanCIL; -internal static class ExceptionLint +internal static partial class ExceptionLint { const string Usage = "Usage: leancil lint-exceptions output-directory --documentation assembly.xml"; const string Notice = "Diagnostic only. Sites are potential model paths, not proved exceptions. XML comments are untrusted documentation, not evidence."; - sealed record Site(string Method, int IlOffset, string Exception, string Origin); + sealed record Site(string Method, int IlOffset, ExceptionType Exception, string Origin); sealed record Declaration(string Exception, string Text); internal static int Run(string[] args) @@ -37,7 +37,8 @@ internal static int Run(string[] args) byte[] xml = new byte[checked((int)documentationStream.Length)]; documentationStream.ReadExactly(xml); if (documentationStream.ReadByte() != -1) throw new InvalidDataException("Exception documentation changed while reading."); - using ModuleDefinition module = StaticData.ReadModule(assembly); + using PinnedAssemblies pinned = new(assembly, Hash(assembly), []); + ModuleDefinition module = pinned.Selected; FeatureProfile profile = identity.GetProperty("profile").Deserialize() ?? throw new InvalidDataException("Missing extraction profile."); SortedSet initialized = new(identity.GetProperty("initializedTypes").Deserialize() ?? [], StringComparer.Ordinal); @@ -50,31 +51,31 @@ internal static int Run(string[] args) throw new InvalidDataException("Extraction changed during exception lint."); string member = DocumentationId(MethodSpecialization.Definition(methods[0])); Declaration[] declarations = ReadDocumentation(xml, module.Assembly.Name.Name, member); - var methodIndices = methods.Select((method, index) => (method, index)) - .ToDictionary(pair => pair.method, pair => pair.index); List sites = []; - for (int methodIndex = 0; methodIndex < methods.Length; methodIndex++) + foreach (var method in methods) { - MethodDefinition method = methods[methodIndex]; - var instructions = new InstructionIndex(method); var reachable = Reachability.Analyze(method, profile); - var nativeOperands = NativeOperands.Certify(method, reachable, profile); - var parameterHomes = ParameterStorage.IndexHomes(ParameterStorage.Analyze(method, reachable)); - Throws.Analysis folded = Throws.Analyze(method); - foreach (Instruction instruction in reachable.OrderBy(value => value.Offset)) + var folded = Throws.Analyze(method); + foreach (var instruction in method.Body.Instructions) { - if (folded.Raises.TryGetValue(instruction, out string? exception)) + if (!reachable.Contains(instruction)) continue; + if (folded.Raises.TryGetValue(instruction, out var exception)) sites.Add(new(method.FullName, instruction.Offset, exception, "folded throw")); - string operation = InstructionTranslation.Translate(instruction, method, methodIndices, - methodIndex, instructions, profile, nativeOperands, parameterHomes); - foreach (string named in RuntimeExceptions(operation)) - sites.Add(new(method.FullName, instruction.Offset, named, "interpreter operation: " + operation)); + if (Faults(method, instruction, profile) is not { } faults) continue; + var origin = $"interpreter operation: {instruction}"; + foreach (var named in RuntimeExceptions(faults)) + sites.Add(new(method.FullName, instruction.Offset, named, origin)); } } - string[] modeled = sites.Select(site => site.Exception).Distinct(StringComparer.Ordinal).Order(StringComparer.Ordinal).ToArray(); + ExceptionType[] modeled = sites.Select(site => site.Exception).Distinct() + .OrderBy(exception => exception.FullName(), StringComparer.Ordinal).ToArray(); + HashSet modeledSet = [.. modeled]; string[] documented = declarations.Select(value => value.Exception).Distinct(StringComparer.Ordinal).Order(StringComparer.Ordinal).ToArray(); - string[] undocumented = modeled.Except(documented, StringComparer.Ordinal).ToArray(); - string[] absent = documented.Except(modeled, StringComparer.Ordinal).ToArray(); + HashSet documentedMembers = [.. documented.Select(DocumentedMember).OfType()]; + string[] undocumented = modeled.Where(exception => !documentedMembers.Contains(exception)) + .Select(exception => exception.FullName()).ToArray(); + string[] absent = documented.Where(name => DocumentedMember(name) is not ExceptionType member || + !modeledSet.Contains(member)).ToArray(); string report = JsonSerializer.Serialize(new { schema = 1, note = Notice, @@ -83,7 +84,10 @@ internal static int Run(string[] args) assemblyIdentity = module.Assembly.Name.FullName, assemblySha256 = Hash(assembly), documentationSha256 = Hash(xml), generatedSha256 = regeneratedHash, entry = methods[0].FullName, documentationMember = member, profile = profile.Name, - potentialModeled = modeled, documented = declarations, sites, + potentialModeled = modeled.Select(exception => exception.FullName()), + documented = declarations, + sites = sites.Select(site => + new { site.Method, site.IlOffset, Exception = site.Exception.FullName(), site.Origin }), findings = undocumented.Select(exception => new { exception, message = "potential modelled exception not documented" }) .Concat(absent.Select(exception => new { exception, message = "documented exception not found in the model" })) }, new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); @@ -99,25 +103,57 @@ internal static int Run(string[] args) return 0; } - static IEnumerable RuntimeExceptions(string operation) + enum FaultKind { DivideByZeroAndOverflow, DivideByZero, Overflow, IndexOutOfRange } + + static IEnumerable RuntimeExceptions(FaultKind kind) + { + if (kind is FaultKind.DivideByZeroAndOverflow or FaultKind.DivideByZero) + yield return ExceptionType.DivideByZero; + if (kind is FaultKind.DivideByZeroAndOverflow or FaultKind.Overflow) + yield return ExceptionType.Overflow; + if (kind == FaultKind.IndexOutOfRange) + yield return ExceptionType.IndexOutOfRange; + } + + static FaultKind? Faults(MethodDefinition method, Instruction instruction, FeatureProfile profile) { - if (operation.StartsWith(".nativeBinary (", StringComparison.Ordinal)) operation = operation[15..^1]; - if (operation.StartsWith(".divide ", StringComparison.Ordinal)) + var code = instruction.OpCode.Code; + if (InstructionOperands.IsCheckedConversion(code)) return FaultKind.Overflow; + FaultKind? kind = code switch { - yield return "System.DivideByZeroException"; - if (operation.StartsWith(".divide true ", StringComparison.Ordinal)) yield return "System.OverflowException"; - } - else if (operation.StartsWith(".checkedArithmetic ", StringComparison.Ordinal) || - operation.StartsWith(".checkedConvert ", StringComparison.Ordinal) || - operation.StartsWith(".intrinsic (.scalar (.math .abs ", StringComparison.Ordinal)) - yield return "System.OverflowException"; - else if (operation == ".intrinsic (.x86Base .divRem64) 3") + Code.Div or Code.Rem => FaultKind.DivideByZeroAndOverflow, + Code.Div_Un or Code.Rem_Un => FaultKind.DivideByZero, + Code.Add_Ovf or Code.Sub_Ovf or Code.Mul_Ovf or + Code.Add_Ovf_Un or Code.Sub_Ovf_Un or Code.Mul_Ovf_Un => FaultKind.Overflow, + _ => null + }; + if (kind.HasValue) return kind; + if (code != Code.Call || instruction.Previous?.OpCode.Code == Code.Constrained || + instruction.Operand is not MethodReference reference) return null; + var typeTests = TypeTests.Analyze(method); + if (typeTests.Operands.Contains(instruction) || typeTests.Results.ContainsKey(instruction)) return null; + return RuntimeModels.CallModel(reference, profile) switch { - yield return "System.DivideByZeroException"; - yield return "System.OverflowException"; - } - else if (operation.StartsWith(".memory (.spanItem ", StringComparison.Ordinal)) - yield return "System.IndexOutOfRangeException"; + Unoutlined when RuntimeModels.IntegerAbs(reference) => FaultKind.Overflow, + Unoutlined when reference.FullName == RuntimeModels.X86DivRemSignature => FaultKind.DivideByZeroAndOverflow, + Unoutlined when Spans.MemberWidth(instruction, "get_Item", 1, profile.Architecture) is not null => + FaultKind.IndexOutOfRange, + _ => null + }; + } + + [GeneratedRegex(@"\AT:[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*\z")] + internal static partial Regex DocumentationReference { get; } + + [GeneratedRegex(@"`[0-9]+")] + internal static partial Regex GenericArity { get; } + + // The documentation boundary: a documented type name is the member of that full name, or none. + static ExceptionType? DocumentedMember(string name) + { + foreach (ExceptionType exception in ExceptionTypeText.All) + if (exception.FullName() == name) return exception; + return null; } static Declaration[] ReadDocumentation(byte[] xml, string assembly, string member) @@ -135,7 +171,7 @@ static Declaration[] ReadDocumentation(byte[] xml, string assembly, string membe foreach (XElement declaration in matches[0].Elements("exception")) { string? cref = (string?)declaration.Attribute("cref"); - if (cref is null || !Regex.IsMatch(cref, @"\AT:[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*\z")) + if (cref is null || !DocumentationReference.IsMatch(cref)) throw new InvalidDataException("Exception documentation requires a resolved type cref, not prose or an unresolved reference."); result.Add(new(cref[2..], declaration.Value.Trim())); } @@ -157,7 +193,7 @@ static string DocumentationId(MethodDefinition method) PointerType pointer => DocumentationType(pointer.ElementType) + "*", ArrayType array => DocumentationType(array.ElementType) + (array.IsVector ? "[]" : "[" + string.Join(",", Enumerable.Repeat("0:", array.Rank)) + "]"), GenericParameter parameter => (parameter.Type == GenericParameterType.Method ? "``" : "`") + parameter.Position, - GenericInstanceType generic => Regex.Replace(generic.ElementType.FullName.Replace('/', '.'), @"`[0-9]+", "") + + GenericInstanceType generic => GenericArity.Replace(generic.ElementType.FullName.Replace('/', '.'), "") + "{" + string.Join(",", generic.GenericArguments.Select(DocumentationType)) + "}", RequiredModifierType modifier => DocumentationType(modifier.ElementType), OptionalModifierType modifier => DocumentationType(modifier.ElementType), diff --git a/Extractor/ExternalIdentityEvidence.cs b/Extractor/ExternalIdentityEvidence.cs index 3dfe58a9c..f24f4ea65 100644 --- a/Extractor/ExternalIdentityEvidence.cs +++ b/Extractor/ExternalIdentityEvidence.cs @@ -1,6 +1,5 @@ using System.Globalization; using System.Text; -using System.Text.Json; namespace Nethermind.LeanCIL; @@ -16,7 +15,7 @@ internal sealed class ExternalIdentityEvidence internal string Reference(string identity, bool reachable) { - if (!reachable) return JsonSerializer.Serialize(identity); + if (!reachable) return IdentityText.Quote(identity); if (!indices.TryGetValue(identity, out var index)) { index = identities.Count; @@ -26,13 +25,14 @@ internal string Reference(string identity, bool reachable) return "externalIdentity" + index.ToString(CultureInfo.InvariantCulture); } - internal void Emit(StringBuilder lean) + internal void Emit(StringBuilder lean, StringBuilder? decoderInputs = null) { while (emitted < identities.Count) { var identity = identities[emitted]; var name = "externalIdentity" + emitted.ToString(CultureInfo.InvariantCulture); var encoded = StrictUtf8.GetBytes(identity); + decoderInputs?.AppendLine($"def {name} : String := {IdentityText.Quote(identity)}"); var proofs = EmitChunks(lean, encoded); var bytes = string.Join(", ", encoded .Select(value => value.ToString(CultureInfo.InvariantCulture))); @@ -62,7 +62,7 @@ List EmitChunks(StringBuilder lean, byte[] encoded) var chunkBytes = string.Join(", ", chunk.ToArray() .Select(value => value.ToString(CultureInfo.InvariantCulture))); var proof = chunk.ContainsAnyInRange((byte)128, byte.MaxValue) ? "by decide +kernel" : - $"by exact ({JsonSerializer.Serialize(Encoding.UTF8.GetString(chunk))}).isValidUTF8"; + $"by exact ({IdentityText.Quote(Encoding.UTF8.GetString(chunk))}).isValidUTF8"; lean.AppendLine($"def {chunkName} : ByteArray := ⟨#[{chunkBytes}]⟩"); lean.AppendLine($"theorem {chunkName}_valid : {chunkName}.IsValidUTF8 := {proof}"); lean.AppendLine($"#print axioms {chunkName}_valid"); diff --git a/Extractor/ExtractionBatch.cs b/Extractor/ExtractionBatch.cs index 94d71764d..4397783c0 100644 --- a/Extractor/ExtractionBatch.cs +++ b/Extractor/ExtractionBatch.cs @@ -8,9 +8,9 @@ internal static partial class Program sealed record ExtractionRequest(string Assembly, string? Identity, string Entry, string Output, string Profile, string[]? InitializedTypes, string[]? TypeArguments = null); - // Only immutable assembly bytes cross requests. Validation rewrites Cecil instruction operands, + // Only immutable assembly and PDB bytes cross requests. Validation rewrites Cecil instruction operands, // and symbols attach readers to modules, so every request owns fresh modules and symbol readers. - sealed class ExtractionBatch + internal sealed class ExtractionBatch { internal sealed record AssemblySnapshot(byte[] Bytes, string Hash) { @@ -19,12 +19,13 @@ internal sealed record AssemblySnapshot(byte[] Bytes, string Hash) internal void ValidateSignatures() { if (signaturesValidated) return; - VectorSignatures.Validate(Bytes); + PlatformSignatures.Validate(Bytes); signaturesValidated = true; } } readonly Dictionary assemblies = new(StringComparer.Ordinal); readonly Dictionary files = new(StringComparer.Ordinal); + readonly Dictionary pdbFiles = new(StringComparer.Ordinal); internal string ExtractorHash { get; } @@ -53,10 +54,22 @@ internal AssemblySnapshot ReadAssembly(string path) snapshot = new(bytes, Hash(bytes)); assemblies.Add(path, snapshot); files.TryAdd(path, snapshot.Hash); - Track(Path.ChangeExtension(path, ".pdb")); + ReadPdb(Path.ChangeExtension(path, ".pdb")); return snapshot; } + /// The PDB at , read and hashed once per batch; null when there is none. + /// + internal Symbols.PdbFile? ReadPdb(string path) + { + path = Path.GetFullPath(path); + if (pdbFiles.TryGetValue(path, out var pdb)) return pdb; + pdb = Symbols.PdbFile.Load(path); + pdbFiles.Add(path, pdb); + files.TryAdd(path, pdb?.Sha256); + return pdb; + } + internal void RequireUnchanged() { foreach (var (path, hash) in files) @@ -73,14 +86,15 @@ static int RunExtractionBatch(string[] args) var requests = JsonSerializer.Deserialize(File.ReadAllBytes(args[1]), new JsonSerializerOptions(JsonSerializerDefaults.Web)) ?? throw new InvalidDataException("Missing extraction batch requests."); - var outputs = new HashSet(StringComparer.OrdinalIgnoreCase); + // Outputs in another case are distinct only where the volume is known to tell case apart; a link never is. + var outputs = new HashSet(global::Program.CollidingLocationComparer); foreach (var request in requests) { if (request is null || string.IsNullOrWhiteSpace(request.Assembly) || string.IsNullOrWhiteSpace(request.Entry) || string.IsNullOrWhiteSpace(request.Output) || string.IsNullOrWhiteSpace(request.Profile)) throw new InvalidDataException("Incomplete extraction batch request."); - if (!outputs.Add(Path.TrimEndingDirectorySeparator(Path.GetFullPath(request.Output)))) + if (!outputs.Add(global::Program.ResolvedPath(request.Output))) throw new InvalidDataException("Extraction batch outputs must be distinct."); } var batch = new ExtractionBatch(); diff --git a/Extractor/Extractor.csproj b/Extractor/Extractor.csproj index fbaca41f4..babd3787d 100644 --- a/Extractor/Extractor.csproj +++ b/Extractor/Extractor.csproj @@ -9,14 +9,17 @@ true leancil README.md - net10.0 + net11.0 enable enable $(DefineConstants);LEANCIL_TOOL + + $(Features);FileBasedProgram Nethermind.LeanCIL.Program + @@ -33,7 +36,10 @@ - + + + + verification/%(Identity) verification/$([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)/..', '%(FullPath)')) PreserveNewest diff --git a/Extractor/FeatureEvidence.cs b/Extractor/FeatureEvidence.cs index f501909f4..3062a1058 100644 --- a/Extractor/FeatureEvidence.cs +++ b/Extractor/FeatureEvidence.cs @@ -10,6 +10,12 @@ internal static class FeatureEvidence { static string Fact(int? value) => value is int word ? $"some (BitVec.ofInt 32 ({word}))" : "none"; + // Lean accepts only known facts in increasing local order, so unknown locals cost nothing. + static IEnumerable KnownLocals(IReadOnlyList locals) => + locals.Select((value, index) => (value, index)) + .Where(entry => entry.value is not null) + .Select(entry => $"({entry.index}, BitVec.ofInt 32 ({entry.value}))"); + internal static void Emit(StringBuilder lean, MethodDefinition method, int number, IReadOnlyDictionary incoming) { @@ -17,7 +23,7 @@ internal static void Emit(StringBuilder lean, MethodDefinition method, int numbe { if (!incoming.TryGetValue(instruction, out FeatureConstants? state)) return "none"; return $"some {{ stack := [{string.Join(", ", state.Stack.Reverse().Select(Fact))}], " + - $"locals := [{string.Join(", ", state.Locals.Select(Fact))}] }}"; + $"locals := [{string.Join(", ", KnownLocals(state.Locals))}] }}"; }); lean.AppendLine($"def method{number}FeatureStates : Extraction.FeatureCertificate := [{string.Join(", ", states)}]"); } @@ -26,7 +32,8 @@ internal static void Emit(StringBuilder lean, MethodDefinition method, int numbe // therefore depends on the whole program and must stay out of any per-method component. internal static void EmitTheorem(StringBuilder lean, int number) => lean.AppendLine($"theorem method{number}_features_validated : Extraction.FeatureFlowValid rawLayouts.toArray " + - $"(Extraction.evidenceConstants methodEvidence) method{number}Closed profile " + - $"method{number}FeatureStates " + + // `MethodEvidenceValid` reads links by `linkIndex`; another reader makes its proof unfold the whole body. + $"(Extraction.evidenceConstants methodEvidence) (Extraction.linkIndex method{number}) " + + $"method{number}Closed profile method{number}FeatureStates " + $"method{number}Reachable method{number}.code := {LeanEmitter.Decide}"); } diff --git a/Extractor/FeatureProfiles.cs b/Extractor/FeatureProfiles.cs index 47d743b23..840ba642e 100644 --- a/Extractor/FeatureProfiles.cs +++ b/Extractor/FeatureProfiles.cs @@ -1,3 +1,4 @@ +using static Nethermind.LeanCIL.LeanText; using Mono.Cecil; using System.Text.Json; using System.Text.Json.Serialization; @@ -24,6 +25,9 @@ internal sealed record FeatureProfile(string Name, string Architecture, int Nati { internal static FeatureProfile Scalar { get; } = new("scalar", "scalar", 64, true); + // .NET 11 x64 refuses to start without SSE through SSE4.2 and reports them all under X86Base. + private static readonly Feature[] X86Baseline = [Feature.Sse2, Feature.Ssse3, Feature.Sse41, Feature.Sse42]; + internal bool Get(Feature feature) => feature switch { Feature.AdvSimd => AdvSimd, Feature.Sse2 => Sse2, Feature.Ssse3 => Ssse3, @@ -39,6 +43,10 @@ internal sealed record FeatureProfile(string Name, string Architecture, int Nati internal void Validate() { + var missingBaseline = X86Base64 ? X86Baseline.Where(feature => !Get(feature)).ToArray() : []; + if (missingBaseline.Length != 0) + throw new InvalidDataException($"Invalid execution profile: {Name}: below the .NET 11 x64 baseline " + + $"(x86-64-v2); missing {string.Join(", ", missingBaseline)}"); if (string.IsNullOrWhiteSpace(Name) || NativeWidth != 64 || !LittleEndian || Architecture is not ("scalar" or "arm64" or "x64") || ((AdvSimd || ArmBase64) && Architecture != "arm64") || (X86Base64 && Architecture != "x64") || @@ -57,7 +65,7 @@ internal void Validate() if ((Sse2 || Ssse3 || Sse42 || Avx || Avx2 || Avx512F || Avx512FVL || Bmi1 || Sse41 || Avx512DQ || Avx512DQVL || Bmi2 || Avx512Vbmi || Aes) && !X86Base64) throw new InvalidDataException($"Invalid execution profile: {Name}: x86 ISA flags require x86Base64"); - if (Sse2 != X86Base64 || Ssse3 != Sse41 || Sse41 != Sse42 || + if (Sse2 != X86Base64 || Avx2 != Bmi1 || Avx2 != Bmi2 || Avx512F != Avx512FVL || Avx512F != Avx512DQ || Avx512F != Avx512DQVL || AdvSimd != ArmBase64 || Vector128Accelerated != (Sse2 || AdvSimd) || @@ -89,7 +97,6 @@ internal static FeatureProfile Select(string name) { "scalar" => Scalar, "arm64-advsimd" => new(name, "arm64", 64, true, AdvSimd: true, ArmBase64: true, Vector128Accelerated: true), - "x64-sse2" => new(name, "x64", 64, true, Sse2: true, X86Base64: true, Vector128Accelerated: true), "x64-sse42" => new(name, "x64", 64, true, Sse2: true, Ssse3: true, Sse41: true, Sse42: true, X86Base64: true, Vector128Accelerated: true), // .NET 10 enables x86-64-v3 (AVX2 with BMI1/BMI2) and v4 (AVX-512 F/DQ/VL) only as whole groups. // x86-64-v4 excludes VBMI, so x64-v4 covers only deployments where Avx512Vbmi is unsupported. @@ -124,8 +131,6 @@ internal static FeatureProfile Select(string name) internal static string LeanFeature(Feature feature) => "." + char.ToLowerInvariant(feature.ToString()[0]) + feature.ToString()[1..]; - private static string Bool(bool value) => value ? "true" : "false"; - internal static int? PlatformConstant(MethodReference reference) { var pointerSize = reference.Name == "get_Size" && @@ -134,7 +139,7 @@ internal static string LeanFeature(Feature feature) => reference.DeclaringType.FullName == "System.Environment"; if (!pointerSize && !processWidth) return null; var result = pointerSize ? "System.Int32" : "System.Boolean"; - if (!RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime") || + if (!RuntimeModels.ExactScope(reference.DeclaringType, PlatformAssemblies.SystemRuntime) || reference.HasThis || reference.ExplicitThis || reference.CallingConvention != MethodCallingConvention.Default || reference.HasGenericParameters || reference is GenericInstanceMethod || reference.Parameters.Count != 0 || reference.FullName != $"{result} {reference.DeclaringType.FullName}::{reference.Name}()") @@ -143,9 +148,14 @@ internal static string LeanFeature(Feature feature) => return pointerSize ? 8 : 1; } + private static readonly TypeLevel BitConverterOwner = + new(MetadataName.OfText("System"), MetadataName.OfText("BitConverter")); + private static readonly MetadataName EndianName = MetadataName.OfText("IsLittleEndian"); + /// Whether a field load is named BitConverter.IsLittleEndian, before identity checks. internal static bool PlatformFieldQuery(FieldReference reference) => - reference.Name == "IsLittleEndian" && reference.DeclaringType.FullName == "System.BitConverter"; + reference.Name == "IsLittleEndian" && reference.DeclaringType is + { Namespace: "System", Name: "BitConverter", DeclaringType: null }; /// /// The value of the exact System.Runtime BitConverter.IsLittleEndian field; @@ -154,13 +164,16 @@ internal static bool PlatformFieldQuery(FieldReference reference) => internal static int? PlatformField(FieldReference reference) { if (!PlatformFieldQuery(reference)) return null; - const string identity = "System.Boolean System.BitConverter::IsLittleEndian"; var exactReference = reference.DeclaringType is not TypeSpecification && - RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime") && - RuntimeModels.ExactScope(reference.FieldType.Scope, "System.Runtime") && reference.FullName == identity; + IdentityRows.TopLevelMatches(reference.DeclaringType, BitConverterOwner, + PlatformAssemblies.SystemRuntime) && + MetadataName.OfText(reference.Name) == EndianName && + reference.FieldType.MetadataType == MetadataType.Boolean && + RuntimeModels.ExactScope(reference.FieldType, PlatformAssemblies.SystemRuntime); // Resolve only exact references: a forged assembly scope must not reach the resolver. var field = exactReference ? reference.Resolve() : null; - if (field is null || field.FullName != identity || + if (field is null || MetadataName.OfText(field.Name) != EndianName || + field.FieldType.MetadataType != MetadataType.Boolean || !field.IsStatic || !field.IsInitOnly || !field.IsPublic || field.HasConstant || (field.Attributes & FieldAttributes.HasFieldRVA) != 0) throw new InvalidDataException($"Unsupported static field load: {reference.FullName} " + @@ -174,7 +187,7 @@ internal static bool PlatformFieldQuery(FieldReference reference) => if (reference.Name is not ("get_IsSupported" or "get_IsHardwareAccelerated") || !reference.DeclaringType.FullName.StartsWith("System.Runtime.Intrinsics.", StringComparison.Ordinal)) return null; // Never reinterpret an unrecognised getter as a disabled capability. - if (!RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime.Intrinsics") || + if (!RuntimeModels.ExactScope(reference.DeclaringType, PlatformAssemblies.SystemRuntimeIntrinsics) || reference.HasThis || reference.ExplicitThis || reference.CallingConvention != MethodCallingConvention.Default || reference.HasGenericParameters || reference.Parameters.Count != 0 || reference.ReturnType.FullName != "System.Boolean" || diff --git a/Extractor/FrameMetadata.cs b/Extractor/FrameMetadata.cs new file mode 100644 index 000000000..0a190495f --- /dev/null +++ b/Extractor/FrameMetadata.cs @@ -0,0 +1,130 @@ +namespace Nethermind.LeanCIL; + +/// An argument kept in a local slot, with the kind of that slot. +internal sealed record ParameterHome(int Argument, LocalKind Kind) +{ + internal string ToLean() => $"({Argument}, {Kind.ToLean()})"; +} + +/// What a static site reads: its value, an opaque binding, or the data block it addresses. +internal abstract record StaticSiteDetail; + +/// A load whose value the caller binds; the zero bytes of the site fix only the load width. +internal sealed record OpaqueStaticValue(bool? Signed) : StaticSiteDetail; + +/// +/// A load of a value an initializer computes; is null for aggregates and +/// , a method index, is null when a literal gives the value, which the term +/// spells as method 0. +/// +internal sealed record InitializedStaticValue(bool? Signed, int? Initializer, ValueList Arguments, + string? Literal, bool Construct) : StaticSiteDetail; + +/// The address of static data aligned to bytes. +internal sealed record StaticDataBlock(int Alignment) : StaticSiteDetail; + +/// +/// An instruction of a method that reads or addresses a static field: is the field as the +/// instruction's operand names it, and keys its storage. +/// +internal sealed record StaticSite(int Pc, StaticIdentityRecord Identity, FieldRef Field, ValueList Bytes, + StaticSiteDetail Detail) +{ + internal string ToLean() => ToLean(LeanText.MethodNumber); + + /// The initializer method this site reads through, or none. + internal IEnumerable Methods => Detail is InitializedStaticValue { Initializer: { } method } ? [method] : []; + + /// This site with its initializer method replaced by of it. + internal StaticSite Renumber(Func map) => + Detail is InitializedStaticValue { Initializer: { } method } value + ? this with { Detail = value with { Initializer = map(method) } } : this; + + /// The Lean site term, with the initializer written as of it. + internal string ToLean(Func calleeName) => + $"({Pc}, {DescriptorLean(calleeName)})"; + + /// The field identity, storage and origin of the descriptor at this site. + internal string DescriptorLean(Func calleeName) + { + var bytes = StaticData.LeanBytes(Bytes); + string detail; + switch (Detail) + { + case StaticDataBlock block: + detail = $"alignment := {block.Alignment}"; + break; + case OpaqueStaticValue opaque: + detail = $"scalar := {Scalar(opaque.Signed)}, opaqueValue := true"; + break; + case InitializedStaticValue value: + var method = value.Initializer is { } index ? calleeName(index) : "0"; + var literal = value.Literal is null ? "" : $", literal := some ({value.Literal})"; + var construct = value.Construct ? ", construct := true" : ""; + detail = $$""" + scalar := {{Scalar(value.Signed)}}, initializer := some { method := {{method}}, + arguments := [{{string.Join(", ", value.Arguments)}}]{{literal}}{{construct}} } + """.ReplaceLineEndings(" "); + break; + default: + throw new InvalidOperationException("Unknown static site detail"); + } + return + $$""" + { identity := {{Identity.Identity}}, field := {{Field.ToLean()}}, bytes := {{bytes}}, {{detail}} } + """; + } + + static string Scalar(bool? signed) => signed is { } value ? "some " + LeanText.Bool(value) : "none"; +} + +/// +/// The frame of one method: whether it returns a value, its locals with their initial values and kinds, the +/// arguments held in locals, the vector locals whose value stays opaque, the static sites its code reads and +/// the Lean term of its declared signature. The shared layout environment is named by the emitted file. +/// +internal sealed record FrameMetadata(bool ReturnsValue, ValueList Locals, + ValueList LocalKinds, ValueList OpaqueVectorLocals, + ValueList ParameterHomes, ValueList StaticSites, string Declared) +{ + /// The environment value: the file's shared constant, or a template's parameter. + internal const string EnvironmentLean = "environment"; + + /// The method reads or addresses at least one static field. + internal bool HasStaticSites => StaticSites.Count != 0; + + /// The method declares no locals. + internal bool HasNoLocals => Locals.Count == 0; + + /// Argument homes follow the declared locals in this frame's slot order. + internal Dictionary IndexParameterHomes() => + ParameterStorage.IndexHomes(ParameterHomes.Select(home => home.Argument), Locals.Count); + + internal string ReturnsValueLean => LeanText.Bool(ReturnsValue); + + internal string LocalsLean => string.Join(", ", Locals.Select(value => value.ToLean())); + + internal string LocalKindsLean => string.Join(", ", LocalKinds.Select(kind => kind.ToLean())); + + internal string ParameterHomesLean => string.Join(", ", ParameterHomes.Select(home => home.ToLean())); + + /// The index of every method the static sites read initial values through. + internal IEnumerable Methods => StaticSites.SelectMany(site => site.Methods); + + /// This frame with every initializer method index replaced by of it. + internal FrameMetadata Renumber(Func map) => this with + { + StaticSites = [.. StaticSites.Select(site => site.Renumber(map))] + }; + + /// The frame fields, with each initializer written as of it. + internal string ToLean(Func calleeName, string opaqueLocals) => + $""" + returnsValue := {ReturnsValueLean}, locals := [{LocalsLean}], localKinds := [{LocalKindsLean}], + opaqueVectorLocals := {opaqueLocals}, parameterHomes := [{ParameterHomesLean}], + staticSites := [{StaticSitesLeanWith(calleeName)}], declared := {Declared}, environment := {EnvironmentLean} + """.ReplaceLineEndings(" "); + + internal string StaticSitesLeanWith(Func calleeName) => + string.Join(", ", StaticSites.Select(site => site.ToLean(calleeName))); +} diff --git a/Extractor/GenericEvidence.cs b/Extractor/GenericEvidence.cs index a85526b69..360ffc142 100644 --- a/Extractor/GenericEvidence.cs +++ b/Extractor/GenericEvidence.cs @@ -1,6 +1,8 @@ +using System.Buffers.Binary; +using System.Collections.Frozen; using System.Globalization; +using System.Reflection.Metadata.Ecma335; using System.Text; -using System.Text.Json; using Mono.Cecil; using Mono.Cecil.Cil; @@ -11,27 +13,50 @@ namespace Nethermind.LeanCIL; internal sealed class GenericEvidence(PinnedAssemblies? pinned, LayoutEvidence layouts, ImportResolution? imports = null) { - readonly ExternalIdentityEvidence externalIdentities = new(); + // Closed instantiations share their definition's bytes; the first method to use a body names them. + internal ExternalIdentityEvidence ExternalIdentities { get; } = new(); + readonly MetadataSlices? metadata = pinned is null ? null : new(pinned); + readonly Dictionary declaredSignatures = new(); + readonly List statedTypes = []; + int platformAt; int methodParameterCount; + // Closed type operands are shared by the decoder, template descriptors and proof outlines. + readonly Dictionary closedTypes = []; // Type serialization needs no method bodies; only method and dispatch evidence read originals. - ModuleDefinition Original(ModuleDefinition module) => (pinned ?? - throw new InvalidOperationException("Method evidence requires the pinned assemblies.")).Original(module); + PinnedAssemblies Pinned => pinned ?? + throw new InvalidOperationException("Method evidence requires the pinned assemblies."); - static string Quote(string value) => JsonSerializer.Serialize(value); + ModuleDefinition Original(ModuleDefinition module) => Pinned.Original(module); + + /// The Lean string literal of . + internal static string Quote(string value) => IdentityText.Quote(value); static string Bool(bool value) => value.ToString().ToLowerInvariant(); static string List(IEnumerable entries) => "[" + string.Join(", ", entries) + "]"; - static string Scope(TypeReference type) - { - var scope = LayoutEvidence.Scope(type); - return scope == TypeTests.RuntimeScope ? "Extraction.runtimeScope" : Quote(scope); - } + /// The Lean term of a type's assembly scope; a platform scope is the named string the decoder + /// writes, so evidence and a decoded method spell the same scope with the same term. + internal static string Scope(TypeReference type) => Scope(LayoutEvidence.Scope(type)); + + /// The platform assembly scopes the Lean library names, with the term of each. + static readonly (string Assembly, string Term)[] NamedScopes = + [ + (TypeTests.RuntimeScope, "Extraction.runtimeScope"), + (PlatformAssemblies.SystemRuntimeIntrinsics.ToDisplay(), "Extraction.intrinsicsScope"), + (PlatformAssemblies.SystemMemory.ToDisplay(), "Extraction.spanMemoryScope"), + ]; + + static string Scope(string assembly) => + NamedScopes.FirstOrDefault(scope => scope.Assembly == assembly).Term ?? Quote(assembly); internal string Type(TypeReference type) => Type(type, false); /// The type expression naming a class instance type, as field operations carry it. - internal static string ObjectType(TypeReference type) => $".object {Scope(type)} {Quote(type.FullName)}"; + internal static string ObjectType(TypeReference type) => ObjectType(LayoutEvidence.Scope(type), type.FullName); + + /// The type expression naming the class of the assembly + /// . + internal static string ObjectType(string assembly, string name) => $".object {Scope(assembly)} {Quote(name)}"; // The kernel sees an `in` reference as the reference it is; the extractor compares the modifier exactly. string Parameter(TypeReference type) => Type(InReferences.Parameter(type)); @@ -40,54 +65,132 @@ static string Scope(TypeReference type) static TypeReference Returned(MethodReference reference) => InitAccessors.Return(CallReturnType.Resolve(reference)); - string Type(TypeReference type, bool preserveGeneric) + string Type(TypeReference type, bool preserveGeneric, bool declared = false) + => RenderType(type, preserveGeneric, declared, methodParameterCount, layouts); + + // Callable identities name closed aggregates independently of any extraction's layout-table order. + internal static string CallableArgument(TypeReference type) => RenderType(type, false, false, 0, null); + + static string RenderType(TypeReference type, bool preserveGeneric, bool declared, int methodParameterCount, + LayoutEvidence? layouts) { + string Nested(TypeReference element) => RenderType(element, false, declared, methodParameterCount, layouts); + string? Aggregate(bool localOnly = false) + { + if (layouts is null) + return MetadataValidation.IsAggregate(type, type.Module) + ? $".aggregate {Quote(LayoutEvidence.Identity(type))} 0" : null; + var index = layouts.Index(type); + return index >= 0 && (!localOnly || !layouts.Metadata.IsImported(type)) + ? $".aggregate {layouts.IdentityTerm(index)} {index}" : null; + } + if (declared && type is IModifierType modifier) + { + var required = Bool(type is RequiredModifierType); + var name = Quote(modifier.ModifierType.FullName); + return $".modified {required} {Scope(modifier.ModifierType)} {name} ({Nested(modifier.ElementType)})"; + } if (type is FunctionPointerType callable) { var parameters = ".void"; foreach (var argument in callable.Parameters.Reverse()) - parameters = $".signatureParameter ({Type(argument.ParameterType)}) ({parameters})"; - return $".functionPointer ({Type(callable.ReturnType)}) ({parameters}) " + - $"{(int)callable.CallingConvention} {callable.HasThis.ToString().ToLowerInvariant()} " + - callable.ExplicitThis.ToString().ToLowerInvariant(); + parameters = $".signatureParameter ({Nested(argument.ParameterType)}) ({parameters})"; + return $""" + .functionPointer ({Nested(callable.ReturnType)}) ({parameters}) + {(int)callable.CallingConvention} {Bool(callable.HasThis)} {Bool(callable.ExplicitThis)} + """.ReplaceLineEndings(" "); } if (type is GenericParameter parameter) - return $".parameter {parameter.Position + - (parameter.Type == GenericParameterType.Method ? 0 : methodParameterCount)}"; - if (type is ByReferenceType byRef) return $".byref ({Type(byRef.ElementType)})"; + { + var ownerParameter = parameter.Type == GenericParameterType.Type; + if (declared && ownerParameter) return $".ownerParameter {parameter.Position}"; + var offset = ownerParameter ? methodParameterCount : 0; + return $".parameter {parameter.Position + offset}"; + } + if (type is ByReferenceType byRef) return $".byref ({Nested(byRef.ElementType)})"; // An element class outside the model is named as an object, as the array's reference slots hold it. if (type is ArrayType { IsVector: true } array) - return $".array ({(ReferenceArrays.Is(array) ? ObjectType(array.ElementType) : Type(array.ElementType))})"; - if (type is PointerType pointer) return $".pointer ({Type(pointer.ElementType)})"; - if (ValueTuples.Is(type) && layouts.Index(type) is >= 0 and var tuple) - return $".aggregate {Quote(LayoutEvidence.Identity(type))} {tuple}"; + { + var element = ReferenceArrays.Is(array) ? ObjectType(array.ElementType) : Nested(array.ElementType); + return $".array ({element})"; + } + if (type is PointerType pointer) return $".pointer ({Nested(pointer.ElementType)})"; + if (ValueTuples.Is(type) && Aggregate() is { } tuple) return tuple; if (type is GenericInstanceType generic) { - if (!preserveGeneric && GenericStructs.Is(type) && layouts.Index(type) is >= 0 and var aggregate) - return $".aggregate {Quote(LayoutEvidence.Identity(type))} {aggregate}"; - var result = $".constructor {Scope(generic.ElementType)} {Quote(generic.ElementType.FullName)}"; - foreach (var argument in generic.GenericArguments) result = $".apply ({result}) ({Type(argument)})"; + if (!preserveGeneric && GenericStructs.Is(type) && Aggregate() is { } aggregate) return aggregate; + var result = $"{Head(generic)} {Scope(generic.ElementType)} {Quote(generic.ElementType.FullName)}"; + foreach (var argument in generic.GenericArguments) result = $".apply ({result}) ({Nested(argument)})"; return result; } if (type.FullName == "System.Void") return ".void"; if (EnumTypes.IsEnum(type)) { var underlying = type.Resolve().Fields.Single(field => !field.IsStatic).FieldType; - return $".enum {Quote(LayoutEvidence.Identity(type))} ({Type(underlying)})"; + return $".enum {Quote(LayoutEvidence.Identity(type))} ({Nested(underlying)})"; } if (ObjectTypes.Reference(type)) return ObjectType(type); - var index = layouts.Index(type); - if (index >= 0 && !layouts.Metadata.IsImported(type)) - return $".aggregate {Quote(LayoutEvidence.Identity(type))} {index}"; - return $".atom {Scope(type)} {Quote(type.FullName)}"; + return Aggregate(localOnly: true) ?? $".atom {Scope(type)} {Quote(type.FullName)}"; } + // The head says whether the instances are values or references, as the signature's marker does. + static string Head(GenericInstanceType generic) => + generic.ElementType.IsValueType ? ".constructor" : ".objectConstructor"; + // Signatures name an imported layout by scoped atom, as the import boundary requires; only a // local declaration binds it to the table entry that sizes its storage. - string LocalType(TypeReference type) => layouts.Metadata.IsImported(type) - ? $".aggregate {Quote(LayoutEvidence.Identity(type))} {layouts.Index(type)}" : Type(type); + string LocalType(TypeReference type) + { + if (!layouts.Metadata.IsImported(type)) return Type(type); + int index = layouts.Index(type); + if (index < 0) throw new InvalidDataException($"Missing imported layout for {type.FullName}"); + return $".aggregate {layouts.IdentityTerm(index)} {index}"; + } + + // The member in the untouched body retains its blob even when the call graph specializes its operand. + string DeclaredSignature(MethodReference original) + { + var member = original.GetElementMethod(); + if (declaredSignatures.TryGetValue(member, out var signature)) return signature; + var convention = (byte)member.CallingConvention | + (member.HasThis ? 0x20 : 0) | (member.ExplicitThis ? 0x40 : 0); + string DeclaredType(TypeReference type) => Type(type, true, true); + signature = $$""" + { convention := {{convention}}, returnType := {{DeclaredType(member.ReturnType)}}, + parameters := {{List(member.Parameters.Select(parameter => DeclaredType(parameter.ParameterType)))}} } + """.ReplaceLineEndings(" "); + declaredSignatures.Add(member, signature); + return signature; + } + + string Call(MethodReference reference, MethodReference original, Instruction instruction, + IReadOnlySet reachable) + { + var (method, name, owners, extracted, typeArguments) = MethodOperand(reference); + var definition = reference is GenericInstanceMethod generic ? generic.ElementMethod : reference; + var resolved = extracted ? definition.Resolve() ?? + throw new InvalidDataException("Unresolved pinned method: " + definition.FullName) : null; + var slot = InstructionPrefixes.IsStaticVirtualSlot(resolved) ? "staticVirtualInterfaceSlot := true, " : ""; + var target = extracted + ? $".managed {resolved!.MetadataToken.ToUInt32()}" + : ".external " + ExternalIdentities.Reference(imports?.Identity(instruction) ?? + (LayoutEvidence.Scope(definition.DeclaringType) + "::" + ExternalSignature(definition)), + reachable.Contains(instruction)); + return $$""" + { method := {{method.ToLean()}}, name := {{Quote(name)}}, target := {{target}}, + typeArguments := {{typeArguments}}, ownerArgumentCount := {{owners}}, + {{slot}}parameterCount := {{reference.Parameters.Count}}, + returnsValue := {{Bool(Returned(reference).FullName != "System.Void")}}, + declared := {{DeclaredSignature(original)}} } + """.ReplaceLineEndings(" "); + } - string Call(MethodReference reference, Instruction instruction, IReadOnlySet reachable) + /// + /// The typed method a call operand names, its name as text, the number of its owner's type arguments, whether its + /// callee is extracted, and the Lean list of its type arguments, method arguments first. + /// + (MethodRef Method, string Name, int Owners, bool Extracted, string TypeArguments) MethodOperand( + MethodReference reference) { var definition = reference is GenericInstanceMethod generic ? generic.ElementMethod : reference; var arguments = reference is GenericInstanceMethod instance ? instance.GenericArguments.ToArray() : @@ -107,15 +210,26 @@ reference.DeclaringType is GenericInstanceType owner foreach (var argument in owners) closedOwner.GenericArguments.Add(argument); declaringType = closedOwner; } - var target = extracted - ? $".managed {definition.Resolve().MetadataToken.ToUInt32()}" - : ".external " + externalIdentities.Reference(imports?.Identity(instruction) ?? - (LayoutEvidence.Scope(definition.DeclaringType) + "::" + ExternalSignature(definition)), - reachable.Contains(instruction)); - var name = extracted - ? ((MethodDefinition)Original(definition.Resolve().Module) - .LookupToken(definition.Resolve().MetadataToken)).Name : definition.Name; - return $"{{ declaringType := {Type(declaringType, true)}, name := {Quote(name)}, target := {target}, typeArguments := {List(arguments.Select(Type))}, ownerArgumentCount := {owners.Length}, parameterCount := {reference.Parameters.Count}, hasThis := {Bool(reference.HasThis)}, returnsValue := {Bool(Returned(reference).FullName != "System.Void")}, returnType := {Type(Returned(reference))}, parameterTypes := {List(CallReturnType.Parameters(reference).Select(Parameter))} }}"; + var resolved = extracted ? definition.Resolve() : null; + var name = resolved is null ? definition.Name : + ((MethodDefinition)Original(resolved.Module).LookupToken(resolved.MetadataToken)).Name; + var genericArity = (reference is MethodDefinition own ? MethodSpecialization.Definition(own) : definition) + .GenericParameters.Count; + // A cross-module MemberRef and its MethodDef can use different core assemblies for primitive elements. + var signature = reference; + if (resolved is not null && resolved.Module != reference.Module && genericArity == 0 && owners.Length == 0) + { + signature = resolved; + declaringType = resolved.DeclaringType; + } + // Rendering a type can number its layout. + var ownerType = Type(declaringType, true); + var typeArguments = List(arguments.Select(Type)); + var returnType = Type(Returned(signature)); + ValueList parameters = [.. CallReturnType.Parameters(signature).Select(Parameter)]; + ValueList methodArguments = [.. arguments[..^owners.Length].Select(Type)]; + return (new(ownerType, MetadataName.OfText(name), reference.HasThis, genericArity, returnType, parameters, + methodArguments), name, owners.Length, extracted, typeArguments); } // An open owner spells caller parameters by position, so the certificate can instantiate it; @@ -141,8 +255,97 @@ static string ExternalSignature(MethodReference definition) _ => type.FullName }; + /// + /// The declaring type, model index, held type and static flag of a field operand, which the raw operand and the + /// typed field reference both state. + /// + (string Owner, int Index, string Held, bool IsStatic) FieldOperand(Instruction instruction, FieldReference field) + { + var isStatic = instruction.OpCode.Code is Code.Ldsfld or Code.Stsfld or Code.Ldsflda or Code.Ldtoken; + var index = 0; + // A field signature's `!n` is the declaring type's parameter: the declaring instance's argument. + var fieldType = field.FieldType.ContainsGenericParameter && + field.DeclaringType is GenericInstanceType instance + ? GenericStructs.Substitute((field.Resolve() ?? throw new InvalidDataException( + "Unresolved generic field: " + field.FullName)).FieldType, instance) : field.FieldType; + if (Carried(field)) + { + var resolved = field.Resolve(); + index = layouts.Metadata.FieldIndex(field.DeclaringType, resolved, isStatic); + if (!isStatic) fieldType = layouts.Metadata.Fields(field.DeclaringType)[index].Type; + // A class field's index counts its bases' fields first, as its model slot does. + if (!isStatic && ObjectTypes.Is(field.DeclaringType)) + index = ObjectTypes.FieldIndex(field.DeclaringType, resolved); + } + else if (!isStatic && ValueTuples.Is(field.DeclaringType)) + { + // Tuple fields are typed by the instance's arguments, not the definition's T1/T2. + var fields = layouts.Metadata.Fields(field.DeclaringType); + index = layouts.Metadata.FieldIndex(field.DeclaringType, field.Resolve()); + if (index < 0) throw new InvalidDataException("Unresolved tuple field: " + field.FullName); + fieldType = fields[index].Type; + } + // Lean refuses such an operand at every pc, reachable or not, so the access is a stated blocker. + else if (!(instruction.OpCode.Code == Code.Ldsfld && FeatureProfile.PlatformField(field) is not null)) + throw new InvalidDataException( + $"IL_{instruction.Offset:x4}: Unsupported field access: {field.FullName}\n" + + " Its owner has no carried definition, so no metadata row gives the field's position."); + // A class field of a type outside the model holds an object reference like an admitted one. + var heldType = !isStatic && ObjectTypes.Is(field.DeclaringType) && ObjectTypes.Opaque(fieldType) && + fieldType is not GenericInstanceType ? ObjectType(fieldType) : Type(fieldType); + return (Type(field.DeclaringType), index, heldType, isStatic); + } + + // A carried slice defines the field: its owner is in the method's module or a pinned assembly. + static bool Carried(FieldReference field) => field.DeclaringType.Scope is ModuleDefinition || + PinnedAssemblies.Active?.ModuleOf(field.DeclaringType) is not null; + + // The static accesses whose field a carried row defines, which the Lean decoder records by instruction index. + static bool BoundStatic(Instruction instruction) => + instruction.OpCode.Code is Code.Ldsfld or Code.Stsfld or Code.Ldsflda or Code.Ldtoken && + instruction.Operand is FieldReference field && Carried(field); + + /// + /// The typed reference of the field operand at in , the owner, + /// index and held type its raw operand states and the name of the row the original body's token names. + /// + internal FieldRef FieldReferenceOf(MethodDefinition method, int pc) + { + methodParameterCount = method.GenericParameters.Count; + var instruction = method.Body.Instructions[pc]; + var (owner, index, held, _) = FieldOperand(instruction, (FieldReference)instruction.Operand); + return new(owner, index, held, FieldName(method, pc)); + } + + /// + /// The name of the Field or MemberRef row the field operand at names in the image of + /// 's definition; the metadata check spells the operand's name against it. + /// + MetadataName FieldName(MethodDefinition method, int pc) + { + // A specialization's operands are references without a token; its definition's body holds the token. + var original = (MethodDefinition)Original(method.Module).LookupToken(method.MetadataToken); + var field = (FieldReference)original.Body.Instructions[pc].Operand; + var image = Pinned.Metadata(original.Module); + var token = field.MetadataToken; + if (token.RID == 0) + throw new InvalidDataException("Field operand without a metadata row: " + field.FullName); + return token.TokenType switch + { + TokenType.Field => IdentityRows.Name(image, + image.Reader.GetFieldDefinition(MetadataTokens.FieldDefinitionHandle((int)token.RID)).Name), + TokenType.MemberRef => IdentityRows.Name(image, + image.Reader.GetMemberReference(MetadataTokens.MemberReferenceHandle((int)token.RID)).Name), + _ => throw new InvalidDataException("Field operand without a Field or MemberRef row: " + field.FullName) + }; + } + + // An element class outside the model is named as an object, as the array's reference slots hold it. + string ArrayElement(TypeReference type) => ReferenceArrays.Element(type) ? ObjectType(type) : Type(type); + string Operand(MethodDefinition method, InstructionIndex instructions, Instruction instruction, - Instruction admittedInstruction, IReadOnlySet reachable) + Instruction original, Instruction admittedInstruction, IReadOnlySet reachable, + Dictionary? typeOperands = null) { switch (instruction.Operand) { @@ -156,40 +359,20 @@ string Operand(MethodDefinition method, InstructionIndex instructions, Instructi if (argument < 0) throw new InvalidDataException("Invalid raw argument index"); return $".argument {argument}"; case VariableDefinition local: return $".local {local.Index}"; - case TypeReference type when instruction.OpCode.Code == Code.Newarr && ReferenceArrays.Element(type): - return $".type ({ObjectType(type)})"; - case TypeReference type: return $".type ({Type(type)})"; - case MethodReference callee: return $".call {Call(callee, admittedInstruction, reachable)}"; + case TypeReference type: + var term = instruction.OpCode.Code == Code.Newarr ? ArrayElement(type) : Type(type); + typeOperands?.Add(admittedInstruction, term); + return $".type ({term})"; + case MethodReference callee: + return $".call {Call(callee, (MethodReference)original.Operand, admittedInstruction, reachable)}"; case Mono.Cecil.CallSite site when instruction.OpCode.Code == Code.Calli: return $".indirectCall ({Type(site.ReturnType)}) "+ $"{List(site.Parameters.Select(parameter => Type(parameter.ParameterType)))} "+ $"{Bool(site.HasThis)} {Bool(site.ExplicitThis)} {(int)site.CallingConvention}"; case FieldReference field: - var isStatic = instruction.OpCode.Code is Code.Ldsfld or Code.Stsfld or Code.Ldsflda or Code.Ldtoken; - var index = 0; - var fieldType = field.FieldType; - if (field.DeclaringType.Scope is ModuleDefinition || - PinnedAssemblies.Active?.ModuleOf(field.DeclaringType) is not null) - { - var resolved = field.Resolve(); - index = layouts.Metadata.FieldIndex(field.DeclaringType, resolved, isStatic); - if (!isStatic) fieldType = layouts.Metadata.Fields(field.DeclaringType)[index].Type; - // A class field's index counts its bases' fields first, as its model slot does. - if (!isStatic && ObjectTypes.Is(field.DeclaringType)) - index = ObjectTypes.FieldIndex(field.DeclaringType, resolved); - } - else if (!isStatic && ValueTuples.Is(field.DeclaringType)) - { - // Tuple fields are typed by the instance's arguments, not the definition's T1/T2. - var fields = layouts.Metadata.Fields(field.DeclaringType); - index = layouts.Metadata.FieldIndex(field.DeclaringType, field.Resolve()); - if (index < 0) throw new InvalidDataException("Unresolved tuple field: " + field.FullName); - fieldType = fields[index].Type; - } - // A class field of a type outside the model holds an object reference like an admitted one. - var heldType = !isStatic && ObjectTypes.Is(field.DeclaringType) && ObjectTypes.Opaque(fieldType) - ? ObjectType(fieldType) : Type(fieldType); - return $".field ({Type(field.DeclaringType)}) {index} ({heldType}) {Bool(isStatic)}"; + var (owner, index, held, isStatic) = FieldOperand(instruction, field); + var name = FieldName(method, instructions.Position(instruction)).ToLean(); + return $".field ({owner}) {index} ({held}) {name} {Bool(isStatic)}"; case string text: return $".text {Quote(text)}"; case float number: return $".text {Quote("float:" + BitConverter.SingleToInt32Bits(number).ToString(CultureInfo.InvariantCulture))}"; case double number: return $".text {Quote("double:" + BitConverter.DoubleToInt64Bits(number).ToString(CultureInfo.InvariantCulture))}"; @@ -199,20 +382,27 @@ string Operand(MethodDefinition method, InstructionIndex instructions, Instructi } } + /// + /// The type term of the type operand of , as the last method's + /// method{n}Closed states it; the emitted instruction must carry the same term. + /// + internal string ClosedOperandOf(Instruction instruction) => + closedTypes.TryGetValue(instruction, out var term) ? term + : throw new InvalidDataException("No closed operand term for the instruction at " + instruction.Offset); + internal static string ConstraintType(TypeReference type) => type is RequiredModifierType modifier ? LayoutEvidence.Identity(modifier.ModifierType) + "!" + LayoutEvidence.Identity(modifier.ElementType) : LayoutEvidence.InterfaceIdentity(type); + static readonly string ValueTypeConstraint = TypeTests.RuntimeScope + "::System.ValueType"; + static readonly string UnmanagedTypeConstraint = TypeTests.RuntimeScope + + "::System.Runtime.InteropServices.UnmanagedType!" + ValueTypeConstraint; + static string Constraint(TypeReference type) { var identity = ConstraintType(type); - return identity switch - { - TypeTests.RuntimeScope + "::System.ValueType" => "Extraction.valueTypeConstraint", - TypeTests.RuntimeScope + "::System.Runtime.InteropServices.UnmanagedType!" + - TypeTests.RuntimeScope + "::System.ValueType" => "Extraction.unmanagedTypeConstraint", - _ => Quote(identity) - }; + return identity == ValueTypeConstraint ? "Extraction.valueTypeConstraint" : + identity == UnmanagedTypeConstraint ? "Extraction.unmanagedTypeConstraint" : Quote(identity); } // A constraint naming generic parameters, such as `T : I`, is closed per instantiation in Lean. @@ -225,7 +415,11 @@ string ParameterConstraint(GenericParameter parameter) (open.Length == 0 ? "" : $", interfaces := {List(open)}") + " }"; } - string Method(MethodDefinition method, MethodDefinition admitted, IReadOnlySet reachable) + string Locals(MethodDefinition method) => + List(method.Body.Variables.Select(local => LocalType(local.VariableType))); + + string Method(MethodDefinition method, MethodDefinition original, MethodDefinition admitted, + IReadOnlySet reachable, string staticInput, Dictionary? typeOperands = null) { methodParameterCount = method.GenericParameters.Count; var instructions = new InstructionIndex(method); @@ -240,17 +434,18 @@ string Method(MethodDefinition method, MethodDefinition admitted, IReadOnlySet Parameter(parameter.ParameterType))); - var locals = List(method.Body.Variables.Select(local => LocalType(local.VariableType))); + var (parameters, returnType) = SignatureTypes(method); + var locals = Locals(method); // Lines break only after a comma, so the emitted definition stays on one line. return $$""" { token := {{method.MetadataToken.ToUInt32()}}, owner := {{owner}}, @@ -261,28 +456,397 @@ string Method(MethodDefinition method, MethodDefinition admitted, IReadOnlySet + method.HasThis && ObjectTypes.Is(method.DeclaringType); + + // The generic context is set here, so a type-level parameter is numbered by this method, not the last one. + (string Parameters, string ReturnType) SignatureTypes(MethodDefinition method) + { + methodParameterCount = method.GenericParameters.Count; + return (List(method.Parameters.Select(parameter => Parameter(parameter.ParameterType))), + Type(InitAccessors.Return(method.ReturnType))); + } + + /// + /// The method's DeclaredSignature: the receiver kind, types and localsinit flag of method{n}Closed. Fields at + /// their default are omitted. + /// + internal string Declared(MethodDefinition method) + { + var receiver = method.HasThis + ? $"receiver := some .{(ObjectReceiver(method) ? "object" : "reference")}, " : ""; + var (parameters, returnType) = SignatureTypes(method); + var initLocals = method.Body.InitLocals ? "" : ", initLocals := false"; + return $"{{ {receiver}parameters := {parameters}, returnType := {returnType}{initLocals} }}"; + } + internal void EmitMethod(StringBuilder lean, MethodDefinition method, int number, - IReadOnlySet reachable) + IReadOnlySet reachable, int methodCount, string staticInput, StringBuilder? decoderInputs = null) { var original = (MethodDefinition)Original(method.Module).LookupToken(method.MetadataToken); - var originalEvidence = Method(original, method, reachable); - var closedEvidence = Method(method, method, reachable); - externalIdentities.Emit(lean); + var originalEvidence = Method(original, original, method, reachable, staticInput); + // Token operands are spelled in the original's generic context, as its code is. + var body = Body(original, method, reachable, number); + closedTypes.Clear(); + var closedEvidence = Method(method, original, method, reachable, staticInput, closedTypes); + ExternalIdentities.Emit(lean, decoderInputs); lean.AppendLine($"def method{number}Original : Extraction.RawMethod := {originalEvidence}"); + lean.AppendLine(body.Bytes); + if (body.Slices.Length > 0) + foreach (var line in body.Slices.Split('\n')) lean.AppendLine(line); + var hasImports = imports?.Bindings.Count > 0; + var bodyImports = hasImports ? $""" method{number}BodyImports""" : ""; + if (hasImports) + lean.AppendLine($""" + /-- The imported calls selected by this method's direct links. -/ + def method{number}BodyImports : Extraction.BodyImports := + importIndex.forLinks {methodCount} method{number}Links + """); + lean.AppendLine(body.Definition); lean.AppendLine($"def method{number}TypeArgumentsRaw : List Extraction.TypeExpr := {List(MethodSpecialization.Arguments(method).Select(Type))}"); - lean.AppendLine($"def method{number}Closed : Extraction.RawMethod := {closedEvidence}"); + var closed = $"def method{number}Closed : Extraction.RawMethod := {closedEvidence}"; + lean.AppendLine(closed); + decoderInputs?.AppendLine(closed); lean.AppendLine($"def method{number}DispatchTypes : List Extraction.DispatchType := {DispatchTypes(method)}"); // Direct equality proofs avoid a separate structural decision over the raw metadata. - lean.AppendLine($"theorem method{number}_specialization_validated :" + - $" Extraction.SpecializationValid rawLayouts method{number}Original method{number}TypeArgumentsRaw" + - $" method{number}Closed method{number}RawArguments := by constructor <;> rfl"); + lean.AppendLine(string.Join(" ", + $"""theorem method{number}_specialization_validated : Extraction.SpecializationValid rawLayouts""", + $"""method{number}Original method{number}TypeArgumentsRaw method{number}Closed""", + $"""method{number}RawArguments := by constructor <;> cil_kernel_rfl""")); + var assembly = $"\u27e8{Quote(PinIdentity(method.Module))}\u27e9"; + var proofs = string.Join(", ", body.Identified, body.TablesValid, "by cil_kernel_rfl", + "pinned_identities_spelled", "rfl", "by cil_kernel_rfl", "by cil_kernel_rfl", "by cil_body_decodes"); + lean.AppendLine(string.Join(" ", + $"""theorem method{number}_body_validated : Extraction.BodyDecodes assemblies platformDefinitions""", + $"""{assembly} method{number}Body method{number}Original method{number}TypeArgumentsRaw""", + $"""method{number}Signature{bodyImports} :=""", $"\u27e8{proofs}\u27e9")); + } + + /// The decoded value opcodes whose operands need no storage, model or target resolution. + internal static readonly FrozenSet SimpleOpcodes = new[] + { + Code.Nop, Code.Dup, Code.Pop, Code.Ret, + Code.Ldarg_0, Code.Ldarg_1, Code.Ldarg_2, Code.Ldarg_3, + Code.Ldloc_0, Code.Ldloc_1, Code.Ldloc_2, Code.Ldloc_3, + Code.Stloc_0, Code.Stloc_1, Code.Stloc_2, Code.Stloc_3, + Code.Localloc, + Code.Ldc_I4_M1, Code.Ldc_I4_0, Code.Ldc_I4_1, Code.Ldc_I4_2, Code.Ldc_I4_3, Code.Ldc_I4_4, Code.Ldc_I4_5, + Code.Ldc_I4_6, Code.Ldc_I4_7, Code.Ldc_I4_8, Code.Ldnull, + Code.Ldind_I1, Code.Ldind_U1, Code.Ldind_I2, Code.Ldind_U2, Code.Ldind_I4, Code.Ldind_U4, Code.Ldind_I8, + Code.Ldind_I, Code.Stind_I1, Code.Stind_I2, Code.Stind_I4, Code.Stind_I8, Code.Stind_I, + Code.Ldlen, Code.Ldelem_I1, Code.Ldelem_U1, Code.Ldelem_I2, Code.Ldelem_U2, Code.Ldelem_I4, Code.Ldelem_U4, + Code.Ldelem_I8, Code.Ldelem_I, Code.Ldelem_Ref, Code.Stelem_I, Code.Stelem_I1, Code.Stelem_I2, + Code.Stelem_I4, Code.Stelem_I8, Code.Stelem_Ref, + Code.Readonly, + Code.Br_S, Code.Brfalse_S, Code.Brtrue_S, Code.Beq_S, Code.Bge_S, Code.Bgt_S, Code.Ble_S, Code.Blt_S, + Code.Bne_Un_S, Code.Bge_Un_S, Code.Bgt_Un_S, Code.Ble_Un_S, Code.Blt_Un_S, + Code.Br, Code.Brfalse, Code.Brtrue, Code.Beq, Code.Bge, Code.Bgt, Code.Ble, Code.Blt, + Code.Bne_Un, Code.Bge_Un, Code.Bgt_Un, Code.Ble_Un, Code.Blt_Un, + Code.Add, Code.Sub, Code.Mul, Code.Div, Code.Div_Un, Code.Rem, Code.Rem_Un, Code.And, Code.Or, Code.Xor, + Code.Shl, Code.Shr, Code.Shr_Un, Code.Neg, Code.Not, + Code.Add_Ovf, Code.Add_Ovf_Un, Code.Mul_Ovf, Code.Mul_Ovf_Un, Code.Sub_Ovf, Code.Sub_Ovf_Un, + Code.Ceq, Code.Cgt, Code.Cgt_Un, Code.Clt, Code.Clt_Un, + Code.Conv_I1, Code.Conv_I2, Code.Conv_I4, Code.Conv_I8, Code.Conv_I, + Code.Conv_U1, Code.Conv_U2, Code.Conv_U4, Code.Conv_U8, Code.Conv_U, + Code.Conv_Ovf_I1, Code.Conv_Ovf_I2, Code.Conv_Ovf_I4, Code.Conv_Ovf_I8, Code.Conv_Ovf_I, + Code.Conv_Ovf_U1, Code.Conv_Ovf_U2, Code.Conv_Ovf_U4, Code.Conv_Ovf_U8, Code.Conv_Ovf_U, + Code.Conv_Ovf_I1_Un, Code.Conv_Ovf_I2_Un, Code.Conv_Ovf_I4_Un, Code.Conv_Ovf_I8_Un, Code.Conv_Ovf_I_Un, + Code.Conv_Ovf_U1_Un, Code.Conv_Ovf_U2_Un, Code.Conv_Ovf_U4_Un, Code.Conv_Ovf_U8_Un, Code.Conv_Ovf_U_Un, + }.ToFrozenSet(); + + /// + /// The opcodes Lean's `Instruction.ofRaw?` decodes for some operand, the set `Instruction.valueOpcode` holds; + /// `Instruction.ofRaw` writes a hole for every other one. Tests/DecodedOpcodes.cs compares the two. + /// + internal static readonly FrozenSet DecodedOpcodes = SimpleOpcodes.Concat(new[] + { + Code.Ldarg_S, Code.Ldarg, Code.Ldarga_S, Code.Ldarga, Code.Starg_S, Code.Starg, + Code.Ldloc_S, Code.Ldloc, Code.Stloc_S, Code.Stloc, Code.Ldloca_S, Code.Ldloca, + Code.Ldc_I4_S, Code.Ldc_I4, Code.Ldc_I8, Code.Ldstr, Code.Newarr, Code.Ldelem_Any, + Code.Stelem_Any, Code.Ldelema, Code.Ldobj, Code.Stobj, Code.Initobj, Code.Cpobj, Code.Sizeof, + Code.Castclass, Code.Isinst, Code.Ldfld, Code.Ldflda, Code.Stfld, Code.Call, Code.Constrained, + Code.Callvirt, Code.Newobj, Code.Ldftn, Code.Calli, Code.Switch, + Code.Cpblk, Code.Initblk, Code.Unaligned, Code.Ldtoken + }).ToFrozenSet(); + + /// The opcodes extraction takes the decoder to read: `DecodedOpcodes`, unless a test narrows the set in + /// its own process to drive a decoder refusal through the extractor. + internal static IReadOnlySet ExtractedOpcodes { get; set; } = DecodedOpcodes; + + /// A reachable opcode has no decoder, or its call operand has no matching resolved target. + internal enum RefusalKind { Hole, UnboundCall } + + /// The first live pc, in body order, at which the decoder gives no instruction, with that + /// instruction. + internal sealed record DecodeRefusal(int Pc, Instruction At, RefusalKind Kind); + + /// The extraction refusal of a method whose decoder refuses a live pc; an admitted method is always + /// defined from its decoder, so no extraction is written for it. + internal sealed class UndecodedInstruction(MethodDefinition method, DecodeRefusal refusal) : Exception($""" + IL_{refusal.At.Offset:x4}: Unsupported instruction: {Reason(refusal)} + at pc {refusal.Pc} of {method.FullName} + """) + { + /// The method whose decoder refuses the instruction. + internal MethodDefinition Method { get; } = method; + + /// The refused pc and instruction. + internal DecodeRefusal Refusal { get; } = refusal; + + static string Reason(DecodeRefusal refusal) => refusal.Kind == RefusalKind.Hole + ? $"the decoder has no form for {refusal.At.OpCode.Name}" + : $"the decoder cannot bind the {refusal.At.OpCode.Name} operand with its prefixes"; + } + + /// + /// The first live instruction without an admitted opcode, field or call binding. Dead sites keep their raw opcode. + /// + internal static DecodeRefusal? FirstDecodeRefusal(MethodDefinition method, IReadOnlySet reachable, + ResolvedCallSites calls, ImportResolution? imports) + { + var instructions = method.Body.Instructions; + var throws = Throws.Analyze(method); + for (var pc = 0; pc < instructions.Count; pc++) + { + var instruction = instructions[pc]; + if (!reachable.Contains(instruction)) continue; + if (throws.Operands.Contains(instruction) || throws.Raises.ContainsKey(instruction)) + continue; + if (instruction.OpCode.Code == Code.Tail && instruction.Next?.OpCode.Code is Code.Call or Code.Calli) + continue; + if (instruction.OpCode.Code is Code.Ldsfld or Code.Ldsflda || + instruction.OpCode.Code == Code.Ldtoken && instruction.Operand is FieldReference) + continue; + if (!ExtractedOpcodes.Contains(instruction.OpCode.Code) || UndecodedFieldAccess(instruction)) + return new(pc, instruction, RefusalKind.Hole); + if (!calls.BindsOperand(instruction, imports)) return new(pc, instruction, RefusalKind.UnboundCall); + } + return null; + } + + // The decoder reads a field instruction on any instance field except one of a generic class instance, whose + // raw owner `Type` writes as an object application. A wrong answer fails at `cil_decoded_method`. + static bool UndecodedFieldAccess(Instruction instruction) => + instruction.OpCode.Code is Code.Ldfld or Code.Ldflda or Code.Stfld && + ((FieldReference)instruction.Operand).DeclaringType is GenericInstanceType owner && + ObjectTypes.Reference(owner); + + static readonly HashSet EmptySet = []; + + // A body's metadata tokens are the four bytes after the opcode of these operand kinds. + static bool HasToken(OpCode opcode) => opcode.OperandType is OperandType.InlineMethod or OperandType.InlineField + or OperandType.InlineType or OperandType.InlineString or OperandType.InlineSig or OperandType.InlineTok; + + /// + /// The body bytes of an original method and its token table: each token read from the body, with the operand + /// the extractor resolved it to. The Lean decoder reads the same bytes and checks them against the code. + /// + (string Bytes, string Slices, string Definition, string Identified, string TablesValid) Body( + MethodDefinition original, MethodDefinition admitted, + IReadOnlySet reachable, int number) + { + var body = Pinned.Body(original); + var bytes = body.Bytes; + var instructions = new InstructionIndex(original); + var operands = new List<(uint Token, string Key, string Operand)>(); + var positions = new Dictionary(); + var statics = new List(); + for (var index = 0; index < original.Body.Instructions.Count; index++) + { + var instruction = original.Body.Instructions[index]; + if (!HasToken(instruction.OpCode)) continue; + var token = TokenOperand(original, body, instruction); + if (BoundStatic(instruction)) statics.Add($"({index}, {token})"); + var site = admitted.Body.Instructions[index]; + // An external identity is a materialized name at a reachable site and a literal at an unreachable + // one. The literal spelling names the operand, so two sites share a token only if it agrees. + var key = Operand(original, instructions, instruction, instruction, site, EmptySet); + var operand = Operand(original, instructions, instruction, instruction, site, reachable); + var known = positions.GetValueOrDefault(token, -1); + if (known < 0) + { + positions[token] = operands.Count; + operands.Add((token, key, operand)); + } + else if (operands[known].Key != key) + throw new InvalidDataException($""" + Metadata token 0x{token:X8} resolves to two operands in {original.FullName}. + The body evidence holds one operand per token. + """); + // The kernel decides the equality of a literal and its materialized name, so the table may hold + // either; the name spares the reachable site a conversion. + else if (reachable.Contains(site)) operands[known] = (token, key, operand); + } + // Each closed instantiation carries its own bytes and metadata slice, built from the open definition and the + // assembly, so a method's component names no other method and does not depend on which instantiations the + // extraction holds or in which order. + var data = ByteLiterals.Of(bytes); + var classOwner = original.HasThis && ObjectTypes.Is(original.DeclaringType); + var arguments = MethodSpecialization.Arguments(admitted); + var definitions = new HashSet(); + var instantiation = arguments.Count == 0 ? "" : + $", instantiation := {List(arguments.Select(argument => InstanceArgument(argument, definitions)))}"; + var (rows, tables, assemblies) = (metadata ?? throw new InvalidOperationException( + "Method evidence requires the pinned assemblies.")).Method(original, + operands.Select(entry => entry.Token).Append(original.Body.LocalVarToken.ToUInt32()) + .Where(token => token != 0), classOwner, definitions); + // The kernel decides once that the literal tables are the decoder's; the body check reads them by name. + var slices = $"def method{number}Metadata : Extraction.MethodMetadata := {rows}\n" + + $"def method{number}Tables : List Extraction.IndexTables := {tables}\n" + + $"def method{number}Names : Extraction.MethodNames := {Names(original, classOwner, admitted.FullName)}\n" + + $"theorem method{number}_tables_valid : Extraction.tablesMatch method{number}Metadata " + + $"method{number}Tables = true := by decide +kernel"; + var table = List(operands.Select(entry => $"({entry.Token}, {entry.Operand})")); + var identities = List(assemblies.Select(index => $"assembly{index}Identity")); + var identified = assemblies.Reverse().Aggregate("Extraction.AllIdentified.nil", + (rest, index) => $"(Extraction.AllIdentified.cons assembly{index}_identified {rest})"); + + var bodyImports = imports?.Bindings.Count > 0 ? $""", imports := method{number}BodyImports""" : ""; + var staticTokens = statics.Count == 0 ? "" : $", statics := {List(statics)}"; + var platform = $"""platformDefinitions{instantiation}{staticTokens}{bodyImports}"""; + var definition = string.Join(" ", + $$"""def method{{number}}Body : Extraction.BodyEvidence := { bytes := method{{number}}Bytes,""", + $$"""tokens := { operands := {{table}} }, metadata := method{{number}}Metadata,""", + $"""identities := {identities}, tables := method{number}Tables, names := method{number}Names,""", + $$"""pinned := pinnedIndex, platform := {{platform}} }"""); + return ($"""def method{number}Bytes : List Nat := {data}""", slices, definition, + identified, $"""method{number}_tables_valid"""); + } + + static string Utf8(string text) => ByteLiterals.Of(Encoding.UTF8.GetBytes(text)); + + /// + /// The owner and base class names the method's metadata spells and the signature the extraction selects it by, + /// as UTF-8 bytes: Lean reads the same names from the carried rows and proves by cil_kernel_rfl that they + /// spell the strings the evidence states. + /// + static string Names(MethodDefinition original, bool classOwner, string signature) + { + var bases = classOwner + ? ObjectTypes.Bases(original.DeclaringType) + .Select(type => $"({Utf8(LayoutEvidence.Scope(type))}, {Utf8(type.FullName)})") + : []; + return $"{{ owner := {Utf8(LayoutEvidence.Identity(original.DeclaringType))}, bases := {List(bases)}, " + + $"signature := {Utf8(signature)} }}"; + } + + // Closed arguments name a carried definition row or a platform entry. Lean resolves their kinds. + string InstanceArgument(TypeReference type, HashSet definitions) => type switch + { + _ when type.IsPrimitive || type.MetadataType is MetadataType.String or MetadataType.Object => + $".primitive {(int)type.MetadataType}", + ArrayType { IsVector: true } array => $".szarray ({InstanceArgument(array.ElementType, definitions)})", + GenericInstanceType generic => $""" + .genericInst {ArgumentHead(generic.ElementType, definitions)} {InstanceArguments(generic, definitions)} + """, + _ => $".named {ArgumentHead(type, definitions)}" + }; + + string InstanceArguments(GenericInstanceType generic, HashSet definitions) + { + var chain = new StringBuilder(); + foreach (var argument in generic.GenericArguments) + chain.Append($"(.cons ({InstanceArgument(argument, definitions)}) "); + return chain.Append(".nil").Append(')', generic.GenericArguments.Count).ToString(); + } + + string ArgumentHead(TypeReference type, HashSet definitions) + { + if (type.Scope is AssemblyNameReference reference && Pinned.IsPlatform(reference)) + { + statedTypes.Add(type); + var parts = PlatformDefinitions.NameParts(type).Select(Utf8); + return $"(.platform {Utf8(reference.FullName)} {List(parts)})"; + } + var definition = type.Resolve() ?? throw new InvalidDataException($"Unresolved type argument {type.FullName}"); + definitions.Add(definition); + var scope = Utf8(definition.Module.Assembly.Name.FullName); + return $"(.definition {scope} {definition.MetadataToken.ToUInt32()})"; + } + + static uint TokenOperand(MethodDefinition original, MethodBodyBytes.Body body, Instruction instruction) + { + var at = body.HeaderLength + instruction.Offset + instruction.OpCode.Size; + if (at + sizeof(uint) > body.Bytes.Length) + throw new InvalidDataException($"Method body of {original.FullName} ends inside a token operand."); + return BinaryPrimitives.ReadUInt32LittleEndian(body.Bytes.AsSpan(at, sizeof(uint))); + } + + /// + /// The identity block of each pinned assembly: its table header and Assembly row, a function of the + /// assembly alone, so every extraction of it writes the same bytes. + /// + internal void EmitAssemblyMetadata(StringBuilder lean) + { + var slices = metadata ?? + throw new InvalidOperationException("Method evidence requires the pinned assemblies."); + foreach (var (module, index) in Pinned.Modules.Select((module, index) => (module, index))) + { + var identity = Pinned.Original(module).Assembly.Name.FullName; + lean.AppendLine($"def assembly{index}Metadata : Extraction.AssemblyMetadata := " + + slices.AssemblyBlock(module)); + lean.AppendLine($"def assembly{index}Identity : List Nat := " + + ByteLiterals.Of(Encoding.UTF8.GetBytes(identity))); + // The assembly's header bytes decode to the stated header, its bytes spell this identity, and it is the + // string the block states: one proof for every method of the assembly. + lean.AppendLine($"theorem assembly{index}_identified : Extraction.AssemblyIdentified " + + $"assembly{index}Metadata assembly{index}Identity := " + + "⟨by cil_kernel_rfl, by first | cil_kernel_rfl | decide +kernel, by cil_kernel_rfl⟩"); + } + // Identities only: hashes and MVIDs change with every rebuild, so they stay in identity.json for `check`. + var pins = Pinned.Pins.ToArray(); + for (var k = 0; k < pins.Length; k++) + lean.AppendLine( + $"def assembly{k} : Extraction.AssemblyPin := {{ identity := {Quote(pins[k].Identity)} }}"); + lean.AppendLine("def assemblies : List Extraction.AssemblyPin := [" + + string.Join(", ", Enumerable.Range(0, pins.Length).Select(k => $"assembly{k}")) + "]"); + // Every body lists these identities; one proof that they spell the pins serves them all. + lean.AppendLine("def pinnedIdentities : List (List Nat) := " + + List(Pinned.Modules.Select((_, index) => $"assembly{index}Identity"))); + lean.AppendLine("def pinnedIndex : Extraction.PinnedIndex := cil_pin_index pinnedIdentities"); + lean.AppendLine("#print axioms pinnedIndex"); + // The platform definitions go here once every method's slices are known; see InsertPlatformDefinitions. + platformAt = lean.Length; + lean.AppendLine("theorem pinned_identities_spelled : pinnedIdentities.mapM Extraction.spelled? = " + + "some (assemblies.map (·.identity)) := by cil_kernel_rfl"); + } + + /// + /// Inserts the platform definitions where left room, once every method's + /// evidence is written: every body is checked against this one list, and Lean refuses a reliance on a platform + /// type it does not state. + /// + internal void InsertPlatformDefinitions(StringBuilder lean) + { + var platform = Platform(); + lean.Insert(platformAt, $""" + set_option maxRecDepth {platform.Depth} in + def platformDefinitions : Extraction.PlatformTable := platform_table% {platform.Lean(ByteLiterals.Of)} + + """); + } + + // The platform types the carried TypeRef rows and the stated type arguments name, under the core library the + // entry module's System.Object resolves to. + PlatformDefinitions Platform() + { + var slices = metadata ?? + throw new InvalidOperationException("Method evidence requires the pinned assemblies."); + var coreLibrary = Pinned.Original(Pinned.Modules.First()).TypeSystem.Object.Resolve()?.Module ?? + throw new InvalidDataException("System.Object does not resolve for the platform definitions."); + var definitions = new PlatformDefinitions(coreLibrary); + foreach (var group in slices.TypeReferences.GroupBy(reference => reference.Module)) + definitions.Add(group.Key, Pinned.Metadata(group.Key).Reader, group.Select(reference => reference.Handle), + Pinned.IsPlatform); + foreach (var type in statedTypes) definitions.Add(type, Pinned.IsPlatform); + return definitions; } string DispatchTypes(MethodDefinition method) @@ -351,7 +915,7 @@ string DispatchMethod(MethodDefinition method, GenericInstanceType? owner) _ => TypeSubstitution.Components(type, Closed) }; var specialization = owner is null ? "" : - $", owner := .constructor {Scope(owner.ElementType)} {Quote(owner.ElementType.FullName)}, " + + $", owner := {Head(owner)} {Scope(owner.ElementType)} {Quote(owner.ElementType.FullName)}, " + $"ownerArguments := {List(owner.GenericArguments.Select(Type))}"; return $"{{ token := {method.MetadataToken.ToUInt32()}, name := {Quote(method.Name)}, " + diff --git a/Extractor/GenericStructs.cs b/Extractor/GenericStructs.cs index c798ea7c4..7e000f9e5 100644 --- a/Extractor/GenericStructs.cs +++ b/Extractor/GenericStructs.cs @@ -6,13 +6,17 @@ namespace Nethermind.LeanCIL; internal static class GenericStructs { internal static bool DirectInterfaceConstraint(TypeReference constraint, TypeReference argument, - ModuleDefinition module) => - PinnedAssemblies.Declares(constraint, module) && !constraint.ContainsGenericParameter && - constraint.Resolve() is { IsInterface: true } declared && Pinned(declared.Module, module) && - argument.Resolve() is { } concrete && Pinned(concrete.Module, module) && concrete.IsValueType && - concrete.Interfaces.Any(implemented => MethodSpecialization.SignatureType(Substitute( - implemented.InterfaceType, argument as GenericInstanceType)) == - MethodSpecialization.SignatureType(constraint)); + ModuleDefinition module) + { + if (!PinnedAssemblies.Declares(constraint, module) || constraint.ContainsGenericParameter || + constraint.Resolve() is not { IsInterface: true } declared || !Pinned(declared.Module, module) || + argument.Resolve() is not { } concrete || !Pinned(concrete.Module, module) || !concrete.IsValueType) + return false; + if (concrete.Interfaces.Count == 0) return false; + var expected = StructuralSignatures.Current.Type(constraint); + return concrete.Interfaces.Any(implemented => StructuralSignatures.Current.Type(Substitute( + implemented.InterfaceType, argument as GenericInstanceType)) == expected); + } static bool Pinned(ModuleDefinition candidate, ModuleDefinition module) => candidate == module || PinnedAssemblies.Active?.Contains(candidate) == true; @@ -56,9 +60,10 @@ internal static void Validate(GenericInstanceType owner) if (DirectInterfaceConstraint(type, argument, definition.Module)) continue; if (type is RequiredModifierType modifier && modifier.ModifierType.FullName == "System.Runtime.InteropServices.UnmanagedType" && - RuntimeModels.ExactScope(modifier.ModifierType.Scope, "System.Runtime")) + RuntimeModels.ExactScope(modifier.ModifierType, PlatformAssemblies.SystemRuntime)) type = modifier.ElementType; - if (type.FullName != "System.ValueType" || !RuntimeModels.ExactScope(type.Scope, "System.Runtime")) + if (type.FullName != "System.ValueType" || + !RuntimeModels.ExactScope(type, PlatformAssemblies.SystemRuntime)) throw new InvalidDataException("Unsupported generic struct constraint type"); } } diff --git a/Extractor/Identities.cs b/Extractor/Identities.cs new file mode 100644 index 000000000..a9a5a5ac7 --- /dev/null +++ b/Extractor/Identities.cs @@ -0,0 +1,831 @@ +using System.Buffers.Binary; +using System.Collections.Immutable; +using System.Globalization; +using System.Numerics; +using System.Runtime.InteropServices; +using System.Reflection.Metadata.Ecma335; +using System.Security.Cryptography; +using System.Text; +using Mono.Cecil; +using Srm = System.Reflection.Metadata; + +namespace Nethermind.LeanCIL; + +/// +/// The .NET exceptions the model raises, in the order of CIL.ExceptionType. Each stands for the type of +/// namespace System in the runtime assembly; no member is zero, so default is none of them. +/// +internal enum ExceptionType +{ + BaseException = 1, SystemException, ApplicationException, Arithmetic, DivideByZero, Overflow, InvalidOperation, + NotSupported, NotImplemented, Argument, ArgumentNull, ArgumentOutOfRange, IndexOutOfRange, NullReference, + Format, InvalidCast, ArrayTypeMismatch, +} + +internal static class ExceptionTypeText +{ + /// The Lean constructor term naming this exception type. + internal static string ToLean(this ExceptionType exception) => "." + exception.LeanName(); + + /// Every member, in the order of CIL.ExceptionType.all. + internal static readonly ExceptionType[] All = + [ + ExceptionType.BaseException, ExceptionType.SystemException, ExceptionType.ApplicationException, + ExceptionType.Arithmetic, ExceptionType.DivideByZero, ExceptionType.Overflow, + ExceptionType.InvalidOperation, ExceptionType.NotSupported, ExceptionType.NotImplemented, + ExceptionType.Argument, ExceptionType.ArgumentNull, ExceptionType.ArgumentOutOfRange, + ExceptionType.IndexOutOfRange, ExceptionType.NullReference, ExceptionType.Format, ExceptionType.InvalidCast, + ExceptionType.ArrayTypeMismatch + ]; + + /// The name column of the member's TypeDef row, such as OverflowException. + internal static string MetadataName(this ExceptionType exception) => exception switch + { + ExceptionType.BaseException => "Exception", + ExceptionType.SystemException => "SystemException", + ExceptionType.ApplicationException => "ApplicationException", + ExceptionType.Arithmetic => "ArithmeticException", + ExceptionType.DivideByZero => "DivideByZeroException", + ExceptionType.Overflow => "OverflowException", + ExceptionType.InvalidOperation => "InvalidOperationException", + ExceptionType.NotSupported => "NotSupportedException", + ExceptionType.NotImplemented => "NotImplementedException", + ExceptionType.Argument => "ArgumentException", + ExceptionType.ArgumentNull => "ArgumentNullException", + ExceptionType.ArgumentOutOfRange => "ArgumentOutOfRangeException", + ExceptionType.IndexOutOfRange => "IndexOutOfRangeException", + ExceptionType.NullReference => "NullReferenceException", + ExceptionType.Format => "FormatException", + ExceptionType.InvalidCast => "InvalidCastException", + ExceptionType.ArrayTypeMismatch => "ArrayTypeMismatchException", + _ => throw new ArgumentOutOfRangeException(nameof(exception)) + }; + + /// The Lean constructor name, such as overflow. + internal static string LeanName(this ExceptionType exception) => exception switch + { + ExceptionType.BaseException => "baseException", + ExceptionType.SystemException => "systemException", + ExceptionType.ApplicationException => "applicationException", + ExceptionType.Arithmetic => "arithmetic", + ExceptionType.DivideByZero => "divideByZero", + ExceptionType.Overflow => "overflow", + ExceptionType.InvalidOperation => "invalidOperation", + ExceptionType.NotSupported => "notSupported", + ExceptionType.NotImplemented => "notImplemented", + ExceptionType.Argument => "argument", + ExceptionType.ArgumentNull => "argumentNull", + ExceptionType.ArgumentOutOfRange => "argumentOutOfRange", + ExceptionType.IndexOutOfRange => "indexOutOfRange", + ExceptionType.NullReference => "nullReference", + ExceptionType.Format => "format", + ExceptionType.InvalidCast => "invalidCast", + ExceptionType.ArrayTypeMismatch => "arrayTypeMismatch", + _ => throw new ArgumentOutOfRangeException(nameof(exception)) + }; + + /// The full type name a reader sees, such as System.OverflowException; for display only. + internal static string FullName(this ExceptionType exception) => "System." + exception.MetadataName(); +} + +/// Identity columns read directly from the module image named by a metadata token. +internal static class IdentityRows +{ + /// Whether a raw top-level type row has the required name and, when supplied, assembly scope. + internal static bool TopLevelMatches(TypeReference type, TypeLevel expected, AssemblyName? assembly = null) + { + MetadataImage image = (PinnedAssemblies.Active ?? + throw new InvalidDataException("Modifier identity without a pinned module image")) + .IdentityMetadata(type.Module); + RequireModule(image, type.Module); + var row = image.TopLevelTypeName(type.MetadataToken.ToUInt32()); + return row is not null && row.Level == expected && (assembly is null || + row.ResolutionScope >> 24 == 0x23 && image.AssemblyIdentity(row.ResolutionScope) == assembly); + } + + internal static void RequireModule(MetadataImage image, ModuleDefinition module) + { + if (image.Reader.GetGuid(image.Reader.GetModuleDefinition().Mvid) != module.Mvid) + throw new InvalidDataException("Identity module and image have different MVIDs"); + } + + internal static MetadataName Name(MetadataImage image, Srm.StringHandle handle) + { + return image.Name(MetadataTokens.GetHeapOffset(handle)); + } + + internal static TypeLevel Level(MetadataImage image, MetadataToken token) + { + if (token.RID == 0) throw new InvalidDataException("Type identity without an image row"); + if (token.TokenType == TokenType.TypeDef) + { + var row = image.Reader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle((int)token.RID)); + return new(Name(image, row.Namespace), Name(image, row.Name)); + } + if (token.TokenType == TokenType.TypeRef) + { + var row = image.Reader.GetTypeReference(MetadataTokens.TypeReferenceHandle((int)token.RID)); + return new(Name(image, row.Namespace), Name(image, row.Name)); + } + throw new InvalidDataException("Type identity token does not name a type definition or reference"); + } +} + +// The identities of CIL/Identity.lean, built from metadata columns. ToLean() elaborates against the expected Lean +// type; ToDisplay() is the text CIL/Identity.lean shows for the value, escapes and raw fallbacks included. + +/// One metadata name, the UTF-8 bytes of a #Strings entry (CIL.MetadataName). +internal sealed record MetadataName +{ + readonly byte[] bytes; + + MetadataName(byte[] bytes) => this.bytes = bytes; + + /// The empty name, which a neutral culture and the global namespace hold. + internal static readonly MetadataName Empty = new([]); + + /// The name whose UTF-8 bytes are . + internal static MetadataName FromBytes(ReadOnlySpan bytes) => new(bytes.ToArray()); + + /// A name constant encoded as UTF-8, rejecting text with an unpaired surrogate. + internal static MetadataName OfText(string entry) => new(IdentityText.Utf8(entry)); + + /// The UTF-8 bytes of the name. + internal ReadOnlySpan Bytes => bytes; + + /// The number CIL.MetadataName.code holds: the bytes after a leading 1, in base 256. + internal BigInteger Code => IdentityText.Code(bytes); + + string? Text => IdentityText.DecodeUtf8(bytes); + + // MetadataName.literal?: no #Strings entry holds a NUL, so name! refuses one. + string? Literal => Text is { } text && !text.Contains('\0') ? IdentityText.Quoted(text) : null; + + /// The name! literal, or the constructor of the code when no literal shows the name. + internal string ToLean() => Literal is { } quoted ? "name!" + quoted : $"(CIL.MetadataName.mk 0x{Code:x})"; + + /// The text of MetadataName.display. + internal string ToDisplay() => Readable() ?? IdentityText.RawCode(Code); + + /// The text of MetadataName.readable?, or null when the bytes are not UTF-8 or hold an + /// invisible character above U+FFFF. + internal string? Readable() => Text is { } text ? IdentityText.Readable(text) : null; + + /// The Repr of the name: its literal, or the raw structure of its code. + internal LeanDoc Repr() => LeanDoc.Of(Literal is { } quoted ? "name!" + quoted : IdentityText.RawCode(Code)); + + /// Equal names have equal bytes. + public bool Equals(MetadataName? other) => other is not null && bytes.AsSpan().SequenceEqual(other.bytes); + + /// A hash of every byte of the name. + public override int GetHashCode() + { + HashCode hash = new(); + hash.AddBytes(bytes); + return hash.ToHashCode(); + } +} + +/// One #US literal, its UTF-16 code units (CIL.UserString). +internal sealed record UserString +{ + readonly char[] units; + + UserString(char[] units) => this.units = units; + + /// The literal of an ldstr operand, whose UTF-16 code units Cecil reads unchanged. + internal static UserString OfLiteral(string literal) => new(literal.ToCharArray()); + + /// The UTF-16 code units of the literal. + internal ReadOnlySpan Units => units; + + /// The number CIL.UserString.code holds: the units after a leading 1, in base 65536. + internal BigInteger Code => IdentityText.Code(units); + + // UserString.text?: none at an unpaired surrogate. + string? Text => IdentityText.WellFormed(units) ? new string(units) : null; + + string? Literal => Text is { } text ? IdentityText.Quoted(text) : null; + + /// The str! literal, or the constructor of the code when no literal shows the literal. + internal string ToLean() => Literal is { } quoted ? "str!" + quoted : $"(CIL.UserString.mk 0x{Code:x})"; + + /// The text of UserString.display. + internal string ToDisplay() => + Text is { } text && IdentityText.Readable(text) is { } readable ? readable : IdentityText.RawCode(Code); + + /// Equal literals have equal code units. + public bool Equals(UserString? other) => other is not null && units.AsSpan().SequenceEqual(other.units); + + /// A hash of every code unit of the literal. + public override int GetHashCode() + { + HashCode hash = new(); + hash.AddBytes(MemoryMarshal.AsBytes(units.AsSpan())); + return hash.ToHashCode(); + } +} + +/// The four version columns of an Assembly or AssemblyRef row (CIL.Version). +/// +internal readonly record struct AssemblyVersion(ushort Major, ushort Minor, ushort Build, ushort Revision) +{ + /// The columns Cecil read into , or null when a component is outside a + /// column's range, which no row can hold. + internal static AssemblyVersion? Of(Version? version) => + version is { Major: >= 0 and <= ushort.MaxValue, Minor: >= 0 and <= ushort.MaxValue, + Build: >= 0 and <= ushort.MaxValue, Revision: >= 0 and <= ushort.MaxValue } + ? new((ushort)version.Major, (ushort)version.Minor, (ushort)version.Build, (ushort)version.Revision) + : null; + + /// The structure literal of the version. + internal string ToLean() => string.Create(CultureInfo.InvariantCulture, + $"{{ major := {Major}, minor := {Minor}, build := {Build}, revision := {Revision} }}"); + + /// The text of Version.display, such as 11.0.0.0. + internal string ToDisplay() => string.Create(CultureInfo.InvariantCulture, $"{Major}.{Minor}.{Build}.{Revision}"); + + /// The derived Repr of the version. + internal LeanDoc Repr() => LeanDoc.Structure(("major", LeanDoc.Of(Major)), ("minor", LeanDoc.Of(Minor)), + ("build", LeanDoc.Of(Build)), ("revision", LeanDoc.Of(Revision))); +} + +/// An assembly by the name, version, culture and public key token of its row (CIL.AssemblyName). +/// The token is the eight token bytes read big-endian, or null for a row without a key. +internal sealed record AssemblyName(MetadataName Name, AssemblyVersion Version, MetadataName Culture, + ulong? PublicKeyToken) +{ + /// The identity of an assembly row, preserving the exact name and culture heap bytes. + internal static AssemblyName? Of(AssemblyNameReference row, MetadataImage image) + { + if (row is AssemblyNameDefinition) return image.AssemblyIdentity(0x20000001); + if (row.MetadataToken.TokenType != TokenType.AssemblyRef || row.MetadataToken.RID == 0) + throw new InvalidDataException("Assembly reference without an image row"); + return image.AssemblyIdentity(row.MetadataToken.ToUInt32()); + } + + /// The exact assembly columns of a definition or reference row in its owning image. + internal static AssemblyName? Read(MetadataImage image, uint token) + { + var columns = image.AssemblyColumns(token); + return columns is null ? null : new(image.Name(columns.Name), columns.Version, + image.Name(columns.Culture), columns.PublicKeyToken); + } + + /// Raw name offsets and decoded fixed-width columns, without expanding string suffixes. + internal sealed record Columns(int Name, AssemblyVersion Version, int Culture, ulong? PublicKeyToken); + + /// A normalized signing column; an invalid width is distinct from an absent token. + internal sealed record KeyColumns(bool Valid, ulong? Token); + + internal static KeyColumns ReadKey(MetadataImage image, Srm.BlobHandle handle, bool fullKey) + { + byte[] bytes = image.Reader.GetBlobBytes(handle); + image.RecordSigningBytes(bytes.Length); + if (fullKey && bytes.Length != 0) bytes = SHA1.HashData(bytes)[^TokenBytes..].Reverse().ToArray(); + return bytes.Length switch + { + 0 => new(true, null), + TokenBytes => new(true, BinaryPrimitives.ReadUInt64BigEndian(bytes)), + _ => new(false, null) + }; + } + + internal static Columns? ReadColumns(MetadataImage image, uint token) + { + var reader = image.Reader; + if (token == 0x20000001) + { + var definition = reader.GetAssemblyDefinition(); + return FromRow(definition.Name, definition.Version, definition.Culture, definition.PublicKey, + (definition.Flags & System.Reflection.AssemblyFlags.PublicKey) != 0); + } + if ((token >> 24) != 0x23 || (token & 0xFFFFFF) == 0) + throw new InvalidDataException("Assembly reference without an image row"); + var handle = MetadataTokens.AssemblyReferenceHandle((int)(token & 0xFFFFFF)); + var reference = reader.GetAssemblyReference(handle); + return FromRow(reference.Name, reference.Version, reference.Culture, reference.PublicKeyOrToken, + (reference.Flags & System.Reflection.AssemblyFlags.PublicKey) != 0); + + Columns? FromRow(Srm.StringHandle name, Version version, Srm.StringHandle culture, + Srm.BlobHandle key, bool fullKey) + { + var token = image.SigningKey(key, fullKey); + if (!token.Valid || AssemblyVersion.Of(version) is not { } columns) return null; + return new(MetadataTokens.GetHeapOffset(name), columns, MetadataTokens.GetHeapOffset(culture), + token.Token); + } + } + + /// The assembly a type's scope names: the referenced row, or the assembly of the module that + /// defines the type; null for a module reference or an invalid assembly row. + /// + internal static AssemblyName? OfScope(TypeReference type, MetadataImage image) + { + IdentityRows.RequireModule(image, type.Module); + return type.Scope switch + { + AssemblyNameReference row => Of(row, image), + ModuleDefinition module when module == type.Module => Of(module.Assembly.Name, image), + _ => null + }; + } + + const int TokenBytes = 8; + + /// The structure literal of the assembly. + internal string ToLean() => + $"{{ name := {Name.ToLean()}, version := {Version.ToLean()}, culture := {Culture.ToLean()}, " + + $"publicKeyToken := {(PublicKeyToken is { } token ? $"some 0x{token:x16}" : "none")} }}"; + + /// The text of AssemblyName.display, such as + /// System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. + internal string ToDisplay() => Parts() is (string name, string culture) + ? $"{name}, Version={Version.ToDisplay()}, Culture={culture}, PublicKeyToken={TokenText()}" + : LeanDoc.Pretty(Repr()); + + // AssemblyName.parts?: text that splits back at its separators into exactly this assembly. + (string Name, string Culture)? Parts() + { + if (Name.Readable() is not { } name || Culture.Readable() is not { } culture || + name.Contains(',') || name.Contains(']') || culture.Contains(',') || culture.Contains(']') || + culture == Neutral) return null; + return (name, culture.Length == 0 ? Neutral : culture); + } + + const string Neutral = "neutral"; + + string TokenText() => PublicKeyToken is { } token ? token.ToString("x16", CultureInfo.InvariantCulture) : "null"; + + /// The text of AssemblyName.scopeText?, which a type display starts with. + internal string? ScopeText() + { + if (Parts() is not (string name, string culture)) return null; + return Culture == MetadataName.Empty && PublicKeyToken is null + ? $"[{name},{Version.ToDisplay()}]" : $"[{name},{Version.ToDisplay()},{culture},{TokenText()}]"; + } + + /// The derived Repr of the assembly. + internal LeanDoc Repr() => LeanDoc.Structure(("name", Name.Repr()), ("version", Version.Repr()), + ("culture", Culture.Repr()), + ("publicKeyToken", PublicKeyToken is { } token + ? LeanDoc.Of("some ") + LeanDoc.Of(token) : LeanDoc.Of("none"))); +} + +/// One row of a type's nesting chain: its namespace and name columns (CIL.TypeLevel). +internal sealed record TypeLevel(MetadataName Namespace, MetadataName Name) +{ + /// The level of a type definition's own row. + internal static TypeLevel Of(TypeDefinition definition, MetadataImage image) => + IdentityRows.Level(image, definition.MetadataToken); + + /// The structure literal of the level. + internal string ToLean() => $"{{ typeNamespace := {Namespace.ToLean()}, name := {Name.ToLean()} }}"; + + /// The Repr text of the level, which is how Lean shows a TypeLevel. + internal string ToDisplay() => LeanDoc.Pretty(Repr()); + + // TypeLevel.text?: a level that splits back at its last '.'. + internal string? Text() + { + if (Namespace.Readable() is not { } space || Name.Readable() is not { } name || + space.Contains('/') || name.Contains('.') || name.Contains('/')) return null; + return space.Length == 0 ? name : space + "." + name; + } + + /// The derived Repr of the level. + internal LeanDoc Repr() => LeanDoc.Structure(("typeNamespace", Namespace.Repr()), ("name", Name.Repr())); +} + +/// A type by the assembly that holds its TypeDef row, its namespace and name columns, and the +/// levels of its enclosing types from the outermost (CIL.TypeIdentity). +internal sealed record TypeIdentity(AssemblyName Assembly, MetadataName Namespace, + ImmutableArray Enclosing, MetadataName Name) +{ + /// The identity of a type definition, read from its row, its enclosing rows and its assembly row. + /// A reference names the same identity once it is resolved to its definition. + internal static TypeIdentity Of(TypeDefinition definition, MetadataImage image) + { + IdentityRows.RequireModule(image, definition.Module); + AssemblyName assembly = AssemblyName.Of(definition.Module.Assembly.Name, image) ?? + throw new InvalidDataException("Assembly row without a version or an eight-byte public key token: " + + definition.Module.Assembly.Name.FullName); + var enclosing = ImmutableArray.CreateBuilder(); + var visited = new HashSet(); + uint parent = image.NamedType(definition.MetadataToken.ToUInt32()).Parent; + while (parent != 0) + { + if (!visited.Add(parent)) throw new InvalidDataException("Cyclic nested type identity"); + var row = image.NamedType(parent); + enclosing.Add(new(image.Name(row.Namespace), image.Name(row.Name))); + parent = row.Parent; + } + enclosing.Reverse(); + TypeLevel level = TypeLevel.Of(definition, image); + return new(assembly, level.Namespace, enclosing.ToImmutable(), level.Name); + } + + /// The structure literal of the type, without enclosing when the type is not nested. + internal string ToLean() => + $"{{ assembly := {Assembly.ToLean()}, typeNamespace := {Namespace.ToLean()}, " + + (Enclosing.IsEmpty ? "" : $"enclosing := [{string.Join(", ", Enclosing.Select(level => level.ToLean()))}], ") + + $"name := {Name.ToLean()} }}"; + + /// The text of TypeIdentity.display, such as [Examples,1.0.0.0]Examples.Point. + internal string ToDisplay() + { + if (Assembly.ScopeText() is { } scope) + { + string?[] levels = [.. Enclosing.Select(level => level.Text()), new TypeLevel(Namespace, Name).Text()]; + if (levels.All(level => level is not null)) return scope + string.Join("/", levels); + } + return LeanDoc.Pretty(LeanDoc.Structure(("assembly", Assembly.Repr()), ("typeNamespace", Namespace.Repr()), + ("enclosing", LeanDoc.List(Enclosing.Select(level => level.Repr()).ToArray())), ("name", Name.Repr()))); + } + + /// Equal types have equal assemblies, columns and levels. + public bool Equals(TypeIdentity? other) => other is not null && Assembly == other.Assembly && + Namespace == other.Namespace && Name == other.Name && Enclosing.SequenceEqual(other.Enclosing); + + /// A hash of every component of the type. + public override int GetHashCode() + { + HashCode hash = new(); + hash.Add(Assembly); + hash.Add(Namespace); + hash.Add(Name); + foreach (TypeLevel level in Enclosing) hash.Add(level); + return hash.ToHashCode(); + } +} + +/// A value type ECMA-335 encodes by element type (II.23.1.16), without an assembly +/// (CIL.PrimitiveType). +internal closed record PrimitiveType(MetadataType ElementType, string Constructor, string Name) +{ + internal sealed record Boolean() : PrimitiveType(MetadataType.Boolean, "boolean", "Boolean"); + internal sealed record Char() : PrimitiveType(MetadataType.Char, "char", "Char"); + internal sealed record SByte() : PrimitiveType(MetadataType.SByte, "sbyte", "SByte"); + internal sealed record Byte() : PrimitiveType(MetadataType.Byte, "byte", "Byte"); + internal sealed record Int16() : PrimitiveType(MetadataType.Int16, "int16", "Int16"); + internal sealed record UInt16() : PrimitiveType(MetadataType.UInt16, "uint16", "UInt16"); + internal sealed record Int32() : PrimitiveType(MetadataType.Int32, "int32", "Int32"); + internal sealed record UInt32() : PrimitiveType(MetadataType.UInt32, "uint32", "UInt32"); + internal sealed record Int64() : PrimitiveType(MetadataType.Int64, "int64", "Int64"); + internal sealed record UInt64() : PrimitiveType(MetadataType.UInt64, "uint64", "UInt64"); + internal sealed record Single() : PrimitiveType(MetadataType.Single, "single", "Single"); + internal sealed record Double() : PrimitiveType(MetadataType.Double, "double", "Double"); + internal sealed record IntPtr() : PrimitiveType(MetadataType.IntPtr, "intPtr", "IntPtr"); + internal sealed record UIntPtr() : PrimitiveType(MetadataType.UIntPtr, "uintPtr", "UIntPtr"); + + /// Every primitive, in the order of CIL.PrimitiveType's constructors. + internal static readonly ImmutableArray All = + [ + new Boolean(), new Char(), new SByte(), new Byte(), new Int16(), new UInt16(), new Int32(), new UInt32(), + new Int64(), new UInt64(), new Single(), new Double(), new IntPtr(), new UIntPtr() + ]; + + /// The primitive an element type code encodes, or null for any other code. + internal static PrimitiveType? OfElementType(MetadataType elementType) => + All.FirstOrDefault(primitive => primitive.ElementType == elementType); + + /// The primitive a type encodes: an element type code, or a reference into a core library, which + /// Cecil reads as the same code. Cecil also gives that code to a type of that name in any other assembly, + /// which is a named type and gives null here. + internal static PrimitiveType? Of(TypeReference type, MetadataImage image) => + type is not (TypeSpecification or GenericParameter) && OfElementType(type.MetadataType) is { } primitive && + AssemblyName.OfScope(type, image) is { } scope && PlatformAssemblies.CoreLibraries.Contains(scope) + ? primitive : null; + + /// The constructor term, such as .int32. + internal string ToLean() => "." + Constructor; + + /// The text of PrimitiveType.display, such as System.Int32. + internal string ToDisplay() => "System." + Name; +} + +/// What metadata says a named type is (CIL.TypeKind). +internal closed record TypeKind +{ + internal sealed record ValueType : TypeKind; + internal sealed record EnumType(PrimitiveType Underlying) : TypeKind; + internal sealed record ClassType : TypeKind; + internal sealed record InterfaceType : TypeKind; + + /// The kind of a definition from its flags and the core-library type it extends: an enumeration + /// holds its underlying primitive, and System.Enum itself is a class. + internal static TypeKind Of(TypeDefinition definition, MetadataImage image) + { + if (definition.IsInterface) return new InterfaceType(); + if (definition.BaseType is not { } baseType || CoreType(definition, EnumName, image)) return new ClassType(); + if (CoreType(baseType, EnumName, image)) return new EnumType(UnderlyingOf(definition, image)); + return CoreType(baseType, ValueTypeName, image) ? new ValueType() : new ClassType(); + } + + static readonly MetadataName SystemNamespace = MetadataName.OfText("System"); + static readonly MetadataName EnumName = MetadataName.OfText("Enum"); + static readonly MetadataName ValueTypeName = MetadataName.OfText("ValueType"); + + // A top-level type of the System namespace defined or referenced in a core library. + static bool CoreType(TypeReference type, MetadataName name, MetadataImage image) => + type is not TypeSpecification && type.DeclaringType is null && + IdentityRows.Level(image, type.MetadataToken) is { } level && + level.Namespace == SystemNamespace && level.Name == name && AssemblyName.OfScope(type, image) is { } scope && + PlatformAssemblies.CoreLibraries.Contains(scope); + + // ECMA-335 II.14.3: an enumeration has exactly one instance field, of its underlying primitive type. + static PrimitiveType UnderlyingOf(TypeDefinition definition, MetadataImage image) + { + var instance = definition.Fields.Where(field => !field.IsStatic).ToArray(); + return instance is [var field] && PrimitiveType.Of(field.FieldType, image) is { } primitive ? primitive : + throw new InvalidDataException("Enumeration without one primitive instance field: " + definition.FullName); + } + + /// The constructor term, such as .valueType or (.enumType .int32). + internal string ToLean() => this switch + { + ValueType => ".valueType", + EnumType kind => $"(.enumType {kind.Underlying.ToLean()})", + ClassType => ".classType", + InterfaceType => ".interfaceType" + }; + + /// The Repr text of the kind, which is how Lean shows a TypeKind. + internal string ToDisplay() => this switch + { + ValueType => "CIL.TypeKind.valueType", + EnumType kind => $"CIL.TypeKind.enumType (CIL.PrimitiveType.{kind.Underlying.Constructor})", + ClassType => "CIL.TypeKind.classType", + InterfaceType => "CIL.TypeKind.interfaceType" + }; +} + +/// A field operand by its declaring type, its index among the owner's fields as the model counts them, +/// the type it holds and its #Strings name (CIL.Extraction.FieldRef). Owner and type are +/// CIL.Extraction.TypeExpr terms. +internal sealed record FieldRef(string Owner, int Index, string Type, MetadataName Name) +{ + /// The structure literal of the reference. + internal string ToLean() => string.Create(CultureInfo.InvariantCulture, + $"{{ owner := {Owner}, index := {Index}, type := {Type}, name := {Name.ToLean()} }}"); +} + +/// A method by declaring type, name and declared signature, with the type arguments of a generic method +/// instance (CIL.Extraction.MethodRef). Types are CIL.Extraction.TypeExpr terms in the closed form the +/// call site states. +internal sealed record MethodRef(string Owner, MetadataName Name, bool HasThis, int GenericArity, string ReturnType, + ValueList Parameters, ValueList MethodArguments) +{ + /// The structure literal of the reference. + internal string ToLean() => string.Create(CultureInfo.InvariantCulture, + $"{{ owner := {Owner}, name := {Name.ToLean()}, hasThis := {LeanText.Bool(HasThis)}, " + + $"genericArity := {GenericArity}, returnType := {ReturnType}, " + + $"parameters := [{string.Join(", ", Parameters)}], " + + $"methodArguments := [{string.Join(", ", MethodArguments)}] }}"); +} + +/// The assemblies of the .NET 11 platform the extractor models, as AssemblyRef rows name them. +/// System.Runtime forwards its types to System.Private.CoreLib, which holds their TypeDef rows. +internal static class PlatformAssemblies +{ + /// The version every platform assembly carries. + internal static readonly AssemblyVersion Version = new(11, 0, 0, 0); + + /// The token of the key that signs System.Runtime. + internal const ulong MicrosoftKeyToken = 0xb03f5f7f11d50a3a; + + /// The token of the key that signs System.Memory and System.Runtime.Intrinsics. + internal const ulong OpenKeyToken = 0xcc7b13ffcd2ddd51; + + /// The token of the key that signs System.Private.CoreLib. + internal const ulong CoreLibraryKeyToken = 0x7cec85d7bea7798e; + + /// The token of the ECMA key, which signs mscorlib and other platform facades. + internal const ulong EcmaKeyToken = 0xb77a5c561934e089; + + /// The tokens of every key that signs a platform assembly; only the runtime answers their names. + /// + internal static readonly ImmutableArray KeyTokens = + [MicrosoftKeyToken, OpenKeyToken, CoreLibraryKeyToken, EcmaKeyToken]; + + /// System.Runtime, the core library a .NET 11 assembly references. + internal static readonly AssemblyName SystemRuntime = Signed("System.Runtime", MicrosoftKeyToken); + + /// System.Memory. + internal static readonly AssemblyName SystemMemory = Signed("System.Memory", OpenKeyToken); + + /// System.Runtime.Intrinsics. + internal static readonly AssemblyName SystemRuntimeIntrinsics = Signed("System.Runtime.Intrinsics", OpenKeyToken); + + /// System.Private.CoreLib, which defines the types System.Runtime forwards. + internal static readonly AssemblyName CoreLibrary = Signed("System.Private.CoreLib", CoreLibraryKeyToken); + + /// The assemblies whose System types are the primitives and the bases of value types. + /// + internal static readonly ImmutableArray CoreLibraries = [SystemRuntime, CoreLibrary]; + + /// The modelled platform assemblies a reference's simple name can claim. + internal static readonly ImmutableArray Modelled = + [SystemRuntime, SystemMemory, SystemRuntimeIntrinsics]; + + /// The modelled platform assembly whose simple name a scope carries, or null. + internal static AssemblyName? Claimed(AssemblyName? scope) + { + return scope is null ? null : Modelled.FirstOrDefault(platform => platform.Name == scope.Name); + } + + static AssemblyName Signed(string name, ulong token) => + new(MetadataName.OfText(name), Version, MetadataName.Empty, token); +} + +/// A Lean Std.Format document of text, line breaks, nesting and all-or-none groups, laid out by +/// Lean's algorithm, so a Repr built here prints as reprStr prints it. +internal closed record LeanDoc +{ + internal abstract int FlatWidth { get; } + + internal sealed record Text(string Value) : LeanDoc + { + internal int Width { get; } = IdentityText.Columns(Value); + internal override int FlatWidth => Width; + } + internal sealed record Line : LeanDoc + { + internal override int FlatWidth => 1; + } + internal sealed record Nest(int Indent, LeanDoc Body) : LeanDoc + { + internal override int FlatWidth { get; } = Body.FlatWidth; + } + internal sealed record Group(LeanDoc Body) : LeanDoc + { + internal override int FlatWidth { get; } = Body.FlatWidth; + } + internal sealed record Append(LeanDoc Left, LeanDoc Right) : LeanDoc + { + internal override int FlatWidth { get; } = AddWidths(Left.FlatWidth, Right.FlatWidth); + } + + static int AddWidths(int left, int right) => (int)Math.Min(int.MaxValue, (long)left + right); + + /// Text without a line break. + internal static LeanDoc Of(string text) => new Text(text); + + /// A natural number, as Repr Nat writes it. + internal static LeanDoc Of(ulong value) => new Text(value.ToString(CultureInfo.InvariantCulture)); + + /// Two documents one after the other. + public static LeanDoc operator +(LeanDoc left, LeanDoc right) => new Append(left, right); + + // Std.Format.bracket: a group nested by the width of its opening text. + static LeanDoc Bracket(string open, LeanDoc body, string close) => + new Group(new Nest(IdentityText.Columns(open), Of(open) + body + Of(close))); + + static readonly LeanDoc Separator = Of(",") + new Line(); + + /// The derived Repr of a structure with these fields. + internal static LeanDoc Structure(params (string Field, LeanDoc Value)[] fields) + { + LeanDoc body = Of(""); + for (int index = 0; index < fields.Length; index++) + { + if (index != 0) body += Separator; + var (field, value) = fields[index]; + body += Of(field) + Of(FieldSeparator) + new Group(new Nest(field.Length + FieldSeparator.Length, value)); + } + return Bracket("{ ", body, " }"); + } + + const string FieldSeparator = " := "; + + /// The Repr of a list of values that are not atoms. + internal static LeanDoc List(IReadOnlyList items) => items.Count == 0 ? Of("[]") : + Bracket("[", items.Skip(1).Aggregate(items[0], (joined, item) => joined + Separator + item), "]"); + + /// The width Std.Format.defWidth that reprStr lays text out in. + internal const int DefaultWidth = 120; + + // Std.Format.prettyM: work groups, each flattened when it and the text up to the next break fit. + enum Flatten { Disallowed, Fits, Overflows } + + sealed record Item(LeanDoc Document, int Indent); + + sealed record Cons(T Head, Cons? Tail); + + sealed record WorkGroup(Flatten Flatten, Cons? Items); + + readonly record struct Space(bool FoundLine, int Width); + + /// The text Lean's Format.pretty lays out as. + internal static string Pretty(LeanDoc document, int width = DefaultWidth) + { + StringBuilder output = new(); + int column = 0; + Cons? groups = new(new(Flatten.Disallowed, new(new(document, 0), null)), null); + while (groups is not null) + { + WorkGroup group = groups.Head; + if (group.Items is null) + { + groups = groups.Tail; + continue; + } + Item item = group.Items.Head; + Cons? rest = groups.Tail; + Cons With(Cons? items) => new(group with { Items = items }, rest); + switch (item.Document) + { + case Text text: + output.Append(text.Value); + column += text.Width; + groups = With(group.Items.Tail); + break; + case Append append: + groups = With(new(item with { Document = append.Left }, + new(item with { Document = append.Right }, group.Items.Tail))); + break; + case Nest nest: + groups = With(new(new(nest.Body, item.Indent + nest.Indent), group.Items.Tail)); + break; + case Line when group.Flatten == Flatten.Fits: + output.Append(' '); + column++; + groups = With(group.Items.Tail); + break; + case Line: + output.Append('\n').Append(' ', item.Indent); + column = item.Indent; + groups = With(group.Items.Tail); + break; + case Group inner when group.Flatten == Flatten.Fits: + groups = With(new(item with { Document = inner.Body }, group.Items.Tail)); + break; + case Group inner: + groups = Push(new(item with { Document = inner.Body }, null), With(group.Items.Tail), width, + column); + break; + } + } + return output.ToString(); + } + + // pushGroup: the group flattens when it and what follows it up to the next line break fit the line. + static Cons Push(Cons items, Cons groups, int width, int column) + { + int available = Math.Max(0, width - column); + WorkGroup group = new(Flatten.Fits, items); + Space own = UpToLine(new(group, null), available); + Space total = Merge(available, own, remaining => UpToLine(groups, remaining)); + return new(group with { Flatten = total.Width <= available ? Flatten.Fits : Flatten.Overflows }, groups); + } + + static Space Merge(int width, Space first, Func rest) + { + if (first.Width > width || first.FoundLine) return first; + Space second = rest(width - first.Width); + return second with { Width = AddWidths(first.Width, second.Width) }; + } + + static Space UpToLine(Cons? groups, int width) + { + int columns = 0; + Stack<(LeanDoc Document, bool Flatten)> pending = new(); + while (groups is not null) + { + for (Cons? items = groups.Head.Items; items is not null; items = items.Tail) + { + pending.Push((items.Head.Document, groups.Head.Flatten == Flatten.Fits)); + while (pending.TryPop(out var item)) + { + if (item.Flatten) + { + columns = AddWidths(columns, item.Document.FlatWidth); + if (columns > width) return new(false, columns); + continue; + } + switch (item.Document) + { + case Text text: + columns = AddWidths(columns, text.Width); + break; + case Line: + return new(true, columns); + case Nest nest: + pending.Push((nest.Body, item.Flatten)); + break; + case Group group: + pending.Push((group.Body, true)); + break; + case Append append: + pending.Push((append.Right, item.Flatten)); + pending.Push((append.Left, item.Flatten)); + break; + } + if (columns > width) return new(false, columns); + } + } + groups = groups.Tail; + } + return new(false, columns); + } +} diff --git a/Extractor/IdentityText.cs b/Extractor/IdentityText.cs new file mode 100644 index 000000000..784c42109 --- /dev/null +++ b/Extractor/IdentityText.cs @@ -0,0 +1,154 @@ +using System.Buffers.Binary; +using System.Collections.Immutable; +using System.Globalization; +using System.Numerics; +using System.Text; +using System.Text.Encodings.Web; +using System.Text.Json; + +namespace Nethermind.LeanCIL; + +/// The codes, escapes and fallbacks CIL/Identity.lean gives names and literals. +internal static class IdentityText +{ + static readonly UTF8Encoding StrictUtf8 = new(false, true); + + /// The UTF-8 bytes of a name; a lone surrogate, which no #Strings entry decodes to, fails. + /// + internal static byte[] Utf8(string text) + { + try + { + return StrictUtf8.GetBytes(text); + } + catch (EncoderFallbackException error) + { + throw new InvalidDataException($"Metadata name without UTF-8 bytes: {error.Message}", error); + } + } + + /// The text of UTF-8 bytes, or null when they are not UTF-8, as String.fromUTF8? decides. + /// + internal static string? DecodeUtf8(byte[] bytes) + { + try + { + return StrictUtf8.GetString(bytes); + } + catch (DecoderFallbackException) + { + return null; + } + } + + /// Whether every surrogate in is a high one followed by a low one. + internal static bool WellFormed(ReadOnlySpan units) + { + for (int index = 0; index < units.Length; index++) + { + if (!char.IsSurrogate(units[index])) continue; + if (!char.IsHighSurrogate(units[index]) || index + 1 == units.Length || + !char.IsLowSurrogate(units[index + 1])) return false; + index++; + } + return true; + } + + /// The bytes after a leading 1 as one big-endian number. + internal static BigInteger Code(ReadOnlySpan bytes) + { + byte[] digits = new byte[bytes.Length + 1]; + digits[0] = 1; + bytes.CopyTo(digits.AsSpan(1)); + return new(digits, isUnsigned: true, isBigEndian: true); + } + + /// The UTF-16 code units after a leading 1 as one big-endian number in base 65536. + internal static BigInteger Code(ReadOnlySpan units) + { + byte[] digits = new byte[2 * units.Length + 2]; + digits[1] = 1; + for (int index = 0; index < units.Length; index++) + BinaryPrimitives.WriteUInt16BigEndian(digits.AsSpan(2 * index + 2), units[index]); + return new(digits, isUnsigned: true, isBigEndian: true); + } + + /// The raw structure of a code, rawCodeDisplay. + internal static string RawCode(BigInteger code) => + $$"""{ code := 0x{{Hex(code)}} }"""; + + /// The minimal lowercase unsigned hexadecimal digits of a natural code. + internal static string Hex(BigInteger code) + { + string digits = Convert.ToHexStringLower(code.ToByteArray(isUnsigned: true, isBigEndian: true)); + return digits[0] == '0' && digits.Length > 1 ? digits[1..] : digits; + } + + /// The number of characters Lean counts in : its Unicode scalar values. + /// + internal static int Columns(string text) + { + int count = 0; + foreach (Rune _ in text.EnumerateRunes()) count++; + return count; + } + + /// Inclusive code point ranges of CIL.invisibleRanges: Unicode categories Cc, Cf, Zl and Zp + /// and the non-ASCII characters of Zs. + internal static readonly ImmutableArray<(int Low, int High)> InvisibleRanges = + [ + (0x0000, 0x001F), (0x007F, 0x00A0), (0x00AD, 0x00AD), (0x0600, 0x0605), (0x061C, 0x061C), + (0x06DD, 0x06DD), (0x070F, 0x070F), (0x0890, 0x0891), (0x08E2, 0x08E2), (0x1680, 0x1680), + (0x180E, 0x180E), (0x2000, 0x200F), (0x2028, 0x202F), (0x205F, 0x2064), (0x2066, 0x206F), + (0x3000, 0x3000), (0xFEFF, 0xFEFF), (0xFFF9, 0xFFFB), (0x110BD, 0x110BD), (0x110CD, 0x110CD), + (0x13430, 0x1343F), (0x1BCA0, 0x1BCA3), (0x1D173, 0x1D17A), (0xE0001, 0xE0001), (0xE0020, 0xE007F) + ]; + + static bool Invisible(Rune rune) + { + foreach (var (low, high) in InvisibleRanges) + if (low <= rune.Value && rune.Value <= high) return true; + return false; + } + + const int UnicodeEscapeLimit = 0x10000; + + // escapedText?: null when an invisible character lies above U+FFFF, which a Lean string cannot escape. + static string? Escaped(string text, bool quoted, bool sourceLiteral = false) + { + StringBuilder escaped = new(text.Length); + foreach (Rune rune in text.EnumerateRunes()) + { + // Lean accepts four hex digits per escape. A supplementary scalar is one source character. + bool escape = sourceLiteral ? rune.IsBmp && JavaScriptEncoder.Default.WillEncode(rune.Value) + : Invisible(rune); + if (escape && rune.Value >= UnicodeEscapeLimit) return null; + if (rune.Value == '\\') escaped.Append(@"\\"); + else if (quoted && rune.Value == '"') escaped.Append("\\\""); + else if (escape) escaped.Append(CultureInfo.InvariantCulture, $"\\u{rune.Value:X4}"); + else if (rune.IsBmp) escaped.Append((char)rune.Value); + else escaped.Append(rune.ToString()); + } + return escaped.ToString(); + } + + /// The text readableText? writes: escaped, with a leading { written \{. + /// + internal static string? Readable(string text) => + Escaped(text, false) is { } escaped ? (text.StartsWith('{') ? $"""\{escaped}""" : escaped) : null; + + /// The Lean string literal quotedText? writes for . + internal static string? Quoted(string text) => Escaped(text, true) is { } escaped ? $"\"{escaped}\"" : null; + + /// A Lean source literal preserving every Unicode scalar of its input. + internal static string Quote(string text) => WellFormed(text) + ? $"\"{Escaped(text, quoted: true, sourceLiteral: true)}\"" + : throw new InvalidDataException("A Lean string cannot represent an unpaired surrogate."); + + /// A Lean list whose strings preserve the scalar values in enumeration order. + internal static string QuoteList(IEnumerable values) => $"""[{string.Join(",", values.Select(Quote))}]"""; + + /// Source literals from Quote use the common Lean and JSON escapes. + internal static string ReadLiteral(string literal) => + JsonSerializer.Deserialize(literal) ?? throw new JsonException("Expected a quoted source path."); +} diff --git a/Extractor/ImportResolution.cs b/Extractor/ImportResolution.cs index 24bee7c17..022bcf8fd 100644 --- a/Extractor/ImportResolution.cs +++ b/Extractor/ImportResolution.cs @@ -36,6 +36,7 @@ internal sealed record Node(string AssemblyIdentity, string ImplementationSha256 readonly HashSet? allowedDependencies; readonly string? validatingAssembly; readonly bool ownsLibraries = true; + readonly PinnedAssemblies? identityOwner; readonly HashSet used = []; readonly Dictionary discovered = new(StringComparer.Ordinal); readonly List bindings = []; @@ -51,6 +52,7 @@ internal sealed record Node(string AssemblyIdentity, string ImplementationSha256 internal ImportResolution(IEnumerable sources) { + identityOwner = PinnedAssemblies.Active; try { foreach (Source source in sources) @@ -92,7 +94,7 @@ void Load(Source source) throw new InvalidDataException("Import implementation MVID mismatch."); string? framework = module.Assembly.CustomAttributes.SingleOrDefault(attribute => attribute.AttributeType.FullName == "System.Runtime.Versioning.TargetFrameworkAttribute")?.ConstructorArguments.Single().Value as string; - if (framework != contract.TargetFramework || framework != ".NETCoreApp,Version=v10.0") + if (framework != contract.TargetFramework || framework != ".NETCoreApp,Version=v11.0") throw new InvalidDataException("Import target framework mismatch."); Library? existing = librariesByIdentity.GetValueOrDefault(contract.AssemblyIdentity); if (existing is not null) @@ -107,11 +109,20 @@ void Load(Source source) if (simpleNames.Contains(module.Assembly.Name.Name)) throw new InvalidDataException("Ambiguous import assembly simple name: " + module.Assembly.Name.Name); var library = new Library(source, contract, ContractInterface.Hash(sidecar), module); + var pinned = identityOwner ?? throw new InvalidDataException( + "Import identity without a pinned module context"); + PlatformSignatures.Validate(implementation); + pinned.RegisterIdentityImage(module, implementation); libraries.Add(library); librariesByIdentity.Add(contract.AssemblyIdentity, library); simpleNames.Add(module.Assembly.Name.Name); } - catch { module.Dispose(); throw; } + catch + { + identityOwner?.UnregisterIdentityImage(module); + module.Dispose(); + throw; + } } void ValidateGraph() @@ -129,6 +140,7 @@ void ValidateGraph() ImportResolution(ImportResolution parent, ContractInterface contract) { + identityOwner = parent.identityOwner; libraries = parent.libraries; librariesByIdentity = parent.librariesByIdentity; simpleNames = parent.simpleNames; @@ -376,24 +388,26 @@ ContractLayout[] PublishedClosure(TypeReference element, Library library, Featur return actual; } - internal static string LeanSlot(Binding binding) => - "{ identity := " + JsonSerializer.Serialize(binding.Identity) + ", closureDigest := " + JsonSerializer.Serialize(binding.ClosureDigest) + - ", parameterCount := " + binding.ParameterCount + ", hasThis := false, returnsValue := " + (binding.ReturnsValue ? "true" : "false") + - (binding.TypedSignature is null ? "" : ", signature := some (" + binding.TypedSignature.Lean + "), layouts := { published := " + - ImportedLayoutMetadata.RawLean(binding.PublishedLayouts) + ", actual := " + ImportedLayoutMetadata.RawLean(binding.ActualLayouts) + - ", computed := " + binding.ComputedLayouts + " }") + " }"; - - internal bool TryTranslate(Instruction instruction, int methodCount, out string translated) + internal static string LeanSlot(Binding binding, string identity) { - if (sites.TryGetValue(instruction, out int slot)) + var digest = IdentityText.Quote(binding.ClosureDigest); + var metadata = $"identity := {identity}, closureDigest := {digest}"; + var arity = $"parameterCount := {binding.ParameterCount}, hasThis := false"; + var result = $"returnsValue := {LeanText.Bool(binding.ReturnsValue)}"; + var typed = ""; + if (binding.TypedSignature is { } signature) { - translated = $".call {methodCount + slot} {bindings[slot].ParameterCount}"; - return true; + var published = ImportedLayoutMetadata.RawLean(binding.PublishedLayouts); + var actual = ImportedLayoutMetadata.RawLean(binding.ActualLayouts); + var computed = binding.ComputedLayouts; + var layouts = $$"""{ published := {{published}}, actual := {{actual}}, computed := {{computed}} }"""; + typed = $", signature := some ({signature.Lean}), layouts := {layouts}"; } - translated = ""; - return false; + return $$"""{ {{metadata}}, {{arity}}, {{result}}{{typed}} }"""; } + internal int? Slot(Instruction instruction) => sites.TryGetValue(instruction, out var slot) ? slot : null; + // Bound libraries and their declared dependencies contribute modules to the extraction. internal SortedDictionary InterfaceModules() => Modules(libraries.Where(used.Contains)); @@ -429,7 +443,11 @@ internal string EmitImports(int methodCount) public void Dispose() { if (!ownsLibraries) return; - foreach (Library library in libraries) library.Module.Dispose(); + foreach (Library library in libraries) + { + identityOwner?.UnregisterIdentityImage(library.Module); + library.Module.Dispose(); + } libraries.Clear(); librariesByIdentity.Clear(); simpleNames.Clear(); diff --git a/Extractor/ImportResolver.cs b/Extractor/ImportResolver.cs index 810e9802c..aacc0af02 100644 --- a/Extractor/ImportResolver.cs +++ b/Extractor/ImportResolver.cs @@ -133,7 +133,7 @@ byte[] Read(string path) => { if (!selected.TryGetValue(name, out RuntimeAsset? asset)) throw new InvalidDataException( - "Requested import is not a net10.0 asset in the supplied manifests: " + name); + "Requested import is not a net11.0 asset in the supplied manifests: " + name); resolved.Add(ResolveImport(name, asset, applicationDirectory, roots, Read)); } return (rid, fallback ?? [], resolved.ToArray()); diff --git a/Extractor/ImportedLayoutMetadata.cs b/Extractor/ImportedLayoutMetadata.cs index b08c1532c..342912246 100644 --- a/Extractor/ImportedLayoutMetadata.cs +++ b/Extractor/ImportedLayoutMetadata.cs @@ -1,4 +1,3 @@ -using System.Text.Json; using Mono.Cecil; namespace Nethermind.LeanCIL; @@ -111,12 +110,22 @@ internal static string RawLean(ContractLayout[] layouts) { Dictionary indices = layouts.Select((layout, index) => (layout.Identity, Index: index)) .ToDictionary(item => item.Identity, item => item.Index, StringComparer.Ordinal); - return "[" + string.Join(", ", layouts.Select(layout => "{ identity := " + JsonSerializer.Serialize(layout.Identity) + - ", explicit := " + Bool(layout.Explicit) + ", auto := " + Bool(layout.Auto) + ", packing := " + - layout.Packing + ", declaredSize := " + layout.DeclaredSize + ", fields := [" + - string.Join(", ", layout.Fields.Select(field => "{ type := " + (field.Storage == "aggregate" ? - ".aggregate " + indices[field.Type.Scope + "::" + field.Type.Name] : "." + field.Storage) + - ", offset := " + (field.Offset is int offset ? "some " + offset : "none") + " }")) + "] }")) + "]"; + string Field(ContractLayoutField field) + { + var type = field.Storage == "aggregate" ? $".aggregate {indices[$"{field.Type.Scope}::{field.Type.Name}"]}" + : $".{field.Storage}"; + var offset = field.Offset is int value ? $"some {value}" : "none"; + return $$"""{ type := {{type}}, offset := {{offset}} }"""; + } + var rows = layouts.Select(layout => + { + var identity = IdentityText.Quote(layout.Identity); + var kind = $"explicit := {LeanText.Bool(layout.Explicit)}, auto := {LeanText.Bool(layout.Auto)}"; + var size = $"packing := {layout.Packing}, declaredSize := {layout.DeclaredSize}"; + var fields = string.Join(", ", layout.Fields.Select(Field)); + return $$"""{ identity := {{identity}}, {{kind}}, {{size}}, fields := [{{fields}}] }"""; + }); + return $"[{string.Join(", ", rows)}]"; } internal static string ComputedLean(ContractLayout[] layouts, ModuleDefinition selected, string architecture) => @@ -127,7 +136,8 @@ internal static string ComputedLean(ContractLayout[] layouts, ModuleDefinition s return "⟨⟨" + computed.Size + ", [" + string.Join(", ", MetadataValidation.Fields(type).Select(field => "⟨" + computed.Offsets[field.Definition.FullName] + ", " + MetadataValidation.FieldSize(field.Type, selected, architecture) + ", " + - Bool(MetadataValidation.IsAggregate(field.Type, selected)) + "⟩")) + "]⟩, " + computed.Alignment + "⟩"; + LeanText.Bool(MetadataValidation.IsAggregate(field.Type, selected)) + "⟩")) + "]⟩, " + + computed.Alignment + "⟩"; })) + "]"; internal static int[] Projection(ContractLayout[] layouts, IEnumerable artifactIdentities) @@ -141,6 +151,4 @@ internal static int[] Projection(ContractLayout[] layouts, IEnumerable a throw new InvalidDataException("Missing or duplicate artifact layout: " + layout.Identity); }).ToArray(); } - - static string Bool(bool value) => value ? "true" : "false"; } diff --git a/Extractor/InReferences.cs b/Extractor/InReferences.cs index a6d122944..1853b6932 100644 --- a/Extractor/InReferences.cs +++ b/Extractor/InReferences.cs @@ -6,13 +6,15 @@ namespace Nethermind.LeanCIL; // The modifier only affects signature identity: the parameter is the same reference a non-virtual `in` passes. internal static class InReferences { - const string Modifier = "System.Runtime.InteropServices.InAttribute"; + static readonly TypeLevel Modifier = new(MetadataName.OfText("System.Runtime.InteropServices"), + MetadataName.OfText("InAttribute")); // Exactly System.Runtime's InAttribute, required directly on a modifier-free by-reference type. internal static ByReferenceType? Unwrap(TypeReference type) => type is RequiredModifierType { ModifierType: var modifier, ElementType: ByReferenceType reference } && - modifier is not TypeSpecification && modifier.FullName == Modifier && - RuntimeModels.ExactScope(modifier.Scope, "System.Runtime") && !Modified(reference.ElementType) + modifier is not TypeSpecification && + IdentityRows.TopLevelMatches(modifier, Modifier, PlatformAssemblies.SystemRuntime) && + !Modified(reference.ElementType) ? reference : null; internal static bool Is(TypeReference type) => Unwrap(type) is not null; diff --git a/Extractor/InitAccessors.cs b/Extractor/InitAccessors.cs index 5857b9550..8d373758e 100644 --- a/Extractor/InitAccessors.cs +++ b/Extractor/InitAccessors.cs @@ -6,13 +6,13 @@ namespace Nethermind.LeanCIL; // which C# callers may bind the accessor: the method returns nothing, exactly as a plain void method. internal static class InitAccessors { - const string Modifier = "System.Runtime.CompilerServices.IsExternalInit"; + static readonly TypeLevel Modifier = new(MetadataName.OfText("System.Runtime.CompilerServices"), + MetadataName.OfText("IsExternalInit")); - // Roslyn binds the marker by full name from any assembly, since libraries for older frameworks - // declare their own; it carries no IL semantics wherever it is declared. + // The top-level marker may be declared in any assembly; it carries no IL semantics. internal static TypeReference? Unwrap(TypeReference type) => type is RequiredModifierType { ModifierType: var modifier, ElementType: var element } && - modifier is not TypeSpecification && modifier.DeclaringType is null && modifier.FullName == Modifier && + modifier is not TypeSpecification && IdentityRows.TopLevelMatches(modifier, Modifier) && element is not TypeSpecification && element.MetadataType == MetadataType.Void ? element : null; internal static bool Is(TypeReference type) => Unwrap(type) is not null; diff --git a/Extractor/InitializationAssumption.cs b/Extractor/InitializationAssumption.cs index e69f94374..60bcd8ee7 100644 --- a/Extractor/InitializationAssumption.cs +++ b/Extractor/InitializationAssumption.cs @@ -1,5 +1,3 @@ -using System.Text.Json; - namespace Nethermind.LeanCIL; // A deployment premise, not a claim that the CIL model executes a CLR type initializer. @@ -29,8 +27,8 @@ internal static void ValidateExport(ContractExport export, int format) } internal static string Lean(IEnumerable assumptions) => - "[" + string.Join(", ", assumptions.Select(item => "(" + JsonSerializer.Serialize(item.AssemblyIdentity) + - ", " + JsonSerializer.Serialize(item.TypeName) + ")")) + "]"; + $"[{string.Join(", ", assumptions.Select(item => + $"({IdentityText.Quote(item.AssemblyIdentity)}, {IdentityText.Quote(item.TypeName)})"))}]"; } internal sealed record ProfileInitializationAssumptions(string Profile, InitializationAssumption[] Types); diff --git a/Extractor/InstructionAdmission.cs b/Extractor/InstructionAdmission.cs new file mode 100644 index 000000000..ea26af490 --- /dev/null +++ b/Extractor/InstructionAdmission.cs @@ -0,0 +1,180 @@ +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace Nethermind.LeanCIL; + +/// The operand and target checks for each live instruction in a method's admitted call graph. +internal sealed class InstructionAdmission(MethodDefinition method, FeatureProfile profile, + IReadOnlyDictionary methods, ConstrainedCalls.TargetIndex constrainedTargets, + MetadataValidation.LayoutContext layouts, ImportResolution? imports, NativeOperands.Certificate native, + IReadOnlyDictionary homes) +{ + readonly InstructionIndex instructions = new(method); + + internal void Check(Instruction site) + { + InstructionPrefixes.ValidatePrefix(method, site); + if (site.Previous is { } previous) InstructionPrefixes.ValidatePrefix(method, previous); + if (imports?.Slot(site) is not null) return; + layouts.Require(method.Module, profile.Architecture); + var code = site.OpCode.Code; + if (code == Code.Calli && site.Operand is CallSite signature) + { + ManagedPointers.Catalog(signature, methods, methods[method], site.Previous?.OpCode.Code == Code.Tail); + return; + } + if (code == Code.Ldftn && site.Operand is MethodReference address) + { + var (target, index) = InstructionOperands.AddressTarget(address, methods); + ManagedPointers.Target(target, index); + ManagedPointers.SignatureEvidence(target); + return; + } + InstructionOperands.ValidateTailStack(site, native); + if (CheckArray(site)) return; + var typeTests = TypeTests.Analyze(method); + if (code == Code.Tail) + { + TailCalls.ValidateReachable(method, site, profile.Architecture); + return; + } + if (typeTests.Results.ContainsKey(site) || typeTests.Operands.Contains(site)) return; + var throws = Throws.Analyze(method); + if (code == Code.Constrained) + { + var next = site.Next ?? throw new InvalidDataException("Constrained prefix has no call"); + ConstrainedCalls.Resolve(method, next, instructions, profile.Architecture); + return; + } + if (!ObjectTypes.ReceiverCall(site) && + (code == Code.Callvirt || code == Code.Call && site.Previous?.OpCode.Code == Code.Constrained)) + { + InstructionOperands.ConstrainedTarget(method, site, instructions, profile, constrainedTargets, methods); + return; + } + if (throws.Raises.ContainsKey(site)) return; + if (throws.Operands.Contains(site)) return; + switch (code) + { + case Code.Call or Code.Callvirt or Code.Newobj: + CheckCall(site); + return; + case Code.Ldobj or Code.Stobj or Code.Initobj or Code.Sizeof or Code.Cpobj + when site.Operand is TypeReference operand: + InstructionOperands.ObjectStorage(method, site, operand, instructions, layouts, profile); + return; + case Code.Castclass or Code.Isinst: + ObjectTypes.ValidateTestOperand(site); + return; + case Code.Ldfld or Code.Stfld or Code.Ldflda: + CheckField(site); + return; + case Code.Ldsfld when FeatureProfile.PlatformField((FieldReference)site.Operand) is not null: + return; + case Code.Ldsfld or Code.Ldsflda or Code.Ldtoken when site.Operand is FieldReference: + StaticData.FieldOf(site, method.Module, profile.Architecture); + return; + case Code.Stsfld: + throw new InvalidDataException($""" + Unsupported static field store: {site} + Static fields are written only by their static constructor, which is never extracted. + """.ReplaceLineEndings("\n")); + case Code.Ldstr: + InstructionOperands.ValidateLiteral((string)site.Operand); + return; + case Code.Starg or Code.Starg_S or Code.Ldarga or Code.Ldarga_S: + InstructionOperands.ArgumentHomeSlot(method, site, homes); + return; + case Code.Ldarg or Code.Ldarg_S: + MethodArguments.Index(method, (ParameterDefinition)site.Operand); + return; + case Code.Ldloca or Code.Ldloca_S: + InstructionOperands.AddressedLocal(method, site); + return; + case Code.Ldloc or Code.Ldloc_S or Code.Stloc or Code.Stloc_S: + _ = ((VariableDefinition)site.Operand).Index; + return; + case Code.Switch: + foreach (var target in (Instruction[])site.Operand) instructions.Position(target); + return; + case Code.Ldc_I4 or Code.Ldc_I4_S: + Convert.ToInt64(site.Operand); + return; + case Code.Ldc_I8: + _ = (long)site.Operand; + return; + case Code.Cpblk or Code.Initblk: + MemoryPrefixes.BlockAlignment(site); + return; + case Code.Unaligned: + return; + default: + if (GenericEvidence.SimpleOpcodes.Contains(code)) + { + if (site.OpCode.OperandType is OperandType.InlineBrTarget or OperandType.ShortInlineBrTarget) + instructions.Position((Instruction)site.Operand); + return; + } + break; + } + throw new InvalidDataException($"Unsupported instruction: {site}"); + } + + void CheckField(Instruction site) + { + var operand = (FieldReference)site.Operand; + var field = operand.Resolve() ?? throw new InvalidDataException("Unresolved field"); + var code = site.OpCode.Code; + if (ObjectTypes.Candidate(operand.DeclaringType)) + ObjectTypes.FieldOperation(operand, field, code == Code.Stfld, code == Code.Ldflda, profile.Architecture); + else + InstructionOperands.StructField(operand, field, code, method, profile, layouts); + } + + bool CheckArray(Instruction site) + { + switch (site.OpCode.Code) + { + case Code.Newarr: + var element = (TypeReference)site.Operand; + if (ReferenceArrays.Element(element)) + { + LayoutEvidence.Scope(element); + return true; + } + return Arrays.Storage(element, profile.Architecture) is { Width: > 0 and <= ushort.MaxValue }; + case Code.Ldelem_Any when InstructionOperands.MethodTableLoad(site, native): + return true; + case Code.Ldelema or Code.Ldelem_Any or Code.Stelem_Any: + Arrays.ElementStorage(site, profile.Architecture); + return true; + case Code.Ldlen or Code.Ldelem_Ref or Code.Stelem_Ref or + Code.Ldelem_I1 or Code.Ldelem_U1 or Code.Ldelem_I2 or Code.Ldelem_U2 or + Code.Ldelem_I4 or Code.Ldelem_U4 or Code.Ldelem_I8 or Code.Ldelem_I or + Code.Stelem_I1 or Code.Stelem_I2 or Code.Stelem_I4 or Code.Stelem_I8 or Code.Stelem_I: + return true; + default: + return false; + } + } + + void CheckCall(Instruction site) + { + var code = site.OpCode.Code; + var reference = (MethodReference)site.Operand; + if (VirtualDispatch.Is(site)) + { + foreach (var entry in VirtualDispatch.Table(reference)) + InstructionOperands.ValidateVirtualTarget(entry, methods, methods[method]); + return; + } + if (code == Code.Call && RuntimeModels.CallModel(reference, profile) is not null || + code == Code.Newobj && Spans.ConstructorOperation(reference, profile.Architecture) is not null) return; + RuntimeModels.ValidateCallScope(reference); + var (target, _) = InstructionOperands.DirectTarget(reference, TailCalls.IsCall(site), + methods[method], methods); + if (code != Code.Newobj) return; + if (ObjectTypes.Candidate(target.DeclaringType)) ObjectTypes.AllocationOf(target); + else layouts.Size(MethodSpecialization.DeclaringType(target)); + } +} diff --git a/Extractor/InstructionOperands.cs b/Extractor/InstructionOperands.cs new file mode 100644 index 000000000..ccaa7a1a2 --- /dev/null +++ b/Extractor/InstructionOperands.cs @@ -0,0 +1,169 @@ +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace Nethermind.LeanCIL; + +/// The storage and managed targets admitted for raw instruction operands. +internal static class InstructionOperands +{ + /// The encoded CIL opcode, including the 0xFE prefix on two-byte opcodes. + internal static int RawOpcode(Instruction instruction) => (ushort)instruction.OpCode.Value; + + internal static bool MethodTableLoad(Instruction site, NativeOperands.Certificate native) => + native.Incoming.TryGetValue(site, out var stack) && stack.Length >= 2 && + stack[^2].Kind == NativeOperands.Kind.MethodTable; + + internal static void ValidateTailStack(Instruction site, NativeOperands.Certificate native) + { + if (TailCalls.IsCall(site) && site.Operand is MethodReference target && + native.Incoming.TryGetValue(site, out var stack) && + stack.Length != target.Parameters.Count + (target.HasThis ? 1 : 0)) + throw new InvalidDataException("Tail call requires exactly its arguments on the evaluation stack"); + } + + /// The frame slot whose address the CIL instruction pushes. + internal static int? AddressSlot(MethodDefinition method, Instruction site, IReadOnlyDictionary homes) => + site.OpCode.Code switch + { + Code.Ldloca or Code.Ldloca_S => AddressedLocal(method, site), + Code.Ldarga or Code.Ldarga_S => ArgumentHomeSlot(method, site, homes), + _ => null + }; + + internal static int AddressedLocal(MethodDefinition method, Instruction site) + { + var local = (VariableDefinition)site.Operand; + if (ObjectTypes.Rooted(local.VariableType) || ReferenceArrays.Is(local.VariableType)) + throw new InvalidDataException($"Unsupported address of a class instance local: {method.FullName}"); + return local.Index; + } + + /// The frame slot assigned by the original local or argument store. + internal static int? StoreSlot(MethodDefinition method, Instruction site, IReadOnlyDictionary homes) => + site.OpCode.Code switch + { + Code.Stloc_0 or Code.Stloc_1 or Code.Stloc_2 or Code.Stloc_3 or Code.Stloc or Code.Stloc_S => + FeatureConstants.Local(site), + Code.Starg or Code.Starg_S => ArgumentHomeSlot(method, site, homes), + _ => null + }; + + /// The local slot read by ldloc or by ldarg of an argument with a frame home. + internal static int? LoadSlot(MethodDefinition method, Instruction site, IReadOnlyDictionary homes) + { + if (site.OpCode.Code is Code.Ldloc_0 or Code.Ldloc_1 or Code.Ldloc_2 or Code.Ldloc_3 or Code.Ldloc or + Code.Ldloc_S) return FeatureConstants.Local(site); + return MethodArguments.LoadIndex(method, site) is int argument && homes.TryGetValue(argument, out int slot) + ? slot : null; + } + + internal static int ArgumentHomeSlot(MethodDefinition method, Instruction site, IReadOnlyDictionary homes) + { + var parameter = (ParameterDefinition)site.Operand; + if (site.OpCode.Code is Code.Ldarga or Code.Ldarga_S) + { + var receiver = method.HasThis && parameter == method.Body.ThisParameter; + if (receiver && ObjectTypes.Candidate(method.DeclaringType)) + throw new InvalidDataException($"Unsupported address of a class instance receiver: {method.FullName}"); + if (ObjectTypes.Rooted(parameter.ParameterType)) + throw new InvalidDataException($"Unsupported address of a class instance argument: {method.FullName}"); + } + var argument = MethodArguments.Index(method, parameter); + if (!homes.TryGetValue(argument, out var slot)) throw new InvalidDataException("Missing parameter storage"); + return slot; + } + + /// Whether this integer conversion throws on overflow. + internal static bool IsCheckedConversion(Code code) => code is + Code.Conv_Ovf_I1 or Code.Conv_Ovf_I1_Un or Code.Conv_Ovf_U1 or Code.Conv_Ovf_U1_Un or + Code.Conv_Ovf_I2 or Code.Conv_Ovf_I2_Un or Code.Conv_Ovf_U2 or Code.Conv_Ovf_U2_Un or + Code.Conv_Ovf_I4 or Code.Conv_Ovf_I4_Un or Code.Conv_Ovf_U4 or Code.Conv_Ovf_U4_Un or + Code.Conv_Ovf_I8 or Code.Conv_Ovf_I8_Un or Code.Conv_Ovf_U8 or Code.Conv_Ovf_U8_Un or + Code.Conv_Ovf_I or Code.Conv_Ovf_I_Un or Code.Conv_Ovf_U or Code.Conv_Ovf_U_Un; + + internal static (int? Bytes, int? ScalarWidth, bool Signed) ObjectStorage(MethodDefinition method, + Instruction site, TypeReference operand, InstructionIndex instructions, + MetadataValidation.LayoutContext layouts, FeatureProfile profile) + { + RuntimeModels.ValidateTypeIdentity(operand, method.Module, profile.Architecture); + if (Spans.Is(operand)) + { + if (site.OpCode.Code == Code.Initobj) SpanReferences.ValidateReset(site, instructions); + else if (site.OpCode.Code != Code.Ldobj) + throw new InvalidDataException($"Unsupported store to a span home: {site}"); + else SpanReferences.ValidateCopy(method, site, instructions); + } + else if (site.OpCode.Code == Code.Sizeof) + { + var size = MetadataValidation.IsAggregate(operand, method.Module) ? layouts.Size(operand) : + MetadataValidation.IntegerVectorWidth(operand) ?? MetadataValidation.PrimitiveWidth(operand); + return (size, null, false); + } + else if (!MetadataValidation.IsAggregate(operand, method.Module) && + MetadataValidation.IntegerVectorWidth(operand) is null) + { + var width = MetadataValidation.PrimitiveWidth(operand); + var signed = EnumTypes.Storage(operand).MetadataType is MetadataType.SByte or MetadataType.Int16; + return (null, width, signed); + } + return (null, null, false); + } + + internal static (Code Opcode, int Offset, int Width, bool Signed, bool IsAggregate) StructField(FieldReference operand, + FieldDefinition field, Code opcode, MethodDefinition method, FeatureProfile profile, + MetadataValidation.LayoutContext layouts) + { + layouts.Size(operand.DeclaringType); + var type = MetadataValidation.InstanceFieldOf(operand).Type; + RuntimeModels.ValidateTypeIdentity(type, method.Module, profile.Architecture); + var aggregate = MetadataValidation.IsAggregate(type, method.Module); + var width = layouts.FieldSize(type); + var offset = layouts.FieldOffset(operand.DeclaringType, field); + var signed = !aggregate && opcode == Code.Ldfld && + EnumTypes.Storage(type).FullName is "System.SByte" or "System.Int16"; + return (opcode, offset, width, signed, aggregate); + } + + internal static (MethodDefinition Method, int Index) DirectTarget(MethodReference reference, bool tail, + int caller, IReadOnlyDictionary methods) + { + var method = reference.Resolve() ?? throw new InvalidDataException($"Unresolved method: {reference.FullName}"); + var index = methods.GetValueOrDefault(method, -1); + if (index > caller || tail && index >= 0) return (method, index); + var scope = reference.DeclaringType.Scope.Name; + throw new InvalidDataException($"Unsupported or unresolved call: {reference.FullName} [{scope}]"); + } + + internal static (MethodDefinition Method, int Index) AddressTarget(MethodReference reference, + IReadOnlyDictionary methods) + { + var method = reference.Resolve() ?? throw new InvalidDataException("Unresolved managed pointer target"); + var index = methods.GetValueOrDefault(method, -1); + if (index < 0) throw new InvalidDataException("Managed pointer target is outside the catalog"); + return (method, index); + } + + internal static (MethodDefinition Method, int Index) ConstrainedTarget(MethodDefinition caller, Instruction site, + InstructionIndex instructions, FeatureProfile profile, ConstrainedCalls.TargetIndex targets, + IReadOnlyDictionary methods) + { + var method = targets.Find(ConstrainedCalls.Resolve(caller, site, instructions, profile.Architecture)); + var index = methods.GetValueOrDefault(method, -1); + if (index <= methods[caller]) throw new InvalidDataException("Unresolved constrained implementation"); + return (method, index); + } + + internal static void ValidateVirtualTarget(VirtualDispatch.Entry entry, + IReadOnlyDictionary methods, int caller) + { + var index = methods.GetValueOrDefault(entry.Body, -1); + if (index <= caller) + throw new InvalidDataException($"Unresolved virtual implementation: {entry.Body.FullName}"); + } + + internal static void ValidateLiteral(string text) + { + if (text.Any(character => char.IsSurrogate(character) || character is '\b' or '\f')) + throw new InvalidDataException("Unsupported string literal: a surrogate, backspace or form feed"); + } +} diff --git a/Extractor/InstructionPrefixes.cs b/Extractor/InstructionPrefixes.cs new file mode 100644 index 000000000..6ef7cdc39 --- /dev/null +++ b/Extractor/InstructionPrefixes.cs @@ -0,0 +1,82 @@ +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace Nethermind.LeanCIL; + +/// The ECMA-335 instruction prefixes, each of which modifies the instruction after it. +internal static class InstructionPrefixes +{ + internal static bool IsPrefix(Code code) => + code is Code.Unaligned or Code.Volatile or Code.Tail or Code.Constrained or Code.No or Code.Readonly; + + internal static bool IsStaticVirtualSlot(MethodDefinition? method) => + method is { IsStatic: true, IsVirtual: true, DeclaringType.IsInterface: true }; + + internal static void ValidatePrefix(MethodDefinition method, Instruction instruction) + { + var next = instruction.Next; + switch (instruction.OpCode.Code) + { + case Code.Unaligned: + if (instruction.Operand is not byte alignment || alignment is not (1 or 2 or 4) || + next is null || !MemoryPrefixes.SupportedTarget(next)) + throw new InvalidDataException("Unsupported or malformed unaligned prefix"); + break; + case Code.Volatile or Code.No: + throw new InvalidDataException($"Unsupported instruction: {instruction}"); + case Code.Tail: + var result = next switch + { + { OpCode.Code: Code.Call, Operand: MethodReference direct } => CallReturnType.Resolve(direct), + { OpCode.Code: Code.Callvirt, Operand: MethodReference { HasThis: true } instance } => + CallReturnType.Resolve(instance), + { OpCode.Code: Code.Calli, Operand: CallSite signature } => signature.ReturnType, + _ => null + }; + var returns = next?.Next is { OpCode.Code: Code.Ret, Operand: null }; + if (instruction.Operand is not null || result is null || !returns) + throw new InvalidDataException("Unsupported tail prefix: requires an adjacent call and return"); + if (StructuralSignatures.Current.Type(InitAccessors.Return(result)) != + StructuralSignatures.Current.Type(InitAccessors.Return(method.ReturnType))) + throw new InvalidDataException("Unsupported tail prefix: mismatched return type"); + break; + case Code.Constrained: + if (instruction.Operand is not TypeReference || next?.Operand is not MethodReference target || + next.OpCode.Code is not (Code.Call or Code.Callvirt)) + throw new InvalidDataException("Constrained prefix requires an adjacent constrained call"); + // An instance slot is called with callvirt and a static virtual slot with call. + if (next.OpCode.Code != (target.HasThis ? Code.Callvirt : Code.Call) || + !target.HasThis && !IsStaticVirtualSlot(target.Resolve())) + throw new InvalidDataException("Unsupported constrained method slot"); + break; + case Code.Readonly: + if (instruction.Operand is not null || + next is not { OpCode.Code: Code.Ldelema, Operand: TypeReference }) + throw new InvalidDataException("Readonly prefix requires an immediate array element address"); + break; + } + } + + /// + /// Checks every prefix and rejects branch or switch entry into its taker, including in unreachable code. + /// + internal static void Validate(MethodDefinition method) + { + foreach (var instruction in method.Body.Instructions) + { + ValidatePrefix(method, instruction); + Instruction?[] targets = instruction.Operand switch + { + Instruction target => [target], + Instruction[] arms => arms, + _ => [] + }; + foreach (var target in targets) + if (target?.Previous is { } prefix && IsPrefix(prefix.OpCode.Code)) + throw new InvalidDataException($""" + Branch enters the instruction after a prefix + {instruction} enters {target}, which {prefix.OpCode.Name} modifies. + """); + } + } +} diff --git a/Extractor/InstructionTranslation.cs b/Extractor/InstructionTranslation.cs index faa9e5713..76b8241cd 100644 --- a/Extractor/InstructionTranslation.cs +++ b/Extractor/InstructionTranslation.cs @@ -1,4 +1,3 @@ -using System.Text.Json; using Mono.Cecil; using Mono.Cecil.Cil; @@ -6,324 +5,123 @@ namespace Nethermind.LeanCIL; internal static class InstructionTranslation { - internal static string? RuntimeModel(MethodReference reference, FeatureProfile? profile = null) - { - if (FeatureProfile.PlatformConstant(reference) is int constant) return $".const32 {constant}"; - if (FeatureProfile.Getter(reference) is Feature feature) - return ".feature " + FeatureProfile.LeanFeature(feature); - return RuntimeModels.Translate(reference, profile ?? FeatureProfile.Scalar); - } - internal static string Translate(Instruction i, MethodDefinition m, - IReadOnlyDictionary methods, int methodIndex, InstructionIndex instructions, - FeatureProfile? profile = null, NativeOperands.Certificate? nativeOperands = null, + internal static Operation Translate(Instruction i, MethodDefinition m, + IReadOnlyDictionary methods, InstructionIndex instructions, + ResolvedCallSites callSites, FeatureProfile? profile = null, NativeOperands.Certificate? nativeOperands = null, IReadOnlyDictionary? parameterHomes = null, ImportResolution? imports = null, - MetadataValidation.LayoutContext? layouts = null, ConstrainedCalls.TargetIndex? constrainedTargets = null) + MetadataValidation.LayoutContext? layouts = null) { - if (imports is not null && imports.TryTranslate(i, methods.Count, out string imported)) return imported; + InstructionPrefixes.ValidatePrefix(m, i); + if (i.Previous is { } previous) InstructionPrefixes.ValidatePrefix(m, previous); + if (imports?.Slot(i) is not null) return Unoutlined.Site; profile ??= FeatureProfile.Scalar; - layouts?.Require(m.Module, profile.Architecture); - var reachable = nativeOperands is null || parameterHomes is null - ? Reachability.Analyze(m, profile) : null; + layouts ??= new(m.Module, profile.Architecture); + layouts.Require(m.Module, profile.Architecture); + var reachable = nativeOperands is null || parameterHomes is null ? Reachability.Analyze(m, profile) : null; nativeOperands ??= NativeOperands.Certify(m, reachable!, profile); - if (nativeOperands.Incoming.TryGetValue(i, out var dispatchStack)) - { - if (i.OpCode.Code == Code.Add && dispatchStack.Length >= 2 && - NativeOperands.NativeTableAddition(dispatchStack[^2], dispatchStack[^1])) - return ".managed .tableOffset"; - if (i.OpCode.Code == Code.Ldind_I && dispatchStack.LastOrDefault() is - { Kind: NativeOperands.Kind.NativeMethodTable, Metadata: { } signature }) - return ".managed (.tableLoad " + signature[".nativeMethodTable ".Length..] + ")"; - } - if (i.OpCode.Code == Code.Calli && i.Operand is Mono.Cecil.CallSite indirect) - return $".managed (.call {ManagedPointers.SignatureEvidence(indirect)} " + - $"{ManagedPointers.Catalog(indirect, methods, methodIndex, i.Previous?.OpCode.Code == Code.Tail)} " + - $"{(i.Previous?.OpCode.Code == Code.Tail ? "true" : "false")})"; - if (i.OpCode.Code is Code.Ldlen or Code.Ldelem_I or Code.Ldelem_Any && - nativeOperands.Incoming.TryGetValue(i, out var tableStack) && - tableStack.Length >= (i.OpCode.Code == Code.Ldlen ? 1 : 2) && - tableStack[^(i.OpCode.Code == Code.Ldlen ? 1 : 2)] is - { Kind: NativeOperands.Kind.MethodTable, Metadata: { } table }) - return i.OpCode.Code == Code.Ldlen ? ".managed .tableLength" : - ".managed (.tableElement " + table[".methodTable ".Length..] + ")"; - if (i.OpCode.Code == Code.Ldftn && i.Operand is MethodReference address) - { - var target = address.Resolve() ?? throw new InvalidDataException("Unresolved managed pointer target"); - var targetIndex = methods.GetValueOrDefault(target, -1); - if (targetIndex < 0) throw new InvalidDataException("Managed pointer target is outside the catalog"); - return $".managed (.address {ManagedPointers.TargetEvidence(target, targetIndex)} " + - $"{ManagedPointers.SignatureEvidence(target)})"; - } - if (TailCalls.IsCall(i) && i.Operand is MethodReference tailTarget && - nativeOperands.Incoming.TryGetValue(i, out var tailStack) && - tailStack.Length != tailTarget.Parameters.Count + (tailTarget.HasThis ? 1 : 0)) - throw new InvalidDataException("Tail call requires exactly its arguments on the evaluation stack"); - if (i.OpCode.Code == Code.Add && nativeOperands.Incoming.TryGetValue(i, out var incoming) && - incoming.Length >= 2 && NativeOperands.ReferenceAddition(incoming[^2], incoming[^1])) - return ".memory (.add 1 true)"; - if (i.OpCode.Code == Code.Readonly) - { - Arrays.ValidateReadOnly(instructions, i); - return ".nop"; - } - if (i.Previous?.OpCode.Code == Code.Readonly) Arrays.ValidateReadOnly(instructions, i.Previous); - var referenceArray = i.OpCode.Code == Code.Ldlen && - nativeOperands.Incoming.TryGetValue(i, out var lengthStack) && - lengthStack.LastOrDefault().Kind == NativeOperands.Kind.Object; - if (ReferenceArrays.Translate(i, referenceArray) is { } referenceOperation) return referenceOperation; + if (i.OpCode.Code is Code.Calli or Code.Ldftn) return Unoutlined.Site; + // Handler tables carry method pointers and cannot supply numeric array outline steps. + if (i.OpCode.Code == Code.Ldelem_Any && InstructionOperands.MethodTableLoad(i, nativeOperands)) + return Unoutlined.Site; + InstructionOperands.ValidateTailStack(i, nativeOperands); + if (ReferenceArrays.Translate(i) is { } referenceOperation) return referenceOperation; if (Arrays.Translate(i, profile.Architecture) is { } arrayOperation) return arrayOperation; - parameterHomes ??= ParameterStorage.IndexHomes(ParameterStorage.Analyze(m, reachable!)); - var translated = TranslateCore(i, m, methods, methodIndex, instructions, profile, parameterHomes, - layouts, constrainedTargets); - return nativeOperands.Mixed.Contains(i) ? $".nativeBinary ({translated})" : translated; + parameterHomes ??= ParameterStorage.IndexHomes(ParameterStorage.Analyze(m, reachable!), m.Body.Variables.Count); + return TranslateCore(i, m, methods, instructions, profile, parameterHomes, + layouts, callSites); } - static string TranslateCore(Instruction i, MethodDefinition m, IReadOnlyDictionary methods, - int methodIndex, InstructionIndex instructions, - FeatureProfile profile, IReadOnlyDictionary parameterHomes, MetadataValidation.LayoutContext? layouts, - ConstrainedCalls.TargetIndex? constrainedTargets) + static Operation TranslateCore(Instruction i, MethodDefinition m, + IReadOnlyDictionary methods, InstructionIndex instructions, + FeatureProfile profile, IReadOnlyDictionary parameterHomes, MetadataValidation.LayoutContext layouts, + ResolvedCallSites callSites) { - profile ??= FeatureProfile.Scalar; var typeTests = TypeTests.Analyze(m); - if (i.OpCode.Code == Code.Tail) + if (i.OpCode.Code is Code.Cpblk or Code.Initblk) { - TailCalls.ValidateReachable(m, i, profile.Architecture); - return ".nop"; + MemoryPrefixes.BlockAlignment(i); + return Unoutlined.Site; } - if (typeTests.Results.TryGetValue(i, out bool same)) + if (i.OpCode.Code == Code.Tail) { - if (i.Operand is Instruction) return same ? $".branch {Target()}" : ".nop"; - return same ? ".const32 1" : ".const32 0"; + TailCalls.ValidateReachable(m, i, profile.Architecture); + return StackOperation.Nop; } - if (typeTests.Operands.Contains(i)) return ".nop"; + if (typeTests.Operands.Contains(i) || i.OpCode.Code == Code.Call && typeTests.Results.ContainsKey(i)) + return Unoutlined.Site; var throws = Throws.Analyze(m); - if (i.OpCode.Code == Code.Constrained) - { - if (i.Next is null) throw new InvalidDataException("Constrained prefix has no call"); - ConstrainedCalls.Resolve(m, i.Next, instructions, profile.Architecture); - return ".nop"; - } + if (i.OpCode.Code == Code.Constrained) return StackOperation.Nop; var receiverCall = ObjectTypes.ReceiverCall(i); if (!receiverCall && (i.OpCode.Code == Code.Callvirt || i is { OpCode.Code: Code.Call, Previous.OpCode.Code: Code.Constrained })) { - var resolved = (constrainedTargets ?? new ConstrainedCalls.TargetIndex(methods.Keys)) - .Find(ConstrainedCalls.Resolve(m, i, instructions, profile.Architecture)); - var index = methods.GetValueOrDefault(resolved, -1); - if (index <= methodIndex) throw new InvalidDataException("Unresolved constrained implementation"); - return $".call {index} {resolved.Parameters.Count + (resolved.HasThis ? 1 : 0)}"; - } - if (throws.Raises.TryGetValue(i, out string? raised)) return $".throw \"{raised}\""; - var spans = Spans.Analyze(m, profile); - if (spans.Constructors.TryGetValue(i, out var constructor)) return constructor; - if (throws.Operands.Contains(i) || spans.Operands.Contains(i)) return ".nop"; - int Target() => instructions.Position((Instruction)i.Operand); - int Local() => ((VariableDefinition)i.Operand).Index; - string Narrow(TypeReference type) => LeanEmitter.Narrow(type) is { } kind ? $" (some .{kind})" : ""; - string LocalOp(string op, int index) => $".{op} {index}" + - (op is "local" or "setLocal" ? Narrow(m.Body.Variables[index].VariableType) : ""); - string Arg(int index) - { - var home = parameterHomes.GetValueOrDefault(index, -1); - return home < 0 ? $".arg {index}" + Narrow(MethodArguments.Type(m, index)) : - $".local {m.Body.Variables.Count + home}"; + callSites.Target(i, methods); + return Unoutlined.Site; } - string Parameter(string op) + if (throws.Raises.ContainsKey(i)) return new StackOperation(i.OpCode.Code); + if (i.OpCode.Code != Code.Ldnull && throws.Operands.Contains(i)) return StackOperation.Nop; + if (i.Operand is Instruction && + i.OpCode.OperandType is OperandType.InlineBrTarget or OperandType.ShortInlineBrTarget && + i.OpCode.Code is not (Code.Leave or Code.Leave_S)) + return Unoutlined.Site; + if (InstructionOperands.AddressSlot(m, i, parameterHomes).HasValue) return Unoutlined.Site; + if (InstructionOperands.StoreSlot(m, i, parameterHomes).HasValue) return Unoutlined.Site; + if (i.OpCode.Code is Code.Ldobj or Code.Stobj or Code.Initobj or Code.Sizeof or Code.Cpobj && + i.Operand is TypeReference operand) { - var home = parameterHomes.GetValueOrDefault(MethodArguments.Index(m, (ParameterDefinition)i.Operand), -1); - if (home < 0) throw new InvalidDataException("Missing parameter storage"); - return $".{op} {m.Body.Variables.Count + home}"; + InstructionOperands.ObjectStorage(m, i, operand, instructions, layouts, profile); + return Unoutlined.Site; } - int Layout(TypeReference type) => layouts?.Size(type) ?? - MetadataValidation.Layout(type, m.Module, profile.Architecture); - string Field(string op) + if (callSites.VirtualTargets(i) is not null) return Unoutlined.Site; + if (i.OpCode.Code is Code.Call or Code.Newobj || receiverCall) { - var f = (FieldReference)i.Operand; - FieldDefinition resolved = f.Resolve() ?? throw new InvalidDataException("Unresolved field"); - if (ObjectTypes.Candidate(f.DeclaringType)) - return ObjectTypes.FieldOperation(f, resolved, store: op == "setField", address: op == "fieldAddr", - profile.Architecture); - Layout(f.DeclaringType); - var actual = MetadataValidation.InstanceFieldOf(f); - var fieldType = actual.Type; - RuntimeModels.ValidateTypeIdentity(fieldType, m.Module, profile.Architecture); - var aggregate = MetadataValidation.IsAggregate(fieldType, m.Module); - int width = layouts?.FieldSize(fieldType) ?? - MetadataValidation.FieldSize(fieldType, m.Module, profile.Architecture); - var owner = f.DeclaringType; - int offset = layouts?.FieldOffset(owner, resolved) ?? - MetadataValidation.FieldOffset(owner, resolved, m.Module, profile.Architecture); - if (aggregate && op != "fieldAddr") - return $".{(op == "field" ? "aggregateFieldAt" : "setAggregateFieldAt")} {offset} {width}"; - return op == "fieldAddr" ? $".fieldAddress {offset}" : - $".{(op == "field" ? "fieldAt" : "setFieldAt")} {offset} {width}" + - (op == "field" && EnumTypes.Storage(fieldType).FullName is "System.SByte" or "System.Int16" ? " true" : ""); + var r = (MethodReference)i.Operand; + if (i.OpCode.Code == Code.Call && RuntimeModels.CallModel(r, profile) is { } model) + return model; + if (i.OpCode.Code == Code.Newobj && Spans.ConstructorOperation(r, profile.Architecture) is { } source) + return source; + if (i.OpCode.Code == Code.Newobj) return Unoutlined.Site; + callSites.Target(i, methods); + return Unoutlined.Site; } - if (i.OpCode.Code is Code.Ldobj or Code.Stobj or Code.Initobj or Code.Sizeof or Code.Cpobj && i.Operand is TypeReference operandType) - RuntimeModels.ValidateTypeIdentity(operandType, m.Module, profile.Architecture); - if (i.OpCode.Code is Code.Ldobj or Code.Stobj or Code.Initobj or Code.Cpobj && - i.Operand is TypeReference spanType && Spans.Is(spanType)) + if (i.OpCode.Code is Code.Castclass or Code.Isinst) { - // A span address on the stack is the view itself, so a copy leaves it in place. - if (i.OpCode.Code != Code.Ldobj) - throw new InvalidDataException($"Unsupported store to a span home: {i}"); - SpanReferences.ValidateCopy(m, i, instructions); - return ".nop"; + ObjectTypes.ValidateTestOperand(i); + return Unoutlined.Site; } - if (VirtualDispatch.Is(i)) + if (InstructionOperands.IsCheckedConversion(i.OpCode.Code)) return new StackOperation(i.OpCode.Code); + if (i.OpCode.Code == Code.Ldstr) { - var slot = (MethodReference)i.Operand; - var entries = VirtualDispatch.Table(slot).Select(entry => - { - var index = methods.GetValueOrDefault(entry.Body, -1); - if (index <= methodIndex) - throw new InvalidDataException("Unresolved virtual implementation: " + entry.Body.FullName); - return $"({GenericEvidence.ObjectType(entry.Class)}, {index})"; - }); - return $".callVirtual [{string.Join(", ", entries)}] {slot.Parameters.Count + 1}"; - } - if (i.OpCode.Code is Code.Call or Code.Newobj || receiverCall) - { - var r = (MethodReference)i.Operand; - if (i.OpCode.Code == Code.Call && RuntimeModels.ObjectConstructor(r)) return ".pop"; - if (RuntimeModel(r, profile) is string modeled) return modeled; - MethodDefinition resolved = r.Resolve() ?? throw new InvalidDataException($"Unresolved method: {r.FullName}"); - int index = methods.GetValueOrDefault(resolved, -1); - if (index > methodIndex || index >= 0 && TailCalls.IsCall(i)) - { - if (i.OpCode.Code == Code.Newobj && ObjectTypes.Candidate(resolved.DeclaringType)) - return ObjectTypes.Construction(resolved, index); - if (i.OpCode.Code == Code.Newobj) - return $".newAggregate {index} {resolved.Parameters.Count} {Layout(MethodSpecialization.DeclaringType(resolved))}"; - var operation = TailCalls.IsCall(i) ? "tailCall" : receiverCall ? "callInstance" : "call"; - return $".{operation} {index} {resolved.Parameters.Count + (resolved.HasThis ? 1 : 0)}"; - } - throw new InvalidDataException($"Unsupported or unresolved call: {r.FullName} [{r.DeclaringType.Scope.Name}]"); + InstructionOperands.ValidateLiteral((string)i.Operand); + return Unoutlined.Site; } return i.OpCode.Code switch { - Code.Nop => ".nop", + Code.Add or Code.Sub or Code.Mul or Code.And or Code.Or or Code.Xor or Code.Not or Code.Neg or + Code.Shl or Code.Shr or Code.Shr_Un or Code.Ceq or Code.Clt or Code.Clt_Un or Code.Cgt or Code.Cgt_Un or + Code.Conv_I1 or Code.Conv_I2 or Code.Conv_I4 or Code.Conv_I8 or Code.Conv_I or + Code.Conv_U1 or Code.Conv_U2 or Code.Conv_U4 or Code.Conv_U8 or Code.Conv_U or + Code.Add_Ovf or Code.Add_Ovf_Un or Code.Sub_Ovf or Code.Sub_Ovf_Un or Code.Mul_Ovf or Code.Mul_Ovf_Un or + Code.Div or Code.Div_Un or Code.Rem or Code.Rem_Un or Code.Dup or Code.Pop or Code.Ldnull or + Code.Ldind_I1 or Code.Ldind_U1 or Code.Ldind_I2 or Code.Ldind_U2 or Code.Ldind_I4 or Code.Ldind_U4 or + Code.Ldind_I8 or Code.Ldind_I or Code.Stind_I1 or Code.Stind_I2 or Code.Stind_I4 or Code.Stind_I8 or + Code.Stind_I or Code.Ret => + new StackOperation(i.OpCode.Code), + Code.Nop or Code.Readonly => StackOperation.Nop, // Ordinary accesses already use the documented byte-aligned runtime model. // For block instructions the validated prefix selects the required alignment. - Code.Unaligned => ".nop", - Code.Localloc => $".stackAlloc {(m.Body.InitLocals ? "true" : "false")}", - Code.Cpblk => $".memory (.copyBlock {MemoryPrefixes.BlockAlignment(i)})", - Code.Initblk => $".memory (.initBlock {MemoryPrefixes.BlockAlignment(i)})", - Code.Starg or Code.Starg_S => Parameter("setLocal"), - Code.Ldarga or Code.Ldarga_S when m.HasThis && i.Operand == m.Body.ThisParameter && - ObjectTypes.Candidate(m.DeclaringType) => - throw new InvalidDataException("Unsupported address of a class instance receiver: " + m.FullName), - Code.Ldarga or Code.Ldarga_S when ObjectTypes.Rooted(((ParameterDefinition)i.Operand).ParameterType) => - throw new InvalidDataException("Unsupported address of a class instance argument: " + m.FullName), - Code.Ldarga or Code.Ldarga_S => Parameter( - !Spans.Is(((ParameterDefinition)i.Operand).ParameterType) ? "localAddr" : "local"), - Code.Conv_I => ".convI8", - Code.Ldind_I => ".load64", Code.Stind_I => ".store64", - Code.Conv_Ovf_I1 => ".checkedConvert 8 true false", Code.Conv_Ovf_I1_Un => ".checkedConvert 8 true true", - Code.Conv_Ovf_U1 => ".checkedConvert 8 false false", Code.Conv_Ovf_U1_Un => ".checkedConvert 8 false true", - Code.Conv_Ovf_I2 => ".checkedConvert 16 true false", Code.Conv_Ovf_I2_Un => ".checkedConvert 16 true true", - Code.Conv_Ovf_U2 => ".checkedConvert 16 false false", Code.Conv_Ovf_U2_Un => ".checkedConvert 16 false true", - Code.Conv_Ovf_I4 => ".checkedConvert 32 true false", Code.Conv_Ovf_I4_Un => ".checkedConvert 32 true true", - Code.Conv_Ovf_U4 => ".checkedConvert 32 false false", Code.Conv_Ovf_U4_Un => ".checkedConvert 32 false true", - Code.Conv_Ovf_I8 => ".checkedConvert 64 true false", Code.Conv_Ovf_I8_Un => ".checkedConvert 64 true true", - Code.Conv_Ovf_U8 => ".checkedConvert 64 false false", Code.Conv_Ovf_U8_Un => ".checkedConvert 64 false true", - Code.Conv_Ovf_I => $".checkedConvert {profile.NativeWidth} true false", Code.Conv_Ovf_I_Un => $".checkedConvert {profile.NativeWidth} true true", - Code.Conv_Ovf_U => $".checkedConvert {profile.NativeWidth} false false", Code.Conv_Ovf_U_Un => $".checkedConvert {profile.NativeWidth} false true", - Code.Add_Ovf => ".checkedArithmetic .add true", Code.Add_Ovf_Un => ".checkedArithmetic .add false", - Code.Sub_Ovf => ".checkedArithmetic .sub true", Code.Sub_Ovf_Un => ".checkedArithmetic .sub false", - Code.Mul_Ovf => ".checkedArithmetic .mul true", Code.Mul_Ovf_Un => ".checkedArithmetic .mul false", - Code.Div => ".divide true false", Code.Div_Un => ".divide false false", - Code.Rem => ".divide true true", Code.Rem_Un => ".divide false true", - Code.Switch => ".switch [" + - string.Join(", ", ((Instruction[])i.Operand).Select(instructions.Position)) + "]", - Code.Sizeof => $".const32 {TypeWidth((TypeReference)i.Operand, m.Module, profile.Architecture, layouts)}", - Code.Cpobj => $".copyObject {TypeWidth((TypeReference)i.Operand, m.Module, profile.Architecture, layouts)}", - Code.Ldind_I1 => ".loadScalar 1 true", Code.Ldind_U1 => ".loadScalar 1", - Code.Ldind_I2 => ".loadScalar 2 true", Code.Ldind_U2 => ".loadScalar 2", - Code.Ldind_I4 or Code.Ldind_U4 => ".loadScalar 4", - Code.Stind_I1 => ".storeScalar 1", Code.Stind_I2 => ".storeScalar 2", Code.Stind_I4 => ".storeScalar 4", - Code.Conv_I1 => ".convI1", Code.Conv_I2 => ".convI2", Code.Conv_U2 => ".convU2", - Code.Neg => ".neg", - Code.Ldnull => ".memory .nullObject", - Code.Castclass or Code.Isinst => ObjectTypes.Test(i), - Code.Ldstr => StringLiteral((string)i.Operand), - Code.Cgt => ".gt", - Code.Bgt or Code.Bgt_S => $".bgt {Target()}", - Code.Ble or Code.Ble_S => $".ble {Target()}", - Code.Ble_Un or Code.Ble_Un_S => $".bleu {Target()}", - Code.Ldsfld when FeatureProfile.PlatformField((FieldReference)i.Operand) is int constant => - $".const32 {constant}", - Code.Ldsfld or Code.Ldsflda when StaticValues.IsLoad(i) => - $".memory (.{(i.OpCode.Code == Code.Ldsfld ? "staticValue" : "staticAddress")} " + - $"{StaticData.LeanBytes(StaticValues.Resolve((FieldReference)i.Operand, m.Module, profile.Architecture).Bytes)})", + Code.Unaligned => StackOperation.Nop, + Code.Localloc or Code.Switch => Unoutlined.Site, + Code.Ldsfld when FeatureProfile.PlatformField((FieldReference)i.Operand) is not null => Unoutlined.Site, + Code.Ldsfld or Code.Ldsflda or Code.Ldtoken when i.Operand is FieldReference => + Unoutlined.Site, Code.Stsfld => throw new InvalidDataException($"Unsupported static field store: {i}\n Static fields are written only by their static constructor, which is never extracted."), - Code.Ldsflda or Code.Ldtoken when i.Operand is FieldReference => $".memory (.staticAddress {StaticData.LeanBytes(StaticData.Validate((FieldReference)i.Operand, m.Module))})", - Code.Ldobj when MetadataValidation.IsAggregate((TypeReference)i.Operand, m.Module) => - $".loadAggregate {Layout((TypeReference)i.Operand)}", - Code.Initobj when MetadataValidation.IsAggregate((TypeReference)i.Operand, m.Module) => - $".initAggregate {Layout((TypeReference)i.Operand)}", - Code.Stobj when MetadataValidation.IsAggregate((TypeReference)i.Operand, m.Module) => - $".storeAggregate {Layout((TypeReference)i.Operand)}", - Code.Ldobj when MetadataValidation.IntegerVectorWidth((TypeReference)i.Operand) is int width => - $".memory .load{width * 8}", - Code.Stobj when MetadataValidation.IntegerVectorWidth((TypeReference)i.Operand) is int width => - $".memory .store{width * 8}", - Code.Initobj when MetadataValidation.IntegerVectorWidth((TypeReference)i.Operand) is int width => - $".initAggregate {width}", - Code.Ldobj => $".loadScalar {MetadataValidation.PrimitiveWidth((TypeReference)i.Operand)}" + - (EnumTypes.Storage((TypeReference)i.Operand).FullName is "System.SByte" or "System.Int16" ? " true" : ""), - Code.Stobj => $".storeScalar {MetadataValidation.PrimitiveWidth((TypeReference)i.Operand)}", - Code.Initobj => $".initAggregate {MetadataValidation.PrimitiveWidth((TypeReference)i.Operand)}", - Code.Ldarg_0 => Arg(0), Code.Ldarg_1 => Arg(1), Code.Ldarg_2 => Arg(2), Code.Ldarg_3 => Arg(3), - Code.Ldarg or Code.Ldarg_S => Arg(MethodArguments.Index(m, (ParameterDefinition)i.Operand)), - Code.Ldloc_0 => LocalOp("local", 0), Code.Ldloc_1 => LocalOp("local", 1), Code.Ldloc_2 => LocalOp("local", 2), Code.Ldloc_3 => LocalOp("local", 3), - Code.Ldloc or Code.Ldloc_S => LocalOp("local", Local()), - Code.Ldloca or Code.Ldloca_S when ObjectTypes.Rooted(((VariableDefinition)i.Operand).VariableType) || - ReferenceArrays.Is(((VariableDefinition)i.Operand).VariableType) => - throw new InvalidDataException("Unsupported address of a class instance local: " + m.FullName), - Code.Ldloca or Code.Ldloca_S => LocalOp( - !Spans.Is(((VariableDefinition)i.Operand).VariableType) ? "localAddr" : "local", Local()), - Code.Stloc_0 => LocalOp("setLocal", 0), Code.Stloc_1 => LocalOp("setLocal", 1), Code.Stloc_2 => LocalOp("setLocal", 2), Code.Stloc_3 => LocalOp("setLocal", 3), - Code.Stloc or Code.Stloc_S => LocalOp("setLocal", Local()), - Code.Ldfld => Field("field"), Code.Ldflda => Field("fieldAddr"), Code.Stfld => Field("setField"), - Code.Ldc_I4_M1 => ".const32 (BitVec.ofInt 32 (-1))", - Code.Ldc_I4_0 => ".const32 0", Code.Ldc_I4_1 => ".const32 1", - Code.Ldc_I4_2 => ".const32 2", Code.Ldc_I4_3 => ".const32 3", Code.Ldc_I4_4 => ".const32 4", - Code.Ldc_I4_5 => ".const32 5", Code.Ldc_I4_6 => ".const32 6", Code.Ldc_I4_7 => ".const32 7", Code.Ldc_I4_8 => ".const32 8", - Code.Ldc_I4 or Code.Ldc_I4_S => $".const32 (BitVec.ofInt 32 ({i.Operand}))", - Code.Ldc_I8 => $".const64 (BitVec.ofInt 64 ({i.Operand}))", - Code.Conv_I8 => ".convI8", Code.Conv_U => ".convU", Code.Conv_U1 => ".convU1", Code.Add => ".add", - Code.Conv_U4 => ".convU4", Code.Conv_U8 => ".convU8", Code.Not => ".bnot", - Code.Sub => ".sub", - Code.And => ".band", Code.Or => ".bor", Code.Xor => ".bxor", Code.Mul => ".mul", - Code.Shl => ".shl", Code.Shr => ".shr", Code.Shr_Un => ".shrUn", Code.Conv_I4 => ".convI4", - Code.Clt => ".lt", Code.Clt_Un => ".ltu", Code.Cgt_Un => ".gtu", Code.Ceq => ".eq", - Code.Ldind_I8 => ".load64", Code.Stind_I8 => ".store64", Code.Dup => ".dup", Code.Pop => ".pop", - Code.Br or Code.Br_S => $".branch {Target()}", - Code.Brfalse or Code.Brfalse_S => $".brzero {Target()}", - Code.Brtrue or Code.Brtrue_S => $".brnonzero {Target()}", - Code.Blt_Un or Code.Blt_Un_S => $".bltu {Target()}", - Code.Bge_Un or Code.Bge_Un_S => $".bgeu {Target()}", - Code.Bgt_Un or Code.Bgt_Un_S => $".bgtu {Target()}", - Code.Bge or Code.Bge_S => $".bge {Target()}", - Code.Blt or Code.Blt_S => $".blt {Target()}", - Code.Beq or Code.Beq_S => $".beq {Target()}", - Code.Bne_Un or Code.Bne_Un_S => $".bne {Target()}", - Code.Ret => ".ret" + Narrow(m.ReturnType), + _ when MethodArguments.LoadIndex(m, i).HasValue => Unoutlined.Site, + Code.Ldloc_0 or Code.Ldloc_1 or Code.Ldloc_2 or Code.Ldloc_3 or Code.Ldloc or Code.Ldloc_S => + Unoutlined.Site, + Code.Ldfld or Code.Ldflda or Code.Stfld => Unoutlined.Site, + >= Code.Ldc_I4_M1 and <= Code.Ldc_I8 => Unoutlined.Site, _ => throw new InvalidDataException($"Unsupported instruction: {i}") }; } - - static int TypeWidth(TypeReference type, ModuleDefinition module, string architecture, - MetadataValidation.LayoutContext? layouts) => MetadataValidation.IsAggregate(type, module) - ? layouts?.Size(type) ?? MetadataValidation.Layout(type, module, architecture) - : MetadataValidation.IntegerVectorWidth(type) ?? MetadataValidation.PrimitiveWidth(type); - - // Lean reads a surrogate escape as NUL, which would give distinct literals one key, and has no \b or \f. - static string StringLiteral(string text) - { - var unrepresentable = text.Any(c => char.IsSurrogate(c) || c is '\b' or '\f'); - if (unrepresentable) - throw new InvalidDataException("Unsupported string literal: a surrogate, backspace or form feed"); - return $".memory (.stringLiteral {JsonSerializer.Serialize(text)})"; - } } diff --git a/Extractor/LayoutEvidence.cs b/Extractor/LayoutEvidence.cs index ebf8acbe6..5b7dd4dfe 100644 --- a/Extractor/LayoutEvidence.cs +++ b/Extractor/LayoutEvidence.cs @@ -1,5 +1,4 @@ using System.Text; -using System.Text.Json; using Mono.Cecil; using Mono.Cecil.Cil; @@ -10,6 +9,8 @@ namespace Nethermind.LeanCIL; internal sealed class LayoutEvidence { readonly TypeReference[] rawTypes; + readonly string[] identityTerms; + readonly Dictionary namedOperandTypes = new(StringComparer.Ordinal); // Raw metadata is topological; emitted layout constants retain the caller supplied order. readonly Dictionary emittedIndices = new(StringComparer.Ordinal); readonly Dictionary typeIndices = new(StringComparer.Ordinal); @@ -22,8 +23,15 @@ internal LayoutEvidence(TypeReference[] types, MetadataValidation.LayoutContext for (var index = 0; index < types.Length; index++) emittedIndices.TryAdd(Identity(types[index]), index); rawTypes = Topological(types); + identityTerms = new string[rawTypes.Length]; for (var index = 0; index < rawTypes.Length; index++) - typeIndices.TryAdd(Identity(rawTypes[index]), index); + { + var type = rawTypes[index]; + var identity = Identity(type); + typeIndices.Add(identity, index); + identityTerms[index] = rawTypes.Length == 1 ? IdentityText.Quote(identity) : + ScopedIdentityTerm(identity, Scope(type).Length + "::".Length); + } } TypeReference[] Topological(TypeReference[] entries) @@ -41,12 +49,25 @@ void Include(TypeReference type) return [.. ordered]; } - internal static string Scope(TypeReference type) => type.Scope is AssemblyNameReference assembly - ? assembly.FullName : type.Module.Assembly.Name.FullName; + internal static string Scope(TypeReference type) + { + if (type.Scope is not AssemblyNameReference assembly) return type.Module.Assembly.Name.FullName; + // Primitive signature elements carry their module's core AssemblyRef directly. + var pins = PinnedAssemblies.Active; + if (pins?.Signatures.IsPrimitiveElement(type) == true) return assembly.FullName; + var module = pins?.ModuleOf(type); + if (module is not { HasExportedTypes: true }) return assembly.FullName; + var element = type.GetElementType(); + if (!module.ExportedTypes.Any(export => export.IsForwarder && export.FullName == element.FullName)) + return assembly.FullName; + return element.Resolve()?.Module.Assembly.Name.FullName ?? + throw new InvalidDataException("Unresolved forwarded type: " + element.FullName); + } internal static string Identity(TypeReference type) { - if (type is GenericInstanceType generic && GenericStructs.Is(type)) return ScopedArguments(generic); + if (type is GenericInstanceType generic && (ValueTuples.Is(type) || GenericStructs.Is(type))) + return ScopedArguments(generic); return Scope(type) + "::" + type.FullName; } @@ -58,7 +79,23 @@ type is GenericInstanceType generic && generic.ElementType.Scope is ModuleDefini static string ScopedArguments(GenericInstanceType generic) => Identity(generic.ElementType) + "<" + string.Join(",", generic.GenericArguments.Select(argument => MetadataValidation.IsAggregate(argument, generic.Module) - ? Identity(argument) : MethodSpecialization.SignatureType(argument))) + ">"; + ? Identity(argument) : ArgumentText(argument))) + ">"; + + static string ArgumentText(TypeReference type) => type switch + { + GenericParameter parameter => $"{parameter.Type}:{parameter.Position}", + ByReferenceType reference => ArgumentText(reference.ElementType) + "&", + RequiredModifierType modifier when InReferences.Is(modifier) => ArgumentText(modifier.ElementType) + " in", + RequiredModifierType modifier when InitAccessors.Is(modifier) => ArgumentText(modifier.ElementType) + " init", + PointerType pointer => ArgumentText(pointer.ElementType) + "*", + ArrayType { IsVector: true } array => ArgumentText(array.ElementType) + "[]", + GenericInstanceType instance => Identity(instance.ElementType) + "<" + + string.Join(",", instance.GenericArguments.Select(ArgumentText)) + ">", + FunctionPointerType pointer => $"fn:{pointer.CallingConvention}:{pointer.HasThis}:{pointer.ExplicitThis}:" + + ArgumentText(pointer.ReturnType) + "(" + + string.Join(",", pointer.Parameters.Select(parameter => ArgumentText(parameter.ParameterType))) + ")", + _ => Identity(type) + }; internal MetadataValidation.LayoutContext Metadata => layouts; @@ -66,6 +103,19 @@ static string ScopedArguments(GenericInstanceType generic) => internal int Index(TypeReference type) => typeIndices.GetValueOrDefault(Identity(type), -1); + // Layouts in one assembly share the UTF-8 encoding of its scope during kernel reduction. + static string ScopedIdentityTerm(string identity, int boundary) + { + var scope = IdentityText.Quote(identity[..boundary]); + var name = IdentityText.Quote(identity[boundary..]); + return $"({scope} ++ {name})"; + } + + internal string IdentityTerm(int index) => identityTerms[index]; + + /// The same operand type with a shared byte-backed name for a closed generic layout. + internal string NamedOperandType(string term) => namedOperandTypes.GetValueOrDefault(term, term); + internal string RawFieldType(TypeReference type, ModuleDefinition module) { if (MetadataValidation.IsAggregate(type, module) || layouts.IsImported(type)) @@ -74,7 +124,7 @@ internal string RawFieldType(TypeReference type, ModuleDefinition module) if (index < 0) throw new InvalidDataException("Missing raw aggregate metadata: " + type.FullName); return $".aggregate {index}"; } - if (MetadataValidation.VectorFieldWidth(type) is int width) return width == 16 ? ".vector128" : ".vector256"; + if (MetadataValidation.VectorFieldWidth(type) is int width) return $"(.vector{width * 8} {VectorLane(type)})"; return EnumTypes.Storage(type).FullName switch { "System.SByte" => ".signed8", "System.Byte" => ".unsigned8", "System.Boolean" => ".boolean", @@ -86,37 +136,66 @@ internal string RawFieldType(TypeReference type, ModuleDefinition module) }; } - internal void EmitLayouts(StringBuilder lean) + // A vector field's lane is its element's field kind; VectorFieldWidth admits only these elements. + static string VectorLane(TypeReference vector) => ((GenericInstanceType)vector).GenericArguments[0].FullName switch { + "System.Byte" => ".unsigned8", "System.Int32" => ".signed32", "System.UInt32" => ".unsigned32", + "System.Int64" => ".signed64", "System.UInt64" => ".unsigned64", + _ => throw new InvalidDataException($"Unsupported vector field lane: {vector.FullName}") + }; + + internal void EmitLayouts(StringBuilder lean, StringBuilder? decoderInputs = null) + { + var start = lean.Length; + var rowIdentities = (string[])identityTerms.Clone(); + for (var index = 0; index < rawTypes.Length; index++) + { + if (rawTypes[index] is not GenericInstanceType) continue; + var identity = Identity(rawTypes[index]); + var name = $"layoutIdentity{index}"; + var bytes = ByteLiterals.Of(IdentityText.Utf8(identity)); + var spelling = IdentityText.Quote(identity); + lean.AppendLine($"def {name} : String := Extraction.byteString {bytes} {spelling} (by cil_kernel_rfl)"); + rowIdentities[index] = name; + // Field resolution compares the owner's and held type's bytes with the bytes in the layout row. + namedOperandTypes[$".aggregate {identityTerms[index]} {index}"] = $".aggregate {name} {index}"; + } lean.AppendLine("def rawLayouts : List Extraction.RawLayout := ["); - lean.AppendLine(string.Join(",\n", rawTypes.Select(type => + lean.AppendLine(string.Join(",\n", rawTypes.Select((type, index) => { var metadata = ValueTuples.Is(type) ? ((GenericInstanceType)type).ElementType.Resolve() : type.Resolve(); var genericEvidence = ""; if (GenericStructs.Is(type) && type is GenericInstanceType closed) { var serializer = new GenericEvidence(null, this); - genericEvidence = $"generic := some {{ owner := .constructor " + - $"{JsonSerializer.Serialize(Scope(type))} {JsonSerializer.Serialize(metadata.FullName)}, " + - $"arity := {metadata.GenericParameters.Count}, arguments := " + - $"[{string.Join(", ", closed.GenericArguments.Select(serializer.Type))}], fields := " + - $"[{string.Join(", ", metadata.Fields.Where(field => !field.IsStatic) - .Select(field => serializer.Type(field.FieldType)))}], constraints := " + - $"[{string.Join(", ", metadata.GenericParameters.Select(parameter => - $"({(uint)parameter.Attributes}, [{string.Join(", ", parameter.Constraints.Select(constraint => - JsonSerializer.Serialize(GenericEvidence.ConstraintType(constraint.ConstraintType))))}])"))}] }}, "; + var owner = $".constructor {IdentityText.Quote(Scope(type))} {IdentityText.Quote(metadata.FullName)}"; + var arguments = string.Join(", ", closed.GenericArguments.Select(serializer.Type)); + var parameters = $"arity := {metadata.GenericParameters.Count}, arguments := [{arguments}]"; + var openFields = string.Join(", ", metadata.Fields.Where(field => !field.IsStatic) + .Select(field => serializer.Type(field.FieldType))); + var constraints = metadata.GenericParameters.Select(parameter => + { + var names = parameter.Constraints.Select(constraint => + IdentityText.Quote(GenericEvidence.ConstraintType(constraint.ConstraintType))); + return $"({(uint)parameter.Attributes}, [{string.Join(", ", names)}])"; + }); + var members = $"fields := [{openFields}], constraints := [{string.Join(", ", constraints)}]"; + genericEvidence = $$"""generic := some { owner := {{owner}}, {{parameters}}, {{members}} }, """; } var fields = layouts.Fields(type).Select(field => $"{{ type := {RawFieldType(field.Type, type.Module)}, offset := {(field.Definition.Offset < 0 ? "none" : $"some {field.Definition.Offset}")} }}"); - return $" {{ identity := {JsonSerializer.Serialize(Identity(type))}, {genericEvidence}" + - $"explicit := {metadata.IsExplicitLayout.ToString().ToLowerInvariant()}, " + - (metadata.IsAutoLayout ? "auto := true, " : "") + - (MetadataValidation.IsByRefLike(type) ? "byRefLike := true, " : "") + - // An imported entry repeats its published layout, which carries no interfaces. - (layouts.IsImported(type) ? "" : $"interfaces := [{string.Join(", ", metadata.Interfaces.Select( - implementation => JsonSerializer.Serialize(InterfaceIdentity(GenericStructs.Substitute( - implementation.InterfaceType, type as GenericInstanceType)))))}], ") + - $"packing := {metadata.PackingSize}, declaredSize := {metadata.ClassSize}, fields := [{string.Join(", ", fields)}] }}"; + var kind = $"explicit := {metadata.IsExplicitLayout.ToString().ToLowerInvariant()}, "; + var auto = metadata.IsAutoLayout ? "auto := true, " : ""; + var marker = MetadataValidation.IsByRefLike(type) ? "byRefLike := true, " : ""; + // An imported entry repeats its published layout, which carries no interfaces. + var interfaces = layouts.IsImported(type) ? "" : $"interfaces := [{string.Join(", ", + metadata.Interfaces.Select(implementation => IdentityText.Quote(InterfaceIdentity( + GenericStructs.Substitute(implementation.InterfaceType, type as GenericInstanceType)))))}], "; + var attributes = $"{genericEvidence}{kind}{auto}{marker}{interfaces}"; + var size = $"packing := {metadata.PackingSize}, declaredSize := {metadata.ClassSize}"; + var fieldList = string.Join(", ", fields); + var identity = rowIdentities[index]; + return $$""" { identity := {{identity}}, {{attributes}}{{size}}, fields := [{{fieldList}}] }"""; }))); lean.AppendLine("]"); lean.AppendLine("def computedLayouts : Array Extraction.ComputedLayout := #[" + @@ -126,6 +205,7 @@ internal void EmitLayouts(StringBuilder lean) var alignment = layouts.AggregateLayout(type).Alignment; return $"⟨layout{index}, {alignment}⟩"; })) + "]"); + decoderInputs?.Append(lean, start, lean.Length - start); lean.AppendLine("theorem layouts_validated : Extraction.LayoutsValid rawLayouts computedLayouts " + $".{Architecture} := " + LeanEmitter.Decide); } @@ -138,87 +218,10 @@ internal void EmitMethod(StringBuilder lean, MethodDefinition method, int number { var instruction = method.Body.Instructions[pc]; if (!reachable.Contains(instruction)) continue; - if (instruction.OpCode.Code is Code.Call or Code.Newobj && - instruction.Operand is MethodReference arrayCall && - arrayCall.Parameters.FirstOrDefault()?.ParameterType is ArrayType) - { - var element = arrayCall.DeclaringType is GenericInstanceType arraySpan && Spans.Is(arraySpan) - ? arraySpan.GenericArguments[0] - : arrayCall is GenericInstanceMethod extension && - extension.DeclaringType.FullName == "System.MemoryExtensions" && extension.Name == "AsSpan" - ? extension.GenericArguments[0] : null; - if (element is not null && MetadataValidation.IsAggregate(element, method.Module)) - uses.Add($".size {pc} {Index(element)} .arrayElement"); - } - if (instruction.OpCode.Code is Code.Call or Code.Newobj && - instruction.Operand is MethodReference spanCall) - { - if (spanCall is GenericInstanceMethod reinterpret && - spanCall.DeclaringType.FullName == "System.Runtime.InteropServices.MemoryMarshal" && - spanCall.Name is "AsBytes" or "Cast") - { - for (var argument = 0; argument < reinterpret.GenericArguments.Count; argument++) - if (MetadataValidation.IsAggregate(reinterpret.GenericArguments[argument], method.Module)) - uses.Add($".size {pc} {Index(reinterpret.GenericArguments[argument])} " + - (argument == 0 ? ".spanSource" : ".spanTarget")); - } - else if (spanCall.DeclaringType is GenericInstanceType spanOwner && - Spans.Is(spanOwner) && - spanCall.Parameters.FirstOrDefault()?.ParameterType is not ArrayType && - MetadataValidation.IsAggregate(spanOwner.GenericArguments[0], method.Module) && - spanCall.Name is ("get_Item" or "CopyTo" or "TryCopyTo" or "Clear" or "Fill" or - ".ctor" or "get_Empty")) - uses.Add($".size {pc} {Index(spanOwner.GenericArguments[0])} .spanElement"); - } - // Class fields sit at model offsets that Lean rechecks without a layout table. - if (instruction.OpCode.Code is Code.Ldfld or Code.Stfld or Code.Ldflda && - !ObjectTypes.Candidate(((FieldReference)instruction.Operand).DeclaringType)) - { - var reference = (FieldReference)instruction.Operand; - var field = reference.Resolve(); - var owner = reference.DeclaringType; - var fieldIndex = layouts.FieldIndex(owner, field); - var access = instruction.OpCode.Code switch { Code.Ldfld => "load", Code.Stfld => "store", _ => "address" }; - uses.Add($".field {pc} {Index(owner)} {fieldIndex} .{access}"); - } - // A class field holding an array of structs takes its element width from the struct's layout. - else if (instruction.OpCode.Code is Code.Ldfld or Code.Stfld && - instruction.Operand is FieldReference { FieldType: ArrayType { IsVector: true } fieldArray } && - MetadataValidation.IsAggregate(fieldArray.ElementType, method.Module)) - uses.Add($".size {pc} {Index(fieldArray.ElementType)} .arrayElement"); - else if (instruction.OpCode.Code == Code.Initobj && - instruction.Operand is GenericInstanceType span && Spans.Is(span) && - MetadataValidation.IsAggregate(span.GenericArguments[0], method.Module)) - uses.Add($".size {pc} {Index(span.GenericArguments[0])} .spanElement"); - else if (instruction.Operand is TypeReference type && MetadataValidation.IsAggregate(type, method.Module)) - { - var access = instruction.OpCode.Code switch - { - Code.Ldobj => "load", Code.Stobj => "store", Code.Initobj => "initialize", - Code.Cpobj => "copy", Code.Sizeof => "sizeOf", - Code.Newarr or Code.Ldelema or Code.Ldelem_Any or Code.Stelem_Any => "arrayElement", _ => null - }; - if (access is not null) uses.Add($".size {pc} {Index(type)} .{access}"); - } - else if (instruction.OpCode.Code == Code.Newobj && instruction.Operand is MethodReference constructor && + if (instruction.OpCode.Code == Code.Newobj && instruction.Operand is MethodReference constructor && MetadataValidation.IsAggregate(constructor.DeclaringType, method.Module)) uses.Add($".size {pc} {Index(constructor is MethodDefinition closedConstructor - ? MethodSpecialization.DeclaringType(closedConstructor) : constructor.DeclaringType)} .construct"); - else if (instruction.OpCode.Code == Code.Call && instruction.Operand is GenericInstanceMethod generic && - generic.DeclaringType.FullName is "System.Runtime.CompilerServices.Unsafe" or - "System.Runtime.InteropServices.MemoryMarshal" && generic.GenericArguments.Count == 1 && - MetadataValidation.IsAggregate(generic.GenericArguments[0], method.Module)) - { - var access = generic.Name switch - { - "SizeOf" => "sizeOf", "Add" => "referenceAdd", "Subtract" => "referenceSubtract", - "ReadUnaligned" or "Read" or "TryRead" => "load", - "WriteUnaligned" or "Write" or "TryWrite" => "store", - "CreateSpan" or "CreateReadOnlySpan" => "spanElement", _ => null - }; - if (access is not null) - uses.Add($".size {pc} {Index(generic.GenericArguments[0])} .{access}"); - } + ? MethodSpecialization.DeclaringType(closedConstructor) : constructor.DeclaringType)}"); } foreach (var local in method.Body.Variables) if (layouts.IsLayout(local.VariableType)) @@ -229,6 +232,5 @@ generic.DeclaringType.FullName is "System.Runtime.CompilerServices.Unsafe" or if (MetadataValidation.IsAggregate(type, method.Module)) uses.Add($".parameter {home} {Index(type)}"); } lean.AppendLine($"def method{number}LayoutUses : List Extraction.LayoutUse := [{string.Join(", ", uses)}]"); - lean.AppendLine($"theorem method{number}_layouts_validated : Extraction.MethodLayoutsValid rawLayouts computedLayouts method{number} method{number}LayoutUses := {LeanEmitter.Decide}"); } } diff --git a/Extractor/LeanEmitter.cs b/Extractor/LeanEmitter.cs index af006d6c6..7864cee0c 100644 --- a/Extractor/LeanEmitter.cs +++ b/Extractor/LeanEmitter.cs @@ -1,5 +1,4 @@ using System.Text; -using System.Text.Json; using Mono.Cecil; using Mono.Cecil.Cil; @@ -7,15 +6,18 @@ namespace Nethermind.LeanCIL; internal static class LeanEmitter { + /// An extraction's Lean source and what it is built from. internal readonly record struct Emission(string Source, string[] LayoutIdentities, - TemplateEmitter.Method[] Templates, ClassViews.View[] Views); + TemplateEmitter.Method[] Templates, ProofOutline.Method[] Outlines, ClassViews.View[] Views, + IReadOnlyList ClassFields, + ComputedLayout[] Layouts, string TemplateInputs); - internal static string? Narrow(TypeReference type) => EnumTypes.Storage(type).FullName switch + internal static Narrowing? Narrow(TypeReference type) => EnumTypes.Storage(type).FullName switch { - "System.SByte" => "signed8", - "System.Byte" or "System.Boolean" => "unsigned8", - "System.Int16" => "signed16", - "System.UInt16" or "System.Char" => "unsigned16", + "System.SByte" => Narrowing.Signed8, + "System.Byte" or "System.Boolean" => Narrowing.Unsigned8, + "System.Int16" => Narrowing.Signed16, + "System.UInt16" or "System.Char" => Narrowing.Unsigned16, _ => null }; @@ -25,71 +27,71 @@ static bool ReturnsValue(MethodDefinition method) => // The kernel rechecks every decide; elaborator whnf costs grow superlinearly with body size. internal const string Decide = "by decide +kernel"; - internal static string Kind(TypeReference type, ModuleDefinition module, string architecture = "scalar", + internal static LocalKind Kind(TypeReference type, ModuleDefinition module, string architecture = "scalar", MetadataValidation.LayoutContext? layouts = null) { layouts?.Require(module, architecture); if (ManagedPointers.Signature(type) is { } pointer) - return $".{(type is ArrayType ? "methodTable" : "methodPointer")} " + - ManagedPointers.SignatureEvidence(pointer); - if (type is ByReferenceType or PointerType) return ".reference"; - if (ObjectTypes.Reference(type) || ReferenceArrays.Is(type)) return ".object"; - if (Arrays.Of(type, architecture) is { } element) return $".array {element.Expression}"; - if (Spans.Width(type, architecture) is { } width) - return $".span {width} {(Spans.Writable(type) ? "true" : "false")}"; + return type is ArrayType ? new LocalKind.MethodTable(ManagedPointers.SignatureEvidence(pointer)) + : new LocalKind.MethodPointer(ManagedPointers.SignatureEvidence(pointer)); + if (type is ByReferenceType or PointerType) return new LocalKind.Reference(); + if (ObjectTypes.Reference(type) || ReferenceArrays.Is(type)) return new LocalKind.Object(); + if (Arrays.Of(type, architecture) is { } element) return new LocalKind.Array(element); + if (Spans.Width(type, architecture) is { } width) return new LocalKind.Span(width, Spans.Writable(type)); if (MetadataValidation.IsAggregate(type, module) || layouts?.IsImported(type) == true) - { - var size = layouts?.Size(type) ?? MetadataValidation.Layout(type, module, architecture); - return $".aggregate {size}"; - } - if (Narrow(type) is { } narrow) return $".narrow .{narrow}"; + return new LocalKind.Aggregate( + layouts?.Size(type) ?? MetadataValidation.Layout(type, module, architecture)); + if (Narrow(type) is { } narrow) return new LocalKind.Narrow(narrow); return EnumTypes.Storage(type).FullName switch { - "System.Int32" or "System.UInt32" => ".word32", - "System.Int64" or "System.UInt64" or "System.IntPtr" or "System.UIntPtr" => ".word64", - string s when s.StartsWith("System.Runtime.Intrinsics.Vector128`1<", StringComparison.Ordinal) => ".vector128", - string s when s.StartsWith("System.Runtime.Intrinsics.Vector256`1<", StringComparison.Ordinal) => ".vector256", - string s when s.StartsWith("System.Runtime.Intrinsics.Vector512`1<", StringComparison.Ordinal) => ".vector512", + "System.Int32" or "System.UInt32" => new LocalKind.Word32(), + "System.Int64" or "System.UInt64" or "System.IntPtr" or "System.UIntPtr" => new LocalKind.Word64(), + string s when s.StartsWith("System.Runtime.Intrinsics.Vector128`1<", StringComparison.Ordinal) => + new LocalKind.Vector(128), + string s when s.StartsWith("System.Runtime.Intrinsics.Vector256`1<", StringComparison.Ordinal) => + new LocalKind.Vector(256), + string s when s.StartsWith("System.Runtime.Intrinsics.Vector512`1<", StringComparison.Ordinal) => + new LocalKind.Vector(512), _ => throw new InvalidDataException($"Unsupported local: {type.FullName}") }; } // An opaque value names no initializer; its zero bytes fix only the load width. - static string OpaqueSite(int pc, StaticValues.Value value) + static StaticSite OpaqueSite(int pc, FieldRef reference, StaticValues.Value value) => + new(pc, StaticIdentity.Of(value.Field), reference, value.Bytes, + new OpaqueStaticValue(StaticValues.ScalarSign(value.Field))); + + static StaticSite SiteOf(Instruction i, int pc, FieldRef reference, MethodDefinition method, + IReadOnlyDictionary methodIndices, FeatureProfile profile) { - StaticIdentityRecord identity = StaticIdentity.Of(value.Field); - return $$""" - ({{pc}}, { identity := {{identity.Identity}}, - assemblyIdentity := {{JsonSerializer.Serialize(identity.AssemblyIdentity)}}, - fieldName := {{JsonSerializer.Serialize(value.Field.FullName)}}, - bytes := {{StaticData.LeanBytes(value.Bytes)}}, scalar := {{StaticValues.Scalar(value.Field)}}, - opaqueValue := true }) - """.ReplaceLineEndings(" "); + if (StaticValues.IsLoad(i)) + { + var value = StaticValues.Resolve((FieldReference)i.Operand, method.Module, profile.Architecture); + if (value.Opaque) return OpaqueSite(pc, reference, value); + int? initializer = value.Initializer is null + ? null : methodIndices.GetValueOrDefault(value.Initializer, -1); + if (initializer < 0) + throw new InvalidDataException($"Missing static initializer: {value.Initializer!.FullName}"); + return new(pc, StaticIdentity.Of(value.Field), reference, value.Bytes, + new InitializedStaticValue(StaticValues.ScalarSign(value.Field), initializer, value.Arguments, + value.Literal, value.Construct)); + } + FieldDefinition field = StaticData.Validate((FieldReference)i.Operand, method.Module); + return new(pc, StaticIdentity.Of(field), reference, field.InitialValue, + new StaticDataBlock(StaticData.Alignment(field))); } - static bool StaticSite(Instruction i) => i.OpCode.Code == Code.Ldsflda || StaticValues.IsLoad(i) || + static bool IsStaticSite(Instruction i) => i.OpCode.Code == Code.Ldsflda || StaticValues.IsLoad(i) || (i.OpCode.Code == Code.Ldtoken && i.Operand is FieldReference); - static FieldDefinition StaticField(Instruction i, ModuleDefinition module, string architecture) => StaticValues.IsLoad(i) - ? StaticValues.Resolve((FieldReference)i.Operand, module, architecture).Field : StaticData.Validate((FieldReference)i.Operand, module); internal static StaticIdentityRecord[] StaticIdentities(MethodDefinition[] methods, FeatureProfile profile) => - methods.SelectMany(method => Reachability.Analyze(method, profile)).Where(StaticSite) - .Select(instruction => StaticIdentity.Of(StaticField(instruction, methods[0].Module, profile.Architecture))) + methods.SelectMany(method => Reachability.Analyze(method, profile)).Where(IsStaticSite) + .Select(instruction => StaticIdentity.Of( + StaticData.FieldOf(instruction, methods[0].Module, profile.Architecture))) .Distinct().OrderBy(identity => identity.Identity, StringComparer.Ordinal).ToArray(); - internal static string Initial(TypeReference type, ModuleDefinition module, string architecture = "scalar", - MetadataValidation.LayoutContext? layouts = null) => Kind(type, module, architecture, layouts) switch - { - ".reference" or ".object" => ".nullRef", ".word32" => ".i32 0", ".word64" => ".i64 0", - string kind when kind.StartsWith(".narrow ", StringComparison.Ordinal) => ".i32 0", - string kind when kind.StartsWith(".span ", StringComparison.Ordinal) || - kind.StartsWith(".array ", StringComparison.Ordinal) || - kind.StartsWith(".methodPointer ", StringComparison.Ordinal) || - kind.StartsWith(".methodTable ", StringComparison.Ordinal) => ".nullRef", - ".vector128" => ".v128 0", ".vector256" => ".v256 0", ".vector512" => ".v512 0", - _ => ".aggregate (List.replicate " + - (layouts?.Size(type) ?? MetadataValidation.Layout(type, module, architecture)) + " 0)" - }; + internal static InitialValue Initial(TypeReference type, ModuleDefinition module, string architecture = "scalar", + MetadataValidation.LayoutContext? layouts = null) => Kind(type, module, architecture, layouts).Initial(); static TypeReference[] LayoutTypes(MethodDefinition[] methods, MetadataValidation.LayoutContext metadata) { - TypeReference[] types = methods + var types = methods .SelectMany(m => m.Parameters.Select(p => InReferences.Parameter(p.ParameterType)) .Concat(m.HasThis ? [MethodSpecialization.DeclaringType(m)] : []) .Concat(m.Body.Variables.Select(v => v.VariableType)).Append(InitAccessors.Return(m.ReturnType)) @@ -104,41 +106,35 @@ static TypeReference[] LayoutTypes(MethodDefinition[] methods, MetadataValidatio .Select(t => t is ByReferenceType r ? r.ElementType : t) .Select(t => t is ArrayType array ? array.ElementType : t) .Select(t => Spans.Is(t) ? ((GenericInstanceType)t).GenericArguments[0] : t) - .Where(metadata.IsLayout).Select(metadata.Canonical).DistinctBy(LayoutEvidence.Identity).ToArray(); + .Where(metadata.IsLayout); List layouts = []; - HashSet seenLayouts = new(StringComparer.Ordinal); - void IncludeLayout(TypeReference type) + HashSet seenReferences = new(ReferenceEqualityComparer.Instance); + Dictionary identities = new(StringComparer.Ordinal); + Func? signatureOf = null; + void IncludeLayout(TypeReference reference) { - if (!seenLayouts.Add(LayoutEvidence.Identity(type))) return; + if (!seenReferences.Add(reference)) return; + var spelling = LayoutEvidence.Identity(reference); + signatureOf ??= StructuralSignatures.Current.LayoutSnapshot(); + var signature = signatureOf(reference); + if (identities.TryGetValue(spelling, out var previous)) + { + if (previous != signature) throw new InvalidDataException($"Ambiguous layout identity: {spelling}"); + return; + } + identities.Add(spelling, signature); + var type = metadata.Canonical(reference); + if (!ReferenceEquals(type, reference) && signatureOf(type) != signature) + throw new InvalidDataException($"Ambiguous imported layout identity: {spelling}"); layouts.Add(type); foreach (MetadataValidation.InstanceField field in metadata.Fields(type)) - if (metadata.IsLayout(field.Type)) IncludeLayout(metadata.Canonical(field.Type)); + if (metadata.IsLayout(field.Type)) IncludeLayout(field.Type); } foreach (TypeReference type in types) IncludeLayout(type); return layouts.ToArray(); } - // These definitions guide proof simplification; the full validator still checks every call. - static string? CallDefinitions(int caller, IReadOnlyList operations, int methodCount) - { - var targets = new SortedSet { caller }; - var hasCall = false; - foreach (var operation in operations) - { - if (!operation.StartsWith(".call ", StringComparison.Ordinal) && - !operation.StartsWith(".callInstance ", StringComparison.Ordinal) && - !operation.StartsWith(".newAggregate ", StringComparison.Ordinal) && - !operation.StartsWith(".newObject ", StringComparison.Ordinal) && - !operation.StartsWith(".newVector256 ", StringComparison.Ordinal)) continue; - hasCall = true; - var fields = operation.Split(' ', 3); - if (fields.Length >= 2 && int.TryParse(fields[1], out var target) && - target >= 0 && target < methodCount) targets.Add(target); - } - if (!hasCall) return null; - return string.Join(", ", targets.SelectMany(target => - new[] { $"method{target}Closed", $"method{target}TypeArgumentsRaw" })); - } + static string LinkName(int method, int pc) => $"method{method}Link{pc}"; /// The recursion depth every generated declaration fits in, whatever the method length. const int BaseRecursionDepth = 4096; @@ -150,20 +146,57 @@ void IncludeLayout(TypeReference type) internal static int RecursionDepth(int longestBody) => Math.Max(BaseRecursionDepth, RecursionDepthPerInstruction * longestBody); + /// Lean's default heartbeat budget, which the instruction checks of a shorter body fit in. + const int BaseHeartbeats = 200_000; + + /// Twice the 60 heartbeats per instruction the instruction checks measured on an arithmetic body. + const int HeartbeatsPerInstruction = 120; + + /// Twice the 216 heartbeats each call site added to the instruction checks of a body of linked calls. + const int HeartbeatsPerCall = 432; + + // The kernel decides the instruction checks once per site and a call site costs more, so the budget counts both. + internal static int InstructionCheckHeartbeats(int instructions, int calls) => + Math.Max(BaseHeartbeats, HeartbeatsPerInstruction * instructions + HeartbeatsPerCall * calls); + + /// The closed decoder and its specialization determine the method and every code position. + static void EmitDecodedMethod(StringBuilder lean, int n, string fields, StringBuilder? decoderInputs) + { + var method = $"method{n}"; + var decoderArgs = $"profile {FrameMetadata.EnvironmentLean} method{n}Links {method}Reachable"; + var closedDecoder = $"Method.ofClosed? {decoderArgs} {method}Closed {method}RawArguments"; + decoderInputs?.AppendLine($"cil_decoded_method {method} := {closedDecoder}"); + var rawDecoder = $"Method.ofRaw? {decoderArgs} {method}Original {method}TypeArgumentsRaw"; + const string equality = "Extraction.SpecializationValid.ofRaw?_eq_ofClosed?"; + var environment = $"(environment := {FrameMetadata.EnvironmentLean})"; + var evidence = $"{method}_specialization_validated profile method{n}Links {method}Reachable"; + var closedProof = $"(congrArg Except.toOption {method}_decoder_eq)"; + var proof = $"Eq.trans ({equality} {environment} {evidence}) {closedProof}"; + var inputs = $"{decoderArgs} {method}Closed {method}RawArguments {method}"; + var code = $"Extraction.methodCode {decoderArgs} {method}Closed"; + var codeProof = $"@Method.ofClosed?_code {inputs} {method}_decoder_eq"; + lean.AppendLine($""" + cil_decoded_method {method} {fields}:= {closedDecoder} + theorem {method}_decoded : {rawDecoder} = some {method} := {proof} + theorem {method}_decoded_code : {method}.code = {code} := {codeProof} + """); + } + internal static Emission Emit(MethodDefinition[] methods, FeatureProfile profile, PinnedAssemblies pinned, - ImportResolution? importResolution = null, - StaticIdentityRecord[]? staticIdentities = null, bool collectTemplates = false) + ResolvedCallSites callSites, ImportResolution? importResolution = null, + StaticIdentityRecord[]? staticIdentities = null, bool collectOutlines = false, bool templateInputs = false) { var methodIndices = methods.Select((method, index) => (method, index)) .ToDictionary(pair => pair.method, pair => pair.index); - var constrainedTargets = new ConstrainedCalls.TargetIndex(methods); var templates = new List(); - var callDefinitions = new string?[methods.Length]; + var outlines = new List(); StringBuilder lean = new($""" import CIL.Extraction.Translation + import CIL.Extraction.DecodedDefinition import CIL.SymbolicExecution import CIL.InstructionEquations import CIL.Safety.Execution + import CIL.Safety.ResolutionFacts namespace Extracted open CIL @@ -172,169 +205,238 @@ open CIL """.ReplaceLineEndings("\n")); - lean.AppendLine($"@[cil_code] def profile : FeatureProfile := {profile.Lean}"); - lean.AppendLine("theorem profile_valid : profile.Valid := " + Decide); + StringBuilder? inputs = templateInputs ? new() : null; + var profileDefinition = $"def profile : FeatureProfile := {profile.Lean}"; + lean.AppendLine($"@[cil_code] {profileDefinition}"); + inputs?.AppendLine(profileDefinition); StaticIdentity.Validate(staticIdentities ?? StaticIdentities(methods, profile)); var layoutContext = new MetadataValidation.LayoutContext(methods[0].Module, profile.Architecture); if (importResolution is not null) layoutContext.AddImported(importResolution.LocalLayouts()); TypeReference[] types = LayoutTypes(methods, layoutContext); + var layouts = new ComputedLayout[types.Length]; for (int i = 0; i < types.Length; i++) { int size = layoutContext.Size(types[i]); - string fields = string.Join(", ", layoutContext.Fields(types[i]) - .Select(field => - { - var offset = layoutContext.FieldOffset(types[i], field.Definition); - var width = layoutContext.FieldSize(field.Type); - var aggregate = layoutContext.IsLayout(field.Type); - return $"⟨{offset}, {width}, {aggregate.ToString().ToLowerInvariant()}⟩"; - })); - lean.AppendLine($"def layout{i} : AggregateLayout := ⟨{size}, [{fields}]⟩"); + LayoutField[] fields = [.. layoutContext.Fields(types[i]).Select(field => new LayoutField( + layoutContext.FieldOffset(types[i], field.Definition), layoutContext.FieldSize(field.Type), + layoutContext.IsLayout(field.Type)))]; + layouts[i] = new ComputedLayout(LayoutEvidence.Identity(types[i]), size, fields); + var layout = $"def layout{i} : AggregateLayout := {layouts[i].ToLean()}"; + lean.AppendLine(layout); + inputs?.AppendLine(layout); lean.AppendLine($"theorem layout{i}_valid : layout{i}.Valid := {Decide}"); } LayoutEvidence layoutEvidence = new(types, layoutContext); - layoutEvidence.EmitLayouts(lean); + layoutEvidence.EmitLayouts(lean, inputs); + // One shared constant, so each method and its evidence name the tables instead of repeating them. + const string tables = "{ rawLayouts := rawLayouts, layouts := computedLayouts }"; + var environment = $"def {FrameMetadata.EnvironmentLean} : LayoutEnvironment := {tables}"; + lean.AppendLine($"@[cil_code] {environment}"); + inputs?.AppendLine(environment); + // The shape facts are written here once every method has been translated, so admission reports its own + // refusals first, and every method's components see them. + IReadOnlyList importBindings = importResolution?.Bindings ?? []; GenericEvidence genericEvidence = new(pinned, layoutEvidence, importResolution); + var importNames = importBindings.Select(binding => + genericEvidence.ExternalIdentities.Reference(binding.Identity, reachable: true)).ToArray(); + genericEvidence.ExternalIdentities.Emit(lean, inputs); + var slots = string.Join(", ", importBindings.Zip(importNames, ImportResolution.LeanSlot)); + lean.AppendLine($"""def imports : List ImportSlot := [{slots}]"""); + if (importBindings.Count != 0) + lean.AppendLine(""" + /-- The package's checked call slots and published layout identities. -/ + def importIndex : Extraction.ImportSlotIndex := .ofList imports + """); + var shapeFactsAt = lean.Length; + var operandShapes = new OperandShapes(layoutEvidence); + genericEvidence.EmitAssemblyMetadata(lean); var fieldAccesses = new List(); for (int n = 0; n < methods.Length; n++) { MethodDefinition m = methods[n]; - var instructions = new InstructionIndex(m); var featureCertificate = Reachability.Certify(m, profile); var reachable = featureCertificate.Keys.ToHashSet(); var scalarCertificate = NativeOperands.Certify(m, reachable, profile); var parameterIndices = ParameterStorage.Analyze(m, reachable); - var parameterHomes = ParameterStorage.IndexHomes(parameterIndices); var dispatch = NativeDispatch.For(m); - string LocalKind(TypeReference type, int? local = null) => dispatch.Kind(type, local) ?? + LocalKind KindOf(TypeReference type, int? local = null) => dispatch.Kind(type, local) ?? Kind(type, m.Module, profile.Architecture, layoutContext); - string parameters = string.Join(", ", parameterIndices.Select(index => - $"({index}, {LocalKind(MethodArguments.Type(m, index))})")); - string locals = string.Join(", ", m.Body.Variables.Select(variable => m.Body.InitLocals - ? dispatch.Kind(variable.VariableType, variable.Index) is not null ? ".nullRef" : - Initial(variable.VariableType, m.Module, profile.Architecture, layoutContext) : ".unmodeled")); - HashSet addressedLocals = m.Body.Instructions - .Where(instruction => instruction.OpCode.Code is Code.Ldloca or Code.Ldloca_S) - .Select(instruction => ((VariableDefinition)instruction.Operand).Index).ToHashSet(); - List ops = []; - foreach (Instruction i in m.Body.Instructions) - ops.Add(reachable.Contains(i) - ? InstructionTranslation.Translate(i, m, methodIndices, n, instructions, profile, - scalarCertificate, parameterHomes, importResolution, layoutContext, constrainedTargets) - : ".unsupported \"Unreachable under selected profile\""); - fieldAccesses.AddRange(m.Body.Instructions.Select((instruction, pc) => - ClassViews.Of(instruction, n, pc, ops[pc])).OfType()); - var needsOpaqueLocals = ops.Any(op => op.StartsWith(".intrinsic (.vector .createScalarUnsafe64) ", StringComparison.Ordinal)); - string opaqueLocals = needsOpaqueLocals ? string.Join(", ", m.Body.Variables - .Where(variable => variable.VariableType.FullName == "System.Runtime.Intrinsics.Vector128`1" && - !addressedLocals.Contains(variable.Index)).Select(variable => variable.Index)) : ""; - string kinds = string.Join(", ", m.Body.Variables.Select(variable => LocalKind(variable.VariableType, variable.Index))); - string sites = string.Join(", ", m.Body.Instructions.Select((i, pc) => (i, pc)) - .Where(s => reachable.Contains(s.i) && StaticSite(s.i)).Select(s => - { - if (StaticValues.IsLoad(s.i)) - { - StaticValues.Value value = StaticValues.Resolve((FieldReference)s.i.Operand, m.Module, profile.Architecture); - if (value.Opaque) return OpaqueSite(s.pc, value); - var initializer = value.Initializer is null ? 0 : methodIndices.GetValueOrDefault(value.Initializer, -1); - if (initializer < 0) - throw new InvalidDataException($"Missing static initializer: {value.Initializer!.FullName}"); - var literal = value.Literal is null ? "" : $", literal := some ({value.Literal})"; - var construct = value.Construct ? ", construct := true" : ""; - StaticIdentityRecord identity = StaticIdentity.Of(value.Field); - // Each method definition is emitted on one line, so the entry's line breaks become spaces. - return $$""" - ({{s.pc}}, { identity := {{identity.Identity}}, - assemblyIdentity := {{JsonSerializer.Serialize(identity.AssemblyIdentity)}}, - fieldName := {{JsonSerializer.Serialize(value.Field.FullName)}}, - bytes := {{StaticData.LeanBytes(value.Bytes)}}, scalar := {{StaticValues.Scalar(value.Field)}}, - initializer := some { method := {{initializer}}, - arguments := [{{string.Join(", ", value.Arguments)}}]{{literal}}{{construct}} } }) - """.ReplaceLineEndings(" "); - } - FieldDefinition f = StaticData.Validate((FieldReference)s.i.Operand, m.Module); - StaticIdentityRecord fieldIdentity = StaticIdentity.Of(f); - return $$""" - ({{s.pc}}, { identity := {{fieldIdentity.Identity}}, - assemblyIdentity := {{JsonSerializer.Serialize(fieldIdentity.AssemblyIdentity)}}, - fieldName := {{JsonSerializer.Serialize(f.FullName)}}, - bytes := {{StaticData.LeanBytes(f)}}, alignment := {{StaticData.Alignment(f)}} }) - """.ReplaceLineEndings(" "); - })); - lean.AppendLine($"def method{n}Signature : String := {JsonSerializer.Serialize(m.FullName)}"); + ParameterHome[] homes = [.. parameterIndices.Select(index => + new ParameterHome(index, KindOf(MethodArguments.Type(m, index))))]; + InitialValue[] locals = [.. m.Body.Variables.Select(variable => m.Body.InitLocals + ? dispatch.Kind(variable.VariableType, variable.Index) is not null ? InitialValue.NullRef : + Initial(variable.VariableType, m.Module, profile.Architecture, layoutContext) + : InitialValue.Unmodeled)]; + + fieldAccesses.AddRange(m.Body.Instructions.Select((site, pc) => reachable.Contains(site) + ? ClassViews.Of(site, n, pc, profile.Architecture) : null).OfType()); + LocalKind[] kinds = + [.. m.Body.Variables.Select(variable => KindOf(variable.VariableType, variable.Index))]; + StaticSite[] sites = [.. m.Body.Instructions.Select((i, pc) => (i, pc)) + .Where(s => reachable.Contains(s.i) && IsStaticSite(s.i)) + .Select(s => SiteOf(s.i, s.pc, genericEvidence.FieldReferenceOf(m, s.pc), m, methodIndices, + profile))]; + var frame = new FrameMetadata(ReturnsValue(m), locals, kinds, [], homes, sites, + genericEvidence.Declared(m)); + SortedSet? linkedMethods = templateInputs ? [] : null; + var linkEntries = callSites.Links(m, reachable, methodIndices, importResolution, + layoutContext, linkedMethods); + lean.AppendLine($"def method{n}Signature : String := {IdentityText.Quote(m.FullName)}"); if (MethodSpecialization.Source(m) is { } origin) { - lean.AppendLine($"def method{n}GenericDefinition : String := {JsonSerializer.Serialize(origin.Definition)}"); - lean.AppendLine($"def method{n}TypeArguments : List String := [{string.Join(", ", origin.TypeArguments.Select(t => JsonSerializer.Serialize(t)))}]"); + var definition = IdentityText.Quote(origin.Definition); + var arguments = string.Join(", ", origin.TypeArguments.Select(IdentityText.Quote)); + lean.AppendLine($"def method{n}GenericDefinition : String := {definition}"); + lean.AppendLine($"def method{n}TypeArguments : List String := [{arguments}]"); } - var metadata = $"returnsValue := {(ReturnsValue(m) ? "true" : "false")}, " + - $"locals := [{locals}], localKinds := [{kinds}], opaqueVectorLocals := [{opaqueLocals}], " + - $"parameterHomes := [{parameters}], staticSites := [{sites}]"; - callDefinitions[n] = CallDefinitions(n, ops, methods.Length); - if (collectTemplates) - templates.Add(new(m.FullName, metadata, ops.ToArray(), sites.Length != 0)); - lean.AppendLine($"def method{n} : Method := {{ profile := profile, {metadata}, code := ["); - lean.AppendLine(string.Join(",\n", ops.Select(op => " " + op)) + "] }"); + // Each link's text is written once, as a definition the table, the index and its facts name. + // The cil_code tag lets a simp-driven step unfold a fact's link to its constructor form. + var inputStart = lean.Length; + foreach (var (pc, link) in linkEntries) + lean.AppendLine($"@[cil_code] def {LinkName(n, pc)} : Link := {link}"); + // One named table, which the decoder takes, so a decoded method's links are the extractor's and its + // link facts name these definitions. + var linkTable = $"method{n}Links"; + lean.AppendLine($"@[cil_code] def {linkTable} : List (Nat × Link) := [" + string.Join(", ", + linkEntries.Select(entry => $"({entry.Pc}, {LinkName(n, entry.Pc)})")) + "]"); + inputs?.Append(lean, inputStart, lean.Length - inputStart); layoutEvidence.EmitMethod(lean, m, n, reachable, parameterIndices); + inputStart = lean.Length; ParameterEvidence.Emit(lean, m, n, reachable, layoutEvidence); - genericEvidence.EmitMethod(lean, m, n, reachable); + var staticInput = ""; + if (sites.Length != 0) + { + var table = $"method{n}StaticFields"; + var entries = sites.Select(site => + { + var descriptor = site.DescriptorLean(LeanText.MethodNumber); + var size = ""; + if (site.Detail is StaticDataBlock) + { + var field = (FieldReference)m.Body.Instructions[site.Pc].Operand; + size = $", rvaSize := some {field.FieldType.Resolve().ClassSize}"; + } + return $$"""({{site.Pc}}, { descriptor := {{descriptor}}{{size}} })"""; + }); + var fields = string.Join(", ", entries); + lean.AppendLine($"def {table} : List (Prod Nat Extraction.StaticFieldSite) := [{fields}]"); + staticInput = $"staticFields := {table}, "; + } + inputs?.Append(lean, inputStart, lean.Length - inputStart); + genericEvidence.EmitMethod(lean, m, n, reachable, methods.Length, staticInput, inputs); + // A field access names its operand's definition, so stepping reads that operand's shape fact. + var (naming, objectProof) = operandShapes.RecordMethod(m, n, reachable, genericEvidence); + if (GenericEvidence.FirstDecodeRefusal(m, reachable, callSites, importResolution) is { } refusal) + throw new GenericEvidence.UndecodedInstruction(m, refusal); + if (templateInputs) + { + var features = reachable.Where(site => site.OpCode.Code == Code.Call && callSites.IsModel(site)) + .Select(site => FeatureProfile.Getter((MethodReference)site.Operand)).OfType() + .Distinct().Order().ToArray(); + templates.Add(new(m.FullName, frame, features, + [.. m.Body.Instructions.Select(InstructionOperands.RawOpcode)], linkedMethods!.ToArray())); + } + if (collectOutlines) + { + var instructions = new InstructionIndex(m); + var parameterHomes = ParameterStorage.IndexHomes(parameterIndices, frame.Locals.Count); + var outlineOperations = new List(m.Body.Instructions.Count); + foreach (var site in m.Body.Instructions) + { + if (!reachable.Contains(site)) + { + outlineOperations.Add(Unoutlined.Site); + continue; + } + outlineOperations.Add(InstructionTranslation.Translate(site, m, methodIndices, instructions, + callSites, profile, scalarCertificate, parameterHomes, importResolution, layoutContext)); + } + outlines.Add(new(frame, [.. outlineOperations], m, reachable, parameterHomes, profile, + layoutContext, instructions, callSites, methodIndices, importResolution)); + } + // The decoder's raw definition is the method's only source; the command states its field facts and + // writes each field operand as the definition the operand shapes name. + var namedOperands = naming.Length == 0 ? "" : $"[{naming}] "; + EmitDecodedMethod(lean, n, namedOperands, inputs); + + const string layoutTables = "Extraction.MethodLayoutsValid rawLayouts computedLayouts"; + var layoutCheck = $"{layoutTables} method{n} method{n}LayoutUses"; + lean.AppendLine($"theorem method{n}_layouts_validated : {layoutCheck} := ⟨layouts_validated, {Decide}⟩"); + lean.AppendLine($"theorem method{n}_parameters_validated : Extraction.ParameterHomesValid " + + $"computedLayouts method{n}RawArguments method{n}Reachable method{n} := {Decide}"); FeatureEvidence.Emit(lean, m, n, featureCertificate); - lean.AppendLine($"@[cil_code] theorem method{n}_opaqueVectorLocals : method{n}.opaqueVectorLocals = [{opaqueLocals}] := rfl"); - ScalarEvidence.Emit(lean, m, n, scalarCertificate); - lean.AppendLine($"@[cil_code] theorem method{n}_parameterHomes : method{n}.parameterHomes = [{parameters}] := rfl"); - // The command states `method{n}_instruction{pc}` for each pc and proves it in time linear in the body. + ScalarEvidence.Emit(lean, m, n, scalarCertificate, objectProof); + // The command states `method{n}_instruction{pc}` for each pc and `method{n}_link{pc}` for each call site, + // in time linear in the body, so a stepping proof reads a link without evaluating the table lookup. lean.AppendLine($"cil_instruction_equations method{n}"); - foreach (string field in new[] { "profile", "locals", "localKinds", "returnsValue", "staticSites", "vectorLocals", "vectorArgs" }) - lean.AppendLine($"@[cil_code] theorem method{n}_{field} : method{n}.{field} = ({(field == "profile" ? "profile" : field == "locals" ? $"[{locals}]" : field == "localKinds" ? $"[{kinds}]" : field == "returnsValue" ? (ReturnsValue(m) ? "true" : "false") : field == "staticSites" ? $"[{sites}]" : "[]")}) := rfl"); + // A modelled call's step reads its model from the fact the command states for its pc. + if (reachable.Any(callSites.IsModel)) lean.AppendLine($"cil_runtime_models method{n}"); + lean.AppendLine($"cil_resolution_facts method{n}"); } - // Identities only: hashes and MVIDs change with every rebuild, so they stay in identity.json for `check`. - PinnedAssemblies.Pin[] pins = pinned.Pins.ToArray(); - for (var k = 0; k < pins.Length; k++) - lean.AppendLine($"def assembly{k} : Extraction.AssemblyPin := " + - $"{{ identity := {JsonSerializer.Serialize(pins[k].Identity)} }}"); - lean.AppendLine("def assemblies : List Extraction.AssemblyPin := [" + - string.Join(", ", Enumerable.Range(0, pins.Length).Select(k => $"assembly{k}")) + "]"); + // The platform definitions sit after the shape facts' position, so they go in first. + genericEvidence.InsertPlatformDefinitions(lean); + lean.Insert(shapeFactsAt, operandShapes.Lean()); lean.AppendLine("def specializedMethods : List Extraction.SpecializedMethod := [" + string.Join(", ", Enumerable.Range(0, methods.Length).Select(n => $"⟨method{n}TypeArgumentsRaw, method{n}Closed⟩")) + "]"); - IReadOnlyList importBindings = importResolution?.Bindings ?? []; - lean.AppendLine("def imports : List ImportSlot := [" + string.Join(", ", importBindings.Select(ImportResolution.LeanSlot)) + "]"); // Whole-program data, so it stays after the `specializedMethods` boundary of per-method components; // feature obligations read callee certificates through it. - lean.AppendLine("def methodEvidence : List Extraction.MethodEvidence := [" + string.Join(", ", - Enumerable.Range(0, methods.Length).Select(n => - $"{{ original := method{n}Original, arguments := method{n}TypeArgumentsRaw, " + - $"closed := method{n}Closed, parameterInput := method{n}RawArguments, " + - $"reachable := method{n}Reachable, featureStates := method{n}FeatureStates, " + - $"scalarStates := method{n}ScalarStates, layoutUses := method{n}LayoutUses, " + - $"dispatchTypes := method{n}DispatchTypes, " + - $"assembly := assembly{pinned.Index(methods[n].Module)} }}")) + "]"); + var records = Enumerable.Range(0, methods.Length).Select(n => + { + var method = $"method{n}"; + var fields = string.Join(", ", + $"original := {method}Original", $"body := {method}Body", $"arguments := {method}TypeArgumentsRaw", + $"closed := {method}Closed", $"parameterInput := {method}RawArguments", + $"reachable := {method}Reachable", + $"featureStates := {method}FeatureStates", $"scalarStates := {method}ScalarStates", + $"layoutUses := {method}LayoutUses", $"dispatchTypes := {method}DispatchTypes", + $"links := {method}Links", + $"assembly := assembly{pinned.Index(methods[n].Module)}", $"signature := {method}Signature"); + return $$"""{ {{fields}} }"""; + }); + lean.AppendLine($"""def methodEvidence : List Extraction.MethodEvidence := [{string.Join(", ", records)}]"""); for (var n = 0; n < methods.Length; n++) { FeatureEvidence.EmitTheorem(lean, n); - var facts = $"specializedMethods, imports, Extraction.lowerConstrainedRaw, {callDefinitions[n]}, " + - $"method{n}, method{n}Reachable, method{n}DispatchTypes"; - var proof = callDefinitions[n] is null ? Decide : $"by cil_managed_calls [{facts}]"; + lean.AppendLine($"theorem method{n}_models_validated : " + + $"Extraction.RuntimeModelsAgree method{n}Closed method{n}Reachable method{n} := " + + $"Extraction.RuntimeModelsAgree.of_closed method{n}_decoder_eq method{n}_features_validated"); lean.AppendLine($"theorem method{n}_calls_validated : Extraction.ManagedCallsValid " + - $"specializedMethods imports (Extraction.lowerConstrainedRaw method{n}DispatchTypes " + - $"method{n}Closed) method{n}Reachable method{n} := {proof}"); + $"specializedMethods imports (Extraction.callModelled method{n}) " + + $"(Extraction.lowerConstrainedRaw method{n}DispatchTypes " + + $"method{n}Closed) method{n}Reachable method{n} := {Decide}"); } lean.AppendLine("def program : Program := [" + string.Join(", ", Enumerable.Range(0, methods.Length).Select(n => $"method{n}")) + "]"); - // Proofs resolve methods by signature (CIL.MethodIdentity), so inserted helpers do not renumber them. - lean.AppendLine("def signatures : List String := [" + string.Join(", ", Enumerable.Range(0, methods.Length).Select(n => $"method{n}Signature")) + "]"); + // Proofs resolve methods by signature (CIL.MethodIdentity), so inserted helpers do not renumber them. Each + // entry is its evidence record's, which the body check equates with the full name its metadata row spells. + lean.AppendLine("def signatures : List String := methodEvidence.map Extraction.MethodEvidence.signature"); lean.AppendLine("theorem signatures_length : signatures.length = program.length := rfl"); - lean.AppendLine("def importSignatures : List String := [" + string.Join(", ", importBindings.Select(binding => JsonSerializer.Serialize(binding.Identity))) + "]"); + var signatures = string.Join(", ", importNames); + lean.AppendLine($"def importSignatures : List String := [{signatures}]"); lean.AppendLine("theorem importSignatures_length : importSignatures.length = imports.length := rfl"); lean.AppendLine("theorem statics_validated : Extraction.StaticValuesValid program := " + Decide); + lean.AppendLine("theorem static_identities_validated : Extraction.StaticIdentitiesValid program " + + "(methodEvidence.map (·.body)) := " + Decide); for (int n = 0; n < methods.Length; n++) { lean.AppendLine($"theorem method{n}_coverage_validated : Extraction.LayoutUsesComplete rawLayouts method{n}Closed method{n}RawArguments method{n}Reachable method{n}LayoutUses := {Decide}"); lean.AppendLine($"theorem method{n}_constrained_validated : Extraction.ConstrainedCallsValid method{n}DispatchTypes specializedMethods method{n}Closed method{n}Reachable method{n} := {Decide}"); - lean.AppendLine($"theorem method{n}_shape_validated : Extraction.MethodShapeValid rawLayouts method{n}Closed method{n} := {Decide}"); - lean.AppendLine($"theorem method{n}_unsafe_validated : Extraction.UnsafeCallsValid rawLayouts method{n}Closed method{n}Reachable method{n}.code := {Decide}"); - lean.AppendLine($"theorem method{n}_intrinsics_validated : Extraction.ExactIntrinsicsValid method{n}Closed method{n}Reachable method{n} := {Decide}"); + lean.AppendLine($"theorem method{n}_shape_validated : " + + $"Extraction.MethodShapeValid method{n}Closed method{n} := {Decide}"); + lean.AppendLine($"theorem method{n}_opaque_validated : Extraction.OpaqueLocalsValid method{n}Closed method{n} := {Decide}"); - lean.AppendLine($"theorem method{n}_vector_initializations_validated : Extraction.VectorInitializationsValid method{n}Closed method{n}Reachable method{n} := {Decide}"); - lean.AppendLine($"theorem method{n}_evidence_validated : Extraction.MethodEvidenceValid rawLayouts computedLayouts methodEvidence imports methodEvidence[{n}] method{n} :="); - lean.AppendLine($" ⟨method{n}_specialization_validated, method{n}_parameters_validated, method{n}_layouts_validated, method{n}_features_validated, method{n}_calls_validated, method{n}_coverage_validated, method{n}_shape_validated, method{n}_unsafe_validated, method{n}_intrinsics_validated, method{n}_opaque_validated, method{n}_vector_initializations_validated, method{n}_scalars_validated, method{n}_constrained_validated⟩"); + lean.AppendLine($"theorem method{n}_environment_validated : " + + $"Extraction.MethodEnvironmentValid rawLayouts computedLayouts method{n} := rfl"); + lean.AppendLine($"theorem method{n}_declared_validated : " + + $"Extraction.DeclaredValid method{n}Closed method{n} := rfl"); + lean.AppendLine($"theorem method{n}_evidence_validated : Extraction.MethodEvidenceValid rawLayouts " + + $"computedLayouts assemblies platformDefinitions methodEvidence imports methodEvidence[{n}] " + + $"method{n} :="); + lean.AppendLine($" ⟨method{n}_specialization_validated, method{n}_parameters_validated, " + + $"method{n}_layouts_validated, method{n}_features_validated, method{n}_calls_validated, " + + $"method{n}_coverage_validated, method{n}_shape_validated, method{n}_unsafe_validated, " + + $"method{n}_decoder_eq, method{n}_opaque_validated, " + + $"method{n}_object_operands_validated, method{n}_scalars_validated, " + + $"method{n}_constrained_validated, method{n}_body_validated,"); + lean.AppendLine($" method{n}_environment_validated, method{n}_declared_validated, rfl\u27e9"); } lean.AppendLine("theorem import_layouts_validated : Extraction.ImportedLayoutsValid imports profile.architecture := " + Decide); lean.AppendLine("#print axioms import_layouts_validated"); @@ -342,14 +444,26 @@ string LocalKind(TypeReference type, int? local = null) => dispatch.Kind(type, l "Extraction.ImportedLocalLayoutsBound rawLayouts computedLayouts imports := " + Decide); lean.AppendLine("theorem attribution_validated : " + "Extraction.AttributionValid (Extraction.bodyOwners methodEvidence) = true := " + Decide); - lean.AppendLine("theorem translation_valid : Extraction.TranslationValid rawLayouts computedLayouts methodEvidence imports program := by"); - lean.AppendLine(" apply Extraction.translation_of_methods (program := program) layouts_validated statics_validated import_layouts_validated"); + lean.AppendLine("theorem translation_valid : Extraction.TranslationValid rawLayouts computedLayouts " + + "assemblies platformDefinitions methodEvidence imports program := by"); + lean.AppendLine(" apply Extraction.translation_of_methods (program := program) layouts_validated"); + lean.AppendLine(" statics_validated static_identities_validated import_layouts_validated"); lean.AppendLine(" imported_locals_validated attribution_validated"); for (int n = 0; n < methods.Length; n++) lean.AppendLine($" refine Extraction.MethodsChecked.cons method{n}_evidence_validated ?_"); lean.AppendLine(" exact Extraction.MethodsChecked.nil"); - for (int n = 0; n < methods.Length; n++) - lean.AppendLine($"@[cil_code] theorem lookup{n} : program[{n}]? = some method{n} := rfl"); + // One index of the program, checked once, serves every closure and run that reads it. + lean.AppendLine("/-- The number of methods in the extracted program. -/"); + lean.AppendLine($"def programLength : Nat := {methods.Length}"); + lean.AppendLine("theorem programLength_eq : programLength = program.length := by cil_kernel_rfl"); + lean.AppendLine("/-- The balanced index of the extracted methods in program order. -/"); + lean.AppendLine("def programIndex : Extraction.FlowIndex Method := program_index% program"); + lean.AppendLine(""" + theorem programIndex_eq : programIndex = Extraction.FlowIndex.ofList program := by cil_kernel_rfl + """); + lean.AppendLine("theorem programIndex_get? (index : Nat) : programIndex.get? index = program[index]? := by"); + lean.AppendLine(" rw [programIndex_eq]; exact Extraction.FlowIndex.get?_ofList program index"); + ProgramLookupFacts.Append(lean, "program", "method", methods.Length); var classViews = ClassViews.Views(fieldAccesses); var views = ClassViews.Emit(fieldAccesses, classViews); if (views.Length > 0) @@ -360,6 +474,75 @@ string LocalKind(TypeReference type, int? local = null) => dispatch.Kind(type, l StringComparison.Ordinal) + imported.Length); } lean.AppendLine("end Extracted"); - return new(lean.ToString(), layoutEvidence.Identities, templates.ToArray(), classViews); + return new(lean.ToString(), layoutEvidence.Identities, templates.ToArray(), outlines.ToArray(), classViews, + fieldAccesses, layouts, + inputs?.ToString().ReplaceLineEndings("\n") ?? ""); + } + + /// + /// The distinct field and type operands of the reachable field and value-type instructions of every method, each + /// written once as a definition beside the fact that states how every method of the extraction resolves it. + /// + internal sealed class OperandShapes(LayoutEvidence layouts) + { + readonly Dictionary fields = []; + // TypeExpr has no typed C# form, so a type operand is keyed by its rendered term. + readonly Dictionary types = new(StringComparer.Ordinal); + readonly StringBuilder text = new(); + + /// Records the reachable raw operands and names the shape facts used by this method. + internal (string Names, string? ObjectProof) RecordMethod(MethodDefinition method, int index, + IReadOnlySet reachable, GenericEvidence evidence) + { + var names = new List(); + var operands = new List<(int Opcode, string Type)>(); + var needsLayout = false; + for (var pc = 0; pc < method.Body.Instructions.Count; pc++) + { + var instruction = method.Body.Instructions[pc]; + if (!reachable.Contains(instruction)) continue; + if (instruction.OpCode.Code is Code.Ldfld or Code.Ldflda or Code.Stfld) + names.Add(RecordField(evidence.FieldReferenceOf(method, pc))); + else if (instruction.OpCode.Code is + Code.Ldobj or Code.Stobj or Code.Initobj or Code.Cpobj or Code.Sizeof) + { + var type = (TypeReference)instruction.Operand; + var name = RecordType(evidence.ClosedOperandOf(instruction)); + names.Add(name); + operands.Add((InstructionOperands.RawOpcode(instruction), name)); + needsLayout |= !Spans.Is(type) && MetadataValidation.IsAggregate(type, method.Module); + } + } + // The method names each operand once, but its admission proof checks every object instruction in order. + var proof = needsLayout ? ObjectOperandProof.Render($"method{index}", operands) : null; + return (string.Join(", ", names.Distinct(StringComparer.Ordinal)), proof); + } + + string RecordField(FieldRef field) + { + if (fields.TryGetValue(field, out var name)) return name; + name = $"fieldRef{fields.Count}"; + fields.Add(field, name); + var operand = field with + { + Owner = layouts.NamedOperandType(field.Owner), Type = layouts.NamedOperandType(field.Type) + }; + text.AppendLine($"def {name} : Extraction.FieldRef := {operand.ToLean()}"); + text.AppendLine($"cil_field_shape {FrameMetadata.EnvironmentLean} {name}"); + return name; + } + + string RecordType(string type) + { + if (types.TryGetValue(type, out var name)) return name; + name = $"valueType{types.Count}"; + types.Add(type, name); + text.AppendLine($"def {name} : Extraction.TypeExpr := {layouts.NamedOperandType(type)}"); + text.AppendLine($"cil_value_shape {FrameMetadata.EnvironmentLean} {name}"); + return name; + } + + /// The definitions and facts of every recorded operand, in the order first seen. + internal string Lean() => text.ToString(); } } diff --git a/Extractor/LeanText.cs b/Extractor/LeanText.cs new file mode 100644 index 000000000..1ce19fccf --- /dev/null +++ b/Extractor/LeanText.cs @@ -0,0 +1,15 @@ +namespace Nethermind.LeanCIL; + +/// The spellings of Lean values that several renderers share. +internal static class LeanText +{ + /// A Lean `Bool` literal. + internal static string Bool(bool value) => value ? "true" : "false"; + + /// A method index written as its number, which a template replaces with a callee parameter. + internal static string MethodNumber(int method) => $"{method}"; + + /// The Lean list of the parenthesized class types a class derives from, nearest first. + internal static string ClassList(IEnumerable classes) => + "[" + string.Join(", ", classes.Select(type => $"({type})")) + "]"; +} diff --git a/Extractor/LocalKind.cs b/Extractor/LocalKind.cs new file mode 100644 index 000000000..ef0232a92 --- /dev/null +++ b/Extractor/LocalKind.cs @@ -0,0 +1,136 @@ +namespace Nethermind.LeanCIL; + +/// +/// What a local, argument home or evaluation stack slot holds, as the interpreter's value model distinguishes it. +/// renders the Lean kind term. +/// +internal abstract record LocalKind +{ + internal abstract string ToLean(); + + /// The value a local of this kind holds before its first assignment. + internal virtual InitialValue Initial() => + throw new InvalidOperationException("A stack-only kind has no local initial value: " + ToLean()); + + internal sealed record Reference : LocalKind + { + internal override string ToLean() => ".reference"; + + internal override InitialValue Initial() => InitialValue.NullRef; + } + + internal sealed record Object : LocalKind + { + internal override string ToLean() => ".object"; + + internal override InitialValue Initial() => InitialValue.NullRef; + } + + internal sealed record Array(Arrays.Element Element) : LocalKind + { + internal override string ToLean() => $".array {Element.Expression}"; + + internal override InitialValue Initial() => InitialValue.NullRef; + } + + internal sealed record Span(int Width, bool Writable) : LocalKind + { + internal override string ToLean() => $".span {Width} {LeanText.Bool(Writable)}"; + + internal override InitialValue Initial() => InitialValue.NullRef; + } + + internal sealed record Aggregate(int Size) : LocalKind + { + internal override string ToLean() => $".aggregate {Size}"; + + internal override InitialValue Initial() => new(InitialKind.Aggregate, Size); + } + + internal sealed record Narrow(Narrowing Narrowing) : LocalKind + { + internal override string ToLean() => $".narrow .{Narrowing.LeanName()}"; + + internal override InitialValue Initial() => InitialValue.Int32; + } + + internal sealed record Word32 : LocalKind + { + internal override string ToLean() => ".word32"; + + internal override InitialValue Initial() => InitialValue.Int32; + } + + internal sealed record Word64 : LocalKind + { + internal override string ToLean() => ".word64"; + + internal override InitialValue Initial() => InitialValue.Int64; + } + + /// A 128, 256 or 512 bit integer vector. + internal sealed record Vector(int Bits) : LocalKind + { + internal override string ToLean() => $".vector{Bits}"; + + internal override InitialValue Initial() => Bits switch + { + 128 => new(InitialKind.Vector128, 0), + 256 => new(InitialKind.Vector256, 0), + 512 => new(InitialKind.Vector512, 0), + _ => throw new InvalidOperationException($"Unsupported vector width: {Bits}") + }; + } + + /// A callable pointer of the given signature (a Lean term). + internal sealed record MethodPointer(string Signature) : LocalKind + { + internal override string ToLean() => $".methodPointer {Signature}"; + + internal override InitialValue Initial() => InitialValue.NullRef; + } + + /// A table of callable pointers of the given signature. + internal sealed record MethodTable(string Signature) : LocalKind + { + internal override string ToLean() => $".methodTable {Signature}"; + + internal override InitialValue Initial() => InitialValue.NullRef; + } + + /// A native address into a table of callable pointers; only the evaluation stack holds one. + internal sealed record NativeMethodTable(string Signature) : LocalKind + { + internal override string ToLean() => $".nativeMethodTable {Signature}"; + } + + /// A span or span reference over an enumeration element; only the evaluation stack holds one. + internal sealed record EnumSpan(Arrays.Element Element, bool Writable, bool ByReference) : LocalKind + { + internal override string ToLean() => + $".enumSpan {Element.Expression} {LeanText.Bool(Writable)} {LeanText.Bool(ByReference)}"; + } +} + +internal enum InitialKind { NullRef, Int32, Int64, Vector128, Vector256, Vector512, Aggregate, Unmodeled } + +/// A local's value before its first assignment; is an aggregate's size. +internal readonly record struct InitialValue(InitialKind Kind, int Size) +{ + internal static readonly InitialValue NullRef = new(InitialKind.NullRef, 0); + internal static readonly InitialValue Int32 = new(InitialKind.Int32, 0); + internal static readonly InitialValue Int64 = new(InitialKind.Int64, 0); + internal static readonly InitialValue Unmodeled = new(InitialKind.Unmodeled, 0); + + internal string ToLean() => Kind switch + { + InitialKind.NullRef => ".nullRef", + InitialKind.Int32 => ".i32 0", + InitialKind.Int64 => ".i64 0", + InitialKind.Vector128 => ".v128 0", + InitialKind.Vector256 => ".v256 0", + InitialKind.Vector512 => ".v512 0", + InitialKind.Aggregate => $".aggregate (List.replicate {Size} 0)", + _ => ".unmodeled" + }; +} diff --git a/Extractor/ManagedPointers.cs b/Extractor/ManagedPointers.cs index e079d33eb..ca691fc02 100644 --- a/Extractor/ManagedPointers.cs +++ b/Extractor/ManagedPointers.cs @@ -1,10 +1,19 @@ -using System.Text.Json; using System.Runtime.CompilerServices; using Mono.Cecil; using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; +/// +/// A managed method a callable pointer may name: its identity as a Lean term and its index in the extraction. +/// +internal sealed record ManagedTarget(string Identity, int Method) +{ + /// The Lean target term, with the method index written as of it. + internal string Lean(Func calleeName) => + $"{{ identity := {Identity}, method := {calleeName(Method)} }}"; +} + // Callable identities retain exact metadata; no numerical address is admitted as a target. internal static class ManagedPointers { @@ -15,7 +24,7 @@ sealed class CatalogCache(IReadOnlyDictionary methods) pair.Key.CallingConvention == MethodCallingConvention.Default) .GroupBy(pair => SignatureEvidence(pair.Key), StringComparer.Ordinal) .ToDictionary(group => group.Key, group => group.ToArray(), StringComparer.Ordinal); - internal readonly Dictionary Expressions = new(StringComparer.Ordinal); + internal readonly Dictionary> Catalogs = new(StringComparer.Ordinal); } static readonly ConditionalWeakTable, CatalogCache> Catalogs = new(); @@ -62,26 +71,33 @@ internal static string TypeEvidence(TypeReference type) if (type.MetadataType == MetadataType.Void) return ".void"; // Callable identity is independent of the extraction's checked layout-table ordering. if (MetadataValidation.IsAggregate(type, type.Module)) - return $".aggregate {JsonSerializer.Serialize(LayoutEvidence.Identity(type))} 0"; + return $".aggregate {IdentityText.Quote(LayoutEvidence.Identity(type))} 0"; if (type is GenericInstanceType generic && MetadataValidation.IntegerVectorWidth(type) is not null) - return $".apply (.constructor {JsonSerializer.Serialize(LayoutEvidence.Scope(generic.ElementType))} " + - $"{JsonSerializer.Serialize(generic.ElementType.FullName)}) " + - $"({TypeEvidence(generic.GenericArguments[0])})"; + { + var name = IdentityText.Quote(generic.ElementType.FullName); + var element = TypeEvidence(generic.GenericArguments[0]); + return $".apply (.constructor {GenericEvidence.Scope(generic.ElementType)} {name}) ({element})"; + } if (EnumTypes.IsEnum(type)) - return $".enum {JsonSerializer.Serialize(LayoutEvidence.Identity(type))} " + - $"({TypeEvidence(EnumTypes.Storage(type))})"; - return $".atom {JsonSerializer.Serialize(LayoutEvidence.Scope(type))} " + - JsonSerializer.Serialize(type.FullName); + { + var identity = IdentityText.Quote(LayoutEvidence.Identity(type)); + return $".enum {identity} ({TypeEvidence(EnumTypes.Storage(type))})"; + } + return $".atom {GenericEvidence.Scope(type)} {IdentityText.Quote(type.FullName)}"; } internal static string SignatureEvidence(IMethodSignature signature) => $"{{ result := {TypeEvidence(signature.ReturnType)}, parameters := " + $"[{string.Join(", ", signature.Parameters.Select(parameter => TypeEvidence(parameter.ParameterType)))}] }}"; - internal static string IdentityEvidence(MethodDefinition method) => - $"{{ owner := {JsonSerializer.Serialize(LayoutEvidence.Identity(method.DeclaringType))}, " + - $"token := {method.MetadataToken.ToUInt32()}, arguments := " + - $"[{string.Join(", ", MethodSpecialization.Arguments(method).Select(TypeEvidence))}] }}"; + internal static string IdentityEvidence(MethodDefinition method) + { + var owner = IdentityText.Quote(LayoutEvidence.Identity(method.DeclaringType)); + var arguments = string.Join(", ", + MethodSpecialization.Arguments(method).Select(GenericEvidence.CallableArgument)); + var token = method.MetadataToken.ToUInt32(); + return $$"""{ owner := {{owner}}, token := {{token}}, arguments := [{{arguments}}] }"""; + } internal static bool SameSignature(IMethodSignature left, IMethodSignature right) => !left.HasThis && !right.HasThis && !left.ExplicitThis && !right.ExplicitThis && @@ -106,21 +122,21 @@ internal static IMethodSignature ReferenceSignature(MethodReference target) return signature; } - internal static string TargetEvidence(MethodDefinition method, int index) => - $"{{ identity := {IdentityEvidence(method)}, method := {index} }}"; + internal static ManagedTarget Target(MethodDefinition method, int index) => + new(IdentityEvidence(method), index); - internal static string Catalog(IMethodSignature signature, + internal static ValueList Catalog(IMethodSignature signature, IReadOnlyDictionary methods, int caller, bool tail) { var signatureKey = SignatureEvidence(signature); var key = signatureKey + ":" + (tail ? -1 : caller); var cache = Catalogs.GetValue(methods, static values => new(values)); - if (cache.Expressions.TryGetValue(key, out var catalog)) return catalog; + if (cache.Catalogs.TryGetValue(key, out var catalog)) return catalog; var targets = cache.Targets.GetValueOrDefault(signatureKey, []) .Where(pair => tail || pair.Value > caller).ToArray(); if (targets.Length == 0) throw new InvalidDataException("Empty managed pointer target catalog"); - catalog = "[" + string.Join(", ", targets.Select(pair => TargetEvidence(pair.Key, pair.Value))) + "]"; - cache.Expressions.Add(key, catalog); + catalog = [.. targets.Select(pair => Target(pair.Key, pair.Value))]; + cache.Catalogs.Add(key, catalog); return catalog; } diff --git a/Extractor/MarshalModels.cs b/Extractor/MarshalModels.cs new file mode 100644 index 000000000..ae57bc104 --- /dev/null +++ b/Extractor/MarshalModels.cs @@ -0,0 +1,75 @@ +using Mono.Cecil; + +namespace Nethermind.LeanCIL; + +// The open signature fixes each overload. Closed element types supply its storage widths. +internal static class MarshalModels +{ + internal static Operation? CallModel(MethodReference reference, string architecture) + { + if (reference is not GenericInstanceMethod method || reference.HasThis || reference.ExplicitThis || + reference.DeclaringType is TypeSpecification || + reference.DeclaringType.FullName != "System.Runtime.InteropServices.MemoryMarshal" || + !RuntimeModels.ExactScope(reference.DeclaringType, PlatformAssemblies.SystemRuntime) || + method.ElementMethod.CallingConvention != MethodCallingConvention.Generic) return null; + var arity = reference.Name == "Cast" ? 2 : 1; + if (method.GenericArguments.Count != arity || method.ElementMethod.GenericParameters.Count != arity || + method.Parameters.Count is < 1 or > 2) return null; + + var source = method.ElementMethod.GenericParameters[0]; + var input = method.Parameters[0].ParameterType; + var output = method.ReturnType; + var writable = Spans.Writable(input); + var second = method.Parameters.Count == 2 ? method.Parameters[1].ParameterType : null; + var byteType = method.Module.TypeSystem.Byte; + bool Reference(TypeReference? type) => type is ByReferenceType address && address.ElementType == source; + var matches = method.Name switch + { + "GetReference" => second is null && Span(input, writable, source) && Reference(output), + "GetArrayDataReference" => second is null && Reference(output) && + input is ArrayType { IsVector: true } array && array.ElementType == source, + "AsBytes" => second is null && Span(input, writable, source) && Span(output, writable, byteType), + "Cast" => second is null && Span(input, writable, source) && + Span(output, writable, method.ElementMethod.GenericParameters[1]), + "CreateSpan" or "CreateReadOnlySpan" => Reference(input) && second?.MetadataType == MetadataType.Int32 && + Span(output, method.Name == "CreateSpan", source), + "Read" => second is null && Span(input, false, byteType) && output == source, + "TryRead" => Span(input, false, byteType) && Reference(second) && + output.MetadataType == MetadataType.Boolean, + "Write" => Span(input, true, byteType) && Reference(second) && output.MetadataType == MetadataType.Void, + "TryWrite" => Span(input, true, byteType) && Reference(second) && + output.MetadataType == MetadataType.Boolean, + _ => false + }; + if (!matches) return null; + RuntimeModels.ValidateSignature(reference, architecture); + var element = method.GenericArguments[0]; + if (Spans.ElementWidth(element, architecture) is null) return null; + switch (method.Name) + { + case "GetReference": + case "GetArrayDataReference": + case "CreateSpan": + case "CreateReadOnlySpan": + return Unoutlined.Site; + case "AsBytes": + case "Cast": + var target = arity == 1 ? 1 : Spans.ElementWidth(method.GenericArguments[1], architecture); + return target is null ? null : Unoutlined.Site; + default: + MemoryTransfers.ValidateElement(element, method.Module, architecture); + return Unoutlined.Site; + } + } + + private static bool Span(TypeReference type, bool writable, TypeReference element) + { + if (type is not GenericInstanceType { GenericArguments.Count: 1 } span || + span.ElementType.FullName != (writable ? "System.Span`1" : "System.ReadOnlySpan`1")) return false; + var definition = span.ElementType; + if (!RuntimeModels.ExactScope(definition, PlatformAssemblies.SystemRuntime)) + throw new InvalidDataException($"Unsupported type identity: {definition.FullName} [{definition.Scope}]"); + var argument = span.GenericArguments[0]; + return element is GenericParameter ? argument == element : argument.MetadataType == MetadataType.Byte; + } +} diff --git a/Extractor/MemoryPrefixes.cs b/Extractor/MemoryPrefixes.cs index 3cb8385ff..8dce7c093 100644 --- a/Extractor/MemoryPrefixes.cs +++ b/Extractor/MemoryPrefixes.cs @@ -1,35 +1,18 @@ -using Mono.Cecil; using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; internal static class MemoryPrefixes { - static bool SupportedTarget(Code code) => code is Code.Cpblk or Code.Initblk or - Code.Ldind_I1 or Code.Ldind_U1 or Code.Ldind_I2 or Code.Ldind_U2 or - Code.Ldind_I4 or Code.Ldind_U4 or Code.Ldind_I8 or Code.Ldind_I or - Code.Stind_I1 or Code.Stind_I2 or Code.Stind_I4 or Code.Stind_I8 or Code.Stind_I or - Code.Ldfld or Code.Stfld or Code.Ldobj or Code.Stobj; - - internal static void Validate(MethodDefinition method) + internal static bool SupportedTarget(Instruction instruction) => instruction.OpCode.Code switch { - foreach (var instruction in method.Body.Instructions) - { - if (instruction.OpCode.Code == Code.Unaligned && - (instruction.Operand is not byte alignment || alignment is not (1 or 2 or 4) || - instruction.Next is null || !SupportedTarget(instruction.Next.OpCode.Code))) - throw new InvalidDataException("Unsupported or malformed unaligned prefix"); - - var targets = instruction.Operand switch - { - Instruction target => new[] { target }, - Instruction[] branches => branches, - _ => [] - }; - if (targets.Any(target => target?.Previous?.OpCode.Code == Code.Unaligned)) - throw new InvalidDataException("Branch enters the middle of an unaligned instruction"); - } - } + Code.Cpblk or Code.Initblk or Code.Ldind_I1 or Code.Ldind_U1 or Code.Ldind_I2 or Code.Ldind_U2 or + Code.Ldind_I4 or Code.Ldind_U4 or Code.Ldind_I8 or Code.Ldind_I or Code.Stind_I1 or + Code.Stind_I2 or Code.Stind_I4 or Code.Stind_I8 or Code.Stind_I => instruction.Operand is null, + Code.Ldfld or Code.Stfld => instruction.Operand is Mono.Cecil.FieldReference, + Code.Ldobj or Code.Stobj => instruction.Operand is Mono.Cecil.TypeReference, + _ => false + }; internal static int BlockAlignment(Instruction instruction) => instruction.Previous?.OpCode.Code == Code.Unaligned ? (byte)instruction.Previous.Operand : 8; diff --git a/Extractor/MemoryTransfers.cs b/Extractor/MemoryTransfers.cs index 0fae07390..7f3c90dd6 100644 --- a/Extractor/MemoryTransfers.cs +++ b/Extractor/MemoryTransfers.cs @@ -5,66 +5,29 @@ namespace Nethermind.LeanCIL; // Managed-reference transfers share the exact layouts used by typed CIL loads and stores. internal static class MemoryTransfers { - internal static string? Marshal(MethodReference reference, string architecture) - { - if (reference.DeclaringType.FullName != "System.Runtime.InteropServices.MemoryMarshal" || - !RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime") || - reference is not GenericInstanceMethod generic || generic.GenericArguments.Count != 1 || - generic.ElementMethod.GenericParameters.Count != 1 || reference.HasThis || reference.ExplicitThis || - (generic.ElementMethod.CallingConvention & ~MethodCallingConvention.Generic) != - MethodCallingConvention.Default) - return null; - bool Parameter(TypeReference type) => type is GenericParameter parameter && - parameter.Type == GenericParameterType.Method && parameter.Position == 0 && - parameter.Owner == generic.ElementMethod; - bool ParameterReference(TypeReference type) => type is ByReferenceType byRef && Parameter(byRef.ElementType); - var read = reference.Name is "Read" or "TryRead"; - var trying = reference.Name is "TryRead" or "TryWrite"; - if (reference.Name is not ("Read" or "TryRead" or "Write" or "TryWrite") || - reference.Parameters.Count != (read && !trying ? 1 : 2)) return null; - var span = read ? "System.ReadOnlySpan`1" : "System.Span`1"; - if (reference.Parameters[0].ParameterType.FullName != span || - (reference.Parameters.Count == 2 && !ParameterReference(reference.Parameters[1].ParameterType)) || - !(trying ? reference.ReturnType.FullName == "System.Boolean" : read ? Parameter(reference.ReturnType) : - reference.ReturnType.MetadataType == MetadataType.Void)) return null; - RuntimeModels.ValidateSignature(reference, architecture); - var type = generic.GenericArguments[0]; - var (kind, _) = Describe(type, reference.Module, architecture); - return $".memory (.marshal{(read ? "Read" : "Write")} ({kind}) {trying.ToString().ToLowerInvariant()})"; - } - - static (string Kind, int Alignment) Describe(TypeReference type, ModuleDefinition module, string architecture) + internal static void ValidateElement(TypeReference type, ModuleDefinition module, + string architecture) { if (MetadataValidation.IsAggregate(type, module)) { - var layout = MetadataValidation.AggregateLayout(type, module, architecture); - return ($".snapshot (.aggregate {layout.Size})", layout.Alignment); + MetadataValidation.AggregateLayout(type, module, architecture); + return; } MetadataValidation.Type(type, module, architecture: architecture); - if (MetadataValidation.IntegerVectorWidth(type) is { } vectorWidth) - return ($".snapshot .vector{vectorWidth * 8}", vectorWidth); + if (MetadataValidation.IntegerVectorWidth(type) is not null) return; if (type is TypeSpecification) throw new InvalidDataException($"Unsupported transferred value type: {type.FullName}"); - var storage = EnumTypes.Storage(type); - var width = MetadataValidation.PrimitiveWidth(storage); - var signed = storage.FullName is "System.SByte" or "System.Int16"; - return ($".scalar {width} {signed.ToString().ToLowerInvariant()}", width); + MetadataValidation.PrimitiveWidth(EnumTypes.Storage(type)); } - internal static string? Unsafe(MethodReference reference, string architecture) + // The analyses use this transfer for the kept call of the validated generic Unsafe owner. + internal static Operation? CallModel(GenericInstanceMethod reference, string architecture, out TypeReference? storage) { - if (reference.DeclaringType.FullName != "System.Runtime.CompilerServices.Unsafe" || - !RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime") || - reference is not GenericInstanceMethod generic || generic.GenericArguments.Count != 1 || - generic.ElementMethod.GenericParameters.Count != 1 || reference.HasThis || reference.ExplicitThis || - (generic.ElementMethod.CallingConvention & ~MethodCallingConvention.Generic) != - MethodCallingConvention.Default) - return null; + storage = null; bool Parameter(TypeReference type) => type is GenericParameter parameter && parameter.Type == GenericParameterType.Method && parameter.Position == 0 && - parameter.Owner == generic.ElementMethod; - bool Address(TypeReference type) => type is ByReferenceType byRef && - byRef.ElementType.FullName == "System.Byte" || + parameter.Owner == reference.ElementMethod; + bool Address(TypeReference type) => type is ByReferenceType { ElementType.MetadataType: MetadataType.Byte } || type is PointerType { ElementType.MetadataType: MetadataType.Void }; var aligned = reference.Name is "Read" or "Write"; if (aligned && reference.Parameters.FirstOrDefault()?.ParameterType is not @@ -76,26 +39,23 @@ bool Address(TypeReference type) => type is ByReferenceType byRef && reference.ReturnType.MetadataType == MetadataType.Void; if (!read && !write) return null; RuntimeModels.ValidateSignature(reference, architecture); - var type = generic.GenericArguments[0]; + var type = reference.GenericArguments[0]; if (aligned) { - var (kind, alignment) = Describe(type, reference.Module, architecture); - return $".memory (.pointerTransfer ({kind}) {alignment} {write.ToString().ToLowerInvariant()})"; + ValidateElement(type, reference.Module, architecture); + return Unoutlined.Site; } MetadataValidation.Type(type, reference.Module, architecture: architecture); if (MetadataValidation.IsAggregate(type, reference.Module)) { - var width = MetadataValidation.Layout(type, reference.Module, architecture); - return read ? $".loadAggregate {width}" : $".storeAggregate {width}"; + MetadataValidation.Layout(type, reference.Module, architecture); + return Unoutlined.Site; } - if (MetadataValidation.IntegerVectorWidth(type) is { } vectorWidth) - return $".memory .{(read ? "load" : "store")}{vectorWidth * 8}"; + if (MetadataValidation.IntegerVectorWidth(type) is not null) return Unoutlined.Site; if (type is TypeSpecification) throw new InvalidDataException($"Unsupported unaligned value type: {type.FullName}"); - var storage = EnumTypes.Storage(type); - var scalarWidth = MetadataValidation.PrimitiveWidth(storage); - var signed = storage.FullName is "System.SByte" or "System.Int16"; - return read ? $".loadScalar {scalarWidth} {signed.ToString().ToLowerInvariant()}" : - $".storeScalar {scalarWidth}"; + storage = EnumTypes.Storage(type); + MetadataValidation.PrimitiveWidth(storage); + return Unoutlined.Site; } } diff --git a/Extractor/MetadataFieldPrefix.cs b/Extractor/MetadataFieldPrefix.cs new file mode 100644 index 000000000..aceebb36f --- /dev/null +++ b/Extractor/MetadataFieldPrefix.cs @@ -0,0 +1,43 @@ +namespace Nethermind.LeanCIL; + +/// A field enumeration whose carried prefix grows once, in metadata order. +internal sealed class MetadataFieldPrefix(IEnumerable fields) +{ + readonly IEnumerator remaining = fields.GetEnumerator(); + readonly HashSet reached = []; + readonly List carried = []; + bool complete; + + internal IReadOnlyList Carried => carried; + + bool Advance(Action carry) + { + if (complete) return false; + if (!remaining.MoveNext()) + { + complete = true; + remaining.Dispose(); + return false; + } + var token = remaining.Current; + reached.Add(token); + carried.Add(token); + carry(token); + return true; + } + + /// Carry exactly the fields through the requested token, retaining any longer prefix. + internal void Through(uint token, Action carry) + { + if (reached.Contains(token)) return; + while (Advance(carry)) + if (carried[^1] == token) return; + throw new InvalidDataException($"Field 0x{token:X8} is absent from its declaring type."); + } + + /// Carry all remaining fields without visiting the existing prefix again. + internal void All(Action carry) + { + while (Advance(carry)) { } + } +} diff --git a/Extractor/MetadataImage.cs b/Extractor/MetadataImage.cs new file mode 100644 index 000000000..b60995dcd --- /dev/null +++ b/Extractor/MetadataImage.cs @@ -0,0 +1,361 @@ +using System.Buffers.Binary; +using System.Collections.Immutable; +using System.Globalization; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; + +namespace Nethermind.LeanCIL; + +/// +/// The metadata tables and heaps of one module image, read with System.Reflection.Metadata: the raw bytes of +/// a table row or heap entry at the position the image gives it. +/// +internal sealed class MetadataImage +{ + readonly ImmutableArray bytes; + readonly byte[] tableHeader; + string? decodedHeader; + + internal MetadataImage(PEReader image) + { + Reader = image.GetMetadataReader(); + bytes = image.GetMetadata().GetContent(); + // Read before any row, so an image whose table stream is refused yields no rows either. + tableHeader = ReadTableHeader(bytes.AsSpan()); + } + + internal MetadataReader Reader { get; } + + const int NamePageSize = 256; + MetadataName?[]?[]? names; + + /// The exact bytes at a heap offset, materialized only for identity output. + internal MetadataName Name(int offset) + { + int size = Reader.GetHeapSize(HeapIndex.String); + if ((uint)offset >= (uint)size) throw new BadImageFormatException("String offset outside its heap"); + names ??= new MetadataName?[(size - 1) / NamePageSize + 1][]; + var page = names[offset / NamePageSize] ??= new MetadataName?[NamePageSize]; + if (page[offset % NamePageSize] is { } name) return name; + byte[] entry = StringEntry(offset); + return page[offset % NamePageSize] = MetadataName.FromBytes(entry.AsSpan(0, entry.Length - 1)); + } + + /// One bounded byte of the raw string heap, including terminators. + internal byte StringByte(int offset) + { + int size = Reader.GetHeapSize(HeapIndex.String); + if ((uint)offset >= (uint)size) throw new BadImageFormatException("String offset outside its heap"); + return bytes[Reader.GetHeapMetadataOffset(HeapIndex.String) + offset]; + } + + /// A raw named row's own columns and its immediate nesting or assembly edge. + internal sealed record NamedRow(int Namespace, int Name, uint Parent, uint Assembly); + + readonly SortedDictionary namedRows = []; + + /// The immutable raw row; nesting edges are retained without flattening their ancestors. + internal NamedRow NamedType(uint token) + { + if (namedRows.TryGetValue(token, out var cached)) return cached; + uint table = token >> 24; + int number = (int)(token & 0xffffff); + if (table is not (1 or 2) || number == 0 || number > Count((int)table)) + throw new InvalidDataException("Type identity requires a valid TypeDef or TypeRef row"); + StringHandle space; + StringHandle name; + uint parent = 0; + uint assembly = 0; + if (table == 2) + { + var row = Reader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle(number)); + space = row.Namespace; + name = row.Name; + if (NestedClassRow(number) is { } nestingToken) + { + byte[] nestingRow = Row(nestingToken); + int width = nestingRow.Length / 2; + uint enclosing = 0; + for (int index = nestingRow.Length - 1; index >= width; index--) + enclosing = enclosing * 256 + nestingRow[index]; + if (enclosing == 0 || enclosing > Count(2)) + throw new InvalidDataException("Nested type has an invalid enclosing row"); + parent = 0x02000000 | enclosing; + } + else assembly = 0x20000001; + } + else + { + var row = Reader.GetTypeReference(MetadataTokens.TypeReferenceHandle(number)); + space = row.Namespace; + name = row.Name; + uint scope = (uint)MetadataTokens.GetToken(row.ResolutionScope); + if (row.ResolutionScope.IsNil) throw new InvalidDataException("Type reference has no scope"); + if (scope >> 24 == 1) parent = scope; + else if (scope >> 24 == 0x23 && (scope & 0xffffff) <= Count(0x23)) assembly = scope; + else if (scope == 1) assembly = 0x20000001; + else throw new InvalidDataException("Type reference has an unsupported resolution scope"); + } + cached = new(MetadataTokens.GetHeapOffset(space), MetadataTokens.GetHeapOffset(name), parent, assembly); + namedRows.Add(token, cached); + return cached; + } + + readonly Dictionary assemblyIdentities = []; + readonly SortedDictionary assemblyColumns = []; + readonly SortedDictionary<(int Offset, bool FullKey), AssemblyName.KeyColumns> signingKeys = []; + + internal long SigningBytes { get; private set; } + internal long SigningReads { get; private set; } + + internal void RecordSigningBytes(int count) { SigningBytes += count; SigningReads++; } + + /// The normalized signing column for a raw blob and its full-key or token interpretation. + internal AssemblyName.KeyColumns SigningKey(BlobHandle handle, bool fullKey) + { + var key = (MetadataTokens.GetHeapOffset(handle), fullKey); + if (signingKeys.TryGetValue(key, out var columns)) return columns; + columns = AssemblyName.ReadKey(this, handle, fullKey); + signingKeys.Add(key, columns); + return columns; + } + + internal AssemblyName.Columns? AssemblyColumns(uint token) + { + if (assemblyColumns.TryGetValue(token, out var columns)) return columns; + assemblyColumns.Add(token, columns = AssemblyName.ReadColumns(this, token)); + return columns; + } + + /// The immutable identity of an assembly row in this image, including invalid null results. + internal AssemblyName? AssemblyIdentity(uint token) + { + if (assemblyIdentities.TryGetValue(token, out var identity)) return identity; + identity = AssemblyName.Read(this, token); + assemblyIdentities.Add(token, identity); + return identity; + } + + /// A top-level type's raw name columns and resolution scope. + /// Definitions have no reference scope. + internal sealed record TypeNameRow(TypeLevel Level, uint ResolutionScope); + + readonly Dictionary typeNames = []; + + /// The cached raw name of a top-level TypeDef or TypeRef; nested types have no top-level name. + internal TypeNameRow? TopLevelTypeName(uint token) + { + if (typeNames.TryGetValue(token, out var cached)) return cached; + uint table = token >> 24; + int rowNumber = (int)(token & 0xffffff); + if (table is not (1 or 2) || rowNumber == 0 || rowNumber > Count((int)table)) + throw new InvalidDataException("Modifier identity requires a valid TypeDef or TypeRef row"); + uint scope = 0; + bool nested; + if (table == 1) + { + var row = Reader.GetTypeReference(MetadataTokens.TypeReferenceHandle(rowNumber)); + scope = (uint)MetadataTokens.GetToken(row.ResolutionScope); + nested = row.ResolutionScope.Kind == HandleKind.TypeReference; + } + else nested = NestedClassRow(rowNumber) is not null; + var name = nested ? null : new TypeNameRow( + IdentityRows.Level(this, new Mono.Cecil.MetadataToken(token)), scope); + typeNames.Add(token, name); + return name; + } + + internal int Count(int table) => Reader.GetTableRowCount((TableIndex)table); + + Dictionary? nesting; + + /// The NestedClass row of a nested type, from a map of the table built on first use. + internal uint? NestedClassRow(int typeRow) + { + if (nesting is null) + { + var entries = new Dictionary(); + var width = Reader.GetTableRowSize(TableIndex.NestedClass) / 2; + for (var row = 1; row <= Count(0x29); row++) + { + var bytes = Row(0x29000000 + (uint)row); + var nested = 0; + for (var index = width - 1; index >= 0; index--) nested = nested * 256 + bytes[index]; + if (!entries.TryAdd(nested, 0x29000000 + (uint)row)) + throw new InvalidDataException("Type has duplicate NestedClass rows"); + } + nesting = entries; + } + return nesting.TryGetValue(typeRow, out var found) ? found : null; + } + + /// The bytes of the row a token names. + internal byte[] Row(uint token) + { + var table = (TableIndex)(token >> 24); + var size = Reader.GetTableRowSize(table); + var at = Reader.GetTableMetadataOffset(table) + ((int)(token & 0xFFFFFF) - 1) * size; + var row = bytes.AsSpan(at, size).ToArray(); + // A MethodDef row's first column is the body's address, which moves whenever an earlier body changes + // size; the carried row has it zeroed so that no method's evidence depends on another method's body. + if (table == TableIndex.MethodDef) row.AsSpan(0, sizeof(uint)).Clear(); + return row; + } + + /// The first Field row of a TypeDef row: its FieldList column. + internal uint FieldList(byte[] typeDefRow) + { + var width = Reader.GetTableRowCount(TableIndex.Field) < 0x10000 ? 2 : 4; + var methodWidth = Reader.GetTableRowCount(TableIndex.MethodDef) < 0x10000 ? 2 : 4; + var at = typeDefRow.Length - width - methodWidth; + return width == 2 ? BitConverter.ToUInt16(typeDefRow, at) : BitConverter.ToUInt32(typeDefRow, at); + } + + /// A #Strings entry through its terminating zero. + internal byte[] StringEntry(int offset) + { + var size = Reader.GetHeapSize(HeapIndex.String); + if ((uint)offset >= (uint)size) throw new BadImageFormatException("String offset outside its heap"); + var start = Reader.GetHeapMetadataOffset(HeapIndex.String) + offset; + var length = bytes.AsSpan(start, size - offset).IndexOf((byte)0); + if (length < 0) throw new BadImageFormatException("String entry without a terminator in its heap"); + return bytes.AsSpan(start, length + 1).ToArray(); + } + + /// A #Blob or #US entry with its compressed length. + internal byte[] SizedEntry(HeapIndex heap, int offset) + { + if (heap is not (HeapIndex.Blob or HeapIndex.UserString)) + throw new BadImageFormatException("Sized entry requires a blob or user-string heap"); + var size = Reader.GetHeapSize(heap); + if ((uint)offset >= (uint)size) throw new BadImageFormatException("Sized offset outside its heap"); + var start = Reader.GetHeapMetadataOffset(heap) + offset; + var first = bytes[start]; + var header = first switch + { + < 0x80 => 1, + < 0xC0 => 2, + < 0xE0 => 4, + _ => throw new BadImageFormatException("Invalid compressed length prefix") + }; + var remaining = size - offset; + if (header > remaining) throw new BadImageFormatException("Compressed length crosses its heap boundary"); + var length = header switch + { + 1 => first, + 2 => ((first & 0x3F) << 8) | bytes[start + 1], + _ => ((first & 0x1F) << 24) | (bytes[start + 1] << 16) | (bytes[start + 2] << 8) | bytes[start + 3] + }; + if (length > remaining - header) throw new BadImageFormatException("Sized entry crosses its heap boundary"); + return bytes.AsSpan(start, header + length).ToArray(); + } + + /// + /// The decoded table stream header as a Lean TableHeader: the heap sizes and each present table's row + /// count, in table order. Lean proves once per assembly that decodes to it. + /// + internal string DecodedHeader() + { + if (decodedHeader is not null) return decodedHeader; + ulong valid = BinaryPrimitives.ReadUInt64LittleEndian(tableHeader.AsSpan(ValidOffset)); + var counts = new List(); + int countOffset = RowCountsOffset; + for (int table = 0; table < 64; table++) + { + if ((valid & (1UL << table)) == 0) continue; + uint count = BinaryPrimitives.ReadUInt32LittleEndian(tableHeader.AsSpan(countOffset)); + counts.Add(string.Create(CultureInfo.InvariantCulture, $"({table}, {count})")); + countOffset += sizeof(uint); + } + return decodedHeader = string.Create(CultureInfo.InvariantCulture, + $".ofCounts {tableHeader[HeapSizesOffset]} [{string.Join(", ", counts)}]"); + } + + // Offsets in the table stream header of the `HeapSizes` byte, the `Valid` mask and the first row count + // (II.24.2.6). + const int HeapSizesOffset = 6; + const int ValidOffset = 8; + const int RowCountsOffset = 24; + + // The metadata root's signature, its fixed fields before the version string, and the version field's + // longest padded length (II.24.2.1). + const uint MetadataSignature = 0x424A5342; + const int RootFixedSize = 16; + const uint MaxVersionLength = 256; + + // A stream name's longest length including its terminator (II.24.2.2). + const int MaxStreamNameBytes = 32; + + /// The table stream header: the bytes from the reserved word through the last row count. + internal byte[] TableHeader() => [.. tableHeader]; + + /// The heap index width recorded in the table stream, including explicitly wide small heaps. + internal int HeapIndexWidth(HeapIndex heap) + { + var mask = heap switch + { + HeapIndex.String => 1, + HeapIndex.Blob => 4, + _ => throw new ArgumentOutOfRangeException(nameof(heap)) + }; + return (tableHeader[HeapSizesOffset] & mask) == 0 ? 2 : 4; + } + + /// + /// The header of the #~ stream that the metadata root's stream directory names, from its reserved word + /// through the row count of the last table its Valid mask marks present, including tables with no rows. + /// A root that names an uncompressed #- stream is refused. + /// + internal static byte[] ReadTableHeader(ReadOnlySpan metadata) + { + Require(metadata, 0, RootFixedSize, "Metadata root is shorter than its fixed fields"); + if (BinaryPrimitives.ReadUInt32LittleEndian(metadata) != MetadataSignature) + throw new BadImageFormatException("Invalid metadata root signature"); + uint versionLength = BinaryPrimitives.ReadUInt32LittleEndian(metadata[12..]); + // System.Reflection.Metadata aligns each stream name to the root, so an unpadded version length would + // make this walk and the reader's disagree about where the stream headers are. + if (versionLength > MaxVersionLength || versionLength % 4 != 0) + throw new BadImageFormatException("Metadata version length is not a multiple of 4 of at most 256"); + int directory = RootFixedSize + (int)versionLength; + Require(metadata, directory, 4, "Metadata stream directory crosses the metadata root"); + int streamCount = BinaryPrimitives.ReadUInt16LittleEndian(metadata[(directory + 2)..]); + directory += 4; + byte[]? found = null; + for (int stream = 0; stream < streamCount; stream++) + { + Require(metadata, directory, 8, "Metadata stream header crosses the metadata root"); + uint offset = BinaryPrimitives.ReadUInt32LittleEndian(metadata[directory..]); + uint size = BinaryPrimitives.ReadUInt32LittleEndian(metadata[(directory + 4)..]); + directory += 8; + var nameField = metadata[directory..]; + int nameLength = nameField[..Math.Min(MaxStreamNameBytes, nameField.Length)].IndexOf((byte)0); + if (nameLength < 0) + throw new BadImageFormatException("Metadata stream name has no terminator within 32 bytes"); + var name = nameField[..nameLength]; + int paddedLength = (nameLength + 4) & ~3; + Require(metadata, directory, paddedLength, "Metadata stream name padding crosses the metadata root"); + directory += paddedLength; + if (name.SequenceEqual("#-"u8)) + throw new NotSupportedException("The uncompressed metadata table stream #- is not supported"); + if (!name.SequenceEqual("#~"u8)) continue; + if (found is not null) throw new BadImageFormatException("Metadata root names two #~ streams"); + if ((ulong)offset + size > (ulong)metadata.Length) + throw new BadImageFormatException("Metadata table stream crosses the metadata root"); + var tableStream = metadata.Slice((int)offset, (int)size); + Require(tableStream, 0, RowCountsOffset, "Metadata table stream is shorter than its fixed header"); + if (tableStream[4] != 2 || tableStream[5] != 0 || tableStream[HeapSizesOffset] >= 8) + throw new BadImageFormatException("Unsupported metadata table stream header"); + ulong valid = BinaryPrimitives.ReadUInt64LittleEndian(tableStream[ValidOffset..]); + int length = RowCountsOffset + sizeof(uint) * System.Numerics.BitOperations.PopCount(valid); + Require(tableStream, 0, length, "Metadata table stream ends inside its row counts"); + found = tableStream[..length].ToArray(); + } + return found ?? throw new BadImageFormatException("Metadata root names no #~ stream"); + } + + static void Require(ReadOnlySpan source, int offset, int length, string failure) + { + if (length > source.Length - offset) throw new BadImageFormatException(failure); + } +} diff --git a/Extractor/MetadataSlices.cs b/Extractor/MetadataSlices.cs new file mode 100644 index 000000000..f82161142 --- /dev/null +++ b/Extractor/MetadataSlices.cs @@ -0,0 +1,942 @@ +using System.Collections.Immutable; +using System.Globalization; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using Mono.Cecil; +using MemberReference = Mono.Cecil.MemberReference; +using TypeReference = Mono.Cecil.TypeReference; +using MethodDefinition = Mono.Cecil.MethodDefinition; +using ModuleDefinition = Mono.Cecil.ModuleDefinition; +using SrmTypeDefinition = System.Reflection.Metadata.TypeDefinition; + +namespace Nethermind.LeanCIL; + +/// +/// The table rows and heap entries a method's tokens, its own definition and its locals reach, with one small +/// header block per pinned assembly. Lean decodes the rows and checks every token operand, local type and the +/// method's identity against them; that the copied bytes are the image's bytes stays the extractor's claim. +/// +internal sealed class MetadataSlices(PinnedAssemblies pinned) +{ + HashSet<(ModuleDefinition Module, uint Token)>? typeReferences; + + /// The carried type references checked against definitions, with their source modules. + internal IEnumerable<(ModuleDefinition Module, TypeReferenceHandle Handle)> TypeReferences => + typeReferences?.Select(reference => + (reference.Module, MetadataTokens.TypeReferenceHandle((int)(reference.Token & 0x00FFFFFF)))) ?? []; + + /// The identity block of a pinned module: its table stream header and Assembly row. + internal string AssemblyBlock(ModuleDefinition module) + { + var image = pinned.Metadata(module); + var reader = image.Reader; + var assembly = reader.GetAssemblyDefinition(); + var strings = new SortedDictionary(); + var blobs = new SortedDictionary(); + foreach (var handle in new[] { assembly.Name, assembly.Culture }) + strings[reader.GetHeapOffset(handle)] = image.StringEntry(reader.GetHeapOffset(handle)); + blobs[reader.GetHeapOffset(assembly.PublicKey)] = + image.SizedEntry(HeapIndex.Blob, reader.GetHeapOffset(assembly.PublicKey)); + var identity = pinned.Original(module).Assembly.Name.FullName; + // Numbered by rank in bytes, as the slices' heaps are, so the block does not move with the image's offsets. + var row = image.Row(0x20000001); + var textWidth = image.HeapIndexWidth(HeapIndex.String); + var blobWidth = image.HeapIndexWidth(HeapIndex.Blob); + var ranked = new SortedDictionary[] { [], [] }; + var numbers = new Dictionary[] { new() { [0] = 0 }, new() { [0] = 0 } }; + foreach (var (heap, source) in new[] { (0, strings), (1, blobs) }) + { + var rank = 1; + foreach (var entry in source.Where(entry => entry.Key != 0) + .OrderBy(entry => Convert.ToHexString(entry.Value), StringComparer.Ordinal)) + { + RequireHeapRank((uint)rank, heap == 0 ? textWidth : blobWidth, heap == 0 ? "#Strings" : "#Blob"); + numbers[heap][entry.Key] = rank; + ranked[heap][rank++] = entry.Value; + } + if (source.TryGetValue(0, out var empty)) ranked[heap][0] = empty; + } + var at = 4 + 8 + 4; + foreach (var (heap, width) in new[] { (1, blobWidth), (0, textWidth), (0, textWidth) }) + { + var offset = width == 2 ? BitConverter.ToUInt16(row, at) : (int)BitConverter.ToUInt32(row, at); + Array.Copy(BitConverter.GetBytes(numbers[heap][offset]), 0, row, at, width); + at += width; + } + // The token is the SHA-1 of the public key, which Lean does not compute: it is stated, and checked only + // to be sixteen hexadecimal digits when the row has a key. + const string TokenMarker = "PublicKeyToken="; + var token = identity[(identity.LastIndexOf(TokenMarker, StringComparison.Ordinal) + TokenMarker.Length)..]; + var hasKey = !assembly.PublicKey.IsNil && reader.GetBlobBytes(assembly.PublicKey).Length > 0; + var keyToken = !hasKey ? "" + : $"keyToken := {ByteLiterals.Of(System.Text.Encoding.ASCII.GetBytes(token))}, "; + var headers = $"tableHeader := {ByteLiterals.Of(image.TableHeader())}, header := {image.DecodedHeader()}"; + var tables = $"assemblyRow := {ByteLiterals.Of(row)}, strings := {Entries(ranked[0])}, blobs := {Entries(ranked[1])}"; + return $$"""{ identity := {{IdentityText.Quote(identity)}}, {{keyToken}}{{headers}}, {{tables}} }"""; + } + + /// + /// The slices of one method's body, as a Lean MethodMetadata value, and the lookup tables of those slices. + /// They hold what this method reaches and nothing else, with heap entries numbered by rank among the entries + /// carried, so the method's evidence is a function of the method and its assemblies alone. + /// + internal (string Metadata, string Tables, int[] Assemblies) Method(MethodDefinition original, + IEnumerable tokens, bool classOwner, IEnumerable arguments) + { + var collection = new Collection(pinned, typeReferences ??= []); + var own = collection.SliceOf(original.Module); + own.Method(original, classOwner); + foreach (var token in tokens) own.Operand(token); + foreach (var argument in arguments) + { + var target = collection.SliceOf(argument.Module); + var token = argument.MetadataToken.ToUInt32(); + target.Token(token); + target.Arity(token); + } + var metadata = collection.Lean(); + return (metadata, collection.Tables(), collection.Assemblies()); + } + + /// The reserved offset for a heap entry omitted from the carried evidence. + internal static uint OmittedHeapOffset(int width) => width switch + { + 2 => ushort.MaxValue, + 4 => uint.MaxValue, + _ => throw new ArgumentOutOfRangeException(nameof(width)) + }; + + /// Every carried rank must fit below the reserved omitted-entry offset. + internal static void RequireHeapRank(uint rank, int width, string heap) + { + if (rank >= OmittedHeapOffset(width)) + throw new InvalidDataException( + $"The carried {heap} rank {rank} reaches its reserved omitted-entry offset."); + } + + internal static string Entries(SortedDictionary entries) where TKey : notnull => + "[" + string.Join(", ", entries.Select(entry => + $"({Convert.ToString(entry.Key, CultureInfo.InvariantCulture)}, {ByteLiterals.Of(entry.Value)})")) + "]"; + + /// The slices of the pinned modules one method reaches, its own first. + sealed class Collection(PinnedAssemblies pinned, HashSet<(ModuleDefinition Module, uint Token)> typeReferences) + { + readonly List slices = []; + readonly Dictionary byModule = []; + + internal PinnedAssemblies Pinned => pinned; + + /// Records that a slice of carries the TypeRef row + /// . + internal void CarriedTypeReference(ModuleDefinition module, uint token) => typeReferences.Add((module, token)); + + internal Slice SliceOf(ModuleDefinition module) + { + var original = pinned.Original(module); + if (byModule.TryGetValue(original, out var existing)) return existing; + var created = new Slice(this, original, slices.Count); + slices.Add(created); + byModule.Add(original, created); + return created; + } + + internal Slice At(int position) => slices[position]; + + internal string Lean() + { + // Closing a slice can reach another module, so the list is read by index while it grows. + for (var position = 0; position < slices.Count; position++) slices[position].Finish(); + foreach (var slice in slices) slice.Canonicalize(); + var others = string.Join(", ", slices.Skip(1).Select(slice => slice.Lean())); + return $"{{ own := {slices[0].Lean()}, others := [{others}] }}"; + } + + /// The pinned assembly index of each slice, in slice order. + internal int[] Assemblies() => [.. slices.Select(slice => slice.AssemblyIndex())]; + + /// + /// The lookup tables of each slice, in slice order; read after has closed them. + /// + internal string Tables() => "[" + string.Join(", ", slices.Select(slice => slice.Tables())) + "]"; + } + + /// The rows, heap entries and links of one module the method reaches. + sealed class Slice(Collection collection, ModuleDefinition module, int position) + { + readonly MetadataImage image = collection.Pinned.Metadata(module); + readonly SortedDictionary rows = []; + readonly SortedDictionary strings = []; + readonly SortedDictionary blobs = []; + readonly SortedDictionary userStrings = []; + readonly List<(int Kind, uint Source, int Slice, uint Target)> links = []; + readonly Dictionary<(int Kind, uint Source), (int Slice, uint Target)> linked = []; + readonly HashSet visited = []; + readonly HashSet namedDefinitions = []; + readonly HashSet baseReferences = []; + readonly HashSet modifierVisited = []; + readonly HashSet fieldsAdded = []; + readonly Dictionary fieldPrefixes = []; + readonly HashSet fieldSignaturesAdded = []; + readonly HashSet chainedOwners = []; + readonly HashSet chained = []; + readonly HashSet arities = []; + int corlib; + + internal ModuleDefinition Module => module; + + MetadataReader Reader => image.Reader; + + static uint TokenOf(EntityHandle handle) => (uint)MetadataTokens.GetToken(handle); + + static int RowOf(uint token) => (int)(token & 0xFFFFFF); + + void Row(uint token) + { + if (!rows.ContainsKey(token)) rows[token] = image.Row(token); + } + + void Text(StringHandle handle) + { + var offset = Reader.GetHeapOffset(handle); + if (!strings.ContainsKey(offset)) strings[offset] = image.StringEntry(offset); + } + + void Blob(BlobHandle handle) + { + var offset = Reader.GetHeapOffset(handle); + if (!blobs.ContainsKey(offset)) blobs[offset] = image.SizedEntry(HeapIndex.Blob, offset); + } + + void UserString(int offset) + { + if (!userStrings.ContainsKey(offset)) userStrings[offset] = image.SizedEntry(HeapIndex.UserString, offset); + } + + void Link(int kind, uint source, int slice, uint target) + { + if (!linked.TryAdd((kind, source), (slice, target))) return; + links.Add((kind, source, slice, target)); + } + + /// + /// The tokens a signature blob names, in the signature's order. A form the Lean checker has no place for + /// stops the extraction with its name: a general array, a typed reference, a pinned local, a function + /// pointer in a member reference and a custom modifier other than the two Roslyn writes on `in` parameters + /// and `init` accessors. + /// + sealed class TokenCollector(MetadataReader reader, bool member) : ISignatureTypeProvider + { + internal readonly List Tokens = []; + readonly Dictionary uses = []; + readonly Dictionary modifierUses = []; + + /// + /// Whether the signature names the type only as a custom modifier, which the checker reads by scope + /// and name alone, so the type needs no link to a definition. + /// + internal bool OnlyModifier(uint token) => + modifierUses.TryGetValue(token, out var asModifier) && asModifier == uses[token]; + + int Named(EntityHandle handle) + { + var token = TokenOf(handle); + Tokens.Add(token); + uses[token] = uses.GetValueOrDefault(token) + 1; + return (int)token; + } + + static InvalidDataException Unsupported(string form) => + new($"Unsupported metadata signature: {form}. " + + "The metadata binding check reads neither this form nor its identity."); + + public int GetPrimitiveType(PrimitiveTypeCode typeCode) => + typeCode == PrimitiveTypeCode.TypedReference ? throw Unsupported("typed reference") : 0; + public int GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, + byte rawTypeKind) => Named(handle); + public int GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, + byte rawTypeKind) => Named(handle); + public int GetTypeFromSpecification(MetadataReader reader, object? context, + TypeSpecificationHandle handle, byte rawTypeKind) => Named(handle); + public int GetSZArrayType(int elementType) => 0; + public int GetArrayType(int elementType, ArrayShape shape) => throw Unsupported("general array"); + public int GetByReferenceType(int elementType) => 0; + public int GetPointerType(int elementType) => 0; + public int GetGenericInstantiation(int genericType, ImmutableArray typeArguments) => 0; + public int GetGenericMethodParameter(object? context, int index) => 0; + public int GetGenericTypeParameter(object? context, int index) => 0; + public int GetFunctionPointerType(MethodSignature signature) => + member ? throw Unsupported("function pointer in a member reference") : 0; + + public int GetModifiedType(int modifier, int unmodifiedType, bool isRequired) + { + var handle = MetadataTokens.EntityHandle(modifier); + modifierUses[(uint)modifier] = modifierUses.GetValueOrDefault((uint)modifier) + 1; + var name = handle.Kind switch + { + HandleKind.TypeReference => + reader.GetString(reader.GetTypeReference((TypeReferenceHandle)handle).Name), + HandleKind.TypeDefinition => + reader.GetString(reader.GetTypeDefinition((TypeDefinitionHandle)handle).Name), + _ => "" + }; + return isRequired && name is "InAttribute" or "IsExternalInit" ? 0 + : throw Unsupported("custom modifier " + (name.Length == 0 ? "of a specification" : name)); + } + + public int GetPinnedType(int elementType) => throw Unsupported("pinned local"); + } + + enum Form { Method, Field, Locals, Type, Instantiation, Standalone } + + /// A signature blob and every token its types name. + void Signature(BlobHandle handle, Form form, bool member = false) + { + Blob(handle); + var collector = new TokenCollector(Reader, form is Form.Method or Form.Field && member); + var decoder = new SignatureDecoder(collector, Reader, null); + var blob = Reader.GetBlobReader(handle); + if (form == Form.Standalone) + { + var first = Reader.GetBlobBytes(handle); + form = first.Length > 0 && first[0] == 0x07 ? Form.Locals : Form.Method; + } + switch (form) + { + case Form.Method: decoder.DecodeMethodSignature(ref blob); break; + case Form.Field: decoder.DecodeFieldSignature(ref blob); break; + case Form.Locals: decoder.DecodeLocalSignature(ref blob); break; + case Form.Type: decoder.DecodeType(ref blob); break; + default: decoder.DecodeMethodSpecificationSignature(ref blob); break; + } + if (blob.RemainingBytes != 0) + throw new InvalidDataException($"Unsupported metadata signature: {form} has trailing bytes."); + foreach (var token in collector.Tokens) + { + Token(token, collector.OnlyModifier(token)); + if (!collector.OnlyModifier(token)) Arity(token); + } + } + + /// Add a token with every row and heap entry the checker reads for it. + internal void Token(uint token, bool onlyModifier = false) + { + if (onlyModifier && token >> 24 == 0x01) + { + // Read by scope and name alone: its rows, and no link, whether or not the type resolves. + if (!visited.Contains(token) && modifierVisited.Add(token)) TypeRef(token, linked: false); + return; + } + if (!visited.Add(token)) return; + switch (token >> 24) + { + case 0x01: TypeRef(token, linked: true); break; + case 0x02: TypeDef(token); break; + case 0x04: Field(token); break; + case 0x06: MethodDef(token); break; + case 0x0A: MemberRef(token); break; + case 0x11: Standalone(token); break; + case 0x1B: TypeSpec(token); break; + case 0x23: AssemblyRef(token); break; + case 0x2B: MethodSpec(token); break; + case 0x70: UserString(RowOf(token)); break; + default: + throw new InvalidDataException( + $"Unsupported metadata table of token 0x{token:X8} in {module.Name}."); + } + } + + void AssemblyRef(uint token) + { + Row(token); + var reference = Reader.GetAssemblyReference(MetadataTokens.AssemblyReferenceHandle(RowOf(token))); + if (reference.Flags != 0) + throw new InvalidDataException("Unsupported assembly reference flags 0x" + + ((int)reference.Flags).ToString("X", CultureInfo.InvariantCulture) + " on " + + Reader.GetString(reference.Name) + + ": a full public key or a retargetable reference has no kernel-checked identity."); + Text(reference.Name); + Text(reference.Culture); + Blob(reference.PublicKeyOrToken); + } + + void BaseReference(uint token) + { + if (!visited.Contains(token) && baseReferences.Add(token)) TypeRef(token, linked: true, baseOnly: true); + } + + void TypeRef(uint token, bool linked, bool baseOnly = false) + { + Row(token); + var typeReference = (TypeReference)module.LookupToken(new MetadataToken(token)); + if (!baseOnly || IsStorageRoot(typeReference)) collection.CarriedTypeReference(module, token); + var reference = Reader.GetTypeReference(MetadataTokens.TypeReferenceHandle(RowOf(token))); + Text(reference.Name); + Text(reference.Namespace); + var scope = reference.ResolutionScope; + if (scope.Kind == HandleKind.ModuleReference) + throw new InvalidDataException("Unsupported type scope: " + Reader.GetString(reference.Name) + + " is resolved through a module reference."); + if (baseOnly && scope.Kind == HandleKind.TypeReference) BaseReference(TokenOf(scope)); + else if (!scope.IsNil && scope.Kind != HandleKind.ModuleDefinition) Token(TokenOf(scope)); + if (!linked || !IsPinned(typeReference)) return; + var definition = typeReference.Resolve(); + if (definition is null) + throw new InvalidDataException("Unresolved pinned type: " + typeReference.FullName); + var definedIn = definition.Module.Assembly.Name.FullName; + if (typeReference.Scope is AssemblyNameReference named && named.FullName != definedIn) + { + var facade = collection.Pinned.ModuleOf(typeReference); + if (facade is null || !collection.Pinned.Contains(collection.Pinned.Working(definition.Module))) + throw new InvalidDataException("Unpinned forwarded type: " + typeReference.FullName); + var source = collection.SliceOf(facade); + Link(4, token, source.PositionOf, source.Forwarder(typeReference, definedIn)); + } + var target = collection.SliceOf(definition.Module); + Link(0, token, target.PositionOf, definition.MetadataToken.ToUInt32()); + if (baseOnly) target.DefinitionName(definition.MetadataToken.ToUInt32()); + else target.Token(definition.MetadataToken.ToUInt32()); + } + + /// The single top-level ExportedType row forwarding this name to the pinned implementation. + uint Forwarder(TypeReference type, string implementation) + { + uint token = 0; + foreach (var handle in Reader.ExportedTypes) + { + var row = Reader.GetExportedType(handle); + if (!Reader.StringComparer.Equals(row.Name, type.Name) || + !Reader.StringComparer.Equals(row.Namespace, type.Namespace)) continue; + if (token != 0 || type.DeclaringType is not null || !row.IsForwarder || + row.Implementation.Kind != HandleKind.AssemblyReference) + throw new InvalidDataException("Ambiguous or unsupported forwarded type: " + type.FullName); + var destination = TokenOf(row.Implementation); + var reference = module.AssemblyReferences.SingleOrDefault(assembly => + assembly.MetadataToken.ToUInt32() == destination); + if (reference?.FullName != implementation) + throw new InvalidDataException("Forwarder destination mismatch: " + type.FullName); + token = TokenOf(handle); + Row(token); + Text(row.Name); + Text(row.Namespace); + Token(destination); + } + return token != 0 ? token : throw new InvalidDataException("Missing type forwarder: " + type.FullName); + } + + /// + /// Whether a type is declared by a pinned module: a module-scoped type of the untouched copy counts. + /// + bool IsPinned(TypeReference type) => type.Scope is ModuleDefinition scope + ? collection.Pinned.Contains(collection.Pinned.Working(scope)) + : collection.Pinned.ModuleOf(type) is not null; + + static bool CoreScope(IMetadataScope scope) => + (scope is ModuleDefinition image ? image.Assembly.Name.Name : scope.Name) + is "System.Runtime" or "mscorlib" or "System.Private.CoreLib" or "netstandard"; + + static bool IsStorageRoot(TypeReference type) => + type.DeclaringType is null && type.Namespace == "System" && type.Name is "ValueType" or "Enum" && + CoreScope(type.Scope); + + bool Extends(SrmTypeDefinition definition, string name) + { + if (definition.BaseType.IsNil || definition.BaseType.Kind == HandleKind.TypeSpecification) return false; + var parent = (TypeReference)module.LookupToken(new MetadataToken(TokenOf(definition.BaseType))); + return parent.Name == name && IsStorageRoot(parent); + } + + bool IsClass(SrmTypeDefinition definition) => + (definition.Attributes & System.Reflection.TypeAttributes.Interface) == 0 && + !definition.BaseType.IsNil && !Extends(definition, "ValueType") && !Extends(definition, "Enum"); + + void TypeDef(uint token) + { + DefinitionName(token); + var definition = Reader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle(RowOf(token))); + if (RowOf(token) < image.Count(0x02)) Row(token + 1); + if (definition.BaseType.IsNil || (definition.Attributes & System.Reflection.TypeAttributes.Interface) != 0) + return; + if (definition.BaseType.Kind == HandleKind.TypeReference) + BaseReference(TokenOf(definition.BaseType)); + else if (definition.BaseType.Kind == HandleKind.TypeDefinition && CoreScope(module)) + DefinitionName(TokenOf(definition.BaseType)); + if (Extends(definition, "Enum")) FieldRows(token, definition, signatures: true); + } + + /// The declaring name and nesting rows, independently of the definition's base. + void DefinitionName(uint token) + { + if (!namedDefinitions.Add(token)) return; + Row(token); + var definition = Reader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle(RowOf(token))); + Text(definition.Name); + Text(definition.Namespace); + var enclosing = definition.GetDeclaringType(); + if (enclosing.IsNil) return; + var nesting = NestedClassRow(token); + Row(nesting); + Link(2, token, position, nesting); + DefinitionName(TokenOf(enclosing)); + } + + /// The NestedClass row that names a nested type. + uint NestedClassRow(uint token) => image.NestedClassRow(RowOf(token)) ?? + throw new InvalidDataException($"No NestedClass row for type 0x{token:X8} in {module.Name}."); + + /// The shared field enumeration of one declaring type. + MetadataFieldPrefix Fields(uint owner, SrmTypeDefinition definition) + { + if (!fieldPrefixes.TryGetValue(owner, out var prefix)) + fieldPrefixes.Add(owner, prefix = new(definition.GetFields().Select(handle => TokenOf(handle)))); + return prefix; + } + + /// Every field row of a type, and for an enum the signature of its instance field. + void FieldRows(uint owner, SrmTypeDefinition definition, bool signatures) + { + var prefix = Fields(owner, definition); + prefix.All(Row); + if (!signatures || !fieldSignaturesAdded.Add(owner)) return; + foreach (var token in prefix.Carried) + Blob(Reader.GetFieldDefinition(MetadataTokens.FieldDefinitionHandle(RowOf(token))).Signature); + } + + void Declared(uint member, TypeDefinitionHandle declaring) + { + Link(1, member, position, TokenOf(declaring)); + Token(TokenOf(declaring)); + } + + /// + /// The base classes of a class definition, with their field rows, through any slice they live in. + /// + void Chain(SrmTypeDefinition definition) + { + if (!IsClass(definition) || definition.BaseType.IsNil) return; + var parent = TokenOf(definition.BaseType); + Token(parent); + Arity(parent); + if ((parent >> 24) == 0x02) + ClassChain(parent); + else if ((parent >> 24) == 0x01 && linked.TryGetValue((0, parent), out var target)) + collection.At(target.Slice).ClassChain(target.Target); + } + + void ClassChain(uint token) + { + if (!chained.Add(token)) return; + var definition = Reader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle(RowOf(token))); + FieldRows(token, definition, signatures: false); + Chain(definition); + } + + void Field(uint token) + { + Row(token); + var handle = MetadataTokens.FieldDefinitionHandle(RowOf(token)); + var field = Reader.GetFieldDefinition(handle); + Text(field.Name); + Signature(field.Signature, Form.Field); + var declaring = field.GetDeclaringType(); + Declared(token, declaring); + var owner = Reader.GetTypeDefinition(declaring); + // The owner's fields up to this one: Lean counts the instance fields before it over the carried rows + // from the owner's first field. Base classes, whose totals the index needs, stay whole. + Fields(TokenOf(declaring), owner).Through(token, Row); + if (IsClass(owner) && chainedOwners.Add(TokenOf(declaring))) Chain(owner); + } + + void MethodDef(uint token) + { + Row(token); + var method = Reader.GetMethodDefinition(MetadataTokens.MethodDefinitionHandle(RowOf(token))); + Text(method.Name); + Signature(method.Signature, Form.Method); + Declared(token, method.GetDeclaringType()); + } + + void MemberRef(uint token) + { + Row(token); + var member = Reader.GetMemberReference(MetadataTokens.MemberReferenceHandle(RowOf(token))); + Text(member.Name); + Signature(member.Signature, + member.GetKind() == MemberReferenceKind.Field ? Form.Field : Form.Method, true); + Token(TokenOf(member.Parent)); + Arity(TokenOf(member.Parent)); + var reference = (MemberReference)module.LookupToken(new MetadataToken(token)); + var declaring = reference.DeclaringType is GenericInstanceType generic + ? generic.ElementType : reference.DeclaringType; + if (!IsPinned(declaring)) return; + (ModuleDefinition definingModule, uint definitionToken) = reference switch + { + MethodReference method when method.Resolve() is { } resolved => + (resolved.Module, resolved.MetadataToken.ToUInt32()), + FieldReference field when field.Resolve() is { } resolved => + (resolved.Module, resolved.MetadataToken.ToUInt32()), + _ => throw new InvalidDataException("Unresolved pinned member: " + reference.FullName) + }; + var target = collection.SliceOf(definingModule); + Link(0, token, target.PositionOf, definitionToken); + target.Token(definitionToken); + } + + void MethodSpec(uint token) + { + Row(token); + var specification = Reader.GetMethodSpecification(MetadataTokens.MethodSpecificationHandle(RowOf(token))); + Signature(specification.Signature, Form.Instantiation); + Token(TokenOf(specification.Method)); + DeclaringArity(TokenOf(specification.Method)); + } + + void Standalone(uint token) + { + Row(token); + Signature(Reader.GetStandaloneSignature(MetadataTokens.StandaloneSignatureHandle(RowOf(token))).Signature, + Form.Standalone); + } + + void TypeSpec(uint token) + { + Row(token); + Signature(Reader.GetTypeSpecification(MetadataTokens.TypeSpecificationHandle(RowOf(token))).Signature, + Form.Type); + } + + /// + /// A method definition with what its identity check reads: its owner's and its own generic parameter rows, + /// and for a class receiver the fields of its owner and bases. + /// + internal void Method(MethodDefinition original, bool classOwner) + { + var token = original.MetadataToken.ToUInt32(); + Token(token); + var method = Reader.GetMethodDefinition(MetadataTokens.MethodDefinitionHandle(RowOf(token))); + // A method without generic parameters has a signature without GENERIC, which Lean reads instead. + if (method.GetGenericParameters().Count != 0) GenericRun(token, method.GetGenericParameters()); + Arity(TokenOf(method.GetDeclaringType())); + if (!classOwner) return; + var owner = Reader.GetTypeDefinition(method.GetDeclaringType()); + if (fieldsAdded.Add(TokenOf(method.GetDeclaringType()))) + { + FieldRows(TokenOf(method.GetDeclaringType()), owner, signatures: false); + Chain(owner); + } + } + + /// + /// An operand token with what its check reads, the arity of a type it names or is declared on included. + /// + internal void Operand(uint token) + { + Token(token); + Arity(token); + DeclaringArity(token); + } + + /// + /// The generic parameter rows that count the parameters of the type a TypeDef token, or a + /// TypeRef linked to a carried slice, names; read where the type is named or used as a base. + /// + internal void Arity(uint token) + { + if (token >> 24 == 0x01) + { + if (linked.TryGetValue((0, token), out var target)) + collection.At(target.Slice).Arity(target.Target); + return; + } + if (token >> 24 != 0x02 || !arities.Add(token)) return; + var definition = Reader.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle(RowOf(token))); + GenericRun(token, definition.GetGenericParameters()); + } + + /// The arity of the type a Field or MethodDef token is declared on. + void DeclaringArity(uint token) + { + var declaring = (token >> 24) switch + { + 0x04 => Reader.GetFieldDefinition(MetadataTokens.FieldDefinitionHandle(RowOf(token))) + .GetDeclaringType(), + 0x06 => Reader.GetMethodDefinition(MetadataTokens.MethodDefinitionHandle(RowOf(token))) + .GetDeclaringType(), + _ => default + }; + if (!declaring.IsNil) Arity(TokenOf(declaring)); + } + + /// + /// The generic parameter rows of an owner, the rows around them and the link to the first; for an owner + /// with none, the rows that show it has none. + /// + void GenericRun(uint owner, GenericParameterHandleCollection parameters) + { + if (parameters.Count == 0) + { + GenericGap(owner); + return; + } + var numbers = parameters.Select(handle => MetadataTokens.GetRowNumber(handle)).Order().ToArray(); + Link(3, owner, position, 0x2A000000 + (uint)numbers[0]); + foreach (var number in numbers) Row(0x2A000000 + (uint)number); + if (numbers[0] > 1) Row(0x2A000000 + (uint)numbers[0] - 1); + if (numbers[^1] < image.Count(0x2A)) Row(0x2A000000 + (uint)numbers[^1] + 1); + } + + /// + /// The first generic parameter row whose owner's coded value lies above an owner's and the row before it, + /// or the last row when none lies above: in the owner-sorted table they bracket where its run would be. + /// + void GenericGap(uint owner) + { + var count = image.Count(0x2A); + var code = GenericOwnerCode(owner); + int low = 1, high = count + 1; + while (low < high) + { + var middle = low + (high - low) / 2; + var parent = Reader.GetGenericParameter(MetadataTokens.GenericParameterHandle(middle)).Parent; + if (GenericOwnerCode(TokenOf(parent)) > code) high = middle; + else low = middle + 1; + } + if (low <= count) Row(0x2A000000 + (uint)low); + if (low > 1) Row(0x2A000000 + (uint)low - 1); + } + + /// The TypeOrMethodDef coded value of a type or method token, the table's sort key. + static uint GenericOwnerCode(uint owner) => (owner & 0xFFFFFF) * 2 + (owner >> 24 == 0x06 ? 1u : 0u); + + /// The primitive types' assembly reference, which Lean names the scope of integer types by. + internal void Finish() + { + var core = module.TypeSystem.CoreLibrary as AssemblyNameReference; + var index = core is null ? -1 : module.AssemblyReferences.ToList().FindIndex(reference => + reference.FullName == core.FullName); + if (index < 0) return; + corlib = index + 1; + Token(0x23000000 + (uint)corlib); + } + + enum Heap { None, Text, Blob } + + /// + /// The columns of a carried row, each with its width and the heap it indexes. The widths follow the + /// table stream header, as Lean's decoder reads them. + /// + (int Width, Heap Heap)[] Columns(TableIndex table) + { + int Rows(TableIndex index) => Reader.GetTableRowCount(index); + int Simple(TableIndex index) => Rows(index) > 0xFFFF ? 4 : 2; + int Coded(int bits, params TableIndex[] tables) => tables.Max(Rows) < 1 << (16 - bits) ? 2 : 4; + var text = image.HeapIndexWidth(HeapIndex.String); + var blob = image.HeapIndexWidth(HeapIndex.Blob); + var scope = Coded(2, TableIndex.Module, TableIndex.ModuleRef, TableIndex.AssemblyRef, TableIndex.TypeRef); + var typeDefOrRef = Coded(2, TableIndex.TypeDef, TableIndex.TypeRef, TableIndex.TypeSpec); + return table switch + { + TableIndex.TypeRef => [(scope, Heap.None), (text, Heap.Text), (text, Heap.Text)], + TableIndex.TypeDef => [(4, Heap.None), (text, Heap.Text), (text, Heap.Text), + (typeDefOrRef, Heap.None), (Simple(TableIndex.Field), Heap.None), + (Simple(TableIndex.MethodDef), Heap.None)], + TableIndex.Field => [(2, Heap.None), (text, Heap.Text), (blob, Heap.Blob)], + TableIndex.MethodDef => [(4, Heap.None), (2, Heap.None), (2, Heap.None), (text, Heap.Text), + (blob, Heap.Blob), (Simple(TableIndex.Param), Heap.None)], + TableIndex.MemberRef => [(Coded(3, TableIndex.TypeDef, TableIndex.TypeRef, TableIndex.ModuleRef, + TableIndex.MethodDef, TableIndex.TypeSpec), Heap.None), (text, Heap.Text), (blob, Heap.Blob)], + TableIndex.StandAloneSig or TableIndex.TypeSpec => [(blob, Heap.Blob)], + TableIndex.GenericParam => [(2, Heap.None), (2, Heap.None), + (Coded(1, TableIndex.TypeDef, TableIndex.MethodDef), Heap.None), (text, Heap.Text)], + TableIndex.MethodSpec => [(Coded(1, TableIndex.MethodDef, TableIndex.MemberRef), Heap.None), + (blob, Heap.Blob)], + TableIndex.AssemblyRef => [(8, Heap.None), (4, Heap.None), (blob, Heap.Blob), (text, Heap.Text), + (text, Heap.Text), (blob, Heap.Blob)], + TableIndex.ExportedType => [(8, Heap.None), (text, Heap.Text), (text, Heap.Text), + (Coded(2, TableIndex.File, TableIndex.AssemblyRef, TableIndex.ExportedType), Heap.None)], + TableIndex.NestedClass => [(Simple(TableIndex.TypeDef), Heap.None), + (Simple(TableIndex.TypeDef), Heap.None)], + _ => throw new InvalidDataException($"No column layout for table {table}.") + }; + } + + /// + /// Renumber the #Strings and #Blob offsets in the rows and the heap keys: the number of a + /// carried entry is its rank among the carried entries of its heap by bytes, from 1, and 0 stays 0. Lean + /// reads a heap entry only through the offset a row names, so any consistent numbering decodes the same + /// rows. The image's own offsets move whenever an earlier name changes length, and its entry order is a + /// hash or suffix sort that one changed name can reshuffle, so either would change the evidence of every + /// method that merely shares the module; the rank depends only on what the extraction carries. + /// + internal void Canonicalize() + { + // By content, not by image order: the image orders its heaps by a hash or a suffix sort, so one + // changed name can move the position of every other entry. + Dictionary Numbering(SortedDictionary heap, int width, string name) + { + var next = 1; + var numbers = new Dictionary { [0] = 0 }; + foreach (var entry in heap.Where(entry => entry.Key != 0) + .OrderBy(entry => Convert.ToHexString(entry.Value), StringComparer.Ordinal)) + { + RequireHeapRank((uint)next, width, name); + numbers[entry.Key] = next++; + } + return numbers; + } + var textNumbers = Numbering(strings, image.HeapIndexWidth(HeapIndex.String), "#Strings"); + var blobNumbers = Numbering(blobs, image.HeapIndexWidth(HeapIndex.Blob), "#Blob"); + foreach (var token in rows.Keys.ToArray()) + { + var row = rows[token]; + var at = 0; + foreach (var (width, heap) in Columns((TableIndex)(token >> 24))) + { + if (heap != Heap.None) + { + var offset = width == 2 ? BitConverter.ToUInt16(row, at) : (int)BitConverter.ToUInt32(row, at); + var numbers = heap == Heap.Text ? textNumbers : blobNumbers; + // A position-only row's omitted entry must remain unreadable even when zero is carried. + var number = numbers.TryGetValue(offset, out var rank) + ? (uint)rank : OmittedHeapOffset(width); + var bytes = BitConverter.GetBytes(number); + Array.Copy(bytes, 0, row, at, width); + } + at += width; + } + if (at != row.Length) + throw new InvalidDataException($"The columns of row 0x{token:X8} do not add up to its length."); + } + Renumber(strings, textNumbers); + Renumber(blobs, blobNumbers); + RequireRanked(strings, "#Strings"); + RequireRanked(blobs, "#Blob"); + } + + /// + /// The emitted numbering is the rank by bytes; a change back to the image's order stops here. + /// + static void RequireRanked(SortedDictionary heap, string name) + { + var rank = 1; + string? previous = null; + foreach (var (key, value) in heap.Where(entry => entry.Key != 0)) + { + var hex = Convert.ToHexString(value); + if (key != rank++ || previous is not null && string.CompareOrdinal(previous, hex) > 0) + throw new InvalidDataException( + $"The carried {name} entries are not numbered by their rank in bytes."); + previous = hex; + } + } + + static void Renumber(SortedDictionary heap, Dictionary numbers) + { + var entries = heap.ToArray(); + heap.Clear(); + foreach (var (key, value) in entries) heap[numbers[key]] = value; + } + + internal int AssemblyIndex() => collection.Pinned.Index(collection.Pinned.Working(module)); + + internal string Lean() + { + var assembly = $"assembly{AssemblyIndex()}Metadata"; + var linkList = string.Join(", ", SortedLinks().Select(link => + $"⟨{link.Kind}, {link.Source}, {link.Slice}, {link.Target}⟩")); + return $"{{ assembly := {assembly}, rows := {Entries(rows)}, strings := {Entries(strings)}, " + + $"blobs := {Entries(blobs)}, userStrings := {Entries(userStrings)}, corlib := {corlib}, " + + $"links := [{linkList}] }}"; + } + + IEnumerable<(int Kind, uint Source, int Slice, uint Target)> SortedLinks() => + links.OrderBy(link => ((ulong)link.Kind << 32) + link.Source); + + /// + /// A balanced search tree over entries in ascending key order, as Lean's SearchTree.ofSorted builds + /// it: the first count / 2 entries go left of the root. + /// + static string Tree(IReadOnlyList<(ulong Key, string Value)> entries) + { + string Build(int start, int count) + { + if (count == 0) return ".leaf"; + var left = count / 2; + var root = entries[start + left]; + var right = Build(start + left + 1, count - 1 - left); + return $"(.node {Build(start, left)} {root.Key} {root.Value} {right})"; + } + return Build(0, entries.Count); + } + + static (ulong, string) Entry(KeyValuePair entry) where TKey : struct, IConvertible => + (Convert.ToUInt64(entry.Key, CultureInfo.InvariantCulture), ByteLiterals.Of(entry.Value)); + + /// + /// The instance field counts Lean derives from the carried rows: for each field of a carried type whose + /// fields and following type are carried, the instance fields before it, and for the type their total. + /// + (string Before, string Total) FieldCounts() + { + var before = new List<(ulong, string)>(); + var total = new List<(ulong, string)>(); + var typeCount = (uint)Reader.GetTableRowCount(TableIndex.TypeDef); + var fieldCount = (uint)Reader.GetTableRowCount(TableIndex.Field); + foreach (var (token, row) in rows.Where(entry => entry.Key >> 24 == 0x02)) + { + var first = image.FieldList(row); + uint stop; + if ((token & 0xFFFFFF) == typeCount) stop = fieldCount + 1; + else if (rows.TryGetValue(token + 1, out var next)) stop = image.FieldList(next); + else continue; + var found = new List<(ulong, string)>(); + uint instances = 0; + var complete = true; + for (uint index = 0; index < (stop > first ? stop - first : 0) && complete; index++) + { + var field = 0x04000000u + first + index; + complete = rows.TryGetValue(field, out var bytes); + if (!complete) break; + found.Add((field, instances.ToString(CultureInfo.InvariantCulture))); + if (bytes![0] / 0x10 % 2 == 0) instances++; + } + before.AddRange(found); + if (complete) total.Add((token, instances.ToString(CultureInfo.InvariantCulture))); + } + return (Tree(before), Tree(total)); + } + + /// Each owner's first carried parameter and count, keyed by the owner's coded value. + string GenericOwners() + { + var ownerWidth = Columns(TableIndex.GenericParam)[2].Width; + var genericOwners = new SortedDictionary(); + foreach (var (token, bytes) in rows.Where(entry => entry.Key >> 24 == 0x2A)) + { + uint codedOwner = 0; + for (var offset = 0; offset < ownerWidth; offset++) + codedOwner |= (uint)bytes[4 + offset] << (8 * offset); + genericOwners[codedOwner] = genericOwners.TryGetValue(codedOwner, out var previous) + ? (previous.First, previous.Count + 1) : (token, 1); + } + var ownerEntries = genericOwners.Select(entry => + ((ulong)entry.Key, $"({entry.Value.First}, {entry.Value.Count})")).ToList(); + return Tree(ownerEntries); + } + + /// + /// The Lean IndexTables of this slice, equal to what the decoder builds from its bytes. + /// + internal string Tables() + { + var (before, total) = FieldCounts(); + var linkEntries = SortedLinks().Select(link => + (((ulong)link.Kind << 32) + link.Source, $"({link.Slice}, {link.Target})")).ToList(); + return $"{{ rows := {Tree([.. rows.Select(Entry)])}, strings := {Tree([.. strings.Select(Entry)])}, " + + $"blobs := {Tree([.. blobs.Select(Entry)])}, userStrings := {Tree([.. userStrings.Select(Entry)])}, " + + $"links := {Tree(linkEntries)}, instanceBefore := {before}, instanceTotal := {total}, " + + $"genericOwners := {GenericOwners()} }}"; + } + + internal int PositionOf => position; + } +} diff --git a/Extractor/MetadataValidation.cs b/Extractor/MetadataValidation.cs index 8f4fe2579..e16ad2abb 100644 --- a/Extractor/MetadataValidation.cs +++ b/Extractor/MetadataValidation.cs @@ -18,7 +18,7 @@ static RefLikeMetadata RefLikeMarker(TypeReference type) var marker = markers[0]; var constructor = marker.Constructor; if (markers.Length != 1 || marker.AttributeType is TypeSpecification || - !RuntimeModels.ExactScope(marker.AttributeType.Scope, "System.Runtime") || + !RuntimeModels.ExactScope(marker.AttributeType, PlatformAssemblies.SystemRuntime) || constructor.Name != ".ctor" || !constructor.HasThis || constructor.ExplicitThis || constructor.CallingConvention != MethodCallingConvention.Default || constructor.HasGenericParameters || constructor.Parameters.Count != 0 || @@ -69,8 +69,8 @@ internal static bool IsAggregate(TypeReference type, ModuleDefinition module) => internal static int? IntegerVectorWidth(TypeReference type) { if (type is not GenericInstanceType { GenericArguments.Count: 1 } vector || - !RuntimeModels.ExactScope(vector.ElementType.Scope, "System.Runtime.Intrinsics") || - !RuntimeModels.ExactScope(vector.GenericArguments[0].Scope, "System.Runtime") || + !RuntimeModels.ExactScope(vector.ElementType, PlatformAssemblies.SystemRuntimeIntrinsics) || + !RuntimeModels.ExactScope(vector.GenericArguments[0], PlatformAssemblies.SystemRuntime) || vector.GenericArguments[0].FullName is not ("System.Byte" or "System.Int32" or "System.UInt32" or "System.Int64" or "System.UInt64")) return null; return vector.ElementType.FullName switch @@ -102,10 +102,13 @@ internal static InstanceField InstanceFieldOf(FieldReference reference) { var definition = reference.Resolve() ?? throw new InvalidDataException("Unresolved aggregate field"); if (definition.IsStatic || reference.Name != definition.Name || - MethodSpecialization.SignatureType(reference.FieldType) != - MethodSpecialization.SignatureType(definition.FieldType)) + StructuralSignatures.Current.Type(reference.FieldType) != + StructuralSignatures.Current.Type(definition.FieldType)) throw new InvalidDataException("Mismatched aggregate field signature: " + reference.FullName); - return Fields(reference.DeclaringType).Single(field => field.Definition == definition); + // The owner and the field resolve separately and must reach one definition, or the field is not the owner's. + return Array.Find(Fields(reference.DeclaringType), field => field.Definition == definition) ?? + throw new InvalidDataException("Unresolved aggregate field: " + reference.FullName + + " is not an instance field of its declaring type as resolved"); } // A read-only validation/emission phase owns its context; metadata rewrites require a new one. @@ -177,7 +180,11 @@ internal int FieldIndex(TypeReference owner, FieldDefinition field, bool include internal AggregateInfo AggregateLayout(TypeReference reference) { - if (validated.TryGetValue(LayoutEvidence.Identity(reference), out var cached)) return cached; + if (validated.TryGetValue(LayoutEvidence.Identity(reference), out var cached)) + { + if (ValueTuples.Is(reference)) ValueTuples.RequirePrimitiveArguments((GenericInstanceType)reference); + return cached; + } if (imported.TryGetValue(LayoutEvidence.Identity(reference), out TypeDefinition? definition)) { // The selected module's own metadata computes an imported layout, as admission did. @@ -338,9 +345,6 @@ static AggregateInfo TupleLayout(TypeReference reference, ModuleDefinition modul { RuntimeModels.ValidateTypeIdentity(reference, module, architecture); var fields = ValueTuples.Fields(reference); - foreach (var field in fields) - if (field.Type is TypeSpecification || EnumTypes.IsEnum(field.Type) || IsAggregate(field.Type, module)) - throw new InvalidDataException($"Unsupported field type: {field.Type.FullName}"); var width = PrimitiveWidth(fields[0].Type); if (fields.Any(field => PrimitiveWidth(field.Type) != width)) throw new InvalidDataException($"Unsupported aggregate layout: {reference.FullName} (auto layout reorders fields of different widths)"); @@ -433,6 +437,11 @@ internal sealed record Blocked(MethodDefinition Method, Instruction? At, Excepti const string OpenGenericMethod = "Unsupported method metadata: open generic method"; const string OpenGenericMember = "Unsupported method metadata: member of an open generic type"; + // The runtime-async implementation flag, absent from Cecil 0.11.6: such a body returns the awaited result and + // the runtime builds the declared task, so the translated body would not compute the method's return value. + const MethodImplAttributes RuntimeAsync = (MethodImplAttributes)0x2000; + const string RuntimeAsyncMethod = "Unsupported method metadata: runtime-async method\n" + + " Its body returns the awaited result, not the declared task."; // With `blocked`, failures after the selector resolves, the entry's shape included, are recorded // and the walk continues past them; without it, the first failure throws. Only profile-reachable @@ -440,7 +449,7 @@ internal sealed record Blocked(MethodDefinition Method, Instruction? At, Excepti internal static MethodDefinition[] Validate(ModuleDefinition module, string assemblyIdentity, string entrySignature, FeatureProfile profile, IReadOnlyCollection? initializedTypes = null, List? blocked = null, ImportResolution? imports = null, string[]? typeArguments = null, Func? importedByref = null, - List<(Instruction Instruction, object Operand)>? rewrites = null) + List<(Instruction Instruction, object Operand)>? rewrites = null, ResolvedCallSites? callSites = null) { profile.Validate(); initializedTypes ??= []; @@ -450,7 +459,7 @@ internal static MethodDefinition[] Validate(ModuleDefinition module, string asse string? Attribute(string name) => module.Assembly.CustomAttributes .SingleOrDefault(a => a.AttributeType.FullName == name)?.ConstructorArguments.Single().Value as string; if ((module.Assembly.Name.FullName != assemblyIdentity && module.Assembly.Name.Name != assemblyIdentity) || - Attribute("System.Runtime.Versioning.TargetFrameworkAttribute") != ".NETCoreApp,Version=v10.0" || + Attribute("System.Runtime.Versioning.TargetFrameworkAttribute") != ".NETCoreApp,Version=v11.0" || // MSBuild configuration names are case-insensitive: `-c release` stamps "release". !string.Equals(Attribute("System.Reflection.AssemblyConfigurationAttribute"), "Release", StringComparison.OrdinalIgnoreCase) || @@ -549,6 +558,8 @@ void Walk(MethodDefinition method) (method.ImplAttributes & (MethodImplAttributes.CodeTypeMask | MethodImplAttributes.ManagedMask | MethodImplAttributes.InternalCall | MethodImplAttributes.ForwardRef)) != 0) throw new InvalidDataException("Unsupported method metadata: no managed IL body"); + if ((method.ImplAttributes & RuntimeAsync) != 0) + throw new InvalidDataException(RuntimeAsyncMethod); }, named: true)) return; // Every other rejection is its own blocker; the body and callees are still analysed. void Reject(Func rejected, string reason) => Check(method, null, () => @@ -613,13 +624,12 @@ void Reject(Func rejected, string reason) => Check(method, null, () => else if (type is not ByReferenceType || importedByref?.Invoke(type) != true) Type(type, module, architecture: profile.Architecture, strings: true); }, named: true); - if (Check(method, null, () => Spans.Analyze(method, profile), named: true)) - foreach (VariableDefinition local in method.Body.Variables) - Check(method, null, () => - { - if (imports?.ValidateLocal(local.VariableType, profile) == true) ImportedLocal(method, local); - else Type(local.VariableType, module, architecture: profile.Architecture, strings: true); - }, named: true); + foreach (VariableDefinition local in method.Body.Variables) + Check(method, null, () => + { + if (imports?.ValidateLocal(local.VariableType, profile) == true) ImportedLocal(method, local); + else Type(local.VariableType, module, architecture: profile.Architecture, strings: true); + }, named: true); HashSet reachable = []; TypeTests.Analysis? typeTests = null; Throws.Analysis? throws = null; @@ -652,6 +662,7 @@ void CallSite(Instruction instruction, MethodReference reference) throw Call("Unsupported virtual call", reference, method, instruction, error.Message, error); } + callSites?.AddVirtual(instruction, table); foreach (var body in table.Select(entry => entry.Body).Distinct()) Enter(body, reference, instruction); return; @@ -663,7 +674,9 @@ void CallSite(Instruction instruction, MethodReference reference) if (VirtualDispatch.Unresolvable(instruction) is { } reason) throw Call("Unsupported virtual call", reference, method, instruction, reason); var resolved = ConstrainedCalls.Resolve(method, instruction, instructions, profile.Architecture); - Enter(ConstrainedCalls.Specialize(resolved, specializations), reference, instruction); + var implementation = ConstrainedCalls.Specialize(resolved, specializations); + callSites?.Add(instruction, implementation); + Enter(implementation, reference, instruction); return; } var pinnedCallee = reference.DeclaringType.Scope is AssemblyNameReference && @@ -699,6 +712,7 @@ void CallSite(Instruction instruction, MethodReference reference) throw Call("Unsupported generic newobj constructor", reference, method, instruction); rewrites?.Add((instruction, instruction.Operand)); instruction.Operand = specialized; + callSites?.Add(instruction, specialized); Enter(specialized, reference, instruction); return; } @@ -724,6 +738,7 @@ void CallSite(Instruction instruction, MethodReference reference) !IsAggregate(callee.DeclaringType, module) && !ObjectTypes.Candidate(callee.DeclaringType))) throw Call("Unsupported newobj constructor", reference, method, instruction, "newobj must construct a reference-free struct or a class of a pinned assembly"); + callSites?.Add(instruction, callee); Enter(callee, reference, instruction); } void StaticLoad(Instruction instruction, FieldReference loaded) @@ -796,8 +811,13 @@ target.DeclaringType is GenericInstanceType continue; } if (instruction.OpCode.Code is not (Code.Call or Code.Callvirt or Code.Newobj or Code.Ldftn or Code.Ldvirtftn) || instruction.Operand is not MethodReference reference || - typeTests!.Operands.Contains(instruction) || typeTests.Results.ContainsKey(instruction) || throws!.Operands.Contains(instruction)) + throws!.Operands.Contains(instruction)) continue; + if (typeTests!.Operands.Contains(instruction) || typeTests.Results.ContainsKey(instruction)) + { + callSites?.AddModel(instruction); + continue; + } if (!Check(method, instruction, () => { // Fold recognition scans the whole body, but only reachable unsupported throws are rejected. @@ -806,11 +826,21 @@ target.DeclaringType is GenericInstanceType "Folded throws require an allowlisted System.Runtime constructor with literal strings or null arguments."); if (instruction.OpCode.Code != Code.Newobj && reference.Name == ".ctor" && Spans.Is(reference.DeclaringType) && - !Spans.Analyze(method, profile).Constructors.ContainsKey(instruction)) - throw Call("Unsupported in-place span constructor", reference, method, instruction, - "The receiver must be a local address followed by at most three simple argument pushes."); - if (instruction.OpCode.Code == Code.Call && RuntimeModels.ObjectConstructor(reference)) return; - if (InstructionTranslation.RuntimeModel(reference, profile) is null) CallSite(instruction, reference); + !Spans.ConstructorCalls(method).Contains(instruction)) + throw Call("Unsupported in-place span constructor", reference, method, instruction, """ + The receiver must be the address of a local or parameter of the constructed type, + followed by at most three simple argument pushes and no branch target. + """); + if (instruction.OpCode.Code == Code.Call && + RuntimeModels.CallModel(reference, profile) is not null || + instruction.OpCode.Code == Code.Newobj && + Spans.ConstructorOperation(reference, profile.Architecture) is not null) + { + callSites?.AddModel(instruction); + return; + } + RuntimeModels.ValidateCallScope(reference); + CallSite(instruction, reference); })) skipped.Add(instruction); } @@ -827,7 +857,7 @@ target.DeclaringType is GenericInstanceType failure.Error.Message == "Unsupported type: " + modified); } ordered.Reverse(); - // Validate translation before producing any artifact. + // Validate the reachable operands and targets before emitting the method evidence. var methods = ordered.ToArray(); // Proofs bind methods by unscoped signature, so one signature must name one assembly's body. foreach (var group in methods.GroupBy(method => method.FullName, StringComparer.Ordinal)) @@ -839,11 +869,8 @@ target.DeclaringType is GenericInstanceType .ToDictionary(pair => pair.method, pair => pair.index); var constrainedTargets = new ConstrainedCalls.TargetIndex(methods); var layoutContext = new LayoutContext(module, profile.Architecture); - for (int i = 0; i < methods.Length; i++) + foreach (var method in methods) { - var index = i; - var method = methods[index]; - var instructions = new InstructionIndex(method); NativeOperands.Certificate? nativeOperands = null; Dictionary? parameterHomes = null; HashSet? reachable = null; @@ -851,14 +878,14 @@ target.DeclaringType is GenericInstanceType { reachable = Reachability.Analyze(method, profile); nativeOperands = NativeOperands.Certify(method, reachable, profile); - parameterHomes = ParameterStorage.IndexHomes(ParameterStorage.Analyze(method, reachable)); + parameterHomes = ParameterStorage.IndexHomes(ParameterStorage.Analyze(method, reachable), method.Body.Variables.Count); }), named: true)) continue; - foreach (Instruction instruction in reachable!) + var admission = new InstructionAdmission(method, profile, methodIndices, constrainedTargets, + layoutContext, imports, nativeOperands!, parameterHomes!); + foreach (var instruction in reachable!) if (!skipped.Contains(instruction)) - Check(method, instruction, () => - InstructionTranslation.Translate(instruction, method, methodIndices, index, instructions, - profile, nativeOperands, parameterHomes, imports, layoutContext, constrainedTargets), named: true); + Check(method, instruction, () => admission.Check(instruction), named: true); } return methods; } diff --git a/Extractor/MethodArguments.cs b/Extractor/MethodArguments.cs index 8513d12a6..03de47232 100644 --- a/Extractor/MethodArguments.cs +++ b/Extractor/MethodArguments.cs @@ -1,4 +1,5 @@ using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -17,6 +18,18 @@ internal static TypeReference Type(MethodDefinition method, int index) return InReferences.Parameter(method.Parameters[parameter].ParameterType); } + /// Whether argument is read as the whole word it holds, its declared type + /// being no narrow integer. + internal static bool Plain(MethodDefinition method, int index) => LeanEmitter.Narrow(Type(method, index)) is null; + + /// The argument (including the instance receiver) loaded by this CIL instruction. + internal static int? LoadIndex(MethodDefinition method, Instruction site) => site.OpCode.Code switch + { + Code.Ldarg_0 => 0, Code.Ldarg_1 => 1, Code.Ldarg_2 => 2, Code.Ldarg_3 => 3, + Code.Ldarg or Code.Ldarg_S => Index(method, (ParameterDefinition)site.Operand), + _ => null + }; + internal static int Index(MethodDefinition method, ParameterDefinition parameter) { if (method.HasThis && parameter == method.Body.ThisParameter) return 0; diff --git a/Extractor/MethodBodyBytes.cs b/Extractor/MethodBodyBytes.cs new file mode 100644 index 000000000..c213b40af --- /dev/null +++ b/Extractor/MethodBodyBytes.cs @@ -0,0 +1,27 @@ +using System.Reflection.Metadata; +using System.Reflection.PortableExecutable; + +namespace Nethermind.LeanCIL; + +/// +/// The raw bytes of a method body, read from the module image at the method's relative virtual address. +/// Lean re-checks the header and the code length; the body's location in the image stays trusted. +/// +internal static class MethodBodyBytes +{ + /// The header and code bytes of a body, and how many of them are header. + internal sealed record Body(byte[] Bytes, int HeaderLength); + + /// + /// The header and code of the body at . A body with exception regions is refused, + /// so its size is exactly its header and code. + /// + internal static Body Read(PEReader image, int rva) + { + MethodBodyBlock body = image.GetMethodBody(rva); + if (body.ExceptionRegions.Length != 0) + throw new InvalidDataException($"Method body at RVA 0x{rva:X} has exception regions."); + byte[] bytes = [.. image.GetSectionData(rva).GetContent(0, body.Size)]; + return new(bytes, bytes.Length - body.GetILBytes()!.Length); + } +} diff --git a/Extractor/MethodSpecialization.cs b/Extractor/MethodSpecialization.cs index 0f97a7b84..ff7161549 100644 --- a/Extractor/MethodSpecialization.cs +++ b/Extractor/MethodSpecialization.cs @@ -13,7 +13,7 @@ internal sealed record Origin(string Definition, string[] TypeArguments); static readonly ConditionalWeakTable ownerArguments = new(); static readonly ConditionalWeakTable definitions = new(); static readonly ConditionalWeakTable declaringTypes = new(); - readonly Dictionary instances = new(StringComparer.Ordinal); + readonly SortedDictionary instances = []; internal static Origin? Source(MethodDefinition method) => origins.TryGetValue(method, out var origin) ? origin : null; @@ -29,23 +29,6 @@ internal static IReadOnlyList OwnerArguments(MethodDefinition met internal static TypeReference DeclaringType(MethodDefinition method) => declaringTypes.TryGetValue(method, out var owner) ? owner : method.DeclaringType; - internal static string SignatureType(TypeReference type) => type switch - { - GenericParameter parameter => $"{parameter.Type}:{parameter.Position}", - ByReferenceType reference => SignatureType(reference.ElementType) + "&", - // Other modifiers keep their full spelling below and cannot equal an `in` or `init` tag. - RequiredModifierType modifier when InReferences.Is(modifier) => SignatureType(modifier.ElementType) + " in", - RequiredModifierType modifier when InitAccessors.Is(modifier) => SignatureType(modifier.ElementType) + " init", - PointerType pointer => SignatureType(pointer.ElementType) + "*", - ArrayType { IsVector: true } array => SignatureType(array.ElementType) + "[]", - GenericInstanceType instance => LayoutEvidence.Identity(instance.ElementType) + "<" + - string.Join(",", instance.GenericArguments.Select(SignatureType)) + ">", - FunctionPointerType pointer => $"fn:{pointer.CallingConvention}:{pointer.HasThis}:{pointer.ExplicitThis}:" + - SignatureType(pointer.ReturnType) + "(" + - string.Join(",", pointer.Parameters.Select(parameter => SignatureType(parameter.ParameterType))) + ")", - _ => LayoutEvidence.Identity(type) - }; - internal MethodDefinition Instantiate(MethodReference reference) { var definition = reference.Resolve() ?? throw new InvalidDataException($"Unresolved generic method: {reference.FullName}"); @@ -60,9 +43,12 @@ internal MethodDefinition Instantiate(MethodReference reference) reference.ExplicitThis || methodArguments.Length != definition.GenericParameters.Count || declaringArguments.Length != definition.DeclaringType.GenericParameters.Count || (methodArguments.Length == 0 && declaringArguments.Length == 0) || - member.Name != definition.Name || SignatureType(member.ReturnType) != SignatureType(definition.ReturnType) || - !member.Parameters.Select(parameter => SignatureType(parameter.ParameterType)) - .SequenceEqual(definition.Parameters.Select(parameter => SignatureType(parameter.ParameterType))) || + member.Name != definition.Name || + StructuralSignatures.Current.Type(member.ReturnType) != + StructuralSignatures.Current.Type(definition.ReturnType) || + StructuralSignatures.Current.Ordered(member.Parameters.Select(parameter => parameter.ParameterType)) != + StructuralSignatures.Current.Ordered( + definition.Parameters.Select(parameter => parameter.ParameterType)) || (declaringArguments.Length != 0 && !definition.IsStatic && !GenericStructs.Is(reference.DeclaringType)) || (definition.IsConstructor && (methodArguments.Length != 0 || definition.IsStatic)) || !definition.HasBody || definition.Body.ExceptionHandlers.Count != 0 || @@ -91,21 +77,21 @@ internal MethodDefinition Instantiate(MethodReference reference) var type = constraint.ConstraintType; if (type is RequiredModifierType modifier && modifier.ModifierType.FullName == "System.Runtime.InteropServices.UnmanagedType" && - RuntimeModels.ExactScope(modifier.ModifierType.Scope, "System.Runtime")) + RuntimeModels.ExactScope(modifier.ModifierType, PlatformAssemblies.SystemRuntime)) type = modifier.ElementType; // A constraint naming parameters, such as `T : I`, is checked at these arguments. if (GenericStructs.DirectInterfaceConstraint( type.ContainsGenericParameter && type is GenericInstanceType ? Substitute(type) : type, argument, module)) continue; - if (type.FullName != "System.ValueType" || !RuntimeModels.ExactScope(type.Scope, "System.Runtime")) + if (type.FullName != "System.ValueType" || + !RuntimeModels.ExactScope(type, PlatformAssemblies.SystemRuntime)) throw new InvalidDataException($"Unsupported generic method constraints: {definition.FullName}"); RuntimeModels.ValidateTypeIdentity(type, module, architecture); } } - var key = definition.MetadataToken.ToInt32() + ":" + SignatureType(reference.DeclaringType) + - "<" + string.Join(",", methodArguments.Select(SignatureType)) + ">"; + var key = StructuralSignatures.Current.Specialization(definition, reference.DeclaringType, methodArguments); if (instances.TryGetValue(key, out var existing)) return existing; if (instances.Count >= 256) throw new InvalidDataException("Generic specialization limit exceeded (possible expanding recursion)"); diff --git a/Extractor/ModifierTwins.cs b/Extractor/ModifierTwins.cs index 638e5f4eb..cd9a1b94d 100644 --- a/Extractor/ModifierTwins.cs +++ b/Extractor/ModifierTwins.cs @@ -23,9 +23,6 @@ internal static void Reject(MethodDefinition method) static bool Modified(MethodDefinition method) => InitAccessors.Is(method.ReturnType) || method.Parameters.Any(parameter => InReferences.Is(parameter.ParameterType)); - static string Erased(MethodDefinition method) => - $"{method.HasThis}:{method.ExplicitThis}:{(int)method.CallingConvention}:{method.GenericParameters.Count}:" + - MethodSpecialization.SignatureType(InitAccessors.Return(method.ReturnType)) + "(" + - string.Join(",", method.Parameters.Select(parameter => - MethodSpecialization.SignatureType(InReferences.Parameter(parameter.ParameterType)))) + ")"; + static StructuralSignatures.Signature Erased(MethodDefinition method) => + StructuralSignatures.Current.ErasedMethod(method); } diff --git a/Extractor/NativeDispatch.cs b/Extractor/NativeDispatch.cs index 11fd40ebb..9fb9e9433 100644 --- a/Extractor/NativeDispatch.cs +++ b/Extractor/NativeDispatch.cs @@ -28,12 +28,13 @@ internal sealed class NativeDispatch internal static NativeDispatch For(MethodDefinition method) => Cache.GetValue(method, static value => new(value)); - internal string? Kind(TypeReference type, int? local = null) + internal LocalKind? Kind(TypeReference type, int? local = null) { if (signature is null) return null; var table = type is PointerType pointer && Native(pointer.ElementType); if (!table && !(local is { } index && pointerLocals.Contains(index) && Native(type))) return null; - return $".{(table ? "methodTable" : "methodPointer")} {ManagedPointers.SignatureEvidence(signature)}"; + var evidence = ManagedPointers.SignatureEvidence(signature); + return table ? new LocalKind.MethodTable(evidence) : new LocalKind.MethodPointer(evidence); } static bool Native(TypeReference type) => type.MetadataType is MetadataType.IntPtr or MetadataType.UIntPtr; diff --git a/Extractor/NativeOperands.cs b/Extractor/NativeOperands.cs index 4803313ae..2b0ea8fb3 100644 --- a/Extractor/NativeOperands.cs +++ b/Extractor/NativeOperands.cs @@ -9,15 +9,57 @@ internal static class NativeOperands { internal enum Kind { - Other, Word32, Word64, Native, Reference, NullObject, Object, MethodPointer, MethodTable, NativeMethodTable, + Other, Word32, Word64, Native, Reference, NullObject, Object, Opaque, MethodPointer, MethodTable, + NativeMethodTable, ArraySigned8, ArrayUnsigned8, ArraySigned16, ArrayUnsigned16, ArraySigned32, ArrayUnsigned32, ArraySigned64, ArrayUnsigned64, ArrayNativeInt, ArrayNativeUInt, ArrayBoolean, ArrayCharacter, ArrayComposite } - internal sealed record Certificate(HashSet Mixed, Dictionary Incoming); + internal sealed record Certificate(Dictionary Incoming); - internal readonly record struct StackType(Kind Kind, string? Metadata = null) + /// + /// A stack slot's type. A callable pointer or table is named by its metadata alone, so its kind is read from + /// the metadata and cannot disagree with it. + /// + internal readonly record struct StackType { + readonly Kind shape; + + internal StackType(Kind kind, LocalKind? metadata = null) + { + Metadata = metadata; + shape = kind; + if (CallableKind(metadata) is { } callable) + { + if (kind != callable) + throw new ArgumentException($"A {callable} slot is not of kind {kind}.", nameof(kind)); + shape = Kind.Other; + } + else if (kind is Kind.MethodPointer or Kind.MethodTable or Kind.NativeMethodTable) + throw new ArgumentException($"A {kind} slot names its signature.", nameof(metadata)); + } + + internal LocalKind? Metadata { get; } + + internal Kind Kind => CallableKind(Metadata) ?? shape; + + /// The signature of a callable pointer or table. + internal string Signature => Metadata switch + { + LocalKind.MethodPointer pointer => pointer.Signature, + LocalKind.MethodTable table => table.Signature, + LocalKind.NativeMethodTable native => native.Signature, + _ => throw new InvalidOperationException("The slot is not a callable pointer or table.") + }; + + static Kind? CallableKind(LocalKind? metadata) => metadata switch + { + LocalKind.MethodPointer => Kind.MethodPointer, + LocalKind.MethodTable => Kind.MethodTable, + LocalKind.NativeMethodTable => Kind.NativeMethodTable, + _ => null + }; + public static implicit operator StackType(Kind kind) => new(kind); } @@ -35,7 +77,7 @@ static StackType Type(TypeReference type) return CallableType(pointer, type is ArrayType); if (Arrays.Of(type) is { } array) return array.Kind == Kind.ArrayComposite || type is ArrayType buffer && EnumTypes.IsEnum(buffer.ElementType) - ? new StackType(array.Kind, $".array {array.Expression}") : array.Kind; + ? new StackType(array.Kind, new LocalKind.Array(array)) : array.Kind; var reference = type is ByReferenceType; var value = reference ? ((ByReferenceType)type).ElementType : type; if (Spans.Is(value) && value is GenericInstanceType span && @@ -43,15 +85,17 @@ static StackType Type(TypeReference type) { var element = Arrays.Storage(span.GenericArguments[0])!; return new(reference ? Kind.Reference : Kind.Other, - $".enumSpan {element.Expression} {Spans.Writable(value).ToString().ToLowerInvariant()} " + - reference.ToString().ToLowerInvariant()); + new LocalKind.EnumSpan(element, Spans.Writable(value), reference)); } return PrimitiveType(type); } - static StackType CallableType(IMethodSignature signature, bool table = false) => - new(table ? Kind.MethodTable : Kind.MethodPointer, - $".{(table ? "methodTable" : "methodPointer")} {ManagedPointers.SignatureEvidence(signature)}"); + static StackType CallableType(IMethodSignature signature, bool table = false) + { + var evidence = ManagedPointers.SignatureEvidence(signature); + return table ? new(Kind.MethodTable, new LocalKind.MethodTable(evidence)) + : new(Kind.MethodPointer, new LocalKind.MethodPointer(evidence)); + } static StackType CallType(MethodReference method, FeatureProfile profile) { @@ -59,9 +103,10 @@ static StackType CallType(MethodReference method, FeatureProfile profile) if (result is RequiredModifierType { ModifierType.FullName: "System.Runtime.InteropServices.InAttribute", ElementType: ByReferenceType }) { - var operation = InstructionTranslation.RuntimeModel(method, profile); - if (operation == ".memory (.observeView .spanPinnable)" || - operation?.StartsWith(".memory (.spanItem ", StringComparison.Ordinal) == true) + if (RuntimeModels.CallModel(method, profile) is not null && + method.Name is "get_Item" or "GetPinnableReference" && + method.DeclaringType is GenericInstanceType + { ElementType.FullName: "System.Span`1" or "System.ReadOnlySpan`1" }) return Kind.Reference; } return Type(result); @@ -78,6 +123,10 @@ MetadataType.Boolean or MetadataType.Char or MetadataType.SByte or MetadataType. _ => Kind.Other }; + // A reference of a generic class instance or System.Object, which only a store, a type test or a null test takes. + static bool UnnamedClassField(FieldReference field) => + OpaqueClassField(field) && FieldType(field) is GenericInstanceType or { MetadataType: MetadataType.Object }; + // An object reference whose class is outside the model; it only ever reaches a field, a test or a comparison. static bool OpaqueClassField(FieldReference field) => field.Resolve() is { IsStatic: false } && ObjectTypes.Is(field.DeclaringType) && @@ -98,12 +147,12 @@ internal static Certificate Certify(MethodDefinition method, IReadOnlySet dispatch.Kind(type, local) is { } kind - ? kind.StartsWith(".methodTable ", StringComparison.Ordinal) - ? new(Kind.NativeMethodTable, kind.Replace(".methodTable ", ".nativeMethodTable ", StringComparison.Ordinal)) + ? kind is LocalKind.MethodTable table + ? new(Kind.NativeMethodTable, new LocalKind.NativeMethodTable(table.Signature)) : new(Kind.MethodPointer, kind) : Type(type); var incoming = new Dictionary { [method.Body.Instructions[0]] = [] }; - var mixed = new HashSet(); + var pending = new Queue(); pending.Enqueue(method.Body.Instructions[0]); while (pending.TryDequeue(out var instruction)) @@ -136,8 +185,8 @@ StackType Pop() else if (code == Code.Ldind_I && stack.Count > 0 && stack[^1].Kind == Kind.NativeMethodTable) { var table = Pop(); - stack.Add(new(Kind.MethodPointer, table.Metadata!.Replace(".nativeMethodTable ", ".methodPointer ", - StringComparison.Ordinal))); + stack.Add(new(Kind.MethodPointer, + new LocalKind.MethodPointer(table.Signature))); } else if (code == Code.Localloc) { @@ -184,7 +233,7 @@ value.Kind is not (Kind.Word32 or Kind.Word64 or Kind.Native)) if (code is Code.Bne_Un or Code.Bne_Un_S or Code.Bgt_Un or Code.Bgt_Un_S or Code.Bge_Un or Code.Bge_Un_S or Code.Blt_Un or Code.Blt_Un_S or Code.Ble_Un or Code.Ble_Un_S) throw new InvalidDataException($"Unsupported mixed native/int32 unsigned branch; insert explicit conv.i or conv.u: {instruction}"); - mixed.Add(instruction); + } if (instruction.OpCode.FlowControl != FlowControl.Cond_Branch) stack.Add(code is Code.Ceq or Code.Clt or Code.Clt_Un or Code.Cgt or Code.Cgt_Un ? Kind.Word32 : @@ -215,7 +264,7 @@ value.Kind is not (Kind.Word32 or Kind.Word64 or Kind.Native)) throw new InvalidDataException("Managed table index must be an integer"); var table = Pop(); stack.Add(new(Kind.MethodPointer, - table.Metadata!.Replace(".methodTable ", ".methodPointer ", StringComparison.Ordinal))); + new LocalKind.MethodPointer(table.Signature))); } else if (code is Code.Call or Code.Callvirt or Code.Newobj) { @@ -271,6 +320,7 @@ StackBehaviour.Popref_popi_popi8 or StackBehaviour.Popref_popi_popr4 or Code.Ldloc or Code.Ldloc_S or Code.Ldloc_0 or Code.Ldloc_1 or Code.Ldloc_2 or Code.Ldloc_3 => Slot(method.Body.Variables[FeatureConstants.Local(instruction)].VariableType, FeatureConstants.Local(instruction)), + Code.Ldfld when UnnamedClassField((FieldReference)instruction.Operand) => Kind.Opaque, Code.Ldfld when OpaqueClassField((FieldReference)instruction.Operand) => Kind.Object, Code.Ldfld or Code.Ldsfld => Type(FieldType((FieldReference)instruction.Operand)), Code.Ldobj or Code.Ldelem_Any or Code.Castclass or Code.Isinst => @@ -321,6 +371,6 @@ void Successor(Instruction? target) if (instruction.OpCode.FlowControl is not (FlowControl.Return or FlowControl.Branch or FlowControl.Throw)) Successor(instruction.Next); } - return new Certificate(mixed, incoming); + return new Certificate(incoming); } } diff --git a/Extractor/ObjectOperandProof.cs b/Extractor/ObjectOperandProof.cs new file mode 100644 index 000000000..f19f1bb87 --- /dev/null +++ b/Extractor/ObjectOperandProof.cs @@ -0,0 +1,12 @@ +namespace Nethermind.LeanCIL; + +/// The admission proof for live object instructions with named operand shape facts. +internal static class ObjectOperandProof +{ + internal static string Render(string method, IEnumerable<(int Opcode, string Type)> operands) + { + var sites = string.Join(", ", operands.Select(site => + $"⟨{site.Opcode}, {site.Type}, _, {site.Type}_shape (body := {method}) rfl⟩")); + return $"Extraction.ObjectOperandsValid.of_sites [{sites}] rfl (by decide +kernel) (by decide +kernel)"; + } +} diff --git a/Extractor/ObjectTypes.cs b/Extractor/ObjectTypes.cs index 689a38623..21ba4609a 100644 --- a/Extractor/ObjectTypes.cs +++ b/Extractor/ObjectTypes.cs @@ -53,7 +53,8 @@ internal static TypeDefinition Validate(TypeReference type) throw new InvalidDataException(Unsupported + type.FullName + "\n A finalizer runs outside the extracted call graph."); var parent = definition.BaseType ?? throw new InvalidDataException(Unsupported + type.FullName); - if (parent.FullName == "System.Object" && RuntimeModels.ExactScope(parent.Scope, "System.Runtime")) + if (parent.FullName == "System.Object" && + RuntimeModels.ExactScope(parent, PlatformAssemblies.SystemRuntime)) return definition; if (!Candidate(parent)) throw new InvalidDataException(Unsupported + type.FullName + @@ -103,7 +104,7 @@ internal static int FieldIndex(TypeReference owner, FieldDefinition resolved) /// internal static bool IsString(TypeReference type) => type is not TypeSpecification && type.FullName == "System.String" && - RuntimeModels.ExactScope(type.Scope, "System.Runtime"); + RuntimeModels.ExactScope(type, PlatformAssemblies.SystemRuntime); /// /// A type whose values are object references on the stack and in argument and local roots: an admitted class, @@ -133,8 +134,8 @@ internal static bool Opaque(TypeReference type) => /// value-array field holds the origin of a whole array of its element type there, a struct element taking its /// width from the layout for . /// - internal static string FieldOperation(FieldReference field, FieldDefinition resolved, bool store, bool address, - string architecture) + internal static ClassViews.FieldOperation FieldOperation(FieldReference field, FieldDefinition resolved, + bool store, bool address, string architecture) { var owner = field.DeclaringType; Validate(owner); @@ -151,12 +152,11 @@ internal static string FieldOperation(FieldReference field, FieldDefinition reso " is another array, a generic parameter or a modified type."); var offset = FieldSlot * FieldIndex(owner, resolved); if (valueArray is not null) - return store - ? $".memory (.setArrayField ({GenericEvidence.ObjectType(owner)}) {offset} {valueArray.Expression})" - : $".memory (.arrayField ({GenericEvidence.ObjectType(owner)}) {offset} {valueArray.Expression})"; + return new(store, GenericEvidence.ObjectType(owner), offset, ClassViews.FieldKind.Array, + null, false, valueArray); if (holdsReference) - return store ? $".memory (.setObjectReferenceField ({GenericEvidence.ObjectType(owner)}) {offset})" - : $".memory (.objectReferenceField ({GenericEvidence.ObjectType(owner)}) {offset})"; + return new(store, GenericEvidence.ObjectType(owner), offset, + ClassViews.FieldKind.Reference, null, false, null); int width; try { width = MetadataValidation.PrimitiveWidth(type); } catch (InvalidDataException) @@ -165,29 +165,25 @@ internal static string FieldOperation(FieldReference field, FieldDefinition reso "\n Only integer, Boolean, Char and enum fields of class instances are modelled."); } var signed = EnumTypes.Storage(type).FullName is "System.SByte" or "System.Int16"; - return store ? $".memory (.setObjectField ({GenericEvidence.ObjectType(owner)}) {offset} {width})" - : $".memory (.objectField ({GenericEvidence.ObjectType(owner)}) {offset} {width} " + - $"{signed.ToString().ToLowerInvariant()})"; + return new(store, GenericEvidence.ObjectType(owner), offset, ClassViews.FieldKind.Word, width, + !store && signed, null); } /// The emitted list of an admitted class's base classes, nearest first. internal static string BaseList(TypeReference type) => - "[" + string.Join(", ", Bases(type).Select(parent => $"({GenericEvidence.ObjectType(parent)})")) + "]"; + LeanText.ClassList(Bases(type).Select(GenericEvidence.ObjectType)); /// - /// The checked operation for newobj of an admitted class: a fresh instance of its runtime type, with - /// one 8-byte slot per instance field including its bases', passed to at - /// method index . An abstract class has no instances of its own. + /// A concrete class allocation includes its base classes and one slot per inherited or declared instance field. /// - internal static string Construction(MethodDefinition constructor, int index) + internal static (string[] Bases, int Size) AllocationOf(MethodDefinition constructor) { var owner = Validate(constructor.DeclaringType); if (owner.IsAbstract) throw new InvalidDataException("Unsupported construction of an abstract class: " + owner.FullName + "\n newobj of an abstract class fails at run time; only its derived classes have instances."); var size = FieldSlot * Fields(owner).Length; - return $".newObject {index} {constructor.Parameters.Count} ({GenericEvidence.ObjectType(owner)}) " + - $"{BaseList(owner)} {size}"; + return ([.. Bases(owner).Select(GenericEvidence.ObjectType)], size); } /// @@ -203,7 +199,7 @@ internal static bool Testable(Instruction instruction) => /// class. An instance records only its own and its admitted base classes, so a test against an interface or /// System.Object is rejected, naming why. /// - internal static string Test(Instruction instruction) + internal static void ValidateTestOperand(Instruction instruction) { var target = (TypeReference)instruction.Operand; var site = $"Unsupported type test: {instruction.OpCode.Name} {target.FullName}\n "; @@ -222,8 +218,6 @@ internal static string Test(Instruction instruction) throw new InvalidDataException( site + error.Message.Replace("\n", "\n ", StringComparison.Ordinal), error); } - var throwing = instruction.OpCode.Code == Code.Castclass ? "true" : "false"; - return $".objectTest ({GenericEvidence.ObjectType(target)}) {throwing}"; } // Matches `CIL.objectFieldSlot`, which Lean uses to recheck every emitted offset. diff --git a/Extractor/Operation.cs b/Extractor/Operation.cs new file mode 100644 index 000000000..87176cebb --- /dev/null +++ b/Extractor/Operation.cs @@ -0,0 +1,76 @@ +using Mono.Cecil.Cil; + +namespace Nethermind.LeanCIL; + +/// +/// Instruction information used by extraction and proof outlines. +/// The compiled method's instructions come from Lean's decoder. +/// +internal abstract record Operation +{ + /// The operation does nothing: a prefix or an operand the translator folded away. + internal bool IsNop => this is StackOperation { Kind: Code.Nop }; +} + +/// A site without a separate outline descriptor. Outlines and diagnostics can inspect its CIL instruction. +internal sealed record Unoutlined : Operation +{ + internal static readonly Unoutlined Site = new(); +} + +/// The CIL stack opcode used by the proof outline for this site. +internal sealed record StackOperation(Code Kind) : Operation +{ + /// The operation is word addition, subtraction, multiplication or a bitwise combination. + internal bool IsArithmetic => Kind is Code.Add or Code.Sub or Code.Mul or + Code.And or Code.Or or Code.Xor; + + /// The infix operator that Lean's bit-vector arithmetic writes for an arithmetic operation. + internal string Symbol => Kind switch + { + Code.Add => "+", Code.Sub => "-", Code.Mul => "*", Code.And => "&&&", + Code.Or => "|||", Code.Xor => "^^^", + _ => throw new InvalidOperationException("Not an arithmetic operation: " + Kind) + }; + + internal static readonly StackOperation Nop = new(Code.Nop); +} + +/// The width and signedness a narrow integer is truncated to when it is read. +internal enum Narrowing { Signed8, Unsigned8, Signed16, Unsigned16 } + +internal static class NarrowingText +{ + /// The Lean constructor name. + internal static string LeanName(this Narrowing narrowing) => narrowing switch + { + Narrowing.Signed8 => "signed8", Narrowing.Unsigned8 => "unsigned8", + Narrowing.Signed16 => "signed16", Narrowing.Unsigned16 => "unsigned16", + _ => throw new ArgumentOutOfRangeException(nameof(narrowing)) + }; +} + +/// +/// The integer element an ldind, stind, ldelem or stelem opcode names; I is +/// native-sized. +/// +internal enum NumericKind { I1, U1, I2, U2, I4, U4, I8, I } + +/// The width, sign extension and Lean term of a . +internal static class NumericKinds +{ + /// Bytes an access of the kind moves; native int is eight bytes on every admitted profile. + internal static int Width(this NumericKind kind) => kind switch + { + NumericKind.I1 or NumericKind.U1 => 1, + NumericKind.I2 or NumericKind.U2 => 2, + NumericKind.I4 or NumericKind.U4 => 4, + _ => 8 + }; + + /// A load sign-extends: only the one- and two-byte signed kinds. + internal static bool Signed(this NumericKind kind) => kind is NumericKind.I1 or NumericKind.I2; + + /// The Lean term of the kind, such as .i4. + internal static string Term(this NumericKind kind) => "." + kind.ToString().ToLowerInvariant(); +} diff --git a/Extractor/ParameterEvidence.cs b/Extractor/ParameterEvidence.cs index da42c7840..e70fac057 100644 --- a/Extractor/ParameterEvidence.cs +++ b/Extractor/ParameterEvidence.cs @@ -16,14 +16,16 @@ static string Type(TypeReference type, ModuleDefinition module, LayoutEvidence l if (type is ByReferenceType or PointerType) return ".reference"; if (ObjectTypes.Reference(type) || ReferenceArrays.Is(type)) return ".object"; if (type is ArrayType array && MetadataValidation.IsAggregate(array.ElementType, module)) - return $".aggregateArray {System.Text.Json.JsonSerializer.Serialize(LayoutEvidence.Identity(array.ElementType))} " + - layouts.Index(array.ElementType); + { + var identity = IdentityText.Quote(LayoutEvidence.Identity(array.ElementType)); + return $".aggregateArray {identity} {layouts.Index(array.ElementType)}"; + } if (Arrays.Of(type) is { } element) return $".array {element.Expression}"; if (type is GenericInstanceType span && Spans.Is(type) && MetadataValidation.IsAggregate(span.GenericArguments[0], module)) - return $".aggregateSpan {layouts.Index(span.GenericArguments[0])} {(Spans.Writable(type) ? "true" : "false")}"; + return $".aggregateSpan {layouts.Index(span.GenericArguments[0])} {LeanText.Bool(Spans.Writable(type))}"; if (Spans.Width(type, layouts.Metadata.Architecture) is { } width) - return $".span {width} {(Spans.Writable(type) ? "true" : "false")}"; + return $".span {width} {LeanText.Bool(Spans.Writable(type))}"; if (type is GenericInstanceType vector && !MetadataValidation.IsAggregate(type, module)) return vector.ElementType.FullName switch { @@ -49,8 +51,8 @@ internal static void Emit(StringBuilder lean, MethodDefinition method, int numbe var arguments = new List(); if (method.HasThis) arguments.Add(ObjectTypes.Is(method.DeclaringType) ? ".object" : ".reference"); arguments.AddRange(method.Parameters.Select(parameter => InReferences.Parameter(parameter.ParameterType)) - .Select(type => dispatch.Kind(type) ?? Type(type, method.Module, layouts))); - var locals = method.Body.Variables.Select(local => dispatch.Kind(local.VariableType, local.Index) ?? + .Select(type => dispatch.Kind(type)?.ToLean() ?? Type(type, method.Module, layouts))); + var locals = method.Body.Variables.Select(local => dispatch.Kind(local.VariableType, local.Index)?.ToLean() ?? Type(local.VariableType, method.Module, layouts)); var code = method.Body.Instructions.Select(instruction => instruction.OpCode.Code switch { @@ -62,6 +64,5 @@ internal static void Emit(StringBuilder lean, MethodDefinition method, int numbe }); lean.AppendLine($"def method{number}RawArguments : Extraction.RawArguments := {{ arguments := [{string.Join(", ", arguments)}], locals := [{string.Join(", ", locals)}], code := [{string.Join(", ", code)}] }}"); lean.AppendLine($"def method{number}Reachable : List Bool := [{string.Join(", ", method.Body.Instructions.Select(instruction => reachable.Contains(instruction).ToString().ToLowerInvariant()))}]"); - lean.AppendLine($"theorem method{number}_parameters_validated : Extraction.ParameterHomesValid computedLayouts method{number}RawArguments method{number}Reachable method{number} := {LeanEmitter.Decide}"); } } diff --git a/Extractor/ParameterStorage.cs b/Extractor/ParameterStorage.cs index 6de35ad28..1591cd13b 100644 --- a/Extractor/ParameterStorage.cs +++ b/Extractor/ParameterStorage.cs @@ -5,11 +5,11 @@ namespace Nethermind.LeanCIL; internal static class ParameterStorage { - // Preserve first occurrence and original slot positions, including malformed duplicate homes. - internal static Dictionary IndexHomes(IEnumerable homes) + /// Frame slots follow the declared locals, in parameter-home order. + internal static Dictionary IndexHomes(IEnumerable homes, int localCount) { var positions = new Dictionary(); - var position = 0; + var position = localCount; foreach (var argument in homes) positions.TryAdd(argument, position++); return positions; } diff --git a/Extractor/PinnedAssemblies.cs b/Extractor/PinnedAssemblies.cs index 04abe772d..020ffa6e3 100644 --- a/Extractor/PinnedAssemblies.cs +++ b/Extractor/PinnedAssemblies.cs @@ -1,3 +1,6 @@ +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Runtime.InteropServices; using System.Security.Cryptography; using Mono.Cecil; @@ -16,21 +19,56 @@ internal sealed record Pin(string Identity, Guid Mvid, string Sha256); /// A referenced assembly on disk with the hash it must still have, if one was recorded. internal sealed record Source(string Path, string? ExpectedSha256 = null); - // Platform keys: only the extractor's trusted platform assemblies can answer these names. - static readonly string[] PlatformKeyTokens = - ["b03f5f7f11d50a3a", "cc7b13ffcd2ddd51", "7cec85d7bea7798e", "b77a5c561934e089"]; + /// An immutable assembly row, decoded once when resolution first requests it. + sealed class IdentityRow(MetadataImage image, uint token) + { + AssemblyName? identity; + + internal AssemblyName Read() + { + if (identity is not null) return identity; + bool definition = token == 0x20000001; + uint flags = definition ? (uint)image.Reader.GetAssemblyDefinition().Flags : + (uint)image.Reader.GetAssemblyReference( + MetadataTokens.AssemblyReferenceHandle((int)(token & 0xffffff))).Flags; + uint allowed = definition ? 0xc001u : 0x0001u; + if ((flags & ~allowed) != 0) + throw new InvalidDataException("Unsupported assembly identity flags in image row"); + return identity = image.AssemblyIdentity(token) ?? + throw new InvalidDataException("Invalid assembly identity in image row"); + } + } [ThreadStatic] static PinnedAssemblies? active; readonly PinnedAssemblies? previous; - readonly DefaultAssemblyResolver platform = new(); - readonly Dictionary modules = new(StringComparer.Ordinal); + readonly Dictionary modules = []; + // One platform assembly per identity, so two references to one platform type resolve to one definition. + readonly Dictionary platformAssemblies = []; + readonly Dictionary platformByName = []; + readonly Dictionary platformPaths = PlatformPaths(); + readonly Dictionary identityRows = new(ReferenceEqualityComparer.Instance); + readonly Dictionary> rowsByImage = []; + sealed record Registration(AssemblyNameReference[] Names, StructuralSignatures.Primitive[] Primitives); + readonly Dictionary registrations = []; + readonly Dictionary ownedModules = []; + bool disposed; readonly Dictionary originals = []; + readonly Dictionary working = []; + readonly Dictionary indices = []; + readonly Dictionary dispatchHierarchies = []; + readonly Dictionary byName = []; + readonly Dictionary images = []; + readonly Dictionary readers = []; + readonly Dictionary metadata = []; + readonly Dictionary identityImages = []; readonly List<(Pin Pin, string Path, ModuleDefinition Module)> references = []; internal ModuleDefinition Selected { get; } + internal StructuralSignatures Signatures { get; } + internal Pin SelectedPin { get; } /// The referenced pins in command-line order; the selected assembly is not included. @@ -39,53 +77,112 @@ internal sealed record Source(string Path, string? ExpectedSha256 = null); /// Every pin, the selected assembly first; certificate assembly indices refer to this order. internal IEnumerable Pins => [SelectedPin, .. references.Select(reference => reference.Pin)]; - internal PinnedAssemblies(byte[] selected, string selectedHash, IEnumerable sources) + /// Every pinned module, the selected assembly first, in the order of . + internal IEnumerable Modules => [Selected, .. references.Select(reference => reference.Module)]; + + /// Classes deriving from the slot's declarer across this fixed set of pinned images. + internal TypeDefinition[] DispatchHierarchy(TypeDefinition declaring) + { + if (!dispatchHierarchies.TryGetValue(declaring, out var classes)) + dispatchHierarchies.Add(declaring, classes = VirtualDispatch.DerivedClasses(Modules, declaring).ToArray()); + return classes; + } + + /// Pins and each source; reads a + /// referenced assembly's bytes and hash, so a batch of extractions reads each reference once. + internal PinnedAssemblies(byte[] selected, string selectedHash, IEnumerable sources, + Func? readReference = null) + : this(new Program.ExtractionBatch.AssemblySnapshot(selected, selectedHash), sources, readReference) { } + + /// A batch shares each immutable image's signature check across extraction requests. + internal PinnedAssemblies(Program.ExtractionBatch.AssemblySnapshot snapshot, IEnumerable sources, + Func? readReference) { + Signatures = new(this); previous = active; - // Only the runtime's trusted platform list may answer; no directory is searched. - foreach (string directory in platform.GetSearchDirectories()) platform.RemoveSearchDirectory(directory); - Selected = Read(selected); - SelectedPin = new(Selected.Assembly.Name.FullName, Selected.Mvid, selectedHash); - modules.Add(SelectedPin.Identity, Selected); - originals.Add(Selected, StaticData.ReadModule(selected, this)); - foreach (Source source in sources) + try { - byte[] bytes = File.ReadAllBytes(source.Path); - string hash = Convert.ToHexStringLower(SHA256.HashData(bytes)); - if (source.ExpectedSha256 is { } expected && !string.Equals(expected, hash, StringComparison.Ordinal)) - throw new InvalidDataException($"Referenced assembly hash mismatch: {CliPaths.Display(source.Path)}\n" + - $" Expected SHA-256: {expected}\n Actual SHA-256: {hash}"); - ModuleDefinition module = Read(bytes); - string identity = module.Assembly?.Name.FullName ?? - throw new InvalidDataException( - "Referenced module has no assembly metadata: " + CliPaths.Display(source.Path)); - if (Platform(module.Assembly.Name)) - throw new InvalidDataException("Platform assemblies are runtime models, not references: " + identity); - if (!modules.TryAdd(identity, module)) - throw new InvalidDataException("Duplicate pinned assembly identity: " + identity); - originals.Add(module, StaticData.ReadModule(bytes, this)); - references.Add((new(identity, module.Mvid, hash), Path.GetFullPath(source.Path), module)); + snapshot.ValidateSignatures(); + byte[] selected = snapshot.Bytes; + Selected = ReadPin(selected); + SelectedPin = new(Selected.Assembly.Name.FullName, Selected.Mvid, snapshot.Hash); + AssemblyName selectedIdentity = IdentityOf(Selected.Assembly.Name); + modules.Add(selectedIdentity, Selected); + byName.TryAdd(selectedIdentity.Name, Selected); + AddOriginal(Selected, selected); + foreach (Source source in sources) + { + var image = readReference?.Invoke(source.Path); + byte[] bytes = image?.Bytes ?? File.ReadAllBytes(source.Path); + string hash = image?.Hash ?? Convert.ToHexStringLower(SHA256.HashData(bytes)); + if (source.ExpectedSha256 is { } expected && !string.Equals(expected, hash, StringComparison.Ordinal)) + throw new InvalidDataException( + $"Referenced assembly hash mismatch: {CliPaths.Display(source.Path)}\n" + + $" Expected SHA-256: {expected}\n Actual SHA-256: {hash}"); + if (image is null) PlatformSignatures.Validate(bytes); + else image.ValidateSignatures(); + ModuleDefinition module = ReadPin(bytes); + string identity = module.Assembly.Name.FullName; + AssemblyName key = IdentityOf(module.Assembly.Name); + if (Platform(key)) + throw new InvalidDataException( + "Platform assemblies are runtime models, not references: " + identity); + if (!modules.TryAdd(key, module)) + throw new InvalidDataException("Duplicate pinned assembly identity: " + identity); + byName.TryAdd(key.Name, module); + AddOriginal(module, bytes); + references.Add((new(identity, module.Mvid, hash), Path.GetFullPath(source.Path), module)); + } + active = this; } - active = this; + catch { Dispose(); throw; } } - ModuleDefinition Read(byte[] bytes) => StaticData.ReadModule(bytes, this); + ModuleDefinition Read(byte[] bytes) + { + MemoryStream? stream = null; + ModuleDefinition? module = null; + try + { + module = StaticData.ReadModule(bytes, this, retained => stream = retained); + ownedModules.Add(module, stream!); + return module; + } + catch { module?.Dispose(); stream?.Dispose(); throw; } + } + + ModuleDefinition ReadPin(byte[] bytes) + { + ModuleDefinition module = Read(bytes); + images.Add(module, bytes); + RegisterRows(module, Metadata(module)); + return module; + } + + // The working module and its untouched copy are two readings of the same image. + void AddOriginal(ModuleDefinition module, byte[] image) + { + ModuleDefinition original = ReadPin(image); + originals.Add(module, original); + working.Add(original, module); + indices.Add(module, indices.Count); + } /// The pinned set of the current extraction, or none outside one. internal static PinnedAssemblies? Active => active; - static bool Platform(AssemblyNameReference name) => name.PublicKeyToken is { Length: 8 } token && - PlatformKeyTokens.Contains(Convert.ToHexStringLower(token)); + // Platform keys: only the extractor's trusted platform assemblies can answer these names. + static bool Platform(AssemblyName name) => name.PublicKeyToken is { } token && + PlatformAssemblies.KeyTokens.Contains(token); /// Whether is one of the pinned modules. - internal bool Contains(ModuleDefinition module) => - modules.TryGetValue(module.Assembly.Name.FullName, out var pinned) && pinned == module; + internal bool Contains(ModuleDefinition module) => indices.ContainsKey(module); /// The pinned module a type's scope names, if any. internal ModuleDefinition? ModuleOf(TypeReference type) => type.Scope switch { ModuleDefinition module when Contains(module) => module, - AssemblyNameReference name when modules.TryGetValue(name.FullName, out var module) => module, + AssemblyNameReference name when modules.TryGetValue(IdentityOf(name), out var module) => module, _ => null }; @@ -97,35 +194,126 @@ internal static bool Declares(TypeReference type, ModuleDefinition module) => /// The untouched copy of a pinned module; extraction rewrites call operands in the /// working one. An untouched copy maps to itself. internal ModuleDefinition Original(ModuleDefinition module) => originals.TryGetValue(module, out var original) - ? original : originals.ContainsValue(module) ? module : throw Outside(module); + ? original : working.ContainsKey(module) ? module : throw Outside(module); static InvalidDataException Outside(ModuleDefinition module) => new("Method body outside the pinned assemblies: " + module.Assembly.Name.FullName); + /// The header and code bytes of a method's body, read from the image of its module. + internal MethodBodyBytes.Body Body(MethodDefinition method) + { + if (!method.HasBody || method.RVA == 0) + throw new InvalidDataException("Method has no body in its image: " + method.FullName); + return MethodBodyBytes.Read(Image(method.Module), (int)method.RVA); + } + + PEReader Image(ModuleDefinition module) + { + if (!images.TryGetValue(module, out byte[]? image)) throw Outside(module); + if (!readers.TryGetValue(image, out PEReader? reader)) + readers.Add(image, reader = new(ImmutableCollectionsMarshal.AsImmutableArray(image))); + return reader; + } + + /// The metadata tables and heaps of a pinned module's image, read once per image. + internal MetadataImage Metadata(ModuleDefinition module) + { + var reader = Image(module); + if (!metadata.TryGetValue(reader, out MetadataImage? image)) metadata.Add(reader, image = new(reader)); + return image; + } + + /// Retains verified import metadata without admitting its bodies or resolution scope. + internal void RegisterIdentityImage(ModuleDefinition module, byte[] bytes) + { + var reader = new PEReader(ImmutableCollectionsMarshal.AsImmutableArray(bytes)); + MetadataImage? image = null; + bool registered = false; + try + { + image = new MetadataImage(reader); + RegisterRows(module, image); + registered = true; + identityImages.Add(module, (reader, image)); + } + catch + { + if (registered) UnregisterRows(module); + if (image is not null) rowsByImage.Remove(image); + reader.Dispose(); + throw; + } + } + + /// Releases an import's metadata when its owning resolution ends. + internal void UnregisterIdentityImage(ModuleDefinition module) + { + if (identityImages.Remove(module, out var image)) + { + UnregisterRows(module); + rowsByImage.Remove(image.Metadata); + image.Reader.Dispose(); + } + } + + /// The raw identity rows of an executable pin or a verified contract import. + internal MetadataImage IdentityMetadata(ModuleDefinition module) => + RegisteredMetadata(module); + + internal void RequireRegistered(ModuleDefinition module) + { + if (disposed || !registrations.ContainsKey(module)) + throw new InvalidDataException("Module identity is not registered in this session"); + } + + MetadataImage RegisteredMetadata(ModuleDefinition module) + { + RequireRegistered(module); + return identityImages.TryGetValue(module, out var image) ? image.Metadata : Metadata(module); + } + + /// + /// The working module that is the untouched copy of, or the module itself. + /// + internal ModuleDefinition Working(ModuleDefinition module) => + working.GetValueOrDefault(module, module); /// The index of a pinned module in . - internal int Index(ModuleDefinition module) => module == Selected ? 0 : - references.FindIndex(reference => reference.Module == module) is >= 0 and var index ? index + 1 : - throw Outside(module); + internal int Index(ModuleDefinition module) => indices.TryGetValue(module, out var index) + ? index : throw Outside(module); /// Rejects a reference whose simple name is pinned under another identity, so a version /// mismatch never falls back to the typed-import path. internal void RejectMismatch(AssemblyNameReference name) { - if (!modules.ContainsKey(name.FullName) && modules.Values.Any(pinned => pinned.Assembly.Name.Name == name.Name)) + AssemblyName identity = IdentityOf(name); + if (!modules.ContainsKey(identity) && byName.ContainsKey(identity.Name)) Resolve(name); } + /// Whether resolves to a platform assembly and no pinned assembly has its simple + /// name, which Lean reads from a carried slice or refuses, never as a platform type. + internal bool IsPlatform(AssemblyNameReference name) + { + AssemblyName identity = IdentityOf(name); + return Platform(identity) && !byName.ContainsKey(identity.Name); + } + /// Whether answers the reference rather than rejecting it. /// - internal bool Resolves(AssemblyNameReference name) => modules.ContainsKey(name.FullName) || Platform(name); + internal bool Resolves(AssemblyNameReference name) + { + AssemblyName identity = IdentityOf(name); + return modules.ContainsKey(identity) || Platform(identity); + } public AssemblyDefinition Resolve(AssemblyNameReference name) => Resolve(name, new ReaderParameters()); public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters) { - if (modules.TryGetValue(name.FullName, out var module)) return module.Assembly; - if (Platform(name)) return platform.Resolve(name, parameters); - var named = modules.Values.FirstOrDefault(candidate => candidate.Assembly.Name.Name == name.Name); + AssemblyName identity = IdentityOf(name); + if (modules.TryGetValue(identity, out var module)) return module.Assembly; + if (Platform(identity)) return PlatformAssembly(identity); + var named = byName.GetValueOrDefault(identity.Name); throw new InvalidDataException(named is null ? $"Unpinned assembly reference: {name.FullName}\n" + $" Pass --reference to extract from it." @@ -133,10 +321,145 @@ public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters p $" Pinned: {named.Assembly.Name.FullName}"); } + // Keyed by the requested identity and by the identity of the assembly read, since the runtime can answer a + // reference of another version with an assembly already read. + AssemblyDefinition PlatformAssembly(AssemblyName requested) + { + if (platformAssemblies.TryGetValue(requested, out AssemblyDefinition? assembly)) return assembly; + if (platformByName.TryGetValue(requested.Name, out assembly)) + { + RequirePlatformIdentity(requested, IdentityOf(assembly.Name)); + platformAssemblies.Add(requested, assembly); + return assembly; + } + if (!platformPaths.TryGetValue(requested.Name, out string? path)) + throw new InvalidDataException("No exact trusted platform assembly: " + requested.ToDisplay()); + byte[] bytes = File.ReadAllBytes(path); + ModuleDefinition module = Read(bytes); + try + { + RegisterIdentityImage(module, bytes); + AssemblyName loaded = IdentityOf(module.Assembly.Name); + RequirePlatformIdentity(requested, loaded); + if (platformAssemblies.TryGetValue(loaded, out assembly)) ReleaseModule(module); + else platformAssemblies.Add(loaded, assembly = module.Assembly); + platformByName.Add(loaded.Name, assembly); + platformAssemblies.TryAdd(requested, assembly); + return assembly; + } + catch { ReleaseModule(module); throw; } + } + + static void RequirePlatformIdentity(AssemblyName requested, AssemblyName loaded) + { + if (loaded.Name != requested.Name || loaded.Culture != requested.Culture || + loaded.PublicKeyToken != requested.PublicKeyToken || !Platform(loaded)) + throw new InvalidDataException("Trusted platform assembly identity mismatch: " + requested.ToDisplay()); + } + + static Dictionary PlatformPaths() + { + var paths = new Dictionary(); + if (AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") is not string trusted) return paths; + foreach (string path in trusted.Split(Path.PathSeparator)) + if (string.Equals(Path.GetExtension(path), ".dll", StringComparison.OrdinalIgnoreCase)) + paths[MetadataName.OfText(Path.GetFileNameWithoutExtension(path))] = path; + return paths; + } + + AssemblyName IdentityOf(AssemblyNameReference name) => identityRows.TryGetValue(name, out var row) + ? row.Read() : throw new InvalidDataException("Assembly reference without registered image provenance"); + + void RegisterRows(ModuleDefinition module, MetadataImage image) + { + IdentityRows.RequireModule(image, module); + if (registrations.ContainsKey(module)) + throw new InvalidDataException("Module identity already registered"); + if (module.Assembly is null || module.MetadataKind != MetadataKind.Ecma335 || + module.AssemblyReferences.Count != image.Count(0x23)) + throw new InvalidDataException("Assembly references do not match image rows"); + AssemblyNameReference[] names = [module.Assembly.Name, .. module.AssemblyReferences]; + for (int index = 0; index < names.Length; index++) + { + uint token = index == 0 ? 0x20000001 : 0x23000000 + (uint)index; + if (names[index].MetadataToken.ToUInt32() != token || identityRows.ContainsKey(names[index])) + throw new InvalidDataException("Assembly reference without an original image row"); + } + bool knownImage = rowsByImage.TryGetValue(image, out var rows); + rows ??= []; + if (!rows.TryGetValue(0x20000001, out var definition)) + rows.Add(0x20000001, definition = new(image, 0x20000001)); + definition.Read(); + var primitiveEntries = Signatures.Prepare(module, image, names); + int added = 0; + bool published = false; + try + { + if (!knownImage) rowsByImage.Add(image, rows); + foreach (AssemblyNameReference name in names) + { + uint token = name.MetadataToken.ToUInt32(); + if (!rows.TryGetValue(token, out var row)) rows.Add(token, row = new(image, token)); + identityRows.Add(name, row); + added++; + } + Signatures.Publish(primitiveEntries); + published = true; + registrations.Add(module, new(names, primitiveEntries)); + } + catch + { + if (published) Signatures.Remove(primitiveEntries); + for (int index = 0; index < added; index++) identityRows.Remove(names[index]); + if (!knownImage) rowsByImage.Remove(image); + throw; + } + } + + void UnregisterRows(ModuleDefinition module) + { + if (registrations.Remove(module, out var registration)) + { + foreach (AssemblyNameReference name in registration.Names) identityRows.Remove(name); + Signatures.Remove(registration.Primitives); + } + } + + void ReleaseModule(ModuleDefinition module) + { + UnregisterIdentityImage(module); + if (!ownedModules.Remove(module, out var stream)) return; + module.Dispose(); + stream.Dispose(); + } + public void Dispose() { - active = previous; - platform.Dispose(); - foreach (ModuleDefinition module in modules.Values.Concat(originals.Values)) module.Dispose(); + if (disposed) return; + disposed = true; + if (active == this) active = previous; + foreach (PEReader reader in readers.Values) reader.Dispose(); + foreach (var image in identityImages.Values) image.Reader.Dispose(); + foreach (var (module, stream) in ownedModules) + { + module.Dispose(); + stream.Dispose(); + } + identityRows.Clear(); + Signatures.Dispose(); + rowsByImage.Clear(); + registrations.Clear(); + ownedModules.Clear(); + identityImages.Clear(); + modules.Clear(); + platformAssemblies.Clear(); + platformByName.Clear(); + images.Clear(); + readers.Clear(); + metadata.Clear(); + indices.Clear(); + originals.Clear(); + working.Clear(); + byName.Clear(); } } diff --git a/Extractor/PlatformDefinitions.cs b/Extractor/PlatformDefinitions.cs new file mode 100644 index 000000000..2dea7fb02 --- /dev/null +++ b/Extractor/PlatformDefinitions.cs @@ -0,0 +1,171 @@ +using System.Collections.Immutable; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using Mono.Cecil; +using ModuleDefinition = Mono.Cecil.ModuleDefinition; +using TypeDefinition = Mono.Cecil.TypeDefinition; +using TypeReference = Mono.Cecil.TypeReference; + +namespace Nethermind.LeanCIL; + +/// What Lean's metadata checks rely on about a platform type: CIL.Extraction.PlatformKind. +internal enum PlatformKind +{ + ClassType, + InterfaceType, + ValueType, + EnumType, + ObjectRoot, + ValueTypeRoot, + EnumRoot, + ValuePair, + ByteOrder, +} + +/// +/// One platform type as Lean reads its reference: the assembly the reference scopes it to, the namespace and name +/// of each enclosing type and of the type, outermost first, and its kind, with the element type code of an enum. +/// +internal sealed record PlatformDefinition( + AssemblyNameReference Scope, ImmutableArray Parts, PlatformKind Kind, int Underlying, int Arity) +{ + /// The Lean CIL.Extraction.PlatformDefinition, with byte lists written by + /// . + internal string Lean(Func, string> bytes) + { + var scope = bytes(Encoding.UTF8.GetBytes(Scope.FullName)); + var parts = string.Join(", ", Parts.Select(part => bytes(Encoding.UTF8.GetBytes(part)))); + return $$"""{ scope := {{scope}}, parts := [{{parts}}], kind := {{LeanKind}} }"""; + } + + string LeanKind => Kind switch + { + PlatformKind.ClassType when Arity != 0 => $".genericClass {Arity}", + PlatformKind.InterfaceType when Arity != 0 => $".genericInterface {Arity}", + PlatformKind.ValueType when Arity != 0 => $".genericValue {Arity}", + PlatformKind.ClassType => ".classType", + PlatformKind.InterfaceType => ".interfaceType", + PlatformKind.ValueType => ".valueType", + PlatformKind.EnumType when Arity != 0 => $".enumType {Underlying} {Arity}", + PlatformKind.EnumType => $".enumType {Underlying}", + PlatformKind.ObjectRoot => ".objectRoot", + PlatformKind.ValueTypeRoot => ".valueTypeRoot", + PlatformKind.EnumRoot => ".enumRoot", + PlatformKind.ValuePair => ".valuePair", + PlatformKind.ByteOrder => ".byteOrder", + _ => throw new ArgumentOutOfRangeException(nameof(Kind), Kind, null), + }; +} + +/// +/// The platform definitions an extraction states: one for each platform type a carried TypeRef row or a +/// stated type argument names, the only places Lean's checks read a platform type's scope and name parts from. +/// Lean refuses a type outside the carried slices whose definition is missing or of another kind, so a type left +/// out is a refusal, never an acceptance. The roots, the value pair and the byte-order class are those of +/// , the module that defines the run's System.Object. +/// +internal sealed class PlatformDefinitions(ModuleDefinition coreLibrary) +{ + // Compiling the table literal recurses about 16 levels per definition: 1,024 need more than 12,288. + const int RecursionDepthPerDefinition = 32; + const int MinimumRecursionDepth = 4096; + + // Keyed by the scope's identity and the parts as Lean compares them, so a type named by several modules is + // stated once. + readonly SortedDictionary definitions = new(StringComparer.Ordinal); + + /// The definitions, in key order. + internal IEnumerable Definitions => definitions.Values; + + /// The maxRecDepth under which Lean compiles a table of + /// definitions. + internal static int RecursionDepth(int count) => + Math.Max(MinimumRecursionDepth, RecursionDepthPerDefinition * count); + + /// The maxRecDepth under which Lean compiles these definitions. + internal int Depth => RecursionDepth(definitions.Count); + + /// Adds the definition of each type the of name + /// in an assembly that holds a platform assembly. A type of another assembly, or one + /// Cecil cannot resolve, gets none, so a check that relies on it refuses. + internal void Add(ModuleDefinition module, MetadataReader reader, IEnumerable references, + Func platform) + { + foreach (var handle in references) + if (Definition(module, reader, handle, platform) is { } definition) + Add(definition); + } + + /// Adds the definition of when it is a type of a platform assembly, named as a + /// stated type argument names it. + internal void Add(TypeReference type, Func platform) + { + if (type.Scope is not AssemblyNameReference assembly || !platform(assembly)) return; + var parts = NameParts(type); + if (type.Resolve() is not { } definition) return; + var kind = Kind(definition); + Add(new(assembly, [.. parts], kind, kind == PlatformKind.EnumType ? Underlying(definition) : 0, + definition.GenericParameters.Count)); + } + + /// The namespace and simple name of every enclosing type, outermost first. + internal static IEnumerable NameParts(TypeReference type) + { + var parts = new Stack(); + for (var level = type; level is not null; level = level.DeclaringType) + { + parts.Push(level.Name); + parts.Push(level.Namespace); + } + return parts; + } + + void Add(PlatformDefinition definition) => + definitions.TryAdd($"""{definition.Scope.FullName}{'\0'}{string.Join('\0', definition.Parts)}""", definition); + + /// The Lean list of the definitions, with byte lists written by . + internal string Lean(Func, string> bytes) => + $"""[{string.Join(",\n ", Definitions.Select(definition => definition.Lean(bytes)))}]"""; + + PlatformDefinition? Definition(ModuleDefinition module, MetadataReader reader, TypeReferenceHandle handle, + Func platform) + { + var parts = new Stack(); + EntityHandle scope = handle; + while (scope.Kind == HandleKind.TypeReference) + { + var row = reader.GetTypeReference((TypeReferenceHandle)scope); + parts.Push(reader.GetString(row.Name)); + parts.Push(reader.GetString(row.Namespace)); + scope = row.ResolutionScope; + } + if (scope.Kind != HandleKind.AssemblyReference) return null; + var assembly = module.AssemblyReferences[MetadataTokens.GetRowNumber(scope) - 1]; + if (!platform(assembly)) return null; + var reference = (TypeReference)module.LookupToken(MetadataTokens.GetToken(handle)); + if (reference.Resolve() is not { } type) return null; + var kind = Kind(type); + return new(assembly, [.. parts], kind, kind == PlatformKind.EnumType ? Underlying(type) : 0, + type.GenericParameters.Count); + } + + // The roots, the value pair and the byte-order class by their core-library definitions, any other type by what + // it is; the same names defined elsewhere are ordinary types. + PlatformKind Kind(TypeDefinition type) => (type.Module.Mvid == coreLibrary.Mvid ? type.FullName : null) switch + { + _ when type.IsInterface => PlatformKind.InterfaceType, + "System.Object" when !type.HasGenericParameters => PlatformKind.ObjectRoot, + "System.ValueType" when !type.HasGenericParameters => PlatformKind.ValueTypeRoot, + "System.Enum" when !type.HasGenericParameters => PlatformKind.EnumRoot, + "System.ValueTuple`2" when type.GenericParameters.Count == 2 => PlatformKind.ValuePair, + "System.BitConverter" when !type.HasGenericParameters => PlatformKind.ByteOrder, + _ when type.IsEnum => PlatformKind.EnumType, + _ when type.IsValueType => PlatformKind.ValueType, + _ => PlatformKind.ClassType, + }; + + // The element type code of an enum's instance field, as Lean reads it from the field's signature. + internal static int Underlying(TypeDefinition type) => + (int)type.Fields.First(field => !field.IsStatic).FieldType.MetadataType; +} diff --git a/Extractor/VectorSignatures.cs b/Extractor/PlatformSignatures.cs similarity index 74% rename from Extractor/VectorSignatures.cs rename to Extractor/PlatformSignatures.cs index 74efbfa0c..10a6f4ac2 100644 --- a/Extractor/VectorSignatures.cs +++ b/Extractor/PlatformSignatures.cs @@ -5,8 +5,10 @@ namespace Nethermind.LeanCIL; -internal sealed class VectorSignatures : ISignatureTypeProvider +/// Checks each platform type's class or value-type marker before Cecil caches its head. +internal sealed class PlatformSignatures(PEReader source) : ISignatureTypeProvider { + MetadataImage? image; readonly HashSet active = []; readonly HashSet checkedTypes = []; @@ -14,7 +16,7 @@ internal static void Validate(byte[] assembly) { using PEReader image = new(new MemoryStream(assembly)); MetadataReader reader = image.GetMetadataReader(); - VectorSignatures provider = new(); + PlatformSignatures provider = new(image); foreach (FieldDefinitionHandle handle in reader.FieldDefinitions) reader.GetFieldDefinition(handle).DecodeSignature(provider, null); foreach (MethodDefinitionHandle handle in reader.MethodDefinitions) @@ -36,7 +38,8 @@ internal static void Validate(byte[] assembly) for (int row = 1; row <= reader.GetTableRowCount(TableIndex.TypeSpec); row++) provider.GetTypeFromSpecification(reader, null, MetadataTokens.TypeSpecificationHandle(row), 0); for (int row = 1; row <= reader.GetTableRowCount(TableIndex.MethodSpec); row++) - reader.GetMethodSpecification(MetadataTokens.MethodSpecificationHandle(row)).DecodeSignature(provider, null); + reader.GetMethodSpecification(MetadataTokens.MethodSpecificationHandle(row)) + .DecodeSignature(provider, null); } static bool Check(MetadataReader reader, StringHandle scope, StringHandle name, byte kind) @@ -45,7 +48,8 @@ static bool Check(MetadataReader reader, StringHandle scope, StringHandle name, (reader.StringComparer.Equals(name, "Vector128`1") || reader.StringComparer.Equals(name, "Vector256`1") || reader.StringComparer.Equals(name, "Vector512`1")) && kind != (byte)SignatureTypeKind.ValueType) - throw new InvalidDataException("Unsupported vector type encoding: " + reader.GetString(name) + " must use a value-type signature."); + throw new InvalidDataException( + $"Unsupported vector type encoding: {reader.GetString(name)} must use a value-type signature."); return false; } @@ -58,13 +62,25 @@ public bool GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle ha public bool GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) { TypeReference type = reader.GetTypeReference(handle); + if (rawTypeKind != (byte)SignatureTypeKind.ValueType && + type.ResolutionScope.Kind == HandleKind.AssemblyReference && + reader.StringComparer.Equals(type.Name, "ValueTuple`2") && + reader.StringComparer.Equals(type.Namespace, "System")) + { + var metadata = image ??= new MetadataImage(source); + if (metadata.AssemblyIdentity((uint)MetadataTokens.GetToken(type.ResolutionScope)) == + PlatformAssemblies.SystemRuntime) + throw new InvalidDataException("The platform pair must use a value-type signature."); + } return Check(reader, type.Namespace, type.Name, rawTypeKind); } - public bool GetTypeFromSpecification(MetadataReader reader, object? genericContext, TypeSpecificationHandle handle, byte rawTypeKind) + public bool GetTypeFromSpecification(MetadataReader reader, object? genericContext, + TypeSpecificationHandle handle, byte rawTypeKind) { if (checkedTypes.Contains(handle)) return false; - if (active.Count >= 128 || !active.Add(handle)) throw new InvalidDataException("Cyclic or excessively nested vector type signature."); + if (active.Count >= 128 || !active.Add(handle)) + throw new InvalidDataException("Cyclic or excessively nested type signature."); reader.GetTypeSpecification(handle).DecodeSignature(this, genericContext); active.Remove(handle); checkedTypes.Add(handle); diff --git a/Extractor/Program.cs b/Extractor/Program.cs index 7968a3635..93a813ee8 100644 --- a/Extractor/Program.cs +++ b/Extractor/Program.cs @@ -29,6 +29,7 @@ internal static partial class Program " leancil new-proof extraction-directory proof.lean\n" + " leancil verify output-directory [--proof proof.lean] [--theorem name] [--fresh] [--rebuild-library] " + "[--proof-root package-directory] [--force-full-verify] [--full-artifact Lib=directory]... " + + "[--deep-checks digests.sha256] " + "[--format text|json] [--trace] [--goals]\n" + " leancil verify-batch requests.json [--fresh] [--rebuild-library] [--parallel count] " + "[--results results.json] [--format text|json] [--trace] [--check]\n" + @@ -46,12 +47,18 @@ internal static partial class Program internal static int CheckExtraction(string output) => Main(["check", output]); + /// The extracted methods, each defined from its decoder. + internal sealed record MethodDefinitions(MethodDefinition[] Methods); + /// Runs extract or check in this process without printing its report; - /// a rejection throws instead of returning an exit code. - internal static void RunQuietly(string[] args) + /// a rejection throws instead of returning an exit code. A batch shares the files read by earlier runs, and + /// `definitions` receives the extracted methods. + /// + internal static void RunQuietly(string[] args, ExtractionBatch? batch = null, + Action? definitions = null) { if (args is not ["extract" or "check", ..]) throw new ArgumentException(Usage); - Run(args, report: TextWriter.Null, rethrow: true); + Run(args, batch, report: TextWriter.Null, rethrow: true, definitions: definitions); } /// @@ -70,7 +77,8 @@ static int Main(string[] args) static int Run(string[] args, ExtractionBatch? batch = null, Action? checkedTemplate = null, TextWriter? report = null, bool rethrow = false, - Action? inspect = null, bool refresh = false) + Action? inspect = null, bool refresh = false, + Action? definitions = null) { report ??= Console.Out; if (args.Length == 0 || args is ["--help"] or ["-h"]) @@ -270,10 +278,10 @@ static int Run(string[] args, ExtractionBatch? batch = null, var assemblySnapshot = batch?.ReadAssembly(assemblyPath); byte[] assembly = assemblySnapshot?.Bytes ?? File.ReadAllBytes(assemblyPath); stage = "metadata and CIL validation"; - if (assemblySnapshot is null) VectorSignatures.Validate(assembly); - else assemblySnapshot.ValidateSignatures(); string Hash(byte[] bytes) => Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant(); var assemblyHash = assemblySnapshot?.Hash ?? Hash(assembly); + assemblySnapshot ??= new(assembly, assemblyHash); + assemblySnapshot.ValidateSignatures(); List referenceSources = []; JsonElement[] recordedReferences = recorded is { } pinnedGraph && pinnedGraph.TryGetProperty("references", out JsonElement pinnedRecords) @@ -286,7 +294,8 @@ static int Run(string[] args, ExtractionBatch? batch = null, global::Program.ExtractionIdentity.Resolve(output, pin.GetProperty("path").GetString()!), pin.GetProperty("sha256").GetString()!)); referenceSources.AddRange(referencePaths.Select(path => new PinnedAssemblies.Source(path))); - using PinnedAssemblies pinned = new(assembly, assemblyHash, referenceSources); + using PinnedAssemblies pinned = new(assemblySnapshot, referenceSources, + batch is null ? null : batch.ReadAssembly); ModuleDefinition module = pinned.Selected; foreach ((JsonElement record, var reference) in recordedReferences.Zip(pinned.References)) if (record.GetProperty("assemblyIdentity").GetString() != reference.Pin.Identity || @@ -294,7 +303,8 @@ static int Run(string[] args, ExtractionBatch? batch = null, throw new InvalidDataException("Stale or mismatched verification artifact: referenced assembly " + $"{CliPaths.Display(reference.Path)} is not the recorded " + $"{record.GetProperty("assemblyIdentity").GetString()}."); - var symbols = Symbols.Read(module, assemblyPath, out var symbolNotice); + var symbols = Symbols.Read(module, assemblyPath, out var symbolNotice, + batch is null ? null : batch.ReadPdb); var assemblyIdentity = module.Assembly?.Name.FullName ?? throw new InvalidDataException("Input must contain assembly metadata."); stage = "contract import validation"; List importSources = []; @@ -332,8 +342,10 @@ static int Run(string[] args, ExtractionBatch? batch = null, using ImportResolution imports = new(importSources); using var enumMetadata = EnumTypes.BeginScope(); stage = "metadata and CIL validation"; - MethodDefinition[] methods = MetadataValidation.Validate(module, expectedAssembly ?? assemblyIdentity, entry, profile, initializedTypes, imports: imports, - typeArguments: entryTypeArguments.ToArray(), importedByref: type => imports.ValidateByref(type, profile)); + var callSites = new ResolvedCallSites(); + MethodDefinition[] methods = MetadataValidation.Validate(module, expectedAssembly ?? assemblyIdentity, + entry, profile, initializedTypes, imports: imports, typeArguments: entryTypeArguments.ToArray(), + importedByref: type => imports.ValidateByref(type, profile), callSites: callSites); foreach (var node in imports.Nodes) if (pinned.References.Any(reference => reference.Pin.Identity == node.AssemblyIdentity)) throw new InvalidDataException( @@ -351,8 +363,9 @@ static int Run(string[] args, ExtractionBatch? batch = null, throw new InvalidDataException("Stale or mismatched verification artifact: extractor has changed since extraction."); } stage = "metadata and CIL validation"; - LeanEmitter.Emission emitted = LeanEmitter.Emit(methods, profile, pinned, imports, - staticIdentities, collectTemplates: checkedTemplate is not null || inspect is not null); + LeanEmitter.Emission emitted = LeanEmitter.Emit(methods, profile, pinned, callSites, imports, + staticIdentities, collectOutlines: inspect is not null, + templateInputs: checkedTemplate is not null); string source = emitted.Source; var loadedValues = methods.SelectMany(m => Reachability.Analyze(m, profile)).Where(StaticValues.IsLoad) .Select(i => StaticValues.Resolve((FieldReference)i.Operand, module, profile.Architecture)).ToArray(); @@ -361,8 +374,12 @@ static int Run(string[] args, ExtractionBatch? batch = null, var opaqueStaticValues = new SortedSet(loadedValues.Where(value => value.Opaque) .Select(value => value.Field.FullName), StringComparer.Ordinal); if (initializedTypes.Count > 0) - source += "\n-- Caller assumption: these types completed initialization successfully before invocation.\n" + - "def Extracted.initializedTypes : List String := " + JsonSerializer.Serialize(initializedTypes) + "\n"; + source += $""" + + -- Caller assumption: these types completed initialization successfully before invocation. + def Extracted.initializedTypes : List String := {IdentityText.QuoteList(initializedTypes)} + + """; var initializationAssumptions = InitializationAssumption.Normalize(initializedTypes.Select(type => new InitializationAssumption(assemblyIdentity, type)).Concat(imports.InitializationAssumptions())); if (initializationAssumptions.Length > 0) @@ -370,12 +387,17 @@ static int Run(string[] args, ExtractionBatch? batch = null, "def Extracted.initializationAssumptions : List (String × String) := " + InitializationAssumption.Lean(initializationAssumptions) + "\n"; if (staticValues.Count > 0) - source += "-- Caller assumption: these fields keep the values their static constructors stored.\n" + - "def Extracted.staticValues : List String := " + JsonSerializer.Serialize(staticValues) + "\n"; + source += $""" + -- Caller assumption: these fields keep the values their static constructors stored. + def Extracted.staticValues : List String := {IdentityText.QuoteList(staticValues)} + + """; if (opaqueStaticValues.Count > 0) - source += "-- Not computed by extraction: loads read the caller's static binding.\n" + - "def Extracted.opaqueStaticValues : List String := " + - JsonSerializer.Serialize(opaqueStaticValues) + "\n"; + source += $""" + -- Not computed by extraction: loads read the caller's static binding. + def Extracted.opaqueStaticValues : List String := {IdentityText.QuoteList(opaqueStaticValues)} + + """; SortedDictionary importModules = imports.InterfaceModules(); if (imports.Bindings.Count > 0) importModules.Add("ExtractedImports", imports.EmitImports(methods.Length)); SortedDictionary importModuleHashes = new(importModules.ToDictionary(pair => pair.Key, @@ -412,7 +434,11 @@ static int Run(string[] args, ExtractionBatch? batch = null, staticIdentities = staticIdentities.Select(identity => JsonSerializer.SerializeToElement(identity, ContractInterface.Json)), generatedSha256 = Hash(Encoding.UTF8.GetBytes(source)), extractorSha256 = extractorHash, - methods = methods.Select(m => new { signature = m.FullName, token = m.MetadataToken.ToInt32(), + // Where each body sits in the image is recorded here, outside the Lean evidence, for the strict audit. + // A method's assembly is its pin index: 0 the selected assembly, k its k-th reference. + methods = methods.Select(m => new { signature = m.FullName, assembly = pinned.Index(m.Module), + token = m.MetadataToken.ToInt32(), + bodyAddress = ((MethodDefinition)pinned.Original(m.Module).LookupToken(m.MetadataToken)).RVA, specialization = MethodSpecialization.Source(m), locals = m.Body.Variables.Select(v => v.VariableType.FullName), instructions = m.Body.Instructions.Select(i => i.ToString()) }), @@ -474,16 +500,23 @@ static int Run(string[] args, ExtractionBatch? batch = null, foreach (var name in opaqueStaticValues) report.WriteLine($" Unknown static value: {name} is not computed; " + "loads read the caller's static binding."); - checkedTemplate?.Invoke(new(output, assemblyHash, profile, emitted.Templates)); + if (checkedTemplate is not null) + { + var frames = global::Program.TemplateVectorFrames(source, emitted.Templates); + checkedTemplate(new(output, assemblyHash, profile, frames) { DecoderInputs = emitted.TemplateInputs }); + } + definitions?.Invoke(new(methods)); if (args[0] == "check" && !refresh) - inspect?.Invoke(new(methods, emitted.Templates, emitted.LayoutIdentities, source, emitted.Views, - profile.Architecture)); + inspect?.Invoke(new(methods, emitted.Outlines, emitted.Views, + emitted.ClassFields.ToDictionary(access => (access.Method, access.Pc), access => access.Operation), + profile.Architecture, emitted.Layouts)); } catch (Exception exception) { if (rethrow) throw; if (exception is CliFailure { Reported: true }) return 1; - CliDiagnostics.Write(exception, stage, args[0] is "check" or "verify" or "audit-library"); + CliDiagnostics.Write(exception, stage, + args[0] is "check" or "verify" or "audit-library"); return 1; } return 0; diff --git a/Extractor/ProgramLookupFacts.cs b/Extractor/ProgramLookupFacts.cs new file mode 100644 index 000000000..806f27d27 --- /dev/null +++ b/Extractor/ProgramLookupFacts.cs @@ -0,0 +1,25 @@ +using System.Text; + +namespace Nethermind.LeanCIL; + +/// The method equations of a program and their shared axiom report. +internal static class ProgramLookupFacts +{ + internal static void Append(StringBuilder source, string program, string methodPrefix, int count) + { + var facts = new List { $"{program}Length_eq", $"{program}Index_eq", $"{program}Index_get?" }; + for (int index = 0; index < count; index++) + { + var fact = $"{program}_method{index}"; + source.AppendLine($$""" + @[cil_code] theorem {{fact}} : {{program}}[{{index}}]? = some {{methodPrefix}}{{index}} := + ({{program}}Index_get? {{index}}).symm.trans (by cil_kernel_rfl) + """); + facts.Add(fact); + } + source.AppendLine($$""" + #print axioms [ + {{string.Join(",\n ", facts)}}] + """); + } +} diff --git a/Extractor/ProofBindingCatalog.cs b/Extractor/ProofBindingCatalog.cs index 0873fd396..a8e7d1071 100644 --- a/Extractor/ProofBindingCatalog.cs +++ b/Extractor/ProofBindingCatalog.cs @@ -315,7 +315,7 @@ static string Instantiate(string template, IReadOnlyDictionary v static string Substitute(string text, IReadOnlyDictionary values, ISet? used = null) { var missing = new SortedSet(StringComparer.Ordinal); - var result = TemplatePlaceholder().Replace(text, match => + var result = TemplatePlaceholder.Replace(text, match => { var name = match.Groups[1].Value; if (!values.TryGetValue(name, out var value)) @@ -333,10 +333,10 @@ static string Substitute(string text, IReadOnlyDictionary values /// The names of the placeholders in `text`. static IEnumerable TemplatePlaceholders(string text) => - TemplatePlaceholder().Matches(text).Select(match => match.Groups[1].Value); + TemplatePlaceholder.Matches(text).Select(match => match.Groups[1].Value); [GeneratedRegex(@"@([A-Za-z_][A-Za-z0-9_]*)@")] - private static partial Regex TemplatePlaceholder(); + private static partial Regex TemplatePlaceholder { get; } } namespace LeanCIL diff --git a/Extractor/ProofGates.cs b/Extractor/ProofGates.cs index 6d91c8c03..42ec1a080 100644 --- a/Extractor/ProofGates.cs +++ b/Extractor/ProofGates.cs @@ -65,7 +65,8 @@ sealed record GateResults(string manifest, string? revision, GateResult[] rows); /// /// How a variant's verified statement relates to production's `T`, both as the theorem audits print them: - /// `same` when the kernel accepts the variant's theorem at `T`, or its refutation at `Not T`; `different` when + /// `same` when the kernel accepts the variant's theorem at `T`, or its refutation at the counterexample predicate + /// of the contract whose `Holds` `T` states, with `T`'s arguments; `different` when /// it does not; `unavailable` when there was nothing to compare; `independent` when the manifest says so. /// sealed record GateStatement(string status, string? production, string? variant); @@ -150,8 +151,9 @@ sealed class GateOptions /// /// Builds, extracts and verifies an explicit catalog of gates and their variants, plans the selected rows /// into balanced jobs, runs one job of a plan, or checks that the results of every job passed. A `rejected` - /// variant passes only when its refutation verifies at the negation of production's statement and its proof - /// fails at proof checking; a certifying variant's theorem must verify at production's statement. + /// variant passes only when production's statement is a contract's `Holds`, its refutation verifies at that + /// contract's counterexample predicate with the same arguments, and its proof fails at proof checking; a + /// certifying variant's theorem must verify at production's statement. /// internal static int RunGates(string[] args) { @@ -558,7 +560,8 @@ void Request(GateRow row) requests.Add(new(row.Output, row.RefutationProof, row.RefutationTheorem!, "verified")); } requestIndex[row] = (refutation, requests.Count); - requests.Add(new(row.Output, row.Proof, row.Theorem, row.Expect)); + // A rejected variant's own proof must fail at proof checking; its refutation is the request above. + requests.Add(new(row.Output, row.Proof, row.Theorem, row.Expect == "rejected" ? "unproved" : row.Expect)); } foreach (var row in selected.Except(compared).Concat(origins.Distinct()) .Where(row => !row.Failed && row.Expect != "unchanged")) @@ -577,8 +580,9 @@ void Request(GateRow row) var origin = requestIndex.TryGetValue(production[(row.Gate, row.Profile)], out var at) ? first.Verified[at.Proof]?.statementIdentity?.term : null; var index = requestIndex[row]; - // Without production's statement the variant still verifies, and its statement is unavailable. - if (origin is { } term) + // Without production's statement the variant still verifies, and its statement is unavailable; a raw + // statement has no counterexample predicate, so a refutation of it is never compared. + if (origin is { } term && (row.Expect != "rejected" || StatesContract(term))) statements[row.Expect == "rejected" ? index.Refutation!.Value : index.Proof] = new(false, term.GetRawText(), row.Expect == "rejected"); } @@ -600,7 +604,8 @@ [.. first.Reports.Concat(second.Reports).Select(node => node?.DeepClone())], return (new("independent", null, own is null ? null : StatementOf(own)), null); var origin = production[(row.Gate, row.Profile)]; var verified = requestIndex.TryGetValue(origin, out var at); - return CompareGateStatement(row, origin, origin.Stage ?? (verified ? run.Results[at.Proof].stage : "build"), + return CompareStatement(row.Expect == "rejected", own?.theorem ?? row.Theorem, + $"Production {origin.Name}", origin.Stage ?? (verified ? run.Results[at.Proof].stage : "build"), verified ? Verified(at.Proof) : null, own); } var results = selected.Select(row => GateOutcome(row, options.CheckOnly, @@ -1030,19 +1035,23 @@ static void CompareGateProgram(GateRow row, GateRow origin) /// /// How the program extracted into `variant` relates to the one in `production`: `same` when the method - /// bodies (each `def methodN : Method`, in order) and method signatures are equal, `renamed` when only the - /// signatures differ, otherwise `different`. Everything else in Extracted.lean is ignored, including the - /// assembly identity, digests, paths and metadata tokens. + /// bodies (each `def methodN : Method`, or for a method defined from its decoder its raw definition, in order) + /// and method signatures are equal, `renamed` when only the signatures differ, otherwise `different`. + /// Everything else in Extracted.lean is ignored, including the assembly identity, digests, paths and metadata + /// tokens. /// static string ProgramRelation(string production, string variant) { static (string[] Bodies, string[] Signatures) Methods(string output) { var text = File.ReadAllText(Path.Combine(output, "Extracted.lean")).ReplaceLineEndings("\n"); - string[] bodies = [.. MethodBody().Matches(text).Select(match => match.Value)]; + var originals = RawDefinition.Matches(text).ToDictionary(match => match.Groups["number"].Value, + match => RawIdentity.Replace(match.Groups["value"].Value, "")); + string[] bodies = [.. MethodBody.Matches(text).Select(match => + match.Groups["decoded"].Success ? originals[match.Groups["decoded"].Value] : match.Value)]; if (bodies.Length == 0) throw new InvalidDataException("No method bodies in " + CliPaths.Display(output)); - return (bodies, [.. MethodSignature().Matches(text).Select(match => match.Value)]); + return (bodies, [.. MethodSignature.Matches(text).Select(match => match.Value)]); } var (bodies, signatures) = Methods(production); var (variantBodies, variantSignatures) = Methods(variant); @@ -1050,11 +1059,21 @@ static string ProgramRelation(string production, string variant) : signatures.SequenceEqual(variantSignatures) ? "same" : "renamed"; } - [GeneratedRegex(@"^def method\d+ : Method := .*?(?=^\S)", RegexOptions.Multiline | RegexOptions.Singleline)] - private static partial Regex MethodBody(); + [GeneratedRegex(@"^def method\d+ : Method := .*?(?=^\S)|" + + @"^cil_decoded_method method(?\d+) (?:\[[^\]\n]*\] )?:= [^\n]*", + RegexOptions.Multiline | RegexOptions.Singleline)] + private static partial Regex MethodBody { get; } + + [GeneratedRegex(@"^def method(?\d+)Original : Extraction\.RawMethod := (?[^\n]*)$", + RegexOptions.Multiline)] + private static partial Regex RawDefinition { get; } + + // The token and owner of a raw definition name its assembly, which the program relation ignores. + [GeneratedRegex(@"\btoken := \d+, owner := ""[^""]*"", ")] + private static partial Regex RawIdentity { get; } [GeneratedRegex(@"^def method\d+Signature : String := .*$", RegexOptions.Multiline)] - private static partial Regex MethodSignature(); + private static partial Regex MethodSignature { get; } /// /// The result of `row`: its verdict, its proof's outcome, its refutation's when it has one, and how its @@ -1076,8 +1095,9 @@ static GateResult GateOutcome(GateRow row, bool checkOnly, (int? Refutation, int var refutation = requested.Refutation is { } at ? results[at] : null; var passed = proof.status != "failed" && refutation?.status is null or "verified"; var mismatch = passed && statement.Error is not null; - return new(row.Name, proof.output, proof.proof, proof.theorem, proof.expect, - passed && !mismatch ? proof.status : "failed", mismatch ? StatementIdentityStage : proof.stage, + var expect = checkOnly ? "checked" : row.Expect; + return new(row.Name, proof.output, proof.proof, proof.theorem, expect, + passed && !mismatch ? expect : "failed", mismatch ? StatementIdentityStage : proof.stage, mismatch ? statement.Error : proof.error, proof.axioms, proof.libraryBuild, proof.failure, proof.elapsedSeconds, row.Gate, row.Profile, row.Variant, row.BuildStatus, row.ExtractionStatus, row.ProgramStatus, refutation is null ? null : new(refutation.proof, refutation.theorem, refutation.status, @@ -1085,33 +1105,70 @@ static GateResult GateOutcome(GateRow row, bool checkOnly, (int? Refutation, int } /// - /// How the statement `row` verified relates to production's statement `T`: the kernel must accept a `rejected` - /// variant's refutation at `¬ T` and any other variant's theorem at `T`, in the variant's own environment. No - /// statement is compared when the variant's own leg did not verify; that leg reports why. + /// How the statement of `report`, a verified theorem, relates to the statement `T` of `originReport`, the + /// theorem `origin` names: `T` must state a contract's `Holds` when the theorem `refutes` it, and the kernel + /// must accept the theorem at that contract's counterexample predicate with `T`'s arguments; otherwise the + /// kernel must accept it at `T`, in the theorem's own environment. No statement is compared when the theorem + /// did not verify; its own failure reports why. /// - static (GateStatement?, string?) CompareGateStatement(GateRow row, GateRow production, string productionStage, - Report? productionReport, Report? variantReport) + static (GateStatement?, string?) CompareStatement(bool refutes, string theorem, string origin, string originStage, + Report? originReport, Report? report) { - if (variantReport is null) return (null, null); - var refutes = row.Expect == "rejected"; - var shown = StatementOf(variantReport); - var expected = productionReport?.statementIdentity; - var identity = variantReport.statementIdentity; - var reason = productionReport is null - ? $"Production {production.Name} did not verify (stage: {productionStage}), so the statement of " + - $"{row.Name} has nothing to match." + if (report is null) return (null, null); + var shown = StatementOf(report); + var expected = originReport?.statementIdentity; + var identity = report.statementIdentity; + const string requirement = "an expected rejection needs the refuted theorem to state"; + const string predicates = "MethodContract.Holds, OutcomeContract.Holds or OutcomeContract.HoldsCLR"; + var reason = originReport is null + ? $"""{origin} did not verify (stage: {originStage}), so the statement of {theorem + } has nothing to match.""" : expected?.term is null - ? $"Production {production.Name} has no statement to compare: {expected?.termError ?? "none reported"}" - : identity?.matches is null ? $"{variantReport.theorem} was not compared with production's statement." + ? $"""{origin} has no statement to compare: {expected?.termError ?? "none reported"}""" + : refutes && !StatesContract(expected.term.Value) + ? $"""{origin} does not state a contract's Holds, so {theorem} has no refutation to state: { + requirement} {predicates}""" + : identity?.matches is null ? $"""{theorem} was not compared with the statement of {origin}.""" : identity.matches == true ? null - : (refutes ? $"Refutation {variantReport.theorem} does not state the negation of production's statement" - : $"{variantReport.theorem} does not state production's statement") + ": " + identity.mismatch; - var productionText = productionReport is null ? null : StatementOf(productionReport); + : refutes && report.refutation is null + ? $"""{theorem} states no counterexample predicate, so it refutes nothing: {identity.mismatch}""" + : refutes + ? $"""Refutation {theorem} does not state the counterexample predicate of the contract of { + origin}, so it refutes another contract: {identity.mismatch}""" + : $"""{theorem} does not state the statement of {origin}: {identity.mismatch}"""; + var originText = originReport is null ? null : StatementOf(originReport); var status = reason is null ? "same" : identity?.matches == false ? "different" : "unavailable"; - return (new(status, productionText, shown), reason is null ? null : reason + "\n Production: " + - (productionText ?? "unavailable") + "\n " + (refutes ? "Refutation: " : "Variant: ") + shown); + var label = refutes ? "Refutation" : "Variant"; + var error = reason is null ? null + : $"""{reason}{'\n'} {origin}: {originText ?? "unavailable"}{'\n'} {label}: {shown}"""; + return (new(status, originText, shown), error); } + /// + /// The library contract predicates, by declaring module and name, that a `rejected` variant refutes. + /// + static readonly (string Module, string Name)[] ContractPredicates = + [ + ("CIL.Safety.MethodContract", "CIL.Safety.MethodContract.Holds"), + ("CIL.Safety.OutcomeContract", "CIL.Safety.OutcomeContract.Holds"), + ("CIL.Safety.RuntimeExceptions", "CIL.Safety.OutcomeContract.HoldsCLR") + ]; + + /// + /// Whether the audited statement applies a library contract predicate. The walk is linear in the + /// statement's node count. + /// + internal static bool StatesContract(JsonElement term) + { + JsonElement[] nodes = [.. term.GetProperty("nodes").EnumerateArray()]; + var node = nodes[term.GetProperty("root").GetInt32()]; + while (node[0].GetString() == "app") node = nodes[node[1].GetInt32()]; + return node[0].GetString() == "const" && ContractPredicates.Contains((Dotted(node[1]), Dotted(node[2]))); + } + + /// A name the audit encodes as its components, written with dots. + static string Dotted(JsonElement name) => string.Join('.', name.EnumerateArray().Select(part => part.ToString())); + static bool SafeGateName(string? name) => name is not (null or "" or "." or "..") && Regex.IsMatch(name, @"\A[A-Za-z0-9_.-]+\z"); diff --git a/Extractor/ProofOutline.cs b/Extractor/ProofOutline.cs index e89acffa0..991a86a5c 100644 --- a/Extractor/ProofOutline.cs +++ b/Extractor/ProofOutline.cs @@ -1,6 +1,8 @@ using System.Runtime.ExceptionServices; +using ILOpCode = System.Reflection.Metadata.ILOpCode; using System.Text; using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -9,11 +11,129 @@ namespace Nethermind.LeanCIL; // the extraction's keyed module, which resolves both in the editor workspace and under verify. internal static partial class ProofOutline { + /// The frame and instruction descriptors used to outline a proof of this method. + internal sealed class Method(FrameMetadata frame, Operation[] operations, MethodDefinition source, + IReadOnlySet reachable, IReadOnlyDictionary parameterHomes, + FeatureProfile profile, MetadataValidation.LayoutContext layouts, InstructionIndex instructions, + ResolvedCallSites calls, IReadOnlyDictionary methodIndices, ImportResolution? imports) + { + internal FrameMetadata Frame { get; } = frame; + internal Operation[] Operations { get; } = operations; + + /// The argument read directly at a reachable site. Addressed and assigned arguments use a frame slot. + internal int? ArgumentAt(int pc) + { + var site = source.Body.Instructions[pc]; + return Operations[pc] is Unoutlined && reachable.Contains(site) && + MethodArguments.LoadIndex(source, site) is int index && !parameterHomes.ContainsKey(index) + ? index : null; + } + + /// The callee selected by the original reachable call and its binding. + internal (CallKind Kind, int Method, int Arguments)? CallAt(int pc) + { + var site = source.Body.Instructions[pc]; + return Operations[pc] is Unoutlined && reachable.Contains(site) + ? calls.OutlineTarget(site, methodIndices, imports) : null; + } + + /// The normalized opcode and instruction index of the branch's original destination. + internal (ILOpCode Kind, int Target)? BranchAt(int pc) + { + var site = source.Body.Instructions[pc]; + if (Operations[pc] is not Unoutlined || site.Operand is not Instruction target || + site.OpCode.OperandType is not (OperandType.InlineBrTarget or OperandType.ShortInlineBrTarget) || + site.OpCode.Code is Code.Leave or Code.Leave_S || !reachable.Contains(site)) return null; + int opcode = site.OpCode.Value; + // ECMA-335 assigns both branch families in the same order. + if (opcode is >= (int)ILOpCode.Br_s and <= (int)ILOpCode.Blt_un_s) + opcode += (int)ILOpCode.Br - (int)ILOpCode.Br_s; + return ((ILOpCode)opcode, instructions.Position(target)); + } + + /// The storage shape of a reachable CIL type operand with the requested opcode. + internal (int? Bytes, int? ScalarWidth, bool Signed)? ObjectAt(int pc, Code opcode) + { + var site = source.Body.Instructions[pc]; + if (site.OpCode.Code != opcode || Operations[pc] is not Unoutlined || + !reachable.Contains(site) || site.Operand is not TypeReference operand) return null; + return InstructionOperands.ObjectStorage(source, site, operand, instructions, layouts, profile); + } + + /// The word pushed by a literal, a modelled call or the size of a closed type. + internal (int Width, long Value, bool Numeral)? ConstantAt(int pc) + { + var site = source.Body.Instructions[pc]; + if (Operations[pc] is not Unoutlined || !reachable.Contains(site)) return null; + var code = site.OpCode.Code; + return code switch + { + Code.Ldc_I4_M1 => (32, -1, false), + >= Code.Ldc_I4_0 and <= Code.Ldc_I4_8 => (32, code - Code.Ldc_I4_0, true), + Code.Ldc_I4 or Code.Ldc_I4_S => (32, Convert.ToInt64(site.Operand), false), + Code.Ldc_I8 => (64, (long)site.Operand, false), + Code.Sizeof when ObjectAt(pc, code)?.Bytes is int size => (32, size, true), + Code.Ldsfld when site.Operand is FieldReference field && + FeatureProfile.PlatformField(field) is int value => (32, value, true), + Code.Call when calls.IsModel(site) && site.Operand is MethodReference member && + (FeatureProfile.PlatformConstant(member) ?? + ReferenceQueries.Constant(member, profile.Architecture) ?? + UnsafeModels.SizeOf(member, profile.Architecture)) is int value => (32, value, true), + _ => null + }; + } + + /// The result width of a reachable bit-count call bound to its runtime declaration. + internal int? BitCountWidthAt(int pc) + { + var site = source.Body.Instructions[pc]; + return Operations[pc] is Unoutlined && site.OpCode.Code == Code.Call && + site.Operand is MethodReference member && reachable.Contains(site) && calls.IsModel(site) + ? BitModels.CountWidth(member, profile) : null; + } + + /// Whether one checked, straight-line word step suffices for this site. + internal bool PureAt(int pc, bool plainReturn) => Operations[pc] switch + { + StackOperation { Kind: Code.Ret } => plainReturn, + Unoutlined when ArgumentAt(pc) is int argument => MethodArguments.Plain(source, argument), + var operation => ConstantAt(pc) is { Width: 32 or 64 } || + BitCountWidthAt(pc) is not null || operation is StackOperation { Kind: + Code.Add or Code.Sub or Code.Mul or Code.And or Code.Or or Code.Xor or Code.Nop or + Code.Conv_I8 or Code.Conv_I or Code.Conv_U8 or Code.Conv_U4 or Code.Conv_I4 } + }; + + /// The admitted struct field at this instruction, using the extraction's layout context. + internal (Code Opcode, int Offset, int Width, bool Signed, bool IsAggregate)? StructFieldAt(int pc) + { + var site = source.Body.Instructions[pc]; + if (site.OpCode.Code is not (Code.Ldfld or Code.Ldflda or Code.Stfld) || + !reachable.Contains(site) || site.Operand is not FieldReference field || + ObjectTypes.Candidate(field.DeclaringType)) return null; + var definition = field.Resolve() ?? throw new InvalidDataException("Unresolved field"); + return InstructionOperands.StructField(field, definition, site.OpCode.Code, source, profile, layouts); + } + } + /// A checked extraction: its methods while still loaded, their emitted operations and - /// the layout identities in emission order, the checked `Extracted.lean` source, its named class fields, and + /// its computed layouts in emission order, each with its identity, its named class fields, and /// the architecture that sizes its struct array elements. - internal sealed record Extraction(MethodDefinition[] Methods, TemplateEmitter.Method[] Emitted, - string[] LayoutIdentities, string Source, ClassViews.View[] Views, string Architecture); + internal sealed record Extraction(MethodDefinition[] Methods, Method[] Emitted, + ClassViews.View[] Views, IReadOnlyDictionary<(int Method, int Pc), ClassViews.FieldOperation> ClassFields, + string Architecture, + ComputedLayout[] Layouts); + + static readonly System.Runtime.CompilerServices.ConditionalWeakTable> LayoutIndices = new(); + + /// The index of the first computed layout with this identity, or -1 when none has it. + internal static int LayoutIndex(Extraction extraction, string identity) => + LayoutIndices.GetValue(extraction.Layouts, static layouts => + { + var indices = new Dictionary(StringComparer.Ordinal); + for (var index = 0; index < layouts.Length; index++) indices.TryAdd(layouts[index].Identity, index); + return indices; + }).GetValueOrDefault(identity, -1); const string Usage = "Usage: leancil new-proof extraction-directory proof.lean"; diff --git a/Extractor/ProofOutlineArrays.cs b/Extractor/ProofOutlineArrays.cs index 443039d61..501dc3c72 100644 --- a/Extractor/ProofOutlineArrays.cs +++ b/Extractor/ProofOutlineArrays.cs @@ -1,6 +1,6 @@ using System.Globalization; -using System.Text.RegularExpressions; using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -11,47 +11,58 @@ internal static partial class ProofOutline { /// The array read and the index argument of a method returning `values[index]`, and the helper /// returning `ref values[index]` it calls instead of `ldelem`, if any. - sealed record ArrayAccess(Parameter Array, Parameter Index, ElementHelper? Helper); + sealed record ArrayAccess(Parameter Array, Parameter Index, ElementHelper? Helper, Arrays.NumericAccess? Load); /// A static method `(T[] values, int index)` whose body only returns `ref values[index]`. sealed record ElementHelper(int Index, MethodDefinition Method, string Name); /// The access of a body that loads an array argument and an `int` argument, reads that element and /// returns it, or null for any other body. - static ArrayAccess? ArrayRead(Extraction extraction, Specification spec, string[] all) + static ArrayAccess? ArrayRead(Extraction extraction, Specification spec, Operation[] all) { - var ops = all.Where(op => op != ".nop").ToArray(); - if (spec.Method.HasThis || spec.Result is not { Boolean: false, Object: false } || ops.Length < 4 || - Argument(ops[0]) is not { Shape: ParameterShape.WordArray } array || - Argument(ops[1]) is not { Shape: ParameterShape.Scalar, Width: 4 } index || - spec.Result.Lean != WordType(array.Width) || ops[^1] != ".ret") + // Instructions after the return cannot affect a straight-line read. + var end = Array.FindIndex(all, op => op is StackOperation { Kind: Code.Ret }); + var ops = all.Take(end + 1).Index().Where(site => !site.Item.IsNop).ToArray(); + if (LeanEmitter.Narrow(spec.Method.ReturnType) is not null || spec.Method.HasThis || + spec.Result is not { Boolean: false, Object: false } || ops.Length < 4 || + Argument(ops[0].Index) is not { Shape: ParameterShape.WordArray } array || + Argument(ops[1].Index) is not { Shape: ParameterShape.Scalar, Width: 4 } index || + spec.Result.Lean != WordType(array.Width) || ops[^1].Item is not StackOperation { Kind: Code.Ret }) return null; - if (ops.Length == 4 && ops[2] == $".memory (.arrayLoad {array.Width} false)") return new(array, index, null); + if (ops.Length == 4 && + Arrays.Numeric(ops[2].Item, spec.Method, ops[2].Index) is { Store: false, Signed: false } load && + load.Width == array.Width) + return new(array, index, null, load); // A helper's address is loaded with `ldind.i4`, the only indirect load whose step the outline gives. - if (ops.Length != 5 || array.Width != 4 || ops[3] != ".loadScalar 4" || - Regex.Match(ops[2], @"\A\.call (\d+) 2\z") is not { Success: true } call) + if (ops.Length != 5 || array.Width != 4 || + ops[3].Item is not StackOperation { Kind: Code.Ldind_I4 } || + extraction.Emitted[0].CallAt(ops[2].Index) is not { Kind: CallKind.Static, Arguments: 2 } call) return null; - var callee = int.Parse(call.Groups[1].Value, CultureInfo.InvariantCulture); - return ElementAddress(extraction, callee, array.ArrayElement) is { } method - ? new(array, index, new(callee, method, "")) : null; + return ElementAddress(extraction, call.Method, array.Element!) is { } method + ? new(array, index, new(call.Method, method, ""), null) : null; - Parameter? Argument(string op) => - Regex.Match(op, @"\A\.arg (\d+)\z") is { Success: true } load - ? spec.Parameters[int.Parse(load.Groups[1].Value, CultureInfo.InvariantCulture)] : null; + Parameter? Argument(int pc) => extraction.Emitted[0].ArgumentAt(pc) is int argument && + MethodArguments.Plain(spec.Method, argument) ? spec.Parameters[argument] : null; } /// Method `index` when it is static `(T[] values, int index)` returning `ref values[index]` of /// arrays of `element`, and nothing else. - static MethodDefinition? ElementAddress(Extraction extraction, int index, string element) + static MethodDefinition? ElementAddress(Extraction extraction, int index, Arrays.Element element) { var method = extraction.Methods[index]; - var ops = extraction.Emitted[index].Ops.Where(op => op != ".nop").ToArray(); + var slots = extraction.Emitted[index].Operations; + var instructions = method.Body.Instructions; + if (slots.Length != instructions.Count) return null; + var sites = StoreBodySites(extraction, index); return !method.HasThis && method.Parameters.Count == 2 && method.ReturnType is ByReferenceType && method.Parameters[0].ParameterType is ArrayType { Rank: 1 } array && WordArrayElement(array.ElementType) == element && method.Parameters[1].ParameterType.FullName is "System.Int32" && - extraction.Emitted[index].Metadata.Contains("locals := [],") && - ops.SequenceEqual([".arg 0", ".arg 1", $".memory (.arrayAddress {element})", ".ret"]) + extraction.Emitted[index].Frame.HasNoLocals && sites is + [var arraySite, var indexSite, (Unoutlined, { OpCode.Code: Code.Ldelema } address, _), + (StackOperation { Kind: Code.Ret }, _, _)] && + extraction.Emitted[index].ArgumentAt(arraySite.Pc) == 0 && extraction.Emitted[index].ArgumentAt(indexSite.Pc) == 1 && + address.Previous?.OpCode.Code != Code.Readonly && Arrays.ElementStorage(address, extraction.Architecture) == element ? method : null; } @@ -80,8 +91,8 @@ static string ArrayReadLine(Specification spec, ArrayAccess access, Writer lean) { lean.Line(" refine ⟨CIL.executionBound Extracted.program 0, ?final, ?results, ?run, ?postcondition⟩"); lean.Line(" case run =>"); - lean.Line($" cil_checked_steps [step_arrayLoad_of_element (index := .i32 {index.Name}) rfl {element}"); - lean.Line($" {loaded}, {array.Name}Valid]"); + lean.Line($" cil_checked_steps [{access.Load!.LoadLemma}"); + lean.Line($" (index := .i32 {index.Name}) rfl {element} {loaded}, {array.Name}Valid]"); } else { @@ -130,7 +141,7 @@ static void ElementContract(Writer lean, ElementHelper helper, Parameter array) lean.Line(" rintro ⟨values, index⟩ initial ⟨valid, typed, elements, contents, bound⟩"); lean.Line(" obtain ⟨item, found, _⟩ := contents.array_load valid (by decide) bound"); lean.Line(" refine MethodContract.Holds.intro initial item ?_ (Confined.refl _ _) ⟨rfl, found⟩"); - lean.Line(" cil_checked_steps [step_arrayAddress_of_element (index := .i32 index) typed rfl found, " + + lean.Line(" cil_checked_steps [arrayAddress_of_element true (index := .i32 index) typed rfl found, " + "valid,"); lean.Line(" arrayElement_formed found, leaveFrame]"); lean.Line(); diff --git a/Extractor/ProofOutlineGenerate.cs b/Extractor/ProofOutlineGenerate.cs index c14b51582..1d714d21b 100644 --- a/Extractor/ProofOutlineGenerate.cs +++ b/Extractor/ProofOutlineGenerate.cs @@ -1,6 +1,8 @@ +using ILOpCode = System.Reflection.Metadata.ILOpCode; using System.Text; using System.Text.RegularExpressions; using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -36,6 +38,9 @@ sealed record Parameter(string Name, ParameterShape Shape, string Lean, int Widt /// `.signed32`. internal string ArrayElement { get; init; } = ""; + /// The element of a word or value array argument, as the extraction names it. + internal Arrays.Element? Element { get; init; } + /// The element term a value array argument's `ArrayElement` names, when it is too long to state /// inline and no named field's `elementType` names it. internal string? ElementDefinition { get; init; } @@ -67,9 +72,12 @@ enum ParameterShape /// The method's return as a Lean type and value constructor, or none for `void`; a `bool` /// is a `Bool` the returned word encodes as 1 or 0, an object a possibly null reference, and a value array /// the view of its origin, length and element. - sealed record Result(string Lean, string Constructor, bool Boolean = false, bool Object = false, + sealed record Result(string Lean, int Width, bool Boolean = false, bool Object = false, bool Array = false) { + /// The value constructor of the result: by word width for a scalar. + internal string Constructor => Object ? ".object" : Array ? ".array" : Width == 8 ? ".i64" : ".i32"; + internal string Encode(string term) => Object ? $".object {term}" : Array ? $".array {term}" : $".scalar ({Constructor} {term})"; } @@ -100,14 +108,14 @@ static Outline Generate(Extraction extraction, string module) var names = new Names(); var views = new FieldViews(extraction); var receiver = method.HasThis ? new[] { Receiver(method, names, views) } : []; + var body = new Body(emitted); var parameters = WrittenArrays(method, receiver.Concat(method.Parameters.Select(parameter => - Describe(method, parameter, names, views, extraction.Architecture))).ToArray(), emitted.Ops, - new Body(emitted.Ops).Loops().Count); + Describe(method, parameter, names, views, extraction.Architecture))).ToArray(), body.Operations, + body.Loops().Count, extraction.Architecture); var result = DescribeResult(method, extraction.Architecture); bool symbolic = parameters.Any(parameter => parameter.Shape is ParameterShape.Reference or ParameterShape.Span or ParameterShape.Object or ParameterShape.StructReference or ParameterShape.Array or ParameterShape.WordArray or ParameterShape.ValueArray); - var body = new Body(emitted.Ops); var lean = new Writer(); var loops = body.Loops(); var spec = new Specification(method, parameters, result, symbolic, extraction.Views.Length > 0); @@ -131,14 +139,16 @@ ParameterShape.Span or ParameterShape.Object or ParameterShape.StructReference o var spanStore = loops.Count == 0 ? SpanStores(extraction, 0) : null; if (loops.Count == 0 && spanStore is null && parameters.Any(parameter => parameter.Writable)) throw Unsupported($"{method.FullName} writes a span outside a loop other than by storing its value " + - "into `target[0]`; new-proof follows a span written from the front by a loop or that one store"); + "into `target[0]`; new-proof follows a span written from the front by a loop or that one store" + + (parameters.FirstOrDefault(parameter => parameter.Shape == ParameterShape.Narrow) is { } narrow + ? $", of a word argument where `{narrow.Name}` is a narrow integer" : "")); if (spanStore is { } store) - source = SpanStoreLine(spec, store.Op, lean); + source = SpanStoreLine(spec, store.Lemma, lean); else if (structReceiver && loops.Count > 0) source = new ReceiverLoop(spec, body, lean).Generate(); else if (structReceiver) source = StructStraightLine(spec, body, names, lean); - else if (loops.Count == 0 && !spec.Framed && ArrayRead(extraction, spec, body.Ops) is { } read) + else if (loops.Count == 0 && !spec.Framed && ArrayRead(extraction, spec, body.Operations) is { } read) source = ArrayReadLine(spec, read, lean); else if (loops.Count == 0) source = StraightLine(extraction, spec, body, lean); @@ -161,11 +171,14 @@ ParameterShape.Span or ParameterShape.Object or ParameterShape.StructReference o /// `parameters` with the array a loop stores into, by index or through a writable span over it, /// marked written; a loop's store is followed only into a method's one array argument. A straight-line store /// stays a hole over the array's contents. - static Parameter[] WrittenArrays(MethodDefinition method, Parameter[] parameters, string[] ops, int loops) + static Parameter[] WrittenArrays(MethodDefinition method, Parameter[] parameters, Operation[] ops, int loops, + string architecture) { var arrays = parameters.Where(parameter => parameter.Shape == ParameterShape.WordArray).ToArray(); - if (loops == 0 || arrays.Length == 0 || !ops.Any(op => op.StartsWith(".memory (.arrayStore ", - StringComparison.Ordinal) || Regex.IsMatch(op, @"\A\.memory \(\.arraySpan \S+ true "))) return parameters; + if (loops == 0 || arrays.Length == 0 || !Enumerable.Range(0, ops.Length).Any(pc => + Arrays.Numeric(ops[pc], method, pc) is { Store: true } || + ops[pc] is Unoutlined && + Spans.ArrayView(method.Body.Instructions[pc], architecture) is { Element.Compound: false, Writable: true })) return parameters; if (arrays.Length > 1) throw Unsupported($"{method.FullName} stores into one of {arrays.Length} array arguments in a loop; " + "new-proof follows a loop's stores into a method's one array argument"); @@ -210,16 +223,17 @@ static Parameter Bind(MethodDefinition method, ParameterDefinition parameter, st if (ReferenceArrays.Is(type)) return new(name, ParameterShape.Array, "Reference", 8); if (type is ArrayType { IsVector: true } array && WordArrayElement(array.ElementType) is { } word) return new(name, ParameterShape.WordArray, "ArrayView", ElementWidth(array.ElementType)!.Value) - { ArrayElement = word }; + { ArrayElement = word.Expression, Element = word }; if (Arrays.Of(type, architecture) is { } values) { // A struct, vector or enumeration element names its type by a quoted identity, often wider than a line. - var named = views.ElementType(values.Expression); - var quoted = named is null && values.Expression.Contains('"', StringComparison.Ordinal); + var named = views.ElementType(values); + var quoted = named is null && values.Compound; return new(name, ParameterShape.ValueArray, "Reference", values.Width) { ArrayElement = named ?? (quoted ? name + "Element" : values.Expression), - ElementDefinition = quoted ? values.Expression : null + ElementDefinition = quoted ? values.Expression : null, + Element = values }; } if (type is ByReferenceType reference && ElementWidth(reference.ElementType) is { } width) @@ -248,12 +262,8 @@ span.ElementType.FullName is "System.ReadOnlySpan`1" or "System.Span`1" && } /// The `ArrayElement` the extraction gives an array of 32- or 64-bit integers. - static string? WordArrayElement(TypeReference type) => type.FullName switch - { - "System.Int32" => ".signed32", "System.UInt32" => ".unsigned32", - "System.Int64" => ".signed64", "System.UInt64" => ".unsigned64", - _ => null - }; + static Arrays.Element? WordArrayElement(TypeReference type) => + ElementWidth(type) is null ? null : Arrays.Primitive(type); static int? ElementWidth(TypeReference type) => type.FullName switch { @@ -265,12 +275,12 @@ span.ElementType.FullName is "System.ReadOnlySpan`1" or "System.Span`1" && static Result? DescribeResult(MethodDefinition method, string architecture) => method.ReturnType.FullName switch { "System.Void" => null, - "System.Boolean" => new("Bool", ".i32", Boolean: true), + "System.Boolean" => new("Bool", 4, Boolean: true), "System.UInt32" or "System.Int32" or "System.Byte" or "System.SByte" or "System.UInt16" - or "System.Int16" or "System.Char" => new("W32", ".i32"), - "System.UInt64" or "System.Int64" => new("W64", ".i64"), - _ when ObjectTypes.Is(method.ReturnType) => new("ManagedReference", ".object", Object: true), - _ when Arrays.Of(method.ReturnType, architecture) is not null => new("ArrayView", ".array", Array: true), + or "System.Int16" or "System.Char" => new("W32", 4), + "System.UInt64" or "System.Int64" => new("W64", 8), + _ when ObjectTypes.Is(method.ReturnType) => new("ManagedReference", 0, Object: true), + _ when Arrays.Of(method.ReturnType, architecture) is not null => new("ArrayView", 0, Array: true), var other => throw Unsupported($"{method.Name} returns {other}; new-proof states integer, object, " + "value array and void results") }; @@ -442,7 +452,14 @@ internal void Placeholders(Writer lean) lean.Line(); var subject = Symbolic ? "its result and the caller's memory" : "its result"; lean.Quiet(); - lean.Line($"/-- Replace with what `{Method.Name}` guarantees about {subject}. -/"); + // A specialized method's name carries its type arguments, so the sentence may need two lines. + var guarantee = $"/-- Replace with what `{Method.Name}` guarantees about {subject}. -/"; + if (guarantee.Length <= 100) lean.Line(guarantee); + else + { + lean.Line($"/-- Replace with what `{Method.Name}` guarantees about"); + lean.Line($" {subject}. -/"); + } var results = Binders.Concat(Symbolic ? [("initial", "Memory"), ("final", "Memory")] : []); if (Result is not null) results = results.Append(("result", Result.Lean)); lean.Wrapped("def Postcondition", Grouped(results), ": Prop := sorry"); @@ -503,16 +520,19 @@ static IEnumerable Grouped(IEnumerable<(string Name, string Type)> binde static string StraightLine(Extraction extraction, Specification spec, Body body, Writer lean) { if (spec.Framed) return ObjectStraightLine(extraction, spec, body, lean)!; + var plainReturn = LeanEmitter.Narrow(spec.Method.ReturnType) is null; + var homes = extraction.Emitted[0].Frame.IndexParameterHomes(); var steppable = spec.Parameters.All(parameter => parameter.Shape is ParameterShape.Scalar or - ParameterShape.StructValue) && body.Ops.All(op => Body.Pure(op) || - Regex.IsMatch(op, @"\A\.(localAddr \d+|local \d+|setLocal \d+|initAggregate \d+|" + - @"fieldAddress \d+|(?:setFieldAt|fieldAt|aggregateFieldAt|setAggregateFieldAt) \d+ \d+|dup)\z")); - var split = !steppable && SplitsOnBooleans(body.Ops, spec.Parameters); - var paths = steppable || split ? null : ScalarPaths(spec, body.Ops); + ParameterShape.StructValue) && + body.Operations.Index().All(entry => body.Method.PureAt(entry.Index, plainReturn) || + Steps(body.Method, spec.Method, entry.Index, homes)); + var split = !steppable && SplitsOnBooleans(body.Method, spec.Parameters, plainReturn); + var paths = steppable || split ? null : ScalarPaths(spec, body.Method); // A body over words that calls helpers composes them through stub contracts, as a body over objects does. if (!steppable && !split && paths is null && spec.Parameters.All(parameter => parameter.Shape == ParameterShape.Scalar) && - body.Ops.Any(op => op.StartsWith(".call ", StringComparison.Ordinal)) && + Enumerable.Range(0, body.Operations.Length) + .Any(pc => body.Method.CallAt(pc) is { Kind: CallKind.Static }) && ObjectStraightLine(extraction, spec, body, lean, stubsOnly: true) is { } composed) return composed; Header(lean, spec, paths is null ? ["CIL.Verification", "CIL.Safety.SymbolicExecution"] @@ -540,13 +560,38 @@ static string StraightLine(Extraction extraction, Specification spec, Body body, { lean.Line(" -- Choose a fuel bound and step the body with `cil_checked_steps [facts]`,"); lean.Line(" -- splitting cases where it branches."); - lean.Noted(" ", "sorry", $"{NotOutlined}{Unstepped(spec.Method, body.Ops, spec.Parameters)}; " + + lean.Noted(" ", "sorry", $"{NotOutlined}{Unstepped(spec.Method, body.Method, spec.Parameters)}; " + "prove its run and Postcondition"); } Footer(lean); return lean.ToString(); } + /// A frame, field or duplicate operation the stepping tactics take without facts: a local access + /// other than to a narrow integer local, an aggregate initialisation or a field access whose load is not + /// sign-extending. + static bool Steps(Method emitted, MethodDefinition method, int pc, IReadOnlyDictionary homes) + { + var site = method.Body.Instructions[pc]; + return emitted.Operations[pc] switch + { + Unoutlined when InstructionOperands.LoadSlot(method, site, homes) is int loaded => + !NarrowLocal(method, loaded), + Unoutlined when InstructionOperands.StoreSlot(method, site, homes) is int stored => + !NarrowLocal(method, stored), + Unoutlined => site.OpCode.Code is Code.Ldloca or Code.Ldloca_S or Code.Ldarga or Code.Ldarga_S || + emitted.StructFieldAt(pc) is { Signed: false } || emitted.ObjectAt(pc, Code.Initobj) is not null, + StackOperation { Kind: Code.Dup } => true, + _ => false + }; + } + + /// Whether local `index` is a declared local of a narrow integer type, whose loads and stores the + /// outlines leave to the author. A `byte` or `bool` argument's home has a narrow slot too, but its index follows + /// the declared locals, so this is false for it. + static bool NarrowLocal(MethodDefinition method, int index) => index < method.Body.Variables.Count && + LeanEmitter.Narrow(method.Body.Variables[index].VariableType) is not null; + /// The checked run of a body `cil_checked_steps` takes alone, and its postcondition hole; `suffix` /// keeps the named holes of each side of a split apart, since a named hole is shared across tactics. static void SteppedRun(Writer lean, Specification spec, string indent, string suffix, string note) @@ -562,19 +607,23 @@ static void SteppedRun(Writer lean, Specification spec, string indent, string su /// Whether a body over words and `bool`s steps once each `bool` is a literal: every operation is /// pure or a jump, and every test reads a `bool` argument just loaded. - static bool SplitsOnBooleans(string[] ops, Parameter[] parameters) + static bool SplitsOnBooleans(Method emitted, Parameter[] parameters, bool plainReturn) { var booleans = parameters.Count(parameter => parameter.Shape == ParameterShape.Boolean); if (booleans is 0 or > MaxBooleanSplits || parameters.Any(parameter => parameter.Shape is not (ParameterShape.Scalar or ParameterShape.Boolean))) return false; - bool LoadsBoolean(string op) => Regex.Match(op, @"\A\.arg (\d+)(?: \(some \.unsigned8\))?\z") is - { Success: true } load && parameters[int.Parse(load.Groups[1].Value, - System.Globalization.CultureInfo.InvariantCulture)].Shape == ParameterShape.Boolean; - return ops.Select((op, pc) => Body.Pure(op) || LoadsBoolean(op) || - Body.Target(op) is not null && !Body.Conditional(op) || - Regex.IsMatch(op, @"\A\.br(non)?zero \d+\z") && pc > 0 && LoadsBoolean(ops[pc - 1])) - .All(stepped => stepped); + var previousBoolean = false; + for (var pc = 0; pc < emitted.Operations.Length; pc++) + { + var loadsBoolean = emitted.ArgumentAt(pc) is int index && parameters[index].Shape == ParameterShape.Boolean; + var jump = emitted.BranchAt(pc); + if (!loadsBoolean && !emitted.PureAt(pc, plainReturn) && jump?.Kind != ILOpCode.Br && + !(previousBoolean && jump?.Kind is ILOpCode.Brfalse or ILOpCode.Brtrue)) + return false; + previousBoolean = loadsBoolean; + } + return true; } /// A straight-line body over object arguments: stepped when it loads fields and stores at most one @@ -605,12 +654,13 @@ string CalleeName(int index) // keeps locals, whose homes change the memory a stub's `post` keeps. Func summarized = !stubsOnly ? Summary : index => Stubbed(index) is null && Summary(index) is { Walk.NumericHomes: true } read ? read : null; - var numericHomes = HasNumericObjectHomes(spec.Method) || body.Ops.Select(op => - Regex.Match(op, @"\A\.call(?:Instance)? (\d+) (\d+)\z")).Where(match => match.Success) - .Any(match => summarized(int.Parse(match.Groups[1].Value))?.Walk.NumericHomes == true); + var numericHomes = HasNumericObjectHomes(spec.Method) || Enumerable.Range(0, body.Operations.Length) + .Any(pc => body.Method.CallAt(pc) is { Kind: not CallKind.Tail } call && + summarized(call.Method)?.Walk.NumericHomes == true); // Local homes belong to the entry memory, so the statement accepts any well-formed entry memory. if (numericHomes && !spec.Symbolic) spec = spec.InMemory(); - var walk = new ObjectWalk(spec, body.Ops, views, summarized, numericHomes: numericHomes, stubs: Stubbed); + var walk = new ObjectWalk(spec, body.Method, views, summarized, + extraction.Emitted[0].Frame.IndexParameterHomes(), numericHomes: numericHomes, stubs: Stubbed); var stepped = spec.Parameters.All(parameter => parameter.Shape is ParameterShape.Scalar or ParameterShape.Object or ParameterShape.Array or ParameterShape.ValueArray or ParameterShape.WordArray) && walk.Run(pc => Location(spec.Method, pc)); @@ -650,7 +700,7 @@ string CalleeName(int index) { lean.Line(" -- Choose a fuel bound and step the body with `cil_checked_steps [facts]`, giving a store"); lean.Line(" -- its `Owned.store`; a scalar field's load is read from its fact in the context."); - var reason = walk.Stopped ?? Unstepped(spec.Method, body.Ops, spec.Parameters); + var reason = walk.Stopped ?? Unstepped(spec.Method, body.Method, spec.Parameters); lean.Noted(" ", "sorry", $"{NotOutlined}the body meets {reason}; prove its run and Postcondition"); } Footer(lean); @@ -772,9 +822,11 @@ static void ElementDefinition(Writer lean, Parameter array) /// Why the outline leaves a body to the author: the first argument or operation that /// `cil_checked_steps` cannot take without facts or a case split. - static string Unstepped(MethodDefinition method, string[] ops, IEnumerable parameters) + static string Unstepped(MethodDefinition method, Method emitted, IEnumerable parameters) { - if (parameters.FirstOrDefault(parameter => parameter.Shape != ParameterShape.Scalar) is { } argument) + // A narrow parameter is named first: its load is what refuses an otherwise outlined body. + if (parameters.OrderBy(parameter => parameter.Shape != ParameterShape.Narrow) + .FirstOrDefault(parameter => parameter.Shape != ParameterShape.Scalar) is { } argument) return $"the body reads `{argument.Name}`, " + argument.Shape switch { ParameterShape.Boolean => "a `bool` that needs a case split", @@ -787,24 +839,41 @@ static string Unstepped(MethodDefinition method, string[] ops, IEnumerable "an array of caller memory", _ => "a span of caller memory" }; - var pc = Array.FindIndex(ops, op => !Body.Pure(op)); + var plainReturn = LeanEmitter.Narrow(method.ReturnType) is null; + var ops = emitted.Operations; + var pc = Enumerable.Range(0, ops.Length).FirstOrDefault(i => !emitted.PureAt(i, plainReturn), -1); if (pc < 0) return "the body has locals"; // A call the outline cannot compose names the contract the author must write and prove. - return Regex.Match(ops[pc], @"\A\.call(?:Instance)? (\d+) \d+\z") is { Success: true } call - ? $"`{ops[pc]}` at {Location(method, pc)} calls method {call.Groups[1].Value}, which " + - "`cil_contract_steps` composes through a `MethodContract` you write and prove with " + - $"`Holds Extracted.program {call.Groups[1].Value}`" - : $"`{ops[pc]}` at {Location(method, pc)} needs facts or a case split"; + var instruction = InstructionAt(method, pc); + return emitted.CallAt(pc) is { Kind: not CallKind.Tail } call + ? $""" + {instruction} calls method {call.Method}, which `cil_contract_steps` composes through a + `MethodContract` you write and prove with `Holds Extracted.program {call.Method}` + """.ReplaceLineEndings(" ") + : $"{instruction} needs facts or a case split"; } + static string? SourceLine(MethodDefinition method, int offset) => + Symbols.Location(method, offset, Path.GetFileName) is { } at ? at[..at.LastIndexOf(':')] : null; + /// The source line of the instruction at `pc`, or its IL offset without a PDB. static string Location(MethodDefinition method, int pc) { var offset = method.Body.Instructions[pc].Offset; - return Symbols.Location(method, offset, Path.GetFileName) is { } at - ? at[..at.LastIndexOf(':')] : $"IL_{offset:x4}"; + return SourceLine(method, offset) ?? $"IL_{offset:x4}"; + } + + /// Cecil's instruction and its source line, with embedded line breaks escaped for a comment. + static string InstructionAt(MethodDefinition method, int pc) + { + var instruction = method.Body.Instructions[pc]; + var raw = instruction.ToString().ReplaceLineEndings(@"\n"); + return SourceLine(method, instruction.Offset) is { } at ? $"`{raw}` at {at}" : $"`{raw}`"; } + static string StopAt(MethodDefinition method, int pc, string reason, bool showInstruction) => + showInstruction ? $"{InstructionAt(method, pc)}{reason}" : $"{reason} at {Location(method, pc)}"; + // `tags` names the goals the generated tactics leave, or is null when the proof is one hole. static void Header(Writer lean, Specification spec, IEnumerable imports, string? tags) { @@ -851,7 +920,7 @@ sealed class Names internal string Claim(string? wanted, string fallback) { // A parameter named like a generated hypothesis would be captured by the names the proof introduces. - var name = Regex.IsMatch(wanted ?? "", @"\A[A-Za-z][A-Za-z0-9_]*\z") && !Generated().IsMatch(wanted!) + var name = Regex.IsMatch(wanted ?? "", @"\A[A-Za-z][A-Za-z0-9_]*\z") && !Generated.IsMatch(wanted!) ? wanted! : fallback; var candidate = name; for (var suffix = 1; !used.Add(candidate); suffix++) candidate = name + suffix; @@ -865,7 +934,7 @@ internal string Claim(string? wanted, string fallback) @"encoded|changed|combined|formed|form|loadSigned|arrayApart|localHome|localWritten|localLoaded|" + @"frameFacts|below|fuel|invoked|callFormed|permitted)\d*|held\d+Final|" + @"(?:owned|kept|added|loaded)\d+(?:Field\d+)?(?:Signed)?)\z")] - private static partial Regex Generated(); + private static partial Regex Generated { get; } /// Lean source with two-space indentation and lines kept under 100 characters. sealed class Writer @@ -936,18 +1005,31 @@ void Words(string first, string rest, string text) if (current.Length + word.Length + 1 > Limit && current.Length > first.Length) { Line(current); - current = rest + word; + current = rest; } - else current += (current == first ? "" : " ") + word; + else if (current != first) current += " "; + // A single metadata name or string literal can exceed a whole comment line. + var remaining = word.AsSpan(); + while (current.Length + remaining.Length > Limit) + { + var count = Limit - current.Length; + Line(string.Concat(current, remaining[..count])); + current = rest; + remaining = remaining[count..]; + } + current = string.Concat(current, remaining); } Line(current); } + /// The indentation of wrapped proof terms following a head. + internal static int ContinuationIndent(string head) => head.Length - head.TrimStart().Length + 4; + /// A head followed by binders and a tail, wrapped at binder boundaries. internal void Wrapped(string head, IEnumerable binders, string tail) { var current = head; - var continuation = new string(' ', head.Length - head.TrimStart().Length + 4); + var continuation = new string(' ', ContinuationIndent(head)); foreach (var part in binders.Append(tail).Where(part => part.Length > 0)) { if (current.Length + 1 + part.Length > Limit) diff --git a/Extractor/ProofOutlineLoops.cs b/Extractor/ProofOutlineLoops.cs index 9972979e1..a9672f654 100644 --- a/Extractor/ProofOutlineLoops.cs +++ b/Extractor/ProofOutlineLoops.cs @@ -1,5 +1,7 @@ +using ILOpCode = System.Reflection.Metadata.ILOpCode; using System.Text.RegularExpressions; using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -7,59 +9,81 @@ namespace Nethermind.LeanCIL; // operations symbolically. A path the evaluation cannot follow becomes a hole, never a guess. internal static partial class ProofOutline { - /// The emitted operations of one method and its control flow. - sealed class Body(string[] ops) + /// A callee's exact signature, optionally continued across a Lean string gap. + internal static string CalleeLiteral(string signature, int column) { - internal string[] Ops { get; } = ops; + const int Room = 96; + var literal = IdentityText.Quote(signature); + if (column + literal.Length <= Room) return literal; + int cut = literal.LastIndexOf(',', Math.Clamp(Room - column - 2, 0, literal.Length - 1)); + if (cut < 0) cut = literal.IndexOf('('); + if (cut < 0) return literal; + // Lean consumes whitespace after the gap, so spaces in the signature precede it. + while (literal[cut + 1] == ' ') cut++; + return literal.Insert(cut + 1, "\\\n "); + } - internal static int? Target(string op) => - Regex.Match(op, @"\A\.(branch|brzero|brnonzero|beq|bne|blt|ble|bgt|bge|bltu|bleu|bgtu|bgeu) (\d+)\z") - is { Success: true } match ? int.Parse(match.Groups[2].Value) : null; + /// The emitted operations of one method and its control flow. + sealed class Body(Method method) + { + internal Method Method { get; } = method; + internal Operation[] Operations { get; } = method.Operations; - internal static bool Conditional(string op) => Target(op) is not null && !op.StartsWith(".branch "); + /// The number of stack values consumed by a normalized CIL branch. + internal static int BranchOperands(ILOpCode kind) => kind switch + { + ILOpCode.Br => 0, + ILOpCode.Brfalse or ILOpCode.Brtrue => 1, + _ => 2 + }; /// The proposition a conditional branch tests of its `bits`-bit operands' terms, and whether it /// is taken when the proposition holds; signed comparisons read the words through `toInt`. A literal /// operand is written as default `simp` writes it, so the split's fact rewrites a simplified goal. - internal static (string Condition, bool TakenWhenTrue) Test(string name, string[] terms, int bits) + internal static (string Condition, bool TakenWhenTrue) Test(ILOpCode kind, Operand[] terms, int bits) { string Word(int index) => Literal(terms[index], bits); - string Signed(int index) => Regex.IsMatch(terms[index], @"\A\d+\z") - ? SignedValue(terms[index], bits) : $"{terms[index]}.toInt"; - return name switch - { - "brzero" => ($"{terms[0]} = {Literal("0", bits)}", true), - "brnonzero" => ($"{terms[0]} = {Literal("0", bits)}", false), - "beq" => ($"{Word(0)} = {Word(1)}", true), - "bne" => ($"{Word(0)} = {Word(1)}", false), - "blt" => ($"{Signed(0)} < {Signed(1)}", true), - "ble" => ($"{Signed(0)} ≤ {Signed(1)}", true), - "bgt" => ($"{Signed(1)} < {Signed(0)}", true), - "bge" => ($"{Signed(1)} ≤ {Signed(0)}", true), - "bltu" => ($"{Word(0)} < {Word(1)}", true), - "bleu" => ($"{Word(0)} ≤ {Word(1)}", true), - "bgtu" => ($"{Word(1)} < {Word(0)}", true), - "bgeu" => ($"{Word(1)} ≤ {Word(0)}", true), - _ => throw new ArgumentException($"`{name}` is not a conditional branch", nameof(name)) + string Signed(int index) => terms[index].Numeral is { } numeral + ? SignedValue(numeral, bits) : $"{terms[index].Term}.toInt"; + return kind switch + { + ILOpCode.Brfalse => ($"{terms[0].Term} = {Literal(new Operand("0", 0), bits)}", true), + ILOpCode.Brtrue => ($"{terms[0].Term} = {Literal(new Operand("0", 0), bits)}", false), + ILOpCode.Beq => ($"{Word(0)} = {Word(1)}", true), + ILOpCode.Bne_un => ($"{Word(0)} = {Word(1)}", false), + ILOpCode.Blt => ($"{Signed(0)} < {Signed(1)}", true), + ILOpCode.Ble => ($"{Signed(0)} ≤ {Signed(1)}", true), + ILOpCode.Bgt => ($"{Signed(1)} < {Signed(0)}", true), + ILOpCode.Bge => ($"{Signed(1)} ≤ {Signed(0)}", true), + ILOpCode.Blt_un => ($"{Word(0)} < {Word(1)}", true), + ILOpCode.Ble_un => ($"{Word(0)} ≤ {Word(1)}", true), + ILOpCode.Bgt_un => ($"{Word(1)} < {Word(0)}", true), + ILOpCode.Bge_un => ($"{Word(1)} ≤ {Word(0)}", true), + _ => throw new ArgumentException( + $"`{kind.ToString().ToLowerInvariant()}` is not a conditional branch", nameof(kind)) }; } /// A decimal literal as the `bits`-bit word `simp` normalizes it to, such as `0#32`; any /// other term is returned as it is. - internal static string Literal(string term, int bits) => - Regex.IsMatch(term, @"\A\d+\z") ? $"{term}#{bits}" : term; + internal static string Literal(Operand operand, int bits) => + operand.Numeral is null ? operand.Term : $"{operand.Term}#{bits}"; + + /// A term of a branch test, with its value when the term is a bare numeral. + internal readonly record struct Operand(string Term, long? Numeral = null); /// The integer a `bits`-bit decimal literal reads as through `toInt`, as `simp` writes it. - static string SignedValue(string term, int bits) + static string SignedValue(long numeral, int bits) { - var value = System.Numerics.BigInteger.Parse(term, System.Globalization.CultureInfo.InvariantCulture) % + var value = new System.Numerics.BigInteger(numeral) % (System.Numerics.BigInteger.One << bits); if (value >= System.Numerics.BigInteger.One << (bits - 1)) value -= System.Numerics.BigInteger.One << bits; return value.Sign < 0 ? $"({value})" : value.ToString(System.Globalization.CultureInfo.InvariantCulture); } /// Each loop as its head and latch, the last pc branching back to it, in head order. - internal List<(int Head, int Latch)> Loops() => Ops.Select((op, pc) => (Target: Target(op), Pc: pc)) + internal List<(int Head, int Latch)> Loops() => Enumerable.Range(0, Operations.Length) + .Select(pc => (Target: Method.BranchAt(pc)?.Target, Pc: pc)) .Where(edge => edge.Target is { } target && target <= edge.Pc) .GroupBy(edge => edge.Target!.Value).OrderBy(group => group.Key) .Select(group => (group.Key, group.Max(edge => edge.Pc))).ToList(); @@ -68,21 +92,16 @@ static string SignedValue(string term, int bits) internal int BlockStart(int pc) { var leaders = new SortedSet { 0 }; - for (var index = 0; index < Ops.Length; index++) + for (var index = 0; index < Operations.Length; index++) { - if (Target(Ops[index]) is { } target) leaders.Add(target); - if (Target(Ops[index]) is not null || Ops[index].StartsWith(".ret")) leaders.Add(index + 1); + var branch = Method.BranchAt(index); + if (branch is { } jump) leaders.Add(jump.Target); + if (branch is not null || Operations[index] is StackOperation { Kind: Code.Ret }) + leaders.Add(index + 1); } return leaders.Where(leader => leader <= pc).Max(); } - /// An operation that steps without a branch, a call, a fault or memory: arithmetic, conversions - /// and the bit counts, which are total. - internal static bool Pure(string op) => Regex.IsMatch(op, - @"\A\.(arg \d+|const32 \S.*|const64 \S.*|add|sub|mul|band|bor|bxor|nop|ret|convI8|convU8|convU4|" + - @"convI4)\z") || - Regex.IsMatch(op, @"\A\.intrinsic \(\.scalar \(\.(log2|popCount|leadingZeroCount|trailingZeroCount) " + - @"\d+ \.word(32|64)\)\) 1\z"); } enum Part { Word, Field, Span, Object } @@ -94,26 +113,30 @@ sealed record Component(string Name, Part Part, int Width, int Slot, int Offset, /// A frame slot: a local's home, or a span parameter's home. A parameter's span slot holds the /// argument on entry, so the entry frame names it; a span local's holds nothing until it is assigned. /// - sealed record Slot(int Index, string Kind, string? Home, int Layout, Component[] Components, + sealed record Slot(int Index, LocalKind Kind, string? Home, int Layout, Component[] Components, bool Argument = false); abstract record Value; /// A word the outline names by `Term`; `Stacked` is its stack entry when the step leaves it in a /// form other than `Term`, which a test reads simplified. - sealed record Number(string Term, int Width, bool Literal = false, bool Narrow = false, string? Stacked = null) - : Value; + sealed record Number(string Term, int Width, long? Value = null, bool Narrow = false, string? Stacked = null) + : Value + { + /// The word is a bare numeral, whose value is `Value`. + internal bool Literal => Value is not null; + } sealed record Unknown(string What) : Value; sealed record LocalValue(int Slot) : Value; - sealed record LocalAddress(int Slot) : Value; + sealed record LocalAddressValue(int Slot) : Value; sealed record Element(int Component) : Value; - sealed record SpanLength(int Component) : Value; + sealed record SpanLengthValue(int Component) : Value; sealed record Sliced(int Component) : Value; sealed record ReferenceArgument(string Name) : Value; sealed record ObjectTerm(string Term) : Value; /// An array argument, destructured into its base and length before the loop. sealed record WordArrayArgument(Parameter Array) : Value; /// The 64-bit length `ldlen` pushes for an array argument. - sealed record ArrayLength(Parameter Array) : Value; + sealed record ArrayLengthValue(Parameter Array) : Value; /// A span argument, which a span local copies before the loop. sealed record SpanArgument(Parameter Span) : Value; /// The span over a whole array argument that a span local holds before the loop. @@ -162,6 +185,7 @@ static string Suffix(int index) => index == 0 ? "" : (index + 1).ToString(System.Globalization.CultureInfo.InvariantCulture); readonly MethodDefinition method = extraction.Methods[0]; + readonly Dictionary homes = extraction.Emitted[0].Frame.IndexParameterHomes(); readonly FieldViews views = new(extraction); readonly List slots = []; readonly List components = []; @@ -176,8 +200,10 @@ internal string Emit(Writer lean) var imports = new List { "CIL.Verification", "CIL.StructuralLabels" }; if (callees.Count > 0) imports.Add("CIL.MethodIdentity"); imports.Add("CIL.Safety.LoopScaffold"); - if (body.Ops.Any(op => op.StartsWith(".memory (.add ", StringComparison.Ordinal)) && - spec.Parameters.Any(parameter => parameter.Shape == ParameterShape.Reference) || + if (spec.Parameters.Any(parameter => parameter.Shape == ParameterShape.Reference) && + Enumerable.Range(0, body.Operations.Length).Any(pc => + body.Operations[pc] is StackOperation { Kind: Code.Add } || body.Operations[pc] is Unoutlined && + UnsafeModels.Offset(method.Body.Instructions[pc], extraction.Architecture) is not null) || callees.Keys.Any(index => Branches(extraction, index)) || Indexed.Any()) imports.Add("CIL.Safety.LoopObligations"); if (spec.Framed) imports.Add("CIL.Safety.ObjectFields"); @@ -230,35 +256,32 @@ void ArrayObligation(Writer lean, string indent) void Frame() { - var metadata = extraction.Emitted[0].Metadata; - var kinds = Items(Regex.Match(metadata, @"localKinds := \[(.*?)\], opaqueVectorLocals").Groups[1].Value); - var homeList = Regex.Match(metadata, @"parameterHomes := \[(.*?)\], staticSites").Groups[1].Value; - var homes = Regex.Matches(homeList, @"\((\d+), ([^)]*)\)") - .Select(match => (Index: int.Parse(match.Groups[1].Value), Kind: match.Groups[2].Value)); + var frame = extraction.Emitted[0].Frame; + var kinds = frame.LocalKinds; var localNames = method.Body.Variables.Select(variable => method.DebugInformation.TryGetName(variable, out var name) ? name : null).ToArray(); - for (var index = 0; index < kinds.Length; index++) + for (var index = 0; index < kinds.Count; index++) { var kind = kinds[index]; var name = names.Claim(localNames[index], "local" + index); var home = $"home{index}"; - if (Regex.Match(kind, @"\A\.word(32|64)\z") is { Success: true } word) + if (kind is LocalKind.Word32 or LocalKind.Word64) { - var width = word.Groups[1].Value == "64" ? 8 : 4; + var width = kind is LocalKind.Word64 ? 8 : 4; Add(new(index, kind, home, -1, [new(name, Part.Word, width, index, 0, null)])); } - else if (Regex.Match(kind, @"\A\.aggregate (\d+)\z").Success) + else if (kind is LocalKind.Aggregate) Add(Aggregate(index, kind, home, name)); - else if (kind == ".object") + else if (kind is LocalKind.Object) Add(new(index, kind, null, -1, [new(name, Part.Object, 8, index, 0, null)])); - else if (kind.StartsWith(".span ", StringComparison.Ordinal)) + else if (kind is LocalKind.Span) { var source = SpanSource(index, kind, name); Add(new(index, kind, null, -1, [new(name, Part.Span, source.Width, index, 0, source)])); } - else throw Unsupported($"local `{name}` has kind {kind}"); + else throw Unsupported($"local `{name}` has kind {kind.ToLean()}"); } - foreach (var (argument, kind) in homes) + foreach (var (argument, kind) in frame.ParameterHomes) { var parameter = spec.Parameters[argument]; if (parameter.Shape != ParameterShape.Span || kind != SpanKind(parameter)) @@ -272,27 +295,36 @@ void Frame() if (components.Count == 0) throw Unsupported("its loop keeps no state in locals"); } - static string SpanKind(Parameter span) => $".span {span.Width} {(span.Writable ? "true" : "false")}"; + static LocalKind SpanKind(Parameter span) => new LocalKind.Span(span.Width, span.Writable); /// The argument a span local is assigned once, before its loop, from: a span argument it copies or /// an array argument it spans whole. Its other stores are the loop's `Slice(1)` of itself. - Parameter SpanSource(int local, string kind, string name) + Parameter SpanSource(int local, LocalKind kind, string name) { - var ops = body.Ops; - var stores = Enumerable.Range(1, ops.Length - 1).Where(pc => ops[pc] == $".setLocal {local}" && - ops[pc - 1] != ".memory (.spanSlice false)").ToArray(); + var ops = body.Operations; var refused = Unsupported($"span local `{name}` is not assigned once from a span argument or from the " + "whole of an array argument"); - if (stores.Length != 1 || stores[0] >= loops[0].Head) throw refused; - var store = stores[0]; - Parameter Argument(int pc) => Regex.Match(ops[pc], @"\A\.arg (\d+)\z") is { Success: true } match - ? spec.Parameters[int.Parse(match.Groups[1].Value)] : throw refused; + int store = -1; + for (int pc = 1; pc < ops.Length; pc++) + { + if (ops[pc] is not Unoutlined || + InstructionOperands.StoreSlot(method, method.Body.Instructions[pc], homes) != local) continue; + if (ops[pc - 1] is Unoutlined && + Spans.MemberWidth(method.Body.Instructions[pc - 1], "Slice", 1, extraction.Architecture) != null) + continue; + if (store >= 0 || pc >= loops[0].Head) throw refused; + store = pc; + } + if (store < 0) throw refused; + Parameter Argument(int pc) => body.Method.ArgumentAt(pc) is int argument && MethodArguments.Plain(spec.Method, argument) + ? spec.Parameters[argument] : throw refused; Parameter source; - if (Regex.IsMatch(ops[store - 1], @"\A\.arg \d+\z")) + if (body.Method.ArgumentAt(store - 1).HasValue) source = Argument(store - 1); else if (store >= 2 && Argument(store - 2) is { Shape: ParameterShape.WordArray } array && - ops[store - 1] == - $".memory (.arraySpan {array.ArrayElement} {(array.Writable ? "true" : "false")} .whole)") + ops[store - 1] is Unoutlined && + Spans.ArrayView(method.Body.Instructions[store - 1], extraction.Architecture, wholeOnly: true) is { } whole && + whole.Writable == array.Writable && whole.Element == array.Element) source = array; else throw refused; if (source.Shape is not (ParameterShape.Span or ParameterShape.WordArray) || kind != SpanKind(source)) @@ -302,9 +334,10 @@ Parameter Argument(int pc) => Regex.Match(ops[pc], @"\A\.arg (\d+)\z") is { Succ if (source.Shape == ParameterShape.WordArray) { // An array read other than through the span is indexed, which a spanned array is not destructured for. - var argument = $".arg {Array.IndexOf(spec.Parameters, source)}"; - if (Enumerable.Range(0, ops.Length).Any(pc => ops[pc] == argument && pc != store - 2)) - throw Unsupported($"array `{source.Name}` is both spanned by `{name}` and read directly"); + var argument = Array.IndexOf(spec.Parameters, source); + for (var pc = 0; pc < ops.Length; pc++) + if (pc != store - 2 && body.Method.ArgumentAt(pc) == argument) + throw Unsupported($"array `{source.Name}` is both spanned by `{name}` and read directly"); spanned.Add(source); } return source; @@ -316,55 +349,32 @@ void Add(Slot slot) components.AddRange(slot.Components); } - Slot Aggregate(int index, string kind, string home, string name) + Slot Aggregate(int index, LocalKind kind, string home, string name) { var type = method.Body.Variables[index].VariableType; - var layout = Array.IndexOf(extraction.LayoutIdentities, LayoutEvidence.Identity(type)); - var definition = Regex.Match(Source, $@"^def layout{layout} : AggregateLayout := ⟨\d+, \[(.*)\]⟩\r?$", - RegexOptions.Multiline); - if (layout < 0 || !definition.Success) throw Unsupported($"no layout for local `{name}`"); - var fields = Regex.Matches(definition.Groups[1].Value, @"⟨(\d+), (\d+), (true|false)⟩").ToArray(); + var identity = LayoutEvidence.Identity(type); + var layout = LayoutIndex(extraction, identity); + if (layout < 0) throw Unsupported($"no layout for local `{name}`"); + var fields = extraction.Layouts[layout].Fields; var named = type.Resolve()?.Fields.Where(field => !field.IsStatic).ToArray() ?? []; - if (fields.Any(field => field.Groups[3].Value == "true") || named.Length != fields.Length) + if (fields.Any(field => field.IsAggregate) || named.Length != fields.Count) throw Unsupported($"struct local `{name}` nests a struct"); return new(index, kind, home, layout, fields.Select((field, position) => { var fieldName = named[position].Name; var component = names.Claim(name + char.ToUpperInvariant(fieldName[0]) + fieldName[1..], $"{name}Field{position}"); - return new Component(component, Part.Field, int.Parse(field.Groups[2].Value), index, - int.Parse(field.Groups[1].Value), null); + return new Component(component, Part.Field, field.Width, index, field.Offset, null); }).ToArray()); } - string Source => extraction.Source; - - static string[] Items(string list) - { - var items = new List(); - var depth = 0; - var start = 0; - for (var index = 0; index < list.Length; index++) - { - if (list[index] is '(' or '[' or '⟨') depth++; - else if (list[index] is ')' or ']' or '⟩') depth--; - else if (list[index] == ',' && depth == 0) - { - items.Add(list[start..index].Trim()); - start = index + 1; - } - } - if (list.Trim().Length > 0) items.Add(list[start..].Trim()); - return [.. items]; - } - // Callees are named for their methods so a reader can match each contract to its call. void Callees() { - foreach (var op in body.Ops) - if (Regex.Match(op, @"\A\.call(?:Instance)? (\d+) \d+\z") is { Success: true } call) + for (var pc = 0; pc < body.Operations.Length; pc++) + if (body.Method.CallAt(pc) is { Kind: not CallKind.Tail } call) { - var index = int.Parse(call.Groups[1].Value); + var index = call.Method; if (callees.ContainsKey(index)) continue; var callee = extraction.Methods[index]; callees[index] = names.Claim(char.ToLowerInvariant(callee.Name[0]) + callee.Name[1..], @@ -417,7 +427,7 @@ var type when ObjectTypes.Is(type) => var output = callee.ReturnType.FullName == "System.Void" ? (int?)null : ElementWidth(callee.ReturnType) ?? throw Unsupported($"callee {callee.Name} returns {callee.ReturnType.FullName}"); var binding = $"bind_callee {name} {name}Body := "; - lean.Line(binding + Quoted(callee.FullName, binding.Length)); + lean.Line($"""{binding}{CalleeLiteral(callee.FullName, binding.Length)}"""); lean.Line(" from Extracted.method0"); lean.Line(); // An object callee whose body the outline reads gets the contract that body meets, proved. @@ -506,18 +516,18 @@ var type when ObjectTypes.Is(type) => lean.Line($" dsimp only [{name}Contract] at writable"); lean.Line($" obtain ⟨after, wrote, formed, confined, filled⟩ := writable.write_one {value}"); lean.Line(" refine MethodContract.Holds.intro after () ?_ confined filled"); - lean.Line($" cil_checked_steps [{StoreLemma(body.Op)} wrote, formed]"); + lean.Line($" cil_checked_steps [{body.Lemma} wrote, formed]"); } else if (spanStore is { } spanBody) - SpanStoreContract(lean, index, name, pattern, Variable(declared[1].Name), spanBody.Op); + SpanStoreContract(lean, index, name, pattern, Variable(declared[1].Name), spanBody.Lemma); else { var reason = inputs.Any(input => input.Reference) ? "it writes through a reference" : inputs.Any(input => input.Span) ? "it writes through a span" : inputs.Any(input => input.Object is not null) ? "it takes an object, whose fields need facts" : output is null ? "it returns nothing to state" - : !extraction.Emitted[index].Metadata.Contains("locals := [],") ? "it has locals" - : Unstepped(callee, extraction.Emitted[index].Ops, []); + : !extraction.Emitted[index].Frame.HasNoLocals ? "it has locals" + : Unstepped(callee, extraction.Emitted[index], []); lean.Noted(" ", "sorry", $"{NotOutlined}{callee.Name}'s body against the contract, since " + reason); } @@ -526,27 +536,31 @@ var type when ObjectTypes.Is(type) => /// The reference and value parameters, width and store of a static callee whose body only /// stores one word argument through one reference argument of that word's width. - (int Slot, int Value, int Width, string Op)? Stores(int index) + (int Slot, int Value, int Width, string Lemma)? Stores(int index) { var callee = extraction.Methods[index]; - var ops = extraction.Emitted[index].Ops.Where(op => op != ".nop").ToArray(); + var ops = StoreBodySites(extraction, index); if (callee.HasThis || callee.ReturnType.FullName != "System.Void" || ops.Length != 4 || - !extraction.Emitted[index].Metadata.Contains("locals := [],") || ops[3] != ".ret") return null; - var slot = Regex.Match(ops[0], @"\A\.arg (\d+)\z"); - var value = Regex.Match(ops[1], @"\A\.arg (\d+)\z"); - if (!slot.Success || !value.Success) return null; - var (reference, word) = (int.Parse(slot.Groups[1].Value), int.Parse(value.Groups[1].Value)); + !extraction.Emitted[index].Frame.HasNoLocals || ops[3].Site.OpCode.Code != Code.Ret) return null; + var emitted = extraction.Emitted[index]; + if (emitted.ArgumentAt(ops[0].Pc) is not int reference || emitted.ArgumentAt(ops[1].Pc) is not int word || + !MethodArguments.Plain(callee, reference) || !MethodArguments.Plain(callee, word)) return null; if (callee.Parameters[reference].ParameterType is not ByReferenceType target || ElementWidth(target.ElementType) is not { } width || ElementWidth(callee.Parameters[word].ParameterType) != width) return null; - var stores = width == 4 ? ops[2] == ".storeScalar 4" : ops[2] is ".store64" or ".storeScalar 8"; - return stores ? (reference, word, width, ops[2]) : null; + var write = ScalarStore(emitted, ops[2].Pc, ops[2].Site, extraction.Architecture); + return width is 4 or 8 && write is { } store && store.Width == width + ? (reference, word, width, store.Lemma) : null; } /// Whether a callee without locals only computes, so one checked run steps its body. - internal static bool Computes(Extraction extraction, int index) => - extraction.Emitted[index].Metadata.Contains("locals := [],") && - extraction.Emitted[index].Ops.All(Body.Pure); + internal static bool Computes(Extraction extraction, int index) + { + var plainReturn = LeanEmitter.Narrow(extraction.Methods[index].ReturnType) is null; + return extraction.Emitted[index].Frame.HasNoLocals && + Enumerable.Range(0, extraction.Emitted[index].Operations.Length) + .All(pc => extraction.Emitted[index].PureAt(pc, plainReturn)); + } /// The `Holds` proof of a callee of words that `Computes` or `Branches`, its inputs bound by /// `pattern` and named `arguments`: one stepped run, or a split on each test. A `kept` contract's @@ -572,52 +586,100 @@ internal static void WordCalleeProof(Writer lean, Extraction extraction, int ind internal static bool Branches(Extraction extraction, int index) { var callee = extraction.Methods[index]; - var ops = extraction.Emitted[index].Ops; - return !callee.HasThis && - callee.Parameters.All(parameter => ElementWidth(parameter.ParameterType) is not null) && - callee.ReturnType.FullName != "System.Void" && ElementWidth(callee.ReturnType) is not null && - extraction.Emitted[index].Metadata.Contains("locals := [],") && - ops.Any(op => Body.Conditional(op) || op.StartsWith(".throw ", StringComparison.Ordinal)) && - ops.All(op => - Regex.IsMatch(op, @"\A\.(arg \d+|const(32|64) \S.*|add|sub|mul|band|bor|bxor|nop|ret)\z") || - Body.Target(op) is not null || op.StartsWith(".throw ", StringComparison.Ordinal)); + var ops = extraction.Emitted[index].Operations; + var plainReturn = LeanEmitter.Narrow(callee.ReturnType) is null; + var emitted = extraction.Emitted[index]; + if (callee.HasThis || callee.Parameters.Any(parameter => ElementWidth(parameter.ParameterType) is null) || + callee.ReturnType.FullName == "System.Void" || ElementWidth(callee.ReturnType) is null || + !emitted.Frame.HasNoLocals) return false; + var hasSplit = false; + for (var pc = 0; pc < ops.Length; pc++) + { + if (emitted.BranchAt(pc) is { } branch) + { + hasSplit |= branch.Kind != ILOpCode.Br; + continue; + } + if (!Supported(pc)) return false; + hasSplit |= ops[pc] is StackOperation { Kind: Code.Throw }; + } + return hasSplit; + + bool Supported(int pc) => ops[pc] switch + { + StackOperation { Kind: Code.Ret } => plainReturn, + Unoutlined when extraction.Emitted[index].ArgumentAt(pc) is int argument => MethodArguments.Plain(callee, argument), + // The word stack ends at this throw before any path can use its object operand. + StackOperation { Kind: Code.Ldnull } => + Throws.Analyze(callee).Operands.Contains(callee.Body.Instructions[pc]), + StackOperation { Kind: Code.Throw or Code.Add or Code.Sub or Code.Mul or + Code.And or Code.Or or Code.Xor or Code.Nop } => true, + _ => extraction.Emitted[index].ConstantAt(pc) is { Width: 32 or 64 } + }; } + /// The term a constant's operand is written as: its digits, or its bit-vector conversion. + static string ConstantTerm((int Width, long Value, bool Numeral) constant) => constant.Numeral + ? $"{constant.Value}" : $"(BitVec.ofInt {constant.Width} ({constant.Value}))"; + + /// The infix operator of a word operation the walks name, or null for any other operation. + static string? Arithmetic(Operation operation) => + operation is StackOperation { IsArithmetic: true } arithmetic ? arithmetic.Symbol : null; + + /// The byte stride of a signed reference addition. + static int? ReferenceStride(Operation operation, (ulong Stride, bool Signed)? displacement) => + displacement is { Signed: true, Stride: <= int.MaxValue } offset + ? Convert.ToInt32(offset.Stride) + : operation is StackOperation { Kind: Code.Add } ? 1 : null; + + /// The operation reads an unsigned element of bytes. + static bool ReadsWidth(Method emitted, int pc, Instruction site, string architecture, int width) => + emitted.Operations[pc] switch + { + Unoutlined when emitted.ObjectAt(pc, Code.Ldobj) is { Signed: false } load => load.ScalarWidth == width, + Unoutlined when UnsafeModels.UnalignedStorage(site, "ReadUnaligned", architecture) is { } storage => + storage.FullName is not ("System.SByte" or "System.Int16") && + MetadataValidation.PrimitiveWidth(storage) == width, + StackOperation { Kind: Code.Ldind_U1 } => width == 1, + StackOperation { Kind: Code.Ldind_U2 } => width == 2, + StackOperation { Kind: Code.Ldind_I4 or Code.Ldind_U4 } => width == 4, + StackOperation { Kind: Code.Ldind_I8 or Code.Ldind_I } => width == 8, + _ => false + }; + + /// A word a branching callee's walk holds: its term, its width in bits, and its value when it is + /// a bare numeral. + internal readonly record struct BranchWord(string Term, int Bits, long? Numeral = null); + /// The contract proof of a branching callee from `pc`: a `by_cases` on each test, each returning /// side stepped with the tests' facts, and each throwing side closed from the precondition. A `kept` /// contract's `post` first states that the memory is as it was, which each returning side proves. /// internal static void BranchedCallee(Writer lean, Extraction extraction, int index, string name, - string[] arguments, int pc, List<(string Term, int Bits)> stack, List<(string Fact, string Text)> facts, + string[] arguments, int pc, List stack, List<(string Fact, string Text)> facts, string indent, ref int sides, bool kept) { var callee = extraction.Methods[index]; - var ops = extraction.Emitted[index].Ops; + var ops = extraction.Emitted[index].Operations; string Where() => facts.Count == 0 ? "" : " where " + string.Join(" and ", facts.Select(fact => fact.Text)); while (true) { var op = ops[pc]; - Match match; - if ((match = Regex.Match(op, @"\A\.arg (\d+)\z")).Success) + if (extraction.Emitted[index].ArgumentAt(pc) is int argument && MethodArguments.Plain(callee, argument)) { - var argument = int.Parse(match.Groups[1].Value); var bits = 8 * ElementWidth(callee.Parameters[argument].ParameterType)!.Value; - stack.Add((arguments[argument], bits)); + stack.Add(new(arguments[argument], bits)); } - else if ((match = Regex.Match(op, @"\A\.const(32|64) (.+)\z")).Success) - stack.Add((match.Groups[2].Value, int.Parse(match.Groups[1].Value))); - else if ((match = Regex.Match(op, @"\A\.(add|sub|mul|band|bor|bxor)\z")).Success) + else if (extraction.Emitted[index].ConstantAt(pc) is { Width: 32 or 64 } constant) + stack.Add(new(ConstantTerm(constant), constant.Width, constant.Numeral ? constant.Value : null)); + else if (Arithmetic(op) is { } symbol) { var right = stack[^1]; var left = stack[^2]; stack.RemoveRange(stack.Count - 2, 2); - var symbol = match.Groups[1].Value switch - { - "add" => "+", "sub" => "-", "mul" => "*", "band" => "&&&", "bor" => "|||", _ => "^^^" - }; - stack.Add(($"({left.Term} {symbol} {right.Term})", left.Bits)); + stack.Add(new($"({left.Term} {symbol} {right.Term})", left.Bits)); } - else if (op == ".ret") + else if (op is StackOperation { Kind: Code.Ret }) { var suffix = (++sides).ToString(System.Globalization.CultureInfo.InvariantCulture); var steps = facts.Count == 0 ? "" : $" [{string.Join(", ", facts.Select(fact => fact.Fact))}]"; @@ -628,7 +690,7 @@ internal static void BranchedCallee(Writer lean, Extraction extraction, int inde $"{name}Contract.post of the returned value{Where()}"); return; } - else if (op.StartsWith(".throw ", StringComparison.Ordinal)) + else if (op is StackOperation { Kind: Code.Throw }) { lean.Comment($"{indent}-- ", $"{indent}-- ", $"`{callee.Name}` throws at " + $"{ProofOutline.Location(callee, pc)}{Where()}, which its precondition must rule out"); @@ -636,20 +698,19 @@ internal static void BranchedCallee(Writer lean, Extraction extraction, int inde lean.Line($"{indent}cil_loop_obligation [{name}Contract]"); return; } - else if (Body.Target(op) is { } target && !Body.Conditional(op)) - { - pc = target; - continue; - } - else if (Body.Conditional(op)) + else if (extraction.Emitted[index].BranchAt(pc) is { } test) { - var taken = Body.Target(op)!.Value; - var opName = Regex.Match(op, @"\A\.(\w+)").Groups[1].Value; - var arity = opName is "brzero" or "brnonzero" ? 1 : 2; + var taken = test.Target; + if (test.Kind == ILOpCode.Br) + { + pc = taken; + continue; + } + var arity = Body.BranchOperands(test.Kind); var operands = stack.TakeLast(arity).ToArray(); var rest = stack.Take(stack.Count - arity).ToList(); - var (condition, takenWhenTrue) = Body.Test(opName, [.. operands.Select(operand => operand.Term)], - operands[0].Bits); + var terms = operands.Select(operand => new Body.Operand(operand.Term, operand.Numeral)).ToArray(); + var (condition, takenWhenTrue) = Body.Test(test.Kind, terms, operands[0].Bits); var fact = $"branch{facts.Count + 1}"; lean.Line($"{indent}by_cases {fact} : {condition}"); foreach (var holds in new[] { true, false }) @@ -669,16 +730,6 @@ internal static void BranchedCallee(Writer lean, Extraction extraction, int inde static string Variable(string name) => Regex.IsMatch(name, @"\A[A-Za-z][A-Za-z0-9_]*\z") && !Reserved.Contains(name) ? name : "argument"; - // A long signature literal continues on the next line through a Lean string gap. - static string Quoted(string text, int column) - { - const int Room = 96; - if (column + text.Length + 2 <= Room) return $"\"{text}\""; - var cut = text.LastIndexOf(',', Math.Min(text.Length - 1, Room - column - 3)); - if (cut < 0) cut = text.IndexOf('('); - return $"\"{text[..(cut + 1)]}\\\n {text[(cut + 1)..]}\""; - } - IEnumerable<(string Name, string Type)> EntryBinders => (spec.Symbolic ? new[] { ("memory", "Memory") } : []) .Concat(spec.Parameters.Select(parameter => (parameter.Name, parameter.Lean))) @@ -732,10 +783,10 @@ string FrameArguments(Func view, string memory) => /// Each slot's frame entry at the loop head, from the state values at `root`. string[] SlotTerms(string root) => slots.Select(slot => slot.Home is { } home - ? $".bytes {(slot.Kind.Contains(' ') ? $"({slot.Kind})" : slot.Kind)} {home}" + ? $".bytes {(slot.Kind is LocalKind.Aggregate ? $"({slot.Kind.ToLean()})" : slot.Kind.ToLean())} {home}" : slot.Components[0].Part == Part.Object ? $".object (some {Projection(root, components.IndexOf(slot.Components[0]), components.Count)})" - : $"{slot.Kind} " + + : $"{slot.Kind.ToLean()} " + $"(some {Projection(root, components.IndexOf(slot.Components[0]), components.Count)}.view)") .ToArray(); @@ -757,7 +808,7 @@ void StateAndHeader(Writer lean) slot.Components.Select(field => $"{ScalarConstructor(field.Width)} {project(field)}")) + "]))", _ => "none" }).ToArray(); - var kinds = string.Join(", ", slots.Select(slot => slot.Kind)); + var kinds = string.Join(", ", slots.Select(slot => slot.Kind.ToLean())); lean.Wrapped($" FrameState {(Writes ? "written" : "[]")} {Memory}", [$"[{kinds}]", ValuesType], ""); var slotsUse = slotTerms.Any(term => term.Contains("values")) ? "values" : "_"; var knownUse = remembered.Any(term => term.Contains("values")) ? "values" : "_"; @@ -779,9 +830,7 @@ void StateAndHeader(Writer lean) void LoopHeader(Writer lean) { var suffix = Suffix(active); - var latchOp = body.Ops[loop.Latch]; - var opName = Regex.Match(latchOp, @"\A\.(\w+)").Groups[1].Value; - if (!Body.Conditional(latchOp)) + if (body.Method.BranchAt(loop.Latch) is not { Kind: not ILOpCode.Br } latch) throw Unsupported($"its loop at {Location(loop.Latch)} does not test a condition at its back edge"); string Projected(Component component) => Projection("state.values", components.IndexOf(component), components.Count); @@ -805,7 +854,8 @@ string Projected(Component component) => .Append($"(state : {StateType("memory")})"); lean.Line($"-- The cutpoint is the loop's back-edge test at {Location(loop.Latch)}."); lean.Wrapped($"bind_cutpoint header{suffix}", binders, ":="); - lean.Line($" Extracted.method0 at loop{suffix}.latch matching (· matches .{opName} _)"); + var mnemonic = latch.Kind.ToString().ToLowerInvariant(); + lean.Line($" Extracted.method0 at loop{suffix}.latch matching (· matches .{mnemonic} _)"); var frame = FrameArguments(component => Projection("state.values", components.IndexOf(component), components.Count) + ".view", "memory"); var entryFrame = $"entry.frame{(frame.Length > 0 ? " " + frame : "")}"; @@ -850,8 +900,8 @@ static string NarrowRead(string term, int width) { Unknown unknown => unknown.What, Element => "a span element before it is loaded", - SpanLength => "a span length outside the loop test", - LocalAddress or LocalValue => "a struct local as a whole", + SpanLengthValue => "a span length outside the loop test", + LocalAddressValue or LocalValue => "a struct local as a whole", ReferenceArgument reference => $"the reference `{reference.Name}`", SpanArgument span => $"the span `{span.Span.Name}`", SpannedArray over => $"a span over the array `{over.Array.Name}`", @@ -862,7 +912,7 @@ static string NarrowRead(string term, int width) { Number number => number.Stacked ?? $".scalar ({ScalarConstructor(number.Width)} {number.Term})", ObjectTerm reference => $".object {reference.Term}", - SpanLength length => + SpanLengthValue length => $".scalar (.i32 (BitVec.ofNat 32 {((SpanTerm)state.State[length.Component]).View}.length))", _ => throw Unsupported($"the loop test at {Location(loop.Latch)} reads a value the outline cannot name") }; @@ -879,9 +929,12 @@ List Guard(PathState state) if (Step(state) != Event.Continue) throw Unsupported( $"the loop test at {Location(loop.Latch)} is not a straight block ({stopReason})"); - var arity = Regex.IsMatch(body.Ops[loop.Latch], @"\A\.br(non)?zero ") ? 1 : 2; + var arity = body.Method.BranchAt(loop.Latch) is { Kind: ILOpCode.Brfalse or ILOpCode.Brtrue } ? 1 : 2; if (state.Stack.Count != arity) throw Unsupported($"the loop test at {Location(loop.Latch)} uses values from before its block"); + if (state.Stack is [Number left, Number right] && left.Width != right.Width) + throw Unsupported($"its loop at {Location(loop.Latch)} tests operands of different widths at " + + "its back edge, which the outline does not step"); return state.Stack; } @@ -940,7 +993,7 @@ void Proof(Writer lean) var checkedArguments = !spec.Symbolic ? "rfl" : WordArrays.Any() ? $"(by cil_checked_side [{string.Join(", ", formed)}])" : "(by simp [cil_checked" + string.Concat(formed.Select(fact => ", " + fact)) + "])"; - lean.Wrapped(" refine invoke_loop_satisfies", ["Extracted.lookup0", checkedArguments, "setup", + lean.Wrapped(" refine invoke_loop_satisfies", ["Extracted.program_method0", checkedArguments, "setup", HeaderTerm(), $"(invariant {Arguments})", $"(measure {Arguments})", "(fun start => ?_)", "?_"], ""); lean.Wrapped(" · have entryFrame := entryFrame", [Writes ? $"(footprint {Arguments})" : "[]"], ""); @@ -951,7 +1004,7 @@ void Proof(Writer lean) Part.Span when slots[component.Slot].Argument => EntrySpan(component.Span!), Part.Span => new Unknown($"the span local `{component.Name}` before it is assigned"), Part.Object => new ObjectTerm(".null"), - _ => (object)new Number("0", component.Width, Literal: true) + _ => (object)new Number("0", component.Width, Value: 0) }).ToArray(), // Each step reading an array's length checks the array, valid in the entry's memory. Facts = [.. Indexed.Select(array => array.Name + "Valid")] @@ -1066,9 +1119,9 @@ void EntryPath(Writer lean, PathState state) var pc = state.Pc; var happened = Step(state); if (happened == Event.Continue) continue; - var what = happened == Event.Stop ? stopReason : "a branch or call"; + var what = happened == Event.Stop ? stopReason : $"a branch or call at {Location(pc)}"; lean.Noted(" ", "sorry", - $"{NotOutlined}the entry path meets {what} at {Location(pc)}; step it to the loop by hand"); + $"{NotOutlined}the entry path meets {what}; step it to the loop by hand"); return; } if (Values(state) is not { } values) @@ -1149,14 +1202,14 @@ void Walk(Writer lean, PathState state, string indent) case Event.Continue: continue; case Event.Stop: - Hole(lean, indent, state, $"{stopReason} at {Location(pc)}"); + Hole(lean, indent, state, stopReason); return; case Event.Return: lean.Line($"{indent}cil_loop_finish{Facts(state)}"); spec.Postcondition(lean, indent, $"Postcondition at the return at {Location(pc)}"); return; case Event.Call: - if (!Call(lean, state, indent, pc)) return; + if (!EnterCall(lean, state, indent, pc)) return; continue; case Event.Branch: Branch(lean, state, indent, $"branch{++branches}"); @@ -1165,11 +1218,11 @@ void Walk(Writer lean, PathState state, string indent) } } - bool Call(Writer lean, PathState state, string indent, int pc) + bool EnterCall(Writer lean, PathState state, string indent, int pc) { - var call = Regex.Match(body.Ops[pc], @"\A\.call(?:Instance)? (\d+) (\d+)\z"); - var callee = int.Parse(call.Groups[1].Value); - var count = int.Parse(call.Groups[2].Value); + var call = body.Method.CallAt(pc) ?? throw new InvalidOperationException("Expected a call at this site"); + var callee = call.Method; + var count = call.Arguments; var arguments = state.Stack.TakeLast(count).ToArray(); state.Stack.RemoveRange(state.Stack.Count - count, count); var stores = Stores(callee) is not null; @@ -1240,10 +1293,12 @@ string Passed(Value argument) => void Branch(Writer lean, PathState state, string indent, string fact) { var pc = state.Pc; - var op = body.Ops[pc]; - var opName = Regex.Match(op, @"\A\.(\w+)").Groups[1].Value; - var target = Body.Target(op)!.Value; - var unary = opName is "brzero" or "brnonzero"; + var jump = body.Method.BranchAt(pc) ?? + throw new InvalidOperationException("Expected a branch at this site"); + var target = jump.Target; + var zero = jump.Kind == ILOpCode.Brfalse; + var nonzero = jump.Kind == ILOpCode.Brtrue; + var unary = zero || nonzero; var operands = state.Stack.TakeLast(unary ? 1 : 2).ToArray(); state.Stack.RemoveRange(state.Stack.Count - operands.Length, operands.Length); state.GoalPc = pc; @@ -1254,12 +1309,12 @@ PathState Side(int next, string? known) if (known is not null) side.Facts.Add(known); return side; } - if (unary && operands[0] is SpanLength length && state.State[length.Component] is SpanTerm + if (unary && operands[0] is SpanLengthValue length && state.State[length.Component] is SpanTerm { Caseable: true } span) { var name = components[length.Component].Name; - var empty = Side(opName == "brzero" ? target : pc + 1, null); - var cons = Side(opName == "brzero" ? pc + 1 : target, null); + var empty = Side(zero ? target : pc + 1, null); + var cons = Side(zero ? pc + 1 : target, null); cons.Obtained.Clear(); lean.Line($"{indent}cases {span.Remaining} with"); // A written span splits on its length; its elements are never read. @@ -1288,15 +1343,15 @@ PathState Side(int next, string? known) } if (unary && operands[0] is Number { Literal: true } literal) { - var truth = literal.Term != "0"; - state.Pc = (opName == "brnonzero") == truth ? target : pc + 1; + var truth = literal.Value != 0; + state.Pc = nonzero == truth ? target : pc + 1; Walk(lean, state, indent); return; } if (unary && operands[0] is ObjectTerm reference) { - NullTest(lean, state, indent, reference.Term, opName == "brnonzero" ? target : pc + 1, - opName == "brnonzero" ? pc + 1 : target); + NullTest(lean, state, indent, reference.Term, nonzero ? target : pc + 1, + nonzero ? pc + 1 : target); return; } // Identity of two object references: the object locals and caller instances the walk names. @@ -1307,10 +1362,10 @@ ReferenceArgument argument when spec.Parameters.Any(parameter => parameter.Name parameter.Shape == ParameterShape.Object) => $"(.address {argument.Name})", _ => null }).ToArray(); - if (opName is "beq" or "bne" && identities.All(term => term is not null)) + if (jump.Kind is ILOpCode.Beq or ILOpCode.Bne_un && identities.All(term => term is not null)) { var (identity, equalTaken) = - Body.Test(opName, [$"({identities[0]} : ManagedReference)", identities[1]!], 64); + Body.Test(jump.Kind, [new($"({identities[0]} : ManagedReference)"), new(identities[1]!)], 64); var same = Side(equalTaken ? target : pc + 1, fact); var different = Side(equalTaken ? pc + 1 : target, fact); lean.Line($"{indent}by_cases {fact} : {identity}"); @@ -1325,8 +1380,14 @@ ReferenceArgument argument when spec.Parameters.Any(parameter => parameter.Name Hole(lean, indent, state, $"the test at {Location(pc)} reads {Unnamed(unnamed)}"); return; } - var terms = operands.Select(operand => ((Number)operand).Term).ToArray(); - var (condition, takenWhenTrue) = Body.Test(opName, terms, 8 * ((Number)operands[0]).Width); + if (operands.Cast().Any(number => number.Width != ((Number)operands[0]).Width)) + { + Hole(lean, indent, state, $"the test at {Location(pc)} reads operands of different widths"); + return; + } + var terms = operands.Select(operand => (Number)operand) + .Select(number => new Body.Operand(number.Term, number.Value)).ToArray(); + var (condition, takenWhenTrue) = Body.Test(jump.Kind, terms, 8 * ((Number)operands[0]).Width); var holds = Side(takenWhenTrue ? target : pc + 1, fact); var fails = Side(takenWhenTrue ? pc + 1 : target, fact); lean.Line($"{indent}by_cases {fact} : {condition}"); @@ -1378,7 +1439,10 @@ string Describe(int pc) => pc == loop.Head ? "another iteration" /// Evaluate the operation at `state.Pc`, advancing it unless the walk must act. Event Step(PathState state) { - var op = body.Ops[state.Pc]; + var op = body.Operations[state.Pc]; + var site = method.Body.Instructions[state.Pc]; + var scalarStore = ScalarStore(body.Method, state.Pc, site, extraction.Architecture); + var displacement = op is Unoutlined ? UnsafeModels.Offset(site, extraction.Architecture) : null; var stack = state.Stack; Value Pop() { @@ -1387,22 +1451,21 @@ Value Pop() stack.RemoveAt(stack.Count - 1); return top; } - Event Stop(string reason) + Event Stop(string reason, bool showInstruction = false) { - stopReason = reason; + stopReason = StopAt(method, state.Pc, reason, showInstruction); return Event.Stop; } - Match match; - if ((match = Regex.Match(op, @"\A\.local (\d+)\z")).Success) + if (op is Unoutlined && InstructionOperands.LoadSlot(method, site, homes) is int loaded) { - var slot = slots[int.Parse(match.Groups[1].Value)]; + var slot = slots[loaded]; var first = components.IndexOf(slot.Components[0]); stack.Add(slot.Components[0].Part is Part.Word or Part.Object ? (Value)state.State[first] : new LocalValue(slot.Index)); } - else if ((match = Regex.Match(op, @"\A\.setLocal (\d+)\z")).Success) + else if (op is Unoutlined && InstructionOperands.StoreSlot(method, site, homes) is int assigned) { - var slot = slots[int.Parse(match.Groups[1].Value)]; + var slot = slots[assigned]; var first = components.IndexOf(slot.Components[0]); var value = Pop(); state.Moved = true; @@ -1423,14 +1486,12 @@ Event Stop(string reason) state.State[first] = EntrySpan(source); else return Stop("a store of a value the outline cannot name"); } - else if ((match = Regex.Match(op, @"\A\.arg (\d+)\z")).Success) + else if (body.Method.ArgumentAt(state.Pc) is int argument && MethodArguments.Plain(spec.Method, argument)) { - var parameter = spec.Parameters[int.Parse(match.Groups[1].Value)]; + var parameter = spec.Parameters[argument]; stack.Add(parameter.Shape switch { ParameterShape.Scalar => new Number(parameter.Name, parameter.Width), - ParameterShape.Boolean => new Unknown($"the `bool` argument `{parameter.Name}`"), - ParameterShape.Narrow => new Unknown($"the narrow argument `{parameter.Name}`"), ParameterShape.Reference or ParameterShape.Object => new ReferenceArgument(parameter.Name), ParameterShape.WordArray => new WordArrayArgument(parameter), _ => new SpanArgument(parameter) @@ -1438,79 +1499,96 @@ Event Stop(string reason) } // The span a local takes over a whole array argument before the loop, on the entry path, where the // entry frame carries the array's validity past the frame's allocations. - else if (op.StartsWith(".memory (.arraySpan ", StringComparison.Ordinal) && stack.Count > 0 && - stack[^1] is WordArrayArgument { Array: var whole } && spanned.Contains(whole)) + else if (op is Unoutlined && stack.Count > 0 && + stack[^1] is WordArrayArgument { Array: var whole } && spanned.Contains(whole) && + Spans.ArrayView(site, extraction.Architecture, wholeOnly: true) is { } view && + view.Writable == whole.Writable && view.Element == whole.Element) { stack.RemoveAt(stack.Count - 1); stack.Add(new SpannedArray(whole)); state.Facts.Add(whole.Name + "Spanned"); } - else if ((match = Regex.Match(op, @"\A\.const(32|64) (.+)\z")).Success) + else if (body.Method.ConstantAt(state.Pc) is { Width: 32 or 64 } constant) { - var literal = match.Groups[2].Value; - var width = match.Groups[1].Value == "64" ? 8 : 4; - stack.Add(new Number(literal, width, Literal: Regex.IsMatch(literal, @"\A\d+\z"))); + var width = constant.Width == 64 ? 8 : 4; + stack.Add(new Number(ConstantTerm(constant), width, Value: constant.Numeral ? constant.Value : null)); } - else if ((match = Regex.Match(op, @"\A\.(add|sub|mul|band|bor|bxor)\z")).Success) + else if (ReferenceStride(op, displacement) is int stride && current is not null && + !state.Called && stack.Count >= 2 && + stack[^1] is Number { Width: 4, Narrow: false } referenceIndex && + stack[^2] is ReferenceArgument reference && + spec.Parameters.Any(parameter => parameter.Name == reference.Name && + parameter.Shape == ParameterShape.Reference)) + { + stack.RemoveRange(stack.Count - 2, 2); + stack.Add(new AddedReference(reference.Name, referenceIndex.Term, stride)); + } + else if (op is StackOperation { Kind: Code.Add } && stack.Count >= 2 && + (stack[^1] is ReferenceArgument or AddedReference || + stack[^2] is ReferenceArgument or AddedReference)) + return Stop("a reference addition other than a reference argument followed by a 32-bit index " + + "before a call in the iteration"); + else if (Arithmetic(op) is { } symbol) { var right = Pop(); var left = Pop(); - var symbol = match.Groups[1].Value switch - { - "add" => "+", "sub" => "-", "mul" => "*", "band" => "&&&", "bor" => "|||", _ => "^^^" - }; if (left is Number { Narrow: false } a && right is Number { Narrow: false } b && a.Width == b.Width) stack.Add(new Number($"({a.Term} {symbol} {b.Term})", a.Width)); else stack.Add(new Unknown("arithmetic on a value the outline cannot name")); } - else if (op == ".convI8" || op == ".convU8") + else if (op is StackOperation { Kind: Code.Conv_I8 or Code.Conv_I or Code.Conv_U8 }) { var value = Pop(); stack.Add(value is Number { Literal: true, Width: 4 } literal ? literal with { Width = 8 } : new Unknown("a widened value")); } - else if ((match = Regex.Match(op, @"\A\.localAddr (\d+)\z")).Success) - stack.Add(new LocalAddress(int.Parse(match.Groups[1].Value))); - else if ((match = Regex.Match(op, @"\A\.initAggregate \d+\z")).Success) + // A span local's address pushes the view it holds, as loading the local does. + else if (op is Unoutlined && InstructionOperands.AddressSlot(method, site, homes) is int addressed) + stack.Add(slots[addressed].Components[0].Part == Part.Span + ? new LocalValue(addressed) : new LocalAddressValue(addressed)); + else if (body.Method.ObjectAt(state.Pc, Code.Initobj) is not null) { - if (Pop() is not LocalAddress address) return Stop("zeroing storage other than a struct local"); + if (Pop() is not LocalAddressValue address) return Stop("zeroing storage other than a struct local"); state.Moved = true; foreach (var field in slots[address.Slot].Components) - state.State[components.IndexOf(field)] = new Number("0", field.Width, Literal: true); + state.State[components.IndexOf(field)] = new Number("0", field.Width, Value: 0); } - else if ((match = Regex.Match(op, @"\A\.fieldAt (\d+) (\d+)\z")).Success) + else if (body.Method.StructFieldAt(state.Pc) is { Opcode: Code.Ldfld, IsAggregate: false, + Signed: false, Offset: var loadOffset }) { var source = Pop(); var slot = source switch { - LocalValue value => value.Slot, LocalAddress address => address.Slot, _ => -1 + LocalValue value => value.Slot, LocalAddressValue address => address.Slot, _ => -1 }; var field = slot < 0 ? null : slots[slot].Components.FirstOrDefault(component => - component.Offset == int.Parse(match.Groups[1].Value)); + component.Offset == loadOffset); if (field is null) return Stop("a field read outside a struct local"); var held = (Value)state.State[components.IndexOf(field)]; // A narrow field loads its low bytes zero-extended, as `narrowLoad` reads the word it holds. stack.Add(field.Width < 4 && held is Number { Literal: false } word ? new Number(NarrowRead(word.Term, field.Width), 4) : held); } - else if ((match = Regex.Match(op, @"\A\.setFieldAt (\d+) (\d+)\z")).Success) + else if (body.Method.StructFieldAt(state.Pc) is { Opcode: Code.Stfld, IsAggregate: false, + Offset: var storeOffset }) { var value = Pop(); - var field = Pop() is LocalAddress address ? slots[address.Slot].Components.FirstOrDefault(component => - component.Offset == int.Parse(match.Groups[1].Value)) : null; + var field = Pop() is LocalAddressValue address + ? slots[address.Slot].Components.FirstOrDefault(component => + component.Offset == storeOffset) : null; if (field is null || value is not (Number or Unknown)) return Stop("a field store outside a struct local"); state.Moved = true; state.State[components.IndexOf(field)] = value is Number { Literal: false } number && field.Width < 4 ? number with { Narrow = true } : value; } - else if (op == ".memory .spanLength") + else if (op is Unoutlined && Spans.MemberWidth(site, "get_Length", 0, extraction.Architecture) is not null) { if (Pop() is not LocalValue value || slots[value.Slot].Components[0].Part != Part.Span) return Stop("the length of a span the outline does not follow"); - stack.Add(new SpanLength(components.IndexOf(slots[value.Slot].Components[0]))); + stack.Add(new SpanLengthValue(components.IndexOf(slots[value.Slot].Components[0]))); } - else if (op == ".memory (.spanSlice false)") + else if (op is Unoutlined && Spans.MemberWidth(site, "Slice", 1, extraction.Architecture) is not null) { var count = Pop(); var source = Pop(); @@ -1521,7 +1599,7 @@ Event Stop(string reason) PendingTail(state, component, span); stack.Add(new Sliced(component)); } - else if ((match = Regex.Match(op, @"\A\.memory \(\.spanItem (\d+)\)\z")).Success) + else if (op is Unoutlined && Spans.MemberWidth(site, "get_Item", 1, extraction.Architecture) is not null) { var index = Pop(); var source = Pop(); @@ -1530,7 +1608,8 @@ Event Stop(string reason) return Stop("an element other than the first of a span known to be nonempty"); stack.Add(new Element(components.IndexOf(slots[value.Slot].Components[0]))); } - else if (op is ".loadScalar 4" or ".load64" or ".loadScalar 8" && stack.Count > 0 && stack[^1] is Element) + else if (stack.Count > 0 && stack[^1] is Element reading && + ReadsWidth(body.Method, state.Pc, site, extraction.Architecture, components[reading.Component].Width)) { var element = (Element)Pop(); var span = (SpanTerm)state.State[element.Component]; @@ -1540,38 +1619,40 @@ Event Stop(string reason) PendingHead(state, element.Component, span); stack.Add(new Number(span.Head!, components[element.Component].Width)); } - else if (op == ".memory .arrayLength") + else if (op is StackOperation { Kind: Code.Ldlen }) { if (Pop() is not WordArrayArgument array) return Stop("the length of an array the outline does not follow"); - stack.Add(new ArrayLength(array.Array)); + stack.Add(new ArrayLengthValue(array.Array)); } // `ldlen` pushes the length as a 64-bit word, which a test of an `int` index narrows; a test reads // the narrowed word simplified. - else if (op == ".convI4" && stack.Count > 0 && stack[^1] is ArrayLength length) + else if (op is StackOperation { Kind: Code.Conv_I4 } && stack.Count > 0 + && stack[^1] is ArrayLengthValue length) { stack.RemoveAt(stack.Count - 1); var count = length.Array.Name + "Length"; stack.Add(new Number($"(BitVec.ofNat 32 {count})", 4, Stacked: $".scalar (.i32 ((BitVec.ofNat 64 {count}).setWidth 32))")); } - else if ((match = Regex.Match(op, @"\A\.memory \(\.arrayLoad (\d+) false\)\z")).Success && + else if (Arrays.Numeric(op, method, state.Pc) is { Store: false, Signed: false } arrayLoad && stack.Count >= 2 && stack[^2] is WordArrayArgument read) { if (stack[^1] is not Number { Width: 4, Narrow: false } index || current is null || - int.Parse(match.Groups[1].Value) != read.Array.Width) + arrayLoad.Width != read.Array.Width) return Stop("an array read the outline cannot name at an index and the element's width"); if (read.Array.Writable) return Stop("a read of the array the loop stores into, whose contents the frame does not carry"); if (state.Moved || state.Called || !state.Obtained.Add(read.Array.Name + "Found")) return Stop("an array read after a store, a call or another read of the iteration"); stack.RemoveRange(stack.Count - 2, 2); - stack.Add(new Number(ElementRead(read.Array, index.Term, state.Facts), read.Array.Width)); + stack.Add(new Number(ElementRead(read.Array, arrayLoad, index.Term, state.Facts), + read.Array.Width)); } - else if ((match = Regex.Match(op, @"\A\.memory \(\.arrayStore (\d+)\)\z")).Success && + else if (Arrays.Numeric(op, method, state.Pc) is { Store: true } arrayStore && stack.Count >= 3 && stack[^3] is WordArrayArgument stores) { - var width = int.Parse(match.Groups[1].Value); + var width = arrayStore.Width; if (stack[^2] is not Number { Width: 4, Narrow: false } index || stack[^1] is not Number { Narrow: false } stored || stored.Width != width || current is null) return Stop("an array store the outline cannot name at an index and the element's width"); @@ -1579,32 +1660,22 @@ Event Stop(string reason) return Stop("an array store after another store or a call of the iteration, whose memory the " + "array's checks are not carried to"); stack.RemoveRange(stack.Count - 3, 3); - state.Facts.Add(ElementStore(stores.Array, index.Term, stored.Term)); + state.Facts.Add(ElementStore(stores.Array, arrayStore, index.Term, stored.Term)); state.Moved = true; } - else if (op == ".nop") { } - else if (Body.Target(op) is { } target && !Body.Conditional(op)) + else if (op.IsNop) { } + else if (body.Method.BranchAt(state.Pc) is { } branch) { - state.Pc = target; + if (branch.Kind != ILOpCode.Br) return Event.Branch; + state.Pc = branch.Target; return Event.Continue; } - else if (Body.Conditional(op)) return Event.Branch; - else if (op.StartsWith(".call ", StringComparison.Ordinal) || - op.StartsWith(".callInstance ", StringComparison.Ordinal)) return Event.Call; - else if (op == ".ret" || op.StartsWith(".ret (some ", StringComparison.Ordinal)) return Event.Return; - else if ((match = Regex.Match(op, @"\A\.memory \(\.add (\d+) true\)\z")).Success && current is not null && - !state.Called && stack.Count >= 2 && - stack[^1] is Number { Width: 4, Narrow: false } index && stack[^2] is ReferenceArgument reference && - spec.Parameters.Any(parameter => parameter.Name == reference.Name && - parameter.Shape == ParameterShape.Reference)) - { - stack.RemoveRange(stack.Count - 2, 2); - stack.Add(new AddedReference(reference.Name, index.Term, int.Parse(match.Groups[1].Value))); - } - else if (op is ".store64" or ".storeScalar 4" or ".storeScalar 8" && stack.Count >= 2 && + else if (body.Method.CallAt(state.Pc) is { Kind: not CallKind.Tail }) return Event.Call; + else if (op is StackOperation { Kind: Code.Ret }) return Event.Return; + else if (scalarStore is { Width: 4 or 8 } spanWrite && stack.Count >= 2 && stack[^2] is Element written && state.State[written.Component] is SpanTerm { Written: true }) { - var width = op == ".storeScalar 4" ? 4 : 8; + var width = spanWrite.Width; if (state.Moved || state.Called) return Stop("a span store after another store or a call of the iteration, whose memory the " + "span's checks are not carried to"); @@ -1612,25 +1683,31 @@ Event Stop(string reason) components[written.Component].Width != width) return Stop("a span store of a value the outline cannot name at the element's width"); stack.RemoveRange(stack.Count - 2, 2); - state.Facts.AddRange(SpanWrite(state, written.Component, stored.Term, StoreLemma(op))); + state.Facts.AddRange(SpanWrite(state, written.Component, stored.Term, spanWrite.Lemma)); state.Moved = true; } - else if (op is ".store64" or ".storeScalar 4" or ".storeScalar 8" && stack.Count >= 2 && + else if (scalarStore is { Width: 4 or 8 } elementWrite && stack.Count >= 2 && stack[^2] is AddedReference added && !state.Moved) { - var width = op == ".storeScalar 4" ? 4 : 8; + var width = elementWrite.Width; if (stack[^1] is not Number { Narrow: false } stored || stored.Width != width || added.Width != width) return Stop("a store through `Unsafe.Add` of a value the outline cannot name at the " + "element's width"); stack.RemoveRange(stack.Count - 2, 2); - state.Facts.AddRange(ElementWrite(added, stored.Term, StoreLemma(op))); + state.Facts.AddRange(ElementWrite(added, stored.Term, elementWrite.Lemma)); state.Moved = true; } - else if (Regex.IsMatch(op, @"\A\.memory \(\.add \d+ (true|false)\)\z")) + else if (displacement is not null) return Stop("an `Unsafe.Add` other than one of a reference argument by a 32-bit index, written " + "through before any other store of an iteration, which needs `add memory base width offset = " + ".ok reference` (such as from `Writable.add_int`) and write authority in `footprint`"); - else return Stop($"`{op}`, which the outline does not step"); + else if (body.Method.ArgumentAt(state.Pc) is int narrow) + { + var name = spec.Parameters[narrow].Name; + return Stop($", a read of the narrow argument `{name}`, which the outline does not step", + showInstruction: true); + } + else return Stop(", which the outline does not step", showInstruction: true); state.Pc++; return Event.Continue; } @@ -1692,7 +1769,7 @@ string[] ElementWrite(AddedReference added, string value, string lemma) /// Load element `index` of a read-only array at the iteration's first memory, its bound left to /// the author's definitions; adds the facts that step `ldelem` to `facts` and returns the loaded term. /// - string ElementRead(Parameter array, string index, List facts) + string ElementRead(Parameter array, Arrays.NumericAccess load, string index, List facts) { var name = array.Name; var position = IndexTerm(index); @@ -1712,7 +1789,8 @@ string ElementRead(Parameter array, string index, List facts) lean.Line($"{indent} ({name}Checked.within wellFormed frame{apart}).contents.array_load"); lean.Line($"{indent} (frame.caller_array_valid wellFormed {name}Valid) (by decide) {name}Bound"); lean.Line($"{indent}have {name}Step :="); - lean.Line($"{indent} step_arrayLoad_of_element (index := .i32 {index}) rfl {name}Found {name}Loaded"); + lean.Wrapped($"{indent} {load.LoadLemma}", $"(index := .i32 {index}) rfl {name}Found {name}Loaded".Terms(), + ""); // The step pushes the element through `numericOfWidth`, which these unfold to the element itself. facts.AddRange(new[] { name + "Step", "numericOfWidth", "BitVec.ofNat_toNat" } .Where(fact => !facts.Contains(fact))); @@ -1722,7 +1800,7 @@ string ElementRead(Parameter array, string index, List facts) /// Store `value` into element `index` of the written array at the iteration's first memory, /// its bound, authority and footprint left to the author's definitions; returns the fact that steps /// `stelem`. - string ElementStore(Parameter array, string index, string value) + string ElementStore(Parameter array, Arrays.NumericAccess store, string index, string value) { var name = array.Name; var width = array.Width; @@ -1745,9 +1823,9 @@ string ElementStore(Parameter array, string index, string value) lean.Line($"{indent}have {name}Frame := frame.write {name}Wrote"); lean.Line($"{indent} (by simpa [numberBytes_length] using {name}Within)"); lean.Line($"{indent} (by simpa [Reference.element_allocation] using {name}Below)"); - var lemma = width == 8 ? "step_arrayStore_i64_of_write" : "step_arrayStore_of_write"; lean.Line($"{indent}have {name}Step :="); - lean.Line($"{indent} {lemma} (index := .i32 {index}) rfl {name}Element {name}Wrote"); + lean.Wrapped($"{indent} {store.StoreLemma}", + $"(index := .i32 {index}) rfl {name}Element {name}Wrote".Terms(), ""); return name + "Step"; } diff --git a/Extractor/ProofOutlineObjects.cs b/Extractor/ProofOutlineObjects.cs index 0f754c867..d5da45252 100644 --- a/Extractor/ProofOutlineObjects.cs +++ b/Extractor/ProofOutlineObjects.cs @@ -1,5 +1,7 @@ +using ILOpCode = System.Reflection.Metadata.ILOpCode; using System.Text.RegularExpressions; using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -9,7 +11,7 @@ internal static partial class ProofOutline { /// A field an extraction names, as `Path.Name`: a word of `Width` bytes, an object reference, or the /// origin of a whole array of `Element`, a Lean `ArrayElement` term. - sealed record ViewField(string Path, string Name, ClassViews.FieldKind Kind, int? Width, string? Element, + sealed record ViewField(string Path, string Name, ClassViews.FieldKind Kind, int? Width, Arrays.Element? Element, int Offset, bool Stored, int[] Loaders, int[] Storers) { internal string Lean => Path + "." + Name; @@ -20,17 +22,22 @@ sealed record ViewField(string Path, string Name, ClassViews.FieldKind Kind, int sealed class FieldViews { readonly Dictionary classes = new(StringComparer.Ordinal); + readonly IReadOnlyDictionary<(int Method, int Pc), ClassViews.FieldOperation> fieldSites; internal FieldViews(Extraction extraction) { + fieldSites = extraction.ClassFields; foreach (var view in extraction.Views) classes[view.FullName] = [.. view.Fields.Select(field => new ViewField(view.Path, field.Lean, field.Kind, field.Width, field.Element, field.Offset, field.Storers.Length > 0, field.Loaders, field.Storers))]; } + /// The reachable field load or store used to generate the named class view at this site. + internal ClassViews.FieldOperation? FieldAt(int method, int pc) => fieldSites.GetValueOrDefault((method, pc)); + /// The short name of an array element some named field holds arrays of, or none. - internal string? ElementType(string element) => classes.Values.SelectMany(fields => fields) + internal string? ElementType(Arrays.Element element) => classes.Values.SelectMany(fields => fields) .FirstOrDefault(field => field.Kind == ClassViews.FieldKind.Array && field.Element == element) is { } named ? named.Lean + ".elementType" : null; @@ -89,7 +96,7 @@ static bool HasNumericObjectHomes(MethodDefinition method) => method.Body.Variab /// A scalar element read and the field footprints its contents must be disjoint from. sealed record WordRead(Parameter Array, string Index, int Stores, string Name, - (Access Write, string Apart)[] Writes, bool CallerIndex); + (Access Write, string Apart)[] Writes, bool CallerIndex, Arrays.NumericAccess Load); enum AccessKind { Load, Store, Read, Own } @@ -122,7 +129,8 @@ enum ElementKind { Length, Load, Store } /// the body reaches as `Target`, null, or a reference outside the model whose formation in the caller's memory /// `Formed` states. A `Reread` load reads back what an earlier store on its path wrote. sealed record ElementAccess(string Array, ElementKind Kind, int Stores, string? Index = null, - string? Value = null, Reached? Target = null, string? Formed = null, string? Slot = null, bool Reread = false) + string? Value = null, Reached? Target = null, string? Formed = null, string? Slot = null, bool Reread = false, + bool LiteralIndex = false) { /// The premise that the stored object passes the covariance check of the array's element class. /// @@ -182,17 +190,23 @@ sealed record Callee(int Index, string Name, Specification Spec, ObjectWalk Walk /// which objects a body with loops touches; its steps are not a proof. /// With `throws`, for a callee, a path may end in a throw, which its contract's `pre` must exclude. /// - sealed class ObjectWalk(Specification spec, string[] ops, FieldViews views, Func callees, - bool linear = false, bool numericHomes = false, Func? stubs = null, bool throws = false) + sealed class ObjectWalk(Specification spec, Method emitted, FieldViews views, Func callees, + IReadOnlyDictionary homes, bool linear = false, bool numericHomes = false, + Func? stubs = null, bool throws = false, + int methodIndex = 0) { internal string? Stopped { get; private set; } internal string? Returned { get; private set; } + + /// The constant the returned word spells, when the body returns one. + Word? returnedWord; internal List Objects { get; private init; } = []; internal List Steps { get; } = []; internal bool NumericHomes => numericHomes; abstract record Item; - sealed record Word(string? Term, int Width) : Item; + /// A named word. Bare numerals and negative literals retain their value for branch and array proofs. + sealed record Word(string? Term, int Width, long Literal = 0, bool Numeral = false) : Item; sealed record Instance(Reached Object) : Item; sealed record Null : Item; /// A reference to an object outside the model, as the field it was loaded from names it before any @@ -291,10 +305,10 @@ internal bool Run(Func location) bool Walk(int start, List stack, Dictionary locals, HashSet visited, Func location) { - for (var pc = start; pc < ops.Length; pc++) + for (var pc = start; pc < emitted.Operations.Length; pc++) { - var op = ops[pc]; - Match match; + var op = emitted.Operations[pc]; + var classField = views.FieldAt(methodIndex, pc); Item? Pop() { if (stack.Count == 0) return null; @@ -302,17 +316,18 @@ bool Walk(int start, List stack, Dictionary locals, HashSet (Next: next, Walk: new ObjectWalk(spec, ops, - views, callees, linear, numericHomes, stubs, throws) { Objects = Objects, Arrays = Arrays, + var walks = new[] { holds, fails }.Select(next => (Next: next, Walk: new ObjectWalk(spec, emitted, + views, callees, homes, linear, numericHomes, stubs, throws, methodIndex) + { Objects = Objects, Arrays = Arrays, Slots = Slots, Stored = Stored, ArrayApart = ArrayApart, stores = stores, fieldMemory = fieldMemory, calls = calls, splits = splits + 1, Fresh = Fresh, previous = [.. PathSteps] })).ToArray(); @@ -327,9 +342,10 @@ bool Branch(string condition, bool takenWhenTrue, int target) return true; } if (!visited.Add(pc)) return Stop("a backward jump"); - if ((match = Regex.Match(op, @"\A\.arg (\d+)\z")).Success) + var branchSite = emitted.BranchAt(pc); + if (emitted.ArgumentAt(pc) is int argument && MethodArguments.Plain(spec.Method, argument)) { - var parameter = spec.Parameters[int.Parse(match.Groups[1].Value)]; + var parameter = spec.Parameters[argument]; Item? item = parameter.Shape switch { ParameterShape.Object => new Instance(Objects.First(reached => reached.Name == parameter.Name)), @@ -344,7 +360,8 @@ bool Branch(string condition, bool takenWhenTrue, int target) if (item is Values loaded) Steps.Add(new ArrayUse(loaded.Array, stores)); stack.Add(item); } - else if ((match = Regex.Match(op, @"\A\.setLocal (\d+)\z")).Success) + else if (op is Unoutlined && InstructionOperands.StoreSlot(spec.Method, + spec.Method.Body.Instructions[pc], homes) is int storedSlot) { if (Pop() is not { } item) return Stop("a local store of an empty stack"); // An array home checks the view it takes and the view it gives back. @@ -353,116 +370,121 @@ bool Branch(string condition, bool takenWhenTrue, int target) { if (item is not Word { Term: { } value } word) return Stop("a numeric local store whose value the outline cannot name"); - Steps.Add(new LocalAccess(int.Parse(match.Groups[1].Value), value, word.Width, stores++, true)); + Steps.Add(new LocalAccess(storedSlot, value, word.Width, stores++, true)); } - locals[int.Parse(match.Groups[1].Value)] = item; + locals[storedSlot] = item; } - else if ((match = Regex.Match(op, @"\A\.local (\d+)\z")).Success) + else if (op is Unoutlined && InstructionOperands.LoadSlot(spec.Method, + spec.Method.Body.Instructions[pc], homes) is int loadedSlot) { - if (!locals.TryGetValue(int.Parse(match.Groups[1].Value), out var item)) + if (!locals.TryGetValue(loadedSlot, out var item)) return Stop("a local the body reads before it stores it"); if (item is Values loaded) Steps.Add(new ArrayUse(loaded.Array, stores)); if (numericHomes && item is Word { Term: { } value } word) - Steps.Add(new LocalAccess(int.Parse(match.Groups[1].Value), value, word.Width, stores, false)); + Steps.Add(new LocalAccess(loadedSlot, value, word.Width, stores, false)); stack.Add(item); } - else if (op == ".memory .nullObject") stack.Add(new Null()); - else if (op == ".dup") + else if (op is StackOperation { Kind: Code.Ldnull }) stack.Add(new Null()); + else if (op is StackOperation { Kind: Code.Dup }) { if (stack.Count == 0) return Stop("a duplicate of an empty stack"); stack.Add(stack[^1]); } - else if (op == ".pop") { if (Pop() is null) return Stop("a pop of an empty stack"); } - else if (linear && Regex.Match(op, @"\A\.(\w+) \d+\z") is { Success: true } jump && - Body.Target(op) is not null) + else if (op is StackOperation { Kind: Code.Pop }) { - var operands = jump.Groups[1].Value switch { "branch" => 0, "brzero" or "brnonzero" => 1, _ => 2 }; - for (var operand = 0; operand < operands; operand++) - if (Pop() is null) return Stop($"`{op}` on an empty stack"); + if (Pop() is null) return Stop("a pop of an empty stack"); } - else if ((match = Regex.Match(op, @"\A\.(brzero|brnonzero) (\d+)\z")).Success && + else if (linear && branchSite is { } jump) + { + for (var operand = 0; operand < Body.BranchOperands(jump.Kind); operand++) + if (Pop() is null) return Stop(" on an empty stack", showInstruction: true); + } + else if (branchSite is { Kind: ILOpCode.Brfalse or ILOpCode.Brtrue } nullTest && stack.Count > 0 && stack[^1] is Opaque) { var tested = (Opaque)Pop()!; if (stores > 0) return Stop(Unformed(tested)); // `brfalse` takes its target when the reference is null. - return Branch($"{tested.Term[1..^1]} = .null", match.Groups[1].Value == "brzero", - int.Parse(match.Groups[2].Value)); + return Branch($"{tested.Term[1..^1]} = .null", nullTest.Kind == ILOpCode.Brfalse, nullTest.Target); } - else if ((match = Regex.Match(op, @"\A\.(beq|bne) (\d+)\z")).Success && stack.Count > 1 && + else if (branchSite is { Kind: ILOpCode.Beq or ILOpCode.Bne_un } equalityTest && stack.Count > 1 && (stack[^1] is Opaque || stack[^2] is Opaque)) { var right = Pop(); var left = Pop(); - if (Compared(left, right) is not { } condition) return Stop(Uncompared(op)); - return Branch(condition, match.Groups[1].Value == "beq", int.Parse(match.Groups[2].Value)); + if (Compared(left, right) is not { } condition) return Stop(Uncompared, showInstruction: true); + return Branch(condition, equalityTest.Kind == ILOpCode.Beq, equalityTest.Target); } - else if (op is ".eq" or ".gtu" && stack.Count > 1 && (stack[^1] is Opaque || stack[^2] is Opaque)) + else if (op is StackOperation { Kind: Code.Ceq or Code.Cgt_Un } comparison + && stack.Count > 1 && + (stack[^1] is Opaque || stack[^2] is Opaque)) { var right = Pop(); var left = Pop(); // `cgt.un` on references is defined only against null. - if (Compared(left, right) is null || op == ".gtu" && right is not Null) - return Stop(Uncompared(op)); + if (Compared(left, right) is null || comparison.Kind == Code.Cgt_Un && right is not Null) + return Stop(Uncompared, showInstruction: true); stack.Add(new Word(null, 4)); } - else if ((match = Regex.Match(op, @"\A\.(brzero|brnonzero) (\d+)\z")).Success && + else if (branchSite is { Kind: ILOpCode.Brfalse or ILOpCode.Brtrue } objectTest && stack.Count > 0 && stack[^1] is not Word) { // An argument object is never null, so its test always goes one way. var zero = Pop() is Null; - if (zero == (match.Groups[1].Value == "brzero")) pc = int.Parse(match.Groups[2].Value) - 1; + if (zero == (objectTest.Kind == ILOpCode.Brfalse)) pc = objectTest.Target - 1; } - else if (Body.Conditional(op)) + else if (branchSite is { Kind: not ILOpCode.Br } test) { - var name = Regex.Match(op, @"\A\.(\w+)").Groups[1].Value; - var operands = new Word?[name is "brzero" or "brnonzero" ? 1 : 2]; + var operands = new Word?[Body.BranchOperands(test.Kind)]; for (var position = operands.Length - 1; position >= 0; position--) operands[position] = Pop() as Word; if (operands.Any(operand => operand?.Term is null) || operands.Any(operand => operand!.Width != operands[0]!.Width)) - return Stop($"`{op}` on a value the outline does not name"); - var (condition, takenWhenTrue) = - Body.Test(name, [.. operands.Select(operand => operand!.Term!)], 8 * operands[0]!.Width); - return Branch(condition, takenWhenTrue, Body.Target(op)!.Value); + return Stop(" on a value the outline does not name", showInstruction: true); + var terms = operands.Select(operand => new Body.Operand(operand!.Term!, + operand.Numeral ? operand.Literal : null)).ToArray(); + var (condition, takenWhenTrue) = Body.Test(test.Kind, terms, 8 * operands[0]!.Width); + return Branch(condition, takenWhenTrue, test.Target); } - else if ((match = Regex.Match(op, @"\A\.branch (\d+)\z")).Success) - pc = int.Parse(match.Groups[1].Value) - 1; - else if ((match = Regex.Match(op, - @"\A\.const(32|64) (?:(\d+)|\(BitVec\.ofInt \d+ \((-?\d+)\)\))\z")).Success) - stack.Add(new Word(match.Groups[2].Success ? match.Groups[2].Value : $"({match.Groups[3].Value})", - match.Groups[1].Value == "64" ? 8 : 4)); - else if ((match = Regex.Match(op, @"\A\.(add|sub|mul|band|bor|bxor)\z")).Success) + else if (branchSite is { Kind: ILOpCode.Br } branch) pc = branch.Target - 1; + else if (emitted.ConstantAt(pc) is { Width: 32 or 64 } constant) + stack.Add(new Word(constant.Numeral ? $"{constant.Value}" : $"({constant.Value})", + constant.Width == 64 ? 8 : 4, constant.Value, constant.Numeral)); + else if (op is StackOperation { IsArithmetic: true } arithmetic) { if (Pop() is not Word right || Pop() is not Word left || left.Width != right.Width) return Stop("arithmetic on a value the outline cannot name"); - var symbol = match.Groups[1].Value switch - { - "add" => "+", "sub" => "-", "mul" => "*", "band" => "&&&", "bor" => "|||", _ => "^^^" - }; + var symbol = arithmetic.Symbol; stack.Add(new Word(left.Term is null || right.Term is null ? null : $"({left.Term} {symbol} {right.Term})", left.Width)); } - else if (op is ".convU8" or ".convI8") + else if (op is StackOperation { Kind: Code.Conv_U8 or Code.Conv_I8 or Code.Conv_I } + widening) { if (Pop() is not Word { Width: 4 } word) return Stop("a widening of a value the outline cannot name"); - var widen = op == ".convU8" ? "setWidth" : "signExtend"; + var widen = widening.Kind == Code.Conv_U8 ? "setWidth" : "signExtend"; stack.Add(new Word(word.Term is null ? null : $"({word.Term}.{widen} 64)", 8)); } // Values the steps compute but the outline does not name: they may be returned, not stored. - else if (Regex.IsMatch(op, @"\A\.(shl|shr|shrUn|div|divUn|rem|remUn|eq|gt|gtu|lt|ltu)\z")) + else if (op is StackOperation { Kind: Code.Shl or Code.Shr or Code.Shr_Un + or Code.Ceq or Code.Cgt or + Code.Cgt_Un or Code.Clt or Code.Clt_Un } unnamed) { if (Pop() is not Word || Pop() is not Word left) - return Stop($"`{op}` on a value that is not a word"); - stack.Add(new Word(null, op is ".eq" or ".gt" or ".gtu" or ".lt" or ".ltu" ? 4 : left.Width)); + return Stop(" on a value that is not a word", showInstruction: true); + stack.Add(new Word(null, unnamed.Kind is Code.Ceq or Code.Cgt or Code.Cgt_Un + or Code.Clt or Code.Clt_Un + ? 4 : left.Width)); } - else if ((match = Regex.Match(op, @"\A\.(neg|not|conv[UI][124])\z")).Success) + else if (op is StackOperation { Kind: Code.Neg or Code.Conv_I1 or Code.Conv_I2 + or Code.Conv_I4 or + Code.Conv_U1 or Code.Conv_U2 or Code.Conv_U4 } unary) { - if (Pop() is not Word word) return Stop($"`{op}` on a value that is not a word"); - stack.Add(new Word(null, op.StartsWith(".conv", StringComparison.Ordinal) ? 4 : word.Width)); + if (Pop() is not Word word) return Stop(" on a value that is not a word", showInstruction: true); + stack.Add(new Word(null, unary.Kind == Code.Neg ? word.Width : 4)); } - else if (ClassViews.Decode(op) is { Store: false, Kind: ClassViews.FieldKind.Word } load) + else if (classField is { Store: false, Kind: ClassViews.FieldKind.Word } load) { if (Pop() is not Instance instance || Field(instance, load) is not { } field) return Stop("a field load of an object the outline cannot name"); @@ -476,7 +498,7 @@ bool Branch(string condition, bool takenWhenTrue, int target) _ => new Word(term, field.Width!.Value) }); } - else if (ClassViews.Decode(op) is { Store: true, Kind: ClassViews.FieldKind.Word } store) + else if (classField is { Store: true, Kind: ClassViews.FieldKind.Word } store) { var value = Pop(); if (Pop() is not Instance instance || Field(instance, store) is not { } field) @@ -492,7 +514,7 @@ bool Branch(string condition, bool takenWhenTrue, int target) Steps.Add(new Access(instance.Object, field, AccessKind.Store, false, stores++, word.Term)); fieldMemory = stores; } - else if (ClassViews.Decode(op) is { Store: false, Kind: ClassViews.FieldKind.Reference } referenceLoad) + else if (classField is { Store: false, Kind: ClassViews.FieldKind.Reference } referenceLoad) { if (Pop() is not Instance instance || Field(instance, referenceLoad) is not { } field) return Stop("a reference field load of an object the outline cannot name"); @@ -524,7 +546,7 @@ bool Branch(string condition, bool takenWhenTrue, int target) Steps.Add(new Access(instance.Object, field, AccessKind.Load, false, stores, Target: reached)); stack.Add(new Instance(reached)); } - else if (ClassViews.Decode(op) is { Store: true, Kind: ClassViews.FieldKind.Reference } referenceStore) + else if (classField is { Store: true, Kind: ClassViews.FieldKind.Reference } referenceStore) { var value = Pop(); if (Pop() is not Instance instance || Field(instance, referenceStore) is not { } field) @@ -547,7 +569,7 @@ bool Branch(string condition, bool takenWhenTrue, int target) Steps.Add(stored); stores++; } - else if (ClassViews.Decode(op) is { Store: false, Kind: ClassViews.FieldKind.Array } arrayLoad) + else if (classField is { Store: false, Kind: ClassViews.FieldKind.Array } arrayLoad) { if (Pop() is not Instance instance || Field(instance, arrayLoad) is not { } field) return Stop("an array field load of an object the outline cannot name"); @@ -563,7 +585,7 @@ bool Branch(string condition, bool takenWhenTrue, int target) Steps.Add(new Access(instance.Object, field, AccessKind.Load, false, stores, Array: held)); stack.Add(new Values(held)); } - else if (ClassViews.Decode(op) is { Store: true, Kind: ClassViews.FieldKind.Array } arrayStore) + else if (classField is { Store: true, Kind: ClassViews.FieldKind.Array } arrayStore) { var value = Pop(); if (Pop() is not Instance instance || Field(instance, arrayStore) is not { } field) @@ -580,12 +602,13 @@ bool Branch(string condition, bool takenWhenTrue, int target) Steps.Add(new Access(instance.Object, field, AccessKind.Store, false, stores++, $"(.address {values.Array.Name})", Array: values.Array)); } - else if ((match = Regex.Match(op, @"\A\.memory \(\.arrayLoad (4|8) false\)\z")).Success) + else if (LeanCIL.Arrays.Numeric(op, spec.Method, pc, methodIndex) is + { Store: false, Width: 4 or 8, Signed: false } elementLoad) { var index = Pop(); if (Pop() is not Values { Array.Words: { } array } || index is not Word { Term: { } term, Width: 4 } || - array.Width != int.Parse(match.Groups[1].Value)) + array.Width != elementLoad.Width) return Stop("an array element read without a named word array and index"); if (linear) { stack.Add(new Word(null, array.Width)); continue; } var callerIndex = !Regex.IsMatch(term, @"\bstored\d+\b"); @@ -602,18 +625,30 @@ bool Branch(string condition, bool takenWhenTrue, int target) ArrayApart.Add(key, apart = "arrayApart" + ArrayApart.Count); return (write, apart); }).ToArray(); - Steps.Add(new WordRead(array, $"({term} : W32).toNat", stores, name, writes, callerIndex)); + Steps.Add(new WordRead(array, $"({term} : W32).toNat", stores, name, writes, callerIndex, + elementLoad)); stack.Add(new Word($"{array.ElementsName}[({term} : W32).toNat]", array.Width)); } - else if (op == ".memory .arrayLength") + else if (op is StackOperation { Kind: Code.Ldlen }) { - if (Pop() is not Values values) return Stop("`ldlen` of an array the outline cannot name"); - Steps.Add(new ArrayUse(values.Array, stores, Length: true)); - stack.Add(new Word($"(BitVec.ofNat 64 {values.Array.Length})", 8)); + switch (Pop()) + { + case Values values: + Steps.Add(new ArrayUse(values.Array, stores, Length: true)); + stack.Add(new Word($"(BitVec.ofNat 64 {values.Array.Length})", 8)); + break; + case ArrayArgument array: + Steps.Add(new ElementAccess(array.Name, ElementKind.Length, stores)); + var length = array.Name == Fresh?.Array ? $"{Fresh.Length}" : array.Name + "Length"; + stack.Add(new Word($"(BitVec.ofNat 64 {length})", 8)); + break; + default: + return Stop("`ldlen` of an array the outline cannot name"); + } } - else if ((match = Regex.Match(op, @"\A\.call(?:Instance)? (\d+) (\d+)\z")).Success) + else if (emitted.CallAt(pc) is { Kind: not CallKind.Tail } invocation) { - var index = int.Parse(match.Groups[1].Value); + var index = invocation.Method; // A contract read whole from the callee's body comes first, then a stub over words. var callee = callees(index); if (callee is null or { Open: true } && !linear && stubs?.Invoke(index) is { } stub) @@ -639,7 +674,7 @@ bool Branch(string condition, bool takenWhenTrue, int target) if (arguments.Any(argument => argument is Word { Term: null }) && !linear) return Stop($"a call to `{callee.Spec.Method.Name}` with a value the outline does not name"); if (Call(callee, arguments) is { } reason) return Stop(reason); - var width = callee.Spec.Result is { Constructor: ".i64" } ? 8 : 4; + var width = callee.Spec.Result is { Width: 8 } ? 8 : 4; if (callee.Open) { // The caller learns of the result only what the contract's `post` states. @@ -663,40 +698,36 @@ bool Branch(string condition, bool takenWhenTrue, int target) { var call = (CallStep)Steps[^1]; stack.Add(new Word(Substitute(returned, callee, arguments, Memory(call.FieldMemory), - call.Found), callee.Spec.Result is { Constructor: ".i64" } ? 8 : 4)); + call.Found), callee.Spec.Result is { Width: 8 } ? 8 : 4, + callee.Walk.returnedWord?.Literal ?? 0, callee.Walk.returnedWord?.Numeral ?? false)); } else if (callee.Spec.Result is not null && !linear) return Stop($"a call to `{callee.Spec.Method.Name}`, whose result the outline cannot name"); else if (callee.Spec.Result is not null) - stack.Add(new Word(null, callee.Spec.Result is { Constructor: ".i64" } ? 8 : 4)); + stack.Add(new Word(null, callee.Spec.Result is { Width: 8 } ? 8 : 4)); } - else if (op == ".memory .referenceArrayLength") - { - if (Pop() is not ArrayArgument array) return Stop("`ldlen` of an array the outline cannot name"); - Steps.Add(new ElementAccess(array.Name, ElementKind.Length, stores)); - var length = array.Name == Fresh?.Array ? $"{Fresh.Length}" : array.Name + "Length"; - stack.Add(new Word($"(BitVec.ofNat 64 {length})", 8)); - } - else if ((match = Regex.Match(op, @"\A\.newReferenceArray \(\.object (""[^""]*"") (""[^""]*"")\)\z")) - .Success) + // An array of a runtime class, such as `string[]`, is not stepped. + else if (op is Unoutlined && spec.Method.Body.Instructions[pc] is + { OpCode.Code: Code.Newarr, Operand: TypeReference element } && ReferenceArrays.Element(element) && + LayoutEvidence.Scope(element) is var elementScope && elementScope != TypeTests.RuntimeScope) { // Facts stated of the caller's memory carry into the fresh one only across this first write. - if (Pop() is not Word { Width: 4, Term: { } count } || !Numeral(count) || int.Parse(count) == 0) + if (Pop() is not Word { Width: 4, Numeral: true, Literal: > 0 } count) return Stop("`newarr` of a length that is not a positive literal"); if (linear || stores > 0 || calls > 0 || Fresh is not null) return Stop("`newarr` after a write, a call or another allocation"); - Fresh = new Allocation("fresh1", match.Groups[1].Value, match.Groups[2].Value, int.Parse(count), - stores); + Fresh = new Allocation("fresh1", GenericEvidence.Quote(elementScope), + GenericEvidence.Quote(element.FullName), (int)count.Literal, stores); Steps.Add(Fresh); stores++; stack.Add(new ArrayArgument(Fresh.Array)); } - else if (op == ".memory .referenceArrayLoad") + else if (op is StackOperation { Kind: Code.Ldelem_Ref }) { if (Pop() is not Word index || Pop() is not ArrayArgument array || Index(index) is not { } at) return Stop("`ldelem.ref` of an array or index the outline cannot name"); if ((Outside(array.Name, at) ?? Unseparated(array.Name, at)) is { } reason) return Stop(reason); - var slot = Slot(array.Name, at); + var slot = Slot(array.Name, at.Term); // The element holds what this path last stored into it, or what it held in the caller's memory. if (PathSteps.OfType().LastOrDefault(access => access.Kind == ElementKind.Store && access.Slot == slot) is { } written) @@ -715,32 +746,35 @@ bool Branch(string condition, bool takenWhenTrue, int target) // Every element of a fresh array holds null until a store. if (array.Name == Fresh?.Array) { - Steps.Add(new ElementAccess(array.Name, ElementKind.Load, stores, at, ".null", Slot: slot, - Reread: true)); + Steps.Add(new ElementAccess(array.Name, ElementKind.Load, stores, at.Term, ".null", Slot: slot, + Reread: true, LiteralIndex: at.Literal is not null)); stack.Add(new Null()); continue; } - var held = new Opaque($"(ReferenceArray.of initial {array.Name} {at})", slot + "Forms.formed"); + var held = new Opaque($"(ReferenceArray.of initial {array.Name} {at.Term})", + slot + "Forms.formed"); if (CallWrote) return Stop(Uncarried(held)); - Steps.Add(new ElementAccess(array.Name, ElementKind.Load, stores, at, held.Term, - Formed: held.Formed, Slot: slot)); + Steps.Add(new ElementAccess(array.Name, ElementKind.Load, stores, at.Term, held.Term, + Formed: held.Formed, Slot: slot, LiteralIndex: at.Literal is not null)); stack.Add(held); } - else if (op == ".memory .referenceArrayStore") + else if (op is StackOperation { Kind: Code.Stelem_Ref }) { var value = Pop(); if (Pop() is not Word index || Pop() is not ArrayArgument array || Index(index) is not { } at) return Stop("`stelem.ref` into an array or at an index the outline cannot name"); if ((Outside(array.Name, at) ?? Unseparated(array.Name, at)) is { } reason) return Stop(reason); if (value is Opaque late && CallWrote) return Stop(Uncarried(late)); - var slot = Slot(array.Name, at); + var slot = Slot(array.Name, at.Term); ElementAccess? stored = value switch { - Instance target => new(array.Name, ElementKind.Store, stores, at, - $"(.address {target.Object.Name})", target.Object, Slot: slot), - Null => new(array.Name, ElementKind.Store, stores, at, ".null", Slot: slot), - Opaque opaque => new(array.Name, ElementKind.Store, stores, at, opaque.Term, - Formed: opaque.Formed, Slot: slot), + Instance target => new(array.Name, ElementKind.Store, stores, at.Term, + $"(.address {target.Object.Name})", target.Object, Slot: slot, + LiteralIndex: at.Literal is not null), + Null => new(array.Name, ElementKind.Store, stores, at.Term, ".null", Slot: slot, + LiteralIndex: at.Literal is not null), + Opaque opaque => new(array.Name, ElementKind.Store, stores, at.Term, opaque.Term, + Formed: opaque.Formed, Slot: slot, LiteralIndex: at.Literal is not null), _ => null }; if (stored is null) return Stop("`stelem.ref` of a value the outline cannot name"); @@ -751,13 +785,13 @@ bool Branch(string condition, bool takenWhenTrue, int target) Steps.Add(stored); stores++; } - else if (op == ".nop") { } - else if (throws && !linear && op.StartsWith(".throw ", StringComparison.Ordinal)) + else if (op.IsNop) { } + else if (throws && !linear && op is StackOperation { Kind: Code.Throw }) { Steps.Add(new Thrown(location(pc))); return true; } - else if (op == ".ret" || op.StartsWith(".ret (some ", StringComparison.Ordinal)) + else if (op is StackOperation { Kind: Code.Ret }) { if (spec.Result is not null) { @@ -772,6 +806,7 @@ bool Branch(string condition, bool takenWhenTrue, int target) Values values when spec.Result.Array => values.Array.View, _ => null }; + returnedWord = Returned is not null ? result as Word : null; // The return checks a returned array in the memory it leaves. if (result is Values returned && spec.Result.Array) Steps.Add(new ArrayUse(returned.Array, stores)); @@ -781,7 +816,7 @@ bool Branch(string condition, bool takenWhenTrue, int target) } if (!linear) return true; } - else return Stop($"`{op}`, which the outline does not step"); + else return Stop(", which the outline does not step", showInstruction: true); } if (linear) return true; Stopped = "the end of the body without a return"; @@ -912,40 +947,41 @@ static string Uncarried(Opaque opaque) => /// Why the facts of element `index` of `array` do not carry across this path's element stores: /// one into another array, which may be the same one, or one at an index not provably different. - string? Unseparated(string array, string index) + string? Unseparated(string array, ElementIndex at) { foreach (var store in PathSteps.OfType().Where(access => access.Kind == ElementKind.Store)) { if (store.Array != array) return $"an access of `{array}` after a store into `{store.Array}`, which may be the same array"; - if (store.Index != index && !(Numeral(store.Index!) && Numeral(index))) - return $"an access of element `{index}` of `{array}` after a store into element `{store.Index}`, " + - "which the statement cannot tell apart"; + if (store.Index != at.Term && !(store.LiteralIndex && at.Literal is not null)) + return $"an access of element `{at.Term}` of `{array}` after a store into element " + + $"`{store.Index}`, which the statement cannot tell apart"; } return null; } - static bool Numeral(string index) => Regex.IsMatch(index, @"\A\d+\z"); - /// The element facts of `array`, with the index each names. internal (string Slot, string Index)[] SlotsOf(string array) => [.. Slots.Where(pair => pair.Key.Array == array).Select(pair => (pair.Value, pair.Key.Index))]; /// Why element `index` of the fresh array is not one its proof names: an index that is not a /// literal inside its length, which may throw. - string? Outside(string array, string index) => - array == Fresh?.Array && !(Numeral(index) && int.Parse(index) < Fresh.Length) - ? $"an element `{index}` of `{array}` not provably inside its {Fresh.Length} elements" : null; + string? Outside(string array, ElementIndex at) => + array == Fresh?.Array && !(at.Literal < Fresh.Length) + ? $"an element `{at.Term}` of `{array}` not provably inside its {Fresh.Length} elements" : null; /// The element number a named int32 index selects, as the CLR reads it: unsigned. - static string? Index(Word index) => index switch + static ElementIndex? Index(Word index) => index switch { { Width: not 4 } or { Term: null } => null, - { Term: var term } when Regex.IsMatch(term, @"\A\d+\z") => term, - { Term: var term } when term.StartsWith("(-", StringComparison.Ordinal) => null, - { Term: var term } => term + ".toNat" + { Numeral: true } => new(index.Term, index.Literal), + { Literal: < 0 } => null, + { Term: var term } => new(term + ".toNat") }; + /// The term naming an element number, and the number itself when it is a literal. + sealed record ElementIndex(string Term, long? Literal = null); + /// The fact naming element `index` of `array`, numbered in the order the body reaches them. /// string Slot(string array, string index) @@ -956,7 +992,7 @@ string Slot(string array, string index) return name; } - static string Uncompared(string op) => $"`{op}` of a reference with a value the outline cannot name"; + const string Uncompared = " of a reference with a value the outline cannot name"; /// The equation a reference test decides, or null when an operand is not a reference the outline /// names or a store has come first, since the statement forms each reference in the caller's memory. @@ -1093,7 +1129,7 @@ internal string ArrayFact(string array) => Flattened.OfType() /// A term's words, keeping each short innermost parenthesized group such as `(by decide)` whole, as /// parts the writer may wrap between. A longer group splits so no part outgrows the line. static string[] Terms(this string term) => - [.. InnermostGroup() + [.. InnermostGroup .Replace(term, group => group.Length <= ShortGroup ? group.Value.Replace(' ', Glue) : group.Value) .Split(' ').Select(part => part.Replace(Glue, ' '))]; @@ -1101,7 +1137,7 @@ [.. InnermostGroup() const char Glue = '\u0001'; [GeneratedRegex(@"\([^()]*\)")] - private static partial Regex InnermostGroup(); + private static partial Regex InnermostGroup { get; } /// The contract of a callee whose body the walk steps with no call of its own and at most one store, /// or reference field loads and no store: `pre` its facts, `post` its store and its result, and its proof. @@ -1119,8 +1155,9 @@ [.. InnermostGroup() return null; var spec = new Specification(method, parameters, DescribeResult(method, extraction.Architecture), true, true); - var walk = new ObjectWalk(spec, extraction.Emitted[index].Ops, views, _ => null, - numericHomes: HasNumericObjectHomes(method), throws: splits); + var walk = new ObjectWalk(spec, extraction.Emitted[index], views, _ => null, + extraction.Emitted[index].Frame.IndexParameterHomes(), + numericHomes: HasNumericObjectHomes(method), throws: splits, methodIndex: index); // A contract states reference stores only of objects the callee reaches, and its caller cannot give // it the premise that a reference outside the model forms; it states no array a field holds. if (!walk.Run(pc => Location(method, pc)) || walk.Flattened.Any(step => step is CallStep) || @@ -1148,10 +1185,6 @@ [.. InnermostGroup() } } - /// A tactic's fact list as parts the writer may wrap after any comma. - static IEnumerable FactList(List facts) => - facts.Select((fact, index) => (index == 0 ? "[" : "") + fact + (index + 1 < facts.Count ? "," : "]")); - /// Binds the actual numeric local homes and caller-relative frame facts of an object method. static void ObjectEntry(Writer lean, Specification spec, string name, string method) { @@ -1179,7 +1212,8 @@ static void LoopPlan(Extraction extraction, Specification spec, Body body) var summaries = new Dictionary(); Callee? Summary(int index) => summaries.TryGetValue(index, out var known) ? known : summaries[index] = Summarize(extraction, index, names.Claim("callee" + index, "callee" + index), views); - var walk = new ObjectWalk(spec, body.Ops, views, Summary, linear: true); + var walk = new ObjectWalk(spec, body.Method, views, Summary, + extraction.Emitted[0].Frame.IndexParameterHomes(), linear: true); if (walk.Run(pc => Location(spec.Method, pc)) && walk.Objects.All(reached => reached.Holder is null) && walk.Arrays.Count == 0) spec.Plan = walk.Plan(); @@ -1462,6 +1496,18 @@ string Carry(string key, int stores, Func across) return name; } + /// Wrap between tactic facts, or between the arguments of a fact wider than a line. + void WriteFacts(string head, List steps, string tail) + { + var width = Writer.Limit - Writer.ContinuationIndent(head); + var parts = steps.SelectMany((step, index) => + { + var fact = (index == 0 ? "[" : "") + step + (index + 1 < steps.Count ? "," : "]"); + return fact.Length > width ? fact.Terms() : [fact]; + }); + lean.Wrapped(head, parts, tail); + } + void Add(string fact) { if (!facts.Contains(fact)) facts.Add(fact); @@ -1570,7 +1616,7 @@ void Enter(ObjectFrameEntry entered) Add(frame.Entry + ".frame"); if (returns is null) lean.Line($"{indent}apply WP.exists_execution"); lean.Line($"{indent}refine WP.enter (by cil_lookup [])"); - lean.Wrapped($"{indent} (by cil_checked_side", FactList([.. frame.Formed, .. facts]), ") setup ?_"); + WriteFacts($"{indent} (by cil_checked_side", [.. frame.Formed, .. facts], ") setup ?_"); stored.Add(entered); } @@ -1618,12 +1664,12 @@ void WordLength(Parameter array, int stores) var name = array.Name + "LengthStep" + stores; var memory = ObjectWalk.Memory(stores); lean.Line($"{indent}have {name} : ∀ body pc args frame rest,"); - lean.Line($"{indent} step body (.memory .arrayLength) pc args frame (.array {array.Name} :: rest)"); + lean.Line($"{indent} step body .ldlen pc args frame (.array {array.Name} :: rest)"); lean.Line($"{indent} {memory} = .ok (.next (pc + 1)"); lean.Line($"{indent} (.scalar (.i64 (BitVec.ofNat 64 {array.Name}.length)) :: rest) frame"); lean.Line($"{indent} {memory}) := by"); lean.Line($"{indent} intros"); - lean.Line($"{indent} simp [step_memory_arrayLength_eq, {WordValid(array, stores)}, " + + lean.Line($"{indent} simp [step_ldlen_array_eq, {WordValid(array, stores)}, " + $"{array.Name}Reference, checkedAt, Except.mapError] <;> rfl"); Add(name); } @@ -1659,7 +1705,7 @@ void WordElement(WordRead read) }); lean.Wrapped($"{indent}obtain ⟨{read.Name}Reference, {read.Name}Found, {read.Name}Loaded⟩ :=", $"{contents}.array_load {WordValid(array, read.Stores)} (by decide) {read.Name}Bound".Terms(), ""); - Add($"step_arrayLoad_of_element (index := .i32 _) rfl {read.Name}Found {read.Name}Loaded"); + Add($"{read.Load.LoadLemma} (index := .i32 _) rfl {read.Name}Found {read.Name}Loaded"); } /// A `newarr` charged to the budget premise, confined to no caller memory, with a `Holds` of null @@ -1796,7 +1842,7 @@ void Call(CallStep call) // The memory after the call is named for the specification: a store's, or the caller's own. var after = call.Writes is { } written ? $"stored{written.Stores + 1}" : call.Callee.Walk.NumericHomes ? $"stored{call.Stores + 1}" : $"called{number}"; - lean.Wrapped($"{indent}cil_contract_steps", FactList(facts), ""); + WriteFacts($"{indent}cil_contract_steps", facts, ""); lean.Line($"{indent} with {after} output{number} call{number}"); if (frame is null) facts.Clear(); Add(call.Callee.Name + "Contract"); @@ -1881,7 +1927,7 @@ void Stubbed(StubCall call) Add($"{name}Contract"); if (frame is null) Add("leaveFrame"); else Add($"frameFacts{call.Stores}.wellFormed"); - lean.Wrapped($"{indent}cil_contract_steps", FactList(facts), ""); + WriteFacts($"{indent}cil_contract_steps", facts, ""); lean.Line($"{indent} with called{number} output{number} call{number}"); // A frame's entry and homes are stepped again from the start, so its facts stay. if (frame is null) facts.Clear(); @@ -1899,7 +1945,7 @@ void RewriteCallFields(CallStep call, string post) var terms = call.Callee.Walk.Returned + " " + call.Callee.Store?.Value; var equalities = fieldEqualities.Where(pair => terms.Contains(pair.Value.Lean + ".of ", StringComparison.Ordinal)).Select(pair => pair.Key).ToList(); - if (equalities.Count > 0) lean.Wrapped($"{indent}simp only", FactList(equalities), $"at {post}"); + if (equalities.Count > 0) WriteFacts($"{indent}simp only", equalities, $"at {post}"); } /// A call preserves local homes when its footprint lies in the caller's older storage. @@ -2039,8 +2085,8 @@ string[] Parts(string tail) => stored.OfType().Any() // The return leaves the method's frame, which owns no caller memory. // After a call the steps may already have reached the return. if (frame is null) Add("leaveFrame"); - lean.Wrapped(walk.Steps.LastOrDefault() is CallStep or StubCall - ? $"{indent}try cil_contract_steps" : $"{indent}cil_contract_steps", FactList(facts), ""); + WriteFacts(walk.Steps.LastOrDefault() is CallStep or StubCall + ? $"{indent}try cil_contract_steps" : $"{indent}cil_contract_steps", facts, ""); var where = branches.Count == 0 ? "" : " where " + string.Join(" and ", branches); if (returns is not null) { diff --git a/Extractor/ProofOutlineScalar.cs b/Extractor/ProofOutlineScalar.cs index 59ee746de..2b44e47e5 100644 --- a/Extractor/ProofOutlineScalar.cs +++ b/Extractor/ProofOutlineScalar.cs @@ -1,5 +1,7 @@ +using ILOpCode = System.Reflection.Metadata.ILOpCode; using System.Globalization; using System.Text.RegularExpressions; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -9,6 +11,18 @@ internal static partial class ProofOutline /// The most return paths a scalar outline splits a body into. const int MaxScalarPaths = 8; + /// The arithmetic lemma, expression and signedness for a checked CIL opcode. + static (string Name, string Symbol, bool Signed)? CheckedWord(Code opcode) => opcode switch + { + Code.Add_Ovf => ("add", "+", true), + Code.Add_Ovf_Un => ("add", "+", false), + Code.Sub_Ovf => ("sub", "-", true), + Code.Sub_Ovf_Un => ("sub", "-", false), + Code.Mul_Ovf => ("mul", "*", true), + Code.Mul_Ovf_Un => ("mul", "*", false), + _ => null + }; + /// What a scalar body does from one pc to a return along every path. abstract record ScalarStep; @@ -24,12 +38,14 @@ sealed record ScalarSplit(string Condition, int Pc, int HoldsPc, ScalarStep Hold ScalarStep Fails) : ScalarStep; /// A word on the walked stack: its term, or null when the outline does not name it. - sealed record ScalarWord(string? Term, int Bits); + sealed record ScalarWord(string? Term, int Bits, long? Numeral = null); /// The paths of a body over word arguments that tests named words or checks arithmetic, with no /// backward jump; null when it does neither, has another operation or splits into too many paths. - static ScalarStep? ScalarPaths(Specification spec, string[] ops) + static ScalarStep? ScalarPaths(Specification spec, Method emitted) { + var ops = emitted.Operations; + var plainReturn = LeanEmitter.Narrow(spec.Method.ReturnType) is null; if (spec.Parameters.Any(parameter => parameter.Shape != ParameterShape.Scalar)) return null; var paths = 0; var decided = false; @@ -38,35 +54,28 @@ sealed record ScalarWord(string? Term, int Bits); for (; pc < ops.Length; pc++) { var op = ops[pc]; - Match match; - if ((match = Regex.Match(op, @"\A\.arg (\d+)\z")).Success) + if (emitted.ArgumentAt(pc) is int argument && MethodArguments.Plain(spec.Method, argument)) { - var parameter = spec.Parameters[int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)]; + var parameter = spec.Parameters[argument]; stack = stack.Push(new(parameter.Name, 8 * parameter.Width)); } - else if ((match = Regex.Match(op, - @"\A\.const(32|64) (?:(\d+)|\(BitVec\.ofInt \d+ \((-?\d+)\)\))\z")).Success) - stack = stack.Push(new(match.Groups[2].Success ? match.Groups[2].Value - : $"({match.Groups[3].Value})", match.Groups[1].Value == "32" ? 32 : 64)); - else if ((match = Regex.Match(op, @"\A\.(add|sub|mul|band|bor|bxor)\z")).Success) + else if (emitted.ConstantAt(pc) is { } constant) + stack = stack.Push(new(constant.Numeral ? $"{constant.Value}" : $"({constant.Value})", + constant.Width == 32 ? 32 : 64, constant.Numeral ? constant.Value : null)); + else if (op is StackOperation { IsArithmetic: true } arithmetic) { if (stack.Count < 2) return null; stack = stack.Pop(out var right).Pop(out var left); - var symbol = match.Groups[1].Value switch - { - "add" => "+", "sub" => "-", "mul" => "*", "band" => "&&&", "bor" => "|||", _ => "^^^" - }; + var symbol = arithmetic.Symbol; stack = stack.Push(new(left.Term is null || right.Term is null ? null : $"{Operand(left.Term)} {symbol} {Operand(right.Term)}", right.Bits)); } - else if ((match = Regex.Match(op, @"\A\.checkedArithmetic \.(add|sub|mul) (false|true)\z")).Success) + else if (op is StackOperation stackOp && CheckedWord(stackOp.Kind) is { } checkedWord) { if (stack.Count < 2) return null; stack = stack.Pop(out var right).Pop(out var left); if (left.Term is null || right.Term is null || left.Bits != right.Bits) return null; - var operation = match.Groups[1].Value; - var signed = match.Groups[2].Value == "true"; - var symbol = operation switch { "add" => "+", "sub" => "-", _ => "*" }; + var (operation, symbol, signed) = checkedWord; var fits = Fits(operation, signed, Operand(left.Term), Operand(right.Term), symbol, left.Bits); stack = stack.Push(new($"{Operand(left.Term)} {symbol} {Operand(right.Term)}", left.Bits)); decided = true; @@ -75,33 +84,45 @@ sealed record ScalarWord(string? Term, int Bits); return new ScalarCheck(fits, $"CIL.checkedValues_{operation}_{reading}_i{left.Bits}", operation, pc, next); } - else if (Body.Target(op) is { } target) + else if (emitted.BranchAt(pc) is { } jump) { + var target = jump.Target; if (target <= pc) return null; - if (!Body.Conditional(op)) { pc = target - 1; continue; } - var name = Regex.Match(op, @"\A\.(\w+)").Groups[1].Value; - var operands = new ScalarWord[name is "brzero" or "brnonzero" ? 1 : 2]; + if (jump.Kind == ILOpCode.Br) { pc = target - 1; continue; } + var operands = new ScalarWord[Body.BranchOperands(jump.Kind)]; if (stack.Count < operands.Length) return null; for (var position = operands.Length - 1; position >= 0; position--) stack = stack.Pop(out operands[position]); if (operands.Any(operand => operand.Term is null || operand.Bits != operands[0].Bits)) return null; - var (condition, takenWhenTrue) = Body.Test(name, [.. operands.Select(operand => - Operand(operand.Term!))], operands[0].Bits); + // A test of literals is decided here; a split on it leaves an infeasible side no step closes. + if (operands.All(operand => operand.Numeral is not null)) + { + decided = true; + long[] literals = [.. operands.Select(operand => operand.Numeral!.Value)]; + if (Taken(jump.Kind, literals, operands[0].Bits)) pc = target - 1; + continue; + } + var (condition, takenWhenTrue) = Body.Test(jump.Kind, [.. operands.Select(operand => + new Body.Operand(Operand(operand.Term!), operand.Numeral))], operands[0].Bits); var (holds, fails) = takenWhenTrue ? (target, pc + 1) : (pc + 1, target); decided = true; if (Walk(holds, stack) is not { } holdsStep || Walk(fails, stack) is not { } failsStep) return null; return new ScalarSplit(condition, pc, holds, holdsStep, fails, failsStep); } - else if (op is ".ret") return ++paths > MaxScalarPaths ? null : new ScalarReturn(); - else if (op is ".nop") { } - else if (Body.Pure(op)) + else if (op is StackOperation { Kind: Code.Ret } && plainReturn) + return ++paths > MaxScalarPaths ? null : new ScalarReturn(); + else if (op.IsNop) { } + else if (emitted.PureAt(pc, plainReturn)) { // Conversions and bit counts compute a word the outline does not name; it may be returned. if (stack.Count == 0) return null; - stack = stack.Pop(out var word).Push(new(null, op is ".convU8" or ".convI8" ? 64 - : op is ".convU4" or ".convI4" || op.Contains(".word32", StringComparison.Ordinal) ? 32 + stack = stack.Pop(out var word).Push(new(null, + op is StackOperation { Kind: Code.Conv_U8 or Code.Conv_I8 or Code.Conv_I } + ? 64 + : op is StackOperation { Kind: Code.Conv_U4 or Code.Conv_I4 } || + emitted.BitCountWidthAt(pc) == 32 ? 32 : word.Bits)); } else return null; @@ -112,6 +133,30 @@ sealed record ScalarWord(string? Term, int Bits); return decided ? walked : null; } + /// Whether a conditional branch of `kind` on literal operands, read at `bits` bits, is taken. + /// + static bool Taken(ILOpCode kind, long[] values, int bits) + { + ulong Word(int index) => bits == 32 ? (uint)values[index] : (ulong)values[index]; + long Signed(int index) => bits == 32 ? (int)values[index] : values[index]; + return kind switch + { + ILOpCode.Brfalse => Word(0) == 0, + ILOpCode.Brtrue => Word(0) != 0, + ILOpCode.Beq => Word(0) == Word(1), + ILOpCode.Bne_un => Word(0) != Word(1), + ILOpCode.Blt => Signed(0) < Signed(1), + ILOpCode.Ble => Signed(0) <= Signed(1), + ILOpCode.Bgt => Signed(0) > Signed(1), + ILOpCode.Bge => Signed(0) >= Signed(1), + ILOpCode.Blt_un => Word(0) < Word(1), + ILOpCode.Ble_un => Word(0) <= Word(1), + ILOpCode.Bgt_un => Word(0) > Word(1), + ILOpCode.Bge_un => Word(0) >= Word(1), + _ => throw new ArgumentException($"`{kind}` is not a conditional branch", nameof(kind)) + }; + } + static string Operand(string term) => Regex.IsMatch(term, @"\A[\w.]+\z") ? term : $"({term})"; /// The fact under which a checked operation of `bits`-bit words does not overflow. diff --git a/Extractor/ProofOutlineSpanStores.cs b/Extractor/ProofOutlineSpanStores.cs index 0ee1d3e78..1c72b15c1 100644 --- a/Extractor/ProofOutlineSpanStores.cs +++ b/Extractor/ProofOutlineSpanStores.cs @@ -1,4 +1,5 @@ using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -6,20 +7,51 @@ namespace Nethermind.LeanCIL; // `WritableSpan.write_head` makes, whether it is the outlined method or a callee of an outlined loop. internal static partial class ProofOutline { + /// Non-nop outline operations paired with their original CIL instructions. + static (Operation Op, Instruction Site, int Pc)[] StoreBodySites(Extraction extraction, int index) => + extraction.Emitted[index].Operations + .Select((op, pc) => (Op: op, Site: extraction.Methods[index].Body.Instructions[pc], Pc: pc)) + .Where(pair => !pair.Op.IsNop).ToArray(); + /// The element width and store of a static method `(Span<T> target, T value)` whose body only /// stores `value` into `target[0]`. - static (int Width, string Op)? SpanStores(Extraction extraction, int index) + static (int Width, string Lemma)? SpanStores(Extraction extraction, int index) { var method = extraction.Methods[index]; - var ops = extraction.Emitted[index].Ops.Where(op => op != ".nop").ToArray(); + var ops = StoreBodySites(extraction, index); if (method.HasThis || method.ReturnType.FullName != "System.Void" || method.Parameters.Count != 2 || WrittenSpanWidth(method.Parameters[0].ParameterType) is not { } width || ElementWidth(method.Parameters[1].ParameterType) != width || - !extraction.Emitted[index].Metadata.Contains("locals := [],")) return null; - string[] body = [".local 0", ".const32 0", $".memory (.spanItem {width})", ".arg 1"]; - if (ops.Length != 6 || !ops.Take(4).SequenceEqual(body) || ops[5] != ".ret") return null; - var stores = width == 4 ? ops[4] == ".storeScalar 4" : ops[4] is ".store64" or ".storeScalar 8"; - return stores ? (width, ops[4]) : null; + !extraction.Emitted[index].Frame.HasNoLocals) return null; + if (ops is not [(Unoutlined, var address, _), var head, + (Unoutlined, var item, _), var value, var store, (_, { OpCode.Code: Code.Ret }, _)] || + extraction.Emitted[index].ConstantAt(head.Pc) != (32, 0L, true) || + extraction.Emitted[index].ArgumentAt(value.Pc) != 1 || + address.OpCode.Code is not (Code.Ldarga or Code.Ldarga_S) || + address.Operand is not ParameterDefinition parameter || MethodArguments.Index(method, parameter) != 0 || + Spans.MemberWidth(item, "get_Item", 1, extraction.Architecture) != width || !MethodArguments.Plain(method, 1)) + return null; + var write = ScalarStore(extraction.Emitted[index], store.Pc, store.Site, extraction.Architecture); + return width is 4 or 8 && write?.Width == width ? write : null; + } + + /// The number of bytes stored and the successful-write theorem for this CIL site. + static (int Width, string Lemma)? ScalarStore(Method emitted, int pc, Instruction site, string architecture) + { + static (int, string) Transfer(int width) => + (width, width == 8 ? "transfer_i64_of_write" : "transfer_i32_of_write"); + return emitted.Operations[pc] switch + { + StackOperation { Kind: Code.Stind_I1 } => (1, "step_stind_narrow_of_write .i1"), + StackOperation { Kind: Code.Stind_I2 } => (2, "step_stind_narrow_of_write .i2"), + StackOperation { Kind: Code.Stind_I4 } => (4, "step_stind_narrow_of_write .i4"), + StackOperation { Kind: Code.Stind_I8 } => (8, "step_stind_i8_of_write"), + StackOperation { Kind: Code.Stind_I } => (8, "step_stind_i_of_write"), + Unoutlined when emitted.ObjectAt(pc, Code.Stobj)?.ScalarWidth is int width => Transfer(width), + Unoutlined when UnsafeModels.UnalignedStorage(site, "WriteUnaligned", architecture) is { } storage => + Transfer(MetadataValidation.PrimitiveWidth(storage)), + _ => null + }; } /// The element width of a writable span of words, or null for any other type. @@ -27,15 +59,9 @@ internal static partial class ProofOutline type is GenericInstanceType { ElementType.FullName: "System.Span`1" } span ? ElementWidth(span.GenericArguments[0]) : null; - /// The step lemma of a scalar store through an address whose write succeeds. - static string StoreLemma(string op) => op switch - { - ".store64" => "step_store64_of_write", ".storeScalar 8" => "step_storeScalar_i64_of_write", - _ => "step_storeScalar_of_write" - }; - /// The contract proof of a callee that stores its value into the first element of its span. - static void SpanStoreContract(Writer lean, int index, string name, string pattern, string value, string op) + static void SpanStoreContract(Writer lean, int index, string name, string pattern, string value, + string lemma) { lean.Line($" rintro {pattern} memory ⟨rest, span⟩"); lean.Line($" dsimp only [{name}Contract] at span"); @@ -44,14 +70,14 @@ static void SpanStoreContract(Writer lean, int index, string name, string patter lean.Line($" ⟨CIL.executionBound Extracted.program {name}, ?_⟩ confined filled"); // Frame entry below is proved by hand, so the goal names the contract's arguments outright. lean.Line($" dsimp only [{name}Contract]"); - SpanStoreRun(lean, " ", index, "span", "found", "wrote", "address", op); + SpanStoreRun(lean, " ", index, "span", "found", "wrote", "address", lemma); } /// The run of method `index` storing into the first element of the nonempty `WritableSpan` named /// `span`, given the facts `WritableSpan.write_head` names: entering the frame that homes the span, then /// stepping the body. static void SpanStoreRun(Writer lean, string indent, int index, string span, string found, string wrote, - string address, string op) + string address, string lemma) { lean.Line($"{indent}have valid := {span}.valid"); lean.Line($"{indent}have width := {span}.elementWidth"); @@ -61,19 +87,19 @@ static void SpanStoreRun(Writer lean, string indent, int index, string span, str lean.Line($"{indent}· cil_lookup []"); lean.Line($"{indent}· cil_checked_side [valid]"); // Entering the frame homes the span in a local, which the stepping tactics do not set up. - lean.Line($"{indent}· simp [enterFrame, Extracted.method{index}, makeLocals, makeArgumentHomes, " + + lean.Line($"{indent}· simp [enterFrame, Extracted.method{index}, makeLocals, " + "makeParameterHomes,"); lean.Line($"{indent} makeLocal, storeLocal, CIL.Safety.Value.hasUnspecified, width, mode, valid, " + "checkedAt,"); lean.Line($"{indent} Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure]"); lean.Line($"{indent} exact ⟨rfl, rfl⟩"); - lean.Line($"{indent}cil_checked_steps [{found}, {StoreLemma(op)} {wrote}, valid, width, mode, nonempty, " + + lean.Line($"{indent}cil_checked_steps [{found}, {lemma} {wrote}, valid, width, mode, nonempty, " + $"{address}]"); } /// The outline of a method that stores its value into the first element of its span: an empty span /// throws, which the precondition must rule out, and a nonempty one is stepped through the write. - static string SpanStoreLine(Specification spec, string op, Writer lean) + static string SpanStoreLine(Specification spec, string lemma, Writer lean) { var target = spec.Parameters[0]; var value = spec.Parameters[1].Name; @@ -96,7 +122,7 @@ static string SpanStoreLine(Specification spec, string op, Writer lean) lean.Line($" obtain ⟨{name}Item, {name}After, {name}Address, {name}Found, {name}Wrote, {name}Confined,"); lean.Line($" {name}Filled⟩ := {name}Checked.write_head {value}"); lean.Line($" refine ⟨CIL.executionBound Extracted.program 0, {name}After, [], ?_, rfl, ?postcondition⟩"); - SpanStoreRun(lean, " ", 0, name + "Checked", name + "Found", name + "Wrote", name + "Address", op); + SpanStoreRun(lean, " ", 0, name + "Checked", name + "Found", name + "Wrote", name + "Address", lemma); lean.Noted(" ", "case postcondition => sorry", $"Postcondition, with `{name}Filled` the stored element and `{name}Confined` the rest kept"); Footer(lean); diff --git a/Extractor/ProofOutlineStructLoops.cs b/Extractor/ProofOutlineStructLoops.cs index 83ccfab48..d368bd909 100644 --- a/Extractor/ProofOutlineStructLoops.cs +++ b/Extractor/ProofOutlineStructLoops.cs @@ -1,6 +1,7 @@ -using System.Globalization; +using ILOpCode = System.Reflection.Metadata.ILOpCode; using System.Text.RegularExpressions; using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -16,11 +17,13 @@ sealed record ReceiverField(string Name, string ReadName, string OwnedName, int /// authority, and every generated path is checked against the extracted instructions. sealed class ReceiverLoop(Specification spec, Body body, Writer lean) { + readonly bool plainReturn = LeanEmitter.Narrow(spec.Method.ReturnType) is null; string receiver => spec.Parameters[0].Name; readonly List fields = []; readonly Names identifiers = new(); int header; int names; + string Footprint => "[" + string.Join(", ", fields.Where(item => item.Written) .Select(item => $"({item.Address(receiver)}, 8)")) + "]"; string Parameters => string.Join(" ", spec.Parameters.Select(parameter => parameter.Name)); @@ -46,23 +49,26 @@ internal string Generate() }).ToArray(); spec = new(spec.Method, parameters, spec.Result, spec.Symbolic, spec.Classes); var loops = body.Loops(); - if (loops.Count != 1 || !Body.Conditional(body.Ops[loops[0].Latch]) || + if (spec.Parameters.FirstOrDefault(parameter => parameter.Shape == ParameterShape.Narrow) is { } narrow) + throw Unsupported( + $"A struct receiver loop reads word arguments only, and `{narrow.Name}` is a narrow integer"); + if (loops.Count != 1 || body.Method.BranchAt(loops[0].Latch) is not { Kind: not ILOpCode.Br } || spec.Method.Body.Variables.Count != 0 || spec.Parameters.Any(parameter => parameter.Shape is not (ParameterShape.StructReference or ParameterShape.Scalar) || parameter.Shape == ParameterShape.Scalar && parameter.Width != 8) || - spec.Result is { Constructor: not ".i64" }) + spec.Result is { Width: not 8 }) throw Unsupported("A struct receiver loop needs one conditional back edge, no local homes, " + "and word arguments with a void or 64-bit result"); header = body.BlockStart(loops[0].Latch); - for (var pc = 0; pc < body.Ops.Length; pc++) + for (var pc = 0; pc < body.Operations.Length; pc++) { - var match = Regex.Match(body.Ops[pc], @"\A\.(fieldAt|setFieldAt) (\d+) 8(?: false)?\z"); - if (!match.Success) continue; - var offset = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture); + if (body.Method.StructFieldAt(pc) is not { Opcode: Code.Stfld or Code.Ldfld, IsAggregate: false, + Width: 8, Signed: false } access) continue; + var offset = access.Offset; if (spec.Method.Body.Instructions[pc].Operand is not FieldReference reference) throw Unsupported("A receiver field has no metadata name"); var known = fields.FindIndex(field => field.Offset == offset); - var written = match.Groups[1].Value == "setFieldAt"; + var written = access.Opcode == Code.Stfld; if (known >= 0) fields[known] = fields[known] with { Written = fields[known].Written || written }; else { @@ -103,7 +109,7 @@ parameter.Shape is not (ParameterShape.StructReference or ParameterShape.Scalar) Line(); Line("/-- The checked cutpoint just before the loop condition loads its fields. -/"); Line($"def header ({receiver} : Reference) (initial : Memory) (state : {StateType}) : LoopState :="); - Line($" ⟨{header}, ⟨initial.nextIdentity, [], [], [], 0⟩, [], state.memory⟩"); + Line($" ⟨{header}, ⟨initial.nextIdentity, [], [], 0⟩, [], state.memory⟩"); lean.Quiet(); Line("/-- The property preserved at the loop condition. -/"); lean.Wrapped("def invariant", Grouped(binders.Concat([("initial", "Memory"), ("state", StateType)])), @@ -123,8 +129,8 @@ parameter.Shape is not (ParameterShape.StructReference or ParameterShape.Scalar) Line($" have checked : {spec.ArgumentValues}.mapM (checkedValue initial) ="); Line($" .ok {spec.ArgumentValues} := by simp [cil_checked, ← same, state.formed]"); Line($" have setup : enterFrame Extracted.method0 {spec.ArgumentValues} initial ="); - Line(" .ok (⟨initial.nextIdentity, [], [], [], 0⟩, initial) := by rfl"); - Line($" refine invoke_loop_satisfies Extracted.lookup0 checked setup {HeaderTerm}"); + Line(" .ok (⟨initial.nextIdentity, [], [], 0⟩, initial) := by rfl"); + Line($" refine invoke_loop_satisfies Extracted.program_method0 checked setup {HeaderTerm}"); Line($" (invariant {Parameters} initial) (measure {Parameters} initial) (fun start => ?_) ?_"); Line(" ·"); var entry = Path("state"); @@ -165,13 +171,17 @@ sealed class ReceiverPath internal Dictionary Reads = []; internal Dictionary Owned = []; internal List Facts = []; + /// The load and store-step facts a branch condition rewrites once a store has run. + internal List Rewritable = []; + /// A field store has run on this path. + internal bool Written; internal List Stack = []; internal bool Stepped; internal ReceiverPath Copy() => new() { Memory = Memory, Formed = Formed, WellFormed = WellFormed, Confined = Confined, Values = new(Values), Reads = new(Reads), Owned = new(Owned), Facts = [.. Facts], Stack = [.. Stack], - Stepped = Stepped + Stepped = Stepped, Rewritable = [.. Rewritable], Written = Written }; } @@ -194,7 +204,7 @@ string State(ReceiverPath path) => "⟨" + string.Join(", ", new[] { path.Memory void Walk(ReceiverPath path, int pc, string indent, string hypothesis, bool iteration) { var visited = new HashSet(); - while (pc < body.Ops.Length) + while (pc < body.Operations.Length) { if (pc == header && (path.Stepped || !iteration)) { @@ -209,7 +219,7 @@ void Walk(ReceiverPath path, int pc, string indent, string hypothesis, bool iter } if (!visited.Add(pc)) throw Unsupported("A receiver loop path cycles outside its cutpoint"); path.Stepped = true; - var op = body.Ops[pc]; + var op = body.Operations[pc]; string Pop() { if (path.Stack.Count == 0) throw Unsupported("A receiver loop has a short symbolic stack"); @@ -217,43 +227,48 @@ string Pop() path.Stack.RemoveAt(path.Stack.Count - 1); return value; } - Match match; - if ((match = Regex.Match(op, @"\A\.arg (\d+)\z")).Success) - path.Stack.Add(spec.Parameters[int.Parse(match.Groups[1].Value)].Name); - else if (StructLiteral(op) is { } literal) + if (body.Method.ArgumentAt(pc) is int argument && MethodArguments.Plain(spec.Method, argument)) + path.Stack.Add(spec.Parameters[argument].Name); + else if (StructLiteral(body.Method.ConstantAt(pc)) is { } literal) { if (literal.Width == 8) path.Stack.Add(literal.Term); - else if (pc + 1 < body.Ops.Length && body.Ops[pc + 1] is ".convU8" or ".convI8") + else if (pc + 1 < body.Operations.Length && + body.Operations[pc + 1] is StackOperation + { Kind: Code.Conv_U8 or Code.Conv_I8 or Code.Conv_I }) path.Stack.Add($"({literal.Term})." + - (body.Ops[++pc] == ".convI8" ? "signExtend" : "setWidth") + " 64"); + (body.Operations[++pc] is StackOperation { Kind: Code.Conv_I8 or Code.Conv_I } + ? "signExtend" : "setWidth") + + " 64"); else throw Unsupported("A receiver loop needs 64-bit arithmetic and comparisons"); } - else if (op is ".convU8" or ".convI8" or ".nop") { } - else if (op == ".dup") path.Stack.Add(path.Stack[^1]); - else if ((match = Regex.Match(op, @"\A\.(add|sub|mul|band|bor|bxor)\z")).Success) + else if (op is StackOperation + { Kind: Code.Conv_U8 or Code.Conv_I8 or Code.Conv_I or Code.Nop }) { } + else if (op is StackOperation { Kind: Code.Dup }) path.Stack.Add(path.Stack[^1]); + else if (op is StackOperation { IsArithmetic: true } arithmetic) { var right = Pop(); var left = Pop(); - var symbol = match.Groups[1].Value switch - { "add" => "+", "sub" => "-", "mul" => "*", "band" => "&&&", "bor" => "|||", _ => "^^^" }; + var symbol = arithmetic.Symbol; path.Stack.Add($"({left} {symbol} {right})"); } - else if ((match = Regex.Match(op, @"\A\.(fieldAt|setFieldAt) (\d+) 8(?: false)?\z")).Success) + else if (body.Method.StructFieldAt(pc) is { Opcode: Code.Stfld or Code.Ldfld, IsAggregate: false, + Width: 8, Signed: false, Offset: var offset } fieldAccess) { - var field = fields.Single(field => field.Offset == int.Parse(match.Groups[2].Value)); - var store = match.Groups[1].Value == "setFieldAt"; + var field = fields.Single(field => field.Offset == offset); + var store = fieldAccess.Opcode == Code.Stfld; var value = store ? Pop() : ""; if (Pop() != receiver) throw Unsupported("A receiver loop accesses another struct's field"); Access(path, field, store ? value : null, indent); if (!store) path.Stack.Add(path.Values[field.Offset]); } - else if (Body.Target(op) is { } target) + else if (body.Method.BranchAt(pc) is { } jump) { - if (!Body.Conditional(op)) { pc = target; continue; } - var instruction = op.Split(' ')[0][1..]; + var target = jump.Target; + if (jump.Kind == ILOpCode.Br) { pc = target; continue; } var right = Pop(); - var operands = instruction is "brzero" or "brnonzero" ? new[] { right } : [Pop(), right]; - var (condition, taken) = Body.Test(instruction, operands, 64); + var operands = Body.BranchOperands(jump.Kind) == 1 ? new[] { right } : [Pop(), right]; + var (condition, taken) = Body.Test(jump.Kind, + [.. operands.Select(term => new Body.Operand(term))], 64); var fact = "branch" + ++names; Line($"{indent}by_cases {fact} : {condition}"); var yes = path.Copy(); @@ -268,7 +283,7 @@ string Pop() Walk(no, taken ? pc + 1 : target, indent + " ", hypothesis, iteration); return; } - else if (op == ".ret") + else if (op is StackOperation { Kind: Code.Ret } && plainReturn) { lean.Wrapped(indent + "cil_loop_finish", ("[" + string.Join(", ", path.Facts .Concat([path.Confined])) + "]").Terms(), ""); @@ -278,7 +293,8 @@ string Pop() lean.Noted(indent, "all_goals sorry", "Required postcondition at this return"); return; } - else throw Unsupported($"A receiver loop does not yet outline `{op}` at {Location(spec.Method, pc)}"); + else throw Unsupported( + $"A receiver loop does not yet outline {InstructionAt(spec.Method, pc)}"); pc++; } throw Unsupported("A receiver loop path has no return or continuing head"); @@ -287,9 +303,8 @@ string Pop() /// A user name outside the numbered path-fact names emitted by the walk. void NormalizeFacts(ReceiverPath path, string condition, string indent) { - if (!path.Facts.Any(fact => fact.StartsWith("written", StringComparison.Ordinal))) return; - foreach (var fact in path.Facts.Where(fact => fact.StartsWith("stepped", StringComparison.Ordinal) || - fact.StartsWith("loaded", StringComparison.Ordinal)).ToArray()) + if (!path.Written) return; + foreach (var fact in path.Rewritable.ToArray()) { var normalized = "normalized" + ++names; Line($"{indent}have {normalized} := {fact}"); @@ -317,16 +332,18 @@ void Access(ReceiverPath path, ReceiverField field, string? value, string indent Line($"{indent}have loaded{index} := {path.Reads[field.Offset]}.load_word (index := 0) rfl"); Line($"{indent}simp only [Reference.element_zero] at loaded{index}"); path.Facts.Add("loaded" + index); + path.Rewritable.Add("loaded" + index); return; } Line($"{indent}obtain ⟨memory{index}, written{index}, -, confined{index}, held{index}⟩ :="); Line($"{indent} {path.Owned[field.Offset]}.write_one {value}"); - Line($"{indent}have stepped{index} : ∀ body pc args frame rest,"); - Line($"{indent} step body (.setFieldAt {field.Offset} 8) pc args frame"); + Line($"{indent}have stepped{index} : ∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef) signed,"); + Line($"{indent} body.fieldShape operand = .value {field.Offset} 8 signed → ∀ pc args frame rest,"); + Line($"{indent} step body (.stfld operand) pc args frame"); Line($"{indent} (.scalar (.i64 {value}) :: .reference (.address {receiver}) :: rest)"); Line($"{indent} {path.Memory} = .ok (.next (pc + 1) rest frame memory{index}) := by"); - Line($"{indent} intros"); - Line($"{indent} rw [step_setFieldAt_i64_eq, added{index}]"); + Line($"{indent} intro body operand signed shape pc args frame rest"); + Line($"{indent} rw [step_stfld_value_i64_eq _ _ _ _ _ _ _ _ shape, added{index}]"); Line($"{indent} dsimp only [checkedAt, Except.mapError, bind, pure, Except.bind, Except.pure]"); Line($"{indent} rw [written{index}]"); Line($"{indent}have formed{index} : form memory{index} {receiver} = .ok {receiver} :="); @@ -361,6 +378,8 @@ void Access(ReceiverPath path, ReceiverField field, string? value, string indent path.Values[field.Offset] = value; path.Facts.Add("written" + index); path.Facts.Add("stepped" + index); + path.Written = true; + path.Rewritable.Add("stepped" + index); path.Facts.Add(path.Formed); } } diff --git a/Extractor/ProofOutlineStructs.cs b/Extractor/ProofOutlineStructs.cs index f40c72b25..941724c7f 100644 --- a/Extractor/ProofOutlineStructs.cs +++ b/Extractor/ProofOutlineStructs.cs @@ -1,6 +1,6 @@ using System.Globalization; -using System.Text.RegularExpressions; using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -10,7 +10,7 @@ internal static partial class ProofOutline { /// A scalar field of the receiver the body reaches: where it lives, the binder naming its value on /// entry when the body reads it first, and whether the body stores it. - sealed class StructField(string name, int offset, int width, string receiver) + sealed class StructScalar(string name, int offset, int width, string receiver) { internal string Name { get; } = name; internal int Offset { get; } = offset; @@ -29,86 +29,90 @@ sealed class StructField(string name, int offset, int width, string receiver) /// facts each needs; null `Stopped` when the walk reached the return. sealed class StructWalk { - internal List Fields { get; } = []; - internal List<(StructField Field, string Value, string Source)> Stores { get; } = []; + internal List Fields { get; } = []; + internal List<(StructScalar Field, string Value, string Source)> Stores { get; } = []; internal string? Returned { get; set; } internal string? Stopped { get; set; } } /// A scalar literal with its stack width in bytes; conversions preserve this source width. - static (string Term, int Width)? StructLiteral(string op) + static (string Term, int Width)? StructLiteral((int Width, long Value, bool Numeral)? literal) => + literal is { } constant + ? (constant.Numeral ? $"({constant.Value}#{constant.Width})" + : $"(BitVec.ofInt {constant.Width} ({constant.Value}))", + constant.Width == 64 ? 8 : 4) : null; + + /// The result width in bytes and signedness of a `conv.i1` to `conv.u8` or of `conv.i`, which + /// sign-extends like `conv.i8`; null for another operation, including the native-width `conv.u`. + static (int Bytes, bool Signed)? SizedConversion(Operation op) => op is StackOperation { Kind: var kind } + ? kind switch { - var match = Regex.Match(op, - @"\A\.const(32|64) (?:(\d+)|(\(BitVec\.ofInt \1 \(-?\d+\)\)))\z"); - if (!match.Success) return null; - var term = match.Groups[2].Success ? $"({match.Groups[2].Value}#{match.Groups[1].Value})" - : match.Groups[3].Value; - return (term, match.Groups[1].Value == "64" ? 8 : 4); - } + Code.Conv_I1 => (1, true), Code.Conv_I2 => (2, true), Code.Conv_I4 => (4, true), + Code.Conv_I8 or Code.Conv_I => (8, true), + Code.Conv_U1 => (1, false), Code.Conv_U2 => (2, false), Code.Conv_U4 => (4, false), + Code.Conv_U8 => (8, false), + _ => null + } : null; /// Walks a branch-free body over the receiver's disjoint scalar fields and integer arguments. - static StructWalk WalkStruct(Specification spec, string[] ops, Names names) + static StructWalk WalkStruct(Specification spec, Body body, Names names) { + var ops = body.Operations; var walk = new StructWalk(); + var plainReturn = LeanEmitter.Narrow(spec.Method.ReturnType) is null; var receiver = spec.Parameters[0]; var stack = new Stack(); const string Receiver = "\u0000receiver"; - StructWalk Stop(string reason, int pc) + StructWalk Stop(string reason, int pc, bool showInstruction = false) { - walk.Stopped = $"{reason} at {Location(spec.Method, pc)}"; + walk.Stopped = StopAt(spec.Method, pc, reason, showInstruction); return walk; } - StructField? Field(int pc, int offset, int width) + StructScalar? Field(int pc, int offset, int width) { if (spec.Method.Body.Instructions[pc].Operand is not FieldReference reference) return null; var field = walk.Fields.FirstOrDefault(known => known.Offset == offset); if (field is not null) return field.Width == width ? field : null; if (walk.Fields.Any(known => known.Offset < offset + width && offset < known.Offset + known.Width)) return null; - field = new StructField(reference.Name, offset, width, receiver.Name); + field = new StructScalar(reference.Name, offset, width, receiver.Name); walk.Fields.Add(field); return field; } for (var pc = 0; pc < ops.Length; pc++) { var op = ops[pc]; - Match match; - if ((match = Regex.Match(op, @"\A\.arg (\d+)\z")).Success) + if (body.Method.ArgumentAt(pc) is int argument) { - var parameter = spec.Parameters[int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)]; + var parameter = spec.Parameters[argument]; + if (!MethodArguments.Plain(spec.Method, argument)) return Stop($"the narrow argument `{parameter.Name}`", pc); if (parameter.Shape == ParameterShape.StructReference) stack.Push(Receiver); else if (parameter.Shape == ParameterShape.Scalar) stack.Push(parameter.Name); - else if (parameter.Shape == ParameterShape.Narrow) - stack.Push($"({parameter.Name}.{(parameter.Signed ? "signExtend" : "setWidth")} 32)"); else return Stop($"the argument `{parameter.Name}`", pc); } - else if (StructLiteral(op) is { } literal) stack.Push(literal.Term); - else if ((match = Regex.Match(op, @"\A\.conv([UI])([1248])\z")).Success) + else if (StructLiteral(body.Method.ConstantAt(pc)) is { } literal) stack.Push(literal.Term); + else if (SizedConversion(op) is { } conversion) { if (stack.Count == 0 || stack.Pop() is not { } value || value == Receiver) return Stop("a conversion of a value the outline does not name", pc); - var bits = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture) * 8; - var extension = match.Groups[1].Value == "I" ? "signExtend" : "setWidth"; + var bits = conversion.Bytes * 8; + var extension = conversion.Signed ? "signExtend" : "setWidth"; stack.Push(bits < 32 ? $"(({value}).setWidth {bits}).{extension} 32" : $"({value}).{extension} {bits}"); } - else if ((match = Regex.Match(op, @"\A\.(add|sub|mul|band|bor|bxor)\z")).Success) + else if (op is StackOperation { IsArithmetic: true } arithmetic) { - if (stack.Count < 2) return Stop($"`{op}` on a short stack", pc); + if (stack.Count < 2) return Stop(" on a short stack", pc, showInstruction: true); var (right, left) = (stack.Pop(), stack.Pop()); if (left is null or Receiver || right is null or Receiver) - return Stop($"`{op}` on a value the outline does not name", pc); - var symbol = match.Groups[1].Value switch - { - "add" => "+", "sub" => "-", "mul" => "*", "band" => "&&&", "bor" => "|||", _ => "^^^" - }; + return Stop(" on a value the outline does not name", pc, showInstruction: true); + var symbol = arithmetic.Symbol; stack.Push($"{Operand(left)} {symbol} {Operand(right)}"); } - else if ((match = Regex.Match(op, @"\A\.fieldAt (\d+) (1|2|4|8)(?: (false|true))?\z")).Success) + else if (body.Method.StructFieldAt(pc) is { Opcode: Code.Ldfld, IsAggregate: false, Width: 1 or 2 or 4 or 8 } load) { if (stack.Count == 0 || stack.Pop() != Receiver) return Stop("a field load of another value", pc); - if (Field(pc, int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), - int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)) is not { } field) + if (Field(pc, load.Offset, load.Width) is not { } field) return Stop("a field load the outline cannot name", pc); var written = walk.Stores.LastOrDefault(store => store.Field == field); if (written.Field is not null) @@ -121,38 +125,37 @@ StructWalk Stop(string reason, int pc) stack.Push(Loaded(field.Initial)); } string Loaded(string value) => field.Width < 4 - ? $"({value}).{(match.Groups[3].Value == "true" ? "signExtend" : "setWidth")} 32" : value; + ? $"({value}).{(load.Signed ? "signExtend" : "setWidth")} 32" : value; } - else if ((match = Regex.Match(op, @"\A\.setFieldAt (\d+) (1|2|4|8)\z")).Success) + else if (body.Method.StructFieldAt(pc) is { Opcode: Code.Stfld, IsAggregate: false, Width: 1 or 2 or 4 or 8 } store) { if (stack.Count < 2) return Stop("a field store on a short stack", pc); var value = stack.Pop(); if (stack.Pop() != Receiver) return Stop("a field store into another value", pc); if (value is null or Receiver) return Stop("a field store of a value the outline does not name", pc); - if (Field(pc, int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), - int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)) is not { } field) + if (Field(pc, store.Offset, store.Width) is not { } field) return Stop("a field store the outline cannot name", pc); field.Stored = true; walk.Stores.Add((field, field.Width < 4 ? $"({value}).setWidth {field.Width * 8}" : value, value)); } - else if (op == ".dup") + else if (op is StackOperation { Kind: Code.Dup }) { if (stack.Count == 0) return Stop("a duplicate of an empty stack", pc); stack.Push(stack.Peek()); } - else if (op == ".nop") { } - else if (op == ".ret" && pc == ops.Length - 1) + else if (op.IsNop) { } + else if (op is StackOperation { Kind: Code.Ret } && plainReturn && pc == ops.Length - 1) { if (spec.Result is not null) { if (stack.Count == 0 || stack.Peek() is not { } result || result == Receiver || - spec.Result.Boolean || spec.Result.Constructor is not (".i64" or ".i32")) + spec.Result.Boolean || spec.Result.Width is not (4 or 8)) return Stop("a result the outline does not name", pc); walk.Returned = result; } return walk; } - else return Stop($"`{op}`, which the outline does not step through a struct", pc); + else return Stop(", which the outline does not step through a struct", pc, showInstruction: true); } return Stop("the end of the body without a return", ops.Length - 1); } @@ -168,7 +171,7 @@ string Loaded(string value) => field.Width < 4 /// postcondition, or one hole naming where the walk stopped. static string StructStraightLine(Specification spec, Body body, Names names, Writer lean) { - var walk = WalkStruct(spec, body.Ops, names); + var walk = WalkStruct(spec, body, names); var stepped = walk.Stopped is null; Header(lean, spec, ["CIL.Verification", "CIL.Safety.SymbolicExecution", "CIL.Safety.BufferContents", "CIL.Safety.LoopCallerFacts", "CIL.Safety.AggregateFields"], stepped ? "`postcondition`" : null); @@ -236,6 +239,8 @@ static string StructStraightLine(Specification spec, Body body, Names names, Wri return lean.ToString(); } var steps = new List { receiverForm, "CIL.NarrowKind.extend" }; + // The facts that load a field unsigned, which the proof simplifies at the element of the address. + var unsignedLoads = new List(); foreach (var width in walk.Fields.Select(field => field.Width).Where(width => width < 4).Distinct()) { lean.Line($" have loadSigned{width} (memory : Memory) (address : Reference) (value : BitVec {8 * width})"); @@ -260,6 +265,7 @@ static string StructStraightLine(Specification spec, Body body, Names names, Wri { LoadStructField(lean, field, field.Read!, "loaded" + field.Offset); steps.Add("loaded" + field.Offset); + unsignedLoads.Add("loaded" + field.Offset); if (field.Width < 4) steps.Add("loaded" + field.Offset + "Signed"); } } @@ -323,6 +329,7 @@ static string StructStraightLine(Specification spec, Body body, Names names, Wri var loaded = $"loaded{level}Field{field.Offset}"; LoadStructField(lean, field, held, loaded); steps.Add(loaded); + unsignedLoads.Add(loaded); if (field.Width < 4) steps.Add(loaded + "Signed"); } } @@ -334,9 +341,7 @@ static string StructStraightLine(Specification spec, Body body, Names names, Wri lean.Line($" have combined{level} := {confined}.trans changed{level}"); confined = index == 0 ? $"changed{level}" : $"combined{level}"; } - var loads = steps.Where(step => step.StartsWith("loaded", StringComparison.Ordinal) && - !step.EndsWith("Signed", StringComparison.Ordinal)).ToArray(); - foreach (var load in loads) lean.Line($" simp only [Reference.element_zero] at {load}"); + foreach (var load in unsignedLoads) lean.Line($" simp only [Reference.element_zero] at {load}"); if (spec.Result is null) lean.Line($" refine ⟨CIL.executionBound Extracted.program 0, {final}, [], ?run, rfl, {confined}, " + "?postcondition⟩"); @@ -355,7 +360,7 @@ static string StructStraightLine(Specification spec, Body body, Names names, Wri } /// A scalar field's loaded value, normalized to the stack's word width. - static void LoadStructField(Writer lean, StructField field, string contents, string name) + static void LoadStructField(Writer lean, StructScalar field, string contents, string name) { if (field.Width == 8) lean.Line($" have {name} := {contents}.load_word (index := 0) rfl"); @@ -373,7 +378,7 @@ static void LoadStructField(Writer lean, StructField field, string contents, str /// The fact that the field access's `add` of its offset to the receiver reaches the field. static void FieldAddress(Writer lean, string memory, string wellFormed, string form, string receiver, - StructField field, string access, string name) + StructScalar field, string access, string name) { lean.Line($" have {name} : add {memory} {receiver} 1 (BitVec.ofNat 64 {field.Offset}) ="); lean.Line($" .ok {field.Address} := by"); diff --git a/Extractor/ProofVerification.cs b/Extractor/ProofVerification.cs index 5198ae3d1..029cfa16e 100644 --- a/Extractor/ProofVerification.cs +++ b/Extractor/ProofVerification.cs @@ -8,18 +8,28 @@ partial class Program { sealed record VerificationSettings(int schema, string proof, string theorem, string? proofRoot = null); - sealed record BatchRequest(string output, string proof, string theorem, string? expect = null); + /// + /// One request of a batch. A `rejected` request names in `refutes` the index of the request, expecting + /// verification, whose contract its theorem refutes; `unproved` is the gate runner's own expectation of a proof + /// that fails at proof checking. + /// + sealed record BatchRequest(string output, string proof, string theorem, string? expect = null, + int? refutes = null); sealed record BatchManifest(int schema, string proofRoot, BatchRequest[] requests); sealed record BatchResult(string output, string proof, string theorem, string expect, string status, string stage, string? error, string[]? axioms, string? libraryBuild, FailureReports.Report? failure, double elapsedSeconds); + const string BatchManifestError = "Invalid batch manifest: expected schema 1, proofRoot and requests with " + + "output, proof, theorem, optional expect (verified or rejected), and refutes exactly when a request expects " + + "rejected."; + const string BatchUsage = "Usage: leancil verify-batch requests.json [--fresh] [--rebuild-library] " + "[--parallel count] [--results results.json] [--format text|json] [--trace] [--check]"; const string VerifyUsage = "Usage: leancil verify output-directory [--proof proof.lean] [--theorem name] " + "[--fresh] [--rebuild-library] [--proof-root package-directory] [--force-full-verify] " + - "[--full-artifact Lib=directory] [--format text|json] [--trace] [--goals]"; + "[--full-artifact Lib=directory] [--deep-checks digests.sha256] [--format text|json] [--trace] [--goals]"; /// /// The tool version, the LeanCIL revision and Lean toolchain its framework comes from, and the @@ -52,7 +62,7 @@ static HashSet RepositoryModules() => [.. Regex.Matches(File.ReadAllText static void RejectRepositoryImports(string proof) { var repository = RepositoryModules(); - var imported = SourceImport().Matches(File.ReadAllText(proof)).Select(import => import.Groups[1].Value) + var imported = SourceImport.Matches(File.ReadAllText(proof)).Select(import => import.Groups[1].Value) .FirstOrDefault(repository.Contains); if (imported is not null) throw new InvalidDataException($"{CliPaths.Display(proof)} imports {imported}, a LeanCIL repository " + @@ -79,6 +89,8 @@ internal static int VerifyBatch(string[] args) var manifest = JsonSerializer.Deserialize(File.ReadAllText(manifestPath), new JsonSerializerOptions { UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow }) ?? throw new InvalidDataException("Missing batch manifest."); + if (manifest.requests?.Any(request => request?.expect == "unproved") == true) + throw new InvalidDataException(BatchManifestError); var run = RunBatch(manifest, Path.GetDirectoryName(manifestPath)!, options); var count = run.Results.Length; var failed = run.Results.Count(result => result.status == "failed"); @@ -158,7 +170,11 @@ static BatchOptions ParseBatchOptions(string[] args) } /// - /// Verifies every request of `manifest`, whose paths are relative to `directory`, in one session. + /// Verifies every request of `manifest`, whose paths are relative to `directory`. A `rejected` request is met + /// only when its theorem verifies as a refutation of the contract whose `Holds` the request it `refutes` + /// verified: the kernel accepts it at that statement's counterexample predicate, read in its own extraction. + /// Requests that expect verification run first, so each refutation is compared with a verified statement; an + /// `unproved` request, which only gates send, passes when its proof fails at proof checking. /// Prints each request's text outcome as soon as it is final, unless the options ask for JSON. /// `statements`, parallel to the requests, says what each request's theorem audit reports of its statement. /// @@ -171,15 +187,23 @@ static BatchRun RunBatch(BatchManifest manifest, string directory, BatchOptions if (manifest.schema != 1 || string.IsNullOrWhiteSpace(manifest.proofRoot) || manifest.requests is null || manifest.requests.Length == 0 || manifest.requests.Any(request => request is null || string.IsNullOrWhiteSpace(request.output) || string.IsNullOrWhiteSpace(request.proof) || string.IsNullOrWhiteSpace(request.theorem) || - request.expect is not (null or "verified" or "rejected"))) - throw new InvalidDataException("Invalid batch manifest: expected schema 1, proofRoot and requests with output, proof, theorem and optional expect (verified or rejected)."); + request.expect is not (null or "verified" or "rejected" or "unproved") || + (request.refutes is null) == (request.expect == "rejected"))) + throw new InvalidDataException(BatchManifestError); + var count = manifest.requests.Length; + for (var i = 0; i < count; i++) + if (manifest.requests[i].refutes is { } target && + (target < 0 || target >= count || manifest.requests[target].expect is not (null or "verified"))) + throw new InvalidDataException($"Request {i} refutes request {target}, which is not a request " + + "of this batch that expects verification."); + if (statements is not null && manifest.requests.Any(request => request.expect == "rejected")) + throw new ArgumentException("A batch with given statements compares no refutation of its own."); var proofRoot = Path.GetFullPath(manifest.proofRoot, directory); - if (checkOnly && manifest.requests.Any(request => request.expect == "rejected")) + if (checkOnly && manifest.requests.Any(request => request.expect is "rejected" or "unproved")) throw new ArgumentException("--check accepts compilation requests, not rejection expectations."); - var count = manifest.requests.Length; var outcomes = new ConsumerOutcome?[count]; - var requests = new List(); - var indices = new List(); + var assemblies = new string?[count]; + var settings = new string?[count]; var outputs = manifest.requests.Select(request => Path.GetFullPath(request.output, directory)).ToArray(); var proofs = manifest.requests.Select(request => Path.GetFullPath(request.proof, directory)).ToArray(); var lastForOutput = Enumerable.Range(0, count).GroupBy(i => outputs[i], OperatingSystem.IsWindows() @@ -205,19 +229,33 @@ request.expect is not (null or "verified" or "rejected"))) File.WriteAllText(settingsPath, JsonSerializer.Serialize(new VerificationSettings(1, Path.GetRelativePath(outputs[i], proofs[i]).Replace('\\', '/'), request.theorem, Path.GetRelativePath(outputs[i], proofRoot).Replace('\\', '/')), Json) + "\n"); + settings[i] = settingsPath; } stage = "extraction integrity"; var identity = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(outputs[i], "identity.json"))); - var assembly = Path.GetFullPath(identity.GetProperty("assemblyPath").GetString()!, outputs[i]); - requests.Add(new(assembly, outputs[i], proofs[i], request.theorem, lastForOutput.Contains(i) ? settingsPath : null, - request.expect == "rejected", Statement: statements?[i])); - indices.Add(i); + assemblies[i] = Path.GetFullPath(identity.GetProperty("assemblyPath").GetString()!, outputs[i]); } catch (Exception error) { outcomes[i] = new ConsumerOutcome { Stage = stage, Error = error }; } } + var refuted = manifest.requests.Where(request => request.refutes is not null) + .Select(request => request.refutes!.Value).ToHashSet(); + + // A refutation that verified but does not refute its target's contract fails at statement identity. + void JudgeRefutation(int i, ConsumerOutcome outcome) + { + if (manifest.requests[i].refutes is not { } target || outcome.Error is not null || outcome.Report is null) + return; + var origin = outcomes[target]!; + var (_, error) = CompareStatement(refutes: true, manifest.requests[i].theorem, + $"Request {target} ({CliPaths.Display(outputs[target])} {manifest.requests[target].theorem})", + origin.Stage, origin.Error is null ? origin.Report : null, outcome.Report); + if (error is null) return; + (outcome.Stage, outcome.Error) = (StatementIdentityStage, new InvalidOperationException(error)); + if (lastForOutput.Contains(i)) File.Delete(Path.Combine(outputs[i], "verified.json")); + } // Print each request as soon as it is final, so a long batch shows progress; workspaces run in parallel. var printed = new bool[count]; @@ -227,7 +265,7 @@ bool Print(int i, ConsumerOutcome outcome) var request = manifest.requests[i]; var expect = checkOnly ? "checked" : request.expect ?? "verified"; var passed = outcome.Error is null && (checkOnly ? outcome.Checked - : expect == "rejected" ? outcome.Rejected : outcome.Report is not null); + : expect == "unproved" ? outcome.Rejected : outcome.Report is not null); lock (gate) { if (printed[i]) return passed; @@ -238,14 +276,19 @@ bool Print(int i, ConsumerOutcome outcome) Console.WriteLine(checkOnly ? $"PASS Checked {CliPaths.Display(outputs[i])} {request.theorem} " + $"({outcome.ElapsedSeconds:F3}s batch-to-result latency)" - : expect == "rejected" - ? $"PASS rejected {CliPaths.Display(outputs[i])} {request.theorem} ({outcome.Stage})" + : expect == "unproved" + ? $"PASS unproved {CliPaths.Display(outputs[i])} {request.theorem} ({outcome.Stage})" + : expect == "rejected" && outcome.Report?.refutation is { } refutation + ? $"PASS rejected {CliPaths.Display(outputs[i])} {request.theorem} (refutes { + refutation.contract} for {refutation.method.signature})" : $"PASS verified {CliPaths.Display(outputs[i])} {request.theorem} " + $"({outcome.ElapsedSeconds:F3}s batch-to-result latency)"); else { var error = outcome.Located() ?? new InvalidOperationException("Verification produced no result."); - Console.WriteLine($"FAIL {CliPaths.Display(outputs[i])} {request.theorem} (stage: {outcome.Stage})"); + Console.WriteLine($"FAIL {CliPaths.Display(outputs[i])} {request.theorem} (stage: { + outcome.Stage})" + (expect == "rejected" ? $", no verified refutation of request { + request.refutes}" : "")); CliDiagnostics.Write(new CliFailure(outcome.Stage, error), outcome.Stage, true); } if (trace) Console.Write(TraceReport(Traced(outcome.ProofMessages(proofs[i]), true).Trace, proofs[i])); @@ -254,18 +297,40 @@ bool Print(int i, ConsumerOutcome outcome) return passed; } - using (var verification = new Program + // Each refutation is compared with the statement of the request it refutes, so those run first; both + // passes share one session and its library. + using var verification = new Program { freshProofs = fresh, freshLibrary = rebuildLibrary, structuredFailures = json, tracesSteps = trace - }) + }; + var selected = Enumerable.Range(0, count).Where(i => outcomes[i] is null).ToArray(); + var (refutations, others) = (selected.Where(i => manifest.requests[i].expect == "rejected").ToArray(), + selected.Where(i => manifest.requests[i].expect != "rejected").ToArray()); + Verify(others, i => statements?[i] ?? (refuted.Contains(i) ? new(Term: true) : null)); + Verify(refutations, i => outcomes[manifest.requests[i].refutes!.Value] is + { Error: null, Report: { } origin } && origin.statementIdentity?.term is { } term && StatesContract(term) + ? new(false, term.GetRawText(), Refuted: true) : null); + + void Verify(int[] indices, Func statement) { - var verified = requests.Count == 0 ? [] : verification.VerifyConsumerRequests(proofRoot, requests, null, batch: true, - parallelism ?? Math.Max(1, Environment.ProcessorCount / 8), - (k, outcome) => Print(indices[k], outcome), checkOnly: checkOnly); - for (var k = 0; k < verified.Length; k++) outcomes[indices[k]] = verified[k]; + if (indices.Length == 0) return; + var requests = indices.Select(i => new ConsumerRequest(assemblies[i]!, outputs[i], proofs[i], + manifest.requests[i].theorem, settings[i], manifest.requests[i].expect == "unproved", + Statement: statement(i))).ToList(); + var verified = verification.VerifyConsumerRequests(proofRoot, requests, null, batch: true, + parallelism ?? Math.Max(1, Environment.ProcessorCount / 8), (k, outcome) => + { + JudgeRefutation(indices[k], outcome); + Print(indices[k], outcome); + }, checkOnly: checkOnly); + for (var k = 0; k < verified.Length; k++) + { + JudgeRefutation(indices[k], verified[k]); + outcomes[indices[k]] = verified[k]; + } } - // A rejected last request leaves no certificate for its output, as in sequential runs. - foreach (var i in lastForOutput.Where(i => !indices.Contains(i))) + // A last request rejected before verification leaves no certificate for its output, as in sequential runs. + foreach (var i in lastForOutput.Where(i => assemblies[i] is null)) File.Delete(Path.Combine(outputs[i], "verified.json")); var results = new List(); @@ -280,7 +345,8 @@ bool Print(int i, ConsumerOutcome outcome) // A verified refutation is a certificate and a failed contract; any other pass leaves no failure. var failure = outcome.Located() is { } error ? FailureReports.Failed(outcome.Stage, error, request.theorem, proofs[i], outputs[i], proofRoot) - : outcome.Report is { } report ? FailureReports.Refuted(report.theoremAudit, request.theorem, proofs[i], outputs[i], proofRoot) + : outcome.Report is { } report ? FailureReports.Refuted(report.refutation, report.theoremAudit, + request.theorem, proofs[i], outputs[i], proofRoot) : outcome.Checked ? null : FailureReports.Classify(outcome.Stage, outcome.Detail ?? "Verification produced no result.", outcome.Hidden, request.theorem, proofs[i], outputs[i], proofRoot, outcome.Sources); @@ -289,8 +355,11 @@ bool Print(int i, ConsumerOutcome outcome) if (failure is null) FailureReports.Delete(outputs[i]); else FailureReports.Write(outputs[i], failure); } + var message = outcome.Error?.Message ?? "Verification produced no result."; results.Add(new(outputs[i], proofs[i], request.theorem, expect, status, outcome.Stage, - passed ? null : (outcome.Error?.Message ?? "Verification produced no result."), + passed ? null : expect == "rejected" + ? $"No verified refutation of request {request.refutes} (stage: {outcome.Stage}): {message}" + : message, outcome.Report?.axioms, outcome.Report?.libraryBuild, failure, outcome.ElapsedSeconds)); if (json) reports.Add(BatchEntry(outcome, failure, expect, status, request.theorem, proofs[i], outputs[i])); } @@ -315,11 +384,32 @@ JsonObject BatchEntry(ConsumerOutcome outcome, FailureReports.Report? failure, s var report = VerifyReports.Result(verified ? "verified" : "failed", failure, theorem, proof, output, proofRoot, verified ? null : outcome.Stage, outcome.Report?.axioms, outcome.Report is { } checkedReport ? StatementOf(checkedReport) : null, JsonMessages(messages), trace, - messages is not null); + messages is not null, binding: verified ? outcome.Report!.binding : null); return new JsonObject { ["expect"] = expect, ["status"] = status, ["report"] = report }; } } + /// The binding facts and inputs recorded by the completed verification. + static IEnumerable BindingLines(BindingSection binding) + { + static string Name(T value) => JsonSerializer.SerializeToElement(value).GetString()!; + yield return "Binding: " + Name(binding.mode); + if (binding.digests is { } digests) + yield return "Digests: " + CliPaths.Display(digests.path) + " (SHA-256 " + digests.sha256 + ")"; + foreach (var fact in binding.facts) + yield return Name(fact.id) + ": " + Name(fact.status) + + (fact.theorem is { } theorem ? " (" + theorem + ")" + : fact.reason is { } reason ? " (" + reason + ")" : ""); + foreach (var image in binding.images) + yield return "Image " + image.identity + ": SHA-256 " + image.sha256 + ", " + Name(image.role) + + ", " + Name(image.status) + " (" + (image.theorem ?? image.reason) + ")"; + if (binding.sources is { } sources) + { + yield return "StrictBinding sources: " + sources.strictBinding; + yield return "Lean toolchain: " + sources.leanToolchain + " (ran " + sources.lean + ")"; + } + } + internal static void VerifyArtifacts(string[] args) { if (args.Length == 0 || args[0].StartsWith("--", StringComparison.Ordinal)) @@ -337,6 +427,8 @@ internal static void VerifyArtifacts(string[] args) var rebuildLibrary = false; string? proofRoot = null; bool forceFull = false; + string? digestFile = null; + DeepCheckRun? deepRun = null; Dictionary fullArtifacts = new(StringComparer.Ordinal); for (var index = 1; index < args.Length; index++) @@ -382,6 +474,9 @@ internal static void VerifyArtifacts(string[] args) !fullArtifacts.TryAdd(artifact[0], Path.GetFullPath(artifact[1]))) throw Rejected(new ArgumentException("--full-artifact requires unique Lib=directory values.")); break; + case "--deep-checks" when digestFile is null: + digestFile = args[index]; + break; case "--proof-root" when proofRoot is null: proofRoot = Path.GetFullPath(args[index]); break; @@ -438,14 +533,6 @@ void RequireNoOpenGoals() theorem ??= saved.theorem; if (saved.proofRoot is not null) proofRoot ??= Path.GetFullPath(saved.proofRoot, output); } - else if (proof is null && File.Exists(reportPath)) - { - // Migrate proof selection from reports created before settings were separate. - var previous = JsonSerializer.Deserialize(File.ReadAllText(reportPath)) - ?? throw new InvalidDataException("Missing verification report."); - proof = previous.inputs.Keys.Single(key => key.StartsWith("proof:", StringComparison.Ordinal))[6..]; - theorem ??= previous.theorem; - } File.Delete(reportPath); File.Delete(Path.Combine(output, ProofMessagesText)); @@ -474,6 +561,13 @@ void RequireNoOpenGoals() var identity = manifest.GetProperty("assemblyIdentity").GetString()!; var entry = manifest.GetProperty("entry").GetString()!; + if (digestFile is not null) + { + stage = "arguments"; + deepRun = new(DeepCheckInputs.Read(digestFile)); + stage = "extraction integrity"; + } + verification = new() { freshProofs = fresh || forceFull, @@ -496,8 +590,10 @@ void Advance(string next) if (forceFull) verification.PreflightFullImports(output, fullArtifacts); var report = proofRoot is null ? verification.Verify(assembly, identity, entry, proof, output, - theorem: theorem, existingArtifacts: true, settings: settingsPath, stage: Advance) - : verification.VerifyConsumer(assembly, proof, output, theorem, proofRoot, settingsPath, Advance); + theorem: theorem, existingArtifacts: true, settings: settingsPath, stage: Advance, + deepChecks: deepRun?.Inputs, deepRun: deepRun) + : verification.VerifyConsumer(assembly, proof, output, theorem, proofRoot, settingsPath, Advance, + deepChecks: deepRun?.Inputs, deepRun: deepRun); RequireNoOpenGoals(); var view = verification.VerifiedStatement; @@ -507,13 +603,14 @@ void Advance(string next) JsonSerializer.Serialize(report with { specification = specification }, Json) + "\n"); if (json) { - var refutation = FailureReports.Refuted(report.theoremAudit, theorem, proof, output, proofRoot); + var refutation = FailureReports.Refuted(report.refutation, report.theoremAudit, theorem, proof, + output, proofRoot); if (refutation is not null) FailureReports.Write(output, refutation); ProofMessagesReport(Observed(verification.ProofMessages, trace, goals).Messages, proof, output); Console.Write(VerifyReports.Serialize(VerifyReports.Result("verified", refutation, report.theorem, proof, output, proofRoot, null, report.axioms, view?.statement ?? StatementOf(report), JsonMessages(verification.ProofMessages), trace, verification.ProofMessages is not null, - goals, Specification(view)))); + goals, Specification(view), report.binding))); reported = true; return; } @@ -547,7 +644,9 @@ void Advance(string next) Console.WriteLine(" Independently composed theorem: " + report.compositionTheorem); Console.WriteLine($" Proof: {CliPaths.Display(proof)}"); Console.WriteLine($" Axioms: {(report.axioms.Length == 0 ? "none" : string.Join(", ", report.axioms))}"); - var refuted = FailureReports.Refuted(report.theoremAudit, theorem, proof, output, proofRoot); + foreach (var line in BindingLines(report.binding)) Console.WriteLine(" " + line); + var refuted = FailureReports.Refuted(report.refutation, report.theoremAudit, theorem, proof, output, + proofRoot); if (refuted is not null) { FailureReports.Write(output, refuted); @@ -575,6 +674,7 @@ void Advance(string next) (stage, error) = (OpenGoalsStage, OpenGoals(open)); var failed = FailureReports.Failed(stage, error, theorem ?? "ExampleProof.correctness", proof ?? Path.Combine(output, "Proof.lean"), output, proofRoot); + failed = failed with { binding = deepRun?.Attempt?.Failure(stage, error.Message) }; FailureReports.Write(output, failed); var (proofMessages, steps, openGoals) = Observed(verification?.ProofMessages, trace, goals); // With --goals the terminal shows the goals alone; the proof's messages stay in their file. diff --git a/Extractor/Reachability.cs b/Extractor/Reachability.cs index 9ab795571..23633eefe 100644 --- a/Extractor/Reachability.cs +++ b/Extractor/Reachability.cs @@ -12,8 +12,7 @@ internal static Dictionary Certify(MethodDefiniti { FeatureProfile profile = selectedProfile ?? FeatureProfile.Scalar; profile.Validate(); - MemoryPrefixes.Validate(method); - TailCalls.ValidateStructure(method); + InstructionPrefixes.Validate(method); var instructions = method.Body.Instructions; var boundaries = instructions.ToHashSet(); foreach (Instruction instruction in instructions) @@ -38,7 +37,6 @@ instruction.OpCode.FlowControl is FlowControl.Branch or FlowControl.Cond_Branch // Facts only move from a constant to unknown, so this reaches a fixed point. var typeTests = TypeTests.Analyze(method); var throws = Throws.Analyze(method); - var spans = Spans.Analyze(method, profile); InstructionIndex? positions = null; Queue pending = new(); HashSet queued = [instructions[0]]; @@ -69,7 +67,7 @@ void Successor(Instruction target) if (queued.Add(target)) pending.Enqueue(target); } - // Folded operands push nothing, so a fold must be entered at its start. + // A jump must not bypass the operands used to establish a folded result. void ValidateJump(Instruction target) { if (TailCalls.IsInterior(target)) @@ -78,8 +76,6 @@ void ValidateJump(Instruction target) throw new InvalidDataException($"Branch into a folded type comparison: {i}"); if (throws.Interior.Contains(target)) throw new InvalidDataException($"Branch into a folded throw: {i}"); - if (spans.Interior.Contains(target)) - throw new InvalidDataException($"Branch into a folded span receiver: {i}"); } if (i.Operand is Instruction target) diff --git a/Extractor/ReferenceArrays.cs b/Extractor/ReferenceArrays.cs index 3b3916d64..af154aec4 100644 --- a/Extractor/ReferenceArrays.cs +++ b/Extractor/ReferenceArrays.cs @@ -12,33 +12,22 @@ internal static bool Is(TypeReference type) => type is ArrayType { IsVector: true } array && Element(array.ElementType); /// - /// An admitted class, or a non-generic class outside the model other than System.Object, such as - /// string. An array of such a static type holds, at run time, elements of a class derived from it, so - /// the stored object's base classes decide covariance exactly; an interface or object element would - /// also admit interface arrays, whose check the model cannot decide. + /// An admitted class or string. An array of such a static type holds, at run time, elements of a class + /// derived from it, so the stored object's base classes decide covariance exactly. Lean binds an element named + /// as an object only to a class of a pinned assembly or string: an interface, object or other + /// class outside the pins may type an interface array, whose check the model cannot decide. /// - internal static bool Element(TypeReference element) => ObjectTypes.Is(element) || - element is not (TypeSpecification or GenericParameter) && !element.IsValueType && - element.FullName != "System.Object" && ObjectTypes.Opaque(element) && - element.Resolve() is { IsInterface: false, HasGenericParameters: false, IsValueType: false }; + internal static bool Element(TypeReference element) => ObjectTypes.Is(element) || ObjectTypes.IsString(element); /// /// The checked operation for an array instruction over an array of class references, or null when the - /// instruction is not one. says whether ldlen reads such an array. + /// instruction is not one; ldlen is the same instruction for every array. /// - internal static string? Translate(Instruction instruction, bool arrayOperand) => instruction.OpCode.Code switch + internal static Operation? Translate(Instruction instruction) => instruction.OpCode.Code switch { Code.Newarr when instruction.Operand is TypeReference element && Element(element) => - $".newReferenceArray ({GenericEvidence.ObjectType(element)})", - Code.Ldlen when arrayOperand => ".memory .referenceArrayLength", - Code.Ldelem_Ref => ".memory .referenceArrayLoad", - Code.Stelem_Ref => ".memory .referenceArrayStore", - Code.Ldelema or Code.Ldelem_Any or Code.Stelem_Any - when instruction.Operand is TypeReference element && - (ObjectTypes.Candidate(element) || Element(element)) => - throw new InvalidDataException("Unsupported typed element access on an array of class references: " + - instruction.OpCode.Name + " " + element.FullName + - "\n Element addresses would expose a reference slot; only ldelem.ref and stelem.ref are modelled."), + Unoutlined.Site, + Code.Ldelem_Ref or Code.Stelem_Ref => new StackOperation(instruction.OpCode.Code), _ => null }; } diff --git a/Extractor/ReferenceQueries.cs b/Extractor/ReferenceQueries.cs index 2ceb91006..947164e60 100644 --- a/Extractor/ReferenceQueries.cs +++ b/Extractor/ReferenceQueries.cs @@ -14,7 +14,7 @@ private sealed class ValidatedStorage { if (reference.Name != "IsReferenceOrContainsReferences" || reference.DeclaringType.FullName != "System.Runtime.CompilerServices.RuntimeHelpers") return null; - if (!RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime") || + if (!RuntimeModels.ExactScope(reference.DeclaringType, PlatformAssemblies.SystemRuntime) || reference is not GenericInstanceMethod { GenericArguments.Count: 1 } generic || generic.ElementMethod.GenericParameters.Count != 1 || reference.HasThis || reference.ExplicitThis || (generic.ElementMethod.CallingConvention & ~MethodCallingConvention.Generic) != diff --git a/Extractor/ResolvedCallSites.cs b/Extractor/ResolvedCallSites.cs new file mode 100644 index 000000000..e62ee29f0 --- /dev/null +++ b/Extractor/ResolvedCallSites.cs @@ -0,0 +1,130 @@ +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace Nethermind.LeanCIL; + +internal enum CallKind { Static, Instance, Tail } + +/// The managed targets of reachable raw sites in one extraction. +internal sealed class ResolvedCallSites +{ + readonly Dictionary direct = []; + readonly Dictionary virtualSlots = []; + readonly Dictionary instanceLinks = []; + readonly HashSet modelCalls = []; + + internal void Add(Instruction site, MethodDefinition target) + { + if (site.OpCode.Code is Code.Call or Code.Callvirt or Code.Newobj or Code.Ldftn) + direct.Add(site, target); + } + + internal void AddVirtual(Instruction site, VirtualDispatch.Entry[] targets) => virtualSlots.Add(site, targets); + + internal void AddModel(Instruction site) => modelCalls.Add(site); + + internal bool IsModel(Instruction site) => modelCalls.Contains(site); + + /// The closed managed target selected for this site and its extraction index. + internal (MethodDefinition Method, int Index) Target(Instruction site, + IReadOnlyDictionary indices) => + direct.TryGetValue(site, out var target) && indices.TryGetValue(target, out var index) + ? (target, index) : throw new InvalidDataException($"Unsupported or unresolved call: {site.Operand}"); + + /// The bound direct callee and its stack arity at a call instruction in a proof outline. + internal (CallKind Kind, int Method, int Arguments)? OutlineTarget(Instruction site, + IReadOnlyDictionary indices, ImportResolution? imports) + { + if (site.OpCode.Code is not (Code.Call or Code.Callvirt)) return null; + if (imports?.Slot(site) is int slot) + return (CallKind.Static, indices.Count + slot, ((MethodReference)site.Operand).Parameters.Count); + if (!direct.TryGetValue(site, out var callee) || virtualSlots.ContainsKey(site)) return null; + var kind = TailCalls.IsCall(site) ? CallKind.Tail : + ObjectTypes.ReceiverCall(site) ? CallKind.Instance : CallKind.Static; + return (kind, indices[callee], callee.Parameters.Count + (callee.HasThis ? 1 : 0)); + } + + /// The validated receiver cases of a virtual slot, in dispatch order. + internal VirtualDispatch.Entry[]? VirtualTargets(Instruction site) => virtualSlots.GetValueOrDefault(site); + + /// The admitted target kind for this raw call, address or constructor, including its prefixes. + internal bool BindsOperand(Instruction site, ImportResolution? imports) + { + var code = site.OpCode.Code; + if (imports?.Slot(site) is not null) + return code == Code.Call && site.Previous?.OpCode.Code != Code.Constrained && !TailCalls.IsCall(site); + return code switch + { + Code.Call or Code.Callvirt when site.Previous?.OpCode.Code == Code.Constrained => direct.ContainsKey(site), + Code.Call when TailCalls.IsCall(site) => direct.ContainsKey(site), + Code.Call or Code.Newobj => direct.ContainsKey(site) || IsModel(site), + Code.Callvirt => site.Previous?.OpCode.OpCodeType != OpCodeType.Prefix && ObjectTypes.ReceiverCall(site) && + (direct.ContainsKey(site) || virtualSlots.ContainsKey(site)), + Code.Ldftn => direct.ContainsKey(site), + Code.Calli => site.Operand is CallSite, + _ => true + }; + } + + /// Each occupied pc names its managed targets in program order. Model calls have no link. + internal List<(int Pc, string Link)> Links(MethodDefinition method, IReadOnlySet reachable, + IReadOnlyDictionary indices, ImportResolution? imports, + MetadataValidation.LayoutContext layouts, SortedSet? linkedMethods) + { + int Callee(int index) + { + linkedMethods?.Add(index); + return index; + } + + List<(int, string)> entries = []; + for (var pc = 0; pc < method.Body.Instructions.Count; pc++) + { + var site = method.Body.Instructions[pc]; + if (!reachable.Contains(site)) continue; + if (imports?.Slot(site) is int slot) + entries.Add((pc, $".direct {Callee(indices.Count + slot)}")); + else if (site.OpCode.Code == Code.Calli && site.Operand is CallSite signature) + { + var catalog = ManagedPointers.Catalog(signature, indices, indices[method], + site.Previous?.OpCode.Code == Code.Tail); + string Name(int index) => LeanText.MethodNumber(Callee(index)); + var table = string.Join(", ", catalog.Select(target => target.Lean(Name))); + entries.Add((pc, $".indirect [{table}]")); + } + else if (virtualSlots.TryGetValue(site, out var targets)) + { + var cases = new string[targets.Length]; + for (var position = 0; position < targets.Length; position++) + { + var entry = targets[position]; + cases[position] = $"({GenericEvidence.ObjectType(entry.Class)}, {Callee(indices[entry.Body])})"; + } + entries.Add((pc, $".virtual [{string.Join(", ", cases)}]")); + } + else if (direct.TryGetValue(site, out var target)) + { + var index = Callee(indices[target]); + var link = site.OpCode.Code switch + { + Code.Ldftn => $".address {index}", + Code.Newobj when ObjectTypes.Candidate(target.DeclaringType) => + InstanceLink(target, index), + Code.Newobj => $".aggregate {index} {layouts.Size(MethodSpecialization.DeclaringType(target))}", + _ => $".direct {index}" + }; + entries.Add((pc, link)); + } + } + return entries; + } + + string InstanceLink(MethodDefinition constructor, int index) + { + if (instanceLinks.TryGetValue(constructor, out var link)) return link; + var allocation = ObjectTypes.AllocationOf(constructor); + link = $".object {index} {LeanText.ClassList(allocation.Bases)} {allocation.Size}"; + instanceLinks.Add(constructor, link); + return link; + } +} diff --git a/Extractor/RuntimeAssetSelection.cs b/Extractor/RuntimeAssetSelection.cs index 0d07723b7..7f70c4871 100644 --- a/Extractor/RuntimeAssetSelection.cs +++ b/Extractor/RuntimeAssetSelection.cs @@ -56,11 +56,12 @@ static string Text(JsonElement parent, string name) => { string target = Text(Object(deps, "runtimeTarget"), "name"); string[] parts = target.Split('/'); - if (parts.Length > 2 || !Framework(parts[0])) throw new InvalidDataException("Contract discovery requires net10.0: " + target); + if (parts.Length > 2 || !Framework(parts[0])) + throw new InvalidDataException("Contract discovery requires net11.0: " + target); return parts.Length == 2 ? parts[1] : null; } - static bool Framework(string target) => target is "net10.0" or ".NETCoreApp,Version=v10.0"; + static bool Framework(string target) => target is "net11.0" or ".NETCoreApp,Version=v11.0"; internal static string Architecture(string rid) { @@ -120,7 +121,9 @@ internal static Dictionary Select(JsonElement root, bool d name.Split('/') is [string ridFramework, string targetRid] && Framework(ridFramework) && targetRid == rid).ToArray(); string[] exact = choices.Where(name => name.Contains('/')).ToArray(); string[] selected = exact.Length > 0 ? exact : choices; - if (selected.Length != 1) throw new InvalidDataException("Missing or ambiguous net10.0 discovery target for RID " + (rid ?? "portable")); + if (selected.Length != 1) + throw new InvalidDataException( + "Missing or ambiguous net11.0 discovery target for RID " + (rid ?? "portable")); target = selected[0]; } JsonElement selectedTarget = Object(targets, target); diff --git a/Extractor/RuntimeModels.cs b/Extractor/RuntimeModels.cs index 894e59aed..40ad5d2b5 100644 --- a/Extractor/RuntimeModels.cs +++ b/Extractor/RuntimeModels.cs @@ -1,4 +1,5 @@ using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; @@ -6,11 +7,6 @@ internal static class RuntimeModels { private static string Vector(int width, string element) => $"System.Runtime.Intrinsics.Vector{width}`1<{element}>"; - private static readonly Version RuntimeAssemblyVersion = new(10, 0, 0, 0); - // The Open key signs System.Memory and System.Runtime.Intrinsics; the Microsoft key signs the rest. - private const string OpenKeyToken = "cc7b13ffcd2ddd51"; - private const string MicrosoftKeyToken = "b03f5f7f11d50a3a"; - private static readonly int[] VectorWidths = [128, 256, 512]; private static readonly string[] VectorElements = [ @@ -35,13 +31,17 @@ internal static class RuntimeModels private const string V256Double = $"{Intrinsics}.Vector256`1"; private const string V256Single = $"{Intrinsics}.Vector256`1"; private const string V512UInt64 = $"{Intrinsics}.Vector512`1"; - private const string UInt64Pair = "System.ValueTuple`2"; + internal const string X86DivRemSignature = "System.ValueTuple`2 " + + $"{X86}.X86Base/X64::DivRem(System.UInt64,System.UInt64,System.UInt64)"; private const string Unsafe = "System.Runtime.CompilerServices.Unsafe"; - internal static bool ExactScope(IMetadataScope scope, string name) => scope is AssemblyNameReference assembly && - assembly.Name == name && assembly.Version == RuntimeAssemblyVersion && - string.IsNullOrEmpty(assembly.Culture) && Convert.ToHexString(assembly.PublicKeyToken).ToLowerInvariant() == - (name is "System.Runtime.Intrinsics" or "System.Memory" ? OpenKeyToken : MicrosoftKeyToken); + /// Whether a scope is the row that names , column for column. + internal static bool ExactScope(TypeReference type, AssemblyName platform) => + type.Scope is AssemblyNameReference && AssemblyName.OfScope(type, IdentityImage(type)) == platform; + + static MetadataImage IdentityImage(TypeReference type) => + (PinnedAssemblies.Active ?? throw new InvalidDataException("Type identity without a pinned module image")) + .IdentityMetadata(type.Module); internal static bool SupportedType(TypeReference type) { @@ -56,7 +56,8 @@ private static bool IsSupportedVector(string name) => VectorWidths.Any(width => VectorElements.Any(element => name == Vector(width, element))); internal static bool HasRuntimeValueTypeBase(TypeDefinition type) => - type.BaseType is { FullName: "System.ValueType" } parent && ExactScope(parent.Scope, "System.Runtime"); + type.BaseType is { FullName: "System.ValueType" } parent && + ExactScope(parent, PlatformAssemblies.SystemRuntime); internal static void ValidateTypeIdentity(TypeReference type, ModuleDefinition module, string architecture = "scalar") @@ -96,7 +97,7 @@ internal static void ValidateTypeIdentity(TypeReference type, ModuleDefinition m } bool expectedValueType = IsModeledValueType(type.FullName); var expectedScope = DefiningAssembly(type.FullName); - bool valid = type.IsValueType == expectedValueType && ExactScope(type.Scope, expectedScope) && + bool valid = type.IsValueType == expectedValueType && ExactScope(type, expectedScope) && (type.FullName != "System.Void" || type.MetadataType == MetadataType.Void); if (!valid) throw new InvalidDataException($"Unsupported type identity: {type.FullName} [{type.Scope}]"); } @@ -108,10 +109,11 @@ private static bool IsModeledValueType(string name) => name is "System.UInt64" o "System.Runtime.Intrinsics.Vector128`1" or "System.Runtime.Intrinsics.Vector256`1" or "System.Runtime.Intrinsics.Vector512`1" or "System.ValueTuple`2"; - private static string DefiningAssembly(string name) => - name is "System.Buffers.Binary.BinaryPrimitives" or "System.MemoryExtensions" ? "System.Memory" : - name.StartsWith("System.Runtime.Intrinsics.", StringComparison.Ordinal) ? "System.Runtime.Intrinsics" : - "System.Runtime"; + private static AssemblyName DefiningAssembly(string name) => + name is "System.Buffers.Binary.BinaryPrimitives" or "System.MemoryExtensions" + ? PlatformAssemblies.SystemMemory : + name.StartsWith("System.Runtime.Intrinsics.", StringComparison.Ordinal) + ? PlatformAssemblies.SystemRuntimeIntrinsics : PlatformAssemblies.SystemRuntime; internal static void ValidateSignature(MethodReference reference, string architecture = "scalar") { @@ -132,42 +134,100 @@ internal static bool ObjectConstructor(MethodReference reference) => reference is { Name: ".ctor", HasThis: true, ExplicitThis: false, Parameters.Count: 0 } && reference is not GenericInstanceMethod && reference.CallingConvention == MethodCallingConvention.Default && reference.ReturnType.MetadataType == MetadataType.Void && - reference.DeclaringType is { FullName: "System.Object" } owner && ExactScope(owner.Scope, "System.Runtime"); + reference.DeclaringType is { FullName: "System.Object" } owner && + ExactScope(owner, PlatformAssemblies.SystemRuntime); - internal static string? Translate(MethodReference reference, FeatureProfile profile) + /// + /// A modelled call keeps its method operand. Analyses use the returned operation, including + /// the checked write for an integer helper with an out parameter. + /// + internal static Operation? CallModel(MethodReference reference, FeatureProfile profile) { - if (ReferenceQueries.Constant(reference, profile.Architecture) is int constant) - return $".const32 {constant}"; - if (reference.ExplicitThis || reference.CallingConvention == MethodCallingConvention.VarArg || - (reference.HasThis && reference.Name is not - (".ctor" or "get_Item" or "get_Length" or "Slice" or "Clear" or "Fill" or - "CopyTo" or "TryCopyTo" or "get_IsEmpty" or "GetPinnableReference")) || - (reference.Name == ".ctor" && !reference.HasThis)) return null; - string scope = reference.DeclaringType.Scope.Name; - if (scope is "System.Runtime" or "System.Runtime.Intrinsics" or "System.Memory" && - !ExactScope(reference.DeclaringType.Scope, scope)) - throw new InvalidDataException( - $"Unsupported runtime assembly scope: {reference.FullName} [{reference.DeclaringType.Scope}]"); - string? op = scope switch + if (FeatureProfile.PlatformConstant(reference) is not null) return Unoutlined.Site; + if (FeatureProfile.Getter(reference) is not null) return Unoutlined.Site; + if (ReferenceQueries.Constant(reference, profile.Architecture) is not null) return Unoutlined.Site; + if (ArgumentGuards.CallModel(reference, profile.Architecture) is { } guard) return guard; + if (Spans.ArrayExtension(reference, profile.Architecture) is { } arrayView) return arrayView; + if (Spans.Member(reference, profile.Architecture) is { } span) return span; + if (Spans.ConstructorOperation(reference, profile.Architecture) is not null) + return Unoutlined.Site; + if (Arrays.Fill(reference, profile.Architecture) is { } fill) return fill; + if (StaticSpanModel(reference, profile.Architecture) is { } staticSpan) return staticSpan; + if (MarshalModels.CallModel(reference, profile.Architecture) is { } marshal) return marshal; + if (BinaryModels.CallModel(reference, profile.Architecture) is { } binary) return binary; + if (ObjectConstructor(reference)) return new StackOperation(Code.Pop); + if (VectorModel(reference, profile) is { } vector) { - "System.Runtime" => Scalar(reference) - ?? SpanReinterpretation.Translate(reference, profile.Architecture) - ?? MemoryTransfers.Marshal(reference, profile.Architecture) - ?? ValueBitCasts.Translate(reference, profile.Architecture) - ?? UnsafeModels.Translate(reference, profile.Architecture) - ?? ElementSpan(reference, profile.Architecture) ?? Runtime(reference.FullName), - "System.Memory" => Scalar(reference) ?? BinaryTransfers.Translate(reference) ?? - ElementSpan(reference, profile.Architecture) ?? Span(reference.FullName), - "System.Runtime.Intrinsics" => Intrinsic(reference.FullName, profile), - _ => null + ValidateSignature(reference, profile.Architecture); + if (ValueTuples.Is(reference.ReturnType)) + ValueTuples.RequirePair(reference.ReturnType, reference.Module, profile.Architecture); + return vector; + } + if (BitModels.Admits(reference, profile)) return Unoutlined.Site; + if (UnsafeModels.CallModel(reference, profile.Architecture) is { } helper) return helper; + if (reference.HasThis || reference.ExplicitThis || reference.HasGenericParameters || + reference is GenericInstanceMethod || + reference.CallingConvention != MethodCallingConvention.Default || + !ExactScope(reference.DeclaringType, PlatformAssemblies.SystemRuntime)) return null; + var model = reference.FullName switch + { + $"System.Void {Unsafe}::CopyBlock(System.Byte&,System.Byte&,System.UInt32)" or + $"System.Void {Unsafe}::CopyBlock(System.Void*,System.Void*,System.UInt32)" or + $"System.Void {Unsafe}::CopyBlockUnaligned(System.Byte&,System.Byte&,System.UInt32)" or + $"System.Void {Unsafe}::CopyBlockUnaligned(System.Void*,System.Void*,System.UInt32)" or + $"System.Void {Unsafe}::InitBlock(System.Byte&,System.Byte,System.UInt32)" or + $"System.Void {Unsafe}::InitBlock(System.Void*,System.Byte,System.UInt32)" or + $"System.Void {Unsafe}::InitBlockUnaligned(System.Byte&,System.Byte,System.UInt32)" or + $"System.Void {Unsafe}::InitBlockUnaligned(System.Void*,System.Byte,System.UInt32)" => + Unoutlined.Site, + _ => IntegerMathCalls.Contains(reference.FullName) ? Unoutlined.Site : null }; - if (op is not null) ValidateSignature(reference, profile.Architecture); - if (op is not null && ValueTuples.Is(reference.ReturnType)) + if (model is null) return null; + ValidateSignature(reference, profile.Architecture); + if (ValueTuples.Is(reference.ReturnType)) ValueTuples.RequirePair(reference.ReturnType, reference.Module, profile.Architecture); - return op; + return model; + } + + private static readonly TypeLevel RuntimeHelpers = new(MetadataName.OfText("System.Runtime.CompilerServices"), + MetadataName.OfText("RuntimeHelpers")); + private static readonly TypeLevel ReadOnlySpan = + new(MetadataName.OfText("System"), MetadataName.OfText("ReadOnlySpan`1")); + private static readonly TypeLevel FieldHandle = + new(MetadataName.OfText("System"), MetadataName.OfText("RuntimeFieldHandle")); + + /// The runtime field handle supplies a readonly span over primitive or enum storage. + internal static Operation? StaticSpanModel(MethodReference reference, string architecture) + { + if (reference is not GenericInstanceMethod { Name: "CreateSpan", GenericArguments.Count: 1 } method || + method.HasThis || method.ExplicitThis || method.ElementMethod.GenericParameters.Count != 1 || + method.ElementMethod.CallingConvention != MethodCallingConvention.Generic || + method.DeclaringType is TypeSpecification || method.Parameters.Count != 1 || + method.ReturnType is not GenericInstanceType { GenericArguments.Count: 1 } result || + result.GenericArguments[0] != method.ElementMethod.GenericParameters[0]) return null; + var runtime = PlatformAssemblies.SystemRuntime; + var handle = method.Parameters[0].ParameterType; + if (handle is TypeSpecification || !IdentityRows.TopLevelMatches(handle, FieldHandle, runtime) || + !IdentityRows.TopLevelMatches(method.DeclaringType, RuntimeHelpers, runtime) || + !IdentityRows.TopLevelMatches(result.ElementType, ReadOnlySpan, runtime)) return null; + var element = method.GenericArguments[0]; + if (Arrays.Primitive(element) is null && !EnumTypes.IsEnum(element)) return null; + ValidateSignature(reference, architecture); + return Spans.ElementWidth(element, architecture) is not null ? Unoutlined.Site : null; } - private static readonly (string Type, int Width, bool Signed)[] MathIntegers = + internal static void ValidateCallScope(MethodReference reference) + { + if (reference.ExplicitThis || reference.CallingConvention == MethodCallingConvention.VarArg || + reference.HasThis != (reference.Name == ".ctor")) return; + if (PlatformAssemblies.Claimed(AssemblyName.OfScope( + reference.DeclaringType, IdentityImage(reference.DeclaringType))) is { } platform && + !ExactScope(reference.DeclaringType, platform)) + throw new InvalidDataException( + $"Unsupported runtime assembly scope: {reference.FullName} [{reference.DeclaringType.Scope}]"); + } + + internal static readonly (string Type, int Width, bool Signed)[] MathIntegers = [ ("System.SByte", 8, true), ("System.Byte", 8, false), ("System.Int16", 16, true), ("System.UInt16", 16, false), @@ -175,275 +235,72 @@ private static readonly (string Type, int Width, bool Signed)[] MathIntegers = ("System.Int64", 64, true), ("System.UInt64", 64, false), ("System.IntPtr", 64, true), ("System.UIntPtr", 64, false) ]; - private static readonly (string Name, string Operation, int Arity)[] MathOperations = - [ - ("Min", "min", 2), ("Max", "max", 2), ("Clamp", "clamp", 3), - ("Abs", "abs", 1), ("Sign", "sign", 1) - ]; - private static readonly (string Type, int Width)[] RotatableWords = - [("System.UInt32", 32), ("System.UInt64", 64), ("System.UIntPtr", 64)]; - private static readonly (string Name, string Operation)[] Rotations = - [("RotateLeft", "rotateLeft"), ("RotateRight", "rotateRight")]; - private static readonly (string Name, string Operation)[] BitCounts = - [ - ("PopCount", "popCount"), ("LeadingZeroCount", "leadingZeroCount"), - ("TrailingZeroCount", "trailingZeroCount"), ("Log2", "log2") - ]; - private static readonly (string Type, int Width, bool Signed)[] Pow2Integers = - [ - ("System.Int32", 32, true), ("System.UInt32", 32, false), - ("System.Int64", 64, true), ("System.UInt64", 64, false), - ("System.IntPtr", 64, true), ("System.UIntPtr", 64, false) - ]; - private static readonly (string Type, int Width, bool Signed)[] ByteAndNativeIntegers = - [ - ("System.SByte", 8, true), ("System.Byte", 8, false), - ("System.IntPtr", 64, true), ("System.UIntPtr", 64, false) - ]; + private static readonly (string Name, int Arity)[] MathOperations = + [("Min", 2), ("Max", 2), ("Clamp", 3), ("Abs", 1), ("Sign", 1)]; - private static readonly Dictionary ScalarOperations = BuildScalarOperations(); - - // Match exact non-generic static signatures, including the defining assembly. - private static string? Scalar(MethodReference reference) + // Vector helpers must bind a static declaration in the exact intrinsics assembly. + static Operation? VectorModel(MethodReference reference, FeatureProfile profile) { - if (reference.HasThis || reference.HasGenericParameters || reference is GenericInstanceMethod || - reference.CallingConvention != MethodCallingConvention.Default) return null; - return reference.DeclaringType.Scope.Name switch - { - "System.Runtime" => RuntimeScalar(reference.FullName), - "System.Memory" => ReverseEndianness(reference.FullName), - _ => null - }; - } - - private static string? RuntimeScalar(string signature) => ScalarOperations.GetValueOrDefault(signature); - - // Exact signatures share the same semantics across Math and primitive integer entry points. - private static Dictionary BuildScalarOperations() - { - var operations = new Dictionary(StringComparer.Ordinal) - { - ["System.UInt64 System.Math::BigMul(System.UInt32,System.UInt32)"] = - ".intrinsic (.scalar (.bigMul32 false)) 2", - ["System.Int64 System.Math::BigMul(System.Int32,System.Int32)"] = - ".intrinsic (.scalar (.bigMul32 true)) 2", - ["System.UInt64 System.Math::BigMul(System.UInt64,System.UInt64,System.UInt64&)"] = - ".memory (.bigMul64 false)", - ["System.Int64 System.Math::BigMul(System.Int64,System.Int64,System.Int64&)"] = ".memory (.bigMul64 true)", - ["System.Int32 System.Math::DivRem(System.Int32,System.Int32,System.Int32&)"] = ".memory (.divRemOut 32)", - ["System.Int64 System.Math::DivRem(System.Int64,System.Int64,System.Int64&)"] = ".memory (.divRemOut 64)" - }; - foreach (var (type, width, signed) in MathIntegers) - operations.Add($"System.ValueTuple`2<{type},{type}> System.Math::DivRem({type},{type})", - $".intrinsic (.scalar (.divRem {width} {LeanBool(signed)})) 2"); - foreach (var (type, width, signed) in MathIntegers) - foreach (var (name, operation, arity) in MathOperations) - { - if (!signed && arity == 1) continue; - var result = name == "Sign" ? "System.Int32" : type; - var parameters = string.Join(",", Enumerable.Repeat(type, arity)); - var model = $".intrinsic (.scalar (.math .{operation} {width} {LeanBool(signed)})) {arity}"; - operations.Add($"{result} System.Math::{name}({parameters})", model); - operations.Add($"{result} {type}::{name}({parameters})", model); - } - foreach (var (type, width) in RotatableWords) - { - foreach (var (name, operation) in Rotations) - operations.Add($"{type} System.Numerics.BitOperations::{name}({type},System.Int32)", - $".intrinsic (.scalar (.{operation} {width})) 2"); - foreach (var (name, operation) in BitCounts) - operations.Add($"System.Int32 System.Numerics.BitOperations::{name}({type})", - $".intrinsic (.scalar (.{operation} {width} .word32)) 1"); - operations.Add($"{type} System.Numerics.BitOperations::RoundUpToPowerOf2({type})", - $".intrinsic (.scalar (.roundUpToPowerOf2 {width})) 1"); - } - foreach (var (type, width, _) in MathIntegers.Where(integer => integer.Width is 32 or 64)) - foreach (var (name, operation) in Rotations) - operations.Add($"{type} {type}::{name}({type},System.Int32)", - $".intrinsic (.scalar (.{operation} {width})) 2"); - foreach (var (type, width, signed) in Pow2Integers) - foreach (var (name, operation) in BitCounts) - { - if (name == "Log2" && signed) continue; - operations.Add($"{type} {type}::{name}({type})", - $".intrinsic (.scalar (.{operation} {width} .word{width})) 1"); - } - foreach (var (type, width, signed) in Pow2Integers) - { - var model = $".intrinsic (.scalar (.isPow2 {width} {LeanBool(signed)})) 1"; - operations.Add($"System.Boolean System.Numerics.BitOperations::IsPow2({type})", model); - operations.Add($"System.Boolean {type}::IsPow2({type})", model); - } - foreach (var (type, width, signed) in MathIntegers.Where(integer => integer.Width is 8 or 16)) - { - operations.Add($"System.Boolean {type}::IsPow2({type})", - $".intrinsic (.scalar (.isPow2 {width} {LeanBool(signed)})) 1"); - foreach (var (name, operation) in BitCounts.Where(count => count.Name != "Log2")) - operations.Add($"{type} {type}::{name}({type})", - $".intrinsic (.scalar (.{operation} {width} .word32)) 1"); - if (signed) continue; - operations.Add($"{type} {type}::Log2({type})", $".intrinsic (.scalar (.log2 {width} .word32)) 1"); - foreach (var (name, operation) in Rotations) - operations.Add($"{type} {type}::{name}({type},System.Int32)", - $".intrinsic (.scalar (.{operation} {width})) 2"); - } - return operations; - } - - // System.Buffers.Binary.BinaryPrimitives, defined in System.Memory. - private static string? ReverseEndianness(string signature) - { - foreach (var (type, width, signed) in ByteAndNativeIntegers) - if (signature == $"{type} System.Buffers.Binary.BinaryPrimitives::ReverseEndianness({type})") - return $".intrinsic (.scalar (.reverseEndianness {width} {LeanBool(signed)})) 1"; - foreach (var width in new[] { 16, 32, 64 }) - foreach (var signed in new[] { false, true }) + var convention = reference is GenericInstanceMethod + ? MethodCallingConvention.Generic : MethodCallingConvention.Default; + if (reference.HasThis || reference.ExplicitThis || reference.CallingConvention != convention || + !ExactScope(reference.DeclaringType, PlatformAssemblies.SystemRuntimeIntrinsics)) return null; + if (reference is GenericInstanceMethod generic && + generic.GenericArguments.Count != generic.ElementMethod.GenericParameters.Count) return null; + if (reference is not GenericInstanceMethod && reference.HasGenericParameters) return null; + string signature = reference.FullName; + if (VectorHelperSignatures.Contains(signature)) return Unoutlined.Site; + if (signature == $"{V128UInt64} {Intrinsics}.Vector128::CreateScalarUnsafe(System.UInt64)") + return Unoutlined.Site; + foreach (int width in new[] { 128, 256 }) { - var type = $"System.{(signed ? "Int" : "UInt")}{width}"; - if (signature == $"{type} System.Buffers.Binary.BinaryPrimitives::ReverseEndianness({type})") - return $".intrinsic (.scalar (.reverseEndianness {width} {LeanBool(signed)})) 1"; + string element = $"!!0 {Intrinsics}.Vector{width}::GetElement"; + if (signature == $"{element}({Vector(width, "!!0")},System.Int32)") + return Unoutlined.Site; } - return null; + return HardwareModel(signature, profile); } - private static string LeanBool(bool value) => value.ToString().ToLowerInvariant(); - - private static string? Runtime(string signature) => signature switch - { - $"System.Void {Unsafe}::CopyBlock(System.Byte&,System.Byte&,System.UInt32)" or - $"System.Void {Unsafe}::CopyBlock(System.Void*,System.Void*,System.UInt32)" => ".memory (.copyBlock 8)", - $"System.Void {Unsafe}::CopyBlockUnaligned(System.Byte&,System.Byte&,System.UInt32)" or - $"System.Void {Unsafe}::CopyBlockUnaligned(System.Void*,System.Void*,System.UInt32)" => - ".memory (.copyBlock 1)", - $"System.Void {Unsafe}::InitBlock(System.Byte&,System.Byte,System.UInt32)" or - $"System.Void {Unsafe}::InitBlock(System.Void*,System.Byte,System.UInt32)" => ".memory (.initBlock 8)", - $"System.Void {Unsafe}::InitBlockUnaligned(System.Byte&,System.Byte,System.UInt32)" or - $"System.Void {Unsafe}::InitBlockUnaligned(System.Void*,System.Byte,System.UInt32)" => ".memory (.initBlock 1)", - $"!!1 {Unsafe}::BitCast(!!0)" => ".memory .bitcastByteBool", - "System.Void System.ReadOnlySpan`1::.ctor(System.Void*,System.Int32)" => ".memory .spanCreate", - _ => ArgumentGuards.Translate(signature) ?? Span(signature) - }; - - private static readonly Dictionary SpanOperations = BuildSpanOperations(); + private static readonly HashSet IntegerMathCalls = BuildIntegerMathCalls(); - // Signatures are immutable: runtime lookup performs no interpolation or per-element scan. - private static string? Span(string signature) => SpanOperations.GetValueOrDefault(signature); + /// Whether a modelled call binds an integer absolute-value overload. + internal static bool IntegerAbs(MethodReference reference) => + reference.Name == "Abs" && IntegerMathCalls.Contains(reference.FullName) && + ExactScope(reference.DeclaringType, PlatformAssemblies.SystemRuntime); - private sealed class ElementSpanOperations + // The call retains its complete operand. Lean selects the operation for the checked overload. + private static HashSet BuildIntegerMathCalls() { - internal readonly Dictionary> Architectures = new(StringComparer.Ordinal); - } - - private static string? ElementSpan(MethodReference method, string architecture) - { - var element = method.DeclaringType is GenericInstanceType { GenericArguments.Count: 1 } owner - ? owner.GenericArguments[0] - : method is GenericInstanceMethod { GenericArguments.Count: 1 } generic - ? generic.GenericArguments[0] : null; - if (element is null || Arrays.Primitive(element) is not null || - Spans.ElementWidth(element, architecture) is null) return null; - if (EnumTypes.IsEnum(element)) EnumTypes.Storage(element); - var architectures = EnumTypes.Cached(element, _ => new ElementSpanOperations()).Architectures; - if (!architectures.TryGetValue(architecture, out var operations)) + var calls = new HashSet(StringComparer.Ordinal) { - operations = BuildSpanOperations(element, architecture); - architectures.Add(architecture, operations); - } - return operations.GetValueOrDefault(method.FullName); - } - - private static Dictionary BuildSpanOperations( - TypeReference? elementType = null, string architecture = "scalar") - { - var operations = new Dictionary(StringComparer.Ordinal); - var primitives = new[] - { - ("System.Byte", 1), ("System.SByte", 1), ("System.Int16", 2), ("System.UInt16", 2), - ("System.Int32", 4), ("System.UInt32", 4), ("System.Int64", 8), ("System.UInt64", 8), - ("System.IntPtr", 8), ("System.UIntPtr", 8), ("System.Char", 2), ("System.Boolean", 1) + "System.UInt64 System.Math::BigMul(System.UInt32,System.UInt32)", + "System.Int64 System.Math::BigMul(System.Int32,System.Int32)", + "System.UInt64 System.Math::BigMul(System.UInt64,System.UInt64,System.UInt64&)", + "System.Int64 System.Math::BigMul(System.Int64,System.Int64,System.Int64&)", + "System.Int32 System.Math::DivRem(System.Int32,System.Int32,System.Int32&)", + "System.Int64 System.Math::DivRem(System.Int64,System.Int64,System.Int64&)" }; - foreach (var (element, width) in elementType is null ? primitives : - [(elementType.FullName, Spans.ElementWidth(elementType, architecture)!.Value)]) + foreach (var (type, _, signed) in MathIntegers) { - var arrayElement = (elementType is null ? Arrays.Primitive(element) : - Arrays.Storage(elementType, architecture))?.Expression; - var span = $"System.ReadOnlySpan`1<{element}>"; - var staticElement = elementType is null || Arrays.Primitive(elementType) is not null || - EnumTypes.IsEnum(elementType); - if (staticElement && arrayElement is not null) + calls.Add($"System.ValueTuple`2<{type},{type}> System.Math::DivRem({type},{type})"); + foreach (var (name, arity) in MathOperations) { - operations.Add($"System.Void System.Array::Fill<{element}>(!!0[],!!0)", - $".memory (.arrayFill {arrayElement} false)"); - operations.Add($"System.Void System.Array::Fill<{element}>(!!0[],!!0,System.Int32,System.Int32)", - $".memory (.arrayFill {arrayElement} true)"); - } - if (staticElement) operations.Add("System.ReadOnlySpan`1 System.Runtime.CompilerServices.RuntimeHelpers::" + - $"CreateSpan<{element}>(System.RuntimeFieldHandle)", $".memory (.createSpan {width})"); - operations.Add($"!0& modreq(System.Runtime.InteropServices.InAttribute) {span}::get_Item(System.Int32)", - $".memory (.spanItem {width})"); - operations.Add("!!0& System.Runtime.InteropServices.MemoryMarshal::" + - $"GetReference<{element}>(System.ReadOnlySpan`1)", ".memory .spanReference"); - operations.Add("!!0& System.Runtime.InteropServices.MemoryMarshal::" + - $"GetReference<{element}>(System.Span`1)", ".memory .spanReference"); - foreach (var name in new[] { "System.Span`1", "System.ReadOnlySpan`1" }) - { - var owner = $"{name}<{element}>"; - var writable = name == "System.Span`1"; - var capability = writable ? "true" : "false"; - var arrayWhole = $".memory (.arraySpan {arrayElement} {capability} .whole)"; - if (arrayElement is not null) operations.Add($"System.Void {owner}::.ctor(!0[])", arrayWhole); - if (arrayElement is not null) operations.Add($"{name} {owner}::op_Implicit(!0[])", arrayWhole); - if (arrayElement is not null) operations.Add($"System.Void {owner}::.ctor(!0[],System.Int32,System.Int32)", - $".memory (.arraySpan {arrayElement} {capability} .counted)"); - operations.Add($"System.Void {owner}::.ctor(System.Void*,System.Int32)", - $".memory (.spanFromReference {width} {capability})"); - operations.Add($"System.Void {owner}::.ctor(!0&)", - $".memory (.spanSingleReference {width} {capability})"); - operations.Add($"{name} {owner}::get_Empty()", $".memory (.spanEmpty {width} {capability})"); - operations.Add($"System.Int32 {owner}::get_Length()", ".memory .spanLength"); - operations.Add($"System.Boolean {owner}::get_IsEmpty()", ".memory (.observeView .spanEmpty)"); - var pinnableResult = writable ? "!0&" : - "!0& modreq(System.Runtime.InteropServices.InAttribute)"; - operations.Add($"{pinnableResult} {owner}::GetPinnableReference()", - ".memory (.observeView .spanPinnable)"); - operations.Add($"System.Void {owner}::CopyTo(System.Span`1)", - $".memory (.spanCopy {width} false)"); - operations.Add($"System.Boolean {owner}::TryCopyTo(System.Span`1)", - $".memory (.spanCopy {width} true)"); - if (writable) - { - operations.Add($"!0& {owner}::get_Item(System.Int32)", $".memory (.spanItem {width})"); - operations.Add($"System.Void {owner}::Clear()", $".memory (.spanFill {width} true)"); - operations.Add($"System.Void {owner}::Fill(!0)", $".memory (.spanFill {width} false)"); - } - operations.Add($"{name} {owner}::Slice(System.Int32)", ".memory (.spanSlice false)"); - operations.Add($"{name} {owner}::Slice(System.Int32,System.Int32)", ".memory (.spanSlice true)"); - var factory = writable ? "CreateSpan" : "CreateReadOnlySpan"; - operations.Add($"{name} System.Runtime.InteropServices.MemoryMarshal::" + - $"{factory}<{element}>(!!0&,System.Int32)", - $".memory (.spanFromReference {width} {capability})"); + if (!signed && arity == 1) continue; + var result = name == "Sign" ? "System.Int32" : type; + var parameters = string.Join(",", Enumerable.Repeat(type, arity)); + calls.Add($"{result} System.Math::{name}({parameters})"); + calls.Add($"{result} {type}::{name}({parameters})"); } - operations.Add($"System.ReadOnlySpan`1 System.Span`1<{element}>::" + - "op_Implicit(System.Span`1)", ".memory .spanReadOnly"); - operations.Add("!!0& System.Runtime.InteropServices.MemoryMarshal::" + - $"GetArrayDataReference<{element}>(!!0[])", ".memory (.observeView .arrayData)"); - foreach (var (parameters, range) in new[] - { - ("!!0[]", "whole"), ("!!0[],System.Int32", "fromStart"), - ("!!0[],System.Int32,System.Int32", "counted") - }) - if (arrayElement is not null) operations.Add("System.Span`1 System.MemoryExtensions::" + - $"AsSpan<{element}>({parameters})", $".memory (.arraySpan {arrayElement} true .{range})"); } - return operations; + return calls; } - private static readonly Dictionary PortableVectorOperations = BuildPortableVectorOperations(); + private static readonly HashSet PortableVectorSignatures = BuildPortableVectorSignatures(); - // Lane-independent integer aliases bind exact signatures to the existing whole-vector operations. - private static Dictionary BuildPortableVectorOperations() + // Exact signatures of the lane-independent integer vector members admitted on every profile. + private static HashSet BuildPortableVectorSignatures() { - var operations = new Dictionary(StringComparer.Ordinal); + var signatures = new HashSet(StringComparer.Ordinal); foreach (var width in new[] { 128, 256, 512 }) { var genericVector = Vector(width, "!!0"); @@ -453,86 +310,84 @@ private static Dictionary BuildPortableVectorOperations() { "System.Byte", "System.Int64", "System.UInt64", "System.UInt32", "System.Int32" }) { var vectorOwner = Vector(width, element); - foreach (var (name, op) in new[] - { ("op_BitwiseAnd", "band"), ("op_BitwiseOr", "bor"), ("op_ExclusiveOr", "bxor") }) - operations.Add($"{memberVector} {vectorOwner}::{name}({memberVector},{memberVector})", - $".intrinsic (.vector (.{op} {width})) 2"); - operations.Add($"System.Boolean {vectorOwner}::op_Equality({memberVector},{memberVector})", - $".intrinsic (.vector (.equalsAll {width})) 2"); - operations.Add($"{memberVector} {vectorOwner}::op_OnesComplement({memberVector})", - $".intrinsic (.vector (.bnot {width})) 1"); - operations.Add($"System.Boolean {owner}::EqualsAll<{element}>({genericVector},{genericVector})", - $".intrinsic (.vector (.equalsAll {width})) 2"); - operations.Add($"{memberVector} {vectorOwner}::get_Zero()", - $".intrinsic (.vector (.zero {width})) 0"); - operations.Add($"{memberVector} {vectorOwner}::get_AllBitsSet()", - $".intrinsic (.vector (.ones {width})) 0"); + foreach (var name in new[] { "op_BitwiseAnd", "op_BitwiseOr", "op_ExclusiveOr" }) + signatures.Add($"{memberVector} {vectorOwner}::{name}({memberVector},{memberVector})"); + signatures.Add($"{memberVector} {vectorOwner}::op_OnesComplement({memberVector})"); + signatures.Add($"System.Boolean {owner}::EqualsAll<{element}>({genericVector},{genericVector})"); + signatures.Add($"{memberVector} {vectorOwner}::get_Zero()"); + signatures.Add($"{memberVector} {vectorOwner}::get_AllBitsSet()"); } } - return operations; + return signatures; } - // Vector128/256.Create(ReadOnlySpan) reads the leading bytes and throws - // ArgumentOutOfRangeException for a short span, exactly as MemoryMarshal.Read of the vector. - private static string? VectorFromByteSpan(string signature) - { - const string spanParameter = "System.ReadOnlySpan`1"; - foreach (var width in new[] { 128, 256 }) - if (signature == $"{Vector(width, "!!0")} {Intrinsics}.Vector{width}::Create({spanParameter})") - return $".memory (.marshalRead (.snapshot .vector{width}) false)"; - return null; - } + private static readonly HashSet VectorHelperSignatures = BuildVectorHelperSignatures(); - // Vector128.LoadUnsafe and StoreUnsafe are Unsafe.Add by the unsigned byte offset, when - // present, then the unaligned 16-byte transfer of Unsafe.ReadUnaligned or WriteUnaligned. - private static string? Vector128ByteTransfer(string signature) + // Every key fixes the open member declaration and the element used by the call. + private static HashSet BuildVectorHelperSignatures() { - const string owner = $"{Intrinsics}.Vector128"; - foreach (var offset in new[] { "false", "true" }) + var signatures = new HashSet(StringComparer.Ordinal); + foreach (int width in VectorWidths) { - var offsetParameter = offset == "true" ? ",System.UIntPtr" : ""; - if (signature == $"{Vector(128, "!!0")} {owner}::LoadUnsafe(!!0&{offsetParameter})") - return $".memory (.vector128Unsafe false {offset})"; - if (signature == - $"System.Void {owner}::StoreUnsafe({Vector(128, "!!0")},!!0&{offsetParameter})") - return $".memory (.vector128Unsafe true {offset})"; + var owner = $"{Intrinsics}.Vector{width}"; + var open = Vector(width, "!0"); + foreach (var element in new[] { "Byte", "Int32", "UInt32", "Int64", "UInt64" }) + { + var vector = Vector(width, $"System.{element}"); + signatures.Add($"System.Boolean {vector}::op_Equality({open},{open})"); + } + if (width == 512) + { + signatures.Add($"{V512UInt64} {owner}::Create({V256UInt64},{V256UInt64})"); + continue; + } + foreach (var (element, count) in new[] + { + ("System.UInt64", 1), ("System.UInt64", width / 64), ("System.UInt32", width / 32) + }) + { + var parameters = string.Join(",", Enumerable.Repeat(element, count)); + signatures.Add($"{Vector(width, element)} {owner}::Create({parameters})"); + } + var result = Vector(width, "!!0"); + signatures.Add($"{result} {owner}::Create(System.ReadOnlySpan`1)"); } - return null; + var source = Vector(128, "!!0"); + foreach (var suffix in new[] { "", ",System.UIntPtr" }) + { + signatures.Add($"{source} {Intrinsics}.Vector128::LoadUnsafe(!!0&{suffix})"); + signatures.Add($"System.Void {Intrinsics}.Vector128::StoreUnsafe({source},!!0&{suffix})"); + } + return signatures; } - private static string? Intrinsic(string signature, FeatureProfile profile) + private static Operation? HardwareModel(string signature, FeatureProfile profile) { - if (PortableVectorOperations.TryGetValue(signature, out var operation)) return operation; - string? Finish(string intrinsic, int argc, Feature? required = null) + if (PortableVectorSignatures.Contains(signature)) return Unoutlined.Site; + Operation Require(Feature feature) { - if (required is Feature feature && !profile.Get(feature)) + if (!profile.Get(feature)) throw new InvalidDataException($"Reachable intrinsic lacks {feature}: {signature}"); - return $".intrinsic ({intrinsic}) {argc}"; + return Unoutlined.Site; } if (signature == $"{V256UInt64} {Avx2}::And({V256UInt64},{V256UInt64})") - return Finish(".avx2 .band", 2, Feature.Avx2); + return Require(Feature.Avx2); if (signature == $"{V256UInt64} {Avx2}::Or({V256UInt64},{V256UInt64})") - return Finish(".avx2 .bor", 2, Feature.Avx2); + return Require(Feature.Avx2); if (signature == $"{V256UInt64} {Avx2}::Xor({V256UInt64},{V256UInt64})") - return Finish(".avx2 .bxor", 2, Feature.Avx2); + return Require(Feature.Avx2); if (signature == $"{V256UInt64} {Avx2}::AndNot({V256UInt64},{V256UInt64})") - return Finish(".avx2 .andNot", 2, Feature.Avx2); + return Require(Feature.Avx2); if (signature == $"{V256UInt64} {Avx2}::ShiftLeftLogical({V256UInt64},{V128UInt64})") - return Finish(".avx2 .shl64Count", 2, Feature.Avx2); + return Require(Feature.Avx2); if (signature == $"{V256UInt64} {Avx2}::ShiftRightLogical({V256UInt64},{V128UInt64})") - return Finish(".avx2 .shr64Count", 2, Feature.Avx2); + return Require(Feature.Avx2); if (signature == $"{V512UInt64} {X86}.Avx512F::CompareLessThan({V512UInt64},{V512UInt64})") - return Finish(".avx512 .ltu512", 2, Feature.Avx512F); + return Require(Feature.Avx512F); if (signature == $"{V512UInt64} {X86}.Avx512F::CompareGreaterThan({V512UInt64},{V512UInt64})") - return Finish(".avx512 .gtu512", 2, Feature.Avx512F); + return Require(Feature.Avx512F); if (signature == $"System.Int32 {Avx512DQ}::MoveMask({V512UInt64})") - return Finish(".avx512DQ .moveMask512", 1, Feature.Avx512DQ); - if (signature == $"{V512UInt64} {Intrinsics}.Vector512::Create({V256UInt64},{V256UInt64})") - return Finish(".vector .combine256", 2); - if (signature == $"{V128UInt64} {Intrinsics}.Vector128::CreateScalarUnsafe(System.UInt64)") - return Finish(".vector .createScalarUnsafe64", 1); - if (VectorFromByteSpan(signature) is { } load) return load; - if (Vector128ByteTransfer(signature) is { } transfer) return transfer; + return Require(Feature.Avx512DQ); foreach (int width in new[] { 128, 256 }) { string ulongVector = Vector(width, "System.UInt64"); @@ -540,16 +395,14 @@ private static Dictionary BuildPortableVectorOperations() string memberVector = Vector(width, "!0"); string owner = $"System.Runtime.Intrinsics.Vector{width}"; string genericOwner = Vector(width, "System.UInt64"); - foreach ((string name, string op) in new[] { ("op_Addition", "add64"), ("op_Subtraction", "sub64") }) + foreach (var name in new[] { "op_Addition", "op_Subtraction" }) if (signature == $"{memberVector} {genericOwner}::{name}({memberVector},{memberVector})") - return Finish($".vector (.{op} {width})", 2); - foreach ((string name, string op) in new[] { ("LessThan", "ltu64"), ("Equals", "eq64") }) + return Unoutlined.Site; + foreach (var name in new[] { "LessThan", "Equals" }) if (signature == $"{genericVector} {owner}::{name}({genericVector},{genericVector})") - return Finish($".vector (.{op} {width})", 2); + return Unoutlined.Site; if (signature == $"{genericVector} {owner}::LessThan({genericVector},{genericVector})") - return Finish($".vector (.lt64 {width})", 2); - if (signature == $"!!0 {owner}::GetElement({genericVector},System.Int32)") - return Finish($".vector (.extract64 {width})", 2); + return Unoutlined.Site; foreach ((string method, string target) in new[] { ("AsByte", "System.Byte"), ("AsUInt32", "System.UInt32"), ("AsInt64", "System.Int64"), ("AsDouble", "System.Double"), ("AsUInt64", "System.UInt64"), ("AsInt32", "System.Int32"), ("AsSingle", "System.Single") }) @@ -558,95 +411,86 @@ private static Dictionary BuildPortableVectorOperations() "System.UInt64", "System.Byte", "System.UInt32", "System.Int64", "System.Int32", "System.Single" }) if (signature == $"{Vector(width, target)} {owner}::{method}<{source}>({genericVector})") - return Finish($".vector (.reinterpret {width})", 1); + return Unoutlined.Site; string longVector = Vector(width, "System.Int64"); if (signature == $"{longVector} {owner}::ShiftRightArithmetic({longVector},System.Int32)") - return Finish($".vector (.ashr64 {width})", 2); - string elementwise = string.Join(",", Enumerable.Repeat("System.UInt64", width / 64)); - if (signature == $"{ulongVector} {owner}::Create({elementwise})") - return Finish($".vector (.create64 {width})", width / 64); - if (signature == $"{ulongVector} {owner}::Create(System.UInt64)") - return Finish($".vector (.create64 {width})", 1); - string uintLanes = string.Join(",", Enumerable.Repeat("System.UInt32", width / 32)); - if (signature == $"{Vector(width, "System.UInt32")} {owner}::Create({uintLanes})") - return Finish($".vector (.create32 {width})", width / 32); + return Unoutlined.Site; if (signature == $"{ulongVector} {owner}::CreateScalar(System.UInt64)") - return Finish($".vector (.createScalar64 {width})", 1); + return Unoutlined.Site; if (signature == $"{Vector(width, "System.UInt32")} {owner}::CreateScalar(System.UInt32)") - return Finish($".vector (.createScalar32 {width})", 1); + return Unoutlined.Site; if (signature == $"System.UInt32 {owner}::ExtractMostSignificantBits({genericVector})") - return Finish($".vector (.extractMSB64 {width})", 1); + return Unoutlined.Site; if (signature == $"!!0 {owner}::Sum({genericVector})") - return Finish($".vector (.sum64 {width})", 1); + return Unoutlined.Site; } return signature switch { $"{V128UInt64} {Intrinsics}.Arm.AdvSimd::ExtractVector128({V128UInt64},{V128UInt64},System.Byte)" => - Finish(".advSimd .extract64", 3, Feature.AdvSimd), + Require(Feature.AdvSimd), $"{V128UInt64} {Intrinsics}.Arm.AdvSimd::ReverseElement8({V128UInt64})" => - Finish(".advSimd .reverseElement8", 1, Feature.AdvSimd), + Require(Feature.AdvSimd), $"{V128UInt64} {X86}.Sse2::ShiftLeftLogical128BitLane({V128UInt64},System.Byte)" => - Finish(".sse .shiftLeftBytes", 2, Feature.Sse2), + Require(Feature.Sse2), $"{V128UInt64} {X86}.Sse2::Multiply({V128UInt32},{V128UInt32})" => - Finish(".sse .multiplyEven32", 2, Feature.Sse2), + Require(Feature.Sse2), $"{V128Byte} {X86}.Ssse3::AlignRight({V128Byte},{V128Byte},System.Byte)" => - Finish(".sse .alignBytes", 3, Feature.Ssse3), + Require(Feature.Ssse3), $"{V128Byte} {X86}.Ssse3::Shuffle({V128Byte},{V128Byte})" => - Finish(".sse .shuffleBytes", 2, Feature.Ssse3), + Require(Feature.Ssse3), $"{V256Byte} {Avx2}::Shuffle({V256Byte},{V256Byte})" => - Finish(".avx2 .shuffleBytes", 2, Feature.Avx2), - $"{V128Byte} {X86}.Aes::Encrypt({V128Byte},{V128Byte})" => Finish(".aes .encrypt", 2, Feature.Aes), + Require(Feature.Avx2), + $"{V128Byte} {X86}.Aes::Encrypt({V128Byte},{V128Byte})" => Require(Feature.Aes), $"{V128Byte} {Intrinsics}.Arm.Aes::Encrypt({V128Byte},{V128Byte})" => - Finish(".armAes .encrypt", 2, Feature.ArmAes), + Require(Feature.ArmAes), $"{V128Byte} {Intrinsics}.Arm.Aes::MixColumns({V128Byte})" => - Finish(".armAes .mixColumns", 1, Feature.ArmAes), + Require(Feature.ArmAes), $"{V256UInt64} {Avx2}::Permute4x64({V256UInt64},System.Byte)" => - Finish(".avx2 .permute4x64", 2, Feature.Avx2), + Require(Feature.Avx2), $"{V256UInt32} {Avx2}::Blend({V256UInt32},{V256UInt32},System.Byte)" => - Finish(".avx2 .blend32", 3, Feature.Avx2), + Require(Feature.Avx2), $"{V256Int32} {Avx2}::Blend({V256Int32},{V256Int32},System.Byte)" => - Finish(".avx2 .blend32", 3, Feature.Avx2), + Require(Feature.Avx2), $"{V256UInt64} {Avx2}::Add({V256UInt64},{V256UInt64})" => - Finish(".avx2 .add64", 2, Feature.Avx2), + Require(Feature.Avx2), $"{V256UInt64} {Avx2}::CompareEqual({V256UInt64},{V256UInt64})" => - Finish(".avx2 .eq64", 2, Feature.Avx2), + Require(Feature.Avx2), $"{V256UInt64} {Avx2}::ShiftLeftLogical({V256UInt64},System.Byte)" => - Finish(".avx2 .shl64", 2, Feature.Avx2), + Require(Feature.Avx2), $"{V256Int64} {Avx2}::CompareGreaterThan({V256Int64},{V256Int64})" => - Finish(".avx2 .signedgt64", 2, Feature.Avx2), + Require(Feature.Avx2), $"{V256UInt64} {Avx2}::Multiply({V256UInt32},{V256UInt32})" => - Finish(".avx2 .multiplyEven32", 2, Feature.Avx2), + Require(Feature.Avx2), $"{V256UInt64} {Avx2}::ShiftRightLogical({V256UInt64},System.Byte)" => - Finish(".avx2 .shr64", 2, Feature.Avx2), + Require(Feature.Avx2), $"{V256UInt64} {Avx512FVL}::AlignRight64({V256UInt64},{V256UInt64},System.Byte)" => - Finish(".avx512 .alignRight64", 3, Feature.Avx512FVL), + Require(Feature.Avx512FVL), $"{V256UInt64} {Avx512FVL}::TernaryLogic({V256UInt64},{V256UInt64},{V256UInt64},System.Byte)" => - Finish(".avx512 .ternaryLogic", 4, Feature.Avx512FVL), + Require(Feature.Avx512FVL), $"{V256UInt64} {Avx512FVL}::PermuteVar4x64x2({V256UInt64},{V256UInt64},{V256UInt64})" => - Finish(".avx512 .permuteVar4x64x2", 3, Feature.Avx512FVL), + Require(Feature.Avx512FVL), $"{V256UInt64} {Avx512FVL}::CompareLessThan({V256UInt64},{V256UInt64})" => - Finish(".avx512 .ltu64", 2, Feature.Avx512FVL), + Require(Feature.Avx512FVL), $"{V256UInt64} {Avx512FVL}::CompareGreaterThan({V256UInt64},{V256UInt64})" => - Finish(".avx512 .gtu64", 2, Feature.Avx512FVL), + Require(Feature.Avx512FVL), $"{V256UInt64} {Avx512FVL}::CompareGreaterThanOrEqual({V256UInt64},{V256UInt64})" => - Finish(".avx512 .geu64", 2, Feature.Avx512FVL), + Require(Feature.Avx512FVL), $"System.Int32 {Avx512DQ}::MoveMask({V256UInt64})" => - Finish(".avx512DQ .moveMask64", 1, Feature.Avx512DQ), + Require(Feature.Avx512DQ), $"{V256UInt64} {Avx512DQ}/VL::MultiplyLow({V256UInt64},{V256UInt64})" => - Finish(".avx512DQ .mul64", 2, Feature.Avx512DQVL), - $"System.Int32 {Avx}::MoveMask({V256Double})" => Finish(".avx .moveMask64", 1, Feature.Avx), - $"System.Int32 {Avx}::MoveMask({V256Single})" => Finish(".avx .moveMask32", 1, Feature.Avx), + Require(Feature.Avx512DQVL), + $"System.Int32 {Avx}::MoveMask({V256Double})" => Require(Feature.Avx), + $"System.Int32 {Avx}::MoveMask({V256Single})" => Require(Feature.Avx), $"{V256Single} {Avx}::Blend({V256Single},{V256Single},System.Byte)" => - Finish(".avx .blend32", 3, Feature.Avx), - $"System.Boolean {Avx}::TestZ({V256UInt64},{V256UInt64})" => Finish(".avx .testZ64", 2, Feature.Avx), + Require(Feature.Avx), + $"System.Boolean {Avx}::TestZ({V256UInt64},{V256UInt64})" => Require(Feature.Avx), $"System.UInt32 {X86}.Bmi1::BitFieldExtract(System.UInt32,System.Byte,System.Byte)" => - Finish(".bmi1 .bextr32", 3, Feature.Bmi1), + Require(Feature.Bmi1), $"System.UInt64 {X86}.Bmi2/X64::MultiplyNoFlags(System.UInt64,System.UInt64)" => - Finish(".bmi2 .multiplyHigh64", 2, Feature.Bmi2), + Require(Feature.Bmi2), $"System.UInt64 {Intrinsics}.Arm.ArmBase/Arm64::MultiplyHigh(System.UInt64,System.UInt64)" => - Finish(".armBase64 .multiplyHigh64", 2, Feature.ArmBase64), - $"{UInt64Pair} {X86}.X86Base/X64::DivRem(System.UInt64,System.UInt64,System.UInt64)" => - Finish(".x86Base .divRem64", 3, Feature.X86Base64), + Require(Feature.ArmBase64), + X86DivRemSignature => Require(Feature.X86Base64), _ => null }; } diff --git a/Extractor/ScalarEvidence.cs b/Extractor/ScalarEvidence.cs index aa959f00e..29bcd6cdc 100644 --- a/Extractor/ScalarEvidence.cs +++ b/Extractor/ScalarEvidence.cs @@ -1,11 +1,12 @@ using System.Text; using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; internal static class ScalarEvidence { - static string Kind(NativeOperands.StackType value) => value.Metadata ?? value.Kind switch + static string Kind(NativeOperands.StackType value) => value.Metadata?.ToLean() ?? value.Kind switch { NativeOperands.Kind.Word32 => ".word32", NativeOperands.Kind.Word64 => ".word64", @@ -13,18 +14,50 @@ internal static class ScalarEvidence NativeOperands.Kind.Reference => ".reference", NativeOperands.Kind.NullObject => ".nullObject", NativeOperands.Kind.Object => ".object", - _ => Arrays.StackKind(value.Kind) ?? ".other" + NativeOperands.Kind.Opaque => ".opaque", + _ => Arrays.StackKind(value.Kind)?.ToLean() ?? ".other" }; internal static void Emit(StringBuilder lean, MethodDefinition method, int number, - NativeOperands.Certificate certificate) + NativeOperands.Certificate certificate, string? objectProof) { IEnumerable states = method.Body.Instructions.Select(instruction => certificate.Incoming.TryGetValue(instruction, out NativeOperands.StackType[]? stack) ? $"some [{string.Join(", ", stack.Reverse().Select(Kind))}]" : "none"); lean.AppendLine($"def method{number}ScalarStates : Extraction.ScalarCertificate := [{string.Join(", ", states)}]"); - lean.AppendLine($"theorem method{number}_scalars_validated : Extraction.ScalarFlowValid rawLayouts.toArray " + - $"method{number}Closed method{number}FeatureStates method{number}ScalarStates " + - $"method{number}.code := {LeanEmitter.Decide}"); + var calls = method.Body.Instructions.Count(instruction => + instruction.OpCode.Code is Code.Call or Code.Callvirt or Code.Newobj or Code.Calli); + EmitInstructionChecks(lean, number, method.Body.Instructions.Count, calls, objectProof); + } + + // The checks share the closed operands; the code equality comes from the method's decoding. + static void EmitInstructionChecks(StringBuilder lean, int number, int instructions, int calls, + string? objectProof) + { + var method = $"method{number}"; + var flow = $"{method}Closed {method}FeatureStates {method}ScalarStates {method}.code"; + var scalar = $"Extraction.ScalarFlowValid {flow}"; + var context = $"{method}.profile {method}.environment {method}.links"; + var code = $"Extraction.methodCode {context} {method}Reachable {method}Closed"; + var binding = $"{method}.code = {code}"; + var witness = $"{method}_decoded_code"; + var scalarEvidence = $"And ({scalar}) ({binding})"; + var unsafeCalls = $"Extraction.UnsafeCallsValid {method}Closed {method}Reachable {method}.code"; + var objects = $"Extraction.ObjectOperandsValid {method}Closed {method}Reachable {method}"; + var remaining = $"And ({unsafeCalls}) ({objects})"; + var decided = objectProof is null ? remaining : unsafeCalls; + var checkedTail = objectProof is null ? "checks.2" : $"(And.intro checks.2 ({objectProof}))"; + var heartbeats = LeanEmitter.InstructionCheckHeartbeats(instructions, calls); + if (heartbeats > LeanEmitter.InstructionCheckHeartbeats(0, 0)) + lean.AppendLine($"set_option maxHeartbeats {heartbeats} in"); + lean.AppendLine($""" + theorem {method}_instruction_checks : And ({scalarEvidence}) ({remaining}) := by + have checks : And ({scalar}) ({decided}) := {LeanEmitter.Decide} + exact And.intro (And.intro checks.1 ({witness})) {checkedTail} + #print axioms {method}_instruction_checks + theorem {method}_scalars_validated : {scalarEvidence} := {method}_instruction_checks.1 + theorem {method}_unsafe_validated : {unsafeCalls} := {method}_instruction_checks.2.1 + theorem {method}_object_operands_validated : {objects} := {method}_instruction_checks.2.2 + """); } } diff --git a/Extractor/SpanReferences.cs b/Extractor/SpanReferences.cs index 809308ea2..ea13620bc 100644 --- a/Extractor/SpanReferences.cs +++ b/Extractor/SpanReferences.cs @@ -17,7 +17,7 @@ internal static class SpanReferences static bool ReadOnly(ParameterDefinition parameter) => parameter.IsIn && !parameter.IsOut && parameter.CustomAttributes.Count(attribute => attribute.AttributeType.FullName is IsReadOnly or RequiresLocation && - RuntimeModels.ExactScope(attribute.AttributeType.Scope, "System.Runtime")) == 1; + RuntimeModels.ExactScope(attribute.AttributeType, PlatformAssemblies.SystemRuntime)) == 1; internal static void ValidateParameter(MethodDefinition method, ParameterDefinition parameter, ModuleDefinition module, string architecture) @@ -43,9 +43,8 @@ internal static void ValidateCopy(MethodDefinition method, Instruction copy, Ins var type = (TypeReference)copy.Operand; var source = copy.Previous switch { - { OpCode.Code: Code.Ldarg or Code.Ldarg_S or Code.Ldarg_0 or Code.Ldarg_1 or Code.Ldarg_2 or - Code.Ldarg_3 } load => - MethodArguments.Type(method, ArgumentIndex(method, load)), + { } load when MethodArguments.LoadIndex(method, load) is int argument => + MethodArguments.Type(method, argument), { OpCode.Code: Code.Ldarga or Code.Ldarga_S, Operand: ParameterDefinition parameter } => new ByReferenceType(parameter.ParameterType), { OpCode.Code: Code.Ldloca or Code.Ldloca_S, Operand: VariableDefinition local } => @@ -53,15 +52,25 @@ internal static void ValidateCopy(MethodDefinition method, Instruction copy, Ins _ => null }; if (source is not ByReferenceType { ElementType: var element } || !Spans.Is(element) || - MethodSpecialization.SignatureType(element) != MethodSpecialization.SignatureType(type) || + StructuralSignatures.Current.Type(element) != StructuralSignatures.Current.Type(type) || instructions.IsTarget(copy)) throw new InvalidDataException("Unsupported span copy source: " + copy + "\n The span must be loaded directly from its own argument, parameter or local address."); } - static int ArgumentIndex(MethodDefinition method, Instruction load) => load.OpCode.Code switch + /// + /// Requires a span reset to follow the address of its own local or parameter directly, so its home names the + /// slot it empties. + /// + internal static void ValidateReset(Instruction reset, InstructionIndex instructions) { - Code.Ldarg_0 => 0, Code.Ldarg_1 => 1, Code.Ldarg_2 => 2, Code.Ldarg_3 => 3, - _ => MethodArguments.Index(method, (ParameterDefinition)load.Operand) - }; + var type = (TypeReference)reset.Operand; + if (Spans.AddressedType(reset.Previous) is not { } addressed || + StructuralSignatures.Current.Type(addressed) != StructuralSignatures.Current.Type(type) || + instructions.IsTarget(reset)) + throw new InvalidDataException($""" + Unsupported span reset: {reset} + The span must be reset directly through its own local or parameter address. + """); + } } diff --git a/Extractor/SpanReinterpretation.cs b/Extractor/SpanReinterpretation.cs deleted file mode 100644 index cd4b3c6a8..000000000 --- a/Extractor/SpanReinterpretation.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Mono.Cecil; - -namespace Nethermind.LeanCIL; - -// Reinterpretation retains the supplied storage; exact generic signatures determine both widths. -internal static class SpanReinterpretation -{ - internal static string? Translate(MethodReference reference, string architecture) - { - if (reference.DeclaringType.FullName != "System.Runtime.InteropServices.MemoryMarshal" || - !RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime") || - reference is not GenericInstanceMethod generic || reference.HasThis || reference.ExplicitThis || - reference.Parameters.Count != 1 || - (generic.ElementMethod.CallingConvention & ~MethodCallingConvention.Generic) != - MethodCallingConvention.Default) return null; - var asBytes = reference.Name == "AsBytes"; - var arity = asBytes ? 1 : 2; - if ((!asBytes && reference.Name != "Cast") || generic.GenericArguments.Count != arity || - generic.ElementMethod.GenericParameters.Count != arity) return null; - bool Parameter(TypeReference type, int position) => type is GenericParameter parameter && - parameter.Type == GenericParameterType.Method && parameter.Position == position && - parameter.Owner == generic.ElementMethod; - if (reference.Parameters[0].ParameterType is not GenericInstanceType input || - input.ElementType.FullName is not ("System.Span`1" or "System.ReadOnlySpan`1") || - input.GenericArguments.Count != 1 || !Parameter(input.GenericArguments[0], 0) || - reference.ReturnType is not GenericInstanceType output || - output.ElementType.FullName != input.ElementType.FullName || output.GenericArguments.Count != 1 || - !(asBytes ? output.GenericArguments[0].FullName == "System.Byte" : - Parameter(output.GenericArguments[0], 1))) return null; - RuntimeModels.ValidateSignature(reference, architecture); - var source = Spans.ElementWidth(generic.GenericArguments[0], architecture); - var target = asBytes ? 1 : Spans.ElementWidth(generic.GenericArguments[1], architecture); - if (source is null || target is null) return null; - var writable = input.ElementType.FullName == "System.Span`1"; - return $".memory (.spanReinterpret {source} {target} {writable.ToString().ToLowerInvariant()})"; - } -} - diff --git a/Extractor/Spans.cs b/Extractor/Spans.cs index 7463429ef..716cf31cb 100644 --- a/Extractor/Spans.cs +++ b/Extractor/Spans.cs @@ -3,13 +3,9 @@ namespace Nethermind.LeanCIL; -// Folds `stloc s; ldloca s` over a ReadOnlySpan local so the span stays on the stack -// as the receiver of the next span call. Reused locals retain their typed span storage. +// Span types, their element widths and the span members and constructors the runtime models cover. internal static class Spans { - internal sealed record Analysis(HashSet Operands, HashSet Interior, - Dictionary Constructors); - // Shape checks do not select an architecture or recompute aggregate layouts. internal static bool Is(TypeReference type) => type is GenericInstanceType span && span.ElementType.FullName is "System.Span`1" or "System.ReadOnlySpan`1" && @@ -40,105 +36,203 @@ private sealed class ElementLayouts internal static bool Writable(TypeReference type) => type is GenericInstanceType { ElementType.FullName: "System.Span`1" }; - private sealed class Architectures + /// The array overloads of AsSpan preserve the identity and checked layout of the element. + internal static Operation? ArrayExtension(MethodReference reference, string architecture) { - internal readonly Dictionary Results = new(StringComparer.Ordinal); + if (reference.Name != "AsSpan" || reference.DeclaringType is TypeSpecification || + reference.DeclaringType.FullName != "System.MemoryExtensions" || + reference.HasThis || reference.ExplicitThis || + !RuntimeModels.ExactScope(reference.DeclaringType, PlatformAssemblies.SystemMemory) || + reference is not GenericInstanceMethod { GenericArguments.Count: 1 } method || + method.ElementMethod.CallingConvention != MethodCallingConvention.Generic || + method.ElementMethod.GenericParameters.Count != 1 || method.Parameters.Count is < 1 or > 3) + return null; + + var variable = method.ElementMethod.GenericParameters[0]; + if (method.Parameters[0].ParameterType is not ArrayType { IsVector: true } array || + array.ElementType != variable || method.ReturnType is not GenericInstanceType result || + result.ElementType.FullName != "System.Span`1" || result.GenericArguments.Count != 1 || + result.GenericArguments[0] != variable || + !RuntimeModels.ExactScope(result.ElementType, PlatformAssemblies.SystemRuntime) || + method.Parameters.Skip(1).Any(parameter => parameter.ParameterType.MetadataType != MetadataType.Int32 || + !RuntimeModels.ExactScope(parameter.ParameterType, PlatformAssemblies.SystemRuntime))) + return null; + + if (Arrays.Storage(method.GenericArguments[0], architecture) is null) return null; + RuntimeModels.ValidateSignature(reference, architecture); + return Unoutlined.Site; } - static readonly System.Runtime.CompilerServices.ConditionalWeakTable cache = new(); + static bool OwnerElement(TypeReference type) => + type is GenericParameter { Type: GenericParameterType.Type, Position: 0 }; - internal static Analysis Analyze(MethodDefinition method, FeatureProfile profile) + /// A supported span constructor at its original call site. + internal static Operation? ConstructorOperation(MethodReference method, string architecture) { - var results = cache.GetValue(method, static _ => new()).Results; - if (!results.TryGetValue(profile.Architecture, out var analysis)) + if (method.Name != ".ctor" || !method.HasThis || method.ExplicitThis || method.HasGenericParameters || + method is GenericInstanceMethod || method.CallingConvention != MethodCallingConvention.Default || + method.ReturnType.MetadataType != MetadataType.Void || + method.DeclaringType is not GenericInstanceType { GenericArguments.Count: 1 } owner || + owner.ElementType.FullName is not ("System.Span`1" or "System.ReadOnlySpan`1") || + !RuntimeModels.ExactScope(owner.ElementType, PlatformAssemblies.SystemRuntime)) return null; + var element = owner.GenericArguments[0]; + if (ElementWidth(element, architecture) is null) return null; + var parameters = method.Parameters; + Operation? operation = parameters switch { - analysis = Compute(method, profile); - results.Add(profile.Architecture, analysis); - } - return analysis; + [{ ParameterType: ByReferenceType byref }] when OwnerElement(byref.ElementType) => + Unoutlined.Site, + [{ ParameterType: PointerType { ElementType.MetadataType: MetadataType.Void } }, + { ParameterType.MetadataType: MetadataType.Int32 }] => + Unoutlined.Site, + _ when parameters.Count is 1 or 3 && parameters[0].ParameterType is ArrayType { IsVector: true } array && + OwnerElement(array.ElementType) && + parameters.Skip(1).All(parameter => parameter.ParameterType.MetadataType == MetadataType.Int32) && + Arrays.Storage(element, architecture) is not null => Unoutlined.Site, + _ => null + }; + if (operation is not null) RuntimeModels.ValidateSignature(method, architecture); + return operation; } - static bool Store(Instruction i) => i.OpCode.Code is Code.Stloc or Code.Stloc_S or Code.Stloc_0 or Code.Stloc_1 or Code.Stloc_2 or Code.Stloc_3; - static bool Address(Instruction i) => i.OpCode.Code is Code.Ldloca or Code.Ldloca_S; - static bool Load(Instruction i) => i.OpCode.Code is Code.Ldloc or Code.Ldloc_S or Code.Ldloc_0 or Code.Ldloc_1 or Code.Ldloc_2 or Code.Ldloc_3; - static bool Push(Instruction? i) => i is not null && (Load(i) || i.OpCode.Code is Code.Ldarg or Code.Ldarg_S or - Code.Ldarg_0 or Code.Ldarg_1 or Code.Ldarg_2 or Code.Ldarg_3 or Code.Ldc_I4 or Code.Ldc_I4_S or Code.Ldc_I4_M1 || - i.OpCode.Code is >= Code.Ldc_I4_0 and <= Code.Ldc_I4_8); - static bool Member(Instruction? i, string name) => i is { OpCode.Code: Code.Call, Operand: MethodReference callee } && - callee.HasThis && callee.Name == name && Is(callee.DeclaringType); + static bool DeclaredSpan(TypeReference type, string owner) => + type is GenericInstanceType { GenericArguments.Count: 1 } span && + span.ElementType.FullName == owner && OwnerElement(span.GenericArguments[0]); - static Analysis Compute(MethodDefinition method, FeatureProfile profile) + static bool ElementReference(TypeReference type, bool writable) { - var analysis = new Analysis([], [], []); - var uses = new int[method.Body.Variables.Count]; - foreach (var instruction in method.Body.Instructions) - if (Store(instruction) || Address(instruction) || Load(instruction)) - uses[FeatureConstants.Local(instruction)]++; - foreach (var i in method.Body.Instructions) + if (!writable) { - if (!(Store(i) || Address(i) || Load(i)) || - !Is(method.Body.Variables[FeatureConstants.Local(i)].VariableType) || - analysis.Operands.Contains(i)) - continue; - var local = FeatureConstants.Local(i); - if (Address(i) && Constructor(i, method.Body.Variables[local].VariableType, profile) is { } constructor) - { - analysis.Operands.Add(i); - for (var interior = i.Next; interior is not null; interior = interior.Next) - { - analysis.Interior.Add(interior); - if (interior != constructor.Call) continue; - analysis.Constructors.Add(interior, $".initializeSpanLocal {local} ({constructor.Descriptor})"); - break; - } - continue; - } - if (uses[local] != 2) - continue; - // The receiver is read once: by get_Length, or by get_Item after one index push. - if (!Store(i) || i.Next is not { } address || !Address(address) || FeatureConstants.Local(address) != FeatureConstants.Local(i) || - !(Member(address.Next, "get_Length") || (Push(address.Next) && Member(address.Next!.Next, "get_Item")))) - continue; - analysis.Operands.Add(i); - analysis.Operands.Add(address); - analysis.Interior.Add(address); + if (type is not RequiredModifierType + { ModifierType.FullName: "System.Runtime.InteropServices.InAttribute" } modifier) return false; + type = modifier.ElementType; } - return analysis; + return type is ByReferenceType reference && OwnerElement(reference.ElementType); + } + + /// The element and access mode of a span created from a vector array at this site. + internal static (Arrays.Element Element, bool Writable)? ArrayView(Instruction site, string architecture, bool wholeOnly = false) + { + if (site.Operand is not MethodReference method || + method.Parameters.Count == 0 || wholeOnly && method.Parameters.Count != 1 || + method.Parameters[0].ParameterType is not ArrayType { IsVector: true }) return null; + if (site.OpCode.Code == Code.Call && method is GenericInstanceMethod extension && + ArrayExtension(extension, architecture) is not null) + return (Arrays.Storage(extension.GenericArguments[0], architecture)!, true); + if (method.DeclaringType is not GenericInstanceType owner) return null; + if (site.OpCode.Code == Code.Newobj && ConstructorOperation(method, architecture) is not null || + site.OpCode.Code == Code.Call && method.Name == "op_Implicit" && Member(method, architecture) is not null) + return (Arrays.Storage(owner.GenericArguments[0], architecture)!, Writable(owner)); + return null; + } + + /// The element width of the specified overload at an original, modelled span call. + internal static int? MemberWidth(Instruction site, string name, int parameterCount, string architecture) + { + if (site.OpCode.Code != Code.Call || site.Operand is not MethodReference { DeclaringType: GenericInstanceType owner } method || + method.Name != name || method.Parameters.Count != parameterCount || + Member(method, architecture) is null) return null; + return ElementWidth(owner.GenericArguments[0], architecture); } - // The local address cannot escape: only bounded single-value pushes precede its exact constructor. - static (Instruction Call, string Descriptor)? Constructor(Instruction address, TypeReference localType, - FeatureProfile profile) + /// A retained span member uses the element of its closed receiver. + internal static Operation? Member(MethodReference method, string architecture) { - if (address.Next is { OpCode.Code: Code.Initobj, Operand: TypeReference initialized } reset) + if (method.DeclaringType is not GenericInstanceType { GenericArguments.Count: 1 } owner || + owner.ElementType.FullName is not ("System.Span`1" or "System.ReadOnlySpan`1") || + method.HasGenericParameters || method is GenericInstanceMethod || method.ExplicitThis || + method.CallingConvention != MethodCallingConvention.Default || + method.HasThis == (method.Name is "get_Empty" or "op_Implicit") || + !RuntimeModels.ExactScope(owner.ElementType, PlatformAssemblies.SystemRuntime)) return null; + + var name = owner.ElementType.FullName; + var writable = Writable(owner); + var parameters = method.Parameters; + var result = method.ReturnType; + var element = owner.GenericArguments[0]; + if (method.Name is not ("get_Item" or "get_Length" or "Slice" or "get_IsEmpty" or + "GetPinnableReference" or "op_Implicit" or "get_Empty" or "Clear" or "Fill" or "CopyTo" or "TryCopyTo")) + return null; + if (ElementWidth(element, architecture) is null) return null; + Operation? operation = method.Name switch { - if (MethodSpecialization.SignatureType(initialized) != MethodSpecialization.SignatureType(localType)) - throw new InvalidDataException("Span default type does not match its local"); - return Width(localType, profile.Architecture) is int width - ? (reset, $".empty {width} {Writable(localType).ToString().ToLowerInvariant()}") : null; - } - var call = address.Next; - var count = 0; - while (count < 3 && (Push(call) || call?.OpCode.Code == Code.Ldnull)) + "Clear" when writable && parameters.Count == 0 && result.MetadataType == MetadataType.Void => + Unoutlined.Site, + "Fill" when writable && parameters.Count == 1 && OwnerElement(parameters[0].ParameterType) && + result.MetadataType == MetadataType.Void => Unoutlined.Site, + "CopyTo" or "TryCopyTo" when parameters.Count == 1 && + DeclaredSpan(parameters[0].ParameterType, "System.Span`1") && + result.MetadataType == (method.Name == "CopyTo" ? MetadataType.Void : MetadataType.Boolean) => + Unoutlined.Site, + "get_Item" when parameters is [{ ParameterType.MetadataType: MetadataType.Int32 }] && + ElementReference(result, writable) => Unoutlined.Site, + "get_Length" when parameters.Count == 0 && result.MetadataType == MetadataType.Int32 => Unoutlined.Site, + "Slice" when parameters.Count is 1 or 2 && DeclaredSpan(result, name) && + parameters.All(parameter => parameter.ParameterType.MetadataType == MetadataType.Int32) => + Unoutlined.Site, + "get_IsEmpty" when parameters.Count == 0 && result.MetadataType == MetadataType.Boolean => + Unoutlined.Site, + "GetPinnableReference" when parameters.Count == 0 && ElementReference(result, writable) => + Unoutlined.Site, + "get_Empty" when parameters.Count == 0 && DeclaredSpan(result, name) => Unoutlined.Site, + "op_Implicit" when parameters is [{ ParameterType: ArrayType { IsVector: true } array }] && + OwnerElement(array.ElementType) && DeclaredSpan(result, name) && + Arrays.Storage(element, architecture) is not null => Unoutlined.Site, + _ => null + }; + if (operation is null && !(method.Name == "op_Implicit" && writable && parameters.Count == 1 && + DeclaredSpan(parameters[0].ParameterType, name) && DeclaredSpan(result, "System.ReadOnlySpan`1"))) + return null; + RuntimeModels.ValidateSignature(method, architecture); + return operation ?? Unoutlined.Site; + } + + static readonly System.Runtime.CompilerServices.ConditionalWeakTable> + windows = new(); + + /// + /// The span constructor calls whose receiver is a span home: the address of a local or parameter of the + /// constructed type, then one simple push per parameter, at most three, and no branch target after the address. + /// + internal static HashSet ConstructorCalls(MethodDefinition method) => + windows.GetValue(method, static method => Windows(method)); + + static bool Push(Instruction i) => i.OpCode.Code is Code.Ldloc or Code.Ldloc_S or Code.Ldloc_0 or Code.Ldloc_1 or + Code.Ldloc_2 or Code.Ldloc_3 or Code.Ldarg or Code.Ldarg_S or Code.Ldarg_0 or Code.Ldarg_1 or Code.Ldarg_2 or + Code.Ldarg_3 or Code.Ldc_I4 or Code.Ldc_I4_S or Code.Ldc_I4_M1 or Code.Ldnull || + i.OpCode.Code is >= Code.Ldc_I4_0 and <= Code.Ldc_I4_8; + + /// The declared type of the local or parameter whose home pushes. + internal static TypeReference? AddressedType(Instruction? address) => address switch + { + { OpCode.Code: Code.Ldloca or Code.Ldloca_S, Operand: VariableDefinition local } => local.VariableType, + { OpCode.Code: Code.Ldarga or Code.Ldarga_S, Operand: ParameterDefinition parameter } => + parameter.ParameterType, + _ => null + }; + + static HashSet Windows(MethodDefinition method) + { + var instructions = new InstructionIndex(method); + HashSet calls = []; + foreach (var call in method.Body.Instructions) { - count++; - call = call!.Next; + if (call is not { OpCode.Code: Code.Call, Operand: MethodReference { Name: ".ctor" } reference } || + !Is(reference.DeclaringType) || reference.Parameters.Count > 3 || instructions.IsTarget(call)) + continue; + var address = call.Previous; + var pushes = 0; + while (pushes < reference.Parameters.Count && address is not null && Push(address) && + !instructions.IsTarget(address)) + { + pushes++; + address = address.Previous; + } + var signatures = StructuralSignatures.Current; + if (pushes == reference.Parameters.Count && AddressedType(address) is { } addressed && + signatures.Type(addressed) == signatures.Type(reference.DeclaringType)) + calls.Add(call); } - if (call is not { OpCode.Code: Code.Call, Operand: MethodReference reference } || - reference.Name != ".ctor" || reference.DeclaringType.FullName != localType.FullName || - reference.Parameters.Count != count) - return null; - var operation = InstructionTranslation.RuntimeModel(reference, profile); - if (operation == ".memory .spanCreate") return (call, ".reference 1 false"); - const string array = ".memory (.arraySpan "; - const string pointer = ".memory (.spanFromReference "; - const string single = ".memory (.spanSingleReference "; - if (operation?.StartsWith(array, StringComparison.Ordinal) == true) - return (call, ".array " + operation[array.Length..^1]); - if (operation?.StartsWith(pointer, StringComparison.Ordinal) == true) - return (call, ".reference " + operation[pointer.Length..^1]); - if (operation?.StartsWith(single, StringComparison.Ordinal) == true) - return (call, ".singleReference " + operation[single.Length..^1]); - return null; + return calls; } } diff --git a/Extractor/StaticData.cs b/Extractor/StaticData.cs index cf361f55c..fc2327783 100644 --- a/Extractor/StaticData.cs +++ b/Extractor/StaticData.cs @@ -16,15 +16,41 @@ internal static class StaticData /// Reads a module from its image and records the image's section headers, which /// requires to admit any static data of that module. /// - internal static ModuleDefinition ReadModule(byte[] assembly, IAssemblyResolver? resolver = null) + internal static ModuleDefinition ReadModule(byte[] assembly, IAssemblyResolver? resolver = null, + Action? retainStream = null) { SectionHeader[] headers; using (PEReader image = new(new MemoryStream(assembly, writable: false))) headers = [.. image.PEHeaders.SectionHeaders]; - ModuleDefinition module = ModuleDefinition.ReadModule(new MemoryStream(assembly, writable: false), - resolver is null ? new ReaderParameters() : new ReaderParameters { AssemblyResolver = resolver }); - sections.AddOrUpdate(module, headers); - return module; + var stream = new MemoryStream(assembly, writable: false); + ModuleDefinition? module = null; + try + { + module = ModuleDefinition.ReadModule(stream, new ReaderParameters + { + AssemblyResolver = resolver, + ReadingMode = ReadingMode.Deferred, + ReadSymbols = false + }); + sections.AddOrUpdate(module, headers); + retainStream?.Invoke(stream); + return module; + } + catch + { + module?.Dispose(); + stream.Dispose(); + throw; + } + } + + /// Each static site binds a validated readonly field or an immutable RVA block. + internal static FieldDefinition FieldOf(Mono.Cecil.Cil.Instruction instruction, ModuleDefinition module, + string architecture) + { + var field = (FieldReference)instruction.Operand; + return StaticValues.IsLoad(instruction) ? StaticValues.Resolve(field, module, architecture).Field : + Validate(field, module); } internal static FieldDefinition Validate(FieldReference reference, ModuleDefinition module) @@ -96,8 +122,6 @@ static void RequireSectionAligned(FieldReference reference, FieldDefinition fiel /// The guaranteed alignment of validated static data: its declared packing. internal static int Alignment(FieldDefinition field) => field.FieldType.Resolve().PackingSize; - internal static string LeanBytes(FieldDefinition field) => LeanBytes(field.InitialValue); - - internal static string LeanBytes(byte[] bytes) => + internal static string LeanBytes(IEnumerable bytes) => "[" + string.Join(", ", bytes.Select(b => $"(BitVec.ofNat 8 {b})")) + "]"; } diff --git a/Extractor/StaticValues.cs b/Extractor/StaticValues.cs index 5343a0086..5467fe2ec 100644 --- a/Extractor/StaticValues.cs +++ b/Extractor/StaticValues.cs @@ -21,9 +21,10 @@ internal static bool IsLoad(Instruction instruction) => instruction.OpCode.Code instruction.OpCode.Code == Code.Ldsflda && instruction.Operand is FieldReference reference && reference.Resolve() is { } field && (field.Attributes & FieldAttributes.HasFieldRVA) == 0; - internal static string Scalar(FieldDefinition field) => MetadataValidation.IsAggregate(field.FieldType, field.Module) - ? "none" : "some " + (EnumTypes.Storage(field.FieldType).FullName is "System.SByte" or "System.Int16" - ? "true" : "false"); + /// Whether a scalar field's loads sign-extend, or null for an aggregate field. + internal static bool? ScalarSign(FieldDefinition field) => + MetadataValidation.IsAggregate(field.FieldType, field.Module) ? null : + EnumTypes.Storage(field.FieldType).FullName is "System.SByte" or "System.Int16"; static int Width(FieldDefinition field, string architecture) => MetadataValidation.IsAggregate(field.FieldType, field.Module) diff --git a/Extractor/StructuralSignatures.cs b/Extractor/StructuralSignatures.cs new file mode 100644 index 000000000..dbfbc90de --- /dev/null +++ b/Extractor/StructuralSignatures.cs @@ -0,0 +1,359 @@ +using Mono.Cecil; + +namespace Nethermind.LeanCIL; + +/// Exact signature nodes owned by one live pinned metadata session. +internal sealed class StructuralSignatures(PinnedAssemblies owner) +{ + /// A canonical node whose identity is meaningful only in its owning live session. + internal readonly struct Signature(StructuralSignatures session, int id) : IEquatable, + IComparable + { + internal int Id { get; } = id; + internal StructuralSignatures Session { get; } = session; + public bool Equals(Signature other) + { + Session.RequireLive(); + if (!ReferenceEquals(Session, other.Session)) + throw new InvalidDataException("Signature belongs to another pinned session"); + return Id == other.Id; + } + public override bool Equals(object? other) => other is Signature signature && Equals(signature); + public override int GetHashCode() { Session.RequireLive(); return Id; } + public int CompareTo(Signature other) + { + _ = Equals(other); + return Id.CompareTo(other.Id); + } + public static bool operator ==(Signature left, Signature right) => left.Equals(right); + public static bool operator !=(Signature left, Signature right) => !left.Equals(right); + } + + sealed class Trie + { + internal readonly SortedDictionary Children = []; + internal int Id; + } + + /// An immutable TypeSystem object's admitted module, row or implicit named scope. + internal sealed record Primitive(TypeReference Type, ModuleDefinition Module, MetadataImage Image, + uint Token, uint Assembly, string Name); + + readonly Trie root = new(); + readonly SortedDictionary<(byte Value, int Suffix), int> nameNodes = []; + readonly Dictionary> heapNames = []; + readonly Dictionary> namedTypes = []; + readonly Dictionary> assemblies = []; + readonly Dictionary primitives = new(ReferenceEqualityComparer.Instance); + readonly Dictionary methods = new(ReferenceEqualityComparer.Instance); + int nextId; + int nextName; + bool disposed; + internal long HeapBytes { get; private set; } + internal long RawRows { get; private set; } + internal long NodeVisits { get; private set; } + internal long EdgeVisits { get; private set; } + internal long TrieProbes { get; private set; } + + internal static StructuralSignatures Current => (PinnedAssemblies.Active ?? + throw new InvalidDataException("Signature requires a pinned metadata session")).Signatures; + + void RequireLive() + { + if (disposed || !ReferenceEquals(PinnedAssemblies.Active, owner)) + throw new InvalidDataException("Signature session is not active"); + } + + int Intern(IEnumerable tokens) + { + Trie node = root; + foreach (long token in tokens) + { + TrieProbes++; + if (!node.Children.TryGetValue(token, out var child)) node.Children.Add(token, child = new()); + node = child; + } + if (node.Id == 0) node.Id = checked(++nextId); + return node.Id; + } + + int Cons(byte value, int suffix) + { + if (nameNodes.TryGetValue((value, suffix), out int id)) return id; + nameNodes.Add((value, suffix), id = checked(++nextName)); + return id; + } + + int Name(ReadOnlySpan bytes) + { + int id = 0; + for (int index = bytes.Length - 1; index >= 0; index--) id = Cons(bytes[index], id); + return id; + } + + int Name(MetadataImage image, int offset) + { + if (!heapNames.TryGetValue(image, out var cache)) heapNames.Add(image, cache = []); + int cursor = offset; + var pending = new List(); + while (!cache.TryGetValue(cursor, out _)) + { + byte value = image.StringByte(cursor); + HeapBytes++; + if (value == 0) { cache.Add(cursor, 0); break; } + pending.Add(value); + cursor = checked(cursor + 1); + } + int id = cache[cursor]; + for (int index = pending.Count - 1; index >= 0; index--) + cache.Add(--cursor, id = Cons(pending[index], id)); + return id; + } + + int Assembly(MetadataImage image, uint token) + { + if (!assemblies.TryGetValue(image, out var cache)) assemblies.Add(image, cache = []); + if (cache.TryGetValue(token, out int id)) return id; + var identity = image.AssemblyColumns(token) ?? throw new InvalidDataException("Invalid assembly identity"); + var version = identity.Version; + ulong key = identity.PublicKeyToken ?? 0; + id = Intern([1, Name(image, identity.Name), Name(image, identity.Culture), version.Major, version.Minor, + version.Build, version.Revision, identity.PublicKeyToken.HasValue ? 1 : 0, (uint)(key >> 32), (uint)key]); + cache.Add(token, id); + return id; + } + + int Named(MetadataImage image, uint token) + { + if (!namedTypes.TryGetValue(image, out var cache)) namedTypes.Add(image, cache = []); + var pending = new List<(uint Token, MetadataImage.NamedRow Row)>(); + var visiting = new SortedSet(); + int parent = 0; + while (!cache.TryGetValue(token, out parent)) + { + if (!visiting.Add(token)) throw new InvalidDataException("Cyclic named type identity"); + var row = image.NamedType(token); + RawRows++; + pending.Add((token, row)); + if (row.Parent == 0) break; + token = row.Parent; + } + for (int index = pending.Count - 1; index >= 0; index--) + { + var entry = pending[index]; + parent = Intern([2, entry.Row.Parent == 0 ? Assembly(image, entry.Row.Assembly) : 0, + parent, Name(image, entry.Row.Namespace), Name(image, entry.Row.Name)]); + cache.Add(entry.Token, parent); + } + return parent; + } + + int Leaf(TypeReference type) + { + if (primitives.TryGetValue(type, out var primitive)) + { + owner.RequireRegistered(primitive.Module); + return primitive.Token != 0 ? Named(primitive.Image, primitive.Token) : + Intern([2, Assembly(primitive.Image, primitive.Assembly), 0, + Name("System"u8), Name(IdentityText.Utf8(primitive.Name))]); + } + owner.RequireRegistered(type.Module); + var image = owner.IdentityMetadata(type.Module); + IdentityRows.RequireModule(image, type.Module); + return Named(image, type.MetadataToken.ToUInt32()); + } + + sealed record Snapshot(long[] Prefix, TypeReference[] Children); + + Snapshot Describe(TypeReference type) + { + switch (type) + { + case GenericParameter parameter: + var module = parameter.Owner switch + { + MethodReference { DeclaringType: not null } method => method.Module, + TypeReference declaring => declaring.Module, + _ => null + }; + if (module is null) throw new InvalidDataException("Generic parameter owner has no module"); + owner.RequireRegistered(module); + if (parameter.Position < 0 || parameter.Position >= parameter.Owner.GenericParameters.Count || + parameter.Type is not (GenericParameterType.Method or + GenericParameterType.Type)) throw new InvalidDataException("Malformed generic parameter"); + return new([3, (int)parameter.Type, parameter.Position], []); + case GenericInstanceType instance: + if (instance.ElementType is TypeSpecification or GenericParameter) + throw new InvalidDataException("Generic application requires a named constructor"); + return new([4, instance.GenericArguments.Count], [instance.ElementType, .. instance.GenericArguments]); + case RequiredModifierType modifier: return new([5], [modifier.ModifierType, modifier.ElementType]); + case OptionalModifierType modifier: return new([6], [modifier.ModifierType, modifier.ElementType]); + case ByReferenceType reference: return new([7], [reference.ElementType]); + case PointerType pointer: return new([8], [pointer.ElementType]); + case PinnedType pinned: return new([9], [pinned.ElementType]); + case SentinelType sentinel: return new([10], [sentinel.ElementType]); + case ArrayType { IsVector: true } array: return new([11], [array.ElementType]); + case ArrayType array: + var bounds = new List { 12, array.Rank }; + foreach (var dimension in array.Dimensions) + { + bounds.Add(dimension.LowerBound.HasValue ? 1 : 0); + bounds.Add(dimension.LowerBound ?? 0); + bounds.Add(dimension.UpperBound.HasValue ? 1 : 0); + bounds.Add(dimension.UpperBound ?? 0); + } + return new([.. bounds], [array.ElementType]); + case FunctionPointerType pointer: + return new([13, (int)pointer.CallingConvention, pointer.HasThis ? 1 : 0, + pointer.ExplicitThis ? 1 : 0, pointer.Parameters.Count], + [pointer.ReturnType, .. pointer.Parameters.Select(parameter => parameter.ParameterType)]); + case TypeSpecification: + throw new InvalidDataException("Unsupported structural type specification"); + default: return new([], []); + } + } + + /// A fresh iterative snapshot observes every mutable wrapper once and rejects cycles. + internal Signature Type(TypeReference type) + => new(this, Types([type])[0]); + + /// An ordered signature list shares one snapshot across all of its roots. + internal Signature Ordered(IEnumerable roots) + { + int[] types = Types([.. roots]); + return new(this, Intern(new long[] { 16, types.Length }.Concat(types.Select(id => (long)id)))); + } + + /// All roots in one read-only layout collection share their completed descendants. + internal Func LayoutSnapshot() + { + Dictionary complete = new(ReferenceEqualityComparer.Instance); + return type => new(this, Types([type], complete)[0]); + } + + int[] Types(TypeReference[] roots, Dictionary? complete = null) + { + RequireLive(); + complete ??= new(ReferenceEqualityComparer.Instance); + var visiting = new HashSet(ReferenceEqualityComparer.Instance); + var work = new Stack<(TypeReference Type, Snapshot? Snapshot)>(); + for (int index = roots.Length - 1; index >= 0; index--) work.Push((roots[index], null)); + while (work.TryPop(out var entry)) + { + if (complete.ContainsKey(entry.Type)) continue; + if (entry.Snapshot is { } snapshot) + { + int id = snapshot.Prefix.Length == 0 ? Leaf(entry.Type) : + Intern(snapshot.Prefix.Concat(snapshot.Children.Select(child => (long)complete[child]))); + complete.Add(entry.Type, id); + visiting.Remove(entry.Type); + continue; + } + if (!visiting.Add(entry.Type)) throw new InvalidDataException("Cyclic structural signature"); + NodeVisits++; + var description = Describe(entry.Type); + EdgeVisits += description.Children.Length; + work.Push((entry.Type, description)); + for (int index = description.Children.Length - 1; index >= 0; index--) + work.Push((description.Children[index], null)); + } + return roots.Select(type => complete[type]).ToArray(); + } + + /// The original method, closed owner and ordered method arguments form one exact key. + internal Signature Specialization(MethodDefinition method, TypeReference declaring, + IEnumerable arguments) + { + RequireLive(); + method = MethodSpecialization.Definition(method); + owner.RequireRegistered(method.Module); + if (!methods.TryGetValue(method, out int definition)) methods.Add(method, definition = methods.Count + 1); + int[] types = Types([declaring, .. arguments]); + return new(this, Intern(new long[] { 14, definition, types[0], types.Length - 1 } + .Concat(types.Skip(1).Select(id => (long)id)))); + } + + /// A method signature erases only the separately validated in and init tags. + internal Signature ErasedMethod(MethodDefinition method) + { + RequireLive(); + int[] types = Types([InitAccessors.Return(method.ReturnType), + .. method.Parameters.Select(parameter => InReferences.Parameter(parameter.ParameterType))]); + return new(this, Intern(new long[] { 15, method.HasThis ? 1 : 0, method.ExplicitThis ? 1 : 0, + (int)method.CallingConvention, method.GenericParameters.Count, types[0], types.Length - 1 } + .Concat(types.Skip(1).Select(id => (long)id)))); + } + + /// Whether Cecil read an implicit element type through its module's TypeSystem. + internal bool IsPrimitiveElement(TypeReference type) => primitives.ContainsKey(type); + + internal Primitive[] Prepare(ModuleDefinition module, MetadataImage image, AssemblyNameReference[] scopes) + { + var system = module.TypeSystem; + // Core-library types have definition rows and need no implicit primitive registration. + if (ReferenceEquals(system.CoreLibrary, module)) return []; + (TypeReference Type, string Name)[] entries = [(system.Void, "Void"), (system.Boolean, "Boolean"), + (system.Char, "Char"), (system.SByte, "SByte"), (system.Byte, "Byte"), (system.Int16, "Int16"), + (system.UInt16, "UInt16"), (system.Int32, "Int32"), (system.UInt32, "UInt32"), + (system.Int64, "Int64"), (system.UInt64, "UInt64"), (system.Single, "Single"), + (system.Double, "Double"), (system.IntPtr, "IntPtr"), (system.UIntPtr, "UIntPtr"), + (system.String, "String"), (system.Object, "Object"), (system.TypedReference, "TypedReference")]; + var result = new List(); + var scopeTokens = new Dictionary(ReferenceEqualityComparer.Instance); + foreach (var scope in scopes) scopeTokens.Add(scope, scope.MetadataToken.ToUInt32()); + if (system.CoreLibrary is AssemblyNameReference { MetadataToken.RID: 0 } synthetic && + !scopeTokens.ContainsKey(synthetic)) + module.AssemblyReferences.Remove(synthetic); + foreach (var (type, name) in entries) + { + if (primitives.ContainsKey(type) || !ReferenceEquals(type.Module, module)) + throw new InvalidDataException("TypeSystem object lacks unique module provenance"); + uint token = type.MetadataToken.ToUInt32(); + uint assembly = 0; + if (type.MetadataToken.RID == 0) + { + if (type.Namespace != "System" || type.Name != name || type.DeclaringType is not null) + throw new InvalidDataException("Invalid implicit TypeSystem identity"); + if (ReferenceEquals(type.Scope, module)) assembly = 0x20000001; + else if (type.Scope is AssemblyNameReference reference && + scopeTokens.TryGetValue(reference, out var row)) + assembly = row; + // Cecil synthesizes a core scope for images with no core-library reference. + else if (ReferenceEquals(type.Scope, system.CoreLibrary) && + type.Scope is AssemblyNameReference { MetadataToken.RID: 0 }) + continue; + else throw new InvalidDataException("TypeSystem scope lacks image provenance"); + if (image.AssemblyColumns(assembly) is null) + throw new InvalidDataException("Invalid TypeSystem scope"); + token = 0; + } + else _ = image.NamedType(token); + result.Add(new(type, module, image, token, assembly, name)); + } + return [.. result]; + } + + internal void Publish(Primitive[] entries) + { + int added = 0; + try { foreach (var entry in entries) { primitives.Add(entry.Type, entry); added++; } } + catch { for (int index = 0; index < added; index++) primitives.Remove(entries[index].Type); throw; } + } + + internal void Remove(Primitive[] entries) + { + foreach (var entry in entries) primitives.Remove(entry.Type); + } + + internal void Dispose() + { + disposed = true; + root.Children.Clear(); + nameNodes.Clear(); + heapNames.Clear(); + namedTypes.Clear(); + assemblies.Clear(); + primitives.Clear(); + methods.Clear(); + } +} diff --git a/Extractor/Symbols.cs b/Extractor/Symbols.cs index 5040e9e56..9db998a2c 100644 --- a/Extractor/Symbols.cs +++ b/Extractor/Symbols.cs @@ -1,6 +1,5 @@ using System.Security.Cryptography; using System.Text; -using System.Text.Json; using System.Text.RegularExpressions; using Mono.Cecil; using Mono.Cecil.Cil; @@ -11,8 +10,22 @@ namespace Nethermind.LeanCIL; // Extracted.lean depends on them, and a missing or mismatched PDB leaves extraction unchanged. internal static class Symbols { - // `module` gains debug information when a matching PDB is read; returns where it came from. - internal static string? Read(ModuleDefinition module, string assemblyPath, out string? notice) + /// A portable PDB's bytes and their SHA-256. + internal sealed record PdbFile(byte[] Bytes, string Sha256) + { + /// The PDB at , or null when there is none. + internal static PdbFile? Load(string path) + { + if (!File.Exists(path)) return null; + var bytes = File.ReadAllBytes(path); + return new(bytes, Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant()); + } + } + + // `module` gains debug information when a matching PDB is read; returns where it came from. `pdbFile` reads the + // PDB beside the assembly, so a batch of extractions can read and hash it once. + internal static string? Read(ModuleDefinition module, string assemblyPath, out string? notice, + Func? pdbFile = null) { notice = null; if (!module.HasDebugHeader) return null; @@ -24,11 +37,12 @@ internal static class Symbols module.ReadSymbols(new EmbeddedPortablePdbReaderProvider().GetSymbolReader(module, assemblyPath), true); return "embedded"; } - var pdb = Path.ChangeExtension(assemblyPath, ".pdb"); - if (!entries.Any(e => e.Directory.Type == ImageDebugType.CodeView) || !File.Exists(pdb)) return null; - var bytes = File.ReadAllBytes(pdb); - module.ReadSymbols(new PortablePdbReaderProvider().GetSymbolReader(module, new MemoryStream(bytes)), true); - return Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant(); + if (!entries.Any(e => e.Directory.Type == ImageDebugType.CodeView) || + (pdbFile ?? PdbFile.Load)(Path.ChangeExtension(assemblyPath, ".pdb")) is not { } pdb) + return null; + module.ReadSymbols(new PortablePdbReaderProvider().GetSymbolReader(module, + new MemoryStream(pdb.Bytes, writable: false)), true); + return pdb.Sha256; } catch (Exception error) when (error is SymbolsNotMatchingException or BadImageFormatException or InvalidOperationException) { @@ -58,8 +72,6 @@ static IEnumerable Points(MethodDefinition definition) => ? "\n Source: " + at : null; } - static string Quote(string text) => JsonSerializer.Serialize(text); - internal static string Emit(MethodDefinition[] methods, string origin) { var definitions = methods.Select(MethodSpecialization.Definition).ToArray(); @@ -87,7 +99,7 @@ void Visit(ScopeDebugInformation scope) { var end = scope.End.IsEndOfMethod ? count : pcs.GetValueOrDefault(scope.End.Offset, count); foreach (var variable in scope.Variables.Where(v => !v.IsDebuggerHidden && !string.IsNullOrEmpty(v.Name))) - scoped.Add($"({variable.Index}, {start}, {end}, {Quote(variable.Name)})"); + scoped.Add($"({variable.Index}, {start}, {end}, {IdentityText.Quote(variable.Name)})"); } foreach (var inner in scope.Scopes) Visit(inner); } @@ -100,7 +112,11 @@ void Visit(ScopeDebugInformation scope) { var pcs = Pcs(definition); return " [" + string.Join(", ", Points(definition).Where(p => pcs.ContainsKey(p.Offset)) - .Select(p => $"({pcs[p.Offset]}, {Quote(Display(p.Document.Url))}, {p.StartLine}, {p.StartColumn})")) + "]"; + .Select(p => + { + var path = IdentityText.Quote(Display(p.Document.Url)); + return $"({pcs[p.Offset]}, {path}, {p.StartLine}, {p.StartColumn})"; + })) + "]"; })) + "]"); lean.AppendLine("end ExtractedSymbols"); return lean.ToString(); diff --git a/Extractor/TailCalls.cs b/Extractor/TailCalls.cs index 872df9ade..d00f9c996 100644 --- a/Extractor/TailCalls.cs +++ b/Extractor/TailCalls.cs @@ -13,18 +13,6 @@ internal static bool IsInterior(Instruction instruction) => instruction.Previous?.OpCode.Code == Code.Tail || instruction.Previous?.Previous?.OpCode.Code == Code.Tail; - internal static void ValidateStructure(MethodDefinition method) - { - foreach (var prefix in method.Body.Instructions) - { - if (prefix.OpCode.Code != Code.Tail) continue; - if (prefix.Operand is not null || prefix.Next is not { } call || - call.OpCode.Code is not (Code.Call or Code.Callvirt or Code.Calli) || - call.Next is not { OpCode.Code: Code.Ret, Operand: null }) - throw new InvalidDataException("Unsupported tail prefix: requires an adjacent call and return"); - } - } - internal static void ValidateReachable(MethodDefinition method, Instruction prefix, string architecture = "scalar") { if (prefix.Next is { OpCode.Code: Code.Calli, Operand: Mono.Cecil.CallSite indirect }) @@ -38,9 +26,9 @@ internal static void ValidateReachable(MethodDefinition method, Instruction pref if (prefix.Next is not { OpCode.Code: Code.Call, Operand: MethodReference target } || InitAccessors.Return(target.ReturnType).FullName != InitAccessors.Return(method.ReturnType).FullName || - target.DeclaringType.Scope != method.Module || - InstructionTranslation.RuntimeModel(target) is not null) + target.DeclaringType.Scope != method.Module) throw new InvalidDataException( "Unsupported tail prefix: requires same-assembly direct call with matching return type"); + RuntimeModels.ValidateCallScope(target); } } diff --git a/Extractor/TemplateEmitter.cs b/Extractor/TemplateEmitter.cs index cce3244c3..e96032b91 100644 --- a/Extractor/TemplateEmitter.cs +++ b/Extractor/TemplateEmitter.cs @@ -7,12 +7,25 @@ namespace Nethermind.LeanCIL; // Templates are proof inputs. Only the generated, kernel-checked bindings connect them to CIL. internal static partial class TemplateEmitter { - internal sealed record Method(string Identity, string Metadata, string[] Ops, bool HasStaticSites); - internal sealed record Variant(string Directory, string AssemblyHash, FeatureProfile Profile, Method[] Methods); - sealed record Row(Variant Variant, Method Method, string[] Ops); - sealed record Template(string Identity, string Metadata, Feature[] Features, string[] Callees, Row[] Rows); + /// One decoded method's frame, observed hardware features, raw opcodes and linked method indices. + internal sealed record Method(string Identity, FrameMetadata Frame, Feature[] Features, int[] Opcodes, + int[] LinkedMethods); + internal sealed record Variant(string Directory, string AssemblyHash, FeatureProfile Profile, Method[] Methods) + { + /// The closed decoder inputs for this extraction in method index order. + internal required string DecoderInputs; + } + /// One profile's frame and body, its callees renumbered to the template's callee parameters. + sealed record Row(Variant Variant, FrameMetadata Frame, string ComparisonBody) + { + /// The qualified name of the decoded method in the shared module. + internal required string Body; + /// The decoded body's links with its method indices mapped to template parameters. + internal required string RelocatedLinks; + } + sealed record Template(string Identity, FrameMetadata Frame, Feature[] Features, string[] Callees, Row[] Rows, + int[] Opcodes); - const string MissingProfile = ".unsupported \"Profile combination not selected for template\""; const string Usage = "Usage: leancil templates output-directory extraction-directory... [--check] " + "[--namespace name] [--omit-equations] [--module file.lean]"; @@ -44,7 +57,7 @@ string Value() => ++index < args.Length && !args[index].StartsWith("--", StringC } } if (positional.Count < 2) throw new ArgumentException(Usage); - if (name is not null && !LeanName().IsMatch(name)) + if (name is not null && !LeanName.IsMatch(name)) throw new ArgumentException("--namespace takes a Lean name such as Proofs.Generated: " + name); var output = Path.GetFullPath(positional[0]); var modulePath = Path.GetFullPath(module ?? Path.Combine(output, "Templates.lean")); @@ -52,7 +65,7 @@ string Value() => ++index < args.Length && !args[index].StartsWith("--", StringC // The bindings import the module by its path under the output directory, a Lean source root. var moduleName = relative.EndsWith(".lean", StringComparison.Ordinal) ? relative[..^".lean".Length] .Replace('/', '.') : ""; - if (!LeanName().IsMatch(moduleName)) + if (!LeanName.IsMatch(moduleName)) throw new ArgumentException("--module takes a .lean file under the output directory whose path " + "names a Lean module: " + module); return new(output, [.. positional.Skip(1).Select(Path.GetFullPath)], name ?? "Templates", omit, modulePath, @@ -60,7 +73,11 @@ string Value() => ++index < args.Length && !args[index].StartsWith("--", StringC } [GeneratedRegex(@"\A[A-Za-z_][A-Za-z0-9_']*(\.[A-Za-z_][A-Za-z0-9_']*)*\z")] - private static partial Regex LeanName(); + internal static partial Regex LeanName { get; } + + /// The generated binding filenames checked for stale inputs. + [GeneratedRegex(@"\ATemplateBindings[0-9]+\.lean\z")] + internal static partial Regex BindingName { get; } /// /// Writes the shared template module, one binding per extraction and templates.json, with LF line endings, @@ -78,15 +95,16 @@ internal static void Run(string[] args, Func read) throw new InvalidDataException("Template inputs contain ambiguous managed method identities."); if (variants.Any(variant => string.Equals(output, variant.Directory, StringComparison.OrdinalIgnoreCase))) throw new InvalidDataException("Template output must be separate from its extraction inputs."); - var methods = variants.SelectMany(variant => variant.Methods.Select(method => - (Variant: variant, Method: method))).ToLookup(row => row.Method.Identity, StringComparer.Ordinal); + var methods = variants.SelectMany((variant, input) => variant.Methods.Select((method, index) => + (Variant: variant, Method: method, Body: $"Input{input}.method{index}"))) + .ToLookup(row => row.Method.Identity, StringComparer.Ordinal); var templates = methods.OrderBy(group => group.Key, StringComparer.Ordinal) .Select(group => Prepare(group.Key, group.ToArray())).ToArray(); var templateIndices = templates.Select((template, index) => (template.Identity, index)) .ToDictionary(pair => pair.Identity, pair => pair.index, StringComparer.Ordinal); var files = new SortedDictionary(StringComparer.Ordinal) { - [options.ModulePath] = EmitTemplates(templates, options.Namespace, options.OmitEquations) + [options.ModulePath] = EmitTemplates(templates, variants, options.Namespace, options.OmitEquations) }; for (var index = 0; index < variants.Length; index++) files[Path.Combine(output, $"TemplateBindings{index}.lean")] = @@ -140,7 +158,7 @@ static void Check(string output, SortedDictionary files) } if (Directory.Exists(output)) problems.AddRange(Directory.EnumerateFiles(output, "TemplateBindings*.lean") - .Where(path => Regex.IsMatch(Path.GetFileName(path), @"\ATemplateBindings[0-9]+\.lean\z") && + .Where(path => BindingName.IsMatch(Path.GetFileName(path)) && !files.ContainsKey(Path.GetFullPath(path))) .Order(StringComparer.Ordinal).Select(path => "Template output has no input: " + path)); if (problems.Count > 0) throw new InvalidDataException(string.Join("\n", problems)); @@ -148,94 +166,135 @@ static void Check(string output, SortedDictionary files) static string Flag(Feature feature) => FeatureProfile.LeanFeature(feature)[1..]; - static (string Identity, string Prefix, string Tail)? Callee(Variant variant, string op) - { - if (op.StartsWith(".newVector256 ", StringComparison.Ordinal)) - throw new InvalidDataException("Templates do not yet support vector constructor calls: " + op); - var prefix = new[] { ".call ", ".callInstance ", ".newAggregate ", ".newObject " } - .FirstOrDefault(prefix => op.StartsWith(prefix, StringComparison.Ordinal)); - if (prefix is null) return null; - var end = op.IndexOf(' ', prefix.Length); - if (end < 0 || !int.TryParse(op[prefix.Length..end], out var index) || - index < 0 || index >= variant.Methods.Length) - throw new InvalidDataException("Templates do not support imported callee slots: " + op); - return (variant.Methods[index].Identity, prefix, op[end..]); - } + static string CalleeIdentity(Variant variant, int index) => index >= 0 && index < variant.Methods.Length + ? variant.Methods[index].Identity + : throw new InvalidDataException("Templates do not support imported callee slots: method " + index); - static Template Prepare(string identity, (Variant Variant, Method Method)[] selected) + static Template Prepare(string identity, (Variant Variant, Method Method, string Body)[] selected) { var first = selected[0].Method; - if (selected.Any(row => row.Method.HasStaticSites != first.HasStaticSites || - row.Method.Metadata != first.Metadata || - row.Method.Ops.Length != first.Ops.Length)) + if (selected.Any(row => !row.Method.Opcodes.AsSpan().SequenceEqual(first.Opcodes))) throw new InvalidDataException("Template metadata or instruction count differs between profiles: " + identity); - var features = Enum.GetValues().Where(feature => selected.Any(row => - row.Method.Ops.Contains(".feature " + FeatureProfile.LeanFeature(feature), StringComparer.Ordinal))) - .ToArray(); - var callees = selected.SelectMany(row => row.Method.Ops.Select(op => Callee(row.Variant, op))) - .Where(callee => callee is not null).Select(callee => callee!.Value.Identity) + var features = selected.SelectMany(row => row.Method.Features).Distinct().Order().ToArray(); + var callees = selected.SelectMany(row => row.Method.LinkedMethods.Concat(row.Method.Frame.Methods) + .Select(index => CalleeIdentity(row.Variant, index))) .Distinct(StringComparer.Ordinal).Order(StringComparer.Ordinal).ToArray(); var calleeIndices = callees.Select((callee, index) => (callee, index)) .ToDictionary(pair => pair.callee, pair => pair.index, StringComparer.Ordinal); - var rows = selected.Select(row => new Row(row.Variant, row.Method, row.Method.Ops.Select(op => + var rows = selected.Select(row => { - var callee = Callee(row.Variant, op); - return callee is null ? op : callee.Value.Prefix + - "callee" + calleeIndices[callee.Value.Identity] + callee.Value.Tail; - }).ToArray())).ToArray(); - return new(identity, first.Metadata, features, callees, rows); + int Renumbered(int index) => calleeIndices[CalleeIdentity(row.Variant, index)]; + string Relocate(Func name) + { + if (row.Method.LinkedMethods.Length == 0) return row.Body; + var cases = string.Join(" ", row.Method.LinkedMethods.Select(index => + $"| {index} => {name(Renumbered(index))}")); + return $"({row.Body}.reindexLinks (fun {cases} | index => index))"; + } + return new Row(row.Variant, row.Method.Frame.Renumber(Renumbered), + Relocate(index => index.ToString(System.Globalization.CultureInfo.InvariantCulture))) + { Body = row.Body, RelocatedLinks = Relocate(Callee) + ".links" }; + }).ToArray(); + // Frames compare once their callees share the template's numbering, as each body does. + if (rows.Any(row => row.Frame != rows[0].Frame)) + throw new InvalidDataException("Template metadata or instruction count differs between profiles: " + + identity); + return new(identity, rows[0].Frame, features, callees, rows, first.Opcodes); } - // Missing combinations stay unsupported even where all selected rows share an instruction. - static string Instruction(Template template, Row[] rows, int pc, int featureIndex = 0) + static string Callee(int index) => $"callee{index}"; + + /// The decoded code and relocated links selected by the same feature flags. + static (string Code, string Links) SelectBody(Template template, Row[] rows, + List<(string Identity, string Left, string Right)> comparisons, int featureIndex = 0) { - if (rows.Length == 0) return MissingProfile; + if (rows.Length == 0) return ($"Extraction.templateHoles {template.Rows[0].Body}Closed.code", "[]"); if (featureIndex == template.Features.Length) { - var values = rows.Select(row => row.Ops[pc]).Distinct(StringComparer.Ordinal).ToArray(); - if (values.Length != 1) - throw new InvalidDataException("Profiles with identical observed flags emit different instructions: " + - template.Identity + " at pc " + pc); - return values[0]; + var selected = rows[0]; + foreach (var other in rows.AsSpan(1)) + comparisons.Add((template.Identity, selected.ComparisonBody, other.ComparisonBody)); + return ($"{selected.Body}.code", selected.RelocatedLinks); } var feature = template.Features[featureIndex]; - var enabled = Instruction(template, rows.Where(row => row.Variant.Profile.Get(feature)).ToArray(), - pc, featureIndex + 1); - var disabled = Instruction(template, rows.Where(row => !row.Variant.Profile.Get(feature)).ToArray(), - pc, featureIndex + 1); - return enabled == disabled ? enabled : $"if {Flag(feature)} then ({enabled}) else ({disabled})"; + var enabled = SelectBody(template, [.. rows.Where(row => row.Variant.Profile.Get(feature))], comparisons, featureIndex + 1); + var disabled = SelectBody(template, [.. rows.Where(row => !row.Variant.Profile.Get(feature))], comparisons, featureIndex + 1); + string Select(string yes, string no) => yes == no ? yes : $"if {Flag(feature)} then ({yes}) else ({no})"; + return (Select(enabled.Code, disabled.Code), Select(enabled.Links, disabled.Links)); } // Lines end in LF on every platform, so committed output compares equal wherever it was generated. - static string EmitTemplates(Template[] templates, string space, bool omitEquations) + static string EmitTemplates(Template[] templates, Variant[] variants, string space, bool omitEquations) { - var depth = LeanEmitter.RecursionDepth(templates.Max(template => template.Rows[0].Ops.Length)); - var lean = new StringBuilder("import CIL.InstructionEquations\n\n" + - "/-! Shared bodies for the selected extraction profiles; bindings establish their CIL identity. -/\n\n" + - $"namespace {space}\nopen CIL\n\nset_option maxRecDepth {depth}\n\n"); + var depth = LeanEmitter.RecursionDepth(templates.Max(template => template.Opcodes.Length)); + var lean = new StringBuilder(); void Line(string text = "") => lean.Append(text).Append('\n'); + Line("import CIL.InstructionEquations"); + Line("import CIL.Extraction.TemplateCode"); + Line("import CIL.Safety.Linking"); + Line(); + Line("/-! Shared decoded bodies for the selected extraction profiles. -/"); + Line(); + Line($"namespace {space}"); + Line("open CIL"); + Line($"set_option maxRecDepth {depth}"); + Line(); + for (var input = 0; input < variants.Length; input++) + { + Line($"namespace Input{input}"); + lean.Append(variants[input].DecoderInputs); + Line($"end Input{input}"); + Line(); + } + var decoderLength = lean.Length; + List<(string Identity, string Left, string Right)> comparisons = []; for (var index = 0; index < templates.Length; index++) { var template = templates[index]; Line($"/-- The stable managed identity of `method{index}`. -/"); - Line($"def method{index}Signature : String := {JsonSerializer.Serialize(template.Identity)}"); + Line($"def method{index}Signature : String := {IdentityText.Quote(template.Identity)}"); Line("/-- A fixed-PC template for the selected flag combinations, with explicit callee indices. -/"); - lean.Append($"@[cil_code] def method{index} (profile : FeatureProfile)"); + lean.Append($"@[cil_code] def method{index} (profile : FeatureProfile) " + + $"({FrameMetadata.EnvironmentLean} : LayoutEnvironment)"); foreach (var feature in template.Features) lean.Append($" ({Flag(feature)} : Bool)"); for (var callee = 0; callee < template.Callees.Length; callee++) lean.Append($" (callee{callee} : Nat)"); - Line($" : Method := {{ profile, {template.Metadata}, code := ["); - Line(string.Join(",\n", Enumerable.Range(0, template.Rows[0].Ops.Length) - .Select(pc => " " + Instruction(template, template.Rows, pc))) + "] }"); + var sites = template.Frame.StaticSitesLeanWith(Callee); + Line($$""" : Method := { {{template.Rows[0].Body}} with"""); + Line(" profile, environment,"); + var body = SelectBody(template, template.Rows, comparisons); + Line($" links := {body.Links}, staticSites := [{sites}],"); + Line($$""" code := cil_template_code% ({{body.Code}}) }"""); + var callees = Enumerable.Range(0, template.Callees.Length).Select(Callee).ToArray(); + var binders = callees.Length == 0 ? "" : $" ({string.Join(' ', callees)} : Nat)"; + foreach (var (rowIndex, row) in template.Rows.Index()) + { + var arguments = new List { "profile", "environment" }; + foreach (var feature in template.Features) + arguments.Add(LeanText.Bool(row.Variant.Profile.Get(feature))); + arguments.AddRange(callees); + var instance = $"(method{index} {string.Join(' ', arguments)})"; + var name = $"method{index}_input{rowIndex}"; + Line("/-- The selected flags give this input's decoded code for every caller. -/"); + Line($"theorem {name}_code (profile : FeatureProfile) (environment : LayoutEnvironment){binders} :"); + Line($" {instance}.code = {row.Body}.code := rfl"); + Line($"#print axioms {name}_code"); + Line("/-- The selected links enter this input's callees at the supplied method indices. -/"); + Line($"theorem {name}_links (profile : FeatureProfile) (environment : LayoutEnvironment){binders} :"); + Line($" {instance}.links = {row.RelocatedLinks} := rfl"); + Line($"#print axioms {name}_links"); + } if (!omitEquations) { Line($"cil_instruction_equations method{index}"); // The last equation's proof reaches every code suffix, so its axioms include every equation's. - if (template.Rows[0].Ops.Length is > 0 and var length) + if (template.Opcodes.Length is > 0 and var length) Line($"#print axioms method{index}_instruction{length - 1}"); } Line(); } + if (comparisons.Count != 0) + global::Program.CheckTemplateBodies(lean.ToString(0, decoderLength), space, comparisons); Line($"end {space}"); return lean.ToString(); } @@ -253,17 +312,18 @@ static string EmitBindings(Template[] templates, Dictionary templat { var templateIndex = templateIndices[variant.Methods[index].Identity]; var template = templates[templateIndex]; - var arguments = new List { "Extracted.profile" }; - arguments.AddRange(template.Features.Select(feature => variant.Profile.Get(feature) ? "true" : "false")); + var arguments = new List { "Extracted.profile", "Extracted.environment" }; + arguments.AddRange(template.Features.Select(feature => LeanText.Bool(variant.Profile.Get(feature)))); foreach (var callee in template.Callees) { var target = methodIndices.GetValueOrDefault(callee, -1); // A callee absent from this extraction can occur only under an excluded branch. arguments.Add(Math.Max(0, target).ToString(System.Globalization.CultureInfo.InvariantCulture)); } + var instance = $"{options.Namespace}.method{templateIndex} {string.Join(' ', arguments)}"; Line("/-- This extracted method is exactly the instantiated shared template. -/"); Line($"@[cil_code] theorem method{index}_template : Extracted.program[{index}]? ="); - Line($" some ({options.Namespace}.method{templateIndex} {string.Join(' ', arguments)}) := rfl"); + Line($" some ({instance}) := rfl"); Line($"#print axioms method{index}_template"); } Line("end TemplateBindings"); diff --git a/Extractor/TemplateVectorFrames.cs b/Extractor/TemplateVectorFrames.cs new file mode 100644 index 000000000..7fcee2f61 --- /dev/null +++ b/Extractor/TemplateVectorFrames.cs @@ -0,0 +1,67 @@ +using System.Text.Json; +using Nethermind.LeanCIL; + +partial class Program +{ + /// Require alternatives with the same observed flags to have identical decoded code and links. + internal static void CheckTemplateBodies(string decoderInputs, string space, + List<(string Identity, string Left, string Right)> comparisons) + { + using var verifier = new Program(); + var session = verifier.Library(CommonInputs()); + var checks = comparisons.Select(pair => + $"MethodBinding.firstCodeOrLinkDifference {pair.Left} {pair.Right}"); + const string marker = "template differences "; + var query = $""" + import CIL.MethodBinding + {decoderInputs} + #eval IO.println ("{marker}" ++ (Lean.toJson + ([{string.Join(",\n ", checks)}] : List (Option Nat))).compress) + end {space} + """; + var probe = Path.Combine(session, "TemplateComparisons.lean"); + File.WriteAllText(probe, query); + var output = Run([verifier.LeanExecutable, probe], cwd: session, leanPath: verifier.SessionLeanPath(session)); + var line = output.Split('\n').Single(line => line.StartsWith(marker, StringComparison.Ordinal)); + var differences = JsonSerializer.Deserialize(line[marker.Length..]); + if (differences is null || differences.Length != comparisons.Count) + throw new InvalidDataException("Lean did not compare every requested template alternative."); + for (var index = 0; index < differences.Length; index++) + if (differences[index] is int pc) + throw new InvalidDataException("Profiles with identical observed flags emit different " + + $"instructions: {comparisons[index].Identity} at pc {pc}"); + } + + /// Templates compare and print the same opaque slots as their checked extractions. + internal static TemplateEmitter.Method[] TemplateVectorFrames(string source, TemplateEmitter.Method[] methods) + { + List vectors = []; + for (int index = 0; index < methods.Length; index++) + if (methods[index].Frame.LocalKinds.Any(kind => kind is LocalKind.Vector { Bits: 128 })) + vectors.Add(index); + if (vectors.Count == 0) return methods; + + using var verifier = new Program(); + var session = verifier.Library(CommonInputs()); + var expressions = vectors.Select(index => $"Extracted.method{index}.opaqueVectorLocals"); + const string marker = "template vector slots "; + var query = $""" + {source} + #eval IO.println ("{marker}" ++ (Lean.toJson + ([{string.Join(", ", expressions)}] : List (List Nat))).compress) + """; + var probe = Path.Combine(session, "TemplateVectorFrames.lean"); + File.WriteAllText(probe, query); + var output = Run(["lean", probe], cwd: session, leanPath: verifier.SessionLeanPath(session)); + var result = output.Split('\n').Single(line => line.StartsWith(marker, StringComparison.Ordinal)); + var rows = JsonSerializer.Deserialize(result[marker.Length..]); + if (rows is null || rows.Length != vectors.Count || rows.Any(row => row is null)) + throw new InvalidDataException("Lean did not return the opaque vector slots of each requested frame."); + foreach (var (index, row) in vectors.Zip(rows)) + { + var method = methods[index]; + methods[index] = method with { Frame = method.Frame with { OpaqueVectorLocals = row } }; + } + return methods; + } +} diff --git a/Extractor/Throws.cs b/Extractor/Throws.cs index 3ccec6b17..c125e2f94 100644 --- a/Extractor/Throws.cs +++ b/Extractor/Throws.cs @@ -8,32 +8,50 @@ namespace Nethermind.LeanCIL; internal static class Throws { const int ArgumentLimit = 4; - internal sealed record Analysis(Dictionary Raises, HashSet Operands, HashSet Interior); + internal sealed record Analysis( + Dictionary Raises, HashSet Operands, HashSet Interior); - // Keep the same constructor allowlist in CIL.Extraction.exceptionConstructor. - static bool StringConstructor(string type, int count) => - (count <= 1 && type is "System.Exception" or "System.SystemException" or "System.ApplicationException" or - "System.ArithmeticException" or "System.DivideByZeroException" or "System.OverflowException" or - "System.InvalidOperationException" or "System.NotSupportedException" or "System.NotImplementedException" or - "System.ArgumentException" or "System.ArgumentNullException" or "System.ArgumentOutOfRangeException" or - "System.IndexOutOfRangeException" or "System.NullReferenceException" or "System.FormatException") || - (count == 2 && type is "System.ArgumentException" or "System.ArgumentNullException" or "System.ArgumentOutOfRangeException"); + const string Message = "System.String"; + const string Inner = "System.Exception"; + const string Actual = "System.Object"; - static bool ExceptionConstructor(MethodReference constructor) + // Keep the same parameter lists in CIL.Extraction.admittedParameters. + static string[][] AdmittedParameters(ExceptionType exception) => exception switch + { + ExceptionType.BaseException or ExceptionType.InvalidOperation => [[], [Message], [Message, Inner]], + ExceptionType.SystemException or ExceptionType.ApplicationException or ExceptionType.Arithmetic or + ExceptionType.DivideByZero or ExceptionType.Overflow or ExceptionType.NotSupported or + ExceptionType.NotImplemented or ExceptionType.IndexOutOfRange or ExceptionType.NullReference or + ExceptionType.Format => [[], [Message]], + ExceptionType.Argument => + [[], [Message], [Message, Message], [Message, Inner], [Message, Message, Inner]], + ExceptionType.ArgumentNull => [[], [Message], [Message, Message]], + ExceptionType.ArgumentOutOfRange => + [[], [Message], [Message, Message], [Message, Inner], [Message, Actual, Message]], + ExceptionType.InvalidCast or ExceptionType.ArrayTypeMismatch => [], + _ => throw new ArgumentOutOfRangeException(nameof(exception)) + }; + + /// The member whose runtime type names, or null for any other type. + internal static ExceptionType? Owner(TypeReference type) + { + if (type is TypeSpecification || type.IsNested || type.Namespace != "System" || + LayoutEvidence.Scope(type) != TypeTests.RuntimeScope) return null; + foreach (ExceptionType exception in ExceptionTypeText.All) + if (type.Name == exception.MetadataName()) return exception; + return null; + } + + static bool AdmittedConstructor(MethodReference constructor, ExceptionType exception) { - var type = constructor.DeclaringType.FullName; var parameters = constructor.Parameters.Select(parameter => parameter.ParameterType).ToArray(); if (parameters.Any(parameter => parameter is TypeSpecification || LayoutEvidence.Scope(parameter) != TypeTests.RuntimeScope)) return false; var names = parameters.Select(parameter => parameter.FullName).ToArray(); - return names.All(name => name == "System.String") && StringConstructor(type, names.Length) || - names is ["System.String", "System.Exception"] && type is "System.Exception" or - "System.InvalidOperationException" or "System.ArgumentException" or "System.ArgumentOutOfRangeException" || - type == "System.ArgumentException" && names is ["System.String", "System.String", "System.Exception"] || - type == "System.ArgumentOutOfRangeException" && names is ["System.String", "System.Object", "System.String"]; + return AdmittedParameters(exception).Any(admitted => admitted.SequenceEqual(names, StringComparer.Ordinal)); } - static string? Raised(Instruction instruction, IList code, int start, int count) => + static ExceptionType? Raised(Instruction instruction, IList code, int start, int count) => instruction.OpCode.Code == Code.Newobj && instruction.Operand is MethodReference constructor && constructor is not GenericInstanceMethod && constructor.Name == ".ctor" && constructor.HasThis && !constructor.ExplicitThis && constructor.CallingConvention == MethodCallingConvention.Default && !constructor.HasGenericParameters && @@ -42,8 +60,8 @@ static bool ExceptionConstructor(MethodReference constructor) parameter.ParameterType.MetadataType == MetadataType.String || code[start + index].OpCode.Code == Code.Ldnull).All(valid => valid) && constructor.DeclaringType is not TypeSpecification && !constructor.DeclaringType.IsValueType && - ExceptionConstructor(constructor) && - LayoutEvidence.Scope(constructor.DeclaringType) == TypeTests.RuntimeScope ? constructor.DeclaringType.FullName : null; + Owner(constructor.DeclaringType) is ExceptionType owner && AdmittedConstructor(constructor, owner) + ? owner : null; static readonly System.Runtime.CompilerServices.ConditionalWeakTable cache = new(); @@ -56,12 +74,12 @@ static Analysis Compute(MethodDefinition method) for (int at = 0; at < code.Count; at++) { int raise; - string name; + ExceptionType raised; if (code[at].OpCode.Code == Code.Ldnull && at + 1 < code.Count && code[at + 1].OpCode.Code == Code.Throw) { raise = at + 1; - name = "System.NullReferenceException"; + raised = ExceptionType.NullReference; } else { @@ -71,13 +89,13 @@ static Analysis Compute(MethodDefinition method) code[at + count].OpCode.Code is Code.Ldstr or Code.Ldnull) count++; raise = at + count + 1; if (count == ArgumentLimit || raise >= code.Count || code[raise].OpCode.Code != Code.Throw || - Raised(code[at + count], code, at, count) is not string constructed) + Raised(code[at + count], code, at, count) is not ExceptionType constructed) continue; - name = constructed; + raised = constructed; } for (int k = at; k < raise; k++) analysis.Operands.Add(code[k]); for (int k = at + 1; k <= raise; k++) analysis.Interior.Add(code[k]); - analysis.Raises.Add(code[raise], name); + analysis.Raises.Add(code[raise], raised); at = raise; } return analysis; diff --git a/Extractor/TypeTests.cs b/Extractor/TypeTests.cs index a5aa79685..e74be3f4b 100644 --- a/Extractor/TypeTests.cs +++ b/Extractor/TypeTests.cs @@ -9,7 +9,8 @@ internal static class TypeTests { internal sealed record Analysis(Dictionary Results, HashSet Operands, HashSet Interior); - internal const string RuntimeScope = "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"; + // The scope text the extractor's string-keyed type identities compare, spelled from the platform identity. + internal static readonly string RuntimeScope = PlatformAssemblies.SystemRuntime.ToDisplay(); sealed record Node(string Kind, string Scope = "", string Name = "", Node? Left = null, Node? Right = null); @@ -48,28 +49,37 @@ internal static bool PlainName(string name) => return (Constructed(a) || Plain(a)) && (Constructed(b) || Plain(b)) ? false : null; } - static bool TypeMethod(Instruction instruction, string name, string signature) => - instruction.OpCode.Code == Code.Call && instruction.Operand is MethodReference method && - method is not GenericInstanceMethod && method.Name == name && method.DeclaringType is not TypeSpecification && - method.DeclaringType.FullName == "System.Type" && LayoutEvidence.Scope(method.DeclaringType) == RuntimeScope && - !method.HasThis && method.ReturnType.FullName != "System.Void" && - LayoutEvidence.Scope(method.DeclaringType) + "::" + method.FullName == RuntimeScope + "::" + signature; + static bool RuntimeType(TypeReference type, string name, bool valueType = false) => + type is not TypeSpecification && !type.IsNested && type.Namespace == "System" && type.Name == name && + type.IsValueType == valueType && RuntimeModels.ExactScope(type, PlatformAssemblies.SystemRuntime); - static bool FromHandle(Instruction instruction) => TypeMethod(instruction, "GetTypeFromHandle", - "System.Type System.Type::GetTypeFromHandle(System.RuntimeTypeHandle)"); + static bool TypeMethod(Instruction instruction, string name) + { + if (instruction.OpCode.Code != Code.Call || instruction.Operand is not MethodReference method || + method is GenericInstanceMethod || method.HasGenericParameters || method.Name != name || + method.HasThis || method.ExplicitThis || method.CallingConvention != MethodCallingConvention.Default || + !RuntimeType(method.DeclaringType, "Type")) return false; + + if (name == "GetTypeFromHandle") + return RuntimeType(method.ReturnType, "Type") && method.Parameters is [var handle] && + RuntimeType(handle.ParameterType, "RuntimeTypeHandle", valueType: true); + return method.ReturnType.MetadataType == MetadataType.Boolean && method.Parameters is [var left, var right] && + RuntimeType(left.ParameterType, "Type") && RuntimeType(right.ParameterType, "Type"); + } static bool? Comparison(Instruction instruction) => instruction.OpCode.Code == Code.Ceq && instruction.Operand is null ? true : instruction.Operand is Instruction && instruction.OpCode.Code is Code.Beq or Code.Beq_S ? true : instruction.Operand is Instruction && instruction.OpCode.Code is Code.Bne_Un or Code.Bne_Un_S ? false : - TypeMethod(instruction, "op_Equality", "System.Boolean System.Type::op_Equality(System.Type,System.Type)") ? true : - TypeMethod(instruction, "op_Inequality", "System.Boolean System.Type::op_Inequality(System.Type,System.Type)") ? false : null; + TypeMethod(instruction, "op_Equality") ? true : + TypeMethod(instruction, "op_Inequality") ? false : null; static bool? Test(IList code, int at) { if (at + 4 >= code.Count || code[at].OpCode.Code != Code.Ldtoken || code[at].Operand is not TypeReference left || code[at + 2].OpCode.Code != Code.Ldtoken || code[at + 2].Operand is not TypeReference right || - !FromHandle(code[at + 1]) || !FromHandle(code[at + 3]) || Comparison(code[at + 4]) is not bool equality) + !TypeMethod(code[at + 1], "GetTypeFromHandle") || !TypeMethod(code[at + 3], "GetTypeFromHandle") || + Comparison(code[at + 4]) is not bool equality) return null; return Same(Expr(left), Expr(right)) is bool same ? same == equality : null; } diff --git a/Extractor/UnsafeModels.cs b/Extractor/UnsafeModels.cs index 36f81181d..92db1a6b7 100644 --- a/Extractor/UnsafeModels.cs +++ b/Extractor/UnsafeModels.cs @@ -1,22 +1,71 @@ using Mono.Cecil; +using Mono.Cecil.Cil; namespace Nethermind.LeanCIL; // Generic runtime helpers share the same validated managed layout as CIL storage. internal static class UnsafeModels { - internal static string? Translate(MethodReference reference, string architecture = "scalar") + static readonly TypeLevel Owner = new(MetadataName.OfText("System.Runtime.CompilerServices"), + MetadataName.OfText("Unsafe")); + + // A modelled helper keeps its call. Extractor analyses read the operation selected for that call. + internal static Operation? CallModel(MethodReference reference, string architecture) => + ClassifyCall(reference, architecture, out _, out _, out _); + + // Proof outlines read the closed storage type from the validated unaligned call at this site. + internal static TypeReference? UnalignedStorage(Instruction site, string name, string architecture) + { + if (site.OpCode.Code != Code.Call || site.Operand is not GenericInstanceMethod call || call.Name != name) + return null; + ClassifyCall(call, architecture, out var storage, out _, out _); + return storage; + } + + /// The signedness and native-width stride of a retained Unsafe pointer addition. + internal static (ulong Stride, bool Signed)? Offset(Instruction site, string architecture) + { + if (site.OpCode.Code != Code.Call || site.Operand is not GenericInstanceMethod + { Name: "Add" or "Subtract" or "AddByteOffset" } call) return null; + ClassifyCall(call, architecture, out _, out var offset, out _); + return offset; + } + + /// The element width of an admitted Unsafe.SizeOf call. + internal static int? SizeOf(MethodReference reference, string architecture) + { + if (reference.Name != "SizeOf") return null; + ClassifyCall(reference, architecture, out _, out _, out var size); + return size; + } + + static Operation? ClassifyCall(MethodReference reference, string architecture, out TypeReference? storage, + out (ulong Stride, bool Signed)? offset, out int? size) { - if (MemoryTransfers.Unsafe(reference, architecture) is { } transfer) return transfer; - if (ReinterpretReference(reference, architecture) is { } cast) return cast; + size = null; + storage = null; + offset = null; + if (reference.Name is "As" or "AsRef" or "BitCast") + { + if (reference.DeclaringType is TypeSpecification || + !IdentityRows.TopLevelMatches(reference.DeclaringType, Owner, PlatformAssemblies.SystemRuntime)) + return null; + return reference.Name == "BitCast" ? ValueBitCasts.CallModel(reference, architecture) : + ReinterpretReference(reference, architecture); + } if (reference.DeclaringType.FullName != "System.Runtime.CompilerServices.Unsafe" || - !RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime") || + !RuntimeModels.ExactScope(reference.DeclaringType, PlatformAssemblies.SystemRuntime) || reference is not GenericInstanceMethod generic || generic.GenericArguments.Count != 1 || generic.ElementMethod.GenericParameters.Count != 1 || reference.HasThis || reference.ExplicitThis || (generic.ElementMethod.CallingConvention & ~MethodCallingConvention.Generic) != MethodCallingConvention.Default) return null; var element = generic.GenericArguments[0]; + if (reference.Name is "Read" or "Write" or "ReadUnaligned" or "WriteUnaligned") + return generic.ElementMethod.CallingConvention == MethodCallingConvention.Generic && + reference.DeclaringType is not TypeSpecification && + IdentityRows.TopLevelMatches(reference.DeclaringType, Owner, PlatformAssemblies.SystemRuntime) + ? MemoryTransfers.CallModel(generic, architecture, out storage) : null; var definition = generic.ElementMethod; bool Parameter(TypeReference type) => type is GenericParameter parameter && parameter.Type == GenericParameterType.Method && parameter.Position == 0 && parameter.Owner == definition; @@ -34,7 +83,8 @@ bool Parameter(TypeReference type) => type is GenericParameter parameter && reference.Parameters[1].ParameterType.FullName == "System.Int32")); var skipInit = reference.Name == "SkipInit" && reference.Parameters.Count == 1 && Reference(reference.Parameters[0].ParameterType) && reference.ReturnType.MetadataType == MetadataType.Void; - var nullRef = reference.Name == "NullRef" && reference.Parameters.Count == 0 && Reference(reference.ReturnType); + var nullRef = reference.Name == "NullRef" && reference.Parameters.Count == 0 && + Reference(reference.ReturnType); var isNullRef = reference.Name == "IsNullRef" && reference.Parameters.Count == 1 && Reference(reference.Parameters[0].ParameterType) && reference.ReturnType.MetadataType == MetadataType.Boolean; var referencePair = reference.Parameters.Count == 2 && @@ -56,41 +106,47 @@ bool Parameter(TypeReference type) => type is GenericParameter parameter && throw new InvalidDataException($"Unsupported Unsafe element type: {element.FullName}"); MetadataValidation.Type(element, reference.Module, architecture: architecture); RuntimeModels.ValidateSignature(reference, architecture); - if (skipInit) return ".skipInit"; - if (nullRef) return ".memory .nullRef"; - if (isNullRef) return ".memory .isNullRef"; - var width = element is GenericInstanceType vector && RuntimeModels.SupportedType(vector) - ? vector.ElementType.FullName switch - { - "System.Runtime.Intrinsics.Vector128`1" => 16, - "System.Runtime.Intrinsics.Vector256`1" => 32, - "System.Runtime.Intrinsics.Vector512`1" => 64, - _ => throw new InvalidDataException($"Unsupported Unsafe element layout: {element.FullName}") - } - : MetadataValidation.FieldSize(element, reference.Module, architecture); + if (nullRef || isNullRef) return Unoutlined.Site; + var width = ElementWidth(element, reference.Module, architecture); + if (skipInit) return Unoutlined.Site; if (comparison) return reference.Name switch { - "AreSame" => ".eq", - "IsAddressLessThan" => ".ltu", - _ => ".gtu" + "AreSame" => new StackOperation(Code.Ceq), + "IsAddressLessThan" => new StackOperation(Code.Clt_Un), + _ => new StackOperation(Code.Cgt_Un) }; - if (byteOffset) return ".memory .byteOffset"; - if (subtractBytes) return ".sub"; - if (sizeOf) return $".const32 {width}"; + if (byteOffset) return Unoutlined.Site; + if (subtractBytes) return new StackOperation(Code.Sub); + if (sizeOf) + { + size = width; + return Unoutlined.Site; + } var signed = reference.Parameters[1].ParameterType.FullName != "System.UIntPtr"; // Native-width negative strides reuse the checked wrapping offset operation. var stride = reference.Name == "Subtract" ? unchecked(0UL - (ulong)width) : (ulong)(addBytes ? 1 : width); - return $".memory (.add {stride} {signed.ToString().ToLowerInvariant()})"; + offset = (stride, signed); + return Unoutlined.Site; } + // An element of field or vector storage has a width; any other element is unsupported. + static int ElementWidth(TypeReference element, ModuleDefinition module, string architecture) => + element is GenericInstanceType vector && RuntimeModels.SupportedType(vector) + ? vector.ElementType.FullName switch + { + "System.Runtime.Intrinsics.Vector128`1" => 16, + "System.Runtime.Intrinsics.Vector256`1" => 32, + "System.Runtime.Intrinsics.Vector512`1" => 64, + _ => throw new InvalidDataException($"Unsupported Unsafe element layout: {element.FullName}") + } + : MetadataValidation.FieldSize(element, module, architecture); + // A byref reinterpretation changes only its static type, never the allocation, // offset, initialized bytes or access bounds used by checked memory operations. - private static string? ReinterpretReference(MethodReference reference, string architecture) + private static Operation? ReinterpretReference(MethodReference reference, string architecture) { - if (reference.DeclaringType.FullName != "System.Runtime.CompilerServices.Unsafe" || - !RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime") || - reference is not GenericInstanceMethod generic || reference.HasThis || reference.ExplicitThis || + if (reference is not GenericInstanceMethod generic || reference.HasThis || reference.ExplicitThis || (generic.ElementMethod.CallingConvention & ~MethodCallingConvention.Generic) != MethodCallingConvention.Default) return null; var definition = generic.ElementMethod; @@ -110,8 +166,52 @@ bool Reference(TypeReference type, int position) => type is ByReferenceType byRe if (type is ByReferenceType or PointerType or GenericParameter) throw new InvalidDataException($"Unsupported Unsafe reference element: {type.FullName}"); MetadataValidation.Type(type, reference.Module, architecture: architecture); + // A span, array or class reference is not the address of bytes the other type can name. + _ = ElementWidth(type, reference.Module, architecture); + } + RuntimeModels.ValidateSignature(reference, architecture); + return Unoutlined.Site; + } +} + +// A value cast reads the closed pair's representations and preserves the snapshot's initialization flags. +internal static class ValueBitCasts +{ + internal static Operation? CallModel(MethodReference reference, string architecture) + { + if (reference is not GenericInstanceMethod generic || generic.GenericArguments.Count != 2 || + generic.ElementMethod.GenericParameters.Count != 2 || reference.HasThis || reference.ExplicitThis || + generic.ElementMethod.CallingConvention != MethodCallingConvention.Generic || + reference.Parameters.Count != 1) return null; + + bool Parameter(TypeReference type, int position) => type is GenericParameter parameter && + parameter.Type == GenericParameterType.Method && parameter.Position == position && + parameter.Owner == generic.ElementMethod; + if (!Parameter(reference.Parameters[0].ParameterType, 0) || !Parameter(reference.ReturnType, 1)) return null; + + var source = generic.GenericArguments[0]; + var target = generic.GenericArguments[1]; + if (source.MetadataType == MetadataType.Byte && target.MetadataType == MetadataType.Boolean) + { + RuntimeModels.ValidateSignature(reference, architecture); + return Unoutlined.Site; } + if (!generic.GenericArguments.Any(type => MetadataValidation.IsAggregate(type, reference.Module) || + type is GenericInstanceType)) return null; + var fromWidth = SnapshotWidth(source, reference.Module, architecture); + var toWidth = SnapshotWidth(target, reference.Module, architecture); + if (fromWidth is null || toWidth is null || fromWidth != toWidth) + throw new InvalidDataException($"Unsupported Unsafe.BitCast value layouts: {reference.FullName}"); RuntimeModels.ValidateSignature(reference, architecture); - return ".memory .asRef"; + return Unoutlined.Site; + } + + static int? SnapshotWidth(TypeReference type, ModuleDefinition module, string architecture) + { + MetadataValidation.Type(type, module, architecture: architecture); + RuntimeModels.ValidateTypeIdentity(type, module, architecture); + if (MetadataValidation.IsAggregate(type, module)) + return MetadataValidation.Layout(type, module, architecture); + return MetadataValidation.IntegerVectorWidth(type); } } diff --git a/Extractor/ValueBitCasts.cs b/Extractor/ValueBitCasts.cs deleted file mode 100644 index 313af1589..000000000 --- a/Extractor/ValueBitCasts.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Mono.Cecil; - -namespace Nethermind.LeanCIL; - -// Value reinterpretation is distinct from Unsafe.As's byref identity. -internal static class ValueBitCasts -{ - internal static string? Translate(MethodReference reference, string architecture = "scalar") - { - if (reference.Name != "BitCast" || reference.DeclaringType.FullName != "System.Runtime.CompilerServices.Unsafe" || - !RuntimeModels.ExactScope(reference.DeclaringType.Scope, "System.Runtime") || - reference is not GenericInstanceMethod generic || generic.GenericArguments.Count != 2 || - generic.ElementMethod.GenericParameters.Count != 2 || reference.HasThis || reference.ExplicitThis || - (generic.ElementMethod.CallingConvention & ~MethodCallingConvention.Generic) != MethodCallingConvention.Default || - reference.Parameters.Count != 1) - return null; - - bool Parameter(TypeReference type, int index) => type is GenericParameter parameter && - parameter.Type == GenericParameterType.Method && parameter.Position == index && - parameter.Owner == generic.ElementMethod; - if (!Parameter(reference.Parameters[0].ParameterType, 0) || !Parameter(reference.ReturnType, 1)) return null; - - (string Shape, int Width)? Shape(TypeReference type) - { - MetadataValidation.Type(type, reference.Module, architecture: architecture); - RuntimeModels.ValidateTypeIdentity(type, reference.Module, architecture); - if (MetadataValidation.IsAggregate(type, reference.Module)) - { - int width = MetadataValidation.Layout(type, reference.Module, architecture); - return ($"(.aggregate {width})", width); - } - if (MetadataValidation.IntegerVectorWidth(type) is int vectorWidth) - return ($".vector{vectorWidth * 8}", vectorWidth); - return null; - } - - // Leave existing independently validated scalar overloads to their matcher. - if (!generic.GenericArguments.Any(t => MetadataValidation.IsAggregate(t, reference.Module) || - t is GenericInstanceType)) return null; - var source = Shape(generic.GenericArguments[0]); - var target = Shape(generic.GenericArguments[1]); - if (source is null || target is null || source.Value.Width != target.Value.Width) - throw new InvalidDataException($"Unsupported Unsafe.BitCast value layouts: {reference.FullName}"); - RuntimeModels.ValidateSignature(reference, architecture); - return $".memory (.bitcastValue {source.Value.Shape} {target.Value.Shape})"; - } -} diff --git a/Extractor/ValueList.cs b/Extractor/ValueList.cs new file mode 100644 index 000000000..cc7c45bea --- /dev/null +++ b/Extractor/ValueList.cs @@ -0,0 +1,53 @@ +using System.Collections; +using System.Runtime.CompilerServices; + +namespace Nethermind.LeanCIL; + +/// Builds a from a collection expression. +internal static class ValueList +{ + internal static ValueList Create(ReadOnlySpan items) => new(items); +} + +/// +/// An immutable list that compares by its elements, for the collection members of records, which would +/// otherwise compare by reference. It copies what it is built from, so the list never changes. A class +/// rather than a struct keeps the copy constructors of the records that hold it admitted by the self-scan. +/// +[CollectionBuilder(typeof(ValueList), nameof(ValueList.Create))] +internal sealed class ValueList : IReadOnlyList, IEquatable> +{ + readonly T[] items; + + public ValueList(ReadOnlySpan source) => items = source.ToArray(); + + public ValueList(T[]? source) : this(new ReadOnlySpan(source)) + { + } + + public int Count => items.Length; + + public T this[int index] => items[index]; + + public IEnumerator GetEnumerator() => ((IEnumerable)items).GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => items.GetEnumerator(); + + public bool Equals(ValueList? other) => other is not null && items.SequenceEqual(other.items); + + public override bool Equals(object? obj) => Equals(obj as ValueList); + + public override int GetHashCode() + { + var hash = new HashCode(); + foreach (var item in items) hash.Add(item); + return hash.ToHashCode(); + } + + public static bool operator ==(ValueList? left, ValueList? right) => + left is null ? right is null : left.Equals(right); + + public static bool operator !=(ValueList? left, ValueList? right) => !(left == right); + + public static implicit operator ValueList(T[] source) => new(source); +} diff --git a/Extractor/ValueTuples.cs b/Extractor/ValueTuples.cs index 8d837929f..b6882e777 100644 --- a/Extractor/ValueTuples.cs +++ b/Extractor/ValueTuples.cs @@ -7,12 +7,21 @@ internal static class ValueTuples { internal static bool Is(TypeReference type) => type is GenericInstanceType { ElementType.FullName: "System.ValueTuple`2", GenericArguments.Count: 2 } tuple && - RuntimeModels.ExactScope(tuple.ElementType.Scope, "System.Runtime"); + RuntimeModels.ExactScope(tuple.ElementType, PlatformAssemblies.SystemRuntime); + + // Only primitive element codes may supply the fields of a certified pair layout. + internal static void RequirePrimitiveArguments(GenericInstanceType tuple) + { + foreach (var argument in tuple.GenericArguments) + if (!StructuralSignatures.Current.IsPrimitiveElement(argument)) + throw new InvalidDataException($"Unsupported field type: {argument.FullName}"); + } // Item1 and Item2 with the instance's type arguments, checked against the resolved runtime definition. internal static MetadataValidation.InstanceField[] Fields(TypeReference type) { var tuple = (GenericInstanceType)type; + RequirePrimitiveArguments(tuple); var definition = tuple.ElementType.Resolve() ?? throw new InvalidDataException($"Unresolved aggregate: {type.FullName}"); var fields = definition.Fields.Where(field => !field.IsStatic).ToArray(); if (!tuple.IsValueType || !tuple.ElementType.IsValueType || !definition.IsValueType || !definition.IsAutoLayout || diff --git a/Extractor/VirtualDispatch.cs b/Extractor/VirtualDispatch.cs index 73029e5a5..1378c93b5 100644 --- a/Extractor/VirtualDispatch.cs +++ b/Extractor/VirtualDispatch.cs @@ -73,17 +73,15 @@ reference.DeclaringType.Scope is AssemblyNameReference scope && internal static IEnumerable Hierarchy(MethodReference reference) { var declaring = reference.DeclaringType.Resolve(); - return declaring is null ? [] : Classes(declaring.Module).Where(candidate => Derives(candidate, declaring)); + if (declaring is null) return []; + return PinnedAssemblies.Active is { } pins + ? pins.DispatchHierarchy(declaring) : DerivedClasses([declaring.Module], declaring); } - // Every class of the pinned assemblies, nested ones included; without pins, the declaring module alone. - static IEnumerable Classes(ModuleDefinition module) - { - IEnumerable modules = PinnedAssemblies.Active is { } pins - ? [pins.Selected, .. pins.References.Select(reference => reference.Module)] : [module]; - return modules.SelectMany(each => each.GetTypes()) - .Where(type => type is { IsValueType: false, IsInterface: false }); - } + /// Every class in the modules, including nested classes, that derives from the slot's class. + internal static IEnumerable DerivedClasses(IEnumerable modules, + TypeDefinition declaring) => modules.SelectMany(module => module.GetTypes()) + .Where(type => type is { IsValueType: false, IsInterface: false } && Derives(type, declaring)); static bool Derives(TypeDefinition candidate, TypeDefinition declaring) { diff --git a/README.md b/README.md index 4266a3d89..a0bb3a4a1 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ The extractor, CIL model and verification machinery have an explicit ## Quick start -Install **Lean 4.35.0** through elan and **.NET SDK 10.0.403**, as pinned in +Install **Lean 4.35.0** through elan and **.NET SDK 11.0.100**, as pinned in [lean-toolchain](lean-toolchain) and [global.json](global.json). From this checkout: ```sh @@ -86,6 +86,7 @@ generic methods, loops, memory access and compiled F#. | `leancil bindings` | Generate the proof sources of a binding catalog, or check them | | `leancil export-contracts` | Publish verified method contracts for separately compiled callers | | `leancil audit-library` | Kernel-replay and axiom-audit the theorems of a Lean library | +| `leancil verify --deep-checks` | Reconfirm in the kernel that an extraction's method bodies and carried metadata rows are those of assembly files with stated digests | `verify --goals` prints the goal at each `sorry` and failing tactic of an unfinished proof. A failing command returns a nonzero exit code. See the [CLI reference](docs/CLI.md) for every diff --git a/StrictBinding.lean b/StrictBinding.lean new file mode 100644 index 000000000..52611663d --- /dev/null +++ b/StrictBinding.lean @@ -0,0 +1,34 @@ +import StrictBinding.Image +import StrictBinding.Sha256 +import CIL.Crypto.Sha1 +import StrictBinding.Pe +import StrictBinding.Tables +import StrictBinding.Identity +import StrictBinding.Dump +import StrictBinding.Bind +import StrictBinding.Evidence +import StrictBinding.Reads +import StrictBinding.Rows +import StrictBinding.Slice +import StrictBinding.Sorted +import StrictBinding.TypeDefs +import StrictBinding.StaticData +import StrictBinding.ObjectFields +import StrictBinding.TupleFields +import StrictBinding.PublicKeys +import StrictBinding.EnumStorageEvidence +import StrictBinding.PlatformKinds +import StrictBinding.LayoutContent +import StrictBinding.DispatchCatalogs +import StrictBinding.GenericImports + +/-! +# Deep verification + +Kernel-checked facts that bind an extraction to the assembly file it was read from, for the +opt-in `leancil verify --deep-checks` command. A consumer states the file's SHA-256 digest; +the audit proves that the digest of an image constant equals it, decodes the PE container and the +metadata tables from the same constant, and checks claims of the extraction against the bytes at +positions it computes itself. This library is not part of `CIL`: no certified module imports it, +and the default `verify`, `check` and `extract` flow never reads it. +-/ diff --git a/StrictBinding/Bind.lean b/StrictBinding/Bind.lean new file mode 100644 index 000000000..cf87fcba6 --- /dev/null +++ b/StrictBinding/Bind.lean @@ -0,0 +1,210 @@ +import StrictBinding.Identity + +/-! +# Binding claims to images + +A claim names an assembly by its identity and says that some bytes belong to it at a place the +claim does not state. The audit decodes each pinned image, checks that the Assembly row spells +the identity the image is pinned under, and takes the place from the image itself: a method body +is at the file offset that the section table gives for the relative virtual address in the +method's own MethodDef row. + +`checkAll` prepares the images once and runs one kind of claim check over every claim in order; +`ClaimsHold` is the proposition an audited theorem states about it, and `bodyClaimBound` is the +check for method bodies. A generated module proves the claims a page at a time, each page its own +kernel evaluation, and `claimsHold_of_pages` composes the pages into the claims. +-/ + +namespace StrictBinding + +/-- A pinned image, decoded: its identity, the checked view of its bytes and its tables. -/ +structure PreparedImage where + identity : String + view : ImageView + layout : MetadataLayout + /-- The offset and width of the RVA column of a MethodDef row, computed once. -/ + rvaColumn : Nat × Nat + /-- The offset and width of the implementation flags column of a MethodDef row. -/ + implementationColumn : Nat × Nat + /-- The laid-out columns of each table, indexed by table number, computed once. -/ + columns : List (Option (List ColumnPlace)) + +/-- A pinned image decoded, with its identity checked against its Assembly row. -/ +def prepareOne (pinned : PinnedImage) : Except AuditError PreparedImage := do + let view ← ImageView.ofImage pinned.image + let layout ← decodeMetadata view + if (← assemblyIdentity view layout) != asciiOf pinned.identity then + throw (.identityMismatch pinned.identity) + pure { identity := pinned.identity, view, layout, rvaColumn := ← layout.columnIn 0x06 0, + implementationColumn := ← layout.columnIn 0x06 1, columns := layout.columnTable } + +/-- The laid-out columns of `table` in the image's rows. -/ +def PreparedImage.columnsOf (image : PreparedImage) (table : Nat) : Option (List ColumnPlace) := + image.columns.getD table none + +/-- The identity of the first prepared image that a later one repeats. -/ +def duplicateIdentity? : List PreparedImage → Option String + | [] => none + | image :: rest => + if rest.any (·.identity == image.identity) then some image.identity + else duplicateIdentity? rest + +/-- Every pinned image decoded, once, with distinct identities. -/ +def prepare (images : List PinnedImage) : Except AuditError (List PreparedImage) := do + let prepared ← images.mapM prepareOne + match duplicateIdentity? prepared with + | some identity => throw (.duplicateImage identity) + | none => pure prepared + +/-- The prepared image that is pinned under `identity`. -/ +def located (prepared : List PreparedImage) (identity : String) : + Except AuditError PreparedImage := + match prepared.find? (·.identity == identity) with + | some image => pure image + | none => throw (.unpinned identity) + +/-- The image `located` selects is one of the prepared images. -/ +theorem mem_of_located {prepared : List PreparedImage} {identity : String} + {image : PreparedImage} (found : located prepared identity = .ok image) : + image ∈ prepared := by + unfold located at found + cases selected : prepared.find? (·.identity == identity) with + | none => simp [selected] at found + | some selectedImage => + have same : selectedImage = image := by + simpa [selected, pure, Except.pure] using found + subst image + exact List.mem_of_find?_eq_some selected + +#print axioms mem_of_located + +/-- A claim that the bytes of a method body, its header and its code, belong to the assembly + `identity`, as the body of the method that `token` names. -/ +structure BodyClaim where + identity : String + /-- A MethodDef token. -/ + token : Nat + bytes : List Nat + +/-- The MethodDef row of the claim has a body and the claimed bytes are at the file offset its + relative virtual address maps to. -/ +def bodyBound (image : PreparedImage) (token : Nat) (bytes : List Nat) : + Except AuditError Unit := do + if token / 2 ^ 24 != 0x06 then throw (.notMethodDef token) + let (row, _) ← image.layout.rowAt token + let rva ← image.view.u (row + image.rvaColumn.1) image.rvaColumn.2 + let implementation ← + image.view.u (row + image.implementationColumn.1) image.implementationColumn.2 + if rva == 0 || implementation % 4 != 0 then throw (.noIlBody token) + let offset ← mapRva image.layout.pe.sections rva bytes.length + unless (← image.view.hasBytesAt offset bytes) do throw (.bodyMismatch token rva offset) + +/-- Every claim holds against the images prepared once, each claim checked once and in order. -/ +def checkAll {Claim : Type} (check : List PreparedImage → Claim → Except AuditError Unit) + (images : List PinnedImage) (claims : List Claim) : Except AuditError Unit := do + let prepared ← prepare images + claims.forM (check prepared) + +/-- The proposition every audited check states: the claims hold against the pinned images. -/ +def ClaimsHold {Claim : Type} (check : List PreparedImage → Claim → Except AuditError Unit) + (images : List PinnedImage) (claims : List Claim) : Prop := + ok (checkAll check images claims) = true + +instance {Claim : Type} (check : List PreparedImage → Claim → Except AuditError Unit) + (images : List PinnedImage) (claims : List Claim) : + Decidable (ClaimsHold check images claims) := + inferInstanceAs (Decidable (_ = _)) + +/-- Claims that each hold against one preparation hold together: they are checked in order. -/ +theorem forM_append_ok {Claim : Type} (check : Claim → Except AuditError Unit) + (first second : List Claim) (holdsFirst : ok (first.forM check) = true) + (holdsSecond : ok (second.forM check) = true) : + ok ((first ++ second).forM check) = true := by + induction first with + | nil => simpa using holdsSecond + | cons claim rest ih => + simp only [List.cons_append, List.forM] at holdsFirst ⊢ + cases checked : check claim with + | error _ => simp [checked, bind, Except.bind, ok] at holdsFirst + | ok _ => + simp only [checked, bind, Except.bind] at holdsFirst ⊢ + exact ih holdsFirst + +#print axioms forM_append_ok + +/-- Two lists of claims that each hold in the pinned images hold together. -/ +theorem claimsHold_append {Claim : Type} + {check : List PreparedImage → Claim → Except AuditError Unit} {images : List PinnedImage} + {first second : List Claim} + (holdsFirst : ClaimsHold check images first) (holdsSecond : ClaimsHold check images second) : + ClaimsHold check images (first ++ second) := by + unfold ClaimsHold checkAll at * + cases decoded : prepare images with + | error _ => simp [decoded, bind, Except.bind, ok] at holdsFirst + | ok prepared => + simp only [decoded, bind, Except.bind] at holdsFirst holdsSecond ⊢ + exact forM_append_ok (check prepared) first second holdsFirst holdsSecond + +#print axioms claimsHold_append + +/-- When the images decode and spell distinct identities, the empty list of claims holds. -/ +theorem claimsHold_nil {Claim : Type} + {check : List PreparedImage → Claim → Except AuditError Unit} {images : List PinnedImage} + (prepared : ok (prepare images) = true) : + ClaimsHold check images [] := by + unfold ClaimsHold checkAll + cases decoded : prepare images with + | error _ => simp [decoded, ok] at prepared + | ok _ => simp [bind, Except.bind, ok, List.forM, pure, Except.pure] + +#print axioms claimsHold_nil + +/-- The claims of page `index` when pages hold `size` claims each. -/ +def claimPage {Claim : Type} (claims : List Claim) (size index : Nat) : List Claim := + (claims.drop (index * size)).take size + +/-- The pages of claims before an index each hold. A page is one kernel evaluation, so the + memory of a page grows with its `size` claims and not with the number of pages. -/ +inductive PagesHold {Claim : Type} (holds : List Claim → Prop) (claims : List Claim) + (size : Nat) : Nat → Prop where + | zero : PagesHold holds claims size 0 + | succ {index : Nat} : holds (claimPage claims size index) → + PagesHold holds claims size index → PagesHold holds claims size (index + 1) + +/-- The first `count` pages of claims hold together as the claims they cover. -/ +theorem claimsHold_of_first_pages {Claim : Type} + {check : List PreparedImage → Claim → Except AuditError Unit} {images : List PinnedImage} + {claims : List Claim} {size : Nat} (prepared : ok (prepare images) = true) : + ∀ count, PagesHold (ClaimsHold check images) claims size count → + ClaimsHold check images (claims.take (count * size)) := by + intro count pages + induction pages with + | zero => simpa using claimsHold_nil prepared + | @succ index page _ ih => + have split : claims.take ((index + 1) * size) = + claims.take (index * size) ++ claimPage claims size index := by + rw [Nat.succ_mul, List.take_add]; rfl + rw [split] + exact claimsHold_append ih page + +#print axioms claimsHold_of_first_pages + +/-- Pages that cover every claim give all claims, each once and in order. -/ +theorem claimsHold_of_pages {Claim : Type} + {check : List PreparedImage → Claim → Except AuditError Unit} {images : List PinnedImage} + {claims : List Claim} {size count : Nat} (prepared : ok (prepare images) = true) + (covers : claims.length ≤ count * size) + (pages : PagesHold (ClaimsHold check images) claims size count) : + ClaimsHold check images claims := by + have whole := claimsHold_of_first_pages (claims := claims) prepared count pages + rwa [List.take_of_length_le covers] at whole + +#print axioms claimsHold_of_pages + +/-- The claimed body is in the image pinned under the claim's identity, at the place that + image's own MethodDef row gives. -/ +def bodyClaimBound (prepared : List PreparedImage) (claim : BodyClaim) : + Except AuditError Unit := do + bodyBound (← located prepared claim.identity) claim.token claim.bytes + +end StrictBinding diff --git a/StrictBinding/CarriedEnums.lean b/StrictBinding/CarriedEnums.lean new file mode 100644 index 000000000..a4184122f --- /dev/null +++ b/StrictBinding/CarriedEnums.lean @@ -0,0 +1,72 @@ +import StrictBinding.EnumEvidence +import CIL.Extraction.BodyDecoding + +/-! Enum storage claims of TypeDef rows in the method's own and referenced slices. -/ + +namespace StrictBinding + +open CIL CIL.Extraction + +/-- The row and typed enum declaration that a carried definition resolves to. -/ +structure CarriedEnum where + row : Nat + entry : PlatformDefinition + deriving DecidableEq, Repr + +/-- Read from the exact slice and TypeDef token the default decoder uses. -/ +def carriedEnum? (env : MetadataEnv) (position token : Nat) : Option CarriedEnum := do + guard (metadataTableOf token == Table.typeDef) + let named ← (typeDefNamed env position resolveFuel token).toOption + let .enumType code := named.kind | none + return ⟨tokenRow token, ⟨named.scope, named.parts, .enumType code named.platformArity⟩⟩ + +/-- Enumerate every carried row from the checked indexes, once per slice. -/ +def carriedEnums (env : MetadataEnv) : List CarriedEnum := + env.slices.toList.flatMap fun (position, index) => + index.rows.toList.filterMap fun (token, _) => carriedEnum? env position token + +/-- A successful lookup of a row in a carried slice places its enum in this audit. -/ +theorem carriedEnum_mem {env : MetadataEnv} {position token : Nat} {index : MetadataIndex} + {bytes : List Nat} {entry : CarriedEnum} (slice : env.slices.find? position = some index) + (row : index.rows.find? token = some bytes) (enum : carriedEnum? env position token = some entry) : + entry ∈ carriedEnums env := by + unfold carriedEnums + apply List.mem_flatMap.mpr + refine ⟨(position, index), SearchTree.find?_mem_toList _ _ _ slice, ?_⟩ + exact List.mem_filterMap.mpr ⟨(token, bytes), SearchTree.find?_mem_toList _ _ _ row, enum⟩ +#print axioms carriedEnum_mem + +/-- Every enum resolved by the decoder at a carried TypeDef is selected from the same environment. -/ +theorem carriedEnums_of_typeDefNamed {env : MetadataEnv} {position fuel token code : Nat} + {named : NamedType} (budget : fuel ≤ resolveFuel) + (lookup : typeDefNamed env position fuel token = .ok named) + (kind : named.kind = .enumType code) : + ⟨tokenRow token, ⟨named.scope, named.parts, .enumType code named.platformArity⟩⟩ ∈ carriedEnums env := by + have resolved := typeDefNamed_of_le budget lookup + have original := resolved + unfold typeDefNamed at resolved + obtain ⟨index, located, later⟩ := CIL.Safety.except_bind_ok resolved + obtain ⟨row, decoded, _⟩ := CIL.Safety.except_bind_ok later + obtain ⟨typed, bytes, fromRow⟩ := MetadataIndex.typeDef_bytes decoded + unfold MetadataEnv.slice at located + cases fromSlice : env.slices.find? position with + | none => simp [fromSlice] at located + | some found => + simp only [fromSlice, Except.ok.injEq] at located + subst found + apply carriedEnum_mem fromSlice fromRow + simp [carriedEnum?, original, typed, guard, pure, Option.bind, Except.toOption, kind] +#print axioms carriedEnums_of_typeDefNamed + +/-- Check both the physical TypeDef token and its field code. A missing assembly leaves the + claim uncovered. An incorrect declaration in a supplied assembly fails. -/ +def carriedEnumBound (images : List PreparedImage) (claim : CarriedEnum) : Except AuditError Bool := do + let some image := images.find? (fun image => bytesOf image.identity == claim.entry.scope) + | return false + let .enumType code _ := claim.entry.kind | throw (.typeDefMismatch claim.row .kind) + unless (← enumDefinition? image 0 claim.entry.parts) == some claim.row do + throw (.typeDefMismatch claim.row .typeName) + unless (← physicalEnumCode image claim.row) == code do throw (.typeDefMismatch claim.row .fields) + return true + +end StrictBinding diff --git a/StrictBinding/ContractSidecar.lean b/StrictBinding/ContractSidecar.lean new file mode 100644 index 000000000..c612a1bca --- /dev/null +++ b/StrictBinding/ContractSidecar.lean @@ -0,0 +1,113 @@ +import StrictBinding.Bind + +/-! Compact JSON fields read by the strict binding audit from a pinned contracts sidecar. -/ + +namespace StrictBinding.ContractSidecar +open StrictBinding + +/-- Compact JSON emitted by the default contracts writer. Floating-point numbers, surrogate + pairs, non-ASCII wire bytes and insignificant whitespace are outside this checked subset. -/ +inductive Node where + | object (fields : List (String × Node)) + | array (items : List Node) + | string (text : String) + | integer (value : Int) + | boolean (value : Bool) + | null + +private def hex (c : Nat) : Option Nat := + if 48 ≤ c && c ≤ 57 then some (c-48) + else if 65 ≤ c && c ≤ 70 then some (c-55) + else if 97 ≤ c && c ≤ 102 then some (c-87) + else none + +private def quoted (s : List Nat) (acc : List Char) : Option (String × List Nat) := + match s with + | [] => none + | 34 :: rest => some (String.ofList acc.reverse, rest) + | 92 :: 117 :: a :: b :: c :: d :: rest => do + let code := 4096 * (← hex a) + 256 * (← hex b) + 16 * (← hex c) + (← hex d) + guard (code < 0xd800 || code > 0xdfff) + quoted rest (Char.ofNat code :: acc) + | 92 :: c :: rest => do + let code ← ([(34,34),(92,92),(47,47),(98,8),(102,12),(110,10),(114,13),(116,9)] + : List (Nat × Nat)).lookup c + quoted rest (Char.ofNat code :: acc) + | c :: rest => do + guard (32 ≤ c && c < 128 && c != 92) + quoted rest (Char.ofNat c :: acc) + +private def digit (c : Nat) := 48 ≤ c && c ≤ 57 + +private def number (s : List Nat) : Option (Node × List Nat) := do + let neg := s.head? == some 45 + let input := if neg then s.drop 1 else s + let digits := input.takeWhile digit + guard (!digits.isEmpty && (digits.head? != some 48 || digits.length == 1)) + let magnitude : Nat := digits.foldl (fun (acc : Nat) d => 10*acc + (d-48)) 0 + let n : Int := magnitude + return (.integer (if neg then -n else n), input.drop digits.length) + +mutual + private def value : Nat → List Nat → Option (Node × List Nat) + | 0, _ => none + | k+1, bytes => + match bytes with + | 123 :: 125 :: rest => some (.object [],rest) + | 123 :: rest => members k rest [] + | 91 :: 93 :: rest => some (.array [],rest) + | 91 :: rest => elements k rest [] + | 34 :: rest => do + let (decoded,tail) ← quoted rest [] + return (.string decoded,tail) + | 116 :: 114 :: 117 :: 101 :: rest => some (.boolean true,rest) + | 102 :: 97 :: 108 :: 115 :: 101 :: rest => some (.boolean false,rest) + | 110 :: 117 :: 108 :: 108 :: rest => some (.null,rest) + | _ => number bytes + + private def members : Nat → List Nat → List (String × Node) → Option (Node × List Nat) + | k+1, 34 :: bytes, acc => do + let (name, 58 :: tail) ← quoted bytes [] | none + guard (!(acc.any fun kv => kv.1 == name)) + let (node,rest) ← value k tail + let all := (name,node) :: acc + match rest with + | 44 :: next => members k next all + | 125 :: next => some (.object all.reverse,next) + | _ => none + | _, _, _ => none + + private def elements : Nat → List Nat → List Node → Option (Node × List Nat) + | k+1, bytes, acc => do + let (node,rest) ← value k bytes + match rest with + | 44 :: next => elements k next (node :: acc) + | 93 :: next => some (.array (node :: acc).reverse,next) + | _ => none + | _, _, _ => none +end + +/-- Parse exactly one complete compact document from its pinned bytes. -/ +def read (image : ImageBytes) : Option Node := do + let view ← (ImageView.ofImage image).toOption + let bytes ← (view.bytes 0 image.length).toOption + let (node,[]) ← value (bytes.length+1) bytes | none + return node + +/-- Select a field of an object with unique member names. -/ +def field (obj : Node) (key : String) : Option Node := do + let .object fields := obj | none + fields.lookup key + +/-- Read a string field, preserving its decoded Unicode contents. -/ +def text (obj : Node) (key : String) : Option String := do + let .string result ← field obj key | none + return result + +/-- Require a unique export with the complete selected identity. -/ +def member (root : Node) (identity : String) : Option Node := do + let .array exports ← field root "exports" | none + let [entry] := exports.filter (fun entry => text entry "identity" == some identity) | none + return entry + +end StrictBinding.ContractSidecar diff --git a/StrictBinding/DefinitionRoute.lean b/StrictBinding/DefinitionRoute.lean new file mode 100644 index 000000000..4c8b9571a --- /dev/null +++ b/StrictBinding/DefinitionRoute.lean @@ -0,0 +1,115 @@ +import StrictBinding.PlatformEnums + +/-! Follow a qualified type reference through the supplied defining and forwarding assemblies. -/ + +namespace StrictBinding + +open CIL.Extraction + +/-- A matching top-level ExportedType must be a unique forwarder to an AssemblyRef. Nested + exports have an ExportedType as their implementation and belong to their enclosing export. -/ +private def forwardedScope? (image : PreparedImage) (space name : List Nat) : + Except AuditError (Option (List Nat)) := do + let exportedType := 0x27 + let cell := image.layout.cellOfRow image.view exportedType + let mut target := none + for i in [:image.layout.rowCount exportedType] do + let row := i + 1 + unless ← stringFound image (← cell row 2) (name ++ [0]) do continue + unless ← stringFound image (← cell row 3) (space ++ [0]) do continue + let implementation ← cell row 4 + if implementation % 4 == 2 then continue + if target.isSome || implementation % 4 != 1 || implementation / 4 == 0 || + (← cell row 0) &&& 0x00200000 == 0 then throw (.typeDefMismatch row .typeName) + let token ← checkedTokenOf Table.assemblyRef (implementation / 4) .rowOverflow + let identity ← assemblyIdentityAt image.view image.layout token 0 + target := some (bytesOf (String.ofList (identity.map Char.ofNat))) + return target + +inductive DefinitionRoute where + | defined (row : Nat) + | forwarded (scope : List Nat) + +/-- Resolve the outermost name in the current image. A local definition owns its nested types, + and a forwarder carries the whole enclosing name chain to the referenced assembly. -/ +def definitionRoute (image : PreparedImage) (parts : List (List Nat)) : + Except AuditError DefinitionRoute := do + let space :: name :: nested := parts | throw (.typeDefMismatch 0 .typeName) + let outer ← enumDefinition? image 0 [space, name] + let forward ← forwardedScope? image space name + match outer, forward with + | some row, none => + if nested.isEmpty then return .defined row + let some child ← enumDefinition? image row nested | throw (.typeDefMismatch row .typeName) + return .defined child + | none, some target => return .forwarded target + | _, _ => throw (.typeDefMismatch 0 .typeName) + +/-- The physical definition, or an uncovered assembly when a scope on the path is missing. -/ +def findPinnedDefinition (images : List PreparedImage) (parts : List (List Nat)) + (scope : List Nat) : Nat → Except AuditError (Option (PreparedImage × Nat)) + | 0 => throw (.typeDefMismatch 0 .typeName) + | fuel + 1 => do + let some image := images.find? (fun image => bytesOf image.identity == scope) | return none + match ← definitionRoute image parts with + | .defined row => return some (image, row) + | .forwarded target => findPinnedDefinition images parts target fuel + +/-- Each step uses the assembly named by the reference or preceding physical forwarder. -/ +inductive PinnedDefinitionAt (images : List PreparedImage) (parts : List (List Nat)) : + List Nat → PreparedImage → Nat → Prop where + | here {scope image row} + (selected : images.find? (fun candidate => bytesOf candidate.identity == scope) = some image) + (defined : definitionRoute image parts = .ok (.defined row)) : + PinnedDefinitionAt images parts scope image row + | forward {scope image next target row} + (selected : images.find? (fun candidate => bytesOf candidate.identity == scope) = some image) + (forwarded : definitionRoute image parts = .ok (.forwarded next)) + (found : PinnedDefinitionAt images parts next target row) : + PinnedDefinitionAt images parts scope target row + +/-- A successful lookup retains the chain of physical references to the defining TypeDef. -/ +theorem pinnedDefinitionAt_of_find {images : List PreparedImage} {parts : List (List Nat)} + {scope : List Nat} {fuel : Nat} {image : PreparedImage} {row : Nat} + (found : findPinnedDefinition images parts scope fuel = .ok (some (image, row))) : + PinnedDefinitionAt images parts scope image row := by + induction fuel generalizing scope with + | zero => cases found + | succ fuel ih => + simp only [findPinnedDefinition, bind, Except.bind] at found + cases selected : images.find? (fun candidate => bytesOf candidate.identity == scope) with + | none => simp [selected, pure, Except.pure] at found + | some candidate => + simp only [selected] at found + cases route : definitionRoute candidate parts with + | error fault => simp [route] at found + | ok routeValue => + cases routeValue with + | defined position => + simp [route, pure, Except.pure] at found + obtain ⟨rfl, rfl⟩ := found + exact .here selected route + | forwarded next => + exact .forward selected route (ih (by simpa [route] using found)) +#print axioms pinnedDefinitionAt_of_find + +/-- A successful traversal contains the result of every selected input. -/ +theorem read_member {α β : Type} {read : α → Except AuditError β} + {inputs : List α} {outputs : List β} {item : α} (member : item ∈ inputs) + (checked : inputs.mapM read = .ok outputs) : + ∃ result ∈ outputs, read item = .ok result := by + induction inputs generalizing outputs with + | nil => cases member + | cons first rest ih => + rw [List.mapM_cons] at checked + obtain ⟨head, firstRead, checked⟩ := CIL.Safety.except_bind_ok checked + obtain ⟨tail, restRead, joined⟩ := CIL.Safety.except_bind_ok checked + have same : head :: tail = outputs := Except.ok.inj joined + subst outputs + rcases List.mem_cons.mp member with rfl | later + · exact ⟨head, by simp, firstRead⟩ + · obtain ⟨result, found, read⟩ := ih later restRead + exact ⟨result, List.mem_cons_of_mem _ found, read⟩ +#print axioms read_member + +end StrictBinding diff --git a/StrictBinding/DispatchCatalogs.lean b/StrictBinding/DispatchCatalogs.lean new file mode 100644 index 000000000..fee235d2d --- /dev/null +++ b/StrictBinding/DispatchCatalogs.lean @@ -0,0 +1,317 @@ +import StrictBinding.GenericConstraints + +/-! Check the complete method catalogs of local, nongeneric virtual class calls in one image. -/ + +namespace StrictBinding.ClassDispatch +open CIL CIL.Extraction + +/-- Unsupported metadata leaves coverage partial. A conflicting supported claim is an error. -/ +private abbrev Reading := OptionT (Except AuditError) + +/-- Structural PE errors retain the token, heap offset or column that failed. -/ +private def physical (action : Except AuditError α) : Reading α := liftM action + +/-- The name of a unique, top-level nongeneric declaration of the required CLI kind. -/ +private def localName (image : PreparedImage) (row : Nat) (kind : TypeKind) : Reading String := do + let cell := image.layout.cellOfRow image.view Table.typeDef row + let name ← physical (image.layout.stringAt image.view (← physical (cell 1))) + let space ← physical (image.layout.stringAt image.view (← physical (cell 2))) + unless (← physical (enumDefinition? image 0 [space, name])) == some row do failure + unless (← physical (physicalTypeShape image row)) == (kind, 0) do failure + let some simple := spelled? name | failure + let some scope := spelled? space | failure + guard (ordinaryTypeName scope simple) + return if scope.isEmpty then simple else scope ++ "." ++ simple + +/-- A physical class identity used by the slot or its signatures. -/ +private def className (image : PreparedImage) (row : Nat) : Reading TypeExpr := do + return .object image.identity (← localName image row .classType) + +/-- Follow a local TypeDef edge. The core Object, ValueType and Enum roots cannot descend + from a local class. This lets a class hierarchy coexist with unrelated structs and enums. -/ +private def parentRow (image : PreparedImage) (row : Nat) : Reading Nat := do + let declaration := image.layout.cellOfRow image.view Table.typeDef row + let coded ← physical (declaration typeDefExtendsColumn) + if coded == 0 then + unless row == 1 do + guard ((← physical (declaration typeDefFlagsColumn)) &&& 0x20 != 0) + return 0 + if coded % 4 == 0 then + let token ← physical (checkedTokenOf Table.typeDef (coded / 4) .rowOverflow) + let _ ← physical (image.layout.rowAt token) + return coded / 4 + guard (coded % 4 == 1) + let cell := image.layout.cellOfRow image.view Table.typeRef (coded / 4) + let scope ← physical (cell 0) + guard (scope % 4 == 2) + let reference ← physical (checkedTokenOf Table.assemblyRef (scope / 4) .rowOverflow) + guard (← physical (coreAssemblyAt image reference 0)) + guard ((← physical (image.layout.stringAt image.view (← physical (cell 2)))) == systemNamespace) + let name ← physical (image.layout.stringAt image.view (← physical (cell 1))) + guard ([objectName, valueTypeName, enumName].contains name) + return 0 + +/-- Check the entire parent chain even when the selected root is an ancestor in a cycle. -/ +private def descends (parents : Array Nat) (root : Nat) : Nat → Nat → Bool → Reading Bool + | _, 0, found => return found + | 0, row, _ => throwThe AuditError (.typeDefMismatch row .base) + | fuel+1, row, found => do + let some parent := parents[row]? | throwThe AuditError (.typeDefMismatch row .base) + descends parents root fuel parent (found || row == root) + +/-- Primitive signatures and references to other supported local classes. -/ +private def formalType (image : PreparedImage) : SigType → Reading TypeExpr + | .primitive 1 => return .void + | .primitive code => do + let some name := (primitiveName code).bind spelled? | failure + return .atom runtimeScope name + | .named false token => do + guard (tableOf token == Table.typeDef) + className image (token % 2^24) + | _ => failure + +/-- A nongeneric managed MethodDef, with its physical flags, signature, and body presence. -/ +private def classMethod (image : PreparedImage) (row : Nat) : Reading DispatchMethod := do + let cell := image.layout.cellOfRow image.view Table.methodDef row + let implementation ← physical (cell 1) + let attributes ← physical (cell 2) + let some name := spelled? (← physical (image.layout.stringAt image.view (← physical (cell 3)))) + | failure + let blob ← physical (image.layout.blobAt image.view (← physical (cell 4))) + let .ok sig := decodeMethodSig blob | failure + guard (sig.genericArity == 0 && (sig.convention == 0 || sig.convention == 32)) + guard (implementation == 0) + let token := StrictBinding.tokenOf Table.methodDef row + let _ ← physical (parameterDeclarations image token []) + let isAbstract := attributes &&& 0x400 != 0 + let hasBody := (← physical (cell 0)) != 0 + let hasThis := sig.convention == 32 + guard (hasBody == !isAbstract && hasThis == (attributes &&& 0x10 == 0)) + return { + token, name, hasThis, isPublic := attributes % 8 == 6, + isAbstract, isVirtual := attributes &&& virtualMethodAttribute != 0, hasBody, genericArity := 0, + explicitThis := false, callingConvention := 0, attributes, implementation, + returnType := ← formalType image sig.result, + parameters := ← sig.parameters.mapM (formalType image), + overrides := [] } + +/-- InterfaceImpl targets grouped by their physical TypeDef owner, in declaration order. -/ +private def interfacesByOwner (image : PreparedImage) (count : Nat) : + Reading (Array (Array Nat)) := do + let mut owners := Array.replicate (count + 1) #[] + let interfaceImpl := 0x09 + for i in [:image.layout.rowCount interfaceImpl] do + let cell := image.layout.cellOfRow image.view interfaceImpl (i+1) + let owner ← physical (cell 0) + guard (owner > 0 && owner ≤ count) + let target ← physical (cell 1) + owners := owners.modify owner (·.push target) + return owners + +/-- A local interface implemented by a class in the selected hierarchy. -/ +private def interfaceName (image : PreparedImage) (coded : Nat) : Reading TypeExpr := do + guard (coded % 4 == 0 && coded / 4 > 0) + let name ← localName image (coded / 4) .interface + return .atom image.identity name + +/-- Local MethodImpl rows bind the implementing MethodDef to its class and the overridden + MethodDef to its interface. Both signatures describe the same nongeneric instance slot. -/ +private def localOverrides (image : PreparedImage) (interfaces : Array (Array Nat)) + (parents : Array Nat) (root : Nat) : + Reading (Array (List (Nat × TypeExpr))) := do + let rowCount := image.layout.rowCount 0x19 + if rowCount == 0 then return #[] + let methods := image.layout.rowCount Table.methodDef + let mut bindings := Array.replicate (methods + 1) [] + for i in [:rowCount] do + let cell := image.layout.cellOfRow image.view 0x19 (i+1) + let owner ← physical (cell 0) + let body ← physical (cell 1) + let slot ← physical (cell 2) + guard (body % 2 == 0) + let bodyToken ← physical (checkedTokenOf Table.methodDef (body / 2) .rowOverflow) + let classToken ← physical (checkedTokenOf Table.typeDef owner .rowOverflow) + unless (← physical (methodDeclaringType image bodyToken)) == classToken do + throwThe AuditError (.dispatchMismatch bodyToken) + -- An unrelated class can implement an external interface. Its slots do not enter this catalog. + unless ← descends parents root (image.layout.rowCount Table.typeDef) owner false do continue + guard (slot % 2 == 0) + let slotToken ← physical (checkedTokenOf Table.methodDef (slot / 2) .rowOverflow) + let interfaceToken ← physical (methodDeclaringType image slotToken) + let some direct := interfaces[owner]? | failure + guard (direct.contains ((interfaceToken % 2^24) * 4)) + let name ← localName image (interfaceToken % 2^24) .interface + let slotOwner := TypeExpr.atom image.identity name + let implementation ← classMethod image (body / 2) + let declaration ← classMethod image (slot / 2) + guard (implementation.hasThis && implementation.hasBody && implementation.isVirtual && + declaration.hasThis && declaration.isVirtual && declaration.isAbstract && + implementation.returnType == declaration.returnType && + implementation.parameters == declaration.parameters) + let binding := (slotToken, slotOwner) + guard (!bindings[body / 2]!.contains binding) + bindings := bindings.modify (body / 2) (binding :: ·) + return bindings.map List.reverse + +/-- Enumerate every physical descendant, including abstract intermediates, and every MethodDef + each one owns, including explicit implementations of direct local interface slots. -/ +def physicalHierarchy (image : PreparedImage) (owner : Nat) : + OptionT (Except AuditError) (List DispatchType) := do + guard (image.layout.rowCount 5 == 0) + -- A File row with metadata introduces another module whose classes this image cannot enumerate. + for i in [:image.layout.rowCount 0x26] do + guard ((← physical (image.layout.cellOfRow image.view 0x26 (i+1) 0)) == 1) + let count := image.layout.rowCount Table.typeDef + let interfaces ← interfacesByOwner image count + let mut parents := #[0] + for i in [:count] do parents := parents.push (← parentRow image (i+1)) + let overrides ← localOverrides image interfaces parents owner + let mut types := #[] + for i in [:count] do + let row := i+1 + unless ← descends parents owner count row false do continue + let cell := image.layout.cellOfRow image.view Table.typeDef row + let flags ← physical (cell 0) + let first ← physical (cell 5) + let finish ← if row == count then pure (image.layout.rowCount Table.methodDef + 1) + else physical (image.layout.cellOfRow image.view Table.typeDef (row+1) 5) + let mut methods := #[] + for index in [first:finish] do + let method ← classMethod image index + methods := methods.push { method with overrides := overrides[index]?.getD [] } + let base ← if parents[row]! == 0 then pure TypeExpr.void else className image parents[row]! + let owned ← interfaces[row]!.toList.mapM (interfaceName image) + guard owned.Nodup + types := types.push { + identity := ← className image row, valueType := false, interfaceType := false, + assembly := {identity := image.identity}, base, abstractType := flags &&& 0x80 != 0, + interfaces := owned, methods := methods.toList } + return types.toList + +/-- The union of all physical class hierarchies selected by a method's original callvirt sites. + Live sites must dispatch through the exact target table validated against that hierarchy. -/ +private def selected (image : PreparedImage) (item : MethodEvidence) + (methods : List SpecializedMethod) : Reading (List DispatchType) := do + guard (item.assembly.identity == image.identity) + let mut types : List DispatchType := [] + for (instruction, pc) in item.original.code.zipIdx do + if [0xfe16, 0x74, 0x75].contains instruction.opcode then failure + unless instruction.opcode == 0x6f do continue + let .call site := instruction.operand + | throwThe AuditError (.dispatchMismatch item.original.token) + let .managed token := site.target | failure + guard (tableOf token == Table.methodDef && image.layout.rowCount 5 == 0) + let attr ← physical (image.layout.cell image.view token 2) + unless attr &&& virtualMethodAttribute != 0 do continue + let owner := (← physical (methodDeclaringType image token)) % 2^24 + let slot ← classMethod image (token % 2^24) + unless site.declaringType == (← className image owner) && slot.name == site.name && + slot.hasThis && slot.returnType == site.returnType && slot.parameters == site.parameterTypes + do throwThe AuditError (.dispatchMismatch token) + let entries ← physicalHierarchy image owner + if item.reachable[pc]? == some true then + let some (_, .virtual targets) := item.links.find? (·.1 == pc) + | throwThe AuditError (.dispatchMismatch token) + unless virtualTableValid entries (findDispatchType entries) (fun index => methods[index]?) + site targets site.argumentCount do throwThe AuditError (.dispatchMismatch token) + types := types ++ entries.filter (fun entry => !types.contains entry) + return types + +/-- A physical catalog contains all claimed declarations with the same multiplicities. + The order of declarations in the metadata can differ from the extractor's name order. -/ +def CatalogBound (image : PreparedImage) (item : MethodEvidence) + (methods : List SpecializedMethod) : Prop := + ∃ actual, (selected image item methods).run = .ok (some actual) ∧ actual.Perm item.dispatchTypes + +/-- Whether this method is covered and whether it selects any virtual class hierarchy. -/ +private def checkItem (image : PreparedImage) (methods : List SpecializedMethod) + (item : MethodEvidence) : Except AuditError (Bool × Bool) := do + let some actual ← (selected image item methods).run | return (false, false) + if actual.Perm item.dispatchTypes then return (true, !actual.isEmpty) + throw (.dispatchMismatch item.original.token) + +/-- Every covered method has exactly the physically reconstructed declarations. -/ +private theorem bound_of_checkItem {image methods item any} + (checked : checkItem image methods item = .ok (true, any)) : + CatalogBound image item methods := by + unfold checkItem at checked + obtain ⟨value, physical, rest⟩ := CIL.Safety.except_bind_ok checked + cases value with + | none => cases rest + | some actual => + refine ⟨actual, physical, ?_⟩ + by_cases same : actual.Perm item.dispatchTypes + · exact same + · simp [same, throw] at rest +#print axioms bound_of_checkItem + +/-- Every supported method selects its complete physical class catalog. -/ +private def checkMethods (image : PreparedImage) (items : List MethodEvidence) + (imports : List ImportSlot) : Except AuditError Bool := do + let coverage ← items.mapM (checkItem image (specializationTable items)) + return imports.isEmpty && coverage.all Prod.fst && coverage.any Prod.snd + +/-- Every individual method was checked against the same physical image and program. -/ +theorem bound_of_checkMethods {image items imports} + (checked : checkMethods image items imports = .ok true) : + ∀ item ∈ items, CatalogBound image item (specializationTable items) := by + unfold checkMethods at checked + obtain ⟨coverage, read, yes⟩ := CIL.Safety.except_bind_ok checked + have all := (Bool.and_eq_true_iff.mp (Bool.and_eq_true_iff.mp (Except.ok.inj yes)).1).2 + intro item mem + obtain ⟨⟨full, any⟩, member, result⟩ := read_member mem read + have covered : full = true := List.all_eq_true.mp all (full, any) member + subst full + exact bound_of_checkItem result +#print axioms bound_of_checkMethods + +/-- The physical catalog of each supported method agrees in both directions with its evidence. + Empty selections and incomplete reference, import, cast or constrained coverage stay partial. -/ +def check (images : List PinnedImage) (pins : List AssemblyPin) (items : List MethodEvidence) + (imports : List ImportSlot) : Except AuditError Bool := do + let [pin] := pins | return false + let [supplied] := images | return false + unless supplied.identity == pin.identity do return false + let image ← prepareOne supplied + checkMethods image items imports + +/-- Complete image coverage entails a physical catalog for every method of the extraction. -/ +theorem bound_of_check {images pins items imports} + (checked : check images pins items imports = .ok true) : + ∃ supplied image, images = [supplied] ∧ prepareOne supplied = .ok image ∧ + ∀ item ∈ items, CatalogBound image item (specializationTable items) := by + unfold check at checked + split at checked <;> try cases checked + split at checked <;> try cases checked + split at checked <;> try cases checked + obtain ⟨image, prepared, complete⟩ := CIL.Safety.except_bind_ok checked + exact ⟨_, image, rfl, prepared, bound_of_checkMethods complete⟩ +#print axioms bound_of_check + +end StrictBinding.ClassDispatch + +namespace StrictBinding +open CIL CIL.Extraction + +/-- The consumer replays the dispatch coverage bit on the assemblies, methods and imports + of the same certified extraction. -/ +def DispatchCatalogsChecked (images : List PinnedImage) (pins : List AssemblyPin) + (items : List MethodEvidence) (imports : List ImportSlot) (complete : Bool) : Prop := + (ClassDispatch.check images pins items imports).toOption = some complete + +instance (images : List PinnedImage) (pins : List AssemblyPin) (items : List MethodEvidence) + (imports : List ImportSlot) (complete : Bool) : + Decidable (DispatchCatalogsChecked images pins items imports complete) := + inferInstanceAs (Decidable (_ = _)) + +/-- Each claimed method catalog is a permutation of its reconstructed physical declarations. -/ +theorem DispatchCatalogsChecked.physical {images pins items imports} + (checked : DispatchCatalogsChecked images pins items imports true) : + ∃ supplied image, images = [supplied] ∧ prepareOne supplied = .ok image ∧ + ∀ item ∈ items, ClassDispatch.CatalogBound image item (specializationTable items) := by + apply ClassDispatch.bound_of_check (pins := pins) (imports := imports) + cases result : ClassDispatch.check images pins items imports with + | error err => simp [DispatchCatalogsChecked, result, Except.toOption] at checked + | ok value => simpa [DispatchCatalogsChecked, result, Except.toOption] using checked +#print axioms DispatchCatalogsChecked.physical +end StrictBinding diff --git a/StrictBinding/Dump.lean b/StrictBinding/Dump.lean new file mode 100644 index 000000000..43d46f65f --- /dev/null +++ b/StrictBinding/Dump.lean @@ -0,0 +1,74 @@ +import StrictBinding.Identity + +/-! +# Image facts + +What the audit decodes from an image, as data: the sections, the metadata root, the streams, the +position and size of every table, the file offset of each MethodDef row's body and the assembly +identity. `facts` lists them in a fixed order and `Fact.line` prints one per line, so an +independent reader of the same file can be compared with the audit's own decoding. +-/ + +namespace StrictBinding + +/-- One decoded fact about an image. Offsets are file offsets. -/ +inductive Fact where + /-- A section header: mapped size, relative virtual address, raw size and raw data offset. -/ + | sectionHeader (virtualSize virtualAddress rawSize rawPointer : Nat) + /-- The metadata root: its offset and size. -/ + | metadata (offset size : Nat) + /-- A nonempty stream: its name, offset and size. -/ + | stream (name : String) (offset size : Nat) + /-- The heap size flags of the table stream. -/ + | heapSizes (flags : Nat) + /-- A nonempty table: its number, row count, row size and the offset of its first row. -/ + | table (table count size offset : Nat) + /-- A MethodDef row with a body: its row number, relative virtual address and file offset. -/ + | method (row rva offset : Nat) + /-- The assembly identity, as code points. -/ + | identity (codePoints : List Nat) + deriving DecidableEq, Repr + +/-- The line that describes a fact: its kind, then its numbers, or the identity as text. -/ +def Fact.line : Fact → String + | .sectionHeader size address rawSize rawPointer => + s!"section {size} {address} {rawSize} {rawPointer}" + | .metadata offset size => s!"metadata {offset} {size}" + | .stream name offset size => s!"stream {name} {offset} {size}" + | .heapSizes flags => s!"heapSizes {flags}" + | .table number count size offset => s!"table {number} {count} {size} {offset}" + | .method row rva offset => s!"method {row} {rva} {offset}" + | .identity points => s!"identity {String.ofList (points.map Char.ofNat)}" + +/-- The fact for a stream the file has; none for an absent one. -/ +def streamFacts (name : String) (stream : Stream) : List Fact := + if stream.size == 0 then [] else [.stream name stream.start stream.size] + +/-- The file offset of every MethodDef row that has a body, from its own relative virtual + address. -/ +def methodFacts (view : ImageView) (layout : MetadataLayout) : Except AuditError (List Fact) := + (List.range (layout.rowCount 0x06)).filterMapM fun index => do + let rva ← layout.cellOfRow view 0x06 (index + 1) 0 + if rva == 0 then pure none + else pure (some (.method (index + 1) rva (← mapRva layout.pe.sections rva 1))) + +/-- Everything the audit decodes from an image's container and tables. -/ +def facts (image : ImageBytes) : Except AuditError (List Fact) := do + let view ← ImageView.ofImage image + let layout ← decodeMetadata view + let pe := layout.pe + let sections := pe.sections.map fun header => + .sectionHeader header.virtualSize header.virtualAddress header.rawSize header.rawPointer + let streams := streamFacts "tables" pe.tables ++ streamFacts "strings" pe.strings ++ + streamFacts "userStrings" pe.userStrings ++ streamFacts "guids" pe.guids ++ + streamFacts "blobs" pe.blobs + let tables := layout.tables.map fun rows => .table rows.table rows.count rows.size rows.start + pure (sections ++ [.metadata pe.metadata pe.metadataSize] ++ streams ++ + [.heapSizes layout.heapSizes] ++ tables ++ (← methodFacts view layout) ++ + [.identity (← assemblyIdentity view layout)]) + +/-- The lines that describe an image, or the reason it does not decode. -/ +def describe (image : ImageBytes) : Except AuditError (List String) := + (facts image).map (·.map Fact.line) + +end StrictBinding diff --git a/StrictBinding/EnumEvidence.lean b/StrictBinding/EnumEvidence.lean new file mode 100644 index 000000000..72b5a7690 --- /dev/null +++ b/StrictBinding/EnumEvidence.lean @@ -0,0 +1,76 @@ +import StrictBinding.DefinitionRoute +import CIL.Extraction.MetadataResolution + +/-! Bind the storage of an admitted enum to the selected physical definition. -/ + +namespace StrictBinding + +open CIL CIL.Extraction + +/-- Missing scope images leave their enum entries uncovered. A bad supplied definition fails. -/ +def coveredEnumEntry (images : List PreparedImage) (entry : PlatformDefinition) : + Except AuditError Bool := do + let .enumType code _ := entry.kind | return true + let some (image, row) ← findPinnedDefinition images entry.parts entry.scope (images.length + 1) + | return false + unless (← physicalEnumCode image row) == code do throw (.typeDefMismatch row .fields) + return true + +/-- Check every entry, including entries after an unavailable image. -/ +def coveredEnumEntries (images : List PreparedImage) : + List PlatformDefinition → Except AuditError Bool + | [] => .ok true + | head :: rest => do + let first ← coveredEnumEntry images head + let remaining ← coveredEnumEntries images rest + return first && remaining + +/-- Complete coverage includes each entry of the table. -/ +theorem coveredEnumEntries_each {images : List PreparedImage} {entries : List PlatformDefinition} + (checked : coveredEnumEntries images entries = .ok true) {entry} (member : entry ∈ entries) : + coveredEnumEntry images entry = .ok true := by + induction entries with + | nil => simp at member + | cons head rest ih => + simp only [coveredEnumEntries] at checked + obtain ⟨first, read, checked⟩ := CIL.Safety.except_bind_ok checked + obtain ⟨remaining, tail, accepted⟩ := CIL.Safety.except_bind_ok checked + change Except.ok (ε := AuditError) (first && remaining) = .ok true at accepted + simp only [Except.ok.injEq, Bool.and_eq_true] at accepted + obtain ⟨firstTrue, remainingTrue⟩ := accepted + rcases List.mem_cons.mp member with rfl | later + · exact firstTrue ▸ read + · exact ih (remainingTrue ▸ tail) later +#print axioms coveredEnumEntries_each + +/-- The element code an extracted enum operand admits is the code in its physical backing field. -/ +theorem externalEnum_physicalStorage {images : List PreparedImage} {env : MetadataEnv} {token : Nat} + {scope name : List Nat} {parts : List (List Nat)} {hint : Option Bool} {frame : Frame} + {fuel : Nat} {named : NamedType} {identity : String} {underlying : TypeExpr} + (resolved : env.externalNamed token scope name parts hint = .ok named) + (admitted : typeAdmits frame (fuel + 1) (.named named) (.enum identity underlying) = true) + (checked : coveredEnumEntries images env.platform.definitions = .ok true) : + ∃ code image row, PinnedDefinitionAt images named.parts named.scope image row ∧ + physicalEnumCode image row = .ok code ∧ integerElement code = true ∧ + typeAdmits frame fuel (.primitive code) underlying = true := by + obtain ⟨_, code, member, integer, representation⟩ := typeAdmits_external_enum resolved admitted + have covered := coveredEnumEntries_each checked member + simp only [coveredEnumEntry, bind, Except.bind] at covered + cases selected : findPinnedDefinition images named.parts named.scope (images.length + 1) with + | error fault => simp [selected] at covered + | ok result => + cases result with + | none => simp [selected, pure, Except.pure] at covered + | some pair => + obtain ⟨image, row⟩ := pair + cases read : physicalEnumCode image row with + | error fault => simp [selected, read] at covered + | ok actual => + simp [selected, read] at covered + split at covered <;> try contradiction + rename_i same + exact ⟨code, image, row, pinnedDefinitionAt_of_find selected, same ▸ read, integer, + representation⟩ +#print axioms externalEnum_physicalStorage + +end StrictBinding diff --git a/StrictBinding/EnumStorage.lean b/StrictBinding/EnumStorage.lean new file mode 100644 index 000000000..95daae92a --- /dev/null +++ b/StrictBinding/EnumStorage.lean @@ -0,0 +1,42 @@ +import StrictBinding.CarriedEnums +import CIL.Extraction.Translation + +/-! Check every enum definition selected by the certified extraction against the supplied images. -/ + +namespace StrictBinding + +open CIL CIL.Extraction + +/-- Collect the declarations from the actual body metadata environments of the proof. -/ +def methodEnumClaims (platform : PlatformTable) (methods : List MethodEvidence) : + Except AuditError (List CarriedEnum) := do + let groups ← methods.mapM fun item => do + let .ok env := item.body.environment | throw (.rowMismatch item.original.token) + unless env.platform == platform do throw (.rowMismatch item.original.token) + return carriedEnums env + return groups.flatten.eraseDups + +/-- Read the backing field for every selected local or platform declaration. With no enum + declaration, or a missing scope image, there is no complete enum receipt. -/ +def checkEnumStorage (images : List PinnedImage) (platform : PlatformTable) + (methods : List MethodEvidence) : Except AuditError Bool := do + let prepared ← prepare images + let localClaims ← methodEnumClaims platform methods + let localCoverage ← localClaims.mapM (carriedEnumBound prepared) + let externalClaims := platform.definitions + let externalCoverage ← coveredEnumEntries prepared externalClaims + let selected := !localClaims.isEmpty || externalClaims.any fun entry => + match entry.kind with | .enumType .. => true | _ => false + return selected && localCoverage.all id && externalCoverage + +/-- An explicit coverage bit distinguishes a complete audit from an incomplete selection. + Both outcomes require every supplied definition to pass the physical checks. -/ +def EnumStorageChecked (images : List PinnedImage) (platform : PlatformTable) + (methods : List MethodEvidence) (complete : Bool) : Prop := + (checkEnumStorage images platform methods).toOption = some complete + +instance (images : List PinnedImage) (platform : PlatformTable) (methods : List MethodEvidence) + (complete : Bool) : Decidable (EnumStorageChecked images platform methods complete) := + inferInstanceAs (Decidable (_ = _)) + +end StrictBinding diff --git a/StrictBinding/EnumStorageEvidence.lean b/StrictBinding/EnumStorageEvidence.lean new file mode 100644 index 000000000..c84f58578 --- /dev/null +++ b/StrictBinding/EnumStorageEvidence.lean @@ -0,0 +1,111 @@ +import StrictBinding.EnumStorage + +/-! Relate a complete enum receipt to the physical definitions of the certified method types. -/ + +namespace StrictBinding + +open CIL CIL.Extraction + +/-- A complete receipt checked every carried enum claim and the same external table as the proof. -/ +theorem EnumStorageChecked.complete {images : List PinnedImage} {platform : PlatformTable} + {methods : List MethodEvidence} (checked : EnumStorageChecked images platform methods true) : + ∃ prepared claims, prepare images = .ok prepared ∧ methodEnumClaims platform methods = .ok claims ∧ + (∀ claim ∈ claims, carriedEnumBound prepared claim = .ok true) ∧ + coveredEnumEntries prepared platform.definitions = .ok true := by + have done : checkEnumStorage images platform methods = .ok true := by + cases result : checkEnumStorage images platform methods with + | error err => simp [EnumStorageChecked, result, Except.toOption] at checked + | ok value => + have yes : value = true := by simpa [EnumStorageChecked, result, Except.toOption] using checked + rw [yes] + unfold checkEnumStorage at done + obtain ⟨prepared, preparedOk, done⟩ := CIL.Safety.except_bind_ok done + obtain ⟨claims, claimsOk, done⟩ := CIL.Safety.except_bind_ok done + obtain ⟨localChecks, localOk, done⟩ := CIL.Safety.except_bind_ok done + obtain ⟨external, externalOk, done⟩ := CIL.Safety.except_bind_ok done + have confirmed := Except.ok.inj done + simp only [Bool.and_eq_true] at confirmed + refine ⟨prepared, claims, preparedOk, claimsOk, ?_, confirmed.2 ▸ externalOk⟩ + intro claim member + obtain ⟨result, found, read⟩ := read_member member localOk + have yes : result = true := List.all_eq_true.mp confirmed.1.2 result found + exact yes ▸ read +#print axioms EnumStorageChecked.complete + + +/-- Each certified body uses the extraction's platform table and contributes every carried enum. -/ +theorem methodEnumClaims_body {platform : PlatformTable} {methods : List MethodEvidence} + {item : MethodEvidence} {env : MetadataEnv} {claims : List CarriedEnum} + (method : item ∈ methods) (environment : item.body.environment = .ok env) + (checked : methodEnumClaims platform methods = .ok claims) : + env.platform = platform ∧ ∀ claim ∈ carriedEnums env, claim ∈ claims := by + unfold methodEnumClaims at checked + obtain ⟨groups, groupsOk, finished⟩ := CIL.Safety.except_bind_ok checked + obtain ⟨group, listed, selected⟩ := read_member method groupsOk + by_cases same : env.platform = platform + · have chosen : carriedEnums env = group := by + simpa [environment, same, bind, Except.bind, pure, Except.pure] using selected + refine ⟨same, ?_⟩ + intro claim found + have equal : groups.flatten.eraseDups = claims := Except.ok.inj finished + rw [← equal] + exact List.mem_eraseDups.mpr (List.mem_flatten.mpr ⟨group, listed, chosen ▸ found⟩) + · simp [environment, same, bind, Except.bind, throw] at selected +#print axioms methodEnumClaims_body + +/-- The physical TypeDef row and storage code of any carried enum resolved in a certified body. -/ +theorem EnumStorageChecked.carried {images : List PinnedImage} {platform : PlatformTable} + {methods : List MethodEvidence} {item : MethodEvidence} {env : MetadataEnv} + {position fuel token code : Nat} {named : NamedType} + (checked : EnumStorageChecked images platform methods true) (method : item ∈ methods) + (environment : item.body.environment = .ok env) (budget : fuel ≤ resolveFuel) + (lookup : typeDefNamed env position fuel token = .ok named) (kind : named.kind = .enumType code) : + ∃ prepared image, prepare images = .ok prepared ∧ image ∈ prepared ∧ + bytesOf image.identity = named.scope ∧ + enumDefinition? image 0 named.parts = .ok (some (tokenRow token)) ∧ + physicalEnumCode image (tokenRow token) = .ok code := by + obtain ⟨prepared, claims, built, selected, covered, _⟩ := checked.complete + have member := carriedEnums_of_typeDefNamed budget lookup kind + have physical := covered _ ((methodEnumClaims_body method environment selected).2 _ member) + unfold carriedEnumBound at physical + simp only [bind, Except.bind] at physical + cases hi : prepared.find? (fun image => bytesOf image.identity == named.scope) with + | none => simp [hi, pure, Except.pure] at physical + | some image => + have scope := List.find?_some hi + refine ⟨prepared, image, built, List.mem_of_find?_eq_some hi, by simpa using scope, ?_⟩ + cases row : enumDefinition? image 0 named.parts with + | error err => simp [hi, row] at physical + | ok result => + cases read : physicalEnumCode image (tokenRow token) with + | error err => + simp [hi, row, read, throw] at physical + split at physical <;> contradiction + | ok actual => + simp [hi, row, read, throw] at physical + split at physical <;> try contradiction + rename_i sameRow + split at physical <;> try contradiction + rename_i sameCode + exact ⟨congrArg Except.ok (by simpa using sameRow), congrArg Except.ok (by simpa using sameCode)⟩ +#print axioms EnumStorageChecked.carried + +/-- A certified external enum resolves through its supplied forwarders to its physical field code. -/ +theorem EnumStorageChecked.external {images : List PinnedImage} {platform : PlatformTable} + {methods : List MethodEvidence} {item : MethodEvidence} {env : MetadataEnv} {token fuel : Nat} + {scope name : List Nat} {parts : List (List Nat)} {hint : Option Bool} {frame : Frame} + {named : NamedType} {identity : String} {underlying : TypeExpr} + (checked : EnumStorageChecked images platform methods true) (method : item ∈ methods) + (environment : item.body.environment = .ok env) + (resolved : env.externalNamed token scope name parts hint = .ok named) + (admitted : typeAdmits frame (fuel + 1) (.named named) (.enum identity underlying) = true) : + ∃ prepared, prepare images = .ok prepared ∧ ∃ code image row, + PinnedDefinitionAt prepared named.parts named.scope image row ∧ + physicalEnumCode image row = .ok code ∧ integerElement code = true ∧ + typeAdmits frame fuel (.primitive code) underlying = true := by + obtain ⟨prepared, _, built, selected, _, covered⟩ := checked.complete + have same := (methodEnumClaims_body method environment selected).1 + exact ⟨prepared, built, externalEnum_physicalStorage resolved admitted (same ▸ covered)⟩ +#print axioms EnumStorageChecked.external + +end StrictBinding diff --git a/StrictBinding/Error.lean b/StrictBinding/Error.lean new file mode 100644 index 000000000..1264aafa0 --- /dev/null +++ b/StrictBinding/Error.lean @@ -0,0 +1,146 @@ +/-! +# Audit errors + +The reasons a strict binding check can fail. Every check in `StrictBinding` returns +`Except AuditError`, so a rejected image, container or body names the fact that did not hold. +Payloads are numbers (offsets, tokens, relative virtual addresses) so a test can compare an +error exactly. +-/ + +namespace StrictBinding + +/-- Why the PE container of an image does not decode or does not map a relative virtual address. -/ +inductive PeError where + /-- The file does not start with the `MZ` signature. -/ + | notDos + /-- The `PE\0\0` signature is missing at this file offset. -/ + | badSignature (offset : Nat) + /-- The optional header magic is neither PE32 (`0x10B`) nor PE32+ (`0x20B`). -/ + | badMagic (magic : Nat) + /-- The optional header has no CLI header data directory. -/ + | noCliHeader + /-- The CLI header is shorter than 72 bytes or does not state its own size. -/ + | badCliHeader + /-- No section holds `count` bytes at the relative virtual address. -/ + | unmappedRva (rva count : Nat) + /-- More than one section holds the relative virtual address. -/ + | ambiguousRva (rva : Nat) + /-- The metadata root is missing, malformed or lacks the `#~` stream. -/ + | badMetadataRoot + /-- A stream header names a stream twice or one the audit does not read. -/ + | badStream + deriving DecidableEq, Repr + +/-- Why the table stream of an image does not decode or does not contain a token. -/ +inductive TableError where + /-- The `#~` header or its tables do not fit inside the stream. -/ + | badTableStream + /-- A table above `0x2C` is present, so row sizes cannot be computed. -/ + | unsupportedTables (valid : Nat) + /-- The table stream has pointer or edit-and-continue tables, which index other tables + indirectly, so row sizes and positions are not computed from the target table alone. -/ + | pointerTables (valid : Nat) + /-- The heap size flags set a bit the audit does not know. -/ + | unsupportedHeapSizes (flags : Nat) + /-- The metadata tables have no such column. -/ + | noSuchColumn (table column : Nat) + /-- A heap key or the entry it names is outside the heap. -/ + | outsideHeap (key : Nat) + /-- A string heap entry is not well-formed UTF-8. -/ + | invalidUtf8 + /-- An unflagged public key token has a nonzero width other than eight bytes. -/ + | invalidPublicKeyToken (length : Nat) + deriving DecidableEq, Repr + +/-- The part of a type definition claim that its TypeDef row does not have. -/ +inductive TypeDefPart where + | typeName + | typeNamespace + /-- The row's visibility or the stated NestedClass row does not give the claimed nesting. -/ + | nesting + /-- The row's flags and base type give another kind. -/ + | kind + /-- The row's field list or the next row's does not bound the claimed Field rows. -/ + | fields + /-- The `Extends` column does not name the claimed base row, or that row has other names. -/ + | base + deriving DecidableEq, Repr + +/-- Why a binding fact does not hold. -/ +inductive AuditError where + /-- The image constant has the wrong chunk count, an oversized chunk or nonzero padding. -/ + | malformedImage + + /-- An AssemblyRef's key or token differs from the physical definition or the carried row. -/ + | referenceKeyMismatch (identity : String) (token : Nat) + /-- A read of `count` bytes at `offset` leaves the image. -/ + | outsideImage (offset count : Nat) + | pe (error : PeError) + | tables (error : TableError) + /-- The token names a row the tables do not have. -/ + | tokenOutOfScope (token : Nat) + /-- The row number exceeds the three bytes available below the table byte. -/ + | rowOverflow (table row : Nat) + /-- A body is bound to a token that is not a MethodDef token. -/ + | notMethodDef (token : Nat) + /-- The MethodDef row of the token has no IL body. -/ + | noIlBody (token : Nat) + /-- The bytes at the mapped position of the row's relative virtual address are not the body. -/ + | bodyMismatch (token rva offset : Nat) + /-- A FieldRVA or ClassLayout row names an absent or repeated Field or TypeDef. -/ + | invalidDataTableKey (table row key : Nat) + /-- The static sites do not match the program counters recorded by the body decoder. -/ + | staticSiteMismatch (method : Nat) + /-- The static site of a decoded method has no corresponding field in the evidence. -/ + | unresolvedStaticField (method pc : Nat) + /-- A field, its RVA or its mapped file position does not permit the claimed data access. -/ + | invalidFieldData (token : Nat) + /-- The slice does not identify the platform scope of its primitive types. -/ + | invalidObjectScope (identity : String) + /-- The platform image lacks a unique public, baseless System.Object class. -/ + | invalidObjectDefinition (identity : String) + /-- A physical field declared by System.Object lacks the Static flag. -/ + | objectInstanceField (identity : String) (field : Nat) + /-- An authenticated platform image has a malformed or ambiguous System.ValueTuple pair. -/ + | invalidTupleDefinition (identity : String) + /-- The physical tuple field differs in number, name, or generic parameter signature. -/ + | invalidTupleField (identity : String) (field : Nat) + /-- The claimed static data differs from the bytes at the field's mapped RVA. -/ + | fieldDataMismatch (token rva offset : Nat) + /-- The Assembly row does not spell the identity the image is pinned under. -/ + | identityMismatch (identity : String) + /-- No pinned image has this identity. -/ + | unpinned (identity : String) + /-- Two pinned images have the same identity. -/ + | duplicateImage (identity : String) + /-- The carried table stream header of the assembly pinned under `identity` is not the image's + header through its last row count, or does not decode to the carried decoded header. -/ + | headerMismatch (identity : String) + /-- The carried row of the token is not the image's row under the carried heap numbering. -/ + | rowMismatch (token : Nat) + /-- The generic parameter declarations or direct interfaces differ from the physical rows. -/ + | constraintMismatch (token : Nat) + /-- The constraint uses a type form whose physical identity is not covered. -/ + | uncoveredConstraint + /-- The claimed value layout differs from the fields or layout metadata in the image. -/ + | layoutMismatch (identity : String) + /-- A physical class slot, linked target or complete catalog differs from the extraction. -/ + | dispatchMismatch (method : Nat) + /-- The carried entry with this key is not the entry of the image at the offset the key names. -/ + | heapMismatch (heap key : Nat) + /-- Two row cells of one offset hold different keys, two of different offsets hold one key, a + carried entry has a key no row cell holds, a row key has no carried entry, a key is not + below the omitted-entry marker, or the carried entries of the heap do not ascend by key. -/ + | heapNumbering (heap key : Nat) + /-- TypeDef row `row` does not have this part of the claimed type definition. -/ + | typeDefMismatch (row : Nat) (part : TypeDefPart) + /-- Row `row` of `table` has a key below the key of the row before it. -/ + | unsortedTable (table row : Nat) + deriving DecidableEq, Repr + +/-- Whether a check succeeded; the proposition of each audited theorem is `ok check = true`. -/ +def ok {α : Type} : Except AuditError α → Bool + | .ok _ => true + | .error _ => false + +end StrictBinding diff --git a/StrictBinding/Evidence.lean b/StrictBinding/Evidence.lean new file mode 100644 index 000000000..d10a0d267 --- /dev/null +++ b/StrictBinding/Evidence.lean @@ -0,0 +1,185 @@ +import StrictBinding.Slice +import CIL.Extraction.Translation + +/-! +# Extraction evidence bound to images + +The propositions that generated audit modules state about an extraction: each one reads the +constants of `Extracted` that `Extracted.translation_valid` certifies and asks a check of +`StrictBinding.Bind` about them. The audit proves them in the same environment as that +certificate, so they describe the certified evidence and no other. +-/ + +namespace StrictBinding + +/-- The body each method evidence record carries, with the assembly and token it names. -/ +def bodyClaims (evidence : List CIL.Extraction.MethodEvidence) : List BodyClaim := + evidence.map fun item => + { identity := item.assembly.identity, token := item.original.token, bytes := item.body.bytes } + +/-- Every method body of the evidence is the bytes at the place the MethodDef row of its token + gives, in the pinned image of the assembly the evidence names. -/ +def BodiesBound (images : List PinnedImage) (evidence : List CIL.Extraction.MethodEvidence) : + Prop := + ClaimsHold bodyClaimBound images (bodyClaims evidence) + +instance (images : List PinnedImage) (evidence : List CIL.Extraction.MethodEvidence) : + Decidable (BodiesBound images evidence) := + inferInstanceAs (Decidable (ClaimsHold _ _ _)) + +/-- The rows and heaps of each carried slice are checked against the image selected by its + assembly's identity. Cost is one lookup and one contents check per carried slice; the + Assembly blocks are checked by `assemblyBlockBound`. -/ +def methodMetadataBound (prepared : List PreparedImage) + (metadata : CIL.Extraction.MethodMetadata) : Except AuditError Unit := + metadata.slices.forM fun slice => do + let image <- located prepared slice.assembly.identity + sliceContentsBound image slice + +/-- A carried Assembly block is checked against the image selected by its identity. -/ +def assemblyBlockBound (prepared : List PreparedImage) + (assembly : CIL.Extraction.AssemblyMetadata) : Except AuditError Unit := do + let image <- located prepared assembly.identity + assemblyBound image assembly + +/-- The carried metadata of each method, in evidence order. -/ +def metadataClaims (evidence : List CIL.Extraction.MethodEvidence) : + List CIL.Extraction.MethodMetadata := evidence.map (fun item => item.body.metadata) + +/-- The distinct Assembly blocks the metadata carries, in first-occurrence order. Blocks are + compared whole, so two blocks with one identity and different bytes are both kept. -/ +def assemblyBlocks (claims : List CIL.Extraction.MethodMetadata) : + List CIL.Extraction.AssemblyMetadata := + (claims.flatMap fun metadata => metadata.slices.map fun slice => slice.assembly).eraseDups + +/-- Every carried metadata block belongs to its named pinned image, modulo heap numbering: each + distinct Assembly block is checked once, and the rows and heaps of every slice. -/ +def MetadataRowsBound (images : List PinnedImage) (evidence : List CIL.Extraction.MethodEvidence) : + Prop := + And (ClaimsHold assemblyBlockBound images (assemblyBlocks (metadataClaims evidence))) + (ClaimsHold methodMetadataBound images (metadataClaims evidence)) + +instance (images : List PinnedImage) (evidence : List CIL.Extraction.MethodEvidence) : + Decidable (MetadataRowsBound images evidence) := + inferInstanceAs (Decidable (And _ _)) + +/-- A successful traversal checked every member successfully. -/ +theorem check_of_forM_ok {Claim : Type} {check : Claim -> Except AuditError Unit} + {claims : List Claim} (holds : ok (claims.forM check) = true) : + forall claim, List.Mem claim claims -> ok (check claim) = true := by + induction claims with + | nil => intro claim member; cases member + | cons first rest onRest => + cases checked : check first with + | error error => simp [List.forM, checked, bind, Except.bind, ok] at holds + | ok value => + simp only [List.forM, checked, bind, Except.bind] at holds + intro claim member + cases member with + | head => simp [checked, ok] + | tail _ member => exact onRest holds claim member + +#print axioms check_of_forM_ok + +/-- Every claim holds individually against the same successful image preparation. -/ +theorem check_of_claimsHold {Claim : Type} + {check : List PreparedImage -> Claim -> Except AuditError Unit} + {images : List PinnedImage} {claims : List Claim} + (holds : ClaimsHold check images claims) : + Exists fun prepared => And (prepare images = .ok prepared) + (forall claim, List.Mem claim claims -> ok (check prepared claim) = true) := by + unfold ClaimsHold checkAll at holds + cases decoded : prepare images with + | error error => simp [decoded, bind, Except.bind, ok] at holds + | ok prepared => + simp only [decoded, bind, Except.bind] at holds + exact Exists.intro prepared (And.intro rfl (check_of_forM_ok holds)) + +#print axioms check_of_claimsHold + +/-- Every member of a successful image preparation has bounded chunks. -/ +theorem prepare_bounded {images : List PinnedImage} {prepared : List PreparedImage} + (decoded : prepare images = .ok prepared) : + forall image, List.Mem image prepared -> image.view.Bounded := by + have mapped : forall (images : List PinnedImage) (prepared : List PreparedImage), + images.mapM prepareOne = .ok prepared -> + forall image, List.Mem image prepared -> image.view.Bounded := by + intro images + induction images with + | nil => + intro prepared decoded image member + simp only [List.mapM_nil, pure, Except.pure, Except.ok.injEq] at decoded + subst prepared + cases member + | cons pinned rest onRest => + intro prepared decoded image member + rw [List.mapM_cons] at decoded + cases CIL.Safety.except_bind_ok decoded with + | intro first firstResult => + cases CIL.Safety.except_bind_ok firstResult.2 with + | intro later laterResult => + have same : first :: later = prepared := Except.ok.inj laterResult.2 + subst prepared + cases member with + | head => exact prepareOne_bounded firstResult.1 + | tail _ member => exact onRest later laterResult.1 image member + unfold prepare at decoded + cases CIL.Safety.except_bind_ok decoded with + | intro result checks => + cases repeated : duplicateIdentity? result with + | some identity => simp [repeated, throw] at checks + | none => + have same : result = prepared := by + simpa [repeated, pure, Except.pure] using checks.2 + subst prepared + exact mapped images result checks.1 + +#print axioms prepare_bounded + +/-- Every carried block is bound to its selected image from one preparation of the pins. -/ +theorem sliceMatches_of_metadataRowsBound {images : List PinnedImage} + {evidence : List CIL.Extraction.MethodEvidence} (bound : MetadataRowsBound images evidence) : + Exists fun prepared => And (prepare images = .ok prepared) + (forall item, List.Mem item evidence -> + forall slice, List.Mem slice item.body.metadata.slices -> + Exists fun image => And (located prepared slice.assembly.identity = .ok image) + (SliceMatches image slice)) := by + cases check_of_claimsHold bound.2 with + | intro prepared checks => + cases check_of_claimsHold bound.1 with + | intro blockPrepared blockChecks => + have samePreparation : prepared = blockPrepared := + Except.ok.inj (checks.1.symm.trans blockChecks.1) + subst samePreparation + refine Exists.intro prepared (And.intro checks.1 ?_) + intro item member slice carried + have metadataMember : List.Mem item.body.metadata (metadataClaims evidence) := + List.mem_map.mpr (Exists.intro item (And.intro member rfl)) + have checked := checks.2 item.body.metadata metadataMember + have one := check_of_forM_ok checked slice carried + have sliceAssembly : + List.Mem slice.assembly (item.body.metadata.slices.map fun slice => slice.assembly) := + List.mem_map.mpr (Exists.intro slice (And.intro carried rfl)) + have blockMember : List.Mem slice.assembly (assemblyBlocks (metadataClaims evidence)) := + List.mem_eraseDups.mpr (List.mem_flatMap.mpr (Exists.intro item.body.metadata + (And.intro metadataMember sliceAssembly))) + have blockOne := blockChecks.2 slice.assembly blockMember + cases found : located prepared slice.assembly.identity with + | error error => simp [found, bind, Except.bind, ok] at one + | ok image => + refine Exists.intro image (And.intro rfl ?_) + have imageMember : List.Mem image prepared := mem_of_located found + have contents : sliceContentsBound image slice = .ok () := by + cases result : sliceContentsBound image slice with + | error error => simp [found, result, bind, Except.bind, ok] at one + | ok value => cases value; rfl + have assembly : assemblyBound image slice.assembly = .ok () := by + unfold assemblyBlockBound at blockOne + cases result : assemblyBound image slice.assembly with + | error error => simp [found, result, bind, Except.bind, ok] at blockOne + | ok value => cases value; rfl + exact sliceMatches_of_bounds (prepare_bounded checks.1 image imageMember) assembly contents + +#print axioms sliceMatches_of_metadataRowsBound + +end StrictBinding diff --git a/StrictBinding/GenericConstraints.lean b/StrictBinding/GenericConstraints.lean new file mode 100644 index 000000000..41ffc8dc5 --- /dev/null +++ b/StrictBinding/GenericConstraints.lean @@ -0,0 +1,313 @@ +import StrictBinding.PlatformKinds +import CIL.Extraction.Translation + +/-! Check generic parameter declarations and the direct interfaces used to satisfy them +against the GenericParam, GenericParamConstraint and InterfaceImpl tables in pinned images. -/ + +namespace StrictBinding + +open CIL.Extraction + +/-- CLI InterfaceImpl table number. -/ +private def interfaceImplTable := 0x09 +/-- CLI GenericParamConstraint table number. -/ +private def constraintTable := 0x2C + +/-- Resolve the TypeDefOrRef coded index used in a constraint or InterfaceImpl row. -/ +private def constraintTarget (image : PreparedImage) (coded : Nat) : Except AuditError Nat := do + let some table := typeDefOrRefTags[coded % 4]? | throw (.constraintMismatch coded) + let token ← checkedTokenOf table (coded / 4) .rowOverflow + let _ ← image.layout.rowAt token + return token + +/-- Full assembly identity recorded in the physical AssemblyRef. -/ +private def referenceIdentity (image : PreparedImage) (token : Nat) : + Except AuditError String := do + let text ← assemblyIdentityAt image.view image.layout token 0 + return String.ofList (text.map Char.ofNat) + +/-- The unambiguous core assembly used to spell primitive elements in this module. -/ +private def primitiveScope (image : PreparedImage) : Except AuditError String := do + if ← platformAssembly image then return image.identity + let cell := image.layout.cellOfRow image.view Table.typeRef + let mut found := none + for index in [:image.layout.rowCount Table.typeRef] do + let row := index + 1 + unless (← image.layout.stringAt image.view (← cell row typeNameColumn)) == bytesOf "Object" && + (← image.layout.stringAt image.view (← cell row typeNamespaceColumn)) == systemNamespace do + continue + let scope ← cell row 0 + unless scope % 4 == 2 do continue + let token ← checkedTokenOf Table.assemblyRef (scope / 4) .rowOverflow + unless ← coreAssemblyAt image token 0 do continue + let identity ← referenceIdentity image token + if found.any (· != identity) then throw .uncoveredConstraint + found := some identity + let some core := found | throw .uncoveredConstraint + return core + +/-- A top-level simple name cannot include a namespace separator or a constructed type suffix. -/ +def ordinaryTypeName (space name : String) : Bool := + let component := fun (c : Char) => c.isAlphanum || c == '_' || c == '`' + !name.isEmpty && (metadataChars name).all component && + (metadataChars space).all (fun c => component c || c == '.') + +/-- A top-level TypeDef or AssemblyRef-scoped TypeRef with its physical assembly identity. -/ +private def constraintTypeName (image : PreparedImage) (token : Nat) : + Except AuditError String := do + let cell := image.layout.cell image.view token + let space ← image.layout.stringAt image.view (← cell typeNamespaceColumn) + let name ← image.layout.stringAt image.view (← cell typeNameColumn) + let some namespaceText := spelled? space | throw (.constraintMismatch token) + let some nameText := spelled? name | throw (.constraintMismatch token) + unless ordinaryTypeName namespaceText nameText do throw .uncoveredConstraint + let scope ← match tableOf token with + | 2 => do + if visibilityOf (← cell typeDefFlagsColumn) ≥ nestedVisibility then + throw .uncoveredConstraint + unless (← enumDefinition? image 0 [space, name]) == some (token % 2^24) do + throw (.constraintMismatch token) + pure image.identity + | 1 => do + let coded ← cell 0 + unless coded % 4 == 2 do throw .uncoveredConstraint + referenceIdentity image (← checkedTokenOf Table.assemblyRef (coded / 4) .rowOverflow) + | _ => throw .uncoveredConstraint + let fullName := if namespaceText.isEmpty then nameText else namespaceText ++ "." ++ nameText + return scope ++ "::" ++ fullName + +/-- Physical constraint identity. A VAR in an implemented interface refers to the corresponding + closed argument of its declaring type. -/ +private def constraintIdentity (image : PreparedImage) (token : Nat) + (arguments : Option (List String) := none) : + Except AuditError String := do + if tableOf token != Table.typeSpec then return ← constraintTypeName image token + let blob ← image.layout.blobAt image.view (← image.layout.cell image.view token 0) + let .ok spec := decodeTypeSpec blob | throw .uncoveredConstraint + match spec with + | .genericInst false head args => + unless tableOf head == Table.typeDef do throw .uncoveredConstraint + let (kind, arity) ← physicalTypeShape image (head % 2^24) + unless kind == .interface && arity == args.toList.length do throw .uncoveredConstraint + let core ← primitiveScope image + let names ← args.toList.mapM fun argument => do + match argument with + | .primitive code => + let some name := (primitiveName code).bind spelled? | throw .uncoveredConstraint + return core ++ "::" ++ name + | .var index => + let some closed := arguments | throw .uncoveredConstraint + let some name := closed[index]? | throw (.constraintMismatch token) + return name + | _ => throw .uncoveredConstraint + return (← constraintTypeName image head) ++ "<" ++ String.intercalate "," names ++ ">" + | .modified true modifier (.named false base) => + let name := (← constraintTypeName image modifier) ++ "!" ++ (← constraintTypeName image base) + unless name == unmanagedTypeConstraint do throw .uncoveredConstraint + return name + | _ => throw .uncoveredConstraint + +/-- Every GenericParamConstraint owned by this GenericParam, in metadata order. -/ +private def parameterTypes (image : PreparedImage) (parameter : Nat) : + Except AuditError (List String) := do + let cell := image.layout.cellOfRow image.view constraintTable + let mut types := #[] + for index in [:image.layout.rowCount constraintTable] do + let row := index + 1 + if (← cell row 0) == parameter then + types := types.push (← constraintIdentity image (← constraintTarget image (← cell row 1))) + return types.toList + +/-- Compare the complete parameter list for a physical TypeOrMethodDef owner. -/ +def parameterDeclarations (image : PreparedImage) (owner : Nat) + (expected : List GenericConstraint) : Except AuditError Unit := do + let tag := if tableOf owner == Table.methodDef then 1 else 0 + let ownerKey := 2 * (owner % 2^24) + tag + let cell := image.layout.cellOfRow image.view Table.genericParam + let mut remaining := expected + let mut ordinal := 0 + for index in [:image.layout.rowCount Table.genericParam] do + let row := index + 1 + if (← cell row 2) == ownerKey then + let parameter :: tail := remaining | throw (.constraintMismatch owner) + unless (← cell row 0) == ordinal && (← cell row 1) == parameter.flags do + throw (.constraintMismatch owner) + let types ← parameterTypes image row + unless parameter.interfaces.isEmpty && parameter.types == types do + throw (.constraintMismatch owner) + remaining := tail + ordinal := ordinal + 1 + unless remaining.isEmpty do throw (.constraintMismatch owner) + +/-- Locate the physical TypeDef whose MethodList range contains this MethodDef. -/ +def methodDeclaringType (image : PreparedImage) (method : Nat) : Except AuditError Nat := do + unless tableOf method == Table.methodDef do throw (.notMethodDef method) + let _ ← image.layout.rowAt method + if image.layout.rowCount 0x05 != 0 then throw .uncoveredConstraint + let target := method % 2^24 + let limit := image.layout.rowCount Table.methodDef + 1 + let cell := image.layout.cellOfRow image.view Table.typeDef + let mut owner := none + let mut previous := 1 + for index in [:image.layout.rowCount Table.typeDef] do + let row := index + 1 + let first ← cell row 5 + unless previous ≤ first && first ≤ limit && (row != 1 || first == 1) do + throw (.constraintMismatch method) + if first ≤ target then owner := some row + previous := first + let some row := owner | throw (.constraintMismatch method) + return StrictBinding.tokenOf Table.typeDef row + +/-- The unique top-level value type of this scoped name, with its physical generic arity. -/ +def valueLayoutOwner (image : PreparedImage) (identity : String) (fullName : List Char) : + Except AuditError (Nat × Nat) := do + let (reversedName, rest) := fullName.reverse.span (· != '.') + let name := bytesOf (String.ofList reversedName.reverse) + let space := bytesOf (String.ofList (rest.drop 1).reverse) + let some row ← enumDefinition? image 0 [space, name] | throw .uncoveredConstraint + unless (← constraintTypeName image (StrictBinding.tokenOf Table.typeDef row)) == identity do + throw (.constraintMismatch (StrictBinding.tokenOf Table.typeDef row)) + let (kind, arity) ← physicalTypeShape image row + unless kind == .valueType do throw .uncoveredConstraint + return (row, arity) + +/-- A closed value type's argument order and parameter declarations are those of its TypeDef. + Only primitive arguments with the physical core scope can close an interface VAR here. -/ +private def layoutTypeArguments (image : PreparedImage) (token : Nat) (layout : RawLayout) : + Except AuditError (Option (List String)) := do + let some generic := layout.generic | return none + unless generic.owner matches .constructor .. do throw (.constraintMismatch token) + unless genericOwnerIdentity generic.owner generic.arguments == some layout.identity && + generic.arity > 0 && generic.arguments.length == generic.arity do + throw (.constraintMismatch token) + parameterDeclarations image token (generic.constraints.map fun (flags, types) => { flags, types }) + let core ← primitiveScope image + let names ← generic.arguments.mapM fun argument => do + let .atom scope name := argument | throw .uncoveredConstraint + unless scope == core && primitiveCodes.any (fun code => + code != 0x01 && (primitiveName code).bind spelled? == some name) do + throw .uncoveredConstraint + return scope ++ "::" ++ name + return some names + +/-- Direct interfaces of a concrete argument are the InterfaceImpl rows the physical type owns. -/ +def layoutInterfaces (images : List PreparedImage) (layout : RawLayout) : + Except AuditError Unit := do + let ownerType := (layout.generic.map (·.owner)).getD (.aggregate layout.identity 0) + let some (scope, name) := namedType ownerType | throw .uncoveredConstraint + let some image := images.find? (·.identity == String.ofList scope) | throw .uncoveredConstraint + let ownerName := String.ofList scope ++ "::" ++ String.ofList name + let (owner, arity) ← valueLayoutOwner image ownerName name + let token := StrictBinding.tokenOf Table.typeDef owner + unless arity == (layout.generic.map (·.arity)).getD 0 do throw (.constraintMismatch token) + let arguments ← layoutTypeArguments image token layout + let cell := image.layout.cellOfRow image.view interfaceImplTable + let mut interfaces := #[] + for index in [:image.layout.rowCount interfaceImplTable] do + let row := index + 1 + if (← cell row 0) == owner then + let target ← constraintTarget image (← cell row 1) + let identity ← constraintIdentity image target arguments + if interfaces.contains identity then + throw (.constraintMismatch (StrictBinding.tokenOf Table.typeDef owner)) + interfaces := interfaces.push identity + unless interfaces.toList == layout.interfaces do + throw (.constraintMismatch (StrictBinding.tokenOf Table.typeDef owner)) + +/-- An unhandled signature or unavailable scope leaves its claim uncovered. -/ +private def constraintCovered (check : Except AuditError Unit) : Except AuditError Bool := + match check with + | .ok () => .ok true + | .error .uncoveredConstraint => .ok false + | .error err => .error err + +/-- Compare the method and owner declarations against the MethodDef and its owning TypeDef. -/ +private def checkMethodConstraints (images : List PreparedImage) (item : MethodEvidence) : + Except AuditError Bool := do + let image ← located images item.assembly.identity + let token := item.original.token + let method ← constraintCovered (parameterDeclarations image token item.original.genericParameters) + let owner ← constraintCovered (do + let declaring ← methodDeclaringType image token + parameterDeclarations image declaring item.original.ownerParameters) + return method && owner + +/-- Check every method, its owner and the interfaces of concrete arguments in the extraction. + Imports, unsupported layout arguments and unhandled signatures leave the result partial. -/ +def checkGenericConstraints (images : List PinnedImage) (methods : List MethodEvidence) + (layouts : List RawLayout) (imports : List CIL.ImportSlot) : Except AuditError Bool := do + let selected := methods.any fun method => + !method.original.genericParameters.isEmpty || !method.original.ownerParameters.isEmpty + unless selected || layouts.any (·.generic.isSome) do return false + let prepared ← prepare images + let declarations ← methods.mapM (checkMethodConstraints prepared) + let interfaces ← layouts.mapM (fun layout => constraintCovered (layoutInterfaces prepared layout)) + return imports.isEmpty && declarations.all id && interfaces.all id + +/-- The completeness argument is computed from the same image and extraction as the proof. -/ +def GenericConstraintsChecked (images : List PinnedImage) (methods : List MethodEvidence) + (layouts : List RawLayout) (imports : List CIL.ImportSlot) (complete : Bool) : Prop := + (checkGenericConstraints images methods layouts imports).toOption = some complete + +instance (images : List PinnedImage) (methods : List MethodEvidence) (layouts : List RawLayout) + (imports : List CIL.ImportSlot) (complete : Bool) : + Decidable (GenericConstraintsChecked images methods layouts imports complete) := + inferInstanceAs (Decidable (_ = _)) + +private theorem constraintCovered_ok {check : Except AuditError Unit} + (complete : constraintCovered check = .ok true) : check = .ok () := by + cases check with + | ok value => rfl + | error err => cases err <;> simp [constraintCovered] at complete +#print axioms constraintCovered_ok + +/-- Full coverage identifies the pinned image and physical owner for every selected method + and binds each concrete layout's interfaces in the same set of images. -/ +theorem GenericConstraintsChecked.physical {images : List PinnedImage} + {methods : List MethodEvidence} + {layouts : List RawLayout} {imports : List CIL.ImportSlot} + (checked : GenericConstraintsChecked images methods layouts imports true) : + ∃ prepared, prepare images = .ok prepared ∧ + (∀ item ∈ methods, ∃ image declaring, + located prepared item.assembly.identity = .ok image ∧ + methodDeclaringType image item.original.token = .ok declaring ∧ + parameterDeclarations image item.original.token item.original.genericParameters = .ok () ∧ + parameterDeclarations image declaring item.original.ownerParameters = .ok ()) ∧ + (∀ layout ∈ layouts, layoutInterfaces prepared layout = .ok ()) := by + have done : checkGenericConstraints images methods layouts imports = .ok true := by + cases result : checkGenericConstraints images methods layouts imports with + | error err => simp [GenericConstraintsChecked, result, Except.toOption] at checked + | ok value => + have yes : value = true := by + simpa [GenericConstraintsChecked, result, Except.toOption] using checked + rw [yes] + unfold checkGenericConstraints at done + dsimp only at done + split at done + · obtain ⟨prepared, built, done⟩ := CIL.Safety.except_bind_ok done + obtain ⟨declarations, declarationsRead, done⟩ := CIL.Safety.except_bind_ok done + obtain ⟨interfaces, interfacesRead, done⟩ := CIL.Safety.except_bind_ok done + have complete := Except.ok.inj done + simp only [Bool.and_eq_true] at complete + refine ⟨prepared, built, ?_, ?_⟩ + · intro item member + obtain ⟨value, found, read⟩ := read_member member declarationsRead + have yes : value = true := List.all_eq_true.mp complete.1.2 value found + unfold checkMethodConstraints at read + obtain ⟨image, located, read⟩ := CIL.Safety.except_bind_ok read + obtain ⟨method, methodRead, read⟩ := CIL.Safety.except_bind_ok read + obtain ⟨owner, ownerRead, joined⟩ := CIL.Safety.except_bind_ok read + have both := Bool.and_eq_true_iff.mp ((Except.ok.inj joined).trans yes) + have ownerOK := constraintCovered_ok (both.2 ▸ ownerRead) + obtain ⟨declaring, declared, parameters⟩ := CIL.Safety.except_bind_ok ownerOK + exact ⟨image, declaring, located, declared, constraintCovered_ok (both.1 ▸ methodRead), + parameters⟩ + · intro layout member + obtain ⟨value, found, read⟩ := read_member member interfacesRead + have yes : value = true := List.all_eq_true.mp complete.2 value found + exact constraintCovered_ok (yes ▸ read) + · cases done +#print axioms GenericConstraintsChecked.physical + +end StrictBinding diff --git a/StrictBinding/GenericImports.lean b/StrictBinding/GenericImports.lean new file mode 100644 index 000000000..b9386f68d --- /dev/null +++ b/StrictBinding/GenericImports.lean @@ -0,0 +1,200 @@ +import StrictBinding.GenericConstraints +import StrictBinding.ContractSidecar + +/-! Physical generic import sites, producer definitions and published contracts. -/ + +namespace StrictBinding.GenericImports +open CIL CIL.Extraction StrictBinding + +private def resolveScalar (arity : Nat) : SigType → Option ResolvedType + | .primitive code => some (.primitive code) + | .mvar n => if n < arity then some (.mvar n) else none + | _ => none + +/-- Reconstruct the open producer definition from its own PE image, then instantiate it + with the consumer's stated arguments for comparison with its imported contract identity. -/ +private def physicalSite (image : PreparedImage) (token : Nat) (args : List ResolvedType) : + Option MethodSite := do + let owner ← (methodDeclaringType image token).toOption + let cell := image.layout.cell image.view token + let stringAt := fun n => (image.layout.stringAt image.view n).toOption + let decl := image.layout.cell image.view owner + let name ← stringAt (← (decl 1).toOption) + let space ← stringAt (← (decl 2).toOption) + guard ((← (enumDefinition? image 0 [space,name]).toOption) == some (owner % 2^24)) + guard ((← (physicalTypeShape image (owner % 2^24)).toOption) == (.classType,0)) + guard (ordinaryTypeName (← spelled? space) (← spelled? name)) + let flags ← (cell 2).toOption + guard (flags % 8 == 6 && flags &&& 0x10 != 0 && (← (cell 1).toOption) == 0 && (← (cell 0).toOption) != 0) + let blob ← (image.layout.blobAt image.view (← (cell 4).toOption)).toOption + let sig ← (decodeMethodSig blob).toOption + guard (sig.convention == 0x10 && sig.genericArity == args.length && !args.isEmpty) + let fullName := if space.isEmpty then name else space ++ [46] ++ name + return { owner := .named { scope := bytesOf image.identity, name := fullName, + parts := [space,name], kind := .classType, pinned := true }, + name := ← stringAt (← (cell 3).toOption), convention := sig.convention, + genericArity := sig.genericArity, result := ← resolveScalar sig.genericArity sig.result, + parameters := ← sig.parameters.mapM (resolveScalar sig.genericArity), + definition := none, methodArguments := some args } + +private def primitiveArg : SigType → Option ResolvedType + | .primitive code => some (.primitive code) + | _ => none + +/-- Follow a MethodSpec through the MemberRef and its top-level TypeRef to the physical AssemblyRef. -/ +private def targetAt (caller : PreparedImage) (env : MetadataEnv) (frame : Frame) (token : Nat) + (slot : ImportSlot) : Option (String × List Nat × List ResolvedType) := do + guard (token / 2^24 == Table.methodSpec) + let spec := caller.layout.cell caller.view token + let parent ← (spec 0).toOption + guard (parent % 2 == 1) + let member := caller.layout.cell caller.view (StrictBinding.tokenOf Table.memberRef (parent/2)) + let owner ← (member 0).toOption + guard (owner % 8 == 1) + let ty := caller.layout.cell caller.view (StrictBinding.tokenOf Table.typeRef (owner/8)) + let resolution ← (ty 0).toOption + guard (resolution % 4 == 2) + let scope ← (assemblyIdentityAt caller.view caller.layout + (StrictBinding.tokenOf Table.assemblyRef (resolution/4)) 0).toOption + let stringAt := fun n => (caller.layout.stringAt caller.view n).toOption + let name ← stringAt (← (member 1).toOption) + let sig ← (caller.layout.blobAt caller.view (← (member 2).toOption)).toOption + let signature ← (decodeMethodSig sig).toOption + let blob ← (caller.layout.blobAt caller.view (← (spec 1).toOption)).toOption + let args ← (← (decodeMethodSpec blob).toOption).mapM primitiveArg + let site ← (env.importMethodSite token frame slot.identity).toOption + let .named named := site.owner | none + guard (named.parts == [← stringAt (← (ty 2).toOption), ← stringAt (← (ty 1).toOption)] && + named.scope == scope && site.name == name && site.methodArguments == some args && + signature.convention == site.convention && signature.genericArity == site.genericArity && + resolveScalar site.genericArity signature.result == some site.result && + signature.parameters.mapM (resolveScalar site.genericArity) == some site.parameters) + return (← spelled? scope, name, args) + +/-- Resolve each import at its decoded instruction boundary against both pinned images. + The only supported form here is a static generic method on a top-level class with primitive + closed arguments. Tiny and fat bodies both pass through the production body decoder. -/ +def resolveCalls (item : MethodEvidence) (prepared : List PreparedImage) : + Option (List (ImportSlot × String × Nat)) := do + if item.body.imports.calls.isEmpty then return [] + let caller ← (located prepared item.assembly.identity).toOption + let bytes := item.body.bytes + let _ ← (bodyBound caller item.original.token bytes).toOption + let env ← item.body.environment.toOption + let arity := item.original.genericParameters.length + let ownerArity := item.original.ownerParameters.length + let frame ← (env.frame arity ownerArity).toOption + let decoded ← (decodeBody env arity ownerArity item.body).toOption + let first ← bytes.head? + let header := if first % 4 == 2 then 1 else 12 + item.body.imports.calls.mapM fun (pc, slot) => do + let instruction ← decoded.code[pc]? + let .call site := instruction.operand | none + guard (instruction.opcode == 0x28 && site.target == .external slot.identity) + let 0x28 :: a :: b :: c :: d :: _ := bytes.drop (header + instruction.offset) | none + let (identity, name, arguments) ← targetAt caller env frame (a+256*(b+256*(c+256*d))) slot + let producer ← (located prepared identity).toOption + -- MethodPtr indirection and metadata in linked files extend the MethodDef search space. + guard (producer.layout.rowCount 5 == 0) + for n in [:producer.layout.rowCount 0x26] do + guard ((← (producer.layout.cellOfRow producer.view 0x26 (n+1) 0).toOption) == 1) + let mut candidates := [] + for r in [:producer.layout.rowCount Table.methodDef] do + let token := StrictBinding.tokenOf Table.methodDef (r+1) + let nameIndex ← (producer.layout.cell producer.view token 3).toOption + let declared ← (producer.layout.stringAt producer.view nameIndex).toOption + if declared == name then + let method ← physicalSite producer token arguments + if method.contractName frame == some (bytesOf slot.identity) then + candidates := token :: candidates + let [token] := candidates | none + return (slot, identity, token) + +/-- The expected publication for one import, including the consumer-selected sidecar bytes. + The independent audit binds the producer, sidecar and declaration closure digests. -/ +structure Publication where + identity : String + producer : String + implementation : String + interfaceDigest : String + profile : String + contractName : String + closureDigest : String + sidecar : ImageBytes + +/-- The selected public export agrees with this slot and its independently audited declaration. -/ +def exportMatches (input : Publication) (slot : ImportSlot) : Bool := Id.run do + let some root := ContractSidecar.read input.sidecar | return false + let some (.integer 1) := ContractSidecar.field root "schema" | return false + let some (.integer 2) := ContractSidecar.field root "formatVersion" | return false + let some (.boolean false) := ContractSidecar.field root "isReferenceAssembly" | return false + let some entry := ContractSidecar.member root slot.identity | return false + let some (.array profiles) := ContractSidecar.field entry "profiles" | return false + let some (.integer count) := ContractSidecar.field entry "parameterCount" | return false + let some (.boolean receiver) := ContractSidecar.field entry "hasThis" | return false + let some (.boolean result) := ContractSidecar.field entry "returnsValue" | return false + return input.profile == "scalar" && ContractSidecar.text root "assemblyIdentity" == some input.producer && + ContractSidecar.text root "implementationSha256" == some input.implementation && + ContractSidecar.text root "interfaceDigest" == some input.interfaceDigest && + ContractSidecar.text entry "visibility" == some "public" && + ContractSidecar.text entry "kind" == some "method" && + ContractSidecar.text entry "contractConstant" == some input.contractName && + ContractSidecar.text entry "closureDigest" == some slot.closureDigest && + slot.closureDigest == input.closureDigest && slot.identity == input.identity && + count == (slot.parameterCount : Int) && receiver == slot.hasThis && result == slot.returnsValue && + profiles.any (fun profile => match profile with | .string name => name == input.profile | _ => false) + +/-- All slots have one publication, a matching export and an exact target in the pinned producer + at each linked use. A missing use, unsupported signature or unavailable producer is incomplete. -/ +def check (images : List PinnedImage) (profile : FeatureProfile) (methods : List MethodEvidence) + (slots : List ImportSlot) (publications : List Publication) : Except AuditError Bool := do + if profile != FeatureProfile.scalar || slots.isEmpty || slots.length != publications.length then + return false + let prepared ← prepare images + let some uses := methods.mapM (fun item => resolveCalls item prepared) | return false + let targets := uses.flatten + return (slots.zip publications).all (fun (slot, publication) => + exportMatches publication slot && targets.any (fun use => + use.1 == slot && use.2.1 == publication.producer)) && + targets.all (fun use => (slots.zip publications).any (fun (slot, publication) => + use.1 == slot && use.2.1 == publication.producer)) + +/-- Each reported publication has a matching export and at least one use in the producer that + supplied it. Every reconstructed use belongs to one of those publications. -/ +theorem physical_of_check {images profile methods slots publications} + (checked : check images profile methods slots publications = .ok true) : + ∃ prepared uses, prepare images = .ok prepared ∧ + methods.mapM (fun item => resolveCalls item prepared) = some uses ∧ + (∀ pair ∈ slots.zip publications, exportMatches pair.2 pair.1 = true ∧ + ∃ target ∈ uses.flatten, target.1 = pair.1 ∧ target.2.1 = pair.2.producer) ∧ + (∀ target ∈ uses.flatten, ∃ pair ∈ slots.zip publications, + target.1 = pair.1 ∧ target.2.1 = pair.2.producer) := by + unfold check at checked + split at checked <;> try cases checked + obtain ⟨prepared, pinned, rest⟩ := CIL.Safety.except_bind_ok checked + cases read : methods.mapM (fun item => resolveCalls item prepared) with + | none => simp [read, pure, Except.pure] at rest + | some uses => + refine ⟨prepared, uses, pinned, read, ?_⟩ + simpa only [read, pure, Except.pure, Except.ok.injEq, Bool.and_eq_true, List.all_eq_true, + List.any_eq_true, beq_iff_eq] using rest +#print axioms physical_of_check + +end StrictBinding.GenericImports + +namespace StrictBinding +open CIL CIL.Extraction + +/-- The completeness reported for generic identities follows from the physical method resolution + and the selected exports of the same extraction. -/ +def GenericImportsChecked (images : List PinnedImage) (profile : FeatureProfile) (methods : List MethodEvidence) + (slots : List ImportSlot) (publications : List GenericImports.Publication) + (complete : Bool) : Prop := + (GenericImports.check images profile methods slots publications).toOption = some complete + +instance (images : List PinnedImage) (profile : FeatureProfile) (methods : List MethodEvidence) (slots : List ImportSlot) + (publications : List GenericImports.Publication) (complete : Bool) : + Decidable (GenericImportsChecked images profile methods slots publications complete) := + inferInstanceAs (Decidable (_ = _)) + +end StrictBinding diff --git a/StrictBinding/Identity.lean b/StrictBinding/Identity.lean new file mode 100644 index 000000000..7f394ec5c --- /dev/null +++ b/StrictBinding/Identity.lean @@ -0,0 +1,137 @@ +import StrictBinding.Tables +import CIL.Crypto.Sha1 + +/-! +# Heaps and assembly identity + +Reads entries of the `#Strings` and `#Blob` heaps and spells the identity of the assembly an +image defines from its Assembly row, in the form the extractor records: +`Name, Version=a.b.c.d, Culture=neutral, PublicKeyToken=0123456789abcdef`. The token is the +strong-name token computed from the full key of a definition, or `null` for an unsigned assembly. +An AssemblyRef may instead store an eight-byte token. +-/ + +namespace StrictBinding + +/-- The bytes of the NUL-terminated string at `key` in the `#Strings` heap. -/ +def MetadataLayout.stringAt (layout : MetadataLayout) (view : ImageView) (key : Nat) : + Except AuditError (List Nat) := do + let heap := layout.pe.strings + if key >= heap.size then throw (.tables (.outsideHeap key)) + readName view (heap.start + key) (.tables (.outsideHeap key)) (heap.size - key) + +/-- The length prefix of a blob (II.24.2.4): one byte below `0x80`, two bytes below `0xC0` and + four bytes below `0xE0`. The result is the bytes of the prefix and the length it states. -/ +def blobPrefix (view : ImageView) (offset : Nat) : Except AuditError (Nat × Nat) := do + let first ← view.u offset 1 + if first < 0x80 then pure (1, first) + else if first < 0xC0 then pure (2, (first - 0x80) * 2 ^ 8 + (← view.slice (offset + 1) 1)) + else if first < 0xE0 then pure (4, (first - 0xC0) * 2 ^ 24 + (← view.slice (offset + 1) 3)) + else throw (.tables (.outsideHeap offset)) + +/-- The bytes of the blob at `key` in the `#Blob` heap, which must lie inside the heap. -/ +def MetadataLayout.blobAt (layout : MetadataLayout) (view : ImageView) (key : Nat) : + Except AuditError (List Nat) := do + let heap := layout.pe.blobs + if key >= heap.size then throw (.tables (.outsideHeap key)) + let (prefixBytes, length) ← blobPrefix view (heap.start + key) + if key + prefixBytes + length > heap.size then throw (.tables (.outsideHeap key)) + view.bytes (heap.start + key + prefixBytes) length + +/-- The code points of well-formed UTF-8 bytes: shortest forms only, no surrogates. -/ +def utf8CodePoints : Nat → List Nat → Option (List Nat) + | 0, bytes => if bytes.isEmpty then some [] else none + | _ + 1, [] => some [] + | fuel + 1, first :: rest => + let continuation (byte : Nat) : Bool := byte / 64 == 2 + let more (point : Nat) (remaining : List Nat) : Option (List Nat) := + (utf8CodePoints fuel remaining).map (point :: ·) + if first < 0x80 then more first rest + else if first / 32 == 6 then + match rest with + | second :: remaining => + let point := first % 32 * 64 + second % 64 + if continuation second && 0x80 <= point then more point remaining else none + | [] => none + else if first / 16 == 14 then + match rest with + | second :: third :: remaining => + let point := (first % 16 * 64 + second % 64) * 64 + third % 64 + if continuation second && continuation third && 0x800 <= point && + (point < 0xD800 || 0xDFFF < point) then more point remaining + else none + | _ => none + else if first / 8 == 30 then + match rest with + | second :: third :: fourth :: remaining => + let point := ((first % 8 * 64 + second % 64) * 64 + third % 64) * 64 + fourth % 64 + if continuation second && continuation third && continuation fourth && + 0x10000 <= point && point < 0x110000 then more point remaining + else none + | _ => none + else none + +/-- The code points of a string heap entry. -/ +def MetadataLayout.stringText (layout : MetadataLayout) (view : ImageView) (key : Nat) : + Except AuditError (List Nat) := do + let bytes ← layout.stringAt view key + let some points := utf8CodePoints bytes.length bytes | throw (.tables .invalidUtf8) + pure points + +/-- The code points of a text, which are ASCII for the identities audited here. -/ +def asciiOf (text : String) : List Nat := text.toList.map Char.toNat + +/-- The ASCII decimal digits of a version component, which is a 16-bit number. -/ +def decimal (value : Nat) : List Nat := + let places := (List.range 5).map fun place => 48 + value / 10 ^ (4 - place) % 10 + match places.dropWhile (· == 48) with + | [] => [48] + | digits => digits + +/-- `count` lowercase hexadecimal digits of a number, as ASCII. -/ +def hexDigits (count value : Nat) : List Nat := + (List.range count).map fun place => + let digit := value / 16 ^ (count - 1 - place) % 16 + if digit < 10 then 48 + digit else 87 + digit + +/-- A definition has its full public key in Assembly.PublicKey, or no publisher key. -/ +def publisherToken (key : List Nat) : Option Nat := + if key.isEmpty then none else some (CIL.Crypto.Sha1.keyToken key) + +/-- AssemblyRef.PublicKeyOrToken contains a full key, an eight-byte token, or no key. -/ +def assemblyKeyToken (flags : Nat) (key : List Nat) : Except AuditError (Option Nat) := do + if key.isEmpty then return none + if flags % 2 == 1 then return some (CIL.Crypto.Sha1.keyToken key) + let width := key.length + if width != 8 then throw (.tables (.invalidPublicKeyToken width)) + pure (some (CIL.Crypto.bytesValue key)) + +/-- The key column of an Assembly row holds a full key. AssemblyRef flags select its encoding. -/ +def rowKeyToken (row flags : Nat) (key : List Nat) : Except AuditError (Option Nat) := + if row == tokenOf 0x20 1 then .ok (publisherToken key) else assemblyKeyToken flags key + +/-- The identity columns shared by Assembly and AssemblyRef, starting at the major version. -/ +def assemblyIdentityAt (view : ImageView) (layout : MetadataLayout) (row first : Nat) : + Except AuditError (List Nat) := do + let cell := fun column => layout.cell view row (first + column) + let major ← cell 0 + let minor ← cell 1 + let build ← cell 2 + let revision ← cell 3 + let flags ← cell 4 + let key ← layout.blobAt view (← cell 5) + let name ← layout.stringText view (← cell 6) + let culture ← layout.stringText view (← cell 7) + let version := decimal major ++ [46] ++ decimal minor ++ [46] ++ decimal build ++ [46] ++ + decimal revision + let token ← rowKeyToken row flags key + let token := token.map (hexDigits 16) |>.getD (asciiOf "null") + pure (name ++ asciiOf ", Version=" ++ version ++ asciiOf ", Culture=" ++ + (if culture.isEmpty then asciiOf "neutral" else culture) ++ asciiOf ", PublicKeyToken=" ++ + token) + +/-- The code points of the identity the Assembly row spells. -/ +def assemblyIdentity (view : ImageView) (layout : MetadataLayout) : Except AuditError (List Nat) := + assemblyIdentityAt view layout (tokenOf 0x20 1) 1 + +end StrictBinding diff --git a/StrictBinding/Image.lean b/StrictBinding/Image.lean new file mode 100644 index 000000000..ea7837cf0 --- /dev/null +++ b/StrictBinding/Image.lean @@ -0,0 +1,138 @@ +import StrictBinding.Error +import CIL.Crypto.Words + +/-! +# Image bytes and reads + +An assembly file reaches the audit as an `ImageBytes` constant: the file zero-padded to a +multiple of 64 bytes, as big-endian 64-byte chunks in pages of at most 64 chunks. The chunks are +what SHA-256 hashes (`StrictBinding.Sha256`), and the same chunks, arranged once into a balanced +tree, are what every container and table read decodes. Both uses read one constant, so a digest +of the constant is a digest of the bytes that are decoded. + +`ImageBytes.wellFormed` is required by every check: a chunk at or above `2 ^ 512` would hash one +way and read another. A read touches only the chunks that hold its bytes, found in time +logarithmic in the image size, so the cost of a check grows with the number of reads and the +bytes they cover, not with the image size per read. Values are joined pairwise, so joining `n` +bytes costs `n log n` rather than `n ^ 2`. +-/ + +namespace StrictBinding + +open CIL.Crypto (joinValues bytesValue littleEndian chunkValues) + +/-- The bits in one 64-byte chunk. -/ +def chunkBits : Nat := 512 + +/-- The most chunks a page holds, so literals and nesting stay small. -/ +def pageChunks : Nat := 64 + +/-- A file zero-padded to a multiple of 64 bytes, as big-endian chunks in pages. -/ +structure ImageBytes where + /-- The length of the file in bytes, before padding. -/ + length : Nat + pages : List (List Nat) + +/-- The chunks of all pages, in file order. -/ +def ImageBytes.chunks (image : ImageBytes) : List Nat := + image.pages.foldr (fun page rest => page ++ rest) [] + +/-- The chunk count matches the length, every page is nonempty and at most `pageChunks` long, + every chunk is below `2 ^ 512` and the padding after the last byte is zero. -/ +def ImageBytes.wellFormed (image : ImageBytes) : Bool := + let chunks := image.chunks + chunks.length == (image.length + 63) / 64 && + image.pages.all (fun page => !page.isEmpty && page.length <= pageChunks) && + chunks.all (· < 2 ^ chunkBits) && + chunks.getLastD 0 % 2 ^ (8 * (64 * chunks.length - image.length)) == 0 + +/-- The chunks of an image in a balanced tree, each node knowing how many chunks it holds. -/ +inductive ChunkTree where + | leaf (chunk : Nat) + | node (count : Nat) (left right : ChunkTree) + +/-- The number of chunks in a tree. -/ +def ChunkTree.count : ChunkTree → Nat + | .leaf _ => 1 + | .node count _ _ => count + +/-- The chunk at an index, found by descending one subtree per level. -/ +def ChunkTree.at : ChunkTree → Nat → Nat + | .leaf chunk, _ => chunk + | .node _ left right, index => + if index < left.count then left.at index else right.at (index - left.count) + +/-- Adjacent trees joined under one node each. -/ +def pairTrees : List ChunkTree → List ChunkTree + | left :: right :: rest => .node (left.count + right.count) left right :: pairTrees rest + | trees => trees + +/-- One tree over all the given trees, built level by level. -/ +def buildTree : Nat → List ChunkTree → Option ChunkTree + | 0, trees => trees.head? + | fuel + 1, trees => + match trees with + | [] => none + | [single] => some single + | _ => buildTree fuel (pairTrees trees) + +/-- A checked image, ready to be read. -/ +structure ImageView where + length : Nat + chunks : Option ChunkTree + +/-- The view of a well-formed image. -/ +def ImageView.ofImage (image : ImageBytes) : Except AuditError ImageView := + if image.wellFormed then + let chunks := image.chunks + .ok { length := image.length, chunks := buildTree chunks.length (chunks.map .leaf) } + else .error .malformedImage + +/-- The big-endian value of `count` bytes at `offset`: the chunks that hold them, joined, with + the bytes after them shifted out. -/ +def ImageView.slice (view : ImageView) (offset count : Nat) : Except AuditError Nat := + match view.chunks with + | some tree => + if offset + count <= view.length then + if count == 0 then .ok 0 + else + let first := offset / 64 + let last := (offset + count - 1) / 64 + let held := (List.range (last - first + 1)).map fun index => + (tree.at (first + index), chunkBits) + let trailing := 8 * (64 * (last + 1) - (offset + count)) + .ok (Nat.mod (Nat.shiftRight (joinValues held.length held).1 trailing) (2 ^ (8 * count))) + else .error (.outsideImage offset count) + | none => + if count == 0 && offset <= view.length then .ok 0 else .error (.outsideImage offset count) + +/-- The little-endian unsigned integer of `count` bytes at `offset`, as PE and metadata store + integers. -/ +def ImageView.u (view : ImageView) (offset count : Nat) : Except AuditError Nat := + (view.slice offset count).map (littleEndian count) + +/-- The bytes at `offset` are exactly `bytes`, each a byte value. -/ +def ImageView.hasBytesAt (view : ImageView) (offset : Nat) (bytes : List Nat) : + Except AuditError Bool := do + pure (bytes.all (· < 256) && (← view.slice offset bytes.length) == bytesValue bytes) + +/-- The `count` bytes at `offset` as a byte list. -/ +def ImageView.bytes (view : ImageView) (offset count : Nat) : Except AuditError (List Nat) := + (List.range count).mapM fun index => view.slice (offset + index) 1 + +/-- Splits a list into pages of at most `pageChunks` items. -/ +def paginate : Nat → List Nat → List (List Nat) + | 0, _ => [] + | fuel + 1, chunks => + if chunks.isEmpty then [] else chunks.take pageChunks :: paginate fuel (chunks.drop pageChunks) + +/-- The image of a file given as bytes, for tests and small inputs. -/ +def ImageBytes.ofBytes (bytes : List Nat) : ImageBytes := + { length := bytes.length, pages := paginate bytes.length (chunkValues bytes.length bytes) } + +/-- An image together with the assembly identity it must spell. -/ +structure PinnedImage where + identity : String + image : ImageBytes + +end StrictBinding diff --git a/StrictBinding/LayoutContent.lean b/StrictBinding/LayoutContent.lean new file mode 100644 index 000000000..d438ae1e9 --- /dev/null +++ b/StrictBinding/LayoutContent.lean @@ -0,0 +1,114 @@ +import StrictBinding.GenericConstraints +import StrictBinding.StaticData + +/-! Reconstruct explicit and sequential scalar value layouts from the pinned PE metadata. -/ + +namespace StrictBinding + +open CIL.Extraction + +/-- ECMA-335 FieldLayout stores the offset and the owning Field. -/ +private def fieldLayoutTable := 0x10 + +/-- Several attributes or interface implementations may legitimately name the same owner. -/ +private def hasLayoutOwner (image : PreparedImage) (table owner : Nat) : + Except AuditError Bool := do + for index in [:image.layout.rowCount table] do + if (← image.layout.cellOfRow image.view table (index + 1) 0) == owner then return true + return false + +/-- Reconstruct a supported value layout using only its scoped name and physical metadata. + Custom attributes, interfaces, generics, automatic layouts and nonscalar fields are partial. -/ +def physicalLayout (images : List PreparedImage) (identity : String) : + Except AuditError (Option RawLayout) := do + let some (scope, name) := namedType (.aggregate identity 0) | return none + let some image := images.find? (·.identity == String.ofList scope) | return none + let (owner, arity) ← match valueLayoutOwner image identity name with + | .error .uncoveredConstraint => return none + | other => other + unless arity == 0 do return none + let flags ← image.layout.cellOfRow image.view Table.typeDef owner typeDefFlagsColumn + let layoutKind := flags &&& 0x18 + let explicit := layoutKind == 0x10 + unless explicit || layoutKind == 0x08 do return none + -- HasCustomAttribute uses tag 3 for TypeDef. InterfaceImpl uses the TypeDef row directly. + if ← hasLayoutOwner image 0x0c (owner * 32 + 3) then return none + if ← hasLayoutOwner image 0x09 owner then return none + let classes ← dataTableIndex image classLayoutTable 2 Table.typeDef + let (packing, declaredSize) ← match classes[StrictBinding.tokenOf Table.typeDef owner]? with + | none => pure ((-1 : Int), (-1 : Int)) + | some row => do + let cell := image.layout.cellOfRow image.view classLayoutTable row + pure (Int.ofNat (← cell 0), Int.ofNat (← cell 1)) + let offsets ← dataTableIndex image fieldLayoutTable 1 Table.field + let (first, finish) ← platformFieldRange image owner + let mut fields := #[] + for field in [first:finish] do + let cell := image.layout.cellOfRow image.view Table.field field + let fieldFlags ← cell 0 + if fieldFlags &&& 0x10 != 0 then continue + if fieldFlags &&& 0x1000 != 0 then return none + let signature ← image.layout.blobAt image.view (← cell 2) + let .ok (.primitive code) := decodeFieldSig signature | return none + let some storage := ((primitiveName code).bind spelled?).bind primitiveField | return none + let row := offsets[StrictBinding.tokenOf Table.field field]? + let offset ← row.mapM fun index => image.layout.cellOfRow image.view fieldLayoutTable index 0 + unless offset.isSome == explicit do throw (.layoutMismatch identity) + fields := fields.push { type := storage, offset } + return some { identity, explicit, packing, declaredSize, fields := fields.toList } + +/-- A supported physical layout must agree in every field, including its flags and interfaces. -/ +private def checkOneLayout (images : List PreparedImage) (layout : RawLayout) : + Except AuditError Bool := do + let some actual ← physicalLayout images layout.identity | return false + if actual = layout then return true + throw (.layoutMismatch layout.identity) + +/-- Check every raw layout, even when a different layout or an import leaves coverage partial. -/ +def checkLayoutContent (images : List PinnedImage) (layouts : List RawLayout) + (imports : List CIL.ImportSlot) : Except AuditError Bool := do + if layouts.isEmpty then return false + let prepared ← prepare images + let coverage ← layouts.mapM (checkOneLayout prepared) + return imports.isEmpty && coverage.all id + +/-- The consumer's coverage bit is the result of checking the entire extraction's layout list. -/ +def LayoutContentChecked (images : List PinnedImage) (layouts : List RawLayout) + (imports : List CIL.ImportSlot) (complete : Bool) : Prop := + (checkLayoutContent images layouts imports).toOption = some complete + +instance (images : List PinnedImage) (layouts : List RawLayout) (imports : List CIL.ImportSlot) + (complete : Bool) : Decidable (LayoutContentChecked images layouts imports complete) := + inferInstanceAs (Decidable (_ = _)) + +/-- Each raw input to the certified size and offset computation comes from its physical image. -/ +theorem LayoutContentChecked.physical {images : List PinnedImage} {layouts : List RawLayout} + {imports : List CIL.ImportSlot} (checked : LayoutContentChecked images layouts imports true) : + ∃ prepared, prepare images = .ok prepared ∧ + ∀ layout ∈ layouts, physicalLayout prepared layout.identity = .ok (some layout) := by + have done : checkLayoutContent images layouts imports = .ok true := by + cases result : checkLayoutContent images layouts imports with + | error err => simp [LayoutContentChecked, result, Except.toOption] at checked + | ok value => simpa [LayoutContentChecked, result, Except.toOption] using checked + unfold checkLayoutContent at done + split at done + · cases done + · obtain ⟨prepared, built, done⟩ := CIL.Safety.except_bind_ok done + obtain ⟨coverage, read, done⟩ := CIL.Safety.except_bind_ok done + refine ⟨prepared, built, ?_⟩ + intro layout member + obtain ⟨value, found, result⟩ := read_member member read + have yes : value = true := + List.all_eq_true.mp (Bool.and_eq_true_iff.mp (Except.ok.inj done)).2 value found + subst value + unfold checkOneLayout at result + obtain ⟨actual, physical, compared⟩ := CIL.Safety.except_bind_ok result + cases actual with + | none => cases compared + | some actual => + by_cases same : actual = layout + · exact same ▸ physical + · simp [same, throw] at compared +#print axioms LayoutContentChecked.physical + +end StrictBinding diff --git a/StrictBinding/ObjectFields.lean b/StrictBinding/ObjectFields.lean new file mode 100644 index 000000000..01def29d2 --- /dev/null +++ b/StrictBinding/ObjectFields.lean @@ -0,0 +1,80 @@ +import StrictBinding.PlatformType +import CIL.Extraction.Translation + +/-! +# Instance fields of System.Object + +Read the full identity of each platform scope from the certified method evidence. In each +corresponding image, find the root class by name and check the flags of every field it owns. +-/ + +namespace StrictBinding + +open CIL.Extraction + +/-- System.Object has no base type, and all its physically declared fields are static. -/ +private def objectFields (image : PreparedImage) (row : Nat) : Except AuditError Unit := do + let base ← image.layout.cellOfRow image.view Table.typeDef row typeDefExtendsColumn + if base != 0 then throw (.invalidObjectDefinition image.identity) + let (first, finish) ← platformFieldRange image row + for field in [first:finish] do + let flags ← image.layout.cellOfRow image.view Table.field field 0 + if flags &&& 0x10 == 0 then throw (.objectInstanceField image.identity field) + +/-- The exact identities of the core scopes retained in one slice, including its primitive + type scope. The identity includes version, culture and publisher. -/ +private def objectScopes (slice : MetadataSlice) (ownCore : Bool) : + Except MetadataError (List (List Nat)) := do + let index ← slice.index + let identity ← match ← index.corlibIdentity with + | some name => pure name + | none => if ownCore then pure (asciiOf slice.assembly.identity) + else throw (.unsupportedAssemblyRef 0) + let mut scopes := [identity] + for (token, _) in slice.rows do + if CIL.Extraction.metadataTableOf token == Table.assemblyRef then + if ← index.coreAssemblyRef token then + let scope ← index.assemblyRefIdentity token + unless scopes.contains scope do scopes := scope :: scopes + return scopes + +/-- Every core scope used in every method's metadata has a pinned defining image. -/ +def checkObjectFields (images : List PinnedImage) (evidence : List MethodEvidence) : + Except AuditError Bool := do + let prepared ← prepare images + let mut scopes : Std.TreeSet (List Nat) := {} + for item in evidence do + for slice in item.body.metadata.slices do + let ownCore ← + if slice.corlib != 0 then pure false + else do + let source ← located prepared slice.assembly.identity + platformAssembly source + let .ok names := objectScopes slice ownCore + | throw (.invalidObjectScope slice.assembly.identity) + for name in names do scopes := scopes.insert name + if scopes.isEmpty then throw (.invalidObjectScope "") + let mut complete := true + for name in scopes do + let identity := String.ofList (name.map Char.ofNat) + let covered ← resolvePlatformType prepared "Object" .invalidObjectDefinition objectFields + identity (prepared.length + 1) + complete := complete && covered + return complete + +/-- Every platform root class selected by the certified methods declares no instance fields. -/ +def ObjectFieldsBound (images : List PinnedImage) (evidence : List MethodEvidence) : Prop := + (checkObjectFields images evidence).toOption = some true + +instance (images : List PinnedImage) (evidence : List MethodEvidence) : + Decidable (ObjectFieldsBound images evidence) := inferInstanceAs (Decidable (_ = some true)) + +/-- Every supplied image reachable through a core scope or its forwarders agrees with the + extraction about System.Object. Some destination images may be absent. -/ +def SuppliedObjectFieldsValid (images : List PinnedImage) (evidence : List MethodEvidence) : Prop := + ok (checkObjectFields images evidence) = true + +instance (images : List PinnedImage) (evidence : List MethodEvidence) : + Decidable (SuppliedObjectFieldsValid images evidence) := inferInstanceAs (Decidable (_ = true)) + +end StrictBinding diff --git a/StrictBinding/Pe.lean b/StrictBinding/Pe.lean new file mode 100644 index 000000000..b073fd635 --- /dev/null +++ b/StrictBinding/Pe.lean @@ -0,0 +1,186 @@ +import StrictBinding.Image + +/-! +# PE container + +Decodes the parts of a PE file that locate CLI metadata: the DOS header, the PE and optional +headers, the section table, the CLI header, the metadata root and its stream headers. Every +offset is a file offset computed from the image constant itself, so a body or row claimed at a +relative virtual address is read where the section table maps that address. + +`mapRva` follows the rule the extractor applies to static data: exactly one section holds the +address, and the bytes stay inside both the section's raw data and its mapped size. +-/ + +namespace StrictBinding + +/-- A section header: its mapped extent and where its raw data sits in the file. -/ +structure Section where + virtualSize : Nat + virtualAddress : Nat + rawSize : Nat + rawPointer : Nat + +/-- A metadata stream: the file offset of its first byte and its size. -/ +structure Stream where + start : Nat + size : Nat + +/-- The positions in the file that locate metadata. Streams the file lacks have size zero. -/ +structure PeLayout where + sections : List Section + /-- The file offset of the metadata root. -/ + metadata : Nat + metadataSize : Nat + /-- The `#~` stream of tables. -/ + tables : Stream + strings : Stream + userStrings : Stream + guids : Stream + blobs : Stream + +/-- The streams of a metadata root as they are read: each at most once. -/ +structure StreamSet where + tables : Option Stream := none + strings : Option Stream := none + userStrings : Option Stream := none + guids : Option Stream := none + blobs : Option Stream := none + +/-- The file holds this relative virtual address in the section's extent. -/ +def Section.contains (header : Section) (rva : Nat) : Bool := + header.virtualAddress <= rva && + rva < header.virtualAddress + Nat.max header.virtualSize header.rawSize + +/-- The file offset of `count` bytes at a relative virtual address: exactly one section holds the + address, and the bytes end inside its raw data and its mapped size. -/ +def mapRva (sections : List Section) (rva count : Nat) : Except AuditError Nat := + match sections.filter (·.contains rva) with + | [header] => + let rawEnd := header.virtualAddress + header.rawSize + let mappedEnd := + if header.virtualSize == 0 then rawEnd else header.virtualAddress + header.virtualSize + if rva + count <= Nat.min rawEnd mappedEnd then + .ok (header.rawPointer + (rva - header.virtualAddress)) + else .error (.pe (.unmappedRva rva count)) + | [] => .error (.pe (.unmappedRva rva count)) + | _ => .error (.pe (.ambiguousRva rva)) + +/-- The bytes of a section header: sizes and pointers at offsets 8 to 20. -/ +def sectionHeaderBytes : Nat := 40 + +/-- The `count` section headers from `offset`. -/ +def readSections (view : ImageView) (offset : Nat) : Nat → Except AuditError (List Section) + | 0 => pure [] + | count + 1 => do + let virtualSize ← view.u (offset + 8) 4 + let virtualAddress ← view.u (offset + 12) 4 + let rawSize ← view.u (offset + 16) 4 + let rawPointer ← view.u (offset + 20) 4 + let rest ← readSections view (offset + sectionHeaderBytes) count + pure ({ virtualSize, virtualAddress, rawSize, rawPointer } :: rest) + +/-- The longest stream name, with its terminator, the metadata format allows. -/ +def streamNameBytes : Nat := 32 + +/-- The bytes of a NUL-terminated name at `offset`, without the terminator; `missing` is the error + when no terminator ends the name within the allowed length. -/ +def readName (view : ImageView) (offset : Nat) (missing : AuditError) : + Nat → Except AuditError (List Nat) + | 0 => throw missing + | fuel + 1 => do + let byte ← view.u offset 1 + if byte == 0 then pure [] + else + let rest ← readName view (offset + 1) missing fuel + pure (byte :: rest) + +/-- Records a stream under its name once; a repeated or unknown name is rejected. -/ +def StreamSet.add (streams : StreamSet) (name : List Nat) (stream : Stream) : + Except AuditError StreamSet := + match name with + | [0x23, 0x7E] => + if streams.tables.isSome then throw (.pe .badStream) + else pure { streams with tables := some stream } + | [0x23, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x73] => + if streams.strings.isSome then throw (.pe .badStream) + else pure { streams with strings := some stream } + | [0x23, 0x55, 0x53] => + if streams.userStrings.isSome then throw (.pe .badStream) + else pure { streams with userStrings := some stream } + | [0x23, 0x47, 0x55, 0x49, 0x44] => + if streams.guids.isSome then throw (.pe .badStream) + else pure { streams with guids := some stream } + | [0x23, 0x42, 0x6C, 0x6F, 0x62] => + if streams.blobs.isSome then throw (.pe .badStream) + else pure { streams with blobs := some stream } + | _ => throw (.pe .badStream) + +/-- The stream headers from `offset`: each stream must lie inside the `metadataSize` bytes of the + root that starts at `root`. -/ +def readStreams (view : ImageView) (root metadataSize : Nat) : + Nat → Nat → StreamSet → Except AuditError StreamSet + | 0, _, streams => pure streams + | count + 1, offset, streams => do + let streamOffset ← view.u offset 4 + let size ← view.u (offset + 4) 4 + let name ← readName view (offset + 8) (.pe .badMetadataRoot) streamNameBytes + if streamOffset + size > metadataSize then throw (.pe .badMetadataRoot) + let streams ← streams.add name { start := root + streamOffset, size } + let headerBytes := 8 + (name.length + 4) / 4 * 4 + readStreams view root metadataSize count (offset + headerBytes) streams + +/-- The empty stream of a heap the file lacks. -/ +def Stream.absent : Stream := { start := 0, size := 0 } + +/-- The offset of the optional header's data directories: 96 bytes into a PE32 header and 112 into + a PE32+ header. -/ +def directoriesOffset (optional magic : Nat) : Except AuditError Nat := + if magic == 0x10B then pure (optional + 96) + else if magic == 0x20B then pure (optional + 112) + else throw (.pe (.badMagic magic)) + +/-- The most bytes a metadata root allocates to its version string (II.24.2.1): the string with its + terminator is at most 255 bytes, and the allocation rounds it up to a multiple of four. -/ +def maxVersionLength : Nat := 256 + +/-- The CLI header's data directory is the fifteenth of sixteen, eight bytes each. -/ +def cliDirectoryIndex : Nat := 14 + +/-- The bytes of a CLI header. -/ +def cliHeaderBytes : Nat := 72 + +/-- The CLI header and metadata root of an image, located through the section table. -/ +def decodePe (view : ImageView) : Except AuditError PeLayout := do + if (← view.u 0 2) != 0x5A4D then throw (.pe .notDos) + let pe ← view.u 0x3C 4 + if (← view.u pe 4) != 0x4550 then throw (.pe (.badSignature pe)) + let sectionCount ← view.u (pe + 6) 2 + let optionalSize ← view.u (pe + 20) 2 + let optional := pe + 24 + let directories ← directoriesOffset optional (← view.u optional 2) + let directoryEnd := directories + 8 * (cliDirectoryIndex + 1) + let directoryCount ← view.u (directories - 4) 4 + if directoryCount <= cliDirectoryIndex || directoryEnd > optional + optionalSize then + throw (.pe .noCliHeader) + let sections ← readSections view (optional + optionalSize) sectionCount + let cliEntry := directories + 8 * cliDirectoryIndex + let cli ← mapRva sections (← view.u cliEntry 4) cliHeaderBytes + if (← view.u (cliEntry + 4) 4) < cliHeaderBytes || (← view.u cli 4) != cliHeaderBytes then + throw (.pe .badCliHeader) + let metadataSize ← view.u (cli + 12) 4 + let metadata ← mapRva sections (← view.u (cli + 8) 4) metadataSize + if (← view.u metadata 4) != 0x424A5342 then throw (.pe .badMetadataRoot) + let versionLength ← view.u (metadata + 12) 4 + if versionLength % 4 != 0 || versionLength > maxVersionLength then + throw (.pe .badMetadataRoot) + let streamCount ← view.u (metadata + 18 + versionLength) 2 + let streams ← readStreams view metadata metadataSize streamCount + (metadata + 20 + versionLength) {} + let some tables := streams.tables | throw (.pe .badMetadataRoot) + pure { sections, metadata, metadataSize, tables, + strings := streams.strings.getD Stream.absent, + userStrings := streams.userStrings.getD Stream.absent, + guids := streams.guids.getD Stream.absent, blobs := streams.blobs.getD Stream.absent } + +end StrictBinding diff --git a/StrictBinding/PlatformEnums.lean b/StrictBinding/PlatformEnums.lean new file mode 100644 index 000000000..8ddfd5723 --- /dev/null +++ b/StrictBinding/PlatformEnums.lean @@ -0,0 +1,116 @@ +import StrictBinding.PlatformType +import CIL.Extraction.PlatformDefinitions + +/-! Read an enum's exact integer storage from the Field rows owned by its pinned TypeDef. -/ + +namespace StrictBinding + +open CIL.Extraction + +/-- The one non-static field must be the runtime special field named value__. + Its entire field signature must consist of the FIELD tag and an integer element type. -/ +def physicalEnumStorage (image : PreparedImage) (row : Nat) : Except AuditError Nat := do + let (first, finish) ← platformFieldRange image row + let mut result := none + for field in [first:finish] do + let cell := image.layout.cellOfRow image.view Table.field field + let flags ← cell 0 + if flags &&& 0x10 != 0 then continue + if result.isSome || flags &&& 0x600 != 0x600 || + !(← stringFound image (← cell 1) (asciiOf "value__" ++ [0])) then + throw (.typeDefMismatch row .fields) + let [0x06, code] ← image.layout.blobAt image.view (← cell 2) + | throw (.typeDefMismatch row .fields) + unless 0x04 ≤ code && code ≤ 0x0b do throw (.typeDefMismatch row .fields) + result := some code + let some code := result | throw (.typeDefMismatch row .fields) + return code + +/-- The enclosing TypeDef row, or zero for a top-level declaration. -/ +private def enumParent (image : PreparedImage) (row flags : Nat) : Except AuditError Nat := do + let mut parent := 0 + for i in [:nestedCount image] do + if (← nestedTypeOf image (i + 1)) == row then + let enclosing ← enclosingOf image (i + 1) + unless parent == 0 && 0 < enclosing && enclosing < row do + throw (.typeDefMismatch row .nesting) + parent := enclosing + unless (visibilityOf flags < nestedVisibility) == (parent == 0) do + throw (.typeDefMismatch row .nesting) + return parent + +/-- Find a unique type at this level, retaining namespace and simple name as separate bytes. -/ +private def enumChild? (image : PreparedImage) (parent : Nat) (space name : List Nat) : + Except AuditError (Option Nat) := do + if name.isEmpty || name.contains 0 || space.contains 0 then throw (.typeDefMismatch parent .typeName) + let cell := image.layout.cellOfRow image.view Table.typeDef + let mut found := none + for i in [:image.layout.rowCount Table.typeDef] do + let row := i + 1 + unless ← stringFound image (← cell row typeNameColumn) (name ++ [0]) do continue + unless ← stringFound image (← cell row typeNamespaceColumn) (space ++ [0]) do continue + unless (← enumParent image row (← cell row typeDefFlagsColumn)) == parent do continue + if found.isSome then throw (.typeDefMismatch row .typeName) + found := some row + return found + +/-- Locate each namespace/name pair from the outermost definition to the innermost. -/ +def enumDefinition? (image : PreparedImage) (parent : Nat) : + List (List Nat) → Except AuditError (Option Nat) + | space :: name :: rest => do + let some row ← enumChild? image parent space name | return none + if rest.isEmpty then return some row + enumDefinition? image row rest + | _ => .error (.typeDefMismatch parent .typeName) + +/-- The resolved row is a non-interface with a direct base in the recognized core assembly. -/ +def physicalEnumCode (image : PreparedImage) (row : Nat) : Except AuditError Nat := do + let cell := image.layout.cellOfRow image.view Table.typeDef row + let flags ← cell typeDefFlagsColumn + let baseValue ← cell typeDefExtendsColumn + let table ← match baseValue % 4 with + | 0 => pure Table.typeDef + | 1 => pure Table.typeRef + | _ => throw (.typeDefMismatch row .base) + let base ← checkedTokenOf table (baseValue / 4) .rowOverflow + let claim := BaseType.named base systemNamespace enumName + baseBound image row baseValue claim + if table == Table.typeDef then + let baseFlags ← image.layout.cell image.view base typeDefFlagsColumn + unless (← enumParent image (baseValue / 4) baseFlags) == 0 do throw (.typeDefMismatch row .base) + unless (← kindOf image flags [] [] claim) == .enum do throw (.typeDefMismatch row .kind) + physicalEnumStorage image row + +/-- Check an enum in the supplied defining image against the entry the method decoder can use. -/ +def directEnumBound (image : PreparedImage) (entry : PlatformDefinition) : Except AuditError Unit := do + unless bytesOf image.identity == entry.scope do throw (.typeDefMismatch 0 .typeName) + let .enumType code _ := entry.kind | throw (.typeDefMismatch 0 .kind) + let some row ← enumDefinition? image 0 entry.parts | throw (.typeDefMismatch 0 .typeName) + unless (← physicalEnumCode image row) == code do throw (.typeDefMismatch row .fields) + +/-- A checked entry identifies the physical row and its declared storage code in this image. -/ +theorem directEnumBound_row {image : PreparedImage} {entry : PlatformDefinition} {code arity : Nat} + (kind : entry.kind = .enumType code arity) (checked : directEnumBound image entry = .ok ()) : + bytesOf image.identity = entry.scope ∧ ∃ row, enumDefinition? image 0 entry.parts = .ok (some row) ∧ + physicalEnumCode image row = .ok code := by + unfold directEnumBound at checked + simp only [kind, bind, Except.bind] at checked + split at checked <;> try contradiction + rename_i scope + cases found : enumDefinition? image 0 entry.parts with + | error error => simp [found] at checked + | ok result => + cases result with + | none => simp [found] at checked + | some row => + cases read : physicalEnumCode image row with + | error error => simp [found, read] at checked + | ok actual => + simp [found, read] at checked + split at checked + · rename_i same + exact ⟨by simpa using scope, row, rfl, same ▸ read⟩ + · contradiction +#print axioms directEnumBound_row + +end StrictBinding diff --git a/StrictBinding/PlatformKinds.lean b/StrictBinding/PlatformKinds.lean new file mode 100644 index 000000000..0abe5eadf --- /dev/null +++ b/StrictBinding/PlatformKinds.lean @@ -0,0 +1,183 @@ +import StrictBinding.DefinitionRoute +import CIL.Extraction.Translation + +/-! Check the TypeDef and GenericParam rows of an externally referenced type. -/ + +namespace StrictBinding + +open CIL.Extraction + +/-- The ordinal of a GenericParam owned by a TypeDef is its position among that owner's rows. -/ +def physicalTypeArity (image : PreparedImage) (row : Nat) : Except AuditError Nat := do + let cell := image.layout.cellOfRow image.view Table.genericParam + let mut count := 0 + for i in [:image.layout.rowCount Table.genericParam] do + if (← cell (i + 1) 2) == 2 * row then + unless (← cell (i + 1) 0) == count do throw (.typeDefMismatch row .kind) + count := count + 1 + return count + +/-- Decode the physical Extends column, including the tag when the base is a generic instance. -/ +private def physicalBase (image : PreparedImage) (row : Nat) : Except AuditError BaseType := do + let value ← image.layout.cellOfRow image.view Table.typeDef row typeDefExtendsColumn + if value == 0 then return .none + unless value / 4 != 0 do throw (.typeDefMismatch row .base) + let table ← match value % 4 with + | 0 => pure Table.typeDef + | 1 => pure Table.typeRef + | 2 => pure Table.typeSpec + | _ => throw (.typeDefMismatch row .base) + let token ← checkedTokenOf table (value / 4) .rowOverflow + let _ ← image.layout.rowAt token + if table == Table.typeSpec then return .spec token + let cell := image.layout.cell image.view token + let space ← image.layout.stringAt image.view (← cell typeNamespaceColumn) + let name ← image.layout.stringAt image.view (← cell typeNameColumn) + return .named token space name + +/-- Read the declaration's CLI kind and generic arity at a resolved TypeDef row. -/ +def physicalTypeShape (image : PreparedImage) (row : Nat) : Except AuditError (TypeKind × Nat) := do + let cell := image.layout.cellOfRow image.view Table.typeDef row + let flags ← cell typeDefFlagsColumn + let space ← image.layout.stringAt image.view (← cell typeNamespaceColumn) + let name ← image.layout.stringAt image.view (← cell typeNameColumn) + let base ← physicalBase image row + let kind ← kindOf image flags space name base + if kind == .interface && base != .none then throw (.typeDefMismatch row .base) + if kind == .enum || kind == .valueType then + if let .named token baseSpace baseName := base then + if tableOf token == Table.typeDef then + unless (← enumDefinition? image 0 [baseSpace, baseName]) == some (token % 2^24) do + throw (.typeDefMismatch row .base) + return (kind, ← physicalTypeArity image row) + +/-- A distinguished root's base is the top-level type in a recognized core assembly. -/ +private def coreBaseNamed (image : PreparedImage) (row : Nat) (expected : List Nat) : + Except AuditError Bool := do + let .named token space name ← physicalBase image row | return false + unless space == systemNamespace && name == expected do return false + if tableOf token == Table.typeDef then + return (← platformAssembly image) && + (← enumDefinition? image 0 [space, name]) == some (token % 2^24) + let scope ← image.layout.cell image.view token 0 + unless scope % 4 == 2 do return false + let reference ← checkedTokenOf Table.assemblyRef (scope / 4) .rowOverflow + coreAssemblyAt image reference 0 + +/-- The physical declaration determines its ordinary kind and arity. The five distinguished + System types also require their exact top-level names, publisher and base. -/ +def physicalPlatformKind (image : PreparedImage) (row : Nat) (parts : List (List Nat)) : + Except AuditError PlatformKind := do + let (kind, arity) ← physicalTypeShape image row + if let [space, name] := parts then + if space == systemNamespace && (← platformAssembly image) then + let special := if name == asciiOf "Object" then some PlatformKind.objectRoot + else if name == valueTypeName then some .valueTypeRoot + else if name == enumName then some .enumRoot + else if name == valueTuplePairName then some .valuePair + else if name == asciiOf "BitConverter" then some .byteOrder + else none + if let some role := special then + unless arity == role.arity do throw (.typeDefMismatch row .kind) + let valid ← match role with + | .objectRoot => pure (kind == .classType && (← physicalBase image row) == .none) + | .valuePair => pure (kind == .valueType && (← coreBaseNamed image row valueTypeName)) + | .enumRoot => pure (kind == .classType && (← coreBaseNamed image row valueTypeName)) + | _ => pure (kind == .classType && (← coreBaseNamed image row (asciiOf "Object"))) + unless valid do throw (.typeDefMismatch row .base) + return role + match kind with + | .classType => return if arity == 0 then .classType else .genericClass arity + | .interface => return if arity == 0 then .interfaceType else .genericInterface arity + | .valueType => return if arity == 0 then .valueType else .genericValue arity + | .enum => return .enumType (← physicalEnumStorage image row) arity + +/-- A missing definition or forwarder image leaves the entry uncovered. A wrong kind in + a supplied image fails verification. -/ +def coveredPlatformKind (images : List PreparedImage) (entry : PlatformDefinition) : + Except AuditError Bool := do + let some (image, row) ← findPinnedDefinition images entry.parts entry.scope (images.length + 1) + | return false + unless (← physicalPlatformKind image row entry.parts) == entry.kind do + throw (.typeDefMismatch row .kind) + return true + +/-- Bind the table to each method's checked environment and read every externally declared kind. + Even a partial audit checks all definitions for which the images were supplied. -/ +def checkPlatformKinds (images : List PinnedImage) (platform : PlatformTable) + (methods : List MethodEvidence) : Except AuditError Bool := do + let _ ← methods.mapM fun item => do + let .ok env := item.body.environment | throw (.rowMismatch item.original.token) + unless env.platform == platform do throw (.rowMismatch item.original.token) + pure () + let prepared ← prepare images + let entries := platform.definitions + let covered ← entries.mapM (coveredPlatformKind prepared) + return !methods.isEmpty && !entries.isEmpty && covered.all id + +/-- The completeness bit describes the exact pinned images and platform declarations used by + these method bodies. -/ +def PlatformKindsChecked (images : List PinnedImage) (platform : PlatformTable) + (methods : List MethodEvidence) (complete : Bool) : Prop := + (checkPlatformKinds images platform methods).toOption = some complete + +instance (images : List PinnedImage) (platform : PlatformTable) (methods : List MethodEvidence) + (complete : Bool) : Decidable (PlatformKindsChecked images platform methods complete) := + inferInstanceAs (Decidable (_ = _)) + +/-- The definition used for a fully covered entry has the declared kind in its pinned image. -/ +theorem coveredPlatformKind_physical {images : List PreparedImage} {entry : PlatformDefinition} + (covered : coveredPlatformKind images entry = .ok true) : + ∃ image row, PinnedDefinitionAt images entry.parts entry.scope image row ∧ + physicalPlatformKind image row entry.parts = .ok entry.kind := by + simp only [coveredPlatformKind, bind, Except.bind] at covered + cases selected : findPinnedDefinition images entry.parts entry.scope (images.length + 1) with + | error fault => simp [selected] at covered + | ok result => + cases result with + | none => simp [selected, pure, Except.pure] at covered + | some pair => + obtain ⟨image, row⟩ := pair + cases read : physicalPlatformKind image row entry.parts with + | error fault => simp [selected, read] at covered + | ok actual => + simp [selected, read] at covered + split at covered <;> try contradiction + rename_i same + exact ⟨image, row, pinnedDefinitionAt_of_find selected, same ▸ read⟩ +#print axioms coveredPlatformKind_physical + +/-- In every certified method, each declaration selected from the platform table has its + claimed kind at the physical TypeDef row found through the supplied images. -/ +theorem PlatformKindsChecked.physical {images : List PinnedImage} {platform : PlatformTable} + {methods : List MethodEvidence} {item : MethodEvidence} {env : MetadataEnv} + (checked : PlatformKindsChecked images platform methods true) (method : item ∈ methods) + (environment : item.body.environment = .ok env) {entry : PlatformDefinition} + (member : entry ∈ env.platform.definitions) : + ∃ prepared image row, prepare images = .ok prepared ∧ + PinnedDefinitionAt prepared entry.parts entry.scope image row ∧ + physicalPlatformKind image row entry.parts = .ok entry.kind := by + have done : checkPlatformKinds images platform methods = .ok true := by + cases result : checkPlatformKinds images platform methods with + | error err => simp [PlatformKindsChecked, result, Except.toOption] at checked + | ok value => + have yes : value = true := by simpa [PlatformKindsChecked, result, Except.toOption] using checked + rw [yes] + unfold checkPlatformKinds at done + obtain ⟨checks, usingPlatform, done⟩ := CIL.Safety.except_bind_ok done + obtain ⟨prepared, built, done⟩ := CIL.Safety.except_bind_ok done + obtain ⟨covered, entries, done⟩ := CIL.Safety.except_bind_ok done + have all := Except.ok.inj done + simp only [Bool.and_eq_true] at all + obtain ⟨_, _, validated⟩ := read_member method usingPlatform + have same : env.platform = platform := by + by_cases same : env.platform = platform + · exact same + · simp [environment, same, bind, Except.bind, throw] at validated + obtain ⟨result, found, read⟩ := read_member (same ▸ member) entries + have yes : result = true := List.all_eq_true.mp all.2 result found + obtain ⟨image, row, route, kind⟩ := coveredPlatformKind_physical (yes ▸ read) + exact ⟨prepared, image, row, built, route, kind⟩ +#print axioms PlatformKindsChecked.physical + +end StrictBinding diff --git a/StrictBinding/PlatformType.lean b/StrictBinding/PlatformType.lean new file mode 100644 index 000000000..1a1a38ec4 --- /dev/null +++ b/StrictBinding/PlatformType.lean @@ -0,0 +1,74 @@ +import StrictBinding.TypeDefs + +/-! Resolve public System types and their physical field ranges in pinned platform images. -/ + +namespace StrictBinding + +open CIL.Extraction + +/-- The unique public, top-level, non-interface definition with the given System name. -/ +private def definitionRow (image : PreparedImage) (name : String) (invalid : AuditError) : + Except AuditError (Option Nat) := do + let cell := image.layout.cellOfRow image.view Table.typeDef + let mut found := none + for i in [:image.layout.rowCount Table.typeDef] do + let row := i + 1 + unless ← stringFound image (← cell row typeNameColumn) (asciiOf name ++ [0]) do continue + unless ← stringFound image (← cell row typeNamespaceColumn) (systemNamespace ++ [0]) do continue + let flags ← cell row typeDefFlagsColumn + if visibilityOf flags ≥ nestedVisibility then continue + if found.isSome || visibilityOf flags != 1 || flags &&& interfaceFlag != 0 then throw invalid + found := some row + let some row := found | return none + for i in [:nestedCount image] do + if (← nestedTypeOf image (i + 1)) == row then throw invalid + return some row + +/-- The half-open range of physical Field rows owned by a TypeDef. -/ +def platformFieldRange (image : PreparedImage) (row : Nat) : Except AuditError (Nat × Nat) := do + let cell := image.layout.cellOfRow image.view Table.typeDef + let first ← cell row typeDefFieldListColumn + let finish ← fieldsEnd image row + unless 1 ≤ first && first ≤ finish && finish ≤ image.layout.rowCount Table.field + 1 do + throw (.typeDefMismatch row .fields) + return (first, finish) + +/-- A top-level ExportedType forwarder for this System type resolves an exact core AssemblyRef. -/ +private def forwarder (image : PreparedImage) (name : String) (invalid : AuditError) : + Except AuditError (Option String) := do + let exportedType := 0x27 + let cell := image.layout.cellOfRow image.view exportedType + let mut target := none + for i in [:image.layout.rowCount exportedType] do + let row := i + 1 + unless ← stringFound image (← cell row 2) (asciiOf name ++ [0]) do continue + unless ← stringFound image (← cell row 3) (systemNamespace ++ [0]) do continue + let implementation ← cell row 4 + if implementation % 4 == 2 then continue -- A nested export names its enclosing export. + if target.isSome || implementation % 4 != 1 || implementation / 4 == 0 || + (← cell row 0) &&& 0x00200000 == 0 then throw invalid + let token ← CIL.Extraction.checkedTokenOf Table.assemblyRef (implementation / 4) .rowOverflow + unless ← coreAssemblyAt image token 0 do throw invalid + let identity ← assemblyIdentityAt image.view image.layout token 0 + target := some (String.ofList (identity.map Char.ofNat)) + return target + +/-- The platform Assembly row carries a full public key that hashes to the recognized token. -/ +def platformAssembly (image : PreparedImage) : Except AuditError Bool := + coreAssemblyAt image (tokenOf 0x20 1) 1 + +/-- Resolve a public System type through authenticated platform images and check its definition. + A missing destination leaves coverage incomplete. A cycle or competing definition fails. -/ +def resolvePlatformType (images : List PreparedImage) (name : String) + (invalid : String → AuditError) (check : PreparedImage → Nat → Except AuditError Unit) + (identity : String) : Nat → Except AuditError Bool + | 0 => throw (invalid identity) + | fuel + 1 => do + let some image := images.find? (·.identity == identity) | return false + unless ← platformAssembly image do throw (invalid identity) + match ← definitionRow image name (invalid identity), ← forwarder image name (invalid identity) with + | some row, none => check image row; pure true + | none, some next => resolvePlatformType images name invalid check next fuel + | _, _ => throw (invalid identity) + +end StrictBinding diff --git a/StrictBinding/PublicKeys.lean b/StrictBinding/PublicKeys.lean new file mode 100644 index 000000000..9f6df4023 --- /dev/null +++ b/StrictBinding/PublicKeys.lean @@ -0,0 +1,96 @@ +import StrictBinding.Bind +import CIL.Safety.Embedding.Step +import CIL.Extraction.Translation + +/-! Compare the publisher tokens in the certified metadata with the keys of the referenced images. -/ + +namespace StrictBinding + +open CIL.Extraction + +/-- The Assembly row supplies either a full publisher key or the empty unsigned identity. -/ +def definitionKeyToken (image : PreparedImage) : Except AuditError (Option Nat) := do + let cell := image.layout.cell image.view (tokenOf 0x20 1) + pure (publisherToken (← image.layout.blobAt image.view (← cell 6))) + +/-- A publisher token for an Assembly definition comes from hashing its full key blob. -/ +theorem definitionKeyToken_signed {image : PreparedImage} {token : Nat} + (verified : definitionKeyToken image = .ok (some token)) : + ∃ offset key, image.layout.cell image.view (tokenOf 0x20 1) 6 = .ok offset ∧ + image.layout.blobAt image.view offset = .ok key ∧ key.isEmpty = false ∧ + token = CIL.Crypto.Sha1.keyToken key := by + unfold definitionKeyToken at verified + obtain ⟨offset, offsetRead, rest⟩ := CIL.Safety.except_bind_ok verified + obtain ⟨key, keyRead, rest⟩ := CIL.Safety.except_bind_ok rest + refine ⟨offset, key, offsetRead, keyRead, ?_⟩ + simp only [pure, Except.pure, Except.ok.injEq, publisherToken] at rest + split at rest <;> simp_all + +#print axioms definitionKeyToken_signed + +/-- The key or eight-byte token stored in this image's AssemblyRef row. -/ +private def referenceKeyToken (image : PreparedImage) (token : Nat) : + Except AuditError (Option Nat) := do + let cell := image.layout.cell image.view token + let flags ← cell 4 + let key ← image.layout.blobAt image.view (← cell 5) + assemblyKeyToken flags key + +/-- Every carried AssemblyRef retains the physical row's key or token under its heap numbering. -/ +private def checkCarriedKeys (images : List PreparedImage) (slice : MetadataSlice) : + Except AuditError Unit := do + let source ← located images slice.assembly.identity + let .ok index := slice.index | throw (.referenceKeyMismatch source.identity 0) + for (token, _) in slice.rows do + unless CIL.Extraction.metadataTableOf token == Table.assemblyRef do continue + let .ok row := index.assemblyRef token | throw (.referenceKeyMismatch source.identity token) + let .ok key := index.blob row.publicKeyOrToken + | throw (.referenceKeyMismatch source.identity token) + unless (← assemblyKeyToken row.flags key) == (← referenceKeyToken source token) do + throw (.referenceKeyMismatch source.identity token) + +/-- Compare every AssemblyRef in each extraction pin with the definition in the supplied images. + A missing image leaves coverage incomplete. A present image must agree even then. -/ +def checkPublicKeys (images : List PinnedImage) (pins : List AssemblyPin) + (evidence : List MethodEvidence) : Except AuditError Bool := do + let prepared ← prepare images + let mut keys : Std.TreeMap String (Option Nat) := {} + for image in prepared do + keys := keys.insert image.identity (← definitionKeyToken image) + for item in evidence do + for slice in item.body.metadata.slices do + unless pins.any (·.identity == slice.assembly.identity) do + throw (.unpinned slice.assembly.identity) + checkCarriedKeys prepared slice + let mut complete := true + let mut seen := false + for pin in pins do + let source ← located prepared pin.identity + for i in [:source.layout.rowCount Table.assemblyRef] do + seen := true + let token ← checkedTokenOf Table.assemblyRef (i + 1) .rowOverflow + let text ← assemblyIdentityAt source.view source.layout token 0 + let expected ← referenceKeyToken source token + match keys.get? (String.ofList (text.map Char.ofNat)) with + | none => complete := false + | some actual => + unless actual == expected do throw (.referenceKeyMismatch source.identity token) + return seen && complete + +/-- All references have images whose full keys reproduce their tokens, including unsigned targets. -/ +def PublicKeysBound (images : List PinnedImage) (pins : List AssemblyPin) + (evidence : List MethodEvidence) : Prop := + (checkPublicKeys images pins evidence).toOption = some true + +instance (images : List PinnedImage) (pins : List AssemblyPin) (evidence : List MethodEvidence) : + Decidable (PublicKeysBound images pins evidence) := inferInstanceAs (Decidable (_ = _)) + +/-- Every supplied AssemblyRef target agrees with its caller's publisher token. -/ +def SuppliedPublicKeysValid (images : List PinnedImage) (pins : List AssemblyPin) + (evidence : List MethodEvidence) : Prop := ok (checkPublicKeys images pins evidence) = true + +instance (images : List PinnedImage) (pins : List AssemblyPin) (evidence : List MethodEvidence) : + Decidable (SuppliedPublicKeysValid images pins evidence) := + inferInstanceAs (Decidable (_ = true)) + +end StrictBinding diff --git a/StrictBinding/Reads.lean b/StrictBinding/Reads.lean new file mode 100644 index 000000000..3411c7657 --- /dev/null +++ b/StrictBinding/Reads.lean @@ -0,0 +1,369 @@ +import StrictBinding.Image +import CIL.Safety.Embedding.Step + +/-! +# Reads of image bytes + +A check reads the bytes it compares with one `ImageView.slice`, which joins the chunks that hold +them into one value, and a proposition about an image states those bytes with `ImageView.bytes`, +one byte at a time. In a view whose chunks are each below `2 ^ chunkBits`, as every view +`ImageView.ofImage` builds is, the two agree: `bytes_of_hasBytesAt` turns a successful +comparison into the bytes the image holds there. +-/ + +namespace StrictBinding + +open CIL.Crypto + +/-- One step of a big-endian concatenation: the value so far, shifted past the next item. -/ +def joinStep (value : Nat) (item : Nat × Nat) : Nat := value * 2 ^ item.2 + item.1 + +/-- Joining adjacent pairs first does not change a big-endian concatenation. -/ +theorem foldl_joinPairs (items : List (Nat × Nat)) (value : Nat) : + (joinPairs items).foldl joinStep value = items.foldl joinStep value := by + induction items using joinPairs.induct generalizing value with + | case1 high highWidth low lowWidth rest onRest => + simp only [joinPairs, List.foldl_cons, onRest] + congr 1 + simp only [joinStep, Nat.pow_add, Nat.add_mul, Nat.mul_assoc, Nat.add_assoc] + | case2 items notPair => + match items, notPair with + | [], _ => rfl + | [_], _ => rfl + | _ :: _ :: _, notPair => exact absurd rfl (notPair _ _ _ _ _) + +#print axioms foldl_joinPairs + +/-- Joining adjacent pairs halves a list, rounding up. -/ +theorem length_joinPairs (items : List (Nat × Nat)) : + (joinPairs items).length = (items.length + 1) / 2 := by + induction items using joinPairs.induct with + | case1 high highWidth low lowWidth rest onRest => + simp only [joinPairs, List.length_cons, onRest]; omega + | case2 items notPair => + match items, notPair with + | [], _ => rfl + | [_], _ => simp [joinPairs] + | _ :: _ :: _, notPair => exact absurd rfl (notPair _ _ _ _ _) + +#print axioms length_joinPairs + +/-- With enough fuel, `joinValues` computes the big-endian concatenation of the items. -/ +theorem joinValues_fst (fuel : Nat) (items : List (Nat × Nat)) (fits : items.length ≤ fuel + 1) : + (joinValues fuel items).1 = items.foldl joinStep 0 := by + induction fuel generalizing items with + | zero => + match items, fits with + | [], _ => rfl + | [item], _ => simp [joinValues, joinStep] + | succ fuel onRest => + match items with + | [] => rfl + | [item] => simp [joinValues, joinStep] + | first :: second :: rest => + simp only [joinValues] + rw [onRest _ (by simp only [length_joinPairs, List.length_cons] at fits ⊢; omega), + foldl_joinPairs] + +#print axioms joinValues_fst + +/-- The bits of a concatenation of `count` values of `width` bits each. -/ +theorem testBit_foldl_joinStep (value : Nat → Nat) {width : Nat} (positive : 0 < width) + (fits : ∀ index, value index < 2 ^ width) (count bit : Nat) : + Nat.testBit (((List.range count).map fun index => (value index, width)).foldl joinStep 0) bit = + (decide (bit < width * count) && + (value (count - 1 - bit / width)).testBit (bit % width)) := by + induction count generalizing bit with + | zero => simp + | succ count onRest => + rw [List.range_succ, List.map_append, List.foldl_append] + simp only [List.map_cons, List.map_nil, List.foldl_cons, List.foldl_nil, joinStep] + rw [Nat.mul_comm, Nat.testBit_two_pow_mul_add _ (fits count)] + by_cases low : bit < width + · simp only [low, ite_true, Nat.div_eq_of_lt low, Nat.mod_eq_of_lt low] + have : bit < width * (count + 1) := by rw [Nat.mul_succ]; omega + simp [this] + · simp only [low, ite_false] + obtain ⟨rest, rfl⟩ : ∃ rest, bit = rest + width := ⟨bit - width, by omega⟩ + rw [Nat.add_sub_cancel, onRest, Nat.add_div_right _ positive, Nat.add_mod_right] + have same : decide (rest < width * count) = decide (rest + width < width * (count + 1)) := by + rw [Nat.mul_succ]; simp + rw [same] + congr 3 + omega + +#print axioms testBit_foldl_joinStep + +/-- Every chunk a view holds is below `2 ^ chunkBits`, as `ImageView.ofImage` requires. -/ +def ImageView.Bounded (view : ImageView) : Prop := + match view.chunks with + | some tree => ∀ index, tree.at index < 2 ^ chunkBits + | none => True + +/-- The bits of a read: bit `bit` of the value of `count` bytes at `offset` is a bit of the byte + at `offset + count - 1 - bit / 8`, in the chunk that holds it. -/ +theorem testBit_slice {view : ImageView} (bounded : view.Bounded) {offset count value : Nat} + (read : view.slice offset count = .ok value) (positive : 0 < count) (bit : Nat) : + ∃ tree, view.chunks = some tree ∧ + value.testBit bit = (decide (bit < 8 * count) && + (tree.at ((offset + count - 1 - bit / 8) / 64)).testBit + (8 * (63 - (offset + count - 1 - bit / 8) % 64) + bit % 8)) := by + unfold ImageView.slice at read + cases chunksEq : view.chunks with + | none => + simp only [chunksEq] at read + split at read + · rename_i empty; simp at empty; omega + · cases read + | some tree => + refine ⟨tree, rfl, ?_⟩ + have fits : ∀ index, tree.at index < 2 ^ chunkBits := by + simpa [ImageView.Bounded, chunksEq] using bounded + simp only [chunksEq] at read + split at read + · rename_i inside + have nonzero : (count == 0) = false := by simp; omega + simp only [nonzero, Bool.false_eq_true, ite_false] at read + injection read with read + subst read + rw [show ∀ value modulus : Nat, Nat.mod value modulus = value % modulus from fun _ _ => rfl, + show ∀ value shift : Nat, Nat.shiftRight value shift = value >>> shift from fun _ _ => rfl, + Nat.testBit_mod_two_pow, Nat.shiftRight_eq_div_pow, Nat.testBit_div_two_pow, + joinValues_fst _ _ (by simp), + testBit_foldl_joinStep (fun index => tree.at (offset / 64 + index)) (by decide) + (fun index => fits _)] + by_cases low : bit < 8 * count + · simp only [low, decide_true, Bool.true_and, chunkBits] + have within : bit + 8 * (64 * ((offset + count - 1) / 64 + 1) - (offset + count)) < + 512 * ((offset + count - 1) / 64 - offset / 64 + 1) := by omega + simp only [within, decide_true, Bool.true_and] + congr 2 <;> omega + · simp [low] + · cases read + +#print axioms testBit_slice + +/-- A read that succeeds is inside the image. -/ +theorem slice_inside {view : ImageView} {offset count value : Nat} + (read : view.slice offset count = .ok value) (positive : 0 < count) : + offset + count ≤ view.length ∧ view.chunks.isSome := by + unfold ImageView.slice at read + cases chunksEq : view.chunks with + | none => + simp only [chunksEq] at read + split at read + · rename_i empty; simp at empty; omega + · cases read + | some tree => + simp only [chunksEq] at read + split at read + · rename_i inside; exact ⟨inside, rfl⟩ + · cases read + +#print axioms slice_inside + +/-- A read inside an image with chunks succeeds. -/ +theorem slice_of_inside {view : ImageView} {offset count : Nat} + (inside : offset + count ≤ view.length) (held : view.chunks.isSome) : + ∃ value, view.slice offset count = .ok value := by + unfold ImageView.slice + cases chunksEq : view.chunks with + | none => simp [chunksEq] at held + | some tree => + simp only [inside, ite_true] + split + · exact ⟨_, rfl⟩ + · exact ⟨_, rfl⟩ + +#print axioms slice_of_inside + +/-- The big-endian bytes of the low `count` bytes of a value. -/ +def valueBytes (count value : Nat) : List Nat := + (List.range count).map fun index => value / 256 ^ (count - 1 - index) % 256 + +/-- Bit `bit` of byte `shift` bytes from the right of a value. -/ +theorem testBit_byte (value shift bit : Nat) : + (value / 256 ^ shift % 256).testBit bit = + (decide (bit < 8) && value.testBit (bit + 8 * shift)) := by + rw [show (256 : Nat) = 2 ^ 8 from rfl, ← Nat.pow_mul, Nat.testBit_mod_two_pow, + Nat.testBit_div_two_pow, Nat.mul_comm 8 shift] + +#print axioms testBit_byte + +/-- Mapping a list through a check that succeeds on every item succeeds with the results. -/ +theorem mapM_ok {ε α β : Type} {check : α → Except ε β} {result : α → β} : + ∀ {items : List α}, (∀ item ∈ items, check item = .ok (result item)) → + items.mapM check = .ok (items.map result) + | [], _ => rfl + | item :: rest, each => by + simp only [List.mapM_cons, each item (List.mem_cons_self ..), + mapM_ok (fun other inside => each other (List.mem_cons_of_mem _ inside))] + rfl + +#print axioms mapM_ok + +/-- The value of `count` bytes read at once is the bytes read one at a time. -/ +theorem bytes_of_slice {view : ImageView} (bounded : view.Bounded) {offset count value : Nat} + (read : view.slice offset count = .ok value) : + view.bytes offset count = .ok (valueBytes count value) := by + unfold ImageView.bytes valueBytes + apply mapM_ok + intro index member + have below : index < count := List.mem_range.mp member + have ⟨inside, held⟩ := slice_inside read (by omega) + have ⟨byte, readByte⟩ := slice_of_inside (offset := offset + index) (count := 1) (by omega) held + rw [readByte] + congr 1 + apply Nat.eq_of_testBit_eq + intro bit + have ⟨tree, chunksEq, byteBit⟩ := testBit_slice bounded readByte (by decide) bit + have ⟨sameTree, sameChunks, valueBit⟩ := + testBit_slice bounded read (by omega) (bit + 8 * (count - 1 - index)) + rw [chunksEq] at sameChunks + cases sameChunks + rw [byteBit, testBit_byte, valueBit] + by_cases low : bit < 8 + · have position : offset + count - 1 - (bit + 8 * (count - 1 - index)) / 8 = + offset + index + 1 - 1 - bit / 8 := by omega + have within : (bit + 8 * (count - 1 - index)) % 8 = bit % 8 := by omega + have inByte : bit + 8 * (count - 1 - index) < 8 * count := by omega + simp [position, within, inByte, low] + · simp [low] + +#print axioms bytes_of_slice + +/-- Bytes are the big-endian bytes of their value. -/ +theorem valueBytes_bytesValue {bytes : List Nat} (small : bytes.all (· < 256) = true) : + valueBytes bytes.length (bytesValue bytes) = bytes := by + have items : bytes.map (fun byte => (byte, 8)) = + (List.range bytes.length).map fun index => (bytes.getD index 0, 8) := by + apply List.ext_getElem + · simp + · intro index inMap _ + simp only [List.getElem_map, List.getElem_range, List.getD_eq_getElem?_getD] + rw [List.getElem?_eq_getElem (by simpa using inMap)] + rfl + have fits : ∀ index, bytes.getD index 0 < 2 ^ 8 := by + intro index + simp only [List.all_eq_true, decide_eq_true_eq] at small + rw [List.getD_eq_getElem?_getD] + cases found : bytes[index]? with + | none => simp + | some byte => exact small byte (List.mem_of_getElem? found) + apply List.ext_getElem + · simp [valueBytes] + · intro index inValue inBytes + simp only [valueBytes, List.getElem_map, List.getElem_range] + apply Nat.eq_of_testBit_eq + intro bit + rw [testBit_byte, bytesValue, joinValues_fst _ _ (by simp), items, + testBit_foldl_joinStep _ (by decide) fits] + by_cases low : bit < 8 + · have position : bytes.length - 1 - (bit + 8 * (bytes.length - 1 - index)) / 8 = index := by + omega + have within : (bit + 8 * (bytes.length - 1 - index)) % 8 = bit := by omega + have inside : bit + 8 * (bytes.length - 1 - index) < 8 * bytes.length := by omega + simp [position, within, inside, low, List.getD_eq_getElem?_getD, inBytes] + · have byteFits : bytes[index] < 2 ^ 8 := by + simpa [List.getD_eq_getElem?_getD, inBytes] using fits index + have large : bytes[index] < 2 ^ bit := + Nat.lt_of_lt_of_le byteFits (Nat.pow_le_pow_right (by decide) (by omega)) + simp [low, Nat.testBit_lt_two_pow large] + +#print axioms valueBytes_bytesValue + +/-- Bytes found at an offset are the bytes read there one at a time. -/ +theorem bytes_of_hasBytesAt {view : ImageView} (bounded : view.Bounded) {offset : Nat} + {bytes : List Nat} (found : view.hasBytesAt offset bytes = .ok true) : + view.bytes offset bytes.length = .ok bytes := by + unfold ImageView.hasBytesAt at found + cases read : view.slice offset bytes.length with + | error _ => simp [read, bind, Except.bind] at found + | ok value => + simp only [read, bind, Except.bind, pure, Except.pure, Except.ok.injEq, Bool.and_eq_true, + beq_iff_eq] at found + rw [bytes_of_slice bounded read, found.2, valueBytes_bytesValue found.1] + +#print axioms bytes_of_hasBytesAt + +/-! ## Views of well-formed images -/ + +/-- Every leaf of a tree is below `2 ^ chunkBits`. -/ +def ChunkTree.Bounded : ChunkTree → Prop + | .leaf chunk => chunk < 2 ^ chunkBits + | .node _ left right => left.Bounded ∧ right.Bounded + +/-- Every chunk a bounded tree holds is below `2 ^ chunkBits`. -/ +theorem ChunkTree.at_lt {tree : ChunkTree} (bounded : tree.Bounded) (index : Nat) : + tree.at index < 2 ^ chunkBits := by + induction tree generalizing index with + | leaf chunk => exact bounded + | node count left right inLeft inRight => + simp only [ChunkTree.at] + split + · exact inLeft bounded.1 _ + · exact inRight bounded.2 _ + +#print axioms ChunkTree.at_lt + +/-- Pairing bounded trees gives bounded trees. -/ +theorem pairTrees_bounded {trees : List ChunkTree} (bounded : ∀ tree ∈ trees, tree.Bounded) : + ∀ tree ∈ pairTrees trees, tree.Bounded := by + induction trees using pairTrees.induct with + | case1 left right rest inRest => + simp only [pairTrees, List.mem_cons, forall_eq_or_imp] at bounded ⊢ + exact ⟨⟨bounded.1, bounded.2.1⟩, inRest bounded.2.2⟩ + | case2 trees notPair => + match trees, notPair with + | [], _ => simp [pairTrees] + | [_], _ => simpa [pairTrees] using bounded + | _ :: _ :: _, notPair => exact absurd rfl (notPair _ _ _) + +#print axioms pairTrees_bounded + +/-- The tree built from bounded trees is bounded. -/ +theorem buildTree_bounded : + ∀ (fuel : Nat) {trees : List ChunkTree}, (∀ tree ∈ trees, tree.Bounded) → + ∀ {tree : ChunkTree}, buildTree fuel trees = some tree → tree.Bounded + | 0, trees, bounded, tree, built => by + cases trees with + | nil => cases built + | cons first _ => + simp only [buildTree, List.head?_cons, Option.some.injEq] at built + exact built ▸ bounded first (List.mem_cons_self ..) + | fuel + 1, trees, bounded, tree, built => by + match trees with + | [] => cases built + | [single] => + simp only [buildTree, Option.some.injEq] at built + exact built ▸ bounded single (List.mem_cons_self ..) + | first :: second :: rest => + exact buildTree_bounded fuel (pairTrees_bounded bounded) built + +#print axioms buildTree_bounded + +/-- A view `ImageView.ofImage` builds holds only chunks below `2 ^ chunkBits`. -/ +theorem ofImage_bounded {image : ImageBytes} {view : ImageView} + (built : ImageView.ofImage image = .ok view) : view.Bounded := by + unfold ImageView.ofImage at built + split at built + · rename_i wellFormed + cases built + simp only [ImageView.Bounded] + split + · rename_i tree treeEq + have small : ∀ chunk ∈ image.chunks, chunk < 2 ^ chunkBits := by + simp only [ImageBytes.wellFormed, Bool.and_eq_true, List.all_eq_true, + decide_eq_true_eq] at wellFormed + exact wellFormed.1.2 + have leaves : ∀ leaf ∈ image.chunks.map ChunkTree.leaf, leaf.Bounded := by + simp only [List.mem_map] + rintro _ ⟨chunk, member, rfl⟩ + exact small chunk member + exact ChunkTree.at_lt (buildTree_bounded _ leaves treeEq) + · trivial + · cases built + +#print axioms ofImage_bounded + +end StrictBinding diff --git a/StrictBinding/Rows.lean b/StrictBinding/Rows.lean new file mode 100644 index 000000000..afdff630f --- /dev/null +++ b/StrictBinding/Rows.lean @@ -0,0 +1,1177 @@ +import StrictBinding.Bind +import StrictBinding.Reads +import CIL.Extraction.MetadataBytes + +/-! +# Carried metadata bound to images + +An extraction carries, for each pinned assembly, its table stream header, the header decoded, +its `Assembly` row and the heap entries that row names (`CIL.Extraction.AssemblyMetadata`), and for +each method the rows and heap entries its tokens reach (`CIL.Extraction.MetadataSlice`). A carried +row does not hold the image's `#Strings` and `#Blob` offsets: each heap column holds a key, 0 for +offset 0, the rank of the entry among the entries carried, or `omittedKey` for an entry the +extraction does not carry; and a carried MethodDef row holds 0 for its RVA. `RowAt`, `StringAt` +and `SizedAt` state that carried bytes are the image's under such a numbering. `RowsMatch` states +it for carried rows and entries under one numbering per heap, which also needs an entry for every +key of the rows other than 0 and `omittedKey`, and a row cell holding the key of every entry; +`AssemblyMatches` adds the header for an assembly block. `rowsBound` and `assemblyBound` check +them, taking every position and length they compare from the image itself, with a fixed number +of image reads per row and per entry and one sort per heap; `rowsMatch_of_rowsBound` and +`assemblyMatches_of_assemblyBound` prove that their success means the propositions. For an image +`prepareOne` decodes, `rvaColumn_of_prepareOne` says the mask covers the four RVA bytes, and +`stringColumns_of_prepareOne` and `blobColumns_of_prepareOne` say every cell of a heap has the +width of that heap's marker. +-/ + +namespace StrictBinding + +open CIL.Extraction (AssemblyMetadata ByteEntry decodeTableHeader stringsHeap blobHeap sortPairs + sortBy_perm pairBefore) +open CIL.Safety (except_bind_ok) + +/-! ## Heap numbering -/ + +/-- The key a carried row writes for a nonzero image offset whose entry it does not carry: the + largest value of a column of `width` bytes. -/ +def omittedKey (width : Nat) : Nat := 2 ^ (8 * width) - 1 + +/-- The key a carried column of `width` bytes holds for an image offset under `keyOf`. -/ +def canonicalKey (keyOf : Nat → Option Nat) (width offset : Nat) : Nat := + if offset == 0 then 0 else (keyOf offset).getD (omittedKey width) + +/-- A numbering of image offsets: offset 0 has no key, no two offsets share a key, and every key + is above 0 and below `omittedKey width`. -/ +def Numbering (keyOf : Nat → Option Nat) (width : Nat) : Prop := + keyOf 0 = none ∧ + (∀ first second key, keyOf first = some key → keyOf second = some key → first = second) ∧ + ∀ offset key, keyOf offset = some key → 0 < key ∧ key < omittedKey width + +/-- `key` names `offset`: key 0 names offset 0, and any other key names the offset the + numbering gives it. -/ +def Names (keyOf : Nat → Option Nat) (key offset : Nat) : Prop := + (key = 0 ∧ offset = 0) ∨ keyOf offset = some key + +/-- The bytes of a `#Strings` index in the image's rows. -/ +def stringWidth (image : PreparedImage) : Nat := + columnWidth image.layout.heapSizes image.layout.rowCount .string + +/-- The bytes of a `#Blob` index in the image's rows. -/ +def blobWidth (image : PreparedImage) : Nat := + columnWidth image.layout.heapSizes image.layout.rowCount .blob + +/-! ## Rows -/ + +/-- The token of the one row of the `Assembly` table. -/ +def assemblyToken : Nat := tokenOf 0x20 1 + +/-- The little-endian value of bytes. -/ +def littleValue (bytes : List Nat) : Nat := bytes.foldr (fun byte value => byte + 256 * value) 0 + +/-- The `width` little-endian bytes of a value. -/ +def littleBytes : Nat → Nat → List Nat + | 0, _ => [] + | width + 1, value => value % 256 :: littleBytes width (value / 256) + +/-- The heap a column indexes, when it indexes `#Strings` or `#Blob`. -/ +def Column.heap? : Column → Option Nat + | .string => some stringsHeap + | .blob => some blobHeap + | _ => none + +/-- The image row with the RVA of a MethodDef row zeroed, as a carried row holds it: where a + method's body is placed changes with the bodies before it. -/ +def maskRva (image : PreparedImage) (token : Nat) (row : List Nat) : List Nat := + if token / 2 ^ 24 == CIL.Extraction.Table.methodDef then + row.take image.rvaColumn.1 ++ + (row.drop image.rvaColumn.1 |>.take image.rvaColumn.2).map (fun _ => 0) ++ + row.drop (image.rvaColumn.1 + image.rvaColumn.2) + else row + +/-- A cell as a carried row holds it: a heap offset becomes its key, any other cell is kept. -/ +def canonicalCell (strings blobs : Nat → Option Nat) (column : Column) (cell : List Nat) : + List Nat := + match column with + | .string => littleBytes cell.length (canonicalKey strings cell.length (littleValue cell)) + | .blob => littleBytes cell.length (canonicalKey blobs cell.length (littleValue cell)) + | _ => cell + +/-- The cells of a row of these columns, each as a carried row holds it; the row must have + exactly the columns' widths. -/ +def canonicalCells (strings blobs : Nat → Option Nat) (token : Nat) : + List ColumnPlace → List Nat → Except AuditError (List Nat) + | [], [] => pure [] + | [], _ :: _ => throw (.rowMismatch token) + | place :: rest, row => do + let cell := row.take place.width + if cell.length != place.width then throw (.rowMismatch token) + else + let tail ← canonicalCells strings blobs token rest (row.drop place.width) + pure (canonicalCell strings blobs place.column cell ++ tail) + +/-- The image row of `token` as a carried row holds it, under the numberings `strings` and + `blobs`, with a MethodDef row's RVA zeroed. -/ +def canonicalRow (image : PreparedImage) (strings blobs : Nat → Option Nat) (token : Nat) + (row : List Nat) : Except AuditError (List Nat) := + match image.columnsOf (token / 2 ^ 24) with + | some columns => canonicalCells strings blobs token columns (maskRva image token row) + | none => throw (.tokenOutOfScope token) + +/-- The carried row is the image row of its token under the numberings `strings` and `blobs`. -/ +def RowAt (image : PreparedImage) (strings blobs : Nat → Option Nat) (token : Nat) + (bytes : List Nat) : Prop := + ∃ offset row, image.layout.rowAt token = .ok (offset, bytes.length) ∧ + image.view.bytes offset bytes.length = .ok row ∧ + canonicalRow image strings blobs token row = .ok bytes + +/-! ## Heap entries -/ + +/-- The image's NUL-terminated `#Strings` entry at `offset`, inside the heap. -/ +def StringAt (image : PreparedImage) (offset : Nat) (bytes : List Nat) : Prop := + ∃ text, bytes = text ++ [0] ∧ 0 ∉ text ∧ + offset + bytes.length ≤ image.layout.pe.strings.size ∧ + image.view.bytes (image.layout.pe.strings.start + offset) bytes.length = .ok bytes + +/-- The image's length prefix and exactly its payload at `offset`, inside `heap`. -/ +def SizedAt (image : PreparedImage) (heap : Stream) (offset : Nat) (bytes : List Nat) : Prop := + ∃ prefixBytes length, blobPrefix image.view (heap.start + offset) = .ok (prefixBytes, length) ∧ + bytes.length = prefixBytes + length ∧ offset + bytes.length ≤ heap.size ∧ + image.view.bytes (heap.start + offset) bytes.length = .ok bytes + +/-! ## Rows and entries under one numbering -/ + +/-- The keys the cells of `heap` hold in a carried row of these columns. -/ +def rowKeys (heap : Nat) : List ColumnPlace → List Nat → List Nat + | [], _ => [] + | place :: rest, row => + let tail := rowKeys heap rest (row.drop place.width) + if place.column.heap? == some heap then littleValue (row.take place.width) :: tail else tail + +/-- Every key the numbering gives is held by a cell of `heap` in a carried row, so a carried + entry cannot be named by a key no row holds. -/ +def Held (image : PreparedImage) (heap : Nat) (rows : List ByteEntry) + (keyOf : Nat → Option Nat) : Prop := + ∀ offset key, keyOf offset = some key → ∃ row ∈ rows, ∃ columns, + image.columnsOf (row.1 / 2 ^ 24) = some columns ∧ key ∈ rowKeys heap columns row.2 + +/-- Every key the numbering gives names a carried entry, so a carried row holds no key other + than 0 and `omittedKey` without the entry it ranks. -/ +def Covered (keyOf : Nat → Option Nat) (entries : List ByteEntry) : Prop := + ∀ offset key, keyOf offset = some key → ∃ entry ∈ entries, entry.1 = key + +/-- The carried rows and `#Strings` and `#Blob` entries are the image's under one numbering per + heap: each row is the image row of its token, each entry the image's entry at the offset its + key names, every key of the rows other than 0 and `omittedKey` has an entry, and a row cell + holds the key of every entry other than 0. -/ +def RowsMatch (image : PreparedImage) (rows strings blobs : List ByteEntry) : Prop := + ∃ stringKeys blobKeys, Numbering stringKeys (stringWidth image) ∧ + Numbering blobKeys (blobWidth image) ∧ + (∀ row ∈ rows, RowAt image stringKeys blobKeys row.1 row.2) ∧ + (∀ entry ∈ strings, ∃ offset, Names stringKeys entry.1 offset ∧ + StringAt image offset entry.2) ∧ + (∀ entry ∈ blobs, ∃ offset, Names blobKeys entry.1 offset ∧ + SizedAt image image.layout.pe.blobs offset entry.2) ∧ + Covered stringKeys strings ∧ Covered blobKeys blobs ∧ + Held image stringsHeap rows stringKeys ∧ Held image blobHeap rows blobKeys + +/-! ## The assembly block -/ + +/-- The offset of the `Valid` mask in a table stream. -/ +def validMaskOffset : Nat := 8 + +/-- The bytes of the `Valid` mask. -/ +def validMaskBytes : Nat := 8 + +/-- The bytes of each row count in a table stream header. -/ +def rowCountBytes : Nat := 4 + +/-- The bytes of a table stream header whose `Valid` mask is `valid`: the fixed fields, then a + row count for every table the mask marks present, including tables without rows. -/ +def tableHeaderLength (valid : Nat) : Nat := + tableHeaderBytes + rowCountBytes * + ((List.range (8 * validMaskBytes)).filter fun table => valid / 2 ^ table % 2 == 1).length + +/-- The carried header is the image's `#~` stream from its reserved word through the last row + count its own `Valid` mask declares. -/ +def HeaderAt (image : PreparedImage) (tableHeader : List Nat) : Prop := + ∃ valid, image.view.u (image.layout.pe.tables.start + validMaskOffset) validMaskBytes = + .ok valid ∧ tableHeader.length = tableHeaderLength valid ∧ + image.view.bytes image.layout.pe.tables.start tableHeader.length = .ok tableHeader + +/-- The assembly block is the image's: its header bytes, the header they decode to, and its + `Assembly` row and entries under a numbering of their own. -/ +def AssemblyMatches (image : PreparedImage) (assembly : AssemblyMetadata) : Prop := + HeaderAt image assembly.tableHeader ∧ + decodeTableHeader assembly.tableHeader = .ok assembly.header ∧ + RowsMatch image [(assemblyToken, assembly.assemblyRow)] assembly.strings assembly.blobs + +/-! ## Checks -/ + +/-- The header bytes are the image's through its last row count, and decode to the carried + decoded header. -/ +def headerBound (image : PreparedImage) (assembly : AssemblyMetadata) : + Except AuditError Unit := do + let tables := image.layout.pe.tables + let valid ← image.view.u (tables.start + validMaskOffset) validMaskBytes + if assembly.tableHeader.length != tableHeaderLength valid then + throw (.headerMismatch image.identity) + else + let found ← image.view.hasBytesAt tables.start assembly.tableHeader + if found && (decodeTableHeader assembly.tableHeader).toOption == some assembly.header then + pure () + else throw (.headerMismatch image.identity) + +/-- The `#Strings` and `#Blob` cells of carried rows that name a nonzero image offset, as + (image offset, key) with key 0 for a cell holding `omittedKey`. -/ +abbrev HeapCells := List (Nat × Nat) × List (Nat × Nat) + +/-- The key a heap cell of `width` bytes contributes to `HeapCells`: 0 for `omittedKey`. -/ +def cellKey (width key : Nat) : Nat := if key == omittedKey width then 0 else key + +/-- A carried heap cell against the image offset its image cell holds: the cell must be `width` + bytes, hold 0 exactly for offset 0, and gives the (offset, key) of a nonzero offset. -/ +def heapCell (token width offset : Nat) (carried : List Nat) : + Except AuditError (Option (Nat × Nat)) := + if carried != littleBytes width (littleValue carried) || + (offset == 0) != (littleValue carried == 0) then throw (.rowMismatch token) + else if offset == 0 then pure none + else pure (some (offset, cellKey width (littleValue carried))) + +/-- A carried row against the image row of its token, cell by cell: a heap cell gives its + (offset, key), any other cell must be the image's. -/ +def rowCells (token : Nat) : List ColumnPlace → List Nat → List Nat → Except AuditError HeapCells + | [], imageRow, carried => + if imageRow.isEmpty && carried.isEmpty then pure ([], []) else throw (.rowMismatch token) + | place :: rest, imageRow, carried => + let imageCell := imageRow.take place.width + let carriedCell := carried.take place.width + if imageCell.length != place.width then throw (.rowMismatch token) + else do + let tail ← rowCells token rest (imageRow.drop place.width) (carried.drop place.width) + match place.column with + | .string => do + let cell ← heapCell token place.width (littleValue imageCell) carriedCell + pure (cell.toList ++ tail.1, tail.2) + | .blob => do + let cell ← heapCell token place.width (littleValue imageCell) carriedCell + pure (tail.1, cell.toList ++ tail.2) + | _ => if carriedCell == imageCell then pure tail else throw (.rowMismatch token) + +/-- The heap cells of a carried row, checked against one read of the image row its token + names. -/ +def rowCellsAt (image : PreparedImage) (token : Nat) (carried : List Nat) : + Except AuditError HeapCells := do + let place ← image.layout.rowAt token + if carried.length != place.2 then throw (.rowMismatch token) + else + let value ← image.view.slice place.1 place.2 + match image.columnsOf (token / 2 ^ 24) with + | none => throw (.tokenOutOfScope token) + | some columns => + rowCells token columns (maskRva image token (valueBytes place.2 value)) carried + +/-- The heap cells of every carried row. -/ +def rowsCells (image : PreparedImage) : List ByteEntry → Except AuditError HeapCells + | [] => pure ([], []) + | row :: rest => do + let here ← rowCellsAt image row.1 row.2 + let later ← rowsCells image rest + pure (here.1 ++ later.1, here.2 ++ later.2) + +/-- Sorted pairs whose equal first components come only in equal pairs, without the repeats; + `keyOf` picks the key an error reports. -/ +def distinctPairs (heap : Nat) (keyOf : Nat × Nat → Nat) : + List (Nat × Nat) → Except AuditError (List (Nat × Nat)) + | [] => pure [] + | first :: rest => do + let later ← distinctPairs heap keyOf rest + match rest with + | [] => pure [first] + | second :: _ => + if first.1 < second.1 then pure (first :: later) + else if first == second then pure later + else throw (.heapNumbering heap (keyOf second)) + +/-- Each image offset of the cells has one key, the omitted key 0 included. -/ +def offsetsBound (heap : Nat) (cells : List (Nat × Nat)) : Except AuditError Unit := do + let _ ← distinctPairs heap (·.2) (sortPairs cells) + +/-- The (key, offset) of each cell whose key is not omitted. -/ +def numberedCells (cells : List (Nat × Nat)) : List (Nat × Nat) := + (cells.filter (·.2 != 0)).map fun cell => (cell.2, cell.1) + +/-- The numbered (key, offset) pairs ascending by key, each key once: every key is below the + marker of `width` bytes and has one offset. -/ +def keysBound (heap width : Nat) (cells : List (Nat × Nat)) : + Except AuditError (List (Nat × Nat)) := + match (numberedCells cells).find? (omittedKey width ≤ ·.1) with + | some pair => throw (.heapNumbering heap pair.1) + | none => distinctPairs heap (·.1) (sortPairs (numberedCells cells)) + +/-- The carried entries ascend strictly by key, as the slice's lookup trees need. -/ +def ascendingBound (heap : Nat) : List ByteEntry → Except AuditError Unit + | first :: second :: rest => + if first.1 < second.1 then ascendingBound heap (second :: rest) + else throw (.heapNumbering heap second.1) + | _ => pure () + +/-- The entry is found at `offset`. -/ +def entryBound (heap : Nat) (found : Nat → List Nat → Except AuditError Bool) (offset : Nat) + (entry : ByteEntry) : Except AuditError Unit := do + if (← found offset entry.2) then pure () else throw (.heapMismatch heap entry.1) + +/-- The numbered pairs and the entries of `heap`, both ascending by key, joined: an entry with key + 0 is found at offset 0, every other entry at the offset of the pair with its key, and every + pair has an entry. -/ +def joinBound (heap : Nat) (found : Nat → List Nat → Except AuditError Bool) : + List (Nat × Nat) → List ByteEntry → Except AuditError Unit + | [], [] => pure () + | pair :: _, [] => throw (.heapNumbering heap pair.1) + | numbered, entry :: rest => + if entry.1 == 0 then do + entryBound heap found 0 entry + joinBound heap found numbered rest + else match numbered with + | [] => throw (.heapNumbering heap entry.1) + | pair :: later => + if pair.1 == entry.1 then do + entryBound heap found pair.2 entry + joinBound heap found later rest + else throw (.heapNumbering heap (min pair.1 entry.1)) + +/-- Whether the bytes are one terminated `#Strings` entry that the image holds at `offset`. -/ +def stringFound (image : PreparedImage) (offset : Nat) (bytes : List Nat) : + Except AuditError Bool := + if bytes == bytes.dropLast ++ [0] && bytes.dropLast.all (· != 0) && + decide (offset + bytes.length ≤ image.layout.pe.strings.size) then + image.view.hasBytesAt (image.layout.pe.strings.start + offset) bytes + else pure false + +/-- Whether the bytes are the length prefix and payload the image holds at `offset` in `heap`. -/ +def sizedFound (image : PreparedImage) (heap : Stream) (offset : Nat) (bytes : List Nat) : + Except AuditError Bool := do + let blob ← blobPrefix image.view (heap.start + offset) + if bytes.length == blob.1 + blob.2 && decide (offset + bytes.length ≤ heap.size) then + image.view.hasBytesAt (heap.start + offset) bytes + else pure false + +/-- The carried rows and `#Strings` and `#Blob` entries are the image's under one numbering per + heap. Each row and each entry takes a fixed number of reads of the image, and the numbering + one sort of the heap cells per heap, so the check costs O((R + E) log (R + E)) comparisons for + R rows and E entries, plus the bytes compared. -/ +def rowsBound (image : PreparedImage) (rows strings blobs : List ByteEntry) : + Except AuditError Unit := do + let cells ← rowsCells image rows + offsetsBound stringsHeap cells.1 + offsetsBound blobHeap cells.2 + let stringKeys ← keysBound stringsHeap (stringWidth image) cells.1 + let blobKeys ← keysBound blobHeap (blobWidth image) cells.2 + ascendingBound stringsHeap strings + ascendingBound blobHeap blobs + joinBound stringsHeap (stringFound image) stringKeys strings + joinBound blobHeap (sizedFound image image.layout.pe.blobs) blobKeys blobs + +/-- The assembly block is the image's: header, decoded header, and the `Assembly` row with its + entries under a numbering of their own. -/ +def assemblyBound (image : PreparedImage) (assembly : AssemblyMetadata) : + Except AuditError Unit := do + headerBound image assembly + rowsBound image [(assemblyToken, assembly.assemblyRow)] assembly.strings assembly.blobs + +/-! ## Soundness -/ + +/-- A thrown error is not a success. -/ +theorem throw_ne_ok {α : Type} {error : AuditError} {value : α} : + (throw error : Except AuditError α) ≠ .ok value := by + intro thrown; cases thrown + +#print axioms throw_ne_ok + +/-- A successful header check means the carried header is the image's and decodes as carried. -/ +theorem header_of_headerBound {image : PreparedImage} {assembly : AssemblyMetadata} + (bounded : image.view.Bounded) (bound : headerBound image assembly = .ok ()) : + HeaderAt image assembly.tableHeader ∧ + decodeTableHeader assembly.tableHeader = .ok assembly.header := by + unfold headerBound at bound + obtain ⟨valid, readValid, bound⟩ := except_bind_ok bound + split at bound + · exact absurd bound throw_ne_ok + · rename_i sameLength + obtain ⟨found, readFound, bound⟩ := except_bind_ok bound + split at bound + · rename_i matched + simp only [Bool.and_eq_true, beq_iff_eq] at matched + obtain ⟨isFound, decoded⟩ := matched + subst isFound + refine ⟨⟨valid, readValid, by simpa using sameLength, bytes_of_hasBytesAt bounded readFound⟩, + ?_⟩ + revert decoded + cases decodeTableHeader assembly.tableHeader <;> simp [Except.toOption] + · exact absurd bound throw_ne_ok + +#print axioms header_of_headerBound + +/-- A string entry the check finds is the image's terminated entry at that offset. -/ +theorem stringAt_of_stringFound {image : PreparedImage} (bounded : image.view.Bounded) + {offset : Nat} {bytes : List Nat} (found : stringFound image offset bytes = .ok true) : + StringAt image offset bytes := by + unfold stringFound at found + split at found + · rename_i shape + simp only [Bool.and_eq_true, beq_iff_eq, List.all_eq_true, bne_iff_ne, ne_eq, + decide_eq_true_eq] at shape + exact ⟨bytes.dropLast, shape.1.1, fun inText => shape.1.2 0 inText rfl, shape.2, + bytes_of_hasBytesAt bounded found⟩ + · cases found + +#print axioms stringAt_of_stringFound + +/-- A sized entry the check finds is the image's prefix and payload at that offset. -/ +theorem sizedAt_of_sizedFound {image : PreparedImage} (bounded : image.view.Bounded) + {heap : Stream} {offset : Nat} {bytes : List Nat} + (found : sizedFound image heap offset bytes = .ok true) : SizedAt image heap offset bytes := by + unfold sizedFound at found + obtain ⟨blob, readPrefix, found⟩ := except_bind_ok found + split at found + · rename_i shape + simp only [Bool.and_eq_true, beq_iff_eq, decide_eq_true_eq] at shape + exact ⟨blob.1, blob.2, readPrefix, shape.1, shape.2, bytes_of_hasBytesAt bounded found⟩ + · cases found + +#print axioms sizedAt_of_sizedFound + +/-- Two pairs in ascending order of first components, or the same pair. -/ +def Linked (first second : Nat × Nat) : Prop := first.1 < second.1 ∨ first = second + +/-- Ascending pairs ascend through a middle pair. -/ +theorem linked_trans {first second third : Nat × Nat} (one : Linked first second) + (two : Linked second third) : Linked first third := by + rcases one with less | same + · rcases two with less' | same' + · exact .inl (Nat.lt_trans less less') + · subst same'; exact .inl less + · subst same; exact two + +#print axioms linked_trans + +/-- A successful `distinctPairs` keeps exactly the pairs it was given, which ascend. -/ +theorem distinctPairs_sound {heap : Nat} {keyOf : Nat × Nat → Nat} : + ∀ {pairs distinct : List (Nat × Nat)}, distinctPairs heap keyOf pairs = .ok distinct → + (∀ pair, pair ∈ distinct ↔ pair ∈ pairs) ∧ pairs.Pairwise Linked + | [], distinct, checked => by + simp only [distinctPairs, pure, Except.pure, Except.ok.injEq] at checked + subst checked; simp + | first :: rest, distinct, checked => by + simp only [distinctPairs] at checked + obtain ⟨later, laterChecked, checked⟩ := except_bind_ok checked + obtain ⟨laterSame, laterLinked⟩ := distinctPairs_sound laterChecked + cases rest with + | nil => + simp only [pure, Except.pure, Except.ok.injEq] at checked + subst checked; simp + | cons second more => + simp only at checked + by_cases ascends : first.1 < second.1 + · simp only [ascends, ite_true, pure, Except.pure, Except.ok.injEq] at checked + subst checked + refine ⟨fun pair => by simp only [List.mem_cons, laterSame], ?_⟩ + refine List.Pairwise.cons (fun other member => ?_) laterLinked + rcases List.mem_cons.mp member with same | inMore + · subst same; exact .inl ascends + · exact linked_trans (.inl ascends) (List.rel_of_pairwise_cons laterLinked inMore) + · by_cases equal : first = second + · subst equal + simp only [ascends, ite_false, beq_self_eq_true, ite_true, pure, Except.pure, + Except.ok.injEq] at checked + subst checked + refine ⟨fun pair => ?_, ?_⟩ + · rw [laterSame]; simp + · refine List.Pairwise.cons (fun other member => ?_) laterLinked + rcases List.mem_cons.mp member with same | inMore + · exact .inr same.symm + · exact List.rel_of_pairwise_cons laterLinked inMore + · have differ : (first == second) = false := by simpa using equal + simp only [ascends, ite_false, differ] at checked + exact absurd checked throw_ne_ok + +#print axioms distinctPairs_sound + +/-- In ascending pairs, a first component determines its pair. -/ +theorem eq_of_pairwise_linked : + ∀ {pairs : List (Nat × Nat)}, pairs.Pairwise Linked → + ∀ first ∈ pairs, ∀ second ∈ pairs, first.1 = second.1 → first = second + | [], _, _, member, _, _, _ => by cases member + | head :: rest, linked, first, inFirst, second, inSecond, same => by + have later := List.Pairwise.of_cons linked + have fromHead : ∀ other ∈ rest, Linked head other := fun other member => + List.rel_of_pairwise_cons linked member + rcases List.mem_cons.mp inFirst with isHead | firstInRest <;> + rcases List.mem_cons.mp inSecond with isHead' | secondInRest + · rw [isHead, isHead'] + · subst isHead + rcases fromHead second secondInRest with less | equal + · omega + · exact equal + · subst isHead' + rcases fromHead first firstInRest with less | equal + · omega + · exact equal.symm + · exact eq_of_pairwise_linked later first firstInRest second secondInRest same + +#print axioms eq_of_pairwise_linked + +/-- Sorted pairs that `distinctPairs` accepts: the same pairs, each first component once. -/ +theorem sorted_sound {heap : Nat} {keyOf : Nat × Nat → Nat} {pairs distinct : List (Nat × Nat)} + (checked : distinctPairs heap keyOf (sortPairs pairs) = .ok distinct) : + (∀ pair, pair ∈ distinct ↔ pair ∈ pairs) ∧ + ∀ first ∈ pairs, ∀ second ∈ pairs, first.1 = second.1 → first = second := by + obtain ⟨same, linked⟩ := distinctPairs_sound checked + have perm := sortBy_perm pairBefore pairs.length pairs + refine ⟨fun pair => (same pair).trans perm.mem_iff, fun first inFirst second inSecond => ?_⟩ + exact eq_of_pairwise_linked linked first (perm.mem_iff.mpr inFirst) second + (perm.mem_iff.mpr inSecond) + +#print axioms sorted_sound + +/-- The key the cells give an offset: the key of a cell at that offset other than an omitted + one. -/ +def keyOfCells (cells : List (Nat × Nat)) (offset : Nat) : Option Nat := + (cells.find? fun cell => cell.1 == offset && cell.2 != 0).map (·.2) + +/-- When an offset determines its cell, the cells give it exactly the key of its cell. -/ +theorem keyOfCells_eq_some {cells : List (Nat × Nat)} + (byOffset : ∀ first ∈ cells, ∀ second ∈ cells, first.1 = second.1 → first = second) + {offset key : Nat} : keyOfCells cells offset = some key ↔ (offset, key) ∈ cells ∧ key ≠ 0 := by + constructor + · intro given + unfold keyOfCells at given + cases found : cells.find? (fun cell => cell.1 == offset && cell.2 != 0) with + | none => simp [found] at given + | some cell => + simp only [found, Option.map_some, Option.some.injEq] at given + have matching := List.find?_some found + simp only [Bool.and_eq_true, beq_iff_eq, bne_iff_ne, ne_eq] at matching + refine ⟨?_, given ▸ matching.2⟩ + have member := List.mem_of_find?_eq_some found + rw [← matching.1, ← given] + exact member + · rintro ⟨member, nonzero⟩ + unfold keyOfCells + cases found : cells.find? (fun cell => cell.1 == offset && cell.2 != 0) with + | none => + have := List.find?_eq_none.mp found (offset, key) member + simp [nonzero] at this + | some cell => + have matching := List.find?_some found + simp only [Bool.and_eq_true, beq_iff_eq] at matching + have same := byOffset cell (List.mem_of_find?_eq_some found) (offset, key) member matching.1 + simp [same] + +#print axioms keyOfCells_eq_some + +/-- What a cell says of a numbering: the omitted key 0 that the offset has no key, any other key + that it is the offset's key. -/ +def Assigned (keyOf : Nat → Option Nat) (cell : Nat × Nat) : Prop := + (cell.2 = 0 → keyOf cell.1 = none) ∧ (cell.2 ≠ 0 → keyOf cell.1 = some cell.2) + +/-- Every cell holds of the key the cells give, when an offset determines its cell. -/ +theorem assigned_of_cells {cells : List (Nat × Nat)} + (byOffset : ∀ first ∈ cells, ∀ second ∈ cells, first.1 = second.1 → first = second) : + ∀ cell ∈ cells, Assigned (keyOfCells cells) cell := by + intro cell member + refine ⟨fun omitted => ?_, fun numbered => (keyOfCells_eq_some byOffset).mpr ⟨member, numbered⟩⟩ + cases given : keyOfCells cells cell.1 with + | none => rfl + | some key => + obtain ⟨other, nonzero⟩ := (keyOfCells_eq_some byOffset).mp given + have := byOffset cell member (cell.1, key) other rfl + rw [this] at omitted + exact absurd omitted nonzero + +#print axioms assigned_of_cells + +/-- Pairs that `offsetsBound` accepts: an offset determines its cell. -/ +theorem offsetsBound_sound {heap : Nat} {cells : List (Nat × Nat)} + (checked : offsetsBound heap cells = .ok ()) : + ∀ first ∈ cells, ∀ second ∈ cells, first.1 = second.1 → first = second := by + unfold offsetsBound at checked + obtain ⟨_, sorted, _⟩ := except_bind_ok checked + exact (sorted_sound sorted).2 + +#print axioms offsetsBound_sound + +/-- What `keysBound` guarantees: the numbered pairs are the swapped cells with a key, each key + has one offset, and every key is below the marker. -/ +theorem keysBound_sound {heap width : Nat} {cells numbered : List (Nat × Nat)} + (checked : keysBound heap width cells = .ok numbered) : + (∀ pair, pair ∈ numbered ↔ (pair.2, pair.1) ∈ cells ∧ pair.1 ≠ 0) ∧ + (∀ first second key, (first, key) ∈ cells → (second, key) ∈ cells → key ≠ 0 → + first = second) ∧ + ∀ cell ∈ cells, cell.2 ≠ 0 → cell.2 < omittedKey width := by + unfold keysBound at checked + have inNumbered : ∀ pair : Nat × Nat, + pair ∈ numberedCells cells ↔ + (pair.2, pair.1) ∈ cells ∧ pair.1 ≠ 0 := by + intro pair + simp only [numberedCells, List.mem_map, List.mem_filter, bne_iff_ne, ne_eq] + constructor + · rintro ⟨cell, ⟨member, nonzero⟩, rfl⟩ + exact ⟨member, nonzero⟩ + · rintro ⟨member, nonzero⟩ + exact ⟨(pair.2, pair.1), ⟨member, nonzero⟩, rfl⟩ + split at checked + · exact absurd checked throw_ne_ok + · rename_i noneFound + obtain ⟨same, byKey⟩ := sorted_sound checked + refine ⟨fun pair => (same pair).trans (inNumbered pair), ?_, ?_⟩ + · intro first second key one two nonzero + have := byKey (key, first) ((inNumbered _).mpr ⟨one, nonzero⟩) (key, second) + ((inNumbered _).mpr ⟨two, nonzero⟩) rfl + simpa using this + · intro cell member nonzero + have := List.find?_eq_none.mp noneFound (cell.2, cell.1) + ((inNumbered _).mpr ⟨by simpa using member, nonzero⟩) + simpa using this + +#print axioms keysBound_sound + +/-- A successful entry check found the entry at that offset. -/ +theorem found_of_entryBound {heap : Nat} {found : Nat → List Nat → Except AuditError Bool} + {offset : Nat} {entry : ByteEntry} (checked : entryBound heap found offset entry = .ok ()) : + found offset entry.2 = .ok true := by + unfold entryBound at checked + obtain ⟨isFound, foundAt, checked⟩ := except_bind_ok checked + cases isFound + · exact absurd checked throw_ne_ok + · exact foundAt + +#print axioms found_of_entryBound + +/-- A successful merge-join names each entry: key 0 at offset 0, any other at the offset of its + numbered pair; and every numbered pair has an entry. -/ +theorem joinBound_sound {heap : Nat} {found : Nat → List Nat → Except AuditError Bool} + {holds : Nat → List Nat → Prop} + (sound : ∀ offset bytes, found offset bytes = .ok true → holds offset bytes) : + ∀ {numbered : List (Nat × Nat)} {entries : List ByteEntry}, + joinBound heap found numbered entries = .ok () → + (∀ entry ∈ entries, ∃ offset, + ((entry.1 = 0 ∧ offset = 0) ∨ (entry.1, offset) ∈ numbered) ∧ holds offset entry.2) ∧ + ∀ pair ∈ numbered, ∃ entry ∈ entries, entry.1 = pair.1 + | [], [], _ => by simp + | _ :: _, [], checked => by + simp only [joinBound] at checked + exact absurd checked throw_ne_ok + | numbered, entry :: rest, checked => by + have unfolded : joinBound heap found numbered (entry :: rest) = + if entry.1 == 0 then do + entryBound heap found 0 entry + joinBound heap found numbered rest + else match numbered with + | [] => throw (.heapNumbering heap entry.1) + | pair :: later => + if pair.1 == entry.1 then do + entryBound heap found pair.2 entry + joinBound heap found later rest + else throw (.heapNumbering heap (min pair.1 entry.1)) := by + cases numbered <;> rfl + rw [unfolded] at checked + split at checked + · rename_i zero + have zero : entry.1 = 0 := by simpa using zero + obtain ⟨_, entryChecked, restChecked⟩ := except_bind_ok checked + obtain ⟨named, covered⟩ := joinBound_sound sound restChecked + refine ⟨fun other member => ?_, fun pair member => ?_⟩ + · rcases List.mem_cons.mp member with same | inRest + · subst same + exact ⟨0, .inl ⟨zero, rfl⟩, sound _ _ (found_of_entryBound entryChecked)⟩ + · exact named other inRest + · obtain ⟨other, inRest, sameKey⟩ := covered pair member + exact ⟨other, List.mem_cons_of_mem _ inRest, sameKey⟩ + · cases numbered with + | nil => exact absurd checked throw_ne_ok + | cons pair later => + simp only at checked + split at checked + · rename_i sameKey + have sameKey : pair.1 = entry.1 := by simpa using sameKey + obtain ⟨_, entryChecked, restChecked⟩ := except_bind_ok checked + obtain ⟨named, covered⟩ := joinBound_sound sound restChecked + refine ⟨fun other member => ?_, fun numberedPair member => ?_⟩ + · rcases List.mem_cons.mp member with same | inRest + · subst same + refine ⟨pair.2, .inr ?_, sound _ _ (found_of_entryBound entryChecked)⟩ + rw [← sameKey] + exact List.mem_cons_self + · obtain ⟨offset, which, holdsAt⟩ := named other inRest + exact ⟨offset, which.imp id (List.mem_cons_of_mem _), holdsAt⟩ + · rcases List.mem_cons.mp member with same | inLater + · subst same + exact ⟨entry, List.mem_cons_self, sameKey.symm⟩ + · obtain ⟨other, inRest, otherKey⟩ := covered numberedPair inLater + exact ⟨other, List.mem_cons_of_mem _ inRest, otherKey⟩ + · exact absurd checked throw_ne_ok + +#print axioms joinBound_sound + +/-- One heap of a successful check: a numbering of its cells, every cell assigned by it, every + entry named and found, every key covered, and every key from a cell. -/ +theorem heap_of_bounds {heap width : Nat} {cells numbered : List (Nat × Nat)} + {entries : List ByteEntry} {found : Nat → List Nat → Except AuditError Bool} + {holds : Nat → List Nat → Prop} + (sound : ∀ offset bytes, found offset bytes = .ok true → holds offset bytes) + (nonzero : ∀ cell ∈ cells, cell.1 ≠ 0) + (offsetsChecked : offsetsBound heap cells = .ok ()) + (keysChecked : keysBound heap width cells = .ok numbered) + (joined : joinBound heap found numbered entries = .ok ()) : + Numbering (keyOfCells cells) width ∧ (∀ cell ∈ cells, Assigned (keyOfCells cells) cell) ∧ + (∀ entry ∈ entries, ∃ offset, Names (keyOfCells cells) entry.1 offset ∧ + holds offset entry.2) ∧ + Covered (keyOfCells cells) entries ∧ + ∀ offset key, keyOfCells cells offset = some key → (offset, key) ∈ cells ∧ key ≠ 0 := by + have byOffset := offsetsBound_sound offsetsChecked + obtain ⟨inNumbered, byKey, below⟩ := keysBound_sound keysChecked + obtain ⟨named, covered⟩ := joinBound_sound sound joined + have given := fun {offset key : Nat} => + keyOfCells_eq_some byOffset (offset := offset) (key := key) + refine ⟨⟨?_, ?_, ?_⟩, assigned_of_cells byOffset, ?_, ?_, fun _ _ found => given.mp found⟩ + · cases zeroKey : keyOfCells cells 0 with + | none => rfl + | some key => exact absurd rfl (nonzero _ (given.mp zeroKey).1) + · intro first second key atFirst atSecond + obtain ⟨inFirst, keyNonzero⟩ := given.mp atFirst + exact byKey first second key inFirst (given.mp atSecond).1 keyNonzero + · intro offset key atOffset + obtain ⟨member, keyNonzero⟩ := given.mp atOffset + exact ⟨Nat.pos_of_ne_zero keyNonzero, below _ member keyNonzero⟩ + · intro entry member + obtain ⟨offset, which, holdsAt⟩ := named entry member + refine ⟨offset, ?_, holdsAt⟩ + rcases which with zero | pair + · exact .inl zero + · exact .inr (given.mpr ((inNumbered _).mp pair)) + · intro offset key atOffset + obtain ⟨entry, member, sameKey⟩ := + covered (key, offset) ((inNumbered _).mpr (given.mp atOffset)) + exact ⟨entry, member, sameKey⟩ + +#print axioms heap_of_bounds + +/-- `littleBytes width` has `width` bytes. -/ +theorem length_littleBytes : ∀ (width value : Nat), (littleBytes width value).length = width + | 0, _ => rfl + | width + 1, value => by simp [littleBytes, length_littleBytes width] + +#print axioms length_littleBytes + +/-- What a successful heap cell check states of the carried cell and the cell it returns. -/ +theorem heapCell_sound {token width offset : Nat} {carried : List Nat} + {result : Option (Nat × Nat)} (checked : heapCell token width offset carried = .ok result) : + carried = littleBytes width (littleValue carried) ∧ + (offset = 0 ↔ littleValue carried = 0) ∧ (offset = 0 → result = none) ∧ + (offset ≠ 0 → result = some (offset, cellKey width (littleValue carried))) := by + unfold heapCell at checked + split at checked + · exact absurd checked throw_ne_ok + · rename_i shape + simp only [Bool.or_eq_true, bne_iff_ne, ne_eq, not_or, Decidable.not_not] at shape + obtain ⟨canonical, zeros⟩ := shape + have zeros : offset = 0 ↔ littleValue carried = 0 := by + clear canonical checked + constructor <;> intro isZero <;> simp_all + refine ⟨canonical, zeros, fun isZero => ?_, fun nonzero => ?_⟩ + · simp only [isZero, beq_self_eq_true, ite_true, pure, Except.pure, Except.ok.injEq] at checked + exact checked.symm + · have : (offset == 0) = false := by simpa using nonzero + simp only [this, Bool.false_eq_true, ite_false, pure, Except.pure, + Except.ok.injEq] at checked + exact checked.symm + +#print axioms heapCell_sound + +/-- A cell a heap cell check returns has a nonzero offset, and a key other than 0 is the carried + cell's value. -/ +theorem heapCell_cell {token width offset : Nat} {carried : List Nat} + {result : Option (Nat × Nat)} (checked : heapCell token width offset carried = .ok result) : + ∀ cell ∈ result.toList, cell.1 ≠ 0 ∧ (cell.2 ≠ 0 → cell.2 = littleValue carried) := by + obtain ⟨-, -, isNone, isSome⟩ := heapCell_sound checked + intro cell member + by_cases zero : offset = 0 + · rw [isNone zero] at member; cases member + · rw [isSome zero] at member + simp only [Option.toList_some, List.mem_singleton] at member + subst member + refine ⟨zero, fun nonzero => ?_⟩ + simp only [cellKey] at nonzero ⊢ + split <;> simp_all + +#print axioms heapCell_cell + +/-- The image cell, canonicalized under a numbering its returned cell holds of, is the carried + cell. -/ +theorem canonical_of_heapCell {keyOf : Nat → Option Nat} {token width : Nat} + {imageCell carried : List Nat} {result : Option (Nat × Nat)} + (length : imageCell.length = width) + (checked : heapCell token width (littleValue imageCell) carried = .ok result) + (assigned : ∀ cell ∈ result.toList, Assigned keyOf cell) : + littleBytes imageCell.length (canonicalKey keyOf imageCell.length (littleValue imageCell)) = + carried := by + obtain ⟨canonical, zeros, isNone, isSome⟩ := heapCell_sound checked + rw [length, canonical] + congr 1 + unfold canonicalKey + by_cases zero : littleValue imageCell = 0 + · simp only [zero, beq_self_eq_true, ite_true] + exact (zeros.mp zero).symm + · have nonzero : (littleValue imageCell == 0) = false := by simpa using zero + simp only [nonzero, Bool.false_eq_true, ite_false] + have cellAssigned := assigned (littleValue imageCell, cellKey width (littleValue carried)) + (by rw [isSome zero]; simp) + have keyNonzero : littleValue carried ≠ 0 := fun isZero => zero (zeros.mpr isZero) + by_cases omitted : littleValue carried = omittedKey width + · have : cellKey width (littleValue carried) = 0 := by simp [cellKey, omitted] + rw [cellAssigned.1 this, omitted] + rfl + · have : cellKey width (littleValue carried) = littleValue carried := by + simp [cellKey, omitted] + have keyed := cellAssigned.2 (by simpa [this] using keyNonzero) + simp only [this] at keyed + rw [keyed] + rfl + +#print axioms canonical_of_heapCell + +/-- A successful row check: every returned cell has a nonzero offset and, unless omitted, a key + a cell of its heap holds in the carried row; and under any numberings the returned cells hold + of, the image row canonicalizes to the carried row. -/ +theorem rowCells_sound {token : Nat} : + ∀ {columns : List ColumnPlace} {imageRow carried : List Nat} {cells : HeapCells}, + rowCells token columns imageRow carried = .ok cells → + (∀ cell ∈ cells.1, cell.1 ≠ 0 ∧ + (cell.2 ≠ 0 → cell.2 ∈ rowKeys stringsHeap columns carried)) ∧ + (∀ cell ∈ cells.2, cell.1 ≠ 0 ∧ (cell.2 ≠ 0 → cell.2 ∈ rowKeys blobHeap columns carried)) ∧ + ∀ strings blobs, (∀ cell ∈ cells.1, Assigned strings cell) → + (∀ cell ∈ cells.2, Assigned blobs cell) → + canonicalCells strings blobs token columns imageRow = .ok carried + | [], imageRow, carried, cells, checked => by + simp only [rowCells] at checked + split at checked + · rename_i empty + simp only [Bool.and_eq_true, List.isEmpty_iff] at empty + obtain ⟨imageEmpty, carriedEmpty⟩ := empty + subst imageEmpty carriedEmpty + simp only [pure, Except.pure, Except.ok.injEq] at checked + subst checked + exact ⟨by simp, by simp, fun _ _ _ _ => rfl⟩ + · exact absurd checked throw_ne_ok + | ⟨offset, width, column⟩ :: rest, imageRow, carried, cells, checked => by + simp only [rowCells] at checked + split at checked + · exact absurd checked throw_ne_ok + · rename_i lengthOk + have imageLength : (imageRow.take width).length = width := by simpa using lengthOk + obtain ⟨tail, tailChecked, checked⟩ := except_bind_ok checked + obtain ⟨tailStrings, tailBlobs, tailCanonical⟩ := rowCells_sound tailChecked + have canonicalHead : ∀ strings blobs, canonicalCells strings blobs token + (⟨offset, width, column⟩ :: rest) imageRow = + (canonicalCells strings blobs token rest (imageRow.drop width)).map + (canonicalCell strings blobs column (imageRow.take width) ++ ·) := by + intro strings blobs + simp only [canonicalCells, imageLength, bne_self_eq_false, Bool.false_eq_true, ite_false] + cases canonicalCells strings blobs token rest (imageRow.drop width) <;> rfl + cases column with + | string => + simp only at checked + obtain ⟨cell, cellChecked, checked⟩ := except_bind_ok checked + simp only [pure, Except.pure, Except.ok.injEq] at checked + subst checked + have fromCell := heapCell_cell cellChecked + refine ⟨fun found member => ?_, fun found member => ?_, + fun strings blobs inStrings inBlobs => ?_⟩ + · rcases List.mem_append.mp member with here | later + · obtain ⟨nonzero, keyed⟩ := fromCell found here + refine ⟨nonzero, fun keyNonzero => ?_⟩ + simp only [rowKeys, Column.heap?, stringsHeap, beq_self_eq_true, ite_true] + exact List.mem_cons.mpr (.inl (keyed keyNonzero)) + · obtain ⟨nonzero, keyed⟩ := tailStrings found later + refine ⟨nonzero, fun keyNonzero => ?_⟩ + simp only [rowKeys, Column.heap?, stringsHeap, beq_self_eq_true, ite_true] + exact List.mem_cons_of_mem _ (keyed keyNonzero) + · obtain ⟨nonzero, keyed⟩ := tailBlobs found member + refine ⟨nonzero, fun keyNonzero => ?_⟩ + simpa [rowKeys, Column.heap?, stringsHeap, blobHeap] using keyed keyNonzero + · rw [canonicalHead, tailCanonical strings blobs + (fun found member => inStrings found (List.mem_append_right _ member)) inBlobs] + simp only [Except.map, canonicalCell] + rw [canonical_of_heapCell imageLength cellChecked + (fun found member => inStrings found (List.mem_append_left _ member)), + List.take_append_drop] + | blob => + simp only at checked + obtain ⟨cell, cellChecked, checked⟩ := except_bind_ok checked + simp only [pure, Except.pure, Except.ok.injEq] at checked + subst checked + have fromCell := heapCell_cell cellChecked + refine ⟨fun found member => ?_, fun found member => ?_, + fun strings blobs inStrings inBlobs => ?_⟩ + · obtain ⟨nonzero, keyed⟩ := tailStrings found member + refine ⟨nonzero, fun keyNonzero => ?_⟩ + simpa [rowKeys, Column.heap?, stringsHeap, blobHeap] using keyed keyNonzero + · rcases List.mem_append.mp member with here | later + · obtain ⟨nonzero, keyed⟩ := fromCell found here + refine ⟨nonzero, fun keyNonzero => ?_⟩ + simp only [rowKeys, Column.heap?, blobHeap, beq_self_eq_true, ite_true] + exact List.mem_cons.mpr (.inl (keyed keyNonzero)) + · obtain ⟨nonzero, keyed⟩ := tailBlobs found later + refine ⟨nonzero, fun keyNonzero => ?_⟩ + simp only [rowKeys, Column.heap?, blobHeap, beq_self_eq_true, ite_true] + exact List.mem_cons_of_mem _ (keyed keyNonzero) + · rw [canonicalHead, tailCanonical strings blobs inStrings + (fun found member => inBlobs found (List.mem_append_right _ member))] + simp only [Except.map, canonicalCell] + rw [canonical_of_heapCell imageLength cellChecked + (fun found member => inBlobs found (List.mem_append_left _ member)), + List.take_append_drop] + | fixed | guid | index | coded => + simp only at checked + split at checked + · rename_i same + have same : carried.take width = imageRow.take width := by simpa using same + simp only [pure, Except.pure, Except.ok.injEq] at checked + subst checked + refine ⟨fun found member => ?_, fun found member => ?_, + fun strings blobs inStrings inBlobs => ?_⟩ + · obtain ⟨nonzero, keyed⟩ := tailStrings found member + exact ⟨nonzero, fun keyNonzero => by + simpa [rowKeys, Column.heap?] using keyed keyNonzero⟩ + · obtain ⟨nonzero, keyed⟩ := tailBlobs found member + exact ⟨nonzero, fun keyNonzero => by + simpa [rowKeys, Column.heap?] using keyed keyNonzero⟩ + · rw [canonicalHead, tailCanonical strings blobs inStrings inBlobs] + simp only [Except.map, canonicalCell] + rw [← same, List.take_append_drop] + · exact absurd checked throw_ne_ok + +#print axioms rowCells_sound + +/-- A successful check of one carried row against the row its token names. -/ +theorem rowCellsAt_sound {image : PreparedImage} (bounded : image.view.Bounded) {token : Nat} + {carried : List Nat} {cells : HeapCells} + (checked : rowCellsAt image token carried = .ok cells) : + ∃ columns, image.columnsOf (token / 2 ^ 24) = some columns ∧ + (∀ cell ∈ cells.1, cell.1 ≠ 0 ∧ + (cell.2 ≠ 0 → cell.2 ∈ rowKeys stringsHeap columns carried)) ∧ + (∀ cell ∈ cells.2, cell.1 ≠ 0 ∧ (cell.2 ≠ 0 → cell.2 ∈ rowKeys blobHeap columns carried)) ∧ + ∀ strings blobs, (∀ cell ∈ cells.1, Assigned strings cell) → + (∀ cell ∈ cells.2, Assigned blobs cell) → RowAt image strings blobs token carried := by + unfold rowCellsAt at checked + obtain ⟨place, placed, checked⟩ := except_bind_ok checked + split at checked + · exact absurd checked throw_ne_ok + · rename_i sameLength + have sameLength : carried.length = place.2 := by simpa using sameLength + obtain ⟨value, readRow, checked⟩ := except_bind_ok checked + split at checked + · exact absurd checked throw_ne_ok + · rename_i columns found + obtain ⟨stringCells, blobCells, canonical⟩ := rowCells_sound checked + refine ⟨columns, found, stringCells, blobCells, fun strings blobs inStrings inBlobs => + ⟨place.1, valueBytes place.2 value, ?_, ?_, ?_⟩⟩ + · rw [placed, sameLength] + · rw [sameLength]; exact bytes_of_slice bounded readRow + · unfold canonicalRow + rw [found] + exact canonical strings blobs inStrings inBlobs + +#print axioms rowCellsAt_sound + +/-- A successful check of carried rows: each returned cell is from a row, and under numberings + the cells hold of, every row is the image's. -/ +theorem rowsCells_sound {image : PreparedImage} (bounded : image.view.Bounded) : + ∀ {rows : List ByteEntry} {cells : HeapCells}, rowsCells image rows = .ok cells → + (∀ cell ∈ cells.1, cell.1 ≠ 0 ∧ (cell.2 ≠ 0 → ∃ row ∈ rows, ∃ columns, + image.columnsOf (row.1 / 2 ^ 24) = some columns ∧ + cell.2 ∈ rowKeys stringsHeap columns row.2)) ∧ + (∀ cell ∈ cells.2, cell.1 ≠ 0 ∧ (cell.2 ≠ 0 → ∃ row ∈ rows, ∃ columns, + image.columnsOf (row.1 / 2 ^ 24) = some columns ∧ + cell.2 ∈ rowKeys blobHeap columns row.2)) ∧ + ∀ strings blobs, (∀ cell ∈ cells.1, Assigned strings cell) → + (∀ cell ∈ cells.2, Assigned blobs cell) → + ∀ row ∈ rows, RowAt image strings blobs row.1 row.2 + | [], cells, checked => by + simp only [rowsCells, pure, Except.pure, Except.ok.injEq] at checked + subst checked + simp + | row :: rest, cells, checked => by + simp only [rowsCells] at checked + obtain ⟨here, hereChecked, checked⟩ := except_bind_ok checked + obtain ⟨later, laterChecked, checked⟩ := except_bind_ok checked + simp only [pure, Except.pure, Except.ok.injEq] at checked + subst checked + obtain ⟨columns, found, hereStrings, hereBlobs, hereRow⟩ := + rowCellsAt_sound bounded hereChecked + obtain ⟨laterStrings, laterBlobs, laterRows⟩ := rowsCells_sound bounded laterChecked + refine ⟨fun cell member => ?_, fun cell member => ?_, + fun strings blobs inStrings inBlobs other member => ?_⟩ + · rcases List.mem_append.mp member with inHere | inLater + · obtain ⟨nonzero, held⟩ := hereStrings cell inHere + exact ⟨nonzero, fun keyNonzero => + ⟨row, List.mem_cons_self, columns, found, held keyNonzero⟩⟩ + · obtain ⟨nonzero, held⟩ := laterStrings cell inLater + refine ⟨nonzero, fun keyNonzero => ?_⟩ + obtain ⟨holder, inRest, rest⟩ := held keyNonzero + exact ⟨holder, List.mem_cons_of_mem _ inRest, rest⟩ + · rcases List.mem_append.mp member with inHere | inLater + · obtain ⟨nonzero, held⟩ := hereBlobs cell inHere + exact ⟨nonzero, fun keyNonzero => + ⟨row, List.mem_cons_self, columns, found, held keyNonzero⟩⟩ + · obtain ⟨nonzero, held⟩ := laterBlobs cell inLater + refine ⟨nonzero, fun keyNonzero => ?_⟩ + obtain ⟨holder, inRest, rest⟩ := held keyNonzero + exact ⟨holder, List.mem_cons_of_mem _ inRest, rest⟩ + · rcases List.mem_cons.mp member with same | inRest + · subst same + exact hereRow strings blobs + (fun cell inHere => inStrings cell (List.mem_append_left _ inHere)) + (fun cell inHere => inBlobs cell (List.mem_append_left _ inHere)) + · exact laterRows strings blobs + (fun cell inLater => inStrings cell (List.mem_append_right _ inLater)) + (fun cell inLater => inBlobs cell (List.mem_append_right _ inLater)) other inRest + +#print axioms rowsCells_sound + +/-- When the rows check succeeds on a view of a well-formed image, the carried rows and entries + are the image's under one numbering per heap. -/ +theorem rowsMatch_of_rowsBound {image : PreparedImage} {rows strings blobs : List ByteEntry} + (bounded : image.view.Bounded) (bound : rowsBound image rows strings blobs = .ok ()) : + RowsMatch image rows strings blobs := by + unfold rowsBound at bound + obtain ⟨cells, cellsChecked, bound⟩ := except_bind_ok bound + obtain ⟨_, stringOffsets, bound⟩ := except_bind_ok bound + obtain ⟨_, blobOffsets, bound⟩ := except_bind_ok bound + obtain ⟨stringKeys, stringKeysChecked, bound⟩ := except_bind_ok bound + obtain ⟨blobKeys, blobKeysChecked, bound⟩ := except_bind_ok bound + obtain ⟨_, _, bound⟩ := except_bind_ok bound + obtain ⟨_, _, bound⟩ := except_bind_ok bound + obtain ⟨_, stringsJoined, blobsJoined⟩ := except_bind_ok bound + obtain ⟨stringCells, blobCells, rowsAt⟩ := rowsCells_sound bounded cellsChecked + obtain ⟨stringNumbering, stringAssigned, stringEntries, stringCovered, stringFrom⟩ := + heap_of_bounds (fun _ _ => stringAt_of_stringFound bounded) + (fun cell member => (stringCells cell member).1) stringOffsets stringKeysChecked + stringsJoined + obtain ⟨blobNumbering, blobAssigned, blobEntries, blobCovered, blobFrom⟩ := + heap_of_bounds (fun _ _ => sizedAt_of_sizedFound bounded) + (fun cell member => (blobCells cell member).1) blobOffsets blobKeysChecked blobsJoined + refine ⟨keyOfCells cells.1, keyOfCells cells.2, stringNumbering, blobNumbering, + rowsAt _ _ stringAssigned blobAssigned, stringEntries, blobEntries, stringCovered, + blobCovered, fun offset key given => ?_, fun offset key given => ?_⟩ + · obtain ⟨member, nonzero⟩ := stringFrom offset key given + exact (stringCells _ member).2 nonzero + · obtain ⟨member, nonzero⟩ := blobFrom offset key given + exact (blobCells _ member).2 nonzero + +#print axioms rowsMatch_of_rowsBound + +/-- When the assembly check succeeds on a view of a well-formed image, the assembly block is the + image's. -/ +theorem assemblyMatches_of_assemblyBound {image : PreparedImage} {assembly : AssemblyMetadata} + (bounded : image.view.Bounded) (bound : assemblyBound image assembly = .ok ()) : + AssemblyMatches image assembly := by + unfold assemblyBound at bound + obtain ⟨_, headerChecked, rowsChecked⟩ := except_bind_ok bound + obtain ⟨headerAt, decoded⟩ := header_of_headerBound bounded headerChecked + exact ⟨headerAt, decoded, rowsMatch_of_rowsBound bounded rowsChecked⟩ + +#print axioms assemblyMatches_of_assemblyBound + +/-- Every image `prepareOne` decodes has a view of a well-formed image. -/ +theorem prepareOne_bounded {pinned : PinnedImage} {image : PreparedImage} + (prepared : prepareOne pinned = .ok image) : image.view.Bounded := by + unfold prepareOne at prepared + obtain ⟨view, built, prepared⟩ := except_bind_ok prepared + obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + split at prepared + · exact absurd prepared throw_ne_ok + · obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + injection prepared with prepared + subst prepared + exact ofImage_bounded built + +#print axioms prepareOne_bounded + +/-- Every image `prepareOne` decodes carries the column table of its own layout, so `RowAt` reads + the schema's columns at the image's widths. -/ +theorem prepareOne_columns {pinned : PinnedImage} {image : PreparedImage} + (prepared : prepareOne pinned = .ok image) : image.columns = image.layout.columnTable := by + unfold prepareOne at prepared + obtain ⟨view, _, prepared⟩ := except_bind_ok prepared + obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + split at prepared + · exact absurd prepared throw_ne_ok + · obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + injection prepared with prepared + subst prepared + rfl + +#print axioms prepareOne_columns + +/-- A prepared image's MethodDef RVA column is the first four bytes of a row, so `maskRva` zeroes + exactly them. -/ +theorem rvaColumn_of_prepareOne {pinned : PinnedImage} {image : PreparedImage} + (prepared : prepareOne pinned = .ok image) : image.rvaColumn = (0, 4) := by + unfold prepareOne at prepared + obtain ⟨view, _, prepared⟩ := except_bind_ok prepared + obtain ⟨layout, _, prepared⟩ := except_bind_ok prepared + obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + split at prepared + · exact absurd prepared throw_ne_ok + · obtain ⟨rva, rvaOk, prepared⟩ := except_bind_ok prepared + obtain ⟨_, _, prepared⟩ := except_bind_ok prepared + injection prepared with prepared + subst prepared + simp [MetadataLayout.columnIn, schema, placeColumns, columnWidth, pure, Except.pure] + at rvaOk + exact rvaOk.symm + +#print axioms rvaColumn_of_prepareOne + +/-- In a prepared image every column of `kind` in every table is as wide as `kind` is in the + image, so all cells of one heap share the width of its omitted-entry marker. -/ +theorem columnWidth_of_prepareOne {pinned : PinnedImage} {image : PreparedImage} + (prepared : prepareOne pinned = .ok image) (kind : Column) : + ∀ table columns, image.columnsOf table = some columns → ∀ place ∈ columns, + place.column = kind → + place.width = columnWidth image.layout.heapSizes image.layout.rowCount kind := by + intro table columns found place member isKind + unfold PreparedImage.columnsOf at found + rw [prepareOne_columns prepared, columnTable_getD] at found + cases schemaAt : schema table with + | none => simp [schemaAt] at found + | some all => + simp only [schemaAt, Option.map_some, Option.some.injEq] at found + subst found + rw [placeColumns_width _ _ _ _ place member, isKind] + +#print axioms columnWidth_of_prepareOne + +/-- Every `#Strings` column of a prepared image is `stringWidth` bytes wide. -/ +theorem stringColumns_of_prepareOne {pinned : PinnedImage} {image : PreparedImage} + (prepared : prepareOne pinned = .ok image) : + ∀ table columns, image.columnsOf table = some columns → + ∀ place ∈ columns, place.column = .string → place.width = stringWidth image := + columnWidth_of_prepareOne prepared .string + +#print axioms stringColumns_of_prepareOne + +/-- Every `#Blob` column of a prepared image is `blobWidth` bytes wide. -/ +theorem blobColumns_of_prepareOne {pinned : PinnedImage} {image : PreparedImage} + (prepared : prepareOne pinned = .ok image) : + ∀ table columns, image.columnsOf table = some columns → + ∀ place ∈ columns, place.column = .blob → place.width = blobWidth image := + columnWidth_of_prepareOne prepared .blob + +#print axioms blobColumns_of_prepareOne + +end StrictBinding diff --git a/StrictBinding/Sha256.lean b/StrictBinding/Sha256.lean new file mode 100644 index 000000000..25549ad34 --- /dev/null +++ b/StrictBinding/Sha256.lean @@ -0,0 +1,143 @@ +import StrictBinding.Image + +/-! +# SHA-256 over image chunks + +FIPS 180-4 SHA-256 written for kernel evaluation: state as lists, and direct `Nat` functions +(`Nat.xor`, `Nat.land`, `Nat.shiftRight`), which the kernel reduces natively. A message is its +64-byte chunks (`StrictBinding.ImageBytes`) and its length; `padded` appends the `0x80` byte and +the bit length in the last block. `DigestIs` is the statement audited theorems make about an image +constant: the image is well formed and its SHA-256 is the stated number. +-/ + +namespace StrictBinding.Sha256 + +open CIL.Crypto (add xor3 choose majority blockWords padded wordsValue) + +/-- Rotation of a 32-bit word to the right. -/ +def rotr (word amount : Nat) : Nat := + Nat.lor (Nat.shiftRight word amount) (Nat.mod (Nat.shiftLeft word (32 - amount)) 4294967296) + +/-- The compression function's upper-case sigma of `a`. -/ +def bigSigma0 (word : Nat) : Nat := xor3 (rotr word 2) (rotr word 13) (rotr word 22) + +/-- The compression function's upper-case sigma of `e`. -/ +def bigSigma1 (word : Nat) : Nat := xor3 (rotr word 6) (rotr word 11) (rotr word 25) + +/-- The message schedule's lower-case sigma of the second word back. -/ +def smallSigma0 (word : Nat) : Nat := xor3 (rotr word 7) (rotr word 18) (Nat.shiftRight word 3) + +/-- The message schedule's lower-case sigma of the fifteenth word back. -/ +def smallSigma1 (word : Nat) : Nat := xor3 (rotr word 17) (rotr word 19) (Nat.shiftRight word 10) + +/-- The 64 round constants. -/ +def roundConstants : List Nat := + [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2] + +/-- The state before the first block. -/ +def initialState : List Nat := + [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19] + +/-- The `count` message schedule words that follow the sixteen words of a block. -/ +def expand : Nat → List Nat → List Nat + | 0, _ => [] + | count + 1, w0 :: w1 :: w2 :: w3 :: w4 :: w5 :: w6 :: w7 :: w8 :: w9 :: w10 :: w11 :: w12 :: + w13 :: w14 :: w15 :: _ => + let next := add (add (smallSigma1 w14) w9) (add (smallSigma0 w1) w0) + next :: expand count [w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15, next] + | _ + 1, _ => [] + +/-- The 64 rounds over the constants and schedule words, returning the working variables. -/ +def rounds : List Nat → List Nat → Nat → Nat → Nat → Nat → Nat → Nat → Nat → Nat → List Nat + | k :: ks, w :: ws, a, b, c, d, e, f, g, h => + let t1 := add (add (add h (bigSigma1 e)) (add (choose e f g) k)) w + let t2 := add (bigSigma0 a) (majority a b c) + rounds ks ws (add t1 t2) a b c (add d t1) e f g + | _, _, a, b, c, d, e, f, g, h => [a, b, c, d, e, f, g, h] + +/-- One block applied to the state. -/ +def compress (state : List Nat) (block : Nat) : List Nat := + match state with + | [a, b, c, d, e, f, g, h] => + let words := blockWords block + match rounds roundConstants (words ++ expand 48 words) a b c d e f g h with + | [a', b', c', d', e', f', g', h'] => + [add a a', add b b', add c c', add d d', add e e', add f f', add g g', add h h'] + | _ => [] + | _ => [] + +/-- All blocks applied in order. Both branches are equal; testing the state sum makes the kernel + evaluate each block's state once instead of once per use. -/ +def hashBlocks : List Nat → List Nat → List Nat + | state, [] => state + | state, block :: blocks => + let next := compress state block + bif Nat.beq (next.foldl Nat.add 0) 0 then hashBlocks next blocks else hashBlocks next blocks + +/-- All pages hashed in order, each from the state the previous one left. -/ +def hashPages : List Nat → List (List Nat) → List Nat + | state, [] => state + | state, page :: pages => hashPages (hashBlocks state page) pages + +/-- The pages to hash for a message of `length` bytes: every page but the last as it is, and the + last page with the padding blocks. -/ +def hashPlan (pages : List (List Nat)) (length : Nat) : List (List Nat) := + pages.dropLast ++ [padded (pages.getLastD []) length] + +/-- The SHA-256 digest, as a number, of a message given as pages of zero-padded chunks and its + length. -/ +def ofPages (pages : List (List Nat)) (length : Nat) : Nat := + wordsValue (hashPages initialState (hashPlan pages length)) + +/-- Hashing `pages` from `state` gives `final`, with each page's result stated separately. One + kernel evaluation of a whole image holds every intermediate term of every block; stating + pages as separate lemmas bounds the memory of each evaluation to one page. -/ +inductive PagesHash : List Nat → List (List Nat) → List Nat → Prop where + | nil (state : List Nat) : PagesHash state [] state + | cons {state next final page : List Nat} {pages : List (List Nat)} : + hashBlocks state page = next → PagesHash next pages final → + PagesHash state (page :: pages) final + +/-- A chain of page results is the hash of the pages. -/ +theorem PagesHash.hashPages_eq {state : List Nat} {pages : List (List Nat)} {final : List Nat} + (chain : PagesHash state pages final) : hashPages state pages = final := by + induction chain with + | nil => rfl + | cons step _ ih => rw [hashPages, step]; exact ih + +#print axioms PagesHash.hashPages_eq + +end StrictBinding.Sha256 + +namespace StrictBinding + +/-- The image is well formed and its SHA-256 digest is `digest`. -/ +def DigestIs (image : ImageBytes) (digest : Nat) : Prop := + image.wellFormed = true ∧ Sha256.ofPages image.pages image.length = digest + +instance (image : ImageBytes) (digest : Nat) : Decidable (DigestIs image digest) := + inferInstanceAs (Decidable (_ ∧ _)) + +/-- The digest of an image from per-page hash results: `blocks` are the pages to hash, `chain` + hashes them to `final`, and `final` is the number `digest`. Each page result can be checked + in its own kernel evaluation. -/ +theorem digestIs_of_hashes {image : ImageBytes} {digest : Nat} {final : List Nat} + {blocks : List (List Nat)} (plan : Sha256.hashPlan image.pages image.length = blocks) + (chain : Sha256.PagesHash Sha256.initialState blocks final) + (wellFormed : image.wellFormed = true) (value : CIL.Crypto.wordsValue final = digest) : + DigestIs image digest := by + refine ⟨wellFormed, ?_⟩ + unfold Sha256.ofPages + rw [plan, chain.hashPages_eq] + exact value + +#print axioms digestIs_of_hashes + +end StrictBinding diff --git a/StrictBinding/Slice.lean b/StrictBinding/Slice.lean new file mode 100644 index 000000000..b18a9d07c --- /dev/null +++ b/StrictBinding/Slice.lean @@ -0,0 +1,85 @@ +import StrictBinding.Rows + +/-! +Carried user strings and complete metadata slices checked against one prepared image. +Assembly entries and row entries have separate numberings, as the extractor emits them. +-/ + +namespace StrictBinding + +open CIL.Extraction (ByteEntry MetadataSlice userStringHeap) +open CIL.Safety (except_bind_ok) + +/-- Each keyed offset has the image's prefix and exact payload inside the heap. + Entry starts reached by a heap walk or named by code are outside this claim. -/ +def UserStringsMatch (image : PreparedImage) (entries : List ByteEntry) : Prop := + forall entry, List.Mem entry entries -> SizedAt image image.layout.pe.userStrings entry.1 entry.2 + +/-- User strings at image offsets, with fixed prefix reads and one full comparison per entry. + The cost is linear in the entries and their bytes, independent of rows and methods. -/ +def userStringsBound (image : PreparedImage) : List ByteEntry -> Except AuditError Unit + | [] => pure () + | entry :: rest => do + entryBound userStringHeap (sizedFound image image.layout.pe.userStrings) entry.1 entry + userStringsBound image rest + +/-- Success binds each user string's prefix, length, bounds and bytes to the image. -/ +theorem userStringsMatch_of_userStringsBound {image : PreparedImage} + (bounded : image.view.Bounded) {entries : List ByteEntry} + (bound : userStringsBound image entries = .ok ()) : UserStringsMatch image entries := by + induction entries with + | nil => intro entry member; cases member + | cons entry rest onRest => + cases except_bind_ok bound with + | intro value results => + intro other member + cases member with + | head => exact sizedAt_of_sizedFound bounded (found_of_entryBound results.1) + | tail _ member => exact onRest results.2 other member + +#print axioms userStringsMatch_of_userStringsBound + +/-- Assembly and row heaps have separate numberings; user-string keys are image offsets. + Identity, key tokens, corlib and links are outside this claim. -/ +def SliceMatches (image : PreparedImage) (slice : MetadataSlice) : Prop := + And (AssemblyMatches image slice.assembly) + (And (RowsMatch image slice.rows slice.strings slice.blobs) + (UserStringsMatch image slice.userStrings)) + +/-- The rows and carried heaps of a slice checked against one prepared image, without its + Assembly block. Cost is the row check plus the user-string entries and bytes compared. -/ +def sliceContentsBound (image : PreparedImage) (slice : MetadataSlice) : + Except AuditError Unit := do + rowsBound image slice.rows slice.strings slice.blobs + userStringsBound image slice.userStrings + +/-- The Assembly block, rows and carried heaps checked against one prepared image. + Cost is the assembly check plus the cost of `sliceContentsBound`. -/ +def sliceBound (image : PreparedImage) (slice : MetadataSlice) : Except AuditError Unit := do + assemblyBound image slice.assembly + sliceContentsBound image slice + +/-- A successful Assembly block check and contents check bind the whole slice to the image. -/ +theorem sliceMatches_of_bounds {image : PreparedImage} {slice : MetadataSlice} + (bounded : image.view.Bounded) (assembly : assemblyBound image slice.assembly = .ok ()) + (contents : sliceContentsBound image slice = .ok ()) : SliceMatches image slice := by + cases except_bind_ok contents with + | intro value checks => + exact And.intro (assemblyMatches_of_assemblyBound bounded assembly) + (And.intro (rowsMatch_of_rowsBound bounded checks.1) + (userStringsMatch_of_userStringsBound bounded checks.2)) + +#print axioms sliceMatches_of_bounds + +/-- Success binds the assembly block, rows and all carried heaps to the prepared image. -/ +theorem sliceMatches_of_sliceBound {image : PreparedImage} {slice : MetadataSlice} + (bounded : image.view.Bounded) (bound : sliceBound image slice = .ok ()) : + SliceMatches image slice := by + cases except_bind_ok bound with + | intro value results => + cases value + exact sliceMatches_of_bounds bounded results.1 results.2 + +#print axioms sliceMatches_of_sliceBound + +end StrictBinding diff --git a/StrictBinding/Sorted.lean b/StrictBinding/Sorted.lean new file mode 100644 index 000000000..b59071ac1 --- /dev/null +++ b/StrictBinding/Sorted.lean @@ -0,0 +1,184 @@ +import StrictBinding.Evidence + +/-! +# Tables sorted by a key + +ECMA-335 requires several metadata tables to be sorted by a primary key, and GenericParam by a +secondary key as well (II.22). A claim names a table of a pinned image and the columns of its +key; `sortedBound` reads the key of every row once and fails at the first row whose key is +below the key of the row before it. `KeysSorted` states the order as reads of the image and +`keysSorted_of_sortedBound` proves that a successful check means it. The audit never reads the +`Sorted` mask of the table stream header, which the standard defines only as a bit vector of +sorted tables. Checks that read a few rows of a table, such as the nesting of a type definition, +conclude facts about the whole table only together with this fact. +-/ + +namespace StrictBinding + +open CIL.Safety (except_bind_ok) + +/-- The key of row `row` of `table`: the values of the key columns, in order. -/ +def keyAt (image : PreparedImage) (table row : Nat) (key : List Nat) : + Except AuditError (List Nat) := + key.mapM fun column => image.layout.cellOfRow image.view table row column + +/-- Whether key `lower` is at most key `upper` in lexicographic order. -/ +def keyLe : List Nat → List Nat → Bool + | first :: rest, other :: others => first < other || (first == other && keyLe rest others) + | _, _ => true + +/-- Every row of `table` in the image has a key at most the key of the row after it. -/ +def KeysSorted (image : PreparedImage) (table : Nat) (key : List Nat) : Prop := + ∀ row, 1 ≤ row → row < image.layout.rowCount table → + ∃ lower upper, keyAt image table row key = .ok lower ∧ + keyAt image table (row + 1) key = .ok upper ∧ keyLe lower upper = true + +/-- The rows after `row`, at most `fuel` of them, each have a key at least the key of the row + before; `previous` is the key of `row`. -/ +def sortedFrom (image : PreparedImage) (table : Nat) (key : List Nat) : + Nat → Nat → List Nat → Except AuditError Unit + | 0, _, _ => pure () + | fuel + 1, row, previous => do + let next ← keyAt image table (row + 1) key + if keyLe previous next then sortedFrom image table key fuel (row + 1) next + else throw (.unsortedTable table (row + 1)) + +/-- The rows of `table` are sorted by `key`. The check reads the key of each row once. -/ +def sortedBound (image : PreparedImage) (table : Nat) (key : List Nat) : + Except AuditError Unit := + match image.layout.rowCount table with + | 0 => pure () + | count + 1 => do + let first ← keyAt image table 1 key + sortedFrom image table key count 1 first + +/-- A claim that `table` of the image pinned under `identity` is sorted by the columns `key`. -/ +structure SortedClaim where + identity : String + table : Nat + key : List Nat + deriving DecidableEq, Repr + +/-- The claim holds in the image pinned under the claim's identity. -/ +def sortedClaimBound (prepared : List PreparedImage) (claim : SortedClaim) : + Except AuditError Unit := do + sortedBound (← located prepared claim.identity) claim.table claim.key + +/-- Every claimed table is sorted by its key in the image its identity names, against one + preparation of the images. -/ +def TablesSorted (images : List PinnedImage) (claims : List SortedClaim) : Prop := + ClaimsHold sortedClaimBound images claims + +instance (images : List PinnedImage) (claims : List SortedClaim) : + Decidable (TablesSorted images claims) := + inferInstanceAs (Decidable (ClaimsHold _ _ _)) + +/-- A successful walk from `row` means each of the walked rows has a key at least the key of the + row before it. -/ +theorem sortedFrom_ok {image : PreparedImage} {table : Nat} {key : List Nat} : + ∀ {fuel row : Nat} {previous : List Nat}, + sortedFrom image table key fuel row previous = .ok () → + keyAt image table row key = .ok previous → + ∀ at_, row ≤ at_ → at_ < row + fuel → + ∃ lower upper, keyAt image table at_ key = .ok lower ∧ + keyAt image table (at_ + 1) key = .ok upper ∧ keyLe lower upper = true + | 0, _, _, _, _, at_, low, high => by omega + | fuel + 1, row, previous, walked, readPrevious, at_, low, high => by + unfold sortedFrom at walked + obtain ⟨next, readNext, walked⟩ := except_bind_ok walked + split at walked + · rename_i ordered + by_cases here : at_ = row + · subst here + exact ⟨previous, next, readPrevious, readNext, ordered⟩ + · exact sortedFrom_ok walked readNext at_ (by omega) (by omega) + · exact absurd walked throw_ne_ok + +#print axioms sortedFrom_ok + +/-- A table check that succeeds means the table is sorted by the key. -/ +theorem keysSorted_of_sortedBound {image : PreparedImage} {table : Nat} {key : List Nat} + (checked : sortedBound image table key = .ok ()) : KeysSorted image table key := by + intro row low high + unfold sortedBound at checked + split at checked + · rename_i empty + omega + · rename_i count rows + obtain ⟨first, readFirst, walked⟩ := except_bind_ok checked + exact sortedFrom_ok walked readFirst row low (by omega) + +#print axioms keysSorted_of_sortedBound + +/-- Every claimed table of a successful check is sorted in the image its identity selects, from + one preparation of the pinned images. -/ +theorem keysSorted_of_tablesSorted {images : List PinnedImage} {claims : List SortedClaim} + (bound : TablesSorted images claims) : + ∃ prepared, prepare images = .ok prepared ∧ + ∀ claim ∈ claims, ∃ image, located prepared claim.identity = .ok image ∧ + KeysSorted image claim.table claim.key := by + obtain ⟨prepared, decoded, checks⟩ := check_of_claimsHold bound + refine ⟨prepared, decoded, fun claim member => ?_⟩ + have one := checks claim member + unfold sortedClaimBound at one + cases found : located prepared claim.identity with + | error error => simp [found, bind, Except.bind, ok] at one + | ok image => + refine ⟨image, rfl, keysSorted_of_sortedBound ?_⟩ + cases result : sortedBound image claim.table claim.key with + | error error => simp [found, result, bind, Except.bind, ok] at one + | ok value => cases value; rfl + +#print axioms keysSorted_of_tablesSorted + +/-- A one-column key is the column's value. -/ +theorem keyAt_single {image : PreparedImage} {table row column : Nat} {key : List Nat} + (read : keyAt image table row [column] = .ok key) : + ∃ value, image.layout.cellOfRow image.view table row column = .ok value ∧ + key = [value] := by + unfold keyAt at read + cases cell : image.layout.cellOfRow image.view table row column with + | error error => simp [cell, List.mapM_cons, bind, Except.bind] at read + | ok value => + simp only [cell, List.mapM_cons, List.mapM_nil, bind, Except.bind, pure, Except.pure, + Except.ok.injEq] at read + exact ⟨value, rfl, read.symm⟩ + +#print axioms keyAt_single + +/-- One-column keys are ordered as their values. -/ +theorem keyLe_single {lower upper : Nat} (ordered : keyLe [lower] [upper] = true) : + lower ≤ upper := by + simp only [keyLe, Bool.or_eq_true, decide_eq_true_eq, Bool.and_eq_true, beq_iff_eq, + and_true] at ordered + omega + +#print axioms keyLe_single + +/-- In a table sorted by one column, a row's value is at most the value of any later row. -/ +theorem value_le_of_keysSorted {image : PreparedImage} {table column : Nat} + (sorted : KeysSorted image table [column]) : + ∀ {lower upper first last : Nat}, 1 ≤ lower → lower ≤ upper → + upper ≤ image.layout.rowCount table → + image.layout.cellOfRow image.view table lower column = .ok first → + image.layout.cellOfRow image.view table upper column = .ok last → first ≤ last + | lower, 0, _, _, low, ordered, _, _, _ => by omega + | lower, upper + 1, first, last, low, ordered, inside, readFirst, readLast => by + by_cases same : lower = upper + 1 + · subst same + rw [readFirst] at readLast + cases readLast + exact Nat.le_refl _ + · obtain ⟨below, above, readBelow, readAbove, keyed⟩ := + sorted upper (by omega) (by omega) + obtain ⟨middle, readMiddle, rfl⟩ := keyAt_single readBelow + obtain ⟨value, readValue, rfl⟩ := keyAt_single readAbove + rw [readLast] at readValue + cases readValue + exact Nat.le_trans + (value_le_of_keysSorted sorted low (by omega) (by omega) readFirst readMiddle) + (keyLe_single keyed) + +#print axioms value_le_of_keysSorted + +end StrictBinding diff --git a/StrictBinding/StaticData.lean b/StrictBinding/StaticData.lean new file mode 100644 index 000000000..1235723ef --- /dev/null +++ b/StrictBinding/StaticData.lean @@ -0,0 +1,155 @@ +import StrictBinding.Bind +import StrictBinding.Reads +import CIL.Extraction.Translation +import CIL.Extraction.StaticIdentities + +/-! +# Static data in a PE image + +The FieldRVA table associates a Field row with an RVA. Build its index once per image, then +check the Field flags, the table row, the mapped range and the bytes for each access. +-/ + +namespace StrictBinding + +/-- ECMA-335 II.22.18, FieldRVA. Its columns are the RVA and the Field index. -/ +def fieldRvaTable : Nat := 0x1d + +/-- Index the physical rows of a table by the full token of the Field or TypeDef each row + names. Reject duplicate keys independently of the table header's sorted bit. -/ +def dataTableIndex (image : PreparedImage) (table column target : Nat) : + Except AuditError (Std.TreeMap Nat Nat) := do + let mut rows : Std.TreeMap Nat Nat := {} + let count := image.layout.rowCount target + for i in [:image.layout.rowCount table] do + let row := i + 1 + let key ← image.layout.cellOfRow image.view table row column + let token := tokenOf target key + if key == 0 || key > count || rows.contains token then + throw (.invalidDataTableKey table row key) + rows := rows.insert token row + return rows + +/-- The FieldRVA row of each Field that has one. -/ +def fieldRvaIndex (image : PreparedImage) : Except AuditError (Std.TreeMap Nat Nat) := + dataTableIndex image fieldRvaTable 1 CIL.Extraction.Table.field + +/-- The bytes attached to a field are found through its physical FieldRVA row. -/ +def fieldDataAt (image : PreparedImage) (field row alignment : Nat) (bytes : List Nat) : + Except AuditError Unit := do + let flags ← image.layout.cell image.view field 0 + let index ← image.layout.cellOfRow image.view fieldRvaTable row 1 + let rva ← image.layout.cellOfRow image.view fieldRvaTable row 0 + if field != tokenOf CIL.Extraction.Table.field index || flags &&& 0x110 != 0x110 || + rva == 0 || bytes.isEmpty || !(alignment ∈ [1, 2, 4, 8]) || rva % alignment != 0 then + throw (.invalidFieldData field) + let offset ← mapRva image.layout.pe.sections rva bytes.length + if offset % alignment != 0 then throw (.invalidFieldData field) + unless ← image.view.hasBytesAt offset bytes do throw (.fieldDataMismatch field rva offset) + +/-- A successful comparison places the claimed bytes in the image section at the RVA read + from the FieldRVA row naming this field. -/ +theorem bytes_of_fieldDataAt {image : PreparedImage} (bounded : image.view.Bounded) + {field row alignment : Nat} {bytes : List Nat} + (h : fieldDataAt image field row alignment bytes = .ok ()) : + ∃ index rva offset, + image.layout.cellOfRow image.view fieldRvaTable row 1 = .ok index ∧ + field = tokenOf CIL.Extraction.Table.field index ∧ + image.layout.cellOfRow image.view fieldRvaTable row 0 = .ok rva ∧ + mapRva image.layout.pe.sections rva bytes.length = .ok offset ∧ + image.view.bytes offset bytes.length = .ok bytes := by + unfold fieldDataAt at h + obtain ⟨flags, _, h⟩ := CIL.Safety.except_bind_ok h + obtain ⟨index, fieldRead, h⟩ := CIL.Safety.except_bind_ok h + obtain ⟨rva, rvaRead, h⟩ := CIL.Safety.except_bind_ok h + split at h + · contradiction + next permitted => + obtain ⟨offset, mapped, h⟩ := CIL.Safety.except_bind_ok h + split at h + · contradiction + obtain ⟨same, compared, h⟩ := CIL.Safety.except_bind_ok h + cases same with + | false => contradiction + | true => + have named : field = tokenOf CIL.Extraction.Table.field index := by + by_cases named : field = tokenOf CIL.Extraction.Table.field index + · exact named + · simp [named] at permitted + exact ⟨index, rva, offset, fieldRead, named, rvaRead, mapped, + bytes_of_hasBytesAt bounded compared⟩ + +#print axioms bytes_of_fieldDataAt + +/-- Physical indices shared by all accesses to static data in one assembly. -/ +private structure StaticTables where + image : PreparedImage + fields : Std.TreeMap Nat Nat + layouts : Std.TreeMap Nat Nat + +/-- ClassLayout stores packing, declared size and the owning TypeDef. -/ +def classLayoutTable : Nat := 0x0f + +private def StaticTables.read (image : PreparedImage) : Except AuditError StaticTables := do + return ⟨image, ← fieldRvaIndex image, + ← dataTableIndex image classLayoutTable 2 CIL.Extraction.Table.typeDef⟩ + +/-- The admitted RVA field has an explicit-layout local value type. Its field signature + identifies the TypeDef whose ClassLayout row supplies its packing and full byte count. -/ +private def StaticTables.site (tables : StaticTables) (token : Nat) + (site : CIL.Extraction.StaticFieldSite) : Except AuditError Unit := do + let image := tables.image + let cell := image.layout.cell image.view + if CIL.Extraction.metadataTableOf token != CIL.Extraction.Table.field then + throw (.invalidFieldData token) + let flags ← cell token 0 + match site.rvaSize, tables.fields[token]? with + | none, none => + unless flags &&& 0x110 == 0x10 do throw (.invalidFieldData token) + | some size, some row => + let signature ← image.layout.blobAt image.view (← cell token 2) + let .ok (.named true owner) := CIL.Extraction.decodeFieldSig signature + | throw (.invalidFieldData token) + let some layout := tables.layouts[owner]? | throw (.invalidFieldData token) + let layoutCell := image.layout.cellOfRow image.view classLayoutTable layout + let alignment ← layoutCell 0 + unless flags &&& 0x130 == 0x130 && (← cell owner 0) &&& 0x18 == 0x10 && + alignment == site.descriptor.alignment && (← layoutCell 1) == size && + size == site.descriptor.bytes.length do throw (.invalidFieldData token) + fieldDataAt image token row alignment (site.descriptor.bytes.map BitVec.toNat) + | _, _ => throw (.invalidFieldData token) + +/-- Check every claimed static field at its decoded operand. The two physical tables of an + assembly are indexed on its first static access. Constructor-initialized fields must have + no FieldRVA. For image-backed fields the full data, size and alignment must agree. -/ +def checkStaticData (images : List PinnedImage) (evidence : List CIL.Extraction.MethodEvidence) : + Except AuditError Unit := do + let prepared ← prepare images + let mut indexed : Std.TreeMap String StaticTables := {} + for item in evidence do + let sites := item.closed.staticFields + let pcs := sites.map Prod.fst + let some tokens := CIL.Extraction.siteTokens item.body.statics pcs + | throw (.staticSiteMismatch item.original.token) + for ((pc, site), token) in sites.zip tokens do + let some (position, field) := item.body.staticFieldRow token + | throw (.unresolvedStaticField item.original.token pc) + let some slice := item.body.metadata.slices[position]? + | throw (.unresolvedStaticField item.original.token pc) + let name := slice.assembly.identity + let tables ← match indexed[name]? with + | some found => pure found + | none => do StaticTables.read (← located prepared name) + indexed := indexed.insert name tables + tables.site field site + +/-- All RVA static bytes used by the certified extraction occupy the whole field at the + address and alignment recorded in the pinned image. -/ +def StaticDataBound (images : List PinnedImage) (evidence : List CIL.Extraction.MethodEvidence) : + Prop := ok (checkStaticData images evidence) = true + +instance (images : List PinnedImage) (evidence : List CIL.Extraction.MethodEvidence) : + Decidable (StaticDataBound images evidence) := + inferInstanceAs (Decidable (_ = true)) + +end StrictBinding diff --git a/StrictBinding/Tables.lean b/StrictBinding/Tables.lean new file mode 100644 index 000000000..bef339b1b --- /dev/null +++ b/StrictBinding/Tables.lean @@ -0,0 +1,336 @@ +import StrictBinding.Pe +import CIL.Extraction.MetadataToken + +/-! +# Metadata tables + +Decodes the `#~` stream header of an image and computes, from the image alone, where each row +of each table starts and where each column lies in a row. A row's size depends on the row counts +of other tables and on the heap size flags (II.22 and II.24.2.6), so the schema of every table is +spelled out here: a row's position needs the size of all earlier tables. The heaps are read +through the stream positions of `StrictBinding.PeLayout`. +-/ + +namespace StrictBinding + +/-- One column of a metadata table. -/ +inductive Column where + /-- An unsigned integer of this many bytes. -/ + | fixed (width : Nat) + | string + | guid + | blob + /-- A row index into one table. -/ + | index (table : Nat) + /-- A tag of `bits` bits and a row index, into one of `tables` by tag. -/ + | coded (bits : Nat) (tables : List Nat) + +namespace Column + +/-- TypeDef, TypeRef or TypeSpec. -/ +def typeDefOrRef : Column := .coded 2 [0x02, 0x01, 0x1B] + +/-- Field, Param or Property. -/ +def hasConstant : Column := .coded 2 [0x04, 0x08, 0x17] + +/-- Every table that can carry a custom attribute. -/ +def hasCustomAttribute : Column := + .coded 5 [0x06, 0x04, 0x01, 0x02, 0x08, 0x09, 0x0A, 0x00, 0x0E, 0x17, 0x14, 0x11, 0x1A, 0x1B, + 0x20, 0x23, 0x26, 0x27, 0x28, 0x2A, 0x2C, 0x2B] + +/-- Field or Param. -/ +def hasFieldMarshal : Column := .coded 1 [0x04, 0x08] + +/-- TypeDef, MethodDef or Assembly. -/ +def hasDeclSecurity : Column := .coded 2 [0x02, 0x06, 0x20] + +/-- TypeDef, TypeRef, ModuleRef, MethodDef or TypeSpec. -/ +def memberRefParent : Column := .coded 3 [0x02, 0x01, 0x1A, 0x06, 0x1B] + +/-- Event or Property. -/ +def hasSemantics : Column := .coded 1 [0x14, 0x17] + +/-- MethodDef or MemberRef. -/ +def methodDefOrRef : Column := .coded 1 [0x06, 0x0A] + +/-- Field or MethodDef. -/ +def memberForwarded : Column := .coded 1 [0x04, 0x06] + +/-- File, AssemblyRef or ExportedType. -/ +def implementation : Column := .coded 2 [0x26, 0x23, 0x27] + +/-- MethodDef or MemberRef, with tags two and three. -/ +def customAttributeType : Column := .coded 3 [0x06, 0x0A] + +/-- Module, ModuleRef, AssemblyRef or TypeRef. -/ +def resolutionScope : Column := .coded 2 [0x00, 0x1A, 0x23, 0x01] + +/-- TypeDef or MethodDef. -/ +def typeOrMethodDef : Column := .coded 1 [0x02, 0x06] + +end Column + +/-- The columns of table `table`, for every table the metadata format defines up to + GenericParamConstraint (`0x2C`). -/ +def schema : Nat → Option (List Column) + -- Module, TypeRef, TypeDef, FieldPtr, Field, MethodPtr + | 0x00 => some [.fixed 2, .string, .guid, .guid, .guid] + | 0x01 => some [Column.resolutionScope, .string, .string] + | 0x02 => some [.fixed 4, .string, .string, Column.typeDefOrRef, .index 0x04, .index 0x06] + | 0x03 => some [.index 0x04] + | 0x04 => some [.fixed 2, .string, .blob] + | 0x05 => some [.index 0x06] + -- MethodDef, ParamPtr, Param, InterfaceImpl, MemberRef, Constant + | 0x06 => some [.fixed 4, .fixed 2, .fixed 2, .string, .blob, .index 0x08] + | 0x07 => some [.index 0x08] + | 0x08 => some [.fixed 2, .fixed 2, .string] + | 0x09 => some [.index 0x02, Column.typeDefOrRef] + | 0x0A => some [Column.memberRefParent, .string, .blob] + | 0x0B => some [.fixed 2, Column.hasConstant, .blob] + -- CustomAttribute, FieldMarshal, DeclSecurity, ClassLayout, FieldLayout, StandAloneSig + | 0x0C => some [Column.hasCustomAttribute, Column.customAttributeType, .blob] + | 0x0D => some [Column.hasFieldMarshal, .blob] + | 0x0E => some [.fixed 2, Column.hasDeclSecurity, .blob] + | 0x0F => some [.fixed 2, .fixed 4, .index 0x02] + | 0x10 => some [.fixed 4, .index 0x04] + | 0x11 => some [.blob] + -- EventMap, EventPtr, Event, PropertyMap, PropertyPtr, Property + | 0x12 => some [.index 0x02, .index 0x14] + | 0x13 => some [.index 0x14] + | 0x14 => some [.fixed 2, .string, Column.typeDefOrRef] + | 0x15 => some [.index 0x02, .index 0x17] + | 0x16 => some [.index 0x17] + | 0x17 => some [.fixed 2, .string, .blob] + -- MethodSemantics, MethodImpl, ModuleRef, TypeSpec, ImplMap, FieldRVA + | 0x18 => some [.fixed 2, .index 0x06, Column.hasSemantics] + | 0x19 => some [.index 0x02, Column.methodDefOrRef, Column.methodDefOrRef] + | 0x1A => some [.string] + | 0x1B => some [.blob] + | 0x1C => some [.fixed 2, Column.memberForwarded, .string, .index 0x1A] + | 0x1D => some [.fixed 4, .index 0x04] + -- EncLog, EncMap, Assembly, AssemblyProcessor, AssemblyOS, AssemblyRef + | 0x1E => some [.fixed 4, .fixed 4] + | 0x1F => some [.fixed 4] + | 0x20 => + some [.fixed 4, .fixed 2, .fixed 2, .fixed 2, .fixed 2, .fixed 4, .blob, .string, .string] + | 0x21 => some [.fixed 4] + | 0x22 => some [.fixed 4, .fixed 4, .fixed 4] + | 0x23 => + some [.fixed 2, .fixed 2, .fixed 2, .fixed 2, .fixed 4, .blob, .string, .string, .blob] + -- AssemblyRefProcessor, AssemblyRefOS, File, ExportedType, ManifestResource, NestedClass + | 0x24 => some [.fixed 4, .index 0x23] + | 0x25 => some [.fixed 4, .fixed 4, .fixed 4, .index 0x23] + | 0x26 => some [.fixed 4, .string, .blob] + | 0x27 => some [.fixed 4, .fixed 4, .string, .string, Column.implementation] + | 0x28 => some [.fixed 4, .fixed 4, .string, Column.implementation] + | 0x29 => some [.index 0x02, .index 0x02] + -- GenericParam, MethodSpec, GenericParamConstraint + | 0x2A => some [.fixed 2, .fixed 2, Column.typeOrMethodDef, .string] + | 0x2B => some [Column.methodDefOrRef, .blob] + | 0x2C => some [.index 0x2A, Column.typeDefOrRef] + | _ => none + +/-- One more than the last table the schema covers. -/ +def tableLimit : Nat := 0x2D + +/-- The bytes of an index or coded index into tables with at most `rows` rows when the index + carries `bits` tag bits: two while the largest row number and tag fit in sixteen bits. -/ +def indexWidth (bits rows : Nat) : Nat := if rows < 2 ^ (16 - bits) then 2 else 4 + +/-- The bytes of a column, given the heap size flags and the row count of each table. -/ +def columnWidth (heapSizes : Nat) (rows : Nat → Nat) : Column → Nat + | .fixed width => width + | .string => if heapSizes % 2 == 1 then 4 else 2 + | .guid => if heapSizes / 2 % 2 == 1 then 4 else 2 + | .blob => if heapSizes / 4 % 2 == 1 then 4 else 2 + | .index table => indexWidth 0 (rows table) + | .coded bits tables => indexWidth bits ((tables.map rows).foldl Nat.max 0) + +/-- The bytes of a row of a table with these columns. -/ +def rowSize (heapSizes : Nat) (rows : Nat → Nat) (columns : List Column) : Nat := + (columns.map (columnWidth heapSizes rows)).foldl Nat.add 0 + +/-- Where one table's rows are: the file offset of the first row, the bytes per row and the + number of rows. -/ +structure TableRows where + table : Nat + start : Nat + size : Nat + count : Nat + +/-- The row count of `table` among `counts`, zero when the table is absent. -/ +def countOf (counts : List (Nat × Nat)) (table : Nat) : Nat := + ((counts.find? (·.1 == table)).map (·.2)).getD 0 + +/-- The positions of every nonempty table, in table order, when the first row of the first one is + at `start`. -/ +def tablePositions (heapSizes : Nat) (counts : List (Nat × Nat)) (start : Nat) : + List TableRows × Nat := + let step (found : List TableRows × Nat) (table : Nat) : List TableRows × Nat := + let count := countOf counts table + match schema table with + | some columns => + if count == 0 then found + else + let size := rowSize heapSizes (countOf counts) columns + (found.1 ++ [{ table, start := found.2, size, count }], found.2 + count * size) + | none => found + (List.range tableLimit).foldl step ([], start) + +/-- The tables of an image and where they are. -/ +structure MetadataLayout where + pe : PeLayout + /-- The heap size flags of the `#~` header: bit 0 widens `#Strings` indexes, bit 1 `#GUID` + and bit 2 `#Blob`. -/ + heapSizes : Nat + tables : List TableRows + +/-- The heap size flags the audit reads: wide `#Strings`, `#GUID` and `#Blob` indexes and the + extra data flag. Any other bit is rejected. -/ +def knownHeapFlags : Nat := 0x47 + +/-- The flag that adds four bytes after the row counts. -/ +def extraDataFlag : Nat := 0x40 + +/-- The bytes before the row counts of a `#~` stream: reserved, versions, flags, `Valid` and + `Sorted`. -/ +def tableHeaderBytes : Nat := 24 + +/-- The pointer tables (FieldPtr, MethodPtr, ParamPtr, EventPtr, PropertyPtr) and the + edit-and-continue tables (EncLog, EncMap), as a mask of table numbers. -/ +def indirectTableMask : Nat := + 2 ^ 0x03 + 2 ^ 0x05 + 2 ^ 0x07 + 2 ^ 0x13 + 2 ^ 0x16 + 2 ^ 0x1E + 2 ^ 0x1F + +/-- The tables present in the `Valid` mask, with their row counts, and the offset after the + counts. -/ +def readCounts (view : ImageView) (valid cursor : Nat) : + Except AuditError (List (Nat × Nat) × Nat) := + (List.range 64).foldlM (init := ([], cursor)) fun (found, position) table => + if valid / 2 ^ table % 2 == 1 then do + let count ← view.u position 4 + pure (found ++ [(table, count)], position + 4) + else pure (found, position) + +/-- The table positions of an image: the container, then the `#~` header and the tables it + declares, all inside the stream. -/ +def decodeMetadata (view : ImageView) : Except AuditError MetadataLayout := do + let pe ← decodePe view + let start := pe.tables.start + let heapSizes ← view.u (start + 6) 1 + if Nat.land heapSizes (Nat.xor 0xFF knownHeapFlags) != 0 then + throw (.tables (.unsupportedHeapSizes heapSizes)) + let valid ← view.u (start + 8) 8 + if valid / 2 ^ tableLimit != 0 then throw (.tables (.unsupportedTables valid)) + if Nat.land valid indirectTableMask != 0 then throw (.tables (.pointerTables valid)) + let (counts, afterCounts) ← readCounts view valid (start + tableHeaderBytes) + let first := if heapSizes / extraDataFlag % 2 == 1 then afterCounts + 4 else afterCounts + let (tables, end_) := tablePositions heapSizes counts first + if end_ > start + pe.tables.size then throw (.tables .badTableStream) + pure { pe, heapSizes, tables } + +/-- Where the rows of `table` are, when it has any. -/ +def MetadataLayout.rows? (layout : MetadataLayout) (table : Nat) : Option TableRows := + layout.tables.find? (·.table == table) + +/-- The number of rows of `table`. -/ +def MetadataLayout.rowCount (layout : MetadataLayout) (table : Nat) : Nat := + ((layout.rows? table).map (·.count)).getD 0 + +/-- The metadata token of a table and a row already known to fit below its table byte. -/ +def tokenOf (table row : Nat) : Nat := table * 2 ^ 24 + row + +/-- The file offset of the row a token names, and its size; row zero and rows past the count are + out of scope. -/ +def MetadataLayout.rowAt (layout : MetadataLayout) (token : Nat) : Except AuditError (Nat × Nat) := + match layout.rows? (token / 2 ^ 24) with + | some rows => + let row := token % 2 ^ 24 + if row == 0 || row > rows.count then throw (.tokenOutOfScope token) + else pure (rows.start + (row - 1) * rows.size, rows.size) + | none => throw (.tokenOutOfScope token) + +/-- A column of a table as the rows of one image lay it out. -/ +structure ColumnPlace where + /-- Where the column starts within a row. -/ + offset : Nat + /-- The bytes the column takes in each row. -/ + width : Nat + /-- The kind of value the column holds, which decides its width and the heap it indexes. -/ + column : Column + +/-- The columns of a row laid out one after another from `offset`, each as wide as the heap size + flags and row counts make it. -/ +def placeColumns (heapSizes : Nat) (rows : Nat → Nat) : Nat → List Column → List ColumnPlace + | _, [] => [] + | offset, column :: rest => + let width := columnWidth heapSizes rows column + { offset, width, column } :: placeColumns heapSizes rows (offset + width) rest + +/-- The laid-out columns of every table the schema covers, indexed by table number. -/ +def MetadataLayout.columnTable (layout : MetadataLayout) : List (Option (List ColumnPlace)) := + (List.range tableLimit).map fun table => + (schema table).map (placeColumns layout.heapSizes layout.rowCount 0) + +/-- Every laid-out column is as wide as its kind is under the heap size flags and row counts. -/ +theorem placeColumns_width (heapSizes : Nat) (rows : Nat → Nat) : + ∀ (offset : Nat) (columns : List Column), ∀ place ∈ placeColumns heapSizes rows offset columns, + place.width = columnWidth heapSizes rows place.column + | _, [], place, member => by simp [placeColumns] at member + | offset, column :: rest, place, member => by + simp only [placeColumns, List.mem_cons] at member + rcases member with here | later + · subst here; rfl + · exact placeColumns_width heapSizes rows _ rest place later + +#print axioms placeColumns_width + +/-- The schema has no table at or above `tableLimit`. -/ +theorem schema_none_above : ∀ table, tableLimit ≤ table → schema table = none := by + intro table above + unfold tableLimit at above + unfold schema + split <;> first | rfl | omega + +#print axioms schema_none_above + +/-- The column table holds, for every table number, the schema's columns laid out from offset 0, + and nothing for a number the schema does not cover. -/ +theorem columnTable_getD (layout : MetadataLayout) (table : Nat) : + layout.columnTable.getD table none = + (schema table).map (placeColumns layout.heapSizes layout.rowCount 0) := by + unfold MetadataLayout.columnTable + rw [List.getD_eq_getElem?_getD, List.getElem?_map] + by_cases inside : table < tableLimit + · rw [List.getElem?_range inside]; rfl + · rw [List.getElem?_eq_none (by simp; omega), schema_none_above table (by omega)]; rfl + +#print axioms columnTable_getD + +/-- The offset within a row of column `column` of `table`, and its width. -/ +def MetadataLayout.columnIn (layout : MetadataLayout) (table column : Nat) : + Except AuditError (Nat × Nat) := do + let some columns := schema table | throw (.tables (.noSuchColumn table column)) + let some place := (placeColumns layout.heapSizes layout.rowCount 0 columns)[column]? + | throw (.tables (.noSuchColumn table column)) + pure (place.offset, place.width) + +/-- The file offset and width of column `column` of the row a token names. -/ +def MetadataLayout.cellAt (layout : MetadataLayout) (token column : Nat) : + Except AuditError (Nat × Nat) := do + let (row, _) ← layout.rowAt token + let (offset, width) ← layout.columnIn (token / 2 ^ 24) column + pure (row + offset, width) + +/-- The unsigned integer in column `column` of the row a token names. -/ +def MetadataLayout.cell (layout : MetadataLayout) (view : ImageView) (token column : Nat) : + Except AuditError Nat := do + let (offset, width) ← layout.cellAt token column + view.u offset width + +/-- Read a column at a table and row after checking that the row fits in a token. -/ +def MetadataLayout.cellOfRow (layout : MetadataLayout) (view : ImageView) + (table row column : Nat) : Except AuditError Nat := do + let token ← CIL.Extraction.checkedTokenOf table row .rowOverflow + layout.cell view token column + +end StrictBinding diff --git a/StrictBinding/TupleFields.lean b/StrictBinding/TupleFields.lean new file mode 100644 index 000000000..ed8920154 --- /dev/null +++ b/StrictBinding/TupleFields.lean @@ -0,0 +1,96 @@ +import StrictBinding.PlatformType +import CIL.Extraction.Translation + +/-! Verify the declared instance-field order of the platform pair used by a canonical extraction. -/ + +namespace StrictBinding + +open CIL CIL.Extraction + +/-- The base of the public pair is System.ValueType in a core assembly. -/ +private def pairBase (image : PreparedImage) (row : Nat) : Except AuditError Bool := do + let base ← image.layout.cellOfRow image.view Table.typeDef row typeDefExtendsColumn + let table := if base % 4 == 0 then Table.typeDef else Table.typeRef + if base % 4 > 1 || base / 4 == 0 then return false + let token ← checkedTokenOf table (base / 4) .rowOverflow + let cell := image.layout.cell image.view token + unless ← stringFound image (← cell typeNameColumn) (valueTypeName ++ [0]) do return false + unless ← stringFound image (← cell typeNamespaceColumn) (systemNamespace ++ [0]) do return false + return (← kindOf image 0 systemNamespace valueTuplePairName + (.named token systemNamespace valueTypeName)) == .valueType + +/-- Every GenericParam owned by this TypeDef has its expected ordinal. Both slots must exist. -/ +private def pairParameters (image : PreparedImage) (row : Nat) : Except AuditError Bool := do + let cell := image.layout.cellOfRow image.view Table.genericParam + let mut count := 0 + for i in [:image.layout.rowCount Table.genericParam] do + if (← cell (i + 1) 2) == 2 * row then + if (← cell (i + 1) 0) != count then return false + count := count + 1 + return count == 2 + +/-- The two instance fields in declaration order are Item1 of VAR 0 and Item2 of VAR 1. + Static fields do not occupy an instance slot. -/ +private def pairFields (image : PreparedImage) (row : Nat) : Except AuditError Unit := do + unless ← pairBase image row do throw (.invalidTupleDefinition image.identity) + unless ← pairParameters image row do throw (.invalidTupleDefinition image.identity) + let (first, finish) ← platformFieldRange image row + let mut slot := 0 + for field in [first:finish] do + let cell := image.layout.cellOfRow image.view Table.field field + if (← cell 0) &&& 0x10 != 0 then continue + let name := if slot == 0 then firstItemName else secondItemName + if slot ≥ 2 || !(← stringFound image (← cell 1) (name ++ [0])) || + (← image.layout.blobAt image.view (← cell 2)) != [0x06, 0x13, slot] then + throw (.invalidTupleField image.identity field) + slot := slot + 1 + if slot != 2 then throw (.invalidTupleDefinition image.identity) + +/-- A retained TypeRef names the pair in exactly the System.Runtime identity admitted for it. -/ +private def pairRef (index : MetadataIndex) (token : Nat) : Except MetadataError Bool := do + if metadataTableOf token != Table.typeRef then return false + let row ← index.typeRef token + if metadataTableOf row.scope != Table.assemblyRef then return false + if (← index.string row.namespace_) != systemNamespace || + (← index.string row.name) != valueTuplePairName then return false + return (← index.assemblyRefName row.scope runtimeAssembly) == some runtimeAssembly && + (← index.assemblyRefIdentity row.scope) == runtimeScopeBytes + +/-- Bind the defining or forwarded pair to both the selected layout and its scoped TypeRef. -/ +def checkTupleFields (images : List PinnedImage) (evidence : List MethodEvidence) + (layouts : List RawLayout) : Except AuditError Bool := do + unless layouts.any (·.identity.startsWith (runtimeScope ++ "::System.ValueTuple`2<")) do + return false + let mut used := false + for item in evidence do + for slice in item.body.metadata.slices do + let .ok index := slice.index | throw (.invalidTupleDefinition slice.assembly.identity) + for (token, _) in slice.rows do + let .ok matchesPair := pairRef index token + | throw (.invalidTupleDefinition slice.assembly.identity) + if matchesPair then used := true; break + if used then break + if used then break + unless used do return false + let prepared ← prepare images + resolvePlatformType prepared "ValueTuple`2" .invalidTupleDefinition pairFields runtimeScope + (prepared.length + 1) + +/-- The selected pair's defining image declares both instance fields in the expected order. -/ +def TupleFieldsBound (images : List PinnedImage) (evidence : List MethodEvidence) + (layouts : List RawLayout) : Prop := + (checkTupleFields images evidence layouts).toOption = some true + +instance (images : List PinnedImage) (evidence : List MethodEvidence) (layouts : List RawLayout) : + Decidable (TupleFieldsBound images evidence layouts) := inferInstanceAs (Decidable (_ = _)) + +/-- Any supplied part of the selected forwarding chain agrees with the extraction. -/ +def SuppliedTupleFieldsValid (images : List PinnedImage) (evidence : List MethodEvidence) + (layouts : List RawLayout) : Prop := + ok (checkTupleFields images evidence layouts) = true + +instance (images : List PinnedImage) (evidence : List MethodEvidence) (layouts : List RawLayout) : + Decidable (SuppliedTupleFieldsValid images evidence layouts) := + inferInstanceAs (Decidable (_ = true)) + +end StrictBinding diff --git a/StrictBinding/TypeDefs.lean b/StrictBinding/TypeDefs.lean new file mode 100644 index 000000000..a4016c3b6 --- /dev/null +++ b/StrictBinding/TypeDefs.lean @@ -0,0 +1,575 @@ +import StrictBinding.Sorted + +/-! +# Type definitions at stated rows + +A claim names a TypeDef row of a pinned image and states what that row defines: its namespace +and name, the type that encloses it, its kind, the Field rows it owns and the type it extends. +The claim also states the NestedClass rows that place the type and the row its base type is, +so `typeDefBound` reads a fixed number of cells and `#Strings` entries for each claim, whatever +the size of the tables. `TypeDefAt` states the same parts as reads of the image, and +`typeDefAt_of_typeDefBound` proves that a successful check means them; `TypeDefsBound` checks a +list of claims against one preparation of the pinned images. That a top-level type has no +NestedClass row, and a nested type exactly one, follows from the stated rows together with +`KeysSorted` for the NestedClass table, a separate fact over the whole table +(`not_nested_of_topLevel`, `only_row_of_nested`). +-/ + +namespace StrictBinding + +open CIL.Safety (except_bind_ok) + +/-! ## Claims -/ + +/-- Where a claim places a type in the NestedClass table, with the rows that show it. -/ +inductive Nesting where + /-- At the top level: NestedClass rows before `next` name lower TypeDef rows and rows from + `next` on name higher ones, so with the table sorted no row names the type. `next` is one + past the table when every row names a lower TypeDef row. -/ + | topLevel (next : Nat) + /-- Nested in TypeDef row `enclosing`, as NestedClass row `nestedRow` states; the rows beside + it name lower and higher TypeDef rows, so with the table sorted it is the type's only row. -/ + | nested (nestedRow enclosing : Nat) + deriving DecidableEq, Repr + +/-- What a type definition is, as the flags and base type of its row decide (II.23.1.15). -/ +inductive TypeKind where + /-- The row's flags set the interface semantics bit. -/ + | interface + /-- The row extends `System.Enum`. -/ + | enum + /-- The row extends `System.ValueType` and is not `System.Enum` itself. -/ + | valueType + /-- Any other row: a class, as `System.Object` and `System.Enum` are. -/ + | classType + deriving DecidableEq, Repr + +/-- The type a TypeDef row's `Extends` column names. -/ +inductive BaseType where + /-- No type: the column's row part is zero, as for an interface or `System.Object`. -/ + | none + /-- The TypeDef or TypeRef row of `token`, which has this namespace and name. -/ + | named (token : Nat) (typeNamespace typeName : List Nat) + /-- The TypeSpec row of `token`: an instantiated generic type. -/ + | spec (token : Nat) + deriving DecidableEq, Repr + +/-- A claim that TypeDef row `row` of the image pinned under `identity` defines a type with + these parts. A name is the bytes of its `#Strings` entry without the terminator. -/ +structure TypeDefClaim where + identity : String + row : Nat + typeNamespace : List Nat + typeName : List Nat + nesting : Nesting + kind : TypeKind + /-- The first Field row the type owns and how many rows it owns. -/ + fields : Nat × Nat + base : BaseType + deriving DecidableEq, Repr + +/-- The table a token names a row of: its top byte. -/ +def tableOf (token : Nat) : Nat := token / 2 ^ 24 + +/-- The token of the claimed TypeDef row, when its row number fits in 24 bits. -/ +def TypeDefClaim.token (claim : TypeDefClaim) : Except AuditError Nat := + CIL.Extraction.checkedTokenOf CIL.Extraction.Table.typeDef claim.row .rowOverflow + +/-! ## Columns and flags -/ + +/-- The flags column of a TypeDef row (II.22.37). -/ +def typeDefFlagsColumn : Nat := 0 + +/-- The name column of a TypeDef or TypeRef row (II.22.37, II.22.38). -/ +def typeNameColumn : Nat := 1 + +/-- The namespace column of a TypeDef or TypeRef row. -/ +def typeNamespaceColumn : Nat := 2 + +/-- The `Extends` column of a TypeDef row. -/ +def typeDefExtendsColumn : Nat := 3 + +/-- The field list column of a TypeDef row: the first Field row the type owns. -/ +def typeDefFieldListColumn : Nat := 4 + +/-- The nested type column of a NestedClass row (II.22.32). -/ +def nestedTypeColumn : Nat := 0 + +/-- The enclosing type column of a NestedClass row. -/ +def enclosingTypeColumn : Nat := 1 + +/-- The visibility of TypeDef flags: the three bits of `VisibilityMask` (II.23.1.15). -/ +def visibilityOf (flags : Nat) : Nat := flags % 8 + +/-- The lowest visibility of a nested type. `NotPublic` (0) and `Public` (1) are the + visibilities of a type at the top level, and II.22.37 says the visibility decides whether a + type is nested; the NestedClass rows a claim states show that the table agrees. -/ +def nestedVisibility : Nat := 2 + +/-- The interface semantics bit of TypeDef flags. -/ +def interfaceFlag : Nat := 0x20 + +/-- The value a cell of this coded index column holds to name the row of `token`: the row above + the tag of the token's table (II.24.2.6). None when the column cannot name that table or is + not a coded index. -/ +def Column.valueOf (column : Column) (token : Nat) : Option Nat := + match column with + | .coded bits tables => + (tables.findIdx? (· == tableOf token)).map fun tag => token % 2 ^ 24 * 2 ^ bits + tag + | _ => Option.none + +/-- The namespace `System`. -/ +def systemNamespace : List Nat := asciiOf "System" + +/-- The name `Enum`. -/ +def enumName : List Nat := asciiOf "Enum" + +/-- The name `ValueType`. -/ +def valueTypeName : List Nat := asciiOf "ValueType" + +/-- Whether the Assembly or AssemblyRef row names a neutral core library with its publisher's + token. Both tables have the same identity columns after the Assembly's hash-algorithm column. -/ +def coreAssemblyAt (image : PreparedImage) (row first : Nat) : Except AuditError Bool := do + let cell := image.layout.cell image.view row + let name ← image.layout.stringAt image.view (← cell (first + 6)) + let some name := CIL.MetadataName.ofBytes? name | return false + let some publisher := CIL.Extraction.coreLibraryKey? name | return false + if !(← image.layout.stringAt image.view (← cell (first + 7))).isEmpty then return false + let flags ← cell (first + 4) + let key ← image.layout.blobAt image.view (← cell (first + 5)) + return (← rowKeyToken row flags key) == some publisher + +/-- A local base uses the defining image's Assembly row. A referenced base must point directly + to an AssemblyRef through its ResolutionScope column (II.24.2.6). -/ +private def coreBaseScope (image : PreparedImage) (token : Nat) : Except AuditError Bool := do + if tableOf token == CIL.Extraction.Table.typeDef then + return ← coreAssemblyAt image (tokenOf 0x20 1) 1 + let scope ← image.layout.cell image.view token 0 + if scope % 4 != 2 then return false + let row ← CIL.Extraction.checkedTokenOf CIL.Extraction.Table.assemblyRef (scope / 4) .rowOverflow + coreAssemblyAt image row 0 + +/-- The kind determined by the flags and the qualified base type, including its assembly. -/ +def kindOf (image : PreparedImage) (flags : Nat) (typeNamespace typeName : List Nat) + (base : BaseType) : Except AuditError TypeKind := do + if flags / interfaceFlag % 2 == 1 then return .interface + let .named token baseNamespace baseName := base | return .classType + if baseNamespace != systemNamespace || (baseName != enumName && baseName != valueTypeName) then + return .classType + if !(← coreBaseScope image token) then return .classType + if baseName == enumName then return .enum + if typeNamespace == systemNamespace && typeName == enumName then + if ← coreAssemblyAt image (tokenOf 0x20 1) 1 then return .classType + return .valueType + +/-! ## Propositions -/ + +/-- The number of NestedClass rows of the image. -/ +def nestedCount (image : PreparedImage) : Nat := + image.layout.rowCount CIL.Extraction.Table.nestedClass + +/-- The TypeDef row that NestedClass row `nestedRow` names as nested. -/ +def nestedTypeOf (image : PreparedImage) (nestedRow : Nat) : Except AuditError Nat := + image.layout.cellOfRow image.view CIL.Extraction.Table.nestedClass nestedRow nestedTypeColumn + +/-- The TypeDef row that NestedClass row `nestedRow` names as enclosing. -/ +def enclosingOf (image : PreparedImage) (nestedRow : Nat) : Except AuditError Nat := + image.layout.cellOfRow image.view CIL.Extraction.Table.nestedClass nestedRow enclosingTypeColumn + +/-- NestedClass row `nestedRow` names a TypeDef row below `row`, or is row zero, before the + table. -/ +def NamesBelow (image : PreparedImage) (nestedRow row : Nat) : Prop := + nestedRow = 0 ∨ ∃ value, nestedTypeOf image nestedRow = .ok value ∧ value < row + +/-- NestedClass row `nestedRow` names a TypeDef row above `row`, or is past the table. -/ +def NamesAbove (image : PreparedImage) (nestedRow row : Nat) : Prop := + nestedCount image < nestedRow ∨ ∃ value, nestedTypeOf image nestedRow = .ok value ∧ row < value + +/-- Column `column` of the row of `token` names the terminated `#Strings` entry `bytes`. -/ +def NameAt (image : PreparedImage) (token column : Nat) (bytes : List Nat) : Prop := + ∃ key, image.layout.cell image.view token column = .ok key ∧ StringAt image key (bytes ++ [0]) + +/-- One past the last Field row TypeDef row `row` owns: the next row's field list, or one past + the Field table for the last row (II.22.37). -/ +def fieldsEnd (image : PreparedImage) (row : Nat) : Except AuditError Nat := + if row < image.layout.rowCount CIL.Extraction.Table.typeDef then + image.layout.cellOfRow image.view CIL.Extraction.Table.typeDef (row + 1) typeDefFieldListColumn + else pure (image.layout.rowCount CIL.Extraction.Table.field + 1) + +/-- A row at the top level has a top-level visibility and the stated NestedClass rows bracket it; + a nested row has a nested visibility, the stated NestedClass row pairs it with the stated + enclosing row, and the rows beside that row name other types. The enclosing TypeDef is a + nonzero row strictly before the nested type (II.22). -/ +def NestingAt (image : PreparedImage) (row flags : Nat) : Nesting → Prop + | .topLevel next => + visibilityOf flags < nestedVisibility ∧ 1 ≤ next ∧ next ≤ nestedCount image + 1 ∧ + NamesBelow image (next - 1) row ∧ NamesAbove image next row + | .nested nestedRow enclosing => + nestedVisibility ≤ visibilityOf flags ∧ 1 ≤ nestedRow ∧ nestedRow ≤ nestedCount image ∧ + nestedTypeOf image nestedRow = .ok row ∧ enclosingOf image nestedRow = .ok enclosing ∧ + (1 ≤ enclosing ∧ enclosing < row) ∧ + NamesBelow image (nestedRow - 1) row ∧ NamesAbove image (nestedRow + 1) row + +/-- The `Extends` value `baseValue` names the stated base type, with its namespace and name. -/ +def BaseAt (image : PreparedImage) (baseValue : Nat) : BaseType → Prop + | .none => baseValue / 2 ^ 2 = 0 + | .named token baseNamespace baseName => + (tableOf token = CIL.Extraction.Table.typeDef ∨ + tableOf token = CIL.Extraction.Table.typeRef) ∧ + Column.typeDefOrRef.valueOf token = some baseValue ∧ + NameAt image token typeNameColumn baseName ∧ + NameAt image token typeNamespaceColumn baseNamespace + | .spec token => + tableOf token = CIL.Extraction.Table.typeSpec ∧ + Column.typeDefOrRef.valueOf token = some baseValue ∧ + ∃ position, image.layout.rowAt token = .ok position + +/-- The claimed TypeDef row of the image defines the claimed type: its names, nesting, Field + rows and base type are the row's, and its kind is the one the row's flags and base type + give. -/ +def TypeDefAt (image : PreparedImage) (claim : TypeDefClaim) : Prop := + ∃ token flags baseValue, claim.token = .ok token ∧ + image.layout.cell image.view token typeDefFlagsColumn = .ok flags ∧ + NameAt image token typeNameColumn claim.typeName ∧ + NameAt image token typeNamespaceColumn claim.typeNamespace ∧ + NestingAt image claim.row flags claim.nesting ∧ + image.layout.cell image.view token typeDefFieldListColumn = .ok claim.fields.1 ∧ + fieldsEnd image claim.row = .ok (claim.fields.1 + claim.fields.2) ∧ + 1 ≤ claim.fields.1 ∧ claim.fields.1 + claim.fields.2 ≤ + image.layout.rowCount CIL.Extraction.Table.field + 1 ∧ + image.layout.cell image.view token typeDefExtendsColumn = .ok baseValue ∧ + BaseAt image baseValue claim.base ∧ + kindOf image flags claim.typeNamespace claim.typeName claim.base = .ok claim.kind + +/-! ## Checks -/ + +/-- Succeeds when `holds`, and otherwise fails naming the row and the part that differs. -/ +def requirePart (row : Nat) (part : TypeDefPart) (holds : Bool) : Except AuditError Unit := + if holds then pure () else throw (.typeDefMismatch row part) + +/-- Column `column` of the row of `token` names the terminated entry `bytes`; a difference + fails as `part` of claimed row `row`. -/ +def nameBound (image : PreparedImage) (token column : Nat) (bytes : List Nat) (row : Nat) + (part : TypeDefPart) : Except AuditError Unit := do + let key ← image.layout.cell image.view token column + requirePart row part (← stringFound image key (bytes ++ [0])) + +/-- Whether NestedClass row `nestedRow` names a TypeDef row below `row` or is row zero. -/ +def namesBelow (image : PreparedImage) (nestedRow row : Nat) : Except AuditError Bool := + if nestedRow = 0 then pure true else do return decide ((← nestedTypeOf image nestedRow) < row) + +/-- Whether NestedClass row `nestedRow` names a TypeDef row above `row` or is past the table. -/ +def namesAbove (image : PreparedImage) (nestedRow row : Nat) : Except AuditError Bool := + if nestedCount image < nestedRow then pure true + else do return decide (row < (← nestedTypeOf image nestedRow)) + +/-- The row's visibility and the stated NestedClass rows agree with the claimed nesting. The + check reads at most three NestedClass rows. -/ +def nestingBound (image : PreparedImage) (row flags : Nat) : Nesting → Except AuditError Unit + | .topLevel next => do + requirePart row .nesting (decide (visibilityOf flags < nestedVisibility ∧ 1 ≤ next ∧ + next ≤ nestedCount image + 1)) + requirePart row .nesting (← namesBelow image (next - 1) row) + requirePart row .nesting (← namesAbove image next row) + | .nested nestedRow enclosing => do + requirePart row .nesting (decide (nestedVisibility ≤ visibilityOf flags ∧ 1 ≤ nestedRow ∧ + nestedRow ≤ nestedCount image)) + let nested ← nestedTypeOf image nestedRow + let outer ← enclosingOf image nestedRow + requirePart row .nesting + (nested == row && outer == enclosing && decide (1 ≤ enclosing ∧ enclosing < row)) + requirePart row .nesting (← namesBelow image (nestedRow - 1) row) + requirePart row .nesting (← namesAbove image (nestedRow + 1) row) + +/-- The `Extends` value names the claimed base type and that row has the claimed names. -/ +def baseBound (image : PreparedImage) (row baseValue : Nat) : + BaseType → Except AuditError Unit + | .none => requirePart row .base (baseValue / 2 ^ 2 == 0) + | .named token baseNamespace baseName => do + requirePart row .base + ((tableOf token == CIL.Extraction.Table.typeDef || + tableOf token == CIL.Extraction.Table.typeRef) && + Column.typeDefOrRef.valueOf token == some baseValue) + nameBound image token typeNameColumn baseName row .base + nameBound image token typeNamespaceColumn baseNamespace row .base + | .spec token => do + requirePart row .base + (tableOf token == CIL.Extraction.Table.typeSpec && + Column.typeDefOrRef.valueOf token == some baseValue) + let _ ← image.layout.rowAt token + pure () + +/-- The claimed TypeDef row defines the claimed type. The check reads the row's five columns, + one NestedClass row, the next TypeDef row's field list, the base row's two names and the + four entries the names select, so its cost does not grow with the tables. -/ +def typeDefBound (image : PreparedImage) (claim : TypeDefClaim) : Except AuditError Unit := do + let token ← claim.token + let flags ← image.layout.cell image.view token typeDefFlagsColumn + nameBound image token typeNameColumn claim.typeName claim.row .typeName + nameBound image token typeNamespaceColumn claim.typeNamespace claim.row .typeNamespace + nestingBound image claim.row flags claim.nesting + let first ← image.layout.cell image.view token typeDefFieldListColumn + let next ← fieldsEnd image claim.row + requirePart claim.row .fields + (first == claim.fields.1 && next == claim.fields.1 + claim.fields.2 && + decide (1 ≤ first ∧ next ≤ image.layout.rowCount CIL.Extraction.Table.field + 1)) + let baseValue ← image.layout.cell image.view token typeDefExtendsColumn + baseBound image claim.row baseValue claim.base + requirePart claim.row .kind + ((← kindOf image flags claim.typeNamespace claim.typeName claim.base) == claim.kind) + +/-- The claim holds in the image pinned under the claim's identity. -/ +def typeDefClaimBound (prepared : List PreparedImage) (claim : TypeDefClaim) : + Except AuditError Unit := do + typeDefBound (← located prepared claim.identity) claim + +/-- Every claim holds in the image its identity names, against one preparation of the images. -/ +def TypeDefsBound (images : List PinnedImage) (claims : List TypeDefClaim) : Prop := + ClaimsHold typeDefClaimBound images claims + +instance (images : List PinnedImage) (claims : List TypeDefClaim) : + Decidable (TypeDefsBound images claims) := + inferInstanceAs (Decidable (ClaimsHold _ _ _)) + +/-! ## Soundness -/ + +/-- A part check that succeeds held. -/ +theorem requirePart_ok {row : Nat} {part : TypeDefPart} {holds : Bool} + (checked : requirePart row part holds = .ok ()) : holds = true := by + unfold requirePart at checked + split at checked + · assumption + · exact absurd checked throw_ne_ok + +#print axioms requirePart_ok + +/-- A name check that succeeds found the entry the column names. -/ +theorem nameAt_of_nameBound {image : PreparedImage} (bounded : image.view.Bounded) + {token column row : Nat} {bytes : List Nat} {part : TypeDefPart} + (checked : nameBound image token column bytes row part = .ok ()) : + NameAt image token column bytes := by + unfold nameBound at checked + obtain ⟨key, readKey, checked⟩ := except_bind_ok checked + obtain ⟨found, readFound, checked⟩ := except_bind_ok checked + have holds := requirePart_ok checked + subst holds + exact ⟨key, readKey, stringAt_of_stringFound bounded readFound⟩ + +#print axioms nameAt_of_nameBound + +/-- A lower-row check that succeeds means the row names a lower TypeDef row or is row zero. -/ +theorem namesBelow_ok {image : PreparedImage} {nestedRow row : Nat} + (checked : namesBelow image nestedRow row = .ok true) : NamesBelow image nestedRow row := by + unfold namesBelow at checked + split at checked + · exact Or.inl (by assumption) + · obtain ⟨value, readValue, checked⟩ := except_bind_ok checked + simp only [pure, Except.pure, Except.ok.injEq, decide_eq_true_eq] at checked + exact Or.inr ⟨value, readValue, checked⟩ + +#print axioms namesBelow_ok + +/-- A higher-row check that succeeds means the row names a higher TypeDef row or is past the + table. -/ +theorem namesAbove_ok {image : PreparedImage} {nestedRow row : Nat} + (checked : namesAbove image nestedRow row = .ok true) : NamesAbove image nestedRow row := by + unfold namesAbove at checked + split at checked + · exact Or.inl (by assumption) + · obtain ⟨value, readValue, checked⟩ := except_bind_ok checked + simp only [pure, Except.pure, Except.ok.injEq, decide_eq_true_eq] at checked + exact Or.inr ⟨value, readValue, checked⟩ + +#print axioms namesAbove_ok + +/-- A nesting check that succeeds means the claimed nesting. -/ +theorem nestingAt_of_nestingBound {image : PreparedImage} {row flags : Nat} {nesting : Nesting} + (checked : nestingBound image row flags nesting = .ok ()) : + NestingAt image row flags nesting := by + cases nesting with + | topLevel next => + unfold nestingBound at checked + obtain ⟨visible, visibleOk, checked⟩ := except_bind_ok checked + cases visible + obtain ⟨below, belowOk, checked⟩ := except_bind_ok checked + obtain ⟨placed, placedOk, checked⟩ := except_bind_ok checked + cases placed + obtain ⟨above, aboveOk, aboveHolds⟩ := except_bind_ok checked + have bounds := of_decide_eq_true (requirePart_ok visibleOk) + have belowTrue := requirePart_ok placedOk + have aboveTrue := requirePart_ok aboveHolds + subst belowTrue + subst aboveTrue + exact ⟨bounds.1, bounds.2.1, bounds.2.2, namesBelow_ok belowOk, namesAbove_ok aboveOk⟩ + | nested nestedRow enclosing => + unfold nestingBound at checked + obtain ⟨visible, visibleOk, checked⟩ := except_bind_ok checked + cases visible + obtain ⟨nested, readNested, checked⟩ := except_bind_ok checked + obtain ⟨outer, readOuter, checked⟩ := except_bind_ok checked + obtain ⟨paired, pairedOk, checked⟩ := except_bind_ok checked + cases paired + obtain ⟨below, belowOk, checked⟩ := except_bind_ok checked + obtain ⟨placed, placedOk, checked⟩ := except_bind_ok checked + cases placed + obtain ⟨above, aboveOk, aboveHolds⟩ := except_bind_ok checked + have bounds := of_decide_eq_true (requirePart_ok visibleOk) + have same := requirePart_ok pairedOk + simp only [Bool.and_eq_true, beq_iff_eq, decide_eq_true_eq] at same + obtain ⟨⟨rfl, rfl⟩, precedes⟩ := same + have belowTrue := requirePart_ok placedOk + have aboveTrue := requirePart_ok aboveHolds + subst belowTrue + subst aboveTrue + exact ⟨bounds.1, bounds.2.1, bounds.2.2, readNested, readOuter, precedes, + namesBelow_ok belowOk, namesAbove_ok aboveOk⟩ + +#print axioms nestingAt_of_nestingBound + +/-- The key II.22 requires the NestedClass table sorted by: its nested type column. -/ +def nestedClassKey : List Nat := [nestedTypeColumn] + +/-- In a sorted NestedClass table, a row names a TypeDef row at most the one a later row names. -/ +theorem nestedType_le {image : PreparedImage} + (sorted : KeysSorted image CIL.Extraction.Table.nestedClass nestedClassKey) + {lower upper first last : Nat} (low : 1 ≤ lower) (ordered : lower ≤ upper) + (inside : upper ≤ nestedCount image) (readFirst : nestedTypeOf image lower = .ok first) + (readLast : nestedTypeOf image upper = .ok last) : first ≤ last := + value_le_of_keysSorted sorted low ordered inside readFirst readLast + +#print axioms nestedType_le + +/-- With the NestedClass table sorted, no NestedClass row names a type claimed at the top level. -/ +theorem not_nested_of_topLevel {image : PreparedImage} {row flags next : Nat} + (sorted : KeysSorted image CIL.Extraction.Table.nestedClass nestedClassKey) + (placed : NestingAt image row flags (.topLevel next)) : + ∀ nestedRow, 1 ≤ nestedRow → nestedRow ≤ nestedCount image → + nestedTypeOf image nestedRow ≠ .ok row := by + intro nestedRow low high read + obtain ⟨_, first, last, below, above⟩ := placed + by_cases before : nestedRow < next + · rcases below with zero | ⟨bound, readBound, less⟩ + · omega + · have := nestedType_le sorted low (show nestedRow ≤ next - 1 by omega) (by omega) read + readBound + omega + · rcases above with past | ⟨bound, readBound, greater⟩ + · omega + · have := nestedType_le sorted first (show next ≤ nestedRow by omega) high readBound read + omega + +#print axioms not_nested_of_topLevel + +/-- With the NestedClass table sorted, the row a nested claim states is the only NestedClass row + that names the type. -/ +theorem only_row_of_nested {image : PreparedImage} {row flags nestedRow enclosing : Nat} + (sorted : KeysSorted image CIL.Extraction.Table.nestedClass nestedClassKey) + (placed : NestingAt image row flags (.nested nestedRow enclosing)) : + ∀ other, 1 ≤ other → other ≤ nestedCount image → nestedTypeOf image other = .ok row → + other = nestedRow := by + intro other low high read + obtain ⟨_, first, last, _, _, _, below, above⟩ := placed + by_cases before : other < nestedRow + · rcases below with zero | ⟨bound, readBound, less⟩ + · omega + · have := nestedType_le sorted low (show other ≤ nestedRow - 1 by omega) (by omega) read + readBound + omega + · by_cases after : nestedRow < other + · rcases above with past | ⟨bound, readBound, greater⟩ + · omega + · have := nestedType_le sorted (show 1 ≤ nestedRow + 1 by omega) + (show nestedRow + 1 ≤ other by omega) high readBound read + omega + · omega + +#print axioms only_row_of_nested + +/-- A base check that succeeds means the claimed base type and its names. -/ +theorem baseAt_of_baseBound {image : PreparedImage} (bounded : image.view.Bounded) + {row baseValue : Nat} {base : BaseType} + (checked : baseBound image row baseValue base = .ok ()) : BaseAt image baseValue base := by + cases base with + | none => + have holds := requirePart_ok checked + show baseValue / 2 ^ 2 = 0 + simpa only [beq_iff_eq] using holds + | named token baseNamespace baseName => + unfold baseBound at checked + obtain ⟨named, namedOk, checked⟩ := except_bind_ok checked + cases named + obtain ⟨named, nameOk, namespaceOk⟩ := except_bind_ok checked + cases named + have holds := requirePart_ok namedOk + simp only [Bool.and_eq_true, Bool.or_eq_true, beq_iff_eq] at holds + exact ⟨holds.1, holds.2, nameAt_of_nameBound bounded nameOk, + nameAt_of_nameBound bounded namespaceOk⟩ + | spec token => + unfold baseBound at checked + obtain ⟨checkedBase, matched, checked⟩ := except_bind_ok checked + cases checkedBase + obtain ⟨position, found, _⟩ := except_bind_ok checked + have holds := requirePart_ok matched + simp only [Bool.and_eq_true, beq_iff_eq] at holds + exact ⟨holds.1, holds.2, position, found⟩ + +#print axioms baseAt_of_baseBound + +/-- A type definition check that succeeds means the claimed row defines the claimed type. -/ +theorem typeDefAt_of_typeDefBound {image : PreparedImage} (bounded : image.view.Bounded) + {claim : TypeDefClaim} (checked : typeDefBound image claim = .ok ()) : + TypeDefAt image claim := by + unfold typeDefBound at checked + obtain ⟨token, readToken, checked⟩ := except_bind_ok checked + obtain ⟨flags, readFlags, checked⟩ := except_bind_ok checked + obtain ⟨named, nameOk, checked⟩ := except_bind_ok checked + cases named + obtain ⟨named, namespaceOk, checked⟩ := except_bind_ok checked + cases named + obtain ⟨nested, nestingOk, checked⟩ := except_bind_ok checked + cases nested + obtain ⟨first, readFirst, checked⟩ := except_bind_ok checked + obtain ⟨next, readNext, checked⟩ := except_bind_ok checked + obtain ⟨owned, fieldsOk, checked⟩ := except_bind_ok checked + cases owned + obtain ⟨baseValue, readBase, checked⟩ := except_bind_ok checked + obtain ⟨based, baseOk, checked⟩ := except_bind_ok checked + cases based + obtain ⟨kind, readKind, kindOk⟩ := except_bind_ok checked + have fields := requirePart_ok fieldsOk + simp only [Bool.and_eq_true, beq_iff_eq, decide_eq_true_eq] at fields + obtain ⟨⟨rfl, rfl⟩, lower, upper⟩ := fields + have sameKind := requirePart_ok kindOk + simp only [beq_iff_eq] at sameKind + exact ⟨token, flags, baseValue, readToken, readFlags, nameAt_of_nameBound bounded nameOk, + nameAt_of_nameBound bounded namespaceOk, nestingAt_of_nestingBound nestingOk, + readFirst, readNext, lower, upper, readBase, baseAt_of_baseBound bounded baseOk, + sameKind ▸ readKind⟩ + +#print axioms typeDefAt_of_typeDefBound + +/-- Every claim of a successful check holds in the image its identity selects, from one + preparation of the pinned images. -/ +theorem typeDefAt_of_typeDefsBound {images : List PinnedImage} {claims : List TypeDefClaim} + (bound : TypeDefsBound images claims) : + ∃ prepared, prepare images = .ok prepared ∧ + ∀ claim ∈ claims, ∃ image, located prepared claim.identity = .ok image ∧ + TypeDefAt image claim := by + obtain ⟨prepared, decoded, checks⟩ := check_of_claimsHold bound + refine ⟨prepared, decoded, fun claim member => ?_⟩ + have one := checks claim member + unfold typeDefClaimBound at one + cases found : located prepared claim.identity with + | error error => simp [found, bind, Except.bind, ok] at one + | ok image => + refine ⟨image, rfl, ?_⟩ + have checked : typeDefBound image claim = .ok () := by + cases result : typeDefBound image claim with + | error error => simp [found, result, bind, Except.bind, ok] at one + | ok value => cases value; rfl + exact typeDefAt_of_typeDefBound (prepare_bounded decoded image (mem_of_located found)) + checked + +#print axioms typeDefAt_of_typeDefsBound + +end StrictBinding diff --git a/Tests/AesRoundContract.lean b/Tests/AesRoundContract.lean new file mode 100644 index 000000000..6af7a6858 --- /dev/null +++ b/Tests/AesRoundContract.lean @@ -0,0 +1,38 @@ +import CIL.Safety.MethodContract + +/-! The compiled portable AES round on its two 64-bit state halves and two round-key halves. -/ + +namespace Tests.AesRoundContract +open CIL.Safety + +/-- The round returns AESENC and writes only its fresh frame storage. -/ +def roundContract : MethodContract (CIL.W64 × CIL.W64 × CIL.W64 × CIL.W64) (BitVec 128) where + args := fun (a, b, k, l) => [.scalar (.i64 a), .scalar (.i64 b), .scalar (.i64 k), .scalar (.i64 l)] + results value := [.scalar (.v128 value)] + pre _ memory := memory.WellFormed + footprint _ := [] + post := fun (a, b, k, l) _ _ value => + value = CIL.Vector.aesEncryptRound (CIL.Vector.pack128 a b) (CIL.Vector.pack128 k l) + +/-- For a zero state, changing only the high key half flips one output bit. Loading the low + half twice returns 0x63 in every byte and violates the same contract on both profiles. -/ +theorem refuted_of_dropped_key {program : CIL.Program} {fuel : Nat} + (observed : (invoke program fuel 0 (roundContract.args (0, 0, 0, 1)) Memory.empty).map Prod.snd = + .ok [.scalar (.v128 0x63636363636363636363636363636363)]) : + roundContract.Refuted program 0 := by + cases ran : invoke program fuel 0 (roundContract.args (0, 0, 0, 1)) Memory.empty with + | error fault => rw [ran] at observed; cases observed + | ok result => + obtain ⟨after, values⟩ := result + rw [ran] at observed + have actual : values = [.scalar (.v128 0x63636363636363636363636363636363)] := Except.ok.inj observed + subst values + apply MethodContract.Refuted.of_return (c := roundContract) (input := (0, 0, 0, 1)) + Memory.empty_wellFormed ran + rintro ⟨_, value, encoded, rfl⟩ + revert encoded + decide +kernel + +#print axioms refuted_of_dropped_key + +end Tests.AesRoundContract diff --git a/Tests/AggregateArrayStorage.lean b/Tests/AggregateArrayStorage.lean index 155ae0706..0fdb6646c 100644 --- a/Tests/AggregateArrayStorage.lean +++ b/Tests/AggregateArrayStorage.lean @@ -19,7 +19,7 @@ private def memory : Memory := { /-- Aggregate stores preserve an uninitialized suffix and both neighbouring regions. -/ theorem masked_store : - (memoryInstruction (.arrayTransfer element true) + (typedElementInstruction (.value element) true [.scalar (.snapshot (.aggregate 16) mask), .scalar (.i32 1), .array view] memory).map (fun result => ((result.1.cells 0 0).bits, (result.1.cells 0 16).bits, (result.1.cells 0 24).initialized, (result.1.cells 0 32).bits)) = @@ -28,40 +28,41 @@ theorem masked_store : /-- Aggregate loads retain initialization masks instead of inventing readable bytes. -/ theorem masked_load : - (memoryInstruction (.arrayTransfer element false) [.scalar (.i32 0), .array view] + (typedElementInstruction (.value element) false [.scalar (.i32 0), .array view] { memory with cells := fun _ offset => if offset < 8 then ⟨7, true⟩ else ⟨0, false⟩ }).map Prod.snd = .ok [.scalar (.snapshot (.aggregate 16) mask)] := rfl #print axioms masked_load /-- A typed write still requires authority over the destination bytes. -/ theorem missing_authority : - (memoryInstruction (.arrayTransfer element true) + (typedElementInstruction (.value element) true [.scalar (.aggregate (List.replicate 16 0)), .scalar (.i32 0), .array view] { memory with views := [] }).isOk = false := rfl #print axioms missing_authority /-- Equal-sized vector and struct storage cannot be interchanged by an array instruction. -/ theorem wrong_identity : - memoryInstruction (.arrayTransfer (.vector "Vector128" 16) false) + typedElementInstruction (.value (.vector "Vector128" 16)) false [.scalar (.i32 0), .array view] memory = .error .invalidState := rfl #print axioms wrong_identity /-- Array indexing preserves named bounds exceptions. -/ theorem past_end : - memoryInstruction (.arrayTransfer element false) [.scalar (.i32 2), .array view] memory = - .error (.thrown "System.IndexOutOfRangeException") := rfl + typedElementInstruction (.value element) false [.scalar (.i32 2), .array view] memory = + .error (.thrown .indexOutOfRange) := rfl #print axioms past_end /-- Expired storage cannot yield a typed element, even when its bytes remain present. -/ theorem expired : - (memoryInstruction (.arrayTransfer element false) [.scalar (.i32 0), .array view] - { memory with allocations := fun _ => some ⟨.callerStack, ⟨48, 16, []⟩, false, [48]⟩ }).isOk = - false := rfl + (typedElementInstruction (.value element) false [.scalar (.i32 0), .array view] + { memory with + allocations := fun _ => some ⟨.callerStack, ⟨48, 16, []⟩, false, [48]⟩ }).isOk = false := + rfl #print axioms expired /-- A readonly element address retains its restriction on subsequent writes. -/ theorem readonly_address : - memoryInstruction (.arrayAddress element false) [.scalar (.i32 1), .array view] memory = + elementAddressInstruction (.value element) true [.scalar (.i32 1), .array view] memory = .ok (memory, [.readOnlyReference (.address ⟨0, 16⟩)]) := rfl #print axioms readonly_address @@ -74,7 +75,7 @@ theorem empty_slice : /-- Array metadata with a forged extent is rejected before dereferencing any element. -/ theorem invalid_extent : - (memoryInstruction (.arrayTransfer element false) + (typedElementInstruction (.value element) false [.scalar (.i32 0), .array { view with length := 4 }] memory).isOk = false := rfl #print axioms invalid_extent diff --git a/Tests/AggregateArrays.cs b/Tests/AggregateArrays.cs index 33f3bd7a1..5cdfb60f5 100644 --- a/Tests/AggregateArrays.cs +++ b/Tests/AggregateArrays.cs @@ -2,12 +2,18 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs using System.Text.Json; using Mono.Cecil; +using Mono.Cecil.Cil; var assembly = At("Examples/Assembly/bin/Examples.dll"); var root = At("artifacts/AggregateArrays"); @@ -19,11 +25,12 @@ CheckLeanFixture("Tests.AggregateArrayStorage"); ExtractBatch("extract", null, methods.Select(method => new GeneratedExtraction(assembly, method.FullName, Path.Combine(root, method.Name)))); +var pairIdentity = metadata.Assembly.Name.FullName + "::Examples.AggregateArrays/Pair"; foreach (var method in methods) { var name = method.Name; var vector = name.Contains("Vector", StringComparison.Ordinal); - var aggregateIdentity = metadata.Assembly.Name.FullName + "::Examples.AggregateArrays/Pair"; + var aggregateIdentity = pairIdentity; if (name == "ReadGeneric") { var owner = (GenericInstanceType)((ArrayType)method.Parameters[0].ParameterType).ElementType; @@ -49,7 +56,25 @@ "ConstructedLength" => ".scalar (.i32 1)", _ => store ? "" : $".scalar ({value})" }; - var source = File.ReadAllText(Path.Combine(root, name, "Extracted.lean")); + AddResult(name, File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), arguments, expected); +} +// A readonly prefix makes the struct element address read-only; the next loop forges its flag. +var readOnlyAssembly = Path.Combine(root, "ReadOnlyAddress.dll"); +using (var module = ModuleDefinition.ReadModule(assembly)) +{ + var address = module.GetType("Examples.AggregateArrays").Methods.Single(method => method.Name == "Address"); + var element = address.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Ldelema); + address.Body.GetILProcessor().InsertBefore(element, Instruction.Create(OpCodes.Readonly)); + module.Write(readOnlyAssembly); +} +Run(["dotnet", ExtractorPath, "extract", readOnlyAssembly, + "AggregateArrays::Address(Examples.AggregateArrays/Pair[],System.Int32)", Path.Combine(root, "ReadOnlyAddress")]); +AddResult("ReadOnlyAddress", File.ReadAllText(Path.Combine(root, "ReadOnlyAddress", "Extracted.lean")), + $".array ⟨.address ⟨0, 0⟩, 2, (.aggregate {JsonSerializer.Serialize(pairIdentity)} 16)⟩, .scalar (.i32 1)", + ".readOnlyReference (.address ⟨0, 16⟩)"); + +void AddResult(string name, string source, string arguments, string expected) +{ var proof = Path.Combine(root, name + ".lean"); File.WriteAllText(proof, source + $$""" @@ -72,24 +97,61 @@ """); evidence.Add(proof, "PASS aggregate array: " + name); } +var pairToken = ".type (.aggregate " + JsonSerializer.Serialize(pairIdentity) + " "; foreach (var (name, before, after, theorem) in new[] { - ("Read", " 16) false)", " 8) false)", "method0_layouts_validated"), - ("Read", " 16) false)", " 16) true)", "method0_scalars_validated"), - ("ReadVector", ".vector " + JsonSerializer.Serialize("System.Runtime.Intrinsics.Vector128`1") + - " 16) false)", ".vector " + JsonSerializer.Serialize("System.Runtime.Intrinsics.Vector128`1") + - " 16) false)", "method0_scalars_validated"), - ("ReadOnlyView", " 16) false .whole)", " 16) true .whole)", "method0_scalars_validated"), - ("View", " 16) true .whole)", " 8) true .whole)", "method0_layouts_validated") + // A typed element access is decoded from its body, so a forgery names another element at its raw token, which + // the certified stack states bind; the element's width is the one its checked layout gives. + ("Read", "/Pair\" 0) }", "/Other\" 0) }", "method0_scalars_validated"), + // The raw token names the struct's checked layout by index, which the layout coverage binds. + ("Read", pairToken + "0)", pairToken + "1)", "method0_coverage_validated"), + ("ReadVector", "\"System.UInt64\")) }", "\"System.Int64\")) }", "method0_scalars_validated"), + // The address flag is decoded from the raw prefix, so a writable forgery drops the prefix from the raw body, + // which its bytes bind. + ("ReadOnlyAddress", "opcode := 65054, operand := .none }", "opcode := 0, operand := .none }", + "method0_body_validated") }) { var original = File.ReadAllText(Path.Combine(root, name, "Extracted.lean")); + if (name is "Read" or "ReadVector" or "ReadOnlyAddress" && + !original.Contains("cil_decoded_method method0 ", StringComparison.Ordinal)) + throw new Exception("Typed element access is not decoded from its body: " + name); var source = NarrowEvidence(original, theorem); if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing array mutation: " + name); - var path = Path.Combine(root, "Rejected" + name + theorem + ".lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS aggregate array evidence rejection: " + name, - theorem.EndsWith("layouts_validated", StringComparison.Ordinal) ? "MethodLayoutsValid" : "ScalarFlowValid"); + Reject(name, theorem, source.Replace(before, after, StringComparison.Ordinal)); +} +// The retained conversion must preserve both the writability and the width of the array element. +foreach (var name in new[] { "ReadOnlyView", "View" }) +{ + var element = name == "ReadOnlyView" + ? $".aggregate {JsonSerializer.Serialize(pairIdentity)} 0" + : ".atom Extraction.runtimeScope \"System.UInt64\""; + var change = $$""" + fun instruction => match instruction with + | .call target prefixes => + let element : Extraction.TypeExpr := {{element}} + let owner := Extraction.TypeExpr.apply (.constructor Extraction.runtimeScope "System.Span\u00601") element + .call { target with owner, returnType := owner, parameters := [.array element] } prefixes + | other => other + """; + var original = File.ReadAllText(Path.Combine(root, name, "Extracted.lean")); + var forged = ForgeDecodedInstruction(original, 0, 1, change); + const string agreement = "method0_models_validated"; + Reject(name, agreement, NarrowEvidence(forged, agreement)); } evidence.Check(); rejected.Check(); + +void Reject(string name, string theorem, string source) +{ + var path = Path.Combine(root, "Rejected" + name + theorem + ".lean"); + File.WriteAllText(path, source); + rejected.Add(path, "PASS aggregate array evidence rejection: " + name, theorem switch + { + "method0_layouts_validated" => "MethodLayoutsValid", + "method0_coverage_validated" => "LayoutUsesComplete", + "method0_body_validated" => "the decoded method does not match its body bytes or metadata", + "method0_models_validated" => "RuntimeModelsAgree", + _ => "ScalarFlowValid" + }); +} diff --git a/Tests/AggregateFields.lean b/Tests/AggregateFields.lean index 2ecebb04a..ffd3e5cca 100644 --- a/Tests/AggregateFields.lean +++ b/Tests/AggregateFields.lean @@ -103,12 +103,14 @@ theorem store_then_load {entered memory : Memory} {boundary : Nat} {kinds slots} (writable : access memory home 12 1 true = .ok ()) (bits : BitVec 32) (word : Nat) (present : (packet.values bytes)[0]? = some word) : ∃ after, - (∀ body pc args frame rest, - step body (.setFieldAt 8 4) pc args frame + (∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value 8 4 signed → ∀ pc args frame rest, + step body (.stfld operand) pc args frame (.scalar (.i32 bits) :: .reference (.address home) :: rest) memory = .ok (.next (pc + 1) rest frame after)) ∧ - ∀ body pc args frame rest, - step body (.fieldAt 0 8 false) pc args frame (.reference (.address home) :: rest) after = + ∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .value 0 8 false → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) after = .ok (.next (pc + 1) (.scalar (.i64 (BitVec.ofNat 64 word)) :: rest) frame after) := by obtain ⟨after, stored, storedSnapshots, storedFormed, -, -, -, storedValues, -⟩ := snapshots.store_field homes formed slot specified writable packet_checked.1 packet_checked.2 @@ -137,10 +139,20 @@ theorem store_unavailable {entered memory : Memory} {boundary : Nat} {kinds slot #print axioms store_then_load #print axioms store_unavailable +private def packetTag : CIL.Extraction.FieldRef := + { owner := .aggregate "Packet" 0, index := 1, name := name!"Tag", + type := .atom CIL.Extraction.runtimeScope "System.Int32" } + /-- Zero a struct local, store its int field, then store and reload a word local. -/ @[cil_code] def structAndWord : CIL.Method := { - code := [.localAddr 0, .initAggregate 12, .localAddr 0, .arg 0, .setFieldAt 8 4, - .arg 1, .setLocal 1, .local 0, .ret] + environment := { + rawLayouts := [{ + identity := "Packet", explicit := false, packing := 4, declaredSize := 12 + fields := [⟨.unsigned64, none⟩, ⟨.signed32, none⟩] }] + layouts := #[⟨packet, 4⟩] } + code := [.ldloca 0, .initobj (.aggregate "Packet" 0), .ldloca 0, .ldarg 0, + .stfld packetTag, + .ldarg 1, .stloc 1, .ldloc 0, .ret] locals := [.aggregate zeros, .i64 0], localKinds := [.aggregate 12, .word64], returnsValue := true } @@ -163,9 +175,10 @@ theorem struct_beside_word {entered memory : Memory} {frame : Frame} {boundary : packet_checked.2 rfl rfl (fieldIndex := 1) rfl (.i32 bits) rfl have wordTagged := snapshot_access_after_write (snapshot_access_after_write wordReady zeroWrite) tagWrite - cil_checked_steps (snapshots := tagSnapshots) [structSlot, wordSlot, tagFormed, zeroStep, - tagStep, access_reference_valid _ _ _ _ _ structReady, zeroForm, wordTagged, zeros, - localAddress] + cil_checked_steps (snapshots := tagSnapshots) + [structSlot, access_reference_valid _ _ _ _ _ structReady, + zeroStep structAndWord (.aggregate "Packet" 0) rfl, wordSlot, tagFormed, + tagStep, zeroForm, wordTagged, zeros, localAddress] #print axioms struct_beside_word @@ -199,11 +212,13 @@ theorem signed_byte_load {entered memory : Memory} {boundary : Nat} {kinds slots (specified : known 0 = some (.aggregate bytes)) (writable : access memory home 40 1 true = .ok ()) : ∃ after, - (∀ body pc args frame rest, - step body (.fieldAt 0 1 true) pc args frame (.reference (.address home) :: rest) after = + (∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .value 0 1 true → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) after = .ok (.next (pc + 1) (.scalar (.i32 (-1)) :: rest) frame after)) ∧ - ∀ body pc args frame rest, - step body (.fieldAt 0 1 false) pc args frame (.reference (.address home) :: rest) after = + ∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .value 0 1 false → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) after = .ok (.next (pc + 1) (.scalar (.i32 255) :: rest) frame after) := by obtain ⟨after, -, stored, storedFormed, -, -, -, storedValues, -⟩ := snapshots.store_field homes formed slot specified writable mixed_checked.1 mixed_checked.2 @@ -238,22 +253,27 @@ theorem vector_and_nested_store_then_load {entered memory : Memory} {boundary : (inner : List (BitVec 8)) (innerSized : inner.length = 12) (sign : Nat) (present : (mixed.values bytes)[0]? = some sign) : ∃ vectored nested, - (∀ body pc args frame rest, - step body (.setFieldAt 8 16) pc args frame + (∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef) (signed : Bool), + body.fieldShape operand = .value 8 16 signed → ∀ pc args frame rest, + step body (.stfld operand) pc args frame (.scalar (.v128 bits) :: .reference (.address home) :: rest) memory = .ok (.next (pc + 1) rest frame vectored)) ∧ - (∀ body pc args frame rest, - step body (.setAggregateFieldAt 24 12) pc args frame + (∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .aggregate 24 12 → ∀ pc args frame rest, + step body (.stfld operand) pc args frame (.scalar (.aggregate inner) :: .reference (.address home) :: rest) vectored = .ok (.next (pc + 1) rest frame nested)) ∧ - (∀ body pc args frame rest, - step body (.aggregateFieldAt 24 12) pc args frame (.reference (.address home) :: rest) + (∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .aggregate 24 12 → + ∀ pc args frame rest, step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) nested = .ok (.next (pc + 1) (.scalar (.aggregate inner) :: rest) frame nested)) ∧ - (∀ body pc args frame rest, - step body (.fieldAt 8 16 false) pc args frame (.reference (.address home) :: rest) + (∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .value 8 16 false → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) nested = .ok (.next (pc + 1) (.scalar (.v128 bits) :: rest) frame nested)) ∧ - ∀ body pc args frame rest, - step body (.fieldAt 0 1 true) pc args frame (.reference (.address home) :: rest) + ∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .value 0 1 true → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) nested = .ok (.next (pc + 1) (.scalar (signedOfWidth 1 sign) :: rest) frame nested) := by obtain ⟨vectored, storeVector, vectorSnapshots, vectorFormed, -, vectorReady, -, vectorValues, -⟩ := @@ -309,8 +329,9 @@ theorem narrow_vector_load_unavailable {memory : Memory} {slots : List LocalSlot (slot : slots[0]? = some (.bytes (.aggregate 40) home)) (specified : known 0 = some (.aggregate bytes)) (vectorValue : Nat) (present : (mixed.values bytes)[2]? = some vectorValue) : - (∀ body pc args frame rest, - step body (.fieldAt 8 16 false) pc args frame (.reference (.address home) :: rest) memory = + (∀ (body : CIL.Method) (operand : CIL.Extraction.FieldRef), + body.fieldShape operand = .value 8 16 false → ∀ pc args frame rest, + step body (.ldfld operand) pc args frame (.reference (.address home) :: rest) memory = .ok (.next (pc + 1) (.scalar (numericOfWidth 16 vectorValue) :: rest) frame memory)) ∧ (⟨8, 8, false⟩ : FieldLayout).numberIn (vector.setIn zeros40 vectorOnes) ≠ vectorOnes := by fail_if_success diff --git a/Tests/AggregateMemorySteps.lean b/Tests/AggregateMemorySteps.lean index f5fcb1f0e..0d8d51132 100644 --- a/Tests/AggregateMemorySteps.lean +++ b/Tests/AggregateMemorySteps.lean @@ -15,46 +15,53 @@ open CIL.Safety /-- Initialized bytes load as an ordinary aggregate. -/ theorem initialized_load {memory : Memory} {source : Reference} {width : Nat} {bytes : List (BitVec 8)} (loaded : read memory source width 1 = .ok bytes) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate width) pc args frame (.reference (.address source) :: rest) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate width) + (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.aggregate bytes) :: rest) frame memory) := by - cil_checked_step [loaded] + cil_checked_step [shape, loaded] #print axioms initialized_load /-- Readonly loads preserve the same initialized byte evidence. -/ theorem readonly_load {memory : Memory} {source : Reference} {width : Nat} {bytes : List (BitVec 8)} (loaded : read memory source width 1 = .ok bytes) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate width) pc args frame (.readOnlyReference (.address source) :: rest) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate width) + (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.readOnlyReference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.aggregate bytes) :: rest) frame memory) := by - cil_checked_step [loaded] + cil_checked_step [shape, loaded] #print axioms readonly_load /-- A read stated at a local kind's width, as a remembered aggregate local gives it, supplies the load at the numeral width the opcode carries. -/ theorem kind_width_load {memory : Memory} {source : Reference} {bytes : List (BitVec 8)} (loaded : read memory source (localWidth (.aggregate 32)) 1 = .ok bytes) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate 32) pc args frame (.reference (.address source) :: rest) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate 32) + (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.aggregate bytes) :: rest) frame memory) := by - cil_checked_step [loaded] + cil_checked_step [shape, loaded] #print axioms kind_width_load /-- One contract step derives the aggregate load from a supplied read, as a checked step does, so a walk taken one step at a time crosses a load through a managed-reference address. -/ theorem contract_step_load {memory : Memory} {source : Reference} {bytes : List (BitVec 8)} (loaded : read memory source 32 1 = .ok bytes) - (body : CIL.Method) (program : CIL.Program) (method : Nat) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate 32) (program : CIL.Program) (method : Nat) (found : program[method]? = some body) - (instruction : body.code[0]? = some (.loadAggregate 32)) (frame : Frame) (rest : List Value) + (instruction : body.code[0]? = some (.ldobj type)) (frame : Frame) (rest : List Value) (continuation : WP (Execution (fun fuel => run program fuel method 1 [] frame (.scalar (.aggregate bytes) :: rest) memory)) (fun _ => True)) : WP (Execution (fun fuel => run program fuel method 0 [] frame (.reference (.address source) :: rest) memory)) (fun _ => True) := by cil_contract_prepare [] -- The read is no step fact until it is prepared, so the unprepared step stops here. - fail_if_success cil_contract_step_normalized [found, instruction, loaded] - cil_contract_step [found, instruction, loaded] + fail_if_success cil_contract_step_normalized [shape, found, instruction, loaded] + cil_contract_step [shape, found, instruction, loaded] exact WP.adequate.mp continuation #print axioms contract_step_load @@ -63,15 +70,16 @@ theorem contract_step_load {memory : Memory} {source : Reference} {bytes : List theorem contract_step_kind_width_load {memory : Memory} {source : Reference} {bytes : List (BitVec 8)} (loaded : read memory source (localWidth (.aggregate 32)) 1 = .ok bytes) - (body : CIL.Method) (program : CIL.Program) (method : Nat) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate 32) (program : CIL.Program) (method : Nat) (found : program[method]? = some body) - (instruction : body.code[0]? = some (.loadAggregate 32)) (frame : Frame) (rest : List Value) + (instruction : body.code[0]? = some (.ldobj type)) (frame : Frame) (rest : List Value) (continuation : WP (Execution (fun fuel => run program fuel method 1 [] frame (.scalar (.aggregate bytes) :: rest) memory)) (fun _ => True)) : WP (Execution (fun fuel => run program fuel method 0 [] frame (.readOnlyReference (.address source) :: rest) memory)) (fun _ => True) := by cil_contract_prepare [] - cil_contract_step [found, instruction, loaded] + cil_contract_step [shape, found, instruction, loaded] exact WP.adequate.mp continuation #print axioms contract_step_kind_width_load @@ -80,122 +88,139 @@ theorem contract_step_kind_width_load {memory : Memory} {source : Reference} theorem contract_walk_restated_kind_load {memory : Memory} {source : Reference} {bytes : List (BitVec 8)} (loaded : read memory source (localWidth (.aggregate 32)) 1 = .ok bytes) - (body : CIL.Method) (program : CIL.Program) (method : Nat) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate 32) (program : CIL.Program) (method : Nat) (found : program[method]? = some body) - (instruction : body.code[0]? = some (.loadAggregate 32)) (frame : Frame) (rest : List Value) + (instruction : body.code[0]? = some (.ldobj type)) (frame : Frame) (rest : List Value) (continuation : WP (Execution (fun fuel => run program fuel method 1 [] frame (.scalar (.aggregate bytes) :: rest) memory)) (fun _ => True)) : WP (Execution (fun fuel => run program fuel method 0 [] frame (.reference (.address source) :: rest) memory)) (fun _ => True) := by have restated := loaded clear loaded - cil_contract_steps [found, instruction, restated] until continuation + cil_contract_steps [shape, found, instruction, restated] until continuation #print axioms contract_walk_restated_kind_load /-- The same walk takes a restated read at the numeral width. -/ theorem contract_walk_restated_load {memory : Memory} {source : Reference} {bytes : List (BitVec 8)} (loaded : read memory source 32 1 = .ok bytes) - (body : CIL.Method) (program : CIL.Program) (method : Nat) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate 32) (program : CIL.Program) (method : Nat) (found : program[method]? = some body) - (instruction : body.code[0]? = some (.loadAggregate 32)) (frame : Frame) (rest : List Value) + (instruction : body.code[0]? = some (.ldobj type)) (frame : Frame) (rest : List Value) (continuation : WP (Execution (fun fuel => run program fuel method 1 [] frame (.scalar (.aggregate bytes) :: rest) memory)) (fun _ => True)) : WP (Execution (fun fuel => run program fuel method 0 [] frame (.reference (.address source) :: rest) memory)) (fun _ => True) := by have restated := loaded clear loaded - cil_contract_steps [found, instruction, restated] until continuation + cil_contract_steps [shape, found, instruction, restated] until continuation #print axioms contract_walk_restated_load /-- An unknown byte loads as a snapshot without becoming initialized. -/ theorem unknown_load {memory : Memory} {source : Reference} (loaded : readSnapshot memory source 1 1 = .ok [(0, false)]) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate 1) pc args frame (.reference (.address source) :: rest) memory = + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate 1) + (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.snapshot (.aggregate 1) [(0, false)]) :: rest) frame memory) := by - cil_checked_step [loaded] + cil_checked_step [shape, loaded] #print axioms unknown_load /-- Readonly aggregate loads also retain an unknown byte's initialization flag. -/ theorem readonly_unknown_load {memory : Memory} {source : Reference} (loaded : readSnapshot memory source 1 1 = .ok [(0, false)]) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate 1) pc args frame (.readOnlyReference (.address source) :: rest) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate 1) + (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.readOnlyReference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.snapshot (.aggregate 1) [(0, false)]) :: rest) frame memory) := by - cil_checked_step [loaded] + cil_checked_step [shape, loaded] #print axioms readonly_unknown_load /-- A symbolic snapshot preserves all its flags when at least one byte is unknown. -/ theorem symbolic_unknown_load {memory : Memory} {source : Reference} {width : Nat} {bytes : List (BitVec 8 × Bool)} (loaded : readSnapshot memory source width 1 = .ok bytes) (unknown : bytes.all Prod.snd = false) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate width) pc args frame (.reference (.address source) :: rest) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate width) + (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.snapshot (.aggregate width) bytes) :: rest) frame memory) := by - cil_checked_step [loaded, unknown] + cil_checked_step [shape, loaded, unknown] #print axioms symbolic_unknown_load /-- A store uses the checked byte write and does not need old destination contents. -/ theorem initialized_store {memory after : Memory} {destination : Reference} {bytes : List (BitVec 8)} (written : write memory destination bytes 1 = .ok after) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.storeAggregate bytes.length) pc args frame + (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) + {type : CIL.Extraction.TypeExpr} (shape : body.valueShape type = .aggregate bytes.length) : + step body (.stobj type) pc args frame (.scalar (.aggregate bytes) :: .reference (.address destination) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by - cil_checked_step [written] + cil_checked_step [shape, written] #print axioms initialized_store /-- Storing unknown bytes uses the snapshot write, which retains their initialization flags. -/ theorem unknown_store {memory after : Memory} {destination : Reference} (written : writeSnapshot memory destination [(0, false)] 1 = .ok after) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.storeAggregate 1) pc args frame + (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) + {type : CIL.Extraction.TypeExpr} (shape : body.valueShape type = .aggregate 1) : + step body (.stobj type) pc args frame (.scalar (.snapshot (.aggregate 1) [(0, false)]) :: .reference (.address destination) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by - cil_checked_step [written] + cil_checked_step [shape, written] #print axioms unknown_store /-- An unknown snapshot cannot be reported as an initialized aggregate. -/ theorem unknown_is_not_initialized {memory : Memory} {source : Reference} (loaded : readSnapshot memory source 1 1 = .ok [(0, false)]) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate 1) pc args frame (.reference (.address source) :: rest) memory ≠ + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate 1) + (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.reference (.address source) :: rest) memory ≠ .ok (.next (pc + 1) (.scalar (.aggregate [0]) :: rest) frame memory) := by - rw [unknown_load loaded] + rw [unknown_load loaded body shape] simp #print axioms unknown_is_not_initialized /-- A snapshot with the wrong shape width is rejected before its write operation. -/ theorem wrong_snapshot_width (memory : Memory) (destination : Reference) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.storeAggregate 1) pc args frame + (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) + {type : CIL.Extraction.TypeExpr} (shape : body.valueShape type = .aggregate 1) : + step body (.stobj type) pc args frame (.scalar (.snapshot (.aggregate 2) [(0, false)]) :: .reference (.address destination) :: rest) memory = .error .invalidState := by - cil_checked_step [] + cil_checked_step [shape] #print axioms wrong_snapshot_width /-- A null aggregate load preserves the requested width in its fault. -/ -theorem null_load_width (memory : Memory) (body : CIL.Method) (pc : Nat) (args : List Value) +theorem null_load_width (memory : Memory) (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate 8) + (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate 8) pc args frame (.reference .null :: rest) memory = + step body (.ldobj type) pc args frame (.reference .null :: rest) memory = .error (.memory .nullDereference .null 8) := by - cil_checked_step [] + cil_checked_step [shape] #print axioms null_load_width /-- Read authority alone cannot prove any particular loaded aggregate. -/ theorem access_does_not_supply_contents (memory : Memory) (source : Reference) (_readable : access memory source 1 1 false = .ok ()) - (_body : CIL.Method) (_pc : Nat) (_args : List Value) (_frame : Frame) (_rest : List Value) : + (_body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : _body.valueShape type = .aggregate 1) + (_pc : Nat) (_args : List Value) (_frame : Frame) (_rest : List Value) : True := by fail_if_success - have : step _body (.loadAggregate 1) _pc _args _frame + have : step _body (.ldobj type) _pc _args _frame (.reference (.address source) :: _rest) memory = .ok (.next (_pc + 1) (.scalar (.aggregate [0]) :: _rest) _frame memory) := by - cil_checked_step [_readable] + cil_checked_step [shape, _readable] trivial #print axioms access_does_not_supply_contents diff --git a/Tests/AggregateSetup.lean b/Tests/AggregateSetup.lean index c108b8c8b..7e0a3445c 100644 --- a/Tests/AggregateSetup.lean +++ b/Tests/AggregateSetup.lean @@ -7,7 +7,8 @@ namespace Tests.AggregateSetup open CIL CIL.Safety def constructor (size : Fin (2^64)) : CIL.Program := [ - { code := [.newAggregate 1 0 size, .ret], locals := [], returnsValue := true }, + { code := [.newobj (.arity 0), .ret], locals := [], returnsValue := true, + links := [(0, .aggregate 1 size)] }, { code := [.ret], locals := [], returnsValue := false }] /-- The existing generic constructor composition API consumes aggregate setup @@ -15,7 +16,8 @@ def constructor (size : Fin (2^64)) : CIL.Program := [ theorem composed_constructor (size : Fin (2^64)) (positive : 0 < size.val) (memory : CIL.Safety.Memory) (wf : memory.WellFormed) (frame : Frame) (child : ∀ temporary updated, - step ((constructor size)[0]'(by simp [constructor])) (.newAggregate 1 0 size) 0 [] frame [] memory = + step ((constructor size)[0]'(by simp [constructor])) (.newobj (.arity 0)) 0 [] frame [] + memory = .ok (.construct 1 [.reference (.address temporary)] [] { frame with owned := temporary.allocation :: frame.owned } temporary (.aggregate size) updated) → invoke (constructor size) 4 1 [.reference (.address temporary)] updated = .ok (updated, [])) @@ -32,8 +34,8 @@ theorem composed_constructor (size : Fin (2^64)) (positive : 0 < size.val) [.scalar (.aggregate (List.replicate size 0))]) ∧ MemoryBelow memory.nextIdentity memory updated := by obtain ⟨temporary, updated, stepped, loaded, _, preserved, _⟩ := - step_newAggregate (body := ((constructor size)[0]'(by simp [constructor]))) (pc := 0) (callee := 1) - size positive [] [] [] frame memory wf rfl + step_newobj_aggregate (body := ((constructor size)[0]'(by simp [constructor]))) (pc := 0) + (callee := 1) (constructor := .arity 0) size positive rfl [] [] [] frame memory wf rfl rfl refine ⟨temporary, updated, ?_, preserved⟩ exact run_construct_of_invoke (body := ((constructor size)[0]'(by simp [constructor]))) rfl rfl stepped (child temporary updated stepped) (load_local_aggregate_of_read loaded) @@ -80,9 +82,9 @@ theorem private_store_load {body : CIL.Method} {frame : Frame} (ready : access memory reference size 1 true = .ok ()) (fresh : watermark ≤ reference.allocation) : ∃ result, - step body (.setLocal index) 0 [] frame [.scalar (.aggregate bytes)] memory = + step body (.stloc index) 0 [] frame [.scalar (.aggregate bytes)] memory = .ok (.next 1 [] frame result) ∧ - step body (.local index) 1 [] frame [] result = + step body (.ldloc index) 1 [] frame [] result = .ok (.next 2 [.scalar (.aggregate bytes)] frame result) ∧ MemoryBelow watermark memory result := by obtain ⟨result, stored, _, loaded, _, _, preserved⟩ := diff --git a/Tests/AggregateSpanFill.cs b/Tests/AggregateSpanFill.cs index 0f6a2b533..c17f3585c 100644 --- a/Tests/AggregateSpanFill.cs +++ b/Tests/AggregateSpanFill.cs @@ -2,8 +2,14 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs +#:include BulkCallEvidence.cs #:include RejectedEvidence.cs using Mono.Cecil; @@ -66,7 +72,10 @@ string Run(string args, string memory = "memory") => var output = Path.Combine(root, name); Directory.CreateDirectory(output); var proof = Path.Combine(output, "Proof.lean"); - File.WriteAllText(proof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + """ + File.WriteAllText(proof, """ + import Extracted + import CIL.Safety.StepComposition + namespace ExampleProof /-- Successful observations identify the checked execution result. -/ private theorem result_of_toOption {error value : Type} {result : Except error value} {expected : value} (observed : result.toOption = some expected) : result = .ok expected := by @@ -84,22 +93,7 @@ string Run(string args, string memory = "memory") => " := by\n repeat' apply And.intro\n" + " all_goals first | decide +kernel | (apply result_of_toOption; decide +kernel)\n" + "#print axioms correctness\nend ExampleProof\n"); + File.AppendAllText(proof, BulkCallEvidence(0, name == "Aliased" ? 5 : 2, $".spanFill {width} false")); queue.Add(new(method.FullName, proof, output), () => Console.WriteLine("PASS aggregate fill: " + name)); } queue.Flush(); -var rejected = new RejectedEvidence(Path.Combine(root, "RejectedEvidence")); -foreach (var (name, before, after, theorem) in new[] -{ - ("Words", ".spanFill 32 false", ".spanFill 16 false", "method0_layouts_validated"), - ("Words", ".spanFill 32 false", ".spanFill 32 true", "method0_scalars_validated"), - ("Vector128", ".spanFill 16 false", ".spanFill 32 false", "method0_scalars_validated") -}) -{ - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), theorem); - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing fill mutation"); - var path = Path.Combine(root, "Rejected" + name + theorem + ".lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS aggregate fill evidence rejection: " + name, - theorem.EndsWith("layouts_validated", StringComparison.Ordinal) ? "MethodLayoutsValid" : "ScalarFlowValid"); -} -rejected.Check(); diff --git a/Tests/AggregateSpans.cs b/Tests/AggregateSpans.cs index a9d738c0a..2dc11e9ac 100644 --- a/Tests/AggregateSpans.cs +++ b/Tests/AggregateSpans.cs @@ -2,9 +2,16 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs +#:include BulkCallEvidence.cs #:include RejectedEvidence.cs +#:include MarshalModelEvidence.cs using Mono.Cecil; @@ -18,10 +25,10 @@ string Span(int width, bool writable = true, int offset = 0, int length = 2) => $".bufferSpan ⟨.address ⟨0, {offset}⟩, {length}, {width}, {writable.ToString().ToLowerInvariant()}⟩"; foreach (var method in metadata.GetType("Examples.AggregateSpans").Methods.Where(method => method.IsPublic)) foreach (var profile in method.Name.StartsWith("Layout", StringComparison.Ordinal) - ? new[] { "x64-sse2", "arm64-advsimd" } : ["scalar"]) + ? new[] { "x64-sse42", "arm64-advsimd" } : ["scalar"]) { var name = method.Name; - var layoutWidth = profile == "x64-sse2" ? 64 : 48; + var layoutWidth = profile == "x64-sse42" ? 64 : 48; var output = Path.Combine(root, profile == "scalar" ? name : name + "-" + profile); Directory.CreateDirectory(output); var arguments = name switch @@ -64,7 +71,15 @@ string Span(int width, bool writable = true, int offset = 0, int length = 2) => "(result.1.cells 0 96).bits]) = .ok [0, 63, 96]", _ => null }; - File.WriteAllText(proof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + """ + var contents = extra is null ? "" : $$""" + /-- Stores and overlapping copies preserve neighbouring bytes. -/ + theorem contents : {{extra}} := by rfl + #print axioms contents + """; + File.WriteAllText(proof, $$""" + import Extracted + import CIL.Safety.StepComposition + namespace ExampleProof /-- Existing initialized caller storage shared by overlapping aggregate views. -/ def memory : CIL.Safety.Memory := { CIL.Safety.Memory.empty with allocations := fun identity => if identity == 0 then @@ -73,31 +88,25 @@ string Span(int width, bool writable = true, int offset = 0, int length = 2) => views := [⟨0, 0, 128, true, true⟩] nextIdentity := 1 } /-- Compiled span operations retain aggregate and vector storage identities. -/ - """ + $"\ntheorem correctness : (CIL.Safety.invoke Extracted.program 64 0 [{arguments}] memory).map " + - $"Prod.snd = .ok [{expected}] := by rfl\n#print axioms correctness\n" + - (extra is null ? "" : "/-- Stores and overlapping copies preserve neighbouring bytes. -/\n" + - "theorem contents : " + extra + " := by rfl\n#print axioms contents\n") + - "end ExampleProof\n"); + theorem correctness : + (CIL.Safety.invoke Extracted.program 64 0 [{{arguments}}] memory).map Prod.snd = + .ok [{{expected}}] := by rfl + #print axioms correctness + {{contents}} + end ExampleProof + + """); + if (name == "Copy") File.AppendAllText(proof, BulkCallEvidence(0, 2, ".spanCopy 32 true")); + if (name == "Clear") File.AppendAllText(proof, BulkCallEvidence(0, 1, ".spanFill 32 true")); queue.Add(new(method.FullName, proof, output, Profile: profile), () => Console.WriteLine("PASS aggregate span: " + name + " (" + profile + ")")); } queue.Flush(); -var rejected = new RejectedEvidence(Path.Combine(root, "RejectedEvidence")); -foreach (var (name, before, after, theorem) in new[] -{ - ("ReadOnlyFactory", ".spanFromReference 32 false", ".spanFromReference 32 true", "method0_scalars_validated"), - ("Factory", ".spanFromReference 32 true", ".spanFromReference 16 true", "method0_layouts_validated"), - ("Bytes", ".spanReinterpret 32 1 true", ".spanReinterpret 16 1 true", "method0_layouts_validated"), - ("WordsView", ".spanReinterpret 1 32 false", ".spanReinterpret 1 16 false", "method0_layouts_validated"), - ("Copy", ".spanCopy 32 true", ".spanCopy 16 true", "method0_layouts_validated"), - ("VectorBytes", ".spanReinterpret 16 1 true", ".spanReinterpret 32 1 true", "method0_scalars_validated") -}) -{ - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), theorem); - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing span mutation"); - var path = Path.Combine(root, "Rejected" + name + ".lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS aggregate span evidence rejection: " + name, - theorem.EndsWith("layouts_validated", StringComparison.Ordinal) ? "MethodLayoutsValid" : "ScalarFlowValid"); -} -rejected.Check(); +CheckMarshalModels(root, +[ + ("ReadOnlyFactory", 2, ".spanFromReference 32 false", ".spanFromReference 2 false"), + ("Factory", 2, ".spanFromReference 32 true", ".spanFromReference 2 true"), + ("Bytes", 1, ".spanReinterpret 32 1 true", ".spanReinterpret 2 1 true"), + ("WordsView", 1, ".spanReinterpret 1 32 false", ".spanReinterpret 2 32 false"), + ("VectorBytes", 1, ".spanReinterpret 16 1 true", ".spanReinterpret 2 1 true") +]); diff --git a/Tests/AllocationExecutionRenaming.lean b/Tests/AllocationExecutionRenaming.lean index b6528116b..881e61ae4 100644 --- a/Tests/AllocationExecutionRenaming.lean +++ b/Tests/AllocationExecutionRenaming.lean @@ -1,5 +1,6 @@ import CIL.Safety.AllocationRefinement import Tests.AllocationRenaming +import Tests.Support /-! # Complete executions with private dead locals @@ -27,25 +28,41 @@ theorem initial_formed : initial.WellFormed where def indices : LocalIndexMap := .skip (.keep (.skip (.keep (.skip .nil)))) /-- The source retains one initialized word32 home and one initialized by its code. -/ -def sourceBody (code : List CIL.Op) : CIL.Method := { +def sourceBody (code : List CIL.Instruction) (links : List (Nat × CIL.Link)) : CIL.Method := { returnsValue := true locals := [.i32 7, .unmodeled] localKinds := [.word32, .word32] - code } + declared := { initLocals := false } + code, links } /-- Initialized private word64 homes precede, separate and follow the retained word32 homes. -/ -def targetBody (code : List CIL.Op) : CIL.Method := { +def targetBody (code : List CIL.Instruction) (links : List (Nat × CIL.Link)) : CIL.Method := { returnsValue := true locals := [.i64 17, .i32 7, .i64 23, .unmodeled, .i64 31] localKinds := [.word64, .word32, .word64, .word32, .word64] - code := code.map indices.operation } + declared := { initLocals := false } + code := code.map indices.operation, links } /-- Retained local operands, all method metadata and exact frame declarations match. -/ -theorem bodies_related (code : List CIL.Op) : - Nonempty (MethodRenaming (sourceBody code) (targetBody code)) := by - refine ⟨⟨indices, ⟨rfl, rfl, rfl⟩, - .skip _ _ (by simp [InitializerFits]) (.keep _ _ (.skip _ _ (by simp [InitializerFits]) - (.keep _ _ (.skip _ _ (by simp [InitializerFits]) .nil)))), ?_, ?_, rfl⟩⟩ +theorem bodies_related (code : List CIL.Instruction) (links : List (Nat × CIL.Link)) : + Nonempty (MethodRenaming (sourceBody code links) (targetBody code links)) := by + refine Nonempty.intro { + indices := indices + metadata := { + profile := rfl + returnsValue := rfl + staticSites := rfl + returnNarrow := rfl + arguments := fun _ => Exists.intro _ (And.intro rfl rfl) + declared := rfl + environment := rfl + links := rfl } + declarations := + .skip _ _ (by simp [InitializerFits]) (.keep _ _ (.skip _ _ (by simp [InitializerFits]) + (.keep _ _ (.skip _ _ (by simp [InitializerFits]) .nil)))) + sourceReady := ?_ + targetReady := ?_ + code := rfl } all_goals constructor <;> first | rfl | (intro kind member @@ -81,43 +98,48 @@ theorem invalid_dead_initializers_rejected : #print axioms invalid_dead_initializers_rejected /-- The child reads seven from its retained initializer after allocating its numeric homes. -/ -def childCode : List CIL.Op := [.local 0, .ret] +def childCode : List CIL.Instruction := [.ldloc 0, .ret] + +/-- The caller's call, at pc 2, enters the child, method 1. -/ +def callerLinks : List (Nat × CIL.Link) := [(2, .direct 1)] /-- The inserted nonzero initializer really writes the fresh private home during frame entry. -/ theorem private_initializer_readback : - (enterFrame (targetBody childCode) [] initial).map + (enterFrame (targetBody childCode []) [] initial).map (fun result => read result.2 ⟨1, 0⟩ 8 1) = .ok (.ok (numberBytes 17 8)) := by rfl #print axioms private_initializer_readback /-- A returned child value is stored before later dynamic allocation changes fresh identities. -/ -def allocationPrefix : List CIL.Op := - [.const32 9, .setLocal 0, .call 1 0, .setLocal 1, .const32 4, .stackAlloc false, .pop] +def allocationPrefix : List CIL.Instruction := + [.ldc_i4 9, .stloc 0, .call (Tests.callee 0), .stloc 1, .ldc_i4 4, .localloc, .pop] /-- The resumed caller reads both retained homes after its child and dynamic allocation. -/ -def returningCode : List CIL.Op := allocationPrefix ++ [.local 0, .local 1, .add, .ret] +def returningCode : List CIL.Instruction := allocationPrefix ++ [.ldloc 0, .ldloc 1, .add, .ret] /-- The exceptional fixture raises after both the nested call and dynamic allocation finish. -/ -def throwingCode : List CIL.Op := allocationPrefix ++ [.throw "AfterNestedAllocation"] +def throwingCode : List CIL.Instruction := allocationPrefix ++ [.throw .invalidOperation] /-- Both methods use the same declaration transformation and preserve their callable indices. -/ -def sourceProgram (code : List CIL.Op) : CIL.Program := [sourceBody code, sourceBody childCode] +def sourceProgram (code : List CIL.Instruction) : CIL.Program := + [sourceBody code callerLinks, sourceBody childCode []] /-- The actual program relocates every local operand in both caller and child. -/ -def targetProgram (code : List CIL.Op) : CIL.Program := [targetBody code, targetBody childCode] +def targetProgram (code : List CIL.Instruction) : CIL.Program := + [targetBody code callerLinks, targetBody childCode []] /-- The complete program satisfies the checked method correspondence, including its child. -/ -theorem programs_related (code : List CIL.Op) : +theorem programs_related (code : List CIL.Instruction) : ProgramRenaming (sourceProgram code) (targetProgram code) := by intro method body found cases method with | zero => cases found - exact ⟨_, rfl, bodies_related code⟩ + exact ⟨_, rfl, bodies_related code callerLinks⟩ | succ method => cases method with | zero => cases found - exact ⟨_, rfl, bodies_related childCode⟩ + exact ⟨_, rfl, bodies_related childCode []⟩ | succ method => cases found #print axioms programs_related @@ -148,7 +170,7 @@ theorem source_confined : MemoryBelow 1 initial sourceFinal := by /-- The actual source raises at the instruction after the nested allocation sequence. -/ theorem source_throws : invoke (sourceProgram throwingCode) fuel 0 [] initial = - .error ⟨0, 7, .thrown "AfterNestedAllocation"⟩ := by rfl + .error ⟨0, 7, .thrown .invalidOperation⟩ := by rfl #print axioms source_throws /-- The source has consumed five fresh identities across both frames and dynamic storage. -/ @@ -173,7 +195,7 @@ theorem transferred_return : ∃ final, /-- The checked public exception transfer covers a throw after nested and dynamic allocation. -/ theorem transferred_exception : - Raises (targetProgram throwingCode) 0 [] initial "AfterNestedAllocation" := + Raises (targetProgram throwingCode) 0 [] initial .invalidOperation := (programs_related throwingCode).raises initial_formed (by intro binding member; cases member) (by intro binding member; cases member) (by intro value member; cases member) @@ -223,12 +245,15 @@ theorem zero_quota_rejected : /-- Leaving the first local operand unrelocated violates the exact instruction equation. -/ theorem unchanged_operand_rejected : - ([.local 0] : List CIL.Op) ≠ [.local 0].map indices.operation := by intro equal; cases equal + ([.ldloc 0] : List CIL.Instruction) ≠ [.ldloc 0].map indices.operation := by + intro equal; cases equal #print axioms unchanged_operand_rejected -/-- Replacing the nested call target violates the exact instruction equation. -/ +/-- Replacing the nested call's method violates the exact instruction equation. -/ theorem changed_callee_rejected : - ([.call 0 0] : List CIL.Op) ≠ [.call 1 0].map indices.operation := by intro equal; cases equal + ([.call (Tests.callee 0)] : List CIL.Instruction) ≠ + [.call (Tests.callee 1)].map indices.operation := by + intro equal; cases equal #print axioms changed_callee_rejected end Tests.AllocationExecutionRenaming @@ -238,7 +263,7 @@ open CIL.Safety /-- A compact method has no local declarations and returns a fixed scalar. -/ def compact : CIL.Method := { - locals := [], returnsValue := true, code := [.const32 7, .ret] } + locals := [], returnsValue := true, code := [.ldc_i4 7, .ret] } /-- The target inserts exactly one unused declaration with its supplied initializer. -/ def inserted (kind : CIL.LocalKind) (initial : CIL.Value) : CIL.Method := { @@ -252,7 +277,7 @@ theorem compact_returns (memory : Memory) : /-- A valid initializer supplies the existing ordinary-frame width requirements. -/ theorem inserted_ready (kind : CIL.LocalKind) (initial : CIL.Value) (fits : InitializerFits kind initial) : OrdinaryFrame (inserted kind initial) := by - refine ⟨rfl, rfl, rfl, ?_⟩ + refine ⟨rfl, rfl, ?_⟩ intro selected member have same : selected = kind := List.mem_singleton.mp member subst selected @@ -270,9 +295,22 @@ theorem inserted_related (kind : CIL.LocalKind) (initial : CIL.Value) cases method with | zero => cases found - refine ⟨inserted kind initial, rfl, ⟨⟨.skip .nil, ⟨rfl, rfl, rfl⟩, - .skip kind initial fits .nil, ?_, inserted_ready kind initial fits, rfl⟩⟩⟩ - exact ⟨rfl, rfl, rfl, by intro selected member; cases member⟩ + refine Exists.intro (inserted kind initial) (And.intro rfl (Nonempty.intro { + indices := .skip .nil + metadata := { + profile := rfl + returnsValue := rfl + staticSites := rfl + returnNarrow := rfl + arguments := fun _ => Exists.intro _ (And.intro rfl rfl) + declared := rfl + environment := rfl + links := rfl } + declarations := .skip kind initial fits .nil + sourceReady := ?_ + targetReady := inserted_ready kind initial fits + code := rfl })) + exact ⟨rfl, rfl, by intro selected member; cases member⟩ | succ method => cases found #print axioms inserted_related diff --git a/Tests/AllocationImports.cs b/Tests/AllocationImports.cs index 04f17e5cc..6fa681f17 100644 --- a/Tests/AllocationImports.cs +++ b/Tests/AllocationImports.cs @@ -1,6 +1,10 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs diff --git a/Tests/AllocationRenaming.lean b/Tests/AllocationRenaming.lean index 7a89c3e50..4d1c17366 100644 --- a/Tests/AllocationRenaming.lean +++ b/Tests/AllocationRenaming.lean @@ -110,14 +110,14 @@ def sourceBody : CIL.Method := { returnsValue := true locals := [.unmodeled] localKinds := [.word64] - code := [.local 0, .ret] } + code := [.ldloc 0, .ret] } /-- Dead numeric declarations occur both before and after the retained local. -/ def targetBody : CIL.Method := { returnsValue := true locals := [.unmodeled, .unmodeled, .unmodeled] localKinds := [.word32, .word64, .word32] - code := [.local 1, .ret] } + code := [.ldloc 1, .ret] } /-- The first source operand relocates past the preceding dead numeric local. -/ def indices : LocalIndexMap := .skip (.keep (.skip .nil)) @@ -174,9 +174,9 @@ theorem initialized_helper_frames : Memory.Renamed later (enterScopes Eq 1 1) sourceAfter targetAfter ∧ Frame.Renamed indices later (enterScopes Eq 1 1) sourceFrame targetFrame := by have sourceReady : OrdinaryFrame initializedSource := - ⟨rfl, rfl, rfl, entry_ready.1.layouts⟩ + ⟨rfl, rfl, entry_ready.1.layouts⟩ have targetReady : OrdinaryFrame initializedTarget := - ⟨rfl, rfl, rfl, entry_ready.2.layouts⟩ + ⟨rfl, rfl, entry_ready.2.layouts⟩ have selected : LocalDeclarations indices initializedSource.localKinds initializedSource.locals initializedTarget.localKinds initializedTarget.locals := .skip .word32 .unmodeled (by simp [InitializerFits]) diff --git a/Tests/ArgumentSlotScaling.lean b/Tests/ArgumentSlotScaling.lean index 687bb277f..a49f24ed6 100644 --- a/Tests/ArgumentSlotScaling.lean +++ b/Tests/ArgumentSlotScaling.lean @@ -13,8 +13,8 @@ set_option maxRecDepth 65536 def raw (count : Nat) : RawArguments := ⟨List.replicate count (.field .unsigned64), [], (List.range count).map ArgumentOp.store⟩ -/-- Each argument store targets its appended home slot. -/ -def emitted (count : Nat) : List Op := (List.range count).map fun index => .setLocal index +/-- Each argument store names its own argument, whose home the check looks up. -/ +def emitted (count : Nat) : List Instruction := (List.range count).map fun index => .starg index /-- Reference list lookup at 128 argument instructions. -/ theorem baseline128 : all3 (validateArgumentOpIn (raw 128) (List.range 128)) (raw 128).code diff --git a/Tests/Arguments.cs b/Tests/Arguments.cs index 102fe60d6..d44684789 100644 --- a/Tests/Arguments.cs +++ b/Tests/Arguments.cs @@ -2,6 +2,13 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include EmptyReturnVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Numerics; @@ -130,18 +137,11 @@ end ExampleProof method.Body.Instructions.Single(i => i.OpCode.Code is Code.Starg or Code.Starg_S).Operand = method.Parameters[1]; module.Write(wrongDll); } -var wrongRejected = false; -try -{ - verification.Verify(wrongDll, "Examples", "Arguments::Overwrite(uint,uint)", - At("Examples/Proofs/ArgumentOverwrite.lean"), wrongOutput); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - wrongRejected = true; -} -if (!wrongRejected || File.Exists(Path.Combine(wrongOutput, "verified.json"))) - throw new InvalidOperationException("Incorrect argument assignment accepted"); +verification.VerifyEmptyReturn(assembly, "Arguments::Overwrite(uint,uint)", + At("Examples/Proofs/ArgumentOverwrite.lean"), wrongDll, "Arguments::Overwrite(uint,uint)", wrongOutput, + new("W32 × W32", "[.scalar (.i32 input.1), .scalar (.i32 input.2)]", + ".scalar (.i32 input.2)", 8, + "exact ExampleProof.correctness input.1 input.2", "(0, 1)")); // Exact full and short encodings, across every admitted integer storage type. var types = new[] { "SByte", "Byte", "Boolean", "Int16", "UInt16", "Char", "Int32", "UInt32", "Int64", "UInt64", "IntPtr", "UIntPtr" }; diff --git a/Tests/ArrayElementReferences.cs b/Tests/ArrayElementReferences.cs index aab2e5b99..165fcfd56 100644 --- a/Tests/ArrayElementReferences.cs +++ b/Tests/ArrayElementReferences.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs @@ -45,10 +50,16 @@ """); evidence.Add(proof, "PASS runtime and certified array reference: " + name); if (name != "Element") continue; - const string target = ".arrayAddress .signed32"; - if (!source.Contains(target, StringComparison.Ordinal)) throw new Exception("Missing address mutation target"); + // The address is decoded from its body, so a forgery names another element at its raw token, which the certified + // stack states bind. + if (!source.Contains("cil_decoded_method method0 ", StringComparison.Ordinal)) + throw new Exception("Element address is not decoded from its body"); + const string target = """opcode := 143, operand := .type (.atom Extraction.runtimeScope "System.Int32")"""; + var scalars = NarrowEvidence(source, "method0_scalars_validated"); + if (!scalars.Contains(target, StringComparison.Ordinal)) throw new Exception("Missing address mutation target"); var forged = Path.Combine(root, "ForgedElement.lean"); - File.WriteAllText(forged, source.Replace(target, ".arrayAddress .unsigned32", StringComparison.Ordinal)); + File.WriteAllText(forged, scalars.Replace(target, target.Replace("Int32", "UInt32", StringComparison.Ordinal), + StringComparison.Ordinal)); var rejection = Execute(["lake", "env", "lean", forged], cwd: FixturePackage()); if (rejection.Code == 0 || !rejection.Output.Contains("ScalarFlowValid", StringComparison.Ordinal)) throw new Exception("Forged element address was not rejected by scalar evidence: " + rejection.Output); diff --git a/Tests/ArrayElementReferences.lean b/Tests/ArrayElementReferences.lean index 0563a059c..9ba4abef0 100644 --- a/Tests/ArrayElementReferences.lean +++ b/Tests/ArrayElementReferences.lean @@ -16,7 +16,7 @@ private def array : ArrayView := ⟨.address ⟨0, 0⟩, 2, .signed32⟩ /-- Address formation needs a live extent, not initialized contents or access authority. -/ theorem address_without_authority : - memoryInstruction (.arrayAddress .signed32) [.scalar (.i32 1), .array array] memory = + elementAddressInstruction (.value .signed32) false [.scalar (.i32 1), .array array] memory = .ok (memory, [.reference (.address ⟨0, 4⟩)]) := rfl #print axioms address_without_authority @@ -27,28 +27,28 @@ theorem address_cannot_grant_write : /-- The end position is not an array element, even though it can be a valid pointer position. -/ theorem end_address_rejected : - memoryInstruction (.arrayAddress .signed32) [.scalar (.i32 2), .array array] memory = - .error (.thrown "System.IndexOutOfRangeException") := rfl + elementAddressInstruction (.value .signed32) false [.scalar (.i32 2), .array array] memory = + .error (.thrown .indexOutOfRange) := rfl #print axioms end_address_rejected /-- A null array has no element address. -/ theorem null_address_rejected : - memoryInstruction (.arrayAddress .signed32) [.scalar (.i32 0), .object .null] memory = - .error (.thrown "System.NullReferenceException") := rfl + elementAddressInstruction (.value .signed32) false [.scalar (.i32 0), .object .null] memory = + .error (.thrown .nullReference) := rfl #print axioms null_address_rejected /-- Negative indices cannot wrap back into the allocation. -/ theorem negative_address_rejected : - memoryInstruction (.arrayAddress .signed32) + elementAddressInstruction (.value .signed32) false [.scalar (.i32 (BitVec.ofInt 32 (-1))), .array array] memory = - .error (.thrown "System.IndexOutOfRangeException") := rfl + .error (.thrown .indexOutOfRange) := rfl #print axioms negative_address_rejected /-- Empty arrays have no element, including at their valid end position. -/ theorem empty_address_rejected : - memoryInstruction (.arrayAddress .signed32) + elementAddressInstruction (.value .signed32) false [.scalar (.i32 0), .array ⟨.address ⟨0, 8⟩, 0, .signed32⟩] memory = - .error (.thrown "System.IndexOutOfRangeException") := rfl + .error (.thrown .indexOutOfRange) := rfl #print axioms empty_address_rejected /-- A returned element reference becomes invalid when its backing allocation expires. -/ @@ -57,11 +57,23 @@ theorem expired_element_rejected : .error (.memory .expiredLifetime (.address ⟨0, 4⟩) 0) := rfl #print axioms expired_element_rejected +/-- An address needs inline element storage: a class element is an ill-typed state, and no heap + array holds a by-ref-like struct. -/ +theorem address_shapes_refused : + elementAddressInstruction (.reference (.atom CIL.Extraction.runtimeScope "System.String")) + false [.scalar (.i32 0), .array array] memory = .error .invalidState ∧ + elementAddressInstruction (.unsupported CIL.ElementShape.byRefLikeElement) true + [.scalar (.i32 0), .array array] memory = + .error (.unsupported CIL.ElementShape.byRefLikeElement) := ⟨rfl, rfl⟩ +#print axioms address_shapes_refused + /-- A same-width element token does not justify a different typed element address. -/ theorem wrong_address_token : - CIL.Extraction.scalarOpMatches + CIL.Extraction.scalarOpMatches none ⟨0, 143, .type (.atom CIL.Extraction.runtimeScope "System.Int32")⟩ - [.word32, .array .signed32] (.memory (.arrayAddress .unsigned32)) = false := by decide +kernel + [.word32, .array .signed32] + (.ldelema (.atom CIL.Extraction.runtimeScope "System.UInt32") false) = false := by + decide +kernel #print axioms wrong_address_token end Tests.ArrayElementReferences diff --git a/Tests/ArrayFields.lean b/Tests/ArrayFields.lean index 4bbbb4330..9d7e63101 100644 --- a/Tests/ArrayFields.lean +++ b/Tests/ArrayFields.lean @@ -10,6 +10,14 @@ open CIL CIL.Safety private def node : RuntimeType := ⟨.object "Examples" "Examples.Node", []⟩ +/-- `Node.values`, the `int[]` field 1, in the slot at model offset 8. -/ +private def valuesField : Extraction.FieldRef := + { owner := node.identity, index := 1, + type := .array (.atom Extraction.runtimeScope "System.Int32"), name := name!"values" } + +/-- A method whose class field accesses need no layouts. -/ +private def body : CIL.Method := { returnsValue := false, locals := [], code := [] } + /-- Allocation 0 is a 16-byte node, 1 a read-only node, 2 an array of three `int` values and 3 a caller stack buffer. -/ private def holding : CIL.Safety.Memory := @@ -31,12 +39,11 @@ private def numbers : ArrayView := ⟨.address ⟨2, 0⟩, 3, .signed32⟩ /-- `stfld` of `array` into the field at offset 8 of `object`. -/ private def store (array : ArrayView) (object : ManagedReference) := - objectFieldInstruction (.setArrayField node.identity 8 .signed32) [.array array, .object object] - holding + fieldStore body valuesField [.array array, .object object] holding /-- `ldfld` of the field at offset 8 of `object` in `memory`. -/ private def load (memory : CIL.Safety.Memory) (object : ManagedReference) := - objectFieldInstruction (.arrayField node.identity 8 .signed32) [.object object] memory + fieldLoad body valuesField [.object object] memory /-- A stored array loads back whole, and only the field's slot changes, not its bytes. -/ theorem field_reads_back : @@ -55,8 +62,8 @@ theorem field_starts_null : /-- A null receiver throws on both the load and the store. -/ theorem null_receiver_throws : - load holding .null = .error (.thrown "System.NullReferenceException") ∧ - store numbers .null = .error (.thrown "System.NullReferenceException") := ⟨rfl, rfl⟩ + load holding .null = .error (.thrown .nullReference) ∧ + store numbers .null = .error (.thrown .nullReference) := ⟨rfl, rfl⟩ #print axioms null_receiver_throws /-- A store needs write authority over the field's slot. -/ diff --git a/Tests/AssemblyPins.lean b/Tests/AssemblyPins.lean index 97e6fcb7d..79e580d03 100644 --- a/Tests/AssemblyPins.lean +++ b/Tests/AssemblyPins.lean @@ -51,4 +51,17 @@ theorem default_pin_rejected : AttributionValid [({}, "::Fixture.Application")] decide +kernel #print axioms default_pin_rejected +/-- A generic argument's scope does not change the declaring aggregate's or enum's scope. -/ +theorem flat_scopes : + (TypeExpr.aggregate "Lib::Box" 0).headScope = some "Lib" ∧ + (TypeExpr.enum "Λ::Flag" (.atom "Other" "Byte")).headScope = some "Λ" := by + decide +kernel +#print axioms flat_scopes + +/-- Flat owners without a scope, separator or type name supply no assembly identity. -/ +theorem incomplete_flat_scopes : + ["Value", "::Value", "Lib:Value", "Lib::"].all + (fun owner => (TypeExpr.aggregate owner 0).headScope.isNone) := by decide +kernel +#print axioms incomplete_flat_scopes + end Tests.AssemblyPins diff --git a/Tests/AuditKeyScaling.lean b/Tests/AuditKeyScaling.lean new file mode 100644 index 000000000..bd1c5d140 --- /dev/null +++ b/Tests/AuditKeyScaling.lean @@ -0,0 +1,31 @@ +import CIL.OleanAudit + +/-! Counts decoded key bytes hashed by the audit as a single JSON object grows. -/ + +/-- Counts the bytes passed to the string hash, including any calls during bucket growth. -/ +private unsafe def countingHash (bytes : IO.Ref Nat) (key : String) : UInt64 := unsafeBaseIO do + bytes.modify (fun total => total + key.utf8ByteSize) + return hash key + +/-- Every key is hashed once, even when later insertions resize the containing object. -/ +private unsafe def checkHashCost : IO Unit := do + let longKey := String.ofList (List.replicate 65536 'x') + let mut excess := false + for count in [8, 128, 2048] do + let keys := longKey :: (List.range count).map (fun index => s!"k{index}") + let members := keys.map (fun key => (Lean.Json.str key).compress ++ ":0") + let source := "{" ++ ",".intercalate members ++ "}" + let expected := keys.foldl (fun total key => total + key.utf8ByteSize) 0 + let bytes <- IO.mkRef 0 + IO.ofExcept (@LeanCILAudit.checkRequestKeys { hash := countingHash bytes } source) + let actual <- bytes.get + IO.println s!"{count} short keys: hashed {actual} bytes for {expected} key bytes" + excess := excess || actual != expected + if excess then throw (IO.userError "A request key was hashed again during insertion") + let collision := @LeanCILAudit.checkRequestKeys { hash := fun _ => 0 } + "{\"a\":0,\"b\":1,\"a\":2}" + if let .error message := collision then + if message.endsWith "Duplicate JSON key: a" then return + throw (IO.userError "Colliding keys must match by their decoded text") + +#eval checkHashCost diff --git a/Tests/Automation.lean b/Tests/Automation.lean index 1e7b13282..72c1ec7a2 100644 --- a/Tests/Automation.lean +++ b/Tests/Automation.lean @@ -1,9 +1,19 @@ +import Tests.Support.IntrinsicMethods import Tests.FeatureExecution import CIL.Safety.SymbolicExecution +import CIL.Safety.MethodContract +import CIL.Safety.LoopAutomation +import CIL.Safety.OutcomeContract +import CIL.Safety.LocalSnapshotSteps import CIL.SIMD.VectorLemmas +import Tests.Support +import Tests.WordFields namespace Tests.Automation +attribute [local cil_code] Tests.supportedGetter_avx2_model +open Tests.WordFields + def candidate : Nat := 7 if_extracted Tests.Automation.candidate { theorem accepted : candidate = 7 := rfl @@ -22,7 +32,7 @@ run_cmd do throwError "Failed summary escaped its transaction" @[cil_code] def branchMethod : CIL.Method := { - code := [.arg 0, .brzero 4, .arg 1, .ret, .arg 1, .ret] + code := [.ldarg 0, .brfalse 4, .ldarg 1, .ret, .ldarg 1, .ret] locals := [], returnsValue := true } @[cil_code] def branchProgram : CIL.Program := [branchMethod] @@ -48,7 +58,7 @@ example : True := by Lean.Core.setMessageLog earlier @[cil_code] def arithmeticMethod : CIL.Method := { - code := [.arg 0, .arg 1, .const32 1, .shl, .add, .const32 0, .gtu, .ret] + code := [.ldarg 0, .ldarg 1, .ldc_i4 1, .shl, .add, .ldc_i4 0, .cgt_un, .ret] locals := [], returnsValue := true } -- Symbolic values still reduce definitionally, including the final return. @@ -62,7 +72,8 @@ theorem checked_arithmetic (memory : CIL.Safety.Memory) (left right : CIL.W32) : #guard_msgs in #print axioms checked_arithmetic @[cil_code] def narrowEqualityMethod : CIL.Method := { - code := [.arg 0, .arg 1, .bxor, .const32 0, .eq, .ret (some .unsigned8)] + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + code := [.ldarg 0, .ldarg 1, .xor, .ldc_i4 0, .ceq, .ret] locals := [], returnsValue := true } def scalarDifference (left right : CIL.W32) : CIL.W32 := left ^^^ right @@ -86,7 +97,8 @@ theorem checked_narrow_return (memory : CIL.Safety.Memory) (frame : CIL.Safety.F #print axioms checked_narrow_return @[cil_code] def decidedNarrowMethod : CIL.Method := { - code := [.const32 0, .const32 0, .eq, .ret (some .unsigned8)] + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + code := [.ldc_i4 0, .ldc_i4 0, .ceq, .ret] locals := [], returnsValue := true } /-- A comparison of literals decides to `1#32` before the narrowed return, which keeps it, so @@ -110,7 +122,7 @@ theorem checked_plain_return (memory : CIL.Safety.Memory) (frame : CIL.Safety.Fr /-! Run-to-run goals: a prefix of a method rewritten to the `run` it continues with. -/ @[cil_code] def prefixMethod : CIL.Method := { - code := [.arg 0, .arg 1, .add, .arg 0, .add, .ret] + code := [.ldarg 0, .ldarg 1, .add, .ldarg 0, .add, .ret] locals := [], returnsValue := true } -- Symbolic remaining fuel stops the unbounded stepper at the target, which cleanup closes. @@ -193,10 +205,11 @@ theorem run_prefix_bounded_two_goals (memory : CIL.Safety.Memory) (frame : CIL.S cil_checked_steps 3 [] @[cil_code] def identityCallee : CIL.Method := { - code := [.arg 0, .ret], locals := [], returnsValue := true } + code := [.ldarg 0, .ret], locals := [], returnsValue := true } @[cil_code] def callPrefixMethod : CIL.Method := { - code := [.arg 0, .call 1 1, .arg 0, .add, .ret], locals := [], returnsValue := true } + code := [.ldarg 0, .call (Tests.callee 1) {}, .ldarg 0, .add, + .ret], links := [(1, .direct 1)], locals := [], returnsValue := true } -- A bounded step over a call runs the callee but stops before the caller's continuation. theorem run_call_prefix_bounded (memory : CIL.Safety.Memory) (frame : CIL.Safety.Frame) (a : CIL.W64) : @@ -266,7 +279,8 @@ theorem checked_negative_literal_return (memory : CIL.Safety.Memory) (frame : CI #print axioms checked_negative_literal_return /-- A reusable body, bound to an extraction only through a lookup hypothesis. -/ def wordBody (profile : CIL.FeatureProfile) : CIL.Method := - { profile, returnsValue := true, locals := [], code := [.arg 0, .bnot, .arg 1, .bnot, .bor, .bnot, .ret] } + { profile, returnsValue := true, locals := [], code := [.ldarg 0, .not, .ldarg 1, .not, .or, + .not, .ret] } theorem word_checked (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) (found : program[index]? = some (wordBody profile)) (memory : CIL.Safety.Memory) (a b : BitVec 64) : @@ -276,7 +290,8 @@ theorem word_checked (program : CIL.Program) (index : Nat) (profile : CIL.Featur /-- A profile-dependent body: its instructions are fetched only once the feature test is decided. -/ @[cil_code] def featureBody (profile : CIL.FeatureProfile) : CIL.Method := - { profile, returnsValue := true, locals := [], code := if profile.avx2 then [.arg 1, .ret] else [.arg 0, .ret] } + { profile, returnsValue := true, locals := [], code := if profile.avx2 then [.ldarg 1, + .ret] else [.ldarg 0, .ret] } theorem feature_checked (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) (scalar : profile.avx2 = false) (found : program[index]? = some (featureBody profile)) @@ -296,18 +311,20 @@ example (program : CIL.Program) (index : Nat) (memory : CIL.Safety.Memory) (a b /-- A 27-instruction read-only leaf over symbolic memory: each field load comes from a fact, every step from an equation, so no step unfolds the dispatcher. -/ -def leafBody (profile : CIL.FeatureProfile) : CIL.Method := { profile, returnsValue := true, locals := [], code := [ - .arg 0, .fieldAt 0 8, .arg 1, .fieldAt 0 8, .bxor, - .arg 0, .fieldAt 8 8, .arg 1, .fieldAt 8 8, .bxor, .bor, - .arg 0, .fieldAt 16 8, .arg 1, .fieldAt 16 8, .bxor, .bor, - .arg 0, .fieldAt 24 8, .arg 1, .fieldAt 24 8, .bxor, .bor, - .const32 0, .convI8, .eq, .ret (some .unsigned8)] } +def leafBody (profile : CIL.FeatureProfile) : CIL.Method := { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + profile, environment := wordsEnvironment, returnsValue := true, locals := [], code := [ + .ldarg 0, .ldfld word0, .ldarg 1, .ldfld word0, .xor, + .ldarg 0, .ldfld word1, .ldarg 1, .ldfld word1, .xor, .or, + .ldarg 0, .ldfld word2, .ldarg 1, .ldfld word2, .xor, .or, + .ldarg 0, .ldfld word3, .ldarg 1, .ldfld word3, .xor, .or, + .ldc_i4 0, .conv_i8, .ceq, .ret] } open CIL.Safety in theorem leaf_checked (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) (found : program[index]? = some (leafBody profile)) (memory : Memory) (left right : Reference) (limb : Reference → Nat → BitVec 64) (fl : form memory left = .ok left) (fr : form memory right = .ok right) - (field : ∀ r offset rest, instruction (.fieldAt offset 8) (.reference (.address r) :: rest) memory = + (field : ∀ r offset rest, loadValueField offset 8 false (.reference (.address r) :: rest) memory = .ok (memory, .scalar (.i64 (limb r offset)) :: rest)) : let difference := (limb left 0 ^^^ limb right 0) ||| (limb left 8 ^^^ limb right 8) ||| (limb left 16 ^^^ limb right 16) ||| (limb left 24 ^^^ limb right 24) @@ -316,7 +333,8 @@ theorem leaf_checked (program : CIL.Program) (index : Nat) (profile : CIL.Featur cil_checked_steps [fl, fr, field] def boundBody (profile : CIL.FeatureProfile) : CIL.Method := - { profile, returnsValue := true, locals := [], code := [.arg 0, .arg 1, .bltu 5, .arg 0, .ret, .arg 1, .ret] } + { profile, returnsValue := true, locals := [], + code := [.ldarg 0, .ldarg 1, .blt_un 5, .ldarg 0, .ret, .ldarg 1, .ret] } /-- A fact decides an unsigned i64 branch, so the run falls through to pc 3. -/ theorem bound_checked (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) @@ -328,7 +346,8 @@ theorem bound_checked (program : CIL.Program) (index : Nat) (profile : CIL.Featu /-- A signed branch takes its fact in the interpreter's own form, so no linear-arithmetic restatement is needed. -/ theorem signed_bound_checked (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) - (found : program[index]? = some { boundBody profile with code := [.arg 0, .arg 1, .blt 5, .arg 0, .ret, .arg 1, .ret] }) + (found : program[index]? = some { boundBody profile with code := [.ldarg 0, .ldarg 1, .blt 5, + .ldarg 0, .ret, .ldarg 1, .ret] }) (memory : CIL.Safety.Memory) (x y : CIL.W64) (above : ¬ x.toInt < y.toInt) : CIL.Safety.invoke program 8 index [.scalar (.i64 x), .scalar (.i64 y)] memory = .ok (memory, [.scalar (.i64 x)]) := by @@ -337,7 +356,8 @@ theorem signed_bound_checked (program : CIL.Program) (index : Nat) (profile : CI -- The step equations alone rewrite a compare-branch; the dispatcher stays folded. example (body : CIL.Method) (args : List CIL.Safety.Value) (frame : CIL.Safety.Frame) (m : CIL.Safety.Memory) (x y : CIL.W64) (above : ¬ x < y) : - CIL.Safety.step body (.bltu 5) 2 args frame [.scalar (.i64 y), .scalar (.i64 x)] m = .ok (.next 3 [] frame m) := by + CIL.Safety.step body (.blt_un 5) 2 args frame [.scalar (.i64 y), .scalar (.i64 x)] m = + .ok (.next 3 [] frame m) := by simp only [cil_step, above, ↓reduceIte] -- Guard reduction inspects only consumed values, even with an unknown stack tail. @@ -384,7 +404,7 @@ theorem guard_unknown_requires_fact (value : CIL.Value) (known : value.hasUnspec open CIL.Safety in theorem scalar_fact_stack (body : CIL.Method) (args : List Value) (frame : Frame) (memory : Memory) (left right : CIL.W64) (word : Nat) (unrelated : word = 7) : - ∃ stack, step body .bxor 0 args frame [.scalar (.i64 right), .scalar (.i64 left)] memory = + ∃ stack, step body .xor 0 args frame [.scalar (.i64 right), .scalar (.i64 left)] memory = .ok (.next 1 stack frame memory) ∧ stack = [.scalar (.i64 (left ^^^ right))] := by refine ⟨?_, ?_, ?_⟩ rotate_left @@ -392,10 +412,10 @@ theorem scalar_fact_stack (body : CIL.Method) (args : List Value) (frame : Frame · rfl open CIL.Safety in -theorem bitcast_fact_stack (body : CIL.Method) (args : List Value) (frame : Frame) +theorem bitcast_fact_stack (body : CIL.Method) (frame : Frame) (memory : Memory) (bytes : List (BitVec 8)) (value : BitVec 256) (casted : bitcastValue (.aggregate 32) .vector256 (.aggregate bytes) = .ok (.v256 value)) : - ∃ stack, step body (.memory (.bitcastValue (.aggregate 32) .vector256)) 0 args frame + ∃ stack, modelStep (some (.memory (.bitcastValue (.aggregate 32) .vector256))) body 0 frame [.scalar (.aggregate bytes)] memory = .ok (.next 1 stack frame memory) ∧ stack = [.scalar (.v256 value)] := by refine ⟨?_, ?_, ?_⟩ @@ -413,7 +433,7 @@ theorem bitcast_fact_stack (body : CIL.Method) (args : List Value) (frame : Fram @[cil_code] def permuteStoreMethod : CIL.Method := { profile := { architecture := .x64, avx2 := true } - code := [.intrinsic (.avx2 .permute4x64) 2, .memory .store256, .ret] + code := [.call Tests.IntrinsicMethods.permute, .stobj Tests.IntrinsicMethods.vector256UInt64, .ret] locals := [], returnsValue := false } @[cil_code] def permuteStoreProgram : CIL.Program := [permuteStoreMethod] @@ -432,11 +452,11 @@ theorem permute_then_store (a b c d : CIL.W64) (destination : Reference) (memory open CIL.Safety in /-- A `memoryInstruction` fact closes a single store step without `staticInstruction` passed. -/ -theorem store_from_instruction_fact (body : CIL.Method) (args : List Value) (frame : Frame) +theorem store_from_instruction_fact (body : CIL.Method) (frame : Frame) (bits : BitVec 256) (destination : Reference) (memory final : Memory) (stored : memoryInstruction .store256 [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (final, [])) : - step body (.memory .store256) 2 args frame + modelStep (some (.memory .store256)) body 2 frame [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (.next 3 [] frame final) := by cil_checked_step [stored] @@ -466,11 +486,11 @@ info: 'Tests.Automation.duplicate_from_instruction_fact' depends on axioms: [pro open CIL.Safety in /-- A fact one level up, on `staticInstruction`, still closes the store: the retry tries it before unfolding `staticInstruction`, which would rewrite past it. -/ -theorem store_from_static_fact (body : CIL.Method) (args : List Value) (frame : Frame) +theorem store_from_static_fact (body : CIL.Method) (frame : Frame) (bits : BitVec 256) (destination : Reference) (memory final : Memory) (stored : staticInstruction body 2 .store256 [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (final, [])) : - step body (.memory .store256) 2 args frame + modelStep (some (.memory .store256)) body 2 frame [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (.next 3 [] frame final) := by cil_checked_step [stored] @@ -480,9 +500,24 @@ info: 'Tests.Automation.store_from_static_fact' depends on axioms: [propext, Quo -/ #guard_msgs in #print axioms store_from_static_fact +open CIL.Safety in +/-- A static load steps by a fact about `staticLoad`, which `staticLoad_initialized` gives from + the descriptor a `staticAt` fact binds to the pc. -/ +theorem static_load_from_fact (body : CIL.Method) (args : List Value) (frame : Frame) + (field : CIL.Extraction.FieldRef) (value : CIL.Value) (memory : Memory) + (loaded : staticLoad body 0 memory = .ok (memory, .scalar value)) : + step body (.ldsfld field) 0 args frame [] memory = + .ok (.next 1 [.scalar value] frame memory) := by + cil_checked_step [loaded] + +/-- +info: 'Tests.Automation.static_load_from_fact' depends on axioms: [propext, Quot.sound] +-/ +#guard_msgs in #print axioms static_load_from_fact + /-- Two vector stores; reached through a lookup hypothesis, so facts can name the body. -/ def twoStoresMethod : CIL.Method := { - code := [.memory .store256, .memory .store256, .ret], locals := [], returnsValue := false } + code := [.stobj Tests.IntrinsicMethods.vector256UInt64, .stobj Tests.IntrinsicMethods.vector256UInt64, .ret], locals := [], returnsValue := false } open CIL.Safety in /-- Facts at both levels in one run: each store takes the retry for its own fact's level, so @@ -504,31 +539,31 @@ theorem stores_from_mixed_facts (program : CIL.Program) (index : Nat) -- Naming `staticInstruction` explicitly, and stating either fact as a proof term, still applies. open CIL.Safety in -example (body : CIL.Method) (args : List Value) (frame : Frame) (bits : BitVec 256) +example (body : CIL.Method) (frame : Frame) (bits : BitVec 256) (destination : Reference) (memory final : Memory) (stored : memoryInstruction .store256 [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (final, [])) : - step body (.memory .store256) 2 args frame + modelStep (some (.memory .store256)) body 2 frame [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (.next 3 [] frame final) := by cil_checked_step [staticInstruction, stored] open CIL.Safety in -example (body : CIL.Method) (args : List Value) (frame : Frame) (bits : BitVec 256) +example (body : CIL.Method) (frame : Frame) (bits : BitVec 256) (destination : Reference) (memory final : Memory) (stored : memoryInstruction .store256 [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (final, [])) : - step body (.memory .store256) 2 args frame + modelStep (some (.memory .store256)) body 2 frame [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (.next 3 [] frame final) := by cil_checked_step [(stored)] open CIL.Safety in -example (body : CIL.Method) (args : List Value) (frame : Frame) (bits : BitVec 256) +example (body : CIL.Method) (frame : Frame) (bits : BitVec 256) (destination : Reference) (memory final : Memory) (stored : staticInstruction body 2 .store256 [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (final, [])) : - step body (.memory .store256) 2 args frame + modelStep (some (.memory .store256)) body 2 frame [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (.next 3 [] frame final) := by cil_checked_step [(stored)] @@ -544,7 +579,7 @@ theorem permute_then_store_value (a b c d : CIL.W64) (destination : Reference) ( /-- A feature test steps from a fact about the profile field, through a body neither unfolded nor tagged. -/ def featureTestBody (profile : CIL.FeatureProfile) : CIL.Method := - { profile, returnsValue := true, locals := [], code := [.feature .avx2, .ret] } + { profile, returnsValue := true, locals := [], code := [.call (Tests.supportedGetter "X86.Avx2"), .ret] } theorem feature_step_from_fact (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) (wide : profile.avx2 = true) (found : program[index]? = some (featureTestBody profile)) @@ -553,24 +588,25 @@ theorem feature_step_from_fact (program : CIL.Program) (index : Nat) (profile : cil_checked_steps [wide] theorem feature_step_free_body (program : CIL.Program) (index : Nat) (body : CIL.Method) - (wide : body.profile.avx2 = false) (code : body.code = [.feature .avx2, .ret]) + (wide : body.profile.avx2 = false) (unlinked : body.linkAt 0 = none) + (code : body.code = [.call (Tests.supportedGetter "X86.Avx2"), .ret]) (returns : body.returnsValue = true) (found : program[index]? = some body) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) : CIL.Safety.run program 3 index 0 [] frame [] memory = .ok (CIL.Safety.leaveFrame frame memory, [.scalar (.i32 0)]) := by - cil_checked_steps [wide, code, returns] + cases narrow : body.returnNarrow <;> cil_checked_steps [unlinked, wide, code, returns, narrow] -- A literal profile decides the test with no fact. theorem feature_step_literal_profile (memory : CIL.Safety.Memory) : - CIL.Safety.invoke [{ permuteStoreMethod with code := [.feature .avx2, .ret], returnsValue := true }] 3 0 [] + CIL.Safety.invoke [{ permuteStoreMethod with code := [.call (Tests.supportedGetter "X86.Avx2"), .ret], returnsValue := true }] 3 0 [] memory = .ok (memory, [.scalar (.i32 1)]) := by cil_checked_steps -- Each memory step rewrites by its own equation; no fact names `staticInstruction` or `memoryInstruction`. open CIL.Safety in -theorem asRef_fact_stack (body : CIL.Method) (args : List Value) (frame : Frame) (memory : Memory) +theorem asRef_fact_stack (body : CIL.Method) (frame : Frame) (memory : Memory) (reference : Reference) (formed : form memory reference = .ok reference) : - ∃ stack, step body (.memory .asRef) 0 args frame [.reference (.address reference)] memory = + ∃ stack, modelStep (some (.memory .asRef)) body 0 frame [.reference (.address reference)] memory = .ok (.next 1 stack frame memory) ∧ stack = [.reference (.address reference)] := by refine ⟨?_, ?_, ?_⟩ rotate_left @@ -578,10 +614,10 @@ theorem asRef_fact_stack (body : CIL.Method) (args : List Value) (frame : Frame) · rfl open CIL.Safety in -theorem load128_fact_stack (body : CIL.Method) (args : List Value) (frame : Frame) (memory : Memory) +theorem load128_fact_stack (body : CIL.Method) (frame : Frame) (memory : Memory) (source : ManagedReference) (value : BitVec 128) (loaded : loadValue memory source 16 = .ok (.v128 value)) : - ∃ stack, step body (.memory .load128) 0 args frame [.reference source] memory = + ∃ stack, modelStep (some (.memory .load128)) body 0 frame [.reference source] memory = .ok (.next 1 stack frame memory) ∧ stack = [.scalar (.v128 value)] := by refine ⟨?_, ?_, ?_⟩ rotate_left @@ -589,10 +625,10 @@ theorem load128_fact_stack (body : CIL.Method) (args : List Value) (frame : Fram · rfl open CIL.Safety in -theorem add_fact_stack (body : CIL.Method) (args : List Value) (frame : Frame) (memory : Memory) +theorem add_fact_stack (body : CIL.Method) (frame : Frame) (memory : Memory) (base result : Reference) (offset : CIL.W32) (moved : add memory base 8 (BitVec.ofInt 64 offset.toInt) = .ok result) : - ∃ stack, step body (.memory (.add 8 true)) 0 args frame + ∃ stack, modelStep (some (.memory (.add 8 true))) body 0 frame [.scalar (.i32 offset), .reference (.address base)] memory = .ok (.next 1 stack frame memory) ∧ stack = [.reference (.address result)] := by refine ⟨?_, ?_, ?_⟩ @@ -602,11 +638,11 @@ theorem add_fact_stack (body : CIL.Method) (args : List Value) (frame : Frame) ( -- A symbolic managed reference stays `referenceAt reference`, so the caller's fact about it applies. open CIL.Safety in -theorem add_symbolic_reference (body : CIL.Method) (args : List Value) (frame : Frame) (memory : Memory) +theorem add_symbolic_reference (body : CIL.Method) (frame : Frame) (memory : Memory) (reference : ManagedReference) (base result : Reference) (offset : CIL.W32) (addressed : referenceAt reference = .ok base) (moved : add memory base 8 (BitVec.ofInt 64 offset.toInt) = .ok result) : - ∃ stack, step body (.memory (.add 8 true)) 0 args frame + ∃ stack, modelStep (some (.memory (.add 8 true))) body 0 frame [.scalar (.i32 offset), .reference reference] memory = .ok (.next 1 stack frame memory) ∧ stack = [.reference (.address result)] := by refine ⟨?_, ?_, ?_⟩ @@ -616,8 +652,8 @@ theorem add_symbolic_reference (body : CIL.Method) (args : List Value) (frame : -- An unknown scalar keeps its guard: the equation exposes it rather than assuming it away. open CIL.Safety in -example (body : CIL.Method) (args : List Value) (frame : Frame) (memory : Memory) (value : CIL.Value) : - step body (.memory (.bitcastValue (.aggregate 32) .vector256)) 0 args frame [.scalar value] memory = +example (body : CIL.Method) (frame : Frame) (memory : Memory) (value : CIL.Value) : + modelStep (some (.memory (.bitcastValue (.aggregate 32) .vector256))) body 0 frame [.scalar value] memory = if value.hasUnspecified then .error (.unsupported "Instruction observes unspecified vector bits") else do let cast ← bitcastValue (.aggregate 32) .vector256 value return .next 1 [.scalar cast] frame memory := by @@ -631,19 +667,20 @@ theorem feature_step_evaluate_fact (program : CIL.Program) (index : Nat) (profil cil_checked_steps [wide] theorem feature_step_evaluate_free_body (program : CIL.Program) (index : Nat) (body : CIL.Method) - (wide : body.profile.evaluate .avx2 = false) (code : body.code = [.feature .avx2, .ret]) + (wide : body.profile.evaluate .avx2 = false) (unlinked : body.linkAt 0 = none) + (code : body.code = [.call (Tests.supportedGetter "X86.Avx2"), .ret]) (returns : body.returnsValue = true) (found : program[index]? = some body) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) : CIL.Safety.run program 3 index 0 [] frame [] memory = .ok (CIL.Safety.leaveFrame frame memory, [.scalar (.i32 0)]) := by - cil_checked_steps [wide, code, returns] + cases narrow : body.returnNarrow <;> cil_checked_steps [unlinked, wide, code, returns, narrow] -- A supplied proof term, not just a local name, is normalized to the profile field. theorem feature_step_evaluate_term (body : CIL.Method) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) - (wide : body.profile.evaluate .avx2 = true) : - CIL.Safety.step body (.feature .avx2) 0 [] frame [] memory = + (unlinked : body.linkAt 0 = none) (wide : body.profile.evaluate .avx2 = true) : + CIL.Safety.step body (.call (Tests.supportedGetter "X86.Avx2")) 0 [] frame [] memory = .ok (.next 1 [.scalar (.i32 1)] frame memory) := by - cil_checked_step [(show body.profile.evaluate .avx2 = true from wide)] + cil_checked_step [unlinked, (show body.profile.evaluate .avx2 = true from wide)] theorem feature_steps_evaluate_term (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) (wide : profile.evaluate .avx2 = true) (found : program[index]? = some (featureTestBody profile)) @@ -653,28 +690,31 @@ theorem feature_steps_evaluate_term (program : CIL.Program) (index : Nat) (profi -- A fact over every feature is instantiated at each constructor, so it still decides the field test. theorem feature_step_evaluate_forall (body : CIL.Method) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) - (all : ∀ f, body.profile.evaluate f = true) : - CIL.Safety.step body (.feature .avx2) 0 [] frame [] memory = + (unlinked : body.linkAt 0 = none) (all : ∀ f, body.profile.evaluate f = true) : + CIL.Safety.step body (.call (Tests.supportedGetter "X86.Avx2")) 0 [] frame [] memory = .ok (.next 1 [.scalar (.i32 1)] frame memory) := by - cil_checked_step [all] + cil_checked_step [unlinked, all] theorem feature_steps_evaluate_forall (program : CIL.Program) (index : Nat) (body : CIL.Method) - (all : ∀ f, body.profile.evaluate f = false) (code : body.code = [.feature .avx2, .ret]) + (all : ∀ f, body.profile.evaluate f = false) (unlinked : body.linkAt 0 = none) + (code : body.code = [.call (Tests.supportedGetter "X86.Avx2"), .ret]) (returns : body.returnsValue = true) (found : program[index]? = some body) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) : CIL.Safety.run program 3 index 0 [] frame [] memory = .ok (CIL.Safety.leaveFrame frame memory, [.scalar (.i32 0)]) := by - cil_checked_steps [all, code, returns] + cases narrow : body.returnNarrow <;> cil_checked_steps [unlinked, all, code, returns, narrow] /-- A fact about a symbolic feature rewrites the pushed test by hand: `cil_step` keeps `body.profile.evaluate f` folded instead of splitting on `f`. -/ theorem feature_step_symbolic_fact (body : CIL.Method) (frame : CIL.Safety.Frame) - (memory : CIL.Safety.Memory) (feature : CIL.Feature) + (memory : CIL.Safety.Memory) (feature : CIL.Feature) (getter : CIL.Extraction.MethodRef) + (unlinked : body.linkAt 0 = none) + (model : CIL.Extraction.runtimeModel body.environment .call getter = some (.feature feature)) (enabled : body.profile.evaluate feature = true) : - CIL.Safety.step body (.feature feature) 0 [] frame [] memory = + CIL.Safety.step body (.call getter) 0 [] frame [] memory = .ok (.next 1 [.scalar (.i32 1)] frame memory) := by - simp only [cil_step, enabled, ite_true] - rfl + rw [CIL.Safety.step_call_feature _ _ _ _ _ unlinked model rfl] + simp only [enabled, ite_true] open Lean Elab Tactic in /-- Fails if normalising `facts` restates any of them as a new hypothesis. -/ @@ -687,12 +727,14 @@ elab "expect_facts_already_normal " "[" facts:term,* "]" : tactic => do /-- Checked stepping uses the fact as stated: the normaliser leaves a symbolic feature test folded, as the step leaves it, rather than restating the fact as a match on `f`. -/ theorem checked_symbolic_feature_fact (body : CIL.Method) (frame : CIL.Safety.Frame) - (memory : CIL.Safety.Memory) (feature : CIL.Feature) + (memory : CIL.Safety.Memory) (feature : CIL.Feature) (getter : CIL.Extraction.MethodRef) + (unlinked : body.linkAt 0 = none) + (model : CIL.Extraction.runtimeModel body.environment .call getter = some (.feature feature)) (disabled : body.profile.evaluate feature = false) : - CIL.Safety.step body (.feature feature) 0 [] frame [] memory = + CIL.Safety.step body (.call getter) 0 [] frame [] memory = .ok (.next 1 [.scalar (.i32 0)] frame memory) := by expect_facts_already_normal [disabled] - cil_checked_step [disabled] + cil_checked_step [unlinked, model, disabled] /-- info: 'Tests.Automation.feature_step_symbolic_fact' depends on axioms: [propext, Quot.sound] -/ #guard_msgs in #print axioms feature_step_symbolic_fact @@ -709,15 +751,15 @@ theorem wideProfile_avx2 : wideProfile.evaluate .avx2 = true := rfl attribute [irreducible] wideProfile theorem feature_step_evaluate_global (body : CIL.Method) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) - (profile : body.profile = wideProfile) : - CIL.Safety.step body (.feature .avx2) 0 [] frame [] memory = + (unlinked : body.linkAt 0 = none) (profile : body.profile = wideProfile) : + CIL.Safety.step body (.call (Tests.supportedGetter "X86.Avx2")) 0 [] frame [] memory = .ok (.next 1 [.scalar (.i32 1)] frame memory) := by - cil_checked_step [profile, wideProfile_avx2] + cil_checked_step [unlinked, profile, wideProfile_avx2] -- A fact on a method's profile projection lands where `cil_step` reduces the projection. theorem feature_step_projection_fact (profile : CIL.FeatureProfile) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) (wide : (featureTestBody profile).profile.evaluate .avx2 = true) : - CIL.Safety.step (featureTestBody profile) (.feature .avx2) 0 [] frame [] memory = + CIL.Safety.step (featureTestBody profile) (.call (Tests.supportedGetter "X86.Avx2")) 0 [] frame [] memory = .ok (.next 1 [.scalar (.i32 1)] frame memory) := by cil_checked_step [wide] @@ -729,16 +771,16 @@ theorem feature_steps_projection_fact (program : CIL.Program) (index : Nat) (pro theorem feature_step_projection_field (profile : CIL.FeatureProfile) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) (wide : (featureTestBody profile).profile.avx2 = true) : - CIL.Safety.step (featureTestBody profile) (.feature .avx2) 0 [] frame [] memory = + CIL.Safety.step (featureTestBody profile) (.call (Tests.supportedGetter "X86.Avx2")) 0 [] frame [] memory = .ok (.next 1 [.scalar (.i32 1)] frame memory) := by cil_checked_step [wide] -- Quantified feature facts given as proof terms or global theorems are instantiated like local ones. theorem feature_step_forall_term (body : CIL.Method) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) - (all : ∀ f, body.profile.evaluate f = true) : - CIL.Safety.step body (.feature .avx2) 0 [] frame [] memory = + (unlinked : body.linkAt 0 = none) (all : ∀ f, body.profile.evaluate f = true) : + CIL.Safety.step body (.call (Tests.supportedGetter "X86.Avx2")) 0 [] frame [] memory = .ok (.next 1 [.scalar (.i32 1)] frame memory) := by - cil_checked_step [(show ∀ f, body.profile.evaluate f = true from all)] + cil_checked_step [unlinked, (show ∀ f, body.profile.evaluate f = true from all)] def bareProfile : CIL.FeatureProfile := {} @@ -747,14 +789,24 @@ theorem bareProfile_none : ∀ f, bareProfile.evaluate f = false := by intro f; attribute [irreducible] bareProfile theorem feature_step_forall_global (body : CIL.Method) (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) - (profile : body.profile = bareProfile) : - CIL.Safety.step body (.feature .avx2) 0 [] frame [] memory = + (unlinked : body.linkAt 0 = none) (profile : body.profile = bareProfile) : + CIL.Safety.step body (.call (Tests.supportedGetter "X86.Avx2")) 0 [] frame [] memory = .ok (.next 1 [.scalar (.i32 0)] frame memory) := by - cil_checked_step [profile, bareProfile_none] + cil_checked_step [unlinked, profile, bareProfile_none] -- Pointer arithmetic in the run loop steps without a `referenceAt` fact. +private def addUInt64 : CIL.Extraction.MethodRef := + let element := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.UInt64" + let offset := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.Int32" + { owner := CIL.Extraction.unsafeOwner, name := name!"Add", hasThis := false, + genericArity := 1, methodArguments := [element], parameters := [.byref element, offset], + returnType := .byref element } + @[cil_code] def addBody : CIL.Method := - { returnsValue := true, locals := [], code := [.arg 0, .const32 8, .memory (.add 8 true), .pop, .const32 1, .ret] } + { returnsValue := true, locals := [], code := [.ldarg 0, .ldc_i4 8, .call addUInt64, .pop, + .ldc_i4 1, .ret] } + +cil_runtime_models addBody open CIL.Safety in theorem add_run (memory : Memory) (source moved : Reference) @@ -796,7 +848,7 @@ reference : Reference -/ #guard_msgs in example (body : CIL.Method) (frame : Frame) (memory : Memory) (reference : Reference) : - step body (.memory .asRef) 0 [] frame [.reference (.address reference)] memory = + modelStep (some (.memory .asRef)) body 0 frame [.reference (.address reference)] memory = .ok (.next 1 [.reference (.address reference)] frame memory) := by cil_checked_step [] @@ -856,15 +908,15 @@ theorem literal_stack_cleanup (program : CIL.Program) (frame : Frame) (memory : /-- A supplied step fact is used before the op's own equation, which here cannot read the frame's slot; the body is `cil_code`, so the fact must be tried before the lookup unfolds it. -/ @[cil_code] def slotBody (profile : CIL.FeatureProfile) : CIL.Method := - { profile, returnsValue := true, locals := [], code := [.setLocal 0, .local 0, .ret] } + { profile, returnsValue := true, locals := [], code := [.stloc 0, .ldloc 0, .ret] } open CIL.Safety in theorem step_facts_first (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) (found : program[index]? = some (slotBody profile)) (args : List Value) (frame stored : Frame) (memory written : Memory) (value : CIL.W32) - (store : step (slotBody profile) (.setLocal 0) 0 args frame [.scalar (.i32 value)] memory = + (store : step (slotBody profile) (.stloc 0) 0 args frame [.scalar (.i32 value)] memory = .ok (.next 1 [] stored written)) - (load : ∀ stack, step (slotBody profile) (.local 0) 1 args stored stack written = + (load : ∀ stack, step (slotBody profile) (.ldloc 0) 1 args stored stack written = .ok (.next 2 (.scalar (.i32 value) :: stack) stored written)) : run program 3 index 0 args frame [.scalar (.i32 value)] memory = .ok (leaveFrame stored written, [.scalar (.i32 value)]) := by @@ -878,9 +930,10 @@ open CIL.Safety in theorem applied_step_facts_first (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) (found : program[index]? = some (slotBody profile)) (args : List Value) (frame stored : Frame) (memory written : Memory) (value : CIL.W32) - (store : ∀ value : CIL.W32, step (slotBody profile) (.setLocal 0) 0 args frame [.scalar (.i32 value)] memory = + (store : ∀ value : CIL.W32, + step (slotBody profile) (.stloc 0) 0 args frame [.scalar (.i32 value)] memory = .ok (.next 1 [] stored written)) - (load : ∀ stack, step (slotBody profile) (.local 0) 1 args stored stack written = + (load : ∀ stack, step (slotBody profile) (.ldloc 0) 1 args stored stack written = .ok (.next 2 (.scalar (.i32 value) :: stack) stored written)) : run program 3 index 0 args frame [.scalar (.i32 value)] memory = .ok (leaveFrame stored written, [.scalar (.i32 value)]) := by @@ -891,14 +944,14 @@ theorem applied_step_facts_first (program : CIL.Program) (index : Nat) (profile /-- An extracted program's lookup unfolds the body to its literal, so a fact on the named body must still match. -/ @[cil_code] def slotMethod : CIL.Method := - { returnsValue := true, locals := [], code := [.setLocal 0, .local 0, .ret] } + { returnsValue := true, locals := [], code := [.stloc 0, .ldloc 0, .ret] } @[cil_code] def slotProgram : CIL.Program := [slotMethod] open CIL.Safety in theorem named_body_step_facts (frame stored : Frame) (memory written : Memory) (value : CIL.W32) - (store : step slotMethod (.setLocal 0) 0 [] frame [.scalar (.i32 value)] memory = + (store : step slotMethod (.stloc 0) 0 [] frame [.scalar (.i32 value)] memory = .ok (.next 1 [] stored written)) - (load : ∀ stack, step slotMethod (.local 0) 1 [] stored stack written = + (load : ∀ stack, step slotMethod (.ldloc 0) 1 [] stored stack written = .ok (.next 2 (.scalar (.i32 value) :: stack) stored written)) : run slotProgram 3 0 0 [] frame [.scalar (.i32 value)] memory = .ok (leaveFrame stored written, [.scalar (.i32 value)]) := by @@ -910,9 +963,9 @@ theorem named_body_step_facts (frame stored : Frame) (memory written : Memory) ( -- A fact quantified over the body matches the literal directly. open CIL.Safety in theorem quantified_body_step_facts (frame stored : Frame) (memory written : Memory) (value : CIL.W32) - (store : ∀ body pc args, step body (.setLocal 0) pc args frame [.scalar (.i32 value)] memory = + (store : ∀ body pc args, step body (.stloc 0) pc args frame [.scalar (.i32 value)] memory = .ok (.next (pc + 1) [] stored written)) - (load : ∀ body pc args stack, step body (.local 0) pc args stored stack written = + (load : ∀ body pc args stack, step body (.ldloc 0) pc args stored stack written = .ok (.next (pc + 1) (.scalar (.i32 value) :: stack) stored written)) : run slotProgram 3 0 0 [] frame [.scalar (.i32 value)] memory = .ok (leaveFrame stored written, [.scalar (.i32 value)]) := by @@ -923,11 +976,11 @@ theorem quantified_body_step_facts (frame stored : Frame) (memory written : Memo open CIL.Safety in /-- A supplied step fact is found after an earlier tactic `have` wraps the target in metadata; - the `setLocal` equation alone cannot read the abstract frame's slot. -/ + the `stloc` equation alone cannot read the abstract frame's slot. -/ theorem step_fact_after_have (frame stored : Frame) (memory written : Memory) (value : CIL.W32) - (store : step slotMethod (.setLocal 0) 0 [] frame [.scalar (.i32 value)] memory = + (store : step slotMethod (.stloc 0) 0 [] frame [.scalar (.i32 value)] memory = .ok (.next 1 [] stored written)) : - step slotMethod (.setLocal 0) 0 [] frame [.scalar (.i32 value)] memory = + step slotMethod (.stloc 0) 0 [] frame [.scalar (.i32 value)] memory = .ok (.next 1 [] stored written) := by have unchanged : value = value := rfl cil_checked_step [store] @@ -936,7 +989,7 @@ theorem step_fact_after_have (frame stored : Frame) (memory written : Memory) (v #guard_msgs in #print axioms step_fact_after_have @[cil_code] def nonzeroMethod : CIL.Method := { - code := [.arg 0, .brnonzero 4, .arg 1, .ret, .arg 0, .ret] + code := [.ldarg 0, .brtrue 4, .ldarg 1, .ret, .ldarg 0, .ret] locals := [], returnsValue := true } -- A branch pc is decided from the condition's own fact, not only from a `CIL.truth` restatement. @@ -958,7 +1011,7 @@ theorem branch_from_zero (memory : CIL.Safety.Memory) (condition value : CIL.W32 -- A literal condition is decided by evaluation, with no fact at all. @[cil_code] def literalBranchMethod : CIL.Method := { - code := [.const32 5, .brnonzero 4, .arg 0, .ret, .arg 1, .ret] + code := [.ldc_i4 5, .brtrue 4, .ldarg 0, .ret, .ldarg 1, .ret] locals := [], returnsValue := true } theorem literal_branch (memory : CIL.Safety.Memory) (condition value : CIL.W32) : @@ -973,13 +1026,13 @@ theorem literal_branch (memory : CIL.Safety.Memory) (condition value : CIL.W32) /-- A body whose instructions, not its code list, depend on a feature. -/ def featureOpBody (profile : CIL.FeatureProfile) : CIL.Method := - { profile, returnsValue := true, locals := [], - code := [if profile.avx2 then .arg 1 else .arg 0, .fieldAt 0 8, .ret] } + { profile, environment := wordsEnvironment, returnsValue := true, locals := [], + code := [if profile.avx2 then .ldarg 1 else .ldarg 0, .ldfld word0, .ret] } -- The lookup decides the feature test, so the fetched op is a constructor with a step equation. -- A term-mode `by`, as the contract and loop tactics call it, must still assign the op. example (profile : CIL.FeatureProfile) (scalar : profile.avx2 = false) : - ∃ op, (featureOpBody profile).code[0]? = some op ∧ op = .arg 0 := + ∃ op, (featureOpBody profile).code[0]? = some op ∧ op = .ldarg 0 := ⟨_, by cil_lookup [scalar], by with_reducible rfl⟩ open CIL.Safety in @@ -987,7 +1040,7 @@ theorem feature_op_checked (program : CIL.Program) (index : Nat) (profile : CIL. (scalar : profile.avx2 = false) (found : program[index]? = some (featureOpBody profile)) (frame : Frame) (memory : Memory) (left right : Reference) (limb : BitVec 64) (formed : form memory left = .ok left) - (field : ∀ rest, instruction (.fieldAt 0 8) (.reference (.address left) :: rest) memory = + (field : ∀ rest, loadValueField 0 8 false (.reference (.address left) :: rest) memory = .ok (memory, .scalar (.i64 limb) :: rest)) : run program 4 index 0 [.reference (.address left), .reference (.address right)] frame [] memory = .ok (leaveFrame frame memory, [.scalar (.i64 limb)]) := by @@ -1061,7 +1114,7 @@ theorem intrinsic_without_facts (a b c d : CIL.W64) (destination : Reference) (m #guard_msgs in #print axioms intrinsic_without_facts @[cil_code] def checkedAdd64Method : CIL.Method := { - code := [.arg 0, .arg 1, .checkedArithmetic .add false, .ret] + code := [.ldarg 0, .ldarg 1, .add_ovf_un, .ret] locals := [], returnsValue := true } @[cil_code] def checkedAdd64Program : CIL.Program := [checkedAdd64Method] @@ -1084,12 +1137,12 @@ theorem checked_add64_return (memory : CIL.Safety.Memory) (x y : W64) (fits : x. open CIL.Safety in /-- A dispatch fact preserves its abstract value when the step infers the next stack. -/ -theorem bitcast_dispatch_keeps_value (body : CIL.Method) (args : List Value) (frame : Frame) +theorem bitcast_dispatch_keeps_value (body : CIL.Method) (frame : Frame) (memory : Memory) (byte : BitVec 8) (value : BitVec 256) (casted : memoryInstruction (.bitcastValue (.aggregate 32) .vector256) [.scalar (.aggregate (List.replicate 32 byte))] memory = .ok (memory, [.scalar (.v256 value)])) : - ∃ stack, step body (.memory (.bitcastValue (.aggregate 32) .vector256)) 0 args frame + ∃ stack, modelStep (some (.memory (.bitcastValue (.aggregate 32) .vector256))) body 0 frame [.scalar (.aggregate (List.replicate 32 byte))] memory = .ok (.next 1 stack frame memory) ∧ stack = [.scalar (.v256 value)] := by refine ⟨?_, ?_, ?_⟩ @@ -1101,12 +1154,12 @@ theorem bitcast_dispatch_keeps_value (body : CIL.Method) (args : List Value) (fr open CIL.Safety in /-- The same abstract result is retained for a fact at the static dispatch boundary. -/ -theorem bitcast_static_keeps_value (body : CIL.Method) (args : List Value) (frame : Frame) +theorem bitcast_static_keeps_value (body : CIL.Method) (frame : Frame) (memory : Memory) (byte : BitVec 8) (value : BitVec 256) (casted : staticInstruction body 0 (.bitcastValue (.aggregate 32) .vector256) [.scalar (.aggregate (List.replicate 32 byte))] memory = .ok (memory, [.scalar (.v256 value)])) : - ∃ stack, step body (.memory (.bitcastValue (.aggregate 32) .vector256)) 0 args frame + ∃ stack, modelStep (some (.memory (.bitcastValue (.aggregate 32) .vector256))) body 0 frame [.scalar (.aggregate (List.replicate 32 byte))] memory = .ok (.next 1 stack frame memory) ∧ stack = [.scalar (.v256 value)] := by refine ⟨?_, ?_, ?_⟩ @@ -1118,22 +1171,31 @@ theorem bitcast_static_keeps_value (body : CIL.Method) (args : List Value) (fram open CIL.Safety in /-- A conjunction-wrapped dispatch fact remains usable through the generic fallback. -/ -theorem store_from_wrapped_fact (body : CIL.Method) (args : List Value) (frame : Frame) +theorem store_from_wrapped_fact (body : CIL.Method) (frame : Frame) (bits : BitVec 256) (destination : Reference) (memory final : Memory) (stored : True ∧ memoryInstruction .store256 [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (final, [])) : - step body (.memory .store256) 2 args frame + modelStep (some (.memory .store256)) body 2 frame [.scalar (.v256 bits), .reference (.address destination)] memory = .ok (.next 3 [] frame final) := by cil_checked_step_normalized [stored] #print axioms store_from_wrapped_fact +private def byteToBoolean : CIL.Extraction.MethodRef := + let source := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.Byte" + let target := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.Boolean" + { owner := CIL.Extraction.unsafeOwner, name := name!"BitCast", hasThis := false, + genericArity := 2, methodArguments := [source, target], parameters := [source], + returnType := target } + /-- Compare two words and reinterpret the result as a Boolean. -/ @[cil_code] def comparedBoolean : CIL.Method := { - code := [.arg 0, .arg 1, .eq, .memory .bitcastByteBool, .ret] + code := [.ldarg 0, .ldarg 1, .ceq, .call byteToBoolean, .ret] locals := [], returnsValue := true } +cil_runtime_models comparedBoolean + open CIL.Safety in /-- A comparison result is a canonical Boolean whatever its condition, so the reinterpretation steps without a fact; an arbitrary int32 would still need its bound. -/ @@ -1148,7 +1210,7 @@ open CIL.Safety in /-- A Boolean cast of an unconstrained int32 keeps its canonical-range obligation. -/ theorem unconstrained_boolean_cast (body : CIL.Method) (frame : Frame) (bits : CIL.W32) (memory : Memory) (canonical : bits.toNat ≤ 1) : - step body (.memory .bitcastByteBool) 0 [] frame [.scalar (.i32 bits)] memory = + modelStep (some (.memory .bitcastByteBool)) body 0 frame [.scalar (.i32 bits)] memory = .ok (.next 1 [.scalar (.i32 bits)] frame memory) := by fail_if_success cil_checked_step [] cil_checked_step [canonical] @@ -1160,7 +1222,7 @@ open CIL.Safety in so its bound steps the Boolean cast of the widened value. -/ theorem widened_boolean_cast (body : CIL.Method) (frame : Frame) (field : CIL.W64) (memory : Memory) (canonical : field.toNat ≤ 1) : - step body (.memory .bitcastByteBool) 0 [] frame + modelStep (some (.memory .bitcastByteBool)) body 0 frame [.scalar (.i32 ((field.setWidth 8).setWidth 32))] memory = .ok (.next 1 [.scalar (.i32 ((field.setWidth 8).setWidth 32))] frame memory) := by fail_if_success cil_checked_step [] @@ -1169,14 +1231,15 @@ theorem widened_boolean_cast (body : CIL.Method) (frame : Frame) (field : CIL.W6 #print axioms widened_boolean_cast open CIL.Safety in -/-- A value known only to be numeric loads as an argument: the fact meets the checked value - test in the unfolded form stepping leaves it in. -/ +/-- A value known only to be numeric loads as a plain argument: the facts meet the argument + access and the checked value test in the unfolded form stepping leaves them in. -/ theorem opaque_numeric_argument (body : CIL.Method) (frame : Frame) (argument : CIL.Value) - (memory : Memory) (numeric : numericValue argument = true) : - step body (.arg 0) 0 [.scalar argument] frame [] memory = + (memory : Memory) (plain : body.argumentAccess 0 = .value none) + (numeric : numericValue argument = true) : + step body (.ldarg 0) 0 [.scalar argument] frame [] memory = .ok (.next 1 [.scalar argument] frame memory) := by - fail_if_success cil_checked_step [] - cil_checked_step [numeric] + fail_if_success cil_checked_step [numeric] + cil_checked_step [plain, numeric] #print axioms opaque_numeric_argument @@ -1184,11 +1247,11 @@ open CIL.Safety in /-- The same fact passes an opaque numeric value to a call. -/ theorem opaque_numeric_call (body : CIL.Method) (frame : Frame) (argument : CIL.Value) (memory : Memory) (numeric : numericValue argument = true) - (known : argument.hasUnspecified = false) : - step body (.call 1 1) 0 [] frame [.scalar argument] memory = + (known : argument.hasUnspecified = false) (linked : body.linkAt 0 = some (.direct 1)) : + step body (.call (Tests.callee 1)) 0 [] frame [.scalar argument] memory = .ok (.call 1 [.scalar argument] [] memory) := by - fail_if_success cil_checked_step [known] - cil_checked_step [numeric, known] + fail_if_success cil_checked_step [known, linked] + cil_checked_step [numeric, known, linked] #print axioms opaque_numeric_call @@ -1198,7 +1261,7 @@ open CIL.Safety in theorem inferred_read_load (memory : Memory) (limb : Reference) (word : CIL.W64) (loaded : read memory limb 8 1 = .ok (numberBytes word.toNat 8)) (body : CIL.Method) (frame : Frame) : - step body .load64 0 [] frame [.reference (.address limb)] memory = + step body (.ldind .i8) 0 [] frame [.reference (.address limb)] memory = .ok (.next 1 [.scalar (.i64 word)] frame memory) := by have restated := loaded clear loaded @@ -1206,4 +1269,906 @@ theorem inferred_read_load (memory : Memory) (limb : Reference) (word : CIL.W64) #print axioms inferred_read_load +open Lean in +/-- A step names its hypotheses from the first suffix that leaves every base unused, found in + one pass: suffixes in use are skipped, and spellings no step produces are not counted. -/ +example : True := by + run_tac do + let declare (context : LocalContext) (name : String) : MetaM LocalContext := + return context.mkLocalDecl (← mkFreshFVarId) (Name.mkSimple name) (mkConst ``Nat) + let bases := #["stored", "written"] + let mut context : LocalContext := {} + unless CIL.firstUnusedSuffix context bases == 0 do throwError "an empty context uses no suffix" + for name in + ["stored_1", "written_2", "stored_01", "stored_0", "other", "other_1", "stored1"] do + context ← declare context name + unless CIL.firstUnusedSuffix context bases == 0 do + throwError "names that no step produces made suffix 0 look used" + context ← declare context "written" + unless CIL.firstUnusedSuffix context bases == 3 do + throwError "suffixes 0, 1 and 2 are in use, so the first free suffix is 3" + trivial + +open Lean in +/-- Choosing a suffix against a context takes one pass over it, so four times as many names cost + about four times as many heartbeats; a probe per suffix costs sixteen times as many. -/ +example : True := by + run_tac do + let cost (names : Nat) : MetaM Nat := do + let mut context : LocalContext := {} + for index in [:names] do + let name := if index == 0 then "stored" else s!"stored_{index}" + context := context.mkLocalDecl (← mkFreshFVarId) (Name.mkSimple name) (mkConst ``Nat) + let started ← IO.getNumHeartbeats + let suffix ← IO.lazyPure fun _ => CIL.firstUnusedSuffix context #["stored", "written"] + unless suffix == names do throwError "expected suffix {names}, found {suffix}" + return (← IO.getNumHeartbeats) - started + let small ← cost 2000 + let large ← cost 8000 + unless large * 10 < small * 54 do + throwError "choosing a suffix cost {small} heartbeats at 2000 names and {large} at 8000" + trivial + +/-! ### The record of normal-form nodes + +A stepping driver skips the terms that earlier steps of the run left unchanged. These tests check +what the record keeps, when it is dropped, and that skipping never changes the goal a step +leaves. `cil.stepping.record` turns it off to give the goal that plain simplification reaches. -/ + +open CIL.Safety + +-- The record is used from the first step here; a driver uses it after a dozen steps. +set_option cil.stepping.warmup 0 + +open Lean Elab Tactic Meta in +/-- The goal that `steps` leaves, with the record of normal-form nodes on or off. The state is + restored afterwards. -/ +def steppedTarget (record : Bool) (steps : TSyntax `tactic) : TacticM Expr := do + let saved ← saveState + try + withOptions (fun options => cil.stepping.record.set options record) (evalTactic steps) + instantiateMVars (← getMainTarget) + finally saved.restore + +open Lean Elab Tactic Meta in +/-- Fails unless `steps` leaves the same goal with the record and without it. -/ +def sameGoal (steps : TSyntax `tactic) : TacticM Unit := do + let recorded ← steppedTarget true steps + let plain ← steppedTarget false steps + unless recorded == plain do + throwError "the record changed the goal:{indentExpr recorded}\n\ + against plain:{indentExpr plain}" + +open Lean Elab Tactic in +/-- Whether a record is active or kept. -/ +def recordIsLeft : TacticM Bool := do + let record ← getNormalNodes + return record.driven || record.active || !record.nodes.isEmpty || + record.equations.covered != 0 || record.equationLocals.isSome || + !record.contexts.isEmpty || !record.others.isEmpty + +open Lean Elab Tactic in +/-- Fails unless no record is active or kept. -/ +def recordIsEmpty : TacticM Unit := do + if ← recordIsLeft then throwError "a stepping record outlived its run" + +/-- Adds two arguments. -/ +@[cil_code] def recordBody : CIL.Method := { + code := [.ldarg 0, .ldarg 1, .add, .ret], locals := [], returnsValue := true } + +/-- The program of `recordBody`. -/ +@[cil_code] def recordProgram : CIL.Program := [recordBody] + +/-- Adds the arguments in the other order, so its goals differ from those of `recordBody`. -/ +@[cil_code] def otherBody : CIL.Method := { + code := [.ldarg 1, .ldarg 0, .add, .ret], locals := [], returnsValue := true } + +/-- The program of `otherBody`. -/ +@[cil_code] def otherProgram : CIL.Program := [otherBody] + +section +-- Declarations rather than theorems, elaborated on the main branch, keep what a failure leaves. +set_option Elab.async false + +-- A step that cannot fetch its instruction fails inside the run, which leaves no record. +#guard_msgs (drop all) in +theorem failedRun (memory : Memory) (frame : Frame) : + ∃ result, run recordProgram 5 7 0 [] frame [] memory = result := + ⟨_, by cil_checked_steps 1 []⟩ + +/-- The declaration after a failed run finds no record, and steps to the goal that plain + simplification gives for a different body. -/ +theorem runAfterFailure (memory : Memory) (frame : Frame) (left right : CIL.W64) : + run otherProgram 5 0 0 [.scalar (.i64 left), .scalar (.i64 right)] frame [] memory = + run otherProgram 3 0 2 [.scalar (.i64 left), .scalar (.i64 right)] frame + [.scalar (.i64 left), .scalar (.i64 right)] memory := by + run_tac recordIsEmpty + run_tac sameGoal (← `(tactic| cil_checked_steps 1 [])) + cil_checked_steps 2 [] + all_goals rfl + +-- A bare step that fails drops the record it started. +#guard_msgs (drop all) in +theorem failedBareStep (memory : Memory) (frame : Frame) : + ∃ result, run recordProgram 5 7 0 [] frame [] memory = result := + ⟨_, by cil_checked_run_step []⟩ + +/-- The declaration after a failed bare step finds no record. -/ +theorem runAfterBareFailure : True := by + run_tac recordIsEmpty + trivial + +/-- A `first` alternative that steps and then fails leaves no record to the next alternative. -/ +theorem backtracked (memory : Memory) (frame : Frame) (left right : CIL.W64) : + run recordProgram 5 0 0 [.scalar (.i64 left), .scalar (.i64 right)] frame [] memory = + run recordProgram 3 0 2 [.scalar (.i64 left), .scalar (.i64 right)] frame + [.scalar (.i64 right), .scalar (.i64 left)] memory := by + first + | (cil_checked_steps 2 []; fail "undone") + | (cil_checked_run_step []; fail "undone") + | run_tac recordIsEmpty + cil_checked_steps 2 [] + all_goals rfl + +open Lean Elab Tactic in +/-- A diagnostic that steps and restores its state leaves no record behind. -/ +theorem diagnosed (memory : Memory) (frame : Frame) (left right : CIL.W64) : + run recordProgram 5 0 0 [.scalar (.i64 left), .scalar (.i64 right)] frame [] memory = + run recordProgram 3 0 2 [.scalar (.i64 left), .scalar (.i64 right)] frame + [.scalar (.i64 right), .scalar (.i64 left)] memory := by + run_tac do + let saved ← saveState + evalTactic (← `(tactic| cil_checked_run_step [])) + saved.restore + run_tac recordIsEmpty + cil_checked_steps 2 [] + all_goals rfl + +open Lean Elab Tactic in +/-- A completed bare step leaves no step running, so a later simplification is plain. -/ +theorem completedBareStep (memory : Memory) (frame : Frame) (left right : CIL.W64) : + run recordProgram 5 0 0 [.scalar (.i64 left), .scalar (.i64 right)] frame [] memory = + run recordProgram 3 0 2 [.scalar (.i64 left), .scalar (.i64 right)] frame + [.scalar (.i64 right), .scalar (.i64 left)] memory := by + cil_checked_run_step [] + run_tac do unless !(← getNormalNodes).active do throwError "a step is still marked running" + cil_checked_steps 1 [] + all_goals rfl + +end + +set_option linter.unusedVariables false in +open Lean Elab Tactic Meta in +/-- Facts that print alike but state different things are keyed apart, and a record made for one + is not used for the other. A step that only adds hypotheses keeps the record, which a step + that rewrites one in place does not. -/ +example (one : (1 : Nat) = 1) (two : (2 : Nat) = 2) : True := by + run_tac withMainContext do + -- Unhygienic names, since a quoted `one` would carry a macro scope no hypothesis has. + let first ← factsKey #[mkIdent `one] + let second ← factsKey #[mkIdent `two] + unless first.isSome && second.isSome && first != second do + throwError "different facts were keyed alike" + let hole (name : Ident) : TacticM Term := do + Term.exprToSyntax (← elabTerm name none) + let holeFirst ← hole (mkIdent `one) + let holeSecond ← hole (mkIdent `two) + unless (← factsKey #[holeFirst]) != (← factsKey #[holeSecond]) do + throwError "facts that print alike were keyed alike" + withNormalRun do + let remember (facts : Array Term) : TacticM Unit := + withNormalNodes facts <| withMainContext <| liftM (recordNormalNodes "probe" #[mkNatLit 7]) + let recorded (facts : Array Term) : TacticM Bool := + withNormalNodes facts <| withMainContext <| liftM do + return (← normalNodesFor "probe").contains (mkNatLit 7) + remember #[holeFirst] + unless ← recorded #[holeFirst] do + throwError "a record was not kept between steps with the same facts" + if ← recorded #[holeSecond] then throwError "a record was used for different facts" + remember #[holeSecond] + unless ← recorded #[holeFirst] do + throwError "the record of the earlier facts was dropped when a step read other facts" + -- Steps that add hypotheses, as stores and calls do, keep what the run recorded. + for extra in [`extraOne, `extraTwo] do + evalTactic (← `(tactic| have $(mkIdent extra) : True := trivial)) + unless ← recorded #[holeSecond] do + throwError "the record was dropped when a step added the hypothesis {extra}" + unless ← recorded #[holeFirst] do + throwError "the record of the other facts was dropped when a step added {extra}" + -- A hypothesis outside the facts, rewritten in place, keeps its name and identity but not + -- what it states, and a simplification may use it. + let goal ← getMainGoal + let declaration ← withMainContext (getLocalDeclFromUserName `one) + let sum ← mkAppM ``HAdd.hAdd #[mkNatLit 1, mkNatLit 0] + replaceMainGoal [← goal.replaceLocalDeclDefEq declaration.fvarId + (← mkEq (mkNatLit 1) sum)] + if ← recorded #[holeSecond] then throwError "a record was used after a hypothesis changed" + recordIsEmpty + trivial + +set_option linter.unusedVariables false in +open Lean Elab Tactic Meta in +/-- Lemmas with implicit arguments and universe parameters among the facts are keyed, so the + run keeps the record and finds it again when the same lemmas elaborate anew. -/ +example : True := by + run_tac withMainContext do + let facts : Array Term := #[← `(List.length_append), ← `(List.map_map)] + withNormalRun do + let probed : TacticM Bool := withNormalNodes facts <| withMainContext <| liftM do + return (← normalNodesFor "probe").contains (mkNatLit 7) + withNormalNodes facts <| withMainContext do + unless (← getNormalNodes).active do + throwError "lemma facts with metavariables switched the record off" + liftM (recordNormalNodes "probe" #[mkNatLit 7]) + unless ← probed do throwError "the record was not found again for the same lemma facts" + recordIsEmpty + trivial + +open Lean Elab Tactic Meta in +/-- The goals `cil_try_simp_only` leaves for a sequence of statements run one after another in + one record, with the record on or off. -/ +def simpSequence (record : Bool) (statements : Array Term) : TacticM (Array Expr) := do + let saved ← saveState + let seen ← IO.mkRef (#[] : Array Expr) + try + withOptions (fun options => cil.stepping.record.set options record) do + withNormalRun do + for statement in statements do + let goal ← mkFreshExprMVar (← elabTerm statement none) + setGoals [goal.mvarId!] + withNormalNodes #[] do + withMainContext do + evalTactic (← `(tactic| cil_try_simp_only [Nat.reduceAdd, id_eq, Nat.add_zero])) + seen.modify (·.push (← instantiateMVars (← getMainTarget))) + finally saved.restore + seen.get + +set_option linter.unusedVariables false in +open Lean Elab Tactic Meta in +/-- Terms that appear first where simplification does not enter them and later where it does, + under a binder, as a dependent argument and as an instance argument, simplify as without the + record. -/ +example (p : Nat → Prop) : True := by + run_tac withMainContext do + let p := mkIdent `p + let statements : Array Term := #[← `($p (id (2 + 3))), + ← `($p ((fun x : Nat => x + (2 + 3)) 1)), + ← `($p (((2 + 3, 1 + 1) : Nat × Nat).1 + (2 + 3))), + ← `($p ((⟨2 + 3, by omega⟩ : { n : Nat // n < 10 }).val + (2 + 3 + 0))), + ← `($p (Nat.succ (2 + 3) + id (id (2 + 3))))] + let recorded ← simpSequence true statements + let plain ← simpSequence false statements + for (kept, reference) in recorded.zip plain do + unless kept == reference do + throwError "the record changed a simplification:{indentExpr kept}\nagainst:\ + {indentExpr reference}" + trivial + +open Lean Elab Tactic Meta in +/-- The goals `cil_literal_operands` leaves for a persistent operand it cannot evaluate and an + operand that appears later and can be evaluated, with the record on or off. -/ +def literalSequence (record : Bool) : TacticM (Array Expr) := do + let saved ← saveState + let seen ← IO.mkRef (#[] : Array Expr) + try + withOptions (fun options => cil.stepping.record.set options record) do + withNormalRun do + let (w, a, b) := (mkIdent `w, mkIdent `a, mkIdent `b) + for statement in #[← `(BitVec.signExtend $w (5#4) = $a), + ← `(BitVec.signExtend $w (5#4) = $a ∧ BitVec.setWidth 8 (3#4) = $b)] do + let goal ← mkFreshExprMVar (← elabTerm statement none) + setGoals [goal.mvarId!] + withNormalNodes #[] (withMainContext (evalTactic (← `(tactic| cil_literal_operands)))) + seen.modify (·.push (← instantiateMVars (← getMainTarget))) + finally saved.restore + seen.get + +set_option linter.unusedVariables false in +open Lean Elab Tactic Meta in +/-- A persistent literal operand that cannot be evaluated does not stop a later one from being + evaluated, and the goal is the one without the record. -/ +example (w : Nat) (a : BitVec w) (b : BitVec 8) : True := by + run_tac withMainContext do + let recorded ← literalSequence true + let plain ← literalSequence false + unless recorded == plain do throwError "the record changed the literal operands" + let [_, evaluated] := recorded.toList | throwError "expected two goals" + unless (evaluated.find? (·.isConstOf ``BitVec.setWidth)).isNone do + throwError "the later operand was not evaluated:{indentExpr evaluated}" + trivial + +set_option linter.unusedVariables false in +open Lean Elab Tactic Meta in +/-- The detector flags the operands the `cil_literal` simprocs evaluate and no others: a width + conversion to a symbolic width stays, and `BitVec.ofInt` of literals is evaluated. -/ +example (w : Nat) (a : BitVec w) (b : BitVec 8) : True := by + run_tac withMainContext do + let symbolic ← elabTerm (← `(BitVec.signExtend $(mkIdent `w) (5#4))) none + if closedLiteralOperand symbolic then + throwError "a conversion to a symbolic width was taken for a literal operand" + let statement ← elabTerm (← `(BitVec.ofInt 8 (-3) = $(mkIdent `b))) none + unless (statement.find? closedLiteralOperand).isSome do + throwError "BitVec.ofInt of literals was not found" + let goals ← getGoals + setGoals [(← mkFreshExprMVar statement).mvarId!] + evalTactic (← `(tactic| cil_literal_operands)) + let reduced ← instantiateMVars (← getMainTarget) + unless (reduced.find? (·.isConstOf ``BitVec.ofInt)).isNone do + throwError "BitVec.ofInt of literals was not evaluated:{indentExpr reduced}" + setGoals goals + trivial + +open Lean Elab Tactic Meta in +/-- A term whose tree doubles at each of sixty levels, as `.dup` then `.add` build it, has sixty + nodes as a DAG. Recording it visits each node once and costs heartbeats in proportion. -/ +example : True := by + run_tac do + let mut shared := mkNatLit 1 + for _ in [:60] do + shared := mkApp2 (mkConst ``Nat.add) shared shared + let (found, (_, clean)) := (cleanWalk (fun _ => false) {} shared).run ({}, #[]) + -- Two applications per level, and the nodes of the base: far fewer than the tree's 2^60. + unless !found && clean.size < 200 do + throwError "expected a node count linear in the depth, found {clean.size}" + withNormalRun do + withNormalNodes #[] do + recordNormalNodes "probe" clean + unless (← getNormalNodes).count == clean.size do + throwError "a node was recorded twice" + trivial + +set_option linter.unusedVariables false in +open Lean Elab Tactic Meta in +/-- Simplifying under a binder opens temporary free variables, and the terms that carry them can + never occur again, so the record keeps none of them while it keeps the closed terms. The goal + is the one that plain simplification gives. -/ +example (p : Nat → Prop) : True := by + run_tac withMainContext do + let statement ← `(∀ x : Nat, $(mkIdent `p) (x + 0) ∧ $(mkIdent `p) (2 + 3 + (x + 0))) + let context ← getLCtx + let mut goals := #[] + for record in [true, false] do + let goal ← mkFreshExprMVar (← elabTerm statement none) + let saved ← getGoals + setGoals [goal.mvarId!] + withOptions (fun options => cil.stepping.record.set options record) do + withNormalRun do + withNormalNodes #[] do + withMainContext (evalTactic (← `(tactic| + cil_try_simp_only [Nat.add_zero, Nat.reduceAdd]))) + if record then + let kept ← getNormalNodes + let mut recorded := 0 + for (_, nodes) in kept.nodes do + for node in nodes.toList do + recorded := recorded + 1 + if node.hasAnyFVar (!context.contains ·) then + throwError "a term with a temporary free variable was recorded:{indentExpr node}" + if recorded == 0 then throwError "no node was recorded, so the test checks nothing" + goals := goals.push (← instantiateMVars (← getMainTarget)) + setGoals saved + unless goals[0]! == goals[1]! do + throwError "the record changed the goal:{indentExpr goals[0]!}\n\ + against:{indentExpr goals[1]!}" + trivial + +open Lean Elab Tactic Meta in +/-- Every goal that `steps` leaves, with its hypotheses, as printed text, with the record on or + off. Printing makes the comparison independent of the identifiers of fresh variables. The + state is restored afterwards. -/ +def steppedGoals (record : Bool) (steps : TSyntax `tactic) : TacticM (Array String) := do + let saved ← saveState + try + withOptions (fun options => cil.stepping.record.set options record) (evalTactic steps) + let mut printed := #[] + for goal in ← getGoals do + printed := printed.push (toString (← withOptions (·.setBool `pp.mvars false) (ppGoal goal))) + return printed + finally saved.restore + +open Lean Elab Tactic in +/-- Fails unless `steps` leaves the same goals, hypotheses included, with the record and without + it. Unless `mayClose`, it also fails when the steps close every goal, since the comparison + would check nothing. -/ +def sameGoals (steps : TSyntax `tactic) (mayClose := false) : TacticM Unit := do + let recorded ← steppedGoals true steps + let plain ← steppedGoals false steps + if recorded.isEmpty && !mayClose then + throwError "the steps closed every goal, so the test checks nothing" + unless recorded == plain do + throwError "the record changed the goals:\n{recorded}\nagainst plain:\n{plain}" + +/-- A body the loop tactics walk: two operands added, ending in a `nop`. -/ +@[cil_code] def loopRecordBody : CIL.Method := { + code := [.ldarg 0, .ldarg 1, .add, .nop], locals := [], returnsValue := true } + +/-- A body that stores an argument into a numeric local and loads it back. -/ +@[cil_code] def snapshotRecordBody : CIL.Method := { + code := [.ldarg 0, .stloc 0, .ldloc 0, .ret], locals := [.i64 0], + localKinds := [.word64], returnsValue := true } + +/-- Contract stepping leaves the goals that plain simplification gives. -/ +example (memory : Memory) (left right : CIL.W64) : + WP (Execution (fun fuel => invoke [recordBody] fuel 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory)) + (fun (final, values) => final = memory ∧ values = [.scalar (.i64 (left + right))]) := by + run_tac sameGoals (← `(tactic| cil_contract_steps 2 [])) + cil_contract_steps [] + exact ⟨rfl, rfl⟩ + +open Lean Elab Tactic Meta in +/-- Raise stepping leaves the goals that plain simplification gives. -/ +example (memory : Memory) (left right : CIL.W64) (exception : CIL.ExceptionType) : True := by + run_tac withMainContext do + let original ← getGoals + let statement ← `(Raises recordProgram 0 [.scalar (.i64 $(mkIdent `left)), + .scalar (.i64 $(mkIdent `right))] $(mkIdent `memory) $(mkIdent `exception)) + setGoals [(← mkFreshExprMVar (← elabTerm statement none)).mvarId!] + sameGoals (← `(tactic| cil_raise_steps [])) + setGoals original + trivial + +/-- Loop stepping leaves the goals that plain simplification gives. -/ +example (memory : Memory) (left right : CIL.W64) (frame : Frame) : + LoopReaches [loopRecordBody] 0 [.scalar (.i64 left), .scalar (.i64 right)] + (LoopState.mk 0 frame [] memory) + (LoopState.mk 3 frame [.scalar (.i64 (left + right))] memory) := by + run_tac sameGoals (← `(tactic| cil_loop_steps)) (mayClose := true) + cil_loop_steps + +/-- Snapshot stepping leaves the goals that plain simplification gives, including the + hypotheses a store adds and clears. -/ +example (memory : Memory) (slots : List LocalSlot) (boundary : Nat) + (homes : CompleteWritableHomes memory boundary snapshotRecordBody.localKinds slots) + (inputs outputs : List ArgumentView) (valid : ValidCall memory inputs outputs) + (word : BitVec 64) : + WP (Execution (fun fuel => run [snapshotRecordBody] fuel 0 0 [.scalar (.i64 word)] + ⟨0, slots, [], 0⟩ [] memory)) (fun result => result.2 = [.scalar (.i64 word)]) := by + have formed := valid.wellFormed + have preserved := MemoryBelow.refl boundary memory + have authority := (MemoryBelow.refl memory.nextIdentity memory).accessBelow + have snapshots := LocalSnapshots.empty memory slots + run_tac sameGoals (← `(tactic| cil_contract_steps (snapshots := snapshots) 2 [])) + cil_contract_steps (snapshots := snapshots) [] + exact rfl + +open Lean Elab Tactic Meta in +/-- A loop walk restates a caller buffer fact at a memory the walk introduced and clears the one + it restated, and keeps a fact at any other memory, which the restatement does not give back, + even one the caller names `stored` as the walk names its own. -/ +example (produced stored entry : Memory) (base : Reference) (x : BitVec 32) + (producedFact : Contents produced base 4 [x]) (namedFact : Contents stored base 4 [x]) + (callerFact : Contents entry base 4 [x]) : True := by + run_tac withMainContext do + let count (memory : Name) : TacticM Nat := withMainContext do + let memory ← elabTerm (mkIdent memory) none + return (← getLCtx).foldl (init := 0) fun found declaration => + if declaration.type.isAppOf ``Contents && + (declaration.type.getAppArgs[0]? == some memory) then found + 1 else found + let carry : LoopCarried → Ident → TacticM (Option Term) := fun _ fact => pure (some fact) + recordWalkVariable (← elabTerm (mkIdent `produced) none).fvarId! + for memory in [`produced, `stored, `entry] do + loopCarryFacts (← elabTerm (mkIdent memory) none) carry + unless (← count `produced) == 1 do + throwError "the fact at the walk's memory was not replaced" + unless (← count `stored) == 2 do throwError "the fact at the memory named stored was cleared" + unless (← count `entry) == 2 do throwError "the caller's fact was cleared" + trivial + +open Lean Elab Tactic in +/-- Closes the goal with `cil_lookup_assumption` both outside a stepping driver, where it scans the + local context, and in a step of one, where it reads the driver's equation index; either may + find an equation the other misses, so each test runs both. -/ +elab "cil_lookup_both_ways" : tactic => do + let saved ← saveState + evalTactic (← `(tactic| cil_lookup_assumption)) + saved.restore + withNormalRun (withNormalNodes #[] (evalTactic (← `(tactic| cil_lookup_assumption)))) + +/-- The lookup it is given, under a name that reducible unfolding removes. -/ +abbrev sameLookup (found : Option Nat) : Option Nat := found + +-- A lookup whose left side unfolds at reducible transparency to a variable is stated by an +-- equation about that variable, although the lookup is written with a constant head. +example (found : Option Nat) (stated : found = some 3) : sameLookup found = some 3 := by + cil_lookup_both_ways + +/-- A list that reducible unfolding spells out. -/ +abbrev lookupList : List Nat := [1, 2] + +/-- The first entry of a list, under a name that reducible unfolding removes. -/ +abbrev firstEntry (entries : List Nat) : Option Nat := entries[0]? + +/-- Twice a natural number, which reducible unfolding turns into a product. -/ +abbrev twice (count : Nat) : Nat := 2 * count + +/-- Another name for natural numbers, which reducible unfolding removes. -/ +abbrev Position := Nat + +-- Both lookups find every hypothesis that unification may equate with the lookup: an +-- abbreviation of a list literal meets an equation about the literal, and arithmetic on natural +-- numbers meets its result whichever side writes the arithmetic. +example (stated : ([1, 2] : List Nat)[0]? = some 1) : lookupList[0]? = some 1 := by + cil_lookup_both_ways +example (entries : List Nat) (stated : entries[Nat.succ 1]? = some 3) : + entries[Nat.add 1 1]? = some 3 := by + cil_lookup_both_ways +example (entries : List Nat) (stated : entries[2 * 1]? = some 3) : entries[2]? = some 3 := by + cil_lookup_both_ways +example (entries : List Nat) (stated : entries[2]? = some 3) : entries[2 * 1]? = some 3 := by + cil_lookup_both_ways +-- A local definition unfolds during unification, in a lookup and in a hypothesis alike, and so +-- does the arithmetic it names. +example (entries : List Nat) (stated : entries[1]? = some 3) : + let position := 1; entries[position]? = some 3 := by + intro position + cil_lookup_both_ways +example (entries : List Nat) : + let found := entries[1]?; found = some 3 → entries[1]? = some 3 := by + intro found stated + cil_lookup_both_ways +example (entries : List Nat) (stated : entries[2]? = some 3) : + let position := 2 * 1; entries[position]? = some 3 := by + intro position + cil_lookup_both_ways +example (entries : List Nat) : + let position := 2 * 1; entries[position]? = some 3 → entries[2]? = some 3 := by + intro position stated + cil_lookup_both_ways +-- A reducible definition unfolds on either side, and so does the arithmetic it names or a type +-- it renames. +example (entries : List Nat) (stated : entries[twice 1]? = some 3) : entries[2]? = some 3 := by + cil_lookup_both_ways +example (entries : List Nat) (stated : entries[2]? = some 3) : entries[twice 1]? = some 3 := by + cil_lookup_both_ways +example (entries : List Nat) (stated : entries[((2 : Position) * 1 : Position)]? = some 3) : + entries[2]? = some 3 := by + cil_lookup_both_ways +example (entries : List Nat) (stated : firstEntry entries = some 3) : entries[0]? = some 3 := by + cil_lookup_both_ways +example (entries : List Nat) (stated : entries[0]? = some 3) : firstEntry entries = some 3 := by + cil_lookup_both_ways +-- Unification matches a pair against its projections and any two values of a type with one value. +example (pair : Nat × Nat) (read : Nat × Nat → Option Nat) + (stated : read (pair.1, pair.2) = some 1) : read pair = some 1 := by + cil_lookup_both_ways +example (pair : Nat × Nat) (read : Nat × Nat → Option Nat) (stated : read pair = some 1) : + read (pair.1, pair.2) = some 1 := by + cil_lookup_both_ways +example (first second : Unit) (read : Unit → Option Nat) (stated : read first = some 1) : + read second = some 1 := by + cil_lookup_both_ways +example (first : Unit) (read : Unit → Option Nat) (stated : read () = some 1) : + read first = some 1 := by + cil_lookup_both_ways +-- A quantified lookup is closed by the quantified hypothesis that states it. +example (entries : List Nat) (stated : ∀ position : Nat, entries[position]? = some 1) : + ∀ position : Nat, entries[position]? = some 1 := by + cil_lookup_both_ways + +open Lean Elab Tactic Meta in +-- A hypothesis whose left side holds a metavariable that is assigned after a step indexed it +-- still states the lookup at the next step. +example (entries : List Nat) : entries = entries := by + run_tac withMainContext do + let list ← elabTerm (mkIdent `entries) none + let position ← mkFreshExprSyntheticOpaqueMVar (mkConst ``Nat) + let found ← mkAppM ``Option.some #[mkNatLit 1] + let stated ← mkEq (← mkAppM ``GetElem?.getElem? #[list, position]) found + let wanted ← mkEq (← mkAppM ``GetElem?.getElem? #[list, mkNatLit 2]) found + let (_, lookup) ← (← mkFreshExprMVar (← mkArrow stated wanted)).mvarId!.intro1P + let saved ← getGoals + setGoals [lookup] + withNormalRun do + withNormalNodes #[] (pure ()) + position.mvarId!.assign (mkNatLit 2) + withNormalNodes #[] (evalTactic (← `(tactic| cil_lookup_assumption))) + unless (← getGoals).isEmpty do throwError "the lookup was not closed" + setGoals saved + rfl + +/-- A running sum of fifteen additions, long enough for the stepping record to be used. -/ +@[cil_code] def runningSum : CIL.Method := { + locals := [], returnsValue := true, + code := [.ldarg 0, .ldarg 1, .add, .ldarg 1, .add, .ldarg 1, .add, .ldarg 1, .add, + .ldarg 1, .add, .ldarg 1, .add, .ldarg 1, .add, .ldarg 1, .add, .ldarg 1, .add, + .ldarg 1, .add, .ldarg 1, .add, .ldarg 1, .add, .ldarg 1, .add, .ldarg 1, .add, + .ldarg 1, .add, .ret] } + +open Lean Elab Tactic Meta in +/-- Run `steps` and fail when its comparisons of local contexts read more positions than one + whole-context comparison per instruction of `runningSum`. Comparing the whole context at each + read of the record costs no heartbeats, so only this count shows it. -/ +elab "cil_reads_at_most_one_context_per_instruction " steps:tactic : tactic => do + let size := (← withMainContext getLCtx).numIndices + let before ← contextReads + evalTactic steps + let reads := (← contextReads) - before + let limit := size * runningSum.code.length + unless reads <= limit do + throwError "the steps read {reads} context positions; at most {limit} were expected" + +-- A step whose goal keeps the local context of the record reads none of its positions, however +-- many hypotheses are in scope; checking each read of the record against the whole context +-- read fifteen to twenty contexts per instruction. +theorem running_sum_reads (memory : Memory) (left right : CIL.W64) (first : right + 1 = right + 1) + (second : right + 2 = right + 2) (third : right + 3 = right + 3) : + invoke [runningSum] 32 0 [.scalar (.i64 left), .scalar (.i64 right)] memory = + .ok (memory, [.scalar (.i64 (left + right + right + right + right + right + right + right + + right + right + right + right + right + right + right + right))]) := by + cil_reads_at_most_one_context_per_instruction cil_checked_steps + +#print axioms running_sum_reads + +theorem running_sum_contract_reads (memory : Memory) (left right : CIL.W64) + (first : right + 1 = right + 1) (second : right + 2 = right + 2) : + WP (Execution (fun fuel => invoke [runningSum] fuel 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory)) + (fun (final, values) => final = memory ∧ values = [.scalar (.i64 (left + right + right + + right + right + right + right + right + right + right + right + right + right + right + + right + right))]) := by + cil_reads_at_most_one_context_per_instruction + (cil_contract_steps []; exact And.intro rfl rfl) + +#print axioms running_sum_contract_reads + +open Lean Elab Tactic Meta in +/-- A step that runs out of heartbeats leaves no record behind. The exception is a runtime + exception, which `try` does not catch, so the cleanup must not depend on a handler. -/ +example (memory : Memory) (frame : Frame) (left right : CIL.W64) : + run recordProgram 5 0 0 [.scalar (.i64 left), .scalar (.i64 right)] frame [] memory = + run recordProgram 3 0 2 [.scalar (.i64 left), .scalar (.i64 right)] frame + [.scalar (.i64 right), .scalar (.i64 left)] memory := by + run_tac do + let saved ← saveState + -- The limit is the cost of preparing the facts plus a little, so it is the step that runs out. + let started ← IO.getNumHeartbeats + evalTactic (← `(tactic| cil_checked_steps 0 [])) + let preparation := (← IO.getNumHeartbeats) - started + saved.restore + -- A handler restores the state, which would empty the record, so it is read as the + -- exception passes. + let left ← IO.mkRef false + let exhausted ← tryCatchRuntimeEx + (Core.withCurrHeartbeats <| withTheReader Core.Context + (fun context => { context with maxHeartbeats := preparation + 20000 }) do + tryFinally (do evalTactic (← `(tactic| cil_checked_steps 2 [])); pure false) + (do left.set (← recordIsLeft))) + (fun exception => if exception.isRuntime then pure true else throw exception) + unless exhausted do + throwError "the steps did not run out of heartbeats, so the test checks nothing" + if ← left.get then throwError "a stepping record outlived the step that ran out of heartbeats" + saved.restore + cil_checked_steps 2 [] + all_goals rfl + +open Lean Elab Tactic Meta in +/-- A driver uses the record from its thirteenth step: twelve steps run plainly and build no key, + and the thirteenth builds one and records. -/ +example : True := by + run_tac do + withOptions (fun options => cil.stepping.warmup.set options 12) do + withNormalRun do + let step : TacticM Bool := withNormalNodes #[] do return (← getNormalNodes).active + for count in [:12] do + if ← step then throwError "step {count + 1} used the record during the warm-up" + unless (← getNormalNodes).key.isNone do + throwError "step {count + 1} built a key during the warm-up" + unless ← step do throwError "step 13 did not use the record" + unless (← getNormalNodes).key.isSome do throwError "step 13 built no key" + trivial + +open Lean Elab Tactic Meta in +/-- No `cil_step` equation about `CIL.Safety.step` is an rfl lemma. Simp applies an rfl lemma + without a proof term, and the kernel then reduces `step` again at every use. -/ +example : True := by + run_tac do + let some extension ← getSimpExtension? `cil_step | throwError "cil_step is not a simp set" + let mut examined := 0 + let mut rflEquations := #[] + for origin in (← extension.getTheorems).lemmaNames.toList do + let .decl name .. := origin | continue + let aboutStep ← forallTelescope (← getConstInfo name).type fun _ conclusion => + pure <| conclusion.eq?.any fun (_, lhs, _) => lhs.getAppFn.isConstOf ``CIL.Safety.step + unless aboutStep do continue + examined := examined + 1 + if (← isRflTheorem name) || (← isBackwardRflTheorem name) then + rflEquations := rflEquations.push name + if examined == 0 then throwError "cil_step holds no equation about CIL.Safety.step" + unless rflEquations.isEmpty do + throwError "cil_step equations about CIL.Safety.step proved by rfl: {rflEquations.toList}" + trivial + +/-- A constructor whose explicit link table keys only its later call: the `System.Object` + constructor call at pc 1 states no link and runs its runtime model. -/ +def modelledConstructor : CIL.Method := + { locals := [], returnsValue := true, links := [(3, .direct 1)], + code := [.ldarg 0, .call CIL.Extraction.objectConstructorMethod {}, .ldarg 1, + .call (Tests.callee 1) {}, .ret] } +cil_instruction_equations modelledConstructor +cil_runtime_models modelledConstructor +/-- The method the linked call enters; it returns its argument. -/ +def passThrough : CIL.Method := { locals := [], returnsValue := true, code := [.ldarg 0, .ret] } +cil_instruction_equations passThrough + +/-- The model call consumes the receiver and the linked call returns the second argument. -/ +theorem modelled_then_linked (memory : Memory) (object : Reference) (word : CIL.W64) + (formed : formObject memory (.address object) = .ok (.object (.address object))) : + invoke [modelledConstructor, passThrough] 10 0 + [.object (.address object), .scalar (.i64 word)] memory = + .ok (memory, [.scalar (.i64 word)]) := by + cil_checked_steps [formed] +#print axioms modelled_then_linked + +/-- The unlinked constructor consumes its receiver and preserves the caller's frame and memory. -/ +theorem contract_steps_modelled_call (program : CIL.Program) + (caller : program[0]? = some modelledConstructor) (frame : Frame) (memory : Memory) + (object : Reference) (args : List Value) (post : Memory → List Value → Prop) + (continuation : ∃ fuel final returned, + run program fuel 0 2 args frame [] memory = .ok (final, returned) ∧ post final returned) : + ∃ fuel final returned, + run program fuel 0 1 args frame [.object (.address object)] memory = + .ok (final, returned) ∧ post final returned := by + cil_contract_steps 1 [caller] + exact continuation +#print axioms contract_steps_modelled_call + +/-- `IntPtr.get_Size`, a call the runtime table models. -/ +def nativeSizeGetter : CIL.Extraction.MethodRef := + { owner := .atom CIL.Extraction.runtimeScope "System.IntPtr", name := name!"get_Size", + hasThis := false, genericArity := 0, parameters := [], + returnType := .atom CIL.Extraction.runtimeScope "System.Int32" } + +/-- Two calls of the modelled size getter. Only the second has a link, which enters method 1. -/ +def linkedModelCalls : CIL.Method := + { locals := [], returnsValue := true, links := [(2, .direct 1)], + code := [.call nativeSizeGetter {}, .pop, .call nativeSizeGetter {}, .ret] } +cil_instruction_equations linkedModelCalls +cil_runtime_models linkedModelCalls +/-- The method the linked getter call enters; it returns 4, where the model pushes 8. -/ +def fourWord : CIL.Method := { locals := [], returnsValue := true, code := [.ldc_i4 4, .ret] } +cil_instruction_equations fourWord + +/-- A linked site enters its callee although its method has a model, so the run returns the + callee's 4 rather than the model's native size. -/ +theorem linked_model_site (memory : Memory) : + invoke [linkedModelCalls, fourWord] 10 0 [] memory = .ok (memory, [.scalar (.i32 4)]) := by + cil_checked_steps [] +#print axioms linked_model_site + +/-- The callee of the linked getter call returns 4 and touches no memory. -/ +def fourContract : MethodContract Unit CIL.W32 where + args _ := [] + results value := [.scalar (.i32 value)] + pre _ _ := True + footprint _ := [] + post _ _ _ value := value = 4 + +/-- Contract stepping runs the unlinked call's model and crosses the linked call with its callee's + contract, so the result is the contract's 4 rather than the model's native size. -/ +theorem contract_steps_linked_model_site (program : CIL.Program) (memory : Memory) + (caller : program[0]? = some linkedModelCalls) (holds : fourContract.Holds program 1) : + ∃ fuel final returned, + run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (final, returned) ∧ + returned = [.scalar (.i32 4)] := by + cil_contract_steps [caller, holds, fourContract] + obtain rfl := call.post + rfl +#print axioms contract_steps_linked_model_site + +-- The stepper enters the model call by its one equation, not by `step_call_eq`, whose rewrite +-- under the call's continuation the kernel rechecks at every call. +open Lean in +run_meta do + let info ← getConstInfo ``modelled_then_linked + let some proof := info.value? (allowOpaque := true) | throwError "no proof" + let used := proof.getUsedConstants + for name in [``CIL.Safety.step_call_model, ``CIL.Safety.step_call_direct] do + unless used.contains name do throwError "modelled_then_linked does not use {name}" + +/-- A return whose list spine depends on the flag. -/ +def conditionalReturn (flag : Bool) : CIL.Method := + { returnsValue := false, locals := [], code := if flag then [.ret, .nop] else [.ret] } + +/-- The same body with the return visible at the head. -/ +def leadingReturn (flag : Bool) : CIL.Method := + { returnsValue := false, locals := [], code := .ret :: (if flag then [.nop] else []) } + +/-- A program with one conditional body. -/ +def returnProgram (flag : Bool) : CIL.Program := [conditionalReturn flag] + +/-- The generated lookup finds the stored body. -/ +@[cil_code] theorem returnProgram_method0 (flag : Bool) : + (returnProgram flag)[0]? = some (conditionalReturn flag) := rfl +#print axioms returnProgram_method0 + +/-- The preferred lookup exposes the return for both values of the flag. -/ +theorem returnProgram_leading (flag : Bool) : + (returnProgram flag)[0]? = some (leadingReturn flag) := by cases flag <;> rfl +#print axioms returnProgram_leading + +/-- A caller can supply the lookup that exposes the return. -/ +theorem supplied_return_steps (flag : Bool) (memory : Memory) : + invoke (returnProgram flag) 1 0 [] memory = .ok (memory, []) := by + cil_checked_steps [returnProgram_leading flag] +#print axioms supplied_return_steps + +/-- The preferred lookup can describe the indexing function before its position is supplied. -/ +theorem returnProgram_lookup (flag : Bool) : + (GetElem?.getElem? (returnProgram flag) : Nat → Option CIL.Method) = + (fun index => match index with | 0 => some (leadingReturn flag) | _ + 1 => none) := by + funext index + cases index with + | zero => cases flag <;> rfl + | succ => rfl +#print axioms returnProgram_lookup + +section +attribute [local cil_code high] returnProgram_lookup + +/-- Stepping uses the partially applied lookup at method zero. -/ +theorem partial_lookup_return_steps (flag : Bool) (memory : Memory) : + invoke (returnProgram flag) 1 0 [] memory = .ok (memory, []) := by + cil_checked_steps +#print axioms partial_lookup_return_steps +end + +attribute [cil_code high] returnProgram_leading + +/-- Stepping honors the higher-priority lookup with the flag still symbolic. -/ +theorem conditional_return_steps (flag : Bool) (memory : Memory) : + invoke (returnProgram flag) 1 0 [] memory = .ok (memory, []) := by + cil_checked_steps +#print axioms conditional_return_steps + +namespace MethodLookup + +/-- The immediate return shared by both branches of the program. -/ +def body : CIL.Method := { returnsValue := false, locals := [], code := [.ret] } + +/-- Both program lengths have the same first method. -/ +def prog (flag : Bool) : CIL.Program := if flag then [body] else [body, body] + +/-- The first method is available before the branch is known. -/ +@[cil_code] theorem prog_method0 (flag : Bool) : (prog flag)[0]? = some body := by + cases flag <;> rfl +#print axioms prog_method0 + +example (flag : Bool) : (prog flag)[0]? = some body := by cil_lookup [prog] + +example (flag : Bool) (memory : Memory) : + invoke (prog flag) 1 0 [] memory = .ok (memory, []) := by cil_checked_steps [prog] + +section +attribute [local cil_code] prog + +example (flag : Bool) : (prog flag)[0]? = some body := by cil_lookup [] + +example (flag : Bool) (memory : Memory) : + invoke (prog flag) 1 0 [] memory = .ok (memory, []) := by cil_checked_steps +end + +/-- A program with no method at position zero. -/ +def vacant : CIL.Program := [] + +/-- The lookup equation can certify an absent method. -/ +@[cil_code] theorem vacant_method0 : vacant[0]? = none := rfl +#print axioms vacant_method0 + +example : vacant[0]? = none := by cil_lookup_equation + +/-- The named result of a successful method lookup. -/ +def hit : Option CIL.Method := some body + +/-- A program with a named lookup result. -/ +def singleton : CIL.Program := [body] + +/-- The generated equation can state its result by name. -/ +@[cil_code] theorem singleton_method0 : singleton[0]? = hit := rfl +#print axioms singleton_method0 + +example : singleton[0]? = hit := by cil_lookup_equation + +end MethodLookup + end Tests.Automation diff --git a/Tests/BatchInputs.cs b/Tests/BatchInputs.cs index ed7bb0313..2b531cf50 100644 --- a/Tests/BatchInputs.cs +++ b/Tests/BatchInputs.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs var directory = At("artifacts/BatchInputs/" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(directory); diff --git a/Tests/BatchVerification.cs b/Tests/BatchVerification.cs index 53b17b4d3..480e9db0d 100644 --- a/Tests/BatchVerification.cs +++ b/Tests/BatchVerification.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Diagnostics; @@ -166,6 +171,36 @@ set_option debug.skipKernelTC false if (!rejected || File.Exists(Path.Combine(requests[0].Output, "verified.json"))) throw new InvalidOperationException("Unchecked helper bypassed batch kernel replay"); Console.WriteLine("PASS unchecked transitive helper rejected by batch kernel replay"); + + // A genuine counterexample about the empty program refutes nothing in the fixture's extraction. + File.WriteAllText(requests[0].Proof, """ + import Extracted + import CIL.Safety.MethodContract + + open CIL.Safety + + /-- Promises a return from any memory. -/ + def ExampleProof.returns : MethodContract Unit := + { args := fun _ => [], pre := fun _ _ => True, footprint := fun _ => [], post := fun _ _ _ _ => True } + + /-- The empty program has no method zero, so a call to it faults. -/ + theorem ExampleProof.correctness : ExampleProof.returns.Refuted [] 0 := + .of_fault (input := ()) (initial := Memory.empty) (observed := 1) + (fault := { method := 0, instruction := 0, fault := .invalidState }) trivial (by decide) rfl + """); + rejected = false; + try + { + verification.VerifyGeneratedBatch(assembly, "Examples", [requests[0]]); + } + catch (InvalidOperationException error) + when (error.Message.Contains("a refutation states BatchCase0.Extracted.program", StringComparison.Ordinal)) + { + rejected = true; + } + if (!rejected || File.Exists(Path.Combine(requests[0].Output, "verified.json"))) + throw new InvalidOperationException("A batch certificate carried a refutation of another program"); + Console.WriteLine("PASS a batch refuses a counterexample about another program"); } finally { diff --git a/Tests/BinarySpans.cs b/Tests/BinarySpans.cs index ca139eab3..a15103280 100644 --- a/Tests/BinarySpans.cs +++ b/Tests/BinarySpans.cs @@ -2,12 +2,16 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs -#:include RejectedEvidence.cs +#:include ModelOperandChecks.cs using System.Reflection; -using System.Text.RegularExpressions; using Mono.Cecil; using var verification = new Program(); @@ -17,27 +21,7 @@ using var metadata = ModuleDefinition.ReadModule(assembly); var runtime = Assembly.Load(File.ReadAllBytes(assembly)).GetType("Examples.BinarySpans")!; var queue = new GeneratedQueue(verification, assembly); -var mutations = new (string Name, string Helper, string Before, string After)[] -{ - ("ReadBig", "ReadInt16BigEndian", ".binaryRead 2 true true false", ".binaryRead 2 false true false"), - ("ReadBig", "ReadIntPtrBigEndian", - "returnType := .atom Extraction.runtimeScope \"System.IntPtr\", parameterTypes :=", - "returnType := .atom Extraction.runtimeScope \"System.UIntPtr\", parameterTypes :="), - ("ReadLittle", "ReadIntPtrLittleEndian", "ReadIntPtrLittleEndian", "ReadInt32LittleEndian"), - ("TryReadBig", "TryReadUIntPtrBigEndian", "TryReadUIntPtrBigEndian", "TryReadIntPtrBigEndian"), - ("ReadBig", "ReadInt16BigEndian", ".binaryRead 2 true true false", ".binaryRead 2 true false false"), - ("ReadLittle", "ReadUInt64LittleEndian", ".binaryRead 8 false false false", ".binaryRead 4 false false false"), - ("TryReadBig", "TryReadInt16BigEndian", ".binaryRead 2 true true true", ".binaryRead 2 true true false"), - ("WriteBig", "WriteInt16BigEndian", ".binaryWrite 2 true false", ".binaryWrite 2 false false"), - ("TryWriteLittle", "TryWriteInt16LittleEndian", ".binaryWrite 2 false true", ".binaryWrite 2 false false") -}; var methods = metadata.GetType("Examples.BinarySpans").Methods.Where(method => method.IsPublic).ToArray(); -var sites = mutations.Select(mutation => (mutation.Name, mutation.Before, mutation.After, - Pc: methods.Single(method => method.Name == mutation.Name).Body.Instructions - .Select((instruction, index) => (instruction, index)) - .Single(item => item.instruction.Operand is MethodReference call && call.Name == mutation.Helper).index)) - .ToArray(); -// Controls project the full certificate; mutations independently reduce the selected call only. foreach (var method in methods) { var name = method.Name; @@ -69,7 +53,7 @@ { foreach (var (length, storage, fault) in new[] { - (1, "memory", ".thrown \"System.ArgumentOutOfRangeException\""), + (1, "memory", ".thrown .argumentOutOfRange"), (8, "uninitialized", ".memory .uninitialized _ _") }) claims.Add("(match CIL.Safety.invoke Extracted.program 512 0 " + @@ -81,11 +65,6 @@ "[.bufferSpan ⟨.address ⟨0, 1⟩, 28, 1, true⟩] readOnlyMemory with " + "| .error ⟨_, _, .memory .unwritable _ _⟩ => true | _ => false) = true"); File.WriteAllText(proof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + """ - /-- Successful observations identify the checked execution result. -/ - private theorem result_of_toOption {error value : Type} {result : Except error value} - {expected : value} (observed : result.toOption = some expected) : result = .ok expected := by - cases result <;> simp_all [Except.toOption] - #print axioms result_of_toOption /-- Misaligned caller storage exposes signed high bits and neighboring sentinel bytes. -/ def memory : CIL.Safety.Memory := { CIL.Safety.Memory.empty with allocations := fun identity => if identity == 0 then @@ -101,47 +80,33 @@ views := [⟨0, 0, 48, true, false⟩] } /-- All admitted integer overloads agree with runtime results, including short try operations. -/ """ + "\ntheorem correctness :\n" + string.Join(" ∧\n", claims) + - " := by\n repeat constructor\n all_goals first | (apply result_of_toOption; decide +kernel) | decide +kernel\n#print axioms correctness\nend ExampleProof\n"); - foreach (var pc in sites.Where(site => site.Name == name).Select(site => site.Pc).Distinct()) - { - // The real method's certificate proves this exact raw/emitted pair is reachable and checked. - File.AppendAllText(proof, $$""" - - namespace Extracted - open CIL - /-- The original certificate validates this reachable endian call and its emitted operation. -/ - theorem endianSite{{pc}} : {{SiteType(pc)}} := by - refine ⟨rfl, rfl, ?_⟩ - simpa only [Extraction.all3, Bool.and_true] using - (Extraction.all3_at (k := {{pc}}) (a := {{RawSite(pc)}}) (b := true) - (c := {{EmittedSite(pc)}}) method0_unsafe_validated.2.2 rfl rfl rfl) - #print axioms endianSite{{pc}} - end Extracted - - """); - } + " := by\n repeat' apply And.intro\n all_goals rfl\n" + + "#print axioms correctness\nend ExampleProof\n"); + ModelOperandChecks.Append(proof, method, EndianModel); queue.Add(new(method.FullName, proof, output), () => Console.WriteLine("PASS endian transfers: " + name)); } queue.Flush(); -var rejected = new RejectedEvidence(Path.Combine(root, "RejectedEvidence")); -var index = 0; -foreach (var (name, before, after, pc) in sites) + +// The primitive's CLI type gives the expected width and sign independently of the model table. +static string? EndianModel(MethodReference call) { - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_unsafe_validated"); - source = Regex.Replace(source, @"(?m)^theorem method0_unsafe_validated : [^\r\n]*", - $"theorem method0_unsafe_validated : {SiteType(pc)} := by decide +kernel"); - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing endian mutation: " + before); - var path = Path.Combine(root, $"Rejected{index++}.lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS exact endian evidence rejection: " + name, "UnsafeCallsValid"); + if (call.DeclaringType.FullName != "System.Buffers.Binary.BinaryPrimitives") return null; + bool read = call.Name.Contains("Read", StringComparison.Ordinal); + bool trying = call.Name.StartsWith("Try", StringComparison.Ordinal); + var integer = read && !trying ? call.ReturnType : call.Parameters[^1].ParameterType.GetElementType(); + var (width, signed) = integer.MetadataType switch + { + MetadataType.Int16 => (2, "true"), MetadataType.UInt16 => (2, "false"), + MetadataType.Int32 => (4, "true"), MetadataType.UInt32 => (4, "false"), + MetadataType.Int64 or MetadataType.IntPtr => (8, "true"), + MetadataType.UInt64 or MetadataType.UIntPtr => (8, "false"), + _ => throw new InvalidDataException($"Unexpected endian operand {integer}") + }; + var order = call.Name.EndsWith("BigEndian", StringComparison.Ordinal) ? "true" : "false"; + var attempt = trying ? "true" : "false"; + return read ? $".memory (.binaryRead {width} {signed} {order} {attempt})" + : $".memory (.binaryWrite {width} {order} {attempt})"; } -rejected.Check(); - -string RawSite(int pc) => $"(method0Closed.code[{pc}]?).getD ⟨0, 0, .none⟩"; -string EmittedSite(int pc) => $"(method0.code[{pc}]?).getD .nop"; -string SiteType(int pc) => "Extraction.UnsafeCallsValid rawLayouts " + - $"{{ method0Closed with code := [{RawSite(pc)}] }} [true] [{EmittedSite(pc)}]"; partial class Program { diff --git a/Tests/BindingFamilies/Binding.lean.in b/Tests/BindingFamilies/Binding.lean.in index db2d19e68..1f52551dc 100644 --- a/Tests/BindingFamilies/Binding.lean.in +++ b/Tests/BindingFamilies/Binding.lean.in @@ -6,6 +6,12 @@ namespace Generated theorem signature : Extracted.method0Signature = "@returnType@ BindingMethods::@name@(@managed@,@managed@)" := rfl #print axioms signature +/-- The declared types retain the exact managed width and signedness; the fixture methods declare + no locals, so their bodies carry no localsinit flag. -/ +def declared : CIL.DeclaredSignature := { + parameters := [.atom CIL.Extraction.runtimeScope "@managed@", .atom CIL.Extraction.runtimeScope "@managed@"] + returnType := .atom CIL.Extraction.runtimeScope "@returnType@" + initLocals := false } /-- The complete extracted body retains the reviewed operation and polarity. -/ theorem binding : Extracted.program[0]? = some (BindingSupport.@body@) := rfl @@ -15,6 +21,7 @@ theorem correctness (left right : CIL.W@width@) (memory : CIL.Safety.Memory) : CIL.Safety.invoke Extracted.program @fuel@ 0 [.scalar (.i@width@ left), .scalar (.i@width@ right)] memory = .ok (memory, [.scalar (@value@)]) := - BindingSupport.@shared@ Extracted.program Extracted.profile binding left right memory + BindingSupport.@shared@ Extracted.program Extracted.profile Extracted.environment declared + rfl rfl rfl binding left right memory #print axioms correctness end Generated diff --git a/Tests/BindingFamilies/Bindings.json b/Tests/BindingFamilies/Bindings.json index 7926bc687..b70da5297 100644 --- a/Tests/BindingFamilies/Bindings.json +++ b/Tests/BindingFamilies/Bindings.json @@ -6,16 +6,16 @@ "dimensions": [ [ { "name": "Equal", "type": "uint", "managed": "System.UInt32", "returnType": "System.Boolean", - "width": "32", "body": "equalityBody Extracted.profile true", + "width": "32", "body": "equalityBody Extracted.profile Extracted.environment declared true", "value": ".i32 (if left = right then 1 else 0)", "fuel": "5", "shared": "equal32" }, { "name": "Different", "type": "long", "managed": "System.Int64", "returnType": "System.Boolean", - "width": "64", "body": "equalityBody Extracted.profile false", + "width": "64", "body": "equalityBody Extracted.profile Extracted.environment declared false", "value": ".i32 (if left ≠ right then 1 else 0)", "fuel": "7", "shared": "different64" }, { "name": "And", "type": "int", "managed": "System.Int32", "returnType": "System.Int32", - "width": "32", "body": "bitwiseBody Extracted.profile .band", + "width": "32", "body": "bitwiseBody Extracted.profile Extracted.environment declared .and", "value": ".i32 (left &&& right)", "fuel": "5", "shared": "and32" }, { "name": "Xor", "type": "ulong", "managed": "System.UInt64", "returnType": "System.UInt64", - "width": "64", "body": "bitwiseBody Extracted.profile .bxor", + "width": "64", "body": "bitwiseBody Extracted.profile Extracted.environment declared .xor", "value": ".i64 (left ^^^ right)", "fuel": "5", "shared": "xor64" } ], [ { "profile": "scalar", "file": "scalar" }, { "profile": "x64-v3", "file": "x64_v3" } ] diff --git a/Tests/BindingFamilies/Negative.json b/Tests/BindingFamilies/Negative.json index 91e768414..18074f4c3 100644 --- a/Tests/BindingFamilies/Negative.json +++ b/Tests/BindingFamilies/Negative.json @@ -5,17 +5,11 @@ "template": "Binding.lean.in", "rows": [ { "case": "WrongWidth", "method": "Equal(uint,uint)", "name": "Equal", "managed": "System.UInt64", - "returnType": "System.Boolean", "width": "64", "body": "equalityBody Extracted.profile true", + "returnType": "System.Boolean", "width": "64", "body": "equalityBody Extracted.profile Extracted.environment declared true", "value": ".i32 (if left = right then 1 else 0)", "fuel": "5", "shared": "equal32" }, { "case": "WrongSignedness", "method": "Equal(uint,uint)", "name": "Equal", "managed": "System.Int32", - "returnType": "System.Boolean", "width": "32", "body": "equalityBody Extracted.profile true", - "value": ".i32 (if left = right then 1 else 0)", "fuel": "5", "shared": "equal32" }, - { "case": "WrongPolarity", "method": "Equal(uint,uint)", "name": "Equal", "managed": "System.UInt32", - "returnType": "System.Boolean", "width": "32", "body": "equalityBody Extracted.profile false", - "value": ".i32 (if left ≠ right then 1 else 0)", "fuel": "7", "shared": "equal32" }, - { "case": "ChangedBody", "method": "Xor(ulong,ulong)", "name": "Xor", "managed": "System.UInt64", - "returnType": "System.UInt64", "width": "64", "body": "bitwiseBody Extracted.profile .bxor", - "value": ".i64 (left ^^^ right)", "fuel": "5", "shared": "xor64" } + "returnType": "System.Boolean", "width": "32", "body": "equalityBody Extracted.profile Extracted.environment declared true", + "value": ".i32 (if left = right then 1 else 0)", "fuel": "5", "shared": "equal32" } ], "gate": { "name": "@case@", diff --git a/Tests/BindingFamilies/Support.lean b/Tests/BindingFamilies/Support.lean index 16c39ebf3..57ebb9b63 100644 --- a/Tests/BindingFamilies/Support.lean +++ b/Tests/BindingFamilies/Support.lean @@ -4,57 +4,106 @@ import CIL.Safety.SymbolicExecution # Reviewed integer binding families These small shared execution proofs are independent of extraction. Generated bindings must -prove the complete method equality and the exact managed signature before instantiation. +prove the complete method equality, the exact managed signature and that the method reads both +arguments without narrowing before instantiation. -/ namespace BindingSupport open CIL CIL.Safety /-- The fixed body of an equality or inequality method, including boolean return narrowing. -/ -@[cil_code] def equalityBody (profile : FeatureProfile) (positive : Bool) : Method := { - profile, locals := [], returnsValue := true, - code := [.arg 0, .arg 1, .eq] ++ - (if positive then [] else [.const32 0, .eq]) ++ [.ret (some .unsigned8)] } +@[cil_code] def equalityBody (profile : FeatureProfile) (environment : LayoutEnvironment) + (declared : DeclaredSignature) (positive : Bool) : Method := { + profile, environment, declared, locals := [], returnsValue := true, + code := [.ldarg 0, .ldarg 1, .ceq] ++ + (if positive then [] else [.ldc_i4 0, .ceq]) ++ [.ret] } /-- The fixed body of one binary bitwise method. -/ -@[cil_code] def bitwiseBody (profile : FeatureProfile) (operation : Op) : Method := { - profile, locals := [], returnsValue := true, code := [.arg 0, .arg 1, operation, .ret] } +@[cil_code] def bitwiseBody (profile : FeatureProfile) (environment : LayoutEnvironment) + (declared : DeclaredSignature) (operation : Instruction) : Method := { + profile, environment, declared, locals := [], returnsValue := true, + code := [.ldarg 0, .ldarg 1, operation, .ret] } /-- The equality family computes its boolean in arbitrary caller memory. -/ theorem equal32 (program : Program) (profile : FeatureProfile) - (found : program[0]? = some (equalityBody profile true)) + (environment : LayoutEnvironment) + (declared : DeclaredSignature) + (returned : declared.returnType = .atom Extraction.runtimeScope "System.Boolean") + (first : (equalityBody profile environment declared true).argumentAccess 0 = .value none) + (second : (equalityBody profile environment declared true).argumentAccess 1 = .value none) + (found : program[0]? = some (equalityBody profile environment declared true)) (left right : W32) (memory : Safety.Memory) : invoke program 5 0 [.scalar (.i32 left), .scalar (.i32 right)] memory = .ok (memory, [.scalar (.i32 (if left = right then 1 else 0))]) := by - cil_checked_steps [found] + rcases declared with ⟨receiver, parameters, returnType, initLocals⟩ + cases returned + -- The stepped body is the unfolded literal, so the argument facts are stated over it too. + simp only [cil_code, ↓reduceIte, List.cons_append, List.nil_append] at first second + cil_checked_steps [found, first, second] #print axioms equal32 /-- The opposite polarity computes inequality rather than weakening the equality contract. -/ theorem different64 (program : Program) (profile : FeatureProfile) - (found : program[0]? = some (equalityBody profile false)) + (environment : LayoutEnvironment) + (declared : DeclaredSignature) + (returned : declared.returnType = .atom Extraction.runtimeScope "System.Boolean") + (first : (equalityBody profile environment declared false).argumentAccess 0 = .value none) + (second : (equalityBody profile environment declared false).argumentAccess 1 = .value none) + (found : program[0]? = some (equalityBody profile environment declared false)) (left right : W64) (memory : Safety.Memory) : invoke program 7 0 [.scalar (.i64 left), .scalar (.i64 right)] memory = .ok (memory, [.scalar (.i32 (if left ≠ right then 1 else 0))]) := by - cil_checked_steps [found] + rcases declared with ⟨receiver, parameters, returnType, initLocals⟩ + cases returned + -- The stepped body is the unfolded literal, so the argument facts are stated over it too. + simp only [cil_code, Bool.false_eq_true, ↓reduceIte, List.cons_append, List.nil_append] + at first second + cil_checked_steps [found, first, second] all_goals (by_cases same : left = right <;> simp [same]) #print axioms different64 /-- The conjunction family computes the complete 32-bit result. -/ theorem and32 (program : Program) (profile : FeatureProfile) - (found : program[0]? = some (bitwiseBody profile .band)) + (environment : LayoutEnvironment) + (declared : DeclaredSignature) + (returned : declared.returnType = .atom Extraction.runtimeScope "System.Int32") + (first : (bitwiseBody profile environment declared .and).argumentAccess 0 = .value none) + (second : (bitwiseBody profile environment declared .and).argumentAccess 1 = .value none) + (found : program[0]? = some (bitwiseBody profile environment declared .and)) (left right : W32) (memory : Safety.Memory) : invoke program 5 0 [.scalar (.i32 left), .scalar (.i32 right)] memory = .ok (memory, [.scalar (.i32 (left &&& right))]) := by - cil_checked_steps [found] + rcases declared with ⟨receiver, parameters, returnType, initLocals⟩ + cases returned + -- The stepped body is the unfolded literal, so the argument facts are stated over it too. + simp only [cil_code] at first second + cil_checked_steps [found, first, second] #print axioms and32 /-- The exclusive-or family computes the complete 64-bit result. -/ theorem xor64 (program : Program) (profile : FeatureProfile) - (found : program[0]? = some (bitwiseBody profile .bxor)) + (environment : LayoutEnvironment) + (declared : DeclaredSignature) + (returned : declared.returnType = .atom Extraction.runtimeScope "System.UInt64") + (first : (bitwiseBody profile environment declared .xor).argumentAccess 0 = .value none) + (second : (bitwiseBody profile environment declared .xor).argumentAccess 1 = .value none) + (found : program[0]? = some (bitwiseBody profile environment declared .xor)) (left right : W64) (memory : Safety.Memory) : invoke program 5 0 [.scalar (.i64 left), .scalar (.i64 right)] memory = .ok (memory, [.scalar (.i64 (left ^^^ right))]) := by - cil_checked_steps [found] + rcases declared with ⟨receiver, parameters, returnType, initLocals⟩ + cases returned + -- The stepped body is the unfolded literal, so the argument facts are stated over it too. + simp only [cil_code] at first second + cil_checked_steps [found, first, second] #print axioms xor64 +/-- A byte-declared bitwise body truncates the result that the full-width helper preserves. -/ +theorem byte_bitwise_truncates (memory : Safety.Memory) : + invoke [bitwiseBody .scalar {} + { returnType := .atom Extraction.runtimeScope "System.Byte" } .and] + 5 0 [.scalar (.i32 300), .scalar (.i32 511)] memory = + .ok (memory, [.scalar (.i32 44)]) := rfl +#print axioms byte_bitwise_truncates + end BindingSupport diff --git a/Tests/BitModels.lean b/Tests/BitModels.lean new file mode 100644 index 000000000..2df585c04 --- /dev/null +++ b/Tests/BitModels.lean @@ -0,0 +1,285 @@ +import CIL.Safety.Execution +import CIL.Safety.StepComposition +import CIL.Extraction.CallAgreement +import CIL.Extraction.KernelEquation +import CIL.ProfileEquivalence +import Tests.Support + +/-! +# Bit and byte-order helper model tests + +The runtime table models `BitOperations`, the bit helpers each integer type declares and +`BinaryPrimitives.ReverseEndianness` by their scalar intrinsics. The model states each helper's +result: `System.Int32` for a count of `BitOperations`, and the integer type, a 64-bit word only at +64 bits, for a count the type declares. A helper site keeps its call, which steps as the +intrinsic it was lowered to, and the lowered intrinsic or another helper's is refused there. +-/ + +namespace Tests.BitModels +open CIL CIL.Extraction CIL.Safety + +private def runtimeType (name : String) : TypeExpr := .atom runtimeScope name +private def byte : TypeExpr := runtimeType "System.Byte" +private def sbyte : TypeExpr := runtimeType "System.SByte" +private def short : TypeExpr := runtimeType "System.Int16" +private def int : TypeExpr := runtimeType "System.Int32" +private def uint : TypeExpr := runtimeType "System.UInt32" +private def long : TypeExpr := runtimeType "System.Int64" +private def ulong : TypeExpr := runtimeType "System.UInt64" +private def bool : TypeExpr := runtimeType "System.Boolean" +private def bitOperations : TypeExpr := runtimeType "System.Numerics.BitOperations" +private def binaryPrimitives : TypeExpr := + .atom spanMemoryScope "System.Buffers.Binary.BinaryPrimitives" + +/-- `owner::name` taking `type`, then `System.Int32` when `rotation`, and returning `result`. -/ +private def helper (owner : TypeExpr) (name : MetadataName) (type result : TypeExpr) + (rotation := false) : MethodRef := + { owner, name, hasThis := false, genericArity := 0, returnType := result, + parameters := if rotation then [type, int] else [type] } + +private def popCount : MethodRef := helper bitOperations name!"PopCount" ulong int +private def leadingZeroCount : MethodRef := helper bitOperations name!"LeadingZeroCount" uint int +private def trailingZeroCount : MethodRef := + helper bitOperations name!"TrailingZeroCount" ulong int +private def log2 : MethodRef := helper bitOperations name!"Log2" ulong int +private def roundUp : MethodRef := helper bitOperations name!"RoundUpToPowerOf2" uint uint +private def rotateLeft : MethodRef := + helper bitOperations name!"RotateLeft" ulong ulong (rotation := true) +private def isPow2 : MethodRef := helper bitOperations name!"IsPow2" long bool +/-- `UInt64.PopCount`, which returns its type. -/ +private def ulongPopCount : MethodRef := helper ulong name!"PopCount" ulong ulong +private def uintPopCount : MethodRef := helper uint name!"PopCount" uint uint +private def bytePopCount : MethodRef := helper byte name!"PopCount" byte byte +private def byteRotateLeft : MethodRef := + helper byte name!"RotateLeft" byte byte (rotation := true) +private def reverseUlong : MethodRef := + helper binaryPrimitives name!"ReverseEndianness" ulong ulong +private def reverseShort : MethodRef := + helper binaryPrimitives name!"ReverseEndianness" short short +private def reverseSbyte : MethodRef := + helper binaryPrimitives name!"ReverseEndianness" sbyte sbyte + +-- Each helper resolves to its scalar intrinsic at the width and signedness of its type. +example : runtimeModel {} .call leadingZeroCount = + some (.intrinsic (.scalar (.leadingZeroCount 32 .word32)) 1) := by decide +kernel +example : runtimeModel {} .call trailingZeroCount = + some (.intrinsic (.scalar (.trailingZeroCount 64 .word32)) 1) := by decide +kernel +example : runtimeModel {} .call log2 = some (.intrinsic (.scalar (.log2 64 .word32)) 1) := by + decide +kernel +example : runtimeModel {} .call roundUp = + some (.intrinsic (.scalar (.roundUpToPowerOf2 32)) 1) := by decide +kernel +example : runtimeModel {} .call rotateLeft = some (.intrinsic (.scalar (.rotateLeft 64)) 2) := by + decide +kernel +example : runtimeModel {} .call isPow2 = some (.intrinsic (.scalar (.isPow2 64 true)) 1) := by + decide +kernel +example : runtimeModel {} .call byteRotateLeft = + some (.intrinsic (.scalar (.rotateLeft 8)) 2) := by decide +kernel +example : runtimeModel {} .call (helper short name!"IsPow2" short bool) = + some (.intrinsic (.scalar (.isPow2 16 true)) 1) := by decide +kernel +example : runtimeModel {} .call reverseUlong = + some (.intrinsic (.scalar (.reverseEndianness 64 false)) 1) := by decide +kernel +example : runtimeModel {} .call reverseSbyte = + some (.intrinsic (.scalar (.reverseEndianness 8 true)) 1) := by decide +kernel + +-- A count of `BitOperations` returns `System.Int32`, as a 32-bit word at every width. +example : runtimeModel {} .call popCount = + some (.intrinsic (.scalar (.popCount 64 .word32)) 1) := by decide +kernel +example : runtimeModel {} .call { popCount with returnType := ulong } = none := by decide +kernel +-- A count an integer type declares returns the type: a 64-bit word at 64 bits, a 32-bit word at +-- 32 bits, and a 32-bit word holding the narrow value at 8 and 16 bits. +example : runtimeModel {} .call ulongPopCount = + some (.intrinsic (.scalar (.popCount 64 .word64)) 1) := by decide +kernel +example : runtimeModel {} .call uintPopCount = + some (.intrinsic (.scalar (.popCount 32 .word32)) 1) := by decide +kernel +example : runtimeModel {} .call bytePopCount = + some (.intrinsic (.scalar (.popCount 8 .word32)) 1) := by decide +kernel +example : runtimeModel {} .call (helper short name!"LeadingZeroCount" short short) = + some (.intrinsic (.scalar (.leadingZeroCount 16 .word32)) 1) := by decide +kernel +example : runtimeModel {} .call { ulongPopCount with returnType := int } = none := by + decide +kernel +example : runtimeModel {} .call { bytePopCount with returnType := int } = none := by + decide +kernel + +-- A receiver, a generic arity or method argument, a wrong or mixed parameter, a wrong result, a +-- signed `Log2`, a signed narrow rotation, a helper the owner does not declare, or another owner, +-- assembly or opcode has no model. +example : runtimeModel {} .call { popCount with hasThis := true } = none := by decide +kernel +example : runtimeModel {} .call { popCount with genericArity := 1 } = none := by decide +kernel +example : runtimeModel {} .call { popCount with methodArguments := [ulong] } = none := by + decide +kernel +example : runtimeModel {} .call { rotateLeft with parameters := [ulong, long] } = none := by + decide +kernel +example : runtimeModel {} .call { rotateLeft with parameters := [ulong] } = none := by + decide +kernel +example : runtimeModel {} .call { popCount with parameters := [ulong, ulong] } = none := by + decide +kernel +example : runtimeModel {} .call (helper ulong name!"PopCount" uint uint) = none := by + decide +kernel +example : runtimeModel {} .call (helper bitOperations name!"PopCount" bool int) = none := by + decide +kernel +example : runtimeModel {} .call + (helper bitOperations name!"PopCount" (.atom "Fixture" "System.UInt64") int) = none := by + decide +kernel +example : runtimeModel {} .call { roundUp with returnType := int } = none := by decide +kernel +example : runtimeModel {} .call (helper int name!"Log2" int int) = none := by decide +kernel +example : runtimeModel {} .call (helper bitOperations name!"Log2" long int) = none := by + decide +kernel +example : runtimeModel {} .call (helper sbyte name!"RotateLeft" sbyte sbyte (rotation := true)) = + none := by decide +kernel +example : runtimeModel {} .call (helper bitOperations name!"PopCount" byte int) = none := by + decide +kernel +example : runtimeModel {} .call (helper uint name!"RoundUpToPowerOf2" uint uint) = none := by + decide +kernel +example : runtimeModel {} .call (helper bitOperations name!"ReverseEndianness" ulong ulong) = + none := by decide +kernel +/-- `BinaryPrimitives` as `System.Runtime` would declare it. -/ +private def runtimePrimitives : TypeExpr := runtimeType "System.Buffers.Binary.BinaryPrimitives" +example : runtimeModel {} .call { reverseUlong with owner := runtimePrimitives } = none := by + decide +kernel +/-- `BitOperations` as `System.Memory` and as `System.Private.CoreLib` would declare it. -/ +private def memoryBits : TypeExpr := .atom spanMemoryScope "System.Numerics.BitOperations" +private def coreBits : TypeExpr := .atom "System.Private.CoreLib" "System.Numerics.BitOperations" +example : runtimeModel {} .call { popCount with owner := memoryBits } = none := by decide +kernel +example : runtimeModel {} .call { popCount with owner := coreBits } = none := by decide +kernel +example : runtimeModel {} .newobj popCount = none := by decide +kernel + +/-- A method whose only instruction is `instruction`. -/ +private def method (instruction : RawInstruction) : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := true, maxStack := 3, genericParameters := [], code := [instruction] } + +/-- A call of `method`. -/ +private def site (method : MethodRef) : RawInstruction := + ⟨0, 40, .call { + method, declared := method.signature 0, target := .external "", typeArguments := [], + parameterCount := method.parameters.length, returnsValue := true }⟩ + +/-- The bit and byte-order members require the default static calling convention. -/ +example : [popCount, reverseUlong].all (fun method => + [0x40, 0x60, 0x01].all fun header => + let operand : CallSite := { + method, declared := method.signature header, + target := .external "", typeArguments := [], parameterCount := 1, returnsValue := true } + !runtimeCatalog.siteAgrees {} {} ⟨0, 40, .call operand⟩ (.call method {})) := by + decide +kernel + +/-- A link does not excuse a mismatched model declaration at an occupied pc. -/ +theorem linkedDeclarationRequired : + let operand : CallSite := { + method := popCount, declared := popCount.signature 0x40, + target := .external "", typeArguments := [], parameterCount := 1, returnsValue := true } + runtimeCatalog.siteAgrees {} {} ⟨0, 40, .call operand⟩ (.call popCount {}) + (some (.direct 0)) true = false := by decide +kernel + +#print axioms linkedDeclarationRequired + +/-- A method with no code, on the scalar profile. -/ +private def body : CIL.Method := { code := [], locals := [], returnsValue := false } + +/-- A body whose only instruction is `emitted`, with no link. -/ +private def emitting (emitted : CIL.Instruction) : CIL.Method := { body with code := [emitted] } + +/-- Each helper family has model ownership at its raw site. -/ +theorem bitClaims : + siteClaim {} none (site popCount) = .model ∧ + siteClaim {} none (site ulongPopCount) = .model ∧ + siteClaim {} none (site reverseUlong) = .model := by decide +kernel + +#print axioms bitClaims + +private def leading64 : CIL.Instruction := + .call (helper bitOperations name!"LeadingZeroCount" ulong int) +example : RuntimeModelsAgree (method (site popCount)) [true] (emitting (.call popCount {})) := by + decide +kernel +example : ¬ RuntimeModelsAgree (method (site popCount)) [true] (emitting leading64) := by + decide +kernel +example : ManagedCallsValid [] [] (callModelled body) (method (site popCount)) [true] + (emitting (.call popCount {})) := by decide +kernel +example : ¬ ManagedCallsValid [] [] (callModelled body) (method (site popCount)) [true] + (emitting (.call log2 {})) := by decide +kernel +private def narrowCount : CIL.Instruction := .call popCount +example : ¬ RuntimeModelsAgree (method (site ulongPopCount)) [true] (emitting narrowCount) := by + decide +kernel +private def reverse32 : CIL.Instruction := .call (helper binaryPrimitives name!"ReverseEndianness" uint uint) +example : ¬ RuntimeModelsAgree (method (site reverseUlong)) [true] (emitting reverse32) := by + decide +kernel + +-- The helpers read no profile flag: a helper site agrees under the scalar and the AVX2 profile, +-- and every two profiles agree on a program that calls one. +example : runtimeCatalog.siteAgrees {} {} (site popCount) (.call popCount {}) = true := by + decide +kernel +example : runtimeCatalog.siteAgrees {} { avx2 := true } (site popCount) (.call popCount {}) = + true := by decide +kernel +/-- A method that returns the population of its argument. -/ +private def popCountProgram : CIL.Program := + [{ body with code := [.ldarg 0, .call popCount {}, .ret], returnsValue := true }] +example : CIL.Program.profileReads popCountProgram = [] := by decide +kernel +example : CIL.Program.ProfileAgreement popCountProgram {} { avx2 := true } := + CIL.Program.ProfileAgreement.of_flags _ _ _ (by decide +kernel) + +/-! ## Steps -/ + +private def frame : Frame := { activation := 0, locals := [], owned := [] } + +/-- A body whose only instruction is a call of `method`, with no link. -/ +private def calling (method : MethodRef) : CIL.Method := { body with code := [.call method {}] } +private def i32 (bits : Nat) : CIL.Safety.Value := .scalar (.i32 (BitVec.ofNat 32 bits)) +private def i64 (bits : Nat) : CIL.Safety.Value := .scalar (.i64 (BitVec.ofNat 64 bits)) + +-- Each checked call runs its declared intrinsic, and the result word is the one the +-- model states: an int from `BitOperations.PopCount`, a long from `UInt64.PopCount`. +example : step (calling popCount) (.call popCount {}) 0 [] frame [i64 7] Memory.empty = + intrinsicCall body.profile (.scalar (.popCount 64 .word32)) 1 0 frame [i64 7] + Memory.empty := by rfl +example : step (calling popCount) (.call popCount {}) 0 [] frame [i64 7] Memory.empty = + .ok (.next 1 [i32 3] frame Memory.empty) := by rfl +example : step (calling ulongPopCount) (.call ulongPopCount {}) 0 [] frame [i64 7] Memory.empty = + .ok (.next 1 [i64 3] frame Memory.empty) := by rfl +example : step (calling bytePopCount) (.call bytePopCount {}) 0 [] frame [i32 0x1FF] + Memory.empty = .ok (.next 1 [i32 8] frame Memory.empty) := by rfl +example : step (calling rotateLeft) (.call rotateLeft {}) 0 [] frame [i64 1, i32 65] + Memory.empty = + intrinsicCall body.profile (.scalar (.rotateLeft 64)) 2 0 frame [i64 1, i32 65] + Memory.empty := by rfl +example : step (calling isPow2) (.call isPow2 {}) 0 [] frame [i64 (2 ^ 63)] Memory.empty = + .ok (.next 1 [i32 0] frame Memory.empty) := by rfl + +-- The counts and `Log2` of zero, `RoundUpToPowerOf2` past the largest power, and the byte +-- reversal of a sign-extended narrow value step as their intrinsics. +example : step (calling log2) (.call log2 {}) 0 [] frame [i64 0] Memory.empty = + intrinsicCall body.profile (.scalar (.log2 64 .word32)) 1 0 frame [i64 0] Memory.empty := by + rfl +example : step (calling log2) (.call log2 {}) 0 [] frame [i64 0] Memory.empty = + .ok (.next 1 [i32 0] frame Memory.empty) := by rfl +example : step (calling leadingZeroCount) (.call leadingZeroCount {}) 0 [] frame [i32 0] + Memory.empty = + intrinsicCall body.profile (.scalar (.leadingZeroCount 32 .word32)) 1 0 frame [i32 0] + Memory.empty := by rfl +example : step (calling leadingZeroCount) (.call leadingZeroCount {}) 0 [] frame [i32 0] + Memory.empty = .ok (.next 1 [i32 32] frame Memory.empty) := by rfl +example : step (calling trailingZeroCount) (.call trailingZeroCount {}) 0 [] frame [i64 0] + Memory.empty = .ok (.next 1 [i32 64] frame Memory.empty) := by rfl +example : step (calling popCount) (.call popCount {}) 0 [] frame [i64 0] Memory.empty = + .ok (.next 1 [i32 0] frame Memory.empty) := by rfl +example : step (calling roundUp) (.call roundUp {}) 0 [] frame [i32 0x80000001] Memory.empty = + intrinsicCall body.profile (.scalar (.roundUpToPowerOf2 32)) 1 0 frame [i32 0x80000001] + Memory.empty := by rfl +example : step (calling roundUp) (.call roundUp {}) 0 [] frame [i32 0x80000001] Memory.empty = + .ok (.next 1 [i32 0] frame Memory.empty) := by rfl +example : step (calling reverseShort) (.call reverseShort {}) 0 [] frame [i32 0xFFFFFF80] + Memory.empty = + intrinsicCall body.profile (.scalar (.reverseEndianness 16 true)) 1 0 frame [i32 0xFFFFFF80] + Memory.empty := by rfl +example : step (calling reverseShort) (.call reverseShort {}) 0 [] frame [i32 0xFFFFFF80] + Memory.empty = .ok (.next 1 [i32 0xFFFF80FF] frame Memory.empty) := by rfl +example : step (calling reverseSbyte) (.call reverseSbyte {}) 0 [] frame [i32 0xFFFFFF80] + Memory.empty = .ok (.next 1 [i32 0xFFFFFF80] frame Memory.empty) := by rfl +-- A call faults where its intrinsic does: a 64-bit helper given a 32-bit word. +example : step (calling popCount) (.call popCount {}) 0 [] frame [i32 7] Memory.empty = + intrinsicCall body.profile (.scalar (.popCount 64 .word32)) 1 0 frame [i32 7] + Memory.empty := by rfl +example : (step (calling popCount) (.call popCount {}) 0 [] frame [i32 7] Memory.empty).toBool = + false := by rfl + +end Tests.BitModels diff --git a/Tests/Blockers.cs b/Tests/Blockers.cs index 7da2fe0fd..01a6301d4 100644 --- a/Tests/Blockers.cs +++ b/Tests/Blockers.cs @@ -1,16 +1,33 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Reflection; using System.Text.Json; +const string holeCorpus = "Tests/HoleCorpus.cs"; +string[] holeArguments = ["--admission-report", At("artifacts/SelfAdmission/blockers.json")]; +var holeCommand = PreparedFixtureCommand(holeCorpus, holeArguments); +if (holeCommand is null) +{ + // Build before admission. The project reference can replace the extractor whose hash the report records. + Run(["dotnet", "build", holeCorpus, "-c", "Release", + "-p:EnforceCodeStyleInBuild=true", "-p:GenerateDocumentationFile=true"]); + holeCommand = FixtureCommand(holeCorpus, holeArguments, prepared: true); +} + // Admission report: shared blockers are deduplicated with every chain, excluded branches stay silent. var directory = At("artifacts/Blockers"); var source = Path.Combine(directory, "Assembly"); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Fixture.csproj"), """ -net10.0true +net11.0 +true """); var fixture = """ using System.Runtime.CompilerServices; @@ -286,8 +303,63 @@ string BuildAs(string configuration) Require(debug.Code != 0 && debug.Output.Contains("build configuration"), "A Debug build is accepted:\n" + debug.Output); +// A runtime-async body returns the awaited result under a task signature: refused as an entry and as a callee. +var asyncSource = Path.Combine(directory, "RuntimeAsync"); +Directory.CreateDirectory(asyncSource); +File.WriteAllText(Path.Combine(asyncSource, "RuntimeAsync.csproj"), """ +net11.0true +runtime-async=on +"""); +File.WriteAllText(Path.Combine(asyncSource, "RuntimeAsync.cs"), """ +using System.Threading.Tasks; + +public static class Awaiting +{ + public static async ValueTask Twice(long x) => x * 2; + + public static long Caller(long x) => Twice(x).Result; +} +"""); +Run(["dotnet", "build", Path.Combine(asyncSource, "RuntimeAsync.csproj"), "-c", "Release"]); +var asyncAssembly = Path.Combine(asyncSource, "bin/RuntimeAsync.dll"); +const int runtimeAsyncFlag = 0x2000; +using (var image = new System.Reflection.PortableExecutable.PEReader(File.OpenRead(asyncAssembly))) +{ + var metadata = System.Reflection.Metadata.PEReaderExtensions.GetMetadataReader(image); + var twice = metadata.MethodDefinitions.Select(metadata.GetMethodDefinition) + .Single(method => metadata.GetString(method.Name) == "Twice"); + Require(((int)twice.ImplAttributes & runtimeAsyncFlag) != 0, "The fixture's Twice is not a runtime-async method"); +} +var asyncManifest = Path.Combine(directory, "runtime-async-manifest.json"); +File.WriteAllText(asyncManifest, JsonSerializer.Serialize(new +{ + entries = new object[] + { + new { signature = "Awaiting::Twice(long)", profile = "scalar" }, + new { signature = "Awaiting::Caller(long)", profile = "scalar" } + } +})); +var asyncReport = Path.Combine(directory, "runtime-async-blockers.json"); +var asyncSummary = Run(["dotnet", ExtractorPath, "blockers", asyncAssembly, "--manifest", asyncManifest, + "--output", asyncReport]); +var asyncJson = JsonSerializer.Deserialize(File.ReadAllText(asyncReport)); +const string twiceMethod = "System.Threading.Tasks.ValueTask`1 Awaiting::Twice(System.Int64)"; +const string callerMethod = "System.Int64 Awaiting::Caller(System.Int64)"; +var refusal = asyncJson.GetProperty("blockers").EnumerateArray() + .Single(b => b.GetProperty("construct").GetString() == "Unsupported method metadata: runtime-async method"); +var refusalSite = refusal.GetProperty("sites").EnumerateArray().Single(); +Require(asyncJson.GetProperty("entries").EnumerateArray().All(e => e.GetProperty("status").GetString() == "blocked") && + refusalSite.GetProperty("method").GetString() == twiceMethod && + refusalSite.GetProperty("message").GetString()!.Contains("returns the awaited result, not the declared task") && + Reached(refusal).SequenceEqual([$"scalar {callerMethod} -> {twiceMethod}", $"scalar {twiceMethod}"]), + "Runtime-async method not refused as entry and callee:\n" + asyncSummary + "\n" + File.ReadAllText(asyncReport)); +var asyncExtract = Execute(["dotnet", ExtractorPath, "extract", asyncAssembly, "Awaiting::Twice(long)", + Path.Combine(directory, "extract", "runtime-async"), "scalar"]); +Require(asyncExtract.Code != 0 && asyncExtract.Output.Contains("Unsupported method metadata: runtime-async method"), + "extract does not name the runtime-async refusal:\n" + asyncExtract.Output); + // Distinct arrays and sites are compared by content, while output keeps first discovery order. -var blockersType = Assembly.LoadFrom(ExtractorPath).GetType("Nethermind.LeanCIL.Blockers", true)!; +var blockersType = Assembly.Load(File.ReadAllBytes(ExtractorPath)).GetType("Nethermind.LeanCIL.Blockers", true)!; var findingType = blockersType.GetNestedType("Finding", BindingFlags.NonPublic)!; var siteType = blockersType.GetNestedType("Site", BindingFlags.NonPublic)!; const BindingFlags instanceMembers = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; @@ -314,6 +386,8 @@ void Add(int offset, string profile, string[] chain) => add.Invoke(finding, // LeanCIL's own assembly against its checked-in baseline, then the gate's decisions on altered baselines. const string selfAdmission = "Tests/SelfAdmission.cs"; Run(["dotnet", "run", "--file", selfAdmission], streamOutput: true, progress: "Self-admission"); +Run(["dotnet", "run", "--file", "Tests/CanonicalMethods.cs"], streamOutput: true, progress: "Canonical methods"); +Run(holeCommand, streamOutput: true, progress: "Hole corpus"); var selfReport = JsonSerializer.Deserialize(File.ReadAllText(At("artifacts/SelfAdmission/blockers.json"))) .GetProperty("entries").EnumerateArray().ToArray(); // Source-named methods, so the gate's stable names equal the report's. @@ -357,4 +431,228 @@ void Add(int offset, string profile, string[] chain) => add.Invoke(finding, gained.Output.Contains("Gained admissions") && gained.Output.Contains(unrecorded), "An unrecorded admission passed the gate:\n" + gained.Output); } +// A volatile field is read and written under a `volatile.` prefix, which the single-threaded model gives no meaning: +// extraction stops at it and names the instruction, the method and the stage. +var volatileSource = Path.Combine(directory, "VolatileAssembly"); +Directory.CreateDirectory(volatileSource); +File.WriteAllText(Path.Combine(volatileSource, "Volatile.csproj"), + File.ReadAllText(Path.Combine(source, "Fixture.csproj"))); +File.WriteAllText(Path.Combine(volatileSource, "Volatile.cs"), """ +public sealed class Flag { public volatile int Raised; } +public static class Access +{ + public static int Read(Flag flag) => flag.Raised; + public static void Write(Flag flag) { flag.Raised = 1; } +} +"""); +Run(["dotnet", "build", Path.Combine(volatileSource, "Volatile.csproj"), "-c", "Release"]); +foreach (var (selector, method) in new[] { ("Access::Read(Flag)", "System.Int32 Access::Read(Flag)"), + ("Access::Write(Flag)", "System.Void Access::Write(Flag)") }) +{ + var refused = Execute(["dotnet", ExtractorPath, "extract", Path.Combine(volatileSource, "bin/Volatile.dll"), + selector, Path.Combine(directory, "VolatileExtraction")]); + Require(refused.Code != 0 && refused.Output.Contains("Unsupported instruction: IL_", StringComparison.Ordinal) && + refused.Output.Contains("volatile.", StringComparison.Ordinal) && + refused.Output.Contains("Method: " + method, StringComparison.Ordinal) && + refused.Output.Contains("Stage: metadata and CIL validation", StringComparison.Ordinal), + "A volatile access was not refused with the unsupported-instruction message:\n" + refused.Output); +} +// A virtual call scans the classes of the image for subclasses of its slot. Resolving every class's base from +// disk makes that scan cost milliseconds per class, so four times the classes would cost four times as long. +const int FewClasses = 125; +const int ManyClasses = 500; +const int VirtualCallers = 100; +const double AllowedClassGrowth = 2.5; +double VirtualDispatchSeconds(int classes) +{ + var fixtureDirectory = Path.Combine(directory, "Dispatch" + classes); + Directory.CreateDirectory(fixtureDirectory); + File.WriteAllText(Path.Combine(fixtureDirectory, "Dispatch.csproj"), """ + + net11.0true + + """); + // Each caller is blocked after its virtual call, so no extraction runs and the dispatch scan is the cost. + File.WriteAllText(Path.Combine(fixtureDirectory, "Dispatch.cs"), string.Join('\n', + ["public abstract class Shape { public abstract int Speak(); }", + "public sealed class Circle : Shape { public override int Speak() => 1; }", + .. Enumerable.Range(0, classes).Select(index => + $"public sealed class Bystander{index} {{ public int Value; }}"), + "public static class Callers {", + .. Enumerable.Range(0, VirtualCallers).Select(index => + $" public static int Call{index}(Shape shape, int size) => shape.Speak() + new decimal[size].Length;"), + "}"])); + Run(["dotnet", "build", Path.Combine(fixtureDirectory, "Dispatch.csproj"), "-c", "Release"]); + var callers = Path.Combine(fixtureDirectory, "callers.json"); + File.WriteAllText(callers, JsonSerializer.Serialize(new + { + entries = Enumerable.Range(0, VirtualCallers).Select(index => + new { signature = $"Callers::Call{index}(Shape,int)", profile = "scalar" }) + })); + var seconds = double.MaxValue; + for (var sample = 0; sample < 3; sample++) + { + var timer = System.Diagnostics.Stopwatch.StartNew(); + Run(["dotnet", ExtractorPath, "blockers", Path.Combine(fixtureDirectory, "bin/Dispatch.dll"), + "--manifest", callers, "--output", Path.Combine(fixtureDirectory, "blockers.json")]); + seconds = Math.Min(seconds, timer.Elapsed.TotalSeconds); + } + return seconds; +} +var fewClassesSeconds = VirtualDispatchSeconds(FewClasses); +var manyClassesSeconds = VirtualDispatchSeconds(ManyClasses); +Require(manyClassesSeconds <= fewClassesSeconds * AllowedClassGrowth, + $"{VirtualCallers} virtual calls took {fewClassesSeconds:F1} s with {FewClasses} classes and " + + $"{manyClassesSeconds:F1} s with {ManyClasses}; the limit is {AllowedClassGrowth} times as long."); +// A regex runner reads fields of its platform base class. Each reference to that class must resolve to one +// definition, so the body is blocked by its unsupported constructs rather than by an internal error. +var regexDirectory = Path.Combine(directory, "Regex"); +Directory.CreateDirectory(regexDirectory); +File.WriteAllText(Path.Combine(regexDirectory, "Regex.csproj"), """ + + net11.0true + +"""); +File.WriteAllText(Path.Combine(regexDirectory, "Patterns.cs"), """ +using System.Text.RegularExpressions; + +public static partial class Patterns +{ + [GeneratedRegex(@"\A[A-Za-z_][A-Za-z0-9_']*\z")] + public static partial Regex Identifier { get; } +} +"""); +Run(["dotnet", "build", Path.Combine(regexDirectory, "Regex.csproj"), "-c", "Release"]); +var regexAssembly = Path.Combine(regexDirectory, "bin/Regex.dll"); +// The generated runner's type name carries a hash of the pattern, so it is read from the built assembly. +var runner = Run(["dotnet", ExtractorPath, "list", regexAssembly]).Split('\n').Select(line => line.Trim()) + .Single(line => line.EndsWith("/RunnerFactory/Runner::TryFindNextPossibleStartingPosition(" + + "System.ReadOnlySpan`1)", StringComparison.Ordinal)); +var runnerType = runner[(runner.IndexOf(' ', StringComparison.Ordinal) + 1).. + runner.IndexOf("::", StringComparison.Ordinal)]; +var regexManifest = Path.Combine(regexDirectory, "manifest.json"); +File.WriteAllText(regexManifest, JsonSerializer.Serialize(new +{ + entries = new[] { new { signature = runner, profile = "scalar" } } +})); +var regexReport = Path.Combine(regexDirectory, "blockers.json"); +Run(["dotnet", ExtractorPath, "blockers", regexAssembly, "--manifest", regexManifest, "--output", regexReport]); +var regexConstructs = JsonSerializer.Deserialize(File.ReadAllText(regexReport)).GetProperty("blockers") + .EnumerateArray().Select(blocker => blocker.GetProperty("construct").GetString()!).Order(StringComparer.Ordinal) + .ToArray(); +Require(regexConstructs.SequenceEqual([ + "Unsupported aggregate layout: System.Text.RegularExpressions.RegexRunner", + "Unsupported class instance type: " + runnerType]), + "The regex runner is not blocked by its own constructs: " + string.Join("; ", regexConstructs)); + +// Lean refuses a field operand no carried row positions at every pc, so a dead read of one blocks its method. +var unpinnedDirectory = Path.Combine(directory, "UnpinnedField"); +var unpinnedLibrary = Path.Combine(unpinnedDirectory, "Lib"); +var unpinnedApplication = Path.Combine(unpinnedDirectory, "App"); +Directory.CreateDirectory(unpinnedLibrary); +Directory.CreateDirectory(unpinnedApplication); +File.WriteAllText(Path.Combine(unpinnedLibrary, "Lib.csproj"), """ +net11.0 +Unpinned.Libtrue +"""); +File.WriteAllText(Path.Combine(unpinnedLibrary, "Point.cs"), """ +namespace Unpinned; +public struct Point { public int X; public int Y; } +"""); +File.WriteAllText(Path.Combine(unpinnedApplication, "App.csproj"), """ +net11.0 +Unpinned.Apptrue + +"""); +File.WriteAllText(Path.Combine(unpinnedApplication, "Reader.cs"), """ +using System.Runtime.Intrinsics.X86; +public static class Reader +{ + public static Unpinned.Point Origin; + + public static int Read(int x) + { + if (Avx2.IsSupported) return Origin.Y; + return x; + } +} +"""); +Run(["dotnet", "build", Path.Combine(unpinnedApplication, "App.csproj"), "-c", "Release"]); +var unpinnedAssembly = Path.Combine(unpinnedApplication, "bin/Unpinned.App.dll"); +var unpinnedManifest = Path.Combine(unpinnedDirectory, "manifest.json"); +File.WriteAllText(unpinnedManifest, JsonSerializer.Serialize(new +{ + entries = new[] { new { signature = "Reader::Read(int)", profile = "scalar" } } +})); +var unpinnedReport = Path.Combine(unpinnedDirectory, "blockers.json"); +Run(["dotnet", ExtractorPath, "blockers", unpinnedAssembly, "--manifest", unpinnedManifest, + "--output", unpinnedReport]); +const string UnpinnedConstruct = "Unsupported field access: System.Int32 Unpinned.Point::Y"; +const string UnpinnedReason = "Its owner has no carried definition, so no metadata row gives the field's position."; +var unpinnedBlockers = JsonSerializer.Deserialize(File.ReadAllText(unpinnedReport)) + .GetProperty("blockers").EnumerateArray().ToArray(); +Require(unpinnedBlockers.Length == 1 && + unpinnedBlockers[0].GetProperty("construct").GetString() == UnpinnedConstruct && + unpinnedBlockers[0].GetProperty("sites")[0].GetProperty("message").GetString()!.Contains(UnpinnedReason, + StringComparison.Ordinal), + "A dead read of an unpinned struct's field is not blocked by that field: " + File.ReadAllText(unpinnedReport)); +var unpinnedExtraction = Execute(["dotnet", ExtractorPath, "extract", unpinnedAssembly, "Reader::Read(int)", + Path.Combine(unpinnedDirectory, "Extracted"), "scalar"]); +var refusedWithReason = unpinnedExtraction.Code != 0 && + unpinnedExtraction.Output.Contains(UnpinnedConstruct, StringComparison.Ordinal) && + unpinnedExtraction.Output.Contains(UnpinnedReason, StringComparison.Ordinal); +Require(refusedWithReason, + "Extraction does not refuse the dead read with the blocker's diagnostic: " + unpinnedExtraction.Output); + +// Each admitted entry is extracted again. Reading and hashing the whole image per entry makes that linear in the +// image, so doubling the methods of an image that grows with them would make admission four times as long. +const int FewAdmitted = 100; +const int ManyAdmitted = 200; +// A resource stands for the rest of an assembly, so the image grows with the methods without adding analysis. +const int PaddingPerMethod = 512 * 1024; +const double AllowedPerMethodGrowth = 1.25; +// One wall-clock run on a loaded machine varied by 1.6 times; the fastest of three tracks the work. +const int AdmissionRuns = 3; +double AdmissionSeconds(int methods) +{ + var fixtureDirectory = Path.Combine(directory, "Admission" + methods); + Directory.CreateDirectory(fixtureDirectory); + File.WriteAllText(Path.Combine(fixtureDirectory, "Admission.csproj"), """ + + net11.0true + + + """); + File.WriteAllBytes(Path.Combine(fixtureDirectory, "padding.bin"), new byte[methods * PaddingPerMethod]); + File.WriteAllText(Path.Combine(fixtureDirectory, "Admission.cs"), string.Join('\n', + ["public static class Admitted {", + .. Enumerable.Range(0, methods).Select(index => + $" public static ulong Add{index}(ulong x) => x + {index}UL;"), + "}"])); + Run(["dotnet", "build", Path.Combine(fixtureDirectory, "Admission.csproj"), "-c", "Release"]); + var admissionManifest = Path.Combine(fixtureDirectory, "manifest.json"); + File.WriteAllText(admissionManifest, JsonSerializer.Serialize(new + { + entries = Enumerable.Range(0, methods).Select(index => + new { signature = $"Admitted::Add{index}(ulong)", profile = "scalar" }) + })); + var admissionReport = Path.Combine(fixtureDirectory, "blockers.json"); + var seconds = double.MaxValue; + for (var run = 0; run < AdmissionRuns; run++) + { + var timer = System.Diagnostics.Stopwatch.StartNew(); + Run(["dotnet", ExtractorPath, "blockers", Path.Combine(fixtureDirectory, "bin/Admission.dll"), + "--manifest", admissionManifest, "--output", admissionReport]); + seconds = Math.Min(seconds, timer.Elapsed.TotalSeconds); + } + var admitted = JsonSerializer.Deserialize(File.ReadAllText(admissionReport)).GetProperty("counts") + .GetProperty("admitted").GetInt32(); + Require(admitted == methods, $"{admitted} of {methods} padded fixture methods admitted"); + return seconds; +} +var fewAdmittedSeconds = AdmissionSeconds(FewAdmitted); +var manyAdmittedSeconds = AdmissionSeconds(ManyAdmitted); +Require(manyAdmittedSeconds <= fewAdmittedSeconds * 2 * AllowedPerMethodGrowth, + $"Admitting {FewAdmitted} methods took {fewAdmittedSeconds:F1} s and {ManyAdmitted} took " + + $"{manyAdmittedSeconds:F1} s; the limit is {2 * AllowedPerMethodGrowth} times as long."); Console.WriteLine($"PASS {checks} admission report checks"); diff --git a/Tests/Blocks.cs b/Tests/Blocks.cs index 5e3868ebe..73e15b4a2 100644 --- a/Tests/Blocks.cs +++ b/Tests/Blocks.cs @@ -2,9 +2,12 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs -#:include GeneratedEvidence.cs -#:include RejectedEvidence.cs using System.Reflection; using System.Runtime.CompilerServices; @@ -32,10 +35,18 @@ void Prove(string dll, string selector, string name, List claims) var output = Path.Combine(root, name); Directory.CreateDirectory(output); var proof = Path.Combine(output, "BoundaryProof.lean"); - File.WriteAllText(proof, "import Extracted\nimport Tests.Support\nset_option maxRecDepth 8192\nnamespace ExampleProof\n" + memory + - "\ntheorem correctness :\n" + string.Join(" ∧\n", claims) + " := by\n exact " + - (claims.Count == 1 ? "rfl" : "⟨" + string.Join(", ", claims.Select(_ => "rfl")) + "⟩") + - "\n#print axioms correctness\nend ExampleProof\n"); + var proofs = string.Join(", ", claims.Select(_ => "rfl")); + File.WriteAllText(proof, $$""" + import Extracted + import Tests.Support + namespace ExampleProof + {{memory}} + theorem correctness : + {{string.Join(" \u2227\n", claims)}} := by + exact {{(claims.Count == 1 ? "rfl" : $"\u27e8{proofs}\u27e9")}} + #print axioms correctness + end ExampleProof + """); queue.Add(new(selector, proof, output, Assembly: dll), () => { cases += claims.Count; @@ -51,8 +62,17 @@ void Prove(string dll, string selector, string name, List claims) var second = copy ? ".reference (.address ⟨1, 0⟩)" : ".scalar (.i32 7)"; var expected = copy ? "[32, 33, 34, 165]" : "[7, 7, 7, 165]"; var parameters = copy ? "byte*,byte*,uint" : "byte*,byte,uint"; + var operation = copy ? "copyBlock" : "initBlock"; + var alignment = aligned ? 8 : 1; var claims = new List { + $$""" + (match (Extracted.method0.code[3]? : Option CIL.Instruction) with + | some (.call method _) => + CIL.Extraction.runtimeModel Extracted.environment .call method == + some (.memory (.{{operation}} {{alignment}})) + | _ => false) = true + """, $"(CIL.Safety.invoke Extracted.program 16 0 [Tests.reference, {second}, .scalar (.i32 3)] " + "memory).map (fun result => (List.range 4).map (fun offset => " + $"(result.1.cells 0 offset).bits.toNat)) = .ok {expected}" @@ -63,30 +83,9 @@ void Prove(string dll, string selector, string name, List claims) Prove(assembly, $"Blocks::{name}({parameters})", name, claims); } -void CheckPointerEvidence() -{ - var rejected = new RejectedEvidence(Path.Combine(root, "RejectedPointerEvidence")); - foreach (var (name, before, after) in new[] - { - ("CopyPointer", ".copyBlock 1", ".copyBlock 8"), - ("FillPointerAligned", ".initBlock 8", ".initBlock 1"), - ("CopyPointer", ".copyBlock 1", ".initBlock 1") - }) - { - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_unsafe_validated"); - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing block mutation"); - var path = Path.Combine(root, name + after.Replace(' ', '-') + ".lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS pointer block evidence rejection: " + name + after, "UnsafeCallsValid"); - } - rejected.Check(); -} - if (args.Length != 0) { queue.Flush(); - CheckPointerEvidence(); return; } @@ -139,6 +138,7 @@ void CheckPointerEvidence() Directory.CreateDirectory(output); var dll = Path.Combine(output, "Examples.dll"); string selector; + int? blockPc = null; var methodName = (copy ? "Copy" : "Fill") + (alignment == 8 ? "Aligned" : ""); using (var module = ModuleDefinition.ReadModule(assembly)) { @@ -150,6 +150,7 @@ void CheckPointerEvidence() call.Operand = null; if (alignment != 8) method.Body.GetILProcessor().InsertBefore(call, Instruction.Create(OpCodes.Unaligned, (byte)alignment)); + blockPc = method.Body.Instructions.IndexOf(call); } selector = method.FullName; module.Write(dll); @@ -158,6 +159,11 @@ void CheckPointerEvidence() var fill = copy ? null : runtime.CreateDelegate(); var move = copy ? runtime.CreateDelegate() : null; var claims = new List(); + if (blockPc is { } pc) + { + var opcode = copy ? "cpblk" : "initblk"; + claims.Add($"Extracted.method0.code[{pc}]? = some (.{opcode} {alignment})"); + } var offset = alignment is 0 or 1 ? 1 : alignment == 8 ? 0 : alignment; foreach (var count in new[] { 0, 1, 3, 8 }) foreach (var value in new byte[] { 0, 127, 255 }) @@ -272,7 +278,7 @@ string Fault(string args, string rule, string state = "memory") => module.Write(dll); } var result = Execute(Extract("extract", dll, "Examples", selector, output)); - var expected = mutation is "branch" or "switch" ? "Branch enters" : + var expected = mutation is "branch" or "switch" ? "Branch enters the instruction after a prefix" : mutation is "count64" or "native-count" or "value64" ? "Unsupported block memory operand types" : "malformed unaligned prefix"; if (result.Code == 0 || !result.Output.Contains(expected) || File.Exists(Path.Combine(output, "Extracted.lean"))) throw new InvalidOperationException("Incorrect block metadata rejection: " + mutation + "\n" + result.Output); @@ -280,7 +286,6 @@ string Fault(string args, string rule, string state = "memory") => } queue.Flush(); -CheckPointerEvidence(); Console.WriteLine($"PASS {cases} block memory checks and universal overrun rejection"); diff --git a/Tests/BodyDecoding.lean b/Tests/BodyDecoding.lean new file mode 100644 index 000000000..a8ed503d5 --- /dev/null +++ b/Tests/BodyDecoding.lean @@ -0,0 +1,672 @@ +import Tests.BodyEncoding +import Tests.MetadataWorld + +/-! +# Method body decoding tests + +Hand-made ECMA-335 bodies with tiny and fat headers, short and long branches, a switch, +two-byte opcodes, token operands and float constants must decode to the expected `RawMethod` +fields, with every token operand, the locals and the method definition checked against the +metadata of a hand-built assembly. Each rejection changes one fact about a body that matches and +asserts the error or mismatch it causes. The opcode table is compared in +`operand_kinds_agree_with_cecil` with a static list generated once from `Mono.Cecil.Cil.OpCodes`; +no test reads Cecil at run time. +-/ + +namespace Tests.BodyDecoding + +open CIL CIL.Extraction Tests.BodyEncoding Tests.MetadataBuilder Tests.MetadataWorld + +/-- The assembly whose metadata the bodies are checked against. -/ +def pin : AssemblyPin := { identity := ownIdentity } + +/-- A decoded method with the given body fields: the world's `Main`, a public static method + with no parameters. -/ +def method (maxStack : Nat) (initLocals : Bool) (locals : List TypeExpr) + (code : List RawInstruction) : RawMethod := + { token := tokens.main, owner := ownIdentity ++ "::Program", hasThis := false, + explicitThis := false, callingConvention := 0, attributes := 0x96, implementation := 0, + returnType := .void, parameters := [], locals := locals, initLocals := initLocals, + maxStack := maxStack, genericParameters := [], code := code } + +/-- A body with the given bytes and token claims, with the metadata of the world's `Main`; its + names are those of every test method, which all sit in `Main`'s class. -/ +private def evidence (bytes : List Nat) (operands : List (Nat × RawOperand) := []) : + BodyEvidence := + evidenceOf bytes operands { own := own.1, others := [lib] } (method 8 false [] []) worldPlatform + +/-- One decoded instruction. -/ +def instr (offset opcode : Nat) (operand : RawOperand := .none) : RawInstruction := + { offset := offset, opcode := opcode, operand := operand } + +/-- The four little-endian bytes of a token. -/ +private def token (value : Nat) : List Nat := (Tests.BodyEncoding.littleEndian 4 value) + +/-! ## Bodies that decode -/ + +/-- Tiny body: `ldarg.0; ldarg.1; add; ret`. -/ +private def tinyBytes : List Nat := [0x12, 0x02, 0x03, 0x58, 0x2A] + +/-- The decoded `tinyBytes`; a tiny header fixes the stack depth at 8 and has no locals. -/ +private def tinyMethod : RawMethod := + method 8 false [] [instr 0 0x02, instr 1 0x03, instr 2 0x58, instr 3 0x2A] + +theorem tiny_header_decodes : bodyMatches (evidence tinyBytes) tinyMethod [] = true := by + decide +kernel +#print axioms tiny_header_decodes + +/-- Fat body with one local: `ldc.i4.s -3; stloc.0; ldloc.s 0; ldc.i4 1000; add; ret`. -/ +private def fatBytes : List Nat := + [0x13, 0x30, 0x02, 0x00, 12, 0, 0, 0, 0x01, 0x00, 0x00, 0x11, + 0x1F, 0xFD, 0x0A, 0x11, 0x00, 0x20, 0xE8, 0x03, 0x00, 0x00, 0x58, 0x2A] + +/-- The decoded `fatBytes`: the signed byte, the macro `stloc.0` and the short local index. -/ +private def fatMethod : RawMethod := + method 2 true [uint64] [instr 0 0x1F (.integer (-3)), instr 2 0x0A, instr 3 0x11 (.local 0), + instr 5 0x20 (.integer 1000), instr 10 0x58, instr 11 0x2A] + +/-- `fatBytes`, whose signature token `0x11000001` names the world's one `uint64` local. -/ +private def fatEvidence : BodyEvidence := evidence fatBytes + +theorem fat_header_decodes : bodyMatches fatEvidence fatMethod [] = true := by decide +kernel +#print axioms fat_header_decodes + +/-- Short and long branches, forward and backward; targets are instruction indices. -/ +private def branchBytes : List Nat := + [0x3A, 0x02, 0x2C, 0x04, 0x17, 0x2A, 0x2B, 0xFD, 0x16, 0x38, 0xF3, 0xFF, 0xFF, 0xFF, 0x2A] + +/-- The decoded `branchBytes`: short and long forms, with indices in place of byte offsets. -/ +private def branchMethod : RawMethod := + method 8 false [] [instr 0 0x02, instr 1 0x2C (.target 5), instr 3 0x17, instr 4 0x2A, + instr 5 0x2B (.target 3), instr 7 0x16, instr 8 0x38 (.target 0), instr 13 0x2A] + +theorem branches_decode_to_instruction_indices : + bodyMatches (evidence branchBytes) branchMethod [] = true := by decide +kernel +#print axioms branches_decode_to_instruction_indices + +/-- A `switch` whose targets are measured from the end of its table. -/ +private def switchBytes : List Nat := + [0x52, 0x02, 0x45, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0xEF, 0xFF, 0xFF, 0xFF, 0x2A, 0x2A] + +/-- The decoded `switchBytes`: the case targets are instruction indices. -/ +private def switchMethod : RawMethod := + method 8 false [] [instr 0 0x02, instr 1 0x45 (.targets [2, 3, 1]), instr 18 0x2A, instr 19 0x2A] + +theorem switch_decodes_to_instruction_indices : + bodyMatches (evidence switchBytes) switchMethod [] = true := by decide +kernel +#print axioms switch_decodes_to_instruction_indices + +/-- Repeated destinations retain their positions in the decoded switch table. -/ +theorem switch_preserves_repeated_targets : + bodyMatches (evidence [0x52, 0x02, 0x45, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0x2A, 0x2A]) + (method 8 false [] [instr 0 0x02, instr 1 0x45 (.targets [3, 2, 3]), + instr 18 0x2A, instr 19 0x2A]) [] = true := by decide +kernel +#print axioms switch_preserves_repeated_targets + +/-- Two-byte opcodes `0xFE nn`: `ceq`, a long local and the unsigned `unaligned.` prefix. -/ +private def twoByteBytes : List Nat := + [0x36, 0x02, 0x03, 0xFE, 0x01, 0xFE, 0x0D, 0x05, 0x00, 0xFE, 0x12, 0xFF, 0x2A, 0x2A] + +/-- The decoded `twoByteBytes`. -/ +private def twoByteMethod : RawMethod := + method 8 false [] [instr 0 0x02, instr 1 0x03, instr 2 0xFE01, instr 4 0xFE0D (.local 5), + instr 8 0xFE12 (.integer 255), instr 11 0x2A, instr 12 0x2A] + +theorem two_byte_opcodes_decode : + bodyMatches (evidence twoByteBytes) twoByteMethod [] = true := by decide +kernel +#print axioms two_byte_opcodes_decode + +/-- A method token, a user string, a type token, and float constants of both widths: + `throw; call; ldstr; ldtoken; ldc.r4; ldc.r8; ret`. -/ +private def tokenBytes : List Nat := + [0x7A, 0x28] ++ token tokens.writeLine ++ [0x72] ++ token tokens.hello ++ [0xD0] ++ + token tokens.objectRef ++ [0x22, 0x00, 0x00, 0x80, 0xBF, 0x23, 0, 0, 0, 0, 0, 0, 0, 0xC0, 0x2A] + +/-- The system type `ldtoken` names in `tokenBytes`. -/ +def objectType : TypeExpr := .atom runtimeScope "System.Object" + +/-- The token table of `tokenBytes`: a method, a user string and a type. -/ +private def tokenOperands : List (Nat × RawOperand) := + [(tokens.writeLine, .call writeLineSite), (tokens.hello, .text "hi"), + (tokens.objectRef, .type objectType)] + +/-- The decoded `tokenBytes`; float constants are the bit patterns of the encoded values. -/ +private def tokenMethod : RawMethod := + method 8 false [] [instr 0 0x28 (.call writeLineSite), instr 5 0x72 (.text "hi"), + instr 10 0xD0 (.type objectType), instr 15 0x22 (.text "float:-1082130432"), + instr 20 0x23 (.text "double:-4611686018427387904"), + instr 29 0x2A] + +theorem token_operands_resolve_through_the_table : + bodyMatches (evidence tokenBytes tokenOperands) tokenMethod [] = true := by decide +kernel +#print axioms token_operands_resolve_through_the_table + +/-- The other operand encodings: long argument, `ldc.i8`, `no.`, `calli`, an instance field and + `ldtoken` of a method and of a static field. -/ +private def formsBytes : List Nat := + [0x9E, 0xFE, 0x09, 0x03, 0x00, 0x0E, 0x02, 0x21, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFE, 0x19, 0x07, 0x29] ++ token tokens.indirect ++ [0x7B] ++ token tokens.countField ++ + [0xD0] ++ token tokens.writeLine ++ [0xD0] ++ token tokens.totalField ++ [0x2A] + +/-- The instance field `ldfld` names in `formsBytes`. -/ +private def someField : RawOperand := countOperand + +/-- The static field `ldtoken` names in `formsBytes`. -/ +private def staticField : RawOperand := + .field (.atom ownIdentity "Program") 1 int32 name!"total" true + +/-- The signature `calli` names in `formsBytes`. -/ +private def someSignature : RawOperand := .indirectCall .void [int32] false false 0 + +/-- The token table of `formsBytes`. -/ +private def formsOperands : List (Nat × RawOperand) := + [(tokens.indirect, someSignature), (tokens.countField, someField), + (tokens.writeLine, .call writeLineSite), (tokens.totalField, staticField)] + +/-- The decoded `formsBytes`. -/ +private def formsMethod : RawMethod := + method 8 false [] [instr 0 0xFE09 (.argument 3), instr 4 0x0E (.argument 2), + instr 6 0x21 (.integer (-2)), instr 15 0xFE19 (.integer 7), instr 18 0x29 someSignature, + instr 23 0x7B someField, instr 28 0xD0 (.call writeLineSite), instr 33 0xD0 staticField, + instr 38 0x2A] + +/-- The forms decode with the static field `ldtoken` recorded at its index, and not without it. -/ +theorem other_operand_forms_decode : + bodyMatches { evidence formsBytes formsOperands with statics := [(7, tokens.totalField)] } + formsMethod [] = true ∧ + bodyMatches (evidence formsBytes formsOperands) formsMethod [] = false := by decide +kernel +#print axioms other_operand_forms_decode + +/-- A branch to a `volatile.` prefix enters the prefixed instruction at its first prefix. -/ +theorem branch_to_a_prefix_decodes : + bodyMatches (evidence [0x1A, 0x2B, 0x00, 0xFE, 0x13, 0x4A, 0x2A]) + (method 8 false [] [instr 0 0x2B (.target 1), instr 2 0xFE13, instr 4 0x4A, + instr 5 0x2A]) [] = true := by decide +kernel +#print axioms branch_to_a_prefix_decodes + +/-! ## The test encoder agrees with the hand-written bytes -/ + +/-- The encoder writes the bytes written out by hand above, and the metadata it writes makes the + body decode to the method. -/ +theorem encoder_reproduces_the_hand_written_bodies : + (encodeBody pin tinyMethod).map (·.bytes) = some tinyBytes ∧ + (encodeBody pin fatMethod).map (·.bytes) = some fatBytes ∧ + (encodeBody pin branchMethod).map (·.bytes) = some branchBytes ∧ + (encodeBody pin switchMethod).map (·.bytes) = some switchBytes ∧ + (encodeBody pin twoByteMethod).map (·.bytes) = some twoByteBytes ∧ + (encodeBody pin tinyMethod).any (bodyMatches · tinyMethod []) = true ∧ + (encodeBody pin fatMethod).any (bodyMatches · fatMethod []) = true ∧ + (encodeBody pin twoByteMethod).any (bodyMatches · twoByteMethod []) = true := by + decide +kernel +#print axioms encoder_reproduces_the_hand_written_bodies + +/-! ## Bodies that must not match -/ + +/-- A method that every test body is checked against unless the test names another. -/ +private def plain : RawMethod := method 8 false [] [] + +/-- Whether the body fails to decode with exactly this error. -/ +private def failsWith (body : BodyEvidence) (error : BodyError) (method : RawMethod := plain) : + Bool := + match checkBody body method [] with + | .error found => found == error + | .ok _ => false + +/-- Whether the body decodes but to something other than the method: the failure is the + mismatch, not a decoding error. -/ +private def mismatches (body : BodyEvidence) (method : RawMethod) : Bool := + match checkBody body method [] with + | .ok _ => !bodyMatches body method [] + | .error _ => false + +/-- One opcode byte changed: `sub` where the decoded method says `add`. -/ +theorem rejects_changed_opcode_byte : + mismatches (evidence [0x12, 0x02, 0x03, 0x59, 0x2A]) tinyMethod = true := by decide +kernel +#print axioms rejects_changed_opcode_byte + +/-- One operand byte changed: the signed byte is -4 where the decoded method says -3. -/ +theorem rejects_changed_operand_byte : + mismatches (evidence (fatBytes.set 13 0xFC)) fatMethod = true := by decide +kernel +#print axioms rejects_changed_operand_byte + +/-- A decoded instruction with a wrong byte offset. -/ +theorem rejects_wrong_instruction_offset : + mismatches (evidence tinyBytes) + (method 8 false [] [instr 0 0x02, instr 1 0x03, instr 3 0x58, instr 3 0x2A]) = true := by + decide +kernel +#print axioms rejects_wrong_instruction_offset + +/-- A decoded branch naming another instruction index than its bytes reach. -/ +theorem rejects_wrong_branch_index : + mismatches (evidence branchBytes) + (method 8 false [] [instr 0 0x02, instr 1 0x2C (.target 6), instr 3 0x17, instr 4 0x2A, + instr 5 0x2B (.target 3), instr 7 0x16, instr 8 0x38 (.target 0), instr 13 0x2A]) = + true := by + decide +kernel +#print axioms rejects_wrong_branch_index + +/-- `brfalse.s` to byte 1, its own operand: no instruction starts there. -/ +private def midInstructionBytes : List Nat := [0x0E, 0x2C, 0xFF, 0x2A] + +theorem rejects_branch_into_an_instruction : + failsWith (evidence midInstructionBytes) (.branchIntoInstruction 0 1) = true := by + decide +kernel +#print axioms rejects_branch_into_an_instruction + +/-- The check is `false` for the same body, whatever instructions the method claims. -/ +theorem branch_into_an_instruction_does_not_match : + bodyMatches (evidence midInstructionBytes) + (method 8 false [] [instr 0 0x2C (.target 1), instr 2 0x2A]) [] = false := by + decide +kernel +#print axioms branch_into_an_instruction_does_not_match + +/-- A branch to the end of the code reaches no instruction. -/ +theorem rejects_branch_to_the_end_of_the_body : + failsWith (evidence [0x0E, 0x2B, 0x01, 0x2A]) (.branchOutsideBody 0) = true := by + decide +kernel +#print axioms rejects_branch_to_the_end_of_the_body + +/-- A branch past the end of the code. -/ +theorem rejects_branch_past_the_body : + failsWith (evidence [0x0E, 0x2B, 0x05, 0x2A]) (.branchOutsideBody 0) = true := by + decide +kernel +#print axioms rejects_branch_past_the_body + +/-- A branch before the first byte. -/ +theorem rejects_branch_before_the_body : + failsWith (evidence [0x0A, 0x2B, 0xF0]) (.branchOutsideBody 0) = true := by decide +kernel +#print axioms rejects_branch_before_the_body + +/-- A branch to the instruction after a `volatile.` prefix: control may enter a prefixed + instruction only at its first prefix. -/ +theorem rejects_branch_to_the_instruction_after_a_prefix : + failsWith (evidence [0x1A, 0x2B, 0x02, 0xFE, 0x13, 0x4A, 0x2A]) (.branchIntoPrefixed 0 4) = + true := by + decide +kernel +#print axioms rejects_branch_to_the_instruction_after_a_prefix + +/-- A branch after the final `ret`, which no path reaches, into the instruction after a + `volatile.` prefix: the rule covers every branch of the body, so decoded code always + satisfies `PrefixEntryFree`. -/ +theorem rejects_unreachable_branch_after_a_prefix : + failsWith (evidence [0x1E, 0x2A, 0xFE, 0x13, 0x4A, 0x2A, 0x2B, 0xFC]) + (.branchIntoPrefixed 5 3) = true := by + decide +kernel +#print axioms rejects_unreachable_branch_after_a_prefix + +/-- A `switch` case that reaches the instruction after a `readonly.` prefix. -/ +theorem rejects_switch_target_after_a_prefix : + failsWith (evidence [0x36, 0x45, 0x01, 0, 0, 0, 0x03, 0, 0, 0, 0x2A, 0xFE, 0x1E, 0x2A]) + (.branchIntoPrefixed 0 12) = true := by + decide +kernel +#print axioms rejects_switch_target_after_a_prefix + +/-- The table resolves a token to another operand than the metadata names: the call has a + parameter more than the member the token names. -/ +theorem rejects_token_with_different_operand : + failsWith (evidence tokenBytes + [(tokens.writeLine, .call { writeLineSite with parameterCount := 2 }), + (tokens.hello, .text "hi"), (tokens.objectRef, .type objectType)]) + (.metadata (.operandMismatch tokens.writeLine)) = true := by decide +kernel +#print axioms rejects_token_with_different_operand + +/-- A field operand that names another field than the row its token names: `ldfld` of the count + field with the name `total`. -/ +theorem rejects_a_field_operand_named_for_another_row : + failsWith (evidence ([0x16, 0x7B] ++ token tokens.countField) + [(tokens.countField, .field (.atom ownIdentity "Program") 0 int32 name!"total" false)]) + (.metadata (.operandMismatch tokens.countField)) = true := by decide +kernel +#print axioms rejects_a_field_operand_named_for_another_row + +/-- A token missing from the table fails the check. -/ +theorem rejects_missing_token : + failsWith (evidence tokenBytes [(tokens.writeLine, .call writeLineSite), + (tokens.hello, .text "hi")]) (.missingToken 10 tokens.objectRef) = true := by + decide +kernel +#print axioms rejects_missing_token + +/-- A call whose token names a type reference, which the table resolves to a type: the entry + matches its metadata, so the failure is the table of the token. -/ +theorem rejects_method_operand_from_the_wrong_table : + failsWith (evidence ([0x16, 0x28] ++ token tokens.objectRef) + [(tokens.objectRef, .type objectType)]) (.mismatchedToken 0 tokens.objectRef) = true := by + decide +kernel +#print axioms rejects_method_operand_from_the_wrong_table + +/-- A user-string operand whose token names a type reference. -/ +theorem rejects_string_operand_from_the_wrong_table : + failsWith (evidence ([0x16, 0x72] ++ token tokens.objectRef) + [(tokens.objectRef, .type objectType)]) (.mismatchedToken 0 tokens.objectRef) = true := by + decide +kernel +#print axioms rejects_string_operand_from_the_wrong_table + +/-- A token the table resolves to an operand the opcode cannot take. -/ +theorem rejects_token_with_the_wrong_operand_kind : + failsWith (evidence ([0x16, 0x28] ++ token tokens.hello) [(tokens.hello, .text "hi")]) + (.mismatchedToken 0 tokens.hello) = true := by + decide +kernel +#print axioms rejects_token_with_the_wrong_operand_kind + +/-- `ldsfld` of a token that names an instance field. -/ +theorem rejects_a_static_load_of_an_instance_field : + failsWith (evidence ([0x16, 0x7E] ++ token tokens.countField) + [(tokens.countField, someField)]) (.mismatchedToken 0 tokens.countField) = true := by + decide +kernel +#print axioms rejects_a_static_load_of_an_instance_field + +/-- A token that has two entries in the table. -/ +theorem rejects_a_token_with_two_entries : + failsWith (evidence tokenBytes (tokenOperands ++ [(tokens.hello, .text "hi")])) + (.duplicateToken tokens.hello) = true := by decide +kernel +#print axioms rejects_a_token_with_two_entries + +/-- The last operand byte is missing although the header agrees with the bytes. -/ +theorem rejects_truncated_operand : + failsWith (evidence [0x0E, 0x20, 0x01, 0x00]) (.truncatedOperand 0) = true := by + decide +kernel +#print axioms rejects_truncated_operand + +/-- A byte missing from the end leaves the declared code size unmet. -/ +theorem rejects_truncated_bytes : + failsWith (evidence [0x12, 0x02, 0x03, 0x58]) (.codeSizeMismatch 4 3) = true := by + decide +kernel +#print axioms rejects_truncated_bytes + +/-- A fat header cut short. -/ +theorem rejects_truncated_header : + failsWith (evidence [0x13, 0x30, 0x02, 0x00]) .truncatedHeader = true := by decide +kernel +#print axioms rejects_truncated_header + +/-- A fat header whose size is four words. -/ +theorem rejects_a_fat_header_of_four_words : + failsWith (evidence [0x03, 0x40, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0]) (.unsupportedHeader 3) = + true := by + decide +kernel +#print axioms rejects_a_fat_header_of_four_words + +/-- A fat header with a reserved flag bit. -/ +theorem rejects_a_reserved_header_flag : + failsWith (evidence [0x23, 0x30, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0]) (.unsupportedHeader 35) = + true := by + decide +kernel +#print axioms rejects_a_reserved_header_flag + +/-- A declared code size that is not the number of code bytes. -/ +theorem rejects_wrong_code_size : + failsWith (evidence [0x16, 0x02, 0x03, 0x58, 0x2A]) (.codeSizeMismatch 5 4) = true := by + decide +kernel +#print axioms rejects_wrong_code_size + +/-- The method claims another stack depth than the header declares. -/ +theorem rejects_wrong_max_stack : + mismatches fatEvidence { fatMethod with maxStack := 3 } = true := by decide +kernel +#print axioms rejects_wrong_max_stack + +/-- The header sets the init-locals flag and the method claims it clear. -/ +theorem rejects_init_locals_claimed_clear : + mismatches fatEvidence { fatMethod with initLocals := false } = true := by decide +kernel +#print axioms rejects_init_locals_claimed_clear + +/-- The tiny header has no init-locals flag and the method claims it set. -/ +theorem rejects_init_locals_claimed_set : + mismatches (evidence tinyBytes) { tinyMethod with initLocals := true } = true := by + decide +kernel +#print axioms rejects_init_locals_claimed_set + +/-- A fat header whose flags also set the extra-sections bit, as a body with handlers does. -/ +theorem rejects_body_with_exception_section : + failsWith (evidence (0x1B :: fatBytes.tail)) .extraSections fatMethod = true := by + decide +kernel +#print axioms rejects_body_with_exception_section + +/-- A six-byte body with a two-byte alignment gap before its small exception section. + The handler pops the caught object and leaves to the return at byte offset five. -/ +private def catchBytes : List Nat := + [0x0B, 0x30, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, + 0x14, 0x7A, 0x26, 0xDE, 0, 0x2A, 0, 0, + 1, 16, 0, 0, 0, 0, 0, 0, 2, 2, 0, 3] ++ token tokens.boxRef + +/-- Decode the catch and its branch destinations against the fixture's pinned metadata. -/ +private def readCatch (bytes : List Nat) (caught := TypeExpr.object libScope "Lib.Box") : + Except BodyError (List RawInstruction × CatchClause) := do + let body := evidence bytes + let env ← body.environment.mapError BodyError.metadata + let (decoded, clause) ← decodeSingleCatch env 0 0 body caught + return (decoded.code, clause) + +/-- The catch retains the class token and half-open byte ranges. The leave targets instruction + four. Executable body decoding still refuses the exception section at the same bytes. -/ +theorem single_catch_binds_code_and_class : + readCatch catchBytes = .ok ([instr 0 0x14, instr 1 0x7A, instr 2 0x26, + instr 3 0xDE (.target 4), instr 5 0x2A], ⟨0, 2, 2, 5, tokens.boxRef⟩) ∧ + failsWith (evidence catchBytes) .extraSections fatMethod = true := + ⟨by cil_kernel_rfl, by decide +kernel⟩ +#print axioms single_catch_binds_code_and_class + +/-- Catch clauses must be complete, disjoint and instruction-aligned. The header, section + kind, clause flags, class token and resolved type all bind independently. -/ +theorem malformed_catch_refused : + ([catchBytes.set 0 3, catchBytes.set 20 0x81, catchBytes.set 21 15, catchBytes.set 24 2, + catchBytes.set 28 0, catchBytes.set 29 1, catchBytes.set 31 2, catchBytes.set 31 5, + catchBytes.set 35 6, catchBytes.take 35, catchBytes ++ [0]].all fun bytes => + match readCatch bytes with | .error .invalidCatchSection => true | _ => false) = true ∧ + readCatch catchBytes (.object ownIdentity "Program") = + .error (.metadata (.operandMismatch tokens.boxRef)) := + ⟨by decide +kernel, by cil_kernel_rfl⟩ +#print axioms malformed_catch_refused + +/-- In `ldarg.0; volatile.; ldind.i4`, the prefix starts at byte one and the load at three. + A protected range may start at the prefix, but cannot skip it by starting at the load. -/ +theorem catch_boundary_respects_prefixes : + let bytes := [0x0B, 0x30, 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, + 2, 0xFE, 0x13, 0x4A, 0x26, 0x14, 0x7A, 0x26, 0xDE, 0, 0x2A, 0, + 1, 16, 0, 0, 0, 0, 1, 0, 6, 7, 0, 3] ++ token tokens.boxRef + ((readCatch bytes).toOption.map Prod.snd = some ⟨1, 7, 7, 10, tokens.boxRef⟩) ∧ + readCatch ((bytes.set 30 3).set 32 4) = .error .invalidCatchSection := by + exact ⟨by cil_kernel_rfl, by cil_kernel_rfl⟩ +#print axioms catch_boundary_respects_prefixes + +/-- A body with an exception section does not match the method that has the same code. -/ +theorem body_with_exception_section_does_not_match : + bodyMatches (evidence (0x1B :: fatBytes.tail)) fatMethod [] = false := by + decide +kernel +#print axioms body_with_exception_section_does_not_match + +/-- The method's locals differ from the types the signature token names. -/ +theorem rejects_wrong_locals : + failsWith fatEvidence (.metadata (.localMismatch 0)) { fatMethod with locals := [] } = true ∧ + failsWith fatEvidence (.metadata (.localMismatch 0)) { fatMethod with locals := [uint32] } = + true := by decide +kernel +#print axioms rejects_wrong_locals + +/-- The header names a standalone signature that is not a local variable signature. -/ +theorem rejects_a_local_signature_token_that_names_a_call_signature : + failsWith (evidence (fatBytes.set 8 0x02)) (.metadata (.unsupportedSignature 0 0)) fatMethod = + true := by + decide +kernel +#print axioms rejects_a_local_signature_token_that_names_a_call_signature + +/-- A tiny header names no locals, so a method that claims some is refused. -/ +theorem rejects_locals_without_a_signature : + failsWith (evidence tinyBytes) (.metadata (.localMismatch 0)) + { tinyMethod with locals := [int32] } = true := by + decide +kernel +#print axioms rejects_locals_without_a_signature + +/-- A method whose attributes differ from its `MethodDef` row. -/ +theorem rejects_a_method_with_other_attributes : + failsWith (evidence tinyBytes) (.metadata (.methodMismatch tokens.main)) + { tinyMethod with attributes := 0x97 } = true := by decide +kernel +#print axioms rejects_a_method_with_other_attributes + +/-- A pin of another version than the method's own assembly states fails the conjunct of + `BodyDecodes` that equates the two, so the pin the method is attributed to must be the + assembly its rows came from. -/ +theorem rejects_a_pin_of_another_version (pins : List AssemblyPin) (method : RawMethod) + (arguments : List TypeExpr) (signature : String) : + ¬ BodyDecodes pins {} + { identity := "T, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" } + (evidence tinyBytes) method arguments signature := + fun decodes => absurd decodes.2.2.1 (by decide) +#print axioms rejects_a_pin_of_another_version + +/-- Names the metadata does not resolve to: the body check refuses an owner or base class the + evidence lists that the rows do not give. -/ +theorem rejects_names_the_metadata_does_not_give : + failsWith { evidence tinyBytes with names := { owner := [65] } } + (.metadata (.methodMismatch tokens.main)) tinyMethod = true ∧ + failsWith { evidence tinyBytes with + names := { (evidence tinyBytes).names with bases := [([65], [66])] } } + (.metadata (.methodMismatch tokens.main)) tinyMethod = true := by decide +kernel +#print axioms rejects_names_the_metadata_does_not_give + +/-- The names the metadata gives are tied to the strings the method states: another owner string + is not what the bytes spell. -/ +theorem rejects_an_owner_string_the_names_do_not_spell : + ¬ (evidence tinyBytes).names.Spell { tinyMethod with owner := ownIdentity ++ "::Other" } := by + decide +#print axioms rejects_an_owner_string_the_names_do_not_spell + +/-- Byte `0x24` is not an opcode. -/ +theorem rejects_unknown_opcode : + failsWith (evidence [0x06, 0x24]) (.unknownOpcode 0 0x24) = true := by decide +kernel +#print axioms rejects_unknown_opcode + +/-- A prefix byte `0xFE` with nothing after it. -/ +theorem rejects_dangling_prefix : + failsWith (evidence [0x06, 0xFE]) (.truncatedOpcode 0) = true := by decide +kernel +#print axioms rejects_dangling_prefix + +/-- A `switch` that declares `0xFFFFFFFF` cases and carries none. -/ +theorem rejects_a_switch_without_its_cases : + failsWith (evidence [0x16, 0x45, 0xFF, 0xFF, 0xFF, 0xFF]) (.truncatedOperand 0) = true := by + decide +kernel +#print axioms rejects_a_switch_without_its_cases + +/-- Only bytes can be body bytes. -/ +theorem rejects_a_value_above_255 : + failsWith (evidence [0x06, 0x100]) .invalidByte = true := by decide +kernel +#print axioms rejects_a_value_above_255 +/-! ## The opcode table -/ + +/-- Every opcode of `Mono.Cecil.Cil.OpCodes`, listed once from Cecil's table, with the operand + encoding its operand type implies: `InlineNone` is `.none`, `ShortInlineI` is `.signed8` + for `ldc.i4.s` and `.unsigned8` for `unaligned.` and `no.`, and so on. -/ +private def cecilOpcodes : List (Nat × OperandKind) := [ + (0x00, .none), (0x01, .none), (0x02, .none), (0x03, .none), + (0x04, .none), (0x05, .none), (0x06, .none), (0x07, .none), + (0x08, .none), (0x09, .none), (0x0A, .none), (0x0B, .none), + (0x0C, .none), (0x0D, .none), (0x0E, .shortArgument), (0x0F, .shortArgument), + (0x10, .shortArgument), (0x11, .shortLocal), (0x12, .shortLocal), (0x13, .shortLocal), + (0x14, .none), (0x15, .none), (0x16, .none), (0x17, .none), + (0x18, .none), (0x19, .none), (0x1A, .none), (0x1B, .none), + (0x1C, .none), (0x1D, .none), (0x1E, .none), (0x1F, .signed8), + (0x20, .signed32), (0x21, .signed64), (0x22, .float32), (0x23, .float64), + (0x25, .none), (0x26, .none), (0x27, .method), (0x28, .method), + (0x29, .signature), (0x2A, .none), (0x2B, .shortBranch), (0x2C, .shortBranch), + (0x2D, .shortBranch), (0x2E, .shortBranch), (0x2F, .shortBranch), (0x30, .shortBranch), + (0x31, .shortBranch), (0x32, .shortBranch), (0x33, .shortBranch), (0x34, .shortBranch), + (0x35, .shortBranch), (0x36, .shortBranch), (0x37, .shortBranch), (0x38, .longBranch), + (0x39, .longBranch), (0x3A, .longBranch), (0x3B, .longBranch), (0x3C, .longBranch), + (0x3D, .longBranch), (0x3E, .longBranch), (0x3F, .longBranch), (0x40, .longBranch), + (0x41, .longBranch), (0x42, .longBranch), (0x43, .longBranch), (0x44, .longBranch), + (0x45, .switch), (0x46, .none), (0x47, .none), (0x48, .none), + (0x49, .none), (0x4A, .none), (0x4B, .none), (0x4C, .none), + (0x4D, .none), (0x4E, .none), (0x4F, .none), (0x50, .none), + (0x51, .none), (0x52, .none), (0x53, .none), (0x54, .none), + (0x55, .none), (0x56, .none), (0x57, .none), (0x58, .none), + (0x59, .none), (0x5A, .none), (0x5B, .none), (0x5C, .none), + (0x5D, .none), (0x5E, .none), (0x5F, .none), (0x60, .none), + (0x61, .none), (0x62, .none), (0x63, .none), (0x64, .none), + (0x65, .none), (0x66, .none), (0x67, .none), (0x68, .none), + (0x69, .none), (0x6A, .none), (0x6B, .none), (0x6C, .none), + (0x6D, .none), (0x6E, .none), (0x6F, .method), (0x70, .type), + (0x71, .type), (0x72, .string), (0x73, .method), (0x74, .type), + (0x75, .type), (0x76, .none), (0x79, .type), (0x7A, .none), + (0x7B, .field), (0x7C, .field), (0x7D, .field), (0x7E, .field), + (0x7F, .field), (0x80, .field), (0x81, .type), (0x82, .none), + (0x83, .none), (0x84, .none), (0x85, .none), (0x86, .none), + (0x87, .none), (0x88, .none), (0x89, .none), (0x8A, .none), + (0x8B, .none), (0x8C, .type), (0x8D, .type), (0x8E, .none), + (0x8F, .type), (0x90, .none), (0x91, .none), (0x92, .none), + (0x93, .none), (0x94, .none), (0x95, .none), (0x96, .none), + (0x97, .none), (0x98, .none), (0x99, .none), (0x9A, .none), + (0x9B, .none), (0x9C, .none), (0x9D, .none), (0x9E, .none), + (0x9F, .none), (0xA0, .none), (0xA1, .none), (0xA2, .none), + (0xA3, .type), (0xA4, .type), (0xA5, .type), (0xB3, .none), + (0xB4, .none), (0xB5, .none), (0xB6, .none), (0xB7, .none), + (0xB8, .none), (0xB9, .none), (0xBA, .none), (0xC2, .type), + (0xC3, .none), (0xC6, .type), (0xD0, .token), (0xD1, .none), + (0xD2, .none), (0xD3, .none), (0xD4, .none), (0xD5, .none), + (0xD6, .none), (0xD7, .none), (0xD8, .none), (0xD9, .none), + (0xDA, .none), (0xDB, .none), (0xDC, .none), (0xDD, .longBranch), + (0xDE, .shortBranch), (0xDF, .none), (0xE0, .none), (0xFE00, .none), + (0xFE01, .none), (0xFE02, .none), (0xFE03, .none), (0xFE04, .none), + (0xFE05, .none), (0xFE06, .method), (0xFE07, .method), (0xFE09, .longArgument), + (0xFE0A, .longArgument), (0xFE0B, .longArgument), (0xFE0C, .longLocal), (0xFE0D, .longLocal), + (0xFE0E, .longLocal), (0xFE0F, .none), (0xFE11, .none), (0xFE12, .unsigned8), + (0xFE13, .none), (0xFE14, .none), (0xFE15, .type), (0xFE16, .type), + (0xFE17, .none), (0xFE18, .none), (0xFE19, .unsigned8), (0xFE1A, .none), + (0xFE1C, .type), (0xFE1D, .none), (0xFE1E, .none) +] + +/-- The opcodes that have an operand encoding: every one-byte value and every `0xFE nn`. -/ +private def candidates : List Nat := List.range 256 ++ (List.range 256).map (0xFE00 + ·) + +theorem operand_kinds_agree_with_cecil : + cecilOpcodes.all (fun (opcode, kind) => operandKind opcode == some kind) = true ∧ + (candidates.filter fun opcode => (operandKind opcode).isSome).length = + cecilOpcodes.length := by + decide +kernel +#print axioms operand_kinds_agree_with_cecil + +/-! ## The extraction's pins -/ + +/-- The pinned identities a body lists fail the conjunct of `BodyDecodes` that ties them to the + extraction's pins when they are left out or name another assembly. -/ +theorem rejects_pinned_identities_other_than_the_pins (method : RawMethod) + (arguments : List TypeExpr) (signature : String) : + ¬ BodyDecodes [pin] {} pin (evidence tinyBytes) method arguments signature ∧ + ¬ BodyDecodes [pin] {} pin + { evidence tinyBytes with pinned := PinnedIndex.ofIdentities [bytesOf "U"] } method arguments + signature := + ⟨fun decodes => absurd decodes.2.2.2.1 (by decide), + fun decodes => absurd decodes.2.2.2.1 (by decide +kernel)⟩ +#print axioms rejects_pinned_identities_other_than_the_pins + +/-- The own slice with its reference to `Lib` at version 2, which no carried slice is. -/ +private def ownWithLibVersion2 : MetadataSlice := + { own.1 with rows := own.1.rows.map fun (key, bytes) => + (key, if key == tokenOf Table.assemblyRef 2 then bytes.set 0 2 else bytes) } + +/-- Whether checking an operand in the environment the body check builds, with `Lib` pinned, + fails with exactly this error. -/ +private def rejectsWithLibPinned (ownSlice : MetadataSlice) (token : Nat) (operand : RawOperand) + (error : MetadataError) : Bool := + match (do + let env ← { own := ownSlice, others := [lib] : MethodMetadata }.environment + (PinnedIndex.ofIdentities [bytesOf libScope]) + checkEntry env 0 token operand) with + | .error found => found == error + | .ok _ => false + +/-- A reference to another version of a pinned assembly is refused unless that version's slice + is carried, so it is never read as a platform type. -/ +theorem rejects_another_version_of_a_pinned_assembly : + rejectsWithLibPinned ownWithLibVersion2 tokens.boxRef + (.type (.atom "Lib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" "Lib.Box")) + (.missingSlice tokens.boxRef) = true ∧ + rejectsWithLibPinned own.1 tokens.boxRef (.type (.atom libScope "Lib.Box")) + (.missingSlice tokens.boxRef) = false := by decide +kernel +#print axioms rejects_another_version_of_a_pinned_assembly + +/-- Tables that do not match the slices in number are refused before any decoding. -/ +theorem rejects_tables_that_do_not_match_the_slices_in_number : + failsWith { evidence tinyBytes with tables := [] } (.metadata .tablesMismatch) tinyMethod = + true ∧ + failsWith { evidence tinyBytes with identities := [] } (.metadata .tablesMismatch) + tinyMethod = true := by decide +kernel +#print axioms rejects_tables_that_do_not_match_the_slices_in_number + +end Tests.BodyDecoding diff --git a/Tests/BodyEncoding.lean b/Tests/BodyEncoding.lean new file mode 100644 index 000000000..8b75c7f42 --- /dev/null +++ b/Tests/BodyEncoding.lean @@ -0,0 +1,228 @@ +import CIL.Extraction.Translation +import Tests.MetadataBuilder + +/-! +# Method body encoding for tests + +Fixtures that build a `RawMethod` by hand need method-body bytes and metadata that decode to +it, since `MethodEvidenceValid` checks both. `encodeBody` writes the bytes of a method whose +operands carry no token and the metadata slice its definition and locals are checked against, +`realizeOffsets` gives its instructions the byte offsets those bytes imply, and `withBody` +applies both to a `MethodEvidence`. The encoder serves tests only: nothing trusts it, because the +decoder re-reads every byte it writes. It is irreducible: the elaborator would evaluate the state +builder by name, unsharing its states exponentially, so only `decide +kernel` evaluates it. + +The encoder handles integer, native integer, `string` and `object` types, references, pointers, +vectors and generic parameters; any other type, a token operand or a float constant gives `none`. +-/ + +namespace Tests.BodyEncoding + +open CIL CIL.Extraction Tests.MetadataBuilder + +/-- The token of the first standalone signature, which a method with locals uses. -/ +def localSignatureToken : Nat := 0x11000001 + +/-- The `width` little-endian bytes of the two's-complement encoding of `value`. -/ +def littleEndian (width : Nat) (value : Int) : List Nat := + let unsigned := (value % ((2 ^ (8 * width) : Nat) : Int)).toNat + (List.range width).map fun position => unsigned / 256 ^ position % 256 + +/-- The opcode bytes: one byte, or `0xFE` and the second byte for `0xFE00 + nn`. -/ +def opcodeBytes (opcode : Nat) : List Nat := + if 0xFE00 ≤ opcode then [0xFE, opcode - 0xFE00] else [opcode] + +/-- The byte width of an operand, or `none` when its shape does not suit the operand kind or it + needs a token. -/ +def operandWidth (kind : OperandKind) (operand : RawOperand) : Option Nat := + match kind, operand with + | .none, .none => some 0 + | .signed8, .integer _ | .unsigned8, .integer _ | .shortArgument, .argument _ + | .shortLocal, .local _ | .shortBranch, .target _ => some 1 + | .longArgument, .argument _ | .longLocal, .local _ => some 2 + | .signed32, .integer _ | .longBranch, .target _ => some 4 + | .signed64, .integer _ => some 8 + | .switch, .targets targets => some (4 + 4 * targets.length) + | _, _ => none + +/-- The byte size of one instruction. -/ +def instructionSize (instruction : RawInstruction) : Option Nat := do + let kind ← operandKind instruction.opcode + let width ← operandWidth kind instruction.operand + some ((opcodeBytes instruction.opcode).length + width) + +/-- The byte offset of each instruction when the first starts at `start`. -/ +def offsetsFrom (start : Nat) : List RawInstruction → Option (List Nat) + | [] => some [] + | instruction :: rest => do + let size ← instructionSize instruction + let tail ← offsetsFrom (start + size) rest + some (start :: tail) + +/-- The operand bytes of an instruction that ends at byte `following`. Branch operands are + relative to `following`. -/ +def encodeOperand (offsets : List Nat) (following : Nat) (kind : OperandKind) + (operand : RawOperand) : Option (List Nat) := + let relative (pc : Nat) : Option Int := + (offsets[pc]?).map fun target => (target : Int) - following + match kind, operand with + | .none, .none => some [] + | .signed8, .integer value | .unsigned8, .integer value => some (littleEndian 1 value) + | .signed32, .integer value => some (littleEndian 4 value) + | .signed64, .integer value => some (littleEndian 8 value) + | .shortArgument, .argument index | .shortLocal, .local index => some (littleEndian 1 index) + | .longArgument, .argument index | .longLocal, .local index => some (littleEndian 2 index) + | .shortBranch, .target pc => (relative pc).map fun value => littleEndian 1 value + | .longBranch, .target pc => (relative pc).map fun value => littleEndian 4 value + | .switch, .targets pcs => do + let values ← pcs.mapM relative + some (littleEndian 4 pcs.length ++ (values.map (littleEndian 4)).flatten) + | _, _ => none + +/-- The bytes of the instructions at the given offsets. -/ +def encodeCode (offsets : List Nat) : List (RawInstruction × Nat) → Option (List Nat) + | [] => some [] + | (instruction, offset) :: rest => do + let kind ← operandKind instruction.opcode + let width ← operandWidth kind instruction.operand + let following := offset + (opcodeBytes instruction.opcode).length + width + let operandBytes ← encodeOperand offsets following kind instruction.operand + let restBytes ← encodeCode offsets rest + some (opcodeBytes instruction.opcode ++ operandBytes ++ restBytes) + +/-! ## Metadata -/ + +/-- The element type byte of each primitive the encoder writes. -/ +def primitiveBytes : List (String × Nat) := + [("System.Boolean", 0x02), ("System.Char", 0x03), ("System.SByte", 0x04), ("System.Byte", 0x05), + ("System.Int16", 0x06), ("System.UInt16", 0x07), ("System.Int32", 0x08), + ("System.UInt32", 0x09), ("System.Int64", 0x0A), ("System.UInt64", 0x0B), + ("System.Single", 0x0C), ("System.Double", 0x0D), ("System.IntPtr", 0x18), + ("System.UIntPtr", 0x19), ("System.Object", 0x1C)] + +/-- The signature bytes of a type of a method with `arity` generic parameters. -/ +def typeBytes (arity : Nat) : TypeExpr → Option (List Nat) + | .void => some [0x01] + | .atom scope name => + if scope == runtimeScope then (primitiveBytes.lookup name).map fun code => [code] else none + | .object scope name => + if scope == runtimeScope && name == "System.String" then some [0x0E] else none + | .byref element => (0x10 :: ·) <$> typeBytes arity element + | .pointer element => (0x0F :: ·) <$> typeBytes arity element + | .array element => (0x1D :: ·) <$> typeBytes arity element + | .parameter number => some (if number < arity then [0x1E, number] else [0x13, number - arity]) + | _ => none + +/-- The signature blob of a method: convention, generic arity, parameter count, result and + parameters. -/ +def methodBytes (method : RawMethod) : Option (List Nat) := do + let arity := method.genericParameters.length + let convention := method.callingConvention + (if method.hasThis then 0x20 else 0) + + (if method.explicitThis then 0x40 else 0) + let result ← typeBytes arity method.returnType + let parameters ← method.parameters.mapM (typeBytes arity) + some ([convention] ++ (if method.callingConvention / 0x10 % 2 == 1 then [arity] else []) ++ + [parameters.length] ++ result ++ parameters.flatten) + +/-- The part of `owner` after `identity ++ "::"`. -/ +def ownerName (identity owner : List Nat) : Option (List Nat) := + if owner.take (identity.length + 2) == identity ++ [58, 58] then + some (owner.drop (identity.length + 2)) + else none + +/-- The metadata of one method in an assembly of the given pin: `System.Runtime` for the primitive + types, the owner class `Fixture`-style, the method itself and its locals. The result is the + slice and the token of the locals signature, or 0. -/ +def methodMetadata (pin : AssemblyPin) (method : RawMethod) : Option (MetadataSlice × Nat) := do + let identity := bytesOf pin.identity + let typeName ← ownerName identity (bytesOf method.owner) + let assemblyName := identity.takeWhile (· != 44) + if method.token != tokenOf Table.methodDef 1 then none + let arity := method.genericParameters.length + let signature ← methodBytes method + let locals ← method.locals.mapM (typeBytes arity) + let step : Step Nat := do + let runtimeName ← string "System.Runtime" + let publicKeyToken ← blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtimeName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let objectName ← string "Object" + let systemName ← string "System" + let _ ← row Table.typeRef [1 * 4 + 2, objectName, systemName] + let moduleName ← string "" + let _ ← row Table.typeDef [0, moduleName, 0, 0, 1, 1] + let ownerNameOffset ← stringBytes typeName + let owner ← row Table.typeDef [1, ownerNameOffset, 0, 1 * 4 + 1, 1, 1] + let methodName ← string "M" + let methodBlob ← blob signature + let definition ← row Table.methodDef + [0, method.implementation, method.attributes, methodName, methodBlob, 1] + link linkDeclaring definition 0 owner + let parameterName ← string "T" + let ownerRows ← method.ownerParameters.zipIdx.mapM fun (parameter, number) => + row Table.genericParam [number, parameter.flags, (owner - 0x02000000) * 2, parameterName] + if let some first := ownerRows.head? then link linkGenericParams owner 0 first + let methodRows ← method.genericParameters.zipIdx.mapM fun (parameter, number) => + row Table.genericParam [number, parameter.flags, + (definition - 0x06000000) * 2 + 1, parameterName] + if let some first := methodRows.head? then link linkGenericParams definition 0 first + if locals.isEmpty then pure 0 else + let localBlob ← blob ([0x07, locals.length] ++ locals.flatten) + row Table.standAloneSig [localBlob] + some (build step assemblyName pin.identity) + +/-- The evidence for body bytes and token claims over `metadata`: the identities, tables and + names the decoder derives from the metadata for `method` under the `platform` definitions, + which a body that does not resolve leaves empty so the check refuses it. -/ +def evidenceOf (bytes : List Nat) (operands : List (Nat × RawOperand)) (metadata : MethodMetadata) + (method : RawMethod) (platform : PlatformTable := objectPlatform) : BodyEvidence := + let identities := metadata.slices.map fun slice => (slice.assembly.spelling).toOption.getD [] + let tables := metadata.tables.toOption.getD [] + let names := + match metadata.environmentWith identities tables (PinnedIndex.ofIdentities []) platform with + | .ok env => (methodIdentityValid env method [] []).toOption.getD {} + | .error _ => {} + { bytes := bytes, tokens := { operands := operands }, metadata, identities, tables, names, + pinned := PinnedIndex.ofIdentities [], platform } + +/-- A body that decodes to the method: a tiny header when the method has no locals, the fixed + stack depth and no init flag, and a fat header otherwise, with the metadata the method's + definition and locals are checked against. -/ +def encodeBody (pin : AssemblyPin) (method : RawMethod) : Option BodyEvidence := do + let offsets ← offsetsFrom 0 method.code + let code ← encodeCode offsets (method.code.zip offsets) + let (slice, signature) ← methodMetadata pin method + let tiny := method.locals.isEmpty && !method.initLocals && method.maxStack == tinyMaxStack && + code.length < 64 + let flags := 0x03 + (if method.initLocals then 0x10 else 0) + let header := + if tiny then [code.length * 4 + 2] + else [flags % 256, 0x30 + flags / 256] ++ littleEndian 2 method.maxStack ++ + littleEndian 4 code.length ++ littleEndian 4 signature + some (evidenceOf (header ++ code) [] { own := slice } method) + +/-- The method with the instruction offsets its encoded bytes imply; unchanged if it has no + encoding. -/ +def realizeOffsets (method : RawMethod) : RawMethod := + match offsetsFrom 0 method.code with + | some offsets => + { method with code := (method.code.zip offsets).map fun (instruction, offset) => + { instruction with offset := offset } } + | none => method + +/-- The evidence with real offsets in its original and closed bodies, the bytes and metadata of + its original body and the signature its row spells. An unencodable body keeps the evidence's + own `body`, which then fails the check, so a fixture needs one positive check that its + evidence has its bytes. -/ +def withBody (item : MethodEvidence) : MethodEvidence := + let original := realizeOffsets item.original + let body := (encodeBody item.assembly original).getD item.body + { item with original := original, closed := realizeOffsets item.closed, body := body, + signature := (spelled? body.names.signature).getD item.signature } + +/-- The body of evidence that `withBody` has not yet filled in; it never decodes. -/ +def noBody : BodyEvidence := + { bytes := [], tokens := { operands := [] }, metadata := {}, identities := [], tables := [], + names := {}, pinned := PinnedIndex.ofIdentities [], platform := objectPlatform } + +end Tests.BodyEncoding diff --git a/Tests/BodyLiteral.lean b/Tests/BodyLiteral.lean new file mode 100644 index 000000000..8ab953f28 --- /dev/null +++ b/Tests/BodyLiteral.lean @@ -0,0 +1,503 @@ +import CIL.Extraction.Translation + +/-! +# The body and metadata check on literal evidence + +The evidence of `Arithmetic::Add(ulong,ulong)` as the extractor emits it: every byte list is a +`fixedBytes` literal. Each kind of literal equals, in the kernel, the same bytes written as a list +literal; the longer ones are split into 32-byte literals here to keep lines short. +`bodyMatches` is irreducible, so the elaborator's `decide` refuses it at once, within a small +heartbeat budget, and only kernel evaluation decides it. Fixtures that build their evidence with a +function must use kernel evaluation too, since the elaborator would evaluate the builder by name +without sharing. The cost rows at the end write literals as the extractor does, one code joined +from numerals of at most 256 bytes, and bound how the heartbeats and time of elaborating one and +the heartbeats of body-checking one grow with the number of bytes, and what elaborating one costs +per byte. Bodies of 5 to 20 KB whose numerals all end in the same eight bytes, and bodies of +random operands, bound how a fold over the bytes and the body check grow per byte. +-/ + +namespace Tests.BodyLiteral + +open CIL CIL.Extraction + +/-! ## The evidence as list literals -/ + +namespace Lists + +/-- The table stream header of the `Examples` assembly. -/ +def tableHeader : List Nat := + [0, 0, 0, 0, 2, 0, 0, 1, 87, 223, 163, 43, 9, 30, 0, 0, 0, 250, 1, 51, 0, 22, 0, 0, 1, 0, 0, 0, + 106, 0, 0, 0, 231, 0, 0, 0, 233, 0, 0, 0, 6, 5, 0, 0, 206, 6, 0, 0, 43, 0, 0, 0, 37, 2, 0, 0, + 26, 0, 0, 0, 50, 1, 0, 0, 1, 0, 0, 0, 31, 0, 0, 0, 13, 0, 0, 0, 113, 0, 0, 0, 20, 0, 0, 0, 31, + 0, 0, 0, 34, 0, 0, 0, 28, 0, 0, 0, 85, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 31, 0, 0, 0, + 76, 0, 0, 0, 60, 1, 0, 0, 88, 0, 0, 0] + +/-- The `Assembly` row of the `Examples` assembly. -/ +def assemblyRow : List Nat := + [4, 128, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0] + +/-- The heap entries the assembly block carries. -/ +def assemblyStrings : List ByteEntry := + [(0, [0]), (1, [69, 120, 97, 109, 112, 108, 101, 115, 0])] + +/-- The bytes of `Add`'s body. -/ +def bodyBytes : List Nat := [18, 2, 3, 88, 42] + +/-- The rows of `Add`'s metadata slice. -/ +def rows : List ByteEntry := + [(16777235, [6, 0, 4, 0, 5, 0]), (33554505, [128, 1, 16, 0, 2, 0, 3, 0, 77, 0, 70, 0, 250, 1]), + (33554506, [16, 1, 16, 0, 255, 255, 3, 0, 101, 0, 70, 0, 16, 2]), (100663802, [0, 0, 0, 0, 0, 0, + 150, 0, 1, 0, 1, 0, 212, 2]), (587202561, [11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 6, 0, 0, + 0, 0, 0]), (704643081, [0, 0, 24, 0, 120, 0, 255, 255]), (704643082, [0, 0, 24, 0, 254, 0, 255, + 255]), (704643120, [0, 0, 0, 0, 169, 3, 255, 255]), (704643121, [0, 0, 0, 0, 95, 5, 255, 255])] + +/-- The `#Strings` entries of `Add`'s metadata slice. -/ +def strings : List ByteEntry := + [(0, [0]), (1, [65, 100, 100, 0]), (2, [65, 114, 105, 116, 104, 109, 101, 116, 105, 99, 0]), (3, + [69, 120, 97, 109, 112, 108, 101, 115, 0]), (4, [79, 98, 106, 101, 99, 116, 0]), (5, [83, 121, + 115, 116, 101, 109, 0]), (6, [83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, + 0])] + +/-- The `#Blob` entries of `Add`'s metadata slice. -/ +def blobs : List ByteEntry := + [(1, [5, 0, 2, 11, 11, 11]), (2, [8, 176, 63, 95, 127, 17, 213, 10, 58])] + +/-- The bytes the `Assembly` row spells. -/ +def identity : List Nat := + [69, 120, 97, 109, 112, 108, 101, 115, 44, 32, 86, 101, 114, 115, 105, 111, 110, 61, 49, 46, 48, + 46, 48, 46, 48, 44, 32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, + 44, 32, 80, 117, 98, 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 110, 117, 108, 108] + +/-- The owner name the metadata gives `Add`. -/ +def owner : List Nat := + [69, 120, 97, 109, 112, 108, 101, 115, 44, 32, 86, 101, 114, 115, 105, 111, 110, 61, 49, 46, 48, + 46, 48, 46, 48, 44, 32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, + 44, 32, 80, 117, 98, 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 110, 117, 108, 108, + 58, 58, 69, 120, 97, 109, 112, 108, 101, 115, 46, 65, 114, 105, 116, 104, 109, 101, 116, 105, + 99] + +/-- The full name the metadata gives `Add`. -/ +def signature : List Nat := + [83, 121, 115, 116, 101, 109, 46, 85, 73, 110, 116, 54, 52, 32, 69, 120, 97, 109, 112, 108, 101, + 115, 46, 65, 114, 105, 116, 104, 109, 101, 116, 105, 99, 58, 58, 65, 100, 100, 40, 83, 121, 115, + 116, 101, 109, 46, 85, 73, 110, 116, 54, 52, 44, 83, 121, 115, 116, 101, 109, 46, 85, 73, 110, + 116, 54, 52, 41] + +end Lists + +/-! ## The evidence as the extractor emits it -/ + +def assembly0Metadata : Extraction.AssemblyMetadata := + { identity := "Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", + tableHeader := + (fixedBytes 32 0x000000000200000157DFA32B091E000000FA013300160000010000006A000000) ++ + (fixedBytes 32 0xE7000000E900000006050000CE0600002B000000250200001A00000032010000) ++ + (fixedBytes 32 0x010000001F0000000D00000071000000140000001F000000220000001C000000) ++ + (fixedBytes 32 0x550000000300000001000000040000001F0000004C0000003C01000058000000), + assemblyRow := (fixedBytes 22 0x04800000010000000000000000000000000001000000), + strings := [(0, (fixedBytes 1 0x00)), (1, (fixedBytes 9 0x4578616D706C657300))], + blobs := [(0, (fixedBytes 1 0x00))] } + +/-- The block's byte literals are the list literals. -/ +theorem assembly_literals_are_lists : + assembly0Metadata = + { assembly0Metadata with + tableHeader := Lists.tableHeader, assemblyRow := Lists.assemblyRow, + strings := Lists.assemblyStrings, blobs := [(0, [0])] } := by + cil_kernel_rfl +#print axioms assembly_literals_are_lists + +/-- The unsigned 64-bit integer. -/ +def word : TypeExpr := .atom runtimeScope "System.UInt64" +def method0Original : RawMethod := + { token := 100663802, + owner := + "Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null::Examples.Arithmetic", + hasThis := false, explicitThis := false, callingConvention := 0, attributes := 150, + implementation := 0, returnType := word, parameters := [word, word], locals := [], + initLocals := false, maxStack := 8, genericParameters := [], + code := [{ offset := 0, opcode := 2, operand := .none }, + { offset := 1, opcode := 3, operand := .none }, + { offset := 2, opcode := 88, operand := .none }, + { offset := 3, opcode := 42, operand := .none }] } +def method0Bytes : List Nat := + (fixedBytes 5 0x120203582A) + +/-- The body literal is the list literal. -/ +theorem body_literal_is_list : method0Bytes = Lists.bodyBytes := by cil_kernel_rfl +#print axioms body_literal_is_list + +def method0Metadata : Extraction.MethodMetadata := + { own := + { assembly := assembly0Metadata, + rows := [(16777235, (fixedBytes 6 0x060004000500)), + (33554505, (fixedBytes 14 0x80011000020003004D004600FA01)), + (33554506, (fixedBytes 14 0x10011000FFFF0300650046001002)), + (100663802, (fixedBytes 14 0x000000000000960001000100D402)), + (587202561, (fixedBytes 20 0x0B00000000000000000000000200060000000000)), + (704643081, (fixedBytes 8 0x000018007800FFFF)), + (704643082, (fixedBytes 8 0x00001800FE00FFFF)), + (704643120, (fixedBytes 8 0x00000000A903FFFF)), + (704643121, (fixedBytes 8 0x000000005F05FFFF))], + strings := [(0, (fixedBytes 1 0x00)), (1, (fixedBytes 4 0x41646400)), + (2, (fixedBytes 11 0x41726974686D6574696300)), (3, (fixedBytes 9 0x4578616D706C657300)), + (4, (fixedBytes 7 0x4F626A65637400)), (5, (fixedBytes 7 0x53797374656D00)), + (6, (fixedBytes 15 0x53797374656D2E52756E74696D6500))], + blobs := [(1, (fixedBytes 6 0x0500020B0B0B)), (2, (fixedBytes 9 0x08B03F5F7F11D50A3A))], + userStrings := [], corlib := 1, links := [⟨1, 100663802, 0, 33554505⟩] }, + others := [] } + +/-- The slice's byte literals are the list literals. -/ +theorem metadata_literals_are_lists : + method0Metadata = + { method0Metadata with + own := { method0Metadata.own with + rows := Lists.rows, strings := Lists.strings, blobs := Lists.blobs } } := by + cil_kernel_rfl +#print axioms metadata_literals_are_lists + +/-- The lookup tables of `method0Metadata`, as the decoder builds them. -/ +def method0Tables : List Extraction.IndexTables := + match method0Metadata.tables with + | .ok built => built + | .error _ => [] + +/-- The lookup tables as the extractor emits them. -/ +def method0TablesLiteral : List Extraction.IndexTables := + [{ rows := (.node (.node (.node (.node .leaf 16777235 (fixedBytes 6 0x060004000500) .leaf) + 33554505 (fixedBytes 14 0x80011000020003004D004600FA01) .leaf) 33554506 (fixedBytes 14 + 0x10011000FFFF0300650046001002) (.node .leaf 100663802 (fixedBytes 14 + 0x000000000000960001000100D402) .leaf)) 587202561 (fixedBytes 20 + 0x0B00000000000000000000000200060000000000) (.node (.node (.node .leaf 704643081 (fixedBytes + 8 0x000018007800FFFF) .leaf) 704643082 (fixedBytes 8 0x00001800FE00FFFF) .leaf) 704643120 + (fixedBytes 8 0x00000000A903FFFF) (.node .leaf 704643121 (fixedBytes 8 0x000000005F05FFFF) + .leaf))), + strings := (.node (.node (.node .leaf 0 (fixedBytes 1 0x00) .leaf) 1 (fixedBytes 4 0x41646400) + (.node .leaf 2 (fixedBytes 11 0x41726974686D6574696300) .leaf)) 3 (fixedBytes 9 + 0x4578616D706C657300) (.node (.node .leaf 4 (fixedBytes 7 0x4F626A65637400) .leaf) 5 + (fixedBytes 7 0x53797374656D00) (.node .leaf 6 (fixedBytes 15 + 0x53797374656D2E52756E74696D6500) .leaf))), + blobs := (.node (.node .leaf 1 (fixedBytes 6 0x0500020B0B0B) .leaf) 2 (fixedBytes 9 + 0x08B03F5F7F11D50A3A) .leaf), + userStrings := .leaf, + links := (.node .leaf 4395631098 (0, 33554505) .leaf), + instanceBefore := .leaf, + instanceTotal := (.node .leaf 33554505 0 .leaf), + genericOwners := (.node (.node (.node .leaf 120 (704643081, 1) .leaf) 254 (704643082, 1) + .leaf) 937 (704643120, 1) (.node .leaf 1375 (704643121, 1) .leaf)) }] + +/-- The emitted tables are the ones the decoder builds from the slice. -/ +theorem tables_literal_is_built : method0TablesLiteral = method0Tables := by cil_kernel_rfl +#print axioms tables_literal_is_built + +/-- The bytes the `Assembly` row spells, which the extraction proves once per assembly. -/ +def assembly0Identity : List Nat := + (fixedBytes 32 0x4578616D706C65732C2056657273696F6E3D312E302E302E302C2043756C7475) ++ + (fixedBytes 31 0x72653D6E65757472616C2C205075626C69634B6579546F6B656E3D6E756C6C) + +/-- The identity literal is the list literal. -/ +theorem identity_literal_is_list : assembly0Identity = Lists.identity := by cil_kernel_rfl +#print axioms identity_literal_is_list + +theorem assembly0_identified : AssemblyIdentified assembly0Metadata assembly0Identity := + ⟨by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl⟩ + +/-- `assembly0Metadata` stating a header with one `TypeDef` row more than its bytes give. -/ +def miscountedAssembly : AssemblyMetadata := + let counts := assembly0Metadata.header.rowCounts.map fun (table, count) => + (table, if table == Table.typeDef then count + 1 else count) + { assembly0Metadata with + header := TableHeader.ofCounts assembly0Metadata.header.heapSizes counts } + +/-- `Add`'s slice of `miscountedAssembly`. -/ +def miscountedSlice : MetadataSlice := { method0Metadata.own with assembly := miscountedAssembly } + +/-- A stated header its bytes do not decode to is refused: the assembly is not identified with + it, and a slice of it has no index. -/ +theorem refuses_a_header_its_bytes_do_not_give : + (decodeTableHeader miscountedAssembly.tableHeader).toOption ≠ + some miscountedAssembly.header ∧ + (match miscountedSlice.index with + | .error .malformedTableHeader => true + | _ => false) = true := by + decide +kernel +#print axioms refuses_a_header_its_bytes_do_not_give + +/-- The owner name and the full name the metadata gives `Add`, as bytes. -/ +def method0Names : Extraction.MethodNames := + { owner := + (fixedBytes 32 0x4578616D706C65732C2056657273696F6E3D312E302E302E302C2043756C7475) ++ + (fixedBytes 32 0x72653D6E65757472616C2C205075626C69634B6579546F6B656E3D6E756C6C3A) ++ + (fixedBytes 20 0x3A4578616D706C65732E41726974686D65746963), + signature := + (fixedBytes 32 0x53797374656D2E55496E743634204578616D706C65732E41726974686D657469) ++ + (fixedBytes 32 0x633A3A4164642853797374656D2E55496E7436342C53797374656D2E55496E74) ++ + (fixedBytes 3 0x363429) } + +/-- The name literals are the list literals. -/ +theorem names_literals_are_lists : + method0Names = { method0Names with owner := Lists.owner, signature := Lists.signature } := by + cil_kernel_rfl +#print axioms names_literals_are_lists + +/-- The signature a proof selects `Add` by. -/ +def method0Signature : String := + "System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64)" + +/-- The platform definition `Add`'s owner relies on: its base `System.Object`. -/ +def method0Platform : PlatformTable := platform_table% + [{ scope := runtimeScopeBytes, parts := [bytesOf "System", bytesOf "Object"], + kind := .objectRoot }] + +def method0Body : Extraction.BodyEvidence := + { bytes := method0Bytes, tokens := { operands := [] }, metadata := method0Metadata, + identities := [assembly0Identity], tables := method0Tables, names := method0Names, + pinned := cil_pin_index [assembly0Identity], platform := method0Platform } + +/-- The pin of the assembly the method was extracted from. -/ +def pin : AssemblyPin := ⟨"Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"⟩ + +theorem add_body_decodes : + BodyDecodes [pin] method0Platform pin method0Body method0Original [] method0Signature := + ⟨AllIdentified.cons assembly0_identified AllIdentified.nil, by decide +kernel, + by cil_kernel_rfl, by cil_kernel_rfl, rfl, by cil_kernel_rfl, by cil_kernel_rfl, + by decide +kernel⟩ +#print axioms add_body_decodes + +set_option maxHeartbeats 20000 in +/-- error: Tactic `decide` failed to reduce -/ +#guard_msgs (substring := true) in +example : bodyMatches method0Body method0Original [] = true := by decide + +/-- A changed body byte fails the kernel's decision: the second byte of the literal is `03`. -/ +theorem changed_byte_is_rejected : + bodyMatches { method0Body with bytes := fixedBytes 5 0x120303582A } method0Original [] = + false := by + decide +kernel +#print axioms changed_byte_is_rejected + +/-- A full name the row does not spell fails the kernel's decision: `Add` read as `Sub`. -/ +theorem other_full_name_is_rejected : + bodyMatches { method0Body with names := { method0Names with + signature := method0Names.signature.set 35 83 |>.set 36 117 |>.set 37 98 } } + method0Original [] = false := by + decide +kernel +#print axioms other_full_name_is_rejected + +/-! ## Cost of a byte literal -/ + +/-- The fat header of a body with `Add`'s stack depth, no locals and `codeSize` bytes of code. -/ +def fatHeader (codeSize : Nat) : List Nat := + [0x03, 0x30, 8, 0, codeSize % 256, codeSize / 256 % 256, codeSize / 65536 % 256, 0, 0, 0, 0, 0] + +/-- `Add`'s decoded method with its code replaced by `count` `nop`s and a `ret`. -/ +def nopMethod (count : Nat) : RawMethod := + { method0Original with + code := (List.range count).map (fun offset => + { offset := offset, opcode := 0, operand := .none }) ++ + [{ offset := count, opcode := 42, operand := .none }] } + +/-- The bytes of `nopMethod count`. -/ +def nopBody (count : Nat) : List Nat := + fatHeader (count + 1) ++ List.replicate count 0 ++ [0x2A] + +/-- The most bytes the extractor writes in one numeral. -/ +def literalChunkBytes : Nat := 256 + +/-- The bytes at the end of every numeral of a byte literal that the equal-tail bodies fill with + `nop`s: the kernel keys a numeral by its low 64 bits. -/ +def literalTailBytes : Nat := 8 + +/-- The bytes of an `ldc.i4` and `pop` pair. -/ +def pairBytes : Nat := 6 + +/-- The next value of a linear congruential generator below `2 ^ 31`, so an `ldc.i4` operand + is never negative. -/ +def nextSeed (seed : Nat) : Nat := (seed * 1103515245 + 12345) % 2 ^ 31 + +/-- Code from byte `offset` of `codeSize`: pairs of an `ldc.i4` of a pseudo-random value + and a `pop`, `nop`s where no pair fits, and a final `ret`. With `equalTails` no pair + covers the last `literalTailBytes` bytes of a numeral of the body, so every numeral of the + body ends in the same bytes. -/ +def pairCode (equalTails : Bool) (codeSize : Nat) : Nat → Nat → Nat → List RawInstruction + | 0, _, _ => [] + | fuel + 1, offset, seed => + let position := ((fatHeader codeSize).length + offset) % literalChunkBytes + if codeSize ≤ offset + 1 then [{ offset := offset, opcode := 0x2A, operand := .none }] + else if offset + pairBytes < codeSize && + (!equalTails || position + pairBytes ≤ literalChunkBytes - literalTailBytes) then + { offset := offset, opcode := 0x20, operand := .integer seed } :: + { offset := offset + 5, opcode := 0x26, operand := .none } :: + pairCode equalTails codeSize fuel (offset + pairBytes) (nextSeed seed) + else + { offset := offset, opcode := 0, operand := .none } :: + pairCode equalTails codeSize fuel (offset + 1) seed + +/-- `Add`'s decoded method with its code replaced by the `pairCode` of a body of `size` + bytes. -/ +def pairMethod (equalTails : Bool) (size : Nat) : RawMethod := + let codeSize := size - (fatHeader 0).length + { method0Original with code := pairCode equalTails codeSize codeSize 0 1 } + +/-- The bytes of `pairMethod equalTails size`. -/ +def pairBody (equalTails : Bool) (size : Nat) : List Nat := + let code := (pairMethod equalTails size).code + fatHeader (size - (fatHeader 0).length) ++ code.flatMap fun instruction => + match instruction.operand with + | .integer value => instruction.opcode :: (List.range 4).map (value.toNat / 256 ^ · % 256) + | _ => [instruction.opcode] + +/-- `bytes` cut into consecutive pieces of `literalChunkBytes`, the last one shorter. -/ +partial def literalChunks : List Nat → List (List Nat) + | [] => [] + | bytes => bytes.take literalChunkBytes :: literalChunks (bytes.drop literalChunkBytes) + +open Lean in +/-- The code of `chunks` as the extractor writes it: one numeral, or the codes of the first half + of the chunks and of the rest joined by `Nat.lor` and `Nat.shiftLeft`. -/ +partial def joinedCode (chunks : List (List Nat)) : MacroM Term := do + match chunks with + | [chunk] => + let code := chunk.foldl (fun code byte => code * 256 + byte) 0 + pure (Syntax.mkNumLit ("0x" ++ String.ofList (Nat.toDigits 16 code))) + | _ => + let high := chunks.take (chunks.length / 2) + let low := chunks.drop (chunks.length / 2) + let lowBits := 8 * (low.map List.length).sum + `(Nat.lor (Nat.shiftLeft $(← joinedCode high) $(quote lowBits)) $(← joinedCode low)) + +open Lean in +/-- `bytes` as the extractor writes them: one `fixedBytes` literal whose code joins numerals of + at most `literalChunkBytes` bytes. -/ +def chunkedLiteral (bytes : List Nat) : MacroM Term := do + if bytes.isEmpty then `([]) else + `(fixedBytes $(quote bytes.length) $(← joinedCode (literalChunks bytes))) + +open Lean Elab Command Meta in +/-- The heartbeats of the kernel deciding `claim`, a `Bool` equal to `true`. -/ +def kernelHeartbeats (claim : Term) : CommandElabM Nat := do + let statement ← liftTermElabM do + instantiateMVars (← Term.elabTermAndSynthesize claim (some (mkSort 0))) + let proof := mkApp2 (mkConst ``Eq.refl [1]) (mkConst ``Bool) (mkConst ``Bool.true) + let start ← IO.getNumHeartbeats + match Kernel.check (← getEnv) {} (Expr.letE `checked statement proof (.bvar 0) false) with + | .error error => throwKernelException error + | .ok _ => pure () + return (← IO.getNumHeartbeats) - start + +/-- How much more a heartbeat cost may be at four times the bytes, as a numerator over 2: linear + growth with headroom for measurement noise. -/ +def growthBound : Nat := 9 + +/-- How much more elaboration time may be at four times the bytes: linear growth with headroom for + scheduler noise, below the sixteen of a parse quadratic in the digits. -/ +def timeGrowthBound : Nat := 8 + +/-- The most heartbeats elaborating a byte literal may cost per byte; a list literal costs + thousands, a numeral and a constructor per byte. -/ +def elaborationPerByte : Nat := 200 + +/-- How many times each size is elaborated; the fastest run is the one compared. -/ +def timedRuns : Nat := 3 + +/-- Elaboration time below which a size is not compared with the next: scheduler noise. -/ +def timeFloorNanos : Nat := 20000000 + +/-- The sizes of the pair bodies, each twice the one before: 5, 10 and 20 KB. -/ +def pairBodySizes : List Nat := [5120, 10240, 20480] + +/-- The most a byte of a pair body may cost the kernel at twice the bytes, in percent of its cost + at the size before: a cost quadratic in the numerals doubles per byte. -/ +def pairGrowthPercent : Nat := 115 + +open Lean Elab Command Meta in +set_option maxHeartbeats 0 in +run_cmd do + -- Elaboration is cheap per byte, so it is timed on bodies large enough for a numeral parse + -- quadratic in its digits to show. + let mut previousHeartbeats := 0 + let mut previousNanos := 0 + for count in [16384, 65536, 262144] do + let bytes := nopBody count + let literal ← liftMacroM (chunkedLiteral bytes) + let mut heartbeats := 0 + let mut nanos := 0 + for run in List.range timedRuns do + let name := mkIdent (Name.mkSimple s!"nopBytes{count}_{run}") + let startHeartbeats ← IO.getNumHeartbeats + let start ← IO.monoNanosNow + elabCommand (← `(def $name : List Nat := $literal)) + let elapsed := (← IO.monoNanosNow) - start + heartbeats := (← IO.getNumHeartbeats) - startHeartbeats + nanos := if run == 0 then elapsed else min nanos elapsed + logInfo m!"byte literal elaboration: {bytes.length} bytes, {heartbeats} heartbeats, {nanos} ns" + if previousHeartbeats != 0 && heartbeats * 2 > previousHeartbeats * growthBound then + throwError "byte literal elaboration grew faster than its bytes" + if heartbeats > elaborationPerByte * bytes.length then + throwError "byte literal elaboration exceeds {elaborationPerByte} heartbeats per byte" + if previousNanos > timeFloorNanos && nanos > previousNanos * timeGrowthBound then + throwError "byte literal elaboration time grew faster than its bytes" + previousHeartbeats := heartbeats + previousNanos := nanos + let mut previousCheck := 0 + for count in [128, 512, 2048] do + let bytes := nopBody count + let literal ← liftMacroM (chunkedLiteral bytes) + let check ← kernelHeartbeats (← `(bodyMatches { method0Body with bytes := $literal } + (nopMethod $(quote count)) [] = true)) + logInfo m!"body check of a byte literal: {bytes.length} bytes, {check} heartbeats" + if previousCheck != 0 && check * 2 > previousCheck * growthBound then + throwError "the body check of a byte literal grew faster than its bytes" + previousCheck := check + -- Numerals with equal last bytes collide in the kernel's caches, so a body whose numerals all + -- end alike costs a comparison per pair of them unless its code is one joined term. + for equalTails in [true, false] do + let kind := if equalTails then "equal-tail" else "random" + let mut previousFold := 0 + let mut previousBody := 0 + let mut previousSize := 0 + for size in pairBodySizes do + let bytes := pairBody equalTails size + let literal ← liftMacroM (chunkedLiteral bytes) + let fold ← kernelHeartbeats + (← `((List.foldl (· + ·) 0 $literal == $(quote bytes.sum)) = true)) + let body ← kernelHeartbeats (← `(bodyMatches { method0Body with bytes := $literal } + (pairMethod $(quote equalTails) $(quote size)) [] = true)) + logInfo m!"{kind} byte literal: {bytes.length} bytes, fold {fold} and body check {body} \ + heartbeats" + if previousSize != 0 && + fold * previousSize * 100 > previousFold * size * pairGrowthPercent then + throwError "the fold of a byte literal with {kind} numerals grew faster than its bytes" + if previousSize != 0 && + body * previousSize * 100 > previousBody * size * pairGrowthPercent then + throwError "the body check of a byte literal with {kind} numerals grew faster than its \ + bytes" + previousFold := fold + previousBody := body + previousSize := size + +/-! ## The splitter's import closure -/ + +open Lean in +/-- The modules `root` imports, directly or through other modules, in an environment that has + imported it. -/ +partial def importClosure (env : Environment) (root : Name) : NameSet := + let rec visit (seen : NameSet) (module : Name) : NameSet := + match env.getModuleIdx? module with + | none => seen + | some index => + (env.header.moduleData[index.toNat]!.imports.map (·.module)).foldl + (fun seen imported => if seen.contains imported then seen + else visit (seen.insert imported) imported) seen + visit {} root + +open Lean Elab Command in +-- An image module imports only the splitter; a `Lean` import there costs every such module. +run_cmd do + unless ((← getEnv).getModuleIdx? `CIL.Extraction.FixedDigits).isSome do + throwError "CIL.Extraction.FixedDigits is not imported here" + let closure := importClosure (← getEnv) `CIL.Extraction.FixedDigits + if let some leanModule := closure.toList.find? (`Lean).isPrefixOf then + throwError "CIL.Extraction.FixedDigits imports {leanModule}, a module of Lean" + +end Tests.BodyLiteral diff --git a/Tests/BuildReuse.cs b/Tests/BuildReuse.cs index 3a64db0e5..dff422abd 100644 --- a/Tests/BuildReuse.cs +++ b/Tests/BuildReuse.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // Reusing a parent's build requires matching source and output bytes, not an environment flag. var buildElapsed = System.Diagnostics.Stopwatch.StartNew(); diff --git a/Tests/BulkCallEvidence.cs b/Tests/BulkCallEvidence.cs new file mode 100644 index 000000000..618dbfb85 --- /dev/null +++ b/Tests/BulkCallEvidence.cs @@ -0,0 +1,35 @@ +partial class Program +{ + // Each checked example retains its call and obtains the expected memory operation from its model fact. + static string BulkCallEvidence(int method, int pc, string operation) => $$""" + + namespace BulkEvidence + open CIL CIL.Extraction Extracted + /-- The retained call at this pc executes the specified memory operation on every stack. -/ + theorem call_step (args : List Safety.Value) (frame : Safety.Frame) + (stack : List Safety.Value) (memory : Safety.Memory) : + match method{{method}}.code[{{pc}}]? with + | some (Instruction.call target prefixes) => + Safety.step method{{method}} (.call target prefixes) {{pc}} args frame stack memory = + Safety.modelStep (some (.memory ({{operation}}))) method{{method}} {{pc}} frame stack memory + | _ => False := by + exact Safety.step_call_model method{{method}} {{pc}} args frame memory + (prefixes := {}) rfl method{{method}}_model{{pc}} rfl stack + #print axioms call_step + /-- A live call binds the raw declaration and the exact prefix of the emitted instruction. -/ + theorem call_operand : + (match method{{method}}Closed.code[{{pc}}]? with + | some raw => match raw.operand with + | .call site => + let agrees := fun operand emitted => + runtimeCatalog.siteAgrees environment profile operand emitted none true + let forged := { raw with operand := .call { site with declared.convention := 0 } } + let call := Instruction.call site.method {} + agrees raw call && !agrees forged call && !agrees raw (.call site.method { tail := true }) + | _ => false + | none => false) = true := by decide +kernel + #print axioms call_operand + end BulkEvidence + + """; +} diff --git a/Tests/CallIndexScaling.lean b/Tests/CallIndexScaling.lean index 98ec64212..fa534b560 100644 --- a/Tests/CallIndexScaling.lean +++ b/Tests/CallIndexScaling.lean @@ -15,17 +15,32 @@ def raw (code : List RawInstruction) : RawMethod := { /-- No constrained opcodes; every instruction still requires reachability and opcode lookup. -/ def nops (count : Nat) := raw (List.replicate count ⟨0, 0, .none⟩) /-- A minimal emitted method. -/ -def emitted (code : List Op) : Method := { locals := [], returnsValue := false, code := code } -/-- A managed call to the identical scalar-free signature at a chosen table index. -/ -def call : RawInstruction := ⟨0, 40, .call { - declaringType := .atom "Tests" "Scaling", target := .managed 1, typeArguments := [], parameterCount := 0, hasThis := false, - returnsValue := false, returnType := .void, parameterTypes := [] }⟩ +def emitted (code : List Instruction) : Method := + { locals := [], returnsValue := false, code := code } +/-- A managed call site of the scalar-free signature every table position holds. -/ +def site : CallSite := { + declared := { convention := 0 }, + method := { + owner := .atom "Tests" "Scaling", name := name!"", hasThis := false, genericArity := 0, + returnType := .void, parameters := [] }, + target := .managed 1, typeArguments := [], parameterCount := 0, returnsValue := false } +/-- A managed call of `site`; the emitted link chooses the table index. -/ +def call : RawInstruction := ⟨0, 40, .call site⟩ /-- Each instruction calls a distinct table position. -/ def calls (count : Nat) := raw (List.replicate count call) /-- Equal signatures at distinct positions isolate positional lookup costs. -/ def methods (count : Nat) : List SpecializedMethod := List.replicate count ⟨[], raw []⟩ -/-- Every table position is exercised once. -/ -def callCode (count : Nat) := emitted ((List.range count).map fun index => .call index 0) +/-- Every table position is exercised once, through the link at the pc of the same index. -/ +def callCode (count : Nat) : Method := + { emitted (List.replicate count (.call site.method)) with + links := (List.range count).map fun index => (index, .direct index) } +/-- The original scan: positional table lookups and a scan of the link table at every pc. -/ +def managedOriginal (count : Nat) : Bool := + all3 (fun instruction (live, link) operation => + validateLinkedCallWith (fun _ => false) count ((methods count)[·]?) + (([] : List ImportSlot)[·]?) instruction live link operation) + (calls count).code ((List.replicate count true).zip + ((List.range count).map (callCode count).linkAt)) (callCode count).code /-- Indexed constrained validation. -/ theorem constrainedNew128 : ConstrainedCallsValid [] [] (nops 128) (List.replicate 128 true) (emitted (List.replicate 128 .nop)) := by @@ -34,17 +49,16 @@ theorem constrainedNew128 : ConstrainedCallsValid [] [] (nops 128) /-- Original constrained validation. -/ theorem constrainedOld128 : (List.range 128).all (constrainedSiteValid [] [] (nops 128) - (List.replicate 128 true) (List.replicate 128 .nop)) = true := by + (List.replicate 128 true) (emitted (List.replicate 128 .nop))) = true := by decide +kernel #print axioms constrainedOld128 /-- Indexed managed calls. -/ -theorem managedNew128 : ManagedCallsValid (methods 128) [] (calls 128) +theorem managedNew128 : ManagedCallsValid (methods 128) [] (fun _ => false) (calls 128) (List.replicate 128 true) (callCode 128) := by decide +kernel #print axioms managedNew128 /-- Original managed calls. -/ -theorem managedOld128 : all3 (validateManagedCall (methods 128) []) (calls 128).code - (List.replicate 128 true) (callCode 128).code = true := by +theorem managedOld128 : managedOriginal 128 = true := by decide +kernel #print axioms managedOld128 @@ -56,17 +70,16 @@ theorem constrainedNew512 : ConstrainedCallsValid [] [] (nops 512) /-- Original constrained validation. -/ theorem constrainedOld512 : (List.range 512).all (constrainedSiteValid [] [] (nops 512) - (List.replicate 512 true) (List.replicate 512 .nop)) = true := by + (List.replicate 512 true) (emitted (List.replicate 512 .nop))) = true := by decide +kernel #print axioms constrainedOld512 /-- Indexed managed calls. -/ -theorem managedNew512 : ManagedCallsValid (methods 512) [] (calls 512) +theorem managedNew512 : ManagedCallsValid (methods 512) [] (fun _ => false) (calls 512) (List.replicate 512 true) (callCode 512) := by decide +kernel #print axioms managedNew512 /-- Original managed calls. -/ -theorem managedOld512 : all3 (validateManagedCall (methods 512) []) (calls 512).code - (List.replicate 512 true) (callCode 512).code = true := by +theorem managedOld512 : managedOriginal 512 = true := by decide +kernel #print axioms managedOld512 diff --git a/Tests/CallSiteAgreement.cs b/Tests/CallSiteAgreement.cs new file mode 100644 index 000000000..d6c0c7a9c --- /dev/null +++ b/Tests/CallSiteAgreement.cs @@ -0,0 +1,259 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include PreparedLeanFixtures.cs +#:include GeneratedEvidence.cs +#:include RejectedEvidence.cs + +using System.Text; + +var output = At("artifacts/CallSiteAgreement"); +var assembly = At("Examples/Assembly/bin/Examples.dll"); +RequireLeanFixtures("CIL.Extraction.Translation", "CIL.Extraction.DecodedDefinition"); +CallWitness[] witnesses = +[ + new("integerModel", "IntegerMath::AbsInt32(System.Int32)", + 0, 1, ".model"), + new("binary", "BinarySpans::ReadBig(System.ReadOnlySpan`1)", + 0, 1, ".model"), + new("arrayDataModel", "ViewAccessors::ArrayData(System.Int32[])", + 0, 1, ".model"), + new("marshal", "MarshaledMemory::Read(System.ReadOnlySpan`1)", + 0, 1, ".model"), + new("pointerOffset", "UnsafeReferences::AddPointer(System.Byte*,System.Int32)", + 0, 2, ".model"), + new("unsafeTransfer", "UnalignedMemory::ReadWord(System.Byte&)", + 0, 1, ".model"), + new("bitCast", "ValueBitCasts::From128Byte(System.Runtime.Intrinsics.Vector128`1)", + 0, 1, ".model"), + new("asReference", "UnsafeReferences::Word(Examples.FourLimbs&)", + 0, 1, ".model"), + new("sizeOf", "UnsafeLayouts::Size0()", + 0, 0, ".model"), + new("bigMul", "WideProduct::UnsignedBigMul32(System.UInt32,System.UInt32)", + 0, 2, ".model"), + new("divRem", "TupleDivision::MathQuotient(System.Int32,System.Int32)", + 0, 2, ".model"), + new("spanReference", "StaticSpans::First()", + 0, 1, ".model"), + new("skipInit", "SkipInitialization::Skip(Examples.FourLimbs&)", + 0, 1, ".model"), + new("spanItem", "MemorySpans::Read(System.ReadOnlySpan`1,System.Int32)", 0, 2, ".model"), + new("spanSlice", + "MemorySpans::SliceRead(System.ReadOnlySpan`1,System.Int32,System.Int32,System.Int32)", + 0, 3, ".model"), + new("spanLength", "SpanReferences::Length(System.ReadOnlySpan`1&)", 0, 1, ".model"), + new("spanIsEmpty", "ViewAccessors::Empty(System.Span`1)", 0, 1, ".model"), + new("spanPinnable", "ViewAccessors::PinnableNull(System.Span`1)", 0, 1, ".model"), + new("spanReadOnly", + "EnumBuffers::SpanRoundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small)", + 0, 10, ".model"), + new("spanEmpty", "SpanConstructors::ReadOnlyEmpty()", 0, 0, ".model"), + new("arrayConversion", "MemorySpans::ArrayReadOnly(System.Int32[])", 0, 1, ".model"), + new("asSpan", "MemorySpans::ArrayAsSpan(System.Boolean[])", 0, 1, ".model"), + new("objectModel", "VirtualCalls::DogLegs()", + 4, 1, ".model"), + new("throwConstructor", "Throws::Inner(System.UInt32)", + 0, 4, ".refused"), + new("typeHandle", "TypeTests::Marker(System.Int32)", + 1, 1, ".model", Refusal: "0 208"), + new("typeTest", "TypeTests::Marker(System.Int32)", + 1, 4, ".model", Refusal: "0 208"), + new("feature", "Capabilities::AesFlags()", + 0, 0, ".model"), + new("aesX64", "Capabilities::AesFlags()", 0, 3, ".model"), + new("armAes", "Capabilities::AesFlags()", 0, 9, ".model"), + new("armAes64", "Capabilities::AesFlags()", 0, 15, ".model"), + new("armSimd64", "Capabilities::ArmFlags()", 0, 0, ".model"), + new("armSimd", "Capabilities::ArmFlags()", 0, 3, ".model"), + new("vbmiVl", "Capabilities::VbmiFlags()", 0, 0, ".model"), + new("vbmi", "Capabilities::VbmiFlags()", 0, 3, ".model"), + new("x86Base64", "Capabilities::Flags()", 0, 0, ".model"), + new("vector128", "Capabilities::Flags()", 0, 3, ".model"), + new("avx2", "Loops::FeatureCountdown(uint)", 0, 3, ".model"), + new("unsignedSize", "Capabilities::Platform()", 0, 3, ".model"), + new("processWidth", "Capabilities::Platform()", 0, 6, ".model"), + new("guard", "Guards::Required(System.String)", + 0, 2, ".model"), + new("positiveGuard", "ItemPool::.ctor(System.Int32)", 0, 4, ".model"), + new("negativeGuard", "Guards::Count(System.Int32)", 0, 2, ".model"), + new("comparisonGuard", "Guards::Index(System.UInt32,System.UInt32)", 0, 3, ".model"), + new("bitModel", "Bits::Population64(System.UInt64)", + 0, 1, ".model"), + new("byteOrderModel", "Bits::ReverseI16(System.Int16)", + 0, 1, ".model"), + new("hardware", "DivisionIntrinsics::And(ulong,ulong)", + 0, 4, ".model", "x64-v3"), + new("vectorConstruction", "DivisionIntrinsics::And(ulong,ulong)", + 0, 1, ".model", "x64-v3"), + new("spanConstruction", + "DivisionIntrinsics::Shuffle128(System.ReadOnlySpan`1,System.ReadOnlySpan`1)", + 0, 1, ".model", "x64-v3"), + new("vectorModel", "DivisionIntrinsics::UnsafeCall(ulong)", + 0, 1, ".model", "x64-v3"), + new("byteTransfer", + "DivisionIntrinsics::SwapHalves(System.ReadOnlySpan`1,System.Span`1)", + 0, 6, ".model", "arm64-advsimd"), + new("areSame", "UnsafeReferences::AreSame(System.Byte&,System.Byte&)", + 0, 2, ".model"), + new("byteOffset", "UnsafeReferences::ByteOffset(System.Byte&,System.Byte&)", + 0, 2, ".model"), + new("byteAddition", "UnsafeReferences::AddSigned(System.Byte&,System.IntPtr)", + 0, 2, ".model"), + new("asRefByref", "UnsafeReferences::Mutable(Examples.FourLimbs&)", + 0, 1, ".model"), + new("asRefPointer", "UnsafeReferences::Pointer(System.Byte*)", + 0, 1, ".model"), + new("nullRef", "NullReferences::IsNull()", + 0, 0, ".model"), + new("isNullRef", "NullReferences::IsNull()", + 0, 1, ".model"), + new("platform", "Capabilities::Platform()", + 0, 0, ".model"), + new("referenceContent", "Capabilities::ReferenceFree()", + 0, 0, ".model"), + new("staticSpan", "SpanTables::First()", + 1, 1, ".model"), + new("initBlockUnaligned", "Blocks::Fill(System.Byte&,System.Byte,System.UInt32)", 0, 3, ".model"), + new("initBlock", "Blocks::FillPointerAligned(System.Byte*,System.Byte,System.UInt32)", 0, 3, ".model"), + new("copyBlockUnaligned", "Blocks::CopyPointer(System.Byte*,System.Byte*,System.UInt32)", 0, 3, ".model"), + new("copyBlock", "Blocks::CopyPointerAligned(System.Byte*,System.Byte*,System.UInt32)", 0, 3, ".model"), +]; +string[] extraOwners = [ + "Arm.ArmBase.Arm64", "X86.Sse2", "X86.Ssse3", "X86.Sse41", "X86.Sse42", "X86.Avx", + "X86.Avx512F", "X86.Avx512F.VL", "X86.Avx512DQ", "X86.Avx512DQ.VL", "X86.Bmi1", "X86.Bmi2.X64", "Vector256" +]; +var fixture = Path.Combine(output, "Fixture"); +Directory.CreateDirectory(fixture); +File.WriteAllText(Path.Combine(fixture, "Queries.csproj"), """ + + net11.0true + + """); +var extraMethods = new List(); +var extraSites = new List(); +foreach (var owner in extraOwners) +{ + var method = owner.Replace(".", "", StringComparison.Ordinal); + var getter = owner == "Vector256" ? "IsHardwareAccelerated" : "IsSupported"; + extraMethods.Add($"public static bool {method}() => System.Runtime.Intrinsics.{owner}.{getter};"); + extraSites.Add(new(method, $"Queries::{method}()", 0, 0, ".model")); +} +foreach (var name in new[] { "ThrowIfZero", "ThrowIfEqual", "ThrowIfNotEqual", "ThrowIfGreaterThan", + "ThrowIfLessThan", "ThrowIfLessThanOrEqual" }) +{ + var unary = name == "ThrowIfZero"; + var parameters = unary ? "int value" : "int value, int bound"; + var arguments = unary ? "value, null" : "value, bound, null"; + var signature = unary ? "int" : "int,int"; + extraMethods.Add($""" + public static void {name}({parameters}) => System.ArgumentOutOfRangeException.{name}({arguments}); + """); + extraSites.Add(new(name, $"Queries::{name}({signature})", 0, unary ? 2 : 3, ".model")); +} +File.WriteAllText(Path.Combine(fixture, "Queries.cs"), $$""" + public static class Queries + { + {{string.Join(Environment.NewLine, extraMethods)}} + } + """); +Run(["dotnet", "build", Path.Combine(fixture, "Queries.csproj"), "-c", "Release"]); +var evidence = new GeneratedEvidence(output); +Check(assembly, "Examples", witnesses); +Check(Path.Combine(fixture, "bin", "Queries.dll"), "Queries", extraSites); +evidence.Check(replay: false); + +void Check(string dll, string identity, IEnumerable sites) +{ + var methods = sites.GroupBy(site => (site.Entry, site.Profile)).ToArray(); + string Folder(CallWitness site) => Path.Combine(output, site.Name); + ExtractBatch("extract", identity, methods.Select(group => + new GeneratedExtraction(dll, group.Key.Entry, Folder(group.First()), group.Key.Profile))); + foreach (var group in methods) + { + var directory = Folder(group.First()); + var source = new StringBuilder(File.ReadAllText(Path.Combine(directory, "Extracted.lean"))); + foreach (var site in group) source.AppendLine(site.Proof()); + var proof = Path.Combine(directory, "Agreement.lean"); + File.WriteAllText(proof, source.ToString()); + evidence.Add(proof, $"PASS exact call-site agreement for {group.Key.Entry}"); + } +} + +sealed record CallWitness(string Name, string Entry, int Method, int Pc, string Claim, string Profile = "scalar", + string? Refusal = null) +{ + // Every different instruction, including every instruction with the same stack effect, is refused. + public string Proof() + { + var method = $"method{Method}"; + // A changed operand that breaks a fold refuses the method at the fold's first live hole. + var refusal = Refusal ?? $"{Pc} raw.opcode"; + var change = Name switch + { + "typeHandle" or "typeTest" => "method.owner := .atom runtimeScope \"MyType\"", + "throwConstructor" => "method.owner := .atom runtimeScope \"System.Object\"", + "areSame" => "method.owner := .atom \"Fixture\" \"System.Runtime.CompilerServices.Unsafe\"", + _ when Claim == ".model" => "declared.convention := site.declared.convention + 1", + _ => "target := .external \"Another overload\"" + }; + var agreement = Claim == ".refused" ? """ + And (foldOwnsSite role = true) + (role.bind (fun part => Instruction.ofRole? part none raw) = some expected)) := by + constructor + case left => cil_kernel_rfl + case right => constructor <;> cil_kernel_rfl + """ : """ + forall emitted, runtimeCatalog.siteAgrees environment profile raw emitted link true = + decide (emitted = expected)) := by + constructor + case left => cil_kernel_rfl + case right => + intro emitted + have token : methodTokenCall raw.opcode = true := by cil_kernel_rfl + have resolved : runtimeCatalog.callRequirement environment.callLayouts profile link raw = + .instruction expected := by cil_kernel_rfl + simp [ModelCatalog.siteAgrees, ModelCatalog.agreesAt, token, resolved, + CallRequirement.accepts] + """; + return $$""" + namespace Extracted.{{Name}} + open CIL CIL.Extraction + /-- The call operand from the closed metadata body. -/ + private def raw := {{method}}Closed.code[{{Pc}}]'(by decide +kernel) + /-- The operation emitted at this pc. -/ + private def expected := {{method}}.code[{{Pc}}]'(by decide +kernel) + /-- The fold role computed from the complete raw body. -/ + private def role := (foldRoles {{method}}Closed.code)[{{Pc}}]?.join + /-- The managed link at this pc, if present. -/ + private def link := {{method}}.linkAt {{Pc}} + /-- The resolving member accepts exactly its emitted instruction. -/ + theorem exactEmission : + And (siteClaim environment link raw = {{Claim}}) + ({{agreement}} + #print axioms exactEmission + /-- Changing one part of the resolving operand refuses the live call. -/ + theorem unresolvedOverload : + let changed := match raw.operand with + | .call site => { raw with operand := .call { site with {{change}} } } + | _ => raw + let code := {{method}}Closed.code.set {{Pc}} changed + let source := { {{method}}Closed with code } + let decoded := Method.ofRaw? profile environment {{method}}Links {{method}}Reachable source [] + let refusal := match Method.ofClosed? profile environment {{method}}Links {{method}}Reachable source + {{method}}RawArguments with + | .error error => some error + | .ok _ => none + And (runtimeCatalog.callRequirement environment.callLayouts profile link changed = + .hole raw.opcode) (And (decoded = none) (refusal = some (.liveHole {{refusal}}))) := by + refine ⟨?_, ?_, ?_⟩ <;> cil_kernel_rfl + #print axioms unresolvedOverload + end Extracted.{{Name}} + """; + } +} diff --git a/Tests/CallingFields.lean b/Tests/CallingFields.lean index caa2f9b95..2cfb5f55d 100644 --- a/Tests/CallingFields.lean +++ b/Tests/CallingFields.lean @@ -2,6 +2,8 @@ import CIL.Safety.CallingFields import CIL.Safety.LoopFrames import CIL.Safety.CallSegments import CIL.Safety.Forwarders +import Tests.Support +import Tests.WordFields /-! # Checked by-reference field evidence @@ -12,18 +14,28 @@ The stepper obtains field reads and formed references from `ValidCall` itself. namespace Tests.CallingFields open CIL.Safety +open Tests.WordFields /-- A four-word argument range. -/ def words (reference : Reference) : ArgumentView := ⟨reference, 32⟩ +/-- The instantiated byref argument of Unsafe.SkipInit. -/ +def skipWords : CIL.Extraction.MethodRef := + { owner := CIL.Extraction.unsafeOwner, name := name!"SkipInit", hasThis := false, + genericArity := 1, returnType := .void, parameters := [.byref (.aggregate "T::Words" 0)], + methodArguments := [.aggregate "T::Words" 0] } + /-- Read every input field before any output could be written. -/ @[cil_code] def reader : CIL.Method := - { locals := [], returnsValue := true, code := [ - .arg 2, .skipInit, - .arg 0, .fieldAt 0 8, .arg 1, .fieldAt 0 8, .add, - .arg 0, .fieldAt 8 8, .arg 1, .fieldAt 8 8, .add, .add, - .arg 0, .fieldAt 16 8, .arg 1, .fieldAt 16 8, .add, .add, - .arg 0, .fieldAt 24 8, .arg 1, .fieldAt 24 8, .add, .add, .ret] } + { locals := [], returnsValue := true, environment := wordsEnvironment, code := [ + .ldarg 2, .call skipWords {}, + .ldarg 0, .ldfld word0, .ldarg 1, .ldfld word0, .add, + .ldarg 0, .ldfld word1, .ldarg 1, .ldfld word1, .add, .add, + .ldarg 0, .ldfld word2, .ldarg 1, .ldfld word2, .add, .add, + .ldarg 0, .ldfld word3, .ldarg 1, .ldfld word3, .add, .add, .ret] } + +cil_instruction_equations Tests.CallingFields.reader +cil_runtime_models Tests.CallingFields.reader /-- Field evidence is gathered with no per-field hypotheses or stepper arguments. -/ theorem reads_four_words (memory : Memory) (left right output : Reference) @@ -53,8 +65,11 @@ theorem overlapping_read (memory : Memory) (left right : Reference) /-- Sum all input words and write the result only after reading every input. -/ @[cil_code] def writer : CIL.Method := - { locals := [], returnsValue := false, - code := [.arg 2] ++ reader.code.dropLast ++ [.store64, .ret] } + { locals := [], returnsValue := false, environment := wordsEnvironment, + code := [.ldarg 2] ++ reader.code.dropLast ++ [.stind .i8, .ret] } + +cil_instruction_equations Tests.CallingFields.writer +cil_runtime_models Tests.CallingFields.writer /-- The checksum uses the original input snapshots, including when output overlaps an input. -/ def checksum (memory : Memory) (left right : Reference) : CIL.W64 := @@ -79,12 +94,12 @@ theorem writes_after_reads (memory : Memory) (left right output : Reference) /-- Four writes followed by a read exercise argument evidence at each new memory. -/ @[cil_code] def repeatedWriter : CIL.Method := - { locals := [], returnsValue := true, code := [ - .arg 2, .const64 1, .storeScalar 8, - .arg 2, .const64 2, .setFieldAt 8 8, - .arg 2, .const64 3, .setFieldAt 16 8, - .arg 2, .const64 4, .setFieldAt 24 8, - .arg 0, .fieldAt 0 8, .ret] } + { environment := wordsEnvironment, locals := [], returnsValue := true, code := [ + .ldarg 2, .ldc_i8 1, .stind .i8, + .ldarg 2, .ldc_i8 2, .stfld word1, + .ldarg 2, .ldc_i8 3, .stfld word2, + .ldarg 2, .ldc_i8 4, .stfld word3, + .ldarg 0, .ldfld word0, .ret] } /-- Later loads observe the current memory, even when output aliases either input. -/ theorem reads_after_writes (memory : Memory) (left right output : Reference) @@ -102,13 +117,39 @@ theorem reads_after_writes (memory : Memory) (left right output : Reference) #print axioms reads_after_writes +/-- `repeatedWriter` with its field accesses decoded: each store and the load name a field. -/ +@[cil_code] def decodedWriter : CIL.Method := + { locals := [], returnsValue := true, environment := wordsEnvironment, code := [ + .ldarg 2, .ldc_i8 1, .stind .i8, + .ldarg 2, .ldc_i8 2, .stfld word1, + .ldarg 2, .ldc_i8 3, .stfld word2, + .ldarg 2, .ldc_i8 4, .stfld word3, + .ldarg 0, .ldfld word0, .ret] } + +/-- A decoded field store writes an output word through `ValidCall` as the lowered store of its + shape does. -/ +theorem decoded_reads_after_writes (memory : Memory) (left right output : Reference) + (call : ValidCall memory [words left, words right] [words output]) : + WP (Execution (fun fuel => invoke [decodedWriter] fuel 0 + [.reference (.address left), .reference (.address right), + .reference (.address output)] memory)) + (fun (final, values) => + values = [.scalar (.i64 ((words left).word final 0))] ∧ + read final { output with offset := output.offset + 24 } 8 1 = + .ok (numberBytes 4 8) ∧ + ValidCall final [words left, words right] [words output]) := by + cil_contract_steps [] + exact ⟨rfl, readback_3, storedCall_3⟩ + +#print axioms decoded_reads_after_writes + /-- An explicit successful write retains its exact final memory when automation can derive another existential write from the same permissions. -/ theorem supplied_write_priority (memory after : Memory) (output : Reference) (word : CIL.W64) (_call : ValidCall memory [] [⟨output, 8⟩]) (written : write memory output (numberBytes word.toNat 8) 1 = .ok after) : WP (Execution (fun fuel => run - [{ locals := [], returnsValue := false, code := [.store64, .ret] }] fuel 0 0 [] + [{ locals := [], returnsValue := false, code := [.stind .i8, .ret] }] fuel 0 0 [] { activation := 0, locals := [], owned := [] } [.scalar (.i64 word), .reference (.address output)] memory)) (fun (final, values) => final = after ∧ values = []) := by @@ -122,14 +163,14 @@ theorem output_bounds_and_permissions (memory : Memory) (output _other : Referen (_call : ValidCall memory [] [⟨output, 8⟩]) : True := by fail_if_success have outside : WP (Execution (fun fuel => run - [{ locals := [], returnsValue := false, code := [.setFieldAt 8 8, .ret] }] fuel 0 0 [] + [{ environment := wordsEnvironment, locals := [], returnsValue := false, code := [.stfld word1, .ret] }] fuel 0 0 [] { activation := 0, locals := [], owned := [] } [.scalar (.i64 1), .reference (.address output)] memory)) (fun _ => True) := by cil_contract_steps [] trivial fail_if_success have unauthorized : WP (Execution (fun fuel => run - [{ locals := [], returnsValue := false, code := [.store64, .ret] }] fuel 0 0 [] + [{ locals := [], returnsValue := false, code := [.stind .i8, .ret] }] fuel 0 0 [] { activation := 0, locals := [], owned := [] } [.scalar (.i64 1), .reference (.address _other)] memory)) (fun _ => True) := by cil_contract_steps [] @@ -141,7 +182,7 @@ theorem output_bounds_and_permissions (memory : Memory) (output _other : Referen /-- The two scalar helpers differ only in their checked arithmetic instruction. -/ @[cil_code] def helper (subtract : Bool) : CIL.Method := { locals := [], returnsValue := true, - code := [.arg 0, .arg 1, if subtract then .sub else .add, .ret] } + code := [.ldarg 0, .ldarg 1, if subtract then .sub else .add, .ret] } /-- A helper preserves memory and returns its specified wrapping arithmetic result. -/ def arithmetic (subtract : Bool) : MethodContract (CIL.W64 × CIL.W64) CIL.W64 where @@ -164,7 +205,9 @@ theorem helper_checked (subtract : Bool) {program : CIL.Program} {callee : Nat} /-- Both helper choices share the same caller segment and return convention. -/ @[cil_code] def caller (callee : Nat) : CIL.Method := - { locals := [], returnsValue := false, code := loadCallStoreCode callee ++ [.ret] } + { environment := wordsEnvironment, locals := [], returnsValue := false, + code := loadCallStoreCode (Tests.callee 2) word0 word0 ++ [.ret], + links := [(5, .direct callee)] } /-- The shared segment writes the chosen operation on the original input snapshots. -/ def Composed (subtract : Bool) : Prop := @@ -187,7 +230,7 @@ theorem composed (subtract : Bool) : Composed subtract := by apply WP.load_call_store (helper_checked subtract rfl) ((⟨left, 8⟩ : ArgumentView).word memory 0, (⟨right, 8⟩ : ArgumentView).word memory 0) id left right output - rfl (by rfl) call rfl trivial (fun _ => rfl) + rfl (by rfl) rfl rfl rfl rfl (fun _ _ => rfl) call rfl trivial (fun _ => rfl) intro middle value after result written have readback := write_readback _ _ _ _ _ written simp only [numberBytes, List.length_map, List.length_range] at readback @@ -217,11 +260,18 @@ def viewResult (value : CIL.Value) : MethodContract Reference Unit where /-- The result helper keeps the argument's exact scalar or aggregate encoding. -/ @[cil_code] def resultHelper : CIL.Method := - { locals := [], returnsValue := true, code := [.arg 1, .ret] } + { locals := [], returnsValue := true, code := [.ldarg 1, .ret] } /-- A wrapper forwards both arguments and the helper's checked result. -/ @[cil_code] def resultWrapper (narrow : Option CIL.NarrowKind) : CIL.Method := - { locals := [], returnsValue := true, code := forwarderCode (plainLoads [0, 1]) 1 narrow } + { locals := [], returnsValue := true, code := forwarderCode [0, 1] (Tests.callee 2), + links := [(2, .direct 1)], + declared := { returnType := match narrow with + | none => .atom CIL.Extraction.runtimeScope "System.Int64" + | some .signed8 => .atom CIL.Extraction.runtimeScope "System.SByte" + | some .unsigned8 => .atom CIL.Extraction.runtimeScope "System.Byte" + | some .signed16 => .atom CIL.Extraction.runtimeScope "System.Int16" + | some .unsigned16 => .atom CIL.Extraction.runtimeScope "System.UInt16" } } /-- The same forwarding proof preserves the valid-call precondition and exact result encoding. -/ theorem forwards_view_result (value : CIL.Value) (narrow : Option CIL.NarrowKind) @@ -240,15 +290,16 @@ theorem forwards_view_result (value : CIL.Value) (narrow : Option CIL.NarrowKind apply WP.enter (body := resultHelper) rfl (by simp [viewResult, checkedValue, formValue, checkedAt, numeric, formed, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure]) rfl - apply WP.step rfl rfl (step_load_unchanged (narrow := none) rfl (valid memory) trivial) + apply WP.step rfl rfl (step_load_unchanged rfl (valid memory) (.plain rfl)) apply WP.returned rfl rfl - (step_return_unchanged (narrow := none) rfl + (step_return_unchanged (narrow := none) rfl rfl (by simpa [Value.hasUnspecified] using known) (by simp [valid] <;> rfl) trivial) (by simp [valid] <;> rfl) exact ⟨Confined.refl [] memory, (), rfl, rfl⟩ - exact helperHolds.forward (loads := plainLoads [0, 1]) (narrow := narrow) - rfl rfl ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) - (fun _ _ _ => plainLoads_unchanged _ _) + exact helperHolds.forward (loads := [0, 1]) (narrow := narrow) + rfl rfl ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, + by cases narrow with | none => rfl | some kind => cases kind <;> rfl⟩ (fun _ => rfl) + (fun _ _ _ => plainLoads_unchanged (fun _ _ => rfl) _) (fun _ _ _ _ member => .inl member) (fun _ _ _ => by simpa only [viewResult, List.any_cons, List.any_nil, Value.hasUnspecified, Bool.false_or, Bool.or_false] using known) diff --git a/Tests/CanonicalMethods.cs b/Tests/CanonicalMethods.cs new file mode 100644 index 000000000..883785b65 --- /dev/null +++ b/Tests/CanonicalMethods.cs @@ -0,0 +1,161 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include StableNames.cs + +using System.Security.Cryptography; +using System.Text.Json; + +// The methods of the self-admission corpus, and of the examples assembly under each CI profile, whose extraction +// defines them from their decoder, against checked-in lists that only grow: a listed method that is no longer +// extracted fails, and so does an addition until --update records it. Reads the extractions Tests/SelfAdmission.cs +// ran with the current extractor and the report Tests/Blockers.cs writes for its fixture, and runs the examples' +// admission itself. +const string Usage = "Usage: CanonicalMethods.cs [--update]"; +const string UpdateCommand = "dotnet run --file Tests/CanonicalMethods.cs -- --update"; +var update = args switch { [] => false, ["--update"] => true, _ => throw new ArgumentException(Usage) }; +var listPath = At("Tests/canonical-methods.txt"); +var reportPath = At("artifacts/SelfAdmission/blockers.json"); +if (!File.Exists(reportPath)) + throw new InvalidOperationException( + $"Missing {reportPath}; run `dotnet run --file Tests/SelfAdmission.cs` first."); +using var document = JsonDocument.Parse(File.ReadAllText(reportPath)); +var root = document.RootElement; +// A report from another extractor describes another emission. +string ExtractorHash() => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(ExtractorPath))); +var extractorHash = ExtractorHash(); +if (root.GetProperty("extractorSha256").GetString() != extractorHash) + throw new InvalidOperationException( + $"{reportPath} was written by another extractor; run Tests/SelfAdmission.cs again."); + +// The methods an admission report defines from their decoder, and its admitted entries. +(HashSet Decoded, int Admitted) Read(string path, JsonElement report) +{ + if (report.GetProperty("extractorSha256").GetString() != extractorHash) + throw new InvalidOperationException($"{path} was written by another extractor."); + var admitted = report.GetProperty("entries").EnumerateArray() + .Where(entry => entry.GetProperty("status").GetString() == "admitted").ToArray(); + // A method extracted by several entries counts once. + var names = admitted + .SelectMany(entry => entry.GetProperty("decodedMethods").EnumerateArray()) + .Select(method => method.GetString()!).ToHashSet(StringComparer.Ordinal); + return (names, admitted.Length); +} +string[] Excess(Dictionary counts, Dictionary bound) => + [.. counts.SelectMany(pair => + Enumerable.Repeat(pair.Key, Math.Max(0, pair.Value - bound.GetValueOrDefault(pair.Key)))) + .Order(StringComparer.Ordinal)]; +// What a checked-in list names that is no longer decoded, and what is decoded that it does not yet name. +(string[] Lost, string[] Added) Compare(string path, string[] names) +{ + var listed = File.Exists(path) ? File.ReadAllLines(path).Where(line => line.Length != 0).ToArray() : []; + var nameCounts = StableNames.Counts(names); + var listedCounts = StableNames.Counts(listed); + return (Excess(listedCounts, nameCounts), Excess(nameCounts, listedCounts)); +} + +var (decodedNames, admittedCount) = Read(reportPath, root); +// A generated method counts toward its source method. +var decoded = decodedNames.Select(StableNames.Of).Order(StringComparer.Ordinal).ToArray(); +var (lost, added) = Compare(listPath, decoded); + +Console.WriteLine($"{decoded.Length} methods extracted for the {admittedCount} admitted entries of the " + + "self-admission corpus, each defined from its decoder."); +foreach (var method in added) Console.WriteLine(" added: " + method); +if (lost.Length != 0) + throw new InvalidOperationException($"Methods listed in {listPath} are no longer extracted:\n" + + string.Join('\n', lost)); + +// Every method of the examples assembly under each admitted profile, so a change that stops defining an example +// from its decoder fails even when no proof of it runs. +var examplesListPath = At("Tests/canonical-examples.txt"); +var examplesAssembly = At("Examples/Assembly/bin/Examples.dll"); +string[] profiles = ["scalar", "x64-sse42", "x64-v3", "x64-v4", "arm64-advsimd"]; +var examplesDirectory = At("artifacts/CanonicalMethods"); +Directory.CreateDirectory(examplesDirectory); +var listing = Run(["dotnet", ExtractorPath, "list", examplesAssembly]).Split('\n') + .Select(line => line.Trim()).ToArray(); +var header = listing.FirstOrDefault(line => line.StartsWith("Methods in ", StringComparison.Ordinal)) ?? + throw new InvalidDataException("leancil list printed no method count for " + examplesAssembly); +var listedCount = int.Parse(header[(header.LastIndexOf('(') + 1)..header.LastIndexOf(')')], + System.Globalization.CultureInfo.InvariantCulture); +var methods = listing.Where(line => line.Contains("::", StringComparison.Ordinal)).ToArray(); +if (methods.Length != listedCount) + throw new InvalidDataException($"leancil list counted {listedCount} methods of {examplesAssembly} but printed " + + $"{methods.Length}."); +// The runs only read the built extractor, so they run at once; one rebuilt meanwhile would mix two emissions. +var reports = await Task.WhenAll(profiles.Select(profile => Task.Run(() => +{ + var manifest = Path.Combine(examplesDirectory, profile + "-manifest.json"); + File.WriteAllText(manifest, JsonSerializer.Serialize(new + { + entries = methods.Select(signature => new { signature, profile }) + })); + var output = Path.Combine(examplesDirectory, profile + "-blockers.json"); + Run(["dotnet", ExtractorPath, "blockers", examplesAssembly, "--manifest", manifest, "--output", output]); + return (Profile: profile, Path: output); +}))); +if (ExtractorHash() != extractorHash) + throw new InvalidOperationException("The extractor changed while the examples' admission ran."); + +string[] decodedExamples = +[ + "System.Void Examples.Arithmetic::Mutate(System.UInt64&)", + "System.Int32 Examples.Capabilities::Endian()", + "System.ReadOnlySpan`1 Examples.StaticSpans::get_Lookup()", + "System.ReadOnlySpan`1 Examples.SpanTables::get_Words()", + "System.ReadOnlySpan`1 Examples.SpanTables::get_Powers()" +]; +const string avx2And = "System.UInt64 Examples.DivisionIntrinsics::And(System.UInt64,System.UInt64)"; +var examplesDecoded = new List(); +foreach (var (profile, path) in reports) +{ + using var report = JsonDocument.Parse(File.ReadAllText(path)); + var (names, _) = Read(path, report.RootElement); + examplesDecoded.AddRange(names.Select(name => profile + " " + StableNames.Of(name))); + Console.WriteLine($"{profile}: {names.Count} methods extracted for the {methods.Length} methods of the examples " + + "assembly, each defined from its decoder."); + foreach (var example in decodedExamples) + if (!names.Contains(example)) + throw new InvalidOperationException($"{example} is not defined from its decoder in {path}."); + if (profile == "x64-v3" && !names.Contains(avx2And)) + throw new InvalidOperationException($"{avx2And} must decode under {profile} in {path}."); +} +Console.WriteLine("PASS field sites and Mutate decode across profiles and Avx2.And decodes under x64-v3"); +var examples = examplesDecoded.Order(StringComparer.Ordinal).ToArray(); +var (examplesLost, examplesAdded) = Compare(examplesListPath, examples); +foreach (var method in examplesAdded) Console.WriteLine(" added: " + method); +if (examplesLost.Length != 0) + throw new InvalidOperationException($"Methods listed in {examplesListPath} are no longer extracted:\n" + + string.Join('\n', examplesLost)); + +// The blockers fixture's admitted method is extracted, so its proof runs over the method its decoder produced. +var fixtureReport = At("artifacts/Blockers/blockers.json"); +if (!File.Exists(fixtureReport)) + throw new InvalidOperationException($"Missing {fixtureReport}; run Tests/Blockers.cs, which writes it."); +using (var fixture = JsonDocument.Parse(File.ReadAllText(fixtureReport))) +{ + const string admittedFixture = "System.UInt64 Fixture::Admitted(System.UInt64,System.UInt64)"; + if (!Read(fixtureReport, fixture.RootElement).Decoded.Contains(admittedFixture)) + throw new InvalidOperationException($"{admittedFixture} is not defined from its decoder in {fixtureReport}."); +} +Console.WriteLine("PASS the blockers fixture's admitted method is defined from its decoder"); +if (update) +{ + File.WriteAllText(listPath, string.Concat(decoded.Select(method => method + "\n"))); + File.WriteAllText(examplesListPath, string.Concat(examples.Select(method => method + "\n"))); + Console.WriteLine($"Recorded {added.Length} additions in {listPath} and {examplesAdded.Length} in " + + $"{examplesListPath}."); + return; +} +if (added.Length + examplesAdded.Length != 0) + throw new InvalidOperationException($"{added.Length} self-admitted and {examplesAdded.Length} example methods " + + $"are newly defined from their decoder. Review them, then record them in {listPath} and {examplesListPath} " + + $"with `{UpdateCommand}`."); +Console.WriteLine($"PASS canonical methods: {decoded.Length} self-admitted and {examples.Length} example methods " + + "defined from their decoder, matching the lists"); diff --git a/Tests/CertificateBranchScaling.lean b/Tests/CertificateBranchScaling.lean index 854751289..c074f8e07 100644 --- a/Tests/CertificateBranchScaling.lean +++ b/Tests/CertificateBranchScaling.lean @@ -10,13 +10,15 @@ set_option maxHeartbeats 0 namespace CertificateBranchScaling open CIL CIL.Extraction -/-- A void method with distinct branch targets in reverse order. -/ +/-- A void method with distinct branch targets in reverse order, each two-byte `br.s` at its own + offset. -/ def raw (count : Nat) : RawMethod := { token := 1, owner := "Scaling", hasThis := false, explicitThis := false, callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], initLocals := true, maxStack := 1, genericParameters := [], - code := (List.range count).map (fun pc => ⟨pc, 43, .target (count - pc)⟩) ++ [⟨0, 42, .none⟩] } + code := (List.range count).map (fun pc => ⟨2 * pc, 43, .target (count - pc)⟩) ++ + [⟨2 * count, 42, .none⟩] } /-- The feature certificate contains an empty stack at every site. -/ def features (count : Nat) : FeatureCertificate := @@ -26,18 +28,19 @@ def features (count : Nat) : FeatureCertificate := def scalars (count : Nat) : ScalarCertificate := List.replicate (count + 1) (some []) /-- Forward edges, a self edge and backedges retain their exact destinations. -/ -def code (count : Nat) : List Op := - (List.range count).map (fun pc => .branch (count - pc)) ++ [.ret] +def code (count : Nat) : List Instruction := + (List.range count).map (fun pc => .br (count - pc)) ++ [.ret] set_option profiler true in /-- Fixed feature validation workload, reduced by the kernel. -/ -theorem feature_valid : FeatureFlowValid #[] (fun _ => none) (raw 512) .scalar (features 512) +theorem feature_valid : FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw 512) .scalar + (features 512) (List.replicate 513 true) (code 512) := by decide #print axioms feature_valid set_option profiler true in /-- Fixed scalar validation workload, reduced by the kernel. -/ -theorem scalar_valid : ScalarFlowValid #[] (raw 512) (features 512) (scalars 512) (code 512) := by +theorem scalar_valid : ScalarFlowValid (raw 512) (features 512) (scalars 512) (code 512) := by decide #print axioms scalar_valid diff --git a/Tests/CertificateFlow.lean b/Tests/CertificateFlow.lean index 2c190233e..3b8793fd9 100644 --- a/Tests/CertificateFlow.lean +++ b/Tests/CertificateFlow.lean @@ -4,7 +4,9 @@ import CIL.Extraction.Scalars # Certificate lookup regressions Forward edges, backedges, empty certificates, unreachable targets and bounds exercise the -same feature/scalar validators used by extracted methods, including the streaming endpoint. +same feature/scalar validators used by extracted methods, including the streaming endpoint. A +branch or switch arm into the instruction after any prefix fails `PrefixEntryFree`, reachable or +not, and a branch to the prefix itself does not. -/ namespace Tests.CertificateFlow @@ -25,7 +27,8 @@ private def forward : RawMethod := raw [⟨0, 43, .target 3⟩, ⟨1, 0, .none⟩, ⟨2, 0, .none⟩, ⟨3, 42, .none⟩] /-- Pruned sites retain explicit holes. -/ -private def forwardCode : List Op := [.branch 3, .unsupported "hole", .unsupported "hole", .ret] +private def forwardCode : List Instruction := + [.br 3, .unadmitted 0, .unadmitted 0, .ret] /-- Only the branch and its destination are reachable. -/ private def forwardFeatures : FeatureCertificate := [emptyState, none, none, emptyState] @@ -35,17 +38,19 @@ private def forwardScalars : ScalarCertificate := [some [], none, none, some []] /-- Forward lookup accepts the final in-bounds entry in both certificates. -/ theorem forward_valid : - FeatureFlowValid #[] (fun _ => none) forward .scalar forwardFeatures [true, false, false, true] + FeatureFlowValid #[] (fun _ => none) (fun _ => none) forward .scalar forwardFeatures [true, + false, false, true] forwardCode ∧ - ScalarFlowValid #[] forward forwardFeatures forwardScalars forwardCode := by decide + ScalarFlowValid forward forwardFeatures forwardScalars forwardCode := by decide #print axioms forward_valid /-- A target absent from either certificate cannot be accepted as an empty stack. -/ theorem missing_target_rejected : - ¬ FeatureFlowValid #[] (fun _ => none) forward .scalar [emptyState, none, none, none] + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) forward .scalar + [emptyState, none, none, none] [true, false, false, false] forwardCode ∧ - ¬ ScalarFlowValid #[] forward forwardFeatures [some [], none, none, none] forwardCode ∧ - ¬ ScalarFlowValid #[] forward [emptyState, none, none, none] forwardScalars forwardCode := by + ¬ ScalarFlowValid forward forwardFeatures [some [], none, none, none] forwardCode ∧ + ¬ ScalarFlowValid forward [emptyState, none, none, none] forwardScalars forwardCode := by decide #print axioms missing_target_rejected @@ -53,40 +58,72 @@ theorem missing_target_rejected : theorem bounds_rejected : ([4, 1000000] : List Nat).all (fun target => let body := { forward with code := forward.code.set 0 ⟨0, 43, .target target⟩ } - let emitted := forwardCode.set 0 (.branch target) - !decide (FeatureFlowValid #[] (fun _ => none) body .scalar forwardFeatures + let emitted := forwardCode.set 0 (.br target) + !decide (FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar forwardFeatures [true, false, false, true] emitted) && - !decide (ScalarFlowValid #[] body forwardFeatures forwardScalars emitted)) = true := by decide + !decide (ScalarFlowValid body forwardFeatures forwardScalars emitted)) = true := by decide #print axioms bounds_rejected /-- A backward branch can target entry after streaming through several fallthroughs. -/ theorem backedge_valid : let body := raw [⟨0, 0, .none⟩, ⟨1, 0, .none⟩, ⟨2, 43, .target 0⟩] let features := List.replicate 3 emptyState - let emitted := [.nop, .nop, .branch 0] - FeatureFlowValid #[] (fun _ => none) body .scalar features [true, true, true] emitted ∧ - ScalarFlowValid #[] body features (List.replicate 3 (some [])) emitted := by decide + let emitted := [.nop, .nop, .br 0] + FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar features [true, true, + true] emitted ∧ + ScalarFlowValid body features (List.replicate 3 (some [])) emitted := by decide #print axioms backedge_valid /-- The end of a certificate is not an implicit successor for a final nop. -/ theorem missing_fallthrough_rejected : - ¬ FeatureFlowValid #[] (fun _ => none) + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [⟨0, 0, .none⟩]) .scalar [emptyState] [true] [.nop] ∧ - ¬ ScalarFlowValid #[] (raw [⟨0, 0, .none⟩]) [emptyState] [some []] [.nop] := by decide + ¬ ScalarFlowValid (raw [⟨0, 0, .none⟩]) [emptyState] [some []] [.nop] := by decide #print axioms missing_fallthrough_rejected /-- Empty certificates still fail the mandatory entry check. -/ theorem empty_rejected : - ¬ FeatureFlowValid #[] (fun _ => none) (raw []) .scalar [] [] [] ∧ - ¬ ScalarFlowValid #[] (raw []) [] [] [] := by decide + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw []) .scalar [] [] [] ∧ + ¬ ScalarFlowValid (raw []) [] [] [] := by decide #print axioms empty_rejected /-- Pairing certificates cannot hide a missing or extra entry. -/ theorem lengths_rejected : - ¬ ScalarFlowValid #[] forward (forwardFeatures ++ [none]) forwardScalars forwardCode ∧ - ¬ ScalarFlowValid #[] forward forwardFeatures (forwardScalars ++ [none]) forwardCode ∧ - ¬ FeatureFlowValid #[] (fun _ => none) forward .scalar (forwardFeatures ++ [none]) + ¬ ScalarFlowValid forward (forwardFeatures ++ [none]) forwardScalars forwardCode ∧ + ¬ ScalarFlowValid forward forwardFeatures (forwardScalars ++ [none]) forwardCode ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) forward .scalar + (forwardFeatures ++ [none]) [true, false, false, true, false] forwardCode := by decide #print axioms lengths_rejected +/-- Each instruction prefix, with an operand of the form it takes. -/ +private def prefixes : List RawInstruction := + [⟨1, 65042, .integer 1⟩, ⟨1, 65043, .none⟩, ⟨1, 65044, .none⟩, + ⟨1, 65046, .type (.atom runtimeScope "System.Int32")⟩, ⟨1, 65049, .integer 1⟩, + ⟨1, 65054, .none⟩] + +/-- `branch; prefix; nop; ret`: the instruction at pc 1 modifies the one at pc 2. -/ +private def prefixed (branch modifier : RawInstruction) : List RawInstruction := + [branch, modifier, ⟨2, 0, .none⟩, ⟨3, 42, .none⟩] + +/-- A branch or switch arm into the instruction after any prefix is refused, and so is one after + the final return that no path reaches; a branch to the prefix itself is accepted. -/ +theorem prefix_entry_rejected : + prefixes.all (fun modifier => + decide (PrefixEntryFree (prefixed ⟨0, 43, .target 1⟩ modifier)) && + !decide (PrefixEntryFree (prefixed ⟨0, 43, .target 2⟩ modifier)) && + !decide (PrefixEntryFree (prefixed ⟨0, 69, .targets [3, 2]⟩ modifier)) && + !decide (PrefixEntryFree + [⟨0, 42, .none⟩, modifier, ⟨2, 0, .none⟩, ⟨3, 43, .target 2⟩])) = true := by + decide +#print axioms prefix_entry_rejected + +/-- A branch may enter the instruction after an instruction that is not a prefix, such as + `initobj` between the prefixes in opcode order. -/ +theorem unprefixed_entry_accepted : + PrefixEntryFree (prefixed ⟨0, 43, .target 2⟩ ⟨1, 0, .none⟩) ∧ + PrefixEntryFree (prefixed ⟨0, 43, .target 2⟩ + ⟨1, 65045, .type (.atom runtimeScope "System.Int32")⟩) := by decide +#print axioms unprefixed_entry_accepted + end Tests.CertificateFlow diff --git a/Tests/CertificateFlowScaling.lean b/Tests/CertificateFlowScaling.lean index f85638cba..06a3bdfdc 100644 --- a/Tests/CertificateFlowScaling.lean +++ b/Tests/CertificateFlowScaling.lean @@ -8,12 +8,14 @@ set_option maxHeartbeats 0 namespace CertificateFlowScaling open CIL CIL.Extraction -/-- A void method whose only successor at each ordinary site is fallthrough. -/ +/-- A void method whose only successor at each ordinary site is fallthrough; each one-byte nop + sits at its own offset, as extraction writes it. -/ def raw (count : Nat) : RawMethod := { token := 1, owner := "Scaling", hasThis := false, explicitThis := false, callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], initLocals := true, maxStack := 1, - genericParameters := [], code := List.replicate count ⟨0, 0, .none⟩ ++ [⟨0, 42, .none⟩] } + genericParameters := [], + code := (List.range count).map (fun pc => ⟨pc, 0, .none⟩) ++ [⟨count, 42, .none⟩] } /-- The feature certificate contains an empty stack at every site. -/ def features (count : Nat) : FeatureCertificate := @@ -23,17 +25,18 @@ def features (count : Nat) : FeatureCertificate := def scalars (count : Nat) : ScalarCertificate := List.replicate (count + 1) (some []) /-- Every nop falls through to the final void return. -/ -def code (count : Nat) : List Op := List.replicate count .nop ++ [.ret] +def code (count : Nat) : List Instruction := List.replicate count .nop ++ [.ret] set_option profiler true in /-- Fixed feature validation workload, reduced by the kernel. -/ -theorem feature_valid : FeatureFlowValid #[] (fun _ => none) (raw 512) .scalar (features 512) +theorem feature_valid : FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw 512) .scalar + (features 512) (List.replicate 513 true) (code 512) := by decide #print axioms feature_valid set_option profiler true in /-- Fixed scalar validation workload, reduced by the kernel. -/ -theorem scalar_valid : ScalarFlowValid #[] (raw 512) (features 512) (scalars 512) (code 512) := by +theorem scalar_valid : ScalarFlowValid (raw 512) (features 512) (scalars 512) (code 512) := by decide #print axioms scalar_valid diff --git a/Tests/CheckedArithmetic.cs b/Tests/CheckedArithmetic.cs index d8e89da69..e49652dcc 100644 --- a/Tests/CheckedArithmetic.cs +++ b/Tests/CheckedArithmetic.cs @@ -1,7 +1,13 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:package Mono.Cecil +#:include ContractVariants.cs #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Numerics; @@ -16,11 +22,6 @@ var queue = new GeneratedQueue(verification, assembly); var cases = 0; -// The universal contracts require no overflow, with no restriction beyond the sum fitting. -foreach (var width in new[] { 32, 64 }) - verification.Verify(assembly, "Examples", width == 32 ? "CheckedArithmetic::Add(uint,uint)" : "CheckedArithmetic::Add64(ulong,ulong)", - At($"Examples/Proofs/CheckedAdd{width}.lean"), Path.Combine(root, $"CheckedAdd{width}")); - // The checked automation follows extracted instruction lookups, not fixed PCs. var shiftedOutput = Path.Combine(root, "ShiftedCheckedAdd64"); Directory.CreateDirectory(shiftedOutput); @@ -108,50 +109,51 @@ queue.Flush(); -// A different actual implementation must not satisfy the same universal contract. -var wrongOutput = Path.Combine(root, "WrongCheckedAdd"); -Directory.CreateDirectory(wrongOutput); -var wrongDll = Path.Combine(wrongOutput, "Examples.dll"); -using (var module = ModuleDefinition.ReadModule(assembly)) -{ - var method = module.GetType("Examples.CheckedArithmetic").Methods.Single(m => m.Name == "Add"); - method.Body.Instructions.Single(i => i.OpCode == OpCodes.Add_Ovf_Un).OpCode = OpCodes.Sub; - module.Write(wrongDll); -} -var rejected = false; -try -{ - verification.Verify(wrongDll, "Examples", "CheckedArithmetic::Add(uint,uint)", - At("Examples/Proofs/CheckedAdd32.lean"), wrongOutput); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) +// The same two inputs distinguish checked addition from the compiled subtraction at either width. +foreach (var width in new[] { 32, 64 }) { - rejected = true; -} -if (!rejected || File.Exists(Path.Combine(wrongOutput, "verified.json"))) - throw new InvalidOperationException("Incorrect sum accepted"); - -Console.WriteLine($"CheckedArithmetic passed: {cases} cases; universal contracts passed and incorrect sum rejected."); + var methodName = width == 32 ? "Add" : "Add64"; + var selector = width == 32 ? "CheckedArithmetic::Add(uint,uint)" : "CheckedArithmetic::Add64(ulong,ulong)"; + var wrongOutput = Path.Combine(root, width == 32 ? "WrongCheckedAdd" : "WrongCheckedAdd64"); + Directory.CreateDirectory(wrongOutput); + var wrongDll = Path.Combine(wrongOutput, "Examples.dll"); + using (var module = ModuleDefinition.ReadModule(assembly)) + { + var method = module.GetType("Examples.CheckedArithmetic").Methods.Single(m => m.Name == methodName); + method.Body.Instructions.Single(i => i.OpCode == OpCodes.Add_Ovf_Un).OpCode = OpCodes.Sub; + module.Write(wrongDll); + } -var wrong64Output = Path.Combine(root, "WrongCheckedAdd64"); -Directory.CreateDirectory(wrong64Output); -var wrong64Dll = Path.Combine(wrong64Output, "Examples.dll"); -using (var module = ModuleDefinition.ReadModule(assembly)) -{ - var method = module.GetType("Examples.CheckedArithmetic").Methods.Single(m => m.Name == "Add64"); - method.Body.Instructions.Single(i => i.OpCode == OpCodes.Add_Ovf_Un).OpCode = OpCodes.Sub; - module.Write(wrong64Dll); -} -rejected = false; -try -{ - verification.Verify(wrong64Dll, "Examples", "CheckedArithmetic::Add64(ulong,ulong)", - At("Examples/Proofs/CheckedAdd64.lean"), wrong64Output); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - rejected = true; + var contract = $$""" + /-- Every fitting unsigned sum returns its natural-number value and preserves caller memory. -/ + def contract : MethodContract (W{{width}} × W{{width}}) where + args input := [.scalar (.i{{width}} input.1), .scalar (.i{{width}} input.2)] + pre input _ := input.1.toNat + input.2.toNat < 2^{{width}} + footprint _ := [] + post input before after values := after = before ∧ + values = [.scalar (.i{{width}} (BitVec.ofNat {{width}} (input.1.toNat + input.2.toNat)))] + """; + var fromExample = width == 32 ? "⟨4, ExampleProof.correctness_framed memory x y fits⟩" : + "ExampleProof.correctness memory x y fits"; + var positive = $$""" + /-- The arbitrary-memory invocation theorem supplies the result and frame. -/ + theorem correctness : contract.Holds Extracted.program 0 := by + rintro ⟨x, y⟩ memory fits + have returned : ∃ fuel, invoke Extracted.program fuel 0 (contract.args (x, y)) memory = + .ok (memory, [.scalar (.i{{width}} (BitVec.ofNat {{width}} (x.toNat + y.toNat)))]) := + {{fromExample}} + obtain ⟨fuel, ran⟩ := returned + exact ⟨fuel, _, ran, Confined.refl _ _, _, rfl, rfl, rfl⟩ + """; + const string counterexample = """ + /-- Two minus one returns one where the checked addition contract requires three. -/ + theorem correctness : contract.Refuted Extracted.program 0 := + .of_return (input := (2, 1)) (initial := Memory.empty) (by dsimp [contract]; decide) + (rfl : invoke Extracted.program 4 0 (contract.args (2, 1)) Memory.empty = .ok _) + (by rintro ⟨-, _, rfl, -, wrong⟩; revert wrong; decide) + """; + verification.VerifyContractVariant(assembly, "Examples", selector, Path.Combine(root, $"CheckedAdd{width}"), + wrongDll, selector, wrongOutput, contract, positive, counterexample, + example: At($"Examples/Proofs/CheckedAdd{width}.lean")); } -if (!rejected || File.Exists(Path.Combine(wrong64Output, "verified.json"))) - throw new InvalidOperationException("Checked automation accepted incorrect addition"); -Console.WriteLine("PASS checked automation rejects subtraction under the unchanged universal contract"); +Console.WriteLine($"CheckedArithmetic passed {cases} boundary cases and verified the two incorrect sums."); diff --git a/Tests/CheckedConversions.cs b/Tests/CheckedConversions.cs index be2742ba0..b18b6780f 100644 --- a/Tests/CheckedConversions.cs +++ b/Tests/CheckedConversions.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Numerics; diff --git a/Tests/CliImportedKernelAudit.cs b/Tests/CliImportedKernelAudit.cs index defbb56e0..b1a5ad736 100644 --- a/Tests/CliImportedKernelAudit.cs +++ b/Tests/CliImportedKernelAudit.cs @@ -1,6 +1,10 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs #:include ConsumerTraceDirectory.cs @@ -68,9 +72,22 @@ set_option debug.skipKernelTC true // Checkout inputs enumerate these folders; the packaged corpus has no sources in them. foreach (var folder in new[] { "Extractor", "Examples", "Tests" }) Directory.CreateDirectory(Path.Combine(corpus, folder)); - foreach (var name in new[] - { "PrepareLibrary.cs", "Verification.cs", "ConsumerVerification.cs", "LeanDiagnostics.cs" }) - File.Copy(At("scripts/" + name), Path.Combine(scripts, name)); + // An included source may include further sources by paths relative to its own folder. + var copiedSources = new HashSet(StringComparer.Ordinal); + var pendingSources = new Queue(["PrepareLibrary.cs"]); + while (pendingSources.TryDequeue(out var name)) + { + var source = Path.GetFullPath(Path.Combine(At("scripts"), name)); + var relative = Path.GetRelativePath(At("scripts"), source); + if (!copiedSources.Add(relative)) continue; + var copy = Path.GetFullPath(Path.Combine(scripts, relative)); + Directory.CreateDirectory(Path.GetDirectoryName(copy)!); + File.Copy(source, copy); + foreach (var included in File.ReadLines(source) + .Where(line => line.StartsWith("#:include ", StringComparison.Ordinal)) + .Select(line => line["#:include ".Length..].Trim())) + pendingSources.Enqueue(Path.Combine(Path.GetDirectoryName(relative) ?? "", included)); + } foreach (var name in new[] { "global.json", "Directory.Build.props", "Directory.Packages.props", ".editorconfig" }) File.Copy(At(name), Path.Combine(corpus, name)); var prepared = Path.Combine(importedRoot, "prepared"); @@ -136,4 +153,3 @@ double Seconds(string name) => stages.Where(stage => stage.GetProperty("stage"). }; File.WriteAllText(Path.Combine(importedRoot, "timings.json"), JsonSerializer.Serialize(measurements, Json)); Console.WriteLine("PASS CLI imported helper preparation counts: " + JsonSerializer.Serialize(measurements)); - diff --git a/Tests/CliReports/LoopHeadMismatch.json b/Tests/CliReports/LoopHeadMismatch.json index 7fabd78ed..541c90a1a 100644 --- a/Tests/CliReports/LoopHeadMismatch.json +++ b/Tests/CliReports/LoopHeadMismatch.json @@ -1,5 +1,5 @@ { - "schema": 1, + "schema": 2, "result": "failed", "stage": "proof checking", "theorem": "ExampleProof.correctness", @@ -13,6 +13,7 @@ "axioms": null, "statement": null, "specification": null, + "binding": null, "failures": [ { "kind": "elaboration-error", @@ -71,7 +72,7 @@ "line": 40, "column": 19 }, - "message": "cil_loop_finish: cannot step pc 3 (CIL.Op.memory (CIL.MemoryOp.spanSlice false)) at memory\n memory\nThe step is\n step Extracted.method0 (CIL.Op.memory (CIL.MemoryOp.spanSlice false)) 3 [Value.bufferSpan view✝]\n { activation := memory.nextIdentity, locals := [LocalSlot.span 4 false (some view)], owned := [] }\n [Value.scalar (CIL.Value.i32 1), Value.bufferSpan view] memory =\n Except.ok (FrameAction.next ?_mvar.4169.6404 ?_mvar.4169.6409 ?_mvar.4169.6411 ?_mvar.4169.6413)\nWith the step equations and facts it reduces to\n (if 1 ≤ 0 then\n match\n match view.slice memory 1 (0 - 1) with\n | Except.error err => Except.error (ExecutionFault.memory err view.reference 0)\n | Except.ok v => Except.ok v with\n | Except.error err => Except.error err\n | Except.ok v =>\n Except.ok\n (FrameAction.next 4 [Value.bufferSpan v]\n { activation := memory.nextIdentity, locals := [LocalSlot.span 4 false (some view)], owned := [] } memory)\n else Except.error (ExecutionFault.thrown \"System.ArgumentOutOfRangeException\")) =\n Except.ok (FrameAction.next ?_mvar.4169.6404 ?_mvar.4169.6409 ?_mvar.4169.6411 ?_mvar.4169.6413)\nSupply the slice: `view.slice memory start length = .ok sliced`, such as from `CheckedSpan.tail` at the caller's memory.", + "message": "cil_loop_finish: cannot step pc 3 (CIL.Instruction.call\n {\n owner :=\n (CIL.Extraction.TypeExpr.constructor CIL.Extraction.runtimeScope \"System.ReadOnlySpan`1\").apply\n (CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope \"System.Int32\"),\n name := name!\"Slice\", hasThis := true, genericArity := 0,\n returnType :=\n (CIL.Extraction.TypeExpr.constructor CIL.Extraction.runtimeScope \"System.ReadOnlySpan`1\").apply\n (CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope \"System.Int32\"),\n parameters := [CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope \"System.Int32\"] }) at memory\n memory\nThe step is\n modelStep (some (CIL.RuntimeModel.memory (CIL.MemoryOp.spanSlice false))) Extracted.method0 3\n { activation := memory.nextIdentity, locals := [LocalSlot.span 4 false (some view)], owned := [] }\n [Value.scalar (CIL.Value.i32 1), Value.spanHome 0] memory =\n Except.ok (FrameAction.next ?_ ?_ ?_ ?_)\nWith the step equations and facts it reduces to\n (if 1 ≤ 0 then\n match\n match view.slice memory 1 (0 - 1) with\n | Except.error err => Except.error (ExecutionFault.memory err view.reference 0)\n | Except.ok v => Except.ok v with\n | Except.error err => Except.error err\n | Except.ok v =>\n Except.ok\n (FrameAction.next 4 [Value.bufferSpan v]\n { activation := memory.nextIdentity, locals := [LocalSlot.span 4 false (some view)], owned := [] } memory)\n else Except.error (ExecutionFault.thrown CIL.ExceptionType.argumentOutOfRange)) =\n Except.ok (FrameAction.next ?_ ?_ ?_ ?_)\nSupply the slice: `view.slice memory start length = .ok sliced`, such as from `CheckedSpan.tail` at the caller's memory.", "tactic": "cil_loop_finish", "stopped": false, "method": { @@ -82,7 +83,7 @@ "pc": 3, "ilOffset": 5, "instruction": "call System.ReadOnlySpan`1 System.ReadOnlySpan`1::Slice(System.Int32)", - "leanInstruction": "CIL.Op.memory (CIL.MemoryOp.spanSlice false)", + "leanInstruction": "CIL.Instruction.call\n {\n owner :=\n (CIL.Extraction.TypeExpr.constructor CIL.Extraction.runtimeScope \"System.ReadOnlySpan`1\").apply\n (CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope \"System.Int32\"),\n name := name!\"Slice\", hasThis := true, genericArity := 0,\n returnType :=\n (CIL.Extraction.TypeExpr.constructor CIL.Extraction.runtimeScope \"System.ReadOnlySpan`1\").apply\n (CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope \"System.Int32\"),\n parameters := [CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope \"System.Int32\"] }", "source": { "file": "Loops.cs", "line": 31, @@ -94,14 +95,14 @@ "frame": "{ activation := memory.nextIdentity, locals := [CIL.Safety.LocalSlot.span 4 false (some view)], owned := [] }", "memory": "memory", "pc": "3", - "stack": "[CIL.Safety.Value.scalar (CIL.Value.i32 1), CIL.Safety.Value.bufferSpan view]" + "stack": "[CIL.Safety.Value.scalar (CIL.Value.i32 1), CIL.Safety.Value.spanHome 0]" }, "expected": {} }, "missing": { "shape": "spanSlice", "residual": [ - "(if 1 ≤ view.length then\n match\n match view.slice memory 1 (view.length - 1) with\n | Except.error err => Except.error (CIL.Safety.ExecutionFault.memory err view.reference 0)\n | Except.ok v => Except.ok v with\n | Except.error err => Except.error err\n | Except.ok v =>\n Except.ok\n (CIL.Safety.FrameAction.next 4 [CIL.Safety.Value.bufferSpan v]\n { activation := memory.nextIdentity, locals := [CIL.Safety.LocalSlot.span 4 false (some view)], owned := [] }\n memory)\n else Except.error (CIL.Safety.ExecutionFault.thrown \"System.ArgumentOutOfRangeException\")) =\n ?m.233" + "(if 1 ≤ view.length then\n match\n match view.slice memory 1 (view.length - 1) with\n | Except.error err => Except.error (CIL.Safety.ExecutionFault.memory err view.reference 0)\n | Except.ok v => Except.ok v with\n | Except.error err => Except.error err\n | Except.ok v =>\n Except.ok\n (CIL.Safety.FrameAction.next 4 [CIL.Safety.Value.bufferSpan v]\n { activation := memory.nextIdentity, locals := [CIL.Safety.LocalSlot.span 4 false (some view)], owned := [] }\n memory)\n else Except.error (CIL.Safety.ExecutionFault.thrown CIL.ExceptionType.argumentOutOfRange)) =\n ?_" ] }, "suggestions": [ @@ -186,7 +187,8 @@ "trace": null, "unknown": { "trace": "not requested; --trace traces each step the proof takes", - "goals": "not requested; --goals reports the goal at each sorry and failing tactic" + "goals": "not requested; --goals reports the goal at each sorry and failing tactic", + "binding": "the run issued no certificate" }, "rerun": [ "leancil", diff --git a/Tests/CliReports/MissingInvariant.json b/Tests/CliReports/MissingInvariant.json index d1f384683..63a1ec657 100644 --- a/Tests/CliReports/MissingInvariant.json +++ b/Tests/CliReports/MissingInvariant.json @@ -1,5 +1,5 @@ { - "schema": 1, + "schema": 2, "result": "failed", "stage": "proof checking", "theorem": "ExampleProof.correctness", @@ -13,6 +13,7 @@ "axioms": null, "statement": null, "specification": null, + "binding": null, "failures": [ { "kind": "unsolved-goal", @@ -20,7 +21,7 @@ "module": "Proof", "source": { "file": "", - "line": 35, + "line": 36, "column": 2 }, "message": "unsolved goals\ncase invariant\nx✝ : W32\nhome : Reference\nentered : Memory\nenter : entry.Checked x✝ home entered\nx : W32\nm : Memory\nframe :\n LoopFrame [] Memory.empty m [CIL.LocalKind.word32] [LocalSlot.bytes CIL.LocalKind.word32 home]\n (knownLocals [some (CIL.Value.i32 x)])\nrecurse :\n ∀ (next : State home),\n True →\n BitVec.toNat next.values < BitVec.toNat { values := x, memory := m, frame := frame }.values →\n LoopTerminates Extracted.program 0 [Value.scalar (CIL.Value.i32 x✝)]\n ((fun x x_1 x_2 values => values = [Value.scalar (CIL.Value.i32 0)]) [Value.scalar (CIL.Value.i32 x✝)]\n Memory.empty)\n (header home next)\nzero : ¬x = 0#32\nhomeLoad✝⁵ : loadLocal m (LocalSlot.bytes CIL.LocalKind.word32 home) = Except.ok (Value.scalar (CIL.Value.i32 x))\nframeLeave✝³ :\n ∀ (exited : Frame),\n exited.owned = (entry.frame home).owned → MemoryBelow Memory.empty.nextIdentity Memory.empty (leaveFrame exited m)\nhomeLoad✝⁴ : loadLocal m (LocalSlot.bytes CIL.LocalKind.word32 home) = Except.ok (Value.scalar (CIL.Value.i32 x))\nframeLeave✝² :\n ∀ (exited : Frame),\n exited.owned = [home.allocation] → MemoryBelow Memory.empty.nextIdentity Memory.empty (leaveFrame exited m)\nstored : Memory\nstoring :\n storeLocal m (LocalSlot.bytes CIL.LocalKind.word32 home) (Value.scalar (CIL.Value.i32 (x - 1))) =\n Except.ok (LocalSlot.bytes CIL.LocalKind.word32 home, stored)\nstoredFrame :\n LoopFrame [] Memory.empty stored [CIL.LocalKind.word32] [LocalSlot.bytes CIL.LocalKind.word32 home]\n (knownLocals [some (CIL.Value.i32 (x - 1))])\nstoredBelow : MemoryBelow Memory.empty.nextIdentity m stored\nhomeLoad✝³ : loadLocal m (LocalSlot.bytes CIL.LocalKind.word32 home) = Except.ok (Value.scalar (CIL.Value.i32 x))\nframeLeave✝¹ :\n ∀ (exited : Frame),\n exited.owned = (entry.frame home).owned → MemoryBelow Memory.empty.nextIdentity Memory.empty (leaveFrame exited m)\nhomeLoad✝² :\n loadLocal stored (LocalSlot.bytes CIL.LocalKind.word32 home) = Except.ok (Value.scalar (CIL.Value.i32 (x - 1)))\nstoring✝ :\n storeLocal m (LocalSlot.bytes CIL.LocalKind.word32 home) (Value.scalar (CIL.Value.i32 (x - 1#32))) =\n Except.ok (LocalSlot.bytes CIL.LocalKind.word32 home, stored)\nhomeLoad✝¹ : loadLocal m (LocalSlot.bytes CIL.LocalKind.word32 home) = Except.ok (Value.scalar (CIL.Value.i32 x))\nframeLeave✝ :\n ∀ (exited : Frame),\n exited.owned = [home.allocation] → MemoryBelow Memory.empty.nextIdentity Memory.empty (leaveFrame exited m)\nhomeLoad✝ :\n loadLocal stored (LocalSlot.bytes CIL.LocalKind.word32 home) = Except.ok (Value.scalar (CIL.Value.i32 (x - 1#32)))\n⊢ True", @@ -61,7 +62,7 @@ }, { "file": "", - "line": 46, + "line": 47, "column": 0, "severity": "info", "text": "'ExampleProof.correctness' depends on axioms: [propext, sorryAx, Classical.choice, Quot.sound]" @@ -71,7 +72,8 @@ "trace": null, "unknown": { "trace": "not requested; --trace traces each step the proof takes", - "goals": "not requested; --goals reports the goal at each sorry and failing tactic" + "goals": "not requested; --goals reports the goal at each sorry and failing tactic", + "binding": "the run issued no certificate" }, "rerun": [ "leancil", diff --git a/Tests/CliReports/UnformedAccount.json b/Tests/CliReports/UnformedAccount.json index 06a029fe4..876a7c6a5 100644 --- a/Tests/CliReports/UnformedAccount.json +++ b/Tests/CliReports/UnformedAccount.json @@ -1,5 +1,5 @@ { - "schema": 1, + "schema": 2, "result": "failed", "stage": "proof checking", "theorem": "ExampleProof.first_null_throws", @@ -13,6 +13,7 @@ "axioms": null, "statement": null, "specification": null, + "binding": null, "failures": [ { "kind": "elaboration-error", @@ -23,7 +24,7 @@ "line": 105, "column": 2 }, - "message": "cil_checked_steps took no step: the method or instruction lookup, or the first step, did not close; pass the lookup hypothesis, feature facts or step facts\nmemory : Memory\nsecond : Reference\namount : W64\n⊢ invoke Extracted.program 15 depositBoth\n [Value.object ManagedReference.null, Value.object (ManagedReference.address second),\n Value.scalar (CIL.Value.i64 amount)]\n memory =\n Except.error\n { method := depositBoth, instruction := 2, fault := ExecutionFault.thrown \"System.NullReferenceException\" }", + "message": "cil_checked_steps took no step: the method or instruction lookup, or the first step, did not close; pass the lookup hypothesis, feature facts or step facts\nmemory : Memory\nsecond : Reference\namount : W64\n⊢ invoke Extracted.program 15 depositBoth\n [Value.object ManagedReference.null, Value.object (ManagedReference.address second),\n Value.scalar (CIL.Value.i64 amount)]\n memory =\n Except.error\n { method := depositBoth, instruction := 2, fault := ExecutionFault.thrown CIL.ExceptionType.nullReference }", "tactic": "cil_checked_run_step", "stopped": false, "method": { @@ -94,7 +95,8 @@ "trace": null, "unknown": { "trace": "not requested; --trace traces each step the proof takes", - "goals": "not requested; --goals reports the goal at each sorry and failing tactic" + "goals": "not requested; --goals reports the goal at each sorry and failing tactic", + "binding": "the run issued no certificate" }, "rerun": [ "leancil", diff --git a/Tests/CliReports/Verified.json b/Tests/CliReports/Verified.json index a7ce5c313..5d9bfbbf9 100644 --- a/Tests/CliReports/Verified.json +++ b/Tests/CliReports/Verified.json @@ -1,5 +1,5 @@ { - "schema": 1, + "schema": 2, "result": "verified", "stage": null, "theorem": "ExampleProof.correctness", @@ -16,6 +16,86 @@ ], "statement": "ExampleProof.correctness (x y : CIL.W64) :\n ∃ result,\n Except.map Prod.snd\n (CIL.Safety.invoke Extracted.program 4 0\n [CIL.Safety.Value.scalar (CIL.Value.i64 x), CIL.Safety.Value.scalar (CIL.Value.i64 y)]\n CIL.Safety.Memory.empty) =\n Except.ok [CIL.Safety.Value.scalar (CIL.Value.i64 result)] ∧\n BitVec.toNat result = (BitVec.toNat x + BitVec.toNat y) % 2 ^ 64", "specification": [], + "binding": { + "mode": "default", + "digests": null, + "sources": null, + "images": [], + "facts": [ + { + "id": "body-bytes", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "metadata-rows", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "platform-type-kinds", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "enum-underlying-types", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "value-tuple-field-order", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "object-fields", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "layout-content", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "dispatch-catalogs", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "static-data", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "generic-constraints", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "public-key-tokens", + "status": "trusted", + "theorem": null, + "reason": null + }, + { + "id": "import-identities", + "status": "trusted", + "theorem": null, + "reason": null + } + ] + }, "failures": [], "messages": [ { diff --git a/Tests/CliReports/WrongAdd.json b/Tests/CliReports/WrongAdd.json index 553c751c2..251786198 100644 --- a/Tests/CliReports/WrongAdd.json +++ b/Tests/CliReports/WrongAdd.json @@ -1,5 +1,5 @@ { - "schema": 1, + "schema": 2, "result": "failed", "stage": "proof checking", "theorem": "ExampleProof.correctness", @@ -13,6 +13,7 @@ "axioms": null, "statement": null, "specification": null, + "binding": null, "failures": [ { "kind": "elaboration-error", @@ -56,7 +57,8 @@ "trace": null, "unknown": { "trace": "not requested; --trace traces each step the proof takes", - "goals": "not requested; --goals reports the goal at each sorry and failing tactic" + "goals": "not requested; --goals reports the goal at each sorry and failing tactic", + "binding": "the run issued no certificate" }, "rerun": [ "leancil", diff --git a/Tests/CliVerification.cs b/Tests/CliVerification.cs index 080ae6680..48cdf2c2a 100644 --- a/Tests/CliVerification.cs +++ b/Tests/CliVerification.cs @@ -1,8 +1,13 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include ../scripts/ConsumerVerification.cs +#:include ../scripts/LeanDiagnostics.cs -using System.Diagnostics; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Nodes; @@ -16,6 +21,27 @@ var proof = At("Examples/Proofs/Add.lean"); var settingsPath = Path.Combine(output, "verification.json"); +// A run that does not exit as expected is reported with the tool's own output, not as the report file or +// JSON it never wrote. +var refused = (Code: 1, Output: "Invalid: boom\n Stage: proof checking\n", Stdout: "", Stderr: "Invalid: boom\n"); +if (FailureOf(() => Exited(refused, 0)) is not { } refusedMessage || !refusedMessage.Contains("exit code 0") || + !refusedMessage.Contains("got 1") || !refusedMessage.Contains("Invalid: boom\n Stage: proof checking")) + throw new InvalidOperationException("A run that failed was not reported with the tool's output."); + +// Every assertion below runs against a prepared certified library, as CI supplies one, so the notices a +// first run prints while it builds the library never reach the stderr checks. +if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("LEANCIL_CERTIFIED_LIBRARY"))) +{ + var preparedLibrary = At("artifacts/CliCertifiedLibrary"); + if (Directory.Exists(preparedLibrary)) DeleteTree(preparedLibrary); + using var producer = new Program(); + producer.PrepareCertifiedFingerprints(); + Environment.SetEnvironmentVariable("LEANCIL_CERTIFIED_LIBRARY_SHA256", producer.PrepareLibrary(preparedLibrary)); + Environment.SetEnvironmentVariable("LEANCIL_CERTIFIED_LIBRARY", preparedLibrary); +} + +Run(["dotnet", "run", "--file", At("Tests/DeepCli.cs"), "--", .. args], progress: "Deep verification CLI"); + void Verified(params string[] options) { var result = Execute([.. cli, "verify", output, .. options]); @@ -36,8 +62,32 @@ void Verified(params string[] options) throw new InvalidOperationException( "CLI verification did not print the readable statement, subject and messages: " + result.Output); + // A default run checks no binding fact: its stages name no deep-check module, and its certificate lists + // every fact as trusted, once each, in this order. + if (result.Output.Contains("BindingAudit", StringComparison.Ordinal) || + result.Output.Contains("StrictBinding", StringComparison.Ordinal)) + throw new InvalidOperationException("A default verification ran a deep check: " + result.Output); var report = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(output, "verified.json"))); - if (report.GetProperty("schema").GetInt32() != 4 || + string[] bindingFacts = + [ + "body-bytes", "metadata-rows", "platform-type-kinds", "enum-underlying-types", "value-tuple-field-order", + "object-fields", "layout-content", "dispatch-catalogs", "static-data", "generic-constraints", + "public-key-tokens", "import-identities" + ]; + var binding = report.GetProperty("binding"); + var facts = binding.GetProperty("facts").EnumerateArray().ToArray(); + if (binding.GetProperty("mode").GetString() != "default" || + binding.GetProperty("digests").ValueKind != JsonValueKind.Null || + binding.GetProperty("sources").ValueKind != JsonValueKind.Null || + binding.GetProperty("images").GetArrayLength() != 0 || + !facts.Select(fact => fact.GetProperty("id").GetString()).SequenceEqual(bindingFacts) || + facts.Any(fact => fact.GetProperty("status").GetString() != "trusted" || + fact.GetProperty("theorem").ValueKind != JsonValueKind.Null || + fact.GetProperty("reason").ValueKind != JsonValueKind.Null)) + throw new InvalidOperationException("The certificate does not list the 12 binding facts as trusted: " + + binding.GetRawText()); + + if (report.GetProperty("schema").GetInt32() != CertificateSchema || !report.GetProperty("extractionAudit").GetString()!.Contains("Extracted.translation_valid")) throw new InvalidOperationException("Missing mandatory extraction certificate audit in report."); @@ -80,6 +130,34 @@ void StageFixture() throw new InvalidOperationException("Expected a relative proof path in verification settings."); Console.WriteLine("PASS CLI proof selection, persistent settings and theorem statement"); +// Without settings or --proof, verify looks for Proof.lean beside the extraction and never reads the proof +// from an earlier certificate. +File.Delete(settingsPath); +try +{ + var unselected = Exited(Execute([.. cli, "verify", output]), 1); + if (!unselected.Output.Contains("Proof file not found") || + !unselected.Output.Contains("Proof.lean", StringComparison.Ordinal) || + File.Exists(Path.Combine(output, "verified.json"))) + throw new InvalidOperationException("verify took its proof from an earlier certificate: " + unselected.Output); +} +finally { File.WriteAllText(settingsPath, settings); } +Console.WriteLine("PASS CLI verify reads no proof selection from an earlier certificate"); + +// The prepared library serves a verification whose cache holds nothing: no notice on stderr, and no +// library built into the cache. +var emptyCache = At("artifacts/CliEmptyCache"); +if (Directory.Exists(emptyCache)) DeleteTree(emptyCache); +var cold = Exited(Execute([.. cli, "verify", output], environment: new Dictionary + { ["LEANCIL_CACHE_DIR"] = emptyCache }), 0); +if (WithoutStageProgress(cold.Stderr).Length != 0 || !cold.Stdout.StartsWith("Verified: ExampleProof.correctness") || + // A library entry is a directory named by its key; only the verification results live beside it. + Directory.Exists(emptyCache) && + Directory.EnumerateDirectories(emptyCache).Any(entry => Path.GetFileName(entry) != "success-v1")) + throw new InvalidOperationException("A verification with an empty cache built or announced a library: " + + cold.Output); +Console.WriteLine("PASS CLI verification with an empty library cache prints no first-run notice"); + // Elaborator acceptance and an empty axiom list do not establish kernel validity. // Test both a forged selected theorem and a normally checked theorem using a forged helper. var auditOutput = At("artifacts/CliKernelAudit"); @@ -112,6 +190,25 @@ set_option debug.skipKernelTC false } Console.WriteLine("PASS CLI kernel replay rejects unchecked theorems and transitive helpers"); +// A false body check names the method and the part that differs: the same tactic the extraction's certificate +// uses, stated for a body whose method claims a maximum stack depth its bytes do not give. +var falseBody = Path.Combine(auditOutput, "FalseBody.lean"); +File.WriteAllText(falseBody, """ + import Extracted + theorem ExampleProof.falseBody : CIL.Extraction.bodyMatches Extracted.method0Body + { Extracted.method0Original with maxStack := 99 } [] = true := by + cil_body_decodes + """.ReplaceLineEndings("\n") + "\n"); +var falseCertificate = Execute([.. cli, "verify", auditOutput, "--proof", falseBody, "--theorem", + "ExampleProof.falseBody"]); +var falseText = falseCertificate.Stderr.ReplaceLineEndings("\n"); +if (falseCertificate.Code != 1 || falseCertificate.Stdout.Length != 0 || !falseText.Contains("Lean errors (") || + !falseText.Contains("Method System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64): " + + "the decoded method does not match its body bytes or metadata: the maximum stack depth differ")) + throw new InvalidOperationException("verify did not name the method and the false body check: " + + falseCertificate.Output); +Console.WriteLine("PASS CLI verify names the method and the part of a false body check"); + // The unchanged addition contract must fail after changing + to - and rebuilding. var methodsPath = Path.Combine(fixture, "Methods.cs"); var methods = File.ReadAllText(methodsPath); @@ -192,13 +289,13 @@ set_option debug.skipKernelTC false var tracedText = traced.Stdout.ReplaceLineEndings("\n"); var tracedMessages = tracedText[..Math.Max(0, tracedText.IndexOf(" Trace (", StringComparison.Ordinal))]; if (traced.Code != 0 || !tracedText.Contains(" Trace (") || tracedText.Contains("Untraced") || - !tracedText.Contains("Countdown.lean:30:4: [CIL.step] pc 0: CIL.Op.arg 0\n") || + !tracedText.Contains("Countdown.lean:31:4: [CIL.step] pc 0: CIL.Instruction.ldarg 0\n") || !tracedText.Contains("[CIL.step] cil_loop_finish took ") || tracedMessages.Contains("[CIL.step]") || // Each tactic reports its step count once, at its own position. - tracedText.Split("Countdown.lean:30:4: [CIL.step] cil_loop_recurse took ").Length != 2) + tracedText.Split("Countdown.lean:31:4: [CIL.step] cil_loop_recurse took ").Length != 2) throw new InvalidOperationException("verify --trace did not report the steps apart: " + traced.Output); -var tracedReport = JsonNode.Parse(Execute([.. cli, "verify", loopOutput, "--proof", countdownProof, "--trace", - "--format", "json"]).Stdout) as JsonObject; +var tracedReport = JsonNode.Parse(Exited(Execute([.. cli, "verify", loopOutput, "--proof", countdownProof, "--trace", + "--format", "json"]), 0).Stdout) as JsonObject; var steps = tracedReport?["trace"]?.AsArray().Select(step => step!["text"]!.GetValue()).ToArray() ?? []; if (steps.Length == 0 || !steps.All(step => step.StartsWith("[CIL.step] ", StringComparison.Ordinal)) || tracedReport!["unknown"]!.AsObject().ContainsKey("trace") || @@ -223,10 +320,14 @@ set_option debug.skipKernelTC false "ExampleProof.first_null_throws", "--format", "json"]), 1, "UnformedAccount.json", (objectOutput, ""), (unformed, "")); // The trace shows the receiver-checked call and the exception it throws on a null receiver. -var objectTrace = Execute([.. cli, "verify", objectOutput, "--proof", depositBothProof, "--theorem", - "ExampleProof.first_null_throws", "--trace"]).Stdout.ReplaceLineEndings("\n"); -if (!objectTrace.Contains("[CIL.step] pc 2: CIL.Op.callInstance 1 2\n") || - !objectTrace.Contains("changes faults: ExecutionFault.thrown \"System.NullReferenceException\"\n")) +var objectTrace = Exited(Execute([.. cli, "verify", objectOutput, "--proof", depositBothProof, "--theorem", + "ExampleProof.first_null_throws", "--trace"]), 0).Stdout.ReplaceLineEndings("\n"); +var depositExtraction = File.ReadAllText(Path.Combine(objectOutput, "Extracted.lean")); +var nullReceiverCall = objectTrace.Split("[CIL.step] ").Skip(1).Any(entry => + entry.StartsWith("pc 2: CIL.Instruction.callvirt", StringComparison.Ordinal) && + entry.Contains("name := name!\"Deposit\", hasThis := true", StringComparison.Ordinal) && + entry.Contains("changes faults: ExecutionFault.thrown CIL.ExceptionType.nullReference\n", StringComparison.Ordinal)); +if (!nullReceiverCall || !depositExtraction.Contains("def method0Link2 : Link := .direct 1", StringComparison.Ordinal)) throw new InvalidOperationException("verify --trace did not show the null receiver's call: " + objectTrace); Console.WriteLine("PASS CLI reports a proof stuck on a class instance and traces its receiver-checked call"); @@ -244,7 +345,7 @@ set_option debug.skipKernelTC false { var fixtureProof = Path.Combine(auditOutput, "Fixture.lean"); File.WriteAllText(fixtureProof, $"import Extracted\nimport {module}\n"); - var fixtureImport = Execute([.. cli, "verify", auditOutput, "--proof", fixtureProof]); + var fixtureImport = Exited(Execute([.. cli, "verify", auditOutput, "--proof", fixtureProof]), 1); var failure = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(auditOutput, "failed.json"))); if (fixtureImport.Code != 1 || !fixtureImport.Stderr.Contains("Stage: proof selection") || !fixtureImport.Stderr.Contains($"imports {module}, a LeanCIL repository module") || @@ -289,13 +390,13 @@ end ExampleProof """); const string addTwo = "System.UInt32 Examples.Loops::AddTwo(System.UInt32) (method 1) via CIL.Safety.MethodContract.Holds"; -var helper = Execute([.. cli, "verify", subjectOutput, "--proof", subjectProof, "--theorem", "ExampleProof.helper"]) - .Stdout.ReplaceLineEndings("\n"); +var helper = Exited(Execute([.. cli, "verify", subjectOutput, "--proof", subjectProof, "--theorem", + "ExampleProof.helper"]), 0).Stdout.ReplaceLineEndings("\n"); if (!helper.Contains($" Subject: {addTwo}\n") || !helper.Contains($" Premise: {addTwo}\n") || helper.Contains("Subject: System.UInt32 Examples.Loops::TallyTwice") || !helper.Contains(" Extraction entry: System.UInt32 Examples.Loops::TallyTwice(System.UInt32)\n")) throw new InvalidOperationException("Helper theorem was not labelled by its subject: " + helper); -var unfixed = Execute([.. cli, "verify", subjectOutput, "--theorem", "ExampleProof.unfixed"]) +var unfixed = Exited(Execute([.. cli, "verify", subjectOutput, "--theorem", "ExampleProof.unfixed"]), 0) .Stdout.ReplaceLineEndings("\n"); if (!unfixed.Contains(" Subject: a method index the statement does not fix via CIL.Safety.MethodContract.Holds\n") || unfixed.Contains("Examples.Loops::AddTwo") || unfixed.Contains("Subject: System.UInt32")) @@ -343,7 +444,7 @@ end ExampleProof // The second run reuses the checked proof, which carries the specification with its statement. foreach (var reuse in new[] { false, true }) { - var specified = Execute([.. cli, "verify", specificationOutput, "--proof", specifiedProof]); + var specified = Exited(Execute([.. cli, "verify", specificationOutput, "--proof", specifiedProof]), 0); var recorded = JsonNode.Parse(File.ReadAllText(Path.Combine(specificationOutput, "verified.json")))! ["specification"]?.AsArray().Select(definition => (definition!["name"]!.GetValue(), definition["text"]!.GetValue())).ToArray() ?? []; @@ -355,8 +456,8 @@ end ExampleProof $"The report does not show the specification the theorem uses (reused: {reuse}): " + specified.Output + string.Join('\n', recorded)); } - var specifiedJson = JsonNode.Parse(Execute([.. cli, "verify", specificationOutput, "--proof", specifiedProof, - "--format", "json"]).Stdout)?["specification"]?.AsArray(); + var specifiedJson = JsonNode.Parse(Exited(Execute([.. cli, "verify", specificationOutput, "--proof", + specifiedProof, "--format", "json"]), 0).Stdout)?["specification"]?.AsArray(); if (specifiedJson is not { Count: 2 } || specifiedJson[0]!["text"]!.GetValue() != postconditionText) throw new InvalidOperationException("verify --format json does not report the specification: " + specifiedJson?.ToJsonString()); @@ -390,8 +491,8 @@ end ExampleProof foreach (var (options, cached) in new[] { (new[] { "--fresh" }, (bool?)false), (Array.Empty(), null), (Array.Empty(), true) }) { - var consumer = Execute([.. cli, "verify", consumerOutput, "--proof", consumerProof, - "--proof-root", consumerPackage, .. options]); + var consumer = Exited(Execute([.. cli, "verify", consumerOutput, "--proof", consumerProof, + "--proof-root", consumerPackage, .. options]), 0); var consumerText = consumer.Stdout.ReplaceLineEndings("\n"); var libraryBuild = JsonSerializer.Deserialize( File.ReadAllText(Path.Combine(consumerOutput, "verified.json"))).GetProperty("libraryBuild").GetString(); @@ -424,6 +525,10 @@ end ExampleProof unfinished.Output); RequireSourceLocations(unfinishedText, "artifacts/CliConsumer/Package/Proofs/Unfinished.lean:5:2 (building Proofs.Unfinished)"); +// The failed package check reaches a reader that expected it to pass as the tool's output, not as a missing report. +if (FailureOf(() => Exited(unfinished, 0)) is not { } unfinishedFailureText || + !unfinishedFailureText.Contains("got 1") || !unfinishedFailureText.Contains("Stage: proof checking")) + throw new InvalidOperationException("A failed --proof-root check was not reported with the tool's output."); // A package's proof shows its goals as a standalone one does. var unfinishedGoals = Execute([.. cli, "verify", consumerOutput, "--proof", unfinishedProof, "--proof-root", consumerPackage, "--goals"]).Stderr.ReplaceLineEndings("\n"); @@ -437,7 +542,8 @@ end ExampleProof File.WriteAllText(holeProof, "import Extracted\ntheorem ExampleProof.correctness : 1 + 1 = 2 := by\n sorry\n"); var hole = Execute([.. cli, "verify", consumerOutput, "--proof", holeProof, "--proof-root", consumerPackage, "--goals"]); -var holeText = hole.Stderr.ReplaceLineEndings("\n"); +// A build that runs past the progress interval reports its stage first, as it does under machine load. +var holeText = WithoutStageProgress(hole.Stderr.ReplaceLineEndings("\n")); if (hole.Code != 1 || !holeText.StartsWith("Invalid: 1 goal remains in artifacts/CliConsumer/Package/Proofs/Hole.lean.\n") || !holeText.Contains(" Stage: open goals\n") || !holeText.Contains(" Goals (1):\n" + @@ -457,35 +563,39 @@ end ExampleProof unfinishedJson.Output); // `verify-batch --format json` prints one document with each request's `verify --format json` report, and -// `--trace` adds each request's steps; the rejected request is last, so the output keeps its settings. +// `--trace` adds each request's steps; the failing request is last, so the output keeps its settings. var batchManifest = Path.Combine(consumerRoot, "requests.json"); File.WriteAllText(batchManifest, JsonSerializer.Serialize(new { schema = 1, proofRoot = consumerPackage, requests = new object[] { new { output = consumerOutput, proof = consumerProof, theorem = "ExampleProof.correctness" }, - new { output = consumerOutput, proof = unfinishedProof, theorem = "ExampleProof.correctness", - expect = "rejected" } + new { output = consumerOutput, proof = unfinishedProof, theorem = "ExampleProof.correctness" } } })); var batch = Execute([.. cli, "verify-batch", batchManifest, "--format", "json", "--trace"]); var batchReport = JsonNode.Parse(batch.Stdout) as JsonObject; var batchRequests = batchReport?["requests"]?.AsArray(); -if (batch.Code != 0 || WithoutStageProgress(batch.Stderr).Length != 0 || batchReport?["schema"]?.GetValue() != 1 || - batchReport["passed"]?.GetValue() != 2 || batchRequests is not { Count: 2 } || +if (batch.Code != 1 || WithoutStageProgress(batch.Stderr).Length != 0 || + batchReport?["schema"]?.GetValue() != 1 || + batchReport["passed"]?.GetValue() != 1 || batchRequests is not { Count: 2 } || batchRequests[0]!["status"]!.GetValue() != "verified" || batchRequests[0]!["report"]!["result"]!.GetValue() != "verified" || batchRequests[0]!["report"]!["trace"] is not JsonArray { Count: > 0 } verifiedSteps || !verifiedSteps.All(step => step!["text"]!.GetValue().StartsWith("[CIL.step] ", StringComparison.Ordinal)) || - batchRequests[1]!["status"]!.GetValue() != "rejected" || + batchRequests[1]!["status"]!.GetValue() != "failed" || batchRequests[1]!["report"]!["result"]!.GetValue() != "failed" || batchRequests[1]!["report"]!["failures"]?[0]?["source"]?["file"]?.GetValue() != - Path.GetFullPath(unfinishedProof)) + Path.GetFullPath(unfinishedProof) || + // The verified request's report carries its certificate's binding section; the failing one issued none. + batchRequests[0]!["report"]!["binding"]?["facts"] is not JsonArray { Count: 12 } batchFacts || + batchFacts.Any(fact => fact!["status"]!.GetValue() != "trusted") || + batchRequests[1]!["report"]!["binding"] is not null) throw new InvalidOperationException("verify-batch --format json --trace did not print one report per request: " + batch.Output); var batchText = Execute([.. cli, "verify-batch", batchManifest, "--trace"]); -if (batchText.Code != 0 || !batchText.Stdout.Contains("PASS verified") || !batchText.Stdout.Contains(" Trace (") || - !batchText.Stdout.Contains("Batch: 2 passed, 0 failed of 2 requests.")) +if (batchText.Code != 1 || !batchText.Stdout.Contains("PASS verified") || !batchText.Stdout.Contains(" Trace (") || + !batchText.Stdout.Contains("Batch: 1 passed, 1 failed of 2 requests.")) throw new InvalidOperationException("verify-batch --trace did not print each request's trace: " + batchText.Output); Console.WriteLine("PASS CLI verify-batch reports each request as JSON and traces each request's steps"); Console.WriteLine("PASS CLI shows Lean messages, with control characters escaped, when a --proof-root proof fails"); @@ -502,23 +612,43 @@ end ExampleProof !written.Stdout.Contains("Holes: 3 obligations, 0 paths not outlined")) throw new InvalidOperationException("new-proof did not write the Add outline: " + written.Output); var outlineText = File.ReadAllText(addOutline); -var refusedOutline = Execute([.. cli, "verify", outlined, "--proof", addOutline]); +var refusalTrace = Path.Combine(outlines, "RefusalTrace"); +var goalsTrace = Path.Combine(outlines, "GoalsTrace"); +Dictionary TraceEnvironment(string directory) => new() +{ + ["LEANCIL_TRACE_DIRECTORY"] = directory, + ["LEANCIL_DISABLE_SUCCESS_CACHE"] = "true" +}; +JsonElement[] AfterProofCheck(string directory) +{ + var records = Directory.EnumerateFiles(directory, "*.jsonl").SelectMany(File.ReadLines) + .Select(line => JsonSerializer.Deserialize(line)) + .OrderBy(record => record.GetProperty("startedAtUtc").GetDateTimeOffset()).ToArray(); + var proofChecks = records.Select((record, index) => (record, index)).Where(item => + item.record.GetProperty("stage").GetString()!.StartsWith("Checking Proof: ", StringComparison.Ordinal)) + .ToArray(); + if (proofChecks.Length != 1 || proofChecks[0].record.GetProperty("exitCode").GetInt32() != 0) + throw new InvalidOperationException("Expected one successful proof check in " + directory); + return records[(proofChecks[0].index + 1)..]; +} +bool ReplaysOrAudits(JsonElement record) => + record.GetProperty("stage").GetString()!.Contains("replay", StringComparison.OrdinalIgnoreCase) || + Path.GetFileNameWithoutExtension(record.GetProperty("command")[0].GetString()) is + "leanchecker" or "leancil-audit"; +var refusedOutline = Execute([.. cli, "verify", outlined, "--proof", addOutline], + environment: TraceEnvironment(refusalTrace)); if (refusedOutline.Code == 0 || !refusedOutline.Output.Contains("Unapproved proof axioms: sorryAx") || refusedOutline.Output.Contains(": error")) throw new InvalidOperationException("The unedited Add outline was not refused for its holes alone: " + refusedOutline.Output); // `--goals` prints the goal at each hole, once each in source order and without the proof's warnings, and -// stops before replay and audit, so it answers sooner than the verification that refuses the file. +// ends the run at the proof check: the kernel replay and audit that refuse the file never start. var holes = Regex.Matches(outlineText.ReplaceLineEndings("\n"), @"(?<=:= |=> |^\s+)sorry\b", RegexOptions.Multiline) .Select(hole => outlineText.ReplaceLineEndings("\n")[..hole.Index].Split('\n')) .Select(before => (Line: before.Length, Column: before[^1].Length)).ToArray(); -var refusal = Stopwatch.StartNew(); -Execute([.. cli, "verify", outlined, "--proof", addOutline]); -refusal.Stop(); -var answer = Stopwatch.StartNew(); -var outlineGoals = Execute([.. cli, "verify", outlined, "--proof", addOutline, "--goals"]); -answer.Stop(); -var outlineGoalsText = outlineGoals.Stderr.ReplaceLineEndings("\n"); +var outlineGoals = Execute([.. cli, "verify", outlined, "--proof", addOutline, "--goals"], + environment: TraceEnvironment(goalsTrace)); +var outlineGoalsText = WithoutStageProgress(outlineGoals.Stderr.ReplaceLineEndings("\n")); var reportedHoles = Regex.Matches(outlineGoalsText, @"^ \S*AddProof\.lean:(\d+):(\d+): sorry$", RegexOptions.Multiline) .Select(goal => (Line: int.Parse(goal.Groups[1].Value), Column: int.Parse(goal.Groups[2].Value))).ToArray(); @@ -530,11 +660,16 @@ end ExampleProof outlineGoalsText.Contains("[LeanCIL goal") || File.Exists(Path.Combine(outlined, "verified.json"))) throw new InvalidOperationException("verify --goals did not print one goal per hole in source order: " + outlineGoals.Output); -if (answer.Elapsed >= refusal.Elapsed) - throw new InvalidOperationException($"verify --goals took {answer.Elapsed.TotalSeconds:F1}s, no less than " + - $"the {refusal.Elapsed.TotalSeconds:F1}s of the verification that refuses the outline."); -var outlineGoalsJson = JsonNode.Parse(Execute([.. cli, "verify", outlined, "--proof", addOutline, "--goals", - "--format", "json"]).Stdout) as JsonObject; +var refusalStages = AfterProofCheck(refusalTrace); +var goalsStages = AfterProofCheck(goalsTrace); +string Stages(JsonElement[] records) => string.Join("; ", records.Select(record => + $"{record.GetProperty("stage").GetString()} " + + $"({Path.GetFileNameWithoutExtension(record.GetProperty("command")[0].GetString())})")); +if (!refusalStages.Any(ReplaysOrAudits) || goalsStages.Any(ReplaysOrAudits)) + throw new InvalidOperationException("verify --goals did not end at the proof check. After it, verify --goals " + + $"ran [{Stages(goalsStages)}] and the refused verification ran [{Stages(refusalStages)}]."); +var outlineGoalsJson = JsonNode.Parse(Exited(Execute([.. cli, "verify", outlined, "--proof", addOutline, "--goals", + "--format", "json"]), 1).Stdout) as JsonObject; var jsonGoals = outlineGoalsJson?["goals"]?.AsArray(); if (outlineGoalsJson?["result"]?.GetValue() != "failed" || outlineGoalsJson["stage"]?.GetValue() != "open goals" || @@ -547,8 +682,8 @@ end ExampleProof !outlineGoalsJson["rerun"]!.AsArray().Any(argument => argument!.GetValue() == "--goals")) throw new InvalidOperationException("verify --goals --format json did not report each hole's goal: " + outlineGoalsJson?.ToJsonString()); -Console.WriteLine($"PASS CLI --goals prints one goal per hole in source order in {answer.Elapsed.TotalSeconds:F1}s, " + - $"under the {refusal.Elapsed.TotalSeconds:F1}s of the refused verification, and the goal of a failing tactic"); +Console.WriteLine("PASS CLI --goals prints one goal per hole in source order, starts no kernel replay or audit " + + "after the proof check, and prints the goal of a failing tactic"); var overwrite = Execute([.. cli, "new-proof", outlined, addOutline]); if (overwrite.Code == 0 || !overwrite.Stderr.Contains("already exists; new-proof never overwrites a proof") || File.ReadAllText(addOutline) != outlineText) @@ -582,13 +717,21 @@ end ExampleProof if (args.Length == 0) { var packages = Path.Combine(output, "packages"); - if (Directory.Exists(packages)) Directory.Delete(packages, true); - Run(["dotnet", "pack", At("Extractor"), "-c", "Release", "-o", packages], progress: "CLI development pack"); + var packBuild = Path.Combine(output, "pack-build"); + foreach (var directory in new[] { packages, packBuild }) + if (Directory.Exists(directory)) Directory.Delete(directory, true); + // Later suites of the same job run the extractor this one found, so the pack builds into its own directories. + var sharedExtractor = Sha(ExtractorPath); + Run(["dotnet", "pack", At("Extractor"), "-c", "Release", "-o", packages, + $"-p:OutputPath={Path.Combine(packBuild, "bin")}/", + $"-p:IntermediateOutputPath={Path.Combine(packBuild, "obj")}/"], progress: "CLI development pack"); + if (Sha(ExtractorPath) != sharedExtractor) + throw new InvalidOperationException("The development pack rebuilt the shared extractor at " + ExtractorPath); var package = Directory.GetFiles(packages, "Nethermind.LeanCIL.*.nupkg").Single(); var packageVersion = Path.GetFileNameWithoutExtension(package)["Nethermind.LeanCIL.".Length..]; var unpacked = Path.Combine(packages, "unpacked"); System.IO.Compression.ZipFile.ExtractToDirectory(package, unpacked); - var packedVersion = Execute(["dotnet", Path.Combine(unpacked, "tools/net10.0/any/Nethermind.LeanCIL.dll"), + var packedVersion = Execute(["dotnet", Path.Combine(unpacked, "tools/net11.0/any/Nethermind.LeanCIL.dll"), "--version"]); var reported = packedVersion.Stdout.ReplaceLineEndings("\n").Split('\n')[0]; if (packedVersion.Code != 0 || !packageVersion.Contains("-dev.") || reported != "leancil " + packageVersion) @@ -601,12 +744,25 @@ end ExampleProof Run(["dotnet", "run", "--file", At("Tests/CliImportedKernelAudit.cs")], streamOutput: true, progress: "CLI imported helper kernel audit"); +// A run that exited otherwise has not written the report the caller reads next, so its own output is what +// the failure must carry. +static (int Code, string Output, string Stdout, string Stderr) Exited( + (int Code, string Output, string Stdout, string Stderr) run, int code) => + run.Code == code ? run + : throw new InvalidOperationException($"Expected exit code {code}, got {run.Code}: " + run.Output); + +static string? FailureOf(Action action) +{ + try { action(); return null; } + catch (InvalidOperationException error) { return error.Message; } +} + // A golden report pins every field a tool reads; paths and hashes vary by machine and are named instead. static void RequireReport((int Code, string Output, string Stdout, string Stderr) run, int code, string golden, params (string Path, string Name)[] paths) { var report = JsonNode.Parse(run.Stdout) as JsonObject; - if (run.Code != code || WithoutStageProgress(run.Stderr).Length != 0 || report?["schema"]?.GetValue() != 1) + if (run.Code != code || WithoutStageProgress(run.Stderr).Length != 0 || report?["schema"]?.GetValue() != 2) throw new InvalidOperationException($"verify --format json did not exit {code} with one report: " + run.Output); var actual = Normalized(report, paths)!.ToJsonString(new JsonSerializerOptions { diff --git a/Tests/CodeAt.lean b/Tests/CodeAt.lean index e9355723b..52eeffea6 100644 --- a/Tests/CodeAt.lean +++ b/Tests/CodeAt.lean @@ -16,7 +16,7 @@ namespace Tests.CodeAt open CIL.Safety /-- A seven-instruction segment repeated at computed positions. -/ -def sevenInstructions : List CIL.Op := [.arg 0, .arg 1, .add, .nop, .nop, .nop, .nop] +def sevenInstructions : List CIL.Instruction := [.ldarg 0, .ldarg 1, .add, .nop, .nop, .nop, .nop] /-- Four identical segments preceded by one instruction. -/ @[cil_code] def repeatedBody : CIL.Method := @@ -77,20 +77,23 @@ theorem computed_placement {body : CIL.Method} {start : Nat} (index : Fin 4) #guard_msgs in example : repeatedBody.code[30]? = some .ret := by cil_lookup [] -/-- A placement supplies the budget even when the rest of the method is abstract. -/ +/-- A placement, whole argument reads and return metadata suffice even when the rest of the + method is abstract. -/ theorem placed_run {program : CIL.Program} {method start : Nat} {body : CIL.Method} (found : program[method]? = some body) - (placed : CodeAt body start [.arg 0, .arg 1, .add, .ret]) - (returns : body.returnsValue = true) (frame : Frame) (memory : Memory) + (placed : CodeAt body start [.ldarg 0, .ldarg 1, .add, .ret]) + (first : body.argumentAccess 0 = .value none) (second : body.argumentAccess 1 = .value none) + (returns : body.returnsValue = true) (narrow : body.returnNarrow = none) + (frame : Frame) (memory : Memory) (left right : CIL.W64) : ∃ fuel, run program fuel method start [.scalar (.i64 left), .scalar (.i64 right)] frame [] memory = .ok (leaveFrame frame memory, [.scalar (.i64 (left + right))]) := by - cil_checked_steps [found, placed, returns] + cil_checked_steps [found, placed, first, second, returns, narrow] #print axioms placed_run /-- The segment `left + right` over the first two arguments. -/ -def sumSegment : List CIL.Op := [.arg 0, .arg 1, .add] +def sumSegment : List CIL.Instruction := [.ldarg 0, .ldarg 1, .add] -- Without a placement the abstract body's instructions are unknown, so stepping cannot start. /-- error: cil_checked_steps took no step: the method or instruction lookup -/ @@ -104,17 +107,18 @@ example {program : CIL.Program} {method fuel start : Nat} {body : CIL.Method} [.scalar (.i64 (left + right))] memory := by cil_checked_steps [found] -/-- Placing `sumSegment` at any `start` of a body is enough to run it: from `start` the run - reaches `start + 3` with the sum pushed. The only facts about the body are its lookup in - the program and the placement. -/ +/-- Placing `sumSegment` at any `start` of a body that reads its first two arguments whole + is enough to run it: from `start` the run reaches `start + 3` with the sum pushed. The only + facts about the body are its lookup in the program, the placement and the two reads. -/ theorem run_sum_segment {program : CIL.Program} {method fuel start : Nat} {body : CIL.Method} (found : program[method]? = some body) (placed : CodeAt body start sumSegment) + (first : body.argumentAccess 0 = .value none) (second : body.argumentAccess 1 = .value none) (memory : Memory) (frame : Frame) (left right : CIL.W64) : run program (fuel + 3) method start [.scalar (.i64 left), .scalar (.i64 right)] frame [] memory = run program fuel method (start + 3) [.scalar (.i64 left), .scalar (.i64 right)] frame [.scalar (.i64 (left + right))] memory := by - cil_checked_steps [found, placed] + cil_checked_steps [found, placed, first, second] /-- Nested additions and a single offset fetch the same placed instruction. -/ theorem lookup_nested {body : CIL.Method} {start : Nat} @@ -137,7 +141,7 @@ theorem placement_limits {body _other : CIL.Method} {start : Nat} (_placed : CodeAt body start sumSegment) : True := by fail_if_success have : body.code[start + 2]? = some .ret := by cil_lookup [_placed] fail_if_success have : body.code[start + 3]? = some .ret := by cil_lookup [_placed] - fail_if_success have : _other.code[start]? = some (.arg 0) := by cil_lookup [_placed] + fail_if_success have : _other.code[start]? = some (.ldarg 0) := by cil_lookup [_placed] trivial #print axioms placement_limits @@ -150,19 +154,20 @@ theorem placement_limits {body _other : CIL.Method} {start : Nat} theorem run_sum_in_region {program : CIL.Program} {method fuel : Nat} {body : CIL.Method} (found : program[method]? = some body) (placed : CodeAt body 2 ([.nop, .nop] ++ sumSegment ++ [.ret])) + (first : body.argumentAccess 0 = .value none) (second : body.argumentAccess 1 = .value none) (memory : Memory) (frame : Frame) (left right : CIL.W64) : run program (fuel + 3) method 4 [.scalar (.i64 left), .scalar (.i64 right)] frame [] memory = run program fuel method 7 [.scalar (.i64 left), .scalar (.i64 right)] frame [.scalar (.i64 (left + right))] memory := - run_sum_segment found placed.middle memory frame left right + run_sum_segment found placed.middle first second memory frame left right /-- info: 'Tests.CodeAt.run_sum_in_region' depends on axioms: [propext, Quot.sound] -/ #guard_msgs in #print axioms run_sum_in_region /-- A concrete body holding `sumSegment` at pc 4. -/ def placedMethod : CIL.Method := { - code := [.nop, .nop, .nop, .nop, .arg 0, .arg 1, .add, .ret] + code := [.nop, .nop, .nop, .nop, .ldarg 0, .ldarg 1, .add, .ret] locals := [], returnsValue := true } -- A concrete placement closes by `rfl`; a shifted one is refuted by the instruction it misplaces. @@ -176,22 +181,22 @@ theorem run_placed_method (memory : Memory) (frame : Frame) (left right : CIL.W6 run [placedMethod] 5 0 4 [.scalar (.i64 left), .scalar (.i64 right)] frame [] memory = run [placedMethod] 2 0 7 [.scalar (.i64 left), .scalar (.i64 right)] frame [.scalar (.i64 (left + right))] memory := - run_sum_segment (fuel := 2) (start := 4) (body := placedMethod) rfl rfl memory frame - left right + run_sum_segment (fuel := 2) (start := 4) (body := placedMethod) rfl rfl rfl rfl memory + frame left right /-- info: 'Tests.CodeAt.run_placed_method' depends on axioms: [propext, Quot.sound] -/ #guard_msgs in #print axioms run_placed_method -- The positional hypotheses an older segment theorem threads give the same placement. -example {body : CIL.Method} {tail : List CIL.Op} +example {body : CIL.Method} {tail : List CIL.Instruction} (positioned : body.code = [.nop, .nop, .nop, .nop] ++ sumSegment ++ tail) : CodeAt body 4 sumSegment := CodeAt.of_eq_append positioned rfl -- Adjacent placements compose, and the parts come back out at the same positions. example {body : CIL.Method} (first : CodeAt body 4 sumSegment) (second : CodeAt body 7 [.ret]) : - CodeAt body 4 (sumSegment ++ [.ret]) ∧ CodeAt body 5 [.arg 1, .add, .ret] ∧ - CodeAt body 4 [.arg 0] := + CodeAt body 4 (sumSegment ++ [.ret]) ∧ CodeAt body 5 [.ldarg 1, .add, .ret] ∧ + CodeAt body 4 [.ldarg 0] := let whole := first.append second ⟨whole, whole.drop 1, whole.take 1⟩ @@ -208,7 +213,7 @@ example {body : CIL.Method} (placed : CodeAt body 4 sumSegment) -- The bound is needed: the empty segment is placed past the end of any code. example : CodeAt placedMethod 100 [] := CodeAt.nil -example : ¬ ∃ leading tail : List CIL.Op, +example : ¬ ∃ leading tail : List CIL.Instruction, placedMethod.code = leading ++ [] ++ tail ∧ leading.length = 100 := by rintro ⟨leading, tail, split, length⟩ have total := congrArg List.length split @@ -217,7 +222,7 @@ example : ¬ ∃ leading tail : List CIL.Op, -- A placement's first instruction is at its start, and the rest follows it. example {body : CIL.Method} (placed : CodeAt body 4 sumSegment) : - body.code[4]? = some (.arg 0) ∧ CodeAt body 5 [.arg 1, .add] := + body.code[4]? = some (.ldarg 0) ∧ CodeAt body 5 [.ldarg 1, .add] := CodeAt.cons_iff.mp placed -- A nonempty placement lies within the code, so it recovers the positional form on its own. diff --git a/Tests/CompilerCorpus/Corpus.cs b/Tests/CompilerCorpus/Corpus.cs index e2af7e73d..a733a46c8 100644 --- a/Tests/CompilerCorpus/Corpus.cs +++ b/Tests/CompilerCorpus/Corpus.cs @@ -5,7 +5,7 @@ [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyConfiguration("Release")] -[assembly: TargetFramework(".NETCoreApp,Version=v10.0")] +[assembly: TargetFramework(".NETCoreApp,Version=v11.0")] public static class Corpus { diff --git a/Tests/CompilerCorpusCoverage.cs b/Tests/CompilerCorpusCoverage.cs index f6eeae4e8..823c75a12 100644 --- a/Tests/CompilerCorpusCoverage.cs +++ b/Tests/CompilerCorpusCoverage.cs @@ -40,7 +40,7 @@ static SortedDictionary WholeCorpus(string compiler, CompilerToolc File.WriteAllText(attributes, """ [assembly: System.Reflection.AssemblyVersion("1.0.0.0")] [assembly: System.Reflection.AssemblyConfiguration("Release")] - [assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v10.0")] + [assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v11.0")] """); var implicitUsings = Path.Combine(directory, "GlobalUsings.cs"); File.WriteAllText(implicitUsings, """ diff --git a/Tests/CompilerCoverage.cs b/Tests/CompilerCoverage.cs index efcca480a..94320741e 100644 --- a/Tests/CompilerCoverage.cs +++ b/Tests/CompilerCoverage.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include CompilerToolchain.cs #:include CompilerCorpusCoverage.cs #:include GeneratedEvidence.cs diff --git a/Tests/CompilerCoverageBaseline.tsv b/Tests/CompilerCoverageBaseline.tsv index 86cfc4817..b039d4a68 100644 --- a/Tests/CompilerCoverageBaseline.tsv +++ b/Tests/CompilerCoverageBaseline.tsv @@ -134,6 +134,9 @@ Examples:System.Boolean Examples.ComputedFlags::get_NotOff() A A A A A A A A Examples:System.Boolean Examples.ComputedFlags::get_Outer() A A A A A A A A Examples:System.Boolean Examples.ComputedFlags::get_Ping() B B B B B B B B Examples:System.Boolean Examples.ComputedFlags::get_Pong() B B B B B B B B +Examples:System.Boolean Examples.DivisionIntrinsics::EqualBytes256(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1) A A A A A A A A +Examples:System.Boolean Examples.DivisionIntrinsics::EqualSigned512(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1) A A A A A A A A +Examples:System.Boolean Examples.DivisionIntrinsics::EqualWords128(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.Runtime.Intrinsics.Vector128`1) A A A A A A A A Examples:System.Boolean Examples.DivisionIntrinsics::UnsafeEquals(System.UInt64) A A A A A A A A Examples:System.Boolean Examples.DoublingAllowance::Examples.IAllowance.Spend(Examples.DoublingAllowance&) B B B B B B B B Examples:System.Boolean Examples.DoublingAllowance::Spend(Examples.DoublingAllowance&,System.UInt64) A A A A A A A A @@ -244,8 +247,12 @@ Examples:System.Int16& Examples.UnsafeLayouts::Add4nint(System.Int16&,System.Int Examples:System.Int16& Examples.UnsafeLayouts::Add4nuint(System.Int16&,System.UIntPtr) A A A A A A A A Examples:System.Int16[] Examples.PrimitiveArrays::AllocateShorts(System.Int32) A A A A A A A A Examples:System.Int32 Examples.AggregateArrays::ConstructedLength(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) A A A A A A A A +Examples:System.Int32 Examples.Arithmetic::AtLeastFive(System.Int32) A A A A A A A A +Examples:System.Int32 Examples.Arithmetic::FiveToOne(System.Int32) A A A A A A A A Examples:System.Int32 Examples.Arithmetic::Larger(System.Int32,System.Int32) A A A A A A A A Examples:System.Int32 Examples.Arithmetic::Unsupported(System.Int32) B B B B B B B B +Examples:System.Int32 Examples.Arithmetic::WordAdjusted(System.Int32) A A A A A A A A +Examples:System.Int32 Examples.Arithmetic::WordSized(System.Int32) A A A A A A A A Examples:System.Int32 Examples.ArrayElementReferences::Read(System.Int32[],System.Int32) A A A A A A A A Examples:System.Int32 Examples.ArrayElementReferences::Write(System.Int32[],System.Int32,System.Int32) A A A A A A A A Examples:System.Int32 Examples.BinarySpans::TryWriteBig(System.Span`1) A A A A A A A A @@ -306,6 +313,7 @@ Examples:System.Int32 Examples.ExceptionDocumentation::CheckedNarrow(System.Int6 Examples:System.Int32 Examples.ExceptionDocumentation::Clamp(System.Int32,System.Int32,System.Int32) A A A A A A A A Examples:System.Int32 Examples.ExplicitCounterValue::Examples.ICounterValue.Read() A A A A A A A A Examples:System.Int32 Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Read() B B B B B B B B +Examples:System.Int32 Examples.Gauge::BelowFive() A A A A A A A A Examples:System.Int32 Examples.Gauge::Blend(System.Int32,System.Int32) A A A A A A A A Examples:System.Int32 Examples.Gauge::Blended(System.Int32,System.Int32) A A A A A A A A Examples:System.Int32 Examples.Gauge::Bounded(System.Int32,System.Int32) A A A A A A A A @@ -318,7 +326,10 @@ Examples:System.Int32 Examples.Gauge::Clamp(System.Int32,System.Int32,System.Int Examples:System.Int32 Examples.Gauge::Clamped(System.Int32,System.Int32,System.Int32) A A A A A A A A Examples:System.Int32 Examples.Gauge::Current() A A A A A A A A Examples:System.Int32 Examples.Gauge::Doubled(System.Int32) A A A A A A A A +Examples:System.Int32 Examples.Gauge::Floor(System.Int32) A A A A A A A A +Examples:System.Int32 Examples.Gauge::Floored(System.Int32) A A A A A A A A Examples:System.Int32 Examples.Gauge::Gap(System.Int32,System.Int32) A A A A A A A A +Examples:System.Int32 Examples.Gauge::IsFive() A A A A A A A A Examples:System.Int32 Examples.Gauge::Margin() A A A A A A A A Examples:System.Int32 Examples.Gauge::Raise(Examples.Gauge,System.Int32,System.Int32) A A A A A A A A Examples:System.Int32 Examples.Gauge::RaisedLevel() A A A A A A A A @@ -358,6 +369,7 @@ Examples:System.Int32 Examples.ItemPool::Trim(System.Int32) A A A A A A A A Examples:System.Int32 Examples.ItemPool::get_Available() A A A A A A A A Examples:System.Int32 Examples.ItemPool::get_Capacity() A A A A A A A A Examples:System.Int32 Examples.ItemPool::get_Rented() A A A A A A A A +Examples:System.Int32 Examples.Loops::CountToFive(System.Int32) A A A A A A A A Examples:System.Int32 Examples.Loops::Drain(System.ReadOnlySpan`1) A A A A A A A A Examples:System.Int32 Examples.ManagedDispatch::Address(System.Int32) B B B B B B B B Examples:System.Int32 Examples.ManagedDispatch::Dispatch(method System.Int32 *(System.Int32)[],System.Int32,System.Int32) B B B B B B B B @@ -416,6 +428,7 @@ Examples:System.Int32 Examples.SampleArrays::Sum(Examples.Samples,System.Int32[] Examples:System.Int32 Examples.Sequential::PackedSize() A A A A A A A A Examples:System.Int32 Examples.Sequential::RecordSize() A A A A A A A A Examples:System.Int32 Examples.SpanConstructors::AggregateLocal(Examples.AggregateSpans/Words&) A A A A A A A A +Examples:System.Int32 Examples.SpanConstructors::BytePointerLocal(System.Byte*,System.Int32) A A A A A A A A Examples:System.Int32 Examples.SpanConstructors::LayoutLocal(Examples.AggregateSpans/VectorRecord*,System.Int32) B B B B B B B B Examples:System.Int32 Examples.SpanConstructors::Local(System.Int32&) A A A A A A A A Examples:System.Int32 Examples.SpanHelpers::Count(System.ReadOnlySpan`1) B B B B B B B B @@ -624,6 +637,8 @@ Examples:System.Runtime.Intrinsics.Vector128`1 Examples.MarshaledMe Examples:System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::Read128(System.Byte&) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::ReadAligned128(System.Byte*) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128Byte(Examples.ValueBitCasts/Pair) A A A A A A A A +Examples:System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadBytes128(Examples.VectorFields/Bytes128) A A A A A A A A +Examples:System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadInts128(Examples.VectorFields/Ints128) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt32(Examples.ValueBitCasts/Pair) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector128`1 Examples.AggregateArrays::ReadVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector128`1 Examples.AggregateSpans::ReadVector(System.ReadOnlySpan`1>) A A A A A A A A @@ -642,10 +657,14 @@ Examples:System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLa Examples:System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::Half(Examples.FourLimbs&) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::SubtractVector(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::Shuffle256(System.ReadOnlySpan`1,System.ReadOnlySpan`1) B B B B B B B B +Examples:System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::ShuffleCrossHalf(System.ReadOnlySpan`1,System.ReadOnlySpan`1) B B B B B B B B +Examples:System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::ShuffleIgnoreHighBit(System.ReadOnlySpan`1,System.ReadOnlySpan`1) B B B B B B B B Examples:System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int32(Examples.ValueBitCasts/Limbs) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int64(Examples.ValueBitCasts/Limbs) A A A A A A A A +Examples:System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadLongs256(Examples.VectorFields/Longs256) B B B B B B B B Examples:System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::CreateWords(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt32(Examples.ValueBitCasts/Limbs) A A A A A A A A +Examples:System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadUInts256(Examples.VectorFields/UInts256) B B B B B B B B Examples:System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeReferences::Vector(Examples.FourLimbs&) A A A A A A A A Examples:System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::SignWindow(System.UInt64,System.UInt64,System.UInt64,System.UInt64,System.UInt64,System.UInt64,System.UInt64,System.UInt64) B B B B B B B B Examples:System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::CopyLocal(Examples.ValueBitCasts/Limbs) A A A A A A A A @@ -722,6 +741,7 @@ Examples:System.UInt32 Examples.Arguments::Alias(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Arguments::CallerIsolation(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Arguments::Overwrite(System.UInt32,System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Arguments::ReassignReference(System.UInt32&,System.UInt32&) A A A A A A A A +Examples:System.UInt32 Examples.Arithmetic::AtMostFive(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Bits::Left32(System.UInt32,System.Int32) A A A A A A A A Examples:System.UInt32 Examples.Bits::PrimitiveLeadingU32(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Bits::PrimitiveLeftU32(System.UInt32,System.Int32) A A A A A A A A @@ -759,10 +779,13 @@ Examples:System.UInt32 Examples.Loops::Bump(System.UInt32,System.UInt32) A A A A Examples:System.UInt32 Examples.Loops::CopyUntilZero(System.ReadOnlySpan`1,System.UInt32&) A A A A A A A A Examples:System.UInt32 Examples.Loops::CountCalls(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Loops::CountUntil(System.UInt32,System.UInt32) A A A A A A A A +Examples:System.UInt32 Examples.Loops::CountUpToFive(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Loops::Countdown(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Loops::Decrement(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Loops::DownThenUp(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Loops::FeatureCountdown(System.UInt32) A A A A A A A A +Examples:System.UInt32 Examples.Loops::InnerCount(Examples.Loops/Outer,System.UInt32) A A A A A A A A +Examples:System.UInt32 Examples.Loops::ReachFive(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Loops::TallyTwice(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.Loops::WrongCountdown(System.UInt32) A A A A A A A A Examples:System.UInt32 Examples.NarrowTotals::ReadSum() A A A A A A A A @@ -867,6 +890,7 @@ Examples:System.UInt64 Examples.DivisionIntrinsics::ShiftRight(System.UInt64,Sys Examples:System.UInt64 Examples.DivisionIntrinsics::UnsafeCall(System.UInt64) A A A A A A A A Examples:System.UInt64 Examples.DivisionIntrinsics::UnsafeHigh(System.UInt64) A A A A A A A A Examples:System.UInt64 Examples.DivisionIntrinsics::UnsafeLow(System.UInt64) A A A A A A A A +Examples:System.UInt64 Examples.DivisionIntrinsics::UnsafeLowNext(System.UInt64) A A A A A A A A Examples:System.UInt64 Examples.DivisionIntrinsics::Xor(System.UInt64,System.UInt64) B B B B B B B B Examples:System.UInt64 Examples.Dog::Legs() A A A A A A A A Examples:System.UInt64 Examples.Dog::Loudness() A A A A A A A A @@ -923,6 +947,7 @@ Examples:System.UInt64 Examples.Loops::Sum(System.ReadOnlySpan`1) Examples:System.UInt64 Examples.Loops::SumIndexed(System.UInt64[]) A A A A A A A A Examples:System.UInt64 Examples.Loops::SumThroughLocal(System.ReadOnlySpan`1) A A A A A A A A Examples:System.UInt64 Examples.Loops::SumThroughSpan(System.UInt64[]) A A A A A A A A +Examples:System.UInt64 Examples.Loops::TwoStructs(System.UInt32) A A A A A A A A Examples:System.UInt64 Examples.MarshaledMemory::Read(System.ReadOnlySpan`1) A A A A A A A A Examples:System.UInt64 Examples.Measured`1::Width(System.UInt64) B B B B B B B B Examples:System.UInt64 Examples.MostSpecificBodies::Five(System.UInt64) A A A A A A A A @@ -952,6 +977,7 @@ Examples:System.UInt64 Examples.Objects::IdOf(Examples.Shape) A A A A A A A A Examples:System.UInt64 Examples.Objects::LinkedValue(Examples.Node,Examples.Node) A A A A A A A A Examples:System.UInt64 Examples.Objects::Replace(Examples.Account,System.UInt64) A A A A A A A A Examples:System.UInt64 Examples.Objects::SecondValue(Examples.Node) A A A A A A A A +Examples:System.UInt64 Examples.Objects::UnderFive(Examples.Account) A A A A A A A A Examples:System.UInt64 Examples.Objects::Withdraw(Examples.Account,System.UInt64) A A A A A A A A Examples:System.UInt64 Examples.Odometer::Next() A A A A A A A A Examples:System.UInt64 Examples.OutlineCountdown::Drain() A A A A A A A A @@ -1138,6 +1164,7 @@ Examples:System.Void Examples.Loops::FillIndexed(System.UInt32[],System.UInt32) Examples:System.Void Examples.Loops::FillThrough(System.Span`1,System.UInt32) A A A A A A A A Examples:System.Void Examples.Loops::FillThroughSpan(System.UInt32[],System.UInt32) A A A A A A A A Examples:System.Void Examples.Loops::FillWords(System.UInt64&,System.UInt64,System.Int32) A A A A A A A A +Examples:System.Void Examples.Loops::FillWordsDown(System.UInt64&,System.UInt64,System.Int32) A A A A A A A A Examples:System.Void Examples.Loops::Put(System.Span`1,System.UInt32) A A A A A A A A Examples:System.Void Examples.Loops::Stamp(System.UInt32&,System.UInt32) A A A A A A A A Examples:System.Void Examples.ManagedDispatch::Targets(method System.Int32 *(System.Int32)[]) B B B B B B B B @@ -1271,12 +1298,64 @@ Examples:U Examples.OwnerOuter`1/Inner`1::First(System.ReadOnlySpan`1) B B B Examples:U Examples.OwnerOuter`1/Inner`1::Identity(U) B B B B B B B B Examples:method System.Int32 *(System.Int32)[] Examples.ManagedDispatch::NullTable() A A A A A A A A Examples:method System.Single *(System.Single) Examples.ManagedDispatch::UnrelatedTarget() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Int32 ImportFixture.Depth10::Run() A A A A A A A A +ImportFixture.App+ImportFixture.Lib:System.Int32 ImportFixture.Depth12::Run() A A A A A A A A +ImportFixture.App+ImportFixture.Lib:System.Int32 ImportFixture.Depth14::Run() A A A A A A A A +ImportFixture.App+ImportFixture.Lib:System.Int32 ImportFixture.Depth8::Run() A A A A A A A A +ImportFixture.App+ImportFixture.Lib:System.Int32 ImportFixture.MetadataDerived::First(System.ValueTuple`2&) A A A A A A A A +ImportFixture.App+ImportFixture.Lib:System.Int32 ImportFixture.MetadataDerived::Run() A A A A A A A A +ImportFixture.App+ImportFixture.Lib:System.Int32 ImportFixture.MetadataRecursive::Run() A A A A A A A A ImportFixture.App+ImportFixture.Lib:System.UInt64 ImportFixture.Application::Entry(System.UInt64) A A A A A A A A ImportFixture.App+ImportFixture.Lib:System.UInt64 ImportFixture.Application::Generic(System.UInt64) A A A A A A A A ImportFixture.App+ImportFixture.Lib:System.UInt64 ImportFixture.Application::Unrelated(System.UInt64) A A A A A A A A +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth0::.ctor() A A A A A A A A +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth10::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth11::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth12::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth13::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth14::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth1::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth2::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth3::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth4::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth5::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth6::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth7::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth8::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.Depth9::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.MetadataBase`1::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.MetadataDerived::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.MetadataPair`2::.ctor() B B B B B B B B +ImportFixture.App+ImportFixture.Lib:System.Void ImportFixture.MetadataRecursive::.ctor() B B B B B B B B +ImportFixture.App:System.Int32 ImportFixture.Depth10::Run() A A A A A A A A +ImportFixture.App:System.Int32 ImportFixture.Depth12::Run() A A A A A A A A +ImportFixture.App:System.Int32 ImportFixture.Depth14::Run() A A A A A A A A +ImportFixture.App:System.Int32 ImportFixture.Depth8::Run() A A A A A A A A +ImportFixture.App:System.Int32 ImportFixture.MetadataDerived::First(System.ValueTuple`2&) A A A A A A A A +ImportFixture.App:System.Int32 ImportFixture.MetadataDerived::Run() A A A A A A A A +ImportFixture.App:System.Int32 ImportFixture.MetadataRecursive::Run() A A A A A A A A ImportFixture.App:System.UInt64 ImportFixture.Application::Entry(System.UInt64) B B B B B B B B ImportFixture.App:System.UInt64 ImportFixture.Application::Generic(System.UInt64) B B B B B B B B ImportFixture.App:System.UInt64 ImportFixture.Application::Unrelated(System.UInt64) A A A A A A A A +ImportFixture.App:System.Void ImportFixture.Depth0::.ctor() A A A A A A A A +ImportFixture.App:System.Void ImportFixture.Depth10::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth11::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth12::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth13::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth14::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth1::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth2::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth3::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth4::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth5::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth6::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth7::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth8::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.Depth9::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.MetadataBase`1::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.MetadataDerived::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.MetadataPair`2::.ctor() B B B B B B B B +ImportFixture.App:System.Void ImportFixture.MetadataRecursive::.ctor() B B B B B B B B ImportFixture.Lib:System.UInt64 ImportFixture.Library::AddOne(System.UInt64) A A A A A A A A ImportFixture.Lib:System.Void ImportFixture.Library::.ctor() A A A A A A A A ImportFixture.Lib:T ImportFixture.Library::Identity(T) B B B B B B B B diff --git a/Tests/CompilerToolchain.cs b/Tests/CompilerToolchain.cs index a1a762fba..af45c6cd2 100644 --- a/Tests/CompilerToolchain.cs +++ b/Tests/CompilerToolchain.cs @@ -3,7 +3,7 @@ partial class Program { static readonly string[] CorpusCompilerVersions = ["4.14.0", "5.0.0", "5.9.0"]; - const string CorpusReferenceVersion = "10.0.0"; + const string CorpusReferenceVersion = "11.0.0"; sealed record CompilerToolchain(string Packages, string References); @@ -16,7 +16,7 @@ static CompilerToolchain RestoreCompilerToolchain(string[]? versions = null) (versions ?? CorpusCompilerVersions).Select(version => "[" + version + "]")); File.WriteAllText(project, $$""" - net10.0 + net11.0 @@ -27,6 +27,7 @@ static CompilerToolchain RestoreCompilerToolchain(string[]? versions = null) Run(["dotnet", "restore", project]); using var assets = JsonDocument.Parse(File.ReadAllText(Path.Combine(directory, "obj/project.assets.json"))); var packages = assets.RootElement.GetProperty("packageFolders").EnumerateObject().First().Name; - return new(packages, Path.Combine(packages, "microsoft.netcore.app.ref", CorpusReferenceVersion, "ref/net10.0")); + return new(packages, + Path.Combine(packages, "microsoft.netcore.app.ref", CorpusReferenceVersion, "ref/net11.0")); } } diff --git a/Tests/ConstantCalls.lean b/Tests/ConstantCalls.lean index e2b443351..3e2ddae60 100644 --- a/Tests/ConstantCalls.lean +++ b/Tests/ConstantCalls.lean @@ -1,4 +1,5 @@ import CIL.Extraction.Translation +import Tests.BodyEncoding /-! # Constant-return calls in feature flow @@ -28,16 +29,19 @@ private def raw (code : List RawInstruction) (returnType : TypeExpr := integer) /-- A parameterless static Boolean slot, as a call to a flag getter names it. -/ private def slot : CallSite := { - declaringType := .atom "Fixture" "Fixture.IFlag", name := "get_IsActive", - target := .managed 7, typeArguments := [], parameterCount := 0, hasThis := false, - returnsValue := true, returnType := boolean } + declared := { convention := 0, returnType := boolean }, + method := { + owner := .atom "Fixture" "Fixture.IFlag", name := name!"get_IsActive", hasThis := false, + genericArity := 0, returnType := boolean, parameters := [] }, + name := "get_IsActive", target := .managed 7, typeArguments := [], parameterCount := 0, + returnsValue := true } private def state (stack : List (Option W32)) : Option FeatureState := some ⟨stack, []⟩ /-- A program method as the whole-program obligation sees it: only its closed body and feature certificate matter here. -/ private def method (closed : RawMethod) (featureStates : FeatureCertificate) : MethodEvidence := { - original := closed, arguments := [], closed, + original := closed, body := Tests.BodyEncoding.noBody, arguments := [], closed, parameterInput := ⟨[], [], []⟩, reachable := featureStates.map Option.isSome, featureStates, scalarStates := [], layoutUses := [] } @@ -53,12 +57,17 @@ private def andBody := raw [i 40 (.call slot), i 44 (.target 4), i 40 (.call slo i 22, i 42] boolean private def andStates : FeatureCertificate := [state [], state [some 1], state [], state [some 1], none, none] -private def andCode : List Op := - [.call 1 0, .brzero 4, .call 1 0, .ret none, .unsupported "pruned", .unsupported "pruned"] +private def andCode : List Instruction := + [.call slot.method, .brfalse 4, .call slot.method, .ret, .unadmitted 22, .unadmitted 42] + +/-- The links of a body whose calls, at `pcs`, enter method `index`. -/ +private def linkedAt (index : Nat) (pcs : List Nat) (pc : Nat) : Option Link := + if pcs.contains pc then some (.direct index) else none /-- Bodies that never fold: a static read, an `Int32` constant, a parameterized constant, two `ret`s with different words, a `ret`-free body, and a Boolean slot returning 2. -/ -private def staticLoad := i 126 (.field (.atom "Fixture" "Fixture.Flags") 0 boolean true) +private def staticLoad := + i 126 (.field (.atom "Fixture" "Fixture.Flags") 0 boolean name!"Enabled" true) private def staticBody := raw [staticLoad, i 42] boolean private def ignored : List MethodEvidence := [ method staticBody [state [], state [none]], @@ -80,15 +89,15 @@ private def caller := raw [i 40 (.call slot), i 44 (.target 4), i 29, i 42, i 23 private def callerOff : FeatureCertificate := [state [], state [some 0], none, none, state [], state [some 1]] -private def callerOffCode (index : Nat) : List Op := [.call index 0, .brzero 4, - .unsupported "pruned", .unsupported "pruned", .const32 1, .ret none] +private def callerOffCode : List Instruction := + [.call slot.method, .brfalse 4, .unadmitted 29, .unadmitted 42, .ldc_i4 1, .ret] /-- The certificate that prunes the block a false flag would run. -/ private def callerOn : FeatureCertificate := [state [], state [some 1], state [], state [some 7], none, none] -private def callerOnCode (index : Nat) : List Op := [.call index 0, .brzero 4, - .const32 7, .ret none, .unsupported "pruned", .unsupported "pruned"] +private def callerOnCode : List Instruction := + [.call slot.method, .brfalse 4, .ldc_i4 7, .ret, .unadmitted 23, .unadmitted 42] /-- The table reads the off, on and computed getters, and nothing else, including any index past the program, where a contract import would be. -/ @@ -100,35 +109,36 @@ theorem evidenceConstants_exact : /-- The conjunction folds through its callee's word, under its own obligation. -/ theorem computed_and_validated : - FeatureFlowValid #[] (evidenceConstants program) andBody .scalar andStates - (andStates.map Option.isSome) andCode := by decide + FeatureFlowValid #[] (evidenceConstants program) (linkedAt 1 [0, 2]) andBody .scalar + andStates (andStates.map Option.isSome) andCode := by decide #print axioms computed_and_validated /-- Calls to the off, on and computed getters each prune the block their word rules out. -/ theorem constant_calls_prune : - FeatureFlowValid #[] (evidenceConstants program) caller .scalar callerOff - (callerOff.map Option.isSome) (callerOffCode 0) ∧ - FeatureFlowValid #[] (evidenceConstants program) caller .scalar callerOn - (callerOn.map Option.isSome) (callerOnCode 1) ∧ - FeatureFlowValid #[] (evidenceConstants program) caller .scalar callerOn - (callerOn.map Option.isSome) (callerOnCode 2) := by decide + FeatureFlowValid #[] (evidenceConstants program) (linkedAt 0 [0]) caller .scalar + callerOff (callerOff.map Option.isSome) callerOffCode ∧ + FeatureFlowValid #[] (evidenceConstants program) (linkedAt 1 [0]) caller .scalar + callerOn (callerOn.map Option.isSome) callerOnCode ∧ + FeatureFlowValid #[] (evidenceConstants program) (linkedAt 2 [0]) caller .scalar + callerOn (callerOn.map Option.isSome) callerOnCode := by decide #print axioms constant_calls_prune -/-- Swapping the emitted callee to the other getter rejects either pruned certificate. -/ +/-- Linking the call to the other getter rejects either pruned certificate. -/ theorem constant_swap_rejected : - ¬ FeatureFlowValid #[] (evidenceConstants program) caller .scalar callerOff - (callerOff.map Option.isSome) (callerOffCode 1) ∧ - ¬ FeatureFlowValid #[] (evidenceConstants program) caller .scalar callerOn - (callerOn.map Option.isSome) (callerOnCode 0) := by decide + ¬ FeatureFlowValid #[] (evidenceConstants program) (linkedAt 1 [0]) caller .scalar + callerOff (callerOff.map Option.isSome) callerOffCode ∧ + ¬ FeatureFlowValid #[] (evidenceConstants program) (linkedAt 0 [0]) caller .scalar + callerOn (callerOn.map Option.isSome) callerOnCode := by decide #print axioms constant_swap_rejected /-- No non-folding body or import prunes the guarded block. -/ theorem constant_body_rejected : (List.range 7).all (fun index => !decide (FeatureFlowValid #[] (evidenceConstants program) - caller .scalar callerOff (callerOff.map Option.isSome) (callerOffCode (index + 3)))) := by + (linkedAt (index + 3) [0]) caller .scalar callerOff (callerOff.map Option.isSome) + callerOffCode)) := by decide #print axioms constant_body_rejected @@ -140,30 +150,34 @@ private def forged : List MethodEvidence := [method offBody onStates] callee's own obligation, also part of the program's, rejects the claim. -/ theorem forged_constant_rejected : evidenceConstants forged 0 = some 1 ∧ - FeatureFlowValid #[] (evidenceConstants forged) caller .scalar callerOn - (callerOn.map Option.isSome) (callerOnCode 0) ∧ - ¬ FeatureFlowValid #[] (evidenceConstants forged) offBody .scalar onStates - (onStates.map Option.isSome) [.const32 1, .ret none] ∧ - ¬ FeatureFlowValid #[] (evidenceConstants forged) offBody .scalar onStates - (onStates.map Option.isSome) [.const32 0, .ret none] := by decide + FeatureFlowValid #[] (evidenceConstants forged) (linkedAt 0 [0]) caller .scalar + callerOn (callerOn.map Option.isSome) callerOnCode ∧ + ¬ FeatureFlowValid #[] (evidenceConstants forged) (fun _ => none) offBody .scalar onStates + (onStates.map Option.isSome) [.ldc_i4 1, .ret] ∧ + ¬ FeatureFlowValid #[] (evidenceConstants forged) (fun _ => none) offBody .scalar onStates + (onStates.map Option.isSome) [.ldc_i4 0, .ret] := by decide #print axioms forged_constant_rejected -/-- Only a parameterless emitted call reads the table; a tail call or argument count does not. -/ +/-- Only a parameterless emitted call reads the table, at the callee its link names; a tail call, + an argument or a pc without a direct link does not. -/ theorem calleeConstant_exact : - calleeConstant (evidenceConstants program) (.call 0 0) = some 0 ∧ - calleeConstant (evidenceConstants program) (.call 2 0) = some 1 ∧ - calleeConstant (evidenceConstants program) (.call 0 1) = none ∧ - calleeConstant (evidenceConstants program) (.tailCall 0 0) = none := by decide + calleeConstant (evidenceConstants program) (some (.direct 0)) (.call slot.method) = some 0 ∧ + calleeConstant (evidenceConstants program) (some (.direct 2)) (.call slot.method) = some 1 ∧ + calleeConstant (evidenceConstants program) (some (.direct 0)) + (.call { slot.method with parameters := [integer] }) = none ∧ + calleeConstant (evidenceConstants program) (some (.direct 0)) + (.call slot.method { tail := true }) = none ∧ + calleeConstant (evidenceConstants program) none (.call slot.method) = none := by decide #print axioms calleeConstant_exact /-- With the guarded block live, the unknown answer accepts both successors. -/ theorem constant_unknown_keeps_both : - FeatureFlowValid #[] (evidenceConstants program) caller .scalar + FeatureFlowValid #[] (evidenceConstants program) (linkedAt 3 [0]) caller .scalar [state [], state [none], state [], state [some 7], state [], state [some 1]] (List.replicate 6 true) - [.call 3 0, .brzero 4, .const32 7, .ret none, .const32 1, .ret none] := by decide + [.call slot.method, .brfalse 4, .ldc_i4 7, .ret, .ldc_i4 1, .ret] := by decide #print axioms constant_unknown_keeps_both diff --git a/Tests/ConstrainedCalls.cs b/Tests/ConstrainedCalls.cs index 3e53655f3..3011f5076 100644 --- a/Tests/ConstrainedCalls.cs +++ b/Tests/ConstrainedCalls.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs @@ -26,12 +31,12 @@ static string Narrowed(string source, out string control) { var obligations = Regex.Matches(source, @"(?m)^theorem method\d+_constrained_validated : [^\r\n]+\r?$"); - var methodCount = Regex.Matches(source, @"(?m)^def method\d+ : Method :=").Count; + var methodCount = DefinedMethods(source).Length; if (methodCount == 0 || obligations.Count != methodCount) throw new Exception("Unexpected constrained obligation count"); - var declarations = Regex.Replace(source, - @"(?m)^(?:@\[[^\r\n]*\] )?theorem [^\r\n]*(?:\r?\n[ \t]+[^\r\n]*)*\r?\n", ""); - declarations = Regex.Replace(declarations, @"(?m)^#print axioms [^\r\n]*\r?\n", ""); + var declarations = GeneratedTheorem.Replace(source, ""); + declarations = AxiomsCommand.Replace(declarations, ""); + declarations = ResolutionFactsCommand.Replace(declarations, ""); var names = obligations.Select(match => Regex.Match(match.Value, @"theorem (\w+)").Groups[1].Value).ToArray(); var narrowed = declarations.Replace("end Extracted", string.Join("\n", obligations.Zip(names).Select(pair => pair.First.Value + "\n#print axioms " + pair.Second)) + "\nend Extracted", StringComparison.Ordinal); @@ -67,6 +72,12 @@ static string Narrowed(string source, out string control) Run(["dotnet", ExtractorPath, "extract", name == "DirectGeneric" ? directAssembly : assembly, $"ConstrainedCalls::{name}(Examples.{type}&)", output]); var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); + // A specialized body's field operands carry no token, so its closed raw method names the field by the row the + // definition's token names. + if (name == "Generic" && !source.Split('\n').Any(line => + line.Contains("Closed : Extraction.RawMethod", StringComparison.Ordinal) && + line.Contains("name!\"Value\"", StringComparison.Ordinal))) + throw new InvalidOperationException($"The closed body of {name} does not name its field operand Value"); var proof = Path.Combine(root, name + ".lean"); var extent = type.Contains('`') ? 8 : 4; File.WriteAllText(proof, source + $$""" @@ -79,19 +90,20 @@ static string Narrowed(string source, out string control) nextIdentity := 1 } theorem constrained_result : (CIL.Safety.invoke Extracted.program 32 0 [.reference (.address ⟨0, 0⟩)] - inputMemory).map Prod.snd = .ok [.scalar (.i32 {{expected}})] := rfl + inputMemory).map Prod.snd = .ok [.scalar (.i32 {{expected}})] := by rfl #print axioms constrained_result """); - var call = name == "DirectGeneric" ? ".call 1 2" : ".call 2 1"; - if (!source.Contains(call, StringComparison.Ordinal)) throw new Exception("Missing constrained call mutation target"); + var calls = DirectLinks(source, name == "DirectGeneric" ? 1 : 2); + if (calls.Length != 1) throw new Exception("Missing constrained call mutation target"); var forged = Path.Combine(root, name + "-wrong-target.lean"); var narrowed = Narrowed(source, out var control); File.AppendAllText(proof, "\n" + control); evidence.Add(proof, "PASS compiled constrained call, certificate and obligation eligibility: " + name); if (!type.Contains('`') && !name.StartsWith("Closed", StringComparison.Ordinal)) { - File.WriteAllText(forged, narrowed.Replace(call, ".call 0 1", StringComparison.Ordinal)); + // The link names another method than the one the constraint resolves. + File.WriteAllText(forged, ForgeLink(narrowed, calls[0].Method, calls[0].Pc, ".direct 0")); rejected.Add(forged, "PASS forged constrained target: " + name, "ConstrainedCallsValid"); } if (type.Contains('`')) @@ -133,7 +145,7 @@ static string Narrowed(string source, out string control) theorem distinct_owner_results : (CIL.Safety.invoke Extracted.program 64 0 [.reference (.address ⟨0, 0⟩), .reference (.address ⟨0, 8⟩)] - twoOwnerMemory).map Prod.snd = .ok [.scalar (.i32 84)] := rfl + twoOwnerMemory).map Prod.snd = .ok [.scalar (.i32 84)] := by rfl #print axioms distinct_owner_results """); evidence.Add(twoOwnerProof, "PASS distinct constrained owner specializations"); @@ -205,6 +217,20 @@ static string Narrowed(string source, out string control) evidence.Add(proof, "PASS static interface dispatch: " + name); } +static void CheckPruning(string proof, bool expected) +{ + var pruned = expected ? "true" : "false"; + File.AppendAllText(proof, $$""" + + /-- Whether the extracted program contains a hole under this profile. -/ + theorem flag_pruning : + (Extracted.program.any fun method => + method.code.any fun op => op matches .unadmitted _) = {{pruned}} := by decide +kernel + #print axioms flag_pruning + + """); +} + // A flag getter whose body is a constant return prunes the block it guards, so the process clock read // there is never extracted; results match the runtime, which runs neither read for these flags. var constantFlags = staticInterfaces.Assembly.GetType("Examples.ConstantFlags")!; @@ -213,8 +239,6 @@ static string Narrowed(string source, out string control) var output = Path.Combine(root, "Flags-" + name); Run(["dotnet", ExtractorPath, "extract", assembly, $"ConstantFlags::{name}(ulong)", output]); var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); - if (!source.Contains(".unsupported \"Unreachable under selected profile\"", StringComparison.Ordinal)) - throw new Exception("Constant flag block was not pruned: " + name); var claims = new[] { 0UL, 5UL, ulong.MaxValue, 1UL << 63 } .Select(input => (input, result: (ulong)constantFlags.GetMethod(name)!.Invoke(null, [input])!)) .Select(pair => $"(CIL.Safety.invoke Extracted.program 32 0 [.scalar (.i64 {pair.input})] " + @@ -222,6 +246,7 @@ static string Narrowed(string source, out string control) var proof = Path.Combine(root, "Flags-" + name + ".lean"); File.WriteAllText(proof, source + "\ntheorem flag_result :\n " + string.Join(" ∧\n ", claims) + " :=\n ⟨" + string.Join(", ", claims.Select(_ => "rfl")) + "⟩\n#print axioms flag_result\n"); + CheckPruning(proof, true); evidence.Add(proof, "PASS constant flag pruning: " + name); } @@ -246,9 +271,6 @@ static string Narrowed(string source, out string control) Run(["dotnet", ExtractorPath, "extract", assembly, $"{type}::{name}(ulong)", output, .. assume ? new[] { "--assume-initialized", "Examples.StaticScalars" } : []]); var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); - if (pruned is bool expected && expected != - source.Contains(".unsupported \"Unreachable under selected profile\"", StringComparison.Ordinal)) - throw new Exception("Unexpected flag pruning: " + name); if (name == "WidthNarrow") widthNarrow = source; var entry = staticInterfaces.Assembly.GetType("Examples." + type)!.GetMethod(name)!; var claims = new[] { 0UL, 5UL, ulong.MaxValue, 1UL << 63 } @@ -258,12 +280,13 @@ static string Narrowed(string source, out string control) var proof = Path.Combine(root, type + "-" + name + ".lean"); File.WriteAllText(proof, source + "\ntheorem flag_result :\n " + string.Join(" ∧\n ", claims) + " :=\n ⟨" + string.Join(", ", claims.Select(_ => "rfl")) + "⟩\n#print axioms flag_result\n"); + if (pruned is bool expected) CheckPruning(proof, expected); evidence.Add(proof, $"PASS {type}: {name}"); } // The receiver's catalog lists the interface instantiated at its own type argument; listing another // instantiation leaves the slot unimplemented. -const string narrowWidth = "interfaces := [.apply (.constructor \"Examples, Version=1.0.0.0, Culture=neutral, " + +const string narrowWidth = "interfaces := [.apply (.objectConstructor \"Examples, Version=1.0.0.0, Culture=neutral, " + "PublicKeyToken=null\" \"Examples.IWidth\\u00601\") (.atom Extraction.runtimeScope \"System.UInt32\")"; if (widthNarrow?.Contains(narrowWidth, StringComparison.Ordinal) != true) throw new Exception("Missing generic receiver interface mutation target"); @@ -292,20 +315,21 @@ static string Narrowed(string source, out string control) throw new Exception("Missing constant flag mutation targets"); var (callerIndex, offIndex, onIndex) = (offCaller.Groups[1].Value, offGetter.Groups[1].Value, onGetter.Groups[1].Value); -var offCall = $".call {offIndex} 0"; +var offCalls = DirectLinks(flags, int.Parse(offIndex, System.Globalization.CultureInfo.InvariantCulture)) + .Where(call => call.Method.ToString(System.Globalization.CultureInfo.InvariantCulture) == callerIndex).ToArray(); var callerStates = Regex.Match(flags, $@"(?m)^def method{callerIndex}FeatureStates : [^\r\n]+"); var offBody = Regex.Match(flags, $@"(?m)^def method{offIndex}Closed : [^\r\n]+"); var offStates = Regex.Match(flags, $@"(?m)^def method{offIndex}FeatureStates : [^\r\n]+"); const string offWord = "stack := [some (BitVec.ofInt 32 (0))]"; const string offLoad = "{ offset := 0, opcode := 22, operand := .none }"; -if (!flags.Contains(offCall, StringComparison.Ordinal) || +if (offCalls.Length != 1 || !callerStates.Value.Contains(offWord, StringComparison.Ordinal) || !offBody.Value.Contains(offLoad, StringComparison.Ordinal) || !offStates.Value.Contains(offWord, StringComparison.Ordinal)) throw new Exception("Missing constant flag mutation sites"); foreach (var (mutation, forgedSource, obligation) in new[] { - ("swapped-callee", flags.Replace(offCall, $".call {onIndex} 0", StringComparison.Ordinal), callerIndex), + ("swapped-callee", ForgeLink(flags, offCalls[0].Method, offCalls[0].Pc, $".direct {onIndex}"), callerIndex), ("forged-word", flags.Replace(callerStates.Value, new Regex(Regex.Escape(offWord)).Replace( callerStates.Value, "stack := [some (BitVec.ofInt 32 (1))]", 1), StringComparison.Ordinal), callerIndex), ("callee-word", flags.Replace(offBody.Value, offBody.Value.Replace(offLoad, @@ -318,8 +342,7 @@ static string Narrowed(string source, out string control) var forged = Path.Combine(root, "Flags-" + mutation + ".lean"); File.WriteAllText(forged, forgedSource); rejected.Add(forged, "PASS forged constant flag: " + mutation, - $"Extraction.FeatureFlowValid rawLayouts.toArray (Extraction.evidenceConstants methodEvidence) " + - $"method{obligation}Closed"); + "Extraction.FeatureFlowValid", FeatureObligation(obligation)); } // Extraction keeps the guarded block, and so rejects its clock read, whenever the flag is live or the @@ -413,23 +436,27 @@ static string Narrowed(string source, out string control) // The emitted call must name the resolved instance: another method, a hidden override or a changed // method type argument each leave the caller's constrained obligation unprovable. var (spendDefault, spendOverride) = (defaultNarrowed["SpendDefault"], defaultNarrowed["SpendOverride"]); -var defaultSite = Regex.Match(spendDefault, @"target := \.managed (\d+), typeArguments := \[(\.aggregate ""[^""]+" + - @"TriplePrice"" \d+), (\.aggregate ""[^""]+Allowance"" \d+)\], ownerArgumentCount := 1"); -var overrideSite = Regex.Match(spendOverride, @"target := \.managed (\d+), typeArguments := \[\.aggregate ""[^""]+" + - @"TriplePrice"" \d+, \.aggregate ""[^""]+DoublingAllowance"" \d+\], ownerArgumentCount := 1"); -var hiddenOverride = overrideSite.Success ? Regex.Match(spendOverride, - @"overrides := \[\(" + overrideSite.Groups[1].Value + @", [^\r\n]+?DoublingAllowance"" \d+\)\)\]") : Match.Empty; -var defaultBody = Regex.Match(spendDefault, @"def method(\d+)Original : Extraction.RawMethod := " + - @"\{ token := \d+, owner := ""[^""]+::Examples.IAllowance\\u00601"""); -var defaultCalls = Regex.Matches(spendDefault, @"(?m)^ \.call " + defaultBody.Groups[1].Value + @" 1,\r?$"); -if (!defaultSite.Success || !hiddenOverride.Success || !defaultBody.Success || defaultCalls.Count != 1) +Match SpendSite(string source, string allowance) => ConstrainedLayoutPattern.SpendSite().Matches(source) + .FirstOrDefault(match => match.Groups["owner"].Value == allowance) ?? Match.Empty; +var defaultSite = SpendSite(spendDefault, "Examples.Allowance"); +var overrideSite = SpendSite(spendOverride, "Examples.DoublingAllowance"); +var hiddenOverride = ConstrainedLayoutPattern.Override().Matches(spendOverride) + .FirstOrDefault(match => overrideSite.Success && match.Groups["token"].Value == overrideSite.Groups["token"].Value) + ?? Match.Empty; +var defaultBody = ConstrainedLayoutPattern.DefaultBody().Match(spendDefault); +var defaultCalls = defaultBody.Success + ? DirectLinks(spendDefault, + int.Parse(defaultBody.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture)) + : []; +if (!defaultSite.Success || !hiddenOverride.Success || !defaultBody.Success || defaultCalls.Length != 1) throw new Exception("Missing static virtual default mutation target"); foreach (var (mutation, forgedSource) in new[] { - ("wrong-target", spendDefault.Replace(defaultCalls[0].Value, " .call 0 1,", StringComparison.Ordinal)), + ("wrong-target", ForgeLink(spendDefault, defaultCalls[0].Method, defaultCalls[0].Pc, ".direct 0")), ("hidden-override", spendOverride.Replace(hiddenOverride.Value, "overrides := []", StringComparison.Ordinal)), ("method-argument", spendDefault.Replace(defaultSite.Value, defaultSite.Value.Replace( - defaultSite.Groups[2].Value + ", ", defaultSite.Groups[3].Value + ", ", StringComparison.Ordinal), + $"{defaultSite.Groups["price"].Value}, ", $"{defaultSite.Groups["argument"].Value}, ", + StringComparison.Ordinal), StringComparison.Ordinal)) }) { @@ -451,9 +478,6 @@ static string Narrowed(string source, out string control) var output = Path.Combine(root, "MostSpecific-" + name); Run(["dotnet", ExtractorPath, "extract", assembly, $"{owner}::{name}(ulong)", output]); var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); - if (name == "TraceQuiet" && - !source.Contains(".unsupported \"Unreachable under selected profile\"", StringComparison.Ordinal)) - throw new Exception("Derived constant flag block was not pruned: " + name); mostSpecificSources[name] = source; var method = examples.GetType("Examples." + owner)!.GetMethod(name)!; var claims = new[] { 0UL, 5UL, ulong.MaxValue, 1UL << 63 } @@ -463,6 +487,7 @@ static string Narrowed(string source, out string control) var proof = Path.Combine(root, "MostSpecific-" + name + ".lean"); File.WriteAllText(proof, source + "\ntheorem most_specific_result :\n " + string.Join(" ∧\n ", claims) + " :=\n ⟨" + string.Join(", ", claims.Select(_ => "rfl")) + "⟩\n#print axioms most_specific_result\n"); + if (name == "TraceQuiet") CheckPruning(proof, true); evidence.Add(proof, "PASS most specific interface body: " + name); } @@ -622,7 +647,7 @@ MethodDefinition Body(string type) => module.GetType("Examples." + type).Methods foreach (var (mutation, marker) in new[] { ("prefix", "adjacent concrete constrained receiver"), - ("branch", "Branch bypasses constrained receiver prefix"), + ("branch", "Branch enters the instruction after a prefix"), ("constraint", "Unsupported generic method constraints") }) { @@ -666,7 +691,8 @@ MethodDefinition Body(string type) => module.GetType("Examples." + type).Methods const string modifierRejection = "except System.Runtime's modreq(InAttribute) directly on a by-reference parameter"; MethodDefinition InAdd(ModuleDefinition module) => module.GetType("Examples.InWord").Methods .Single(method => method.Name.EndsWith(".Add", StringComparison.Ordinal)); -foreach (var mutation in new[] { "modopt", "other-modreq", "by-value", "nested", "versioned-scope", "foreign-scope" }) +foreach (var mutation in new[] + { "modopt", "other-modreq", "by-value", "nested", "versioned-scope", "foreign-scope", "flattened-name" }) { var dll = Path.Combine(root, "in-" + mutation + ".dll"); string selector; @@ -676,10 +702,15 @@ MethodDefinition InAdd(ModuleDefinition module) => module.GetType("Examples.InWo var modified = (RequiredModifierType)method.Parameters[0].ParameterType; var reference = (ByReferenceType)modified.ElementType; var runtime = (AssemblyNameReference)modified.ModifierType.Scope; - TypeReference Forged(AssemblyNameReference scope) => - new(modified.ModifierType.Namespace, modified.ModifierType.Name, module, scope); + TypeReference Forged(AssemblyNameReference scope) + { + module.AssemblyReferences.Add(scope); + return new(modified.ModifierType.Namespace, modified.ModifierType.Name, module, scope); + } method.Parameters[0].ParameterType = mutation switch { + "flattened-name" => new RequiredModifierType(new TypeReference("", + "System.Runtime.InteropServices.InAttribute", module, runtime), reference), "modopt" => new OptionalModifierType(modified.ModifierType, reference), "other-modreq" => new RequiredModifierType( new TypeReference("System.Runtime.CompilerServices", "IsVolatile", module, runtime), reference), @@ -769,3 +800,22 @@ TypeReference Forged(AssemblyNameReference scope) => if (returnResult.Code == 0 || !returnResult.Output.Contains(returnRejection, StringComparison.Ordinal)) throw new Exception("Incorrect in-modifier return rejection\n" + returnResult.Output); Console.WriteLine("PASS in-modifier rejection: ref readonly return"); + +static partial class ConstrainedLayoutPattern +{ + const string Price = """(?\.aggregate \("[^"]+" \+\+ "Examples\.TriplePrice"\) \d+)"""; + const string Argument = """(?\.aggregate \("[^"]+" \+\+ "(?[^"]+)"\) \d+)"""; + + const string Target = """target := \.managed (?\d+), typeArguments := """; + + [GeneratedRegex($"""{Target}\[{Price}, {Argument}\], ownerArgumentCount := 1""")] + internal static partial Regex SpendSite(); + + [GeneratedRegex("""overrides := \[\((?\d+), [^\]\r\n]+?DoublingAllowance"\) \d+\)\)\]""")] + internal static partial Regex Override(); + + const string Definition = """def method(\d+)Original : Extraction\.RawMethod := """; + + [GeneratedRegex($$"""{{Definition}}\{ token := \d+, owner := "[^"]+::Examples\.IAllowance\\u00601["]""")] + internal static partial Regex DefaultBody(); +} diff --git a/Tests/ConstrainedEvidence.lean b/Tests/ConstrainedEvidence.lean index 3c03f8998..e5e77b3d4 100644 --- a/Tests/ConstrainedEvidence.lean +++ b/Tests/ConstrainedEvidence.lean @@ -1,6 +1,12 @@ import CIL.Extraction.Constrained +import CIL.Extraction.ValueDecoding +import Tests.Support -/-! # Independently tampered constrained-call metadata and translations -/ +/-! # Constrained-call evidence + +These fixtures check receiver and interface dispatch, including the owner kind of a closed +generic struct, and the emitted calls and type tests against their metadata. +-/ namespace Tests.ConstrainedEvidence open CIL CIL.Extraction @@ -36,9 +42,12 @@ private def declared : DispatchType := { interfaces := [], methods := [slot], assembly := fixturePin } private def types := [concrete, declared] private def site : CallSite := { - declaringType := interface, name := "Read", target := .managed 5, typeArguments := [], - parameterCount := 0, hasThis := true, returnsValue := true, - returnType := integer, parameterTypes := [] } + declared := { convention := 0x20, returnType := integer }, + method := { + owner := interface, name := name!"Read", hasThis := true, genericArity := 0, + returnType := integer, parameters := [] }, + name := "Read", target := .managed 5, typeArguments := [], parameterCount := 0, + returnsValue := true } private def raw : RawMethod := { token := 1, owner := "Fixture::Caller", hasThis := false, explicitThis := false, callingConvention := 0, attributes := 0, implementation := 0, @@ -49,22 +58,53 @@ private def target : SpecializedMethod := ⟨[], { raw with token := 21, owner := "Fixture::Counter", hasThis := true, parameters := [], code := [⟨0, 23, .none⟩, ⟨1, 42, .none⟩] }⟩ private def methods := [⟨[], raw⟩, target] -private def emitted : Method := { - locals := [], returnsValue := true, code := [.arg 0, .nop, .call 1 1, .ret] } +/-- The receiver load, the dropped prefix and `call` with its prefix, linked to the target. -/ +private def constrainedEmitted (receiver : TypeExpr) (site : CallSite) : Method := { + locals := [], returnsValue := true, + code := [.ldarg 0, .nop, .call site.method { constrained := some receiver }, .ret], + links := [(2, .direct 1)] } +private def emitted : Method := constrainedEmitted receiver site +/-- `emitted` with `op` in place of its call. -/ +private def callAs (op : Instruction) : Method := { emitted with code := emitted.code.set 2 op } theorem concrete_call_valid : ConstrainedCallsValid types methods raw [true, true, true, true] emitted := by decide +kernel #print axioms concrete_call_valid +/-- A decoded call must link to the resolved body and name its dispatch receiver. -/ +theorem forged_decoded_call_rejected : + let valid := fun (source : RawMethod) (link : Nat) => + ConstrainedCallsValid types methods source [true, true, true, true] + { emitted with + code := Instruction.ofRawCode none source.code, links := [(2, .direct link)] } + let other := raw.code.set 1 ⟨1, 0xFE16, .type (.aggregate "Fixture::Other" 1)⟩ + valid raw 1 ∧ ¬ valid raw 0 ∧ ¬ valid { raw with code := other } 1 := by + decide +kernel +#print axioms forged_decoded_call_rejected + +/-- A link to any body but the resolved implementation, or no direct link, is rejected. -/ theorem changed_target_rejected : ¬ ConstrainedCallsValid types methods raw [true, true, true, true] - { emitted with code := emitted.code.set 2 (.call 0 1) } := by decide +kernel + { emitted with links := [(2, .direct 0)] } ∧ + ¬ ConstrainedCallsValid types methods raw [true, true, true, true] + { emitted with links := [] } ∧ + ¬ ConstrainedCallsValid types methods raw [true, true, true, true] + { emitted with links := [(2, .virtual [(receiver, 1)])] } := by decide +kernel #print axioms changed_target_rejected -theorem changed_arity_rejected : +/-- The emitted call names the site's method, carries the site's receiver as its prefix and is + no tail call. -/ +theorem changed_call_rejected : ¬ ConstrainedCallsValid types methods raw [true, true, true, true] - { emitted with code := emitted.code.set 2 (.call 1 0) } := by decide +kernel -#print axioms changed_arity_rejected + (callAs (.call { site.method with hasThis := false } { constrained := some receiver })) ∧ + ¬ ConstrainedCallsValid types methods raw [true, true, true, true] + (callAs (.call site.method)) ∧ + ¬ ConstrainedCallsValid types methods raw [true, true, true, true] + (callAs (.call site.method { constrained := some interface })) ∧ + ¬ ConstrainedCallsValid types methods raw [true, true, true, true] + (callAs (.call site.method { tail := true, constrained := some receiver })) := by + decide +kernel +#print axioms changed_call_rejected theorem prefix_bypass_rejected : ¬ ConstrainedCallsValid types methods @@ -103,15 +143,22 @@ private def staticImplementation : DispatchMethod := { staticSlot with token := 31, isAbstract := false, isVirtual := false, hasBody := true } private def staticTypes : List DispatchType := [{ concrete with methods := [staticImplementation] }, { declared with methods := [staticSlot] }] -private def staticSite : CallSite := { site with target := .managed 6, hasThis := false } +private def staticSite : CallSite := { site with target := .managed 6, method.hasThis := false } private def staticRaw : RawMethod := { raw with parameters := [], code := [⟨0, 65046, .type receiver⟩, ⟨6, 40, .call staticSite⟩, ⟨11, 42, .none⟩] } private def staticTarget : SpecializedMethod := ⟨[], { raw with token := 31, owner := "Fixture::Counter", parameters := [], code := [⟨0, 22, .none⟩, ⟨1, 42, .none⟩] }⟩ private def staticMethods := [⟨[], staticRaw⟩, staticTarget] -private def staticEmitted : Method := { - locals := [], returnsValue := true, code := [.nop, .call 1 0, .ret] } +/-- The dropped prefix and the prefixed static `call` of `site` on `receiver`. -/ +private def staticCall (receiver : TypeExpr) (site : CallSite) : Method := { + locals := [], returnsValue := true, + code := [.nop, .call site.method { constrained := some receiver }, .ret], + links := [(1, .direct 1)] } +private def staticEmitted : Method := staticCall receiver staticSite +/-- The static slot's call with a receiver argument added. -/ +private def receiverCall : Instruction := + .call { staticSite.method with hasThis := true } { constrained := some receiver } /-- A static interface slot (`constrained. T; call`) resolves to the receiver's static implementation, called without a receiver argument. -/ @@ -122,7 +169,7 @@ theorem static_call_valid : theorem static_call_with_receiver_rejected : ¬ ConstrainedCallsValid staticTypes staticMethods staticRaw [true, true, true] - { staticEmitted with code := staticEmitted.code.set 1 (.call 1 1) } := by decide +kernel + { staticEmitted with code := staticEmitted.code.set 1 receiverCall } := by decide +kernel #print axioms static_call_with_receiver_rejected /-- `callvirt` never dispatches a static slot, and `call` never dispatches an instance slot. -/ @@ -140,7 +187,7 @@ theorem static_slot_mismatches_rejected : resolveConstrained [concrete, { declared with methods := [staticSlot] }] receiver staticSite = none ∧ resolveConstrained [{ concrete with methods := [{ staticImplementation with token := 6 }] }] - receiver { staticSite with declaringType := receiver } = none := by decide +kernel + receiver { staticSite with method.owner := receiver } = none := by decide +kernel #print axioms static_slot_mismatches_rejected /-- An instance default interface method slot is rejected even when the struct implements it: @@ -154,7 +201,7 @@ theorem default_interface_slot_rejected : private def boolean : TypeExpr := .atom runtimeScope "System.Boolean" private def allowance : TypeExpr := .aggregate "Fixture::Allowance" 0 private def price : TypeExpr := .aggregate "Fixture::Price" 1 -private def allowanceOwner : TypeExpr := .constructor "Fixture" "IAllowance`1" +private def allowanceOwner : TypeExpr := .objectConstructor "Fixture" "IAllowance`1" private def allowanceInterface : TypeExpr := .apply allowanceOwner allowance /-- `static virtual bool Spend(ref TSelf self)` with a body, on `IAllowance`. -/ private def defaultSlot : DispatchMethod := { @@ -179,10 +226,12 @@ private def overrideTypes := [{ defaultConcrete with methods := [genericOverride] }, defaultDeclared] /-- `constrained. Allowance; call bool IAllowance::Spend(TSelf&)`. -/ private def defaultSite : CallSite := { - declaringType := allowanceInterface, name := "Spend", target := .managed 40, - typeArguments := [price, allowance], ownerArgumentCount := 1, parameterCount := 1, - hasThis := false, returnsValue := true, returnType := boolean, - parameterTypes := [.byref allowance] } + declared := { convention := 0x10, returnType := boolean, parameters := [.byref (.parameter 0)] }, + method := { + owner := allowanceInterface, name := name!"Spend", hasThis := false, genericArity := 1, + returnType := boolean, parameters := [.byref allowance], methodArguments := [price] }, + name := "Spend", target := .managed 40, typeArguments := [price, allowance], + ownerArgumentCount := 1, parameterCount := 1, returnsValue := true } private def defaultRaw : RawMethod := { raw with parameters := [.byref allowance], code := [⟨0, 2, .none⟩, ⟨1, 65046, .type allowance⟩, ⟨7, 40, .call defaultSite⟩, @@ -195,8 +244,7 @@ private def defaultTarget : SpecializedMethod := ⟨[price, allowance], { raw wi /-- The recorded override instantiated at `TPrice := Price`. -/ private def overrideTarget : SpecializedMethod := ⟨[price], { defaultTarget.closed with token := 41, owner := "Fixture::Allowance", ownerArgumentCount := 0 }⟩ -private def defaultEmitted : Method := { - locals := [], returnsValue := true, code := [.arg 0, .nop, .call 1 1, .ret] } +private def defaultEmitted : Method := constrainedEmitted allowance defaultSite /-- Without an implementation on the receiver, a generic static virtual slot runs its own body, instantiated at the site's method and interface arguments. -/ @@ -257,14 +305,15 @@ theorem static_default_owner_rejected : resolveConstrained [defaultConcrete, { defaultDeclared with methods := [{ defaultSlot with ownerArguments := [price] }] }] allowance defaultSite = none ∧ resolveConstrained [defaultConcrete, { defaultDeclared with methods := - [{ defaultSlot with owner := .constructor "Other" "IAllowance`1" }] }] + [{ defaultSlot with owner := .objectConstructor "Other" "IAllowance`1" }] }] allowance defaultSite = none := by decide +kernel #print axioms static_default_owner_rejected -private def derivedInterface : TypeExpr := .apply (.constructor "Fixture" "IRefund`1") allowance +private def derivedInterface : TypeExpr := + .apply (.objectConstructor "Fixture" "IRefund`1") allowance /-- `IRefund`'s `MethodImpl` of `IAllowance.Spend`, closed at `Allowance`. -/ private def derivedBody : DispatchMethod := { genericOverride with - token := 50, owner := .constructor "Fixture" "IRefund`1", ownerArguments := [allowance] } + token := 50, owner := .objectConstructor "Fixture" "IRefund`1", ownerArguments := [allowance] } /-- `IRefund : IAllowance` re-implementing `IAllowance.Spend`. -/ private def derivedDeclared : DispatchType := { identity := derivedInterface, valueType := false, interfaceType := true, @@ -329,13 +378,14 @@ private def scaledEntry (interfaces : List TypeExpr) : DispatchType := { identity := scaled, valueType := true, interfaceType := false, interfaces, methods := [], assembly := fixturePin } private def scaleSite : CallSite := { - staticSite with declaringType := scaleInterface, name := "Scale", target := .managed 60 } + staticSite with method.owner := scaleInterface, name := "Scale", target := .managed 60 } private def scaleRaw : RawMethod := { staticRaw with code := [⟨0, 65046, .type scaled⟩, ⟨6, 40, .call scaleSite⟩, ⟨11, 42, .none⟩] } /-- The program body of the interface-level method `token` declared by `owner`. -/ private def scaleProgram (token : Nat) (owner : String) : SpecializedMethod := ⟨[], { staticTarget.closed with token, owner }⟩ private def scaleChain := [scaledEntry [moreInterface, scaleInterface], scaleEntry, moreEntry] +private def scaleEmitted : Method := staticCall scaled scaleSite /-- A derived interface's override of a static virtual slot runs instead of the default, and only its own program body may be emitted for the call. -/ @@ -344,13 +394,13 @@ theorem derived_override_valid : some (.interfaceBody moreInterface (scaleBody 61)) ∧ ConstrainedCallsValid scaleChain [⟨[], scaleRaw⟩, scaleProgram 61 "Fixture::IScaleMore"] scaleRaw [true, true, true] - staticEmitted ∧ + scaleEmitted ∧ ¬ ConstrainedCallsValid scaleChain [⟨[], scaleRaw⟩, scaleProgram 60 "Fixture::IScale"] scaleRaw [true, true, true] - staticEmitted ∧ + scaleEmitted ∧ ¬ ConstrainedCallsValid scaleChain [⟨[], scaleRaw⟩, scaleProgram 61 "Fixture::IScale"] scaleRaw [true, true, true] - staticEmitted := by decide +kernel + scaleEmitted := by decide +kernel #print axioms derived_override_valid /-- A derived interface supplies the body of a base `static abstract` slot. -/ @@ -371,7 +421,7 @@ theorem most_derived_body_valid : [scaledEntry [mostInterface, moreInterface, scaleInterface], scaleEntry, moreEntry, mostEntry] [⟨[], scaleRaw⟩, scaleProgram 61 "Fixture::IScaleMore"] scaleRaw [true, true, true] - staticEmitted := by decide +kernel + scaleEmitted := by decide +kernel #print axioms most_derived_body_valid /-- Two unrelated interfaces overriding the slot leave no most specific body, so the call is @@ -384,7 +434,7 @@ theorem diamond_rejected : [scaledEntry [moreInterface, sideInterface, scaleInterface], scaleEntry, moreEntry, sideEntry] [⟨[], scaleRaw⟩, scaleProgram 60 "Fixture::IScale"] scaleRaw [true, true, true] - staticEmitted ∧ + scaleEmitted ∧ resolveConstrained [scaledEntry [moreInterface, scaleInterface], scaleEntry, { moreEntry with methods := [scaleBody 61, scaleBody 64] }] scaled scaleSite = none := by decide +kernel @@ -423,14 +473,15 @@ private def appRaw : RawMethod := { staticRaw with code := [⟨0, 65046, .type appReceiver⟩, ⟨6, 40, .call staticSite⟩, ⟨11, 42, .none⟩] } private def appTarget : SpecializedMethod := ⟨[], { staticTarget.closed with owner := "App::Mine" }⟩ +private def appEmitted : Method := staticCall appReceiver staticSite /-- A struct of one pinned assembly implements a slot of another pinned assembly's interface, and the emitted body must carry the struct's own scope. -/ theorem cross_pin_implementation_valid : ConstrainedCallsValid appTypes [⟨[], appRaw⟩, appTarget] appRaw [true, true, true] - staticEmitted ∧ + appEmitted ∧ ¬ ConstrainedCallsValid appTypes [⟨[], appRaw⟩, staticTarget] appRaw [true, true, true] - staticEmitted := by decide +kernel + appEmitted := by decide +kernel #print axioms cross_pin_implementation_valid /-- Every catalog entry dispatch reads must name a valid pin scoping its type: a receiver, slot @@ -455,7 +506,7 @@ theorem lowered_default_exact : some ⟨7, 40, .call defaultSite⟩ ∧ (lowerConstrainedRaw overrideTypes defaultRaw).code[2]? = some ⟨7, 40, .call { defaultSite with - target := .managed 41, declaringType := allowance, typeArguments := [price], + target := .managed 41, method.owner := allowance, typeArguments := [price], ownerArgumentCount := 0 }⟩ := by decide +kernel #print axioms lowered_default_exact @@ -491,7 +542,7 @@ theorem prefix_target_valid : theorem lowered_call_exact : (lowerConstrainedRaw types raw).code = [⟨0, 2, .none⟩, ⟨1, 0, .none⟩, - ⟨7, 40, .call { site with target := .managed 21, declaringType := receiver }⟩, + ⟨7, 40, .call { site with target := .managed 21, method.owner := receiver }⟩, ⟨12, 42, .none⟩] := by decide +kernel #print axioms lowered_call_exact @@ -522,7 +573,7 @@ theorem indexed_type_identity : (DispatchIndex.ofTypes [concrete, declared]).resolve (.aggregate "Fixture::Counter" 1) site = none ∧ (DispatchIndex.ofTypes [concrete, declared]).resolve receiver - { site with declaringType := .atom "Other" "IValue" } = none := by decide +kernel + { site with method.owner := .atom "Other" "IValue" } = none := by decide +kernel #print axioms indexed_type_identity /-- Repeated override tokens do not duplicate one implementation; explicit still wins. -/ @@ -557,25 +608,26 @@ private def genericRaw : RawMethod := { raw with code := raw.code.set 1 ⟨1, 65046, .type genericReceiver⟩ } private def genericTarget : SpecializedMethod := ⟨[integer], { target.closed with owner := "Fixture::Counter`1", ownerArgumentCount := 1 }⟩ +private def genericEmitted : Method := constrainedEmitted genericReceiver site /-- Closed owner specialization composes with the existing exact constrained-call rule. -/ theorem generic_call_valid : ConstrainedCallsValid genericTypes [⟨[], genericRaw⟩, genericTarget] - genericRaw [true, true, true, true] emitted := by decide +kernel + genericRaw [true, true, true, true] genericEmitted := by decide +kernel #print axioms generic_call_valid /-- A different specialization of the same method token cannot satisfy the receiver. -/ theorem changed_owner_arguments_rejected : ¬ ConstrainedCallsValid genericTypes [⟨[], genericRaw⟩, { genericTarget with arguments := [.atom runtimeScope "System.Int64"] }] - genericRaw [true, true, true, true] emitted := by decide +kernel + genericRaw [true, true, true, true] genericEmitted := by decide +kernel #print axioms changed_owner_arguments_rejected /-- The owner arity cannot be dropped while retaining a selected generic method body. -/ theorem changed_owner_arity_rejected : ¬ ConstrainedCallsValid genericTypes [⟨[], genericRaw⟩, { genericTarget with closed := { genericTarget.closed with ownerArgumentCount := 0 } }] - genericRaw [true, true, true, true] emitted := by decide +kernel + genericRaw [true, true, true, true] genericEmitted := by decide +kernel #print axioms changed_owner_arity_rejected /-- Dispatch metadata cannot substitute a same-named owner from another assembly. -/ @@ -586,6 +638,73 @@ theorem changed_owner_scope_rejected : genericReceiver site = none := by decide +kernel #print axioms changed_owner_scope_rejected +/-- The class head with the same scope and name as the generic struct. -/ +private def classOwner : TypeExpr := .objectConstructor "Fixture" "Counter`1" + +/-- A method with a class owner cannot implement an interface slot on the struct. -/ +theorem class_implementation_owner_rejected : + Not (ConstrainedCallsValid + [{ concrete with identity := genericReceiver, methods := [ + { genericImplementation with owner := classOwner }] }, declared] + [SpecializedMethod.mk [] genericRaw, genericTarget] genericRaw + [true, true, true, true] genericEmitted) := by decide +kernel +#print axioms class_implementation_owner_rejected + +/-- A slot declared on the same-named class has no target on the struct receiver. -/ +theorem class_declaring_owner_rejected : + resolveConstrained genericTypes genericReceiver + { site with + method.owner := .apply classOwner integer, target := .managed 21, + typeArguments := [integer], ownerArgumentCount := 1 } = none := by decide +kernel +#print axioms class_declaring_owner_rejected + +/-- The struct lists the closed interface on which slot 5 is declared. -/ +private def genericSlotTypes (owner : TypeExpr) : List DispatchType := + [{ concrete with + identity := genericReceiver, interfaces := [.apply owner integer], + methods := [genericImplementation] }, + { declared with identity := .apply owner integer }] + +/-- An instance call of slot 5 on the supplied generic interface at Int32. -/ +private def genericSlotSite (owner : TypeExpr) : CallSite := + { site with + method.owner := .apply owner integer, typeArguments := [integer], ownerArgumentCount := 1 } + +/-- The caller and its direct link to token 21 must agree with resolution of slot 5. -/ +private def genericSlotCallValid (owner : TypeExpr) : Prop := + let callSite := genericSlotSite owner + let caller := { genericRaw with code := genericRaw.code.set 2 (.mk 7 111 (.call callSite)) } + ConstrainedCallsValid (genericSlotTypes owner) [.mk [] caller, genericTarget] caller + [true, true, true, true] (constrainedEmitted genericReceiver callSite) + +/-- The receiver's own scoped type cannot also declare interface slot 5. -/ +theorem conflicting_interface_slot_rejected : + resolveConstrained (genericSlotTypes classOwner) genericReceiver + (genericSlotSite classOwner) = none := by decide +kernel +#print axioms conflicting_interface_slot_rejected + +/-- A link to token 21 cannot certify slot 5 on the conflicting interface. -/ +theorem conflicting_interface_call_rejected : Not (genericSlotCallValid classOwner) := by + unfold genericSlotCallValid + decide +kernel +#print axioms conflicting_interface_call_rejected + +/-- The same receiver implements slot 5 of a distinct closed generic interface with token 21. -/ +theorem distinct_generic_interface_call_valid : + genericSlotCallValid (.objectConstructor "Fixture" "IValue`1") := by + unfold genericSlotCallValid + decide +kernel +#print axioms distinct_generic_interface_call_valid + +/-- A bare Counter constructor cannot select token 21 from the Counter<> aggregate. -/ +theorem bare_constructor_slot_rejected : + let counter : TypeExpr := .aggregate "Fixture::Counter<>" 0 + resolveConstrained [{ concrete with identity := counter, interfaces := [] }] counter + { site with + method.owner := .constructor "Fixture" "Counter", target := .managed 21, + typeArguments := [], ownerArgumentCount := 0 } = none := by decide +kernel +#print axioms bare_constructor_slot_rejected + /-- A class of the fixture assembly that type tests name. -/ private def dogClass : TypeExpr := .object "Fixture" "Fixture.Dog" private def dogEntry : DispatchType := { @@ -596,7 +715,7 @@ private def castRaw : RawMethod := { raw with parameters := [dogClass], returnType := dogClass, code := [⟨0, 2, .none⟩, ⟨1, 116, .type dogClass⟩, ⟨6, 42, .none⟩] } private def castEmitted : Method := { - locals := [], returnsValue := true, code := [.arg 0, .objectTest dogClass true, .ret] } + locals := [], returnsValue := true, code := [.ldarg 0, .castclass dogClass, .ret] } /-- A cast to a pinned catalog class is a type test of that class. -/ theorem class_cast_valid : @@ -623,7 +742,7 @@ theorem root_cast_rejected : [⟨[], { castRaw with code := [⟨0, 2, .none⟩, ⟨1, 116, .type root⟩, ⟨6, 42, .none⟩] }⟩] { castRaw with code := [⟨0, 2, .none⟩, ⟨1, 116, .type root⟩, ⟨6, 42, .none⟩] } [true, true, true] - { castEmitted with code := [.arg 0, .objectTest root true, .ret] } := by decide +kernel + { castEmitted with code := [.ldarg 0, .castclass root, .ret] } := by decide +kernel #print axioms root_cast_rejected /-- A type test emitted for an instruction that is no cast is rejected. -/ @@ -633,4 +752,27 @@ theorem misplaced_test_rejected : [true, true, true] castEmitted := by decide +kernel #print axioms misplaced_test_rejected -end Tests.ConstrainedEvidence \ No newline at end of file +/-- A cast emitted for a raw `isinst` is rejected: a type test stays in the opcode it lowers. -/ +theorem isinst_emitted_as_cast_rejected : + ¬ ConstrainedCallsValid [dogEntry] [⟨[], castRaw⟩] + { castRaw with code := [⟨0, 2, .none⟩, ⟨1, 117, .type dogClass⟩, ⟨6, 42, .none⟩] } + [true, true, true] castEmitted := by decide +kernel +#print axioms isinst_emitted_as_cast_rejected + +/-- `isinst Dog` of the argument, returned. -/ +private def isinstRaw : RawMethod := + { castRaw with code := [⟨0, 2, .none⟩, ⟨1, 117, .type dogClass⟩, ⟨6, 42, .none⟩] } + +/-- A raw `isinst` of a pinned catalog class emitted as `isinst` is valid. -/ +theorem class_isinst_valid : + ConstrainedCallsValid [dogEntry] [⟨[], isinstRaw⟩] isinstRaw [true, true, true] + { castEmitted with code := [.ldarg 0, .isinst dogClass, .ret] } := by decide +kernel +#print axioms class_isinst_valid + +/-- An `isinst` emitted for a raw `castclass` is rejected: the two differ on a failed test. -/ +theorem cast_emitted_as_isinst_rejected : + ¬ ConstrainedCallsValid [dogEntry] [⟨[], castRaw⟩] castRaw [true, true, true] + { castEmitted with code := [.ldarg 0, .isinst dogClass, .ret] } := by decide +kernel +#print axioms cast_emitted_as_isinst_rejected + +end Tests.ConstrainedEvidence diff --git a/Tests/ConstrainedResolution.cs b/Tests/ConstrainedResolution.cs index 856a5f8fe..e2f4149b2 100644 --- a/Tests/ConstrainedResolution.cs +++ b/Tests/ConstrainedResolution.cs @@ -2,6 +2,12 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include PinnedMetadata.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Reflection; using Mono.Cecil; @@ -10,6 +16,9 @@ var extractor = Assembly.LoadFrom(ExtractorPath); var resolve = extractor.GetType("Nethermind.LeanCIL.ConstrainedCalls", throwOnError: true)! .GetMethod("Resolve", BindingFlags.Static | BindingFlags.NonPublic)!; +const BindingFlags Internal = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; +var pinnedType = extractor.GetType("Nethermind.LeanCIL.PinnedAssemblies", throwOnError: true)!; +PinnedMetadata Pinned() => new(extractor, At("Examples/Assembly/bin/Examples.dll")); object Index(MethodDefinition method) => Activator.CreateInstance( extractor.GetType("Nethermind.LeanCIL.InstructionIndex", true)!, @@ -21,7 +30,8 @@ object Index(MethodDefinition method) => Activator.CreateInstance( ("ExplicitCounterValue", "Examples.ICounterValue.Read") }) { - using var module = ModuleDefinition.ReadModule(At("Examples/Assembly/bin/Examples.dll")); + using var pinned = Pinned(); + var module = pinned.Module; var method = module.GetType("Examples.ConstrainedCalls").Methods.Single(method => method.Name == "Read"); var receiver = module.GetType("Examples." + receiverName); var prefix = method.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Constrained); @@ -43,7 +53,8 @@ object Index(MethodDefinition method) => Activator.CreateInstance( ("open-direct-owner", "Unsupported constrained method owner") }) { - using var module = ModuleDefinition.ReadModule(At("Examples/Assembly/bin/Examples.dll")); + using var pinned = Pinned(); + var module = pinned.Module; var method = module.GetType("Examples.ConstrainedCalls").Methods.Single(method => method.Name == "Read"); var receiver = module.GetType("Examples.CounterValue"); var prefix = method.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Constrained); @@ -79,3 +90,88 @@ object Index(MethodDefinition method) => Activator.CreateInstance( Console.WriteLine("PASS constrained rejection: " + mutation); } } + +var runtimeType = extractor.GetType("Nethermind.LeanCIL.RuntimeModels", throwOnError: true)!; +var exactScope = runtimeType.GetMethod("ExactScope", BindingFlags.Static | BindingFlags.NonPublic)!; +var platformType = extractor.GetType("Nethermind.LeanCIL.PlatformAssemblies", throwOnError: true)!; +var platform = platformType.GetField("SystemRuntime", BindingFlags.Static | BindingFlags.NonPublic)!.GetValue(null); +foreach (bool forged in new[] { false, true }) +{ + using var fixture = Pinned(); + var pinned = fixture.Context; + using var source = ModuleDefinition.ReadModule(At("Examples/Assembly/bin/Examples.dll")); + source.Assembly.Name.Name = "IdentityOnly"; + if (forged) source.AssemblyReferences.Single(reference => reference.Name == "System.Runtime").Version = new(1, 0); + using var stream = new MemoryStream(); + source.Write(stream); + byte[] bytes = stream.ToArray(); + using var imported = ModuleDefinition.ReadModule(new MemoryStream(bytes)); + var parent = imported.GetType("Examples.CounterValue").BaseType; + ExpectRejected(() => exactScope.Invoke(null, [parent, platform])); + pinnedType.GetMethod("RegisterIdentityImage", Internal)!.Invoke(pinned, [imported, bytes]); + // Cecil's display fields cannot replace the image's assembly identity columns. + imported.AssemblyReferences.Single(reference => reference.Name == "System.Runtime").Version = new(11, 0, 0, 0); + if ((bool)exactScope.Invoke(null, [parent, platform])! == forged) + throw new Exception("Imported identity did not match its raw assembly row."); + if ((bool)pinnedType.GetMethod("Contains", Internal)!.Invoke(pinned, [imported])! || + pinnedType.GetMethod("ModuleOf", Internal)! + .Invoke(pinned, [imported.GetType("Examples.CounterValue")]) is not null) + throw new Exception("Metadata-only image became an executable pin."); + var method = imported.GetType("Examples.CounterValue").Methods.Single(member => member.Name == "Read"); + ExpectRejected(() => pinnedType.GetMethod("Body", Internal)!.Invoke(pinned, [method])); + ExpectRejected(() => pinnedType.GetMethod("Metadata", Internal)!.Invoke(pinned, [imported])); + ExpectRejected(() => pinnedType.GetMethod("Original", Internal)!.Invoke(pinned, [imported])); + ExpectRejected(() => pinnedType.GetMethod("Index", Internal)!.Invoke(pinned, [imported])); + ExpectRejected(() => ((IAssemblyResolver)pinned).Resolve(imported.Assembly.Name)); + Console.WriteLine("PASS metadata-only identity and executable isolation: forged=" + forged); +} + +double IdentityLookupCost(int count) +{ + using var fixture = Pinned(); + var register = pinnedType.GetMethod("RegisterIdentityImage", Internal)! + .CreateDelegate>(fixture.Context); + var lookup = pinnedType.GetMethod("IdentityMetadata", Internal)! + .CreateDelegate>(fixture.Context); + byte[] bytes = File.ReadAllBytes(At("Examples/Assembly/bin/Examples.dll")); + var modules = new List(); + try + { + for (int index = 0; index < count; index++) + { + var module = ModuleDefinition.ReadModule(new MemoryStream(bytes)); + modules.Add(module); + register(module, bytes); + } + var samples = new List(); + for (int sample = 0; sample < 7; sample++) + { + long start = System.Diagnostics.Stopwatch.GetTimestamp(); + for (int repeat = 0; repeat < 128; repeat++) + foreach (var module in modules) + if (!ReferenceEquals(lookup(module), lookup(module))) + throw new Exception("Import metadata was not cached by module."); + samples.Add(System.Diagnostics.Stopwatch.GetElapsedTime(start).TotalMilliseconds); + } + samples.Sort(); + Console.WriteLine($"{count} imported modules, 128 identity lookup passes: {samples[0]:F3} ms minimum"); + return samples[0]; + } + finally { foreach (var module in modules) module.Dispose(); } +} + +// A lookup that scans the registered modules costs 256 times as much at 16 times the modules; a keyed +// lookup stays within the factor of 4 that cache misses add per lookup on a loaded machine. +double smallest = IdentityLookupCost(64); +IdentityLookupCost(256); +double largest = IdentityLookupCost(1024); +if (largest > smallest * 16 * 4 + 1) + throw new Exception("Import identity lookup cost grew faster than module count."); + +static void ExpectRejected(Action action) +{ + try { action(); } + catch (InvalidDataException) { return; } + catch (TargetInvocationException error) when (error.InnerException is InvalidDataException) { return; } + throw new Exception("Unregistered identity or imported executable image was accepted."); +} diff --git a/Tests/ConstructContract.cs b/Tests/ConstructContract.cs index b0e524c9d..8f11f84ab 100644 --- a/Tests/ConstructContract.cs +++ b/Tests/ConstructContract.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // One caller proof, unedited, must verify against a build whose constructors are reimplemented; // a build whose constructor stores the wrong value must fail the constructor proof and violate its contract. @@ -14,7 +19,8 @@ string Build(string name, string pair, string word) var source = Path.Combine(root, name); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Constructs.csproj"), """ - net10.0true + net11.0 + true """); File.WriteAllText(Path.Combine(source, "Constructs.cs"), "namespace Constructs; " + "public struct Pair { public ulong Low; public ulong High; public Pair(ulong low, ulong high) { " + pair + " } } " + @@ -171,11 +177,10 @@ subst member exact ⟨_, rfl, by decide⟩ /-- This build stores `high + 1`, so `Pair(0, 0)` reads back as `Pair(0, 1)`. -/ -theorem correctness : ¬ pairContract.Holds Extracted.program pair := by - intro holds - obtain ⟨_, ⟨final, values⟩, ran, -, _, -, wrong⟩ := holds ⟨⟨0, 0⟩, 0, 0⟩ storage ⟨storage_formed, rfl⟩ - obtain ⟨rfl, rfl⟩ := Prod.mk.inj (invoke_ok_unique ran (rfl : invoke Extracted.program 16 pair - (pairContract.args ⟨⟨0, 0⟩, 0, 0⟩) storage = .ok _)) +theorem correctness : pairContract.Refuted Extracted.program pair := + .of_return (input := ⟨⟨0, 0⟩, 0, 0⟩) (initial := storage) ⟨storage_formed, rfl⟩ + (rfl : invoke Extracted.program 16 pair (pairContract.args ⟨⟨0, 0⟩, 0, 0⟩) storage = .ok _) <| by + rintro ⟨-, _, -, wrong⟩ have observed : (Except.ok (pairBytes ⟨⟨0, 0⟩, 0, 0⟩) : Checked (List (BitVec 8))) = .ok (numberBytes 0 8 ++ numberBytes 1 8) := wrong.symm.trans rfl exact absurd (Except.ok.inj observed) (by decide) diff --git a/Tests/ConstructContract.lean b/Tests/ConstructContract.lean index 8f7ee3776..7fd1486e7 100644 --- a/Tests/ConstructContract.lean +++ b/Tests/ConstructContract.lean @@ -9,17 +9,63 @@ namespace Extracted open CIL +@[cil_code] def fieldEnvironment : LayoutEnvironment := + { rawLayouts := [ + { identity := "Constructs.Pair", explicit := false, packing := 0, declaredSize := 0, + fields := List.replicate 2 { type := .unsigned64 } }, + { identity := "Constructs.Word", explicit := false, packing := 0, declaredSize := 0, + fields := List.replicate 4 { type := .unsigned64 } }, + { identity := "Constructs.Logged", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .unsigned64 }] }] + layouts := #[⟨⟨16, [⟨0, 8, false⟩, ⟨8, 8, false⟩]⟩, 8⟩, + ⟨⟨32, [⟨0, 8, false⟩, ⟨8, 8, false⟩, ⟨16, 8, false⟩, ⟨24, 8, false⟩]⟩, 8⟩, + ⟨⟨8, [⟨0, 8, false⟩]⟩, 8⟩] } + +theorem layouts_valid : Extraction.LayoutsValid fieldEnvironment.rawLayouts fieldEnvironment.layouts .x64 := + by decide +kernel +#print axioms layouts_valid + +def pairLow : Extraction.FieldRef := + { owner := .aggregate "Constructs.Pair" 0, index := 0, name := name!"Low", type := .atom Extraction.runtimeScope "System.UInt64" } +cil_field_shape fieldEnvironment pairLow + +def pairHigh : Extraction.FieldRef := + { owner := .aggregate "Constructs.Pair" 0, index := 1, name := name!"High", type := .atom Extraction.runtimeScope "System.UInt64" } +cil_field_shape fieldEnvironment pairHigh + +def limb0 : Extraction.FieldRef := + { owner := .aggregate "Constructs.Word" 1, index := 0, name := name!"U0", type := .atom Extraction.runtimeScope "System.UInt64" } +cil_field_shape fieldEnvironment limb0 + +def limb1 : Extraction.FieldRef := + { owner := .aggregate "Constructs.Word" 1, index := 1, name := name!"U1", type := .atom Extraction.runtimeScope "System.UInt64" } +cil_field_shape fieldEnvironment limb1 + +def limb2 : Extraction.FieldRef := + { owner := .aggregate "Constructs.Word" 1, index := 2, name := name!"U2", type := .atom Extraction.runtimeScope "System.UInt64" } +cil_field_shape fieldEnvironment limb2 + +def limb3 : Extraction.FieldRef := + { owner := .aggregate "Constructs.Word" 1, index := 3, name := name!"U3", type := .atom Extraction.runtimeScope "System.UInt64" } +cil_field_shape fieldEnvironment limb3 + +def loggedValue : Extraction.FieldRef := + { owner := .aggregate "Constructs.Logged" 2, index := 0, name := name!"Value", type := .atom Extraction.runtimeScope "System.UInt64" } +cil_field_shape fieldEnvironment loggedValue + def method0Signature : String := "System.UInt64 Constructs.Calls::Root(System.UInt64,System.UInt64)" def method1Signature : String := "System.Void Constructs.Word::.ctor(System.UInt64,System.UInt64,System.UInt64,System.UInt64)" def method2Signature : String := "System.Void Constructs.Pair::.ctor(System.UInt64,System.UInt64)" -@[cil_code] def method0 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .arg 1, .newAggregate 2 2 16, .fieldAt 8 8, .arg 1, .const32 1, .convI8, .const32 2, .convI8, .arg 0, - .newAggregate 1 4 32, .fieldAt 24 8, .add, .ret] } -@[cil_code] def method1 : Method := { returnsValue := false, locals := [], code := - [.arg 0, .arg 1, .setFieldAt 0 8, .arg 0, .arg 2, .setFieldAt 8 8, .arg 0, .arg 3, .setFieldAt 16 8, - .arg 0, .arg 4, .setFieldAt 24 8, .ret] } -@[cil_code] def method2 : Method := { returnsValue := false, locals := [], code := - [.arg 0, .arg 1, .setFieldAt 0 8, .arg 0, .arg 2, .setFieldAt 8 8, .ret] } +@[cil_code] def method0 : Method := { + environment := fieldEnvironment, returnsValue := true, locals := [], + code := [.ldarg 0, .ldarg 1, .newobj (.arity 2), .ldfld Extracted.pairHigh, .ldarg 1, .ldc_i4 1, .conv_i8, + .ldc_i4 2, .conv_i8, .ldarg 0, .newobj (.arity 4), .ldfld limb3, .add, .ret], + links := [(2, .aggregate 2 16), (10, .aggregate 1 32)] } +@[cil_code] def method1 : Method := { environment := Extracted.fieldEnvironment, returnsValue := false, locals := [], code := + [.ldarg 0, .ldarg 1, .stfld limb0, .ldarg 0, .ldarg 2, .stfld limb1, .ldarg 0, .ldarg 3, + .stfld limb2, .ldarg 0, .ldarg 4, .stfld limb3, .ret] } +@[cil_code] def method2 : Method := { environment := Extracted.fieldEnvironment, returnsValue := false, locals := [], code := + [.ldarg 0, .ldarg 1, .stfld Extracted.pairLow, .ldarg 0, .ldarg 2, .stfld pairHigh, .ret] } @[cil_code] def program : Program := [method0, method1, method2] def signatures : List String := [method0Signature, method1Signature, method2Signature] @@ -209,10 +255,10 @@ theorem root_correct : rootContract.Holds Extracted.program root := root_holds p /-! `newobj` zeroes its storage first, so an unassigned `High` reads back as zero, not uninitialized. -/ namespace Defaults -@[cil_code] def caller : CIL.Method := { returnsValue := true, locals := [], code := - [.arg 0, .newAggregate 1 1 16, .fieldAt 8 8, .ret] } -@[cil_code] def lowOnly : CIL.Method := { returnsValue := false, locals := [], code := - [.arg 0, .arg 1, .setFieldAt 0 8, .ret] } +@[cil_code] def caller : CIL.Method := { environment := Extracted.fieldEnvironment, returnsValue := true, locals := [], code := + [.ldarg 0, .newobj (.arity 1), .ldfld Extracted.pairHigh, .ret], links := [(1, .aggregate 1 16)] } +@[cil_code] def lowOnly : CIL.Method := { environment := Extracted.fieldEnvironment, returnsValue := false, locals := [], code := + [.ldarg 0, .ldarg 1, .stfld Extracted.pairLow, .ret] } @[cil_code] def program : CIL.Program := [caller, lowOnly] structure LowInput where @@ -310,19 +356,14 @@ def unguardedContract : MethodContract PairInput Unit := { pairContract with pre := fun input m => m.WellFormed ∧ read m input.this 16 1 = .ok (List.replicate 16 0) } /-- Readable, zeroed storage is not enough: the constructor's first store faults without write authority. -/ -theorem missing_authority_rejected : ¬ unguardedContract.Holds Extracted.program pair := by - intro holds - obtain ⟨fuel, ⟨final, values⟩, ran, -⟩ := holds ⟨⟨0, 0⟩, 0, 0⟩ readOnly ⟨readOnly_formed, rfl⟩ - have faulted : invoke Extracted.program 8 pair (unguardedContract.args ⟨⟨0, 0⟩, 0, 0⟩) readOnly = - .error ⟨2, 2, .memory .unwritable (.address ⟨0, 0⟩) 8⟩ := rfl - have up := invoke_mono _ _ 8 _ _ _ (.ok (final, values)) trivial ran - have down := invoke_mono _ _ fuel _ _ _ _ (by simp [Settled]) faulted - rw [Nat.add_comm] at down - cases up.symm.trans down +theorem missing_authority_rejected : unguardedContract.Refuted Extracted.program pair := + .of_fault (input := ⟨⟨0, 0⟩, 0, 0⟩) (initial := readOnly) ⟨readOnly_formed, rfl⟩ (by decide) + (rfl : invoke Extracted.program 8 pair (unguardedContract.args ⟨⟨0, 0⟩, 0, 0⟩) readOnly = + .error ⟨2, 2, .memory .unwritable (.address ⟨0, 0⟩) 8⟩) /-- `Logged(ref ulong log, ulong value) { log = 1; Value = value; }` -/ -@[cil_code] def logged : CIL.Method := { returnsValue := false, locals := [], code := - [.arg 1, .const64 1, .store64, .arg 0, .arg 2, .setFieldAt 0 8, .ret] } +@[cil_code] def logged : CIL.Method := { environment := Extracted.fieldEnvironment, returnsValue := false, locals := [], code := + [.ldarg 1, .ldc_i8 1, .stind .i8, .ldarg 0, .ldarg 2, .stfld Extracted.loggedValue, .ret] } @[cil_code] def loggedProgram : CIL.Program := [logged] structure LoggedInput where @@ -350,12 +391,10 @@ theorem shared_formed : shared.WellFormed where exact ⟨_, rfl, by decide⟩ /-- The write through `log` lands outside the footprint, so the contract is refuted by a concrete run. -/ -theorem stray_write_rejected : ¬ loggedContract.Holds loggedProgram 0 := by - intro holds - obtain ⟨_, ⟨final, values⟩, ran, framed, -⟩ := - holds ⟨⟨0, 0⟩, ⟨0, 8⟩, 0⟩ shared ⟨shared_formed, rfl, rfl⟩ - obtain ⟨rfl, rfl⟩ := Prod.mk.inj (invoke_ok_unique ran (rfl : invoke loggedProgram 8 0 - (loggedContract.args ⟨⟨0, 0⟩, ⟨0, 8⟩, 0⟩) shared = .ok _)) +theorem stray_write_rejected : loggedContract.Refuted loggedProgram 0 := + .of_return (input := ⟨⟨0, 0⟩, ⟨0, 8⟩, 0⟩) (initial := shared) ⟨shared_formed, rfl, rfl⟩ + (rfl : invoke loggedProgram 8 0 (loggedContract.args ⟨⟨0, 0⟩, ⟨0, 8⟩, 0⟩) shared = .ok _) <| by + rintro ⟨framed, -⟩ have kept := framed.cells 0 (by decide) 8 (by simp [Outside, loggedContract]) exact absurd kept (by decide) @@ -379,12 +418,7 @@ theorem half_width_rejected (value : PairInput → Unit → List (BitVec 8)) : set_option pp.mvars false in /-- -error: Tactic `rfl` failed: The left-hand side - [CIL.Op.arg 0, CIL.Op.arg 1, CIL.Op.newAggregate 2 2 16, CIL.Op.fieldAt 8 8, CIL.Op.arg 1, CIL.Op.const32 1, - CIL.Op.convI8, CIL.Op.const32 2, CIL.Op.convI8, CIL.Op.arg 0, CIL.Op.newAggregate 1 4 32, CIL.Op.fieldAt 24 8, - CIL.Op.add, CIL.Op.ret][2]? -is not definitionally equal to the right-hand side - some (CIL.Op.newAggregate 2 ?_ 8) +⊢ some (CIL.Link.aggregate 2 16) = some (CIL.Link.aggregate 2 8) -/ #guard_msgs (substring := true) in /-- The tactic refuses an 8-byte constructor contract at the 16-byte `newobj` site. -/ diff --git a/Tests/Constructors.cs b/Tests/Constructors.cs index 04c0b2bd6..a19645a98 100644 --- a/Tests/Constructors.cs +++ b/Tests/Constructors.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -132,8 +137,9 @@ string Mutate(string mutation, string output) var classExtraction = Path.Combine(classOutput, "Extraction"); Run(Extract("extract", classDll, "Examples", "Constructors::Return(ulong)", classExtraction)); var classLean = File.ReadAllText(Path.Combine(classExtraction, "Extracted.lean")); -if (!classLean.Contains(".newObject ", StringComparison.Ordinal) || - classLean.Contains(".newAggregate ", StringComparison.Ordinal)) +// Only a construction has an `.object` link. +if (!classLean.Contains(": Link := .object ", StringComparison.Ordinal) || + classLean.Contains(": Link := .aggregate ", StringComparison.Ordinal)) throw new InvalidOperationException("Class construction was not extracted as a heap allocation:\n" + classLean); var classProof = Path.Combine(classOutput, "ClassProof.lean"); File.WriteAllText(classProof, """ diff --git a/Tests/ConsumerArtifactSnapshot.cs b/Tests/ConsumerArtifactSnapshot.cs index d3ec52e38..a5370990e 100644 --- a/Tests/ConsumerArtifactSnapshot.cs +++ b/Tests/ConsumerArtifactSnapshot.cs @@ -1,6 +1,10 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs #:include ../scripts/LibraryAudit.cs diff --git a/Tests/ConsumerBatchOutcomes.cs b/Tests/ConsumerBatchOutcomes.cs index 011581c49..b13888f08 100644 --- a/Tests/ConsumerBatchOutcomes.cs +++ b/Tests/ConsumerBatchOutcomes.cs @@ -14,6 +14,11 @@ static void CheckConsumerBatchOutcomes(string[] cli, string root, string proof, var axiomProof = Path.Combine(root, "ProofAxiom.lean"); var missingProof = Path.Combine(root, "ProofMissingSibling.lean"); var hiddenProof = Path.Combine(root, "ProofHiddenSibling.lean"); + var contract = Path.Combine(root, "Client", "Contract.lean"); + var contractProof = Path.Combine(root, "ContractProof.lean"); + var refutationProof = Path.Combine(root, "RefutationProof.lean"); + var brokenProof = Path.Combine(root, "BrokenRefutation.lean"); + var otherProof = Path.Combine(root, "OtherRefutation.lean"); var directory = Path.Combine(Path.GetDirectoryName(batchB)!, "OutcomeChecks-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(directory); var trace = ConsumerTraceDirectory.Create(directory, "Trace"); @@ -64,21 +69,46 @@ set_option debug.skipKernelTC true "MissingSibling-" + Guid.NewGuid().ToString("N")); var hiddenOutput = Path.Combine(Path.GetDirectoryName(batchB)!, "HiddenSibling-" + Guid.NewGuid().ToString("N")); + string Copy(string source, string name) + { + var copy = Path.Combine(Path.GetDirectoryName(source)!, name + "-" + Guid.NewGuid().ToString("N")); + CopyTree(source, copy); + return copy; + } foreach (var output in new[] { axiomOutput, forgedOutput, missingOutput, hiddenOutput }) CopyTree(batchB, output); + // Expected rejections: a refutation of the contract the Add copy verifies, and one control per way a + // rejection can fail; the tampered copy fails its extraction check. + var (contractOutput, absentOutput) = (Copy(batchB, "Contract"), Copy(batchB, "Absent")); + var (refutedOutput, tamperedOutput) = (Copy(batchC, "Refuted"), Copy(batchC, "Tampered")); + File.AppendAllText(Path.Combine(tamperedOutput, "Extracted.lean"), "\n-- changed after extraction\n"); + File.WriteAllText(contract, ContractSource); + File.WriteAllText(contractProof, $"import Client.Contract\n{HoldsSource}"); + File.WriteAllText(refutationProof, Refuting("RefutationProof", "add", 0, 1)); + File.WriteAllText(brokenProof, Refuting("BrokenRefutation", "add", 0, 0)); + File.WriteAllText(otherProof, Refuting("OtherRefutation", "product", 1, 1)); foreach (var output in new[] { axiomOutput, forgedOutput, missingOutput, hiddenOutput, batchC }) File.WriteAllText(Path.Combine(output, "verified.json"), "{}"); object Request(string output, string selectedProof, string theorem = "ExampleProof.correctness", string expect = "verified") => new { output, proof = selectedProof, theorem, expect }; + // Separate runs put every request from the third on in one manifest, which numbers them from zero. + const int contractRequest = 9; + var refutes = separate ? contractRequest - 2 : contractRequest; + object Rejected(string output, string selectedProof, string theorem) => + new { output, proof = selectedProof, theorem, expect = "rejected", refutes }; object[] requests = [ - Request(axiomOutput, axiomProof), Request(forgedOutput, forgedProof), - Request(batchA, proof, expect: "rejected"), Request(batchA, proof), + Request(axiomOutput, axiomProof), Request(forgedOutput, forgedProof), Request(batchA, proof), Request(batchB, forgedProof), Request(batchB, proof), Request(batchB, alternate, "AlternateProof.correctness"), Request(batchC, proof), - Request(batchC, proof, expect: "rejected"), Request(missingOutput, missingProof, "AlternateProof.correctness"), - Request(hiddenOutput, hiddenProof, "HiddenSibling.false") + Request(hiddenOutput, hiddenProof, "HiddenSibling.false"), + Request(contractOutput, contractProof, "ContractProof.holds"), + Rejected(refutedOutput, refutationProof, "RefutationProof.refuted"), + Rejected(refutedOutput, brokenProof, "BrokenRefutation.refuted"), + Rejected(refutedOutput, otherProof, "OtherRefutation.refuted"), + Rejected(absentOutput, contractProof, "ContractProof.holds"), + Rejected(tamperedOutput, refutationProof, "RefutationProof.refuted") ]; Environment.SetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY", trace); Environment.SetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE", "true"); @@ -102,17 +132,24 @@ object Request(string output, string selectedProof, string theorem = "ExamplePro item.GetProperty("stage").GetString())); } timer.Stop(); - string[] expected = ["failed/theorem audit", "failed/kernel replay", "failed/proof checking", - "verified/extraction integrity", "failed/kernel replay", "verified/extraction integrity", - "verified/extraction integrity", "failed/proof checking", "rejected/proof checking", - "failed/theorem audit", "failed/kernel replay"]; + // A rejection fails when its refutation does not build, refutes another contract, refutes nothing or + // reads a changed extraction. + string[] expected = ["failed/theorem audit", "failed/kernel replay", "verified/extraction integrity", + "failed/kernel replay", "verified/extraction integrity", "verified/extraction integrity", + "failed/proof checking", "failed/theorem audit", "failed/kernel replay", + "verified/extraction integrity", "rejected/extraction integrity", "failed/proof checking", + "failed/statement identity", "failed/statement identity", "failed/extraction integrity"]; var text = string.Join('\n', diagnostics); if (!statuses.SequenceEqual(expected) || !text.Contains("Unapproved proof axioms") || !text.Contains("Client.Axiomatic.execution") || !text.Contains("Client.Batch.forged") || !text.Contains("while replaying declaration") || !text.Contains("Expected a theorem") || !text.Contains("HiddenSibling.false") || !text.Contains("Client.Axiomatic.hidden") || - !text.Contains(separate ? "Batch: 4 passed, 5 failed of 9 requests." : - "Batch: 4 passed, 7 failed of 11 requests.")) + !text.Contains("PASS rejected") || !text.Contains("(refutes CIL.Safety.MethodContract.Holds for " + + "System.UInt64 Examples.Arithmetic::WrongAdd(System.UInt64,System.UInt64))") || + !text.Contains("so it refutes another contract") || !text.Contains("so it refutes nothing") || + text.Split($"no verified refutation of request {refutes}").Length != 5 || + !text.Contains(separate ? "Batch: 5 passed, 8 failed of 13 requests." : + "Batch: 5 passed, 10 failed of 15 requests.")) throw new InvalidOperationException( "Unexpected batch outcomes: " + string.Join(", ", statuses) + "\n" + text); CheckConsumerLeanSummary(text, "Client/Shared.lean:", "(building Client.Shared)"); @@ -164,11 +201,66 @@ object Request(string output, string selectedProof, string theorem = "ExamplePro { Environment.SetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY", savedTrace); Environment.SetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE", savedCache); - foreach (var path in new[] { forged, forgedProof, axiomatic, axiomProof, missingProof, hiddenProof }) + foreach (var path in new[] { forged, forgedProof, axiomatic, axiomProof, missingProof, hiddenProof, + contract, contractProof, refutationProof, brokenProof, otherProof }) File.Delete(path); } } + /// The addition contract the Add copy verifies, and the product contract a control refutes. + const string ContractSource = """ + import Extracted + import CIL.Safety.SymbolicExecution + import CIL.Safety.MethodContract + open CIL.Safety + namespace Client.Contract + /-- Every pair of words returns their wrapping sum and leaves memory unchanged. -/ + def add : MethodContract (CIL.W64 × CIL.W64) where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + pre _ _ := True + footprint _ := [] + post input initial final values := + final = initial ∧ values = [.scalar (.i64 (input.1 + input.2))] + /-- Every pair of words returns their wrapping product and leaves memory unchanged. -/ + def product : MethodContract (CIL.W64 × CIL.W64) where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + pre _ _ := True + footprint _ := [] + post input initial final values := + final = initial ∧ values = [.scalar (.i64 (input.1 * input.2))] + end Client.Contract + """; + + /// The addition contract proved for the extracted Add. + const string HoldsSource = """ + namespace ContractProof + open CIL.Safety + /-- The extracted method meets the addition contract. -/ + theorem holds : Client.Contract.add.Holds Extracted.program 0 := by + rintro ⟨left, right⟩ memory - + refine MethodContract.Holds.intro (c := Client.Contract.add) (input := (left, right)) memory + [.scalar (.i64 (left + right))] ?_ (Confined.refl _ memory) ⟨rfl, rfl⟩ + dsimp only [Client.Contract.add] + cil_checked_steps [] + #print axioms holds + end ContractProof + """; + + /// A refutation of `contract` whose witness is `(left, right)`. + static string Refuting(string space, string contract, int left, int right) => $$""" + import Client.Contract + namespace {{space}} + open CIL.Safety + /-- The extracted method returns another value than the contract at `({{left}}, {{right}})`. -/ + theorem refuted : Client.Contract.{{contract}}.Refuted Extracted.program 0 := + .of_return (input := ({{left}}, {{right}})) (initial := Memory.empty) trivial + (rfl : invoke Extracted.program 16 0 [.scalar (.i64 {{left}}), .scalar (.i64 {{right}})] + Memory.empty = .ok _) + (by rintro ⟨-, out, rfl, -, wrong⟩; revert wrong; decide) + #print axioms refuted + end {{space}} + """; + static void CheckConsumerLeanSummary(string text, params string[] required) { var start = text.IndexOf("Lean errors (", StringComparison.Ordinal); diff --git a/Tests/ConsumerBatchSetup.cs b/Tests/ConsumerBatchSetup.cs index 8285b8e8d..d9326c5b4 100644 --- a/Tests/ConsumerBatchSetup.cs +++ b/Tests/ConsumerBatchSetup.cs @@ -1,9 +1,15 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ConsumerTraceDirectory.cs #:include ConsumerBatchOutcomes.cs +// Standalone timing runner for the mixed-outcome batch that core checks; no suite runs it. using System.Text.Json; var separate = args.Contains("--separate", StringComparer.Ordinal); diff --git a/Tests/ConsumerCertificateReuse.cs b/Tests/ConsumerCertificateReuse.cs index 56e94b140..8f02ef374 100644 --- a/Tests/ConsumerCertificateReuse.cs +++ b/Tests/ConsumerCertificateReuse.cs @@ -40,7 +40,9 @@ import Extracted namespace ExampleProof /-- The extracted program satisfies its translation evidence. -/ theorem correctness : CIL.Extraction.TranslationValid Extracted.rawLayouts Extracted.computedLayouts - Extracted.methodEvidence Extracted.imports Extracted.program := Extracted.translation_valid + Extracted.assemblies Extracted.platformDefinitions Extracted.methodEvidence Extracted.imports + Extracted.program := + Extracted.translation_valid #print axioms correctness end ExampleProof """); @@ -156,9 +158,13 @@ void Requests(bool replaceArtifact, bool expectRejection = false) => schema = 1, proofRoot = package, requests = new[] { - new { output, proof = replaceArtifact ? proof : cleanProof, - theorem = "ExampleProof.correctness", expect = expectRejection ? "rejected" : "verified" }, - new { output = other, proof = cleanProof, theorem = "ExampleProof.correctness", expect = "verified" } + expectRejection + ? new { output, proof, theorem = "ExampleProof.correctness", expect = "rejected", + refutes = (int?)1 } + : new { output, proof = replaceArtifact ? proof : cleanProof, + theorem = "ExampleProof.correctness", expect = "verified", refutes = (int?)null }, + new { output = other, proof = cleanProof, theorem = "ExampleProof.correctness", expect = "verified", + refutes = (int?)null } } }, Json)); Requests(replaceArtifact: false); @@ -181,7 +187,7 @@ IO.FS.writeBinFile artifact (bytes.push 0) foreach (string[] options in new string[][] { [], ["--fresh"] }) { var preparation = options.Length == 0 ? "restored" : "built"; - // Expected rejections require an audit wrapper absent from the verified warmup. + // An expected rejection runs after the request it refutes, and still fails on the changed artifact. Requests(replaceArtifact: true, expectRejection: options.Length != 0); File.WriteAllText(Path.Combine(output, "verified.json"), "stale report"); var replaced = Execute([.. cli, "verify-batch", batch, "--parallel", "2", diff --git a/Tests/ConsumerCombinedAudit.cs b/Tests/ConsumerCombinedAudit.cs index e6378cfa4..9c54343d3 100644 --- a/Tests/ConsumerCombinedAudit.cs +++ b/Tests/ConsumerCombinedAudit.cs @@ -1,7 +1,13 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +// Standalone timing and profiling workload for a combined audit (several minutes); no suite runs it. using System.Diagnostics; using System.Text.Json; diff --git a/Tests/ConsumerComponents.cs b/Tests/ConsumerComponents.cs index 31963f782..68f72d795 100644 --- a/Tests/ConsumerComponents.cs +++ b/Tests/ConsumerComponents.cs @@ -18,7 +18,7 @@ static void CheckConsumerComponents(string[] args) Directory.CreateDirectory(package); File.WriteAllText(Path.Combine(assemblyRoot, "Components.csproj"), """ - net10.0true + net11.0true """); File.WriteAllText(Path.Combine(assemblyRoot, "Methods.cs"), """ @@ -28,6 +28,10 @@ public static class Methods public static ulong Second(ulong value) => Helper(value) + 2; public static ulong Third(ulong value) => Helper(value) + 3; public static ulong Fourth(ulong value) => Helper(value) + 4; + public static ulong Leaf(ulong value) => value ^ 5UL; + public static ulong Wrapped(ulong value) => Leaf(value) + 6; + public static ulong ViaWrapped(ulong value) => Wrapped(value) + 7; + public static ulong ViaBoth(ulong value) => Wrapped(value) + Helper(value); public static ulong Helper(ulong value) { BODY @@ -50,7 +54,9 @@ import Extracted namespace ExampleProof /-- Every method and connection in this extraction satisfies the translation validator. -/ theorem correctness : CIL.Extraction.TranslationValid Extracted.rawLayouts Extracted.computedLayouts - Extracted.methodEvidence Extracted.imports Extracted.program := Extracted.translation_valid + Extracted.assemblies Extracted.platformDefinitions Extracted.methodEvidence Extracted.imports + Extracted.program := + Extracted.translation_valid #print axioms correctness end ExampleProof """); @@ -82,14 +88,17 @@ end ExampleProof .Replace("nativeWidth := 64", "nativeWidth := 32", StringComparison.Ordinal)); if (changedProfile.Sources.Keys.Intersect(first.Sources.Keys).Any()) throw new Exception("A changed header retained dependent component identities."); - var helperStart = original.IndexOf("def method1Signature", StringComparison.Ordinal); - var changedHelper = SplitExtractionComponents(original[..helperStart] + original[helperStart..] - .Replace(".add", ".sub", StringComparison.Ordinal)); + // The helper is defined from its decoder, so its add is the raw opcode the decoder reads. + var changedHelper = SplitExtractionComponents(ForgeDecodedOriginal(original, 1, "opcode := 88,", + "opcode := 89,")); if (changedHelper.Sources.Keys.Intersect(first.Sources.Keys).Count() != 2) throw new Exception("A changed helper did not invalidate exactly its checked component."); - var changedCall = SplitExtractionComponents(original.Replace(".call 1 1", ".call 0 1", StringComparison.Ordinal)); + // The call's target is the link at its pc, so the forged connection is that link. + var entryCall = DirectLinks(original, 1).Single(call => call.Method == 0); + var changedCall = SplitExtractionComponents(ForgeLink(original, 0, entryCall.Pc, ".direct 0")); if (changedCall.Sources.Keys.Intersect(first.Sources.Keys).Count() != 2 || changedCall.InputHash == first.InputHash) throw new Exception("A changed callee connection retained the caller component or input identity."); + CheckCalleeIndexReuse(cli, assembly, directory); // A long method raises the emitted recursion depth; a component at another depth would not compile it. const string deepOption = "\nset_option maxRecDepth 8192\n"; var deepened = SplitExtractionComponents(original.Replace("\nset_option maxRecDepth 4096\n", deepOption, @@ -181,6 +190,54 @@ string Transcript(JsonElement stage) => File.ReadAllText(Path.Combine(trace, } } + // A helper whose callee sits at another index in a second extraction is not shared: only the header, which holds + // no program indices, is. The helper's own method number and the import of its callee's component change too, + // so its texts are also compared with both erased: they may differ only in the `.call` line and in the link + // definition that repeats the callee's index. + static void CheckCalleeIndexReuse(string[] cli, string assembly, string directory) + { + var requests = new[] { "ViaWrapped", "ViaBoth" }.Select(method => + new GeneratedExtraction(assembly, "Methods::" + method + "(ulong)", + Path.Combine(directory, method))).ToArray(); + ExtractBatch("extract", null, requests, cli[1]); + var texts = requests.Select(request => File.ReadAllText(Path.Combine(request.Output, "Extracted.lean"))) + .ToArray(); + var shared = SplitExtractionComponents(texts[0]).Sources.Keys + .Intersect(SplitExtractionComponents(texts[1]).Sources.Keys).ToArray(); + if (shared.Length != 1) + throw new Exception("Expected only the header to be shared when a helper's callee index differs, found " + + shared.Length + " shared components."); + // Wrapped is method 1 beside ViaWrapped and method 2 beside ViaBoth, and its callee Leaf moves from 2 to 3. + var differing = CalleeIndexDifferences(texts[0], texts[1], "Methods::Wrapped("); + // A call names its callee's method, which does not move; only the link holds the callee's index. + if (differing is not [("@[cil_code] def method#Link1 : Link := .direct 2", + "@[cil_code] def method#Link1 : Link := .direct 3")]) + throw new Exception("The helper's components must differ only in its link, found:\n" + + string.Join("\n", differing.Select(pair => pair.First + " | " + pair.Second))); + Console.WriteLine( + "PASS a helper whose callee moves differs only in its link and shares only the header"); + } + + /// + /// The lines of the component holding that differ between two extractions, once + /// imports are dropped and method numbers are erased, trimmed and in order. Both components must have as many + /// lines. + /// + static (string First, string Second)[] CalleeIndexDifferences(string first, string second, string signature) + { + string[] Lines(string text) => [.. SplitExtractionComponents(text).Sources.Values + .Single(source => source.Contains(signature, StringComparison.Ordinal)).Split('\n') + .Where(line => !line.StartsWith("import ", StringComparison.Ordinal)) + .Select(line => ComponentMethodNumber.Replace(line.Trim(), "method#"))]; + var (left, right) = (Lines(first), Lines(second)); + if (left.Length != right.Length) + throw new Exception($"The components of {signature} have {left.Length} and {right.Length} lines."); + return [.. left.Zip(right).Where(pair => pair.First != pair.Second)]; + } + + [GeneratedRegex(@"\bmethod[0-9]+")] + private static partial Regex ComponentMethodNumber { get; } + static void CheckConsumerCompilation(string[] cli, string package, string proof, string[] outputs) { var directory = Path.GetDirectoryName(package)!; diff --git a/Tests/ConsumerDeepChecks.cs b/Tests/ConsumerDeepChecks.cs new file mode 100644 index 000000000..b7d6b4b69 --- /dev/null +++ b/Tests/ConsumerDeepChecks.cs @@ -0,0 +1,681 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include ../scripts/ConsumerVerification.cs +#:include ../scripts/LeanDiagnostics.cs +#:include MultiAssemblyFixture.cs +#:include DeepImportChecks.cs + +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; + +long previousAllocated = 0; +foreach (int count in new[] { 64, 256, 1024 }) +{ + string[] paths = [.. Enumerable.Range(0, count).SelectMany(index => + new[] { $"Module{index}.olean", $"Module{index}.olean.private", $"Module{index}.olean.server" })]; + _ = ConsumerArtifactCounts(paths); + var allocated = GC.GetAllocatedBytesForCurrentThread(); + for (int repeat = 0; repeat < 32; repeat++) + { + var counts = ConsumerArtifactCounts(paths); + if (counts.Count != count || counts.Values.Any(parts => parts != 3)) + throw new InvalidOperationException("Module family index lost a companion."); + } + allocated = GC.GetAllocatedBytesForCurrentThread() - allocated; + if (previousAllocated > 0 && allocated > previousAllocated * 6) + throw new InvalidOperationException("Module family indexing allocates faster than linear growth."); + previousAllocated = allocated; + Console.WriteLine($"PASS {count} module families indexed 32 times, {allocated} allocated bytes"); + int manifestVisits = 0; + int keyVisits = 0; + IEnumerable> Manifests() + { + for (int index = 0; index < count; index++) + { + manifestVisits++; + yield return Keys(index); + } + } + IEnumerable Keys(int index) + { + keyVisits++; + yield return "Module" + index; + } + var modules = Enumerable.Range(0, count).ToDictionary(index => "Module" + index, index => "path" + index, + StringComparer.Ordinal); + var selected = ConsumerSupportModules(Manifests(), modules); + if (selected.Count != count || manifestVisits != count || keyVisits != count) + throw new InvalidOperationException("Portable support selection repeatedly scanned its manifests."); + Console.WriteLine($"PASS {count} portable manifests selected with {manifestVisits} manifest " + + $"and {keyVisits} key visits"); +} +if (args is ["--inventory"]) return; + +MultiAssemblyFixture fixture = new("ConsumerDeepChecks"); +var harmlessAncestor = Path.Combine(fixture.Root, "BindingAudit", "Consumer"); +Directory.CreateDirectory(Path.Combine(harmlessAncestor, "Client/BindingAudit")); +File.WriteAllText(Path.Combine(harmlessAncestor, "Client/BindingAudit/Helper.lean"), "def helper := 1\n"); +RequireConsumerDeepNamespaces([harmlessAncestor]); +var reserved = Path.Combine(harmlessAncestor, "StrictBinding.lean"); +File.WriteAllText(reserved, "def helper := 1\n"); +ExpectFailure(() => RequireConsumerDeepNamespaces([harmlessAncestor]), "reserved deep-check namespace"); +File.Delete(reserved); +Console.WriteLine("PASS namespace checks distinguish source roots from ancestors and nested namespaces"); +using Program verification = new(); +verification.EnsureExtractorBuilt(); +var image = fixture.Build("Input", "ConsumerDeepFixture", """ + public static class Input { public static ulong Value(ulong value) => value + 1; } + """); +var output = fixture.Artifact("extracted"); +Run([.. fixture.Cli, "extract", image, "Input::Value(System.UInt64)", output]); +var package = Path.Combine(fixture.Root, "Consumer"); +Directory.CreateDirectory(package); +var revision = Run(["git", "rev-parse", "HEAD"], Root).Trim(); +File.WriteAllText(Path.Combine(package, "lakefile.toml"), $""" + name = "deep_consumer" + [[require]] + name = "leancil" + git = {JsonSerializer.Serialize(Root.Replace('\\', '/'))} + rev = "{revision}" + """); +File.Copy(At("lean-toolchain"), Path.Combine(package, "lean-toolchain")); +var proof = Path.Combine(package, "Proof.lean"); +File.WriteAllText(proof, """ + import Extracted + import CIL.Safety.Execution + namespace ExampleProof + theorem correctness : + (CIL.Safety.invoke Extracted.program 8 0 [.scalar (.i64 2)] CIL.Safety.Memory.empty).map + Prod.snd = .ok [.scalar (.i64 3)] := rfl + #print axioms correctness + end ExampleProof + """); +Run(["lake", "update"], package); +var manifest = Path.Combine(fixture.Root, "images.sha256"); +File.WriteAllText(manifest, Sha(image) + " " + Path.GetRelativePath(fixture.Root, image) + "\n"); +var inputs = DeepCheckInputs.Read(manifest); +ConsumerRequest request = new(image, output, proof, "ExampleProof.correctness", null); +var selectionRun = new DeepCheckRun(inputs); +var invalidSelection = verification.VerifyConsumerRequests(package, + [request with { Proof = Path.Combine(fixture.Root, "Outside.lean") }], null, batch: false, + deepChecks: inputs, deepRun: selectionRun)[0]; +if (invalidSelection.Error is null || selectionRun.Attempt is not null) + throw new InvalidOperationException("Consumer selection failure began a deep attempt."); +var libraryRun = new DeepCheckRun(inputs); +ExpectFailure(() => verification.VerifyConsumer(image, proof, output, "ExampleProof.correctness", package, + null, stage => + { + if (stage != "Lean library build") return; + if (libraryRun.Attempt is null) throw new InvalidOperationException("Missing pre-library attempt."); + throw new InvalidOperationException("diagnostic library failure"); + }, inputs, libraryRun), "diagnostic library failure"); +RequireUnattributed(libraryRun); +Console.WriteLine("PASS consumer attempts begin after selection and before library work"); +ExpectFailure(() => verification.VerifyConsumerRequests(package, [request], null, batch: true, + deepChecks: inputs), "one ordinary"); +ExpectFailure(() => verification.VerifyConsumerRequests(package, [request], null, batch: false, + checkOnly: true, deepChecks: inputs), "one ordinary"); +ExpectFailure(() => verification.VerifyConsumerRequests(package, [request, request], null, batch: false, + deepChecks: inputs), "one ordinary"); + +var priorCache = Environment.GetEnvironmentVariable("LEANCIL_CACHE_DIR"); +var priorDisableCache = Environment.GetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE"); +Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", At("artifacts/ConsumerDeepChecksCache")); +Environment.SetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE", null); +try +{ + if (args is ["--replay-diagnostics"]) + { + foreach (bool mutateOriginal in new[] { false, true }) + { + var token = Guid.NewGuid().ToString("N"); + File.AppendAllText(proof, "\n-- " + token + "\n"); + var original = File.ReadAllText(proof); + var run = new DeepCheckRun(inputs); + SortedDictionary? before = null; + bool diagnosed = false; + try + { + ExpectFailure(() => Verify(verification, deepRun: run, stage: stage => + { + if (stage == "replay diagnostics") + { + diagnosed = true; + if (mutateOriginal) File.AppendAllText(proof, "\n-- changed during replay\n"); + } + if (stage != "deep-check sources") return; + var workspace = Directory.EnumerateDirectories(Path.GetTempPath(), "leancil-consumer-*") + .Single(directory => File.Exists(Path.Combine(directory, "LeanCILProof.lean")) && + File.ReadAllText(Path.Combine(directory, "LeanCILProof.lean")) + .Contains(token, StringComparison.Ordinal)); + var generated = Path.Combine(workspace, ".deep-checks/BindingAudit/Bodies.lean"); + var source = File.ReadAllText(generated); + var theorem = source.IndexOf("theorem bodies :", StringComparison.Ordinal); + var start = source.LastIndexOf("/--", theorem, StringComparison.Ordinal); + File.WriteAllText(generated, source[..start] + """ + set_option debug.skipKernelTC true in + run_cmd Lean.Elab.Command.liftCoreM <| Lean.addDecl (.thmDecl { + name := `BindingAudit.bodies + levelParams := [] + type := Lean.mkConst ``False + value := Lean.mkConst ``True.intro + }) + end BindingAudit + """); + before = Receipts(); + }), mutateOriginal ? "Inputs changed during consumer verification" : "Kernel replay rejected"); + } + finally { File.WriteAllText(proof, original); } + if (!diagnosed || before is null) throw new Exception("Consumer diagnostics were not reached"); + if (mutateOriginal) RequireUnattributed(run); + else + { + var failed = run.Attempt!.Failure("kernel replay", "rejected"); + if (failed.facts.Where(fact => fact.status == FailureReports.BindingFailureStatus.Failed) + .Select(fact => fact.id).Single() != "body-bytes" || + failed.images.Any(image => image.status == FailureReports.BindingFailureStatus.Failed)) + throw new Exception("Consumer replay attribution changed"); + } + RequireNoPublication(before); + Console.WriteLine("PASS consumer typed replay rejection; original mutation=" + mutateOriginal); + } + return; + } + if (args is ["--export-support"]) + { + CheckExportMutation(); + return; + } + if (args is ["--export-original"]) + { + CheckExportMutation(originalProofMutation: true); + return; + } + var ordinary = Verify(verification, deep: false); + if (ordinary.binding.mode != BindingMode.Default || verification.bindingPreparation is not null) + throw new InvalidOperationException("Ordinary consumer verification prepared deep checks."); + var ordinaryReceipts = ProofReceipts(); + if (ordinaryReceipts.Count == 0) throw new InvalidOperationException("Ordinary proof receipt was not published."); + foreach (var entry in DeepResults()) Directory.Delete(entry, true); + var deepHits = verification.deepCacheHits; + CheckDeep(Verify(verification)); + RequireOrdinaryReceipts(); + if (verification.deepCacheHits != deepHits || DeepResults().Length != 1) + throw new InvalidOperationException("A cold consumer deep request did not publish one deep result."); + RequireDeepImports(verification.deepImports, "consumer"); + Console.WriteLine("PASS consumer joint deep audit preserves ordinary result receipts"); + bool reusedAudit = false; + CheckDeep(Verify(verification, stage: stage => reusedAudit |= stage == "theorem audit")); + if (verification.deepCacheHits != deepHits + 1 || !reusedAudit) + throw new InvalidOperationException("An unchanged consumer deep request did not reuse its result."); + Console.WriteLine("PASS an unchanged consumer deep request reuses its result and runs the theorem audit"); + var beforeImportChange = Receipts(); + var importToken = Guid.NewGuid().ToString("N"); + File.AppendAllText(proof, "\n-- " + importToken + "\n"); + ExpectFailure(() => Verify(verification, stage: stage => + { + if (stage != "deep-check sources") return; + SameLengthChange(Path.Combine(ConsumerSession(importToken), + ".lake/packages/leancil_generated/.lake/build/lib/lean/Extracted.olean")); + }), "Deep-check artifact changed"); + RequireNoPublication(beforeImportChange); + if (verification.deepCacheHits != deepHits + 1) + throw new InvalidOperationException("A consumer deep result was restored over a changed imported module."); + Console.WriteLine("PASS a same-length consumer extraction change with its write time restored is not restored"); + var deepEntry = DeepResults().Single(); + var cachedBodies = Path.Combine(deepEntry, "files", "BindingAudit", "Bodies.olean"); + var forged = Path.Combine(fixture.Root, "ConsumerWrongBodies.olean"); + var forgeToken = Guid.NewGuid().ToString("N"); + File.AppendAllText(proof, "\n-- " + forgeToken + "\n"); + var beforeLateFailure = Receipts(); + // A changed generated source misses, so this cold request fails at its last check after build and replay. + ExpectFailure(() => Verify(verification, stage: stage => + { + if (stage is not ("deep-check sources" or "theorem audit")) return; + var generated = Path.Combine(ConsumerSession(forgeToken), ".deep-checks", "BindingAudit", "Bodies.lean"); + if (stage == "deep-check sources") + { + var source = File.ReadAllText(generated); + var start = source.IndexOf("theorem bodies :", StringComparison.Ordinal); + if (start < 0) throw new InvalidOperationException("Missing generated bodies theorem."); + File.WriteAllText(generated, source[..start] + "theorem bodies : StrictBinding.BodiesBound images [] := " + + "StrictBinding.claimsHold_nil Bodies.prepared\n#print axioms bodies\nend BindingAudit\n"); + } + if (stage == "theorem audit" && !File.Exists(forged)) + File.Copy(Path.ChangeExtension(generated, ".olean"), forged); + }), "Theorem statement mismatch"); + if (!File.Exists(forged)) throw new InvalidOperationException("The forged body module was not built."); + RequireNoPublication(beforeLateFailure); + if (verification.deepCacheHits != deepHits + 1 || DeepResults().Single() != deepEntry) + throw new InvalidOperationException("A cold consumer deep request that failed late published a result."); + Console.WriteLine("PASS a cold consumer deep request rejected by its theorem audit publishes nothing"); + File.Copy(forged, cachedBodies, true); + var deepManifest = JsonSerializer.SerializeToUtf8Bytes(CacheFiles(Path.Combine(deepEntry, "files"))); + File.WriteAllBytes(Path.Combine(deepEntry, "receipt.json"), deepManifest); + File.WriteAllBytes(Path.Combine(deepEntry, "receipt.hmac"), HMACSHA256.HashData(verification.ReceiptKey, + Encoding.UTF8.GetBytes(Path.GetFileName(deepEntry)).Concat(deepManifest).ToArray())); + var beforeForged = Receipts(); + ExpectFailure(() => Verify(verification), "Theorem statement mismatch"); + RequireNoPublication(beforeForged); + if (verification.deepCacheHits != deepHits + 2) + throw new InvalidOperationException("The forged consumer deep result was not restored."); + Console.WriteLine("PASS the consumer theorem audit rejects an authenticated restored module stating another fact"); + // A same-length change with its timestamp restored must fail the receipt's content hashes. + SameLengthChange(cachedBodies); + var changedBodies = File.ReadAllBytes(cachedBodies); + // A source changed after the result is staged fails the publication check, so the staged copy is discarded. + var publicationToken = Guid.NewGuid().ToString("N"); + File.AppendAllText(proof, "\n-- " + publicationToken + "\n"); + var beforePublication = Receipts(); + bool publicationReached = false; + ExpectFailure(() => Verify(verification, stage: stage => + { + if (stage != "deep result publication") return; + publicationReached = true; + File.AppendAllText(Path.Combine(ConsumerSession(publicationToken), ".deep-checks", "BindingAudit", + "Bodies.lean"), "\n"); + }), "Deep-check source changed"); + if (!publicationReached) throw new InvalidOperationException("The deep result publication stage was not reached."); + RequireNoPublication(beforePublication); + if (verification.deepCacheHits != deepHits + 2 || DeepResults().Single() != deepEntry || + !File.ReadAllBytes(cachedBodies).SequenceEqual(changedBodies)) + throw new InvalidOperationException("A staged consumer deep result was published after a failed check."); + Console.WriteLine("PASS a staged consumer deep result is discarded when a later stability check fails"); + CheckDeep(Verify(verification)); + if (verification.deepCacheHits != deepHits + 2 || verification.AuthenticatedSuccess(Path.GetFileName(deepEntry)) + is null || File.ReadAllBytes(cachedBodies).SequenceEqual(changedBodies)) + throw new InvalidOperationException("A rebuilt consumer deep result did not replace the changed entry."); + Console.WriteLine("PASS a changed cached consumer deep module runs cold and its rebuilt result replaces " + + "the entry"); + if (args is ["--positive"]) return; + var combinedRun = new DeepCheckRun(inputs); + var combinedOriginalProof = File.ReadAllBytes(proof); + var combinedToken = Guid.NewGuid().ToString("N"); + File.AppendAllText(proof, "\n-- " + combinedToken + "\n"); + bool combinedMutation = false; + bool originalMutation = false; + SortedDictionary? combinedReceipts = null; + try + { + ExpectFailure(() => Verify(verification, stage: stage => + { + if (stage == "theorem audit" && !originalMutation) + { + File.AppendAllText(proof, "\n-- concurrent original proof edit\n"); + originalMutation = true; + } + if (stage != "deep-check sources") return; + var workspace = Directory.EnumerateDirectories(Path.GetTempPath(), "leancil-consumer-*") + .Single(directory => File.Exists(Path.Combine(directory, "LeanCILProof.lean")) && + File.ReadAllText(Path.Combine(directory, "LeanCILProof.lean")) + .Contains(combinedToken, StringComparison.Ordinal)); + var generated = Path.Combine(workspace, ".deep-checks/BindingAudit/Bodies.lean"); + var source = File.ReadAllText(generated); + var start = source.IndexOf("theorem bodies :", StringComparison.Ordinal); + if (start < 0) throw new InvalidOperationException("Missing generated bodies theorem."); + File.WriteAllText(generated, source[..start] + "theorem bodies : True := True.intro\n" + + "#print axioms bodies\nend BindingAudit\n"); + combinedMutation = true; + combinedReceipts = Receipts(); + }, deepRun: combinedRun), "Inputs changed during consumer verification"); + } + finally { File.WriteAllBytes(proof, combinedOriginalProof); } + if (!combinedMutation || !originalMutation || combinedReceipts is null) + throw new InvalidOperationException("Missing combined consumer mutation."); + RequireUnattributed(combinedRun); + RequireNoPublication(combinedReceipts); + Console.WriteLine("PASS consumer original-input mutation precedes named rejection attribution"); + if (args is ["--diagnostics"]) return; + // Without a deep result the fallback must replay the generated modules themselves. + foreach (var entry in DeepResults()) Directory.Delete(entry, true); + using (Program restored = new()) + { + var replay = Environment.GetEnvironmentVariable("LEANCIL_PROFILE_CONSUMER_REPLAY"); + var previousTrace = Environment.GetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY"); + var trace = Path.Combine(fixture.Root, "FallbackTrace"); + var previousOutput = Console.Out; + using var captured = new StringWriter(); + try + { + Environment.SetEnvironmentVariable("LEANCIL_PROFILE_CONSUMER_REPLAY", "true"); + Environment.SetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY", trace); + Console.SetOut(captured); + CheckDeep(Verify(restored)); + } + finally + { + Environment.SetEnvironmentVariable("LEANCIL_PROFILE_CONSUMER_REPLAY", replay); + Environment.SetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY", previousTrace); + Console.SetOut(previousOutput); + Console.Write(captured.ToString()); + } + if (!captured.ToString().Contains("CACHE certified consumer extraction preparation", StringComparison.Ordinal)) + throw new InvalidOperationException( + "Fresh deep consumer did not restore authenticated extraction support."); + var records = File.ReadLines(Path.Combine(trace, TraceFile)).Select(line => + JsonSerializer.Deserialize(line)).ToArray(); + var fallback = records.Single(record => record.GetProperty("stage").GetString() == + "Profiled kernel replay of consumer modules"); + if (fallback.GetProperty("exitCode").GetInt32() != 0 || + !File.ReadAllText(Path.Combine(trace, fallback.GetProperty("transcript").GetString()!)) + .Contains("BindingAudit.Bodies", StringComparison.Ordinal)) + throw new InvalidOperationException("Fallback did not replay the body-check module successfully."); + if (restored.deepCacheHits != 0) + throw new InvalidOperationException("A consumer without a deep result restored one."); + } + RequireOrdinaryReceipts(); + Console.WriteLine("PASS fresh consumer with restored support and no deep result runs fallback deep audit"); + using (Program viewed = new() { viewsStatement = true }) + { + CheckDeep(Verify(viewed)); + if (viewed.VerifiedStatement is null) + throw new InvalidOperationException("Consumer deep verification lost the statement view."); + } + Console.WriteLine("PASS consumer deep statement view completes before publication"); + var originalProof = File.ReadAllText(proof); + var beforeAxiom = Receipts(); + try + { + File.WriteAllText(proof, originalProof + .Replace("namespace ExampleProof", "namespace ExampleProof\naxiom uncheckedCorrectness : False", + StringComparison.Ordinal) + .Replace(" := rfl", " := False.elim uncheckedCorrectness", StringComparison.Ordinal)); + ExpectFailure(() => Verify(verification), "Unapproved proof axioms"); + RequireNoPublication(beforeAxiom); + } + finally { File.WriteAllText(proof, originalProof); } + Console.WriteLine("PASS consumer deep joint audit rejects an unapproved proof axiom"); + var beforeInvalidDeclaration = Receipts(); + try + { + File.WriteAllText(proof, "import Lean\n" + originalProof + """ + + open Lean Elab Command in + run_cmd do + let environment <- getEnv + let environment <- IO.ofExcept <| (environment.addDeclCore 0 0 (.thmDecl { + name := `ConsumerInvalid.unrelated + levelParams := [] + type := Lean.mkConst ``False + value := Lean.mkConst ``True.intro + }) none false).mapError (fun _ => "Could not construct rejection fixture") + setEnv environment + """); + ExpectFailure(() => Verify(verification), "Kernel replay rejected LeanCILProof"); + RequireNoPublication(beforeInvalidDeclaration); + } + finally { File.WriteAllText(proof, originalProof); } + Console.WriteLine("PASS consumer deep replay rejects an invalid unrelated declaration"); + Reject("source", "Deep-check source changed", "theorem audit", session => + File.AppendAllText(Path.Combine(session, ".deep-checks/BindingAudit/Bodies.lean"), "\n-- changed\n")); + Reject("companion", "artifact", "theorem audit", session => + File.WriteAllBytes(Path.Combine(session, ".deep-checks/BindingAudit/Bodies.olean.unexpected"), [0])); + Reject("extraction", "Compiled artifacts changed", "deep checks", session => + File.WriteAllBytes(Path.Combine(session, + ".lake/packages/leancil_generated/.lake/build/lib/lean/Extracted.olean.unexpected"), [0])); + Reject("foreign extraction", "Compiled artifacts changed", "deep checks", session => + File.Copy(Path.Combine(verification.library!, ".lake/build/lib/lean/CIL.olean"), Path.Combine(session, + ".lake/packages/leancil_generated/.lake/build/lib/lean/Extracted.olean"), true)); + Reject("prepared binding", "prepared StrictBinding library changed", "kernel replay", _ => + File.WriteAllBytes(Path.Combine(verification.bindingSession!, + ".lake/build/lib/lean/StrictBinding.olean.unexpected"), [0])); + File.Delete(Path.Combine(verification.bindingSession!, ".lake/build/lib/lean/StrictBinding.olean.unexpected")); + verification.fullImportAudit = (_, session, _, _) => + { + File.AppendAllText(Path.Combine(session, ".deep-checks/BindingAudit/Images.lean"), "\n-- changed\n"); + return new("composition", "", [], []); + }; + var receipts = Receipts(); + try { ExpectFailure(() => Verify(verification), "Deep-check source changed"); } + finally { verification.fullImportAudit = null; } + RequireNoPublication(receipts); + Console.WriteLine("PASS full-import callback mutation leaves no new success receipt or report"); + var viewModule = Path.Combine(package, "ViewEffects.lean"); + var viewMarker = Path.Combine(fixture.Root, "view-mutation.txt"); + var configuration = Path.Combine(package, "lakefile.toml"); + var originalConfiguration = File.ReadAllText(configuration); + originalProof = File.ReadAllText(proof); + File.WriteAllText(viewModule, $$""" + import Lean + initialize do + let current <- IO.currentDir + if current.toString.endsWith "certified-result-0" then + IO.FS.writeFile (current / "../.deep-checks/BindingAudit/Unexpected.lean") "-- changed\n" + IO.FS.writeFile {{JsonSerializer.Serialize(viewMarker.Replace('\\', '/'))}} "mutated" + """); + File.AppendAllText(configuration, "\n[[lean_lib]]\nname = \"ViewEffects\"\n"); + File.WriteAllText(proof, "import ViewEffects\n" + originalProof); + receipts = Receipts(); + try + { + using Program viewed = new() { viewsStatement = true }; + ExpectFailure(() => Verify(viewed), "Deep-check source changed"); + if (!File.Exists(viewMarker)) throw new InvalidOperationException("Statement view mutation did not execute."); + RequireNoPublication(receipts); + } + finally + { + File.WriteAllText(proof, originalProof); + File.WriteAllText(configuration, originalConfiguration); + File.Delete(viewModule); + } + Console.WriteLine("PASS statement-view mutation precedes every successful support publication"); + + var portableModule = Path.Combine(package, "PortableSupport.lean"); + // Normalization receipts persist in this test's cache, so only a module new to this run must be serialized. + File.WriteAllText(portableModule, "theorem portableSupport : True := True.intro\n#print axioms portableSupport\n" + + "def portableToken : String := \"" + Guid.NewGuid().ToString("N") + "\"\n"); + File.AppendAllText(configuration, "\n[[lean_lib]]\nname = \"PortableSupport\"\n"); + File.WriteAllText(proof, "import PortableSupport\n" + originalProof); + var portableVariables = new[] { "LEANCIL_EXPORT_CONSUMER_SUPPORT", "LEANCIL_CONSUMER_SUPPORT", + "LEANCIL_CONSUMER_SUPPORT_SHA256", "LEANCIL_TRACE_DIRECTORY", "LEANCIL_DISABLE_SUCCESS_CACHE" } + .ToDictionary(name => name, Environment.GetEnvironmentVariable); + var portableDirectory = Path.Combine(fixture.Root, "PortableSupport"); + try + { + Environment.SetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE", "true"); + Environment.SetEnvironmentVariable("LEANCIL_EXPORT_CONSUMER_SUPPORT", portableDirectory); + Verify(verification, deep: false); + var portableManifest = Directory.GetFiles(portableDirectory, "*.json").Single(); + Environment.SetEnvironmentVariable("LEANCIL_EXPORT_CONSUMER_SUPPORT", null); + Environment.SetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE", null); + Environment.SetEnvironmentVariable("LEANCIL_CONSUMER_SUPPORT", portableManifest); + Environment.SetEnvironmentVariable("LEANCIL_CONSUMER_SUPPORT_SHA256", Sha(portableManifest)); + var trace = Path.Combine(fixture.Root, "PortableTrace"); + Environment.SetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY", trace); + var beforePortable = Receipts(); + bool reachedAudit = false; + ExpectFailure(() => Verify(verification, stage: stage => + { + if (stage != "theorem audit") return; + reachedAudit = true; + throw new InvalidDataException("Terminal portable deep rejection"); + }), "Terminal portable deep rejection"); + if (!reachedAudit) throw new InvalidOperationException("Portable deep rejection did not reach the audit."); + RequireNoPublication(beforePortable); + var fingerprintTrace = File.ReadLines(Path.Combine(trace, TraceFile)) + .Select(line => JsonSerializer.Deserialize(line)) + .Single(record => record.GetProperty("stage").GetString() == "Canonical module fingerprints"); + var fingerprintCommand = fingerprintTrace.GetProperty("command").EnumerateArray() + .Select(value => value.GetString()).ToArray(); + var serializedIndex = Array.IndexOf(fingerprintCommand, "serialized"); + if (serializedIndex < 0 || !int.TryParse(fingerprintCommand[serializedIndex + 1], out var serialized) || + serialized == 0) + throw new InvalidOperationException("Portable deep verification did not normalize imported support."); + var fingerprintsBefore = Directory.EnumerateFiles(SuccessCacheRoot, "fingerprints.json", + SearchOption.AllDirectories).ToHashSet(StringComparer.Ordinal); + var portableReport = Verify(verification, deep: false); + if (portableReport.consumerSupportSha256 != Sha(portableManifest) || + !Directory.EnumerateFiles(SuccessCacheRoot, "fingerprints.json", SearchOption.AllDirectories) + .Any(path => !fingerprintsBefore.Contains(path))) + throw new InvalidOperationException("Ordinary portable control did not publish a normalization receipt."); + Console.WriteLine("PASS rejected deep portable support publishes no normalization receipt; " + + "ordinary control does"); + } + finally + { + foreach (var (name, value) in portableVariables) Environment.SetEnvironmentVariable(name, value); + File.WriteAllText(proof, originalProof); + File.WriteAllText(configuration, originalConfiguration); + File.Delete(portableModule); + } + CheckExportMutation(); + CheckExportMutation(originalProofMutation: true); + + void RequireOrdinaryReceipts() + { + if (!Same(ordinaryReceipts, ProofReceipts())) + throw new InvalidOperationException("Deep consumer verification changed ordinary proof receipts."); + } +} +finally +{ + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", priorCache); + Environment.SetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE", priorDisableCache); +} + +Report Verify(Program program, bool deep = true, Action? stage = null, DeepCheckRun? deepRun = null) => + program.VerifyConsumer(image, proof, output, "ExampleProof.correctness", package, null, stage, + deep ? inputs : null, deepRun); + +void CheckExportMutation(bool originalProofMutation = false) +{ + var mutationName = originalProofMutation ? "original-proof" : "deep-source"; + var export = Path.Combine(fixture.Root, "MutatedExport-" + mutationName); + var trace = Path.Combine(fixture.Root, "ExportTrace-" + mutationName); + var marker = Path.Combine(fixture.Root, "normalization-mutation-" + mutationName + ".txt"); + var configuration = Path.Combine(package, "lakefile.toml"); + var originalConfiguration = File.ReadAllText(configuration); + var originalProof = File.ReadAllText(proof); + var token = Guid.NewGuid().ToString("N"); + var source = Path.Combine(package, "ExportSupport.lean"); + File.WriteAllText(source, "def exportSupport : String := \"" + token + "\"\n"); + File.AppendAllText(configuration, "\n[[lean_lib]]\nname = \"ExportSupport\"\n"); + File.WriteAllText(proof, "import ExportSupport\n" + originalProof + "\n-- " + token + "\n"); + var variables = new[] { "LEANCIL_EXPORT_CONSUMER_SUPPORT", "LEANCIL_CONSUMER_SUPPORT", + "LEANCIL_CONSUMER_SUPPORT_SHA256", "LEANCIL_TRACE_DIRECTORY" } + .ToDictionary(name => name, Environment.GetEnvironmentVariable); + try + { + Environment.SetEnvironmentVariable("LEANCIL_EXPORT_CONSUMER_SUPPORT", export); + Environment.SetEnvironmentVariable("LEANCIL_CONSUMER_SUPPORT", null); + Environment.SetEnvironmentVariable("LEANCIL_CONSUMER_SUPPORT_SHA256", null); + Environment.SetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY", trace); + var receipts = Receipts(); + ExpectFailure(() => Verify(verification, stage: stage => + { + if (stage != "support normalization") return; + var session = Directory.EnumerateDirectories(Path.GetTempPath(), "leancil-consumer-*") + .Single(candidate => File.Exists(Path.Combine(candidate, "LeanCILProof.lean")) && + File.ReadAllText(Path.Combine(candidate, "LeanCILProof.lean")) + .Contains(token, StringComparison.Ordinal)); + var target = originalProofMutation ? proof : + Path.Combine(session, ".deep-checks/BindingAudit/Bodies.lean"); + File.AppendAllText(target, "\n-- changed\n"); + File.WriteAllText(marker, "mutated"); + }), originalProofMutation ? "Inputs changed during consumer verification" : "Deep-check source changed"); + var normalization = File.ReadLines(Path.Combine(trace, TraceFile)) + .Select(line => JsonSerializer.Deserialize(line)) + .Single(record => record.GetProperty("stage").GetString() == "Canonical module fingerprints"); + var command = normalization.GetProperty("command").EnumerateArray() + .Select(value => value.GetString()).ToArray(); + var serializedIndex = Array.IndexOf(command, "serialized"); + if (!File.Exists(marker) || serializedIndex < 0 || + !int.TryParse(command[serializedIndex + 1], out var serialized) || serialized == 0) + throw new InvalidOperationException("Export mutation did not follow actual module normalization."); + RequireNoPublication(receipts); + if (Directory.Exists(export) && Directory.EnumerateFiles(export, "*.json").Any()) + throw new InvalidOperationException("Rejected deep export published a support manifest."); + Console.WriteLine("PASS deep export " + mutationName + + " normalization mutation leaves no receipts, manifest or verified report"); + } + finally + { + foreach (var (name, value) in variables) Environment.SetEnvironmentVariable(name, value); + File.WriteAllText(configuration, originalConfiguration); + File.WriteAllText(proof, originalProof); + File.Delete(source); + } +} + +static void CheckDeep(Report report) +{ + BindingFact[] audited = [BindingFact.BodyBytes, BindingFact.MetadataRows, BindingFact.StaticData]; + if (report.binding.mode != BindingMode.Deep || report.binding.facts.Length != 12 || + report.binding.facts.Any(fact => fact.status != + (audited.Contains(fact.id) ? BindingStatus.Reconfirmed : BindingStatus.NotCovered))) + throw new InvalidOperationException("Consumer deep report has incorrect binding coverage."); +} + +SortedDictionary Receipts() => Directory.Exists(SuccessCacheRoot) + ? HashFiles(Directory.EnumerateFiles(SuccessCacheRoot, "receipt.*", SearchOption.AllDirectories) + .Select(path => (path, path))) : new(StringComparer.Ordinal); + +// The consumer session whose staged proof carries `token`. +static string ConsumerSession(string token) => Directory.EnumerateDirectories(Path.GetTempPath(), "leancil-consumer-*") + .Single(directory => File.Exists(Path.Combine(directory, "LeanCILProof.lean")) && + File.ReadAllText(Path.Combine(directory, "LeanCILProof.lean")).Contains(token, StringComparison.Ordinal)); + +// Published deep results, the only entries that hold generated binding modules. +string[] DeepResults() => Directory.Exists(SuccessCacheRoot) + ? [.. Directory.EnumerateDirectories(SuccessCacheRoot) + .Where(entry => Directory.Exists(Path.Combine(entry, "files", "BindingAudit")))] : []; + +SortedDictionary ProofReceipts() => Directory.Exists(SuccessCacheRoot) + ? HashFiles(Directory.EnumerateFiles(SuccessCacheRoot, "report.json", SearchOption.AllDirectories) + .SelectMany(path => new[] { path, + Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(path))!, "receipt.json"), + Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(path))!, "receipt.hmac") }) + .Select(path => (path, path))) : new(StringComparer.Ordinal); + +void RequireNoPublication(SortedDictionary receipts) +{ + if (File.Exists(Path.Combine(output, "verified.json")) || !Same(receipts, Receipts())) + throw new InvalidOperationException("Rejected deep verification published a success receipt or report."); +} + +void Reject(string name, string diagnostic, string atStage, Action mutation) +{ + var token = Guid.NewGuid().ToString("N"); + File.AppendAllText(proof, "\n-- " + token + "\n"); + bool mutated = false; + var run = new DeepCheckRun(inputs); + SortedDictionary? receipts = null; + ExpectFailure(() => Verify(verification, deepRun: run, stage: stage => + { + if (stage != atStage || mutated) return; + var session = Directory.EnumerateDirectories(Path.GetTempPath(), "leancil-consumer-*") + .Single(directory => File.Exists(Path.Combine(directory, "LeanCILProof.lean")) && + File.ReadAllText(Path.Combine(directory, "LeanCILProof.lean")) + .Contains(token, StringComparison.Ordinal)); + receipts = Receipts(); + mutation(session); + mutated = true; + }), diagnostic); + if (!mutated || receipts is null) throw new InvalidOperationException("Mutation was not exercised: " + name); + RequireUnattributed(run); + RequireNoPublication(receipts); + Console.WriteLine("PASS consumer deep " + name + " mutation leaves no receipt or report"); +} + +static void RequireUnattributed(DeepCheckRun run) +{ + var failure = run.Attempt?.Failure("verification", "rejected") + ?? throw new InvalidOperationException("Missing consumer deep attempt."); + if (failure.facts.Any(fact => fact.status != FailureReports.BindingFailureStatus.NotCovered) || + failure.images.Any(item => item.status != FailureReports.BindingFailureStatus.NotCovered)) + throw new InvalidOperationException("An unclassified consumer failure marked a rejected subject."); +} + +static void ExpectFailure(Action action, string diagnostic) +{ + try { action(); } + catch (Exception error) when (error.Message.Contains(diagnostic, StringComparison.Ordinal)) { return; } + throw new InvalidOperationException("Expected rejection containing " + diagnostic); +} diff --git a/Tests/ConsumerImportDirectories.cs b/Tests/ConsumerImportDirectories.cs new file mode 100644 index 000000000..680dece53 --- /dev/null +++ b/Tests/ConsumerImportDirectories.cs @@ -0,0 +1,106 @@ +partial class Program +{ + static void CheckConsumerImportDirectories(string[] args) + { + var root = Path.Combine(Path.GetTempPath(), "leancil-import-directories-" + Guid.NewGuid().ToString("N")); + var physical = Path.Combine(root, "physical"); + var alias = Path.Combine(root, "alias"); + var session = Path.Combine(alias, "session"); + var physicalSession = Path.Combine(physical, "session"); + var trusted = Path.Combine(alias, "toolchain"); + var certified = Path.Combine(alias, "certified"); + Directory.CreateDirectory(physicalSession); + Directory.CreateDirectory(Path.Combine(physical, "toolchain")); + Directory.CreateDirectory(Path.Combine(physical, "certified")); + Directory.CreateDirectory(Path.Combine(physical, "session-sibling")); + var links = new List(); + void Link(string path, string target) + { + Directory.CreateSymbolicLink(path, target); + links.Add(path); + } + try + { + Link(alias, "physical"); + string[] Validate(params string[] paths) => + ValidatedConsumerImportDirectories( + session, trusted, certified, string.Join(Path.PathSeparator, paths)); + void Require(string[] actual, params string[] expected) + { + if (!actual.SequenceEqual(expected)) + throw new InvalidOperationException("Unexpected import directories: " + string.Join(", ", actual)); + } + void Reject(string path) + { + try { Validate(path); } + catch (InvalidDataException) { return; } + throw new InvalidOperationException("Accepted outside or unresolved import directory: " + path); + } + var build = Path.Combine(".lake", "build", "lib", "lean"); + Require(Validate(build), Path.Combine(session, build)); + Require(Validate(Path.Combine(physicalSession, build), Path.Combine(session, build)), + Path.Combine(session, build)); + Require(Validate(trusted, Path.Combine(physical, "toolchain"), certified, + Path.Combine(physical, "certified")), certified); + var supportSource = Path.Combine(physicalSession, "Support.lean"); + var proofSource = Path.Combine(physicalSession, "Proof.lean"); + File.WriteAllText(supportSource, "theorem support : True := True.intro\n"); + File.WriteAllText(proofSource, "import Support\n"); + Require(ConsumerSupportCandidates([proofSource], [session, physicalSession], [], trusted), + "Support"); + var otherSource = Path.Combine(physical, "session-sibling", "Support.lean"); + File.WriteAllText(otherSource, "theorem other : True := True.intro\n"); + Require(ConsumerSupportCandidates([proofSource], + [session, physicalSession, Path.GetDirectoryName(otherSource)!], [], trusted)); + var nested = Path.Combine(physicalSession, "nested"); + Directory.CreateDirectory(nested); + Link(Path.Combine(physicalSession, "inside"), nested); + Require(Validate(Path.Combine(session, "inside", "..", build)), Path.Combine(session, build)); + Reject(Path.Combine(physical, "session-sibling")); + Reject(Path.Combine("..", "session-sibling")); + Reject(Path.Combine(trusted, "outside")); + Reject(Path.Combine(certified, "outside")); + Link(Path.Combine(physicalSession, "escape"), Path.Combine(physical, "session-sibling")); + Reject(Path.Combine(session, "escape")); + // Parent traversal follows the symbolic link before resolving its parent directory. + Reject(Path.Combine(session, "escape", "..", "certified", "outside")); + Link(Path.Combine(physicalSession, "cycle"), Path.Combine(physicalSession, "cycle")); + Reject(Path.Combine(session, "cycle")); + Link(Path.Combine(physicalSession, "dangling"), Path.Combine(physical, "missing")); + Reject(Path.Combine(session, "dangling")); + var module = Path.Combine(physicalSession, "Proof.olean"); + File.WriteAllText(module, "artifact"); + AuditedOleans audited = new(new() { ["Proof"] = module }, [], []); + RequireAuditedModule(audited, "Proof", Path.Combine(session, "Proof.olean")); + RequireAuditedModule(audited, "Proof", Path.Combine(session, "inside", "..", "Proof.olean")); + void RejectArtifact(string expected) + { + try { RequireAuditedModule(audited, "Proof", expected); } + catch (InvalidDataException) { return; } + throw new InvalidOperationException("Accepted different audited artifact: " + expected); + } + var siblingModule = Path.Combine(physical, "session-sibling", "Proof.olean"); + File.WriteAllText(siblingModule, "artifact"); + RejectArtifact(siblingModule); + var renamedModule = Path.Combine(physicalSession, "Other.olean"); + File.WriteAllText(renamedModule, "artifact"); + RejectArtifact(renamedModule); + try + { + RequireAuditedModule(audited, "Missing", module); + throw new InvalidOperationException("Accepted missing audited module."); + } + catch (KeyNotFoundException) { } + Console.WriteLine("PASS consumer import directory aliases and containment"); + } + finally + { + foreach (var link in links.AsEnumerable().Reverse()) + { + if (OperatingSystem.IsWindows()) Directory.Delete(link); + else File.Delete(link); + } + Directory.Delete(root, true); + } + } +} diff --git a/Tests/ConsumerInspection.cs b/Tests/ConsumerInspection.cs index 0c4924757..ab91f841b 100644 --- a/Tests/ConsumerInspection.cs +++ b/Tests/ConsumerInspection.cs @@ -1,6 +1,10 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs #:include ../scripts/LibraryAudit.cs diff --git a/Tests/ConsumerPackagePaths.cs b/Tests/ConsumerPackagePaths.cs new file mode 100644 index 000000000..e87e66d18 --- /dev/null +++ b/Tests/ConsumerPackagePaths.cs @@ -0,0 +1,282 @@ +partial class Program +{ + static void CheckConsumerPackagePaths(string[] args) + { + var work = Path.Combine(Path.GetTempPath(), "leancil-package-paths-" + Guid.NewGuid().ToString("N")); + var real = Path.Combine(work, "real"); + var package = Path.Combine(real, "package"); + var outside = Path.Combine(work, "outside"); + var built = Path.Combine(package, ".lake", "packages", "generated", "lib"); + Directory.CreateDirectory(built); + Directory.CreateDirectory(outside); + File.WriteAllText(Path.Combine(outside, "Secret.lean"), "-- outside the package\n"); + void Require(bool condition, string message) + { + if (!condition) throw new Exception(message); + } + void Reject(string root, string path, string route) + { + try { Within(root, path); } + catch (InvalidDataException) { return; } + throw new Exception($"A path that leaves its package through {route} was accepted: {path}"); + } + try + { + Reject(package, "../outside/Secret.lean", ".."); + Reject(package, ".lake/../../../outside", ".."); + Reject(package, "..", ".."); + Reject(package, Path.Combine(outside, "Secret.lean"), "an absolute path"); + Reject(package, outside, "an absolute path"); + Require(Within(package, "Missing/Module.lean") == Path.Combine(package, "Missing", "Module.lean") && + Within(package, ".") == package, "A path inside its package was not returned as spelled under it."); + Console.WriteLine("PASS a package path that leaves through .. or an absolute path is rejected"); + + // A case variant is one location only where the file system says so; no sibling exists to confuse it. + var variant = Path.Combine(real, "PACKAGE"); + var sameUnderCase = Directory.Exists(variant); + Require(SameLocation(package, variant) == sameUnderCase && + Inside(package, Path.Combine(variant, ".lake")) == sameUnderCase && + new[] { package, variant }.Select(ResolvedPath).Distinct(LocationComparer).Count() == + (sameUnderCase ? 1 : 2), + $"A case variant of a directory was judged {(sameUnderCase ? "another" : "the same")} location."); + Console.WriteLine("PASS a case variant of a directory is " + + (sameUnderCase ? "the same location" : "another location")); + Require(NameCaseIn(real) == (sameUnderCase ? NameCase.Insensitive : NameCase.Sensitive) && + NameCaseIn(Path.Combine(real, "missing", "deeper")) == NameCaseIn(real), + "The probe read another case rule than the temporary volume follows."); + // macOS reports the case rule of every volume; Windows and Linux report only a flag that decides it. + var queried = QueriedNameCase(real); + Require(queried == NameCaseIn(real) || queried is null && !OperatingSystem.IsMacOS(), + $"The operating system reported {queried} for the temporary volume."); + Console.WriteLine("PASS the probe reads the case rule of the temporary volume"); + + CheckNameCaseRule(work, Require); + CheckCaseSensitiveVolume(Path.Combine(work, "sensitive"), Require, Reject); + + // Lake reports its directories with links followed, as macOS reaches /var through /private/var. + var link = Path.Combine(work, "link"); + if (!TryLinkDirectory(link, real)) + { + // Every Unix user can make a symbolic link, so only Windows may lack both kinds of link. + if (!OperatingSystem.IsWindows()) throw new Exception("Could not make a directory symbolic link."); + Console.WriteLine("SKIP package paths through links: no directory link or junction could be made"); + return; + } + var linkedPackage = Path.Combine(link, "package"); + var linkedBuilt = Path.Combine(linkedPackage, ".lake", "packages", "generated", "lib"); + Require(Within(linkedPackage, Path.GetRelativePath(linkedPackage, built)) == linkedBuilt && + Within(linkedPackage, built) == linkedBuilt && Within(package, linkedBuilt) == built, + "A package reached through a directory link rejected or respelled its own directory."); + var builtRelative = Path.Combine(".lake", "packages", "generated", "lib"); + Require(ResolvedRelativePath(linkedPackage, built) == builtRelative, + "A directory reached through a link was not found inside its package."); + Reject(linkedPackage, Path.GetRelativePath(linkedPackage, outside), ".."); + Reject(linkedPackage, Path.Combine(link, "..", "outside"), "an absolute path"); + Console.WriteLine("PASS a package reached through a directory link holds its own directories"); + + // Lean reports an audited module with links followed; the check still names one file. + var olean = Path.Combine(built, "Proof.olean"); + var shadow = Path.Combine(outside, "Proof.olean"); + File.WriteAllText(olean, ""); + File.WriteAllText(shadow, ""); + AuditedOleans Audited(string path) => new(new() { ["Proof"] = path }, [], []); + RequireAuditedModule(Audited(olean), "Proof", Path.Combine(linkedBuilt, "Proof.olean")); + RequireAuditedModule(Audited(Path.Combine(linkedBuilt, "Proof.olean")), "Proof", olean); + try + { + RequireAuditedModule(Audited(shadow), "Proof", Path.Combine(linkedBuilt, "Proof.olean")); + throw new Exception("An audited module elsewhere was taken for the checked one."); + } + catch (InvalidDataException) { } + Console.WriteLine("PASS an audited module reached through a link is the checked file; a shadow is not"); + + // Guards that keep an output out of an input decide on the location, not the spelling. + Require(Inside(linkedPackage, olean) && Inside(package, Path.Combine(linkedBuilt, "Proof.olean")) && + !Inside(linkedPackage, shadow) && SameLocation(linkedPackage, package) && + !SameLocation(linkedBuilt, outside), "A location reached through a link was judged by its spelling."); + Require(CompiledName([ResolvedPath(linkedBuilt)], ResolvedPath(olean)) == "Proof.olean" && + CompiledName([ResolvedPath(linkedBuilt)], ResolvedPath(shadow)) is null && + CompiledName([ResolvedPath(linkedBuilt)], ResolvedPath(linkedBuilt)) is null, + "A compiled module reached through a link was not named, or a file elsewhere was."); + Console.WriteLine("PASS containment and identity of locations reached through a link"); + + var escape = Path.Combine(package, "escape"); + Require(TryLinkDirectory(escape, outside), "Could not link a package directory outside the package."); + Reject(package, "escape/Secret.lean", "a link"); + Reject(package, "escape", "a link"); + Reject(package, "escape/New/Module.lean", "a link"); + Reject(linkedPackage, "escape/Secret.lean", "a link"); + Require(ResolvedRelativePath(outside, Path.Combine(package, "escape", "Secret.lean")) == "Secret.lean", + "A location inside a directory reached through a link was not found inside it."); + // A dangling link still names where a write through it would land. + var gone = Path.Combine(work, "gone"); + Directory.CreateDirectory(gone); + var dangling = Path.Combine(package, "dangling"); + Require(TryLinkDirectory(dangling, gone), "Could not link a package directory outside the package."); + Directory.Delete(gone); + Reject(package, "dangling", "a dangling link"); + Reject(package, "dangling/New.lean", "a dangling link"); + var alias = Path.Combine(package, "alias"); + Require(TryLinkDirectory(alias, Path.Combine(package, ".lake")), "Could not link inside the package."); + Require(Within(package, "alias/packages") == Path.Combine(package, ".lake", "packages"), + "A link that stays inside its package was rejected or not followed."); + // POSIX reads `..` where a link leads and Windows where it is spelled; a path both place inside stays. + Reject(package, "escape/../outside/Secret.lean", "a link followed by .."); + Require(Within(package, "alias/../.lake") == Path.Combine(package, ".lake"), + "A path whose .. after a link stays inside its package under both readings was rejected."); + Console.WriteLine("PASS a link inside a package that leaves it is rejected, also when dangling"); + + var first = Path.Combine(package, "first"); + var second = Path.Combine(package, "second"); + Require(TryLinkDirectory(first, second) && TryLinkDirectory(second, first), + "Could not make a cycle of directory links."); + Reject(package, "first/Module.lean", "a cycle of links"); + Console.WriteLine("PASS a cycle of links is rejected"); + } + // A recursive delete removes a link without entering the directory it names. + finally { Directory.Delete(work, true); } + } + + /// + /// Paths compared under a case rule given per directory: a name is matched by the rule of the directory that + /// holds it, so a case-sensitive volume mounted inside one that ignores case is judged at each level. + /// + static void CheckNameCaseRule(string work, Action require) + { + var volume = Path.Combine(work, "volume"); + var package = Path.Combine(volume, "package"); + var sibling = Path.Combine(volume, "Package", "Module.lean"); + var asked = new HashSet(StringComparer.Ordinal); + Func Every(NameCase rule) => directory => { asked.Add(directory); return rule; }; + require(RelativeBelow(package, sibling, Every(NameCase.Sensitive)) is null && + RelativeBelow(package, sibling, Every(NameCase.Insensitive)) == "Module.lean" && + asked.SetEquals([volume]) && RelativeBelow(package, package, Every(NameCase.Sensitive)) == ".", + "A name differing only in case was not decided by the rule of the directory holding it."); + // `work` ignores case and `volume` tells it apart, as a disk image mounted in a temporary directory. + Func mounted = directory => directory == work ? NameCase.Insensitive : NameCase.Sensitive; + var respelled = Path.Combine(work, "VOLUME", "package", "Module.lean"); + require(RelativeBelow(package, respelled, mounted) == "Module.lean" && + RelativeBelow(package, sibling, mounted) is null, + "A path crossing volumes with different case rules was judged by one rule."); + LocationEquality sensitive = new(Every(NameCase.Sensitive), unknownMatches: true); + LocationEquality insensitive = new(Every(NameCase.Insensitive), unknownMatches: false); + var upper = Path.Combine(volume, "PACKAGE"); + require(!sensitive.Equals(package, upper) && insensitive.Equals(package, upper) && + insensitive.GetHashCode(package) == insensitive.GetHashCode(upper) && + new HashSet([package, upper], insensitive).Count == 1 && + new HashSet([package, upper], sensitive).Count == 2, + "Locations differing only in case were compared against the given rule."); + // An unknown rule never admits a path into a package, and keeps two outputs apart as if they collided. + LocationEquality known = new(Every(NameCase.Unknown), unknownMatches: false); + LocationEquality colliding = new(Every(NameCase.Unknown), unknownMatches: true); + require(RelativeBelow(package, sibling, Every(NameCase.Unknown)) is null && + RelativeBelow(package, sibling, Every(NameCase.Unknown), unknownMatches: true) == "Module.lean" && + !known.Equals(package, upper) && colliding.Equals(package, upper) && + new HashSet([package, upper], colliding).Count == 1, + "A name in another case under an unknown rule was admitted, or two outputs were not kept apart."); + Console.WriteLine("PASS paths are compared under the case rule of each directory that holds a name"); + } + + /// + /// Containment and identity on a volume that tells case apart, where a sibling whose name differs only in case + /// is another directory. The volume is made without privileges: a disk image on macOS, a directory with the + /// case-sensitive flag on Windows, and the temporary directory itself on Linux. + /// + static void CheckCaseSensitiveVolume(string directory, Action require, + Action reject) + { + // Asked before it exists, the directory takes the rule of the volume it would be made on. + var before = NameCaseIn(directory); + require(before == NameCaseIn(Path.GetDirectoryName(directory)!), + "A missing directory did not take the rule of its nearest existing ancestor."); + if (CaseSensitiveDirectory(directory) is not { } removeVolume) return; + try + { + var package = Path.Combine(directory, "package"); + var sibling = Path.Combine(directory, "Package"); + Directory.CreateDirectory(Path.Combine(package, ".lake")); + Directory.CreateDirectory(sibling); + File.WriteAllText(Path.Combine(sibling, "Secret.lean"), "-- beside the package\n"); + require(!Directory.Exists(Path.Combine(directory, "PACKAGE")), + "The case-sensitive volume found a directory under a name no entry has."); + reject(package, Path.Combine(sibling, "Secret.lean"), "a sibling named in another case"); + reject(package, "../Package/Secret.lean", "a sibling named in another case"); + reject(package, Path.Combine(directory, "PACKAGE", "New.lean"), + "a missing directory named in another case"); + File.WriteAllText(Path.Combine(sibling, "Fixture.cs"), ""); + var checkout = Root; + Root = package; + try + { + FixtureInputs("../Package/Fixture.cs"); + throw new Exception("A fixture input in a sibling named in another case was taken as checked out."); + } + catch (InvalidOperationException) { } + finally { Root = checkout; } + require(!SameLocation(package, sibling) && !Inside(package, Path.Combine(sibling, "Secret.lean")) && + Inside(package, Path.Combine(package, ".lake")) && + new[] { package, sibling }.Select(ResolvedPath).Distinct(LocationComparer).Count() == 2, + "A sibling whose name differs only in case was judged the package's location."); + // The answer given before the volume existed is not reused for it. + require(NameCaseIn(directory) == NameCase.Sensitive && + NameCaseIn(Path.Combine(directory, "missing", "deeper")) == NameCase.Sensitive, + "A case-sensitive volume was read as ignoring case, or given an answer from before it existed."); + require(OperatingSystem.IsLinux() ? QueriedNameCase(directory) is null + : QueriedNameCase(directory) == NameCase.Sensitive, + $"The operating system reported {QueriedNameCase(directory)} for a case-sensitive volume."); + // Both spellings of one name are listed, so neither shows the rule; the other entry does. + require(EntryNameCase(directory) == NameCase.Sensitive, + "The entries of a directory holding both spellings of a name were read as ignoring case."); + var pair = Path.Combine(directory, "pair"); + Directory.CreateDirectory(Path.Combine(pair, "name")); + Directory.CreateDirectory(Path.Combine(pair, "NAME")); + var empty = Path.Combine(directory, "empty"); + Directory.CreateDirectory(empty); + require(EntryNameCase(pair) == NameCase.Unknown && EntryNameCase(empty) == NameCase.Unknown, + "Entries that cannot show the case rule were read as showing one."); + // An unknown rule is never kept: the directory is asked again once an entry can show it. + var firstAnswer = NameCaseIn(empty); + require(firstAnswer == (QueriedNameCase(empty) ?? NameCase.Unknown), + $"A directory whose entries show no rule was given {firstAnswer}."); + Directory.CreateDirectory(Path.Combine(empty, "probe")); + require(NameCaseIn(empty) == NameCase.Sensitive, + "An unknown case rule was kept after the directory gained an entry that shows it."); + reject(Path.Combine(pair, "name"), Path.Combine(pair, "NAME", "Module.lean"), + "a sibling named in another case where the entries show no rule"); + Console.WriteLine( + "PASS on a case-sensitive volume a sibling named in another case lies outside the package"); + } + finally { removeVolume(); } + } + + /// + /// Makes `directory` on a volume that tells case apart and returns how to remove it, or null after printing SKIP + /// where Windows refuses the flag or the Linux temporary directory ignores case. macOS always can, so a failure + /// there throws. + /// + static Action? CaseSensitiveDirectory(string directory) + { + var parent = Path.GetDirectoryName(directory)!; + if (OperatingSystem.IsMacOS()) + { + var image = directory + ".dmg"; + Run(["hdiutil", "create", "-size", "16m", "-fs", "Case-sensitive APFS", "-volname", "leancil-case", + "-type", "UDIF", image], parent); + Directory.CreateDirectory(directory); + Run(["hdiutil", "attach", "-nobrowse", "-noautoopen", "-mountpoint", directory, image], parent); + return () => Run(["hdiutil", "detach", "-force", directory], parent); + } + Directory.CreateDirectory(directory); + if (OperatingSystem.IsWindows()) + { + var flag = Execute(["fsutil.exe", "file", "setCaseSensitiveInfo", directory, "enable"], parent); + if (flag.Code == 0) return () => { }; + Console.WriteLine("SKIP case-sensitive volume: fsutil could not mark a directory: " + flag.Output.Trim()); + return null; + } + Directory.CreateDirectory(Path.Combine(directory, "probe")); + if (!Directory.Exists(Path.Combine(directory, "PROBE"))) return () => { }; + Console.WriteLine("SKIP case-sensitive volume: the temporary directory ignores case"); + return null; + } +} diff --git a/Tests/ConsumerSupportCandidates.cs b/Tests/ConsumerSupportCandidates.cs index 48a6dca73..b94eeea98 100644 --- a/Tests/ConsumerSupportCandidates.cs +++ b/Tests/ConsumerSupportCandidates.cs @@ -1,8 +1,13 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs +#:include DirectoryLinks.cs bool includeLake = args switch { @@ -38,6 +43,23 @@ string Source(string module, string contents) throw new Exception("Unexpected candidate union: " + string.Join(',', modules)); } Console.WriteLine("PASS candidate union is order independent and excludes transitive generated imports"); + // One source directory listed in two spellings, as Lake's resolved path beside the spelled package root. + var alias = root + "-alias"; + if (TryLinkDirectory(alias, root)) + { + try + { + var modules = ConsumerSupportCandidates([first, second], [alias, root], ["ImportedInterface"], + Path.Combine(root, "Toolchain")); + if (!modules.SequenceEqual(new[] { "Early", "Late" })) + throw new Exception("A source directory reached through a link made its modules ambiguous: " + + string.Join(',', modules)); + } + finally { Directory.Delete(alias); } + Console.WriteLine("PASS a source directory reached through a link is one location"); + } + else if (!OperatingSystem.IsWindows()) throw new Exception("Could not make a directory symbolic link."); + else Console.WriteLine("SKIP source directories through links: no directory link or junction could be made"); } finally { Directory.Delete(root, true); } diff --git a/Tests/ConsumerSymbols.cs b/Tests/ConsumerSymbols.cs index 2c6ee4e6d..222d74abd 100644 --- a/Tests/ConsumerSymbols.cs +++ b/Tests/ConsumerSymbols.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; @@ -31,7 +36,8 @@ string Build(string name, string first, string debugType) var source = Path.Combine(root, name); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Names.csproj"), $""" - net10.0true{debugType} + net11.0 + true{debugType} """); File.WriteAllText(Path.Combine(source, "Names.cs"), string.Join("\n", "namespace Names;", @@ -83,7 +89,8 @@ import Client.Named namespace ExampleProof -- The second block stores the wide product in its own `carry`. theorem correctness : - Client.sumBody.code[Client.atWide + 1]? = some (.setLocal (local_named Client.sum "carry" at (Client.atWide + 1))) := rfl + Client.sumBody.code[Client.atWide + 1]? = + some (.stloc (local_named Client.sum "carry" at (Client.atWide + 1))) := rfl end ExampleProof """); var plain = Path.Combine(package, "Plain.lean"); @@ -95,7 +102,7 @@ namespace ExampleProof bind_method sum sumBody := "{{Sum}}" bind_callee wide wideBody := "System.UInt64 Names.Calls::Wide(System.UInt32,System.UInt32)" from sumBody bind_call atWide := wide from sumBody - theorem correctness : sumBody.code[atWide + 1]? = some (.setLocal 1) := rfl + theorem correctness : sumBody.code[atWide + 1]? = some (.stloc 1) := rfl end ExampleProof """); Run(["lake", "update"], package); @@ -155,7 +162,7 @@ static string Statement(JsonElement report) => if (Cached(first) || !Replayed("ExtractedSymbols") || !Replayed("Client.Named")) throw new InvalidOperationException("ExtractedSymbols and its importer were not checked and replayed."); // The statement holds the chosen slot, not a reference to the PDB names. -if (!statement.Contains("setLocal 1") || statement.Contains("local_named") || statement.Contains("ExtractedSymbols")) +if (!statement.Contains("stloc 1") || statement.Contains("local_named") || statement.Contains("ExtractedSymbols")) throw new InvalidOperationException("Unexpected elaborated statement: " + statement); if (!Cached(Verify(named, proof))) throw new InvalidOperationException("An unchanged proof importing ExtractedSymbols was not reused."); if (Cached(Verify(plainOutput, plain)) || Replayed("ExtractedSymbols")) diff --git a/Tests/ConsumerVerification.cs b/Tests/ConsumerVerification.cs index dcd1fee6b..c48d6acb9 100644 --- a/Tests/ConsumerVerification.cs +++ b/Tests/ConsumerVerification.cs @@ -1,23 +1,34 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ConsumerTraceDirectory.cs #:include ConsumerBatchOutcomes.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs #:include ../scripts/LibraryAudit.cs #:include ConsumerSourceResolution.cs +#:include ConsumerImportDirectories.cs #:include ConsumerSupportUnion.cs #:include ConsumerCertificateReuse.cs #:include ConsumerComponents.cs #:include ConsumerFingerprintReuse.cs #:include CertifiedFingerprints.cs +#:include ConsumerPackagePaths.cs +#:include DirectoryLinks.cs +#:include GeneratedEvidence.cs using System.Text.Json; +using System.Text.Json.Nodes; var checks = new Dictionary>(StringComparer.Ordinal) { ["source-resolution"] = CheckConsumerSourceResolution, + ["package-paths"] = CheckConsumerPackagePaths, + ["import-directories"] = CheckConsumerImportDirectories, ["support-union"] = CheckConsumerSupportUnion, ["certificate-reuse"] = CheckConsumerCertificateReuse, ["components"] = CheckConsumerComponents, @@ -33,7 +44,8 @@ } // Compile the harness once; separate processes retain private environment and verifier state. foreach (var name in checks.Keys) - Run(["dotnet", typeof(Program).Assembly.Location, "--check", name], streamOutput: name == "compile-check"); + Run(["dotnet", typeof(Program).Assembly.Location, "--check", name], + streamOutput: name is "compile-check" or "package-paths"); var cli = new[] { "dotnet", args.Length == 0 ? ExtractorPath : Path.GetFullPath(args[0]) }; var root = Path.GetFullPath(At("artifacts/ConsumerPackage")); @@ -106,7 +118,7 @@ string Reject(string marker, params string[] options) Verify("--proof", proof, "--proof-root", root, "--theorem", bindingGate.Theorem); var first = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(output, "verified.json"))); var inputs = first.GetProperty("inputs"); -if (first.GetProperty("schema").GetInt32() != 4 || +if (first.GetProperty("schema").GetInt32() != CertificateSchema || !first.GetProperty("extractionAudit").GetString()!.Contains("Extracted.translation_valid", StringComparison.Ordinal) || !first.GetProperty("libraryBuild").GetString()!.StartsWith("consumer-", StringComparison.Ordinal)) throw new InvalidOperationException("Consumer verification omitted certified base or exact extraction certificate audit."); @@ -124,6 +136,59 @@ string Reject(string marker, params string[] options) throw new InvalidOperationException("Unchanged consumer proof was not reused."); Console.WriteLine("PASS unchanged consumer proof reuses an authenticated successful result"); +// An authenticated receipt whose certificate this tool does not accept is a miss: the run verifies afresh +// instead of failing or reusing it. Republishing under the same key keeps the receipt authenticated. +var receiptEntries = Directory.EnumerateFiles(SuccessCacheRoot, "report.json", SearchOption.AllDirectories) + .Where(receipt => JsonSerializer.Deserialize(File.ReadAllText(receipt)).GetProperty("inputs") + .TryGetProperty("consumer:" + proof, out _)) + .Select(receipt => Path.GetDirectoryName(Path.GetDirectoryName(receipt)!)!).ToArray(); +if (receiptEntries.Length == 0) throw new InvalidOperationException("No consumer proof receipt was published."); +var originalReceipts = At("artifacts/ConsumerReceipts-" + Guid.NewGuid().ToString("N")); +using (var publisher = new Program()) +{ + try + { + foreach (var entry in receiptEntries) + { + var original = Path.Combine(originalReceipts, Path.GetFileName(entry)); + CopyTree(Path.Combine(entry, "files"), original); + var unknownStatus = original + "-unknown-status"; + CopyTree(original, unknownStatus); + var certificate = JsonNode.Parse(File.ReadAllText(Path.Combine(unknownStatus, "report.json")))!; + certificate["binding"]!["facts"]![0]!["status"] = "assumed"; + File.WriteAllText(Path.Combine(unknownStatus, "report.json"), certificate.ToJsonString()); + DeleteTree(entry); + publisher.PublishSuccess(Path.GetFileName(entry), unknownStatus); + // The edited receipt must be the authenticated entry the run restores, or the miss proves nothing. + var published = unknownStatus + "-published"; + if (!publisher.RestoreSuccess(Path.GetFileName(entry), published) || + !File.ReadAllText(Path.Combine(published, "report.json")).Contains("\"assumed\"", + StringComparison.Ordinal)) + throw new InvalidOperationException("The edited consumer receipt was not published authenticated."); + } + Verify(); + var refreshed = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(output, "verified.json"))); + if (refreshed.GetProperty("libraryBuild").GetString() == "consumer-proof-cache") + throw new InvalidOperationException("A receipt with an unknown binding status was reused."); + } + finally + { + foreach (var entry in receiptEntries.Where(entry => Directory.Exists(Path.Combine(originalReceipts, + Path.GetFileName(entry))))) + { + if (Directory.Exists(entry)) DeleteTree(entry); + publisher.PublishSuccess(Path.GetFileName(entry), Path.Combine(originalReceipts, Path.GetFileName(entry))); + } + DeleteTree(originalReceipts); + } +} +Verify(); +var reused = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(output, "verified.json"))); +if (reused.GetProperty("libraryBuild").GetString() != "consumer-proof-cache") + throw new InvalidOperationException("The restored consumer receipt was not reused."); +Console.WriteLine("PASS a consumer receipt with a certificate this tool refuses is verified afresh"); + // A distinct extraction identity must recheck the extraction-dependent proof, // while the independently certified consumer helper can retain its artifact. var trace = testTrace; diff --git a/Tests/ContractCalls.lean b/Tests/ContractCalls.lean index 167c61318..fccdecb99 100644 --- a/Tests/ContractCalls.lean +++ b/Tests/ContractCalls.lean @@ -1,5 +1,6 @@ import CIL.Safety.MethodContract import CIL.Safety.Certificate +import Tests.Support /-! Contract stepping composes opaque child invocations with independent fuel. -/ namespace Tests.ContractCalls @@ -7,7 +8,8 @@ open CIL CIL.Safety /-- The caller has no access to the helper body. -/ @[cil_code] def caller : Method := { - returnsValue := true, locals := [], code := [.call 1 0, .ret] } + returnsValue := true, locals := [], code := [.call (Tests.callee 0) {}, + .ret], links := [(0, .direct 1)] } /-- A separately proved child run supplies the result without unfolding the helper. -/ theorem child_exists (program : Program) (memory final : Safety.Memory) (value : W32) @@ -15,7 +17,7 @@ theorem child_exists (program : Program) (memory final : Safety.Memory) (value : (child : ∃ fuel, Safety.invoke program fuel 1 [] memory = .ok (final, [.scalar (.i32 value)])) : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = .ok (result, returned) ∧ + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ result = final ∧ returned = [.scalar (.i32 value)] := by cil_contract_steps [found, child] exact ⟨rfl, rfl⟩ @@ -26,7 +28,7 @@ theorem child_certificate (program : Program) (memory final : Safety.Memory) (va (fuel : Nat) (found : program[0]? = some caller) (child : InvocationCertificate program 1 [] memory fuel final [.scalar (.i32 value)]) : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = .ok (result, returned) ∧ + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ result = final ∧ returned = [.scalar (.i32 value)] := by cil_contract_steps [found, child] exact ⟨rfl, rfl⟩ @@ -37,7 +39,7 @@ theorem child_fixed (program : Program) (memory final : Safety.Memory) (value : (fuel : Nat) (found : program[0]? = some caller) (child : Safety.invoke program fuel 1 [] memory = .ok (final, [.scalar (.i32 value)])) : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = .ok (result, returned) ∧ + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ result = final ∧ returned = [.scalar (.i32 value)] := by cil_contract_steps [found, child] exact ⟨rfl, rfl⟩ @@ -47,7 +49,7 @@ theorem child_fixed (program : Program) (memory final : Safety.Memory) (value : theorem checked_child (program : Program) (memory final : Safety.Memory) (value : W32) (found : program[0]? = some caller) (child : Safety.invoke program 2 1 [] memory = .ok (final, [.scalar (.i32 value)])) : - Safety.run program 3 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run program 3 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (final, [.scalar (.i32 value)]) := by cil_checked_steps [found, child] #print axioms checked_child @@ -68,10 +70,10 @@ theorem wrong_callee (program : Program) (memory final : Safety.Memory) (value : (wrong : ∃ fuel, Safety.invoke program fuel 2 [] memory = .ok (final, [.scalar (.i32 value)])) (remaining : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = .ok (result, returned) ∧ + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ result = final ∧ returned = [.scalar (.i32 value)]) : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = .ok (result, returned) ∧ + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ result = final ∧ returned = [.scalar (.i32 value)] := by fail_if_success cil_contract_step [found, wrong] exact remaining @@ -89,7 +91,7 @@ def seven : MethodContract Unit W32 where theorem supplied_contract (program : Program) (memory : Safety.Memory) (found : program[0]? = some caller) (holds : seven.Holds program 1) : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = .ok (result, returned) ∧ + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 7)] := by cil_contract_steps [found, holds, seven] obtain rfl := call.post @@ -98,7 +100,8 @@ theorem supplied_contract (program : Program) (memory : Safety.Memory) /-- A caller that passes its argument to the helper and returns the helper's result. -/ @[cil_code] def forwarder : Method := { - returnsValue := true, locals := [], code := [.arg 0, .call 1 1, .ret] } + returnsValue := true, locals := [], code := [.ldarg 0, .call (Tests.callee 1) {}, + .ret], links := [(1, .direct 1)] } /-- A helper over one word, which returns its successor. -/ def successor : MethodContract W32 W32 where @@ -113,7 +116,7 @@ def successor : MethodContract W32 W32 where theorem word_input (program : Program) (memory : Safety.Memory) (value : W32) (found : program[0]? = some forwarder) (holds : successor.Holds program 1) : ∃ fuel result returned, - Safety.run program fuel 0 0 [.scalar (.i32 value)] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run program fuel 0 0 [.scalar (.i32 value)] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 (value + 1))] := by cil_contract_steps [found, holds, successor] @@ -124,7 +127,7 @@ theorem word_input (program : Program) (memory : Safety.Memory) (value : W32) /-- A concrete helper used only to establish its registered public contract. -/ @[cil_code] def helper : Method := { - returnsValue := true, locals := [], code := [.const32 7, .ret] } + returnsValue := true, locals := [], code := [.ldc_i4 7, .ret] } /-- A method binding fixes the callee index used by the registered theorem. -/ @[cil_code] def boundProgram : Program := [caller, helper] @@ -182,20 +185,20 @@ example (memory final : Safety.Memory) (returned : seven.Returns () memory final /-- Leaving a literal frame that owns nothing is stated as the memory it left; a frame that owns an allocation, or is a variable, keeps `leaveFrame`. -/ example (memory : Safety.Memory) (frame : Safety.Frame) - (kept : memory.WellFormed ∧ (Safety.leaveFrame ⟨0, [], [3], [], 0⟩ memory).WellFormed ∧ + (kept : memory.WellFormed ∧ (Safety.leaveFrame ⟨0, [], [3], 0⟩ memory).WellFormed ∧ (Safety.leaveFrame frame memory).WellFormed) : - (Safety.leaveFrame ⟨0, [], [], [], 0⟩ memory).WellFormed ∧ - (Safety.leaveFrame ⟨0, [], [3], [], 0⟩ memory).WellFormed ∧ + (Safety.leaveFrame ⟨0, [], [], 0⟩ memory).WellFormed ∧ + (Safety.leaveFrame ⟨0, [], [3], 0⟩ memory).WellFormed ∧ (Safety.leaveFrame frame memory).WellFormed := by cil_frame_exit - show memory.WellFormed ∧ (Safety.leaveFrame ⟨0, [], [3], [], 0⟩ memory).WellFormed ∧ + show memory.WellFormed ∧ (Safety.leaveFrame ⟨0, [], [3], 0⟩ memory).WellFormed ∧ (Safety.leaveFrame frame memory).WellFormed exact kept /-- Registration supplies the callee contract without an explicit holds fact. -/ theorem registered_caller (memory : Safety.Memory) : ∃ fuel result returned, - Safety.run boundProgram fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run boundProgram fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 7)] := by cil_contract_steps [seven] obtain rfl := call.post @@ -206,14 +209,15 @@ theorem registered_caller (memory : Safety.Memory) : is never stepped, so only the fact can give `value`. -/ theorem checked_child_concrete (memory final : Safety.Memory) (value : W32) (child : Safety.invoke boundProgram 2 1 [] memory = .ok (final, [.scalar (.i32 value)])) : - Safety.run boundProgram 3 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run boundProgram 3 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (final, [.scalar (.i32 value)]) := by cil_checked_steps [child] #print axioms checked_child_concrete /-- A caller forwards two scalar arguments to a separately contracted helper. -/ @[cil_code] def pairCaller : Method := { - returnsValue := true, locals := [], code := [.arg 0, .arg 1, .call 1 2, .ret] } + returnsValue := true, locals := [], code := [.ldarg 0, .ldarg 1, .call (Tests.callee 2) {}, + .ret], links := [(2, .direct 1)] } /-- A tuple input exposes both argument inference and a nontrivial precondition. -/ def orderedPair : MethodContract (W32 × W32) W32 where @@ -229,7 +233,7 @@ theorem tuple_input (program : Program) (memory : Safety.Memory) (left right : W (ordered : left < right) : ∃ fuel result returned, Safety.run program fuel 0 0 [.scalar (.i32 left), .scalar (.i32 right)] - ⟨0, [], [], [], 0⟩ [] memory = .ok (result, returned) ∧ + ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 (left + right))] := by cil_contract_steps [found, holds, orderedPair, ordered] obtain rfl := call.post @@ -242,7 +246,7 @@ theorem explicit_precondition (program : Program) (memory : Safety.Memory) (left (later : Unit → left < right) : ∃ fuel result returned, Safety.run program fuel 0 0 [.scalar (.i32 left), .scalar (.i32 right)] - ⟨0, [], [], [], 0⟩ [] memory = .ok (result, returned) ∧ + ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 (left + right))] := by cil_contract_steps [found, holds, orderedPair] case precondition => @@ -268,7 +272,7 @@ theorem explicit_precondition (program : Program) (memory : Safety.Memory) (left theorem parameterized_registration (program : Program) (memory : Safety.Memory) (found : program[0]? = some caller) (helperFound : program[1]? = some helper) : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 7)] := by cil_contract_steps [found, helperFound, seven] obtain rfl := call.post @@ -278,7 +282,7 @@ theorem parameterized_registration (program : Program) (memory : Safety.Memory) /-- Automatic call composition preserves an unrelated sibling obligation. -/ theorem sibling_goal (memory : Safety.Memory) : (∃ fuel result returned, - Safety.run boundProgram fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run boundProgram fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 7)]) ∧ (7 : Nat) = 7 := by constructor cil_contract_steps [seven] @@ -291,10 +295,10 @@ theorem sibling_goal (memory : Safety.Memory) : theorem missing_binding (program : Program) (memory : Safety.Memory) (found : program[0]? = some caller) (remaining : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 7)]) : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 7)] := by fail_if_success cil_contract_step [found, seven] exact remaining @@ -330,10 +334,10 @@ def ghostContract : MethodContract GhostInput W32 where theorem ambiguous_input (program : Program) (memory : Safety.Memory) (found : program[0]? = some caller) (holds : ghostContract.Holds program 1) (remaining : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 7)]) : ∃ fuel result returned, - Safety.run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory = + Safety.run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory = .ok (result, returned) ∧ returned = [.scalar (.i32 7)] := by fail_if_success cil_contract_step [found, holds, ghostContract] exact remaining diff --git a/Tests/ContractConsequence.lean b/Tests/ContractConsequence.lean index 29c971e15..0b857eb79 100644 --- a/Tests/ContractConsequence.lean +++ b/Tests/ContractConsequence.lean @@ -5,6 +5,7 @@ import CIL.Safety.WordMemory import CIL.Safety.WordFootprint import CIL.Safety.ReturnMemory import CIL.Safety.Forwarders +import Tests.Support /-! Synthetic extraction: `Root(v) => Percent(v) + 1`, a throwing `Percent`, `AddTo(in a, in b, out res)`, `Sum` and `SumSwapped(in b, in a, out res)` forwarding to it, @@ -37,35 +38,46 @@ def method8Signature : String := /-- `IsZeroIgnoring(v, unused) => IsZero(v)`. -/ def method9Signature : String := "System.Boolean Consequence.Calls::IsZeroIgnoring(System.UInt64,System.UInt64)" -@[cil_code] def method0 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .call 1 1, .const32 1, .convI8, .add, .ret] } +@[cil_code] def method0 : Method := { + returnsValue := true, locals := [], links := [(1, .direct 1)], code := + [.ldarg 0, .call (Tests.callee 1) {}, .ldc_i4 1, .conv_i8, .add, .ret] } @[cil_code] def method1 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .const32 (BitVec.ofInt 32 100), .convI8, .bleu 7, .nop, .nop, - .throw "System.ArgumentOutOfRangeException", .arg 0, .ret] } + [.ldarg 0, .ldc_i4 (BitVec.ofInt 32 100), .conv_i8, .ble_un 7, .nop, .nop, + .throw .argumentOutOfRange, .ldarg 0, .ret] } @[cil_code] def method2 : Method := { returnsValue := false, locals := [], code := - [.arg 2, .arg 0, .load64, .arg 1, .load64, .add, .store64, .ret] } + [.ldarg 2, .ldarg 0, .ldind .i8, .ldarg 1, .ldind .i8, .add, .stind .i8, .ret] } @[cil_code] def method3 : Method := { returnsValue := false, locals := [], code := - [.arg 0, .arg 1, .arg 2, .call 2 3, .ret] } + [.ldarg 0, .ldarg 1, .ldarg 2, .call (Tests.callee 3) {}, .ret], links := [(3, .direct 2)] } /-- `SumSwapped`: loads its arguments in the order `AddTo` takes them. -/ @[cil_code] def method4 : Method := { returnsValue := false, locals := [], code := - [.arg 1, .arg 0, .arg 2, .call 2 3, .ret] } + [.ldarg 1, .ldarg 0, .ldarg 2, .call (Tests.callee 3) {}, .ret], links := [(3, .direct 2)] } /-- `SumDiscard`: passes its local's address as `AddFlag`'s `res` and returns the flag. -/ @[cil_code] def method5 : Method := - { returnsValue := true, locals := [.unmodeled], localKinds := [.word64], code := - [.arg 0, .arg 1, .localAddr 0, .call 8 3, .ret (some .unsigned8)] } + { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [.unmodeled], localKinds := [.word64], code := + [.ldarg 0, .ldarg 1, .ldloca 0, .call (Tests.callee 3) {}, .ret], links := [(3, .direct 8)] } /-- `IsZero`: a comparison returned as a narrowed boolean. -/ -@[cil_code] def method6 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .const32 0, .convI8, .eq, .ret (some .unsigned8)] } +@[cil_code] def method6 : Method := { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [], code := + [.ldarg 0, .ldc_i4 0, .conv_i8, .ceq, .ret] } /-- `IsZeroForward`: re-narrows `IsZero`'s boolean through its own `ret`. -/ -@[cil_code] def method7 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .call 6 1, .ret (some .unsigned8)] } +@[cil_code] def method7 : Method := { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [], code := + [.ldarg 0, .call (Tests.callee 1) {}, .ret], links := [(1, .direct 6)] } /-- `AddFlag`: `AddTo`'s store followed by a constant `true`. -/ -@[cil_code] def method8 : Method := { returnsValue := true, locals := [], code := - [.arg 2, .arg 0, .load64, .arg 1, .load64, .add, .store64, .const32 1, - .ret (some .unsigned8)] } +@[cil_code] def method8 : Method := { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [], code := + [.ldarg 2, .ldarg 0, .ldind .i8, .ldarg 1, .ldind .i8, .add, .stind .i8, .ldc_i4 1, + .ret] } /-- `IsZeroIgnoring`: forwards its first argument and never loads its second. -/ -@[cil_code] def method9 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .call 6 1, .ret (some .unsigned8)] } +@[cil_code] def method9 : Method := { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [], code := + [.ldarg 0, .call (Tests.callee 1) {}, .ret], links := [(1, .direct 6)] } /-- `AddFrom(out res, in b, in a) => AddTo(in a, in b, out res)`. -/ def method10Signature : String := "System.Void Consequence.Calls::AddFrom(System.UInt64&,System.UInt64&,System.UInt64&)" @@ -82,20 +94,30 @@ def method14Signature : String := "System.Boolean Consequence.Calls::IsSetForward(System.Boolean)" /-- `AddFrom`: loads its arguments in reverse, the order `AddTo` takes them. -/ @[cil_code] def method10 : Method := { returnsValue := false, locals := [], code := - [.arg 2, .arg 1, .arg 0, .call 2 3, .ret] } + [.ldarg 2, .ldarg 1, .ldarg 0, .call (Tests.callee 3) {}, .ret], links := [(3, .direct 2)] } /-- `CopyTo`: passes its zero-initialized local's address as `AddFrom`'s `a`. -/ @[cil_code] def method11 : Method := { returnsValue := false, locals := [.i64 0], localKinds := [.word64], code := - [.arg 0, .arg 1, .localAddr 0, .call 10 3, .ret] } + [.ldarg 0, .ldarg 1, .ldloca 0, .call (Tests.callee 3) {}, .ret], links := [(3, .direct 10)] } /-- `IsZeroSecond`: forwards its second argument and never loads its first. -/ -@[cil_code] def method12 : Method := { returnsValue := true, locals := [], code := - [.arg 1, .call 6 1, .ret (some .unsigned8)] } +@[cil_code] def method12 : Method := { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [], code := + [.ldarg 1, .call (Tests.callee 1) {}, .ret], links := [(1, .direct 6)] } /-- `IsSet`: a boolean parameter, loaded narrowed, returned narrowed. -/ -@[cil_code] def method13 : Method := { returnsValue := true, locals := [], code := - [.arg 0 (some .unsigned8), .ret (some .unsigned8)] } +@[cil_code] def method13 : Method := { + declared := { + parameters := [.atom CIL.Extraction.runtimeScope "System.Boolean"] + returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [], code := + [.ldarg 0, .ret] } /-- `IsSetForward`: passes its boolean parameter through a narrowed load. -/ -@[cil_code] def method14 : Method := { returnsValue := true, locals := [], code := - [.arg 0 (some .unsigned8), .call 13 1, .ret (some .unsigned8)] } +@[cil_code] def method14 : Method := { + declared := { + parameters := [.atom CIL.Extraction.runtimeScope "System.Boolean"] + returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [], code := + [.ldarg 0, .call (Tests.callee 1) {}, .ret], links := [(1, .direct 13)] } @[cil_code] def program : Program := [method0, method1, method2, method3, method4, method5, method6, method7, method8, method9, method10, method11, method12, method13, method14] @@ -146,7 +168,7 @@ def percentExact : OutcomeContract CIL.W64 CIL.W64 where pre _ _ := True footprint _ := [] post v _ _ out := out = v - raises v _ := if v ≤ 100 then none else some "System.ArgumentOutOfRangeException" + raises v _ := if v ≤ 100 then none else some .argumentOutOfRange def percentBounded : OutcomeContract CIL.W64 CIL.W64 := { percentExact with post := fun _ _ _ out => out ≤ 100 } @@ -320,8 +342,9 @@ theorem sum_correct : sumContract.Holds Extracted.program sum := sum_holds add_p /-- `Sum` passes its arguments through in order, so it meets `addPublic` itself. -/ theorem sum_forwards : sumContract.Holds Extracted.program sum := - add_public.forward (loads := plainLoads [0, 1, 2]) (narrow := none) sum.found addTo.found - ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) (fun _ _ _ => plainLoads_unchanged _ _) + add_public.forward (loads := [0, 1, 2]) (narrow := none) sum.found addTo.found + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) + (fun _ _ _ => plainLoads_unchanged (by decide) _) (fun _ _ _ _ member => .inl member) (fun _ _ _ => rfl) (fun _ _ _ _ _ => trivial) @@ -335,9 +358,9 @@ def swappedArgs (input : AddInput) : List Value := /-- The swapped forwarder meets `addPublic` over its own argument order: the frame fact about `a` follows from `AddTo`'s footprint although `a` is now the second argument. -/ theorem swapped_forwards : (addPublic.withArgs swappedArgs).Holds Extracted.program swapped := - add_public.forward (loads := plainLoads [1, 0, 2]) (narrow := none) (forwarded := swappedArgs) - swapped.found addTo.found ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) - (fun _ _ _ => plainLoads_unchanged _ _) + add_public.forward (loads := [1, 0, 2]) (narrow := none) (forwarded := swappedArgs) + swapped.found addTo.found ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) + (fun _ _ _ => plainLoads_unchanged (by decide) _) (fun _ _ _ _ member => .inl (by simp only [swappedArgs, addPublic, addArgs, List.mem_cons, List.not_mem_nil, or_false] at member ⊢ @@ -347,10 +370,10 @@ theorem swapped_forwards : (addPublic.withArgs swappedArgs).Holds Extracted.prog #print axioms swapped_forwards /-- The indices are checked against the body: `SumSwapped` is not an in-order forwarder. -/ -theorem swapped_not_in_order : - ¬ Forwards swappedBody addBody (plainLoads [0, 1, 2]) addTo none := fun forwards => by +theorem swapped_not_in_order (method : CIL.Extraction.MethodRef) : + ¬ Forwards swappedBody addBody [0, 1, 2] method addTo none := fun forwards => by have code := forwards.code - simp [swappedBody, Extracted.method4, forwarderCode, plainLoads] at code + simp [swappedBody, Extracted.method4, forwarderCode] at code #print axioms swapped_not_in_order @@ -410,19 +433,21 @@ def discardContract : MethodContract DiscardInput Bool where /-- The out local is fresh, so `AddFlag`'s footprint drops out, `a` keeps its bytes and the returned flag outlives the local; the forwarder's frame is never stepped by hand. -/ theorem discard_forwards : discardContract.Holds Extracted.program discard := - add_flag_holds.forward_out (loads := plainLoads [0, 1]) (narrow := some .unsigned8) + add_flag_holds.forward_out (loads := [0, 1]) (narrow := some .unsigned8) (kind := .word64) (initial := .unmodeled) (fun input home => ⟨input.a, input.b, home, home, input.x, input.y⟩) discard.found addFlag.found - ⟨rfl, rfl, rfl, nofun, fun _ _ => nofun, fun _ => nofun, fun _ => nofun, + ⟨rfl, rfl, rfl, rfl, rfl, nofun, fun _ _ => nofun, fun _ => nofun, fun _ => nofun, fun _ => nofun, nofun, .inl rfl, rfl, rfl, rfl, rfl⟩ (fun _ _ => rfl) (fun _ _ ⟨formed, readA, readB⟩ => ⟨formed, fun value member => by simp only [discardContract, List.mem_cons, List.not_mem_nil, or_false] at member rcases member with same | same <;> subst same · exact access_reference_valid _ _ _ _ _ (read_access readA) - · exact access_reference_valid _ _ _ _ _ (read_access readB)⟩) - (fun _ _ _ _ => plainLoads_unchanged _ _) (fun _ _ _ _ => rfl) + · exact access_reference_valid _ _ _ _ _ (read_access readB), fun value member => by + simp only [discardContract, List.mem_cons, List.not_mem_nil, or_false] at member + rcases member with same | same <;> subst same <;> rfl⟩) + (fun _ _ _ _ => plainLoads_unchanged (by decide) _) (fun _ _ _ _ => rfl) (fun _ _ _ _ _ flag _ _ => ⟨_, rfl, by cases flag <;> decide⟩) (fun _ => rfl) (fun _ _ _ _ _ _ _ _ _ member => ⟨_, List.mem_singleton.mp member⟩) (fun _ _ _ _ ⟨formed, readA, readB⟩ below _ _ writable _ => ⟨ @@ -443,9 +468,9 @@ theorem array_local_has_no_home (activation : Nat) (element : CIL.ArrayElement) /-- An array local cannot satisfy the fresh-byte-home shape required by out forwarding. -/ theorem array_out_forwarder_rejected (body calleeBody : CIL.Method) - (loads : List (Nat × Option CIL.NarrowKind)) (callee : Nat) + (loads : List Nat) (method : CIL.Extraction.MethodRef) (callee : Nat) (narrow : Option CIL.NarrowKind) (element : CIL.ArrayElement) (initial : CIL.Value) : - ¬ ForwardsOut body calleeBody loads callee narrow (.array element) initial := + ¬ ForwardsOut body calleeBody loads method callee narrow (.array element) initial := fun forwards => forwards.notArray element rfl #print axioms array_out_forwarder_rejected @@ -457,9 +482,9 @@ def fromArgs (input : AddInput) : List Value := /-- `AddFrom` meets `addPublic` over its own argument order. -/ theorem add_from_forwards : (addPublic.withArgs fromArgs).Holds Extracted.program addFrom := - add_public.forward (loads := plainLoads [2, 1, 0]) (narrow := none) (forwarded := fromArgs) - addFrom.found addTo.found ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) - (fun _ _ _ => plainLoads_unchanged _ _) + add_public.forward (loads := [2, 1, 0]) (narrow := none) (forwarded := fromArgs) + addFrom.found addTo.found ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) + (fun _ _ _ => plainLoads_unchanged (by decide) _) (fun _ _ _ _ member => .inl (by simp only [fromArgs, addPublic, addArgs, List.mem_cons, List.not_mem_nil, or_false] at member ⊢ @@ -488,19 +513,21 @@ def copyContract : MethodContract CopyInput where /-- The zeroed local is `AddTo`'s `a`: its initializer supplies `a`'s word, its freshness the `Apart` fact between `a` and `res`, and `AddTo`'s region footprint is the caller's own. -/ theorem copy_forwards : copyContract.Holds Extracted.program copyTo := - add_from_forwards.forward_out (loads := plainLoads [0, 1]) (narrow := none) + add_from_forwards.forward_out (loads := [0, 1]) (narrow := none) (kind := .word64) (initial := .i64 0) (fun input home => ⟨home, input.b, input.res, input.region, 0, input.y⟩) copyTo.found addFrom.found - ⟨rfl, rfl, rfl, nofun, fun _ _ => nofun, fun _ => nofun, fun _ => nofun, + ⟨rfl, rfl, rfl, rfl, rfl, nofun, fun _ _ => nofun, fun _ => nofun, fun _ => nofun, fun _ => nofun, nofun, .inr trivial, rfl, rfl, rfl, rfl⟩ (fun _ _ => rfl) (fun _ _ ⟨formed, readB, writable, _⟩ => ⟨formed, fun value member => by simp only [copyContract, List.mem_cons, List.not_mem_nil, or_false] at member rcases member with same | same <;> subst same · exact access_reference_valid _ _ _ _ _ writable - · exact access_reference_valid _ _ _ _ _ (read_access readB)⟩) - (fun _ _ _ _ => plainLoads_unchanged _ _) (fun _ _ _ _ => rfl) + · exact access_reference_valid _ _ _ _ _ (read_access readB), fun value member => by + simp only [copyContract, List.mem_cons, List.not_mem_nil, or_false] at member + rcases member with same | same <;> subst same <;> rfl⟩) + (fun _ _ _ _ => plainLoads_unchanged (by decide) _) (fun _ _ _ _ => rfl) (fun _ _ _ _ _ _ _ _ => trivial) (fun _ => rfl) (fun _ _ _ _ _ _ _ ⟨empty, _⟩ _ member => by subst empty; cases member) (fun input memory entered home ⟨formed, readB, writable, within⟩ below enteredFormed fresh @@ -533,7 +560,7 @@ def isZeroContract : MethodContract CIL.W64 Bool where /-- `IsZero` meets its contract by stepping its body. -/ theorem is_zero_holds : isZeroContract.Holds Extracted.program isZero := by intro v memory _ - cil_contract_steps [isZeroContract, NarrowKind.extend_ite_ofNat_one_zero] + cil_contract_steps [isZeroContract, CIL.NarrowKind.extend_ite_ofNat_one_zero] refine ⟨Confined.refl _ memory, decide (v = 0), ?_, rfl⟩ simp [isZeroContract, CIL.NarrowKind.extend_ite_ofNat_one_zero] @@ -543,9 +570,9 @@ theorem is_zero_holds : isZeroContract.Holds Extracted.program isZero := by identity on 0 and 1, so it inherits the contract. -/ theorem is_zero_forwards : (isZeroContract.withArgs isZeroContract.args).Holds Extracted.program isZeroForward := - is_zero_holds.forward (loads := plainLoads [0]) (narrow := some .unsigned8) - isZeroForward.found isZero.found ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) - (fun _ _ _ => plainLoads_unchanged _ _) + is_zero_holds.forward (loads := [0]) (narrow := some .unsigned8) + isZeroForward.found isZero.found ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) + (fun _ _ _ => plainLoads_unchanged (by decide) _) (fun _ _ _ _ member => .inl member) (fun _ _ _ => rfl) (fun _ _ _ zero _ => ⟨_, rfl, by cases zero <;> decide⟩) @@ -558,14 +585,14 @@ def ignoringArgs (v : CIL.W64) : List Value := [.scalar (.i64 v), .scalar (.i64 forwarder's own argument check needs. -/ theorem is_zero_ignoring_forwards : (isZeroContract.withArgs ignoringArgs).Holds Extracted.program isZeroIgnoring := - is_zero_holds.forward (loads := plainLoads [0]) (narrow := some .unsigned8) - isZeroIgnoring.found isZero.found ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) - (fun _ _ _ => plainLoads_unchanged _ _) + is_zero_holds.forward (loads := [0]) (narrow := some .unsigned8) + isZeroIgnoring.found isZero.found ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) + (fun _ _ _ => plainLoads_unchanged (by decide) _) (fun _ _ _ value member => by simp only [ignoringArgs, List.mem_cons, List.not_mem_nil, or_false] at member rcases member with same | same <;> subst same · exact .inl (List.mem_singleton_self _) - · exact .inr rfl) + · exact .inr ⟨rfl, rfl⟩) (fun _ _ _ => rfl) (fun _ _ _ zero _ => ⟨_, rfl, by cases zero <;> decide⟩) #print axioms is_zero_ignoring_forwards @@ -591,13 +618,13 @@ def pairArgs (pair : CIL.W64 × CIL.W64) : List Value := /-- `IsZeroSecond(v, w)` returns `w == 0`: the load of argument 1 picks `w`. -/ theorem is_zero_second_forwards : (secondZero.withArgs pairArgs).Holds Extracted.program isZeroSecond := - second_zero_holds.forward (loads := plainLoads [1]) (narrow := some .unsigned8) - isZeroSecond.found isZero.found ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) - (fun _ _ _ => plainLoads_unchanged _ _) + second_zero_holds.forward (loads := [1]) (narrow := some .unsigned8) + isZeroSecond.found isZero.found ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) + (fun _ _ _ => plainLoads_unchanged (by decide) _) (fun _ _ _ value member => by simp only [pairArgs, List.mem_cons, List.not_mem_nil, or_false] at member rcases member with same | same <;> subst same - · exact .inr rfl + · exact .inr ⟨rfl, rfl⟩ · exact .inl (List.mem_singleton_self _)) (fun _ _ _ => rfl) (fun _ _ _ zero _ => ⟨_, rfl, by cases zero <;> decide⟩) @@ -623,20 +650,22 @@ theorem is_set_holds : isSetContract.Holds Extracted.program isSet := by /-- `IsSetForward`'s narrowed load keeps a boolean, so it inherits `IsSet`'s contract. -/ theorem is_set_forwards : (isSetContract.withArgs isSetContract.args).Holds Extracted.program isSetForward := - is_set_holds.forward (loads := [(0, some .unsigned8)]) (narrow := some .unsigned8) - isSetForward.found isSet.found ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) + is_set_holds.forward (loads := [0]) (narrow := some .unsigned8) + isSetForward.found isSet.found ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) (fun flag _ _ pair member => by simp only [isSetContract, List.zip_cons_cons, List.zip_nil_left, List.mem_singleton] at member subst member - exact ⟨_, rfl, by cases flag <;> decide⟩) + exact .narrow rfl (by cases flag <;> decide)) (fun _ _ _ _ member => .inl member) (fun _ _ _ => rfl) (fun _ _ _ out _ => ⟨_, rfl, by cases out <;> decide⟩) #print axioms is_set_forwards -/-- A narrowed load of a value outside the narrow range would change it. -/ -theorem wide_load_changes : ¬ LoadsUnchanged (some .unsigned8) (.scalar (.i32 256)) := by +/-- `IsSetForward`'s narrowed load of a value outside the Boolean range would change it. -/ +theorem wide_load_changes : ¬ LoadsUnchanged Extracted.method14 0 (.scalar (.i32 256)) := by + unfold LoadsUnchanged + rw [show Extracted.method14.argumentAccess 0 = .value (some .unsigned8) from rfl] rintro ⟨bits, same, extended⟩ simp only [Value.scalar.injEq, CIL.Value.i32.injEq] at same subst same @@ -668,22 +697,6 @@ def sampleInput : AddInput := ⟨⟨0, 0⟩, ⟨0, 8⟩, ⟨0, 16⟩, ⟨0, 0⟩ theorem sample_pre : addPre sampleInput sample := ⟨rfl, rfl, rfl⟩ -theorem raises_unique {program : CIL.Program} {method : Nat} {args : List Value} {memory : Memory} - {one other : String} (first : Raises program method args memory one) - (second : Raises program method args memory other) : one = other := by - obtain ⟨_, fault, ran, thrown⟩ := first - obtain ⟨_, fault', ran', thrown'⟩ := second - cases invoke_unique (by simp [Settled, thrown]) (by simp [Settled, thrown']) ran ran' - rw [thrown] at thrown' - cases thrown' - rfl - -theorem raises_not_returns {program : CIL.Program} {method fuel : Nat} {args : List Value} {memory : Memory} - {exception : String} {result : Memory × List Value} (raised : Raises program method args memory exception) - (returned : invoke program fuel method args memory = .ok result) : False := by - obtain ⟨_, fault, ran, thrown⟩ := raised - cases invoke_unique (second := .ok result) (by simp [Settled, thrown]) trivial ran returned - theorem percent_returns {v : CIL.W64} (within : v ≤ 100) (m : Memory) : ∃ fuel final, invoke Extracted.program fuel percent [.scalar (.i64 v)] m = .ok (final, [.scalar (.i64 v)]) := by obtain ⟨fuel, final, values, invoked, -, out, encoded, same⟩ := @@ -702,14 +715,14 @@ theorem stronger_post_not_refines : ¬ percentExact.Refines percentTooStrong := have bound : (100 : CIL.W64) < 100 := refines.post 100 sample sample 100 trivial (by decide) (Confined.refl _ _) rfl exact absurd bound (by decide) -theorem stronger_post_rejected : ¬ percentTooStrong.Holds Extracted.program percent := fun holds => by - have outcome := holds 100 sample trivial - simp only [percentTooStrong, percentExact, show (100 : CIL.W64) ≤ 100 by decide, ite_true] at outcome - obtain ⟨fuel, final, values, invoked, -, out, rfl, bound⟩ := outcome - obtain ⟨fuel', final', invoked'⟩ := percent_returns (v := 100) (by decide) sample - have same := invoke_ok_unique invoked invoked' - simp only [Prod.mk.injEq, List.cons.injEq, Value.scalar.injEq, CIL.Value.i64.injEq, and_true] at same - rw [same.2] at bound +theorem stronger_post_rejected : percentTooStrong.Refuted Extracted.program percent := by + obtain ⟨_, _, invoked⟩ := percent_returns (v := 100) (by decide) sample + refine .of_return (input := 100) (initial := sample) trivial invoked ?_ + simp only [OutcomeContract.Violates, MethodContract.Violates, percentTooStrong, percentExact, + show (100 : CIL.W64) ≤ 100 by decide, ite_true] + rintro ⟨-, out, encoded, bound⟩ + simp only [List.cons.injEq, Value.scalar.injEq, CIL.Value.i64.injEq, and_true] at encoded + rw [← encoded] at bound exact absurd (show (100 : CIL.W64) < 100 from bound) (by decide) /-- A weaker pre: `Percent(101)` raises, so it cannot be promised to return. -/ @@ -719,9 +732,11 @@ theorem weaker_pre_not_refines : ¬ percentInRange.Refines percentUnguarded := f have bound : (101 : CIL.W64) ≤ 100 := refines.pre 101 sample trivial exact absurd bound (by decide) -theorem weaker_pre_rejected : ¬ percentUnguarded.Holds Extracted.program percent := fun holds => by - obtain ⟨fuel, ⟨final, values⟩, invoked, -⟩ := holds 101 sample trivial - exact raises_not_returns (percent_exact.raising (input := 101) (exception := "System.ArgumentOutOfRangeException") trivial (by decide)) invoked +theorem weaker_pre_rejected : percentUnguarded.Refuted Extracted.program percent := by + obtain ⟨_, _, failed, thrown⟩ := percent_exact.raising (input := 101) (initial := sample) + (exception := .argumentOutOfRange) trivial (by decide) + exact .of_fault (input := 101) (initial := sample) trivial + (by simp [thrown, ExecutionFault.definite]) failed /-- A smaller footprint: `AddTo` changes `res`, which an empty footprint keeps. -/ def addUnframed : MethodContract AddInput := { addInternal with footprint := fun _ => [] } @@ -730,13 +745,11 @@ theorem smaller_footprint_not_refines : ¬ addInternal.Refines addUnframed := fu obtain ⟨_, member, _⟩ := refines.footprint sampleInput sample sample_pre _ (List.mem_singleton_self _) exact nomatch member -theorem smaller_footprint_rejected : ¬ addUnframed.Holds Extracted.program addTo := fun holds => by - obtain ⟨_, ⟨final, values⟩, invoked, framed, -⟩ := holds sampleInput sample sample_pre - obtain ⟨_, ⟨final', values'⟩, invoked', -, _, rfl, -, readback⟩ := +theorem smaller_footprint_rejected : addUnframed.Refuted Extracted.program addTo := by + obtain ⟨_, ⟨final, values⟩, invoked, -, _, rfl, -, readback⟩ := add_internal sampleInput sample sample_pre - have same := congrArg Prod.fst (invoke_ok_unique invoked invoked') - dsimp only at same - subst same + refine .of_return (input := sampleInput) (initial := sample) sample_pre invoked ?_ + rintro ⟨framed, -⟩ have kept := framed.read_eq sampleInput.res (by decide) 8 1 fun _ _ => Outside.nil _ _ have zero : read sample sampleInput.res 8 1 = .ok (numberBytes 0 8) := rfl rw [readback, zero] at kept @@ -744,15 +757,17 @@ theorem smaller_footprint_rejected : ¬ addUnframed.Holds Extracted.program addT /-- A changed exception type: `Percent(101)` throws `ArgumentOutOfRangeException`. -/ def percentRenamed : OutcomeContract CIL.W64 CIL.W64 := - { percentBounded with raises := fun v _ => if v ≤ 100 then none else some "System.ArgumentException" } + { percentBounded with raises := fun v _ => if v ≤ 100 then none else some .argument } theorem changed_exception_not_refines : ¬ percentExact.Refines percentRenamed := fun refines => absurd (refines.raises 101 sample trivial) (by decide) -theorem changed_exception_rejected : ¬ percentRenamed.Holds Extracted.program percent := fun holds => by - have renamed : Raises Extracted.program percent [.scalar (.i64 101)] sample "System.ArgumentException" := - holds.raising (input := 101) trivial (by decide) - exact absurd (raises_unique renamed (percent_exact.raising (input := 101) (exception := "System.ArgumentOutOfRangeException") trivial (by decide))) (by decide) +theorem changed_exception_rejected : percentRenamed.Refuted Extracted.program percent := by + obtain ⟨_, _, failed, thrown⟩ := percent_exact.raising (input := 101) (initial := sample) + (exception := .argumentOutOfRange) trivial (by decide) + refine .of_fault (input := 101) (initial := sample) trivial + (by simp [thrown, ExecutionFault.definite]) failed ?_ + simp [OutcomeContract.Violates, percentRenamed, thrown] /-- The callee contract over the first word of the pair. -/ def firstZero : MethodContract (CIL.W64 × CIL.W64) Bool := @@ -761,19 +776,19 @@ def firstZero : MethodContract (CIL.W64 × CIL.W64) Bool := post := fun pair _ _ zero => zero = decide (pair.1 = 0) } /-- Loading argument 0 instead of 1 does not match `IsZeroSecond`'s body. -/ -theorem first_not_forwarded : - ¬ Forwards isZeroSecondBody isZeroBody (plainLoads [0]) isZero (some .unsigned8) := +theorem first_not_forwarded (method : CIL.Extraction.MethodRef) : + ¬ Forwards isZeroSecondBody isZeroBody [0] method isZero (some .unsigned8) := fun forwards => by have code := forwards.code - simp [isZeroSecondBody, Extracted.method12, forwarderCode, plainLoads] at code + simp [isZeroSecondBody, Extracted.method12, forwarderCode] at code /-- What loading argument 0 would derive is false: `IsZeroSecond(0, 7)` returns `false`. -/ theorem first_selection_rejected : - ¬ (firstZero.withArgs pairArgs).Holds Extracted.program isZeroSecond := fun holds => by - obtain ⟨_, ⟨_, _⟩, invoked, -, zero, rfl, decided⟩ := holds (0, 7) sample trivial - obtain ⟨_, ⟨_, _⟩, invoked', -, zero', rfl, decided'⟩ := + (firstZero.withArgs pairArgs).Refuted Extracted.program isZeroSecond := by + obtain ⟨_, ⟨_, _⟩, invoked, -, zero', rfl, decided'⟩ := is_zero_second_forwards (0, 7) sample trivial - have same := congrArg Prod.snd (invoke_ok_unique invoked invoked') + refine .of_return (input := (0, 7)) (initial := sample) trivial invoked ?_ + rintro ⟨-, zero, same, decided⟩ simp only [firstZero, secondZero, MethodContract.withArgs] at decided decided' same subst decided decided' exact absurd same (by decide) @@ -785,16 +800,10 @@ def danglingArgs (v : CIL.W64) : List Value := /-- Every premise but `covered` holds for `danglingArgs`, and the contract is false: the forwarder rejects the dangling argument the callee never sees. -/ theorem dangling_rejected : - ¬ (isZeroContract.withArgs danglingArgs).Holds Extracted.program isZeroIgnoring := - fun holds => by - obtain ⟨_, _, invoked, -⟩ := holds 0 sample trivial - obtain ⟨_, _, _, _, -, checked, -, -⟩ := invoke_success_setup invoked - obtain ⟨same, valid⟩ := checkedValues_valid _ _ _ checked - subst same - have dangling := valid (.reference (.address ⟨1, 0⟩)) - (by simp [MethodContract.withArgs, danglingArgs]) - have missing : form sample ⟨1, 0⟩ = .error .missingAllocation := rfl - exact absurd (missing.symm.trans dangling) nofun + (isZeroContract.withArgs danglingArgs).Refuted Extracted.program isZeroIgnoring := + .of_fault (input := 0) (initial := sample) trivial (by decide) + (rfl : invoke Extracted.program 64 isZeroIgnoring + ((isZeroContract.withArgs danglingArgs).args 0) sample = .error _) #print axioms first_not_forwarded #print axioms first_selection_rejected diff --git a/Tests/ContractDiscovery.cs b/Tests/ContractDiscovery.cs index a0f17d620..cfca50694 100644 --- a/Tests/ContractDiscovery.cs +++ b/Tests/ContractDiscovery.cs @@ -2,6 +2,10 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs #:include ../Extractor/RuntimeAssetSelection.cs @@ -37,7 +41,7 @@ bool Same(string? left, string right) => left is not null && string.Equals(Path. OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); File.WriteAllText(Path.Combine(producer, "Producer.csproj"), """ -net10.0 +net11.0 ImportFixture.Libtruetrue """); @@ -85,17 +89,17 @@ void Entry(string path, byte[] bytes) Entry(Package + ".nuspec", Encoding.UTF8.GetBytes(""" LeanCil.Discovery.Fixture1.0.0 LeanCILDiscovery fixture - + """)); - Entry("ref/net10.0/" + Library + ".dll", File.ReadAllBytes(reference)); + Entry("ref/net11.0/" + Library + ".dll", File.ReadAllBytes(reference)); Entry("lib/net9.0/" + Library + ".dll", variants["net9"]); - Entry("lib/net10.0/" + Library + ".dll", variants["portable"]); + Entry("lib/net11.0/" + Library + ".dll", variants["portable"]); foreach (string rid in new[] { "linux-x64", "linux-arm64", "unix-x64", "unix" }) - Entry("runtimes/" + rid + "/lib/net10.0/" + Library + ".dll", variants[rid]); + Entry("runtimes/" + rid + "/lib/net11.0/" + Library + ".dll", variants[rid]); Entry("contracts/" + digest + ".leancil.contracts", Encoding.UTF8.GetBytes(contract.ToJsonString())); } File.WriteAllText(Path.Combine(consumer, "Consumer.csproj"), """ -net10.0 +net11.0 ImportFixture.Apptrue falsetrue @@ -146,7 +150,7 @@ void Rejected(string name, string marker, (int Code, string Output, JsonElement? if (outcome.Code != 1 || !outcome.Output.Contains(marker, StringComparison.Ordinal)) throw new InvalidOperationException(name + " did not fail at the intended resolver guard: " + outcome.Output); } -string RidAsset(string rid) => Path.Combine(bin, "runtimes", rid, "lib", "net10.0", Library + ".dll"); +string RidAsset(string rid) => Path.Combine(bin, "runtimes", rid, "lib", "net11.0", Library + ".dll"); // NuGet's RuntimeGraph.ExpandRuntime and the .NET 8+ host list are breadth-first: // osx-x64 -> osx, unix-x64, unix and linux-musl-x64 -> linux-musl, linux-x64. A depth-first @@ -176,11 +180,15 @@ void Rejected(string name, string marker, (int Code, string Output, JsonElement? Directory.CreateDirectory(Path.GetDirectoryName(relocated)!); File.WriteAllBytes(relocated, variants["linux-x64"]); JsonObject localDeps = Copy(deps); -EachLibrary(localDeps, library => library["runtimeTargets"]!["runtimes/linux-x64/lib/net10.0/" + Library + ".dll"]!["localPath"] = "alt/" + Library + ".dll"); +EachLibrary(localDeps, library => + library["runtimeTargets"]!["runtimes/linux-x64/lib/net11.0/" + Library + ".dll"]!["localPath"] = + "alt/" + Library + ".dll"); Selected("linux-x64", "linux-x64", relocated, Write("local.deps.json", localDeps)); -EachLibrary(localDeps, library => library["runtimeTargets"]!["runtimes/linux-x64/lib/net10.0/" + Library + ".dll"]!["localPath"] = "../" + Library + ".dll"); +EachLibrary(localDeps, library => + library["runtimeTargets"]!["runtimes/linux-x64/lib/net11.0/" + Library + ".dll"]!["localPath"] = + "../" + Library + ".dll"); Rejected("unsafe localPath", "Unsafe discovery asset path", Resolve("linux-x64", Write("unsafe-local.deps.json", localDeps))); -Console.WriteLine("PASS actual SDK deps/assets and portable RID graph select the host's net10.0 runtime asset"); +Console.WriteLine("PASS actual SDK deps/assets and portable RID graph select the host's net11.0 runtime asset"); Rejected("portable without RID", "Runtime identifier is required", Resolve(null)); Rejected("RID without graph", "requires --rid-graph", Resolve("linux-x64", ridGraph: null)); @@ -212,23 +220,23 @@ void EachLibrary(JsonObject document, Action edit) EachLibrary(document, library => { JsonObject targets = library["runtimeTargets"]!.AsObject(); - JsonNode linux = targets["runtimes/linux-x64/lib/net10.0/" + Library + ".dll"]!.DeepClone(); - targets.Remove("runtimes/linux-x64/lib/net10.0/" + Library + ".dll"); - targets["runtimes/linux-x64/lib/net10.0/Companion.dll"] = linux; + JsonNode linux = targets["runtimes/linux-x64/lib/net11.0/" + Library + ".dll"]!.DeepClone(); + targets.Remove("runtimes/linux-x64/lib/net11.0/" + Library + ".dll"); + targets["runtimes/linux-x64/lib/net11.0/Companion.dll"] = linux; }); Rejected("per-library RID group", "no runtime implementation", Resolve("linux-x64", Write("group.deps.json", perLibraryDeps), Write("group.assets.json", perLibraryAssets))); JsonObject wrongFramework = Copy(deps); wrongFramework["runtimeTarget"]!["name"] = ".NETCoreApp,Version=v9.0"; -Rejected("wrong TFM", "requires net10.0", Resolve("linux-x64", Write("wrong-framework.deps.json", wrongFramework))); +Rejected("wrong TFM", "requires net11.0", Resolve("linux-x64", Write("wrong-framework.deps.json", wrongFramework))); JsonObject traversalDeps = Copy(deps), traversalAssets = Copy(assets); foreach (JsonObject document in new[] { traversalDeps, traversalAssets }) EachLibrary(document, library => { JsonObject targets = library["runtimeTargets"]!.AsObject(); - string path = "runtimes/linux-x64/lib/net10.0/" + Library + ".dll"; + string path = "runtimes/linux-x64/lib/net11.0/" + Library + ".dll"; JsonNode entry = targets[path]!.DeepClone(); targets.Remove(path); targets["../../" + Library + ".dll"] = entry; @@ -237,7 +245,8 @@ void EachLibrary(JsonObject document, Action edit) Resolve("linux-x64", Write("traversal.deps.json", traversalDeps), Write("traversal.assets.json", traversalAssets))); JsonObject conflictingAssets = Copy(assets); -EachLibrary(conflictingAssets, library => library["runtimeTargets"]!.AsObject().Remove("runtimes/linux-x64/lib/net10.0/" + Library + ".dll")); +EachLibrary(conflictingAssets, library => + library["runtimeTargets"]!.AsObject().Remove("runtimes/linux-x64/lib/net11.0/" + Library + ".dll")); Rejected("deps/assets disagreement", "Conflicting deps/NuGet runtime asset", Resolve("linux-x64", "", Write("conflict.assets.json", conflictingAssets))); JsonObject referenceDeps = Copy(deps), referenceAssets = Copy(assets); @@ -262,12 +271,12 @@ byte[] Attributed(byte[] assembly, string? advertised) metadata.Write(bytes); return bytes.ToArray(); } -string packagedReference = Path.Combine(packageRoot, "ref", "net10.0", Library + ".dll"); +string packagedReference = Path.Combine(packageRoot, "ref", "net11.0", Library + ".dll"); byte[] originalReference = File.ReadAllBytes(packagedReference); File.WriteAllBytes(packagedReference, Attributed(originalReference, new string('0', 64))); Rejected("reference/runtime digest disagreement", "Reference/runtime contract attribute mismatch", Resolve("linux-x64")); File.WriteAllBytes(packagedReference, originalReference); -string packagedRuntime = Path.Combine(packageRoot, "runtimes", "linux-x64", "lib", "net10.0", Library + ".dll"); +string packagedRuntime = Path.Combine(packageRoot, "runtimes", "linux-x64", "lib", "net11.0", Library + ".dll"); byte[] unadvertised = Attributed(variants["linux-x64"], null); foreach (string path in new[] { RidAsset("linux-x64"), packagedRuntime }) File.WriteAllBytes(path, unadvertised); (int Code, string Output, JsonElement? Result) referenceAdvertised = Resolve("linux-x64"); @@ -277,7 +286,7 @@ byte[] Attributed(byte[] assembly, string? advertised) Console.WriteLine("PASS resolver rejects missing RID or graph, cross-architecture and malformed graphs, and unsafe or ambiguous manifests"); // Certify the resolved linux-x64 import; the resolver output is locations, the pins come separately. -string selectedImplementation = Path.Combine(packageRoot, "runtimes", "linux-x64", "lib", "net10.0", Library + ".dll"); +string selectedImplementation = Path.Combine(packageRoot, "runtimes", "linux-x64", "lib", "net11.0", Library + ".dll"); string artifact = Path.Combine(root, "ProducerArtifact"); Run([.. cli, "extract", selectedImplementation, "ImportFixture.Library::AddOne(ulong)", artifact]); File.WriteAllText(Path.Combine(artifact, "verification.json"), JsonSerializer.Serialize(new @@ -306,7 +315,8 @@ string[] Extract(string output, string hash) => [.. cli, "extract", application, JsonElement binding = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(selectedOutput, "identity.json"))).GetProperty("imports")[0]; if (import.GetProperty("implementationSha256").GetString() != Hash(variants["linux-x64"]) || !Same(ExtractionIdentity.Resolve(selectedOutput, binding.GetProperty("implementationPath").GetString()!), RidAsset("linux-x64")) || import.GetProperty("interfaceDigest").GetString() != digest) - throw new InvalidOperationException("Report did not name the resolved net10.0 linux-x64 implementation: " + import); + throw new InvalidOperationException( + "Report did not name the resolved net11.0 linux-x64 implementation: " + import); (int Code, string Output, string Stdout, string Stderr) wrongPin = Execute(Extract(Path.Combine(root, "WrongPin"), Hash(variants["linux-arm64"]))); if (wrongPin.Code != 1 || !wrongPin.Output.Contains("implementation SHA-256 mismatch", StringComparison.Ordinal) || File.Exists(Path.Combine(root, "WrongPin", "Extracted.lean"))) diff --git a/Tests/ContractExport.cs b/Tests/ContractExport.cs index 83155d23b..7eee336d0 100644 --- a/Tests/ContractExport.cs +++ b/Tests/ContractExport.cs @@ -2,6 +2,10 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs #:include ../scripts/ExportVerificationSuites.cs @@ -35,10 +39,13 @@ Directory.CreateDirectory(package); Directory.CreateDirectory(callerPackage); File.WriteAllText(Path.Combine(libraryRoot, "Lib.csproj"), """ -net10.0ImportFixture.Libtrue +net11.0 +ImportFixture.Libtrue """); File.WriteAllText(Path.Combine(appRoot, "App.csproj"), """ -net10.0ImportFixture.Apptrue +net11.0 +ImportFixture.Apptrue + """); string librarySource = Path.Combine(libraryRoot, "Library.cs"); File.WriteAllText(librarySource, "namespace ImportFixture; public static class Library { public static ulong AddOne(ulong value) => value + 1; public static ulong AddSame(ulong value) => value + 1; " + @@ -189,11 +196,16 @@ void RejectExport(JsonObject manifest, string marker, string? previous) inventory["leanCilRevision"]?.GetValue() != revision) throw new InvalidOperationException("Source bundle did not record its framework key and LeanCIL revision: " + inventory.ToJsonString()); -string CallerVerify() +void ExtractCaller() { Run([.. cli, "extract", app, "ImportFixture.Application::Entry(ulong)", callerArtifact, "--import", sidecar, "--import-implementation", "ImportFixture.Lib=" + library, "--import-sha256", "ImportFixture.Lib=" + Sha(library), "--import-interface-sha256", "ImportFixture.Lib=" + interfaceDigest]); +} + +string CallerVerify() +{ + ExtractCaller(); return Run([.. cli, "verify", callerArtifact, "--proof", callerProof, "--proof-root", callerPackage]); } @@ -230,7 +242,8 @@ string CallerVerify() CallerVerify(); var report = JsonNode.Parse(File.ReadAllText(Path.Combine(callerArtifact, "verified.json")))!; - if (report["schema"]!.GetValue() != 4 || report["contractImports"]!.AsArray().Count != 1 || + if (report["schema"]!.GetValue() != CertificateSchema || + report["contractImports"]!.AsArray().Count != 1 || report["imports"]![0]!["acceptedVia"]!.GetValue() != "published-certification" || !report["imports"]![0]!["deploymentObligation"]!.GetValue() .Contains("exact implementation bytes", StringComparison.Ordinal)) @@ -284,8 +297,8 @@ string CallerVerify() completed.CompleteCase("core.publication"); CallerVerify(); JsonElement initialCaller = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(callerArtifact, "verified.json"))); -if (initialCaller.GetProperty("schema").GetInt32() != 4) - throw new InvalidOperationException("Imported contract verification did not produce the schema-4 trust report."); +if (initialCaller.GetProperty("schema").GetInt32() != CertificateSchema) + throw new InvalidOperationException("Imported contract verification did not produce the current trust report."); completed.CompleteCase("core.caller"); Console.WriteLine("PASS verified Lib -> generated sidecar -> App, with a source-only complete bundle"); @@ -376,10 +389,10 @@ JsonObject RestrictedManifest(string assembly, string method, string folder) // A realizable x64 group still cannot masquerade as the built-in scalar profile. JsonObject profile = JsonNode.Parse(JsonSerializer.Serialize(new { - name = "scalar", architecture = "x64", nativeWidth = 64, littleEndian = true, advSimd = false, sse2 = true, ssse3 = false, - sse42 = false, avx = false, avx2 = false, avx512F = false, avx512FVL = false, bmi1 = false, sse41 = false, avx512DQ = false, - avx512DQVL = false, bmi2 = false, armBase64 = false, vector256Accelerated = false, x86Base64 = true, - vector128Accelerated = true, avx512Vbmi = false, aes = false, armAes = false + name = "scalar", architecture = "x64", nativeWidth = 64, littleEndian = true, advSimd = false, sse2 = true, + ssse3 = true, sse42 = true, avx = false, avx2 = false, avx512F = false, avx512FVL = false, bmi1 = false, + sse41 = true, avx512DQ = false, avx512DQVL = false, bmi2 = false, armBase64 = false, vector256Accelerated = false, + x86Base64 = true, vector128Accelerated = true, avx512Vbmi = false, aes = false, armAes = false }))!.AsObject(); File.WriteAllText(customProfile, profile.ToJsonString()); RejectExport(RestrictedManifest(library, "ImportFixture.Library::AddOne(ulong)", @@ -490,6 +503,59 @@ JsonObject RestrictedManifest(string assembly, string method, string folder) Console.WriteLine("PASS a failing library contract cannot replace a published sidecar"); completed.CompleteCase("core.incorrect-post"); +// Verify the caller's fixed postcondition under the original interface and warm its proof cache. +File.WriteAllText(contractSource, originalContractSource); +string callerSource = File.ReadAllText(callerProof); +File.AppendAllText(callerProof, """ + + namespace IncrementClient + open CIL.Safety + def exactIncrement : MethodContract CIL.W64 CIL.W64 := + { ImportFixture.addOne with post := fun input _ _ output => output = input + 1 } + theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold + (fun linked => exactIncrement.Holds linked 0) := ExampleProof.correctness + end IncrementClient + """); +string[] fixedCaller = [.. cli, "verify", callerArtifact, "--proof", callerProof, "--proof-root", callerPackage, + "--theorem", "IncrementClient.correctness"]; +ExtractCaller(); +Run(fixedCaller); +Run(fixedCaller); +string callerReport = Path.Combine(callerArtifact, "verified.json"); +if (JsonSerializer.Deserialize(File.ReadAllText(callerReport)) + .GetProperty("libraryBuild").GetString() != "consumer-proof-cache") + throw new InvalidOperationException("The fixed-postcondition caller did not warm its proof cache."); + +string unchangedImplementation = Sha(library); +File.WriteAllText(contractSource, originalContractSource.Replace("output = input + 1", + "output = input + 1 ∨ output = input + 2", StringComparison.Ordinal)); +File.WriteAllText(proof, File.ReadAllText(proof).Replace("case post => simp only [ImportFixture.addOne, CIL.importedPost]", + "case post => exact Or.inl rfl", StringComparison.Ordinal)); +Export(ProducerManifest()); +JsonObject revised = JsonNode.Parse(File.ReadAllText(sidecar))!.AsObject(); +var revisedInterface = revised["interfaceDigest"]!.GetValue(); +if (revisedInterface == interfaceDigest || revised["exports"]![0]!["closureDigest"]!.GetValue() == closureDigest || + revised["implementationSha256"]!.GetValue() != unchangedImplementation) + throw new InvalidOperationException("The revised postcondition must change both contract digests for the same DLL."); +interfaceDigest = revisedInterface; +ExtractCaller(); +var incompatible = Execute(fixedCaller); +if (incompatible.Code != 1 || !incompatible.Output.Contains("ExampleProof.correctness", StringComparison.Ordinal) || + !incompatible.Output.Contains("exactIncrement", StringComparison.Ordinal) || File.Exists(callerReport)) + throw new InvalidOperationException("Unexpected fixed caller result under the revised postcondition: " + + incompatible.Output); +File.WriteAllText(callerProof, callerSource); +Run([.. cli, "verify", callerArtifact, "--proof", callerProof, "--proof-root", callerPackage, + "--theorem", "ExampleProof.correctness"]); +var revisedReport = JsonSerializer.Deserialize(File.ReadAllText(callerReport)); +var import = revisedReport.GetProperty("imports").EnumerateArray().Single(); +if (revisedReport.GetProperty("theorem").GetString() != "ExampleProof.correctness" || + import.GetProperty("interfaceDigest").GetString() != revisedInterface || + import.GetProperty("implementationSha256").GetString() != unchangedImplementation) + throw new InvalidOperationException("The weaker caller receipt did not bind the revised contract and unchanged DLL."); +Console.WriteLine("PASS a revised imported contract invalidates the cached exact increment and admits the weaker caller"); +completed.CompleteCase("core.revised-contract"); + completed.Finish(); partial class Program diff --git a/Tests/ContractImports.cs b/Tests/ContractImports.cs index bf9ef2821..ad63b3a18 100644 --- a/Tests/ContractImports.cs +++ b/Tests/ContractImports.cs @@ -2,8 +2,14 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs +#:include GeneratedEvidence.cs +#:include DeepImportChecks.cs using System.Text.Json; using System.Text.Json.Nodes; @@ -188,6 +194,106 @@ string Transcript(JsonElement record) => File.ReadAllText(Path.Combine(trace, if (builds.Length != 2 || compiled != 1 || replayed < 2) throw new InvalidOperationException($"Interface reuse mismatch: {builds.Length} checks, {compiled} builds, {replayed} replays."); Console.WriteLine("PASS identical published interfaces compile once across fresh extraction groups and replay independently"); + var selected = requests[0]; + var pinManifest = Path.Combine(reuseRoot, "images.sha256"); + File.WriteAllText(pinManifest, Sha(app) + " " + Path.GetRelativePath(reuseRoot, app) + "\n"); + var deepInputs = DeepCheckInputs.Read(pinManifest); + var extractionIdentity = ExtractionIdentity.Read(selected.output); + if (deepInputs.MatchPins(extractionIdentity.Root).Count != 1 || extractionIdentity.Imports.Length == 0) + throw new InvalidOperationException( + "Imported-interface deep fixture lost its executable pin or typed import."); + using Program deepConsumer = new(); + var deepReport = deepConsumer.VerifyConsumer(app, proof, selected.output, "ExampleProof.correctness", + reusePackage, null, null, deepInputs); + if (deepReport.binding.mode != BindingMode.Deep || + deepReport.binding.facts.Single(fact => fact.id == BindingFact.BodyBytes).status != BindingStatus.Reconfirmed) + throw new InvalidOperationException("Consumer deep verification did not check the imported caller body."); + Console.WriteLine("PASS consumer deep audit checks the caller with an independently audited published interface"); + RequireDeepImports(deepConsumer.deepImports, "published-interface consumer"); + var reuseHits = deepConsumer.deepCacheHits; + deepConsumer.VerifyConsumer(app, proof, selected.output, "ExampleProof.correctness", reusePackage, null, null, + deepInputs); + if (deepConsumer.deepCacheHits != reuseHits + 1) + throw new InvalidOperationException("An unchanged published-interface deep request did not reuse its result."); + Console.WriteLine("PASS an unchanged published-interface deep request reuses its result"); + // Neither module is in the deep import closure, so a restored result meets the change only in later guards. + foreach (var changedModule in new[] { "ExtractedImports", interfaceModule }) + { + var changeToken = Guid.NewGuid().ToString("N"); + File.AppendAllText(proof, "\n-- " + changeToken + "\n"); + var beforeChange = Receipts(); + var publications = deepConsumer.publishedSuccess.Count; + var hits = deepConsumer.deepCacheHits; + bool changed = false; + string? diagnostic = null; + try + { + deepConsumer.VerifyConsumer(app, proof, selected.output, "ExampleProof.correctness", reusePackage, null, + stage => + { + // The key is read after this stage, so the change meets the restore decision. + if (stage != "deep-check sources" || changed) return; + var session = Directory.EnumerateDirectories(Path.GetTempPath(), "leancil-consumer-*") + .Single(candidate => File.Exists(Path.Combine(candidate, "LeanCILProof.lean")) && + File.ReadAllText(Path.Combine(candidate, "LeanCILProof.lean")) + .Contains(changeToken, StringComparison.Ordinal)); + SameLengthChange(Path.Combine(session, ".lake/packages/leancil_generated/.lake/build/lib/lean", + changedModule + ".olean")); + changed = true; + }, deepInputs); + } + catch (Exception error) when (error is InvalidOperationException or InvalidDataException && + error.Message.Contains("changed", StringComparison.Ordinal)) { diagnostic = error.Message; } + var afterChange = Receipts(); + if (!changed || diagnostic is null || deepConsumer.deepCacheHits != hits + 1 || + File.Exists(Path.Combine(selected.output, "verified.json")) || + deepConsumer.publishedSuccess.Count != publications || !Same(beforeChange, afterChange)) + throw new InvalidOperationException("A same-length change to " + changedModule + + " beside a restored deep result was not rejected without receipts: " + diagnostic + "; restored " + + (deepConsumer.deepCacheHits - hits) + ", report " + + File.Exists(Path.Combine(selected.output, "verified.json")) + ", published " + + string.Join(", ", deepConsumer.publishedSuccess.Skip(publications)) + ", changed " + + string.Join(", ", beforeChange.Keys.Union(afterChange.Keys).Where(path => + beforeChange.GetValueOrDefault(path) != afterChange.GetValueOrDefault(path)))); + Console.WriteLine("PASS a same-length " + changedModule + " change beside a restored deep result is " + + "rejected without receipts: " + diagnostic.Split('\n')[0]); + } + var token = Guid.NewGuid().ToString("N"); + File.AppendAllText(proof, "\n-- " + token + "\n"); + // Other sessions on the machine share the success cache, so only this driver's own entries are compared. + SortedDictionary Receipts() => HashFiles(deepConsumer.publishedSuccess + .Distinct(StringComparer.Ordinal).SelectMany(key => new[] { "receipt.json", "receipt.hmac" } + .Select(name => Path.Combine(SuccessCacheRoot, key, name))) + .Where(File.Exists).Select(path => (path, path))); + var receipts = Receipts(); + var publishedBefore = deepConsumer.publishedSuccess.Count; + bool mutated = false; + bool rejected = false; + try + { + deepConsumer.VerifyConsumer(app, proof, selected.output, "ExampleProof.correctness", reusePackage, null, + stage => + { + if (stage != "deep checks" || mutated) return; + var session = Directory.EnumerateDirectories(Path.GetTempPath(), "leancil-consumer-*") + .Single(candidate => File.Exists(Path.Combine(candidate, "LeanCILProof.lean")) && + File.ReadAllText(Path.Combine(candidate, "LeanCILProof.lean")) + .Contains(token, StringComparison.Ordinal)); + var artifact = Path.Combine(session, ".lake/packages/leancil_generated/.lake/build/lib/lean", + interfaceModule + ".olean"); + if (!File.Exists(artifact)) + throw new InvalidOperationException("Accepted interface artifact is missing."); + File.WriteAllBytes(artifact + ".unexpected", [0]); + mutated = true; + }, deepInputs); + } + catch (InvalidOperationException error) when (error.Message.Contains("Compiled artifacts changed", + StringComparison.Ordinal)) { rejected = true; } + if (!mutated || !rejected || File.Exists(Path.Combine(selected.output, "verified.json")) || + deepConsumer.publishedSuccess.Count != publishedBefore || !Same(receipts, Receipts())) + throw new InvalidOperationException( + "Changed published interface escaped deep inventory or published a receipt."); + Console.WriteLine("PASS consumer deep audit rejects a new published-interface companion without receipts"); Reset(); } @@ -235,7 +341,7 @@ string Transcript(JsonElement record) => File.ReadAllText(Path.Combine(trace, File.WriteAllText(identityPath, currentIdentity); string generated = File.ReadAllText(Path.Combine(output, "Extracted.lean")); if (identity.GetProperty("schema").GetInt32() != 3 || identity.GetProperty("imports").GetArrayLength() != 1 || - !generated.Contains(".call 1 1", StringComparison.Ordinal) || generated.Contains(expectedHash, StringComparison.Ordinal) || + DirectLinks(generated, 1).Length == 0 || generated.Contains(expectedHash, StringComparison.Ordinal) || !File.ReadAllText(Path.Combine(output, "ExtractedImports.lean")).Contains("@_root_.CIL.Safety.MethodContract.Holds _ _ _root_.ImportFixture.addOne linked 1", StringComparison.Ordinal)) throw new InvalidOperationException("Import slot, conditional contract or implementation-independent extraction missing."); string deletedProofs = Path.Combine(directory, "deleted-library-proofs"); @@ -275,7 +381,8 @@ string Transcript(JsonElement record) => File.ReadAllText(Path.Combine(trace, if (!verified.Contains("Conditional verification: assumes _root_.CIL.Safety.MethodContract.Holds _root_.ImportFixture.addOne linked 1", StringComparison.Ordinal)) throw new InvalidOperationException("Verification did not disclose imported contract premises."); JsonElement report = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(output, "verified.json"))); -if (report.GetProperty("schema").GetInt32() != 4 || report.GetProperty("contractImports").GetArrayLength() != 1 || +if (report.GetProperty("schema").GetInt32() != CertificateSchema || + report.GetProperty("contractImports").GetArrayLength() != 1 || report.GetProperty("imports")[0].GetProperty("acceptedVia").GetString() != "published-certification" || !report.GetProperty("imports")[0].GetProperty("deploymentObligation").GetString()!.Contains("exact implementation bytes", StringComparison.Ordinal)) throw new InvalidOperationException("Verification report omitted imported contract premises."); @@ -865,7 +972,9 @@ void RejectReplayWindowSwap(string expectedDiagnostic, Func locate, Act }); } catch (Exception error) { rejected = error; } - if (!replaced) throw new InvalidOperationException("Precondition: verification never reached the interface replay window."); + if (!replaced) + throw new InvalidOperationException( + "Precondition: verification never reached the interface replay window: " + rejected?.Message); if (rejected is null || !rejected.Message.Contains(expectedDiagnostic, StringComparison.Ordinal) || File.Exists(Path.Combine(output, "verified.json"))) throw new InvalidOperationException("Incorrect rejection of an interface olean replaced before replay: " + rejected?.Message); @@ -874,9 +983,28 @@ void RejectReplayWindowSwap(string expectedDiagnostic, Func locate, Act try { Environment.SetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE", "true"); - RejectReplayWindowSwap("Published contract canonical closure digest mismatch: ImportFixture.addOne", () => Directory.EnumerateDirectories(output, "proof-*") - .Select(fresh => Path.Combine(fresh, interfaceModule + ".olean")).SingleOrDefault(File.Exists), - stage => { using Program single = new(); single.Verify(app, "", "", Path.Combine(fixture, "Caller.lean"), output, existingArtifacts: true, stage: stage); }); + // A directory a killed run left behind holds an interface olean too, and must not be mistaken for this run's. + string staleProof = Path.Combine(output, "proof-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(staleProof); + File.WriteAllBytes(Path.Combine(staleProof, interfaceModule + ".olean"), unauditedArtifact); + // This run's directory is the one the listing taken before the step lacks, whatever the file system's clock. + HashSet earlierProofs = [.. new DirectoryInfo(output).EnumerateDirectories("proof-*") + .Select(earlier => earlier.FullName)]; + try + { + RejectReplayWindowSwap("Published contract canonical closure digest mismatch: ImportFixture.addOne", + () => new DirectoryInfo(output).EnumerateDirectories("proof-*") + .Where(fresh => !earlierProofs.Contains(fresh.FullName)) + .Select(fresh => Path.Combine(fresh.FullName, interfaceModule + ".olean")) + .SingleOrDefault(File.Exists), + stage => + { + using Program single = new(); + single.Verify(app, "", "", Path.Combine(fixture, "Caller.lean"), output, existingArtifacts: true, + stage: stage); + }); + } + finally { Directory.Delete(staleProof, recursive: true); } Console.WriteLine("PASS single-file verification binds interface oleans before their closure audit"); // A token in the proof tells this session apart from concurrent consumer sessions. string token = Guid.NewGuid().ToString("N"); diff --git a/Tests/ContractImports/App/App.csproj b/Tests/ContractImports/App/App.csproj index b435f037c..b9bf4e216 100644 --- a/Tests/ContractImports/App/App.csproj +++ b/Tests/ContractImports/App/App.csproj @@ -1,6 +1,6 @@ - net10.0 + net11.0 ImportFixture.App true $(NoWarn);CS1591 diff --git a/Tests/ContractImports/App/Application.cs b/Tests/ContractImports/App/Application.cs index 3f0cd4aae..da2ef0266 100644 --- a/Tests/ContractImports/App/Application.cs +++ b/Tests/ContractImports/App/Application.cs @@ -6,3 +6,45 @@ public static class Application public static ulong Generic(ulong value) => Library.Identity(value); public static ulong Unrelated(ulong value) => value + 2; } + +internal class MetadataBase; + +internal sealed class MetadataRecursive : MetadataBase +{ + public static int Run() => 7; +} + +internal class MetadataPair; +internal class Depth0; +internal class Depth1 : MetadataPair; +internal class Depth2 : MetadataPair; +internal class Depth3 : MetadataPair; +internal class Depth4 : MetadataPair; +internal class Depth5 : MetadataPair; +internal class Depth6 : MetadataPair; +internal class Depth7 : MetadataPair; +internal class Depth8 : MetadataPair +{ + public static int Run() => 7; +} +internal class Depth9 : MetadataPair; +internal class Depth10 : MetadataPair +{ + public static int Run() => 7; +} +internal class Depth11 : MetadataPair; +internal class Depth12 : MetadataPair +{ + public static int Run() => 7; +} +internal class Depth13 : MetadataPair; +internal class Depth14 : MetadataPair +{ + public static int Run() => 7; +} + +internal sealed class MetadataDerived : MetadataBase +{ + public static int Run() => 7; + public static int First(ref (int, uint) value) => value.Item1; +} diff --git a/Tests/ContractImports/Caller.lean b/Tests/ContractImports/Caller.lean index 19426252c..115fafb65 100644 --- a/Tests/ContractImports/Caller.lean +++ b/Tests/ContractImports/Caller.lean @@ -7,7 +7,7 @@ open CIL.Safety theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => ImportFixture.addOne.Holds linked 0) := by intro linked links imported input memory formed - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : ImportFixture.addOne.Holds linked 1 := imported dsimp only [ImportFixture.addOne, MethodContract.Returns] apply WP.enter found (by rfl) (by rfl) diff --git a/Tests/ContractImports/DerivedAddProof.lean b/Tests/ContractImports/DerivedAddProof.lean index 76bc22cb9..4d836ed62 100644 --- a/Tests/ContractImports/DerivedAddProof.lean +++ b/Tests/ContractImports/DerivedAddProof.lean @@ -105,13 +105,11 @@ theorem smaller_footprint_not_refines : ¬ addInternal.Refines addUnframed := fu exact nomatch member /-- No proof of the empty footprint exists for the actual extraction. -/ -theorem smaller_footprint_rejected : ¬ addUnframed.Holds Extracted.program 0 := fun holds => by - obtain ⟨_, ⟨final, values⟩, invoked, framed, -⟩ := holds sampleInput sample sample_pre - obtain ⟨_, ⟨otherFinal, otherValues⟩, otherInvoked, -, _, rfl, -, readback⟩ := +theorem smaller_footprint_rejected : addUnframed.Refuted Extracted.program 0 := by + obtain ⟨_, ⟨final, values⟩, invoked, -, _, rfl, -, readback⟩ := add_internal sampleInput sample sample_pre - have same := congrArg Prod.fst (invoke_ok_unique invoked otherInvoked) - dsimp only at same - subst same + refine .of_return (input := sampleInput) (initial := sample) sample_pre invoked ?_ + rintro ⟨framed, -⟩ have kept := framed.read_eq sampleInput.sum (by decide) 8 1 fun _ _ => Outside.nil _ _ have zero : read sample sampleInput.sum 8 1 = .ok (numberBytes 0 8) := rfl rw [readback, zero] at kept diff --git a/Tests/ContractImports/DerivedInterface.lean b/Tests/ContractImports/DerivedInterface.lean index 9c66701bf..943abb933 100644 --- a/Tests/ContractImports/DerivedInterface.lean +++ b/Tests/ContractImports/DerivedInterface.lean @@ -9,8 +9,8 @@ open CIL.Safety /-- The exception `Percent` throws for an input: none up to 100, otherwise `ArgumentOutOfRangeException`. -/ -def percentRaises (value : CIL.W64) : Option String := - if value ≤ 100 then none else some "System.ArgumentOutOfRangeException" +def percentRaises (value : CIL.W64) : Option CIL.ExceptionType := + if value ≤ 100 then none else some .argumentOutOfRange /-- Public `Percent`: inputs above 100 throw `ArgumentOutOfRangeException`; a returned result is only promised to be at most 100, not equal to the input. -/ diff --git a/Tests/ContractImports/DerivedPercentProof.lean b/Tests/ContractImports/DerivedPercentProof.lean index 6f871f9ba..3e9319267 100644 --- a/Tests/ContractImports/DerivedPercentProof.lean +++ b/Tests/ContractImports/DerivedPercentProof.lean @@ -20,7 +20,7 @@ def percentExact : OutcomeContract CIL.W64 CIL.W64 where pre _ _ := True footprint _ := [] post value _ _ output := output = value - raises value _ := if value ≤ 100 then none else some "System.ArgumentOutOfRangeException" + raises value _ := if value ≤ 100 then none else some .argumentOutOfRange /-- Each input returns itself or throws the named exception. -/ theorem percent_exact : percentExact.Holds Extracted.program 0 := by @@ -61,29 +61,6 @@ theorem correctness : DerivedInterface.percentBounded.Holds Extracted.program 0 derive is false of the actual extraction. -/ namespace Rejected -/-- Two outcomes of one invocation that both raise name the same exception. -/ -theorem raises_unique {method : Nat} {args : List Value} {memory : Memory} - {one other : String} (first : Raises Extracted.program method args memory one) - (second : Raises Extracted.program method args memory other) : one = other := by - obtain ⟨_, fault, ran, thrown⟩ := first - obtain ⟨_, otherFault, otherRan, otherThrown⟩ := second - cases invoke_unique (by simp [Settled, thrown]) (by simp [Settled, otherThrown]) ran otherRan - rw [thrown] at otherThrown - cases otherThrown - rfl - -#print axioms raises_unique - -/-- An invocation that raises never also returns. -/ -theorem raises_not_returns {method fuel : Nat} {args : List Value} {memory : Memory} - {exception : String} {result : Memory × List Value} - (raised : Raises Extracted.program method args memory exception) - (returned : invoke Extracted.program fuel method args memory = .ok result) : False := by - obtain ⟨_, fault, ran, thrown⟩ := raised - cases invoke_unique (second := .ok result) (by simp [Settled, thrown]) trivial ran returned - -#print axioms raises_not_returns - /-- `Percent(100)` returns 100. -/ theorem percent_hundred : ∃ fuel final, invoke Extracted.program fuel 0 [.scalar (.i64 100)] Memory.empty = @@ -98,7 +75,7 @@ theorem percent_hundred : ∃ fuel final, /-- `Percent(101)` raises `ArgumentOutOfRangeException`. -/ theorem percent_above : Raises Extracted.program 0 [.scalar (.i64 101)] Memory.empty - "System.ArgumentOutOfRangeException" := + .argumentOutOfRange := percent_exact.raising (input := 101) trivial (by decide) #print axioms percent_above @@ -115,15 +92,15 @@ theorem stronger_post_not_refines : ¬ percentExact.Refines percentTooStrong := exact absurd bound (by decide) /-- No proof of the stronger post exists for the actual extraction. -/ -theorem stronger_post_rejected : ¬ percentTooStrong.Holds Extracted.program 0 := fun holds => by - obtain ⟨fuel, final, values, invoked, -, output, rfl, bound⟩ := - holds.returning (input := 100) (initial := Memory.empty) Memory.empty_wellFormed (by decide) - obtain ⟨otherFuel, otherFinal, otherInvoked⟩ := percent_hundred - have same := invoke_ok_unique invoked otherInvoked - simp only [percentTooStrong, DerivedInterface.percentBounded, Prod.mk.injEq, List.cons.injEq, - Value.scalar.injEq, CIL.Value.i64.injEq, and_true] at same bound - rw [same.2] at bound - exact absurd bound (by decide) +theorem stronger_post_rejected : percentTooStrong.Refuted Extracted.program 0 := by + obtain ⟨_, _, invoked⟩ := percent_hundred + refine .of_return (input := 100) (initial := Memory.empty) Memory.empty_wellFormed invoked ?_ + simp only [OutcomeContract.Violates, MethodContract.Violates, percentTooStrong, + DerivedInterface.percentBounded, show (100 : CIL.W64) ≤ 100 by decide, ite_true] + rintro ⟨-, output, encoded, bound⟩ + simp only [List.cons.injEq, Value.scalar.injEq, CIL.Value.i64.injEq, and_true] at encoded + subst encoded + exact absurd (show (100 : CIL.W64) < 100 from bound) (by decide) /-- `percentExact` with a pre that excludes every throwing input, so it names no exception. -/ def percentInRangeOutcome : OutcomeContract CIL.W64 CIL.W64 := @@ -158,27 +135,27 @@ theorem weaker_pre_not_refines : ¬ percentInRange.Refines percentUnguarded := f exact absurd bound (by decide) /-- No proof of the weaker pre exists for the actual extraction. -/ -theorem weaker_pre_rejected : ¬ percentUnguarded.Holds Extracted.program 0 := fun holds => by - obtain ⟨fuel, ⟨final, values⟩, invoked, -⟩ := holds 101 Memory.empty trivial - exact raises_not_returns percent_above invoked +theorem weaker_pre_rejected : percentUnguarded.Refuted Extracted.program 0 := by + obtain ⟨_, _, failed, thrown⟩ := percent_above + exact .of_fault (input := 101) (initial := Memory.empty) trivial + (by simp [thrown, ExecutionFault.definite]) failed /-- A changed exception type: `Percent(101)` throws `ArgumentOutOfRangeException`, not `ArgumentException`. -/ def percentRenamed : OutcomeContract CIL.W64 CIL.W64 := { DerivedInterface.percentBounded with - raises := fun value _ => if value ≤ 100 then none else some "System.ArgumentException" } + raises := fun value _ => if value ≤ 100 then none else some .argument } /-- The exact contract does not refine a renamed exception. -/ theorem changed_exception_not_refines : ¬ percentExact.Refines percentRenamed := fun refines => absurd (refines.raises 101 Memory.empty Memory.empty_wellFormed) (by decide) /-- No proof of the renamed exception exists for the actual extraction. -/ -theorem changed_exception_rejected : ¬ percentRenamed.Holds Extracted.program 0 := - fun holds => by - have renamed : Raises Extracted.program 0 [.scalar (.i64 101)] Memory.empty - "System.ArgumentException" := - holds.raising (input := 101) Memory.empty_wellFormed (by decide) - exact absurd (raises_unique renamed percent_above) (by decide) +theorem changed_exception_rejected : percentRenamed.Refuted Extracted.program 0 := by + obtain ⟨_, _, failed, thrown⟩ := percent_above + refine .of_fault (input := 101) (initial := Memory.empty) Memory.empty_wellFormed + (by simp [thrown, ExecutionFault.definite]) failed ?_ + simp [OutcomeContract.Violates, percentRenamed, thrown] -- An explicit `sorry` is only a warning, so each refutation's axioms are pinned to fail the build. /-- info: 'DerivedPercentProof.Rejected.stronger_post_not_refines' diff --git a/Tests/ContractImports/DerivedShiftCaller.lean b/Tests/ContractImports/DerivedShiftCaller.lean index 75aec8eef..25dd57bb2 100644 --- a/Tests/ContractImports/DerivedShiftCaller.lean +++ b/Tests/ContractImports/DerivedShiftCaller.lean @@ -21,7 +21,7 @@ def shiftContract : OutcomeContract CIL.W64 CIL.W64 where theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => shiftContract.Holds linked 0) := by intro linked links imported value memory formed - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : DerivedInterface.percentBounded.Holds linked 1 := imported dsimp only [shiftContract] cases raised : DerivedInterface.percentRaises value with diff --git a/Tests/ContractImports/DerivedSumCaller.lean b/Tests/ContractImports/DerivedSumCaller.lean index c4eca0970..b015eff6d 100644 --- a/Tests/ContractImports/DerivedSumCaller.lean +++ b/Tests/ContractImports/DerivedSumCaller.lean @@ -13,7 +13,7 @@ theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => DerivedInterface.addPublic.Holds linked 0) := by rintro linked links imported ⟨left, right, sum, region, leftValue, rightValue⟩ memory ⟨formed, readLeft, readRight, writable, apart, within⟩ - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : DerivedInterface.addPublic.Holds linked 1 := imported have leftForm := access_reference_valid _ _ _ _ _ (read_access readLeft) have rightForm := access_reference_valid _ _ _ _ _ (read_access readRight) diff --git a/Tests/ContractImports/Lib.leancil.contracts b/Tests/ContractImports/Lib.leancil.contracts index 043c795bd..42fea069c 100644 --- a/Tests/ContractImports/Lib.leancil.contracts +++ b/Tests/ContractImports/Lib.leancil.contracts @@ -5,7 +5,7 @@ "frameworkKey": "FRAMEWORK", "assemblyIdentity": "ImportFixture.Lib, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null", "mvid": "00000000-0000-0000-0000-000000000000", - "targetFramework": ".NETCoreApp,Version=v10.0", + "targetFramework": ".NETCoreApp,Version=v11.0", "implementationSha256": "IMPLEMENTATION", "isReferenceAssembly": false, "interfaceModules": [{ "name": "ImportFixture", "source": "SOURCE" }], diff --git a/Tests/ContractImports/Lib/Lib.csproj b/Tests/ContractImports/Lib/Lib.csproj index f8ddfd5db..26d4db2e9 100644 --- a/Tests/ContractImports/Lib/Lib.csproj +++ b/Tests/ContractImports/Lib/Lib.csproj @@ -1,6 +1,6 @@ - net10.0 + net11.0 ImportFixture.Lib enable true diff --git a/Tests/ContractImports/ProfileFamilyCaller.lean b/Tests/ContractImports/ProfileFamilyCaller.lean index 9b0b8abf5..b46746acc 100644 --- a/Tests/ContractImports/ProfileFamilyCaller.lean +++ b/Tests/ContractImports/ProfileFamilyCaller.lean @@ -9,7 +9,7 @@ open CIL.Safety theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => FamilyContract.supported.Holds linked 0) := by intro linked links imported input memory formed - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : FamilyContract.supported.Holds linked 1 := imported dsimp only [FamilyContract.supported, MethodContract.Returns] apply WP.enter found (by rfl) (by rfl) diff --git a/Tests/ContractImports/ProfileFamilyProof.lean b/Tests/ContractImports/ProfileFamilyProof.lean index bd1a5d1d0..79a4ba3ac 100644 --- a/Tests/ContractImports/ProfileFamilyProof.lean +++ b/Tests/ContractImports/ProfileFamilyProof.lean @@ -7,7 +7,7 @@ import CIL.Safety.SymbolicExecution namespace FamilyProof open CIL CIL.Safety -/-- The reference extraction's methods all use the selected SSE2 profile. -/ +/-- The reference extraction's methods all use the selected SSE4.2 profile. -/ theorem uniform : ∀ body ∈ Reference.program, body.profile = Reference.profile := by decide #print axioms uniform diff --git a/Tests/ContractImports/TypedChainCaller.lean b/Tests/ContractImports/TypedChainCaller.lean index 3d454962e..7309e211e 100644 --- a/Tests/ContractImports/TypedChainCaller.lean +++ b/Tests/ContractImports/TypedChainCaller.lean @@ -13,20 +13,20 @@ open CIL.Safety theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => TypedImportInterface.copy.Holds linked 0) := by rintro linked links imported ⟨source, destination, value⟩ memory ⟨formed, readable, writable⟩ - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : TypedImportInterface.copy.Holds linked 1 := imported obtain ⟨middle, entered, made, middleWritable, -⟩ := make_fitting_local memory memory.nextIdentity (.aggregate 8) (.aggregate (List.replicate 8 0)) formed nofun (fun _ _ => nofun) (fun _ => nofun) (fun _ => nofun) (fun _ => nofun) nofun (Or.inr rfl) let frame : Frame := ⟨memory.nextIdentity, [.bytes (.aggregate 8) middle], [middle.allocation], - [], 0⟩ + 0⟩ have setup : enterFrame Extracted.method0 [.reference (.address source), .reference (.address destination)] memory = .ok (frame, entered) := by simp only [enterFrame, Extracted.method0_opaqueVectorLocals, makeSelectedLocals, List.isEmpty_nil, ite_true, Extracted.method0_localKinds, Extracted.method0_locals, - makeLocals, made, Extracted.method0_vectorArgs, makeArgumentHomes, + makeLocals, made, Extracted.method0_parameterHomes, makeParameterHomes, List.map_nil, Bind.bind, Except.bind, Pure.pure, Except.pure, List.append_nil, frame] have owned := enterFrame_fresh _ _ _ _ _ setup diff --git a/Tests/ContractImports/TypedCopyCaller.lean b/Tests/ContractImports/TypedCopyCaller.lean index 02fb8185b..b88edba21 100644 --- a/Tests/ContractImports/TypedCopyCaller.lean +++ b/Tests/ContractImports/TypedCopyCaller.lean @@ -6,7 +6,7 @@ open CIL.Safety theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => TypedImportInterface.copy.Holds linked 0) := by rintro linked links imported ⟨source, destination, value⟩ memory ⟨formed, readable, writable⟩ - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : TypedImportInterface.copy.Holds linked 1 := imported have sourceForm := access_reference_valid _ _ _ _ _ (read_access readable) have destinationForm := access_reference_valid _ _ _ _ _ writable diff --git a/Tests/ContractImports/TypedIdentityCaller.lean b/Tests/ContractImports/TypedIdentityCaller.lean index aafcf8b50..ac263ad03 100644 --- a/Tests/ContractImports/TypedIdentityCaller.lean +++ b/Tests/ContractImports/TypedIdentityCaller.lean @@ -6,7 +6,7 @@ open CIL.Safety theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => TypedImportInterface.identity.Holds linked 0) := by intro linked links imported input memory formed - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : TypedImportInterface.identity.Holds linked 1 := imported dsimp only [TypedImportInterface.identity, MethodContract.Returns] apply WP.enter found (by rfl) (by rfl) diff --git a/Tests/ContractVariants.cs b/Tests/ContractVariants.cs new file mode 100644 index 000000000..fa97d38f2 --- /dev/null +++ b/Tests/ContractVariants.cs @@ -0,0 +1,56 @@ +using System.Text.Json; + +partial class Program +{ + /// Checks one contract against the positive entry and a witness against the wrong entry. + void VerifyContractVariant(string assembly, string identity, string selector, string positiveOutput, + string wrongAssembly, string wrongSelector, string wrongOutput, string contract, string positive, + string counterexample, string profile = "scalar", string? example = null, string imports = "") + { + string Proof(string output, string theorem, string source) + { + Directory.CreateDirectory(output); + var path = Path.Combine(output, "ContractVariant.lean"); + File.WriteAllText(path, $$""" + import CIL.Safety.MethodContract + {{imports}} + {{source}} + + /-! The extracted entry's contract and its verification at this implementation. -/ + + open CIL (W32 W64) + open CIL.Safety + namespace VariantProof + + {{contract}} + + {{theorem}} + #print axioms correctness + end VariantProof + + """); + return path; + } + + const string theorem = "VariantProof.correctness"; + var source = example is null ? "import Extracted" : File.ReadAllText(example); + if (source.StartsWith("import Extracted.", StringComparison.Ordinal)) + source = $"import Extracted{source[source.IndexOf('\n')..]}"; + Verify(assembly, identity, selector, Proof(positiveOutput, positive, source), positiveOutput, profile, + theorem: theorem); + var wrongProof = Proof(wrongOutput, counterexample, "import Extracted"); + VerifyRefutation(wrongAssembly, identity, wrongSelector, wrongProof, wrongOutput, profile, theorem); + } + + /// The checked counterexample must name the entry in this extraction. + void VerifyRefutation(string assembly, string identity, string selector, string proof, string output, + string profile, string theorem) + { + var result = Verify(assembly, identity, selector, proof, output, profile, theorem: theorem); + using var manifest = JsonDocument.Parse(File.ReadAllText(Path.Combine(output, "identity.json"))); + var entry = manifest.RootElement.GetProperty("entry").GetString() + ?? throw new InvalidDataException("The extraction has no entry signature."); + if (result.refutation is not { } refutation || refutation.method.signature != entry) + throw new InvalidOperationException($"Expected a verified refutation of {entry}."); + } +} diff --git a/Tests/CoreExamples.cs b/Tests/CoreExamples.cs index da78690c0..9c456d9c0 100644 --- a/Tests/CoreExamples.cs +++ b/Tests/CoreExamples.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include ../scripts/CoreExamples.cs @@ -8,6 +13,7 @@ using System.Text.Json; using System.Text.RegularExpressions; +// Standalone runner of the core example proofs; the core suite runs them through scripts/CoreExamples.cs. if (args is ["--check-existing", var existing]) { using var checker = new Program(); diff --git a/Tests/Crypto.lean b/Tests/Crypto.lean new file mode 100644 index 000000000..9b6eac803 --- /dev/null +++ b/Tests/Crypto.lean @@ -0,0 +1,59 @@ +import CIL.Crypto.Sha1 +import Lean + +/-! +# Core hash operations + +SHA-1 is available without the image audit library. Reference digests cover increasing key +lengths, and repeated evaluations check that hashing cost grows linearly with the byte count. +-/ + +namespace Tests.Crypto + +open CIL.Crypto + +run_elab + if (← Lean.getEnv).contains `StrictBinding.ImageBytes then + throwError "Core SHA-1 imports the image audit library" + +/-- The empty message has the FIPS 180-4 SHA-1 digest. -/ +theorem empty_digest : Sha1.ofBytes [] = 0xda39a3ee5e6b4b0d3255bfef95601890afd80709 := by + decide +kernel + +#print axioms empty_digest + +/-- The least of three timings for repeated hashing of a fixed byte pattern. -/ +def measureHash (bytes : List Nat) (expected iterations : Nat) : IO Nat := do + let mut best := 1000000000000 + for _ in [:3] do + let started <- IO.monoNanosNow + for _ in [:iterations] do + unless Sha1.ofBytes bytes == expected do + throw (IO.userError "SHA-1 differs from the reference digest") + best := min best ((<- IO.monoNanosNow) - started) + return best + +/-- Sixty-fourfold input growth stays below quadratic hashing cost. -/ +def checkHashCost : IO Unit := do + let cases := [(64, 0xc6138d514ffa2135bfce0ed0b8fac65669917ec7), + (512, 0xdbe649daba340bce7a44b809016d914839b99f10), + (4096, 0xe9dded8c84614e894501965af60c2525794a8c7d)] + let mut baseline := 0 + let mut iterations := 16 + let minimumNanos := 25000000 + for (count, expected) in cases do + let bytes := (List.range count).map (fun value => value % 256) + let mut elapsed <- measureHash bytes expected iterations + if count == 64 then + while elapsed < minimumNanos do + iterations := iterations * 2 + elapsed <- measureHash bytes expected iterations + baseline := elapsed + -- A quadratic hash costs 4096x at 64x bytes, above the 256x bound. + if count == 4096 && elapsed > baseline * 256 then + throw (IO.userError s!"SHA-1 cost grew too quickly: {baseline} -> {elapsed}") + IO.println s!"PASS SHA-1 {count} bytes: {elapsed} ns for {iterations} hashes" + +#eval checkHashCost + +end Tests.Crypto diff --git a/Tests/DebugNames.cs b/Tests/DebugNames.cs index 3b1aac805..2096864da 100644 --- a/Tests/DebugNames.cs +++ b/Tests/DebugNames.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // PDB names and locations only locate: extraction is byte-identical without a PDB, a proof // naming locals by scope survives an inserted local, and diagnostics gain `file:line:column`. @@ -44,7 +49,8 @@ string Build(string name, string inserted, string debugType) var source = Path.Combine(root, name); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Names.csproj"), $""" - net10.0true{debugType} + net11.0 + true{debugType} """); File.WriteAllText(Path.Combine(source, "Names.cs"), Source(inserted)); Run(["dotnet", "build", Path.Combine(source, "Names.csproj"), "-c", "Release"]); @@ -87,9 +93,9 @@ namespace ExampleProof -- Each block stores its call's result in its own `carry`, and the second block reads its own back. theorem correctness : - sumBody.code[atMix + 1]? = some (.setLocal {{slots[0]}}) ∧ - sumBody.code[atWide + 1]? = some (.setLocal {{slots[1]}}) ∧ - sumBody.code[atWide + 2]? = some (.local {{slots[2]}}) := + sumBody.code[atMix + 1]? = some (.stloc {{slots[0]}}) ∧ + sumBody.code[atWide + 1]? = some (.stloc {{slots[1]}}) ∧ + sumBody.code[atWide + 2]? = some (.ldloc {{slots[2]}}) := ⟨rfl, rfl, rfl⟩ end ExampleProof diff --git a/Tests/DebugNames.lean b/Tests/DebugNames.lean index 460b08245..465790d41 100644 --- a/Tests/DebugNames.lean +++ b/Tests/DebugNames.lean @@ -48,7 +48,8 @@ error: method 1 has no local named "borrow"; its named locals: example : local_named 1 "borrow" = 0 := rfl def body (target : Nat) : Method := - { code := [.arg 0, .arg 0, .brzero target, .arg 0, .ret, .arg 0, .ret], locals := [], returnsValue := true } + { code := [.ldarg 0, .ldarg 0, .brfalse target, .ldarg 0, .ret, .ldarg 0, .ret], locals := [], + returnsValue := true } def program : Program := [{ code := [.ret], locals := [], returnsValue := false }, body 5] -- A failing binding names the source of the first differing pc, after the unchanged report. @@ -56,8 +57,8 @@ def program : Program := [{ code := [.ret], locals := [], returnsValue := false error: cil_body: rfl could not prove the method-body binding method index 1 field code: first difference at index 2 (left length 7, right length 7) - left: CIL.Op.brzero 5 - right: CIL.Op.brzero 3 + left: CIL.Instruction.brfalse 5 + right: CIL.Instruction.brfalse 3 source of actual pc 2: src/Math.cs:10:9 -/ #guard_msgs (substring := true) in @@ -67,14 +68,15 @@ example : program[1]? = some (body 3) := by cil_body error: cil_embed: the expected body does not embed into the extracted method method index 1 expected pc 3 maps to actual pc 3 - expected: CIL.Op.const32 0x00000001#32 - actual: CIL.Op.arg 0 none + expected: CIL.Instruction.ldc_i4 0x00000001#32 + actual: CIL.Instruction.ldarg 0 the instructions differ expected pc 2 falls through to it, so it must be actual pc 3 source of actual pc 3: src/Math.cs:11:13 -/ #guard_msgs in -example : MethodEmbeds program 1 { body 5 with code := [.arg 0, .arg 0, .brzero 5, .const32 1, .ret, .arg 0, .ret] } := by cil_embed +example : MethodEmbeds program 1 { body 5 with code := [.ldarg 0, .ldarg 0, .brfalse 5, .ldc_i4 1, + .ret, .ldarg 0, .ret] } := by cil_embed -- Methods without sequence points add nothing. run_meta do diff --git a/Tests/DecodedMethod.lean b/Tests/DecodedMethod.lean new file mode 100644 index 000000000..a65c27bd7 --- /dev/null +++ b/Tests/DecodedMethod.lean @@ -0,0 +1,983 @@ +import CIL.Extraction.DecodedDefinition +import CIL.InstructionEquations +import CIL.Safety.ResolutionFacts +import CIL.Safety.SymbolicExecution +import Tests.BodyDecoding +import Tests.Support.ModelOperands + +/-! +# Decoded method tests + +Bodies built by `Tests.BodyEncoding.encodeBody` and by hand over `Tests.MetadataWorld` must +decode through `Method.ofBody?` to the method their bytes denote: the code `Instruction.ofRaw` +gives, locals initialized by the flag in the bytes rather than the definition's, and the code of +the bytes rather than the definition's. A stored argument takes a home. An instruction outside +the admitted families at a live site prevents method construction. A body the +check refuses, or one with an argument instruction naming an undeclared argument, gives no +method, and neither does a program with such a body. A method defined by `cil_decoded_method` +is the decoder's method, written as literals its facts and stepping read, with a zero-initialized +struct local written as `List.replicate size 0` so frame entry gives the term a caller's +hypothesis about making that local names. A call decodes to the method its operand names, with +the target the supplied link table gives its pc, and the call check rejects a link that names +another method. +-/ + +namespace Tests.DecodedMethod + +open CIL CIL.Extraction Tests.BodyEncoding Tests.BodyDecoding Tests.MetadataWorld + +/-- The mask for a fixture whose entire body is live. -/ +def allLive (raw : RawMethod) : List Bool := raw.code.map fun _ => true + +/-- The method of the bytes of `body` against the definition `original`, with no layouts. -/ +private def decoded (body : Option BodyEvidence) (original : RawMethod) : Option Method := + body.bind fun body => Method.ofBody? .scalar {} [] (allLive original) body original [] + +/-- `ldc.i4.1; ldc.i4.2; add; ret`. -/ +private def addRaw : RawMethod := + method 8 false [] [instr 0 0x17, instr 1 0x18, instr 2 0x58, instr 3 0x2A] + +/-- `ldc.i4.1; ldc.i4.2; sub; ret`. -/ +private def subRaw : RawMethod := + method 8 false [] [instr 0 0x17, instr 1 0x18, instr 2 0x59, instr 3 0x2A] + +/-- The frame both bodies declare: no locals, no homes, no arguments, no return value. -/ +private def frame (code : List Instruction) : Method := + { code := code, locals := [], + declared := { parameters := [], returnType := .void, initLocals := false }, + returnsValue := false } + +/-- The tiny add body decodes to its code and its declared frame. -/ +theorem add_decodes : + decoded (encodeBody pin addRaw) addRaw = some (frame [.ldc_i4 1, .ldc_i4 2, .add, .ret]) := by + cil_kernel_rfl +#print axioms add_decodes + +/-- The unsafe-call certificate requires a mask entry for every instruction. -/ +theorem decoded_missing_mask : + let source := { addRaw with code := [instr 0 0] } + ¬ UnsafeCallsValid source [] (methodCode .scalar {} [] [] source) := by decide +kernel +#print axioms decoded_missing_mask + +/-- A span reset decodes to `initobj`, and object admission refuses it without the address of a + local or parameter of its type before it. -/ +theorem decoded_unbound_span_reset : + let span := TypeExpr.apply (.constructor runtimeScope "System.Span`1") uint32 + let source := { addRaw with code := [instr 0 0xfe15 (.type span)] } + let code := methodCode .scalar {} [] [true] source + code = [.initobj span] ∧ objectSites source [true] (frame code) = none := by + exact ⟨rfl, rfl⟩ +#print axioms decoded_unbound_span_reset + +/-- A raw static access uses the owner, slot, held type and name encoded by its token. -/ +private def fieldSite (opcode : Nat) (field : FieldRef) : RawInstruction := + instr 0 opcode (.field field.owner field.index field.type field.name true) + +/-- The decoder binds a single field instruction under its profile and RVA evidence. -/ +private def fieldCode (opcode : Nat) (field : FieldRef) + (sites : List (Nat × StaticFieldSite) := []) (profile := FeatureProfile.scalar) : + Except RawDecodeError (List Instruction) := + let source := { addRaw with code := [fieldSite opcode field], staticFields := sites } + (decodeRaw profile {} [] [true] source []).map Method.code + +/-- The constant requires the exact runtime field and a little-endian profile. -/ +theorem endianFieldDecodes : + let field := littleEndianField + let wrong := [{ field with owner := .atom "Foreign" "System.BitConverter" }, + { field with owner := .atom runtimeScope "System.Other" }, + { field with name := name!"Other" }, { field with type := int32 }] + fieldCode 126 field = .ok [.ldc_i4 1] ∧ + wrong.all (fun other => decide (fieldCode 126 other = liveHoleAt 0 126)) ∧ + fieldCode 126 field [] { littleEndian := false } = liveHoleAt 0 126 := by + decide +kernel + +#print axioms endianFieldDecodes + +/-- Both RVA opcodes bind the descriptor's field and require its complete declared storage. -/ +theorem rvaFieldsDecode : + let field : FieldRef := ⟨.object "Fixture" "Tables", 1, .atom "Fixture" "Data", name!"Words"⟩ + let data : StaticFieldSite := + ⟨{ identity := 3, field, bytes := [1, 2, 3, 4], alignment := 4 }, some 4⟩ + [(127, Instruction.ldsflda field), (208, .ldtoken (.field field))].all (fun (opcode, op) => + let sites := [(0, data)] + let wrong := [{ field with owner := .object "Other" "Tables" }, + { field with owner := .object "Fixture" "Other" }, { field with name := name!"Other" }, + { field with type := int32 }, { field with index := 0 }] + decide (fieldCode opcode field sites = .ok [op]) && + wrong.all (fun other => decide (fieldCode opcode other sites = liveHoleAt 0 opcode)) && + decide (fieldCode opcode field [] = liveHoleAt 0 opcode) && + decide (fieldCode opcode field [(0, { data with rvaSize := some 3 })] = + liveHoleAt 0 opcode)) = true := by + decide +kernel + +#print axioms rvaFieldsDecode + +/-- The fixture's unsigned word field is initialized to seven. -/ +private def bias : StaticDescriptor := + { identity := 9, field := ⟨.object ownIdentity "Statics", 1, uint32, name!"Bias"⟩, + bytes := [7, 0, 0, 0], scalar := some false, + initializer := some { method := 0, arguments := [], literal := some (.i32 7) } } + +/-- The checked inputs of a static access retain its descriptor in the resulting method. -/ +private def staticResult (opcode : Nat) (operand : FieldRef) (descriptor : StaticDescriptor) + (hasVolatilePrefix := false) (owner := addRaw.owner) (environment : LayoutEnvironment := {}) : + Except MethodDecodeError Method := + let value := if opcode == 128 then [instr 0 0x17] else [] + let prefixes := if hasVolatilePrefix then [instr value.length 0xfe13] else [] + let offset := value.length + 2 * prefixes.length + let access := { fieldSite opcode operand with offset } + let code := value ++ prefixes ++ [access, instr (offset + 5) 0x2a] + let result : TypeExpr := match opcode with + | 127 => .byref operand.type + | 128 => .void + | _ => operand.type + let source := { addRaw with + code, owner, returnType := result, + staticFields := [(value.length + prefixes.length, ⟨descriptor, none⟩)] } + let arguments : RawArguments := + { arguments := [], locals := [], code := code.map fun _ => .other } + Method.ofClosed? .scalar environment [] (allLive source) source arguments + +/-- A load and an address use their exact field, and the method retains its initializer. -/ +theorem ordinaryStaticsDecode : + let field := bias.field + let descriptor := bias + [(126, Instruction.ldsfld field), (127, .ldsflda field)].all + (fun (opcode, op) => + match staticResult opcode field descriptor with + | .ok method => method.code == [op, .ret] && method.staticSites == [(0, descriptor)] + | .error _ => false) = true := by + decide +kernel + +#print axioms ordinaryStaticsDecode + +/-- A reachable store to a static field is refused at the store, with a matching descriptor. -/ +theorem ordinaryStaticStoreRefused : + (match staticResult 128 bias.field bias with + | .error (.liveHole pc opcode) => pc == 1 && opcode == 128 + | _ => false) = true := by + decide +kernel + +#print axioms ordinaryStaticStoreRefused + +/-- A mismatched value leaves a hole at the access. A volatile prefix is itself a live hole. -/ +theorem ordinaryStaticRefusals : + let field := bias.field + let descriptor := bias + let init := bias.initializer.getD { method := 0, arguments := [] } + let changed := { init with literal := some (.i32 8) } + let wrongLiteral := { descriptor with initializer := some changed } + let wrongCallee := { descriptor with initializer := some { init with method := 1 } } + let other := { field with owner := .object "Foreign" "Statics" } + [126, 127, 128].all (fun opcode => + let pc := if opcode == 128 then 1 else 0 + let wrong := [{ field with name := name!"Other" }, { field with index := 0 }, + { field with owner := .object ownIdentity "Other" }, { field with type := int32 }] + let errorAt := fun (result : Except MethodDecodeError Method) pc => + match result with + | .error error => error == .liveHole pc opcode + | .ok _ => false + wrong.all (fun operand => errorAt (staticResult opcode operand descriptor) pc) && + errorAt (staticResult opcode other { descriptor with field := other }) pc && + errorAt (staticResult opcode field wrongLiteral) pc && + errorAt (staticResult opcode field wrongCallee) pc && + (match staticResult opcode field descriptor true with + | .error (.liveHole index 0xfe13) => index == pc + | _ => false)) = true := by + decide +kernel + +#print axioms ordinaryStaticRefusals + +/-- The load operand names exactly the field in the descriptor under test. -/ +private def ordinaryLoad (descriptor : StaticDescriptor) (owner := addRaw.owner) + (environment : LayoutEnvironment := {}) : Except RawDecodeError (List Instruction) := + (staticResult 126 descriptor.field descriptor (owner := owner) (environment := environment)) + |>.map Method.code |>.mapError .method + +/-- Ordinary static storage requires byte alignment. -/ +theorem ordinaryStaticAlignmentRefused : + ordinaryLoad { bias with alignment := 2 } = liveHoleAt 0 126 := by decide +kernel +#print axioms ordinaryStaticAlignmentRefused + +/-- A non-opaque static load requires an initializer for its bytes. -/ +theorem ordinaryStaticMissingInitializerRefused : + ordinaryLoad { bias with initializer := none } = liveHoleAt 0 126 := by decide +kernel +#print axioms ordinaryStaticMissingInitializerRefused + +/-- Seven fits in one byte, but the declared unsigned word requires four. -/ +theorem ordinaryStaticWidthRefused : + ordinaryLoad { bias with bytes := [7] } = liveHoleAt 0 126 := by decide +kernel +#print axioms ordinaryStaticWidthRefused + +/-- The unsigned word field refuses a descriptor that requests signed extension. -/ +theorem ordinaryStaticSignednessRefused : + ordinaryLoad { bias with scalar := some true } = liveHoleAt 0 126 := by decide +kernel +#print axioms ordinaryStaticSignednessRefused + +/-- A static field on an aggregate owner requires that aggregate's assembly to match. -/ +theorem ordinaryStaticAggregateOwnerRefused : + let localSite := { bias with field.owner := .aggregate (ownIdentity ++ "::Statics") 0 } + let foreignSite := { localSite with field.owner := .aggregate "Foreign::Statics" 0 } + ordinaryLoad localSite = .ok [.ldsfld localSite.field, .ret] ∧ + ordinaryLoad foreignSite = liveHoleAt 0 126 := by decide +kernel +#print axioms ordinaryStaticAggregateOwnerRefused + +/-- Matching empty assembly names cannot establish ownership of a static field. -/ +theorem ordinaryStaticEmptyScopeRefused : + ordinaryLoad { bias with field.owner := .object "" "Statics" } "::Caller" = + liveHoleAt 0 126 := by decide +kernel +#print axioms ordinaryStaticEmptyScopeRefused + +/-- A caller-bound field supports a load with zero placeholder bytes and no address or store. -/ +theorem opaqueStaticAccess : + let field := bias.field + let site := { bias with bytes := [0, 0, 0, 0], initializer := none, opaqueValue := true } + let refusal := fun opcode (descriptor : StaticDescriptor) => + match staticResult opcode field descriptor with + | .error (.liveHole pc raw) => pc == (if opcode == 128 then 1 else 0) && raw == opcode + | _ => false + ((staticResult 126 field site).isOk && + refusal 126 { site with bytes := [1, 0, 0, 0] } && refusal 127 site && refusal 128 site) = + true := by + decide +kernel + +#print axioms opaqueStaticAccess + +/-- The decoder preserves a call initializer's index so the program check executes that callee. -/ +theorem staticInitializerCallee : + let check := fun callee => + let site := { bias with initializer := some { method := callee, arguments := [] } } + match staticResult 126 bias.field site with + | .ok method => + let seven : Method := { code := [.ldc_i4 7, .ret], locals := [], returnsValue := true } + some (method.staticSites == [(0, site)], decide (StaticValuesValid [method, seven])) + | .error _ => none + check 1 = some (true, true) ∧ check 0 = some (true, false) := by + decide +kernel + +#print axioms staticInitializerCallee + +/-- The decoder's code for `code` with RVA `sites`, reachable pcs and the link table. -/ +private def staticCode (code : List RawInstruction) (sites : List (Nat × StaticFieldSite)) + (reachable : List Bool) (links : List (Nat × Link) := []) : + Except RawDecodeError (List Instruction) := + (decodeRaw .scalar {} links reachable { addRaw with code, staticFields := sites } []).map Method.code + +/-- A volatile prefix makes the following ordinary load a hole even when only the load is live. -/ +theorem ordinaryStaticVolatileLoadRefused : + let sites := [(1, ⟨bias, none⟩)] + let load := { fieldSite 126 bias.field with offset := 2 } + staticCode [instr 0 0, load] sites [false, true] = .ok [.unadmitted 0, .ldsfld bias.field] ∧ + staticCode [instr 0 0xfe13, load] sites [false, true] = liveHoleAt 1 126 := by decide +kernel +#print axioms ordinaryStaticVolatileLoadRefused + +/-- Each RVA guard refuses on its own: data at a platform field, an empty, misaligned, + initialized, scalar or opaque blob, another field opcode, a prefix and a link at the pc. -/ +theorem rvaGuardsRefuse : + let field : FieldRef := ⟨.object "Fixture" "Tables", 1, .atom "Fixture" "Data", name!"Words"⟩ + let data : StaticFieldSite := + { descriptor := { identity := 3, field, bytes := [1, 2, 3, 4], alignment := 4 }, + rvaSize := some 4 } + let refused : List StaticFieldSite := + [{ descriptor := { data.descriptor with bytes := [] }, rvaSize := some 0 }, + { data with descriptor.alignment := 3 }, { data with descriptor.alignment := 16 }, + { data with descriptor.initializer := some { method := 0, arguments := [] } }, + { data with descriptor.scalar := some false }, { data with descriptor.opaqueValue := true }] + let load := { fieldSite 127 field with offset := 2 } + fieldCode 126 littleEndianField [(0, data)] = liveHoleAt 0 126 ∧ + refused.all (fun other => decide (fieldCode 127 field [(0, other)] = liveHoleAt 0 127)) ∧ + [126, 128].all (fun opcode => + decide (fieldCode opcode field [(0, data)] = liveHoleAt 0 opcode)) ∧ + staticCode [instr 0 0x00, load] [(1, data)] [false, true] = + .ok [.unadmitted 0, .ldsflda field] ∧ + staticCode [instr 0 0xFE13, load] [(1, data)] [false, true] = liveHoleAt 1 127 ∧ + staticCode [fieldSite 127 field] [(0, data)] [true] [(0, .direct 0)] = liveHoleAt 0 127 := by + decide +kernel + +#print axioms rvaGuardsRefuse + +/-- A field handle produces a span only at the open declaration and the matching closed width. -/ +theorem staticSpanDecodes : + let owner := "System.Runtime.CompilerServices.RuntimeHelpers" + let name := "CreateSpan" + let handle := "System.RuntimeFieldHandle" + let span := TypeExpr.apply (.constructor runtimeScope "System.ReadOnlySpan`1") + let site : CallSite := { + method := { + owner := .atom runtimeScope owner, name := name!"CreateSpan", hasThis := false, + genericArity := 1, returnType := span int32, parameters := [.atom runtimeScope handle], + methodArguments := [int32] }, + name, typeArguments := [int32], parameterCount := 1, returnsValue := true, + declared := { + convention := 16, returnType := span (.parameter 0), + parameters := [.atom runtimeScope handle] }, + target := .external + s!"{runtimeScope}::System.ReadOnlySpan`1 {owner}::{name}({handle})" } + let wide := TypeExpr.atom runtimeScope "System.UInt64" + Tests.ModelOperands.checks site ∧ + runtimeModel {} .call site.method = some (.memory (.createSpan 4)) ∧ + Tests.ModelOperands.decoded { site with method.methodArguments := [wide] } = + liveHoleAt 0 40 := by + decide +kernel + +#print axioms staticSpanDecodes + +/-- The argument check requires one mask entry for every raw instruction. -/ +theorem mismatched_mask_refused : + (Method.ofRaw? .scalar {} [] [true, true, true] addRaw []).isNone && + (Method.ofRaw? .scalar {} [] [true, true, true, true, false] addRaw []).isNone := by + decide +kernel +#print axioms mismatched_mask_refused + +/-- A dead constrained call whose target is an ordinary static method. -/ +private def ordinaryStaticRaw := method 8 false [] [instr 0 42, instr 1 22, + instr 2 0xFE16 (.type objectType), instr 8 40 (.call writeLineSite), instr 13 42] + +/-- A dead tail call returning an integer in a void method. -/ +private def wrongTailRaw := method 8 false [] [instr 0 42, instr 1 0xFE14, + instr 3 40 (.call getSite), instr 8 42] + +/-- An unreachable indirect tail call returning an integer from a void method. -/ +private def wrongIndirectTailRaw := method 8 false [] [instr 0 42, instr 1 0xFE14, + instr 3 41 (.indirectCall int32 [] false false 0), instr 8 42] + +/-- A static method without a virtual interface slot cannot take a constrained receiver. -/ +theorem ordinary_static_prefix_refused : + (Method.ofRaw? .scalar {} [] [true, false, false, false, false] ordinaryStaticRaw []).isNone && + !decide (MethodShapeValid ordinaryStaticRaw (frame [])) := by + decide +kernel +#print axioms ordinary_static_prefix_refused + +/-- An unreachable tail transfer still returns the enclosing method's declared result. -/ +theorem tail_result_mismatch_refused : + (Method.ofRaw? .scalar {} [] [true, false, false, false] wrongTailRaw []).isNone && + !decide (MethodShapeValid wrongTailRaw (frame [])) := by + decide +kernel +#print axioms tail_result_mismatch_refused + +/-- Refusal by the closed method shape excludes every full evidence record for these bodies. -/ +theorem dead_call_evidence_refused (item : MethodEvidence) (emitted : Method) + (layouts : List RawLayout) (computed : Array ComputedLayout) (pins : List AssemblyPin) + (platform : PlatformTable) (items : List MethodEvidence) (imports : List ImportSlot) + (body : item.closed = ordinaryStaticRaw ∨ item.closed = wrongTailRaw ∨ + item.closed = wrongIndirectTailRaw) : + ¬ MethodEvidenceValid layouts computed pins platform items imports item emitted := by + intro valid + have prefixes := valid.2.2.2.2.2.2.1.2.2.2.2 + rcases body with invalid | invalid | invalid <;> rw [invalid] at prefixes <;> + exact absurd prefixes (by decide +kernel) +#print axioms dead_call_evidence_refused + +/-- Bytes that decode to either invalid body cannot construct a method. -/ +theorem dead_call_bytes_refused {pins : List AssemblyPin} {platform : PlatformTable} + {assembly : AssemblyPin} {imports : BodyImports} + {body : BodyEvidence} {raw : RawMethod} {signature : String} + (decodes : BodyDecodes pins platform assembly body raw [] signature imports) + (invalid : raw = ordinaryStaticRaw ∨ raw = wrongTailRaw) : + Method.ofBody? .scalar {} [] (raw.code.mapIdx fun pc _ => pc == 0) body raw [] = none := by + rw [Method.ofBody?_of_decodes decodes] + rcases invalid with rfl | rfl <;> decide +kernel +#print axioms dead_call_bytes_refused + +/-- Branch entry must include the readonly modifier when constructing a method from raw code. -/ +theorem raw_prefix_entry_refused : + let raw := method 8 false [] [instr 0 20, instr 1 22, instr 2 43 (.target 4), + instr 4 0xFE1E, instr 6 143 (.type int32), instr 11 38, instr 12 42] + (Method.ofRaw? .scalar {} [] (allLive raw) raw []).isNone = true := by + decide +kernel +#print axioms raw_prefix_entry_refused + +/-- An unordered switch may target the prefix and return, but cannot enter the element address. -/ +theorem raw_switch_prefix_entry : + let raw := fun targets => [instr 0 69 (.targets targets), instr 16 0xFE1E, + instr 18 143 (.type int32), instr 23 42] + rawPrefixesValid .void (raw [3, 1, 3, 2^128]) && !rawPrefixesValid .void (raw [3, 2, 3]) := by + decide +kernel +#print axioms raw_switch_prefix_entry + +/-- A malformed prefix anywhere in a method precludes both decoding and shape evidence. -/ +theorem invalid_prefix_sequences_refused : + let modifier := instr 0 0xFE12 (.integer 1) + let load := instr 3 0x4A + let add := instr 3 0x58 + let readOnly := instr 0 0xFE1E + let owner := TypeExpr.object "T" "Account" + let field := instr 3 123 (.field owner 0 (.atom runtimeScope "System.Int32") name!"n" false) + let address := instr 4 143 (.type owner) + let ret := instr 8 42 + let cases := [[modifier, add], [instr 0 0xFE12 (.integer 3), field], [instr 0 0xFE12, load], + [instr 0 0xFE13, load], [instr 0 0xFE14, add], [instr 0 0xFE16 (.type owner), add], + [instr 0 0xFE19 (.integer 7), add], [instr 0 0xFE19 (.integer 255), field], + [instr 0 0xFE1E (.integer 3), address], [readOnly, ret], [readOnly, readOnly, address], + [modifier, instr 3 0xFE13, load], [ret, readOnly], [ret, instr 1 0xFE19 (.integer 4), field], + [modifier, instr 3 0xFE17 (.integer 1)], [modifier, instr 3 0x4A (.integer 1)]] + cases.all (fun code => + let raw := { addRaw with code } + (Method.ofRaw? .scalar {} [] (allLive raw) raw []).isNone && + !decide (MethodShapeValid raw (frame []))) = true := by + decide +kernel +#print axioms invalid_prefix_sequences_refused + +/-- An explicit receiver cannot be decoded as an additional implicit argument. -/ +theorem explicit_this_has_no_raw_method : + let raw := { addRaw with explicitThis := true } + (Method.ofRaw? .scalar {} [] (allLive raw) raw []).isNone = true := by + decide +kernel +#print axioms explicit_this_has_no_raw_method + +/-- Matching EXPLICITTHIS metadata is refused even when the body bytes are valid. -/ +theorem explicit_this_has_no_body : + ((encodeBody pin { addRaw with explicitThis := true }).map fun body => + (checkBody body { addRaw with explicitThis := true } []).toBool) = some false := by + decide +kernel +#print axioms explicit_this_has_no_body + +/-- The method of the sub bytes against the add definition is the sub method: `Method.ofBody?` + reads no instruction of the definition. -/ +theorem code_read_from_bytes : + decoded (encodeBody pin subRaw) addRaw = some (frame [.ldc_i4 1, .ldc_i4 2, .sub, .ret]) := by + cil_kernel_rfl +#print axioms code_read_from_bytes + +/-- `ldarg.0; ldarg.1; add; ret` over two `uint64` parameters, returning `uint64`. -/ +def addArgumentsRaw : RawMethod := + { method 8 false [] [instr 0 0x02, instr 1 0x03, instr 2 0x58, instr 3 0x2A] with + parameters := [uint64, uint64], returnType := uint64 } + +/-- The bytes `12 02 03 58 2A` decode to the two argument loads, the add and the return. -/ +theorem add_arguments_decode : + (encodeBody pin addArgumentsRaw).map (·.bytes) = some [0x12, 0x02, 0x03, 0x58, 0x2A] ∧ + decoded (encodeBody pin addArgumentsRaw) addArgumentsRaw = some + { code := [.ldarg 0, .ldarg 1, .add, .ret], locals := [], + declared := + { parameters := [uint64, uint64], returnType := uint64, initLocals := false }, + returnsValue := true } := by + constructor <;> cil_kernel_rfl +#print axioms add_arguments_decode + +/-- `ldc.i4.1; stloc.0; ldloc.0; ret` over one `uint32` local, with the given init flag. -/ +private def localRaw (initLocals : Bool) : RawMethod := + method 8 initLocals [uint32] [instr 0 0x17, instr 1 0x0A, instr 2 0x06, instr 3 0x2A] + +/-- The locals start at zero exactly when the bytes set the init flag, whatever the definition + says. -/ +theorem locals_initialized_by_the_bytes : + (decoded (encodeBody pin (localRaw true)) (localRaw false)).map (·.locals) = + some [.i32 0] ∧ + (decoded (encodeBody pin (localRaw false)) (localRaw true)).map (·.locals) = + some [.unmodeled] := by + constructor <;> cil_kernel_rfl +#print axioms locals_initialized_by_the_bytes + +/-- `ldc.i4.8; localloc; pop; ret` with no locals and the given init flag. -/ +private def allocationRaw (initLocals : Bool) : RawMethod := + method 8 initLocals [] [instr 0 0x1E, instr 1 0xFE0F, instr 3 0x26, instr 4 0x2A] + +/-- Bytes without localsinit decode `localloc` under a declared signature whose init flag is + false, whichever flag the definition carries, so the allocation is not zeroed. -/ +theorem localloc_flag_read_from_the_bytes : + (decoded (encodeBody pin (allocationRaw false)) (allocationRaw false)).map + (fun method => (method.code, method.declared.initLocals)) = + some ([.ldc_i4 8, .localloc, .pop, .ret], false) ∧ + (decoded (encodeBody pin (allocationRaw false)) (allocationRaw true)).map + (fun method => (method.code, method.declared.initLocals)) = + some ([.ldc_i4 8, .localloc, .pop, .ret], false) := by + constructor <;> cil_kernel_rfl +#print axioms localloc_flag_read_from_the_bytes + +/-- `ldc.i4.1; starg.s index; ret` over one `uint32` parameter. -/ +private def storeRaw (index : Nat) : RawMethod := + { method 8 false [] [instr 0 0x17, instr 1 0x10 (.argument index), instr 3 0x2A] with + parameters := [uint32] } + +/-- A stored argument takes a home after the locals, so its `starg` writes a frame slot. -/ +theorem stored_argument_homed : + (decoded (encodeBody pin (storeRaw 0)) (storeRaw 0)).map + (fun method => (method.code, method.parameterHomes)) = + some ([.ldc_i4 1, .starg 0, .ret], [(0, .word32)]) := by + cil_kernel_rfl +#print axioms stored_argument_homed + +/-- `opcode index; pop; ret` over one `uint32` parameter, for an argument load or address. -/ +private def argumentRaw (opcode index : Nat) : RawMethod := + { method 8 false [] [instr 0 opcode (.argument index), instr 2 0x26, instr 3 0x2A] with + parameters := [uint32] } + +/-- An argument load, store or address of the one declared argument decodes, and one naming a + second argument gives no method: no frame holds it, and a store or address has no home. -/ +theorem undeclared_argument_has_no_method : + (Method.ofRaw? .scalar {} [] [true, true, true] (argumentRaw 0x0E 0) []).isSome = true ∧ + (Method.ofRaw? .scalar {} [] [true, true, true] (storeRaw 0) []).isSome = true ∧ + (Method.ofRaw? .scalar {} [] [true, true, true] (argumentRaw 0x0F 0) []).isSome = true ∧ + (Method.ofRaw? .scalar {} [] [true, true, true] (argumentRaw 0x0E 1) []).isNone = true ∧ + (Method.ofRaw? .scalar {} [] [true, true, true] (storeRaw 1) []).isNone = true ∧ + (Method.ofRaw? .scalar {} [] [true, true, true] (argumentRaw 0x0F 1) []).isNone = true := by + decide +kernel +#print axioms undeclared_argument_has_no_method + +/-- The add bytes against the add definition with one parameter, and the local body against its + definition with a `uint64` local, fail the body check, so neither has a method. -/ +theorem refused_body_has_no_method : + ((encodeBody pin addArgumentsRaw).map fun body => + (checkBody body { addArgumentsRaw with parameters := [uint64] } []).toBool) = some false ∧ + (decoded (encodeBody pin addArgumentsRaw) + { addArgumentsRaw with parameters := [uint64] }).isNone = true ∧ + ((encodeBody pin (localRaw true)).map fun body => + (checkBody body { localRaw true with locals := [uint64] } []).toBool) = some false ∧ + (decoded (encodeBody pin (localRaw true)) { localRaw true with locals := [uint64] }).isNone = + true := by + decide +kernel +#print axioms refused_body_has_no_method + +/-- A class outside the metadata world, as a type argument. -/ +private def dogClass : TypeExpr := .object "Fixture" "Fixture.Dog" + +/-- `ldnull; castclass !!0; pop; ret`, generic over one method type parameter. -/ +private def genericCastRaw : RawMethod := + { method 8 false [] [instr 0 0x14, instr 1 0x74 (.type (.parameter 0)), instr 6 0x26, + instr 7 0x2A] with genericParameters := [{ flags := 0, types := [] }] } + +/-- The code is decoded from the instantiated definition, so the cast tests the type argument + rather than the open parameter. -/ +theorem generic_cast_tests_its_argument : + (Method.ofRaw? .scalar {} [] (allLive genericCastRaw) genericCastRaw [dogClass]).map (·.code) = + some [.ldnull, .castclass dogClass, .pop, .ret] := by + cil_kernel_rfl +#print axioms generic_cast_tests_its_argument + +/-- Instantiation substitutes the method argument into the receiver of the decoded call. -/ +theorem generic_constrained_receiver : + let slot := { getSite with method.hasThis := true } + let caller := { method 8 false [] [instr 0 2, instr 1 0xFE16 (.type (.parameter 0)), + instr 7 111 (.call slot), instr 12 42] with + genericParameters := [{ flags := 0, types := [] }], returnType := int32, + parameters := [.byref (.parameter 0)] } + (Method.ofRaw? .scalar {} [(2, .direct 1)] (allLive caller) caller [int32]).map (·.code) = + some [.ldarg 0, .nop, .call slot.method { constrained := some int32 }, .ret] := by + cil_kernel_rfl +#print axioms generic_constrained_receiver + +/-- `ldnull; unbox.any object; pop; ret`, with the type token of the world's `System.Object`. -/ +private def unboxBytes : List Nat := + [0x22, 0x14, 0xA5] ++ littleEndian 4 tokens.objectRef ++ [0x26, 0x2A] + +/-- The definition `unboxBytes` decode to. -/ +private def unboxRaw : RawMethod := + method 8 false [] [instr 0 0x14, instr 1 0xA5 (.type objectType), instr 6 0x26, instr 7 0x2A] + +/-- `unboxBytes` with its token table and the world's metadata. -/ +private def unboxBody : BodyEvidence := + evidenceOf unboxBytes [(tokens.objectRef, .type objectType)] { own := own.1, others := [lib] } + unboxRaw + +/-- The pinned bytes are well formed, but live unbox.any prevents method construction. -/ +theorem live_unbox_refused : + (checkBody unboxBody unboxRaw []).toBool = true ∧ + (Method.ofBody? .scalar {} [] (allLive unboxRaw) unboxBody unboxRaw []).map (·.code) = + none := by + constructor + · decide +kernel + · cil_kernel_rfl +#print axioms live_unbox_refused + +/-- Closed decoding identifies the first live hole by its index and opcode. -/ +theorem unbox_refusal_site : + Method.ofClosed? .scalar {} [] (allLive unboxRaw) unboxRaw + ⟨[], [], List.replicate 4 .other⟩ = .error (.liveHole 1 0xa5) := by + cil_kernel_rfl +#print axioms unbox_refusal_site + +/-- error: cil_decoded_method: CIL.Extraction.MethodDecodeError.liveHole 1 165 -/ +#guard_msgs in +cil_decoded_method refusedUnbox := Method.ofClosed? .scalar {} [] (allLive unboxRaw) + unboxRaw ⟨[], [], List.replicate 4 .other⟩ + +/-- A method evidence record with every instruction reachable and no links. -/ +def item (original : RawMethod) (body : BodyEvidence) : MethodEvidence := + { original := original, body := body, arguments := [], closed := original, + parameterInput := ⟨[], [], []⟩, reachable := allLive original, featureStates := [], + scalarStates := [], layoutUses := [] } + +/-- The receiver method named by its own address operand. -/ +private def instanceAddressSite : CallSite := { + method := { + owner := .object ownIdentity "Program", name := name!"M", hasThis := true, + genericArity := 0, returnType := .void, parameters := [] }, + declared := { convention := 0x20 }, target := .managed 0x06000001, name := "M", + typeArguments := [], parameterCount := 0, returnsValue := false } + +/-- A parameterless instance method that takes its own address, pops it and returns. -/ +private def instanceAddressRaw : RawMethod := + { method 8 false [] [instr 0 0xFE06 (.call instanceAddressSite), instr 6 0x26, instr 7 0x2A] + with hasThis := true, attributes := 0x86, objectReceiver := true } + +/-- The bytes match the instance method, but its address cannot bind to a static signature. -/ +theorem instance_address_program_refused : + (do + let (slice, _) ← methodMetadata pin instanceAddressRaw + let body := evidenceOf [0x22, 0xFE, 0x06, 1, 0, 0, 6, 0x26, 0x2A] + [(0x06000001, .call instanceAddressSite)] { own := slice } instanceAddressRaw + let evidence := { item instanceAddressRaw body with + links := [(0, .address 0)], + featureStates := [some ⟨[], []⟩, some ⟨[none], []⟩, some ⟨[], []⟩] } + pure (bodyMatches body instanceAddressRaw [], (decodedProgram? .scalar {} [evidence]).isNone)) + = some (true, true) := by decide +kernel +#print axioms instance_address_program_refused + +/-- The adder's certificate records the stack depth before each instruction. -/ +def addFeatureStates : FeatureCertificate := + [0, 1, 2, 1].map fun depth => some ⟨List.replicate depth none, []⟩ + +/-- Both adders decode. A malformed second body or missing certificate rejects the program. -/ +theorem program_needs_every_body : + ((encodeBody pin addArgumentsRaw).bind fun body => do + let adder := { item addArgumentsRaw body with featureStates := addFeatureStates } + let pair ← decodedProgram? .scalar {} [adder, adder] + let badBody := { adder with original := { addArgumentsRaw with parameters := [uint64] } } + let noCertificate := { adder with featureStates := [] } + pure (pair.length, (decodedProgram? .scalar {} [adder, badBody]).isNone, + (decodedProgram? .scalar {} [adder, noCertificate]).isNone)) = + some (2, true, true) := by decide +kernel +#print axioms program_needs_every_body + +/-- The profile the decoded definitions below are evaluated under; a named constant passed to + the decoder stays named in the method. -/ +@[cil_code] def testProfile : FeatureProfile := .scalar + +/-- The layouts the decoded definitions below are evaluated under. -/ +@[cil_code] def testEnvironment : LayoutEnvironment := {} + +/-- `ldarg.0; ldarg.1; add; ldc.i4.m1; add; ret` over two `uint32` parameters. -/ +private def sumRaw : RawMethod := + { method 8 false [] [instr 0 2, instr 1 3, instr 2 0x58, instr 3 0x15, instr 4 0x58, + instr 5 0x2A] with parameters := [uint32, uint32], returnType := uint32 } + +cil_decoded_method sumMethod := + Method.ofRaw? testProfile testEnvironment [] (allLive sumRaw) sumRaw [] +cil_instruction_equations sumMethod + +/-- The definition is the method the decoder gives: the decoded code, the declared signature, + and the profile and layouts it was evaluated under. -/ +example : sumMethod = + { code := [.ldarg 0, .ldarg 1, .add, .ldc_i4 4294967295, .add, .ret], locals := [], + profile := testProfile, environment := testEnvironment, + declared := { parameters := [uint32, uint32], returnType := uint32, initLocals := false }, + returnsValue := true } := rfl + +/-- info: Tests.DecodedMethod.sumMethod_decoder_eq : + Method.ofRaw? testProfile testEnvironment [] (allLive sumRaw) sumRaw [] = some sumMethod -/ +#guard_msgs (whitespace := lax) in #check sumMethod_decoder_eq + +-- The kernel proves the decoded fact and the field facts by reduction, with no other axiom. +/-- info: 'Tests.DecodedMethod.sumMethod_decoder_eq' depends on axioms: + [propext, Classical.choice, Quot.sound] -/ +#guard_msgs (whitespace := lax) in #print axioms sumMethod_decoder_eq +/-- info: 'Tests.DecodedMethod.sumMethod_profile' does not depend on any axioms -/ +#guard_msgs in #print axioms sumMethod_profile + +-- Stepping reads each instruction as a constructor over numerals, a negative constant included. +/-- info: Tests.DecodedMethod.sumMethod_instruction3 : + sumMethod.code[3]? = some (Instruction.ldc_i4 0xFFFFFFFF) -/ +#guard_msgs (whitespace := lax) in #check sumMethod_instruction3 + +-- The profile fact names the profile the decoder was given, as an extraction's facts do. +/-- info: Tests.DecodedMethod.sumMethod_profile : sumMethod.profile = testProfile -/ +#guard_msgs in #check sumMethod_profile + +-- The same unbox opcode remains a hole when its mask entry is false. +cil_decoded_method unboxMethod := + Method.ofRaw? testProfile testEnvironment [] [true, false, true, true] unboxRaw [] +cil_instruction_equations unboxMethod + +/-- info: Tests.DecodedMethod.unboxMethod_instruction1 : + unboxMethod.code[1]? = some (Instruction.unadmitted 165) -/ +#guard_msgs (whitespace := lax) in #check unboxMethod_instruction1 + +/-- The `uint32` field of `Dog` the decoded definition below names. -/ +def ageField : FieldRef := { owner := dogClass, index := 0, type := uint32, name := name!"Age" } + +/-- `ldarg.0; ldfld Dog.Age; ret` over one `Dog` parameter. -/ +private def ageRaw : RawMethod := + { method 8 false [] [instr 0 2, instr 1 0x7B (.field dogClass 0 uint32 name!"Age" false), + instr 6 0x2A] with parameters := [dogClass], returnType := uint32 } + +-- A field operand equal to a listed constant is written as that constant, as an extraction's +-- literal names it, so the shape facts stated about the constant apply to the decoded method. +cil_decoded_method ageMethod [ageField] := + Method.ofRaw? testProfile testEnvironment [] (allLive ageRaw) ageRaw [] +cil_instruction_equations ageMethod + +/-- info: Tests.DecodedMethod.ageMethod_instruction1 : + ageMethod.code[1]? = some (Instruction.ldfld ageField) -/ +#guard_msgs (whitespace := lax) in #check ageMethod_instruction1 + +/-- A field of `Dog` that `ageRaw` does not access. -/ +def weightField : FieldRef := { ageField with index := 1 } + +-- A listed constant that names no field operand is refused rather than ignored. +/-- error: cil_decoded_method: the decoded method has no field operand + Tests.DecodedMethod.weightField -/ +#guard_msgs (whitespace := lax) in +cil_decoded_method weightMethod [weightField] := + Method.ofRaw? testProfile testEnvironment [] (allLive ageRaw) ageRaw [] + +-- A body naming an undeclared argument defines nothing. +/-- error: cil_decoded_method: the decoder gives no method + Method.ofRaw? testProfile testEnvironment [] [true, true, true] (argumentRaw 14 1) [] -/ +#guard_msgs (whitespace := lax) in +cil_decoded_method undeclaredMethod := + Method.ofRaw? testProfile testEnvironment [] [true, true, true] (argumentRaw 0x0E 1) [] + +-- A decoder with a hole is not evaluated. +/-- error: cil_decoded_method: the decoder must be a closed term + Method.ofRaw? ?m.1 testEnvironment [] (allLive sumRaw) sumRaw [] -/ +#guard_msgs (whitespace := lax) in +cil_decoded_method openMethod := Method.ofRaw? _ testEnvironment [] (allLive sumRaw) sumRaw [] + +/-- A number the kernel cannot reduce; compiled code evaluates it to its default. -/ +opaque stuckIndex : Nat + +/-- A list the kernel cannot reduce; compiled code evaluates it to its default. -/ +opaque stuckLocals : List Value + +/-- An empty method under the given profile and layouts with the given locals and opaque vector + locals, a decoder whose fields the kernel may not reduce. -/ +private def emptyMethod (profile : FeatureProfile) (environment : LayoutEnvironment) + (locals : List Value) (opaqueVectorLocals : List Nat) : Option Method := + some { code := [], locals, returnsValue := false, opaqueVectorLocals, profile, environment } + +-- A value the kernel does not reduce to the evaluated literal is refused, never kept unreduced. +/-- error: cil_decoded_method: the kernel does not reduce both sides of + Tests.DecodedMethod.stuckIndexMethod_decoder_eq to one term -/ +#guard_msgs (whitespace := lax) in +cil_decoded_method stuckIndexMethod := emptyMethod testProfile testEnvironment [] [stuckIndex] + +-- A constant passed to the decoder is written by name, so the opaque list is passed inside a term. +/-- error: cil_decoded_method: the kernel does not reduce both sides of + Tests.DecodedMethod.stuckLocalsMethod_decoder_eq to one term -/ +#guard_msgs (whitespace := lax) in +cil_decoded_method stuckLocalsMethod := + emptyMethod testProfile testEnvironment (stuckLocals ++ []) [] + +/-- `ldarg.0; stloc.0; br 7; ldloc.0; ldc.i4.1; sub; stloc.0; ldloc.0; brtrue 3; ldloc.0; ret`: + a `uint32` count copied to an initialized local and decremented to zero. -/ +private def countdownRaw : RawMethod := + { method 8 true [uint32] [instr 0 2, instr 1 10, instr 2 0x2B (.target 7), instr 3 6, + instr 4 0x17, instr 5 0x59, instr 6 10, instr 7 6, instr 8 0x2D (.target 3), instr 9 6, + instr 10 0x2A] with + parameters := [uint32], returnType := uint32 } + +cil_decoded_method countdownMethod := + Method.ofRaw? testProfile testEnvironment [] (allLive countdownRaw) countdownRaw [] +cil_instruction_equations countdownMethod +cil_resolution_facts countdownMethod + +-- The frame facts hold the decoded local, initialized by the body's flag. +/-- info: Tests.DecodedMethod.countdownMethod_locals : countdownMethod.locals = [Value.i32 0] -/ +#guard_msgs in #check countdownMethod_locals +/-- info: Tests.DecodedMethod.countdownMethod_localKinds : + countdownMethod.localKinds = [LocalKind.word32] -/ +#guard_msgs (whitespace := lax) in #check countdownMethod_localKinds + +cil_resolution_facts sumMethod + +/-- Stepping reads the decoded definition through its `cil_code` facts: the sum of the two + arguments and the constant `-1`, with memory unchanged. -/ +theorem sum_returns (memory : Safety.Memory) (left right : W32) : + ∃ fuel, Safety.invoke [sumMethod] fuel 0 [.scalar (.i32 left), .scalar (.i32 right)] memory = + .ok (memory, [.scalar (.i32 (left + right + 0xFFFFFFFF))]) := by + cil_checked_steps +#print axioms sum_returns + +/-- `sumRaw` as the definition with its own token, the callee of `callerRaw`. -/ +private def summingRaw : RawMethod := { sumRaw with token := tokens.main + 1 } + +/-- The call site of `summingRaw`, a static method of the world's `Program` class. -/ +private def sumSite : CallSite := { + declared := { convention := 0, returnType := uint32, parameters := [uint32, uint32] }, + method := { + owner := .object ownIdentity "Program", name := name!"Sum", hasThis := false, + genericArity := 0, returnType := uint32, parameters := [uint32, uint32] }, + name := "Sum", target := .managed summingRaw.token, typeArguments := [], parameterCount := 2, + returnsValue := true } + +/-- `ldarg.0; ldarg.1; call Sum(uint32, uint32); ret` over two `uint32` parameters. -/ +private def callerRaw : RawMethod := + { method 8 false [] [instr 0 2, instr 1 3, instr 2 0x28 (.call sumSite), instr 7 0x2A] with + parameters := [uint32, uint32], returnType := uint32 } + +-- The bytes name the callee's token; the program index of `summingRaw`, 1, is the link at pc 2. +cil_decoded_method callerMethod := + Method.ofRaw? testProfile testEnvironment [(2, .direct 1)] (allLive callerRaw) callerRaw [] +cil_instruction_equations callerMethod +cil_resolution_facts callerMethod + +/-- The call decodes to the method its operand names, and its target is the link the decoder + was given. -/ +example : callerMethod.code[2]? = some (.call sumSite.method) ∧ + callerMethod.linkAt 2 = some (.direct 1) := ⟨rfl, rfl⟩ + +/-- The decoded caller returns what the decoded callee its link names returns. -/ +theorem caller_returns (memory : Safety.Memory) (left right : W32) : + ∃ fuel, Safety.invoke [callerMethod, sumMethod] fuel 0 + [.scalar (.i32 left), .scalar (.i32 right)] memory = + .ok (memory, [.scalar (.i32 (left + right + 0xFFFFFFFF))]) := by + cil_checked_steps +#print axioms caller_returns + +/-- The table `callerMethod`'s link indexes: the caller at 0 and its callee at 1. -/ +private def callTable : List SpecializedMethod := [⟨[], callerRaw⟩, ⟨[], summingRaw⟩] + +/-- The call check accepts the link the decoder was given and rejects a forged one: another + method with the same signature but another token, an index past the table, or a link of + another kind. -/ +theorem decoded_call_links : + ManagedCallsValid callTable [] (fun _ => false) callerRaw [true, true, true, true] + callerMethod ∧ + ¬ ManagedCallsValid callTable [] (fun _ => false) callerRaw [true, true, true, true] + { callerMethod with links := [(2, .direct 0)] } ∧ + ¬ ManagedCallsValid callTable [] (fun _ => false) callerRaw [true, true, true, true] + { callerMethod with links := [(2, .direct 2)] } ∧ + ¬ ManagedCallsValid callTable [] (fun _ => false) callerRaw [true, true, true, true] + { callerMethod with links := [(2, .virtual [(uint32, 1)])] } ∧ + ¬ ManagedCallsValid callTable [] (fun _ => false) callerRaw [true, true, true, true] + { callerMethod with links := [] } := by + decide +kernel +#print axioms decoded_call_links + +/-- `T::Word`, one `uint64`. -/ +private def wordLayout : RawLayout := + { identity := "T::Word", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .unsigned64 }] } + +/-- `T::Word` and the layout CoreCLR computes for it. -/ +@[cil_code] def wordEnvironment : LayoutEnvironment := + { rawLayouts := [wordLayout], + layouts := #[⟨{ size := 8, fields := [{ offset := 0, width := 8 }] }, 8⟩] } + +/-- A static aggregate load requires all eight bytes of the field's checked layout. -/ +theorem ordinaryStaticAggregateSizeRefused : + let site := { bias with + field.type := .aggregate wordLayout.identity 0, scalar := none, bytes := List.replicate 8 0, + initializer := some { method := 1, arguments := [] } } + ordinaryLoad site (environment := wordEnvironment) = .ok [.ldsfld site.field, .ret] ∧ + ordinaryLoad { site with bytes := List.replicate 7 0 } (environment := wordEnvironment) = + liveHoleAt 0 126 := by decide +kernel +#print axioms ordinaryStaticAggregateSizeRefused + +/-- `ret` over one zero-initialized `T::Word` local. -/ +private def aggregateLocalRaw : RawMethod := + method 8 true [.aggregate "T::Word" 0] [instr 0 0x2A] + +cil_decoded_method aggregateLocalMethod := + Method.ofRaw? testProfile wordEnvironment [] (allLive aggregateLocalRaw) aggregateLocalRaw [] + +-- A zero-initialized struct local is written as an extraction's literal writes it, so a proof's +-- hypothesis about making that local matches the goal frame entry leaves. +/-- info: Tests.DecodedMethod.aggregateLocalMethod_locals : + aggregateLocalMethod.locals = [Value.aggregate (List.replicate 8 0)] -/ +#guard_msgs (whitespace := lax) in #check aggregateLocalMethod_locals + +open CIL.Safety in +/-- Entering the decoded method's frame makes its struct local from `List.replicate 8 0`, the + term a caller's hypothesis about the local names. -/ +theorem aggregate_local_frame_entry (memory entered : Safety.Memory) (slot : Reference) + (made : makeLocal memory.nextIdentity (.aggregate 8) (.aggregate (List.replicate 8 0)) memory = + .ok (.bytes (.aggregate 8) slot, [slot.allocation], entered)) : + enterFrame aggregateLocalMethod [] memory = + .ok ({ activation := memory.nextIdentity, locals := [.bytes (.aggregate 8) slot], + owned := [slot.allocation] }, entered) := by + simp only [enterFrame, aggregateLocalMethod_opaqueVectorLocals, makeSelectedLocals, + List.isEmpty_nil, ite_true, aggregateLocalMethod_localKinds, aggregateLocalMethod_locals, + makeLocals, made, aggregateLocalMethod_parameterHomes, makeParameterHomes, List.map_nil, + Bind.bind, Except.bind, Pure.pure, Except.pure, List.append_nil] +#print axioms aggregate_local_frame_entry + +/-- `simp` unfolds a decoded method in the module that defines it, as a batch that inlines an + extraction beside its proof does. -/ +theorem aggregate_local_unfolds : aggregateLocalMethod.code = [CIL.Instruction.ret] := by + simp only [aggregateLocalMethod] +#print axioms aggregate_local_unfolds + +/-- The closed method of `code` under the `live` mask, with no arguments, locals or result. -/ +private def prefixedResult (code : List RawInstruction) + (live : List Bool := code.map fun _ => true) : Except MethodDecodeError Method := + let source := { addRaw with code } + let arguments : RawArguments := + { arguments := [], locals := [], code := code.map fun _ => .other } + Method.ofClosed? .scalar {} [] live source arguments + +/-- `unaligned. n` decodes to `nop` and gives the block operation after it its alignment, an + unprefixed block operation has the default alignment, and a load after the prefix decodes as + without it. -/ +theorem blockOperationsDecode : + let copy := [instr 0 0xfe12 (.integer 2), instr 2 0xfe17, instr 4 0x2a] + let fill := [instr 0 0xfe18, instr 2 0x2a] + let load := [instr 0 0xfe12 (.integer 1), instr 2 0x4b, instr 3 0x2a] + (match prefixedResult copy, prefixedResult fill, prefixedResult load with + | .ok copied, .ok filled, .ok loaded => + copied.code == [.nop, .cpblk 2, .ret] && + filled.code == [.initblk defaultBlockAlignment, .ret] && + loaded.code == [.nop, .ldind .u4, .ret] + | _, _, _ => false) = true := by + decide +kernel + +#print axioms blockOperationsDecode + +/-- Whether the closed method of `code` is refused at a live hole of `opcode` at `pc`. -/ +private def refusedAt (code : List RawInstruction) (pc opcode : Nat) : Bool := + match prefixedResult code with + | .error (.liveHole site found) => site == pc && found == opcode + | _ => false + +/-- `volatile.` is a hole before an access or a block operation, an `unaligned.` alignment + other than 1, 2 or 4 is a hole at the prefix, and an instruction `unaligned.` does not + modify, a second prefix included, is a hole after it. -/ +theorem prefixHoles : + refusedAt [instr 0 0xfe13, instr 2 0x4a, instr 3 0x2a] 0 0xfe13 && + refusedAt [instr 0 0xfe13, instr 2 0xfe17, instr 4 0x2a] 0 0xfe13 && + refusedAt [instr 0 0xfe12 (.integer 3), instr 2 0x4a, instr 3 0x2a] 0 0xfe12 && + refusedAt [instr 0 0xfe12 (.integer 8), instr 2 0xfe17, instr 4 0x2a] 0 0xfe12 && + refusedAt [instr 0 0xfe12 (.integer 1), instr 2 0x58, instr 3 0x2a] 1 0x58 && + refusedAt [instr 0 0xfe12 (.integer 1), instr 2 0xfe13, instr 4 0x4a, instr 5 0x2a] 1 0xfe13 && + refusedAt [instr 0 0xfe12 (.integer 1), instr 2 0xfe12 (.integer 1), instr 4 0x4a, + instr 5 0x2a] 1 0xfe12 && + refusedAt [instr 0 0xfe13, instr 2 0xfe12 (.integer 1), instr 4 0x4a, instr 5 0x2a] + 0 0xfe13 = true := by + decide +kernel + +#print axioms prefixHoles + +/-- `unaligned.` next to any other prefix is a hole at the second of the two, or at the first + when that one decodes to nothing: `readonly.`, `constrained.`, `tail.` and `no.` in + either order. -/ +theorem prefixCombinationsRefused : + let load := [instr 6 0x4b, instr 7 0x2a] + let aligned := instr 0 0xfe12 (.integer 1) + refusedAt ([aligned, instr 2 0xfe1e] ++ load) 1 0xfe1e && + refusedAt ([instr 0 0xfe1e, instr 2 0xfe12 (.integer 1)] ++ load) 1 0xfe12 && + refusedAt ([aligned, instr 2 0xfe16 (.type int32)] ++ load) 1 0xfe16 && + refusedAt ([instr 0 0xfe16 (.type int32), instr 2 0xfe12 (.integer 1)] ++ load) 1 0xfe12 && + refusedAt ([aligned, instr 2 0xfe14] ++ load) 1 0xfe14 && + refusedAt ([instr 0 0xfe14, instr 2 0xfe12 (.integer 1)] ++ load) 0 0xfe14 && + refusedAt ([aligned, instr 2 0xfe19 (.integer 1)] ++ load) 1 0xfe19 && + refusedAt ([instr 0 0xfe19 (.integer 1), instr 2 0xfe12 (.integer 1)] ++ load) + 0 0xfe19 = true := by + decide +kernel + +#print axioms prefixCombinationsRefused + +/-- A decoded `unaligned.` with no instruction after it, and a branch into the instruction it + modifies from a live pc, fail the prefix check. -/ +theorem prefixStructureRefused : + let trailing := [instr 0 0x16, instr 1 0xfe12 (.integer 1)] + let entered := + [instr 0 0x38 (.target 2), instr 5 0xfe12 (.integer 1), instr 7 0x4b, instr 8 0x2a] + (match prefixedResult trailing, prefixedResult entered [true, false, true, true] with + | .error .invalidPrefixes, .error .invalidPrefixes => true + | _, _ => false) = true := by + decide +kernel + +#print axioms prefixStructureRefused + +end Tests.DecodedMethod diff --git a/Tests/DecodedOpcodes.cs b/Tests/DecodedOpcodes.cs new file mode 100644 index 000000000..3cd4b984e --- /dev/null +++ b/Tests/DecodedOpcodes.cs @@ -0,0 +1,476 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:property AssemblyName=LeanCIL.IdentityTests +#:project ../Extractor/Extractor.csproj +#:package Mono.Cecil +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include GeneratedEvidence.cs + +extern alias IdentityExtractor; + +using System.Reflection; +using Mono.Cecil; +using Mono.Cecil.Cil; +using IdentityExtractor::Nethermind.LeanCIL; + +CheckPrefixes(); + +// The extractor predicts which methods Lean's decoder defines from its own opcode table. Lean decides that the +// table holds exactly the opcodes of `Instruction.valueOpcode`, so the prediction cannot drift from the decoder. +var opcodes = typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static) + .Select(field => (OpCode)field.GetValue(null)!).ToArray(); +var table = opcodes.Where(opcode => GenericEvidence.DecodedOpcodes.Contains(opcode.Code)) + .Select(opcode => (ushort)opcode.Value).Order().ToArray(); +if (table.Length != GenericEvidence.DecodedOpcodes.Count) + throw new InvalidOperationException("A decoded opcode has no Cecil opcode value."); +var directory = At("artifacts/DecodedOpcodes"); +Directory.CreateDirectory(directory); +var path = Path.Combine(directory, "Table.lean"); +File.WriteAllText(path, $$""" + import CIL.Extraction.Folds + + /-! The extractor opcode table and folded instructions against the Lean decoder. -/ + + open CIL CIL.Extraction + + /-- The opcodes the extractor predicts `Instruction.ofRaw?` decodes. -/ + def table : List Nat := [{{string.Join(", ", table)}}] + + /-- Every one-byte opcode and every two-byte opcode `0xFE nn`, numbered as raw instructions are. -/ + def opcodes : List Nat := List.range 256 ++ (List.range 256).map (65024 + ·) + + /-- The table holds an opcode exactly when `Instruction.valueOpcode` does. -/ + theorem table_is_valueOpcode : + (opcodes.all fun opcode => + CIL.Instruction.valueOpcode opcode == table.contains opcode) = true := by + decide +kernel + #print axioms table_is_valueOpcode + + /-- The instruction selected by a recognized role at its raw pc. -/ + private def folded (code : List RawInstruction) (pc : Nat) : Option Instruction := do + let role <- (foldRoles code)[pc]?.join + let raw <- code[pc]? + Instruction.ofRole? role (rawBefore (fun i => code[i]?) pc) raw + + /-- All three opcodes are required for the indirect tail transfer. -/ + theorem tail_opcodes : + let tailPrefix := RawInstruction.mk 0 {{(ushort)OpCodes.Tail.Value}} .none + let indirect := RawInstruction.mk 2 {{OpCodes.Calli.Value}} (.indirectCall .void [] false false 0) + let code := [tailPrefix, indirect, RawInstruction.mk 7 {{OpCodes.Ret.Value}} .none] + let nop := RawInstruction.mk 0 {{OpCodes.Nop.Value}} .none + foldRoles code == [some .head, some .tailCall, some .argument] && + (folded code 0 matches some .nop) && (folded code 1 matches some (.calli _ true)) && + ([0, 1, 2].all fun pc => + foldRoles (code.set pc nop) == List.replicate 3 none) := by + decide +kernel + #print axioms tail_opcodes + + /-- A null throw needs both the null load and the throw opcode. -/ + theorem throw_opcodes : + let code := [RawInstruction.mk 0 {{OpCodes.Ldnull.Value}} .none, + RawInstruction.mk 1 {{OpCodes.Throw.Value}} .none] + let nop := RawInstruction.mk 0 {{OpCodes.Nop.Value}} .none + foldRoles code == [some .head, some (.raise .nullReference)] && + (folded code 1 matches some (.throw .nullReference)) && + ([0, 1].all fun pc => foldRoles (code.set pc nop) == [none, none]) := by + decide +kernel + #print axioms throw_opcodes + + """ + "\n"); +CheckLean(path); +Console.WriteLine($"PASS the extractor's {table.Length} decoded opcodes are those of Instruction.valueOpcode"); + +// A dead tail includes a raw prefix and virtual call. Both retain their opcode at the original pc. +var deadDirectory = Path.Combine(directory, "UnreachableTail"); +Directory.CreateDirectory(deadDirectory); +var deadAssembly = Path.Combine(deadDirectory, "Examples.dll"); +// The examples assembly carries the identity and target framework extraction admits. +using (var module = Mono.Cecil.ModuleDefinition.ReadModule(At("Examples/Assembly/bin/Examples.dll"))) +{ + var owner = new Mono.Cecil.TypeDefinition("Examples", "UnreachableTail", Mono.Cecil.TypeAttributes.Public | + Mono.Cecil.TypeAttributes.Abstract | Mono.Cecil.TypeAttributes.Sealed, module.TypeSystem.Object); + module.Types.Add(owner); + var method = new Mono.Cecil.MethodDefinition("One", + Mono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Int32); + owner.Methods.Add(method); + var il = method.Body.GetILProcessor(); + foreach (var opcode in new[] { OpCodes.Ldc_I4_1, OpCodes.Ret, OpCodes.Ldc_I4_0, OpCodes.Ret }) il.Emit(opcode); + var hash = new MethodReference("GetHashCode", module.TypeSystem.Int32, module.TypeSystem.Object) + { HasThis = true }; + il.Emit(OpCodes.Ldnull); + il.Emit(OpCodes.Tail); + il.Emit(OpCodes.Callvirt, hash); + il.Emit(OpCodes.Ret); + var frame = new MethodDefinition("Frame", Mono.Cecil.MethodAttributes.Public | + Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); + owner.Methods.Add(frame); + var array = new ParameterDefinition(new ArrayType(module.TypeSystem.Int32)); + frame.Parameters.Add(array); + var deadArray = new ParameterDefinition(new ArrayType(module.TypeSystem.Int32)); + frame.Parameters.Add(deadArray); + var vector = new MethodDefinition("VectorLocal", Mono.Cecil.MethodAttributes.Public | + Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); + owner.Methods.Add(vector); + var unsafeReturn = module.GetType("Examples.DivisionIntrinsics").Methods + .Single(candidate => candidate.Name == "UnsafeReturn"); + var create = (MethodReference)unsafeReturn.Body.Instructions.Single(op => op.OpCode == OpCodes.Call).Operand; + vector.Body.Variables.Add(new VariableDefinition(unsafeReturn.ReturnType)); + var constructor = new MethodReference(".ctor", module.TypeSystem.Void, module.TypeSystem.Object) + { HasThis = true }; + var body = frame.Body.GetILProcessor(); + body.Emit(OpCodes.Ldarg_0); + body.Emit(OpCodes.Starg_S, array); + body.Emit(OpCodes.Call, vector); + body.Emit(OpCodes.Ret); + body.Emit(OpCodes.Ldarga_S, deadArray); + body.Emit(OpCodes.Pop); + body.Emit(OpCodes.Ret); + body = vector.Body.GetILProcessor(); + body.Emit(OpCodes.Ret); + body.Emit(OpCodes.Ldc_I8, 0L); + body.Emit(OpCodes.Call, create); + body.Emit(OpCodes.Pop); + body.Emit(OpCodes.Newobj, constructor); + body.Emit(OpCodes.Pop); + body.Emit(OpCodes.Ret); + var liveVector = new MethodDefinition("LiveVector", Mono.Cecil.MethodAttributes.Public | + Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); + owner.Methods.Add(liveVector); + liveVector.Body.InitLocals = true; + liveVector.Body.Variables.Add(new VariableDefinition(unsafeReturn.ReturnType)); + body = liveVector.Body.GetILProcessor(); + body.Emit(OpCodes.Ldc_I8, 0L); + body.Emit(OpCodes.Call, create); + body.Emit(OpCodes.Stloc_0); + body.Emit(OpCodes.Ret); + body.Emit(OpCodes.Ret); + module.Write(deadAssembly); +} +var deadOutput = Path.Combine(deadDirectory, "Extraction"); +Run(["dotnet", ExtractorPath, "extract", deadAssembly, "UnreachableTail::One()", deadOutput]); +var deadSource = Path.Combine(deadOutput, "Extracted.lean"); +var deadText = File.ReadAllText(deadSource).ReplaceLineEndings("\n"); +RequireDecoded(deadText, "UnreachableTail::One"); +File.AppendAllText(deadSource, """ + + namespace Extracted + open CIL CIL.Extraction + + /-- Every excluded instruction retains its raw opcode, including both bytes of the prefix. -/ + theorem dead_tail : method0.code = [.ldc_i4 1, .ret, .unadmitted 22, .unadmitted 42, + .unadmitted 20, .unadmitted 65044, .unadmitted 111, .unadmitted 42] := rfl + #print axioms dead_tail + + /-- A mask that excludes the entry instruction. -/ + def hidden : List Bool := false :: method0Reachable.tail + cil_decoded_method forged := Method.ofRaw? profile environment method0Links hidden + method0Original method0TypeArgumentsRaw + + /-- The entry state in the certificate requires the first instruction to be live. -/ + theorem hidden_entry_rejected : Not (FeatureFlowValid rawLayouts.toArray + (evidenceConstants methodEvidence) (linkIndex forged) method0Closed profile + method0FeatureStates hidden forged.code) := by decide +kernel + #print axioms hidden_entry_rejected + end Extracted + + """); +CheckLean(deadSource); +Console.WriteLine("PASS the decoded tail preserves its holes and the feature check rejects a hidden entry"); + +foreach (var (name, selector, method, code) in new[] +{ + ("SameText", "Strings::SameText()", 0, + """[.ldstr "x", .ldstr "x", .ceq, .ret]"""), + ("WordFlag", "GenericReceivers::CountWide(ulong)", 2, """ + (let word := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.UInt64" + let getType := CIL.Extraction.getTypeFromHandle.method + [.ldtoken (.type word), .call getType, .ldtoken (.type word), .call getType, + .call (CIL.Extraction.runtimeTypeComparison true).method, .ret]) + """) +}) +{ + var output = Path.Combine(directory, name); + Run(["dotnet", ExtractorPath, "extract", At("Examples/Assembly/bin/Examples.dll"), selector, output]); + var pathToSource = Path.Combine(output, "Extracted.lean"); + RequireDecoded(File.ReadAllText(pathToSource), name, method); + File.AppendAllText(pathToSource, $$""" + + /-- The closed example decodes to its exact instruction sequence. -/ + theorem decoded_code : Extracted.method{{method}}.code = {{code}} := rfl + #print axioms decoded_code + + """); + if (name == "WordFlag") + File.AppendAllText(pathToSource, $$""" + + open CIL CIL.Extraction + + /-- Both type-handle opcodes are required for this closed equality fold. -/ + theorem token_opcodes : + let code := Extracted.method{{method}}Closed.code + let token := {{(ushort)OpCodes.Ldtoken.Value}} + let nop := RawInstruction.mk 0 {{OpCodes.Nop.Value}} .none + let opcode := fun (pc : Nat) => (code[pc]?).map RawInstruction.opcode + (opcode 0, opcode 2) == (some token, some token) && + foldRoles code == [some .typeHead, some .typeOperand, some .typeOperand, some .typeOperand, + some (.result true), none] && + ([0, 2].all fun pc => foldRoles (code.set pc nop) == List.replicate 6 none) := by + decide +kernel + #print axioms token_opcodes + + """); + CheckLean(pathToSource); +} + +CheckClassAddress(directory); +CheckReceiverMethods(directory); + +var frameOutput = Path.Combine(deadDirectory, "Frame"); +Run(["dotnet", ExtractorPath, "extract", deadAssembly, "UnreachableTail::Frame(int[],int[])", frameOutput]); +var framePath = Path.Combine(frameOutput, "Extracted.lean"); +var frames = File.ReadAllText(framePath); +RequireDecoded(frames, "UnreachableTail::Frame"); +RequireDecoded(frames, "UnreachableTail::VectorLocal", 1); +File.AppendAllText(framePath, """ + + namespace Extracted + open CIL CIL.Extraction + + /-- Only the live store homes its parameter. The other parameter's dead address is a hole. -/ + theorem dead_frame : + (method0.parameterHomes, method1.localKinds, method1.opaqueVectorLocals, + method0.code[4]?, method1.code[2]?, method1.code[4]?) = + ([(0, .array .signed32)], [.vector128], [], some (.unadmitted 15), + some (.unadmitted 40), some (.unadmitted 115)) := rfl + #print axioms dead_frame + + /-- A link at the dead runtime call or the dead construction fails the call check. -/ + theorem dead_links : + let fits := fun (method : Method) => ManagedCallsValid specializedMethods imports + (callModelled method) method1Closed method1Reachable method + And (fits method1) (And (Not (fits { method1 with links := [(2, .direct 999)] })) + (Not (fits { method1 with links := [(4, .direct 0)] }))) := by decide +kernel + #print axioms dead_links + end Extracted + + """); +CheckLean(framePath); +Console.WriteLine("PASS dead argument addresses, vector calls and constructions preserve the frame and reject links"); + +var liveOutput = Path.Combine(deadDirectory, "LiveVector"); +Run(["dotnet", ExtractorPath, "extract", deadAssembly, "UnreachableTail::LiveVector()", liveOutput]); +var livePath = Path.Combine(liveOutput, "Extracted.lean"); +RequireDecoded(File.ReadAllText(livePath), "UnreachableTail::LiveVector"); +var vectorTemplate = Path.Combine(deadDirectory, "VectorTemplate"); +Run(["dotnet", ExtractorPath, "templates", vectorTemplate, liveOutput]); +var vectorPath = Path.Combine(vectorTemplate, "Templates.lean"); +File.AppendAllText(vectorPath, """ + + /-- The decoded template retains the opaque slot needed by live vector creation. -/ + theorem live_vector_template_frame (profile : CIL.FeatureProfile) (environment : CIL.LayoutEnvironment) : + (Templates.method0 profile environment).opaqueVectorLocals = [0] := rfl + #print axioms live_vector_template_frame + + """); +CheckLean(vectorPath); +File.AppendAllText(livePath, """ + + namespace Extracted + /-- A live scalar-unsafe creation requires the vector slot even with a dead final return. -/ + theorem live_vector_frame : And (method0.opaqueVectorLocals = [0]) + (And (method0.code[4]? = some (.unadmitted 42)) + (CIL.Extraction.OpaqueLocalsValid method0Closed method0)) := + .intro rfl (.intro rfl method0_opaque_validated) + #print axioms live_vector_frame + end Extracted + + """); +CheckLean(livePath); + +var examples = At("Examples/Assembly/bin/Examples.dll"); +var getter = Path.Combine(directory, "Both"); +var trace = Path.Combine(directory, "Trace"); +var shared = Path.Combine(directory, "Templates"); +Run(["dotnet", ExtractorPath, "extract", examples, "ComputedFlags::get_Both()", getter]); +Run(["dotnet", ExtractorPath, "extract", examples, "ComputedFlags::TraceGated(ulong)", trace]); +RequireDecoded(File.ReadAllText(Path.Combine(getter, "Extracted.lean")), "ComputedFlags::get_Both"); +RequireDecoded(File.ReadAllText(Path.Combine(trace, "Extracted.lean")), "ComputedFlags::TraceGated"); +Run(["dotnet", ExtractorPath, "templates", shared, getter]); +var searchPath = string.Join(Path.PathSeparator, getter, shared, FixtureLeanPath()); +foreach (var (root, name) in new[] { (getter, "Extracted"), (shared, "Templates"), (shared, "TemplateBindings0") }) +{ + var result = Run(["lean", $"--root={root}", "-o", Path.Combine(root, $"{name}.olean"), + Path.Combine(root, $"{name}.lean")], leanPath: searchPath); + if (result.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException($"Admitted declaration in {name}."); +} +CheckLean(Path.Combine(trace, "Extracted.lean")); +Console.WriteLine("PASS get_Both binds to its template and TraceGated's frame checks"); + +static void CheckLean(string path) +{ + var output = Run(["lake", "env", "lean", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException($"Admitted declaration in {path}."); +} + +static void RequireDecoded(string source, string name, int number = 0) +{ + if (!IsDecodedMethod(source, number)) throw new InvalidOperationException($"{name} must decode."); +} + +static void CheckClassAddress(string directory) +{ + var bytes = File.ReadAllBytes(At("Examples/Assembly/bin/Examples.dll")); + var hash = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes)); + using var pinned = new PinnedAssemblies(bytes, hash, []); + var module = pinned.Selected; + var definition = new MethodDefinition("M", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); + module.GetType("Examples.Objects").Methods.Add(definition); + definition.GenericParameters.Add(new GenericParameter("T", definition)); + definition.Body.GetILProcessor().Emit(OpCodes.Ret); + var address = new GenericInstanceMethod(definition); + address.GenericArguments.Add(module.GetType("Examples.Account")); + var target = new MethodSpecialization(module).Instantiate(address); + var classAddress = Path.Combine(directory, "ClassAddress.lean"); + File.WriteAllText(classAddress, $$""" + import CIL.Extraction.ValueDecoding + + /-! The literal and decoded identities of a static method specialized at a class. -/ + open CIL CIL.Extraction + + /-- Both paths retain the class argument, excluding the same name encoded as an atom. -/ + theorem class_address_identity : + let scope := {{System.Text.Json.JsonSerializer.Serialize(module.Assembly.Name.FullName)}} + let account := TypeExpr.object scope "Examples.Account" + let site : CallSite := { + declared := { convention := 0x10 }, name := "M", typeArguments := [account], + target := .managed {{target.MetadataToken.ToUInt32()}}, parameterCount := 0, returnsValue := false, + method := { + owner := .object scope "Examples.Objects", name := name!"M", hasThis := false, + genericArity := 1, returnType := .void, parameters := [] } } + let literal : ManagedIdentity := {{ManagedPointers.IdentityEvidence(target)}} + Instruction.ofRaw? none ⟨0, 0xfe06, .call site⟩ = + some (.ldftn literal ⟨.void, []⟩) ∧ + literal.arguments ≠ [.atom scope "Examples.Account"] := by + constructor + · rfl + · decide +kernel + #print axioms class_address_identity + """); + CheckLean(classAddress); +} + +static void CheckReceiverMethods(string directory) +{ + var assembly = At("Examples/Assembly/bin/Examples.dll"); + using var module = ModuleDefinition.ReadModule(assembly); + (string Owner, string[] Names)[] families = + [ + ("Objects", ["DepositBoth", "DepositFrom", "DepositTimes"]), + ("SampleArrays", ["CountAfterDeposit"]), + ("VirtualCalls", ["DogLegs", "LegsOf", "LoudnessOf"]), + ("ItemPool", ["Rent"]), + ("IndirectValues", ["Generic", "Record", "Reference", "Vector"]), + ("GenericOwners", ["Address"]), + ("ManagedDispatch", ["LocalEscape"]) + ]; + foreach (var profile in new[] { "scalar", "x64-v3", "arm64-advsimd" }) + { + var requests = families.SelectMany(family => family.Names.Select(name => + { + var owner = module.GetType($"Examples.{family.Owner}"); + var method = owner.Methods.Single(candidate => candidate.Name == name); + var output = Path.Combine(directory, profile, family.Owner, name); + return new GeneratedExtraction(assembly, method.FullName, output, profile); + })).ToArray(); + ExtractBatch("extract", null, requests); + foreach (var request in requests) + { + var path = Path.Combine(request.Output, "Extracted.lean"); + RequireDecoded(File.ReadAllText(path), request.Entry); + CheckLean(path); + } + } +} + +static void CheckPrefixes() +{ + var bytes = File.ReadAllBytes(At("Examples/Assembly/bin/Examples.dll")); + var hash = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes)); + using var pinned = new PinnedAssemblies(bytes, hash, []); + var module = pinned.Selected; + var method = new MethodDefinition("Body", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); + var element = module.TypeSystem.Int32; + static Instruction Raw(OpCode opcode, object? operand = null) + { + var instruction = Instruction.Create(OpCodes.Nop); + instruction.OpCode = opcode; + instruction.Operand = operand; + return instruction; + } + + void Check(bool accepted, params Instruction[] sequence) + { + method.Body.Instructions.Clear(); + foreach (var instruction in sequence) method.Body.Instructions.Add(instruction); + try { InstructionPrefixes.Validate(method); } + catch (InvalidDataException) when (!accepted) { return; } + if (!accepted) + { + var opcodes = string.Join(" ", sequence.Select(instruction => instruction.OpCode.Name)); + throw new InvalidOperationException($"Accepted prefix sequence {opcodes}"); + } + } + + foreach (var alignment in new byte[] { 1, 2, 4 }) + Check(true, Raw(OpCodes.Unaligned, alignment), Raw(OpCodes.Ldind_I4)); + Check(true, Raw(OpCodes.Readonly), Raw(OpCodes.Ldelema, element)); + Check(true, Raw(OpCodes.Tail), Raw(OpCodes.Call, method), Raw(OpCodes.Ret)); + Check(true, Raw(OpCodes.Tail), Raw(OpCodes.Calli, new CallSite(method.ReturnType)), Raw(OpCodes.Ret)); + var receiver = module.GetType("Examples.StepOn"); + var slot = module.GetType("Examples.IStepFlag").Properties.Single().GetMethod; + Check(false, Raw(OpCodes.Ret), Raw(OpCodes.Constrained, element), Raw(OpCodes.Call, method)); + Check(true, Raw(OpCodes.Constrained, receiver), Raw(OpCodes.Call, slot)); + Check(false, Raw(OpCodes.Ret), Raw(OpCodes.Tail), Raw(OpCodes.Calli, new CallSite(element)), Raw(OpCodes.Ret)); + Check(false, Raw(OpCodes.Ret), Raw(OpCodes.Tail), Raw(OpCodes.Call, slot), Raw(OpCodes.Ret)); + var instance = new MethodReference("Instance", element, element) { HasThis = true }; + Check(true, Raw(OpCodes.Constrained, element), Raw(OpCodes.Callvirt, instance)); + var virtualVoid = new MethodReference("Visit", method.ReturnType, element) { HasThis = true }; + Check(true, Raw(OpCodes.Ret), Raw(OpCodes.Tail), Raw(OpCodes.Callvirt, virtualVoid), Raw(OpCodes.Ret)); + try + { + TailCalls.ValidateReachable(method, method.Body.Instructions[1]); + throw new InvalidOperationException("Accepted a reachable virtual tail transfer"); + } + catch (InvalidDataException error) when (error.Message.StartsWith("Unsupported tail prefix:", + StringComparison.Ordinal)) { } + foreach (var flags in Enumerable.Range(0, 256)) + Check(false, Raw(OpCodes.No, (byte)flags), Raw(OpCodes.Ldelem_I4)); + Check(false, Raw(OpCodes.Ret), Raw(OpCodes.No, (byte)255), Raw(OpCodes.Add)); + Check(false, Raw(OpCodes.Volatile), Raw(OpCodes.Ldind_I4)); + Check(false, Raw(OpCodes.Ret), Raw(OpCodes.Volatile), Raw(OpCodes.Ldsfld)); + Check(false, Raw(OpCodes.Unaligned, (byte)3), Raw(OpCodes.Ldfld)); + Check(false, Raw(OpCodes.Unaligned), Raw(OpCodes.Ldind_I4)); + Check(false, Raw(OpCodes.Unaligned, (byte)1), Raw(OpCodes.Add)); + Check(false, Raw(OpCodes.Unaligned, (byte)1), Raw(OpCodes.Cpblk, 1)); + Check(false, Raw(OpCodes.Unaligned, (byte)1), Raw(OpCodes.Ldind_I4, 1)); + Check(false, Raw(OpCodes.Readonly, 3), Raw(OpCodes.Ldelema, element)); + Check(false, Raw(OpCodes.Readonly), Raw(OpCodes.Add)); + Check(false, Raw(OpCodes.Constrained), Raw(OpCodes.Call, method)); + Check(false, Raw(OpCodes.Constrained, element), Raw(OpCodes.Call, instance)); + Check(false, Raw(OpCodes.Constrained, element), Raw(OpCodes.Add)); + Check(false, Raw(OpCodes.Tail, 1), Raw(OpCodes.Call, method), Raw(OpCodes.Ret)); + Check(false, Raw(OpCodes.Tail), Raw(OpCodes.Add)); + Check(false, Raw(OpCodes.Tail), Raw(OpCodes.Call, method)); + foreach (var opcode in new[] { OpCodes.Unaligned, OpCodes.Tail, OpCodes.Constrained, OpCodes.Readonly }) + Check(false, Raw(opcode)); + Check(false, Raw(OpCodes.Readonly), Raw(OpCodes.Readonly), Raw(OpCodes.Ldelema, element)); + Check(false, Raw(OpCodes.Unaligned, (byte)1), Raw(OpCodes.Volatile), Raw(OpCodes.Ldind_I4)); + Console.WriteLine("PASS extractor prefix operands, takers and adjacency"); +} diff --git a/Tests/DecodedSegment.lean b/Tests/DecodedSegment.lean new file mode 100644 index 000000000..f3d6d9b29 --- /dev/null +++ b/Tests/DecodedSegment.lean @@ -0,0 +1,144 @@ +import CIL.Safety.DecodedSegment +import Tests.DecodedSummary + +/-! A summary of the adder's instruction at pc 2, reused after reversing its argument loads. -/ + +namespace Tests.DecodedSegment +open CIL CIL.Extraction CIL.Safety Tests.BodyEncoding Tests.BodyDecoding Tests.DecodedMethod +open Tests.DecodedSummary + +/-- The stack supplies both addends. Only their sum remains when control reaches the return. -/ +private def addStack (left right : W64) : SegmentContract := + .continuing 3 (fun _ before => before.stack = [.scalar (.i64 right), .scalar (.i64 left)]) + (fun _ before after => after = { before with stack := [.scalar (.i64 (left + right))] }) + (fun _ _ => []) + +/-- Stepping the add on the decoded body proves its framed continuation for every incoming frame. -/ +theorem add_region (left right : W64) : + DecodedSegmentSummary [addMethod] [addItem] testProfile testEnvironment 0 2 pin + addItem.original.token [] .add [] (addStack left right) := by + refine ⟨decoded_summary.bound, ⟨addMethod, rfl, rfl⟩, ?_⟩ + rintro args ⟨frame, stack, memory⟩ permitted + change stack = _ at permitted + subst stack + let after : SegmentState := ⟨frame, [.scalar (.i64 (left + right))], memory⟩ + refine ⟨.reaches 3 after, ⟨1, by decide, SegmentRun.reaches ?_⟩, Confined.refl _ _, + rfl, rfl⟩ + intro fuel + rfl + +#print axioms add_region + +/-- Swapping the two loads changes the method bytes outside the arithmetic suffix. -/ +private def swappedRaw : RawMethod := { addArgumentsRaw with + code := [instr 0 0x03, instr 1 0x02, instr 2 0x58, instr 3 0x2A] } + +/-- Bytes and metadata encoded for the reversed loads. -/ +private def swappedBody := (encodeBody pin swappedRaw).get (by decide +kernel) +/-- The decoded method with the reversed prefix. -/ +private def swappedMethod := { addMethod with code := [.ldarg 1, .ldarg 0, .add, .ret] } +/-- The second extraction carries the new body under the same method identity. -/ +private def swappedItem : MethodEvidence := { addItem with + original := swappedRaw, closed := swappedRaw, body := swappedBody } + +/-- The new body decodes and its own metadata still binds the selected method. -/ +private theorem swapped_bound : + DecodedAt [swappedMethod] [swappedItem] testProfile testEnvironment 0 + pin addItem.original.token [] := by + apply DecodedAt.of_decoded (imports := []) (item := swappedItem) + · cil_kernel_rfl + · rfl + · exact ⟨[], by decide +kernel⟩ + · cil_kernel_rfl + +#print axioms swapped_bound + +/-- The add and its return form a closed region starting at instruction two. -/ +private abbrev suffix (method pc : Nat) := inStarts [(0, 2)] method pc = true + +/-- The changed prefix needs no new proof of the add's stack result or memory confinement. -/ +theorem reuse_after_changed_loads (left right : W64) : + DecodedSegmentSummary [swappedMethod] [swappedItem] testProfile testEnvironment 0 2 pin + addItem.original.token [] .add [] (addStack left right) := by + apply (add_region left right).congr_region (region := suffix) swapped_bound + (RegionClosed.of_check (by decide +kernel)) + · exact AgreesOn.set (program := [addMethod]) (index := 0) rfl swappedMethod.code fun _ inside => + code_agrees_of_drop (lower := 2) rfl (by simpa [suffix, inStarts] using inside) + · intro i _ + simp [suffix, inStarts] + · intro args before pc after _ post + obtain ⟨rfl, _⟩ := post + decide + +#print axioms reuse_after_changed_loads + +/-- Substitution of the instruction inside the region invalidates its placement. -/ +theorem wrong_instruction_refused (left right : W64) : + ¬ DecodedSegmentSummary [swappedMethod] [swappedItem] testProfile testEnvironment 0 2 pin + addItem.original.token [] .sub [] (addStack left right) := by + intro summary + obtain ⟨body, present, placed⟩ := summary.placed + cases present + have wrong := placed.lookup 0 (op := .sub) rfl + contradiction + +#print axioms wrong_instruction_refused + +/-- A different return convention invalidates reuse even when the suffix bytes agree. -/ +theorem changed_return_shape_refused : + ¬ AgreesOn [addMethod] [{ swappedMethod with returnsValue := false }] suffix := by + intro agrees + obtain ⟨body, found, metadata, _⟩ := agrees.2 0 2 addMethod (by decide) rfl + cases found + have same := congrArg Method.returnsValue metadata + contradiction + +#print axioms changed_return_shape_refused + +/-- The right operand is the second argument. Loading it leaves the incoming frame and memory + intact and places it above the left operand. -/ +private def loadRight (left right : W64) : SegmentContract := + .continuing 2 + (fun args before => args = [.scalar (.i64 left), .scalar (.i64 right)] ∧ + before.stack = [.scalar (.i64 left)]) + (fun _ before after => after = + { before with stack := [.scalar (.i64 right), .scalar (.i64 left)] }) (fun _ _ => []) + +/-- The decoded load at pc 1 meets the arithmetic region's stack precondition at pc 2. -/ +private theorem load_region (left right : W64) : + DecodedSegmentSummary [addMethod] [addItem] testProfile testEnvironment 0 1 pin + addItem.original.token [] (.ldarg 1) [] (loadRight left right) := by + refine ⟨decoded_summary.bound, ⟨addMethod, rfl, rfl⟩, ?_⟩ + rintro args ⟨frame, stack, memory⟩ ⟨rfl, rfl⟩ + let after : SegmentState := ⟨frame, [.scalar (.i64 right), .scalar (.i64 left)], memory⟩ + refine ⟨.reaches 2 after, ⟨1, by decide, SegmentRun.reaches ?_⟩, Confined.refl _ _, + rfl, rfl⟩ + intro fuel + rfl + +#print axioms load_region + +/-- Joining the two summaries certifies the load and add at pcs 1 and 2. The sum replaces the + left operand at pc 3, with the incoming frame and memory. -/ +theorem load_then_add (left right : W64) : + DecodedSegmentSummary [addMethod] [addItem] testProfile testEnvironment 0 1 pin + addItem.original.token [] (.ldarg 1) [.add] + (.continuing 3 (loadRight left right).pre + (fun _ before final => final = { before with stack := [.scalar (.i64 (left + right))] }) + (fun _ _ => [])) := by + have joined := (load_region left right).then (footprint := fun _ _ => []) + (add_region left right) (by intro args before after _ same; subst after; rfl) + (by intros; exact ⟨fun _ h => h, fun _ h => h⟩) + refine ⟨joined.bound, joined.placed, ?_⟩ + intro args before permitted + obtain ⟨exit, ran, framed, post⟩ := joined.holds args before permitted + refine ⟨exit, ran, framed, ?_⟩ + cases exit with + | reaches pc final => + obtain ⟨rfl, after, rfl, rfl⟩ := post + exact ⟨rfl, rfl⟩ + | returns | faults | diverges => exact post + +#print axioms load_then_add + +end Tests.DecodedSegment diff --git a/Tests/DecodedSummary.lean b/Tests/DecodedSummary.lean new file mode 100644 index 000000000..a0bcbb139 --- /dev/null +++ b/Tests/DecodedSummary.lean @@ -0,0 +1,226 @@ +import CIL.Safety.DecodedSummary +import CIL.Safety.SymbolicExecution +import Tests.DecodedMethod +import Tests.ProfileContract + +/-! +# Decoded summary tests + +A contract proved for the world's adder, at index 1 of a program whose caller at index 0 calls +it, is a `DecodedSummary` bound to the adder's pinned bytes, metadata token and type +arguments. The bodies of a one-record program decode to that program through +`decodedProgram?_eq_some`, and `DecodedSummary.of_decoded` builds a summary from the decoded +program. At the caller's call site, `DecodedSummary.site` shows the summary is for the method +the site's token names, and the caller's proof composes the call through the summary alone. +-/ + +namespace Tests.DecodedSummary + +open CIL CIL.Extraction CIL.Safety Tests.BodyEncoding Tests.BodyDecoding Tests.MetadataWorld +open Tests.DecodedMethod + +/-- The adder's pinned bytes, `12 02 03 58 2A` against its metadata. -/ +def addBody : BodyEvidence := (encodeBody pin addArgumentsRaw).get (by decide +kernel) + +cil_decoded_method addMethod := + Method.ofRaw? testProfile testEnvironment [] (allLive addArgumentsRaw) addArgumentsRaw [] +cil_instruction_equations addMethod +cil_resolution_facts addMethod + +/-- The call site of the adder, the world's `Program.Main` given two `uint64` parameters. -/ +def addSite : CallSite := { + declared := { convention := 0, returnType := uint64, parameters := [uint64, uint64] }, + method := { + owner := .object ownIdentity "Program", name := name!"Main", hasThis := false, + genericArity := 0, returnType := uint64, parameters := [uint64, uint64] }, + name := "Main", target := .managed tokens.main, typeArguments := [], parameterCount := 2, + returnsValue := true } + +/-- `ldarg.0; ldarg.1; call Main(uint64, uint64); ret`, a method with the next token. -/ +def callerRaw : RawMethod := + { method 8 false [] [instr 0 2, instr 1 3, instr 2 0x28 (.call addSite), instr 7 0x2A] with + token := tokens.main + 1, parameters := [uint64, uint64], returnType := uint64 } + +cil_decoded_method callerMethod := + Method.ofRaw? testProfile testEnvironment [(2, .direct 1)] (allLive callerRaw) callerRaw [] +cil_instruction_equations callerMethod +cil_resolution_facts callerMethod + +/-- The caller at 0 and the adder at 1. -/ +def program : CIL.Program := [callerMethod, addMethod] + +/-- The adder's record, with its bytes. -/ +def addItem : MethodEvidence := { item addArgumentsRaw addBody with + assembly := pin, featureStates := addFeatureStates, + signature := (spelled? addBody.names.signature).getD "" } + +/-- The adder's row and body bind the selector and assembly of its summary. -/ +theorem add_body_bound : addItem.BodyBound := ⟨[], by decide +kernel⟩ +#print axioms add_body_bound + +/-- A different assembly label cannot reuse the adder's unchanged bytes and metadata. -/ +theorem rejects_substituted_assembly : + ¬ DecodedAt [addMethod] [{ addItem with assembly := ⟨"Elsewhere"⟩ }] + testProfile testEnvironment 0 ⟨"Elsewhere"⟩ tokens.main [] := by + rintro ⟨record, found, _, _, _, _, ⟨_, body⟩, _⟩ + cases found + exact (by decide : addBody.metadata.own.assembly.identity ≠ "Elsewhere") body.2.2.1 +#print axioms rejects_substituted_assembly + +/-- The records of `program`; the caller's carries the adder's bytes, so only the adder's record + decodes to its method. -/ +def evidence : List MethodEvidence := + [{ item callerRaw addBody with links := [(2, .direct 1)] }, addItem] + +/-- The adder's bytes decode to the adder. -/ +theorem add_body_decodes : + Method.ofBody? testProfile testEnvironment [] (allLive addArgumentsRaw) + addBody addArgumentsRaw [] = some addMethod := by + cil_kernel_rfl +#print axioms add_body_decodes + +/-- An evidence-only change of a calli catalog from method 1 to 0 fails translation. -/ +theorem changed_indirect_index_refused {rawLayouts layouts pins platform all imports} + (record : MethodEvidence) (emitted : Method) (handler : ManagedIdentity) + (recorded : linkLookup record.links 18 = some (.indirect [⟨handler, 0⟩])) + (checked : emitted.linkAt 18 = some (.indirect [⟨handler, 1⟩])) : + ¬ MethodEvidenceValid rawLayouts layouts pins platform all imports record emitted := by + intro valid + rw [valid.links_eq] at recorded + change linkLookup emitted.links 18 = _ at checked + rw [checked] at recorded + cases recorded +#print axioms changed_indirect_index_refused + +/-- A one-record program decodes record by record, so `decodedProgram?` gives it. -/ +theorem adder_program_decodes : + decodedProgram? testProfile testEnvironment [addItem] = some [addMethod] := + decodedProgram?_eq_some (by decide +kernel) (.cons add_body_decodes .nil) +#print axioms adder_program_decodes + +/-- The original certificate marks the add live, while the supplied mask hides it. -/ +private def hiddenAdd := { addItem with reachable := [true, true, false, true] } + +/-- The byte decoder under the forged mask replaces the live add with its raw opcode. -/ +private def hiddenMethod := { addMethod with code := [.ldarg 0, .ldarg 1, .unadmitted 88, .ret] } + +/-- Neither the decoded program nor a summary with the original identity accepts the hidden add. -/ +theorem hidden_add_rejected : + Method.ofBody? testProfile testEnvironment [] hiddenAdd.reachable addBody addArgumentsRaw [] = + some hiddenMethod ∧ + decodedProgram? testProfile testEnvironment [hiddenAdd] = none ∧ + ¬ DecodedAt [hiddenMethod] [hiddenAdd] testProfile testEnvironment 0 pin tokens.main [] := by + refine ⟨by cil_kernel_rfl, by decide +kernel, ?_⟩ + rintro ⟨record, found, _, _, _, _, _, method, present, _, valid⟩ + simp only [List.getElem?_cons_zero, Option.some.injEq] at found + subst record + cases present + exact (by decide +kernel : ¬ hiddenAdd.MaskValid [hiddenAdd] hiddenMethod) valid +#print axioms hidden_add_rejected + +/-- An empty body fails to decode even though the adder's definition still instantiates. -/ +private def emptyBody := { addItem with body.bytes := [] } + +/-- A failed body decode and an absent program entry cannot bind the adder's record. -/ +theorem missing_method_rejected : + instantiateMethod testEnvironment.rawLayouts emptyBody.original [] = some emptyBody.closed ∧ + Method.ofBody? testProfile testEnvironment emptyBody.links emptyBody.reachable emptyBody.body + emptyBody.original emptyBody.arguments = none ∧ + ¬ DecodedAt [] [emptyBody] testProfile testEnvironment 0 pin tokens.main [] := by + refine ⟨by cil_kernel_rfl, by cil_kernel_rfl, ?_⟩ + rintro ⟨_, _, _, _, _, _, _, _, present, _⟩ + cases present +#print axioms missing_method_rejected + +/-- `(left, right)` returns `left + right` and changes no memory. -/ +def addContract : MethodContract (W64 × W64) W64 where + args := fun (left, right) => [.scalar (.i64 left), .scalar (.i64 right)] + results sum := [.scalar (.i64 sum)] + pre _ _ := True + footprint _ := [] + post := fun (left, right) initial final sum => sum = left + right ∧ final = initial + +/-- The adder meets its contract at index 1 of `program`. -/ +theorem add_holds : addContract.Holds program 1 := by + rintro ⟨left, right⟩ memory - + refine MethodContract.Holds.intro memory (left + right) ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + cil_checked_steps +#print axioms add_holds + +/-- The adder meets its contract as the only method of a program. -/ +theorem add_holds_alone : addContract.Holds [addMethod] 0 := by + rintro ⟨left, right⟩ memory - + refine MethodContract.Holds.intro memory (left + right) ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + cil_checked_steps +#print axioms add_holds_alone + +/-- The adder's contract, bound to its record at index 1. -/ +theorem addSummary : DecodedSummary program evidence testProfile testEnvironment 1 pin + tokens.main [] addContract := + ⟨add_holds, addItem, rfl, rfl, rfl, rfl, by cil_kernel_rfl, add_body_bound, addMethod, rfl, + add_body_decodes, by decide +kernel⟩ +#print axioms addSummary + +/-- A summary of the decoded one-record program is bound to the adder's identity. -/ +theorem decoded_summary : DecodedSummary [addMethod] [addItem] testProfile testEnvironment 0 pin + tokens.main [] addContract := + .of_decoded (index := 0) adder_program_decodes rfl add_body_bound (by cil_kernel_rfl) add_holds_alone +#print axioms decoded_summary + +/-- The scalar adder's summary also certifies the same body and contract under x86-64-v3. -/ +theorem add_summary_x64 : + DecodedSummary (reprofile [addMethod] Tests.ProfileContract.avx2Profile) [addItem] + Tests.ProfileContract.avx2Profile testEnvironment 0 pin tokens.main [] addContract := + decoded_summary.reprofile (by decide +kernel) + (Program.ProfileAgreement.of_flags _ _ _ (by decide +kernel)) + (imports := []) (by cil_kernel_rfl) +#print axioms add_summary_x64 + +/-- Reusing the scalar outcome proof under x86-64-v3 retains the adder's pinned body. -/ +theorem add_outcome_x64 : + DecodedOutcomeSummary (reprofile [addMethod] Tests.ProfileContract.avx2Profile) [addItem] + Tests.ProfileContract.avx2Profile testEnvironment 0 pin tokens.main [] + { addContract with raises := fun _ _ => none } := by + let outcome : OutcomeContract (W64 × W64) W64 := + { addContract with raises := fun _ _ => none } + have source : DecodedOutcomeSummary [addMethod] [addItem] testProfile testEnvironment 0 + pin tokens.main [] outcome := ⟨add_holds_alone.outcomes, decoded_summary.bound⟩ + exact source.reprofile (by decide +kernel) + (Program.ProfileAgreement.of_flags _ _ _ (by decide +kernel)) + (imports := []) (by cil_kernel_rfl) +#print axioms add_outcome_x64 + +/-- The caller's call check, against the records' method table. -/ +theorem caller_calls_valid : + ManagedCallsValid (specializationTable evidence) [] (callModelled callerMethod) callerRaw + [true, true, true, true] callerMethod := by + decide +kernel +#print axioms caller_calls_valid + +/-- Every summary of the index the caller's call enters is for the method its site names. -/ +theorem summary_names_site {token : Nat} {arguments : List TypeExpr} + (summary : DecodedSummary program evidence testProfile testEnvironment 1 pin token arguments + addContract) : token = tokens.main ∧ arguments = [] := + summary.site caller_calls_valid (pc := 2) rfl rfl rfl rfl rfl rfl +#print axioms summary_names_site + +/-- A summary of the index the caller's call enters, bound to another token, is refuted at that + call. -/ +theorem other_token_refused + (summary : DecodedSummary program evidence testProfile testEnvironment 1 pin + (tokens.main + 1) [] addContract) : False := by + have named := (summary_names_site summary).1 + omega +#print axioms other_token_refused + +/-- The caller returns the sum, proved from the adder's summary without stepping the adder. -/ +theorem caller_returns (memory : Safety.Memory) (left right : W64) : + WP (Execution (fun fuel => invoke program fuel 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory)) + (fun (final, values) => final = memory ∧ values = [.scalar (.i64 (left + right))]) := by + cil_contract_steps [addSummary.holds, addContract] with after sum added + obtain ⟨rfl, rfl⟩ := added.post + exact ⟨rfl, rfl⟩ +#print axioms caller_returns + +end Tests.DecodedSummary diff --git a/Tests/DeepBinding.cs b/Tests/DeepBinding.cs new file mode 100644 index 000000000..4677934db --- /dev/null +++ b/Tests/DeepBinding.cs @@ -0,0 +1,178 @@ +using System.Text.Json; +using System.Text.RegularExpressions; + +partial class Program +{ + /// Joint proof and image binding verification with explicit consumer digests. + static void CheckDeepBinding(string root, string[] cli, string extraction, string app, string lib, + string libName, string[] verify, bool controlsOnly = false, bool finalOnly = false) + { + var manifest = Path.Combine(root, "deep-inputs.sha256"); + File.WriteAllText(manifest, Sha(app) + " " + Path.GetRelativePath(root, app) + "\n" + + Sha(lib) + " " + Path.GetRelativePath(root, lib) + "\n"); + if (!controlsOnly && !finalOnly) + { + var result = Execute([.. verify, "--deep-checks", manifest, "--format", "json"]); + if (result.Code != 0) throw new InvalidOperationException(result.Output); + var report = JsonSerializer.Deserialize(result.Stdout); + var binding = report.GetProperty("binding"); + if (binding.GetProperty("mode").GetString() != "deep" || + binding.GetProperty("facts").GetArrayLength() != 12 || + binding.GetProperty("images").GetArrayLength() != 2 || + binding.GetProperty("facts").EnumerateArray().Count(fact => + fact.GetProperty("status").GetString() == "reconfirmed") != 3) + throw new InvalidOperationException("Unexpected joint binding report: " + result.Stdout); + Console.WriteLine("PASS joint CLI verification reports image digests and all twelve facts"); + } + var inputs = DeepCheckInputs.Read(manifest); + var identity = ExtractionIdentity.Read(extraction); + var pins = inputs.MatchPins(identity.Root); + var images = pins.Select(pin => new BindingImagePin(pin.Identity, pin.Image.Digest, + pin.Image.Bytes.ToArray())).ToArray(); + var reference = images.Single(image => image.Identity.Split(',')[0] == libName); + var application = images.Single(image => image != reference); + var proof = verify[Array.IndexOf(verify, "--proof") + 1]; + var theorem = verify[Array.IndexOf(verify, "--theorem") + 1]; + var methods = identity.Root.GetProperty("methods").GetArrayLength(); + if (methods < 2) throw new InvalidOperationException("The binding fixture requires two method pages"); + + var sources = Path.Combine(root, "binding-sources"); + CopyBindingSources(sources); + var sourceKey = BindingSourcesKey(sources); + File.AppendAllText(Path.Combine(sources, "StrictBinding", "Bind.lean"), "\n-- changed\n"); + if (sourceKey != BindingSourcesKey(Root) || sourceKey == BindingSourcesKey(sources)) + throw new InvalidOperationException("Binding source key does not identify its sources"); + using Program checking = new() { freshProofs = true }; + + void Mutation(string name, string module, Func edit, string diagnostic, + BindingFact? rejectedFact = null, bool succeeds = false, string? rejectedStage = null) + { + if (controlsOnly && !succeeds) return; + if (finalOnly && name is not ("false later body page" or "wrong pinned identity theorem")) return; + DeepCheckRun run = new(inputs); + var mutated = false; + string stage = "proof selection"; + Exception? rejected = null; + try + { + checking.Verify(app, identity.Root.GetProperty("assemblyIdentity").GetString()!, + identity.Root.GetProperty("entry").GetString()!, proof, extraction, theorem: theorem, + existingArtifacts: true, deepChecks: inputs, deepRun: run, stage: next => + { + stage = next; + if (next != "deep-check sources") return; + var directory = Directory.EnumerateDirectories(extraction, "proof-*").Single(); + var bodies = ModuleSource(directory, BodiesModule); + File.WriteAllText(bodies, PagedFactModuleSource(BindingFact.BodyBytes, methods, 1)); + var path = ModuleSource(directory, module); + var source = File.ReadAllText(path); + var changed = edit(source); + if (!succeeds && changed == source) + throw new InvalidOperationException("Mutation changed nothing: " + name); + File.WriteAllText(path, changed); + mutated = true; + }); + } + catch (Exception error) { rejected = error; } + if (succeeds) + { + if (!mutated || rejected is not null || !File.Exists(Path.Combine(extraction, "verified.json"))) + throw new InvalidOperationException("Valid method pages failed: " + rejected); + Console.WriteLine("PASS valid method pages in joint verification"); + return; + } + if (!mutated || rejected is null || !rejected.Message.Contains(diagnostic, StringComparison.Ordinal)) + throw new InvalidOperationException(name + " was not rejected as expected: " + rejected); + if (rejectedStage is not null && stage != rejectedStage) + throw new InvalidOperationException(name + " failed during " + stage + ", expected " + rejectedStage); + if (File.Exists(Path.Combine(extraction, "verified.json"))) + throw new InvalidOperationException(name + " retained a successful certificate"); + var failed = run.Attempt!.Failure(stage, rejected.Message); + if (rejectedFact is { } fact && !failed.facts.Any(item => + item.id == JsonSerializer.SerializeToElement(fact).GetString() && + item.status == FailureReports.BindingFailureStatus.Failed)) + throw new InvalidOperationException(name + " lost its named audit rejection"); + Console.WriteLine("PASS " + name); + } + + Mutation("valid method pages", BodiesModule, source => source, "", succeeds: true); + Mutation("changed second page", BodiesModule, source => source.Replace( + "Extracted.methodEvidence) 1 1)", "Extracted.methodEvidence) 1 0)", StringComparison.Ordinal), + "Deep-check module build failed"); + Mutation("changed image digit", reference.Module, source => FirstChunkDigit.Replace(source, + match => match.Groups[1].Value + (match.Groups[2].Value == "0" ? "1" : "0"), 1), + "replay"); + Mutation("body theorem about other evidence", BodiesModule, source => ReplaceBodies(source, + "theorem bodies : StrictBinding.BodiesBound images [] := by decide +kernel"), + "Theorem statement mismatch", BindingFact.BodyBytes); + Mutation("platform kind theorem about other evidence", PlatformKindsModule, source => source.Replace( + "Extracted.methodEvidence kindCoverage% :=", "[] false :=", StringComparison.Ordinal), + "Theorem statement mismatch", BindingFact.PlatformTypeKinds); + Mutation("platform kind coverage claimed for an empty selection", PlatformKindsModule, source => source.Replace( + "Extracted.methodEvidence kindCoverage% :=", "Extracted.methodEvidence true :=", StringComparison.Ordinal), + "replay", BindingFact.PlatformTypeKinds); + Mutation("layout content about other layouts", LayoutContentModule, source => source.Replace( + "Extracted.rawLayouts Extracted.imports layoutCoverage% :=", "[] Extracted.imports false :=", + StringComparison.Ordinal), "Theorem statement mismatch", BindingFact.LayoutContent); + Mutation("layout coverage for an empty selection", LayoutContentModule, source => source.Replace( + "Extracted.imports layoutCoverage% :=", "Extracted.imports true :=", StringComparison.Ordinal), + "replay", BindingFact.LayoutContent); + Mutation("dispatch catalog about other methods", DispatchCatalogsModule, source => source.Replace( + "Extracted.methodEvidence Extracted.imports dispatchCoverage% :=", "[] Extracted.imports false :=", + StringComparison.Ordinal), "Theorem statement mismatch", BindingFact.DispatchCatalogs); + Mutation("dispatch coverage for an empty selection", DispatchCatalogsModule, source => source.Replace( + "Extracted.imports dispatchCoverage% :=", "Extracted.imports true :=", StringComparison.Ordinal), + "replay", BindingFact.DispatchCatalogs); + Mutation("generic constraints about other imports", GenericConstraintsModule, source => source.Replace( + "Extracted.imports constraintCoverage% :=", "[] false :=", StringComparison.Ordinal), + "Theorem statement mismatch", BindingFact.GenericConstraints); + Mutation("generic constraint coverage for an empty selection", GenericConstraintsModule, source => source.Replace( + "Extracted.imports constraintCoverage% :=", "Extracted.imports true :=", StringComparison.Ordinal), + "replay", BindingFact.GenericConstraints); + Mutation("enum theorem about other evidence", EnumStorageModule, source => source.Replace( + "Extracted.methodEvidence enumCoverage% :=", "[] false :=", StringComparison.Ordinal), + "Theorem statement mismatch", BindingFact.EnumUnderlyingTypes); + Mutation("enum coverage claimed for an empty selection", EnumStorageModule, source => source.Replace( + "Extracted.methodEvidence enumCoverage% :=", "Extracted.methodEvidence true :=", StringComparison.Ordinal), + "replay", BindingFact.EnumUnderlyingTypes); + const string statement = "StrictBinding.BodiesBound images Extracted.methodEvidence"; + Mutation("body theorem with extra axiom", BodiesModule, source => ReplaceBodies(source, + $"axiom cheat : {statement}\ntheorem bodies : {statement} := cheat"), + "axiom", BindingFact.BodyBytes); + Mutation("digest of other bytes", application.Module, + _ => ImageModuleSource(application with { Bytes = reference.Bytes }), "replay"); + Mutation("digest theorem about another constant", reference.Module, source => source + .Replace("import StrictBinding.Sha256", "import StrictBinding.Sha256\nimport " + application.Module, + StringComparison.Ordinal) + .Replace("theorem digest : StrictBinding.DigestIs image ", + "theorem digest : StrictBinding.DigestIs " + application.Module + ".image ", StringComparison.Ordinal), + "Deep-check module build failed"); + Mutation("false later body page", BodiesModule, source => source + .Replace("import BindingAudit.Images", "import Lean\nimport BindingAudit.Images", StringComparison.Ordinal) + .Replace("theorem page1 :", "theorem page1_valid :", StringComparison.Ordinal) + .Replace(BodiesDocstring, """ + set_option debug.skipKernelTC true in + run_cmd Lean.Elab.Command.liftCoreM do + let valid <- Lean.getConstInfo `BindingAudit.Bodies.page1_valid + Lean.addDecl (.thmDecl { + name := `BindingAudit.Bodies.page1, levelParams := [], type := valid.type, + value := Lean.mkConst ``True.intro }) + + """ + "\n" + BodiesDocstring, StringComparison.Ordinal), "BindingAudit.Bodies.page1", + BindingFact.BodyBytes, rejectedStage: "replay diagnostics"); + Mutation("wrong pinned identity theorem", ImagesModule, source => source.Replace( + "theorem images_eq : images =", + "theorem images_eq : images = images := rfl\ntheorem images_unused : images =", StringComparison.Ordinal), + "Theorem statement mismatch"); + } + /// The docstring that opens the `bodies` theorem, the last declaration of the bodies module. + const string BodiesDocstring = "/-- The method bodies of `Extracted.methodEvidence`"; + + /// The module with its `bodies` theorem, which closes the module, replaced by other text. + static string ReplaceBodies(string source, string replacement) => + source[..source.IndexOf(BodiesDocstring, StringComparison.Ordinal)] + replacement + "\n\nend BindingAudit\n"; + + [GeneratedRegex(@"(?m)^(def page0 : List Nat := [^\n]*?0x[0-9A-F]{40})([0-9A-F])")] + private static partial Regex FirstChunkDigit { get; } + +} diff --git a/Tests/DeepCheckInputs.cs b/Tests/DeepCheckInputs.cs new file mode 100644 index 000000000..a900c0819 --- /dev/null +++ b/Tests/DeepCheckInputs.cs @@ -0,0 +1,226 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/DeepCheckInputs.cs +#:include ../scripts/BindingImage.cs + +using System.Diagnostics; +using System.Reflection; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; + +var root = Path.Combine(Path.GetTempPath(), "LeanCIL-DeepInputs-" + Guid.NewGuid().ToString("N")); +Directory.CreateDirectory(root); +try +{ + var file = Path.Combine(root, "pins.sha256"); + byte[] original = [1, 2, 3, 4]; + var digest = Hash(original); + File.WriteAllBytes(Path.Combine(root, "first image.dll"), original); + File.WriteAllBytes(Path.Combine(root, "second.dll"), original); + var source = $"{digest.ToUpperInvariant()} first image.dll\r\n{digest} *second.dll\n"; + var inputs = Read(source); + Require(inputs.Images.Count == 2 && inputs.Digest.ToString() == Hash(Encoding.UTF8.GetBytes(source)), + "File snapshot hash"); + var pins = inputs.MatchPins(Identity(digest, [digest, digest], "Unrelated, Version=1.0.0.0")); + Require(pins.Count == 3 && pins.All(pin => pin.Image.Digest.ToString() == digest), + "Digest matching ignores names"); + var stated = Sha256Digest.Parse(digest.ToUpperInvariant(), "a test digest"); + Require(stated == Sha256Digest.Parse(digest, "a test digest") && stated == Sha256Digest.Of(original) && + stated.ToString() == digest && new HashSet { stated, Sha256Digest.Of(original) }.Count == 1, + "A digest stated in either case is one 32-byte value with lowercase text"); + var changed = digest[..^1] + (digest[^1] == '0' ? '1' : '0'); + Require(Sha256Digest.Parse(changed, "a test digest") != stated, "One changed digit is another digest"); + Reject(() => Sha256Digest.Parse(digest.AsSpan()[..^1], "a test digest"), "Invalid SHA-256 digest"); + Require(Read(source + $"{digest} ./first image.dll\n").Images.Count == 2, "Identical duplicate paths"); + Reject(() => Read(source + $"{new string('0', 64)} first image.dll\n"), "Conflicting SHA-256"); + Reject(() => inputs.MatchPins(Identity(new string('0', 64), [])), "No line"); + Reject(() => inputs.MatchPins(Identity(digest, [new string('0', 64)])), "No line"); + Reject(() => Read($"{new string('0', 64)} first image.dll\n"), "SHA-256 of"); + File.WriteAllBytes(Path.Combine(root, "second.dll"), [9]); + Reject(() => Read(source), "SHA-256 of"); + Require(inputs.Images.All(image => image.Bytes.SequenceEqual(original)), "Retained image snapshot"); + File.WriteAllText(file, "changed"); + Require(inputs.Digest.ToString() == Hash(Encoding.UTF8.GetBytes(source)), "Retained digest file hash"); + File.WriteAllBytes(file, [0xc3, 0x28]); + Reject(() => DeepCheckInputs.Read(file), null); + Reject(() => Read($"{digest} missing.dll\n"), "Could not find file"); + foreach (var malformed in new[] { "", "\n", "# comment\n", digest + " first image.dll\n", + digest + " xfirst image.dll\n", digest + " \n", new string('g', 64) + " first image.dll\n", + "\\" + digest + " invalid\\q\n", "\\" + digest + " incomplete\\\n" }) + Reject(() => Read(malformed), null); + + var nested = Path.Combine(root, "folder"); + Directory.CreateDirectory(nested); + var escapedName = OperatingSystem.IsWindows() ? "folder\\escaped.dll" : "escaped\\name.dll"; + File.WriteAllBytes(Path.Combine(root, escapedName), original); + var escaped = "\\" + digest + " *" + escapedName.Replace("\\", "\\\\") + "\n"; + Require(Read(escaped).Images.Single().Bytes.SequenceEqual(original), "Escaped backslash filename"); + if (!OperatingSystem.IsWindows()) + { + foreach (var character in new[] { '\n', '\r' }) + { + var name = "escaped" + character + "name.dll"; + File.WriteAllBytes(Path.Combine(root, name), original); + var encoded = name.Replace("\n", "\\n").Replace("\r", "\\r"); + Require(Read("\\" + digest + " " + encoded).Images.Count == 1, "Escaped control filename"); + } + } + + var consumer = Image("Consumer", reference: "Facade"); + var facade = Image("Facade", forward: "Middle", other: "Unrelated"); + var middle = Image("Middle", forward: "Destination"); + var destination = Image("Destination"); + var unrelated = Image("Unrelated"); + var request = Identity(Hash(File.ReadAllBytes(consumer)), [], AssemblyName.GetAssemblyName(consumer).FullName); + string[] Selected(params string[] paths) => + Read(string.Concat(paths.Select(path => Hash(File.ReadAllBytes(path)) + " " + path + "\n"))) + .MatchPins(request).Select(pin => Path.GetFileNameWithoutExtension(pin.Image.Path)).ToArray(); + + Require(Selected(unrelated, destination, consumer, middle, facade) + .SequenceEqual(["Consumer", "Facade", "Middle", "Destination"]), "Matching type across two forwards"); + Require(Selected(consumer, facade, destination, unrelated).SequenceEqual(["Consumer", "Facade"]), + "A destination without the intervening forwarding image cannot be selected"); + Image("Middle", forward: "Facade"); + Require(Selected(consumer, facade, middle, destination).SequenceEqual(["Consumer", "Facade", "Middle"]), + "A forwarding cycle terminates without selecting an unrelated destination"); + + var shortChain = MeasureForwarding(8); + var longChain = MeasureForwarding(64); + Console.WriteLine($"Forwarding selection: 8={shortChain:F3} ms, 64={longChain:F3} ms"); + Require(longChain < shortChain * 24, "Forwarding selection must scale below quadratic growth"); + + var small = Measure(512); + var large = Measure(4096); + Console.WriteLine($"Pin matching: 512={small.Match:F3} ms, 4096={large.Match:F3} ms"); + Console.WriteLine($"Reading and hashing: 512={small.Read:F3} ms, 4096={large.Read:F3} ms"); + Require(large.Match < small.Match * 24, "Pin matching must scale below quadratic growth"); + Require(large.Read < small.Read * 24, "Reading and hashing must scale below quadratic growth"); + Console.WriteLine("PASS digest parsing, all-file checking, pin matching, snapshots and scaling"); + + DeepCheckInputs Read(string text) + { + File.WriteAllText(file, text, new UTF8Encoding(false)); + return DeepCheckInputs.Read(file); + } + + string Image(string name, string? reference = null, string? forward = null, string? other = null) + { + var rows = new MetadataBuilder(); + var version = new Version(1, 0, 0, 0); + var fileName = name + ".dll"; + rows.AddModule(0, rows.GetOrAddString(fileName), rows.GetOrAddGuid(Guid.Empty), default, default); + rows.AddAssembly(rows.GetOrAddString(name), version, default, default, 0, AssemblyHashAlgorithm.Sha1); + rows.AddTypeDefinition(0, default, rows.GetOrAddString(""), default, + MetadataTokens.FieldDefinitionHandle(1), MetadataTokens.MethodDefinitionHandle(1)); + var ns = rows.GetOrAddString("Example"); + var item = rows.GetOrAddString("Item"); + AssemblyReferenceHandle AssemblyRef(string target) => + rows.AddAssemblyReference(rows.GetOrAddString(target), version, default, default, 0, default); + if (reference is not null) rows.AddTypeReference(AssemblyRef(reference), ns, item); + const TypeAttributes forwarder = (TypeAttributes)0x00200000; + if (forward is not null) rows.AddExportedType(forwarder, ns, item, AssemblyRef(forward), 0); + if (other is not null) + rows.AddExportedType(forwarder, rows.GetOrAddString("Other"), item, AssemblyRef(other), 0); + var pe = new ManagedPEBuilder(PEHeaderBuilder.CreateLibraryHeader(), new MetadataRootBuilder(rows), + new BlobBuilder()); + var bytes = new BlobBuilder(); + pe.Serialize(bytes); + var path = Path.Combine(root, fileName); + File.WriteAllBytes(path, bytes.ToArray()); + return path; + } + + double MeasureForwarding(int links) + { + var first = Image("ChainConsumer", reference: "Link0"); + var paths = new List { first }; + for (var index = 0; index < links; index++) + paths.Add(Image("Link" + index, forward: "Link" + (index + 1))); + paths.Add(Image("Link" + links)); + var manifest = Read(string.Concat(paths.Select(path => Hash(File.ReadAllBytes(path)) + " " + path + "\n"))); + var identity = Identity(Hash(File.ReadAllBytes(first)), [], AssemblyName.GetAssemblyName(first).FullName); + Require(manifest.MatchPins(identity).Select(pin => pin.Image.Path).SequenceEqual(paths), + "Every hop resolves the same qualified type through its assembly reference"); + List samples = []; + for (var sample = 0; sample < 7; sample++) + { + var timer = Stopwatch.StartNew(); + for (var repeat = 0; repeat < 12; repeat++) manifest.MatchPins(identity); + samples.Add(timer.Elapsed.TotalMilliseconds); + } + samples.Sort(); + return samples[samples.Count / 2]; + } + + (double Read, double Match) Measure(int count) + { + StringBuilder records = new(); + List hashes = []; + for (var index = 0; index < count; index++) + { + var bytes = BitConverter.GetBytes(index); + var hash = Hash(bytes); + var name = $"image{index}.dll"; + File.WriteAllBytes(Path.Combine(root, name), bytes); + hashes.Add(hash); + records.Append(hash).Append(" ").Append(name).Append('\n'); + } + var request = Read(records.ToString()); + List reads = []; + for (var sample = 0; sample < 5; sample++) + { + var timer = Stopwatch.StartNew(); + Require(DeepCheckInputs.Read(file).Images.Count == count, "All stated images retained"); + reads.Add(timer.Elapsed.TotalMilliseconds); + } + reads.Sort(); + var identity = Identity(hashes[0], hashes.Skip(1).ToArray()); + Require(request.MatchPins(identity).Select(pin => pin.Image.Digest.ToString()).SequenceEqual(hashes), + "Distinct digests with the same display name match distinct images"); + for (var warm = 0; warm < 10; warm++) request.MatchPins(identity); + List measurements = []; + for (var sample = 0; sample < 7; sample++) + { + var timer = Stopwatch.StartNew(); + for (var repeat = 0; repeat < 30; repeat++) + Require(request.MatchPins(identity).Count == count, "All pins retained"); + measurements.Add(timer.Elapsed.TotalMilliseconds); + } + measurements.Sort(); + return (reads[reads.Count / 2], measurements[measurements.Count / 2]); + } +} +finally +{ + Directory.Delete(root, recursive: true); +} + +static string Hash(byte[] bytes) => Convert.ToHexStringLower(SHA256.HashData(bytes)); + +static JsonElement Identity(string digest, string[] references, string name = "Same, Version=1.0.0.0") => + JsonSerializer.SerializeToElement(new + { + assemblyIdentity = name, + assemblySha256 = digest, + references = references.Select(hash => new { assemblyIdentity = name, sha256 = hash }).ToArray() + }); + +static void Require(bool condition, string message) +{ + if (!condition) throw new InvalidOperationException(message); +} + +static void Reject(Action action, string? message) +{ + try { action(); } + catch (Exception error) when (error is ArgumentException or IOException) + { + if (message is null || error.Message.Contains(message, StringComparison.Ordinal)) return; + throw new InvalidOperationException($"Expected {message}, got {error.Message}", error); + } + throw new InvalidOperationException("Invalid deep input was accepted"); +} diff --git a/Tests/DeepCli.cs b/Tests/DeepCli.cs new file mode 100644 index 000000000..9b48104b2 --- /dev/null +++ b/Tests/DeepCli.cs @@ -0,0 +1,756 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include MultiAssemblyFixture.cs +#:include DeepBinding.cs + +using System.Reflection; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Text.Json; + +using Program preparation = new(); +preparation.EnsureExtractorBuilt(); +MultiAssemblyFixture fixture = new("DeepCli"); +var cli = args is [var executable] && !executable.StartsWith("--", StringComparison.Ordinal) + ? new[] { Path.GetFullPath(executable) } : fixture.Cli; +if (args is ["--constraints"]) +{ + CheckGenericConstraints(); + return; +} +if (args is ["--layouts"]) +{ + CheckLayoutContent(); + return; +} +if (args is ["--dispatch"]) +{ + CheckDispatchCatalogs(); + return; +} +if (args is ["--enums"]) +{ + CheckEnumStorage(); + return; +} + +const string libName = "DeepCli.Library"; +var lib = fixture.Build("Library", libName, + "namespace MultiFixture; public static class Mix { public static ulong Step(ulong x) => x * 3 + 1; }"); +var app = fixture.Build("Application", "DeepCli.Application", + "namespace MultiFixture; public static class Application { " + + "public static ulong Direct(ulong x) => Mix.Step(x) + 1; }", + references: $"{lib}"); +var output = fixture.Artifact("Direct"); +Run([.. cli, "extract", app, "MultiFixture.Application::Direct(System.UInt64)", output, "--reference", lib]); +var proof = At("Tests/MultiAssembly/Direct.lean"); +string[] verify = [.. cli, "verify", output, "--proof", proof, "--theorem", "MultiAssemblyProof.correctness"]; +var digestFile = Path.Combine(fixture.Root, "inputs.sha256"); +var digests = Sha(app) + " " + Path.GetRelativePath(fixture.Root, app) + "\n" + + Sha(lib) + " " + Path.GetRelativePath(fixture.Root, lib) + "\n"; +File.WriteAllText(digestFile, digests); + +// Each fixture carries the actual platform identity and public key. A forwarded object has an +// ExportedType and an AssemblyRef. A defined object has one physical Field row. +static void WritePlatformImage(string path, AssemblyName name, FieldAttributes field, AssemblyName? forward = null, + AssemblyName? pairForward = null, bool definePair = false, bool reverseSlots = false, + bool definitionKeyFlag = true, bool mathInterface = false) +{ + var rows = new MetadataBuilder(); + rows.AddModule(0, rows.GetOrAddString(name.Name + ".dll"), rows.GetOrAddGuid(Guid.Empty), default, default); + rows.AddAssembly(rows.GetOrAddString(name.Name!), name.Version!, default, + rows.GetOrAddBlob(name.GetPublicKey()!), definitionKeyFlag ? AssemblyFlags.PublicKey : 0, + AssemblyHashAlgorithm.Sha1); + var firstField = MetadataTokens.FieldDefinitionHandle(1); + var firstMethod = MetadataTokens.MethodDefinitionHandle(1); + rows.AddTypeDefinition(default, default, rows.GetOrAddString(""), default, firstField, firstMethod); + var system = rows.GetOrAddString("System"); + var objectName = rows.GetOrAddString("Object"); + if (forward is null) + { + rows.AddTypeDefinition(TypeAttributes.Public, system, objectName, default, firstField, firstMethod); + var signature = new BlobBuilder(); + new BlobEncoder(signature).Field().Type().Int32(); + rows.AddFieldDefinition(field, rows.GetOrAddString("Value"), rows.GetOrAddBlob(signature)); + } + else + { + var target = rows.AddAssemblyReference(rows.GetOrAddString(forward.Name!), forward.Version!, default, + rows.GetOrAddBlob(forward.GetPublicKeyToken()!), default, default); + const TypeAttributes forwarder = (TypeAttributes)0x00200000; + rows.AddExportedType(forwarder, system, objectName, target, 0); + } + if (pairForward is not null) + { + var mathFlags = mathInterface ? TypeAttributes.Interface | TypeAttributes.Abstract : + TypeAttributes.Abstract | TypeAttributes.Sealed; + var mathBase = mathInterface ? default : MetadataTokens.TypeDefinitionHandle(2); + rows.AddTypeDefinition(TypeAttributes.Public | mathFlags, system, rows.GetOrAddString("Math"), mathBase, + MetadataTokens.FieldDefinitionHandle(rows.GetRowCount(TableIndex.Field) + 1), firstMethod); + var target = rows.AddAssemblyReference(rows.GetOrAddString(pairForward.Name!), pairForward.Version!, default, + rows.GetOrAddBlob(pairForward.GetPublicKeyToken()!), default, default); + rows.AddExportedType((TypeAttributes)0x00200000, system, rows.GetOrAddString("ValueTuple`2"), target, 0); + } + if (definePair) + { + var start = MetadataTokens.FieldDefinitionHandle(rows.GetRowCount(TableIndex.Field) + 1); + var valueType = rows.AddTypeDefinition(TypeAttributes.Public, system, rows.GetOrAddString("ValueType"), + MetadataTokens.TypeDefinitionHandle(2), start, firstMethod); + var pair = rows.AddTypeDefinition(TypeAttributes.Public | TypeAttributes.Sealed, system, + rows.GetOrAddString("ValueTuple`2"), valueType, start, firstMethod); + for (var slot = 0; slot < 2; slot++) + { + var signature = new BlobBuilder(); + new BlobEncoder(signature).Field().Type().GenericTypeParameter(reverseSlots ? 1 - slot : slot); + rows.AddFieldDefinition(FieldAttributes.Public, rows.GetOrAddString("Item" + (slot + 1)), + rows.GetOrAddBlob(signature)); + rows.AddGenericParameter(pair, default, rows.GetOrAddString("T" + (slot + 1)), slot); + } + } + var builder = new ManagedPEBuilder(PEHeaderBuilder.CreateLibraryHeader(), + new MetadataRootBuilder(rows), new BlobBuilder()); + var bytes = new BlobBuilder(); + builder.Serialize(bytes); + File.WriteAllBytes(path, bytes.ToArray()); +} + +void CheckPlatformObject() +{ + var runtime = Assembly.Load("System.Runtime").GetName(); + var core = typeof(object).Assembly.GetName(); + var reference = Path.Combine(fixture.Root, "reference-image.dll"); + var definition = Path.Combine(fixture.Root, "definition.dll"); + var previous = Path.Combine(fixture.Root, "previous-version.dll"); + + void Check(string expected, params (string File, AssemblyName Name)[] platforms) + { + var extra = File.Exists(previous) ? Sha(previous) + " " + Path.GetFileName(previous) + "\n" : ""; + File.WriteAllText(digestFile, digests + extra + string.Concat(platforms.Select(p => + Sha(p.File) + " " + Path.GetFileName(p.File) + "\n"))); + var result = Execute([.. verify, "--deep-checks", digestFile, "--format", "json"]); + if (expected == "failed") + { + if (result.Code == 0 || File.Exists(Path.Combine(output, "verified.json"))) + throw new InvalidOperationException("The changed platform definition was certified."); + var failure = JsonSerializer.Deserialize(result.Stdout).GetProperty("binding"); + var rejected = failure.GetProperty("facts").EnumerateArray() + .Single(f => f.GetProperty("id").GetString() == "object-fields"); + if (rejected.GetProperty("status").GetString() != "failed" || + !rejected.GetProperty("reason").GetString()!.Contains("instance field 0x04000001", StringComparison.Ordinal)) + throw new InvalidOperationException("Expected the physical field diagnostic: " + result.Output); + return; + } + if (result.Code != 0) throw new InvalidOperationException(result.Output); + var binding = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(output, "verified.json"))) + .GetProperty("binding"); + var facts = binding.GetProperty("facts").EnumerateArray().ToArray(); + var keyFact = facts.Single(f => f.GetProperty("id").GetString() == "public-key-tokens"); + if (keyFact.GetProperty("status").GetString() != "reconfirmed" || + keyFact.GetProperty("theorem").GetString() != "BindingAudit.publicKeyTokens") + throw new InvalidOperationException("Signed and unsigned AssemblyRefs were not checked: " + keyFact); + var fact = facts.Single(f => f.GetProperty("id").GetString() == "object-fields"); + if (fact.GetProperty("status").GetString() != expected || + fact.GetProperty("theorem").GetString() != (expected == "reconfirmed" ? "BindingAudit.objectFields" : null)) + throw new InvalidOperationException("Incorrect object-fields certificate: " + fact); + if (File.Exists(previous)) + { + var unused = binding.GetProperty("images").EnumerateArray() + .Single(i => i.GetProperty("sha256").GetString() == Sha(previous)); + if (unused.GetProperty("role").GetString() != "unused") + throw new InvalidOperationException("The facade selected the wrong version: " + unused); + } + foreach (var (file, assembly) in platforms) + { + var image = binding.GetProperty("images").EnumerateArray() + .Single(i => i.GetProperty("identity").GetString() == assembly.FullName); + if (image.GetProperty("role").GetString() != "platform" || + image.GetProperty("sha256").GetString() != Sha(file) || + image.GetProperty("status").GetString() != "reconfirmed") + throw new InvalidOperationException("Incorrect platform image: " + image); + } + } + + WritePlatformImage(reference, runtime, FieldAttributes.Static); + Check("reconfirmed", (reference, runtime)); + Console.WriteLine("PASS the exact platform reference permits a static field on System.Object"); + WritePlatformImage(reference, runtime, default, core); + var older = typeof(object).Assembly.GetName(); + older.Version = new Version(core.Version!.Major - 1, 0, 0, 0); + WritePlatformImage(previous, older, FieldAttributes.Public); + Check("not covered", (reference, runtime)); + Console.WriteLine("PASS a facade with only an older destination leaves System.Object fields uncovered"); + WritePlatformImage(definition, core, FieldAttributes.Static); + Check("reconfirmed", (reference, runtime), (definition, core)); + Console.WriteLine("PASS the platform facade resolves System.Object in its exact destination"); + WritePlatformImage(definition, core, FieldAttributes.Public); + Check("failed", (reference, runtime), (definition, core)); + Console.WriteLine("PASS a rehashed instance field in the forwarded definition fails the object-fields check"); + File.WriteAllText(digestFile, digests); +} + +void CheckPlatformPair() +{ + var pairApp = fixture.Build("Pair", "DeepCli.Pair", + "public static class Pair { public static int Quotient(int x, int y) => System.Math.DivRem(x, y).Quotient; }"); + var pairOutput = fixture.Artifact("PairExtraction"); + Run([.. cli, "extract", pairApp, "Pair::Quotient(int,int)", pairOutput]); + var runtime = Assembly.Load("System.Runtime").GetName(); + var core = typeof(object).Assembly.GetName(); + var facade = Path.Combine(fixture.Root, "pair-facade.dll"); + var destination = Path.Combine(fixture.Root, "pair-definition.dll"); + var pairPins = Path.Combine(fixture.Root, "pair.sha256"); + var pairProof = At("Examples/Proofs/MathDivRemQuotient.lean"); + + void Check(string expected, string[] images, bool wrongMathKind = false) + { + File.WriteAllLines(pairPins, images.Prepend(pairApp).Select(path => Sha(path) + " " + path)); + var result = Execute([.. cli, "verify", pairOutput, "--proof", pairProof, "--deep-checks", pairPins, + "--format", "json"]); + var certificate = Path.Combine(pairOutput, "verified.json"); + if (expected == "failed" ? result.Code == 0 || File.Exists(certificate) : result.Code != 0) + throw new InvalidOperationException("Unexpected pair verification outcome: " + result.Output); + var report = expected == "failed" + ? JsonSerializer.Deserialize(result.Stdout) + : JsonSerializer.Deserialize(File.ReadAllText(certificate)); + var facts = report.GetProperty("binding").GetProperty("facts").EnumerateArray().ToArray(); + var kind = facts.Single(f => f.GetProperty("id").GetString() == "platform-type-kinds"); + if (wrongMathKind) + { + if (kind.GetProperty("status").GetString() != "failed") + throw new InvalidOperationException("Expected the physical System.Math kind rejection: " + kind); + return; + } + if (expected != "failed" && (kind.GetProperty("status").GetString() != expected || + kind.GetProperty("theorem").GetString() != (expected == "reconfirmed" ? "BindingAudit.platformKinds" : null))) + throw new InvalidOperationException("Wrong platform kind coverage: " + kind); + var pair = facts.Single(f => f.GetProperty("id").GetString() == "value-tuple-field-order"); + if (pair.GetProperty("status").GetString() != expected || + expected == "reconfirmed" && pair.GetProperty("theorem").GetString() != "BindingAudit.tupleFields") + throw new InvalidOperationException("Incorrect tuple field-order report: " + pair); + var count = facts.Count(fact => fact.GetProperty("status").GetString() == "reconfirmed"); + var publicKeys = facts.Single(f => f.GetProperty("id").GetString() == "public-key-tokens"); + if (expected != "failed" && (publicKeys.GetProperty("status").GetString() != "reconfirmed" || + publicKeys.GetProperty("theorem").GetString() != "BindingAudit.publicKeyTokens")) + throw new InvalidOperationException("Missing publisher token proof for the pair assembly: " + publicKeys); + if (expected == "reconfirmed" && count != 7 || expected == "not covered" && count != 5) + throw new InvalidOperationException($"Unexpected number of reconfirmed facts: {count} ({expected})."); + if (expected == "failed" && + !pair.GetProperty("reason").GetString()!.Contains("Item1, signature 061301", StringComparison.Ordinal)) + throw new InvalidOperationException("Missing physical tuple signature in the rejection: " + pair); + if (expected == "not covered" && report.GetProperty("binding").GetProperty("images").EnumerateArray() + .Single(image => image.GetProperty("sha256").GetString() == Sha(destination)) + .GetProperty("role").GetString() != "unused") + throw new InvalidOperationException("The tuple forwarder selected a different assembly version."); + if (expected != "failed" && facts.Single(f => f.GetProperty("id").GetString() == "object-fields") + .GetProperty("status").GetString() != "reconfirmed") + throw new InvalidOperationException("The supplied facade defines System.Object."); + } + + WritePlatformImage(facade, runtime, FieldAttributes.Static, pairForward: core); + var previousCore = typeof(object).Assembly.GetName(); + previousCore.Version = new Version(core.Version!.Major - 1, 0, 0, 0); + WritePlatformImage(destination, previousCore, FieldAttributes.Static, definePair: true); + Check("not covered", [facade, destination]); + WritePlatformImage(destination, core, FieldAttributes.Static, definePair: true, definitionKeyFlag: false); + Check("reconfirmed", [facade, destination]); + WritePlatformImage(destination, core, FieldAttributes.Static, definePair: true, reverseSlots: true); + Check("failed", [facade, destination]); + WritePlatformImage(destination, core, FieldAttributes.Static, definePair: true); + WritePlatformImage(facade, runtime, FieldAttributes.Static, pairForward: core, mathInterface: true); + Check("failed", [facade, destination], wrongMathKind: true); + Console.WriteLine("PASS a rehashed System.Math interface fails the platform kind check"); + Console.WriteLine("PASS tuple forwarding, missing destination and swapped physical generic field signatures"); +} + +void CheckGenericConstraints() +{ + var assembly = fixture.Build("Constraints", "DeepCli.Constraints", """ + namespace Gates; + public interface IClosed { } + public struct Both : IClosed, IClosed { public int Value; } + public struct Pair : IClosed where S : unmanaged where T : unmanaged + { + public S First; + public T Second; + } + public static class Counter + { + public static int Read(int n) where T : struct, IClosed => n + 1; + public static int ReadWide(int n) where T : struct, IClosed => n + 1; + public static int Entry(int n) => ReadWide>(Read(n)); + } + """); + var extracted = fixture.Artifact("GenericExtraction"); + Run([.. cli, "extract", assembly, "Gates.Counter::Entry(System.Int32)", extracted]); + var pins = Path.Combine(fixture.Root, "constraints.sha256"); + File.WriteAllText(pins, Sha(assembly) + " " + assembly + Environment.NewLine); + var proof = Path.Combine(fixture.Root, "Constraints.lean"); + File.WriteAllText(proof, """ + import Extracted + open CIL + set_option maxRecDepth 16384 in + theorem twice (value : W32) (memory : Safety.Memory) : + Safety.invoke Extracted.program 32 0 [.scalar (.i32 value)] memory = + .ok (memory, [.scalar (.i32 ((value + 1) + 1))]) := rfl + #print axioms twice + """); + Run([.. cli, "verify", extracted, "--proof", proof, "--theorem", "twice", + "--deep-checks", pins, "--format", "json"]); + using var report = JsonDocument.Parse(File.ReadAllText(Path.Combine(extracted, "verified.json"))); + var fact = report.RootElement.GetProperty("binding").GetProperty("facts").EnumerateArray() + .Single(item => item.GetProperty("id").GetString() == "generic-constraints"); + if (fact.GetProperty("status").GetString() != "reconfirmed" || + fact.GetProperty("theorem").GetString() != "BindingAudit.genericConstraints") + throw new InvalidOperationException("Incorrect generic constraint report: " + fact); + var layout = report.RootElement.GetProperty("binding").GetProperty("facts").EnumerateArray() + .Single(item => item.GetProperty("id").GetString() == "layout-content"); + if (layout.GetProperty("status").GetString() != "not covered") + throw new InvalidOperationException("Generic layouts incorrectly reconfirmed: " + layout); +} + +void CheckDispatchCatalogs() +{ + foreach (bool changed in new[] { false, true }) + { + var build = changed ? "FiveFaces" : "SixFaces"; + var assembly = fixture.Build(build, "DeepCli.Dispatch", $$""" + namespace Geometry; + public interface IShapeTag { int Tag(); int OtherTag(); } + public interface IColour { } + public abstract class Shape { public abstract int Sides(); } + public class Triangle : Shape, IShapeTag, IColour { + public override int Sides() => 3; + int IShapeTag.Tag() => 1; + int IShapeTag.OtherTag() => 2; + } + public sealed class Equilateral : Triangle { } + public abstract class Solid { public abstract int Faces(); } + public sealed class Cube : Solid, IColour { public override int Faces() => {{(changed ? 5 : 6)}}; } + public sealed class TagOnly : IShapeTag { public int Tag() => 1; public int OtherTag() => 2; } + public sealed class UnrelatedResource : System.IDisposable { void System.IDisposable.Dispose() { } } + public static class Reader { + public static int Total(Shape shape, Solid solid) => shape.Sides() + solid.Faces() + shape.Sides(); + } + """); + var pins = Path.Combine(fixture.Root, build + ".sha256"); + File.WriteAllText(pins, Sha(assembly) + " " + assembly + Environment.NewLine); + var output = fixture.Artifact(build + "Total"); + const string selector = "Geometry.Reader::Total(Geometry.Shape,Geometry.Solid)"; + Run([.. cli, "extract", assembly, selector, output]); + var proof = Path.Combine(output, "Proof.lean"); + var template = File.ReadAllText(At("Tests/StrictBinding/DispatchTotal.lean.in")); + var claim = changed ? "Refuted" : "Holds"; + var evidence = changed ? "refuted rfl" : "holds (by intro shape; cases shape <;> rfl)"; + File.WriteAllText(proof, template + + $"\ntheorem correctness : contract.{claim} Extracted.program 0 := {evidence}\n" + + "#print axioms correctness\nend DispatchGate\n"); + string[] binding = changed ? [] : ["--deep-checks", pins]; + Run([.. cli, "verify", output, "--proof", proof, "--theorem", "DispatchGate.correctness", .. binding]); + using var report = JsonDocument.Parse(File.ReadAllText(Path.Combine(output, "verified.json"))); + if (changed) + { + var method = report.RootElement.GetProperty("refutation").GetProperty("method"); + if (method.GetProperty("assemblySha256").GetString() != Sha(assembly) || + !method.GetProperty("signature").GetString()!.Contains(selector, StringComparison.Ordinal)) + throw new InvalidOperationException("Wrong dispatch counterexample: " + method); + continue; + } + var fact = report.RootElement.GetProperty("binding").GetProperty("facts").EnumerateArray() + .Single(item => item.GetProperty("id").GetString() == "dispatch-catalogs"); + if (fact.GetProperty("status").GetString() != "reconfirmed" || + fact.GetProperty("theorem").GetString() != "BindingAudit.dispatchCatalogs") + throw new InvalidOperationException("Incorrect dispatch binding report: " + fact); + ReplayImageCases(output, assembly, "DispatchImage", At("Tests/StrictBinding/DispatchOverrideCases.lean"), + ["StrictBinding.DispatchCatalogs", "Tests.StrictBinding.Mutation"]); + Console.WriteLine("PASS physical MethodImpl rows reject missing slots, crossed owners and crossed declarations"); + } +} + +void CheckLayoutContent() +{ + foreach (bool changed in new[] { false, true }) + { + var build = changed ? "RelocatedFields" : "OriginalFields"; + var assembly = fixture.Build(build, "DeepCli.Layouts", $$""" + using System.Runtime.InteropServices; + namespace Gates; + [StructLayout(LayoutKind.Explicit, Pack = 2, Size = 24)] + public struct Overlay { + [FieldOffset(0)] public uint Tag; + [FieldOffset({{(changed ? 4 : 8)}})] public int Value; + [FieldOffset(16)] public long Stamp; + public static int Outside; + } + [StructLayout(LayoutKind.Sequential, Pack = {{(changed ? 8 : 1)}})] + public struct Packed { public short Flag; public uint Count; } + public static class Reader { + public static int Read(ref Overlay row) => row.Value; + public static uint ReadPacked(ref Packed row) => row.Count; + } + """); + var pins = Path.Combine(fixture.Root, build + ".sha256"); + File.WriteAllText(pins, Sha(assembly) + " " + assembly + Environment.NewLine); + foreach (var (method, type, offset) in new[] { ("Read", "Overlay", "8"), ("ReadPacked", "Packed", "2") }) + { + var output = fixture.Artifact(build + method); + Run([.. cli, "extract", assembly, $"Gates.Reader::{method}(Gates.{type}&)", output]); + var proof = Path.Combine(output, "Proof.lean"); + var (claim, lemma, evidence) = changed + ? ("Refuted", "refuted", "readAtOffset (Extracted.fieldRef0_shape rfl) witness") + : ("Holds", "holds", "readAtOffset (Extracted.fieldRef0_shape rfl)"); + var template = File.ReadAllText(At("Tests/StrictBinding/LayoutRead.lean.in")); + File.WriteAllText(proof, template.Replace("FIELD_OFFSET", offset, StringComparison.Ordinal) + .Replace("READ_OFFSET", changed ? "4" : offset, StringComparison.Ordinal) + + $"\ntheorem correctness : contract.{claim} Extracted.program 0 := {lemma} ({evidence})\n" + + "#print axioms correctness\nend LayoutGate\n"); + string[] binding = changed ? [] : ["--deep-checks", pins]; + Run([.. cli, "verify", output, "--proof", proof, "--theorem", "LayoutGate.correctness", .. binding]); + using var report = JsonDocument.Parse(File.ReadAllText(Path.Combine(output, "verified.json"))); + if (changed) + { + var counter = report.RootElement.GetProperty("refutation").GetProperty("method"); + var signature = counter.GetProperty("signature").GetString()!; + if (counter.GetProperty("assemblySha256").GetString() != Sha(assembly) || + !signature.Contains("::" + method + "(", StringComparison.Ordinal)) + throw new InvalidOperationException("Wrong layout counterexample: " + counter); + continue; + } + var fact = report.RootElement.GetProperty("binding").GetProperty("facts").EnumerateArray() + .Single(item => item.GetProperty("id").GetString() == "layout-content"); + if (fact.GetProperty("status").GetString() != "reconfirmed" || + fact.GetProperty("theorem").GetString() != "BindingAudit.layoutContent") + throw new InvalidOperationException("Incorrect layout binding report: " + fact); + } + } +} + +void CheckEnumStorage() +{ + var assembly = fixture.Build("Enums", "DeepCli.Enums", """ + namespace EnumFixture; + public enum Flags : ulong { One = 1 } + public static class Operations + { + public static Flags Set(Flags input) => input | Flags.One; + } + """); + var extracted = fixture.Artifact("EnumExtraction"); + Run([.. cli, "extract", assembly, "EnumFixture.Operations::Set(EnumFixture.Flags)", extracted]); + var pins = Path.Combine(fixture.Root, "enum.sha256"); + File.WriteAllText(pins, Sha(assembly) + " " + assembly + Environment.NewLine); + var verified = Execute([.. cli, "verify", extracted, "--proof", At("Examples/Proofs/EnumFlags.lean"), + "--theorem", "ExampleProof.correctness", "--deep-checks", pins, "--format", "json"]); + if (verified.Code != 0) throw new InvalidOperationException(verified.Output); + using var report = JsonDocument.Parse(File.ReadAllText(Path.Combine(extracted, "verified.json"))); + var fact = report.RootElement.GetProperty("binding").GetProperty("facts").EnumerateArray() + .Single(item => item.GetProperty("id").GetString() == "enum-underlying-types"); + if (fact.GetProperty("status").GetString() != "reconfirmed" || + fact.GetProperty("theorem").GetString() != "BindingAudit.enumStorage") + throw new InvalidOperationException("Incorrect enum storage report: " + fact); +} + +void CheckPublisherKey() +{ + var identity = JsonSerializer.SerializeToElement(new + { + assemblySha256 = Sha(app), + assemblyIdentity = AssemblyName.GetAssemblyName(app).FullName + }); + var inputs = DeepCheckInputs.Read(digestFile); + var planned = inputs.MatchPins(identity); + if (planned.Count != 2 || planned[0].KeyCoverage != false || !planned[1].Reference || + planned[1].Platform || planned[1].Identity != AssemblyName.GetAssemblyName(lib).FullName) + throw new InvalidOperationException("The digest-listed library did not match the unpinned AssemblyRef."); + + var unusedReference = fixture.Build("ReferenceProbe", libName, + "namespace MultiFixture; public static class Mix { public static ulong Step(ulong x) => x * 3 + 1; " + + "public static void Print() => System.Console.WriteLine(); }"); + var bytes = File.ReadAllBytes(unusedReference); + using (var pe = new PEReader(new MemoryStream(bytes, writable: false))) + { + var metadata = pe.GetMetadataReader(); + var handle = metadata.AssemblyReferences.Single(h => + metadata.StringComparer.Equals(metadata.GetAssemblyReference(h).Name, "System.Console")); + var signature = metadata.GetMethodDefinition(metadata.MethodDefinitions.First()).Signature; + var width = metadata.GetBlobReader(signature).Length; + if (width is 0 or 8 || metadata.GetHeapSize(HeapIndex.Blob) >= 65536) + throw new InvalidOperationException("Expected a short non-token signature and two-byte blob indices."); + // AssemblyRef.PublicKeyOrToken follows the four version components and the flags. + var row = metadata.GetTableMetadataOffset(TableIndex.AssemblyRef) + + (MetadataTokens.GetRowNumber(handle) - 1) * metadata.GetTableRowSize(TableIndex.AssemblyRef); + System.Buffers.Binary.BinaryPrimitives.WriteUInt16LittleEndian( + bytes.AsSpan(pe.PEHeaders.MetadataStartOffset + row + 12), + checked((ushort)MetadataTokens.GetHeapOffset(signature))); + } + var changed = Path.Combine(fixture.Root, "short-token.dll"); + File.WriteAllBytes(changed, bytes); + var extraction = fixture.Artifact("ShortToken"); + Run([.. cli, "extract", app, "MultiFixture.Application::Direct(System.UInt64)", extraction, + "--reference", changed]); + var digestPath = Path.Combine(fixture.Root, "short-token.sha256"); + File.WriteAllText(digestPath, $"{Sha(app)} {Path.GetRelativePath(fixture.Root, app)}\n" + + $"{Sha(changed)} {Path.GetFileName(changed)}\n"); + var result = Execute([.. cli, "verify", extraction, "--proof", proof, "--theorem", + "MultiAssemblyProof.correctness", "--deep-checks", digestPath, "--format", "json"]); + if (result.Code == 0 || File.Exists(Path.Combine(extraction, "verified.json"))) + throw new InvalidOperationException("A malformed token in a supplied assembly was certified."); + var failure = JsonSerializer.Deserialize(result.Stdout).GetProperty("binding"); + var rejected = failure.GetProperty("facts").EnumerateArray() + .Single(fact => fact.GetProperty("id").GetString() == "public-key-tokens"); + if (rejected.GetProperty("status").GetString() != "failed" || + !rejected.GetProperty("reason").GetString()!.Contains("AssemblyRef System.Console", StringComparison.Ordinal)) + throw new InvalidOperationException("Missing publisher-key rejection: " + result.Output); + Console.WriteLine("PASS an invalid AssemblyRef token outside the method slice fails with partial reference inputs"); +} + +if (args is ["--keys"]) +{ + CheckPublisherKey(); + return; +} + +if (args is ["--pair"]) +{ + CheckPlatformPair(); + return; +} + +if (args is ["--platform"]) +{ + CheckPlatformObject(); + return; +} + +void Refused(string name, string[] command, string? expectedStage = null) +{ + var result = Execute(command); + if (result.Code == 0) throw new InvalidOperationException(name + " was accepted"); + if (expectedStage is not null) + { + var report = JsonSerializer.Deserialize(result.Stdout); + if (report.GetProperty("stage").GetString() != expectedStage || + report.GetProperty("binding").ValueKind != JsonValueKind.Null) + throw new InvalidOperationException(name + " has the wrong failure boundary: " + result.Output); + } + Console.WriteLine("PASS " + name); +} + +// The deep checks run only as an option of verify, so a command name that is not one of the listed commands +// is a usage error that runs nothing, whatever it is called. +void RefusedAsUnknownCommand(string[] cli, string output, string digestFile) +{ + foreach (var name in new[] { "audit" + "-binding", "audit", "binding-audit", "strict-binding", "deep-checks", + "deep", "verify-deep", "audit-deep" }) + { + var result = Execute([.. cli, name, output, "--deep-checks", digestFile]); + if (result.Code == 0 || !result.Output.Contains("Stage: arguments", StringComparison.Ordinal) || + !result.Output.Contains("Usage: leancil", StringComparison.Ordinal) || + result.Output.Contains("Binding:", StringComparison.Ordinal) || + result.Output.Contains("BindingAudit", StringComparison.Ordinal) || + result.Output.Contains("StrictBinding", StringComparison.Ordinal)) + throw new InvalidOperationException(name + " was not refused as an unknown command: " + result.Output); + } + Console.WriteLine("PASS unknown audit command names"); +} + +// The help text lists the deep checks only as an option of verify. The one audit command, audit-library, checks +// the theorems of a Lean library and runs no binding check. +void RequireNoSeparateAudit(string[] cli) +{ + var help = Execute([.. cli, "--help"]); + // A command's usage wraps onto indented lines that do not start with `leancil`; each belongs to the command above. + var commands = help.Stdout.ReplaceLineEndings("\n").Split('\n') + .Aggregate(new List(), (joined, raw) => + { + var line = raw.Trim().StartsWith("Usage: ", StringComparison.Ordinal) ? raw.Trim()["Usage: ".Length..] + : raw.Trim(); + if (line.StartsWith("leancil ", StringComparison.Ordinal)) joined.Add(line); + else if (line.Length > 0 && raw.StartsWith(' ') && joined.Count > 0) joined[^1] = $"{joined[^1]} {line}"; + return joined; + }).ToArray(); + var names = commands.Select(line => line.Split(' ')[1]) + .Where(name => !name.StartsWith("--", StringComparison.Ordinal)).ToArray(); + var audits = names.Where(name => name.Contains("audit", StringComparison.OrdinalIgnoreCase)).ToArray(); + var deepLines = commands.Where(line => line.Contains("--deep-checks", StringComparison.Ordinal)).ToArray(); + if (help.Code != 0 || !names.Contains("verify") || !audits.SequenceEqual(["audit-library"]) || + deepLines.Length != 1 || !deepLines[0].StartsWith("leancil verify ", StringComparison.Ordinal) || + help.Stdout.Contains("binding audit", StringComparison.OrdinalIgnoreCase)) + throw new InvalidOperationException("The help text names a separate audit entry: " + help.Output); + Console.WriteLine("PASS help names no separate audit command"); +} + +if (args is not (["--consumer"] or ["--mutations"] or ["--controls"] or ["--final-mutations"])) +{ + Refused("missing digest option value", [.. verify, "--format", "json", "--deep-checks"], "arguments"); + Refused("duplicate digest option", [.. verify, "--format", "json", "--deep-checks", digestFile, + "--deep-checks", digestFile], "arguments"); + Refused("missing digest file", [.. verify, "--deep-checks", digestFile + ".missing", "--format", "json"], + "arguments"); + foreach (var content in new[] { "malformed\n", new string('0', 64) + " " + app + "\n", + Sha(app) + " " + app + "\n" }) + { + File.WriteAllText(digestFile, content); + Refused("invalid or missing image digest", [.. verify, "--deep-checks", digestFile, "--format", "json"], + "arguments"); + } + File.WriteAllBytes(digestFile, [0xff]); + Refused("invalid UTF-8 digest file", [.. verify, "--deep-checks", digestFile, "--format", "json"], "arguments"); + File.WriteAllText(digestFile, digests); + foreach (var command in new[] { "verify-batch", "gates", "watch", "workspace", "check", "audit-library" }) + Refused(command + " rejects deep option", [.. cli, command, output, "--deep-checks", digestFile]); + RefusedAsUnknownCommand(cli, output, digestFile); + RequireNoSeparateAudit(cli); + Refused("invalid proof root", [.. verify, "--proof-root", fixture.Root + ".missing", "--deep-checks", + digestFile, "--format", "json"], "extraction integrity"); + File.Delete(Path.Combine(output, "verification.json")); + if (args is ["--arguments"]) return; + + var ordinary = Execute(verify); + if (ordinary.Code != 0 || !ordinary.Stdout.Contains("Binding: default", StringComparison.Ordinal) || + ordinary.Stdout.Split(": trusted", StringSplitOptions.None).Length != 13 || + ordinary.Output.Contains("BindingAudit", StringComparison.Ordinal)) + throw new InvalidOperationException("Default binding output or work changed: " + ordinary.Output); + var deep = Execute([.. verify, "--deep-checks", digestFile]); + string[] reconfirmed = ["body-bytes", "metadata-rows", "static-data"]; + if (deep.Code != 0 || !deep.Stdout.Contains("StrictBinding sources:", StringComparison.Ordinal) || + deep.Stdout.Split(": not covered", StringSplitOptions.None).Length != 10) + throw new InvalidOperationException("Deep binding text is incomplete: " + deep.Output); + string[] factNames = + [ + "body-bytes", "metadata-rows", "platform-type-kinds", "enum-underlying-types", "value-tuple-field-order", + "object-fields", "layout-content", "dispatch-catalogs", "static-data", "generic-constraints", + "public-key-tokens", "import-identities" + ]; + foreach (var fact in factNames) + if (!ordinary.Stdout.Contains(fact + ": trusted", StringComparison.Ordinal) || + !deep.Stdout.Contains(fact + (reconfirmed.Contains(fact) ? ": reconfirmed" : ": not covered"), + StringComparison.Ordinal)) + throw new InvalidOperationException("Binding text omitted wire name " + fact); + Console.WriteLine("PASS default and deep binding text"); + CheckPlatformObject(); + CheckPlatformPair(); + CheckPublisherKey(); + CheckEnumStorage(); + CheckGenericConstraints(); + CheckLayoutContent(); + CheckDispatchCatalogs(); + + var tables = fixture.Build("Table", "DeepCli.Table", """ + public static class Table { + static System.ReadOnlySpan Words => [11, 13, 17, 19, 23, 29, 31, 37]; + public static uint Third() => Words[2]; + } + """); + var tableOutput = fixture.Artifact("Third"); + Run([.. cli, "extract", tables, "Table::Third()", tableOutput]); + var tableDigest = Path.Combine(fixture.Root, "table.sha256"); + File.WriteAllText(tableDigest, Sha(tables) + " " + Path.GetRelativePath(fixture.Root, tables) + "\n"); + var tableProof = Path.Combine(fixture.Root, "Third.lean"); + File.WriteAllText(tableProof, """ + import Extracted + import CIL.Safety.Execution + + open CIL CIL.Safety + namespace ExampleProof + + example : Extracted.methodEvidence.any (fun item => item.closed.staticFields.any + (fun site => site.2.rvaSize == some 32)) = true := by decide +kernel + + theorem correctness : (invoke Extracted.program 64 0 [] Memory.empty).map Prod.snd = + .ok [.scalar (.i32 17)] := by rfl + #print axioms correctness + end ExampleProof + + """); + Run([.. cli, "verify", tableOutput, "--proof", tableProof, "--deep-checks", tableDigest]); + var binding = JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(tableOutput, "verified.json"))).GetProperty("binding"); + var data = binding.GetProperty("facts").EnumerateArray().Single(fact => + fact.GetProperty("id").GetString() == "static-data"); + if (data.GetProperty("status").GetString() != "reconfirmed" || + data.GetProperty("theorem").GetString() != "BindingAudit.staticData") + throw new InvalidOperationException("The compiled table has no audited physical-data theorem."); + Console.WriteLine("PASS the third word comes from a 32-byte RVA field in the pinned table assembly"); + + ReplayImageCases(tableOutput, tables, "RvaImage", At("Tests/StrictBinding/StaticSiteCases.lean"), + ["StrictBinding.StaticData"]); + Console.WriteLine("PASS the pinned FieldRVA rejects a matching prefix, changed bytes and a missing RVA marker"); +} + +void ReplayImageCases(string directory, string assembly, string imageModule, string casesFile, string[] dependencies) +{ + Run(["lake", "build", .. dependencies], preparation.CheckoutPackage()); + var leanPath = directory + Path.PathSeparator + preparation.CheckoutLeanPath(); + File.WriteAllText(Path.Combine(directory, imageModule + ".lean"), + $"import StrictBinding.Image\nimport CIL.Extraction.FixedDigits\nnamespace {imageModule}\n" + + BindingImage.Definitions(File.ReadAllBytes(assembly)) + $"\nend {imageModule}\n"); + var cases = Path.GetFileNameWithoutExtension(casesFile); + File.Copy(casesFile, Path.Combine(directory, cases + ".lean"), true); + string[] modules = ["Extracted", imageModule, cases]; + foreach (var module in modules) + { + var log = Run([preparation.LeanExecutable, "--root=" + directory, "-o", + Path.Combine(directory, module + ".olean"), Path.Combine(directory, module + ".lean")], + leanPath: leanPath); + if (log.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidDataException(module + " depends on sorryAx"); + } + preparation.ReplayCheckedModules(modules, leanPath, "Kernel replay of " + cases); +} +if (args is ["--text"]) return; +if (args is ["--mutations"]) + CheckDeepBinding(fixture.Root, cli, output, app, lib, libName, verify); +if (args is ["--controls"]) + CheckDeepBinding(fixture.Root, cli, output, app, lib, libName, verify, controlsOnly: true); +if (args is ["--final-mutations"]) + CheckDeepBinding(fixture.Root, cli, output, app, lib, libName, verify, finalOnly: true); +if (args is not (["--mutations"] or ["--controls"] or ["--final-mutations"])) +{ + var package = Path.Combine(fixture.Root, "Consumer"); + Directory.CreateDirectory(package); + var revision = Run(["git", "rev-parse", "HEAD"], Root).Trim(); + File.WriteAllText(Path.Combine(package, "lakefile.toml"), $""" + name = "deep_cli_consumer" + [[require]] + name = "leancil" + git = {JsonSerializer.Serialize(Root.Replace('\\', '/'))} + rev = "{revision}" + """); + File.Copy(At("lean-toolchain"), Path.Combine(package, "lean-toolchain")); + var consumerProof = Path.Combine(package, "Proof.lean"); + File.Copy(proof, consumerProof); + Run(["lake", "update"], package); + string[] consumer = [.. cli, "verify", output, "--proof", consumerProof, "--theorem", + "MultiAssemblyProof.correctness", "--proof-root", package, "--deep-checks", digestFile, "--format", "json"]; + var checkedConsumer = Execute(consumer); + if (checkedConsumer.Code != 0) throw new InvalidOperationException(checkedConsumer.Output); + var consumerReport = JsonSerializer.Deserialize(checkedConsumer.Stdout); + var binding = consumerReport.GetProperty("binding"); + if (binding.GetProperty("mode").GetString() != "deep" || + binding.GetProperty("facts").GetArrayLength() != 12 || + binding.GetProperty("facts")[0].GetProperty("status").GetString() != "reconfirmed") + throw new InvalidOperationException("Consumer CLI did not retain deep binding"); + Console.WriteLine("PASS proof-root CLI deep verification"); + File.WriteAllText(consumerProof, + "import Extracted\nnamespace MultiAssemblyProof\ntheorem correctness : False := by exact True.intro\n" + + "end MultiAssemblyProof\n"); + var rejected = Execute(consumer); + if (rejected.Code == 0 || File.Exists(Path.Combine(output, "verified.json"))) + throw new InvalidOperationException("Rejected consumer proof published a certificate"); + var failed = JsonSerializer.Deserialize(rejected.Stdout); + var failedFile = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(output, "failed.json"))); + var failureBinding = failed.GetProperty("binding"); + if (!JsonElement.DeepEquals(failureBinding, failedFile.GetProperty("binding")) || + failureBinding.GetProperty("facts").GetArrayLength() != 12 || + failureBinding.GetProperty("facts").EnumerateArray().Any(fact => + fact.GetProperty("status").GetString() != "not covered")) + throw new InvalidOperationException("Failed consumer binding payload is inconsistent: " + rejected.Output); + Console.WriteLine("PASS failed.json and JSON retain the same incomplete deep attempt"); +} diff --git a/Tests/DeepDiagnostics.cs b/Tests/DeepDiagnostics.cs new file mode 100644 index 000000000..4f47515a1 --- /dev/null +++ b/Tests/DeepDiagnostics.cs @@ -0,0 +1,169 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/VerifyReports.cs + +using System.Diagnostics; +using System.Text.Json; + +var root = At("artifacts/DeepDiagnostics-" + Guid.NewGuid().ToString("N")); +Directory.CreateDirectory(root); +var image = Path.Combine(root, "image.bin"); +File.WriteAllBytes(image, [1, 2, 3]); +var manifest = Path.Combine(root, "images.sha256"); +File.WriteAllText(manifest, Sha(image) + " image.bin\n"); +var inputs = DeepCheckInputs.Read(manifest); +var identity = new ExtractionIdentity(root, new string('a', 64), JsonSerializer.SerializeToElement(new +{ + assemblyIdentity = "First", assemblySha256 = Sha(image), + references = new[] { new { assemblyIdentity = "Second", sha256 = Sha(image) } } +}), new(StringComparer.Ordinal)); +var holder = new DeepCheckRun(inputs); +Require(holder.Attempt is null, "An unbegun request has no binding attempt"); +Expect(() => DeepCheckRun.Claim(null, holder), "requires inputs"); +Expect(() => DeepCheckRun.Claim(DeepCheckInputs.Read(manifest), holder), "inputs do not match"); +DeepCheckRun.Claim(inputs, holder); +holder.Begin(identity); +Expect(() => DeepCheckRun.Claim(inputs, holder), "already used"); +Expect(() => holder.Begin(identity), "one claimed request"); +var attempt = holder.Attempt!; +Expect(() => attempt.RequireIdentity(identity with { Sha256 = new string('b', 64) }), "identity changed"); +Expect(() => attempt.RejectImage(Sha256Digest.Parse(new string('b', 64), "a test digest"), "unknown"), + "Unknown deep-check image"); +Expect(() => attempt.RejectFact((BindingFact)100, "unknown"), "Unknown binding fact"); +Expect(() => attempt.RejectFact(BindingFact.BodyBytes, " "), "reason"); +var incomplete = attempt.Failure("kernel replay", "resource limit"); +Require(incomplete.images.Length == 2 && incomplete.images.All(result => + result.status == FailureReports.BindingFailureStatus.NotCovered), "Duplicate digest identities are retained"); +Require(incomplete.facts.Select(fact => fact.id).SequenceEqual(Enum.GetValues().Select(fact => + JsonSerializer.SerializeToElement(fact).GetString())), "Every fact uses its existing wire name in order"); +attempt.RejectImage(Sha256Digest.Parse(Sha(image), "the test image"), "digest theorem rejected"); +var rejected = attempt.Failure("theorem audit", "rejected"); +Require(rejected.images.All(result => result.status == FailureReports.BindingFailureStatus.Failed) && + rejected.facts.All(result => result.status == FailureReports.BindingFailureStatus.NotCovered), + "A rejected digest affects its identities without a transitive fact rejection"); + +foreach (var status in Enum.GetValues()) +{ + var encoded = JsonSerializer.Serialize(status); + Require(encoded == (status == FailureReports.BindingFailureStatus.Failed ? "\"failed\"" : "\"not covered\"") && + JsonSerializer.Deserialize(encoded) == status, "Exact status spelling"); +} +foreach (var invalid in new[] { "0", "1", "\"reconfirmed\"", "\"trusted\"", "null", "\"Failed\"" }) + Expect(() => JsonSerializer.Deserialize(invalid), "Expected"); +Expect(() => JsonSerializer.Serialize((FailureReports.BindingFailureStatus)100), "Invalid binding failure status"); +Expect(() => JsonSerializer.Deserialize(JsonSerializer.Serialize(rejected)), "required properties"); +var failure = FailureReports.Classify("theorem audit", "rejected", null, "Proof.correct", "Proof.lean", root, null); +Require(!JsonSerializer.SerializeToElement(failure).TryGetProperty("binding", out _), "Absent binding is omitted"); +failure = failure with { binding = rejected }; +var result = VerifyReports.Result("failed", failure, "Proof.correct", "Proof.lean", root, null, + "theorem audit", null, null, []); +Require(result["binding"]!.ToJsonString() == JsonSerializer.Serialize(rejected) && + result["unknown"]?["binding"] is null, "JSON reports retain failed binding"); +Expect(() => VerifyReports.Result("failed", failure, "Proof.correct", "Proof.lean", root, null, + "theorem audit", null, null, [], binding: BindingSection.Default), "successful and failed binding"); +VerifyReports.Result("verified", failure with { binding = null }, "Proof.correct", "Proof.lean", root, null, + null, [], "True", [], binding: BindingSection.Default); +Console.WriteLine("PASS failed binding serialization, request ownership and duplicate-digest identities"); + +BindingRequest[] requests = +[ + new("BindingAudit.Bodies", new("BindingAudit.bodies"), new BindingSubject.Fact(BindingFact.BodyBytes)), + new("BindingAudit.Images", new("BindingAudit.images_eq")) +]; +var source = Path.Combine(root, "sources"); +Directory.CreateDirectory(source); +DeepSessionContribution contribution = new([], new(requests), new(StringComparer.Ordinal), + new(StringComparer.Ordinal), new(StringComparer.Ordinal), source); +AuditRequest[] expected = [new("Proof.correct"), .. requests.Select(request => request.Audit)]; +AuditedTheorem body = new("BindingAudit.bodies", "Theorem statement mismatch", [], null, null); +AuditedTheorem equality = new("BindingAudit.images_eq", null, [], null, null); +AuditedTheorem proof = new("Proof.correct", null, [], null, null); +foreach (var malformed in new AuditedTheorem[][] +{ + [body, equality], [proof, body, equality, new("Extra", null, [], null, null)], + [proof, body, equality, body], [proof with { axioms = null }, body, equality] +}) +{ + var untouched = new DeepCheckAttempt(inputs, identity.Sha256, inputs.MatchPins(identity.Root)); + Expect(() => contribution.RequireTheorems(new(new(), malformed, []), expected, untouched), + malformed.Select(result => result.name).Distinct().Count() != malformed.Length + ? "same key" : "Incomplete deep-check"); + Require(untouched.Failure("audit", "incomplete").facts.All(fact => + fact.status == FailureReports.BindingFailureStatus.NotCovered), "Malformed joined results mark no subject"); +} +var bodyAttempt = new DeepCheckAttempt(inputs, identity.Sha256, inputs.MatchPins(identity.Root)); +Expect(() => contribution.RequireTheorems(new(new(), [proof, body, equality], []), expected, bodyAttempt), + "Theorem statement mismatch"); +Require(bodyAttempt.Failure("audit", "rejected").facts.Single(fact => fact.id == "body-bytes").status == + FailureReports.BindingFailureStatus.Failed, "Named body rejection marks only BodyBytes"); +var equalityAttempt = new DeepCheckAttempt(inputs, identity.Sha256, inputs.MatchPins(identity.Root)); +Expect(() => contribution.RequireTheorems(new(new(), [proof, body with { error = null }, + equality with { error = "Theorem statement mismatch" }], []), expected, equalityAttempt), + "Theorem statement mismatch"); +Require(equalityAttempt.Failure("audit", "rejected").facts.All(fact => + fact.status == FailureReports.BindingFailureStatus.NotCovered), "Images equality has no fact subject"); +Console.WriteLine("PASS joined request-set validation precedes named failure attribution"); + +var scales = new[] { 64, 256, 1024 }.Select(count => +{ + DeepCheckInputs.Pin[] pins = [.. Enumerable.Range(0, count).Select(index => + new DeepCheckInputs.Pin("Identity" + index, + new("", Sha256Digest.Parse(index.ToString("x64"), "a test pin"), [])))]; + var scaled = new DeepCheckAttempt(inputs, identity.Sha256, pins); + foreach (var pin in pins) scaled.RejectImage(pin.Image.Digest, "rejected"); + return (Count: count, Attempt: scaled); +}).ToArray(); +foreach (var scale in scales) + for (var warm = 0; warm < 64; warm++) scale.Attempt.Failure("audit", "rejected"); +int repetitions = 1; +while (Measure(scales[0].Attempt, repetitions) < 25) repetitions *= 2; +var timings = scales.Select(_ => new List()).ToArray(); +Require(Growth(20, 80) && Growth(20, 100) && !Growth(20, 320), + "Scaling bound must accept linear/n log n growth and reject quadratic growth"); +double previousBytes = 0; +for (var round = 0; round < 7; round++) + foreach (var index in round % 2 == 0 ? new[] { 0, 1, 2 } : new[] { 2, 1, 0 }) + timings[index].Add(Measure(scales[index].Attempt, repetitions)); +double previousTime = 0; +for (var index = 0; index < scales.Length; index++) +{ + var started = GC.GetAllocatedBytesForCurrentThread(); + scales[index].Attempt.Failure("audit", "rejected"); + var allocated = GC.GetAllocatedBytesForCurrentThread() - started; + var minimum = timings[index].Min(); + if (previousTime > 0) + Require(Growth(previousTime, minimum) && allocated <= previousBytes * 6 + 1024, + "Failure mapping grows faster than linear work"); + Console.WriteLine($"PASS {scales[index].Count} failed image subjects: {allocated} bytes, " + + $"{minimum:F3} ms minimum for {repetitions} mappings"); + previousTime = minimum; + previousBytes = allocated; +} + +static double Measure(DeepCheckAttempt attempt, int repetitions) +{ + var timer = Stopwatch.StartNew(); + for (var repeat = 0; repeat < repetitions; repeat++) + GC.KeepAlive(attempt.Failure("audit", "rejected")); + return timer.Elapsed.TotalMilliseconds; +} + +static void Require(bool condition, string message) +{ + if (!condition) throw new InvalidOperationException(message); +} + +static bool Growth(double previous, double current) => current <= previous * 6 + 1; + +static void Expect(Action action, string diagnostic) +{ + try { action(); } + catch (Exception error) when (error.Message.Contains(diagnostic, StringComparison.Ordinal)) { return; } + throw new InvalidOperationException("Expected diagnostic rejection: " + diagnostic); +} diff --git a/Tests/DeepImportChecks.cs b/Tests/DeepImportChecks.cs new file mode 100644 index 000000000..01e533a1b --- /dev/null +++ b/Tests/DeepImportChecks.cs @@ -0,0 +1,32 @@ +partial class Program +{ + /// + /// Fails unless a keyed deep session bound its import closure, and unless every module in it outside the + /// toolchain is the extraction, StrictBinding or CIL. A contract interface, `ExtractedImports` or proof module + /// in the closure would mean the deep modules depend on a module this classification does not name. + /// + static void RequireDeepImports(SortedDictionary? imports, string path) + { + var modules = (imports ?? throw new InvalidOperationException("No deep import closure was keyed: " + path)) + .Keys.Select(ModuleName).ToHashSet(StringComparer.Ordinal); + static bool Under(string module, string library) => + module == library || module.StartsWith(library + ".", StringComparison.Ordinal); + if (!modules.Contains("Extracted") || !modules.Any(module => Under(module, "StrictBinding")) || + !modules.Any(module => Under(module, "CIL")) || modules.Any(module => module != "Extracted" && + !Under(module, "StrictBinding") && !Under(module, "CIL"))) + throw new InvalidOperationException("Unexpected deep import closure: " + string.Join(", ", modules)); + Console.WriteLine($"PASS the {path} deep key binds {imports.Count} parts of {modules.Count} imported " + + $"modules: Extracted, {modules.Count(module => Under(module, "StrictBinding"))} StrictBinding and " + + $"{modules.Count(module => Under(module, "CIL"))} CIL"); + } + + /// Changes the last byte of a file and restores its write time, keeping its length. + static void SameLengthChange(string path) + { + var written = File.GetLastWriteTimeUtc(path); + var bytes = File.ReadAllBytes(path); + bytes[^1] ^= 1; + File.WriteAllBytes(path, bytes); + File.SetLastWriteTimeUtc(path, written); + } +} diff --git a/Tests/DeepSession.cs b/Tests/DeepSession.cs new file mode 100644 index 000000000..8f99a20e8 --- /dev/null +++ b/Tests/DeepSession.cs @@ -0,0 +1,323 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/DeepCheckInputs.cs +#:include MultiAssemblyFixture.cs + +using System.Text.Json; +using System.Text.Json.Nodes; + +if (args is ["--request-scaling"]) +{ + foreach (var count in new[] { 16, 128, 1024 }) + { + BindingImagePin[] pins = [.. Enumerable.Range(0, count).Select(index => + new BindingImagePin("Identity" + index, Sha256Digest.Parse(index.ToString("x64"), "a test pin"), []))]; + BindingPlan plan = new("", pins, 0); + var json = JsonSerializer.Serialize(BindingRequests(plan).Select(request => request.Audit), AuditJson); + Console.WriteLine($"PASS {count} pins serialized in {json.Length} characters"); + } + return; +} + +using Program verification = new(); +var replayDeclaration = "BindingAudit.I" + new string('a', 64) + ".hash0._proof_1_1"; +var replayDiagnostic = BindingDiagnostic($""" + Command failed (1): C:/{new string('p', 320)}/leanchecker.exe BindingAudit.I{new string('a', 64)} + leanchecker found a problem in BindingAudit.I{new string('a', 64)} + uncaught exception: while replaying declaration '{replayDeclaration}': + (kernel) application type mismatch + id (Eq.refl Bool.true) + """); +if (!replayDiagnostic.Contains(replayDeclaration, StringComparison.Ordinal) || + !replayDiagnostic.Contains("(kernel) application type mismatch", StringComparison.Ordinal) || + replayDiagnostic.Length > 303) + throw new InvalidOperationException("Binding diagnostic lost the rejected replay declaration or its error"); +Console.WriteLine("PASS replay diagnostics retain the declaration and kernel rejection after a long command"); +SortedDictionary certifiedArtifacts = new(StringComparer.Ordinal) +{ + ["CIL.olean"] = "main", + ["CIL.olean.private"] = "private" +}; +SortedDictionary builtArtifacts = new(certifiedArtifacts, StringComparer.Ordinal) +{ + ["StrictBinding.olean"] = "binding", + ["StrictBinding.olean.private"] = "binding-private" +}; +if (!ModuleArtifactsPreserved(certifiedArtifacts, builtArtifacts)) + throw new InvalidOperationException("Binding preparation rejected an additional module family"); +builtArtifacts["CIL.olean.private"] = "changed"; +if (ModuleArtifactsPreserved(certifiedArtifacts, builtArtifacts)) + throw new InvalidOperationException("Binding preparation accepted a changed certified companion"); +builtArtifacts.Remove("CIL.olean.private"); +if (ModuleArtifactsPreserved(certifiedArtifacts, builtArtifacts)) + throw new InvalidOperationException("Binding preparation accepted a removed certified companion"); +builtArtifacts["CIL.olean.private"] = "private"; +builtArtifacts["CIL.olean.injected"] = "injected"; +if (ModuleArtifactsPreserved(certifiedArtifacts, builtArtifacts)) + throw new InvalidOperationException("Binding preparation accepted an added certified companion"); +SortedDictionary sessionArtifacts = new(StringComparer.Ordinal) +{ + ["CIL.olean.injected"] = "injected" +}; +SortedDictionary combinedArtifacts = new(certifiedArtifacts, StringComparer.Ordinal); +foreach (var (name, hash) in sessionArtifacts) combinedArtifacts[name] = hash; +if (!ModuleArtifactsPreserved(new(StringComparer.Ordinal), sessionArtifacts) || + ModuleArtifactsPreserved(certifiedArtifacts, combinedArtifacts)) + throw new InvalidOperationException("Binding preparation accepted a session companion for a base-only family"); +Console.WriteLine("PASS binding builds preserve complete certified module families"); +double previousInventoryMilliseconds = 0; +foreach (var count in new[] { 64, 512, 4096 }) +{ + SortedDictionary before = new(StringComparer.Ordinal); + for (var index = 0; index < count; index++) before[$"M{index:D5}.olean"] = "bytes"; + SortedDictionary after = new(before, StringComparer.Ordinal) + { + ["StrictBinding.olean"] = "binding" + }; + const int repetitions = 64; + var milliseconds = double.MaxValue; + for (var sample = 0; sample < 7; sample++) + { + var elapsed = System.Diagnostics.Stopwatch.StartNew(); + for (var repetition = 0; repetition < repetitions; repetition++) + { + after["StrictBinding.olean"] = repetition.ToString(); + if (!ModuleArtifactsPreserved(before, after)) + throw new InvalidOperationException("Binding inventory scaling rejected an additional family"); + } + milliseconds = Math.Min(milliseconds, elapsed.Elapsed.TotalMilliseconds); + } + if (previousInventoryMilliseconds > 0 && milliseconds > previousInventoryMilliseconds * 20 + 20) + throw new InvalidOperationException("Binding inventory validation grew faster than n log n"); + previousInventoryMilliseconds = milliseconds; + Console.WriteLine($"PASS {count} binding artifacts validated {repetitions} times in {milliseconds:F1} ms minimum"); +} +if (args is ["--artifact-inventory"]) return; +MultiAssemblyFixture fixture = new("DeepSession"); +var artifactDirectory = Path.Combine(fixture.Root, "directory.olean.test"); +Directory.CreateDirectory(artifactDirectory); +var testArtifact = Path.Combine(artifactDirectory, "Extracted.olean"); +File.WriteAllBytes(testArtifact, [0]); +if (BindingArtifactHashes([testArtifact]).Count != 1) + throw new InvalidOperationException("An artifact directory name hid its module"); +File.Delete(testArtifact); +ExpectFailure(() => BindingArtifactHashes([testArtifact]), "missing"); +Console.WriteLine("PASS artifact inventories include modules below .olean directories and reject missing modules"); +var image = fixture.Build("Input", "DeepFixture", """ + public static class Input { public static int Value(int value) => value + 1; } + """); +var extraction = fixture.Artifact("Input"); +Run([.. fixture.Cli, "extract", image, "Input::Value(System.Int32)", extraction]); +var identity = ExtractionIdentity.Read(extraction); +var digest = Sha(image); +var digestFile = Path.Combine(fixture.Root, "inputs.sha256"); +File.WriteAllText(digestFile, digest + " " + Path.GetRelativePath(fixture.Root, image) + "\n"); +var inputs = DeepCheckInputs.Read(digestFile); +var package = verification.Library(Inputs(At("CIL.lean")), writes: true); +var session = Path.Combine(fixture.Root, "session"); +Directory.CreateDirectory(session); +var search = session + Path.PathSeparator + verification.SessionLeanPath(package); +var extractedSource = Path.Combine(session, "Extracted.lean"); +var extractedOlean = Path.ChangeExtension(extractedSource, ".olean"); +File.Copy(Path.Combine(extraction, "Extracted.lean"), extractedSource); +Compile(extractedSource); +File.Delete(extractedSource); +var retainedExtraction = BindingArtifactHashes([extractedOlean]); +var baseArtifact = Path.Combine(verification.library!, ".lake/build/lib/lean/CIL.olean"); +var baseBytes = File.ReadAllBytes(baseArtifact); +File.WriteAllBytes(baseArtifact, [0]); +ExpectFailure(() => verification.PrepareBindingLibrary(package), "certified library changed"); +File.WriteAllBytes(baseArtifact, baseBytes); +verification.PrepareBindingLibrary(package); +var preparedArtifacts = verification.SessionModules(package); +verification.PrepareBindingLibrary(package); +verification.bindingPreparation = null; +verification.PrepareBindingLibrary(package); +if (!Same(preparedArtifacts, verification.SessionModules(package)) || + !Same(retainedExtraction, BindingArtifactHashes([extractedOlean]))) + throw new InvalidOperationException("Repeated binding preparation changed compiled artifacts"); +ExpectFailure(() => verification.PrepareBindingLibrary(session), "selected certified library session"); +var bindingArtifact = Path.Combine(package, ".lake/build/lib/lean/StrictBinding.olean"); +var bindingBytes = File.ReadAllBytes(bindingArtifact); +File.WriteAllBytes(bindingArtifact, [0]); +ExpectFailure(() => verification.PrepareBindingLibrary(package), "prepared StrictBinding library changed"); +File.WriteAllBytes(bindingArtifact, bindingBytes); +var addedCompanion = bindingArtifact + ".unexpected"; +File.WriteAllBytes(addedCompanion, [0]); +ExpectFailure(() => verification.PrepareBindingLibrary(package), "prepared StrictBinding library changed"); +File.Delete(addedCompanion); +File.Delete(bindingArtifact); +ExpectFailure(() => verification.PrepareBindingLibrary(package), "prepared StrictBinding library changed"); +File.WriteAllBytes(bindingArtifact, bindingBytes); +var bindingSource = Path.Combine(package, "StrictBinding.lean"); +var bindingSourceText = File.ReadAllText(bindingSource); +File.AppendAllText(bindingSource, "\n"); +ExpectFailure(() => verification.PrepareBindingLibrary(package), "prepared StrictBinding library changed"); +File.WriteAllText(bindingSource, bindingSourceText); +verification.PrepareBindingLibrary(package); +Console.WriteLine("PASS binding preparation reuses the selected library and rejects artifact and source mutations"); +CheckClosureWork(); +var proofSource = Path.Combine(session, "Proof.lean"); +File.WriteAllText(proofSource, """ + import Extracted + /-- The extracted program has the certificate checked in this same environment. -/ + theorem certificate : CIL.Extraction.TranslationValid Extracted.rawLayouts Extracted.computedLayouts + Extracted.assemblies Extracted.platformDefinitions Extracted.methodEvidence Extracted.imports + Extracted.program := + Extracted.translation_valid + #print axioms certificate + """); +Compile(proofSource); +File.WriteAllBytes(image, [0]); +var contribution = verification.BuildDeepSession(session, search, extractedOlean, identity, + inputs.MatchPins(identity.Root)); +if (File.Exists(extractedSource)) throw new InvalidOperationException("Deep checks copied extraction source"); +if (!contribution.Modules.Contains("BindingAudit.I" + digest)) + throw new InvalidOperationException("Image module does not use the complete digest"); +var requests = contribution.Requests.Select(request => request.Audit).Append(ProofTheorem("certificate")).ToArray(); +string[] jointModules = [.. contribution.Modules, "Proof"]; +var audited = verification.AuditOleans(search, jointModules, requests, replayTargets: jointModules); +contribution.RequireAudited(audited); +foreach (var name in contribution.Requests.Where(request => request.CoveragePredicate is not null) + .Select(request => request.Audit.name)) + if (CoverageComplete(audited.theorems.Single(theorem => theorem.name == name))) + throw new InvalidOperationException($"The scalar-only fixture reported complete coverage for {name}"); +RequireAuditedModule(audited, "Proof", Path.ChangeExtension(proofSource, ".olean")); +foreach (var theorem in audited.theorems) AuditedAxioms(theorem); +Console.WriteLine("PASS deep modules and proof share the existing extraction with retained image bytes"); + +var digestRequest = contribution.Requests.Single(request => request.Module == "BindingAudit.I" + digest).Audit; +var wrongDigest = JsonSerializer.SerializeToNode(digestRequest.type)!; +wrongDigest["app"]![2]!["nat"] = 0; +var wrongConstant = JsonSerializer.SerializeToNode(digestRequest.type)!; +wrongConstant["app"]![1]!["const"] = "BindingAudit.images"; +var bodiesRequest = contribution.Requests.Single(request => request.Module == BodiesModule).Audit; +var wrongEvidence = JsonSerializer.SerializeToNode(bodiesRequest.type)!; +wrongEvidence["app"]![2]!["const"] = "Extracted.program"; +AuditRequest[] rejected = +[ + digestRequest with { type = wrongDigest }, + digestRequest with { type = wrongConstant }, + bodiesRequest with { type = wrongEvidence } +]; +var negative = verification.AuditOleans(search, [.. contribution.Modules, "Proof"], + [.. rejected, ProofTheorem("certificate"), ExtractionCertificate("Extracted.translation_valid")]); +contribution.RequireAudited(negative); +if (negative.theorems.Take(3).Any(theorem => theorem.error is null) || + negative.theorems.Skip(3).Any(theorem => theorem.error is not null)) + throw new InvalidOperationException("Joint audit accepted a wrong digest, image constant or evidence constant"); +Console.WriteLine("PASS joint audit rejects wrong digests, image constants and body evidence"); + +var originalOlean = File.ReadAllBytes(extractedOlean); +File.WriteAllBytes(extractedOlean, [0]); +ExpectFailure(contribution.RequireStable, "Deep-check artifact changed"); +File.WriteAllBytes(extractedOlean, originalOlean); +foreach (var companion in Directory.EnumerateFiles(session, "Extracted.olean.*") + .Where(path => path != extractedOlean)) +{ + var original = File.ReadAllBytes(companion); + File.WriteAllBytes(companion, [0]); + ExpectFailure(contribution.RequireStable, "Deep-check artifact changed"); + File.WriteAllBytes(companion, original); +} +Console.WriteLine("PASS changed extraction artifact is detected"); +foreach (var artifact in new[] { extractedOlean, contribution.Artifacts["BindingAudit.I" + digest] }) +{ + var added = artifact + ".unexpected"; + File.WriteAllBytes(added, [0]); + ExpectFailure(contribution.RequireStable, "Deep-check artifact changed"); + File.Delete(added); + var unrelated = artifact + "Other"; + File.WriteAllBytes(unrelated, [0]); + contribution.RequireStable(); + File.Delete(unrelated); +} +Console.WriteLine("PASS added module companions are rejected and unrelated prefix names are excluded"); + +var manifest = JsonNode.Parse(identity.Root.GetRawText())!; +manifest["references"] = new JsonArray(new JsonObject +{ + ["assemblyIdentity"] = "Missing", + ["sha256"] = new string('0', 64) +}); +using var missing = JsonDocument.Parse(manifest.ToJsonString()); +ExpectFailure(() => inputs.MatchPins(missing.RootElement), "No line of"); +Console.WriteLine("PASS every reference requires a consumer-stated digest"); + +var same = new BindingImagePin("First", Sha256Digest.Parse(digest, "the test image"), + inputs.Images[0].Bytes.ToArray()); +BindingPlan shared = new("", [same, same with { Identity = "Second" }], 0); +var sharedSource = ImagesModuleSource(shared); +if (sharedSource.Split("import " + same.Module).Length != 2 || + !sharedSource.Contains("identity := \"First\"", StringComparison.Ordinal) || + !sharedSource.Contains("identity := \"Second\"", StringComparison.Ordinal) || + BindingRequests(shared).Count(request => request.Module == same.Module) != 1) + throw new InvalidOperationException("Equal digests lost an association or duplicated their image module"); +Console.WriteLine("PASS equal digests share an image module and retain both identity associations"); + +var imageModule = "BindingAudit.I" + digest; +var imageSource = ModuleSource(session, imageModule); +var sourceText = File.ReadAllText(imageSource); +var page = sourceText.IndexOf("def page0 : List Nat := ", StringComparison.Ordinal); +var digit = page < 0 ? -1 : sourceText.IndexOf("0x", page, StringComparison.Ordinal) + 2; +if (digit < 2) throw new InvalidOperationException("Missing image byte literal"); +File.WriteAllText(imageSource, sourceText[..digit] + (sourceText[digit] == '0' ? '1' : '0') + + sourceText[(digit + 1)..]); +Compile(imageSource); +ExpectFailure(() => verification.ReplayCheckedModules([imageModule], search, "Replay changed image"), + imageModule + ".hash"); +Console.WriteLine("PASS kernel replay rejects changed bytes in a deep-session image module"); + +void Compile(string source) +{ + var result = Execute([verification.LeanExecutable, "--root=" + session, "-o", + Path.ChangeExtension(source, ".olean"), source], leanPath: search, environment: verification.LeanEnvironment); + if (result.Code != 0) throw new InvalidOperationException(result.Output); +} + +void ExpectFailure(Action action, string marker) +{ + try { action(); } + catch (Exception error) when (error.Message.Contains(marker, StringComparison.Ordinal)) { return; } + throw new InvalidOperationException("Expected failure: " + marker); +} + +// A request reads headers in one launch; after the first, it reads and hashes only the session's own modules, +// so a warm request's work does not grow with the certified part of its closure. +void CheckClosureWork() +{ + int? sessionHeaders = null; + foreach (var (label, import) in new[] { ("small", "CIL.Identity"), ("medium", "CIL.Instructions"), + ("large", "StrictBinding") }) + { + var directory = Path.Combine(session, "Closure" + label); + Directory.CreateDirectory(directory); + File.WriteAllText(Path.Combine(directory, "Root.lean"), "import " + import + "\n"); + var started = System.Diagnostics.Stopwatch.StartNew(); + var cold = verification.DeepImportClosure(session, search, directory); + var coldWork = verification.deepClosureWork; + var coldSeconds = started.Elapsed.TotalSeconds; + started.Restart(); + var warm = verification.DeepImportClosure(session, search, directory); + var warmWork = verification.deepClosureWork; + Directory.Delete(directory, true); + var certified = cold.Keys.Count(name => verification.baseModules!.ContainsKey(name)); + sessionHeaders ??= warmWork.Headers; + if (!Same(cold, warm) || certified == 0 || coldWork.Launches != 2 || warmWork.Launches != 1 || + warmWork.Hashed != cold.Count - certified || coldWork.Hashed < warmWork.Hashed || + label == "small" && coldWork.Hashed != cold.Count || + warmWork.Headers != sessionHeaders || label == "small" && coldWork.Headers <= warmWork.Headers) + throw new InvalidOperationException( + $"Deep import closure work changed for {label}: {coldWork}, {warmWork}"); + Console.WriteLine($"PASS a {label} deep import closure binds {cold.Count} parts, {certified} certified: " + + $"cold {coldWork.Launches} launches, {coldWork.Headers} headers, {coldWork.Hashed} parts " + + $"({coldWork.HashedBytes} bytes) hashed in {coldSeconds:F1}s; warm {warmWork.Launches} launch, " + + $"{warmWork.Headers} headers, {warmWork.Hashed} parts ({warmWork.HashedBytes} bytes) hashed in " + + $"{started.Elapsed.TotalSeconds:F1}s"); + } +} diff --git a/Tests/DeepVerify.cs b/Tests/DeepVerify.cs new file mode 100644 index 000000000..0c88947d6 --- /dev/null +++ b/Tests/DeepVerify.cs @@ -0,0 +1,753 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include MultiAssemblyFixture.cs +#:include DeepImportChecks.cs + +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; + +using Program verification = new(); +bool focusedDiagnostics = args is ["--diagnostics"] or ["--diagnostic", _]; +Environment.SetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE", null); +SortedDictionary retained = new(StringComparer.Ordinal) { ["Extracted.olean"] = "original" }; +SortedDictionary extended = new(retained, StringComparer.Ordinal) +{ + ["BindingAudit/Image.olean"] = "image", + ["BindingAudit/Image.olean.private"] = "private" +}; +RequireOnlyAdded(retained, extended, "BindingAudit/Image"); +extended["BindingAudit/Image.oleanOther"] = "unexpected"; +ExpectFailure(() => RequireOnlyAdded(retained, extended, "BindingAudit/Image"), "Compiled artifacts changed"); +extended.Remove("BindingAudit/Image.oleanOther"); +extended["Extracted.olean"] = "changed"; +ExpectFailure(() => RequireOnlyAdded(retained, extended, "BindingAudit/Image"), "Compiled artifacts changed"); +extended.Remove("Extracted.olean"); +ExpectFailure(() => RequireOnlyAdded(retained, extended, "BindingAudit/Image"), "Compiled artifacts changed"); +double previousInventoryMilliseconds = 0; +foreach (var count in new[] { 64, 512, 4096 }) +{ + string[] modules = [.. Enumerable.Range(0, count).Select(index => "BindingAudit/I" + index)]; + SortedDictionary built = new(retained, StringComparer.Ordinal); + foreach (var module in modules) built[module + ".olean"] = "image"; + var milliseconds = double.MaxValue; + for (var sample = 0; sample < 7; sample++) + { + var elapsed = System.Diagnostics.Stopwatch.StartNew(); + for (var repetition = 0; repetition < 64; repetition++) RequireOnlyAdded(retained, built, modules); + milliseconds = Math.Min(milliseconds, elapsed.Elapsed.TotalMilliseconds); + } + if (previousInventoryMilliseconds > 0 && milliseconds > previousInventoryMilliseconds * 20 + 20) + throw new InvalidOperationException("Artifact addition checks grew faster than n log n"); + previousInventoryMilliseconds = milliseconds; + Console.WriteLine($"PASS {count} added module families checked 64 times in {milliseconds:F1} ms minimum"); +} +if (args is ["--artifact-inventory"]) return; +MultiAssemblyFixture fixture = new("DeepVerify"); +verification.EnsureExtractorBuilt(); +var image = fixture.Build("Input", "DeepFixture", """ + public static class Input { public static ulong Value(ulong value) => value + 1; } + """); +var proof = Path.Combine(fixture.Root, "Proof.lean"); +File.WriteAllText(proof, """ + import Extracted + import CIL.Safety.Execution + import Tests.Support + namespace ExampleProof + theorem correctness : + (CIL.Safety.invoke Extracted.program 8 0 [.scalar (.i64 2)] CIL.Safety.Memory.empty).map + Prod.snd = .ok [.scalar (.i64 3)] := rfl + #print axioms correctness + end ExampleProof + """); +var manifest = Path.Combine(fixture.Root, "inputs.sha256"); +var unused = Path.Combine(fixture.Root, "unused.bin"); +File.WriteAllBytes(unused, [1, 2, 3]); +File.WriteAllText(manifest, Sha(image) + " " + Path.GetRelativePath(fixture.Root, image) + "\n" + + Sha(unused) + " " + Path.GetFileName(unused) + "\n"); +var inputs = DeepCheckInputs.Read(manifest); +var output = fixture.Artifact("Verified"); +const string selector = "Input::Value(System.UInt64)"; +if (args is not ["--support-restore"] && !focusedDiagnostics) +using (Program attacked = new()) +{ + attacked.Library(Inputs(proof)); + var originalCache = Environment.GetEnvironmentVariable("LEANCIL_CACHE_DIR"); + try + { + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", Path.Combine(fixture.Root, "SupportMutationCache")); + string[]? receipts = null; + ExpectFailure(() => attacked.Verify(image, "DeepFixture", selector, proof, output, deepChecks: inputs, + stage: stage => + { + if (stage != "kernel replay" || receipts is not null) return; + receipts = Receipts(); + var artifact = Path.Combine(attacked.session!, ".lake/build/lib/lean/StrictBinding.olean.unexpected"); + File.WriteAllBytes(artifact, [0]); + }), "prepared StrictBinding library changed"); + if (receipts is null || !receipts.SequenceEqual(Receipts())) + throw new InvalidOperationException("Rejected support mutation published a successful receipt"); + RequireNoReport(); + } + finally { Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", originalCache); } +} +if (args is not ["--support-restore"] && !focusedDiagnostics) + Console.WriteLine("PASS support replay mutation preserves no successful receipt or deep report"); +if (args is ["--support-mutation"]) return; +var report = verification.Verify(image, "DeepFixture", selector, proof, output, deepChecks: inputs); +// Each audited fact names the theorem that states it; every other fact stays not covered. +(BindingFact Fact, string Theorem)[] auditedFacts = + [(BindingFact.BodyBytes, "BindingAudit.bodies"), (BindingFact.MetadataRows, "BindingAudit.metadataRows"), + (BindingFact.StaticData, "BindingAudit.staticData")]; +if (report.binding.mode != BindingMode.Deep || + report.binding.facts.Any(fact => auditedFacts.Any(audited => audited.Fact == fact.id) + ? fact.status != BindingStatus.Reconfirmed || + fact.theorem != auditedFacts.Single(audited => audited.Fact == fact.id).Theorem + : fact.status != BindingStatus.NotCovered) || + report.binding.images.Length != 2 || report.binding.images[0].status != BindingStatus.Reconfirmed || + report.binding.images[1].role != ImageRole.Unused || + report.binding.images[1].status != BindingStatus.NotCovered || + CertificateProblem(File.ReadAllText(Path.Combine(output, "verified.json")), BindingMode.Deep) is not null) + throw new InvalidOperationException("Deep verification reported facts beyond its joint audit"); +Console.WriteLine("PASS standalone verification reports exactly the deep facts it audited"); +if (args is not ["--support-restore"]) CheckFailureDiagnostics(); +if (focusedDiagnostics) return; +using (Program restoredSupport = new()) +{ + bool audited = false; + var restoredReport = restoredSupport.Verify(image, "DeepFixture", selector, proof, output, deepChecks: inputs, + stage: stage => audited |= stage == "theorem audit"); + var original = restoredSupport.bindingPreparation!; + if (restoredSupport.supportCacheHits == 0 || !audited || restoredReport.binding.mode != BindingMode.Deep || + !restoredReport.binding.facts.SequenceEqual(report.binding.facts) || + !restoredReport.binding.images.SequenceEqual(report.binding.images) || + !Same(original.BaseArtifacts, ModuleArtifacts(restoredSupport.library!)) || + !ModuleArtifactsPreserved(original.SessionArtifacts, ModuleArtifacts(restoredSupport.session!)) || + !ModuleArtifactsPreserved(original.CertifiedArtifacts, + restoredSupport.SessionModules(restoredSupport.session!))) + throw new InvalidOperationException( + "Authenticated support reuse changed the binding results or prepared library, or skipped audit"); +} +Console.WriteLine("PASS authenticated support restoration preserves prepared families and runs a deep audit"); +if (args is ["--support-restore"]) return; + +var untrusted = Path.Combine(verification.session!, ".lake/build/lib/lean/Unexpected.olean"); +File.WriteAllBytes(untrusted, [0]); +try +{ + ExpectFailure(() => verification.PrepareBindingLibrary(verification.session!), "certified library changed"); +} +finally { File.Delete(untrusted); } +const string companionName = "StrictBinding.olean.unexpected"; +var companion = Path.Combine(verification.session!, ".lake/build/lib/lean", companionName); +File.WriteAllBytes(companion, [0]); +verification.certifiedModules!.Add(companionName, Sha(companion)); +try +{ + ExpectFailure(() => verification.PrepareBindingLibrary(verification.session!), + "prepared StrictBinding library changed"); +} +finally +{ + verification.certifiedModules.Remove(companionName); + File.Delete(companion); +} +Console.WriteLine("PASS binding preparation rejects uncertified families and prepared-family additions"); + +var prepared = verification.bindingPreparation!; +var sessionFamilies = prepared.SessionArtifacts.Keys.Select(ModuleName).ToHashSet(StringComparer.Ordinal); +var baseOnly = prepared.BaseArtifacts.Keys.First(name => name.EndsWith(".olean", StringComparison.Ordinal) && + !sessionFamilies.Contains(ModuleName(name))); +var baseCompanionName = baseOnly + ".unexpected"; +var baseCompanion = Path.Combine(verification.session!, ".lake/build/lib/lean", baseCompanionName); +Directory.CreateDirectory(Path.GetDirectoryName(baseCompanion)!); +File.WriteAllBytes(baseCompanion, [0]); +verification.certifiedModules.Add(baseCompanionName, Sha(baseCompanion)); +try +{ + ExpectFailure(() => verification.PrepareBindingLibrary(verification.session!), + "prepared StrictBinding library changed"); +} +finally +{ + verification.certifiedModules.Remove(baseCompanionName); + File.Delete(baseCompanion); +} +Console.WriteLine("PASS a base-only family's certified session companion is rejected on preparation reuse"); +if (args is ["--prepared-inventory"]) return; + +var subjects = BindingRequests(new("", [new("Identity", inputs.Images[0].Digest, [])], 1)); +// Exercise report projection and scaling with the same literal term shape as the joint audit. +static AuditedTheorem IncompleteCoverage(string theorem, string predicate) => new(theorem, null, [], null, null, + JsonSerializer.SerializeToElement(new { root = 2, nodes = new object[] { + new object[] { "const", 0, new[] { "StrictBinding", predicate }, Array.Empty() }, + new object[] { "const", 0, new[] { "Bool", "false" }, Array.Empty() }, + new object[] { "app", 0, 1 } } })); +var conditional = subjects.Where(request => request.CoveragePredicate is not null).ToArray(); +AuditedOleans coverageAudit = new(new(), [.. conditional.Select(request => + IncompleteCoverage(request.Audit.name, request.CoveragePredicate!))], []); +var withoutRows = DeepBindingSection(inputs, [new("Identity", inputs.Images[0])], + [.. subjects.Where(request => request.Module != MetadataRowsModule)], report.binding.sources!, coverageAudit); +if (conditional.Select(request => ((BindingSubject.Fact)request.Subject!).Value).Any(subject => + withoutRows.facts.Single(fact => fact.id == subject).status != BindingStatus.NotCovered) || + withoutRows.facts.Single(fact => fact.id == BindingFact.MetadataRows).status != BindingStatus.NotCovered || + withoutRows.facts.Single(fact => fact.id == BindingFact.BodyBytes).theorem != "BindingAudit.bodies") + throw new InvalidOperationException("A fact without an audited request was reported as reconfirmed"); +Console.WriteLine("PASS the deep report reconfirms exactly the subjects of its audited requests"); +CheckDeepResultKey(); +double previousMilliseconds = 0; +foreach (var count in new[] { 64, 512, 4096 }) +{ + DeepCheckInputs.Pin[] pins = [.. Enumerable.Range(0, count) + .Select(index => new DeepCheckInputs.Pin("Identity" + index, inputs.Images[0]))]; + var milliseconds = double.MaxValue; + for (var sample = 0; sample < 7; sample++) + { + var elapsed = System.Diagnostics.Stopwatch.StartNew(); + for (var repetition = 0; repetition < 64; repetition++) + { + var section = DeepBindingSection(inputs, pins, subjects, report.binding.sources!, coverageAudit); + if (section.images.Length != count + 1) + throw new InvalidOperationException("Deep report lost an image association"); + } + milliseconds = Math.Min(milliseconds, elapsed.Elapsed.TotalMilliseconds); + } + if (previousMilliseconds > 0 && milliseconds > previousMilliseconds * 20 + 20) + throw new InvalidOperationException("Deep report construction grew faster than n log n"); + previousMilliseconds = milliseconds; + Console.WriteLine($"PASS {count} image associations reported 64 times in {milliseconds:F1} ms minimum"); +} + +CheckDeepResultReuse(); +if (args is ["--support-reuse"]) return; + +Reject("generated source mutation", "Deep-check source changed", "theorem audit", fresh => + File.AppendAllText(Path.Combine(fresh, "BindingAudit", "Images.lean"), "\n-- changed\n")); +Reject("generated artifact mutation", "artifact", "theorem audit", fresh => + File.WriteAllBytes(Path.Combine(fresh, "BindingAudit", "Bodies.olean.unexpected"), [0])); +Reject("late source mutation", "Deep-check source changed", "extraction integrity", fresh => + File.AppendAllText(Path.Combine(fresh, "BindingAudit", "Bodies.lean"), "\n-- changed\n")); +Reject("added generated source", "Deep-check source changed", "theorem audit", fresh => + File.WriteAllText(Path.Combine(fresh, "BindingAudit", "Unexpected.lean"), "import BindingAudit.Bodies\n")); +ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, proof, output, deepChecks: inputs, + stage: stage => + { + if (stage != "deep checks") return; + var fresh = Directory.EnumerateDirectories(output, "proof-*").Single(); + File.WriteAllBytes(Path.Combine(fresh, "Extracted.olean.unexpected"), [0]); + }), "Compiled artifacts changed"); +RequireNoReport(); +Console.WriteLine("PASS a pre-build extraction companion mutation prevents deep report publication"); +Reject("wrong body evidence", "Compiled artifacts changed", "theorem audit", fresh => WriteWrongBodies(fresh)); +Reject("cold wrong body evidence", "Compiled artifacts changed", "kernel replay", fresh => WriteWrongBodies(fresh), + Path.Combine(fixture.Root, "ColdRejectCache")); + +var wrongManifest = Path.Combine(fixture.Root, "wrong.sha256"); +File.WriteAllText(wrongManifest, Sha(unused) + " " + Path.GetFileName(unused) + "\n"); +ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, proof, output, + deepChecks: DeepCheckInputs.Read(wrongManifest)), "No line of"); +RequireNoReport(); +Console.WriteLine("PASS missing pinned input rejects verification and removes its earlier report"); + +verification.fullImportAudit = (_, fresh, _, _) => +{ + File.AppendAllText(Path.Combine(fresh, "BindingAudit", "Images.lean"), "\n-- changed\n"); + return new("composition", "", [], []); +}; +try +{ + ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, proof, output, + deepChecks: inputs), "Deep-check source changed"); + RequireNoReport(); +} +finally { verification.fullImportAudit = null; } +Console.WriteLine("PASS import callback mutation prevents deep report publication"); + +var previousCache = Environment.GetEnvironmentVariable("LEANCIL_CACHE_DIR"); +var coldProof = Path.Combine(fixture.Root, "ColdProof.lean"); +File.WriteAllText(coldProof, File.ReadAllText(proof) + "\n-- Separate proof input.\n"); +var coldCache = Path.Combine(fixture.Root, "ColdCache"); +try +{ + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", coldCache); + string[]? receipts = null; + ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, coldProof, output, deepChecks: inputs, + stage: stage => + { + if (stage == "deep checks") receipts = Receipts(); + if (stage != "extraction integrity" || receipts is null) return; + var fresh = Directory.EnumerateDirectories(output, "proof-*").Single(); + File.AppendAllText(Path.Combine(fresh, "BindingAudit", "Images.lean"), "\n-- changed\n"); + }), "Deep-check source changed"); + if (receipts is null || !receipts.SequenceEqual(Receipts())) + throw new InvalidOperationException("Rejected cold deep verification published a receipt"); + RequireNoReport(); +} +finally { Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", previousCache); } +Console.WriteLine("PASS rejected cold deep verification publishes no proof, extraction or deep result receipt"); + +string[] Receipts() => Directory.Exists(SuccessCacheRoot) + ? [.. Directory.EnumerateFiles(SuccessCacheRoot, "receipt.json", SearchOption.AllDirectories) + .Select(path => path + "=" + Sha(path)).Order(StringComparer.Ordinal)] : []; + +foreach (var name in new[] { "source", "toolchain" }) +{ + var path = Path.Combine(verification.session!, name == "source" ? "StrictBinding.lean" : "lean-toolchain"); + var original = File.ReadAllBytes(path); + try + { + Reject("prepared " + name + " mutation", "Deep-check sources, toolchain", "extraction integrity", _ => + File.AppendAllText(path, "\n")); + } + finally { File.WriteAllBytes(path, original); } +} + +// Each input of a deep result key, the StrictBinding sources by content alone, selects another key. +void CheckDeepResultKey() +{ + BindingImagePin[] pins = [new("Identity", inputs.Images[0].Digest, [])]; + var requests = BindingRequests(new("", pins, 1)); + string Key(string environment = "environment", string sources = "sources", string toolchain = "toolchain", + char imported = 'a', char generated = 'b', BindingImagePin[]? pinned = null, + BindingRequest[]? requested = null) => DeepResultKey(environment, sources, toolchain, + new(StringComparer.Ordinal) { ["Extracted.olean"] = new string(imported, 64) }, + new(StringComparer.Ordinal) { ["BindingAudit/Bodies.lean"] = new string(generated, 64) }, + pinned ?? pins, requested ?? requests); + var copy = Path.Combine(fixture.Root, "BindingSources"); + foreach (var source in Directory.EnumerateFiles(At("StrictBinding"), "*.lean", SearchOption.AllDirectories) + .Append(At("StrictBinding.lean"))) + { + var destination = Path.Combine(copy, Path.GetRelativePath(Root, source)); + Directory.CreateDirectory(Path.GetDirectoryName(destination)!); + File.Copy(source, destination); + } + var original = BindingSourcesKey(copy); + var edited = Path.Combine(copy, "StrictBinding.lean"); + var written = File.GetLastWriteTimeUtc(edited); + var bytes = File.ReadAllBytes(edited); + bytes[^1] ^= 1; + File.WriteAllBytes(edited, bytes); + File.SetLastWriteTimeUtc(edited, written); + string[] changed = [Key("other"), Key(sources: BindingSourcesKey(copy)), Key(toolchain: "other"), + Key(imported: 'c'), Key(generated: 'c'), Key(pinned: [new("Other", inputs.Images[0].Digest, [])]), + Key(pinned: [new("Identity", Sha256Digest.Of([1]), [])]), Key(requested: requests[..^1])]; + if (Key() != Key() || + changed.Append(Key(sources: original)).Append(Key()).Distinct().Count() != changed.Length + 2) + throw new InvalidOperationException("A deep result key ignores one of its inputs"); + Console.WriteLine("PASS a same-length StrictBinding change and every other deep result input change the key"); +} + +// One cache holds every run here, so whether each run restores its deep result is known. +void CheckDeepResultReuse() +{ + var previousCache = Environment.GetEnvironmentVariable("LEANCIL_CACHE_DIR"); + try + { + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", Path.Combine(fixture.Root, "ReuseCache")); + var cold = DeepRun(image, selector, proof, output, inputs); + Require(cold.DeepHits == 0 && cold.AfterDeepChecks.Contains("kernel replay") && + cold.AfterDeepChecks.Contains("theorem audit") && SameFacts(cold.Report), + "A first deep request in an empty cache did not build, replay and audit its modules"); + RequireDeepImports(verification.deepImports, "standalone"); + var unchanged = DeepRun(image, selector, proof, output, inputs, check: true); + Require(unchanged.DeepHits == 1 && unchanged.ProofHits == 1 && + unchanged.AfterDeepChecks.Contains("theorem audit") && + !unchanged.AfterDeepChecks.Contains("kernel replay") && SameFacts(unchanged.Report), + "An unchanged deep request did not reuse its result under a fresh theorem audit"); + Console.WriteLine("PASS an unchanged deep request reuses its replayed modules and runs the theorem audit"); + + var hitsBeforeImport = verification.deepCacheHits; + ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, proof, output, deepChecks: inputs, + stage: stage => + { + if (stage != "deep-check sources") return; + var fresh = Directory.EnumerateDirectories(output, "proof-*").Single(); + SameLengthChange(Path.Combine(fresh, "Extracted.olean")); + }), "Deep-check artifact changed"); + Require(verification.deepCacheHits == hitsBeforeImport, + "A deep result was restored over a changed imported module"); + RequireNoReport(); + Console.WriteLine("PASS a same-length imported module change with its write time restored is not restored"); + + var changedProof = Path.Combine(fixture.Root, "ChangedProof.lean"); + File.WriteAllText(changedProof, File.ReadAllText(proof) + "\n-- A changed proof input.\n"); + var reproved = DeepRun(image, selector, changedProof, output, inputs); + Require(reproved.DeepHits == 1 && reproved.ProofHits == 0 && + reproved.AfterDeepChecks.Contains("kernel replay") && + reproved.Output.Contains("\"auditFused\":false", StringComparison.Ordinal) && SameFacts(reproved.Report), + "A changed proof did not reuse the deep result through the joint replay of the proof"); + Console.WriteLine("PASS a changed proof is replayed jointly while its deep result is reused"); + + var entry = Directory.EnumerateDirectories(SuccessCacheRoot).Single(directory => + File.Exists(Path.Combine(directory, "files", "BindingAudit", "Bodies.olean"))); + var cachedBodies = Path.Combine(entry, "files", "BindingAudit", "Bodies.olean"); + var forged = Path.Combine(fixture.Root, "WrongBodies.olean"); + ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, proof, output, deepChecks: inputs, + stage: stage => + { + if (stage != "theorem audit" || File.Exists(forged)) return; + var fresh = Directory.EnumerateDirectories(output, "proof-*").Single(); + WriteWrongBodies(fresh); + File.Copy(Path.Combine(fresh, "BindingAudit", "Bodies.olean"), forged); + }), "Compiled artifacts changed"); + File.Copy(forged, cachedBodies, true); + var manifest = JsonSerializer.SerializeToUtf8Bytes(CacheFiles(Path.Combine(entry, "files"))); + File.WriteAllBytes(Path.Combine(entry, "receipt.json"), manifest); + File.WriteAllBytes(Path.Combine(entry, "receipt.hmac"), HMACSHA256.HashData(verification.ReceiptKey, + Encoding.UTF8.GetBytes(Path.GetFileName(entry)).Concat(manifest).ToArray())); + var deepHits = verification.deepCacheHits; + var audited = new DeepCheckRun(inputs); + ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, proof, output, deepChecks: inputs, + deepRun: audited), "Theorem statement mismatch"); + Require(verification.deepCacheHits == deepHits + 1 && audited.Attempt!.Failure("theorem audit", "rejected") + .facts.Single(fact => fact.id == "body-bytes").status == FailureReports.BindingFailureStatus.Failed, + "The theorem audit did not reject a restored body module that states another fact"); + RequireNoReport(); + Console.WriteLine("PASS the theorem audit rejects an authenticated restored module that states another fact"); + + SameLengthChange(cachedBodies); + var changedBodies = File.ReadAllBytes(cachedBodies); + var previousReplay = Environment.GetEnvironmentVariable("LEANCIL_PROFILE_CONSUMER_REPLAY"); + (Report Report, string[] AfterDeepChecks, string Output, int DeepHits, int ProofHits) rebuilt; + try + { + // The cold run takes the separate replay that follows a rejected joint replay. + Environment.SetEnvironmentVariable("LEANCIL_PROFILE_CONSUMER_REPLAY", "true"); + rebuilt = DeepRun(image, selector, proof, output, inputs); + } + finally { Environment.SetEnvironmentVariable("LEANCIL_PROFILE_CONSUMER_REPLAY", previousReplay); } + Require(rebuilt.DeepHits == 0 && rebuilt.AfterDeepChecks.Contains("kernel replay") && + rebuilt.AfterDeepChecks.Contains("theorem audit") && SameFacts(rebuilt.Report), + "A same-length change to a cached deep module with its timestamp restored was restored"); + Require(verification.AuthenticatedSuccess(Path.GetFileName(entry)) is not null && + !File.ReadAllBytes(cachedBodies).SequenceEqual(changedBodies), + "A rebuilt deep result did not replace the changed entry"); + Console.WriteLine("PASS a changed cached deep module runs a complete cold deep audit and is replaced"); + + var variant = fixture.Build("Variant", "DeepFixture", """ + public static class Input + { + public static ulong Value(ulong value) => value | 1; + public static ulong Other(ulong value) => value + 1; + } + """); + var variantDigests = Path.Combine(fixture.Root, "variant.sha256"); + File.WriteAllText(variantDigests, Sha(variant) + " " + Path.GetRelativePath(fixture.Root, variant) + "\n"); + var variantInputs = DeepCheckInputs.Read(variantDigests); + Require(DeepRun(variant, selector, proof, fixture.Artifact("Variant"), variantInputs).DeepHits == 0, + "A changed instruction in the library reused a deep result"); + Require(DeepRun(variant, "Input::Other(System.UInt64)", proof, fixture.Artifact("VariantOther"), + variantInputs).DeepHits == 0, "Another entry method of one image reused a deep result"); + Console.WriteLine("PASS a changed library instruction and another entry method of one image miss"); + } + finally { Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", previousCache); } +} + +// Runs one deep request with its stages from the deep checks on, its standard output and its cache hits. +(Report Report, string[] AfterDeepChecks, string Output, int DeepHits, int ProofHits) DeepRun(string assembly, + string entry, string proofPath, string outputPath, DeepCheckInputs digests, bool check = false) +{ + List stages = []; + var deepHits = verification.deepCacheHits; + var proofHits = verification.proofCacheHits; + var previousOutput = Console.Out; + using var captured = new StringWriter(); + Report result; + try + { + Console.SetOut(captured); + result = verification.Verify(assembly, "DeepFixture", entry, proofPath, outputPath, check: check, + deepChecks: digests, stage: stages.Add); + } + finally + { + Console.SetOut(previousOutput); + Console.Write(captured.ToString()); + } + return (result, [.. stages.SkipWhile(stage => stage != "deep checks")], captured.ToString(), + verification.deepCacheHits - deepHits, verification.proofCacheHits - proofHits); +} + +bool SameFacts(Report checkedReport) => checkedReport.binding.facts + .Select(fact => (fact.id, fact.status, fact.theorem)) + .SequenceEqual(report.binding.facts.Select(fact => (fact.id, fact.status, fact.theorem))); + +static void Require(bool condition, string message) +{ + if (!condition) throw new InvalidOperationException(message); +} + +// A run with a cold cache builds its deep modules; any other run restores those an earlier run published. +void Reject(string name, string marker, string callback, Action mutate, string? coldCache = null) +{ + bool changed = false; + var run = new DeepCheckRun(inputs); + var deepHits = verification.deepCacheHits; + var previousCache = Environment.GetEnvironmentVariable("LEANCIL_CACHE_DIR"); + try + { + if (coldCache is not null) Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", coldCache); + ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, proof, output, deepChecks: inputs, + deepRun: run, + stage: stage => + { + if (changed || stage != callback) return; + var fresh = Directory.EnumerateDirectories(output, "proof-*").SingleOrDefault(); + if (fresh is null || !File.Exists(Path.Combine(fresh, "BindingAudit", "Bodies.olean"))) return; + mutate(fresh); + changed = true; + }), marker); + } + finally { Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", previousCache); } + if (!changed) throw new InvalidOperationException("Mutation callback did not execute: " + name); + if (verification.deepCacheHits != deepHits + (coldCache is null ? 1 : 0)) + throw new InvalidOperationException("Mutation ran on the wrong deep result path: " + name); + RequireUnattributed(run, callback); + RequireNoReport(); + Console.WriteLine("PASS " + name + " prevents deep report publication"); +} + +// Replaces the body theorem with one about no evidence and compiles it over the generated module. +void WriteWrongBodies(string fresh) +{ + var source = Path.Combine(fresh, "BindingAudit", "Bodies.lean"); + var text = File.ReadAllText(source); + var start = text.IndexOf("theorem bodies :", StringComparison.Ordinal); + if (start < 0) throw new InvalidOperationException("Missing body theorem"); + File.WriteAllText(source, text[..start] + """ + theorem bodies : StrictBinding.BodiesBound images [] := StrictBinding.claimsHold_nil Bodies.prepared + #print axioms bodies + end BindingAudit + """); + Run([verification.LeanExecutable, "--root=" + fresh, "-o", Path.ChangeExtension(source, ".olean"), source], + leanPath: fresh + Path.PathSeparator + verification.SessionLeanPath(verification.session!)); +} + +void RequireUnattributed(DeepCheckRun run, string stage) +{ + var failed = run.Attempt?.Failure(stage, "rejected") + ?? throw new InvalidOperationException("Missing begun deep attempt"); + if (failed.facts.Any(fact => fact.status != FailureReports.BindingFailureStatus.NotCovered) || + failed.images.Any(item => item.status != FailureReports.BindingFailureStatus.NotCovered)) + throw new InvalidOperationException("Unclassified failure attributed a binding rejection"); +} + +void CheckFailureDiagnostics() +{ + using Program privateChecker = new(); + if (args is not ["--diagnostic", var selectedCase] || + selectedCase is "original-library" or "originals" or "replay-library" or "replay-auditor" or + "kernel" or "kernel-remaining") + { + privateChecker.Library(Inputs(proof), writes: true); + if (!privateChecker.ownsLibrary || privateChecker.libraryStamps is not null) + throw new InvalidOperationException("Expected an owned private library for the integrity regression"); + } + var priorCache = Environment.GetEnvironmentVariable("LEANCIL_CACHE_DIR"); + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", Path.Combine(fixture.Root, "DiagnosticsCache")); + try + { + if (args is not ["--diagnostic", _]) + { + var selected = new DeepCheckRun(inputs); + ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, proof, output, + theorem: "bad theorem", deepChecks: inputs, deepRun: selected), "Invalid theorem name"); + if (selected.Attempt is not null) + throw new InvalidOperationException("Selection failure began an attempt"); + foreach (var early in new[] { "Lean library build", "proof checking", "kernel replay" }) + { + File.AppendAllText(proof, "\n-- diagnostics " + Guid.NewGuid().ToString("N") + "\n"); + var run = new DeepCheckRun(inputs); + ExpectFailure(() => verification.Verify(image, "DeepFixture", selector, proof, output, + deepChecks: inputs, deepRun: run, stage: stage => + { + if (stage == early) + { + if (run.Attempt is null) throw new InvalidOperationException("Missing early attempt"); + throw new InvalidOperationException("diagnostic resource failure"); + } + }), "diagnostic resource failure"); + RequireUnattributed(run, early); + RequireNoReport(); + } + Console.WriteLine("PASS selection leaves no attempt and library/proof/replay failures stay not covered"); + } + + foreach (var target in new[] { "body-type", "body-axiom", "digest-type", "images-type", + "body-kernel", "digest-kernel", "images-kernel", "rows-page-kernel", + "replay-proof", "replay-identity", "replay-library", "replay-assembly", "replay-auditor", + "original-proof", "original-identity", "original-library", "original-assembly" }) + { + if (args is ["--diagnostic", var only] && target != only && + !((only == "kernel" || only == "kernel-remaining" && target != "body-kernel") && + (target.EndsWith("-kernel", StringComparison.Ordinal) || + target.StartsWith("replay-", StringComparison.Ordinal))) && + !(only == "originals" && target.StartsWith("original-", StringComparison.Ordinal))) continue; + bool kernelForgery = target.EndsWith("-kernel", StringComparison.Ordinal) || + target.StartsWith("replay-", StringComparison.Ordinal); + var checker = target is "original-library" or "replay-library" or "replay-auditor" + ? privateChecker : verification; + var originalPath = target switch + { + "original-proof" or "replay-proof" => proof, + "original-identity" or "replay-identity" => Path.Combine(output, "identity.json"), + "original-library" or "replay-library" => Path.Combine(checker.library!, "CIL.lean"), + "original-assembly" or "replay-assembly" => image, + "replay-auditor" => checker.CheckedAuditExecutable(), + _ => null + }; + var originalBytes = originalPath is null ? null : File.ReadAllBytes(originalPath); + var originalTime = originalPath is null ? default : File.GetLastWriteTimeUtc(originalPath); + var run = new DeepCheckRun(inputs); + bool changed = false; + bool originalChanged = false; + string lastStage = ""; + string[]? before = null; + var diagnostic = target switch + { + "body-kernel" or "digest-kernel" or "images-kernel" or "rows-page-kernel" => "while replaying", + "body-axiom" => "Unapproved proof axioms", + "original-proof" or "replay-proof" => "Proof inputs changed", + "original-identity" or "replay-identity" => "extraction identity changed", + "original-library" or "replay-library" or "replay-auditor" => "certified library contents changed", + "original-assembly" or "replay-assembly" => "assembly has changed since extraction", + _ => "Theorem statement mismatch" + }; + try + { + ExpectFailure(() => checker.Verify(image, "DeepFixture", selector, proof, output, deepChecks: inputs, + deepRun: run, stage: stage => + { + lastStage = stage; + if (stage == (kernelForgery ? "replay diagnostics" : "theorem audit") && + originalPath is not null && !originalChanged) + { + var bytes = (byte[])originalBytes!.Clone(); + const int dosStubOffset = 0x50; + var offset = target is "original-assembly" or "replay-assembly" ? dosStubOffset : 0; + if (target is "original-assembly" or "replay-assembly" && + (bytes.Length <= dosStubOffset || BitConverter.ToInt32(bytes, 0x3c) <= dosStubOffset)) + throw new InvalidOperationException("Fixture has no DOS stub byte before the PE header"); + bytes[offset] ^= 1; + File.WriteAllBytes(originalPath, bytes); + File.SetLastWriteTimeUtc(originalPath, originalTime); + originalChanged = true; + } + if (stage != "deep-check sources") return; + var fresh = Directory.EnumerateDirectories(output, "proof-*").Single(); + if (target == "rows-page-kernel") + { + var rows = ModuleSource(fresh, MetadataRowsModule); + File.WriteAllText(rows, ForgedRowPage(File.ReadAllText(rows))); + changed = true; + before = Receipts(); + return; + } + var module = target is "digest-type" or "digest-kernel" ? "I" + Sha(image) + : target is "images-type" or "images-kernel" ? "Images" : "Bodies"; + var path = Path.Combine(fresh, "BindingAudit", module + ".lean"); + var source = File.ReadAllText(path); + var theorem = target is "digest-type" or "digest-kernel" ? "digest" + : target is "images-type" or "images-kernel" ? "images_eq" : "bodies"; + var start = source.IndexOf("theorem " + theorem + " :", StringComparison.Ordinal); + if (start < 0) throw new InvalidOperationException("Missing generated theorem"); + var ns = target is "digest-type" or "digest-kernel" ? "BindingAudit." + module : "BindingAudit"; + string replacement = target == "body-axiom" + ? "axiom cheat : StrictBinding.BodiesBound images Extracted.methodEvidence\n" + + "theorem bodies : StrictBinding.BodiesBound images Extracted.methodEvidence := cheat\n" + : "theorem " + theorem + " : True := True.intro\n"; + if (kernelForgery) + { + var documentation = source.LastIndexOf("/--", start, StringComparison.Ordinal); + if (documentation >= 0) start = documentation; + replacement = $$""" + set_option debug.skipKernelTC true in + run_cmd Lean.Elab.Command.liftCoreM <| Lean.addDecl (.thmDecl { + name := `{{ns}}.{{theorem}} + levelParams := [] + type := Lean.mkConst ``False + value := Lean.mkConst ``True.intro + }) + + """; + } + File.WriteAllText(path, (kernelForgery ? "import Lean\n" : "") + source[..start] + + replacement + "#print axioms " + theorem + + "\nend " + ns + "\n"); + changed = true; + before = Receipts(); + }), diagnostic); + } + finally + { + if (originalPath is not null) + { + File.WriteAllBytes(originalPath, originalBytes!); + File.SetLastWriteTimeUtc(originalPath, originalTime); + } + } + var failed = run.Attempt!.Failure("theorem audit", diagnostic); + var failedFacts = failed.facts + .Where(fact => fact.status == FailureReports.BindingFailureStatus.Failed) + .Select(fact => fact.id).ToArray(); + var failedImages = failed.images.Where(item => item.status == FailureReports.BindingFailureStatus.Failed) + .Select(item => item.sha256).ToArray(); + string[] expectedFacts = target.StartsWith("body-", StringComparison.Ordinal) ? ["body-bytes"] + : target.StartsWith("rows-", StringComparison.Ordinal) ? ["metadata-rows"] : []; + if (!changed || before is null || !before.SequenceEqual(Receipts()) || + !failedFacts.SequenceEqual(expectedFacts) || + !failedImages.SequenceEqual(target is "digest-type" or "digest-kernel" ? [Sha(image)] : [])) + throw new InvalidOperationException("Wrong named deep rejection attribution: " + target + + "; changed=" + changed + "; facts=" + string.Join(",", failedFacts) + + "; images=" + string.Join(",", failedImages) + "; receipts added=" + + string.Join(",", Receipts().Except(before ?? [])) + "; receipts removed=" + + string.Join(",", (before ?? []).Except(Receipts()))); + if (originalPath is not null) + { + if (!originalChanged) throw new InvalidOperationException("Original input mutation did not run"); + RequireUnattributed(run, lastStage); + } + else if (lastStage != (target.EndsWith("-kernel", StringComparison.Ordinal) + ? "replay diagnostics" : "theorem audit")) + throw new InvalidOperationException("Named rejection lost the theorem audit stage: " + lastStage); + RequireNoReport(); + Console.WriteLine(originalPath is null + ? "PASS named " + target + " rejection marks exactly its subject without publication" + : "PASS " + target + " integrity failure leaves all subjects not covered without publication"); + } + } + finally { Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", priorCache); } +} + +// Row page 0 keeps its statement and takes a proof of True, which only kernel replay rejects. +static string ForgedRowPage(string source) => "import Lean\n" + source + .Replace("theorem page0 :", "theorem page0_valid :", StringComparison.Ordinal) + .Replace("end MetadataRows", """ + set_option debug.skipKernelTC true in + run_cmd Lean.Elab.Command.liftCoreM do + let valid <- Lean.getConstInfo `BindingAudit.MetadataRows.page0_valid + Lean.addDecl (.thmDecl { + name := `BindingAudit.MetadataRows.page0, levelParams := [], type := valid.type, + value := Lean.mkConst ``True.intro }) + + end MetadataRows + """, StringComparison.Ordinal); + +void RequireNoReport() +{ + if (File.Exists(Path.Combine(output, "verified.json"))) + throw new InvalidOperationException("Rejected deep verification left a successful report"); +} + +void ExpectFailure(Action action, string marker) +{ + try { action(); } + catch (Exception error) when (error.Message.Contains(marker, StringComparison.Ordinal)) { return; } + throw new InvalidOperationException("Expected rejection: " + marker); +} diff --git a/Tests/DerivedContractExports.cs b/Tests/DerivedContractExports.cs index 5d12f9f94..3a773a0de 100644 --- a/Tests/DerivedContractExports.cs +++ b/Tests/DerivedContractExports.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // A library proves exact internal contracts against its actual extraction and exports weaker public // contracts derived only by Holds.weaken; separately compiled callers verify from the sidecar alone. @@ -16,7 +21,7 @@ foreach (string directory in new[] { libRoot, appRoot, package, callerPackage }) Directory.CreateDirectory(directory); string[] cli = ["dotnet", ExtractorPath]; string Project(string assembly, string references) => - "net10.0" + + "net11.0" + assembly + "true" + references + ""; File.WriteAllText(Path.Combine(libRoot, "Lib.csproj"), Project("DerivedFixture.Lib", "")); File.WriteAllText(Path.Combine(appRoot, "App.csproj"), Project("DerivedFixture.App", @@ -138,8 +143,8 @@ string Manifest(string name, string? onlyProof = null) // stands for all four kinds; each proof module's Rejected section refutes every kind on the actual extraction. string certified = Sha(sidecar); string publicText = File.ReadAllText(interfaceSource); -string renamed = publicText.Replace("else some \"System.ArgumentOutOfRangeException\"", - "else some \"System.ArgumentException\"", StringComparison.Ordinal); +string renamed = publicText.Replace("else some .argumentOutOfRange", + "else some .argument", StringComparison.Ordinal); if (renamed == publicText) throw new InvalidOperationException("Precondition: the public contract must name its exception."); File.WriteAllText(interfaceSource, renamed); diff --git a/Tests/Directory.Build.targets b/Tests/Directory.Build.targets new file mode 100644 index 000000000..808fb0b0c --- /dev/null +++ b/Tests/Directory.Build.targets @@ -0,0 +1,5 @@ + + + + + diff --git a/Tests/DirectoryLinks.cs b/Tests/DirectoryLinks.cs new file mode 100644 index 000000000..ed2d639c9 --- /dev/null +++ b/Tests/DirectoryLinks.cs @@ -0,0 +1,23 @@ +partial class Program +{ + /// + /// Makes `link` a directory link to `target`, which need not exist: a symbolic link, or on Windows without + /// that privilege a junction. False when the platform can make neither. + /// + static bool TryLinkDirectory(string link, string target) + { + try { Directory.CreateSymbolicLink(link, target); return true; } + catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } + if (!OperatingSystem.IsWindows()) return false; + // A junction needs no privilege. + using var mklink = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("cmd.exe", + $"/c mklink /J \"{link}\" \"{target}\"") + { + CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false + })!; + mklink.StandardOutput.ReadToEnd(); + mklink.StandardError.ReadToEnd(); + mklink.WaitForExit(); + return mklink.ExitCode == 0 && new DirectoryInfo(link).LinkTarget is not null; + } +} diff --git a/Tests/DispatchEvidence.cs b/Tests/DispatchEvidence.cs index 75e4c1034..c0f249c91 100644 --- a/Tests/DispatchEvidence.cs +++ b/Tests/DispatchEvidence.cs @@ -1,13 +1,19 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs if (args is not ([] or ["--prepare-only"])) throw new ArgumentException("Usage: DispatchEvidence.cs [--prepare-only]"); using var fixtures = new Program(); -fixtures.PrepareLeanFixtures("Tests.ConstrainedEvidence", "Tests.ConstantCalls", "Tests.NativeMethodTables", - "Tests.StackAllocation", "Tests.StackAllocationEvidence"); +fixtures.PrepareLeanFixtures("Tests.ConstrainedEvidence", "Tests.MetadataBuilder", + "Tests.BodyEncoding", "Tests.ConstantCalls", + "Tests.NativeMethodTables", "Tests.StackAllocation", "Tests.StackAllocationEvidence"); if (args is ["--prepare-only"]) return; Run(["dotnet", "run", "--file", "Tests/ConstrainedResolution.cs"], streamOutput: true, progress: "ConstrainedResolution"); diff --git a/Tests/DispatchScaling.lean b/Tests/DispatchScaling.lean index e49251eb8..759d31b7d 100644 --- a/Tests/DispatchScaling.lean +++ b/Tests/DispatchScaling.lean @@ -30,9 +30,12 @@ def types (count : Nat) : List DispatchType := interfaces := [], methods := [slot], assembly := pin}] /-- Call site selecting the shared interface slot. -/ def site : CallSite := { - declaringType := iface, name := "Read", target := .managed 5, - typeArguments := [], parameterCount := 0, hasThis := true, returnsValue := false, - returnType := .void, parameterTypes := [] } + declared := { convention := 0x20 }, + method := { + owner := iface, name := name!"Read", hasThis := true, genericArity := 0, returnType := .void, + parameters := [] }, + name := "Read", target := .managed 5, typeArguments := [], parameterCount := 0, + returnsValue := false } /-- Resolve one call per concrete type using the list specification. -/ def baseline (count : Nat) : Bool := let catalog := types count diff --git a/Tests/Division.cs b/Tests/Division.cs index 27a6d6dea..2694a186c 100644 --- a/Tests/Division.cs +++ b/Tests/Division.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Numerics; diff --git a/Tests/DocSnippets.cs b/Tests/DocSnippets.cs index 5c410073e..3a7b89d0c 100644 --- a/Tests/DocSnippets.cs +++ b/Tests/DocSnippets.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/Documentation.cs using System.Diagnostics; @@ -59,7 +64,7 @@ import CIL open CIL.Safety open CIL (W64 Method) - @[cil_code] def identity : Method := { code := [.arg 0, .ret], locals := [], returnsValue := true } + @[cil_code] def identity : Method := { code := [.ldarg 0, .ret], locals := [], returnsValue := true } """; var document = $$""" # Stale diff --git a/Tests/EmptyReturnVariants.cs b/Tests/EmptyReturnVariants.cs new file mode 100644 index 000000000..051febc93 --- /dev/null +++ b/Tests/EmptyReturnVariants.cs @@ -0,0 +1,56 @@ +partial class Program +{ + const string ProjectedReturn = """ + /-- Projecting the result values of a successful computation retains a final memory witness. -/ + private theorem fromEmpty {outcome : Except LocatedFault (CIL.Safety.Memory × List CIL.Safety.Value)} + {values : List CIL.Safety.Value} (observed : outcome.map Prod.snd = .ok values) : + ∃ final, outcome = .ok (final, values) ∧ Confined [] Memory.empty final := by + cases outcome with + | error fault => cases observed + | ok pair => + obtain ⟨final, actual⟩ := pair + cases Except.ok.inj observed + exact ⟨final, rfl, by constructor <;> simp [Memory.empty]⟩ + #print axioms fromEmpty + """; + + /// A universal result from empty memory and one input where a variant returns another value. + sealed record EmptyReturn(string Input, string Arguments, string Expected, int Fuel, string FromExample, + string Witness, string Requires = "True"); + + void VerifyEmptyReturn(string assembly, string selector, string example, string wrongAssembly, + string wrongSelector, string wrongOutput, EmptyReturn spec) + { + var contract = $$""" + /-- The arguments, permitted inputs and result of the universal invocation equation. -/ + def contract : MethodContract ({{spec.Input}}) where + args input := {{spec.Arguments}} + pre input memory := memory = Memory.empty ∧ {{spec.Requires}} + footprint _ := [] + post input _ _ values := values = [{{spec.Expected}}] + """; + var positive = $$""" + {{ProjectedReturn}} + /-- The example's fixed-fuel equation supplies the return required by the contract. -/ + theorem correctness : contract.Holds Extracted.program 0 := by + rintro input _ ⟨rfl, permitted⟩ + have answer : (invoke Extracted.program {{spec.Fuel}} 0 (contract.args input) + Memory.empty).map Prod.snd = .ok [{{spec.Expected}}] := by + {{spec.FromExample.ReplaceLineEndings("\n").Replace("\n", "\n ")}} + obtain ⟨final, ran, framed⟩ := fromEmpty answer + exact ⟨{{spec.Fuel}}, (final, [{{spec.Expected}}]), ran, framed, _, rfl, rfl⟩ + """; + var counterexample = $$""" + /-- The extracted variant returns a different value at the witness. -/ + theorem correctness : contract.Refuted Extracted.program 0 := + .of_return (input := {{spec.Witness}}) (initial := Memory.empty) ⟨rfl, by decide⟩ + (rfl : invoke Extracted.program 64 0 (contract.args {{spec.Witness}}) Memory.empty = .ok _) + (by rintro ⟨-, _, rfl, wrong⟩ + dsimp only [contract] at wrong + revert wrong + decide) + """; + VerifyContractVariant(assembly, "Examples", selector, Path.Combine(wrongOutput, "Positive"), wrongAssembly, + wrongSelector, wrongOutput, contract, positive, counterexample, example: example); + } +} diff --git a/Tests/EnumBufferEvidence.lean b/Tests/EnumBufferEvidence.lean index 850e5ba7f..42d7156b9 100644 --- a/Tests/EnumBufferEvidence.lean +++ b/Tests/EnumBufferEvidence.lean @@ -31,6 +31,13 @@ theorem enum_storage_checked : some (.enumeration "Fixture::Choice" storage)) = true := by decide +kernel #print axioms enum_storage_checked +/-- An Int32 enum widens to a word, while an enum over an Int32 byref has no scalar kind. -/ +theorem enumScalarNeedsInteger : + let int32 := TypeExpr.atom runtimeScope "System.Int32" + (scalarType (.enum "E" (.byref int32)), scalarType (.enum "E" int32)) = + (.other, .word32) := by decide +kernel +#print axioms enumScalarNeedsInteger + /-- Exact enum identity survives local stores and returns instead of becoming only a width. -/ theorem span_identity_checked : scalarTransfer method ⟨0, 10, .none⟩ [scalarType (span "First")] = some [] ∧ @@ -43,9 +50,12 @@ theorem span_identity_checked : /-- A method call cannot silently reinterpret one enum span as another. -/ theorem span_call_identity_checked : let call : RawInstruction := ⟨0, 40, .call { - declaringType := .atom "Fixture" "Helpers", name := "Use", target := .managed 2, - typeArguments := [], parameterCount := 1, hasThis := false, returnsValue := false, - returnType := .void, parameterTypes := [span "First"] }⟩ + declared := { convention := 0, parameters := [span "First"] }, + method := { + owner := .atom "Fixture" "Helpers", name := name!"Use", hasThis := false, + genericArity := 0, returnType := .void, parameters := [span "First"] }, + name := "Use", target := .managed 2, typeArguments := [], parameterCount := 1, + returnsValue := false }⟩ scalarTransfer method call [scalarType (span "First")] = some [] ∧ scalarTransfer method call [scalarType (span "Second")] = none := by decide +kernel #print axioms span_call_identity_checked @@ -61,13 +71,16 @@ theorem array_identity_checked : /-- Pointer-based in-place construction keeps the enum receiver's exact metadata identity. -/ theorem enum_local_constructor : let constructor : CallSite := { - declaringType := span "First", name := ".ctor" + declared := { + convention := 0x20, parameters := [.pointer .void, .atom runtimeScope "System.Int32"] }, + method := { + owner := span "First", name := name!".ctor", hasThis := true, genericArity := 0, + returnType := .void, parameters := [.pointer .void, .atom runtimeScope "System.Int32"] }, + name := ".ctor", target := .external (runtimeScope ++ - "::System.Void System.Span`1::.ctor(System.Void*,System.Int32)") - typeArguments := [], parameterCount := 2, hasThis := true, returnsValue := false - returnType := .void - parameterTypes := [.pointer .void, .atom runtimeScope "System.Int32"] } - spanLocalConstructor constructor = some (.reference 1 true) ∧ + "::System.Void System.Span`1::.ctor(System.Void*,System.Int32)"), + typeArguments := [], parameterCount := 2, returnsValue := false } + runtimeModel {} .call constructor.method = some (.spanInPlace (.reference 1 true)) ∧ scalarTransfer method ⟨0, 40, .call constructor⟩ [.word32, .reference, scalarType (.byref (span "First"))] = some [] ∧ scalarTransfer method ⟨0, 40, .call constructor⟩ diff --git a/Tests/EnumBuffers.cs b/Tests/EnumBuffers.cs index d54bc1b7e..00833201a 100644 --- a/Tests/EnumBuffers.cs +++ b/Tests/EnumBuffers.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs diff --git a/Tests/EnumMetadata.cs b/Tests/EnumMetadata.cs index 454979e4c..09cc867a6 100644 --- a/Tests/EnumMetadata.cs +++ b/Tests/EnumMetadata.cs @@ -2,9 +2,16 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include PinnedMetadata.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Reflection; using Mono.Cecil; +using Mono.Cecil.Cil; var extractor = Assembly.LoadFrom(ExtractorPath); const BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic; @@ -13,18 +20,22 @@ var arrays = extractor.GetType("Nethermind.LeanCIL.Arrays", true)!; var descriptor = arrays.GetMethod("Storage", flags)!; var runtime = extractor.GetType("Nethermind.LeanCIL.RuntimeModels", true)!; -var span = runtime.GetMethod("ElementSpan", flags)!; -var spanOperations = runtime.GetMethod("BuildSpanOperations", flags)!; -using var module = ModuleDefinition.ReadModule(At("Examples/Assembly/bin/Examples.dll")); +var spans = extractor.GetType("Nethermind.LeanCIL.Spans", true)!; +var memberWidth = spans.GetMethod("MemberWidth", flags)!; +var arrayView = spans.GetMethod("ArrayView", flags)!; +var staticSpan = runtime.GetMethod("StaticSpanModel", flags)!; +using var pinned = new PinnedMetadata(extractor, At("Examples/Assembly/bin/Examples.dll")); +var module = pinned.Module; var owner = module.GetType("Examples.EnumBuffers"); var element = owner.NestedTypes.Single(type => type.Name == "Small"); var field = element.Fields.Single(field => !field.IsStatic); -var getter = (MethodReference)owner.Methods.Single(method => method.Name == "Read") - .Body.Instructions.Single(instruction => instruction.Operand is MethodReference { Name: "get_Item" }).Operand; +var getter = owner.Methods.Single(method => method.Name == "Read") + .Body.Instructions.Single(instruction => instruction.Operand is MethodReference { Name: "get_Item" }); IDisposable Scope() => (IDisposable)begin.Invoke(null, null)!; object Describe() => descriptor.Invoke(null, [element, null])!; -string Span() => (string)span.Invoke(null, [getter, "scalar"])!; +int ItemWidth() => (int?)memberWidth.Invoke(null, [getter, "get_Item", 1, "scalar"]) + ?? throw new InvalidOperationException("Missing span item at the getter call"); int Width(object value) => (int)value.GetType().GetProperty("Width")!.GetValue(value)!; void Reject(string reason) { @@ -45,20 +56,31 @@ void Reject(string reason) for (var index = 0; index < 1000; index++) if (!ReferenceEquals(original, Describe())) throw new InvalidOperationException("Enum descriptor was not reused within a request"); - if (Width(original) != 1 || Span() != ".memory (.spanItem 1)") + if (Width(original) != 1 || ItemWidth() != 1) throw new InvalidOperationException("Wrong initial enum storage"); var aggregate = module.GetType("Examples.AggregateArrays").NestedTypes.Single(type => type.Name == "Pair"); var vector = ((ArrayType)module.GetType("Examples.AggregateArrays").Methods .Single(method => method.Name == "ReadVector").Parameters[0].ParameterType).ElementType; + var words = module.GetType("Examples.SpanTables").Methods.Single(method => method.Name == "get_Words"); + var create = (GenericInstanceMethod)words.Body.Instructions.Single(instruction => + instruction.Operand is GenericInstanceMethod).Operand; + var arrayMethod = module.GetType("Examples.MemorySpans").Methods + .Single(method => method.Name == "ArrayConstructor"); + var arrayConstructor = arrayMethod.Body.Instructions.Select(instruction => instruction.Operand) + .OfType().Single(reference => reference.Name == ".ctor"); foreach (var (type, admitted) in new[] { (module.TypeSystem.Int32, true), ((TypeReference)element, true), (aggregate, false), (vector, false) }) { - var operations = (Dictionary)spanOperations.Invoke(null, [type, "scalar"])!; - var createsStaticSpan = operations.Keys.Any(name => - name.Contains("System.Runtime.CompilerServices.RuntimeHelpers::CreateSpan<", StringComparison.Ordinal)); - if (createsStaticSpan != admitted || - !operations.Values.Any(operation => operation.StartsWith(".memory (.arraySpan ", StringComparison.Ordinal))) + var spanOwner = new GenericInstanceType(((GenericInstanceType)arrayConstructor.DeclaringType).ElementType); + spanOwner.GenericArguments.Add(type); + var arrayCall = new MethodReference(".ctor", module.TypeSystem.Void, spanOwner) { HasThis = true }; + arrayCall.Parameters.Add(new ParameterDefinition(arrayConstructor.Parameters[0].ParameterType)); + var call = new GenericInstanceMethod(create.ElementMethod); + call.GenericArguments.Add(type); + var createsStaticSpan = staticSpan.Invoke(null, [call, "scalar"]) is not null; + var createsArraySpan = arrayView.Invoke(null, [Instruction.Create(OpCodes.Newobj, arrayCall), "scalar", true]) is not null; + if (createsStaticSpan != admitted || !createsArraySpan) throw new InvalidOperationException("Static and supplied array span admission were conflated: " + type.FullName); } } @@ -66,7 +88,7 @@ void Reject(string reason) using (Scope()) { var changed = Describe(); - if (ReferenceEquals(original, changed) || Width(changed) != 4 || Span() != ".memory (.spanItem 4)") + if (ReferenceEquals(original, changed) || Width(changed) != 4 || ItemWidth() != 4) throw new InvalidOperationException("New request reused an old enum width or span signature table"); } field.FieldType = module.TypeSystem.Boolean; diff --git a/Tests/Enums.cs b/Tests/Enums.cs index 9dc82ef86..15dc79f51 100644 --- a/Tests/Enums.cs +++ b/Tests/Enums.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; diff --git a/Tests/EnvironmentLimit.cs b/Tests/EnvironmentLimit.cs index 8994f17ec..aca76e407 100644 --- a/Tests/EnvironmentLimit.cs +++ b/Tests/EnvironmentLimit.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Diagnostics; using System.Globalization; diff --git a/Tests/ExactIntrinsics.cs b/Tests/ExactIntrinsics.cs index 278c412cc..c0e28b569 100644 --- a/Tests/ExactIntrinsics.cs +++ b/Tests/ExactIntrinsics.cs @@ -2,9 +2,17 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs +#:include ModelOperandChecks.cs +#:include HardwareExamples.cs +#:include ContractVariants.cs using System.Buffers.Binary; using System.Reflection; @@ -12,15 +20,59 @@ using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; using Mono.Cecil; +using Mono.Cecil.Cil; using X86Aes = System.Runtime.Intrinsics.X86.Aes; using Program verification = new(); +Run(["lake", "build", "Tests.ExactIntrinsics", "Tests.HalfSwapContract"], verification.CheckoutPackage()); string assembly = At("Examples/Assembly/bin/Examples.dll"); using ModuleDefinition metadata = ModuleDefinition.ReadModule(assembly); -Dictionary selectors = metadata.GetType("Examples.DivisionIntrinsics").Methods - .ToDictionary(method => method.Name, method => method.FullName); +var divisionMethods = metadata.GetType("Examples.DivisionIntrinsics").Methods.ToDictionary(method => method.Name); +var selectors = divisionMethods.ToDictionary(pair => pair.Key, pair => pair.Value.FullName); Type runtime = Assembly.LoadFile(assembly).GetType("Examples.DivisionIntrinsics")!; string root = At("artifacts/DivisionIntrinsics"); +verification.EnsureExtractorBuilt(); +var profileType = Assembly.Load(File.ReadAllBytes(ExtractorPath)) + .GetType("Nethermind.LeanCIL.FeatureProfile", throwOnError: true)!; +var selectProfile = profileType.GetMethod("Select", BindingFlags.Static | BindingFlags.NonPublic)!; +string DeclareAes(string name, string flag) +{ + var profile = JsonSerializer.SerializeToNode(selectProfile.Invoke(null, [name]), + new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })!; + profile["name"] = $"{name}-{flag}"; + profile[flag] = true; + var path = Path.Combine(root, $"{name}-{flag}.json"); + File.WriteAllText(path, profile.ToJsonString()); + return $"@{path}"; +} +var hardwareRoot = Path.Combine(root, "HardwareMembers"); +Directory.CreateDirectory(hardwareRoot); +var hardwareImage = Path.Combine(hardwareRoot, "HardwareMembers.dll"); +var hardwareExamples = HardwareExamples.Write(assembly, hardwareImage, + DeclareAes("x64-v4", "aes"), DeclareAes("arm64-advsimd", "armAes")); +ExtractBatch("extract", "Examples", hardwareExamples.Select(example => + new GeneratedExtraction(hardwareImage, example.Selector, + Path.Combine(hardwareRoot, example.Name), example.Profile))); +using (var hardware = ModuleDefinition.ReadModule(hardwareImage)) +{ + var members = hardware.GetType("Examples.HardwareMembers").Methods.ToDictionary(method => method.Name); + // Each evidence batch keeps the kernel's memory bounded across the independent wrappers. + foreach (var group in hardwareExamples.Chunk(8)) + { + var evidence = new GeneratedEvidence(Path.Combine(hardwareRoot, $"Batch-{group[0].Name}")); + foreach (var example in group) + { + var folder = Path.Combine(hardwareRoot, example.Name); + var proof = Path.Combine(folder, "Operand.lean"); + File.Copy(Path.Combine(folder, "Extracted.lean"), proof, true); + ModelOperandChecks.Append(proof, members[example.Name], _ => example.Model, example.NeedsFeature); + evidence.Add(proof, $"PASS checked hardware MemberRef in {example.Selector}"); + } + evidence.Check(replay: false); + } +} + + var pending = new List<(GeneratedVerification Request, string Profile, Action Passed)>(); void Flush() { @@ -34,7 +86,7 @@ void Flush() foreach (var name in new[] { "DefinedLocal", "PrunedUnsafeLocal" }) { - var method = metadata.GetType("Examples.DivisionIntrinsics").Methods.Single(method => method.Name == name); + var method = divisionMethods[name]; var local = method.Body.Variables.First(variable => variable.VariableType.FullName == "System.Runtime.Intrinsics.Vector128`1").Index; var output = Path.Combine(root, name); @@ -130,14 +182,18 @@ end ExampleProof "ShiftRight" => ShiftProof("shr64Count", 3, "shiftRight"), _ => "\n apply ok_of_toOption\n decide +kernel" }; - string BroadcastProof(string operation, int lane, string lemma) => - $"exact congrArg (Except.map Prod.snd) (CIL.Safety.invoke_broadcastBinary_word " + - $"Extracted.profile .{operation} {lane} {left} {right} {expected} CIL.Safety.Memory.empty " + - $"rfl (by decide) (CIL.Safety.checked_broadcast_{lemma} {left} {right}))"; - string ShiftProof(string operation, int lane, string lemma) => - $"exact congrArg (Except.map Prod.snd) (CIL.Safety.invoke_broadcastShift " + - $"Extracted.profile .{operation} {lane} {left} {right} {expected} CIL.Safety.Memory.empty " + - $"rfl (by decide) (CIL.Safety.checked_broadcast_{lemma} {left} {right}))"; + string BroadcastProof(string operation, int lane, string lemma) => $$""" + exact congrArg (Except.map Prod.snd) (CIL.Safety.invoke_broadcastBinary_word + Extracted.profile .{{operation}} {{lane}} {{left}} {{right}} {{expected}} CIL.Safety.Memory.empty + rfl rfl rfl Extracted.method0_model4 rfl (by decide) + (CIL.Safety.checked_broadcast_{{lemma}} {{left}} {{right}})) + """; + string ShiftProof(string operation, int lane, string lemma) => $$""" + exact congrArg (Except.map Prod.snd) (CIL.Safety.invoke_broadcastShift + Extracted.profile .{{operation}} {{lane}} {{left}} {{right}} {{expected}} CIL.Safety.Memory.empty + rfl rfl rfl Extracted.method0_model6 rfl (by decide) + (CIL.Safety.checked_broadcast_{{lemma}} {{left}} {{right}})) + """; File.WriteAllText(proof, "import Extracted\nimport Tests.Support\n" + "import CIL.Safety.IntrinsicExecution\nnamespace ExampleProof\n" + (wide ? "private theorem ok_of_toOption {error value : Type} {result : Except error value} {expected : value}\n" + @@ -161,6 +217,203 @@ string ShiftProof(string operation, int lane, string lemma) => Flush(); +// Change only the AVX2 member reference in the compiled AND entry, keeping its broadcast and lane. +var wrongAnd = Path.Combine(root, "OrInsteadOfAnd"); +Directory.CreateDirectory(wrongAnd); +var wrongAndAssembly = Path.Combine(wrongAnd, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) +{ + var methods = variant.GetType("Examples.DivisionIntrinsics").Methods; + var andMethod = methods.Single(method => method.Name == "And"); + var orMethod = methods.Single(method => method.Name == "Or"); + static bool IsAvx2Member(object? operand) => operand is MethodReference target && + target.DeclaringType.FullName == "System.Runtime.Intrinsics.X86.Avx2"; + andMethod.Body.Instructions.Single(site => IsAvx2Member(site.Operand)).Operand = + orMethod.Body.Instructions.Single(site => IsAvx2Member(site.Operand)).Operand; + variant.Write(wrongAndAssembly); +} +verification.VerifyContractVariant(assembly, "Examples", selectors["And"], Path.Combine(root, "AndContract"), + wrongAndAssembly, selectors["And"], wrongAnd, """ + /-- Every pair of words returns its bitwise intersection and preserves caller memory. -/ + def intersection : MethodContract (W64 × W64) W64 where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post input before after value := after = before ∧ value = (input.1 &&& input.2) + """, """ + /-- The selected lane of the compiled AVX2 broadcast returns the intersection. -/ + theorem correctness : intersection.Holds Extracted.program 0 := by + rintro ⟨left, right⟩ memory - + apply MethodContract.Holds.intro (c := intersection) (input := (left, right)) + memory (left &&& right) ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + exact ⟨256, invoke_broadcastBinary_word Extracted.profile .band 0 left right _ memory + rfl rfl rfl Extracted.method0_model4 rfl (by decide) (checked_broadcast_and left right)⟩ + """, """ + /-- On disjoint words one and two the replacement returns three. The intersection is zero. -/ + theorem correctness : intersection.Refuted Extracted.program 0 := + .of_return (input := (1, 2)) (initial := Memory.empty) trivial + (invoke_broadcastBinary_word Extracted.profile .bor 0 1 2 3 Memory.empty + rfl rfl rfl Extracted.method0_model4 rfl (by decide) (checked_broadcast_or 1 2)) + (by rintro ⟨-, value, encoded, -, rfl⟩; revert encoded; decide) + """, profile: "x64-v3", imports: "import CIL.Safety.IntrinsicExecution"); +Console.WriteLine("PASS AVX2 AND contract and certified OR replacement at (1, 2)"); + +foreach (var (name, width, zero, highBit) in new (string, int, object, object)[] +{ + ("EqualBytes256", 256, Vector256.Zero, Vector256.Zero.WithElement(31, (byte)0x80)), + ("EqualSigned512", 512, Vector512.Zero, Vector512.Zero.WithElement(7, long.MinValue)) +}) +{ + var folder = Path.Combine(root, name + "LeftTwice"); + Directory.CreateDirectory(folder); + var image = Path.Combine(folder, "Examples.dll"); + using (var variant = ModuleDefinition.ReadModule(assembly)) + { + var method = variant.GetType("Examples.DivisionIntrinsics").Methods.Single(method => method.Name == name); + method.Body.Instructions.Single(site => site.OpCode.Code == Code.Ldarg_1).OpCode = OpCodes.Ldarg_0; + variant.Write(image); + } + var original = runtime.GetMethod(name)!; + var changed = Assembly.Load(File.ReadAllBytes(image)).GetType("Examples.DivisionIntrinsics")!.GetMethod(name)!; + if (original.Invoke(null, [zero, zero]) is not true || original.Invoke(null, [zero, highBit]) is not false || + changed.Invoke(null, [zero, highBit]) is not true) + throw new InvalidOperationException($"Unexpected CLR equality result for {name}"); + verification.VerifyContractVariant(assembly, "Examples", selectors[name], Path.Combine(root, name + "Contract"), + image, selectors[name], folder, $$""" + /-- Equality compares every bit of the two vectors and preserves caller memory. -/ + def equalVectors : MethodContract (BitVec {{width}} × BitVec {{width}}) Bool where + args pair := [.scalar (.v{{width}} pair.1), .scalar (.v{{width}} pair.2)] + results equal := [.scalar (.i32 (if equal then 1 else 0))] + pre _ _ := True + footprint _ := [] + post pair before after equal := after = before ∧ equal = (pair.1 == pair.2) + """, """ + set_option maxRecDepth 8192 in + theorem correctness : equalVectors.Holds Extracted.program 0 := by + rintro ⟨left, right⟩ memory - + apply MethodContract.Holds.intro (c := equalVectors) (input := (left, right)) memory + (left == right) ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + refine ⟨16, ?_⟩ + change Except.ok (memory, [Value.scalar (.i32 + (CIL.NarrowKind.unsigned8.extend (if left == right then 1 else 0)))]) = + Except.ok (memory, [Value.scalar (.i32 (if left == right then 1 else 0))]) + rw [CIL.NarrowKind.extend_ite_one_zero] + """, $$""" + set_option maxRecDepth 8192 in + theorem correctness : equalVectors.Refuted Extracted.program 0 := by + apply MethodContract.Refuted.of_return (c := equalVectors) (program := Extracted.program) + (method := 0) (input := (0, 1 <<< {{width - 1}})) (initial := Memory.empty) + (fuel := 16) (values := [.scalar (.i32 1)]) trivial rfl + rintro ⟨_, value, encoded, _, rfl⟩ + revert encoded + decide + """); + Console.WriteLine($"PASS {name}: the top bit distinguishes the original from the repeated-operand variant"); +} + +// Keep the broadcasts, vector local and MoveMask while reversing the unsigned comparison. +var wrongLess = Path.Combine(root, "GreaterInsteadOfLess"); +Directory.CreateDirectory(wrongLess); +var wrongLessAssembly = Path.Combine(wrongLess, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) +{ + var methods = variant.GetType("Examples.DivisionIntrinsics").Methods; + static Instruction Comparison(MethodDefinition method, string name) => + method.Body.Instructions.Single(site => site.Operand is MethodReference target && + target.DeclaringType.FullName == "System.Runtime.Intrinsics.X86.Avx512F" && target.Name == name); + Comparison(methods.Single(method => method.Name == "Less"), "CompareLessThan").Operand = + Comparison(methods.Single(method => method.Name == "Greater"), "CompareGreaterThan").Operand; + variant.Write(wrongLessAssembly); +} +if (Avx512F.IsSupported && Avx512DQ.IsSupported) +{ + const ulong high = 1UL << 63; + var changedType = Assembly.LoadFile(wrongLessAssembly).GetType("Examples.DivisionIntrinsics")!; + var original = runtime.GetMethod("Less")!.CreateDelegate>(); + var changed = changedType.GetMethod("Less")!.CreateDelegate>(); + if (original(1, high) != 255 || changed(1, high) != 0) + throw new InvalidOperationException("AVX-512 comparison masks differ from the unsigned high-bit witness."); +} +verification.VerifyContractVariant(assembly, "Examples", selectors["Less"], Path.Combine(root, "LessContract"), + wrongLessAssembly, selectors["Less"], wrongLess, """ + /-- An unsigned comparison returns eight identical mask bits in well-formed caller memory. -/ + def unsignedMask : MethodContract (W64 × W64) W32 where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + results value := [.scalar (.i32 value)] + pre _ memory := memory.WellFormed + footprint _ := [] + post input _ _ value := value = if input.1.ult input.2 then 255 else 0 + """, """ + open CIL + /-- The same unsigned word in all eight lanes. -/ + private abbrev splat (word : W64) := Vector.pack512 word word word word word word word word + /-- The comparison sets every mask bit precisely when the left word is smaller. -/ + private theorem mask (left right : W64) : + Vector.moveMask512 (Vector.zip512 (fun x y => Vector.mask64 (x.ult y)) + (splat left) (splat right)) = (if left.ult right then 255 else 0) := by + simp only [splat, Vector.zip512, Vector.lane512_0, Vector.lane512_1, + Vector.lane512_2, Vector.lane512_3, Vector.lane512_4, Vector.lane512_5, + Vector.lane512_6, Vector.lane512_7] + cases left.ult right <;> rfl + + #print axioms mask + + set_option maxRecDepth 8192 + set_option exponentiation.threshold 1024 + /-- The compiled method initializes one 512-bit vector local. -/ + private def localSpec : NumericLocalSpec := ⟨.vector512, .v512 0, 0, rfl⟩ + /-- The checked call returns the comparison mask and confines its write to the fresh local. -/ + theorem correctness : unsignedMask.Holds Extracted.program 0 := by + rintro ⟨left, right⟩ memory formed + let args : List CIL.Safety.Value := [.scalar (.i64 left), .scalar (.i64 right)] + obtain ⟨frame, entered, setup, homes, _, _⟩ := + numeric_frame_setup Extracted.method0 [localSpec] rfl rfl rfl rfl memory args formed + obtain ⟨home, slot, fresh, _, writable⟩ := homes.home_at 0 localSpec rfl + obtain ⟨after, stored, written, readback⟩ := step_store_numeric_local + (body := Extracted.method0) (pc := 10) (args := args) (rest := [.scalar (.v512 (splat left))]) + .vector512 (.v512 (splat right)) (splat right).toNat rfl slot writable + have loaded := step_load_numeric_local (body := Extracted.method0) (pc := 11) + (args := args) (stack := [.scalar (.v512 (splat left))]) .vector512 + (.v512 (splat right)) (splat right).toNat rfl slot readback + have preserved := write_preserves_memory_below _ _ _ _ _ _ fresh written + have final := Confined.frame_round_trip setup preserved (Nat.le_of_eq (write_nextIdentity written).symm) + apply MethodContract.Holds.intro (c := unsignedMask) (input := (left, right)) (leaveFrame frame after) + (if left.ult right then 255 else 0) ?_ final rfl + refine ⟨64, invoke_of_run Extracted.program_method0 rfl setup ?_⟩ + cil_checked_steps [] + refine (run_next (fuel := 53) Extracted.program_method0 (by rfl) stored).trans ?_ + refine (run_next (fuel := 52) Extracted.program_method0 (by rfl) loaded).trans ?_ + rw [← mask left right] + rfl + """, """ + set_option maxRecDepth 8192 + set_option exponentiation.threshold 1024 + /-- Returning zero violates the postcondition regardless of the final memory. -/ + private theorem wrongMask (after : Memory) : + ¬ unsignedMask.Returns (1, 0x8000000000000000) Memory.empty after [.scalar (.i32 0)] := by + rintro ⟨value, encoded, rfl⟩ + exact (by decide : ¬ ([Value.scalar (.i32 0)] = [.scalar (.i32 255)])) encoded + + #print axioms wrongMask + + /-- Reversing the comparison clears every mask bit on this unsigned input. -/ + theorem correctness : unsignedMask.Refuted Extracted.program 0 := by + let high : W64 := 0x8000000000000000 + let call := invoke Extracted.program 64 0 [.scalar (.i64 1), .scalar (.i64 high)] Memory.empty + have observed : call.map Prod.snd = .ok [.scalar (.i32 0)] := by decide +kernel + cases ran : call with + | error fault => simp [ran, Except.map] at observed + | ok result => + obtain ⟨after, values⟩ := result + have actual : values = [.scalar (.i32 0)] := by simpa [ran, Except.map] using observed + subst values + exact MethodContract.Refuted.of_return (c := unsignedMask) (input := (1, high)) + (program := Extracted.program) (fuel := 64) (method := 0) (final := after) + (values := [.scalar (.i32 0)]) Memory.empty_wellFormed ran (fun h => wrongMask after h.2) + """, profile: "x64-v4", imports: "import CIL.Safety.LocalSnapshots\nimport CIL.SIMD.PackedLanes"); +Console.WriteLine("PASS AVX-512 unsigned comparison contract and certified greater-than replacement"); + string rejectedEncoding = Path.Combine(root, "Reject-Vector512Encoding"); Directory.CreateDirectory(rejectedEncoding); string malformedAssembly = Path.Combine(rejectedEncoding, "Examples.dll"); @@ -230,7 +483,7 @@ end ExampleProof var shuffleRandom = new Random(0x5348); foreach ((string name, int width, string profile, string unavailable, string feature) in new[] { - ("Shuffle256", 32, "x64-v3", "x64-sse42", "Avx2"), ("Shuffle128", 16, "x64-sse42", "x64-sse2", "Ssse3") + ("Shuffle256", 32, "x64-v3", "x64-sse42", "Avx2"), ("Shuffle128", 16, "x64-sse42", "scalar", "Ssse3") }) { var cases = new List<(byte[] Source, byte[] Control)>(); @@ -283,7 +536,7 @@ string Claim(int sourceLength, int controlLength, string source, string control, foreach ((int sourceLength, int controlLength, int pc) in new[] { (width - 1, width, 1), (width, width - 1, 3) }) { claims.Add(Claim(sourceLength, controlLength, "[]", "[]", - $".error ⟨0, {pc}, .thrown \"System.ArgumentOutOfRangeException\"⟩")); + $".error ⟨0, {pc}, .thrown .argumentOutOfRange⟩")); proofs.Add("by rfl"); } File.WriteAllText(proof, $$""" @@ -330,43 +583,105 @@ end ExampleProof Flush(); -// Forged evidence must not substitute another load width or another intrinsic for the shuffle. -var rejectedShuffles = new RejectedEvidence(Path.Combine(root, "RejectedShuffles")); -foreach ((string name, string theorem, string before, string after, string validator) in new[] +static string ShuffleProof(int width, string contract) => $$""" + set_option maxRecDepth 8192 in + /-- The compiled shuffle reads each valid span once and leaves caller memory unchanged. -/ + theorem correctness : {{contract}}.Holds Extracted.program 0 := by + rintro ⟨s, c⟩ memory ⟨⟨sv, ss, sr⟩, cv, cs, cr⟩ + apply MethodContract.Holds.intro (c := {{contract}}) (input := (s, c)) memory + (CIL.Vector.shuffleBytes{{width}} s.bits c.bits) ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + refine ⟨32, ?_⟩ + change invoke Extracted.program 32 0 + [.bufferSpan (byteSpan s.address s.length), .bufferSpan (byteSpan c.address c.length)] memory = + .ok (memory, [.scalar (.v{{width}} (CIL.Vector.shuffleBytes{{width}} s.bits c.bits))]) + have first := read_span memory s.address s.length (.v{{width}} s.bits) sv ss sr [] + have second := read_span memory c.address c.length (.v{{width}} c.bits) cv cs cr [.scalar (.v{{width}} s.bits)] + cil_checked_steps [sv, cv, first, second] + """; + +// The AVX2 source halves contain ones and twos. Bit 7 masks every low byte. +static byte[] Halves(byte low, byte high) => + Enumerable.Repeat(low, 16).Concat(Enumerable.Repeat(high, 16)).ToArray(); +var shuffleSource = Halves(1, 2); +var shuffleMask = Halves(0x80, 0); +if (!RuntimeShuffle(runtime.GetMethod("Shuffle256")!, shuffleSource, shuffleMask).SequenceEqual(Halves(0, 2))) + throw new InvalidOperationException("AVX2 did not mask the low half or select byte two in the high half."); +foreach ((string name, byte low, byte high) in new (string, byte, byte)[] { - ("Shuffle256", "method0_unsafe_validated", ".snapshot .vector256", ".snapshot .vector128", "UnsafeCallsValid"), - ("Shuffle256", "method0_intrinsics_validated", ".avx2 .shuffleBytes", ".avx2 .add64", "ExactIntrinsicsValid"), - ("Shuffle128", "method0_intrinsics_validated", ".sse .shuffleBytes", ".sse .shiftLeftBytes", - "ExactIntrinsicsValid") + ("ShuffleCrossHalf", 0, 1), ("ShuffleIgnoreHighBit", 1, 2) }) { - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), theorem); - if (!source.Contains(before, StringComparison.Ordinal)) - throw new InvalidOperationException($"Missing shuffle mutation: {before}"); - var path = Path.Combine(root, "RejectedShuffles", $"{name}-{validator}.lean"); - Directory.CreateDirectory(Path.GetDirectoryName(path)!); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejectedShuffles.Add(path, $"PASS forged {name} evidence rejected by {validator}", validator); + if (!RuntimeShuffle(runtime.GetMethod(name)!, shuffleSource, shuffleMask).SequenceEqual(Halves(low, high))) + throw new InvalidOperationException($"The compiled {name} differs from its counterexample."); + verification.VerifyContractVariant(assembly, "Examples", selectors["Shuffle256"], + Path.Combine(root, "ShuffleContract"), assembly, selectors[name], Path.Combine(root, name), + "open Tests.ShuffleContract", ShuffleProof(256, "shuffle"), $$""" + set_option maxRecDepth 16384 in + /-- The extracted mutant returns the wrong bytes for the two readable spans. -/ + theorem correctness : shuffle.Refuted Extracted.program 0 := by + apply refuted_of_shuffle (fuel := 64) (actual := halves {{low}} {{high}}) (by decide) + rfl + """, profile: "x64-v3", imports: "import Tests.ShuffleContract"); + Console.WriteLine($"PASS AVX2 byte shuffle contract and checked {name} counterexample"); } -rejectedShuffles.Check(); + + +var reversed = Path.Combine(root, "Shuffle128Reversed"); +Directory.CreateDirectory(reversed); +var reversedImage = Path.Combine(reversed, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) +{ + var method = variant.GetType("Examples.DivisionIntrinsics").Methods.Single(m => m.Name == "Shuffle128"); + var loads = method.Body.Instructions.Where(i => i.OpCode.Code is Code.Ldarg_0 or Code.Ldarg_1).ToArray(); + if (loads.Length != 2 || loads[0].OpCode.Code != Code.Ldarg_0 || loads[1].OpCode.Code != Code.Ldarg_1) + throw new InvalidOperationException("Expected the shuffle to load source and then control."); + (loads[0].OpCode, loads[1].OpCode) = (loads[1].OpCode, loads[0].OpCode); + variant.Write(reversedImage); +} +if (Ssse3.IsSupported) +{ + var source = Enumerable.Repeat((byte)1, 16).ToArray(); + var control = Enumerable.Repeat((byte)0x80, 16).ToArray(); + var reversedMethod = Assembly.Load(File.ReadAllBytes(reversedImage)) + .GetType("Examples.DivisionIntrinsics")!.GetMethod("Shuffle128")!; + if (!RuntimeShuffle(runtime.GetMethod("Shuffle128")!, source, control).SequenceEqual(new byte[16]) || + !RuntimeShuffle(reversedMethod, source, control).SequenceEqual(control)) + throw new InvalidOperationException("The SSSE3 shuffle or its reversed variant disagrees with the witness."); +} +verification.VerifyContractVariant(assembly, "Examples", selectors["Shuffle128"], Path.Combine(root, "Shuffle128Contract"), + reversedImage, selectors["Shuffle128"], reversed, "open Tests.ShuffleContract", ShuffleProof(128, "shuffle128"), """ + set_option maxRecDepth 8192 in + /-- Reversing the two loads returns sixteen 0x80 bytes where the masked shuffle requires zeros. -/ + theorem correctness : shuffle128.Refuted Extracted.program 0 := by + apply refuted_of_reversed128 (fuel := 32) + rfl + """, profile: "x64-sse42", imports: "import Tests.ShuffleContract"); +Console.WriteLine("PASS SSSE3 byte shuffle contract and checked operand reversal"); + +// A forged operation at the hardware call must fail the model agreement of the complete method. +var rejectedHardware = new RejectedEvidence(Path.Combine(root, "RejectedHardware")); +void RejectHardware(string folder, string example, string member, string replacement, string label) +{ + var method = divisionMethods[example]; + var pc = method.Body.Instructions.Index().Single(item => + item.Item.Operand is MethodReference reference && + reference.FullName.Contains(member, StringComparison.Ordinal)).Index; + var source = File.ReadAllText(Path.Combine(root, folder, "Extracted.lean")); + var changed = "import Tests.Support.IntrinsicMethods\n" + + ForgeModelEmission(source, pc, $".call Tests.IntrinsicMethods.{replacement}"); + var output = Path.Combine(root, "RejectedHardware"); + Directory.CreateDirectory(output); + var path = Path.Combine(output, $"{folder}-{label}.lean"); + File.WriteAllText(path, changed); + rejectedHardware.Add(path, $"PASS {folder} rejects {replacement}", "Extraction.RuntimeModelsAgree", "is false"); +} +RejectHardware("Shuffle256", "Shuffle256", "Avx2::Shuffle(", "add64", "add"); +RejectHardware("Shuffle128", "Shuffle128", "Ssse3::Shuffle(", "shiftLeftBytes", "shift"); // The hash round is compared with the host CPU's AESENC under a custom x64 and a custom ARM64 AES // profile; the ARM64 extraction must agree, since AESE with a zero key, AESMC and the key are AESENC. if (!X86Aes.IsSupported) throw new InvalidOperationException("The AES differential test needs an x86 AES host"); -verification.EnsureExtractorBuilt(); -var profileType = Assembly.Load(File.ReadAllBytes(ExtractorPath)) - .GetType("Nethermind.LeanCIL.FeatureProfile", throwOnError: true)!; -var selectProfile = profileType.GetMethod("Select", BindingFlags.Static | BindingFlags.NonPublic)!; -string DeclareAes(string name, string flag) -{ - var profile = JsonSerializer.SerializeToNode(selectProfile.Invoke(null, [name]), - new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })!; - profile["name"] = $"{name}-{flag}"; - profile[flag] = true; - var path = Path.Combine(root, $"{name}-{flag}.json"); - File.WriteAllText(path, profile.ToJsonString()); - return "@" + path; -} + const int AesCases = 8; var aesRandom = new Random(20261102); ulong RandomWord() @@ -418,6 +733,69 @@ end ExampleProof () => Console.WriteLine($"PASS hash round matches the host CPU's AESENC on {AesCases} blocks: {label}"))); } Flush(); +// Both profiles must implement the same round for every input and reject a lost high key half. +var wrongAes = Path.Combine(root, "AesRoundLowKeyTwice"); +Directory.CreateDirectory(wrongAes); +var wrongAesImage = Path.Combine(wrongAes, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) +{ + var round = variant.GetType("Examples.DivisionIntrinsics").Methods.Single(m => m.Name == "AesRound"); + var highKey = round.Body.Instructions.Single(i => i.OpCode.Code == Code.Ldarg_3); + if (highKey.Previous?.OpCode.Code != Code.Ldarg_2 || + highKey.Next?.Operand is not MethodReference { Name: "Create" }) + throw new InvalidOperationException("Expected the two key halves before Vector128.Create."); + highKey.OpCode = OpCodes.Ldarg_2; + variant.Write(wrongAesImage); +} +var wrongRound = Assembly.Load(File.ReadAllBytes(wrongAesImage)).GetType("Examples.DivisionIntrinsics")!.GetMethod("AesRound")!; +var keyWitness = new object[] { 0UL, 0UL, 0UL, 1UL }; +var expectedZeroRound = Vector128.Create((byte)0x63).AsUInt64(); +var observedRound = ((Vector128)aesRound.Invoke(null, keyWitness)!).AsUInt64(); +var observedWrongRound = ((Vector128)wrongRound.Invoke(null, keyWitness)!).AsUInt64(); +if (observedWrongRound != expectedZeroRound || observedRound != (expectedZeroRound ^ Vector128.Create(0UL, 1UL))) + throw new InvalidOperationException("The CLR AES results disagree with the high-key witness."); + +foreach (var (label, profile, arm) in new[] +{ + ("x64", DeclareAes("x64-v4", "aes"), false), ("arm64", DeclareAes("arm64-advsimd", "armAes"), true) +}) +{ + var result = arm ? """ + change [Value.scalar (.v128 (CIL.Vector.aesMixColumns (CIL.Vector.armAesEncrypt state 0) ^^^ key))] = + [Value.scalar (.v128 (CIL.Vector.aesEncryptRound state key))] + rw [CIL.Vector.armAes_round_eq_aesEncryptRound] + """ : " rfl"; + verification.VerifyContractVariant(assembly, "Examples", selectors["AesRound"], Path.Combine(root, "AesRoundContract-" + label), + wrongAesImage, selectors["AesRound"], Path.Combine(wrongAes, label), "open Tests.AesRoundContract", $$""" + bind_entry entry (memory : Memory) (a b k l : W64) (wf : memory.WellFormed) := + Extracted.method0 with [.scalar (.i64 a), .scalar (.i64 b), .scalar (.i64 k), .scalar (.i64 l)] + from memory allocating wf + + set_option maxRecDepth 8192 in + /-- The compiled round uses its two fresh vector locals and preserves caller storage. -/ + theorem correctness : roundContract.Holds Extracted.program 0 := by + rintro ⟨a, b, k, l⟩ memory wf + obtain ⟨stateHome, keyHome, entered, enter⟩ := entry.checked memory a b k l wf + let state := CIL.Vector.pack128 a b + let key := CIL.Vector.pack128 k l + obtain ⟨first, stateStore, frame1, -⟩ := (enter.frame []).store 0 (.v128 state) rfl + obtain ⟨second, keyStore, frame2, -⟩ := frame1.store 1 (.v128 key) rfl + apply WP.enter (by rfl) (by rfl) enter.setup + have stateRead := frame2.snapshots.read_vector128 (index := 0) (bits := state) rfl (by rfl) + have keyRead := frame2.snapshots.read_vector128 (index := 1) (bits := key) rfl (by rfl) + cil_contract_steps [stateStore, keyStore, entry.frame, stateRead, keyRead, state, key] + refine ⟨(frame2.leave enter.setup).1, CIL.Vector.aesEncryptRound state key, ?_, rfl⟩ + {{result}} + """, """ + set_option maxRecDepth 8192 in + /-- Reusing the low key half drops the high bit set by the witness. -/ + theorem correctness : roundContract.Refuted Extracted.program 0 := by + apply refuted_of_dropped_key (fuel := 64) + rfl + """, profile: profile, imports: "import CIL\nimport CIL.Safety.LoopScaffold\nimport Tests.AesRoundContract"); + Console.WriteLine($"PASS {label} AES round contract and verified high-key counterexample"); +} + // Without AES the x86 getter folds false, so the reachable ARM64 calls are rejected on every built-in. foreach (var profile in new[] { "x64-v4", "arm64-advsimd" }) { @@ -432,23 +810,8 @@ end ExampleProof Console.WriteLine($"PASS AesRound rejected under {profile}"); } // Forged evidence must not substitute another AES step for the one the raw call names. -var rejectedRounds = new RejectedEvidence(Path.Combine(root, "RejectedAes")); -foreach ((string name, string before, string after) in new[] -{ - ("AesRound-x64", ".aes .encrypt", ".armAes .encrypt"), - ("AesRound-arm64", ".armAes .mixColumns", ".armAes .encrypt") -}) -{ - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_intrinsics_validated"); - if (!source.Contains(before, StringComparison.Ordinal)) - throw new InvalidOperationException($"Missing AES mutation: {before}"); - var path = Path.Combine(root, "RejectedAes", $"{name}.lean"); - Directory.CreateDirectory(Path.GetDirectoryName(path)!); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejectedRounds.Add(path, $"PASS forged {name} evidence rejected by ExactIntrinsicsValid", "ExactIntrinsicsValid"); -} -rejectedRounds.Check(); +RejectHardware("AesRound-x64", "AesRound", "X86.Aes::Encrypt(", "armEncrypt", "encrypt"); +RejectHardware("AesRound-arm64", "AesRound", "Arm.Aes::MixColumns(", "armEncrypt", "mix"); Run(["dotnet", "run", "--file", At("Tests/AesHardware.cs")], cwd: Root); Console.WriteLine("PASS host AES instructions match the Lean AES model"); @@ -470,9 +833,9 @@ ulong WindowWord() windowInputs.Add(Enumerable.Range(0, 8).Select(_ => WindowWord()).ToArray()); string VectorBits(byte[] bytes) => $"0x{Convert.ToHexString(bytes.Reverse().ToArray())}"; // Index bits 0-1 pick a lane and bit 2 picks the fill table; the mutants change which bit or table it is. -byte[] Window(ulong[] input, int tableBit, bool swapTables) +byte[] Window(ulong[] input, int tableBit, bool swapTables, bool lowSign = false) { - ulong fill = (long)input[3] < 0 ? ulong.MaxValue : 0; + ulong fill = (long)input[lowSign ? 0 : 3] < 0 ? ulong.MaxValue : 0; return input[4..].SelectMany(selector => BitConverter.GetBytes( (((selector >> tableBit) & 1) == 1) != swapTables ? fill : input[(int)(selector & 3)])).ToArray(); } @@ -501,6 +864,160 @@ byte[] Window(ulong[] input, int tableBit, bool swapTables) .All(same => same)) throw new InvalidOperationException($"Permutation inputs do not reject the {mutant} mutant"); } +// Change the VPERMQ immediate to broadcast limb zero's sign to all four upper-table lanes. +var lowSign = Path.Combine(root, "SignWindowLowSign"); +Directory.CreateDirectory(lowSign); +var lowSignImage = Path.Combine(lowSign, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) +{ + var method = variant.GetType("Examples.DivisionIntrinsics").Methods.Single(m => m.Name == "SignWindow"); + var permute = method.Body.Instructions.Single(instruction => + instruction.OpCode == OpCodes.Call && instruction.Operand is MethodReference + { Name: "Permute4x64", DeclaringType.FullName: "System.Runtime.Intrinsics.X86.Avx2" }); + var control = permute.Previous; + if (control.OpCode != OpCodes.Ldc_I4 || control.Operand is not 255) + throw new InvalidDataException("The sign broadcast must select lane three for all four lanes."); + control.OpCode = OpCodes.Ldc_I4_0; + control.Operand = null; + variant.Write(lowSignImage); +} +// Exchange the two tables passed to VPERMT2Q, keeping the index vector unchanged. +var swappedTables = Path.Combine(root, "SignWindowSwappedTables"); +Directory.CreateDirectory(swappedTables); +var swappedImage = Path.Combine(swappedTables, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) +{ + var method = variant.GetType("Examples.DivisionIntrinsics").Methods.Single(m => m.Name == "SignWindow"); + var tableCall = method.Body.Instructions.Single(instruction => + instruction.OpCode == OpCodes.Call && instruction.Operand is MethodReference { Name: "PermuteVar4x64x2" }); + var storeFill = method.Body.Instructions.Single(instruction => instruction.OpCode == OpCodes.Stloc_0); + var loadFill = tableCall.Previous; + if (loadFill.OpCode != OpCodes.Ldloc_0 || method.Body.Variables.Count != 1) + throw new InvalidDataException("Expected the upper sign table in local zero before VPERMT2Q."); + var sourceTable = new VariableDefinition(method.Body.Variables[0].VariableType); + method.Body.Variables.Add(sourceTable); + var il = method.Body.GetILProcessor(); + var saveSource = il.Create(OpCodes.Stloc, sourceTable); + il.InsertAfter(storeFill, saveSource); + il.InsertAfter(saveSource, il.Create(OpCodes.Ldloc_0)); + il.Replace(loadFill, il.Create(OpCodes.Ldloc, sourceTable)); + variant.Write(swappedImage); +} +// Move bit 3 of each scalar selector to bit 2 before VPERMT2Q chooses a table. +var bitThree = Path.Combine(root, "SignWindowBitThree"); +Directory.CreateDirectory(bitThree); +var bitThreeImage = Path.Combine(bitThree, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) +{ + var method = variant.GetType("Examples.DivisionIntrinsics").Methods.Single(m => m.Name == "SignWindow"); + var indices = method.Body.Instructions.Where(instruction => instruction.OpCode == OpCodes.Ldarg_S + && instruction.Operand is ParameterDefinition { Index: >= 4 and <= 7 }).ToArray(); + if (!indices.Select(instruction => ((ParameterDefinition)instruction.Operand).Index).SequenceEqual([4, 5, 6, 7])) + throw new InvalidDataException("Expected four scalar selector loads in lane order."); + var il = method.Body.GetILProcessor(); + foreach (var load in indices) + { + var next = load.Next; + // (index & 3) | ((index >> 1) & 4) preserves the lane and changes the table selector. + il.InsertBefore(next, il.Create(OpCodes.Ldc_I4_3)); + il.InsertBefore(next, il.Create(OpCodes.Conv_I8)); + il.InsertBefore(next, il.Create(OpCodes.And)); + il.InsertBefore(next, il.Create(OpCodes.Ldarg_S, (ParameterDefinition)load.Operand)); + il.InsertBefore(next, il.Create(OpCodes.Ldc_I4_1)); + il.InsertBefore(next, il.Create(OpCodes.Shr_Un)); + il.InsertBefore(next, il.Create(OpCodes.Ldc_I4_4)); + il.InsertBefore(next, il.Create(OpCodes.Conv_I8)); + il.InsertBefore(next, il.Create(OpCodes.And)); + il.InsertBefore(next, il.Create(OpCodes.Or)); + } + variant.Write(bitThreeImage); +} +const string windowContractProof = """ + set_option maxRecDepth 12288 + set_option exponentiation.threshold 1024 + + /-- The compiled method stores a 256-bit sign-fill table in one frame-local slot. -/ + private def localSpec : NumericLocalSpec := ⟨.vector256, .v256 0, 0, rfl⟩ + + /-- SignWindow implements the lane selector and preserves caller storage on x64-v4. -/ + theorem correctness : signWindow.Holds Extracted.program 0 := by + rintro ⟨v, i⟩ memory formed + let args := signWindow.args (v, i) + let value := pack256 v.a v.b v.c v.d + let indices := pack256 i.a i.b i.c i.d + let fill := permute4x64 (zip256 (fun x y => mask64 (x.slt y)) value 0) 255 + obtain ⟨frame, entered, setup, homes, _, _⟩ := + numeric_frame_setup Extracted.method0 [localSpec] rfl rfl rfl rfl memory args formed + obtain ⟨home, slot, fresh, _, writable⟩ := homes.home_at 0 localSpec rfl + obtain ⟨after, stored, written, readback⟩ := step_store_numeric_local + (body := Extracted.method0) (pc := 12) (args := args) (rest := [.scalar (.v256 value)]) + .vector256 (.v256 fill) fill.toNat rfl slot writable + have loaded := step_load_numeric_local (body := Extracted.method0) (pc := 18) + (args := args) (stack := [.scalar (.v256 indices), .scalar (.v256 value)]) + .vector256 (.v256 fill) fill.toNat rfl slot readback + have preserved := write_preserves_memory_below _ _ _ _ _ _ fresh written + have final := Confined.frame_round_trip setup preserved (Nat.le_of_eq (write_nextIdentity written).symm) + apply MethodContract.Holds.intro (c := signWindow) (input := (v, i)) (leaveFrame frame after) + (permuteVar4x64x2 value indices fill) ?_ final ?_ + · refine ⟨64, invoke_of_run Extracted.program_method0 rfl setup ?_⟩ + dsimp only [args, signWindow] at * + cil_checked_steps [] + refine (run_next (fuel := 51) Extracted.program_method0 (by rfl) stored).trans ?_ + cil_checked_steps [] + refine (run_next (fuel := 45) Extracted.program_method0 (by rfl) loaded).trans ?_ + rfl + · exact signWindow_result v i + """; +string WindowCounterexample(string actual) => $$""" + set_option maxRecDepth 12288 + /-- The sign bit is set only in limb three. Each selector requests the sign-fill table. -/ + private def witness : Limbs × Limbs := (⟨0, 1, 2, 0x8000000000000000⟩, ⟨4, 5, 6, 7⟩) + + /-- The compiled variant returns a different vector from the high-sign table at this input. -/ + theorem correctness : signWindow.Refuted Extracted.program 0 := by + let call := invoke Extracted.program 128 0 (signWindow.args witness) Memory.empty + have observed : call.map Prod.snd = .ok [.scalar (.v256 ({{actual}}))] := by decide +kernel + cases ran : call with + | error fault => simp [ran, Except.map] at observed + | ok result => + obtain ⟨after, values⟩ := result + have actual : values = [.scalar (.v256 ({{actual}}))] := by simpa [ran, Except.map] using observed + subst values + apply MethodContract.Refuted.of_return (c := signWindow) (input := witness) + (program := Extracted.program) (fuel := 128) (method := 0) (final := after) + (values := [.scalar (.v256 ({{actual}}))]) Memory.empty_wellFormed ran + rintro ⟨_, value, encoded, rfl⟩ + exact (by decide : ¬ ([CIL.Safety.Value.scalar (.v256 ({{actual}}))] = + [.scalar (.v256 (pack256 (selected witness.1 witness.2.a) (selected witness.1 witness.2.b) + (selected witness.1 witness.2.c) (selected witness.1 witness.2.d)))])) encoded + """; +foreach (var (image, output, actual, tableBit, swapTables) in new[] +{ + (lowSignImage, lowSign, "0", 2, false), + (swappedImage, swappedTables, "pack256 0 1 2 0x8000000000000000", 2, true), + (bitThreeImage, bitThree, "pack256 0 1 2 0x8000000000000000", 3, false) +}) +{ + if (Avx512F.VL.IsSupported) + { + var changed = Assembly.Load(File.ReadAllBytes(image)).GetType("Examples.DivisionIntrinsics")! + .GetMethod("SignWindow")!; + ulong[] witness = [0, 1, 2, 1UL << 63, 4, 5, 6, 7]; + foreach (var input in windowInputs.Append(witness)) + { + var observed = (Vector256)changed.Invoke(null, input.Cast().ToArray())!; + var expected = Window(input, tableBit, swapTables, lowSign: output == lowSign); + if (!Vector256.EqualsAll(observed.AsByte(), Vector256.Create(expected))) + throw new InvalidDataException($"The compiled {Path.GetFileName(output)} disagreed with its scalar result."); + } + } + verification.VerifyContractVariant(assembly, "Examples", selectors["SignWindow"], Path.Combine(root, "SignWindowContract"), + image, selectors["SignWindow"], output, "open Tests.SignWindowContract CIL.Vector", + windowContractProof, WindowCounterexample(actual), profile: "x64-v4", + imports: "import Tests.SignWindowContract\nimport CIL.Safety.LocalSnapshots"); + Console.WriteLine($"PASS AVX-512 sign window and checked {Path.GetFileName(output)} counterexample"); +} + uint[] words = Enumerable.Range(0, 8).Select(_ => (uint)WindowWord()).ToArray(); var wordBytes = new byte[32]; ((Vector256)runtime.GetMethod("CreateWords")!.Invoke(null, words.Cast().ToArray())!) @@ -542,14 +1059,19 @@ end ExampleProof () => Console.WriteLine($"PASS {name} extraction matches its reference on {claims.Length} inputs"))); } -// No host here runs AdvSimd, so REV64 is checked against BinaryPrimitives.ReverseEndianness of each lane. The -// halves are checked against the host's own Load/StoreUnsafe; a destination one byte short faults on the second store. +// The compiled REV64 result is checked against BinaryPrimitives.ReverseEndianness of each lane. var reverseClaims = new[] { (0x0706050403020100UL, 0x0f0e0d0c0b0a0908UL), (WindowWord(), WindowWord()) } .Select(input => { var bytes = new byte[16]; Vector128.Create(BinaryPrimitives.ReverseEndianness(input.Item1), BinaryPrimitives.ReverseEndianness(input.Item2)).AsByte().CopyTo(bytes); + if (System.Runtime.Intrinsics.Arm.AdvSimd.IsSupported) + { + var observed = (Vector128)runtime.GetMethod("ReverseLimbs")!.Invoke(null, [input.Item1, input.Item2])!; + if (observed.AsByte() != Vector128.Create((ReadOnlySpan)bytes)) + throw new InvalidOperationException("Host REV64 differs from byte reversal within each input lane."); + } return Invocation([$".scalar (.i64 {input.Item1})", $".scalar (.i64 {input.Item2})"], $".v128 {VectorBits(bytes)}"); }).ToArray(); @@ -569,10 +1091,161 @@ end ExampleProof """); pending.Add((new(selectors["ReverseLimbs"], reverseProof, reverseOutput), "arm64-advsimd", () => Console.WriteLine("PASS ReverseLimbs reverses the bytes of each lane under arm64-advsimd"))); +// Exchange the two leading loads and preserve the intrinsic and the rest of the body. +(string Assembly, string Output) SwapFirstArguments(string methodName) +{ + var output = Path.Combine(root, "Swapped" + methodName); + Directory.CreateDirectory(output); + var image = Path.Combine(output, "Examples.dll"); + using var variant = ModuleDefinition.ReadModule(assembly); + var method = variant.GetType("Examples.DivisionIntrinsics").Methods.Single(m => m.Name == methodName); + var code = method.Body.Instructions; + if (code[0].OpCode != OpCodes.Ldarg_0 || code[1].OpCode != OpCodes.Ldarg_1) + throw new InvalidDataException($"{methodName} must load arguments zero and one first."); + (code[0].OpCode, code[1].OpCode) = (code[1].OpCode, code[0].OpCode); + variant.Write(image); + return (image, output); +} + +var equalityArguments = SwapFirstArguments("EqualWords128"); +var equalWords = runtime.GetMethod("EqualWords128")!; +var swappedEqualWords = Assembly.Load(File.ReadAllBytes(equalityArguments.Assembly)) + .GetType("Examples.DivisionIntrinsics")!.GetMethod("EqualWords128")!; +object[] firstWordSet = [1u, 0u, 0u, 0u, Vector128.Create(1u, 0u, 0u, 0u)]; +if (equalWords.Invoke(null, firstWordSet) is not true || swappedEqualWords.Invoke(null, firstWordSet) is not false) + throw new InvalidOperationException("Swapping the two low words must change equality at the CLR witness."); +verification.VerifyContractVariant(assembly, "Examples", selectors["EqualWords128"], + Path.Combine(root, "EqualWords128Contract"), equalityArguments.Assembly, selectors["EqualWords128"], + equalityArguments.Output, """ + /-- Four 32-bit words and the vector to compare against. -/ + structure Comparison where + a : W32 + b : W32 + c : W32 + d : W32 + expected : BitVec 128 + + /-- The comparison packs the four words in lane order and preserves caller memory. -/ + def equalWords : MethodContract Comparison Bool where + args w := [.scalar (.i32 w.a), .scalar (.i32 w.b), .scalar (.i32 w.c), .scalar (.i32 w.d), + .scalar (.v128 w.expected)] + results equal := [.scalar (.i32 (if equal then 1 else 0))] + pre _ _ := True + footprint _ := [] + post w before after equal := after = before ∧ + equal = (((w.d ++ w.c) ++ (w.b ++ w.a)) == w.expected) + """, """ + set_option maxRecDepth 8192 in + /-- The compiled comparison checks every bit of all four words. -/ + theorem correctness : equalWords.Holds Extracted.program 0 := by + intro w memory _ + let equal := (((w.d ++ w.c) ++ (w.b ++ w.a)) == w.expected) + apply MethodContract.Holds.intro (c := equalWords) (input := w) memory equal ?_ + (Confined.refl _ _) ⟨rfl, rfl⟩ + refine ⟨32, ?_⟩ + change Except.ok (memory, [Value.scalar (.i32 + (CIL.NarrowKind.unsigned8.extend (if equal then 1 else 0)))]) = + Except.ok (memory, [Value.scalar (.i32 (if equal then 1 else 0))]) + rw [CIL.NarrowKind.extend_ite_one_zero] + """, """ + /-- Moving the only set bit from lane zero to lane one changes the return to false. -/ + theorem correctness : equalWords.Refuted Extracted.program 0 := by + apply MethodContract.Refuted.of_return (c := equalWords) (input := ⟨1, 0, 0, 0, 1⟩) + (initial := Memory.empty) (fuel := 32) (values := [.scalar (.i32 0)]) trivial rfl + rintro ⟨_, value, encoded, _, rfl⟩ + revert encoded + decide + """); +Console.WriteLine("PASS four-word vector equality preserves memory and refutes swapped low words"); + +var wordArguments = SwapFirstArguments("CreateWords"); +verification.VerifyContractVariant(assembly, "Examples", selectors["CreateWords"], Path.Combine(root, "WordsContract"), + wordArguments.Assembly, selectors["CreateWords"], wordArguments.Output, """ + /-- The eight input words in argument order. -/ + structure Words where + a : W32 + b : W32 + c : W32 + d : W32 + e : W32 + f : W32 + g : W32 + h : W32 + + /-- Lane zero contains the first word, and lane seven contains the last. -/ + def lanes (w : Words) : BitVec 256 := + ((w.h ++ w.g) ++ (w.f ++ w.e)) ++ ((w.d ++ w.c) ++ (w.b ++ w.a)) + + /-- Creating the vector packs all eight words and preserves caller memory. -/ + def createWords : MethodContract Words (BitVec 256) where + args w := [.scalar (.i32 w.a), .scalar (.i32 w.b), .scalar (.i32 w.c), .scalar (.i32 w.d), + .scalar (.i32 w.e), .scalar (.i32 w.f), .scalar (.i32 w.g), .scalar (.i32 w.h)] + results v := [.scalar (.v256 v)] + pre _ _ := True + footprint _ := [] + post w before after v := after = before ∧ v = lanes w + """, """ + set_option maxRecDepth 8192 in + /-- The compiled method puts every word in its declared lane. -/ + theorem correctness : createWords.Holds Extracted.program 0 := by + intro w memory _ + apply MethodContract.Holds.intro (c := createWords) (input := w) memory (lanes w) + ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + exact ⟨64, rfl⟩ + """, """ + private def input : Words := ⟨1, 0, 0, 0, 0, 0, 0, 0⟩ + + set_option maxRecDepth 8192 in + /-- Swapping the leading loads puts the only nonzero word in lane one. -/ + theorem correctness : createWords.Refuted Extracted.program 0 := by + apply MethodContract.Refuted.of_return (input := input) (initial := Memory.empty) + (fuel := 64) (values := [.scalar (.v256 4294967296)]) trivial rfl + rintro ⟨_, v, encoded, _, rfl⟩ + exact (by decide : ¬ ([CIL.Safety.Value.scalar (.v256 4294967296)] = + [.scalar (.v256 (lanes input))])) encoded + """); +Console.WriteLine("PASS vector creation packs every input lane and refutes swapped low words"); + +var (swappedAssembly, swapped) = SwapFirstArguments("ReverseLimbs"); +verification.VerifyContractVariant(assembly, "Examples", selectors["ReverseLimbs"], + Path.Combine(root, "ReverseContract"), swappedAssembly, selectors["ReverseLimbs"], swapped, """ + open CIL (reverseBytes) + open CIL.Vector (pack128 map128_pack128) + + /-- Both limbs stay in their original lanes, with the bytes in each limb reversed. -/ + def reversed : MethodContract (W64 × W64) (BitVec 128) where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + results value := [.scalar (.v128 value)] + pre _ _ := True + footprint _ := [] + post input before after value := after = before ∧ + value = pack128 (reverseBytes input.1) (reverseBytes input.2) + """, """ + /-- REV64 reverses each input limb and preserves the caller's memory. -/ + theorem correctness : reversed.Holds Extracted.program 0 := by + rintro ⟨low, high⟩ memory - + apply MethodContract.Holds.intro (c := reversed) (input := (low, high)) memory + (pack128 (reverseBytes low) (reverseBytes high)) ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + refine ⟨64, ?_⟩ + change invoke Extracted.program 64 0 [.scalar (.i64 low), .scalar (.i64 high)] memory = + .ok (memory, [.scalar (.v128 (pack128 (reverseBytes low) (reverseBytes high)))]) + rw [← map128_pack128] + rfl + """, """ + /-- Distinct limbs expose the swapped argument loads on a successful execution. -/ + theorem correctness : reversed.Refuted Extracted.program 0 := by + have ran : invoke Extracted.program 64 0 [.scalar (.i64 1), .scalar (.i64 2)] Memory.empty = + .ok (Memory.empty, [.scalar (.v128 (pack128 (reverseBytes 2) (reverseBytes 1)))]) := rfl + exact .of_return (input := (1, 2)) trivial ran + (by rintro ⟨-, value, encoded, -, rfl⟩; revert encoded; decide) + """, profile: "arm64-advsimd", imports: "import CIL.SIMD.PackedLanes"); +Console.WriteLine("PASS ARM64 REV64 contract and certified swapped lanes at (1, 2)"); + var halves = new byte[32]; windowRandom.NextBytes(halves); var swappedHalves = new byte[32]; -runtime.GetMethod("SwapHalves")!.CreateDelegate()(halves, swappedHalves); +var originalSwap = runtime.GetMethod("SwapHalves")!.CreateDelegate(); +originalSwap(halves, swappedHalves); if (!swappedHalves.SequenceEqual(halves[16..].Concat(halves[..16]))) throw new InvalidOperationException("Runtime SwapHalves did not swap the 16-byte halves"); var halvesOutput = Path.Combine(root, "SwapHalves"); @@ -614,6 +1287,101 @@ end ExampleProof pending.Add((new(selectors["SwapHalves"], halvesProof, halvesOutput), "arm64-advsimd", () => Console.WriteLine("PASS SwapHalves matches the host's halves and faults past a short destination"))); Flush(); +// Move only the upper LoadUnsafe to offset zero. The compiled variant still returns normally. +var repeatedHalf = Path.Combine(root, "RepeatLowerHalf"); +Directory.CreateDirectory(repeatedHalf); +var repeatedImage = Path.Combine(repeatedHalf, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) +{ + var method = variant.GetType("Examples.DivisionIntrinsics").Methods.Single(m => m.Name == "SwapHalves"); + var load = method.Body.Instructions.Single(i => i.Operand is MethodReference call && + call.Name == "LoadUnsafe" && call.Parameters.Count == 2); + var offset = load.Previous; + if (offset.OpCode == OpCodes.Conv_I || offset.OpCode == OpCodes.Conv_U) offset = offset.Previous; + if (offset.OpCode != OpCodes.Ldc_I4_S || (sbyte)offset.Operand != 16) + throw new InvalidDataException("SwapHalves must load its second vector at byte offset 16."); + offset.Operand = (sbyte)0; + variant.Write(repeatedImage); +} + +var repeatedSwap = Assembly.Load(File.ReadAllBytes(repeatedImage)).GetType("Examples.DivisionIntrinsics")! + .GetMethod("SwapHalves")!.CreateDelegate(); +var overlapRandom = new Random(568); +foreach (var displacement in new[] { -16, -8, -1, 0, 1, 8, 16, 32 }) +{ + const int source = 24; + var destination = source + displacement; + for (int trial = 0; trial < 12; trial++) + { + var initial = new byte[96]; + overlapRandom.NextBytes(initial); + var expected = (byte[])initial.Clone(); + initial.AsSpan(source + 16, 16).CopyTo(expected.AsSpan(destination)); + initial.AsSpan(source, 16).CopyTo(expected.AsSpan(destination + 16)); + var actual = (byte[])initial.Clone(); + originalSwap(actual.AsSpan(source, 32), actual.AsSpan(destination, 32)); + var changed = (byte[])initial.Clone(); + repeatedSwap(changed.AsSpan(source, 32), changed.AsSpan(destination, 32)); + if (!actual.AsSpan().SequenceEqual(expected) || changed.AsSpan().SequenceEqual(expected)) + throw new InvalidOperationException($"SwapHalves overlap mismatch at displacement {displacement}, trial {trial}."); + } +} + +verification.VerifyContractVariant(assembly, "Examples", selectors["SwapHalves"], Path.Combine(root, "InPlaceHalfSwap"), + repeatedImage, selectors["SwapHalves"], repeatedHalf, "open Tests.HalfSwapContract", """ + /-- The first load snapshots the low half before either caller write. -/ + private theorem firstLoad (data : Nat → BitVec 8) (fuel : Nat) : + invoke Extracted.program (fuel + 7) 0 (swap.args data) (memory data) = + run Extracted.program fuel 0 7 (swap.args data) saved + [.scalar (.v128 (packed data 0)), .readOnlyReference (.address ⟨0, 0⟩)] (entered data) := by + with_unfolding_all rfl + #print axioms firstLoad + + /-- The low half occupies the private local while the second load reads the high half. -/ + private theorem saveAndLoad (data : Nat → BitVec 8) (fuel : Nat) : + run Extracted.program (fuel + 4) 0 7 (swap.args data) saved + [.scalar (.v128 (packed data 0)), .readOnlyReference (.address ⟨0, 0⟩)] (entered data) = + run Extracted.program fuel 0 11 (swap.args data) saved + [.scalar (.v128 (packed data 16))] (savedLow data) := by + unfold savedLow + rw [← unpack data 0] + with_unfolding_all rfl + #print axioms saveAndLoad + + /-- The first store copies the high half into the low addresses. -/ + private theorem firstStore (data : Nat → BitVec 8) (fuel : Nat) : + run Extracted.program (fuel + 2) 0 11 (swap.args data) saved + [.scalar (.v128 (packed data 16))] (savedLow data) = + run Extracted.program fuel 0 13 (swap.args data) saved [] (highWritten data) := by + unfold highWritten + rw [← unpack data 16] + with_unfolding_all rfl + #print axioms firstStore + + /-- The saved vector fills the upper half and the return retires its private home. -/ + private theorem secondStore (data : Nat → BitVec 8) : + run Extracted.program 6 0 13 (swap.args data) saved [] (highWritten data) = + .ok (expire (lowWritten data) 1, []) := by + unfold lowWritten + rw [← unpack data 0] + with_unfolding_all rfl + #print axioms secondStore + + /-- Every in-place swap exchanges both halves inside the caller footprint. -/ + theorem correctness : swap.Holds Extracted.program 0 := by + rintro data _ rfl + refine ⟨19, (expire (lowWritten data) 1, []), ?_, framed data, (), rfl, swapped data⟩ + exact (firstLoad data 12).trans ((saveAndLoad data 8).trans ((firstStore data 6).trans (secondStore data))) + """, """ + theorem correctness : swap.Refuted Extracted.program 0 := by + apply refuted_of_first_byte + with_unfolding_all rfl + """, profile: "arm64-advsimd", imports: "import Tests.HalfSwapContract"); +var overlapProof = Path.Combine(root, "HalfSwapOverlap.lean"); +File.Copy(At("Tests/HalfSwapOverlap.lean.in"), overlapProof, overwrite: true); +verification.Verify(assembly, "Examples", selectors["SwapHalves"], overlapProof, + Path.Combine(root, "OverlapHalfSwap"), "arm64-advsimd", theorem: "HalfSwapOverlap.correctness"); +Console.WriteLine("PASS SwapHalves contracts for all eight placements, certified repeated load and 96 CLR comparisons"); // Without AdvSimd or AVX-512 VL the reachable calls are rejected before any evidence is written. foreach ((string name, string profile, string feature) in new[] { @@ -630,31 +1398,96 @@ end ExampleProof $"{name} admitted without {feature} under {profile}: {rejectedCall.Output}"); Console.WriteLine($"PASS {name} rejected under {profile}"); } -// Forged evidence must not change the table select, the lane reversal, or a transfer's offset form. -var rejectedEndian = new RejectedEvidence(Path.Combine(root, "RejectedEndian")); -foreach ((string name, string theorem, string before, string after, string validator, int index) in new[] -{ - ("SignWindow", "method0_intrinsics_validated", ".avx512 .permuteVar4x64x2", ".avx512 .ternaryLogic", - "ExactIntrinsicsValid", 0), - ("ReverseLimbs", "method0_intrinsics_validated", ".advSimd .reverseElement8", ".avx2 .shuffleBytes", - "ExactIntrinsicsValid", 1), - ("SwapHalves", "method0_unsafe_validated", ".vector128Unsafe true true", ".vector128Unsafe true false", - "UnsafeCallsValid", 2), - ("SwapHalves", "method0_unsafe_validated", ".memory (.vector128Unsafe false false)", ".memory .load128", - "UnsafeCallsValid", 3) -}) +// Exercise every portable vector declaration over pinned example metadata. +var vectorWitnesses = new (string Name, string Profile)[] +{ + ("And", "x64-v3"), ("Less", "x64-v4"), ("SignWindow", "x64-v4"), + ("DefinedLocal", "scalar"), ("UnsafeEquals", "scalar"), ("ReverseLimbs", "arm64-advsimd"), + ("Shuffle128", "x64-v3"), ("Shuffle256", "x64-v3"), ("CreateWords", "scalar"), + ("SwapHalves", "arm64-advsimd"), ("EqualWords128", "scalar"), + ("EqualBytes256", "scalar"), ("EqualSigned512", "scalar") +}; +var operandRoot = Path.Combine(root, "VectorOperands"); +ExtractBatch("extract", "Examples", vectorWitnesses.Select(witness => + new GeneratedExtraction(assembly, selectors[witness.Name], Path.Combine(operandRoot, witness.Name), + witness.Profile))); +var operands = new GeneratedEvidence(operandRoot); +foreach (var (name, _) in vectorWitnesses) { - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), theorem); - if (!source.Contains(before, StringComparison.Ordinal)) - throw new InvalidOperationException($"Missing {name} mutation: {before}"); - var path = Path.Combine(root, "RejectedEndian", $"{name}-{index}.lean"); - Directory.CreateDirectory(Path.GetDirectoryName(path)!); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejectedEndian.Add(path, $"PASS forged {name} evidence rejected by {validator}", validator); + var folder = Path.Combine(operandRoot, name); + var proof = Path.Combine(folder, "Operand.lean"); + File.Copy(Path.Combine(folder, "Extracted.lean"), proof, true); + var method = divisionMethods[name]; + ModelOperandChecks.Append(proof, method, VectorModel); + if (name == "Shuffle256") CheckShuffleReadWidth(proof, method); + operands.Add(proof, $"PASS vector operands of {name}"); +} +operands.Check(replay: false); + +// Each of the two source spans must provide all 32 bytes of its vector. +static void CheckShuffleReadWidth(string proof, MethodDefinition method) +{ + var reads = method.Body.Instructions.Index() + .Where(item => item.Item.Operand is MethodReference call && + VectorModel(call) == ".memory (.marshalRead (.snapshot .vector256) false)") + .Select(item => item.Index).ToArray(); + if (reads.Length != 2) throw new InvalidDataException($"Expected two vector reads in {method.FullName}"); + File.AppendAllText(proof, $$""" + + namespace Extracted + open CIL CIL.Extraction + /-- Neither 256-bit read can emit a direct 128-bit snapshot read. -/ + theorem shuffle_rejects_narrow_read : + [{{string.Join(", ", reads)}}].all (fun pc => (fixtureOperandCode[pc]?).any fun raw => + let closed := { method0Closed with code := [raw] } + match raw.operand with + | .call site => + let vector := TypeExpr.apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") + (.atom runtimeScope "System.Byte") + let narrow := { site.method with + owner := .atom intrinsicsScope "System.Runtime.Intrinsics.Vector128", returnType := vector } + let emitted := { method0 with code := [.call narrow {}], links := [] } + runtimeModel environment .call narrow == some (.memory (.marshalRead (.snapshot .vector128) false)) && + !(decide (RuntimeModelsAgree closed [true] emitted)) + | _ => false) = true := by cil_kernel_rfl + #print axioms shuffle_rejects_narrow_read + end Extracted + + """); +} + +// The fixture's declaration fixes the expected operation independently of the extractor. +static string? VectorModel(MethodReference call) +{ + if (call.DeclaringType.Namespace != "System.Runtime.Intrinsics") return null; + int width = call.DeclaringType.Name switch + { + "Vector128" or "Vector128`1" => 128, "Vector256" or "Vector256`1" => 256, + "Vector512" or "Vector512`1" => 512, _ => 0 + }; + if (width == 0) return null; + var count = call.Parameters.Count; + var hasOffset = call.Parameters.LastOrDefault()?.ParameterType.MetadataType == MetadataType.UIntPtr; + var offset = hasOffset ? "true" : "false"; + return call.Name switch + { + "op_Equality" => $".intrinsic (.vector (.equalsAll {width})) 2", + "LoadUnsafe" => $".memory (.vector128Unsafe false {offset})", + "StoreUnsafe" => $".memory (.vector128Unsafe true {offset})", + "Create" when width == 512 => ".intrinsic (.vector .combine256) 2", + "Create" when call is GenericInstanceMethod => $".memory (.marshalRead (.snapshot .vector{width}) false)", + "Create" when call.Parameters[0].ParameterType.MetadataType == MetadataType.UInt32 => + $".intrinsic (.vector (.create32 {width})) {count}", + "Create" => $".intrinsic (.vector (.create64 {width})) {count}", + _ => null + }; } -rejectedEndian.Check(); -// PMULUDQ runs on the host CPU and its x64-sse2 extraction must return the same products. +// Forged evidence cannot change the table select or the lane reversal. +RejectHardware("SignWindow", "SignWindow", "::PermuteVar4x64x2(", "ternaryLogic", "permute"); +RejectHardware("ReverseLimbs", "ReverseLimbs", "::ReverseElement8(", "shuffle256", "reverse"); + +// PMULUDQ runs on the host CPU and its x64-sse42 extraction must return the same products. // Lanes with set high halves and both 32-bit extremes come first, then seeded random words. const int MultiplyCases = 12; var multiplyRandom = new Random(0x4d55); @@ -717,9 +1550,97 @@ namespace ExampleProof #print axioms correctness end ExampleProof """); -pending.Add((new(selectors["MultiplyEven128"], multiplyProof, multiplyOutput), "x64-sse2", +pending.Add((new(selectors["MultiplyEven128"], multiplyProof, multiplyOutput), "x64-sse42", () => Console.WriteLine($"PASS MultiplyEven128 matches the host CPU on {multiplyClaims.Length} inputs"))); Flush(); +// Shift each input's odd UInt32 element into the even position before the original SSE2 multiply. +var oddProducts = Path.Combine(root, "OddProducts128"); +Directory.CreateDirectory(oddProducts); +var oddProductsAssembly = Path.Combine(oddProducts, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) +{ + var method = variant.GetType("Examples.DivisionIntrinsics").Methods + .Single(method => method.Name == "MultiplyEven128"); + var loads = method.Body.Instructions.Where(site => + site.OpCode.Code is Code.Ldarg_0 or Code.Ldarg_1 or Code.Ldarg_2 or Code.Ldarg_3).ToArray(); + Code[] expected = [Code.Ldarg_0, Code.Ldarg_1, Code.Ldarg_2, Code.Ldarg_3]; + if (!loads.Select(load => load.OpCode.Code).SequenceEqual(expected)) + throw new InvalidDataException("MultiplyEven128 must load the four words in lane order."); + var il = method.Body.GetILProcessor(); + foreach (var load in loads) + { + var count = il.Create(OpCodes.Ldc_I4_S, (sbyte)32); + il.InsertAfter(load, count); + il.InsertAfter(count, il.Create(OpCodes.Shr_Un)); + } + variant.Write(oddProductsAssembly); +} +if (Sse2.IsSupported) +{ + var changedType = Assembly.LoadFile(oddProductsAssembly).GetType("Examples.DivisionIntrinsics")!; + static Vector128 Products(Type type) => type.GetMethod("MultiplyEven128")! + .CreateDelegate>>() + (0x100000002, 0x300000004, 0x500000006, 0x700000008); + if (!Products(runtime).Equals(Vector128.Create(12UL, 32UL)) || + !Products(changedType).Equals(Vector128.Create(5UL, 21UL))) + throw new InvalidOperationException("SSE2 products differ from the unsigned low- and high-half witness."); +} +verification.VerifyContractVariant(assembly, "Examples", selectors["MultiplyEven128"], + Path.Combine(root, "EvenProductsContract"), oddProductsAssembly, selectors["MultiplyEven128"], oddProducts, """ + open CIL.Vector (pack128 sseMultiplyEven32_pack128) + + /-- Each unsigned 32-bit operand comes from the low half of its 64-bit lane. -/ + private def product (left right : W64) : W64 := + (left.setWidth 32).zeroExtend 64 * (right.setWidth 32).zeroExtend 64 + + /-- PMULUDQ returns two full-width products and preserves caller memory. -/ + def evenProducts : MethodContract ((W64 × W64) × (W64 × W64)) (BitVec 128) where + args input := [.scalar (.i64 input.1.1), .scalar (.i64 input.1.2), + .scalar (.i64 input.2.1), .scalar (.i64 input.2.2)] + results value := [.scalar (.v128 value)] + pre _ _ := True + footprint _ := [] + post input before after value := after = before ∧ + value = pack128 (product input.1.1 input.2.1) (product input.1.2 input.2.2) + """, """ + set_option maxRecDepth 8192 in + /-- The compiled SSE2 method multiplies the low halves for all four arguments. -/ + theorem correctness : evenProducts.Holds Extracted.program 0 := by + rintro ⟨⟨a, b⟩, c, d⟩ memory - + apply MethodContract.Holds.intro (c := evenProducts) (input := ((a, b), (c, d))) memory + (pack128 (product a c) (product b d)) ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + refine ⟨64, ?_⟩ + change invoke Extracted.program 64 0 _ memory = + .ok (memory, [.scalar (.v128 (pack128 (product a c) (product b d)))]) + unfold product + rw [← sseMultiplyEven32_pack128] + rfl + """, """ + set_option maxRecDepth 8192 + /-- Each high half and its low half contribute different products at this input. -/ + private def witness : (W64 × W64) × (W64 × W64) := + ((0x100000002, 0x300000004), (0x500000006, 0x700000008)) + + /-- The observed high-half products violate the low-half postcondition. -/ + private theorem wrongProducts : + ¬ evenProducts.Returns witness Memory.empty Memory.empty [.scalar (.v128 (pack128 5 21))] := by + rintro ⟨value, encoded, -, rfl⟩ + exact (by decide : ¬ ([CIL.Safety.Value.scalar (.v128 (pack128 5 21))] = + [.scalar (.v128 (pack128 12 32))])) encoded + + #print axioms wrongProducts + + /-- Selecting the odd UInt32 elements instead returns the two high-half products. -/ + theorem correctness : evenProducts.Refuted Extracted.program 0 := by + have ran : invoke Extracted.program 64 0 (evenProducts.args witness) Memory.empty = + .ok (Memory.empty, [.scalar (.v128 (pack128 5 21))]) := by + simp only [evenProducts, witness] + cil_checked_steps [] <;> rfl + exact MethodContract.Refuted.of_return (c := evenProducts) (input := witness) + (program := Extracted.program) (fuel := 64) (method := 0) (final := Memory.empty) + (values := [.scalar (.v128 (pack128 5 21))]) trivial ran (fun h => wrongProducts h.2) + """, profile: "x64-sse42", imports: "import CIL.SIMD.PackedLanes"); +Console.WriteLine("PASS SSE2 even-lane product contract and certified odd-lane replacement"); // Without SSE2 the reachable call is rejected before any evidence is written. var multiplyRejectedOutput = Path.Combine(root, "MultiplyEven128-scalar"); Directory.CreateDirectory(multiplyRejectedOutput); @@ -733,22 +1654,10 @@ end ExampleProof } Console.WriteLine("PASS MultiplyEven128 rejected under scalar"); // Forged evidence must not substitute the 256-bit multiply or another exact SSE call for PMULUDQ. -var rejectedMultiplies = new RejectedEvidence(Path.Combine(root, "RejectedMultiply")); -var multiplySource = NarrowEvidence(File.ReadAllText(Path.Combine(multiplyOutput, "Extracted.lean")), - "method0_intrinsics_validated"); -if (!multiplySource.Contains(".sse .multiplyEven32", StringComparison.Ordinal)) -{ - throw new InvalidOperationException("Missing multiply mutation: .sse .multiplyEven32"); -} -foreach ((string after, int index) in new[] { (".avx2 .multiplyEven32", 0), (".sse .shuffleBytes", 1) }) -{ - var path = Path.Combine(root, "RejectedMultiply", $"MultiplyEven128-{index}.lean"); - Directory.CreateDirectory(Path.GetDirectoryName(path)!); - File.WriteAllText(path, multiplySource.Replace(".sse .multiplyEven32", after, StringComparison.Ordinal)); - rejectedMultiplies.Add(path, "PASS forged MultiplyEven128 evidence rejected by ExactIntrinsicsValid", - "ExactIntrinsicsValid"); -} -rejectedMultiplies.Check(); +RejectHardware("MultiplyEven128", "MultiplyEven128", "Sse2::Multiply(", "multiplyEven32", "wide"); +RejectHardware("MultiplyEven128", "MultiplyEven128", "Sse2::Multiply(", "shuffle128", "shuffle"); +BuildLeanTestLibraries("Checking replacement hardware method signatures", "+Tests.Support.IntrinsicMethods"); +rejectedHardware.Check(FixtureLeanPath()); // Runs the compiled example on the host CPU and returns the vector's bytes, lane zero first. static byte[] RuntimeShuffle(MethodInfo method, byte[] source, byte[] control) diff --git a/Tests/ExactIntrinsics.lean b/Tests/ExactIntrinsics.lean index 16f0e20fa..06d63d4b4 100644 --- a/Tests/ExactIntrinsics.lean +++ b/Tests/ExactIntrinsics.lean @@ -1,177 +1,341 @@ import Lean -import CIL.Extraction.Translation +import CIL.Extraction.DecodedMethod import CIL.SIMD.UnspecifiedLanes +import CIL.Safety.IntrinsicExecution +import Tests.Support +import Tests.Support.RawDecoding open CIL CIL.Extraction namespace Tests.ExactIntrinsics -private def scope := "System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" -private def runtimeScope := "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" +private def scope := + "System.Runtime.Intrinsics, Version=11.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" +private def runtimeScope := + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" private def vectorType (width : String) : TypeExpr := .apply (.constructor scope ("System.Runtime.Intrinsics.Vector" ++ width ++ "`1")) (.atom runtimeScope "System.UInt64") private def profile : FeatureProfile := { architecture := .x64, x86Base64 := true, sse2 := true, ssse3 := true, sse41 := true, sse42 := true, avx := true, avx2 := true, bmi1 := true, bmi2 := true, avx512F := true, avx512FVL := true, avx512DQ := true, avx512DQVL := true, vector128Accelerated := true, vector256Accelerated := true } -private def body (operations : List Op) : Method := { code := operations, locals := [], returnsValue := true, profile } +private def body (operations : List Instruction) : Method := + { code := operations, locals := [], returnsValue := true, profile } private def raw (site : CallSite) : RawMethod := { token := 1, owner := "Fixture", hasThis := false, explicitThis := false, callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], initLocals := false, maxStack := 2, genericParameters := [], code := [⟨0, 40, .call site⟩] } +/-- A resolving call keeps its reference and executes the expected operation. -/ +private def keeps (site : CallSite) (expected : RuntimeModel) (features := profile) : Bool := + runtimeModel {} .call site.method == some expected && + decide ((decodeRaw features {} [] [true] (raw site) []).map Method.code = + .ok [.call site.method {}]) + +/-- A live unresolved operand produces its own opcode's hole and prevents method construction. -/ +private def refuses (site : CallSite) (features := profile) : Bool := + methodCode features {} [] [true] (raw site) == [.unadmitted 40] && + decide ((decodeRaw features {} [] [true] (raw site) []).map Method.code = liveHoleAt 0 40) + private def call0 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := "And", - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 System.Runtime.Intrinsics.X86.Avx2::And(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1)"), typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "256", parameterTypes := [vectorType "256", vectorType "256"] } -example : ExactIntrinsicsValid (raw call0) [true] - (body [.intrinsic (.avx2 .band) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid - (raw { call0 with returnType := .atom runtimeScope "System.UInt64" }) - [true] (body [.intrinsic (.avx2 .band) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid - (raw { call0 with parameterTypes := [vectorType "128", vectorType "256"] }) - [true] (body [.intrinsic (.avx2 .band) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call0) [true] - (body [.intrinsic (.avx2 .band) 3]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call0 with hasThis := true }) [true] - (body [.intrinsic (.avx2 .band) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call0 with target := .external "forged" }) [true] - (body [.intrinsic (.avx2 .band) 2]) := by decide +kernel + declared := { + convention := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "256"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := name!"And", + hasThis := false, genericArity := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "256"] }, + name := "And", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 \ + System.Runtime.Intrinsics.X86.Avx2::And(System.Runtime.Intrinsics.Vector256`1,\ + System.Runtime.Intrinsics.Vector256`1)"), + typeArguments := [], parameterCount := 2, returnsValue := true } +example : keeps call0 (.intrinsic (.avx2 .band) 2) := by decide +kernel +example : refuses { call0 with method.returnType := .atom runtimeScope "System.UInt64" } := by + decide +kernel +example : refuses { call0 with method.parameters := [vectorType "128", vectorType "256"] } := by + decide +kernel +example : runtimeModel {} .call call0.method ≠ some (.intrinsic (.avx2 .band) 3) := by + decide +kernel +example : refuses { call0 with method.hasThis := true } := by decide +kernel +example : refuses { call0 with target := .external "forged" } := by decide +kernel private def call1 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := "Or", - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 System.Runtime.Intrinsics.X86.Avx2::Or(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1)"), typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "256", parameterTypes := [vectorType "256", vectorType "256"] } -example : ExactIntrinsicsValid (raw call1) [true] - (body [.intrinsic (.avx2 .bor) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call1) [true] - (body [.intrinsic (.avx2 .bor) 3]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call1 with hasThis := true }) [true] - (body [.intrinsic (.avx2 .bor) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call1 with target := .external "forged" }) [true] - (body [.intrinsic (.avx2 .bor) 2]) := by decide +kernel + declared := { + convention := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "256"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := name!"Or", hasThis := false, + genericArity := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "256"] }, + name := "Or", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 \ + System.Runtime.Intrinsics.X86.Avx2::Or(System.Runtime.Intrinsics.Vector256`1,\ + System.Runtime.Intrinsics.Vector256`1)"), + typeArguments := [], parameterCount := 2, returnsValue := true } +example : keeps call1 (.intrinsic (.avx2 .bor) 2) := by decide +kernel +example : runtimeModel {} .call call1.method ≠ some (.intrinsic (.avx2 .bor) 3) := by decide +kernel +example : refuses { call1 with method.hasThis := true } := by decide +kernel +example : refuses { call1 with target := .external "forged" } := by decide +kernel private def call2 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := "Xor", - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 System.Runtime.Intrinsics.X86.Avx2::Xor(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1)"), typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "256", parameterTypes := [vectorType "256", vectorType "256"] } -example : ExactIntrinsicsValid (raw call2) [true] - (body [.intrinsic (.avx2 .bxor) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call2) [true] - (body [.intrinsic (.avx2 .bxor) 3]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call2 with hasThis := true }) [true] - (body [.intrinsic (.avx2 .bxor) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call2 with target := .external "forged" }) [true] - (body [.intrinsic (.avx2 .bxor) 2]) := by decide +kernel + declared := { + convention := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "256"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := name!"Xor", + hasThis := false, genericArity := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "256"] }, + name := "Xor", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 \ + System.Runtime.Intrinsics.X86.Avx2::Xor(System.Runtime.Intrinsics.Vector256`1,\ + System.Runtime.Intrinsics.Vector256`1)"), + typeArguments := [], parameterCount := 2, returnsValue := true } +example : keeps call2 (.intrinsic (.avx2 .bxor) 2) := by decide +kernel +example : runtimeModel {} .call call2.method ≠ some (.intrinsic (.avx2 .bxor) 3) := by + decide +kernel +example : refuses { call2 with method.hasThis := true } := by decide +kernel +example : refuses { call2 with target := .external "forged" } := by decide +kernel private def call3 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := "AndNot", - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 System.Runtime.Intrinsics.X86.Avx2::AndNot(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1)"), typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "256", parameterTypes := [vectorType "256", vectorType "256"] } -example : ExactIntrinsicsValid (raw call3) [true] - (body [.intrinsic (.avx2 .andNot) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call3) [true] - (body [.intrinsic (.avx2 .andNot) 3]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call3 with hasThis := true }) [true] - (body [.intrinsic (.avx2 .andNot) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call3 with target := .external "forged" }) [true] - (body [.intrinsic (.avx2 .andNot) 2]) := by decide +kernel + declared := { + convention := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "256"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := name!"AndNot", + hasThis := false, genericArity := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "256"] }, + name := "AndNot", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 \ + System.Runtime.Intrinsics.X86.Avx2::AndNot(\ + System.Runtime.Intrinsics.Vector256`1,\ + System.Runtime.Intrinsics.Vector256`1)"), + typeArguments := [], parameterCount := 2, returnsValue := true } +example : keeps call3 (.intrinsic (.avx2 .andNot) 2) := by decide +kernel +example : runtimeModel {} .call call3.method ≠ some (.intrinsic (.avx2 .andNot) 3) := by + decide +kernel +example : refuses { call3 with method.hasThis := true } := by decide +kernel +example : refuses { call3 with target := .external "forged" } := by decide +kernel private def call4 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := "ShiftLeftLogical" - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 System.Runtime.Intrinsics.X86.Avx2::ShiftLeftLogical(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector128`1)"), typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "256", parameterTypes := [vectorType "256", vectorType "128"] } -example : ExactIntrinsicsValid (raw call4) [true] - (body [.intrinsic (.avx2 .shl64Count) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call4) [true] - (body [.intrinsic (.avx2 .shl64Count) 3]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call4 with hasThis := true }) [true] - (body [.intrinsic (.avx2 .shl64Count) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call4 with target := .external "forged" }) [true] - (body [.intrinsic (.avx2 .shl64Count) 2]) := by decide +kernel + declared := { + convention := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "128"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := name!"ShiftLeftLogical", + hasThis := false, genericArity := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "128"] }, + name := "ShiftLeftLogical", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 \ + System.Runtime.Intrinsics.X86.Avx2::ShiftLeftLogical(\ + System.Runtime.Intrinsics.Vector256`1,\ + System.Runtime.Intrinsics.Vector128`1)"), + typeArguments := [], parameterCount := 2, returnsValue := true } +example : keeps call4 (.intrinsic (.avx2 .shl64Count) 2) := by decide +kernel +example : runtimeModel {} .call call4.method ≠ some (.intrinsic (.avx2 .shl64Count) 3) := by + decide +kernel +example : refuses { call4 with method.hasThis := true } := by decide +kernel +example : refuses { call4 with target := .external "forged" } := by decide +kernel private def call5 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := "ShiftRightLogical" - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 System.Runtime.Intrinsics.X86.Avx2::ShiftRightLogical(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector128`1)"), typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "256", parameterTypes := [vectorType "256", vectorType "128"] } -example : ExactIntrinsicsValid (raw call5) [true] - (body [.intrinsic (.avx2 .shr64Count) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call5) [true] - (body [.intrinsic (.avx2 .shr64Count) 3]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call5 with hasThis := true }) [true] - (body [.intrinsic (.avx2 .shr64Count) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call5 with target := .external "forged" }) [true] - (body [.intrinsic (.avx2 .shr64Count) 2]) := by decide +kernel + declared := { + convention := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "128"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := name!"ShiftRightLogical", + hasThis := false, genericArity := 0, returnType := vectorType "256", + parameters := [vectorType "256", vectorType "128"] }, + name := "ShiftRightLogical", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 \ + System.Runtime.Intrinsics.X86.Avx2::ShiftRightLogical(\ + System.Runtime.Intrinsics.Vector256`1,\ + System.Runtime.Intrinsics.Vector128`1)"), + typeArguments := [], parameterCount := 2, returnsValue := true } +example : keeps call5 (.intrinsic (.avx2 .shr64Count) 2) := by decide +kernel +example : runtimeModel {} .call call5.method ≠ some (.intrinsic (.avx2 .shr64Count) 3) := by + decide +kernel +example : refuses { call5 with method.hasThis := true } := by decide +kernel +example : refuses { call5 with target := .external "forged" } := by decide +kernel private def call6 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx512F", name := "CompareLessThan", - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector512`1 System.Runtime.Intrinsics.X86.Avx512F::CompareLessThan(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1)"), typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "512", parameterTypes := [vectorType "512", vectorType "512"] } -example : ExactIntrinsicsValid (raw call6) [true] - (body [.intrinsic (.avx512 .ltu512) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call6) [true] - (body [.intrinsic (.avx512 .ltu512) 3]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call6 with hasThis := true }) [true] - (body [.intrinsic (.avx512 .ltu512) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call6 with target := .external "forged" }) [true] - (body [.intrinsic (.avx512 .ltu512) 2]) := by decide +kernel + declared := { + convention := 0, returnType := vectorType "512", + parameters := [vectorType "512", vectorType "512"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx512F", name := name!"CompareLessThan", + hasThis := false, genericArity := 0, returnType := vectorType "512", + parameters := [vectorType "512", vectorType "512"] }, + name := "CompareLessThan", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector512`1 \ + System.Runtime.Intrinsics.X86.Avx512F::CompareLessThan(\ + System.Runtime.Intrinsics.Vector512`1,\ + System.Runtime.Intrinsics.Vector512`1)"), + typeArguments := [], parameterCount := 2, returnsValue := true } +example : keeps call6 (.intrinsic (.avx512 .ltu512) 2) := by decide +kernel +example : runtimeModel {} .call call6.method ≠ some (.intrinsic (.avx512 .ltu512) 3) := by + decide +kernel +example : refuses { call6 with method.hasThis := true } := by decide +kernel +example : refuses { call6 with target := .external "forged" } := by decide +kernel private def call7 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx512F", name := "CompareGreaterThan", - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector512`1 System.Runtime.Intrinsics.X86.Avx512F::CompareGreaterThan(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1)"), typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "512", parameterTypes := [vectorType "512", vectorType "512"] } -example : ExactIntrinsicsValid (raw call7) [true] - (body [.intrinsic (.avx512 .gtu512) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call7) [true] - (body [.intrinsic (.avx512 .gtu512) 3]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call7 with hasThis := true }) [true] - (body [.intrinsic (.avx512 .gtu512) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call7 with target := .external "forged" }) [true] - (body [.intrinsic (.avx512 .gtu512) 2]) := by decide +kernel + declared := { + convention := 0, returnType := vectorType "512", + parameters := [vectorType "512", vectorType "512"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx512F", + name := name!"CompareGreaterThan", hasThis := false, genericArity := 0, + returnType := vectorType "512", parameters := [vectorType "512", vectorType "512"] }, + name := "CompareGreaterThan", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector512`1 \ + System.Runtime.Intrinsics.X86.Avx512F::CompareGreaterThan(\ + System.Runtime.Intrinsics.Vector512`1,\ + System.Runtime.Intrinsics.Vector512`1)"), + typeArguments := [], parameterCount := 2, returnsValue := true } +example : keeps call7 (.intrinsic (.avx512 .gtu512) 2) := by decide +kernel +example : runtimeModel {} .call call7.method ≠ some (.intrinsic (.avx512 .gtu512) 3) := by + decide +kernel +example : refuses { call7 with method.hasThis := true } := by decide +kernel +example : refuses { call7 with target := .external "forged" } := by decide +kernel private def call8 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx512DQ", name := "MoveMask" - target := .external (scope ++ "::System.Int32 System.Runtime.Intrinsics.X86.Avx512DQ::MoveMask(System.Runtime.Intrinsics.Vector512`1)"), typeArguments := [], parameterCount := 1, hasThis := false, returnsValue := true, returnType := .atom runtimeScope "System.Int32", parameterTypes := [vectorType "512"] } -example : ExactIntrinsicsValid (raw call8) [true] - (body [.intrinsic (.avx512DQ .moveMask512) 1]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call8) [true] - (body [.intrinsic (.avx512DQ .moveMask512) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call8 with hasThis := true }) [true] - (body [.intrinsic (.avx512DQ .moveMask512) 1]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call8 with target := .external "forged" }) [true] - (body [.intrinsic (.avx512DQ .moveMask512) 1]) := by decide +kernel + declared := { + convention := 0, returnType := .atom runtimeScope "System.Int32", + parameters := [vectorType "512"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx512DQ", name := name!"MoveMask", + hasThis := false, genericArity := 0, returnType := .atom runtimeScope "System.Int32", + parameters := [vectorType "512"] }, + name := "MoveMask", + target := .external (scope ++ "::System.Int32 System.Runtime.Intrinsics.X86.Avx512DQ::MoveMask(\ + System.Runtime.Intrinsics.Vector512`1)"), + typeArguments := [], parameterCount := 1, returnsValue := true } +example : keeps call8 (.intrinsic (.avx512DQ .moveMask512) 1) := by decide +kernel +example : runtimeModel {} .call call8.method ≠ some (.intrinsic (.avx512DQ .moveMask512) 2) := by + decide +kernel +example : refuses { call8 with method.hasThis := true } := by decide +kernel +example : refuses { call8 with target := .external "forged" } := by decide +kernel private def call9 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.Vector512", name := "Create", - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector512`1 System.Runtime.Intrinsics.Vector512::Create(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1)"), typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "512", parameterTypes := [vectorType "256", vectorType "256"] } -example : ExactIntrinsicsValid (raw call9) [true] - (body [.intrinsic (.vector .combine256) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call9) [true] - (body [.intrinsic (.vector .combine256) 3]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call9 with hasThis := true }) [true] - (body [.intrinsic (.vector .combine256) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call9 with target := .external "forged" }) [true] - (body [.intrinsic (.vector .combine256) 2]) := by decide +kernel + declared := { + convention := 0, returnType := vectorType "512", + parameters := [vectorType "256", vectorType "256"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.Vector512", name := name!"Create", + hasThis := false, genericArity := 0, returnType := vectorType "512", + parameters := [vectorType "256", vectorType "256"] }, + name := "Create", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector512`1 \ + System.Runtime.Intrinsics.Vector512::Create(\ + System.Runtime.Intrinsics.Vector256`1,\ + System.Runtime.Intrinsics.Vector256`1)"), + typeArguments := [], parameterCount := 2, returnsValue := true } +example : runtimeModel {} .call call9.method = some (.intrinsic (.vector .combine256) 2) := by + decide +kernel +example : runtimeCatalog.siteAgrees {} profile ⟨0, 40, .call call9⟩ (.call call9.method {}) := by + decide +kernel +example : ¬ RuntimeModelsAgree (raw call9) [true] + (body [.call call0.method]) := by decide +kernel +example : ¬ RuntimeModelsAgree (raw call9) [true] + (body [.call { call9.method with parameters := call9.method.parameters ++ [call9.method.returnType] }]) := by decide +kernel +example : ¬ RuntimeModelsAgree (raw { call9 with method.hasThis := true }) [true] + (body [.call call9.method]) := by decide +kernel +example : ¬ RuntimeModelsAgree (raw { call9 with target := .external "forged" }) [true] + (body [.call call9.method]) := by decide +kernel +/-- The same vector equality declaration instantiated at the supplied owner element. -/ +private def equalitySite (width : Nat) (element : String) : CallSite := + let head := s!"System.Runtime.Intrinsics.Vector{width}`1" + let vector := TypeExpr.apply (.constructor scope head) (.atom runtimeScope element) + let openVector := TypeExpr.apply (.constructor scope head) (.ownerParameter 0) + let boolean := TypeExpr.atom runtimeScope "System.Boolean" + let parameters := s!"{head},{head}" + let target := s!"{scope}::System.Boolean {head}<{element}>::op_Equality({parameters})" + let method : MethodRef := + { owner := vector, name := name!"op_Equality", hasThis := false, genericArity := 0, + returnType := boolean, parameters := [vector, vector] } + let declared : OpenSignature := + { convention := 0, returnType := boolean, parameters := [openVector, openVector] } + { method, declared, + name := "op_Equality", target := .external target, returnsValue := true, + parameterCount := 2, typeArguments := [] } + +/-- Integer equality keeps its call and a floating element at the same declaration is refused. -/ +theorem equalityElements : + [128, 256, 512].all (fun width => + ["System.Byte", "System.Int32", "System.UInt32", "System.Int64", "System.UInt64", + "System.Single", "System.Double"].all fun element => + let site := equalitySite width element + let expected := if element ∈ ["System.Single", "System.Double"] then liveHoleAt 0 40 + else .ok [Instruction.call site.method {}] + decide ((decodeRaw .scalar {} [] [true] (raw site) []).map Method.code = + expected)) = true := by decide +kernel + +#print axioms equalityElements + private def call10 : CallSite := { - declaringType := .atom scope "System.Runtime.Intrinsics.Vector128", name := "CreateScalarUnsafe", - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector128`1 System.Runtime.Intrinsics.Vector128::CreateScalarUnsafe(System.UInt64)"), typeArguments := [], parameterCount := 1, hasThis := false, returnsValue := true, returnType := vectorType "128", parameterTypes := [.atom runtimeScope "System.UInt64"] } -example : ExactIntrinsicsValid (raw call10) [true] - (body [.intrinsic (.vector .createScalarUnsafe64) 1]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call10) [true] - (body [.intrinsic (.vector .createScalarUnsafe64) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call10 with hasThis := true }) [true] - (body [.intrinsic (.vector .createScalarUnsafe64) 1]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call10 with target := .external "forged" }) [true] - (body [.intrinsic (.vector .createScalarUnsafe64) 1]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call10 with target := .external "forged" }) [true] - (body [.intrinsic (.vector (.createScalar64 128)) 1]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw { call0 with parameterCount := 1 }) [true] - (body [.intrinsic (.vector (.band 256)) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call0) [true] - (body [.intrinsic (.avx2 .bor) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call4) [true] - (body [.intrinsic (.avx2 .shl64) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call6) [true] - (body [.intrinsic (.avx512 .gtu512) 2]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call10) [true] - (body [.intrinsic (.vector (.createScalar64 128)) 1]) := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call0) [true] - { body [.intrinsic (.avx2 .band) 2] with profile := .scalar } := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call6) [true] - { body [.intrinsic (.avx512 .ltu512) 2] with profile := { profile with avx512F := false } } := by decide +kernel -example : ¬ ExactIntrinsicsValid (raw call8) [true] - { body [.intrinsic (.avx512DQ .moveMask512) 1] with profile := { profile with avx512DQ := false } } := by decide +kernel + declared := { + convention := 0, returnType := vectorType "128", + parameters := [.atom runtimeScope "System.UInt64"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.Vector128", name := name!"CreateScalarUnsafe", + hasThis := false, genericArity := 0, returnType := vectorType "128", + parameters := [.atom runtimeScope "System.UInt64"] }, + name := "CreateScalarUnsafe", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector128`1 \ + System.Runtime.Intrinsics.Vector128::CreateScalarUnsafe(System.UInt64)"), + typeArguments := [], parameterCount := 1, returnsValue := true } +-- The emitted call must name the member and instantiation of the original operand. +example : RuntimeModelsAgree (raw call10) [true] (body [.call call10.method {}]) := by + decide +kernel +example : ¬ RuntimeModelsAgree (raw call10) [true] + (body [.call call0.method]) := by decide +kernel +example : ¬ RuntimeModelsAgree (raw call0) [true] + (body [.call createScalarUnsafeMethod]) := by decide +kernel +example : ¬ RuntimeModelsAgree (raw call10) [true] + (body [.call getElement128Method]) := by decide +kernel +-- CreateScalar initializes the upper lane. Its checked call cannot replace CreateScalarUnsafe. +private def createScalar : MethodRef := { createScalarUnsafeMethod with name := name!"CreateScalar" } +example : call10.method = createScalarUnsafeMethod := by decide +kernel +example : runtimeModel {} .call createScalar = some (.intrinsic (.vector (.createScalar64 128)) 1) := rfl +example : ¬ ModelsAgree runtimeCatalog {} profile (raw call10) [true] + [.call createScalar] := by decide +kernel +example : ¬ ModelsAgree runtimeCatalog {} profile + (raw { call10 with target := .external "forged" }) [true] + [.call createScalar] := by decide +kernel +example : refuses { call0 with parameterCount := 1 } := by decide +kernel +example : runtimeModel {} .call call0.method ≠ some (.intrinsic (.avx2 .bor) 2) := by decide +kernel +example : runtimeModel {} .call call4.method ≠ some (.intrinsic (.avx2 .shl64) 2) := by + decide +kernel +example : runtimeModel {} .call call6.method ≠ some (.intrinsic (.avx512 .gtu512) 2) := by + decide +kernel +example : refuses call0 (.scalar) := by decide +kernel +example : refuses call6 ({ profile with avx512F := false }) := by decide +kernel +example : refuses call8 ({ profile with avx512DQ := false }) := by decide +kernel example : Intrinsic.available profile (.avx512 .ltu512) = true := by decide +kernel example : Intrinsic.available profile (.avx512DQ .moveMask512) = true := by decide +kernel +-- A kept hardware call is refused at the evidence level under a profile lacking its feature. +example : RuntimeModelsAgree (raw call0) [true] (body [.call call0.method {}]) := by + decide +kernel +example : ¬ RuntimeModelsAgree (raw call0) [true] + { body [.call call0.method {}] with profile := .scalar } := by decide +kernel +example : ¬ RuntimeModelsAgree (raw call6) [true] + { body [.call call6.method {}] with profile := { profile with avx512F := false } } := by + decide +kernel +example : ¬ RuntimeModelsAgree (raw call8) [true] + { body [.call call8.method {}] with profile := { profile with avx512DQ := false } } := by + decide +kernel +-- A link at a hardware site keeps the feature requirement of the model the site resolves to. +example : RuntimeModelsAgree (raw call0) [true] + { body [.call call0.method {}] with links := [(0, .direct 0)] } := by decide +kernel +example : ¬ RuntimeModelsAgree (raw call0) [true] + { body [.call call0.method {}] with links := [(0, .direct 0)], profile := .scalar } := by + decide +kernel -example : ¬ ExactIntrinsicsValid (raw call0) [true] - { body [.intrinsic (.avx2 .band) 2] with profile := { profile with architecture := .arm64 } } := by decide +kernel +example : ¬ ({ profile with architecture := .arm64 } : FeatureProfile).Valid := by decide +kernel private def vector128 : TypeExpr := .apply (.constructor scope "System.Runtime.Intrinsics.Vector128`1") (.atom runtimeScope "System.UInt64") private def withLocal : RawMethod := { raw call10 with locals := [vector128], code := [] } -private def localBody : Method := { body [.intrinsic (.vector .createScalarUnsafe64) 1] with locals := [.unmodeled], localKinds := [.vector128], opaqueVectorLocals := [0] } +private def createsBody : Method := body [.call createScalarUnsafeMethod {}] + +/-- Masking the creator removes the opaque local from the decoded frame. -/ +theorem decoded_opaque_mask : + let source := { raw call10 with locals := [vector128] } + let slots := fun live => (Method.ofRaw? profile {} [] [live] source []).map + Method.opaqueVectorLocals + slots true = some [0] ∧ slots false = some [] := by + exact ⟨rfl, rfl⟩ +#print axioms decoded_opaque_mask +private def localBody : Method := + { createsBody with + locals := [.unmodeled], localKinds := [.vector128], opaqueVectorLocals := [0] } example : OpaqueLocalsValid withLocal localBody := by decide +kernel example : OpaqueLocalsValid withLocal { localBody with code := [], opaqueVectorLocals := [] } := by decide +kernel @@ -183,10 +347,15 @@ example : ¬ OpaqueLocalsValid { withLocal with code := [⟨0, 18, .local 0⟩] example : ¬ OpaqueLocalsValid { withLocal with code := [⟨0, 65037, .local 0⟩] } localBody := by decide +kernel example : ¬ OpaqueLocalsValid withLocal - { localBody with code := [.intrinsic (.vector .createScalarUnsafe64) 1, .localAddr 0] } := by decide +kernel + { localBody with code := [.call createScalarUnsafeMethod {}, .ldloca 0] } := by + decide +kernel +-- A different closed member cannot claim the opaque local of CreateScalarUnsafe. +example : ¬ OpaqueLocalsValid withLocal + { localBody with code := [.call getElement128Method] } := by decide +kernel example : ¬ OpaqueLocalsValid withLocal { localBody with localKinds := [.word64] } := by decide +kernel -example : ¬ OpaqueLocalsValid { withLocal with locals := [.atom runtimeScope "System.UInt64"] } localBody := by decide +kernel +example : ¬ OpaqueLocalsValid { withLocal with locals := [.atom runtimeScope "System.UInt64"] } localBody := by + decide +kernel example : evalIntrinsic (.avx2 .andNot) [.v256 1, .v256 3] = some (.v256 2) := by decide +kernel example : evalIntrinsic (.vector .combine256) [.v256 1, .v256 2] = some (.v512 ((2 : BitVec 256) ++ (1 : BitVec 256))) := rfl @@ -211,9 +380,8 @@ example : Safety.bitcastValue .vector128 .vector128 (.v128Low64 7) = .error (.unsupported "Bitcast observes unspecified vector bits") := rfl example (memory : Safety.Memory) : Safety.storeLocal memory (.opaque128 none) (.scalar (.v128Low64 7)) = .ok (.opaque128 (some (.low64 7)), memory) := rfl -example (memory : Safety.Memory) : Safety.makeArgumentHomes 0 [0] [.scalar (.v128Low64 7)] memory = - .error (.unsupported "Argument home observes unspecified vector bits") := rfl -example (memory : Safety.Memory) : Safety.instruction .eq [.scalar (.v128Low64 7), .scalar (.v128Low64 7)] memory = +example (memory : Safety.Memory) : + Safety.instruction .ceq [.scalar (.v128Low64 7), .scalar (.v128Low64 7)] memory = .error (.unsupported "Instruction observes unspecified vector bits") := rfl theorem unspecified_return (body : Method) (memory : Safety.Memory) (frame : Safety.Frame) : @@ -221,13 +389,19 @@ theorem unspecified_return (body : Method) (memory : Safety.Memory) (frame : Saf .error (.unsupported "Instruction observes unspecified vector bits") := rfl #print axioms unspecified_return -theorem unspecified_call (body : Method) (memory : Safety.Memory) (frame : Safety.Frame) : - Safety.step body (.call 1 2) 0 [] frame [.scalar (.i64 0), .scalar (.v128Low64 7)] memory = - .error (.unsupported "Instruction observes unspecified vector bits") := rfl +theorem unspecified_call (body : Method) (link : Link) (linked : body.linkAt 0 = some link) + (memory : Safety.Memory) (frame : Safety.Frame) : + Safety.step body (.call (Tests.callee 2)) 0 [] frame [.scalar (.i64 0), .scalar (.v128Low64 7)] + memory = + .error (.unsupported "Instruction observes unspecified vector bits") := by + simp only [Safety.step, Safety.observerCount, linked] + rfl #print axioms unspecified_call -theorem unspecified_call_rest (body : Method) (memory : Safety.Memory) (frame : Safety.Frame) : - Safety.step body (.call 1 1) 0 [] frame [.scalar (.i64 3), .scalar (.v128Low64 7)] memory = +theorem unspecified_call_rest (memory : Safety.Memory) (frame : Safety.Frame) : + Safety.step { body [.call (Tests.callee 1)] with links := [(0, .direct 1)] } + (.call (Tests.callee 1)) 0 [] frame + [.scalar (.i64 3), .scalar (.v128Low64 7)] memory = .ok (.call 1 [.scalar (.i64 3)] [.scalar (.v128Low64 7)] memory) := rfl #print axioms unspecified_call_rest @@ -237,7 +411,7 @@ theorem defined_vector_return (memory : Safety.Memory) (frame : Safety.Frame) (b #print axioms defined_vector_return theorem unspecified_constructor (body : Method) (memory : Safety.Memory) (frame : Safety.Frame) : - Safety.step body (.newAggregate 1 1 16) 0 [] frame [.scalar (.v128Low64 7)] memory = + Safety.step body (.newobj (.arity 1)) 0 [] frame [.scalar (.v128Low64 7)] memory = .error (.unsupported "Instruction observes unspecified vector bits") := rfl #print axioms unspecified_constructor @@ -253,7 +427,8 @@ example : Vector.lane64 (Vector.shiftLeftCount64 (Vector.pack256 1 1 1 1) 1) 3 = := by decide +kernel example : Vector.lane64 (Vector.shiftRightCount64 (Vector.pack256 1 1 1 1) 1) 3 = 0 := by decide +kernel -example : Vector.lane64 (Vector.shiftLeftCount64 (Vector.pack256 1 1 1 1) 63) 3 = 9223372036854775808 := by decide +kernel +example : Vector.lane64 (Vector.shiftLeftCount64 (Vector.pack256 1 1 1 1) 63) 3 = 9223372036854775808 := by + decide +kernel example : Vector.lane64 (Vector.shiftRightCount64 (Vector.pack256 1 1 1 1) 63) 3 = 0 := by decide +kernel example : Vector.lane64 (Vector.shiftLeftCount64 (Vector.pack256 1 1 1 1) 64) 3 = 0 @@ -268,8 +443,10 @@ example : Vector.lane64 (Vector.shiftLeftCount64 (Vector.pack256 1 1 1 1) 256) 3 := by decide +kernel example : Vector.lane64 (Vector.shiftRightCount64 (Vector.pack256 1 1 1 1) 256) 3 = 0 := by decide +kernel -example : Vector.lane64 (Vector.shiftLeftCount64 (Vector.pack256 1 1 1 1) 18446744073709551615) 3 = 0 := by decide +kernel -example : Vector.lane64 (Vector.shiftRightCount64 (Vector.pack256 1 1 1 1) 18446744073709551615) 3 = 0 := by decide +kernel +example : Vector.lane64 (Vector.shiftLeftCount64 (Vector.pack256 1 1 1 1) 18446744073709551615) 3 = 0 := by + decide +kernel +example : Vector.lane64 (Vector.shiftRightCount64 (Vector.pack256 1 1 1 1) 18446744073709551615) 3 = 0 := by + decide +kernel example : Vector.moveMask512 (Vector.pack512 9223372036854775808 0 0 0 0 0 0 0) = 1 := by decide +kernel example : Vector.moveMask512 (Vector.pack512 0 9223372036854775808 0 0 0 0 0 0) = 2 @@ -290,72 +467,137 @@ example : evalIntrinsic (.avx512 .ltu512) [.v512 (Vector.pack512 9223372036854775808 1 0 0 0 0 0 0), .v512 (Vector.pack512 1 1 0 0 0 0 0 0)] = some (.v512 0) := by decide +kernel -example : ExactIntrinsicsValid (raw { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx2" - name := "ShiftLeftLogical" - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 System.Runtime.Intrinsics.X86.Avx2::ShiftLeftLogical(System.Runtime.Intrinsics.Vector256`1,System.Byte)"), - typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "256", parameterTypes := [vectorType "256", .atom runtimeScope "System.Byte"] }) [true] - (body [.intrinsic (.avx2 .shl64) 2]) := by decide +kernel - -example : ExactIntrinsicsValid (raw { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx2" - name := "ShiftRightLogical" - target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 System.Runtime.Intrinsics.X86.Avx2::ShiftRightLogical(System.Runtime.Intrinsics.Vector256`1,System.Byte)"), - typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := vectorType "256", parameterTypes := [vectorType "256", .atom runtimeScope "System.Byte"] }) [true] - (body [.intrinsic (.avx2 .shr64) 2]) := by decide +kernel - -example : ExactIntrinsicsValid (raw { - declaringType := .atom scope "System.Runtime.Intrinsics.X86.Avx512DQ" - name := "MoveMask" - target := .external (scope ++ "::System.Int32 System.Runtime.Intrinsics.X86.Avx512DQ::MoveMask(System.Runtime.Intrinsics.Vector256`1)"), - typeArguments := [], parameterCount := 1, hasThis := false, returnsValue := true, returnType := .atom runtimeScope "System.Int32", parameterTypes := [vectorType "256"] }) [true] - (body [.intrinsic (.avx512DQ .moveMask64) 1]) := by decide +kernel - - -private def initBody (type : TypeExpr) : RawMethod := - { raw call0 with code := [⟨0, 65045, .type type⟩] } - -example : VectorInitializationsValid (initBody (vectorType "128")) [true] - (body [.initAggregate 16]) := by decide +kernel -example : VectorInitializationsValid (initBody (vectorType "256")) [true] - (body [.initAggregate 32]) := by decide +kernel -example : VectorInitializationsValid (initBody (vectorType "512")) [true] - (body [.initAggregate 64]) := by decide +kernel -example : VectorInitializationsValid (initBody (vectorType "512")) [true] - { body [.initAggregate 64] with profile := .scalar } := by decide +kernel -example : VectorInitializationsValid (initBody (vectorType "256")) [true] - { body [.initAggregate 32] with profile := { architecture := .arm64 } } := by decide +kernel -example : ¬ VectorInitializationsValid (initBody (vectorType "128")) [true] - (body [.initAggregate 32]) := by decide +kernel -example : ¬ VectorInitializationsValid (initBody (vectorType "256")) [true] - (body [.initAggregate 16]) := by decide +kernel -example : ¬ VectorInitializationsValid (initBody (vectorType "512")) [true] - (body [.initAggregate 32]) := by decide +kernel -example : ¬ VectorInitializationsValid (initBody (vectorType "128")) [true] - (body [.nop]) := by decide +kernel -example : ¬ VectorInitializationsValid - { initBody (vectorType "128") with code := [⟨0, 113, .type (vectorType "128")⟩] } [true] - (body [.initAggregate 16]) := by decide +kernel -example : ¬ VectorInitializationsValid (initBody (.atom runtimeScope "System.UInt64")) [true] - (body [.initAggregate 16]) := by decide +kernel -example : ¬ VectorInitializationsValid - (initBody (.apply (.constructor "forged" "System.Runtime.Intrinsics.Vector128`1") (.atom runtimeScope "System.UInt64"))) - [true] (body [.initAggregate 16]) := by decide +kernel -example : ¬ VectorInitializationsValid - (initBody (.apply (.constructor scope "System.Runtime.Intrinsics.Vector128`1") (.atom "forged" "System.UInt64"))) - [true] (body [.initAggregate 16]) := by decide +kernel -example : ¬ VectorInitializationsValid - (initBody (.apply (.constructor scope "System.Runtime.Intrinsics.Vector128`1") (.atom runtimeScope "System.Int16"))) - [true] (body [.initAggregate 16]) := by decide +kernel -example : ¬ VectorInitializationsValid - (initBody (.apply (.constructor scope "System.Runtime.Intrinsics.Vector64`1") (.atom runtimeScope "System.UInt64"))) - [true] (body [.initAggregate 16]) := by decide +kernel -example : ¬ VectorInitializationsValid (initBody (vectorType "128")) [true] - { body [.initAggregate 16] with profile := { nativeWidth := 32 } } := by decide +kernel -example : ¬ VectorInitializationsValid (initBody (vectorType "128")) [] - (body [.initAggregate 16]) := by decide +kernel -example : ¬ VectorInitializationsValid (initBody (vectorType "128")) [true] - (body []) := by decide +kernel +example : keeps { + declared := { + convention := 0, returnType := vectorType "256", + parameters := [vectorType "256", .atom runtimeScope "System.Byte"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := name!"ShiftLeftLogical", + hasThis := false, genericArity := 0, returnType := vectorType "256", + parameters := [vectorType "256", .atom runtimeScope "System.Byte"] }, + name := "ShiftLeftLogical", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 \ + System.Runtime.Intrinsics.X86.Avx2::ShiftLeftLogical(\ + System.Runtime.Intrinsics.Vector256`1,System.Byte)"), + typeArguments := [], parameterCount := 2, returnsValue := true } + (.intrinsic (.avx2 .shl64) 2) := by decide +kernel + +example : keeps { + declared := { + convention := 0, returnType := vectorType "256", + parameters := [vectorType "256", .atom runtimeScope "System.Byte"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx2", name := name!"ShiftRightLogical", + hasThis := false, genericArity := 0, returnType := vectorType "256", + parameters := [vectorType "256", .atom runtimeScope "System.Byte"] }, + name := "ShiftRightLogical", + target := .external (scope ++ "::System.Runtime.Intrinsics.Vector256`1 \ + System.Runtime.Intrinsics.X86.Avx2::ShiftRightLogical(\ + System.Runtime.Intrinsics.Vector256`1,System.Byte)"), + typeArguments := [], parameterCount := 2, returnsValue := true } + (.intrinsic (.avx2 .shr64) 2) := by decide +kernel + +example : keeps { + declared := { + convention := 0, returnType := .atom runtimeScope "System.Int32", + parameters := [vectorType "256"] }, + method := { + owner := .atom scope "System.Runtime.Intrinsics.X86.Avx512DQ", name := name!"MoveMask", + hasThis := false, genericArity := 0, returnType := .atom runtimeScope "System.Int32", + parameters := [vectorType "256"] }, + name := "MoveMask", + target := .external (scope ++ "::System.Int32 System.Runtime.Intrinsics.X86.Avx512DQ::\ + MoveMask(System.Runtime.Intrinsics.Vector256`1)"), + typeArguments := [], parameterCount := 1, returnsValue := true } + (.intrinsic (.avx512DQ .moveMask64) 1) := by decide +kernel + + +/-- A method using the specified type at its only instruction. -/ +private def objectBody (opcode : Nat) (type : TypeExpr) : RawMethod := + { raw call0 with code := [⟨0, opcode, .type type⟩] } + +/-- Object admission checks vector storage on each profile and refuses malformed evidence. -/ +theorem init_vector_admission : + let unsigned := vectorType "128" + let emitted := body [.initobj unsigned] + let valid := fun operand => decide (ObjectOperandsValid (objectBody 0xfe15 operand) + [true] emitted) + let malformed := fun head element => TypeExpr.apply head element + ["128", "256", "512"].all (fun width => valid (vectorType width)) && + decide (ObjectOperandsValid (objectBody 0xfe15 unsigned) [true] + { emitted with profile := .scalar }) && + decide (ObjectOperandsValid (objectBody 0xfe15 unsigned) [true] + { emitted with profile := { architecture := .arm64 } }) && + !(valid (malformed (.constructor "forged" "System.Runtime.Intrinsics.Vector128`1") + (.atom runtimeScope "System.UInt64"))) && + !(valid (malformed (.constructor scope "System.Runtime.Intrinsics.Vector128`1") + (.atom "forged" "System.UInt64"))) && + !(valid (malformed (.constructor scope "System.Runtime.Intrinsics.Vector128`1") + (.atom runtimeScope "System.Int16"))) && + !(valid (vectorType "64")) && + !decide (ObjectOperandsValid (objectBody 0xfe15 unsigned) [true] + { emitted with profile := { nativeWidth := 32 } }) && + !decide (ObjectOperandsValid (objectBody 0xfe15 unsigned) [] emitted) && + !decide (ObjectOperandsValid (objectBody 0xfe15 unsigned) [true] (body [])) := by + decide +kernel +#print axioms init_vector_admission + +/-- An enum-span copy reads its own direct home and clears the reference flag on the value. -/ +theorem enum_span_copy : + let element := TypeExpr.enum "Signed8" (.atom runtimeScope "System.SByte") + let span := TypeExpr.apply (.constructor runtimeScope "System.Span`1") element + let load := RawInstruction.mk 0 2 .none + let copy := RawInstruction.mk 1 0x71 (.type span) + let input := { raw call0 with parameters := [.byref span], code := [load, copy] } + let emitted := body [.ldarg 0, .ldobj span] + let kind := ScalarKind.enumSpan (.enumeration "Signed8" .signed8) true + ObjectOperandsValid input [true, true] emitted ∧ + scalarTransfer input copy [kind true] = some [kind false] ∧ + scalarTransfer input copy [kind false] = none ∧ + ¬ ObjectOperandsValid { input with parameters := [.byref (.apply + (.constructor runtimeScope "System.Span`1") (.atom runtimeScope "System.Int32"))] } + [true, true] emitted ∧ + ¬ ObjectOperandsValid { input with code := [load, ⟨1, 0, .none⟩, copy] } + [true, true, true] (body [.ldarg 0, .nop, .ldobj span]) ∧ + ¬ ObjectOperandsValid { input with code := [load, copy, ⟨2, 43, .target 1⟩] } + [true, true, false] (body [.ldarg 0, .ldobj span, .unadmitted 43]) := by decide +kernel +#print axioms enum_span_copy + +/-- A valid load of a span does not admit a later sizeof of the same span type. -/ +theorem span_load_then_size_refused : + let span := TypeExpr.apply (.constructor runtimeScope "System.Span`1") + (.atom runtimeScope "System.Int32") + let source := { raw call0 with + parameters := [.byref span] + code := [⟨0, 2, .none⟩, ⟨1, 0x71, .type span⟩] } + let input := { source with + returnType := .atom runtimeScope "System.Int32" + code := source.code ++ [⟨6, 38, .none⟩, ⟨7, 0xfe1c, .type span⟩, ⟨13, 42, .none⟩] } + let emitted := body [.ldarg 0, .ldobj span, .pop, .sizeof span, .ret] + ObjectOperandsValid source [true, true] (body [.ldarg 0, .ldobj span]) ∧ + ¬ ObjectOperandsValid input [true, true, true, true, true] emitted := by decide +kernel +#print axioms span_load_then_size_refused + +/-- A literal site binds both opcode and token, including between two supported vectors. -/ +theorem init_vector_binding : + let target := vectorType "128" + let raw := RawInstruction.mk 0 0xfe15 (.type target) + [Instruction.nop, .initobj (vectorType "256"), .initobj (vectorType "512"), + .initobj (.atom runtimeScope "System.UInt64"), .ldobj target].all fun emitted => + !scalarEmissionMatches none none raw [] emitted := by decide +kernel +#print axioms init_vector_binding + +/-- Floating-point loads, class sizes and span stores fail admission. -/ +theorem object_shape_refusals : + let span := TypeExpr.apply (.constructor runtimeScope "System.Span`1") + (.atom runtimeScope "System.Int32") + let double := TypeExpr.atom runtimeScope "System.Double" + let input := fun opcode type => objectBody opcode type + ¬ ObjectOperandsValid (input 0x71 double) [true] (body [.ldobj double]) ∧ + ¬ ObjectOperandsValid (input 0xfe1c (.object runtimeScope "System.Object")) [true] + (body [.sizeof (.object runtimeScope "System.Object")]) ∧ + ¬ ObjectOperandsValid (input 0x81 span) [true] (body [.stobj span]) := by decide +kernel +#print axioms object_shape_refusals -- A default case yields a guarded equation, so a new op reading vector bits would pass as a -- forwarder. Every equation must be unconditional: one per constructor pattern. @@ -380,97 +622,87 @@ example : Safety.checkedIntrinsic (.avx2 .shl64Count) [.v128Low64 1, .v128Low64 example : Safety.checkedIntrinsic (.scalar (.leadingZeroCount 64 .word32)) [.v128Low64 1] = .error (.unsupported "Intrinsic observes unspecified vector bits") := rfl -/-- An admitted call still requires its exact emitted intrinsic and arity. -/ -theorem exact_call_site : - ExactIntrinsicsValid (raw call0) [true] (body [.intrinsic (.avx2 .band) 2]) := by - decide +kernel -#print axioms exact_call_site - -/-- A recognized owner and name cannot be hidden behind a forged identity or owner scope. -/ -theorem malformed_family_without_intrinsic : - ¬ ExactIntrinsicsValid - (raw { call0 with - declaringType := .atom "forged" "System.Runtime.Intrinsics.X86.Avx2" - target := .external "forged" }) [true] (body [.nop]) := by - decide +kernel -#print axioms malformed_family_without_intrinsic +/-- Family membership and model selection use the typed reference for both target spellings. -/ +theorem typed_family : + let crossed := { call10 with target := call0.target } + exactCallFamily call0.method = true ∧ exactCallFamily crossed.method = false ∧ + exactIntrinsic crossed.method = none ∧ + exactIntrinsic call0.method = some (.intrinsic (.avx2 .band) 2) := by decide +kernel -/-- An exact intrinsic identity still marks the family when both owner and name are forged. -/ -theorem identity_family_without_intrinsic : - ¬ ExactIntrinsicsValid - (raw { call0 with declaringType := .atom "forged" "Other", name := "Other" }) - [true] (body [.nop]) := by - decide +kernel -#print axioms identity_family_without_intrinsic - -/-- Calls outside the admitted family need no exact intrinsic. -/ -theorem unrelated_call_without_intrinsic : - ExactIntrinsicsValid - (raw { call0 with - declaringType := .atom "Other" "Other", name := "Other" - target := .external "Other" }) [true] (body [.nop]) := by - decide +kernel -#print axioms unrelated_call_without_intrinsic +#print axioms typed_family -/-- Live malformed family calls cannot be discarded even when the opcode is not `call`. -/ -theorem family_wrong_opcode : - ¬ ExactIntrinsicsValid - { raw call0 with code := [⟨0, 111, .call call0⟩] } [true] (body [.nop]) := by - decide +kernel -#print axioms family_wrong_opcode +/-- The model row compares the complete external identity and the raw metadata name. -/ +theorem exact_identity_boundaries : + refuses { call0 with name := "Andλ" } ∧ refuses { call0 with target := .external scope } ∧ + refuses { call0 with target := .external (scope ++ "::λ") } := by decide +kernel -/-- Unreachable sites retain the original exemption from metadata and intrinsic checks. -/ -theorem unreachable_malformed_exact_call : - ExactIntrinsicsValid - { raw call0 with code := [⟨0, 111, .call { call0 with target := .external "forged" }⟩] } - [false] (body [.intrinsic (.avx2 .band) 99]) := by - decide +kernel -#print axioms unreachable_malformed_exact_call +#print axioms exact_identity_boundaries -/-- Exact metadata does not admit a mismatched emitted arity. -/ -theorem exact_call_wrong_arity : - ¬ ExactIntrinsicsValid (raw call0) [true] (body [.intrinsic (.avx2 .band) 99]) := by +/-- A dead call is its opcode's hole even if the closed signature or opcode is unmodelled. -/ +theorem unreachable_call : + let source := { raw call0 with code := [⟨0, 111, .call { call0 with name := "Other" }⟩] } + (decodeRaw profile {} [] [false] source []).map Method.code = .ok [.unadmitted 111] ∧ + (decodeRaw profile {} [] [true] source []).map Method.code = liveHoleAt 0 111 := by decide +kernel -#print axioms exact_call_wrong_arity -/-- UTF-8 comparisons do not truncate or normalize non-ASCII metadata. -/ -theorem exact_call_non_ascii_name : - ¬ ExactIntrinsicsValid (raw { call0 with name := "Andλ" }) [true] - (body [.intrinsic (.avx2 .band) 2]) := by - decide +kernel -#print axioms exact_call_non_ascii_name - -/-- A truncated scope or a non-ASCII identity suffix cannot forge a family identity. -/ -theorem exact_call_identity_boundaries : - ExactIntrinsicsValid - (raw { call0 with - declaringType := .atom "Other" "Other", name := "Other" - target := .external intrinsicsScope }) [true] (body [.nop]) ∧ - ExactIntrinsicsValid - (raw { call0 with - declaringType := .atom "Other" "Other", name := "Other" - target := .external (intrinsicsScope ++ "::λ") }) [true] (body [.nop]) := by - decide +kernel -#print axioms exact_call_identity_boundaries - -/-- An exact intrinsic still requires its profile feature. -/ -theorem exact_call_unavailable : - ({ profile with - avx512F := false, avx512FVL := false - avx512DQ := false, avx512DQVL := false } : FeatureProfile).Valid ∧ - ¬ ExactIntrinsicsValid (raw call8) [true] - { body [.intrinsic (.avx512DQ .moveMask512) 1] with - profile := { profile with - avx512F := false, avx512FVL := false - avx512DQ := false, avx512DQVL := false } } := by - decide +kernel -#print axioms exact_call_unavailable +#print axioms unreachable_call -/-- Dispatch does not relax either certificate length check. -/ +/-- Decoding a nop cannot introduce a call to an intrinsic. -/ +theorem nop_excludes_intrinsic_call : + let source := { raw call0 with code := [⟨0, 0, .none⟩] } + methodCode profile {} [] [true] source ≠ [.call call0.method] := by decide +kernel + +#print axioms nop_excludes_intrinsic_call + +/-- Model agreement requires the same number of raw instructions, live flags and emissions. -/ theorem exact_call_lengths : - (¬ ExactIntrinsicsValid (raw call0) [] (body [.intrinsic (.avx2 .band) 2])) ∧ - (¬ ExactIntrinsicsValid (raw call0) [false] (body [])) := by - decide +kernel + (¬ RuntimeModelsAgree (raw call0) [] (body [.call call0.method {}])) ∧ + (¬ RuntimeModelsAgree (raw call0) [false] (body [])) := by decide +kernel + #print axioms exact_call_lengths +/-! Generated runtime comparisons apply the broadcast execution facts to extracted bodies, which + carry their declared signature and the extraction's layouts. Each example states a body in + the emitted form and proves its claim with the term the fixture generator writes. -/ + +/-- The declared signature of a `(ulong, ulong) -> ulong` method. -/ +private def wordSignature : DeclaredSignature := + { parameters := [.atom Extraction.runtimeScope "System.UInt64", + .atom Extraction.runtimeScope "System.UInt64"], + returnType := .atom Extraction.runtimeScope "System.UInt64" } + +/-- A layout environment with one entry, as an extraction that reads a struct records. -/ +private def emittedEnvironment : LayoutEnvironment := { layouts := #[⟨⟨4, []⟩, 4⟩] } + +/-- `Avx2.And` of two broadcasts, then a call for lane 0, in the form an extraction emits. -/ +private def emittedAnd : Method := + { profile := profile, returnsValue := true, locals := [], localKinds := [], + opaqueVectorLocals := [], parameterHomes := [], staticSites := [], + declared := wordSignature, environment := emittedEnvironment, links := [], + code := [.ldarg 0, .call broadcast256Method {}, .ldarg 1, + .call broadcast256Method {}, .call call0.method {}, .ldc_i4 0, + .call getElement256Method {}, .ret] } + +example : (Safety.invoke [emittedAnd] 256 0 [.scalar (.i64 13), .scalar (.i64 6)] + Safety.Memory.empty).map Prod.snd = .ok [.scalar (.i64 4)] := by + exact congrArg (Except.map Prod.snd) (Safety.invoke_broadcastBinary_word profile .band + (reference := call0.method) 0 13 6 4 + Safety.Memory.empty rfl rfl rfl rfl rfl (by decide) (Safety.checked_broadcast_and 13 6)) + +/-- `Avx2.ShiftLeftLogical` of a broadcast by a full-width count, in the emitted form. -/ +private def emittedShift : Method := + { profile := profile, returnsValue := true, locals := [.v128 0], localKinds := [.vector128], + opaqueVectorLocals := [0], parameterHomes := [], staticSites := [], + declared := wordSignature, environment := emittedEnvironment, links := [], + code := [.ldarg 1, .call createScalarUnsafeMethod {}, .stloc 0, .ldarg 0, + .call broadcast256Method {}, .ldloc 0, .call call4.method {}, + .ldc_i4 0, .call getElement256Method {}, .ret] } + +example : (Safety.invoke [emittedShift] 256 0 [.scalar (.i64 1), .scalar (.i64 63)] + Safety.Memory.empty).map Prod.snd = .ok [.scalar (.i64 9223372036854775808)] := by + exact congrArg (Except.map Prod.snd) (Safety.invoke_broadcastShift profile .shl64Count + (reference := call4.method) 0 1 63 + 9223372036854775808 Safety.Memory.empty rfl rfl rfl rfl rfl (by decide) + (Safety.checked_broadcast_shiftLeft 1 63)) + end Tests.ExactIntrinsics diff --git a/Tests/ExactMetadataBytes.cs b/Tests/ExactMetadataBytes.cs new file mode 100644 index 000000000..137405923 --- /dev/null +++ b/Tests/ExactMetadataBytes.cs @@ -0,0 +1,279 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:property AssemblyName=LeanCIL.IdentityTests +#:project ../Extractor/Extractor.csproj +#:package Mono.Cecil + +extern alias IdentityExtractor; + +using System.Buffers.Binary; +using System.Collections.Immutable; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Text; +using Mono.Cecil; +using Mono.Cecil.Cil; +using IdentityExtractor::Nethermind.LeanCIL; +using MetadataReaderProvider = System.Reflection.Metadata.MetadataReaderProvider; + +static void Require(bool condition, string message) +{ + if (!condition) throw new InvalidOperationException(message); +} + +static byte[] Image(int padding) +{ + using var module = ModuleDefinition.CreateModule("ExactRows", ModuleKind.Dll); + var type = new TypeDefinition("Fixture", "Methods", TypeAttributes.Public, module.TypeSystem.Object); + module.Types.Add(type); + foreach (string name in new[] { "First", "Later" }) + { + var method = new MethodDefinition(name, MethodAttributes.Public | MethodAttributes.Static, + module.TypeSystem.Void); + type.Methods.Add(method); + if (name == "First") + for (int index = 0; index < padding; index++) + method.Body.Instructions.Add(Instruction.Create(OpCodes.Nop)); + method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); + } + using var output = new MemoryStream(); + module.Write(output); + return output.ToArray(); +} + +// The stream directory of a metadata root: each header's position, the stream's offset and size, and its name. +static List<(int At, int Offset, int Size, string Name)> Streams(byte[] metadata) +{ + int at = 20 + BitConverter.ToInt32(metadata, 12); + int count = BitConverter.ToUInt16(metadata, at - 2); + var streams = new List<(int, int, int, string)>(); + for (int stream = 0; stream < count; stream++) + { + int nameEnd = Array.IndexOf(metadata, (byte)0, at + 8); + streams.Add((at, BitConverter.ToInt32(metadata, at), BitConverter.ToInt32(metadata, at + 4), + Encoding.ASCII.GetString(metadata, at + 8, nameEnd - at - 8))); + at = (nameEnd + 4) & ~3; + } + return streams; +} + +// A root whose version field holds the table stream header at offset 16 and which ends after its directory. +static byte[] Synthetic(byte[] table, ushort? count, params (uint Offset, uint Size, byte[] Name)[] streams) +{ + using var root = new MemoryStream(); + using var writer = new BinaryWriter(root); + int versionLength = (table.Length + 3) & ~3; + writer.Write(0x424A5342u); writer.Write(0x00010001u); writer.Write(0u); writer.Write(versionLength); + writer.Write(table); writer.Write(new byte[versionLength - table.Length]); + writer.Write((ushort)0); writer.Write(count ?? (ushort)streams.Length); + foreach (var (offset, size, name) in streams) + { + writer.Write(offset); writer.Write(size); + writer.Write(name); writer.Write(new byte[((name.Length + 3) & ~3) - name.Length]); + } + writer.Flush(); + return root.ToArray(); +} + +// A root rebuilt with a version field of versionLength bytes and each stream header on a four-byte boundary +// of the root, which is how System.Reflection.Metadata walks the directory. +static byte[] Realigned(byte[] metadata, int versionLength) +{ + var streams = Streams(metadata); + int directory = 16 + versionLength + 4; + int dataAt = directory; + foreach (var stream in streams) dataAt = (dataAt + 8 + stream.Name.Length + 4) & ~3; + var root = new byte[dataAt + streams.Sum(stream => (stream.Size + 3) & ~3)]; + metadata.AsSpan(0, 12).CopyTo(root); + BinaryPrimitives.WriteInt32LittleEndian(root.AsSpan(12), versionLength); + metadata.AsSpan(16, Math.Min(versionLength, BitConverter.ToInt32(metadata, 12))).CopyTo(root.AsSpan(16)); + BinaryPrimitives.WriteUInt16LittleEndian(root.AsSpan(directory - 2), (ushort)streams.Count); + int at = directory; + foreach (var stream in streams) + { + BinaryPrimitives.WriteInt32LittleEndian(root.AsSpan(at), dataAt); + BinaryPrimitives.WriteInt32LittleEndian(root.AsSpan(at + 4), stream.Size); + Encoding.ASCII.GetBytes(stream.Name).CopyTo(root, at + 8); + metadata.AsSpan(stream.Offset, stream.Size).CopyTo(root.AsSpan(dataAt)); + dataAt += (stream.Size + 3) & ~3; + at = (at + 8 + stream.Name.Length + 4) & ~3; + } + return root; +} + +static bool ReaderAccepts(byte[] metadata) +{ + try { MetadataReaderProvider.FromMetadataImage(ImmutableArray.Create(metadata)).GetMetadataReader(); } + catch (BadImageFormatException) { return false; } + return true; +} + +static void Refused(string label, string reason, Action read) where TException : Exception +{ + try { read(); } + catch (TException error) when (error.Message.Contains(reason, StringComparison.Ordinal)) { return; } + catch (Exception error) + { + throw new InvalidOperationException($"{label}: expected \"{reason}\", got {error.GetType().Name}: " + + error.Message); + } + throw new InvalidOperationException($"{label}: accepted"); +} + +static void Bad(string label, string reason, byte[] metadata) => + Refused(label, reason, () => MetadataImage.ReadTableHeader(metadata)); + +static byte[] Patched(byte[] source, Action patch) +{ + var copy = (byte[])source.Clone(); + patch(copy); + return copy; +} + +byte[] original = Image(0); +using var pe = new PEReader(new MemoryStream(original)); +var metadata = new MetadataImage(pe); +byte[] root = pe.GetMetadata().GetContent().ToArray(); +var tableEntry = Streams(root).Single(stream => stream.Name == "#~"); +var (tableOffset, tableSize) = (tableEntry.Offset, tableEntry.Size); +byte[] header = metadata.TableHeader(); +Require(root.AsSpan(tableOffset, header.Length).SequenceEqual(header), "Header differs from original bytes"); + +// A carried MethodDef row has its RVA column zeroed: an earlier body's length moves the image's own RVA of a +// later method, and that method's evidence must not change with it. +byte[] later = metadata.Row(0x06000002); +int rowOffset = metadata.Reader.GetTableMetadataOffset(TableIndex.MethodDef) + + metadata.Reader.GetTableRowSize(TableIndex.MethodDef); +uint imageRva = BitConverter.ToUInt32(root, rowOffset); +Require(imageRva != 0, "The fixture's MethodDef row has no RVA, so masking it tests nothing"); +Require(BitConverter.ToUInt32(later) == 0, "The carried MethodDef row kept its RVA column"); +Require(root.AsSpan(rowOffset + 4, later.Length - 4).SequenceEqual(later.AsSpan(4)), + "The carried MethodDef row differs from the image's row outside its RVA column"); +using var longerPe = new PEReader(new MemoryStream(Image(100))); +var longer = new MetadataImage(longerPe); +byte[] longerRoot = longerPe.GetMetadata().GetContent().ToArray(); +Require(BitConverter.ToUInt32(longerRoot, rowOffset) != imageRva, "A longer earlier body did not move the RVA"); +Require(longer.Row(0x06000002).AsSpan().SequenceEqual(later), + "A longer earlier body changed the carried row of a later method"); +int typeOffset = metadata.Reader.GetTableMetadataOffset(TableIndex.TypeDef) + + metadata.Reader.GetTableRowSize(TableIndex.TypeDef); +Require(metadata.Row(0x02000002).AsSpan().SequenceEqual( + root.AsSpan(typeOffset, metadata.Reader.GetTableRowSize(TableIndex.TypeDef))), "TypeDef row differs from image"); + +// The last present table has no later rows whose positions depend on its count. +byte[] zeroImage = (byte[])original.Clone(); +int metadataFileOffset = pe.PEHeaders.MetadataStartOffset; +int lastCount = tableOffset + header.Length - sizeof(uint); +BinaryPrimitives.WriteUInt32LittleEndian(zeroImage.AsSpan(metadataFileOffset + lastCount), 0); +using var zeroPe = new PEReader(new MemoryStream(zeroImage)); +var zero = new MetadataImage(zeroPe); +ulong valid = BitConverter.ToUInt64(header, 8); +int lastTable = 63 - System.Numerics.BitOperations.LeadingZeroCount(valid); +Require(zero.Count(lastTable) == 0, "Present-zero fixture has rows"); +Require(zero.TableHeader().Length == header.Length, "Present-zero count omitted from header"); +Require(zero.DecodedHeader().Contains($"({lastTable}, 0)", StringComparison.Ordinal), + "Present-zero table omitted from decoded header"); +var expectedCounts = Enumerable.Range(0, 64).Where(table => (valid & (1UL << table)) != 0) + .Select(table => $"({table}, {zero.Count(table)})"); +Require(zero.DecodedHeader() == $".ofCounts {header[6]} [{string.Join(", ", expectedCounts)}]", + "Decoded header differs from the image table counts"); +Require(ReferenceEquals(zero.DecodedHeader(), zero.DecodedHeader()), "Decoded header was not cached"); +Require(zero.TableHeader().AsSpan().SequenceEqual( + zeroImage.AsSpan(metadataFileOffset + tableOffset, header.Length)), "Present-zero header is reconstructed"); +header[0] ^= 0xff; +Require(metadata.TableHeader()[0] == root[tableOffset], "Caller changed cached header"); +header[0] ^= 0xff; +Console.WriteLine("PASS carried MethodDef rows mask the RVA, other rows are exact, present-zero tables are kept"); + +// Truncation at every prefix of the root up to the end of the table stream. +for (int length = 0; length < tableOffset + tableSize; length++) +{ + try + { + MetadataImage.ReadTableHeader(root.AsSpan(0, length)); + throw new InvalidOperationException($"Truncated metadata accepted at {length}"); + } + catch (BadImageFormatException) { } +} +foreach (int offset in new[] { tableOffset + 4, tableOffset + 5, tableOffset + 6 }) + Bad($"header byte {offset}", "Unsupported metadata table stream header", + Patched(root, copy => copy[offset] = 0x40)); + +// One rejection per bound, each with its own reason. +byte[] tableName = "#~\0"u8.ToArray(); +uint size = (uint)header.Length; +Require(MetadataImage.ReadTableHeader(Synthetic(header, null, (16, size, tableName))).AsSpan().SequenceEqual(header), + "The synthetic root does not read back its header"); +Bad("short root", "shorter than its fixed fields", root[..15]); +Bad("signature", "Invalid metadata root signature", Patched(root, copy => copy[0] ^= 1)); +byte[] unpadded = Realigned(root, 13); +Require(ReaderAccepts(unpadded), "System.Reflection.Metadata refuses the unpadded version length fixture"); +Bad("unpadded version", "not a multiple of 4", unpadded); +Bad("long version", "not a multiple of 4", Realigned(root, 260)); +Bad("directory", "stream directory crosses", root[..30]); +Bad("no stream", "names no #~ stream", Synthetic(header, 0, (16, size, tableName))); +Bad("stream count", "stream header crosses", Synthetic(header, 2, (16, size, tableName))); +Bad("long name", "no terminator within 32 bytes", + Synthetic(header, null, (0, 0, Encoding.ASCII.GetBytes(new string('A', 32) + "\0")), (16, size, tableName))); +Bad("name padding", "name padding crosses", Synthetic(header, null, (16, size, tableName))[..^1]); +Bad("offset", "table stream crosses", Synthetic(header, null, (0xFFFFFFF0, 0x20, tableName))); +Bad("size", "table stream crosses", Synthetic(header, null, (16, 0x80000000, tableName))); +Bad("end", "table stream crosses", Synthetic(header, null, (16, 0x1000, tableName))); +Bad("fixed header", "shorter than its fixed header", Synthetic(header, null, (16, 23, tableName))); +Bad("row counts", "ends inside its row counts", Synthetic(header, null, (16, size - 1, tableName))); +Bad("two streams", "two #~ streams", Synthetic(header, null, (16, size, tableName), (16, size, tableName))); +Refused("uncompressed", "#- is not supported", + () => MetadataImage.ReadTableHeader(Synthetic(header, null, (16, size, "#-\0"u8.ToArray())))); + +// System.Reflection.Metadata reads both images below, so the refusal is this reader's. +var blob = Streams(root).Single(stream => stream.Name == "#Blob"); +byte[] twoTables = Patched(original, image => +{ + var at = metadataFileOffset + blob.At; + BinaryPrimitives.WriteInt32LittleEndian(image.AsSpan(at), tableOffset); + BinaryPrimitives.WriteInt32LittleEndian(image.AsSpan(at + 4), tableSize); + "#~\0\0\0\0\0\0"u8.CopyTo(image.AsSpan(at + 8)); +}); +byte[] uncompressed = Patched(original, image => image[metadataFileOffset + tableEntry.At + 9] = (byte)'-'); +foreach (var (label, image) in new[] { ("two #~", twoTables), ("#-", uncompressed) }) +{ + using var patched = new PEReader(new MemoryStream(image)); + System.Reflection.Metadata.PEReaderExtensions.GetMetadataReader(patched); + if (label == "#-") + Refused(label, "#- is not supported", () => new MetadataImage(patched)); + else Refused(label, "two #~ streams", () => new MetadataImage(patched)); +} +Console.WriteLine("PASS truncations, each header bound, two table streams and the #- stream are refused"); + +// Per request, a row and a header copy: allocation is constant per request and linear in the present tables. +_ = metadata.DecodedHeader(); +double perRequest = 0; +foreach (int count in new[] { 64, 256, 1024 }) +{ + long allocated = GC.GetAllocatedBytesForCurrentThread(); + long bytes = 0; + for (int index = 0; index < count; index++) + { + bytes += metadata.Row(0x06000002).Length; + bytes += metadata.TableHeader().Length; + bytes += metadata.DecodedHeader().Length; + } + allocated = GC.GetAllocatedBytesForCurrentThread() - allocated; + Console.WriteLine($"requests={count} bytes={bytes} allocated={allocated} perRequest={allocated / count}"); + if (perRequest == 0) perRequest = (double)allocated / count; + else Require((double)allocated / count <= perRequest * 1.25, "Allocation per request grows with requests"); +} +foreach (int tables in new[] { 1, 16, 45 }) +{ + byte[] wide = new byte[24 + 4 * tables]; + header.AsSpan(0, 8).CopyTo(wide); + BinaryPrimitives.WriteUInt64LittleEndian(wide.AsSpan(8), (1UL << tables) - 1); + byte[] wideRoot = Synthetic(wide, null, (16, (uint)wide.Length, tableName)); + _ = MetadataImage.ReadTableHeader(wideRoot); + const int Reads = 256; + long allocated = GC.GetAllocatedBytesForCurrentThread(); + for (int index = 0; index < Reads; index++) _ = MetadataImage.ReadTableHeader(wideRoot); + allocated = (GC.GetAllocatedBytesForCurrentThread() - allocated) / Reads; + Console.WriteLine($"tables={tables} headerBytes={wide.Length} allocatedPerRead={allocated}"); + Require(allocated <= wide.Length + 40, "Reading the header allocates more than one copy of it"); +} diff --git a/Tests/ExceptionLint.cs b/Tests/ExceptionLint.cs index 9c030fcb9..68cd137a0 100644 --- a/Tests/ExceptionLint.cs +++ b/Tests/ExceptionLint.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; using System.Xml.Linq; @@ -72,6 +77,13 @@ void Reject(string diagnostic) JsonElement report = Lint(); if (!Potential(report).SequenceEqual(item.Expected)) throw new InvalidOperationException("Wrong modeled exception set: " + item.Selector); + if (item.Selector == "Examples.ExceptionDocumentation::Absolute(int)") + { + var site = report.GetProperty("sites").EnumerateArray().Single(); + const string origin = "interpreter operation: IL_0001: call System.Int32 System.Math::Abs(System.Int32)"; + if (site.GetProperty("origin").GetString() != origin) + throw new InvalidOperationException("The overflow site must name the call to the selected overload."); + } bool expectedDrift = item.Selector.Contains("Clamp", StringComparison.Ordinal) || item.Selector.Contains("Profile", StringComparison.Ordinal); if (report.GetProperty("findings").GetArrayLength() != (expectedDrift ? 1 : 0)) throw new InvalidOperationException("Compiler XML matching or separate invalid-bounds/profile exclusion failed: " + item.Selector); diff --git a/Tests/ExceptionMembers.cs b/Tests/ExceptionMembers.cs new file mode 100644 index 000000000..63e7baca9 --- /dev/null +++ b/Tests/ExceptionMembers.cs @@ -0,0 +1,31 @@ +partial class Program +{ + // The CIL.ExceptionType member of each CLR exception type a differential test can observe. + static readonly Dictionary ExceptionMembers = new() + { + [typeof(Exception)] = "baseException", + [typeof(SystemException)] = "systemException", + [typeof(ApplicationException)] = "applicationException", + [typeof(ArithmeticException)] = "arithmetic", + [typeof(DivideByZeroException)] = "divideByZero", + [typeof(OverflowException)] = "overflow", + [typeof(InvalidOperationException)] = "invalidOperation", + [typeof(NotSupportedException)] = "notSupported", + [typeof(NotImplementedException)] = "notImplemented", + [typeof(ArgumentException)] = "argument", + [typeof(ArgumentNullException)] = "argumentNull", + [typeof(ArgumentOutOfRangeException)] = "argumentOutOfRange", + [typeof(IndexOutOfRangeException)] = "indexOutOfRange", + [typeof(NullReferenceException)] = "nullReference", + [typeof(FormatException)] = "format", + [typeof(InvalidCastException)] = "invalidCast", + [typeof(ArrayTypeMismatchException)] = "arrayTypeMismatch", + }; + + /// + /// The Lean member naming the type of ; a type the model has no member for fails the test. + /// + static string ExceptionMember(Exception error) => + ExceptionMembers.TryGetValue(error.GetType(), out var member) ? member + : throw new InvalidOperationException("No modelled exception type for " + error.GetType().FullName); +} diff --git a/Tests/ExcludedBranchScaling.cs b/Tests/ExcludedBranchScaling.cs index 025abd450..e4a97da89 100644 --- a/Tests/ExcludedBranchScaling.cs +++ b/Tests/ExcludedBranchScaling.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include GeneratedEvidence.cs using System.Text.Json; @@ -11,7 +16,8 @@ var source = Path.Combine(directory, "Assembly"); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Excluded.csproj"), """ -net10.0true +net11.0 +true """); File.WriteAllText(Path.Combine(source, "Excluded.cs"), "public static class Excluded { public static ulong Accumulate(ulong x, ulong y) { " + @@ -60,19 +66,36 @@ List Entries(string name) Run(["lake", "env", "lean", Path.Combine(liveOutput, "Extracted.lean")], cwd: FixturePackage()); Console.WriteLine($"PASS {liveSites} live sites validate at the default heartbeat budget"); -// Each validator must still reach and reject a forged claim at the last pruned site it checks. -void Reject(string name, string validator, string obligation, Func site, bool forgeState) +// Each validator must still reach and reject a forged claim at the last pruned site it checks. A decoded method +// decodes the mask it is given, so a pinned run decodes the extracted mask and keeps its holes. +void Reject(string name, string validator, string obligation, Func site, bool forgeState, + bool pinnedCode = false) { var live = Entries("method0Reachable"); var facts = Entries("method0FeatureStates"); var pc = Enumerable.Range(0, live.Count).Last(i => live[i] == "false" && site(i)); live[pc] = "true"; + // The method has no locals, so the forged state is well shaped and only the check at the claimed site rejects it. + var closed = extracted.Split('\n').Single(l => l.StartsWith("def method0Closed ", StringComparison.Ordinal)); + if (forgeState && !closed.Contains("], locals := [], initLocals", StringComparison.Ordinal)) + throw new InvalidOperationException( + "The excluded-branch method has locals; the forged state may be badly shaped."); if (forgeState) facts[pc] = "some { stack := [], locals := [] }"; var tampered = Path.Combine(directory, name + ".lean"); var changed = string.Join('\n', extracted.Split('\n').Select(line => line.StartsWith("def method0Reachable ", StringComparison.Ordinal) ? $"def method0Reachable : List Bool := [{string.Join(", ", live)}]" : line.StartsWith("def method0FeatureStates ", StringComparison.Ordinal) ? $"def method0FeatureStates : Extraction.FeatureCertificate := [{string.Join(", ", facts)}]" : line)); + if (pinnedCode) + { + var decoder = DecodedDefinition(changed, 0); + if (!decoder.Success) throw new InvalidOperationException("The excluded-branch method is not decoded."); + var mask = extracted.Split('\n').Single(l => l.StartsWith("def method0Reachable ", StringComparison.Ordinal)); + changed = changed.Replace(decoder.Value, $""" + {mask.Replace("def method0Reachable ", "def method0ExtractedReachable ", StringComparison.Ordinal)} + {decoder.Value.Replace(" method0Reachable ", " method0ExtractedReachable ", StringComparison.Ordinal)} + """, StringComparison.Ordinal); + } File.WriteAllText(tampered, NarrowEvidence(changed, obligation)); var rejected = Execute(["lake", "env", "lean", tampered], cwd: FixturePackage()); if (rejected.Code == 0 || !rejected.Output.Contains($"Extraction.{validator} ", StringComparison.Ordinal) || @@ -82,9 +105,12 @@ void Reject(string name, string validator, string obligation, Func si } var argumentCode = extracted.Split('\n').Single(l => l.StartsWith("def method0RawArguments ", StringComparison.Ordinal)); var operations = argumentCode[(argumentCode.LastIndexOf("code := [", StringComparison.Ordinal) + 9)..argumentCode.LastIndexOf(']')].Split(", "); +bool ArgumentLoad(int pc) => operations[pc].StartsWith(".load", StringComparison.Ordinal); Reject("ForgedFeatureSite", "FeatureFlowValid", featureObligation, _ => true, forgeState: true); -Reject("ForgedArgumentSite", "ParameterHomesValid", parameterObligation, - pc => operations[pc].StartsWith(".load", StringComparison.Ordinal), forgeState: true); +// Code decoded from the forged mask loads the argument there, so the feature flow refuses the site. +Reject("ForgedArgumentFeatures", "FeatureFlowValid", featureObligation, ArgumentLoad, forgeState: true); +Reject("ForgedArgumentSite", "ParameterHomesValid", parameterObligation, ArgumentLoad, forgeState: true, + pinnedCode: true); var consumer = Path.Combine(directory, "Consumer"); Directory.CreateDirectory(consumer); diff --git a/Tests/ExternalIdentityEncoding.cs b/Tests/ExternalIdentityEncoding.cs index 8e7cadaa7..2434002fe 100644 --- a/Tests/ExternalIdentityEncoding.cs +++ b/Tests/ExternalIdentityEncoding.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include ../Extractor/ExternalIdentityEvidence.cs @@ -43,7 +48,7 @@ void Check(string name, string source, bool accepted, string? diagnostic = null) { "", "ASCII", new string('a', 31) + "λ", new string('b', 30) + "漢", new string('c', 29) + "🚀", "embedded\0null", "quote\" and slash\\", - "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a::" + + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a::" + "System.Boolean System.Runtime.Intrinsics.Vector256::get_IsHardwareAccelerated()" }; foreach (var identity in identities) diff --git a/Tests/ExtractionBatch.cs b/Tests/ExtractionBatch.cs index f9dbd01ac..7b0c578ed 100644 --- a/Tests/ExtractionBatch.cs +++ b/Tests/ExtractionBatch.cs @@ -2,7 +2,14 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs +#:include ../scripts/ConsumerVerification.cs +#:include ../scripts/LeanDiagnostics.cs +#:include DirectoryLinks.cs using System.Text.Json; using Mono.Cecil; @@ -24,7 +31,7 @@ new Request(modified, "Examples", "Arithmetic::Add(ulong,ulong)", Path.Combine(root, "Modified"), "scalar"), new Request(assembly, "Examples", "GenericMethods::Repeated(ulong)", Path.Combine(root, "Arm"), "arm64-advsimd"), new Request(assembly, "Examples", "GenericMethods::Repeated(ulong)", Path.Combine(root, "Repeated"), "scalar"), - new Request(assembly, "Examples", "Arithmetic::Add(ulong,ulong)", Path.Combine(root, "Original"), "x64-sse2"), + new Request(assembly, "Examples", "Arithmetic::Add(ulong,ulong)", Path.Combine(root, "Original"), "x64-sse42"), new Request(assembly, "Examples", "Capabilities::Flags()", Path.Combine(root, "ScalarFlags"), "scalar"), new Request(assembly, "Examples", "Capabilities::Flags()", Path.Combine(root, "X64Flags"), "x64-v3"), new Request(assembly, "Examples", "Loops::Countdown(uint)", Path.Combine(root, "Loop"), "scalar"), @@ -49,6 +56,49 @@ Require(!File.Exists(Path.Combine(requests[1].Output, "Symbols.lean")), "Symbols leaked into another DLL"); Console.WriteLine("PASS mixed DLLs, profiles, repeated generic specialization and symbol identities match direct extraction"); +// Two extractions of one input, in separate processes and separate directories, write the same bytes in every file. +// A batch in the opposite request order and a reversed --assume-initialized list must not change any file either. +var determinism = Path.Combine(root, "Determinism"); +Request[] repeated = +[ + .. requests, + new(assembly, "Examples", "GenericLayouts::Create(ulong,ulong)", "", "scalar"), + new(assembly, "Examples", "GenericLayouts::Nested(uint,uint)", "", "x64-v3"), + requests[^1] with { InitializedTypes = ["Examples.Word256", "Examples.InitializedTypes"] } +]; +if (Directory.Exists(determinism)) Directory.Delete(determinism, recursive: true); +Request[] RunOf(string name, Func initialized) => [.. repeated.Select((request, index) => + request with { Output = Path.Combine(determinism, name + "-" + index), InitializedTypes = initialized(request) })]; +var firstRun = RunOf("first", request => request.InitializedTypes); +var secondRun = RunOf("second", request => + request.InitializedTypes is { } types ? [.. Enumerable.Reverse(types)] : null); +var batchRun = RunOf("batch", request => request.InitializedTypes); +foreach (var request in firstRun.Concat(secondRun)) + Run([.. Extract("extract", request.Assembly, request.Identity, request.Entry, request.Output, request.Profile), + .. (request.InitializedTypes ?? []).SelectMany(type => new[] { "--assume-initialized", type })]); +Batch("extract", [.. Enumerable.Reverse(batchRun)]); +string[] writtenFiles = ["Extracted.lean", "Symbols.lean", "identity.json"]; +for (var index = 0; index < repeated.Length; index++) +{ + var context = repeated[index].Entry + " (" + repeated[index].Profile + ")"; + var (first, second, batched) = (firstRun[index].Output, secondRun[index].Output, batchRun[index].Output); + Require(FirstDifference(first, second) is null, + "Two extractions of " + context + " differ: " + FirstDifference(first, second)); + Require(FirstDifference(first, batched) is null, + "A reversed batch of " + context + " differs: " + FirstDifference(first, batched)); + var files = Directory.GetFiles(first).Select(path => Path.GetFileName(path)).ToArray(); + Require(files.Contains("Extracted.lean") && files.Contains("identity.json") && files.All(writtenFiles.Contains), + "An extraction of " + context + " wrote unexpected files: " + string.Join(", ", files)); + // The consumer path splits Extracted.lean into content-addressed components, so equal text must split alike. + var (left, right) = (SplitExtractionComponents(File.ReadAllText(Path.Combine(first, "Extracted.lean"))), + SplitExtractionComponents(File.ReadAllText(Path.Combine(second, "Extracted.lean")))); + Require(left.Aggregate == right.Aggregate && left.Sources.Count == right.Sources.Count && + left.Sources.All(pair => right.Sources.TryGetValue(pair.Key, out var text) && text == pair.Value), + "Components of " + context + " differ between two extractions"); +} +Console.WriteLine("PASS repeated, reversed-batch and reordered-assumption extractions write identical " + + "files and components"); + var generated = Path.Combine(requests[0].Output, "Extracted.lean"); var source = File.ReadAllText(generated); File.AppendAllText(generated, "\n-- tampered\n"); @@ -81,6 +131,31 @@ } Require(rejected, "Generated verification accepted aliased output directories"); } +// Two requests writing one directory, one through a link, would overwrite each other's extraction. +var throughLink = Path.Combine(Path.GetTempPath(), "leancil-batch-link-" + Guid.NewGuid().ToString("N")); +if (TryLinkDirectory(throughLink, root)) +{ + try + { + var linked = Path.Combine(throughLink, Path.GetFileName(duplicates[0].Output)); + Reject("extract", [duplicates[0], duplicates[1] with { Output = linked }], "outputs must be distinct"); + using var verification = new Program(); + var rejected = false; + try + { + verification.VerifyGeneratedBatch(assembly, "Examples", + [new(requests[0].Entry, "unused.lean", duplicates[0].Output), + new(requests[1].Entry, "unused.lean", linked)]); + } + catch (ArgumentException error) when (error.Message == "Batch outputs must be distinct") + { + rejected = true; + } + Require(rejected, "Generated verification accepted an output reached through a link"); + } + finally { Directory.Delete(throughLink); } +} +else Console.WriteLine("SKIP batch outputs through a link: no directory link or junction could be made"); var invalid = requests[0] with { Entry = "Missing::Method()", Output = Path.Combine(root, "Invalid") }; var never = requests[0] with { Output = Path.Combine(root, "Never") }; Reject("extract", [requests[0], invalid, never], "Missing::Method()"); diff --git a/Tests/ExtractionEvidence.cs b/Tests/ExtractionEvidence.cs index 069f51521..f6c225c35 100644 --- a/Tests/ExtractionEvidence.cs +++ b/Tests/ExtractionEvidence.cs @@ -1,13 +1,20 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs if (args is not ([] or ["--prepare-only"])) throw new ArgumentException("Usage: ExtractionEvidence.cs [--prepare-only]"); using var fixtures = new Program(); fixtures.PrepareLeanFixtures( - "Tests.ExtractionLayout", "Tests.ExtractionParameters", "Tests.FeatureEvidence", "Tests.ScalarEvidence", + "Tests.MetadataBuilder", "Tests.BodyEncoding", "Tests.ExtractionLayout", "Tests.ExtractionParameters", + "Tests.FeatureEvidence", "Tests.LoweredCallRefusal", + "Tests.ScalarEvidence", "Tests.LayoutCoverage", "Tests.TypeTestEvidence", "Tests.ThrowEvidence", "Tests.SpanEvidence", "Tests.ImportEvidence", "Tests.ImportedLayouts", "Tests.AssemblyPins", "Tests.StaticValueEvidence", "Tests.SpanViews", "Tests.MemorySpanExecution", "Tests.ManagedPointerValues", "Tests.PrimitiveArrays", "Tests.ArrayElementReferences", @@ -22,13 +29,15 @@ progress: "Array element references"); Run(["dotnet", "run", "--file", "Tests/ReadOnlyArrayReferences.cs"], streamOutput: true, progress: "Readonly array references"); +Run(["dotnet", "run", "--file", "Tests/SpanHomeReads.cs"], streamOutput: true, + progress: "Span locals read through their addresses"); Run(["dotnet", "run", "--file", "Tests/ExternalIdentityEncoding.cs"], streamOutput: true, progress: "UTF-8 metadata identity certificates"); foreach (var name in new[] { "LayoutEvidence", "ParameterEvidence", "GenericEvidence", "FeatureEvidence", "TypeTests", "Throws", "SpanTables", "MemorySpans", "ReferenceInstructions", "PointerOperations", "Objects", "StaticValues", "StaticScalarValues", - "TranslationAudit" + "TranslationAudit", "MetadataDifferential", "Guards", "CallSiteAgreement" }) Run(["dotnet", "run", "--file", $"Tests/{name}.cs"], streamOutput: true, progress: name); Console.WriteLine("PASS complete extraction evidence, compiled fixtures, mutations and mandatory certificate audit"); diff --git a/Tests/ExtractionGenerics.lean b/Tests/ExtractionGenerics.lean index e28d3f6f2..e81d3bd2f 100644 --- a/Tests/ExtractionGenerics.lean +++ b/Tests/ExtractionGenerics.lean @@ -1,11 +1,16 @@ import CIL.Extraction.Generics import CIL.Extraction.Scalars import CIL.Safety.SymbolicExecution +import Tests.Support + +/-! Raw method specialization, call-site binding and construction checks. -/ namespace Tests.ExtractionGenerics open CIL CIL.Extraction -def word : TypeExpr := .atom "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" "System.UInt64" +def word : TypeExpr := .atom + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" + "System.UInt64" def raw : RawMethod := { token := 1, owner := "Test::Owner", hasThis := false, explicitThis := false, callingConvention := 16, attributes := 0, implementation := 0, @@ -24,7 +29,8 @@ example : instantiateMethod [] { raw with genericParameters := [{ flags := 4, ty /-- `T : I` closes at each argument and needs that argument's layout to list `I`. -/ def selfConstrained : RawMethod := { raw with genericParameters := [{ - flags := 24, types := [], interfaces := [.apply (.constructor "Test" "I`1") (.parameter 0)] }] } + flags := 24, types := [], + interfaces := [.apply (.objectConstructor "Test" "I`1") (.parameter 0)] }] } def selfLayout : RawLayout := { identity := "Test::A", explicit := false, packing := 0, declaredSize := 0, fields := [], interfaces := ["Test::I`1"] } @@ -45,111 +51,156 @@ example : typeStorage [{ example : substituteType [word] (.apply (.constructor "scope" "Box`1") (.parameter 0)) = some (.apply (.constructor "scope" "Box`1") word) := by decide -/-- A call whose scoped signature and generic arguments match the selected closed method. -/ -def caller (parameter : TypeExpr) : RawMethod := { closed with code := [⟨0, 40, .call { - declaringType := .atom "Test" "Owner", name := "Call", target := .managed 1, - typeArguments := [word], parameterCount := 1, hasThis := false, returnsValue := true, - returnType := word, parameterTypes := [.byref parameter] }⟩] } +/-- A call site whose scoped signature and generic arguments match the selected closed method. -/ +def callSite (parameter : TypeExpr) : CallSite := { + declared := { convention := 0x10, returnType := word, parameters := [.byref (.parameter 0)] }, + method := { + owner := .atom "Test" "Owner", name := name!"Call", hasThis := false, genericArity := 1, + returnType := word, parameters := [.byref parameter], methodArguments := [word] }, + name := "Call", target := .managed 1, typeArguments := [word], parameterCount := 1, + returnsValue := true } + +/-- A method whose one instruction calls `callSite parameter`. -/ +def caller (parameter : TypeExpr) : RawMethod := + { closed with code := [⟨0, 40, .call (callSite parameter)⟩] } /-- The emitted target and argument count are independently checked against the call site. -/ -def callCode : Method := { locals := [], returnsValue := true, code := [.call 0 1] } +def callCode : Method := { + locals := [], returnsValue := true, code := [.call (callSite word).method], + links := [(0, .direct 0)] } -/-- Structural proof construction retains the complete managed-call predicate. -/ -theorem structural_call : ManagedCallsValid [⟨[word], closed⟩] [] (caller word) [true] - callCode := by - cil_strict (cil_managed_calls [caller, closed, raw, word, callCode]) +/-- A generic call site whose operand, link and closed signature agree passes the check. -/ +theorem structural_call : + ManagedCallsValid [⟨[word], closed⟩] [] (fun _ => false) (caller word) [true] callCode := by + decide +kernel #print axioms structural_call /-- Equal storage width does not permit substituting a signed parameter. -/ theorem signed_parameter_rejected : - ¬ ManagedCallsValid [⟨[word], closed⟩] [] + ¬ ManagedCallsValid [⟨[word], closed⟩] [] (fun _ => false) (caller (.atom runtimeScope "System.Int64")) [true] callCode := by - fail_if_success (cil_strict ( - have invalid : ManagedCallsValid [⟨[word], closed⟩] [] - (caller (.atom runtimeScope "System.Int64")) [true] callCode := by - cil_managed_calls [caller, closed, raw, word, callCode])) decide +kernel #print axioms signed_parameter_rejected /-- A matching type name from another assembly remains a different parameter type. -/ theorem parameter_scope_rejected : - ¬ ManagedCallsValid [⟨[word], closed⟩] [] + ¬ ManagedCallsValid [⟨[word], closed⟩] [] (fun _ => false) (caller (.atom "attacker" "System.UInt64")) [true] callCode := by - fail_if_success (cil_strict ( - have invalid : ManagedCallsValid [⟨[word], closed⟩] [] - (caller (.atom "attacker" "System.UInt64")) [true] callCode := by - cil_managed_calls [caller, closed, raw, word, callCode])) decide +kernel #print axioms parameter_scope_rejected /-- A closed signature cannot stand in for different method type arguments. -/ theorem generic_argument_rejected : - ¬ ManagedCallsValid [⟨[.atom runtimeScope "System.Int64"], closed⟩] [] + ¬ ManagedCallsValid [⟨[.atom runtimeScope "System.Int64"], closed⟩] [] (fun _ => false) (caller word) [true] callCode := by - fail_if_success (cil_strict ( - have invalid : ManagedCallsValid [⟨[.atom runtimeScope "System.Int64"], closed⟩] [] - (caller word) [true] callCode := by - cil_managed_calls [caller, closed, raw, word, callCode])) decide +kernel #print axioms generic_argument_rejected -/-- Structural proof construction rejects a call with an altered argument count. -/ +/-- A call operand naming the method with a second parameter, so another argument count. -/ +def twoArguments : Method := + { callCode with + code := [.call { (callSite word).method with parameters := [.byref word, word] }] } + +/-- A call operand with an altered argument count fails the check. -/ theorem argument_count_rejected : - ¬ ManagedCallsValid [⟨[word], closed⟩] [] (caller word) [true] - { callCode with code := [.call 0 2] } := by - fail_if_success (cil_strict ( - have invalid : ManagedCallsValid [⟨[word], closed⟩] [] (caller word) [true] - { callCode with code := [.call 0 2] } := by - cil_managed_calls [caller, closed, raw, word, callCode])) + ¬ ManagedCallsValid [⟨[word], closed⟩] [] (fun _ => false) (caller word) [true] + twoArguments := by decide +kernel #print axioms argument_count_rejected /-- A class deriving from `Test.Counter` whose instances have two fields, its base's included, - and its one-argument constructor. -/ + and its constructor, taking one `word`. -/ def tally : TypeExpr := .object "Test" "Tally" def counter : TypeExpr := .object "Test" "Counter" def tallyConstructor : RawMethod := { closed with - hasThis := true, objectReceiver := true, objectFieldCount := 2, objectBases := [counter] } + token := 2, owner := "Test::Tally", hasThis := true, returnType := .void, parameters := [word], + objectReceiver := true, objectFieldCount := 2, objectBases := [counter] } +/-- A struct `Test::Pair` and its constructor, taking one `word` after its storage's address. -/ +def pair : TypeExpr := .aggregate "Test::Pair" 16 +def pairConstructor : RawMethod := { tallyConstructor with + token := 3, owner := "Test::Pair", objectReceiver := false, objectFieldCount := 0, + objectBases := [] } +/-- The program's methods: the class constructor at index 0, the struct constructor at 1. -/ +def constructors (index : Nat) : Option SpecializedMethod := + [(⟨[], tallyConstructor⟩ : SpecializedMethod), ⟨[], pairConstructor⟩][index]? +/-- The managed site of each constructor. -/ def construction : CallSite := { - declaringType := tally, name := ".ctor", target := .managed 0, - typeArguments := [], parameterCount := 1, hasThis := true, returnsValue := false } - --- A construction allocates the call site's class, with its constructor's class's bases, at one --- slot per field of that class. -theorem construction_owner_and_size : - constructedOwnerMatches construction tallyConstructor (.newObject 0 1 tally [counter] 16) ∧ - ¬ constructedOwnerMatches construction tallyConstructor (.newObject 0 1 tally [counter] 8) ∧ - ¬ constructedOwnerMatches construction tallyConstructor (.newObject 0 1 tally [counter] 24) ∧ - ¬ constructedOwnerMatches construction tallyConstructor (.newObject 0 1 tally [] 16) ∧ - ¬ constructedOwnerMatches construction tallyConstructor - (.newObject 0 1 tally [counter, tally] 16) ∧ - ¬ constructedOwnerMatches construction tallyConstructor - (.newObject 0 1 (.object "Test" "Other") [counter] 16) ∧ - ¬ constructedOwnerMatches { construction with declaringType := word } - { tallyConstructor with objectReceiver := false } (.newObject 0 1 word [counter] 16) ∧ - ¬ constructedOwnerMatches construction { tallyConstructor with objectReceiver := false } - (.newObject 0 1 tally [counter] 16) := by decide +kernel -#print axioms construction_owner_and_size + declared := { convention := 0x20, parameters := [word] }, + method := { + owner := tally, name := name!".ctor", hasThis := true, genericArity := 0, returnType := .void, + parameters := [word] }, + name := ".ctor", target := .managed 2, typeArguments := [], parameterCount := 1, + returnsValue := false } +def pairConstruction : CallSite := { construction with method.owner := pair, target := .managed 3 } +/-- The raw `newobj` of each site. -/ +def newTally : RawInstruction := ⟨0, 115, .call construction⟩ +def newPair : RawInstruction := ⟨0, 115, .call pairConstruction⟩ + +-- A class construction is linked `.object` to the method its site names, with that class's +-- bases and one slot per field; a struct construction is linked `.aggregate` to its method. A +-- dead site passes unlinked. +theorem constructions_linked : + validateConstructionWith constructors newTally true (some (.object 0 [counter] 16)) + construction.method ∧ + validateConstructionWith constructors newPair true (some (.aggregate 1 16)) + pairConstruction.method ∧ + validateConstructionWith constructors newTally false none construction.method := by + decide +kernel +#print axioms constructions_linked + +-- Forged links: another size, other bases, a constructor of the other kind, another method, no +-- link, a constructor the site does not name, or a call opcode. +theorem forged_constructions_rejected : + ¬ validateConstructionWith constructors newTally true (some (.object 0 [counter] 8)) + construction.method ∧ + ¬ validateConstructionWith constructors newTally true (some (.object 0 [counter] 24)) + construction.method ∧ + ¬ validateConstructionWith constructors newTally true (some (.object 0 [] 16)) + construction.method ∧ + ¬ validateConstructionWith constructors newTally true + (some (.object 0 [counter, tally] 16)) construction.method ∧ + ¬ validateConstructionWith constructors newTally true (some (.aggregate 0 16)) + construction.method ∧ + ¬ validateConstructionWith constructors newPair true (some (.object 1 [] 0)) + pairConstruction.method ∧ + ¬ validateConstructionWith constructors newTally true (some (.object 1 [counter] 16)) + construction.method ∧ + ¬ validateConstructionWith constructors newTally true none construction.method ∧ + ¬ validateConstructionWith constructors newTally true (some (.object 0 [counter] 16)) + pairConstruction.method ∧ + ¬ validateConstructionWith constructors ⟨0, 40, .call construction⟩ true + (some (.object 0 [counter] 16)) construction.method := by + decide +kernel +#print axioms forged_constructions_rejected /-- A non-virtual instance method of `Tally` taking one word, and a call site of it. -/ def deposit : RawMethod := { closed with owner := "Test::Tally", hasThis := true, objectReceiver := true, parameters := [word] } def depositSite : CallSite := { - declaringType := tally, name := "Deposit", target := .managed closed.token, - typeArguments := [], parameterCount := 1, hasThis := true, returnsValue := true, - returnType := word, parameterTypes := [word] } + declared := { convention := 0x20, returnType := word, parameters := [word] }, + method := { + owner := tally, name := name!"Deposit", hasThis := true, genericArity := 0, returnType := word, + parameters := [word] }, + name := "Deposit", target := .managed closed.token, typeArguments := [], parameterCount := 1, + returnsValue := true } + +/-- The link of a call site whose callee is method 0. -/ +def linkedZero : Option Link := some (.direct 0) -- Only `callvirt` emits a receiver-checked call, counting the receiver, and only into a method -- whose receiver is an object; a `callvirt` that skips the null check as a plain call is rejected. theorem receiver_checked_calls : - validateManagedCall [⟨[], deposit⟩] [] ⟨0, 111, .call depositSite⟩ true (.callInstance 0 2) ∧ - ¬ validateManagedCall [⟨[], deposit⟩] [] ⟨0, 111, .call depositSite⟩ true (.call 0 2) ∧ - ¬ validateManagedCall [⟨[], deposit⟩] [] ⟨0, 40, .call depositSite⟩ true - (.callInstance 0 2) ∧ - ¬ validateManagedCall [⟨[], deposit⟩] [] ⟨0, 111, .call depositSite⟩ true - (.callInstance 0 1) ∧ + validateManagedCall [⟨[], deposit⟩] [] ⟨0, 111, .call depositSite⟩ true linkedZero + (.callvirt depositSite.method) ∧ + ¬ validateManagedCall [⟨[], deposit⟩] [] ⟨0, 111, .call depositSite⟩ true linkedZero + (.call depositSite.method) ∧ + ¬ validateManagedCall [⟨[], deposit⟩] [] ⟨0, 40, .call depositSite⟩ true linkedZero + (.callvirt depositSite.method) ∧ + ¬ validateManagedCall [⟨[], deposit⟩] [] ⟨0, 111, .call depositSite⟩ true linkedZero + (.callvirt { depositSite.method with parameters := [] }) ∧ ¬ validateManagedCall [⟨[], { deposit with objectReceiver := false }⟩] [] - ⟨0, 111, .call depositSite⟩ true (.callInstance 0 2) := by decide +kernel + ⟨0, 111, .call depositSite⟩ true linkedZero (.callvirt depositSite.method) := by + decide +kernel #print axioms receiver_checked_calls /-- `deposit` marked virtual, as an override or a `virtual` member is. -/ @@ -158,11 +209,47 @@ def virtualDeposit : RawMethod := { deposit with attributes := virtualMethodAttr -- A virtual body is admitted as the target of a plain `call`, which runs exactly that body, but a -- `callvirt` of it can never become a fixed-target receiver-checked call. theorem virtual_bodies_called_directly : - validateManagedCall [⟨[], virtualDeposit⟩] [] ⟨0, 40, .call depositSite⟩ true (.call 0 2) ∧ + validateManagedCall [⟨[], virtualDeposit⟩] [] ⟨0, 40, .call depositSite⟩ true + linkedZero (.call depositSite.method) ∧ ¬ validateManagedCall [⟨[], virtualDeposit⟩] [] ⟨0, 111, .call depositSite⟩ true - (.callInstance 0 2) := by decide +kernel + linkedZero (.callvirt depositSite.method) := by decide +kernel #print axioms virtual_bodies_called_directly +/-- Both receiver link kinds authenticate the slot. A virtual table requires virtual bodies. -/ +theorem receiver_links_bind_slot : + let site : RawInstruction := ⟨0, 111, .call depositSite⟩ + let other := Instruction.callvirt { depositSite.method with name := name!"Withdraw" } + let table := some (Link.virtual [(tally, 0)]) + validateManagedCall [⟨[], virtualDeposit⟩] [] site true table (.callvirt depositSite.method) ∧ + ¬ validateManagedCall [⟨[], deposit⟩] [] site true table (.callvirt depositSite.method) ∧ + ¬ validateManagedCall [⟨[], deposit⟩] [] site true linkedZero other ∧ + ¬ validateManagedCall [⟨[], virtualDeposit⟩] [] site true table other := by decide +kernel +#print axioms receiver_links_bind_slot + +/-- Managed addresses and catalog entries authenticate the token of the indexed body. -/ +theorem managed_pointer_bindings : + let first := { closed with parameters := [], returnType := .void } + let second := { first with token := 2 } + let methods : List SpecializedMethod := [⟨[], first⟩, ⟨[], second⟩] + let signature : ManagedSignature := ⟨.void, []⟩ + let identity : ManagedIdentity := ⟨first.owner, first.token, []⟩ + let site := { callSite word with + method.parameters := [], method.returnType := .void, + method.methodArguments := [], method.genericArity := 0, typeArguments := [], + parameterCount := 0, returnsValue := false } + let address := Instruction.ldftn identity signature + let load := RawInstruction.mk 0 65030 (.call site) + let indirect := RawInstruction.mk 0 41 (.indirectCall .void [] false false 0) + let transfer := Instruction.calli signature false + let target : ManagedTarget := ⟨identity, 0⟩ + let forged : ManagedTarget := ⟨{ identity with token := second.token }, 0⟩ + validateManagedCall methods [] load true (some (.address 0)) address ∧ + ¬ validateManagedCall methods [] load true (some (.address 1)) address ∧ + validateManagedCall methods [] indirect true (some (.indirect [target])) transfer ∧ + ¬ validateManagedCall methods [] indirect true (some (.indirect [target, forged])) + transfer := by decide +kernel +#print axioms managed_pointer_bindings + /-- A method with a class instance local and parameter beside a scalar of each. -/ def holder : RawMethod := { closed with parameters := [tally, word], locals := [tally, word], code := [] } diff --git a/Tests/ExtractionHomeIndexes.lean b/Tests/ExtractionHomeIndexes.lean index 2c7783a8e..4dc31db9a 100644 --- a/Tests/ExtractionHomeIndexes.lean +++ b/Tests/ExtractionHomeIndexes.lean @@ -17,21 +17,26 @@ theorem home_slots : (argumentHomeIndex [2, 0, 2] 5)[2]? = some 5 ∧ (argumentHomeIndex [2, 0, 2] 5)[1]? = none := by decide +kernel #print axioms home_slots -/-- Indexed argument validation rejects missing arguments, incorrect slots and short masks. -/ +/-- Indexed argument validation accepts a store to a homed argument and rejects missing + arguments, incorrect indexes, a store to the home slot and short masks. -/ theorem argument_bounds : + argumentInstructionsValid ⟨[.field .unsigned64], [], [.store 0]⟩ [0] + [true] [.starg 0] = true ∧ argumentInstructionsValid ⟨[.field .unsigned64], [], [.store 1]⟩ [1] - [true] [.setLocal 0] = false ∧ + [true] [.starg 1] = false ∧ + argumentInstructionsValid ⟨[.field .unsigned64], [], [.store 0]⟩ [0] + [true] [.starg 1] = false ∧ argumentInstructionsValid ⟨[.field .unsigned64], [], [.store 0]⟩ [0] - [true] [.setLocal 1] = false ∧ + [true] [.stloc 0] = false ∧ argumentInstructionsValid ⟨[.field .unsigned64], [], [.store 0]⟩ [0] - [] [.setLocal 0] = false := by decide +kernel + [] [.starg 0] = false := by decide +kernel #print axioms argument_bounds -/-- Pruned instructions must remain unsupported, even when their indexes are invalid. -/ +/-- Pruned instructions must be holes, even when their indexes are invalid. -/ theorem pruned_argument : argumentInstructionsValid ⟨[], [], [.load 99]⟩ [] - [false] [.unsupported "dead"] = true ∧ - argumentInstructionsValid ⟨[], [], [.load 99]⟩ [] [false] [.arg 99] = false := by + [false] [.unadmitted 0x0E] = true ∧ + argumentInstructionsValid ⟨[], [], [.load 99]⟩ [] [false] [.ldarg 99] = false := by decide +kernel #print axioms pruned_argument /-- A minimal raw body for metadata-only validation. -/ @@ -53,31 +58,50 @@ theorem opaque_edges : opaqueLocalIndices { raw with decide +kernel #print axioms opaque_edges -/-- A one-byte unsigned static field descriptor. -/ -def descriptor : StaticDescriptor := ⟨0, "Field", [0], none, some false, "", 1, false⟩ - /-- The matching raw static field load. -/ def load : RawInstruction := - ⟨0, 126, .field .void 0 (.atom runtimeScope "System.Byte") true⟩ + ⟨0, 126, .field .void 0 (.atom runtimeScope "System.Byte") name!"Value" true⟩ + +/-- The field `load` names. -/ +def loaded : FieldRef := + { owner := .void, index := 0, type := .atom runtimeScope "System.Byte", name := name!"Value" } + +/-- A one-byte unsigned static field descriptor of `loaded`. -/ +def descriptor : StaticDescriptor := ⟨0, loaded, [0], none, some false, 1, false⟩ /-- An emitted scalar load with caller-provided static metadata. -/ def body (sites : List (Nat × StaticDescriptor)) : Method := { - code := [.memory (.staticValue [0])], locals := [], returnsValue := false, staticSites := sites } + code := [.ldsfld loaded], locals := [], returnsValue := false, staticSites := sites } /-- Duplicate static sites use the first descriptor, rather than overwriting it. -/ theorem first_static_wins : - methodShapeInstructions [] { raw with code := [load] } + methodShapeInstructions { raw with code := [load] } (body [(0, descriptor), (0, { descriptor with scalar := some true })]) = true ∧ - methodShapeInstructions [] { raw with code := [load] } + methodShapeInstructions { raw with code := [load] } (body [(0, { descriptor with scalar := some true }), (0, descriptor)]) = false := by decide +kernel #print axioms first_static_wins +/-- A scalar static load with matching enum identities and a descriptor of the given width. -/ +private def enumLoad (name : String) (width : Nat) (scalar : Option Bool := some false) : Bool := + let field := { loaded with type := .enum "E" (.atom runtimeScope name) } + let operand := RawOperand.field field.owner field.index field.type field.name true + let site := { descriptor with field, bytes := List.replicate width 0, scalar } + methodShapeInstructions { raw with code := [{ load with operand }] } + { body [(0, site)] with code := [.ldsfld field] } + +/-- The load admits an Int32 enum and rejects Boolean, Char and native integer bases. -/ +theorem staticEnumNeedsInteger : + [enumLoad "System.Boolean" 1, enumLoad "System.Char" 2, enumLoad "System.IntPtr" 8, + enumLoad "System.Boolean" 1 none, enumLoad "System.Int32" 4] = + [false, false, false, false, true] := by decide +kernel +#print axioms staticEnumNeedsInteger + /-- Missing raw instructions, absent descriptors and wrong raw opcodes cannot pass. -/ theorem missing_static_rejects : - methodShapeInstructions [] raw (body [(0, descriptor)]) = false ∧ - methodShapeInstructions [] { raw with code := [load] } (body []) = false ∧ - methodShapeInstructions [] { raw with code := [{ load with opcode := 0 }] } + methodShapeInstructions raw (body [(0, descriptor)]) = false ∧ + methodShapeInstructions { raw with code := [load] } (body []) = false ∧ + methodShapeInstructions { raw with code := [{ load with opcode := 0 }] } (body [(0, descriptor)]) = false := by decide +kernel #print axioms missing_static_rejects diff --git a/Tests/ExtractionLayout.lean b/Tests/ExtractionLayout.lean index d9e1d7c69..e089fd36d 100644 --- a/Tests/ExtractionLayout.lean +++ b/Tests/ExtractionLayout.lean @@ -1,12 +1,18 @@ -import CIL.Extraction.LayoutUses +import CIL.Extraction.ValueDecoding +import CIL.Resolution + +/-! Raw and computed struct layouts and their field uses. -/ open CIL CIL.Extraction namespace Tests.ExtractionLayout +/-- The sequential leaf and its packed parent whose second field embeds the leaf. -/ def raw : List RawLayout := [ - { explicit := false, packing := 0, declaredSize := -1, fields := [⟨.unsigned8, none⟩, ⟨.unsigned32, none⟩] }, - { explicit := false, packing := 1, declaredSize := -1, fields := [⟨.unsigned8, none⟩, ⟨.aggregate 0, none⟩] }] + { identity := "Leaf", explicit := false, packing := 0, declaredSize := -1, + fields := [⟨.unsigned8, none⟩, ⟨.unsigned32, none⟩] }, + { identity := "Parent", explicit := false, packing := 1, declaredSize := -1, + fields := [⟨.unsigned8, none⟩, ⟨.aggregate 0, none⟩] }] def computed : Array ComputedLayout := #[ ⟨⟨8, [⟨0, 1, false⟩, ⟨4, 4, false⟩]⟩, 4⟩, @@ -35,10 +41,29 @@ example : computeLayouts [{ explicit := false, auto := true, packing := -1, decl example : computeLayouts [{ explicit := false, packing := 0, declaredSize := -1, fields := [⟨.unsigned64, none⟩] }, { explicit := false, auto := true, packing := -1, declaredSize := -1, fields := [⟨.aggregate 0, none⟩, ⟨.aggregate 0, none⟩] }] = none := by decide -def goodMethod : Method := { code := [.fieldAt 4 4], locals := [], returnsValue := true } -def wrongMethod : Method := { goodMethod with code := [.fieldAt 3 4] } -example : MethodLayoutsValid raw computed goodMethod [.field 0 0 1 .load] := by decide -example : validateLayoutUse raw computed wrongMethod (.field 0 0 1 .load) = false := by decide -example : validateLayoutUse raw computed goodMethod (.field 0 1 1 .load) = false := by decide +/-- The second field of Leaf holds an unsigned word at byte four. -/ +private def leafWord : FieldRef := + { owner := .aggregate "Leaf" 0, index := 1, name := name!"word", + type := .atom runtimeScope "System.UInt32" } + +/-- The decoded load and its execution environment share the typed field operand. -/ +theorem leaf_word_shape : + let site : RawInstruction := ⟨0, 0x7b, + .field leafWord.owner leafWord.index leafWord.type leafWord.name false⟩ + let environment : LayoutEnvironment := { rawLayouts := raw, layouts := computed } + Instruction.ofRaw? none site = some (.ldfld leafWord) ∧ + environment.fieldShape leafWord = .value 4 4 false := ⟨rfl, by decide +kernel⟩ +#print axioms leaf_word_shape + +/-- Layout validation rejects the in-bounds offset three. A correct layout also refuses a + field operand that names the parent with the leaf's row or holds the parent's nested field. -/ +theorem leaf_word_forgeries : + let environment : LayoutEnvironment := { rawLayouts := raw, layouts := computed } + ¬ LayoutsValid raw (computed.set! 0 wrong) .x64 ∧ + environment.fieldShape { leafWord with owner := .aggregate "Parent" 0 } = + .unsupported "Field owner is not a value type" ∧ + environment.fieldShape { leafWord with owner := .aggregate "Parent" 1 } = + .unsupported "Field outside the checked layouts" := by decide +kernel +#print axioms leaf_word_forgeries end Tests.ExtractionLayout diff --git a/Tests/ExtractionParameters.lean b/Tests/ExtractionParameters.lean index 900e19e70..2a134ae27 100644 --- a/Tests/ExtractionParameters.lean +++ b/Tests/ExtractionParameters.lean @@ -1,4 +1,4 @@ -import CIL.Extraction.Parameters +import CIL.Extraction.Generics open CIL CIL.Extraction @@ -10,7 +10,7 @@ def raw : RawArguments := { code := [.load 0, .store 3, .address 1, .load 3] } def reachable : List Bool := [true, true, true, true] def method : Method := { - code := [.arg 0, .setLocal 2, .localAddr 1, .local 2] + code := [.ldarg 0, .starg 3, .ldarga 1, .ldarg 3] locals := [.i64 0], localKinds := [.word64] parameterHomes := [(1, .word32), (3, .word32)] returnsValue := true } @@ -20,26 +20,212 @@ example : ParameterHomesValid #[] raw reachable method := by decide example : validateParameterHomes #[] raw reachable { method with parameterHomes := [] } = false := by decide example : validateParameterHomes #[] raw reachable { method with parameterHomes := [(3, .word32), (1, .word32)] } = false := by decide +-- A homed argument is stored, addressed and loaded by its own index, never as its home slot. example : validateParameterHomes #[] raw reachable - { method with code := [.arg 0, .setLocal 2, .localAddr 1, .arg 3] } = false := by decide + { method with code := [.ldarg 0, .stloc 2, .ldarga 1, .ldarg 3] } = false := by decide example : validateParameterHomes #[] raw reachable - { method with code := [.arg 0, .setLocal 1, .localAddr 1, .local 2] } = false := by decide + { method with code := [.ldarg 0, .starg 3, .ldloca 1, .ldarg 3] } = false := by decide +example : validateParameterHomes #[] raw reachable + { method with code := [.ldarg 0, .starg 3, .ldarga 1, .ldloc 2] } = false := by decide +example : validateParameterHomes #[] raw reachable + { method with code := [.ldarg 0, .starg 1, .ldarga 1, .ldarg 3] } = false := by decide +example : validateParameterHomes #[] raw reachable + { method with code := [.ldarg 0, .starg 3, .ldarga 3, .ldarg 3] } = false := by decide example : validateParameterHomes #[] raw reachable { method with localKinds := [.word32] } = false := by decide example : validateParameterHomes #[] raw reachable { method with locals := [] } = false := by decide example : validateParameterHomes #[] raw [true, true, true] method = false := by decide -example : validateArgumentOp raw reachable .other true (.local 2) = false := by decide -example : validateArgumentOp raw reachable .other true (.arg 0) = false := by decide +example : validateArgumentOp raw reachable .other true (.ldloc 2) = false := by decide +example : validateArgumentOp raw reachable .other true (.ldarg 0) = false := by decide +example : validateArgumentOp raw reachable .other true (.starg 3) = false := by decide +example : validateArgumentOp raw reachable .other true (.ldarga 1) = false := by decide +-- A local operand must name one of the declared locals: two locals admit index 1 and reject 2. +def twoLocals : RawArguments := { raw with locals := [.field .unsigned64, .field .unsigned64] } +example : validateArgumentOpIn twoLocals [] .other true (.ldloc 1) = true := by decide +example : validateArgumentOpIn twoLocals [] .other true (.ldloc 2) = false := by decide example : validateParameterHomes #[] { raw with code := [.load 0, .store 9, .address 1, .load 3] } reachable method = false := by decide --- Dead argument stores create no home; their emitted site must actually be unsupported. +-- A Byte parameter is loaded by `ldarg` of its own index: the step narrows it from the declared +-- signature, so the evidence compares no narrowing. A homed parameter is loaded the same way, +-- and a misnumbered load is rejected. +def byteArgument : RawArguments := + { arguments := [.field .unsigned32, .field .unsigned8], locals := [], code := [.load 1] } +example : validateArgumentOpIn byteArgument [] (.load 1) true (.ldarg 1) = true := by decide +example : validateArgumentOpIn byteArgument [] (.load 1) true (.ldarg 0) = false := by decide +example : validateArgumentOpIn byteArgument [1] (.load 1) true (.ldarg 1) = true := by decide +example : validateArgumentOpIn byteArgument [1] (.load 1) true (.ldloc 0) = false := by decide +example : validateArgumentOpIn byteArgument [] (.store 1) true (.starg 1) = false := by decide + +-- The address of a span parameter is `ldarga` of the parameter, whose step pushes the view its +-- home holds; the address of the home slot or a load in its place is rejected. +def spanArgument : RawArguments := + { arguments := [.span 4 true], locals := [], code := [.address 0] } +example : validateArgumentOpIn spanArgument [0] (.address 0) true (.ldarga 0) = true := by + decide +example : validateArgumentOpIn spanArgument [0] (.address 0) true (.ldloca 0) = false := by + decide +example : validateArgumentOpIn spanArgument [0] (.address 0) true (.ldarg 0) = false := by + decide +-- An argument without a home has no address to take. +example : validateArgumentOpIn spanArgument [] (.address 0) true (.ldarga 0) = false := by + decide + +-- Dead argument stores create no home; their emitted site must be a hole. def pruned : RawArguments := { arguments := [.field .unsigned32], locals := [], code := [.store 0, .load 0] } example : ParameterHomesValid #[] pruned [false, true] - { code := [.unsupported "pruned", .arg 0], locals := [], returnsValue := true } := by decide + { code := [.unadmitted 0x10, .ldarg 0], locals := [], returnsValue := true } := by decide example : validateParameterHomes #[] pruned [false, true] - { code := [.setLocal 0, .arg 0], locals := [], returnsValue := true } = false := by decide + { code := [.starg 0, .ldarg 0], locals := [], returnsValue := true } = false := by decide + +/-- An `Option` `mapM` relates each output entry to the input entry at the same index. -/ +private theorem mapM_entry {α β : Type} {f : α → Option β} : + ∀ {inputs : List α} {outputs : List β}, inputs.mapM f = some outputs → + ∀ {index : Nat} {output : β}, outputs[index]? = some output → + ∃ input, inputs[index]? = some input ∧ f input = some output + | [], outputs, mapped, index, output, found => by + simp only [List.mapM_nil, pure, Option.some.injEq] at mapped + subst mapped + simp at found + | input :: inputs, outputs, mapped, index, output, found => by + simp only [List.mapM_cons, bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at mapped + obtain ⟨head, headMapped, tail, tailMapped, rfl⟩ := mapped + cases index with + | zero => + simp only [List.getElem?_cons_zero, Option.some.injEq] at found + subst found + exact ⟨input, rfl, headMapped⟩ + | succ index => exact mapM_entry tailMapped found +#print axioms mapM_entry + +/-- Whether a slot kind is a span slot. -/ +private def spanSlot : LocalKind → Bool + | .span .. => true + | _ => false + +/-- Whether metadata storage is span storage, of primitive or aggregate elements. -/ +private def spanStored : StorageType → Bool + | .span .. | .aggregateSpan .. => true + | _ => false + +/-- Only span storage gives a span slot. -/ +private theorem storageKind_span {computed : Array ComputedLayout} {storage : StorageType} + {kind : LocalKind} (bound : storageKind computed storage = some kind) : + spanSlot kind = spanStored storage := by + revert bound + cases storage <;> (try cases ‹FieldType›) <;> + simp [storageKind, spanSlot, spanStored, bind, Option.bind_eq_some_iff] <;> + (intros; subst_vars; rfl) +#print axioms storageKind_span + +/-- Native dispatch storage is never span storage, and its types are never spans. -/ +private theorem nativeDispatchStorage_span {context : NativeDispatchContext} {type : TypeExpr} + {localIndex : Option Nat} {pointer : Nat → Bool} {storage : StorageType} + (stored : nativeDispatchStorage context type localIndex pointer = some storage) : + spanStored storage = false ∧ spanLocal type = false := by + revert stored + cases type <;> simp [nativeDispatchStorage, nativeDispatchInteger, bind, Option.bind_eq_some_iff, + spanLocal, spanStorage] <;> (intros; subst_vars; rfl) +#print axioms nativeDispatchStorage_span + +/-- A generic instance's storage is span storage exactly for a span type. -/ +private theorem typeStorage_apply_span {layouts : List RawLayout} {scope name : String} + {element : TypeExpr} {storage : StorageType} + (stored : typeStorage layouts (.apply (.constructor scope name) element) = some storage) : + spanStored storage = spanLocal (.apply (.constructor scope name) element) := by + unfold spanLocal + cases spanned : spanStorage (.apply (.constructor scope name) element) with + | none => + simp only [typeStorage, typeStorageAt, spanned] at stored + simp only [Option.isSome_none] + revert stored + cases storage <;> simp only [spanStored] <;> (repeat' split) <;> simp + | some span => + have shape : spanStored span = true := by + revert spanned + simp only [spanStorage, bind] + split + · simp + · rw [Option.bind_eq_some_iff] + rintro ⟨_, -, found⟩ + simp only [pure, Option.some.injEq] at found + subst found + rfl + simp only [typeStorage, typeStorageAt, spanned] at stored + simp only [Option.isSome_some] + cases element + case aggregate identity index => + revert stored + simp only [bind, Option.bind_eq_some_iff] + rintro ⟨_, -, found⟩ + split at found + · simp at found + · simp only [pure, Option.some.injEq] at found + subst found + rfl + all_goals + simp only [pure, Option.some.injEq] at stored + subst stored + exact shape +#print axioms typeStorage_apply_span + +/-- Closed metadata storage is span storage exactly for a span type. -/ +private theorem typeStorage_span {layouts : List RawLayout} {type : TypeExpr} + {storage : StorageType} (stored : typeStorage layouts type = some storage) : + spanStored storage = spanLocal type := by + cases type + case apply constructor element => + cases constructor + case constructor scope name => exact typeStorage_apply_span stored + all_goals simp [typeStorage, typeStorageAt] at stored + all_goals unfold typeStorage typeStorageAt at stored + all_goals (try split at stored) <;> cases storage <;> + simp_all [spanStored, spanLocal, spanStorage, bind, Option.bind_eq_some_iff] + all_goals + (try obtain ⟨_, -, stored⟩ := stored) + (repeat' split at stored) <;> simp_all +#print axioms typeStorage_span + +/-- A local's storage is span storage exactly for a span type, whether native dispatch or the + metadata type chose it. -/ +private theorem orElse_span {native : Option StorageType} {layouts : List RawLayout} + {type : TypeExpr} {storage : StorageType} + (nativeSpan : ∀ dispatched, native = some dispatched → + spanStored dispatched = false ∧ spanLocal type = false) + (chosen : (native <|> typeStorage layouts type) = some storage) : + spanStored storage = spanLocal type := by + cases native with + | none => exact typeStorage_span chosen + | some dispatched => + obtain ⟨notStored, notSpan⟩ := nativeSpan dispatched rfl + cases Option.some.inj chosen + rw [notStored, notSpan] +#print axioms orElse_span + +/-- On a method whose argument storage and slot kinds bind, a local's slot is a span slot exactly + when its declared type is a span, so the slot `localAddress` reads and the declared type the + extractor reads agree on span locals. -/ +theorem span_kind_iff_spanLocal {layouts : List RawLayout} {computed : Array ComputedLayout} + {closed : RawMethod} {raw : RawArguments} {kinds : List LocalKind} {index : Nat} + {type : TypeExpr} {kind : LocalKind} + (arguments : closedArguments layouts closed = some raw) + (bound : raw.locals.mapM (storageKind computed) = some kinds) + (declared : closed.locals[index]? = some type) (slot : kinds[index]? = some kind) : + spanLocal type = (kind matches .span ..) := by + obtain ⟨storage, stored, kindOf⟩ := mapM_entry bound slot + rw [closedArguments_eq_list] at arguments + simp only [closedArgumentsWith, bind, Option.bind_eq_some_iff, pure, Option.some.injEq] + at arguments + obtain ⟨parameters, -, locals, localsMapped, code, -, rfl⟩ := arguments + obtain ⟨⟨declaredType, position⟩, entry, chosen⟩ := mapM_entry localsMapped stored + rw [List.getElem?_zipIdx, declared] at entry + simp only [Option.map_some, Option.some.injEq, Prod.mk.injEq] at entry + obtain ⟨rfl, rfl⟩ := entry + have storedSpan := orElse_span (fun _ dispatched => nativeDispatchStorage_span dispatched) chosen + rw [← storedSpan, ← storageKind_span kindOf] + cases kind <;> rfl +#print axioms span_kind_iff_spanLocal end Tests.ExtractionParameters diff --git a/Tests/FSharp.cs b/Tests/FSharp.cs index cffa4f6f8..cfd508e4e 100644 --- a/Tests/FSharp.cs +++ b/Tests/FSharp.cs @@ -1,7 +1,13 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs +#:include ContractVariants.cs using System.Reflection; using System.Runtime.Loader; @@ -58,9 +64,10 @@ queue.Add(new($"Arithmetic::{name}({parameters})", proof, output), () => { var extracted = File.ReadAllText(Path.Combine(output, "Extracted.lean")); - if (name is "Calls" or "Mutate" && !extracted.Contains(".call ")) + // A compiled call's target is the direct link at its pc. + if (name is "Calls" or "Mutate" && !extracted.Contains(" : Link := .direct ", StringComparison.Ordinal)) throw new InvalidOperationException($"{name} no longer exercises compiled method calls"); - if (name == "Mutate" && !extracted.Contains(".newAggregate ")) + if (name == "Mutate" && !extracted.Contains(": Link := .aggregate ")) throw new InvalidOperationException("Mutate no longer exercises struct construction"); cases += claims.Count; Console.WriteLine($"PASS F# {name}: {claims.Count} runtime comparisons"); @@ -79,19 +86,28 @@ queue.Flush(); -var wrongOutput = Path.Combine(root, "WrongAddition"); -var failed = false; -try -{ - verification.Verify(assembly, "Examples.FSharp", "Arithmetic::WrongAdd(ulong,ulong)", - At("Examples/Proofs/Add.lean"), wrongOutput); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - failed = true; -} -if (!failed || File.Exists(Path.Combine(wrongOutput, "verified.json"))) - throw new InvalidOperationException("Incorrect F# addition accepted"); +verification.VerifyContractVariant(assembly, "Examples.FSharp", "Arithmetic::Add(ulong,ulong)", + Path.Combine(root, "AdditionContract"), assembly, "Arithmetic::WrongAdd(ulong,ulong)", + Path.Combine(root, "WrongAddition"), """ + /-- Addition returns the wrapping sum and preserves caller memory. -/ + def addition : MethodContract (W64 × W64) W64 where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post input initial final value := final = initial ∧ value = input.1 + input.2 + """, """ + /-- The compiled F# addition meets the contract for every pair of words. -/ + theorem correctness : addition.Holds Extracted.program 0 := by + rintro ⟨left, right⟩ memory - + exact MethodContract.Holds.intro memory (left + right) ⟨4, rfl⟩ (Confined.refl _ _) ⟨rfl, rfl⟩ + """, """ + /-- Subtraction at (0, 1) returns the largest word. The addition contract requires one. -/ + theorem correctness : addition.Refuted Extracted.program 0 := + .of_return (input := (0, 1)) (initial := Memory.empty) trivial + (rfl : invoke Extracted.program 4 0 (addition.args (0, 1)) Memory.empty = .ok _) + (by rintro ⟨-, value, encoded, -, rfl⟩; revert encoded; decide) + """); var rejectedOutput = Path.Combine(root, "UnsupportedList"); var rejected = Execute(Extract("extract", assembly, "Examples.FSharp", "Arithmetic::UnsupportedList(ulong)", rejectedOutput)); diff --git a/Tests/FailureReporting.cs b/Tests/FailureReporting.cs index 864311464..5c37632b1 100644 --- a/Tests/FailureReporting.cs +++ b/Tests/FailureReporting.cs @@ -1,11 +1,17 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; // failed.json classifies each failure, points at the obligation in the user's file and never -// reports an unproved goal as a disproved contract; only a verified `Not (contract.Holds ...)` is. +// reports an unproved goal as a disproved contract; only a verified counterexample (`contract.Refuted ...`) +// is, and a theorem stating `Not (contract.Holds ...)` from a run the model does not cover is refused. var cli = new[] { "dotnet", ExtractorPath }; var root = Path.GetFullPath(At("artifacts/FailureReporting")); var package = Path.Combine(root, "Package"); @@ -19,7 +25,8 @@ string Build(string name, string helpers) var source = Path.Combine(root, name); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Contracts.csproj"), """ - net10.0true + net11.0 + true """); File.WriteAllText(Path.Combine(source, "Contracts.cs"), "namespace Contracts; public static class Calls { " + helpers + "public static void Root(ref ulong r, in ulong b) { AddTo(in r, in b, out r); r = Mix(r, b); } }"); @@ -109,14 +116,10 @@ dsimp only [addContract] def empty : Memory := { allocations := fun _ => none, cells := fun _ _ => ⟨0, false⟩, views := [], nextIdentity := 0 } /-- This build returns `Mix(0, 0) = 1`; the contract requires `0 * 3 + 0`. -/ -theorem correctness : ¬ mixContract.Holds Extracted.program mix := by - intro holds - obtain ⟨_, ⟨final, values⟩, ran, -, out, encoded, wrong⟩ := holds (0, 0) empty trivial - obtain ⟨rfl, rfl⟩ := Prod.mk.inj (invoke_ok_unique ran (rfl : invoke Extracted.program 16 mix - (mixContract.args (0, 0)) empty = .ok _)) - simp only [mixContract] at encoded wrong - subst wrong - simp at encoded +theorem correctness : mixContract.Refuted Extracted.program mix := + .of_return (input := (0, 0)) (initial := empty) trivial + (rfl : invoke Extracted.program 16 mix (mixContract.args (0, 0)) empty = .ok _) + (by rintro ⟨-, out, encoded, wrong⟩; simp only [mixContract] at encoded wrong; subst wrong; simp at encoded) """; File.WriteAllText(Path.Combine(package, "Client", "Contracts.lean"), Contracts); string Proof(string name, string body) @@ -290,7 +293,10 @@ open Lean Elab Tactic in var refutation = results[2].GetProperty("failure"); var (refutedLine, _) = Lines(refuted); +// The refutation names the callee it refutes, not the extraction's entry. if (Text(refutation, "kind") != "refutation" || Text(refutation, "file") != refuted || Number(refutation, "line") != refutedLine || + refutation.GetProperty("method").GetProperty("entry").GetString() != + "System.UInt64 Contracts.Calls::Mix(System.UInt64,System.UInt64)" || Number(refutation, "column") != "theorem ".Length || !File.Exists(Path.Combine(outputs["Refuted"], "verified.json")) || results.Where((_, i) => i != 2).Any(item => item.GetProperty("failure") is { ValueKind: JsonValueKind.Object } other && Text(other, "kind") == "refutation")) @@ -304,3 +310,155 @@ open Lean Elab Tactic in !stale.GetProperty("message").GetString()!.Contains("assembly has changed since extraction", StringComparison.Ordinal)) throw new InvalidOperationException("Unexpected stale assembly report:\n" + stale); Console.WriteLine("PASS a replaced assembly is a stale input naming the extracted method and its recorded assembly"); + +// SameText returns true on the CLR from every memory; with no object bound to its literal the model stops as +// unsupported, which proves the negation of the contract but is no counterexample, so the audit refuses it. +const string Forged = """ + import Extracted + import CIL.Safety.SymbolicExecution + import CIL.Safety.MethodContract + + /-! A forged refutation. `Examples.Strings.SameText()` returns true on the CLR from every + memory, but with no object bound to "x" the model stops as unsupported at the first load. -/ + + open CIL CIL.Safety + namespace Forge + + /-- `SameText` returns true from any memory; true on the CLR. -/ + def sameTextContract : MethodContract Unit where + args _ := [] + pre _ _ := True + footprint _ := [] + post _ _ _ values := values = [.scalar (.i32 1)] + + /-- Refutes the contract from an unsupported fault alone, without any refutation lemma. -/ + theorem refuted : ¬ sameTextContract.Holds Extracted.program 0 := by + intro holds + obtain ⟨fuel, result, ran, -⟩ := holds () Memory.empty trivial + have unbound : Memory.empty.literals.lookup "x" = none := rfl + have missing : literalObject Memory.empty "x" = + .error (.unsupported "String literal is not bound by the caller") := by + simp only [literalObject, unbound] + have stuck : ∃ observed, invoke Extracted.program observed 0 [] Memory.empty = + .error ⟨0, 0, .unsupported "String literal is not bound by the caller"⟩ := by + cil_checked_steps [missing] + obtain ⟨observed, stuck⟩ := stuck + have up := invoke_mono Extracted.program observed fuel 0 [] Memory.empty _ + (by simp [Stable]) stuck + have down := invoke_mono Extracted.program fuel observed 0 [] Memory.empty (.ok result) + trivial ran + rw [Nat.add_comm, up] at down + cases down + + #print axioms refuted + end Forge + """; +var sameText = Path.Combine(root, "Out", "SameText"); +Run([.. cli, "extract", At("Examples/Assembly/bin/Examples.dll"), "Strings::SameText()", sameText, "scalar", + "--assembly", "Examples"]); +var forgedProof = Path.Combine(root, "Forged.lean"); +File.WriteAllText(forgedProof, Forged); +var forgedRun = Execute([.. cli, "verify", sameText, "--proof", forgedProof, "--theorem", "Forge.refuted"]); +var forgedReport = Failure(sameText); +if (forgedRun.Code != 1 || File.Exists(Path.Combine(sameText, "verified.json")) || + Text(forgedReport, "kind") != "replay-or-audit-rejected" || + Text(forgedReport, "message")?.Contains("which a run the model does not cover can prove", + StringComparison.Ordinal) != true) + throw new InvalidOperationException("A negated contract from an unsupported fault was not refused:\n" + + forgedRun.Output); +Console.WriteLine("PASS a negated contract proved from a run the model does not cover is refused as no " + + "counterexample"); + +// A genuine counterexample about another program refutes nothing in this extraction, so verify refuses it. +const string Elsewhere = """ + import Extracted + import CIL.Safety.MethodContract + + /-! A counterexample about the empty program, verified against an extraction it does not name. -/ + + open CIL.Safety + namespace Elsewhere + + /-- Promises a return from any memory. -/ + def returns : MethodContract Unit := + { args := fun _ => [], pre := fun _ _ => True, footprint := fun _ => [], post := fun _ _ _ _ => True } + + /-- The empty program has no method zero, so a call to it faults. -/ + theorem refuted : returns.Refuted [] 0 := + .of_fault (input := ()) (initial := Memory.empty) (observed := 1) + (fault := { method := 0, instruction := 0, fault := .invalidState }) trivial (by decide) rfl + + #print axioms refuted + end Elsewhere + """; +var elsewhereProof = Path.Combine(root, "Elsewhere.lean"); +File.WriteAllText(elsewhereProof, Elsewhere); +var elsewhereRun = Execute([.. cli, "verify", sameText, "--proof", elsewhereProof, "--theorem", "Elsewhere.refuted"]); +var elsewhereReport = Failure(sameText); +if (elsewhereRun.Code != 1 || File.Exists(Path.Combine(sameText, "verified.json")) || + Text(elsewhereReport, "kind") != "replay-or-audit-rejected" || + Text(elsewhereReport, "message")?.Contains("a refutation states Extracted.program", + StringComparison.Ordinal) != true) + throw new InvalidOperationException( + $"A refutation of another program was reported against this one:\n{elsewhereRun.Output}"); +Console.WriteLine("PASS a counterexample about another program is refused, not reported against the extraction"); + +// A refutation at the first index past the extraction's methods names no method of it, so verify refuses it. +var sameTextIdentity = File.ReadAllText(Path.Combine(sameText, "identity.json")); +var sameTextMethods = JsonSerializer.Deserialize(sameTextIdentity).GetProperty("methods") + .GetArrayLength(); +var outOfRange = $$""" + import Extracted + import CIL.Safety.MethodContract + + /-! A counterexample at the first method index the extraction does not have. -/ + + open CIL.Safety + namespace Past + + /-- Promises a return from any memory. -/ + def returns : MethodContract Unit := + { args := fun _ => [], pre := fun _ _ => True, footprint := fun _ => [], post := fun _ _ _ _ => True } + + /-- The extraction has no method at this index, so a call to it faults. -/ + theorem refuted : returns.Refuted Extracted.program {{sameTextMethods}} := + .of_fault (input := ()) (initial := Memory.empty) (observed := 1) + (fault := { method := {{sameTextMethods}}, instruction := 0, fault := .invalidState }) + trivial (by decide) rfl + + #print axioms refuted + end Past + """; +var outOfRangeProof = Path.Combine(root, "Past.lean"); +File.WriteAllText(outOfRangeProof, outOfRange); +var outOfRangeRun = Execute([.. cli, "verify", sameText, "--proof", outOfRangeProof, "--theorem", "Past.refuted"]); +var outOfRangeReport = Failure(sameText); +var pastLast = $"method {sameTextMethods}; a refutation states Extracted.program and one of its { + sameTextMethods} methods"; +if (outOfRangeRun.Code != 1 || File.Exists(Path.Combine(sameText, "verified.json")) || + Text(outOfRangeReport, "kind") != "replay-or-audit-rejected" || + Text(outOfRangeReport, "message")?.Contains(pastLast, StringComparison.Ordinal) != true) + throw new InvalidOperationException( + $"A refutation past the extraction's last method was not refused:\n{outOfRangeRun.Output}"); +Console.WriteLine("PASS a counterexample at the first index past the extraction's methods is refused"); + +// A refutation may name the extraction's program through a definition whose value is that constant. +var aliasRefuted = Path.Combine(root, "Out", "AliasRefuted"); +Run([.. cli, "extract", arithmetic, Selector, aliasRefuted]); +var aliasProof = Path.Combine(root, "AliasRefuted.lean"); +File.WriteAllText(aliasProof, $$""" + {{Contracts.Replace("namespace Client", "namespace ExampleProof").Replace("end Client", "")}} + /-- The extraction's program under another name. -/ + def aliasedProgram : CIL.Program := Extracted.program + + {{Refutation.Replace("Refuted Extracted.program mix", "Refuted aliasedProgram mix")}} + end ExampleProof + """); +var aliasRun = Execute([.. cli, "verify", aliasRefuted, "--proof", aliasProof]); +var aliasReport = Failure(aliasRefuted); +if (Text(aliasReport, "kind") != "refutation" || !File.Exists(Path.Combine(aliasRefuted, "verified.json")) || + aliasReport.GetProperty("method").GetProperty("entry").GetString() != + "System.UInt64 Contracts.Calls::Mix(System.UInt64,System.UInt64)") + throw new InvalidOperationException( + $"A refutation naming an alias of the program was not bound:\n{aliasRun.Output}"); +Console.WriteLine("PASS a refutation naming the program through an alias is bound to the refuted method"); diff --git a/Tests/FeatureEvidence.cs b/Tests/FeatureEvidence.cs index cd9c4aae3..0c42df5f8 100644 --- a/Tests/FeatureEvidence.cs +++ b/Tests/FeatureEvidence.cs @@ -2,11 +2,17 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include ../scripts/BatchVerification.cs #:include RejectedEvidence.cs #:include GeneratedEvidence.cs #:include ../scripts/RuntimeProfileValidation.cs +#:include PinnedMetadata.cs using System.Text.RegularExpressions; using System.Reflection; @@ -32,7 +38,7 @@ // that omit or split a nested flag are refused. foreach (var (nested, baseFlag, getter, _) in RuntimeProfileValidation.NestedGetters) { - var evidence = nested is "advSimd64" or "armAes64" ? "macos-arm64-net10.json" : "windows-x64-net10.json"; + var evidence = nested is "advSimd64" or "armAes64" ? "macos-arm64-net10.json" : "windows-x64-net11.json"; var recorded = JsonNode.Parse(File.ReadAllText(At($"docs/evidence/runtime-profiles/{evidence}")))!; foreach (var (name, mutate) in new (string, Action)[] { @@ -53,7 +59,7 @@ Console.WriteLine($"PASS {getter} observation rejected: {name}"); } } -foreach (var name in new[] { "scalar", "x64-sse2", "x64-sse42", "x64-v3", "x64-v4", "arm64-advsimd" }) +foreach (var name in new[] { "scalar", "x64-sse42", "x64-v3", "x64-v4", "arm64-advsimd" }) sharedCases.Add((JsonSerializer.Serialize(SelectProfile(name), jsonOptions), true)); foreach (var name in new[] { "x64-avx2", "x64-avx2-bmi1", "x64-avx512", "x64-avx512-bmi1", "x64-bmi2", "x64-vector256", "x64-sse41", "arm64-armbase" }) @@ -74,7 +80,7 @@ ("x64-v4", "avx512Vbmi", true, true), ("x64-v3", "avx512Vbmi", true, false), ("arm64-advsimd", "avx512Vbmi", true, false), // AES is an optional group on each architecture that needs only that architecture's base. - ("x64-sse2", "aes", true, true), ("x64-v4", "aes", true, true), ("arm64-advsimd", "aes", true, false), + ("x64-sse42", "aes", true, true), ("x64-v4", "aes", true, true), ("arm64-advsimd", "aes", true, false), ("arm64-advsimd", "armAes", true, true), ("x64-v4", "armAes", true, false), ("scalar", "aes", true, false), ("scalar", "armAes", true, false) }) @@ -92,6 +98,29 @@ catch (TargetInvocationException exception) when (!accepted && exception.InnerException is InvalidDataException) { } } Console.WriteLine("PASS runtime profile groups, retired profiles and reduced vector width"); +// No .NET 11 x64 process reports SSE2 without SSSE3, SSE4.1 and SSE4.2, so that set is rejected with the gap. +var belowBaseline = JsonSerializer.SerializeToNode(SelectProfile("x64-sse42"), jsonOptions)!; +foreach (var flag in new[] { "ssse3", "sse41", "sse42" }) belowBaseline[flag] = false; +belowBaseline["name"] = "sse2-only"; +sharedCases.Add((belowBaseline.ToJsonString(), false)); +var belowBaselinePath = Path.Combine(output, "sse2-only.json"); +File.WriteAllText(belowBaselinePath, belowBaseline.ToJsonString()); +const string missingBaseline = "sse2-only: below the .NET 11 x64 baseline (x86-64-v2); missing Ssse3, Sse41, Sse42"; +try +{ + _ = SelectProfile("@" + belowBaselinePath); + throw new InvalidOperationException("Profile below the x64 baseline accepted: " + belowBaselinePath); +} +catch (TargetInvocationException exception) when (exception.InnerException is InvalidDataException below && + below.Message.Contains(missingBaseline, StringComparison.Ordinal)) { } +try +{ + _ = SelectProfile("x64-sse2"); + throw new InvalidOperationException("Built-in profile below the x64 baseline accepted: x64-sse2"); +} +catch (TargetInvocationException exception) when (exception.InnerException is ArgumentException unknown && + unknown.Message.Contains("Unknown execution profile: x64-sse2", StringComparison.Ordinal)) { } +Console.WriteLine("PASS x64 feature sets below the .NET 11 baseline rejected with the missing sets"); RequireLeanFixtures("CIL.Extraction.Features", "CIL.SymbolicExecution", "CIL.Safety.Execution"); var sharedEvidence = Path.Combine(output, "RuntimeProfiles.lean"); RuntimeProfileValidation.Check(sharedCases, ExtractorPath, sharedEvidence); @@ -118,16 +147,16 @@ void Reject(string name, string mutated) "Extraction.FeatureFlowValid", "is false"); } -string MutateState(string source, string update) => Regex.Replace(source, - @"(?m)^(def method0FeatureStates : Extraction.FeatureCertificate := )([^\r\n]+)", - match => match.Groups[1].Value + "(" + match.Groups[2].Value + ")." + update); +string MutateState(string source, string update) => FeatureLayoutPattern.FeatureStates.Replace(source, + match => $"""{match.Groups[1].Value}({match.Groups[2].Value}).{update}"""); // Each mutation checks feature flow independently, without unrelated validator proofs // or instruction lookup equations that could reject it for a different reason. -Reject("wrong-feature", baseline.Replace(".feature .avx2", ".feature .sse2")); -Reject("wrong-branch", baseline.Replace(".brzero 10", ".brzero 11")); +Reject("wrong-feature", ForgeDecodedInstruction(baseline, 0, 3, "fun _ => .call { owner := .atom Extraction.intrinsicsScope \"System.Runtime.Intrinsics.X86.Sse2\", " + + "name := name!\"get_IsSupported\", hasThis := false, genericArity := 0, returnType := .atom Extraction.runtimeScope \"System.Boolean\", parameters := [] }")); +Reject("wrong-branch", ForgeDecodedInstruction(baseline, 0, 4, "fun _ => .brfalse 11")); Reject("forged-constant", baseline.Replace("stack := [some (BitVec.ofInt 32 (0))]", "stack := [some (BitVec.ofInt 32 (1))]")); -Reject("malformed-entry", MutateState(baseline, "set 0 (some { stack := [none], locals := [none] })")); +Reject("malformed-entry", MutateState(baseline, "set 0 (some { stack := [], locals := [(0, BitVec.ofInt 32 (0))] })")); Reject("false-pruned-successor", MutateState(baseline, "set 10 none")); Reject("omitted-states", MutateState(baseline, "take 17")); var unsupported = Execute(Extract("extract", assembly, "Examples", "Loops::FeatureCountdown(uint)", @@ -136,6 +165,20 @@ string MutateState(string source, string update) => Regex.Replace(source, throw new InvalidOperationException("Enabled feature failed to expose unsupported array code: " + unsupported.Output); // X86Base.X64 and Vector128 getters decide their branches from the profile alone. var capabilitiesQueue = new GeneratedQueue(verification, assembly); + +static void WriteResultProof(string path, int value) => + File.WriteAllText(path, $$""" + import Extracted + import CIL.Safety.Execution + namespace ExampleProof + theorem correctness : + (CIL.Safety.invoke Extracted.program 64 0 [] CIL.Safety.Memory.empty).map Prod.snd = + .ok [.scalar (.i32 {{value}})] := by rfl + #print axioms correctness + end ExampleProof + + """); + var platformResult = Assembly.Load(File.ReadAllBytes(assembly)).GetType("Examples.Capabilities")! .GetMethod("Platform")!.Invoke(null, []); if (!Equals(platformResult, 17)) throw new InvalidOperationException("Platform comparison requires a 64-bit process"); @@ -144,29 +187,22 @@ string MutateState(string source, string update) => Regex.Replace(source, var capabilities = Path.Combine(output, "Capabilities-" + profile); Directory.CreateDirectory(capabilities); var proof = Path.Combine(capabilities, "Proof.lean"); - File.WriteAllText(proof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\ntheorem correctness :\n" + - $" (CIL.Safety.invoke Extracted.program 64 0 [] CIL.Safety.Memory.empty).map Prod.snd = .ok [.scalar (.i32 {flags})] := rfl\nend ExampleProof\n"); + WriteResultProof(proof, flags); capabilitiesQueue.Add(new("Capabilities::Flags()", proof, Path.Combine(capabilities, "Verified"), Profile: profile), () => Console.WriteLine($"PASS Capabilities::Flags under {profile} returns {flags}")); var platform = Path.Combine(capabilities, "Platform.lean"); - File.WriteAllText(platform, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + - "theorem correctness : (CIL.Safety.invoke Extracted.program 64 0 [] CIL.Safety.Memory.empty).map Prod.snd = " + - ".ok [.scalar (.i32 17)] := rfl\n#print axioms correctness\nend ExampleProof\n"); + WriteResultProof(platform, 17); capabilitiesQueue.Add(new("Capabilities::Platform()", platform, Path.Combine(capabilities, "Platform"), Profile: profile), () => Console.WriteLine($"PASS platform constants and pruned unsupported branch under {profile}")); // AdvSimd.Arm64 and AdvSimd share one answer, so ArmFlags is 0 or 3 and never 1 or 2. var armFlags = profile == "arm64-advsimd" ? 3 : 0; var armProof = Path.Combine(capabilities, "ArmFlags.lean"); - File.WriteAllText(armProof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + - "theorem correctness : (CIL.Safety.invoke Extracted.program 64 0 [] CIL.Safety.Memory.empty).map Prod.snd = " + - $".ok [.scalar (.i32 {armFlags})] := rfl\n#print axioms correctness\nend ExampleProof\n"); + WriteResultProof(armProof, armFlags); capabilitiesQueue.Add( new("Capabilities::ArmFlags()", armProof, Path.Combine(capabilities, "ArmFlags"), Profile: profile), () => Console.WriteLine($"PASS AdvSimd.Arm64 follows AdvSimd under {profile}: {armFlags}")); var endianProof = Path.Combine(capabilities, "Endian.lean"); - File.WriteAllText(endianProof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + - "theorem correctness : (CIL.Safety.invoke Extracted.program 64 0 [] CIL.Safety.Memory.empty).map Prod.snd = " + - ".ok [.scalar (.i32 1)] := rfl\n#print axioms correctness\nend ExampleProof\n"); + WriteResultProof(endianProof, 1); capabilitiesQueue.Add( new("Capabilities::Endian()", endianProof, Path.Combine(capabilities, "Endian"), Profile: profile), () => Console.WriteLine($"PASS BitConverter.IsLittleEndian prunes the big-endian branch under {profile}")); @@ -186,16 +222,7 @@ string MutateState(string source, string update) => Regex.Replace(source, var vbmiFolder = Path.Combine(output, $"Vbmi-{label}"); Directory.CreateDirectory(vbmiFolder); var vbmiProof = Path.Combine(vbmiFolder, "VbmiFlags.lean"); - File.WriteAllText(vbmiProof, $$""" - import Extracted - import CIL.Safety.Execution - namespace ExampleProof - theorem correctness : (CIL.Safety.invoke Extracted.program 64 0 [] CIL.Safety.Memory.empty).map Prod.snd = - .ok [.scalar (.i32 {{vbmiFlags}})] := rfl - #print axioms correctness - end ExampleProof - - """); + WriteResultProof(vbmiProof, vbmiFlags); capabilitiesQueue.Add( new("Capabilities::VbmiFlags()", vbmiProof, Path.Combine(vbmiFolder, "VbmiFlags"), Profile: profile), () => Console.WriteLine($"PASS Avx512Vbmi.VL follows Avx512Vbmi under {label}: {vbmiFlags}")); @@ -239,16 +266,7 @@ string CustomProfile(string baseName, string name, string flag) var aesFolder = Path.Combine(output, $"Aes-{label}"); Directory.CreateDirectory(aesFolder); var aesProof = Path.Combine(aesFolder, "AesFlags.lean"); - File.WriteAllText(aesProof, $$""" - import Extracted - import CIL.Safety.Execution - namespace ExampleProof - theorem correctness : (CIL.Safety.invoke Extracted.program 64 0 [] CIL.Safety.Memory.empty).map Prod.snd = - .ok [.scalar (.i32 {{aesFlags}})] := rfl - #print axioms correctness - end ExampleProof - - """); + WriteResultProof(aesProof, aesFlags); capabilitiesQueue.Add( new("Capabilities::AesFlags()", aesProof, Path.Combine(aesFolder, "AesFlags"), Profile: profile), () => Console.WriteLine($"PASS Aes getters follow their architecture's flag under {label}: {aesFlags}")); @@ -288,50 +306,53 @@ end ExampleProof var referenceResult = Assembly.Load(File.ReadAllBytes(assembly)).GetType("Examples.Capabilities")! .GetMethod("ReferenceFree")!.Invoke(null, null); if (!Equals(referenceResult, 73)) throw new InvalidOperationException("Unexpected CLR reference-content result"); -File.WriteAllText(referenceQueryProof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + - "theorem correctness : (CIL.Safety.invoke Extracted.program 64 0 [] CIL.Safety.Memory.empty).map Prod.snd = " + - ".ok [.scalar (.i32 73)] := rfl\n#print axioms correctness\nend ExampleProof\n"); +WriteResultProof(referenceQueryProof, 73); capabilitiesQueue.Add(new("Capabilities::ReferenceFree()", referenceQueryProof, Path.Combine(referenceQueryFolder, "Verified")), () => Console.WriteLine("PASS validated reference-free queries and pruned unsupported branch")); capabilitiesQueue.Flush(); var referenceQuerySource = File.ReadAllText(Path.Combine(referenceQueryFolder, "Verified", "Extracted.lean")); var queryControls = new GeneratedEvidence(Path.Combine(output, "ReferenceQueryControls")); -foreach (var obligation in new[] { featureObligation, "method0_scalars_validated" }) +foreach (var obligation in new[] { featureObligation, "method0_models_validated" }) { var narrowed = NarrowEvidence(referenceQuerySource, obligation); var controlPath = Path.Combine(output, obligation + "-reference-query.lean"); File.WriteAllText(controlPath, narrowed); queryControls.Add(controlPath, "PASS exact reference-content control: " + obligation, obligation); - var aggregate = Regex.Match(narrowed, "typeArguments := \\[\\.aggregate \"[^\"]+\" [0-9]+\\]").Value; - if (aggregate.Length == 0) throw new InvalidOperationException("Missing query-only aggregate layout"); - foreach (var (name, before, after) in new[] + var answer = ForgeDecodedInstruction(referenceQuerySource, 0, 0, "fun _ => .ldc_i4 1"); + var answerPath = Path.Combine(output, $"{obligation}-wrong-reference-answer.lean"); + File.WriteAllText(answerPath, NarrowEvidence(answer, obligation)); + rejected.Add(answerPath, $"PASS wrong reference-content answer rejected by {obligation}", + obligation == featureObligation ? "FeatureFlowValid" : "RuntimeModelsAgree"); + foreach (var (name, pc, update) in new[] { - ("answer", ".const32 0", ".const32 1"), - ("scope", ".atom Extraction.runtimeScope \"System.Runtime.CompilerServices.RuntimeHelpers\"", - ".atom \"forged\" \"System.Runtime.CompilerServices.RuntimeHelpers\""), - ("byref", "typeArguments := [.atom Extraction.runtimeScope \"System.Int32\"]", - "typeArguments := [.byref (.atom Extraction.runtimeScope \"System.Int32\")]"), - ("layout", aggregate, Regex.Replace(aggregate, "\"[^\"]+\"", "\"forged\"")), - ("signature", "parameterCount := 0, hasThis := false, returnsValue := true", - "parameterCount := 1, hasThis := false, returnsValue := true") + ("scope", 0, """method.owner := .atom "forged" "System.Runtime.CompilerServices.RuntimeHelpers" """), + ("byref", 0, "typeArguments := site.typeArguments.map Extraction.TypeExpr.byref"), + ("layout", 6, """ + typeArguments := site.typeArguments.map (fun | .aggregate _ i => .aggregate "forged" i | t => t) + """), + ("signature", 0, "parameterCount := 1") }) { - var changed = narrowed.Replace(before, after, StringComparison.Ordinal); - if (changed == narrowed) throw new InvalidOperationException("Missing reference-query mutation: " + name); - var forged = Path.Combine(output, obligation + "-reference-query-" + name + ".lean"); - File.WriteAllText(forged, changed); - rejected.Add(forged, "PASS reference-content forgery rejected: " + name + " " + obligation, - obligation == featureObligation ? "FeatureFlowValid" : "ScalarFlowValid"); + var code = $$""" + modify {{pc}} (fun raw => match raw.operand with + | .call site => { raw with operand := .call { site with {{update}} } } + | _ => raw) + """; + var changed = ForgeClosedCode(referenceQuerySource, 0, code); + var forged = Path.Combine(output, $"{obligation}-reference-query-{name}.lean"); + File.WriteAllText(forged, NarrowEvidence(changed, obligation)); + rejected.Add(forged, $"PASS {name} forgery rejected by {obligation}", + obligation == featureObligation ? "FeatureFlowValid" : "RuntimeModelsAgree"); } } queryControls.Check(replay: false); // A cached type validation belongs to its module and architecture within this request. -using (var module = ModuleDefinition.ReadModule(assembly)) -using (var other = ModuleDefinition.CreateModule("OtherQueryOwner", ModuleKind.Dll)) +using (var pinned = new PinnedMetadata(profileType.Assembly, assembly)) using (var scope = (IDisposable)profileType.Assembly.GetType("Nethermind.LeanCIL.EnumTypes", true)! .GetMethod("BeginScope", BindingFlags.Static | BindingFlags.NonPublic)!.Invoke(null, null)!) { + var module = pinned.Module; var validate = profileType.Assembly.GetType("Nethermind.LeanCIL.ReferenceQueries", true)! .GetMethod("Constant", BindingFlags.Static | BindingFlags.NonPublic)!; var method = module.Types.Single(type => type.FullName == "Examples.Capabilities") @@ -341,12 +362,12 @@ end ExampleProof if (!Equals(validate.Invoke(null, [validQuery, "scalar"]), 0) || !Equals(validate.Invoke(null, [validQuery, "scalar"]), 0)) throw new InvalidOperationException("Reference-content cache control failed"); - var owner = validQuery.DeclaringType; - var definition = new MethodReference(validQuery.Name, validQuery.ReturnType, - new TypeReference(owner.Namespace, owner.Name, other, owner.Scope)) - { CallingConvention = MethodCallingConvention.Generic }; - definition.GenericParameters.Add(new GenericParameter("T", definition)); - var foreignQuery = new GenericInstanceMethod(definition); + using var foreign = new PinnedMetadata(profileType.Assembly, assembly); + var foreignMethod = foreign.Module.GetType("Examples.Capabilities") + .Methods.Single(candidate => candidate.Name == "ReferenceFree"); + var foreignQuery = foreignMethod.Body.Instructions.Select(instruction => instruction.Operand) + .OfType().Single(call => call.GenericArguments[0].Name == "EnumRecord"); + foreignQuery.GenericArguments.Clear(); foreignQuery.GenericArguments.Add(validQuery.GenericArguments[0]); try { @@ -385,7 +406,9 @@ end ExampleProof diagnostic = "Unsupported field type"; break; case "scope": - query.ElementMethod.DeclaringType.Scope = new AssemblyNameReference("Forged", new Version(10, 0)); + var forgedQueryScope = new AssemblyNameReference("Forged", new Version(10, 0)); + module.AssemblyReferences.Add(forgedQueryScope); + query.ElementMethod.DeclaringType.Scope = forgedQueryScope; diagnostic = "Unsupported reference-content query signature"; break; } @@ -425,7 +448,8 @@ end ExampleProof module.Write(mutatedAssembly); var result = Execute(Extract("extract", mutatedAssembly, "Examples", "Capabilities::Platform()", Path.Combine(output, mutation))); - var diagnostic = mutation == "missing-switch-target" ? "Malformed switch targets" : "middle of a tail transfer"; + var diagnostic = mutation == "missing-switch-target" ? "Malformed switch targets" + : "Branch enters the instruction after a prefix"; if (result.Code == 0 || !result.Output.Contains(diagnostic, StringComparison.Ordinal)) throw new InvalidOperationException("Invalid unselected target accepted: " + mutation + "\n" + result.Output); Console.WriteLine("PASS invalid unselected control-flow target rejected: " + mutation); @@ -441,17 +465,16 @@ end ExampleProof var controlPath = Path.Combine(output, obligation + "-platform.lean"); File.WriteAllText(controlPath, narrowed); platformControls.Add(controlPath, "PASS exact platform constant control: " + diagnostic, obligation); - const string literal = ".const32 8"; - var position = narrowed.IndexOf(literal, StringComparison.Ordinal); - if (position < 0) throw new InvalidOperationException("Missing initial platform getter constant"); var forged = Path.Combine(output, obligation + "-wrong-platform.lean"); - File.WriteAllText(forged, narrowed[..position] + ".const32 4" + narrowed[(position + literal.Length)..]); + var changed = ForgeDecodedInstruction(platformSource, 0, 0, "fun _ => .ldc_i4 4"); + File.WriteAllText(forged, NarrowEvidence(changed, obligation)); rejected.Add(forged, "PASS exact platform constant rejection: " + diagnostic, diagnostic); } platformControls.Check(replay: false); // Lean binds the raw endianness load to System.Runtime's BitConverter Boolean and the value 1. var endianSource = File.ReadAllText(Path.Combine(output, "Capabilities-scalar", "Endian", "Extracted.lean")); var endianControls = new GeneratedEvidence(Path.Combine(output, "EndianControls")); +const string endianOwner = ".atom Extraction.runtimeScope \"System.BitConverter\""; foreach (var (obligation, diagnostic) in new[] { (featureObligation, "FeatureFlowValid"), ("method0_scalars_validated", "ScalarFlowValid") @@ -461,21 +484,30 @@ end ExampleProof var controlPath = Path.Combine(output, obligation + "-endian.lean"); File.WriteAllText(controlPath, narrowed); endianControls.Add(controlPath, "PASS exact endianness constant control: " + diagnostic, obligation); - const string literal = ".const32 1"; - var position = narrowed.IndexOf(literal, StringComparison.Ordinal); - if (position < 0) throw new InvalidOperationException("Missing endianness constant"); + var changedValue = ForgeDecodedInstruction(endianSource, 0, 0, "fun _ => .ldc_i4 0"); var forgedValue = Path.Combine(output, obligation + "-wrong-endian.lean"); - File.WriteAllText(forgedValue, narrowed[..position] + ".const32 0" + narrowed[(position + literal.Length)..]); + File.WriteAllText(forgedValue, NarrowEvidence(changedValue, obligation)); rejected.Add(forgedValue, "PASS endianness constant value rejection: " + diagnostic, diagnostic); - const string owner = ".atom Extraction.runtimeScope \"System.BitConverter\""; - if (!narrowed.Contains(owner, StringComparison.Ordinal)) + if (!narrowed.Contains(endianOwner, StringComparison.Ordinal)) throw new InvalidOperationException("Missing endianness owner"); var forgedOwner = Path.Combine(output, obligation + "-forged-endian-owner.lean"); - File.WriteAllText(forgedOwner, - narrowed.Replace(owner, ".atom \"forged\" \"System.BitConverter\"", StringComparison.Ordinal)); + var changedOwner = ForgeClosedCode(endianSource, 0, """ + modify 0 (fun raw => match raw.operand with + | .field _ index type name isStatic => + { raw with operand := .field (.atom "forged" "System.BitConverter") index type name isStatic } + | _ => raw) + """); + File.WriteAllText(forgedOwner, NarrowEvidence(changedOwner, obligation)); rejected.Add(forgedOwner, "PASS endianness owner forgery rejection: " + diagnostic, diagnostic); } endianControls.Check(replay: false); + +var refusedOwner = Path.Combine(output, "forged-endian-decoder.lean"); +var refusedEndian = ForgeDecodedOriginal(endianSource, 0, endianOwner, + ".atom \"forged\" \"System.BitConverter\""); +File.WriteAllText(refusedOwner, NarrowEvidence(refusedEndian, featureObligation)); +rejected.Add(refusedOwner, "PASS forged endianness owner refuses raw decoding", + "cil_decoded_method: the decoder gives no method"); foreach (var mutation in new[] { "scope", "field", "module-type" }) { using var module = ModuleDefinition.ReadModule(assembly); @@ -486,7 +518,11 @@ end ExampleProof var diagnostic = "Only System.Runtime's static readonly BitConverter.IsLittleEndian is a platform constant"; switch (mutation) { - case "scope": field.DeclaringType.Scope = new AssemblyNameReference("Forged", new Version(10, 0)); break; + case "scope": + var forgedScope = new AssemblyNameReference("Forged", new Version(10, 0)); + module.AssemblyReferences.Add(forgedScope); + field.DeclaringType.Scope = forgedScope; + break; case "field": load.Operand = new FieldReference("IsBigEndian", field.FieldType, field.DeclaringType); diagnostic = "Only same-assembly static readonly"; @@ -513,18 +549,17 @@ end ExampleProof "Endianness lookalike accepted or misreported: " + mutation + "\n" + result.Output); Console.WriteLine("PASS endianness lookalike rejected: " + mutation); } -var comparisonEvidence = NarrowEvidence(platformSource, featureObligation); -foreach (var (name, before, after) in new[] +foreach (var (name, pattern, replacement) in new[] { - ("branch", ".bne ", ".beq "), ("signedness", ".gt,", ".gtu,"), - ("unsigned-comparison", ".ltu,", ".lt,") + ("branch", ".bne_un target", ".beq target"), ("signedness", ".cgt", ".cgt_un"), + ("unsigned-comparison", ".clt_un", ".clt") }) { - if (!comparisonEvidence.Contains(before, StringComparison.Ordinal)) - throw new InvalidOperationException("Missing comparison mutation target: " + name); - var forged = Path.Combine(output, "wrong-comparison-" + name + ".lean"); - File.WriteAllText(forged, comparisonEvidence.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(forged, "PASS exact constant comparison rejection: " + name, "FeatureFlowValid"); + var update = $"code := method.code.map (fun | {pattern} => {replacement} | op => op)"; + var changed = ForgeDecodedMethod(platformSource, 0, update); + var forged = Path.Combine(output, $"wrong-comparison-{name}.lean"); + File.WriteAllText(forged, NarrowEvidence(changed, featureObligation)); + rejected.Add(forged, $"PASS exact constant comparison rejection for {name}", "FeatureFlowValid"); } rejected.Check(); var scalarCapabilities = Path.Combine(output, "Capabilities-scalar"); @@ -556,3 +591,10 @@ end ExampleProof throw new InvalidOperationException("Profile without X86Base64 was not rejected by name: " + invalid.Output); Console.WriteLine("PASS x86 ISA flags without X86Base64 rejected with the rule"); Console.WriteLine("PASS raw feature analysis, join/backedge weakening, local eligibility, switches and emitted-code binding"); + +static partial class FeatureLayoutPattern +{ + [GeneratedRegex(@"(?m)^(def method0FeatureStates : Extraction.FeatureCertificate := )([^\r\n]+)")] + internal static partial Regex FeatureStates { get; } + +} diff --git a/Tests/FeatureEvidence.lean b/Tests/FeatureEvidence.lean index f4c22b5b6..59779e6f9 100644 --- a/Tests/FeatureEvidence.lean +++ b/Tests/FeatureEvidence.lean @@ -6,22 +6,44 @@ namespace Tests.FeatureEvidence open CIL CIL.Extraction private def intType : TypeExpr := .atom - "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" "System.Int32" + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" + "System.Int32" private def getter : CallSite := { - target := .external "System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51::System.Boolean System.Runtime.Intrinsics.X86.Sse2::get_IsSupported()" - typeArguments := [], parameterCount := 0, hasThis := false, returnsValue := true } -private def intrinsic (member : String) : CallSite := - { getter with target := .external ("System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51::System.Boolean System.Runtime.Intrinsics." ++ member ++ "()") } + declared := { convention := 0, returnType := .atom runtimeScope "System.Boolean" }, + method := { + owner := .atom intrinsicsScope "System.Runtime.Intrinsics.X86.Sse2", + name := name!"get_IsSupported", hasThis := false, genericArity := 0, + returnType := .atom runtimeScope "System.Boolean", + parameters := [] }, + name := "get_IsSupported", target := .external + s!"{intrinsicsScope}::System.Boolean System.Runtime.Intrinsics.X86.Sse2::get_IsSupported()", + typeArguments := [], + parameterCount := 0, returnsValue := true } +private def intrinsic (owner name : String) : CallSite := + { getter with + method.owner := .atom intrinsicsScope ("System.Runtime.Intrinsics." ++ owner) + method.name := (MetadataName.ofBytes? (name.toUTF8.toList.map UInt8.toNat)).getD ⟨0⟩ + name, target := .external s!"{intrinsicsScope}::System.Boolean System.Runtime.Intrinsics.\ + {owner}::{name}()" } -- Each getter binds to its own flag; Vector128 has no IsSupported. -example : featureGetter (intrinsic "X86.X86Base/X64::get_IsSupported") = some .x86Base64 := by decide -example : featureGetter (intrinsic "Vector128::get_IsHardwareAccelerated") = some .vector128Accelerated := by decide -example : featureGetter (intrinsic "Vector128::get_IsSupported") = none := by decide +example : platformGetterModel (intrinsic "X86.X86Base/X64" "get_IsSupported") = + some (.feature .x86Base64) := by + decide +kernel +example : platformGetterModel (intrinsic "Vector128" "get_IsHardwareAccelerated") = + some (.feature .vector128Accelerated) := by decide +kernel +example : platformGetterModel (intrinsic "Vector128" "get_IsSupported") = none := by decide +kernel -- Every x86 ISA class derives from X86Base, and X86Base.X64 exists only on x64. example : ¬ ({ architecture := .x64, bmi2 := true } : FeatureProfile).Valid := by decide example : ¬ ({ architecture := .x64, bmi2 := true, x86Base64 := true } : FeatureProfile).Valid := by decide example : ¬ ({ architecture := .arm64, x86Base64 := true } : FeatureProfile).Valid := by decide example : ¬ ({ architecture := .arm64, vector128Accelerated := true } : FeatureProfile).Valid := by decide +-- .NET 11 x64 reports SSE2 only with SSSE3, SSE4.1 and SSE4.2; EnableHWIntrinsic=0 turns all off. +/-- An x64 process with SSE2 and without SSSE3, SSE4.1 and SSE4.2, which .NET 11 never starts. -/ +private def belowBaseline : FeatureProfile := { + architecture := .x64, x86Base64 := true, sse2 := true, vector128Accelerated := true } +example : ¬ belowBaseline.Valid := by decide +example : ({ architecture := .x64 } : FeatureProfile).Valid := by decide private def v3 : FeatureProfile := { architecture := .x64, x86Base64 := true, sse2 := true, ssse3 := true, sse41 := true, sse42 := true @@ -43,29 +65,34 @@ private def raw (code : List RawInstruction) (locals : List TypeExpr := []) returnType, parameters := [], locals, initLocals := true, maxStack := 8, genericParameters := [], code } private def i (opcode : Nat) (operand : RawOperand := .none) : RawInstruction := ⟨0, opcode, operand⟩ -private def state (stack : List (Option W32)) (locals : List (Option W32) := []) : Option FeatureState := +private def state (stack : List (Option W32)) (locals : List (Nat × W32) := []) : + Option FeatureState := some ⟨stack, locals⟩ private def branchRaw := raw [i 40 (.call getter), i 45 (.target 4), i 22, i 42, i 23, i 42] private def branchStates : FeatureCertificate := [state [], state [some 0], state [], state [some 0], none, none] -private def branchCode : List Op := [.feature .sse2, .brnonzero 4, .const32 0, .ret, .unsupported "pruned", .unsupported "pruned"] +private def branchCode : List Instruction := + [.call getter.method {}, .brtrue 4, .ldc_i4 0, .ret, .unadmitted 23, .unadmitted 42] private def branchMask := [true, true, true, true, false, false] -example : FeatureFlowValid #[] (fun _ => none) +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) branchRaw .scalar branchStates branchMask branchCode := by decide -- A forged incoming constant cannot justify deleting the actual fallthrough. -example : ¬ FeatureFlowValid #[] (fun _ => none) +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) branchRaw .scalar (branchStates.set 1 (state [some 1])) branchMask branchCode := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) branchRaw .scalar (branchStates.set 2 none) - (branchMask.set 2 false) (branchCode.set 2 (.unsupported "false pruning")) := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) branchRaw .scalar branchStates branchMask - (branchCode.set 0 (.feature .avx2)) := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) branchRaw .scalar branchStates branchMask - (branchCode.set 1 (.brnonzero 5)) := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) branchRaw .scalar + (branchStates.set 2 none) + (branchMask.set 2 false) (branchCode.set 2 (.unadmitted 22)) := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) branchRaw .scalar branchStates + branchMask + (branchCode.set 0 (.call (intrinsic "X86.Avx2" "get_IsSupported").method)) := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) branchRaw .scalar branchStates + branchMask + (branchCode.set 1 (.brtrue 5)) := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) branchRaw .scalar (branchStates.set 0 (state [none])) branchMask branchCode := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) branchRaw .scalar branchStates (branchMask.set 0 false) branchCode := by decide @@ -73,34 +100,74 @@ example : ¬ FeatureFlowValid #[] (fun _ => none) -- so the branch keeps both successors even though the first iteration is false. private def loopRaw := raw [i 22, i 10, i 6, i 22, i 51 (.target 8), i 23, i 10, i 43 (.target 2), i 42] [intType] .void -private def loopStates : FeatureCertificate := [state [] [none], state [some 0] [none], - state [] [none], state [none] [none], state [some 0, none] [none], - state [] [none], state [some 1] [none], - state [] [some 1], state [] [none]] -private def loopCode : List Op := [.const32 0, .setLocal 0, .local 0, .const32 0, .bne 8, - .const32 1, .setLocal 0, .branch 2, .ret] -example : FeatureFlowValid #[] (fun _ => none) loopRaw .scalar loopStates +private def loopStates : FeatureCertificate := [state [] [], state [some 0] [], + state [] [], state [none] [], state [some 0, none] [], + state [] [], state [some 1] [], + state [] [(0, 1)], state [] []] +private def loopCode : List Instruction := + [.ldc_i4 0, .stloc 0, .ldloc 0, .ldc_i4 0, .bne_un 8, .ldc_i4 1, .stloc 0, .br 2, .ret] +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) loopRaw .scalar loopStates (List.replicate 9 true) loopCode := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) - loopRaw .scalar (loopStates.set 2 (state [] [some 0])) +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) + loopRaw .scalar (loopStates.set 2 (state [] [(0, 0)])) (List.replicate 9 true) loopCode := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) loopRaw .scalar (loopStates.set 8 none) +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) loopRaw .scalar + (loopStates.set 8 none) [true, true, true, true, true, true, true, true, false] - (loopCode.set 8 (.unsupported "pruned backedge exit")) := by decide + (loopCode.set 8 (.unadmitted 42)) := by decide -- Taking a local's address anywhere invalidates constant facts for that local. -example : ¬ FeatureFlowValid #[] (fun _ => none) +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) { loopRaw with code := loopRaw.code ++ [i 18 (.local 0)] } .scalar (loopStates ++ [none]) ((List.replicate 9 true) ++ [false]) - (loopCode ++ [.unsupported "unreachable address"]) := by decide + (loopCode ++ [.unadmitted 18]) := by decide + +-- A state no edge reaches is checked for its shape alone: its known facts must name each local +-- once, by increasing index, below the local count, with a word the local's type admits. +private def boolType : TypeExpr := .atom + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" + "System.Boolean" +private def deadRaw := raw [i 22, i 10, i 42, i 6, i 38, i 42] [intType, boolType] .void +private def deadStates (facts : List (Nat × W32)) : FeatureCertificate := + [state [], state [some 0], state [] [(0, 0)], state [] facts, state [some 0], state []] +private def deadCode : List Instruction := [.ldc_i4 0, .stloc 0, .ret, .ldloc 0, .pop, .ret] +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) deadRaw .scalar + (deadStates [(0, 0), (1, 1)]) + (List.replicate 6 true) deadCode := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) deadRaw .scalar + (deadStates [(1, 1), (0, 0)]) + (List.replicate 6 true) deadCode := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) deadRaw .scalar + (deadStates [(0, 0), (0, 0)]) + (List.replicate 6 true) deadCode := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) deadRaw .scalar + (deadStates [(0, 0), (2, 1)]) + (List.replicate 6 true) deadCode := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) deadRaw .scalar + (deadStates [(0, 0), (1, 2)]) + (List.replicate 6 true) deadCode := by decide +-- The entry state has no known fact. +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) deadRaw .scalar + ((deadStates [(0, 0)]).set 0 (state [] [(0, 0)])) (List.replicate 6 true) deadCode := by decide +-- Loading or storing a local the method does not have is rejected. +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) + (raw [i 17 (.local 2), i 38, i 42] [intType] .void) .scalar + [state [], state [none], state []] (List.replicate 3 true) [.ldloc 2, .pop, .ret] := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) + (raw [i 22, i 19 (.local 2), i 42] [intType] .void) .scalar + [state [], state [some 0], state []] (List.replicate 3 true) [.ldc_i4 0, .stloc 2, .ret] := by + decide private def switchRaw := raw [i 2, i 69 (.targets [4, 6]), i 23, i 42, i 24, i 42, i 25, i 42] private def switchStates : FeatureCertificate := [state [], state [none], state [], state [some 1], state [], state [some 2], state [], state [some 3]] -private def switchCode : List Op := [.arg 0, .switch [4, 6], .const32 1, .ret, .const32 2, .ret, .const32 3, .ret] -example : FeatureFlowValid #[] (fun _ => none) switchRaw .scalar switchStates +private def switchCode : List Instruction := + [.ldarg 0, .switch [4, 6], .ldc_i4 1, .ret, .ldc_i4 2, .ret, .ldc_i4 3, .ret] +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) switchRaw .scalar switchStates (List.replicate 8 true) switchCode := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) switchRaw .scalar (switchStates.set 6 none) - [true, true, true, true, true, true, false, true] (switchCode.set 6 (.unsupported "missing switch edge")) := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) switchRaw .scalar + (switchStates.set 6 none) + [true, true, true, true, true, true, false, true] (switchCode.set 6 (.unadmitted 25)) := by + decide -- Bitwise feature expressions preserve exact int32 words, including negative -- constants. Unknown operands remain unknown; ceq never guesses equality. @@ -111,31 +178,28 @@ example : featureTransfer #[] branchRaw .scalar (i 65025) ⟨[some 1, some 1], [ example : featureTransfer #[] branchRaw .scalar (i 65025) ⟨[none, some 1], []⟩ = some ⟨[none], []⟩ := by decide example : featureTransfer #[] branchRaw .scalar (i 95) ⟨[], []⟩ = none := by decide -- Native operand normalization does not change a binary branch destination. -example : featureOpMatches #[] branchRaw (i 48 (.target 2)) (.nativeBinary (.bgt 2)) = true := by decide -example : featureOpMatches #[] branchRaw (i 48 (.target 2)) (.nativeBinary (.bgt 3)) = false := by decide +example : featureOpMatches #[] branchRaw (i 48 (.target 2)) (.bgt 2) = true := by decide +example : featureOpMatches #[] branchRaw (i 48 (.target 2)) (.bgt 3) = false := by decide -- A stack-changing replacement for nop used to forge the value at a following -- branch while the raw-body analysis still justified pruning its taken edge. private def nopRaw := raw [i 22, i 0, i 45 (.target 5), i 22, i 42, i 23, i 42] private def nopStates : FeatureCertificate := [state [], state [some 0], state [some 0], state [], state [some 0], none, none] -private def nopCode : List Op := [.const32 0, .nop, .brnonzero 5, .const32 0, .ret, - .unsupported "pruned", .unsupported "pruned"] -example : FeatureFlowValid #[] (fun _ => none) nopRaw .scalar nopStates +private def nopCode : List Instruction := + [.ldc_i4 0, .nop, .brtrue 5, .ldc_i4 0, .ret, .unadmitted 23, .unadmitted 42] +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) nopRaw .scalar nopStates [true, true, true, true, true, false, false] nopCode := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) nopRaw .scalar nopStates +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) nopRaw .scalar nopStates [true, true, true, true, true, false, false] - (nopCode.set 1 (.const32 1)) := by decide -example : featureOpMatches #[] loopRaw (i 0) (.setLocal 0) = false := by decide -example : featureOpMatches #[] loopRaw (i 38) (.setLocal 0) = false := by decide + (nopCode.set 1 (.ldc_i4 1)) := by decide +example : featureOpMatches #[] loopRaw (i 0) (.stloc 0) = false := by decide +example : featureOpMatches #[] loopRaw (i 38) (.stloc 0) = false := by decide example : featureOpMatches #[] loopRaw (i 0) (.bgt 7) = false := by decide example : featureOpMatches #[] loopRaw (i 0) (.ret) = false := by decide -example : featureOpMatches #[] loopRaw (i 0) (.nativeBinary (.bgt 7)) = false := by decide -example : featureOpMatches #[] loopRaw (i 33 (.integer 0)) (.localAddr 0) = false := by decide -example : featureOpMatches #[] loopRaw (i 33 (.integer 0)) (.vectorLocalAddr 0) = false := by decide -example : featureOpMatches #[] loopRaw (i 33 (.integer 0)) (.vectorArgAddr 0) = false := by decide -example : featureOpMatches #[] loopRaw (i 10) (.setLocal 1) = false := by decide -example : featureOpMatches #[] loopRaw (i 18 (.local 0)) (.localAddr 1) = false := by decide +example : featureOpMatches #[] loopRaw (i 33 (.integer 0)) (.ldloca 0) = false := by decide +example : featureOpMatches #[] loopRaw (i 10) (.stloc 1) = false := by decide +example : featureOpMatches #[] loopRaw (i 18 (.local 0)) (.ldloca 1) = false := by decide example : featureOpMatches #[] loopRaw (i 122) (.pop) = false := by decide example : featureOpMatches #[] loopRaw (i 65050) (.nop) = false := by decide example : featureOpMatches #[] loopRaw (i 220) (.nop) = false := by decide @@ -144,8 +208,33 @@ example : featureOpMatches #[] loopRaw (i 39) (.nop) = false := by decide -- Unknown-result operations still have to preserve the rest of the stack. example : featureOpMatches #[] branchRaw (i 88) (.add) = true := by decide example : featureOpMatches #[] branchRaw (i 88) (.nop) = false := by decide -example : emittedFeatureEffect (.memory (.copyBlock 8)) false = some (3, 0) := by decide -example : emittedFeatureEffect (.newAggregate 1 2 ⟨8, by decide⟩) false = some (2, 1) := by decide +-- A Boolean local that is never addressed is eligible for constant facts; its load and store bind +-- to the plain instructions at its own index, since the slot's kind does the narrowing. +private def booleanRaw := raw [i 6, i 10] [.atom runtimeScope "System.Boolean"] .void +example : featureOpMatches #[] booleanRaw (i 6) (.ldloc 0) = true := by decide +kernel +example : featureOpMatches #[] booleanRaw (i 10) (.stloc 0) = true := by decide +kernel +example : featureOpMatches #[] booleanRaw (i 6) (.ldloc 1) = false := by decide +kernel +example : featureOpMatches #[] booleanRaw (i 10) (.ldloc 0) = false := by decide +kernel +example : featureOpMatches #[] booleanRaw (i 6) (.stloc 0) = false := by decide +kernel +-- Taking the address of a span local or of a span argument binds only the address instruction of +-- the same local or argument, whose step pushes the span's view; a load in its place is rejected. +private def spanRaw := raw [i 18 (.local 0), i 15 (.argument 0)] + [.apply (.constructor runtimeScope "System.Span`1") (.atom runtimeScope "System.UInt32")] .void +example : featureOpMatches #[] spanRaw (i 18 (.local 0)) (.ldloca 0) = true := by decide +kernel +example : featureOpMatches #[] spanRaw (i 18 (.local 0)) (.ldloc 0) = false := by decide +kernel +example : featureOpMatches #[] spanRaw (i 15 (.argument 0)) (.ldarga 0) = true := by + decide +kernel +example : featureOpMatches #[] spanRaw (i 15 (.argument 0)) (.ldarga 1) = false := by + decide +kernel +example : featureOpMatches #[] spanRaw (i 15 (.argument 0)) (.ldloca 1) = false := by + decide +kernel +example : featureOpMatches #[] spanRaw (i 15 (.argument 0)) (.ldarg 0) = false := by decide +kernel +-- An argument store binds only `starg` of the same argument, not a store to its home slot. +example : featureOpMatches #[] spanRaw (i 16 (.argument 0)) (.starg 0) = true := by decide +kernel +example : featureOpMatches #[] spanRaw (i 16 (.argument 0)) (.stloc 1) = false := by decide +kernel +example : featureOpMatches #[] spanRaw (i 16 (.argument 0)) (.starg 1) = false := by decide +kernel +example : emittedFeatureEffect (.cpblk 8) false = some (3, 0) := by decide +example : emittedFeatureEffect (.newobj (.arity 2)) false = some (2, 1) := by decide /-- Indexed facts retain boolean bounds, exact type identity, address exclusions and absence. -/ theorem indexed_local_eligibility : let types := [intType, .atom runtimeScope "System.Boolean", @@ -176,7 +265,7 @@ theorem constant_branch_boundaries : List (Int × Int × List Bool)).all (fun (left, right, expected) => [46, 59].all fun first => (List.range 10).all fun index => featureSuccessors none 2 (i (first + index) (.target 5)) - ⟨[some (BitVec.ofInt 32 right), some (BitVec.ofInt 32 left)], []⟩ == + [some (BitVec.ofInt 32 right), some (BitVec.ofInt 32 left)] == some [if expected[index]?.getD false then 5 else 3]) = true := by decide +kernel #print axioms constant_branch_boundaries @@ -184,7 +273,7 @@ theorem constant_branch_boundaries : theorem unknown_comparison_edges : [46, 59].all (fun first => (List.range 10).all fun index => [([none, some 0] : List (Option W32)), [some 0, none], [none, none]].all fun stack => - featureSuccessors none 2 (i (first + index) (.target 5)) ⟨stack, []⟩ == some [5, 3]) = + featureSuccessors none 2 (i (first + index) (.target 5)) stack == some [5, 3]) = true := by decide +kernel #print axioms unknown_comparison_edges @@ -193,14 +282,14 @@ theorem comparison_certificate : let body := raw [i 21, i 22, i 50 (.target 5), i 22, i 42, i 23, i 42] let facts := [state [], state [some (-1)], state [some 0, some (-1)], none, none, state [], state [some 1]] - let code := [.const32 (-1), .const32 0, .blt 5, .unsupported "pruned", .unsupported "pruned", - .const32 1, .ret] - FeatureFlowValid #[] (fun _ => none) + let code := [.ldc_i4 (-1), .ldc_i4 0, (.blt 5), .unadmitted 22, .unadmitted 42, + .ldc_i4 1, .ret] + FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar facts [true, true, true, false, false, true, true] code ∧ - ¬ FeatureFlowValid #[] (fun _ => none) + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar facts [true, true, true, false, false, true, true] - (code.set 2 (.bltu 5)) ∧ - ¬ FeatureFlowValid #[] (fun _ => none) body .scalar (facts.set 5 none) + (code.set 2 (.blt_un 5)) ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar (facts.set 5 none) [true, true, true, false, false, false, true] code := by decide +kernel #print axioms comparison_certificate @@ -218,19 +307,19 @@ theorem comparison_results : theorem widened_comparison_unknown : featureTransfer #[] (raw []) .scalar (i 211) ⟨[some (-1)], []⟩ = some ⟨[none], []⟩ ∧ featureTransfer #[] (raw []) .scalar (i 224) ⟨[some (-1)], []⟩ = some ⟨[none], []⟩ ∧ - featureSuccessors none 2 (i 51 (.target 5)) ⟨[none, some (-1)], []⟩ = some [5, 3] := + featureSuccessors none 2 (i 51 (.target 5)) [none, some (-1)] = some [5, 3] := by decide +kernel #print axioms widened_comparison_unknown /-- Switch indices are unsigned int32 values; unknowns preserve every encoded successor. -/ theorem constant_switch_successors : - featureSuccessors none 1 (i 69 (.targets [4, 6])) ⟨[some 0], []⟩ = some [4] ∧ - featureSuccessors none 1 (i 69 (.targets [4, 6])) ⟨[some 1], []⟩ = some [6] ∧ - featureSuccessors none 1 (i 69 (.targets [4, 4])) ⟨[some 1], []⟩ = some [4] ∧ - featureSuccessors none 1 (i 69 (.targets [4, 6])) ⟨[some 2], []⟩ = some [2] ∧ - featureSuccessors none 1 (i 69 (.targets [4, 6])) ⟨[some (-1)], []⟩ = some [2] ∧ - featureSuccessors none 1 (i 69 (.targets [])) ⟨[some 0], []⟩ = some [2] ∧ - featureSuccessors none 1 (i 69 (.targets [4, 6])) ⟨[none], []⟩ = some [2, 4, 6] := + featureSuccessors none 1 (i 69 (.targets [4, 6])) [some 0] = some [4] ∧ + featureSuccessors none 1 (i 69 (.targets [4, 6])) [some 1] = some [6] ∧ + featureSuccessors none 1 (i 69 (.targets [4, 4])) [some 1] = some [4] ∧ + featureSuccessors none 1 (i 69 (.targets [4, 6])) [some 2] = some [2] ∧ + featureSuccessors none 1 (i 69 (.targets [4, 6])) [some (-1)] = some [2] ∧ + featureSuccessors none 1 (i 69 (.targets [])) [some 0] = some [2] ∧ + featureSuccessors none 1 (i 69 (.targets [4, 6])) [none] = some [2, 4, 6] := by decide +kernel #print axioms constant_switch_successors @@ -238,17 +327,18 @@ theorem constant_switch_successors : theorem switch_certificate_targets : let body := raw [i 22, i 69 (.targets [4, 6]), i 23, i 42, i 24, i 42, i 25, i 42] let facts := [state [], state [some 0], none, none, state [], state [some 2], none, none] - let code := [.const32 0, .switch [4, 6], .unsupported "pruned", .unsupported "pruned", - .const32 2, .ret, .unsupported "pruned", .unsupported "pruned"] + let code := [.ldc_i4 0, .switch [4, 6], .unadmitted 23, .unadmitted 42, + .ldc_i4 2, .ret, .unadmitted 25, .unadmitted 42] let mask := [true, true, false, false, true, true, false, false] - FeatureFlowValid #[] (fun _ => none) body .scalar facts mask code ∧ - ¬ FeatureFlowValid #[] (fun _ => none) body .scalar facts mask + FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar facts mask code ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar facts mask (code.set 1 (.switch [4, 5])) ∧ - ¬ FeatureFlowValid #[] (fun _ => none) + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) { body with code := body.code.set 1 (i 69 (.targets [4, 100])) } .scalar facts mask (code.set 1 (.switch [4, 100])) ∧ - ¬ FeatureFlowValid #[] (fun _ => none) body .scalar (facts.set 4 none) (mask.set 4 false) - (code.set 4 (.unsupported "false pruning")) := by decide +kernel + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar (facts.set 4 none) + (mask.set 4 false) + (code.set 4 (.unadmitted 24)) := by decide +kernel #print axioms switch_certificate_targets /-- An unselected table arm or untaken comparison cannot enter a tail transfer after its prefix. -/ @@ -256,24 +346,24 @@ theorem pruned_tail_entry_rejected : let tail := [i 65044, i 40 (.call { getter with target := .managed 0 }), i 42] let switchBody := raw ([i 22, i 69 (.targets [2, 5]), i 23, i 42] ++ tail) let switchFacts := [state [], state [some 0], state [], state [some 1], none, none, none] - let switchCode := [.const32 0, .switch [2, 5], .const32 1, .ret, - .unsupported "pruned", .unsupported "pruned", .unsupported "pruned"] + let switchCode := [.ldc_i4 0, .switch [2, 5], .ldc_i4 1, .ret, + .unadmitted 65044, .unadmitted 40, .unadmitted 42] let branchBody := raw ([i 22, i 22, i 51 (.target 6), i 23, i 42] ++ tail) let branchFacts := [state [], state [some 0], state [some 0, some 0], state [], state [some 1], none, none, none] - let branchCode := [.const32 0, .const32 0, .bne 6, .const32 1, .ret, - .unsupported "pruned", .unsupported "pruned", .unsupported "pruned"] - FeatureFlowValid #[] (fun _ => none) + let branchCode := [.ldc_i4 0, .ldc_i4 0, .bne_un 6, .ldc_i4 1, .ret, + .unadmitted 65044, .unadmitted 40, .unadmitted 42] + FeatureFlowValid #[] (fun _ => none) (fun _ => none) { switchBody with code := switchBody.code.set 1 (i 69 (.targets [2, 4])) } .scalar switchFacts [true, true, true, true, false, false, false] (switchCode.set 1 (.switch [2, 4])) ∧ - ¬ FeatureFlowValid #[] (fun _ => none) switchBody .scalar switchFacts + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) switchBody .scalar switchFacts [true, true, true, true, false, false, false] switchCode ∧ - FeatureFlowValid #[] (fun _ => none) + FeatureFlowValid #[] (fun _ => none) (fun _ => none) { branchBody with code := branchBody.code.set 2 (i 51 (.target 5)) } .scalar branchFacts [true, true, true, true, true, false, false, false] - (branchCode.set 2 (.bne 5)) ∧ - ¬ FeatureFlowValid #[] (fun _ => none) branchBody .scalar branchFacts + (branchCode.set 2 (.bne_un 5)) ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) branchBody .scalar branchFacts [true, true, true, true, true, false, false, false] branchCode := by decide +kernel #print axioms pruned_tail_entry_rejected @@ -281,16 +371,18 @@ theorem pruned_tail_entry_rejected : theorem switch_backedge_weakening : let body := raw [i 22, i 10, i 6, i 69 (.targets [4, 7]), i 23, i 10, i 43 (.target 2), i 42] [intType] .void - let facts := [state [] [none], state [some 0] [none], state [] [none], state [none] [none], - state [] [none], state [some 1] [none], state [] [some 1], state [] [none]] - let code := [.const32 0, .setLocal 0, .local 0, .switch [4, 7], .const32 1, .setLocal 0, - .branch 2, .ret] - FeatureFlowValid #[] (fun _ => none) body .scalar facts (List.replicate 8 true) code ∧ - ¬ FeatureFlowValid #[] (fun _ => none) body .scalar (facts.set 2 (state [] [some 0])) + let facts := [state [] [], state [some 0] [], state [] [], state [none] [], + state [] [], state [some 1] [], state [] [(0, 1)], state [] []] + let code := [.ldc_i4 0, .stloc 0, .ldloc 0, .switch [4, 7], .ldc_i4 1, .stloc 0, + .br 2, .ret] + FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar facts (List.replicate 8 true) + code ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar + (facts.set 2 (state [] [(0, 0)])) (List.replicate 8 true) code ∧ - ¬ FeatureFlowValid #[] (fun _ => none) body .scalar (facts.set 7 none) + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar (facts.set 7 none) [true, true, true, true, true, true, true, false] - (code.set 7 (.unsupported "pruned backedge exit")) := by decide +kernel + (code.set 7 (.unadmitted 42)) := by decide +kernel #print axioms switch_backedge_weakening end Tests.FeatureEvidence diff --git a/Tests/FeatureExecution.lean b/Tests/FeatureExecution.lean index 66b68cba3..5dbb2dde8 100644 --- a/Tests/FeatureExecution.lean +++ b/Tests/FeatureExecution.lean @@ -1,29 +1,32 @@ import CIL.Safety.SymbolicExecution +import Tests.Support /-! Feature facts for checked execution through the shared value evaluator. -/ namespace Tests.FeatureExecution /-- A method returning a single profile feature test. -/ -@[cil_code] def featureMethod (profile : CIL.FeatureProfile) (feature : CIL.Feature) : - CIL.Method := { profile, code := [.feature feature, .ret], locals := [], returnsValue := true } +@[cil_code] def featureMethod (profile : CIL.FeatureProfile) (getter : CIL.Extraction.MethodRef) : + CIL.Method := { profile, code := [.call getter, .ret], locals := [], returnsValue := true } /-- A caller fact decides an unknown feature without unfolding its dispatcher. -/ -theorem symbolic_feature (profile : CIL.FeatureProfile) (feature : CIL.Feature) +theorem symbolic_feature (profile : CIL.FeatureProfile) (feature : CIL.Feature) (getter : CIL.Extraction.MethodRef) + (model : CIL.Extraction.runtimeModel {} .call getter = some (.feature feature)) (enabled : profile.evaluate feature = true) (memory : CIL.Safety.Memory) : - CIL.Safety.invoke [featureMethod profile feature] 3 0 [] memory = + CIL.Safety.invoke [featureMethod profile getter] 3 0 [] memory = .ok (memory, [.scalar (.i32 1)]) := by - cil_checked_steps [enabled] + cil_checked_steps [model, enabled] #print axioms symbolic_feature /-- A disabled symbolic feature returns zero without splitting on its constructors. -/ -theorem symbolic_feature_disabled (profile : CIL.FeatureProfile) (feature : CIL.Feature) +theorem symbolic_feature_disabled (profile : CIL.FeatureProfile) (feature : CIL.Feature) (getter : CIL.Extraction.MethodRef) + (model : CIL.Extraction.runtimeModel {} .call getter = some (.feature feature)) (disabled : profile.evaluate feature = false) (memory : CIL.Safety.Memory) : - CIL.Safety.invoke [featureMethod profile feature] 3 0 [] memory = + CIL.Safety.invoke [featureMethod profile getter] 3 0 [] memory = .ok (memory, [.scalar (.i32 0)]) := by - cil_checked_steps [disabled] + cil_checked_steps [model, disabled] #print axioms symbolic_feature_disabled /-- A concrete feature is reduced to the profile field named by the caller. -/ theorem concrete_feature (profile : CIL.FeatureProfile) (enabled : profile.avx2 = true) (memory : CIL.Safety.Memory) : - CIL.Safety.invoke [featureMethod profile .avx2] 3 0 [] memory = + CIL.Safety.invoke [featureMethod profile (Tests.supportedGetter "X86.Avx2")] 3 0 [] memory = .ok (memory, [.scalar (.i32 1)]) := by cil_checked_steps [enabled] #print axioms concrete_feature diff --git a/Tests/FeatureGetter.lean b/Tests/FeatureGetter.lean index 737603ea6..57bf900da 100644 --- a/Tests/FeatureGetter.lean +++ b/Tests/FeatureGetter.lean @@ -1,9 +1,10 @@ import CIL.Extraction.Features +import Tests.Support.ModelOperands /-! # Exact feature getter identities -The byte-table lookup must retain assembly, namespace, class and member identity, +The model operand binds assembly, namespace, class and member identity, including malformed signatures which share the admitted prefix. -/ @@ -13,45 +14,61 @@ open CIL CIL.Extraction /-- A static feature getter with independently constructed raw metadata. -/ def getter (owner member : String) : CallSite := { - declaringType := .atom intrinsicsScope ("System.Runtime.Intrinsics." ++ owner) + declared := { convention := 0, returnType := .atom runtimeScope "System.Boolean" }, + method := { + owner := .atom intrinsicsScope ("System.Runtime.Intrinsics." ++ owner), + name := (MetadataName.ofBytes? (member.toUTF8.toList.map UInt8.toNat)).getD ⟨0⟩, + hasThis := false, genericArity := 0, + returnType := .atom runtimeScope "System.Boolean", parameters := [] } name := member target := .external (intrinsicsScope ++ "::System.Boolean System.Runtime.Intrinsics." ++ owner ++ "::" ++ member ++ "()") - parameterCount := 0, hasThis := false, returnsValue := true, typeArguments := [] - returnType := .atom runtimeScope "System.Boolean", parameterTypes := [] } + parameterCount := 0, returnsValue := true, typeArguments := [] } + +/-- The external identity must name the selected owner even for two admitted getters. -/ +theorem targetOwner : + let site := getter "X86.Avx2" "get_IsSupported" + platformGetterModel site = some (.feature .avx2) ∧ + platformGetterModel { site with target := (getter "X86.Aes" "get_IsSupported").target } = + none := by decide +kernel + +#print axioms targetOwner /-- Hardware acceleration, nested class and ordinary supported getters retain their flags. -/ theorem supported : - featureGetter (getter "Vector256" "get_IsHardwareAccelerated") = some .vector256Accelerated ∧ - featureGetter (getter "X86.Sse41" "get_IsSupported") = some .sse41 ∧ - featureGetter (getter "Arm.ArmBase/Arm64" "get_IsSupported") = some .armBase64 ∧ - featureGetter (getter "Arm.AdvSimd/Arm64" "get_IsSupported") = some .advSimd ∧ - featureGetter (getter "X86.Avx512Vbmi" "get_IsSupported") = some .avx512Vbmi ∧ - featureGetter (getter "X86.Avx512Vbmi/VL" "get_IsSupported") = some .avx512Vbmi ∧ - featureGetter (getter "X86.Aes" "get_IsSupported") = some .aes ∧ - featureGetter (getter "X86.Aes/X64" "get_IsSupported") = some .aes ∧ - featureGetter (getter "Arm.Aes" "get_IsSupported") = some .armAes ∧ - featureGetter (getter "Arm.Aes/Arm64" "get_IsSupported") = some .armAes := by + platformGetterModel (getter "Vector256" "get_IsHardwareAccelerated") = + some (.feature .vector256Accelerated) ∧ + platformGetterModel (getter "X86.Sse41" "get_IsSupported") = some (.feature .sse41) ∧ + platformGetterModel (getter "Arm.ArmBase/Arm64" "get_IsSupported") = + some (.feature .armBase64) ∧ + platformGetterModel (getter "Arm.AdvSimd/Arm64" "get_IsSupported") = some (.feature .advSimd) ∧ + platformGetterModel (getter "X86.Avx512Vbmi" "get_IsSupported") = some (.feature .avx512Vbmi) ∧ + platformGetterModel (getter "X86.Avx512Vbmi/VL" "get_IsSupported") = + some (.feature .avx512Vbmi) ∧ + platformGetterModel (getter "X86.Aes" "get_IsSupported") = some (.feature .aes) ∧ + platformGetterModel (getter "X86.Aes/X64" "get_IsSupported") = some (.feature .aes) ∧ + platformGetterModel (getter "Arm.Aes" "get_IsSupported") = some (.feature .armAes) ∧ + platformGetterModel (getter "Arm.Aes/Arm64" "get_IsSupported") = some (.feature .armAes) := by decide +kernel #print axioms supported /-- A syntactically similar identity cannot change the exact admitted feature class or member. -/ theorem wrong_names : - featureGetter (getter "Vector256" "get_IsSupported") = none ∧ - featureGetter (getter "X86.Sse41X" "get_IsSupported") = none ∧ - featureGetter (getter "X86.Sse41" "get_IsSupportedX") = none ∧ - featureGetter (getter "X86.Ssé41" "get_IsSupported") = none := by decide +kernel + platformGetterModel (getter "Vector256" "get_IsSupported") = none ∧ + platformGetterModel (getter "X86.Sse41X" "get_IsSupported") = none ∧ + platformGetterModel (getter "X86.Sse41" "get_IsSupportedX") = none ∧ + platformGetterModel (getter "X86.Ssé41" "get_IsSupported") = none := by decide +kernel #print axioms wrong_names /-- Nested 64-bit classes outside the measured runtime groups remain unclassified. -/ theorem unclassified_nested : - featureGetter (getter "Arm.Crc32/Arm64" "get_IsSupported") = none ∧ - featureGetter (getter "Arm.Rdm/Arm64" "get_IsSupported") = none ∧ - featureGetter (getter "Arm.AdvSimd/Arm64" "get_IsHardwareAccelerated") = none ∧ - featureGetter (getter "X86.Avx512Vbmi2/VL" "get_IsSupported") = none ∧ - featureGetter (getter "X86.Avx512Vbmi/VL" "get_IsHardwareAccelerated") = none ∧ - featureGetter (getter "X86.Pclmulqdq" "get_IsSupported") = none ∧ - featureGetter (getter "X86.Aes/X86" "get_IsSupported") = none := by + platformGetterModel (getter "Arm.Crc32/Arm64" "get_IsSupported") = none ∧ + platformGetterModel (getter "Arm.Rdm/Arm64" "get_IsSupported") = none ∧ + platformGetterModel (getter "Arm.AdvSimd/Arm64" "get_IsHardwareAccelerated") = none ∧ + platformGetterModel (getter "X86.Avx512Vbmi2/VL" "get_IsSupported") = none ∧ + platformGetterModel (getter "X86.Avx512Vbmi/VL" "get_IsHardwareAccelerated") = none ∧ + platformGetterModel (getter "X86.Pclmulqdq" "get_IsSupported") = none ∧ + platformGetterModel (getter "X86.Aes/X86" "get_IsSupported") = none := by decide +kernel #print axioms unclassified_nested @@ -90,60 +107,92 @@ theorem wrong_prefix_or_tail : "Other::System.Boolean System.Runtime.Intrinsics.X86.Sse41::get_IsSupported()", intrinsicsScope ++ "::System.Boolean System.Runtime.Intrinsics.X86.Sse41::get_IsSupported()X"] (fun identity => - (featureGetter { getter "X86.Sse41" "get_IsSupported" with + (platformGetterModel { getter "X86.Sse41" "get_IsSupported" with target := .external identity }).isNone)) = true := by decide +kernel #print axioms wrong_prefix_or_tail /-- Signature acceleration leaves the existing call-shape checks intact. -/ theorem wrong_call_shape : - featureGetter { getter "X86.Sse41" "get_IsSupported" with parameterCount := 1 } = none ∧ - featureGetter { getter "X86.Sse41" "get_IsSupported" with hasThis := true } = none ∧ - featureGetter { getter "X86.Sse41" "get_IsSupported" with returnsValue := false } = none ∧ - featureGetter { getter "X86.Sse41" "get_IsSupported" with + platformGetterModel { getter "X86.Sse41" "get_IsSupported" with parameterCount := 1 } = none ∧ + platformGetterModel { getter "X86.Sse41" "get_IsSupported" with method.hasThis := true } = + none ∧ + platformGetterModel { getter "X86.Sse41" "get_IsSupported" with returnsValue := false } = none ∧ + platformGetterModel { getter "X86.Sse41" "get_IsSupported" with typeArguments := [.atom runtimeScope "System.Int32"] } = none := by decide +kernel #print axioms wrong_call_shape /-- Raw process-width calls retain their exact owner, result and external identity. -/ def platform (owner member result : String) : CallSite := { - declaringType := .atom runtimeScope owner, name := member + declared := { convention := 0, returnType := .atom runtimeScope result }, + method := { + owner := .atom runtimeScope owner, + name := (MetadataName.ofBytes? (member.toUTF8.toList.map UInt8.toNat)).getD ⟨0⟩, + hasThis := false, genericArity := 0, returnType := .atom runtimeScope result, + parameters := [] } + name := member target := .external (runtimeScope ++ "::" ++ result ++ " " ++ owner ++ "::" ++ member ++ "()") - parameterCount := 0, hasThis := false, returnsValue := true, typeArguments := [] - returnType := .atom runtimeScope result, parameterTypes := [] } + parameterCount := 0, returnsValue := true, typeArguments := [] } + +/-- Each supported hardware owner and each process getter keeps its call only at its exact row. -/ +theorem getter_decoding : + (["Arm.AdvSimd", "Arm.AdvSimd/Arm64", "Arm.ArmBase/Arm64", "X86.Sse2", "X86.Ssse3", + "X86.Sse41", "X86.Sse42", "X86.Avx", "X86.Avx2", "X86.Avx512F", "X86.Avx512F/VL", + "X86.Avx512DQ", "X86.Avx512DQ/VL", "X86.Avx512Vbmi", "X86.Avx512Vbmi/VL", "X86.Aes", + "X86.Aes/X64", "Arm.Aes", "Arm.Aes/Arm64", "X86.Bmi1", "X86.Bmi2/X64", "X86.X86Base/X64"].all + (fun owner => Tests.ModelOperands.checks (getter owner "get_IsSupported"))) = true ∧ + (["Vector128", "Vector256"].all (fun owner => + Tests.ModelOperands.checks (getter owner "get_IsHardwareAccelerated"))) = true ∧ + ([platform "System.IntPtr" "get_Size" "System.Int32", + platform "System.UIntPtr" "get_Size" "System.Int32", + platform "System.Environment" "get_Is64BitProcess" "System.Boolean"].all + Tests.ModelOperands.checks) = true := by + decide +kernel + +#print axioms getter_decoding /-- Both native-size getters and the process query use the admitted 64-bit platform. -/ theorem platform_values : - platformConstant (platform "System.IntPtr" "get_Size" "System.Int32") = some 8 ∧ - platformConstant (platform "System.UIntPtr" "get_Size" "System.Int32") = some 8 ∧ - platformConstant (platform "System.Environment" "get_Is64BitProcess" "System.Boolean") = - some 1 := by decide +kernel + platformGetterModel (platform "System.IntPtr" "get_Size" "System.Int32") = some (.constant 8) ∧ + platformGetterModel (platform "System.UIntPtr" "get_Size" "System.Int32") = some (.constant 8) ∧ + platformGetterModel (platform "System.Environment" "get_Is64BitProcess" "System.Boolean") = + some (.constant 1) := by decide +kernel #print axioms platform_values /-- A platform constant is checked even when its forged instruction has the same stack effect. -/ theorem platform_encoding (raw : RawMethod) : let site := platform "System.IntPtr" "get_Size" "System.Int32" - featureOpMatches #[] raw ⟨0, 40, .call site⟩ (.const32 8) = true ∧ - featureOpMatches #[] raw ⟨0, 40, .call site⟩ (.const32 4) = false ∧ + featureOpMatches #[] raw ⟨0, 40, .call site⟩ (.call site.method {}) = true ∧ + featureOpMatches #[] raw ⟨0, 40, .call site⟩ (.ldc_i4 4) = false ∧ featureOpMatches #[] raw ⟨0, 40, .call { site with - declaringType := .atom "forged" "System.IntPtr" }⟩ (.const32 8) = false ∧ + method.owner := .atom "forged" "System.IntPtr" }⟩ (.ldc_i4 8) = false ∧ featureOpMatches #[] raw ⟨0, 40, .call { site with - target := .external "forged" }⟩ (.const32 8) = false ∧ + target := .external "forged" }⟩ (.ldc_i4 8) = false ∧ featureOpMatches #[] raw ⟨0, 40, .call { site with - returnType := .atom runtimeScope "System.UInt32" }⟩ (.const32 8) = false ∧ - featureOpMatches #[] raw ⟨0, 111, .call site⟩ (.const32 8) = false ∧ - featureOpMatches #[] raw ⟨0, 40, .call { site with hasThis := true }⟩ (.const32 8) = false ∧ - featureOpMatches #[] raw ⟨0, 40, .call { site with parameterCount := 1 }⟩ (.const32 8) = false ∧ + method.returnType := .atom runtimeScope "System.UInt32" }⟩ (.ldc_i4 8) = false ∧ + featureOpMatches #[] raw ⟨0, 111, .call site⟩ (.ldc_i4 8) = false ∧ + featureOpMatches #[] raw ⟨0, 40, .call { site with method.hasThis := true }⟩ (.ldc_i4 8) = + false ∧ + featureOpMatches #[] raw ⟨0, 40, .call { site with parameterCount := 1 }⟩ (.ldc_i4 8) = false ∧ featureOpMatches #[] raw ⟨0, 40, .call { site with - typeArguments := [.atom runtimeScope "System.Int32"] }⟩ (.const32 8) = false := by - exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + typeArguments := [.atom runtimeScope "System.Int32"] }⟩ (.ldc_i4 8) = false := by + unfold featureOpMatches + decide +kernel #print axioms platform_encoding /-- A raw static Boolean load from `owner` in `scope`. -/ def staticBoolean (scope owner : String) : RawInstruction := - ⟨0, 126, .field (.atom scope owner) 0 (.atom runtimeScope "System.Boolean") true⟩ + ⟨0, 126, .field (.atom scope owner) 0 (.atom runtimeScope "System.Boolean") + name!"IsLittleEndian" true⟩ /-- A static `BitConverter` field of another type, which is not the endianness constant. -/ def integerField : RawOperand := - .field (.atom runtimeScope "System.BitConverter") 0 (.atom runtimeScope "System.Int32") true + .field (.atom runtimeScope "System.BitConverter") 0 (.atom runtimeScope "System.Int32") + name!"IsLittleEndian" true + +/-- A static `BitConverter` Boolean with another name, which is not the endianness constant. -/ +def otherBoolean : RawOperand := + .field (.atom runtimeScope "System.BitConverter") 0 (.atom runtimeScope "System.Boolean") + name!"IsBigEndian" true /-- Only the runtime's `BitConverter` Boolean is the little-endian constant, bound exactly. -/ theorem platform_field (raw : RawMethod) : @@ -153,27 +202,32 @@ theorem platform_field (raw : RawMethod) : platformField (staticBoolean runtimeScope "System.BitConverterX") = none ∧ platformField { endian with opcode := 127 } = none ∧ platformField { endian with operand := integerField } = none ∧ - featureOpMatches #[] raw endian (.const32 1) = true ∧ - featureOpMatches #[] raw endian (.const32 0) = false ∧ - featureOpMatches #[] raw (staticBoolean "forged" "System.BitConverter") (.const32 1) = + platformField { endian with operand := otherBoolean } = none ∧ + featureOpMatches #[] raw endian (.ldc_i4 1) = true ∧ + featureOpMatches #[] raw endian (.ldc_i4 0) = false ∧ + featureOpMatches #[] raw (staticBoolean "forged" "System.BitConverter") (.ldc_i4 1) = false := by - exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ #print axioms platform_field /-- A reference-content query retains its independently serialized generic argument. -/ def referenceQuery (type : TypeExpr) : CallSite := { - declaringType := .atom runtimeScope "System.Runtime.CompilerServices.RuntimeHelpers" - name := "IsReferenceOrContainsReferences" + declared := { convention := 0x10, returnType := .atom runtimeScope "System.Boolean" }, + method := { + owner := .atom runtimeScope "System.Runtime.CompilerServices.RuntimeHelpers", + name := name!"IsReferenceOrContainsReferences", hasThis := false, genericArity := 1, + returnType := .atom runtimeScope "System.Boolean", parameters := [], + methodArguments := [type] }, + name := "IsReferenceOrContainsReferences", target := .external (runtimeScope ++ "::System.Boolean System.Runtime.CompilerServices.RuntimeHelpers::" ++ - "IsReferenceOrContainsReferences()") - parameterCount := 0, hasThis := false, returnsValue := true, typeArguments := [type] - returnType := .atom runtimeScope "System.Boolean", parameterTypes := [] } + "IsReferenceOrContainsReferences()"), parameterCount := 0, returnsValue := true, + typeArguments := [type] } /-- Representable references and views are not reference-free generic value storage. -/ theorem reference_content_boundary : let integer := TypeExpr.atom runtimeScope "System.Int32" - let query := fun type => referenceContentConstant #[] (referenceQuery type) + let query := fun type => referenceContentFact #[] (referenceQuery type) query integer = some 0 ∧ query (.atom runtimeScope "System.Boolean") = some 0 ∧ query (.atom runtimeScope "System.Char") = some 0 ∧ @@ -192,4 +246,13 @@ theorem reference_content_boundary : query (.aggregate "missing" 0) = none := by decide +kernel #print axioms reference_content_boundary +/-- A malformed query or an argument with managed references cannot acquire a model. -/ +theorem reference_decoding : + let query := referenceQuery (.atom runtimeScope "System.Int32") + Tests.ModelOperands.checks query = true ∧ + Tests.ModelOperands.decoded (referenceQuery (.array (.atom runtimeScope "System.Int32"))) = + liveHoleAt 0 40 := by decide +kernel + +#print axioms reference_decoding + end Tests.FeatureGetter diff --git a/Tests/FieldRef.lean b/Tests/FieldRef.lean new file mode 100644 index 000000000..b3da60c95 --- /dev/null +++ b/Tests/FieldRef.lean @@ -0,0 +1,57 @@ +import CIL.Extraction.MetadataBinding +import Tests.MetadataWorld + +/-! +# Field references + +Pins `FieldRef`, the typed field operand: a literal elaborates and equality decides on it, a +reference that differs from another in any one of owner, index, type or name is unequal to it, +and `FieldRef.ofOperand?` reads the operand of the metadata world's `T.Program.count` into the +reference with the operand's owner, index, type and name. +-/ + +namespace Tests.FieldRef + +open CIL CIL.Extraction Tests.MetadataWorld + +/-- The owner of the metadata world's fields. -/ +private def program : TypeExpr := .atom ownIdentity "Program" + +/-- `T.Program.count`, the first instance field of `Program`, holding an `int32`. -/ +private def countField : FieldRef := + { owner := program, index := 0, type := int32, name := name!"count" } + +-- A literal elaborates and compares equal to itself spelled again. +theorem decides_a_literal : + countField = + { owner := .atom ownIdentity "Program", index := 0, + type := .atom runtimeScope "System.Int32", name := name!"count" } := by decide +kernel +#print axioms decides_a_literal + +theorem another_owner_is_another_field : + countField ≠ { countField with owner := .atom ownIdentity "Other" } := by decide +kernel +#print axioms another_owner_is_another_field + +theorem another_index_is_another_field : + countField ≠ { countField with index := 1 } := by decide +kernel +#print axioms another_index_is_another_field + +theorem another_type_is_another_field : + countField ≠ { countField with type := uint32 } := by decide +kernel +#print axioms another_type_is_another_field + +theorem another_name_is_another_field : + countField ≠ { countField with name := name!"total" } := by decide +kernel +#print axioms another_name_is_another_field + +/-- The count field's operand gives the count field. -/ +theorem builds_the_count_field : + FieldRef.ofOperand? countOperand = some countField := by decide +kernel +#print axioms builds_the_count_field + +/-- An operand other than a field gives no reference. -/ +theorem refuses_an_operand_that_is_not_a_field : + FieldRef.ofOperand? (.type int32) = none := by decide +kernel +#print axioms refuses_an_operand_that_is_not_a_field + +end Tests.FieldRef diff --git a/Tests/FieldShape.lean b/Tests/FieldShape.lean new file mode 100644 index 000000000..3c873bfef --- /dev/null +++ b/Tests/FieldShape.lean @@ -0,0 +1,630 @@ +import CIL.Extraction.ObjectOperands +import CIL.Extraction.Scalars +import CIL.Safety.ResolutionFacts +import CIL.Safety.ObjectSteps +import CIL.Safety.FuelLemmas +import CIL.Safety.SymbolicExecution + +/-! +# Field and value shapes + +Pins `CIL.LayoutEnvironment.fieldShape`, `valueShape` and the facts `cil_field_shape` and +`cil_value_shape` declare, over a hand-written environment of five structs. A struct field +resolves to the offset and width of its checked layout entry, sign-extended only when it is +declared as a narrow signed integer; a field whose index or held type does not match the +declaration, a vector's element lane included, is unsupported; a class field resolves to the model +slot of its index, a struct array element taking its size from the layout; and a method, defined +or known by a hypothesis on its environment, reads the shared facts through the `cil_code` +simprocs. Stepping `ldflda` of a class +field faults; a struct field steps on each receiver kind as the lowered access of its shape, and +a field operand without a fact steps to the same literal shape a fact states. A nested struct +copied out of memory keeps its uninitialized padding, through a load and a store, as a snapshot. +-/ + +namespace Tests.FieldShape + +open CIL CIL.Extraction + +/-- The test assembly's scope. -/ +private def scope : String := "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + +/-- `T::Inner`, one `int32`; `T::Pair`, an `int16`, a `uint32` and an `Inner`; `T::Ref`, a + byref-like struct; `T::Lanes`, a 32-byte and a 16-byte vector and an `int32`; `T::Outer`, a + `Pair` and an `int32`. -/ +def rawLayouts : List RawLayout := [ + { identity := "T::Inner", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .signed32 }] }, + { identity := "T::Pair", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .signed16 }, { type := .unsigned32 }, { type := .aggregate 0 }] }, + { identity := "T::Ref", explicit := false, packing := 0, declaredSize := 0, byRefLike := true, + fields := [{ type := .signed32 }] }, + { identity := "T::Lanes", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .vector256 .unsigned64 }, { type := .vector128 .unsigned64 }, + { type := .signed32 }] }, + { identity := "T::Outer", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .aggregate 1 }, { type := .signed32 }] }] + +/-- The layouts CoreCLR computes for `rawLayouts`. -/ +def computedLayouts : Array ComputedLayout := #[ + ⟨{ size := 4, fields := [{ offset := 0, width := 4 }] }, 4⟩, + ⟨{ size := 12, fields := [{ offset := 0, width := 2 }, { offset := 4, width := 4 }, + { offset := 8, width := 4, aggregate := true }] }, 4⟩, + ⟨{ size := 4, fields := [{ offset := 0, width := 4 }] }, 4⟩, + ⟨{ size := 64, fields := [{ offset := 0, width := 32 }, { offset := 32, width := 16 }, + { offset := 48, width := 4 }] }, 32⟩, + ⟨{ size := 16, fields := [{ offset := 0, width := 12, aggregate := true }, + { offset := 12, width := 4 }] }, 4⟩] + +theorem layouts_valid : LayoutsValid rawLayouts computedLayouts .x64 := by decide +kernel +#print axioms layouts_valid + +/-- An enum over another enum has neither metadata storage nor a declared field kind. -/ +theorem nestedEnumHasNoFieldKind : + let nested := TypeExpr.enum "E" (.enum "Inner" (.atom runtimeScope "System.Int32")) + And (typeStorage rawLayouts nested = none) (fieldKind? rawLayouts nested = none) := by + decide +kernel +#print axioms nestedEnumHasNoFieldKind + +/-- The environment every method of the test shares. -/ +@[cil_code] def environment : LayoutEnvironment := + { rawLayouts := rawLayouts, layouts := computedLayouts } + +private def int16 : TypeExpr := .atom runtimeScope "System.Int16" +private def uint32 : TypeExpr := .atom runtimeScope "System.UInt32" +private def inner : TypeExpr := .aggregate "T::Inner" 0 +private def pair : TypeExpr := .aggregate "T::Pair" 1 +private def node : TypeExpr := .object scope "Node" + +/-- A raw size site cannot substitute the layout index, the computed size or the literal token. -/ +theorem sizeof_forgeries : + let site := RawInstruction.mk 0 0xfe1c (.type inner) + let forged := TypeExpr.aggregate "T::Inner" 1 + let body : Method := { + environment, code := [.sizeof forged], locals := [], returnsValue := true } + let raw : RawMethod := { + token := 0, owner := "T", hasThis := false, explicitThis := false, callingConvention := 0 + attributes := 0, implementation := 0, returnType := int16, parameters := [], locals := [] + initLocals := false, maxStack := 1, genericParameters := [] + code := [{ site with operand := .type forged }] } + ¬ ObjectOperandsValid raw [true] body ∧ + ¬ LayoutsValid rawLayouts (computedLayouts.set! 0 ⟨⟨12, [⟨0, 4, false⟩]⟩, 4⟩) .x64 ∧ + scalarEmissionMatches none none site [] (.sizeof inner) = true ∧ + scalarEmissionMatches none none site [] (.sizeof pair) = false := by + decide +kernel +#print axioms sizeof_forgeries + +/-- `Pair.low`, the `int16` at offset 0. -/ +def lowField : FieldRef := { owner := pair, index := 0, type := int16, name := name!"low" } +/-- `Pair.high`, the `uint32` at offset 4. -/ +def highField : FieldRef := { owner := pair, index := 1, type := uint32, name := name!"high" } +/-- `Pair.inner`, the nested struct at offset 8. -/ +def innerField : FieldRef := { owner := pair, index := 2, type := inner, name := name!"inner" } + +cil_field_shape environment lowField +cil_field_shape environment highField +cil_field_shape environment innerField + +-- Each fact states the layout entry's offset and width; only the narrow signed field extends. +theorem low_at_its_layout (body : Method) (shared : body.environment = environment) : + body.fieldShape lowField = .value 0 2 true := lowField_shape shared +#print axioms low_at_its_layout + +theorem high_at_its_layout (body : Method) (shared : body.environment = environment) : + body.fieldShape highField = .value 4 4 false := highField_shape shared +#print axioms high_at_its_layout + +theorem inner_at_its_layout (body : Method) (shared : body.environment = environment) : + body.fieldShape innerField = .aggregate 8 4 := innerField_shape shared +#print axioms inner_at_its_layout + +/-- The test's one method, which shares the environment. -/ +def body : Method := { code := [], locals := [], returnsValue := false, environment } + +-- A method reads a shared fact through `cil_code`, which unfolds the method's environment. +theorem body_reads_high : body.fieldShape highField = .value 4 4 false := by + simp only [cil_code] +#print axioms body_reads_high + +-- A method known only by a hypothesis on its environment reads the fact through that hypothesis. +theorem hypothesis_reads_low (method : Method) (shared : method.environment = environment) : + method.fieldShape lowField = .value 0 2 true := by + simp only [cil_code] +#print axioms hypothesis_reads_low + +/-- A reducible environment hypothesis supplies the shared shape of an open method. -/ +theorem reducible_environment_reads_low (method : Method) + (shared : id method.environment = environment) : + method.fieldShape lowField = .value 0 2 true := by + simp only [cil_code] +#print axioms reducible_environment_reads_low + +/-- A supplied step on a decoded field applies while the goal still names that field. -/ +theorem supplied_decoded_field_step (frame : Safety.Frame) (memory : Safety.Memory) + (result : Safety.ExecutionResult Safety.FrameAction) + (loaded : Safety.step body (.ldfld highField) 0 [] frame [] memory = result) : + Safety.step body (.ldfld highField) 0 [] frame [] memory = result := by + cil_checked_step [loaded] +#print axioms supplied_decoded_field_step + +-- An index whose declared type is not the held type, and an index past the fields, are refused. +theorem refuses_a_mismatched_index : + environment.fieldShape { highField with index := 0 } = + .unsupported "Field outside the checked layouts" := by decide +kernel +#print axioms refuses_a_mismatched_index + +theorem refuses_an_index_past_the_fields : + environment.fieldShape { highField with index := 3 } = + .unsupported "Field outside the checked layouts" := by decide +kernel +#print axioms refuses_an_index_past_the_fields + +-- A held type the declaration does not store, at the declared index, is refused. +theorem refuses_another_held_type : + environment.fieldShape { lowField with type := uint32 } = + .unsupported "Field outside the checked layouts" := by decide +kernel +#print axioms refuses_another_held_type + +-- An owner naming one struct with another's layout index resolves to neither. +theorem refuses_an_identity_at_another_layout : + environment.fieldShape { highField with owner := .aggregate "T::Pair" 0 } = + .unsupported "Field owner is not a value type" := by decide +kernel +#print axioms refuses_an_identity_at_another_layout + +theorem refuses_an_owner_that_is_not_a_value_type : + environment.fieldShape { highField with owner := uint32 } = + .unsupported "Field owner is not a value type" := by decide +kernel +#print axioms refuses_an_owner_that_is_not_a_value_type + +-- A class field sits in the model slot of its index, whatever the struct layouts. +theorem class_word_slot : + environment.fieldShape { owner := node, index := 2, type := int16, name := name!"count" } = + .classSlot 16 (.word 2 true) := by decide +kernel +#print axioms class_word_slot + +theorem class_reference_slot : + environment.fieldShape { owner := node, index := 1, type := node, name := name!"next" } = + .classSlot 8 .reference := by decide +kernel +#print axioms class_reference_slot + +-- A struct array element is sized by its layout; a byref-like element has no slot. +theorem class_struct_array_slot : + environment.fieldShape + { owner := node, index := 0, type := .array inner, name := name!"items" } = + .classSlot 0 (.array (.aggregate "T::Inner" 4)) := by decide +kernel +#print axioms class_struct_array_slot + +/-- `Node.lanes`, an array of `Vector128`, whose element name the resolver renders. -/ +def lanesField : FieldRef := + { owner := node, index := 1, name := name!"lanes", + type := .array (.apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") + (.atom runtimeScope "System.UInt64")) } + +cil_field_shape environment lanesField + +-- A vector array slot names its element as the evaluation stack records it. +theorem lanes_vector_slot (method : Method) (shared : method.environment = environment) : + method.fieldShape lanesField = + .classSlot 8 (.array (.vector "System.Runtime.Intrinsics.Vector128`1" 16)) := + lanesField_shape shared +#print axioms lanes_vector_slot + +theorem refuses_a_byref_like_array_slot : + environment.fieldShape + { owner := node, index := 0, type := .array (.aggregate "T::Ref" 2), + name := name!"items" } = + .unsupported "Class instance field type" := by decide +kernel +#print axioms refuses_a_byref_like_array_slot + +theorem refuses_a_double_slot : + environment.fieldShape + { owner := node, index := 3, type := .atom runtimeScope "System.Double", + name := name!"ratio" } = + .unsupported "Class instance field type" := by decide +kernel +#print axioms refuses_a_double_slot + +-- A class slot's offset is the model offset of the field's index. +theorem class_offset_from_shape (field : FieldRef) (offset : Nat) (slot : ClassSlot) + (shape : environment.fieldShape field = .classSlot offset slot) : + offset = 8 * field.index := (LayoutEnvironment.fieldShape_class_offset shape).1 +#print axioms class_offset_from_shape + +/-- `T::Pair` as a type operand. -/ +def pairType : TypeExpr := pair +/-- `System.Int16` as a type operand. -/ +def shortType : TypeExpr := int16 + +cil_value_shape environment pairType +cil_value_shape environment shortType + +open CIL.Safety in +/-- A named type operand lets a symbolic initialization step use its checked width. -/ +theorem init_named_pair (body : Method) (shared : body.environment = environment) + (pc : Nat) (args rest : List Safety.Value) (frame : Frame) (memory : Safety.Memory) + (reference : ManagedReference) : + Safety.step body (.initobj pairType) pc args frame (.reference reference :: rest) memory = (do + let address ← referenceAt reference + let stored ← checkedAt reference 12 + (write memory address (List.replicate 12 0) ordinaryAccessAlignment) + return .next (pc + 1) rest frame stored) := by + simp only [cil_instruction_step] +#print axioms init_named_pair + +theorem pair_value_size (body : Method) (shared : body.environment = environment) : + body.valueShape pairType = .aggregate 12 := pairType_shape shared +#print axioms pair_value_size + +theorem short_value_width (body : Method) (shared : body.environment = environment) : + body.valueShape shortType = .primitive 2 true := shortType_shape shared +#print axioms short_value_width + +-- Integer vectors and spans resolve before layouts. +theorem value_kinds : + environment.valueShape (.apply (.constructor intrinsicsScope + "System.Runtime.Intrinsics.Vector128`1") (.atom runtimeScope "System.Int32")) = + .vector 16 ∧ + environment.valueShape (.apply (.constructor runtimeScope "System.Span`1") int16) = .span := + by decide +kernel +#print axioms value_kinds + +/-- The storage size of a 128-bit floating-point vector is available to runtime models. -/ +theorem double_vector_shape : + environment.valueShape (.apply (.constructor intrinsicsScope + "System.Runtime.Intrinsics.Vector128`1") (.atom runtimeScope "System.Double")) = + .vector 16 := by decide +kernel +#print axioms double_vector_shape + +-- The kernel checks each generated fact, which rests on no axiom beyond `propext`. +/-- info: 'Tests.FieldShape.lowField_shape' depends on axioms: [propext] -/ +#guard_msgs in #print axioms lowField_shape + +/-- info: 'Tests.FieldShape.pairType_shape' depends on axioms: [propext] -/ +#guard_msgs in #print axioms pairType_shape + +/-- `Pair.high` under another name, for a fact that misstates its shape. -/ +def misstatedField : FieldRef := + { owner := pair, index := 1, type := uint32, name := name!"misstated" } + +-- The kernel refuses a fact whose shape differs from the resolver's, so no fact rests on the +-- compiled evaluation that proposes its shape. The attempt leaves no declaration behind. +open Lean Meta Elab in +set_option Elab.async false in +run_cmd Command.liftTermElabM do + let misstate : Expr → MetaM Expr := fun _ => pure (toExpr (FieldShape.value 4 2 true)) + let refused ← withoutModifyingEnv do + try + Safety.ResolutionFacts.declareShapeFact .field ``environment ``misstatedField misstate + pure false + catch error => pure (((← error.toMessageData.toString).splitOn "(kernel)").length > 1) + unless refused do throwError "the kernel did not refuse a misstated shape fact" + if (← getEnv).contains `Tests.FieldShape.misstatedField_shape then + throwError "a refused shape fact stayed in the environment" + +/-- `Node.count`, an `int32` class field at raw index 0. -/ +def countField : FieldRef := + { owner := node, index := 0, type := .atom runtimeScope "System.Int32", name := name!"count" } + +-- `ldflda` of a class field faults as unsupported, since the model forms no address into an +-- object; the fault is not definite, so no refutation lemma takes it as evidence about the method. +theorem class_field_address_faults (frame : Safety.Frame) (memory : Safety.Memory) + (receiver : Safety.ManagedReference) : + Safety.step body (.ldflda countField) 0 [] frame [.object receiver] memory = + .error (.unsupported "Class field address") ∧ + (Safety.ExecutionFault.unsupported "Class field address").definite = false := by + refine ⟨?_, rfl⟩ + simp only [cil_step] +#print axioms class_field_address_faults + +-- A struct field of an object receiver is not a memory access, so the load faults. +theorem value_field_of_an_object_faults (frame : Safety.Frame) (memory : Safety.Memory) + (receiver : Safety.ManagedReference) : + Safety.step body (.ldfld highField) 0 [] frame [.object receiver] memory = + .error (.unsupported "Not a memory instruction") := by + simp only [cil_step, cil_code] + rfl +#print axioms value_field_of_an_object_faults + +/-- Loading the high word uses the displacement, width and sign of the checked field. -/ +theorem high_load_uses_checked_offset (frame : Safety.Frame) (memory : Safety.Memory) + (receiver : Safety.Value) : + Safety.step body (.ldfld highField) 0 [] frame [receiver] memory = + (do let (after, values) ← Safety.loadValueField 4 4 false [receiver] memory + return .next 1 values frame after) := by + simp only [cil_step] +#print axioms high_load_uses_checked_offset + +theorem inner_store_uses_checked_offset (frame : Safety.Frame) (memory : Safety.Memory) + (stack : List Safety.Value) : + Safety.step body (.stfld innerField) 0 [] frame stack memory = + (do let (after, values) ← Safety.storeAggregateField 8 4 stack memory + return .next 1 values frame after) := by + simp only [cil_step] +#print axioms inner_store_uses_checked_offset + +/-- A readonly receiver rejects the write before a malformed field value can reach memory. -/ +theorem inner_store_readonly (frame : Safety.Frame) (memory : Safety.Memory) : + Safety.step body (.stfld innerField) 0 [] frame + [.scalar (.aggregate []), .readOnlyReference .null] memory = + .error (.memory .unwritable .null 4) := by + rw [inner_store_uses_checked_offset] + rfl +#print axioms inner_store_readonly + +/-- On a writable receiver the value width is checked before dereferencing its address. -/ +theorem inner_store_wrong_width (frame : Safety.Frame) (memory : Safety.Memory) : + Safety.step body (.stfld innerField) 0 [] frame + [.scalar (.snapshot (.aggregate 4) []), .reference .null] memory = .error .invalidState := by + rw [inner_store_uses_checked_offset] + rfl +#print axioms inner_store_wrong_width + +theorem inner_load_uses_checked_offset (frame : Safety.Frame) (memory : Safety.Memory) + (stack : List Safety.Value) : + Safety.step body (.ldfld innerField) 0 [] frame stack memory = + (do let (after, values) ← Safety.loadAggregateField 8 4 stack memory + return .next 1 values frame after) := by + simp only [cil_step] +#print axioms inner_load_uses_checked_offset + +theorem inner_address_uses_offset (frame : Safety.Frame) (memory : Safety.Memory) + (stack : List Safety.Value) : + Safety.step body (.ldflda innerField) 0 [] frame stack memory = + (do let (after, values) ← Safety.structFieldAddress 8 stack memory + return .next 1 values frame after) := by + simp only [cil_step] +#print axioms inner_address_uses_offset + +theorem high_address_uses_offset (frame : Safety.Frame) (memory : Safety.Memory) + (stack : List Safety.Value) : + Safety.step body (.ldflda highField) 0 [] frame stack memory = + (do let (after, values) ← Safety.structFieldAddress 4 stack memory + return .next 1 values frame after) := by + simp only [cil_step] +#print axioms high_address_uses_offset + +section +open CIL.Safety + +/-- A writable receiver yields a writable field reference. -/ +theorem address_writable (frame : Frame) (memory : Safety.Memory) (source field : Reference) + (rest : List Safety.Value) (added : add memory source 1 4 = .ok field) : + step body (.ldflda highField) 0 [] frame (.reference (.address source) :: rest) memory = + .ok (.next 1 (.reference (.address field) :: rest) frame memory) := by + cil_checked_step [added] +#print axioms address_writable + +/-- A readonly receiver retains its write restriction. -/ +theorem address_readonly (frame : Frame) (memory : Safety.Memory) (source field : Reference) + (rest : List Safety.Value) (added : add memory source 1 4 = .ok field) : + step body (.ldflda highField) 0 [] frame (.readOnlyReference (.address source) :: rest) memory = + .ok (.next 1 (.readOnlyReference (.address field) :: rest) frame memory) := by + cil_checked_step [added] +#print axioms address_readonly +end + +private def lanes : TypeExpr := .aggregate "T::Lanes" 3 +/-- A `Vector*` of the given generic definition. -/ +private def ulongVector (definition : String) : TypeExpr := + .apply (.constructor intrinsicsScope definition) (.atom runtimeScope "System.UInt64") + +/-- `Lanes.wide`, the 32-byte vector at offset 0. -/ +def wideField : FieldRef := + { owner := lanes, index := 0, type := ulongVector "System.Runtime.Intrinsics.Vector256`1", + name := name!"wide" } +/-- `Lanes.narrow`, the 16-byte vector at offset 32. -/ +def narrowField : FieldRef := + { owner := lanes, index := 1, type := ulongVector "System.Runtime.Intrinsics.Vector128`1", + name := name!"narrow" } +/-- `Lanes.tag`, the `int32` at offset 48. -/ +def tagField : FieldRef := + { owner := lanes, index := 2, type := .atom runtimeScope "System.Int32", name := name!"tag" } + +cil_field_shape environment wideField +cil_field_shape environment narrowField +cil_field_shape environment tagField + +/-- The byte at `offset` of a test value: its offset, initialized unless it is in `unset`. -/ +def testByte (unset : List Nat) (offset : Nat) : BitVec 8 × Bool := + (BitVec.ofNat 8 offset, !unset.contains offset) + +/-- A `T::Lanes` value held as a byte snapshot, as a vector bitcast leaves it: each byte holds + its offset, and byte 33 of `narrow` is uninitialized. -/ +def lanesSnapshot : Safety.Value := + .scalar (.snapshot (.aggregate 64) ((List.range 64).map (testByte [33]))) + +-- A snapshot receiver yields each field from its own bytes: an initialized vector or word as its +-- number, a vector with an uninitialized byte as a snapshot of the field's bytes and flags. +theorem snapshot_fields_load (frame : Safety.Frame) (memory : Safety.Memory) : + Safety.step body (.ldfld wideField) 0 [] frame [lanesSnapshot] memory = + .ok (.next (0 + 1) + [.scalar (.v256 (BitVec.ofNat 256 + (Safety.byteNumber ((List.range 32).map (BitVec.ofNat 8)))))] frame memory) ∧ + Safety.step body (.ldfld narrowField) 0 [] frame [lanesSnapshot] memory = + .ok (.next (0 + 1) + [.scalar (.snapshot .vector128 ((List.range' 32 16).map (testByte [33])))] frame memory) ∧ + Safety.step body (.ldfld tagField) 0 [] frame [lanesSnapshot] memory = + .ok (.next (0 + 1) [.scalar (.i32 (BitVec.ofNat 32 (Safety.byteNumber + ((List.range' 48 4).map (BitVec.ofNat 8)))))] frame memory) := by + simp only [cil_step] + exact ⟨rfl, rfl, rfl⟩ +#print axioms snapshot_fields_load + +-- A word field with an uninitialized byte is not read, and the fault is not definite. +theorem snapshot_uninitialized_word_unsupported (frame : Safety.Frame) (memory : Safety.Memory) : + Safety.step body (.ldfld tagField) 0 [] frame + [.scalar (.snapshot (.aggregate 64) ((List.range 64).map (testByte [50])))] memory = + .error (.unsupported "Uninitialized snapshot field") ∧ + (Safety.ExecutionFault.unsupported "Uninitialized snapshot field").definite = false := by + refine ⟨?_, rfl⟩ + simp only [cil_step] + rfl +#print axioms snapshot_uninitialized_word_unsupported + +-- A store through a readonly reference faults before any address is formed. +theorem readonly_store_unwritable (frame : Safety.Frame) (memory : Safety.Memory) + (receiver : Safety.ManagedReference) : + Safety.step body (.stfld highField) 0 [] frame + [.scalar (.i32 7), .readOnlyReference receiver] memory = + .error (.memory .unwritable receiver 4) := by + simp only [cil_step] + rfl +#print axioms readonly_store_unwritable + +-- A struct value on the stack has no address, so `ldflda` of it faults. +theorem value_address_faults (frame : Safety.Frame) (memory : Safety.Memory) + (bytes : List (BitVec 8)) : + Safety.step body (.ldflda highField) 0 [] frame [.scalar (.aggregate bytes)] memory = + .error (.unsupported "Not a memory instruction") := by + simp only [cil_step] + rfl +#print axioms value_address_faults + +/-- `T::Outer`, a `Pair` followed by an `int32`. -/ +private def outer : TypeExpr := .aggregate "T::Outer" 4 + +/-- `Outer.pair`, the nested `Pair` at offset 0, whose bytes 2 and 3 are padding. -/ +def outerPairField : FieldRef := + { owner := outer, index := 0, type := pair, name := name!"pair" } + +cil_field_shape environment outerPairField + +/-- Two `T::Outer` values in one caller allocation, a source at offset 0 and a destination at + 16; every byte holds its offset, and the padding of the source's `Pair` is uninitialized. -/ +def paddedMemory : Safety.Memory := { + allocations := fun identity => if identity == 0 then + some ⟨.callerStack, ⟨32, 1, []⟩, true, []⟩ else none + cells := fun _ offset => ⟨(testByte [2, 3] offset).1, (testByte [2, 3] offset).2⟩ + views := [⟨0, 0, 32, true, true⟩] + nextIdentity := 1 } + +/-- The source's `Pair` as a copy holds it: its twelve bytes with their flags. -/ +def paddedPair : List (BitVec 8 × Bool) := (List.range 12).map (testByte [2, 3]) + +/-- Whether the destination's `Pair` reads back as the source's bytes and flags. -/ +def copiedPair (memory : Safety.Memory) : Bool := + match Safety.readSnapshot memory ⟨0, 16⟩ 12 1 with + | .ok bytes => bytes == paddedPair + | .error _ => false + +/-- A frame with no locals, for running steps on concrete memory. -/ +def emptyFrame : Safety.Frame := ⟨0, [], [], 0⟩ + +-- Loading the padded `Pair` through a reference pushes it as a snapshot with its flags, so +-- `ldfld` followed by `pop` succeeds instead of faulting on the padding. +theorem padded_load_keeps_padding : + (match Safety.step body (.ldfld outerPairField) 0 [] emptyFrame + [.reference (.address ⟨0, 0⟩)] paddedMemory with + | .ok (.next 1 [.scalar (.snapshot (.aggregate 12) copied)] _ loaded) => + copied == paddedPair && match Safety.step body .pop 1 [] emptyFrame + [.scalar (.snapshot (.aggregate 12) copied)] loaded with + | .ok (.next 2 [] _ _) => true + | _ => false + | _ => false) = true := by decide +kernel +#print axioms padded_load_keeps_padding + +-- A struct copy `destination.pair = source.pair` compiles to `ldfld` then `stfld`, or to +-- `ldflda`, `ldfld` and `stobj`; either way the destination gets the source's flags. +theorem padded_copy_keeps_padding : + (match Safety.step body (.ldfld outerPairField) 0 [] emptyFrame + [.reference (.address ⟨0, 0⟩), .reference (.address ⟨0, 16⟩)] paddedMemory with + | .ok (.next _ stack _ loaded) => + match Safety.step body (.stfld outerPairField) 1 [] emptyFrame stack loaded with + | .ok (.next _ [] _ stored) => copiedPair stored + | _ => false + | _ => false) = true ∧ + (match Safety.step body (.ldflda outerPairField) 0 [] emptyFrame + [.reference (.address ⟨0, 16⟩)] paddedMemory with + | .ok (.next _ [field] _ _) => + match Safety.step body (.ldfld outerPairField) 1 [] emptyFrame + [.reference (.address ⟨0, 0⟩), field] paddedMemory with + | .ok (.next _ stack _ loaded) => + match Safety.step body (.stobj pair) 2 [] emptyFrame stack loaded with + | .ok (.next _ [] _ stored) => copiedPair stored + | _ => false + | _ => false + | _ => false) = true := by decide +kernel +#print axioms padded_copy_keeps_padding + +/-- A padded field load checks lifetime and bounds. Its byte read faults on bounds even when + the first byte is uninitialized. -/ +theorem padded_load_faults_first : + let memory := { paddedMemory with + cells := fun _ offset => + let (bits, initialized) := testByte [2, 3, 24] offset + Safety.Cell.mk bits initialized } + let start := Safety.Reference.mk 0 24 + let bytes := Safety.read memory start 12 Safety.ordinaryAccessAlignment + let load (address : Safety.Reference) (source : Safety.Memory) := + (Safety.step body (.ldfld outerPairField) 0 [] emptyFrame + [.reference (.address address)] source).map (fun _ => ()) + let expired := { paddedMemory with + allocations := fun _ => some (.mk .callerStack (.mk 32 1 []) false []) } + And (bytes = .error .outsideAllocation) + (And (load start memory = .error (.memory .outsideAllocation (.address start) 12)) + (load (.mk 0 0) expired = .error (.memory .expiredLifetime (.address (.mk 0 0)) 0))) := + And.intro rfl (.intro rfl rfl) +#print axioms padded_load_faults_first + +/-- `Node.lanes` written inline under another name, which no shape fact states. -/ +def inlineLanes : FieldRef := + { owner := node, index := 1, name := name!"inline", + type := .array (.apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") + (.atom runtimeScope "System.UInt64")) } + +-- A closed body steps a field without a fact by evaluating its shape to the literal a fact would +-- state, so the stepped goal spells the vector element as a string literal. +theorem inline_lanes_load (frame : Safety.Frame) (memory : Safety.Memory) + (receiver : Safety.ManagedReference) : + ∃ outcome, Safety.step body (.ldfld inlineLanes) 0 [] frame [.object receiver] memory = + outcome := by + simp only [cil_step] + guard_target =ₛ ∃ outcome, (do + let address ← Safety.objectFieldAddress memory receiver node 8 + let value ← Safety.loadArraySlot memory address + (.vector "System.Runtime.Intrinsics.Vector128`1" 16) + pure (Safety.FrameAction.next (0 + 1) [value] frame memory)) = outcome + exact ⟨_, rfl⟩ +#print axioms inline_lanes_load + +-- With shape facts required, the operand that has none is refused instead of evaluated. +/-- error: field inlineLanes of a closed body has no shape fact -/ +#guard_msgs in +set_option cil.requireFieldShapeFacts true in +example (frame : Safety.Frame) (memory : Safety.Memory) (receiver : Safety.ManagedReference) : + ∃ outcome, Safety.step body (.ldfld inlineLanes) 0 [] frame [.object receiver] memory = + outcome := by + simp only [cil_step] + exact ⟨_, rfl⟩ + +-- The operand with a fact still steps by it. +set_option cil.requireFieldShapeFacts true in +theorem fact_lanes_load (frame : Safety.Frame) (memory : Safety.Memory) + (receiver : Safety.ManagedReference) : + ∃ outcome, Safety.step body (.ldfld lanesField) 0 [] frame [.object receiver] memory = + outcome := by + simp only [cil_step] + exact ⟨_, rfl⟩ +#print axioms fact_lanes_load + +/-- `Lanes.narrow` read as a `Vector128`, whose element no vector field declares. -/ +def doubleNarrow : FieldRef := + { narrowField with type := .apply (.constructor intrinsicsScope + "System.Runtime.Intrinsics.Vector128`1") (.atom runtimeScope "System.Double") } +/-- `Lanes.narrow` read as a `Vector128`, the width it declares with another lane. -/ +def longNarrow : FieldRef := + { narrowField with type := .apply (.constructor intrinsicsScope + "System.Runtime.Intrinsics.Vector128`1") (.atom runtimeScope "System.Int64") } + +-- A vector field is held only at its declared lane, so another element type is refused. +theorem refuses_another_lane : + environment.fieldShape doubleNarrow = .unsupported "Field outside the checked layouts" ∧ + environment.fieldShape longNarrow = .unsupported "Field outside the checked layouts" := by + decide +kernel +#print axioms refuses_another_lane + +theorem another_lane_load_unsupported (frame : Safety.Frame) (memory : Safety.Memory) : + Safety.step body (.ldfld doubleNarrow) 0 [] frame [lanesSnapshot] memory = + .error (.unsupported "Field outside the checked layouts") := by + simp only [cil_step] + rfl +#print axioms another_lane_load_unsupported + +end Tests.FieldShape diff --git a/Tests/FileHashing.cs b/Tests/FileHashing.cs index 1d4f7875f..8da750550 100644 --- a/Tests/FileHashing.cs +++ b/Tests/FileHashing.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Security.Cryptography; using System.Text.Json; diff --git a/Tests/FlowIndex.lean b/Tests/FlowIndex.lean index a95cc32ee..96515fa8c 100644 --- a/Tests/FlowIndex.lean +++ b/Tests/FlowIndex.lean @@ -1,4 +1,5 @@ import CIL.Extraction.FlowIndex +import CIL.Extraction.KernelEquation /-! # Consuming certificate index regressions @@ -38,4 +39,33 @@ theorem partitions : decide +kernel #print axioms partitions +/-- A named cell, which `program_index%` writes into its leaf as written. -/ +def cell : Nat := 7 + +open Lean Elab Term Command in +run_cmd liftTermElabM do + let tree ← elabTerm (← `(program_index% [cell])) none + let_expr FlowIndex.leaf _ value := tree | throwError "Expected a singleton leaf" + unless value.isConstOf ``cell do throwError "Expected the named cell, found {value}" + +/-- The kernel rejects index agreement with a duplicated or missing leaf, and a false length. -/ +example : True := by + fail_if_success + have : (program_index% [10, 20, 20]) = FlowIndex.ofList [10, 20, 30] := by cil_kernel_rfl + fail_if_success + have : (program_index% [10, 20]) = FlowIndex.ofList [10, 20, 30] := by cil_kernel_rfl + fail_if_success have : 2 = [10, 20, 30].length := by cil_kernel_rfl + trivial + +/-- `program_index%` writes the index `ofList` builds, split at the same middles, for lists with + none, one, an odd and an even number of cells. -/ +theorem literal_indices : + (program_index% ([] : List Nat)) = FlowIndex.ofList [] ∧ + (program_index% [cell]) = FlowIndex.ofList [cell] ∧ + (program_index% [10, 20, 30]) = .branch 1 (.leaf 10) (.branch 1 (.leaf 20) (.leaf 30)) ∧ + (program_index% [10, 20, 30]) = FlowIndex.ofList [10, 20, 30] ∧ + (program_index% [10, 11, 12, 13, 14, 15, 16, 17]) = + FlowIndex.ofList [10, 11, 12, 13, 14, 15, 16, 17] := by + decide +kernel +#print axioms literal_indices end Tests.FlowIndex diff --git a/Tests/FullImportVerification.cs b/Tests/FullImportVerification.cs index 2764bfafb..f9be7dc0b 100644 --- a/Tests/FullImportVerification.cs +++ b/Tests/FullImportVerification.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ExportVerificationSuites.cs using System.Text.Json; @@ -144,7 +149,10 @@ string[] TraceStages(string directory) => Directory.EnumerateFiles(directory, "* string pairApp = Path.Combine(pairRoot, "App"); Directory.CreateDirectory(pairApp); File.WriteAllText(Path.Combine(pairApp, "PairApp.csproj"), $$""" - net10.0ImportFixture.PairApptrue{{library}} + net11.0 + ImportFixture.PairApptrue + {{library}} + """); File.WriteAllText(Path.Combine(pairApp, "Application.cs"), "namespace ImportFixture; public static class PairApplication { public static ulong Entry(ulong value) => Library.AddSame(Library.AddOne(value)); }\n"); Run(["dotnet", "build", Path.Combine(pairApp, "PairApp.csproj"), "-c", "Release"]); diff --git a/Tests/FullModuleResolution.cs b/Tests/FullModuleResolution.cs index 31fd2aaaa..c6bc49b17 100644 --- a/Tests/FullModuleResolution.cs +++ b/Tests/FullModuleResolution.cs @@ -1,13 +1,18 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/FullModuleResolution.cs #:include ../scripts/ExportVerificationSuites.cs ExportCaseLedger completed = new("resolution"); using Program paths = new(); -string root = At("artifacts/FullModuleResolution-" + Guid.NewGuid().ToString("N")); +string root = At($"artifacts/FullModuleResolution-\U00010400-{Guid.NewGuid():N}"); Directory.CreateDirectory(root); string library = Path.GetFullPath(Path.Combine(paths.Toolchain, "lib", "lean")); SortedDictionary accepted = new(StringComparer.Ordinal) @@ -29,4 +34,19 @@ throw new InvalidOperationException("Full-verification resolution accepted a module loaded from a different file: " + rejection); Console.WriteLine("PASS full-verification resolution rejects a module loaded from a different file"); completed.CompleteCase("resolution.changed-file"); + +string artifact = Path.Combine(root, "Caller.olean"); +File.WriteAllBytes(artifact, [1, 2, 3, 4]); +SortedDictionary hashed = FullArtifactHashes([artifact]); +DateTime written = File.GetLastWriteTimeUtc(artifact); +File.WriteAllBytes(artifact, [1, 2, 3, 5]); +File.SetLastWriteTimeUtc(artifact, written); +string? rewritten = null; +try { RequireFullArtifactHashes(hashed); } +catch (InvalidDataException error) { rewritten = error.Message; } +if (rewritten?.Contains("Full-verification source or compiled artifact changed: " + artifact, + StringComparison.Ordinal) != true) + throw new InvalidOperationException("Full verification accepted a same-length artifact rewrite: " + rewritten); +Console.WriteLine("PASS full verification rejects a same-length caller artifact rewrite with its write time restored"); +completed.CompleteCase("resolution.rewritten-artifact"); completed.Finish(); diff --git a/Tests/GatePlans.cs b/Tests/GatePlans.cs index 4dd34df4a..b0f278181 100644 --- a/Tests/GatePlans.cs +++ b/Tests/GatePlans.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; using System.Text.Json.Nodes; diff --git a/Tests/GeneratedEvidence.cs b/Tests/GeneratedEvidence.cs index a35d669be..2c220c2db 100644 --- a/Tests/GeneratedEvidence.cs +++ b/Tests/GeneratedEvidence.cs @@ -5,71 +5,349 @@ partial class Program { - /// One instruction of an extracted method, as its definition in `Extracted.lean` writes it. - sealed record ExtractedInstruction(int Method, int Pc, string Op); + // Heartbeats count the elaborator's and the kernel's work on one thread, so they repeat exactly where compile + // time varies with the machine's load. Returns the count `text` costs, imports included, and Lean's output. + static (long Heartbeats, string Output) CountHeartbeats(string path, string text) + { + File.WriteAllText(path, text + "\n#eval show IO Nat from IO.getNumHeartbeats\n"); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("A measured file depends on sorryAx: " + path); + var count = HeartbeatCount.Match(output); + if (!count.Success) throw new InvalidOperationException("Missing heartbeat count:\n" + output); + return (long.Parse(count.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture), output); + } + + // The line `#eval show IO Nat from IO.getNumHeartbeats` prints. + [GeneratedRegex(@"(?m)^([0-9]+)\r?$")] + private static partial Regex HeartbeatCount { get; } + + [GeneratedRegex(@"(?m)^cil_resolution_facts [A-Za-z_][A-Za-z0-9_]*\r?\n")] + private static partial Regex ResolutionFactsCommand { get; } + + const string DecodedMethodDefinition = """cil_decoded_method method(?\d+)(?: \[[^\]\r\n]*\])?"""; + + [GeneratedRegex($"""(?m)^{DecodedMethodDefinition} :=""")] + private static partial Regex DefinedMethod { get; } + + // A decoded method's definition line: its options, then its decoder over the method's own definitions. + const string DecodedMethodHead = """(?m)^cil_decoded_method method(?\d+)( \[[^\]\r\n]*\])? := """; + const string DecoderCall = """Method\.ofClosed\? profile environment method\kLinks """; + const string DecoderInput = + """method\kReachable method\kClosed method\kRawArguments"""; + + [GeneratedRegex($"""{DecodedMethodHead}{DecoderCall}{DecoderInput}(?=\r?$)""")] + internal static partial Regex DecodedMethod { get; } + + [GeneratedRegex(@"(?m)^def method(\d+)Original : Extraction\.RawMethod := [^\r\n]*")] + private static partial Regex RawDefinition { get; } + + [GeneratedRegex(@"(?m)^def method(\d+)Closed : Extraction\.RawMethod := (?[^\r\n]+)")] + internal static partial Regex ClosedDefinition { get; } + + [GeneratedRegex(@"(?m)^def method(?\d+)Closed : [^\r\n]*?returnType := (?[^\r\n]*?), parameters := ")] + private static partial Regex ClosedReturnType { get; } + + [GeneratedRegex(@"(?m)^@\[cil_code\] def method(\d+)Link(\d+) : Link := (?[^\r\n]*)")] + private static partial Regex MethodLink { get; } + + [GeneratedRegex(@"(?m)^@\[cil_code\] def method(\d+)Link(\d+) : Link := \.direct (\d+)\r?$")] + private static partial Regex DirectLink { get; } + + // The head of a one-line theorem, up to its proof. + [GeneratedRegex(@"(?m)^(theorem (?[\w?!']+) : [^\r\n]*?) := [^\r\n]*")] + private static partial Regex TheoremHead { get; } + + // A theorem and its indented continuation lines; a Lean name may end in ?, ! or '. + [GeneratedRegex(@"(?m)^(?:@\[[^\r\n]*\] )?theorem ([\w?!']+) [^\r\n]*(?:\r?\n[ \t]+[^\r\n]*)*\r?\n")] + private static partial Regex GeneratedTheorem { get; } + + [GeneratedRegex(@" := method\d+_instruction_checks(?:\.[12])+(?=\r?$)", RegexOptions.Multiline)] + private static partial Regex InstructionCheckProjection { get; } + + [GeneratedRegex(@"\AexternalIdentity(?:Chunk\d+|\d+Bytes)_valid\z")] + private static partial Regex IdentityWitnessName { get; } - // `cil_instruction_equations` states each lookup equation from the definition, which lists one op per line. - static List ExtractedInstructions(string source) + [GeneratedRegex(@"\A(?:method\d+_tables_valid|assembly\d+_identified|pinned_identities_spelled)\z")] + private static partial Regex RetainedObligationName { get; } + + // A generated report names one theorem or a bracketed group spanning several lines. + [GeneratedRegex(@"(?m)^#print axioms (?:\[(?[\w'?!.,\s]+)\]|[\w'?!.]+)[ \t]*\r?(?:\n|\z)")] + private static partial Regex AxiomsCommand { get; } + + [GeneratedRegex(@"""(?:\\.|[^""\\])*""")] + private static partial Regex StringLiteral { get; } + + [GeneratedRegex(@"(?m)^(?:namespace|end) Extracted\r?$")] + private static partial Regex ExtractedNamespaceLine { get; } + + [GeneratedRegex(@"\bExtracted\b")] + private static partial Regex ExtractedWord { get; } + + [GeneratedRegex(@"(?m)^namespace Extracted\r?$")] + private static partial Regex NamespaceExtracted { get; } + + [GeneratedRegex(@"(?m)^end Extracted\r?$")] + private static partial Regex EndExtracted { get; } + + [GeneratedRegex(@"(?m)^import [^\r\n]+\r?\n")] + private static partial Regex ImportCommand { get; } + + [GeneratedRegex(@"(?m)^(?:namespace|end) Extracted\r?\n")] + private static partial Regex ExtractedNamespaceCommand { get; } + + [GeneratedRegex(@"^import[ \t]+([A-Za-z_][A-Za-z0-9_.]*)[ \t]*\r?$", RegexOptions.Multiline)] + private static partial Regex ImportedModule { get; } + + // An extraction's definition of method `method` from its decoder, with the field and type operands it names. + static Match DecodedDefinition(string source, int method) => DecodedMethod.Matches(source) + .FirstOrDefault(match => match.Groups["number"].Value == $"{method}") ?? Match.Empty; + + /// Whether an extraction defines method `method` with `cil_decoded_method`. + static bool IsDecodedMethod(string source, int method) => DecodedDefinition(source, method).Success; + + /// The numbers of the methods an extraction defines. + static int[] DefinedMethods(string source) => [.. DefinedMethod.Matches(source) + .Select(match => int.Parse(match.Groups["number"].Value, System.Globalization.CultureInfo.InvariantCulture))]; + + /// + /// Change the emitted instruction at a decoded pc while retaining its raw evidence. + /// + static string ForgeDecodedInstruction(string source, int method, int pc, string change) => + ForgeDecodedMethod(source, method, $"code := method.code.modify {pc} ({change})"); + + // The decoder keeps the genuine closed body. Every obligation reads the changed evidence record. + static string ForgeClosedCode(string source, int method, string code) { - var instructions = new List(); - var lines = source.Split('\n'); - for (var line = 0; line < lines.Length; line++) - { - var header = Regex.Match(lines[line], @"\Adef method(\d+) : Method := .* code := \[\r?\z"); - if (!header.Success) continue; - var method = int.Parse(header.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture); - // The line that closes the code list ends the definition, whatever follows it. - for (var (pc, last) = (0, false); !last && ++line < lines.Length; pc++) - { - var op = lines[line].Trim(); - last = op.EndsWith("] }", StringComparison.Ordinal); - if (op == "] }") break; - instructions.Add(new(method, pc, last ? op[..^3] : op[..^1])); - } - } - return instructions; + var closed = $"method{method}Closed"; + var input = $"method{method}DecoderInput"; + var definition = ClosedDefinition.Matches(source).Single(match => match.Groups[1].Value == $"{method}"); + var decoder = DecodedDefinition(source, method); + if (!decoder.Success) throw new InvalidOperationException($"Missing decoder for method{method}"); + var declarations = $$""" + def {{input}} : Extraction.RawMethod := {{definition.Groups["body"].Value}} + def {{closed}} : Extraction.RawMethod := { {{input}} with code := {{input}}.code.{{code}} } + """; + return source.Replace(definition.Value, declarations, StringComparison.Ordinal) + .Replace(decoder.Value, decoder.Value.Replace(closed, input, StringComparison.Ordinal), + StringComparison.Ordinal); } + /// + /// The line defining the raw method a decoded method is decoded from, the decoder's input. A method the + /// extraction does not define fails, so a reader never takes an absent body for an empty one. + /// + static string DecodedOriginal(string source, int method) + { + if (!IsDecodedMethod(source, method)) + throw new InvalidOperationException($"method{method} is not defined from its decoder"); + return RawDefinition.Matches(source) + .FirstOrDefault(match => match.Groups[1].Value == $"{method}")?.Value ?? ""; + } + + /// + /// Decodes the forged original so the emitted code differs from the closed evidence used by the checks. + /// + static string ForgeDecodedOriginal(string source, int method, string before, string after) + { + var original = DecodedOriginal(source, method); + if (!original.Contains(before, StringComparison.Ordinal)) + throw new InvalidOperationException($"method{method}Original has no {before}"); + var definition = DecodedDefinition(source, method); + var name = $"method{method}"; + var context = $"profile environment {name}Links {name}Reachable"; + var decoder = $"Method.ofRaw? {context} {name}Original {name}TypeArgumentsRaw"; + var command = definition.Value[..definition.Value.IndexOf("Method.ofClosed?", StringComparison.Ordinal)]; + var changed = source.Replace(definition.Value, $"""{command}{decoder}""", StringComparison.Ordinal); + return changed.Replace(original, original.Replace(before, after, StringComparison.Ordinal), + StringComparison.Ordinal); + } + + /// + /// `source` with decoded method `method` defined by a forged decoder: the extraction's decoder call with + /// `update`, a structure update of the decoded `method`, applied to the method it gives. + /// + static string ForgeDecodedMethod(string source, int method, string update) + { + // The definition writes a decoded method's links as the table constant its decoder is given. + if (update.Contains("links :=", StringComparison.Ordinal)) + throw new InvalidOperationException( + $"Forge method{method}Links, the decoder's input, to change its links"); + var call = DecodedDefinition(source, method); + if (!call.Success) throw new InvalidOperationException($"method{method} is not defined from its decoder"); + var forged = $"method{method}Forged"; + var name = $"method{method}"; + var decoder = $"{forged} profile environment {name}Links {name}Reachable {name}Closed {name}RawArguments"; + return $$""" + {{source[..call.Index]}}def {{forged}} (profile : FeatureProfile) (environment : LayoutEnvironment) + (links : List (Prod Nat Link)) (reachable : List Bool) (closed : Extraction.RawMethod) + (storage : Extraction.RawArguments) : Except Extraction.MethodDecodeError Method := + (Method.ofClosed? profile environment links reachable closed storage).map fun method => + { method with {{update}} } + cil_decoded_method {{name}}{{call.Groups[1].Value}} := {{decoder}}{{source[(call.Index + call.Length)..]}} + """; + } + + /// + /// `source` with decoded method `method` declaring its closed definition's return type with `atom` replaced by + /// `replacement`, through a forged decoder. + /// + static string ForgeDecodedReturnType(string source, int method, string atom, string replacement) + { + var closed = ClosedReturnType.Matches(source) + .FirstOrDefault(match => match.Groups["number"].Value == $"{method}"); + var returnType = closed?.Groups["type"].Value ?? ""; + if (!returnType.Contains(atom, StringComparison.Ordinal)) + throw new InvalidOperationException($"method{method}Closed returns no {atom}"); + return ForgeDecodedMethod(source, method, "declared := { method.declared with returnType := " + + returnType.Replace(atom, replacement, StringComparison.Ordinal) + " }"); + } + + /// + /// `source` with the link of the call at `pc` of method `method` changed to `link`: the definition the method's + /// link table names. + /// + static string ForgeLink(string source, int method, int pc, string link) + { + var definition = MethodLink.Matches(source).FirstOrDefault(match => + match.Groups[1].Value == $"{method}" && match.Groups[2].Value == $"{pc}") + ?? throw new InvalidOperationException($"method{method} has no link at pc {pc}"); + return source[..definition.Index] + $"@[cil_code] def method{method}Link{pc} : Link := {link}" + + source[(definition.Index + definition.Length)..]; + } + + /// + /// The arguments of method `method`'s feature-flow obligation that a decided-false report prints on one line, + /// naming that method's raw definition and certificate. + /// + static string FeatureObligation(object method) => + $"method{method}Closed profile method{method}FeatureStates method{method}Reachable"; + + /// The single binding of this kind in a fixture method. + static (int Pc, string Link) SingleLink(string source, int method, string constructor) + { + var binding = MethodLink.Matches(source).Single(match => match.Groups[1].Value == $"{method}" && + match.Groups["link"].Value.StartsWith(constructor, StringComparison.Ordinal)); + return (int.Parse(binding.Groups[2].Value, System.Globalization.CultureInfo.InvariantCulture), + binding.Groups["link"].Value); + } + + /// The method and pc of each call whose link in `source` is the direct link to method `callee`. + static (int Method, int Pc)[] DirectLinks(string source, int callee) => [.. DirectLink.Matches(source) + .Where(match => match.Groups[3].Value == $"{callee}") + .Select(match => (int.Parse(match.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture), + int.Parse(match.Groups[2].Value, System.Globalization.CultureInfo.InvariantCulture)))]; + // Keep emitted definitions intact and select one independent validator obligation. // Its unchanged form must also be compiled as a positive control by the caller. static string NarrowEvidence(string source, string theorem) { var found = 0; - var result = Regex.Replace(source, - @"(?m)^(?:@\[[^\r\n]*\] )?theorem (\w+) [^\r\n]*(?:\r?\n[ \t]+[^\r\n]*)*\r?\n", match => + var result = GeneratedTheorem.Replace(source, match => { // Byte-backed identity definitions carry these checked UTF-8 witnesses. - if (Regex.IsMatch(match.Groups[1].Value, @"\AexternalIdentity(?:Chunk\d+|\d+Bytes)_valid\z")) + if (IdentityWitnessName.IsMatch(match.Groups[1].Value)) + return match.Value; + // A body obligation reads its module's lookup tables through their equality with the metadata, the + // identity of each assembly it reaches through the assembly's own proof, and the pins through theirs. + if (match.Groups[1].Value != theorem && + RetainedObligationName.IsMatch(match.Groups[1].Value) && + !theorem.EndsWith("_tables_valid", StringComparison.Ordinal)) return match.Value; if (match.Groups[1].Value != theorem) return ""; found++; - return match.Value; + // Isolated layout, object and scalar checks decide the fixture's complete obligation. + if (match.Value.Contains(" : Extraction.MethodLayoutsValid ", StringComparison.Ordinal) || + match.Value.Contains(" : Extraction.ObjectOperandsValid ", StringComparison.Ordinal) || + match.Value.Contains(" : Extraction.RuntimeModelsAgree ", StringComparison.Ordinal) || + match.Value.Contains(" : And (Extraction.ScalarFlowValid ", StringComparison.Ordinal)) + return TheoremHead.Replace(match.Value, "$1 := by decide +kernel"); + return InstructionCheckProjection.Replace(match.Value, " := by decide +kernel"); }); if (found != 1) throw new InvalidOperationException("Missing unique evidence obligation: " + theorem); - return Regex.Replace(result, @"(?m)^#print axioms [^\r\n]*\r?\n", ""); + result = ResolutionFactsCommand.Replace(result, ""); + return AxiomsCommand.Replace(result, ""); + } + + /// Exchange call and newobj at a constructor site, preserving its exact member. + static string ForgeConstructorOpcode(string source, int pc) => ForgeModelEmission(source, pc, $$""" + match method0.code[{{pc}}]'(by decide +kernel) with + | .newobj ctor => .call ctor + | .call ctor _ => .newobj ctor + | original => original + """.ReplaceLineEndings(" ")); + + /// A call with the original prefixes and a changed method operand at this instruction position. + static string ChangedCallOperand(int pc, string fields) => + $$""" + match (method0.code[{{pc}}]'(by decide +kernel) : CIL.Instruction) with + | .call method prefixes => .call { method with {{fields}} } prefixes + | original => original + """.ReplaceLineEndings(" "); + + /// Check a replacement instruction at a live pc against the original call operand. + static string ForgeModelEmission(string source, int pc, string instruction) + { + const string inputs = "method0Closed method0Reachable method0"; + var narrowed = NarrowEvidence(source, "method0_models_validated"); + var changed = narrowed.Replace(inputs, + $$"""method0Closed method0Reachable { method0 with code := method0.code.set {{pc}} ({{instruction}}) }""", + StringComparison.Ordinal); + if (changed == narrowed) throw new InvalidOperationException("Missing model agreement inputs"); + return changed; } + /// + /// A theorem that the generated model fact of the call or construction at `pc` of method0 is `model`. It + /// elaborates only when the runtime table resolves that site to exactly `model`. + /// + static string StatedModelFact(int pc, string model) => $$""" + + namespace ModelFactEvidence + /-- The generated model fact of the site at pc {{pc}} names the stated model. -/ + theorem stated_model : True := by + have _fact : CIL.Extraction.runtimeModel _ _ _ = some ({{model}}) := Extracted.method0_model{{pc}} + trivial + #print axioms stated_model + end ModelFactEvidence + + """; + + /// `source` narrowed to its model check, stating that the model fact at `pc` is `model`. + static string ForgeModelFact(string source, int pc, string model) => + $"{NarrowEvidence(source, "method0_models_validated")}{StatedModelFact(pc, model)}"; + + /// The kernel rejected the emitted argument evidence at the closed method's signature. + static bool ClosedArgumentsMismatch(string output) => output.ReplaceLineEndings("\n").Contains( + "cil_kernel_rfl: the kernel does not accept the two sides as equal\n" + + "left:\n Extraction.closedArguments rawLayouts method0Closed\nright:\n some method0RawArguments", + StringComparison.Ordinal); + + /// The rejection of a stated model fact: a type mismatch at the fact of the site at `pc`. + static Func ModelFactMismatch(int pc) => output => + output.Contains("Type mismatch", StringComparison.Ordinal) && + output.Contains($"method0_model{pc}", StringComparison.Ordinal); + // Keep narrowed declarations and exact obligation types checked, but reuse the original // proof instead of reducing the same validator twice. Mutations never use these witnesses. static string CheckedEvidenceControl(string original, string narrowed, IEnumerable theorems, int index) { // Explicit references could bypass the projected declarations and reach the original. - var code = Regex.Replace(narrowed, @"""(?:\\.|[^""\\])*""", "\"\""); - code = Regex.Replace(code, @"(?m)^(?:namespace|end) Extracted\r?$", ""); - if (Regex.IsMatch(code, @"\bExtracted\b")) + var code = StringLiteral.Replace(narrowed, "\"\""); + code = ExtractedNamespaceLine.Replace(code, ""); + if (ExtractedWord.IsMatch(code)) throw new InvalidOperationException("Evidence controls require namespace-relative references."); - if (Regex.Matches(narrowed, @"(?m)^namespace Extracted\r?$").Count != 1 || - Regex.Matches(narrowed, @"(?m)^end Extracted\r?$").Count != 1) + if (NamespaceExtracted.Count(narrowed) != 1 || EndExtracted.Count(narrowed) != 1) throw new InvalidOperationException("Missing unique evidence control namespace."); var witnesses = new StringBuilder(); var audits = new StringBuilder(); foreach (var theorem in theorems) { var witness = $"evidenceControl{index}_{theorem}"; - var pattern = @"(?m)^(theorem " + Regex.Escape(theorem) + @" : [^\r\n]*?) := [^\r\n]*"; - var originalMatches = Regex.Matches(original, pattern); - var narrowedMatches = Regex.Matches(narrowed, pattern); - if (narrowedMatches.Count != 1 || originalMatches.Count != 1 || + Match[] Stated(string text) => + [.. TheoremHead.Matches(text).Where(match => match.Groups["name"].Value == theorem)]; + var (originalMatches, narrowedMatches) = (Stated(original), Stated(narrowed)); + if (narrowedMatches.Length != 1 || originalMatches.Length != 1 || narrowedMatches[0].Groups[1].Value != originalMatches[0].Groups[1].Value) throw new InvalidOperationException("Missing unique control obligation: " + theorem); var header = originalMatches[0].Groups[1].Value; @@ -81,9 +359,9 @@ static string CheckedEvidenceControl(string original, string narrowed, IEnumerab narrowed[(obligation.Index + obligation.Length)..]; audits.AppendLine($"#print axioms EvidenceControl{index}.{theorem}"); } - narrowed = Regex.Replace(narrowed, @"(?m)^import [^\r\n]+\r?\n", ""); + narrowed = ImportCommand.Replace(narrowed, ""); // A nested Extracted namespace can resolve missing names in the original Extracted. - narrowed = Regex.Replace(narrowed, @"(?m)^(?:namespace|end) Extracted\r?\n", ""); + narrowed = ExtractedNamespaceCommand.Replace(narrowed, ""); return witnesses + $"\nnamespace EvidenceControl{index}\nset_option autoImplicit false\n" + narrowed + $"\nend EvidenceControl{index}\n" + audits; } @@ -107,11 +385,11 @@ string BatchSource() if (!source.Contains("namespace Extracted\n", StringComparison.Ordinal) || !source.Contains("theorem " + cases[index].Theorem + " :", StringComparison.Ordinal)) throw new InvalidOperationException("Expected generated extraction evidence: " + cases[index].Path); - var body = Regex.Replace(source, @"^import[ \t]+([A-Za-z_][A-Za-z0-9_.]*)[ \t]*\r?$", match => + var body = ImportedModule.Replace(source, match => { imports.Add(match.Groups[1].Value); return ""; - }, RegexOptions.Multiline); + }); bodies.AppendLine($"namespace EvidenceCase{index}"); bodies.AppendLine(body); bodies.AppendLine("#print axioms Extracted." + cases[index].Theorem); diff --git a/Tests/GenericEvidence.cs b/Tests/GenericEvidence.cs index e7b5800ee..cb6e6acd3 100644 --- a/Tests/GenericEvidence.cs +++ b/Tests/GenericEvidence.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include RejectedEvidence.cs #:include GeneratedEvidence.cs @@ -8,16 +13,32 @@ using System.Text; using System.Text.Json; +using System.Text.RegularExpressions; using Nethermind.LeanCIL; -if (args is not ([] or ["--external-identities"] or ["--measure"])) - throw new ArgumentException("Usage: GenericEvidence.cs [--external-identities|--measure]"); +if (args is not ([] or ["--external-identities"] or ["--measure"] or ["--return-only"])) + throw new ArgumentException("Usage: GenericEvidence.cs [--external-identities|--measure|--return-only]"); var output = At("artifacts/GenericEvidence"); Directory.CreateDirectory(output); var rejected = new RejectedEvidence(output); var evidence = new GeneratedEvidence(output, args.Contains("--measure", StringComparer.Ordinal)); var controls = new Dictionary>(); var controlSources = new HashSet(StringComparer.Ordinal); +if (args is ["--return-only"]) +{ + RequireLeanFixtures("CIL.Extraction.Translation"); + var directory = Path.Combine(output, "9"); + Run(Extract("extract", At("Examples/Assembly/bin/Examples.dll"), "Examples", + "GenericMethods::EnumIdentity(Examples.Signed8)", directory)); + ReturnMutations(); + var baseline = File.ReadAllText(Path.Combine(directory, "Extracted.lean")); + var positive = Path.Combine(output, "ReturnControl.lean"); + File.WriteAllText(positive, baseline + "\n" + string.Join("\n", controls[9])); + evidence.Add(positive, "PASS declared return control"); + evidence.Check(); + rejected.Check(); + return; +} CheckExternalIdentities(); if (args is ["--external-identities"]) { @@ -106,10 +127,17 @@ string ChangeDefinition(string source, string name, Func change) var start = source.IndexOf("def " + name + " :", StringComparison.Ordinal); if (start < 0) throw new InvalidOperationException("Missing definition " + name); var end = source.IndexOf('\n', start); - return source[..start] + change(source[start..end]) + source[end..]; + var changed = change(source[start..end]); + // A replacement that matched nothing would leave a fixture rejected for another reason. + if (changed == source[start..end]) throw new InvalidOperationException("Mutation did not change " + name); + return source[..start] + changed + source[end..]; } +string Replaced(string text, string before, string after) => + text.Contains(before, StringComparison.Ordinal) + ? text.Replace(before, after, StringComparison.Ordinal) + : throw new InvalidOperationException("Missing mutation target: " + before); void Reject(int sample, string name, Func mutate, - string? theorem = "method1_specialization_validated") + string? theorem = "method1_specialization_validated", string diagnostic = "error:") { var baseline = File.ReadAllText(Path.Combine(output, sample.ToString(), "Extracted.lean")); var source = mutate(baseline); @@ -126,12 +154,12 @@ void Reject(int sample, string name, Func mutate, } var path = Path.Combine(output, name + ".lean"); File.WriteAllText(path, source); - rejected.Add(path, "PASS generic evidence rejects " + name, "error:"); + rejected.Add(path, "PASS generic evidence rejects " + name, diagnostic); } foreach (var (name, scope) in new[] { ("wrong-runtime-version", "System.Runtime, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), - ("wrong-runtime-token", "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=0000000000000000") + ("wrong-runtime-token", "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=0000000000000000") }) Reject(0, name, source => ChangeDefinition(source, "method1Closed", line => line.Replace("Extraction.runtimeScope", System.Text.Json.JsonSerializer.Serialize(scope)))); @@ -147,20 +175,176 @@ void Reject(int sample, string name, Func mutate, line => line.Replace("opcode := 2,", "opcode := 3,"))); Reject(4, "altered-branch", source => ChangeDefinition(source, "method1Closed", line => line.Replace("operand := .target 4", "operand := .target 3"))); -Reject(0, "wrong-managed-target", source => source.Replace(".call 1 1", ".call 0 1"), "method0_calls_validated"); +// A failed kernel decision prints this text; a mutation that only breaks parsing does not. +const string decisionFailed = "proved that the proposition"; +// A theorem proved by `rfl` prints this text when its two sides differ. +const string rflRefused = "not definitionally equal"; +// A call names the method its raw operand names, so the binding check rejects a call naming another method. +Reject(0, "wrong-call-operand", source => ForgeDecodedMethod(source, 0, "code := method.code.map fun " + + "| .call callee prefixes => .call { callee with name := name!\"Other\" } prefixes | other => other"), + "method0_calls_validated", decisionFailed); Console.WriteLine("PASS original-metadata generic substitution, complete managed-call binding and malformed evidence rejection"); +// Each mutation changes one fact of the first sample's body evidence, so only the decoding check can reject it; +// the unmutated obligation is checked as the positive control. +const string bodyTheorem = "method0_body_validated"; +// The tactic that proves a body obligation names the method and the failure when the kernel refutes it; a mutation +// that only breaks parsing prints other text, and the unmutated obligation is the positive control. +const string bodyDecisionFailed = "the decoded method does not match its body bytes or metadata"; +// A mutated body obligation must fail to elaborate and its negation must be proved by the kernel. +void RejectBody(int sample, string name, string theorem, string definition, Func change) +{ + Reject(sample, name, source => ChangeDefinition(source, definition, change), theorem, bodyDecisionFailed); + RefuteMutation(sample, name, theorem, source => ChangeDefinition(source, definition, change)); +} +// A file that keeps the mutated definitions and states the obligation's negation, which the kernel must prove. +void RefuteMutation(int sample, string name, string theorem, Func mutate) +{ + var mutated = mutate(File.ReadAllText(Path.Combine(output, sample.ToString(), "Extracted.lean"))); + var refutation = theorem[..^"_validated".Length] + "_refuted"; + var refuted = KernelRefutation(mutated, theorem, refutation); + var path = Path.Combine(output, "Refuted-" + name + ".lean"); + File.WriteAllText(path, NarrowEvidence(refuted, refutation)); + evidence.Add(path, "PASS the kernel proves the mutated obligation false: " + name, refutation); +} +// A changed declared signature of method `method` fails its `rfl` obligation, which the kernel proves false. +void RejectDeclared(int sample, string name, int method, Func mutate) +{ + var theorem = $"method{method}_declared_validated"; + Reject(sample, name, mutate, theorem, rflRefused); + RefuteMutation(sample, name, theorem, mutate); +} +// The metadata rows of a body shift with the assembly, so each byte is found by its position in the emitted list. +string ChangeByte(string line, Regex pattern, int change) => pattern.Replace(line, + match => match.Groups[1].Value + (int.Parse(match.Groups[2].Value) + change) + match.Groups[3].Value); +// A byte list is emitted as `(CIL.Extraction.fixedBytes 0x)`; a pattern's second group is one byte. +string ChangeLiteralByte(string line, Regex pattern, int change) => pattern.Replace(line, + match => match.Groups[1].Value + ((Convert.ToInt32(match.Groups[2].Value, 16) + change) % 256).ToString("X2")); +// Sample 0 starts `ldarg.0; call ` under a tiny header: header, ldarg.0, call, then the token bytes. +RejectBody(0, "altered-body-byte", bodyTheorem, "method0Bytes", + line => ChangeLiteralByte(line, GenericPatterns.SecondByte, 1)); +RejectBody(0, "altered-body-header", bodyTheorem, "method0Bytes", + line => ChangeLiteralByte(line, GenericPatterns.FirstByte, 4)); +RejectBody(0, "altered-token-operand-byte", bodyTheorem, "method0Bytes", + line => ChangeLiteralByte(line, GenericPatterns.TokenOperandByte, 1)); +// A count one short of the digits keeps the code but drops its leading byte, the body header. +RejectBody(0, "altered-literal-count", bodyTheorem, "method0Bytes", + line => ChangeByte(line, GenericPatterns.LiteralCount, -1)); +// Keep the vector's name and arity, but give its platform definition a class marker. +string WrongVectorKind(string source) +{ + var head = $"0x{Convert.ToHexString("Vector128`1"u8)})], kind := "; + return Replaced(source, head + ".genericValue 1", head + ".genericClass 1"); +} +Reject(6, "wrong-instantiation-marker", WrongVectorKind, "method1_body_validated", bodyDecisionFailed); +RefuteMutation(6, "wrong-instantiation-marker", "method1_body_validated", WrongVectorKind); +RejectBody(0, "altered-token-table-key", bodyTheorem, "method0Body", + line => ChangeByte(line, GenericPatterns.CallTokenKey, 1)); +RejectBody(0, "altered-token-table-operand", bodyTheorem, "method0Body", + line => line.Replace("name := \"Identity\"", "name := \"Other\"")); +RejectBody(0, "altered-instruction-offset", bodyTheorem, "method0Original", + line => line.Replace("offset := 1, opcode := 40", "offset := 2, opcode := 40")); +RejectBody(0, "altered-max-stack", bodyTheorem, "method0Original", + line => line.Replace("maxStack := 8", "maxStack := 9")); +// The module's lookup tables are decided once against its metadata; a changed row, heap entry or table is +// refuted there. +void RejectTables(string name, string definition, Func change) +{ + const string theorem = "method0_tables_valid"; + Reject(0, name, source => ChangeDefinition(source, definition, change), theorem); + var mutated = ChangeDefinition(File.ReadAllText(Path.Combine(output, "0", "Extracted.lean")), definition, change); + var refuted = KernelRefutation(mutated, theorem, "method0_tables_refuted"); + var path = Path.Combine(output, "RefutedTables-" + name + ".lean"); + File.WriteAllText(path, NarrowEvidence(refuted, "method0_tables_refuted")); + evidence.Add(path, "PASS the kernel proves the mutated tables obligation false: " + name, + "method0_tables_refuted"); +} +RejectTables("altered-metadata-row", "method0Metadata", + line => ChangeLiteralByte(line, GenericPatterns.MetadataRowByte, 1)); +RejectTables("altered-metadata-string", "method0Metadata", + line => ChangeLiteralByte(line, GenericPatterns.MetadataStringByte, 1)); +RejectTables("altered-lookup-row", "method0Tables", + line => ChangeLiteralByte(line, GenericPatterns.LookupRowByte, 1)); +// A leaf entry of the rows tree removed, and one added before another. +RejectTables("missing-lookup-row", "method0Tables", + line => GenericPatterns.LeafRow.Replace(line, ".leaf", 1)); +RejectTables("extra-lookup-row", "method0Tables", + line => GenericPatterns.LeafRowParts.Replace(line, "(.node (.node .leaf 1 [0] .leaf) $1 $2 .leaf)", 1)); +RejectBody(4, "altered-body-branch-index", "method1_body_validated", "method1Original", + line => line.Replace("operand := .target 4", "operand := .target 3")); +Console.WriteLine("PASS body bytes, token table and original definition reject changed bytes and operands"); Reject(0, "omitted-method-evidence", source => ChangeDefinition(source, "methodEvidence", _ => "def methodEvidence : List Extraction.MethodEvidence := []"), null); Reject(0, "reordered-method-evidence", source => ChangeDefinition(source, "methodEvidence", line => line.Replace("method0", "swapped").Replace("method1", "method0").Replace("swapped", "method1")), null); -Reject(0, "wrong-emitted-return", source => ChangeDefinition(source, "method1", - line => line.Replace("returnsValue := true", "returnsValue := false")) - .Replace("method1.returnsValue = (true)", "method1.returnsValue = (false)"), "method1_shape_validated"); -Reject(7, "wrong-local-initializer", source => source.Replace("[.i32 0]", "[.i32 1]"), "method1_shape_validated"); -Reject(7, "wrong-initlocals-flag", source => source.Replace("initLocals := true", "initLocals := false"), "method1_shape_validated"); -Reject(9, "missing-return-narrowing", source => source.Replace(".ret (some .signed8)", ".ret none"), "method1_shape_validated"); -Reject(9, "wrong-return-signedness", source => source.Replace(".ret (some .signed8)", ".ret (some .unsigned8)"), +// Identity64(ulong) and its callee Identity(ulong) have the same shape; with their signature strings swapped, +// each body check refutes the string a proof would select the method by. +string SwapSignatures(string source) +{ + string Value(string name) => GenericPatterns.StringDefinition.Matches(source) + .FirstOrDefault(match => match.Groups["name"].Value == name)?.Groups["value"].Value ?? ""; + var (first, second) = (Value("method0Signature"), Value("method1Signature")); + return ChangeDefinition(ChangeDefinition(source, "method0Signature", line => line.Replace(first, second)), + "method1Signature", line => line.Replace(second, first)); +} +const string equationRefused = "cil_kernel_rfl: the kernel does not accept the two sides as equal"; +Reject(0, "swapped-signature-of-a-method", SwapSignatures, bodyTheorem, equationRefused); +Reject(0, "swapped-signature-of-an-instance", SwapSignatures, "method1_body_validated", equationRefused); +// The callee is defined from its decoder, so the forged return flag is the value of a forged decoder. +Reject(0, "wrong-emitted-return", source => ForgeDecodedMethod(source, 1, "returnsValue := false"), + "method1_shape_validated"); +Reject(7, "wrong-local-initializer", source => ForgeDecodedMethod(source, 1, "locals := [.i32 1]"), "method1_shape_validated"); +Reject(7, "wrong-initlocals-flag", source => ChangeDefinition( + ForgeDecodedMethod(source, 1, "locals := [.i32 0]"), "method1Closed", + line => line.Replace("initLocals := true", "initLocals := false", StringComparison.Ordinal)), + "method1_shape_validated"); +ReturnMutations(); + +void ReturnMutations() +{ + foreach (var (name, replacement) in new[] + { ("missing-return-narrowing", "System.Int32"), ("wrong-return-signedness", "System.Byte") }) + // The callee is defined from its decoder, so the forged declared return is a forged decoder's value. + RejectDeclared(9, name, 1, source => ForgeDecodedReturnType(source, 1, "\"System.SByte\"", + JsonSerializer.Serialize(replacement))); +} + +// The declared signature, call links and layout environment are each checked against their own evidence. +const string word = "Extraction.runtimeScope \"System.UInt64\""; +// The caller is defined from its decoder, so each forged declared signature is the value of a forged decoder. +RejectDeclared(0, "wrong-declared-parameter", 0, source => ForgeDecodedMethod(source, 0, + $"declared := {{ method.declared with parameters := [.atom {word.Replace("UInt64", "UInt32")}] }}")); +RejectDeclared(0, "wrong-declared-return", 0, source => ForgeDecodedMethod(source, 0, + $"declared := {{ method.declared with returnType := .atom {word.Replace("UInt64", "UInt32")} }}")); +RejectDeclared(0, "wrong-declared-init-flag", 0, source => ForgeDecodedMethod(source, 0, + "declared := { method.declared with initLocals := true }")); +// The instance method is defined from its decoder, so its forged receiver is the value of a forged decoder. +RejectDeclared(14, "missing-declared-receiver", 1, + source => ForgeDecodedMethod(source, 1, "declared := { method.declared with receiver := none }")); +RejectDeclared(14, "wrong-declared-receiver-kind", 1, + source => ForgeDecodedMethod(source, 1, "declared := { method.declared with receiver := some .object }")); +Reject(0, "wrong-link-callee", source => ChangeDefinition(source, "method0Link1", line => + line.Replace(".direct 1", ".direct 0")), "method0_calls_validated", decisionFailed); +// A missing link at the live call produces the call hole before a method can be defined. +const string missingCallLink = "CIL.Extraction.MethodDecodeError.liveHole 1 40"; +Reject(0, "dropped-link", source => ChangeDefinition(source, "method0Links", line => + Replaced(line, "[(1, method0Link1)]", "[]")), "method0_calls_validated", missingCallLink); +Reject(0, "link-at-wrong-pc", source => ChangeDefinition(source, "method0Links", line => + Replaced(line, "[(1, method0Link1)]", "[(0, method0Link1)]")), + "method0_calls_validated", missingCallLink); +// Managed-call evidence also rejects the same forged tables in an already decoded method. +foreach (var (name, links) in new[] { ("dropped-link", "[]"), ("link-at-wrong-pc", "[(0, .direct 1)]") }) +{ + const string prefix = "method0DispatchTypes method0Closed) method0Reachable"; + Reject(0, $"emitted-{name}", source => Replaced(source, $"{prefix} method0 :=", + $$"""{{prefix}} { method0 with links := {{links}} } :="""), "method0_calls_validated", decisionFailed); +} +Reject(0, "link-at-non-call", source => ChangeDefinition(source, "method1Links", line => + Replaced(line, ":= []", ":= [(0, method0Link1)]")), + "method1_calls_validated", decisionFailed); +Reject(0, "wrong-environment", source => ChangeDefinition(source, "environment", line => + line.Replace("layouts := computedLayouts", "layouts := #[⟨⟨4, []⟩, 4⟩]")), "method0_environment_validated", + rflRefused); foreach (var (selector, index) in samples.Select((selector, index) => (selector, index))) { @@ -178,7 +362,7 @@ void CheckExternalIdentities() var identityEvidence = new ExternalIdentityEvidence(); var identities = new[] { - "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a::" + + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a::" + "System.UInt64 Namespace.Type::Method(System.UInt64)", "Unicode::Namespace.λ漢字::Method(\"quoted\",\\path)", "System.Runtime, Version=9.0.0.0, Culture=neutral, PublicKeyToken=0000000000000000::" + @@ -217,7 +401,7 @@ void CheckExternalIdentities() foreach (var (name, source) in new[] { ("changed-byte", baseline.Replace("#[83,", "#[84,", StringComparison.Ordinal)), - ("changed-version", baseline.Replace("Version=10.0.0.0", "Version=11.0.0.0", StringComparison.Ordinal)), + ("changed-version", baseline.Replace("Version=11.0.0.0", "Version=10.0.0.0", StringComparison.Ordinal)), ("changed-token", baseline.Replace("b03f5f7f11d50a3a", "b03f5f7f11d50a3b", StringComparison.Ordinal)) }) { @@ -237,23 +421,88 @@ void CheckExternalCallTargets() Run(Extract("extract", assembly, "Examples", "DivisionIntrinsics::UnsafeEquals(ulong)", folder, "x64-v3")); var path = Path.Combine(folder, "Extracted.lean"); var baseline = File.ReadAllText(path); - const string theorem = "method0_intrinsics_validated"; + const string theorem = "method0_models_validated"; var control = NarrowEvidence(baseline, theorem); var positive = Path.Combine(folder, "Checked.lean"); File.WriteAllText(positive, baseline + "\n" + CheckedEvidenceControl(baseline, control, [theorem], 0)); evidence.Add(positive, "PASS complete materialized external call certificate and mutation control"); - foreach (var (name, source) in new[] + foreach (var (name, source, obligation, markers) in new (string, string, string, string[])[] { + // CreateScalarUnsafe keeps its call and runs its model, so the body evidence, which binds each raw call + // target to its metadata row, refuses another target. ("wrong-raw-target", baseline.Replace("target := .external externalIdentity0", - "target := .external externalIdentity1", StringComparison.Ordinal)), - ("wrong-emitted-intrinsic", baseline.Replace(".vector .createScalarUnsafe64", - ".avx2 .band", StringComparison.Ordinal)) + "target := .external externalIdentity1", StringComparison.Ordinal), bodyTheorem, [bodyDecisionFailed]), + // The typed scalar-unsafe reference and its declaration still name the original member. + ("crossed-Avx2-And-target", baseline.Replace( + """name := "CreateScalarUnsafe", target := .external externalIdentity0""", + $"""name := "CreateScalarUnsafe", target := .external {AndTarget()}""", + StringComparison.Ordinal), bodyTheorem, [bodyDecisionFailed]), + // Substitute the decoded CreateScalarUnsafe call at pc 1 for op_Equality at pc 4. + ("wrong-emitted-intrinsic", ForgeDecodedInstruction(baseline, 0, 4, "fun _ => method.code[1]'(by decide)"), + theorem, ["Extraction.RuntimeModelsAgree", "is false"]) }) { if (source == baseline) throw new InvalidOperationException("Call mutation was not applied: " + name); var rejectedPath = Path.Combine(folder, name + ".lean"); - File.WriteAllText(rejectedPath, NarrowEvidence(source, theorem)); - rejected.Add(rejectedPath, "PASS materialized external call rejects " + name, - "Extraction.ExactIntrinsicsValid", "is false"); + if (name == "crossed-Avx2-And-target") + { + File.WriteAllText(rejectedPath, source); + const string refuted = "crossed_target_fails_body_binding"; + var refutationPath = Path.Combine(folder, "RefutedCrossedTarget.lean"); + File.WriteAllText(refutationPath, NarrowEvidence(KernelRefutation(source, obligation, refuted), refuted)); + evidence.Add(refutationPath, + "PASS the kernel refutes BodyDecodes for the crossed Avx2.And target", refuted); + } + else File.WriteAllText(rejectedPath, NarrowEvidence(source, obligation)); + rejected.Add(rejectedPath, "PASS materialized external call rejects " + name, markers); } } + +static string AndTarget() +{ + const string vector = "System.Runtime.Intrinsics.Vector256`1"; + const string owner = "System.Runtime.Intrinsics.X86.Avx2"; + return $"""(CIL.Extraction.intrinsicsScope ++ "::{vector} {owner}::And({vector},{vector})")"""; +} + +/// The patterns that find the bytes and rows a mutation changes in generated evidence. +static partial class GenericPatterns +{ + // A byte list is emitted as (CIL.Extraction.fixedBytes count 0xdigits); a byte pattern's second group is one byte. + const string LiteralHead = """\(CIL\.Extraction\.fixedBytes """; + const string ByteLiteral = $$"""{{LiteralHead}}\d+ 0x"""; + + [GeneratedRegex($$"""({{ByteLiteral}})([0-9A-F]{2})""")] + internal static partial Regex FirstByte { get; } + + [GeneratedRegex($$"""({{ByteLiteral}}[0-9A-F]{2})([0-9A-F]{2})""")] + internal static partial Regex SecondByte { get; } + + /// The first token byte of a body that starts with a tiny header, ldarg.0 and call. + [GeneratedRegex($$"""({{ByteLiteral}}[0-9A-F]{2}0228)([0-9A-F]{2})""")] + internal static partial Regex TokenOperandByte { get; } + + [GeneratedRegex($$"""({{LiteralHead}})(\d+)( 0x)""")] + internal static partial Regex LiteralCount { get; } + + [GeneratedRegex(@"(\()(\d+)(, \.call)")] + internal static partial Regex CallTokenKey { get; } + + [GeneratedRegex($$"""(rows := \[\(\d+, {{ByteLiteral}})([0-9A-F]{2})""")] + internal static partial Regex MetadataRowByte { get; } + + [GeneratedRegex($$"""(strings := \[\(0, {{LiteralHead}}1 0x00\)\), \(\d+, {{ByteLiteral}})([0-9A-F]{2})""")] + internal static partial Regex MetadataStringByte { get; } + + [GeneratedRegex($$"""(rows := .*?{{ByteLiteral}})([0-9A-F]{2})""")] + internal static partial Regex LookupRowByte { get; } + + [GeneratedRegex($$"""\(\.node \.leaf \d+ {{ByteLiteral}}[0-9A-F]+\) \.leaf\)""")] + internal static partial Regex LeafRow { get; } + + [GeneratedRegex($$"""\(\.node \.leaf (\d+) ({{ByteLiteral}}[0-9A-F]+\)) \.leaf\)""")] + internal static partial Regex LeafRowParts { get; } + + [GeneratedRegex(@"(?m)^def (?\w+) : String := (?.*)$")] + internal static partial Regex StringDefinition { get; } +} diff --git a/Tests/GenericImportIdentities.cs b/Tests/GenericImportIdentities.cs new file mode 100644 index 000000000..651795f01 --- /dev/null +++ b/Tests/GenericImportIdentities.cs @@ -0,0 +1,240 @@ +using System.Globalization; +using System.Reflection; +using System.Text; +using Mono.Cecil; +using Nethermind.LeanCIL; + +partial class Program +{ + static void CheckGenericImportIdentities(string library, string output, Type metadata) + { + Run(["lake", "build", "CIL.Extraction.BodyDecoding"]); + long previousNames = 0; + foreach (int size in new[] { 64, 256, 1024 }) + { + string path = Path.Combine(output, "ClosedNameScaling" + size + ".lean"); + File.WriteAllText(path, $$""" + import CIL.Extraction.MetadataBinding + open CIL CIL.Extraction + set_option maxRecDepth 100000 + set_option maxHeartbeats 10000000 + /-- Closed arguments share the independently expected UInt32 spelling. -/ + def arguments : List ResolvedType := List.replicate {{size}} (.primitive 9) + /-- Closed argument names are indexed once for the signature. -/ + def argumentNames : SearchTree (List Nat) := SearchTree.ofSorted + (((arguments.mapM (cecilName false resolveFuel)).getD []).zipIdx.map + fun (name, index) => (index, name)) + /-- Open parameters select every argument in declaration order. -/ + def parameters : List ResolvedType := (List.range {{size}}).map .mvar + /-- Parameter rendering preserves each selected argument's name. -/ + theorem closed_names : + parameters.mapM (contractClosedName argumentNames resolveFuel) = + some (List.replicate {{size}} (bytesOf "System.UInt32")) := by decide +kernel + #print axioms closed_names + #eval show IO Nat from IO.getNumHeartbeats + """); + string result = Compile(path); + long heartbeats = long.Parse(result.Split('\n', StringSplitOptions.TrimEntries | + StringSplitOptions.RemoveEmptyEntries).Last(), CultureInfo.InvariantCulture); + Console.WriteLine($"PASS closed names {size} arguments and parameters: {heartbeats} heartbeats"); + if (previousNames != 0 && heartbeats > previousNames * 6) + throw new InvalidOperationException("Closed name indexing exceeded linear growth: " + result); + previousNames = heartbeats; + } + using var pinned = new PinnedMetadata(metadata.Assembly, library); + MethodInfo instantiate = metadata.GetMethod("Instantiate", BindingFlags.Static | BindingFlags.NonPublic)!; + ModuleDefinition module = pinned.Module; + string[] identities = module.GetType("TypedFixture.Library").Methods + .Where(method => method.Name is "Identity" or "Ignore" or "Overload" or "First") + .Append(module.GetType("TypedFixture.\u03A9wner").Methods.Single(method => method.HasGenericParameters)) + .Select(Check).ToArray(); + if (identities.Distinct(StringComparer.Ordinal).Count() != identities.Length) + throw new InvalidOperationException( + "Distinct open definitions produced the same closed contract identity."); + Console.WriteLine("PASS exact generic identity, void, Unicode and distinct open overloads"); + + MethodDefinition identity = module.GetType("TypedFixture.Library").Methods + .Single(method => method.Name == "Identity"); + long previous = 0; + foreach (int size in new[] { 128, 512, 2048 }) + { + identity.Name = new string('N', size); + string text = Source(identity, Selected(identity)) + """ + + /-- A linked slot resolves under its full identity and rejects an appended byte. -/ + theorem indexed_import (call : CallSite) : + let slot : ImportSlot := ⟨identity, "", 1, false, true, none, {}⟩ + let imports := (BodyImports.mk [(0, slot)] #[]).byIdentity + (callImportSlot imports { call with target := .external identity }).isSome = true ∧ + (callImportSlot imports { call with target := .external (identity ++ "x") }).isNone = true := by + simp only [callImportSlot] + decide +kernel + #print axioms indexed_import + """; + string path = Path.Combine(output, "IdentityScaling" + size + ".lean"); + File.WriteAllText(path, text + "\n#eval show IO Nat from IO.getNumHeartbeats\n"); + string result = Compile(path); + long heartbeats = long.Parse(result.Split('\n', StringSplitOptions.TrimEntries | + StringSplitOptions.RemoveEmptyEntries).Last(), CultureInfo.InvariantCulture); + if (previous != 0 && heartbeats > previous * 6) + throw new InvalidOperationException("Generic identity construction exceeded linear growth: " + result); + Console.WriteLine($"PASS generic identity {size} name bytes: {heartbeats} heartbeats"); + previous = heartbeats; + } + + previous = 0; + foreach (int size in new[] { 16, 64, 256 }) + { + MethodDefinition counted = new("Counted", Mono.Cecil.MethodAttributes.Public | + Mono.Cecil.MethodAttributes.Static, + module.TypeSystem.Void) { + DeclaringType = identity.DeclaringType, + CallingConvention = MethodCallingConvention.Generic }; + for (int index = 0; index < size; index++) + counted.GenericParameters.Add(new GenericParameter("T" + index, counted)); + counted.ReturnType = counted.GenericParameters[^1]; + for (int index = 0; index < size; index++) + counted.Parameters.Add(new ParameterDefinition(counted.GenericParameters[^1])); + string path = Path.Combine(output, "IdentityCountScaling" + size + ".lean"); + File.WriteAllText(path, Source(counted, Selected(counted)) + + "\n#eval show IO Nat from IO.getNumHeartbeats\n"); + string result = Compile(path); + long heartbeats = long.Parse(result.Split('\n', StringSplitOptions.TrimEntries | + StringSplitOptions.RemoveEmptyEntries).Last(), CultureInfo.InvariantCulture); + Console.WriteLine($"PASS generic identity {size} arguments and parameters: {heartbeats} heartbeats"); + if (previous != 0 && heartbeats > previous * 6) + throw new InvalidOperationException("Generic argument indexing exceeded linear growth: " + result); + previous = heartbeats; + } + + string Selected(MethodDefinition method) + { + TypeReference[] arguments = method.GenericParameters.Select((_, index) => + index == 0 ? module.TypeSystem.UInt64 : module.TypeSystem.UInt32).ToArray(); + object selection = instantiate.Invoke(null, [method, arguments, "scalar", true])!; + return (string)selection.GetType().GetProperty("Identity")!.GetValue(selection)!; + } + + static string Compile(string path) + { + string result = Run(["lake", "env", "lean", "-DElab.async=false", path]); + if (result.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("Generic identity proof contains an unproved claim: " + result); + return result; + } + + string Check(MethodDefinition method) + { + string identity = Selected(method); + string path = Path.Combine(output, "IdentityKey" + method.MetadataToken.ToUInt32() + ".lean"); + string checks = Source(method, identity) + """ + + /-- Changing any definition or instantiation component changes the contract key. -/ + theorem mutations_rejected : + ([{ site with name := bytesOf "Other" }, + { site with genericArity := site.genericArity + 1 }, + { site with convention := 0x30 }, + { site with result := .primitive 9 }, + { site with parameters := [.primitive 9] }, + { site with methodArguments := some [.primitive 9] }, + { site with methodArguments := some [] }, + { site with methodArguments := some [.pointer (.primitive 11)] }].all fun changed => + !changed.externalMatches frame identity) = true := by decide +kernel + #print axioms mutations_rejected + /-- An appended descriptor byte cannot be accepted as the same contract. -/ + theorem appended_byte_rejected : + site.externalMatches frame (identity ++ "x") = false := by decide +kernel + #print axioms appended_byte_rejected + /-- Unknown or incorrectly scoped argument kinds have no generic contract identity. -/ + theorem malformed_argument_rejected : + ([TypeKind.external none, .external (some true)].all fun kind => + !({ site with methodArguments := some [.named { + scope := bytesOf "Other", name := bytesOf "System.UInt64", + parts := [], kind := kind, pinned := false }] }).externalMatches + frame identity) = true := by decide +kernel + #print axioms malformed_argument_rejected + /-- A differently scoped owner cannot use the same imported contract identity. -/ + theorem different_owner_rejected : + (match site.owner with + | .named owner => ({ site with owner := .named { + owner with scope := bytesOf "Other" } }).externalMatches frame identity + | _ => true) = false := by decide +kernel + #print axioms different_owner_rejected + /-- Closed imported signatures cannot retain a method or owner parameter. -/ + theorem open_parameters_refused : + ({ site with result := .var 0 }).contractName frame = none ∧ + ({ site with parameters := [.byref (.var 0)] }).contractName frame = none ∧ + ({ site with result := .mvar site.genericArity }).contractName frame = none := + by decide +kernel + #print axioms open_parameters_refused + """; + if (method.Name == "Overload") + { + string otherType = method.ReturnType is GenericParameter ? ".primitive 11" : ".mvar 0"; + checks += $$""" + + /-- Equal closed signatures retain distinct open overload identities. -/ + theorem distinct_open_overload : + ({ site with result := {{otherType}}, parameters := [{{otherType}}] }).externalMatches + frame identity = false := by decide +kernel + #print axioms distinct_open_overload + """; + } + if (method.Name == "First") checks += """ + + /-- Generic arguments and open parameters retain their order. -/ + theorem ordered_components : + ({ site with methodArguments := some [.primitive 9, .primitive 11] }).externalMatches + frame identity = false ∧ + ({ site with parameters := [.mvar 1, .mvar 0] }).externalMatches + frame identity = false := by decide +kernel + #print axioms ordered_components + """; + File.WriteAllText(path, checks); + Compile(path); + return identity; + } + } + + static string Source(MethodDefinition method, string identity) + { + static string Bytes(string value) => "[" + string.Join(",", Encoding.UTF8.GetBytes(value)) + "]"; + static string Type(TypeReference type) => type is GenericParameter parameter + ? ".mvar " + parameter.Position : ".primitive " + (int)type.MetadataType; + string scope = method.Module.Assembly.Name.FullName; + string arguments = string.Join(",", method.GenericParameters.Select((_, index) => + index == 0 ? ".primitive 11" : ".primitive 9")); + string parameters = string.Join(",", method.Parameters.Select(parameter => Type(parameter.ParameterType))); + ExternalIdentityEvidence evidence = new(); + string identityReference = evidence.Reference(identity, reachable: true); + StringBuilder encoded = new(); + evidence.Emit(encoded); + return $$""" + import CIL.Extraction.BodyDecoding + open CIL CIL.Extraction + set_option maxRecDepth 100000 + set_option maxHeartbeats 10000000 + {{encoded}} + /-- The primitive scope named by the compiled fixture. -/ + def frame : Frame := { + corlib := some runtimeScopeBytes, anchor := runtimeScopeBytes, + arity := 0, ownerArity := 0 } + /-- The raw open signature and MethodSpec arguments of the compiled fixture. -/ + def site : MethodSite := { + owner := .named { + scope := {{Bytes(scope)}}, name := {{Bytes(method.DeclaringType.FullName)}}, + parts := [], kind := .external none, pinned := false }, + name := {{Bytes(method.Name)}}, convention := 0x10, + genericArity := {{method.GenericParameters.Count}}, + result := {{Type(method.ReturnType)}}, + parameters := [{{parameters}}], + definition := none, + methodArguments := some [{{arguments}}] } + /-- The identity emitted by the production C# selector. -/ + def identity : String := {{identityReference}} + /-- The kernel reconstructs the producer's entire generic contract identity. -/ + theorem exact_identity : site.externalMatches frame identity = true := by decide +kernel + #print axioms exact_identity + """; + } +} diff --git a/Tests/GenericLayoutImports.cs b/Tests/GenericLayoutImports.cs index 07563e73c..50076b589 100644 --- a/Tests/GenericLayoutImports.cs +++ b/Tests/GenericLayoutImports.cs @@ -2,8 +2,15 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs +#:include PinnedMetadata.cs +#:include GenericImportIdentities.cs +#:include ../Extractor/ExternalIdentityEvidence.cs using System.Reflection; using System.Text.Json; @@ -20,9 +27,14 @@ string[] cli = ["dotnet", ExtractorPath]; string libProject = Path.Combine(libRoot, "Lib.csproj"); string appProject = Path.Combine(appRoot, "App.csproj"); -const string libProjectText = "net10.0TypedFixture.Libtrue"; +const string libProjectText = "" + + "net11.0TypedFixture.Lib" + + "true"; File.WriteAllText(libProject, libProjectText); -File.WriteAllText(appProject, "net10.0TypedFixture.Apptrue"); +File.WriteAllText(appProject, "" + + "net11.0TypedFixture.App" + + "true" + + ""); string libSource = Path.Combine(libRoot, "Library.cs"); const string libSourceText = """ using System; @@ -41,11 +53,14 @@ public static void Copy(in Pair source, out Pair destination) destination.Head.Value = source.Head.Value; } public static T Identity(T value) where T : unmanaged => value; + public static void Ignore(T value) where T : unmanaged { } + public static T First(T left, U right) where T : unmanaged where U : unmanaged => left; public static T Overload(T value) where T : unmanaged => value; public static ulong Overload(ulong value) where T : unmanaged => value + 1; public static T Unsupported(T value) where T : class => value; } public static class Generic { public static ulong Value(ulong value) => value; } +public static class \u03A9wner { public static T \u03A9dentity(T value) where T : unmanaged => value; } """; File.WriteAllText(libSource, libSourceText); File.WriteAllText(Path.Combine(appRoot, "Application.cs"), """ @@ -98,8 +113,9 @@ public static void Unpublished(in Pair source, out Pair destination) Type methodMetadata = Assembly.LoadFrom(ExtractorPath).GetType("Nethermind.LeanCIL.ContractMethodMetadata", throwOnError: true)!; MethodInfo selectMethod = methodMetadata.GetMethod("Select", BindingFlags.Static | BindingFlags.NonPublic)!; List<(string Selector, string Definition, string Closed, string Identity)> overloads = []; -using (ModuleDefinition module = ModuleDefinition.ReadModule(lib)) +using (var pinned = new PinnedMetadata(methodMetadata.Assembly, lib)) { + var module = pinned.Module; foreach (string selector in new[] { "TypedFixture.Library::Overload(T)", "TypedFixture.Library::Overload(ulong)" }) { object selection = selectMethod.Invoke(null, [module, selector, new[] { "System.UInt64" }, "scalar"])!; @@ -128,6 +144,8 @@ object Member(object value, string name) => value.GetType().GetProperty(name, if (overloadBodies[0] == overloadBodies[1]) throw new InvalidOperationException("Generic overload regression requires distinct actual method bodies."); Console.WriteLine("PASS distinct generic definitions retain distinct closed contract identities"); +CheckGenericImportIdentities(lib, root, methodMetadata); +if (args is ["--identities-only"]) return; string revision = Run(["git", "rev-parse", "HEAD"], Root).Trim(); File.WriteAllText(Path.Combine(package, "lakefile.toml"), @@ -270,7 +288,9 @@ string InterfaceDigest(JsonObject contract) MethodReference call = module.GetType("TypedFixture.Application").Methods.Single(method => method.Name == "Forward") .Body.Instructions.Where(instruction => instruction.OpCode.Code == Code.Call).Select(instruction => (MethodReference)instruction.Operand).Single(); ByReferenceType parameter = (ByReferenceType)call.Parameters[0].ParameterType; - TypeReference modifier = new("System.Runtime.InteropServices", "InAttribute", module, new AssemblyNameReference(modifierScope, new Version(1, 0, 0, 0))); + AssemblyNameReference scope = new(modifierScope, new Version(1, 0, 0, 0)); + module.AssemblyReferences.Add(scope); + TypeReference modifier = new("System.Runtime.InteropServices", "InAttribute", module, scope); call.Parameters[0].ParameterType = new ByReferenceType(new RequiredModifierType(modifier, parameter.ElementType)); module.Write(malformed); } @@ -282,8 +302,9 @@ string InterfaceDigest(JsonObject contract) { GenericInstanceMethod call = module.GetType("TypedFixture.Application").Methods.Single(method => method.Name == "Identity") .Body.Instructions.Select(instruction => instruction.Operand).OfType().Single(); - call.GenericArguments[0] = new TypeReference("System", "UInt64", module, - new AssemblyNameReference("Other", new Version(1, 0, 0, 0))) { IsValueType = true }; + AssemblyNameReference otherScope = new("Other", new Version(1, 0, 0, 0)); + module.AssemblyReferences.Add(otherScope); + call.GenericArguments[0] = new TypeReference("System", "UInt64", module, otherScope) { IsValueType = true }; module.Write(wrongScope); } Reject("WrongScope", "exact scoped", wrongScope, "TypedFixture.Application::Identity(ulong)", sidecar, interfaceDigest); @@ -301,13 +322,35 @@ string InterfaceDigest(JsonObject contract) string artifact = Path.Combine(root, "App-" + name); Run([.. cli, "extract", app, selector, artifact, .. Import(sidecar, interfaceDigest)]); string author = Path.Combine(callerPackage, proof + ".lean"); - Run([.. cli, "verify", artifact, "--proof", author, "--theorem", proof + ".correctness", "--proof-root", callerPackage]); - Run([.. cli, "verify", artifact, "--proof", author, "--theorem", proof + ".correctness", "--proof-root", callerPackage, "--force-full-verify"]); + string[] verify = [.. cli, "verify", artifact, "--proof", author, "--theorem", proof + ".correctness", + "--proof-root", callerPackage]; + if (name == "Identity") + { + string pins = Path.Combine(root, "generic-imports.sha256"); + void CheckImportBinding(string[] images, string expected, string? theorem, params string[] options) + { + File.WriteAllLines(pins, images.Select(image => Sha(image) + " " + image)); + Run([.. verify, "--deep-checks", pins, .. options]); + using var certificate = JsonDocument.Parse(File.ReadAllText(Path.Combine(artifact, "verified.json"))); + var fact = certificate.RootElement.GetProperty("binding").GetProperty("facts").EnumerateArray() + .Single(entry => entry.GetProperty("id").GetString() == "import-identities"); + if (fact.GetProperty("status").GetString() != expected || fact.GetProperty("theorem").GetString() != theorem) + throw new InvalidOperationException("Incorrect physical import evidence: " + fact); + } + CheckImportBinding([app], "not covered", null); + CheckImportBinding([app, lib], "reconfirmed", "BindingAudit.genericImports", "--force-full-verify"); + } + else + { + Run(verify); + Run([.. verify, "--force-full-verify"]); + } JsonElement report = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(artifact, "verified.json"))); JsonElement imported = report.GetProperty("imports")[0]; if (imported.GetProperty("acceptedVia").GetString() != "independently-verified" || imported.GetProperty("implementationSha256").GetString() != Sha(lib) || !imported.TryGetProperty("typedSignature", out _)) throw new InvalidOperationException("Full verification omitted typed import or exact implementation evidence."); + Console.WriteLine("PASS typed imported " + name + ": actual SDK DLL, exact contract and independent linked proof"); } Console.WriteLine("PASS typed layout metadata, closed instances, modifier rejection and canonical sidecar digest"); diff --git a/Tests/GenericLayouts.cs b/Tests/GenericLayouts.cs index 8773ad81d..2c3c39418 100644 --- a/Tests/GenericLayouts.cs +++ b/Tests/GenericLayouts.cs @@ -2,9 +2,15 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs +#:include PinnedMetadata.cs using System.Reflection; using Mono.Cecil; @@ -14,14 +20,87 @@ var root = At("artifacts/GenericLayouts"); Directory.CreateDirectory(root); var extractor = Assembly.LoadFrom(ExtractorPath); -using (var module = ModuleDefinition.ReadModule(assembly)) +if (!LayoutVisits() || args.Contains("--layout-visits-only", StringComparer.Ordinal)) return; + +bool LayoutVisits() +{ + const BindingFlags members = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; + using var pinned = new PinnedMetadata(extractor, assembly); + var module = pinned.Module; + var box = module.GetType("Examples.IndirectValues/Box`1"); + var method = new MethodDefinition("LayoutVisits", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); + module.GetType("Examples.IndirectValues").Methods.Add(method); + var validation = extractor.GetType("Nethermind.LeanCIL.MetadataValidation", true)!; + var context = validation.GetNestedType("LayoutContext", members)!; + var emitter = extractor.GetType("Nethermind.LeanCIL.LeanEmitter", true)!; + var collect = emitter.GetMethod("LayoutTypes", members)!; + var signatures = pinned.Context.GetType().GetProperty("Signatures", members)!.GetValue(pinned.Context)!; + var visits = signatures.GetType().GetProperty("NodeVisits", members)!; + long Count() => (long)visits.GetValue(signatures)!; + bool passed = true; + foreach (int depth in new[] { 8, 16, 32 }) + { + TypeReference nested = module.TypeSystem.UInt64; + for (int index = 0; index < depth; index++) + { + var instance = new GenericInstanceType(box); + instance.GenericArguments.Add(nested); + nested = instance; + } + method.Body.Variables.Clear(); + method.Body.Variables.Add(new VariableDefinition(nested)); + var layouts = Activator.CreateInstance(context, members | BindingFlags.Public, null, [module, "scalar"], null); + long before = Count(); + var rows = (TypeReference[])collect.Invoke(null, [new[] { method }, layouts])!; + long observed = Count() - before; + // Each application, the shared Box constructor and the UInt64 leaf are distinct nodes. + int distinct = depth + 2; + Console.WriteLine($"Layout depth {depth}: {rows.Length} rows, {observed} visits, {distinct} distinct nodes"); + passed &= rows.Length == depth && observed == distinct; + } + if (!passed) Environment.ExitCode = 1; + return passed; +} + +// Identities are read from metadata rows, so the same-named definition is written into an image. +var shadowImage = Path.Combine(root, "Shadowed.dll"); +using (var source = ModuleDefinition.ReadModule(assembly)) { - var owner = module.GetType("Examples.GenericPair`1"); var shadow = new TypeDefinition("System", "UInt64", Mono.Cecil.TypeAttributes.Public | - Mono.Cecil.TypeAttributes.Sealed | Mono.Cecil.TypeAttributes.SequentialLayout, owner.BaseType); - shadow.Fields.Add(new FieldDefinition("First", Mono.Cecil.FieldAttributes.Public, module.TypeSystem.UInt64)); - shadow.Fields.Add(new FieldDefinition("Second", Mono.Cecil.FieldAttributes.Public, module.TypeSystem.UInt64)); - module.Types.Add(shadow); + Mono.Cecil.TypeAttributes.Sealed | Mono.Cecil.TypeAttributes.SequentialLayout, + source.GetType("Examples.GenericPair`1").BaseType); + shadow.Fields.Add(new FieldDefinition("First", Mono.Cecil.FieldAttributes.Public, source.TypeSystem.UInt64)); + shadow.Fields.Add(new FieldDefinition("Second", Mono.Cecil.FieldAttributes.Public, source.TypeSystem.UInt64)); + source.Types.Add(shadow); + var pair = source.GetType("Examples.GenericPair`1"); + var closed = new GenericInstanceType(pair); + var word = source.TypeSystem.UInt64; + closed.GenericArguments.Add(word); + var scope = ((AssemblyNameReference)word.Scope).FullName; + var literal = new TypeDefinition(pair.Namespace, $"{pair.Name}<{scope}::{word.FullName}>", + pair.Attributes, pair.BaseType); + foreach (var field in pair.Fields.Where(field => !field.IsStatic)) + literal.Fields.Add(new FieldDefinition(field.Name, field.Attributes, word)); + source.Types.Add(literal); + // Both locals have the same physical layout. Their metadata identities must still be distinguishable. + var method = new MethodDefinition("AmbiguousLocals", Mono.Cecil.MethodAttributes.Static, source.TypeSystem.Void); + source.GetType("Examples.GenericLayouts").Methods.Add(method); + method.Body.Variables.Add(new VariableDefinition(closed)); + method.Body.Variables.Add(new VariableDefinition(literal)); + method.Body.GetILProcessor().Emit(OpCodes.Ret); + source.Write(shadowImage); +} +var ambiguous = Execute(["dotnet", ExtractorPath, "extract", shadowImage, "GenericLayouts::AmbiguousLocals()", + Path.Combine(root, "AmbiguousLocals")]); +if (ambiguous.Code == 0 || !ambiguous.Output.Contains("Ambiguous layout identity:", StringComparison.Ordinal)) + throw new InvalidOperationException($"Expected a refusal for the ambiguous layout spelling: {ambiguous.Output}"); +Console.WriteLine("PASS extraction refuses distinct layout identities with the same spelling"); +using (var pinned = new PinnedMetadata(extractor, shadowImage)) +{ + var module = pinned.Module; + var owner = module.GetType("Examples.GenericPair`1"); + var shadow = module.GetType("System.UInt64") ?? + throw new InvalidOperationException("Missing same-named definition in the written image"); var genuine = new GenericInstanceType(owner); genuine.GenericArguments.Add(module.TypeSystem.UInt64); var shadowed = new GenericInstanceType(owner); @@ -71,7 +150,7 @@ theorem generic_size_{{name}} : (CIL.Safety.invoke Extracted.program 8 0 [] { allocations := fun _ => none, cells := fun _ _ => ⟨0, false⟩, - views := [], nextIdentity := 0 }).map Prod.snd = .ok [.scalar (.i32 32)] := rfl + views := [], nextIdentity := 0 }).map Prod.snd = .ok [.scalar (.i32 32)] := by rfl #print axioms generic_size_{{name}} """; } @@ -173,13 +252,13 @@ .ok [.scalar (.i64 {{expected}})] := rfl theorem interface_owner_size : (CIL.Safety.invoke Extracted.program 8 0 [] { allocations := fun _ => none, cells := fun _ _ => ⟨0, false⟩, - views := [], nextIdentity := 0 }).map Prod.snd = .ok [.scalar (.i32 {{expected}})] := rfl + views := [], nextIdentity := 0 }).map Prod.snd = .ok [.scalar (.i32 {{expected}})] := by rfl #print axioms interface_owner_size """); evidence.Add(path, "PASS generic owner with direct interface constraint: " + name, "layouts_validated"); var invalid = Path.Combine(root, name + "-missing-interface.lean"); // Same-assembly generic interface identities spell each argument with its assembly scope. - const string runtimeScope = "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"; + const string runtimeScope = "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"; var marker = closedInterface ? $"::Examples.IClosedCounter`1<{runtimeScope}::System.Int32>" : "::Examples.ICounterValue"; marker = System.Text.Json.JsonSerializer.Serialize(marker)[1..^1]; diff --git a/Tests/GenericMethods.cs b/Tests/GenericMethods.cs index 494ab3e6e..110849d51 100644 --- a/Tests/GenericMethods.cs +++ b/Tests/GenericMethods.cs @@ -2,6 +2,13 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include EmptyReturnVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -10,6 +17,7 @@ using Mono.Cecil.Cil; using var verification = new Program(); +Run(["dotnet", "run", "--file", At("Tests/StructuralSignatures.cs")]); var assembly = At("Examples/Assembly/bin/Examples.dll"); var fsharp = At("Examples/FSharp/bin/Examples.FSharp.dll"); var root = At("artifacts/GenericMethods"); @@ -151,18 +159,11 @@ object[] Values(Type type) => type == typeof(bool) ? [false, true] : } var wrong = Path.Combine(root, "WrongIdentity"); -var rejected = false; -try -{ - verification.Verify(assembly, "Examples", "GenericMethods::WrongIdentity(int)", - At("Examples/Proofs/GenericIdentity32.lean"), wrong); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - rejected = true; -} -if (!rejected || File.Exists(Path.Combine(wrong, "verified.json"))) - throw new InvalidOperationException("Wrong generic implementation accepted"); +verification.VerifyEmptyReturn(assembly, "GenericMethods::Identity32(int)", + At("Examples/Proofs/GenericIdentity32.lean"), assembly, "GenericMethods::WrongIdentity(int)", wrong, + new("W32", "[.scalar (.i32 input)]", + ".scalar (.i32 input)", 16, + "exact ExampleProof.correctness input", "0")); // A closed primitive-array local now specializes and executes through its typed root slot. var arrayLocal = Path.Combine(root, "ClosedArrayLocal"); diff --git a/Tests/GenericOwnerScaling.lean b/Tests/GenericOwnerScaling.lean new file mode 100644 index 000000000..df2be7adb --- /dev/null +++ b/Tests/GenericOwnerScaling.lean @@ -0,0 +1,76 @@ +import Tests.MetadataBuilder +import Lean + +/-! Scaling checks for metadata generic owner indexing and owner lookups, for owners with a + parameter and for owners shown by their neighbours to have none. -/ + +namespace Tests.GenericOwnerScaling + +open CIL.Extraction Tests.MetadataBuilder + +set_option maxHeartbeats 0 +set_option maxRecDepth 32768 + +/-- An assembly with one carried parameter for each of `count` distinct owners. -/ +private def metadata (count : Nat) : MetadataSlice := + let empty := (build (pure ()) (bytesOf "T") + "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null").1 + let counts := [(Table.assembly, 1), (Table.genericParam, count)] + { empty with + assembly := { empty.assembly with + header := TableHeader.ofCounts 0 counts + tableHeader := tableHeader counts } + rows := (List.range count).map fun position => + (tokenOf Table.genericParam (position + 1), + encodeRow Table.genericParam [0, 0, (position + 1) * 2, 0]) } + +/-- Build the shared tables once and read every owner's complete parameter run. -/ +private def checksOwners (count : Nat) : Bool := + match (do + let metadata := metadata count + let index ← metadata.indexWith (← metadata.tables) [] + (List.range count).allM fun position => do + let rows ← index.genericParameters 0 (tokenOf Table.typeDef (position + 1)) + pure (rows.length == 1)) with + | .ok accepted => accepted + | .error _ => false + +/-- Build the shared tables once and read that no parameter names each of `count` methods, + each bracketed by the parameters of two adjacent types. -/ +private def checksAbsentOwners (count : Nat) : Bool := + match (do + let metadata := metadata count + let index ← metadata.indexWith (← metadata.tables) [] + (List.range count).allM fun position => do + let rows ← index.genericParameters 0 (tokenOf Table.methodDef (position + 1)) + pure rows.isEmpty) with + | .ok accepted => accepted + | .error _ => false + +/-- The kernel checks timed, each with the label its report uses. -/ +private def timedChecks : List (String × Lean.Name) := + [("Generic owner", ``checksOwners), ("Absent owner", ``checksAbsentOwners)] + +open Lean Meta Elab Command in +run_cmd do + let allowedFourfoldGrowth := 6 + for (label, check) in timedChecks do + let mut previous := 0 + for count in [128, 512, 2048] do + let (statement, proof) ← liftTermElabM do + let statement ← mkEq (mkApp (mkConst check) (mkNatLit count)) + (mkConst ``Bool.true) + let proof ← mkEqRefl (mkConst ``Bool.true) + pure (statement, proof) + let start ← IO.getNumHeartbeats + let checked := Expr.letE `checked statement proof (.bvar 0) false + match Kernel.check (← getEnv) {} checked with + | .error error => throwKernelException error + | .ok _ => pure () + let cost := (← IO.getNumHeartbeats) - start + if previous != 0 && cost > previous * allowedFourfoldGrowth then + throwError "{label} cost grew from {previous} to {cost} for four times the owners" + logInfo m!"{label} checks: {count} owners, {cost} heartbeats" + previous := cost + +end Tests.GenericOwnerScaling diff --git a/Tests/GenericOwners.cs b/Tests/GenericOwners.cs index 43dd8d0b9..e487c3e88 100644 --- a/Tests/GenericOwners.cs +++ b/Tests/GenericOwners.cs @@ -2,9 +2,15 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs +#:include PinnedMetadata.cs using System.Reflection; using System.Text.RegularExpressions; @@ -15,8 +21,9 @@ var root = At("artifacts/GenericOwners"); Directory.CreateDirectory(root); // Foreign owners cannot be serialized as distinct CLI slots; check the in-memory metadata boundary. -using (var module = ModuleDefinition.ReadModule(assembly)) +using (var pinned = new PinnedMetadata(Assembly.LoadFrom(ExtractorPath), assembly)) { + var module = pinned.Module; var caller = module.Types.Single(type => type.Name == "GenericOwners").Methods .Single(method => method.Name == "Identity"); var target = (MethodReference)caller.Body.Instructions.Single(instruction => @@ -61,7 +68,7 @@ string ChangeDefinition(string source, string name, Func change) var output = Path.Combine(root, name); Run(["dotnet", ExtractorPath, "extract", assembly, $"GenericOwners::{name}(uint)", output]); var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); - if (Regex.Matches(source, @"(?m)^def method\d+ : Method :=").Count != 2) + if (DefinedMethods(source).Length != 2) throw new InvalidOperationException("Closed specialization was duplicated: " + name); var proof = source + $$""" @@ -69,7 +76,8 @@ string ChangeDefinition(string source, string name, Func change) theorem generic_owner_result : (CIL.Safety.invoke Extracted.program 64 0 [.scalar (.i32 42)] { allocations := fun _ => none, cells := fun _ _ => ⟨0, false⟩, - views := [], nextIdentity := 0 }).map Prod.snd = .ok [.scalar (.i32 {{expected}})] := rfl + views := [], nextIdentity := 0 }).map Prod.snd = .ok [.scalar (.i32 {{expected}})] := + by cil_kernel_rfl #print axioms generic_owner_result """; if (name == "Combined") @@ -231,14 +239,16 @@ string Forge(string source, string line, string text, string from, string to) var tablePath = Path.Combine(root, "TableFirst.lean"); File.WriteAllText(tablePath, tableSource + $$""" + open CIL in /-- The entry `TableFirst` calls the catalogued `OwnerHelper.Identity`. -/ theorem generic_table_entry : (CIL.Safety.invoke Extracted.program 64 0 [.methodTable ⟨⟨.atom CIL.Extraction.runtimeScope "System.UInt32", - [.atom CIL.Extraction.runtimeScope "System.UInt32"]⟩, some #[{{target.Groups[1].Value}}], none⟩, + [.atom CIL.Extraction.runtimeScope "System.UInt32"]⟩, some #[{{target.Groups[1].Value}}], none, 1, + by decide⟩, .scalar (.i32 42)] { allocations := fun _ => none, cells := fun _ _ => ⟨0, false⟩, - views := [], nextIdentity := 0 }).map Prod.snd = .ok [.scalar (.i32 42)] := by rfl + views := [], nextIdentity := 0 }).map Prod.snd = .ok [.scalar (.i32 42)] := by cil_kernel_rfl #print axioms generic_table_entry """); evidence.Add(tablePath, "PASS generic entry over a supplied table: TableFirst"); diff --git a/Tests/Guards.cs b/Tests/Guards.cs index f9784c286..f98d56632 100644 --- a/Tests/Guards.cs +++ b/Tests/Guards.cs @@ -1,42 +1,81 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:package Mono.Cecil +#:include ContractVariants.cs #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using Mono.Cecil; using Mono.Cecil.Cil; -// Framework argument guards as checked runtime contracts: proofs that each raises its exception exactly when its .NET -// condition holds, a proof that fails once the guard is swapped, and named rejection of uncatalogued guard overloads. +// Check normal and exceptional outcomes for integer and null guards. The swapped guard has a certified +// counterexample, and unsupported overloads must fail extraction with the expected member identity. using var verification = new Program(); var assembly = At("Examples/Assembly/bin/Examples.dll"); var root = At("artifacts/Guards"); Directory.CreateDirectory(root); -foreach (var (proof, entry) in new[] +foreach (var (source, entry) in new[] { - ("GuardCount", "Guards::Count(System.Int32)"), - ("GuardIndex", "Guards::Index(System.UInt32,System.UInt32)"), - ("GuardRequired", "Guards::Required(System.String)") + ("Examples/Proofs/GuardIndex.lean", "Guards::Index(System.UInt32,System.UInt32)"), + ("Examples/Proofs/GuardRequired.lean", "Guards::Required(System.String)") }) { - verification.Verify(assembly, "Examples", entry, At($"Examples/Proofs/{proof}.lean"), Path.Combine(root, proof)); + var proof = Path.GetFileNameWithoutExtension(source); + verification.Verify(assembly, "Examples", entry, At(source), Path.Combine(root, proof)); Console.WriteLine($"PASS {proof}: {entry}"); } -// The guard method decides the proof: with ThrowIfNegativeOrZero, zero raises and GuardCount no longer holds. +// Zero returns under ThrowIfNegative and raises ArgumentOutOfRangeException under ThrowIfNegativeOrZero. var swapped = Mutate("swapped", "Count", (module, call) => Guard(module, call, "ThrowIfNegativeOrZero", module.TypeSystem.Int32)); -var refuted = Execute(["dotnet", ExtractorPath, "extract", swapped, "Examples.Guards::Count(System.Int32)", - Path.Combine(root, "Reject-swapped")]); -if (refuted.Code != 0) - throw new InvalidOperationException("The swapped guard should extract\n" + refuted.Output); -var checkedSwap = Execute(["dotnet", ExtractorPath, "verify", Path.Combine(root, "Reject-swapped"), "--proof", - At("Examples/Proofs/GuardCount.lean")]); -if (checkedSwap.Code == 0 || - !checkedSwap.Output.Contains("Lean could not prove the selected theorem", StringComparison.Ordinal)) - throw new InvalidOperationException("GuardCount must fail for ThrowIfNegativeOrZero\n" + checkedSwap.Output); -Console.WriteLine("PASS swapped guard refutes GuardCount"); +const string countSelector = "Guards::Count(System.Int32)"; +const string countContract = """ + /-- Negative counts raise, and a nonnegative count returns unchanged in the caller's memory. -/ + def contract : OutcomeContract W32 where + args count := [.scalar (.i32 count)] + pre _ memory := ∃ literal, memory.literals.lookup "count" = some literal ∧ + formObject memory (.address literal) = .ok (.object (.address literal)) + footprint _ := [] + post count before after values := after = before ∧ values = [.scalar (.i32 count)] + raises count _ := if count.toInt < 0 then some .argumentOutOfRange else none + """; +const string positive = """ + /-- The compiled guard satisfies both branches for every count and bound parameter name. -/ + theorem correctness : contract.HoldsCLR Extracted.program 0 := by + rintro count memory ⟨literal, bound, formed⟩ + obtain ⟨fuel, ran⟩ := ExampleProof.correctness memory literal count bound formed + by_cases negative : count.toInt < 0 + · simp only [negative, ite_true] at ran + simpa only [contract, negative, ite_true] using + (RaisesCLR.of_invoke ran (exception := .argumentOutOfRange) rfl) + · simp only [negative, ite_false] at ran + simp only [contract, negative, ite_false] + exact ⟨fuel, _, _, ran, Confined.refl _ _, _, rfl, rfl, rfl⟩ + """; +const string counterexample = """ + /-- The caller binds the parameter name to a live string object at allocation zero. -/ + private def literalMemory : Memory := { Memory.empty with + allocations := fun id => if id = 0 then some + ⟨.object ⟨.object CIL.Extraction.runtimeScope "System.String", []⟩, ⟨1, 1, []⟩, true, []⟩ + else none + nextIdentity := 1 + literals := [("count", ⟨0, 0⟩)] } + + /-- The substituted guard throws at pc 2 on zero, which the contract requires to return. -/ + theorem correctness : contract.RefutedCLR Extracted.program 0 := + .of_fault (input := 0) (initial := literalMemory) ⟨⟨0, 0⟩, rfl, rfl⟩ (by decide) + (rfl : invoke Extracted.program 8 0 (contract.args 0) literalMemory = + .error ⟨0, 2, .thrown .argumentOutOfRange⟩) trivial + """; +verification.VerifyContractVariant(assembly, "Examples", countSelector, Path.Combine(root, "GuardCount"), + swapped, countSelector, Path.Combine(root, "Reject-swapped"), countContract, positive, counterexample, + example: At("Examples/Proofs/GuardCount.lean"), imports: "import CIL.Safety.RuntimeExceptions"); +Console.WriteLine("PASS GuardCount returns zero and its replacement is certified to throw at the guard"); // Floating-point and pointer guards are not modeled: NaN and pointer comparison are outside the integer contract. foreach (var (name, method, rewrite, expected) in diff --git a/Tests/HalfSwapContract.lean b/Tests/HalfSwapContract.lean new file mode 100644 index 000000000..21e198566 --- /dev/null +++ b/Tests/HalfSwapContract.lean @@ -0,0 +1,135 @@ +import CIL.Safety.MethodContract +import CIL.Safety.ValueBitCasts + +/-! Caller memory for the in-place `DivisionIntrinsics.SwapHalves` contract. Both spans +start at the same address. The vector local occupies a separate frame-owned allocation. -/ + +namespace Tests.HalfSwapContract +open CIL.Safety + +/-- A live caller allocation grants read and write access to its 32 initialized bytes. -/ +def memory (data : Nat → BitVec 8) : Memory := { Memory.empty with + allocations := fun id => if id == 0 then some ⟨.callerStack, ⟨32, 1, []⟩, true, [32]⟩ else none + cells := fun _ offset => ⟨data offset, true⟩ + views := [⟨0, 0, 32, true, true⟩] + nextIdentity := 1 } + +/-- Swapping in place rotates the 32 caller bytes by 16 and returns no value. -/ +def swap : MethodContract (Nat → BitVec 8) Unit where + args _ := [.bufferSpan ⟨.address ⟨0, 0⟩, 32, 1, false⟩, .bufferSpan ⟨.address ⟨0, 0⟩, 32, 1, true⟩] + results _ := [] + footprint _ := [(⟨0, 0⟩, 32)] + pre data before := before = memory data + post data _ after _ := ∀ i, i < 32 → (after.cells 0 i).bits = data ((i + 16) % 32) + +/-- Sixteen input bytes in increasing address order. -/ +def bytes (data : Nat → BitVec 8) (start : Nat) : List (BitVec 8) := + (List.range 16).map fun i => data (start + i) + +/-- The little-endian vector value of one input half. -/ +def packed (data : Nat → BitVec 8) (start : Nat) := + BitVec.ofNat 128 (byteNumber (bytes data start)) + +/-- Storing the vector writes its sixteen source bytes. -/ +theorem unpack (data : Nat → BitVec 8) (start : Nat) : + numberBytes (packed data start).toNat 16 = bytes data start := + vector128_bytes_roundtrip _ (by simp [bytes]) +#print axioms unpack + +/-- After the destination reference is saved, the vector local owns allocation one. -/ +def saved : Frame := ⟨1, [.root (some (.address ⟨0, 0⟩)), .bytes .vector128 ⟨1, 0⟩], [1], 0⟩ + +/-- Contents after a checked write into an already readable window. -/ +def put (m : Memory) (id offset : Nat) (block : List (BitVec 8)) : Memory := + { m with cells := fun target i => + if target = id ∧ offset ≤ i then + match block[i - offset]? with + | some bits => ⟨bits, true⟩ + | none => m.cells target i + else m.cells target i } + +/-- Frame entry initializes the private vector home to zero. -/ +def entered (data : Nat → BitVec 8) : Memory := + let m := memory data + let fresh := ((allocateHome m 1 16 1).toOption.getD (⟨1, 0⟩, m)).2 + put fresh 1 0 (numberBytes 0 16) + +/-- The private home holds the lower half before the first caller write. -/ +def savedLow (data : Nat → BitVec 8) := put (entered data) 1 0 (bytes data 0) +/-- The low half holds the original high half. -/ +def highWritten (data : Nat → BitVec 8) := put (savedLow data) 0 0 (bytes data 16) +/-- The high half holds the saved low half. -/ +def lowWritten (data : Nat → BitVec 8) := put (highWritten data) 0 16 (bytes data 0) + +private theorem highWrite (data : Nat → BitVec 8) : + write (savedLow data) ⟨0, 0⟩ (bytes data 16) 1 = .ok (highWritten data) := by with_unfolding_all rfl +#print axioms highWrite + +private theorem lowWrite (data : Nat → BitVec 8) : + write (highWritten data) ⟨0, 16⟩ (bytes data 0) 1 = .ok (lowWritten data) := by with_unfolding_all rfl +#print axioms lowWrite + +/-- Retiring the frame preserves caller allocations, permissions and bytes outside the span. -/ +theorem framed (data : Nat → BitVec 8) : + Confined [(⟨0, 0⟩, 32)] (memory data) (expire (lowWritten data) 1) := by + constructor + · change 1 ≤ 2 + decide + all_goals + intro id old + have h : id = 0 := Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ old) + subst id + · with_unfolding_all rfl + · intro offset outside + have endBound : 32 ≤ offset := by simpa [Outside] using outside + change (lowWritten data).cells 0 offset = _ + rw [write_outside _ _ _ _ _ _ _ (lowWrite data) (Or.inr (Or.inr (by simpa [bytes] using endBound))), + write_outside _ _ _ _ _ _ _ (highWrite data) (Or.inr (Or.inr (by simp [bytes]; omega)))] + with_unfolding_all rfl + · intro offset + with_unfolding_all rfl + · intro offset _ + with_unfolding_all rfl + · intro offset _ + with_unfolding_all rfl +#print axioms framed + +/-- Each output byte comes from the opposite half of the original input. -/ +theorem swapped (data : Nat → BitVec 8) (i : Nat) (bound : i < 32) : + ((expire (lowWritten data) 1).cells 0 i).bits = data ((i + 16) % 32) := by + change ((lowWritten data).cells 0 i).bits = _ + by_cases low : i < 16 + · rw [write_outside _ _ _ _ _ _ _ (lowWrite data) (Or.inr (Or.inl low))] + have inside : i < (bytes data 16).length := by simpa [bytes] using low + have same := write_initializes _ _ _ _ _ i (highWrite data) inside + simpa [bytes, Nat.add_comm, Nat.mod_eq_of_lt (show i + 16 < 32 by omega)] using congrArg Cell.bits same + · have inside : i - 16 < (bytes data 0).length := by simp [bytes]; omega + have same := write_initializes _ _ _ _ _ (i - 16) (lowWrite data) inside + have idx : 16 + (i - 16) = i := by omega + have rotated : (i + 16) % 32 = i - 16 := by omega + simpa [bytes, idx, rotated] using congrArg Cell.bits same +#print axioms swapped + +/-- Only the first byte of the high half is set. -/ +def witness (offset : Nat) : BitVec 8 := if offset == 16 then 1 else 0 + +/-- A successful return leaving zero at byte zero violates the swap on this input. -/ +theorem refuted_of_first_byte {program : CIL.Program} + (observed : (invoke program 64 0 (swap.args witness) (memory witness)).map + (fun r => (r.2, (r.1.cells 0 0).bits)) = .ok ([], 0)) : swap.Refuted program 0 := by + cases h : invoke program 64 0 (swap.args witness) (memory witness) with + | error error => rw [h] at observed; cases observed + | ok pair => + obtain ⟨after, values⟩ := pair + rw [h] at observed + have eq : (values, (after.cells 0 0).bits) = (([] : List Value), (0 : BitVec 8)) := + Except.ok.inj observed + apply MethodContract.Refuted.of_return (c := swap) (input := witness) rfl h + rintro ⟨_, _, _, post⟩ + have expected : (after.cells 0 0).bits = 1 := post 0 (by decide) + have first : (after.cells 0 0).bits = 0 := congrArg Prod.snd eq + rw [first] at expected + contradiction +#print axioms refuted_of_first_byte + +end Tests.HalfSwapContract diff --git a/Tests/HalfSwapCost.lean.in b/Tests/HalfSwapCost.lean.in new file mode 100644 index 000000000..e78d9f13c --- /dev/null +++ b/Tests/HalfSwapCost.lean.in @@ -0,0 +1,20 @@ +open CIL.Safety Tests.HalfSwapContract +namespace VariantProof + +def witnessByte : Prop := ∃ fuel, + (invoke Extracted.program fuel 0 (swap.args witness) (memory witness)).map + (fun result => (result.1.cells 0 0).bits) = .ok 1 + +theorem directWitnessByte : witnessByte := ⟨19, by with_unfolding_all rfl⟩ +#print axioms directWitnessByte + +theorem sharedWitnessByte : witnessByte := by + obtain ⟨fuel, ⟨after, values⟩, ran, _, _, _, post⟩ := + correctness witness (memory witness) rfl + have first : (after.cells 0 0).bits = 1 := post 0 (by decide) + refine ⟨fuel, ?_⟩ + change invoke Extracted.program fuel 0 (swap.args witness) (memory witness) = .ok (after, values) at ran + rw [ran] + exact congrArg Except.ok first +#print axioms sharedWitnessByte +end VariantProof diff --git a/Tests/HalfSwapOverlap.lean.in b/Tests/HalfSwapOverlap.lean.in new file mode 100644 index 000000000..59424796a --- /dev/null +++ b/Tests/HalfSwapOverlap.lean.in @@ -0,0 +1,139 @@ +import Extracted +import Tests.HalfSwapContract +open CIL.Safety +open Tests.HalfSwapContract (bytes packed unpack put) +/-! The source occupies bytes 24 through 55 of a 96-byte caller allocation. The eight +placements use the same source and destination slices as the compiled CLR comparisons. -/ + +namespace HalfSwapOverlap + +inductive Placement where + | left16 | left8 | left1 | same | right1 | right8 | right16 | separate + +/-- Destination byte offset in the caller allocation. -/ +def dest : Placement → Nat + | .left16 => 8 + | .left8 => 16 + | .left1 => 23 + | .same => 24 + | .right1 => 25 + | .right8 => 32 + | .right16 => 40 + | .separate => 56 + +def memory (data : Nat → BitVec 8) : Memory := { Memory.empty with + allocations := fun id => if id == 0 then some ⟨.callerStack, ⟨96, 1, []⟩, true, [96]⟩ else none + cells := fun _ offset => ⟨data offset, true⟩ + views := [⟨0, 0, 96, true, true⟩] + nextIdentity := 1 } + +def args (p : Placement) : List Value := + [.bufferSpan ⟨.address ⟨0, 24⟩, 32, 1, false⟩, .bufferSpan ⟨.address ⟨0, dest p⟩, 32, 1, true⟩] + +def saved (p : Placement) : Frame := + ⟨1, [.root (some (.address ⟨0, dest p⟩)), .bytes .vector128 ⟨1, 0⟩], [1], 0⟩ + +def entered (data : Nat → BitVec 8) : Memory := + let m := memory data + let fresh := ((allocateHome m 1 16 1).toOption.getD (⟨1, 0⟩, m)).2 + put fresh 1 0 (numberBytes 0 16) + +/-- Both vector loads read the original source bytes before either destination store. -/ +def savedLow (data : Nat → BitVec 8) := put (entered data) 1 0 (bytes data 24) +def highWritten (p : Placement) (data : Nat → BitVec 8) := put (savedLow data) 0 (dest p) (bytes data 40) +def lowWritten (p : Placement) (data : Nat → BitVec 8) := put (highWritten p data) 0 (dest p + 16) (bytes data 24) + +private theorem firstLoad (p : Placement) (data : Nat → BitVec 8) (fuel : Nat) : + invoke Extracted.program (fuel + 7) 0 (args p) (memory data) = + run Extracted.program fuel 0 7 (args p) (saved p) + [.scalar (.v128 (packed data 24)), .readOnlyReference (.address ⟨0, 24⟩)] (entered data) := by + cases p <;> with_unfolding_all rfl + +private theorem saveAndLoad (p : Placement) (data : Nat → BitVec 8) (fuel : Nat) : + run Extracted.program (fuel + 4) 0 7 (args p) (saved p) + [.scalar (.v128 (packed data 24)), .readOnlyReference (.address ⟨0, 24⟩)] (entered data) = + run Extracted.program fuel 0 11 (args p) (saved p) + [.scalar (.v128 (packed data 40))] (savedLow data) := by + unfold savedLow + rw [← unpack data 24] + cases p <;> with_unfolding_all rfl + +private theorem firstStore (p : Placement) (data : Nat → BitVec 8) (fuel : Nat) : + run Extracted.program (fuel + 2) 0 11 (args p) (saved p) + [.scalar (.v128 (packed data 40))] (savedLow data) = + run Extracted.program fuel 0 13 (args p) (saved p) [] (highWritten p data) := by + unfold highWritten + rw [← unpack data 40] + cases p <;> with_unfolding_all rfl + +private theorem secondStore (p : Placement) (data : Nat → BitVec 8) : + run Extracted.program 6 0 13 (args p) (saved p) [] (highWritten p data) = + .ok (expire (lowWritten p data) 1, []) := by + unfold lowWritten + rw [← unpack data 24] + cases p <;> with_unfolding_all rfl + +theorem ran (p : Placement) (data : Nat → BitVec 8) : + invoke Extracted.program 19 0 (args p) (memory data) = .ok (expire (lowWritten p data) 1, []) := + (firstLoad p data 12).trans ((saveAndLoad p data 8).trans ((firstStore p data 6).trans (secondStore p data))) +#print axioms ran + +/-- The method exchanges the two original halves and preserves caller storage outside the destination. -/ +def contract : MethodContract (Placement × (Nat → BitVec 8)) Unit where + args input := args input.1 + results _ := [] + footprint input := [(⟨0, dest input.1⟩, 32)] + pre input before := before = memory input.2 + post input _ after _ := ∀ i, i < 32 → + (after.cells 0 (dest input.1 + i)).bits = input.2 (24 + (i + 16) % 32) + +private theorem writeHigh (p : Placement) (data : Nat → BitVec 8) : + write (savedLow data) ⟨0, dest p⟩ (bytes data 40) 1 = .ok (highWritten p data) := by + cases p <;> with_unfolding_all rfl + +private theorem writeLow (p : Placement) (data : Nat → BitVec 8) : + write (highWritten p data) ⟨0, dest p + 16⟩ (bytes data 24) 1 = .ok (lowWritten p data) := by + cases p <;> with_unfolding_all rfl + +private theorem confined (p : Placement) (data : Nat → BitVec 8) : + Confined [(⟨0, dest p⟩, 32)] (memory data) (expire (lowWritten p data) 1) := by + constructor + · change 1 ≤ 2 + decide + all_goals + intro id old + have h : id = 0 := Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ old) + subst id + · with_unfolding_all rfl + · intro offset outside + have bound : offset < dest p ∨ dest p + 32 ≤ offset := by simpa [Outside] using outside + change (lowWritten p data).cells 0 offset = _ + rw [write_outside _ _ _ _ _ _ _ (writeLow p data) (Or.inr (by simp [bytes]; omega)), + write_outside _ _ _ _ _ _ _ (writeHigh p data) (Or.inr (by simp [bytes]; omega))] + with_unfolding_all rfl + · intro offset + with_unfolding_all rfl + · intro offset _ + with_unfolding_all rfl + · intro offset _ + with_unfolding_all rfl + +private theorem outputs (p : Placement) (data : Nat → BitVec 8) (i : Nat) (bound : i < 32) : + ((expire (lowWritten p data) 1).cells 0 (dest p + i)).bits = data (24 + (i + 16) % 32) := by + change ((lowWritten p data).cells 0 (dest p + i)).bits = _ + by_cases low : i < 16 + · rw [write_outside _ _ _ _ _ _ _ (writeLow p data) (Or.inr (Or.inl (by dsimp; omega)))] + have inside : i < (bytes data 40).length := by simpa [bytes] using low + have eq := congrArg Cell.bits (write_initializes _ _ _ _ _ i (writeHigh p data) inside) + simpa [bytes, Nat.mod_eq_of_lt (show i + 16 < 32 by omega), Nat.add_comm, Nat.add_assoc, Nat.add_left_comm] using eq + · have inside : i - 16 < (bytes data 24).length := by simp [bytes]; omega + have eq := congrArg Cell.bits (write_initializes _ _ _ _ _ (i - 16) (writeLow p data) inside) + have idx : dest p + 16 + (i - 16) = dest p + i := by omega + have rotated : (i + 16) % 32 = i - 16 := by omega + simpa [bytes, idx, rotated] using eq + +theorem correctness : contract.Holds Extracted.program 0 := by + rintro ⟨p, data⟩ _ rfl + exact ⟨19, (expire (lowWritten p data) 1, []), ran p data, confined p data, (), rfl, outputs p data⟩ +#print axioms correctness +end HalfSwapOverlap diff --git a/Tests/HardwareExamples.cs b/Tests/HardwareExamples.cs new file mode 100644 index 000000000..29a20936a --- /dev/null +++ b/Tests/HardwareExamples.cs @@ -0,0 +1,226 @@ +using Mono.Cecil; +using Mono.Cecil.Cil; + +// These single-call bodies exercise the MemberRefs even when the host CPU cannot run the instruction. +internal static class HardwareExamples +{ + internal sealed record Example(string Selector, string Name, string Profile, string Model, bool NeedsFeature); + + internal static List Write(string assembly, string output, string x86, string arm) + { + using var module = ModuleDefinition.ReadModule(assembly); + var intrinsics = module.AssemblyReferences.Single(row => row.Name == "System.Runtime.Intrinsics"); + // The fixture retains the target framework's assembly references and contains just the call wrappers. + while (module.Types.Count > 1) module.Types.RemoveAt(module.Types.Count - 1); + var wrapper = new TypeDefinition("Examples", "HardwareMembers", + TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Sealed, module.TypeSystem.Object); + module.Types.Add(wrapper); + var examples = new List(); + var slots = new HashSet<(string Owner, string Name)>(); + var types = module.TypeSystem; + var uint64 = types.UInt64; + var int64 = types.Int64; + var uint32 = types.UInt32; + var int32 = types.Int32; + var byteType = types.Byte; + var boolean = types.Boolean; + var single = types.Single; + var doubleType = types.Double; + + TypeReference Owner(string name) + { + var nested = name.LastIndexOf('/'); + if (nested >= 0) + return new TypeReference("", name[(nested + 1)..], module, intrinsics) + { DeclaringType = Owner(name[..nested]) }; + var full = $"System.Runtime.Intrinsics.{name}"; + var dot = full.LastIndexOf('.'); + return new TypeReference(full[..dot], full[(dot + 1)..], module, intrinsics); + } + + static GenericInstanceType Apply(TypeReference head, params TypeReference[] arguments) + { + var applied = new GenericInstanceType(head); + foreach (var argument in arguments) applied.GenericArguments.Add(argument); + return applied; + } + + var heads = new Dictionary(); + foreach (var width in new[] { 128, 256, 512 }) + { + var head = Owner($"Vector{width}`1"); + head.IsValueType = true; + head.GenericParameters.Add(new GenericParameter("T", head)); + heads.Add(width, head); + } + TypeReference Vector(int width, TypeReference element) => Apply(heads[width], element); + var tuple = new TypeReference("System", "ValueTuple`2", module, types.CoreLibrary, true); + var pair = Apply(tuple, uint64, uint64); + + TypeReference Closed(TypeReference type) => type switch + { + GenericParameter => uint64, + GenericInstanceType generic => + Apply(generic.ElementType, generic.GenericArguments.Select(Closed).ToArray()), + _ => type + }; + + void Emit(MethodReference call, string model, string profile, bool feature) + { + if (!slots.Add((call.DeclaringType.GetElementType().FullName, call.Name))) + throw new InvalidDataException($"Repeated hardware slot in {call.FullName}"); + var name = $"Member{examples.Count}"; + var method = new MethodDefinition(name, MethodAttributes.Static | MethodAttributes.Public, + Closed(call.ReturnType)); + wrapper.Methods.Add(method); + var il = method.Body.GetILProcessor(); + foreach (var parameter in call.Parameters) + { + var argument = new ParameterDefinition(Closed(parameter.ParameterType)); + method.Parameters.Add(argument); + il.Emit(OpCodes.Ldarg, argument); + } + il.Emit(OpCodes.Call, call); + il.Emit(OpCodes.Ret); + examples.Add(new(method.FullName, name, profile, $".intrinsic ({model}) {call.Parameters.Count}", feature)); + } + + void Machine(string owner, string name, string model, TypeReference result, params TypeReference[] parameters) + { + var method = new MethodReference(name, result, Owner(owner)); + foreach (var parameter in parameters) method.Parameters.Add(new ParameterDefinition(parameter)); + Emit(method, model, owner.StartsWith("Arm.", StringComparison.Ordinal) ? arm : x86, true); + } + + Machine("X86.Avx2", "And", ".avx2 .band", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx2", "Or", ".avx2 .bor", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx2", "Xor", ".avx2 .bxor", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx2", "AndNot", ".avx2 .andNot", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx2", "ShiftLeftLogical", ".avx2 .shl64Count", Vector(256, uint64), + Vector(256, uint64), Vector(128, uint64)); + Machine("X86.Avx2", "ShiftRightLogical", ".avx2 .shr64Count", Vector(256, uint64), + Vector(256, uint64), Vector(128, uint64)); + Machine("X86.Avx512F", "CompareLessThan", ".avx512 .ltu512", Vector(512, uint64), + Vector(512, uint64), Vector(512, uint64)); + Machine("X86.Avx512F", "CompareGreaterThan", ".avx512 .gtu512", Vector(512, uint64), + Vector(512, uint64), Vector(512, uint64)); + Machine("X86.Avx512DQ", "MoveMask", ".avx512DQ .moveMask512", int32, + Vector(512, uint64)); + Machine("Arm.AdvSimd", "ReverseElement8", ".advSimd .reverseElement8", Vector(128, uint64), + Vector(128, uint64)); + Machine("X86.Sse2", "Multiply", ".sse .multiplyEven32", Vector(128, uint64), + Vector(128, uint32), Vector(128, uint32)); + Machine("X86.Ssse3", "Shuffle", ".sse .shuffleBytes", Vector(128, byteType), + Vector(128, byteType), Vector(128, byteType)); + Machine("X86.Avx2", "Shuffle", ".avx2 .shuffleBytes", Vector(256, byteType), + Vector(256, byteType), Vector(256, byteType)); + Machine("X86.Aes", "Encrypt", ".aes .encrypt", Vector(128, byteType), + Vector(128, byteType), Vector(128, byteType)); + Machine("Arm.Aes", "Encrypt", ".armAes .encrypt", Vector(128, byteType), + Vector(128, byteType), Vector(128, byteType)); + Machine("Arm.Aes", "MixColumns", ".armAes .mixColumns", Vector(128, byteType), + Vector(128, byteType)); + Machine("X86.Avx512F/VL", "PermuteVar4x64x2", ".avx512 .permuteVar4x64x2", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64), Vector(256, uint64)); + Machine("Arm.AdvSimd", "ExtractVector128", ".advSimd .extract64", Vector(128, uint64), + Vector(128, uint64), Vector(128, uint64), byteType); + Machine("X86.Sse2", "ShiftLeftLogical128BitLane", ".sse .shiftLeftBytes", Vector(128, uint64), + Vector(128, uint64), byteType); + Machine("X86.Ssse3", "AlignRight", ".sse .alignBytes", Vector(128, byteType), + Vector(128, byteType), Vector(128, byteType), byteType); + Machine("X86.Avx2", "Permute4x64", ".avx2 .permute4x64", Vector(256, uint64), + Vector(256, uint64), byteType); + Machine("X86.Avx2", "Blend", ".avx2 .blend32", Vector(256, uint32), + Vector(256, uint32), Vector(256, uint32), byteType); + Machine("X86.Avx2", "Add", ".avx2 .add64", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx2", "CompareEqual", ".avx2 .eq64", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx2", "CompareGreaterThan", ".avx2 .signedgt64", Vector(256, int64), + Vector(256, int64), Vector(256, int64)); + Machine("X86.Avx2", "Multiply", ".avx2 .multiplyEven32", Vector(256, uint64), + Vector(256, uint32), Vector(256, uint32)); + Machine("X86.Avx512F/VL", "AlignRight64", ".avx512 .alignRight64", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64), byteType); + Machine("X86.Avx512F/VL", "TernaryLogic", ".avx512 .ternaryLogic", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64), Vector(256, uint64), byteType); + Machine("X86.Avx512F/VL", "CompareLessThan", ".avx512 .ltu64", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx512F/VL", "CompareGreaterThan", ".avx512 .gtu64", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx512F/VL", "CompareGreaterThanOrEqual", ".avx512 .geu64", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx512DQ/VL", "MultiplyLow", ".avx512DQ .mul64", Vector(256, uint64), + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Avx", "MoveMask", ".avx .moveMask64", int32, + Vector(256, doubleType)); + Machine("X86.Avx", "Blend", ".avx .blend32", Vector(256, single), + Vector(256, single), Vector(256, single), byteType); + Machine("X86.Avx", "TestZ", ".avx .testZ64", boolean, + Vector(256, uint64), Vector(256, uint64)); + Machine("X86.Bmi1", "BitFieldExtract", ".bmi1 .bextr32", uint32, + uint32, byteType, byteType); + Machine("X86.Bmi2/X64", "MultiplyNoFlags", ".bmi2 .multiplyHigh64", uint64, + uint64, uint64); + Machine("Arm.ArmBase/Arm64", "MultiplyHigh", ".armBase64 .multiplyHigh64", uint64, + uint64, uint64); + Machine("X86.X86Base/X64", "DivRem", ".x86Base .divRem64", pair, + uint64, uint64, uint64); + + foreach (var width in new[] { 128, 256, 512 }) + { + void Portable(string name, string operation, bool ownerBinder, bool genericMethod, + Func result, params Func[] parameters) + { + var owner = ownerBinder ? Vector(width, uint64) : Owner($"Vector{width}"); + var method = new MethodReference(name, types.Void, owner); + TypeReference binder = uint64; + if (ownerBinder) binder = heads[width].GenericParameters[0]; + if (genericMethod) + { + binder = new GenericParameter("T", method); + method.GenericParameters.Add((GenericParameter)binder); + method.CallingConvention = MethodCallingConvention.Generic; + } + method.ReturnType = result(binder); + foreach (var parameter in parameters) method.Parameters.Add(new ParameterDefinition(parameter(binder))); + MethodReference call = method; + if (genericMethod) + { + var instance = new GenericInstanceMethod(method); + instance.GenericArguments.Add(uint64); + call = instance; + } + Emit(call, $".vector (.{operation} {width})", "scalar", false); + } + TypeReference V(TypeReference element) => Vector(width, element); + foreach (var (name, operation) in new[] + { + ("op_BitwiseAnd", "band"), ("op_BitwiseOr", "bor"), ("op_ExclusiveOr", "bxor") + }) + Portable(name, operation, true, false, V, V, V); + Portable("op_OnesComplement", "bnot", true, false, V, V); + Portable("get_Zero", "zero", true, false, V); + Portable("get_AllBitsSet", "ones", true, false, V); + Portable("EqualsAll", "equalsAll", false, true, _ => boolean, V, V); + if (width == 512) continue; + Portable("op_Addition", "add64", true, false, V, V, V); + Portable("op_Subtraction", "sub64", true, false, V, V, V); + Portable("Equals", "eq64", false, true, V, V, V); + Portable("LessThan", "ltu64", false, true, V, V, V); + Portable("Sum", "sum64", false, true, element => element, V); + Portable("ExtractMostSignificantBits", "extractMSB64", false, true, _ => uint32, V); + Portable("ShiftRightArithmetic", "ashr64", false, false, _ => V(int64), _ => V(int64), _ => int32); + Portable("CreateScalar", "createScalar64", false, false, _ => V(uint64), _ => uint64); + foreach (var target in new[] { byteType, doubleType, int32, int64, single, uint32, uint64 }) + Portable($"As{target.Name}", "reinterpret", false, true, _ => V(target), V); + } + if (examples.Count != 90) throw new InvalidDataException($"Expected 90 hardware slots, found {examples.Count}"); + module.Write(output); + return examples; + } +} diff --git a/Tests/HoleCorpus.cs b/Tests/HoleCorpus.cs new file mode 100644 index 000000000..310a0a8f8 --- /dev/null +++ b/Tests/HoleCorpus.cs @@ -0,0 +1,312 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:property AssemblyName=LeanCIL.IdentityTests +#:project ../Extractor/Extractor.csproj +#:package Mono.Cecil + +extern alias IdentityExtractor; + +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Security.Cryptography; +using System.Text.Json; +using Mono.Cecil; +using Mono.Cecil.Cil; +using IdentityExtractor::Nethermind.LeanCIL; + +// Dead instructions in the admitted scalar self-admission corpus, including compiler-generated bodies. +const string Usage = "Usage: HoleCorpus.cs [--update] [--baseline path] [--admission-report path]"; +const string Profile = "scalar"; +var root = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Source())!, "..")); +var baselinePath = Path.Combine(root, "Tests/HoleCorpusBaseline.txt"); +string? admissionReport = null; +var update = false; +for (var index = 0; index < args.Length; index++) + switch (args[index]) + { + case "--update": update = true; break; + case "--baseline" when index + 1 < args.Length: baselinePath = Path.GetFullPath(args[++index]); break; + case "--admission-report" when index + 1 < args.Length: + admissionReport = Path.GetFullPath(args[++index]); + break; + default: throw new ArgumentException(Usage); + } + +CheckInspection(); +CheckRefusalReport(); +CheckUndecodedCallee(Path.Combine(root, "Extractor/bin/Nethermind.LeanCIL.dll")); +var timer = Stopwatch.StartNew(); +var assemblyPath = Path.Combine(root, "Extractor/bin/Nethermind.LeanCIL.dll"); +var bytes = File.ReadAllBytes(assemblyPath); +var hash = Convert.ToHexStringLower(SHA256.HashData(bytes)); +using var pinned = new PinnedAssemblies(bytes, hash, []); +var methods = pinned.Selected.GetTypes().SelectMany(type => type.Methods) + .ToDictionary(method => method.FullName, StringComparer.Ordinal); +var directory = Path.Combine(root, "artifacts/HoleCorpus"); +Directory.CreateDirectory(directory); +if (admissionReport is null) +{ + var manifest = Path.Combine(directory, "manifest.json"); + File.WriteAllText(manifest, JsonSerializer.Serialize(new + { + entries = methods.Keys.Order(StringComparer.Ordinal).Select(signature => new { signature, profile = Profile }) + })); + admissionReport = Path.Combine(directory, "blockers.json"); + if (Blockers.Run([assemblyPath, "--manifest", manifest, "--output", admissionReport]) != 0) + throw new InvalidOperationException("Corpus admission failed."); +} + +using var document = JsonDocument.Parse(File.ReadAllText(admissionReport)); +var report = document.RootElement; +var extractorBytes = File.ReadAllBytes(typeof(FeatureProfile).Assembly.Location); +var extractorHash = Convert.ToHexStringLower(SHA256.HashData(extractorBytes)); +if (report.GetProperty("assemblySha256").GetString() != hash || + report.GetProperty("extractorSha256").GetString() != extractorHash || + report.GetProperty("initializedTypes").GetArrayLength() != 0) + throw new InvalidDataException("Admission report does not match the corpus assembly, extractor or assumptions."); +var results = new List(); +var seen = new HashSet(StringComparer.Ordinal); +var skipped = 0; +foreach (var entry in report.GetProperty("entries").EnumerateArray()) +{ + var signature = entry.GetProperty("signature").GetString()!; + if (!seen.Add(signature) || !methods.TryGetValue(signature, out var method) || + entry.GetProperty("profile").GetString() != Profile || + entry.GetProperty("typeArguments").GetArrayLength() != 0) + throw new InvalidDataException("Unexpected corpus admission entry: " + signature); + switch (entry.GetProperty("status").GetString()) + { + case "admitted": results.Add(Inspect(method)); break; + case "blocked" or "notChecked": skipped++; break; + default: throw new InvalidDataException("Unexpected admission status for " + signature); + } +} +if (seen.Count != methods.Count) + throw new InvalidDataException($"Admission report covers {seen.Count} of {methods.Count} methods."); + +File.WriteAllText(Path.Combine(directory, "methods.json"), + JsonSerializer.Serialize(new { skipped, methods = results }, new JsonSerializerOptions { WriteIndented = true })); +// A corpus with no admitted method would pass with nothing inspected. +if (results.Count == 0) + throw new InvalidDataException("The admission report admits no corpus method, so no body was inspected."); +string[] rows = [.. results.SelectMany(Rows).Order(StringComparer.Ordinal)]; +if (update) File.WriteAllText(baselinePath, string.Concat(rows.Select(row => row + "\n"))); +var baseline = File.ReadAllLines(baselinePath).Where(line => line.Length != 0).ToArray(); +var removed = baseline.Except(rows, StringComparer.Ordinal).ToArray(); +var added = rows.Except(baseline, StringComparer.Ordinal).ToArray(); +var homes = results.Count(result => result.Arguments.Length != 0); +var prefixes = results.Sum(result => result.Prefixes.Length); +Console.WriteLine($"{results.Count} admitted methods; {skipped} skipped refused methods; " + + $"{homes} home differences; {prefixes} dead prefixes; {timer.Elapsed.TotalSeconds:F2} seconds"); +foreach (var row in rows) Console.WriteLine(row); +if (!baseline.SequenceEqual(rows, StringComparer.Ordinal)) + throw new InvalidOperationException("Hole corpus differs from " + baselinePath + ":\n" + + string.Join('\n', removed.Select(row => "- " + row).Concat(added.Select(row => "+ " + row))) + + "\nReview the differences, then run HoleCorpus.cs --update."); +Console.WriteLine("PASS hole corpus matches the baseline"); + +static string Source([CallerFilePath] string path = "") => path; + +static MethodHoles Inspect(MethodDefinition method) +{ + if (!method.HasBody) return new(method.FullName, [], []); + var reachable = Reachability.Analyze(method, FeatureProfile.Scalar); + var allHomes = new HashSet(); + var liveHomes = new HashSet(); + var prefixes = new List(); + var instructions = method.Body.Instructions; + for (var pc = 0; pc < instructions.Count; pc++) + { + var instruction = instructions[pc]; + var live = reachable.Contains(instruction); + if (instruction.OpCode.Code is Code.Starg or Code.Starg_S or Code.Ldarga or Code.Ldarga_S) + { + var argument = MethodArguments.Index(method, (ParameterDefinition)instruction.Operand); + allHomes.Add(argument); + if (live) liveHomes.Add(argument); + } + if (!live && instruction.OpCode.OpCodeType == OpCodeType.Prefix) + prefixes.Add(new(pc, instruction.Offset, instruction.OpCode.Name)); + } + allHomes.ExceptWith(liveHomes); + return new(method.FullName, allHomes.Order().ToArray(), prefixes.ToArray()); +} + +static IEnumerable Rows(MethodHoles result) +{ + if (result.Arguments.Length != 0) + yield return $"homes\t{result.Method}\t{string.Join(',', result.Arguments)}"; + foreach (var prefix in result.Prefixes) + yield return $"prefix\t{result.Method}\tpc={prefix.Pc}\tIL_{prefix.Offset:x4}\t{prefix.Opcode}"; +} + +static void CheckInspection() +{ + using var module = ModuleDefinition.CreateModule("HoleChecks", ModuleKind.Dll); + var owner = new TypeDefinition("", "Receiver", TypeAttributes.Public, module.TypeSystem.Object); + module.Types.Add(owner); + var method = new MethodDefinition("Body", MethodAttributes.Public, module.TypeSystem.Void); + owner.Methods.Add(method); + var first = new ParameterDefinition(module.TypeSystem.Int32); + var second = new ParameterDefinition(module.TypeSystem.Int32); + method.Parameters.Add(first); + method.Parameters.Add(second); + var finish = Instruction.Create(OpCodes.Ret); + Instruction[] body = + [ + Instruction.Create(OpCodes.Ldarga_S, first), Instruction.Create(OpCodes.Pop), + Instruction.Create(OpCodes.Br, finish), Instruction.Create(OpCodes.Ldc_I4_0), + Instruction.Create(OpCodes.Starg, second), Instruction.Create(OpCodes.Ldc_I4_0), + Instruction.Create(OpCodes.Starg_S, first), Instruction.Create(OpCodes.Ldnull), + Instruction.Create(OpCodes.Unaligned, (byte)1), Instruction.Create(OpCodes.Ldind_I4), + Instruction.Create(OpCodes.Pop), Instruction.Create(OpCodes.Ldnull), Instruction.Create(OpCodes.Ldc_I4_0), + Instruction.Create(OpCodes.Readonly), Instruction.Create(OpCodes.Ldelema, module.TypeSystem.Int32), + Instruction.Create(OpCodes.Pop), finish + ]; + foreach (var instruction in body) method.Body.Instructions.Add(instruction); + // Decoded instructions have byte offsets independently of their instruction indices. + using var image = new MemoryStream(); + module.Write(image); + image.Position = 0; + using var decoded = ModuleDefinition.ReadModule(image); + var result = Inspect(decoded.Types.Single(type => type.Name == owner.Name).Methods.Single()); + string[] expected = + [ + "homes\tSystem.Void Receiver::Body(System.Int32,System.Int32)\t2", + "prefix\tSystem.Void Receiver::Body(System.Int32,System.Int32)\tpc=8\tIL_0011\tunaligned.", + "prefix\tSystem.Void Receiver::Body(System.Int32,System.Int32)\tpc=13\tIL_0018\treadonly." + ]; + if (!Rows(result).SequenceEqual(expected, StringComparer.Ordinal)) + throw new InvalidOperationException("Incorrect argument or prefix position:\n" + + string.Join('\n', Rows(result))); +} + +// A live instruction the decoder refuses blocks its entry at the refusing method's instruction, with its opcode, +// pc and IL offset in the report, while the same instruction dead refuses nothing. +static void CheckRefusalReport() +{ + using var module = ModuleDefinition.CreateModule("RefusalChecks", ModuleKind.Dll); + var owner = new TypeDefinition("", "Receiver", TypeAttributes.Public, module.TypeSystem.Object); + module.Types.Add(owner); + foreach (var name in new[] { "Entry", "Body" }) + owner.Methods.Add(new(name, MethodAttributes.Public | MethodAttributes.Static, module.TypeSystem.Void)); + owner.Methods[0].Body.GetILProcessor().Emit(OpCodes.Ret); + var il = owner.Methods[1].Body.GetILProcessor(); + il.Emit(OpCodes.Ldc_I4, 5); + foreach (var opcode in new[] { OpCodes.Pop, OpCodes.Arglist, OpCodes.Pop, OpCodes.Ret }) il.Emit(opcode); + // Written and read back, so the refused instruction's IL offset differs from its pc. + using var image = new MemoryStream(); + module.Write(image); + image.Position = 0; + using var decoded = ModuleDefinition.ReadModule(image); + var methods = decoded.Types.Single(type => type.Name == owner.Name).Methods; + var entry = methods.Single(method => method.Name == "Entry"); + var body = methods.Single(method => method.Name == "Body"); + var instructions = body.Body.Instructions; + var calls = new ResolvedCallSites(); + var refusal = GenericEvidence.FirstDecodeRefusal(body, instructions.ToHashSet(), calls, null) ?? + throw new InvalidOperationException("The decoder accepted a live arglist."); + var failure = Blockers.Rejection(entry, new GenericEvidence.UndecodedInstruction(body, refusal)); + var site = Blockers.SiteOf(failure); + var construct = Blockers.Construct(site.Message); + if (failure.Method != body || !failure.Chain.SequenceEqual([entry]) || + site != site with { Method = body.FullName, IlOffset = 6, Instruction = "IL_0006: arglist", Source = null } || + !site.Message.EndsWith($"at pc 2 of {body.FullName}", StringComparison.Ordinal) || + construct != "Unsupported instruction: the decoder has no form for arglist") + throw new InvalidOperationException($"Incorrect decode refusal site {site} in {construct}"); + var live = instructions.Where(instruction => instruction.OpCode != OpCodes.Arglist).ToHashSet(); + if (GenericEvidence.FirstDecodeRefusal(body, live, calls, null) is { } dead) + throw new InvalidOperationException($"The decoder refused a dead {dead.At}."); + // The same raw call needs a target from the admitted graph. + var call = Instruction.Create(OpCodes.Call, entry); + body.Body.Instructions.Insert(0, call); + var liveCall = new HashSet { call }; + var unbound = GenericEvidence.FirstDecodeRefusal(body, liveCall, calls, null); + if (unbound != new GenericEvidence.DecodeRefusal(0, call, GenericEvidence.RefusalKind.UnboundCall) || + Blockers.Construct(new GenericEvidence.UndecodedInstruction(body, unbound).Message) != + "Unsupported instruction: the decoder cannot bind the call operand with its prefixes") + throw new InvalidOperationException($"Incorrect call refusal {unbound}"); + calls.Add(call, entry); + if (GenericEvidence.FirstDecodeRefusal(body, liveCall, calls, null) is { } bound) + throw new InvalidOperationException($"The decoder refused the bound call: {bound}"); + var models = new ResolvedCallSites(); + models.AddModel(call); + if (!models.BindsOperand(call, null)) + throw new InvalidOperationException("The decoder refused an ordinary runtime model call."); + body.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Tail)); + if (!calls.BindsOperand(call, null) || models.BindsOperand(call, null)) + throw new InvalidOperationException("A tail call must enter a managed target."); +} + +// The blockers command, run on an assembly whose callee the extractor admits, blocks the entry at the callee's add +// when add is outside the opcodes the decoder reads, and admits it with the decoder's own opcodes. The fixture is a +// copy of the extractor's assembly, which the extractor accepts as a module, with one added type. +static void CheckUndecodedCallee(string template) +{ + var directory = Path.Combine(Path.GetTempPath(), "leancil-undecoded-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(directory); + try + { + using var assembly = AssemblyDefinition.ReadAssembly(template); + var module = assembly.MainModule; + var owner = new TypeDefinition("", "Receiver", TypeAttributes.Public | TypeAttributes.Abstract | + TypeAttributes.Sealed, module.TypeSystem.Object); + module.Types.Add(owner); + MethodDefinition Define(string name) + { + var method = new MethodDefinition(name, MethodAttributes.Public | MethodAttributes.Static, + module.TypeSystem.Int32); + method.Parameters.Add(new("value", ParameterAttributes.None, module.TypeSystem.Int32)); + owner.Methods.Add(method); + return method; + } + var (entry, body) = (Define("Entry"), Define("Body")); + var il = body.Body.GetILProcessor(); + foreach (var opcode in new[] { OpCodes.Ldarg_0, OpCodes.Ldc_I4_1, OpCodes.Add, OpCodes.Ret }) il.Emit(opcode); + il = entry.Body.GetILProcessor(); + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Call, body); + il.Emit(OpCodes.Ret); + var path = Path.Combine(directory, "Undecoded.dll"); + assembly.Write(path); + var manifest = Path.Combine(directory, "manifest.json"); + File.WriteAllText(manifest, """{ "entries": [ { "signature": "Receiver::Entry(int)", "profile": "scalar" } ] }"""); + JsonElement Report(IReadOnlySet opcodes, string name) + { + var output = Path.Combine(directory, name); + GenericEvidence.ExtractedOpcodes = opcodes; + try + { + if (Blockers.Run([path, "--manifest", manifest, "--output", output]) != 0) + throw new InvalidOperationException("The blockers command failed."); + } + finally { GenericEvidence.ExtractedOpcodes = GenericEvidence.DecodedOpcodes; } + return JsonSerializer.Deserialize(File.ReadAllText(output)); + } + const string bodyName = "System.Int32 Receiver::Body(System.Int32)"; + const string entryName = "System.Int32 Receiver::Entry(System.Int32)"; + var admitted = Report(GenericEvidence.DecodedOpcodes, "admitted.json").GetProperty("entries")[0]; + if (admitted.GetProperty("status").GetString() != "admitted" || + admitted.GetProperty("reachableMethods").GetInt32() != 2) + throw new InvalidOperationException($"The decodable fixture was not admitted: {admitted}"); + var refused = Report(GenericEvidence.DecodedOpcodes.Where(code => code != Code.Add).ToHashSet(), + "refused.json"); + var outcome = refused.GetProperty("entries")[0]; + var blocker = refused.GetProperty("blockers").EnumerateArray().Single(); + var site = blocker.GetProperty("sites").EnumerateArray().Single(); + var chains = blocker.GetProperty("entries").EnumerateArray().Single().GetProperty("chains"); + if (outcome.GetProperty("status").GetString() != "blocked" || + blocker.GetProperty("construct").GetString() != + "Unsupported instruction: the decoder has no form for add" || + site.GetProperty("method").GetString() != bodyName || site.GetProperty("ilOffset").GetInt32() != 2 || + site.GetProperty("instruction").GetString() != "IL_0002: add" || + !site.GetProperty("message").GetString()!.EndsWith($"at pc 2 of {bodyName}", StringComparison.Ordinal) || + chains.GetArrayLength() != 1 || chains[0].GetArrayLength() != 1 || + chains[0][0].GetString() != entryName) + throw new InvalidOperationException($"Incorrect undecoded callee report: {refused}"); + } + finally { Directory.Delete(directory, recursive: true); } +} + +sealed record MethodHoles(string Method, int[] Arguments, DeadPrefix[] Prefixes); +sealed record DeadPrefix(int Pc, int Offset, string Opcode); diff --git a/Tests/HoleCorpusBaseline.txt b/Tests/HoleCorpusBaseline.txt new file mode 100644 index 000000000..e69de29bb diff --git a/Tests/Identities.cs b/Tests/Identities.cs new file mode 100644 index 000000000..1eb5f8ac9 --- /dev/null +++ b/Tests/Identities.cs @@ -0,0 +1,996 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:property AssemblyName=LeanCIL.IdentityTests +#:project ../Extractor/Extractor.csproj +#:package Mono.Cecil +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include LeanStringEmissions.cs + +extern alias IdentityExtractor; + +using System.Diagnostics; +using System.Globalization; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Security.Cryptography; +using System.Text; +using Mono.Cecil; +using Mono.Cecil.Cil; +using IdentityExtractor::Nethermind.LeanCIL; +using Srm = System.Reflection.Metadata; + +// Every identity of the corpus is rendered with ToLean(), elaborated by Lean, and shown by CIL/Identity.lean's +// display; that text must equal ToDisplay(). Each record is first checked against the raw metadata rows it reads. +var clock = Stopwatch.StartNew(); +if (args.Length == 0 || args.Contains("--emissions-only", StringComparer.Ordinal)) +{ + CheckLeanStringEmissions(); + if (args.Length != 0) return; +} +if (args.Contains("--pair-signatures-only", StringComparer.Ordinal)) +{ + CheckPairSignatures(); + return; +} +if (args.Contains("--exception-types-only", StringComparer.Ordinal)) +{ + CheckExceptionTypes(); + return; +} +if (args.Contains("--assembly-cache-only", StringComparer.Ordinal)) +{ + CheckAssemblyCache(); + return; +} +if (args.Contains("--heap-bounds-only", StringComparer.Ordinal)) +{ + CheckHeapBounds(); + return; +} +if (args.Contains("--raw-names-only", StringComparer.Ordinal)) +{ + CheckRawNames(At("artifacts/Identities/Unusual.dll")); + return; +} +if (args.Contains("--field-references-only", StringComparer.Ordinal)) +{ + CheckFieldReferences(); + return; +} +if (args.Contains("--code-cost", StringComparer.Ordinal)) +{ + CheckCodeCost(); + return; +} +if (args.Length == 0) CheckExceptionTypes(); +CheckScaling(); +CheckCodeCost(); +if (args.Contains("--scaling-only", StringComparer.Ordinal)) return; +CheckHeapBounds(); +CheckAssemblyCache(); +CheckFieldReferences(); +CheckPairSignatures(); +var directory = At("artifacts/Identities"); +Directory.CreateDirectory(directory); +var examplesPath = At("Examples/Assembly/bin/Examples.dll"); +var unusualPath = Path.Combine(directory, "Unusual.dll"); +WriteUnusual(unusualPath); +var resolver = new DefaultAssemblyResolver(); +var parameters = new ReaderParameters { AssemblyResolver = resolver }; +using var examples = ModuleDefinition.ReadModule(examplesPath, parameters); +using var unusual = ModuleDefinition.ReadModule(unusualPath, parameters); +foreach (var (path, module) in new[] { (examplesPath, examples), (unusualPath, unusual) }) + CheckRows(path, module); +Console.WriteLine("PASS identity records equal the raw rows of Examples.dll and Unusual.dll"); + +var corpus = new IdentityCorpus(); +corpus.Add(examples, resolve: true); +corpus.Add(unusual, resolve: false); +CheckTypedPredicates(examples, unusual); +CheckRawNames(unusualPath); +CheckEquality(examplesPath, parameters, corpus); +corpus.RequireCoverage(); + +var source = new StringBuilder(""" + import CIL.Identity + open CIL + + /-- The UTF-8 bytes of a display in hexadecimal, so a display that spans lines prints on one. -/ + def hexOf (text : String) : String := + String.join (text.toUTF8.toList.map fun byte => + let digits := Nat.toDigits 16 byte.toNat + String.ofList (List.replicate (2 - digits.length) '0' ++ digits)) + + /-- Prints each numbered text on its own line. -/ + def emit (texts : List (Nat × String)) : IO Unit := + texts.forM fun (index, text) => IO.println s!"@{index}:{hexOf text}" + + #eval IO.println s!"@ranges:{invisibleRanges}" + + /-- Raw code formatting retains every digit with bounded growth in payload length. -/ + def checkRawCodeCost : IO Unit := do + let mut previous := 0 + for size in [4096, 32768, 262144] do + let baseCode := (1 <<< (8 * size + 1)) - 1 + let mut best := 1000000000 + for sample in [:3] do + let code := baseCode - sample + let started ← IO.monoMsNow + let shown := rawCodeDisplay code + let elapsed := (← IO.monoMsNow) - started + let ending := [Nat.digitChar (15 - sample)] + let expected := "{ code := 0x1" ++ + String.ofList (List.replicate (2 * size - 1) 'f' ++ ending) ++ " }" + unless shown == expected do + throw (IO.userError "Raw code formatting changed its numeric value") + best := min best elapsed + if previous > 0 && best > previous * 16 + 10 then + throw (IO.userError s!"Raw formatting cost {best}ms after {previous}ms for 8x fewer bytes") + IO.println s!"PASS Lean raw display {size}: {best} ms" + previous := best + + #eval checkRawCodeCost + + """); +var expected = new List<(string Kind, string Term, string Text)>(); +const int ChunkSize = 64; +void Emit(string kind, IEnumerable<(string Term, string LeanText, string Text)> entries) +{ + foreach (var chunk in entries.Chunk(ChunkSize)) + { + source.AppendLine("#eval emit ["); + source.AppendLine(string.Join(",\n", chunk.Select(entry => + { + expected.Add((kind, entry.Term, entry.Text)); + return $" ({expected.Count - 1}, {entry.LeanText})"; + }))); + source.AppendLine("]"); + } +} +static string Show(string term, string type, string function) => $"(({term} : {type}).{function})"; +Emit("raw-code", new[] { 0, 1, 15, 16, 255, 256, 511, 767, 120832, 121856 }.Select(code => + (code.ToString(CultureInfo.InvariantCulture), $"rawCodeDisplay {code}", IdentityText.RawCode(code)))); +Emit("name", corpus.Names.SelectMany(name => new[] +{ + (name.ToLean(), Show(name.ToLean(), "MetadataName", "display"), name.ToDisplay()), + (name.ToLean(), $"toString {Show(name.ToLean(), "MetadataName", "code")}", + name.Code.ToString(CultureInfo.InvariantCulture)) +})); +Emit("literal", corpus.Literals.SelectMany(literal => new[] +{ + (literal.ToLean(), Show(literal.ToLean(), "UserString", "display"), literal.ToDisplay()), + (literal.ToLean(), $"toString {Show(literal.ToLean(), "UserString", "code")}", + literal.Code.ToString(CultureInfo.InvariantCulture)) +})); +Emit("version", corpus.Versions.Select(version => + (version.ToLean(), Show(version.ToLean(), "Version", "display"), version.ToDisplay()))); +Emit("assembly", corpus.Assemblies.Select(assembly => + (assembly.ToLean(), Show(assembly.ToLean(), "AssemblyName", "display"), assembly.ToDisplay()))); +Emit("level", corpus.Levels.Select(level => + (level.ToLean(), $"reprStr ({level.ToLean()} : TypeLevel)", level.ToDisplay()))); +Emit("type", corpus.Types.Select(type => + (type.ToLean(), Show(type.ToLean(), "TypeIdentity", "display"), type.ToDisplay()))); +Emit("primitive", PrimitiveType.All.SelectMany(primitive => new[] +{ + (primitive.ToLean(), Show(primitive.ToLean(), "PrimitiveType", "display"), primitive.ToDisplay()), + (primitive.ToLean(), $"toString {Show(primitive.ToLean(), "PrimitiveType", "elementType")}", + ((int)primitive.ElementType).ToString(CultureInfo.InvariantCulture)) +})); +Emit("kind", corpus.Kinds.Select(kind => + (kind.ToLean(), $"reprStr ({kind.ToLean()} : TypeKind)", kind.ToDisplay()))); +var leanPath = Path.Combine(directory, "Identities.lean"); +File.WriteAllText(leanPath, source.ToString()); +var leanClock = Stopwatch.StartNew(); +var output = Run(["lake", "env", "lean", leanPath], cwd: FixturePackage()); +leanClock.Stop(); +foreach (string line in output.Split('\n').Where(line => + line.StartsWith("PASS Lean raw display", StringComparison.Ordinal))) + Console.WriteLine(line.TrimEnd('\r')); + +var shown = new Dictionary(); +string? ranges = null; +foreach (var line in output.Split('\n').Select(line => line.TrimEnd('\r'))) +{ + if (line.StartsWith("@ranges:", StringComparison.Ordinal)) ranges = line["@ranges:".Length..]; + else if (line.StartsWith('@') && line.IndexOf(':') is > 1 and var colon) + shown.Add(int.Parse(line[1..colon], CultureInfo.InvariantCulture), + Encoding.UTF8.GetString(Convert.FromHexString(line[(colon + 1)..]))); +} +var tableText = "[" + string.Join(", ", IdentityText.InvisibleRanges.Select(range => + string.Create(CultureInfo.InvariantCulture, $"({range.Low}, {range.High})"))) + "]"; +if (ranges != tableText) + throw new InvalidOperationException( + $"CIL.invisibleRanges is {ranges ?? "missing"}, the C# table {tableText}.\n" + output); +var mismatches = expected.Select((entry, index) => (entry, index)) + .Where(item => !shown.TryGetValue(item.index, out var text) || text != item.entry.Text).ToList(); +if (mismatches.Count != 0) + throw new InvalidOperationException( + $"{mismatches.Count} of {expected.Count} Lean displays differ from ToDisplay():\n" + + string.Join("\n", mismatches.Take(20).Select(item => $"{item.entry.Kind} {item.entry.Term}\n Lean: " + + $"{(shown.TryGetValue(item.index, out var text) ? text : "")}\n ToDisplay: {item.entry.Text}")) + + "\n" + output[..Math.Min(output.Length, 4000)]); +Console.WriteLine($"PASS {expected.Count} Lean displays of {corpus.Summary()} equal ToDisplay()"); +Console.WriteLine(string.Create(CultureInfo.InvariantCulture, + $"PASS identity records cost {clock.Elapsed.TotalSeconds:F1} s, " + + $"of which Lean took {leanClock.Elapsed.TotalSeconds:F1} s")); + +// A rendered field reference elaborates to the structure holding the record's owner, index, type and name bytes; +// a swapped field or a name rendered from other bytes fails to elaborate or fails its guard. +static void CheckExceptionTypes() +{ + var directory = At("artifacts/Identities"); + Directory.CreateDirectory(directory); + var path = Path.Combine(directory, "ExceptionTypes.lean"); + var members = ExceptionTypeText.All; + string enumeration = string.Join(", ", members.Select(member => member.ToLean())); + string displays = string.Join("\n", members.Select(member => + $"#guard CIL.ExceptionType.ownerName {member.ToLean()} == \"{member.FullName()}\"")); + File.WriteAllText(path, $$""" + import CIL.ExceptionType + example : CIL.ExceptionType.all = [{{enumeration}}] := rfl + {{displays}} + """); + Run(["lake", "env", "lean", path], cwd: FixturePackage()); + using var module = ModuleDefinition.CreateModule("ExceptionConstructors", ModuleKind.Dll); + var scope = new AssemblyNameReference("System.Runtime", new Version(11, 0, 0, 0)) + { + PublicKeyToken = [0xb0, 0x3f, 0x5f, 0x7f, 0x11, 0xd5, 0x0a, 0x3a] + }; + (string Name, ExceptionType? Raised)[] constructors = + [ + ("InvalidOperationException", ExceptionType.InvalidOperation), + ("PlatformNotSupportedException", null), + ("InvalidCastException", null) + ]; + foreach (var (name, raised) in constructors) + { + var owner = new TypeReference("System", name, module, scope); + var constructor = new MethodReference(".ctor", module.TypeSystem.Void, owner) { HasThis = true }; + var method = new MethodDefinition(name, MethodAttributes.Public | MethodAttributes.Static, + module.TypeSystem.Void); + var throwing = Instruction.Create(OpCodes.Throw); + method.Body.Instructions.Add(Instruction.Create(OpCodes.Newobj, constructor)); + method.Body.Instructions.Add(throwing); + var analysis = Throws.Analyze(method); + ExceptionType? actual = analysis.Raises.TryGetValue(throwing, out var result) ? result : null; + if (actual != raised) throw new InvalidOperationException("Unexpected exception admission: " + name); + } + Console.WriteLine("PASS exception members, display names and constructor admission"); +} + +// The fixture contains metadata and an abstract method with no body. +static byte[] PairSignatureImage(bool secondValueType = true, bool namedArgument = false, bool trailing = false) +{ + var rows = new MetadataBuilder(); + var fixture = rows.GetOrAddString("PairSignatures"); + rows.AddModule(0, fixture, rows.GetOrAddGuid(Guid.Empty), default, default); + rows.AddAssembly(fixture, new Version(1, 0, 0, 0), default, default, 0, 0); + var runtime = AssemblyNameReference.Parse(PlatformAssemblies.SystemRuntime.ToDisplay()); + var scope = rows.AddAssemblyReference(rows.GetOrAddString(runtime.Name), runtime.Version, default, + rows.GetOrAddBlob(runtime.PublicKeyToken), 0, default); + var system = rows.GetOrAddString("System"); + var pair = rows.AddTypeReference(scope, system, rows.GetOrAddString("ValueTuple`2")); + var integer = rows.AddTypeReference(scope, system, rows.GetOrAddString("Int32")); + byte pairIndex = checked((byte)CodedIndex.TypeDefOrRef(pair)); + byte intIndex = checked((byte)CodedIndex.TypeDefOrRef(integer)); + var firstField = MetadataTokens.FieldDefinitionHandle(1); + var method = MetadataTokens.MethodDefinitionHandle(1); + rows.AddTypeDefinition(0, default, rows.GetOrAddString(""), default, firstField, method); + rows.AddTypeDefinition(System.Reflection.TypeAttributes.Abstract, default, fixture, default, firstField, method); + Srm.BlobHandle Blob(params byte[] bytes) => rows.GetOrAddBlob(bytes); + rows.AddFieldDefinition(0, rows.GetOrAddString("First"), Blob(6, 0x15, 0x11, pairIndex, 2, 8, 8)); + byte kind = secondValueType ? (byte)0x11 : (byte)0x12; + byte[] argument = namedArgument ? [0x11, intIndex] : [8]; + rows.AddFieldDefinition(0, rows.GetOrAddString("Second"), Blob([6, 0x15, kind, pairIndex, 2, .. argument, 8])); + rows.AddMethodDefinition(System.Reflection.MethodAttributes.Abstract | System.Reflection.MethodAttributes.Virtual, + 0, rows.GetOrAddString("Anchor"), Blob(0x20, 0, 1), -1, MetadataTokens.ParameterHandle(1)); + rows.AddTypeSpecification(trailing ? Blob(8, 0) : Blob(8)); + rows.AddMemberReference(pair, rows.GetOrAddString("Item1"), trailing ? Blob(6, 8, 0) : Blob(6, 8)); + var output = new Srm.BlobBuilder(); + var image = new ManagedPEBuilder(new PEHeaderBuilder(), new MetadataRootBuilder(rows), new Srm.BlobBuilder()); + image.Serialize(output); + return output.ToArray(); +} + +static void CheckPairSignatures() +{ + var failures = new List(); + void Refuses(string test, string reason, Action check) + { + try { check(); } + catch (InvalidDataException error) when (error.Message.Contains(reason, StringComparison.Ordinal)) + { + Console.WriteLine($"PASS {test}: {error.Message}"); + return; + } + failures.Add(test); + Console.WriteLine($"FAIL {test}: accepted"); + } + static int Layout(byte[] image, bool cached = false) + { + using var pins = new PinnedAssemblies(image, Convert.ToHexStringLower(SHA256.HashData(image)), []); + var module = pins.Selected; + var fields = module.Types[1].Fields; + if (!cached) return MetadataValidation.AggregateLayout(fields[1].FieldType, module).Size; + var context = new MetadataValidation.LayoutContext(module, "scalar"); + context.AggregateLayout(fields[0].FieldType); + return context.AggregateLayout(fields[1].FieldType).Size; + } + static void Slice(byte[] image, uint token) + { + using var pins = new PinnedAssemblies(image, Convert.ToHexStringLower(SHA256.HashData(image)), []); + var method = pins.Original(pins.Selected).Types[1].Methods.Single(); + new MetadataSlices(pins).Method(method, [token], classOwner: false, arguments: []); + } + var control = PairSignatureImage(); + if (Layout(control) != 8) throw new InvalidOperationException("Unexpected primitive pair size"); + Refuses("CLASS after VALUETYPE", "value-type signature", + () => Layout(PairSignatureImage(secondValueType: false))); + var named = PairSignatureImage(namedArgument: true); + Refuses("named Int32 argument", "Unsupported field type", () => Layout(named)); + Refuses("named Int32 after cached primitive layout", "Unsupported field type", () => Layout(named, cached: true)); + var suffix = PairSignatureImage(trailing: true); + foreach (var (form, token) in new[] { ("TypeSpec", 0x1b000001u), ("field MemberRef", 0x0a000001u) }) + { + Slice(control, token); + Refuses($"{form} trailing byte", "trailing", () => Slice(suffix, token)); + } + if (failures.Count != 0) throw new InvalidOperationException(string.Join(", ", failures)); +} + +static void CheckFieldReferences() +{ + const string scope = "\"T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\""; + (FieldRef Reference, byte[] NameBytes)[] cases = + [ + (new($".atom {scope} \"Program\"", 0, ".atom \"System.Private.CoreLib\" \"System.Int32\"", + MetadataName.OfText("count")), "count"u8.ToArray()), + (new($".apply (.constructor {scope} \"Box`1\") (.parameter 0)", 70000, ".array (.byref (.parameter 0))", + MetadataName.FromBytes([0xFF, 0x00])), [0xFF, 0x00]), + (new($".object {scope} \"Node\"", 3, $".pointer (.object {scope} \"Node\")", + MetadataName.OfText("quote\"back\\slash")), "quote\"back\\slash"u8.ToArray()) + ]; + var source = new StringBuilder($$""" + import CIL.CallSite + open CIL CIL.Extraction + + /-- The extractor and the field binder use the same assembly identity. -/ + theorem fieldRuntimeAssembly : + ({{PlatformAssemblies.SystemRuntime.ToLean()}} : AssemblyName) = runtimeAssembly := rfl + #print axioms fieldRuntimeAssembly + + """); + foreach (var (reference, nameBytes) in cases) + source.AppendLine(string.Create(CultureInfo.InvariantCulture, $""" + #guard + let reference : FieldRef := {reference.ToLean()} + reference.owner == ({reference.Owner}) && reference.index == {reference.Index} && + reference.type == ({reference.Type}) && + reference.name.bytes? == some [{string.Join(", ", nameBytes)}] + """)); + var directory = At("artifacts/Identities"); + Directory.CreateDirectory(directory); + var leanPath = Path.Combine(directory, "FieldReferences.lean"); + File.WriteAllText(leanPath, source.ToString()); + Run(["lake", "env", "lean", leanPath], cwd: FixturePackage()); + Console.WriteLine($"PASS {cases.Length} rendered field references elaborate to their records"); +} + +// The rows of an image read with System.Reflection.Metadata, which shares no decoding with Cecil. +static void CheckRows(string path, ModuleDefinition module) +{ + using var image = new PEReader(File.OpenRead(path)); + var reader = Srm.PEReaderExtensions.GetMetadataReader(image); + var metadata = image.GetMetadata().GetContent(); + int strings = reader.GetHeapMetadataOffset(Srm.Ecma335.HeapIndex.String); + MetadataName Name(Srm.StringHandle handle) + { + int start = strings + MetadataTokens.GetHeapOffset(handle); + return MetadataName.FromBytes(metadata.AsSpan()[start..metadata.IndexOf((byte)0, start)]); + } + void Require(bool holds, string row) + { + if (!holds) throw new InvalidOperationException($"{Path.GetFileName(path)}: record differs from row {row}"); + } + foreach (var handle in reader.TypeDefinitions) + { + var row = reader.GetTypeDefinition(handle); + var definition = (TypeDefinition)module.LookupToken(MetadataTokens.GetToken(handle)); + Require(TypeLevel.Of(definition, FixtureImages.For(module)) == + new TypeLevel(Name(row.Namespace), Name(row.Name)), definition.FullName); + } + foreach (var handle in reader.TypeReferences) + { + var row = reader.GetTypeReference(handle); + var reference = (TypeReference)module.LookupToken(MetadataTokens.GetToken(handle)); + Require(IdentityRows.Level(FixtureImages.For(module), reference.MetadataToken) == + new TypeLevel(Name(row.Namespace), Name(row.Name)), reference.FullName); + } + AssemblyName Row(Srm.StringHandle name, Version version, Srm.StringHandle culture, bool hasKey, + Srm.BlobHandle key) + { + var bytes = reader.GetBlobBytes(key); + // ECMA-335 II.6.2.1.3: a token is the last eight bytes of the key's SHA-1 hash, reversed. + var token = hasKey ? SHA1.HashData(bytes)[^8..].Reverse().ToArray() : bytes; + return new(Name(name), AssemblyVersion.Of(version)!.Value, Name(culture), + token.Length == 0 ? null : System.Buffers.Binary.BinaryPrimitives.ReadUInt64BigEndian(token)); + } + var assembly = reader.GetAssemblyDefinition(); + Require(AssemblyName.Of(module.Assembly.Name, FixtureImages.For(module)) == + Row(assembly.Name, assembly.Version, assembly.Culture, + !assembly.PublicKey.IsNil, assembly.PublicKey), "Assembly"); + var references = reader.AssemblyReferences.Select(reader.GetAssemblyReference).ToArray(); + Require(references.Length == module.AssemblyReferences.Count, "AssemblyRef count"); + for (int index = 0; index < references.Length; index++) + { + var row = references[index]; + Require(AssemblyName.Of(module.AssemblyReferences[index], FixtureImages.For(module)) == + Row(row.Name, row.Version, row.Culture, + (row.Flags & System.Reflection.AssemblyFlags.PublicKey) != 0, row.PublicKeyOrToken), + "AssemblyRef " + module.AssemblyReferences[index].FullName); + } + // ECMA-335 II.24.2.4: each #US entry is a compressed length, UTF-16 code units and one final byte. + int userStrings = reader.GetHeapMetadataOffset(Srm.Ecma335.HeapIndex.UserString); + var heap = new HashSet(); + for (var handle = MetadataTokens.UserStringHandle(1); !handle.IsNil; handle = reader.GetNextHandle(handle)) + { + int start = userStrings + MetadataTokens.GetHeapOffset(handle); + var entry = metadata.AsSpan()[start..]; + int length = entry[0] < 0x80 ? entry[0] : entry[0] < 0xC0 ? ((entry[0] & 0x3F) << 8) | entry[1] : + ((entry[0] & 0x1F) << 24) | (entry[1] << 16) | (entry[2] << 8) | entry[3]; + int prefix = entry[0] < 0x80 ? 1 : entry[0] < 0xC0 ? 2 : 4; + var units = new char[(length - 1) / 2]; + for (int unit = 0; unit < units.Length; unit++) + units[unit] = (char)(entry[prefix + 2 * unit] | (entry[prefix + 2 * unit + 1] << 8)); + heap.Add(UserString.OfLiteral(new string(units))); + } + foreach (var literal in Literals(module)) + Require(heap.Contains(UserString.OfLiteral(literal)), "#US " + literal); +} + +static IEnumerable Literals(ModuleDefinition module) => module.GetTypes().SelectMany(type => type.Methods) + .Where(method => method.HasBody).SelectMany(method => method.Body.Instructions) + .Where(instruction => instruction.OpCode.Code == Code.Ldstr).Select(instruction => (string)instruction.Operand); + +static void CheckScaling() +{ + Measure("raw name payload", [512, 4096, 32768], size => + { + var bytes = new byte[size]; + Array.Fill(bytes, (byte)0xFF); + var name = MetadataName.FromBytes(bytes); + return name.ToLean().Length + name.ToDisplay().Length + name.GetHashCode(); + }); + Measure("type nesting", [64, 512, 4096], size => + { + var level = new TypeLevel(MetadataName.Empty, MetadataName.OfText("Nested.Name")); + var type = new TypeIdentity(PlatformAssemblies.SystemRuntime, MetadataName.Empty, + [.. Enumerable.Repeat(level, size)], MetadataName.OfText("Type")); + return type.ToLean().Length + type.ToDisplay().Length + type.GetHashCode(); + }); + + static void Measure(string label, int[] sizes, Func operation) + { + const int Repetitions = 4; + double previousMs = 0; + long previousBytes = 0; + foreach (int size in sizes) + { + _ = operation(size); + double bestMs = double.MaxValue; + long allocated = 0; + for (int sample = 0; sample < 3; sample++) + { + long before = GC.GetAllocatedBytesForCurrentThread(); + var timer = Stopwatch.StartNew(); + for (int iteration = 0; iteration < Repetitions; iteration++) GC.KeepAlive(operation(size)); + timer.Stop(); + bestMs = Math.Min(bestMs, timer.Elapsed.TotalMilliseconds / Repetitions); + allocated = (GC.GetAllocatedBytesForCurrentThread() - before) / Repetitions; + } + if (previousBytes != 0 && (allocated > previousBytes * 12 + 4096 || bestMs > previousMs * 16 + 10)) + throw new InvalidOperationException($"Identity {label} grows superlinearly at {size}: " + + $"{bestMs:F2} ms/{allocated} bytes after {previousMs:F2} ms/{previousBytes} bytes"); + Console.WriteLine($"PASS {label} {size}: {bestMs:F2} ms, {allocated} allocated bytes"); + previousMs = bestMs; + previousBytes = allocated; + } + } +} + +static void CheckCodeCost() +{ + double previousDisplayMs = 0; + foreach (int size in new[] { 4096, 32768, 262144, 2097152 }) + { + var bytes = new byte[size]; + Array.Fill(bytes, (byte)0xFF); + var code = IdentityText.Code(bytes); + Measure("construct", () => GC.KeepAlive(IdentityText.Code(bytes))); + Measure("hex", () => GC.KeepAlive(code.ToString("x", CultureInfo.InvariantCulture))); + double displayMs = Measure("raw-display", () => GC.KeepAlive(IdentityText.RawCode(code))); + if (size == 2097152 && displayMs > previousDisplayMs * 16 + 10) + throw new InvalidOperationException($"Raw identity display exceeds its payload scaling bound: " + + $"{size} bytes took {displayMs:F3} CPU ms after {previousDisplayMs:F3} CPU ms " + + "for one eighth the payload"); + previousDisplayMs = displayMs; + + double Measure(string label, Action action) + { + action(); + double best = double.MaxValue; + double bestCpu = double.MaxValue; + long allocated = 0; + using var process = Process.GetCurrentProcess(); + for (int repeat = 0; repeat < 3; repeat++) + { + long before = GC.GetAllocatedBytesForCurrentThread(); + process.Refresh(); + TimeSpan cpuBefore = process.TotalProcessorTime; + var timer = Stopwatch.StartNew(); + action(); + timer.Stop(); + process.Refresh(); + bestCpu = Math.Min(bestCpu, (process.TotalProcessorTime - cpuBefore).TotalMilliseconds); + best = Math.Min(best, timer.Elapsed.TotalMilliseconds); + allocated = GC.GetAllocatedBytesForCurrentThread() - before; + } + Console.WriteLine($"{label} {size}: {best:F3} ms, {bestCpu:F3} CPU ms, {allocated} allocated bytes"); + return bestCpu; + } + } +} + +// The typed platform identity accepts only the exact System.Runtime row, and refuses without throwing a token +// that is not eight bytes; Cecil reads a type named System.Int32 in another assembly with Int32's element type. +static void CheckTypedPredicates(ModuleDefinition examples, ModuleDefinition unusual) +{ + var runtime = examples.AssemblyReferences.Single(reference => + AssemblyName.Of(reference, FixtureImages.For(examples)) == PlatformAssemblies.SystemRuntime); + AssemblyNameReference Variant(Action change) + { + var copy = new AssemblyNameReference(runtime.Name, runtime.Version) + { + Culture = runtime.Culture, PublicKeyToken = runtime.PublicKeyToken + }; + change(copy); + return copy; + } + var variants = new (string Name, AssemblyNameReference Reference)[] + { + ("version", Variant(reference => reference.Version = new Version(10, 0, 0, 0))), + ("zero token", Variant(reference => reference.PublicKeyToken = new byte[8])), + ("no token", Variant(reference => reference.PublicKeyToken = [])), + ("culture", Variant(reference => reference.Culture = "en-US")), + ("name", Variant(reference => reference.Name = "System.Runtime.Extra")) + }; + foreach (var (name, reference) in variants) + if (ReadVariant(reference) is not { } variant || variant == PlatformAssemblies.SystemRuntime) + throw new InvalidOperationException("The platform identity accepts a changed " + name); + if (ReadVariant(Variant(reference => reference.PublicKeyToken = [1, 2, 3, 4])) is not null) + throw new InvalidOperationException("A four-byte public key token has an assembly name"); + if (ReadVariant(Variant(reference => reference.PublicKeyToken = new byte[8]))!.PublicKeyToken != 0 || + ReadVariant(Variant(reference => reference.PublicKeyToken = []))!.PublicKeyToken is not null) + throw new InvalidOperationException("Eight zero token bytes and no token are not told apart"); + var forged = unusual.GetType("Odd.Holder").Fields.Single(field => field.Name == "Forged").FieldType; + if (PrimitiveType.Of(forged, FixtureImages.For(unusual)) is not null) + throw new InvalidOperationException($"{forged.FullName} [{forged.Scope}] reads as a primitive"); + Console.WriteLine($"PASS the typed platform identity refuses {variants.Length} changed rows, and " + + $"[{forged.Scope.Name}]{forged.FullName}, which Cecil reads as {forged.MetadataType}, is no primitive"); +} + +static AssemblyName? ReadVariant(AssemblyNameReference reference) +{ + using var definition = ModuleDefinition.CreateModule("IdentityVariant", ModuleKind.Dll); + definition.AssemblyReferences.Add(reference); + using var stream = new MemoryStream(); + definition.Write(stream); + byte[] bytes = stream.ToArray(); + using var module = ModuleDefinition.ReadModule(new MemoryStream(bytes)); + using var pe = new PEReader(new MemoryStream(bytes)); + return AssemblyName.Of(module.AssemblyReferences[0], new MetadataImage(pe)); +} + +// Distinct invalid UTF-8 bytes may decode to the same Cecil name; typed identities retain each byte. +static void CheckRawNames(string path) +{ + byte[] original = File.ReadAllBytes(path); + using var pe = new PEReader(new MemoryStream(original)); + var reader = Srm.PEReaderExtensions.GetMetadataReader(pe); + var typeHandle = reader.TypeDefinitions.Single(handle => + reader.GetString(reader.GetTypeDefinition(handle).Name) == "Dotted.Name"); + foreach (var handle in new[] { reader.GetTypeDefinition(typeHandle).Name, reader.GetAssemblyDefinition().Name }) + { + int offset = pe.PEHeaders.MetadataStartOffset + reader.GetHeapMetadataOffset(Srm.Ecma335.HeapIndex.String) + + MetadataTokens.GetHeapOffset(handle); + (TypeIdentity Identity, string CecilName) Read(byte first) + { + byte[] changed = (byte[])original.Clone(); + changed[offset] = first; + using var module = ModuleDefinition.ReadModule(new MemoryStream(changed)); + using var image = new PEReader(new MemoryStream(changed)); + var definition = (TypeDefinition)module.LookupToken(MetadataTokens.GetToken(typeHandle)); + return (TypeIdentity.Of(definition, new MetadataImage(image)), + handle == reader.GetAssemblyDefinition().Name ? module.Assembly.Name.Name : definition.Name); + } + var first = Read(0xFE); + var second = Read(0xFF); + if (first.CecilName != second.CecilName || first.Identity == second.Identity) + throw new InvalidOperationException("Raw metadata names were aliased by decoded text"); + } + Console.WriteLine("PASS distinct malformed assembly and type names retain their raw identities"); +} + +// A second reading of an image gives equal records with equal hashes. +static void CheckEquality(string path, ReaderParameters parameters, IdentityCorpus corpus) +{ + using var again = ModuleDefinition.ReadModule(path, parameters); + var types = again.GetTypes().Select(type => TypeIdentity.Of(type, FixtureImages.For(again))).ToHashSet(); + var first = corpus.Types.ToHashSet(); + if (!types.All(first.Contains) || again.GetTypes().Count() != types.Count) + throw new InvalidOperationException("A second reading of Examples.dll gives other type identities"); + Console.WriteLine($"PASS {types.Count} type identities of a second reading equal the first"); +} + +static void CheckAssemblyCache() +{ + byte[] Create(int keyBytes, string name) + { + using var assembly = AssemblyDefinition.CreateAssembly( + new AssemblyNameDefinition(name, new Version(1, 0)), name, ModuleKind.Dll); + foreach (string referenceName in new[] { "First", "Second" }) + assembly.MainModule.AssemblyReferences.Add(new AssemblyNameReference(referenceName, new Version(1, 0)) + { PublicKey = new byte[keyBytes] }); + assembly.MainModule.AssemblyReferences.Add(new AssemblyNameReference("Invalid", new Version(1, 0)) + { PublicKeyToken = [1, 2, 3] }); + using var stream = new MemoryStream(); + assembly.Write(stream); + return stream.ToArray(); + } + + foreach (int keyBytes in new[] { 4096, 32768, 262144 }) + { + byte[] bytes = Create(keyBytes, "Cache"); + using var pe = new PEReader(new MemoryStream(bytes)); + using var module = ModuleDefinition.ReadModule(new MemoryStream(bytes)); + var image = new MetadataImage(pe); + var references = module.AssemblyReferences; + var rawReference = image.Reader.GetAssemblyReference(MetadataTokens.AssemblyReferenceHandle(1)); + if ((rawReference.Flags & System.Reflection.AssemblyFlags.PublicKey) == 0 || + image.Reader.GetBlobBytes(rawReference.PublicKeyOrToken).Length != keyBytes) + throw new InvalidOperationException("Fixture did not preserve its full public key"); + var first = AssemblyName.Of(references[0], image); + var second = AssemblyName.Of(references[1], image); + if (first is null || second is null || first == second) + throw new InvalidOperationException("Distinct assembly rows aliased"); + if (AssemblyName.Of(references[2], image) is not null) + throw new InvalidOperationException("Invalid token was admitted"); + // A lookup that allocates does so in every sample; the runtime can allocate once on this thread during + // one sample (starting a tiering worker), so the check reads the least of three. + long allocated = long.MaxValue; + var watch = Stopwatch.StartNew(); + for (int sample = 0; sample < 3; sample++) + { + long before = GC.GetAllocatedBytesForCurrentThread(); + for (int repeat = 0; repeat < 4096; repeat++) + { + if (!ReferenceEquals(first, AssemblyName.Of(references[0], image)) || + !ReferenceEquals(second, AssemblyName.Of(references[1], image)) || + AssemblyName.Of(references[2], image) is not null) + throw new InvalidOperationException("Assembly row result was not retained by its image"); + } + allocated = Math.Min(allocated, GC.GetAllocatedBytesForCurrentThread() - before); + } + if (allocated > 4096) + throw new InvalidOperationException($"Repeated assembly rows allocated {allocated} bytes"); + Console.WriteLine($"PASS assembly key {keyBytes} bytes: 12288 lookups, {allocated} bytes minimum of 3, " + + $"{watch.Elapsed.TotalMilliseconds:F3} ms"); + byte[] otherBytes = Create(keyBytes, "Other"); + using var otherPe = new PEReader(new MemoryStream(otherBytes)); + using var otherModule = ModuleDefinition.ReadModule(new MemoryStream(otherBytes)); + var otherImage = new MetadataImage(otherPe); + if (AssemblyName.Of(module.Assembly.Name, image) == AssemblyName.Of(otherModule.Assembly.Name, otherImage) || + ReferenceEquals(first, AssemblyName.Of(otherModule.AssemblyReferences[0], otherImage))) + throw new InvalidOperationException("Distinct metadata images shared a cached assembly row"); + } +} + +static void CheckHeapBounds() +{ + using var assembly = AssemblyDefinition.CreateAssembly( + new AssemblyNameDefinition("HeapBounds", new Version(1, 0)) { PublicKey = new byte[32768] }, + "HeapBounds", ModuleKind.Dll); + var module = assembly.MainModule; + var holder = new TypeDefinition("", "Holder", TypeAttributes.Public); + module.Types.Add(holder); + var method = new MethodDefinition("Literal", MethodAttributes.Public | MethodAttributes.Static, + module.TypeSystem.Void); + holder.Methods.Add(method); + method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldstr, new string('x', 32768))); + method.Body.Instructions.Add(Instruction.Create(OpCodes.Pop)); + method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); + using var stream = new MemoryStream(); + assembly.Write(stream); + byte[] original = stream.ToArray(); + using var pe = new PEReader(new MemoryStream(original)); + var metadata = new MetadataImage(pe); + + void Reject(Action action, string label) + { + try { action(); } + catch (BadImageFormatException) { return; } + throw new InvalidOperationException($"Accepted invalid heap entry: {label}"); + } + + void Check(HeapIndex heap, byte[] entry, bool valid) + { + int size = metadata.Reader.GetHeapSize(heap); + int offset = size - entry.Length; + if (heap == HeapIndex.String && !valid) + { + // SRM excludes trailing zero padding from the string heap's reported size. + int physicalEnd = metadata.Reader.GetHeapMetadataOffset(HeapIndex.UserString) - + metadata.Reader.GetHeapMetadataOffset(heap); + entry = Enumerable.Repeat((byte)0xFF, physicalEnd - offset).ToArray(); + } + byte[] changed = (byte[])original.Clone(); + int start = pe.PEHeaders.MetadataStartOffset + metadata.Reader.GetHeapMetadataOffset(heap) + offset; + entry.CopyTo(changed, start); + using var changedPe = new PEReader(new MemoryStream(changed)); + var changedMetadata = new MetadataImage(changedPe); + byte[] Read() => heap == HeapIndex.String + ? changedMetadata.StringEntry(offset) : changedMetadata.SizedEntry(heap, offset); + if (valid) + { + if (!Read().AsSpan().SequenceEqual(entry)) + throw new InvalidOperationException($"Changed exact heap bytes: {heap}/{entry.Length}"); + } + else Reject(() => Read(), $"{heap}/{Convert.ToHexString(entry)}"); + } + + Check(HeapIndex.String, [0], true); + Check(HeapIndex.String, [0xFF], false); + Reject(() => metadata.StringEntry(-1), "negative string offset"); + Reject(() => metadata.StringEntry(metadata.Reader.GetHeapSize(HeapIndex.String)), "string heap end"); + foreach (var heap in new[] { HeapIndex.Blob, HeapIndex.UserString }) + { + Reject(() => metadata.SizedEntry(heap, -1), "negative sized offset"); + Reject(() => metadata.SizedEntry(heap, metadata.Reader.GetHeapSize(heap)), "sized heap end"); + foreach (byte[] entry in new byte[][] + { [0x80], [0xC0], [0xC0, 0], [0xC0, 0, 0], [1], [0x80, 1], [0xC0, 0, 0, 1], [0xE0], [0xFF] }) + Check(heap, entry, false); + foreach (int length in new[] { 0, 1, 127, 128, 16383, 16384 }) + { + byte[] header = length < 128 ? [(byte)length] : length < 16384 + ? [(byte)(0x80 | (length >> 8)), (byte)length] + : [(byte)(0xC0 | (length >> 24)), (byte)(length >> 16), (byte)(length >> 8), (byte)length]; + Check(heap, [.. header, .. new byte[length]], true); + } + } + Reject(() => metadata.SizedEntry(HeapIndex.String, 0), "wrong sized heap"); + Console.WriteLine("PASS exact heap boundaries, malformed entries, and 1/2/4-byte lengths"); +} + +// The rows of an assembly hold what no example does: a culture, a zero token, names whose display falls back +// to their structure or escapes characters, a literal with an unpaired surrogate, and a forged System.Int32. +static void WriteUnusual(string path) +{ + var assemblyName = new AssemblyNameDefinition("Unusual", new Version(2, 1, 3, 4)) { Culture = "de-DE" }; + using var assembly = AssemblyDefinition.CreateAssembly(assemblyName, "Unusual", ModuleKind.Dll); + var module = assembly.MainModule; + var longName = string.Concat(Enumerable.Repeat("Segment", 12)); + AssemblyNameReference Reference(string name, string culture = "", byte[]? token = null) + { + var reference = new AssemblyNameReference(name, new Version(1, 0, 0, 0)) + { + Culture = culture, PublicKeyToken = token ?? [] + }; + module.AssemblyReferences.Add(reference); + return reference; + } + var other = Reference("Other", token: new byte[8]); + Reference("Bracket]" + longName); + Reference("Comma,Name"); + Reference("Spelled", "neutral"); + TypeDefinition Type(string space, string name, TypeDefinition? outer = null) + { + var type = new TypeDefinition(space, name, TypeAttributes.Abstract | TypeAttributes.Sealed | + (outer is null ? TypeAttributes.Public : TypeAttributes.NestedPublic)); + if (outer is null) module.Types.Add(type); + else outer.NestedTypes.Add(type); + return type; + } + var holder = Type("Odd", "Holder"); + holder.Fields.Add(new FieldDefinition("Forged", FieldAttributes.Public | FieldAttributes.Static, + new TypeReference("System", "Int32", module, other, true))); + Type("Odd", "Dotted.Name"); + Type("Odd/Slashed", "Name"); + Type("Odd", "{Braced"); + Type("Odd", "Back\\slash\"quote"); + Type("Odd", "Zero\u200Bwidth"); + Type("Odd", "Tag\U000E0001"); + Type("Odd", "Größe\U0001D538"); + Type("", longName + "/" + longName, Type("", "Outer", Type("Odd", "Outermost"))); + var method = new MethodDefinition("Literals", MethodAttributes.Public | MethodAttributes.Static, + module.TypeSystem.Void); + holder.Methods.Add(method); + var il = method.Body.GetILProcessor(); + foreach (var literal in new[] { "quote\" back\\ line\n", "\uD800unpaired", "\uDC00", "tag\U000E0001", + "smile\U0001F600", "{brace" }) + { + il.Emit(OpCodes.Ldstr, literal); + il.Emit(OpCodes.Pop); + } + il.Emit(OpCodes.Ret); + assembly.Write(path); + byte[] bytes = File.ReadAllBytes(path); + using (var image = new PEReader(new MemoryStream(bytes))) + { + var reader = Srm.PEReaderExtensions.GetMetadataReader(image); + for (var handle = MetadataTokens.UserStringHandle(1); !handle.IsNil; handle = reader.GetNextHandle(handle)) + { + if (reader.GetUserString(handle) != "{brace") continue; + int start = image.PEHeaders.MetadataStartOffset + + reader.GetHeapMetadataOffset(Srm.Ecma335.HeapIndex.UserString) + MetadataTokens.GetHeapOffset(handle); + if (bytes[start] >= 0x80) throw new InvalidDataException("Unexpected fixture literal size"); + bytes[start + 1] = 0; + bytes[start + 2] = 0xD8; + } + } + File.WriteAllBytes(path, bytes); +} + +/// Fixture image readers retained for the lifetime of this test process. +static class FixtureImages +{ + static readonly Dictionary images = []; + + internal static MetadataImage For(ModuleDefinition module) + { + if (images.TryGetValue(module, out var entry)) return entry.Image; + var reader = new PEReader(File.OpenRead(module.FileName)); + var image = new MetadataImage(reader); + if (image.Reader.GetGuid(image.Reader.GetModuleDefinition().Mvid) != module.Mvid) + { + reader.Dispose(); + throw new InvalidDataException("Fixture module and image have different MVIDs"); + } + images.Add(module, (reader, image)); + return image; + } +} + +/// The distinct identities of a corpus in the order they were found, and the cases they cover. +sealed class IdentityCorpus +{ + readonly List names = []; + readonly List literals = []; + readonly List versions = []; + readonly List assemblies = []; + readonly List levels = []; + readonly List types = []; + readonly List kinds = []; + readonly HashSet seen = []; + readonly HashSet primitives = []; + + internal IdentityCorpus() + { + Add(names, MetadataName.FromBytes([0xFE])); + Add(names, MetadataName.FromBytes([0xC0, 0x80])); + Add(literals, UserString.OfLiteral("\uD800")); + Add(literals, UserString.OfLiteral("\uDC00")); + } + + internal IReadOnlyList Names => names; + internal IReadOnlyList Literals => literals; + internal IReadOnlyList Versions => versions; + internal IReadOnlyList Assemblies => assemblies; + internal IReadOnlyList Levels => levels; + internal IReadOnlyList Types => types; + internal IReadOnlyList Kinds => kinds; + + void Add(List list, T item) where T : notnull + { + if (seen.Add(item)) list.Add(item); + } + + /// Every identity a module's rows name; with , also the definitions its + /// type references resolve to. + internal void Add(ModuleDefinition module, bool resolve) + { + Add(AssemblyName.Of(module.Assembly.Name, FixtureImages.For(module))!); + foreach (var reference in module.AssemblyReferences) + Add(AssemblyName.Of(reference, FixtureImages.For(module))!); + foreach (var definition in module.GetTypes()) Add(definition); + if (resolve) + foreach (var reference in module.GetTypeReferences()) + Add(reference.Resolve() ?? throw new InvalidOperationException("Unresolved " + reference.FullName)); + foreach (var field in module.GetTypes().SelectMany(type => type.Fields)) AddPrimitives(field.FieldType); + foreach (var method in module.GetTypes().SelectMany(type => type.Methods)) + { + foreach (var type in method.Parameters.Select(parameter => parameter.ParameterType) + .Append(method.ReturnType).Concat(method.HasBody ? method.Body.Variables.Select(local => + local.VariableType) : [])) + AddPrimitives(type); + if (method.HasBody) + foreach (var instruction in method.Body.Instructions.Where(item => item.OpCode.Code == Code.Ldstr)) + Add(literals, UserString.OfLiteral((string)instruction.Operand)); + } + } + + // The primitives a signature type names, in its element and generic argument types too. + void AddPrimitives(TypeReference type) + { + if (type is GenericInstanceType generic) + foreach (var argument in generic.GenericArguments) AddPrimitives(argument); + // A function pointer type has no element type; its signature names the types. + if (type is FunctionPointerType pointer) + foreach (var part in pointer.Parameters.Select(parameter => parameter.ParameterType) + .Append(pointer.ReturnType)) AddPrimitives(part); + else if (type is TypeSpecification specification) AddPrimitives(specification.ElementType); + else if (PrimitiveType.Of(type, FixtureImages.For(type.Module)) is { } primitive) primitives.Add(primitive); + } + + void Add(AssemblyName assembly) + { + Add(assemblies, assembly); + Add(versions, assembly.Version); + Add(names, assembly.Name); + Add(names, assembly.Culture); + } + + void Add(TypeDefinition definition) + { + var type = TypeIdentity.Of(definition, FixtureImages.For(definition.Module)); + Add(types, type); + Add(type.Assembly); + foreach (var level in (TypeLevel[])[.. type.Enclosing, new TypeLevel(type.Namespace, type.Name)]) + { + Add(levels, level); + Add(names, level.Namespace); + Add(names, level.Name); + } + Add(kinds, TypeKind.Of(definition, FixtureImages.For(definition.Module))); + } + + /// Fails naming each case no identity of the corpus covers. + internal void RequireCoverage() + { + (string Case, bool Covered)[] cases = + [ + .. PrimitiveType.All.Select(primitive => ("primitive " + primitive.ToDisplay(), + primitives.Contains(primitive))), + ("value type", kinds.OfType().Any()), + ("enumeration", kinds.OfType().Any()), + ("class", kinds.OfType().Any()), + ("interface", kinds.OfType().Any()), + ("public key token", assemblies.Any(assembly => assembly.PublicKeyToken is > 0UL)), + ("zero public key token", assemblies.Any(assembly => assembly.PublicKeyToken is 0UL)), + ("no public key token", assemblies.Any(assembly => assembly.PublicKeyToken is null)), + ("culture", assemblies.Any(assembly => assembly.Culture != MetadataName.Empty)), + ("assembly shown as its structure", assemblies.Any(assembly => assembly.ToDisplay().StartsWith('{'))), + ("nested type", types.Any(type => !type.Enclosing.IsEmpty)), + ("type in the global namespace", types.Any(type => type.Namespace == MetadataName.Empty)), + ("type shown as its structure", types.Any(type => type.ToDisplay().StartsWith('{'))), + ("structure laid out on several lines", types.Any(type => type.ToDisplay().Contains('\n'))), + ("escaped name", names.Any(name => name.ToDisplay().Contains("\\u", StringComparison.Ordinal))), + ("name shown as its code", + names.Any(name => name.ToDisplay().StartsWith("{ code", StringComparison.Ordinal))), + ("literal shown as its code", + literals.Any(literal => literal.ToDisplay().StartsWith("{ code", StringComparison.Ordinal))) + ]; + var missing = cases.Where(item => !item.Covered).Select(item => item.Case).ToArray(); + if (missing.Length != 0) + throw new InvalidOperationException("The identity corpus covers no " + string.Join(", ", missing)); + } + + /// The number of identities of each record. + internal string Summary() => $"{names.Count} names, {literals.Count} literals, {versions.Count} versions, " + + $"{assemblies.Count} assemblies, {levels.Count} levels, {types.Count} types, " + + $"{PrimitiveType.All.Length} primitives and {kinds.Count} kinds"; +} diff --git a/Tests/Identity.lean b/Tests/Identity.lean new file mode 100644 index 000000000..1e7783a3e --- /dev/null +++ b/Tests/Identity.lean @@ -0,0 +1,697 @@ +import CIL.Identity +import Std.Data.TreeMap + +/-! +# Typed identity tests + +Pins how identities print, compare and hash: the display text of each kind and its fallbacks, +`name!` and `str!` round trips and escapes, goals and `#print` output in `name!` and `type!` +form, raw structures for codes that spell no text or hide a component, the codec theorems and +their axioms, and equality decided on codes, including types whose dotted text coincides and +nested types that differ in one level's namespace. +-/ + +namespace Tests.Identity + +open CIL + +/-- The core library assembly of .NET 11. -/ +def runtime : AssemblyName := + { name := name!"System.Runtime", version := ⟨11, 0, 0, 0⟩, culture := name!"", + publicKeyToken := some 0xb03f5f7f11d50a3a } + +/-- An application assembly with a neutral culture and a null public key token. -/ +def examples : AssemblyName := + { name := name!"Examples", version := ⟨1, 0, 0, 0⟩, culture := name!"", + publicKeyToken := none } + +/-- A type in a namespace. -/ +def pointType : TypeIdentity := + { assembly := examples, typeNamespace := name!"Examples", name := name!"Point" } + +/-- A generic type nested in a generic type; as C# emits it, only the outer one has a namespace. -/ +def enumeratorType : TypeIdentity := + { assembly := runtime, typeNamespace := name!"", + enclosing := [⟨name!"System.Collections.Generic", name!"List`1"⟩], name := name!"Enumerator" } + +-- Each kind displays as a reader writes it. +/-- info: "System.Collections" -/ +#guard_msgs in +#eval (name!"System.Collections").display + +/-- info: "a \"quoted\" line" -/ +#guard_msgs in +#eval (str!"a \"quoted\" line").display + +/-- info: "11.0.0.0" -/ +#guard_msgs in +#eval runtime.version.display + +/-- info: "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" -/ +#guard_msgs in +#eval runtime.display + +/-- info: "Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" -/ +#guard_msgs in +#eval examples.display + +/-- info: "Examples, Version=1.0.0.0, Culture=en-US, PublicKeyToken=000000000000beef" -/ +#guard_msgs in +#eval ({ examples with culture := name!"en-US", publicKeyToken := some 0xbeef }).display + +-- A type shows its whole assembly scope: name and version, and culture and token when either +-- is not neutral or null. +/-- info: "[Examples,1.0.0.0]Examples.Point" -/ +#guard_msgs in +#eval pointType.display + +#guard enumeratorType.display == "[System.Runtime,11.0.0.0,neutral,b03f5f7f11d50a3a]" ++ + "System.Collections.Generic.List`1/Enumerator" + +/-- info: "[Examples,1.0.0.0,en-US,null]Global" -/ +#guard_msgs in +#eval ({ assembly := { examples with culture := name!"en-US" }, typeNamespace := name!"", + name := name!"Global" } : TypeIdentity).display + +/-- info: "System.Int32" -/ +#guard_msgs in +#eval PrimitiveType.int32.display + +/-- info: 8 -/ +#guard_msgs in +#eval PrimitiveType.int32.elementType + +/-- info: CIL.TypeKind.enumType (CIL.PrimitiveType.uint16) -/ +#guard_msgs in +#eval TypeKind.enumType .uint16 + +-- An unapplied constructor prints unchanged; only applied literals take the notation. +/-- info: MetadataName.mk : Nat → MetadataName -/ +#guard_msgs in +#check (MetadataName.mk) + +/-- info: UserString.mk : Nat → UserString -/ +#guard_msgs in +#check (UserString.mk) + +-- Under `open CIL` the type of type identities is unambiguous and prints unqualified. +/-- info: fun type => type.name : TypeIdentity → MetadataName -/ +#guard_msgs in +#check fun type : TypeIdentity => type.name + +-- Terms print in the notation that wrote them. +/-- info: name!"Int32" : MetadataName -/ +#guard_msgs in +#check name!"Int32" + +/-- info: str!"hello" : UserString -/ +#guard_msgs in +#check str!"hello" + +/-- info: type!"[Examples,1.0.0.0]Examples.Point" : TypeIdentity -/ +#guard_msgs in +#check ({ assembly := examples, typeNamespace := name!"Examples", name := name!"Point" } : + TypeIdentity) + +/-- A signed assembly with a short name, so its types print within one line. -/ +def signed : AssemblyName := + { name := name!"Lib", version := ⟨4, 2, 0, 1⟩, culture := name!"", + publicKeyToken := some 0xb03f5f7f11d50a3a } + +/-- info: type!"[Lib,4.2.0.1,neutral,b03f5f7f11d50a3a]N.Outer/Inner" : TypeIdentity -/ +#guard_msgs in +#check ({ assembly := signed, typeNamespace := name!"", enclosing := [⟨name!"N", name!"Outer"⟩], + name := name!"Inner" } : TypeIdentity) + +/-- info: def Tests.Identity.pointType : TypeIdentity := +type!"[Examples,1.0.0.0]Examples.Point" -/ +#guard_msgs in +#print pointType + +-- Another pinned version of the assembly prints as another type. +/-- info: type!"[Examples,2.0.0.0]Examples.Point" : TypeIdentity -/ +#guard_msgs in +#check ({ assembly := { name := name!"Examples", version := ⟨2, 0, 0, 0⟩, culture := name!"", + publicKeyToken := none }, + typeNamespace := name!"Examples", name := name!"Point" } : TypeIdentity) + +-- A goal shows a closed type in `type!` form and a named one by its name. +/-- +trace: type : TypeIdentity +found : type = type!"[Examples,1.0.0.0]Examples.Point" +⊢ type = pointType +--- +trace: ⊢ type!"[Examples,1.0.0.0]Examples.Point" = pointType +-/ +#guard_msgs in +example (type : TypeIdentity) + (found : type = + { assembly := examples, typeNamespace := name!"Examples", name := name!"Point" }) : + type = pointType := by + trace_state + subst found + trace_state + rfl + +-- A type whose assembly has a free version, culture or token keeps its structure, so the free +-- component stays visible. +/-- +trace: version : Version +culture : MetadataName +token : Option Nat +type : TypeIdentity +freeVersion : type = { assembly := { name := name!"E", version := version, culture := name!"", + publicKeyToken := none }, typeNamespace := name!"", name := name!"P" } +freeCulture : type = { assembly := { name := name!"E", version := { major := 1, minor := 0, + build := 0, revision := 0 }, culture := culture, publicKeyToken := none }, + typeNamespace := name!"", name := name!"P" } +freeToken : type = { assembly := { name := name!"E", version := { major := 1, minor := 0, + build := 0, revision := 0 }, culture := name!"", publicKeyToken := token }, + typeNamespace := name!"", name := name!"P" } +⊢ True +-/ +#guard_msgs (whitespace := lax) in +set_option linter.unusedVariables false in +example (version : Version) (culture : MetadataName) (token : Option Nat) (type : TypeIdentity) + (freeVersion : type = + { assembly := { name := name!"E", version, culture := name!"", publicKeyToken := none }, + typeNamespace := name!"", name := name!"P" }) + (freeCulture : type = + { assembly := { name := name!"E", version := ⟨1, 0, 0, 0⟩, culture, + publicKeyToken := none }, + typeNamespace := name!"", name := name!"P" }) + (freeToken : type = + { assembly := { name := name!"E", version := ⟨1, 0, 0, 0⟩, culture := name!"", + publicKeyToken := token }, + typeNamespace := name!"", name := name!"P" }) : + True := by + trace_state + trivial + +-- Text that would be ambiguous falls back to the structure: an assembly name or culture with +-- `,` or `]`, a culture spelled `neutral`, a name with `.` or `/`, a namespace with `/`, and a +-- component that is not UTF-8. +/-- An unsigned neutral assembly of version 1.0.0.0 with the given name. -/ +def renamed (name : MetadataName) : AssemblyName := + { name, version := ⟨1, 0, 0, 0⟩, culture := name!"", publicKeyToken := none } + +/-- An unsigned assembly `E` of version 1.0.0.0 with the given culture. -/ +def localized (culture : MetadataName) : AssemblyName := + { name := name!"E", version := ⟨1, 0, 0, 0⟩, culture, publicKeyToken := none } + +/-- An assembly whose name holds `]`, written as a literal so the delaborator reads it. -/ +def bracketed : AssemblyName := + { name := name!"E]", version := ⟨1, 0, 0, 0⟩, culture := name!"", publicKeyToken := none } + +/-- An assembly whose culture is spelled `neutral`, written as a literal. -/ +def spelledNeutral : AssemblyName := + { name := name!"E", version := ⟨1, 0, 0, 0⟩, culture := name!"neutral", publicKeyToken := none } + +#guard (renamed name!"E").scopeText? == some "[E,1.0.0.0]" +#guard (renamed name!"E]").scopeText?.isNone +#guard (renamed name!"E,F").scopeText?.isNone +#guard (renamed ⟨511⟩).scopeText?.isNone +#guard (localized name!"en-US").scopeText? == some "[E,1.0.0.0,en-US,null]" +#guard (localized name!"en]").scopeText?.isNone +#guard (localized name!"en,US").scopeText?.isNone +#guard (localized name!"neutral").scopeText?.isNone +#guard (localized name!"neutral").display != (localized name!"").display +#guard (localized name!"neutral").display.startsWith "{ name := name!\"E\"" +#guard (renamed name!"E,F").display.startsWith "{ name := name!\"E,F\"" + +/-- info: { assembly := examples, typeNamespace := name!"N", name := name!"A/B" } : TypeIdentity -/ +#guard_msgs in +#check ({ assembly := examples, typeNamespace := name!"N", name := name!"A/B" } : TypeIdentity) + +/-- info: { assembly := examples, typeNamespace := name!"N/M", name := name!"A" } : TypeIdentity -/ +#guard_msgs in +#check ({ assembly := examples, typeNamespace := name!"N/M", name := name!"A" } : TypeIdentity) + +/-- info: { assembly := bracketed, typeNamespace := name!"N", name := name!"A" } : TypeIdentity -/ +#guard_msgs in +#check ({ assembly := bracketed, typeNamespace := name!"N", name := name!"A" } : TypeIdentity) + +/-- +info: { assembly := spelledNeutral, typeNamespace := name!"N", name := name!"A" } : TypeIdentity +-/ +#guard_msgs in +#check ({ assembly := spelledNeutral, typeNamespace := name!"N", name := name!"A" } : + TypeIdentity) + +/-- info: { assembly := examples, typeNamespace := { code := 0x1ff }, name := name!"A" } : + TypeIdentity -/ +#guard_msgs (whitespace := lax) in +#check ({ assembly := examples, typeNamespace := ⟨511⟩, name := name!"A" } : TypeIdentity) + +-- A type with a free or malformed component keeps its structure. +/-- +trace: space : MetadataName +type : TypeIdentity +free : type = { assembly := examples, typeNamespace := space, name := name!"P" } +malformed : type = { assembly := examples, typeNamespace := name!"N", name := { code := 0x2ff } } +⊢ True +-/ +#guard_msgs in +set_option linter.unusedVariables false in +example (space : MetadataName) (type : TypeIdentity) + (free : type = { assembly := examples, typeNamespace := space, name := name!"P" }) + (malformed : type = { assembly := examples, typeNamespace := name!"N", name := ⟨767⟩ }) : + True := by + trace_state + trivial + +-- A code that spells no text prints as its raw structure, never as a name. +/-- info: { code := 0x2ff } : MetadataName -/ +#guard_msgs in +#check MetadataName.mk 767 + +/-- info: { code := 0x0 } : MetadataName -/ +#guard_msgs in +#check MetadataName.mk 0 + +/-- info: "{ code := 0x2ff }" -/ +#guard_msgs in +#eval (MetadataName.mk 767).display + +-- 511 is the byte 0xFF after the leading 1: a well-formed code whose byte is not UTF-8. +/-- info: { code := 0x1ff } : MetadataName -/ +#guard_msgs in +#check MetadataName.mk 511 + +-- 120832 is the code unit 0xD800 after the leading 1: an unpaired high surrogate. +/-- info: { code := 0x1d800 } : UserString -/ +#guard_msgs in +#check UserString.mk 120832 + +/-- info: "{ code := 0x1d800 }" -/ +#guard_msgs in +#eval (UserString.mk 120832).display + +-- The compiled UTF-8 decision refuses an overlong form (C0 80) and an encoded surrogate +-- (ED A0 80), and a lone low surrogate (DC00) is no UTF-16 text. +/-- info: { code := 0x1c080 } : MetadataName -/ +#guard_msgs in +#check MetadataName.mk 114816 + +/-- info: { code := 0x1eda080 } : MetadataName -/ +#guard_msgs in +#check MetadataName.mk 32350336 + +/-- info: { code := 0x1dc00 } : UserString -/ +#guard_msgs in +#check UserString.mk 121856 + +example : MetadataName.ofBytes? [0xC0, 0x80] = some ⟨114816⟩ := by decide +kernel +example : MetadataName.ofBytes? [0xED, 0xA0, 0x80] = some ⟨32350336⟩ := by decide +kernel +example : UserString.ofUnits? [0xDC00] = some ⟨121856⟩ := by decide +kernel + +-- Codes that are zero or whose leading digit is not 1 read back as nothing. +example : (MetadataName.mk 0).bytes? = none := by decide +kernel +example : (MetadataName.mk 2).bytes? = none := by decide +kernel +example : (MetadataName.mk 767).bytes? = none := by decide +kernel +example : UserString.ofUnits? [65536] = none := by decide +kernel + +-- Byte lists with the same digits and different lengths give different names. +example : MetadataName.ofBytes? [0] ≠ MetadataName.ofBytes? [0, 0] := by decide +kernel +example : MetadataName.ofBytes? [65] ≠ MetadataName.ofBytes? [0, 65] := by decide +kernel + +-- An escaped quote and backslash reach the name as the bytes they stand for. +example : (name!"a\"b\\c").bytes? = some [97, 34, 98, 92, 99] := by decide +kernel + +-- Under `pp.all` a name is its constructor applied to its code, with no text. +/-- +info: CIL.MetadataName.mk (@OfNat.ofNat.{0} Nat (nat_lit 321) (instOfNatNat (nat_lit 321))) + : CIL.MetadataName +-/ +#guard_msgs (whitespace := lax) in +set_option pp.all true in +#check name!"A" + +-- The element type codes of ECMA-335 II.23.1.16. +example : [PrimitiveType.boolean, .char, .sbyte, .byte, .int16, .uint16, .int32, .uint32, .int64, + .uint64, .single, .double, .intPtr, .uintPtr].map PrimitiveType.elementType = + [0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x18, 0x19] := by + decide + +-- No metadata name holds a NUL: `name!` refuses one, and the code of the byte 0 prints raw. +/-- error: name!: a metadata name cannot hold a NUL character -/ +#guard_msgs in +#check name!"a\x00" + +/-- info: { code := 0x100 } : MetadataName -/ +#guard_msgs in +#check MetadataName.mk 256 + +-- A malformed code and a name whose text spells its raw structure display differently. +#guard (MetadataName.mk 767).display == "{ code := 0x2ff }" +#guard (name!"{ code := 0x2ff }").display == "\\{ code := 0x2ff }" +#guard (UserString.mk 120832).display != (str!"{ code := 0x1d800 }").display + +-- Escapes in printed text: a backslash, and control, format, separator and non-ASCII space +-- characters, each of which would otherwise print as nothing or as a space. +#guard (name!"a\\b").display == "a\\\\b" +#guard (name!"a\u200Bb").display == "a\\u200Bb" +#guard (str!"line\nnext").display == "line\\u000Anext" + +/-- info: name!"a\"b\\c" : MetadataName -/ +#guard_msgs in +#check name!"a\"b\\c" + +-- A `#guard_msgs` docstring reads `\uHHHH` of a visible-width character as the character, so +-- these escapes are pinned on the literal text that `Repr` and the delaborators print. +#guard reprStr name!"Cc\u0085 Cf\u202E Zl\u2028 Zp\u2029 Zs\u00A0" == + "name!\"Cc\\u0085 Cf\\u202E Zl\\u2028 Zp\\u2029 Zs\\u00A0\"" +#guard (str!"zero\x00width\u200B").literal? == some "\"zero\\u0000width\\u200B\"" +#guard ({ assembly := examples, typeNamespace := name!"N", name := name!"A\u200B" } : + TypeIdentity).text? == some "[Examples,1.0.0.0]N.A\\u200B" + +/-- info: str!"tab\u0009zero\u0000next\u0085" : UserString -/ +#guard_msgs in +#check str!"tab\tzero\x00next\u0085" + +-- U+E0041, a tag character, is invisible and above U+FFFF, where a Lean literal has no escape, +-- so a name holding it prints raw although its bytes are valid UTF-8. +#guard MetadataName.ofBytes? [0xF3, 0xA0, 0x81, 0x81] == some ⟨8382349697⟩ +#guard (MetadataName.mk 8382349697).text?.isSome + +/-- info: { code := 0x1f3a08181 } : MetadataName -/ +#guard_msgs in +#check MetadataName.mk 8382349697 + +#guard (MetadataName.mk 8382349697).display == "{ code := 0x1f3a08181 }" + +-- `type!` is display only. +/-- error: type!"..." is display only; write the TypeIdentity structure -/ +#guard_msgs in +#check type!"[Examples]Examples.Point" + +-- `name!` and `str!` round trip through the bytes and code units they encode. +example : (name!"Int32").bytes? = some [73, 110, 116, 51, 50] := by decide +kernel +example : MetadataName.ofBytes? [73, 110, 116, 51, 50] = some name!"Int32" := by decide +kernel +example : (name!"é").bytes? = some [0xC3, 0xA9] := by decide +kernel +example : (name!"").bytes? = some [] := by decide +kernel +example : name!"" = ⟨1⟩ := by decide +kernel +example : MetadataName.ofBytes? [256] = none := by decide +kernel +example : (str!"A😀").units? = some [0x41, 0xD83D, 0xDE00] := by decide +kernel + +-- A printed name or literal determines its value: reading back and building are inverse. +example (name : MetadataName) (bytes : List Nat) (decoded : name.bytes? = some bytes) : + MetadataName.ofBytes? bytes = some name := MetadataName.ofBytes?_bytes? decoded +example (literal : UserString) (units : List Nat) (decoded : literal.units? = some units) : + UserString.ofUnits? units = some literal := UserString.ofUnits?_units? decoded + +-- Every codec theorem rests on the standard axioms alone. +/-- +info: 'CIL.MetadataName.ofBytes?_injective' depends on axioms: + [propext, Classical.choice, Quot.sound] +-/ +#guard_msgs (whitespace := lax) in +#print axioms MetadataName.ofBytes?_injective + +/-- +info: 'CIL.MetadataName.bytes?_ofBytes?' depends on axioms: [propext, Classical.choice, Quot.sound] +-/ +#guard_msgs in +#print axioms MetadataName.bytes?_ofBytes? + +/-- +info: 'CIL.MetadataName.ofBytes?_bytes?' depends on axioms: [propext, Quot.sound] +-/ +#guard_msgs in +#print axioms MetadataName.ofBytes?_bytes? + +/-- +info: 'CIL.UserString.ofUnits?_injective' depends on axioms: + [propext, Classical.choice, Quot.sound] +-/ +#guard_msgs (whitespace := lax) in +#print axioms UserString.ofUnits?_injective + +/-- +info: 'CIL.UserString.units?_ofUnits?' depends on axioms: [propext, Classical.choice, Quot.sound] +-/ +#guard_msgs in +#print axioms UserString.units?_ofUnits? + +/-- +info: 'CIL.UserString.ofUnits?_units?' depends on axioms: [propext, Quot.sound] +-/ +#guard_msgs in +#print axioms UserString.ofUnits?_units? + +/-- info: name!"é" : MetadataName -/ +#guard_msgs in +#check name!"é" + +/-- info: name!"" : MetadataName -/ +#guard_msgs in +#check name!"" + +/-- info: str!"A😀" : UserString -/ +#guard_msgs in +#check str!"A😀" + +-- Equality is decided on codes and components. +example : name!"Int32" ≠ name!"Int64" := by decide +kernel +example : name!"" ≠ ⟨256⟩ := by decide +kernel +example : pointType = + { assembly := examples, typeNamespace := name!"Examples", name := name!"Point" } := by + decide +kernel +-- The same names defined in another pinned version of the assembly are another type. +example : pointType ≠ { pointType with assembly := { examples with version := ⟨2, 0, 0, 0⟩ } } := + by decide +kernel +example : examples ≠ { examples with culture := name!"en-US" } := by decide +kernel +example : examples ≠ { examples with publicKeyToken := some 0 } := by decide +kernel +example : compare name!"A" name!"B" = .lt := by decide +kernel + +-- A hash depends on every byte or code unit, not only on the last 64 bits of the code. +#guard hash name!"ArgumentException" != hash name!"FormatException" +#guard hash str!"xABCD" != hash str!"yABCD" +#guard hash pointType != hash { pointType with name := name!"Points" } + +-- Namespace `A.B` with name `C` and namespace `A` with name `B.C` are different types. +/-- The type `C` in namespace `A.B`. -/ +def dottedNamespace : TypeIdentity := + { assembly := examples, typeNamespace := name!"A.B", name := name!"C" } + +/-- The type `B.C` in namespace `A`. -/ +def dottedName : TypeIdentity := + { assembly := examples, typeNamespace := name!"A", name := name!"B.C" } + +example : dottedNamespace ≠ dottedName := by decide +kernel + +-- Both would read `[Examples,1.0.0.0]A.B.C`, so only the first prints and displays as text. +#guard dottedNamespace.display == "[Examples,1.0.0.0]A.B.C" +#guard dottedName.display.startsWith "{ assembly := { name := name!\"Examples\"" + +/-- info: type!"[Examples,1.0.0.0]A.B.C" : TypeIdentity -/ +#guard_msgs in +#check ({ assembly := examples, typeNamespace := name!"A.B", name := name!"C" } : TypeIdentity) + +/-- +info: { assembly := examples, typeNamespace := name!"A", name := name!"B.C" } : TypeIdentity +-/ +#guard_msgs in +#check ({ assembly := examples, typeNamespace := name!"A", name := name!"B.C" } : TypeIdentity) + +-- A nested type differs from a type whose name holds the enclosing path, and from a deeper one. +example : enumeratorType ≠ + { enumeratorType with enclosing := [], name := name!"List`1/Enumerator" } := by + decide +kernel +example : enumeratorType ≠ { enumeratorType with + enclosing := enumeratorType.enclosing ++ [⟨name!"", name!"Enumerator"⟩] } := by + decide +kernel + +-- Each level of a nesting chain keeps its namespace column: nested types that differ only in an +-- enclosing level's namespace, or only in their own, are different types and print differently. +/-- `Outer/Inner` with the outer type in namespace `N`. -/ +def innerType : TypeIdentity := + { assembly := examples, typeNamespace := name!"", enclosing := [⟨name!"N", name!"Outer"⟩], + name := name!"Inner" } + +example : innerType ≠ { innerType with enclosing := [⟨name!"M", name!"Outer"⟩] } := by + decide +kernel +example : innerType ≠ { innerType with typeNamespace := name!"N" } := by decide +kernel + +/-- info: "[Examples,1.0.0.0]N.Outer/Inner" -/ +#guard_msgs in +#eval innerType.display + +/-- info: "[Examples,1.0.0.0]M.Outer/Inner" -/ +#guard_msgs in +#eval ({ innerType with enclosing := [⟨name!"M", name!"Outer"⟩] }).display + +/-- info: "[Examples,1.0.0.0]N.Outer/N.Inner" -/ +#guard_msgs in +#eval ({ innerType with typeNamespace := name!"N" }).display + +/-- info: type!"[Examples,1.0.0.0]N.Outer/N.Inner" : TypeIdentity -/ +#guard_msgs in +#check ({ assembly := examples, typeNamespace := name!"N", enclosing := [⟨name!"N", name!"Outer"⟩], + name := name!"Inner" } : TypeIdentity) + +/-- Boundary codes and payload digits agree with the mathematical radix specification. -/ +def checkIdentityCodec : IO Unit := do + for bits in [8, 16] do + let base := 2^bits + for code in List.range 1024 ++ + [base - 1, base, base + 1, 2 * base - 1, 2 * base, base^2 - 1, + base^2, base^2 + 1, 2 * base^2 - 1, 2 * base^2] do + unless bitDigits? bits code == radixDigits? base code code [] do + throw (IO.userError s!"decoder admission differs: width {bits}, code {code}") + for digits in [[], [0], [0, 0], [base - 1], [0, base - 1, 0], + [base - 1, 0, base - 1], [base], [0, base], [base + 1, 0]] do + unless bitCode? bits digits == radixCode? base digits 1 do + throw (IO.userError s!"encoder differs: width {bits}, digits {digits}") + if let some code := bitCode? bits digits then + unless bitDigits? bits code == some digits do + throw (IO.userError s!"payload digits differ: width {bits}, digits {digits}") + for seed in [0, 1, 0x0123456789abcdef] do + for code in [0, 1, 2^64 - 1, 2^64, 2^64 + 1, 2^128 - 1, + 2^128, 2^128 + 1, 2^192 + 2^64 + 7] do + unless mixCodeLimbs seed code == mixCodeLimbsSpec seed code do + throw (IO.userError s!"hash limbs differ: seed {seed}, code {code}") + +#eval checkIdentityCodec + +/-- Separate runtime measurements cover encoding, decoding and hashing of growing payloads. -/ +def checkIdentityCodecCost : IO Unit := do + for bits in [8, 16] do + let mut previous : Array Nat := #[0, 0, 0] + for size in [8192, 65536, 524288] do + let mut best : Array Nat := #[1000000000000, 1000000000000, 1000000000000] + let mut checksum : UInt64 := 0 + for sample in [:3] do + let digits := (List.range (size * 8 / bits)).map + (fun index => (index + sample) % (2^bits)) + let started ← IO.monoNanosNow + let some code := bitCode? bits digits | throw (IO.userError "bounded encoder rejected") + let encoded ← IO.monoNanosNow + let decoded := bitDigits? bits code + let finished ← IO.monoNanosNow + let hashed := mixCodeLimbs (UInt64.ofNat sample) code + let ended ← IO.monoNanosNow + unless decoded == some digits do + throw (IO.userError "growing payload did not round trip") + checksum := checksum ^^^ hashed + let times := #[encoded - started, finished - encoded, ended - finished] + for index in [:3] do best := best.set! index (min best[index]! times[index]!) + for index in [:3] do + if previous[index]! > 0 && best[index]! > previous[index]! * 16 + 2000000 then + throw (IO.userError (s!"codec cost grew too quickly: width {bits}, size {size}, " ++ + s!"operation {index}, {previous[index]!} -> {best[index]!} ns")) + IO.println (s!"PASS codec width {bits}, bytes {size}: " ++ + s!"encode/decode/hash ns {best}, {checksum}") + previous := best + +#eval checkIdentityCodecCost + +/-- Every component takes precedence over all following components. -/ +def checkAssemblyComponentOrder : IO Unit := do + let lower : AssemblyName := ⟨⟨1⟩, ⟨1, 1, 1, 1⟩, ⟨1⟩, none⟩ + let upper : AssemblyName := ⟨⟨2⟩, ⟨2, 2, 2, 2⟩, ⟨2⟩, some 0⟩ + let pairs : Array (AssemblyName × AssemblyName) := #[ + ({ upper with name := lower.name }, { lower with name := upper.name }), + ({ upper with name := lower.name, version := ⟨1, 2, 2, 2⟩ }, + { lower with version := ⟨2, 1, 1, 1⟩ }), + (⟨⟨1⟩, ⟨1, 1, 2, 2⟩, ⟨2⟩, some 0⟩, { lower with version := ⟨1, 2, 1, 1⟩ }), + (⟨⟨1⟩, ⟨1, 1, 1, 2⟩, ⟨2⟩, some 0⟩, { lower with version := ⟨1, 1, 2, 1⟩ }), + ({ lower with culture := upper.culture, publicKeyToken := upper.publicKeyToken }, + { lower with version := ⟨1, 1, 1, 2⟩ }), + ({ lower with publicKeyToken := upper.publicKeyToken }, + { lower with culture := upper.culture }), + (lower, { lower with publicKeyToken := some 0 }), + ({ lower with publicKeyToken := some 0 }, { lower with publicKeyToken := some 1 })] + for (left, right) in pairs do + unless compare left right == .lt && compare right left == .gt do + throw (IO.userError "assembly component precedence changed") + +#eval checkAssemblyComponentOrder + +/-- Text delimiters, invalid UTF-8, culture and null-token distinctions survive ordered lookup. -/ +def assemblyMapKeys : Array AssemblyName := #[examples, + { examples with publicKeyToken := some 0 }, + { examples with culture := name!"neutral" }, + { examples with name := name!"A, Version=1.0.0.0" }, + { examples with name := name!"A", culture := name!"Version=1.0.0.0" }, + { examples with name := ⟨0x1ff⟩ }, { examples with name := ⟨0x1fe⟩ }] + +/-- All distinct assembly components remain separate map entries with their original values. -/ +def checkAssemblyMap : IO Unit := do + let mut entries : Std.TreeMap AssemblyName Nat := ∅ + for bound : index in [:assemblyMapKeys.size] do + entries := entries.insert assemblyMapKeys[index] index + unless entries.size == assemblyMapKeys.size do + throw (IO.userError "distinct assembly keys collapsed") + for bound : index in [:assemblyMapKeys.size] do + unless entries[assemblyMapKeys[index]]? == some index do + throw (IO.userError "assembly lookup returned another identity") + unless entries[{ examples with version := ⟨9, 0, 0, 0⟩ }]? == none do + throw (IO.userError "absent assembly key was found") + entries := entries.insert examples 99 + unless entries.size == assemblyMapKeys.size && entries[examples]? == some 99 do + throw (IO.userError "equal assembly key did not replace its value") + +#eval checkAssemblyMap + +/-- Equal comparison implies exact identity, including optional tokens and undecodable names. -/ +theorem assemblyMapComparison (left right : AssemblyName) : + compare left right = .eq ↔ left = right := + AssemblyName.compare_eq_iff left right + +/-- info: 'Tests.Identity.assemblyMapComparison' depends on axioms: +[propext, Classical.choice, Quot.sound] -/ +#guard_msgs in +#print axioms assemblyMapComparison + +/-- Ordered lookup uses transitive raw component comparisons. -/ +theorem assemblyComparisonTransitive (first second third : AssemblyName) + (firstBefore : compare first second = .lt) (secondBefore : compare second third = .lt) : + compare first third = .lt := + Std.TransCmp.lt_trans firstBefore secondBefore + +/-- info: 'Tests.Identity.assemblyComparisonTransitive' depends on axioms: +[propext, Classical.choice, Quot.sound] -/ +#guard_msgs in +#print axioms assemblyComparisonTransitive + +/-- Prebuilt names share a byte prefix and differ only in their two final nonzero bytes. -/ +def assemblyCostKeys (count prefixBytes : Nat) : Array AssemblyName := Id.run do + let prefixCode := (bitCode? 8 (List.replicate prefixBytes 97)).getD 1 + return (Array.range count).map fun index => + { examples with name := ⟨(prefixCode <<< 16) + (index / 255 + 1) * 256 + index % 255 + 1⟩ } + +/-- Times map construction and successful lookup without timing identity construction. -/ +def measureAssemblyMap (keys : Array AssemblyName) : IO Nat := do + let mut best := 1000000000000 + for _ in [:3] do + let started ← IO.monoNanosNow + for _ in [:16] do + let mut entries : Std.TreeMap AssemblyName Nat := ∅ + for bound : index in [:keys.size] do entries := entries.insert keys[index] index + unless entries.size == keys.size do throw (IO.userError "growing map lost keys") + for bound : index in [:keys.size] do + unless entries[keys[index]]? == some index do + throw (IO.userError "growing map lookup failed") + best := min best ((← IO.monoNanosNow) - started) + return best + +/-- Map cost grows with key count and comparison bit length, with no text decoding. Each bound + compares the largest size with the smallest and leaves a factor of 4 for cache misses: 64 times + the keys or bytes cost at most 128 times as much here, and 4096 times as much when quadratic. -/ +def checkAssemblyMapCost : IO Unit := do + for prefixBytes in [4, 4096] do + let smallest ← measureAssemblyMap (assemblyCostKeys 64 prefixBytes) + let largest ← measureAssemblyMap (assemblyCostKeys 4096 prefixBytes) + if largest > smallest * 512 + 2000000 then + throw (IO.userError s!"assembly count cost grew too quickly: {smallest} -> {largest}") + IO.println s!"PASS assembly counts 64 and 4096, prefix bytes {prefixBytes}: {largest} ns" + let smallest ← measureAssemblyMap (assemblyCostKeys 64 4096) + let largest ← measureAssemblyMap (assemblyCostKeys 64 262144) + if largest > smallest * 256 + 2000000 then + throw (IO.userError s!"assembly bit cost grew too quickly: {smallest} -> {largest}") + IO.println s!"PASS assembly count 64, prefix bytes 4096 and 262144: {largest} ns" + +#eval checkAssemblyMapCost + +end Tests.Identity diff --git a/Tests/ImportEvidence.lean b/Tests/ImportEvidence.lean index 7e1bfad37..11e9544e1 100644 --- a/Tests/ImportEvidence.lean +++ b/Tests/ImportEvidence.lean @@ -1,10 +1,13 @@ import CIL.Extraction.Generics +import Tests.Support /-! An external call site may emit `.call` only into a matching import slot past every extracted method. -/ namespace Tests.ImportEvidence open CIL CIL.Extraction -def word : TypeExpr := .atom "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" "System.UInt64" +def word : TypeExpr := .atom + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" + "System.UInt64" def addToIdentity : String := "Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null::System.Void Library.Adder::AddTo(System.UInt64&,System.UInt64&,System.UInt64&)" def callee : RawMethod := { @@ -17,79 +20,187 @@ def methods : List SpecializedMethod := [⟨[], callee⟩] def slot : ImportSlot := { identity := addToIdentity, closureDigest := "digest", parameterCount := 3, hasThis := false, returnsValue := false } def imports : List ImportSlot := [slot] def site : CallSite := { - declaringType := .atom "Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" "Library.Adder", name := "AddTo", - target := .external addToIdentity, typeArguments := [], parameterCount := 3, hasThis := false, returnsValue := false, - returnType := .void, parameterTypes := [.byref word, .byref word, .byref word] } + declared := { convention := 0, parameters := [.byref word, .byref word, .byref word] }, + method := { + owner := .atom "Library, Version=1.0.0.0, Culture=neutral, \ + PublicKeyToken=null" "Library.Adder", name := name!"AddTo", hasThis := false, + genericArity := 0, returnType := .void, + parameters := [.byref word, .byref word, .byref word] }, + name := "AddTo", target := .external addToIdentity, typeArguments := [], parameterCount := 3, + returnsValue := false } def call (site : CallSite) : RawInstruction := ⟨8, 40, .call site⟩ -example : validateManagedCall methods imports (call site) true (.call 1 3) = true := by decide +kernel +/-- The link to method or slot `target`. -/ +def linked (target : Nat) : Option Link := some (.direct target) +/-- The plain `call` the extractor emits for `site`. -/ +def emits (site : CallSite) : Instruction := .call site.method + +example : validateManagedCall methods imports (call site) true (linked 1) (emits site) = true := by + decide +kernel -- Forged slots: identity, argument count, receiver and result shape must each match the site. -example : validateManagedCall methods [{ slot with identity := addToIdentity ++ " " }] (call site) true (.call 1 3) = false := by decide +kernel -example : validateManagedCall methods [{ slot with identity := "Other::System.Void Library.Adder::AddTo(System.UInt64&,System.UInt64&,System.UInt64&)" }] (call site) true (.call 1 3) = false := by decide +kernel -example : validateManagedCall methods [{ slot with parameterCount := 2 }] (call site) true (.call 1 3) = false := by decide +kernel -example : validateManagedCall methods [{ slot with returnsValue := true }] (call site) true (.call 1 3) = false := by decide +kernel -example : validateManagedCall methods [{ slot with hasThis := true }] (call site) true (.call 1 3) = false := by decide +kernel --- The emitted argument count must agree with the site, including the receiver. -example : validateManagedCall methods imports (call site) true (.call 1 2) = false := by decide +kernel -example : validateManagedCall methods imports (call site) true (.call 1 4) = false := by decide +kernel -example : validateManagedCall methods [{ slot with hasThis := true }] (call { site with hasThis := true }) true (.call 1 4) = true := by decide +kernel -example : validateManagedCall methods [{ slot with hasThis := true }] (call { site with hasThis := true }) true (.call 1 3) = false := by decide +kernel +example : validateManagedCall methods [{ slot with identity := addToIdentity ++ " " }] (call site) + true (linked 1) (emits site) = false := by decide +kernel +example : validateManagedCall methods [{ slot with identity := "Other::System.Void \ + Library.Adder::AddTo(System.UInt64&,System.UInt64&,System.UInt64&)" }] (call site) + true (linked 1) (emits site) = false := by decide +kernel +example : validateManagedCall methods [{ slot with parameterCount := 2 }] (call site) + true (linked 1) (emits site) = false := by decide +kernel +example : validateManagedCall methods [{ slot with returnsValue := true }] (call site) + true (linked 1) (emits site) = false := by decide +kernel +example : validateManagedCall methods [{ slot with hasThis := true }] (call site) + true (linked 1) (emits site) = false := by decide +kernel +-- The emitted call names the site's method, so its argument count is the site's, receiver +-- included. +example : validateManagedCall methods imports (call site) true (linked 1) + (.call { site.method with parameters := [.byref word, .byref word] }) = false := by + decide +kernel +example : validateManagedCall methods imports (call site) true (linked 1) + (.call { site.method with hasThis := true }) = false := by decide +kernel +example : validateManagedCall methods [{ slot with hasThis := true }] + (call { site with method.hasThis := true }) true (linked 1) + (emits { site with method.hasThis := true }) = true := by decide +kernel +example : validateManagedCall methods [{ slot with hasThis := true }] + (call { site with method.hasThis := true }) true (linked 1) (emits site) = false := by + decide +kernel -- The site's own shape is checked against the slot too. -example : validateManagedCall methods imports (call { site with returnsValue := true }) true (.call 1 3) = false := by decide +kernel -example : validateManagedCall methods imports (call { site with parameterCount := 2 }) true (.call 1 2) = false := by decide +kernel +example : validateManagedCall methods imports (call { site with returnsValue := true }) true + (linked 1) (emits site) = false := by decide +kernel +example : validateManagedCall methods imports (call { site with parameterCount := 2 }) true + (linked 1) (emits site) = false := by decide +kernel -- A slot index inside the program, or past the import table, is never an import. -example : validateManagedCall methods imports (call site) true (.call 0 3) = false := by decide +kernel -example : validateManagedCall methods imports (call site) true (.call 2 3) = false := by decide +kernel -example : validateManagedCall [] imports (call site) true (.call 0 3) = true := by decide +kernel -example : validateManagedCall [] imports (call site) true (.call 1 3) = false := by decide +kernel +example : validateManagedCall methods imports (call site) true (linked 0) (emits site) = false := + by + decide +kernel +example : validateManagedCall methods imports (call site) true (linked 2) (emits site) = false := + by + decide +kernel +example : validateManagedCall [] imports (call site) true (linked 0) (emits site) = true := by + decide +kernel +example : validateManagedCall [] imports (call site) true (linked 1) (emits site) = false := by + decide +kernel -- Without an import table the previous rule applies: an external site emits no call. -example : validateManagedCall methods [] (call site) true (.call 1 3) = false := by decide +kernel +example : validateManagedCall methods [] (call site) true (linked 1) (emits site) = false := by + decide +kernel +-- A forged link: none, or not a direct one, enters no slot. +example : validateManagedCall methods imports (call site) true none (emits site) = false := by + decide +kernel +example : validateManagedCall methods imports (call site) true (some (.virtual [(word, 1)])) + (emits site) = false := by decide +kernel +-- An import call takes no prefix: the call must not retire the caller's frame or be resolved on +-- a constrained type. +example : validateManagedCall methods imports (call site) true (linked 1) + (.call site.method { tail := true }) = false := by decide +kernel +example : validateManagedCall methods imports (call site) true (linked 1) + (.call site.method { constrained := some word }) = false := by decide +kernel --- Only a plain `call` of a closed method may target an import; constructors and generic sites may not. -example : validateManagedCall methods imports ⟨8, 115, .call site⟩ true (.call 1 3) = false := by decide +kernel -example : validateManagedCall methods imports ⟨8, 115, .call site⟩ true (.newAggregate 1 3 0) = false := by decide +kernel -example : validateManagedCall methods imports (call site) true (.newVector256 1 3) = false := by decide +kernel -example : validateManagedCall methods imports (call { site with typeArguments := [word] }) true (.call 1 3) = false := by decide +kernel +-- Only a plain `call` of a closed method may target an import; constructors and generic sites +-- may not. +example : + validateManagedCall methods imports ⟨8, 115, .call site⟩ true (linked 1) (emits site) = + false := by decide +kernel +example : validateManagedCall methods imports ⟨8, 115, .call site⟩ true (linked 1) + (.newobj site.method) = false := by decide +kernel +example : validateManagedCall methods imports (call { site with typeArguments := [word] }) true + (linked 1) (emits site) = false := by decide +kernel -- A managed site or a non-call instruction cannot reach the import range either. -example : validateManagedCall methods imports (call { site with target := .managed 100663298 }) true (.call 1 3) = false := by decide +kernel -example : validateManagedCall methods imports ⟨8, 88, .none⟩ true (.call 1 3) = false := by decide +kernel +example : validateManagedCall methods imports (call { site with target := .managed 100663298 }) + true (linked 1) (emits site) = false := by decide +kernel +example : validateManagedCall methods imports ⟨8, 88, .none⟩ true (linked 1) (emits site) = + false := by decide +kernel -- A runtime-modelled external call still emits its model, and dead code is unconstrained. -example : validateManagedCall methods imports (call site) true .add = true := by decide +kernel -example : validateManagedCall methods imports (call site) false (.call 0 3) = true := by decide +kernel +example : validateManagedCall methods imports (call site) true none .add = true := by + decide +kernel +example : validateManagedCall methods imports (call site) false (linked 0) (emits site) = true := + by + decide +kernel /-- The whole-method check: the same forged slot is rejected at the only live call. -/ def caller : RawMethod := { callee with code := [⟨0, 2, .none⟩, ⟨1, 3, .none⟩, ⟨2, 4, .none⟩, ⟨3, 40, .call site⟩, ⟨8, 42, .none⟩] } -def emitted : Method := { returnsValue := false, locals := [], code := [.arg 0, .arg 1, .arg 2, .call 1 3, .ret] } -example : ManagedCallsValid methods imports caller [true, true, true, true, true] emitted := by decide +kernel -example : ¬ ManagedCallsValid methods [{ slot with identity := "forged" }] caller [true, true, true, true, true] emitted := by decide +kernel -example : ¬ ManagedCallsValid methods [] caller [true, true, true, true, true] emitted := by decide +kernel -example : ManagedCallsValid methods [] caller [true, true, true, false, true] emitted := by decide +kernel +def emitted : Method := + { returnsValue := false, locals := [], code := [.ldarg 0, .ldarg 1, .ldarg 2, emits site, .ret], + links := [(3, .direct 1)] } +example : ManagedCallsValid methods imports (fun _ => false) caller [true, true, true, true, true] + emitted := by decide +kernel +example : ¬ ManagedCallsValid methods [{ slot with identity := "forged" }] (fun _ => false) caller + [true, true, true, true, true] emitted := by decide +kernel +example : ¬ ManagedCallsValid methods [] (fun _ => false) caller [true, true, true, true, true] + emitted := by decide +kernel +example : ManagedCallsValid methods [] (fun _ => false) caller [true, true, true, false, true] + emitted := by decide +kernel + +/-- Two calls to the slot, at pcs 3 and 7, among nops. -/ +def callerTwice : RawMethod := { callee with code := [⟨0, 0, .none⟩, ⟨1, 0, .none⟩, + ⟨2, 0, .none⟩, ⟨3, 40, .call site⟩, ⟨8, 0, .none⟩, ⟨9, 0, .none⟩, ⟨10, 0, .none⟩, + ⟨11, 40, .call site⟩, ⟨16, 42, .none⟩] } +/-- The emitted body of `callerTwice`, each call linked to the slot. -/ +def emittedTwice : Method := { + returnsValue := false, locals := [], + code := [.nop, .nop, .nop, emits site, .nop, .nop, .nop, emits site, + .ret], links := [(3, .direct 1), (7, .direct 1)] } +/-- Every pc of `callerTwice` is live. -/ +def liveTwice : List Bool := List.replicate 9 true +/-- `emittedTwice` with the link table `links`. -/ +def linkedTwice (links : List (Nat × Link)) : Method := { emittedTwice with links := links } + +-- A table is accepted when its keys ascend, sit at the call pcs and give each its call's link. +example : + ManagedCallsValid methods imports (fun _ => false) callerTwice liveTwice emittedTwice := by + decide +kernel +example : ManagedCallsValid methods imports (fun _ => false) callerTwice liveTwice + (linkedTwice [(3, .direct 1), (7, .direct 1)]) := by decide +kernel +-- A table that omits a call pc, even a dead one. +example : ¬ ManagedCallsValid methods imports (fun _ => false) callerTwice liveTwice + (linkedTwice [(3, .direct 1)]) := by decide +kernel +example : ¬ ManagedCallsValid methods imports (fun _ => false) callerTwice (liveTwice.set 7 false) + (linkedTwice [(3, .direct 1)]) := by decide +kernel +-- A key at a pc that is not a call, or past the code. +example : ¬ ManagedCallsValid methods imports (fun _ => false) callerTwice liveTwice + (linkedTwice [(3, .direct 1), (5, .direct 1), (7, .direct 1)]) := by decide +kernel +example : ¬ ManagedCallsValid methods imports (fun _ => false) callerTwice liveTwice + (linkedTwice [(3, .direct 1), (7, .direct 1), (9, .direct 1)]) := by decide +kernel +-- Keys out of order or repeated. +example : ¬ ManagedCallsValid methods imports (fun _ => false) callerTwice liveTwice + (linkedTwice [(7, .direct 1), (3, .direct 1)]) := by decide +kernel +example : ¬ ManagedCallsValid methods imports (fun _ => false) callerTwice liveTwice + (linkedTwice [(3, .direct 1), (3, .direct 1), (7, .direct 1)]) := by decide +kernel +-- Every key sits at a call pc with its call's link, but key 5 follows the larger key 7. +example : ¬ ManagedCallsValid methods imports (fun _ => false) callerTwice liveTwice + (linkedTwice [(3, .direct 1), (7, .direct 1), (5, .direct 1)]) := by decide +kernel +-- A call pc linked to an extracted method instead of the slot its site names. +example : ¬ ManagedCallsValid methods imports (fun _ => false) callerTwice liveTwice + (linkedTwice [(3, .direct 0), (7, .direct 1)]) := by decide +kernel + -- The validator's guarantees, not only these instances. -example (raw : RawInstruction) (target count : Nat) (valid : validateManagedCall methods imports raw true (.call target count) = true) : +example (raw : RawInstruction) (method : MethodRef) (prefixes : CallPrefixes) (target : Nat) + (valid : validateManagedCall methods imports raw true (linked target) (.call method prefixes) = + true) : target < 2 := validated_call_bound valid /-- Imported slot zero follows all extracted methods, even with a larger indexed table. -/ theorem indexed_import_valid : - ManagedCallsValid (List.replicate 64 ⟨[], callee⟩) imports + ManagedCallsValid (List.replicate 64 ⟨[], callee⟩) imports (fun _ => false) { callee with code := [call site] } [true] - { locals := [], returnsValue := false, code := [.call 64 3] } := by decide +kernel + { locals := [], returnsValue := false, code := [emits site], + links := [(0, .direct 64)] } := by decide +kernel #print axioms indexed_import_valid /-- A target before the import table cannot borrow its slot zero by truncated subtraction. -/ theorem indexed_import_underflow_rejected : - ¬ ManagedCallsValid (List.replicate 64 ⟨[], callee⟩) imports + ¬ ManagedCallsValid (List.replicate 64 ⟨[], callee⟩) imports (fun _ => false) { callee with code := [call site] } [true] - { locals := [], returnsValue := false, code := [.call 63 3] } := by decide +kernel + { locals := [], returnsValue := false, code := [emits site], + links := [(0, .direct 63)] } := by decide +kernel #print axioms indexed_import_underflow_rejected /-- A missing imported slot is rejected rather than replaced with a default descriptor. -/ theorem indexed_import_missing_rejected : - ¬ ManagedCallsValid (List.replicate 64 ⟨[], callee⟩) imports + ¬ ManagedCallsValid (List.replicate 64 ⟨[], callee⟩) imports (fun _ => false) { callee with code := [call site] } [true] - { locals := [], returnsValue := false, code := [.call 65 3] } := by decide +kernel + { locals := [], returnsValue := false, code := [emits site], + links := [(0, .direct 65)] } := by decide +kernel #print axioms indexed_import_missing_rejected end Tests.ImportEvidence diff --git a/Tests/ImportIndexes.cs b/Tests/ImportIndexes.cs index 9c5385d6f..e373cfad1 100644 --- a/Tests/ImportIndexes.cs +++ b/Tests/ImportIndexes.cs @@ -1,9 +1,18 @@ #!/usr/bin/env dotnet #:property PublishAot=false +#:package Mono.Cecil #:include ../scripts/Verification.cs +#:include PinnedMetadata.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Collections; using System.Reflection; +using System.Reflection.PortableExecutable; +using System.Security.Cryptography; using System.Text.Json; using System.Text.Json.Nodes; @@ -38,7 +47,7 @@ var contract = JsonSerializer.Deserialize(JsonSerializer.Serialize(new { Schema = 1, SemanticsVersion = 1, FormatVersion = 1, FrameworkKey = framework, - AssemblyIdentity = "Fixture", Mvid = Guid.NewGuid(), TargetFramework = ".NETCoreApp,Version=v10.0", + AssemblyIdentity = "Fixture", Mvid = Guid.NewGuid(), TargetFramework = ".NETCoreApp,Version=v11.0", ImplementationSha256 = digest, IsReferenceAssembly = false, InterfaceModules = new[] { new { Name = "Published", Source = "namespace Published\nend Published\n" } }, InterfaceDigest = digest, Exports = exports, Certifications = certifications, @@ -98,5 +107,86 @@ SortedDictionary MergeModules(params (string Name, string Source rejected.Message == "Conflicting contract interface module: " + pair.Item3) { } } Console.WriteLine("PASS module ordering, exact deduplication, conflicting contents and ordinal case collisions"); + + var implementation = At("Examples/Assembly/bin/Examples.dll"); + var implementationHash = Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(implementation))); + var sourceType = resolutionType.GetNestedType("Source", BindingFlags.NonPublic)!; + var directory = At("artifacts/ImportIndexes"); + Directory.CreateDirectory(directory); + var sidecar = Path.Combine(directory, "Examples.leancil.contracts"); + using (var pinned = new PinnedMetadata(assembly, implementation)) + { + var description = value.DeepClone().AsObject(); + description["assemblyIdentity"] = pinned.Module.Assembly.Name.FullName; + description["mvid"] = pinned.Module.Mvid; + description["implementationSha256"] = implementationHash; + var descriptionValue = JsonSerializer.Deserialize(description.ToJsonString(), contractType, json)!; + var interfaceHash = (string)contractType.GetMethod("ComputeInterfaceDigest", hiddenInstance)! + .Invoke(descriptionValue, null)!; + description["interfaceDigest"] = interfaceHash; + File.WriteAllText(sidecar, description.ToJsonString()); + var importedContract = Read(description); + object Source(string hash) => Activator.CreateInstance(sourceType, + hiddenInstance | BindingFlags.Public, null, [sidecar, implementation, hash, interfaceHash, false], null)!; + IDisposable Resolution(params object[] sources) + { + var entries = Array.CreateInstance(sourceType, sources.Length); + for (int index = 0; index < sources.Length; index++) entries.SetValue(sources[index], index); + return (IDisposable)Activator.CreateInstance(resolutionType, hiddenInstance, null, [entries], null)!; + } + var registry = (IDictionary)pinned.Context.GetType().GetField("identityImages", hiddenInstance)! + .GetValue(pinned.Context)!; + var baselineCount = registry.Count; + PEReader[] Readers() => registry.Values.Cast().Select(entry => + (PEReader)entry.GetType().GetField("Item1")!.GetValue(entry)!).ToArray(); + void Released(PEReader[] readers) + { + if (registry.Count != baselineCount) throw new Exception("Disposed import retained identity metadata."); + foreach (var reader in readers) + { + try { reader.GetMetadata(); } + catch (ObjectDisposedException) { continue; } + throw new Exception("Disposed import retained an open metadata reader."); + } + } + for (int repeat = 0; repeat < 32; repeat++) + { + var resolution = Resolution(Source(implementationHash)); + if (registry.Count != baselineCount + 1) throw new Exception("Import metadata was not registered."); + var readers = Readers(); + using (var child = (IDisposable)Activator.CreateInstance(resolutionType, hiddenInstance, null, + [resolution, importedContract], null)!) { } + if (registry.Count != baselineCount + 1) throw new Exception("A child released its parent's metadata."); + using (var other = new PinnedMetadata(assembly, implementation)) resolution.Dispose(); + Released(readers); + resolution.Dispose(); + Released(readers); + } + var first = Source(implementationHash); + var invalid = Source(new string('0', 64)); + PEReader[] failedReaders = []; + IEnumerable FailingSources() + { + yield return first; + failedReaders = Readers(); + yield return invalid; + } + var failingSources = typeof(Enumerable).GetMethod("Cast")!.MakeGenericMethod(sourceType) + .Invoke(null, [FailingSources()]); + try + { + using var failed = (IDisposable)Activator.CreateInstance(resolutionType, hiddenInstance, null, + [failingSources], null)!; + throw new Exception("Import with a wrong implementation hash was accepted."); + } + catch (TargetInvocationException error) when (error.InnerException is InvalidDataException rejected && + rejected.Message.StartsWith("Import implementation SHA-256 mismatch:", StringComparison.Ordinal)) { } + if (failedReaders.Length != 1) throw new Exception("Failure did not follow a registered import."); + Released(failedReaders); + Console.WriteLine("PASS import metadata release, child ownership, changed context " + + "and failed constructor cleanup"); + } + using (var empty = (IDisposable)Activator.CreateInstance(resolutionType, hiddenInstance, null, + [Array.CreateInstance(sourceType, 0)], null)!) { } } finally { AppContext.SetData("APP_CONTEXT_BASE_DIRECTORY", savedBaseDirectory); } diff --git a/Tests/ImportReviewRegressions.cs b/Tests/ImportReviewRegressions.cs index 414db7da9..41ab31eb6 100644 --- a/Tests/ImportReviewRegressions.cs +++ b/Tests/ImportReviewRegressions.cs @@ -126,6 +126,7 @@ internal static void ImportResolution_WhenTwoVersionsShareSimpleName_RejectsAmbi AppContext.SetData("APP_CONTEXT_BASE_DIRECTORY", extractorDirectory + Path.DirectorySeparatorChar); if (Path.TrimEndingDirectorySeparator(AppContext.BaseDirectory) != extractorDirectory) throw new InvalidOperationException("The isolated ambiguity test did not select the actual CLI's framework directory."); + using PinnedMetadata metadata = new(assembly, implementation); sources.SetValue(firstSource, 1); using (IDisposable duplicate = (IDisposable)Construct(resolutionType, sources)) { diff --git a/Tests/ImportedFieldIndex.lean b/Tests/ImportedFieldIndex.lean new file mode 100644 index 000000000..50be53add --- /dev/null +++ b/Tests/ImportedFieldIndex.lean @@ -0,0 +1,547 @@ +import CIL.Extraction.Translation +import Tests.BodyEncoding + +/-! +# Instance fields of an unpinned owner + +`T` names the type `Ext.Point` of an assembly `Ext` that no slice carries, and its fields `X` +and `Y`, both `int`, by member references. No carried field list gives the position of either, +so metadata binding refuses each as an instance field, at any index and whether the operand names +`Ext.Point` as a struct or as a class. A method `M(ref Ext.Point)` whose body loads, addresses or +stores `Y` through its token fails every evidence check, with the emitted access at the offset of +`X`. `M(Ext.Point)` fails earlier, on its own signature, since no carried slice defines the class +its parameter names, and is no valid translation with the access at the slot of `X`. A +parameterless `M()` that reaches `Y` of the class through `ldnull` names `Ext.Point` only in +the operand and fails on the `Y` token. An operand that flags `Y` static fails on its token for +the struct and for the parameterless class method, since no carried row gives a static of +`Ext.Point` either, and on the signature for `M(Ext.Point)`. The platform pair binds `Item1` +and `Item2` at indices 0 and 1 for primitive arguments of equal storage width. Its signature +must mark the head as a value type, and its TypeRef must name the exact runtime assembly. +-/ + +namespace Tests.ImportedFieldIndex + +open CIL CIL.Extraction Tests.MetadataBuilder Tests.BodyEncoding + +private def pin : AssemblyPin := + { identity := "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } +private def extScope : String := "Ext, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" +private def int32 : TypeExpr := .atom runtimeScope "System.Int32" +/-- `Ext.Point` as a struct and as a class; its member references admit either reading. -/ +private def point : TypeExpr := .aggregate (extScope ++ "::Ext.Point") 0 +private def pointClass : TypeExpr := .object extScope "Ext.Point" + +/-- The field instructions under test: `ldfld`, `ldflda` and `stfld`. -/ +private def ldfld : Nat := 0x7B +private def ldflda : Nat := 0x7C +private def stfld : Nat := 0x7D + +/-- The signature bytes of the result of the method that accesses a field with `opcode`: the + loaded `int`, a reference to it, or nothing after a store. -/ +private def resultBytes (opcode : Nat) : List Nat := + if opcode == ldfld then [0x08] else if opcode == ldflda then [0x10, 0x08] else [0x01] + +/-- `T` with `static M(p)`, `p` a `ref Ext.Point` struct or an `Ext.Point` class, or with no + parameter when `parameterless`, and the result `opcode` gives, and the references to + `Ext.Point.X` and `Ext.Point.Y`, whose tokens it returns. -/ +private def world (classOwner : Bool) (opcode : Nat) (parameterless := false) : + Step (Nat × Nat) := do + let runtimeName ← string "System.Runtime" + let extName ← string "Ext" + let publicKeyToken ← blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtimeName, 0, 0] + let _ ← row Table.assemblyRef [1, 0, 0, 0, 0, 0, extName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let objectName ← string "Object" + let systemName ← string "System" + let _ ← row Table.typeRef [1 * 4 + 2, objectName, systemName] + let pointName ← string "Point" + let pointRef ← row Table.typeRef [2 * 4 + 2, pointName, extName] + let moduleName ← string "" + let _ ← row Table.typeDef [0, moduleName, 0, 0, 1, 1] + let fixtureName ← string "Fixture" + let owner ← row Table.typeDef [1, fixtureName, 0, 1 * 4 + 1, 1, 1] + let methodName ← string "M" + let pointCoded := (pointRef - 0x01000000) * 4 + 1 + let parameter := + if parameterless then [] + else if classOwner then [0x12, pointCoded] else [0x10, 0x11, pointCoded] + let count := if parameterless then 0 else 1 + let methodBlob ← blob ([0x00, count] ++ resultBytes opcode ++ parameter) + let definition ← row Table.methodDef [0, 0, 0, methodName, methodBlob, 1] + link linkDeclaring definition 0 owner + let intField ← blob [0x06, 0x08] + let parent := (pointRef - 0x01000000) * 8 + 1 + let xName ← string "X" + let yName ← string "Y" + let xRef ← row Table.memberRef [parent, xName, intField] + let yRef ← row Table.memberRef [parent, yName, intField] + pure (xRef, yRef) + +private def slice (classOwner : Bool) (opcode : Nat) (parameterless := false) : + MetadataSlice × Nat × Nat := + build (world classOwner opcode parameterless) (bytesOf "T") pin.identity + +/-- `Ext.Point` as a platform definition of this kind. -/ +private def pointDefinition (kind : PlatformKind) : PlatformDefinition := + { scope := bytesOf extScope, parts := [bytesOf "Ext", bytesOf "Point"], kind } + +/-- The field owner has value storage. -/ +private def structPlatform : PlatformTable := platform_table% + [runtimeType "Object" .objectRoot, pointDefinition .valueType] + +/-- The field owner has reference storage. -/ +private def classPlatform : PlatformTable := platform_table% + [runtimeType "Object" .objectRoot, pointDefinition .classType] + +/-- The platform definitions of a world: `Ext.Point` is the class or the struct its signature + reads it as. -/ +private def pointPlatform (classOwner : Bool) : PlatformTable := + if classOwner then classPlatform else structPlatform + +/-- The error checking `operand` against `token` gives, or `none` when it is accepted, with + `Ext.Point` defined as the kind the operand's owner names. -/ +private def check (token : Nat) (operand : RawOperand) : Option MetadataError := + let platform := match operand with + | .field owner .. => pointPlatform (owner == pointClass) + | _ => structPlatform + match (do + let env := MetadataEnv.ofSlices [← (slice false ldfld).1.index] (platform := platform) + checkEntry env 0 token operand) with + | .ok _ => none + | .error found => some found + +private def xToken : Nat := (slice false ldfld).2.1 +private def yToken : Nat := (slice false ldfld).2.2 + +/-- `X` and `Y` are refused as instance fields of the struct and of the class, at index 0 and at + the position `Y` has in CoreCLR's layout. -/ +theorem unpinned_instance_fields_refused : + check xToken (.field point 0 int32 name!"X" false) = some (.unboundInstanceField xToken) ∧ + check yToken (.field point 0 int32 name!"Y" false) = some (.unboundInstanceField yToken) ∧ + check yToken (.field point 1 int32 name!"Y" false) = some (.unboundInstanceField yToken) ∧ + check xToken (.field pointClass 0 int32 name!"X" false) = + some (.unboundInstanceField xToken) ∧ + check yToken (.field pointClass 0 int32 name!"Y" false) = + some (.unboundInstanceField yToken) := by decide +kernel +#print axioms unpinned_instance_fields_refused + +/-- The receiver opcode of the body: `ldnull` in the parameterless method, else `ldarg.0`. -/ +private def receiverOpcode (parameterless : Bool) : Nat := if parameterless then 0x14 else 0x02 + +/-- The body `ldarg.0; ldfld Y; ret`, `ldarg.0; ldflda Y; ret` or `ldarg.0; ldc.i4.1; stfld Y; + ret` with `Y` claimed at `index`, and static when `flagged`, over `p` of type `owner`, or + over `ldnull` in place of `ldarg.0` when `parameterless`. -/ +private def raw (owner : TypeExpr) (opcode index : Nat) (flagged parameterless := false) : + RawMethod := + let access : RawOperand := .field owner index int32 name!"Y" flagged + let receiver := receiverOpcode parameterless + { token := 0x06000001, owner := pin.identity ++ "::Fixture", hasThis := false, + explicitThis := false, callingConvention := 0, attributes := 0, implementation := 0, + returnType := + if opcode == ldfld then int32 else if opcode == ldflda then .byref int32 else .void, + parameters := + if parameterless then [] else [if owner == pointClass then pointClass else .byref point], + locals := [], initLocals := false, maxStack := 8, genericParameters := [], + code := + if opcode == stfld then + [⟨0, receiver, .none⟩, ⟨1, 0x17, .none⟩, ⟨2, stfld, access⟩, ⟨7, 0x2A, .none⟩] + else [⟨0, receiver, .none⟩, ⟨1, opcode, access⟩, ⟨6, 0x2A, .none⟩] } + +/-- The bytes and token table of `raw`, over the metadata of its world. -/ +private def body (owner : TypeExpr) (opcode index : Nat) (flagged parameterless := false) : + BodyEvidence := + let world := slice (owner == pointClass) opcode parameterless + let token := MetadataBuilder.littleEndian 4 world.2.2 + let receiver := receiverOpcode parameterless + let bytes := + if opcode == stfld then [8 * 4 + 2, receiver, 0x17, stfld] ++ token ++ [0x2A] + else [7 * 4 + 2, receiver, opcode] ++ token ++ [0x2A] + evidenceOf bytes [(world.2.2, .field owner index int32 name!"Y" flagged)] { own := world.1 } + (raw owner opcode index flagged parameterless) (pointPlatform (owner == pointClass)) + +/-- Whether decoding the body fails on exactly the refusal of its `Y` token. -/ +private def refusedAtY (owner : TypeExpr) (opcode index : Nat) (parameterless := false) : Bool := + match checkBody (body owner opcode index (parameterless := parameterless)) + (raw owner opcode index (parameterless := parameterless)) [] with + | .error (.metadata (.unboundInstanceField token)) => + token == (slice (owner == pointClass) opcode).2.2 + | _ => false + +/-- Whether decoding the body fails on the signature of `M`, whose parameter names a class no + carried slice defines, before any field operand is bound. -/ +private def refusedAtSignature (opcode index : Nat) (flagged := false) : Bool := + match checkBody (body pointClass opcode index flagged) (raw pointClass opcode index flagged) [] + with + | .error (.metadata (.methodMismatch token)) => token == (raw pointClass opcode index).token + | _ => false + +/-- Whether decoding the body with `Y` flagged static fails on its token, which names a static + of a type no slice carries. -/ +private def flaggedRefused (owner : TypeExpr) (opcode : Nat) (parameterless := false) : Bool := + match checkBody (body owner opcode 0 true parameterless) (raw owner opcode 0 true parameterless) + [] with + | .error (.metadata (.unboundStaticField token)) => + token == (slice (owner == pointClass) opcode parameterless).2.2 + | _ => false + +private def evidence (owner : TypeExpr) (opcode index : Nat) (flagged := false) : + MethodEvidence := + let classOwner := owner == pointClass + let unknown (count : Nat) : Option FeatureState := some ⟨List.replicate count none, []⟩ + let argument : StorageType := if classOwner then .object else .reference + let receiver : ScalarKind := if classOwner then .object else .reference + let stored := opcode == stfld + let operations : List ArgumentOp := + if stored then [.load 0, .other, .other, .other] else [.load 0, .other, .other] + { original := raw owner opcode index flagged, body := body owner opcode index flagged, + arguments := [], closed := raw owner opcode index flagged, + parameterInput := { arguments := [argument], locals := [], code := operations }, + reachable := List.replicate (if stored then 4 else 3) true, + featureStates := + if stored then [unknown 0, unknown 1, unknown 2, unknown 0] + else [unknown 0, unknown 1, unknown 1], + scalarStates := + if stored then [some [], some [receiver], some [.word32, receiver], some []] + else [some [], some [receiver], + some [if opcode == ldfld then .word32 else .reference]], + layoutUses := [], + assembly := pin, + signature := (spelled? (body owner opcode index flagged).names.signature).getD "" } + +/-- `Ext.Point` as CoreCLR lays it out: `X` at 0, `Y` at 4. -/ +private def rawLayouts : List RawLayout := + [{ identity := extScope ++ "::Ext.Point", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .signed32 }, { type := .signed32 }] }] +private def layouts : Array ComputedLayout := + #[⟨{ size := 8, fields := [{ offset := 0, width := 4 }, { offset := 4, width := 4 }] }, 4⟩] + +/-- The emitted struct method, accessing `Y` at the claimed index. -/ +private def structMethod (opcode index : Nat) : Method := + let field : FieldRef := { owner := point, index, type := int32, name := name!"Y" } + { locals := [], returnsValue := opcode != stfld, + declared := DeclaredSignature.ofRaw (raw point opcode 0), + code := + if opcode == ldfld then [.ldarg 0, .ldfld field, .ret] + else if opcode == ldflda then [.ldarg 0, .ldflda field, .ret] + else [.ldarg 0, .ldc_i4 1, .stfld field, .ret], + environment := { rawLayouts := rawLayouts, layouts := layouts } } + +/-- The emitted class method, accessing `Y` claimed at `index`. -/ +private def classMethod (opcode index : Nat) : Method := + let field : FieldRef := { owner := pointClass, index, type := int32, name := name!"Y" } + { locals := [], returnsValue := opcode != stfld, + declared := DeclaredSignature.ofRaw (raw pointClass opcode 0), + code := + if opcode == ldfld then [.ldarg 0, .ldfld field, .ret] + else if opcode == ldflda then [.ldarg 0, .ldflda field, .ret] + else [.ldarg 0, .ldc_i4 1, .stfld field, .ret] } + +/-- Each struct access to `Y` through the offset of `X` fails on its token and is no valid + translation. -/ +theorem struct_access_of_y_at_offset_of_x_rejected : + ([ldfld, ldflda, stfld].all fun opcode => refusedAtY point opcode 0) = true ∧ + ¬ TranslationValid rawLayouts layouts [] structPlatform [evidence point ldfld 0] [] + [structMethod ldfld 0] ∧ + ¬ TranslationValid rawLayouts layouts [] structPlatform [evidence point ldflda 0] [] + [structMethod ldflda 0] ∧ + ¬ TranslationValid rawLayouts layouts [] structPlatform [evidence point stfld 0] [] + [structMethod stfld 0] := by + decide +kernel +#print axioms struct_access_of_y_at_offset_of_x_rejected + +/-- Each class access to `Y` at the slot of index 0, which `X` shares, fails on the signature of + its method and is no valid translation. -/ +theorem class_access_of_y_at_slot_of_x_rejected : + ([ldfld, ldflda, stfld].all fun opcode => refusedAtSignature opcode 0) = true ∧ + ¬ TranslationValid [] #[] [] classPlatform [evidence pointClass ldfld 0] [] + [classMethod ldfld 0] ∧ + ¬ TranslationValid [] #[] [] classPlatform [evidence pointClass ldflda 0] [] + [classMethod ldflda 0] ∧ + ¬ TranslationValid [] #[] [] classPlatform [evidence pointClass stfld 0] [] + [classMethod stfld 0] := by + decide +kernel +#print axioms class_access_of_y_at_slot_of_x_rejected + +/-- The parameterless class method, whose signature does not name `Ext.Point`, reaches the field + check and fails on its `Y` token for each instance field instruction. -/ +theorem class_access_of_y_without_signature_rejected : + ([ldfld, ldflda, stfld].all fun opcode => refusedAtY pointClass opcode 0 true) = true := by + decide +kernel +#print axioms class_access_of_y_without_signature_rejected + +/-- An operand flagging `Y` static fails each instance field instruction on its token for the + struct and for the parameterless class method, since no carried row gives a static of + `Ext.Point`, and on its method's signature for `M(Ext.Point)`; none is a valid translation. -/ +theorem static_flagged_access_of_y_rejected : + ([ldfld, ldflda, stfld].all fun opcode => + flaggedRefused point opcode && refusedAtSignature opcode 0 true && + flaggedRefused pointClass opcode true) = true ∧ + ¬ TranslationValid rawLayouts layouts [] structPlatform [evidence point ldfld 0 true] [] + [structMethod ldfld 0] ∧ + ¬ TranslationValid rawLayouts layouts [] structPlatform [evidence point ldflda 0 true] [] + [structMethod ldflda 0] ∧ + ¬ TranslationValid rawLayouts layouts [] structPlatform [evidence point stfld 0 true] [] + [structMethod stfld 0] ∧ + ¬ TranslationValid [] #[] [] classPlatform [evidence pointClass ldfld 0 true] [] + [classMethod ldfld 0] ∧ + ¬ TranslationValid [] #[] [] classPlatform [evidence pointClass ldflda 0 true] [] + [classMethod ldflda 0] ∧ + ¬ TranslationValid [] #[] [] classPlatform [evidence pointClass stfld 0 true] [] + [classMethod stfld 0] := by + decide +kernel +#print axioms static_flagged_access_of_y_rejected + +/-! ## The platform pair -/ + +/-- A fixture that returns the member tokens for Item1, Item2 and Rest in that order. -/ +private abbrev PairWorld := Step (Nat × Nat × Nat) + +/-- A tuple with the given assembly, type marker, arity and argument bytes. Its member + references declare Item1 and Rest as !0 and Item2 with the signature type `second`. -/ +private def pairWorldWith (arguments : List Nat) (second := [0x13, 0x01]) + (assembly := runtimeAssembly) (marker := 0x11) (headArity := 2) (arity := 2) + (first := [0x13, 0x00]) : PairWorld := do + let name ← Tests.MetadataBuilder.stringBytes (assembly.name.bytes?.getD []) + let culture ← Tests.MetadataBuilder.stringBytes (assembly.culture.bytes?.getD []) + let publicKeyToken ← blob (assembly.publicKeyToken.elim [] fun key => fixedDigits 8 8 key []) + let version := assembly.version + let _ ← row Table.assemblyRef + [version.major, version.minor, version.build, version.revision, 0, publicKeyToken, name, + culture, 0] + modify fun builder => { builder with corlib := 1 } + let tupleName ← string s!"ValueTuple`{headArity}" + let systemName ← string "System" + let tupleRef ← row Table.typeRef [1 * 4 + 2, tupleName, systemName] + let tupleBlob ← blob ([0x15, marker, tokenRow tupleRef * 4 + 1, arity] ++ arguments) + let tupleSpec ← row Table.typeSpec [tupleBlob] + let parent := (tupleSpec - 0x1B000000) * 8 + 4 + let firstSignature ← blob (0x06 :: first) + let secondSignature ← blob (0x06 :: second) + let firstName ← string "Item1" + let secondName ← string "Item2" + let restName ← string "Rest" + let firstRef ← row Table.memberRef [parent, firstName, firstSignature] + let secondRef ← row Table.memberRef [parent, secondName, secondSignature] + let restRef ← row Table.memberRef [parent, restName, firstSignature] + pure (firstRef, secondRef, restRef) + +/-- `ValueTuple` with `Item2 : !1`. -/ +private def pairWorld : PairWorld := pairWorldWith [0x08, 0x08] + +private def pairSlice : MetadataSlice × Nat × Nat × Nat := + build pairWorld (bytesOf "T") pin.identity + +private def pair : TypeExpr := + .aggregate (s!"{runtimeScope}::System.ValueTuple`2<{runtimeScope}::System.Int32,{runtimeScope}::System.Int32>") 0 + +/-- The platform definition of the pair. -/ +private def pairPlatform : PlatformTable := platform_table% [runtimeType "ValueTuple`2" .valuePair] + +/-- The error checking `operand` against `token` of the pair world gives, if any. -/ +private def checkPair (token : Nat) (operand : RawOperand) (slice := pairSlice.1) (platform := pairPlatform) : + Option MetadataError := + match (do checkEntry (MetadataEnv.ofSlices [← slice.index] (platform := platform)) 0 token operand) with + | .ok _ => none + | .error found => some found + +/-- `Item1` and `Item2` are the pair's fields 0 and 1; `Rest` is refused at index 0 as an + instance member, and as a static since the pair is an instantiation. -/ +theorem pair_items_bound_and_other_members_refused : + checkPair pairSlice.2.1 (.field pair 0 int32 name!"Item1" false) = none ∧ + checkPair pairSlice.2.2.1 (.field pair 1 int32 name!"Item2" false) = none ∧ + checkPair pairSlice.2.2.2 (.field pair 0 int32 name!"Rest" false) = + some (.unboundInstanceField pairSlice.2.2.2) ∧ + checkPair pairSlice.2.2.2 (.field pair 0 int32 name!"Rest" true) = + some (.instantiatedStaticField pairSlice.2.2.2) := by decide +kernel +#print axioms pair_items_bound_and_other_members_refused + +/-- Each primitive argument of Item1's owner must carry the signature's primitive scope. -/ +theorem pair_argument_scopes_checked : + let rejects := fun first second => + let owner := s!"{runtimeScope}::System.ValueTuple`2<{first}System.Int32,{second}System.Int32>" + (checkPair pairSlice.2.1 (.field (.aggregate owner 0) 0 int32 name!"Item1" false)).isSome + let scope := runtimeScope ++ "::" + rejects "" scope && rejects scope "" && rejects "Other::" scope && rejects scope "Other::" := + by decide +kernel +#print axioms pair_argument_scopes_checked + +/-- The error checking `operand` against the `Item2` reference of `pairWorldWith arguments + second` gives, if any. -/ +private def checkSecond (arguments second : List Nat) (operand : RawOperand) : + Option MetadataError := + let (slice, _, token, _) := build (pairWorldWith arguments second) (bytesOf "T") pin.identity + checkPair token operand slice + +/-- `ValueTuple` as the extractor names it. -/ +private def mixedPair : TypeExpr := + .aggregate (s!"{runtimeScope}::System.ValueTuple`2<{runtimeScope}::System.Int32,{runtimeScope}::System.UInt32>") 0 + +/-- `Item2` declared `!0` is refused even where both arguments are `int`, and on + `ValueTuple` a valid `Item2 : !1` holds the second argument, `uint`, not the + first. -/ +theorem pair_second_item_reads_its_own_declaration : + checkSecond [0x08, 0x08] [0x13, 0x00] (.field pair 1 int32 name!"Item2" false) = + some (.operandMismatch (build (pairWorldWith [0x08, 0x08] [0x13, 0x00]) (bytesOf "T") + pin.identity).2.2.1) ∧ + checkSecond [0x08, 0x09] [0x13, 0x01] + (.field mixedPair 1 (.atom runtimeScope "System.UInt32") name!"Item2" false) = none ∧ + (checkSecond [0x08, 0x09] [0x13, 0x01] (.field mixedPair 1 int32 name!"Item2" false)).isSome = + true := by decide +kernel +#print axioms pair_second_item_reads_its_own_declaration + +/-- The field reference at the requested item position, checked against its claimed type. -/ +private def itemError (world : PairWorld) (index : Nat) (owner held : TypeExpr) + (platform := pairPlatform) : + Option MetadataError := + let (slice, first, second, _) := build world (bytesOf "T") pin.identity + if index == 0 then checkPair first (.field owner index held name!"Item1" false) slice platform + else checkPair second (.field owner index held name!"Item2" false) slice platform + +/-- A member reference declaring the first int32 item as int64 fails signature binding. -/ +theorem refuses_int64_first_item : + itemError (pairWorldWith [0x08, 0x08] (first := [0x0A])) 0 pair int32 = + some (.operandMismatch 0x0A000001) := by decide +kernel +#print axioms refuses_int64_first_item + +/-- The extractor's aggregate operand for two tuple arguments in the given assembly. -/ +private def tupleOwner (first second : String) (scope := runtimeScope) (layout := 0) : TypeExpr := + .aggregate (s!"{scope}::System.ValueTuple`2<{scope}::{first},{scope}::{second}>") layout + +/-- The inner pair uses the same head as the outer pair (TypeRef row 1, coded index 5). -/ +private def nestedWorld := pairWorldWith [0x15, 0x11, 5, 2, 0x08, 0x08, 0x08] + +/-- The nested tuple has its own layout after the inner pair. -/ +private def nestedOwner := tupleOwner "System.ValueTuple`2" + "System.Int32" (layout := 1) + +/-- An aggregate argument prevents binding the outer pair's first item. -/ +theorem refuses_nested_first_item : + itemError nestedWorld 0 nestedOwner pair = some (.unboundInstanceField 0x0A000001) := by + decide +kernel +#print axioms refuses_nested_first_item + +/-- The scalar second item of the same outer pair has no certified index either. -/ +theorem refuses_nested_second_item : + itemError nestedWorld 1 nestedOwner int32 = some (.unboundInstanceField 0x0A000002) := by + decide +kernel +#print axioms refuses_nested_second_item + +/-- A pair with unequal primitive widths has no certified field order. -/ +theorem refuses_unequal_width_pair : + itemError (pairWorldWith [0x08, 0x0A]) 0 (tupleOwner "System.Int32" "System.Int64") int32 = + some (.unboundInstanceField 0x0A000001) := by decide +kernel +#print axioms refuses_unequal_width_pair + +/-- Two unsupported primitives have no certified storage width. -/ +theorem refuses_single_pair : + itemError (pairWorldWith [0x0C, 0x0C]) 0 (tupleOwner "System.Single" "System.Single") + (.atom runtimeScope "System.Single") = some (.unboundInstanceField 0x0A000001) := by + decide +kernel +#print axioms refuses_single_pair + +/-- CoreCLR places the four-byte Item2 of (byte, uint) at offset 0. Its index cannot certify + the sequential offset 4, even when the MemberRef correctly declares Item2 as !1. -/ +theorem refuses_byte_uint_second_item : + checkSecond [0x05, 0x09] [0x13, 0x01] + (.field (tupleOwner "System.Byte" "System.UInt32") 1 (.atom runtimeScope "System.UInt32") + name!"Item2" false) = some (.unboundInstanceField 0x0A000002) := by decide +kernel +#print axioms refuses_byte_uint_second_item + +/-- Equal widths preserve both field positions across distinct primitive types. -/ +theorem binds_equal_width_items : + let bytes := pairWorldWith [0x05, 0x02] + let owner := tupleOwner "System.Byte" "System.Boolean" + itemError (pairWorldWith [0x08, 0x09]) 0 mixedPair int32 = none ∧ + itemError bytes 0 owner (.atom runtimeScope "System.Byte") = none ∧ + itemError bytes 1 owner (.atom runtimeScope "System.Boolean") = none := by decide +kernel +#print axioms binds_equal_width_items + +/-- Repeating a CLASS marker in the operand cannot give the tuple a field index. -/ +theorem refuses_class_marked_pair : + let owner := TypeExpr.objectConstructor runtimeScope "System.ValueTuple`2" + itemError (pairWorldWith [8, 8] (marker := 0x12)) 0 (.apply (.apply owner int32) int32) + int32 = some (.platformKindMismatch 0x01000001) ∧ + itemError (pairWorldWith [8, 8] (marker := 0x12)) 0 (.apply (.apply owner int32) int32) + int32 (restating pairPlatform "ValueTuple`2" (.genericClass 2)) = + some (.unboundInstanceField 0x0A000001) := by decide +kernel +#print axioms refuses_class_marked_pair + +/-- The first item with the same assembly scope in its reference and its operand. -/ +private def scopedItem (assembly : AssemblyName) (scope := assembly.display) : Option MetadataError := + let claimed : PlatformDefinition := + ⟨bytesOf scope, [bytesOf "System", bytesOf "ValueTuple`2"], .valuePair⟩ + let platform : PlatformTable := + { includesScope := false, entries := SearchTree.ofSorted [(platformKey claimed.parts, [claimed])] } + itemError (pairWorldWith [8, 8] (assembly := assembly)) 0 + (tupleOwner "System.Int32" "System.Int32" scope) (.atom scope "System.Int32") platform + +/-- An unsigned reference cannot name the platform pair. -/ +theorem refuses_unsigned_pair : + scopedItem { runtimeAssembly with publicKeyToken := none } = + some (.unsupportedAssemblyRef 0x23000001) := by decide +kernel +#print axioms refuses_unsigned_pair + +/-- Two signed references with different eight-byte tokens identify different assemblies. -/ +theorem refuses_other_signed_pair : + scopedItem { runtimeAssembly with + publicKeyToken := some (runtimeAssembly.publicKeyToken.getD 0 + 1) } = + some (.unsupportedAssemblyRef 0x23000001) := by decide +kernel +#print axioms refuses_other_signed_pair + +/-- A different reference assembly version cannot name the platform pair. -/ +theorem refuses_other_version_pair : + scopedItem { runtimeAssembly with version.revision := 1 } + "System.Runtime, Version=11.0.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" = + some (.unboundInstanceField 0x0A000001) := by cil_kernel_rfl +#print axioms refuses_other_version_pair + +/-- A different core library cannot name the supported pair. -/ +theorem refuses_mscorlib_pair : + scopedItem { runtimeAssembly with + name := name!"mscorlib", publicKeyToken := some 0xb77a5c561934e089 } + "mscorlib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" = + some (.unboundInstanceField 0x0A000001) := by cil_kernel_rfl +#print axioms refuses_mscorlib_pair + +/-- A literal culture named neutral shares the empty culture's display spelling. -/ +theorem refuses_literal_neutral_culture : + let assembly := { runtimeAssembly with culture := name!"neutral" } + itemError (pairWorldWith [8, 8] (assembly := assembly)) 0 pair int32 = + some (.unsupportedAssemblyRef 0x23000001) := by decide +kernel +#print axioms refuses_literal_neutral_culture + +/-- Three primitive arguments cannot use the two-item layout. -/ +theorem refuses_three_item_tuple : + itemError (pairWorldWith [8, 8, 8] (arity := 3)) 0 + (.aggregate (runtimeScope ++ "::System.ValueTuple`2") + 0) int32 = some (.genericMismatch 0x01000001) := by decide +kernel +#print axioms refuses_three_item_tuple + +/-- Two arguments cannot give the three-item head a pair layout. -/ +theorem refuses_three_item_head : + itemError (pairWorldWith [8, 8] (headArity := 3)) 0 + (.aggregate (runtimeScope ++ "::System.ValueTuple`3") 0) int32 = + some (.missingPlatformDefinition 0x01000001) ∧ + itemError (pairWorldWith [8, 8] (headArity := 3)) 0 + (.aggregate (runtimeScope ++ "::System.ValueTuple`3") 0) int32 + (platform_table% [runtimeType "ValueTuple`3" (.genericValue 3)]) = + some (.genericMismatch 0x01000001) := by decide +kernel +#print axioms refuses_three_item_head + +/-- The pair's reference to `System.ValueTuple`2`, the first in its world. -/ +private def tupleRef : Nat := 0x01000001 + +/-- `Item1` is bound only under a definition that states the pair: without the definition the + pair's reference is refused, and under one that states a plain value type the item is. -/ +theorem pair_items_need_the_pair_definition : + checkPair pairSlice.2.1 (.field pair 0 int32 name!"Item1" false) = none ∧ + checkPair pairSlice.2.1 (.field pair 0 int32 name!"Item1" false) + (platform := removing pairPlatform "ValueTuple`2") = + some (.missingPlatformDefinition tupleRef) ∧ + checkPair pairSlice.2.1 (.field pair 0 int32 name!"Item1" false) + (platform := restating pairPlatform "ValueTuple`2" (.genericValue 2)) = + some (.platformKindMismatch pairSlice.2.1) := by decide +kernel +#print axioms pair_items_need_the_pair_definition + +end Tests.ImportedFieldIndex diff --git a/Tests/ImportedLayouts.lean b/Tests/ImportedLayouts.lean index e9ffa7af0..de2b25c6f 100644 --- a/Tests/ImportedLayouts.lean +++ b/Tests/ImportedLayouts.lean @@ -1,6 +1,7 @@ import CIL.Extraction.Translation import CIL.Extraction.ImportLayouts import CIL.Extraction.LayoutProjection +import Tests.LayoutAccumulation /-! Certificate regression fixtures, evaluated against the checked CIL extraction model. -/ @@ -48,6 +49,14 @@ theorem rejection_4 : ¬ LayoutProjectionValid artifactRaw artifactComputed publ #[leafLayout, { pairLayout with alignment := 4 }] [1, 2] .scalar := by decide +kernel #print axioms rejection_4 +/-- Matching published and actual rows cannot certify four bytes for an enum over an enum. -/ +theorem nestedEnumCatalogRejected : + let underlying := TypeExpr.enum "Inner" (.atom runtimeScope "System.Int32") + let rows := [Tests.LayoutAccumulation.enumWrap underlying .signed32] + let imported := ImportLayoutCatalog.mk rows rows #[privateLayout] + Not (imported.Valid .scalar) := by decide +kernel +#print axioms nestedEnumCatalogRejected + def catalog : ImportLayoutCatalog := ⟨publicRaw, publicRaw, publicComputed⟩ def copySignature : ImportSignature := ⟨owner, "Copy", [], .void, [.byref pair, .byref pair]⟩ def copySlot : ImportSlot := { @@ -55,9 +64,12 @@ def copySlot : ImportSlot := { parameterCount := 2, hasThis := false, returnsValue := false, signature := some copySignature, layouts := catalog } def copySite : CallSite := { - declaringType := owner, name := "Copy", target := .external copySlot.identity, - typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := false, - returnType := .void, parameterTypes := [.byref pair, .byref pair] } + declared := { convention := 0, parameters := [.byref pair, .byref pair] }, + method := { + owner := owner, name := name!"Copy", hasThis := false, genericArity := 0, returnType := .void, + parameters := [.byref pair, .byref pair] }, + name := "Copy", target := .external copySlot.identity, typeArguments := [], parameterCount := 2, + returnsValue := false } theorem imported_pair_valid : ImportedLayoutsValid [copySlot] .scalar := by decide +kernel #print axioms imported_pair_valid @@ -78,11 +90,12 @@ theorem rejection_8 : ¬ ImportedLayoutsValid [{ copySlot with layouts := ⟨[le theorem rejection_9 : validateImportCall [] [{ copySlot with signature := none, layouts := {} }] 40 copySite copySlot.identity 0 2 = false := by decide +kernel #print axioms rejection_9 -theorem rejection_10 : validateImportCall [] [copySlot] 40 { copySite with parameterTypes := [.byref pair] } +theorem rejection_10 : validateImportCall [] [copySlot] 40 + { copySite with method.parameters := [.byref pair] } copySlot.identity 0 2 = false := by decide +kernel #print axioms rejection_10 theorem rejection_11 : validateImportCall [] [copySlot] 40 - { copySite with parameterTypes := [.byref (.atom "Other" "Library.Pair"), .byref pair] } + { copySite with method.parameters := [.byref (.atom "Other" "Library.Pair"), .byref pair] } copySlot.identity 0 2 = false := by decide +kernel #print axioms rejection_11 @@ -91,9 +104,12 @@ def identitySlot : ImportSlot := { identity := library ++ "::Identity", closureDigest := "digest", parameterCount := 1, hasThis := false, returnsValue := true, signature := some identitySignature } def identitySite : CallSite := { - declaringType := owner, name := "Identity", target := .external identitySlot.identity, - typeArguments := [word], parameterCount := 1, hasThis := false, returnsValue := true, - returnType := word, parameterTypes := [word] } + declared := { convention := 0x10, returnType := .parameter 0, parameters := [.parameter 0] }, + method := { + owner := owner, name := name!"Identity", hasThis := false, genericArity := 1, + returnType := word, parameters := [word], methodArguments := [word] }, + name := "Identity", target := .external identitySlot.identity, typeArguments := [word], + parameterCount := 1, returnsValue := true } theorem closed_generic_valid : ImportedLayoutsValid [identitySlot] .scalar := by decide +kernel #print axioms closed_generic_valid @@ -117,17 +133,19 @@ theorem rejection_16 : ¬ ImportedLayoutsValid [{ identitySlot with signature := theorem rejection_17 : ¬ ImportedLayoutsValid [{ copySlot with signature := some { copySignature with parameters := [pair, .byref pair] } }] .scalar := by decide +kernel #print axioms rejection_17 -theorem complete_certificate_valid : TranslationValid [] #[] [] [identitySlot] [] := by decide +kernel +theorem complete_certificate_valid : TranslationValid [] #[] [] {} [] [identitySlot] [] := by + decide +kernel #print axioms complete_certificate_valid theorem complete_certificate_rejects_open_argument : - ¬ TranslationValid [] #[] [] + ¬ TranslationValid [] #[] [] {} [] [{ identitySlot with signature := some { identitySignature with arguments := [.parameter 0] } }] [] := by decide +kernel #print axioms complete_certificate_rejects_open_argument theorem complete_certificate_rejects_changed_layout : - ¬ TranslationValid [] #[] [] [{ copySlot with layouts := { catalog with actual := [] } }] [] := by + ¬ TranslationValid [] #[] [] {} [] + [{ copySlot with layouts := { catalog with actual := [] } }] [] := by decide +kernel #print axioms complete_certificate_rejects_changed_layout @@ -138,7 +156,7 @@ theorem imported_local_bound : ImportedLocalLayoutsBound artifactRaw artifactComputed [copySlot] := by decide +kernel #print axioms imported_local_bound theorem imported_local_certificate : - TranslationValid artifactRaw artifactComputed [] [copySlot] [] := by decide +kernel + TranslationValid artifactRaw artifactComputed [] {} [] [copySlot] [] := by decide +kernel #print axioms imported_local_certificate /-- A caller layout that no slot publishes is not bound by an import. -/ @@ -177,7 +195,7 @@ theorem local_rejects_disagreeing_slot : ¬ ImportedLocalLayoutsBound artifactRa theorem complete_certificate_rejects_changed_local : ¬ TranslationValid [privateRaw, leafRaw, { pairArtifact with packing := 1 }] artifactComputed - [] [copySlot] [] := by decide +kernel + [] {} [] [copySlot] [] := by decide +kernel #print axioms complete_certificate_rejects_changed_local end Tests.ImportedLayouts diff --git a/Tests/Imports.lean b/Tests/Imports.lean index e6eb06d60..445ef420f 100644 --- a/Tests/Imports.lean +++ b/Tests/Imports.lean @@ -4,6 +4,7 @@ import CIL.Safety.Imports import CIL.Safety.WordMemory import CIL.Safety.WordFootprint import CIL.Safety.ReturnMemory +import Tests.Support /-! `Root(ref r, in b) { AddTo(in r, in b, out r); r = Mix(r, b); }` with `AddTo` imported at slot 0. -/ namespace Tests.Imports.Calls.Extracted @@ -13,9 +14,11 @@ open CIL def method0Signature : String := "System.Void Contracts.Calls::Root(System.UInt64&,System.UInt64&)" def method1Signature : String := "System.UInt64 Contracts.Calls::Mix(System.UInt64,System.UInt64)" @[cil_code] def method0 : Method := { returnsValue := false, locals := [], code := - [.arg 0, .arg 1, .arg 0, .call 2 3, .arg 0, .arg 0, .load64, .arg 1, .load64, .call 1 2, .store64, .ret] } + [.ldarg 0, .ldarg 1, .ldarg 0, .call (Tests.callee 3) {}, .ldarg 0, .ldarg 0, .ldind .i8, .ldarg + 1, .ldind .i8, + .call (Tests.callee 2) {}, .stind .i8, .ret], links := [(3, .direct 2), (9, .direct 1)] } @[cil_code] def method1 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .const32 3, .convI8, .mul, .arg 1, .add, .ret] } + [.ldarg 0, .ldc_i4 3, .conv_i8, .mul, .ldarg 1, .add, .ret] } @[cil_code] def program : Program := [method0, method1] def signatures : List String := [method0Signature, method1Signature] @@ -127,7 +130,7 @@ example : ∀ linked, Links Extracted.program linked → addContract.Holds linke /-! One concrete implementation of the import, appended at its slot. -/ @[cil_code] def addBody : CIL.Method := { returnsValue := false, locals := [], code := - [.arg 2, .arg 0, .load64, .arg 1, .load64, .add, .store64, .ret] } + [.ldarg 2, .ldarg 0, .ldind .i8, .ldarg 1, .ldind .i8, .add, .stind .i8, .ret] } @[cil_code] def linkedProgram : CIL.Program := Extracted.program ++ [addBody] theorem add_linked : importsAdd linkedProgram := by @@ -155,7 +158,7 @@ theorem invoke_eq_of_run {program : CIL.Program} {fuel method : Nat} {body : CIL (setup : enterFrame body args memory = .ok (frame, entered)) (executed : run program fuel method 0 args frame [] entered = outcome) : invoke program fuel method args memory = outcome := by - simp only [invoke, found, checked, setup, Except.mapError, Bind.bind, Except.bind] + simp only [invoke_eq, found, checked, setup, Except.mapError, Bind.bind, Except.bind] exact executed theorem root_unlinked (input : RootInput) (m : Memory) (permitted : rootContract.pre input m) (fuel : Nat) : @@ -177,7 +180,8 @@ theorem root_unlinked (input : RootInput) (m : Memory) (permitted : rootContract theorem root_unlinked_never_returns (input : RootInput) (m : Memory) (permitted : rootContract.pre input m) (fuel : Nat) (result : Memory × List Value) : invoke Extracted.program fuel root (rootContract.args input) m ≠ .ok result := fun returned => - nomatch invoke_unique (first := .ok result) trivial (by simp [Settled]) returned (root_unlinked input m permitted 0) + nomatch invoke_unique (first := .ok result) trivial (by simp [Stable]) returned + (root_unlinked input m permitted 0) /-- So the unlinked contract holds only vacuously: no input meets its precondition. -/ theorem root_unlinked_vacuous (holds : rootContract.Holds Extracted.program root) (input : RootInput) (m : Memory) : @@ -199,10 +203,13 @@ open CIL def method0Signature : String := "System.UInt64 Outcomes.Calls::Root(System.UInt64)" def method1Signature : String := "System.UInt64 Outcomes.Calls::Strict(System.UInt64)" -@[cil_code] def method0 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .call 1 1, .const32 1, .convI8, .add, .ret] } -@[cil_code] def method1 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .call 2 1, .brzero 5, .arg 0, .ret, .nop, .nop, .throw "System.ArgumentException"] } +@[cil_code] def method0 : Method := { + returnsValue := true, locals := [], links := [(1, .direct 1)], code := + [.ldarg 0, .call (Tests.callee 1) {}, .ldc_i4 1, .conv_i8, .add, .ret] } +@[cil_code] def method1 : Method := { + returnsValue := true, locals := [], links := [(1, .direct 2)], code := + [.ldarg 0, .call (Tests.callee 1) {}, .brfalse 5, .ldarg 0, .ret, .nop, .nop, + .throw .argument] } @[cil_code] def program : Program := [method0, method1] def signatures : List String := [method0Signature, method1Signature] @@ -224,11 +231,11 @@ def percentContract : OutcomeContract CIL.W64 CIL.W64 where pre _ _ := True footprint _ := [] post v _ _ out := out = v - raises v _ := if v ≤ 100 then none else some "System.ArgumentOutOfRangeException" + raises v _ := if v ≤ 100 then none else some .argumentOutOfRange -def strictRaises (v : CIL.W64) : Option String := - if v ≤ 100 then if v = 0 then some "System.ArgumentException" else none - else some "System.ArgumentOutOfRangeException" +def strictRaises (v : CIL.W64) : Option CIL.ExceptionType := + if v ≤ 100 then if v = 0 then some .argument else none + else some .argumentOutOfRange def strictContract : OutcomeContract CIL.W64 CIL.W64 where args v := [.scalar (.i64 v)] @@ -309,8 +316,8 @@ theorem root_given : exact MethodContract.Holds.returns call.framed rfl @[cil_code] def percentBody : CIL.Method := { returnsValue := true, locals := [], code := - [.arg 0, .const32 (BitVec.ofInt 32 100), .convI8, .bleu 7, .nop, .nop, - .throw "System.ArgumentOutOfRangeException", .arg 0, .ret] } + [.ldarg 0, .ldc_i4 (BitVec.ofInt 32 100), .conv_i8, .ble_un 7, .nop, .nop, + .throw .argumentOutOfRange, .ldarg 0, .ret] } @[cil_code] def linkedProgram : CIL.Program := Extracted.program ++ [percentBody] theorem percent_linked : importsPercent linkedProgram := by @@ -328,7 +335,7 @@ theorem root_linked : rootContract.Holds linkedProgram root := root_given.discharge ⟨[percentBody], rfl⟩ percent_linked /-- The imported exception reaches `Root` in the linked program. -/ -example (m : Memory) : Raises linkedProgram root [.scalar (.i64 101)] m "System.ArgumentOutOfRangeException" := +example (m : Memory) : Raises linkedProgram root [.scalar (.i64 101)] m .argumentOutOfRange := root_linked.raising (input := 101) trivial (by simp only [rootContract, strictRaises]; decide) /-- Linking further keeps both outcomes. -/ diff --git a/Tests/IncrementalEvidence.cs b/Tests/IncrementalEvidence.cs new file mode 100644 index 000000000..bacf56f8e --- /dev/null +++ b/Tests/IncrementalEvidence.cs @@ -0,0 +1,221 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs + +using System.Text.RegularExpressions; + +// A method's evidence, its metadata slice, lookup tables, body and validity theorems, must be a function of the +// method and its assemblies and of nothing else in the extraction or the image. An edit to one method moves things +// in the image that another method's evidence could pick up (a longer body moves every later body, a changed name +// moves heap entries, whose order the image fixes by a hash or a suffix sort), and extracting a different entry +// method changes which other methods the extraction holds. The fixture has the shape of the workspace test, whose +// refresh of only the edited method rests on this, with a helper that two entry methods share. +var directory = At("artifacts/IncrementalEvidence"); +string Build(string name, string method, string body) +{ + var source = Path.Combine(directory, name); + // An earlier run's sources would join the build and shift every token. + if (Directory.Exists(source)) Directory.Delete(source, true); + Directory.CreateDirectory(source); + File.WriteAllText(Path.Combine(source, "Incremental.csproj"), """ + net11.0 + true + """); + File.WriteAllText(Path.Combine(source, "Arith.cs"), $$""" + namespace Workspace; + public static class Arith + { + public static ulong {{method}}(ulong x) => {{body}}; + } + """); + File.WriteAllText(Path.Combine(source, "Third.cs"), """ + namespace Workspace; + public static class Third + { + public static ulong Thrice(ulong x) => x + x + x; + } + """); + File.WriteAllText(Path.Combine(source, "Shared.cs"), """ + namespace Workspace; + public static class Shared + { + public static ulong Helper(ulong x) => x * 7UL + 1UL; + public static ulong EntryA(ulong x) => Helper(x) + 1UL; + public static ulong EntryB(ulong x) => Helper(x) ^ 3UL; + public static ulong Size(ulong x) where T : unmanaged => + x + (ulong)System.Runtime.CompilerServices.Unsafe.SizeOf(); + public static ulong EntryGenericA(ulong x) => Size(x) + Size(x); + public static ulong EntryGenericB(ulong x) => Size(x) ^ Size(x); + } + """); + File.WriteAllText(Path.Combine(source, "Wide.cs"), """ + namespace Workspace; + public readonly struct Wide(ulong u0, ulong u1, ulong u2, ulong u3) + { + public static readonly Wide Zero = default; + public static readonly Wide One = new(1, 0, 0, 0); + readonly ulong u0 = u0, u1 = u1, u2 = u2, u3 = u3; + public bool IsZeroOrOne => ((u0 >> 1) | u1 | u2 | u3) == 0; + } + """); + Run(["dotnet", "build", Path.Combine(source, "Incremental.csproj"), "-c", "Release"]); + return Path.Combine(source, "bin/Incremental.dll"); +} +var baseline = Build("Baseline", "Twice", "x + x"); +var longer = Build("Longer", "Twice", "x + x + x + x + x + x"); +var renamed = Build("Renamed", "Doubled", "x + x"); + +string Extracted(string assembly, string selector, string name) +{ + var output = Path.Combine(directory, name); + Run([.. Extract("extract", assembly, "Incremental", selector, output), "--assume-initialized", "Workspace.Wide"]); + return File.ReadAllText(Path.Combine(output, "Extracted.lean")); +} +// The assembly block and every definition and theorem of one method's component, the decoded definition included. +string Component(string text, int method, string name) +{ + var lines = IncrementalPatterns.ComponentLine.Matches(text) + .Where(match => !match.Groups["method"].Success || match.Groups["method"].Value == $"{method}") + .Select(match => match.Value.TrimEnd('\r')) + // The composed theorem names the method's position in the whole program, which is not part of its component. + .Where(line => !line.Contains("_evidence_validated", StringComparison.Ordinal)).ToArray(); + // A closed instantiation of a generic method shares its open definition's slice, which another component holds. + var ownSlice = lines.Any(line => line.Contains($"metadata := method{method}Metadata,", StringComparison.Ordinal)); + foreach (var required in (ownSlice + ? new[] { $"def method{method}Metadata :", $"def method{method}Tables :", + $"theorem method{method}_tables_valid :" } + : []).Concat([$"def method{method}Body :", $"theorem method{method}_body_validated :"])) + if (!lines.Any(line => line.StartsWith(required, StringComparison.Ordinal))) + throw new InvalidOperationException($"No `{required}` in the component of method {method}: {name}"); + if (IncrementalPatterns.BodyAddress.IsMatch(string.Concat(lines))) + throw new InvalidOperationException("Evidence holds a body address: " + name); + return string.Join('\n', lines); +} + +foreach (var (method, label) in new[] { ("Workspace.Third::Thrice(ulong)", "Thrice"), + ("Workspace.Wide::get_IsZeroOrOne()", "Wide") }) +{ + var expected = Component(Extracted(baseline, method, "Baseline" + label), 0, label); + if (expected != Component(Extracted(longer, method, "Longer" + label), 0, label)) + throw new InvalidOperationException( + $"Lengthening Twice changed the evidence of {label}: the incremental refresh would redo it."); + if (expected != Component(Extracted(renamed, method, "Renamed" + label), 0, label)) + throw new InvalidOperationException( + $"Renaming Twice changed the evidence of {label}: the image's heap order must not reach the evidence."); +} +Console.WriteLine("PASS lengthening or renaming one method leaves the evidence of the other methods byte-identical"); + +// Two entry methods that share a helper: the helper's whole component is the same in both extractions, so a +// consumer that verified it once reuses it. +var entryA = Extracted(baseline, "Workspace.Shared::EntryA(ulong)", "EntryA"); +var entryB = Extracted(baseline, "Workspace.Shared::EntryB(ulong)", "EntryB"); +string HelperIndex(string text) => IncrementalPatterns.HelperSignature.Match(text).Groups[1].Value; +var (helperA, helperB) = (HelperIndex(entryA), HelperIndex(entryB)); +if (helperA.Length == 0 || helperA != helperB) + throw new InvalidOperationException($"Expected the helper at one index in both extractions: {helperA} {helperB}"); +if (Component(entryA, int.Parse(helperA), "EntryA") != Component(entryB, int.Parse(helperB), "EntryB")) + throw new InvalidOperationException( + "The helper's component differs between two extractions that share it: its evidence depends on the entry."); +Console.WriteLine("PASS a helper's metadata, tables, body and validity theorems are identical in two " + + "extractions that share it"); + +// A generic helper closed twice: the second instantiation names the first one's metadata, which is a function of the +// open definition and the assembly, so both components are the same in two extractions that hold the same helper. +// The two entries close the helper in opposite orders, so each instantiation has another index in the second +// extraction: the component, with its own index named `#`, must be the same text. +var genericA = Extracted(baseline, "Workspace.Shared::EntryGenericA(ulong)", "EntryGenericA"); +var genericB = Extracted(baseline, "Workspace.Shared::EntryGenericB(ulong)", "EntryGenericB"); +string Instantiation(string text, string argument, string name) +{ + var index = IncrementalPatterns.SizeSignature.Matches(text) + .FirstOrDefault(match => match.Groups["argument"].Value == argument)?.Groups[1].Value + ?? throw new InvalidOperationException($"No Size<{argument}> in {name}."); + return IncrementalPatterns.MethodName.Replace(Component(text, int.Parse(index), name), + match => match.Groups[1].Value == index ? "method#" : match.Value); +} +var (uintIndexA, uintIndexB) = (IncrementalPatterns.UIntSizeSignature.Match(genericA).Groups[1].Value, + IncrementalPatterns.UIntSizeSignature.Match(genericB).Groups[1].Value); +if (uintIndexA == uintIndexB) + throw new InvalidOperationException( + "The two extractions close the helper in the same order; the case tests nothing."); +foreach (var argument in new[] { "System.UInt32", "System.UInt64" }) + if (Instantiation(genericA, argument, "EntryGenericA") != Instantiation(genericB, argument, "EntryGenericB")) + throw new InvalidOperationException( + $"The component of Size<{argument}> differs between two extractions that close the helper " + + "in another order."); +Console.WriteLine("PASS a generic helper's instantiations have identical components in extractions " + + "that close it in opposite orders"); + +// A string literal is a `#US` heap entry whose offset the image fixes by the order of the literals. A literal added to +// an earlier method moves every later literal's offset, which changes the `ldstr` operand bytes of the methods that +// use them and nothing in a method that uses none: that evidence stays byte-identical, and the evidence of a method +// whose operand moved is refreshed with its new token, so its body bytes and carried user string differ. +string BuildLiteral(string name, string earlyStatement) +{ + var assembly = Build(name, "Twice", "x + x"); + File.WriteAllText(Path.Combine(directory, name, "Lit.cs"), $$""" + namespace Workspace; + public static class Lit + { + static void Use(string text) { } + public static ulong Early(ulong x) { {{earlyStatement}} return x + 1UL; } + public static ulong Late(ulong x) { Use("late"); return x; } + public static ulong Plain(ulong x) => x + 2UL; + } + """); + Run(["dotnet", "build", Path.Combine(directory, name, "Incremental.csproj"), "-c", "Release"]); + return assembly; +} +var withoutLiteral = BuildLiteral("NoEarlyLiteral", ""); +var withLiteral = BuildLiteral("EarlyLiteral", "Use(\"an earlier literal\");"); +var plainBefore = Component(Extracted(withoutLiteral, "Workspace.Lit::Plain(ulong)", "PlainBefore"), 0, "Plain"); +var plainAfter = Component(Extracted(withLiteral, "Workspace.Lit::Plain(ulong)", "PlainAfter"), 0, "Plain"); +if (plainBefore != plainAfter) + throw new InvalidOperationException( + "A literal added to another method changed the evidence of a method that has none."); +var lateBefore = Component(Extracted(withoutLiteral, "Workspace.Lit::Late(ulong)", "LateBefore"), 0, "Late"); +var lateAfter = Component(Extracted(withLiteral, "Workspace.Lit::Late(ulong)", "LateAfter"), 0, "Late"); +if (lateBefore == lateAfter) + throw new InvalidOperationException( + "A literal added to an earlier method did not move the `ldstr` operand of a later one: " + + "the case tests nothing."); +if (!IncrementalPatterns.FirstBody.IsMatch(lateAfter)) + throw new InvalidOperationException("The refreshed component of Late has no body definition."); +Console.WriteLine("PASS a literal added to one method leaves a method without literals byte-identical and refreshes " + + "the evidence of a method whose ldstr operand moved"); + +/// The patterns that find definitions in the extractions this check compares. +static partial class IncrementalPatterns +{ + [GeneratedRegex(@"\brva\b")] + internal static partial Regex BodyAddress { get; } + + [GeneratedRegex(@"(?m)^def method(\d+)Signature : String := ""[^""]*Shared::Helper\(")] + internal static partial Regex HelperSignature { get; } + + [GeneratedRegex(@"(?m)^def method(\d+)Signature[^\r\n]*Size\\u003CSystem.UInt32")] + internal static partial Regex UIntSizeSignature { get; } + + [GeneratedRegex(@"(?m)^def method0Body :")] + internal static partial Regex FirstBody { get; } + + const string MethodDeclaration = + """(?:@\[[^\r\n]*\] )?(?:def|theorem|cil_decoded_method) method(?[0-9]+)(?![0-9])"""; + + /// The assembly block, or a declaration of the method that the method group numbers. + [GeneratedRegex($"""(?m)^(?:def assembly\d+Metadata |{MethodDeclaration})[^\n]*$""")] + internal static partial Regex ComponentLine { get; } + + const string SizeArgument = """Shared::Size\\u003C(?[^"\\]*)\\u003E"""; + + [GeneratedRegex($"""(?m)^def method(\d+)Signature : String := "[^"]*{SizeArgument}""")] + internal static partial Regex SizeSignature { get; } + + [GeneratedRegex("method([0-9]+)")] + internal static partial Regex MethodName { get; } +} diff --git a/Tests/InitializedExports.cs b/Tests/InitializedExports.cs index e88e35df9..8793df6a1 100644 --- a/Tests/InitializedExports.cs +++ b/Tests/InitializedExports.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; using System.Text.Json.Nodes; @@ -14,7 +19,7 @@ string Project(string name, string? reference, string source) var directory = Path.Combine(root, name); Directory.CreateDirectory(directory); File.WriteAllText(Path.Combine(directory, name + ".csproj"), - "net10.0" + + "net11.0" + "InitializationFixture." + name + "true" + (reference is null ? "" : "") + ""); diff --git a/Tests/InitializedTypes.cs b/Tests/InitializedTypes.cs index 028070507..1dce6e34b 100644 --- a/Tests/InitializedTypes.cs +++ b/Tests/InitializedTypes.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; diff --git a/Tests/InstructionEquations.lean b/Tests/InstructionEquations.lean index 87afdeae6..54a59b5d9 100644 --- a/Tests/InstructionEquations.lean +++ b/Tests/InstructionEquations.lean @@ -1,5 +1,6 @@ import CIL.InstructionEquations import CIL.Safety.SymbolicExecution +import Tests.Support /-! # Instruction equations for a parametric body @@ -14,28 +15,29 @@ proved once over the flags is then bound to concrete profiles without restating namespace Tests.InstructionEquations /-- The instruction a profile extraction emits for code its feature tests exclude. -/ -abbrev unreachable : CIL.Op := .unsupported "Unreachable under selected profile" +abbrev unreachable : CIL.Instruction := .unadmitted 0 /-- `if (Avx2.IsSupported) return Callee(a, b); return Sse42.IsSupported ? b : a;` with the paths of the other profile padded as `unreachable`, so every profile has the same pcs. -/ def dispatchBody (sse42 avx2 : Bool) (callee : Nat) : CIL.Method := - { profile := { sse42, avx2 }, returnsValue := true, locals := [], code := [ - .feature .avx2, - .brzero 6, - if avx2 then .arg 0 else unreachable, - if avx2 then .arg 1 else unreachable, - if avx2 then .call callee 2 else unreachable, + { profile := { sse42, avx2 }, returnsValue := true, locals := [], links := [(4, .direct callee)], + code := [ + .call (Tests.supportedGetter "X86.Avx2"), + .brfalse 6, + if avx2 then .ldarg 0 else unreachable, + if avx2 then .ldarg 1 else unreachable, + if avx2 then .call (Tests.callee 2) else unreachable, if avx2 then .ret else unreachable, - if avx2 then unreachable else if sse42 then .arg 1 else .arg 0, + if avx2 then unreachable else if sse42 then .ldarg 1 else .ldarg 0, if avx2 then unreachable else .ret] } cil_instruction_equations dispatchBody --- The statement keeps the feature test and the callee parameter as the body writes them. +-- The statement keeps the feature test and the called method as the body writes them. /-- info: Tests.InstructionEquations.dispatchBody_instruction4 (sse42 avx2 : Bool) (callee : Nat) : (dispatchBody sse42 avx2 callee).code[4]? = - some (if avx2 = true then CIL.Op.call callee 2 else unreachable) + some (if avx2 = true then CIL.Instruction.call (Tests.callee 2) else unreachable) -/ #guard_msgs (whitespace := lax) in #check dispatchBody_instruction4 @@ -68,26 +70,26 @@ attribute [local irreducible] dispatchBody -- `cil_lookup` closes a lookup through the `@[cil_code]` equations where `rfl` cannot. example (sse42 avx2 : Bool) (callee : Nat) : - ∃ op, (dispatchBody sse42 avx2 callee).code[1]? = some op ∧ op = .brzero 6 := + ∃ op, (dispatchBody sse42 avx2 callee).code[1]? = some op ∧ op = .brfalse 6 := ⟨_, by (fail_if_success rfl); cil_lookup [], by with_reducible rfl⟩ -- Symbolic flags: a fact about each flag decides the fetched instruction's feature test. example (sse42 avx2 : Bool) (callee : Nat) (wide : avx2 = true) : - ∃ op, (dispatchBody sse42 avx2 callee).code[4]? = some op ∧ op = .call callee 2 := + ∃ op, (dispatchBody sse42 avx2 callee).code[4]? = some op ∧ op = .call (Tests.callee 2) := ⟨_, by cil_lookup [wide], by with_reducible rfl⟩ example (sse42 avx2 : Bool) (callee : Nat) (scalar : avx2 = false) (narrow : sse42 = true) : - ∃ op, (dispatchBody sse42 avx2 callee).code[6]? = some op ∧ op = .arg 1 := + ∃ op, (dispatchBody sse42 avx2 callee).code[6]? = some op ∧ op = .ldarg 1 := ⟨_, by cil_lookup [scalar, narrow], by with_reducible rfl⟩ -- Concrete profiles: literal flags decide the test with no fact. -example : ∃ op, (dispatchBody true false 3).code[6]? = some op ∧ op = .arg 1 := +example : ∃ op, (dispatchBody true false 3).code[6]? = some op ∧ op = .ldarg 1 := ⟨_, by cil_lookup [], by with_reducible rfl⟩ -example : ∃ op, (dispatchBody false false 3).code[6]? = some op ∧ op = .arg 0 := +example : ∃ op, (dispatchBody false false 3).code[6]? = some op ∧ op = .ldarg 0 := ⟨_, by cil_lookup [], by with_reducible rfl⟩ -example : ∃ op, (dispatchBody false true 3).code[4]? = some op ∧ op = .call 3 2 := +example : ∃ op, (dispatchBody false true 3).code[4]? = some op ∧ op = .call (Tests.callee 2) := ⟨_, by cil_lookup [], by with_reducible rfl⟩ end Sealed @@ -129,13 +131,171 @@ example (memory : CIL.Safety.Memory) (left right : CIL.W32) : .ok (memory, [.scalar (.i32 right)]) := scalar_returns _ 0 0 true false rfl rfl memory left right +/-- `return Callee(a);` with the callee index as a parameter: its call site's link is a table + entry, so the command states it. -/ +def callingBody (callee : Nat) : CIL.Method := + { returnsValue := true, locals := [], code := [.ldarg 0, .call (Tests.callee 1) {}, .ret], + links := [(1, .direct callee)] } + +cil_instruction_equations callingBody + +/-- +info: Tests.InstructionEquations.callingBody_link1 (callee : Nat) : + (callingBody callee).linkAt 1 = some (CIL.Link.direct callee) +-/ +#guard_msgs (whitespace := lax) in #check callingBody_link1 + +-- A pc that is not a call site gets no link fact; a call a feature flag decides gets the one its +-- table entry states. +/-- error: Unknown identifier `callingBody_link0` -/ +#guard_msgs in #check callingBody_link0 +example (sse42 avx2 : Bool) (callee : Nat) : + (dispatchBody sse42 avx2 callee).linkAt 4 = some (.direct callee) := + dispatchBody_link4 sse42 avx2 callee + +section SealedCall + +attribute [local irreducible] callingBody + +/-- With the body sealed, the call enters its parameter callee by the link fact alone. -/ +theorem sealed_call (callee : Nat) (args : List CIL.Safety.Value) (frame : CIL.Safety.Frame) + (memory : CIL.Safety.Memory) (argument : CIL.W64) : + CIL.Safety.step (callingBody callee) (.call (Tests.callee 1)) 1 args frame + [.scalar (.i64 argument)] memory = + .ok (.call callee [.scalar (.i64 argument)] [] memory) := by + cil_checked_step [] + +/-- info: 'Tests.InstructionEquations.sealed_call' depends on axioms: [propext, Quot.sound] -/ +#guard_msgs in #print axioms sealed_call + +end SealedCall + +section ExtractedLinks + +/-- The links of an extraction's call sites, named and tagged as the extractor writes them. -/ +@[cil_code] def namedDirect : CIL.Link := .direct 1 +/-- The link of a constructor call site, as the extractor writes it. -/ +@[cil_code] def namedAggregate : CIL.Link := .aggregate 1 8 + +/-- An extracted body: an explicit table and one generated fact per call site. -/ +def linkedBody : CIL.Method := + { returnsValue := true, locals := [], + code := [.ldarg 0, .call (Tests.callee 1) {}, .newobj (.arity 0), .ret], + links := [(1, namedDirect), (2, namedAggregate)] } + +@[cil_code] theorem linkedBody_link1 : linkedBody.linkAt 1 = some namedDirect := rfl +@[cil_code] theorem linkedBody_link2 : linkedBody.linkAt 2 = some namedAggregate := rfl + +-- Sealed, so each step below reads its link from the facts and not by evaluating the table. +attribute [local irreducible] linkedBody + +/-- A call enters the method its site's fact and tagged link name. -/ +theorem linked_call (args : List CIL.Safety.Value) (frame : CIL.Safety.Frame) + (memory : CIL.Safety.Memory) (argument : CIL.W64) : + CIL.Safety.step linkedBody (.call (Tests.callee 1)) 1 args frame [.scalar (.i64 argument)] + memory = + .ok (.call 1 [.scalar (.i64 argument)] [] memory) := by + cil_checked_step [] + +/-- A call at a site linked to a constructor faults, which needs the link's constructor form. -/ +theorem constructor_link_faults (args : List CIL.Safety.Value) (frame : CIL.Safety.Frame) + (memory : CIL.Safety.Memory) (argument : CIL.W64) : + CIL.Safety.step linkedBody (.call (Tests.callee 1)) 2 args frame [.scalar (.i64 argument)] + memory = + .error CIL.Safety.unlinkedCall := by + cil_checked_step [] + +/-- info: 'Tests.InstructionEquations.linked_call' depends on axioms: [propext, Quot.sound] -/ +#guard_msgs in #print axioms linked_call +/-- +info: 'Tests.InstructionEquations.constructor_link_faults' depends on axioms: [propext, Quot.sound] +-/ +#guard_msgs in #print axioms constructor_link_faults + +end ExtractedLinks + +/-- Thirty-two linked calls in two full blocks of the table, with calls the table omits before, + inside and after them. -/ +def sparseBody : CIL.Method := + { returnsValue := true, locals := [], + links := [(1, .direct 0), (4, .direct 0), (7, .direct 0), (10, .direct 0), (13, .direct 0), + (19, .direct 0), (22, .direct 0), (25, .direct 0), (28, .direct 0), (31, .direct 0), + (34, .direct 0), (37, .direct 0), (40, .direct 0), (43, .direct 0), (46, .direct 0), + (49, .direct 0), (52, .direct 0), (55, .direct 0), (58, .direct 0), (64, .direct 0), + (67, .direct 0), (70, .direct 0), (73, .direct 0), (76, .direct 0), (79, .direct 0), + (82, .direct 0), (85, .direct 0), (88, .direct 0), (91, .direct 0), (94, .direct 0), + (97, .direct 0), (100, .direct 0)], + code := [.ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldarg 0, .call (Tests.callee 1) {}, .pop, + .ldc_i4 0, .ret] } + +cil_instruction_equations sparseBody + +-- An omitted call reads its `none` from the block where its pc would sit, before the first block, +-- between keys of the second block, and after the last key of a full last block. +example : sparseBody.linkAt 16 = none := sparseBody_link16 +example : sparseBody.linkAt 61 = none := sparseBody_link61 +example : sparseBody.linkAt 103 = none := sparseBody_link103 +example : sparseBody.linkAt 4 = some (.direct 0) := sparseBody_link4 + +/-- A call site its table omits. -/ +def unlinkedBody : CIL.Method := + { returnsValue := true, locals := [], code := [.ldarg 0, .call (Tests.callee 1) {}, .ret], + links := [] } + +cil_instruction_equations unlinkedBody + +/-- The run faults at the call's pc, before any callee runs. -/ +theorem unlinked_call_faults (memory : CIL.Safety.Memory) (argument : CIL.W64) : + CIL.Safety.invoke [unlinkedBody] 4 0 [.scalar (.i64 argument)] memory = + .error ⟨0, 1, CIL.Safety.unlinkedCall⟩ := by + cil_checked_steps [] + +/-- +info: 'Tests.InstructionEquations.unlinked_call_faults' depends on axioms: [propext, Quot.sound] +-/ +#guard_msgs in #print axioms unlinked_call_faults + /-- A body composed from a padding block and a helper list, as transcriptions are written. -/ def composedBody (avx2 : Bool) : CIL.Method := { returnsValue := false, locals := [], code := List.replicate 2 unreachable ++ tailCode avx2 } where /-- The instructions after the padding. -/ - tailCode (avx2 : Bool) : List CIL.Op := [if avx2 then .nop else unreachable, .ret] + tailCode (avx2 : Bool) : List CIL.Instruction := [if avx2 then .nop else unreachable, .ret] cil_instruction_equations composedBody @@ -163,7 +323,7 @@ def lengthDependsBody (avx2 : Bool) : CIL.Method := error: cil_instruction_equations: the instruction at pc 0 of Tests.InstructionEquations.lengthDependsBody is not determined by its parameters, or the body does not unfold; state each feature-dependent instruction as an `if` on the instruction, -padding the shorter side with `.unsupported`, so every profile has the same pcs +padding the shorter side with `.unadmitted`, so every profile has the same pcs -/ #guard_msgs (whitespace := lax) in cil_instruction_equations lengthDependsBody @@ -176,7 +336,7 @@ def tailDependsBody (avx2 : Bool) : CIL.Method := error: cil_instruction_equations: the instruction at pc 1 of Tests.InstructionEquations.tailDependsBody is not determined by its parameters, or the body does not unfold; state each feature-dependent instruction as an `if` on the instruction, -padding the shorter side with `.unsupported`, so every profile has the same pcs +padding the shorter side with `.unadmitted`, so every profile has the same pcs -/ #guard_msgs (whitespace := lax) in cil_instruction_equations tailDependsBody /-- error: Unknown identifier `tailDependsBody_instruction0` -/ @@ -193,18 +353,45 @@ example : ∃ op, fixedBody.code[0]? = some op ∧ op = .nop := /-- A body with an implicit width and a hypothesis about it, both kept as parameters. -/ def guardedBody {width : Nat} (_wide : 32 < width) : CIL.Method := - { returnsValue := true, locals := [], code := [.const32 (BitVec.ofNat 32 width), .ret] } + { returnsValue := true, locals := [], code := [.ldc_i4 (BitVec.ofNat 32 width), .ret] } cil_instruction_equations guardedBody /-- info: Tests.InstructionEquations.guardedBody_instruction0 {width : Nat} (_wide : 32 < width) : - (guardedBody _wide).code[0]? = some (CIL.Op.const32 (BitVec.ofNat 32 width)) + (guardedBody _wide).code[0]? = some (CIL.Instruction.ldc_i4 (BitVec.ofNat 32 width)) -/ #guard_msgs (whitespace := lax) in #check guardedBody_instruction0 example (wide : 32 < 40) : - ∃ op, (guardedBody wide).code[0]? = some op ∧ op = .const32 (BitVec.ofNat 32 40) := + ∃ op, (guardedBody wide).code[0]? = some op ∧ op = .ldc_i4 (BitVec.ofNat 32 40) := ⟨_, by cil_lookup [], by with_reducible rfl⟩ +/-- This instance constructor consumes an int32 array and produces a writable span. -/ +private def arrayConstructor : CIL.Extraction.MethodRef := + let int := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.Int32" + { owner := .apply (CIL.Extraction.spanHead true) int, name := name!".ctor", + genericArity := 0, hasThis := true, returnType := .void, parameters := [.array int] } + +/-- The body constructs an empty view from a null array without a managed constructor link. -/ +def arrayConstruction : CIL.Method := + { code := [.newobj arrayConstructor], locals := [], returnsValue := true } + +cil_instruction_equations arrayConstruction +cil_runtime_models arrayConstruction + +/-- The checked stepper uses the constructor's lookup fact to push the view. -/ +theorem step_array_constructor (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) : + CIL.Safety.step arrayConstruction (.newobj arrayConstructor) 0 [] frame [.object .null] memory = + .ok (.next 1 [.bufferSpan ⟨.null, 0, 4, true⟩] frame memory) := by + cil_checked_step [] +#print axioms step_array_constructor + +open Lean in +run_meta do + let info ← getConstInfo ``step_array_constructor + let some proof := info.value? (allowOpaque := true) | throwError "Missing constructor proof" + unless proof.getUsedConstants.contains ``CIL.Safety.step_newobj_memory do + throwError "The stepper did not use the constructor's memory equation" + end Tests.InstructionEquations diff --git a/Tests/InstructionIndexes.cs b/Tests/InstructionIndexes.cs index 658f78228..cb33d62e4 100644 --- a/Tests/InstructionIndexes.cs +++ b/Tests/InstructionIndexes.cs @@ -2,8 +2,15 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include PinnedMetadata.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Reflection; +using System.Text.RegularExpressions; using Mono.Cecil; using Mono.Cecil.Cil; @@ -11,11 +18,12 @@ var indexType = extractor.GetType("Nethermind.LeanCIL.InstructionIndex", true)!; var position = indexType.GetMethod("Position", BindingFlags.Instance | BindingFlags.NonPublic)!; var isTarget = indexType.GetMethod("IsTarget", BindingFlags.Instance | BindingFlags.NonPublic)!; -var validate = extractor.GetType("Nethermind.LeanCIL.Arrays", true)! - .GetMethod("ValidateReadOnly", BindingFlags.Static | BindingFlags.NonPublic)!; +var validateEntry = extractor.GetType("Nethermind.LeanCIL.InstructionPrefixes", true)! + .GetMethod("Validate", BindingFlags.Static | BindingFlags.NonPublic)!; object Index(MethodDefinition method) => Activator.CreateInstance(indexType, BindingFlags.Instance | BindingFlags.NonPublic, null, [method], null)!; -using var module = ModuleDefinition.CreateModule("Indexes", ModuleKind.Dll); +using var pinned = new PinnedMetadata(extractor, At("Examples/Assembly/bin/Examples.dll")); +var module = pinned.Module; var method = new MethodDefinition("Body", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); var first = Instruction.Create(OpCodes.Nop); var prefix = Instruction.Create(OpCodes.Readonly); @@ -29,7 +37,7 @@ object Index(MethodDefinition method) => Activator.CreateInstance(indexType, throw new Exception("Instruction position used byte offsets instead of identity"); if ((int)position.Invoke(index, [Instruction.Create(OpCodes.Nop)])! != -1) throw new Exception("Foreign instruction was admitted"); -validate.Invoke(null, [index, prefix]); +validateEntry.Invoke(null, [method]); foreach (var switched in new[] { false, true }) { branch.OpCode = switched ? OpCodes.Switch : OpCodes.Br; @@ -38,11 +46,11 @@ object Index(MethodDefinition method) => Activator.CreateInstance(indexType, if (!(bool)isTarget.Invoke(index, [address])!) throw new Exception("Missing prefix-bypass target"); try { - validate.Invoke(null, [index, prefix]); + validateEntry.Invoke(null, [method]); throw new Exception("Accepted a branch bypassing readonly"); } catch (TargetInvocationException error) when (error.InnerException is InvalidDataException rejected && - rejected.Message.Contains("Branch enters an array address", StringComparison.Ordinal)) { } + rejected.Message.Contains("Branch enters the instruction after a prefix", StringComparison.Ordinal)) { } } method.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Nop)); if ((int)position.Invoke(Index(method), [first])! != 1) @@ -62,13 +70,36 @@ object Index(MethodDefinition method) => Activator.CreateInstance(indexType, var translate = extractor.GetType("Nethermind.LeanCIL.InstructionTranslation", true)! .GetMethod("Translate", BindingFlags.Static | BindingFlags.NonPublic)!; var methods = new Dictionary { [caller] = 0, [callee] = 1 }; -string Translate(Instruction instruction) => (string)translate.Invoke(null, - [instruction, caller, methods, 0, Index(caller), null, null, null, null, null, null])!; -if (Translate(call) != ".call 1 0") throw new Exception("Incorrect call index"); +var sitesType = extractor.GetType("Nethermind.LeanCIL.ResolvedCallSites", true)!; +var sites = Activator.CreateInstance(sitesType)!; +sitesType.GetMethod("Add", BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(sites, [call, callee]); +object Operation(Instruction instruction) => translate.Invoke(null, + [instruction, caller, methods, Index(caller), sites, null, null, null, null, null])!; +List<(int Pc, string Link)> Links(SortedSet targets) => + (List<(int, string)>)sitesType.GetMethod("Links", BindingFlags.Instance | BindingFlags.NonPublic)! + .Invoke(sites, [caller, caller.Body.Instructions.ToHashSet(), methods, null, null, targets])!; + +// The proof outline and the template link use the same bound target at the call site. +object? BoundCall(Instruction site) => sitesType.GetMethod("OutlineTarget", BindingFlags.Instance | + BindingFlags.NonPublic)!.Invoke(sites, [site, methods, null]); +if (Operation(call).GetType() != Model("Unoutlined") || + BoundCall(call) is not System.Runtime.CompilerServices.ITuple direct || + !Equals(direct[0], Named("CallKind", "Static")) || !Equals(direct[1], 1) || !Equals(direct[2], 0)) + throw new Exception("Incorrect direct call binding"); +var linked = new SortedSet(); +if (!Links(linked).SequenceEqual([(0, ".direct 1")]) || !linked.SetEquals([1])) + throw new Exception("Incorrect call link or target"); +caller.Body.GetILProcessor().InsertBefore(call, Instruction.Create(OpCodes.Tail)); +linked.Clear(); +if (Operation(call).GetType() != Model("Unoutlined") || + BoundCall(call) is not System.Runtime.CompilerServices.ITuple tail || + !Equals(tail[0], Named("CallKind", "Tail")) || !Equals(tail[1], 1) || + !Links(linked).SequenceEqual([(1, ".direct 1")]) || !linked.SetEquals([1])) + throw new Exception("The prefixed call lost its target or its link at the call instruction"); methods.Remove(callee); try { - Translate(call); + Operation(call); throw new Exception("Unresolved method was assigned a call index"); } catch (TargetInvocationException error) when (error.InnerException is InvalidDataException rejected && @@ -80,10 +111,12 @@ string Translate(Instruction instruction) => (string)translate.Invoke(null, caller.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_0)); caller.Body.Instructions.Add(choose); caller.Body.Instructions.Add(destination); -if (Translate(choose) != ".switch [2, 2]") throw new Exception("Switch target order or duplicates changed"); -Console.WriteLine("PASS translated method indexes reject missing callees and preserve switch target order"); -var runtimeScope = AssemblyNameReference.Parse( - "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); +if (Operation(choose).GetType() != Model("Unoutlined")) + throw new Exception("The switch has a redundant outline descriptor"); +linked.Clear(); +if (Links(linked).Count != 0 || linked.Count != 0) throw new Exception("A branch carries a call link"); +Console.WriteLine("PASS call indexes reject missing callees and a switch requires no outline descriptor or call link"); +var runtimeScope = module.AssemblyReferences.Single(reference => reference.Name == "System.Runtime"); module.TypeSystem.UInt64.Scope = runtimeScope; var valueType = new TypeReference("System", "ValueType", module, runtimeScope); var inner = new TypeDefinition("Fixture", "ZInner", Mono.Cecil.TypeAttributes.Public | @@ -108,7 +141,7 @@ object Context(ModuleDefinition owner, string architecture) => Activator.CreateI (int)layoutIndex.Invoke(layouts, [module.TypeSystem.String])! != -1) throw new Exception("Layout index lost topological order or admitted an absent type"); var emitted = new System.Text.StringBuilder(); -layoutType.GetMethod("EmitLayouts", BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(layouts, [emitted]); +layoutType.GetMethod("EmitLayouts", BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(layouts, [emitted, null]); if (!emitted.ToString().Contains("computedLayouts : Array Extraction.ComputedLayout := #[⟨layout1, 8⟩, ⟨layout0, 8⟩]", StringComparison.Ordinal)) throw new Exception("Raw layout order was confused with emitted layout order"); @@ -153,8 +186,7 @@ void RejectLayout(TypeReference type, string diagnostic) (int)contextFieldIndex.Invoke(freshContext, [inner, inner.Fields[1], false])! != 1 || (int)contextFieldIndex.Invoke(freshContext, [inner, duplicateField, false])! != -1) throw new Exception("Field snapshots lost field identity or instance order"); -var intrinsicsScope = AssemblyNameReference.Parse( - "System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"); +var intrinsicsScope = module.AssemblyReferences.Single(reference => reference.Name == "System.Runtime.Intrinsics"); var vector = new GenericInstanceType(new TypeReference("System.Runtime.Intrinsics", "Vector256`1", module, intrinsicsScope, true)); vector.GenericArguments.Add(module.TypeSystem.UInt64); @@ -204,9 +236,15 @@ int ContextSize(object selected, TypeReference type) var indexHomes = extractor.GetType("Nethermind.LeanCIL.ParameterStorage", true)! .GetMethod("IndexHomes", BindingFlags.Static | BindingFlags.NonPublic)! - .CreateDelegate, Dictionary>>(); -var homePositions = indexHomes([2, 1, 2]); -if (homePositions[2] != 0 || homePositions[1] != 1 || homePositions.ContainsKey(0)) + .CreateDelegate, int, Dictionary>>(); +var addressSlot = Model("InstructionOperands").GetMethod("AddressSlot", BindingFlags.Static | BindingFlags.NonPublic)! + .CreateDelegate, int?>>(); +var storeSlot = Model("InstructionOperands").GetMethod("StoreSlot", BindingFlags.Static | BindingFlags.NonPublic)! + .CreateDelegate, int?>>(); +var loadSlot = Model("InstructionOperands").GetMethod("LoadSlot", BindingFlags.Static | BindingFlags.NonPublic)! + .CreateDelegate, int?>>(); +var homePositions = indexHomes([2, 1, 2], 2); +if (homePositions[2] != 2 || homePositions[1] != 3 || homePositions.ContainsKey(0)) throw new Exception("Home positions lost first-match or missing-argument behavior"); var argumentMethod = new MethodDefinition("Arguments", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); owner.Methods.Add(argumentMethod); @@ -221,14 +259,47 @@ int ContextSize(object selected, TypeReference type) foreach (var instruction in new[] { Instruction.Create(OpCodes.Ldc_I4_0), storeArgument, loadArgument, Instruction.Create(OpCodes.Pop), addressArgument, Instruction.Create(OpCodes.Pop), Instruction.Create(OpCodes.Ret) }) argumentMethod.Body.Instructions.Add(instruction); -string TranslateArgument(Instruction instruction, Dictionary positions) => (string)translate.Invoke(null, +object TranslateArgument(Instruction instruction, Dictionary positions) => translate.Invoke(null, [instruction, argumentMethod, new Dictionary { [argumentMethod] = 0 }, - 0, Index(argumentMethod), null, null, positions, null, null, null])!; -if (TranslateArgument(storeArgument, homePositions) != ".setLocal 3" || - TranslateArgument(loadArgument, homePositions) != ".local 3" || - TranslateArgument(addressArgument, homePositions) != ".localAddr 3") - throw new Exception("Home translation lost the raw-local offset or slot index"); -if (TranslateArgument(loadArgument, []) != ".arg 1") + Index(argumentMethod), sites, null, null, positions, null, null])!; +if (TranslateArgument(storeArgument, homePositions).GetType() != Model("Unoutlined") || + storeSlot(argumentMethod, storeArgument, homePositions) != 3 || + TranslateArgument(loadArgument, homePositions).GetType() != Model("Unoutlined") || + loadSlot(argumentMethod, loadArgument, homePositions) != 3 || + TranslateArgument(addressArgument, homePositions).GetType() != Model("Unoutlined") || + addressSlot(argumentMethod, addressArgument, homePositions) != 3) + throw new Exception("A homed argument's store, load or address used the wrong frame slot"); +var shortStore = Instruction.Create(OpCodes.Starg_S, argumentMethod.Parameters[1]); +if (storeSlot(argumentMethod, shortStore, homePositions) != 3 || + storeSlot(argumentMethod, addressArgument, homePositions) is not null || + storeSlot(argumentMethod, loadArgument, homePositions) is not null) + throw new Exception("An argument store did not retain its original opcode and home"); +var storeMacros = new[] { OpCodes.Stloc_0, OpCodes.Stloc_1, OpCodes.Stloc_2, OpCodes.Stloc_3 }; +var loadMacros = new[] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }; +for (int slot = 0; slot < loadMacros.Length; slot++) + if (loadSlot(argumentMethod, Instruction.Create(loadMacros[slot]), homePositions) != slot) + throw new Exception("A local load macro selected another slot"); +foreach (var opcode in new[] { OpCodes.Ldloc, OpCodes.Ldloc_S }) + if (loadSlot(argumentMethod, Instruction.Create(opcode, argumentMethod.Body.Variables[1]), homePositions) != 1) + throw new Exception("A local load operand lost its declared slot"); +if (loadSlot(argumentMethod, loadArgument, new Dictionary()) is not null || + loadSlot(argumentMethod, storeArgument, homePositions) is not null) + throw new Exception("An unhomed argument or store was mistaken for a local load"); +for (int slot = 0; slot < storeMacros.Length; slot++) + if (storeSlot(argumentMethod, Instruction.Create(storeMacros[slot]), homePositions) != slot) + throw new Exception("A local store macro selected another slot"); +foreach (var opcode in new[] { OpCodes.Stloc, OpCodes.Stloc_S }) + if (storeSlot(argumentMethod, Instruction.Create(opcode, argumentMethod.Body.Variables[1]), homePositions) != 1) + throw new Exception("A local store operand lost its declared slot"); +foreach (var opcode in new[] { OpCodes.Ldloca, OpCodes.Ldloca_S }) + if (addressSlot(argumentMethod, Instruction.Create(opcode, argumentMethod.Body.Variables[1]), homePositions) != 1) + throw new Exception("A local address did not retain its declared slot"); +if (addressSlot(argumentMethod, Instruction.Create(OpCodes.Ldarga_S, argumentMethod.Parameters[1]), homePositions) != 3 || + addressSlot(argumentMethod, loadArgument, homePositions) is not null) + throw new Exception("An argument load and a short address did not retain their distinct opcodes"); +var argumentIndex = Model("MethodArguments").GetMethod("LoadIndex", BindingFlags.Static | BindingFlags.NonPublic)! + .CreateDelegate>(); +if (TranslateArgument(loadArgument, []).GetType() != Model("Unoutlined") || argumentIndex(argumentMethod, loadArgument) != 1) throw new Exception("Unhomed loads no longer use the original argument"); try { @@ -237,4 +308,352 @@ string TranslateArgument(Instruction instruction, Dictionary positions } catch (TargetInvocationException error) when (error.InnerException is InvalidDataException rejected && rejected.Message == "Missing parameter storage") { } -Console.WriteLine("PASS home lookup preserves first positions, local offsets and missing-store rejection"); +try +{ + TranslateArgument(addressArgument, []); + throw new Exception("An address without a home was admitted"); +} +catch (TargetInvocationException error) when (error.InnerException is InvalidDataException rejected && + rejected.Message == "Missing parameter storage") { } +Console.WriteLine("PASS home lookup preserves first positions, argument indexes and missing-home rejection"); + +// Outline generation reads the typed operation and its operands. +const BindingFlags Declared = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | + BindingFlags.NonPublic; +Type Model(string name) => extractor.GetType("Nethermind.LeanCIL." + name, true)!; +object Make(string name, params object?[] arguments) => Activator.CreateInstance(Model(name), arguments)!; +object Named(string type, string value) => Enum.Parse(Model(type), value); +var bodyType = extractor.GetType("Nethermind.LeanCIL.ProofOutline", true)!.GetNestedType("Body", + BindingFlags.NonPublic)!; +Array Operations(params object[] operations) +{ + var array = Array.CreateInstance(Model("Operation"), operations.Length); + for (var at = 0; at < operations.Length; at++) array.SetValue(operations[at], at); + return array; +} +var scalarProfile = Model("FeatureProfile").GetMethod("Select", Declared)!.Invoke(null, ["scalar"]); +object OutlineMethod(MethodDefinition source, HashSet reachable, Dictionary homes, + params object[] operations) => + Make("ProofOutline+Method", null, Operations(operations), source, reachable, homes, scalarProfile, context, + Index(source), sites, methods, null); +int Loops(object body) => ((System.Collections.IList)bodyType.GetMethod("Loops", Declared)!.Invoke(body, null)!).Count; +// An argument read is pure only when its declared type is no narrow integer: (int, byte) below. +var mixedArguments = new MethodDefinition("Mixed", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Int32); +mixedArguments.Parameters.Add(new ParameterDefinition("word", Mono.Cecil.ParameterAttributes.None, + module.TypeSystem.Int32)); +mixedArguments.Parameters.Add(new ParameterDefinition("small", Mono.Cecil.ParameterAttributes.None, + module.TypeSystem.Byte)); +owner.Methods.Add(mixedArguments); +var mixedCode = mixedArguments.Body.Instructions; +mixedCode.Add(Instruction.Create(OpCodes.Ldarg_0)); +mixedCode.Add(Instruction.Create(OpCodes.Ldarg_S, mixedArguments.Parameters[1])); +mixedCode.Add(Instruction.Create(OpCodes.Ret)); +mixedCode.Add(Instruction.Create(OpCodes.Call, mixedArguments)); +var mixedLive = mixedCode.ToHashSet(); +var mixedHomes = new Dictionary(); +var mixed = OutlineMethod(mixedArguments, mixedLive, mixedHomes, Make("Unoutlined"), Make("Unoutlined"), + Make("StackOperation", Code.Ret), Make("Unoutlined")); +bool Pure(int pc, bool plainReturn = true) => + (bool)mixed.GetType().GetMethod("PureAt", Declared)!.Invoke(mixed, [pc, plainReturn])!; +foreach (var encoding in new[] { OpCodes.Blt_S, OpCodes.Blt }) +{ + var head = Instruction.Create(OpCodes.Ldc_I4_0); + caller.Body.Instructions.Clear(); + foreach (var site in new[] { head, Instruction.Create(OpCodes.Ldc_I4_1), Instruction.Create(encoding, head), + Instruction.Create(OpCodes.Ret) }) + caller.Body.Instructions.Add(site); + var code = caller.Body.Instructions; + var liveBranches = code.ToHashSet(); + var loopMethod = OutlineMethod(caller, liveBranches, [], code.Select(Operation).ToArray()); + var body = Activator.CreateInstance(bodyType, Declared, null, [loopMethod], null)!; + if (Loops(body) != 1) + throw new Exception("A conditional back edge is not found as a loop: " + encoding.Name); + (System.Reflection.Metadata.ILOpCode Kind, int Target)? Branch(int pc) => + ((System.Reflection.Metadata.ILOpCode, int)?)loopMethod.GetType() + .GetMethod("BranchAt", Declared)!.Invoke(loopMethod, [pc]); + if (Branch(2) != (System.Reflection.Metadata.ILOpCode.Blt, 0) || Branch(0) is not null) + throw new Exception("A short or long branch lost its original target"); + code[2].Operand = code[3]; + loopMethod = OutlineMethod(caller, liveBranches, [], code.Select(Operation).ToArray()); + if (Branch(2)?.Target != 3) throw new Exception("The new body index lost the changed CIL target"); + liveBranches.Remove(code[2]); + if (Branch(2) is not null) throw new Exception("An unreachable branch contributed an outline edge"); +} +var bitMethods = module.GetType("Examples.Bits").Methods; +foreach (var (methodName, width) in new (string, int?)[] + { ("PrimitiveCounts64", 64), ("PrimitiveCountsNative", 64), ("PopulationNative", 32), ("RoundUp64", null) }) +{ + var bitMethod = bitMethods.Single(candidate => candidate.Name == methodName); + var instructions = bitMethod.Body.Instructions; + var site = instructions.First(instruction => instruction.OpCode.Code == Code.Call); + var pc = instructions.IndexOf(site); + sitesType.GetMethod("AddModel", Declared)!.Invoke(sites, [site]); + var liveCalls = instructions.ToHashSet(); + var outline = OutlineMethod(bitMethod, liveCalls, [], instructions.Select(_ => Make("Unoutlined")).ToArray()); + int? CountWidth() => (int?)outline.GetType().GetMethod("BitCountWidthAt", Declared)!.Invoke(outline, [pc]); + if (CountWidth() != width) throw new Exception("Wrong bit-count result width for " + methodName); + liveCalls.Remove(site); + if (CountWidth() is not null) throw new Exception("An unreachable bit count was outlined"); + liveCalls.Add(site); + var original = (MethodReference)site.Operand; + site.Operand = new MethodReference(original.Name, original.ReturnType, owner); + if (CountWidth() is not null) throw new Exception("An unrelated member was outlined as a bit count"); + site.Operand = original; +} +var fieldReader = module.GetType("Examples.Counter").Methods.Single(member => member.Name == "Read"); +var fieldCode = fieldReader.Body.Instructions; +var fieldSite = fieldCode.Single(site => site.OpCode.Code == Code.Ldfld); +var live = fieldCode.ToHashSet(); +var extractedReader = OutlineMethod(fieldReader, live, [], + fieldCode.Select(_ => Make("Unoutlined")).ToArray()); +var fieldAt = extractedReader.GetType().GetMethod("StructFieldAt", Declared)!; +object? Shape(Instruction site) => fieldAt.Invoke(extractedReader, [fieldCode.IndexOf(site)]); +if (!Equals(Shape(fieldSite), (Code.Ldfld, 0, 8, false, false)) || Shape(fieldCode[0]) is not null) + throw new Exception("The outline did not resolve the compiled unsigned field load at its original site"); +live.Remove(fieldSite); +if (Shape(fieldSite) is not null) throw new Exception("An unreachable struct field contributed an outline operand"); +if (!Pure(0) || Pure(1) || !Pure(2) || Pure(2, plainReturn: false) || Pure(3)) + throw new Exception("The pure operations are no longer exactly the admitted unnarrowed ones"); +mixedHomes.Add(0, 0); +if (Pure(0)) throw new Exception("A homed argument was stepped as a direct argument load"); +mixedHomes.Clear(); +mixedLive.Remove(mixedCode[0]); +if (Pure(0)) throw new Exception("An unreachable argument load contributed a pure step"); +var unaligned = Model("UnsafeModels").GetMethod("UnalignedStorage", Declared)!; +Instruction TransferSite(string name) => module.GetType("Examples.UnalignedMemory").Methods + .Single(method => method.Name == name).Body.Instructions.Single(site => site.OpCode.Code == Code.Call); +TypeReference? Storage(Instruction site, string name) => + (TypeReference?)unaligned.Invoke(null, [site, name, "x64"]); +foreach (var (name, expected) in new[] { ("ReadSigned", MetadataType.SByte), ("ReadHalf", MetadataType.Int16), + ("ReadWord", MetadataType.UInt64), ("ReadPointerWord", MetadataType.UInt64) }) + if (Storage(TransferSite(name), "ReadUnaligned")?.MetadataType != expected) + throw new Exception("Incorrect closed Unsafe.ReadUnaligned storage type for " + name); +var writeSite = TransferSite("WritePointerWord"); +if (Storage(writeSite, "WriteUnaligned")?.MetadataType != MetadataType.UInt64 || + Storage(writeSite, "ReadUnaligned") is not null || + Storage(Instruction.Create(OpCodes.Callvirt, (MethodReference)writeSite.Operand), "WriteUnaligned") is not null || + Storage(TransferSite("ReadAlignedWord"), "Read") is not null || + Storage(TransferSite("ReadPointerAggregate"), "ReadUnaligned") is not null) + throw new Exception("A transfer outline lost its opcode, direction, alignment or scalar type"); +var outlineType = Model("ProofOutline"); +var unoutlined = Model("Unoutlined").GetField("Site", Declared)!.GetValue(null)!; +var objectSites = new MethodDefinition("ObjectOperands", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); +owner.Methods.Add(objectSites); +var objectCode = objectSites.Body.Instructions; +objectCode.Add(Instruction.Create(OpCodes.Sizeof, module.TypeSystem.UInt32)); +objectCode.Add(Instruction.Create(OpCodes.Ldobj, module.TypeSystem.Int16)); +objectCode.Add(Instruction.Create(OpCodes.Stobj, module.TypeSystem.UInt64)); +var objectLive = objectCode.ToHashSet(); +var objectMethod = OutlineMethod(objectSites, objectLive, [], objectCode.Select(_ => unoutlined).ToArray()); +(int? Bytes, int? ScalarWidth, bool Signed)? ObjectShape(int pc, Code op) => + ((int?, int?, bool)?)objectMethod.GetType().GetMethod("ObjectAt", Declared)!.Invoke(objectMethod, [pc, op]); +if (ObjectShape(0, Code.Sizeof) != (4, null, false) || ObjectShape(1, Code.Ldobj) != (null, 2, true) || + ObjectShape(2, Code.Stobj) != (null, 8, false) || ObjectShape(2, Code.Ldobj) is not null) + throw new Exception("An object outline lost its original opcode, width or signedness"); +objectLive.Remove(objectCode[0]); +if (ObjectShape(0, Code.Sizeof) is not null) + throw new Exception("An unreachable sizeof contributed a literal to the outline"); + +var readsWidth = outlineType.GetNestedType("LoopOutline", BindingFlags.NonPublic)!.GetMethod("ReadsWidth", Declared)!; +(object Method, int Pc, Instruction Site) TransferOutline(string name) +{ + var method = module.GetType("Examples.UnalignedMemory").Methods.Single(candidate => candidate.Name == name); + var code = method.Body.Instructions; + var site = TransferSite(name); + return (OutlineMethod(method, code.ToHashSet(), [], code.Select(_ => unoutlined).ToArray()), + code.IndexOf(site), site); +} +bool Reads(string name, int width) +{ + var (method, pc, site) = TransferOutline(name); + return (bool)readsWidth.Invoke(null, [method, pc, site, "x64", width])!; +} +var scalarStore = outlineType.GetMethod("ScalarStore", Declared)!; +(int Width, string Lemma)? Store(string name) +{ + var (method, pc, site) = TransferOutline(name); + return ((int, string)?)scalarStore.Invoke(null, [method, pc, site, "x64"]); +} +if (!Reads("ReadWord", 8) || Reads("ReadWord", 4) || Reads("ReadSigned", 1) || Reads("ReadHalf", 2) || + Store("WritePointerWord") != (8, "transfer_i64_of_write") || Store("WriteHalf") != (2, "transfer_i32_of_write") || + Store("ReadWord") is not null || Store("WritePointerAggregate") is not null) + throw new Exception("The loop outline selected an incorrect scalar read width or store theorem"); +var offsetModel = Model("UnsafeModels").GetMethod("Offset", Declared)!; +(ulong Stride, bool Signed)? Offset(Instruction site) => + ((ulong, bool)?)offsetModel.Invoke(null, [site, "x64"]); +var pointers = module.GetType("Examples.UnsafeReferences"); +Instruction PointerCall(string name) => pointers.Methods.Single(method => method.Name == name) + .Body.Instructions.Single(site => site.OpCode.Code == Code.Call); +var subtract = PointerCall("SubtractPointer"); +if (Offset(subtract) != (ulong.MaxValue - 7, true) || Offset(PointerCall("AddPointer")) != (8UL, true) || + Offset(Instruction.Create(OpCodes.Callvirt, (MethodReference)subtract.Operand)) is not null || + Offset(TransferSite("ReadWord")) is not null) + throw new Exception("Incorrect displacement for a retained Unsafe call"); +var constants = new MethodDefinition("Constants", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); +owner.Methods.Add(constants); +var literals = constants.Body.Instructions; +foreach (var literal in new[] { Instruction.Create(OpCodes.Ldc_I4_M1), Instruction.Create(OpCodes.Ldc_I4_0), + Instruction.Create(OpCodes.Ldc_I4_8), Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)-3), + Instruction.Create(OpCodes.Ldc_I4, 5), Instruction.Create(OpCodes.Ldc_I8, long.MinValue) }) + literals.Add(literal); +var liveLiterals = literals.ToHashSet(); +var literalMethod = OutlineMethod(constants, liveLiterals, [], literals.Select(_ => unoutlined).ToArray()); +(int Width, long Value, bool Numeral)? Literal(int pc) => + ((int, long, bool)?)literalMethod.GetType().GetMethod("ConstantAt", Declared)!.Invoke(literalMethod, [pc]); +(int, long, bool)[] expectedLiterals = [(32, -1, false), (32, 0, true), (32, 8, true), (32, -3, false), + (32, 5, false), (64, long.MinValue, false)]; +for (var pc = 0; pc < expectedLiterals.Length; pc++) + if (Literal(pc) != expectedLiterals[pc]) throw new Exception("Incorrect original literal at " + pc); +liveLiterals.Remove(literals[0]); +if (Literal(0) is not null) throw new Exception("An unreachable literal contributed an outline operand"); +Console.WriteLine("PASS typed operations preserve outline operands, argument kinds and tail-call targets"); + +// The extractor's narrowing table, which the outline readers refuse arguments by, states for every primitive +// head the narrowing Lean's declaredNarrow gives it (pinned in Tests/ResolutionFacts.lean); an admitted enum +// narrows as its underlying type on both sides. +var narrowOf = extractor.GetType("Nethermind.LeanCIL.LeanEmitter", true)! + .GetMethod("Narrow", BindingFlags.Static | BindingFlags.NonPublic)!; +string? NarrowName(TypeReference type) => narrowOf.Invoke(null, [type])?.ToString(); +var system = module.TypeSystem; +foreach (var (head, narrowing) in new (TypeReference, string?)[] +{ + (system.Boolean, "Unsigned8"), (system.Char, "Unsigned16"), (system.SByte, "Signed8"), (system.Byte, "Unsigned8"), + (system.Int16, "Signed16"), (system.UInt16, "Unsigned16"), (system.Int32, null), (system.UInt32, null), + (system.Int64, null), (system.UInt64, null), (system.IntPtr, null), (system.UIntPtr, null), + (system.Single, null), (system.Double, null) +}) + if (NarrowName(head) != narrowing) + throw new Exception( + $"{head.FullName} narrows as {NarrowName(head) ?? "nothing"}, not {narrowing ?? "nothing"}"); +var enums = module.Types.Where(type => type.IsEnum).ToArray(); +if (enums.Length == 0) throw new Exception("The examples declare no enum to compare narrowing on"); +foreach (var enumeration in enums) +{ + var storage = enumeration.Fields.Single(field => !field.IsStatic).FieldType; + if (NarrowName(enumeration) != NarrowName(storage)) + throw new Exception($"{enumeration.FullName} does not narrow as its underlying {storage.FullName}"); +} +Console.WriteLine( + $"PASS argument narrowing agrees with declaredNarrow on 14 primitive heads and {enums.Length} enums"); + +// A stage learns an instruction's category, callee and target from the typed Operation, never from the Lean text +// that renders it. Each place that reads generated Lean text is listed here with what it reads. +var sourceFiles = Directory.GetFiles(At("Extractor"), "*.cs").Concat(Directory.GetFiles(At("scripts"), "*.cs") + .Where(path => !Path.GetFileName(path).StartsWith("Test", StringComparison.Ordinal))) + .ToDictionary(path => Path.GetRelativePath(Root, path).Replace('\\', '/'), File.ReadAllText); +foreach (var type in extractor.GetTypes().Where(Model("Operation").IsAssignableFrom)) + if (type.GetMethods(Declared | BindingFlags.DeclaredOnly).Any(method => method.Name is "Render" or "ToLean")) + throw new Exception("Extractor operation defines an instruction renderer: " + type); +var constructors = RenderedLeanReaders.Constructors(File.ReadLines(At("CIL/Instructions.lean"))); +foreach (var rendered in new[] { "ret", "ldloc", "switch", "br", "brtrue", "call", "callvirt" }) + if (!constructors.Contains(rendered)) throw new Exception("Missing CIL instruction ." + rendered); +var readers = RenderedLeanReaders.Scan(sourceFiles, constructors); +(string File, int Reads, string Reason)[] allowedReaders = +[ + ("Extractor/ProofGates.cs", 3, + "compares whole method definitions, the raw definitions decoded methods come from, and signatures"), + ("scripts/ConsumerVerification.cs", 3, "the component splitter cuts module text at declaration boundaries"), + ("scripts/VerifyReports.cs", 1, "reads the source location table of Symbols.lean, not an instruction"), + ("scripts/BenchmarkExternalIdentities.cs", 1, "reads identity byte arrays from baseline extractions"), +]; +var actualReaders = readers.Where(finding => finding.Kind == "reads generated Lean") + .GroupBy(finding => finding.File).Select(group => (group.Key, group.Count())).Order().ToArray(); +var expectedReaders = allowedReaders.Select(entry => (entry.File, entry.Reads)).Order().ToArray(); +if (!actualReaders.SequenceEqual(expectedReaders) || readers.Any(finding => finding.Kind != "reads generated Lean")) + throw new Exception("Stages read generated Lean text outside the allowed list:\n" + + string.Join("\n", readers.Select(finding => $"{finding.File}:{finding.Line}: {finding.Kind}"))); +// Each rule must fail on a scratch source, so that a weakened pattern cannot pass unnoticed. +foreach (var (scratchFile, scratchText, rule) in new (string, string, string)[] +{ + ("Extractor/Scratch.cs", "var calls = Regex.Matches(emitted, @\"^\\s*\\.call \\d+\");", + "reads generated Lean"), + ("Extractor/Scratch.cs", "if (line.StartsWith(\".ret\", StringComparison.Ordinal)) returns++;", + "reads generated Lean"), + ("Extractor/Scratch.cs", "var bodies = Regex.Matches(module, @\"^def method\\d+ : Method := \");", + "reads generated Lean"), + ("Extractor/Scratch.cs", "var text = op.ToLean(); // parsed below", + "renders an instruction"), + ("Extractor/ProofOutlineLoops.cs", "if (op.ToLean().StartsWith(\".br\")) return Stop(\"x\");", + "renders an instruction"), + ("Extractor/LeanEmitter.cs", "var first = ops.Select(op => op.ToLean()).First(text => text.Contains(\"x\"));", + "renders an instruction"), + ("Extractor/LeanEmitter.cs", "if (op.Link(callee).Contains(target)) return true;", + "searches a rendered link"), +}) + if (!RenderedLeanReaders.Scan(new Dictionary { [scratchFile] = scratchText }, constructors) + .Any(finding => finding.Kind == rule)) + throw new Exception("Not found as " + rule + ": " + scratchText); +if (RenderedLeanReaders.Scan(new Dictionary { ["Extractor/Scratch.cs"] = + "var calls = operations.Count(operation => operation is Call);" }, constructors).Count != 0) + throw new Exception("A typed read of calls was reported"); +Console.WriteLine("PASS no stage reads opcode, call or target information back from generated Lean text"); + +/// Finds code that reads generated Lean text or reads back what an Operation renders. +static partial class RenderedLeanReaders +{ + internal sealed record Finding(string File, int Line, string Kind); + + /// Files that serialize method links. + static readonly string[] Emitters = ["Extractor/LeanEmitter.cs", "Extractor/TemplateEmitter.cs"]; + + [GeneratedRegex(@"\| ([a-z][A-Za-z0-9_]*)")] + private static partial Regex RenderedConstructor { get; } + + const string SearchMethods = + "Contains|StartsWith|EndsWith|IndexOf|Split|Matches|Replace|IsMatch|Match|GeneratedRegex|new Regex"; + + [GeneratedRegex($"""(?:{SearchMethods})\((?.*)$""")] + private static partial Regex SearchCall { get; } + + const string DeclarationKeywords = "def|theorem|lemma|namespace|structure|instance"; + + [GeneratedRegex($""" + "[^"]*(?:\b(?:{DeclarationKeywords})\s*\(?[A-Za-z.]|method\\?d|method\[0-9\])[^"]*" + """)] + private static partial Regex GeneratedDeclaration { get; } + + const string RenderedReceivers = """op|operation|ops\[\w+\]|values\[\w+\]"""; + + [GeneratedRegex($"""\b(?:{RenderedReceivers})\.ToLean\(|new Unadmitted\(.*\)\.ToLean\(""")] + private static partial Regex RenderedOperation { get; } + + /// A dotted name, with its dot escaped or not, that no word or dot precedes. + [GeneratedRegex(@"(?\w+)\b")] + private static partial Regex DottedName { get; } + + /// Whether text holds a string literal that names one of the constructors after a dot. + static bool NamesConstructor(string text, HashSet constructors) => + DottedName.Matches(text).Any(match => constructors.Contains(match.Groups["name"].Value) && + text.LastIndexOf('"', match.Index) >= 0 && text.IndexOf('"', match.Index + match.Length) >= 0); + + /// The constructors of the executable instruction type in Lean. + internal static string[] Constructors(IEnumerable lines) => + [.. lines.SkipWhile(line => line != "inductive Instruction where") + .TakeWhile(line => !line.StartsWith(" deriving ", StringComparison.Ordinal)) + .Where(line => line.StartsWith(" | ", StringComparison.Ordinal)) + .SelectMany(line => RenderedConstructor.Matches(line).Select(match => match.Groups[1].Value)).Distinct()]; + + /// One finding per line that reads generated Lean or misuses a rendered operation. + internal static List Scan(IReadOnlyDictionary files, string[] constructors) + { + var rendered = constructors.ToHashSet(StringComparer.Ordinal); + var found = new List(); + foreach (var (file, text) in files) + { + var lines = text.Split('\n'); + for (var index = 0; index < lines.Length; index++) + { + var line = lines[index]; + var search = SearchCall.Match(line); + var searched = search.Success && (GeneratedDeclaration.IsMatch(search.Groups["rest"].Value) || + NamesConstructor(search.Groups["rest"].Value, rendered)); + if (searched) found.Add(new(file, index + 1, "reads generated Lean")); + if (RenderedOperation.IsMatch(line)) found.Add(new(file, index + 1, "renders an instruction")); + if (!line.Contains("op.Link(", StringComparison.Ordinal)) continue; + if (!Emitters.Contains(file)) found.Add(new(file, index + 1, "renders a link outside emission")); + else if (search.Success) found.Add(new(file, index + 1, "searches a rendered link")); + } + } + return found; + } +} diff --git a/Tests/IntegerMath.cs b/Tests/IntegerMath.cs index 82d2dd7b6..508b8e6f6 100644 --- a/Tests/IntegerMath.cs +++ b/Tests/IntegerMath.cs @@ -2,7 +2,13 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs +#:include PinnedMetadata.cs using System.Reflection; using Mono.Cecil; @@ -12,11 +18,12 @@ var root = At("artifacts/IntegerMath"); Directory.CreateDirectory(root); var runtime = Assembly.Load(File.ReadAllBytes(assembly)).GetType("Examples.IntegerMath")!; -using var metadata = ModuleDefinition.ReadModule(assembly); verification.EnsureExtractorBuilt(); var extractor = Assembly.Load(File.ReadAllBytes(ExtractorPath)); +using var pinned = new PinnedMetadata(extractor, assembly); +var metadata = pinned.Module; var flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; -var translate = extractor.GetType("Nethermind.LeanCIL.RuntimeModels", true)!.GetMethod("Translate", flags)!; +var callModel = extractor.GetType("Nethermind.LeanCIL.RuntimeModels", true)!.GetMethod("CallModel", flags)!; var select = extractor.GetType("Nethermind.LeanCIL.FeatureProfile", true)!.GetMethod("Select", flags)!; var profile = select.Invoke(null, ["scalar"]); var cases = 0; @@ -103,9 +110,9 @@ string Observe(MethodInfo method, object[] inputs) alias.Parameters.Add(new ParameterDefinition(parameter.ParameterType)); math.Parameters.Add(new ParameterDefinition(parameter.ParameterType)); } - var originalModel = (string?)translate.Invoke(null, [math, profile]); - var aliasModel = (string?)translate.Invoke(null, [alias, profile]); - if (originalModel is null || originalModel != aliasModel) + var originalModel = callModel.Invoke(null, [math, profile]); + var aliasModel = callModel.Invoke(null, [alias, profile]); + if (originalModel is null || !originalModel.Equals(aliasModel)) throw new InvalidOperationException("Primitive helper must share its Math model: " + alias.FullName); var values = new ulong[] { 0, 1, 2, 127, 128, 255, 32767, 32768, 65535, int.MaxValue, 1UL << 31, uint.MaxValue, long.MaxValue, 1UL << 63, ulong.MaxValue } .Select(x => Value(type, x)).Distinct().ToArray(); @@ -138,8 +145,10 @@ string Observe(MethodInfo method, object[] inputs) var binders = $"({string.Join(" ", variables)} : CIL.W{stackWidth})"; var evaluation = $"CIL.evalIntegerMath .{operation} {width} {signed} [{string.Join(", ", loaded)}]"; var invocation = $"CIL.Safety.invoke Extracted.program 8 0 [{inputsLean}] CIL.Safety.Memory.empty"; - var prefix = $" change CIL.Safety.run Extracted.program {8 - argc} 0 {argc} [{inputsLean}] ⟨0, [], [], [], 0⟩ [{stack}] CIL.Safety.Memory.empty = _\n"; - var instruction = $"(body := Extracted.method0) (op := .intrinsic (.scalar (.math .{operation} {width} {signed})) {argc})"; + var prefix = $" change CIL.Safety.run Extracted.program {8 - argc} 0 {argc} [{inputsLean}] ⟨0, [], [], 0⟩ [{stack}] CIL.Safety.Memory.empty = _\n"; + // The helper keeps its call, which steps as the scalar intrinsic its model fact names. + var instruction = "(body := Extracted.method0)"; + var callStep = $" math_wrapper_step Extracted.method0_model{argc} evaluated\n"; var argumentsLean = string.Join(" ", variables); var caseArguments = string.Join(" ", variables.Select(variable => "test." + variable)); string ForCase(string expression) @@ -153,13 +162,13 @@ string ForCase(string expression) $" (evaluated : {evaluation} = .ok (.i{resultWidth} r)) :\n" + $" {invocation} = .ok (CIL.Safety.Memory.empty, [.scalar (.i{resultWidth} {Narrow(method.ReturnType, "r")})]) := by\n" + prefix + $" refine (CIL.Safety.run_next {instruction} (by rfl) (by rfl)\n" + - $" (show CIL.Safety.step _ _ _ _ _ _ _ = .ok (.next {argc + 1} [.scalar (.i{resultWidth} r)] ⟨0, [], [], [], 0⟩ CIL.Safety.Memory.empty) from ?_)).trans (by rfl)\n" + - " math_wrapper_step evaluated\n" + + $" (show CIL.Safety.step _ _ _ _ _ _ _ = .ok (.next {argc + 1} [.scalar (.i{resultWidth} r)] ⟨0, [], [], 0⟩ CIL.Safety.Memory.empty) from ?_)).trans (by rfl)\n" + + callStep + $"theorem wrapper_fault_full {binders} (fault : CIL.ArithmeticFault)\n" + $" (evaluated : {evaluation} = .error fault) :\n" + $" {invocation} = .error ⟨0, {argc}, .arithmetic fault⟩ := by\n" + prefix + $" apply Tests.WrapperProofs.run_fault {instruction} (by rfl) (by rfl)\n" + - " math_wrapper_step evaluated\n" + + callStep + $"theorem wrapper_success {binders} (r : CIL.W{resultWidth})\n" + $" (evaluated : {evaluation} = .ok (.i{resultWidth} r)) :\n" + $" ({invocation}).map Prod.snd = .ok [.scalar (.i{resultWidth} {Narrow(method.ReturnType, "r")})] :=\n" + diff --git a/Tests/IntegerOpcodes.cs b/Tests/IntegerOpcodes.cs index 6c4a4648a..1f5ebbade 100644 --- a/Tests/IntegerOpcodes.cs +++ b/Tests/IntegerOpcodes.cs @@ -2,6 +2,13 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include EmptyReturnVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Numerics; @@ -106,22 +113,14 @@ { // The rejection reuses this fixture's output, so certify the fixture first. queue.Flush(); - var rejected = false; - - try - { - verification.Verify(dll, "Examples", $"IntegerOperations::Greater{width}({parameters})", - At($"Examples/Proofs/Negate{width}.lean"), output); - } - catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) - { - rejected = true; - } - - if (!rejected || File.Exists(Path.Combine(output, "verified.json"))) - throw new InvalidOperationException("Incorrect negation implementation accepted"); - - Console.WriteLine($"PASS incorrect negation i{width} rejected by proof checking"); + verification.VerifyEmptyReturn(assembly, $"IntegerOperations::Negate{width}({type})", + At($"Examples/Proofs/Negate{width}.lean"), dll, $"IntegerOperations::Greater{width}({parameters})", output, + new($"W{width}", $"[.scalar (.i{width} input)]", $".scalar (.i{width} (-input))", 3, + """ + obtain ⟨result, ran, inverse⟩ := ExampleProof.correctness input + have same : result = -input := by bv_omega + exact same ▸ ran + """, "1")); } } diff --git a/Tests/InterpreterEquations.lean b/Tests/InterpreterEquations.lean index 431207202..8c3390b02 100644 --- a/Tests/InterpreterEquations.lean +++ b/Tests/InterpreterEquations.lean @@ -8,8 +8,9 @@ open Lean -- A consumer macro naming a reserved equation lemma, which a quotation cannot generate itself. macro "unfold_dispatch" : tactic => `(tactic| rw [CIL.step.eq_def]) -example (returns : Bool) (pc : Nat) (args stack : List CIL.Value) (frame : Nat) (memory : CIL.Memory) : - CIL.step .nop returns pc args frame stack memory = CIL.step .nop returns pc args frame stack memory := by +example (returns : Bool) (pc : Nat) (stack : List CIL.Value) (memory : CIL.Memory) : + CIL.step .nop returns none pc stack memory = + CIL.step .nop returns none pc stack memory := by unfold_dispatch -- Each lemma must come from its shared module; one generated here would mean importers rebuild it. @@ -33,4 +34,271 @@ run_cmd do let stored := env.header.moduleNames[index.toNat]! unless stored == owner do throwError "{name} is stored by {stored}, expected {owner}" +-- A `step` match wider than 79 alternatives exceeds the default heartbeat budget of an importer +-- that splits it. Finding no matcher means the names changed, and the bound would hold vacuously. +run_cmd do + let mut widest := 0 + let mut found := 0 + for index in [1:200] do + let name := `CIL.Safety.step ++ Name.mkSimple s!"match_{index}" + if let some info ← Meta.getMatcherInfo? name then + widest := max widest info.numAlts + found := found + 1 + if found == 0 then throwError "no CIL.Safety.step.match_N matcher found below 200" + unless widest ≤ 79 do + throwError "a match in CIL.Safety.step has {widest} alternatives; importers cannot split it" + +section FaultingArithmetic +open CIL CIL.Safety + +/-- The flags each division and overflow-checked constructor stands for. -/ +example : Instruction.divideKind? .rem_un = some (false, true) := rfl +example : Instruction.divideKind? .div = some (true, false) := rfl +example : Instruction.divideKind? .add_ovf = none := rfl +example : Instruction.checkedKind? .sub_ovf_un = some (.sub, false) := rfl +example : Instruction.checkedKind? .mul_ovf = some (.mul, true) := rfl +example : Instruction.checkedKind? .rem = none := rfl + +private def body : Method := { returnsValue := true, locals := [], code := [] } +private def frame : Frame := { activation := 0, locals := [], owned := [] } + +/-- One checked step of `op` over `x` then `y` (`y` on top). -/ +private def binaryStep (op : Instruction) (x y : W32) : Except ExecutionFault FrameAction := + step body op 0 [] frame [.scalar (.i32 y), .scalar (.i32 x)] Memory.empty + +/-- The checked step that pushes `result` and resumes at the next instruction. -/ +private def pushes (result : W32) : Except ExecutionFault FrameAction := + .ok (.next 1 [.scalar (.i32 result)] frame Memory.empty) + +private def overflows : Except ExecutionFault FrameAction := .error (.arithmetic .overflow) + +-- Each operand pair separates the constructor from its signedness and quotient/remainder twins. +example : binaryStep .div 0xFFFFFFF9 2 = pushes 0xFFFFFFFD := rfl +example : binaryStep .div_un 0xFFFFFFF9 2 = pushes 0x7FFFFFFC := rfl +example : binaryStep .rem 0xFFFFFFF9 2 = pushes 0xFFFFFFFF := rfl +example : binaryStep .rem_un 0xFFFFFFF9 2 = pushes 1 := rfl +example : binaryStep .add_ovf 0x7FFFFFFF 1 = overflows := rfl +example : binaryStep .add_ovf_un 0x7FFFFFFF 1 = pushes 0x80000000 := rfl +example : binaryStep .sub_ovf 0 1 = pushes 0xFFFFFFFF := rfl +example : binaryStep .sub_ovf_un 0 1 = overflows := rfl +example : binaryStep .mul_ovf 0xFFFFFFFF 2 = pushes 0xFFFFFFFE := rfl +example : binaryStep .mul_ovf_un 0xFFFFFFFF 2 = overflows := rfl + +-- An unsigned checked op zero-extends a native-int `i32`; sign extension would overflow here. +example : step body .mul_ovf_un 0 [] frame + [.scalar (.i32 0x80000000), .scalar (.i64 2)] Memory.empty = + .ok (.next 1 [.scalar (.i64 0x100000000)] frame Memory.empty) := rfl + +/-- One checked step of `op` over a single operand `value`. -/ +private def unaryStep (op : Instruction) (value : CIL.Value) : + Except ExecutionFault FrameAction := + step body op 0 [] frame [.scalar value] Memory.empty + +/-- The checked step that pushes `result` from a one-operand instruction. -/ +private def pushesValue (result : CIL.Value) : Except ExecutionFault FrameAction := + .ok (.next 1 [.scalar result] frame Memory.empty) + +-- `conv.ovf.i.un` converts to 64 signed bits from an unsigned operand: 2^63 overflows, which +-- a signed operand or an unsigned target would accept, and `i32` operands zero-extend. +example : unaryStep (.conv_ovf_un .i) (.i64 0x8000000000000000) = overflows := rfl +example : unaryStep (.conv_ovf .i) (.i64 0x8000000000000000) = + pushesValue (.i64 0x8000000000000000) := rfl +example : unaryStep (.conv_ovf_un .u) (.i64 0x8000000000000000) = + pushesValue (.i64 0x8000000000000000) := rfl +example : unaryStep (.conv_ovf_un .i) (.i64 0x7FFFFFFFFFFFFFFF) = + pushesValue (.i64 0x7FFFFFFFFFFFFFFF) := rfl +example : unaryStep (.conv_ovf_un .i) (.i32 0xFFFFFFFF) = pushesValue (.i64 0xFFFFFFFF) := rfl +example : unaryStep (.conv_ovf .i4) (.i64 0x80000000) = overflows := rfl +example : unaryStep (.conv_ovf .u2) (.i32 0xFFFF) = pushesValue (.i32 0xFFFF) := rfl + +-- A native target's width and signedness reduce, so a fact about `convertInteger 64 true true` +-- drives the bridge for `.conv_ovf_un .i`. +example (input value : CIL.Value) (rest : List CIL.Safety.Value) + (calculated : CIL.convertInteger 64 true true input = .ok value) + (known : input.hasUnspecified = false) + (checked : checkedValue Memory.empty (.scalar value) = .ok (.scalar value)) : + step body (.conv_ovf_un .i) 0 [] frame (.scalar input :: rest) Memory.empty = + .ok (.next 1 (.scalar value :: rest) frame Memory.empty) := + step_conv_ovf_un_result (target := .i) calculated known checked body 0 [] frame rest + +-- `conv.i` sign-extends like `conv.i8` on the admitted 64-bit native width; `conv.u` does not. +example (value : CIL.Value) : CIL.unary .conv_i value = CIL.unary .conv_i8 value := by + cases value <;> rfl +example : unaryStep .conv_i (.i32 0xFFFFFFFF) = pushesValue (.i64 0xFFFFFFFFFFFFFFFF) := rfl +example : unaryStep .conv_u (.i32 0xFFFFFFFF) = pushesValue (.i64 0xFFFFFFFF) := rfl + +section Guards +variable (left right : CIL.Value) (rest : List CIL.Safety.Value) + (leftKnown : left.hasUnspecified = false) (rightKnown : right.hasUnspecified = false) +include leftKnown rightKnown + +-- Each faulting constructor's guard lemma decides its operand guard; without it `cil_guard` +-- leaves `observerCount` folded and makes no progress. +example : unspecifiedOperands (observerCount .div) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] +example : unspecifiedOperands (observerCount .div_un) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] +example : unspecifiedOperands (observerCount .rem) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] +example : unspecifiedOperands (observerCount .rem_un) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] +example : unspecifiedOperands (observerCount .add_ovf) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] +example : unspecifiedOperands (observerCount .add_ovf_un) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] +example : unspecifiedOperands (observerCount .sub_ovf) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] +example : unspecifiedOperands (observerCount .sub_ovf_un) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] +example : unspecifiedOperands (observerCount .mul_ovf) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] +example : unspecifiedOperands (observerCount .mul_ovf_un) (.scalar right :: .scalar left :: rest) = + false := by + simp only [cil_guard, leftKnown, rightKnown, Bool.or_false] + +end Guards + +end FaultingArithmetic + +section LocalAddress +open CIL CIL.Safety + +-- Taking a span local's address pushes its home, which names the slot and reads no view, so +-- an uninitialized local is taken as readily as an initialized one. +example (body : CIL.Method) (pc : Nat) (args stack : List CIL.Safety.Value) + (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) (width : Nat) (writable : Bool) + (view : Option SpanView) + (slot : frame.locals[0]? = some (.span width writable view)) : + step body (.ldloca 0) pc args frame stack memory = + .ok (.next (pc + 1) (.spanHome 0 :: stack) frame memory) := by + simp only [step_ldloca_eq, slot, localAddress, Bind.bind, Except.bind, Pure.pure, Except.pure] + +-- Taking a byte home's address pushes the formed home reference. +example (body : CIL.Method) (pc : Nat) (args stack : List CIL.Safety.Value) + (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) (kind : CIL.LocalKind) + (home : CIL.Safety.Reference) (slot : frame.locals[0]? = some (.bytes kind home)) : + step body (.ldloca 0) pc args frame stack memory = do + let address ← formValue memory (.address home) + return FrameAction.next (pc + 1) (address :: stack) frame memory := by + simp only [step_ldloca_eq, slot, localAddress] + +end LocalAddress + +-- Every generated faulting and `conv.i` equation exists and uses only the allowed axioms. At +-- all admitted widths a faulting equation keeps its helper folded with the operand guard decided, the +-- form the checked bridge reads; an unfolded unsigned bound would expand `2 ^ width`. +run_cmd do + let binary := ["div", "div_un", "rem", "rem_un", "add_ovf", "add_ovf_un", "sub_ovf", + "sub_ovf_un", "mul_ovf", "mul_ovf_un"] + let helperOf (op : String) : Name := + if op.startsWith "div" || op.startsWith "rem" then ``CIL.divideValues else ``CIL.checkedValues + let folded : List (String × Name) := + binary.flatMap (fun op => + ["i32_i32", "i64_i64", "i32_i64", "i64_i32"].map fun widths => + (s!"{op}_{widths}", helperOf op)) ++ + ["conv_ovf", "conv_ovf_un"].flatMap fun op => + [(s!"{op}_i32", ``CIL.convertInteger), (s!"{op}_i64", ``CIL.convertInteger)] + let plain := ["conv_i_i32", "conv_i_i64"] + let names := (folded.map (·.1) ++ plain).map fun suffix => + `CIL.Safety ++ Name.mkSimple s!"step_{suffix}_eq" + unless names.length == 46 do throwError "expected 46 equations, listed {names.length}" + let allowed := [``propext, ``Quot.sound, ``Classical.choice] + for equation in names do + unless (← getEnv).contains equation do throwError "missing {equation}" + let axioms ← collectAxioms equation + unless axioms.all allowed.contains do throwError "{equation} depends on {axioms}" + for (suffix, helper) in folded do + let equation := `CIL.Safety ++ Name.mkSimple s!"step_{suffix}_eq" + let type := (← getConstInfo equation).type + unless (type.find? (·.isAppOf helper)).isSome do + throwError "{equation} no longer applies {helper}" + if (type.find? fun term => term.isAppOf ``Bool.rec || + term.isConstOf ``CIL.Safety.unspecifiedOperands).isSome then + throwError "{equation} leaves the operand guard undecided" + +section Returns + +open CIL CIL.Safety + +variable (body : Method) (pc : Nat) (args : List Safety.Value) + (frame : Frame) (memory : Safety.Memory) + +/-- A narrowed return with an empty stack reports a malformed execution state. -/ +theorem narrowReturnEmpty (kind : NarrowKind) (narrow : body.returnNarrow = some kind) : + Safety.step body .ret pc args frame [] memory = .error .invalidState := by + rw [step_ret_eq, narrow] + rfl +#print axioms narrowReturnEmpty + +/-- A narrowed return rejects a scalar of the wrong width. -/ +theorem narrowReturnWrongWidth (kind : NarrowKind) (bits : W64) + (narrow : body.returnNarrow = some kind) : + Safety.step body .ret pc args frame [.scalar (.i64 bits)] memory = + .error .invalidState := by + rw [step_ret_eq, narrow] + rfl +#print axioms narrowReturnWrongWidth + +/-- A narrowed return rejects extra stack values. -/ +theorem narrowReturnExtraValue (kind : NarrowKind) (bits extra : W32) + (narrow : body.returnNarrow = some kind) : + Safety.step body .ret pc args frame + [.scalar (.i32 bits), .scalar (.i32 extra)] memory = .error .invalidState := by + rw [step_ret_eq, narrow] + rfl +#print axioms narrowReturnExtraValue + +/-- Narrowing cannot turn a void method into a value-returning method. -/ +theorem narrowReturnVoid (returns : body.returnsValue = false) (bits : W32) : + Safety.step body .ret pc args frame [.scalar (.i32 bits)] memory = + .error .invalidState := by + rw [step_ret_eq, Method.returnNarrow_of_void returns] + change returnFrom none body.returnsValue [.scalar (.i32 bits)] memory = _ + rw [returnFrom_none_eq, returns] + rfl +#print axioms narrowReturnVoid + +/-- A narrowed return extends exactly the single i32 result. -/ +theorem narrowReturnValue (returns : body.returnsValue = true) (kind : NarrowKind) (bits : W32) + (narrow : body.returnNarrow = some kind) : + Safety.step body .ret pc args frame [.scalar (.i32 bits)] memory = + .ok (.returned [.scalar (.i32 (kind.extend bits))] memory) := by + rw [step_ret_eq, narrow] + change returnFrom (some kind) body.returnsValue [.scalar (.i32 bits)] memory = _ + rw [returnFrom_narrow_eq, returns] + rfl +#print axioms narrowReturnValue + +/-- A plain return preserves its checked scalar result. -/ +theorem plainReturnValue (returns : body.returnsValue = true) (bits : W32) + (narrow : body.returnNarrow = none) : + Safety.step body .ret pc args frame [.scalar (.i32 bits)] memory = + .ok (.returned [.scalar (.i32 bits)] memory) := by + rw [step_ret_eq, narrow] + change returnFrom none body.returnsValue [.scalar (.i32 bits)] memory = _ + rw [returnFrom_none_eq, returns] + rfl +#print axioms plainReturnValue + +/-- A value-returning method rejects an empty plain return. -/ +theorem plainReturnWrongArity (returns : body.returnsValue = true) + (narrow : body.returnNarrow = none) : + Safety.step body .ret pc args frame [] memory = .error .invalidState := by + rw [step_ret_eq, narrow] + change returnFrom none body.returnsValue [] memory = _ + rw [returnFrom_none_eq, returns] + rfl +#print axioms plainReturnWrongArity + +end Returns + end Tests.InterpreterEquations diff --git a/Tests/InvocationCertificates.lean b/Tests/InvocationCertificates.lean index 467c7137c..292d89eab 100644 --- a/Tests/InvocationCertificates.lean +++ b/Tests/InvocationCertificates.lean @@ -1,5 +1,6 @@ import CIL.Safety.CertifiedContract import CIL.Safety.ReturnMemory +import Tests.Support /-! Consumer uses of invocation certificates on `Echo(v) => v` and its caller @@ -20,11 +21,13 @@ abbrev echoMethod : Nat := 0 abbrev relayMethod : Nat := 1 /-- `Echo(v) => v` on one scalar argument. -/ -@[cil_code] def echo : CIL.Method := { returnsValue := true, locals := [], code := [.arg 0, .ret] } +@[cil_code] def echo : CIL.Method := + { returnsValue := true, locals := [], code := [.ldarg 0, .ret] } /-- `Relay(v) => Echo(v)`: one call whose result it returns. -/ @[cil_code] def relay : CIL.Method := - { returnsValue := true, locals := [], code := [.arg 0, .call echoMethod 1, .ret] } + { returnsValue := true, locals := [], code := [.ldarg 0, .call (Tests.callee 1) {}, .ret], + links := [(1, .direct echoMethod)] } /-- `Echo` then `Relay`; the program declares no statics. -/ @[cil_code] def program : CIL.Program := [echo, relay] @@ -178,7 +181,7 @@ macro "relay_finish" : tactic => `(tactic| ( )) /-- The call `Relay` makes at pc 1, standing in for a `bind_call` site. -/ -theorem relaySite.call : relay.code[1]? = some (.call echoMethod 1) := rfl +theorem relaySite.call : relay.code[1]? = some (.call (Tests.callee 1)) := rfl /-- The caller supplies the entry premises, so a callee without a precondition still hands over its certificate, whose visited-state clause is the postcondition. -/ @@ -236,15 +239,18 @@ def echoOff : MethodContract CIL.W64 CIL.W64 := { echoContract with post := fun v _ _ out => out = v + 1 } /-- Only the returned values of a computed run are compared; memories never are. -/ -theorem echo_off_rejected : ¬ echoOff.Holds program echoMethod := fun holds => by - obtain ⟨_, ⟨_, values⟩, invoked, -, out, encoded, wrong⟩ := - holds sample empty ⟨empty_formed, empty_world⟩ - have observed := invoke_ok_observation_unique Prod.snd - (rfl : (invoke program echoFuel echoMethod (echoOff.args sample) empty).map Prod.snd = - .ok sampleArgs) - invoked - dsimp only [echoOff, echoContract] at observed encoded wrong - subst observed wrong +theorem echo_off_rejected : echoOff.Refuted program echoMethod := by + have mapped : (invoke program echoFuel echoMethod (echoOff.args sample) empty).map Prod.snd = + .ok sampleArgs := rfl + obtain ⟨⟨final, values⟩, invoked, rfl⟩ : ∃ result, + invoke program echoFuel echoMethod (echoOff.args sample) empty = .ok result ∧ + result.2 = sampleArgs := by + revert mapped + cases invoke program echoFuel echoMethod (echoOff.args sample) empty <;> simp [Except.map] + refine .of_return (input := sample) (initial := empty) ⟨empty_formed, empty_world⟩ invoked ?_ + rintro ⟨-, out, encoded, wrong⟩ + dsimp only [echoOff, echoContract] at encoded wrong + subst wrong simp at encoded #print axioms echo_off_rejected diff --git a/Tests/ItemPool.cs b/Tests/ItemPool.cs index 177451f1c..c6ad16ab4 100644 --- a/Tests/ItemPool.cs +++ b/Tests/ItemPool.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.RegularExpressions; using Mono.Cecil; diff --git a/Tests/JointReplay.cs b/Tests/JointReplay.cs index 078fb1bea..06dd375e7 100644 --- a/Tests/JointReplay.cs +++ b/Tests/JointReplay.cs @@ -1,18 +1,201 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs #:include ConsumerTraceDirectory.cs +using System.Diagnostics; +using System.Text.Json; + +if (args is not ([] or ["--audit-only"] or ["--results-only"] or ["--diagnostics-only"])) + throw new ArgumentException("Usage: JointReplay.cs [--audit-only|--results-only|--diagnostics-only]"); +CheckSeparateReplayResults(); +CheckReplayDiagnostics(); +CheckReplayWorkers(); +if (args is ["--results-only"]) return; using var verification = new Program(); -if (args is not ([] or ["--audit-only"])) - throw new ArgumentException("Usage: JointReplay.cs [--audit-only]"); var root = At("artifacts/JointReplay"); if (Directory.Exists(root)) Directory.Delete(root, true); Directory.CreateDirectory(root); var environment = new Dictionary { ["LEAN_SYSROOT"] = verification.Toolchain }; +void CheckReplayWorkers() +{ + var original = Environment.GetEnvironmentVariable("LEAN_NUM_THREADS"); + try + { + foreach (var configured in new string?[] { null, "", " ", "8" }) + { + Environment.SetEnvironmentVariable("LEAN_NUM_THREADS", configured); + if (ReplayWorkerCount() != (configured == "8" ? "8" : "2")) + throw new InvalidOperationException("Isolated replay worker count changed."); + } + foreach (var configured in new[] { "0", "-1", "invalid" }) + { + Environment.SetEnvironmentVariable("LEAN_NUM_THREADS", configured); + try { ReplayWorkerCount(); } + catch (InvalidDataException) { continue; } + throw new InvalidOperationException("Invalid isolated replay worker count was accepted."); + } + } + finally { Environment.SetEnvironmentVariable("LEAN_NUM_THREADS", original); } + Console.WriteLine("PASS isolated replay worker configuration"); +} + +void CheckSeparateReplayResults() +{ + const string success = """{"module":"A","milliseconds":1}"""; + const string failure = """{"module":"A","error":"rejected"}"""; + void Reject(string[] targets, int code, string output) + { + try { ParseSeparateReplayResults(targets, code, output); } + catch (InvalidDataException) { return; } + throw new InvalidOperationException("Invalid replay result was accepted: " + output); + } + if (ParseSeparateReplayResults(["A"], 0, "progress\r\n " + success + " \r\nfinished").Count != 0 || + ParseSeparateReplayResults(["A"], 1, failure)["A"] != "rejected" || + ParseSeparateReplayResults([], 0, "progress\n").Count != 0) + throw new InvalidOperationException("Valid replay results changed"); + var mixed = ParseSeparateReplayResults(["A", "B"], 7, + success + "\n" + """{"error":"kernel rejected B","module":"B"}"""); + if (mixed.Count != 1 || mixed["B"] != "kernel rejected B") + throw new InvalidOperationException("Replay failure ownership changed"); + if (ParseSeparateReplayResults(["A"], 0, + """{"milliseconds":18446744073709551615,"module":"A"}""").Count != 0) + throw new InvalidOperationException("Maximum elapsed value was rejected"); + if (ParseSeparateReplayResults(["A"], 0, """{"mo\u0064ule":"A","milliseconds":1}""").Count != 0) + throw new InvalidOperationException("Escaped field name was not read as its unescaped name"); + Reject(["A"], 0, success + "\n" + success); + Reject(["A"], 1, failure + "\n" + failure); + Reject(["A"], 1, success + "\n" + failure); + Reject(["A"], 0, success + "\n" + success.Replace("A", "B", StringComparison.Ordinal)); + Reject(["A"], 1, success + "\n" + failure.Replace("A", "B", StringComparison.Ordinal)); + Reject(["A", "B"], 0, success); + Reject(["A"], 1, success); + Reject(["A"], 0, failure); + Reject(["A"], 0, ""); + Reject(["A"], 0, success.Replace("A", "a", StringComparison.Ordinal)); + Reject([], 0, success); + Reject([], 1, ""); + Reject(["A", "A"], 0, success); + Reject([""], 0, ""); + Reject([" "], 0, ""); + try { SeparateReplayTargets(["A", "A"]); throw new Exception("Duplicate input targets accepted"); } + catch (InvalidDataException) { } + foreach (var malformed in new[] + { + "{", "{}", "{invalid", success + " trailing", """{"milliseconds":0}""", + """{"module":"A"}""", """{"module":null,"milliseconds":0}""", + """{"module":"","milliseconds":0}""", """{"module":" ","milliseconds":0}""", + """{"module":"A","module":"A","milliseconds":0}""", + """{"module":"A","mo\u0064ule":"A","milliseconds":0}""", + """{"module":"A","milliseconds":0,"milliseconds":1}""", + """{"module":"A","error":"one","error":"two"}""", + """{"module":"A","milliseconds":0,"extra":true}""", + """{"module":"A","milliseconds":0,"error":"rejected"}""" + }) Reject(["A"], 0, success + "\n" + malformed); + foreach (var invalidElapsed in new[] { "-1", "1.5", "1.0", "1e0", "18446744073709551616", + "null", "true", "\"1\"", "[]", "{}" }) + Reject(["A"], 0, "{\"module\":\"A\",\"milliseconds\":" + invalidElapsed + "}"); + foreach (var invalidError in new[] { "null", "true", "1", "\"\"", "[]", "{}" }) + Reject(["A"], 1, "{\"module\":\"A\",\"error\":" + invalidError + "}"); + Console.WriteLine("PASS separate replay exact records, target coverage, field types and exit status"); + + long previousBytes = 0; + double previousMilliseconds = 0; + // Each sample parses the same number of records, so the larger sizes stay well under a second. + const int recordsPerSample = 65536; + // A parse that allocates a line array, line strings or field-name strings exceeds this per record. + const int maxBytesPerRecord = 256; + foreach (int count in new[] { 256, 1024, 4096, 16384 }) + { + var targets = Enumerable.Range(0, count).Select(index => "Module" + index).ToArray(); + var output = string.Join('\n', targets.Select(module => JsonSerializer.Serialize(new { module, milliseconds = 1 }))); + for (int warm = 0; warm < 4; warm++) ParseSeparateReplayResults(targets, 0, output); + long before = GC.GetAllocatedBytesForCurrentThread(); + ParseSeparateReplayResults(targets, 0, output); + long bytes = GC.GetAllocatedBytesForCurrentThread() - before; + int repetitions = recordsPerSample / count; + // The fastest sample is the parse itself; slower samples add scheduler and collector delays. + double milliseconds = double.MaxValue; + for (int sample = 0; sample < 9; sample++) + { + var timer = Stopwatch.StartNew(); + for (int repeat = 0; repeat < repetitions; repeat++) ParseSeparateReplayResults(targets, 0, output); + milliseconds = Math.Min(milliseconds, timer.Elapsed.TotalMilliseconds / repetitions); + } + if (bytes > (long)maxBytesPerRecord * count) + throw new InvalidOperationException( + $"Separate replay parsing allocated {bytes} bytes for {count} records, " + + $"over {maxBytesPerRecord} per record"); + if (previousBytes != 0 && (bytes > previousBytes * 6 || milliseconds > previousMilliseconds * 6 + 1)) + throw new InvalidOperationException( + $"Separate replay parsing exceeded its growth bound at {count}: {bytes} allocated bytes and " + + $"{milliseconds:F3} ms against {previousBytes} bytes and {previousMilliseconds:F3} ms"); + Console.WriteLine($"PASS replay parsing {count}: {bytes} allocated bytes, {milliseconds:F3} ms"); + previousBytes = bytes; + previousMilliseconds = milliseconds; + } +} + +void CheckReplayDiagnostics() +{ + const string accepted = """{"module":"A","outcome":"accepted"}"""; + const string rejected = + """{"module":"A","outcome":"rejected","declaration":"A.bad","kind":"declTypeMismatch"}"""; + const string complete = """{"complete":true}"""; + void Reject(string[] targets, int code, string output) + { + try { ParseReplayDiagnostics(targets, code, output); } + catch (InvalidDataException) { return; } + throw new InvalidOperationException("Invalid diagnostic accepted: " + output); + } + if (ParseReplayDiagnostics(["A"], 0, accepted + "\n" + complete).Count != 0 || + ParseReplayDiagnostics(["A"], 0, rejected + "\n" + complete)["A"].Declaration != "A.bad" || + ParseReplayDiagnostics([], 0, complete).Count != 0 || + ParseReplayDiagnostics(["A"], 0, + """{"module":"A","outcome":"unavailable","error":"resource"}""" + "\n" + complete).Count != 0) + throw new InvalidOperationException("Valid diagnostic rejected"); + Reject(["A"], 1, rejected + "\n" + complete); + Reject(["A"], 0, rejected); + Reject(["A"], 0, complete + "\n" + rejected); + Reject(["A"], 0, rejected + "\n" + rejected + "\n" + complete); + Reject(["A", "B"], 0, rejected + "\n" + complete); + Reject(["B"], 0, rejected + "\n" + complete); + Reject(["A", "A"], 0, rejected + "\n" + complete); + foreach (var kind in new[] { "\"0\"", "0", "null", "\"other\"", "\"interrupted\"", + "\"deterministicTimeout\"", "\"excessiveMemory\"", "\"deepRecursion\"" }) + Reject(["A"], 0, rejected.Replace("\"declTypeMismatch\"", kind, StringComparison.Ordinal) + + "\n" + complete); + foreach (var record in new[] { "{", "{}", "{\"complete\":false}", + accepted.Replace("\"A\"", "null", StringComparison.Ordinal), + rejected.Replace("\"A.bad\"", "\"\"", StringComparison.Ordinal), + rejected.Replace("\"kind\":", "\"kind\":0,\"kind\":", StringComparison.Ordinal), + accepted[..^1] + ",\"extra\":true}" }) + Reject(["A"], 0, record + "\n" + complete); + long previous = 0; + foreach (int count in new[] { 64, 256, 1024 }) + { + var targets = Enumerable.Range(0, count).Select(index => "M" + index).ToArray(); + var output = string.Join('\n', targets.Select(module => JsonSerializer.Serialize(new + { + module, outcome = "rejected", declaration = module + ".bad", kind = "declTypeMismatch" + }))) + "\n" + complete; + ParseReplayDiagnostics(targets, 0, output); + var before = GC.GetAllocatedBytesForCurrentThread(); + if (ParseReplayDiagnostics(targets, 0, output).Count != count) throw new Exception("Lost records"); + var bytes = GC.GetAllocatedBytesForCurrentThread() - before; + if (previous != 0 && bytes > previous * 6) throw new Exception("Diagnostic parsing allocation growth"); + Console.WriteLine($"PASS diagnostic parser {count}: {bytes} allocated bytes"); + previous = bytes; + } +} + void Compile(string module, string source) { var path = Path.Combine(root, module + ".lean"); @@ -48,8 +231,9 @@ set_option debug.skipKernelTC true Compile("JointClean", "theorem JointDuplicate.same : True := True.intro\n"); Compile("JointUnapproved", "axiom JointUnapproved.oracle : True\n" + "theorem JointDuplicate.same : True := JointUnapproved.oracle\n"); -Compile("JointRefutation", "def CIL.Safety.MethodContract.Holds : Prop := False\n" + - "theorem JointRefutation.test : ¬ CIL.Safety.MethodContract.Holds := fun h => h\n"); +// A same-named predicate of another module is no contract, so stating it is no refutation. +Compile("JointOwnRefuted", "def CIL.Safety.MethodContract.Refuted : Prop := True\n" + + "theorem JointOwnRefuted.test : CIL.Safety.MethodContract.Refuted := trivial\n"); Compile("JointVisible", "theorem JointVisible.correctness : True := True.intro\n"); Compile("JointHidden", """ import Lean @@ -80,15 +264,58 @@ set_option debug.skipKernelTC true "theorem JointForgedDefinitionUse.same : JointForgedDefinition.value = JointForgedDefinition.value := rfl\n"); Compile("JointForgedChain", "import JointForged\ntheorem JointForgedChain.use : False := JointForged.false\n"); +void CheckIsolatedDiagnostics() +{ + if (args is not ["--diagnostics-only"]) verification.Library(Inputs(At("CIL.lean"))); + var executable = args is ["--diagnostics-only"] + ? At(".lake/build/bin/leancil-audit" + (OperatingSystem.IsWindows() ? ".exe" : "")) + : verification.CheckedAuditExecutable(); + string[] targets = ["JointA", "JointForged", "JointForgedDefinition", "JointForgedChain", + "JointHidden", "JointMalformed"]; + var input = Path.Combine(root, "diagnostic-targets.json"); + File.WriteAllText(input, JsonSerializer.Serialize(targets)); + var result = Execute([executable, "--diagnose-replay", input], root, root, environment: environment); + var rejected = ParseReplayDiagnostics(targets, result.Code, result.Stdout); + if (rejected.Count != 3 || rejected["JointForged"].Declaration != "JointForged.false" || + rejected["JointForgedDefinition"].Declaration != "JointForgedDefinition.value" || + rejected["JointHidden"].Declaration != "JointHidden.correctness") + throw new InvalidOperationException("Isolated diagnostic ownership changed: " + result.Output); + Console.WriteLine("PASS isolated typed rejection, imported dependency exclusion and unavailable olean"); +} + +if (args is ["--diagnostics-only"]) +{ + CheckIsolatedDiagnostics(); + return; +} + void CheckFusedReplay() { var shadow = Path.Combine(root, "Shadow"); Directory.CreateDirectory(shadow); File.WriteAllText(Path.Combine(shadow, "Init.olean"), "Not an olean"); - var leanPath = shadow + Path.PathSeparator + root; + if (verification.library is null) verification.Library(Inputs(At("CIL.lean"))); + var library = Path.Combine(verification.library!, ".lake/build/lib/lean"); + var leanPath = shadow + Path.PathSeparator + root + Path.PathSeparator + library; + // A refutation of the library's contract predicate needs the library on the search path. + var refutationSource = Path.Combine(root, "JointRefutation.lean"); + File.WriteAllText(refutationSource, """ + import CIL.Safety.MethodContract + open CIL.Safety + def JointRefutation.returns : MethodContract Unit where + args _ := [] + pre _ _ := True + footprint _ := [] + post _ _ _ _ := True + theorem JointRefutation.test : JointRefutation.returns.Refuted [] 0 := + .of_fault (input := ()) (initial := Memory.empty) (observed := 1) + (fault := ⟨0, 0, .invalidState⟩) trivial (by decide) rfl + """); + Run([verification.LeanExecutable, "--root=" + root, "-o", Path.Combine(root, "JointRefutation.olean"), + refutationSource], root, root + Path.PathSeparator + library, environment: environment); System.Text.Json.JsonElement Audit(string[] targets, string module, string theorem, bool fused, - bool rejected = false, bool metadata = false) + bool rejected = false, bool metadata = false, string[]? axioms = null, bool term = false) { var request = Path.Combine(root, "fused-request.json"); var result = Path.Combine(root, "fused-result.json"); @@ -97,7 +324,7 @@ System.Text.Json.JsonElement Audit(string[] targets, string module, string theor ["modules"] = new[] { module }, ["replayTargets"] = targets, ["theorems"] = new[] { - new { name = theorem, axioms = Array.Empty(), statement = true, refutation = true } + new { name = theorem, axioms = axioms ?? [], statement = true, refutation = true, term } } }; if (metadata) @@ -119,15 +346,27 @@ System.Text.Json.JsonElement Audit(string[] targets, string module, string theor Audit(["JointA"], "JointA", "JointA.base", fused: true); Audit(["JointA"], "JointA", "JointA.base", fused: false, metadata: true); Console.WriteLine("PASS fused and metadata fallback audit cannot shadow toolchain modules"); + var imported = Audit(["JointClean"], "JointA", "JointA.base", fused: false, term: true); + var replayed = Audit(["JointA"], "JointA", "JointA.base", fused: true, term: true); + if (!JsonElement.DeepEquals(imported.GetProperty("theorems")[0].GetProperty("term"), + replayed.GetProperty("theorems")[0].GetProperty("term"))) + throw new InvalidOperationException("Partial replay changed the statement of an independently imported theorem"); + Console.WriteLine("PASS partially replayed audit preserves imported theorem statements"); Audit(["JointClean", "JointUnapproved"], "JointUnapproved", "JointDuplicate.same", fused: false, rejected: true); Audit(["JointUnapproved", "JointClean"], "JointUnapproved", "JointDuplicate.same", fused: false, rejected: true); Console.WriteLine("PASS duplicate theorem bodies retain their own rejected axiom closure in both orders"); - var refutation = Audit(["JointRefutation"], "JointRefutation", "JointRefutation.test", fused: false); - if (refutation.GetProperty("theorems")[0].GetProperty("refutes").GetProperty("line").GetInt32() != 2) + // The library's refutation lemmas use the two standard axioms. + var refutation = Audit(["JointRefutation"], "JointRefutation", "JointRefutation.test", fused: false, + axioms: ["propext", "Quot.sound"]); + if (refutation.GetProperty("theorems")[0].GetProperty("refutes").GetProperty("line").GetInt32() != 8) throw new InvalidOperationException("Fused fallback lost the refutation's source location"); - Console.WriteLine("PASS refutation audit retains original source locations"); + var own = Audit(["JointOwnRefuted"], "JointOwnRefuted", "JointOwnRefuted.test", fused: false); + if (own.GetProperty("theorems")[0].TryGetProperty("refutes", out var ownRefutes) && + ownRefutes.ValueKind != JsonValueKind.Null) + throw new InvalidOperationException("A same-named predicate of another module counted as a refutation"); + Console.WriteLine("PASS refutation audit retains original source locations and refuses same-named predicates"); var forgedRequest = Path.Combine(root, "forged-fused.json"); File.WriteAllText(forgedRequest, """ @@ -398,6 +637,7 @@ void CheckReplayedInterfaces() CheckReplays(native: false); CheckReplays(native: true); } +CheckIsolatedDiagnostics(); CheckFusedReplay(); CheckReplayedClosures(); CheckReplayedInterfaces(); diff --git a/Tests/KernelEquation.lean b/Tests/KernelEquation.lean new file mode 100644 index 000000000..cfd1a3b07 --- /dev/null +++ b/Tests/KernelEquation.lean @@ -0,0 +1,157 @@ +import CIL.Extraction.KernelEquation +import CIL.Extraction.MetadataBytes + +/-! +# Kernel equations between names and bytes + +`cil_kernel_rfl` closes an equation only when the kernel accepts it, and says which two sides it +compared when it does not. These tests check that a name read from heap bytes equals the literal +an extraction states, that one wrong byte on the heap side and one wrong character on the literal +side are each refused with both sides in the message, also under the default options a generated +module is compiled with, and that bytes which are not UTF-8 spell no name. A name that the +checker accepted for malformed bytes would let a different name pass. +-/ + +namespace Tests.KernelEquation + +open CIL.Extraction + +/-- The UTF-8 bytes of `Examples.Arithmetic`. -/ +private def arithmeticBytes : List Nat := + [69, 120, 97, 109, 112, 108, 101, 115, 46, 65, 114, 105, 116, 104, 109, 101, 116, 105, 99] + +theorem ascii_name_spelled : + spelled? arithmeticBytes = some "Examples.Arithmetic" := by cil_kernel_rfl +/-- info: 'Tests.KernelEquation.ascii_name_spelled' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms ascii_name_spelled + +-- Each member contributes a distinct dependency to the shared report. +/-- depends on axioms: [propext, Classical.choice, Quot.sound] -/ +#guard_msgs (substring := true) in +#print axioms [ascii_name_spelled, Classical.choice, Quot.sound] + +/-- A name accepted through `spelled?` has the bytes it was read from, as `bytesOf` spells them. -/ +theorem spelled_name_has_its_bytes : bytesOf "Examples.Arithmetic" = arithmeticBytes := + bytesOf_of_spelled ascii_name_spelled +#print axioms spelled_name_has_its_bytes + +/-- `café` and `日本` and `😀`: two, three and four byte characters. -/ +theorem non_ascii_name_spelled : + spelled? [99, 97, 102, 195, 169, 230, 151, 165, 230, 156, 172, 240, 159, 152, 128] = + some "café日本😀" := by cil_kernel_rfl +/-- info: 'Tests.KernelEquation.non_ascii_name_spelled' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms non_ascii_name_spelled + +-- A wrong byte on the heap side: `Eyamples` is not `Examples`. +/-- +error: cil_kernel_rfl: the kernel does not accept the two sides as equal +left: + spelled? (arithmeticBytes.set 1 121) +right: + some "Examples.Arithmetic" +-/ +#guard_msgs in +theorem wrong_heap_byte : + spelled? (arithmeticBytes.set 1 121) = some "Examples.Arithmetic" := by cil_kernel_rfl + +-- A wrong character on the literal side. +/-- +error: cil_kernel_rfl: the kernel does not accept the two sides as equal +left: + spelled? arithmeticBytes +right: + some "Examples.Arithmetid" +-/ +#guard_msgs in +theorem wrong_literal_character : + spelled? arithmeticBytes = some "Examples.Arithmetid" := by cil_kernel_rfl + +open Lean Elab Tactic Meta in +/-- Run `cil_kernel_rfl` on the equation `type` under the options this module is compiled with + and require its own refusal. `#guard_msgs` checks declarations synchronously, so a tactic that + left the kernel check to an asynchronous task would pass there and assign an unchecked proof + here. -/ +elab "refused_under_module_options " type:term : tactic => do + unless Elab.async.get (← getOptions) do + throwError "the module must be elaborated with the default asynchronous kernel checks" + let equation ← Term.elabTerm type (some (mkSort .zero)) + let goal ← mkFreshExprMVar equation + let saved ← getGoals + setGoals [goal.mvarId!] + let refusal ← try + evalTactic (← `(tactic| cil_kernel_rfl)) + pure none + catch error => pure (some (← error.toMessageData.toString)) + setGoals saved + match refusal with + | none => throwError "cil_kernel_rfl accepted a wrong equation{indentExpr equation}" + | some message => + let refused := "cil_kernel_rfl: the kernel does not accept the two sides as equal" + unless message.startsWith refused do + throwError "cil_kernel_rfl failed with another message:\n{message}" + +/-- A wrong character on the literal side and a wrong byte on the heap side are refused by the + tactic itself under the default options, outside `#guard_msgs`. -/ +example : True := by + refused_under_module_options spelled? arithmeticBytes = some "Examples.Arithmetid" + refused_under_module_options spelled? (arithmeticBytes.set 1 121) = some "Examples.Arithmetic" + trivial + +/-- An overlong encoding of NUL, a surrogate, a truncated character and a stray continuation + byte spell nothing. -/ +theorem malformed_bytes_spell_nothing : + spelled? [0xC0, 0x80] = none ∧ spelled? [0xED, 0xA0, 0x80] = none ∧ + spelled? [0xE6, 0x97] = none ∧ spelled? [0x80] = none ∧ + spelled? [0xF4, 0x90, 0x80, 0x80] = none ∧ spelled? [0xE0, 0x80, 0x80] = none := + ⟨by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl, + by cil_kernel_rfl, by cil_kernel_rfl⟩ + +-- Malformed bytes are not an empty name. +/-- +error: cil_kernel_rfl: the kernel does not accept the two sides as equal +left: + spelled? [192, 128] +right: + some "" +-/ +#guard_msgs in +theorem malformed_bytes_are_not_the_empty_name : spelled? [0xC0, 0x80] = some "" := by + cil_kernel_rfl + +/-- The spelling of a string's own bytes is the string, so a name accepted through `spelled?` is + the name `bytesOf` would have compared. -/ +theorem spelling_inverts_bytes : + ["", "System.Private.CoreLib", "café日本😀", "A`1", "Outer/Inner"].all + (fun name => spelled? (bytesOf name) == some name) = true := by decide +kernel +/-- info: 'Tests.KernelEquation.spelling_inverts_bytes' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms spelling_inverts_bytes + + +/-- `Examples.Arithmetic` stored as its bytes: `bytesOf` of it reads them without encoding. -/ +private def arithmeticText : String := + byteString arithmeticBytes "Examples.Arithmetic" ascii_name_spelled + +theorem stored_string_is_its_literal : arithmeticText = "Examples.Arithmetic" := + byteString_eq _ +#print axioms stored_string_is_its_literal + +theorem stored_string_reads_its_bytes : bytesOf arithmeticText = arithmeticBytes := by + decide +kernel +#print axioms stored_string_reads_its_bytes + +-- Bytes that are not those of the literal: the kernel refuses the string where it is defined. +/-- +error: cil_kernel_rfl: the kernel does not accept the two sides as equal +left: + spelled? (arithmeticBytes.set 1 121) +right: + some "Examples.Arithmetic" +-/ +#guard_msgs in +private def misstoredText : String := + byteString (arithmeticBytes.set 1 121) "Examples.Arithmetic" (by cil_kernel_rfl) + +end Tests.KernelEquation diff --git a/Tests/LayoutAccumulation.lean b/Tests/LayoutAccumulation.lean index e4a45c7cd..3ab893e75 100644 --- a/Tests/LayoutAccumulation.lean +++ b/Tests/LayoutAccumulation.lean @@ -1,6 +1,11 @@ import CIL.Extraction.Layout +import CIL.Extraction.TypeStorage +import CIL.Extraction.ValueDecoding -/-! Field accumulation preserves declaration order and all metadata rejection boundaries. -/ +/-! Field accumulation preserves declaration order and all metadata rejection boundaries. Named +tables with a wrong offset, size, length, child position or generic owner are rejected by both +`layoutsValidIndexed` and `LayoutsValid`, a class owner included, the owner index finds the first +matching owner, class applications stay classes, and enum arguments require integer atoms. -/ namespace Tests.LayoutAccumulation open CIL CIL.Extraction @@ -9,13 +14,16 @@ def mixed : RawLayout := { explicit := false, packing := 0, declaredSize := 0, fields := [⟨.unsigned8, none⟩, ⟨.unsigned64, none⟩, ⟨.unsigned16, none⟩] } +/-- A lookup with no nested layouts. -/ +def noLayouts : Nat → Option ComputedLayout := fun _ => none + /-- Field order and padding agree with sequential layout. -/ -theorem ordered : computeLayout #[] mixed = +theorem ordered : computeLayout noLayouts mixed = some ⟨⟨24, [⟨0, 1, false⟩, ⟨8, 8, false⟩, ⟨16, 2, false⟩]⟩, 8⟩ := by decide +kernel #print axioms ordered /-- Explicit offsets can overlap and need not increase in declaration order. -/ -theorem overlapping : computeLayout #[] { mixed with +theorem overlapping : computeLayout noLayouts { mixed with explicit := true, declaredSize := 16, fields := [⟨.unsigned64, some 2⟩, ⟨.unsigned32, some 0⟩] } = some ⟨⟨16, [⟨2, 8, false⟩, ⟨0, 4, false⟩]⟩, 8⟩ := by decide +kernel @@ -23,23 +31,273 @@ theorem overlapping : computeLayout #[] { mixed with /-- Empty layouts retain their one-byte size and missing nested layouts reject. -/ theorem emptyAndMissing : - computeLayout #[] { mixed with fields := [] } = some ⟨⟨1, []⟩, 1⟩ ∧ - computeLayout #[] { mixed with fields := [⟨.aggregate 0, none⟩] } = none := by decide +kernel + computeLayout noLayouts { mixed with fields := [] } = some ⟨⟨1, []⟩, 1⟩ ∧ + computeLayout noLayouts { mixed with fields := [⟨.aggregate 0, none⟩] } = none := by + decide +kernel #print axioms emptyAndMissing /-- Invalid packing, mixed auto layout and absent or overflowing offsets still reject. -/ -theorem invalid : computeLayout #[] { mixed with packing := 3 } = none ∧ - computeLayout #[] { mixed with auto := true } = none ∧ - computeLayout #[] { mixed with explicit := true, declaredSize := 32 } = none ∧ - computeLayout #[] { mixed with +theorem invalid : computeLayout noLayouts { mixed with packing := 3 } = none ∧ + computeLayout noLayouts { mixed with auto := true } = none ∧ + computeLayout noLayouts { mixed with explicit := true, declaredSize := 32 } = none ∧ + computeLayout noLayouts { mixed with explicit := true, declaredSize := 65536, fields := [⟨.unsigned32, some 65535⟩] } = none := by decide +kernel #print axioms invalid /-- Architecture-dependent vector alignment and the scalar restriction are unchanged. -/ theorem vectorProfiles : - computeLayout #[] { mixed with fields := [⟨.vector256, none⟩] } .scalar = none ∧ - computeLayout #[] { mixed with fields := [⟨.vector256, none⟩] } .arm64 = + computeLayout noLayouts { mixed with fields := [⟨.vector256 .unsigned64, none⟩] } .scalar = + none ∧ + computeLayout noLayouts { mixed with fields := [⟨.vector256 .unsigned64, none⟩] } .arm64 = some ⟨⟨32, [⟨0, 32, false⟩]⟩, 16⟩ := by decide +kernel #print axioms vectorProfiles + +/-- One `ulong`. -/ +def leaf : RawLayout := { + identity := "Leaf", explicit := false, packing := 0, declaredSize := 0, + fields := [⟨.unsigned64, none⟩] } + +/-- The leaf at position 0 followed by a byte. -/ +def parent : RawLayout := { + identity := "Parent", explicit := false, packing := 0, declaredSize := 0, + fields := [⟨.aggregate 0, none⟩, ⟨.unsigned8, none⟩] } + +/-- The computed layout of `leaf`. -/ +def leafLayout : ComputedLayout := ⟨⟨8, [⟨0, 8, false⟩]⟩, 8⟩ + +/-- The computed layout of `parent`. -/ +def parentLayout : ComputedLayout := ⟨⟨16, [⟨0, 8, true⟩, ⟨8, 1, false⟩]⟩, 8⟩ + +/-- The correct table passes both checks. -/ +theorem nestedAccepted : layoutsValidIndexed [leaf, parent] #[leafLayout, parentLayout] .scalar = + true ∧ LayoutsValid [leaf, parent] #[leafLayout, parentLayout] .scalar := by decide +kernel +#print axioms nestedAccepted + +/-- A repeated identity is rejected even when both rows have correct computed layouts. -/ +theorem repeatedIdentity : + ¬ LayoutsValid [leaf, { parent with identity := leaf.identity }] + #[leafLayout, parentLayout] .scalar := by decide +kernel +#print axioms repeatedIdentity + +/-- The proof form of the extraction header: the kernel runs the `Decidable` instance, which + decides the indexed check. -/ +theorem headerForm : LayoutsValid [leaf, parent] #[leafLayout, parentLayout] .scalar := by + decide +kernel + +/-- info: 'Tests.LayoutAccumulation.headerForm' depends on axioms: [propext, Classical.choice, +Quot.sound] -/ +#guard_msgs (whitespace := lax) in #print axioms headerForm + +/-- `parent` with its byte one past where sequential layout places it. -/ +def offByOne : ComputedLayout := ⟨⟨16, [⟨0, 8, true⟩, ⟨9, 1, false⟩]⟩, 8⟩ + +/-- `parent` with a size that still contains every field. -/ +def oversized : ComputedLayout := ⟨⟨24, [⟨0, 8, true⟩, ⟨8, 1, false⟩]⟩, 8⟩ + +/-- A claimed offset one byte off is rejected. -/ +theorem wrongOffset : + layoutsValidIndexed [leaf, parent] #[leafLayout, offByOne] .scalar = false ∧ + ¬ LayoutsValid [leaf, parent] #[leafLayout, offByOne] .scalar := by decide +kernel +#print axioms wrongOffset + +/-- A claimed size that still contains every field is rejected. -/ +theorem wrongSize : + layoutsValidIndexed [leaf, parent] #[leafLayout, oversized] .scalar = false ∧ + ¬ LayoutsValid [leaf, parent] #[leafLayout, oversized] .scalar := by decide +kernel +#print axioms wrongSize + +/-- A struct cannot contain itself, even when the claimed table is self-consistent. -/ +theorem childAtParent : + layoutsValidIndexed [{ parent with fields := [⟨.aggregate 0, none⟩] }] + #[⟨⟨8, [⟨0, 8, true⟩]⟩, 8⟩] .scalar = false ∧ + ¬ LayoutsValid [{ parent with fields := [⟨.aggregate 0, none⟩] }] + #[⟨⟨8, [⟨0, 8, true⟩]⟩, 8⟩] .scalar := by decide +kernel +#print axioms childAtParent + +/-- `parent` holding the layout at `child` and then a byte. -/ +def parentOf (child : Nat) : RawLayout := + { parent with fields := [⟨.aggregate child, none⟩, ⟨.unsigned8, none⟩] } + +/-- A child listed after its parent is rejected, even when the claimed table agrees with it. -/ +theorem childAfterParent : + layoutsValidIndexed [parentOf 1, leaf] #[parentLayout, leafLayout] .scalar = false ∧ + ¬ LayoutsValid [parentOf 1, leaf] #[parentLayout, leafLayout] .scalar := by decide +kernel +#print axioms childAfterParent + +/-- A child index past the end of the table is rejected. -/ +theorem childPastEnd : + layoutsValidIndexed [leaf, parentOf 5] #[leafLayout, parentLayout] .scalar = false ∧ + ¬ LayoutsValid [leaf, parentOf 5] #[leafLayout, parentLayout] .scalar := by decide +kernel +#print axioms childPastEnd + +/-- A claimed table longer or shorter than the raw table is rejected. -/ +theorem wrongLength : + layoutsValidIndexed [leaf, parent] #[leafLayout] .scalar = false ∧ + ¬ LayoutsValid [leaf, parent] #[leafLayout] .scalar ∧ + layoutsValidIndexed [leaf] #[leafLayout, leafLayout] .scalar = false ∧ + ¬ LayoutsValid [leaf] #[leafLayout, leafLayout] .scalar := by decide +kernel +#print axioms wrongLength + +/-- The closed generic owner `S::Wrap{number}`1` applied to `Leaf`. -/ +def wrapType (number : Nat) : TypeExpr := + .apply (.constructor "S" s!"Wrap{number}`1") (.aggregate "Leaf" 0) + +/-- `Wrap{number}` with the given original field type, stored as the layout at `child`. -/ +def wrap (number : Nat) (field : TypeExpr) (child : Nat) : RawLayout := { + identity := s!"S::Wrap{number}`1", explicit := false, packing := 0, declaredSize := 0, + generic := some { + owner := .constructor "S" s!"Wrap{number}`1", arity := 1, arguments := [.aggregate "Leaf" 0], + fields := [field], constraints := [(0, [])] }, + fields := [⟨.aggregate child, none⟩] } + +/-- The original field type of `Wrap2`: `Wrap1` applied to the type parameter. -/ +def wrapsFirst : TypeExpr := .apply (.constructor "S" "Wrap1`1") (.parameter 0) + +/-- `Wrap2` holds `Wrap1`, found through its closed generic owner. -/ +def genericTable : List RawLayout := [leaf, wrap 1 (.parameter 0) 0, wrap 2 wrapsFirst 1] + +/-- The computed layouts of `genericTable`. -/ +def genericLayouts : Array ComputedLayout := + #[leafLayout, ⟨⟨8, [⟨0, 8, true⟩]⟩, 8⟩, ⟨⟨8, [⟨0, 8, true⟩]⟩, 8⟩] + +/-- The generic table passes both checks. -/ +theorem genericAccepted : layoutsValidIndexed genericTable genericLayouts .scalar = true ∧ + LayoutsValid genericTable genericLayouts .scalar := by decide +kernel +#print axioms genericAccepted + +/-- A generic layout whose owner differs from the one its identity spells is rejected by the + identity check, before any owner lookup. -/ +theorem wrongOwner : + layoutsValidIndexed (genericTable.set 2 + { wrap 2 wrapsFirst 1 with generic := (wrap 3 wrapsFirst 1).generic }) + genericLayouts .scalar = false ∧ + ¬ LayoutsValid (genericTable.set 2 + { wrap 2 wrapsFirst 1 with generic := (wrap 3 wrapsFirst 1).generic }) + genericLayouts .scalar := by decide +kernel +#print axioms wrongOwner + +/-- A field naming a generic owner that no layout has is rejected: the owner lookup finds + nothing. -/ +theorem missingOwner : + layoutsValidIndexed (genericTable.set 2 + (wrap 2 (.apply (.constructor "S" "Missing`1") (.parameter 0)) 1)) + genericLayouts .scalar = false ∧ + ¬ LayoutsValid (genericTable.set 2 + (wrap 2 (.apply (.constructor "S" "Missing`1") (.parameter 0)) 1)) + genericLayouts .scalar := by decide +kernel +#print axioms missingOwner + +/-- A field whose owner lookup finds position 1 while the stored field names position 0 is + rejected. -/ +theorem otherOwner : + layoutsValidIndexed (genericTable.set 2 (wrap 2 wrapsFirst 0)) genericLayouts .scalar = + false ∧ + ¬ LayoutsValid (genericTable.set 2 (wrap 2 wrapsFirst 0)) genericLayouts .scalar := by + decide +kernel +#print axioms otherOwner + +/-- `Wrap1` and `Wrap2` have the same layout, and `Wrap3` holds `Wrap2`, stored at `child`. -/ +def sameShapes (child : Nat) : List RawLayout := + [leaf, wrap 1 (.parameter 0) 0, wrap 2 (.parameter 0) 0, + wrap 3 (.apply (.constructor "S" "Wrap2`1") (.parameter 0)) child] + +/-- The computed layouts of `sameShapes`, the same for every `child` of equal shape. -/ +def sameShapeLayouts : Array ComputedLayout := + #[leafLayout, ⟨⟨8, [⟨0, 8, true⟩]⟩, 8⟩, ⟨⟨8, [⟨0, 8, true⟩]⟩, 8⟩, ⟨⟨8, [⟨0, 8, true⟩]⟩, 8⟩] + +/-- Only the owner lookup tells `Wrap1` from `Wrap2`: storing the other owner of the same shape + is rejected while the claimed table stays the same. -/ +theorem swappedOwner : + layoutsValidIndexed (sameShapes 2) sameShapeLayouts .scalar = true ∧ + layoutsValidIndexed (sameShapes 1) sameShapeLayouts .scalar = false ∧ + ¬ LayoutsValid (sameShapes 1) sameShapeLayouts .scalar := by decide +kernel +#print axioms swappedOwner + +/-- Of two layouts with one closed type, the owner index finds the first, as the list scan does. -/ +theorem firstOwner : + (GenericOwnerIndex.ofList [leaf, wrap 1 (.parameter 0) 0, wrap 1 (.parameter 0) 0]).find + (wrapType 1) = some 1 ∧ + genericLayoutIndex [leaf, wrap 1 (.parameter 0) 0, wrap 1 (.parameter 0) 0] (wrapType 1) = + some 1 := by decide +kernel +#print axioms firstOwner +/-- `Wrap1` stored with a class as its generic owner; its identity is spelled as the + struct's is. -/ +def classWrap : RawLayout := { + wrap 1 (.parameter 0) 0 with + generic := some { + owner := .objectConstructor "S" "Wrap1`1", arity := 1, arguments := [.aggregate "Leaf" 0], + fields := [.parameter 0], constraints := [(0, [])] } } + +/-- `Wrap1` applied as a class. -/ +def classWrapType : TypeExpr := .apply (.objectConstructor "S" "Wrap1`1") (.aggregate "Leaf" 0) + +/-- A generic layout whose owner is a class is rejected, while the same layout with a struct + owner is accepted: a class instance is a reference and never a struct's storage. -/ +theorem classOwnerRejected : + layoutsValidIndexed [leaf, wrap 1 (.parameter 0) 0] (genericLayouts.extract 0 2) .scalar = + true ∧ + layoutsValidIndexed [leaf, classWrap] (genericLayouts.extract 0 2) .scalar = false ∧ + ¬ LayoutsValid [leaf, classWrap] (genericLayouts.extract 0 2) .scalar := by decide +kernel +#print axioms classOwnerRejected + +/-- Closing a class application never looks it up, so it stays a class even against a table + that claims it; the struct application still closes to its entry. -/ +theorem classApplicationStaysClass : + closeLayoutType [leaf, classWrap] classWrapType = classWrapType ∧ + closeLayoutType [leaf, wrap 1 (.parameter 0) 0] (wrapType 1) = + .aggregate "S::Wrap1`1" 1 := by decide +kernel +#print axioms classApplicationStaysClass + +/-- A generic class field keeps its owner through substitution and refuses struct decoding. -/ +theorem classFieldRefusesStructLayout : + let head := TypeExpr.objectConstructor "S" "Wrap1`1" + let argument := TypeExpr.aggregate "Leaf" 0 + let source := RawOperand.field (.apply head (.parameter 0)) 0 (.parameter 0) name!"first" false + let closed := RawOperand.field classWrapType 0 argument name!"first" false + substituteOperand [argument] [leaf, classWrap] 0 source = some closed ∧ + Instruction.ofRaw? none ⟨0, 123, closed⟩ = none := by decide +kernel +#print axioms classFieldRefusesStructLayout + +/-- A class keeps its second argument even when a layout claims that exact closed owner. -/ +theorem twoArgumentClassStaysClass : + let owner := TypeExpr.objectConstructor "S" "C`2" + let first := TypeExpr.atom runtimeScope "System.Byte" + let second := TypeExpr.atom runtimeScope "System.Int32" + let applied := TypeExpr.apply (.apply owner first) second + let entry := { classWrap with generic := some { + owner, arity := 2, arguments := [first, second], fields := [], constraints := [] } } + closeLayoutType [entry] applied = applied := by decide +kernel +#print axioms twoArgumentClassStaysClass + +/-- `System.Runtime.Intrinsics.Vector128`. -/ +def vectorOfWords : TypeExpr := + .apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") + (.atom runtimeScope "System.UInt64") + +/-- `S::Wrap1` for an enum `E` over `underlying`, stored with the one field `kind`. -/ +def enumWrap (underlying : TypeExpr) (kind : FieldType) : RawLayout := { + identity := "S::Wrap1`1", explicit := false, packing := 0, declaredSize := 0, + generic := some { + owner := .constructor "S" "Wrap1`1", arity := 1, arguments := [.enum "E" underlying], + fields := [.parameter 0], constraints := [(0, [])] }, + fields := [⟨kind, none⟩] } + +/-- An enum argument requires an immediate integer atom. The indexed and propositional checks + reject the same four-byte claim for a nested enum. -/ +theorem enumArgumentNeedsIntegerUnderlying : + let int32 := TypeExpr.atom runtimeScope "System.Int32" + let integer := enumWrap int32 .signed32 + let nested := enumWrap (.enum "Inner" int32) .signed32 + let aggregate := enumWrap (.aggregate leaf.identity 0) (.aggregate 0) + let computed : Array ComputedLayout := #[.mk (.mk 4 [.mk 0 4 false]) 4] + [genericLayoutValid (.ofList [integer]) integer, + genericLayoutValid (.ofList [nested]) nested, + genericLayoutValid (.ofList [leaf, aggregate]) aggregate, + genericLayoutValid (.ofList []) (enumWrap vectorOfWords (.vector128 .unsigned64)), + layoutsValidIndexed [integer] computed .scalar, + layoutsValidIndexed [nested] computed .scalar, + decide (LayoutsValid [nested] computed .scalar)] = + [true, false, false, false, true, false, false] := by decide +kernel +#print axioms enumArgumentNeedsIntegerUnderlying end Tests.LayoutAccumulation diff --git a/Tests/LayoutAccumulationScaling.lean b/Tests/LayoutAccumulationScaling.lean index 26da8aaf0..b46daef0e 100644 --- a/Tests/LayoutAccumulationScaling.lean +++ b/Tests/LayoutAccumulationScaling.lean @@ -11,8 +11,8 @@ private def fieldStorage (previous : Array ComputedLayout) (architecture : Archi | .signed16 | .unsigned16 | .character => some (2, 2, false) | .signed32 | .unsigned32 => some (4, 4, false) | .signed64 | .unsigned64 | .nativeInt | .nativeUInt => some (8, 8, false) - | .vector128 => some (16, 16, false) - | .vector256 => some (32, if architecture == .arm64 then 16 else 32, false) + | .vector128 _ => some (16, 16, false) + | .vector256 _ => some (32, if architecture == .arm64 then 16 else 32, false) | .aggregate index => do let child ← previous[index]? return (child.layout.size, child.alignment, true) @@ -25,8 +25,7 @@ private def alignUp (size alignment : Nat) : Nat := private def autoSequential (previous : Array ComputedLayout) (raw : RawLayout) : Bool := !raw.explicit && raw.packing ≤ 0 && raw.declaredSize ≤ 0 && match raw.fields.mapM (fun field => do - if field.offset.isSome || field.type == FieldType.vector128 || - field.type == FieldType.vector256 then none else + if field.offset.isSome || (field.type matches .vector128 _ | .vector256 _) then none else let (width, _, aggregate) ← fieldStorage previous .scalar field.type if aggregate then none else some width) with | some (width :: widths) => widths.all (· == width) @@ -43,7 +42,7 @@ def baselineLayout (previous : Array ComputedLayout) (raw : RawLayout) let pack := if raw.packing ≤ 0 then 64 else raw.packing.toNat let (size, alignment, fields) ← raw.fields.foldlM (init := (0, 1, ([] : List FieldLayout))) fun (size, alignment, fields) field => do - if field.type == .vector256 && pack > 16 && architecture == .scalar then none else do + if (field.type matches .vector256 _) && pack > 16 && architecture == .scalar then none else do let (width, natural, aggregate) ← fieldStorage previous architecture field.type let aligned := min pack natural if aligned = 0 then none else do @@ -72,14 +71,16 @@ def expected (count : Nat) : ComputedLayout := theorem baseline128 : baselineLayout #[] (raw 128) = some (expected 128) := by decide +kernel #print axioms baseline128 /-- Linear kernel workload at 128 fields. -/ -theorem candidate128 : computeLayout #[] (raw 128) = some (expected 128) := by decide +kernel +theorem candidate128 : computeLayout (fun _ => none) (raw 128) = some (expected 128) := by + decide +kernel #print axioms candidate128 /-- Original append-based kernel workload at 512 fields. -/ theorem baseline512 : baselineLayout #[] (raw 512) = some (expected 512) := by decide +kernel #print axioms baseline512 /-- Linear kernel workload at 512 fields. -/ -theorem candidate512 : computeLayout #[] (raw 512) = some (expected 512) := by decide +kernel +theorem candidate512 : computeLayout (fun _ => none) (raw 512) = some (expected 512) := by + decide +kernel #print axioms candidate512 end Tests.LayoutAccumulationScaling diff --git a/Tests/LayoutCoverage.lean b/Tests/LayoutCoverage.lean index 5d5a2dde3..465ecfff0 100644 --- a/Tests/LayoutCoverage.lean +++ b/Tests/LayoutCoverage.lean @@ -1,5 +1,7 @@ import CIL.Extraction.LayoutCoverage +/-! Layout coverage for struct fields, span resets, locals and parameters. -/ + namespace Tests.LayoutCoverage open CIL CIL.Extraction @@ -11,18 +13,20 @@ private def layouts : List RawLayout := fields := [⟨.unsigned64, none⟩] }] private def packet : TypeExpr := .aggregate "Packet" 0 private def word : TypeExpr := .atom - "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" "System.UInt64" + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" + "System.UInt64" private def body : RawMethod := { token := 1, owner := "Fixture", hasThis := false, explicitThis := false, callingConvention := 0, attributes := 0, implementation := 0, returnType := word, parameters := [packet], locals := [packet], initLocals := true, maxStack := 8, genericParameters := [], - code := [⟨0, 0x7b, .field packet 0 word false⟩, - ⟨5, 0xfe15, .type packet⟩, ⟨11, 0x0f, .argument 0⟩] } + code := [⟨0, 0x7b, .field packet 0 word name!"first" false⟩, + ⟨5, 0xfe15, .type (.apply (.constructor runtimeScope "System.Span`1") packet)⟩, + ⟨11, 0x0f, .argument 0⟩] } private def arguments : RawArguments := ⟨[.field (.aggregate 0)], [.field (.aggregate 0)], [.other, .other, .address 0]⟩ private def uses : List LayoutUse := - [.field 0 0 0 .load, .size 1 0 .initialize, .local 0 0, .parameter 0 0] + [.local 0 0, .parameter 0 0] example : LayoutUsesComplete layouts body arguments [true, true, true] uses := by decide -- Omitting any instruction, local or parameter site cannot evade validation. @@ -30,12 +34,29 @@ example : ∀ index ∈ List.range uses.length, ¬ LayoutUsesComplete layouts body arguments [true, true, true] (uses.eraseIdx index) := by decide example : ¬ LayoutUsesComplete layouts body arguments [true] uses := by decide example : ¬ LayoutUsesComplete layouts body arguments [true, true, true] uses.reverse := by decide +/-- Typed object operations obtain their width from the closed operand. -/ +theorem object_sites : [0x70, 0x71, 0x81, 0xfe15, 0xfe1c].all (fun opcode => + instructionLayoutUses layouts 0 ⟨0, opcode, .type packet⟩ == some []) = true := by decide +#print axioms object_sites + +-- A struct field access needs no use: the step reads its entry from the checked layouts. +example : instructionLayoutUses layouts 0 + ⟨0, 0x7b, .field packet 0 word name!"first" false⟩ = some [] := by decide +example : instructionLayoutUses layouts 0 + ⟨0, 0x7b, .field packet 1 word name!"first" false⟩ = none := by decide +-- An owner naming another identity at Packet's layout index has no checked layout. example : instructionLayoutUses layouts 0 - ⟨0, 0x7b, .field packet 1 word false⟩ = none := by decide + ⟨0, 0x7b, .field (.aggregate "Counter" 0) 0 word name!"first" false⟩ = none := by decide example : instructionLayoutUses layouts 0 - ⟨0, 0x7b, .field packet 0 packet false⟩ = none := by decide + ⟨0, 0x7b, .field packet 0 packet name!"first" false⟩ = none := by decide example : instructionLayoutUses layouts 0 - ⟨0, 0x73, .call { target := .managed 1, declaringType := packet, name := ".ctor", typeArguments := [], parameterCount := 0, hasThis := true, returnsValue := false }⟩ = - some [.size 0 0 .construct] := by decide + ⟨0, 0x73, .call { + declared := { convention := 0x20 }, + method := { + owner := packet, name := name!".ctor", hasThis := true, genericArity := 0, + returnType := .void, parameters := [] }, + target := .managed 1, name := ".ctor", typeArguments := [], + parameterCount := 0, returnsValue := false }⟩ = + some [.size 0 0] := by decide end Tests.LayoutCoverage diff --git a/Tests/LayoutEvidence.cs b/Tests/LayoutEvidence.cs index f5e8d3f25..078d0505d 100644 --- a/Tests/LayoutEvidence.cs +++ b/Tests/LayoutEvidence.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include RejectedEvidence.cs #:include GeneratedEvidence.cs @@ -33,19 +38,31 @@ evidence.Check(); var baseline = File.ReadAllText(Path.Combine(output, "0", "Extracted.lean")); -void Reject(string name, string mutated) +var snapshot = File.ReadAllText(Path.Combine(output, "2", "Extracted.lean")); +void Reject(string name, string source, string mutated, string marker = "decide") { - if (mutated == baseline) throw new InvalidOperationException("Mutation did not change generated source: " + name); - var path = Path.Combine(output, name + ".lean"); + if (mutated == source) + throw new InvalidOperationException($"""Mutation did not change generated source: {name}"""); + var path = Path.Combine(output, $"""{name}.lean"""); File.WriteAllText(path, mutated); - rejected.Add(path, "PASS layout evidence rejects " + name, "decide"); + rejected.Add(path, $"""PASS layout evidence rejects {name}""", marker); } -// Change lookup lemmas and instructions together: lookup rfl lemmas still hold. -Reject("field-offset", Regex.Replace(baseline, @"\.fieldAt 4 4", ".fieldAt 3 4")); +// A field operand naming another field of the struct no longer matches the decoded body. +Reject("field-index", baseline, baseline.Replace("index := 1, type := .atom", "index := 0, type := .atom"), + "the decoded method has no field operand"); // Offset 3 remains within the 8-byte aggregate, so old bounds-only validation passes. -Reject("computed-offset", baseline.Replace("⟨4, 4, false⟩", "⟨3, 4, false⟩")); -Reject("packing", baseline.Replace("packing := -1", "packing := 3").Replace("packing := 0", "packing := 3")); -Reject("omitted-layout-uses", Regex.Replace(baseline, @"def method0LayoutUses : List Extraction.LayoutUse := [^\r\n]*", "def method0LayoutUses : List Extraction.LayoutUse := []")); +Reject("computed-offset", baseline, baseline.Replace("⟨4, 4, false⟩", "⟨3, 4, false⟩")); +Reject("packing", baseline, + baseline.Replace("packing := -1", "packing := 3").Replace("packing := 0", "packing := 3")); +// A struct field access reads its layout from the checked environment, so the omitted uses are a local's. +Reject("omitted-layout-uses", snapshot, + LayoutUsesDefinition.Replace(snapshot, "def method0LayoutUses : List Extraction.LayoutUse := []")); rejected.Check(); Console.WriteLine("PASS raw sequential/nested layout recomputation and output binding"); + +partial class Program +{ + [GeneratedRegex(@"def method0LayoutUses : List Extraction.LayoutUse := [^\r\n]*")] + internal static partial Regex LayoutUsesDefinition { get; } +} diff --git a/Tests/LayoutProjectionIndexes.lean b/Tests/LayoutProjectionIndexes.lean index 2716075f9..7f03c8fbc 100644 --- a/Tests/LayoutProjectionIndexes.lean +++ b/Tests/LayoutProjectionIndexes.lean @@ -38,7 +38,7 @@ theorem duplicatePublic : ¬ LayoutProjectionValid [leaf "a"] #[byteLayout] #print axioms duplicatePublic /-- The ordered index uniqueness check rejects a distant duplicate. -/ -theorem duplicateIndex : projectionDistinct [5, 2, 8, 1, 5] = false := by decide +kernel +theorem duplicateIndex : orderedDistinct [5, 2, 8, 1, 5] = false := by decide +kernel #print axioms duplicateIndex /-- Missing artifact entries retain option lookup failure. -/ diff --git a/Tests/LeanStringEmissions.cs b/Tests/LeanStringEmissions.cs new file mode 100644 index 000000000..d12669ef9 --- /dev/null +++ b/Tests/LeanStringEmissions.cs @@ -0,0 +1,159 @@ +extern alias IdentityExtractor; + +using Mono.Cecil; +using Mono.Cecil.Cil; +using IdentityExtractor::Nethermind.LeanCIL; +using Verification = IdentityExtractor::Program; + +partial class Program +{ + static void CheckLeanStringEmissions() + { + string directory = At($"artifacts/LeanStrings-\U00010401-{Guid.NewGuid():N}"); + Directory.CreateDirectory(directory); + string build = Run(["lake", "build", "+StrictBinding.Image", "+CIL.Interface.FullTransfer"], Root); + if (build.Contains("sorryAx", StringComparison.Ordinal)) throw new InvalidOperationException(build); + string leanPath = string.Join(Path.PathSeparator, directory, At(".lake/build/lib/lean")); + var checks = new EmissionChecks(directory, leanPath); + checks.Callees(); + checks.Assumptions(); + checks.SymbolValues(); + checks.Capture(); + checks.LakeArguments(); + checks.ImageIdentity(); + } + + sealed class EmissionChecks(string directory, string leanPath) + { + const string Unusual = "X\U00010400\"\\n\b\r\n"; + + void Elaborate(string module, string source, bool compile = false) + { + string path = Path.Combine(directory, module.Replace('.', '/') + ".lean"); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllText(path, source); + string[] output = compile ? ["-o", Path.ChangeExtension(path, ".olean")] : []; + Run(["lean", "--root", directory, .. output, path], directory, leanPath); + } + + // The expected Lean string is constructed from decimal scalar values independently of all source escapers. + static string Expected(string value) => + $"""(String.ofList ([{string.Join(",", value.EnumerateRunes().Select(r => r.Value))}].map Char.ofNat))"""; + + internal void Callees() + { + string shortName = $"A::{Unusual}()"; + string longName = $"A::{Unusual}(Int32, {new string('T', 96)})"; + string shortLiteral = ProofOutline.CalleeLiteral(shortName, 20); + string wrapped = ProofOutline.CalleeLiteral(longName, 20); + if (shortLiteral.Contains('\n') || !wrapped.Contains("\\\n", StringComparison.Ordinal)) + throw new InvalidOperationException("Expected both single-line and continued callee signatures."); + Elaborate("Callees", $""" + #guard {shortLiteral} == {Expected(shortName)} + #guard {wrapped} == {Expected(longName)} + """); + Console.WriteLine("PASS short and wrapped loop callee signatures"); + } + + internal void Assumptions() + { + string assembly = Path.Combine(directory, "Assumptions.dll"); + string owner; + string[] fields; + using (var module = ModuleDefinition.ReadModule(At("Examples/Assembly/bin/Examples.dll"))) + { + var type = module.GetType("Examples.SeededHash"); + type.Name += "\U00010400"; + owner = type.FullName; + var seeds = type.Fields.Where(field => field.Name is "Seed0" or "Seed1").ToArray(); + foreach (var field in seeds) field.Name += "\b\U00010401"; + fields = seeds.Select(field => field.FullName).Order(StringComparer.Ordinal).ToArray(); + module.Write(assembly); + } + string extraction = Path.Combine(directory, "Assumptions"); + Run(["dotnet", ExtractorPath, "extract", assembly, $"{owner}::Hash(ulong)", extraction, + "--assume-initialized", owner]); + string[] names = ["initializedTypes", "staticValues", "opaqueStaticValues"]; + var definitions = File.ReadLines(Path.Combine(extraction, "Extracted.lean")).Where(line => + names.Any(name => line.StartsWith($"def Extracted.{name} :", StringComparison.Ordinal))); + string expectedFields = string.Join(", ", fields.Select(Expected)); + Elaborate("Assumptions", $""" + {string.Join('\n', definitions)} + #guard Extracted.initializedTypes == [{Expected(owner)}] + #guard Extracted.staticValues == [{expectedFields}] + #guard Extracted.opaqueStaticValues == [{expectedFields}] + """); + Console.WriteLine("PASS initialized type and both static field declarations"); + } + + internal void SymbolValues() + { + using var pdb = ModuleDefinition.CreateModule("Symbols", ModuleKind.Dll); + var method = new MethodDefinition("Read", MethodAttributes.Static, pdb.TypeSystem.Void); + pdb.Types[0].Methods.Add(method); + var instruction = Instruction.Create(OpCodes.Ret); + method.Body.Instructions.Add(instruction); + var local = new VariableDefinition(pdb.TypeSystem.Int32); + method.Body.Variables.Add(local); + var scope = new ScopeDebugInformation(instruction, null); + scope.Variables.Add(new VariableDebugInformation(local, Unusual)); + method.DebugInformation.Scope = scope; + const string document = "Source\U00010401\"\b.cs"; + method.DebugInformation.SequencePoints.Add(new SequencePoint(instruction, new Document($"src/{document}")) + { StartLine = 2, StartColumn = 3 }); + string symbols = Symbols.Emit([method], "embedded"); + File.WriteAllText(Path.Combine(directory, "Symbols.lean"), symbols); + File.WriteAllText(Path.Combine(directory, "identity.json"), """{"methods":[{"signature":"Read"}]}"""); + if (IdentityExtractor::VerifyReports.Methods(directory) is not [{ Points: [{ File: document, Pc: 0, + Line: 2, Column: 3 }] }]) + throw new InvalidOperationException("The reported sequence point differs from the PDB."); + Elaborate("SymbolValues", $""" + {symbols} + #guard ExtractedSymbols.locals == [[(0, 0, 1, {Expected(Unusual)})]] + #guard ExtractedSymbols.points == [[(0, {Expected(document)}, 2, 3)]] + """); + Console.WriteLine("PASS PDB local, relative source path and C# symbol report"); + } + + internal void Capture() + { + const string shared = "Shared\U0001D49C"; + Elaborate(shared, """ + /-- The root of the term capture belongs to the shared module. -/ + theorem LeanCILExportAudit.certified : True := True.intro + #print axioms LeanCILExportAudit.certified + """, compile: true); + using var verifier = new Verification(); + string captured = verifier.CaptureFullTerms(directory, shared, "Transferred", [new(shared, "")], leanPath); + if (!captured.Split('\n').Any(line => line == $"import {shared}")) + throw new InvalidOperationException($"Captured terms lost their shared module: {captured}"); + Console.WriteLine("PASS shared module name and capture path"); + } + + internal void LakeArguments() + { + string lakefile = Path.Combine(directory, "lakefile.lean"); + File.WriteAllText(lakefile, "import Lake\nopen Lake DSL\npackage fixture\n"); + Verification.AddConsumerLibrary(directory, "Strings", [Unusual]); + File.AppendAllText(lakefile, $"\n#guard Strings.config.moreLeanArgs == #[{Expected(Unusual)}]\n"); + Run(["lake", "env", "lean", "--version"], directory); + Console.WriteLine("PASS lakefile Lean arguments"); + } + + internal void ImageIdentity() + { + var image = new BindingImagePin(Unusual, Sha256Digest.Of([]), []); + Elaborate(image.Module, $$""" + import StrictBinding.Image + /-- The empty pinned file. -/ + def {{image.Module}}.image : StrictBinding.ImageBytes := { length := 0, pages := [] } + """, compile: true); + string images = ImagesModuleSource(new(directory, [image], 0)); + Elaborate("Images", $""" + {images} + #guard BindingAudit.images.map StrictBinding.PinnedImage.identity == [{Expected(Unusual)}] + """); + Console.WriteLine("PASS deep binding assembly identity including CRLF"); + } + } +} diff --git a/Tests/LibraryCache.cs b/Tests/LibraryCache.cs index 247b6ea8d..7ed0e4068 100644 --- a/Tests/LibraryCache.cs +++ b/Tests/LibraryCache.cs @@ -1,6 +1,12 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include DirectoryLinks.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // Child mode: an independent process holding the use lock of one entry until told to stop. if (args is ["hold-entry", var heldByChild, var childSignal, var childStop]) @@ -336,7 +342,7 @@ Program Open(string[]? closureRoots) GC.Collect(); using (var editorProbe = HoldEntryExclusively(wholeEntry)) if (editorProbe is not null) throw new Exception("An editor process did not hold its entry's lock"); - if (EvictUnused(builtCache, KeyName(), LibraryStaging(), Days(7), 1, null, library: true) + if (EvictUnused(builtCache, KeyName, LibraryStaging, Days(7), 1, null, library: true) .Contains(wholeLibraryKey) || !Directory.Exists(wholeEntry)) throw new Exception("A pass removed the entry an editor process was using"); } @@ -348,6 +354,81 @@ Program Open(string[]? closureRoots) } Console.WriteLine("PASS the whole library replaces the closure entry and is preferred; an import outside " + "a closure is refused from the compiled header in every spelling"); + // Consumer cache keys bind the library directory, so the run that builds and publishes the library must + // read the entry later runs open, or no later run can restore the support it publishes. + var buildingCache = Path.Combine(Path.GetTempPath(), "lp-" + Guid.NewGuid().ToString("N")[..8]); + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", buildingCache); + try + { + using var built = Open(null); + using var reopened = Open(null); + if (built.libraryBuild != "fresh" || built.library != Path.Combine(buildingCache, wholeLibraryKey) || + reopened.libraryBuild != "cache" || reopened.library != built.library) + throw new Exception("The run that built the library read another directory than later runs: " + + built.library + " and " + reopened.library); + } + finally + { + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", builtCache); + if (Directory.Exists(buildingCache)) Directory.Delete(buildingCache, true); + } + Console.WriteLine("PASS the run that builds and publishes the library reads the entry later runs open"); + + // Lake on Windows reports a file whose full path exceeds its limit as missing, so an entry it cannot read + // is refused when opened instead of failing the first build that requires it; the limit itself is checked. + var longest = ReadManifest(wholeEntry)!.Keys + .Where(name => name.StartsWith(CompiledLibraryPrefix, StringComparison.Ordinal)) + .MaxBy(name => name.Length)!; + var compiledModule = longest[CompiledLibraryPrefix.Length..longest.IndexOf('.', CompiledLibraryPrefix.Length)] + .Replace('/', '.'); + // A module importing the one that owns the longest file makes Lake read that file's hash; the package + // requires the entry by its absolute path, as consumer verification does. + void BuildImporter(string entry) + { + var package = Path.Combine(Path.GetTempPath(), "lq-" + Guid.NewGuid().ToString("N")[..8]); + Directory.CreateDirectory(package); + try + { + File.Copy(Path.Combine(entry, "lean-toolchain"), Path.Combine(package, "lean-toolchain")); + var location = System.Text.Json.JsonSerializer.Serialize(entry.Replace('\\', '/')); + File.WriteAllText(Path.Combine(package, "lakefile.toml"), "name = \"probe\"\n\n[[require]]\n" + + "name = \"leancil\"\npath = " + location + "\n\n[[lean_lib]]\nname = \"PathProbe\"\n"); + File.WriteAllText(Path.Combine(package, "PathProbe.lean"), "import " + compiledModule + "\n"); + Run(["lake", "build", "PathProbe"], package); + } + finally { Directory.Delete(package, true); } + } + foreach (var beyondLimit in new[] { false, true }) + { + var padded = Path.Combine(Path.GetTempPath(), "ll-"); + var rootLength = LakePathLimit + (beyondLimit ? 1 : 0) - longest.Length - wholeLibraryKey.Length - 2; + var longRoot = padded + new string('r', rootLength - padded.Length); + var longEntry = Path.Combine(longRoot, wholeLibraryKey); + CopyTree(wholeEntry, longEntry); + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", longRoot); + var reader = new Program(); + try + { + if (!beyondLimit || !OperatingSystem.IsWindows()) + { + reader.Library(Inputs(At("CIL.lean"))); + BuildImporter(longEntry); + if (reader.libraryBuild != "cache" || reader.library != longEntry) + throw new Exception("A library entry within the path limit was not opened in place"); + continue; + } + ExpectRejection(() => reader.Library(Inputs(At("CIL.lean"))), "cannot be read by Lake"); + ExpectRejection(() => BuildImporter(longEntry), "no such file"); + } + finally + { + ((IDisposable)reader).Dispose(); + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", builtCache); + Directory.Delete(longRoot, true); + } + } + Console.WriteLine($"PASS a library entry whose files Lake reads at {LakePathLimit} characters is opened; " + + "one character more is refused on Windows, where Lake fails to read it"); } finally { @@ -396,21 +477,8 @@ string PlantEntry(string root, string name, TimeSpan unused, bool receipt = true return path; } List EvictLibraries(string root, int? limit = null, string? current = null, double retentionDays = 7) => - EvictUnused(root, KeyName(), LibraryStaging(), TimeSpan.FromDays(retentionDays), limit, current, library: true); + EvictUnused(root, KeyName, LibraryStaging, TimeSpan.FromDays(retentionDays), limit, current, library: true); TimeSpan Days(double days) => TimeSpan.FromDays(days); - bool TryLink(string link, string target) - { - try { Directory.CreateSymbolicLink(link, target); return true; } - catch (Exception error) when (error is IOException or UnauthorizedAccessException) { } - if (!OperatingSystem.IsWindows()) return false; - // A junction needs no privilege. - using var mklink = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("cmd.exe", - $"/c mklink /J \"{link}\" \"{target}\"") - { CreateNoWindow = true, RedirectStandardOutput = true, UseShellExecute = false })!; - mklink.StandardOutput.ReadToEnd(); - mklink.WaitForExit(); - return mklink.ExitCode == 0 && Directory.Exists(link); - } var retained = NewEvictionRoot(); var stale = PlantEntry(retained, LibraryKeyOf('1'), Days(8)); @@ -438,7 +506,7 @@ bool TryLink(string link, string target) foreach (var file in new[] { "cache-manifest.json", "cache-receipt.hmac" }) File.WriteAllText(Path.Combine(elsewhere, file), "published"); var linkedEntry = Path.Combine(retained, LibraryKeyOf('c')); - var linked = TryLink(linkedEntry, elsewhere); + var linked = TryLinkDirectory(linkedEntry, elsewhere); if (linked) Directory.SetLastWriteTimeUtc(linkedEntry, DateTime.UtcNow - Days(30)); var removed = EvictLibraries(retained, current: Path.GetFileName(inUse)); if (removed.Count != 4 || Directory.Exists(stale) || Directory.Exists(killedPublisher) || @@ -463,7 +531,7 @@ bool TryLink(string link, string target) File.WriteAllText(Path.Combine(linkTarget, "keep.txt"), "outside the entry"); var holdsLink = NewEvictionRoot(); var withLink = PlantEntry(holdsLink, LibraryKeyOf('a'), Days(30)); - if (TryLink(Path.Combine(withLink, ".lake", "link"), linkTarget)) + if (TryLinkDirectory(Path.Combine(withLink, ".lake", "link"), linkTarget)) { if (EvictLibraries(holdsLink).Count != 1 || Directory.Exists(withLink) || !File.Exists(Path.Combine(linkTarget, "keep.txt"))) @@ -473,9 +541,9 @@ bool TryLink(string link, string target) else Console.WriteLine("SKIP link inside an entry: no link could be made"); // The names are anchored, so a name that only ends in a newline is no key. - if (KeyName().IsMatch(LibraryKeyOf('a') + "\n") || LibraryStaging().IsMatch(Staging('a') + "\n") || - SupportGeneration().IsMatch(LibraryKeyOf('a') + "-" + LibraryKeyOf('b') + "\n") || - AbandonedStaging().IsMatch(Staging('a') + "\n") || !KeyName().IsMatch(LibraryKeyOf('a'))) + if (KeyName.IsMatch(LibraryKeyOf('a') + "\n") || LibraryStaging.IsMatch(Staging('a') + "\n") || + SupportGeneration.IsMatch(LibraryKeyOf('a') + "-" + LibraryKeyOf('b') + "\n") || + AbandonedStaging.IsMatch(Staging('a') + "\n") || !KeyName.IsMatch(LibraryKeyOf('a'))) throw new Exception("A cache name pattern matched a name with a trailing newline"); Console.WriteLine("PASS cache names match exactly"); @@ -743,14 +811,14 @@ string Captured(Action action) var vanishing = NewEvictionRoot(); PlantEntry(vanishing, LibraryKeyOf('a'), Days(30)); PlantEntry(vanishing, LibraryKeyOf('b'), Days(30)); - if (EvictUnused(vanishing, KeyName(), LibraryStaging(), Days(7), 8, null, _ => Directory.Delete(vanishing, true), + if (EvictUnused(vanishing, KeyName, LibraryStaging, Days(7), 8, null, _ => Directory.Delete(vanishing, true), library: true).Count != 0) throw new Exception("A pass counted entries it could not remove from a root that vanished"); var counted = NewEvictionRoot(); PlantEntry(counted, LibraryKeyOf('a'), Days(30)); PlantEntry(counted, LibraryKeyOf('b'), Days(30)); var countReport = new PassReport(); - if (EvictUnused(counted, KeyName(), LibraryStaging(), Days(7), null, null, null, true, countReport).Count != 2 || + if (EvictUnused(counted, KeyName, LibraryStaging, Days(7), null, null, null, true, countReport).Count != 2 || countReport.Removed != 2) throw new Exception("The report did not count the directories removed"); Console.WriteLine("PASS a pin that cannot be read keeps its entry for the retention period, and no pass raises"); @@ -769,7 +837,7 @@ string Captured(Action action) var spelledEntry = PlantEntry(spelledRoot, LibraryKeyOf('a'), Days(30)); var spelledLink = Path.Combine(evictionRoots, "link-to-" + Path.GetFileName(spelledRoot)); var spelledOwner = Path.Combine(packageOwner, "spelled"); - var reachedByLink = TryLink(spelledLink, spelledRoot); + var reachedByLink = TryLinkDirectory(spelledLink, spelledRoot); var entryThroughLink = Path.Combine(reachedByLink ? spelledLink : spelledRoot, LibraryKeyOf('a')); Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", reachedByLink ? spelledLink : spelledRoot); try { FrameworkPackage(spelledOwner, entryThroughLink, pin: true); } @@ -839,9 +907,10 @@ void AgePins(string root, double days) File.WriteAllText(Path.Combine(racePackage, "lakefile.toml"), "name = \"leancil\"\nsrcDir = \"" + Path.Combine(racing, LibraryKeyOf('b')).Replace('\\', '/') + "\"\n"); var racedPin = PlantPin(racing, racedEntry, racePackage, 2); + var pinHook = beforePinRetired; beforePinRetired = pin => File.SetLastWriteTimeUtc(pin, DateTime.UtcNow); try { EvictLibraries(racing); } - finally { beforePinRetired = null; } + finally { beforePinRetired = pinHook; } if (!File.Exists(racedPin) || !Directory.Exists(racedEntry) || Directory.EnumerateFiles(racing, "*.dead-*").Any()) throw new Exception("A pin renewed while a pass retired it was deleted, or left under another name"); @@ -873,18 +942,41 @@ void AgePins(string root, double days) if (singleRead.Elapsed > TimeSpan.FromSeconds(30) || slowEntries.Any(path => !Directory.Exists(path)) || !single.SequenceEqual(new[] { Path.GetFileName(unpinnedStale) })) throw new Exception("A lakefile that never answers delayed the pass, or its entries were removed"); - // The whole pass is bounded too: it stops reading, stops removing, and says so. + // The whole pass is bounded too: a read under way when the limit comes is cut off there rather than at + // the read limit, and the pass then stops reading and removing, and says so. The pass sees only its own + // clock: the first reading fixes the limit, the second, taken by the first read, leaves it a moment, + // and every later one lies past it, so machine load cannot change what the pass does. PlantEntry(slowRoot, LibraryKeyOf('f'), Days(30)); - packageReadLimit = TimeSpan.FromSeconds(30); - passLimit = TimeSpan.FromMilliseconds(500); - var wholePass = System.Diagnostics.Stopwatch.StartNew(); + var passStart = DateTime.UtcNow; + var passReadings = 0; + var clockBefore = passClock; + packageReader = _ => { neverAnswers.Wait(TimeSpan.FromSeconds(10)); return ""; }; + packageReadLimit = TimeSpan.FromHours(1); + passLimit = TimeSpan.FromHours(1); + passClock = () => Interlocked.Increment(ref passReadings) switch + { + 1 => passStart, + 2 => passStart + passLimit - TimeSpan.FromMilliseconds(200), + _ => passStart + passLimit + passLimit + }; var gaveUp = new PassReport(); - var stopped = EvictUnused(slowRoot, KeyName(), LibraryStaging(), Days(7), null, null, null, true, gaveUp); - if (wholePass.Elapsed > TimeSpan.FromSeconds(20) || !gaveUp.GaveUp || stopped.Count != 0 || - !Directory.Exists(Path.Combine(slowRoot, LibraryKeyOf('f')))) - throw new Exception("The pass was not bounded, or did not say that it gave up"); + List stopped; + try { stopped = EvictUnused(slowRoot, KeyName, LibraryStaging, Days(7), null, null, null, true, gaveUp); } + finally { passClock = clockBefore; } + // A read waited for until the read limit would have answered after ten seconds and been judged. + if (!gaveUp.GaveUp || gaveUp.Unjudged != 1 || stopped.Count != 0 || + !Directory.Exists(Path.Combine(slowRoot, LibraryKeyOf('f'))) || + slowEntries.Any(path => !Directory.Exists(path))) + throw new Exception("The pass was not bounded, or did not say that it gave up: " + + $"gave up {gaveUp.GaveUp}, unjudged {gaveUp.Unjudged}, removed [{string.Join(", ", stopped)}]"); + // A clock that lies past the limit after its first reading makes the pass give up whatever the load. + var stageReadings = 0; + passClock = () => + Interlocked.Increment(ref stageReadings) == 1 ? passStart : passStart + passLimit + passLimit; libraryEvicted = 0; - var gaveUpStage = Captured(() => EvictUnusedLibraries(slowRoot, LibraryKeyOf('9'))); + string gaveUpStage; + try { gaveUpStage = Captured(() => EvictUnusedLibraries(slowRoot, LibraryKeyOf('9'))); } + finally { passClock = clockBefore; } if (!gaveUpStage.Contains("END ") || !gaveUpStage.Contains("s limit") || !gaveUpStage.Contains("(exit 0, ") || WithoutStageProgress(gaveUpStage).Length != 0) throw new Exception("A pass that gave up did not report it: " + gaveUpStage); @@ -952,11 +1044,53 @@ void RequireUnreadKept(string how, bool staleToo = true) { passClock = () => clockCalls++ == 0 ? jumpStart : jumpStart.AddHours(2); var jumped = new PassReport(); - EvictUnused(unread, KeyName(), LibraryStaging(), Days(7), null, null, null, true, jumped); + EvictUnused(unread, KeyName, LibraryStaging, Days(7), null, null, null, true, jumped); RequireUnreadKept("a clock that jumped"); if (!jumped.GaveUp) throw new Exception("A pass whose clock jumped past its limit did not say so"); } finally { passClock = realClock; } + // Past its limit a pass retires no pin, even one judged by its age without reading a lakefile, and a pass + // that reaches its limit while removing closure records stops there. + var agedPinRoot = NewEvictionRoot(); + var agedPin = PlantPin(agedPinRoot, PlantEntry(agedPinRoot, LibraryKeyOf('a'), Days(30)), "", 8); + var lateRoot = NewEvictionRoot(); + var lateRecord = Path.Combine(lateRoot, LibraryKeyOf('d') + ".closure"); + File.WriteAllText(lateRecord, LibraryKeyOf('7')); + var lateCalls = 0; + try + { + passLimit = TimeSpan.Zero; + EvictLibraries(agedPinRoot); + if (!File.Exists(agedPin)) throw new Exception("A pass past its limit deleted a pin"); + passLimit = zeroLimit; + // The first two readings are the deadline and the check before closure records are removed. + passClock = () => lateCalls++ < 2 ? jumpStart : jumpStart.AddHours(2); + EvictLibraries(lateRoot); + if (!File.Exists(lateRecord)) throw new Exception("A pass past its limit deleted a closure record"); + } + finally { (passLimit, passClock) = (zeroLimit, realClock); } + // A read that waited out the rest of the pass stops it even when the clock has not yet passed the + // deadline, as when a wait rounded down to whole milliseconds returns just before it. + var stalledRead = new ManualResetEventSlim(false); + var limitsBefore = (packageReader, packageReadLimit, passLimit); + try + { + var frozenAt = DateTime.UtcNow; + passClock = () => frozenAt; + packageReader = _ => { stalledRead.Wait(); return ""; }; + packageReadLimit = TimeSpan.FromSeconds(30); + passLimit = TimeSpan.FromMilliseconds(200); + var cutShort = new PassReport(); + EvictUnused(unread, KeyName, LibraryStaging, Days(7), null, null, null, true, cutShort); + RequireUnreadKept("a read that waited out the pass"); + if (!cutShort.GaveUp) throw new Exception("A pass whose read waited out its limit did not say so"); + } + finally + { + stalledRead.Set(); + passClock = realClock; + (packageReader, packageReadLimit, passLimit) = limitsBefore; + } var holdsRead = new ManualResetEventSlim(false); var readerBefore = (packageReader, packageReadLimit); try @@ -964,7 +1098,7 @@ void RequireUnreadKept(string how, bool staleToo = true) packageReader = _ => { holdsRead.Wait(); return ""; }; packageReadLimit = TimeSpan.FromMilliseconds(300); var timedOut = new PassReport(); - EvictUnused(unread, KeyName(), LibraryStaging(), Days(7), null, null, null, true, timedOut); + EvictUnused(unread, KeyName, LibraryStaging, Days(7), null, null, null, true, timedOut); RequireUnreadKept("a read that did not finish", staleToo: false); if (timedOut.Unjudged != 1) throw new Exception("A read that did not finish was not counted"); } diff --git a/Tests/LibraryConfiguration.cs b/Tests/LibraryConfiguration.cs index 371b9cf00..c323ec3f3 100644 --- a/Tests/LibraryConfiguration.cs +++ b/Tests/LibraryConfiguration.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // The certified library is bound to the lakefile it is built from, so editing a test library must // not rebuild CIL, while every setting that can change the CIL build or its auditor must. diff --git a/Tests/LibraryCopyScaling.cs b/Tests/LibraryCopyScaling.cs index 7d244c4cb..10250e065 100644 --- a/Tests/LibraryCopyScaling.cs +++ b/Tests/LibraryCopyScaling.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // Synthetic performance fixture only: never supplied to a verifier or certified as Lean output. using var certified = new Program(); diff --git a/Tests/LibraryIncremental.cs b/Tests/LibraryIncremental.cs index 415dfca23..d4c5dde4f 100644 --- a/Tests/LibraryIncremental.cs +++ b/Tests/LibraryIncremental.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; diff --git a/Tests/LibraryRelease.cs b/Tests/LibraryRelease.cs index 4d9d1bb80..f9d048bfb 100644 --- a/Tests/LibraryRelease.cs +++ b/Tests/LibraryRelease.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.IO.Compression; using System.Text.Json; diff --git a/Tests/LibraryReplayCoverage.cs b/Tests/LibraryReplayCoverage.cs index 53d38e6f0..ef3ac15ab 100644 --- a/Tests/LibraryReplayCoverage.cs +++ b/Tests/LibraryReplayCoverage.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; diff --git a/Tests/LibrarySourceExtension.cs b/Tests/LibrarySourceExtension.cs index a07ea1e32..d121f1f34 100644 --- a/Tests/LibrarySourceExtension.cs +++ b/Tests/LibrarySourceExtension.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; diff --git a/Tests/Linking.lean b/Tests/Linking.lean index be1318f7e..d6605f2f3 100644 --- a/Tests/Linking.lean +++ b/Tests/Linking.lean @@ -1,5 +1,6 @@ -import CIL.Safety.Linking +import CIL.Safety.ProgramClosure import Tests.Imports +import Tests.Support /-! `AddTo(in a, in b, out res) { res = Sum(a, b); }` extracted alone, with its helper `Sum` at local index 1. -/ namespace Tests.Linking.Calls @@ -7,10 +8,11 @@ namespace Tests.Linking.Calls open CIL CIL.Safety open Tests.Imports.Calls (addContract AddInput rootContract root root_given) -@[cil_code] def addEntry : Method := { returnsValue := false, locals := [], code := - [.arg 2, .arg 0, .arg 1, .call 1 2, .store64, .ret] } +@[cil_code] def addEntry : Method := { + returnsValue := false, locals := [], links := [(3, .direct 1)], code := + [.ldarg 2, .ldarg 0, .ldarg 1, .call (Tests.callee 2) {}, .stind .i8, .ret] } @[cil_code] def sumHelper : Method := { returnsValue := true, locals := [], code := - [.arg 0, .load64, .arg 1, .load64, .add, .ret] } + [.ldarg 0, .ldind .i8, .ldarg 1, .ldind .i8, .add, .ret] } def addLib : Library := { entry := addEntry, helpers := [sumHelper] } @[cil_code] def addProgram : CIL.Program := [addEntry, sumHelper] @@ -32,8 +34,9 @@ theorem add_holds : addContract.Holds addProgram 0 := by @[cil_code] def linked : CIL.Program := link Tests.Imports.Calls.Extracted.program [addLib] -/-- The entry fills import slot 0 at index 2 and its call to the helper is renamed from 1 to 3. -/ -example : linked.map (fun body => body.code.flatMap Op.callees) = [[2, 1], [], [3], []] := by decide +/-- The entry fills import slot 0 at index 2 and its link to the helper is renamed from 1 to 3. -/ +example : linked.map (fun body => body.links.flatMap (·.2.callees)) = + [[2, 1], [], [3], []] := by decide /-- `Root`'s conditional proof, discharged by the library proved alone and relocated. -/ theorem root_composed : rootContract.Holds linked root := @@ -71,13 +74,13 @@ open CIL CIL.Safety open Tests.Imports.Outcomes (percentContract rootContract strictRaises root root_given percentBody) @[cil_code] def percentEntry : Method := { returnsValue := true, locals := [], code := - [.arg 0, .call 1 1, .ret] } + [.ldarg 0, .call (Tests.callee 1) {}, .ret], links := [(1, .direct 1)] } def percentLib : Library := { entry := percentEntry, helpers := [percentBody] } @[cil_code] def percentProgram : CIL.Program := [percentEntry, percentBody] theorem percentLib_closed : Closed percentLib.program := Closed.of_check (by decide) -theorem checkSite.call : percentEntry.code[1]? = some (.call 1 1) := rfl +theorem checkSite.call : percentEntry.code[1]? = some (.call (Tests.callee 1)) := rfl theorem check_holds : percentContract.Holds percentProgram 1 := by rintro v m - @@ -112,7 +115,8 @@ theorem percent_holds : percentContract.Holds percentProgram 0 := by @[cil_code] def linked : CIL.Program := link Tests.Imports.Outcomes.Extracted.program [percentLib] -example : linked.map (fun body => body.code.flatMap Op.callees) = [[1], [2], [3], []] := by decide +example : linked.map (fun body => body.links.flatMap (·.2.callees)) = + [[1], [2], [3], []] := by decide theorem root_composed : rootContract.Holds linked root := composed (specs := [percentContract.relocatable]) (root_given.mono fun _ met => met 0 (by decide)) rfl @@ -137,11 +141,12 @@ theorem root_prefix_relocated : rootContract.Holds relocatedCompletion (root + 1 rfl /-- The relocated helper's exception reaches `Root` through two relocated calls. -/ -example (m : CIL.Safety.Memory) : Raises linked root [.scalar (.i64 101)] m "System.ArgumentOutOfRangeException" := +example (m : CIL.Safety.Memory) : Raises linked root [.scalar (.i64 101)] m .argumentOutOfRange := root_composed.raising (input := 101) trivial (by simp only [rootContract, strictRaises]; decide) /-- Two libraries: entries fill slots 0 and 1, then each library's helpers follow in order. -/ -example : (link [] [Tests.Linking.Calls.addLib, percentLib]).map (fun body => body.code.flatMap Op.callees) = +example : (link [] [Tests.Linking.Calls.addLib, percentLib]).map + (fun body => body.links.flatMap (·.2.callees)) = [[2], [3], [], []] := by decide #print axioms check_holds @@ -150,3 +155,109 @@ example : (link [] [Tests.Linking.Calls.addLib, percentLib]).map (fun body => bo #print axioms root_prefix_relocated end Tests.Linking.Outcomes + +/-! A relocated body renames its link table and keeps its code. -/ +namespace Tests.Linking.Links + +open CIL + +def body : Method := { + code := [.ldarg 0, .call (Tests.callee 1) {}, .pop, .callvirt (.arity 0), + .newobj (.arity 0), .ret] + links := [(1, .direct 1), (3, .virtual [(.void, 2)]), (4, .aggregate 3 8)] + locals := [], returnsValue := false } + +/-- Direct, virtual and aggregate entries all move by the renaming. -/ +example : (body.relocate (· + 10)).links = + [(1, .direct 11), (3, .virtual [(.void, 12)]), (4, .aggregate 13 8)] := rfl + +/-- The pcs of the entries stay. -/ +example : (body.relocate (· + 10)).links.map Prod.fst = body.links.map Prod.fst := rfl + +example : (body.relocate (· + 10)).linkAt 1 = some (.direct 11) := rfl + +/-- A relocated catalog keeps its identities and renames only the body indices. -/ +example (identity : ManagedIdentity) : + (Link.indirect [⟨identity, 2⟩]).relocate (· + 5) = .indirect [⟨identity, 7⟩] := rfl + +/-- The code, operands included, is unchanged, since a call reads its target from the table. -/ +example : (body.relocate (· + 10)).code = body.code := rfl + +/-- An address that the root discards reaches no callee, even when the target has a body. -/ +theorem discarded_address_closure : + let root : Method := + { code := [.ldftn ⟨"T", 1, []⟩ ⟨.void, []⟩, .pop, .ret], + links := [(0, .address 1)], locals := [], returnsValue := false } + (Program.closure [root, body] 0).map Prod.snd = some [0] := rfl +#print axioms discarded_address_closure + +end Tests.Linking.Links + +/-! How a proof reads `linkAt` for an extracted method, a template and a literal body. -/ +namespace Tests.Linking.Resolution + +open CIL + +/-- The link of the call at pc 1, named and tagged as an extraction names it. -/ +@[cil_code] def callLink : Link := .direct 3 + +/-- An extraction's method: its table and call-site fact are generated, with the table explicit. -/ +def extracted : Method := { + code := [.ldarg 0, .call (Tests.callee 1) {}, .pop, .ret], locals := [], returnsValue := false + links := [(1, callLink)] } + +/-- The generated fact of the table. -/ +@[cil_code] theorem extracted_links : extracted.links = [(1, callLink)] := rfl + +/-- The generated fact of the call site at pc 1. -/ +@[cil_code] theorem extracted_link1 : extracted.linkAt 1 = some callLink := rfl + +/-- A call site: the generated fact and the tagged link definition reach the constructor, so the + match of a stepping-style read reduces under `simp only [cil_code]`. -/ +example : (match extracted.linkAt 1 with | some (.direct callee) => callee | _ => 0) = 3 := by + simp only [cil_code] + +/-- A pc that is not a call site: no fact exists, and the table is read by the listed keys. -/ +example : extracted.linkAt 0 = none := + Method.linkAt_of_unlisted (by simp [extracted_links]) + +/-- The same read through a scan of the table, which is bounded by the number of calls. -/ +example : extracted.linkAt 2 = none := by + simp [Method.linkAt, linkLookup, extracted_links] + +/-- A literal body states the link of its call, which its code does not imply. -/ +def literal : Method := { + code := [.ldarg 0, .call (Tests.callee 1) {}, .pop, .ret], links := [(1, .direct 3)], + locals := [], returnsValue := false } + +/-- Link facts for a literal virtual call come from the explicit table. -/ +def virtualBody : Method := + { code := [.ldarg 0, .callvirt (.arity 0), .pop, .ret], locals := [], + links := [(1, .virtual [(.void, 3)])], returnsValue := false } + +cil_instruction_equations virtualBody + +example : virtualBody.linkAt 1 = some (.virtual [(.void, 3)]) := virtualBody_link1 + +example : virtualBody.linkAt 0 = none := rfl + +/-- A literal table is read by evaluation. -/ +example : literal.linkAt 1 = some (.direct 3) := rfl + +example : literal.linkAt 0 = none := Method.linkAt_of_unlisted (by decide) + +/-- A template body takes its callee as a parameter, so its read stays symbolic. -/ +def template (profile : FeatureProfile) (callee : Nat) : Method := { + profile, code := [.ldarg 0, .call (Tests.callee 1) {}, .pop, .ret], + links := [(1, .direct callee)], locals := [], returnsValue := false } + +example (profile : FeatureProfile) (callee : Nat) : + (template profile callee).linkAt 1 = some (.direct callee) := rfl + +example (profile : FeatureProfile) (callee : Nat) : (template profile callee).linkAt 2 = none := + Method.linkAt_of_unlisted (by simp [template]) + +/-- A body built with `withCode` clears all pc-indexed links. -/ +example : (literal.withCode [.call (Tests.callee 0), .ret]).linkAt 0 = none := rfl + +end Tests.Linking.Resolution diff --git a/Tests/LiteralDisplay.lean b/Tests/LiteralDisplay.lean index 7345f42a7..1fa18f793 100644 --- a/Tests/LiteralDisplay.lean +++ b/Tests/LiteralDisplay.lean @@ -71,8 +71,8 @@ open CIL.Safety in byte; `high` and `count` are the literals a case checks. -/ def literalBody (high count narrow : Nat) : CIL.Method := { returnsValue := true, locals := [], code := - [.const32 (BitVec.ofNat 32 high), .convI8, .arg 0, .const32 (BitVec.ofNat 32 count), .shl, - .const32 (BitVec.ofNat 32 narrow), .convU1, .ret] } + [.ldc_i4 (BitVec.ofNat 32 high), .conv_i8, .ldarg 0, .ldc_i4 (BitVec.ofNat 32 count), .shl, + .ldc_i4 (BitVec.ofNat 32 narrow), .conv_u1, .ret] } section LiteralOperands open CIL.Safety @@ -141,7 +141,7 @@ example (program : CIL.Program) (found : program[0]? = some (literalBody 0xFFFFF /-- A symbolic count keeps its masked form, which a consumer's lemma about it still matches. -/ def symbolicShiftBody : CIL.Method := - { returnsValue := true, locals := [], code := [.arg 0, .arg 1, .shl, .ret] } + { returnsValue := true, locals := [], code := [.ldarg 0, .ldarg 1, .shl, .ret] } example (count : W32) (rest : run [symbolicShiftBody] 2 0 3 [.scalar (.i64 x), .scalar (.i32 count)] frame diff --git a/Tests/LocalCalls.lean b/Tests/LocalCalls.lean index d68943baf..b6b4640f8 100644 --- a/Tests/LocalCalls.lean +++ b/Tests/LocalCalls.lean @@ -1,6 +1,7 @@ import CIL.Safety.LocalCalls import CIL.Safety.WideMultiply import Tests.LoopFrames +import Tests.Support /-! # Private outputs of checked helpers @@ -13,10 +14,19 @@ The same transition applies to vector outputs without assuming any previous init namespace Tests.LocalCalls open CIL.Safety +/-- The static unsigned multiplication overload taking a low-word destination. -/ +private def multiplyMethod : CIL.Extraction.MethodRef := + let word := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.UInt64" + { owner := .atom CIL.Extraction.runtimeScope "System.Math", name := name!"BigMul", + hasThis := false, genericArity := 0, parameters := [word, word, .byref word], + returnType := word } + /-- The checked body of an unsigned wide multiply with an out reference. -/ @[cil_code] def multiplyBody : CIL.Method := { locals := [], returnsValue := true, - code := [.arg 0, .arg 1, .arg 2, .memory (.bigMul64 false), .ret] } + code := [.ldarg 0, .ldarg 1, .ldarg 2, .call multiplyMethod {}, .ret] } + +cil_runtime_models multiplyBody /-- Inputs name the two factors and the actual low-word destination. -/ structure BinaryOutputInput where @@ -41,7 +51,10 @@ theorem multiply_holds {program : CIL.Program} {callee : Nat} intro input memory writable obtain ⟨final, written, _, readback⟩ := store_word64 (input.left * input.right) writable have formed := access_reference_valid _ _ _ _ _ writable - cil_contract_steps [found, formed, step_bigMul64_of_write written] + cil_contract_steps [found, formed] + refine run_next_exists _ found rfl (step_bigMul64_of_write (pc := 3) rfl + multiplyBody_model3 written) ?_ + cil_contract_steps [found] exact ⟨Confined.write written (by simp [numberBytes_length]), CIL.WideMultiply.high64 input.left input.right, rfl, rfl, readback⟩ @@ -77,7 +90,8 @@ theorem multiply_updates_mixed_frame {program : CIL.Program} {callee : Nat} @[cil_code] def callerBody : CIL.Method := { locals := [.unmodeled, .v256 42], localKinds := [.word64, .vector256], returnsValue := true, - code := [.arg 0, .arg 1, .localAddr 0, .call 1 3, .pop, .local 0, .ret] } + code := [.ldarg 0, .ldarg 1, .ldloca 0, .call (Tests.callee 3) {}, .pop, .ldloc 0, .ret], + links := [(3, .direct 1)] } bind_entry callerEntry (memory : Memory) (left right : CIL.W64) (formed : memory.WellFormed) := callerBody @@ -129,7 +143,7 @@ theorem caller_invokes (memory : Memory) (formed : memory.WellFormed) (left righ /-- Omitting the helper exposes the actual uninitialized local read, at its checked location. -/ theorem uninitialized_output_rejected : - invoke [{ callerBody with code := [.local 0, .ret] }] 1 0 [] Memory.empty = + invoke [{ callerBody with code := [.ldloc 0, .ret] }] 1 0 [] Memory.empty = .error ⟨0, 0, .memory .uninitialized (.address ⟨0, 0⟩) 8⟩ := by rfl @@ -137,15 +151,15 @@ theorem uninitialized_output_rejected : /-- A returned address to a private home is checked after teardown and cannot escape. -/ theorem escaped_home_rejected : - invoke [{ callerBody with code := [.localAddr 0, .ret] }] 2 0 [] Memory.empty = + invoke [{ callerBody with code := [.ldloca 0, .ret] }] 2 0 [] Memory.empty = .error ⟨0, 1, .memory .expiredLifetime (.address ⟨0, 0⟩) 0⟩ := by rfl #print axioms escaped_home_rejected -/-- Changing the selected helper to a missing method fails at the actual call instruction. -/ +/-- Linking the call to a missing method fails at the actual call instruction. -/ theorem wrong_helper_rejected : - invoke [{ callerBody with code := callerBody.code.set 3 (.call 2 3) }, multiplyBody] + invoke [{ callerBody with links := [(3, .direct 2)] }, multiplyBody] 4 0 [.scalar (.i64 2), .scalar (.i64 3)] Memory.empty = .error ⟨0, 3, .invalidState⟩ := by rfl @@ -154,7 +168,7 @@ theorem wrong_helper_rejected : /-- Writing the vector home instead of the selected word leaves the word uninitialized. -/ theorem wrong_output_home_rejected : - invoke [{ callerBody with code := callerBody.code.set 2 (.localAddr 1) }, multiplyBody] + invoke [{ callerBody with code := callerBody.code.set 2 (.ldloca 1) }, multiplyBody] 10 0 [.scalar (.i64 2), .scalar (.i64 3)] Memory.empty = .error ⟨0, 5, .memory .uninitialized (.address ⟨0, 0⟩) 8⟩ := by rfl @@ -203,9 +217,9 @@ def arithmeticFlag (subtract : Bool) (left right : CIL.W64) : CIL.W32 := /-- Both helpers store their wrapped word before returning the unsigned carry or borrow. -/ @[cil_code] def arithmeticBody (subtract : Bool) : CIL.Method := { locals := [], returnsValue := true, - code := [.arg 2, .arg 0, .arg 1, if subtract then .sub else .add, .store64] ++ - (if subtract then [.arg 0, .arg 1] else [.arg 0, .arg 1, .add, .arg 0]) ++ - [.ltu, .ret] } + code := [.ldarg 2, .ldarg 0, .ldarg 1, if subtract then .sub else .add, .stind .i8] ++ + (if subtract then [.ldarg 0, .ldarg 1] else [.ldarg 0, .ldarg 1, .add, .ldarg 0]) ++ + [.clt_un, .ret] } /-- Arithmetic output storage needs write authority, while the returned flag remains exact. -/ def arithmeticContract (subtract : Bool) : MethodContract BinaryOutputInput CIL.W32 where @@ -225,7 +239,7 @@ theorem arithmetic_holds (subtract : Bool) {program : CIL.Program} {callee : Nat obtain ⟨final, written, _, readback⟩ := store_word64 (arithmeticWord subtract input.left input.right) writable have formed := access_reference_valid _ _ _ _ _ writable - have stored := step_store64_of_write written + have stored := step_stind_i8_of_write written cases subtract <;> cil_contract_steps [found, formed, stored, arithmeticBody, arithmeticContract, multiplyContract, arithmeticWord, arithmeticFlag] diff --git a/Tests/LocalSnapshotSteps.lean b/Tests/LocalSnapshotSteps.lean index 07ccad400..84cf0681e 100644 --- a/Tests/LocalSnapshotSteps.lean +++ b/Tests/LocalSnapshotSteps.lean @@ -1,3 +1,4 @@ +import Tests.WordFields import CIL.Safety.CallingFields import CIL.Safety.LocalSnapshotSteps @@ -14,7 +15,7 @@ open CIL.Safety /-- Store both inputs, read the vector, and return the stored int32. -/ @[cil_code] def storeBoth : CIL.Method := { - code := [.arg 0, .setLocal 0, .arg 1, .setLocal 1, .local 0, .pop, .local 1, .ret] + code := [.ldarg 0, .stloc 0, .ldarg 1, .stloc 1, .ldloc 0, .pop, .ldloc 1, .ret] locals := [.v128 0, .i32 0], returnsValue := true } /-- A single empty snapshot collection follows stores and reloads through the whole body. -/ @@ -36,8 +37,8 @@ theorem store_and_reload {entered memory : Memory} {frame : Frame} {boundary : N /-- Overwrite the vector after the int32 store, then reload both locals. -/ @[cil_code] def overwriteVector : CIL.Method := { - code := [.arg 0, .setLocal 0, .arg 1, .setLocal 1, .arg 2, .setLocal 0, - .local 1, .pop, .local 0, .ret] + code := [.ldarg 0, .stloc 0, .ldarg 1, .stloc 1, .ldarg 2, .stloc 0, + .ldloc 1, .pop, .ldloc 0, .ret] locals := [.v128 0, .i32 0], returnsValue := true } /-- A later store replaces the remembered vector while preserving the int32 snapshot. -/ @@ -60,8 +61,8 @@ theorem overwrite_and_reload {entered memory : Memory} {frame : Frame} {boundary /-- Keep a 256-bit vector across stores and reloads of a 128-bit vector and an int32. -/ @[cil_code] def mixedVectors : CIL.Method := { - code := [.arg 0, .setLocal 0, .arg 1, .setLocal 1, .arg 2, .setLocal 2, - .local 1, .pop, .local 2, .pop, .local 0, .ret] + code := [.ldarg 0, .stloc 0, .ldarg 1, .stloc 1, .ldarg 2, .stloc 2, + .ldloc 1, .pop, .ldloc 2, .pop, .ldloc 0, .ret] locals := [.v256 0, .v128 0, .i32 0], returnsValue := true } /-- Different vector widths share one snapshot invariant and preserve the full wide value. -/ @@ -85,9 +86,9 @@ theorem mixed_vector_widths {entered memory : Memory} {frame : Frame} {boundary #print axioms mixed_vector_widths -/-- A compiled signed-byte local carries its narrow annotation on both instructions. -/ +/-- A compiled signed-byte local, whose slot kind truncates its store and extends its load. -/ @[cil_code] def narrowLocal : CIL.Method := { - code := [.arg 0, .setLocal 0 (some .signed8), .local 0 (some .signed8), .ret] + code := [.ldarg 0, .stloc 0, .ldloc 0, .ret] locals := [.i32 0], localKinds := [.narrow .signed8], returnsValue := true } /-- The snapshot remembers the truncated and extended value, not the original operand. -/ @@ -104,9 +105,29 @@ theorem narrow_round_trip {entered memory : Memory} {frame : Frame} {boundary : #print axioms narrow_round_trip +/-- A homed int32 parameter that the body overwrites with the second argument and reads back. -/ +@[cil_code] def homedArgument : CIL.Method := { + code := [.ldarg 1, .starg 0, .ldarg 0, .ret] + locals := [], parameterHomes := [(0, .word32)], returnsValue := true } + +/-- A homed argument's store and load go through its home slot's snapshot, as `stloc` and + `ldloc` of that slot do. -/ +theorem homed_argument_round_trip {entered memory : Memory} {frame : Frame} {boundary : Nat} + {home : Reference} + (homes : WritableHomes entered boundary [.word32] frame.locals) + (formed : memory.WellFormed) + (slot : frame.locals[0]? = some (.bytes .word32 home)) + (ready : access memory home 4 1 true = .ok ()) (passed stored : BitVec 32) : + ∃ after, run [homedArgument] 5 0 0 [.scalar (.i32 passed), .scalar (.i32 stored)] frame [] + memory = .ok (after, [.scalar (.i32 stored)]) := by + have snapshots := LocalSnapshots.empty memory frame.locals + cil_checked_steps (snapshots := snapshots) [slot, ready] + +#print axioms homed_argument_round_trip + /-- An isolated store has no later argument or load obligations. -/ @[cil_code] def storeOnly : CIL.Method := { - code := [.setLocal 0, .ret], locals := [.v128 0], returnsValue := false } + code := [.stloc 0, .ret], locals := [.v128 0], returnsValue := false } /-- A snapshot collection grants no authority; adding exactly that authority permits the run. -/ theorem missing_authority {entered memory : Memory} {frame : Frame} {boundary : Nat} @@ -147,7 +168,7 @@ theorem wrong_local_kind {entered memory : Memory} {frame : Frame} {boundary : N /-- An isolated load returns exactly the remembered value. -/ @[cil_code] def loadOnly : CIL.Method := { - code := [.local 0, .ret], locals := [.v128 0], returnsValue := true } + code := [.ldloc 0, .ret], locals := [.v128 0], returnsValue := true } /-- An empty collection cannot justify a load; a supplied snapshot permits the same run. -/ theorem unknown_load (memory : Memory) (frame : Frame) (vector : BitVec 128) : @@ -163,11 +184,11 @@ theorem unknown_load (memory : Memory) (frame : Frame) (vector : BitVec 128) : #print axioms unknown_load -/-- Mixed private homes include a narrowed byte and a vector addressed by vector opcodes. -/ +/-- Mixed private homes include a narrowed byte and a vector. -/ @[cil_code] def mixedHomes : CIL.Method := { - code := [.arg 0, .setLocal 0, .const32 0x1FF, .setLocal 1 (some .unsigned8), - .arg 1, .setVectorLocal 2, .vectorLocal 2, .pop, .local 1 (some .unsigned8), .pop, - .local 0, .ret] + code := [.ldarg 0, .stloc 0, .ldc_i4 0x1FF, .stloc 1, + .ldarg 1, .stloc 2, .ldloc 2, .pop, .ldloc 1, .pop, + .ldloc 0, .ret] locals := [.i64 0, .i32 0, .v128 0] localKinds := [.word64, .narrow .unsigned8, .vector128], returnsValue := true } @@ -177,7 +198,7 @@ theorem mixed_homes_wp (memory : Memory) (slots : List LocalSlot) (boundary : Na (inputs outputs : List ArgumentView) (valid : ValidCall memory inputs outputs) (word : BitVec 64) (vector : BitVec 128) : WP (Execution (fun fuel => run [mixedHomes] fuel 0 0 - [.scalar (.i64 word), .scalar (.v128 vector)] ⟨0, slots, [], [], 0⟩ [] memory)) + [.scalar (.i64 word), .scalar (.v128 vector)] ⟨0, slots, [], 0⟩ [] memory)) (fun result => result.2 = [.scalar (.i64 word)] ∧ ValidCall result.1 inputs outputs ∧ MemoryBelow boundary memory result.1 ∧ AccessBelow memory.nextIdentity memory result.1) := by @@ -192,7 +213,7 @@ theorem mixed_homes_wp (memory : Memory) (slots : List LocalSlot) (boundary : Na /-- Supplied stores retain their chosen memory and can stop before the next instruction. -/ theorem supplied_store_until (before after : Memory) (frame : Frame) (vector : BitVec 128) - (stored : step storeOnly (.setLocal 0) 0 [] frame [.scalar (.v128 vector)] before = + (stored : step storeOnly (.stloc 0) 0 [] frame [.scalar (.v128 vector)] before = .ok (.next 1 [] frame after)) (_snapshots : LocalSnapshots before frame.locals (fun _ => none)) (continuation : WP (Execution (fun fuel => run [storeOnly] fuel 0 1 [] frame [] after)) @@ -204,22 +225,10 @@ theorem supplied_store_until (before after : Memory) (frame : Frame) (vector : B #print axioms supplied_store_until -/-- The checked slot kind remains authoritative when the extracted store has an annotation. -/ -theorem annotated_supplied_store (before after : Memory) (frame : Frame) (bits : BitVec 32) - (stored : step narrowLocal (.setLocal 0) 1 [.scalar (.i32 bits)] frame - [.scalar (.i32 bits)] before = .ok (.next 2 [] frame after)) - (continuation : WP (Execution (fun fuel => run [narrowLocal] fuel 0 2 - [.scalar (.i32 bits)] frame [] after)) (fun result => result.1 = leaveFrame frame after)) : - WP (Execution (fun fuel => run [narrowLocal] fuel 0 1 [.scalar (.i32 bits)] frame - [.scalar (.i32 bits)] before)) (fun result => result.1 = leaveFrame frame after) := by - cil_contract_steps [stored] until continuation - -#print axioms annotated_supplied_store - /-- Reduced literal operands match quantified checked stores without changing their memory. -/ theorem constructor_supplied_store (before after : Memory) (frame : Frame) (stored : ∀ (pc : Nat) (args rest : List Value), - step narrowLocal (.setLocal 0) pc args frame (.scalar (.i32 0) :: rest) before = + step narrowLocal (.stloc 0) pc args frame (.scalar (.i32 0) :: rest) before = .ok (.next (pc + 1) rest frame after)) (continuation : WP (Execution (fun fuel => run [narrowLocal] fuel 0 2 [] frame [] after)) (fun result => result.1 = leaveFrame frame after)) : @@ -231,7 +240,7 @@ theorem constructor_supplied_store (before after : Memory) (frame : Frame) #print axioms constructor_supplied_store /-- A named instruction exposes the same local lookup as a literal opcode. -/ -def rememberedLoad : CIL.Op := .local 0 +def rememberedLoad : CIL.Instruction := .ldloc 0 /-- Named generated instructions still consume the exact current snapshot. -/ theorem named_snapshot_load (memory : Memory) (frame : Frame) (bits : BitVec 32) @@ -273,7 +282,7 @@ theorem symbolic_snapshot_index (memory : Memory) (frame : Frame) (base : Nat) (known : LocalSnapshots memory frame.locals (rangedValues base words)) (body : CIL.Method) (program : CIL.Program) (method : Nat) (found : program[method]? = some body) - (instruction : body.code[0]? = some (.local (base + 1))) + (instruction : body.code[0]? = some (.ldloc (base + 1))) (continuation : WP (Execution (fun fuel => run program fuel method 1 [] frame [.scalar (.i64 (words 1))] memory)) (fun _ => True)) : WP (Execution (fun fuel => run program fuel method 0 [] frame [] memory)) @@ -282,18 +291,6 @@ theorem symbolic_snapshot_index (memory : Memory) (frame : Frame) (base : Nat) #print axioms symbolic_snapshot_index -/-- An annotated supplied fact also applies to the same checked store without an annotation. -/ -theorem annotated_fact_to_plain (before after : Memory) (frame : Frame) (vector : BitVec 128) - (stored : step storeOnly (.setLocal 0 (some .unsigned8)) 0 [] frame - [.scalar (.v128 vector)] before = .ok (.next 1 [] frame after)) - (continuation : WP (Execution (fun fuel => run [storeOnly] fuel 0 1 [] frame [] after)) - (fun result => result.1 = leaveFrame frame after)) : - WP (Execution (fun fuel => run [storeOnly] fuel 0 0 [] frame - [.scalar (.v128 vector)] before)) (fun result => result.1 = leaveFrame frame after) := by - cil_contract_steps [stored] until continuation - -#print axioms annotated_fact_to_plain - /-- An empty snapshot collection grants neither a readable value nor permission to write. -/ theorem wp_unknown_load (memory : Memory) (frame : Frame) (vector : BitVec 128) (known : LocalSnapshots memory frame.locals @@ -341,7 +338,7 @@ theorem entry_homes_wp (entered memory : Memory) (slots : List LocalSlot) (bound (retained : AccessBelow entered.nextIdentity entered memory) (word : BitVec 64) (vector : BitVec 128) : WP (Execution (fun fuel => run [mixedHomes] fuel 0 0 - [.scalar (.i64 word), .scalar (.v128 vector)] ⟨0, slots, [], [], 0⟩ [] memory)) + [.scalar (.i64 word), .scalar (.v128 vector)] ⟨0, slots, [], 0⟩ [] memory)) (fun result => result.2 = [.scalar (.i64 word)] ∧ AccessBelow entered.nextIdentity entered result.1 ∧ MemoryBelow boundary memory result.1) := by @@ -370,7 +367,7 @@ theorem entry_homes_need_retained_access (entered memory : Memory) (frame : Fram /-- A reference local precedes the numeric local the method stores and reloads. -/ @[cil_code] def rootedHomes : CIL.Method := { - code := [.arg 0, .setLocal 1, .local 1, .ret] + code := [.ldarg 0, .stloc 1, .ldloc 1, .ret] locals := [.i64 0, .i64 0], localKinds := [.reference, .word64], returnsValue := true } /-- Numeric homes that cover the locals after a leading root slot justify a store past it: @@ -442,7 +439,7 @@ theorem opaque_kinds_wp (memory : Memory) (frame : Frame) (boundary : Nat) #print axioms opaque_kinds_wp /-- Store an argument in the local three past a base and reload it. -/ @[cil_code] def basedLocal (base : Nat) : CIL.Method := { - code := [.arg 0, .setLocal (base + 3), .local (base + 3), .ret] + code := [.ldarg 0, .stloc (base + 3), .ldloc (base + 3), .ret] locals := [], returnsValue := true } /-- A block stated at a symbolic local base stores and reloads through entry homes, given the @@ -477,7 +474,7 @@ theorem symbolic_current_home_wp (memory : Memory) (frame : Frame) (boundary bas #print axioms symbolic_current_home_wp /-- Store a vector and reload it. -/ @[cil_code] def storeReload : CIL.Method := { - code := [.setLocal 0, .local 0, .ret], locals := [.v128 0], returnsValue := true } + code := [.stloc 0, .ldloc 0, .ret], locals := [.v128 0], returnsValue := true } /-- A supplied store keeps its chosen memory and the snapshots follow it there, so the reload after it reads the stored value in the same walk. -/ @@ -487,7 +484,7 @@ theorem supplied_store_reload (before after : Memory) (frame : Frame) (boundary (formed : before.WellFormed) (slot : frame.locals[0]? = some (.bytes .vector128 home)) (ready : access before home 16 1 true = .ok ()) - (supplied : step storeReload (.setLocal 0) 0 [] frame [.scalar (.v128 vector)] before = + (supplied : step storeReload (.stloc 0) 0 [] frame [.scalar (.v128 vector)] before = .ok (.next 1 [] frame after)) : WP (Execution (fun fuel => run [storeReload] fuel 0 0 [] frame [.scalar (.v128 vector)] before)) @@ -505,7 +502,7 @@ theorem supplied_store_reload (before after : Memory) (frame : Frame) (boundary /-- Store a word and return. -/ @[cil_code] def wordStore : CIL.Method := { - code := [.arg 0, .setLocal 0, .ret], locals := [.i64 0], localKinds := [.word64] + code := [.ldarg 0, .stloc 0, .ret], locals := [.i64 0], localKinds := [.word64] returnsValue := false } /-- A store names its memory and evidence, and restates the caller's reads and buffers below @@ -518,7 +515,7 @@ theorem store_evidence (memory : Memory) (slots : List LocalSlot) (boundary : Na (unrelated : read memory other 8 1 = .ok unknown) (room : Writable memory caller 8 0 2) (word : BitVec 64) : WP (Execution (fun fuel => run [wordStore] fuel 0 0 [.scalar (.i64 word)] - ⟨0, slots, [], [], 0⟩ [] memory)) + ⟨0, slots, [], 0⟩ [] memory)) (fun result => result.2 = [] ∧ read result.1 caller 8 1 = .ok bytes ∧ Writable result.1 caller 8 0 2 ∧ result.1.WellFormed ∧ MemoryBelow boundary memory result.1 ∧ @@ -538,12 +535,13 @@ def pair (reference : Reference) : ArgumentView := ⟨reference, 16⟩ /-- Store an argument, then read caller data after each store: an input word, the output reference, and two caller limbs. -/ @[cil_code] def callerReads : CIL.Method := { - code := [.arg 0, .setLocal 0, - .arg 1, .fieldAt 8 8, .setLocal 1, - .arg 2, .pop, - .arg 3, .load64, .setLocal 0, - .arg 4, .load64, .pop, - .local 1, .local 0, .add, .ret] + environment := Tests.WordFields.pairEnvironment + code := [.ldarg 0, .stloc 0, + .ldarg 1, .ldfld Tests.WordFields.high, .stloc 1, + .ldarg 2, .pop, + .ldarg 3, .ldind .i8, .stloc 0, + .ldarg 4, .ldind .i8, .pop, + .ldloc 1, .ldloc 0, .add, .ret] locals := [.i64 0, .i64 0], localKinds := [.word64, .word64], returnsValue := true } /-- A caller limb whose facts are projections, which no hypothesis name carries. -/ @@ -568,7 +566,7 @@ theorem caller_reads_after_stores (memory : Memory) (slots : List LocalSlot) (bo WP (Execution (fun fuel => run [callerReads] fuel 0 0 [.scalar (.i64 word), .reference (.address input), .reference (.address output), .reference (.address limb), .reference (.address other)] - ⟨0, slots, [], [], 0⟩ [] memory)) + ⟨0, slots, [], 0⟩ [] memory)) (fun result => result.2 = [.scalar (.i64 ((pair input).word memory 8 + first))]) := by have formed := valid.wellFormed have snapshots := LocalSnapshots.empty memory slots @@ -592,7 +590,7 @@ theorem counted_walk (memory : Memory) (slots : List LocalSlot) (boundary : Nat) (homes : CompleteWritableHomes memory boundary mixedHomes.localKinds slots) (formed : memory.WellFormed) (word : BitVec 64) (vector : BitVec 128) : WP (Execution (fun fuel => run [mixedHomes] fuel 0 0 - [.scalar (.i64 word), .scalar (.v128 vector)] ⟨0, slots, [], [], 0⟩ [] memory)) + [.scalar (.i64 word), .scalar (.v128 vector)] ⟨0, slots, [], 0⟩ [] memory)) (fun result => result.2 = [.scalar (.i64 word)]) := by have snapshots := LocalSnapshots.empty memory slots cil_contract_steps (snapshots := snapshots) 2 [] @@ -607,12 +605,12 @@ theorem counted_walk (memory : Memory) (slots : List LocalSlot) (boundary : Nat) /-- Store a word and reload it. -/ @[cil_code] def reloadWord : CIL.Method := { - code := [.arg 0, .setLocal 0, .local 0, .ret], locals := [.i64 0], localKinds := [.word64] + code := [.ldarg 0, .stloc 0, .ldloc 0, .ret], locals := [.i64 0], localKinds := [.word64] returnsValue := true } /-- The same store and reload after a leading `nop`. -/ @[cil_code] def paddedReloadWord : CIL.Method := { - code := [.nop, .arg 0, .setLocal 0, .local 0, .ret], locals := [.i64 0] + code := [.nop, .ldarg 0, .stloc 0, .ldloc 0, .ret], locals := [.i64 0] localKinds := [.word64], returnsValue := true } /-- One proof covers two bodies: the body is a variable until the case split, so the homes are @@ -651,4 +649,124 @@ theorem derived_homes_walk (program : CIL.Program) (method : Nat) rfl #print axioms derived_homes_walk -end Tests.LocalSnapshotSteps + +/-- Three stores, the second one after a step that changes memory without a store. -/ +@[cil_code] def storesAroundChange : CIL.Method := { + code := [.ldarg 0, .stloc 0, .ldarg 1, .stloc 0, .ldarg 0, .stloc 0, .ret], + locals := [.i64 0], localKinds := [.word64], returnsValue := false } + +/-- A store after a step that changes memory without a store, and one more after it, find the + homes' access through the link the user states between the memories: the walk restates a + chain that starts at the memory the homes were last stated at, though a store made it. -/ +theorem stores_across_a_memory_change (memory : Memory) (slots : List LocalSlot) + (boundary : Nat) + (homes : CompleteWritableHomes memory boundary storesAroundChange.localKinds slots) + (inputs outputs : List ArgumentView) (valid : ValidCall memory inputs outputs) + (first second : BitVec 64) (bump : Memory → Memory) + (change : ∀ current, step storesAroundChange (.ldarg 1) 2 + [.scalar (.i64 first), .scalar (.i64 second)] ⟨0, slots, [], 0⟩ [] current = + .ok (.next 3 [.scalar (.i64 second)] ⟨0, slots, [], 0⟩ (bump current))) + (bumpFormed : ∀ current, current.WellFormed → (bump current).WellFormed) + (bumpBelow : ∀ current, AccessBelow current.nextIdentity current (bump current)) + (bumpSnapshots : ∀ current known, LocalSnapshots current slots known → + LocalSnapshots (bump current) slots known) : + WP (Execution (fun fuel => run [storesAroundChange] fuel 0 0 + [.scalar (.i64 first), .scalar (.i64 second)] ⟨0, slots, [], 0⟩ [] memory)) + (fun _ => True) := by + have formed := valid.wellFormed + have preserved := MemoryBelow.refl boundary memory + have authority := (MemoryBelow.refl memory.nextIdentity memory).accessBelow + have snapshots := LocalSnapshots.empty memory slots + cil_contract_steps (snapshots := snapshots) 2 [] + have changedSnapshots := bumpSnapshots storedLocal _ localSnapshots + have link := bumpBelow storedLocal + have changedFormed := bumpFormed storedLocal localFormed + cil_contract_steps (snapshots := changedSnapshots) [change] + trivial + +#print axioms stores_across_a_memory_change + +open Lean Elab Tactic Meta in +/-- How many hypotheses of the context state a predicate with this head. -/ +def hypothesesWith (head : Name) : TacticM Nat := withMainContext do + return (← getLCtx).foldl (init := 0) fun count declaration => + if !declaration.isImplementationDetail && declaration.type.isAppOf head then count + 1 + else count + +/-- Two stores into one numeric local. -/ +@[cil_code] def twoStores : CIL.Method := { + code := [.ldarg 0, .stloc 0, .ldarg 1, .stloc 0, .ret], + locals := [.i64 0], localKinds := [.word64], returnsValue := false } + +open Lean Elab Tactic Meta in +/-- A walk of two stores leaves the caller's facts at the entry memory in scope next to their + restatement at the last memory, one restatement and no more, and each store's own evidence + under its names. -/ +theorem entry_facts_stay (memory : Memory) (slots : List LocalSlot) (boundary : Nat) + (homes : CompleteWritableHomes memory boundary twoStores.localKinds slots) + (inputs outputs : List ArgumentView) (valid : ValidCall memory inputs outputs) + (first second : BitVec 64) : + WP (Execution (fun fuel => run [twoStores] fuel 0 0 + [.scalar (.i64 first), .scalar (.i64 second)] ⟨0, slots, [], 0⟩ [] memory)) + (fun _ => True) := by + have formed := valid.wellFormed + have preserved := MemoryBelow.refl boundary memory + have authority := (MemoryBelow.refl memory.nextIdentity memory).accessBelow + have snapshots := LocalSnapshots.empty memory slots + cil_contract_steps (snapshots := snapshots) [] + run_tac do + for (head, expected) in [(``ValidCall, 2), (``CompleteWritableHomes, 2)] do + let found ← hypothesesWith head + unless found == expected do + throwError "expected {expected} hypotheses about {head}, found {found}" + withMainContext do + for name in [`storedLocal, `storingLocal, `localSnapshots, `localFormed, `localBelow, + `localAccess, `localWrite, `storedLocal_1, `storingLocal_1, `localSnapshots_1, + `localFormed_1, `localBelow_1, `localAccess_1, `localWrite_1] do + unless ((← getLCtx).findFromUserName? name).isSome do + throwError "a store's own hypothesis {name} is missing" + -- The evidence a store unpacks into its named hypotheses is cleared with it. + for declaration in ← getLCtx do + let base := declaration.userName.eraseMacroScopes + if [`snapshotLocated, `snapshotStored, `snapshotRest].contains base then + throwError "a store's unpacked evidence {declaration.userName} is still in scope" + -- The entry memory's facts are still there for a proof that states them. + have _ : ValidCall memory inputs outputs := by assumption + have _ : CompleteWritableHomes memory boundary twoStores.localKinds slots := by assumption + have _ : MemoryBelow boundary memory memory := by assumption + have _ : AccessBelow memory.nextIdentity memory memory := by assumption + trivial + +#print axioms entry_facts_stay + +/-- A theorem may name its own memory `stored`: the walk clears restated facts only at the + memories it introduced, so the caller's facts at that memory stay in scope after two stores, + whatever their memory is called. -/ +theorem caller_memory_named_stored (stored : Memory) (slots : List LocalSlot) (boundary : Nat) + (homes : CompleteWritableHomes stored boundary twoStores.localKinds slots) + (inputs outputs : List ArgumentView) (valid : ValidCall stored inputs outputs) + (first second : BitVec 64) : + WP (Execution (fun fuel => run [twoStores] fuel 0 0 + [.scalar (.i64 first), .scalar (.i64 second)] ⟨0, slots, [], 0⟩ [] stored)) + (fun _ => True) := by + have formed := valid.wellFormed + have preserved := MemoryBelow.refl boundary stored + have authority := (MemoryBelow.refl stored.nextIdentity stored).accessBelow + have snapshots := LocalSnapshots.empty stored slots + cil_contract_steps (snapshots := snapshots) [] + have _ : ValidCall stored inputs outputs := by assumption + have _ : CompleteWritableHomes stored boundary twoStores.localKinds slots := by assumption + have _ : MemoryBelow boundary stored stored := by assumption + trivial + +#print axioms caller_memory_named_stored + +-- `cil_cheap_assumption` closes what `assumption` closes when the hypothesis states the goal +-- through a head that unfolds at reducible transparency, in either direction. +example (left right : Nat) (greater : left > right) : right < left := by cil_cheap_assumption +example (left right : Nat) (less : right < left) : left > right := by cil_cheap_assumption +example (left right : Nat) (atLeast : left ≥ right) : right ≤ left := by cil_cheap_assumption +example (left right : Nat) (atMost : right ≤ left) : left ≥ right := by cil_cheap_assumption +example (left right : Nat) (apart : left ≠ right) : ¬ left = right := by cil_cheap_assumption +example (left right : Nat) (apart : ¬ left = right) : left ≠ right := by cil_cheap_assumption +end Tests.LocalSnapshotSteps \ No newline at end of file diff --git a/Tests/LocalSnapshots.lean b/Tests/LocalSnapshots.lean index bd340650e..6df69d622 100644 --- a/Tests/LocalSnapshots.lean +++ b/Tests/LocalSnapshots.lean @@ -36,7 +36,7 @@ theorem unknown_scalar_vector_homes (memory : Memory) (formed : memory.WellForme obtain ⟨frame, entered, setup, homes, _, _⟩ := unknown_frame_setup unknownMixedBody rfl (by simp [unknownMixedBody]) (by simp [unknownMixedBody]) (by simp [unknownMixedBody]) (by simp [unknownMixedBody]) (by simp [unknownMixedBody]) (by simp [unknownMixedBody]) - rfl rfl rfl memory [] formed + rfl rfl memory [] formed obtain ⟨scalar, scalarSlot, _, scalarAccess⟩ := homes.home_at 0 .word64 rfl obtain ⟨vector, vectorSlot, _, vectorAccess⟩ := homes.home_at 1 .vector256 rfl exact ⟨frame, entered, scalar, vector, setup, scalarSlot, vectorSlot, @@ -68,9 +68,9 @@ theorem vector_and_int_survive {body : CIL.Method} {pc : Nat} {args stack : List (callerWrite : write afterInt target bytes 1 = .ok afterOther) (belowFrame : target.allocation < boundary) (storeVector : write afterOther vectorHome (numberBytes vector.toNat 16) 1 = .ok final) : - step body (.local 0) pc args frame stack final = + step body (.ldloc 0) pc args frame stack final = .ok (.next (pc + 1) (.scalar (.v128 vector) :: stack) frame final) ∧ - step body (.local 1) pc args frame stack final = + step body (.ldloc 1) pc args frame stack final = .ok (.next (pc + 1) (.scalar (.i32 count) :: stack) frame final) := by have intKnown := (LocalSnapshots.empty start frame.locals).after_write homes 1 .word32 intHome (.i32 count) count.toNat rfl intSlot storeInt @@ -94,11 +94,11 @@ theorem numeric_frame_store {body : CIL.Method} {pc : Nat} {args stack : List Va (homes : NumericHomes entered lower zeroSpecs frame.locals) (formed : entered.WellFormed) (count : BitVec 32) : ∃ after, - step body (.setLocal 1) pc args frame (.scalar (.i32 count) :: stack) entered = + step body (.stloc 1) pc args frame (.scalar (.i32 count) :: stack) entered = .ok (.next (pc + 1) stack frame after) ∧ - step body (.local 0) pc args frame stack after = + step body (.ldloc 0) pc args frame stack after = .ok (.next (pc + 1) (.scalar (.v128 0) :: stack) frame after) ∧ - step body (.local 1) pc args frame stack after = + step body (.ldloc 1) pc args frame stack after = .ok (.next (pc + 1) (.scalar (.i32 count) :: stack) frame after) := by obtain ⟨intHome, intSlot, _, _, intReady⟩ := homes.home_at 1 ⟨.word32, .i32 0, 0, rfl⟩ rfl obtain ⟨after, stepped, known, _⟩ := homes.snapshots.store (body := body) (pc := pc) @@ -115,7 +115,7 @@ theorem narrow_store_loads_extended {body : CIL.Method} {pc : Nat} {args stack : (homes : WritableHomes entered boundary [.narrow .signed8] frame.locals) (slot : frame.locals[0]? = some (.bytes (.narrow .signed8) home)) (bits : BitVec 32) (stored : write start home (numberBytes bits.toNat 1) 1 = .ok final) : - step body (.local 0) pc args frame stack final = + step body (.ldloc 0) pc args frame stack final = .ok (.next (pc + 1) (.scalar (.i32 ((bits.setWidth 8).signExtend 32)) :: stack) frame final) := ((LocalSnapshots.empty start frame.locals).after_write homes 0 (.narrow .signed8) home @@ -132,7 +132,7 @@ theorem word_store_loads_as_local {body : CIL.Method} {pc index : Nat} (homes : WritableHomes entered boundary kinds frame.locals) (formed : before.WellFormed) (word : BitVec 64) (slot : frame.locals[index]? = some (.bytes .word64 home)) (written : write before home (numberBytes word.toNat 8) 1 = .ok after) : - step body (.local index) pc args frame stack after = + step body (.ldloc index) pc args frame stack after = .ok (.next (pc + 1) (.scalar (.i64 word) :: stack) frame after) := by have locals := (snapshots.after_write homes formed index home word slot written).toLocalSnapshots rw [wordLocals_rememberWord] at locals @@ -184,7 +184,7 @@ theorem byte_overwrite_loads_latest {body : CIL.Method} {pc : Nat} {args stack : (slot : frame.locals[0]? = some (.bytes .byte home)) (first : write start home (numberBytes 7 1) 1 = .ok middle) (second : write middle home (numberBytes 200 1) 1 = .ok final) : - step body (.local 0) pc args frame stack final = + step body (.ldloc 0) pc args frame stack final = .ok (.next (pc + 1) (.scalar (.i32 200) :: stack) frame final) := by have once := (LocalSnapshots.empty start frame.locals).after_write homes 0 .byte home (.i32 7) 7 rfl slot first @@ -219,7 +219,7 @@ theorem vector_survives_callee_frame {body : CIL.Method} {pc : Nat} {args stack {frame : Frame} {caller returned : Memory} {vector : BitVec 128} (snapshots : LocalSnapshots caller frame.locals (vectorKnown vector)) (roundTrip : MemoryBelow caller.nextIdentity caller returned) (formed : caller.WellFormed) : - step body (.local 0) pc args frame stack returned = + step body (.ldloc 0) pc args frame stack returned = .ok (.next (pc + 1) (.scalar (.v128 vector) :: stack) frame returned) := (snapshots.after_below roundTrip formed).load (.v128 vector) rfl @@ -251,11 +251,11 @@ theorem overlapping_write_not_apart (home : Reference) (byte : BitVec 8) : /-- Loads a vector local and returns it. -/ @[cil_code] def loadVectorBody : CIL.Method := { - code := [.local 0, .ret], locals := [.v128 0], localKinds := [.vector128], + code := [.ldloc 0, .ret], locals := [.v128 0], localKinds := [.vector128], returnsValue := true } /-- The stepping tactics take a snapshot load as a supplied step fact, with no change to the - tactics: the `.local` step pushes the remembered vector. -/ + tactics: the `.ldloc` step pushes the remembered vector. -/ theorem load_fact_steps (program : CIL.Program) (found : program[0]? = some loadVectorBody) (args : List Value) (frame : Frame) (memory : Memory) (vector : BitVec 128) (known : Nat → Option CIL.Value) (snapshots : LocalSnapshots memory frame.locals known) diff --git a/Tests/LookupScaling.cs b/Tests/LookupScaling.cs index f9e832f8b..1bdfe3a7e 100644 --- a/Tests/LookupScaling.cs +++ b/Tests/LookupScaling.cs @@ -1,86 +1,2555 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include GeneratedEvidence.cs +#:include ../Extractor/ProgramLookupFacts.cs +#:include ../Extractor/ObjectOperandProof.cs +#:include ../Extractor/PlatformDefinitions.cs +#:include ../Extractor/ByteLiterals.cs +#:package Mono.Cecil using System.Globalization; +using System.Text; using System.Text.RegularExpressions; +using Nethermind.LeanCIL; + +if (args.Contains("--object-admission-only", StringComparer.Ordinal)) +{ + ShapeResolutionCosts(admissionOnly: true); + return; +} +if (args.Contains("--layout-identities-only", StringComparer.Ordinal)) +{ + LayoutIdentityCosts(); + return; +} +if (args.Contains("--exception-types-only", StringComparer.Ordinal)) +{ + ExceptionDecisionCosts(); + return; +} +if (args.Contains("--closed-arguments-only", StringComparer.Ordinal)) +{ + ClosedArgumentsCosts(); + return; +} +if (args.Contains("--argument-resolution-only", StringComparer.Ordinal)) +{ + ArgumentResolutionCosts(); + return; +} +if (args.Contains("--element-shape-only", StringComparer.Ordinal)) +{ + ElementShapeCosts(); + return; +} +if (args.Contains("--shape-resolution-only", StringComparer.Ordinal)) +{ + ShapeResolutionCosts(); + return; +} +if (args.Contains("--indirect-access-only", StringComparer.Ordinal)) +{ + IndirectAccessCosts(); + return; +} +if (args.Contains("--call-check-only", StringComparer.Ordinal)) +{ + CallCheckCosts(); + return; +} +if (args.Contains("--decode-only", StringComparer.Ordinal)) +{ + DecodeCosts(); + return; +} +if (args.Contains("--method-decode-only", StringComparer.Ordinal)) +{ + MethodDecodeCosts(); + return; +} +if (args.Contains("--static-facts-only", StringComparer.Ordinal)) +{ + StaticFactCosts(); + StaticIdentityCosts(); + return; +} +if (args.Contains("--decoded-definition-only", StringComparer.Ordinal)) +{ + DecodedDefinitionCosts(); + return; +} +if (args.Contains("--decoded-program-only", StringComparer.Ordinal)) +{ + DecodedProgramCosts(); + return; +} +if (args.Contains("--closure-only", StringComparer.Ordinal)) +{ + ClosureCosts(); + return; +} +if (args.Contains("--run-only", StringComparer.Ordinal)) +{ + RunCosts(); + return; +} +if (args.Contains("--return-step-only", StringComparer.Ordinal)) +{ + ReturnResolutionCosts(stepOnly: true); + return; +} +if (args.Contains("--metadata-name-only", StringComparer.Ordinal)) +{ + MetadataNameCosts(); + return; +} +if (args.Contains("--model-table-only", StringComparer.Ordinal)) +{ + ModelTableCosts(); + return; +} +if (args.Contains("--import-links-only", StringComparer.Ordinal)) +{ + ImportLinkCosts(); + return; +} +if (args.Contains("--platform-table-only", StringComparer.Ordinal)) +{ + PlatformTableCosts(); + return; +} +if (args.Contains("--indirect-target-only", StringComparer.Ordinal)) +{ + IndirectTargetCosts(); + return; +} +if (args.Contains("--lineage-only", StringComparer.Ordinal)) +{ + LineageCosts(); + return; +} +if (args.Contains("--prefix-entry-only", StringComparer.Ordinal)) +{ + PrefixEntryCosts(); + return; +} +var instructionEquationsOnly = args.Contains("--instruction-equations-only", StringComparer.Ordinal); +if (!instructionEquationsOnly) +{ + NativeTableCosts(); + if (args.Contains("--native-table-only", StringComparer.Ordinal)) return; + ExceptionDecisionCosts(); + CheckNarrowedResolution(); + if (args.Contains("--return-evidence-only", StringComparer.Ordinal)) return; + ReturnResolutionCosts(); + ArgumentResolutionCosts(); + ElementShapeCosts(); + ShapeResolutionCosts(); + LineageCosts(); + if (args.Contains("--return-resolution-only", StringComparer.Ordinal)) return; + CallCheckCosts(); + DecodeCosts(); + MethodDecodeCosts(); + StaticFactCosts(); + StaticIdentityCosts(); + DecodedDefinitionCosts(); + DecodedProgramCosts(); + PrefixEntryCosts(); + ClosureCosts(); + RunCosts(); + ClosedArgumentsCosts(); + + LayoutIdentityCosts(); + Run(["dotnet", "run", "--file", At("Tests/MetadataFieldPrefixes.cs"), "-c", "Release"]); + BuildLeanTestLibraries("Certificate index, feature getter, layout and home index libraries", + "Tests.FlowIndex", "Tests.CertificateFlow", "Tests.FeatureGetter", "Tests.ExtractionHomeIndexes", + "Tests.LayoutAccumulation", "Tests.LayoutProjectionIndexes"); +} // `cil_instruction_equations` proves each equation from the code suffix that starts its block. Proved // against the whole code list instead, it walks one cell per preceding instruction and a method's equations // cost quadratic time, which this fixture reports as a per-instruction cost that grows with the method. const int ShortStatements = 125; +const int MiddleStatements = 250; const int LongStatements = 500; // Compiling four times the instructions may cost at most this much more per instruction. -const double AllowedGrowth = 1.25; +const double AllowedGrowth = 1.15; +const int FewCalls = 100; +const int ManyCalls = 400; +// Link reads whose cost grows with the square of the calls make the recheck of an embedding cost 1.29 times as +// much per call at 2,048 calls as at 512; linear reads cost the same. +const int EmbedFewCalls = 512; +const int EmbedManyCalls = 2048; +// Segments in each of the two blocks of an embedding whose pc map puts the second block first. +const int ReorderFewSegments = 128; +const int ReorderManySegments = 512; +// Four times the calls of an embedding may cost at most this much more per call. +const double AllowedEmbedGrowth = 1.15; +// Two calls in bodies of these many statements: the link output must not grow with the body. +const int SparseShortStatements = 250; +const int SparseLongStatements = 1000; +// Four times the call sites may cost at most this much more per call site. +const double AllowedCallGrowth = 1.35; +// Type identities compare one code per name, so a name four times as long may cost at most this much more. +const double AllowedNameGrowth = 1.05; +// A model table sixteen times as large may cost a lookup at most this much more. +const double AllowedModelLookupGrowth = 1.05; +// The most heartbeats deciding equality or inequality of two type identities may cost at either name length. +const long TypeDecisionLimit = 9_500; +// `decide +kernel` costs about 2,000 heartbeats for any statement; less means the decision was not measured. +const long KernelDecisionFloor = 2_000; +// Bodies of these many array element addresses. A readonly rule that scans the body at every address costs +// about 1.5 times as much per address at each doubling. +const int FewAddresses = 128; +const int MoreAddresses = 256; +const int ManyAddresses = 512; +// Bodies of these many local loads, stores, addresses and pops, which cycle over four int32 locals. +const int FewLocalAccesses = 256; +const int MoreLocalAccesses = 1024; +const int ManyLocalAccesses = 4096; +// Bodies of these many locals, each stored once and then loaded or addressed. A check that walks the locals list, +// or the list of locals a `calli` consumes, at each such site costs between 1.3 and 2.3 times as much per +// instruction at 512 as at 128; reading indexes built once per body costs at most about 1.1 times as much. +const int FewLocals = 128; +const int MoreLocals = 256; +const int ManyLocals = 512; +// Four times the locals may cost at most this much more per instruction; the closed argument storage reads a set +// of pointer locals whose lookups cost the logarithm of the locals. +const double AllowedLocalGrowth = 1.25; +// Bodies of a fixed length over these many int32 locals, cycling through every local. Feature states list known +// facts alone, so a body that stores no constant costs the same per instruction at every local count. +const int FewFeatureLocals = 16; +const int MoreFeatureLocals = 64; +const int ManyFeatureLocals = 256; +const int FeatureLocalSegments = 256; +const int FeatureLocalInstructions = 8 * FeatureLocalSegments + 1; +// Bodies over `ManyKnownFacts` int32 locals that store zero into the first few, more or many of them, then store a +// new word into each in turn and load it back for `KnownFactRounds` rounds; every loop state holds every fact. +const int FewKnownFacts = 4; +const int MoreKnownFacts = 16; +const int ManyKnownFacts = 64; +const int KnownFactRounds = 128; +// Linear growth makes the increment from 16 to 64 facts four times the one from 4 to 16; at about 400 heartbeats per +// fact, a quadratic term of one heartbeat per fact pair in each state exceeds this ratio. +const double AllowedFactIncrementRatio = 4.5; var directory = At("artifacts/LookupScaling"); +Directory.CreateDirectory(directory); + +// Signature facts inspect a return head; code and unrelated layout entries stay outside that inspection. +void ReturnResolutionCosts(bool stepOnly = false) +{ + var resolutionDirectory = At("artifacts/LookupScaling/ReturnResolution"); + Directory.CreateDirectory(resolutionDirectory); + const double GrowthLimit = AllowedLocalGrowth; + static bool WithinGrowth((int Size, double Cost)[] samples, bool perItem) + { + double Normalized((int Size, double Cost) sample) => sample.Cost / (perItem ? sample.Size : 1); + return samples.Skip(1).All(sample => Normalized(sample) <= Normalized(samples[0]) * GrowthLimit); + } + int[] controlSizes = [32, 128, 512]; + if (!WithinGrowth(controlSizes.Select(size => (size, (double)size)).ToArray(), perItem: true) || + WithinGrowth(controlSizes.Select(size => (size, (double)size * size)).ToArray(), perItem: true) || + !WithinGrowth(controlSizes.Select(size => (size, 32.0)).ToArray(), perItem: false) || + WithinGrowth(controlSizes.Select(size => (size, (double)size)).ToArray(), perItem: false)) + throw new InvalidOperationException("Return resolution growth check failed its normalization controls."); + var dimensions = stepOnly ? new[] { "step-methods", "step-body" } : + ["methods", "depth", "body", "layouts", "enum-depth", "enum-layouts", "step-methods", "step-body"]; + foreach (var dimension in dimensions) + { + var samples = new List<(int Size, long Facts, long Simplifier)>(); + foreach (var size in new[] { 32, 128, 512 }) + { + var source = new StringBuilder(""" + import CIL.Safety.ValueSteps + import CIL.Safety.StepComposition + open CIL CIL.Extraction Lean Elab Command + set_option maxRecDepth 8192 + /-- Elaboration and kernel checking of one command, excluding fixture construction. -/ + elab "#resolution_cost " label:str " in " command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + logInfo m!"resolution {label.getString} {after - before} heartbeats" + + """); + var type = ".atom runtimeScope \"System.Byte\""; + var expected = "some .unsigned8"; + if (dimension == "layouts") + { + type = ".aggregate \"Point\" 0"; + expected = "none"; + } + if (dimension is "depth" or "enum-depth" or "enum-layouts") + { + var depth = dimension == "enum-layouts" ? 1 : size; + var underlying = dimension == "depth" ? type : ".array (.aggregate \"Point\" 0)"; + var heads = string.Concat(Enumerable.Repeat(".enum \"Nested\" (", depth)); + type = $"""{heads}{underlying}{new string(')', depth)}"""; + expected = "none"; + } + var instructions = dimension is "body" or "step-body" + ? string.Join(", ", Enumerable.Repeat(".nop", size)) : ""; + var layouts = dimension is "layouts" or "enum-layouts" ? string.Join(", ", Enumerable.Repeat( + "{ identity := \"Point\", explicit := false, packing := 0, declaredSize := 0, fields := [] }", + size)) : ""; + source.AppendLine($"def rawLayouts : List RawLayout := [{layouts}]"); + var methods = dimension is "methods" or "step-methods" ? size : 1; + for (var index = 0; index < methods; index++) + { + source.AppendLine($$""" + def body{{index}} : Method := { + code := [{{instructions}}], locals := [], returnsValue := true, + declared := { returnType := {{type}} }, environment := { rawLayouts := rawLayouts } } + theorem body{{index}}_returnsValue : body{{index}}.returnsValue = true := rfl + theorem body{{index}}_declared : + body{{index}}.declared = { returnType := {{type}} } := rfl + #resolution_cost "fact" in cil_resolution_facts body{{index}} + """); + if (dimension.StartsWith("step-", StringComparison.Ordinal)) + source.AppendLine($$""" + #resolution_cost "simplifier" in + theorem body{{index}}_simplified (frame : Safety.Frame) (memory : Safety.Memory) : + Safety.step body{{index}} .ret 0 [] frame [.scalar (.i32 300)] memory = + .ok (.returned [.scalar (.i32 44)] memory) := by + simp only [Safety.step_ret_eq, body{{index}}_returnsValue, + body{{index}}_returnNarrow] + rfl + """); + else source.AppendLine($$""" + #resolution_cost "simplifier" in + theorem body{{index}}_simplified : body{{index}}.returnNarrow = {{expected}} := by + simp only [cil_step] + """); + foreach (var suffix in new[] { "returnsValue", "declared", "returnNarrow", "simplified" }) + source.AppendLine($"#print axioms body{index}_{suffix}"); + } + var path = Path.Combine(resolutionDirectory, $"{dimension}{size}.lean"); + File.WriteAllText(path, source.ToString()); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("Return resolution introduced an unproved theorem."); + var costs = LookupPatterns.ReturnResolution.Matches(output).Cast().ToArray(); + if (costs.Length != methods * 2) + throw new InvalidOperationException("Missing return resolution costs: " + output); + long Sum(string label) => costs.Where(match => match.Groups[1].Value == label) + .Sum(match => long.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)); + samples.Add((size, Sum("fact"), Sum("simplifier"))); + } + foreach (var simplifier in new[] { false, true }) + { + var measured = samples.Select(sample => + (sample.Size, (double)(simplifier ? sample.Simplifier : sample.Facts))).ToArray(); + if (!WithinGrowth(measured, + perItem: dimension is "methods" or "depth" or "enum-depth" or "step-methods")) + throw new InvalidOperationException($"Superlinear return resolution {dimension}: " + + string.Join("; ", measured)); + Console.WriteLine($"PASS return resolution {(simplifier ? "simplifier" : "fact")} {dimension}: " + + string.Join("; ", samples.Select(sample => + $"{sample.Size} = {(simplifier ? sample.Simplifier : sample.Facts)} heartbeats"))); + } + } +} + +// Fails when argument facts or reads at 8, 32 and 128 parameters cost over 1.35 times more per argument than +// at the size before; evaluating parameters[index]? or searching homes gives 2.40x, 3.40x (2.48x, 3.46x homed). +void ArgumentResolutionCosts() +{ + const double AllowedArgumentGrowth = 1.35; + var resolutionDirectory = At("artifacts/LookupScaling/ArgumentResolution"); + Directory.CreateDirectory(resolutionDirectory); + const string Int32Type = ".atom runtimeScope \"System.Int32\""; + foreach (var homed in new[] { false, true }) + { + var samples = new List<(int Size, long Facts, long Reads)>(); + foreach (var size in new[] { 8, 32, 128 }) + { + // Each homed argument's slot follows the one declared local, in argument order. + var slots = Enumerable.Range(0, size).Where(index => homed && index % 2 == 1) + .Select((index, position) => (index, position)) + .ToDictionary(home => home.index, home => 1 + home.position); + var homes = string.Join(", ", slots.Keys.Order().Select(index => $"({index}, .word32)")); + var signature = $"{{ parameters := [{string.Join(", ", Enumerable.Repeat(Int32Type, size))}] }}"; + var code = string.Join(", ", Enumerable.Range(0, size).Select(index => + slots.TryGetValue(index, out var slot) ? $".ldloc {slot}, .pop" : $".ldarg {index}, .pop")) + ", .ret"; + var accesses = string.Join(", ", Enumerable.Range(0, size).Select(index => + slots.TryGetValue(index, out var slot) ? $".home {slot}" : ".value none")); + var reads = string.Join(", ", Enumerable.Range(0, size).Select(index => $"body.argumentAccess {index}")); + var source = $$""" + import CIL.Safety.ValueSteps + open CIL CIL.Extraction Lean Elab Command + set_option maxRecDepth 8192 + /-- Elaboration and kernel checking of one command, excluding fixture construction. -/ + elab "#resolution_cost " label:str " in " command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + logInfo m!"resolution {label.getString} {after - before} heartbeats" + def body : Method := { + code := [{{code}}], locals := [.i32 0], localKinds := [.word32], + parameterHomes := [{{homes}}], returnsValue := false, declared := {{signature}} } + theorem body_returnsValue : body.returnsValue = false := rfl + theorem body_declared : body.declared = {{signature}} := rfl + #resolution_cost "fact" in cil_resolution_facts body + #resolution_cost "simplifier" in + theorem body_reads : [{{reads}}] = [{{accesses}}] := by simp only [cil_code] + #print axioms body_argumentAccess{{size - 1}} + #print axioms body_reads + """ + "\n"; + var path = Path.Combine(resolutionDirectory, $"{(homed ? "Homed" : "Unhomed")}{size}.lean"); + File.WriteAllText(path, source); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("Argument resolution introduced an unproved theorem."); + var costs = LookupPatterns.ReturnResolution.Matches(output).Cast().ToArray(); + if (costs.Length != 2) + throw new InvalidOperationException("Missing argument resolution costs: " + output); + samples.Add((size, long.Parse(costs[0].Groups[2].Value, CultureInfo.InvariantCulture), + long.Parse(costs[1].Groups[2].Value, CultureInfo.InvariantCulture))); + } + var label = homed ? "with every other argument homed" : "without homes"; + foreach (var (name, cost) in new (string, Func<(int Size, long Facts, long Reads), long>)[] + { ("facts", sample => sample.Facts), ("reads", sample => sample.Reads) }) + { + var perArgument = samples.Select(sample => (double)cost(sample) / sample.Size).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{sample.Size} = {(double)cost(sample) / sample.Size:F0}"))); + if (perArgument.Skip(1).Zip(perArgument).Any(pair => pair.First > pair.Second * AllowedArgumentGrowth)) + throw new InvalidOperationException( + $"Argument {name} {label} cost {measured} heartbeats per argument; the limit is " + + $"{AllowedArgumentGrowth} times the cost per argument at the size before."); + Console.WriteLine($"PASS argument {name} {label}: {measured} heartbeats per argument"); + } + } +} + +// `cil_resolution_facts` states each static site's descriptor from one ascending check and the named site suffix +// of its block, so facts and the simplifier's reads of them at 8, 32 and 128 sites cost at most 1.35 times more +// per site than at the size before; a fact that searched the sites before it would grow with the size. +void StaticFactCosts() +{ + const double AllowedSiteGrowth = 1.35; + var factDirectory = At("artifacts/LookupScaling/StaticFacts"); + Directory.CreateDirectory(factDirectory); + var samples = new List<(int Size, long Facts, long Reads)>(); + foreach (var size in new[] { 8, 32, 128 }) + { + string Descriptor(int index) => string.Create(CultureInfo.InvariantCulture, + $"{{ identity := {index}, field := field, bytes := [{index % 256}] }}"); + var code = string.Join(", ", Enumerable.Repeat(".ldsfld field, .pop", size)) + ", .ret"; + var sites = string.Join(", ", + Enumerable.Range(0, size).Select(index => $"({2 * index}, {Descriptor(index)})")); + var reads = string.Join(", ", Enumerable.Range(0, size).Select(index => $"body.staticAt {2 * index}")); + var resolved = string.Join(", ", Enumerable.Range(0, size).Select(index => $"some {Descriptor(index)}")); + var source = $$""" + import CIL.Safety.ResolutionFacts + open CIL CIL.Extraction Lean Elab Command + set_option maxRecDepth 8192 + /-- Elaboration and kernel checking of one command, excluding fixture construction. -/ + elab "#resolution_cost " label:str " in " command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + logInfo m!"resolution {label.getString} {after - before} heartbeats" + def field : FieldRef := + { owner := .atom "Fixture" "Holder", index := 0, type := .atom "Fixture" "Pair", name := name!"Field" } + def body : Method := { + code := [{{code}}], locals := [], returnsValue := false, declared := {}, + staticSites := [{{sites}}] } + theorem body_returnsValue : body.returnsValue = false := rfl + theorem body_declared : body.declared = {} := rfl + #resolution_cost "fact" in cil_resolution_facts body + #resolution_cost "simplifier" in + theorem body_reads : [{{reads}}] = [{{resolved}}] := by simp only [cil_code] + #print axioms body_staticAt{{2 * (size - 1)}} + #print axioms body_reads + """ + "\n"; + var path = Path.Combine(factDirectory, $"Sites{size}.lean"); + File.WriteAllText(path, source); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("Static site facts introduced an unproved theorem."); + var costs = LookupPatterns.ReturnResolution.Matches(output).Cast().ToArray(); + if (costs.Length != 2) + throw new InvalidOperationException("Missing static site fact costs: " + output); + samples.Add((size, long.Parse(costs[0].Groups[2].Value, CultureInfo.InvariantCulture), + long.Parse(costs[1].Groups[2].Value, CultureInfo.InvariantCulture))); + } + foreach (var (name, cost) in new (string, Func<(int Size, long Facts, long Reads), long>)[] + { ("facts", sample => sample.Facts), ("reads", sample => sample.Reads) }) + { + var perSite = samples.Select(sample => (double)cost(sample) / sample.Size).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{sample.Size} = {(double)cost(sample) / sample.Size:F0}"))); + if (perSite.Skip(1).Zip(perSite).Any(pair => pair.First > pair.Second * AllowedSiteGrowth)) + throw new InvalidOperationException( + $"Static site {name} cost {measured} heartbeats per site; the limit is " + + $"{AllowedSiteGrowth} times the cost per site at the size before."); + Console.WriteLine($"PASS static site {name}: {measured} heartbeats per site"); + } +} + +// `StaticIdentitiesValid` reads each site's recorded token from one index of the body and inserts the site into two +// ordered maps, so deciding it at 8, 32 and 128 sites of distinct fields and identities costs at most +// AllowedIdentityGrowth times more per site than at the size before; comparing each site with every other would not. +void StaticIdentityCosts() +{ + const double AllowedIdentityGrowth = 1.35; + var identityDirectory = At("artifacts/LookupScaling/StaticIdentities"); + Directory.CreateDirectory(identityDirectory); + var samples = new List<(int Size, long Cost)>(); + foreach (var size in new[] { 8, 32, 128 }) + { + var code = string.Join(", ", Enumerable.Repeat(".ldsfld field, .pop", size)) + ", .ret"; + var sites = string.Join(", ", Enumerable.Range(0, size).Select(index => string.Create( + CultureInfo.InvariantCulture, $"({2 * index}, {{ identity := {index}, field := field, bytes := [0] }})"))); + var statics = string.Join(", ", Enumerable.Range(0, size).Select(index => string.Create( + CultureInfo.InvariantCulture, $"({2 * index}, {0x04000001 + index})"))); + var source = $$""" + import CIL.Extraction.StaticIdentities + open CIL CIL.Extraction Lean Elab Command + set_option maxRecDepth 8192 + /-- Elaboration and kernel checking of one command, excluding fixture construction. -/ + elab "#identity_cost " command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + logInfo m!"static identities {after - before} heartbeats" + def field : FieldRef := + { owner := .atom "Fixture" "Holder", index := 0, type := .atom "Fixture" "Pair", name := name!"Field" } + def body : Method := { + code := [{{code}}], locals := [], returnsValue := false, declared := {}, + staticSites := [{{sites}}] } + def evidence : BodyEvidence := + { bytes := [], tokens := { operands := [] }, metadata := {}, identities := [bytesOf "Fixture"], + tables := [], names := {}, pinned := PinnedIndex.ofIdentities [], statics := [{{statics}}] } + #identity_cost + theorem identities_valid : StaticIdentitiesValid [body] [evidence] := by decide +kernel + #print axioms identities_valid + """ + "\n"; + var path = Path.Combine(identityDirectory, $"Sites{size}.lean"); + File.WriteAllText(path, source); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("The static identity check introduced an unproved theorem."); + var cost = LookupPatterns.StaticIdentities.Match(output); + if (!cost.Success) throw new InvalidOperationException("Missing static identity cost: " + output); + samples.Add((size, long.Parse(cost.Groups[1].Value, CultureInfo.InvariantCulture))); + } + var perSite = samples.Select(sample => (double)sample.Cost / sample.Size).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{sample.Size} = {(double)sample.Cost / sample.Size:F0}"))); + if (perSite.Skip(1).Zip(perSite).Any(pair => pair.First > pair.Second * AllowedIdentityGrowth)) + throw new InvalidOperationException( + $"Static identities cost {measured} heartbeats per site; the limit is " + + $"{AllowedIdentityGrowth} times the cost per site at the size before."); + Console.WriteLine($"PASS static identities: {measured} heartbeats per site"); +} + +// Fails when element shape facts or reads cost over 1.35 times more per element type as types and layouts grow +// together, or over 1.35 times more in total as sites of 8 types grow; per-site facts would grow with the sites. +void ElementShapeCosts() +{ + const double AllowedElementGrowth = 1.35; + var shapeDirectory = At("artifacts/LookupScaling/ElementShapes"); + Directory.CreateDirectory(shapeDirectory); + (string Name, Func<(int Size, long Facts, long Reads), long> Cost)[] measures = + [("facts", sample => sample.Facts), ("reads", sample => sample.Reads)]; + // Both rows include 8 types at 8 sites; a sample is elaborated once and read by every row that includes it. + var samples = new Dictionary<(int Types, int Sites), (int Size, long Facts, long Reads)>(); + (int Size, long Facts, long Reads) Sample(int types, int sites) => + samples.TryGetValue((types, sites), out var known) ? known + : samples[(types, sites)] = ElementShapeSample(shapeDirectory, types, sites); + var byType = new[] { 8, 32, 128 }.Select(size => (Count: size, Sample: Sample(size, size))).ToArray(); + foreach (var (name, cost) in measures) + { + var perElement = byType.Select(sample => (double)cost(sample.Sample) / sample.Count).ToArray(); + var measured = string.Join("; ", byType.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{sample.Count} = {(double)cost(sample.Sample) / sample.Count:F0}"))); + if (perElement.Skip(1).Zip(perElement).Any(pair => pair.First > pair.Second * AllowedElementGrowth)) + throw new InvalidOperationException( + $"Element shape {name} cost {measured} heartbeats per element type; the limit is " + + $"{AllowedElementGrowth} times the cost per element type at the size before."); + Console.WriteLine($"PASS element shape {name}: {measured} heartbeats per element type"); + } + const int SharedTypes = 8; + var bySite = new[] { 8, 32, 128 }.Select(sites => + (Count: sites, Sample: Sample(SharedTypes, sites))).ToArray(); + foreach (var (name, cost) in measures) + { + var totals = bySite.Select(sample => (double)cost(sample.Sample)).ToArray(); + var measured = string.Join("; ", bySite.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{sample.Count} = {cost(sample.Sample)}"))); + if (totals.Skip(1).Zip(totals).Any(pair => pair.First > pair.Second * AllowedElementGrowth)) + throw new InvalidOperationException( + $"Element shape {name} of {SharedTypes} types cost {measured} heartbeats by site count; the limit " + + $"is {AllowedElementGrowth} times the cost at the site count before."); + Console.WriteLine($"PASS element shape {name} of {SharedTypes} types: {measured} heartbeats by site count"); + } +} + +// The heartbeats of the element shape facts of a method with `sites` struct element sites over `types` layouts, the +// site at position k naming layout k mod types, and of reading every type's shape through them. +(int Size, long Facts, long Reads) ElementShapeSample(string directory, int types, int sites) +{ + var indices = Enumerable.Range(0, types).ToArray(); + var raw = string.Join(",\n ", indices.Select(index => $$""" + { identity := "Cell{{index}}", explicit := false, packing := 0, declaredSize := 0, fields := [] } + """)); + var computed = string.Join(",\n ", indices.Select(index => + $$"""{ layout := { size := {{4 * (index + 1)}}, fields := [] }, alignment := 4 }""")); + var code = string.Join(", ", Enumerable.Range(0, sites).Select(site => + $$"""⟨{{5 * site}}, 163, .type (.aggregate "Cell{{site % types}}" {{site % types}})⟩""")); + var reads = string.Join(", ", indices.Select(index => + $$"""body.elementShape (.aggregate "Cell{{index}}" {{index}})""")); + var shapes = string.Join(", ", indices.Select(index => + $$""".value (.aggregate "Cell{{index}}" {{4 * (index + 1)}})""")); + var source = $$""" + import CIL.Safety.ValueSteps + open CIL CIL.Extraction Lean Elab Command + set_option maxRecDepth 8192 + /-- Elaboration and kernel checking of one command, excluding fixture construction. -/ + elab "#resolution_cost " label:str " in " command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + logInfo m!"resolution {label.getString} {after - before} heartbeats" + def rawLayouts : List RawLayout := [ + {{raw}}] + def computedLayouts : Array ComputedLayout := #[ + {{computed}}] + def environment : LayoutEnvironment := { rawLayouts := rawLayouts, layouts := computedLayouts } + def body : Method := { code := [.ret], locals := [], returnsValue := false, environment := environment } + theorem body_returnsValue : body.returnsValue = false := rfl + theorem body_declared : body.declared = {} := rfl + def bodyClosed : RawMethod := { + token := 1, owner := "Scaling", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := true, maxStack := 2, genericParameters := [], code := [{{code}}] } + #resolution_cost "fact" in cil_resolution_facts body + #resolution_cost "simplifier" in + theorem body_reads : [{{reads}}] = [{{shapes}}] := by simp only [cil_code] + #print axioms body_elementShape{{types - 1}} + #print axioms body_reads + """ + "\n"; + var path = Path.Combine(directory, $"Elements{types}Sites{sites}.lean"); + File.WriteAllText(path, source); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("Element shape facts introduced an unproved theorem."); + var costs = LookupPatterns.ReturnResolution.Matches(output).Cast().ToArray(); + if (costs.Length != 2) + throw new InvalidOperationException("Missing element shape costs: " + output); + return (types, long.Parse(costs[0].Groups[2].Value, CultureInfo.InvariantCulture), + long.Parse(costs[1].Groups[2].Value, CultureInfo.InvariantCulture)); +} + +// Fails when the shape facts or reads of distinct operands cost over 1.35 times more per operand than at the size +// before: fields of one struct up to 128, and fields and value types of a struct each up to 512, read by a +// 512-instruction body. A fact that walks the layouts to its struct costs 1.7 times as much per operand at 512 as +// at 128 for fields and 3.2 times for value types. Every row is measured before the first failure is reported. +void ShapeResolutionCosts(bool admissionOnly = false) +{ + const double AllowedShapeGrowth = 1.35; + var resolutionDirectory = At("artifacts/LookupScaling/ShapeResolution"); + Directory.CreateDirectory(resolutionDirectory); + (string Name, string Label, int[] Sizes, bool Spread, bool Value, bool Repeated)[] rows = + [ + ("Struct", "field shape {0} with every field in one struct", [8, 32, 128], false, false, false), + ("Spread", "field shape {0} with each field in its own struct", [8, 32, 128, 512], true, false, false), + ("Value", "value shape {0} with each type a struct of its own", [8, 32, 128, 512], true, true, false), + ("RepeatedValue", "value shape {0} at repeated sites of one struct", [8, 32, 128], true, true, true), + ]; + var failures = new List(); + foreach (var row in rows) + { + if (admissionOnly && row.Name != "Value") continue; + var samples = new List<(int Size, long Facts, long Reads)>(); + var admission = new List<(int Sites, double Cost)>(); + foreach (var size in row.Sizes) + { + var measureAdmission = row.Name == "Value" && size <= 128; + if (admissionOnly && !measureAdmission) continue; + var output = RunShapeFixture(resolutionDirectory, row.Name, size, row.Spread, row.Value, row.Repeated, + admissionOnly); + var costs = LookupPatterns.ReturnResolution.Matches(output).Cast().ToArray(); + var shapeCosts = admissionOnly ? 0 : (row.Repeated ? 1 : size) + 1; + if (costs.Length != shapeCosts + (measureAdmission ? 1 : 0)) + throw new InvalidOperationException("Missing shape resolution costs: " + output); + long Sum(string label) => costs.Where(match => match.Groups[1].Value == label) + .Sum(match => long.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)); + samples.Add((size, Sum("fact"), Sum("simplifier"))); + if (measureAdmission) admission.Add((size, (double)Sum("objects") / size)); + } + foreach (var (previous, current) in admission.Zip(admission.Skip(1))) + if (previous.Cost <= 0 || current.Cost <= 0 || current.Cost > previous.Cost * AllowedGrowth) + failures.Add($"Object admission grew from {previous.Cost:F0} to {current.Cost:F0} heartbeats/site."); + foreach (var sample in admission) + Console.WriteLine($"Object admission at {sample.Sites} distinct layouts: {sample.Cost:F0} heartbeats/site"); + if (admissionOnly) continue; + foreach (var (name, cost) in new (string, Func<(int Size, long Facts, long Reads), long>)[] + { ("facts", sample => sample.Facts), ("reads", sample => sample.Reads) }) + { + var label = string.Format(CultureInfo.InvariantCulture, row.Label, name); + var perOperand = samples.Select(sample => (double)cost(sample) / sample.Size).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{sample.Size} = {(double)cost(sample) / sample.Size:F0}"))); + if (perOperand.Skip(1).Zip(perOperand).Any(pair => pair.First > pair.Second * AllowedShapeGrowth)) + { + failures.Add($"{label} cost {measured} heartbeats per operand; the limit is {AllowedShapeGrowth} " + + "times the cost per operand at the size before."); + Console.WriteLine($"FAIL {label}: {measured} heartbeats per operand"); + } + else Console.WriteLine($"PASS {label}: {measured} heartbeats per operand"); + } + } + if (failures.Count > 0) throw new InvalidOperationException(string.Join("\n", failures)); +} + +// Fails when locating a class field on an instance, or rejecting one whose owner is outside the instance's lineage, +// costs over 1.35 times more per base than at the size before, for 2, 8 and 32 bases. The check walks the lineage +// once, so its cost per base falls toward a constant; a walk per base would grow with the bases. +void LineageCosts() +{ + const double AllowedLineageGrowth = 1.35; + var lineageDirectory = At("artifacts/LookupScaling/Lineage"); + Directory.CreateDirectory(lineageDirectory); + var samples = new List<(int Bases, long Rejected, long Located)>(); + foreach (var bases in new[] { 2, 8, 32 }) + { + var output = RunLineageFixture(lineageDirectory, bases); + var costs = LookupPatterns.Lineage.Matches(output).Cast().ToArray(); + if (costs.Length != 2) throw new InvalidOperationException("Missing lineage costs: " + output); + long Cost(string label) => long.Parse(costs.Single(match => match.Groups[1].Value == label).Groups[2].Value, + CultureInfo.InvariantCulture); + samples.Add((bases, Cost("rejected"), Cost("located"))); + } + var failures = new List(); + foreach (var (name, cost) in new (string, Func<(int Bases, long Rejected, long Located), long>)[] + { ("rejecting an unrelated owner", sample => sample.Rejected), ("locating the furthest base's field", + sample => sample.Located) }) + { + var perBase = samples.Select(sample => (double)cost(sample) / sample.Bases).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{sample.Bases} = {(double)cost(sample) / sample.Bases:F0}"))); + if (perBase.Skip(1).Zip(perBase).Any(pair => pair.First > pair.Second * AllowedLineageGrowth)) + { + failures.Add($"Lineage check {name} cost {measured} heartbeats per base; the limit is " + + $"{AllowedLineageGrowth} times the cost per base at the size before."); + Console.WriteLine($"FAIL lineage check {name}: {measured} heartbeats per base"); + } + else Console.WriteLine($"PASS lineage check {name}: {measured} heartbeats per base"); + } + if (failures.Count > 0) throw new InvalidOperationException(string.Join("\n", failures)); +} + +// Locates a field of the furthest of `bases` base classes on a fresh instance and rejects one of an unrelated class. +string RunLineageFixture(string directory, int bases) +{ + var lineage = string.Join(", ", Enumerable.Range(0, bases).Reverse().Select(index => + $".object \"T\" \"T.Base{index}\"")); + var source = $$""" + import CIL.Safety.HeapObjectFacts + open CIL CIL.Extraction CIL.Safety Lean Elab Command + /-- Elaboration and kernel checking of one command, excluding fixture construction. -/ + elab "#lineage_cost " label:str " in " command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + logInfo m!"lineage {label.getString} {after - before} heartbeats" + def lineageType : RuntimeType := { identity := .object "T" "T.Derived", bases := [{{lineage}}] } + #lineage_cost "rejected" in + theorem rejected (memory : Safety.Memory) : + objectFieldAddress (objectMemory memory lineageType 64) (.address ⟨memory.nextIdentity, 0⟩) + (.object "T" "T.Unrelated") 8 = + .error (.unsupported "Field owner is not the object's class or a base of it") := + objectMemory_field_unrelated memory lineageType 64 8 _ (by decide) + #lineage_cost "located" in + theorem located (memory : Safety.Memory) : + objectFieldAddress (objectMemory memory lineageType 64) (.address ⟨memory.nextIdentity, 0⟩) + (.object "T" "T.Base0") 8 = .ok ⟨memory.nextIdentity, 8⟩ := + objectMemory_field memory lineageType (by decide) (by decide) (by decide) + #print axioms rejected + #print axioms located + """ + "\n"; + var path = Path.Combine(directory, $"Lineage{bases}.lean"); + File.WriteAllText(path, source); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("The lineage fixture introduced an unproved theorem."); + return output; +} + +// Declares one shape fact per operand over size structs, or over one struct of size fields, and reads every fact. +string RunShapeFixture(string directory, string name, int size, bool spread, bool value, bool repeated, + bool admissionOnly) +{ + var operandCount = repeated ? 1 : size; + var structs = spread ? operandCount : 1; + var fieldsPerStruct = spread ? 1 : size; + var structFields = string.Join(", ", Enumerable.Repeat("{ type := .signed32 }", fieldsPerStruct)); + var raw = string.Join(",\n ", Enumerable.Range(0, structs).Select(index => + $"{{ identity := \"T::S{index}\", explicit := false, packing := 0, declaredSize := 0, " + + $"fields := [{structFields}] }}")); + var offsets = string.Join(", ", Enumerable.Range(0, fieldsPerStruct).Select(field => + $"{{ offset := {4 * field}, width := 4 }}")); + var computed = string.Join(",\n ", Enumerable.Repeat( + $"⟨{{ size := {4 * fieldsPerStruct}, fields := [{offsets}] }}, 4⟩", structs)); + var operands = new StringBuilder(); + var facts = new StringBuilder(); + for (var operand = 0; operand < operandCount; operand++) + { + var owner = spread ? operand : 0; + operands.AppendLine(value + ? $"def operand{operand} : TypeExpr := .aggregate \"T::S{owner}\" {owner}" + : $"def operand{operand} : FieldRef := {{ owner := .aggregate \"T::S{owner}\" {owner}, " + + $"index := {(spread ? 0 : operand)}, type := .atom runtimeScope \"System.Int32\", " + + $"name := name!\"f{operand}\" }}"); + if (!admissionOnly) facts.Append("#resolution_cost \"fact\" in "); + facts.AppendLine($"cil_{(value ? "value" : "field")}_shape environment " + + $"operand{operand}"); + } + var reads = string.Join(", ", Enumerable.Range(0, size).Select(operand => + $"body.{(value ? "valueShape" : "fieldShape")} operand{(repeated ? 0 : operand)}")); + var shapes = string.Join(", ", Enumerable.Range(0, size).Select(operand => + value ? ".aggregate 4" : $".value {(spread ? 0 : 4 * operand)} 4 false")); + var shapeType = value ? "ValueShape" : "FieldShape"; + var admission = ""; + if (name == "Value" && size <= 128) + { + var indices = Enumerable.Range(0, size); + var sites = string.Join(", ", indices.Select(index => + $"RawInstruction.mk {index} 0xfe1c (.type operand{index})")); + var code = string.Join(", ", indices.Select(index => $".sizeof operand{index}")); + var proof = Nethermind.LeanCIL.ObjectOperandProof.Render("emitted", + indices.Select(index => (0xfe1c, $"operand{index}"))); + admission = $$""" + def raw : RawMethod := { + token := 1, owner := "Scaling", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := false, maxStack := {{size}}, genericParameters := [], code := [{{sites}}] } + def emitted : Method := { body with code := [{{code}}] } + #resolution_cost "objects" in + theorem objects_checked : ObjectOperandsValid raw (List.replicate {{size}} true) emitted := + {{proof}} + #print axioms objects_checked + """; + } + var shapeReads = admissionOnly ? "" : $$""" + #resolution_cost "simplifier" in + theorem body_reads : ([{{reads}}] : List {{shapeType}}) = [{{shapes}}] := by simp only [cil_code] + #print axioms body_reads + """; + var source = $$""" + import CIL.Safety.ResolutionFacts + import CIL.Extraction.ObjectOperands + open CIL CIL.Extraction Lean Elab Command + set_option maxRecDepth 8192 + /-- Elaboration and kernel checking of one command, excluding fixture construction. -/ + elab "#resolution_cost " label:str " in " command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + logInfo m!"resolution {label.getString} {after - before} heartbeats" + def rawLayouts : List RawLayout := [ + {{raw}}] + def computedLayouts : Array ComputedLayout := #[ + {{computed}}] + @[cil_code] def environment : LayoutEnvironment := + { rawLayouts := rawLayouts, layouts := computedLayouts } + def body : Method := + { code := List.replicate 512 .nop, locals := [], returnsValue := false, environment } + {{operands}} + {{facts}} + {{admission}} + {{shapeReads}} + #print axioms operand{{operandCount - 1}}_shape + """ + "\n"; + var path = Path.Combine(directory, $"{name}{size}.lean"); + File.WriteAllText(path, source); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("Shape resolution introduced an unproved theorem."); + return output; +} + +// Decides the managed-call check on bodies of call and nop pairs with the extractor's literal table; a per-pc table +// scan costs the square of the call sites. +void CallCheckCosts() +{ + var checkDirectory = At("artifacts/LookupScaling/CallCheck"); + Directory.CreateDirectory(checkDirectory); + var samples = new List<(int Sites, long Cost)>(); + foreach (var sites in new[] { 64, 256, 1024 }) + { + var pcs = Enumerable.Range(0, sites).Select(site => 2 * site).ToArray(); + var raw = string.Join(", ", pcs.Select(_ => "site, ⟨0, 0, .none⟩")); + var code = string.Join(", ", pcs.Select(_ => ".call callMethod, .nop")); + var links = string.Join(", ", pcs.Select(pc => $"({pc}, .direct 0)")); + var statement = + $"ManagedCallsValid [⟨[], callee⟩] [] (fun _ => false) raw (List.replicate {2 * sites} true) body"; + var source = $$""" + import CIL.Extraction.Generics + open CIL CIL.Extraction Lean Elab Command + set_option maxRecDepth 65536 + set_option maxHeartbeats 0 + /-- Elaboration and kernel checking of one command, excluding fixture construction. -/ + elab "#call_check_cost " command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + logInfo m!"call check {after - before} heartbeats" + def callee : RawMethod := { + token := 1, owner := "Tests::Scaling", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, + returnType := .void, parameters := [], locals := [], initLocals := true, maxStack := 1, + genericParameters := [], code := [] } + def callMethod : MethodRef := { + owner := .atom "Tests" "Scaling", name := name!"", hasThis := false, genericArity := 0, + returnType := .void, parameters := [] } + def site : RawInstruction := ⟨0, 40, .call { + method := callMethod, declared := { convention := 0 }, target := .managed 1, + typeArguments := [], parameterCount := 0, + returnsValue := false }⟩ + def raw : RawMethod := { callee with code := [{{raw}}] } + def body : Method := { locals := [], returnsValue := false, code := [{{code}}], links := [{{links}}] } + #call_check_cost + theorem checked : {{statement}} := by decide +kernel + #print axioms checked + """ + "\n"; + var path = Path.Combine(checkDirectory, $"Sites{sites}.lean"); + File.WriteAllText(path, source); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("The call check cost fixture introduced an unproved theorem."); + var cost = LookupPatterns.CallCheck.Match(output); + if (!cost.Success) throw new InvalidOperationException("Missing the call check cost: " + output); + samples.Add((sites, long.Parse(cost.Groups[1].Value, CultureInfo.InvariantCulture))); + } + var perSite = samples.Select(sample => (double)sample.Cost / sample.Sites).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{sample.Sites} = {(double)sample.Cost / sample.Sites:F0}"))); + if (perSite.Skip(1).Zip(perSite).Any(pair => pair.First > pair.Second * AllowedCallGrowth)) + throw new InvalidOperationException( + $"The managed-call check costs {measured} heartbeats per call site; the limit is {AllowedCallGrowth} " + + "times the cost per call site at the size before."); + Console.WriteLine($"PASS the managed-call check costs {measured} heartbeats per call site"); +} + +// Isolating one validator must also remove generated facts whose signature premises were removed. +void CheckNarrowedResolution() +{ + const string Source = """ + import CIL.Safety.ResolutionFacts + namespace Extracted + open CIL CIL.Extraction + def method0 : Method := + { code := [], locals := [], returnsValue := true, + declared := { returnType := .atom runtimeScope "System.Byte" } } + theorem method0_returnsValue : method0.returnsValue = true := rfl + theorem method0_declared : + method0.declared = { returnType := .atom runtimeScope "System.Byte" } := rfl + cil_resolution_facts method0 + theorem selected : True := True.intro + #print axioms method0_returnsValue + #print axioms method0_declared + #print axioms method0_returnNarrow + #print axioms selected + end Extracted + """; + var directory = At("artifacts/LookupScaling/ReturnResolution"); + Directory.CreateDirectory(directory); + var path = Path.Combine(directory, "Narrowed.lean"); + File.WriteAllText(path, NarrowEvidence(Source + "\n", "selected") + "\n#print axioms Extracted.selected\n"); + Run(["lake", "env", "lean", path], cwd: FixturePackage()); + Console.WriteLine("PASS isolated evidence retains its selected obligation with generated return facts"); +} + +// A command that logs the heartbeats `decide +kernel` adds to the same statement closed with `sorry`. +const string KernelCostCommand = """ + /-- Logs what `decide +kernel` costs beyond elaborating the statement. -/ + elab "#kernel_cost " label:str statement:term : command => do + let before <- IO.getNumHeartbeats + elabCommand (<- `(example : $statement := sorry)) + let middle <- IO.getNumHeartbeats + elabCommand (<- `(example : $statement := by decide +kernel)) + let after <- IO.getNumHeartbeats + let cost : Int := ((after - middle : Nat) : Int) - ((middle - before : Nat) : Int) + logInfo m!"{label.getString} {cost} heartbeats" + """; + +// Exception decisions compare constructors, so display-name length cannot increase their cost. +void ExceptionDecisionCosts() +{ + var directory = At("artifacts/LookupScaling/ExceptionTypes"); + Directory.CreateDirectory(directory); + string[] members = ["format", "applicationException"]; + var statements = members.SelectMany(member => + { + string fault = $"ExecutionFault.thrown .{member}"; + return new[] + { + $"#kernel_cost \"thrownEqual {member}\" {fault} = {fault}", + $"#kernel_cost \"thrownUnequal {member}\" Not ({fault} = ExecutionFault.thrown .overflow)", + $"#kernel_cost \"clrUnequal {member}\" Not (({fault}).clrException = some .overflow)", + $"#kernel_cost \"raisesEqual {member}\" (some .{member} : Option ExceptionType) = some .{member}" + }; + }); + var path = Path.Combine(directory, "Equality.lean"); + File.WriteAllText(path, """ + import CIL.Safety.RuntimeExceptions + open CIL CIL.Safety Lean Elab Command + + """ + KernelCostCommand + "\n" + string.Join("\n", statements) + "\n"); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var costs = LookupPatterns.ExceptionDecision.Matches(output).ToDictionary( + match => match.Groups[1].Value, + match => long.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)); + if (costs.Count != 8) throw new InvalidOperationException("Missing exception decision costs:\n" + output); + foreach (var decision in new[] { "thrownEqual", "thrownUnequal", "clrUnequal", "raisesEqual" }) + { + long shortName = costs[decision + " format"]; + long longName = costs[decision + " applicationException"]; + var measured = $"{shortName} heartbeats at format and {longName} at applicationException"; + if (Math.Min(shortName, longName) < KernelDecisionFloor || + Math.Max(shortName, longName) > TypeDecisionLimit || longName > shortName * AllowedNameGrowth) + throw new InvalidOperationException($"Exception decision {decision} exceeds its cost bound: {measured}"); + Console.WriteLine($"PASS deciding {decision} costs {measured}"); + } +} + +// Lean definitions of a value-only body of `instructions` instructions: `raw`, its raw instructions, and `code`, the +// instructions the translator emits for them. Raw instructions carry distinct offsets, so the kernel decodes each one +// instead of reusing a cached decode. +static string ValueBody(int instructions) +{ + const int SegmentLength = 10; + return $$""" + /-- The raw instructions of one segment: short and long constants, locals, an argument, + arithmetic, a checked conversion, a branch, a four-target switch and a return. -/ + def segment (index : Nat) : List RawInstruction := + let start := {{SegmentLength}} * index + [⟨start, 31, .integer (index % 100)⟩, ⟨start + 1, 32, .integer (70000 + index)⟩, + ⟨start + 2, 17, .local (index % 4)⟩, ⟨start + 3, 19, .local (index % 4)⟩, + ⟨start + 4, 14, .argument (index % 4)⟩, ⟨start + 5, 88, .none⟩, ⟨start + 6, 130, .none⟩, + ⟨start + 7, 58, .target index⟩, + ⟨start + 8, 69, .targets [index, index + 1, index + 2, index + 3]⟩, ⟨start + 9, 42, .none⟩] + /-- The instructions the translator emits for one segment. -/ + def codeSegment (index : Nat) : List Instruction := + [.ldc_i4 (BitVec.ofNat 32 (index % 100)), .ldc_i4 (BitVec.ofNat 32 (70000 + index)), + .ldloc (index % 4), .stloc (index % 4), .ldarg (index % 4), .add, .conv_ovf_un .i1, + .brtrue index, .switch [index, index + 1, index + 2, index + 3], .ret] + /-- A body of {{instructions}} raw instructions. -/ + def raw : List RawInstruction := (List.range {{instructions / SegmentLength}}).flatMap segment + /-- The {{instructions}} emitted instructions of the body. -/ + def code : List Instruction := (List.range {{instructions / SegmentLength}}).flatMap codeSegment + """; +} + +// The heartbeats `decide +kernel` adds to deciding that every instruction of a body of 500, 2000 and 8000 +// instructions is the one its raw value opcode decodes to. Decoding costs the same at every instruction and a +// `switch` its targets, so four times the instructions may cost at most `AllowedLocalGrowth` more per instruction. +void DecodeCosts() +{ + var decodeDirectory = At("artifacts/LookupScaling/ValueDecoding"); + Directory.CreateDirectory(decodeDirectory); + var samples = new List<(int Instructions, long Cost, long Milliseconds)>(); + foreach (var instructions in new[] { 500, 2000, 8000 }) + { + var path = Path.Combine(decodeDirectory, $"Instructions{instructions}.lean"); + File.WriteAllText(path, $$""" + import CIL.Extraction.ValueDecoding + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + open CIL CIL.Extraction Lean Elab Command + {{KernelCostCommand}} + namespace ValueDecodingCost + {{ValueBody(instructions)}} + #kernel_cost "decode {{instructions}}" + (raw.zip code).all (fun pair => valueEncodingMatches none pair.1 pair.2) = true + end ValueDecodingCost + """ + "\n"); + var package = FixturePackage(); + var clock = System.Diagnostics.Stopwatch.StartNew(); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: package); + var cost = LookupPatterns.ValueDecoding.Match(output); + if (!cost.Success) throw new InvalidOperationException($"Missing the value decoding cost:\n{output}"); + samples.Add((instructions, long.Parse(cost.Groups[1].Value, CultureInfo.InvariantCulture), + clock.ElapsedMilliseconds)); + } + var perInstruction = samples.Select(sample => (double)sample.Cost / sample.Instructions).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{(double)sample.Cost / sample.Instructions:F1} at {sample.Instructions} ({sample.Milliseconds} ms)"))); + if (samples.Any(sample => sample.Cost < KernelDecisionFloor) || + perInstruction.Skip(1).Zip(perInstruction).Any(pair => pair.First > pair.Second * AllowedLocalGrowth)) + throw new InvalidOperationException( + $"Decoding raw value opcodes costs {measured} heartbeats per instruction; the limit is " + + $"{AllowedLocalGrowth} times the cost per instruction at the size before."); + Console.WriteLine($"PASS decoding raw value opcodes costs {measured} heartbeats per instruction"); +} + +// Body evidence checks prefix structure. Raw construction also decides branch entry. +void PrefixEntryCosts() +{ + const int SegmentLength = 4; + const double DestinationOrderTolerance = 0.02; + var directory = At("artifacts/LookupScaling/PrefixEntry"); + Directory.CreateDirectory(directory); + var samples = new Dictionary> + { ["body"] = [], ["ascending"] = [], ["descending"] = [], ["shuffled"] = [] }; + foreach (var instructions in new[] { 500, 2000, 8000 }) + { + var path = Path.Combine(directory, $"Instructions{instructions}.lean"); + var ascending = Enumerable.Range(0, instructions / SegmentLength).Select(index => SegmentLength * index) + .ToArray(); + var shuffled = ascending.ToArray(); + new Random(1701).Shuffle(shuffled); + File.WriteAllText(path, $$""" + import CIL.Extraction.Scalars + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + open CIL CIL.Extraction Lean Elab Command + {{KernelCostCommand}} + namespace PrefixEntryCost + /-- The element type of every address. -/ + def intType : TypeExpr := .atom runtimeScope "System.Int32" + /-- Each segment branches to the prefix at the given destination. -/ + def segment (destination index : Nat) : List RawInstruction := + let start := {{SegmentLength}} * index + [⟨start, 65054, .none⟩, ⟨start + 1, 143, .type intType⟩, ⟨start + 2, 38, .none⟩, + ⟨start + 3, 43, .target destination⟩] + /-- The same {{instructions}} instructions with the given branch operands. -/ + def raw (targets : List Nat) := + targets.zipIdx.flatMap fun (target, pc) => segment target pc + /-- The branch targets in increasing order. -/ + def ascending := raw [{{string.Join(",", ascending)}}] + /-- The same branch targets in decreasing order. -/ + def descending := raw [{{string.Join(",", ascending.Reverse())}}] + /-- A fixed permutation of the same branch targets. -/ + def shuffled := raw [{{string.Join(",", shuffled)}}] + #kernel_cost "prefix body {{instructions}}" + prefixSequenceValid .void ascending = true + #kernel_cost "prefix ascending {{instructions}}" rawPrefixesValid .void ascending = true + #kernel_cost "prefix descending {{instructions}}" rawPrefixesValid .void descending = true + #kernel_cost "prefix shuffled {{instructions}}" rawPrefixesValid .void shuffled = true + end PrefixEntryCost + + """); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var costs = LookupPatterns.PrefixEntry.Matches(output); + foreach (var (kind, rows) in samples) + { + var cost = costs.Single(match => match.Groups[1].Value == kind); + rows.Add((instructions, long.Parse(cost.Groups[2].Value, CultureInfo.InvariantCulture))); + } + } + var failures = new List(); + foreach (var (kind, rows) in samples) + { + var costs = rows.Select(row => (double)row.Cost / row.Instructions).ToArray(); + var measured = string.Join("; ", rows.Select(row => string.Create(CultureInfo.InvariantCulture, + $"{(double)row.Cost / row.Instructions:F1} at {row.Instructions}"))); + if (rows.Any(row => row.Cost < KernelDecisionFloor) || + costs.Skip(1).Zip(costs).Any(pair => pair.First > pair.Second * AllowedLocalGrowth)) + failures.Add($"Prefix {kind} exceeds the growth limit at {measured}"); + Console.WriteLine($"Prefix {kind} costs {measured} heartbeats per instruction"); + if (kind is "descending" or "shuffled" && rows.Zip(samples["ascending"]).Any(pair => + Math.Abs((double)pair.First.Cost / pair.Second.Cost - 1) > DestinationOrderTolerance)) + failures.Add($"Prefix {kind} differs from ascending by more than 2% at {measured}"); + } + if (failures.Count != 0) throw new InvalidOperationException(string.Join(Environment.NewLine, failures)); + Console.WriteLine("PASS prefix validation growth and destination order"); +} + +// The heartbeats `decide +kernel` adds to deciding that `Method.ofRaw?` of a value-only body of 500, 2000 and 8000 +// instructions with four parameters and four locals gives the emitted code, the initialized locals and no homes or +// links. Each instruction is decoded and instantiated once, so four times the instructions may cost at most +// `AllowedLocalGrowth` more per instruction. +void MethodDecodeCosts() +{ + var decodeDirectory = At("artifacts/LookupScaling/DecodedMethod"); + Directory.CreateDirectory(decodeDirectory); + var samples = new List<(int Instructions, long Cost)>(); + foreach (var instructions in new[] { 500, 2000, 8000 }) + { + var path = Path.Combine(decodeDirectory, $"Instructions{instructions}.lean"); + File.WriteAllText(path, $$""" + import CIL.Extraction.DecodedMethod + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + open CIL CIL.Extraction Lean Elab Command + {{KernelCostCommand}} + namespace DecodedMethodCost + {{ValueBody(instructions)}} + /-- The type of every parameter and local. -/ + def uint32 : TypeExpr := .atom runtimeScope "System.UInt32" + /-- A static method with four parameters, four zero-initialized locals and {{instructions}} raw + instructions. -/ + def source : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := List.replicate 4 uint32, + locals := List.replicate 4 uint32, initLocals := true, + maxStack := 8, genericParameters := [], code := raw } + #kernel_cost "decoded method {{instructions}}" + (Method.ofRaw? .scalar {} [] (List.replicate {{instructions}} true) source []).any (fun method => + method.code.length == {{instructions}} && + (method.code.zip code).all (fun pair => pair.1.sameValue pair.2) && + method.locals == List.replicate 4 (.i32 0) && method.parameterHomes.isEmpty && + method.links.isEmpty) = true + end DecodedMethodCost + """ + "\n"); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var cost = LookupPatterns.DecodedMethod.Match(output); + if (!cost.Success) throw new InvalidOperationException($"Missing the decoded method cost:\n{output}"); + samples.Add((instructions, long.Parse(cost.Groups[1].Value, CultureInfo.InvariantCulture))); + } + var perInstruction = samples.Select(sample => (double)sample.Cost / sample.Instructions).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{(double)sample.Cost / sample.Instructions:F1} at {sample.Instructions}"))); + if (samples.Any(sample => sample.Cost < KernelDecisionFloor) || + perInstruction.Skip(1).Zip(perInstruction).Any(pair => pair.First > pair.Second * AllowedLocalGrowth)) + throw new InvalidOperationException( + $"Decoding a method costs {measured} heartbeats per instruction; the limit is " + + $"{AllowedLocalGrowth} times the cost per instruction at the size before."); + Console.WriteLine($"PASS decoding a method costs {measured} heartbeats per instruction"); +} + +// The heartbeats `cil_decoded_method` spends defining a value-only method of 500, 2000 and 8000 instructions with +// four locals from its raw definition, its kernel checks included. Its literal and its checks are linear in the +// instructions, so four times the instructions may cost at most `AllowedLocalGrowth` more per instruction. +void DecodedDefinitionCosts() +{ + var definitionDirectory = At("artifacts/LookupScaling/DecodedDefinition"); + Directory.CreateDirectory(definitionDirectory); + const int SegmentLength = 10; + var samples = new List<(int Instructions, long Cost)>(); + foreach (var instructions in new[] { 500, 2000, 8000 }) + { + var path = Path.Combine(definitionDirectory, $"Instructions{instructions}.lean"); + File.WriteAllText(path, $$""" + import CIL.Extraction.DecodedDefinition + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + open CIL CIL.Extraction Lean Elab Command + /-- Logs the heartbeats elaborating `body` costs, its kernel checks included. -/ + elab "#command_cost " label:str body:command : command => do + let before <- IO.getNumHeartbeats + elabCommand body + let after <- IO.getNumHeartbeats + logInfo m!"{label.getString} {after - before} heartbeats" + namespace DecodedDefinitionCost + /-- The raw instructions of one segment: short and long constants, locals, arithmetic, a checked + conversion, a branch, a four-target switch and a return. -/ + def segment (index : Nat) : List RawInstruction := + let start := {{SegmentLength}} * index + [⟨start, 31, .integer (index % 100)⟩, ⟨start + 1, 32, .integer (70000 + index)⟩, + ⟨start + 2, 17, .local (index % 4)⟩, ⟨start + 3, 19, .local (index % 4)⟩, + ⟨start + 4, 6, .none⟩, ⟨start + 5, 88, .none⟩, ⟨start + 6, 130, .none⟩, + ⟨start + 7, 58, .target index⟩, + ⟨start + 8, 69, .targets [index, index + 1, index + 2, index + 3]⟩, ⟨start + 9, 42, .none⟩] + /-- The profile the method is decoded under, named as an extraction names it. -/ + def profile : FeatureProfile := .scalar + /-- The layouts the method is decoded under, named as an extraction names them. -/ + def environment : LayoutEnvironment := {} + /-- A static method with four zero-initialized `uint32` locals and {{instructions}} raw instructions. -/ + def source : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], + locals := List.replicate 4 (.atom runtimeScope "System.UInt32"), initLocals := true, + maxStack := 8, genericParameters := [], + code := (List.range {{instructions / SegmentLength}}).flatMap segment } + #command_cost "decoded definition {{instructions}}" + cil_decoded_method method := + Method.ofRaw? profile environment [] (List.replicate {{instructions}} true) source [] + end DecodedDefinitionCost + """ + "\n"); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var cost = LookupPatterns.DecodedDefinition.Match(output); + if (!cost.Success) throw new InvalidOperationException($"Missing the decoded definition cost:\n{output}"); + samples.Add((instructions, long.Parse(cost.Groups[1].Value, CultureInfo.InvariantCulture))); + } + var perInstruction = samples.Select(sample => (double)sample.Cost / sample.Instructions).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{(double)sample.Cost / sample.Instructions:F1} at {sample.Instructions}"))); + if (samples.Any(sample => sample.Cost < KernelDecisionFloor) || + perInstruction.Skip(1).Zip(perInstruction).Any(pair => pair.First > pair.Second * AllowedLocalGrowth)) + throw new InvalidOperationException( + $"Defining a decoded method costs {measured} heartbeats per instruction; the limit is " + + $"{AllowedLocalGrowth} times the cost per instruction at the size before."); + Console.WriteLine($"PASS defining a decoded method costs {measured} heartbeats per instruction"); +} + +// The heartbeats `decodedProgram?_eq_some` costs to state and prove that 8, 32 and 128 records decode to their +// methods, given one decoding fact per record. Each record adds one constructor, so four times the records may cost +// at most `AllowedLocalGrowth` more per record. +void DecodedProgramCosts() +{ + var programDirectory = At("artifacts/LookupScaling/DecodedProgram"); + Directory.CreateDirectory(programDirectory); + var samples = new List<(int Records, long Cost)>(); + foreach (var records in new[] { 8, 32, 128 }) + { + var indexes = Enumerable.Range(0, records).ToArray(); + var items = string.Join(" ", indexes.Select(index => $"item{index}")); + var methods = string.Join(" ", indexes.Select(index => $"method{index}")); + var facts = string.Join("\n", indexes.Select(index => + $" (decodes{index} : Decodes profile environment item{index} method{index})")); + var bodies = string.Concat(indexes.Select(index => $"(.cons decodes{index} ")) + ".nil" + + new string(')', records); + var path = Path.Combine(programDirectory, $"Records{records}.lean"); + File.WriteAllText(path, $$""" + import CIL.Safety.DecodedSummary + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + open CIL CIL.Extraction CIL.Safety Lean Elab Command + /-- Logs the heartbeats elaborating `body` costs, its kernel checks included. -/ + elab "#command_cost " label:str body:command : command => do + let before <- IO.getNumHeartbeats + elabCommand body + let after <- IO.getNumHeartbeats + logInfo m!"{label.getString} {after - before} heartbeats" + namespace DecodedProgramCost + variable (profile : FeatureProfile) (environment : LayoutEnvironment) + /-- The pinned body bytes of `item` decode to `method`. -/ + abbrev Decodes (item : MethodEvidence) (method : CIL.Method) : Prop := + Method.ofBody? profile environment item.links item.reachable item.body item.original + item.arguments = some method + #command_cost "decoded program {{records}}" + theorem decoded ({{items}} : MethodEvidence) ({{methods}} : CIL.Method) + {{facts}} + (masks : programBindingsValid [{{items.Replace(" ", ", ")}}] + [{{methods.Replace(" ", ", ")}}] = true) : + decodedProgram? profile environment [{{items.Replace(" ", ", ")}}] = + some [{{methods.Replace(" ", ", ")}}] := + decodedProgram?_eq_some masks {{bodies}} + end DecodedProgramCost + """ + "\n"); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var cost = LookupPatterns.DecodedProgram.Match(output); + if (!cost.Success) throw new InvalidOperationException($"Missing the decoded program cost:\n{output}"); + samples.Add((records, long.Parse(cost.Groups[1].Value, CultureInfo.InvariantCulture))); + } + var perRecord = samples.Select(sample => (double)sample.Cost / sample.Records).ToArray(); + var measured = string.Join("; ", samples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{(double)sample.Cost / sample.Records:F0} at {sample.Records}"))); + if (samples.Any(sample => sample.Cost < KernelDecisionFloor) || + perRecord.Skip(1).Zip(perRecord).Any(pair => pair.First > pair.Second * AllowedLocalGrowth)) + throw new InvalidOperationException( + $"Proving a decoded program costs {measured} heartbeats per record; the limit is " + + $"{AllowedLocalGrowth} times the cost per record at the size before."); + Console.WriteLine($"PASS proving a decoded program costs {measured} heartbeats per record"); +} + +// A closure search reads each of its n methods from a program index stated once and gives each of the e callees their +// link tables enter one packed table entry, so it costs O(n * log P + e) kernel steps in a program of P methods. Each +// shape grows one term: a chain whose closure is the whole program grows n * log P, up to the corpus scale of several +// hundred methods, one method calling seven others grows e, and a closure of four methods at the start of a longer +// program grows log P alone. Reading each method from the list instead costs n * P, which the tail pays in P and which +// the row also measures; folding over the search state costs time quadratic in e. Stating the index costs O(P) once. +void ClosureCosts() +{ + var closureDirectory = At("artifacts/LookupScaling/Closures"); + Directory.CreateDirectory(closureDirectory); + // Four times the units may cost at most this much more per unit. + const double AllowedClosureGrowth = 1.15; + int[][] Chain(int size) => [.. Enumerable.Range(0, size).Select(int[] (index) => + index + 1 < size ? [index + 1] : [])]; + ClosureShape[] shapes = + [ + new("chain", [8, 32, 128], Chain, size => size, size => size - 1), + new("longchain", [128, 256, 512], Chain, size => size, size => size - 1), + new("fan", [32, 128, 512], size => [[.. Enumerable.Range(0, size).Select(index => 1 + index % 7)], + .. Enumerable.Repeat(Array.Empty(), 7)], _ => 8, size => size), + new("tail", [128, 512, 2048], size => [[1], [2], [3], .. Enumerable.Repeat(Array.Empty(), size - 3)], + _ => 4, _ => 3) + ]; + // The list form of the long chain costs tens of millions of heartbeats at 512, so only the others measure it. + bool ListMeasured(ClosureShape shape) => shape.Name != "longchain"; + string Statement(ClosureShape shape, int size) + { + var program = shape.Program(size); + var name = $"{shape.Name}{size}"; + var expected = $"some [{string.Join(", ", Enumerable.Range(0, shape.Methods(size)))}]"; + var list = ListMeasured(shape) + ? $"#kernel_cost \"list closure {shape.Name} {size}\" (({name}.closure 0).map Prod.snd = {expected})\n" + : ""; + // The tail programs also state their index as an extraction does, between two heartbeat marks. + var start = shape.Name == "tail" ? $"#heartbeat_mark \"index {size} start\"" : ""; + var stated = shape.Name != "tail" ? "" : $$""" + /-- The length of the {{name}} program. -/ + def {{name}}Length : Nat := {{program.Length}} + theorem {{name}}Length_eq : {{name}}Length = {{name}}.length := by cil_kernel_rfl + theorem {{name}}Index_eq : {{name}}Index = FlowIndex.ofList {{name}} := by cil_kernel_rfl + theorem {{name}}Index_get? (index : Nat) : {{name}}Index.get? index = {{name}}[index]? := by + rw [{{name}}Index_eq]; exact FlowIndex.get?_ofList {{name}} index + #heartbeat_mark "index {{size}} end" + #print axioms [{{name}}Length_eq, {{name}}Index_eq, {{name}}Index_get?] + + """; + return $$""" + /-- The {{shape.Name}} program of size {{size}}. -/ + def {{name}} : Program := + [{{string.Join(", ", program.Select(callees => $"stub [{string.Join(", ", callees)}]"))}}] + {{start}} + /-- The index of the {{name}} program. -/ + def {{name}}Index : FlowIndex Method := program_index% {{name}} + {{stated}}#kernel_cost "closure {{shape.Name}} {{size}}" + ((closureWith {{name}}Index.get? {{program.Length}} 0).map Prod.snd = {{expected}}) + {{list}} + """; + } + var path = Path.Combine(closureDirectory, "Closures.lean"); + File.WriteAllText(path, $$""" + import CIL.Safety.ProgramClosure + import CIL.Extraction.KernelEquation + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + open CIL CIL.Extraction Lean Elab Command + {{KernelCostCommand}} + /-- Build a literal list outside the measured elaboration of its index. -/ + elab "sample_cells% " size:num : term => do + let cells := List.replicate size.getNat (mkNatLit 0) + Meta.mkListLit (mkConst ``Nat) cells + /-- The smaller input to the native index construction check. -/ + def smallCells : List Nat := sample_cells% 2048 + /-- Sixteen times the smaller input. -/ + def largeCells : List Nat := sample_cells% 32768 + -- Heartbeats omit native list traversal when the reads allocate nothing. + run_cmd liftTermElabM do + let measure (input : Term) : Term.TermElabM Nat := do + let mut samples := #[] + for _ in [:3] do + let before <- IO.monoNanosNow + let tree <- Term.elabTerm (<- `(program_index% $input)) none + let after <- IO.monoNanosNow + unless tree.isAppOf ``FlowIndex.branch do throwError "Expected a literal branch" + samples := samples.push (after - before) + return samples.foldl min samples[0]! + let small <- measure (<- `(smallCells)) + let large <- measure (<- `(largeCells)) + logInfo m!"Index construction at 2048 / 32768 cells: {small} / {large} ns" + -- Sixteen times as many cells allow four times the linear growth for timing variation. + unless 0 < small && large <= 64 * small do + throwError "Index construction grows faster than the traversal bound" + /-- Logs the heartbeats spent so far, so two marks give the cost of the commands between them. -/ + elab "#heartbeat_mark " label:str : command => do + let count <- IO.getNumHeartbeats + logInfo m!"mark {label.getString} {count}" + /-- A method whose link table enters `callees` in order, one call pc each. -/ + def stub (callees : List Nat) : Method := + { code := [], locals := [], returnsValue := false, + links := callees.zipIdx.map fun (callee, pc) => (pc, .direct callee) } + {{string.Join("\n", shapes.SelectMany(shape => shape.Sizes.Select(size => Statement(shape, size))))}} + + """); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException($"A closure certificate depends on sorryAx in {path}"); + Console.WriteLine(output.Split('\n').Single(line => line.StartsWith("Index construction at", + StringComparison.Ordinal)).TrimEnd()); + var costs = LookupPatterns.ClosureCost.Matches(output).ToDictionary( + match => (match.Groups[1].Value.Length > 0, match.Groups[2].Value, + int.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture)), + match => long.Parse(match.Groups[4].Value, CultureInfo.InvariantCulture)); + var marks = LookupPatterns.HeartbeatMark.Matches(output).ToDictionary( + match => (int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), match.Groups[2].Value), + match => long.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture)); + var tail = shapes.Single(shape => shape.Name == "tail"); + if (costs.Count != shapes.Sum(shape => shape.Sizes.Length * (ListMeasured(shape) ? 2 : 1)) || + costs.Values.Min() < KernelDecisionFloor || marks.Count != 2 * tail.Sizes.Length) + throw new InvalidOperationException($"Missing closure costs:\n{output}"); + string Measured(ClosureShape shape, bool list) => string.Join(", ", shape.Sizes.Select(size => + string.Create(CultureInfo.InvariantCulture, $"{costs[(list, shape.Name, size)]} at {size}"))); + foreach (var shape in shapes) + { + var units = shape.Sizes.Select(size => shape.Methods(size) * + Math.Ceiling(Math.Log2(shape.Program(size).Length)) + shape.Edges(size)).ToArray(); + if (ClosureGrowth(units, [.. units.Select(unit => unit * unit)]) <= AllowedClosureGrowth) + throw new InvalidOperationException($"The {shape.Name} bound accepts a cost quadratic in n * log P + e"); + // A whole-program closure of a growing program that read each method from the list would cost n * P, which + // the bound must refuse. + var listReads = shape.Sizes.Select(size => + (double)shape.Methods(size) * shape.Program(size).Length + shape.Edges(size)).ToArray(); + if (shape.Program(shape.Sizes[0]).Length < shape.Program(shape.Sizes[^1]).Length && + shape.Sizes.All(size => shape.Methods(size) == shape.Program(size).Length) && + ClosureGrowth(units, listReads) <= AllowedClosureGrowth) + throw new InvalidOperationException($"The {shape.Name} bound accepts list reads of its methods"); + void CheckGrowth(bool list, double[] work, string unit) + { + var samples = shape.Sizes.Select(size => (double)costs[(list, shape.Name, size)]).ToArray(); + var growth = ClosureGrowth(work, samples); + var route = list ? "list" : "index"; + var measured = string.Create(CultureInfo.InvariantCulture, $$""" + The {{shape.Name}} closure through the {{route}} costs {{Measured(shape, list)}} heartbeats. + Each added unit of {{unit}} costs {{growth:F2}} times as much between the last two sizes. + """); + if (!double.IsFinite(growth) || growth > AllowedClosureGrowth) + throw new InvalidOperationException($"{measured} The limit is {AllowedClosureGrowth}."); + Console.WriteLine($"PASS {measured}"); + } + CheckGrowth(false, units, "n * log P + e"); + if (ListMeasured(shape)) CheckGrowth(true, listReads, "n * P + e"); + } + // A closure of a few methods must cost less through the index than through the list at every program size. + foreach (var size in tail.Sizes) + if (costs[(false, "tail", size)] >= costs[(true, "tail", size)]) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $$""" + The tail closure of {{size}} costs {{costs[(false, "tail", size)]}} heartbeats through the index. + It must cost less than the list lookup's {{costs[(true, "tail", size)]}} heartbeats. + """)); + // Stating the index, its length and its lookup theorem costs time linear in P. + var programs = tail.Sizes.Select(size => (double)size).ToArray(); + var stating = tail.Sizes.Select(size => (double)(marks[(size, "end")] - marks[(size, "start")])).ToArray(); + var statingGrowth = ClosureGrowth(programs, stating); + var statingMeasured = string.Join(", ", tail.Sizes.Select(size => string.Create(CultureInfo.InvariantCulture, + $"{marks[(size, "end")] - marks[(size, "start")]} at {size}"))); + if (statingGrowth > AllowedClosureGrowth) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $$""" + Stating the program index costs {{statingMeasured}} heartbeats. + Its cost per added method grows {{statingGrowth:F2}} times, more than {{AllowedClosureGrowth}}. + """)); + Console.WriteLine(string.Create(CultureInfo.InvariantCulture, + $$""" + PASS stating the program index costs {{statingMeasured}} heartbeats. + Each added method costs {{statingGrowth:F2}} times as much between the last two sizes. + """)); +} + +// Each caller executes a direct call and returns the result. Its callee's position varies +// independently of its two-instruction body. The stepper uses the emitted method equations. +void RunCosts() +{ + var runDirectory = At("artifacts/LookupScaling/Runs"); + Directory.CreateDirectory(runDirectory); + // Allow the extra tree depth when the program grows from 16 to 256 methods. + const double AllowedRunSpread = 1.15; + int[] sizes = [16, 64, 256]; + int[] Positions(int size) => [0, size / 2 - 1, size - 1]; + string[] routes = ["rfl list", "rfl index", "kernel list", "kernel index", "stepper"]; + string Statement(int size) + { + var name = $"run{size}"; + var source = new StringBuilder(); + var positions = Positions(size); + foreach (var index in Enumerable.Range(0, size)) + { + var body = index is >= 1 and <= 3 + ? $$"""code := [.call runTarget {}, .ret], links := [(0, .direct {{positions[index - 1]}})]""" + : $$"""code := [.ldc_i4 {{index}}, .ret]"""; + source.AppendLine(CultureInfo.InvariantCulture, $$""" + /-- Method {{index}} of the {{size}}-method program. -/ + def {{name}}m{{index}} : Method := + { returnsValue := true, locals := [], {{body}} } + cil_instruction_equations {{name}}m{{index}} + """); + } + source.AppendLine(CultureInfo.InvariantCulture, $$""" + /-- The {{size}}-method program. -/ + def {{name}} : Program := + [{{string.Join(", ", Enumerable.Range(0, size).Select(index => $"{name}m{index}"))}}] + """); + source.AppendLine(CultureInfo.InvariantCulture, $$""" + /-- The index of the {{size}}-method program. -/ + def {{name}}Index : FlowIndex Method := program_index% {{name}} + /-- The number of methods in the program. -/ + def {{name}}Length : Nat := {{size}} + theorem {{name}}Length_eq : {{name}}Length = {{name}}.length := by cil_kernel_rfl + theorem {{name}}Index_eq : {{name}}Index = FlowIndex.ofList {{name}} := by cil_kernel_rfl + theorem {{name}}Index_get? (index : Nat) : {{name}}Index.get? index = {{name}}[index]? := by + rw [{{name}}Index_eq]; exact FlowIndex.get?_ofList {{name}} index + """); + var equations = new StringBuilder(); + Nethermind.LeanCIL.ProgramLookupFacts.Append(equations, name, $"{name}m", size); + var emitted = equations.ToString(); + var report = AxiomsCommand.Matches(emitted).Single(); + var members = report.Groups["members"].Value.Split(',', StringSplitOptions.TrimEntries); + string[] expected = [$"{name}Length_eq", $"{name}Index_eq", $"{name}Index_get?", + .. Enumerable.Range(0, size).Select(index => $"{name}_method{index}")]; + var exactGroup = members.Length == expected.Length && expected.ToHashSet(StringComparer.Ordinal) + .SetEquals(members); + var wrapped = report.Value.Split('\n').All(line => line.TrimEnd('\r').Length < 100); + if (!exactGroup || !wrapped || report.Index + report.Length != emitted.Length) + throw new InvalidOperationException($"Invalid axiom report for {name}: {report.Value}"); + source.AppendLine($"#heartbeat_mark \"index {size} equations\""); + source.Append(emitted.AsSpan(0, report.Index)); + source.AppendLine($"#heartbeat_mark \"index {size} start\""); + source.Append(emitted.AsSpan(report.Index)); + source.AppendLine($"#heartbeat_mark \"index {size} end\""); + foreach (var index in Enumerable.Range(0, size)) + source.AppendLine(CultureInfo.InvariantCulture, $$""" + example : {{name}}[{{index}}]? = some {{name}}m{{index}} := by cil_lookup [{{name}}] + """); + source.AppendLine($"#heartbeat_mark \"index {size} lookups\""); + foreach (var (ordinal, position) in positions.Index()) + { + var caller = ordinal + 1; + var statement = $$""" + (invoke {{name}} 5 {{caller}} [] Memory.empty).map Prod.snd = + .ok [.scalar (.i32 {{position}})] + """; + var rewrite = $"rw [invoke_eq_invokeWith {name}Index_get?]"; + string[] proofs = ["rfl", $"{rewrite}; rfl", "cil_kernel_rfl", $"{rewrite}; cil_kernel_rfl"]; + foreach (var (route, proof) in routes.Zip(proofs)) + source.AppendLine(CultureInfo.InvariantCulture, $$""" + #heartbeat_mark "{{size}} {{position}} {{route}} start" + example : {{statement}} := by {{proof}} + #heartbeat_mark "{{size}} {{position}} {{route}} end" + """); + source.AppendLine(CultureInfo.InvariantCulture, $$""" + #heartbeat_mark "{{size}} {{position}} stepper start" + theorem {{name}}_invoke{{position}} (memory : Safety.Memory) : + invoke {{name}} 5 {{caller}} [] memory = .ok (memory, [.scalar (.i32 {{position}})]) := by + cil_checked_steps + #heartbeat_mark "{{size}} {{position}} stepper end" + #print axioms {{name}}_invoke{{position}} + """); + } + source.AppendLine(CultureInfo.InvariantCulture, $$""" + run_meta do + let info <- getConstInfo ``{{name}}_invoke{{positions[^1]}} + let some proof := info.value? (allowOpaque := true) | throwError "Missing call proof" + let used := proof.getUsedConstants + for name in [``{{name}}_method3, ``{{name}}_method{{positions[^1]}}] do + unless used.contains name do throwError "The call proof did not use {name}" + """); + return source.ToString(); + } + var path = Path.Combine(runDirectory, "Runs.lean"); + File.WriteAllText(path, $$""" + import CIL + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + open CIL CIL.Safety CIL.Extraction Lean Elab Command + /-- The direct call operand shared by the three callers. -/ + def runTarget : MethodRef := + { owner := .void, name := ⟨1⟩, hasThis := false, genericArity := 0, + returnType := .void, parameters := [] } + /-- Logs the heartbeats spent so far, so two marks give the cost of the commands between them. -/ + elab "#heartbeat_mark " label:str : command => do + let count <- IO.getNumHeartbeats + logInfo m!"mark {label.getString} {count}" + {{string.Join("\n", sizes.Select(Statement))}} + + """); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException($"A measured call or lookup fact depends on sorryAx in {path}"); + var auditMarks = LookupPatterns.HeartbeatMark.Matches(output).ToDictionary( + match => (int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), match.Groups[2].Value), + match => long.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture)); + var audits = sizes.Select(size => (double)(auditMarks[(size, "end")] - auditMarks[(size, "start")])).ToArray(); + var equations = sizes.Select(size => (double)(auditMarks[(size, "start")] - auditMarks[(size, "equations")])) + .ToArray(); + var equationGrowth = ClosureGrowth([.. sizes.Select(size => size * Math.Log2(size))], equations); + var equationCosts = string.Join(", ", equations.Zip(sizes, (cost, size) => $"{cost} at {size}")); + if (equations.Any(cost => cost <= 0) || !double.IsFinite(equationGrowth) || equationGrowth > AllowedRunSpread) + throw new InvalidOperationException( + $"Method equations cost {equationCosts} heartbeats; growth {equationGrowth}"); + Console.WriteLine($"PASS method equations cost {equationCosts} heartbeats; growth {equationGrowth:F3}"); + var auditGrowth = ClosureGrowth([.. sizes.Select(size => (double)size)], audits); + var auditCosts = string.Join(", ", audits.Zip(sizes, (cost, size) => $"{cost} at {size}")); + if (audits.Any(cost => cost <= 0) || !double.IsFinite(auditGrowth) || auditGrowth > AllowedRunSpread) + throw new InvalidOperationException( + $"Method axiom reports cost {auditCosts} heartbeats; growth {auditGrowth}"); + Console.WriteLine($"PASS method axiom reports cost {auditCosts} heartbeats"); + var lookups = sizes.Select(size => (double)(auditMarks[(size, "lookups")] - auditMarks[(size, "end")])).ToArray(); + var lookupGrowth = ClosureGrowth([.. sizes.Select(size => (double)size)], lookups); + var lookupCosts = string.Join(", ", lookups.Zip(sizes, (cost, size) => $"{cost} at {size}")); + if (lookups.Any(cost => cost <= 0) || !double.IsFinite(lookupGrowth) || lookupGrowth > AllowedRunSpread) + throw new InvalidOperationException( + $"Supplied-program lookups cost {lookupCosts} heartbeats; growth {lookupGrowth}"); + Console.WriteLine($"PASS supplied-program lookups cost {lookupCosts} heartbeats; growth {lookupGrowth:F3}"); + var marks = LookupPatterns.RunMark.Matches(output).ToDictionary( + match => (int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), + int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture), match.Groups[3].Value, + match.Groups[4].Value), + match => long.Parse(match.Groups[5].Value, CultureInfo.InvariantCulture)); + if (marks.Count != 2 * routes.Length * sizes.Sum(size => Positions(size).Length)) + throw new InvalidOperationException($"Missing run costs:\n{output}"); + long Cost(int size, int position, string route) => + marks[(size, position, route, "end")] - marks[(size, position, route, "start")]; + string Measured(string route) => string.Join("; ", sizes.Select(size => string.Create( + CultureInfo.InvariantCulture, + $"{string.Join(" / ", Positions(size).Select(position => Cost(size, position, route)))} at {size}"))); + void Flat(string route) + { + var measured = sizes.SelectMany(size => Positions(size).Select(position => Cost(size, position, route))) + .ToArray(); + var spread = (double)measured.Max() / measured.Min(); + if (spread > AllowedRunSpread) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $$""" + The {{route}} route costs {{Measured(route)}} heartbeats. + The maximum/minimum ratio {{spread:F4}} exceeds {{AllowedRunSpread}}. + """)); + } + foreach (var route in new[] { "rfl index", "kernel index", "stepper" }) + Flat(route); + // A list read walks to its position, so the last method of the largest program costs most through the list. + var largest = sizes[^1]; + foreach (var (list, index) in new[] { ("rfl list", "rfl index"), ("kernel list", "kernel index") }) + if (Cost(largest, largest - 1, list) <= Cost(largest, largest - 1, index) || + Cost(largest, largest - 1, list) <= Cost(largest, 0, list)) + throw new InvalidOperationException($"The {list} route no longer walks the list: {Measured(list)}"); + foreach (var route in routes) + Console.WriteLine($"PASS the {route} route costs {Measured(route)} heartbeats at methods 1, n/2 and n"); +} + +// How many times as much each added unit costs between the last two sizes as between the first two. +static double ClosureGrowth(double[] units, double[] costs) => + (costs[2] - costs[1]) / (units[2] - units[1]) / ((costs[1] - costs[0]) / (units[1] - units[0])); + +// Four times the rows allow logarithmic index growth but rule out repeated scans of the table. +void LayoutIdentityCosts() +{ + var directory = At("artifacts/LookupScaling/LayoutIdentities"); + Directory.CreateDirectory(directory); + double previousPerRow = 0; + foreach (int count in new[] { 32, 128, 512 }) + { + var names = Enumerable.Range(0, count).Select(index => $"row \"Scope::Layout{index:D4}\""); + var header = $$""" + import CIL.Extraction.Layout + /-! Cost of validating distinct layouts with fixed-width type identities. -/ + set_option maxRecDepth 65536 + set_option maxHeartbeats 0 + namespace LayoutIdentityCost + open CIL CIL.Extraction + /-- An empty sequential struct with the given identity. -/ + def row (name : String) : RawLayout := + { identity := name, explicit := false, packing := 0, declaredSize := 0, fields := [] } + /-- The rows compared by identity. -/ + def raw := [{{string.Join(", ", names)}}] + /-- Each empty struct occupies one byte. -/ + def layouts : Array ComputedLayout := Array.replicate {{count}} ⟨⟨1, []⟩, 1⟩ + + """; + var path = Path.Combine(directory, $"Rows{count}.lean"); + long cost = DecideCost(path, header, "LayoutsValid raw layouts", "LayoutIdentityCost"); + double perRow = (double)cost / count; + Console.WriteLine($"Layout identities at {count} rows cost {cost} heartbeats ({perRow:F1} per row)"); + if (cost < KernelDecisionFloor || previousPerRow > 0 && perRow > 1.5 * previousPerRow) + throw new InvalidOperationException($"Layout validation exceeds its growth bound at {count} rows."); + previousPerRow = perRow; + } + SharedLayoutCost(directory); +} + +void SharedLayoutCost(string directory) +{ + const int rowCount = 8; + var assembly = Path.Combine(directory, "Assembly"); + Directory.CreateDirectory(assembly); + var project = Path.Combine(assembly, "SharedLayouts.csproj"); + File.WriteAllText(project, """ + + net11.0true + + """); + var rows = Enumerable.Range(0, rowCount).Select(index => + { + var field = index == 0 ? "int" : $"Row{index - 1}"; + return $"public struct Row{index} {{ public {field} Value; }}"; + }); + File.WriteAllText(Path.Combine(assembly, "SharedLayouts.cs"), $$""" + {{string.Join("\n", rows)}} + public static class SharedLayouts + { + static int First(int value) => value + 1; + static int Second(int value) => value + 2; + static int Third(int value) => value + 3; + public static int Read(Row7 row) => + Third(Second(First(row.Value.Value.Value.Value.Value.Value.Value.Value))); + } + """); + Run(["dotnet", "build", project, "-c", "Release"]); + var output = Path.Combine(directory, "Read"); + Run(Extract("extract", Path.Combine(assembly, "bin/SharedLayouts.dll"), "SharedLayouts", + "SharedLayouts::Read(Row7)", output)); + using var manifest = System.Text.Json.JsonDocument.Parse(File.ReadAllText(Path.Combine(output, "identity.json"))); + var extraction = manifest.RootElement; + if (extraction.GetProperty("methods").GetArrayLength() != 4 || + extraction.GetProperty("layoutIdentities").GetArrayLength() != rowCount) + throw new InvalidOperationException("Expected four methods and eight layout rows in the cost fixture."); + var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); + var (cost, _) = CountHeartbeats(Path.Combine(directory, "SharedLayouts.lean"), source); + const long limit = 45_000_000; + Console.WriteLine($"Four methods sharing {rowCount} layouts cost {cost} whole-file heartbeats (limit {limit})"); + if (cost > limit) throw new InvalidOperationException("Shared layout extraction exceeds its whole-file bound."); +} + +// Primitive argument storage costs at most 150,000 heartbeats per parameter, independently of unused layouts. +void ClosedArgumentsCosts() +{ + var storageDirectory = At("artifacts/LookupScaling/ClosedArguments"); + Directory.CreateDirectory(storageDirectory); + const long PerParameterLimit = 150_000; + const double LayoutGrowthLimit = 1.05; + var samples = new List<(int Parameters, int Layouts, long Cost)>(); + foreach (var parameters in new[] { 1, 4, 16 }) + foreach (var layouts in new[] { 8, 32, 128 }) + { + var path = Path.Combine(storageDirectory, $"Parameters{parameters}Layouts{layouts}.lean"); + File.WriteAllText(path, $$""" + import CIL.Extraction.Generics + import Lean + /-! Kernel cost of closed primitive arguments with unrelated aggregate layouts. -/ + set_option maxRecDepth 10000 + set_option maxHeartbeats 0 + open CIL CIL.Extraction Lean Elab Command + {{KernelCostCommand}} + /-- Every parameter is the exact scoped uint32 type. -/ + def raw : RawMethod := { + token := 0, owner := "Scaling", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 150, implementation := 0, returnType := .void, + parameters := List.replicate {{parameters}} (.atom runtimeScope "System.UInt32"), + locals := [], initLocals := false, maxStack := 1, genericParameters := [], + code := [.mk 0 42 .none] } + /-- Primitive argument storage does not inspect unrelated layouts. -/ + def layouts : List RawLayout := List.replicate {{layouts}} { + identity := "Unrelated", explicit := false, packing := 0, declaredSize := 4, + fields := [.mk .unsigned32 none] } + #kernel_cost "closed arguments {{parameters}} {{layouts}}" + closedArguments layouts raw = some + (.mk (List.replicate {{parameters}} (.field .unsigned32)) [] [.other]) + """ + "\n"); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var cost = LookupPatterns.ClosedArguments.Match(output); + if (!cost.Success) throw new InvalidOperationException($"Missing the closed argument cost:\n{output}"); + samples.Add((parameters, layouts, long.Parse(cost.Groups[1].Value, CultureInfo.InvariantCulture))); + } + foreach (var sample in samples) + { + var firstLayout = samples.Single(row => row.Parameters == sample.Parameters && row.Layouts == 8); + var firstParameter = samples.Single(row => row.Parameters == 1 && row.Layouts == sample.Layouts); + var perParameter = (double)sample.Cost / sample.Parameters; + if (sample.Cost < KernelDecisionFloor || perParameter > PerParameterLimit || + sample.Cost > firstLayout.Cost * LayoutGrowthLimit || + perParameter > firstParameter.Cost * AllowedLocalGrowth) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Closed arguments cost {perParameter:F1} heartbeats per parameter at " + + $"{sample.Parameters} parameters and {sample.Layouts} layouts; limit {PerParameterLimit}, " + + $"layout growth {LayoutGrowthLimit}, parameter growth {AllowedLocalGrowth}.")); + Console.WriteLine(string.Create(CultureInfo.InvariantCulture, + $"PASS closed arguments {sample.Parameters} parameters {sample.Layouts} layouts: " + + $"{sample.Cost} heartbeats, {perParameter:F1} per parameter")); + } +} + +// The heartbeats `decide +kernel` adds to one `add` of an int32 offset to a native method table of 16, 64 and 256 +// entries and to one load of its first and of its last entry, each table and its storage defined once outside the +// decision. The step and the load's bound read the table's entry count, not its entries, so their cost may grow by at +// most `AllowedGrowth` between sizes. Reading entry i walks i cells of the literal, so the last entry's cost beyond +// the first's may grow per index by at most `AllowedGrowth`. +void NativeTableCosts() +{ + var tableDirectory = At("artifacts/LookupScaling/NativeTableStep"); + Directory.CreateDirectory(tableDirectory); + var costs = new List<(int Entries, long Step, long First, long Last)>(); + foreach (var entries in new[] { 16, 64, 256 }) + { + var targets = string.Join(", ", Enumerable.Range(1, entries).Select(token => $"⟨\"Handlers\", {token}, []⟩")); + var path = Path.Combine(tableDirectory, $"Entries{entries}.lean"); + File.WriteAllText(path, $$""" + import CIL.Safety.Execution + set_option maxRecDepth 8192 + open CIL CIL.Safety Lean Elab Command + {{KernelCostCommand}} + namespace NativeTableStep + /-- The closed signature every entry shares. -/ + def signature : ManagedSignature := ⟨.void, []⟩ + /-- The {{entries}} supplied entries, the entry at index i holding token i + 1. -/ + def targets : Array ManagedIdentity := #[{{targets}}] + /-- A supplied native table of the entries whose storage is static binding 42. -/ + def table : MethodTable := + { signature, targets := some targets, backing := some ⟨42, 0⟩, entryCount := {{entries}} } + /-- The table with its cursor on the last entry. -/ + def lastTable : MethodTable := + { signature, targets := some targets, backing := some ⟨42, {{8 * (entries - 1)}}⟩, + entryCount := {{entries}} } + /-- Immutable static storage holding exactly the table's entries. -/ + def memory : CIL.Safety.Memory := + { allocations := fun index => if index == 0 then + some { kind := .immutableStatic, layout := ⟨{{8 * entries}}, 8, []⟩ } else none + cells := fun _ _ => ⟨0, true⟩ + views := [⟨0, 0, {{8 * entries}}, true, false⟩] + nextIdentity := 1 + staticBindings := [(42, ⟨0, 0⟩)] } + /-- A body with no code; the step reads only its profile. -/ + def body : CIL.Method := { code := [], locals := [], returnsValue := false } + /-- The frame of the step, which owns nothing. -/ + def frame : CIL.Safety.Frame := { activation := 0, locals := [], owned := [] } + #kernel_cost "table step {{entries}}" (match CIL.Safety.step body .add 0 [] frame + [.scalar (.i32 8), .methodTable table] memory with + | .ok (.next _ [.methodTable moved] _ _) => moved.backing + | _ => none) = some ⟨42, 8⟩ + #kernel_cost "table load first {{entries}}" (match loadNativeTable table memory with + | .ok ⟨_, some ⟨_, token, _⟩⟩ => token == 1 + | _ => false) = true + #kernel_cost "table load last {{entries}}" (match loadNativeTable lastTable memory with + | .ok ⟨_, some ⟨_, token, _⟩⟩ => token == {{entries}} + | _ => false) = true + end NativeTableStep + """ + "\n"); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + long Measured(Regex pattern, string row) + { + var cost = pattern.Match(output); + if (!cost.Success) throw new InvalidOperationException($"Missing the native table {row} cost:\n{output}"); + return long.Parse(cost.Groups[1].Value, CultureInfo.InvariantCulture); + } + costs.Add((entries, Measured(LookupPatterns.NativeTableStep, "step"), + Measured(LookupPatterns.NativeTableLoadFirst, "first load"), + Measured(LookupPatterns.NativeTableLoadLast, "last load"))); + } + RequireConstant("one native table step", costs.Select(sample => (sample.Entries, sample.Step)).ToList()); + RequireConstant("one load of a native table's first entry", + costs.Select(sample => (sample.Entries, sample.First)).ToList()); + // The last entry sits at index entries - 1, so its cost beyond the first entry's is divided by that index. + var perIndex = costs.Select(sample => (sample.Entries, sample.Last, + PerIndex: (sample.Last - sample.First) / (sample.Entries - 1))).ToList(); + var walked = string.Join("; ", perIndex.Select(sample => + $"{sample.Last} heartbeats, {sample.PerIndex} per index, at {sample.Entries}")); + if (costs.Any(sample => sample.Last < KernelDecisionFloor) || + perIndex.Skip(1).Any(sample => sample.PerIndex > perIndex[0].PerIndex * AllowedGrowth)) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Loading a native table's last entry costs {walked}; the limit is {AllowedGrowth} times the cost per " + + $"index at {costs[0].Entries} entries.")); + Console.WriteLine($"PASS loading a native table's last entry costs {walked} entries"); +} + +// Fails unless every cost is a real decision and none exceeds `AllowedGrowth` times the cost at the smallest size. +void RequireConstant(string row, List<(int Entries, long Cost)> costs) +{ + var measured = string.Join("; ", costs.Select(sample => $"{sample.Cost} heartbeats at {sample.Entries} entries")); + if (costs.Any(sample => sample.Cost < KernelDecisionFloor) || + costs.Skip(1).Any(sample => sample.Cost > costs[0].Cost * AllowedGrowth)) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Deciding {row} costs {measured}; the limit is {AllowedGrowth} times the cost at " + + $"{costs[0].Entries} entries.")); + Console.WriteLine($"PASS deciding {row} costs {measured}"); +} + +// The heartbeats `decide +kernel` adds to the same statement closed with `sorry`, for equal and unequal type +// identities whose names have 20 and 80 characters and share all but their last character. Each statement is +// distinct, so no decision reuses the auxiliary lemma of another. +Dictionary TypeDecisionCosts() +{ + const string Prefix = "System.Runtime.InteropServices.Marshalling.Collections.Generic.Dictionary.Keys."; + string Type(int length, char last) + { + var name = Prefix[..(length - 1)] + last; + return $$"""({ assembly := runtime, typeNamespace := name!"System", name := name!"{{name}}" } : """ + + "TypeIdentity)"; + } + var statements = new[] { 20, 80 }.SelectMany(length => new[] + { + $"#kernel_cost \"equal {length}\" {Type(length, '0')} = {Type(length, '0')}", + $"#kernel_cost \"unequal {length}\" Not ({Type(length, '1')} = {Type(length, '2')})" + }); + var path = Path.Combine(directory, "TypeIdentityEquality.lean"); + File.WriteAllText(path, """ + import CIL.Identity + open CIL Lean Elab Command + /-- The core library assembly of .NET 11. -/ + def runtime : AssemblyName := + { name := name!"System.Runtime", version := { major := 11, minor := 0, build := 0, revision := 0 }, + culture := name!"", publicKeyToken := some 0xb03f5f7f11d50a3a } + + """ + KernelCostCommand + "\n" + string.Join("\n", statements) + "\n"); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var costs = LookupPatterns.TypeDecision.Matches(output).ToDictionary( + match => match.Groups[1].Value, match => long.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)); + if (costs.Count != 4) throw new InvalidOperationException("Missing type identity decision costs:\n" + output); + return costs; +} +if (!instructionEquationsOnly) +{ + var decisions = TypeDecisionCosts(); + foreach (var outcome in new[] { "equal", "unequal" }) + { + long shortName = decisions[$"{outcome} 20"], longName = decisions[$"{outcome} 80"]; + var measured = $"{shortName} heartbeats at 20 characters and {longName} at 80"; + if (Math.Min(shortName, longName) < KernelDecisionFloor || Math.Max(shortName, longName) > TypeDecisionLimit || + longName > shortName * AllowedNameGrowth) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $""" + Deciding {outcome} type identities costs {measured}; the limits are {TypeDecisionLimit} heartbeats + and {AllowedNameGrowth} times as many at 80 characters. + """).ReplaceLineEndings(" ")); + Console.WriteLine($"PASS deciding {outcome} type identities costs {measured}"); + } + MetadataNameCosts(); + ModelTableCosts(); + PlatformTableCosts(); + ImportLinkCosts(); +} + +// Each input quadruples the number of distinct exports, links and call instructions together. +void ImportLinkCosts() +{ + int[] sizes = [12, 48, 192]; + var directory = At("artifacts/LookupScaling/ImportLinks"); + Directory.CreateDirectory(directory); + var path = Path.Combine(directory, "ImportLinks.lean"); + var samples = sizes.Select(size => $"""#kernel_cost "import links {size}" (checkLinks {size} = true)"""); + File.WriteAllText(path, $$""" + import CIL.Extraction.BodyDecoding + open CIL CIL.Extraction Lean Elab Command + {{KernelCostCommand}} + /-- Distinct static exports with a scalar call shape. -/ + def slot (index : Nat) : ImportSlot := + { identity := toString index, closureDigest := "", parameterCount := 0, + hasThis := false, returnsValue := false } + /-- Every direct call uses its own export at the matching instruction index. -/ + def checkLinks (count : Nat) : Bool := + let indices := List.range count + let imports := ImportSlotIndex.ofList (indices.map slot) + let bound := imports.forLinks 1 (indices.map fun i => (i, .direct (i + 1))) + let sites := indices.map fun i => + let method := { MethodRef.arity 0 with hasThis := false } + let site : CallSite := + { method, declared := method.signature 0, target := .external (toString i), + typeArguments := [], parameterCount := 0, returnsValue := false } + ({ offset := i, opcode := 0x28, operand := .call site } : RawInstruction) + let names := bound.calls.foldl (fun acc (_, entry) => acc.insert entry.identity entry) + ({} : Std.TreeMap String ImportSlot) + bound.calls.length == count && bound.checkUses names sites + {{string.Join("\n", samples)}} + """); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var costs = LookupPatterns.ImportLinkCost.Matches(output).ToDictionary( + match => int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), + match => long.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)); + if (costs.Count != sizes.Length || costs.Values.Any(cost => cost < KernelDecisionFloor)) + throw new InvalidOperationException($"""Missing import link costs: {output}"""); + var measured = string.Join(", ", sizes.Select(size => $"""{costs[size]} at {size}""")); + if (sizes.Zip(sizes.Skip(1)).Any(pair => costs[pair.Second] > 2 * costs[pair.First] * pair.Second / pair.First)) + throw new InvalidOperationException($"""Import lookup cost per call more than doubled: {measured}"""); + Console.WriteLine($"""PASS import link checks cost {measured} heartbeats"""); +} + +// Compare each lookup at three table sizes, including identical names scoped by different assemblies. +void PlatformTableCosts() +{ + var tableDirectory = At("artifacts/LookupScaling/PlatformTables"); + Directory.CreateDirectory(tableDirectory); + int[] sizes = [16, 128, 1024]; + string[] sites = ["reference", "enum", "pair", "chain", "constant", "base", "shared", "scoped", "missing"]; + static string Bytes(string text) => $"""[{string.Join(", ", Encoding.UTF8.GetBytes(text))}]"""; + static string Parts(string space, string name) => $"[{Bytes(space)}, {Bytes(name)}]"; + // Table names in the literal form an extraction writes, so the literal costs what an extraction's does. + static string Literal(string text) => ByteLiterals.Of(Encoding.UTF8.GetBytes(text)); + // The text of `runtimeScopeBytes`, which the checks pass as the scope of the types they read. + const string runtimeScope = "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"; + static string Definition(string space, string name, string kind, string scope = runtimeScope) => + $$"""{ scope := {{Literal(scope)}}, parts := [{{Literal(space)}}, {{Literal(name)}}], kind := {{kind}} }"""; + // The depth an extraction sets for its list, so a list it would refuse to compile fails here. + static string Depth(int count) => $"set_option maxRecDepth {PlatformDefinitions.RecursionDepth(count)} in"; + string[] relied = + [ + Definition("System", "Console", ".classType"), Definition("System", "Object", ".objectRoot"), + Definition("System", "ValueType", ".valueTypeRoot"), Definition("System", "ValueTuple`2", ".valuePair"), + Definition("System", "BitConverter", ".byteOrder"), Definition("", "Number", ".enumType 8"), + ]; + string Table(int size) => $$""" + #elab_cost "platform elaboration {{size}}" + {{Depth(size + relied.Length)}} + /-- The definitions the sites rely on among {{size}} others of distinct names. -/ + def table{{size}} : PlatformTable := platform_table% [ + {{string.Join(",\n ", relied.Concat(Enumerable.Range(0, size).Select(index => + Definition("Filler", $"Type{index:D5}", ".classType"))))}}] + {{Depth(size)}} + /-- {{size}} classes named `Enumerator`, each in a namespace of its own. -/ + def shared{{size}} : PlatformTable := platform_table% [ + {{string.Join(",\n ", Enumerable.Range(0, size).Select(index => + Definition($"Shared{index:D5}", "Enumerator", ".classType")))}}] + {{Depth(size)}} + /-- Identical names alternate between class and value storage in distinct scopes. -/ + def scopes{{size}} : PlatformTable := platform_table% [ + {{string.Join(",\n ", Enumerable.Range(0, size).Select(index => + Definition("Shared", "Enumerator", index % 2 == 0 ? ".classType" : ".valueType", $"Scope{index:D5}")))}}] + /-- The environment of no slices under the table. -/ + def env{{size}} : MetadataEnv := MetadataEnv.ofSlices [] (platform := table{{size}}) + #kernel_cost "platform reference {{size}}" + ((env{{size}}.externalNamed 0 runtimeScopeBytes (bytesOf "System.Console") + {{Parts("System", "Console")}} (some false)).isOk = true) + #kernel_cost "platform enum {{size}}" + ((do + let head <- env{{size}}.externalNamed 0 runtimeScopeBytes (bytesOf "Number") + {{Parts("", "Number")}} (some true) + let frame : Frame := + { corlib := some runtimeScopeBytes, anchor := runtimeScopeBytes, arity := 0, ownerArity := 0 } + return frame.admits (.named head) + (.enum (runtimeScope ++ "::Number") (.atom runtimeScope "System.Int32"))).toOption = some true) + #kernel_cost "platform pair {{size}}" + ((env{{size}}.platformField 0 + { owner := .genericInst (named "System.ValueTuple`2" {{Parts("System", "ValueTuple`2")}}) + (.cons (.primitive 8) (.cons (.primitive 10) .nil)), + name := bytesOf "Item2", declared := .var 1, definition := none } + (.primitive 10) false false).isOk = true) + #kernel_cost "platform chain {{size}}" + ((env{{size}}.chainRoot 0 0 runtimeScopeBytes {{Parts("System", "Object")}} true).isOk = true) + #kernel_cost "platform constant {{size}}" + ((env{{size}}.platformConstant 0 + { owner := .named (named "System.BitConverter" {{Parts("System", "BitConverter")}}), + name := bytesOf "IsLittleEndian", declared := .primitive 2, definition := none } + (.primitive 2) true true).isOk = true) + #kernel_cost "platform base {{size}}" + ((table{{size}}.require 0 runtimeScopeBytes {{Parts("System", "ValueType")}} + (baseFits true {{Parts("System", "ValueType")}})).isOk = true) + #kernel_cost "platform shared {{size}}" + (shared{{size}}.kindOf runtimeScopeBytes {{Parts($"Shared{size / 2:D5}", "Enumerator")}} = + some .classType) + #kernel_cost "platform scoped {{size}}" + (scopes{{size}}.kindOf {{Bytes($"Scope{size - 2:D5}")}} {{Parts("Shared", "Enumerator")}} = + some .classType /\ + scopes{{size}}.kindOf {{Bytes($"Scope{size - 1:D5}")}} {{Parts("Shared", "Enumerator")}} = + some .valueType /\ + scopes{{size}}.kindOf {{Bytes($"Scope{size:D5}")}} {{Parts("Shared", "Enumerator")}} = none) + #kernel_cost "platform missing {{size}}" + (table{{size}}.kindOf runtimeScopeBytes {{Parts("System", "Absent")}} = none) + """; + var path = Path.Combine(tableDirectory, "PlatformTables.lean"); + File.WriteAllText(path, $$""" + import CIL.Extraction.MetadataBinding + open CIL CIL.Extraction Lean Elab Command + {{KernelCostCommand}} + /-- Logs what elaborating a command costs. -/ + elab "#elab_cost " label:str command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + logInfo m!"{label.getString} {after - before} heartbeats" + /-- A runtime type outside the carried slices, as a reference resolves it. -/ + def named (name : String) (parts : List (List Nat)) : NamedType := + { scope := runtimeScopeBytes, name := bytesOf name, parts, kind := .external (some false), + pinned := false } + {{string.Join("\n", sizes.Select(Table))}} + """); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var costs = LookupPatterns.PlatformTableCost.Matches(output).ToDictionary( + match => (match.Groups[1].Value, int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)), + match => long.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture)); + if (costs.Count != (sites.Length + 1) * sizes.Length || + sites.Any(site => sizes.Any(size => costs[(site, size)] < KernelDecisionFloor))) + throw new InvalidOperationException($"""Missing platform table costs: {output}"""); + var perDefinition = sizes.Select(size => (double)costs[("elaboration", size)] / (size + relied.Length)).ToArray(); + var rates = string.Join(", ", sizes.Zip(perDefinition).Select(pair => + string.Create(CultureInfo.InvariantCulture, $"{pair.Second:F0} at {pair.First}"))); + if (perDefinition.Zip(perDefinition.Skip(1)).Any(pair => pair.Second > pair.First * AllowedGrowth)) + throw new InvalidOperationException($"Platform elaboration exceeds {AllowedGrowth} times: {rates}"); + Console.WriteLine($"Elaborating a platform table costs {rates} heartbeats per definition"); + foreach (var site in sites) + { + var measured = string.Join(", ", sizes.Select(size => + string.Create(CultureInfo.InvariantCulture, $"{costs[(site, size)]} at {size}"))); + var grows = sizes.Zip(sizes.Skip(1)) + .Any(pair => costs[(site, pair.Second)] > costs[(site, pair.First)] * AllowedGrowth); + if (grows) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"""Platform {site} costs {measured} heartbeats, exceeding the growth limit of {AllowedGrowth}.""")); + Console.WriteLine($"PASS the platform {site} check costs {measured} heartbeats at that many definitions"); + } +} + +// The heartbeats `decide +kernel` adds to the name clause of the call and field operand checks, which reads a typed +// name back and compares its bytes with the row's. Names of 20, 80 and 320 bytes share all but their last byte. The +// clause reads one byte per step, so the cost per byte may grow at most `AllowedGrowth` times from one length to the +// next. +void MetadataNameCosts() +{ + var nameDirectory = At("artifacts/LookupScaling/MetadataNames"); + Directory.CreateDirectory(nameDirectory); + const string Prefix = "WriteLineToTheConsoleOfTheRuntimeEachValueThatTheCallerPassesAsAnIntegerOfThirtyTwoBits"; + int[] lengths = [20, 80, 320]; + string Statement(int length) + { + var name = string.Concat(Enumerable.Repeat(Prefix, length / Prefix.Length + 1))[..(length - 1)] + "0"; + var bytes = string.Join(", ", Encoding.UTF8.GetBytes(name)); + return $$""" + /-- The {{length}} bytes of a row name. -/ + def bytes{{length}} : List Nat := [{{bytes}}] + /-- The metadata name of those bytes. -/ + def name{{length}} : MetadataName := name!"{{name}}" + #kernel_cost "name spells {{length}}" (name{{length}}.spells bytes{{length}} = true) + """; + } + var path = Path.Combine(nameDirectory, "MetadataNames.lean"); + File.WriteAllText(path, $$""" + import CIL.Identity + open CIL Lean Elab Command + {{KernelCostCommand}} + {{string.Join("\n", lengths.Select(Statement))}} + """); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var costs = LookupPatterns.MetadataNameCost.Matches(output).ToDictionary( + match => int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), + match => long.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)); + if (costs.Count != lengths.Length) + throw new InvalidOperationException("Missing metadata name costs:\n" + output); + double PerByte(int length) => (double)costs[length] / length; + var measured = string.Join(", ", lengths.Select(length => + string.Create(CultureInfo.InvariantCulture, $"{costs[length]} at {length} bytes"))); + var flat = lengths.Zip(lengths.Skip(1)).All(pair => PerByte(pair.Second) <= PerByte(pair.First) * AllowedGrowth); + if (lengths.Min(length => costs[length]) < KernelDecisionFloor || !flat) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"A metadata name spells its bytes at {measured} heartbeats; per byte, the limit is " + + $"{AllowedGrowth} times the cost at the shorter length.")); + Console.WriteLine($"PASS a metadata name spells its bytes at {measured} heartbeats"); +} + +// `runtimeModel` searches a literal tree of model entries by name code and compares one method reference at the leaf, +// so a lookup in 256 entries costs the four extra tree levels over 16 and no more, whether it queries the first, the +// middle or the last entry; a scan of the entries would make the last cost more. The agreement check runs one +// lookup per live call site, so its cost per site stays flat over 8, 32 and 128 sites. Each site calls a distinct +// method, so the kernel cannot reuse one site's lookup for another. +void ModelTableCosts() +{ + var tableDirectory = At("artifacts/LookupScaling/ModelTables"); + Directory.CreateDirectory(tableDirectory); + const int modelLookupDepth = 75; + const int layoutLookupDepth = 210; + int[] tableSizes = [16, 256]; + int[] siteCounts = [8, 32, 128]; + string[] positions = ["first", "middle", "last"]; + int Queried(string position, int size) => position switch { "first" => 0, "middle" => size / 2, _ => size - 1 }; + string Entries(int size) => string.Join(",\n ", Enumerable.Range(0, size).Select(index => + $$""" + ModelEntry.exact .call (method name!"Model{{index:D4}}") { convention := 0 } + (.memory (.divRemOut {{index}})) + """)); + string RawRows(int size) => string.Join(", ", Enumerable.Range(0, size - 1).Select(i => $"row \"L{i}\" 1")); + string Table(int size) => $$""" + /-- {{size}} modelled methods. -/ + def entries{{size}} : List ModelEntry := [ + {{Entries(size)}}] + /-- Their literal table. -/ + def table{{size}} : ModelTable := model_table% entries{{size}} + {{string.Join("\n", positions.Select(position => Lookup(position, size)))}} + /-- The target follows every unrelated layout in this table. -/ + def layouts{{size}} : LayoutEnvironment := { + rawLayouts := [{{RawRows(size)}}, row "Target" 8] + layouts := #[{{string.Join(", ", Enumerable.Repeat("layout 1", size - 1))}}, layout 8] } + set_option maxRecDepth {{layoutLookupDepth}} in + example : runtimeModel layouts{{size}} .call (sizeRef {{size - 1}}) = + some (.constant 8) := by rfl + """; + string Lookup(string position, int size) + { + var index = Queried(position, size); + var claim = $$""" + table{{size}}.find {} .call (method name!"Model{{index:D4}}") = some (.memory (.divRemOut {{index}})) + """; + return $$""" + set_option maxRecDepth {{modelLookupDepth}} in + example : {{claim}} := by rfl + #kernel_cost "model lookup {{position}} {{size}}" ({{claim}}) + """; + } + string Call(int index) => $$""".call (method name!"Model{{index:D4}}") {}"""; + string Site(int index) => $$""" + ⟨{{index}}, 40, .call { method := method name!"Model{{index:D4}}", declared := { convention := 0 }, + target := .external "", typeArguments := [], parameterCount := 0, returnsValue := false }⟩ + """.ReplaceLineEndings(" "); + string Sites(int count) => $$""" + /-- {{count}} live calls of distinct modelled methods, the call at index i to method i. -/ + def raw{{count}} : RawMethod := { shell with code := [ + {{string.Join(",\n ", Enumerable.Range(0, count).Select(Site))}}] } + /-- Each site names the next method in the list, wrapping at its end. -/ + def code{{count}} : List Instruction := + [{{string.Join(", ", Enumerable.Range(0, count).Select(index => Call((index + 1) % count)))}}] + /-- The same sites keeping the calls to their own methods. -/ + def calls{{count}} : List Instruction := + [{{string.Join(", ", Enumerable.Range(0, count).Select(Call))}}] + #kernel_cost "model calls {{count}}" + (ModelsAgree catalog {} .scalar raw{{count}} (List.replicate {{count}} true) calls{{count}}) + #kernel_cost "model agreement {{count}}" + (all3 (fun raw live emitted => !live || !catalog.siteAgrees {} .scalar raw emitted) + raw{{count}}.code (List.replicate {{count}} true) code{{count}} = true) + """; + var path = Path.Combine(tableDirectory, "ModelTables.lean"); + File.WriteAllText(path, $$""" + import CIL.Extraction.CallAgreement + open CIL CIL.Extraction Lean Elab Command + {{KernelCostCommand}} + /-- A static method of no parameters named `name`. -/ + def method (name : MetadataName) : MethodRef := + { owner := .atom "Tests" "Models", name, hasThis := false, genericArity := 0, returnType := .void, + parameters := [] } + /-- Only the final position in each layout table carries the eight-byte target. -/ + def sizeRef (position : Nat) : MethodRef := + { owner := unsafeOwner, name := name!"SizeOf", hasThis := false, genericArity := 1, + parameters := [], returnType := .atom runtimeScope "System.Int32", + methodArguments := [.aggregate "Target" position] } + /-- The raw row binds a size query to its named type. -/ + def row (identity : String) (size : Nat) : RawLayout := + { identity, fields := [], explicit := false, packing := 0, declaredSize := size } + /-- The computed row supplies the byte count read by the size query. -/ + def layout (size : Nat) : ComputedLayout := .mk (.mk size []) 1 + {{string.Join("\n", tableSizes.Select(Table))}} + /-- The larger table of modelled calls. -/ + def catalog : ModelCatalog where + table := table256 + /-- A method shell whose code each fixture supplies. -/ + def shell : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := true, maxStack := 1, genericParameters := [], code := [] } + /-- Interleave the three sizes so each median samples the same period of machine load. -/ + def checkScaling (label : String) (limit : Nat) (measure : Nat → Nat → IO Nat) : IO Unit := do + let sizes := #[512, 1024, 2048] + let timers := (sizes.map measure).toList.zipIdx + let mut samples : Array (Array Nat) := Array.replicate sizes.size #[] + for trial in [:9] do + for (timer, index) in timers do + let elapsed <- timer trial + samples := samples.modify index (·.push elapsed) + let mut previous := 0 + for (count, values) in sizes.toList.zip samples.toList do + let elapsed := values.qsortOrd[4]! + IO.println s!"{label} {count}: {elapsed} ns" + if elapsed == 0 || (previous != 0 && elapsed * 100 > previous * limit) then + throw (IO.userError s!"{label} exceeded {limit}% of the preceding time per doubling") + previous := elapsed + /-- Repeated DivRem queries against two identical oversized rows. -/ + def timeOversizedTuple (count : Nat) : Nat → IO Nat := + let identity := integerPairIdentity "System.UInt64" + let row : RawLayout := { + identity, explicit := false, packing := 0, declaredSize := 0, + fields := List.replicate count ⟨.unsigned64, none⟩ } + let layout : ComputedLayout := ⟨⟨16, [⟨0, 8, false⟩, ⟨8, 8, false⟩]⟩, 8⟩ + let environment := LayoutEnvironment.callLayouts { + rawLayouts := [row, row], layouts := #[layout, layout] } + fun trial => do + let start <- IO.monoNanosNow + for i in [:count] do + let pair := TypeExpr.aggregate identity ((i + trial) % 2) + if integerPairResult environment pair "System.UInt64" 8 then + throw (IO.userError "DivRem accepted an oversized tuple") + return (← IO.monoNanosNow) - start + #eval checkScaling "DivRem oversized tuple queries" 250 timeOversizedTuple + /-- Decide the production relation on the supplied body. -/ + @[noinline] def agreedNops (raw : RawMethod) (reachable : List Bool) (body : Method) : Bool := + decide (RuntimeModelsAgree raw reachable body) + /-- Prepare the body, reachability and links before timing a walk. -/ + def timeNops (live : Bool) (count : Nat) : Nat → IO Nat := + let raw := { shell with code := List.replicate count (.mk 0 0 .none) } + let links := if live then [] else (List.range count).reverse.map fun pc => (pc, Link.direct pc) + let body : Method := { code := List.replicate count .nop, links, locals := [], returnsValue := false } + let reachable := List.replicate count live + fun trial => do + let start <- IO.monoNanosNow + unless agreedNops { raw with token := trial } reachable body do + throw (IO.userError "RuntimeModelsAgree refused nops") + return (← IO.monoNanosNow) - start + #eval checkScaling "RuntimeModelsAgree live nops" 230 (timeNops true) + #eval checkScaling "RuntimeModelsAgree dead nops with reversed links" 230 (timeNops false) + {{string.Join("\n", siteCounts.Select(Sites))}} + """); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + foreach (var line in output.Split('\n').Where(line => + line.StartsWith("RuntimeModelsAgree ", StringComparison.Ordinal) || + line.StartsWith("DivRem ", StringComparison.Ordinal))) + Console.WriteLine(line.TrimEnd()); + var costs = LookupPatterns.ModelTableCost.Matches(output).ToDictionary( + match => (match.Groups[1].Value, int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)), + match => long.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture)); + if (costs.Count != positions.Length * tableSizes.Length + 2 * siteCounts.Length || + costs.Values.Min() < KernelDecisionFloor) + throw new InvalidOperationException("Missing model table costs:\n" + output); + foreach (var position in positions) + { + var lookups = tableSizes.Select(size => costs[("lookup " + position, size)]).ToArray(); + var lookupMeasure = string.Join(", ", tableSizes.Select(size => + string.Create(CultureInfo.InvariantCulture, $"{costs[("lookup " + position, size)]} at {size} entries"))); + if (lookups[1] > lookups[0] * AllowedModelLookupGrowth) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"A model lookup of the {position} entry costs {lookupMeasure} heartbeats; the limit is " + + $"{AllowedModelLookupGrowth} times the cost in the smaller table.")); + Console.WriteLine($"PASS a model lookup of the {position} entry costs {lookupMeasure} heartbeats"); + } + double PerSite(int count) => (double)costs[("agreement", count)] / count; + var agreementMeasure = string.Join(", ", siteCounts.Select(count => + string.Create(CultureInfo.InvariantCulture, $"{costs[("agreement", count)]} at {count} sites"))); + if (!siteCounts.Zip(siteCounts.Skip(1)).All(pair => PerSite(pair.Second) <= PerSite(pair.First) * AllowedGrowth)) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"The model agreement check costs {agreementMeasure} heartbeats; per site, the limit is {AllowedGrowth} " + + $"times the cost at the smaller count.")); + Console.WriteLine($"PASS the model agreement check costs {agreementMeasure} heartbeats"); + var callCosts = siteCounts.Select(count => (Count: count, Total: costs[("calls", count)])).ToArray(); + var callMeasure = string.Join(", ", callCosts.Select(sample => + FormattableString.Invariant($"{sample.Total} at {sample.Count} calls"))); + if (callCosts.Zip(callCosts.Skip(1)).Any(pair => + pair.Second.Total * pair.First.Count > pair.First.Total * pair.Second.Count)) + throw new InvalidOperationException($"Modelled call heartbeats per site increased ({callMeasure})."); + Console.WriteLine($"PASS modelled call heartbeats per site are nonincreasing ({callMeasure})"); +} + var source = Path.Combine(directory, "Assembly"); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Lookups.csproj"), """ - net10.0true + net11.0true """); string Method(string name, int statements) => $"public static ulong {name}(ulong x, ulong y) {{ ulong word = x; " + string.Concat(Enumerable.Repeat("word = word * y + x; ", statements)) + "return word; } "; +// A body with a call at every statement, to measure what its link table and link facts cost. +string Calling(string name, int calls) => + $"public static ulong {name}(ulong x, ulong y) {{ ulong word = x; " + + string.Concat(Enumerable.Repeat("word = Step(word, y); ", calls)) + "return word; } "; +// A long body with two calls, where the link table stays the same size as the body grows. +string Sparse(string name, int statements) => + $"public static ulong {name}(ulong x, ulong y) {{ ulong word = Step(x, y); " + + string.Concat(Enumerable.Repeat("word = word * y + x; ", statements)) + + "word = Step(word, y); return word; } "; File.WriteAllText(Path.Combine(source, "Lookups.cs"), "public static class Lookups { " + - Method("Short", ShortStatements) + Method("Long", LongStatements) + "}"); + Method("Short", ShortStatements) + Method("Middle", MiddleStatements) + Method("Long", LongStatements) + + "public static ulong Step(ulong a, ulong b) { return a * b + a; } " + + Calling("Few", FewCalls) + Calling("Many", ManyCalls) + + Sparse("SparseShort", SparseShortStatements) + Sparse("SparseLong", SparseLongStatements) + "}"); Run(["dotnet", "build", Path.Combine(source, "Lookups.csproj"), "-c", "Release"]); var assembly = Path.Combine(source, "bin/Lookups.dll"); -const string Report = "\n#eval show IO Nat from IO.getNumHeartbeats\n"; +// The heartbeats `text` costs, imports included. +long Heartbeats(string path, string text, bool equations = true) => Reported(path, text, equations).Count; -// Heartbeats count the elaborator's and the kernel's work on one thread, so they repeat exactly -// where compile time varies with the machine's load. -long Heartbeats(string path, string text, bool equations = true) +// The heartbeats `text` costs, imports included, and Lean's output. +(long Count, string Output) Reported(string path, string text, bool equations = true) { - File.WriteAllText(path, text + Report); - var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); - if (output.Contains("sorryAx", StringComparison.Ordinal)) - throw new InvalidOperationException("Untrusted lookup equations: " + path); - var count = Regex.Match(output, @"(?m)^([0-9]+)\r?$"); - if (!count.Success) throw new InvalidOperationException("Missing heartbeat count:\n" + output); + var (count, output) = CountHeartbeats(path, text); // Each file reports the last equation's axioms once; a report per equation would walk the body each time. - if (equations && Regex.Matches(output, @"_instruction[0-9]+' depends on axioms: \[propext\]").Count != 1) + if (equations && LookupPatterns.EquationAxioms.Count(output) != 1) throw new InvalidOperationException("Instruction equations must depend on propext alone:\n" + output); - return long.Parse(count.Groups[1].Value, CultureInfo.InvariantCulture); + return (count, output); +} + +// The heartbeats of each part of one file after the first, each read between two counts and less the cost of reading +// a count. Three counts follow the first part, so every part is read after the evaluator has run. +long[] PartHeartbeats(string path, IReadOnlyList parts) +{ + const string Reading = "#eval show IO Nat from IO.getNumHeartbeats\n"; + var text = new StringBuilder(parts[0]).Append(Reading).Append(Reading).Append(Reading); + foreach (var part in parts.Skip(1)) text.Append(part).Append(Reading); + File.WriteAllText(path, text.ToString()); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + if (output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException("A measured file depends on sorryAx: " + path); + long[] readings = [.. HeartbeatCount.Matches(output) + .Select(match => long.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture))]; + if (readings.Length != parts.Count + 2) + throw new InvalidOperationException($"Expected {parts.Count + 2} heartbeat counts in {path}:\n{output}"); + var reading = readings[2] - readings[1]; + return [.. Enumerable.Range(3, parts.Count - 1).Select(index => readings[index] - readings[index - 1] - reading)]; +} + +// The kernel heartbeats of deciding `statement` after `header`, less those of stating it, read in one file with the +// statement first. The file closes namespace `scope`, which the header opens. +long DecideCost(string path, string header, string statement, string scope) +{ + var costs = PartHeartbeats(path, [header, $"example : Prop := {statement}\n", + $"theorem valid : {statement} := by decide +kernel\n#print axioms valid\n", $"end {scope}\n"]); + return costs[1] - costs[0]; } // The last pc is the farthest from the head of the code list. Its equation must keep the statement // stepping tactics rewrite with, depend on `propext` alone, and still be fetched by `cil_code`. -long Checked(string path, string text, string binder, string method, string equation, int last) +(long Count, string Output) Checked(string path, string text, string binder, string method, string equation, + int last, string verifyAfter = "") { - var statement = $"example {binder}: {method}.code[{last}]? = some CIL.Op.ret"; - return Heartbeats(path, text + $""" + var statement = $"example {binder}: {method}.code[{last}]? = some CIL.Instruction.ret"; + var checks = $""" {statement} := {equation} {statement} := by simp only [cil_code] - """); + """; + // CountHeartbeats reads the first count. The independent equality is checked outside the measured row. + if (verifyAfter.Length > 0) + checks = $""" + {checks} + #eval show IO Nat from IO.getNumHeartbeats + {verifyAfter} + """; + return Reported(path, $"""{text}{checks}"""); +} + +// The extraction with its call check and its method evidence theorem each reporting the heartbeats it costs alone. +string WithEvidenceCosts(string extracted) +{ + const string Command = """ + /-- Elaboration and kernel checking of one command, labelled in the report. -/ + elab "#evidence_cost " label:str " in " command:command : command => do + let before <- IO.getNumHeartbeats + Lean.Elab.Command.elabCommand command + let after <- IO.getNumHeartbeats + Lean.logInfo m!"evidence {label.getString} {after - before} heartbeats" + + """; + var namespaces = LookupPatterns.ExtractedNamespace.Matches(extracted).Count; + var calls = LookupPatterns.CallsTheorem.Matches(extracted).Count; + var evidence = LookupPatterns.EvidenceTheorem.Matches(extracted).Count; + if (namespaces != 1 || calls != 1 || evidence != 1) + throw new InvalidOperationException("Missing the namespace, call check or evidence theorem of method0."); + var labelled = LookupPatterns.ExtractedNamespace.Replace(extracted, match => Command + match.Value); + labelled = LookupPatterns.CallsTheorem.Replace(labelled, + match => "#evidence_cost \"calls\" in\n" + match.Value); + return LookupPatterns.EvidenceTheorem.Replace(labelled, + match => "#evidence_cost \"assembly\" in\n" + match.Value); +} + +// The heartbeats of the call check and of the evidence theorem that `WithEvidenceCosts` reports. +(long Calls, long Assembly) EvidenceCosts(string output) +{ + var costs = LookupPatterns.EvidenceCost.Matches(output).ToDictionary(match => match.Groups[1].Value, + match => long.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture)); + return costs.Count == 2 && costs.TryGetValue("calls", out var calls) && + costs.TryGetValue("assembly", out var assembled) + ? (calls, assembled) + : throw new InvalidOperationException("Missing the call check and evidence theorem costs:\n" + output); } // The three places `cil_instruction_equations` states lookup equations for one method: its extraction, a // hand-written body with a parameter, and the shared template of the extraction. -(int Instructions, long[] Heartbeats) Measure(string name) +(int Instructions, long[] Heartbeats, (long Calls, long Assembly) Evidence) Measure(string name) { var output = Path.Combine(directory, name); Run(Extract("extract", assembly, "Lookups", $"Lookups::{name}(ulong,ulong)", output)); var extracted = File.ReadAllText(Path.Combine(output, "Extracted.lean")); - const string Header = "def method0 : Method := { profile := profile,"; - // The definition lists one instruction per line after its header. - var definition = Regex.Match(extracted, @"(?m)^" + Regex.Escape(Header) + @"[^\r\n]*(\r?\n [^\r\n]*)+"); - var depth = Regex.Match(extracted, @"(?m)^set_option maxRecDepth [0-9]+"); - if (!definition.Success || !depth.Success || - !Regex.IsMatch(extracted, @"(?m)^cil_instruction_equations method0\r?$")) - throw new InvalidOperationException($"Missing the definition, equations or recursion depth of {name}."); - var instructions = definition.Groups[1].Captures.Count; + var shared = Path.Combine(output, "Shared"); + Run(["dotnet", ExtractorPath, "templates", shared, output]); + var templates = File.ReadAllText(Path.Combine(shared, "Templates.lean")); + // Count the fixture's actual CIL sites independently of how the template spells its body. + var mask = extracted.Split('\n') + .Single(line => line.StartsWith("def method0Reachable :", StringComparison.Ordinal)); + var instructions = mask.Count(character => character == ',') + 1; + var depth = LookupPatterns.RecursionDepth.Match(extracted); + if (!depth.Success || !IsDecodedMethod(extracted, 0) || !LookupPatterns.FirstEquations.IsMatch(extracted) || + !templates.Contains("cil_template_code% (Input0.method0.code)", StringComparison.Ordinal) || + !templates.Contains("cil_instruction_equations method0", StringComparison.Ordinal)) + throw new InvalidOperationException($"Missing the decoded body, equations or recursion depth of {name}."); var last = instructions - 1; var extraction = Checked(Path.Combine(output, "Measured.lean"), - extracted + $"\n#print axioms Extracted.method0_instruction{last}\n", + WithEvidenceCosts(extracted) + $"\n#print axioms Extracted.method0_instruction{last}\n", "", "Extracted.method0", $"Extracted.method0_instruction{last}", last); - const string Binder = "(profile : CIL.FeatureProfile) "; - var body = "def method0 (profile : FeatureProfile) : Method := { profile := profile," + - definition.Value[Header.Length..]; + const string Binder = "(profile : CIL.FeatureProfile) (environment : CIL.LayoutEnvironment) "; + var statements = name == "Short" ? ShortStatements : LongStatements; + var operations = string.Join(", ", Enumerable.Repeat(".ldarg 1, .mul, .ldarg 0, .add", statements)); + var body = $$""" + def method0 (profile : FeatureProfile) (environment : LayoutEnvironment) : Method := + { profile, environment, locals := [], returnsValue := true, code := [.ldarg 0, {{operations}}, .ret] } + """; + if (instructions != 4 * statements + 2) + throw new InvalidOperationException($"The CIL no longer matches the multiply-add fixture for {name}."); var command = Checked(Path.Combine(output, "Body.lean"), $""" import CIL.InstructionEquations @@ -91,16 +2560,28 @@ open CIL cil_instruction_equations method0 #print axioms method0_instruction{last} end Body - """, Binder, "(Body.method0 profile)", $"Body.method0_instruction{last} profile", last); - - var shared = Path.Combine(output, "Shared"); - Run(["dotnet", ExtractorPath, "templates", shared, output]); - var templates = File.ReadAllText(Path.Combine(shared, "Templates.lean")); - if (!templates.Contains("cil_instruction_equations method0", StringComparison.Ordinal)) - throw new InvalidOperationException($"The template of {name} has no instruction equations."); + """, Binder, "(Body.method0 profile environment)", + $"Body.method0_instruction{last} profile environment", last); var template = Checked(Path.Combine(shared, "Measured.lean"), templates, - Binder, "(Templates.method0 profile)", $"Templates.method0_instruction{last} profile", last); - return (instructions, [extraction, command, template]); + Binder, "(Templates.method0 profile environment)", + $"Templates.method0_instruction{last} profile environment", last, $$""" + namespace Handwritten + open CIL + {{body}} + open Lean Meta in + run_cmd Elab.Command.liftTermElabM do + forallTelescope (<- getConstInfo ``method0).type fun parameters _ => do + let code := fun name => mkApp (mkConst ``Method.code) (mkAppN (mkConst name) parameters) + let (expected, leftClosed) <- InstructionEquations.listElements (code ``method0) + let (actual, rightClosed) <- InstructionEquations.listElements (code ``Templates.method0) + unless leftClosed && rightClosed && expected.size == actual.size do + throwError "the hand-written and generated templates have different or open lengths" + for pc in [:expected.size] do + unless <- isDefEq expected[pc]! actual[pc]! do + throwError "the hand-written and generated instructions differ at pc {pc}" + end Handwritten + """); + return (instructions, [extraction.Count, command.Count, template.Count], EvidenceCosts(extraction.Output)); } var imports = Path.Combine(directory, "Imports.lean"); @@ -115,7 +2596,7 @@ end Body string[] producers = ["an extracted method", "a cil_instruction_equations body", "a generated template"]; for (var producer = 0; producer < producers.Length; producer++) { - double Cost((int Instructions, long[] Heartbeats) run) => + double Cost((int Instructions, long[] Heartbeats, (long, long) Evidence) run) => (double)(run.Heartbeats[producer] - floor) / run.Instructions; var growth = Cost(extended) / Cost(brief); var measured = string.Create(CultureInfo.InvariantCulture, @@ -127,6 +2608,315 @@ double Cost((int Instructions, long[] Heartbeats) run) => Console.WriteLine($"PASS compiling {producers[producer]} costs {measured}"); } +TemplateChoiceCosts(); +if (instructionEquationsOnly) return; + +// Each body has eight feature branches. Vary the number of distinct getters and inputs independently. +void TemplateChoiceCosts() +{ + var root = Path.Combine(directory, "TemplateChoices"); + var project = Path.Combine(root, "Assembly"); + Directory.CreateDirectory(project); + File.Copy(Path.Combine(source, "Lookups.csproj"), Path.Combine(project, "Flags.csproj"), overwrite: true); + string[] getters = ["Avx512F.IsSupported", "Aes.IsSupported", "Vector256.IsHardwareAccelerated", + "Avx.IsSupported", "Sse2.IsSupported", "Ssse3.IsSupported", "Avx2.IsSupported", "Bmi2.X64.IsSupported"]; + int[] sizes = [2, 4, 8]; + string MethodWithFlags(int flags) + { + var branches = Enumerable.Range(0, getters.Length) + .Select(i => $"if ({getters[i % flags]}) total += {i + 1};"); + return $$""" + public static int Read{{flags}}(int word) { int total = word; {{string.Join(' ', branches)}} return total; } + """; + } + File.WriteAllText(Path.Combine(project, "Flags.cs"), $$""" + using System.Runtime.Intrinsics; + using System.Runtime.Intrinsics.X86; + public static class Flags { {{string.Join('\n', sizes.Select(MethodWithFlags))}} } + """); + Run(["dotnet", "build", Path.Combine(project, "Flags.csproj"), "-c", "Release"]); + var image = Path.Combine(project, "bin/Flags.dll"); + int[] masks = [0, 7, 2, 5, 1, 6, 3, 4]; + foreach (var mask in masks.Append(15)) + { + var wide = (mask & 1) != 0; + var profile = new { name = $"choice{mask}", architecture = "x64", nativeWidth = 64, littleEndian = true, + sse2 = true, ssse3 = true, sse41 = true, sse42 = true, avx = true, avx2 = true, x86Base64 = true, + vector128Accelerated = true, vector256Accelerated = (mask & 4) != 0, bmi1 = true, bmi2 = true, + avx512F = wide, avx512FVL = wide, avx512DQ = wide, avx512DQVL = wide, avx512Vbmi = (mask & 8) != 0, + aes = (mask & 2) != 0, armAes = false, advSimd = false, armBase64 = false }; + var json = System.Text.Json.JsonSerializer.Serialize(profile); + File.WriteAllText(Path.Combine(root, $"profile{mask}.json"), json); + } + var samples = new Dictionary<(bool VaryInputs, int Size), (long Total, long Selection)>(); + var extracted = new HashSet(StringComparer.Ordinal); + var pcs = 0; + foreach (var varyInputs in new[] { true, false }) + foreach (var size in sizes) + { + var flags = varyInputs ? 4 : size; + // Two pairs of profiles agree on all observed getters as the number of distinct getters grows. + string[] profiles = varyInputs ? [.. masks.Take(size).Select(mask => $"profile{mask}")] : + ["scalar", "arm64-advsimd", "profile7", "profile15"]; + var paths = new List(); + foreach (var profile in profiles) + { + var path = Path.Combine(root, $"Read{flags}-{profile}"); + if (extracted.Add(path)) + { + var specification = profile.StartsWith("profile", StringComparison.Ordinal) + ? $"@{Path.Combine(root, $"{profile}.json")}" : profile; + Run(Extract("extract", image, "Flags", $"Flags::Read{flags}(int)", path, specification)); + } + paths.Add(path); + } + var shared = Path.Combine(root, varyInputs ? $"Inputs{size}" : $"Flags{size}"); + Run(["dotnet", ExtractorPath, "templates", shared, .. paths]); + var module = File.ReadAllText(Path.Combine(shared, "Templates.lean")).ReplaceLineEndings("\n"); + var start = module.IndexOf("/-- The stable managed identity", StringComparison.Ordinal); + var last = module.LastIndexOf("#print axioms method0_instruction", StringComparison.Ordinal); + using var manifest = System.Text.Json.JsonDocument.Parse(File.ReadAllText(Path.Combine(shared, + "templates.json"))); + var features = manifest.RootElement.GetProperty("methods")[0].GetProperty("flags").EnumerateArray() + .Select(flag => flag.GetString()!) + .Select(flag => $"{char.ToUpperInvariant(flag[0])}{flag[1..]}").ToArray(); + var bindings = manifest.RootElement.GetProperty("profiles"); + int ObservedFlags(System.Text.Json.JsonElement binding) + { + var profile = binding.GetProperty("profile"); + return features.Aggregate(0, (bits, flag) => + 2 * bits + (profile.GetProperty(flag).GetBoolean() ? 1 : 0)); + } + var leaves = bindings.EnumerateArray().Select(ObservedFlags).Distinct().Count(); + if (features.Length != flags || bindings.GetArrayLength() != paths.Count || + leaves != (varyInputs ? size : 2) || start < 0 || last < 0) + throw new InvalidOperationException($"Unexpected dimensions at {paths.Count} inputs, {flags} flags."); + var number = module[(last + "#print axioms method0_instruction".Length)..].Split('\n')[0]; + var count = int.Parse(number, CultureInfo.InvariantCulture) + 1; + if (pcs != 0 && count != pcs) throw new InvalidOperationException("Template instruction counts differ."); + pcs = count; + var header = module.IndexOf("/-!", StringComparison.Ordinal); + var parts = new[] { module[..header], module[header..start], module[start..] }; + var costs = PartHeartbeats(Path.Combine(shared, "Costs.lean"), parts); + samples.Add((varyInputs, size), (costs.Sum(), costs[1])); + Console.WriteLine($""" + Template {Path.GetFileName(shared)}, {paths.Count} inputs, {flags} flags, {pcs} pcs: + {costs.Sum()} total heartbeats, {costs[1]} for selection, leaf proofs and equations + """); + } + foreach (var varyInputs in new[] { true, false }) + foreach (var selectionOnly in new[] { true, false }) + { + double PerUnit(int size) + { + var cost = samples[(varyInputs, size)]; + return (double)(selectionOnly ? cost.Selection : cost.Total) / (size * pcs); + } + foreach (var (smaller, larger) in sizes.Zip(sizes.Skip(1))) + if (PerUnit(larger) > AllowedGrowth * PerUnit(smaller)) + throw new InvalidOperationException($""" + Template cost from {smaller} to {larger} exceeds {AllowedGrowth} per unit per pc. + Inputs varying: {varyInputs}. Selection only: {selectionOnly}. + Measured {PerUnit(smaller):F0} and {PerUnit(larger):F0} heartbeats. + """); + } +} + +// A body with no call at three lengths. Its call check reads every pc's link by one walk, so it costs the same per +// instruction; its evidence theorem is assembled from the conjuncts' theorems and costs the same at every length. +// A conjunct stated through another term than `MethodEvidenceValid` states makes the assembly reduce the whole body. +var middleOutput = Path.Combine(directory, "Middle"); +Run(Extract("extract", assembly, "Lookups", "Lookups::Middle(ulong,ulong)", middleOutput)); +var middle = EvidenceCosts(Reported(Path.Combine(middleOutput, "Measured.lean"), + WithEvidenceCosts(File.ReadAllText(Path.Combine(middleOutput, "Extracted.lean"))), equations: false).Output); +(int Statements, long Calls, long Assembly)[] evidenceSamples = [ + (ShortStatements, brief.Evidence.Calls, brief.Evidence.Assembly), + (MiddleStatements, middle.Calls, middle.Assembly), + (LongStatements, extended.Evidence.Calls, extended.Evidence.Assembly)]; +var evidenceMeasured = string.Join("; ", evidenceSamples.Select(sample => string.Create(CultureInfo.InvariantCulture, + $"{sample.Statements} statements = {(double)sample.Calls / sample.Statements:F0} per statement for the call " + + $"check and {sample.Assembly} for the evidence theorem"))); +if (evidenceSamples.Any(sample => sample.Calls < KernelDecisionFloor || sample.Assembly < KernelDecisionFloor) || + evidenceSamples.Skip(1).Zip(evidenceSamples).Any(pair => + (double)pair.First.Calls / pair.First.Statements > + (double)pair.Second.Calls / pair.Second.Statements * AllowedGrowth || + pair.First.Assembly > pair.Second.Assembly * AllowedGrowth)) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Checking the evidence of a body with no call costs {evidenceMeasured} heartbeats; the limit is " + + $"{AllowedGrowth} times the cost per statement of the call check and the cost of the evidence theorem at " + + $"the length before.")); +Console.WriteLine($"PASS checking the evidence of a body with no call costs {evidenceMeasured} heartbeats"); + +// The extraction of a body with many calls: its link table, key check, per-site facts and the checks that +// tie them together must each cost the same per call site at four times the size. +long CallingCost(string name) +{ + var output = Path.Combine(directory, name); + Run(Extract("extract", assembly, "Lookups", $"Lookups::{name}(ulong,ulong)", output)); + return Heartbeats(Path.Combine(output, "Measured.lean"), File.ReadAllText(Path.Combine(output, "Extracted.lean")), + equations: false); +} +var fewCost = (double)(CallingCost("Few") - floor) / FewCalls; +var manyCost = (double)(CallingCost("Many") - floor) / ManyCalls; +var callGrowth = manyCost / fewCost; +var callsMeasured = string.Create(CultureInfo.InvariantCulture, + $"{fewCost:F0} heartbeats per call at {FewCalls} calls and {manyCost:F0} at {ManyCalls}, " + + $"{callGrowth:F2} times as many"); +if (callGrowth > AllowedCallGrowth) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Extracting a body with many calls costs {callsMeasured}; the limit is {AllowedCallGrowth}.")); +Console.WriteLine($"PASS extracting a body with many calls costs {callsMeasured}"); + +// The kernel heartbeats to recheck the proof that `written` embeds into `extraction`, both defined by +// `definitions`. The two link tables differ and their call sites sit at different pcs, so the kernel cannot close +// the link clause by comparing equal arguments: it must read both tables, and a read that scans the table per +// call grows with the call count. `cil_embed` states the proof as an auxiliary lemma, so that lemma is rechecked. +long EmbeddingCost(string name, string definitions) +{ + var path = Path.Combine(directory, name + ".lean"); + File.WriteAllText(path, $$""" + import CIL.Embedding.Tactic + set_option maxHeartbeats 0 + set_option maxRecDepth 100000 + open CIL + namespace Embedded + {{definitions}} + theorem embeds : MethodEmbeds [extraction] 0 written := by cil_embed + end Embedded + open Lean in + run_cmd do + let env ← getEnv + let some (.thmInfo declaration) := env.find? `Embedded.embeds | throwError "missing" + let start ← IO.getNumHeartbeats + let mut count : Nat := 0 + for name in declaration.value.getUsedConstants do + let some (.thmInfo used) := env.find? name | continue + let copy : Declaration := .thmDecl { used with name := name ++ `recheck } + match env.toKernelEnv.addDeclCore 0 1000000 copy none with + | .ok _ => count := count + 1 + | .error _ => throwError "Kernel rejected the recheck of {name}" + let beats := (← IO.getNumHeartbeats) - start + logInfo m!"{count} lemmas rechecked, {beats} kernel heartbeats" + """ + "\n"); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var cost = LookupPatterns.EmbeddingRecheck.Match(output); + if (!cost.Success || cost.Groups[1].Value == "0") + throw new InvalidOperationException("Missing the embedding recheck cost:\n" + output); + return long.Parse(cost.Groups[2].Value, CultureInfo.InvariantCulture); +} + +// The method every embedded call names; the link at its pc chooses the body it enters. +const string EmbeddedCallees = """ +/-- A one-argument method whose result each segment pops. -/ +def callee : Extraction.MethodRef := + { owner := .atom "Tests" "Embedded", name := name!"Callee", hasThis := false, genericArity := 0, + returnType := .atom "Tests" "Word", parameters := [.atom "Tests" "Word"] } +"""; + +// A hand-written body embedded into an extraction with a dead block after each return. +string DeadBlocks(int calls) => $$""" +{{EmbeddedCallees}} +/-- What an extraction emits for one call followed by a dead block. -/ +def extractedSegment : List Instruction := + [.ldarg 0, .call callee, .pop, .ret, .unadmitted 0] +/-- The hand-written segment without the dead block. -/ +def writtenSegment : List Instruction := [.ldarg 0, .call callee, .pop, .ret] +/-- The extraction, whose calls sit at pcs 1, 6, 11 and so on, each linked to method 1. -/ +def extraction : Method := + { locals := [], returnsValue := false, + code := (List.replicate {{calls}} extractedSegment).flatten ++ [.ret], + links := (List.range {{calls}}).map fun index => (5 * index + 1, .direct 1) } +/-- The proof's body, with calls at pcs 1, 5, 9 and so on, each linked to method 1. -/ +def written : Method := + { extraction.withCode ((List.replicate {{calls}} writtenSegment).flatten ++ [.ret]) with + links := (List.range {{calls}}).map fun index => (4 * index + 1, .direct 1) } +"""; + +// A body whose head branches over its first block to its second, embedded into an extraction that places the +// second block first, so the pc map descends once at every size. +string Reordered(int segments) => $$""" +{{EmbeddedCallees}} +/-- The block after the head, whose call is linked to method 1. -/ +def first : List Instruction := [.ldarg 0, .call callee, .pop, .ret] +/-- The block the head branches to, whose call is linked to method 2. -/ +def second : List Instruction := [.ldarg 1, .call callee, .pop, .ret] +/-- The links of `count` copies of a block starting at `start`, each call linked to `target`. -/ +def blockLinks (start count target : Nat) : List (Nat × Link) := + (List.range count).map fun index => (start + 4 * index + 1, .direct target) +/-- The extraction, with the second block first. -/ +def extraction : Method := + { locals := [], returnsValue := false, + code := [.br 1] ++ (List.replicate {{segments}} second).flatten ++ + (List.replicate {{segments}} first).flatten ++ [.ret], + links := blockLinks 1 {{segments}} 2 ++ blockLinks {{1 + 4 * segments}} {{segments}} 1 } +/-- The proof's body, with the blocks in source order. -/ +def written : Method := + { extraction.withCode ([.br {{1 + 4 * segments}}] ++ (List.replicate {{segments}} first).flatten ++ + (List.replicate {{segments}} second).flatten ++ [.ret]) with + links := blockLinks 1 {{segments}} 1 ++ blockLinks {{1 + 4 * segments}} {{segments}} 2 } +#guard let map := (MethodBinding.alignLive written.code.toArray extraction.code.toArray).toList + (map.zip map.tail).countP (fun (left, right) => right < left) == 1 +"""; + +// Fails when a fixture at the larger size costs more per call to embed than the allowed growth. +void CheckEmbeddingGrowth(string fixture, string description, Func definitions, int callsPerSize, + int few, int many) +{ + double Cost(int size) => (double)EmbeddingCost(fixture + size, definitions(size)) / (callsPerSize * size); + var fewCost = Cost(few); + var manyCost = Cost(many); + var growth = manyCost / fewCost; + var measured = string.Create(CultureInfo.InvariantCulture, + $"{fewCost:F0} kernel heartbeats per call at {callsPerSize * few} calls and {manyCost:F0} at " + + $"{callsPerSize * many}, {growth:F2} times as many"); + if (growth > AllowedEmbedGrowth) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Embedding {description} costs {measured}; the limit is {AllowedEmbedGrowth}.")); + Console.WriteLine($"PASS embedding {description} costs {measured}"); +} +CheckEmbeddingGrowth("Embedding", "a body with many calls", DeadBlocks, 1, EmbedFewCalls, EmbedManyCalls); +CheckEmbeddingGrowth("Reordered", "a body whose blocks the extraction reorders", Reordered, 2, + ReorderFewSegments, ReorderManySegments); + +// The text a method emits for its links and the kernel heartbeats to recheck that its table keys ascend, for a body +// with two calls: both depend on the calls, not on the length of the body. +(long Bytes, long Heartbeats) SparseLinkCost(string name) +{ + var output = Path.Combine(directory, name); + Run(Extract("extract", assembly, "Lookups", $"Lookups::{name}(ulong,ulong)", output)); + var extracted = File.ReadAllText(Path.Combine(output, "Extracted.lean")); + var linkLines = extracted.Split('\n').Where(line => LookupPatterns.LinkLine.IsMatch(line)) + .Sum(line => (long)line.Length + 1); + if (linkLines == 0) throw new InvalidOperationException("The extraction of " + name + " has no link output."); + var path = Path.Combine(output, "LinkAscend.lean"); + File.WriteAllText(path, extracted + """ + + open Lean in + run_cmd do + let env ← getEnv + let some (.thmInfo declaration) := env.find? `Extracted.method0_links_ascend + | throwError "missing the link key check" + let start ← IO.getNumHeartbeats + let copy : Declaration := .thmDecl { declaration with name := declaration.name ++ `recheck } + match env.toKernelEnv.addDeclCore 0 1000000 copy none with + | .ok _ => pure () + | .error _ => throwError "Kernel rejected the link key check" + logInfo m!"{(← IO.getNumHeartbeats) - start} kernel heartbeats" + """ + "\n"); + var report = Run(["lake", "env", "lean", path], cwd: FixturePackage()); + var beats = LookupPatterns.KernelHeartbeats.Match(report); + if (!beats.Success) throw new InvalidOperationException("Missing the link key recheck cost:\n" + report); + return (linkLines, long.Parse(beats.Groups[1].Value, CultureInfo.InvariantCulture)); +} +var sparseShort = SparseLinkCost("SparseShort"); +var sparseLong = SparseLinkCost("SparseLong"); +var sparseMeasured = $"{sparseShort.Bytes} link bytes and {sparseShort.Heartbeats} kernel heartbeats at " + + $"{SparseShortStatements} statements, {sparseLong.Bytes} and {sparseLong.Heartbeats} at {SparseLongStatements}"; +if (sparseLong.Bytes > sparseShort.Bytes * 1.1 || sparseLong.Heartbeats > sparseShort.Heartbeats * AllowedCallGrowth) + throw new InvalidOperationException("A longer body with the same calls emits more link output: " + sparseMeasured); +Console.WriteLine("PASS link output and key check of a body with two calls do not grow with the body: " + + sparseMeasured); + // Subtract the same generated body's cost to isolate computed lookup from equation generation. long ComputedLookupCost(int cases) { @@ -134,21 +2924,19 @@ long ComputedLookupCost(int cases) import CIL.InstructionEquations import CIL.Safety.SymbolicExecution open CIL.Safety - def segment : List CIL.Op := [.arg 0, .arg 1, .add, .nop, .nop, .nop, .nop] + def segment : List CIL.Instruction := [.ldarg 0, .ldarg 1, .add, .nop, .nop, .nop, .nop] def body : CIL.Method := { locals := [], returnsValue := true, code := [.nop] ++ (List.replicate {{cases}} segment).flatten ++ [.ret] } cil_instruction_equations body """ + "\n"; var path = Path.Combine(directory, "Computed" + cases + ".lean"); - var generated = Heartbeats(path, header, equations: false); - var checkedLookup = Heartbeats(path, header + $$""" + return PartHeartbeats(path, [header, $$""" /-- Each repeated segment has its addition at the same relative offset. -/ theorem fetched (index : Fin {{cases}}) : body.code[1 + 7 * index.val + 2]? = some .add := by cil_lookup [] #print axioms fetched - """ + "\n", equations: false); - return checkedLookup - generated; + """ + "\n"])[0]; } var fourCaseCost = ComputedLookupCost(4) / 4.0; var sixteenCaseCost = ComputedLookupCost(16) / 16.0; @@ -156,3 +2944,930 @@ cil_lookup [] $"PASS computed lookup: {fourCaseCost:F0} heartbeats/case at 4; {sixteenCaseCost:F0} at 16")); if (sixteenCaseCost > fourCaseCost * AllowedGrowth) throw new Exception("Computed lookup cost per case grows with the segment count."); + +// Each distinct metadata token of a body adds a row lookup and a decode to the kernel's metadata check, so the cost +// per token may not grow with the number of tokens, whichever table the token names: user strings, instance fields +// of a class, and methods (whose count also grows the number of methods of the extraction and so the metadata +// carried). +const int FewTokens = 125; +const int ManyTokens = 500; +// Each called method is an extracted method with a certificate of its own, and composing the certificate of an +// extraction of about 500 methods exceeds the default heartbeat budget, so the call counts stay below that. +const int FewCalledMethods = 25; +const int ManyCalledMethods = 100; +var tokenSource = Path.Combine(directory, "TokenAssembly"); +Directory.CreateDirectory(tokenSource); +File.WriteAllText(Path.Combine(tokenSource, "Tokens.csproj"), """ + + net11.0true + +"""); +string Join(int count, Func piece) => string.Concat(Enumerable.Range(0, count).Select(piece)); +string StringMethod(int count, string name) => + $"public static ulong {name}(ulong x) {{ " + Join(count, index => $"Use(\"{name}{index}\"); ") + "return x; } "; +string FieldMethod(int count, string name) => + $"public static ulong {name}(Wide{count} w) {{ return 0UL" + Join(count, index => $" + w.F{index}") + "; } " + + $"public sealed class Wide{count} {{ " + Join(count, index => $"public ulong F{index}; ") + "} "; +string CallMethod(int count, string name) => + $"public static ulong {name}(ulong x) {{ " + Join(count, index => $"x = {name}{index}(x); ") + "return x; } " + + Join(count, index => $"static ulong {name}{index}(ulong x) => x + {index + 1}UL; "); +File.WriteAllText(Path.Combine(tokenSource, "Tokens.cs"), + "public static class Tokens { public static void Use(string text) { } " + + StringMethod(FewTokens, "StringsFew") + StringMethod(ManyTokens, "StringsMany") + + FieldMethod(FewTokens, "FieldsFew") + FieldMethod(ManyTokens, "FieldsMany") + + CallMethod(FewCalledMethods, "CallsFew") + CallMethod(ManyCalledMethods, "CallsMany") + "}"); +Run(["dotnet", "build", Path.Combine(tokenSource, "Tokens.csproj"), "-c", "Release"]); +var tokenAssembly = Path.Combine(tokenSource, "bin/Tokens.dll"); +// The heartbeats per token of the whole extraction, less the imports, and of its call check alone. A call check that +// reads each pc's link by a scan of the link table grows with the tokens that each carry a link. +(double Total, double Calls) TokenCost(string name, string selector, int count, Regex distinctToken) +{ + var output = Path.Combine(directory, "Tokens" + name); + Run(Extract("extract", tokenAssembly, "Tokens", selector, output)); + var extracted = File.ReadAllText(Path.Combine(output, "Extracted.lean")); + var distinct = distinctToken.Count(extracted); + if (distinct < count) + throw new InvalidOperationException($"Expected {count} distinct tokens in {name}; found {distinct}."); + var report = Reported(Path.Combine(output, "Measured.lean"), WithEvidenceCosts(extracted), equations: false); + return ((double)(report.Count - floor) / count, (double)EvidenceCosts(report.Output).Calls / count); +} +foreach (var (kind, few, many, pattern, fewCount, manyCount) in new[] +{ + ("Strings", "Tokens::StringsFew(ulong)", "Tokens::StringsMany(ulong)", + LookupPatterns.StringToken, FewTokens, ManyTokens), + ("Fields", $"Tokens::FieldsFew(Tokens/Wide{FewTokens})", $"Tokens::FieldsMany(Tokens/Wide{ManyTokens})", + LookupPatterns.FieldToken, FewTokens, ManyTokens), + ("Calls", "Tokens::CallsFew(ulong)", "Tokens::CallsMany(ulong)", LookupPatterns.CallToken, FewCalledMethods, + ManyCalledMethods) +}) +{ + var manyTokens = TokenCost(kind + "Many", many, manyCount, pattern); + var fewTokens = TokenCost(kind + "Few", few, fewCount, pattern); + var tokenGrowth = manyTokens.Total / fewTokens.Total; + var callCheckGrowth = manyTokens.Calls / fewTokens.Calls; + var tokensMeasured = string.Create(CultureInfo.InvariantCulture, + $"{tokenGrowth:F2} times as much per token as {fewCount}, and {callCheckGrowth:F2} times for the call check"); + if (tokenGrowth > AllowedGrowth || callCheckGrowth > AllowedGrowth) + throw new Exception(string.Create(CultureInfo.InvariantCulture, + $"Checking {manyCount} {kind.ToLowerInvariant()} tokens costs {tokensMeasured}; the limit is " + + $"{AllowedGrowth}.")); + Console.WriteLine( + $"PASS metadata check of {manyCount} distinct {kind.ToLowerInvariant()} tokens costs {tokensMeasured}"); +} + +// One method's body check reads the literal lookup tables of its own metadata slice by name, so its kernel cost is its +// own slice and tokens times a logarithm: independent of the number of methods in the extraction and of the rows +// that other methods carry. +const double AllowedBodyGrowth = 1.1; +int[] chainLengths = [8, 32, 128]; +int[] bulkSizes = [125, 500]; +int[] fieldCounts = [10, 100, 200]; +int[] stringTokenCounts = [125, 500, 1000]; +var bodySource = Path.Combine(directory, "BodyAssembly"); +Directory.CreateDirectory(bodySource); +File.WriteAllText(Path.Combine(bodySource, "Bodies.csproj"), + File.ReadAllText(Path.Combine(tokenSource, "Tokens.csproj"))); +string Chain(int count) => + $"public static ulong Chain{count}(ulong x) {{ " + + Join(count, index => $"x = Chain{count}_{index}(x); ") + "return x; } " + + Join(count, index => $"static ulong Chain{count}_{index}(ulong x) => x + {index + 1}UL; "); +string Rows(int count) => + $"public static ulong Rows{count}(ulong x) {{ x = Small{count}(x); return Bulk{count}(x); }} " + + $"static ulong Small{count}(ulong x) => x + 1UL; " + + $"static ulong Bulk{count}(ulong x) {{ " + Join(count, index => $"Use(\"r{count}_{index}\"); ") + "return x; } "; +// A method that reads one field of a class with `count` fields carries that class's field rows. +string Fields(int count) => + $"public sealed class Wide{count} {{ " + Join(count, index => $"public ulong F{index}; ") + "} " + + $"public static ulong Read{count}(Wide{count} w) => w.F{count / 2}; "; +// A method of `count` string tokens has `2 * count + 2` instructions. +string Strings(int count) => + $"public static ulong Strings{count}(ulong x) {{ " + Join(count, index => $"Use(\"s{count}_{index}\"); ") + + "return x; } "; +File.WriteAllText(Path.Combine(bodySource, "Bodies.cs"), + "public static class Bodies { public static void Use(string text) { } " + + string.Concat(chainLengths.Select(Chain)) + string.Concat(bulkSizes.Select(Rows)) + + string.Concat(fieldCounts.Select(Fields)) + string.Concat(stringTokenCounts.Select(Strings)) + "}"); +Run(["dotnet", "build", Path.Combine(bodySource, "Bodies.csproj"), "-c", "Release"]); +var bodyAssembly = Path.Combine(bodySource, "bin/Bodies.dll"); +// The index of a method in an extraction, found by the name in its signature. +string MethodNumber(string extracted, string method, string name) +{ + return LookupPatterns.BodySignature.Matches(extracted) + .FirstOrDefault(match => match.Groups["method"].Value == method)?.Groups[1].Value + ?? throw new InvalidOperationException($"Method {method} is not in the extraction {name}."); +} +// The heartbeats of the method's `method{n}_body_validated` and, for `tablesMethod`, of that method's +// `tables_valid`, read in one file of the extraction narrowed to the body obligation. Each is read after the same +// statement closed with `sorry`, which is subtracted. +(long Body, long Tables) BodyChecks(string name, string entry, string method, string? tablesMethod = null) +{ + var output = Path.Combine(directory, "Body" + name); + Run(Extract("extract", bodyAssembly, "Bodies", entry.Contains('(') ? entry : $"Bodies::{entry}(ulong)", output)); + var extracted = File.ReadAllText(Path.Combine(output, "Extracted.lean")); + var body = $"method{MethodNumber(extracted, method, name)}_body_validated"; + var narrowed = NarrowEvidence(extracted, body); + var obligations = new List<(Match Proof, string Kind)> { (Obligation(narrowed, body, decided: false), "body") }; + if (tablesMethod is not null) + obligations.Add((Obligation(narrowed, $"method{MethodNumber(extracted, tablesMethod, name)}_tables_valid", + decided: true), "tables")); + var parts = new List(); + var readAt = new Dictionary(); + var start = 0; + foreach (var (proof, kind) in obligations.OrderBy(obligation => obligation.Proof.Index)) + { + parts.Add(narrowed[start..proof.Index]); + var statement = proof.Groups["statement"].Value; + parts.Add($"theorem {proof.Groups["name"].Value}_open : {statement} := sorry\n"); + readAt[kind] = parts.Count - 1; + parts.Add(proof.Value); + start = proof.Index + proof.Length; + } + // Audit each measured proof; later declarations cannot contribute to its elaboration cost. + parts.Add(string.Join('\n', obligations.Select(obligation => + $"#print axioms {obligation.Proof.Groups["name"].Value}")) + "\nend Extracted\n"); + var costs = PartHeartbeats(Path.Combine(output, "BodyChecked.lean"), parts); + long Cost(string kind) => costs[readAt[kind]] - costs[readAt[kind] - 1]; + return (Cost("body"), tablesMethod is null ? 0 : Cost("tables")); +} +// The line of obligation `theorem` in `narrowed`, proved by `decide +kernel` when `decided` and by an anonymous +// constructor otherwise. +static Match Obligation(string narrowed, string theorem, bool decided) => + LookupPatterns.ProvedObligation.Matches(narrowed).FirstOrDefault(match => + match.Groups["name"].Value == theorem && match.Groups["decision"].Success == decided) + ?? throw new InvalidOperationException("Missing obligation " + theorem); +long BodyCheck(string name, string entry, string method) => BodyChecks(name, entry, method).Body; +var chainCosts = chainLengths.Select(count => (double)BodyCheck("Chain" + count, "Chain" + count, + $"Chain{count}_{count - 1}")).ToArray(); +var rowChecks = bulkSizes.Select(count => + BodyChecks("Rows" + count, "Rows" + count, $"Small{count}", $"Bulk{count}")).ToArray(); +var rowCosts = rowChecks.Select(check => (double)check.Body).ToArray(); +for (var step = 1; step < chainCosts.Length; step++) +{ + var growth = chainCosts[step] / chainCosts[step - 1]; + Console.WriteLine(FormattableString.Invariant( + $"PASS body check of one method in {chainLengths[step - 1]} and {chainLengths[step]} methods: ") + + FormattableString.Invariant($"{chainCosts[step - 1]:F0} and {chainCosts[step]:F0} heartbeats, ") + + FormattableString.Invariant($"{growth:F2} times")); + if (growth > AllowedBodyGrowth) + throw new Exception(FormattableString.Invariant( + $"One body check costs {growth:F2} times as much at {chainLengths[step]} methods ") + + FormattableString.Invariant($"as at {chainLengths[step - 1]}; the limit is {AllowedBodyGrowth}.")); +} +var rowGrowth = rowCosts[1] / rowCosts[0]; +Console.WriteLine(FormattableString.Invariant( + $"PASS body check of one method against {bulkSizes[0]} and {bulkSizes[1]} metadata rows: {rowGrowth:F2} times")); +if (rowGrowth > AllowedBodyGrowth) + throw new Exception(FormattableString.Invariant( + $"One body check costs {rowGrowth:F2} times as much against {bulkSizes[1]} rows ") + + FormattableString.Invariant($"as against {bulkSizes[0]}; the limit is {AllowedBodyGrowth}.")); +// The body check of a method of many string tokens costs the same per instruction at every size: numerals of its +// byte literal with equal low bytes would collide in the kernel's caches unless the literal is one joined code. +var stringCosts = stringTokenCounts.Select(count => + BodyCheck("Strings" + count, "Strings" + count, "Strings" + count) / (2.0 * count + 2)).ToArray(); +for (var step = 1; step < stringCosts.Length; step++) +{ + var growth = stringCosts[step] / stringCosts[step - 1]; + Console.WriteLine(FormattableString.Invariant( + $"PASS body check of a method of {stringTokenCounts[step - 1]} and {stringTokenCounts[step]} ") + + FormattableString.Invariant($"string tokens: {stringCosts[step - 1]:F0} and {stringCosts[step]:F0} ") + + FormattableString.Invariant($"heartbeats per instruction, {growth:F2} times")); + if (growth > AllowedBodyGrowth) + throw new Exception(FormattableString.Invariant( + $"One body check costs {growth:F2} times as much per instruction at {stringTokenCounts[step]} string ") + + FormattableString.Invariant( + $"tokens as at {stringTokenCounts[step - 1]}; the limit is {AllowedBodyGrowth}.")); +} +// The kernel's decision that a method's literal tables are the decoder's costs the rows that method carries, times a +// logarithm, and is made once per method. +var tablesFew = rowChecks[0].Tables; +var tablesMany = rowChecks[1].Tables; +var tablesGrowth = tablesMany / (double)tablesFew; +Console.WriteLine(FormattableString.Invariant( + $"PASS tables check of a method carrying {bulkSizes[0]} and {bulkSizes[1]} user string rows: ") + + FormattableString.Invariant($"{tablesFew} and {tablesMany} heartbeats, ") + + FormattableString.Invariant($"{tablesGrowth / (bulkSizes[1] / (double)bulkSizes[0]):F2} times the cost per row")); +if (tablesGrowth > AllowedBodyGrowth * bulkSizes[1] / bulkSizes[0]) + throw new Exception(FormattableString.Invariant( + $"The tables check costs {tablesGrowth:F2} times as much for {bulkSizes[1]} rows ") + + FormattableString.Invariant($"as for {bulkSizes[0]}.")); + +// A method reading one field of a class with 10, 100 and 200 fields carries those field rows. Its body check does one +// lookup of the field's index, so it stays flat. Its tables check is a fixed part plus a part linear in the rows +// carried: the increment from 100 to 200 fields is 100/90 of the increment from 10 to 100, and a quadratic walk of the +// rows makes it more than three times as large. +const double AllowedFieldIncrementRatio = 1.5; +var fieldChecks = fieldCounts.Select(count => BodyChecks("Fields" + count, + $"Bodies::Read{count}(Bodies/Wide{count})", $"Read{count}", $"Read{count}")).ToArray(); +var fieldBody = fieldChecks.Select(check => (double)check.Body).ToArray(); +var fieldTables = fieldChecks.Select(check => (double)check.Tables).ToArray(); +var fieldBodyGrowth = fieldBody[^1] / fieldBody[0]; +Console.WriteLine(FormattableString.Invariant( + $"PASS body check of a method reading one of {fieldCounts[0]}, {fieldCounts[1]} and {fieldCounts[2]} fields: ") + + FormattableString.Invariant($"{fieldBody[0]:F0}, {fieldBody[1]:F0} and {fieldBody[2]:F0} heartbeats, ") + + FormattableString.Invariant($"{fieldBodyGrowth:F2} times")); +if (fieldBodyGrowth > AllowedBodyGrowth) + throw new Exception(FormattableString.Invariant( + $"The body check costs {fieldBodyGrowth:F2} times as much with {fieldCounts[2]} fields ") + + FormattableString.Invariant($"as with {fieldCounts[0]}; the limit is {AllowedBodyGrowth}.")); +var fieldIncrementRatio = (fieldTables[2] - fieldTables[1]) / (fieldTables[1] - fieldTables[0]); +Console.WriteLine(FormattableString.Invariant( + $"PASS tables check of a method reading one of {fieldCounts[0]}, {fieldCounts[1]} and {fieldCounts[2]} fields: ") + + FormattableString.Invariant($"{fieldTables[0]:F0}, {fieldTables[1]:F0} and {fieldTables[2]:F0} heartbeats, ") + + FormattableString.Invariant($"increments in the ratio {fieldIncrementRatio:F2}")); +if (fieldIncrementRatio > AllowedFieldIncrementRatio) + throw new Exception(FormattableString.Invariant( + $"The tables check grows by {fieldIncrementRatio:F2} times as much from {fieldCounts[1]} to ") + + FormattableString.Invariant( + $"{fieldCounts[2]} fields as from {fieldCounts[0]} to {fieldCounts[1]}; ") + + FormattableString.Invariant($"the limit is {AllowedFieldIncrementRatio}.")); + +// `spelled?` reads a name's bytes in one pass, and `cil_kernel_rfl` equates the result with the name's literal: +// the cost grows with the length of the name and no faster. Each proof is read after the same statement closed with +// `sorry` in its file, so the statement's own elaboration is not counted. +const double AllowedSpellingGrowth = 1.1; +var spellingLengths = new[] { 20, 80, 320 }; +long SpellingCheck(int length) +{ + var name = string.Concat(Enumerable.Range(0, length).Select(index => (char)('a' + index % 26))); + var bytes = string.Join(", ", name.Select(character => (int)character)); + var statement = $"CIL.Extraction.spelled? [{bytes}] = some \"{name}\""; + var costs = PartHeartbeats(Path.Combine(directory, $"Spelling{length}.lean"), + ["import CIL.Extraction.KernelEquation\n", $"theorem spells_open : {statement} := sorry\n", + $"theorem spells : {statement} := by cil_kernel_rfl\n"]); + return costs[1] - costs[0]; +} +var spellingCosts = spellingLengths.Select(length => (double)SpellingCheck(length)).ToArray(); +for (var step = 1; step < spellingLengths.Length; step++) +{ + var lengthRatio = spellingLengths[step] / (double)spellingLengths[step - 1]; + var growth = spellingCosts[step] / spellingCosts[step - 1]; + Console.WriteLine(FormattableString.Invariant( + $"PASS spelling a name of {spellingLengths[step - 1]} and {spellingLengths[step]} bytes: ") + + FormattableString.Invariant($"{spellingCosts[step - 1]:F0} and {spellingCosts[step]:F0} heartbeats, ") + + FormattableString.Invariant($"{growth / lengthRatio:F2} times the cost per byte")); + if (growth > AllowedSpellingGrowth * lengthRatio) + throw new Exception(FormattableString.Invariant( + $"Spelling {spellingLengths[step]} bytes costs {growth:F2} times as much as ") + + FormattableString.Invariant($"{spellingLengths[step - 1]}; ") + + FormattableString.Invariant($"the limit is {AllowedSpellingGrowth * lengthRatio:F1}.")); +} + +// A name stored as its bytes (`byteString`) gives `bytesOf` those bytes without encoding the name, so reading them +// grows with the name's length and no faster, and the name's definition and one read cost less than reading the +// bytes of its literal. Each read is measured after the same statement is stated, so its elaboration is not counted. +const double AllowedStoredGrowth = 1.1; +long[] StoredCosts(int length) +{ + var name = string.Concat(Enumerable.Range(0, length).Select(index => (char)('a' + index % 26))); + var hex = string.Concat(name.Select(character => ((int)character).ToString("X2", CultureInfo.InvariantCulture))); + var sum = name.Sum(character => (int)character); + var literal = $"\"{name}\""; + string Read(string text) => $"(CIL.Extraction.bytesOf {text}).foldl (fun a b => a + b) 0 = {sum}"; + var costs = PartHeartbeats(Path.Combine(directory, $"Stored{length}.lean"), + ["import CIL.Extraction.MetadataBytes\n", + $"def stored : String :=\n CIL.Extraction.byteString (CIL.Extraction.fixedBytes {length} 0x{hex})\n" + + $" \"{name}\" (by cil_kernel_rfl)\n", + $"example : Prop := {Read("stored")}\n", $"theorem reads_stored : {Read("stored")} := by decide +kernel\n", + $"example : Prop := {Read(literal)}\n", + $"theorem reads_literal : {Read(literal)} := by decide +kernel\n"]); + return [costs[0], costs[2] - costs[1], costs[4] - costs[3]]; +} +var storedCosts = spellingLengths.Select(StoredCosts).ToArray(); +for (var step = 0; step < spellingLengths.Length; step++) +{ + var (definition, stored, literal) = (storedCosts[step][0], storedCosts[step][1], storedCosts[step][2]); + Console.WriteLine(FormattableString.Invariant( + $"PASS reading the bytes of a stored name of {spellingLengths[step]} bytes: {stored} heartbeats, ") + + FormattableString.Invariant($"{definition} to define it, {literal} for its literal")); + if (definition + stored >= literal) + throw new Exception(FormattableString.Invariant( + $"Defining and reading a stored name of {spellingLengths[step]} bytes costs {definition + stored} ") + + FormattableString.Invariant($"heartbeats, not below the {literal} of reading its literal.")); + if (step == 0) continue; + var lengthRatio = spellingLengths[step] / (double)spellingLengths[step - 1]; + var growth = stored / (double)storedCosts[step - 1][1]; + if (growth > AllowedStoredGrowth * lengthRatio) + throw new Exception(FormattableString.Invariant( + $"Reading a stored name of {spellingLengths[step]} bytes costs {growth:F2} times as much as ") + + FormattableString.Invariant($"{spellingLengths[step - 1]}; ") + + FormattableString.Invariant($"the limit is {AllowedStoredGrowth * lengthRatio:F1}.")); +} + +// `indirectTarget` selects the body of a `calli` by comparing the pointer's owner with each catalog entry's, and a +// proof decides it by `cil_kernel_rfl`: the kernel's cost grows with the owner's length and no faster. +void IndirectTargetCosts() +{ + var targetDirectory = At("artifacts/LookupScaling/IndirectTarget"); + Directory.CreateDirectory(targetDirectory); + int[] ownerLengths = [64, 256, 1024]; + // The selection's heartbeats, less those of its statement closed with `sorry` and read before it in the same file. + long Selection(int length) + { + var owner = string.Concat(Enumerable.Range(0, length).Select(index => (char)('a' + index % 26))); + var identity = $$"""⟨"{{owner}}", 6, []⟩"""; + var header = """ + import CIL.Safety.Execution + import CIL.Extraction.KernelEquation + def signature : CIL.ManagedSignature := + ⟨.atom CIL.Extraction.runtimeScope "System.Int32", [.atom CIL.Extraction.runtimeScope "System.Int32"]⟩ + """ + "\n"; + var statement = $$""" + CIL.Safety.indirectTarget signature [⟨⟨"other", 6, []⟩, 2⟩, ⟨{{identity}}, 1⟩] + ⟨signature, some {{identity}}⟩ = some 1 + """; + var costs = PartHeartbeats(Path.Combine(targetDirectory, $"Owner{length}.lean"), + [header, $"theorem selected_open : {statement} := sorry\n", + $"theorem selected : {statement} := by cil_kernel_rfl\n"]); + return costs[1] - costs[0]; + } + var costs = ownerLengths.Select(length => (double)Selection(length)).ToArray(); + for (var step = 1; step < ownerLengths.Length; step++) + { + var lengthRatio = ownerLengths[step] / (double)ownerLengths[step - 1]; + var growth = costs[step] / costs[step - 1]; + Console.WriteLine(FormattableString.Invariant( + $"PASS selecting a calli body for an owner of {ownerLengths[step - 1]} and {ownerLengths[step]} bytes: ") + + FormattableString.Invariant($"{costs[step - 1]:F0} and {costs[step]:F0} heartbeats, ") + + FormattableString.Invariant($"{growth / lengthRatio:F2} times the cost per byte")); + if (growth > AllowedSpellingGrowth * lengthRatio) + throw new Exception(FormattableString.Invariant( + $"Selecting a calli body for an owner of {ownerLengths[step]} bytes costs {growth:F2} times ") + + FormattableString.Invariant($"as much as for {ownerLengths[step - 1]}; ") + + FormattableString.Invariant($"the limit is {AllowedSpellingGrowth * lengthRatio:F1}.")); + } +} +IndirectTargetCosts(); + +// A method reading a field declared `depth` base classes up carries one row and one link per class of the chain, and +// its body check walks the chain once. The cost is a fixed part plus a part linear in the depth, so the increment from +// depth 8 to 16 is twice the increment from 4 to 8; a quadratic walk makes it four times. +const double AllowedChainIncrementRatio = 2.5; +int[] baseDepths = [4, 8, 16]; +var deepSource = Path.Combine(directory, "DeepAssembly"); +Directory.CreateDirectory(deepSource); +File.WriteAllText(Path.Combine(deepSource, "Deep.csproj"), """ + + net11.0true + Bodies + +"""); +string Deep(int depth) => + $"public class Deep{depth}_0 {{ public ulong F; }} " + + Join(depth, index => $"public class Deep{depth}_{index + 1} : Deep{depth}_{index} {{ }} ") + + $"public static ulong ReadDeep{depth}(Deep{depth}_{depth} o) => o.F; "; +File.WriteAllText(Path.Combine(deepSource, "Deep.cs"), + "public static class Bodies { " + string.Concat(baseDepths.Select(Deep)) + "}"); +Run(["dotnet", "build", Path.Combine(deepSource, "Deep.csproj"), "-c", "Release"]); +bodyAssembly = Path.Combine(deepSource, "bin/Bodies.dll"); +var depthCosts = baseDepths.Select(depth => (double)BodyCheck("Deep" + depth, + $"Bodies::ReadDeep{depth}(Bodies/Deep{depth}_{depth})", $"ReadDeep{depth}")).ToArray(); +var incrementRatio = (depthCosts[2] - depthCosts[1]) / (depthCosts[1] - depthCosts[0]); +Console.WriteLine(FormattableString.Invariant( + $"PASS body check of a field read through {baseDepths[0]}, {baseDepths[1]} and {baseDepths[2]} base classes: ") + + FormattableString.Invariant($"{depthCosts[0]:F0}, {depthCosts[1]:F0} and {depthCosts[2]:F0} heartbeats, ") + + FormattableString.Invariant($"increments in the ratio {incrementRatio:F2} (linear 2, quadratic 4)")); +if (incrementRatio > AllowedChainIncrementRatio) + throw new Exception(FormattableString.Invariant( + $"The body check grows by {incrementRatio:F2} times as much from depth {baseDepths[1]} to {baseDepths[2]} ") + + FormattableString.Invariant( + $"as from {baseDepths[0]} to {baseDepths[1]}; the limit is {AllowedChainIncrementRatio}.")); + +// The kernel heartbeats of the scalar certificate of a body whose segments each address an array element, with +// or without a `readonly.` prefix, and branch to the next segment, less those of stating the same certificate. +// Each instruction carries the byte offset extraction writes, so no two sites present the kernel the same check. +long ArrayAddressCost(bool prefixed, int sites) +{ + var flag = prefixed ? "true" : "false"; + var header = $$""" + import CIL.Extraction.Scalars + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + namespace ArrayAddresses + open CIL CIL.Extraction + def intType : TypeExpr := .atom runtimeScope "System.Int32" + def width (prefixed : Bool) : Nat := if prefixed then 6 else 5 + def bytes (prefixed : Bool) : Nat := if prefixed then 12 else 10 + def segment (prefixed : Bool) (index : Nat) : List RawInstruction := + let base := index * bytes prefixed + let address := base + bytes prefixed - 8 + [⟨base, 2, .none⟩, ⟨base + 1, 3, .none⟩] ++ + (if prefixed then [⟨base + 2, 65054, .none⟩] else []) ++ + [⟨address, 143, .type intType⟩, ⟨address + 5, 38, .none⟩, + ⟨address + 6, 43, .target ((index + 1) * width prefixed)⟩] + def raw (prefixed : Bool) (sites : Nat) : RawMethod := { + token := 1, owner := "Scaling", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [.array intType, intType], locals := [], initLocals := true, maxStack := 2, + genericParameters := [], + code := (List.range sites).flatMap (segment prefixed) ++ [⟨sites * bytes prefixed, 42, .none⟩] } + def featureSegment (prefixed : Bool) : FeatureCertificate := + [some ⟨[], []⟩, some ⟨[none], []⟩] ++ + (if prefixed then [some ⟨[none, none], []⟩] else []) ++ + [some ⟨[none, none], []⟩, some ⟨[none], []⟩, some ⟨[], []⟩] + def features (prefixed : Bool) (sites : Nat) : FeatureCertificate := + (List.replicate sites (featureSegment prefixed)).flatten ++ [some ⟨[], []⟩] + def stateSegment (prefixed : Bool) : ScalarCertificate := + [some [], some [.array .signed32]] ++ + (if prefixed then [some [.word32, .array .signed32]] else []) ++ + [some [.word32, .array .signed32], some [.reference], some []] + def states (prefixed : Bool) (sites : Nat) : ScalarCertificate := + (List.replicate sites (stateSegment prefixed)).flatten ++ [some []] + def codeSegment (prefixed : Bool) (index : Nat) : List Instruction := + [.ldarg 0, .ldarg 1] ++ (if prefixed then [.nop] else []) ++ + [.ldelema intType prefixed, .pop, .br ((index + 1) * width prefixed)] + def code (prefixed : Bool) (sites : Nat) : List Instruction := + (List.range sites).flatMap (codeSegment prefixed) ++ [.ret] + """ + "\n"; + var statement = $"ScalarFlowValid (raw {flag} {sites}) (features {flag} {sites}) " + + $"(states {flag} {sites}) (code {flag} {sites})"; + var path = Path.Combine(directory, $"ArrayAddresses{(prefixed ? "Prefixed" : "")}{sites}.lean"); + return DecideCost(path, header, statement, "ArrayAddresses"); +} +foreach (var prefixed in new[] { false, true }) +{ + var label = prefixed ? "readonly element addresses" : "element addresses"; + var perSite = new[] { FewAddresses, MoreAddresses, ManyAddresses } + .Select(sites => (Sites: sites, Cost: (double)ArrayAddressCost(prefixed, sites) / sites)).ToArray(); + var addressesMeasured = string.Join("; ", perSite.Select(run => + string.Create(CultureInfo.InvariantCulture, $"{run.Cost:F0} kernel heartbeats per address at {run.Sites}"))); + if (perSite.Skip(1).Any(run => run.Cost > perSite[0].Cost * AllowedGrowth)) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Checking the scalar certificate of {label} costs {addressesMeasured}; the limit is {AllowedGrowth} " + + $"times the cost per address at {FewAddresses}.")); + Console.WriteLine($"PASS checking the scalar certificate of {label} costs {addressesMeasured}"); +} +IndirectAccessCosts(); + +// The kernel heartbeats of the feature and scalar certificates of a body whose segments each copy a block, with or +// without an `unaligned.` prefix, and branch to the next segment, less those of stating them. The indirect access +// rule reads the instructions next to each site from an index built once per body. Instructions carry byte offsets. +void IndirectAccessCosts() +{ + var accessDirectory = At("artifacts/LookupScaling/IndirectAccess"); + Directory.CreateDirectory(accessDirectory); + long Cost(bool prefixed, int sites) + { + var flag = prefixed ? "true" : "false"; + var header = $$""" + import CIL.Extraction.Scalars + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + namespace IndirectAccesses + open CIL CIL.Extraction + def word : TypeExpr := .atom runtimeScope "System.UInt64" + def width (prefixed : Bool) : Nat := if prefixed then 6 else 5 + def bytes (prefixed : Bool) : Nat := if prefixed then 10 else 7 + def segment (prefixed : Bool) (index : Nat) : List RawInstruction := + let base := index * bytes prefixed + let copy := base + bytes prefixed - 4 + [⟨base, 2, .none⟩, ⟨base + 1, 2, .none⟩, ⟨base + 2, 2, .none⟩] ++ + (if prefixed then [⟨base + 3, 65042, .integer 1⟩] else []) ++ + [⟨copy, 65047, .none⟩, ⟨copy + 2, 43, .target ((index + 1) * width prefixed)⟩] + def raw (prefixed : Bool) (sites : Nat) : RawMethod := { + token := 1, owner := "Scaling", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [word], locals := [], initLocals := true, maxStack := 3, + genericParameters := [], + code := (List.range sites).flatMap (segment prefixed) ++ + [⟨sites * bytes prefixed, 42, .none⟩] } + def featureSegment (prefixed : Bool) : FeatureCertificate := + [some ⟨[], []⟩, some ⟨[none], []⟩, some ⟨[none, none], []⟩] ++ + (if prefixed then [some ⟨[none, none, none], []⟩] else []) ++ + [some ⟨[none, none, none], []⟩, some ⟨[], []⟩] + def features (prefixed : Bool) (sites : Nat) : FeatureCertificate := + (List.replicate sites (featureSegment prefixed)).flatten ++ [some ⟨[], []⟩] + def stateSegment (prefixed : Bool) : ScalarCertificate := + [some [], some [.word64], some [.word64, .word64]] ++ + (if prefixed then [some [.word64, .word64, .word64]] else []) ++ + [some [.word64, .word64, .word64], some []] + def states (prefixed : Bool) (sites : Nat) : ScalarCertificate := + (List.replicate sites (stateSegment prefixed)).flatten ++ [some []] + def codeSegment (prefixed : Bool) (index : Nat) : List Instruction := + [.ldarg 0, .ldarg 0, .ldarg 0] ++ (if prefixed then [.nop] else []) ++ + [.cpblk (if prefixed then 1 else 8), .br ((index + 1) * width prefixed)] + def code (prefixed : Bool) (sites : Nat) : List Instruction := + (List.range sites).flatMap (codeSegment prefixed) ++ [.ret] + """ + "\n"; + var statement = $"FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw {flag} {sites}) .scalar " + + $"(features {flag} {sites}) ((features {flag} {sites}).map Option.isSome) (code {flag} {sites}) ∧ " + + $"ScalarFlowValid (raw {flag} {sites}) (features {flag} {sites}) (states {flag} {sites}) " + + $"(code {flag} {sites})"; + var path = Path.Combine(accessDirectory, $"Blocks{(prefixed ? "Prefixed" : "")}{sites}.lean"); + return DecideCost(path, header, statement, "IndirectAccesses"); + } + foreach (var prefixed in new[] { false, true }) + { + var label = prefixed ? "unaligned block copies" : "block copies"; + var perSite = new[] { FewAddresses, MoreAddresses, ManyAddresses } + .Select(sites => (Sites: sites, Cost: (double)Cost(prefixed, sites) / sites)).ToArray(); + var measured = string.Join("; ", perSite.Select(run => + string.Create(CultureInfo.InvariantCulture, $"{run.Cost:F0} kernel heartbeats per copy at {run.Sites}"))); + if (perSite.Skip(1).Any(run => run.Cost > perSite[0].Cost * AllowedGrowth)) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Checking the feature and scalar certificates of {label} costs {measured}; the limit is " + + $"{AllowedGrowth} times the cost per copy at {FewAddresses}.")); + Console.WriteLine($"PASS checking the feature and scalar certificates of {label} costs {measured}"); + } +} + +// The kernel heartbeats of the feature certificate of a body that loads, stores and addresses four int32 locals in +// turn, popping each address, less those of stating the same certificate. A local load, store or address checks +// its opcode and index alone. +long LocalAccessCost(int accesses) +{ + var cycles = accesses / 4; + var header = $$""" + import CIL.Extraction.Features + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + namespace LocalAccesses + open CIL CIL.Extraction + def intType : TypeExpr := .atom runtimeScope "System.Int32" + def localCount : Nat := 4 + def segment (index : Nat) : List RawInstruction := + [⟨0, 17, .local (index % localCount)⟩, ⟨0, 19, .local ((index + 1) % localCount)⟩, + ⟨0, 18, .local ((index + 2) % localCount)⟩, ⟨0, 38, .none⟩] + def raw (cycles : Nat) : RawMethod := { + token := 1, owner := "Scaling", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [], locals := List.replicate localCount intType, initLocals := true, + maxStack := 1, genericParameters := [], + code := (List.range cycles).flatMap segment ++ [⟨0, 42, .none⟩] } + def unknown : List (Nat × W32) := [] + def features (cycles : Nat) : FeatureCertificate := + (List.replicate cycles [some ⟨[], unknown⟩, some ⟨[none], unknown⟩, some ⟨[], unknown⟩, + some ⟨[none], unknown⟩]).flatten ++ [some ⟨[], unknown⟩] + def codeSegment (index : Nat) : List Instruction := + [.ldloc (index % localCount), .stloc ((index + 1) % localCount), + .ldloca ((index + 2) % localCount), .pop] + def code (cycles : Nat) : List Instruction := (List.range cycles).flatMap codeSegment ++ [.ret] + """ + "\n"; + var statement = $"FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw {cycles}) .scalar " + + $"(features {cycles}) (List.replicate {accesses + 1} true) (code {cycles})"; + var path = Path.Combine(directory, $"LocalAccesses{accesses}.lean"); + return DecideCost(path, header, statement, "LocalAccesses"); +} +var perAccess = new[] { FewLocalAccesses, MoreLocalAccesses, ManyLocalAccesses } + .Select(accesses => (Accesses: accesses, Cost: (double)LocalAccessCost(accesses) / accesses)).ToArray(); +var accessesMeasured = string.Join("; ", perAccess.Select(run => + string.Create(CultureInfo.InvariantCulture, + $"{run.Cost:F0} kernel heartbeats per instruction at {run.Accesses}"))); +if (perAccess.Skip(1).Any(run => run.Cost > perAccess[0].Cost * AllowedGrowth)) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Checking the feature certificate of local loads, stores and addresses costs {accessesMeasured}; " + + $"the limit is {AllowedGrowth} times the cost per instruction at {FewLocalAccesses}.")); +Console.WriteLine( + $"PASS checking the feature certificate of local loads, stores and addresses costs {accessesMeasured}"); +// The kernel heartbeats of a check over a body that stores each of its locals once and then loads or addresses +// it, less those of stating the same check. Integer locals take a constant and are popped after a load or after +// `ldloca`; native-int locals take a method address and are +// consumed by `calli`, which makes each a pointer local. The check is the scalar certificate, or for pointer +// locals also the closed argument storage. Instructions carry byte offsets. +long LocalSlotCost(LocalSlots slots, int count, bool storage = false) +{ + var pointers = slots == LocalSlots.Pointer; + var addressed = slots == LocalSlots.Addressed; + var slotType = pointers ? "System.IntPtr" : "System.Int32"; + var kind = pointers ? ".methodPointer signature" : ".word32"; + var push = pointers ? "65030, .call handler" : "22, .none"; + var load = addressed ? "18, .local index" : "17, .local index"; + var consume = pointers ? "41, .indirectCall .void [] false false 0" : "38, .none"; + // ldftn and calli carry a four-byte token; ldc.i4.0 and pop are one byte, and the local forms two. + var pushBytes = pointers ? 6 : 1; + var consumeBytes = pointers ? 5 : 1; + var loaded = addressed ? ".reference" : "kind"; + const string identity = """{ owner := "T::Scaling", token := 2, arguments := [] }"""; + var pushed = pointers ? $".ldftn {identity} signature" : ".ldc_i4 0"; + var emittedLoad = addressed ? ".ldloca index" : ".ldloc index"; + var consumed = pointers ? ".calli signature false" : ".pop"; + var header = $$""" + import CIL.Extraction.Scalars + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + namespace LocalSlots + open CIL CIL.Extraction + def slotType : TypeExpr := .atom runtimeScope "{{slotType}}" + def handler : CallSite := + { declared := { convention := 0 }, method := { + owner := .atom "T" "Scaling", name := name!"", hasThis := false, genericArity := 0, + returnType := .void, parameters := [] }, + target := .managed 2, typeArguments := [], parameterCount := 0, returnsValue := false } + def signature : ManagedSignature := ⟨.void, []⟩ + def kind : ScalarKind := {{kind}} + def bytes : Nat := {{pushBytes + 4 + consumeBytes}} + def segment (index : Nat) : List RawInstruction := + let base := index * bytes + [⟨base, {{push}}⟩, ⟨base + {{pushBytes}}, 19, .local index⟩, ⟨base + {{pushBytes + 2}}, {{load}}⟩, + ⟨base + {{pushBytes + 4}}, {{consume}}⟩] + def raw (slots : Nat) : RawMethod := { + token := 1, owner := "Scaling", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [], locals := List.replicate slots slotType, initLocals := true, maxStack := 1, + genericParameters := [], + code := (List.range slots).flatMap segment ++ [⟨slots * bytes, 42, .none⟩] } + def features (slots : Nat) : FeatureCertificate := + (List.replicate slots [some ⟨[], []⟩, some ⟨[none], []⟩, some ⟨[], []⟩, some ⟨[none], []⟩]).flatten ++ + [some ⟨[], []⟩] + def states (slots : Nat) : ScalarCertificate := + (List.replicate slots [some [], some [kind], some [], some [{{loaded}}]]).flatten ++ [some []] + def codeSegment (index : Nat) : List Instruction := + [{{pushed}}, .stloc index, {{emittedLoad}}, {{consumed}}] + def code (slots : Nat) : List Instruction := (List.range slots).flatMap codeSegment ++ [.ret] + """ + "\n"; + var statement = storage ? $"(closedArguments [] (raw {count})).isSome = true" + : $"ScalarFlowValid (raw {count}) (features {count}) (states {count}) (code {count})"; + var path = Path.Combine(directory, $"{slots}{(storage ? "Storage" : "Locals")}{count}.lean"); + return DecideCost(path, header, statement, "LocalSlots"); +} +(LocalSlots Slots, bool Storage, string Label)[] localChecks = +[ + (LocalSlots.Integer, false, "the scalar certificate of integer locals"), + (LocalSlots.Addressed, false, "the scalar certificate of addressed integer locals"), + (LocalSlots.Pointer, false, "the scalar certificate of native-int locals consumed by calli"), + (LocalSlots.Pointer, true, "the closed argument storage of native-int locals consumed by calli"), +]; +foreach (var (slots, storage, label) in localChecks) +{ + // Each local is stored once and loaded or addressed once, between a push and a pop or call; `ret` ends the body. + var perInstruction = new[] { FewLocals, MoreLocals, ManyLocals } + .Select(count => (Count: count, Cost: (double)LocalSlotCost(slots, count, storage) / (4 * count + 1))) + .ToArray(); + var localsMeasured = string.Join("; ", perInstruction.Select(run => string.Create(CultureInfo.InvariantCulture, + $"{run.Cost:F0} kernel heartbeats per instruction at {run.Count} locals"))); + if (perInstruction.Skip(1).Any(run => run.Cost > perInstruction[0].Cost * AllowedLocalGrowth)) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Checking {label} costs {localsMeasured}; the limit is {AllowedLocalGrowth} " + + $"times the cost per instruction at {FewLocals} locals.")); + Console.WriteLine($"PASS checking {label} costs {localsMeasured}"); +} + +// The kernel heartbeats of the feature certificate of a body of fixed length over `count` int32 locals, less those of +// stating the same certificate. Each segment loads, stores and addresses a local, then addresses the argument and +// stores a constant into it, so the local and argument sites of the check are all exercised. +long FeatureLocalCost(int count) +{ + var header = $$""" + import CIL.Extraction.Features + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + namespace FeatureLocals + open CIL CIL.Extraction + def intType : TypeExpr := .atom runtimeScope "System.Int32" + def localCount : Nat := {{count}} + def segment (index : Nat) : List RawInstruction := + [⟨0, 17, .local (index % localCount)⟩, ⟨0, 19, .local ((index + 1) % localCount)⟩, + ⟨0, 18, .local ((index + 2) % localCount)⟩, ⟨0, 38, .none⟩, ⟨0, 15, .argument 0⟩, ⟨0, 38, .none⟩, + ⟨0, 22, .none⟩, ⟨0, 16, .argument 0⟩] + def raw (segments : Nat) : RawMethod := { + token := 1, owner := "Scaling", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [intType], locals := List.replicate localCount intType, initLocals := true, + maxStack := 1, genericParameters := [], + code := (List.range segments).flatMap segment ++ [⟨0, 42, .none⟩] } + def unknown : List (Nat × W32) := [] + def features (segments : Nat) : FeatureCertificate := + (List.replicate segments [some ⟨[], unknown⟩, some ⟨[none], unknown⟩, some ⟨[], unknown⟩, + some ⟨[none], unknown⟩, some ⟨[], unknown⟩, some ⟨[none], unknown⟩, some ⟨[], unknown⟩, + some ⟨[some 0], unknown⟩]).flatten ++ [some ⟨[], unknown⟩] + def codeSegment (index : Nat) : List Instruction := + [.ldloc (index % localCount), .stloc ((index + 1) % localCount), + .ldloca ((index + 2) % localCount), .pop, .ldarga 0, .pop, .ldc_i4 0, .starg 0] + def code (segments : Nat) : List Instruction := (List.range segments).flatMap codeSegment ++ [.ret] + """ + "\n"; + var statement = $"FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw {FeatureLocalSegments}) .scalar " + + $"(features {FeatureLocalSegments}) (List.replicate {FeatureLocalInstructions} true) " + + $"(code {FeatureLocalSegments})"; + var path = Path.Combine(directory, $"FeatureLocals{count}.lean"); + return DecideCost(path, header, statement, "FeatureLocals"); +} +var perFeatureInstruction = new[] { FewFeatureLocals, MoreFeatureLocals, ManyFeatureLocals } + .Select(count => (Count: count, Cost: (double)FeatureLocalCost(count) / FeatureLocalInstructions)).ToArray(); +var featureLocalsMeasured = string.Join("; ", perFeatureInstruction.Select(run => string.Create( + CultureInfo.InvariantCulture, $"{run.Cost:F0} kernel heartbeats per instruction at {run.Count} locals"))); +if (perFeatureInstruction.Skip(1).Any(run => run.Cost > perFeatureInstruction[0].Cost * AllowedLocalGrowth)) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Checking the feature certificate of {FeatureLocalInstructions} instructions costs " + + $"{featureLocalsMeasured}; the limit is {AllowedLocalGrowth} times the cost per instruction at " + + $"{FewFeatureLocals} locals.")); +Console.WriteLine($"PASS checking the feature certificate of {FeatureLocalInstructions} instructions costs " + + featureLocalsMeasured); + +// One literal certificate state, written as the extractor writes states, so a fact costs only what the check costs. +static string KnownFactState(IEnumerable stack, IEnumerable words) => + $"some ⟨[{string.Join(", ", stack.Select(word => $"some {word}"))}], " + + $"[{string.Join(", ", words.Select((word, index) => $"({index}, {word})"))}]⟩"; + +// The literal certificate of the body `KnownFactCost` checks: round `round` stores the word `round + 1`. +static string KnownFactCertificate(int facts, int rounds) +{ + var states = new List(); + for (var index = 0; index < facts; index++) + { + var stored = new int[index]; + states.Add(KnownFactState([], stored)); + states.Add(KnownFactState([0], stored)); + } + var words = new int[facts]; + for (var round = 0; round < rounds; round++) + { + states.Add(KnownFactState([], words)); + states.Add(KnownFactState([round + 1], words)); + words[round % facts] = round + 1; + states.Add(KnownFactState([], words)); + states.Add(KnownFactState([round + 1], words)); + } + states.Add(KnownFactState([], words)); + return string.Join(",\n ", states); +} + +// The kernel heartbeats of the certificate of a body storing zero into `facts` of `ManyKnownFacts` int32 locals and +// then running `rounds` rounds that each store into one of them in turn and load it, less those of stating it. +long KnownFactCost(int facts, int rounds) +{ + var header = $$""" + import CIL.Extraction.Features + set_option maxRecDepth 100000 + set_option maxHeartbeats 0 + namespace KnownFacts + open CIL CIL.Extraction + def intType : TypeExpr := .atom runtimeScope "System.Int32" + def facts : Nat := {{facts}} + def rounds : Nat := {{rounds}} + def raw : RawMethod := { + token := 1, owner := "Scaling", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [], locals := List.replicate {{ManyKnownFacts}} intType, initLocals := true, + maxStack := 1, genericParameters := [], + code := (List.range facts).flatMap (fun index => [⟨0, 22, .none⟩, ⟨0, 19, .local index⟩]) ++ + (List.range rounds).flatMap (fun round => [⟨0, 32, .integer (round + 1 : Nat)⟩, + ⟨0, 19, .local (round % facts)⟩, ⟨0, 17, .local (round % facts)⟩, ⟨0, 38, .none⟩]) ++ + [⟨0, 42, .none⟩] } + def features : FeatureCertificate := [ + {{KnownFactCertificate(facts, rounds)}}] + def code : List Instruction := + (List.range facts).flatMap (fun index => [.ldc_i4 0, .stloc index]) ++ + (List.range rounds).flatMap (fun round => + [.ldc_i4 (BitVec.ofNat 32 (round + 1)), .stloc (round % facts), .ldloc (round % facts), .pop]) ++ + [.ret] + """ + "\n"; + var statement = $"FeatureFlowValid #[] (fun _ => none) (fun _ => none) raw .scalar features " + + $"(List.replicate {2 * facts + 4 * rounds + 1} true) code"; + var path = Path.Combine(directory, $"KnownFacts{facts}-{rounds}.lean"); + return DecideCost(path, header, statement, "KnownFacts"); +} +// Subtract the stores alone, whose states together hold about the square of the facts. +int[] factCounts = [FewKnownFacts, MoreKnownFacts, ManyKnownFacts]; +var factCosts = factCounts + .Select(facts => (double)(KnownFactCost(facts, KnownFactRounds) - KnownFactCost(facts, 0))).ToArray(); +var factIncrementRatio = (factCosts[2] - factCosts[1]) / (factCosts[1] - factCosts[0]); +var perFactState = (factCosts[2] - factCosts[1]) / ((ManyKnownFacts - MoreKnownFacts) * 4.0 * KnownFactRounds); +var factsMeasured = string.Join("; ", factCounts.Zip(factCosts, (facts, cost) => + string.Create(CultureInfo.InvariantCulture, $"{cost:F0} kernel heartbeats at {facts} facts"))) + + string.Create(CultureInfo.InvariantCulture, + $"; increments in the ratio {factIncrementRatio:F2} (linear 4, quadratic 16); {perFactState:F0} per fact " + + $"per state, {perFactState * ManyKnownFacts * 4 * KnownFactRounds:F0} for {ManyKnownFacts} facts in " + + $"{4 * KnownFactRounds} states"); +if (factIncrementRatio > AllowedFactIncrementRatio) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Checking the feature certificate of {KnownFactRounds} rounds of known facts costs {factsMeasured}; the " + + $"limit on the ratio is {AllowedFactIncrementRatio}.")); +Console.WriteLine( + $"PASS checking the feature certificate of {KnownFactRounds} rounds of known facts costs {factsMeasured}"); + +/// How the generated bodies of the local slot checks use each local. +enum LocalSlots +{ + /// An int32 local that takes a constant and is loaded and popped. + Integer, + /// An int32 local that takes a constant and whose address is taken and popped. + Addressed, + /// A native-int local that takes a method address and is loaded by `calli`. + Pointer, +} + +/// +/// A program shape of the closure cost row: the program of each size, and the methods and link callees of the +/// closure of its method 0. +/// +record ClosureShape(string Name, int[] Sizes, Func Program, Func Methods, + Func Edges); + +/// The patterns that read the cost reports and the link output of the fixtures above. +static partial class LookupPatterns +{ + [GeneratedRegex( + @"\bmark ([0-9]+) ([0-9]+) (rfl list|rfl index|kernel list|kernel index|stepper) (start|end) ([0-9]+)")] + internal static partial Regex RunMark { get; } + + [GeneratedRegex(@"\bevidence (calls|assembly) ([0-9]+) heartbeats")] + internal static partial Regex EvidenceCost { get; } + + [GeneratedRegex(@"(?m)^namespace Extracted\r?$")] + internal static partial Regex ExtractedNamespace { get; } + + [GeneratedRegex(@"(?m)^theorem method0_calls_validated ")] + internal static partial Regex CallsTheorem { get; } + + [GeneratedRegex(@"(?m)^theorem method0_evidence_validated ")] + internal static partial Regex EvidenceTheorem { get; } + + [GeneratedRegex(@"closed arguments [0-9]+ [0-9]+ (-?[0-9]+) heartbeats")] + internal static partial Regex ClosedArguments { get; } + + [GeneratedRegex(@"resolution (fact|simplifier|objects) ([0-9]+) heartbeats")] + internal static partial Regex ReturnResolution { get; } + + [GeneratedRegex(@"static identities ([0-9]+) heartbeats")] + internal static partial Regex StaticIdentities { get; } + + [GeneratedRegex(@"call check ([0-9]+) heartbeats")] + internal static partial Regex CallCheck { get; } + + [GeneratedRegex(@"lineage (rejected|located) ([0-9]+) heartbeats")] + internal static partial Regex Lineage { get; } + + [GeneratedRegex(@"([0-9]+) lemmas rechecked, ([0-9]+) kernel heartbeats")] + internal static partial Regex EmbeddingRecheck { get; } + + [GeneratedRegex(@"([0-9]+) kernel heartbeats")] + internal static partial Regex KernelHeartbeats { get; } + + [GeneratedRegex(@"^@\[cil_code\] def method0Link")] + internal static partial Regex LinkLine { get; } + + [GeneratedRegex(@"\b((?:un)?equal [0-9]+) (-?[0-9]+) heartbeats")] + internal static partial Regex TypeDecision { get; } + + [GeneratedRegex(@"\bname spells ([0-9]+) (-?[0-9]+) heartbeats")] + internal static partial Regex MetadataNameCost { get; } + + [GeneratedRegex(@"\bmodel (lookup (?:first|middle|last)|agreement|calls) ([0-9]+) (-?[0-9]+) heartbeats")] + internal static partial Regex ModelTableCost { get; } + + [GeneratedRegex(@"\bplatform ([a-z]+) ([0-9]+) (-?[0-9]+) heartbeats")] + internal static partial Regex PlatformTableCost { get; } + + /// The cost of selecting and checking distinct imported calls. + [GeneratedRegex(@"import links (\d+) (\d+) heartbeats")] + internal static partial Regex ImportLinkCost { get; } + + [GeneratedRegex(@"\b((?:thrownEqual|thrownUnequal|clrUnequal|raisesEqual) [A-Za-z]+) (-?[0-9]+) heartbeats")] + internal static partial Regex ExceptionDecision { get; } + + [GeneratedRegex(@"table step [0-9]+ (-?[0-9]+) heartbeats")] + internal static partial Regex NativeTableStep { get; } + + [GeneratedRegex(@"table load first [0-9]+ (-?[0-9]+) heartbeats")] + internal static partial Regex NativeTableLoadFirst { get; } + + [GeneratedRegex(@"table load last [0-9]+ (-?[0-9]+) heartbeats")] + internal static partial Regex NativeTableLoadLast { get; } + + [GeneratedRegex(@"decode [0-9]+ (-?[0-9]+) heartbeats")] + internal static partial Regex ValueDecoding { get; } + + [GeneratedRegex(@"decoded method [0-9]+ (-?[0-9]+) heartbeats")] + internal static partial Regex DecodedMethod { get; } + + [GeneratedRegex(@"decoded definition [0-9]+ (-?[0-9]+) heartbeats")] + internal static partial Regex DecodedDefinition { get; } + + [GeneratedRegex(@"prefix (body|ascending|descending|shuffled) [0-9]+ (-?[0-9]+) heartbeats")] + internal static partial Regex PrefixEntry { get; } + + [GeneratedRegex(@"decoded program [0-9]+ (-?[0-9]+) heartbeats")] + internal static partial Regex DecodedProgram { get; } + + [GeneratedRegex(@"\b(list )?closure (chain|longchain|fan|tail) ([0-9]+) (-?[0-9]+) heartbeats")] + internal static partial Regex ClosureCost { get; } + + [GeneratedRegex(@"\bmark index ([0-9]+) (start|end|equations|lookups) ([0-9]+)")] + internal static partial Regex HeartbeatMark { get; } + + [GeneratedRegex(@"(?m)^set_option maxRecDepth [0-9]+")] + internal static partial Regex RecursionDepth { get; } + + [GeneratedRegex(@"(?m)^cil_instruction_equations method0\r?$")] + internal static partial Regex FirstEquations { get; } + + [GeneratedRegex(@"_instruction[0-9]+' depends on axioms: \[propext\]")] + internal static partial Regex EquationAxioms { get; } + + [GeneratedRegex("""\(\d+, \.text "[^"]*"\)""")] + internal static partial Regex StringToken { get; } + + [GeneratedRegex(@"\(\d+, \.field ")] + internal static partial Regex FieldToken { get; } + + [GeneratedRegex(@"\(\d+, \.call ")] + internal static partial Regex CallToken { get; } + + [GeneratedRegex("""def method(\d+)Signature : String := "[^"]*?Bodies::(?[^"(]*)\(""")] + internal static partial Regex BodySignature { get; } + + const string ProofTerm = """(?:⟨[^\r\n]*⟩|(?by decide \+kernel))"""; + + /// A one-line theorem proved by a constructor or, in its decision group, by the kernel. + [GeneratedRegex($"""(?m)^theorem (?[\w?!']+) : (?[^\r\n]*?) := {ProofTerm}\r?\n""")] + internal static partial Regex ProvedObligation { get; } +} diff --git a/Tests/LoopDiagnostics.lean b/Tests/LoopDiagnostics.lean index 8f9d2cece..ca7697975 100644 --- a/Tests/LoopDiagnostics.lean +++ b/Tests/LoopDiagnostics.lean @@ -1,6 +1,7 @@ import CIL.Safety.LoopScaffold import CIL.Safety.Execution import CIL.Observe +import Tests.Support /-! Loop stepping over a struct local, and over an int local and a call through a contract with a footprint in a caller buffer, with the facts gathered by default, and the failures that name @@ -14,19 +15,42 @@ open CIL (W32) /-- A struct of two 32-bit fields: `count` at offset 0 and `sum` at offset 4. -/ def layout : CIL.AggregateLayout := ⟨8, [⟨0, 4, false⟩, ⟨4, 4, false⟩]⟩ +/-- The type token for the two-field loop counter. -/ +def tallyType : CIL.Extraction.TypeExpr := .aggregate "Tally" 0 + +/-- The storage metadata for the counter's two words. -/ +private def tallyEnvironment : CIL.LayoutEnvironment := { + rawLayouts := [{ + identity := "Tally", explicit := false, packing := 0, declaredSize := 0 + fields := [⟨.unsigned32, none⟩, ⟨.unsigned32, none⟩] }] + layouts := #[⟨layout, 4⟩] } + +def tallyCount : CIL.Extraction.FieldRef := + { owner := tallyType, index := 0, name := name!"Count", + type := .atom CIL.Extraction.runtimeScope "System.UInt32" } + +def tallySum : CIL.Extraction.FieldRef := + { owner := tallyType, index := 1, name := name!"Sum", + type := .atom CIL.Extraction.runtimeScope "System.UInt32" } + +cil_field_shape tallyEnvironment tallyCount +cil_field_shape tallyEnvironment tallySum + /-- The layout's fields are in bounds and apart. -/ theorem layout_valid : layout.Valid := by decide +kernel /-- `for (t = default; t.count != value; t.count++) t.sum = addTwo(t.sum); return t.sum;` -/ @[cil_code] def tally : CIL.Method := { returnsValue := true, locals := [.aggregate (List.replicate 8 0)], localKinds := [.aggregate 8] - code := [.localAddr 0, .initAggregate 8, .branch 14, .localAddr 0, .local 0, .fieldAt 4 4, - .call 1 1, .setFieldAt 4 4, .localAddr 0, .local 0, .fieldAt 0 4, .const32 1, .add, - .setFieldAt 0 4, .local 0, .fieldAt 0 4, .arg 0, .bne 3, .local 0, .fieldAt 4 4, .ret] } + environment := tallyEnvironment + code := [.ldloca 0, .initobj tallyType, .br 14, .ldloca 0, .ldloc 0, .ldfld tallySum, + .call (Tests.callee 1) {}, .stfld tallySum, .ldloca 0, .ldloc 0, .ldfld tallyCount, .ldc_i4 1, .add, + .stfld tallyCount, .ldloc 0, .ldfld tallyCount, .ldarg 0, .bne_un 3, .ldloc 0, .ldfld tallySum, .ret] + links := [(6, .direct 1)] } /-- `return x + 2;` -/ @[cil_code] def addTwo : CIL.Method := { - returnsValue := true, locals := [], code := [.arg 0, .const32 2, .add, .ret] } + returnsValue := true, locals := [], code := [.ldarg 0, .ldc_i4 2, .add, .ret] } /-- The program, with `tally` calling `addTwo` as method 1. -/ @[cil_code] def program : CIL.Program := [tally, addTwo] @@ -57,7 +81,7 @@ abbrev State (memory : Memory) (home : Reference) := /-- The loop test, with the input and `count` on the stack. -/ bind_cutpoint header (memory : Memory) (value : W32) (home : Reference) (state : State memory home) := - tally matching (fun op => op matches .bne _) with entry.frame memory home, + tally matching (fun op => op matches .bne_un _) with entry.frame memory home, [.scalar (.i32 value), .scalar (.i32 state.values.1)], state.memory /-- The frame lemmas compose by hand: the struct's zeroing and stores are named, the call is @@ -73,6 +97,7 @@ theorem doubles (memory : Memory) (value : W32) (wellFormed : memory.WellFormed) (fun state => (value - state.values.1).toNat) (fun start => ?_) ?_ · obtain ⟨zeroed, zeroing, frame, -⟩ := (enter.frame []).zero_aggregate 0 layout [.i32 0, .i32 0] + have zeroTally := zeroing tally tallyType rfl exact loop_reaches_terminates (target := header memory value home ⟨(0, 0), zeroed, frame⟩) (by cil_loop_steps) (start _ rfl) rintro ⟨⟨count, sum⟩, current, frame⟩ doubled recurse @@ -140,6 +165,42 @@ theorem doubles_by_default (memory : Memory) (value : W32) (wellFormed : memory. bv_omega #print axioms doubles_by_default +/-- A contract held by a structure whose parameters name it, as a decoded summary holds one. -/ +structure HeldContract (contract : MethodContract W32 W32) (index : Nat) : Prop where + /-- The contract of the method at `index`. -/ + holds : contract.Holds program index + +/-- `cil_loop_call` reads the contract of a projection, whose type names it through the + structure's parameters, and unfolds its footprint as for a named contract proof. -/ +theorem doubles_through_projection (held : HeldContract addTwoContract 1) (memory : Memory) + (value : W32) (wellFormed : memory.WellFormed) : + ∃ fuel final, invoke program fuel 0 [.scalar (.i32 value)] memory = + .ok (final, [.scalar (.i32 (2 * value))]) ∧ + MemoryBelow memory.nextIdentity memory final := by + obtain ⟨home, entered, enter⟩ := entry.checked memory value wellFormed + refine invoke_loop_induction (by rfl) rfl enter.setup (header memory value home) + (fun state => state.values.2 = 2 * state.values.1) + (fun state => (value - state.values.1).toNat) (fun start => ?_) ?_ + · cil_loop_recurse start (0, 0) + rfl + rintro ⟨⟨count, sum⟩, current, frame⟩ doubled recurse + dsimp only at doubled + by_cases done : count = value + · subst done + rw [← doubled] + cil_loop_finish + · cil_loop_advance + cil_loop_call held.holds sum + cil_loop_recurse recurse (count + 1, sum + 2) + case measure => + show (value - (count + 1)).toNat < (value - count).toNat + bv_omega + case invariant => + show sum + 2 = 2 * (count + 1) + rw [doubled] + bv_omega +#print axioms doubles_through_projection + -- Loop values that are not the reached head state's are refused, showing the reached state. /-- error: cil_loop_recurse: the reached state is not the head of recurse at -/ #guard_msgs (whitespace := lax, substring := true) in @@ -153,7 +214,7 @@ example (memory : Memory) (value count sum : W32) (home : Reference) (current : cil_loop_recurse recurse (count + 1, sum) -- A store whose home has no frame fact at its memory stops the walk at that store. -/-- error: cil_loop_recurse: cannot step pc 7 (CIL.Op.setFieldAt 4 4) at memory +/-- error: cil_loop_recurse: cannot step pc 7 (CIL.Instruction.stfld tallySum) at memory final -/ #guard_msgs (whitespace := lax, substring := true) in example (memory : Memory) (value sum : W32) (home : Reference) (final : Memory) @@ -165,7 +226,7 @@ example (memory : Memory) (value sum : W32) (home : Reference) (final : Memory) cil_loop_recurse recurse (0, sum + 2) -- A contract applied before the loop reaches its call names the instruction it is at. -/-- error: cil_loop_call: pc 3 is CIL.Op.localAddr 0, not a call; step to the call first with +/-- error: cil_loop_call: pc 3 is CIL.Instruction.ldloca 0, not a call; step to the call first with `cil_loop_advance` -/ #guard_msgs (whitespace := lax) in example (memory : Memory) (value count sum : W32) (home : Reference) (current : Memory) @@ -176,7 +237,8 @@ example (memory : Memory) (value count sum : W32) (home : Reference) (current : cil_loop_call addTwo_holds sum -- A call stops `cil_loop_steps`, which names the pc and the tactic that crosses it. -/-- error: cil_loop_steps: pc 6 is a call (CIL.Op.call 1 1); cross it with `cil_loop_call` -/ +/-- error: cil_loop_steps: pc 6 is a call (CIL.Instruction.call (callee 1)); cross it with +`cil_loop_call` -/ #guard_msgs (whitespace := lax, substring := true) in example (memory : Memory) (value count sum : W32) (home : Reference) (current : Memory) (frame : LoopFrame [] memory current [.aggregate 8] [.bytes (.aggregate 8) home] @@ -187,7 +249,7 @@ example (memory : Memory) (value count sum : W32) (home : Reference) (current : -- A store with no frame fact at its memory fails naming the pc, the instruction and the -- memory, and what to supply. -/-- error: cil_loop_steps: cannot step pc 7 (CIL.Op.setFieldAt 4 4) at memory +/-- error: cil_loop_steps: cannot step pc 7 (CIL.Instruction.stfld tallySum) at memory final -/ #guard_msgs (whitespace := lax, substring := true) in example (memory : Memory) (value sum : W32) (home : Reference) (final : Memory) : @@ -202,6 +264,7 @@ example (memory : Memory) (value sum : W32) (home : Reference) (final : Memory) example (memory : Memory) (value : W32) (wellFormed : memory.WellFormed) : True := by obtain ⟨home, entered, enter⟩ := entry.checked memory value wellFormed obtain ⟨zeroed, zeroing, frame, -⟩ := (enter.frame []).zero_aggregate 0 layout [.i32 0, .i32 0] + have zeroTally := zeroing tally tallyType rfl have : LoopReaches program 0 [.scalar (.i32 value)] ⟨0, entry.frame memory home, [], entered⟩ (header memory (value + 1) home ⟨(0, 0), zeroed, frame⟩) := by cil_loop_steps @@ -229,13 +292,43 @@ example (memory current : Memory) (home : Reference) -- A by-value read of a field narrower than the value it lists cannot name the value. /-- error: could not synthesize default value for parameter 'exact' using tactics -/ #guard_msgs (substring := true) in -example := step_fieldAt_encode layout_valid 0 [.i64 0, .i32 0] +example := step_ldfld_encode layout_valid 0 [.i64 0, .i32 0] /-- A struct of a flag byte at 0, a signed 16-bit count at 2, a 32-bit total at 4 and a nested 8-byte struct at 8. -/ def mixed : CIL.AggregateLayout := ⟨16, [⟨0, 1, false⟩, ⟨2, 2, false⟩, ⟨4, 4, false⟩, ⟨8, 8, true⟩]⟩ +/-- The storage metadata for the mixed local and its nested counter. -/ +private def mixedEnvironment : CIL.LayoutEnvironment := { + rawLayouts := tallyEnvironment.rawLayouts ++ [{ + identity := "Mixed", explicit := false, packing := 0, declaredSize := 0 + fields := [⟨.unsigned8, none⟩, ⟨.signed16, none⟩, ⟨.unsigned32, none⟩, ⟨.aggregate 0, none⟩] }] + layouts := tallyEnvironment.layouts.push ⟨mixed, 4⟩ } + +def mixedFlag : CIL.Extraction.FieldRef := + { owner := .aggregate "Mixed" 1, index := 0, name := name!"Flag", + type := .atom CIL.Extraction.runtimeScope "System.Byte" } + +def mixedCount : CIL.Extraction.FieldRef := + { owner := .aggregate "Mixed" 1, index := 1, name := name!"Count", + type := .atom CIL.Extraction.runtimeScope "System.Int16" } + +def mixedTotal : CIL.Extraction.FieldRef := + { owner := .aggregate "Mixed" 1, index := 2, name := name!"Total", + type := .atom CIL.Extraction.runtimeScope "System.UInt32" } + +cil_field_shape mixedEnvironment mixedFlag +cil_field_shape mixedEnvironment mixedCount +cil_field_shape mixedEnvironment mixedTotal + +/-- Both fixtures use the recomputed layouts of the local and its nested fields. -/ +theorem loop_layouts_valid : + CIL.Extraction.LayoutsValid tallyEnvironment.rawLayouts tallyEnvironment.layouts ∧ + CIL.Extraction.LayoutsValid mixedEnvironment.rawLayouts mixedEnvironment.layouts := by + decide +kernel +#print axioms loop_layouts_valid + /-- The layout's fields are in bounds and apart. -/ theorem mixed_valid : mixed.Valid := by decide +kernel @@ -247,8 +340,9 @@ example : mixed.zeros = [.i32 0, .i32 0, .i32 0, .aggregate (List.replicate 8 0) the signed `count` through the struct's address. -/ @[cil_code] def flagged : CIL.Method := { returnsValue := true, locals := [.aggregate (List.replicate 16 0)], localKinds := [.aggregate 16] - code := [.localAddr 0, .initAggregate 16, .branch 6, .localAddr 0, .const32 1, .setFieldAt 0 1, - .local 0, .fieldAt 0 1, .brzero 3, .localAddr 0, .fieldAt 2 2 true, .ret] } + environment := mixedEnvironment + code := [.ldloca 0, .initobj (.aggregate "Mixed" 1), .br 6, .ldloca 0, .ldc_i4 1, + .stfld mixedFlag, .ldloc 0, .ldfld mixedFlag, .brfalse 3, .ldloca 0, .ldfld mixedCount, .ret] } /-- The program of `flagged` alone. -/ @[cil_code] def flaggedProgram : CIL.Program := [flagged] @@ -265,7 +359,7 @@ abbrev Flagged (memory : Memory) (home : Reference) := /-- The loop test, with nothing on the stack. -/ bind_cutpoint flaggedHeader (memory : Memory) (home : Reference) (state : Flagged memory home) := - flagged matching (fun op => op matches .local _) with flaggedEntry.frame memory home, [], + flagged matching (fun op => op matches .ldloc _) with flaggedEntry.frame memory home, [], state.memory /-- `cil_loop_recurse` zeroes a struct with 1-, 2- and 4-byte and nested fields through its @@ -313,28 +407,34 @@ example (memory current : Memory) (home : Reference) -- A narrow read of a 4-byte field is refused; that field reads back exactly. /-- error: could not synthesize default value for parameter 'narrow' using tactics -/ #guard_msgs (substring := true) in -example := step_fieldAt_encode_narrow mixed_valid 2 +example := step_ldfld_encode_narrow mixed_valid 2 [.i32 0, .i32 0, .i32 7, .aggregate (List.replicate 8 0)] -- A stored byte reads back truncated to its field: 0x1FF in the flag byte loads as 0xFF, and -- as -1 signed. example (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (memory : Memory) - (rest : List Value) : - step body (.fieldAt 0 1 false) pc args frame (.scalar (.aggregate (mixed.encode + (rest : List Value) (unsigned signed : CIL.Extraction.FieldRef) + (zeroExtended : body.fieldShape unsigned = .value 0 1 false) + (signExtended : body.fieldShape signed = .value 0 1 true) : + step body (.ldfld unsigned) pc args frame (.scalar (.aggregate (mixed.encode [.i32 0x1FF, .i32 0, .i32 0, .aggregate (List.replicate 8 0)])) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 0xFF) :: rest) frame memory) ∧ - step body (.fieldAt 0 1 true) pc args frame (.scalar (.aggregate (mixed.encode + step body (.ldfld signed) pc args frame (.scalar (.aggregate (mixed.encode [.i32 0x1FF, .i32 0, .i32 0, .aggregate (List.replicate 8 0)])) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 (-1)) :: rest) frame memory) := by - constructor <;> rw [step_fieldAt_encode_narrow mixed_valid 0 _] <;> rfl + have read := step_ldfld_encode_narrow mixed_valid 0 + [.i32 0x1FF, .i32 0, .i32 0, .aggregate (List.replicate 8 0)] + exact ⟨read body unsigned false zeroExtended pc args frame memory rest, + read body signed true signExtended pc args frame memory rest⟩ /-- `s = default; while (s.total != value) s.total++; s.flag = 1; return s.flag;`, storing the flag byte on the exit path. -/ @[cil_code] def marked : CIL.Method := { returnsValue := true, locals := [.aggregate (List.replicate 16 0)], localKinds := [.aggregate 16] - code := [.localAddr 0, .initAggregate 16, .branch 9, .localAddr 0, .local 0, .fieldAt 4 4, - .const32 1, .add, .setFieldAt 4 4, .local 0, .fieldAt 4 4, .arg 0, .bne 3, .localAddr 0, - .const32 1, .setFieldAt 0 1, .local 0, .fieldAt 0 1, .ret] } + environment := mixedEnvironment + code := [.ldloca 0, .initobj (.aggregate "Mixed" 1), .br 9, .ldloca 0, .ldloc 0, .ldfld mixedTotal, + .ldc_i4 1, .add, .stfld mixedTotal, .ldloc 0, .ldfld mixedTotal, .ldarg 0, .bne_un 3, .ldloca 0, + .ldc_i4 1, .stfld mixedFlag, .ldloc 0, .ldfld mixedFlag, .ret] } /-- The program of `marked` alone. -/ @[cil_code] def markedProgram : CIL.Program := [marked] @@ -352,7 +452,7 @@ abbrev Marked (memory : Memory) (home : Reference) := /-- The loop test, with the input and `total` on the stack. -/ bind_cutpoint markedHeader (memory : Memory) (value : W32) (home : Reference) (state : Marked memory home) := - marked matching (fun op => op matches .bne _) with markedEntry.frame memory home, + marked matching (fun op => op matches .bne_un _) with markedEntry.frame memory home, [.scalar (.i32 value), .scalar (.i32 state.values)], state.memory /-- `cil_loop_finish` takes a store on the exit path through the frame and returns the stored @@ -427,16 +527,119 @@ open Lean in let env ← getEnv [``entry.checked, ``header].mapM fun name => liftM (findDocString? env name : IO _) +/-- The signed element offset of the loop's reference to an Int32. -/ +private def stampOffset : CIL.Extraction.MethodRef := + let word := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.Int32" + { owner := CIL.Extraction.unsafeOwner, name := name!"Add", hasThis := false, + genericArity := 1, methodArguments := [word], parameters := [.byref word, word], + returnType := .byref word } + /-- `for (i = 0; i != n; i++) stamp(Unsafe.Add(ref p, i), i); return i;`, with `i` an int local. -/ @[cil_code] def stamps : CIL.Method := { returnsValue := true, locals := [.i32 0], localKinds := [.word32] - code := [.const32 0, .setLocal 0, .branch 12, .arg 1, .local 0, .memory (.add 4 true), - .local 0, .call 1 2, .local 0, .const32 1, .add, .setLocal 0, .local 0, .arg 0, .bne 3, - .local 0, .ret] } + code := [.ldc_i4 0, .stloc 0, .br 12, .ldarg 1, .ldloc 0, .call stampOffset {}, + .ldloc 0, .call (Tests.callee + 2) {}, .ldloc 0, .ldc_i4 1, .add, .stloc 0, .ldloc 0, .ldarg 0, .bne_un 3, + .ldloc 0, .ret], links := [(7, .direct 1)] } + +cil_runtime_models stamps + +/-- Both calls name the native word size getter. Only the second site has a link. -/ +@[cil_code] private def sizeCalls : CIL.Method := + let getter : CIL.Extraction.MethodRef := { + owner := .atom CIL.Extraction.runtimeScope "System.IntPtr", name := name!"get_Size", + hasThis := false, genericArity := 0, parameters := [], + returnType := .atom CIL.Extraction.runtimeScope "System.Int32" } + { returnsValue := true, locals := [], code := [.call getter {}, .call getter {}, .ret], + links := [(1, .direct 1)] } + +cil_runtime_models sizeCalls + +/-- The model leaves its result on the stack at the linked call and preserves frame and memory. -/ +theorem advance_size_call (frame : Frame) (memory : Memory) (post : Memory → List Value → Prop) + (linked : LoopTerminates [sizeCalls, sizeCalls] 0 [] post + ⟨1, frame, [.scalar (.i32 8)], memory⟩) : + LoopTerminates [sizeCalls, sizeCalls] 0 [] post ⟨0, frame, [], memory⟩ := by + cil_loop_advance + exact linked + +#print axioms advance_size_call + +-- A call without a link runs its model, so `cil_loop_call` refuses it and names the tactic that +-- steps it. +/-- error: cil_loop_call: pc 0 has no callee link; step it with `cil_loop_advance` -/ +#guard_msgs (whitespace := lax, substring := true) in +example (frame : Frame) (memory : Memory) (post : Memory → List Value → Prop) (sum : W32) : + LoopTerminates [sizeCalls, sizeCalls] 0 [] post ⟨0, frame, [], memory⟩ := by + cil_loop_call addTwo_holds sum + +/-- A call without a link of a method the runtime table does not model. -/ +@[cil_code] private def modelessCall : CIL.Method := + { returnsValue := false, locals := [], code := [.call (Tests.callee 0) {}, .ret] } + +/-- The modeless call as method 0 of a program. -/ +@[cil_code] private def modelessProgram : CIL.Program := [modelessCall] + +/-- The modeless call after a `nop`, so a run stops at it after one step. -/ +@[cil_code] private def lateModeless : CIL.Method := + { returnsValue := false, locals := [], code := [.nop, .call (Tests.callee 0) {}, .ret] } + +/-- The late modeless call as method 0 of a program. -/ +@[cil_code] private def lateModelessProgram : CIL.Program := [lateModeless] + +-- No step crosses a call with neither a link nor a model, and the walk says so. +/-- error: cil_loop_advance: pc 0 (CIL.Instruction.call (callee 0)) has neither a link nor a +runtime model, so no step crosses it -/ +#guard_msgs (whitespace := lax, substring := true) in +example (frame : Frame) (memory : Memory) (post : Memory → List Value → Prop) : + LoopTerminates [modelessCall] 0 [] post ⟨0, frame, [], memory⟩ := by + cil_loop_advance + +-- The failure record names the same gap instead of a callee contract. +/-- "missing":"linkOrModel" -/ +#guard_msgs (substring := true) in +set_option cil.diagnostics true in +example (frame : Frame) (memory : Memory) (post : Memory → List Value → Prop) : + LoopTerminates modelessProgram 0 [] post ⟨0, frame, [], memory⟩ := by + cil_loop_advance + +-- A run stopped at the modeless call reports the same gap. +/-- "missing":"linkOrModel","obligation":null,"pc":1 -/ +#guard_msgs (substring := true) in +set_option cil.diagnostics true in +example (frame : Frame) (memory : Memory) + (result : Except LocatedFault (Memory × List Value)) : + run lateModelessProgram 100 0 0 [] frame [] memory = result := by + cil_checked_steps 1 + +/-- `new Span(ref value)` without a link, a construction the runtime table models. -/ +@[cil_code] private def spanOfReference : CIL.Method := + let int := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.Int32" + { returnsValue := true, locals := [], code := [.ldarg 0, .newobj { + owner := .apply (.constructor CIL.Extraction.runtimeScope "System.Span`1") int, + name := name!".ctor", hasThis := true, genericArity := 0, returnType := .void, + parameters := [.byref int] }, .ret] } + +cil_runtime_models spanOfReference + +-- The stuck constructor reports its bound span model. +/-- modelStep (some (CIL.RuntimeModel.memory (CIL.MemoryOp.spanSingleReference 4 true))) -/ +#guard_msgs (whitespace := lax, substring := true) in +example (frame : Frame) (memory : Memory) (value : Value) (post : Memory → List Value → Prop) : + LoopTerminates [spanOfReference] 0 [value] post ⟨1, frame, [value], memory⟩ := by + cil_loop_advance + +-- The failure record at the construction reads the model's step, not the raw constructor's. +/-- "missing":"spanValidity","obligation":null,"pc":1 -/ +#guard_msgs (substring := true) in +set_option cil.diagnostics true in +example (frame : Frame) (memory : Memory) (value : Value) (post : Memory → List Value → Prop) : + LoopTerminates [spanOfReference] 0 [value] post ⟨1, frame, [value], memory⟩ := by + cil_loop_advance /-- `*slot = value;` -/ @[cil_code] def stamp : CIL.Method := { - returnsValue := false, locals := [], code := [.arg 0, .arg 1, .storeScalar 4, .ret] } + returnsValue := false, locals := [], code := [.ldarg 0, .ldarg 1, .stind .i4, .ret] } /-- The program, with `stamps` calling `stamp` as method 1. -/ @[cil_code] def stampsProgram : CIL.Program := [stamps, stamp] @@ -455,7 +658,7 @@ theorem stamp_holds : stampContract.Holds stampsProgram 1 := by dsimp only [stampContract] at writable obtain ⟨after, wrote, formed, confined, filled⟩ := writable.write_one value refine MethodContract.Holds.intro after () ?_ confined filled - cil_checked_steps [step_storeScalar_of_write wrote, formed] + cil_checked_steps [step_stind_narrow_of_write .i4 wrote, formed] /-- info: 'Tests.LoopDiagnostics.stamp_holds' depends on axioms: [propext, Quot.sound] -/ #guard_msgs (whitespace := lax) in #print axioms stamp_holds @@ -474,7 +677,7 @@ abbrev Stamping (memory : Memory) (buffer : Reference) (count : Nat) (home : Ref /-- The loop test, with the count and the index on the stack. -/ bind_cutpoint stampsHeader (memory : Memory) (count : W32) (buffer : Reference) (home : Reference) (state : Stamping memory buffer count.toNat home) := - stamps matching (fun op => op matches .bne _) with stampsEntry.frame memory home, + stamps matching (fun op => op matches .bne_un _) with stampsEntry.frame memory home, [.scalar (.i32 count), .scalar (.i32 state.values)], state.memory /-- The index stays within the count, and the rest of the buffer stays writable. -/ @@ -532,8 +735,55 @@ theorem stamps_returns_count (memory : Memory) (count : W32) (buffer : Reference #guard_msgs (whitespace := lax) in #print axioms stamps_returns_count +/-- A theorem may name its own memory `stored`, as the loop tactics name a store's memory: the + walk clears a restated fact only at a memory it introduced, so the caller's write authority + at `stored` stays in scope after the index store restates it at the store's memory. -/ +theorem stamps_keep_caller_facts (stored : Memory) (count : W32) (buffer : Reference) + (wellFormed : stored.WellFormed) (formed : form stored buffer = .ok buffer) + (small : count.toNat < 2 ^ 31) (writable : Writable stored buffer 4 0 count.toNat) : + ∃ fuel final, invoke stampsProgram fuel 0 + [.scalar (.i32 count), .reference (.address buffer)] stored = + .ok (final, [.scalar (.i32 count)]) ∧ + Confined [(buffer, 4 * count.toNat)] stored final := by + obtain ⟨home, entered, enter⟩ := + stampsEntry.checked stored count buffer wellFormed + refine invoke_loop_induction (by rfl) ?_ enter.setup (stampsHeader stored count buffer home) + (StampingInvariant count buffer) (fun state => count.toNat - state.values.toNat) + (fun start => ?_) ?_ + · simp [cil_checked, formed] + · cil_loop_recurse start 0 + exact ⟨by simp, by simpa using writable⟩ + rintro ⟨index, current, frame⟩ ⟨bounded, rest⟩ recurse + dsimp only at bounded rest + by_cases done : index = count + · subst done + cil_loop_finish + · have lower : index.toNat < count.toNat := by + have := BitVec.toNat_ne.mp done + omega + have offset := writable.add_int wellFormed formed (index := index) (by omega) (by omega) + (by omega) + have slot := rest.sub (index := index.toNat) (length := 1) (by omega) (by omega) + cil_loop_advance + cil_loop_call stamp_holds (buffer.element 4 index.toNat, index) + have next : (index + 1).toNat = index.toNat + 1 := by bv_omega + cil_loop_recurse recurse (index + 1) + case invariant => + have _ : Writable stored buffer 4 0 count.toNat := by assumption + refine ⟨show (index + 1).toNat ≤ count.toNat by omega, ?_⟩ + have later := (Writable.append (left := 1)).mp + (show Writable _ buffer 4 index.toNat (1 + (count.toNat - (index.toNat + 1))) by + rwa [show 1 + (count.toNat - (index.toNat + 1)) = count.toNat - index.toNat by omega]) + rw [next] + simpa [Nat.add_comm] using later.2 + case measure => + dsimp only + omega + +#print axioms stamps_keep_caller_facts + -- Without the `Unsafe.Add` result the advance stops at the add, naming that fact. -/-- error: cil_loop_advance: cannot step pc 5 (CIL.Op.memory (CIL.MemoryOp.add 4 true)) -/ +/-- error: cil_loop_advance: cannot step pc 5 (CIL.Instruction.call stampOffset) -/ #guard_msgs (whitespace := lax, substring := true) in example (memory current : Memory) (count index : W32) (buffer home : Reference) (wellFormed : memory.WellFormed) (formed : form memory buffer = .ok buffer) @@ -543,8 +793,8 @@ example (memory current : Memory) (count index : W32) (buffer home : Reference) (fun _ _ => True) ⟨3, stampsEntry.frame memory home, [], current⟩ := by cil_loop_advance --- The stuck step shows its pc as a literal, as the message's first line does. -/-- (CIL.Op.memory (CIL.MemoryOp.add 4 true)) 5 -/ +-- The stuck offset call reports its bound element width and signedness. +/-- modelStep (some (CIL.RuntimeModel.memory (CIL.MemoryOp.add 4 true))) -/ #guard_msgs (whitespace := lax, substring := true) in example (memory current : Memory) (count index : W32) (buffer home : Reference) (wellFormed : memory.WellFormed) (formed : form memory buffer = .ok buffer) @@ -568,10 +818,10 @@ stack -/ open Lean Meta in #eval show MetaM Unit from do logInfo (← loopMissingHint #[mkConst ``True]) -/-- `i = (byte) 0x1FF; return i;`, storing the int local through a byte narrowing. -/ +/-- `i = (byte) 0x1FF; return i;`: the byte local keeps the low byte of the stored word. -/ @[cil_code] def narrowed : CIL.Method := { - returnsValue := true, locals := [.i32 0], localKinds := [.word32] - code := [.const32 0x1FF, .setLocal 0 (some .unsigned8), .local 0, .ret] } + returnsValue := true, locals := [.i32 0], localKinds := [.narrow .unsigned8] + code := [.ldc_i4 0x1FF, .stloc 0, .ldloc 0, .ret] } /-- The program of `narrowed` alone. -/ @[cil_code] def narrowedProgram : CIL.Program := [narrowed] @@ -580,13 +830,11 @@ open Lean Meta in bind_entry narrowedEntry (memory : Memory) (wellFormed : memory.WellFormed) := narrowed with [] from memory allocating wellFormed --- A narrowing local store is not one `LoopFrame.store` models, so the walk stops at it. -/-- error: cil_loop_finish: cannot step pc 1 (CIL.Op.setLocal 0 (some CIL.NarrowKind.unsigned8)) -/ -#guard_msgs (whitespace := lax, substring := true) in +-- A store to a byte home records the truncated word, which the load and the return read back. example (memory current : Memory) (home : Reference) - (frame : LoopFrame [] memory current [.word32] [.bytes .word32 home] + (frame : LoopFrame [] memory current [.narrow .unsigned8] [.bytes (.narrow .unsigned8) home] (knownLocals [some (.i32 0)])) : - LoopTerminates narrowedProgram 0 [] (fun _ _ => True) + LoopTerminates narrowedProgram 0 [] (fun _ results => results = [.scalar (.i32 0xFF)]) ⟨0, narrowedEntry.frame memory home, [], current⟩ := by cil_loop_finish @@ -609,8 +857,8 @@ example (memory current : Memory) (count index : W32) (buffer home slotted : Ref -- A call whose address argument has no `form` fact does not step, and the failure names the -- check and the fact it needs. -/-- error: cil_loop_call: the call at pc 7 (CIL.Op.call 1 2) does not step: its argument checks -reduce to -/ +/-- error: cil_loop_call: the call at pc 7 (CIL.Instruction.call (callee 2)) does not step: its +argument checks reduce to -/ #guard_msgs (whitespace := lax, substring := true) in example (memory current : Memory) (count index : W32) (home slotted : Reference) (frame : LoopFrame [] memory current [.word32] [.bytes .word32 home] @@ -638,8 +886,8 @@ example (memory : Memory) (count : W32) (buffer home : Reference) (current : Mem case postcondition.part3 => exact stored _ -- A call whose stacked argument is not the contract's names both. -/-- error: cil_loop_call: the call at pc 7 (CIL.Op.call 1 2) does not take the contract's -arguments for this input, or is not the named site. -/ +/-- error: cil_loop_call: the call at pc 7 (CIL.Instruction.call (callee 2)) does not take the +contract's arguments for this input, or is not the named site. -/ #guard_msgs (whitespace := lax, substring := true) in example (memory current : Memory) (count index : W32) (buffer home : Reference) (wellFormed : memory.WellFormed) (formed : form memory buffer = .ok buffer) @@ -757,6 +1005,7 @@ macro "walk_to_head" : tactic => `(tactic| ( obtain ⟨home, entered, enter⟩ := entry.checked memory value wellFormed obtain ⟨zeroed, zeroing, frame, -⟩ := (enter.frame []).zero_aggregate 0 layout [.i32 0, .i32 0] + have zeroTally := zeroing tally tallyType rfl have : LoopReaches program 0 [.scalar (.i32 value)] ⟨0, entry.frame memory home, [], entered⟩ (header memory value home ⟨(0, 0), zeroed, frame⟩) := by cil_loop_steps @@ -767,11 +1016,11 @@ macro "walk_to_head" : tactic => `(tactic| ( example (memory : Memory) (value : W32) (wellFormed : memory.WellFormed) : True := by walk_to_head --- A step names each hypothesis its step proof used: the zeroing fact steps `initAggregate`. -/-- [CIL.step] pc 1: CIL.Op.initAggregate (8 % 2 ^ 64) +-- A step names each hypothesis its step proof used: the zeroing fact steps `initobj`. +/-- [CIL.step] pc 1: CIL.Instruction.initobj tallyType source: unknown, since the proof does not import ExtractedSymbols by loop_reaches_next - used zeroing : -/ + used zeroTally : -/ #guard_msgs (whitespace := lax, substring := true) in set_option trace.CIL.step true in example (memory : Memory) (value : W32) (wellFormed : memory.WellFormed) : True := by @@ -779,7 +1028,7 @@ example (memory : Memory) (value : W32) (wellFormed : memory.WellFormed) : True -- A branch uses no hypothesis, so no fact is named although `setup` and `wellFormed` are in -- context; the tactic's summary names the goals it left by obligation tag. -/-- [CIL.step] pc 2: CIL.Op.branch 14 +/-- [CIL.step] pc 2: CIL.Instruction.br 14 source: unknown, since the proof does not import ExtractedSymbols by loop_reaches_next changes pc: 1 + 1 ⟶ 14 -/ @@ -808,7 +1057,9 @@ example (memory : Memory) (value : W32) (frame : Frame) : -- A call tactic applied before the walk reaches a call records the instruction it is at and -- that the call site is missing. -/-- "instruction":"CIL.Op.localAddr 0","method":0,"missing":"callSite","obligation":null,"pc":3 -/ +/-- +"instruction":"CIL.Instruction.ldloca 0","method":0,"missing":"callSite","obligation":null,"pc":3 +-/ #guard_msgs (substring := true) in set_option cil.diagnostics true in example (memory : Memory) (value count sum : W32) (home : Reference) (current : Memory) @@ -848,7 +1099,7 @@ example (memory : Memory) (value count sum : W32) (home : Reference) (current : -- A tactic that stops without an error, leaving a step for the proof, is recorded as stopped -- there, with the instruction it did not take. -/-- "instruction":"CIL.Op.const32 2","method":1,"missing" -/ +/-- "instruction":"CIL.Instruction.ldc_i4 2","method":1,"missing" -/ #guard_msgs (substring := true) in set_option cil.diagnostics true in example (memory : Memory) (value : W32) (frame : Frame) @@ -929,4 +1180,202 @@ example (memory : Memory) (home : Reference) : True := by throwError "The missing-authority diagnostic asserted a violation: {message}" trivial +/-- Two operands added on the stack, then a `nop` at the state the path reaches. -/ +@[cil_code] def addPair : CIL.Method := { + returnsValue := true, locals := [], code := [.ldarg 0, .ldarg 1, .add, .nop] } + +/-- The program holding only `addPair`. -/ +@[cil_code] def addPairProgram : CIL.Program := [addPair] + +-- A fact that is no simp lemma fails the whole-goal rewrite without a message: the walk goes on +-- and the step takes the facts it can use. +#guard_msgs in +example (memory : Memory) (left right : CIL.W64) (frame : Frame) : + LoopReaches addPairProgram 0 [.scalar (.i64 left), .scalar (.i64 right)] + ⟨0, frame, [], memory⟩ ⟨3, frame, [.scalar (.i64 (left + right))], memory⟩ := by + cil_loop_steps [(0 : Nat)] + +open Lean Meta Elab Tactic in +/-- Local reference classification uses the current list and reduces stored slot terms afresh. -/ +theorem localReferenceClassification + (_body : CIL.Method) (_memory : Memory) (_home : Reference) (_tail : List LocalSlot) : + True := by + run_tac withMainContext do + let slotType := mkConst ``LocalSlot + let body ← Term.elabTerm (mkIdent `_body) none + let memory ← Term.elabTerm (mkIdent `_memory) none + let home ← Term.elabTerm (mkIdent `_home) none + let byteSlot := mkApp2 (mkConst ``LocalSlot.bytes) (mkConst ``CIL.LocalKind.word32) home + let spanSlot ← Term.elabTerm (← `(LocalSlot.span 1 true none)) none + let unknownTail ← Term.elabTerm (mkIdent `_tail) none + let makeList (slots : List Expr) := mkListLit slotType slots + let makeStep (locals : Expr) (position : Nat) (load := false) := do + let instruction := mkApp (mkConst (if load then ``CIL.Instruction.ldloc + else ``CIL.Instruction.ldloca)) (mkNatLit position) + let frame ← mkAppM ``Frame.mk #[mkNatLit 0, locals, + ← mkListLit (mkConst ``AllocationId) [], mkNatLit 0] + let values ← mkListLit (mkConst ``Value) [] + let result := mkAppN (mkConst ``step) + #[body, instruction, mkNatLit 0, values, frame, values, memory] + let goal ← mkFreshExprMVar (← mkEq result result) + pure (instruction, goal.mvarId!) + let check (locals : Expr) (position : Nat) (expected : Bool) + (cache : LoopLocalIndex) (load := false) := do + let (instruction, goal) ← makeStep locals position load + let (actual, cache) ← (loopReadsReferences instruction goal).run cache + unless actual == expected do throwError "incorrect local reference classification" + pure cache + let cache ← check (← makeList [byteSlot]) 0 false {} + let cache ← check (← makeList [spanSlot]) 0 true cache + let cache ← check (← makeList [byteSlot]) 1 true cache + let partialList := mkApp3 (mkConst ``List.cons [Level.zero]) slotType byteSlot unknownTail + let cache ← check partialList 0 false cache + let _ ← check partialList 1 true cache + let unknownSlot ← mkFreshExprMVar slotType + let locals ← makeList [unknownSlot] + let cache ← check locals 0 true {} + unknownSlot.mvarId!.assign byteSlot + let _ ← check locals 0 false cache + withLetDecl `slot slotType byteSlot fun slot => do + let locals ← makeList [slot] + let cache ← check locals 0 false {} + let declaration ← slot.fvarId!.getDecl + let changed := (← getLCtx).modifyLocalDecl declaration.fvarId + (fun declaration => declaration.setValue spanSlot) + withLCtx changed (← getLocalInstances) do + let _ ← check locals 0 true cache + withLetDecl `kind (mkConst ``CIL.LocalKind) (mkConst ``CIL.LocalKind.word32) fun kind => do + let locals ← makeList [mkApp2 (mkConst ``LocalSlot.bytes) kind home] + let cache ← check locals 0 false {} true + let changed := (← getLCtx).modifyLocalDecl kind.fvarId! + (fun declaration => declaration.setValue (mkConst ``CIL.LocalKind.reference)) + withLCtx changed (← getLocalInstances) do + let _ ← check locals 0 true cache true + let measure (localCount addresses : Nat) := do + let locals ← makeList (List.replicate localCount byteSlot) + let (instruction, goal) ← makeStep locals (localCount - 1) + let start ← IO.getNumHeartbeats + let mut cache : LoopLocalIndex := {} + for _ in [:addresses] do + let (reads, updated) ← (loopReadsReferences instruction goal).run cache + unless !reads do throwError "a byte address unexpectedly reads a reference" + cache := updated + return (← IO.getNumHeartbeats) - start + let small ← measure 64 64 + let manyLocals ← measure 512 64 + let manyBoth ← measure 512 512 + logInfo m!"local address classification heartbeats: {small}, {manyLocals}, {manyBoth}" + unless manyLocals < small * 3 && manyBoth < small * 12 do + throwError "local address costs grew too quickly: {small}, {manyLocals}, {manyBoth}" + trivial + +#print axioms localReferenceClassification + +open Lean Meta Elab Tactic in +/-- Fact-list traversal preserves positions, raw heads and incomplete prefixes in linear work. -/ +theorem factListTraversal (_tail : List (Option Nat)) : True := by + run_tac withMainContext do + let itemType := mkApp (mkConst ``Option [Level.zero]) (mkConst ``Nat) + let unknownTail ← Term.elabTerm (mkIdent `_tail) none + let absent := mkApp (mkConst ``Option.none [Level.zero]) (mkConst ``Nat) + let present := mkApp2 (mkConst ``Option.some [Level.zero]) (mkConst ``Nat) (mkNatLit 7) + withLetDecl `entry itemType present fun entry => do + let heads := #[entry, absent, present] + let mut list := unknownTail + for head in heads.reverse do + list := mkApp3 (mkConst ``List.cons [Level.zero]) itemType head list + let entries ← listEntries list 4 + unless entries == #[(4, entry), (5, absent), (6, present)] do + throwError "fact entries lost positions, raw heads or an incomplete prefix" + unless (← rememberedIndices list 4) == #[4, 6] do + throwError "remembered positions lost a reducible head or an incomplete prefix" + unless (← listEntries unknownTail 4).isEmpty && + (← rememberedIndices unknownTail 4).isEmpty do + throwError "an unknown list has no visible entries" + let measure (count : Nat) := do + let list ← mkListLit itemType (List.replicate count present) + let start ← IO.getNumHeartbeats + let entries ← listEntries list 3 + let entriesCost := (← IO.getNumHeartbeats) - start + unless entries.size == count && entries.back?.map Prod.fst == some (count + 2) do + throwError "fact traversal lost the last entry" + let start ← IO.getNumHeartbeats + let indices ← rememberedIndices list 3 + let indicesCost := (← IO.getNumHeartbeats) - start + unless indices.size == count && indices.back? == some (count + 2) do + throwError "remembered traversal lost the last position" + return (entriesCost, indicesCost) + let small ← measure 128 + let medium ← measure 512 + let large ← measure 2048 + logInfo m!"fact-list heartbeats: {small}, {medium}, {large}" + unless medium.1 < small.1 * 6 && large.1 < medium.1 * 6 && + medium.2 < small.2 * 6 && large.2 < medium.2 * 6 do + throwError "fact-list traversal costs grew too quickly" + let measureCopies (count : Nat) := do + let mut list := mkApp (mkConst ``List.nil [Level.zero]) itemType + for _ in [:count] do + list := mkApp3 (mkConst ``List.cons [Level.zero]) itemType present list + let _ ← listEntries list + let _ ← rememberedIndices list + let mut entriesTimes := #[] + let mut indicesTimes := #[] + for _ in [:3] do + let start ← IO.monoMsNow + let entries ← listEntries list + let middle ← IO.monoMsNow + let indices ← rememberedIndices list + let stop ← IO.monoMsNow + unless entries.size == count && indices.size == count do + throwError "fact-list traversal lost entries in a deep list" + entriesTimes := entriesTimes.push (middle - start) + indicesTimes := indicesTimes.push (stop - middle) + return ((entriesTimes.qsort (fun left right => left < right))[0]!, + (indicesTimes.qsort (fun left right => left < right))[0]!) + let shorter ← measureCopies 16384 + let longer ← measureCopies 65536 + logInfo m!"fact-list minimum milliseconds: {shorter}, {longer}" + -- Heartbeats count small allocations; array copying also needs an elapsed-time check. + unless longer.1 < shorter.1 * 8 + 20 && longer.2 < shorter.2 * 8 + 20 do + throwError "fact-list copying costs grew too quickly" + trivial + +#print axioms factListTraversal + +-- A failure record prints an unassigned metavariable as `?_`, so the record a run logs does not +-- depend on how many metavariables the run created before it. +run_meta do + let hole ← Lean.Meta.mkFreshExprMVar (Lean.mkConst ``Nat) + let record : FailureRecord := { tactic := "probe", residual := #[hole] } + let rendered ← record.render + unless (rendered.splitOn "\"residual\":[\"?_\"]").length == 2 do + throwError "the failure record printed a metavariable by its number: {rendered}" + +/-- A static field whose value the caller's memory binds, so no step can compute its load. -/ +def opaqueField : CIL.Extraction.FieldRef := + { owner := .atom "Fixture" "Holder", index := 0, type := .atom "Fixture" "System.Int32", + name := name!"Value" } + +/-- The site of `opaqueField`: an opaque value of four bytes. -/ +def opaqueSite : CIL.StaticDescriptor := + { identity := 0, field := opaqueField, bytes := [0, 0, 0, 0], + initializer := some { method := 1, arguments := [] }, scalar := some true, + opaqueValue := true } + +/-- A loop that loads `opaqueField` and drops it on every iteration. -/ +def staticLoop : CIL.Method := { + code := [.ldsfld opaqueField, .pop, .br 0], locals := [], returnsValue := false, + staticSites := [(0, opaqueSite)] } + +/-- `cil_loop_steps` takes the `staticLoad` fact from the context and cannot cross the load + without it. -/ +theorem static_load_gathered (memory : Memory) (frame : Frame) (value : W32) + (loaded : staticLoad staticLoop 0 memory = .ok (memory, .scalar (.i32 value))) : + LoopReaches [staticLoop] 0 [] (LoopState.mk 0 frame [] memory) + (LoopState.mk 2 frame [] memory) := by + fail_if_success (clear loaded; cil_loop_steps) + cil_loop_steps + +#print axioms static_load_gathered + end Tests.LoopDiagnostics diff --git a/Tests/LoopFrames.lean b/Tests/LoopFrames.lean index 56be38e10..07a49ca18 100644 --- a/Tests/LoopFrames.lean +++ b/Tests/LoopFrames.lean @@ -1,4 +1,5 @@ import CIL.MethodIdentity +import Tests.Support.SpanMethods import CIL.Safety.LoopScaffold import CIL.Safety.LoopVC import CIL.Verification @@ -190,8 +191,8 @@ namespace Extracted /-- `while (remaining != 0) remaining--; return remaining;` with `remaining` in a frame home. -/ @[cil_code] def method0 : CIL.Method := { locals := [.i32 0], localKinds := [.word32], returnsValue := true - code := [.arg 0, .setLocal 0, .local 0, .brnonzero 6, .local 0, .ret, .local 0, .const32 1, - .sub, .setLocal 0, .branch 2] } + code := [.ldarg 0, .stloc 0, .ldloc 0, .brtrue 6, .ldloc 0, .ret, .ldloc 0, .ldc_i4 1, + .sub, .stloc 0, .br 2] } /-- The extracted program. -/ @[cil_code] def program : CIL.Program := [method0] @@ -212,7 +213,7 @@ bind_entry entry (input : CIL.W32) := downBody with [.scalar (.i32 input)] from -- The only conditional branch, selected by its shape, with the home as a binder. bind_cutpoint cutpoint (home : Reference) := downBody - matching (fun op => match op with | .brnonzero _ => true | _ => false) with entry.frame home + matching (fun op => match op with | .brtrue _ => true | _ => false) with entry.frame home /-- The remaining count, held in the frame home, with the frame facts of the current memory. -/ abbrev State (home : Reference) := @@ -258,7 +259,7 @@ open CIL.Safety locals as local 1 without a byte home. -/ def body : CIL.Method := { locals := [.i64 0], localKinds := [.word64], returnsValue := true - parameterHomes := [(0, .span 8 false)], code := [.local 0, .ret] } + parameterHomes := [(0, .span 8 false)], code := [.ldloc 0, .ret] } bind_entry entry (memory : Memory) (view : SpanView) (wellFormed : memory.WellFormed) (valid : view.validate memory = .ok ()) (width : view.width = 8) @@ -272,10 +273,10 @@ theorem store_then_load (memory : Memory) (view : SpanView) (wellFormed : memory (valid : view.validate memory = .ok ()) (width : view.width = 8) (readOnly : view.writable = false) {pc : Nat} {args stack : List Value} (word : CIL.W64) : ∃ home entered after, - step body (.setLocal 0) pc args (entry.frame memory view home) + step body (.stloc 0) pc args (entry.frame memory view home) (.scalar (.i64 word) :: stack) entered = .ok (.next (pc + 1) stack (entry.frame memory view home) after) ∧ - step body (.local 0) pc args (entry.frame memory view home) stack after = + step body (.ldloc 0) pc args (entry.frame memory view home) stack after = .ok (.next (pc + 1) (.scalar (.i64 word) :: stack) (entry.frame memory view home) after) := by obtain ⟨home, entered, enter⟩ := @@ -376,7 +377,7 @@ example {xs : List (BitVec 32)} {x : BitVec 32} (filled : Filled caller base 4 x #guard_msgs (substring := true) in example {memory after : Memory} {target : Reference} {bits : CIL.W32} (written : write memory target (numberBytes bits.toNat 8) 1 = .ok after) := - step_storeScalar_of_write written + transfer_i32_of_write written /-- info: 'Tests.LoopFrames.CallerFacts.caller_facts_carried' depends on axioms: [propext, Quot.sound] -/ @@ -397,10 +398,12 @@ open CIL (W64) @[cil_code] def spanSum : CIL.Method := { returnsValue := true, locals := [.i64 0], localKinds := [.word64], parameterHomes := [(0, .span 8 false)] - code := [.const32 0, .convI8, .setLocal 0, .branch 15, .local 0, .local 1, .const32 0, - .memory (.spanItem 8), .load64, .add, .setLocal 0, .local 1, .const32 1, - .memory (.spanSlice false), .setLocal 1, .local 1, .memory .spanLength, .brnonzero 4, - .local 0, .ret] } + code := [.ldc_i4 0, .conv_i8, .stloc 0, .br 15, .ldloc 0, .ldarg 0, .ldc_i4 0, + .call (SpanMethods.item SpanMethods.uint64), .ldind .i8, .add, .stloc 0, .ldarg 0, .ldc_i4 1, + .call (SpanMethods.slice SpanMethods.uint64 false), .starg 0, .ldarg 0, .call (SpanMethods.length SpanMethods.uint64), .brtrue 4, + .ldloc 0, .ret] } + +cil_runtime_models spanSum /-- The program of `spanSum` alone. -/ @[cil_code] def spanSumProgram : CIL.Program := [spanSum] @@ -421,7 +424,7 @@ abbrev SpanSumState (memory : Memory) (home : Reference) := /-- The loop test, with the unread suffix's length on the stack. -/ bind_cutpoint spanSumHeader (memory : Memory) (home : Reference) (state : SpanSumState memory home) := - spanSum matching (· matches .brnonzero _) + spanSum matching (· matches .brtrue _) with spanSumEntry.frame memory state.values.2.view home, [.scalar (.i32 (BitVec.ofNat 32 state.values.2.view.length))], state.memory diff --git a/Tests/LoopOutcome.lean b/Tests/LoopOutcome.lean index de1e17b88..53312c827 100644 --- a/Tests/LoopOutcome.lean +++ b/Tests/LoopOutcome.lean @@ -18,7 +18,7 @@ example {α : Type} (program : CIL.Program) (method : Nat) (args : List Value) loop_outcome_induction program method args configuration invariant measure post none progress state valid example {Input Output : Type} {c : MethodContract Input Output} - {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Op} + {program : CIL.Program} {method pc callee : Nat} {body : CIL.Method} {op : CIL.Instruction} {args stack arguments rest : List Value} {frame : Frame} {memory updated : Memory} {post : Memory → List Value → Prop} (holds : c.Holds program callee) (input : Input) @@ -35,32 +35,41 @@ example (program : CIL.Program) (method : Nat) (pre : List Value → Memory → (post : List Value → Memory → Memory → List Value → Prop) : EnsuresOutcome program method pre post (fun _ _ => none) ↔ Ensures program method pre post := .rfl -/-! Unsupported execution, invalid state, fuel exhaustion, normal return and another exception never discharge an expected one. -/ +/-! Unsupported execution, invalid state, fuel exhaustion, normal return and another exception + never discharge an expected one. All but unsupported execution also refute it; the model + does not cover a run that reaches a hole. -/ -@[cil_code] def stuck : CIL.Method := { returnsValue := false, locals := [], code := [.unsupported "fixture"] } -@[cil_code] def spin : CIL.Method := { returnsValue := false, locals := [], code := [.branch 0] } +@[cil_code] def stuck : CIL.Method := + { returnsValue := false, locals := [], code := [.unadmitted 0] } +@[cil_code] def spin : CIL.Method := { returnsValue := false, locals := [], code := [.br 0] } @[cil_code] def done : CIL.Method := { returnsValue := false, locals := [], code := [.ret] } -@[cil_code] def other : CIL.Method := { returnsValue := false, locals := [], code := [.throw "System.ArgumentException"] } +@[cil_code] def other : CIL.Method := + { returnsValue := false, locals := [], code := [.throw .argument] } variable (post : Memory → List Value → Prop) (frame : Frame) (m : Memory) -example : ¬ LoopOutcome [stuck] 0 [] post (some "System.InvalidOperationException") ⟨0, frame, [], m⟩ := - loop_outcome_some_refuted (fuel := 1) - (outcome := .error ⟨0, 0, .unsupported "Not a memory instruction"⟩) rfl - (by simp [Settled]) (by rintro _ ⟨⟩; simp) +/-- The hole's fault is not settled, so `loop_outcome_some_refuted` takes no run that reaches + it. -/ +theorem stuck_not_settled : + run [stuck] 1 0 0 [] frame [] m = .error ⟨0, 0, .unsupported "Opcode not admitted"⟩ ∧ + ¬ Settled (.error ⟨0, 0, .unsupported "Opcode not admitted"⟩ : + Except LocatedFault (Memory × List Value)) := + ⟨rfl, by decide⟩ +#print axioms stuck_not_settled /-- A pc past the body is invalid state. -/ -example : ¬ LoopOutcome [stuck] 0 [] post (some "System.InvalidOperationException") ⟨1, frame, [], m⟩ := +example : ¬ LoopOutcome [stuck] 0 [] post (some .invalidOperation) ⟨1, frame, [], m⟩ := loop_outcome_some_refuted (fuel := 1) (outcome := .error ⟨0, 1, .invalidState⟩) rfl (by simp [Settled]) (by rintro _ ⟨⟩; simp) /-- A method index outside the program is invalid state. -/ -example : ¬ LoopOutcome [] 0 [] post (some "System.InvalidOperationException") ⟨0, frame, [], m⟩ := +example : ¬ LoopOutcome [] 0 [] post (some .invalidOperation) ⟨0, frame, [], m⟩ := loop_outcome_some_refuted (fuel := 1) (outcome := .error ⟨0, 0, .invalidState⟩) rfl (by simp [Settled]) (by rintro _ ⟨⟩; simp) -example : ¬ LoopOutcome [other] 0 [] post (some "System.InvalidOperationException") ⟨0, frame, [], m⟩ := - loop_outcome_some_refuted (fuel := 1) (outcome := .error ⟨0, 0, .thrown "System.ArgumentException"⟩) rfl (by simp [Settled]) (by rintro _ ⟨⟩; simp) +example : ¬ LoopOutcome [other] 0 [] post (some .invalidOperation) ⟨0, frame, [], m⟩ := + loop_outcome_some_refuted (fuel := 1) (outcome := .error ⟨0, 0, .thrown .argument⟩) rfl + (by simp [Settled]) (by rintro _ ⟨⟩; simp) -example : ¬ LoopOutcome [done] 0 [] post (some "System.InvalidOperationException") ⟨0, frame, [], m⟩ := +example : ¬ LoopOutcome [done] 0 [] post (some .invalidOperation) ⟨0, frame, [], m⟩ := loop_outcome_some_refuted (fuel := 1) (outcome := .ok (leaveFrame frame m, [])) rfl trivial (by rintro _ ⟨⟩) theorem spin_exhausts (fuel : Nat) : @@ -69,7 +78,7 @@ theorem spin_exhausts (fuel : Nat) : | zero => rfl | succ fuel ih => exact (show run [spin] (fuel + 1) 0 0 [] frame [] m = run [spin] fuel 0 0 [] frame [] m from rfl).trans ih -example : ¬ LoopOutcome [spin] 0 [] post (some "System.InvalidOperationException") ⟨0, frame, [], m⟩ := by +example : ¬ LoopOutcome [spin] 0 [] post (some .invalidOperation) ⟨0, frame, [], m⟩ := by rintro ⟨fuel, fault, ran, thrown⟩ change run [spin] fuel 0 0 [] frame [] m = _ at ran rw [spin_exhausts] at ran @@ -78,7 +87,7 @@ example : ¬ LoopOutcome [spin] 0 [] post (some "System.InvalidOperationExceptio /-- The method-level outcome inherits the same exactness. -/ example : ¬ EnsuresOutcome [spin] 0 (fun args initial => args = [] ∧ initial = m) (fun _ _ _ _ => True) - (fun _ _ => some "System.InvalidOperationException") := by + (fun _ _ => some .invalidOperation) := by intro holds obtain ⟨fuel, fault, ran, thrown⟩ := holds [] m ⟨rfl, rfl⟩ change run [spin] fuel 0 0 [] _ [] m = _ at ran @@ -86,4 +95,45 @@ example : ¬ EnsuresOutcome [spin] 0 (fun args initial => args = [] ∧ initial cases ran simp at thrown +/-! A loop that never exits refutes a contract that promises a return. A run that exhausts one + budget and returns from a larger one does not diverge, and neither does a run that reaches a + hole. -/ + +/-- Promises a return from any memory, which `done` meets and `spin` does not. -/ +def returns : MethodContract Unit := + { args := fun _ => [], pre := fun _ _ => True, footprint := fun _ => [], + post := fun _ _ _ _ => True } + +/-- Every budget exhausts `spin` from any memory. -/ +theorem spin_diverges : Diverges [spin] 0 [] m := fun fuel => + ⟨⟨0, 0, .fuelExhausted⟩, + by change run [spin] fuel 0 0 [] _ [] m = _; exact spin_exhausts _ m fuel, rfl⟩ +#print axioms spin_diverges + +/-- Divergence refutes the contract, and so its negation holds. -/ +theorem spin_refuted : returns.Refuted [spin] 0 ∧ ¬ returns.Holds [spin] 0 := + let refuted : returns.Refuted [spin] 0 := + .of_diverges (input := ()) (initial := Memory.empty) trivial (spin_diverges Memory.empty) + ⟨refuted, refuted.not_holds⟩ +#print axioms spin_refuted + +/-- `done` exhausts the empty budget and returns from one step, so it does not diverge. -/ +theorem done_not_diverges : + invoke [done] 0 0 [] Memory.empty = .error ⟨0, 0, .fuelExhausted⟩ ∧ + ¬ Diverges [done] 0 [] Memory.empty := by + refine ⟨rfl, fun diverges => ?_⟩ + obtain ⟨fault, ran, -⟩ := diverges 1 + rw [(rfl : invoke [done] 1 0 [] Memory.empty = .ok _)] at ran + cases ran +#print axioms done_not_diverges + +/-- The hole's fault is stable, so no budget beyond it exhausts `stuck`. -/ +theorem stuck_not_diverges : ¬ Diverges [stuck] 0 [] Memory.empty := by + intro diverges + obtain ⟨fault, ran, exhausted⟩ := diverges 1 + rw [(rfl : invoke [stuck] 1 0 [] Memory.empty = .error _)] at ran + cases ran + simp at exhausted +#print axioms stuck_not_diverges + end Tests.LoopOutcome diff --git a/Tests/LoopScaffold.lean b/Tests/LoopScaffold.lean index deb490e75..fb4e42f32 100644 --- a/Tests/LoopScaffold.lean +++ b/Tests/LoopScaffold.lean @@ -1,4 +1,5 @@ import CIL.Safety.LoopScaffold +import Tests.Support.SpanMethods import CIL.Safety.LoopVC import CIL.Safety.Execution import CIL.StructuralLabels @@ -11,11 +12,11 @@ open CIL.Safety /-- A numeric local with a single conditional loop edge. -/ def body : CIL.Method := { locals := [.i32 0], localKinds := [.word32], returnsValue := true - code := [.local 0, .brnonzero 0, .ret] } + code := [.ldloc 0, .brtrue 0, .ret] } bind_entry entry := body with [] from Memory.empty bind_cutpoint header := body matching - (fun op => match op with | .brnonzero _ => true | _ => false) with entry.frame + (fun op => match op with | .brtrue _ => true | _ => false) with entry.frame /-- Generated homes come from checked allocation and the cutpoint follows compiled instructions. -/ theorem generated_state : @@ -31,9 +32,9 @@ bind_cutpoint absent := body matching (fun _ => false) with entry.frame #guard_msgs in bind_cutpoint ambiguous := body matching (fun _ => true) with entry.frame -/-- Missing vector arguments cannot supply a frame witness. -/ +/-- A parameter home with no matching argument cannot supply a frame witness. -/ def missingArgument : CIL.Method := { - code := [.ret], locals := [], returnsValue := false, vectorArgs := [0] } + code := [.ret], locals := [], returnsValue := false, parameterHomes := [(0, .word32)] } /-- error: Frame entry must reduce to a successful checked allocation -/ #guard_msgs in @@ -46,15 +47,15 @@ open CIL.Safety /-- Both nested loop latches have the same opcode, so instruction shape is ambiguous. -/ def body : CIL.Method := { - code := [.nop, .const32 0, .brnonzero 1, .const32 0, .brnonzero 0, .ret] + code := [.nop, .ldc_i4 0, .brtrue 1, .ldc_i4 0, .brtrue 0, .ret] locals := [], returnsValue := false } bind_loop outer := body [0] bind_loop inner := body [1] bind_cutpoint outerExit (frame : Frame) := body at outer.latch matching - (fun op => match op with | .brnonzero _ => true | _ => false) with frame + (fun op => match op with | .brtrue _ => true | _ => false) with frame bind_cutpoint innerExit (frame : Frame) := body at inner.latch matching - (fun op => match op with | .brnonzero _ => true | _ => false) with frame + (fun op => match op with | .brtrue _ => true | _ => false) with frame /-- Structural labels select distinct checked latches despite identical instruction shapes. -/ theorem distinct_latches : outerExit.pc = 4 ∧ innerExit.pc = 2 := by exact ⟨rfl, rfl⟩ @@ -67,7 +68,7 @@ bind_cutpoint outside (frame : Frame) := body at 99 matching (fun _ => true) wit /-- error: Selected cutpoint must exist and match the instruction predicate -/ #guard_msgs in bind_cutpoint wrongShape (frame : Frame) := body at outer.latch matching - (fun op => match op with | .ret _ => true | _ => false) with frame + (fun op => match op with | .ret => true | _ => false) with frame end Tests.RepeatedCutpoints @@ -87,19 +88,6 @@ theorem parameter_entry (value : CIL.W32) : .ok (parameter.frame value, parameter.memory value) := parameter.checked value #print axioms parameter_entry -/-- Vector argument homes preserve a symbolic vector's bytes. -/ -def vectorBody : CIL.Method := { - code := [.ret], locals := [], returnsValue := false, vectorArgs := [0] } - -bind_entry vector (bits : BitVec 256) := vectorBody - with [.scalar (.v256 bits)] from Memory.empty - -/-- Vector entry uses the supplied bits and is checked for every vector. -/ -theorem vector_entry (bits : BitVec 256) : - enterFrame vectorBody [.scalar (.v256 bits)] Memory.empty = - .ok (vector.frame bits, vector.memory bits) := vector.checked bits -#print axioms vector_entry - end Tests.SymbolicLoopScaffold namespace Tests.SymbolicStorageEntry @@ -290,8 +278,8 @@ open CIL.Safety /-- `while (remaining != 0) remaining--; return remaining;` with `remaining` in a frame home. -/ @[cil_code] def countdown : CIL.Method := { locals := [.i32 0], localKinds := [.word32], returnsValue := true - code := [.arg 0, .setLocal 0, .local 0, .brnonzero 6, .local 0, .ret, .local 0, .const32 1, - .sub, .setLocal 0, .branch 2] } + code := [.ldarg 0, .stloc 0, .ldloc 0, .brtrue 6, .ldloc 0, .ret, .ldloc 0, .ldc_i4 1, + .sub, .stloc 0, .br 2] } /-- The program holding only `countdown`. -/ @[cil_code] def program : CIL.Program := [countdown] @@ -306,7 +294,7 @@ abbrev State (memory : Memory) (home : Reference) := -- The guard sees the remaining count. bind_cutpoint header (memory : Memory) (home : Reference) (state : State memory home) := - countdown at 3 matching (fun op => match op with | .brnonzero _ => true | _ => false) + countdown at 3 matching (fun op => match op with | .brtrue _ => true | _ => false) with (entry.frame memory home), [.scalar (.i32 state.values)], state.memory /-- The store and exit rules carry the frame part of the state, and the loop tactics read the @@ -393,10 +381,12 @@ open CIL.Safety @[cil_code] def sum : CIL.Method := { locals := [.i64 0], localKinds := [.word64], returnsValue := true parameterHomes := [(0, .span 8 false)] - code := [.const32 0, .convI8, .setLocal 0, .branch 15, .local 0, .local 1, .const32 0, - .memory (.spanItem 8), .load64, .add, .setLocal 0, .local 1, .const32 1, - .memory (.spanSlice false), .setLocal 1, .local 1, .memory .spanLength, .brnonzero 4, - .local 0, .ret] } + code := [.ldc_i4 0, .conv_i8, .stloc 0, .br 15, .ldloc 0, .ldarg 0, .ldc_i4 0, + .call (SpanMethods.item SpanMethods.uint64), .ldind .i8, .add, .stloc 0, .ldarg 0, .ldc_i4 1, + .call (SpanMethods.slice SpanMethods.uint64 false), .starg 0, .ldarg 0, .call (SpanMethods.length SpanMethods.uint64), .brtrue 4, + .ldloc 0, .ret] } + +cil_runtime_models sum /-- The program holding only `sum`. -/ @[cil_code] def program : CIL.Program := [sum] @@ -416,7 +406,7 @@ abbrev State (memory : Memory) (home : Reference) := -- The guard sees the remaining length. bind_cutpoint header (memory : Memory) (home : Reference) (state : State memory home) := - sum at 17 matching (· matches .brnonzero _) + sum at 17 matching (· matches .brtrue _) with entry.frame memory state.values.1.view home, [.scalar (.i32 (BitVec.ofNat 32 state.values.1.view.length))], state.memory @@ -512,9 +502,65 @@ theorem constant_measure (memory : Memory) (view : SpanView) (values : List CIL. · simpa [BitVec.add_assoc] using invariant · decide +/-- `sum` as the extractor emits it: `span[0]`, `span.Slice(1)` and `span.Length` take the span + parameter's address with `ldarga`, which pushes the view its home holds. -/ +@[cil_code] def sumByAddress : CIL.Method := { + locals := [.i64 0], localKinds := [.word64], returnsValue := true + parameterHomes := [(0, .span 8 false)] + code := [.ldc_i4 0, .conv_i8, .stloc 0, .br 15, .ldloc 0, .ldarga 0, .ldc_i4 0, + .call (SpanMethods.item SpanMethods.uint64), .ldind .i8, .add, .stloc 0, .ldarga 0, .ldc_i4 1, + .call (SpanMethods.slice SpanMethods.uint64 false), .starg 0, .ldarga 0, .call (SpanMethods.length SpanMethods.uint64), .brtrue 4, + .ldloc 0, .ret] } + +cil_runtime_models sumByAddress + +/-- The program holding only `sumByAddress`. -/ +@[cil_code] def addressProgram : CIL.Program := [sumByAddress] + +bind_entry addressEntry (memory : Memory) (view : SpanView) (wellFormed : memory.WellFormed) + (valid : view.validate memory = .ok ()) (width : view.width = 8) + (readOnly : view.writable = false) := sumByAddress + with [.bufferSpan view] from memory allocating wellFormed + simplifying [valid, width, readOnly] + +-- The guard sees the remaining length. +bind_cutpoint addressHeader (memory : Memory) (home : Reference) (state : State memory home) := + sumByAddress at 17 matching (· matches .brtrue _) + with addressEntry.frame memory state.values.1.view home, + [.scalar (.i32 (BitVec.ofNat 32 state.values.1.view.length))], state.memory + +/-- Taking the span home's address checks the view it holds, so the loop tactics give that step + the span's facts, including those of a slice an earlier iteration stored. -/ +theorem sum_by_address_returns (memory : Memory) (view : SpanView) (values : List CIL.W64) + (wellFormed : memory.WellFormed) (span : CheckedSpan memory view 8 false values) : + ∃ fuel final, invoke addressProgram fuel 0 [.bufferSpan view] memory = + .ok (final, [.scalar (.i64 values.sum)]) ∧ MemoryBelow memory.nextIdentity memory final := by + obtain ⟨home, entered, enter⟩ := + addressEntry.checked memory view wellFormed span.valid span.elementWidth span.mode + obtain ⟨started, stored, frame, -⟩ := (enter.frame []).store 0 (.i64 0) rfl + refine invoke_loop_results (by rfl) (by simp [cil_checked, span.valid]) enter.setup + (addressHeader memory home) + (fun state => state.values.2 + state.values.1.values.sum = values.sum) + (·.values.1.values.length) ⟨(⟨view, values, span⟩, 0), started, frame⟩ + (by cil_loop_steps) (by simp) ?_ + rintro ⟨⟨⟨view, remaining, span⟩, total⟩, current, frame⟩ invariant + cases remaining with + | nil => exact .inl (by cil_loop_finish [invariant.symm]) + | cons value remaining => + obtain ⟨item, -, found, element⟩ := (span.within wellFormed frame).head (by decide) + obtain ⟨updated, stored, nextFrame, -⟩ := frame.store 0 (.i64 (total + value)) rfl + obtain ⟨sliced, slicedOK, next, -⟩ := span.tail_below wellFormed nextFrame.kept + cil_loop_continue ⟨(⟨sliced, remaining, next⟩, total + value), updated, + nextFrame.set_local 1 (.span 8 false (some sliced))⟩ + · simpa [BitVec.add_assoc] using invariant + · simp +#print axioms sum_by_address_returns + /-- `return span.Length;` read straight from the argument, with no parameter home. -/ @[cil_code] def length : CIL.Method := - { locals := [], returnsValue := true, code := [.arg 0, .memory .spanLength, .ret] } + { locals := [], returnsValue := true, code := [.ldarg 0, .call (SpanMethods.length SpanMethods.uint64), .ret] } + +cil_runtime_models length /-- The program holding only `length`. -/ @[cil_code] def lengthProgram : CIL.Program := [length] diff --git a/Tests/LoopVariants/CountCallsDiscarded.lean.in b/Tests/LoopVariants/CountCallsDiscarded.lean.in new file mode 100644 index 000000000..e25c19f17 --- /dev/null +++ b/Tests/LoopVariants/CountCallsDiscarded.lean.in @@ -0,0 +1,42 @@ +bind_entry entry (value : W32) := Extracted.method0 + with [.scalar (.i32 value)] from Memory.empty allocating Memory.empty_wellFormed + +/-- Discarding Decrement's result keeps the guard true for every value of the count. -/ +theorem correctness : contract.Refuted Extracted.program 0 := by + apply MethodContract.Refuted.of_diverges (input := 1) (initial := Memory.empty) rfl + obtain ⟨remaining, count, entered, enter⟩ := entry.checked 1 + obtain ⟨first, wroteFirst, firstFacts, -⟩ := (enter.frame []).store 0 (.i32 1) rfl + obtain ⟨started, wroteCount, facts, -⟩ := firstFacts.store 1 (.i32 0) rfl + let frame := entry.frame remaining count + let state (stack : List CIL.Safety.Value) (memory : Memory) : SegmentState := + ⟨frame, stack, memory⟩ + apply Diverges.of_reaches (pc := 12) (cost := 5) (head := state [] started) + Extracted.program_method0 rfl enter.setup + · apply SegmentRun.reaches + intro fuel + cil_checked_steps [state, frame, entry.frame, wroteFirst, wroteCount] <;> rfl + let invariant (pc : Nat) (s : SegmentState) := pc = 12 ∧ s = state [] s.memory ∧ + ∃ n : W32, LoopFrame [] Memory.empty s.memory [.word32, .word32] + [.bytes .word32 remaining, .bytes .word32 count] (knownLocals [some (.i32 1), some (.i32 n)]) + apply SegmentExit.diverges_of_recurrent (invariant := invariant) _ ⟨rfl, rfl, 0, facts⟩ + rintro _ ⟨currentFrame, stack, current⟩ ⟨rfl, same, n, known⟩ + cases same + obtain ⟨updated, wrote, next, -⟩ := known.store 1 (.i32 (n + 1)) rfl + let args := contract.args 1 + have head : SegmentRun Extracted.program 0 12 6 3 args (state [] current) + (state [.scalar (.i32 1)] current) := by + intro fuel + cil_checked_steps [args, contract, state, frame, entry.frame, known.load 0] <;> rfl + have call : ReachesRun Extracted.program 0 6 7 1 args (state [.scalar (.i32 1)] current) + (state [.scalar (.i32 0)] current) := by + apply ReachesRun.call (callee := 1) (values := [.scalar (.i32 1)]) + (results := [.scalar (.i32 0)]) (rest := []) (updated := current) (entered := current) + (childFrame := ⟨current.nextIdentity, [], [], 0⟩) (calleeFuel := 4) + Extracted.program_method0 Extracted.method0_instruction6 _ Extracted.program_method1 + all_goals rfl + have tail : SegmentRun Extracted.program 0 7 12 5 args (state [.scalar (.i32 0)] current) + (state [] updated) := by + intro fuel + cil_checked_steps [args, contract, state, frame, entry.frame, known.load 1, wrote] <;> rfl + exact ⟨12, state [] updated, 9, by decide, (head.reaches.trans call).trans tail.reaches, + rfl, rfl, n + 1, next⟩ diff --git a/Tests/LoopVariants/CountdownReuse.lean.in b/Tests/LoopVariants/CountdownReuse.lean.in new file mode 100644 index 000000000..9bc39a8b9 --- /dev/null +++ b/Tests/LoopVariants/CountdownReuse.lean.in @@ -0,0 +1,29 @@ +import Proof + +namespace CountdownConsumer +open CIL CIL.Safety + +/-- The compiled method returns zero on the empty entry memory for this argument. -/ +def returnsZero (n : W32) : Prop := ∃ fuel, + ((invoke Extracted.program fuel 0 [.scalar (.i32 n)] Memory.empty).toOption.map Prod.snd) = + some [.scalar (.i32 0)] + +/-- Use the compiled loop contract's termination witness and returned stack. -/ +theorem reuse (n : W32) : returnsZero n := by + obtain ⟨fuel, final, values, ran, rfl⟩ := + ExampleProof.correctness [.scalar (.i32 n)] Memory.empty ⟨n, rfl, rfl⟩ + exact ⟨fuel, by rw [ran]; rfl⟩ + +set_option maxRecDepth 100000 +set_option maxHeartbeats 16000000 + +theorem direct4 : returnsZero 4 := ⟨64, rfl⟩ +theorem reused4 : returnsZero 4 := reuse 4 + +theorem direct16 : returnsZero 16 := ⟨160, rfl⟩ +theorem reused16 : returnsZero 16 := reuse 16 +#print axioms direct4 +#print axioms reused4 +#print axioms direct16 +#print axioms reused16 +end CountdownConsumer diff --git a/Tests/LoopVariants/CountdownStuttering.lean.in b/Tests/LoopVariants/CountdownStuttering.lean.in new file mode 100644 index 000000000..f24dfd518 --- /dev/null +++ b/Tests/LoopVariants/CountdownStuttering.lean.in @@ -0,0 +1,40 @@ +bind_entry entry (value : W32) := Extracted.method0 + with [.scalar (.i32 value)] from Memory.empty allocating Memory.empty_wellFormed + +/-- Subtracting zero leaves the remaining count one on every visit to the guard. -/ +theorem correctness : contract.Refuted Extracted.program 0 := by + apply MethodContract.Refuted.of_diverges (input := 1) (initial := Memory.empty) rfl + obtain ⟨home, entered, enter⟩ := entry.checked 1 + obtain ⟨started, stored, facts, -⟩ := (enter.frame []).store 0 (.i32 1) rfl + let frame := entry.frame home + let atGuard (memory : Memory) : SegmentState := ⟨frame, [], memory⟩ + apply Diverges.of_reaches (pc := 7) (cost := 3) (head := atGuard started) + Extracted.program_method0 rfl enter.setup + · apply SegmentRun.reaches + intro fuel + dsimp only [atGuard, frame, entry.frame] + cil_checked_steps [stored] <;> rfl + let invariant (pc : Nat) (state : SegmentState) := pc = 7 ∧ state = atGuard state.memory ∧ + LoopFrame [] Memory.empty state.memory [.word32] [.bytes .word32 home] + (knownLocals [some (.i32 1)]) + apply SegmentExit.diverges_of_recurrent (invariant := invariant) _ ⟨rfl, rfl, facts⟩ + rintro _ ⟨currentFrame, stack, current⟩ ⟨rfl, same, known⟩ + cases same + obtain ⟨updated, wrote, next, -⟩ := known.store 0 (.i32 1) rfl + refine ⟨7, atGuard updated, 6, by decide, SegmentRun.reaches ?_, rfl, rfl, next⟩ + intro fuel + dsimp only [atGuard, frame, entry.frame] + cil_checked_steps [known.load 0, wrote] + change run Extracted.program (fuel + 1) 0 6 (contract.args 1) frame [.scalar (.i32 1)] current = + run Extracted.program fuel 0 7 (contract.args 1) frame [] updated + have last : step Extracted.method0 (.stloc 0) 6 (contract.args 1) frame [.scalar (.i32 1)] + current = .ok (.next 7 [] frame updated) := by + change (do + let (slot, memory) ← storeLocal current (.bytes .word32 home) (.scalar (.i32 1)) + pure (FrameAction.next 7 [] {frame with locals := [slot]} memory)) = _ + rw [show storeLocal current (.bytes .word32 home) (.scalar (.i32 1)) = + .ok (.bytes .word32 home, updated) from wrote] + rfl + have placed : Extracted.method0.code[6]? = some (.stloc 0) := rfl + simp only [run_succ, Extracted.program_method0, placed, last, Except.mapError, + Bind.bind, Except.bind] diff --git a/Tests/Loops.cs b/Tests/Loops.cs index e6c31d0b6..a9aa13492 100644 --- a/Tests/Loops.cs +++ b/Tests/Loops.cs @@ -2,11 +2,19 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include ExceptionMembers.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include WordArrayOutlines.cs using System.Reflection; using System.Buffers.Binary; using System.Reflection.PortableExecutable; +using System.Text.Json; using System.Text.RegularExpressions; using Mono.Cecil; using Mono.Cecil.Cil; @@ -62,21 +70,15 @@ } module.Write(dll); } - Run(Extract("extract", dll, "Examples", "Loops::Countdown(uint)", output)); if (variant == "Stuttering") { - var refused = false; - try { verification.Verify(dll, "Examples", "Loops::Countdown(uint)", contract, output, existingArtifacts: true); } - catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) - { - refused = true; - } - if (!refused || File.Exists(Path.Combine(output, "verified.json"))) - throw new InvalidOperationException("Nondecreasing loop satisfied the termination contract"); - Console.WriteLine("PASS unchanged universal contract rejects nondecreasing loop"); + VerifyNondecreasing(assembly, "Loops::Countdown(uint)", contract, + Path.Combine(root, "CountdownContract"), dll, output, "0", + "Tests/LoopVariants/CountdownStuttering.lean.in"); } else { + Run(Extract("extract", dll, "Examples", "Loops::Countdown(uint)", output)); var actual = Assembly.Load(File.ReadAllBytes(dll)).GetType("Examples.Loops")!.GetMethod("Countdown")!; foreach (var input in new uint[] { 0, 1, 7 }) if ((uint)actual.Invoke(null, [input])! != 0) @@ -95,7 +97,6 @@ var loopStart = countProof.IndexOf("theorem terminates", StringComparison.Ordinal); if (helperEnd < 0 || loopStart < helperEnd) throw new InvalidDataException("Examples/Proofs/CountCalls.lean has no helper section before its loop proof"); -var loopLine = countProof[..loopStart].Count(c => c == '\n') + 1; verification.Verify(assembly, "Examples", CountCalls, At("Examples/Proofs/CountCalls.lean"), Path.Combine(root, "CountCalls")); Console.WriteLine("PASS counted loop returns its iteration count for every uint through the helper contract"); @@ -258,19 +259,18 @@ cil_contract_steps [] verification.Verify(replacedCount, "Examples", CountCalls, replacedProof, Path.Combine(root, "CountCallsReplaced"), existingArtifacts: true); Console.WriteLine("PASS unchanged loop proof after reimplementing the helper and re-proving only its contract"); -// Both builds keep the helper, so the unchanged proof must fail inside the loop proof: -// a discarded helper result never decreases the measure; counting twice breaks the invariant. -foreach (var (name, opcode, replacement) in new[] { ("CountCallsDiscarded", OpCodes.Stloc_0, OpCodes.Pop), ("CountCallsOvercount", OpCodes.Ldc_I4_1, OpCodes.Ldc_I4_2) }) -{ - var dll = CountVariant(name, (caller, _) => caller.Body.Instructions.Single(i => i.OpCode == opcode && - (opcode != OpCodes.Stloc_0 || i.Previous.OpCode == OpCodes.Call)).OpCode = replacement); - if (name == "CountCallsOvercount" && Invoke(dll, "CountCalls", 3) != 6) - throw new InvalidOperationException("The overcounting fixture did not change the result"); - var lines = RejectedLines(dll, At("Examples/Proofs/CountCalls.lean"), name, existingArtifacts: true); - if (lines.Length == 0 || lines.Any(line => line < loopLine)) - throw new InvalidOperationException($"{name} was not rejected by the loop proof: lines {string.Join(", ", lines)}"); - Console.WriteLine($"PASS unchanged loop proof rejects {name}"); -} +// Discarding the helper result keeps the guard true. Counting twice returns the wrong value. +var discarded = CountVariant("CountCallsDiscarded", (caller, _) => caller.Body.Instructions.Single(i => + i.OpCode == OpCodes.Stloc_0 && i.Previous.OpCode == OpCodes.Call).OpCode = OpCodes.Pop); +VerifyNondecreasing(assembly, CountCalls, At("Examples/Proofs/CountCalls.lean"), + Path.Combine(root, "CountCallsContract"), discarded, Path.Combine(root, "CountCallsDiscarded"), + "input", "Tests/LoopVariants/CountCallsDiscarded.lean.in"); +var overcount = CountVariant("CountCallsOvercount", (caller, _) => + caller.Body.Instructions.Single(i => i.OpCode == OpCodes.Ldc_I4_1).OpCode = OpCodes.Ldc_I4_2); +if (Invoke(overcount, "CountCalls", 3) != 6) + throw new InvalidOperationException("The overcounting fixture did not change the result"); +Refute(overcount, CountCalls, "CountCallsOvercount", "input", 3, existingArtifacts: true); +Console.WriteLine("PASS counting contract refuted for CountCallsOvercount at 3, where it returns 6"); // On the correct build, the rule itself must refuse a nondecreasing measure and an invariant // that holds at entry and exit but is not preserved, each at exactly that obligation. @@ -315,7 +315,7 @@ int LineOf(string text) => countProof.IndexOf(text, StringComparison.Ordinal) is object Outcome(string dll, string name, uint value, uint limit) { try { return Assembly.Load(File.ReadAllBytes(dll)).GetType("Examples.Loops")!.GetMethod(name)!.Invoke(null, [value, limit])!; } - catch (TargetInvocationException error) { return error.InnerException!.GetType().FullName!; } + catch (TargetInvocationException error) { return ExceptionMember(error.InnerException!); } } (uint Value, uint Limit)[] untilInputs = [(0, 0), (3, 7), (3, 3), (5, 2), (4, 0)]; var untilClaims = untilInputs.Select(input => (input, Outcome(assembly, "CountUntil", input.Value, input.Limit))).Select(pair => @@ -323,9 +323,9 @@ object Outcome(string dll, string name, uint value, uint limit) var invocation = $"CIL.Safety.invoke Extracted.program {32 + 16 * pair.input.Value} 0 [.scalar (.i32 {pair.input.Value}), .scalar (.i32 {pair.input.Limit})] CIL.Safety.Memory.empty"; return pair.Item2 is uint result ? $"({invocation}).map Prod.snd = .ok [.scalar (.i32 {result})]" - : $"(match {invocation} with | .error fault => fault.fault | .ok _ => .invalidState) = .thrown \"{pair.Item2}\""; + : $"(match {invocation} with | .error fault => fault.fault | .ok _ => .invalidState) = .thrown .{pair.Item2}"; }).ToArray(); -if (untilClaims.Count(claim => claim.Contains("System.InvalidOperationException")) != 2) +if (untilClaims.Count(claim => claim.EndsWith(".thrown .invalidOperation", StringComparison.Ordinal)) != 2) throw new InvalidOperationException("CountUntil fixture did not raise for exactly the two inputs above their limit"); Prove(assembly, CountUntil, "CountUntilRuntime", "theorem correctness :\n" + string.Join(" ∧\n", untilClaims) + " := by\n exact ⟨" + string.Join(", ", untilClaims.Select(_ => "rfl")) + "⟩"); @@ -398,15 +398,15 @@ int UntilLine(string text) => untilProof.IndexOf(text, StringComparison.Ordinal) var type = new TypeReference("System", "ArgumentException", module, original.DeclaringType.Scope); ctor.Operand = new MethodReference(".ctor", module.TypeSystem.Void, type) { HasThis = true }; }); -if (!Outcome(otherException, "CountUntil", 5, 2).Equals("System.ArgumentException")) +if (!Outcome(otherException, "CountUntil", 5, 2).Equals("argument")) throw new InvalidOperationException("The other-exception fixture did not change the thrown type"); var helperContract = untilProof[contractStart..bumpStart]; -if (helperContract.Split("\"System.InvalidOperationException\"").Length != 2) +if (helperContract.Split("some .invalidOperation").Length != 2) throw new InvalidDataException("Ambiguous helper contract mutation"); Directory.CreateDirectory(Path.Combine(root, "CountUntilOtherException")); var otherProof = Path.Combine(root, "CountUntilOtherException", "CountUntil.lean"); File.WriteAllText(otherProof, untilProof[..contractStart] + - helperContract.Replace("System.InvalidOperationException", "System.ArgumentException") + untilProof[bumpStart..]); + helperContract.Replace("some .invalidOperation", "some .argument") + untilProof[bumpStart..]); var otherLines = RejectedLines(otherException, otherProof, "CountUntilOtherException", existingArtifacts: true, CountUntil); if (otherLines.Length == 0 || otherLines.Any(line => line != exceptionalExit)) throw new InvalidOperationException($"CountUntilOtherException was not rejected at line {exceptionalExit}: lines {string.Join(", ", otherLines)}"); @@ -421,8 +421,8 @@ int UntilLine(string text) => untilProof.IndexOf(text, StringComparison.Ordinal) ("CountUntilLimitInvariant", "state.values.2.toNat ≤ limit.toNat\n", "state.values.2.toNat ≤ limit.toNat - state.values.1.toNat\n", "show (count + 1).toNat ≤ limit.toNat"), - ("CountUntilWrongException", "then some \"System.InvalidOperationException\" else none\n\nbind_entry entry", - "then some \"System.ArgumentException\" else none\n\nbind_entry entry", + ("CountUntilWrongException", "then some .invalidOperation else none\n\nbind_entry entry", + "then some .argument else none\n\nbind_entry entry", "case raises => simp only [bumpContract, ite_true]") }) { @@ -449,6 +449,40 @@ int[] RejectedLines(string dll, string proof, string name, bool existingArtifact return Regex.Matches(failure, @"Proof\.lean:(\d+):\d+: error").Select(m => int.Parse(m.Groups[1].Value)).Distinct().ToArray(); } +// The result `selector` returns from empty memory for every `uint`, refuted where `dll` returns another value. +void Refute(string dll, string selector, string name, string result, uint witness, bool existingArtifacts) +{ + var output = Path.Combine(root, name); + Directory.CreateDirectory(output); + var proof = Path.Combine(output, "Refutation.lean"); + File.WriteAllText(proof, $$""" + import Extracted + import CIL.Safety.MethodContract + open CIL.Safety + namespace ExampleProof + /-- The result a correct build returns for every word from empty caller memory. -/ + def contract : MethodContract CIL.W32 where + args input := [.scalar (.i32 input)] + pre _ memory := memory = Memory.empty + footprint _ := [] + post input _ _ values := values = [.scalar (.i32 ({{result}}))] + /-- The incorrect build returns another value at `{{witness}}`. -/ + theorem refuted : contract.Refuted Extracted.program 0 := + .of_return (input := {{witness}}) (initial := Memory.empty) rfl + (rfl : invoke Extracted.program 256 0 [.scalar (.i32 {{witness}})] Memory.empty = .ok _) + (by rintro ⟨-, out, rfl, wrong⟩; revert wrong; dsimp only [contract]; decide) + #print axioms refuted + end ExampleProof + + """); + var refutation = verification.Verify(dll, "Examples", selector, proof, output, theorem: "ExampleProof.refuted", + existingArtifacts: existingArtifacts).refutation; + var entry = JsonDocument.Parse(File.ReadAllText(Path.Combine(output, "identity.json"))).RootElement + .GetProperty("entry").GetString(); + if (refutation?.method.signature != entry) + throw new InvalidOperationException($"{name} has no verified refutation of its method"); +} + void Prove(string dll, string selector, string name, string source) { var output = Path.Combine(root, name); @@ -464,9 +498,9 @@ void Prove(string dll, string selector, string name, string source) CIL.Safety.invoke Extracted.program 6 0 [.scalar (.i32 0)] CIL.Safety.Memory.empty = .error ⟨0, 10, .fuelExhausted⟩ ∧ Extracted.method0.code[5]? = some .sub ∧ - CIL.Safety.step Extracted.method0 .sub 5 [] ⟨0, [], [], [], 0⟩ + CIL.Safety.step Extracted.method0 .sub 5 [] ⟨0, [], [], 0⟩ [.scalar (.i32 1), .scalar (.i32 4294967295)] CIL.Safety.Memory.empty = - .ok (.next 6 [.scalar (.i32 4294967294)] ⟨0, [], [], [], 0⟩ CIL.Safety.Memory.empty) := ⟨rfl, rfl, rfl⟩ + .ok (.next 6 [.scalar (.i32 4294967294)] ⟨0, [], [], 0⟩ CIL.Safety.Memory.empty) := ⟨rfl, rfl, rfl⟩ """); Console.WriteLine("PASS insufficient fuel and maximum unsigned decrement boundary"); @@ -494,21 +528,9 @@ CIL.Safety.invoke Extracted.program 6 0 [.scalar (.i32 0)] CIL.Safety.Memory.emp throw new InvalidOperationException("Feature loop failed to reject reachable array: " + feature.Output); Console.WriteLine("PASS fixed feature selection inside a loop"); -var wrong = Path.Combine(root, "WrongContract"); -Directory.CreateDirectory(wrong); -File.Copy(Path.Combine(correct, "verified.json"), Path.Combine(wrong, "verified.json"), overwrite: true); -bool rejected = false; -try -{ - verification.Verify(assembly, "Examples", "Loops::WrongCountdown(uint)", contract, wrong); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - rejected = true; -} -if (!rejected || File.Exists(Path.Combine(wrong, "verified.json"))) - throw new InvalidOperationException("Incorrect countdown passed or retained a verification report"); -Console.WriteLine("PASS unchanged universal contract rejects incorrect loop"); +// WrongCountdown stops at one, so the countdown contract is refuted where it returns 1. +Refute(assembly, "Loops::WrongCountdown(uint)", "WrongContract", "0", 1, existingArtifacts: false); +Console.WriteLine("PASS countdown contract refuted for the incorrect loop at 1"); var constantMeasure = File.ReadAllText(contract).Replace("(fun s => s.values.toNat)", "(fun _ => 0)", StringComparison.Ordinal); @@ -684,8 +706,8 @@ theorem exhausted (fuel : Nat) (args : List CIL.Safety.Value) induction fuel with | zero => rfl | succ fuel ih => - rw [CIL.Safety.run_next Extracted.lookup0 Extracted.method0_instruction0 (show - CIL.Safety.step Extracted.method0 (.branch 0) 0 args frame [] memory = + rw [CIL.Safety.run_next Extracted.program_method0 Extracted.method0_instruction0 (show + CIL.Safety.step Extracted.method0 (.br 0) 0 args frame [] memory = .ok (.next 0 [] frame memory) from rfl)] exact ih theorem correctness (fuel : Nat) (x : CIL.W32) : @@ -839,6 +861,28 @@ .ok [.scalar (.i32 3)] := rfl Console.WriteLine("PASS rejection: " + mutation); } +void VerifyNondecreasing(string assembly, string selector, string example, string positive, string wrongAssembly, + string output, string result, string witness) +{ + verification.VerifyContractVariant(assembly, "Examples", selector, positive, wrongAssembly, selector, output, $$""" + /-- The loop's universal result from empty memory. -/ + def contract : MethodContract W32 where + args input := [.scalar (.i32 input)] + pre _ memory := memory = Memory.empty + footprint _ := [] + post input _ _ values := values = [.scalar (.i32 {{result}})] + """, """ + /-- The universal invocation theorem supplies a framed return from empty memory. -/ + theorem correctness : contract.Holds Extracted.program 0 := by + rintro value _ rfl + obtain ⟨fuel, final, values, ran, result⟩ := ExampleProof.correctness _ _ ⟨value, rfl, rfl⟩ + exact ⟨fuel, (final, values), ran, by constructor <;> simp [Memory.empty], values, rfl, result⟩ + """, File.ReadAllText(At(witness)), example: example, imports: """ + import CIL.Safety.LoopScaffold + import CIL.Safety.SegmentContract + """); +} + delegate void FillWordsDelegate(ref ulong destination, ulong value, int count); delegate uint CopyUntilZeroDelegate(ReadOnlySpan values, ref uint output); delegate bool AllZeroDelegate(ReadOnlySpan values); diff --git a/Tests/LoweredCallRefusal.lean b/Tests/LoweredCallRefusal.lean new file mode 100644 index 000000000..6f1e03dc5 --- /dev/null +++ b/Tests/LoweredCallRefusal.lean @@ -0,0 +1,281 @@ +import CIL.Extraction.DecodedDefinition +import CIL.Extraction.DecodedMethod +import Tests.Support.RawDecoding + +/-! Decoder refusal tests for incomplete method-token operands. -/ + +namespace Tests.LoweredCallRefusal +open CIL CIL.Extraction + +/-- A managed member with no runtime model, embedded in a static caller. -/ +private def body (opcode : Nat) (owner : TypeExpr) (member : MetadataName) (name : String) : + RawMethod := { + token := 1, owner := "Fixture", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := false, maxStack := 0, genericParameters := [], + code := [⟨0, opcode, .call { + method := { + owner, name := member, hasThis := opcode != 40, genericArity := 0, + returnType := .void, parameters := [] }, + declared := { convention := if opcode == 40 then 0 else 0x20 }, name, target := .managed 2, + typeArguments := [], parameterCount := 0, returnsValue := false }⟩] } + +/-- Removing a managed member's direct link leaves a hole tagged with its own opcode. -/ +private def withoutLink (opcode : Nat) (member : MetadataName) (name : String) : Bool := Id.run do + let source := body opcode (.object "Fixture" "Fixture") member name + let some raw := source.code.head? | return false + let requirement := fun link => runtimeCatalog.callRequirement + (LayoutEnvironment.callLayouts {}) .scalar link raw + return (requirement (some (.direct 0)) matches .linked) && + decide ((decodeRaw .scalar {} [] [true] source []).map Method.code = + liveHoleAt 0 opcode) && + (requirement none).accepts false (.unadmitted opcode) && + !(requirement none).accepts true (.unadmitted opcode) + +/-- A direct managed call requires its link. -/ +theorem unclaimed : withoutLink 40 name!"Run" "Run" = true := rfl +#print axioms unclaimed + +/-- A virtual managed call requires its dispatch binding. -/ +theorem virtualCall : withoutLink 111 name!"Run" "Run" = true := rfl +#print axioms virtualCall + +/-- A managed constructor requires its construction binding. -/ +theorem construction : withoutLink 115 name!".ctor" ".ctor" = true := rfl +#print axioms construction + +/-- Both helpers use the same scoped vector type in each occurrence of their type argument. -/ +private def vectorSite (read : Bool) (element : String) : RawInstruction := Id.run do + let vector := fun name => TypeExpr.apply (.constructor intrinsicsScope + "System.Runtime.Intrinsics.Vector128`1") (.atom runtimeScope name) + let source := vector element + let target := vector "System.UInt64" + let arguments := if read then [source] else [source, target] + let byteRef := TypeExpr.byref (.atom runtimeScope "System.Byte") + let signature := if read then + "!!0 System.Runtime.CompilerServices.Unsafe::ReadUnaligned(System.Byte&)" + else "!!1 System.Runtime.CompilerServices.Unsafe::BitCast(!!0)" + return ⟨0, 40, .call { + method := { + owner := .atom runtimeScope "System.Runtime.CompilerServices.Unsafe", + name := if read then name!"ReadUnaligned" else name!"BitCast", hasThis := false, + genericArity := arguments.length, methodArguments := arguments, + parameters := [if read then byteRef else source], + returnType := if read then source else target } + declared := { + convention := 0x10, returnType := .parameter (if read then 0 else 1), + parameters := [if read then byteRef else .parameter 0] } + name := if read then "ReadUnaligned" else "BitCast", typeArguments := arguments, + target := .external s!"{runtimeScope}::{signature}", parameterCount := 1, + returnsValue := true }⟩ + +/-- A change of vector element alone is sufficient to refuse either transfer. -/ +theorem integerVectorTransfers : + let decode := fun raw => methodCode .scalar {} [] [true] + { body 40 .void name!"Run" "Run" with code := [raw] } + let control := vectorSite false "System.Byte" + (match control.operand with + | .call site => decide (decode control = [.call site.method {}]) + | _ => false) = true ∧ + decode (vectorSite false "System.Double") = [.unadmitted 40] ∧ + (match (vectorSite true "System.Byte").operand with + | .call site => decide (decode (vectorSite true "System.Byte") = [.call site.method {}]) + | _ => false) = true ∧ + decode (vectorSite true "System.Double") = [.unadmitted 40] := by decide +kernel + +#print axioms integerVectorTransfers + +/-- The one-instruction decoder requires a model for its live arithmetic call. -/ +private def arithmeticCode (env : LayoutEnvironment) (site : CallSite) := + (decodeRaw .scalar env [] [true] + { body 40 .void name!"Run" "Run" with code := [⟨0, 40, .call site⟩] } []).map Method.code + +/-- Both arithmetic members bind the raw operand and the closed signature independently. -/ +private def arithmeticChecks (env : LayoutEnvironment) (site : CallSite) + (unsupported : CallSite) (expected : RuntimeModel) : Bool := + let method := site.method + let wrong := TypeExpr.atom runtimeScope "System.Boolean" + let refusals := [ + { site with method.owner := .atom "Fixture" "System.Math" }, + { site with method.name := name!"Wrong" }, { site with name := "Wrong" }, + { site with method.genericArity := 1 }, { site with method.hasThis := true }, + { site with parameterCount := site.parameterCount + 1 }, + { site with method.parameters := method.parameters.set 0 wrong }, + { site with method.returnType := wrong }, { site with declared.convention := 0x20 }, + { site with target := .external "Wrong" }, unsupported] + runtimeModel env .call method == some expected && + decide (arithmeticCode env site = .ok [.call method {}]) && + refusals.all fun changed => decide (arithmeticCode env changed = liveHoleAt 0 40) + +/-- A signed widening multiply accepts int32 input and rejects an int16 widening signature. -/ +theorem bigMulBinding : + let site := fun (element : String) => + let word := TypeExpr.atom runtimeScope element + let method : MethodRef := { + owner := .atom runtimeScope "System.Math", name := name!"BigMul", hasThis := false, + genericArity := 0, returnType := .atom runtimeScope "System.Int64", + parameters := [word, word] } + let target := s!"System.Int64 System.Math::BigMul({element},{element})" + ({ method, name := "BigMul", declared := method.signature 0, + target := .external s!"{runtimeScope}::{target}", + typeArguments := [], parameterCount := 2, returnsValue := true } : CallSite) + arithmeticChecks {} (site "System.Int32") (site "System.Int16") + (.intrinsic (.scalar (.bigMul32 true)) 2) = true := by decide +kernel + +#print axioms bigMulBinding + +/-- A crossed result selects no arithmetic overload even before target text or layouts are read. -/ +theorem arithmetic_result_signedness : + let unsigned := TypeExpr.atom runtimeScope "System.UInt32" + let multiply : MethodRef := { + owner := .atom runtimeScope "System.Math", name := name!"BigMul", hasThis := false, + genericArity := 0, parameters := [unsigned, unsigned], + returnType := .atom runtimeScope "System.Int64" } + let pair := integerPairIdentity + let divide := { multiply with + name := name!"DivRem", parameters := [int32Type, int32Type], + returnType := .aggregate (pair "System.UInt32") 0 } + let distinguishes := fun (crossed : MethodRef) (result : TypeExpr) => + (runtimeCatalog.table.entry? .call crossed).any fun entry => + (entry.select? { crossed with returnType := result }).isSome && + (entry.select? crossed).isNone + distinguishes multiply (.atom runtimeScope "System.UInt64") ∧ + distinguishes divide (.aggregate (pair "System.Int32") 0) := by decide +kernel + +#print axioms arithmetic_result_signedness + +/-- DivRem binds the tuple's indexed declaration, both field types, packing and alignment. -/ +theorem divRemTupleLayout : + let pair := "System.ValueTuple`2" + let identity := integerPairIdentity "System.UInt64" + let word := TypeExpr.atom runtimeScope "System.UInt64" + let method : MethodRef := { + owner := .atom runtimeScope "System.Math", name := name!"DivRem", + genericArity := 0, hasThis := false, returnType := .aggregate identity 1, + parameters := [word, word] } + let site : CallSite := { + method, declared := method.signature 0, name := "DivRem", + target := .external s!"{runtimeScope}::{pair} System.Math::DivRem(System.UInt64,System.UInt64)", + typeArguments := [], parameterCount := 2, returnsValue := true } + let row : RawLayout := { + identity, auto := true, explicit := false, declaredSize := 0, + packing := 0, fields := [⟨.unsigned64, none⟩, ⟨.unsigned64, none⟩] } + let env := fun (raw : RawLayout) alignment => + let computed := (⟨⟨16, [⟨0, 8, false⟩, ⟨8, 8, false⟩]⟩, alignment⟩ : ComputedLayout) + ({ rawLayouts := [{ row with identity := "Fixture" }, raw], + layouts := #[computed, computed] } : LayoutEnvironment) + let refused := fun raw alignment => + arithmeticCode (env raw alignment) site = liveHoleAt 0 40 + let boolean := TypeExpr.atom runtimeScope "System.Boolean" + let result := "System.ValueTuple`2" + let invalid := { method with + parameters := [boolean, boolean] + returnType := .aggregate (integerPairIdentity "System.Boolean") 1 } + let target := s!"{result} System.Math::DivRem(System.Boolean,System.Boolean)" + let unsupported := { site with + method := invalid, declared := invalid.signature 0, + target := .external (runtimeScope ++ "::" ++ target) } + arithmeticChecks (env row 8) site unsupported (.intrinsic (.scalar (.divRem 64 false)) 2) ∧ + arithmeticCode (env row 8) { site with declared.returnType := .aggregate identity 0 } = + liveHoleAt 0 40 ∧ + refused { row with fields := row.fields.set 0 ⟨.signed64, none⟩ } 8 ∧ + refused { row with fields := row.fields.set 1 ⟨.signed64, none⟩ } 8 ∧ + refused { row with fields := List.replicate 512 ⟨.unsigned64, none⟩ } 8 ∧ + refused { row with packing := 1 } 8 ∧ refused row 1 := by decide +kernel + +#print axioms divRemTupleLayout + +/-- The open vector parameter of Sum belongs to the method. -/ +theorem sumDeclaration : + let owner := "System.Runtime.Intrinsics.Vector128" + let vector := TypeExpr.apply (.constructor intrinsicsScope (owner ++ "`1")) + let word := TypeExpr.atom runtimeScope "System.UInt64" + let site : CallSite := { + method := { + owner := .atom intrinsicsScope owner, name := name!"Sum", hasThis := false, + genericArity := 1, methodArguments := [word], returnType := word, + parameters := [vector word] } + name := "Sum", parameterCount := 1, returnsValue := true, typeArguments := [word], + declared := { + convention := 0x10, returnType := .parameter 0, parameters := [vector (.parameter 0)] } + target := .external s!"{intrinsicsScope}::!!0 {owner}::Sum({owner}`1)" } + runtimeModel {} .call site.method = some (.intrinsic (.vector (.sum64 128)) 1) ∧ + arithmeticCode {} site = .ok [.call site.method {}] ∧ + arithmeticCode {} { site with declared.parameters := [vector (.ownerParameter 0)] } = + liveHoleAt 0 40 := by decide +kernel + +#print axioms sumDeclaration + +/-- A direct call to the length getter of a span of the fixture struct. -/ +private def lengthBody : RawMethod := + let owner := TypeExpr.apply (.constructor runtimeScope "System.Span`1") + (.aggregate "Fixture::Words" 0) + let int := TypeExpr.atom runtimeScope "System.Int32" + let site : CallSite := { + method := ⟨owner, name!"get_Length", true, 0, int, [], []⟩, + declared := ⟨0x20, int, []⟩, name := "get_Length", typeArguments := [], + target := .external s!"{runtimeScope}::System.Int32 System.Span`1::get_Length()", + parameterCount := 0, returnsValue := true } + { body 40 owner site.method.name site.name with code := [⟨0, 40, .call site⟩] } + +/-- A resolving getter rejects an unrelated one-in, one-out operation. A getter outside + the layouts and an unknown span member decode only to their opcode's hole. -/ +theorem spanClaims : + let int := TypeExpr.atom runtimeScope "System.Int32" + let rows : List RawLayout := [{ + identity := "Fixture::Words", explicit := false, packing := 0, declaredSize := 0, + fields := [⟨.unsigned32, none⟩] }] + let full : LayoutEnvironment := + { rawLayouts := rows, layouts := (computeLayouts rows).getD #[] } + let unknown := body 40 (TypeExpr.apply (.constructor runtimeScope "System.Span`1") int) + name!"Missing" "Missing" + let missing := fun (source : RawMethod) (claim : SiteClaim) => + source.code.all fun raw => siteClaim {} none raw == claim && + !(runtimeCatalog.siteAgrees {} .scalar raw .pop none true) && + runtimeCatalog.siteAgrees {} .scalar raw (.unadmitted 40) none false && + decide ((decodeRaw .scalar {} [] [true] source []).map Method.code = + liveHoleAt 0 40) + (lengthBody.code.all fun raw => Id.run do + let .call site := raw.operand | return false + let lengthField : FieldRef := ⟨site.method.owner, 0, int, name!"_length"⟩ + return siteClaim full none raw == .model && + runtimeCatalog.siteAgrees full .scalar raw (.call site.method {}) none true && + !(runtimeCatalog.siteAgrees full .scalar raw (.ldflda lengthField) none true)) ∧ + missing lengthBody .model ∧ missing unknown .refused := by + decide +kernel + +#print axioms spanClaims + +/-- The empty layout environment of the method with an unknown call. -/ +private def emptyEnvironment : LayoutEnvironment := {} + +cil_decoded_method unknownMethod := Method.ofRaw? .scalar emptyEnvironment [] [false] + (body 40 (.atom "Fixture" "Fixture") name!"Missing" "Missing") [] + +/-- The generated code retains the opcode of the dead unresolved member. -/ +theorem unknownMethodCode : unknownMethod.code = [.unadmitted 40] := rfl + +#print axioms unknownMethodCode + +/-- The linked instance slot takes the receiver type from the preceding constrained prefix. -/ +private def constrainedSlot : CallSite := { + method := { + owner := .object "Fixture" "Fixture.Interface", name := name!"Read", + hasThis := true, genericArity := 0, returnType := .void, parameters := [] }, + declared := { convention := 0x20 }, name := "Read", target := .managed 1, typeArguments := [], + parameterCount := 0, returnsValue := false } + +/-- Closed decoding preserves a constrained receiver at a linked call and refuses an unowned pc. -/ +theorem constrainedLinkCode : + let receiver := TypeExpr.atom runtimeScope "System.Int32" + let source := { body 111 constrainedSlot.declaringType name!"Read" "Read" with + code := [⟨0, 0xFE16, .type receiver⟩, ⟨6, 111, .call constrainedSlot⟩] } + let decode := fun links => + (decodeRaw .scalar {} links [true, true] source []).map Method.code + decode [(1, .direct 1)] = .ok [.nop, .call constrainedSlot.method + { constrained := some receiver }] ∧ decode [] = liveHoleAt 1 111 := by + exact ⟨rfl, rfl⟩ + +#print axioms constrainedLinkCode + +end Tests.LoweredCallRefusal diff --git a/Tests/ManagedDispatch.cs b/Tests/ManagedDispatch.cs index 11997b0c1..d7e7ea11d 100644 --- a/Tests/ManagedDispatch.cs +++ b/Tests/ManagedDispatch.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs @@ -78,7 +83,7 @@ string RunCase(string args, int fuel = 128) => else if (name == "NullLength") { claims.Add($"(match {RunCase("")} with | .error fault => some fault.fault | " + - ".ok _ => none) = some (.thrown \"System.NullReferenceException\")"); + ".ok _ => none) = some (.thrown .nullReference)"); } else { @@ -89,16 +94,18 @@ string RunCase(string args, int fuel = 128) => var raise = methods.Single(method => method.Name == "Raise").MetadataToken.ToUInt32(); var owner = module.Assembly.Name.FullName + "::Examples.ManagedDispatch"; string Identity(uint token) => $"⟨{System.Text.Json.JsonSerializer.Serialize(owner)}, {token}, []⟩"; - string Table(string targets) => $".methodTable ⟨dispatchSignature, some #[{targets}], none⟩"; - var table = Table(Identity(repeat) + ", " + Identity(raise)); + string Table(params string[] targets) => + $".methodTable ⟨dispatchSignature, some #[{string.Join(", ", targets)}], none, " + + $"{targets.Length}, by decide⟩"; + var table = Table(Identity(repeat), Identity(raise)); string Args(string view, int index, int value) => $"{view}, .scalar (.i32 {index}), .scalar (.i32 {value})"; claims.Add($"({RunCase(Args(table, 0, 3))}).map Prod.snd = .ok [.scalar (.i32 17)]"); string Fault(string args, int fuel = 128) => $"(match {RunCase(args, fuel)} with | .error fault => some fault.fault | .ok _ => none)"; - claims.Add($"{Fault(Args(table, 1, 2))} = some (.thrown \"System.ArgumentException\")"); - claims.Add($"{Fault(Args(table, 2, 0))} = some (.thrown \"System.IndexOutOfRangeException\")"); - claims.Add($"{Fault(Args(".methodTable ⟨dispatchSignature, none, none⟩", 0, 0))} = " + - "some (.thrown \"System.NullReferenceException\")"); + claims.Add($"{Fault(Args(table, 1, 2))} = some (.thrown .argument)"); + claims.Add($"{Fault(Args(table, 2, 0))} = some (.thrown .indexOutOfRange)"); + claims.Add($"{Fault(Args(".methodTable ⟨dispatchSignature, none, none, 0, by decide⟩", 0, 0))} = " + + "some (.thrown .nullReference)"); claims.Add($"{Fault(Args(Table(Identity(uint.MaxValue)), 0, 0))} = some .invalidState"); claims.Add($"({RunCase(Args(table, 0, 3), 1)}).isOk = false"); } @@ -107,37 +114,56 @@ string Fault(string args, int fuel = 128) => ⟨.atom CIL.Extraction.runtimeScope "System.Int32", [.atom CIL.Extraction.runtimeScope "System.Int32"]⟩ """ + "\ntheorem managed_dispatch_result :\n" + string.Join(" ∧\n", claims) + - " := " + (claims.Count == 1 ? "by rfl" : - "by exact ⟨" + string.Join(", ", claims.Select(_ => "by rfl")) + "⟩") + + " := " + (claims.Count == 1 ? "by cil_kernel_rfl" : + "by exact ⟨" + string.Join(", ", claims.Select(_ => "by cil_kernel_rfl")) + "⟩") + "\n#print axioms managed_dispatch_result\n"); evidence.Add(proof, "PASS compiled typed indirect dispatch: " + name); if (name != "Address") continue; - var call = source.Split('\n').First(line => line.TrimStart().StartsWith(".managed (.call ", - StringComparison.Ordinal)); - var address = source.Split('\n').First(line => line.TrimStart().StartsWith(".managed (.address ", - StringComparison.Ordinal)); - var changedTarget = Regex.Replace(call, @"method := \d+", "method := 999999"); - foreach (var (mutation, theorem, before, after, marker) in new[] + var call = SingleLink(source, 0, ".indirect "); + var address = SingleLink(source, 0, ".address "); + const string changeSignature = """ + fun op => match op with + | .calli signature tail => .calli { signature with result := .void } tail + | _ => op + """; + const string dropTail = "fun op => match op with | .calli s _ => .calli s false | _ => op"; + const string callCheck = "method0_calls_validated"; + const string flowCheck = "method0_features_validated"; + var signatureChange = changeSignature.ReplaceLineEndings(" "); + var calls = NarrowEvidence(source, callCheck); + var numericPointer = ForgeDecodedInstruction(calls, 0, address.Pc, "fun _ => .ldc_i8 0"); + var addressEntry = $"({address.Pc}, method0Link{address.Pc}), "; + var numericWithoutLink = numericPointer.Replace(addressEntry, "", StringComparison.Ordinal); + if (numericPointer == numericWithoutLink) + throw new InvalidOperationException("Missing address entry before the indirect call"); + var flow = NarrowEvidence(source, flowCheck); + var badIndex = ManagedPatterns.LinkTarget.Replace(call.Link, "method := 999999"); + var badOwner = call.Link.Replace("Examples.ManagedDispatch", "Examples.ForgedDispatch", StringComparison.Ordinal); + const string checkedRaw = "method0Closed) method0Reachable method0"; + var convention = $$""" + { method0Closed with code := method0Closed.code.modify {{call.Pc}} fun raw => + { raw with operand := match raw.operand with + | .indirectCall result args hasThis explicitThis _ => .indirectCall result args hasThis explicitThis 1 + | operand => operand } }) method0Reachable method0 + """; + var badConvention = calls.Replace(checkedRaw, convention, StringComparison.Ordinal); + if (badIndex == call.Link || badOwner == call.Link || badConvention == calls) + throw new InvalidOperationException("Missing managed target, owner or convention in Address"); + foreach (var (mutation, mutated, marker) in new[] { - ("target", "method0_calls_validated", call, changedTarget, "ManagedCallsValid"), - ("identity", "method0_calls_validated", call, - call.Replace("Examples.ManagedDispatch", "Examples.ForgedDispatch", StringComparison.Ordinal), - "ManagedCallsValid"), - ("signature", "method0_calls_validated", call, - call.Replace("System.Int32", "System.UInt32", StringComparison.Ordinal), "ManagedCallsValid"), - ("numeric-pointer", "method0_calls_validated", address, " .const64 0,", "ManagedCallsValid"), - ("convention", "method0_calls_validated", "] false false 0 },", "] false false 1 },", - "ManagedCallsValid"), - ("tail", "method0_features_validated", call, - call.Replace("] true)", "] false)", StringComparison.Ordinal), "FeatureFlowValid") + ("target", ForgeLink(calls, 0, call.Pc, badIndex), "ManagedCallsValid"), + ("identity", ForgeLink(calls, 0, call.Pc, badOwner), "ManagedCallsValid"), + ("address-target", ForgeLink(calls, 0, address.Pc, ".address 0"), "ManagedCallsValid"), + ("signature", ForgeDecodedInstruction(calls, 0, call.Pc, signatureChange), "ManagedCallsValid"), + ("numeric-pointer-link", numericPointer, "ManagedCallsValid"), + ("numeric-pointer", numericWithoutLink, "ManagedCallsValid"), + ("convention", badConvention, "ManagedCallsValid"), + ("tail", ForgeDecodedInstruction(flow, 0, call.Pc, dropTail), "FeatureFlowValid") }) { - var narrowed = NarrowEvidence(source, theorem); - if (before == after || !narrowed.Contains(before, StringComparison.Ordinal)) - throw new Exception("Missing managed mutation: " + mutation); - var path = Path.Combine(root, "Rejected-" + mutation + ".lean"); - File.WriteAllText(path, narrowed.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS exact managed dispatch evidence rejection: " + mutation, marker); + var path = Path.Combine(root, $"Rejected-{mutation}.lean"); + File.WriteAllText(path, mutated); + rejected.Add(path, $"PASS exact managed dispatch evidence rejection: {mutation}", marker); } } foreach (var mutation in new[] { "convention", "prefix-entry" }) @@ -211,28 +237,27 @@ string Invoke(string value) => "\n#print axioms indirect_value_result\n"); evidence.Add(proof, "PASS aggregate/vector indirect transfer: " + entry.Name); if (entry.Name != "Generic") continue; - var call = source.Split('\n').First(line => line.TrimStart().StartsWith(".managed (.call ", - StringComparison.Ordinal)); - var aggregate = Regex.Match(call, @"\.aggregate ""[^""]+"" 0").Value; - var pair = ".aggregate " + System.Text.Json.JsonSerializer.Serialize( - module.Assembly.Name.FullName + "::Examples.IndirectValues/Pair") + " 0"; - var catalog = call.IndexOf(" } [", StringComparison.Ordinal); - if (aggregate.Length == 0 || catalog < 0) throw new Exception("Missing generic callable type"); - // Box and Pair both occupy 16 bytes; their scoped type identities still differ. - var forged = call[..catalog].Replace(aggregate, pair, StringComparison.Ordinal) + call[catalog..]; - if (call == forged) throw new Exception("Missing scoped generic callable signature"); + var call = SingleLink(source, 0, ".indirect "); + var owner = System.Text.Json.JsonSerializer.Serialize( + $"{module.Assembly.Name.FullName}::Examples.IndirectValues/Pair"); + var forged = $$""" + fun op => match op with + | .calli signature tail => + .calli { signature with result := .aggregate {{owner}} 0 } tail + | _ => op + """; var narrowed = NarrowEvidence(source, "method0_calls_validated"); var path = Path.Combine(root, "Rejected-aggregate-identity.lean"); - File.WriteAllText(path, narrowed.Replace(call, forged, StringComparison.Ordinal)); + File.WriteAllText(path, ForgeDecodedInstruction(narrowed, 0, call.Pc, forged.ReplaceLineEndings(" "))); rejected.Add(path, "PASS distinct aggregate callable identity rejected", "ManagedCallsValid"); foreach (var (label, pattern) in new[] { - ("calli-layout", @"(\.indirectCall \(\.aggregate ""[^""]+"" )\d+"), - ("address-layout", @"(\.call \{[^\r\n]*?returnType := \.aggregate ""[^""]+"" )\d+"), - ("address-type-argument", @"(typeArguments := \[\.aggregate ""[^""]+"" )\d+") + ("calli-layout", ManagedPatterns.IndirectCallLayout), + ("address-layout", ManagedPatterns.ReturnLayout), + ("address-type-argument", ManagedPatterns.TypeArgumentLayout) }) { - var match = Regex.Match(narrowed, pattern); + var match = pattern.Match(narrowed); if (!match.Success) throw new Exception("Missing raw callable layout: " + label); var changed = narrowed.Replace(match.Value, match.Groups[1].Value + "999999", StringComparison.Ordinal); var invalid = Path.Combine(root, "Rejected-" + label + ".lean"); @@ -241,13 +266,36 @@ string Invoke(string value) => } var local = NarrowEvidence(source, "method0_specialization_validated") .Replace("by constructor <;> rfl", "by decide +kernel", StringComparison.Ordinal); - var signature = Regex.Match(local, @"(\.functionPointer \(\.aggregate ""[^""]+"" )\d+"); + var signature = ManagedPatterns.FunctionPointerLayout.Match(local); if (!signature.Success) throw new Exception("Missing callable local type"); var invalidLocal = Path.Combine(root, "Rejected-local-layout.lean"); File.WriteAllText(invalidLocal, local.Replace(signature.Value, signature.Groups[1].Value + "999999", StringComparison.Ordinal)); - rejected.Add(invalidLocal, "PASS raw callable local layout binding", "SpecializationValid"); + rejected.Add(invalidLocal, "PASS raw callable local layout binding", ClosedArgumentsMismatch); } } evidence.Check(); rejected.Check(); + +/// The patterns that find generated text to mutate. +static partial class ManagedPatterns +{ + /// The method a link names. + [GeneratedRegex(@"method := \d+")] + internal static partial Regex LinkTarget { get; } + + // Each layout pattern captures the text before the layout index it ends with. + const string ScopedIdentity = """\("[^"]+" \+\+ "[^"]+"\)"""; + + [GeneratedRegex($"""(\.indirectCall \(\.aggregate {ScopedIdentity} )\d+""")] + internal static partial Regex IndirectCallLayout { get; } + + [GeneratedRegex($$"""(\.call \{[^\r\n]*?returnType := \.aggregate {{ScopedIdentity}} )\d+""")] + internal static partial Regex ReturnLayout { get; } + + [GeneratedRegex($"""(typeArguments := \[\.aggregate {ScopedIdentity} )\d+""")] + internal static partial Regex TypeArgumentLayout { get; } + + [GeneratedRegex($"""(locals := \[\.functionPointer \(\.aggregate {ScopedIdentity} )\d+""")] + internal static partial Regex FunctionPointerLayout { get; } +} diff --git a/Tests/ManagedPointerValues.lean b/Tests/ManagedPointerValues.lean index 2de319b2f..f37b9b2ed 100644 --- a/Tests/ManagedPointerValues.lean +++ b/Tests/ManagedPointerValues.lean @@ -1,6 +1,9 @@ -import CIL.Safety.Execution +import CIL.Safety.StepComposition +import CIL.Extraction.KernelEquation -/-! Managed pointer values retain exact signatures and cannot be forged from integers. -/ +/-! Managed pointer values retain exact signatures and cannot be forged from integers. An +indirect call selects its body through `indirectTarget`, which a kernel-checked fact decides, +so a call through a pointer with a long owner steps without the elaborator comparing owners. -/ namespace Tests.ManagedPointerValues @@ -16,37 +19,148 @@ private def frame : Frame := { activation := 0, locals := [], owned := [] } /-- A table lookup retains stable identity rather than turning it into a numeric address. -/ theorem table_provenance : - managedStep (.tableElement signature) 0 frame - [.scalar (.i32 1), .methodTable ⟨signature, some #[other, identity], none⟩] memory = - .ok (.next 1 [.methodPointer ⟨signature, some identity⟩] frame memory) := rfl + instruction (.ldelem .i) + [.scalar (.i32 1), + .methodTable ⟨signature, some #[other, identity], none, 2, by decide⟩] memory = + .ok (memory, [.methodPointer ⟨signature, some identity⟩]) := rfl #print axioms table_provenance +/-- The typed `ldelem` of a table's own pointer type reads the same entry; an element type of + another signature is an ill-typed state, and a null table throws the named access fault. -/ +theorem typed_table_provenance : + typedElementInstruction (.method signature) false + [.scalar (.i32 1), + .methodTable ⟨signature, some #[other, identity], none, 2, by decide⟩] memory = + .ok (memory, [.methodPointer ⟨signature, some identity⟩]) ∧ + typedElementInstruction (.method ⟨signature.result, []⟩) false + [.scalar (.i32 1), + .methodTable ⟨signature, some #[other, identity], none, 2, by decide⟩] memory = + .error .invalidState ∧ + typedElementInstruction (.method signature) false [.scalar (.i32 0), .object .null] memory = + .error (.thrown .nullReference) ∧ + typedElementInstruction (.method signature) true + [.methodPointer ⟨signature, some identity⟩, .scalar (.i32 1), + .methodTable ⟨signature, some #[other, identity], none, 2, by decide⟩] memory = + .error .invalidState := + ⟨rfl, rfl, rfl, rfl⟩ +#print axioms typed_table_provenance + /-- A numerical value is never callable, even when it equals an admitted program index. -/ theorem numeric_pointer_rejected : - managedStep (.call signature [⟨identity, 1⟩] true) 0 frame + calliStep signature true (some (.indirect [⟨identity, 1⟩])) frame [.scalar (.i64 1), .scalar (.i32 42)] memory = .error .invalidState := rfl #print axioms numeric_pointer_rejected /-- A well-typed pointer still needs its exact identity in the admitted target catalog. -/ theorem foreign_target_rejected : - managedStep (.call signature [⟨identity, 1⟩] true) 0 frame + calliStep signature true (some (.indirect [⟨identity, 1⟩])) frame [.methodPointer ⟨signature, some other⟩, .scalar (.i32 42)] memory = .error .invalidState := rfl #print axioms foreign_target_rejected /-- A successful indirect tail call uses the existing frame-retiring driver action. -/ theorem checked_tail_target : - managedStep (.call signature [⟨identity, 1⟩] true) 0 frame + calliStep signature true (some (.indirect [⟨identity, 1⟩])) frame [.methodPointer ⟨signature, some identity⟩, .scalar (.i32 42)] memory = .ok (.tailCall 1 [.scalar (.i32 42)] memory) := rfl #print axioms checked_tail_target -/-- Null array values returned by ordinary helpers retain the same named access fault. -/ +/-- A null array an ordinary helper returns in place of a table throws the named access fault + from `ldlen`, and from `ldelem.i` at a well-formed index; at an index of another width + `ldelem.i` is an ill-typed state, as for a null value array. -/ theorem literal_null_table_rejected : - managedStep .tableLength 0 frame [.object .null] memory = - .error (.thrown "System.NullReferenceException") ∧ - managedStep (.tableElement signature) 0 frame [.scalar (.i32 0), .object .null] memory = - .error (.thrown "System.NullReferenceException") := ⟨rfl, rfl⟩ + instruction .ldlen [.object .null] memory = .error (.thrown .nullReference) ∧ + instruction (.ldelem .i) [.scalar (.i32 0), .object .null] memory = + .error (.thrown .nullReference) ∧ + instruction (.ldelem .i) [.scalar (.i8 0), .object .null] memory = .error .invalidState := + ⟨rfl, rfl, rfl⟩ #print axioms literal_null_table_rejected +/-- The selection `indirectTarget` names is the comparison the call arm made in place over the + catalog of the site's link: a pointer of another signature, a null pointer and a target + outside the catalog all fault alike. -/ +theorem call_selection_unchanged (signature : ManagedSignature) + (catalog : List ManagedTarget) (tail : Bool) (frame : Frame) + (pointer : MethodPointer) (values : List CIL.Safety.Value) (memory : CIL.Safety.Memory) : + calliStep signature tail (some (.indirect catalog)) frame + (.methodPointer pointer :: values) memory = + (do + if pointer.signature != signature then .error .invalidState else pure () + let some identity := pointer.target | .error .invalidState + let some target := catalog.find? (·.identity == identity) | .error .invalidState + let count := signature.parameters.length + if (if tail then values.length != count else values.length < count) then + .error .invalidState else pure () + let arguments ← (values.take count).reverse.mapM (callArgument frame memory) + if tail then return FrameAction.tailCall target.method arguments memory + else return .call target.method arguments (values.drop count) memory) := by + simp only [calliStep, indirectTarget] + cases pointer.signature != signature with + | true => rfl + | false => + simp only [Bool.false_eq_true, ↓reduceIte] + cases pointer.target with + | none => rfl + | some identity => + simp only [Option.bind] + cases catalog.find? (·.identity == identity) <;> rfl +#print axioms call_selection_unchanged + +/-- An owner of 400 bytes: an assembly-qualified generic type over three assembly-qualified + arguments, longer than the elaborator can compare within its recursion limit. -/ +private def longIdentity : ManagedIdentity := + ⟨"Examples, Version=1.0.0.0, Culture=neutral, \ + PublicKeyToken=null::Examples.Dispatching.IndirectHandlers`3", 6, []⟩ + +/-- A catalog whose second entry is the long owner's body, method 1. -/ +private def longCatalog : List ManagedTarget := [⟨other, 2⟩, ⟨longIdentity, 1⟩] + +/-- A body for the step; the indirect call arm reads only its link at pc 1. -/ +private def caller : Method := + { returnsValue := true, locals := [], code := [], links := [(1, .indirect longCatalog)] } + +/-- The call site's link, as an extraction states it. -/ +private theorem caller_link : caller.linkAt 1 = some (.indirect longCatalog) := rfl + +/-- The call site's signature takes one argument. -/ +private theorem parameter_count : signature.parameters.length = 1 := rfl + +/-- The kernel decides the selection. Closed by `rfl`, the same statement stops at the + elaborator's recursion limit while it compares the two owners. -/ +private theorem long_selected : + indirectTarget signature longCatalog ⟨signature, some longIdentity⟩ = some 1 := by + cil_kernel_rfl +#print axioms long_selected + +/-- A call through a pointer to the long owner steps by `step_managed_call_eq` and the kernel's + fact, whatever lies below its argument. -/ +theorem long_owner_call (rest : List CIL.Safety.Value) : + step caller (.calli signature false) 1 [] frame + (.methodPointer ⟨signature, some longIdentity⟩ :: .scalar (.i32 42) :: rest) memory = + .ok (.call 1 [.scalar (.i32 42)] rest memory) := by + simp only [cil_step, caller_link, long_selected, parameter_count] + simp [unspecifiedOperands, CIL.Safety.Value.hasUnspecified, CIL.Value.hasUnspecified] + rfl +#print axioms long_owner_call + +/-- A program whose entry takes the long owner's address and calls it through the pointer. -/ +private def longProgram : Program := + [{ returnsValue := true, locals := [], + links := [(1, .address 1), (2, .indirect longCatalog)], + code := [.ldarg 0, .ldftn longIdentity signature, + .calli signature false, .ret] }, + { returnsValue := true, locals := [], code := [.ldarg 0, .ret] }] + +/-- A whole run through the long owner, closed as an extraction's proofs of indirect calls are: + the kernel evaluates the run, including both owner comparisons. -/ +theorem long_owner_run : + (invoke longProgram 16 0 [.scalar (.i32 42)] Memory.empty).map Prod.snd = + .ok [.scalar (.i32 42)] := by + cil_kernel_rfl +#print axioms long_owner_run + end Tests.ManagedPointerValues diff --git a/Tests/MarshalModelEvidence.cs b/Tests/MarshalModelEvidence.cs new file mode 100644 index 000000000..227afb0ea --- /dev/null +++ b/Tests/MarshalModelEvidence.cs @@ -0,0 +1,52 @@ +partial class Program +{ + // Each retained call binds its closed reference, including the width of a generic transfer. + static void CheckMarshalModels(string root, (string Name, int Pc, string Model, string? UInt16Model)[] cases) + { + var evidence = new GeneratedEvidence(Path.Combine(root, "ModelEvidence")); + foreach (var (name, pc, model, uint16Model) in cases) + { + var directory = Path.Combine(root, name); + var path = Path.Combine(directory, "ModelEvidence.lean"); + File.Copy(Path.Combine(directory, "Extracted.lean"), path, overwrite: true); + var alternate = uint16Model is null ? "none" : $"some ({uint16Model})"; + File.AppendAllText(path, $$""" + + namespace Extracted + open CIL CIL.Extraction + /-- Closing the same declaration with UInt16 selects its own transfer width. + The original site refuses a call to that instantiation. -/ + private def changedElement (raw : RawInstruction) (site : CallSite) + (expected : MemoryOp) : Bool := Id.run do + let arguments := site.typeArguments.set 0 (.atom runtimeScope "System.UInt16") + let close := substituteType arguments + let some result := close site.declared.returnType | return false + let some parameters := site.declared.parameters.mapM close | return false + let method := { site.method with + methodArguments := arguments, returnType := result, parameters } + return runtimeModel environment .call method == some (.memory expected) && + !runtimeCatalog.siteAgrees environment profile raw (.call method {}) none true + + /-- The retained call binds its exact operation, closed operand and open signature. -/ + theorem marshalEvidence : + (match method0Closed.code[{{pc}}]?, method0.code[{{pc}}]? with + | some raw, some (Instruction.call method _) => match raw.operand with + | .call site => + let agrees := fun operand op => + runtimeCatalog.siteAgrees environment profile operand op none true + let forged := { site with declared.convention := 0 } + let changed := { raw with operand := .call forged } + runtimeModel environment .call method == some (.memory ({{model}})) && + agrees raw (.call method {}) && !agrees changed (.call method {}) && + !agrees raw (.newobj method) && + ({{alternate}} : Option MemoryOp).all (changedElement raw site) + | _ => false + | _, _ => false) = true := by decide +kernel + #print axioms marshalEvidence + end Extracted + """); + evidence.Add(path, $"PASS exact MemoryMarshal model for {name}", "marshalEvidence"); + } + evidence.Check(replay: false); + } +} diff --git a/Tests/MarshalModels.lean b/Tests/MarshalModels.lean new file mode 100644 index 000000000..5d44967e1 --- /dev/null +++ b/Tests/MarshalModels.lean @@ -0,0 +1,330 @@ +import CIL.Extraction.DecodedMethod +import CIL.Extraction.BitModels +import CIL.Safety.StepComposition +import Tests.MetadataWorld +import Tests.Support.RawDecoding + +/-! Call-site checks for the MemoryMarshal overloads in the example assembly. -/ + +namespace Tests.MarshalModels +open CIL CIL.Extraction + +/-- One closed instantiation and the operation the extractor emits for it. -/ +private structure Fixture where + name : String + result : TypeExpr + parameters : List TypeExpr + arguments : List TypeExpr + operation : MemoryOp + +/-- The writable or readonly view of one element. -/ +private def view (writable : Bool) (element : TypeExpr) : TypeExpr := + let owner := if writable then "System.Span`1" else "System.ReadOnlySpan`1" + .apply (.constructor runtimeScope owner) element + +/-- All observed overloads, with the source and target elements supplied by the caller. -/ +private def fixtures (source target : TypeExpr) (width targetWidth : Nat) (kind : TransferKind) : + List Fixture := + let byte := TypeExpr.atom runtimeScope "System.Byte" + let span := view true + let readOnly := view false + [⟨"GetReference", .byref source, [span source], [source], .spanReference⟩, + ⟨"GetReference", .byref source, [readOnly source], [source], .spanReference⟩, + ⟨"GetArrayDataReference", .byref source, [.array source], [source], + .observeView .arrayData⟩, + ⟨"AsBytes", span byte, [span source], [source], .spanReinterpret width 1 true⟩, + ⟨"AsBytes", readOnly byte, [readOnly source], [source], .spanReinterpret width 1 false⟩, + ⟨"Cast", span target, [span source], [source, target], + .spanReinterpret width targetWidth true⟩, + ⟨"Cast", readOnly target, [readOnly source], [source, target], + .spanReinterpret width targetWidth false⟩, + ⟨"CreateSpan", span source, [.byref source, int32Type], [source], + .spanFromReference width true⟩, + ⟨"CreateReadOnlySpan", readOnly source, [.byref source, int32Type], [source], + .spanFromReference width false⟩, + ⟨"Read", source, [readOnly byte], [source], .marshalRead kind false⟩, + ⟨"TryRead", booleanType, [readOnly byte, .byref source], [source], .marshalRead kind true⟩, + ⟨"Write", .void, [span byte, .byref source], [source], .marshalWrite kind false⟩, + ⟨"TryWrite", booleanType, [span byte, .byref source], [source], .marshalWrite kind true⟩] + +/-- The actual open declarations, in fixture order. -/ +private def declarations : List OpenSignature := + (fixtures (.parameter 0) (.parameter 1) 0 0 (.scalar 0 false)).map fun fixture => + { convention := 0x10, returnType := fixture.result, parameters := fixture.parameters } + +/-- The closed method reference at a fixture's call. -/ +private def Fixture.method (fixture : Fixture) : MethodRef := + { owner := .atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal", + name := (MetadataName.ofBytes? (fixture.name.toList.map Char.toNat)).getD ⟨0⟩, + hasThis := false, genericArity := fixture.arguments.length, + methodArguments := fixture.arguments, parameters := fixture.parameters, + returnType := fixture.result } + +/-- A live call with a supplied declaration. -/ +private def Fixture.raw (fixture : Fixture) (declared : OpenSignature) : RawInstruction := + let site : CallSite := + { method := fixture.method, declared, name := fixture.name, target := .external "" + typeArguments := fixture.arguments + parameterCount := fixture.parameters.length, returnsValue := fixture.result != .void } + ⟨0, 40, .call site⟩ + +/-- Concrete 32-bit elements and 16-bit Cast targets. -/ +private def scalarSites : List (Fixture × OpenSignature) := + (fixtures int32Type (.atom runtimeScope "System.Int16") 4 2 (.scalar 4 false)).zip declarations + +/-- The same closed signature under a declaration with a different parameter list. -/ +private def changed (declaration : OpenSignature) : OpenSignature := + { declaration with parameters := .void :: declaration.parameters.drop 1 } + +/-- The struct type at layout index zero. -/ +private def packet : TypeExpr := .aggregate "Fixture::Packet" 0 + +/-- A checked struct layout holding the given number of 64-bit fields. -/ +private def sized (words : Nat) : LayoutEnvironment := + let layout : RawLayout := + { identity := "Fixture::Packet", explicit := false, packing := 0, declaredSize := 0 + fields := List.replicate words { type := .unsigned64 } } + let raw := [layout] + { rawLayouts := raw, layouts := (computeLayouts raw).getD #[] } + +/-- A vector of the runtime's specified lane type. -/ +private def vector (element : String) (bits : Nat := 128) : TypeExpr := + let owner := if bits == 128 then "System.Runtime.Intrinsics.Vector128`1" + else if bits == 256 then "System.Runtime.Intrinsics.Vector256`1" + else "System.Runtime.Intrinsics.Vector512`1" + .apply (.constructor intrinsicsScope owner) + (.atom runtimeScope element) + +/-- Every reference resolves to its expected operation. -/ +private def resolves (environment : LayoutEnvironment) (cases : List Fixture) : Bool := + cases.all fun fixture => + runtimeModel environment .call fixture.method == some (.memory fixture.operation) + +/-- Both overloads preserve mutability. Aggregate widths follow each environment's layout. -/ +theorem marshal_resolutions : + resolves {} (scalarSites.map Prod.fst) ∧ + resolves {} (fixtures (vector "System.Byte") int32Type 16 4 (.snapshot .vector128)) ∧ + resolves (sized 1) (fixtures packet packet 8 8 (.snapshot (.aggregate 8))) ∧ + resolves (sized 3) (fixtures packet packet 24 24 (.snapshot (.aggregate 24))) := by + decide +kernel + +#print axioms marshal_resolutions + +/-- Narrow signed integers and enums keep their sign. Boolean and Char use unsigned storage. -/ +theorem marshal_scalar_storage : + resolves {} (fixtures (.atom runtimeScope "System.Int16") int32Type 2 4 (.scalar 2 true)) ∧ + resolves {} (fixtures (.enum "Fixture::Kind" (.atom runtimeScope "System.SByte")) + int32Type 1 4 (.scalar 1 true)) ∧ + resolves {} (fixtures booleanType int32Type 1 4 (.scalar 1 false)) ∧ + resolves {} (fixtures (.atom runtimeScope "System.Char") int32Type 2 4 (.scalar 2 false)) := by + decide +kernel + +#print axioms marshal_scalar_storage + +/-- The selected references must have the exact owner, shape, closed result and parameters. -/ +theorem marshal_reference_shapes : + scalarSites.all (fun (fixture, _) => + let method := fixture.method + [{ method with owner := .apply method.owner int32Type }, + { method with owner := .atom "Other" "System.Runtime.InteropServices.MemoryMarshal" }, + { method with hasThis := true }, { method with genericArity := method.genericArity + 1 }, + { method with methodArguments := [] }, { method with returnType := .pointer .void }, + { method with parameters := [] }].all fun changedMethod => + (runtimeModel {} .call changedMethod).isNone) = true := by decide +kernel + +#print axioms marshal_reference_shapes + +/-- Every member refuses types outside its checked element set, on either side of a Cast. -/ +theorem marshal_excluded_elements : + [view true int32Type, TypeExpr.parameter 0, .pointer int32Type, .byref int32Type, + .object "Fixture" "Class", .apply (.objectConstructor "Fixture" "Box") int32Type, + .atom runtimeScope "System.Single", vector "System.Double", packet].all (fun element => + (fixtures element int32Type 0 4 (.scalar 4 false)).all (fun fixture => + (runtimeModel {} .call fixture.method).isNone) && + ((fixtures int32Type element 4 0 (.scalar 4 false)).filter + (fun fixture => fixture.name == "Cast")).all (fun fixture => + (runtimeModel {} .call fixture.method).isNone)) = true := by decide +kernel + +#print axioms marshal_excluded_elements + +/-- A Cast result keeps the target element identity and the input span's capability. -/ +theorem cast_result_identity : + scalarSites.all (fun (fixture, _) => fixture.name != "Cast" || + let method := fixture.method + let writable := method.parameters == [view true int32Type] + let unsigned := view writable (.atom runtimeScope "System.UInt16") + let flipped := view (!writable) (.atom runtimeScope "System.Int16") + [unsigned, flipped].all fun result => + (runtimeModel {} .call { method with returnType := result }).isNone) = true := by + decide +kernel + +#print axioms cast_result_identity + +/-- The token instantiates one Cast declaration with Int32 and Int16. -/ +private def castToken (span : String) : MetadataSlice × Nat := open Tests.MetadataBuilder in + build (do + let _ ← Tests.MetadataWorld.ownWorld + let owner ← row Table.typeRef + [6, ← string "MemoryMarshal", ← string "System.Runtime.InteropServices"] + let viewType ← row Table.typeRef [6, ← string span, ← string "System"] + let viewOf := fun index => [0x15, 0x11, tokenRow viewType * 4 + 1, 1, 0x1e, index] + let signature ← blob ([0x10, 2, 1] ++ viewOf 1 ++ viewOf 0) + let member ← row Table.memberRef [tokenRow owner * 8 + 1, ← string "Cast", signature] + row Table.methodSpec [tokenRow member * 2 + 1, ← blob [0x0a, 2, 0x08, 0x06]]) + (bytesOf "T") Tests.MetadataWorld.ownIdentity + +/-- The token, external target and open declaration all name the same Cast overload. -/ +private def castBinding (writable : Bool) : Bool := Id.run do + let span := if writable then "Span`1" else "ReadOnlySpan`1" + let (metadata, token) := castToken span + let .ok index := metadata.index | return false + let platform : PlatformTable := platform_table% + [Tests.MetadataBuilder.runtimeType "Object" .objectRoot, + Tests.MetadataBuilder.runtimeType "Span`1" (.genericValue 1), + Tests.MetadataBuilder.runtimeType "ReadOnlySpan`1" (.genericValue 1), + ⟨runtimeScopeBytes, [bytesOf "System.Runtime.InteropServices", bytesOf "MemoryMarshal"], + .classType⟩] + let env := MetadataEnv.ofSlices [index] (platform := platform) + let .ok frame := env.frame 0 0 | return false + let findCast := fun capability => scalarSites.find? fun (fixture, _) => + fixture.name == "Cast" && fixture.parameters == [view capability int32Type] + let some (matching, declared) := findCast writable | return false + let some (opposite, _) := findCast (!writable) | return false + let owner := "System.Runtime.InteropServices.MemoryMarshal" + let target := s!"{runtimeScope}::System.{span} {owner}::Cast(System.{span})" + let .call original := (matching.raw declared).operand | return false + let control := { original with target := .external target } + let crossed := { control with method := opposite.method } + return (checkCallOperand env frame token control).isOk && + runtimeCatalog.siteAgrees {} .scalar ⟨0, 40, .call crossed⟩ (.call crossed.method {}) + none true && + (match checkCallOperand env frame token crossed with + | .error (.operandMismatch found) => found == token + | _ => false) + +/-- The model row accepts either crossed Cast, but BodyDecodes refuses its closed reference + through checkCallOperand for the token of the declared overload. -/ +theorem crossed_cast_overloads : castBinding true ∧ castBinding false := by decide +kernel + +#print axioms crossed_cast_overloads + +/-- The caller contains one marshal call at pc zero. -/ +private def marshalCaller (raw : RawInstruction) : RawMethod := + { token := 1, owner := "Fixture", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [], locals := [], + initLocals := false, maxStack := 0, genericParameters := [], code := [raw] } + +/-- A refused method-token operand decodes to its opcode's hole, which must be dead. -/ +private def refused (raw : RawInstruction) (alternative : Instruction) : Bool := + let required := runtimeCatalog.callRequirement (LayoutEnvironment.callLayouts {}) + .scalar none raw + siteClaim {} none raw == .refused && + decide ((decodeRaw .scalar {} [] [true] (marshalCaller raw) []).map Method.code = + liveHoleAt 0 40) && + required.accepts false (.unadmitted 40) && !required.accepts true (.unadmitted 40) && + !required.accepts true alternative && !required.accepts false alternative + +/-- Floating vectors of every supported register width have no transfer or span model. -/ +theorem floating_vectors_refused : + [128, 256, 512].all (fun bits => ["System.Single", "System.Double"].all fun lane => + let element := vector lane bits + let shape := if bits == 128 then SnapshotShape.vector128 else + if bits == 256 then .vector256 else .vector512 + let cases := fixtures element int32Type (bits / 8) 4 (.snapshot shape) + (cases.zip declarations).all (fun (fixture, sig) => + refused (fixture.raw sig) (.call fixture.method {})) && + ((fixtures int32Type element 4 (bits / 8) (.scalar 4 false)).zip declarations).all + (fun (fixture, sig) => fixture.name != "Cast" || + refused (fixture.raw sig) (.call fixture.method {}))) = true := by decide +kernel + +#print axioms floating_vectors_refused + +/-- Missing layouts and unknown names both require the call's hole. -/ +theorem unresolved_marshal_sites : + ((fixtures packet packet 8 8 (.snapshot (.aggregate 8))).zip declarations).all + (fun (fixture, sig) => refused (fixture.raw sig) (.call fixture.method {})) = true ∧ + scalarSites.all (fun (fixture, sig) => + refused ({ fixture with name := "Unknown" }.raw sig) (.call fixture.method {})) = + true := by decide +kernel + +#print axioms unresolved_marshal_sites + +/-- ManagedCallsValid refuses a linked Write through import admission even when the + slot has the exact signature and the decoder retains the call. -/ +theorem linked_write_import_refused : + let double := TypeExpr.atom runtimeScope "System.Double" + let bytes := view true (.atom runtimeScope "System.Byte") + let owner := "System.Runtime.InteropServices.MemoryMarshal" + let target := s!"{runtimeScope}::System.Void {owner}::Write(System.Span`1,!!0&)" + let method : MethodRef := { + owner := .atom runtimeScope owner, name := name!"Write", hasThis := false, genericArity := 1, + methodArguments := [double], returnType := .void, parameters := [bytes, .byref double] } + let site : CallSite := { + method, name := "Write", target := .external target, typeArguments := [double], + declared := { convention := 0x10, parameters := [bytes, .byref (.parameter 0)] }, + parameterCount := 2, returnsValue := false } + let source := marshalCaller ⟨0, 40, .call site⟩ + let slot : ImportSlot := { + identity := target, closureDigest := "digest", parameterCount := 2, hasThis := false, + returnsValue := false, signature := some ⟨method.owner, site.name, site.typeArguments, + method.returnType, method.parameters⟩ } + (Method.ofRaw? .scalar {} [(0, .direct 0)] [true] source []).any (fun emitted => + decide (emitted.code = [.call method {}] ∧ emitted.links = [(0, .direct 0)] ∧ + runtimeModel {} .call method = none ∧ importSignatureMatches slot site = false ∧ + ¬ ManagedCallsValid [] [slot] (callModelled emitted) source [true] emitted)) = true := by + decide +kernel + +#print axioms linked_write_import_refused + +/-- A live marshal site keeps the genuine call and requires its open declaration. -/ +theorem marshal_site_emissions : + scalarSites.all (fun (fixture, declaration) => + let site := fixture.raw declaration + let agrees := fun raw emitted => + runtimeCatalog.siteAgrees {} .scalar raw emitted none true + let call := Instruction.call fixture.method {} + let wrongPrefix := Instruction.call fixture.method { tail := true } + agrees site call && !agrees site wrongPrefix && !agrees site .nop && + !agrees (fixture.raw (changed declaration)) call && + !agrees (fixture.raw (changed declaration)) wrongPrefix && + [0x00, 0x11, 0x30, 0x50, 0x70].all (fun convention => + !agrees (fixture.raw { declaration with convention }) call)) = true := by decide +kernel + +#print axioms marshal_site_emissions + +/-- An external method whose span argument and reference result fit a unary stack effect. -/ +private def unrelated : RawInstruction := + let method : MethodRef := + { owner := .atom "Fixture" "Methods", name := name!"First" + hasThis := false, genericArity := 0, returnType := .byref int32Type + parameters := [view true int32Type] } + let site : CallSite := + { method, declared := method.signature 0, name := "First" + target := .external "Fixture::System.Int32& Methods::First(System.Span`1)" + typeArguments := [], parameterCount := 1, returnsValue := true } + ⟨0, 40, .call site⟩ + +/-- An unrelated call cannot observe array data, construct these spans or transfer their bytes. -/ +theorem unrelated_marshal_emissions : + scalarSites.all (fun (fixture, _) => + !runtimeCatalog.siteAgrees {} .scalar unrelated (.call fixture.method {}) none true) = + true := by decide +kernel + +#print axioms unrelated_marshal_emissions + +/-- Each resolved overload executes its bound memory model on every execution state. -/ +private theorem marshal_model_steps (fixture : Fixture) + (member : fixture ∈ scalarSites.map Prod.fst) (body : Method) (pc : Nat) + (environment : body.environment = {}) (link : body.linkAt pc = none) + (args stack : List Safety.Value) (frame : Safety.Frame) (memory : Safety.Memory) : + Safety.step body (.call fixture.method {}) pc args frame stack memory = + Safety.modelStep (some (.memory fixture.operation)) body pc frame stack memory := by + have found := List.all_eq_true.mp marshal_resolutions.1 fixture member + apply Safety.step_call_model body pc args frame memory link _ rfl stack + rw [environment] + exact beq_iff_eq.mp found + +#print axioms marshal_model_steps + +end Tests.MarshalModels diff --git a/Tests/MarshaledMemory.cs b/Tests/MarshaledMemory.cs index a6a48c29b..a8b92143a 100644 --- a/Tests/MarshaledMemory.cs +++ b/Tests/MarshaledMemory.cs @@ -2,9 +2,14 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs -#:include RejectedEvidence.cs +#:include MarshalModelEvidence.cs using System.Numerics; using System.Reflection; @@ -60,7 +65,7 @@ string Observed(string run, int identity, int count) => else { claims.Add($"({Run(width - 1)}).map Prod.snd = " + - $".error ⟨0, {(read ? 1 : 2)}, .thrown \"System.ArgumentOutOfRangeException\"⟩"); + $".error ⟨0, {(read ? 1 : 2)}, .thrown .argumentOutOfRange⟩"); proofs.Add("by rfl"); } if (name == "Read") @@ -106,35 +111,16 @@ string Observed(string run, int identity, int count) => queue.Add(new(method.FullName, proof, output), () => Console.WriteLine("PASS checked MemoryMarshal: " + name)); } queue.Flush(); -var rejected = new RejectedEvidence(Path.Combine(root, "RejectedEvidence")); -var index = 0; -foreach (var (name, before, after) in new[] -{ - ("Read", ".marshalRead (.scalar 8 false) false", ".marshalRead (.scalar 4 false) false"), - ("TryRead", ".marshalRead (.scalar 8 false) true", ".marshalRead (.scalar 8 false) false"), - ("TryWrite", ".marshalWrite (.scalar 8 false) true", ".marshalRead (.scalar 8 false) true"), - ("Write", "target := .external externalIdentity0", "target := .external \"forged\""), - ("ReadVector", ".snapshot .vector128", ".snapshot .vector256") -}) -{ - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_unsafe_validated"); - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing marshal mutation: " + before); - var path = Path.Combine(root, $"Rejected{index++}.lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS exact MemoryMarshal evidence rejection: " + name, "UnsafeCallsValid"); -} -foreach (var name in new[] { "ReadAggregate", "WriteAggregate" }) -{ - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_layouts_validated"); - const string before = ".snapshot (.aggregate 32)"; - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing aggregate transfer"); - var path = Path.Combine(root, $"Rejected{index++}.lean"); - File.WriteAllText(path, source.Replace(before, ".snapshot (.aggregate 16)", StringComparison.Ordinal)); - rejected.Add(path, "PASS exact aggregate transfer width: " + name, "MethodLayoutsValid"); -} -rejected.Check(); +CheckMarshalModels(root, +[ + ("Read", 1, ".marshalRead (.scalar 8 false) false", ".marshalRead (.scalar 2 false) false"), + ("TryRead", 2, ".marshalRead (.scalar 8 false) true", ".marshalRead (.scalar 2 false) true"), + ("TryWrite", 2, ".marshalWrite (.scalar 8 false) true", ".marshalWrite (.scalar 2 false) true"), + ("Write", 2, ".marshalWrite (.scalar 8 false) false", ".marshalWrite (.scalar 2 false) false"), + ("ReadVector", 1, ".marshalRead (.snapshot .vector128) false", ".marshalRead (.scalar 2 false) false"), + ("ReadAggregate", 1, ".marshalRead (.snapshot (.aggregate 32)) false", ".marshalRead (.scalar 2 false) false"), + ("WriteAggregate", 2, ".marshalWrite (.snapshot (.aggregate 32)) false", ".marshalWrite (.scalar 2 false) false") +]); partial class Program { diff --git a/Tests/MemorySpanContracts.lean b/Tests/MemorySpanContracts.lean index 7f2983604..c013ebe8f 100644 --- a/Tests/MemorySpanContracts.lean +++ b/Tests/MemorySpanContracts.lean @@ -53,17 +53,17 @@ theorem read_holds : readContract.Holds Extracted.program 1 := by Safety.Value.hasUnspecified, CIL.Value.hasUnspecified] have entered : enterFrame Extracted.method1 [.bufferSpan input.view, .scalar (.i32 input.index)] memory = - .ok (⟨memory.nextIdentity, [.span 4 false (some input.view)], [], [], 0⟩, memory) := by + .ok (⟨memory.nextIdentity, [.span 4 false (some input.view)], [], 0⟩, memory) := by have checked := valid dsimp only [ReadInput.view] at checked - simp [enterFrame, Extracted.method1, makeLocals, makeArgumentHomes, makeParameterHomes, + simp [enterFrame, Extracted.method1, makeLocals, makeParameterHomes, makeLocal, storeLocal, Safety.Value.hasUnspecified, ReadInput.view, checked, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] refine MethodContract.Holds.intro (c := readContract) (input := input) memory input.expected ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ cil_supply_fuel dsimp only [readContract] - simp only [Safety.invoke, Extracted.lookup1] + simp only [Safety.invoke_eq, Extracted.program_method1] simp [checkedValue, numericValue, valid, entered, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, List.mapM_cons, List.mapM_nil] cil_checked_steps [readContract, ReadInput.view, makeParameterHomes, makeLocal, storeLocal, @@ -93,9 +93,9 @@ theorem slice_read_composes (program : Program) let args : List Safety.Value := [.bufferSpan source, .scalar (.i32 start), .scalar (.i32 length), .scalar (.i32 input.index)] let frame : Frame := ⟨memory.nextIdentity, - [.span 4 false (some ⟨.null, 0, 4, false⟩), .span 4 false (some source)], [], [], 0⟩ + [.span 4 false (some ⟨.null, 0, 4, false⟩), .span 4 false (some source)], [], 0⟩ have entered : enterFrame Extracted.method0 args memory = .ok (frame, memory) := by - simp [enterFrame, Extracted.method0, args, frame, makeLocals, makeArgumentHomes, + simp [enterFrame, Extracted.method0, args, frame, makeLocals, makeParameterHomes, makeLocal, storeLocal, sourceWidth, sourceReadonly, sourceValid, Safety.Value.hasUnspecified, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] diff --git a/Tests/MemorySpanExecution.lean b/Tests/MemorySpanExecution.lean index 3a655f5ca..ed6534c75 100644 --- a/Tests/MemorySpanExecution.lean +++ b/Tests/MemorySpanExecution.lean @@ -1,4 +1,6 @@ import CIL.Safety.Execution +import Tests.Support +import Tests.Support.SpanMethods /-! # Span values across interpreter calls, locals and frame lifetimes @@ -19,12 +21,12 @@ private def memory : Safety.Memory := { private def view : SpanView := ⟨.address ⟨0, 0⟩, 3, 4, false⟩ private def reader : Method := { - code := [.arg 0, .arg 1, .memory (.spanItem 4), .loadScalar 4, .ret] + code := [.ldarg 0, .ldarg 1, .call (SpanMethods.item SpanMethods.int32), .ldind .u4, .ret] locals := [], returnsValue := true } private def caller : Method := { - code := [.arg 0, .setLocal 0, .local 0, .arg 1, .arg 2, .memory (.spanSlice true), - .arg 3, .call 1 2, .ret] + code := [.ldarg 0, .stloc 0, .ldloc 0, .ldarg 1, .ldarg 2, .call (SpanMethods.slice SpanMethods.int32 true), + .ldarg 3, .call (Tests.callee 2) {}, .ret], links := [(7, .direct 1)] locals := [.nullRef], localKinds := [.span 4 false], returnsValue := true } theorem local_slice_and_call : @@ -34,7 +36,7 @@ theorem local_slice_and_call : #print axioms local_slice_and_call private def emptyLength : Method := { - code := [.arg 0, .const32 3, .const32 0, .memory (.spanSlice true), .memory .spanLength, .ret] + code := [.ldarg 0, .ldc_i4 3, .ldc_i4 0, .call (SpanMethods.slice SpanMethods.int32 true), .call (SpanMethods.length SpanMethods.int32), .ret] locals := [], returnsValue := true } theorem end_empty_slice : @@ -46,13 +48,13 @@ theorem end_empty_slice : theorem invalid_slice_ranges : memoryInstruction (.spanSlice true) [.scalar (.i32 0), .scalar (.i32 4), .bufferSpan view] memory = - .error (.thrown "System.ArgumentOutOfRangeException") ∧ + .error (.thrown .argumentOutOfRange) ∧ memoryInstruction (.spanSlice true) [.scalar (.i32 2), .scalar (.i32 2), .bufferSpan view] memory = - .error (.thrown "System.ArgumentOutOfRangeException") ∧ + .error (.thrown .argumentOutOfRange) ∧ memoryInstruction (.spanSlice true) [.scalar (.i32 0), .scalar (.i32 4294967295), .bufferSpan view] memory = - .error (.thrown "System.ArgumentOutOfRangeException") := by + .error (.thrown .argumentOutOfRange) := by exact ⟨rfl, rfl, rfl⟩ #print axioms invalid_slice_ranges @@ -65,11 +67,11 @@ theorem null_empty_slice : theorem invalid_index : (invoke [reader] 16 0 [.bufferSpan view, .scalar (.i32 3)] memory).map Prod.snd = - .error ⟨0, 2, .thrown "System.IndexOutOfRangeException"⟩ := rfl + .error ⟨0, 2, .thrown .indexOutOfRange⟩ := rfl #print axioms invalid_index private def illegalWriter : Method := { - code := [.arg 0, .const32 0, .memory (.spanItem 4), .const32 1, .storeScalar 4, .ret] + code := [.ldarg 0, .ldc_i4 0, .call (SpanMethods.item SpanMethods.int32), .ldc_i4 1, .stind .i4, .ret] locals := [], returnsValue := false } theorem readonly_reference_rejects_store : @@ -78,7 +80,7 @@ theorem readonly_reference_rejects_store : #print axioms readonly_reference_rejects_store private def escaping : Method := { - code := [.localAddr 0, .const32 1, .memory (.spanFromReference 4 true), .ret] + code := [.ldloca 0, .ldc_i4 1, .call SpanMethods.createInt32, .ret] locals := [.i32 7], localKinds := [.word32], returnsValue := true } theorem expired_local_cannot_escape : diff --git a/Tests/MemorySpanWriteContracts.lean b/Tests/MemorySpanWriteContracts.lean index 5add3192c..441da7ba6 100644 --- a/Tests/MemorySpanWriteContracts.lean +++ b/Tests/MemorySpanWriteContracts.lean @@ -52,17 +52,17 @@ theorem write_holds : writeContract.Holds Extracted.program 2 := by Pure.pure, Except.pure, unspecifiedOperands, MemoryOp.argumentCount, Safety.Value.hasUnspecified, CIL.Value.hasUnspecified] have entered : enterFrame Extracted.method2 (writeContract.args input) memory = - .ok (⟨memory.nextIdentity, [.span 4 true (some input.view)], [], [], 0⟩, memory) := by + .ok (⟨memory.nextIdentity, [.span 4 true (some input.view)], [], 0⟩, memory) := by have checked := valid dsimp only [WriteInput.view] at checked - simp [enterFrame, Extracted.method2, writeContract, makeLocals, makeArgumentHomes, + simp [enterFrame, Extracted.method2, writeContract, makeLocals, makeParameterHomes, makeLocal, storeLocal, Safety.Value.hasUnspecified, WriteInput.view, checked, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] refine MethodContract.Holds.intro final () ?_ (Confined.write written (by simp [writeContract])) updated cil_supply_fuel dsimp only [writeContract] - simp only [Safety.invoke, Extracted.lookup2] + simp only [Safety.invoke_eq, Extracted.program_method2] simp [writeContract, checkedValue, numericValue, valid, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] at entered ⊢ rw [entered] @@ -115,17 +115,17 @@ theorem read_holds : readContract.Holds Extracted.program 1 := by Safety.Value.hasUnspecified, CIL.Value.hasUnspecified] have entered : enterFrame Extracted.method1 [.bufferSpan input.view, .scalar (.i32 input.index)] memory = - .ok (⟨memory.nextIdentity, [.span 4 false (some input.view)], [], [], 0⟩, memory) := by + .ok (⟨memory.nextIdentity, [.span 4 false (some input.view)], [], 0⟩, memory) := by have checked := valid dsimp only [ReadInput.view] at checked - simp [enterFrame, Extracted.method1, makeLocals, makeArgumentHomes, makeParameterHomes, + simp [enterFrame, Extracted.method1, makeLocals, makeParameterHomes, makeLocal, storeLocal, Safety.Value.hasUnspecified, ReadInput.view, checked, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] refine MethodContract.Holds.intro (c := readContract) (input := input) memory input.expected ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ cil_supply_fuel dsimp only [readContract] - simp only [Safety.invoke, Extracted.lookup1] + simp only [Safety.invoke_eq, Extracted.program_method1] simp [checkedValue, numericValue, valid, entered, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, List.mapM_cons, List.mapM_nil] cil_checked_steps [readContract, ReadInput.view, makeParameterHomes, makeLocal, storeLocal, @@ -156,9 +156,9 @@ theorem slice_write_composes (program : Program) have bound := permitted.2.2.1 let args : List Safety.Value := [.bufferSpan source, .scalar (.i32 start), .scalar (.i32 length), .scalar (.i32 input.index), .scalar (.i32 input.replacement)] - let frame : Frame := ⟨memory.nextIdentity, [.span 4 true (some source)], [], [], 0⟩ + let frame : Frame := ⟨memory.nextIdentity, [.span 4 true (some source)], [], 0⟩ have entered : enterFrame Extracted.method0 args memory = .ok (frame, memory) := by - simp [enterFrame, Extracted.method0, args, frame, makeLocals, makeArgumentHomes, + simp [enterFrame, Extracted.method0, args, frame, makeLocals, makeParameterHomes, makeLocal, storeLocal, sourceWidth, sourceWritable, sourceValid, Safety.Value.hasUnspecified, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure] diff --git a/Tests/MemorySpans.cs b/Tests/MemorySpans.cs index 8e2e0416c..86463f183 100644 --- a/Tests/MemorySpans.cs +++ b/Tests/MemorySpans.cs @@ -2,8 +2,14 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs +#:include BulkCallEvidence.cs #:include RejectedEvidence.cs using Mono.Cecil; @@ -61,13 +67,17 @@ if (mutation == "alias") processor.InsertAfter(head, processor.Create(Mono.Cecil.Cil.OpCodes.Dup)); else - processor.InsertBefore(head, processor.Create(Mono.Cecil.Cil.OpCodes.Br, head.Next)); + { + // The branch carries the same home the receiver load pushes, so only the window's target check refuses it. + var address = processor.Create(head.OpCode, (Mono.Cecil.Cil.VariableDefinition)head.Operand); + processor.InsertBefore(head, address); + processor.InsertAfter(address, processor.Create(Mono.Cecil.Cil.OpCodes.Br, head.Next)); + } module.Write(dll); } var result = Execute(["dotnet", ExtractorPath, "extract", dll, "MemorySpans::InPlaceArrayConstructor(System.Int32[],System.Int32,System.Int32)", output]); - var marker = mutation == "alias" ? "Unsupported in-place span constructor" : "folded span"; - if (result.Code == 0 || !result.Output.Contains(marker, StringComparison.OrdinalIgnoreCase)) + if (result.Code == 0 || !result.Output.Contains("Unsupported in-place span constructor", StringComparison.Ordinal)) throw new InvalidOperationException("Incorrect constructor rejection: " + mutation + "\n" + result.Output); Console.WriteLine("PASS in-place constructor rejection: " + mutation); } @@ -114,7 +124,7 @@ theorem compiled_span_result : (CIL.Safety.invoke Extracted.program 64 0 {{arguments}} spanMemory).map Prod.snd = - .ok [.scalar (.i32 {{expected}})] := rfl + .ok [.scalar (.i32 {{expected}})] := by rfl #print axioms compiled_span_result """); spanProofs.Add(name, proof); @@ -124,36 +134,28 @@ .ok [.scalar (.i32 {{expected}})] := rfl theorem constructor_fault_keeps_call_pc : (CIL.Safety.invoke Extracted.program 64 0 [.object .null, .scalar (.i32 1), .scalar (.i32 0)] spanMemory).map Prod.snd = - .error ⟨0, 4, .thrown "System.ArgumentOutOfRangeException"⟩ := rfl + .error ⟨0, 4, .thrown .argumentOutOfRange⟩ := rfl #print axioms constructor_fault_keeps_call_pc theorem uninitialized_span_local_rejected : - CIL.Safety.step Extracted.method0 (.local 0) 0 [] - ⟨0, [.span 4 false none], [], [], 0⟩ [] spanMemory = .error .invalidState := rfl + CIL.Safety.step Extracted.method0 (.ldloc 0) 0 [] + ⟨0, [.span 4 false none], [], 0⟩ [] spanMemory = .error .invalidState := rfl #print axioms uninitialized_span_local_rejected theorem constructor_expired_storage_rejected : - CIL.Safety.step Extracted.method0 - (.initializeSpanLocal 0 (.array .signed32 false .counted)) 4 [] - ⟨0, [.span 4 false none], [], [], 0⟩ - [.scalar (.i32 0), .scalar (.i32 0), .array ⟨.address ⟨0, 0⟩, 3, .signed32⟩] - { spanMemory with - allocations := fun _ => some ⟨.callerStack, ⟨12, 1, []⟩, false, []⟩ } = - .error (.memory .expiredLifetime (.address ⟨0, 0⟩) 0) := rfl + (Extracted.method0.code[4]?).map (fun constructor => + CIL.Safety.step Extracted.method0 constructor 4 [] + ⟨0, [.span 4 false none], [], 0⟩ + [.scalar (.i32 0), .scalar (.i32 0), .array ⟨.address ⟨0, 0⟩, 3, .signed32⟩, + .spanHome 0] + { spanMemory with + allocations := fun _ => some ⟨.callerStack, ⟨12, 1, []⟩, false, []⟩ }) = + some (.error (.memory .expiredLifetime (.address ⟨0, 0⟩) 0)) := rfl #print axioms constructor_expired_storage_rejected """); - if (name == "InPlaceArrayConstructor") - { - const string operation = ".initializeSpanLocal 0 (.array .signed32 true .counted)"; - if (!source.Contains(operation, StringComparison.Ordinal)) - throw new InvalidOperationException("Missing compiled in-place span constructor"); - var forged = Path.Combine(root, "Forged-constructor-local.lean"); - File.WriteAllText(forged, source.Replace(operation, - ".initializeSpanLocal 1 (.array .signed32 true .counted)", StringComparison.Ordinal)); - rejected.Add(forged, "PASS independent constructor destination rejection", - "Extraction.FeatureFlowValid rawLayouts.toArray (Extraction.evidenceConstants methodEvidence) " + - "method0Closed"); - } + // The constructor's home and model come from the raw call, so the extraction emits no form to forge. + if (name == "InPlaceArrayConstructor" && !source.Contains("theorem method0_decoded :", StringComparison.Ordinal)) + throw new InvalidOperationException("In-place span constructor is not decoded from its raw call"); } foreach (var (name, signature, width, arguments, bytes) in new[] @@ -172,20 +174,11 @@ CIL.Safety.step Extracted.method0 Run(["dotnet", ExtractorPath, "extract", assembly, $"MemorySpans::{name}({signature})", output]); var proof = Path.Combine(root, name + ".lean"); var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); - if (name == "FillIntegers") - { - var forged = Path.Combine(root, "Forged-fill-width.lean"); - if (!source.Contains(".spanFill 4 false", StringComparison.Ordinal)) - throw new Exception("Missing fill-width mutation"); - File.WriteAllText(forged, source.Replace(".spanFill 4 false", ".spanFill 8 false", - StringComparison.Ordinal)); - rejected.Add(forged, "PASS independent span fill width rejection", - "Extraction.ScalarFlowValid rawLayouts.toArray method0Closed " + - "method0FeatureStates method0ScalarStates method0.code"); - } var start = name == "FillLongSlice" ? width : 0; var count = name == "FillLongSlice" ? 4 : 2; - File.WriteAllText(proof, source + $$""" + File.WriteAllText(proof, $$""" + import CIL.Safety.StepComposition + {{source}} private def fillMemory : CIL.Safety.Memory := { allocations := fun identity => if identity == 0 then @@ -201,9 +194,12 @@ CIL.Safety.step Extracted.method0 let contents := CIL.Safety.read memory ⟨0, {{width + start}}⟩ {{2 * width}} 1 pure (contents, memory.cells 0 {{width + start - 1}}, memory.cells 0 {{width + start + 2 * width}}, result)) = - .ok (.ok ({{bytes}}), ⟨99, false⟩, ⟨99, false⟩, []) := rfl + .ok (.ok ({{bytes}}), ⟨99, false⟩, ⟨99, false⟩, []) := by rfl #print axioms compiled_fill_contents """); + var pc = name switch { "ClearBytes" => 1, "FillLongSlice" => 7, _ => 2 }; + var clear = name == "ClearBytes" ? "true" : "false"; + File.AppendAllText(proof, BulkCallEvidence(0, pc, $".spanFill {width} {clear}")); spanProofs.Add(name, proof); } @@ -237,7 +233,7 @@ CIL.Safety.step Extracted.method0 theorem compiled_primitive_span_result : (CIL.Safety.invoke Extracted.program 64 0 {{arguments}} primitiveMemory).map Prod.snd = - .ok [.scalar ({{expected}})] := rfl + .ok [.scalar ({{expected}})] := by rfl #print axioms compiled_primitive_span_result """); if (name == "BooleanWrite") @@ -313,13 +309,13 @@ .ok [.scalar (.i32 2)] := rfl (CIL.Safety.invoke Extracted.program 64 0 [.array ⟨.address ⟨0, 0⟩, 3, .{{element}}⟩, .scalar (.i32 (-1)){{tail}}] arrayMemory).map Prod.snd = - .error ⟨0, {{range + 1}}, .thrown "System.ArgumentOutOfRangeException"⟩ := rfl + .error ⟨0, {{range + 1}}, .thrown .argumentOutOfRange⟩ := rfl #print axioms compiled_negative_start theorem compiled_null_invalid_start : (CIL.Safety.invoke Extracted.program 64 0 [.object .null, .scalar (.i32 1){{tail}}] arrayMemory).map Prod.snd = - .error ⟨0, {{range + 1}}, .thrown "System.ArgumentOutOfRangeException"⟩ := rfl + .error ⟨0, {{range + 1}}, .thrown .argumentOutOfRange⟩ := rfl #print axioms compiled_null_invalid_start """); } @@ -331,14 +327,14 @@ .ok [.scalar (.i32 2)] := rfl (CIL.Safety.invoke Extracted.program 64 0 [.array ⟨.address ⟨0, 0⟩, 3, .{{element}}⟩, .scalar (.i32 2147483647), .scalar (.i32 2147483647)] arrayMemory).map Prod.snd = - .error ⟨0, {{range + 1}}, .thrown "System.ArgumentOutOfRangeException"⟩ := rfl + .error ⟨0, {{range + 1}}, .thrown .argumentOutOfRange⟩ := rfl #print axioms compiled_overflowing_range theorem compiled_negative_length : (CIL.Safety.invoke Extracted.program 64 0 [.array ⟨.address ⟨0, 0⟩, 3, .{{element}}⟩, .scalar (.i32 0), .scalar (.i32 (-1))] arrayMemory).map Prod.snd = - .error ⟨0, {{range + 1}}, .thrown "System.ArgumentOutOfRangeException"⟩ := rfl + .error ⟨0, {{range + 1}}, .thrown .argumentOutOfRange⟩ := rfl #print axioms compiled_negative_length theorem expired_array_is_not_a_range_exception : @@ -351,23 +347,34 @@ .ok [.scalar (.i32 2)] := rfl #print axioms expired_array_is_not_a_range_exception """); } - spanProofs.Add(name, proof); - if (name == "ArrayReadOnly") + if (name is "ArrayReadOnly" or "ArrayConstructor" or "ArrayRange") { - foreach (var (mutation, before, after) in new[] + // The generated model fact names the element, capability and bounds, and each forgery changes one of them. + var bounds = range == 2 ? ".counted" : ".whole"; + var otherCapability = writable ? "false" : "true"; + var site = range + 1; + var model = $".memory (.arraySpan .{element} {capability} {bounds})"; + File.AppendAllText(proof, StatedModelFact(site, model)); + foreach (var (mutation, operation) in new[] { - ("array-element", ".arraySpan .signed32 false .whole", ".arraySpan .unsigned32 false .whole"), - ("array-writability", ".arraySpan .signed32 false .whole", ".arraySpan .signed32 true .whole") + ("array-element", $".memory (.arraySpan .unsigned32 {capability} {bounds})"), + ("array-writability", $".memory (.arraySpan .{element} {otherCapability} {bounds})") }) { - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing " + mutation); - var forged = Path.Combine(root, "Forged-" + mutation + ".lean"); - File.WriteAllText(forged, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(forged, "PASS independent span call rejection: " + mutation, - "Extraction.ScalarFlowValid rawLayouts.toArray method0Closed " + - "method0FeatureStates method0ScalarStates method0.code"); + var forged = Path.Combine(root, $"Forged-{name}-{mutation}.lean"); + File.WriteAllText(forged, ForgeModelFact(source, site, operation)); + rejected.Add(forged, $"PASS forged span model fact refused: {name} {mutation}", ModelFactMismatch(site)); + } + if (name == "ArrayConstructor") + { + // The constructor operand must retain its original opcode. + var substituted = Path.Combine(root, $"Forged-{name}-substituted.lean"); + File.WriteAllText(substituted, ForgeConstructorOpcode(source, site)); + rejected.Add(substituted, $"PASS substituted constructor opcode refused: {name}", "RuntimeModelsAgree", + "is false"); } } + spanProofs.Add(name, proof); } if (argumentStart < 0) throw new Exception("Missing span argument evidence"); @@ -385,8 +392,7 @@ .ok [.scalar (.i32 2)] := rfl // Keep both raw methods intact: rejection must bind argument evidence to the closed signature. File.WriteAllText(proof, original[..argumentStart] + argumentDefinition.Replace(before, after, StringComparison.Ordinal) + original[argumentEnd..]); - var rejectedGoal = "⊢ Extraction.closedArguments rawLayouts method0Closed = some method0RawArguments"; - rejected.Add(proof, "PASS independent span evidence rejection: " + name, rejectedGoal); + rejected.Add(proof, "PASS independent span evidence rejection: " + name, ClosedArgumentsMismatch); } rejected.Check(); diff --git a/Tests/MetadataAssemblies.lean b/Tests/MetadataAssemblies.lean new file mode 100644 index 000000000..5caa8907b --- /dev/null +++ b/Tests/MetadataAssemblies.lean @@ -0,0 +1,186 @@ +import Tests.MetadataWorld +import Lean + +/-! +# Assembly lookup regressions + +Assembly lookups preserve positional links, the first identity match and arbitrary pinned +prefixes. Scaling varies assembly count independently of method instructions and metadata rows. +-/ + +namespace Tests.MetadataAssemblies + +open CIL CIL.Extraction Tests.MetadataBuilder Tests.MetadataWorld + +set_option maxRecDepth 32768 + +/-- Resolve a caller's TypeRef through the facade's physical ExportedType and AssemblyRef rows. -/ +private def forwarded (flags := 0x00200000) (typeDefId := 0) (implementation := 5) + (forwardName := "InvalidOperationException") (targetName := "Core") + (declaredName := "InvalidOperationException") (visibility := 1) + (forwarderSlice := 1) (carry := true) : Option (List Nat) := do + let identity := fun (name : String) => name ++ ", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + let make := fun (name : String) (entries : Step Nat) => build entries (bytesOf name) (identity name) + let (library, definition) := make "Core" do + let name ← string declaredName + let ns ← string "System" + row Table.typeDef [visibility, name, ns, 0, 1, 1] + let (facade, exported) := make "Facade" do + let dest ← string targetName + let _ ← row Table.assemblyRef [1, 0, 0, 0, 0, 0, dest, 0, 0] + let name ← string forwardName + let ns ← string "System" + row Table.exportedType [flags, typeDefId, name, ns, implementation] + let (caller, reference) := make "Caller" do + let scope ← string "Facade" + let _ ← row Table.assemblyRef [1, 0, 0, 0, 0, 0, scope, 0, 0] + let name ← string "InvalidOperationException" + let ns ← string "System" + let reference ← row Table.typeRef [6, name, ns] + link linkDefinition reference 2 definition + if carry then link linkForwarder reference forwarderSlice exported + return reference + let slices ← [caller, facade, library].mapM fun slice => slice.index.toOption + let .inl named ← (typeRefTarget (.ofSlices slices) 0 resolveFuel reference).toOption | none + return named.scope + +/-- A top-level forwarder selects the public definition in its named implementation. + A changed flag, name, implementation tag, assembly, visibility or witness fails. -/ +theorem checked_forwarder : + forwarded = some (bytesOf "Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") ∧ + [forwarded (flags := 0), forwarded (typeDefId := 1), forwarded (implementation := 0), + forwarded (implementation := 1), forwarded (implementation := 4), + forwarded (implementation := 6), forwarded (implementation := 7), + forwarded (forwardName := "Exception"), forwarded (declaredName := "Exception"), + forwarded (targetName := "Other"), forwarded (visibility := 0), + forwarded (forwarderSlice := 0), forwarded (carry := false)].all Option.isNone := by + decide +kernel +#print axioms checked_forwarder + +/-- Prefix matching agrees with the prefix predicate, including overlapping and empty prefixes. -/ +theorem prefixes : + let stored := [[1], [1, 2, 3], [4, 5], [4, 6]] + let trie := stored.foldl AssemblyPrefixes.insert {} + [[1], [1, 2, 4], [4], [4, 5, 6], [4, 6], [4, 7], []].all + (fun scope => trie.matches scope == stored.any (·.isPrefixOf scope)) = true ∧ + (({} : AssemblyPrefixes).insert []).matches [9] = true := by decide +kernel +#print axioms prefixes + +/-- Full version markers, duplicate stems and large byte keys retain exact prefix matching. -/ +theorem version_prefixes : + let stems := [[65] ++ versionMarker, [65, 44] ++ versionMarker, [512, 1], [512, 1]] + let trie := stems.foldl AssemblyPrefixes.insert {} + [[65] ++ versionMarker ++ [49], [65, 44] ++ versionMarker ++ [50], + [65, 44, 32, 86], [66] ++ versionMarker, [512, 1, 2], [512], []].all + (fun scope => trie.matches scope == stems.any (·.isPrefixOf scope)) = true := by + decide +kernel +#print axioms version_prefixes + +/-- The first complete version marker terminates a stem, even after a partial marker. -/ +theorem version_stems : + assemblyStem ([65, 44] ++ versionMarker ++ [49]) = [65, 44] ++ versionMarker ∧ + assemblyStem [65, 44, 66] = [65, 44, 66] := by decide +kernel +#print axioms version_stems + +/-- `System.Object`, the base of `Lib.Box`, and `Lib.Box` as the platform class it is when no + slice carries `Lib`. -/ +private def libraryPlatform : PlatformTable := platform_table% + [runtimeType "Object" .objectRoot, + { scope := bytesOf libScope, parts := [bytesOf "Lib", bytesOf "Box"], kind := .classType }] + +/-- Resolve the library type with the supplied assembly order and pins. -/ +private def resolveLibrary (others : List MetadataSlice) (pins : List (List Nat) := []) := do + let env := MetadataEnv.ofSlices (← (own.1 :: others).mapM MetadataSlice.index) + (platform := libraryPlatform) + (pins.foldl AssemblyPrefixes.insert {}) + typeRefNamed env 0 resolveFuel tokens.boxRef none + +/-- Compare the exact error while retaining the successful type separately. -/ +private def failedWith (result : Except MetadataError NamedType) (error : MetadataError) : Bool := + match result with + | .error found => found == error + | .ok _ => false + +/-- Duplicate identities retain their first position; a link to a different position is refused. -/ +theorem first_identity : + (resolveLibrary [lib, lib]).isOk = true ∧ + failedWith (resolveLibrary [own.1, lib]) (.unresolvedType tokens.boxRef) = true := by + decide +kernel +#print axioms first_identity + +/-- Missing carried assemblies are external only when no pinned prefix covers their identity. -/ +theorem pinned_missing : + (resolveLibrary []).isOk = true ∧ + failedWith (resolveLibrary [] [assemblyStem (bytesOf libScope)]) + (.missingSlice tokens.boxRef) = true ∧ + failedWith (resolveLibrary [] [[]]) (.missingSlice tokens.boxRef) = true := by decide +kernel +#print axioms pinned_missing + +/-- A carried version cannot satisfy a reference to another version of the same pinned name. -/ +theorem wrong_version : + let versioned := (build libWorld (bytesOf "Lib") + "Lib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" 2).1 + failedWith (resolveLibrary [versioned] [assemblyStem (bytesOf libScope)]) + (.missingSlice tokens.boxRef) = true := by decide +kernel +#print axioms wrong_version + +/-- Fixed-width identities keep the benchmark's byte count proportional to assembly count. -/ +private def identity (position : Nat) : List Nat := + [65, position / 256, position % 256, 44] + +/-- Construct and query each assembly once, including the pin lookup for a missing version. -/ +private def exercise (base : MetadataIndex) (count : Nat) : Bool := Id.run do + let indexes := (List.range count).map fun position => { base with identity := identity position } + let env := MetadataEnv.ofSlices indexes + ((indexes.map (·.identity)).foldl AssemblyPrefixes.insert {}) + for position in [:count] do + let scope := identity position + if env.identities[scope]? != some position then return false + if (env.slice position 0).toOption.map (·.identity) != some scope then return false + if !env.pinned.matches (scope ++ [99]) then return false + return true + +/-- Kernel evaluation includes construction as well as every assembly query. -/ +private def checkedExercise (count : Nat) : Bool := + match lib.index with + | .ok base => exercise base count + | .error _ => false + +open Lean Meta Elab Command in +set_option maxHeartbeats 0 in +run_cmd do + let mut previous := 0 + for count in [128, 512, 2048] do + let (statement, proof) ← liftTermElabM do + let statement ← mkEq (mkApp (mkConst ``checkedExercise) (mkNatLit count)) + (mkConst ``Bool.true) + pure (statement, ← mkEqRefl (mkConst ``Bool.true)) + let start ← IO.getNumHeartbeats + match Kernel.check (← getEnv) {} (Expr.letE `checked statement proof (.bvar 0) false) with + | .error error => throwKernelException error + | .ok _ => pure () + let cost := (← IO.getNumHeartbeats) - start + logInfo m!"assembly kernel checks: {count} assemblies, {cost} heartbeats" + if previous != 0 && cost > previous * 6 then + throwError "assembly kernel cost grew too quickly" + previous := cost + +/-- Minimum elapsed time includes index construction and all lookups. -/ +private def measure (base : MetadataIndex) (count : Nat) : IO Nat := do + let mut times := #[] + for _ in [:5] do + let start ← IO.monoNanosNow + unless exercise base count do throw (IO.userError "assembly lookup lost an identity") + times := times.push ((← IO.monoNanosNow) - start) + return (times.qsort (fun left right => left < right))[0]! + +#eval do + let .ok base := lib.index | throw (IO.userError "invalid assembly fixture") + let _ ← measure base 128 + let small ← measure base 1024 + let large ← measure base 8192 + IO.println s!"assembly lookup minimum ns: {small}, {large}" + unless large < small * 24 + 30000000 do + throw (IO.userError "assembly lookup cost grew faster than indexed construction and queries") + +end Tests.MetadataAssemblies diff --git a/Tests/MetadataAssemblyIndexes.cs b/Tests/MetadataAssemblyIndexes.cs new file mode 100644 index 000000000..0c7d5d681 --- /dev/null +++ b/Tests/MetadataAssemblyIndexes.cs @@ -0,0 +1,522 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:package Mono.Cecil +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs + +using System.Reflection; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Security.Cryptography; +using Mono.Cecil; +using Srm = System.Reflection.Metadata; + +// Direct calls isolate assembly lookup from extraction admission and proof compilation. +var extractor = Assembly.LoadFrom(ExtractorPath); +const BindingFlags Internal = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; +var pinnedType = extractor.GetType("Nethermind.LeanCIL.PinnedAssemblies", throwOnError: true)!; +var sourceType = pinnedType.GetNestedType("Source", BindingFlags.Public | BindingFlags.NonPublic)!; +var collectionType = extractor.GetType("Nethermind.LeanCIL.MetadataSlices+Collection", throwOnError: true)!; +var directory = At("artifacts/MetadataAssemblyIndexes"); +Directory.CreateDirectory(directory); + +void CultureCollision() +{ + using var selectedAssembly = AssemblyDefinition.CreateAssembly(new("CultureSource", new(1, 0)), + "CultureSource", ModuleKind.Dll); + selectedAssembly.MainModule.AssemblyReferences.Add(new("CultureTarget", new(1, 0)) { Culture = "neutral" }); + using var target = AssemblyDefinition.CreateAssembly(new("CultureTarget", new(1, 0)), + "CultureTarget", ModuleKind.Dll); + var targetPath = Path.Combine(directory, "CultureTarget.dll"); + target.Write(targetPath); + using var stream = new MemoryStream(); + selectedAssembly.Write(stream); + var pins = Array.CreateInstance(sourceType, 1); + pins.SetValue(Activator.CreateInstance(sourceType, Internal, null, [targetPath, null], null), 0); + using var pinned = (IDisposable)Activator.CreateInstance(pinnedType, Internal, null, + [stream.ToArray(), "test", pins, null], null)!; + var module = (ModuleDefinition)pinnedType.GetProperty("Selected", Internal)!.GetValue(pinned)!; + var reference = module.AssemblyReferences.Single(); + ExpectOutside(() => ((IAssemblyResolver)pinned).Resolve(reference)); + var reject = pinnedType.GetMethod("RejectMismatch", Internal)! + .CreateDelegate>(pinned); + ExpectOutside(() => reject(reference)); + var moduleOf = pinnedType.GetMethod("ModuleOf", Internal)! + .CreateDelegate>(pinned); + Require(moduleOf(new("Fixture", "Type", module, reference)) is null, "Mismatched culture acquired a type owner."); + Console.WriteLine("PASS raw literal-neutral culture differs from absent culture"); +} + +CultureCollision(); +if (args.Contains("--culture", StringComparer.Ordinal)) return; + +byte[] Image(string name, int version = 1, int references = 0) +{ + using var assembly = AssemblyDefinition.CreateAssembly(new(name, new(version, 0)), name, ModuleKind.Dll); + if (references > 0) + { + assembly.MainModule.AssemblyReferences.Add(new("Assembly0", new(2, 0))); + assembly.MainModule.AssemblyReferences.Add(new("Assembly0", new(3, 0))); + assembly.MainModule.AssemblyReferences.Add(new("Unpinned", new(1, 0))); + for (int index = 0; index < references; index++) + assembly.MainModule.AssemblyReferences.Add(new("Assembly" + index, new(1, 0))); + } + using var stream = new MemoryStream(); + assembly.Write(stream); + return stream.ToArray(); +} + +IdentityCases(); +if (args.Contains("--identities", StringComparer.Ordinal)) return; +ReferenceScaling(); +if (args.Contains("--reference-scaling", StringComparer.Ordinal)) return; +var sources = new List(); +for (var index = 0; index < 4096; index++) +{ + var path = Path.Combine(directory, "Assembly" + index + ".dll"); + File.WriteAllBytes(path, Image("Assembly" + index)); + sources.Add(Activator.CreateInstance(sourceType, Internal, null, [path, null], null)!); +} + +double Measure(int count) +{ + var array = Array.CreateInstance(sourceType, count); + for (var index = 0; index < count; index++) array.SetValue(sources[index], index); + var start = System.Diagnostics.Stopwatch.GetTimestamp(); + using var pinned = (IDisposable)Activator.CreateInstance(pinnedType, Internal, null, + [Image("Assembly0", 9, count), "test", array, null], null)!; + var construction = System.Diagnostics.Stopwatch.GetElapsedTime(start).TotalMilliseconds; + var modules = ((IEnumerable)pinnedType.GetProperty("Modules", Internal)! + .GetValue(pinned)!).ToArray(); + var original = pinnedType.GetMethod("Original", Internal)! + .CreateDelegate>(pinned); + var working = pinnedType.GetMethod("Working", Internal)! + .CreateDelegate>(pinned); + var position = pinnedType.GetMethod("Index", Internal)! + .CreateDelegate>(pinned); + var rejectMismatch = pinnedType.GetMethod("RejectMismatch", Internal)! + .CreateDelegate>(pinned); + var collection = Activator.CreateInstance(collectionType, Internal, null, + [pinned, new HashSet<(ModuleDefinition, uint)>()], null)!; + var getSlice = collectionType.GetMethod("SliceOf", Internal)! + .CreateDelegate>(collection); + var copies = modules.Select(original).ToArray(); + var slices = modules.Select(getSlice).ToArray(); + using var outside = AssemblyDefinition.CreateAssembly(new("Outside", new(1, 0)), "Outside", ModuleKind.Dll); + Require(working(outside.MainModule) == outside.MainModule, "Unpinned working module changed."); + ExpectOutside(() => original(outside.MainModule)); + ExpectOutside(() => position(copies[0])); + var requests = modules[0].AssemblyReferences; + ExpectOutside(() => rejectMismatch(requests[0])); + rejectMismatch(requests[2]); + var resolver = (IAssemblyResolver)pinned; + try + { + resolver.Resolve(requests[1]); + throw new InvalidOperationException("An unmatched assembly version was accepted."); + } + catch (InvalidDataException error) + { + Require(error.Message.Contains("Pinned: " + modules[0].Assembly.Name.FullName, StringComparison.Ordinal), + "The first pinned assembly with this simple name changed."); + } + Require(resolver.Resolve(modules[^1].Assembly.Name) == modules[^1].Assembly, + "Pinned resolution returned another module."); + ExpectOutside(() => resolver.Resolve(requests[2])); + + var times = new List(); + for (var repeat = 0; repeat < 6; repeat++) + { + start = System.Diagnostics.Stopwatch.GetTimestamp(); + for (var pass = 0; pass < 8; pass++) + for (var index = 0; index < modules.Length; index++) + { + Require(original(copies[index]) == copies[index] && working(copies[index]) == modules[index] && + working(modules[index]) == modules[index] && position(modules[index]) == index, + "Original/working mapping or command-line position changed."); + Require(ReferenceEquals(getSlice(modules[index]), slices[index]) && + ReferenceEquals(getSlice(copies[index]), slices[index]), "Module evidence was duplicated."); + rejectMismatch(requests[2]); + if (index > 0) + Require(resolver.Resolve(requests[index + 2]) == modules[index].Assembly, + "Repeated row lookup returned another assembly."); + } + var elapsed = System.Diagnostics.Stopwatch.GetElapsedTime(start).TotalMilliseconds; + if (repeat > 0) times.Add(elapsed); + } + times.Sort(); + Console.WriteLine($"{count} assemblies: construction {construction:F2} ms, queries {times[0]:F2} ms minimum"); + return times[0]; +} + +var small = Measure(512); +var large = Measure(4096); +Require(large < small * 24 + 30, "Assembly lookup cost grew faster than indexed queries."); +Console.WriteLine("PASS stable module positions, original copies, missing/versioned references and lookup scaling"); + +IDisposable Pin(byte[] selected, params byte[][] targets) +{ + var pins = Array.CreateInstance(sourceType, targets.Length); + for (int index = 0; index < targets.Length; index++) + { + string path = Path.Combine(directory, "IdentityTarget" + index + ".dll"); + File.WriteAllBytes(path, targets[index]); + pins.SetValue(Activator.CreateInstance(sourceType, Internal, null, [path, null], null), index); + } + return (IDisposable)Activator.CreateInstance(pinnedType, Internal, null, [selected, "test", pins, null], null)!; +} + +static byte[] Serialize(AssemblyNameDefinition name, params AssemblyNameReference[] references) +{ + using var assembly = AssemblyDefinition.CreateAssembly(name, "IdentityFixture", ModuleKind.Dll); + foreach (var reference in references) assembly.MainModule.AssemblyReferences.Add(reference); + using var stream = new MemoryStream(); + assembly.Write(stream); + return stream.ToArray(); +} + +ModuleDefinition Selected(IDisposable pinned) => + (ModuleDefinition)pinnedType.GetProperty("Selected", Internal)!.GetValue(pinned)!; + +void IdentityCases() +{ + byte[] empty = Serialize(new("Target", new(1, 2, 3, 4))); + byte[] neutral = Serialize(new("Target", new(1, 2, 3, 4)) { Culture = "neutral" }); + byte[] zero = Serialize(new("Target", new(1, 2, 3, 4)) { PublicKey = new byte[8], Attributes = 0 }); + var references = new[] + { + new AssemblyNameReference("Target", new(1, 2, 3, 4)), + new AssemblyNameReference("Target", new(1, 2, 3, 4)) { Culture = "neutral" }, + new AssemblyNameReference("Target", new(1, 2, 3, 4)) { PublicKeyToken = new byte[8] }, + new AssemblyNameReference("Target", new(2, 2, 3, 4)), + new AssemblyNameReference("Target", new(1, 3, 3, 4)), + new AssemblyNameReference("Target", new(1, 2, 4, 4)), + new AssemblyNameReference("Target", new(1, 2, 3, 5)) + }; + using (var pinned = Pin(Serialize(new("Source", new(1, 0)), references), empty, neutral, zero)) + { + var module = Selected(pinned); + var resolver = (IAssemblyResolver)pinned; + var targets = ((IEnumerable)pinnedType.GetProperty("Modules", Internal)! + .GetValue(pinned)!).Skip(1).ToArray(); + var moduleOf = pinnedType.GetMethod("ModuleOf", Internal)! + .CreateDelegate>(pinned); + var original = pinnedType.GetMethod("Original", Internal)! + .CreateDelegate>(pinned); + var contains = pinnedType.GetMethod("Contains", Internal)! + .CreateDelegate>(pinned); + for (int index = 0; index < 3; index++) + { + var reference = module.AssemblyReferences[index]; + Require(resolver.Resolve(reference) == targets[index].Assembly, "Raw identity resolved another pin."); + Require(moduleOf(new("Fixture", "Type", module, reference)) == targets[index], + "Type scope lost raw identity."); + Require(resolver.Resolve(original(module).AssemblyReferences[index]) == targets[index].Assembly, + "Original reference resolved another pin."); + } + for (int index = 3; index < references.Length; index++) + ExpectOutside(() => resolver.Resolve(module.AssemblyReferences[index])); + var changed = module.AssemblyReferences[0]; + changed.Name = "Other"; + changed.Culture = "neutral"; + changed.Version = new(99, 98, 97, 96); + changed.PublicKeyToken = new byte[8]; + changed.MetadataToken = new(TokenType.AssemblyRef, 3); + Require(resolver.Resolve(changed) == targets[0].Assembly, "Mutable Cecil columns redirected a raw row."); + targets[0].Assembly.Name.Name = "ChangedDefinition"; + Require(contains(targets[0]) && !contains(original(targets[0])), "Executable membership changed."); + Require(resolver.Resolve(targets[0].Assembly.Name) == targets[0].Assembly, + "Mutable definition name redirected resolution."); + ExpectOutside(() => resolver.Resolve(new("Target", new(1, 2, 3, 4)) + { MetadataToken = new(TokenType.AssemblyRef, 1) })); + } + CheckRawNames(); + CheckKeysAndFlags(); + CheckPlatform(); + CheckOwnership(); + CheckEntryType(); + Console.WriteLine("PASS raw identities, provenance, flags, platform equality and ownership"); +} + +void CheckRawNames() +{ + byte[] Variant(byte invalid, bool source) + { + byte[] bytes = source ? Serialize(new("Source", new(1, 0)), new AssemblyNameReference("BadX", new(1, 0))) : + Serialize(new("BadX", new(1, 0))); + using var pe = new PEReader(new MemoryStream(bytes, writable: false)); + var reader = Srm.PEReaderExtensions.GetMetadataReader(pe); + var handle = source ? reader.GetAssemblyReference(reader.AssemblyReferences.Single()).Name : + reader.GetAssemblyDefinition().Name; + int offset = pe.PEHeaders.MetadataStartOffset + reader.GetHeapMetadataOffset(HeapIndex.String) + + MetadataTokens.GetHeapOffset(handle); + bytes[offset + 3] = invalid; + return bytes; + } + using var pinned = Pin(Variant(0xff, true), Variant(0xff, false), Variant(0xfe, false)); + var modules = ((IEnumerable)pinnedType.GetProperty("Modules", Internal)! + .GetValue(pinned)!).ToArray(); + Require(modules[1].Assembly.Name.FullName == modules[2].Assembly.Name.FullName, + "Invalid UTF-8 fixture no longer collides in Cecil display."); + Require(((IAssemblyResolver)pinned).Resolve(modules[0].AssemblyReferences.Single()) == modules[1].Assembly, + "Invalid UTF-8 bytes aliased another pin."); + using var other = Pin(Variant(0xfe, true), Variant(0xff, false), Variant(0xfe, false)); + var otherModules = ((IEnumerable)pinnedType.GetProperty("Modules", Internal)! + .GetValue(other)!).ToArray(); + Require(((IAssemblyResolver)other).Resolve(otherModules[0].AssemblyReferences.Single()) == + otherModules[2].Assembly, + "Equal row indices in different images aliased."); + ExpectOutside(() => ((IAssemblyResolver)pinned).Resolve(otherModules[0].AssemblyReferences.Single())); + using var shared = Pin(Serialize(new("FirstSource", new(1, 0)), + new AssemblyNameReference("FirstTarget", new(1, 0))), + Serialize(new("SecondSource", new(1, 0)), new AssemblyNameReference("SecondTarget", new(1, 0))), + Serialize(new("FirstTarget", new(1, 0))), Serialize(new("SecondTarget", new(1, 0)))); + var sharedModules = ((IEnumerable)pinnedType.GetProperty("Modules", Internal)! + .GetValue(shared)!).ToArray(); + for (int index = 0; index < 2; index++) + { + var reference = sharedModules[index].AssemblyReferences.Single(); + Require(reference.MetadataToken.RID == 1 && + ((IAssemblyResolver)shared).Resolve(reference) == sharedModules[index + 2].Assembly, + "Equal reference row indices in one pinned set lost their owning images."); + } +} + +void CheckKeysAndFlags() +{ + byte[] key = Enumerable.Range(0, 64).Select(index => (byte)index).ToArray(); + byte[] token = SHA1.HashData(key)[^8..].Reverse().ToArray(); + var target = new AssemblyNameDefinition("Signed", new(1, 0)) + { PublicKey = key, HashAlgorithm = Mono.Cecil.AssemblyHashAlgorithm.Reserved }; + using (var pinned = Pin(Serialize(new("Source", new(1, 0)), + new("Signed", new(1, 0)) { PublicKey = key }, new("Signed", new(1, 0)) { PublicKeyToken = token }), + Serialize(target))) + { + var requests = Selected(pinned).AssemblyReferences; + Require(((IAssemblyResolver)pinned).Resolve(requests[0]) == ((IAssemblyResolver)pinned).Resolve(requests[1]), + "Full public key and equivalent token resolved apart."); + } + var invalid = new List + { + new("FlagTarget", new(1, 0)), + new("FlagTarget", new(1, 0)) { PublicKeyToken = [1, 2, 3] } + }; + foreach (uint flag in new uint[] { 0x0100, 0x0200, 0x0400, 0x0800, 0x4000, 0x8000, 0x0020, 0x80000000 }) + invalid.Add(new("FlagTarget", new(1, 0)) { Attributes = (AssemblyAttributes)flag }); + using (var pinned = Pin(Serialize(new("Source", new(1, 0)), [.. invalid]), + Serialize(new("FlagTarget", new(1, 0))))) + { + var resolver = (IAssemblyResolver)pinned; + var requests = Selected(pinned).AssemblyReferences; + Require(resolver.Resolve(requests[0]).Name.Name == "FlagTarget", "Valid flag control did not resolve."); + ExpectRejected(() => resolver.Resolve(requests[1]), "Invalid assembly identity in image row"); + foreach (var request in requests.Skip(2)) + ExpectRejected(() => resolver.Resolve(request), "Unsupported assembly identity flags in image row"); + } + using (var pinned = Pin(Serialize(new("JitFlags", new(1, 0)) { Attributes = (AssemblyAttributes)0xc000 }))) + Require(((IAssemblyResolver)pinned).Resolve(Selected(pinned).Assembly.Name) == Selected(pinned).Assembly, + "Definition JIT flags changed identity admission."); + foreach (uint flag in new uint[] { 0x0100, 0x0200, 0x0400, 0x0800, 0x0020, 0x80000000 }) + ExpectOutside(() => { using var rejected = Pin(Serialize(new("BadFlags", new(1, 0)) + { Attributes = (AssemblyAttributes)flag })); }); +} + +void CheckPlatform() +{ + var runtime = typeof(object).Assembly.GetName(); + var name = runtime.Name!; + byte[] token = runtime.GetPublicKeyToken()!; + var requests = new[] + { + new AssemblyNameReference(name, runtime.Version!) { PublicKeyToken = token }, + new AssemblyNameReference(name, new(1, 2, 3, 4)) { PublicKeyToken = token }, + new AssemblyNameReference(name, runtime.Version!) { PublicKeyToken = token, Culture = "neutral" }, + new AssemblyNameReference(name.ToLowerInvariant(), runtime.Version!) { PublicKeyToken = token }, + new AssemblyNameReference(name, runtime.Version!) + { PublicKeyToken = Convert.FromHexString("b03f5f7f11d50a3a") } + }; + using var pinned = Pin(Serialize(new("PlatformSource", new(1, 0)), requests), + Serialize(new(name, runtime.Version!))); + var module = Selected(pinned); + var resolver = (IAssemblyResolver)pinned; + var first = resolver.Resolve(module.AssemblyReferences[0]); + Require(first == resolver.Resolve(module.AssemblyReferences[1]), "Runtime versions did not reuse one module."); + Require(first == resolver.Resolve(module.AssemblyReferences[0]), "Repeated runtime request duplicated a module."); + for (int index = 2; index < requests.Length; index++) + ExpectOutside(() => resolver.Resolve(module.AssemblyReferences[index])); + using var noCore = Pin(Serialize(new("NoCore", new(1, 0)))); + var synthetic = Selected(noCore).TypeSystem.CoreLibrary as AssemblyNameReference ?? + throw new InvalidOperationException("Expected a synthetic core reference."); + ExpectOutside(() => ((IAssemblyResolver)noCore).Resolve(synthetic)); + var signatures = pinnedType.GetProperty("Signatures", Internal)!.GetValue(noCore)!; + var signatureOf = signatures.GetType().GetMethod("Type", Internal)!; + ExpectOutside(() => signatureOf.Invoke(signatures, [Selected(noCore).TypeSystem.Int32])); +} + +void CheckOwnership() +{ + byte[] bytes = Serialize(new("Owner", new(1, 0)), new AssemblyNameReference("Absent", new(1, 0))); + using var pinned = Pin(bytes); + var register = pinnedType.GetMethod("RegisterIdentityImage", Internal)! + .CreateDelegate>(pinned); + var unregister = pinnedType.GetMethod("UnregisterIdentityImage", Internal)! + .CreateDelegate>(pinned); + var contains = pinnedType.GetMethod("Contains", Internal)! + .CreateDelegate>(pinned); + var reject = pinnedType.GetMethod("RejectMismatch", Internal)! + .CreateDelegate>(pinned); + var rows = (System.Collections.IDictionary)pinnedType.GetField("rowsByImage", Internal)!.GetValue(pinned)!; + int rowsBefore = rows.Count; + byte[] invalid = Serialize(new("InvalidImport", new(1, 0)) { Attributes = (AssemblyAttributes)0x100 }); + using var invalidStream = new MemoryStream(invalid, writable: false); + using var invalidModule = ModuleDefinition.ReadModule(invalidStream); + ExpectOutside(() => register(invalidModule, invalid)); + Require(rows.Count == rowsBefore, "Rejected import retained its image rows."); + ExpectOutside(() => reject(invalidModule.Assembly.Name)); + using var stream = new MemoryStream(bytes, writable: false); + using var imported = ModuleDefinition.ReadModule(stream); + register(imported, bytes); + Require(!contains(imported), "Import registration admitted executable membership."); + reject(imported.AssemblyReferences[0]); + ExpectOutside(() => register(imported, bytes)); + reject(imported.AssemblyReferences[0]); + Require(rows.Count == rowsBefore + 1, "Duplicate import registration changed its existing image rows."); + unregister(imported); + Require(rows.Count == rowsBefore, "Released import retained its image rows."); + ExpectOutside(() => reject(imported.AssemblyReferences[0])); + var active = pinnedType.GetProperty("Active", BindingFlags.NonPublic | BindingFlags.Static)!; + ExpectOutside(() => { using var duplicate = Pin(bytes, bytes); }); + Require(ReferenceEquals(active.GetValue(null), pinned), "Constructor failure replaced the active owner."); + var owned = (System.Collections.IDictionary)pinnedType.GetField("ownedModules", Internal)!.GetValue(pinned)!; + var streams = owned.Values.Cast().ToArray(); + var reference = Selected(pinned).AssemblyReferences[0]; + pinned.Dispose(); + pinned.Dispose(); + Require(streams.All(item => !item.CanRead), "Disposed resolver retained an owned module stream."); + ExpectOutside(() => reject(reference)); + + var read = extractor.GetType("Nethermind.LeanCIL.StaticData", true)! + .GetMethod("ReadModule", BindingFlags.Static | BindingFlags.NonPublic)!; + MemoryStream? failedStream = null; + Action failRetention = stream => + { + failedStream = stream; + throw new InvalidDataException("Retention callback rejected ownership"); + }; + ExpectOutside(() => read.Invoke(null, [bytes, null, failRetention])); + Require(failedStream is { CanRead: false }, "Failed ownership callback retained an open stream."); +} + +void CheckEntryType() +{ + using var target = AssemblyDefinition.CreateAssembly(new("ValueOwner", new(1, 0)), "ValueOwner", ModuleKind.Dll); + var core = new AssemblyNameReference("System.Runtime", new(11, 0, 0, 0)) + { PublicKeyToken = Convert.FromHexString("b03f5f7f11d50a3a") }; + target.MainModule.AssemblyReferences.Add(core); + target.MainModule.Types.Add(new("Fixture", "Value", Mono.Cecil.TypeAttributes.Public | + Mono.Cecil.TypeAttributes.Sealed | Mono.Cecil.TypeAttributes.SequentialLayout, + new TypeReference("System", "ValueType", target.MainModule, core))); + using var stream = new MemoryStream(); + target.Write(stream); + using var pinned = Pin(Serialize(new("Entry", new(1, 0))), stream.ToArray()); + var module = Selected(pinned); + var method = extractor.GetType("Nethermind.LeanCIL.ContractMethodMetadata", true)! + .GetMethod("EntryArgument", BindingFlags.Static | BindingFlags.NonPublic)!; + var argument = (TypeReference)method.Invoke(null, [module, "Fixture.Value, ValueOwner"])!; + Require(argument is TypeDefinition && argument.Module != module && + argument.MetadataToken.TokenType == TokenType.TypeDef && ReferenceEquals(argument.Resolve(), argument) && + module.AssemblyReferences.Count == 0, "Pinned entry type lost its defining image provenance."); +} + +void ReferenceScaling() +{ + const int QueryPasses = 3; + (long Registration, long Decoding, long Queries) MeasureReferences(int count, int nameBytes, int keyBytes) + { + var requests = new AssemblyNameReference[count]; + for (int index = 0; index < count; index++) + requests[index] = new(new string('N', nameBytes - 4) + index.ToString("D4"), new(1, 0)) + { PublicKey = new byte[keyBytes] }; + byte[] bytes = Serialize(new("Scaling", new(1, 0)), requests); + long allocated = GC.GetAllocatedBytesForCurrentThread(); + long started = System.Diagnostics.Stopwatch.GetTimestamp(); + using var pinned = Pin(bytes); + double registrationMs = System.Diagnostics.Stopwatch.GetElapsedTime(started).TotalMilliseconds; + long registration = GC.GetAllocatedBytesForCurrentThread() - allocated; + var reject = pinnedType.GetMethod("RejectMismatch", Internal)! + .CreateDelegate>(pinned); + var module = Selected(pinned); + allocated = GC.GetAllocatedBytesForCurrentThread(); + started = System.Diagnostics.Stopwatch.GetTimestamp(); + foreach (var reference in module.AssemblyReferences) reject(reference); + double decodingMs = System.Diagnostics.Stopwatch.GetElapsedTime(started).TotalMilliseconds; + long decoding = GC.GetAllocatedBytesForCurrentThread() - allocated; + var metadata = pinnedType.GetMethod("Metadata", Internal)!.Invoke(pinned, [module])!; + var cache = (System.Collections.IDictionary)metadata.GetType().GetField("assemblyIdentities", Internal)! + .GetValue(metadata)!; + Require(cache.Count == count + 1, "Raw identities were not cached once per requested row."); + var cached = cache.Values.Cast().ToArray(); + // One pass can carry bytes the runtime allocated on this thread for itself; an allocation + // per query appears in every pass, so the least pass is the cost of the queries. + double queryMs = double.MaxValue; + long queries = long.MaxValue; + for (int pass = 0; pass < QueryPasses; pass++) + { + allocated = GC.GetAllocatedBytesForCurrentThread(); + started = System.Diagnostics.Stopwatch.GetTimestamp(); + for (int repeat = 0; repeat < 64; repeat++) + foreach (var reference in module.AssemblyReferences) reject(reference); + queryMs = Math.Min(queryMs, System.Diagnostics.Stopwatch.GetElapsedTime(started).TotalMilliseconds); + queries = Math.Min(queries, GC.GetAllocatedBytesForCurrentThread() - allocated); + } + Require(cache.Count == cached.Length && cache.Values.Cast().Zip(cached) + .All(pair => ReferenceEquals(pair.First, pair.Second)), "Repeated resolution rebuilt raw identities."); + Console.WriteLine($"{count} refs, name/key {nameBytes}/{keyBytes}: registration {registrationMs:F2} ms " + + $"{registration} B, decoding {decodingMs:F2} ms {decoding} B, queries {queryMs:F2} ms {queries} B"); + return (registration, decoding, queries); + } + + MeasureReferences(8, 16, 64); + (long Registration, long Decoding, long Queries)? previous = null; + foreach (int count in new[] { 64, 256, 1024 }) + { + var current = MeasureReferences(count, 64, 256); + if (previous is { } prior) + Require(current.Registration < prior.Registration * 6 && current.Decoding < prior.Decoding * 6 && + current.Queries < prior.Queries * 6 + 1024, "Reference count allocation grew superlinearly."); + previous = current; + } + previous = null; + foreach (int size in new[] { 16, 64, 256 }) + { + var current = MeasureReferences(64, size, size * 4); + if (previous is { } prior) + Require(current.Registration < prior.Registration * 6 && current.Decoding < prior.Decoding * 6, + "Identity payload allocation grew superlinearly."); + previous = current; + } +} + +static void Require(bool condition, string message) +{ + if (!condition) throw new InvalidOperationException(message); +} + +static void ExpectRejected(Action action, string message) +{ + try { action(); } + catch (InvalidDataException error) when (error.Message == message) { return; } + throw new InvalidOperationException("Expected exact assembly identity rejection: " + message); +} + +static void ExpectOutside(Action action) +{ + try { action(); } + catch (InvalidDataException) { return; } + catch (TargetInvocationException error) when (error.InnerException is InvalidDataException) { return; } + throw new InvalidOperationException("An unpinned module or mismatched reference was accepted."); +} diff --git a/Tests/MetadataBinding.lean b/Tests/MetadataBinding.lean new file mode 100644 index 000000000..37aa9681e --- /dev/null +++ b/Tests/MetadataBinding.lean @@ -0,0 +1,1869 @@ +import Tests.MetadataWorld + +/-! +# Metadata binding tests + +A hand-built world of two pinned assemblies: `T`, which carries the operands under test, and +`Lib`, which defines the class `Lib.Box` that `T` calls into. Every operand of `T` that the +extractor can state is checked against its metadata and must pass. Each rejection then changes +one fact of the metadata or of the operand and asserts the exact error. +-/ + +namespace Tests.MetadataBinding + +open CIL CIL.Extraction Tests.MetadataBuilder Tests.MetadataWorld + + +/-- The publisher token in the framework's System.Runtime AssemblyRef rows. -/ +private def runtimePublisher : List Nat := [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + +/-- Whether checking an operand against a pair of slices gives these facts. -/ +private def passes (ownSlice libSlice : MetadataSlice) (token : Nat) (operand : RawOperand) + (arity : Nat := 0) : Bool := + match (do + let env := MetadataEnv.ofSlices [← ownSlice.index, ← libSlice.index] + (platform := worldPlatform) + checkEntry env arity token operand) with + | .ok _ => true + | .error _ => false + +/-- Whether checking an operand fails with exactly this error. -/ +private def rejects (ownSlice libSlice : MetadataSlice) (token : Nat) (operand : RawOperand) + (error : MetadataError) (arity : Nat := 0) : Bool := + match (do + let env := MetadataEnv.ofSlices [← ownSlice.index, ← libSlice.index] + (platform := worldPlatform) + checkEntry env arity token operand) with + | .error found => found == error + | .ok _ => false + +/-- A runtime MemberRef, optionally instantiated by a MethodSpec, in the calling assembly. -/ +private def memberFixture (namespace_ owner name : String) (signature : List Nat) + (instantiation : Option (List Nat)) (ownerArgument : Option Nat := none) : + MetadataSlice × Nat := build (do + let _ ← ownWorld + let reference ← row Table.typeRef [6, ← string owner, ← string namespace_] + let parent ← match ownerArgument with + | none => pure (tokenRow reference * 8 + 1) + | some code => do + let spec ← row Table.typeSpec [← blob [0x15, 0x12, tokenRow reference * 4 + 1, 1, code]] + pure (tokenRow spec * 8 + 4) + let member ← row Table.memberRef [parent, ← string name, ← blob signature] + match instantiation with + | none => pure member + | some arguments => row Table.methodSpec [tokenRow member * 2 + 1, ← blob arguments] + ) (bytesOf "T") ownIdentity + +/-- A static external call whose closed arguments and open declaration are checked separately. -/ +private def memberCall (owner : TypeExpr) (name : MetadataName) (text identity : String) + (arity : Nat) (arguments : List TypeExpr) (declared : OpenSignature) (returned : TypeExpr) + (parameters : List TypeExpr) : CallSite := + { method := { owner, name, genericArity := arity, hasThis := false, + methodArguments := arguments, returnType := returned, parameters }, + name := text, target := .external (runtimeScope ++ "::" ++ identity), declared, + typeArguments := arguments, parameterCount := parameters.length, returnsValue := true } + +/-- The SizeOf call states Int32 as its only method argument. -/ +private def sizeCall (arity : Nat) (arguments : List TypeExpr) : CallSite := + memberCall (.atom runtimeScope "System.Runtime.CompilerServices.Unsafe") name!"SizeOf" "SizeOf" + "System.Int32 System.Runtime.CompilerServices.Unsafe::SizeOf()" arity arguments + { convention := 0x10, returnType := int32 } int32 [] + +/-- The first parameter of Object.ReferenceEquals as claimed before and after substitution. -/ +private def referenceEqualsCall (declared closed : TypeExpr) : CallSite := + let root := TypeExpr.object runtimeScope "System.Object" + let result := TypeExpr.atom runtimeScope "System.Boolean" + memberCall (.atom runtimeScope "System.Object") name!"ReferenceEquals" "ReferenceEquals" + "System.Boolean System.Object::ReferenceEquals(System.Object,System.Object)" 0 [] + { convention := 0, returnType := result, parameters := [declared, root] } result [closed, root] + +/-- An object parameter must name System.Object in the signature's primitive scope in both + the open declaration and the closed call. -/ +theorem binds_root_object_parameters : + let member := memberFixture "System" "Object" "ReferenceEquals" [0, 2, 2, 0x1C, 0x1C] none + let root := TypeExpr.object runtimeScope "System.Object" + let wrongScope := TypeExpr.object libScope "System.Object" + let wrongName := TypeExpr.object runtimeScope "System.String" + passes member.1 lib member.2 (.call (referenceEqualsCall root root)) = true ∧ + [(wrongScope, root), (root, wrongScope), (wrongName, root), (root, wrongName)].all + (fun (openType, closedType) => rejects member.1 lib member.2 + (.call (referenceEqualsCall openType closedType)) (.operandMismatch member.2)) = true + := by decide +kernel +#print axioms binds_root_object_parameters + +/-- An absent instantiation is valid for Min. A present instantiation must have positive arity. -/ +private def minCall : CallSite := + memberCall (.atom runtimeScope "System.Math") name!"Min" "Min" + "System.Int32 System.Math::Min(System.Int32,System.Int32)" 0 [] + { convention := 0, returnType := int32, parameters := [int32, int32] } int32 [int32, int32] + +example : + let spec := memberFixture "System.Runtime.CompilerServices" "Unsafe" "SizeOf" + [0x10, 2, 0, 8] (some [0x0A, 1, 8]) + rejects spec.1 lib spec.2 (.call (sizeCall 2 [int32])) (.genericMismatch spec.2) := by + decide +kernel + +example : + let member := memberFixture "System.Runtime.CompilerServices" "Unsafe" "SizeOf" + [0x10, 1, 0, 8] none + rejects member.1 lib member.2 (.call (sizeCall 1 [])) (.genericMismatch member.2) := by + decide +kernel + +example : + let spec := memberFixture "System" "Math" "Min" [0, 2, 8, 8, 8] (some [0x0A, 0]) + rejects spec.1 lib spec.2 (.call minCall) (.genericMismatch spec.2) := by decide +kernel + +example : + let spec := memberFixture "System.Runtime.CompilerServices" "Unsafe" "SizeOf" + [0x10, 1, 0, 8] (some [0x0A, 1, 8]) + let member := memberFixture "System" "Math" "Min" [0, 2, 8, 8, 8] none + passes spec.1 lib spec.2 (.call (sizeCall 1 [int32])) && + passes member.1 lib member.2 (.call minCall) := by decide +kernel + +/-- G.M() declares an owner or method variable as its return type. -/ +private def genericOwnerCall (returned : TypeExpr) (spelling : String) : CallSite := + memberCall (.apply (.objectConstructor runtimeScope "System.G`1") uint64) name!"M" "M" + (spelling ++ " System.G`1::M()") 1 [uint64] + { convention := 0x10, returnType := returned } uint64 [] + +/-- Both instances return UInt64, so only the open declaration distinguishes !0 and !!0. -/ +example : + let owner := memberFixture "System" "G`1" "M" [0x10, 1, 0, 0x13, 0] + (some [0x0A, 1, 0x0B]) (some 0x0B) + rejects owner.1 lib owner.2 (.call (genericOwnerCall (.parameter 0) "!0")) + (.operandMismatch owner.2) := by decide +kernel + +example : + let member := memberFixture "System" "G`1" "M" [0x10, 1, 0, 0x1E, 0] + (some [0x0A, 1, 0x0B]) (some 0x0B) + rejects member.1 lib member.2 (.call (genericOwnerCall (.ownerParameter 0) "!!0")) + (.operandMismatch member.2) := by decide +kernel + +example : + let owner := memberFixture "System" "G`1" "M" [0x10, 1, 0, 0x13, 0] + (some [0x0A, 1, 0x0B]) (some 0x0B) + let member := memberFixture "System" "G`1" "M" [0x10, 1, 0, 0x1E, 0] + (some [0x0A, 1, 0x0B]) (some 0x0B) + passes owner.1 lib owner.2 (.call (genericOwnerCall (.ownerParameter 0) "!0")) && + passes member.1 lib member.2 (.call (genericOwnerCall (.parameter 0) "!!0")) := by + decide +kernel + +/-- The signed BigMul(int,int) operand has a concrete Int64 result before substitution. -/ +private def signedProduct (declared : TypeExpr) (identity : String) : CallSite := + let result := TypeExpr.atom runtimeScope "System.Int64" + memberCall (.atom runtimeScope "System.Math") name!"BigMul" "BigMul" + (identity ++ " System.Math::BigMul(System.Int32,System.Int32)") 0 [] + { convention := 0, returnType := declared, parameters := [int32, int32] } result [int32, int32] + +example : + let result := TypeExpr.atom runtimeScope "System.Int64" + let member := memberFixture "System" "Math" "BigMul" [0, 2, 0x0A, 8, 8] none + passes member.1 lib member.2 (.call (signedProduct result "System.Int64")) := by decide +kernel + +example : + let member := memberFixture "System" "Math" "BigMul" [0, 2, 0x1E, 0, 8, 8] none + let openCall := { signedProduct (.parameter 0) "!!0" with method.returnType := .parameter 0 } + rejects member.1 lib member.2 (.call openCall) (.operandMismatch member.2) 1 && + (substituteOperand [.atom runtimeScope "System.Int64"] [] 1 (.call openCall)).any + (fun | .call site => site.method.returnType == .atom runtimeScope "System.Int64" + | _ => false) := by decide +kernel + +/-- Replace the bytes of a row of a slice. -/ +private def mutateRow (slice : MetadataSlice) (token : Nat) (change : List Nat → List Nat) : + MetadataSlice := + { slice with rows := slice.rows.map fun (key, bytes) => + if key == token then (key, change bytes) else (key, bytes) } + +/-- Replace the bytes of a blob of a slice. -/ +private def mutateBlob (slice : MetadataSlice) (offset : Nat) (change : List Nat → List Nat) : + MetadataSlice := + { slice with blobs := slice.blobs.map fun (key, bytes) => + if key == offset then (key, change bytes) else (key, bytes) } + +/-! ## Operands that match their metadata -/ + +theorem accepts_type_operand : + passes own.1 lib tokens.objectRef (.type (.atom runtimeScope "System.Object")) = true := by + decide +kernel +theorem accepts_external_call : + passes own.1 lib tokens.writeLine (.call writeLineSite) = true := by decide +kernel +theorem accepts_managed_call : + passes own.1 lib tokens.getRef (.call getSite) = true := by decide +kernel +/-- A virtual interface slot requires a carried method declared on an interface. -/ +theorem static_slot_requires_metadata : + rejects own.1 lib tokens.writeLine + (.call { writeLineSite with staticVirtualInterfaceSlot := true }) + (.operandMismatch tokens.writeLine) && + rejects own.1 lib tokens.getRef (.call { getSite with staticVirtualInterfaceSlot := true }) + (.operandMismatch tokens.getRef) := by decide +kernel +#print axioms static_slot_requires_metadata + +/-- Both the interface owner and the virtual method flag are required for a static slot. -/ +theorem static_slot_flags : + let markInterface := fun (slice : MetadataSlice) => + mutateRow slice (tokenOf Table.typeDef 2) fun bytes => bytes.set 0 0xA1 + let markVirtual := fun (slice : MetadataSlice) => + mutateRow slice (tokenOf Table.methodDef 1) fun bytes => bytes.set 6 0xD6 + let slot := RawOperand.call { getSite with staticVirtualInterfaceSlot := true } + passes own.1 (markVirtual (markInterface lib)) tokens.getRef slot && + !passes own.1 (markVirtual lib) tokens.getRef slot && + !passes own.1 (markInterface lib) tokens.getRef slot := by + decide +kernel +#print axioms static_slot_flags +theorem accepts_generic_call : + passes own.1 lib tokens.idSpec (.call idSite) = true := by decide +kernel +theorem accepts_string : + passes own.1 lib tokens.hello (.text "hi") = true := by decide +kernel +theorem accepts_indirect : + passes own.1 lib tokens.indirect (.indirectCall .void [int32] false false 0) = true := by + decide +kernel +theorem accepts_fields : + passes own.1 lib tokens.totalField + (.field (.atom "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" "Program") + 1 int32 name!"total" true) = true ∧ + passes own.1 lib tokens.countField countOperand = true := by decide +kernel +/-! ## Operands whose metadata or claim changed -/ + +/-- The bytes of a row of a slice. -/ +private def rowOf (slice : MetadataSlice) (token : Nat) : List Nat := + (slice.rows.lookup token).getD [] + +/-- The offset of the blob entry with a payload, assuming a one-byte length prefix. -/ +private def blobOffset (slice : MetadataSlice) (payload : List Nat) : Nat := + match slice.blobs.find? (fun entry => entry.2.drop 1 == payload) with + | some (offset, _) => offset + | none => 0 + +/-- Both assemblies declare Int32 Get(Object). Lib uses a different core AssemblyRef version. -/ +private def getWithObject (slice : MetadataSlice) : MetadataSlice := + mutateBlob slice (blobOffset slice [0, 0, 8]) fun _ => [4, 0, 1, 8, 0x1C] + +/-- Primitive results and parameters bind separately to the MemberRef and MethodDef scopes. -/ +theorem managed_call_binds_both_primitive_scopes : + let caller := getWithObject own.1 + let callee := mutateRow (getWithObject lib) 0x23000001 fun row => row.set 0 10 + let scope := "System.Runtime, Version=10.0.0.0, Culture=neutral, " ++ + "PublicKeyToken=b03f5f7f11d50a3a" + let declaredObject := TypeExpr.object runtimeScope "System.Object" + let closedObject := TypeExpr.object scope "System.Object" + let closedResult := TypeExpr.atom scope "System.Int32" + let call := { getSite with + declared.parameters := [declaredObject], parameterCount := 1 + method.parameters := [closedObject], method.returnType := closedResult } + passes caller callee tokens.getRef (.call call) = true ∧ + [{ call with declared.parameters := [closedObject] }, + { call with method.parameters := [declaredObject] }, + { call with declared.returnType := closedResult }, + { call with method.returnType := int32 }].all (fun changed => + rejects caller callee tokens.getRef (.call changed) (.operandMismatch tokens.getRef)) = true + := by decide +kernel +#print axioms managed_call_binds_both_primitive_scopes + +/-- A row whose two bytes at `position` are copied from another row of the slice. -/ +private def borrow (slice : MetadataSlice) (token source position : Nat) : MetadataSlice := + mutateRow slice token fun bytes => + bytes.take position ++ ((rowOf slice source).drop position).take 2 ++ bytes.drop (position + 2) + +/-- A type token whose name column names another string that is present: `Console` becomes + `Object`. -/ +private def renamedConsole : MetadataSlice := borrow own.1 tokens.consoleRef tokens.objectRef 2 + +/-- A type token whose name offset has no entry. -/ +private def dangling : MetadataSlice := + mutateRow own.1 tokens.consoleRef fun bytes => bytes.take 2 ++ [0xFF, 0x7F] ++ bytes.drop 4 + +theorem rejects_a_row_byte_that_names_another_string : + rejects renamedConsole lib tokens.consoleRef (.type (.atom runtimeScope "System.Console")) + (.operandMismatch tokens.consoleRef) = true := by decide +kernel +#print axioms rejects_a_row_byte_that_names_another_string + +theorem rejects_a_signature_byte_that_changes_a_parameter : + rejects (mutateBlob own.1 (blobOffset own.1 [0x00, 0x01, 0x01, 0x08]) fun bytes => + bytes.set 4 0x0A) lib tokens.writeLine (.call writeLineSite) + (.operandMismatch tokens.writeLine) = true := by decide +kernel +#print axioms rejects_a_signature_byte_that_changes_a_parameter + +theorem rejects_a_heap_offset_with_no_entry : + rejects dangling lib tokens.consoleRef (.type (.atom runtimeScope "System.Console")) + (.missingHeap 0 0x7FFF) = true := by decide +kernel +#print axioms rejects_a_heap_offset_with_no_entry + +theorem rejects_a_generic_argument_that_differs_from_the_specification : + rejects own.1 lib tokens.idSpec (.call { idSite with typeArguments := [uint64] }) + (.genericMismatch tokens.idSpec) = true := by decide +kernel +#print axioms rejects_a_generic_argument_that_differs_from_the_specification + +/-- `System.Console` as an assembly of version 9 would define it. -/ +private def olderConsole : TypeExpr := + .atom "System.Runtime, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" + "System.Console" + +theorem rejects_an_owner_scope_with_another_version : + rejects own.1 lib tokens.writeLine (.call { writeLineSite with method.owner := olderConsole }) + (.scopeMismatch tokens.writeLine) = true := by decide +kernel +#print axioms rejects_an_owner_scope_with_another_version + +theorem rejects_a_field_index_the_metadata_does_not_give : + rejects own.1 lib tokens.countField + (.field (.atom ownIdentity "Program") 1 int32 name!"count" false) + (.fieldIndexMismatch tokens.countField 1 0) = true := by decide +kernel +#print axioms rejects_a_field_index_the_metadata_does_not_give + +/-- The count field's token with the operand naming `total`, the static field after it. -/ +theorem rejects_a_field_operand_named_for_another_row : + rejects own.1 lib tokens.countField + (.field (.atom ownIdentity "Program") 0 int32 name!"total" false) + (.operandMismatch tokens.countField) = true := by decide +kernel +#print axioms rejects_a_field_operand_named_for_another_row + +theorem rejects_a_static_field_claimed_as_an_instance_field : + rejects own.1 lib tokens.totalField + (.field (.atom ownIdentity "Program") 1 int32 name!"total" false) + (.operandMismatch tokens.totalField) = true := by decide +kernel +#print axioms rejects_a_static_field_claimed_as_an_instance_field + +/-- The local variable types of the signature at `tokens.localsSignature`. -/ +private def localsFail (slice : MetadataSlice) (locals : List TypeExpr) (error : MetadataError) : + Bool := + match (do + let env := MetadataEnv.ofSlices [← slice.index, ← lib.index] (platform := worldPlatform) + checkLocals env 0 tokens.localsSignature locals) with + | .error found => found == error + | .ok _ => false + +theorem accepts_the_locals_the_signature_names : + (match (do + let env := MetadataEnv.ofSlices [← own.1.index, ← lib.index] (platform := worldPlatform) + checkLocals env 0 tokens.localsSignature [uint64]) with + | .ok _ => true + | .error _ => false) = true := by decide +kernel +#print axioms accepts_the_locals_the_signature_names + +theorem rejects_a_local_of_another_type : + localsFail own.1 [uint32] (.localMismatch 0) = true ∧ + localsFail own.1 [uint64, uint64] (.localMismatch 1) = true ∧ + localsFail own.1 [] (.localMismatch 0) = true := by decide +kernel +#print axioms rejects_a_local_of_another_type + +theorem rejects_a_pinned_local : + localsFail (mutateBlob own.1 (blobOffset own.1 [0x07, 0x01, 0x0B]) fun _ => + [4, 0x07, 0x01, 0x45, 0x0B]) [uint64] (.unsupportedSignature 2 0x45) = true := by + decide +kernel +#print axioms rejects_a_pinned_local + +/-! ## Objects outside a class field -/ + +/-- The coded `TypeDefOrRef` index of a type reference, as a signature writes it. -/ +private def typeRefCoded (token : Nat) : Nat := (token - 0x01000000) * 4 + 1 + +/-- The own slice with its one local retyped to an array of the signature type `element`. -/ +private def arrayLocal (element : List Nat) : MetadataSlice := + mutateBlob own.1 (blobOffset own.1 [0x07, 0x01, 0x0B]) fun _ => + [element.length + 3, 0x07, 0x01, 0x1D] ++ element + +/-- Whether the locals check accepts these local types. -/ +private def localsPass (slice : MetadataSlice) (locals : List TypeExpr) : Bool := + match (do + let env := MetadataEnv.ofSlices [← slice.index, ← lib.index] (platform := worldPlatform) + checkLocals env 0 tokens.localsSignature locals) with + | .ok _ => true + | .error _ => false + +/-- A type operand names as an object a class of a carried slice or `System.String`, never an + interface, an external class or `System.Object`, whose lineage no instance records. -/ +theorem names_only_carried_classes_and_strings_as_objects : + passes own.1 lib tokens.boxRef (.type (.object libScope "Lib.Box")) = true ∧ + passes own.1 lib tokens.stringRef (.type (.object runtimeScope "System.String")) = true ∧ + rejects own.1 lib tokens.shapeRef (.type (.object libScope "Lib.IShape")) + (.operandMismatch tokens.shapeRef) = true ∧ + rejects own.1 lib tokens.consoleRef (.type (.object runtimeScope "System.Console")) + (.operandMismatch tokens.consoleRef) = true ∧ + rejects own.1 lib tokens.objectRef (.type (.object runtimeScope "System.Object")) + (.operandMismatch tokens.objectRef) = true := by decide +kernel +#print axioms names_only_carried_classes_and_strings_as_objects + +/-- An array of objects holds carried classes or strings: `stelem.ref` decides a store by the + stored instance's lineage, which never lists an interface or `System.Object`. -/ +theorem holds_only_carried_classes_and_strings_in_object_arrays : + localsPass (arrayLocal [0x12, typeRefCoded tokens.boxRef]) + [.array (.object libScope "Lib.Box")] = true ∧ + localsPass (arrayLocal [0x0E]) [.array (.object runtimeScope "System.String")] = true ∧ + localsFail (arrayLocal [0x12, typeRefCoded tokens.shapeRef]) + [.array (.object libScope "Lib.IShape")] (.localMismatch 0) = true ∧ + localsFail (arrayLocal [0x12, typeRefCoded tokens.objectRef]) + [.array (.object runtimeScope "System.Object")] (.localMismatch 0) = true ∧ + localsFail (arrayLocal [0x12, typeRefCoded tokens.consoleRef]) + [.array (.object runtimeScope "System.Console")] (.localMismatch 0) = true := by + decide +kernel +#print axioms holds_only_carried_classes_and_strings_in_object_arrays + +/-- Metadata binding refuses claims that give the instantiated member types as its declaration. -/ +example : rejects own.1 lib tokens.idSpec + (.call { idSite with declared.returnType := uint32 }) (.operandMismatch tokens.idSpec) := by + decide +kernel +example : rejects own.1 lib tokens.idSpec + (.call { idSite with declared.parameters := [uint32] }) (.operandMismatch tokens.idSpec) := by + decide +kernel +example : rejects own.1 lib tokens.writeLine + (.call { writeLineSite with declared.convention := 0x40 }) (.operandMismatch tokens.writeLine) + := by decide +kernel + +/-- `System.Console.Id` as a call site instantiated with `argument`. -/ +private def idWith (argument : TypeExpr) : CallSite := + let method := { idSite.method with + returnType := argument, parameters := [argument], methodArguments := [argument] } + { idSite with method := method, typeArguments := [argument] } + +/-- The own slice with `Id` instantiated with the class the reference `token` names. -/ +private def idInstantiatedWith (token : Nat) : MetadataSlice := + mutateBlob own.1 (blobOffset own.1 [0x0A, 0x01, 0x09]) fun _ => + [4, 0x0A, 0x01, 0x12, typeRefCoded token] + +/-- A generic argument named as an object is a carried class, so substituting it into an + element position never forms an array of interfaces or of `System.Object`. -/ +theorem passes_only_carried_classes_as_object_arguments : + passes (idInstantiatedWith tokens.boxRef) lib tokens.idSpec + (.call (idWith (.object libScope "Lib.Box"))) = true ∧ + rejects (idInstantiatedWith tokens.shapeRef) lib tokens.idSpec + (.call (idWith (.object libScope "Lib.IShape"))) (.genericMismatch tokens.idSpec) = true ∧ + rejects (idInstantiatedWith tokens.objectRef) lib tokens.idSpec + (.call (idWith (.object runtimeScope "System.Object"))) + (.genericMismatch tokens.idSpec) = true := by decide +kernel +#print axioms passes_only_carried_classes_as_object_arguments + +/-- The own slice with the fields `count`, `total` and `second` retyped to `held`. -/ +private def fieldsHolding (held : List Nat) : MetadataSlice := + mutateBlob own.1 (blobOffset own.1 [0x06, 0x08]) fun _ => [held.length + 1, 0x06] ++ held + +/-- The instance field `count` of the class `T.Program`, holding `held`. -/ +private def countHolding (held : TypeExpr) : RawOperand := + .field (.object ownIdentity "Program") 0 held name!"count" false + +/-- A class instance field keeps a reference it never reads through, so it may hold an + interface or an external class as an object; an array it holds still holds carried classes, + and a static field holds no such reference. -/ +theorem holds_opaque_objects_only_in_class_instance_fields : + passes (fieldsHolding [0x12, typeRefCoded tokens.shapeRef]) lib tokens.countField + (countHolding (.object libScope "Lib.IShape")) = true ∧ + passes (fieldsHolding [0x12, typeRefCoded tokens.consoleRef]) lib tokens.countField + (countHolding (.object runtimeScope "System.Console")) = true ∧ + rejects (fieldsHolding [0x1D, 0x12, typeRefCoded tokens.objectRef]) lib tokens.countField + (countHolding (.array (.object runtimeScope "System.Object"))) + (.operandMismatch tokens.countField) = true ∧ + rejects (fieldsHolding [0x12, typeRefCoded tokens.shapeRef]) lib tokens.totalField + (.field (.atom ownIdentity "Program") 1 (.object libScope "Lib.IShape") name!"total" true) + (.operandMismatch tokens.totalField) = true := by decide +kernel +#print axioms holds_opaque_objects_only_in_class_instance_fields + +/-! ## Rows and heap entries the decoder refuses -/ + +theorem rejects_a_row_of_the_wrong_length : + rejects (mutateRow own.1 tokens.consoleRef fun bytes => bytes.take 5) lib tokens.consoleRef + (.type (.atom runtimeScope "System.Console")) (.rowLength tokens.consoleRef 6 5) = true := by + decide +kernel +#print axioms rejects_a_row_of_the_wrong_length + +theorem rejects_row_zero : + rejects own.1 lib 0x01000000 (.type (.atom runtimeScope "System.Console")) + (.tokenOutOfScope 0x01000000) = true := by decide +kernel +#print axioms rejects_row_zero + +theorem rejects_a_duplicate_token : + (match { own.1 with rows := own.1.rows ++ own.1.rows.take 1 }.index with + | .error found => found == .duplicateEntry (own.1.rows.headD (0, [])).1 + | .ok _ => false) = true := by decide +kernel +#print axioms rejects_a_duplicate_token + +theorem rejects_a_wrong_user_string_terminal_byte : + rejects { own.1 with userStrings := own.1.userStrings.map fun (offset, bytes) => + (offset, bytes.set (bytes.length - 1) 1) } lib tokens.hello (.text "hi") + (.malformedUserString (tokens.hello - 0x70000000)) = true := by decide +kernel +#print axioms rejects_a_wrong_user_string_terminal_byte + +theorem rejects_another_string_than_the_user_string : + rejects own.1 lib tokens.hello (.text "ho") (.operandMismatch tokens.hello) = true := by + decide +kernel +#print axioms rejects_another_string_than_the_user_string + +/-! ## Method names -/ + +/-- A call site's typed fields read its method, so they keep the values the fixtures state. -/ +theorem call_site_fields_read_the_method : + writeLineSite.declaringType = .atom runtimeScope "System.Console" ∧ + writeLineSite.hasThis = false ∧ writeLineSite.returnType = .void ∧ + writeLineSite.parameterTypes = [int32] ∧ writeLineSite.method.argumentCount = 1 ∧ + getSite.declaringType = .atom libScope "Lib.Box" ∧ getSite.returnType = int32 ∧ + getSite.parameterTypes = [] ∧ + idSite.method.methodArguments = idSite.typeArguments ∧ idSite.method.genericArity = 1 := by + decide +#print axioms call_site_fields_read_the_method + +/-- The method a call operand names is the row's name read as a metadata name; a site whose + display name still matches the row but whose typed name does not is refused. -/ +theorem rejects_a_method_name_other_than_the_row : + rejects own.1 lib tokens.writeLine + (.call { writeLineSite with method.name := name!"WriteLin" }) + (.operandMismatch tokens.writeLine) = true ∧ + rejects own.1 lib tokens.getRef (.call { getSite with method.name := name!"Set" }) + (.operandMismatch tokens.getRef) = true ∧ + rejects own.1 lib tokens.idSpec (.call { idSite with method.name := name!"Ids" }) + (.operandMismatch tokens.idSpec) = true := by decide +kernel +#print axioms rejects_a_method_name_other_than_the_row + +/-- A typed name whose code spells no byte list, the empty code or one whose leading digit is + not 1, names no row. -/ +theorem rejects_a_method_name_that_spells_no_bytes : + rejects own.1 lib tokens.writeLine (.call { writeLineSite with method.name := ⟨0⟩ }) + (.operandMismatch tokens.writeLine) = true ∧ + rejects own.1 lib tokens.writeLine (.call { writeLineSite with method.name := ⟨767⟩ }) + (.operandMismatch tokens.writeLine) = true := by decide +kernel +#print axioms rejects_a_method_name_that_spells_no_bytes + +/-- A call's typed method states the row's generic arity and the method arguments the site + instantiates; a site that disagrees on either names another method. -/ +theorem rejects_a_method_with_another_arity_or_arguments : + rejects own.1 lib tokens.idSpec (.call { idSite with method.genericArity := 0 }) + (.genericMismatch tokens.idSpec) = true ∧ + rejects own.1 lib tokens.idSpec (.call { idSite with method.methodArguments := [int32] }) + (.genericMismatch tokens.idSpec) = true := by decide +kernel +#print axioms rejects_a_method_with_another_arity_or_arguments + +/-- A string heap entry that holds a value above a byte is refused when the slice is indexed, + so every row name the call check reads is a metadata name. -/ +theorem rejects_a_row_name_that_is_not_bytes : + rejects { own.1 with strings := own.1.strings.map fun (offset, bytes) => + (offset, if bytes == bytesOf "WriteLine" ++ [0] then 256 :: bytes else bytes) } lib + tokens.writeLine (.call writeLineSite) .invalidByte = true := by decide +kernel +#print axioms rejects_a_row_name_that_is_not_bytes + +/-! ## Calls into a pinned assembly -/ + +theorem rejects_a_pinned_callee_with_another_name : + rejects (borrow own.1 tokens.getRef tokens.writeLine 2) lib + tokens.getRef (.call { getSite with name := "WriteLine", method.name := name!"WriteLine" }) + (.unresolvedMember tokens.getRef) = true := by decide +kernel +#print axioms rejects_a_pinned_callee_with_another_name + +theorem rejects_a_pinned_callee_with_no_link : + rejects { own.1 with links := own.1.links.filter (·.source != tokens.getRef) } lib + tokens.getRef (.call getSite) (.unresolvedMember tokens.getRef) = true := by decide +kernel +#print axioms rejects_a_pinned_callee_with_no_link + +theorem rejects_a_managed_target_other_than_the_definition : + rejects own.1 lib tokens.getRef (.call { getSite with target := .managed 0x06000002 }) + (.operandMismatch tokens.getRef) = true := by decide +kernel +#print axioms rejects_a_managed_target_other_than_the_definition + +/-- The identity of `WriteLine(long)`, which the `WriteLine(int)` token does not name. -/ +private def longWriteLine : MethodId := + .external (runtimeScope ++ "::System.Void System.Console::WriteLine(System.Int64)") + +theorem rejects_an_external_identity_other_than_the_member : + rejects own.1 lib tokens.writeLine (.call { writeLineSite with target := longWriteLine }) + (.operandMismatch tokens.writeLine) = true := by decide +kernel +#print axioms rejects_an_external_identity_other_than_the_member + +/-- `Lib.Box` defined with namespace `Lib` and name `Box`, as the reference spells it, and the + same type defined with an empty namespace and the name `Lib.Box`: both join to `Lib.Box`. -/ +private def libWithJoinedName : MetadataSlice := + let joined : ByteEntry := (0x4000, bytesOf "Lib.Box" ++ [0]) + { mutateRow lib (tokenOf Table.typeDef 2) (fun bytes => + bytes.take 4 ++ [0x00, 0x40, 0x00, 0x00] ++ bytes.drop 8) with + strings := lib.strings ++ [joined] } + +theorem rejects_a_definition_whose_namespace_and_name_only_join_to_the_reference : + rejects own.1 libWithJoinedName tokens.getRef (.call getSite) + (.operandMismatch tokens.boxRef) = true := by decide +kernel +#print axioms rejects_a_definition_whose_namespace_and_name_only_join_to_the_reference + +/-- Whether checking an operand against the own slice alone fails with exactly this error, when + `Lib` is pinned: its slice is not carried. -/ +private def rejectsWithoutLibSlice (token : Nat) (operand : RawOperand) (error : MetadataError) : + Bool := + match (do + let env := MetadataEnv.ofSlices [← own.1.index] (platform := worldPlatform) + (({} : AssemblyPrefixes).insert (bytesOf libScope)) + checkEntry env 0 token operand) with + | .error found => found == error + | .ok _ => false + +/-- A reference into a pinned assembly whose slice is left out is refused, not read as a platform + type or call. -/ +theorem rejects_a_pinned_assembly_whose_slice_is_missing : + rejectsWithoutLibSlice tokens.boxRef (.type (.atom libScope "Lib.Box")) + (.missingSlice tokens.boxRef) = true ∧ + rejectsWithoutLibSlice tokens.getRef (.call getSite) (.missingSlice tokens.boxRef) = true := by + decide +kernel +#print axioms rejects_a_pinned_assembly_whose_slice_is_missing + +/-- The `Lib` slice with a wrong number for its core-library reference. -/ +private def libWithoutCorlib : MetadataSlice := { lib with corlib := 0 } + +/-- The struct is a value type from its base reference alone, whatever `corlib` says, so a claim + that it is a class is refused with or without the number. -/ +theorem decides_a_struct_without_the_corlib_number : + passes own.1 lib tokens.pairRef (.type (.atom libScope "Lib.Pair")) = true ∧ + passes own.1 libWithoutCorlib tokens.pairRef (.type (.atom libScope "Lib.Pair")) = true ∧ + rejects own.1 lib tokens.pairRef (.type (.object libScope "Lib.Pair")) + (.operandMismatch tokens.pairRef) = true ∧ + rejects own.1 libWithoutCorlib tokens.pairRef (.type (.object libScope "Lib.Pair")) + (.operandMismatch tokens.pairRef) = true := by decide +kernel +#print axioms decides_a_struct_without_the_corlib_number + +/-- Whether a physical AssemblyRef's name, culture and token identify a core-library family. -/ +private def coreReference (family culture : String) (publisher : List Nat) : Bool := + let (slice, reference) := build (do + let name ← string family + let language ← string culture + let key ← blob publisher + row Table.assemblyRef [4, 0, 0, 0, 0, key, name, language, 0]) (bytesOf "T") ownIdentity + (slice.index >>= (·.coreAssemblyRef reference)).toOption == some true + +/-- Each core-library family retains its own publisher. An unsigned reference, a different + culture or another platform publisher cannot stand in for System.Runtime. -/ +theorem core_library_publishers : + let ecma := [0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89] + coreReference "System.Runtime" "" runtimePublisher = true ∧ + coreReference "mscorlib" "" ecma = true ∧ + coreReference "netstandard" "" [0xcc, 0x7b, 0x13, 0xff, 0xcd, 0x2d, 0xdd, 0x51] = true ∧ + coreReference "System.Private.CoreLib" "" [0x7c, 0xec, 0x85, 0xd7, 0xbe, 0xa7, 0x79, 0x8e] = + true ∧ + coreReference "System.Runtime" "" ecma = false ∧ coreReference "System.Runtime" "" [] = false ∧ + coreReference "System.Runtime" "fr" runtimePublisher = false := by decide +kernel +#print axioms core_library_publishers + +/-- Changing the publisher of System.Runtime leaves the type and assembly names intact. -/ +private def libWithOtherPublisher : MetadataSlice := + mutateBlob lib (blobOffset lib runtimePublisher) fun _ => [8, 0, 0, 0, 0, 0, 0, 0, 1] + +/-- Resolve the value-type marker of the caller's signature through its link to Lib.Pair. -/ +private def pairSignatureKind (slice : MetadataSlice) : + Except MetadataError Extraction.TypeKind := do + let env := MetadataEnv.ofSlices [← own.1.index, ← slice.index] (platform := worldPlatform) + return (← namedOf env 0 tokens.pairRef (some true)).kind + +/-- A different publisher's System.ValueType cannot satisfy a value-type signature or serve + as the slice's primitive-type assembly. -/ +theorem rejects_another_publishers_value_type : + pairSignatureKind lib = .ok .valueType ∧ + pairSignatureKind libWithOtherPublisher = .error (.operandMismatch tokens.pairRef) ∧ + (libWithOtherPublisher.index >>= MetadataIndex.corlibIdentity) = + .error (.unsupportedAssemblyRef 0x23000001) := by + exact ⟨by cil_kernel_rfl, by cil_kernel_rfl, by cil_kernel_rfl⟩ +#print axioms rejects_another_publishers_value_type + +/-- A field of a struct in a pinned assembly is read from that assembly's slice: without the + slice it is refused, not taken for a platform field. -/ +theorem rejects_a_pinned_struct_field_whose_slice_is_missing : + passes own.1 lib tokens.firstRef (.field (.atom libScope "Lib.Pair") 0 int32 name!"first" false) + = true ∧ + rejectsWithoutLibSlice tokens.firstRef + (.field (.atom libScope "Lib.Pair") 0 int32 name!"first" false) + (.missingSlice tokens.pairRef) = true := by decide +kernel +#print axioms rejects_a_pinned_struct_field_whose_slice_is_missing + +/-- `Lib.Pair` as the extractor names a struct owner, by identity and layout index. -/ +private def pairOwner : TypeExpr := .aggregate (libScope ++ "::Lib.Pair") 0 + +/-- A field of `Lib.Pair` as an operand claims it: owner, index, held type and name. -/ +private def pairFieldOf (owner : TypeExpr) (index : Nat) (held : TypeExpr) (name : MetadataName) : + RawOperand := + .field owner index held name false + +/-- A struct field operand states its owner as a value type, its index among the owner's + instance fields, its held type and its name, and the token refuses each other claim: a class + owner's index walks the struct's bases to `System.ValueType`, which no class chain has, and + a class field's token refuses an owner named as a struct. -/ +theorem checks_each_claim_of_a_struct_field : + passes own.1 lib tokens.firstRef (pairFieldOf pairOwner 0 int32 name!"first") = true ∧ + rejects own.1 lib tokens.firstRef (pairFieldOf pairOwner 1 int32 name!"first") + (.fieldIndexMismatch tokens.firstRef 1 0) = true ∧ + rejects own.1 lib tokens.firstRef + (pairFieldOf (.object libScope "Lib.Pair") 0 int32 name!"first") + (.unsupportedBase (tokenOf Table.typeDef 3)) = true ∧ + rejects own.1 lib tokens.firstRef + (pairFieldOf (.aggregate (libScope ++ "::Lib.Box") 0) 0 int32 name!"first") + (.operandMismatch tokens.firstRef) = true ∧ + rejects own.1 lib tokens.countField + (pairFieldOf (.aggregate (ownIdentity ++ "::Program") 0) 0 int32 name!"count") + (.operandMismatch tokens.countField) = true ∧ + rejects own.1 lib tokens.firstRef (pairFieldOf pairOwner 0 uint32 name!"first") + (.operandMismatch tokens.firstRef) = true ∧ + rejects own.1 lib tokens.firstRef (pairFieldOf pairOwner 0 int32 name!"second") + (.operandMismatch tokens.firstRef) = true := by decide +kernel +#print axioms checks_each_claim_of_a_struct_field + +/-- `T` with the generic struct `Pair`1` and its instance field `first : !0`, referenced + through `Pair` and through a specification that gives `Pair` two arguments. -/ +private def genericPairWorld : Step (Nat × Nat) := do + let runtimeName ← string "System.Runtime" + let publicKeyToken ← blob runtimePublisher + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtimeName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let valueTypeName ← string "ValueType" + let systemName ← string "System" + let valueTypeRef ← row Table.typeRef [1 * 4 + 2, valueTypeName, systemName] + let moduleName ← string "" + let _ ← row Table.typeDef [0, moduleName, 0, 0, 1, 1] + let pairName ← string "Pair`1" + let pair ← row Table.typeDef [0x101, pairName, 0, (valueTypeRef - 0x01000000) * 4 + 1, 1, 1] + let parameterName ← string "T" + let _ ← row Table.genericParam [0, 0, (pair - 0x02000000) * 2, parameterName] + let firstName ← string "first" + let parameterField ← blob [0x06, 0x13, 0x00] + let first ← row Table.field [0x0006, firstName, parameterField] + link linkDeclaring first 0 pair + let pairCoded := (pair - 0x02000000) * 4 + let oneBlob ← blob [0x15, 0x11, pairCoded, 1, 0x08] + let twoBlob ← blob [0x15, 0x11, pairCoded, 2, 0x08, 0x08] + let oneSpec ← row Table.typeSpec [oneBlob] + let twoSpec ← row Table.typeSpec [twoBlob] + let oneRef ← row Table.memberRef [(oneSpec - 0x1B000000) * 8 + 4, firstName, parameterField] + let twoRef ← row Table.memberRef [(twoSpec - 0x1B000000) * 8 + 4, firstName, parameterField] + link linkDefinition oneRef 0 first + link linkDefinition twoRef 0 first + pure (oneRef, twoRef) + +/-- The generic pair's slice, with its references through `Pair` and `Pair`. -/ +private def genericPair : MetadataSlice × Nat × Nat := + build genericPairWorld (bytesOf "T") ownIdentity + +/-- `Pair` as the extractor names it, and as an owner with one argument too many. -/ +private def pairOfInt : TypeExpr := + .aggregate (ownIdentity ++ "::Pair`1<" ++ runtimeScope ++ "::System.Int32>") 0 +private def pairOfTwoInts : TypeExpr := .aggregate + (ownIdentity ++ "::Pair`1<" ++ runtimeScope ++ "::System.Int32," ++ runtimeScope ++ + "::System.Int32>") 0 + +/-- A field of a generic struct instance holds its argument, read through the instance; a + specification giving the one-parameter definition two arguments is refused by its arity, and + an owner claiming two arguments names no instance the token gives. -/ +theorem checks_the_arity_of_a_generic_struct_owner : + passes genericPair.1 lib genericPair.2.1 (.field pairOfInt 0 int32 name!"first" false) = + true ∧ + rejects genericPair.1 lib genericPair.2.2 (.field pairOfInt 0 int32 name!"first" false) + (.genericMismatch 0x02000002) = true ∧ + rejects genericPair.1 lib genericPair.2.1 (.field pairOfTwoInts 0 int32 name!"first" false) + (.operandMismatch genericPair.2.1) = true := by decide +kernel +#print axioms checks_the_arity_of_a_generic_struct_owner + +/-- A void signature needs no core-library scope, and its struct owner still binds as a value + type. An Int32 result needs the callee's primitive scope. -/ +theorem rejects_a_call_naming_a_struct_as_a_class_without_the_corlib_number : + let asVoid := fun slice => mutateBlob slice (blobOffset slice [0, 0, 8]) fun _ => [3, 0, 0, 1] + let call := { sumSite with + declared.returnType := .void, method.returnType := .void, returnsValue := false } + passes own.1 lib tokens.sumRef (.call sumSite) = true ∧ + rejects own.1 libWithoutCorlib tokens.sumRef (.call sumSite) + (.operandMismatch tokens.sumRef) = true ∧ + passes (asVoid own.1) (asVoid libWithoutCorlib) tokens.sumRef (.call call) = true ∧ + rejects (asVoid own.1) (asVoid libWithoutCorlib) tokens.sumRef + (.call { call with method.owner := .object libScope "Lib.Pair" }) + (.operandMismatch tokens.sumRef) = true := by decide +kernel +#print axioms rejects_a_call_naming_a_struct_as_a_class_without_the_corlib_number + +/-- `Lib.Holder` and `System.Lazy` as a method with one type parameter names them. -/ +private def holderOfMethodParameter : TypeExpr := + .apply (.objectConstructor libScope "Lib.Holder`1") (.parameter 0) +private def lazyOfMethodParameter : TypeExpr := + .apply (.objectConstructor runtimeScope "System.Lazy`1") (.parameter 0) + +/-- Each closed instantiation of a generic class has its own statics, so a static field named + through `Lib.Holder`, whose row a pinned slice carries, or through the external + `System.Lazy` is refused whatever type the operand gives it. -/ +theorem refuses_a_static_field_of_an_instantiation : + rejects own.1 lib tokens.holderValueRef + (.field holderOfMethodParameter 0 (.parameter 0) name!"Value" true) + (.instantiatedStaticField tokens.holderValueRef) (arity := 1) = true ∧ + rejects own.1 lib tokens.holderValueRef + (.field holderOfMethodParameter 0 (.parameter 1) name!"Value" true) + (.instantiatedStaticField tokens.holderValueRef) (arity := 1) = true ∧ + rejects own.1 lib tokens.lazyValueRef + (.field lazyOfMethodParameter 0 (.parameter 0) name!"Value" true) + (.instantiatedStaticField tokens.lazyValueRef) (arity := 1) = true ∧ + rejects own.1 lib tokens.lazyValueRef + (.field lazyOfMethodParameter 0 (.parameter 1) name!"Value" true) + (.instantiatedStaticField tokens.lazyValueRef) (arity := 1) = true := by decide +kernel +#print axioms refuses_a_static_field_of_an_instantiation + +/-- No carried field list gives the position of an instance field of an unpinned type, so + `System.Lazy.Value` read as an instance field is refused at every index, and an operand + named for another member is refused by its name first. -/ +theorem rejects_an_unpinned_instance_field : + rejects own.1 lib tokens.lazyValueRef + (.field lazyOfMethodParameter 0 (.parameter 0) name!"Value" false) + (.unboundInstanceField tokens.lazyValueRef) (arity := 1) = true ∧ + rejects own.1 lib tokens.lazyValueRef + (.field lazyOfMethodParameter 1 (.parameter 0) name!"Value" false) + (.unboundInstanceField tokens.lazyValueRef) (arity := 1) = true ∧ + rejects own.1 lib tokens.lazyValueRef + (.field lazyOfMethodParameter 0 (.parameter 0) name!"Other" false) + (.operandMismatch tokens.lazyValueRef) (arity := 1) = true := by decide +kernel +#print axioms rejects_an_unpinned_instance_field + +/-- The own slice without the row of the static field between the first and the second instance + field of `Program`: the carried rows of the type leave a gap. -/ +private def ownWithGappedFields : MetadataSlice := + { own.1 with rows := own.1.rows.filter (·.1 != tokens.totalField) } + +/-- The second instance field is index 1 when every field row before it is carried. A prefix of + the type's fields with a missing row cannot give the count of instance fields before it, so + the token is refused, not read as the first instance field. -/ +theorem rejects_a_field_after_a_gap_in_the_carried_field_rows : + passes own.1 lib tokens.secondField + (.field (.atom ownIdentity "Program") 1 int32 name!"second" false) = true ∧ + rejects ownWithGappedFields lib tokens.secondField + (.field (.atom ownIdentity "Program") 1 int32 name!"second" false) + (.unresolvedMember tokens.secondField) = true := by decide +kernel +#print axioms rejects_a_field_after_a_gap_in_the_carried_field_rows + +/-! ## The use of a token -/ + +theorem rejects_a_field_access_that_disagrees_with_the_field_row : + checkSite 0x7E { staticField := some false } countOperand = false ∧ + checkSite 0x7B { staticField := some true } countOperand = false ∧ + checkSite 0x7B { staticField := some false } countOperand = true ∧ + checkSite 0x7B { staticField := some false } + (.field (.atom ownIdentity "Program") 0 int32 name!"count" false) = true ∧ + checkSite 0x7E { staticField := some true } + (.field (.atom ownIdentity "Program") 1 int32 name!"total" true) = true := by decide +kernel +#print axioms rejects_a_field_access_that_disagrees_with_the_field_row + +/-! ## The method definition -/ + +/-- A method whose receiver kind follows its declaring type's metadata. -/ +private def receiverWorld (baseName : String) (interfaceType hasThis generic : Bool) : + Step Nat := do + let runtime ← string "System.Runtime" + let system ← string "System" + let parentName ← string baseName + let program ← string "Program" + let methodName ← string "Run" + let publicKeyToken ← blob runtimePublisher + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtime, 0, 0] + modify fun builder => { builder with corlib := 1 } + let parent ← row Table.typeRef [6, parentName, system] + let base := if interfaceType then 0 else (parent - 0x01000000) * 4 + 1 + let owner <- row Table.typeDef [if interfaceType then 0x21 else 1, program, 0, base, 1, 1] + if baseName == "Enum" then + let valueName ← string "value__" + let fieldSignature ← blob [0x06, 0x08] + let field ← row Table.field [0x606, valueName, fieldSignature] + link linkDeclaring field 0 owner + if generic then + let parameterName ← string "T" + let parameter ← row Table.genericParam [0, 0, (owner - 0x02000000) * 2, parameterName] + link linkGenericParams owner 0 parameter + let signature ← blob [if hasThis then 0x20 else 0, 0, 1] + let method ← row Table.methodDef + [0, 0, if hasThis then 0x86 else 0x96, methodName, signature, 1] + link linkDeclaring method 0 owner + pure method + +/-- The complete receiver binding result for a metadata owner and a claimed receiver bit. -/ +private def receiverIdentity (baseName : String) (interfaceType hasThis objectReceiver : Bool) + (generic : Bool := false) (omitLink omitParameters : Bool := false) + (argument : InstanceArgument := .primitive 8) (claimed : TypeExpr := int32) + (platform := worldPlatform) : + Except MetadataError MethodNames := do + let fixture := build (receiverWorld baseName interfaceType hasThis generic) + (bytesOf "T") ownIdentity + let method : RawMethod := + { token := fixture.2, owner := ownIdentity ++ "::Program", hasThis := hasThis, + explicitThis := false, callingConvention := 0, attributes := if hasThis then 0x86 else 0x96, + implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := false, maxStack := 8, genericParameters := [], code := [], + ownerParameters := if generic && !omitParameters then [{ flags := 0, types := [] }] else [], + objectReceiver := objectReceiver } + let metadata := if omitLink then + { fixture.1 with links := fixture.1.links.filter (·.kind != linkGenericParams) } + else fixture.1 + let env := MetadataEnv.ofSlices [← metadata.index, ← lib.index] (platform := platform) + (exactPins := indexPins [bytesOf ownIdentity, bytesOf libScope]) + methodIdentityValid env method (if generic && !omitParameters then [argument] else []) + (if generic && !omitParameters then [claimed] else []) + +/-- Carried owner parameters remain binding when their optional link is absent. -/ +theorem binds_generic_owner_without_link : + (receiverIdentity "Object" false false false true true false).isOk = true ∧ + (receiverIdentity "Object" false false false true true true).isOk = false := by + decide +kernel +/-- info: 'Tests.MetadataBinding.binds_generic_owner_without_link' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms binds_generic_owner_without_link + +/-- The carried class admits object storage. The carried interface refuses that same storage. -/ +theorem instantiates_owners_with_only_carried_classes_as_objects : + (receiverIdentity "Object" false false false true + (argument := .named (.definition (bytesOf libScope) 0x02000002)) + (claimed := .object libScope "Lib.Box")).isOk = true ∧ + (receiverIdentity "Object" false false false true + (argument := .named (.definition (bytesOf libScope) 0x02000005)) + (claimed := .object libScope "Lib.IShape")).isOk = false := by + decide +kernel +#print axioms instantiates_owners_with_only_carried_classes_as_objects + +/-- A closed owner argument names a platform enum with the storage its entry supplies. -/ +theorem binds_closed_enum_argument : + (receiverIdentity "Object" false false false true + (argument := .named (.platform runtimeScopeBytes [systemNamespace, bytesOf "DayOfWeek"])) + (claimed := .enum (runtimeScope ++ "::System.DayOfWeek") int32) + (platform := platform_table% + [runtimeType "Object" .objectRoot, runtimeType "DayOfWeek" (.enumType 8)])).isOk = + true := by decide +kernel +#print axioms binds_closed_enum_argument + +/-- A parameter table whose entries give a parameter number and its coded owner. -/ +private def parameterWorld (parameters : List (Nat × Nat)) : MetadataSlice := + (build (parameters.mapM fun (number, owner) => + row Table.genericParam [number, 0, owner, 0]) (bytesOf "T") ownIdentity).1 + +/-- Read the first type's parameters after changing the carried rows or their optional link. -/ +private def parameterResult (parameters : List (Nat × Nat)) + (linkTarget : Option (Nat × Nat) := none) (omitted : List Nat := []) : + Except MetadataError (List GenericParamRow) := do + let metadata := parameterWorld parameters + let metadata := { metadata with + rows := metadata.rows.filter fun (token, _) => !omitted.contains (tokenRow token) + links := match linkTarget with + | none => [] + | some (position, target) => [⟨linkGenericParams, 0x02000001, position, target⟩] } + let index ← metadata.indexWith (← metadata.tables) [] + index.genericParameters 0 0x02000001 + +/-- Generic owners accept complete zero-based runs and reject conflicting or incomplete rows. -/ +theorem checks_all_carried_generic_parameters : + (parameterResult []).isOk = true ∧ + (parameterResult [(0, 2), (1, 2), (0, 4)]).isOk = true ∧ + (parameterResult [(0, 3), (0, 2)]).isOk = true ∧ + (parameterResult [(0, 2)] (some (0, 0x2A000001))).isOk = true ∧ + (parameterResult [(0, 2)] (some (1, 0x2A000001))).isOk = false ∧ + (parameterResult [(0, 2), (0, 4)] (some (0, 0x2A000002))).isOk = false ∧ + (parameterResult [(0, 4)] (some (0, 0x2A000001))).isOk = false ∧ + (parameterResult [(0, 2), (1, 2)] (some (0, 0x2A000002))).isOk = false ∧ + (parameterResult [(0, 2), (1, 2)] none [1]).isOk = false ∧ + (parameterResult [(0, 2), (1, 2)] none [2]).isOk = false ∧ + (parameterResult [(0, 2), (0, 4), (1, 2)]).isOk = false ∧ + (parameterResult [(0, 2), (2, 2)]).isOk = false := by decide +kernel +/-- info: 'Tests.MetadataBinding.checks_all_carried_generic_parameters' depends on axioms: +[propext] -/ +#guard_msgs in +#print axioms checks_all_carried_generic_parameters + +/-- An owner's parameter count over parameter rows given as a number and a coded owner, with + the rows at `omitted` left out; with `absentOnly`, only the check that no row names it, and + with `declared`, as a method whose signature declares that many. -/ +private def ownerResult (parameters : List (Nat × Nat)) (owner : Nat) (omitted : List Nat := []) + (absentOnly := false) (declared : Option Nat := none) : Option Nat := + match (do + let metadata := parameterWorld parameters + let metadata := { metadata with + rows := metadata.rows.filter fun (token, _) => + !(metadataTableOf token == Table.genericParam && omitted.contains (tokenRow token)) } + let index ← metadata.indexWith (← metadata.tables) [] + if absentOnly then do index.noGenericParameters owner; pure 0 + else match declared with + | some count => do pure (← index.methodGenericParameters 0 owner count).length + | none => do pure (← index.genericParameters 0 owner).length) with + | .ok count => some count + | .error _ => none + +/-- `TypeDef 2` (coded 4) and `MethodDef 2` (coded 5) as owners. -/ +private def typeTwo : Nat := 0x02000002 +private def methodTwo : Nat := 0x06000002 + +/-- An owner with no carried parameter has none only where its neighbours show it: the first + carried row above its coded value is row 1 or follows a carried row below it, or the last + row lies below it, or the table is empty. Coded values order a `TypeDef` before the + `MethodDef` of the same row number. -/ +theorem checks_absent_generic_parameters_by_neighbours : + ownerResult [] typeTwo = some 0 ∧ + ownerResult [(0, 6)] typeTwo = some 0 ∧ + ownerResult [(0, 2)] typeTwo = some 0 ∧ + ownerResult [(0, 2)] typeTwo [1] = none ∧ + ownerResult [(0, 2), (0, 6)] typeTwo = some 0 ∧ + ownerResult [(0, 2), (0, 6)] typeTwo [1] = none ∧ + ownerResult [(0, 2), (0, 3), (0, 6)] typeTwo [2] = none ∧ + ownerResult [(0, 4), (0, 6)] typeTwo (absentOnly := true) = none ∧ + ownerResult [(0, 4)] typeTwo (absentOnly := true) = none ∧ + ownerResult [(0, 5)] typeTwo = some 0 ∧ ownerResult [(0, 5)] methodTwo = some 1 ∧ + ownerResult [(0, 4)] methodTwo = some 0 ∧ ownerResult [(0, 4)] typeTwo = some 1 ∧ + ownerResult [(0, 4), (0, 5)] typeTwo [1] = none ∧ + ownerResult [(0, 2), (0, 4), (0, 6)] typeTwo [2] = none := by decide +kernel +#print axioms checks_absent_generic_parameters_by_neighbours + +/-- A method whose signature declares no generic parameters has none without neighbours, while + a run carried for it is still read; one that declares parameters keeps the neighbour rule. -/ +theorem reads_an_undeclared_method_run_from_its_signature : + ownerResult [(0, 2), (0, 6)] methodTwo [1] (declared := some 0) = some 0 ∧ + ownerResult [(0, 2), (0, 6)] methodTwo [1] = none ∧ + ownerResult [(0, 2), (0, 6)] methodTwo [1] (declared := some 1) = none ∧ + ownerResult [(0, 5)] methodTwo (declared := some 0) = some 1 ∧ + ownerResult [(0, 5), (0, 6)] methodTwo (declared := some 1) = some 1 := by decide +kernel +#print axioms reads_an_undeclared_method_run_from_its_signature + +/-- A slice with the generic class `Holder`1` and the class `Plain`, each declaring one static + `int`; gives the tokens of `Holder`, its field, `Plain` and its field. -/ +private def arityWorld : Step (Nat × Nat × Nat × Nat) := do + let runtimeName ← string "System.Runtime" + let publicKeyToken ← blob runtimePublisher + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtimeName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let objectName ← string "Object" + let systemName ← string "System" + let _ ← row Table.typeRef [1 * 4 + 2, objectName, systemName] + let moduleName ← string "" + let _ ← row Table.typeDef [0, moduleName, 0, 0, 1, 1] + let intField ← blob [0x06, 0x08] + let holderName ← string "Holder`1" + let holder ← row Table.typeDef [1, holderName, 0, 1 * 4 + 1, 1, 1] + let parameterName ← string "T" + let _ ← row Table.genericParam [0, 0, (holder - 0x02000000) * 2, parameterName] + let heldName ← string "V" + let held ← row Table.field [0x0016, heldName, intField] + link linkDeclaring held 0 holder + let plainName ← string "Plain" + let plain ← row Table.typeDef [1, plainName, 0, 1 * 4 + 1, 2, 1] + let valueName ← string "W" + let value ← row Table.field [0x0016, valueName, intField] + link linkDeclaring value 0 plain + pure (holder, held, plain, value) + +private def arity : MetadataSlice × Nat × Nat × Nat × Nat := + build arityWorld (bytesOf "T") ownIdentity + +private def holder : Nat := arity.2.1 +private def held : Nat := arity.2.2.1 +private def plain : Nat := arity.2.2.2.1 +private def plainValue : Nat := arity.2.2.2.2 + +/-- Whether a step over the environment of the arity slice succeeds. -/ +private def arityAccepts {α : Type} (step : MetadataEnv → Except MetadataError α) : Bool := + match (do step (MetadataEnv.ofSlices [← arity.1.index] (platform := objectPlatform))) with + | .ok _ => true + | .error _ => false + +/-- One `int` argument, and two. -/ +private def oneInt : SigList := .cons (.primitive 8) .nil +private def twoInts : SigList := .cons (.primitive 8) oneInt + +/-- A generic definition is named only as an instance with one argument per parameter: not as + a type alone, through a type token, or as the owner of a field token, and a non-generic one + takes no arguments. -/ +theorem generic_definitions_take_their_arguments : + arityAccepts (·.resolve 0 (.named false plain)) = true ∧ + arityAccepts (·.resolve 0 (.named false holder)) = false ∧ + arityAccepts (·.resolve 0 (.genericInst false holder oneInt)) = true ∧ + arityAccepts (·.resolve 0 (.genericInst false holder .nil)) = false ∧ + arityAccepts (·.resolve 0 (.genericInst false holder twoInts)) = false ∧ + arityAccepts (·.resolve 0 (.genericInst false plain oneInt)) = false ∧ + arityAccepts (·.resolveToken 0 plain) = true ∧ + arityAccepts (·.resolveToken 0 holder) = false ∧ + arityAccepts (·.fieldSite plainValue) = true ∧ + arityAccepts (·.fieldSite held) = false := by decide +kernel +#print axioms generic_definitions_take_their_arguments + +/-- Class receivers are objects; value and enum receivers are managed references. -/ +theorem accepts_matching_receivers : + (receiverIdentity "Object" false true true).isOk = true ∧ + (receiverIdentity "ValueType" false true false).isOk = true ∧ + (receiverIdentity "Enum" false true false).isOk = true ∧ + (receiverIdentity "ValueType" false true false true).isOk = true ∧ + (receiverIdentity "Object" false false false).isOk = true := by decide +kernel +/-- info: 'Tests.MetadataBinding.accepts_matching_receivers' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms accepts_matching_receivers + +/-- A rejected receiver reports the method token whose claim is inconsistent. -/ +private def receiverMismatch (result : Except MetadataError MethodNames) : Bool := + match result with + | .error error => error == .methodMismatch 0x06000001 + | .ok _ => false + +/-- A receiver claim cannot reinterpret a class instance as a managed reference. -/ +theorem rejects_mismatched_receivers : + receiverMismatch (receiverIdentity "Object" false true false) = true ∧ + receiverMismatch (receiverIdentity "ValueType" false true true) = true ∧ + receiverMismatch (receiverIdentity "Enum" false true true) = true ∧ + receiverMismatch (receiverIdentity "Object" true true false) = true ∧ + receiverMismatch (receiverIdentity "Object" true true true) = true ∧ + receiverMismatch (receiverIdentity "Object" false false true) = true := by decide +kernel +/-- info: 'Tests.MetadataBinding.rejects_mismatched_receivers' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms rejects_mismatched_receivers + +/-- `T.Program.Main`, a public static method with no parameters. -/ +private def mainMethod : RawMethod := + { token := tokens.main, owner := ownIdentity ++ "::Program", hasThis := false, + explicitThis := false, callingConvention := 0, attributes := 0x96, implementation := 0, + returnType := .void, parameters := [], locals := [], initLocals := false, maxStack := 8, + genericParameters := [], code := [] } + +/-- Whether the method definition is accepted, or refused with this error. -/ +private def identity (slice : MetadataSlice) (method : RawMethod) : + Except MetadataError MethodNames := do + let env := MetadataEnv.ofSlices [← slice.index, ← lib.index] (platform := worldPlatform) + methodIdentityValid env method [] [] + +private def identityFails (slice : MetadataSlice) (method : RawMethod) (error : MetadataError) : + Bool := + match identity slice method with + | .error found => found == error + | .ok _ => false + +theorem accepts_the_method_definition_its_row_gives : + (match identity own.1 mainMethod with + | .ok _ => true + | .error _ => false) = true := by decide +kernel +#print axioms accepts_the_method_definition_its_row_gives + +/-- A MethodDef taking ELEMENT_TYPE_OBJECT binds the root's scope and full name. -/ +theorem binds_method_definition_object_parameter : + let slice := mutateBlob own.1 (blobOffset own.1 [0, 0, 1]) fun _ => [4, 0, 1, 1, 0x1C] + let claim := fun scope name => { mainMethod with parameters := [.object scope name] } + (identity slice (claim runtimeScope "System.Object")).isOk = true ∧ + identityFails slice (claim libScope "System.Object") (.methodMismatch tokens.main) = true ∧ + identityFails slice (claim runtimeScope "System.String") (.methodMismatch tokens.main) = true + := by decide +kernel +#print axioms binds_method_definition_object_parameter + +theorem rejects_a_method_definition_that_differs_from_its_row : + identityFails own.1 { mainMethod with attributes := 0x97 } + (.methodMismatch tokens.main) = true ∧ + identityFails own.1 { mainMethod with implementation := 1 } + (.methodMismatch tokens.main) = true ∧ + identityFails own.1 { mainMethod with hasThis := true } + (.methodMismatch tokens.main) = true ∧ + identityFails own.1 { mainMethod with returnType := int32 } + (.methodMismatch tokens.main) = true ∧ + identityFails own.1 { mainMethod with parameters := [int32] } + (.methodMismatch tokens.main) = true ∧ + identityFails own.1 { mainMethod with genericParameters := [{ flags := 0, types := [] }] } + (.methodMismatch tokens.main) = true := by decide +kernel +#print axioms rejects_a_method_definition_that_differs_from_its_row + +/-- The names the row gives spell the owner the method states, and not another owner. -/ +theorem names_spell_the_owner_the_method_states : + (match identity own.1 mainMethod with + | .ok names => decide (names.Spell mainMethod) && + !decide (names.Spell { mainMethod with owner := libScope ++ "::Program" }) + | .error _ => false) = true := by decide +kernel +#print axioms names_spell_the_owner_the_method_states + +/-- A method row whose address differs from the one the body was read at is accepted: where a body + sits in the image is not part of the default check. -/ +theorem ignores_the_body_address : + (match identity { own.1 with rows := own.1.rows.map fun (token, bytes) => + (token, if token == tokens.main then 7 :: bytes.drop 1 else bytes) } mainMethod with + | .ok _ => true + | .error _ => false) = true := by decide +kernel +#print axioms ignores_the_body_address + +/-- A method's own full name spells a function pointer as Cecil does, so a method that takes a + table of them can be selected by its signature; an external call identity still spells none, + since the spelling leaves out the calling convention. -/ +theorem spells_a_function_pointer_in_the_own_signature_only : + instanceSignature (bytesOf "Examples.Kinds") (bytesOf "Indirect") false 0 [] (.primitive 9) + [.szarray (.functionPointer 0 (.primitive 9) (.cons (.primitive 9) .nil)), .primitive 8, + .primitive 9] = + some (bytesOf ("System.UInt32 Examples.Kinds::Indirect(method System.UInt32 " ++ + "*(System.UInt32)[],System.Int32,System.UInt32)")) ∧ + cecilName false resolveFuel (.functionPointer 0 (.primitive 9) .nil) = none := by + decide +kernel +#print axioms spells_a_function_pointer_in_the_own_signature_only + +/-- `Main`'s row and the method with the flags and implementation flags changed together. -/ +private def withFlags (flags implementation : Nat) : MetadataSlice × RawMethod := + (mutateRow own.1 tokens.main fun bytes => + (bytes.set 4 (implementation % 256)).set 5 (implementation / 256) + |>.set 6 (flags % 256) |>.set 7 (flags / 256), + { mainMethod with attributes := flags, implementation := implementation }) + +/-- A row whose flags say it has no IL body is refused, so no body bytes are taken for it: an + abstract method, a platform invoke and an internal call. -/ +theorem rejects_body_bytes_for_a_row_without_a_body : + identityFails (withFlags 0x0496 0).1 (withFlags 0x0496 0).2 + (.bodyLocation tokens.main) = true ∧ + identityFails (withFlags 0x2096 0).1 (withFlags 0x2096 0).2 + (.bodyLocation tokens.main) = true ∧ + identityFails (withFlags 0x0096 0x1000).1 (withFlags 0x0096 0x1000).2 + (.bodyLocation tokens.main) = true := by decide +kernel +#print axioms rejects_body_bytes_for_a_row_without_a_body + +/-- A runtime-async row is refused: its body returns the result its signature states as a task. -/ +theorem rejects_a_runtime_async_row : + identityFails (withFlags 0x0096 0x2000).1 (withFlags 0x0096 0x2000).2 + (.runtimeAsync tokens.main) = true := by decide +kernel +#print axioms rejects_a_runtime_async_row + +/-- The same rewrite of the row with `Main`'s own flags is accepted, so the refusals above come + from the flags and not from the rewrite. -/ +theorem accepts_the_row_rewritten_with_its_own_flags : + (match identity (withFlags 0x0096 0).1 (withFlags 0x0096 0).2 with + | .ok _ => true + | .error _ => false) = true := by decide +kernel +#print axioms accepts_the_row_rewritten_with_its_own_flags + +/-! ## Which extractor types name a resolved type -/ + +/-- A frame whose primitive assembly is `C`, whose modified types are anchored at `A`, and whose + method and owner have one generic parameter each. -/ +private def pairFrame : Frame := + { corlib := some (bytesOf "C"), anchor := bytesOf "A", arity := 1, ownerArity := 1 } + +/-- The type `name` of scope `scope` with the given kind. -/ +private def namedIn (scope name : String) (kind : Extraction.TypeKind) (pinned := false) : + NamedType := + { scope := bytesOf scope, name := bytesOf name, parts := [], kind := kind, pinned := pinned } + +/-- A modified open parameter binds the modifier and the variable inside the reference. -/ +example : + let frame := { pairFrame with rawIndices := true } + let modifier := namedIn "Library" "Modifier" (.external none) + let type := ResolvedType.modified true modifier (.byref (.var 0)) + frame.admits type (.modified true "Library" "Modifier" (.byref (.ownerParameter 0))) && + !frame.admits type (.modified true "Other" "Modifier" (.byref (.ownerParameter 0))) && + !frame.admits type (.modified true "Library" "Modifier" (.byref (.ownerParameter 1))) && + !frame.admits type (.modified false "Library" "Modifier" (.byref (.ownerParameter 0))) := by + decide +kernel + +/-- `System.Int32` of the primitive assembly. -/ +private def int32Atom : TypeExpr := .atom "C" "System.Int32" + +/-- The resolved platform enum `L::E` has 32-bit signed storage. -/ +private def platformEnum : NamedType := + { namedIn "L" "E" (.enumType 8) with parts := [[], bytesOf "E"] } + +/-- `L.G`, with `L.G` of the given kind and pinning. -/ +private def instanceOf (kind : Extraction.TypeKind) (pinned := false) : ResolvedType := + .genericInst (namedIn "L" "G`1" kind pinned) (.cons (.primitive 8) .nil) + +/-- A generic application with `count` method-parameter arguments. -/ +private def application : Nat → TypeExpr + | 0 => .constructor "L" "G" + | count + 1 => .apply (application count) (.parameter count) + +/-- A resolved generic instance of the same arity. -/ +private def instanceArguments (count : Nat) : ResolvedType := + .genericInst (namedIn "L" "G" .valueType) + (ResolvedList.ofList ((List.range count).map ResolvedType.mvar)) + +/-- Splitting a whole application visits each argument once. -/ +private def splitApplication (count : Nat) : Bool := + (appliedArguments (application count)).2.length == count + +/-- Structural comparison admits the matching arguments with enough fuel for their count. -/ +private def admitsApplication (count : Nat) : Bool := + typeAdmits { pairFrame with arity := count } (count + 4) + (instanceArguments count) (application count) + +/-- A single expected argument whose type differs from the outermost claimed argument. -/ +private def refusesApplication (count : Nat) : Bool := + !pairFrame.admits (instanceArguments 1) (.apply (application count) .void) + +open Lean Meta Elab Command in +run_cmd do + for check in [``splitApplication, ``admitsApplication, ``refusesApplication] do + let mut previous := 0 + for count in [16, 64, 256] do + let (statement, proof) ← liftTermElabM do + let decision := mkApp (mkConst check) (mkNatLit count) + pure (← mkEq decision (mkConst ``Bool.true), ← mkEqRefl (mkConst ``Bool.true)) + let start ← IO.getNumHeartbeats + match Kernel.check (← getEnv) {} (.letE `checked statement proof (.bvar 0) false) with + | .error error => throwKernelException error + | .ok _ => pure () + let cost := (← IO.getNumHeartbeats) - start + logInfo m!"{check.getString!} {count} arguments: {cost} kernel heartbeats" + let growth := if check == ``refusesApplication then 2 else 5 + if previous != 0 && cost > previous * growth then + throwError "generic application comparison exceeded its heartbeat growth bound" + previous := cost + +/-- The carried struct `L.S` closed over the given argument. -/ +private def structOf (argument : ResolvedType) : ResolvedType := + .genericInst (namedIn "L" "S`1" .valueType true) (.cons argument .nil) + +/-- The aggregate identity of `S` cannot name `S` even when both arguments are structs. -/ +theorem refuses_another_struct_argument : + pairFrame.admits (structOf (.named (namedIn "L" "A" .valueType true))) + (.aggregate "L::S`1" 1) = false := by decide +kernel +#print axioms refuses_another_struct_argument + +/-- The aggregate identity shared by the class and struct spellings of the inner `G`. -/ +private def structOfInstance : TypeExpr := .aggregate "L::S`1>" 1 + +/-- A class instance cannot supply the value argument of a preclosed aggregate. -/ +theorem refuses_a_class_argument : + pairFrame.admits (structOf (instanceOf .classType true)) structOfInstance = false := by + decide +kernel +#print axioms refuses_a_class_argument + +/-- Array element admission checks the generic argument of its aggregate element. -/ +theorem refuses_an_array_of_structs_with_a_class_argument : + pairFrame.admits (.szarray (structOf (instanceOf .classType true))) + (.array structOfInstance) = false := by decide +kernel +#print axioms refuses_an_array_of_structs_with_a_class_argument + +/-- Byref admission checks the generic argument of its target aggregate. -/ +theorem refuses_a_byref_to_a_struct_with_a_class_argument : + pairFrame.admits (.byref (structOf (instanceOf .classType true))) + (.byref structOfInstance) = false := by decide +kernel +#print axioms refuses_a_byref_to_a_struct_with_a_class_argument + +/-- A struct argument that itself takes a class prevents admission of the outer aggregate. -/ +theorem refuses_a_class_inside_a_struct_argument : + pairFrame.admits (structOf (structOf (instanceOf .classType true))) + (.aggregate "L::S`1>>" 2) = false := by decide +kernel +#print axioms refuses_a_class_inside_a_struct_argument + +/-- An integer, a struct closed over an integer and a carried enum are value arguments. -/ +theorem admits_aggregate_value_arguments : + pairFrame.admits (structOf (.primitive 8)) (.aggregate "L::S`1" 0) && + pairFrame.admits (structOf (instanceOf .valueType true)) structOfInstance && + pairFrame.admits (structOf (.named (namedIn "L" "E" (.enumType 8) true))) + (.aggregate "L::S`1" 0) := by decide +kernel +#print axioms admits_aggregate_value_arguments + +/-- The matching identity of a struct closed over System.Void still has an invalid argument. -/ +theorem refuses_a_void_aggregate_argument : + pairFrame.admits (structOf (.primitive 0x01)) (.aggregate "L::S`1" 0) = + false := by decide +kernel +#print axioms refuses_a_void_aggregate_argument + +/-- An enum declared inside a generic type takes its enclosing type's arguments. -/ +theorem admits_a_closed_nested_enum_argument : + let nestedEnum := ResolvedType.genericInst (namedIn "L" "Outer`1/E" (.enumType 8) true) + (.cons (.primitive 8) .nil) + pairFrame.admits (structOf nestedEnum) + (.aggregate "L::S`1>" 1) = true := by decide +kernel +#print axioms admits_a_closed_nested_enum_argument + +/-- An external head uses Cecil's spelling, including for arguments following a value argument. -/ +private def externalPairAdmits (argument : ResolvedType) (name : String) : Bool := + let pair := ResolvedType.genericInst (namedIn "L" "Pair`2" (.external (some true))) + (.cons (.primitive 8) (.cons argument .nil)) + pairFrame.admits pair (.aggregate ("L::Pair`2") 0) + +/-- A matching Cecil spelling cannot give a reference, an address or a variable value storage. -/ +theorem refuses_nonvalue_arguments_of_an_external_struct : + ([(.named (namedIn "L" "C" .classType), "C"), + (.named (namedIn "L" "I" .interfaceType), "I"), + (.primitive 0x0E, "System.String"), (.primitive 0x1C, "System.Object"), + (.szarray (.primitive 8), "System.Int32[]"), (.pointer (.primitive 8), "System.Int32*"), + (.byref (.primitive 8), "System.Int32&"), (.var 0, "!0"), (.mvar 0, "!!0"), + (.modified false (namedIn "L" "M" (.external none)) (.primitive 8), + "System.Int32 modopt(M)")] : + List (Prod ResolvedType String)).all (fun (arg, name) => !externalPairAdmits arg name) := by + decide +kernel +#print axioms refuses_nonvalue_arguments_of_an_external_struct + +/-- One pairing for each way `typeAdmits` accepts, by resolved and extractor constructor. -/ +private def acceptedPairings : List (ResolvedType × TypeExpr) := [ + -- Primitives: `void`, an atom of the primitive assembly, and a string as an object. + (.primitive 1, .void), (.primitive 8, int32Atom), + (.primitive 0x0E, .atom "C" "System.String"), (.primitive 0x1C, .atom "C" "System.Object"), + (.primitive 0x0E, .object "C" "System.String"), + -- Named types: an atom unless a carried enum, an object if a carried class or the runtime's + -- string, an aggregate if value-like, and an enum over the underlying type its carried row + -- or its platform definition states. + (.named (namedIn "L" "N" .valueType), .atom "L" "N"), + (.named (namedIn "L" "N" .classType), .atom "L" "N"), + (.named (namedIn "L" "N" (.external none)), .atom "L" "N"), + (.named (namedIn "L" "N" .classType true), .object "L" "N"), + (.named (namedIn runtimeScope "System.String" (.external (some false))), + .object runtimeScope "System.String"), + (.named (namedIn "L" "N" .valueType), .aggregate "L::N" 0), + (.named (namedIn "L" "N" (.external (some true))), .aggregate "L::N" 0), + (.named (namedIn "L" "N" (.external none)), .aggregate "L::N" 0), + (.named (namedIn "L" "E" (.enumType 8) true), .enum "L::E" int32Atom), + (.named platformEnum, .enum "L::E" int32Atom), + -- Generic parameters: the method's first, then the owner's. + (.mvar 0, .parameter 0), (.var 0, .parameter 1), + -- Element types, element by element. + (.byref (.primitive 8), .byref int32Atom), (.pointer (.primitive 8), .pointer int32Atom), + (.szarray (.primitive 8), .array int32Atom), + (.szarray (.named (namedIn "L" "N" .classType true)), .array (.object "L" "N")), + -- Instances: a value-like one as an aggregate, with its scoped arguments + -- in declaration order, and any instance as an `apply` chain over a head of its kind. + (instanceOf .valueType, .aggregate "L::G`1" 0), + (instanceOf .valueType true, .aggregate "L::G`1" 0), + (instanceOf .classType, .apply (.objectConstructor "L" "G`1") int32Atom), + (instanceOf .interfaceType, .apply (.objectConstructor "L" "G`1") int32Atom), + (instanceOf (.external (some false)), .apply (.objectConstructor "L" "G`1") int32Atom), + (instanceOf .valueType, .apply (.constructor "L" "G`1") int32Atom), + (instanceOf (.external (some true)), .apply (.constructor "L" "G`1") int32Atom), + -- Function pointers, with the calling convention and receiver flags. + (.functionPointer 0 (.primitive 8) (.cons (.primitive 8) .nil), + .functionPointer int32Atom (.signatureParameter int32Atom .void) 0 false false), + (.functionPointer 0x20 (.primitive 8) .nil, .functionPointer int32Atom .void 0 true false), + -- A modified type as an atom of the anchor, spelled with its modifier. + (.modified true (namedIn "M" "Mod" (.external none)) (.primitive 8), + .atom "A" "System.Int32 modreq(Mod)"), + (.modified false (namedIn "M" "Mod" (.external none)) (.primitive 8), + .atom "A" "System.Int32 modopt(Mod)")] + +/-- For each accepting arm, a pairing that changes the scope, the name, the kind, the index, the + element, the convention or the constructor; and pairings of constructors no arm relates. -/ +private def refusedPairings : List (ResolvedType × TypeExpr) := [ + -- Primitives. + (.primitive 8, .void), (.primitive 1, .atom "C" "System.Void"), + (.primitive 8, .atom "D" "System.Int32"), (.primitive 8, .atom "C" "System.UInt32"), + (.primitive 0x0E, .object "C" "System.Object"), (.primitive 0x1C, .object "C" "System.Object"), + (.primitive 8, .object "C" "System.Int32"), (.primitive 8, .enum "C::System.Int32" int32Atom), + -- Named types. + (.named (namedIn "L" "N" .valueType), .atom "L" "M"), + (.named (namedIn "L" "N" .valueType), .atom "K" "N"), + (.named (namedIn "L" "N" (.enumType 8)), .atom "L" "N"), + (.named (namedIn "L" "N" .valueType), .object "L" "N"), + (.named (namedIn "L" "N" (.external (some true))), .object "L" "N"), + (.named (namedIn "L" "N" .classType), .object "L" "N"), + (.named (namedIn "L" "N" .interfaceType true), .object "L" "N"), + (.named (namedIn "L" "N" (.external (some false))), .object "L" "N"), + (.named (namedIn "L" "N" (.external none)), .object "L" "N"), + (.named (namedIn "L" "System.String" (.external none)), .object "L" "System.String"), + (.named (namedIn "L" "N" .classType), .aggregate "L::N" 0), + (.named (namedIn "L" "N" (.external (some false))), .aggregate "L::N" 0), + (.named (namedIn "L" "N" .valueType), .aggregate "L::M" 0), + (.named (namedIn "L" "E" (.enumType 8) true), .enum "L::E" (.atom "C" "System.Int64")), + (.named platformEnum, .enum "L::E" (.atom "C" "System.Byte")), + (.named (namedIn "L" "E" (.external none)), .enum "L::E" int32Atom), + (.named (namedIn "L" "E" .classType), .enum "L::E" int32Atom), + (.named (namedIn "L" "E" (.external (some false))), .enum "L::E" int32Atom), + (.named (namedIn "L" "N" .valueType), .constructor "L" "N"), + (.named (namedIn "L" "N" .valueType), .parameter 0), + -- Generic parameters. + (.var 0, .parameter 0), (.mvar 0, .parameter 1), (.primitive 8, .parameter 0), + (.var 0, int32Atom), + -- Element types. + (.byref (.primitive 8), .pointer int32Atom), (.byref (.primitive 8), .array int32Atom), + (.pointer (.primitive 8), .byref int32Atom), (.szarray (.primitive 8), .pointer int32Atom), + (.szarray (.named (namedIn "L" "N" .interfaceType true)), .array (.object "L" "N")), + (.byref (.primitive 9), .byref int32Atom), + -- Instances. + (instanceOf .valueType true, .aggregate "L::G`1" 0), + (instanceOf .classType, .aggregate "L::G`1" 0), + (instanceOf .classType, .apply (.objectConstructor "L" "G`1") (.atom "C" "System.UInt32")), + (instanceOf .classType, .apply (.objectConstructor "K" "G`1") int32Atom), + (instanceOf .classType, .objectConstructor "L" "G`1"), + -- A head of the other kind, and a kind the metadata leaves open. + (instanceOf .classType, .apply (.constructor "L" "G`1") int32Atom), + (instanceOf .interfaceType, .apply (.constructor "L" "G`1") int32Atom), + (instanceOf (.external (some false)), .apply (.constructor "L" "G`1") int32Atom), + (instanceOf .valueType, .apply (.objectConstructor "L" "G`1") int32Atom), + (instanceOf (.external (some true)), .apply (.objectConstructor "L" "G`1") int32Atom), + (instanceOf (.external none), .apply (.constructor "L" "G`1") int32Atom), + (instanceOf (.external none), .apply (.objectConstructor "L" "G`1") int32Atom), + (instanceOf .classType, .atom "L" "G`1"), + -- Function pointers. + (.functionPointer 0 (.primitive 8) (.cons (.primitive 8) .nil), + .functionPointer int32Atom (.signatureParameter int32Atom .void) 1 false false), + (.functionPointer 0x20 (.primitive 8) .nil, .functionPointer int32Atom .void 0 false false), + (.functionPointer 0 (.primitive 8) .nil, int32Atom), + -- Modified types. + (.modified true (namedIn "M" "Mod" (.external none)) (.primitive 8), + .atom "B" "System.Int32 modreq(Mod)"), + (.modified true (namedIn "M" "Mod" (.external none)) (.primitive 8), + .atom "A" "System.Int32 modopt(Mod)"), + (.modified true (namedIn "M" "Mod" (.external none)) (.primitive 9), + .atom "A" "System.Int32 modreq(Mod)"), + (.modified true (namedIn "M" "Mod" (.external none)) (.primitive 8), + .object "A" "System.Int32 modreq(Mod)")] + +/-- Every listed pairing is accepted. -/ +theorem admits_each_accepted_pairing : + acceptedPairings.all (fun (type, expr) => pairFrame.admits type expr) = true := by + decide +kernel +#print axioms admits_each_accepted_pairing + +/-- Every listed pairing is refused. -/ +theorem refuses_each_refused_pairing : + refusedPairings.all (fun (type, expr) => !pairFrame.admits type expr) = true := by + decide +kernel +#print axioms refuses_each_refused_pairing + +/-- A method parameter past the method's arity is refused, so it cannot be read as the owner + parameter whose extractor index it shares. -/ +theorem refuses_a_method_parameter_past_the_arity : + !pairFrame.admits (.mvar 1) (.parameter 1) && pairFrame.admits (.var 0) (.parameter 1) && + pairFrame.admits (.mvar 0) (.parameter 0) := by + decide +kernel +#print axioms refuses_a_method_parameter_past_the_arity + +/-! ## Facts read from a type's own reference -/ + +/-- `T` with `System.ValueTuple` and references to its fields `Item1` and + `Item2` whose field signatures end with the type bytes `first` and `second`. -/ +private def tupleWorldWith (first second : List Nat) : Step (Nat × Nat) := do + let _ ← ownWorld + let tupleName ← string "ValueTuple`2" + let systemName ← string "System" + let firstName ← string "Item1" + let secondName ← string "Item2" + let tupleRef ← row Table.typeRef [1 * 4 + 2, tupleName, systemName] + let tupleBlob ← blob [0x15, 0x11, typeRefCoded tupleRef, 2, 0x08, 0x08] + let tupleSpec ← row Table.typeSpec [tupleBlob] + let firstSignature ← blob (0x06 :: first) + let secondSignature ← blob (0x06 :: second) + let parent := (tupleSpec - 0x1B000000) * 8 + 4 + let firstRef ← row Table.memberRef [parent, firstName, firstSignature] + let secondRef ← row Table.memberRef [parent, secondName, secondSignature] + pure (firstRef, secondRef) + +/-- The tuple world with `Item1 : !0` and `Item2 : !1`. -/ +private def tupleWorld : Step (Nat × Nat) := tupleWorldWith [0x13, 0x00] [0x13, 0x01] + +/-- The tuple world's slice with the given core-library number, and its field tokens. -/ +private def tupleSlice (corlib : Nat) : MetadataSlice × Nat × Nat := + let (slice, fields) := build tupleWorld (bytesOf "T") ownIdentity + ({ slice with corlib := corlib }, fields) + +/-- The pair as the extractor names it. -/ +private def pairTuple : TypeExpr := + .aggregate (s!"{runtimeScope}::System.ValueTuple`2<{runtimeScope}::System.Int32,{runtimeScope}::System.Int32>") 0 + +/-- The pair's instance field at `index` with `name`. -/ +private def pairField (index : Nat) (name : MetadataName) : RawOperand := + .field pairTuple index int32 name false + +/-- The pair's reference determines its field indices even without a core-library number. + Checking the held primitive at the correct index also requires its scope. -/ +theorem reads_the_pair_from_its_own_reference : + passes (tupleSlice 1).1 lib (tupleSlice 1).2.2 (pairField 1 name!"Item2") = true ∧ + rejects (tupleSlice 0).1 lib (tupleSlice 0).2.2 (pairField 1 name!"Item2") + (.operandMismatch (tupleSlice 0).2.2) = true ∧ + rejects (tupleSlice 0).1 lib (tupleSlice 0).2.1 (pairField 0 name!"Item1") + (.operandMismatch (tupleSlice 0).2.1) = true ∧ + rejects (tupleSlice 1).1 lib (tupleSlice 1).2.2 (pairField 0 name!"Item2") + (.fieldIndexMismatch (tupleSlice 1).2.2 0 1) = true ∧ + rejects (tupleSlice 0).1 lib (tupleSlice 0).2.2 (pairField 0 name!"Item2") + (.fieldIndexMismatch (tupleSlice 0).2.2 0 1) = true := by decide +kernel +#print axioms reads_the_pair_from_its_own_reference + +/-- The tuple world with `Item1` declared `int64`, and its field tokens. -/ +private def wideTuple : MetadataSlice × Nat × Nat := + build (tupleWorldWith [0x0A] [0x13, 0x01]) (bytesOf "T") ownIdentity + +/-- The tuple world with `Item1` declared `!1`, and its field tokens. -/ +private def swappedTuple : MetadataSlice × Nat × Nat := + build (tupleWorldWith [0x13, 0x01] [0x13, 0x01]) (bytesOf "T") ownIdentity + +/-- With no carried row a reference's signature is the item's only declaration, so `Item1` + declared `int64` or `!1` is refused although the operand holds the pair's first argument. -/ +theorem refuses_a_pair_item_declared_otherwise : + rejects wideTuple.1 lib wideTuple.2.1 (pairField 0 name!"Item1") + (.operandMismatch wideTuple.2.1) = true ∧ + rejects swappedTuple.1 lib swappedTuple.2.1 (pairField 0 name!"Item1") + (.operandMismatch swappedTuple.2.1) = true := by decide +kernel +#print axioms refuses_a_pair_item_declared_otherwise + +/-- A `Lib` whose class `Lib.Outer` encloses `Leaf` twice, with namespace columns `A` and `B`, + and `Mid` twice, with columns `A` and `B`, each enclosing an `Inner` of its own. -/ +private def nestedLibWorld : Step Unit := do + let runtimeName ← string "System.Runtime" + let objectName ← string "Object" + let systemName ← string "System" + let moduleName ← string "" + let libName ← string "Lib" + let outerName ← string "Outer" + let leafName ← string "Leaf" + let midName ← string "Mid" + let innerName ← string "Inner" + let first ← string "A" + let second ← string "B" + let key ← blob runtimePublisher + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, key, runtimeName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let objectRef ← row Table.typeRef [1 * 4 + 2, objectName, systemName] + let base := (objectRef - 0x01000000) * 4 + 1 + let _ ← row Table.typeDef [0, moduleName, 0, 0, 1, 1] + let outer ← row Table.typeDef [1, outerName, libName, base, 1, 1] + -- Nested public types: `Leaf` and `Mid` in `Outer`, then an `Inner` in each `Mid`. + let types := [(leafName, first, outer), (leafName, second, outer), (midName, first, outer), + (midName, second, outer), (innerName, 0, 0x02000005), (innerName, 0, 0x02000006)] + for (name, namespace_, enclosing) in types do + let nested ← row Table.typeDef [2, name, namespace_, base, 1, 1] + let nesting ← row Table.nestedClass [nested - 0x02000000, enclosing - 0x02000000] + link linkNesting nested 1 nesting + +/-- The nested `Lib` slice. -/ +private def nestedLib : MetadataSlice := + (build nestedLibWorld (bytesOf "Lib") libScope).1 + +/-- A modifier in this module is identified through its enclosing TypeDef rows. -/ +private def modifierClaim (name : String) : Bool := + let checked := do + let env := MetadataEnv.ofSlices [← own.1.index, ← nestedLib.index] + let marker ← modifierNamed env 1 (tokenOf Table.typeDef 7) + let frame := { pairFrame with rawIndices := true } + pure (frame.admits (.modified true marker (.primitive 1)) (.modified true libScope name .void)) + checked.toOption == some true + +example : modifierClaim "Inner" = false := by decide +kernel +example : modifierClaim "Lib.Outer/A.Mid/Inner" := by decide +kernel + +/-- `T` with references to `Outer/Leaf` and `Outer/Mid/Inner` through namespace `A` at each + nested level, linked to the definitions at the given `TypeDef` rows of `Lib`. -/ +private def nestedOwnWorld (leaf inner : Nat) : Step (Nat × Nat) := do + let runtimeName ← string "System.Runtime" + let libName ← string "Lib" + let outerName ← string "Outer" + let leafName ← string "Leaf" + let midName ← string "Mid" + let innerName ← string "Inner" + let first ← string "A" + let key ← blob runtimePublisher + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, key, runtimeName, 0, 0] + let _ ← row Table.assemblyRef [1, 0, 0, 0, 0, 0, libName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let outerRef ← row Table.typeRef [2 * 4 + 2, outerName, libName] + let enclosedBy (reference : Nat) := (reference - 0x01000000) * 4 + 3 + let leafRef ← row Table.typeRef [enclosedBy outerRef, leafName, first] + let midRef ← row Table.typeRef [enclosedBy outerRef, midName, first] + let innerRef ← row Table.typeRef [enclosedBy midRef, innerName, 0] + link linkDefinition leafRef 1 (tokenOf Table.typeDef leaf) + link linkDefinition innerRef 1 (tokenOf Table.typeDef inner) + pure (leafRef, innerRef) + +/-- The nested own slice with the given links, and its two reference tokens. -/ +private def nestedOwn (leaf inner : Nat) : MetadataSlice × Nat × Nat := + build (nestedOwnWorld leaf inner) (bytesOf "T") ownIdentity + +/-- A nested type is the type of its namespace column at every level: a reference through `A` + resolves to the definitions through `A`, and a link to the type that differs only in its own + column, or only in an enclosing type's, is refused though both spell `Lib.Outer/Leaf` or + `Lib.Outer/Mid/Inner`. -/ +theorem tells_nested_types_apart_by_their_namespace_columns : + passes (nestedOwn 3 7).1 nestedLib (nestedOwn 3 7).2.1 + (.type (.object libScope "Lib.Outer/Leaf")) = true ∧ + passes (nestedOwn 3 7).1 nestedLib (nestedOwn 3 7).2.2 + (.type (.object libScope "Lib.Outer/Mid/Inner")) = true ∧ + rejects (nestedOwn 4 7).1 nestedLib (nestedOwn 4 7).2.1 + (.type (.object libScope "Lib.Outer/Leaf")) (.operandMismatch (nestedOwn 4 7).2.1) = true ∧ + rejects (nestedOwn 3 8).1 nestedLib (nestedOwn 3 8).2.2 + (.type (.object libScope "Lib.Outer/Mid/Inner")) + (.operandMismatch (nestedOwn 3 8).2.2) = true := by decide +kernel +#print axioms tells_nested_types_apart_by_their_namespace_columns + +/-! ## Kinds against the extractor's constructors -/ + +/-- `System.Int32` of the runtime scope as a type of the given kind. -/ +private def runtimeInt32 (kind : Extraction.TypeKind) : ResolvedType := + .named (namedIn runtimeScope "System.Int32" kind) + +/-- A type that cannot be a value type is never admitted as the runtime's primitive atom, which + uses fixed value storage. An ordinary class keeps its atom. -/ +theorem refuses_a_class_named_as_a_primitive : + !pairFrame.admits (runtimeInt32 (.external (some false))) int32 && + !pairFrame.admits (runtimeInt32 .classType) int32 && + !pairFrame.admits (runtimeInt32 .interfaceType) int32 && + !pairFrame.admits (runtimeInt32 (.external none)) int32 && + pairFrame.admits (runtimeInt32 (.external (some true))) int32 && + pairFrame.admits (.named (namedIn runtimeScope "System.Console" .classType)) + (.atom runtimeScope "System.Console") := by + decide +kernel +#print axioms refuses_a_class_named_as_a_primitive + +/-- An owner parameter past the owner's arity is refused, even where its extractor index would + follow the method's parameters. -/ +theorem refuses_an_owner_parameter_past_the_owner_arity : + !pairFrame.admits (.var 1) (.parameter 2) && pairFrame.admits (.var 0) (.parameter 1) := by + decide +kernel +#print axioms refuses_an_owner_parameter_past_the_owner_arity + +/-! ## Each reason a reference or type is refused -/ + +/-- A type reference scoped by a module reference, which the checker does not read. -/ +private def consoleInModuleReference : MetadataSlice := + mutateRow own.1 tokens.consoleRef fun bytes => [5, 0] ++ bytes.drop 2 + +theorem refuses_a_type_reference_scoped_by_a_module_reference : + rejects consoleInModuleReference lib tokens.consoleRef + (.type (.atom runtimeScope "System.Console")) (.unsupportedScope tokens.consoleRef) = + true := by decide +kernel +#print axioms refuses_a_type_reference_scoped_by_a_module_reference + +/-- The links of `T` with the one from `Lib.Box`'s reference removed, or aimed at `T` itself. -/ +private def ownWithoutBoxLink : MetadataSlice := + { own.1 with links := own.1.links.filter fun link => link.kind != linkDefinition || + link.source != tokens.boxRef } + +private def ownWithBoxLinkIntoItself : MetadataSlice := + { own.1 with links := own.1.links.map fun link => + if link.kind == linkDefinition && link.source == tokens.boxRef then + { link with slice := 0 } else link } + +theorem refuses_a_reference_into_a_pinned_slice_without_its_definition_link : + rejects ownWithoutBoxLink lib tokens.boxRef (.type (.atom libScope "Lib.Box")) + (.unresolvedType tokens.boxRef) = true ∧ + rejects ownWithBoxLinkIntoItself lib tokens.boxRef (.type (.atom libScope "Lib.Box")) + (.unresolvedType tokens.boxRef) = true := by decide +kernel +#print axioms refuses_a_reference_into_a_pinned_slice_without_its_definition_link + +/-- `Program` extending a type specification, and extending `Console` instead of `Object`: the + chain of base classes ends only at `System.Object`. -/ +private def programExtends (coded : Nat) : MetadataSlice := + mutateRow own.1 (tokenOf Table.typeDef 2) fun bytes => + bytes.take 8 ++ [coded, 0] ++ bytes.drop 10 + +private def objectFieldOperand : RawOperand := + .field (.object ownIdentity "Program") 0 int32 name!"count" false + +theorem refuses_a_base_class_chain_that_does_not_end_at_object : + passes own.1 lib tokens.countField objectFieldOperand = true ∧ + rejects (programExtends 6) lib tokens.countField objectFieldOperand + (.unsupportedBase (tokenOf Table.typeDef 2)) = true ∧ + rejects (programExtends 9) lib tokens.countField objectFieldOperand + (.unsupportedBase (tokenOf Table.typeDef 2)) = true := by decide +kernel +#print axioms refuses_a_base_class_chain_that_does_not_end_at_object + +/-! ## Claims that name strings stored as bytes + +A claim may name a `byteString`, a string stored as its bytes, where it would write a literal. The +checks read the same value either way, so a claim naming another valid string is refused by the +check that refuses the wrong literal, with the same error. -/ + +/-- `System.Object`, stored as its bytes. -/ +private def objectText : String := byteString + [83, 121, 115, 116, 101, 109, 46, 79, 98, 106, 101, 99, 116] + "System.Object" (by cil_kernel_rfl) + +/-- `System.Console`, stored as its bytes. -/ +private def consoleText : String := byteString + [83, 121, 115, 116, 101, 109, 46, 67, 111, 110, 115, 111, 108, 101] + "System.Console" (by cil_kernel_rfl) + +/-- `Program`, stored as its bytes. -/ +private def programText : String := byteString + [80, 114, 111, 103, 114, 97, 109] + "Program" (by cil_kernel_rfl) + +/-- `Lib.Box`, stored as its bytes. -/ +private def boxText : String := byteString + [76, 105, 98, 46, 66, 111, 120] + "Lib.Box" (by cil_kernel_rfl) + +/-- The identity of `T`, stored as its bytes. -/ +private def ownText : String := byteString + [84, 44, 32, 86, 101, 114, 115, 105, 111, 110, 61, 49, 46, 48, 46, 48, 46, 48, 44, 32, 67, + 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, 98, + 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 110, 117, 108, 108] + "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" (by cil_kernel_rfl) + +/-- The runtime scope of version 9, stored as its bytes. -/ +private def olderRuntimeText : String := byteString + [83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 44, 32, 86, 101, 114, 115, + 105, 111, 110, 61, 57, 46, 48, 46, 48, 46, 48, 44, 32, 67, 117, 108, 116, 117, 114, 101, 61, + 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, 98, 108, 105, 99, 75, 101, 121, 84, 111, + 107, 101, 110, 61, 98, 48, 51, 102, 53, 102, 55, 102, 49, 49, 100, 53, 48, 97, 51, 97] + "System.Runtime, Version=9.0.0.0, Culture=neutral, \ + PublicKeyToken=b03f5f7f11d50a3a" (by cil_kernel_rfl) + +/-- `System.Int32`, stored as its bytes. -/ +private def int32Text : String := byteString + [83, 121, 115, 116, 101, 109, 46, 73, 110, 116, 51, 50] + "System.Int32" (by cil_kernel_rfl) + +/-- `System.UInt32`, stored as its bytes. -/ +private def uint32Text : String := byteString + [83, 121, 115, 116, 101, 109, 46, 85, 73, 110, 116, 51, 50] + "System.UInt32" (by cil_kernel_rfl) + +/-- The primitive assembly `C` of `pairFrame`, stored as its bytes. -/ +private def primitiveScopeText : String := byteString + [67] + "C" (by cil_kernel_rfl) + +/-- The owner of `mainMethod`, stored as its bytes. -/ +private def ownerText : String := byteString + [84, 44, 32, 86, 101, 114, 115, 105, 111, 110, 61, 49, 46, 48, 46, 48, 46, 48, 44, 32, 67, + 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, 98, + 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 110, 117, 108, 108, 58, 58, 80, 114, + 111, 103, 114, 97, 109] + "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null::Program" (by cil_kernel_rfl) + +/-- A `Program` owner in `Lib`, stored as its bytes. -/ +private def libOwnerText : String := byteString + [76, 105, 98, 44, 32, 86, 101, 114, 115, 105, 111, 110, 61, 49, 46, 48, 46, 48, 46, 48, 44, + 32, 67, 117, 108, 116, 117, 114, 101, 61, 110, 101, 117, 116, 114, 97, 108, 44, 32, 80, 117, + 98, 108, 105, 99, 75, 101, 121, 84, 111, 107, 101, 110, 61, 110, 117, 108, 108, 58, 58, 80, + 114, 111, 103, 114, 97, 109] + "Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null::Program" (by cil_kernel_rfl) + +/-- Each stored string is the literal it spells. -/ +theorem stored_strings_are_their_literals : + objectText = "System.Object" ∧ ownerText = ownIdentity ++ "::Program" ∧ + olderRuntimeText = "System.Runtime, Version=9.0.0.0, Culture=neutral, \ + PublicKeyToken=b03f5f7f11d50a3a" := + ⟨byteString_eq _, byteString_eq _, byteString_eq _⟩ +#print axioms stored_strings_are_their_literals + +/-- The type operand check accepts the stored name of the type and refuses another, as it + refuses the other literal. -/ +theorem type_operand_check_reads_stored_strings : + passes own.1 lib tokens.objectRef (.type (.atom runtimeScope objectText)) = true ∧ + rejects own.1 lib tokens.objectRef (.type (.atom runtimeScope consoleText)) + (.operandMismatch tokens.objectRef) = true ∧ + rejects own.1 lib tokens.objectRef (.type (.atom runtimeScope "System.Console")) + (.operandMismatch tokens.objectRef) = true := by decide +kernel +#print axioms type_operand_check_reads_stored_strings + +/-- The call operand check accepts the stored owner of the callee and refuses a stored owner + scope of another version, as it refuses that scope's literal. -/ +theorem call_operand_check_reads_stored_strings : + passes own.1 lib tokens.writeLine + (.call { writeLineSite with method.owner := .atom runtimeScope consoleText }) = true ∧ + rejects own.1 lib tokens.writeLine + (.call { writeLineSite with method.owner := .atom olderRuntimeText consoleText }) + (.scopeMismatch tokens.writeLine) = true := by decide +kernel +#print axioms call_operand_check_reads_stored_strings + +/-- The field operand check accepts the stored owner of the field and refuses another stored + owner, as it refuses the other literal. -/ +theorem field_operand_check_reads_stored_strings : + passes own.1 lib tokens.totalField + (.field (.atom ownText programText) 1 int32 name!"total" true) = true ∧ + rejects own.1 lib tokens.totalField (.field (.atom ownText boxText) 1 int32 name!"total" true) + (.operandMismatch tokens.totalField) = true ∧ + rejects own.1 lib tokens.totalField + (.field (.atom ownIdentity "Lib.Box") 1 int32 name!"total" true) + (.operandMismatch tokens.totalField) = true := by decide +kernel +#print axioms field_operand_check_reads_stored_strings + +/-- The method definition check refuses a stored return type the row does not give, as it + refuses the literal. -/ +theorem method_definition_check_reads_stored_strings : + identityFails own.1 { mainMethod with returnType := .atom runtimeScope int32Text } + (.methodMismatch tokens.main) = true ∧ + identityFails own.1 { mainMethod with returnType := .atom runtimeScope "System.Int32" } + (.methodMismatch tokens.main) = true := by decide +kernel +#print axioms method_definition_check_reads_stored_strings + +/-- `Frame.admits` names a primitive by its stored name and refuses another stored name, as it + refuses the other literal. -/ +theorem admits_reads_stored_strings : + pairFrame.admits (.primitive 8) (.atom primitiveScopeText int32Text) = true ∧ + pairFrame.admits (.primitive 8) (.atom primitiveScopeText uint32Text) = false ∧ + pairFrame.admits (.primitive 8) (.atom "C" "System.UInt32") = false := by decide +kernel +#print axioms admits_reads_stored_strings + +/-- The names the row of `mainMethod` gives. -/ +private def mainNames : MethodNames := + match identity own.1 mainMethod with + | .ok names => names + | .error _ => {} + +/-- The names spell a stored owner through a comparison of bytes, and that comparison and `Spell` + both refuse another stored owner, as `Spell` refuses the other literal. -/ +theorem spell_reads_stored_strings : mainNames.Spell { mainMethod with owner := ownerText } := + MethodNames.spell_of_spelledBy (by decide +kernel) +#print axioms spell_reads_stored_strings + +theorem spell_refuses_another_stored_owner : + mainNames.spelledBy { mainMethod with owner := libOwnerText } = false ∧ + !decide (mainNames.Spell { mainMethod with owner := libOwnerText }) = true ∧ + !decide (mainNames.Spell { mainMethod with owner := libScope ++ "::Program" }) = true := by + decide +kernel +#print axioms spell_refuses_another_stored_owner + +end Tests.MetadataBinding diff --git a/Tests/MetadataBuilder.lean b/Tests/MetadataBuilder.lean new file mode 100644 index 000000000..369d5623d --- /dev/null +++ b/Tests/MetadataBuilder.lean @@ -0,0 +1,190 @@ +import CIL.Extraction.MetadataBinding + +/-! +# Hand-built metadata for tests + +`Builder` assembles the rows, heap entries and links of a `MetadataSlice` the way an image +lays them out, with every index two bytes wide. `string`, `blob`, `userString` and `row` add +an entry and return its offset or token; `finish` adds the `Assembly` row and the table stream +header for the row counts so far. The builder serves tests only: nothing trusts it, because the +decoder re-reads every byte it writes. +-/ + +namespace Tests.MetadataBuilder + +open CIL CIL.Extraction + +/-- A type of `System.Runtime`'s `System` namespace as a platform definition of this kind. -/ +def runtimeType (name : String) (kind : PlatformKind) : PlatformDefinition := + { scope := runtimeScopeBytes, parts := [bytesOf "System", bytesOf name], kind } + +/-- The platform definition of `System.Object`, the base of a fixture's classes. -/ +def objectPlatform : PlatformTable := platform_table% [runtimeType "Object" .objectRoot] + +/-- The table with each definition changed or dropped by `edit`; an edit that keeps the name + parts keeps the table's keys right. -/ +def editPlatform (edit : PlatformDefinition → Option PlatformDefinition) + (table : PlatformTable) : PlatformTable := + { table with entries := visit table.entries } +where + /-- Keep every key and replace its associated definitions. -/ + visit : SearchTree (List PlatformDefinition) → SearchTree (List PlatformDefinition) + | .leaf => .leaf + | .node left key group right => .node (visit left) key (group.filterMap edit) (visit right) + +/-- The table without its definitions of types with simple name `name`. -/ +def removing (table : PlatformTable) (name : String) : PlatformTable := + editPlatform (fun entry => if entry.parts.getLastD [] == bytesOf name then none else some entry) + table + +/-- The table with its definitions of types with simple name `name` stating `kind`. -/ +def restating (table : PlatformTable) (name : String) (kind : PlatformKind) : PlatformTable := + editPlatform (fun entry => + some (if entry.parts.getLastD [] == bytesOf name then { entry with kind } else entry)) table + +/-- The entries added so far. -/ +structure Builder where + rows : List ByteEntry := [] + strings : List ByteEntry := [(0, [0])] + blobs : List ByteEntry := [(0, [0])] + userStrings : List ByteEntry := [] + stringEnd : Nat := 1 + blobEnd : Nat := 1 + userEnd : Nat := 1 + counts : List (Nat × Nat) := [] + links : List MetadataLink := [] + corlib : Nat := 0 + +/-- The column widths of an image whose tables are all small. -/ +def smallWidths : ColumnWidths := columnWidthsOf 0 fun _ => 0 + +/-- The `width` little-endian bytes of a number. -/ +def littleEndian : Nat → Nat → List Nat + | 0, _ => [] + | width + 1, value => value % 256 :: littleEndian width (value / 256) + +/-- The compressed encoding of a length below `0x4000`. -/ +def compressedLength (length : Nat) : List Nat := + if length < 0x80 then [length] else [0x80 + length / 256, length % 256] + +/-- The bytes of a row of a table with the given column values. -/ +def encodeRow (table : Nat) (columns : List Nat) : List Nat := + match columnWidths smallWidths table with + | some widths => (widths.zip columns).flatMap fun (width, value) => littleEndian width value + | none => [] + +/-- Add a `#Strings` entry of the given UTF-8 bytes and return its offset. Each operation takes + the builder apart once, so that elaborator reduction reads the previous state once per field + instead of once per use. -/ +def Builder.addString : Builder → List Nat → Builder × Nat + | ⟨rows, strings, blobs, userStrings, stringEnd, blobEnd, userEnd, counts, links, corlib⟩, + text => + let bytes := text ++ [0] + (⟨rows, strings ++ [(stringEnd, bytes)], blobs, userStrings, stringEnd + bytes.length, blobEnd, + userEnd, counts, links, corlib⟩, stringEnd) + +/-- Add a `#Blob` entry with the length prefix and return its offset. -/ +def Builder.addBlob : Builder → List Nat → Builder × Nat + | ⟨rows, strings, blobs, userStrings, stringEnd, blobEnd, userEnd, counts, links, corlib⟩, + payload => + let bytes := compressedLength payload.length ++ payload + (⟨rows, strings, blobs ++ [(blobEnd, bytes)], userStrings, stringEnd, blobEnd + bytes.length, + userEnd, counts, links, corlib⟩, blobEnd) + +/-- Add a `#US` entry with the length prefix and terminal byte, and return its offset. -/ +def Builder.addUserString : Builder → List Nat → Builder × Nat + | ⟨rows, strings, blobs, userStrings, stringEnd, blobEnd, userEnd, counts, links, corlib⟩, + units => + let payload := units.flatMap (littleEndian 2) ++ [if units.any userStringFlag then 1 else 0] + let bytes := compressedLength payload.length ++ payload + (⟨rows, strings, blobs, userStrings ++ [(userEnd, bytes)], stringEnd, blobEnd, + userEnd + bytes.length, counts, links, corlib⟩, userEnd) + +/-- Add a row of a table and return its token. -/ +def Builder.addRow : Builder → Nat → List Nat → Builder × Nat + | ⟨rows, strings, blobs, userStrings, stringEnd, blobEnd, userEnd, counts, links, corlib⟩, + table, columns => + let count := ((counts.lookup table).getD 0) + 1 + let token := tokenOf table count + (⟨rows ++ [(token, encodeRow table columns)], strings, blobs, userStrings, stringEnd, blobEnd, + userEnd, (counts.filter (·.1 != table)) ++ [(table, count)], links, corlib⟩, token) + +/-- Add a link. -/ +def Builder.addLink : Builder → Nat → Nat → Nat → Nat → Builder + | ⟨rows, strings, blobs, userStrings, stringEnd, blobEnd, userEnd, counts, links, corlib⟩, + kind, source, slice, target => + ⟨rows, strings, blobs, userStrings, stringEnd, blobEnd, userEnd, counts, + links ++ [⟨kind, source, slice, target⟩], corlib⟩ +/-- A builder step, so a world reads as a sequence of additions. -/ +abbrev Step := StateM Builder + +/-- A `#Strings` entry of the bytes of a text. -/ +def string (text : String) : Step Nat := + modifyGet fun builder => let (next, offset) := builder.addString (bytesOf text); (offset, next) + +/-- A `#Strings` entry of the given UTF-8 bytes. -/ +def stringBytes (text : List Nat) : Step Nat := + modifyGet fun builder => let (next, offset) := builder.addString text; (offset, next) + +/-- A `#Blob` entry. -/ +def blob (payload : List Nat) : Step Nat := + modifyGet fun builder => let (next, offset) := builder.addBlob payload; (offset, next) + +/-- A `#US` entry from the UTF-16 units of a text. -/ +def userString (units : List Nat) : Step Nat := + modifyGet fun builder => let (next, offset) := builder.addUserString units; (offset, next) + +/-- A row of a table. -/ +def row (table : Nat) (columns : List Nat) : Step Nat := + modifyGet fun builder => let (next, token) := builder.addRow table columns; (token, next) + +/-- A link from a token to a token of the slice at `slice`. -/ +def link (kind source slice target : Nat) : Step Unit := + modify fun builder => builder.addLink kind source slice target + +/-- The table stream header for row counts: version 2.0, two-byte heaps, the present tables + flagged in `Valid` and their row counts. -/ +def tableHeader (counts : List (Nat × Nat)) : List Nat := + let tables := (List.range 64).filter fun table => counts.any (·.1 == table) + let valid := tables.foldl (fun mask table => mask + 2 ^ table) 0 + [0, 0, 0, 0, 2, 0, 0, 1] ++ littleEndian 8 valid ++ littleEndian 8 0 ++ + (tables.flatMap fun table => littleEndian 4 ((counts.lookup table).getD 0)) + +/-- An assembly name and the pinned identity it spells, with one `Assembly` row. -/ +def assemblyBlock (builder : Builder) (name : List Nat) (identity : String) (version : Nat) : + Builder × AssemblyMetadata := + let (builder, nameOffset) := builder.addString name + let counts := builder.counts.filter (·.1 != Table.assembly) ++ [(Table.assembly, 1)] + let row := littleEndian 4 0x8004 ++ littleEndian 2 version ++ [0, 0, 0, 0, 0, 0] ++ + littleEndian 4 0 ++ littleEndian 2 0 ++ littleEndian 2 nameOffset ++ littleEndian 2 0 + (builder, { identity := identity, tableHeader := tableHeader counts, assemblyRow := row, + strings := [(0, [0]), (nameOffset, name ++ [0])], blobs := [(0, [0])] }) + +/-- Insert `item` before the first item whose key is not smaller, keeping equal keys in order. -/ +def insertByKey {α : Type} (key : α → Nat) (item : α) : List α → List α + | [] => [item] + | other :: rest => + if key item ≤ key other then item :: other :: rest else other :: insertByKey key item rest + +/-- The items in ascending key order, as the extractor carries rows and links. -/ +def sortByKey {α : Type} (key : α → Nat) (items : List α) : List α := + items.foldr (insertByKey key) [] + +/-- The slice a builder describes, with the `Assembly` row of the given name and identity. -/ +def finish (builder : Builder) (name : List Nat) (identity : String) (version : Nat) : + MetadataSlice := + let (builder, assembly) := assemblyBlock builder name identity version + { assembly := { assembly with + tableHeader := tableHeader (builder.counts.filter (·.1 != Table.assembly) ++ + [(Table.assembly, 1)]) }, + rows := sortByKey (·.1) builder.rows, strings := builder.strings, blobs := builder.blobs, + userStrings := builder.userStrings, corlib := builder.corlib, + links := sortByKey (fun link => link.kind * 0x100000000 + link.source) builder.links } + +/-- The slice that `step` builds, and what it returns. -/ +def build {α : Type} (step : Step α) (name : List Nat) (identity : String) (version : Nat := 1) : + MetadataSlice × α := + let (value, builder) := step.run {} + (finish builder name identity version, value) + +end Tests.MetadataBuilder diff --git a/Tests/MetadataDecoding.lean b/Tests/MetadataDecoding.lean new file mode 100644 index 000000000..9f50b4cde --- /dev/null +++ b/Tests/MetadataDecoding.lean @@ -0,0 +1,640 @@ +import CIL.Extraction.Signatures +import CIL.TypeExpr +import Lean + +/-! +# Metadata table and heap decoding tests + +The vectors are rows, header bytes and heap entries copied from `Examples.dll` (two-byte +columns) and from `System.Private.CoreLib.dll` (four-byte string, blob and coded columns), with +the major version of each assembly row set to the platform version 11, together with the values +`System.Reflection.Metadata` reads from the same positions and the assembly names Mono.Cecil +spells. Each rejection changes one fact of a slice that +decodes and asserts the exact error. +-/ + +namespace Tests.MetadataDecoding + +open CIL.Extraction + +/-! ## Vectors read from the images -/ + +def examplesHeader : List Nat := [0, 0, 0, 0, 2, 0, 0, 1, 87, 223, 163, 43, 9, 30, 0, 0, 0, 250, + 1, 51, 0, 22, 0, 0, 1, 0, 0, 0, 106, 0, 0, 0, 227, 0, 0, 0, 227, 0, 0, 0, 248, 4, 0, 0, 191, 6, + 0, 0, 43, 0, 0, 0, 37, 2, 0, 0, 26, 0, 0, 0, 50, 1, 0, 0, 1, 0, 0, 0, 31, 0, 0, 0, 13, 0, 0, 0, + 111, 0, 0, 0, 20, 0, 0, 0, 31, 0, 0, 0, 34, 0, 0, 0, 28, 0, 0, 0, 85, 0, 0, 0, 3, 0, 0, 0, 1, 0, + 0, 0, 4, 0, 0, 0, 27, 0, 0, 0, 76, 0, 0, 0, 60, 1, 0, 0, 88, 0, 0, 0] +def examplesRowSizes : List (Nat × Nat) := [(1, 6), (2, 14), (4, 6), (6, 14), (10, 6), (17, 2), + (27, 2), (32, 22), (35, 20), (41, 4), (42, 8), (43, 4)] +def examplesAssemblyRow : List Nat := [4, 128, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, + 52, 0, 0] +def examplesRows : List (Nat × List Nat) := [(16777217, [6, 0, 57, 28, 12, 51]), (16777218, [6, 0, + 235, 28, 12, 51]), (16777219, [6, 0, 177, 26, 92, 50]), (33554433, [0, 0, 0, 0, 228, 11, 0, 0, + 0, 0, 1, 0, 1, 0]), (33554434, [128, 1, 16, 0, 3, 44, 14, 46, 77, 0, 1, 0, 1, 0]), (33554435, + [128, 1, 16, 0, 3, 44, 232, 23, 77, 0, 1, 0, 2, 0]), (33554436, [128, 1, 16, 0, 37, 57, 9, 52, + 77, 0, 1, 0, 5, 0]), (67108865, [6, 0, 220, 55, 68, 18]), (67108866, [6, 0, 37, 52, 74, 18]), + (100663297, [80, 32, 0, 0, 0, 0, 150, 0, 111, 14, 52, 10, 1, 0]), (100663298, [85, 32, 0, 0, 0, + 0, 150, 0, 111, 14, 52, 10, 3, 0]), (100663299, [90, 32, 0, 0, 0, 0, 150, 0, 111, 14, 26, 10, 5, + 0]), (167772161, [9, 0, 6, 49, 1, 0]), (167772162, [17, 0, 6, 49, 6, 0]), (167772163, [25, 0, 6, + 49, 10, 0]), (285212673, [60, 1]), (452984833, [50, 0]), (587202561, [11, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 77, 17, 217, 23, 0, 0, 0, 0]), (587202562, [11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 86, + 17, 47, 50, 0, 0, 0, 0]), (687865857, [201, 0, 4, 0]), (704643073, [0, 0, 0, 0, 36, 0, 105, + 12]), (721420289, [83, 0, 255, 0])] +def examplesStrings : List (Nat × List Nat) := [(0, [0]), (3044, [60, 77, 111, 100, 117, 108, 101, + 62, 0]), (3177, [84, 0]), (3695, [65, 100, 100, 0]), (6105, [83, 121, 115, 116, 101, 109, 46, + 82, 117, 110, 116, 105, 109, 101, 0]), (6120, [83, 101, 108, 101, 99, 116, 111, 114, 69, 120, + 97, 109, 112, 108, 101, 115, 46, 79, 110, 101, 0]), (6833, [68, 101, 98, 117, 103, 103, 97, 98, + 108, 101, 65, 116, 116, 114, 105, 98, 117, 116, 101, 0]), (7225, [67, 111, 109, 112, 105, 108, + 97, 116, 105, 111, 110, 82, 101, 108, 97, 120, 97, 116, 105, 111, 110, 115, 65, 116, 116, 114, + 105, 98, 117, 116, 101, 0]), (7403, [82, 117, 110, 116, 105, 109, 101, 67, 111, 109, 112, 97, + 116, 105, 98, 105, 108, 105, 116, 121, 65, 116, 116, 114, 105, 98, 117, 116, 101, 0]), (11267, + [67, 111, 108, 108, 105, 115, 105, 111, 110, 0]), (11790, [83, 101, 108, 101, 99, 116, 111, 114, + 69, 120, 97, 109, 112, 108, 101, 115, 46, 84, 119, 111, 0]), (12550, [46, 99, 116, 111, 114, + 0]), (12847, [83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 46, 73, 110, + 116, 114, 105, 110, 115, 105, 99, 115, 0]), (12892, [83, 121, 115, 116, 101, 109, 46, 68, 105, + 97, 103, 110, 111, 115, 116, 105, 99, 115, 0]), (13068, [83, 121, 115, 116, 101, 109, 46, 82, + 117, 110, 116, 105, 109, 101, 46, 67, 111, 109, 112, 105, 108, 101, 114, 83, 101, 114, 118, 105, + 99, 101, 115, 0]), (13321, [69, 120, 97, 109, 112, 108, 101, 115, 0]), (13349, [76, 97, 110, + 101, 115, 0]), (14300, [80, 97, 105, 114, 115, 0]), (14629, [65, 103, 103, 114, 101, 103, 97, + 116, 101, 65, 114, 114, 97, 121, 115, 0])] +def examplesStringText : List (Nat × String) := [(0, ""), (3044, "\u003CModule\u003E"), (3177, + "T"), (3695, "Add"), (6105, "System.Runtime"), (6120, "SelectorExamples.One"), (6833, + "DebuggableAttribute"), (7225, "CompilationRelaxationsAttribute"), (7403, + "RuntimeCompatibilityAttribute"), (11267, "Collision"), (11790, "SelectorExamples.Two"), (12550, + ".ctor"), (12847, "System.Runtime.Intrinsics"), (12892, "System.Diagnostics"), (13068, + "System.Runtime.CompilerServices"), (13321, "Examples"), (13349, "Lanes"), (14300, "Pairs"), + (14629, "AggregateArrays")] +def examplesBlobs : List (Nat × List Nat) := [(0, [0]), (1, [4, 32, 1, 1, 8]), (6, [3, 32, 0, 1]), + (10, [5, 32, 1, 1, 17, 17]), (50, [5, 21, 18, 72, 1, 8]), (255, [5, 10, 1, 17, 131, 36]), (316, + [9, 7, 1, 21, 17, 89, 1, 17, 131, 36]), (2586, [5, 0, 2, 9, 9, 9]), (2612, [5, 0, 2, 11, 11, + 11]), (4429, [8, 176, 63, 95, 127, 17, 213, 10, 58]), (4438, [8, 204, 123, 19, 255, 205, 45, + 221, 81]), (4676, [5, 6, 29, 17, 131, 36]), (4682, [7, 6, 29, 21, 17, 97, 1, 11])] +def examplesBlobPayloads : List (Nat × List Nat) := [(0, []), (1, [32, 1, 1, 8]), (6, [32, 0, 1]), + (10, [32, 1, 1, 17, 17]), (50, [21, 18, 72, 1, 8]), (255, [10, 1, 17, 131, 36]), (316, [7, 1, + 21, 17, 89, 1, 17, 131, 36]), (2586, [0, 2, 9, 9, 9]), (2612, [0, 2, 11, 11, 11]), (4429, [176, + 63, 95, 127, 17, 213, 10, 58]), (4438, [204, 123, 19, 255, 205, 45, 221, 81]), (4676, [6, 29, + 17, 131, 36]), (4682, [6, 29, 21, 17, 97, 1, 11])] +def examplesExpected : List (Nat × List Nat) := [(16777217, [587202561, 7225, 13068]), (16777218, + [587202561, 7403, 13068]), (16777219, [587202561, 6833, 12892]), (67108865, [6, 14300, 4676]), + (67108866, [6, 13349, 4682]), (100663297, [8272, 0, 150, 3695, 2612]), (100663298, [8277, 0, + 150, 3695, 2612]), (100663299, [8282, 0, 150, 3695, 2586]), (167772161, [16777217, 12550, 1]), + (167772162, [16777218, 12550, 6]), (167772163, [16777219, 12550, 10]), (285212673, [316]), + (452984833, [50]), (587202561, [11, 0, 0, 0, 0, 4429, 6105, 0]), (587202562, [11, 0, 0, 0, 0, + 4438, 12847, 0]), (687865857, [33554633, 33554436]), (704643073, [0, 0, 33554450, 3177]), + (721420289, [167772201, 255])] +def examplesUserStrings : List (Nat × List Nat) := [(1, [11, 118, 0, 97, 0, 108, 0, 117, 0, 101, + 0, 0]), (13, [11, 99, 0, 111, 0, 117, 0, 110, 0, 116, 0, 0]), (25, [11, 105, 0, 110, 0, 100, 0, + 101, 0, 120, 0, 0]), (37, [11, 108, 0, 97, 0, 98, 0, 101, 0, 108, 0, 0]), (49, [17, 99, 0, 97, + 0, 112, 0, 97, 0, 99, 0, 105, 0, 116, 0, 121, 0, 0]), (67, [45, 84, 0, 104, 0, 101, 0, 32, 0, + 112, 0, 111, 0, 111, 0, 108, 0, 32, 0, 105, 0, 115, 0, 32, 0, 101, 0, 120, 0, 104, 0, 97, 0, + 117, 0, 115, 0, 116, 0, 101, 0, 100, 0, 46, 0, 0])] +def examplesUserUnits : List (Nat × List Nat) := [(1, [118, 97, 108, 117, 101]), (13, [99, 111, + 117, 110, 116]), (25, [105, 110, 100, 101, 120]), (37, [108, 97, 98, 101, 108]), (49, [99, 97, + 112, 97, 99, 105, 116, 121]), (67, [84, 104, 101, 32, 112, 111, 111, 108, 32, 105, 115, 32, 101, + 120, 104, 97, 117, 115, 116, 101, 100, 46])] +def examplesIdentity : String := "Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" +def examplesRefIdentities : List String := [ + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", + "System.Runtime.Intrinsics, Version=11.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51", + "System.Memory, Version=11.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51", + "System.Security.Cryptography, Version=11.0.0.0, Culture=neutral, " ++ + "PublicKeyToken=b03f5f7f11d50a3a"] + +def corelibHeader : List Nat := [0, 0, 0, 0, 2, 0, 5, 10, 85, 191, 183, 63, 1, 31, 0, 0, 0, 250, + 1, 51, 0, 22, 196, 0, 1, 0, 0, 0, 83, 11, 0, 0, 253, 37, 0, 0, 136, 165, 0, 0, 156, 11, 1, 0, + 132, 8, 0, 0, 147, 32, 0, 0, 166, 14, 0, 0, 75, 108, 0, 0, 1, 1, 0, 0, 164, 0, 0, 0, 143, 0, 0, + 0, 214, 14, 0, 0, 12, 0, 0, 0, 32, 0, 0, 0, 53, 4, 0, 0, 205, 21, 0, 0, 246, 23, 0, 0, 69, 16, + 0, 0, 15, 0, 0, 0, 237, 8, 0, 0, 140, 2, 0, 0, 193, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 249, 3, 0, + 0, 76, 16, 0, 0, 143, 19, 0, 0, 192, 7, 0, 0] +def corelibRowSizes : List (Nat × Nat) := [(1, 10), (2, 18), (4, 10), (6, 20), (10, 12), (17, 4), + (27, 4), (32, 28), (35, 28), (41, 4), (42, 12), (43, 8)] +def corelibAssemblyRow : List Nat := [4, 128, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, + 0, 130, 95, 1, 0, 0, 0, 0, 0] +def corelibRows : List (Nat × List Nat) := [(33554932, [0, 1, 0, 0, 102, 86, 7, 0, 128, 9, 5, 0, + 44, 2, 37, 7, 66, 38]), (33556432, [0, 1, 16, 0, 139, 153, 8, 0, 153, 155, 5, 0, 148, 2, 53, 23, + 137, 132]), (33556932, [8, 1, 16, 0, 79, 247, 2, 0, 204, 13, 4, 0, 60, 3, 231, 33, 37, 156]), + (67108869, [86, 128, 124, 193, 6, 0, 34, 24, 0, 0]), (67117864, [17, 0, 48, 78, 2, 0, 57, 228, + 5, 0]), (100663326, [0, 0, 0, 0, 128, 0, 147, 32, 184, 2, 4, 0, 3, 10, 0, 0, 81, 0, 0, 0]), + (100684296, [52, 93, 147, 0, 0, 1, 150, 0, 245, 180, 8, 0, 35, 55, 3, 0, 226, 125, 0, 0]), + (100705672, [168, 135, 157, 0, 0, 0, 147, 0, 125, 6, 4, 0, 246, 126, 4, 0, 156, 11, 1, 0]), + (167775160, [68, 27, 0, 0, 158, 136, 0, 0, 163, 0, 0, 0]), (167780499, [156, 70, 0, 0, 132, 210, + 6, 0, 100, 136, 1, 0]), (285214672, [223, 145, 2, 0]), (452984932, [171, 105, 0, 0]), + (452987117, [127, 33, 6, 0]), (687865906, [53, 0, 51, 0]), (704643172, [9, 0, 34, 0, 202, 1, 0, + 0, 189, 1, 0, 0]), (704647244, [1, 0, 0, 0, 17, 75, 1, 0, 140, 10, 8, 0]), (721420338, [98, 25, + 0, 0, 88, 48, 0, 0]), (721425295, [104, 247, 0, 0, 190, 84, 0, 0])] +def corelibStrings : List (Nat × List Nat) := [(0, [0]), (445, [84, 49, 48, 0]), (34974, [73, 116, + 101, 109, 49, 0]), (89986, [83, 121, 115, 116, 101, 109, 46, 80, 114, 105, 118, 97, 116, 101, + 46, 67, 111, 114, 101, 76, 105, 98, 0]), (151088, [115, 95, 105, 110, 115, 116, 97, 110, 99, + 101, 0]), (194383, [77, 97, 110, 105, 102, 101, 115, 116, 69, 110, 118, 101, 108, 111, 112, 101, + 0]), (262840, [73, 110, 100, 101, 120, 79, 102, 0]), (263805, [73, 110, 108, 105, 110, 101, 65, + 114, 114, 97, 121, 70, 105, 114, 115, 116, 69, 108, 101, 109, 101, 110, 116, 82, 101, 102, 0]), + (265676, [83, 121, 115, 116, 101, 109, 46, 68, 105, 97, 103, 110, 111, 115, 116, 105, 99, 115, + 46, 84, 114, 97, 99, 105, 110, 103, 0]), (330112, [83, 121, 115, 116, 101, 109, 0]), (367513, + [83, 121, 115, 116, 101, 109, 46, 82, 101, 102, 108, 101, 99, 116, 105, 111, 110, 0]), (442748, + [85, 110, 107, 110, 111, 119, 110, 69, 114, 114, 111, 114, 0]), (447108, [46, 99, 116, 111, 114, + 0]), (480870, [84, 105, 109, 101, 90, 111, 110, 101, 73, 110, 102, 111, 79, 112, 116, 105, 111, + 110, 115, 0]), (526988, [84, 69, 108, 101, 109, 101, 110, 116, 0]), (563595, [76, 111, 97, 100, + 101, 114, 65, 108, 108, 111, 99, 97, 116, 111, 114, 83, 99, 111, 117, 116, 0]), (570613, [78, + 97, 114, 114, 111, 119, 0])] +def corelibStringText : List (Nat × String) := [(0, ""), (445, "T10"), (34974, "Item1"), (89986, + "System.Private.CoreLib"), (151088, "s_instance"), (194383, "ManifestEnvelope"), (262840, + "IndexOf"), (263805, "InlineArrayFirstElementRef"), (265676, "System.Diagnostics.Tracing"), + (330112, "System"), (367513, "System.Reflection"), (442748, "UnknownError"), (447108, ".ctor"), + (480870, "TimeZoneInfoOptions"), (526988, "TElement"), (563595, "LoaderAllocatorScout"), + (570613, "Narrow")] +def corelibBlobs : List (Nat × List Nat) := [(1, [128, 160, 0, 36, 0, 0, 4, 128, 0, 0, 148, 0, 0, + 0, 6, 2, 0, 0, 0, 36, 0, 0, 82, 83, 65, 49, 0, 4, 0, 0, 1, 0, 1, 0, 141, 86, 199, 111, 158, 134, + 73, 56, 48, 73, 243, 131, 196, 75, 224, 236, 32, 65, 129, 130, 42, 108, 49, 207, 94, 183, 239, + 72, 105, 68, 208, 50, 24, 142, 161, 211, 146, 7, 99, 113, 44, 203, 18, 215, 95, 183, 126, 152, + 17, 20, 158, 97, 72, 229, 211, 47, 186, 171, 55, 97, 28, 24, 120, 221, 193, 158, 32, 239, 19, + 93, 12, 178, 207, 242, 191, 236, 61, 17, 88, 16, 195, 217, 6, 150, 56, 254, 75, 226, 21, 219, + 247, 149, 134, 25, 32, 229, 171, 111, 125, 178, 226, 206, 239, 19, 106, 194, 61, 93, 210, 191, + 3, 23, 0, 174, 194, 50, 246, 198, 177, 199, 133, 180, 48, 92, 18, 59, 55, 171]), (163, [3, 6, + 19, 0]), (2563, [15, 0, 7, 8, 24, 15, 3, 8, 15, 3, 8, 17, 138, 148, 15, 8]), (6178, [3, 6, 17, + 24]), (12376, [3, 10, 1, 8]), (21694, [4, 10, 1, 30, 1]), (27051, [6, 21, 17, 135, 0, 1, 11]), + (100452, [10, 32, 1, 1, 21, 18, 171, 44, 1, 19, 0]), (168415, [5, 7, 1, 17, 145, 92]), (210723, + [20, 0, 2, 21, 17, 150, 32, 1, 4, 21, 17, 150, 32, 1, 6, 21, 17, 150, 32, 1, 6]), (294646, [9, + 16, 2, 1, 16, 30, 1, 16, 30, 0]), (386105, [4, 6, 18, 168, 56]), (401791, [8, 21, 18, 150, 4, 2, + 30, 0, 11])] +def corelibBlobPayloads : List (Nat × List Nat) := [(1, [0, 36, 0, 0, 4, 128, 0, 0, 148, 0, 0, 0, + 6, 2, 0, 0, 0, 36, 0, 0, 82, 83, 65, 49, 0, 4, 0, 0, 1, 0, 1, 0, 141, 86, 199, 111, 158, 134, + 73, 56, 48, 73, 243, 131, 196, 75, 224, 236, 32, 65, 129, 130, 42, 108, 49, 207, 94, 183, 239, + 72, 105, 68, 208, 50, 24, 142, 161, 211, 146, 7, 99, 113, 44, 203, 18, 215, 95, 183, 126, 152, + 17, 20, 158, 97, 72, 229, 211, 47, 186, 171, 55, 97, 28, 24, 120, 221, 193, 158, 32, 239, 19, + 93, 12, 178, 207, 242, 191, 236, 61, 17, 88, 16, 195, 217, 6, 150, 56, 254, 75, 226, 21, 219, + 247, 149, 134, 25, 32, 229, 171, 111, 125, 178, 226, 206, 239, 19, 106, 194, 61, 93, 210, 191, + 3, 23, 0, 174, 194, 50, 246, 198, 177, 199, 133, 180, 48, 92, 18, 59, 55, 171]), (163, [6, 19, + 0]), (2563, [0, 7, 8, 24, 15, 3, 8, 15, 3, 8, 17, 138, 148, 15, 8]), (6178, [6, 17, 24]), + (12376, [10, 1, 8]), (21694, [10, 1, 30, 1]), (27051, [21, 17, 135, 0, 1, 11]), (100452, [32, 1, + 1, 21, 18, 171, 44, 1, 19, 0]), (168415, [7, 1, 17, 145, 92]), (210723, [0, 2, 21, 17, 150, 32, + 1, 4, 21, 17, 150, 32, 1, 6, 21, 17, 150, 32, 1, 6]), (294646, [16, 2, 1, 16, 30, 1, 16, 30, + 0]), (386105, [6, 18, 168, 56]), (401791, [21, 18, 150, 4, 2, 30, 0, 11])] +def corelibExpected : List (Nat × List Nat) := [(33556432, [1048832, 563595, 367513, 33554597, + 5941, 33929]), (67108869, [32854, 442748, 6178]), (67117864, [17, 151088, 386105]), (100663326, + [0, 128, 8339, 262840, 2563]), (100684296, [9657652, 256, 150, 570613, 210723]), (100705672, + [10323880, 0, 147, 263805, 294646]), (167775160, [452985704, 34974, 163]), (167780499, + [452987091, 447108, 100452]), (285214672, [168415]), (452984932, [27051]), (452987117, + [401791]), (687865906, [33554485, 33554483]), (704643172, [9, 34, 33554661, 445]), (704647244, + [1, 0, 100705672, 526988]), (721420338, [100666545, 12376]), (721425295, [100694964, 21694])] +def corelibUserStrings : List (Nat × List Nat) := [(1, [59, 123, 0, 123, 0, 32, 0, 84, 0, 104, 0, + 105, 0, 115, 0, 32, 0, 61, 0, 32, 0, 123, 0, 48, 0, 125, 0, 44, 0, 32, 0, 102, 0, 108, 0, 97, 0, + 103, 0, 115, 0, 32, 0, 61, 0, 32, 0, 123, 0, 49, 0, 125, 0, 32, 0, 125, 0, 125, 0, 0]), (61, + [35, 85, 0, 110, 0, 107, 0, 110, 0, 111, 0, 119, 0, 110, 0, 32, 0, 101, 0, 114, 0, 114, 0, 111, + 0, 114, 0, 32, 0, 40, 0, 48, 0, 120, 0, 0]), (97, [3, 120, 0, 0]), (101, [3, 41, 0, 0]), (105, + [23, 116, 0, 97, 0, 114, 0, 103, 0, 101, 0, 116, 0, 67, 0, 108, 0, 97, 0, 115, 0, 115, 0, 0]), + (129, [15, 99, 0, 117, 0, 108, 0, 116, 0, 117, 0, 114, 0, 101, 0, 0])] +def corelibUserUnits : List (Nat × List Nat) := [(1, [123, 123, 32, 84, 104, 105, 115, 32, 61, 32, + 123, 48, 125, 44, 32, 102, 108, 97, 103, 115, 32, 61, 32, 123, 49, 125, 32, 125, 125]), (61, + [85, 110, 107, 110, 111, 119, 110, 32, 101, 114, 114, 111, 114, 32, 40, 48, 120]), (97, [120]), + (101, [41]), (105, [116, 97, 114, 103, 101, 116, 67, 108, 97, 115, 115]), (129, [99, 117, 108, + 116, 117, 114, 101])] +def corelibIdentity : String := + "System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" +def corelibRefIdentities : List String := [] + +/-! ## Decoding -/ + +/-- Whether a decoding gives the value. -/ +private def gives {α : Type} [BEq α] (result : Except MetadataError α) (value : α) : Bool := + match result with + | .ok found => found == value + | .error _ => false + +/-- Whether a decoding fails with exactly this error. -/ +private def failsWith {α : Type} (result : Except MetadataError α) + (error : MetadataError) : Bool := + match result with + | .error found => found == error + | .ok _ => false + +/-- A slice cut from one of the images. -/ +private def sliceOf (identity : String) (header assemblyRow : List Nat) + (rows strings blobs userStrings : List ByteEntry) (keyToken : List Nat := []) : + MetadataSlice := + { assembly := { identity := identity, keyToken := keyToken, tableHeader := header, + assemblyRow := assemblyRow, strings := strings, blobs := blobs }, + rows := rows, strings := strings, blobs := blobs, userStrings := userStrings } + +private def examplesSlice : MetadataSlice := + sliceOf examplesIdentity examplesHeader examplesAssemblyRow examplesRows examplesStrings + examplesBlobs examplesUserStrings + +private def corelibSlice : MetadataSlice := + sliceOf corelibIdentity corelibHeader corelibAssemblyRow corelibRows corelibStrings + corelibBlobs [] (bytesOf "7cec85d7bea7798e") + +/-- The columns of a row as the typed accessors read them, with coded indices as tokens. -/ +private def typedColumns (index : MetadataIndex) (token : Nat) : + Except MetadataError (List Nat) := + match metadataTableOf token with + | 0x01 => do + let row ← index.typeRef token + .ok [row.scope, row.name, row.namespace_] + | 0x02 => do + let row ← index.typeDef token + .ok [row.flags, row.name, row.namespace_, row.extends_, row.fieldList, row.methodList] + | 0x04 => do + let row ← index.field token + .ok [row.flags, row.name, row.signature] + | 0x06 => do + let row ← index.methodDef token + .ok [row.rva, row.implFlags, row.flags, row.name, row.signature] + | 0x0A => do + let row ← index.memberRef token + .ok [row.parent, row.name, row.signature] + | 0x11 => (fun signature => [signature]) <$> index.signatureOf Table.standAloneSig token + | 0x1B => (fun signature => [signature]) <$> index.signatureOf Table.typeSpec token + | 0x23 => do + let row ← index.assemblyRef token + .ok [row.major, row.minor, row.build, row.revision, row.flags, row.publicKeyOrToken, row.name, + row.culture] + | 0x29 => do + let row ← index.nestedClass token + .ok [row.nested, row.enclosing] + | 0x2A => do + let row ← index.genericParam token + .ok [row.number, row.flags, row.owner, row.name] + | 0x2B => do + let row ← index.methodSpec token + .ok [row.method, row.instantiation] + | _ => .error (.unsupportedTable token) + +/-- Every expected row decodes to the columns the reference reader gave, for the slice. -/ +private def rowsDecode (slice : MetadataSlice) (expected : List (Nat × List Nat)) : Bool := + match slice.index with + | .ok index => expected.all fun (token, columns) => gives (typedColumns index token) columns + | .error _ => false + +/-- The slice's `#Strings` entries read as the reference reader's strings. -/ +private def stringsDecode (slice : MetadataSlice) (expected : List (Nat × String)) : Bool := + match slice.index with + | .ok index => expected.all fun (offset, text) => gives (index.string offset) (bytesOf text) + | .error _ => false + +/-- The slice's `#Blob` entries read as the reference reader's blob payloads. -/ +private def blobsDecode (slice : MetadataSlice) (expected : List (Nat × List Nat)) : Bool := + match slice.index with + | .ok index => expected.all fun (offset, payload) => gives (index.blob offset) payload + | .error _ => false + +/-- The slice's `#US` entries read as the reference reader's UTF-16 units. -/ +private def userStringsDecode (slice : MetadataSlice) (expected : List (Nat × List Nat)) : Bool := + match slice.index with + | .ok index => expected.all fun (offset, units) => gives (index.userString offset) units + | .error _ => false + +/-- The `rowSize` of every table agrees with `GetTableRowSize` for the image's header. -/ +private def rowSizesAgree (header : List Nat) (sizes : List (Nat × Nat)) : Bool := + match decodeTableHeader header with + | .ok decoded => sizes.all fun (table, size) => rowSize decoded.widths table == some size + | .error _ => false + +theorem examples_row_sizes_agree : rowSizesAgree examplesHeader examplesRowSizes = true := by + decide +kernel +#print axioms examples_row_sizes_agree + +theorem corelib_row_sizes_agree : rowSizesAgree corelibHeader corelibRowSizes = true := by + decide +kernel +#print axioms corelib_row_sizes_agree + +theorem examples_rows_decode : rowsDecode examplesSlice examplesExpected = true := by + decide +kernel +#print axioms examples_rows_decode + +theorem corelib_rows_decode : rowsDecode corelibSlice corelibExpected = true := by + decide +kernel +#print axioms corelib_rows_decode + +theorem examples_strings_decode : stringsDecode examplesSlice examplesStringText = true := by + decide +kernel +#print axioms examples_strings_decode + +theorem corelib_strings_decode : stringsDecode corelibSlice corelibStringText = true := by + decide +kernel +#print axioms corelib_strings_decode + +theorem examples_blobs_decode : blobsDecode examplesSlice examplesBlobPayloads = true := by + decide +kernel +#print axioms examples_blobs_decode + +theorem corelib_blobs_decode : blobsDecode corelibSlice corelibBlobPayloads = true := by + decide +kernel +#print axioms corelib_blobs_decode + +theorem examples_user_strings_decode : + userStringsDecode examplesSlice examplesUserUnits = true := by decide +kernel +#print axioms examples_user_strings_decode + +/-- The identities of the `Assembly` row and the carried `AssemblyRef` rows are Cecil's names. -/ +theorem identities_match_cecil : + (match examplesSlice.index with + | .ok index => (List.range 2).all fun position => + gives (index.assemblyRefIdentity (tokenOf Table.assemblyRef (position + 1))) + (bytesOf (examplesRefIdentities.getD position "")) + | .error _ => false) = true ∧ + gives (examplesSlice.index.map (·.identity)) (bytesOf examplesIdentity) ∧ + gives (corelibSlice.index.map (·.identity)) (bytesOf corelibIdentity) := by + decide +kernel +#print axioms identities_match_cecil + +/-! ## Compressed integers and coded indices -/ + +theorem compressed_integers_decode : + decodeCompressed [0x03, 9] = some (3, [9]) ∧ + decodeCompressed [0x7F] = some (0x7F, []) ∧ + decodeCompressed [0x80, 0x80] = some (0x80, []) ∧ + decodeCompressed [0xAE, 0x57] = some (0x2E57, []) ∧ + decodeCompressed [0xBF, 0xFF] = some (0x3FFF, []) ∧ + decodeCompressed [0xC0, 0x00, 0x40, 0x00] = some (0x4000, []) ∧ + decodeCompressed [0xDF, 0xFF, 0xFF, 0xFF] = some (0x1FFFFFFF, []) ∧ + decodeCompressed [0xE0, 0, 0, 0] = none ∧ decodeCompressed [0x80] = none ∧ + decodeCompressed [0xC0, 0, 0] = none ∧ decodeCompressed [] = none := by decide +kernel +#print axioms compressed_integers_decode + +theorem coded_indices_decode : + gives (decodeCoded typeDefOrRefTags 2 (5 * 4 + 1)) (tokenOf Table.typeRef 5) ∧ + gives (decodeCoded typeDefOrRefTags 2 (7 * 4 + 2)) (tokenOf Table.typeSpec 7) ∧ + gives (decodeCoded resolutionScopeTags 2 (3 * 4 + 2)) (tokenOf Table.assemblyRef 3) ∧ + gives (decodeCoded resolutionScopeTags 2 (3 * 4 + 3)) (tokenOf Table.typeRef 3) ∧ + gives (decodeCoded memberRefParentTags 3 (9 * 8 + 3)) (tokenOf Table.methodDef 9) ∧ + gives (decodeCoded memberRefParentTags 3 (9 * 8 + 4)) (tokenOf Table.typeSpec 9) ∧ + gives (decodeCoded methodDefOrRefTags 1 (4 * 2 + 1)) (tokenOf Table.memberRef 4) ∧ + gives (decodeCoded typeOrMethodDefTags 1 (4 * 2 + 1)) (tokenOf Table.methodDef 4) := by + decide +kernel +#print axioms coded_indices_decode + +/-- Both parent and scope indices admit the last representable row and reject the next. -/ +theorem coded_rows_fit_token : + [(resolutionScopeTags, 2, 1), (memberRefParentTags, 3, 2)].all (fun (tags, bits, tag) => + let limit := 2 ^ 24 + let field := 2 ^ bits + let oversized := limit * field + tag + gives (decodeCoded tags bits ((limit - 1) * field + tag)) (tokenOf 0x1A (limit - 1)) && + [oversized, oversized + field].all (fun value => + failsWith (decodeCoded tags bits value) (.badCodedIndex value))) := by + decide +kernel +#print axioms coded_rows_fit_token + +/-- A four-byte compressed TypeRef index cannot carry into the TypeDef table. -/ +theorem signature_rows_fit_token : + failsWith (decodeTypeSpec [0x11, 0xC4, 0, 0, 5]) (.badCodedIndex 0x04000005) := by + decide +kernel +#print axioms signature_rows_fit_token + +/-- The nested and enclosing tokens from a NestedClass row with four-byte TypeDef indices. -/ +private def wideNestedClass (nested enclosing : List Nat) (typeDefs : Nat := 0xffffff) : + Except MetadataError (Nat × Nat) := do + let index ← examplesSlice.index + let rows ← indexEntries [(0x29000001, nested ++ enclosing)] + let header := { index.header with + widths.typeDef := 4 + rowCounts := [(Table.typeDef, typeDefs), (Table.nestedClass, 1)] } + let index := { index with rows, header } + let entry ← index.nestedClass 0x29000001 + return (entry.nested, entry.enclosing) + +/-- A type can occupy the final representable row. Neither index can carry into another table. -/ +theorem nested_class_rows_fit_token : + let oversized := [1, 0, 0, 1] + let zero := [0, 0, 0, 0] + gives (wideNestedClass [255, 255, 255, 0] [254, 255, 255, 0]) (0x02ffffff, 0x02fffffe) ∧ + failsWith (wideNestedClass oversized zero) (.rowOverflow Table.typeDef 0x1000001) ∧ + failsWith (wideNestedClass zero oversized) (.rowOverflow Table.typeDef 0x1000001) := by + decide +kernel +#print axioms nested_class_rows_fit_token + +/-- A nested type cannot be owned by a nil row, itself or a later type. -/ +theorem rejects_invalid_enclosing_type_rows : + let nested := [6, 0, 0, 0] + failsWith (wideNestedClass nested [0, 0, 0, 0]) (.invalidNesting 0x02000006 0x02000000) ∧ + failsWith (wideNestedClass nested nested) (.invalidNesting 0x02000006 0x02000006) ∧ + failsWith (wideNestedClass nested [7, 0, 0, 0]) (.invalidNesting 0x02000006 0x02000007) ∧ + failsWith (wideNestedClass nested [5, 0, 0, 0] 5) (.tokenOutOfScope 0x02000006) := by + decide +kernel +#print axioms rejects_invalid_enclosing_type_rows + +/-! ## Slices that must be refused -/ + +/-- A tag past the table list of a coded index. -/ +theorem rejects_a_coded_index_with_an_unused_tag : + failsWith (decodeCoded typeDefOrRefTags 2 3) (.badCodedIndex 3) = true := by decide +kernel +#print axioms rejects_a_coded_index_with_an_unused_tag + +/-- A row one byte short of the length its table's widths give. -/ +theorem rejects_a_row_of_the_wrong_length : + failsWith (({ examplesSlice with rows := [(0x01000001, [6, 0, 57, 28, 12])] }).index.bind + (·.typeRef 0x01000001)) (.rowLength 0x01000001 6 5) = true := by decide +kernel +#print axioms rejects_a_row_of_the_wrong_length + +/-- Two rows of one token. -/ +theorem rejects_a_duplicate_token : + failsWith ({ examplesSlice with rows := examplesRows ++ examplesRows.take 1 }.index.map + (fun _ => ())) (.duplicateEntry 0x01000001) = true := by decide +kernel +#print axioms rejects_a_duplicate_token + +/-- Two heap entries at one offset. -/ +theorem rejects_a_duplicate_heap_offset : + failsWith ({ examplesSlice with strings := [(0, [0]), (0, [0])] }.index.map + (fun _ => ())) (.duplicateEntry 0) = true := by decide +kernel +#print axioms rejects_a_duplicate_heap_offset + +/-- Row 0 is no row. -/ +theorem rejects_row_zero : + failsWith (examplesSlice.index.bind (·.row 0x01000000)) (.tokenOutOfScope 0x01000000) = + true := by decide +kernel +#print axioms rejects_row_zero + +/-- A row past the header's row count of the table. -/ +theorem rejects_a_row_past_the_table : + failsWith (examplesSlice.index.bind (·.row 0x011FFFFF)) (.tokenOutOfScope 0x011FFFFF) = + true := by decide +kernel +#print axioms rejects_a_row_past_the_table + +/-- A row the slice does not carry. -/ +theorem rejects_a_missing_row : + failsWith (examplesSlice.index.bind (·.row 0x01000004)) (.missingRow 0x01000004) = true := by + decide +kernel +#print axioms rejects_a_missing_row + +/-- A table the checker does not read (`Param`, table 8). -/ +theorem rejects_a_table_the_checker_does_not_read : + failsWith (examplesSlice.index.bind (·.row 0x08000001)) (.unsupportedTable 0x08000001) = + true := by decide +kernel +#print axioms rejects_a_table_the_checker_does_not_read + +/-- A heap offset with no entry. -/ +theorem rejects_a_missing_heap_entry : + failsWith (examplesSlice.index.bind (·.string 5)) (.missingHeap 0 5) = true ∧ + failsWith (examplesSlice.index.bind (·.blob 5)) (.missingHeap 1 5) = true ∧ + failsWith (examplesSlice.index.bind (·.userString 5)) (.missingHeap 2 5) = true := by + decide +kernel +#print axioms rejects_a_missing_heap_entry + +/-- A string with no terminating zero, and one with a zero inside. -/ +theorem rejects_malformed_strings : + failsWith (({ examplesSlice with strings := [(7, [65, 66])] }).index.bind (·.string 7)) + (.malformedHeap 0 7) = true ∧ + failsWith (({ examplesSlice with strings := [(7, [65, 0, 66, 0])] }).index.bind (·.string 7)) + (.malformedHeap 0 7) = true := by + decide +kernel +#print axioms rejects_malformed_strings + +/-- A blob whose length prefix is not its remaining length. -/ +theorem rejects_a_blob_with_the_wrong_length_prefix : + failsWith (({ examplesSlice with blobs := [(7, [3, 1, 2])] }).index.bind (·.blob 7)) + (.malformedHeap 1 7) = true := by decide +kernel +#print axioms rejects_a_blob_with_the_wrong_length_prefix + +/-- A user string whose terminal byte disagrees with its units: `"hi"` needs 0. -/ +theorem rejects_a_wrong_user_string_terminal_byte : + failsWith (({ examplesSlice with userStrings := [(7, [5, 104, 0, 105, 0, 1])] }).index.bind + (·.userString 7)) (.malformedUserString 7) = true ∧ + gives (({ examplesSlice with userStrings := [(7, [5, 104, 0, 105, 0, 0])] }).index.bind + (·.userString 7)) [104, 105] := by decide +kernel +#print axioms rejects_a_wrong_user_string_terminal_byte + +/-- A user string with an odd byte count. -/ +theorem rejects_a_user_string_of_odd_length : + failsWith (({ examplesSlice with userStrings := [(7, [4, 104, 0, 105, 0])] }).index.bind + (·.userString 7)) (.malformedUserString 7) = true := by decide +kernel +#print axioms rejects_a_user_string_of_odd_length + +/-- A header with the extra-data heap flag, and one marking an edit-and-continue table. -/ +theorem rejects_unsupported_table_streams : + failsWith (decodeTableHeader (examplesHeader.set 6 0x40) |>.map (fun _ => ())) + (.unsupportedTableStream 0x40) = true ∧ + failsWith (decodeTableHeader (examplesHeader.set 11 0x40) |>.map (fun _ => ())) + (.unsupportedTableStream 0x100) = true := by + decide +kernel +#print axioms rejects_unsupported_table_streams + +/-- A header cut short, and one with a byte that is not a byte. -/ +theorem rejects_malformed_headers : + failsWith (decodeTableHeader (examplesHeader.take 30) |>.map (fun _ => ())) + .malformedTableHeader = true ∧ + failsWith (decodeTableHeader (examplesHeader.set 0 256) |>.map (fun _ => ())) + .invalidByte = true ∧ + failsWith (decodeTableHeader (examplesHeader ++ [0]) |>.map (fun _ => ())) + .malformedTableHeader = true := by decide +kernel +#print axioms rejects_malformed_headers + +/-- A heap entry that is not a byte. -/ +theorem rejects_a_value_above_255 : + failsWith ({ examplesSlice with blobs := [(7, [1, 256])] }.index.map (fun _ => ())) + .invalidByte = true := by decide +kernel +#print axioms rejects_a_value_above_255 + +/-- The claimed version differs from the one the `Assembly` row spells. -/ +theorem rejects_a_changed_assembly_identity : + failsWith ({ examplesSlice with assembly := { examplesSlice.assembly with + identity := + "Examples, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" } }.index.map + (fun _ => ())) .identityMismatch = true := by decide +kernel +#print axioms rejects_a_changed_assembly_identity + +/-- A reference with a public key flag has a token only SHA-1 can give. -/ +theorem rejects_an_assembly_reference_with_a_full_key : + failsWith (({ examplesSlice with rows := [(0x23000001, + ((examplesRows.lookup 0x23000001).getD []).set 8 1)] }).index.bind + (·.assemblyRefIdentity 0x23000001)) + (.unsupportedAssemblyRef 0x23000001) = true := by decide +kernel +#print axioms rejects_an_assembly_reference_with_a_full_key + +/-- A rejected reference name of any tested length needs no constructed assembly identity. -/ +private def skipsLongName (length : Nat) : Bool := Id.run do + let long := List.replicate length 65 ++ [0] + let row := (examplesRows.lookup 0x23000001).getD [] + let rejected := fun name culture => + let strings := [(7, culture), (6105, name)] + let rows := [(0x23000001, row.set 16 7)] + gives (({ examplesSlice with strings, rows }.index.bind fun index => + index.assemblyRefName 0x23000001 runtimeAssembly)) none + return rejected long [0] && rejected (runtimeAssembly.name.bytes?.getD [] ++ [0]) long + +open Lean Meta Elab Command in +run_cmd do + let perByteBudget := 1850 + let mut previousSize := 0 + let mut previousCost := 0 + let mut exceeded := false + for length in [64, 256, 1024] do + let (statement, proof) ← liftTermElabM do + let exercise := mkApp (mkConst ``skipsLongName) (mkNatLit length) + let statement ← mkEq exercise (mkConst ``Bool.true) + pure (statement, ← mkEqRefl (mkConst ``Bool.true)) + let start ← IO.getNumHeartbeats + match Kernel.check (← getEnv) {} (.letE `checked statement proof (.bvar 0) false) with + | .error error => throwKernelException error + | .ok _ => pure () + let cost := (← IO.getNumHeartbeats) - start + logInfo m!"assembly name and culture: {length} bytes, {cost} kernel heartbeats" + if previousSize != 0 then + exceeded := exceeded || cost - previousCost > perByteBudget * (length - previousSize) + previousSize := length + previousCost := cost + if exceeded then throwError "assembly name and culture exceeded the per-byte heartbeat budget" + +/-! ## Signatures -/ + +/-- The signature blobs of `Examples.dll` and `System.Private.CoreLib.dll` decode to the types + their bytes spell by ECMA-335 II.23.2. -/ +theorem real_signatures_decode : + -- `void (int)` on an instance + gives (decodeMethodSig [32, 1, 1, 8]) ⟨0x20, 0, .primitive 1, [.primitive 8]⟩ ∧ + -- `uint (uint, uint)` as a static + gives (decodeMethodSig [0, 2, 9, 9, 9]) + ⟨0, 0, .primitive 9, [.primitive 9, .primitive 9]⟩ ∧ + -- an instance method taking a value type named by a `TypeRef` + gives (decodeMethodSig [32, 1, 1, 17, 17]) ⟨0x20, 0, .primitive 1, [.named true 0x01000004]⟩ ∧ + gives (decodeTypeSpec [21, 18, 72, 1, 8]) + (.genericInst false 0x02000012 (.cons (.primitive 8) .nil)) ∧ + gives (decodeLocalSig [7, 1, 21, 17, 89, 1, 17, 131, 36]) + [.genericInst true 0x01000016 (.cons (.named true 0x020000C9) .nil)] ∧ + gives (decodeFieldSig [6, 29, 17, 131, 36]) (.szarray (.named true 0x020000C9)) ∧ + gives (decodeMethodSpec [10, 1, 30, 1]) [.mvar 1] ∧ + gives (decodeTypeSpec [21, 17, 135, 0, 1, 11]) + (.genericInst true 0x020001C0 (.cons (.primitive 11) .nil)) ∧ + gives (decodeMethodSig [0, 7, 8, 24, 15, 3, 8, 15, 3, 8, 17, 138, 148, 15, 8]) + ⟨0, 0, .primitive 8, [.primitive 24, .pointer (.primitive 3), .primitive 8, + .pointer (.primitive 3), .primitive 8, .named true 0x020002A5, .pointer (.primitive 8)]⟩ + := by + decide +kernel +#print axioms real_signatures_decode + +/-- Modifiers wrap the type that follows them, function pointers carry their convention, and + generic methods state their arity. -/ +theorem modifiers_function_pointers_and_generic_arity_decode : + gives (decodeMethodSig [0x20, 1, 1, 0x1F, 13, 0x10, 9]) + ⟨0x20, 0, .primitive 1, [.modified true 0x01000003 (.byref (.primitive 9))]⟩ ∧ + gives (decodeMethodSig [0x30, 2, 2, 1, 0x13, 0, 0x1E, 1]) + ⟨0x30, 2, .primitive 1, [.var 0, .mvar 1]⟩ ∧ + gives (decodeFieldSig [6, 0x1B, 0, 1, 1, 8]) + (.functionPointer 0 (.primitive 1) (.cons (.primitive 8) .nil)) + ∧ gives (decodeMethodSig [0x20, 1, 0x1F, 13, 1, 8]) + ⟨0x20, 0, .modified true 0x01000003 (.primitive 1), [.primitive 8]⟩ := by + decide +kernel +#print axioms modifiers_function_pointers_and_generic_arity_decode + +/-- A general array, `typedref`, the vararg sentinel and a pinned local report their byte. -/ +theorem rejects_signature_forms_the_model_lacks : + failsWith (decodeMethodSig [0, 1, 1, 0x14, 8, 1, 3]) (.unsupportedSignature 3 0x14) = true ∧ + failsWith (decodeMethodSig [0, 1, 1, 0x16]) (.unsupportedSignature 3 0x16) = true ∧ + failsWith (decodeMethodSig [0, 1, 1, 0x41, 8]) (.unsupportedSignature 3 0x41) = true ∧ + failsWith (decodeLocalSig [7, 1, 0x45, 8]) (.unsupportedSignature 2 0x45) = true ∧ + failsWith (decodeMethodSig [0x05, 0, 1]) (.unsupportedSignature 0 0x05) = true ∧ + failsWith (decodeFieldSig [0x07, 8]) (.unsupportedSignature 0 0x07) = true ∧ + failsWith (decodeFieldSig [6, 0x1B, 0x10, 0, 1]) (.unsupportedSignature 2 0x10) = true := by + decide +kernel +#print axioms rejects_signature_forms_the_model_lacks + +/-- A byte after the last type, a blob that ends inside a type and a bad type token tag. -/ +theorem rejects_malformed_signatures : + failsWith (decodeMethodSig [0, 0, 1, 8]) (.unsupportedSignature 3 0x100) = true ∧ + failsWith (decodeMethodSig [0, 1, 1]) (.truncatedSignature 3) = true ∧ + failsWith (decodeFieldSig [6, 0x11]) (.truncatedSignature 2) = true ∧ + failsWith (decodeTypeSpec [0x11, 3]) (.badCodedIndex 3) = true ∧ + failsWith (decodeTypeSpec [0x15, 0x0E, 1, 1, 8]) (.unsupportedSignature 1 0x0E) = true := by + decide +kernel +#print axioms rejects_malformed_signatures + +end Tests.MetadataDecoding diff --git a/Tests/MetadataDifferential.cs b/Tests/MetadataDifferential.cs new file mode 100644 index 000000000..50ff6ce9f --- /dev/null +++ b/Tests/MetadataDifferential.cs @@ -0,0 +1,376 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include GeneratedEvidence.cs +#:include ../Extractor/PlatformDefinitions.cs +#:include ../Extractor/ByteLiterals.cs +#:package Mono.Cecil + +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Text; +using System.Text.Json; +using Mono.Cecil; +using Nethermind.LeanCIL; +using ModuleDefinition = Mono.Cecil.ModuleDefinition; +using TypeReference = Mono.Cecil.TypeReference; +using TypeSpecification = Mono.Cecil.TypeSpecification; + +// Differential check of the Lean metadata decoder against System.Reflection.Metadata and Mono.Cecil: every row of the +// reference, type, signature and instantiation tables of an assembly and the heap entries they name are written to +// Lean, together with the full name Cecil spells for each method reference. Lean must decode the rows to the same +// names, from the bytes alone. Member references whose owner or signature mentions a generic parameter are spelled by +// the extractor's own owner rule and are covered by the extraction suites, so they are counted and skipped here. +// The extractor refuses general arrays, so Lean must refuse every reference that names one. +// References decided by one kernel theorem, which bounds the memory of each decision. +const int ReferencesPerTheorem = 128; +// The cost per added reference may grow at most this much from half to all of one theorem's references. +const double AllowedIncrementRatio = 1.25; +var imports = new HashSet(StringComparer.Ordinal); +var exports = new HashSet(StringComparer.Ordinal); +foreach (var sidecar in args.Skip(1)) +{ + using var contract = JsonDocument.Parse(File.ReadAllText(sidecar)); + imports.Add(contract.RootElement.GetProperty("assemblyIdentity").GetString()!); + foreach (var export in contract.RootElement.GetProperty("exports").EnumerateArray()) + exports.Add(export.GetProperty("identity").GetString()!); +} +var assembly = args.Length > 0 ? args[0] : At("Examples/Assembly/bin/Examples.dll"); +var name = Path.GetFileNameWithoutExtension(assembly); +var output = At("artifacts/MetadataDifferential"); +Directory.CreateDirectory(output); +if (args.Length == 0) +{ + Run(["dotnet", "build", At("Tests/ContractImports/App/App.csproj"), "-c", "Release"]); + var fixture = At("Tests/ContractImports/App/bin/ImportFixture.App.dll"); + using var compiled = ModuleDefinition.ReadModule(fixture); + void CheckOwner(string image, Mono.Cecil.MethodDefinition method, string owner) + { + var destination = Path.Combine(output, owner, method.Name); + Run(["dotnet", ExtractorPath, "extract", image, method.FullName, destination]); + Run(["lake", "env", "lean", Path.Combine(destination, "Extracted.lean")], cwd: FixturePackage()); + Console.WriteLine($"""PASS extracted and checked {method.FullName}."""); + } + string[] owners = ["MetadataDerived", "MetadataRecursive", "Depth8", "Depth10", "Depth12", "Depth14"]; + foreach (var owner in owners) + foreach (var method in compiled.GetType($"""ImportFixture.{owner}""").Methods.Where(method => method.IsStatic)) + CheckOwner(fixture, method, owner); + + // C# emits methods in named types. A global method belongs to the module's first TypeDef. + var named = compiled.GetType("ImportFixture.MetadataDerived"); + var global = named.Methods.Single(method => method.Name == "Run"); + named.Methods.Remove(global); + compiled.Types[0].Methods.Add(global); + var globalImage = Path.Combine(output, Path.GetFileName(fixture)); + compiled.Write(globalImage); + CheckOwner(globalImage, global, "Module"); +} +using var pe = new PEReader(File.OpenRead(assembly)); +var reader = pe.GetMetadataReader(); +var meta = pe.GetMetadata().GetContent(); +using var module = ModuleDefinition.ReadModule(assembly); + +var rows = new SortedDictionary(); +var strings = new SortedDictionary(); +var blobs = new SortedDictionary(); +string Bytes(IEnumerable values) => "[" + string.Join(", ", values) + "]"; +string Entries(SortedDictionary entries) where TKey : notnull => + "[" + string.Join(", ", entries.Select(entry => $"({entry.Key}, {Bytes(entry.Value)})")) + "]"; +void Row(TableIndex table, int row) +{ + var size = reader.GetTableRowSize(table); + var at = reader.GetTableMetadataOffset(table) + (row - 1) * size; + rows[((int)table << 24) + row] = meta.AsSpan(at, size).ToArray(); +} +void Text(StringHandle handle) +{ + var offset = reader.GetHeapOffset(handle); + var start = reader.GetHeapMetadataOffset(HeapIndex.String) + offset; + var end = start; + while (meta[end] != 0) end++; + strings[offset] = meta.AsSpan(start, end - start + 1).ToArray(); +} +void Blob(BlobHandle handle) +{ + var offset = reader.GetHeapOffset(handle); + var start = reader.GetHeapMetadataOffset(HeapIndex.Blob) + offset; + var first = meta[start]; + var (header, length) = first < 0x80 ? (1, (int)first) + : first < 0xC0 ? (2, ((first & 0x3F) << 8) | meta[start + 1]) : + (4, ((first & 0x1F) << 24) | (meta[start + 1] << 16) | (meta[start + 2] << 8) | meta[start + 3]); + blobs[offset] = meta.AsSpan(start, header + length).ToArray(); +} + +foreach (var handle in reader.TypeReferences) +{ + Row(TableIndex.TypeRef, MetadataTokens.GetRowNumber(handle)); + var reference = reader.GetTypeReference(handle); + Text(reference.Name); + Text(reference.Namespace); +} +foreach (var handle in reader.AssemblyReferences) +{ + Row(TableIndex.AssemblyRef, MetadataTokens.GetRowNumber(handle)); + var reference = reader.GetAssemblyReference(handle); + Text(reference.Name); + Text(reference.Culture); + Blob(reference.PublicKeyOrToken); +} +for (var row = 1; row <= reader.GetTableRowCount(TableIndex.TypeSpec); row++) +{ + Row(TableIndex.TypeSpec, row); + Blob(reader.GetTypeSpecification(MetadataTokens.TypeSpecificationHandle(row)).Signature); +} +for (var row = 1; row <= reader.GetTableRowCount(TableIndex.MethodSpec); row++) +{ + Row(TableIndex.MethodSpec, row); + Blob(reader.GetMethodSpecification(MetadataTokens.MethodSpecificationHandle(row)).Signature); +} +// Lean resolves a type this assembly defines through its `TypeDef` row, a nested one through a link to its +// `NestedClass` row and an enum through its fields, so every definition is carried with those. +var links = new SortedSet<(int Kind, int Source, int Target)>(); +foreach (var handle in reader.TypeDefinitions) +{ + Row(TableIndex.TypeDef, MetadataTokens.GetRowNumber(handle)); + var definition = reader.GetTypeDefinition(handle); + Text(definition.Name); + Text(definition.Namespace); + if (definition.BaseType.Kind != HandleKind.TypeReference) continue; + var parent = reader.GetTypeReference((TypeReferenceHandle)definition.BaseType); + if (reader.GetString(parent.Namespace) != "System" || reader.GetString(parent.Name) != "Enum") continue; + foreach (var field in definition.GetFields()) + { + Row(TableIndex.Field, MetadataTokens.GetRowNumber(field)); + Blob(reader.GetFieldDefinition(field).Signature); + } +} +// Lean counts a definition's generic parameters from its run in the `GenericParam` table, so every row is carried. +for (var row = 1; row <= reader.GetTableRowCount(TableIndex.GenericParam); row++) +{ + Row(TableIndex.GenericParam, row); + Text(reader.GetGenericParameter(MetadataTokens.GenericParameterHandle(row)).Name); +} +var nestingWidth = reader.GetTableRowSize(TableIndex.NestedClass) / 2; +for (var row = 1; row <= reader.GetTableRowCount(TableIndex.NestedClass); row++) +{ + Row(TableIndex.NestedClass, row); + var bytes = rows[((int)TableIndex.NestedClass << 24) + row]; + var nested = nestingWidth == 2 ? BitConverter.ToUInt16(bytes, 0) : (int)BitConverter.ToUInt32(bytes, 0); + links.Add((2, ((int)TableIndex.TypeDef << 24) + nested, ((int)TableIndex.NestedClass << 24) + row)); +} +// A reference to a method this assembly defines is located by a link to its `MethodDef` row, which Lean checks +// against the owner, name and signature the reference spells. +void Defined(int token, MethodReference method) +{ + var declaring = method.DeclaringType is GenericInstanceType generic ? generic.ElementType : method.DeclaringType; + if (declaring.Scope != module || method.Resolve() is not { } resolved || resolved.Module != module) return; + var definitionToken = resolved.MetadataToken.ToInt32(); + var definitionHandle = (MethodDefinitionHandle)MetadataTokens.EntityHandle(definitionToken); + Row(TableIndex.MethodDef, MetadataTokens.GetRowNumber(definitionHandle)); + var row = reader.GetMethodDefinition(definitionHandle); + Text(row.Name); + Blob(row.Signature); + links.Add((0, token, definitionToken)); + links.Add((1, definitionToken, MetadataTokens.GetToken(row.GetDeclaringType()))); +} +// Whether a type is or contains an array of more than one dimension or with bounds. +static bool GeneralArray(TypeReference type) => type switch +{ + ArrayType array when !array.IsVector => true, + GenericInstanceType generic => GeneralArray(generic.ElementType) || generic.GenericArguments.Any(GeneralArray), + TypeSpecification specification => GeneralArray(specification.ElementType), + _ => false +}; +var importedReferences = new List<(int Token, string Identity)>(); +var expected = new List<(int Token, string Name)>(); +var refused = new List(); +var skipped = 0; +foreach (var handle in reader.MemberReferences) +{ + var member = reader.GetMemberReference(handle); + Row(TableIndex.MemberRef, MetadataTokens.GetRowNumber(handle)); + Text(member.Name); + Blob(member.Signature); + var token = MetadataTokens.GetToken(handle); + if (module.LookupToken(token) is not MethodReference method) continue; + var owner = method.DeclaringType; + if (GeneralArray(owner) || GeneralArray(method.ReturnType) || + method.Parameters.Any(parameter => GeneralArray(parameter.ParameterType))) + { + refused.Add(token); + continue; + } + // The extractor spells owners and signatures that mention generic parameters by position; only closed + // ones are compared. + if (owner.ContainsGenericParameter || method.ReturnType.ContainsGenericParameter || + method.Parameters.Any(parameter => parameter.ParameterType.ContainsGenericParameter) || + method.FullName.Contains("modreq", StringComparison.Ordinal) || + method.CallingConvention is MethodCallingConvention.VarArg) + { + skipped++; + continue; + } + Defined(token, method); + var scope = owner.Scope is AssemblyNameReference reference ? reference.FullName : module.Assembly.Name.FullName; + var identity = $"""{scope}::{method.FullName}"""; + if (imports.Contains(scope) && exports.Contains(identity)) + { + importedReferences.Add((token, identity)); + Console.WriteLine($"Comparing imported method {identity} at 0x{token:X8}"); + } + expected.Add((token, identity)); +} +// The tables above refer to each other through coded indices, so every table row that a row names is carried too. +var core = (module.TypeSystem.CoreLibrary as AssemblyNameReference)!; +var corlib = module.AssemblyReferences.ToList().FindIndex(reference => reference.FullName == core.FullName) + 1; +var header = DifferentialHeader(reader, meta); +var assemblyDefinition = reader.GetAssemblyDefinition(); +Text(assemblyDefinition.Name); +Text(assemblyDefinition.Culture); +Blob(assemblyDefinition.PublicKey); +var assemblyRowAt = reader.GetTableMetadataOffset(TableIndex.Assembly); +var assemblyRow = meta.AsSpan(assemblyRowAt, reader.GetTableRowSize(TableIndex.Assembly)).ToArray(); +var identityText = JsonSerializer.Serialize(module.Assembly.Name.FullName); +// The runtime directory supplies platform identities; the caller identifies contract libraries separately. +var runtimeDirectory = Path.GetDirectoryName(typeof(object).Assembly.Location); +var trusted = (string?)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") ?? + throw new InvalidOperationException("The runtime did not supply its trusted assembly paths."); +var platformScopes = trusted.Split(Path.PathSeparator) + .Where(path => string.Equals(Path.GetDirectoryName(path), runtimeDirectory, StringComparison.Ordinal)) + .Select(path => System.Reflection.AssemblyName.GetAssemblyName(path).FullName) + .ToHashSet(StringComparer.Ordinal); +var coreLibrary = module.TypeSystem.Object.Resolve()?.Module ?? + throw new InvalidDataException("System.Object does not resolve for the platform definitions."); +var platform = new PlatformDefinitions(coreLibrary); +platform.Add(module, reader, reader.TypeReferences, + scope => !imports.Contains(scope.FullName) && platformScopes.Contains(scope.FullName)); +var linkList = string.Join(", ", links.Select(link => $"⟨{link.Kind}, {(uint)link.Source}, 0, {link.Target}⟩")); +// Each compared reference with the UTF-8 bytes of Cecil's name, then each general-array reference with none. The +// names are written as bytes because encoding a string literal in the kernel costs more than reading the reference. +var checks = expected.Select(entry => (entry.Token, Name: (string?)entry.Name)) + .Concat(refused.Select(token => (Token: token, Name: (string?)null))).ToList(); +string Check((int Token, string? Name) entry) => + $"({(uint)entry.Token}, {(entry.Name is null ? "none" : "some " + Bytes(Encoding.UTF8.GetBytes(entry.Name)))})"; +string Checks(IEnumerable<(int Token, string? Name)> entries) => "[" + string.Join(", ", entries.Select(Check)) + "]"; +var resolver = "env.methodSite token"; +var declaration = ""; +if (importedReferences.Count != 0) +{ + var cases = importedReferences.Select(entry => + $"""| {entry.Token} => env.importMethodSite token frame {JsonSerializer.Serialize(entry.Identity)}"""); + resolver = "readSite env token"; + declaration = $""" + /-- Only fixture exports have an imported declaring name. -/ + def readSite (env : MetadataEnv) (token : Nat) : Except MetadataError MethodSite := do + let frame <- env.frame 0 0 + match token with + {string.Join("\n ", cases)} + | _ => env.methodSite token + """; +} +var prefix = $$""" + import CIL.Extraction.MetadataBinding + + namespace Differential + open CIL CIL.Extraction + + -- The literals are too deep for the code generator; only the kernel evaluates them. + noncomputable section + + def slice : MetadataSlice := + { assembly := + { identity := {{identityText}}, tableHeader := {{Bytes(header)}}, + assemblyRow := {{Bytes(assemblyRow)}}, strings := {{Entries(strings)}}, + blobs := {{Entries(blobs)}} }, + rows :={{Entries(rows)}}, strings := {{Entries(strings)}}, blobs := {{Entries(blobs)}}, corlib := {{corlib}}, + links := [{{linkList}}] } + + /-- Scoped definitions supplied by the runtime libraries referenced by this assembly. -/ + def platform : PlatformTable := platform_table% {{platform.Lean(ByteLiterals.Of)}} + + {{declaration}} + + /-- The references among `entries` that Lean reads otherwise than Cecil: one Lean spells otherwise + than the bytes of Cecil's `FullName` or cannot read, or one to a general array (no name) that + Lean reads. -/ + def disagreements (entries : List (Nat × Option (List Nat))) : List Nat := + match slice.index with + | .error _ => [0] + | .ok index => + let env := MetadataEnv.ofSlices [index] (platform := platform) + entries.filterMap fun (token, name) => + match {{resolver}}, name with + | .ok site, some name => if site.externalName == some name then none else some token + | .error _, some _ => some token + | .ok _, none => some token + | .error _, none => none + + """; +// One kernel decision per chunk, checked one at a time, bounds the memory of the run; each rebuilds the index. +var chunks = checks.Chunk(ReferencesPerTheorem).ToList(); +var theorems = chunks.Select((chunk, number) => $""" + theorem agrees{number} : disagreements {Checks(chunk)} = [] := by decide +kernel + #print axioms agrees{number} + + """); +var path = Path.Combine(output, name + ".lean"); +File.WriteAllText(path, (prefix + string.Concat(theorems) + "end\nend Differential\n").ReplaceLineEndings("\n")); +Console.WriteLine($""" + {name}: comparing {expected.Count} references ({importedReferences.Count} imported) in {chunks.Count} theorems. + Refusing {refused.Count} general arrays. Skipping {skipped} open or modified references. + """); +if (imports.Count != 0 && importedReferences.Count == 0) + throw new InvalidOperationException("The import fixture has no compared references to its contract library."); +Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); +Console.WriteLine($"PASS Lean agrees with Cecil on every compared method reference of {name}."); +if (imports.Count != 0) return; + +// The decision over nested samples of the compared references: each sample holds every other reference of the +// next, so the samples mix references alike, and the cost per added reference must not grow with the sample. +var sampleSizes = new[] { ReferencesPerTheorem / 4, ReferencesPerTheorem / 2, ReferencesPerTheorem }; +if (expected.Count < ReferencesPerTheorem) + throw new InvalidOperationException($"{name} has fewer than {ReferencesPerTheorem} references to sample."); +var costs = sampleSizes.Select(size => +{ + var stride = ReferencesPerTheorem / size; + var sample = expected.Take(ReferencesPerTheorem).Where((_, position) => position % stride == 0) + .Select(entry => (entry.Token, Name: (string?)entry.Name)); + return CountHeartbeats(Path.Combine(output, $"Sample{size}.lean"), prefix + $""" + theorem agrees : disagreements {Checks(sample)} = [] := by decide +kernel + end + end Differential + """).Heartbeats; +}).ToArray(); +var incrementRatio = (double)(costs[2] - costs[1]) / (sampleSizes[2] - sampleSizes[1]) / + ((double)(costs[1] - costs[0]) / (sampleSizes[1] - sampleSizes[0])); +Console.WriteLine(FormattableString.Invariant( + $"Deciding {string.Join("/", sampleSizes)} references costs {string.Join("/", costs)} heartbeats; ") + + FormattableString.Invariant($"the cost per added reference grows {incrementRatio:F2} times")); +if (incrementRatio > AllowedIncrementRatio) + throw new InvalidOperationException(FormattableString.Invariant( + $"The cost per added reference grows {incrementRatio:F2} times from {sampleSizes[1]} to ") + + FormattableString.Invariant($"{sampleSizes[2]} references; the limit is {AllowedIncrementRatio}.")); +Console.WriteLine(FormattableString.Invariant( + $"PASS the cost per added reference grows {incrementRatio:F2} times, within {AllowedIncrementRatio}")); + +if (args.Length == 0) +{ + var app = At("Tests/ContractImports/App/bin/ImportFixture.App.dll"); + var sidecar = At("Tests/ContractImports/Lib.leancil.contracts"); + Run(["dotnet", typeof(Program).Assembly.Location, app, sidecar], streamOutput: true); +} + +// The table stream header: the bytes from the reserved word through the last row count. +static byte[] DifferentialHeader(MetadataReader reader, System.Collections.Immutable.ImmutableArray meta) +{ + var tables = Enumerable.Range(0, 64).Where(table => reader.GetTableRowCount((TableIndex)table) > 0).ToArray(); + var length = 24 + 4 * tables.Length; + var start = tables.Min(table => reader.GetTableMetadataOffset((TableIndex)table)) - length; + return meta.AsSpan(start, length).ToArray(); +} diff --git a/Tests/MetadataFieldPrefixes.cs b/Tests/MetadataFieldPrefixes.cs new file mode 100644 index 000000000..7ee1b0a51 --- /dev/null +++ b/Tests/MetadataFieldPrefixes.cs @@ -0,0 +1,207 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:package Mono.Cecil +#:include ../Extractor/MetadataFieldPrefix.cs +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs + +using System.Reflection; +using Mono.Cecil; +using Nethermind.LeanCIL; +using FieldAttributes = Mono.Cecil.FieldAttributes; +using MethodAttributes = Mono.Cecil.MethodAttributes; +using TypeAttributes = Mono.Cecil.TypeAttributes; + +Run(["dotnet", "run", "--file", At("Tests/MetadataHeapOmissions.cs"), "-c", "Release"]); + +foreach (var count in new[] { 64, 512, 4096 }) +{ + uint[] fields = [.. Enumerable.Range(0, count).Select(index => 0x04000001u + (uint)index * 3)]; + foreach (var order in new[] { fields, fields.Reverse().ToArray(), + fields.OrderBy(token => token % 17).ToArray() }) + { + var visited = 0; + var disposed = false; + IEnumerable Enumerate() + { + try + { + foreach (var field in fields) + { + visited++; + yield return field; + } + } + finally { disposed = true; } + } + var prefix = new MetadataFieldPrefix(Enumerate()); + var carried = new List(); + var length = 0; + foreach (var token in order.Concat(order)) + { + prefix.Through(token, carried.Add); + length = Math.Max(length, (int)((token - fields[0]) / 3) + 1); + Require(visited == length && carried.Count == length, "A field prefix was traversed again."); + } + prefix.All(carried.Add); + prefix.All(carried.Add); + Require(disposed && visited == count && carried.SequenceEqual(fields), "Full field coverage differs."); + try + { + prefix.Through(0x04000002, carried.Add); + throw new InvalidOperationException("An absent field was accepted."); + } + catch (InvalidDataException) { } + } + Console.WriteLine($"PASS {count} fields: ascending, descending and shuffled requests visit each field once"); +} + +// The collector is internal; calling it directly isolates metadata evidence from admission and Lean builds. +var extractor = Assembly.LoadFrom(ExtractorPath); +const BindingFlags Internal = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; +var pinnedType = extractor.GetType("Nethermind.LeanCIL.PinnedAssemblies", throwOnError: true)!; +var sourceType = pinnedType.GetNestedType("Source", BindingFlags.NonPublic | BindingFlags.Public)!; +var sources = Array.CreateInstance(sourceType, 0); +using var assembly = AssemblyDefinition.CreateAssembly(new AssemblyNameDefinition("FieldPrefixes", new(1, 0)), + "FieldPrefixes", ModuleKind.Dll); +var module = assembly.MainModule; +var valueType = new TypeReference("System", "ValueType", module, module.TypeSystem.CoreLibrary); +var owner = new TypeDefinition("Fixture", "Cells", TypeAttributes.Public | TypeAttributes.Sealed, valueType); +module.Types.Add(owner); +foreach (var index in Enumerable.Range(0, 4)) + owner.Fields.Add(new("Field" + index, FieldAttributes.Public | + (index == 1 ? FieldAttributes.Static : 0), index == 3 ? module.TypeSystem.Int64 : module.TypeSystem.Int32)); +var entry = new TypeDefinition("Fixture", "Entry", TypeAttributes.Public, module.TypeSystem.Object); +module.Types.Add(entry); +var method = new MethodDefinition("Run", MethodAttributes.Public | MethodAttributes.Static, module.TypeSystem.Void); +method.Body.Instructions.Add(Mono.Cecil.Cil.Instruction.Create(Mono.Cecil.Cil.OpCodes.Ret)); +entry.Methods.Add(method); +var baseType = new TypeDefinition("Fixture", "Base", TypeAttributes.Public, module.TypeSystem.Object); +baseType.Fields.Add(new("BaseField", FieldAttributes.Public, module.TypeSystem.Int32)); +module.Types.Add(baseType); +var derived = new TypeDefinition("Fixture", "Derived", TypeAttributes.Public, baseType); +derived.Fields.Add(new("First", FieldAttributes.Public, module.TypeSystem.Int32)); +derived.Fields.Add(new("Last", FieldAttributes.Public, module.TypeSystem.Int64)); +module.Types.Add(derived); +var enumType = new TypeDefinition("Fixture", "Choice", TypeAttributes.Public | TypeAttributes.Sealed, + new TypeReference("System", "Enum", module, module.TypeSystem.CoreLibrary)); +enumType.Fields.Add(new("value__", FieldAttributes.Public | FieldAttributes.SpecialName | + FieldAttributes.RTSpecialName, module.TypeSystem.Int32)); +enumType.Fields.Add(new("Item", FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal, + enumType) { Constant = 0 }); +module.Types.Add(enumType); +using var stream = new MemoryStream(); +assembly.Write(stream); +using var pinned = (IDisposable)Activator.CreateInstance(pinnedType, Internal, null, + [stream.ToArray(), "fixture", sources, null], null)!; +var selected = (ModuleDefinition)pinnedType.GetProperty("Selected", Internal)!.GetValue(pinned)!; +var original = (ModuleDefinition)pinnedType.GetMethod("Original", Internal)!.Invoke(pinned, [selected])!; +var run = original.GetType("Fixture.Entry").Methods.Single(); +var cells = original.GetType("Fixture.Cells").Fields.Select(field => field.MetadataToken.ToUInt32()).ToArray(); +var baseField = original.GetType("Fixture.Base").Fields.Single().MetadataToken.ToUInt32(); +var derivedFields = original.GetType("Fixture.Derived").Fields + .Select(field => field.MetadataToken.ToUInt32()).ToArray(); +var choice = original.GetType("Fixture.Choice"); +var sliceType = extractor.GetType("Nethermind.LeanCIL.MetadataSlices", throwOnError: true)!; +var collector = Activator.CreateInstance(sliceType, pinned)!; +var collect = sliceType.GetMethod("Method", Internal)!; +(string Metadata, string Tables, int[] Assemblies) Collect(params uint[] tokens) => + ((string, string, int[]))collect.Invoke(collector, [run, tokens, false, Array.Empty()])!; + +uint[] FieldRows(string metadata) +{ + var start = metadata.IndexOf("rows := [", StringComparison.Ordinal) + "rows := [".Length; + var stop = metadata.IndexOf("], strings :=", start, StringComparison.Ordinal); + return [.. FieldRowPattern().Matches(metadata[start..stop]) + .Select(match => uint.Parse(match.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture)) + .Where(token => token >> 24 == 4)]; +} + +var partial = Collect(cells[2]); +Require(FieldRows(partial.Metadata).SequenceEqual(cells[..3]), "Partial field evidence includes the wrong rows."); +// The extractor's own byte literal, so a carried entry matches whatever form the extractor writes it in. +var bytesLiteral = extractor.GetType("Nethermind.LeanCIL.ByteLiterals", throwOnError: true)! + .GetMethod("Of", BindingFlags.Static | BindingFlags.NonPublic)!; +string Bytes(byte[] values) => (string)bytesLiteral.Invoke(null, [values])!; +bool HasName(string metadata, string name) => + metadata.Contains(Bytes([.. System.Text.Encoding.UTF8.GetBytes(name), 0]), StringComparison.Ordinal); +Require(HasName(partial.Metadata, "Field2") && !HasName(partial.Metadata, "Field0") && + !HasName(partial.Metadata, "Field1") && !HasName(partial.Metadata, "Field3"), + "Prefix-only field rows must not carry unrequested names."); +Require(partial.Metadata.Contains(Bytes([2, 6, 8]), StringComparison.Ordinal) && + !partial.Metadata.Contains(Bytes([2, 6, 10]), StringComparison.Ordinal), + "Prefix-only field rows must not carry unrequested signatures."); +var repeated = Collect(cells[2], cells[0], cells[2]); +Require(FieldRows(repeated.Metadata).SequenceEqual(cells[..3]), "Repeated fields extend the carried prefix."); +var forward = Collect(cells); +var reverse = Collect(cells.Reverse().ToArray()); +Require(forward.Metadata == reverse.Metadata && forward.Tables == reverse.Tables, + "Field order changes emitted rows, signatures, names or links."); +var inherited = Collect(derivedFields[0]); +Require(FieldRows(inherited.Metadata).SequenceEqual(new[] { baseField, derivedFields[0] }), + "A derived field must carry all base fields and only its own prefix."); +Require(!HasName(inherited.Metadata, "BaseField") && !HasName(inherited.Metadata, "Last"), + "Inherited row coverage must not carry unrequested names."); +var inheritedAll = Collect([.. derivedFields, baseField]); +var inheritedReverse = Collect([baseField, .. derivedFields.Reverse()]); +Require(inheritedAll.Metadata == inheritedReverse.Metadata && inheritedAll.Tables == inheritedReverse.Tables, + "Repeated ancestors change emitted evidence."); +var enumEvidence = Collect(choice.MetadataToken.ToUInt32()); +Require(FieldRows(enumEvidence.Metadata).SequenceEqual(choice.Fields.Select(field => field.MetadataToken.ToUInt32())), + "Enum evidence omits field rows."); +Require(enumEvidence.Metadata.Contains(Bytes([2, 6, 8]), StringComparison.Ordinal) && + enumEvidence.Metadata.Contains(Bytes([3, 6, 17, (byte)(choice.MetadataToken.RID << 2)]), + StringComparison.Ordinal) && !HasName(enumEvidence.Metadata, "value__") && + !HasName(enumEvidence.Metadata, "Item"), + "Enum field coverage must carry signatures without unrequested names."); +Console.WriteLine("PASS partial prefixes, repeated fields, inheritance, enums and order-independent evidence"); +var evidence = string.Join("\n", new[] { partial, repeated, forward, reverse, inherited, + inheritedAll, inheritedReverse, enumEvidence }.Select(item => item.Metadata + "\n" + item.Tables)); +Console.WriteLine("Evidence SHA256 " + Convert.ToHexString(System.Security.Cryptography.SHA256.HashData( + System.Text.Encoding.UTF8.GetBytes(evidence)))); + +foreach (var count in new[] { 256, 1024, 4096 }) +{ + using var input = AssemblyDefinition.CreateAssembly(new("FieldCount", new(1, 0)), "FieldCount", ModuleKind.Dll); + var inputModule = input.MainModule; + var inputType = new TypeDefinition("Fixture", "Fields", TypeAttributes.Public, + new TypeReference("System", "ValueType", inputModule, inputModule.TypeSystem.CoreLibrary)); + inputModule.Types.Add(inputType); + for (var index = 0; index < count; index++) + inputType.Fields.Add(new("Field" + index, FieldAttributes.Public, inputModule.TypeSystem.Int32)); + var inputMethod = new MethodDefinition("Run", MethodAttributes.Public | MethodAttributes.Static, + inputModule.TypeSystem.Void); + inputMethod.Body.Instructions.Add(Mono.Cecil.Cil.Instruction.Create(Mono.Cecil.Cil.OpCodes.Ret)); + inputType.Methods.Add(inputMethod); + using var image = new MemoryStream(); + input.Write(image); + using var inputPinned = (IDisposable)Activator.CreateInstance(pinnedType, Internal, null, + [image.ToArray(), "fixture", sources, null], null)!; + var inputSelected = (ModuleDefinition)pinnedType.GetProperty("Selected", Internal)!.GetValue(inputPinned)!; + var inputOriginal = (ModuleDefinition)pinnedType.GetMethod("Original", Internal)!.Invoke(inputPinned, + [inputSelected])!; + var fieldsType = inputOriginal.GetType("Fixture.Fields"); + var inputCollector = Activator.CreateInstance(sliceType, inputPinned)!; + uint[] tokens = [.. fieldsType.Fields.Select(field => field.MetadataToken.ToUInt32())]; + var watch = System.Diagnostics.Stopwatch.StartNew(); + var result = ((string Metadata, string Tables, int[] Assemblies))collect.Invoke(inputCollector, + [fieldsType.Methods.Single(), tokens, false, Array.Empty()])!; + watch.Stop(); + Require(FieldRows(result.Metadata).SequenceEqual(tokens), "Field collection lost a row."); + Console.WriteLine($"Collected {count} fields in {watch.Elapsed.TotalMilliseconds:F1} ms"); +} + +static void Require(bool condition, string message) +{ + if (!condition) throw new InvalidOperationException(message); +} + +partial class Program +{ + [System.Text.RegularExpressions.GeneratedRegex(@"\((\d+), \(CIL\.Extraction\.fixedBytes ")] + private static partial System.Text.RegularExpressions.Regex FieldRowPattern(); +} diff --git a/Tests/MetadataHeapOmissions.cs b/Tests/MetadataHeapOmissions.cs new file mode 100644 index 000000000..9208b4ec4 --- /dev/null +++ b/Tests/MetadataHeapOmissions.cs @@ -0,0 +1,229 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:package Mono.Cecil +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs + +using System.Reflection; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using Mono.Cecil; +using AssemblyDefinition = Mono.Cecil.AssemblyDefinition; +using CustomAttribute = Mono.Cecil.CustomAttribute; +using FieldAttributes = Mono.Cecil.FieldAttributes; +using MethodDefinition = Mono.Cecil.MethodDefinition; +using MethodAttributes = Mono.Cecil.MethodAttributes; +using ModuleDefinition = Mono.Cecil.ModuleDefinition; +using TypeDefinition = Mono.Cecil.TypeDefinition; +using TypeReference = Mono.Cecil.TypeReference; +using TypeAttributes = Mono.Cecil.TypeAttributes; + +var extractor = Assembly.LoadFrom(ExtractorPath); +const BindingFlags Internal = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; +var pinnedType = extractor.GetType("Nethermind.LeanCIL.PinnedAssemblies", throwOnError: true)!; +var sourceType = pinnedType.GetNestedType("Source", BindingFlags.Public | BindingFlags.NonPublic)!; +var collectionType = extractor.GetType("Nethermind.LeanCIL.MetadataSlices+Collection", throwOnError: true)!; +var sources = Array.CreateInstance(sourceType, 0); +var proofs = new System.Text.StringBuilder("import CIL.Extraction.MetadataBytes\nopen CIL.Extraction\n"); +var proofCount = 0; + +foreach (var wide in new[] { false, true }) + Check(Image(4, wide), wide ? 4 : 2); +Check(SmallWideImage(), 4); + +var slicesType = extractor.GetType("Nethermind.LeanCIL.MetadataSlices", throwOnError: true)!; +var requireRank = slicesType.GetMethod("RequireHeapRank", BindingFlags.Static | BindingFlags.NonPublic)!; +foreach (var width in new[] { 2, 4 }) +{ + var marker = width == 2 ? ushort.MaxValue : uint.MaxValue; + foreach (var heap in new[] { "#Strings", "#Blob" }) + { + requireRank.Invoke(null, [marker - 1, width, heap]); + try + { + requireRank.Invoke(null, [marker, width, heap]); + throw new InvalidOperationException("A reserved rank was accepted."); + } + catch (TargetInvocationException error) when (error.InnerException is InvalidDataException) { } + } +} +Console.WriteLine("PASS rank boundaries for both heaps and widths"); + +double previous = 0; +foreach (var count in new[] { 256, 1024, 4096 }) +{ + var image = Image(count, false); + var timings = Enumerable.Range(0, 7).Select(_ => Check(image, 2, false)).Order().ToArray(); + var elapsed = timings[0]; + Require(previous == 0 || elapsed <= previous * 8 + 2, + "Position-only row canonicalization grows faster than its row count allows."); + Console.WriteLine($"Canonicalized {count} position-only field rows in {elapsed:F3} ms minimum"); + previous = elapsed; +} + +var lean = At("artifacts/MetadataHeapOmissions.lean"); +File.WriteAllText(lean, proofs.ToString()); +Run(["lake", "build", "CIL.Extraction.MetadataBytes"]); +Console.WriteLine(Run(["lake", "env", "lean", lean])); + +byte[] SmallWideImage() +{ + var bytes = Image(4, true); + using var pe = new PEReader(new MemoryStream(bytes)); + var reader = pe.GetMetadataReader(); + var metadata = pe.PEHeaders.MetadataStartOffset; + var paddingType = reader.TypeDefinitions.Last(); + var paddingName = reader.GetHeapOffset(reader.GetTypeDefinition(paddingType).Name); + var attribute = reader.CustomAttributes.Single(); + var paddingBlob = reader.GetHeapOffset(reader.GetCustomAttribute(attribute).Value); + Require(paddingName < ushort.MaxValue && paddingBlob < ushort.MaxValue, + "Padding must follow all meaningful heap entries."); + var typeRow = metadata + reader.GetTableMetadataOffset(TableIndex.TypeDef) + + (MetadataTokens.GetRowNumber(paddingType) - 1) * reader.GetTableRowSize(TableIndex.TypeDef); + bytes.AsSpan(typeRow + 4, 4).Clear(); + var attributeRow = metadata + reader.GetTableMetadataOffset(TableIndex.CustomAttribute); + bytes.AsSpan(attributeRow + reader.GetTableRowSize(TableIndex.CustomAttribute) - 4, 4).Clear(); + var versionLength = BitConverter.ToInt32(bytes, metadata + 12); + var at = metadata + 16 + versionLength; + var streams = BitConverter.ToUInt16(bytes, at + 2); + at += 4; + for (var index = 0; index < streams; index++) + { + var end = Array.IndexOf(bytes, (byte)0, at + 8); + var name = System.Text.Encoding.ASCII.GetString(bytes, at + 8, end - at - 8); + var length = name switch { "#Strings" => paddingName, "#Blob" => paddingBlob, _ => -1 }; + if (length >= 0) BitConverter.GetBytes(length).CopyTo(bytes, at + 4); + at = (end + 4) & ~3; + } + using var narrowed = new PEReader(new MemoryStream(bytes)); + var small = narrowed.GetMetadataReader(); + Require(small.GetHeapSize(HeapIndex.String) < ushort.MaxValue && + small.GetHeapSize(HeapIndex.Blob) < ushort.MaxValue, "Both declared heaps must be small."); + Require(small.GetTableRowSize(TableIndex.Field) == 10, "Small heap indexes must remain four bytes."); + foreach (var field in small.FieldDefinitions) + { + Require(small.GetString(small.GetFieldDefinition(field).Name) == + reader.GetString(reader.GetFieldDefinition(field).Name), "Shrinking changed a field name."); + Require(small.GetBlobBytes(small.GetFieldDefinition(field).Signature).SequenceEqual( + reader.GetBlobBytes(reader.GetFieldDefinition(field).Signature)), "Shrinking changed a signature."); + } + Console.WriteLine("PASS SRM small heaps retain wide rows and exact field names/signatures"); + return bytes; +} + +byte[] Image(int count, bool wide) +{ + using var assembly = AssemblyDefinition.CreateAssembly(new("OmittedHeaps", new(1, 0)), + "OmittedHeaps", ModuleKind.Dll); + var module = assembly.MainModule; + ((AssemblyNameReference)module.TypeSystem.CoreLibrary).PublicKeyToken = []; + var owner = new TypeDefinition("", "Fields", TypeAttributes.Public | TypeAttributes.Sealed, + new TypeReference("System", "ValueType", module, module.TypeSystem.CoreLibrary)); + module.Types.Add(owner); + for (var index = 0; index < count; index++) + owner.Fields.Add(new("Field" + index, FieldAttributes.Public, + index == count - 1 ? module.TypeSystem.Int32 : module.TypeSystem.Int64)); + var method = new MethodDefinition("Run", MethodAttributes.Public | MethodAttributes.Static, + module.TypeSystem.Void); + method.Body.Instructions.Add(Mono.Cecil.Cil.Instruction.Create(Mono.Cecil.Cil.OpCodes.Ret)); + owner.Methods.Add(method); + if (wide) + { + module.Types.Add(new("", new string('z', 70000), TypeAttributes.Public, module.TypeSystem.Object)); + var attribute = new TypeReference("Fixture", "PaddingAttribute", module, module.TypeSystem.CoreLibrary); + var constructor = new MethodReference(".ctor", module.TypeSystem.Void, attribute) { HasThis = true }; + constructor.Parameters.Add(new(module.TypeSystem.String)); + var padding = new CustomAttribute(constructor); + padding.ConstructorArguments.Add(new(module.TypeSystem.String, new string('z', 70000))); + assembly.CustomAttributes.Add(padding); + } + using var stream = new MemoryStream(); + assembly.Write(stream); + return stream.ToArray(); +} + +double Check(byte[] bytes, int width, bool proof = true) +{ + using var pinned = (IDisposable)Activator.CreateInstance(pinnedType, Internal, null, + [bytes, "fixture", sources, null], null)!; + var selected = (ModuleDefinition)pinnedType.GetProperty("Selected", Internal)!.GetValue(pinned)!; + var original = (ModuleDefinition)pinnedType.GetMethod("Original", Internal)!.Invoke(pinned, [selected])!; + var owner = original.GetType("Fields"); + var collection = Activator.CreateInstance(collectionType, Internal, null, + [pinned, new HashSet<(ModuleDefinition, uint)>()], null)!; + var slice = collectionType.GetMethod("SliceOf", Internal)!.Invoke(collection, [original])!; + var sliceType = slice.GetType(); + sliceType.GetMethod("Method", Internal)!.Invoke(slice, [owner.Methods.Single(), false]); + sliceType.GetMethod("Token", Internal)!.Invoke(slice, [owner.Fields.Last().MetadataToken.ToUInt32(), false]); + sliceType.GetMethod("Finish", Internal)!.Invoke(slice, null); + var rows = (SortedDictionary)sliceType.GetField("rows", Internal)!.GetValue(slice)!; + var strings = (SortedDictionary)sliceType.GetField("strings", Internal)!.GetValue(slice)!; + var blobs = (SortedDictionary)sliceType.GetField("blobs", Internal)!.GetValue(slice)!; + var row = rows[owner.Fields[0].MetadataToken.ToUInt32()]; + uint Read(int at) => width == 2 ? BitConverter.ToUInt16(row, at) : BitConverter.ToUInt32(row, at); + var name = Read(2); + var signature = Read(2 + width); + Require(name != 0 && !strings.ContainsKey((int)name), "Position-only name was carried."); + Require(signature != 0 && !blobs.ContainsKey((int)signature), "Position-only signature was carried."); + Require(strings[0].SequenceEqual(new byte[] { 0 }) && blobs[0].SequenceEqual(new byte[] { 0 }), + "The fixture must carry both legitimate empty entries."); + var flags = BitConverter.ToUInt16(row); + var ownerRow = rows[owner.MetadataToken.ToUInt32()]; + var namespaceOffset = 4 + width; + Require(ownerRow.AsSpan(namespaceOffset, width).IndexOfAnyExcept((byte)0) < 0, + "The fixture must use the legitimate empty namespace."); + var watch = System.Diagnostics.Stopwatch.StartNew(); + sliceType.GetMethod("Canonicalize", Internal)!.Invoke(slice, null); + watch.Stop(); + var marker = width == 2 ? ushort.MaxValue : uint.MaxValue; + Require(Read(2) == marker && Read(2 + width) == marker, + $"Omitted heap columns must use {marker}, observed {Read(2)} and {Read(2 + width)}."); + Require(BitConverter.ToUInt16(row) == flags, "Position-only field flags changed."); + Require(ownerRow.AsSpan(namespaceOffset, width).IndexOfAnyExcept((byte)0) < 0, + "The legitimate empty namespace offset changed."); + Require(!strings.ContainsKey(unchecked((int)marker)) && !blobs.ContainsKey(unchecked((int)marker)), + "An omitted heap marker was emitted as an entry."); + foreach (var field in owner.Fields.Take(owner.Fields.Count - 1)) + { + var prefixRow = rows[field.MetadataToken.ToUInt32()]; + var prefixName = width == 2 ? BitConverter.ToUInt16(prefixRow, 2) : BitConverter.ToUInt32(prefixRow, 2); + var prefixSignature = width == 2 ? BitConverter.ToUInt16(prefixRow, 2 + width) + : BitConverter.ToUInt32(prefixRow, 2 + width); + Require(prefixName == marker && prefixSignature == marker, "A position-only field became readable."); + } + if (proof) + { + var metadataType = extractor.GetType("Nethermind.LeanCIL.MetadataSlices", throwOnError: true)!; + var metadata = Activator.CreateInstance(metadataType, pinned)!; + var assemblyBlock = (string)metadataType.GetMethod("AssemblyBlock", Internal)!.Invoke(metadata, [original])!; + Require(assemblyBlock.Contains("assemblyRow :=", StringComparison.Ordinal), "Assembly row was not emitted."); + foreach (var (heap, read, offset, heapNumber) in new[] + { (strings, "stringBytes", Read(2), 0), (blobs, "blobBytes", Read(2 + width), 1) }) + { + var entries = string.Join(", ", heap.Select(entry => + $"({entry.Key}, [{string.Join(", ", entry.Value)}])")); + var namePrefix = "heap" + proofCount++; + proofs.AppendLine($"/-- Carried heap entries from a position-only field fixture. -/"); + proofs.AppendLine($"def {namePrefix} : SearchTree (List Nat) := SearchTree.ofSorted [{entries}]"); + proofs.AppendLine($"/-- An omitted field column cannot read the carried empty entry. -/"); + proofs.AppendLine($"theorem {namePrefix}_missing : {read} {namePrefix} {offset} ="); + proofs.AppendLine($" .error (.missingHeap {heapNumber} {offset}) := by rfl"); + proofs.AppendLine($"#print axioms {namePrefix}_missing"); + proofs.AppendLine($"/-- A genuine zero column still reads the empty entry. -/"); + proofs.AppendLine($"theorem {namePrefix}_empty : {read} {namePrefix} 0 = .ok [] := by rfl"); + proofs.AppendLine($"#print axioms {namePrefix}_empty"); + } + Console.WriteLine($"PASS {width}-byte omitted name and signature with carried empty entries"); + } + return watch.Elapsed.TotalMilliseconds; +} + +static void Require(bool condition, string message) +{ + if (!condition) throw new InvalidOperationException(message); +} diff --git a/Tests/MetadataWorld.lean b/Tests/MetadataWorld.lean new file mode 100644 index 000000000..8f20ac3b0 --- /dev/null +++ b/Tests/MetadataWorld.lean @@ -0,0 +1,263 @@ +import Tests.MetadataBuilder + +/-! +# A hand-built world of two pinned assemblies + +`T` carries the operands under test: references to `System.Runtime`, calls into `Lib`, instance +and static fields, fields of generic instances, a generic method specification, a string, a +locals signature and a standalone call signature. `Lib` defines the class `Lib.Box`, the struct +`Lib.Pair`, the generic class `Lib.Holder` and the interface `Lib.IShape` that `T` uses. Tests +check each operand against the slices, then change one fact and assert the exact error. +-/ + +namespace Tests.MetadataWorld + +open CIL CIL.Extraction Tests.MetadataBuilder +/-- The `Lib` assembly: a class `Lib.Box` with one instance field and one static method, whose + base `System.Object` is named through its own reference to `System.Runtime`. -/ +def libWorld : Step Unit := do + let runtimeName ← string "System.Runtime" + let objectName ← string "Object" + let systemName ← string "System" + let moduleName ← string "" + let boxName ← string "Box" + let libName ← string "Lib" + let valueName ← string "value" + let getName ← string "Get" + let publicKeyToken ← blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtimeName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let _ ← row Table.typeRef [1 * 4 + 2, objectName, systemName] + let _ ← row Table.typeDef [0, moduleName, 0, 0, 1, 1] + let box ← row Table.typeDef [1, boxName, libName, 1 * 4 + 1, 1, 1] + let fieldSignature ← blob [0x06, 0x08] + let field ← row Table.field [0x0006, valueName, fieldSignature] + let getSignature ← blob [0x00, 0x00, 0x08] + let get ← row Table.methodDef [0x2050, 0, 0x0096, getName, getSignature, 1] + link linkDeclaring field 1 box + link linkDeclaring get 1 box + -- The struct `Lib.Pair`, which extends `System.ValueType` through a reference of its own. + let pairName ← string "Pair" + let valueTypeName ← string "ValueType" + let valueTypeRef ← row Table.typeRef [1 * 4 + 2, valueTypeName, systemName] + let pair ← row Table.typeDef + [0x101, pairName, libName, (valueTypeRef - 0x01000000) * 4 + 1, 2, 2] + -- Its instance field `first` and its static method `Sum`. + let firstName ← string "first" + let sumName ← string "Sum" + let first ← row Table.field [0x0006, firstName, fieldSignature] + let sum ← row Table.methodDef [0x2060, 0, 0x0096, sumName, getSignature, 1] + link linkDeclaring first 1 pair + link linkDeclaring sum 1 pair + -- The generic class `Lib.Holder` with the static field `Value : T`. + let holderName ← string "Holder`1" + let holderValueName ← string "Value" + let parameterName ← string "T" + let holder ← row Table.typeDef [0x1, holderName, libName, 1 * 4 + 1, 3, 3] + let parameterSignature ← blob [0x06, 0x13, 0x00] + let holderValue ← row Table.field [0x0016, holderValueName, parameterSignature] + let parameter ← row Table.genericParam [0, 0, (holder - 0x02000000) * 2, parameterName] + link linkDeclaring holderValue 1 holder + link linkGenericParams holder 1 parameter + -- The interface `Lib.IShape`, which extends no type. + let shapeName ← string "IShape" + let _ ← row Table.typeDef [0xA1, shapeName, libName, 0, 4, 3] + +/-- The tokens of the `T` world that the tests name. -/ +structure OwnTokens where + objectRef : Nat + consoleRef : Nat + boxRef : Nat + writeLine : Nat + getRef : Nat + idRef : Nat + idSpec : Nat + countField : Nat + totalField : Nat + main : Nat + localsSignature : Nat + hello : Nat + indirect : Nat + extra : Nat + /-- The reference to the struct `Lib.Pair`. -/ + pairRef : Nat + /-- The second instance field of `T.Program`, after the static `total`. -/ + secondField : Nat + /-- The references to the field `Lib.Pair.first` and the method `Lib.Pair.Sum`. -/ + firstRef : Nat + sumRef : Nat + /-- `Value` of `Lib.Holder`, a pinned generic class, and of `System.Lazy`, an + external one, as a generic method reads them. -/ + holderValueRef : Nat + lazyValueRef : Nat + /-- The references to the interface `Lib.IShape` and to `System.String`. -/ + shapeRef : Nat + stringRef : Nat + +/-- The `T` assembly: the references and members its operands name. -/ +def ownWorld : Step OwnTokens := do + let runtimeName ← string "System.Runtime" + let libName ← string "Lib" + let objectName ← string "Object" + let consoleName ← string "Console" + let systemName ← string "System" + let boxName ← string "Box" + let moduleName ← string "" + let programName ← string "Program" + let countName ← string "count" + let totalName ← string "total" + let mainName ← string "Main" + let writeLineName ← string "WriteLine" + let getName ← string "Get" + let idName ← string "Id" + let secondName ← string "second" + let publicKeyToken ← blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtimeName, 0, 0] + let _ ← row Table.assemblyRef [1, 0, 0, 0, 0, 0, libName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let objectRef ← row Table.typeRef [1 * 4 + 2, objectName, systemName] + let consoleRef ← row Table.typeRef [1 * 4 + 2, consoleName, systemName] + let boxRef ← row Table.typeRef [2 * 4 + 2, boxName, libName] + let _ ← row Table.typeDef [0, moduleName, 0, 0, 1, 1] + let program ← row Table.typeDef [1, programName, 0, 1 * 4 + 1, 1, 1] + let intField ← blob [0x06, 0x08] + let countField ← row Table.field [0x0006, countName, intField] + let totalField ← row Table.field [0x0016, totalName, intField] + let secondField ← row Table.field [0x0006, secondName, intField] + let mainSignature ← blob [0x00, 0x00, 0x01] + let main ← row Table.methodDef [0x2050, 0, 0x0096, mainName, mainSignature, 1] + let writeLineSignature ← blob [0x00, 0x01, 0x01, 0x08] + let writeLine ← row Table.memberRef [2 * 8 + 1, writeLineName, writeLineSignature] + let getSignature ← blob [0x00, 0x00, 0x08] + let getRef ← row Table.memberRef [3 * 8 + 1, getName, getSignature] + let idSignature ← blob [0x10, 0x01, 0x01, 0x1E, 0x00, 0x1E, 0x00] + let idRef ← row Table.memberRef [2 * 8 + 1, idName, idSignature] + let instantiation ← blob [0x0A, 0x01, 0x09] + let idSpec ← row Table.methodSpec [(idRef - 0x0A000000) * 2 + 1, instantiation] + let localsBlob ← blob [0x07, 0x01, 0x0B] + let localsSignature ← row Table.standAloneSig [localsBlob] + let hello ← userString [104, 105] + let indirectBlob ← blob [0x00, 0x01, 0x01, 0x08] + let indirect ← row Table.standAloneSig [indirectBlob] + link linkDeclaring countField 0 program + link linkDeclaring totalField 0 program + link linkDeclaring secondField 0 program + link linkDeclaring main 0 program + link linkDefinition boxRef 1 (tokenOf Table.typeDef 2) + link linkDefinition getRef 1 (tokenOf Table.methodDef 1) + let pairName ← string "Pair" + let pairRef ← row Table.typeRef [2 * 4 + 2, pairName, libName] + link linkDefinition pairRef 1 (tokenOf Table.typeDef 3) + let firstName ← string "first" + let sumName ← string "Sum" + let pairParent := (pairRef - 0x01000000) * 8 + 1 + let firstRef ← row Table.memberRef [pairParent, firstName, intField] + let sumRef ← row Table.memberRef [pairParent, sumName, getSignature] + link linkDefinition firstRef 1 (tokenOf Table.field 2) + link linkDefinition sumRef 1 (tokenOf Table.methodDef 2) + -- `Holder` and `Lazy` as type specifications, each with a member `Value : !0`. + let holderName ← string "Holder`1" + let lazyName ← string "Lazy`1" + let valueName ← string "Value" + let holderRef ← row Table.typeRef [2 * 4 + 2, holderName, libName] + let lazyRef ← row Table.typeRef [1 * 4 + 2, lazyName, systemName] + link linkDefinition holderRef 1 (tokenOf Table.typeDef 4) + let holderBlob ← blob [0x15, 0x12, (holderRef - 0x01000000) * 4 + 1, 1, 0x1E, 0x00] + let lazyBlob ← blob [0x15, 0x12, (lazyRef - 0x01000000) * 4 + 1, 1, 0x1E, 0x00] + let holderSpec ← row Table.typeSpec [holderBlob] + let lazySpec ← row Table.typeSpec [lazyBlob] + let parameterField ← blob [0x06, 0x13, 0x00] + let holderValueRef ← + row Table.memberRef [(holderSpec - 0x1B000000) * 8 + 4, valueName, parameterField] + let lazyValueRef ← + row Table.memberRef [(lazySpec - 0x1B000000) * 8 + 4, valueName, parameterField] + link linkDefinition holderValueRef 1 (tokenOf Table.field 3) + let shapeName ← string "IShape" + let stringName ← string "String" + let shapeRef ← row Table.typeRef [2 * 4 + 2, shapeName, libName] + link linkDefinition shapeRef 1 (tokenOf Table.typeDef 5) + let stringRef ← row Table.typeRef [1 * 4 + 2, stringName, systemName] + pure { shapeRef := shapeRef, stringRef := stringRef, + pairRef := pairRef, secondField := secondField, firstRef := firstRef, sumRef := sumRef, + objectRef := objectRef, consoleRef := consoleRef, boxRef := boxRef, + holderValueRef := holderValueRef, + lazyValueRef := lazyValueRef, writeLine := writeLine, getRef := getRef, idRef := idRef, + idSpec := idSpec, + countField := countField, totalField := totalField, main := main, + localsSignature := localsSignature, hello := 0x70000000 + hello, + indirect := indirect, extra := 0 } + +/-- The own slice and its tokens. -/ +def own : MetadataSlice × OwnTokens := + build ownWorld (bytesOf "T") "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + +/-- The `Lib` slice. -/ +def lib : MetadataSlice := + (build libWorld (bytesOf "Lib") "Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null").1 + +def tokens : OwnTokens := own.2 + +/-- The platform definitions of the `System.Runtime` types the world and the fixtures built on + it name. -/ +def worldPlatform : PlatformTable := platform_table% + [runtimeType "Object" .objectRoot, runtimeType "Console" .classType, + runtimeType "ValueType" .valueTypeRoot, runtimeType "Enum" .enumRoot, + runtimeType "Lazy`1" (.genericClass 1), runtimeType "String" .classType, + runtimeType "ValueTuple`2" .valuePair, runtimeType "Math" .classType, + runtimeType "G`1" (.genericClass 1), + ⟨runtimeScopeBytes, [bytesOf "System.Runtime.CompilerServices", bytesOf "Unsafe"], .classType⟩] + +/-- The 32-bit and 64-bit unsigned types as the extractor writes them. -/ +def int32 : TypeExpr := .atom runtimeScope "System.Int32" +def uint32 : TypeExpr := .atom runtimeScope "System.UInt32" +def uint64 : TypeExpr := .atom runtimeScope "System.UInt64" + +def libScope : String := "Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + +/-- `System.Console.WriteLine(int)`: an external static call. -/ +def writeLineSite : CallSite := + { declared := { convention := 0, parameters := [int32] }, + method := { + owner := .atom runtimeScope "System.Console", name := name!"WriteLine", hasThis := false, + genericArity := 0, returnType := .void, parameters := [int32] }, + name := "WriteLine", + target := .external (runtimeScope ++ "::System.Void System.Console::WriteLine(System.Int32)"), + typeArguments := [], parameterCount := 1, returnsValue := false } + +/-- `Lib.Box.Get()`: a managed call into the other assembly. -/ +def getSite : CallSite := + { declared := { convention := 0, returnType := int32 }, + method := { + owner := .atom libScope "Lib.Box", name := name!"Get", hasThis := false, genericArity := 0, + returnType := int32, parameters := [] }, + name := "Get", target := .managed 0x06000001, typeArguments := [], parameterCount := 0, + returnsValue := true } + +/-- `Lib.Pair.Sum()`: a managed call to a static method of a struct in the other assembly. -/ +def sumSite : CallSite := + { declared := { convention := 0, returnType := int32 }, + method := { + owner := .atom libScope "Lib.Pair", name := name!"Sum", hasThis := false, genericArity := 0, + returnType := int32, parameters := [] }, + name := "Sum", target := .managed 0x06000002, typeArguments := [], parameterCount := 0, + returnsValue := true } + +/-- `System.Console.Id(!!0)`: an external generic method. -/ +def idSite : CallSite := + { declared := { convention := 0x10, returnType := .parameter 0, parameters := [.parameter 0] }, + method := { + owner := .atom runtimeScope "System.Console", name := name!"Id", hasThis := false, + genericArity := 1, returnType := uint32, parameters := [uint32], + methodArguments := [uint32] }, + name := "Id", target := .external (runtimeScope ++ "::!!0 System.Console::Id(!!0)"), + typeArguments := [uint32], parameterCount := 1, returnsValue := true } + +/-- The count and total fields of `T.Program`. -/ +def countOperand : RawOperand := .field (.atom "T, Version=1.0.0.0, Culture=neutral, \ + PublicKeyToken=null" "Program") 0 int32 name!"count" false + +/-- The identity of the assembly `T`. -/ +def ownIdentity : String := + "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + +end Tests.MetadataWorld diff --git a/Tests/MethodBinding.lean b/Tests/MethodBinding.lean index 44b4707b3..e767e607d 100644 --- a/Tests/MethodBinding.lean +++ b/Tests/MethodBinding.lean @@ -1,4 +1,6 @@ import CIL.MethodBinding +import CIL.Extraction.TemplateCode +import Tests.Support namespace Tests.MethodBinding @@ -6,7 +8,7 @@ open CIL /-- An extraction-independent body whose branch target depends on a shift. -/ def body (shift : Nat) (locals : List Value := [.i32 0]) : Method := { - code := [.arg 0, .brzero (4 + shift), .arg 1, .ret, .arg 1, .ret] + code := [.ldarg 0, .brfalse (4 + shift), .ldarg 1, .ret, .ldarg 1, .ret] locals, returnsValue := true } def program : Program := [{ code := [.ret], locals := [], returnsValue := false }, body 6] @@ -23,8 +25,8 @@ run_cmd do error: cil_body: rfl could not prove the method-body binding method index 1 field code: first difference at index 1 (left length 6, right length 6) - left: CIL.Op.brzero 10 - right: CIL.Op.brzero 4 + left: CIL.Instruction.brfalse 10 + right: CIL.Instruction.brfalse 4 -/ #guard_msgs (substring := true) in example : program[1]? = some (body 0) := by cil_body @@ -51,9 +53,103 @@ example : program[2]? = some (body 6) := by cil_body error: cil_body: rfl could not prove the method-body binding field code: first difference at index 6 (left length 6, right length 7) left: (past the end) - right: CIL.Op.nop + right: CIL.Instruction.nop -/ #guard_msgs (substring := true) in example : body 6 = { body 6 with code := (body 6).code ++ [.nop] } := by cil_body +/-- An extracted method whose call sits at pc 2, behind a branch, and which reads no static. -/ +def calling : Method := { + code := [.ldarg 0, .br 2, .call (Tests.callee 1) {}, .ret], links := [(2, .direct 7)], + locals := [], returnsValue := true } + +/-- A tail-prefixed call with a managed operand and its own target slot. -/ +private def tailCall (slot : Nat) : Method := + { calling with + code := [.nop, .call (Tests.callee 1) { tail := true }, .ret] + links := [(1, .direct slot)] } + +-- Relocation retains the operand and the prefix, and only the managed target index changes. +#guard ((tailCall 3).relocate (fun _ => 1)).code == (tailCall 1).code +#guard MethodBinding.firstCodeOrLinkDifference + ((tailCall 3).relocate (fun _ => 1)) (tailCall 1) == none +#guard MethodBinding.firstCodeOrLinkDifference (tailCall 3) (tailCall 1) == some 1 + +/-- Templates rename the definition named by an address as well as indirect call targets. -/ +theorem reindexed_address (newIndex : Nat) : + (({ calling with links := [(2, .address 3)] } : Method).reindexLinks + (fun _ => newIndex)).links = [(2, .address newIndex)] := rfl +#print axioms reindexed_address + +#guard MethodBinding.firstCodeOrLinkDifference calling { calling with links := [] } == some 2 +#guard MethodBinding.firstCodeOrLinkDifference { calling with links := [] } calling == some 2 +#guard MethodBinding.firstCodeOrLinkDifference calling + { calling with links := [(3, .direct 7)] } == some 2 +#guard MethodBinding.firstCodeOrLinkDifference calling + { calling with code := [.nop], links := [] } == some 0 +#guard MethodBinding.firstCodeOrLinkDifference calling + { calling with code := calling.code ++ [.nop], links := [] } == some 2 + +/-- The field operand of the static reads below. -/ +def fieldRef : CIL.Extraction.FieldRef := + { owner := .atom "Fixture" "Fixture", index := 0, type := .atom "Fixture" "System.Int64", + name := name!"Value" } + +/-- The static field the fixtures below read. -/ +def field : StaticDescriptor := + { identity := 1, field := fieldRef, bytes := List.replicate 8 0 } + +/-- An extracted method that reads a static at pc 0 and calls at pc 3. -/ +def reading : Method := { + code := [.ldsflda fieldRef, .pop, .ldarg 0, .call (Tests.callee 1) {}, .ret], + links := [(3, .direct 7)] + locals := [], returnsValue := true, staticSites := [(0, field)] } + +/-- Another method whose frame fields a body is built on, with a call and a static at other pcs. -/ +def donor : Method := { + code := [.ldarg 0, .ldsflda fieldRef, .pop, .ldarg 0, .call (Tests.callee 1) {}, .ret], + links := [(4, .direct 3)] + locals := [], returnsValue := true, staticSites := [(1, field)] } + +def withCodeProgram : Program := [calling, reading] + +/-- The body is the extraction's method: `withCode` drops the donor's call link and static site, + keyed by the donor's pcs, and the call's link is written for the new code. -/ +theorem with_code_binds : + withCodeProgram[0]? = some { donor.withCode calling.code with links := [(2, .direct 7)] } := by + cil_body + +#print axioms with_code_binds + +-- A structure update keeps the donor's links and static sites, and the binding names both. +#guard ((MethodBinding.methodDifferences calling { donor with code := calling.code }).map + fun line => (line.takeWhile (· != ':')).toString) == ["field staticSites", "field links"] + +-- `withCode` alone drops a call's link, which its code does not imply, and the binding says so. +#guard ((MethodBinding.methodDifferences calling (donor.withCode calling.code)).map + fun line => (line.takeWhile (· != ':')).toString) == ["field links"] + +#guard ((MethodBinding.methodDifferences calling (donor.withCode calling.code)).map + fun line => (line.splitOn "links := [(pc, link), ...]").length) == [2] + +/-- `reading`'s code under `withCode`, with its call's link. -/ +def readingLinked : Method := { donor.withCode reading.code with links := [(3, .direct 7)] } + +-- A body that reads a static is not bound by `withCode` alone, which resets the sites. +example : readingLinked.staticSites = [] := rfl + +#guard ((MethodBinding.methodDifferences reading readingLinked).map + fun line => (line.takeWhile (· != ':')).toString) == ["field staticSites"] + +#guard ((MethodBinding.methodDifferences reading readingLinked).map + fun line => (line.splitOn "Method.withCode").length) == [2] + +/-- Setting the sites after `withCode` binds it. -/ +theorem with_code_sites_bind : + withCodeProgram[1]? = + some { readingLinked with staticSites := [(0, field)] } := by + cil_body + +#print axioms with_code_sites_bind + end Tests.MethodBinding diff --git a/Tests/MethodContract.cs b/Tests/MethodContract.cs index deddac6a9..0d76a304c 100644 --- a/Tests/MethodContract.cs +++ b/Tests/MethodContract.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.RegularExpressions; @@ -17,7 +22,8 @@ string Build(string name, string helpers) var source = Path.Combine(root, name); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Contracts.csproj"), """ - net10.0true + net11.0 + true """); File.WriteAllText(Path.Combine(source, "Contracts.cs"), "namespace Contracts; public static class Calls { " + helpers + Root + " }"); Run(["dotnet", "build", Path.Combine(source, "Contracts.csproj"), "-c", "Release"]); @@ -118,14 +124,10 @@ dsimp only [addContract] // Each bug fails the original helper proofs, and a concrete run refutes the contract itself. const string RefuteArithmetic = """ /-- This build returns `Mix(0, 0) = 1`; the contract requires `0 * 3 + 0`. -/ -theorem correctness : ¬ mixContract.Holds Extracted.program mix := by - intro holds - obtain ⟨_, ⟨final, values⟩, ran, -, out, encoded, wrong⟩ := holds (0, 0) Memory.empty Memory.empty_wellFormed - obtain ⟨rfl, rfl⟩ := Prod.mk.inj (invoke_ok_unique ran (rfl : invoke Extracted.program 16 mix - (mixContract.args (0, 0)) Memory.empty = .ok _)) - simp only [mixContract] at encoded wrong - subst wrong - simp at encoded +theorem correctness : mixContract.Refuted Extracted.program mix := + .of_return (input := (0, 0)) (initial := Memory.empty) Memory.empty_wellFormed + (rfl : invoke Extracted.program 16 mix (mixContract.args (0, 0)) Memory.empty = .ok _) + (by rintro ⟨-, out, encoded, wrong⟩; simp only [mixContract] at encoded wrong; subst wrong; simp at encoded) """; const string RefuteAliasing = """ @@ -137,12 +139,10 @@ simp at encoded nextIdentity := 1 } /-- The contract permits `res` to alias `b`; this build then writes `2`, not `1 + 2`. -/ -theorem correctness : ¬ addContract.Holds Extracted.program addTo := by - intro holds - obtain ⟨_, ⟨final, values⟩, ran, -, _, -, -, wrong⟩ := - holds ⟨⟨0, 0⟩, ⟨0, 8⟩, ⟨0, 8⟩, 1, 2⟩ aliased ⟨rfl, rfl, rfl⟩ - obtain ⟨rfl, rfl⟩ := Prod.mk.inj (invoke_ok_unique ran (rfl : invoke Extracted.program 16 addTo - (addContract.args ⟨⟨0, 0⟩, ⟨0, 8⟩, ⟨0, 8⟩, 1, 2⟩) aliased = .ok _)) +theorem correctness : addContract.Refuted Extracted.program addTo := + .of_return (input := ⟨⟨0, 0⟩, ⟨0, 8⟩, ⟨0, 8⟩, 1, 2⟩) (initial := aliased) ⟨rfl, rfl, rfl⟩ + (rfl : invoke Extracted.program 16 addTo (addContract.args ⟨⟨0, 0⟩, ⟨0, 8⟩, ⟨0, 8⟩, 1, 2⟩) aliased = .ok _) <| by + rintro ⟨-, _, -, -, wrong⟩ have observed : (Except.ok (numberBytes (1 + 2 : CIL.W64).toNat 8) : Checked (List (BitVec 8))) = .ok (numberBytes 2 8) := wrong.symm.trans rfl exact absurd (Except.ok.inj observed) (by decide) diff --git a/Tests/MethodContract.lean b/Tests/MethodContract.lean index 4e4daf399..8b12c108b 100644 --- a/Tests/MethodContract.lean +++ b/Tests/MethodContract.lean @@ -5,6 +5,9 @@ import CIL.Safety.WordMemory import CIL.Safety.WordFootprint import CIL.Safety.ReturnMemory import CIL.Safety.Segment +import Tests.Support +import Tests.Support.IntrinsicMethods +import Tests.WordFields /-! A synthetic extraction with the bodies the C# fixture compiles to: `Root(ref r, in b) { AddTo(in r, in b, out r); r = Mix(r, b); }`. -/ @@ -16,17 +19,20 @@ def method0Signature : String := "System.Void Contracts.Calls::Root(System.UInt6 def method1Signature : String := "System.UInt64 Contracts.Calls::Mix(System.UInt64,System.UInt64)" def method2Signature : String := "System.Void Contracts.Calls::AddTo(System.UInt64&,System.UInt64&,System.UInt64&)" @[cil_code] def method0 : Method := { returnsValue := false, locals := [], code := - [.arg 0, .arg 1, .arg 0, .call 2 3, .arg 0, .arg 0, .load64, .arg 1, .load64, .call 1 2, .store64, .ret] } + [.ldarg 0, .ldarg 1, .ldarg 0, .call (Tests.callee 3) {}, .ldarg 0, .ldarg 0, .ldind .i8, .ldarg + 1, .ldind .i8, + .call (Tests.callee 2) {}, .stind .i8, .ret], links := [(3, .direct 2), (9, .direct 1)] } @[cil_code] def method1 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .const32 3, .convI8, .mul, .arg 1, .add, .ret] } + [.ldarg 0, .ldc_i4 3, .conv_i8, .mul, .ldarg 1, .add, .ret] } @[cil_code] def method2 : Method := { returnsValue := false, locals := [], code := - [.arg 2, .arg 0, .load64, .arg 1, .load64, .add, .store64, .ret] } + [.ldarg 2, .ldarg 0, .ldind .i8, .ldarg 1, .ldind .i8, .add, .stind .i8, .ret] } @[cil_code] def program : Program := [method0, method1, method2] def signatures : List String := [method0Signature, method1Signature, method2Signature] end Extracted namespace Tests.MethodContract +open Tests.WordFields open CIL.Safety @@ -242,8 +248,8 @@ example {program : CIL.Program} {before after : Memory} (kept : FrameInvariants -- equation; unfolding `CIL.step`, or re-elaborating a tactic per step, exceeds the budget. def loadedGuardBody (profile : CIL.FeatureProfile) : CIL.Method := { profile, returnsValue := false, locals := [], code := - [.local 0, .const32 4, .bltu 7, .local 0, .const32 0, .bge 7, .nop, .ret, - .local 1, .local 2, .bltu 7, .ret] } + [.ldloc 0, .ldc_i4 4, .blt_un 7, .ldloc 0, .ldc_i4 0, .bge 7, .nop, .ret, + .ldloc 1, .ldloc 2, .blt_un 7, .ret] } set_option maxHeartbeats 2000 in theorem loaded_unsigned_branch (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) @@ -286,7 +292,7 @@ theorem loaded_limb_branch (program : CIL.Program) (index : Nat) (profile : CIL. -- equation; re-elaborating a tactic at every contract step exceeds the budget. def widenBody (signed : Bool) : CIL.Method := { returnsValue := true, locals := [], code := - [.arg 0, if signed then .convI8 else .convU8, .arg 1, .pop, .pop, .const32 0, .ret] } + [.ldarg 0, if signed then .conv_i8 else .conv_u8, .ldarg 1, .pop, .pop, .ldc_i4 0, .ret] } def widenArgs (scalarFirst : Bool) (input : Reference) (value : CIL.Value) : List Value := if scalarFirst then [.scalar value, .reference (.address input)] else [.reference (.address input), .scalar value] @@ -317,7 +323,8 @@ theorem selected_widening (signed : Bool) (program : CIL.Program) (index : Nat) /-- A feature test and a vector load step by their own equations inside a contract proof. -/ def featureLoadBody (profile : CIL.FeatureProfile) : CIL.Method := - { profile, returnsValue := true, locals := [], code := [.feature .avx2, .pop, .arg 0, .memory .load128, .ret] } + { profile, returnsValue := true, locals := [], code := [.call (Tests.supportedGetter "X86.Avx2"), .pop, .ldarg 0, + .ldobj Tests.IntrinsicMethods.vector128Byte, .ret] } theorem feature_load_contract (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) (wide : profile.avx2 = true) (found : program[index]? = some (featureLoadBody profile)) @@ -334,15 +341,15 @@ theorem feature_load_contract (program : CIL.Program) (index : Nat) (profile : C /-- A body whose first instruction depends on a feature, stepped as a contract proof steps it. -/ def featureOpBody (profile : CIL.FeatureProfile) : CIL.Method := - { profile, returnsValue := true, locals := [], - code := [if profile.avx2 then .arg 1 else .arg 0, .fieldAt 0 8, .ret] } + { profile, environment := wordsEnvironment, returnsValue := true, locals := [], + code := [if profile.avx2 then .ldarg 1 else .ldarg 0, .ldfld word0, .ret] } open CIL.Safety in theorem feature_op_contract_steps (program : CIL.Program) (index : Nat) (profile : CIL.FeatureProfile) (scalar : profile.avx2 = false) (found : program[index]? = some (featureOpBody profile)) (frame : Frame) (memory : Memory) (left right : Reference) (limb : BitVec 64) (formed : form memory left = .ok left) - (field : ∀ rest, instruction (.fieldAt 0 8) (.reference (.address left) :: rest) memory = + (field : ∀ rest, loadValueField 0 8 false (.reference (.address left) :: rest) memory = .ok (memory, .scalar (.i64 limb) :: rest)) : ∃ fuel final values, run program fuel index 0 [.reference (.address left), .reference (.address right)] frame [] memory = @@ -356,10 +363,10 @@ theorem feature_op_contract_steps (program : CIL.Program) (index : Nat) (profile def orderedWidenBody (signed scalarFirst reversed : Bool) : CIL.Method := let first := if reversed then 1 else 0 let second := if reversed then 0 else 1 - let convert := if signed then CIL.Op.convI8 else CIL.Op.convU8 + let convert := if signed then CIL.Instruction.conv_i8 else CIL.Instruction.conv_u8 { returnsValue := true, locals := [], code := - (if scalarFirst then [.arg first, convert, .arg second] - else [.arg first, .arg second, convert]) ++ [.pop, .pop, .const32 0, .ret] } + (if scalarFirst then [.ldarg first, convert, .ldarg second] + else [.ldarg first, .ldarg second, convert]) ++ [.pop, .pop, .ldc_i4 0, .ret] } set_option maxHeartbeats 12000 in /-- All eight wrapper cases use one three-step proof without unfolding the evaluator. -/ @@ -408,10 +415,11 @@ theorem ordered_widening_returns (signed scalarFirst reversed : Bool) theorem framed_opaque_call {program : CIL.Program} {memory : Memory} (value : CIL.W32) (formed : memory.WellFormed) (found : program[0]? = some - { locals := [], returnsValue := true, code := [.call 1 0, .ret] }) + { locals := [], returnsValue := true, code := [.call (Tests.callee 0) {}, .ret], + links := [(0, .direct 1)] }) (helper : WP (Execution (fun fuel => invoke program fuel 1 [] memory)) (fun result => result.2 = [.scalar (.i32 value)])) : - WP (Execution (fun fuel => run program fuel 0 0 [] ⟨0, [], [], [], 0⟩ [] memory)) + WP (Execution (fun fuel => run program fuel 0 0 [] ⟨0, [], [], 0⟩ [] memory)) (fun result => FrameInvariants program memory result.1 ∧ AllocationExtension memory result.1 ∧ result.2 = [.scalar (.i32 value)]) := by apply WP.call_framed found rfl (by cil_checked_step []) formed helper @@ -426,7 +434,7 @@ theorem framed_opaque_call {program : CIL.Program} {memory : Memory} (value : CI /-- A feature test and a literal each feed a branch, which then continues at a numeral. -/ @[cil_code] def literalBranchBody (profile : CIL.FeatureProfile) : CIL.Method := { profile, returnsValue := true, locals := [], code := - [.feature .avx2, .brzero 5, .const64 1, .brnonzero 6, .ret, .const32 9, .const32 7, .ret] } + [.call (Tests.supportedGetter "X86.Avx2"), .brfalse 5, .ldc_i8 1, .brtrue 6, .ret, .ldc_i4 9, .ldc_i4 7, .ret] } /-- A branch on a literal operand is decided where it is taken: the lookups after it meet a numeral, and so does a continuation stated at the numeral. A conditional left in the @@ -449,15 +457,15 @@ theorem literal_branch_pc (program : CIL.Program) (index : Nat) (profile : CIL.F a `Fin` literal, and a loaded `localResult` of a typed value is the value the next fact takes. The facts would otherwise match only an index spelled as the numeral. -/ theorem coerced_index_facts (program : CIL.Program) (index : Nat) (body : CIL.Method) - (found : program[index]? = some body) (load : body.code[0]? = some (.local 7)) - (store : body.code[1]? = some (.setLocal 8)) (frame : Frame) (memory after : Memory) + (found : program[index]? = some body) (load : body.code[0]? = some (.ldloc 7)) + (store : body.code[1]? = some (.stloc 8)) (frame : Frame) (memory after : Memory) (bits : BitVec 128) (loaded : ∀ pc args stack, - step body (.local (↑(6 : Fin 10) + 1)) pc args frame stack memory = + step body (.ldloc (↑(6 : Fin 10) + 1)) pc args frame stack memory = .ok (.next (pc + 1) (.scalar (localResult .vector128 (.v128 bits)) :: stack) frame memory)) (stored : ∀ pc args stack, - step body (.setLocal (↑(7 : Fin 10) + 1)) pc args frame (.scalar (.v128 bits) :: stack) + step body (.stloc (↑(7 : Fin 10) + 1)) pc args frame (.scalar (.v128 bits) :: stack) memory = .ok (.next (pc + 1) stack frame after)) (continuation : WP (Execution (fun fuel => run program fuel index 2 [] frame [] after)) (fun _ => True)) : @@ -469,7 +477,7 @@ theorem coerced_index_facts (program : CIL.Program) (index : Nat) (body : CIL.Me /-- Two symbolic words are combined and branched on. -/ @[cil_code] def symbolicBranchBody : CIL.Method := { returnsValue := true, locals := [], code := - [.arg 0, .arg 1, .bor, .brnonzero 6, .const32 7, .ret, .const32 9, .ret] } + [.ldarg 0, .ldarg 1, .or, .brtrue 6, .ldc_i4 7, .ret, .ldc_i4 9, .ret] } /-- A branch no fact decides ends the walk with its condition in the program counter, where `split` gives both successors; stepping resumes in each. -/ @@ -497,7 +505,7 @@ theorem symbolic_branch_splits (program : CIL.Program) (index : Nat) error: cil_lookup: undecided branch if ¬left = 0#64 then 6 else 4; split on its condition -/ #guard_msgs in -example (left : CIL.W64) (operation : CIL.Op) : +example (left : CIL.W64) (operation : CIL.Instruction) : symbolicBranchBody.code[if ¬left = 0#64 then 6 else 4]? = some operation := by cil_lookup [] diff --git a/Tests/MethodEmbedding.lean b/Tests/MethodEmbedding.lean index 3574ae90f..d172bfa21 100644 --- a/Tests/MethodEmbedding.lean +++ b/Tests/MethodEmbedding.lean @@ -4,6 +4,7 @@ import CIL.Safety.Embedding.Simulation import CIL.Safety.Embedding.Contracts import CIL.Safety.SymbolicExecution import Tests.MulScalarFixture +import Tests.Support namespace Tests.MethodEmbedding @@ -11,7 +12,7 @@ open CIL private def byteHomeBody : Method := { locals := [.v128 0], localKinds := [.vector128], returnsValue := false, - code := [.local 0, .pop, .ret] } + code := [.ldloc 0, .pop, .ret] } private def opaqueHomeBody : Method := { byteHomeBody with opaqueVectorLocals := [0] } example : CIL.MethodBinding.embedFailure byteHomeBody byteHomeBody [0, 1, 2] = none := by decide +kernel @@ -25,13 +26,13 @@ example : MethodEmbeds [opaqueHomeBody] 0 opaqueHomeBody := by cil_embed /-- Returns argument 1 on both paths; pc 4 is reached only by the branch. -/ @[cil_code] def body : Method := { - code := [.arg 0, .brzero 4, .arg 1, .ret, .arg 1, .ret] + code := [.ldarg 0, .brfalse 4, .ldarg 1, .ret, .ldarg 1, .ret] locals := [], returnsValue := true } /-- `body` with a dead block before the branch target, as a profile-specific extraction emits it. -/ def inserted : Method := { - code := [.arg 0, .brzero 6, .arg 1, .ret, - .unsupported "Unreachable under selected profile", .const32 9, .arg 1, .ret] + code := [.ldarg 0, .brfalse 6, .ldarg 1, .ret, + .unadmitted 0, .ldc_i4 9, .ldarg 1, .ret] locals := [], returnsValue := true } -- Each `cil_embed` proof is kernel checked against the extracted method, never against `expected`. @@ -41,13 +42,13 @@ theorem dead_block_inserted : MethodEmbeds [inserted] 0 body := by cil_embed /-- A placed canonical suffix reuses its proof at the checked embedding's new position. -/ theorem relocated_suffix (frame : Safety.Frame) (memory : Safety.Memory) (value : W32) : - Safety.CodeAt inserted 6 [.arg 1, .ret] ∧ + Safety.CodeAt inserted 6 [.ldarg 1, .ret] ∧ Safety.WP (Safety.Execution (fun fuel => Safety.run [inserted] fuel 0 6 [.scalar (.i32 0), .scalar (.i32 value)] frame [] memory)) (fun (final, values) => final = Safety.leaveFrame frame memory ∧ values = [.scalar (.i32 value)]) := by refine ⟨rfl, ?_⟩ - have placed : Safety.CodeAt body 4 [.arg 1, .ret] := rfl + have placed : Safety.CodeAt body 4 [.ldarg 1, .ret] := rfl have canonical : Safety.WP (Safety.Execution (fun fuel => Safety.run ([inserted].set 0 body) fuel 0 4 [.scalar (.i32 0), .scalar (.i32 value)] frame [] memory)) @@ -62,8 +63,8 @@ theorem relocated_suffix (frame : Safety.Frame) (memory : Safety.Memory) (value /-- An expected body that still carries a dead block the extraction no longer emits. -/ def withDeadBlock : Method := { - code := [.arg 0, .brzero 6, .arg 1, .ret, .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", .arg 1, .ret] + code := [.ldarg 0, .brfalse 6, .ldarg 1, .ret, .unadmitted 0, + .unadmitted 0, .ldarg 1, .ret] locals := [], returnsValue := true } theorem dead_block_removed : MethodEmbeds [body] 0 withDeadBlock := by cil_embed @@ -91,38 +92,41 @@ theorem inserted_invocation (memory : Safety.Memory) (condition value : W32) : error: cil_embed: the expected body does not embed into the extracted method method index 0 expected pc 2 maps to actual pc 2 - expected: CIL.Op.arg 1 none - actual: CIL.Op.arg 0 none + expected: CIL.Instruction.ldarg 1 + actual: CIL.Instruction.ldarg 0 the instructions differ expected pc 1 falls through to it, so it must be actual pc 2 -/ #guard_msgs (substring := true) in -example : MethodEmbeds [{ inserted with code := inserted.code.set 2 (.arg 0 none) }] 0 body := by cil_embed +example : MethodEmbeds [{ inserted with code := inserted.code.set 2 (.ldarg 0) }] 0 body := by + cil_embed /-- error: cil_embed: the expected body does not embed into the extracted method method index 0 expected pc 1 maps to actual pc 1 - expected: CIL.Op.brzero 4 - renamed: CIL.Op.brzero 6 - actual: CIL.Op.brzero 5 - code target differs: expected target 4 maps to actual pc 6, but the actual instruction targets 5; actual pc 5 (CIL.Op.const32 0x00000009#32) is not the image of any live expected pc + expected: CIL.Instruction.brfalse 4 + renamed: CIL.Instruction.brfalse 6 + actual: CIL.Instruction.brfalse 5 + code target differs: expected target 4 maps to actual pc 6, but the actual instruction targets 5; actual pc 5 (CIL.Instruction.ldc_i4 0x00000009#32) is not the image of any live expected pc -/ #guard_msgs (substring := true) in -example : MethodEmbeds [{ inserted with code := inserted.code.set 1 (.brzero 5) }] 0 body := by cil_embed +example : MethodEmbeds [{ inserted with code := inserted.code.set 1 (.brfalse 5) }] 0 body := by + cil_embed -- The branch reaches an inserted live instruction, so the extracted method can return 9. /-- error: cil_embed: the expected body does not embed into the extracted method method index 0 expected pc 1 maps to actual pc 1 - expected: CIL.Op.brzero 4 - renamed: CIL.Op.brzero 6 - actual: CIL.Op.brzero 4 - code target differs: expected target 4 maps to actual pc 6, but the actual instruction targets 4; actual pc 4 (CIL.Op.const32 0x00000009#32) is not the image of any live expected pc + expected: CIL.Instruction.brfalse 4 + renamed: CIL.Instruction.brfalse 6 + actual: CIL.Instruction.brfalse 4 + code target differs: expected target 4 maps to actual pc 6, but the actual instruction targets 4; actual pc 4 (CIL.Instruction.ldc_i4 0x00000009#32) is not the image of any live expected pc -/ #guard_msgs (substring := true) in -example : MethodEmbeds [{ body with code := [.arg 0, .brzero 4, .arg 1, .ret, .const32 9, .ret, .arg 1, .ret] }] 0 body := by +example : MethodEmbeds [{ body with code := [.ldarg 0, .brfalse 4, .ldarg 1, .ret, .ldc_i4 9, + .ret, .ldarg 1, .ret] }] 0 body := by cil_embed -- The fallthrough of the branch reaches an inserted instruction. @@ -130,13 +134,14 @@ example : MethodEmbeds [{ body with code := [.arg 0, .brzero 4, .arg 1, .ret, .c error: cil_embed: the expected body does not embed into the extracted method method index 0 expected pc 2 maps to actual pc 2 - expected: CIL.Op.arg 1 none - actual: CIL.Op.const32 0x00000009#32 + expected: CIL.Instruction.ldarg 1 + actual: CIL.Instruction.ldc_i4 0x00000009#32 the instructions differ expected pc 1 falls through to it, so it must be actual pc 2 -/ #guard_msgs (substring := true) in -example : MethodEmbeds [{ body with code := [.arg 0, .brzero 5, .const32 9, .arg 1, .ret, .arg 1, .ret] }] 0 body := by +example : MethodEmbeds [{ body with code := [.ldarg 0, .brfalse 5, .ldc_i4 9, .ldarg 1, .ret, + .ldarg 1, .ret] }] 0 body := by cil_embed /-- @@ -169,10 +174,9 @@ theorem mulscalar_embeds : MethodEmbeds mulScalar 0 expected := by cil_embed /-- error: cil_embed: the expected body does not embed into the extracted method method index 0 -expected pc 188 maps to actual pc 200 - expected: CIL.Op.call 8 5 - actual: CIL.Op.call 7 5 - the instructions differ +expected pc 188 (actual pc 200) has a different call link + expected: some (CIL.Link.direct 8) + actual: some (CIL.Link.direct 7) -/ #guard_msgs (substring := true) in example : MethodEmbeds mulScalar 0 (fullBody profile 9 5 8) := by cil_embed @@ -183,7 +187,7 @@ section DeadLocals /-- Reads its one local, a null reference, then returns argument 0. -/ @[cil_code] def stored : Method := { - code := [.local 0, .pop, .arg 0, .ret] + code := [.ldloc 0, .pop, .ldarg 0, .ret] locals := [.nullRef], localKinds := [.reference], returnsValue := true } /-- `stored` as extracted with two unused reference locals after its own, as a profile-specific @@ -219,26 +223,26 @@ theorem stored_dead_returns (memory : Safety.Memory) (value : W32) : /-- Jumps to a throw. -/ def thrower : Method := - { code := [.branch 1, .throw "Overflow"], locals := [], returnsValue := false } + { code := [.br 1, .throw .overflow], locals := [], returnsValue := false } /-- `thrower` with a dead block before the throw and unused reference and span locals. -/ def throwerDead : Method := { - code := [.branch 2, .unsupported "Unreachable under selected profile", .throw "Overflow"] + code := [.br 2, .unadmitted 0, .throw .overflow] locals := [.nullRef, .nullRef], localKinds := [.reference, .span 1 false], returnsValue := false } /-- A throw of the expected body is raised by the extraction with the dead local. -/ theorem thrower_dead_raises (memory : Safety.Memory) : - Safety.Raises [throwerDead] 0 [] memory "Overflow" := + Safety.Raises [throwerDead] 0 [] memory .overflow := have embeds : MethodEmbedsDeadLocals [throwerDead] 0 thrower := by cil_embed embeds.raises ⟨2, _, rfl, rfl⟩ /-- The relocated fault names the throw's pc in the extraction, past the dead block. -/ theorem thrower_dead_fault (memory : Safety.Memory) : - Safety.invoke [throwerDead] 2 0 [] memory = .error ⟨0, 2, .thrown "Overflow"⟩ := by + Safety.invoke [throwerDead] 2 0 [] memory = .error ⟨0, 2, .thrown .overflow⟩ := by have embeds : EmbedsDeadLocals thrower throwerDead [0, 2] := by refine ⟨?_, rfl, rfl, rfl, rfl, rfl, rfl⟩ constructor <;> rfl - exact embeds.invoke_raise (index := 0) rfl (fault := ⟨0, 1, .thrown "Overflow"⟩) rfl rfl + exact embeds.invoke_raise (index := 0) rfl (fault := ⟨0, 1, .thrown .overflow⟩) rfl rfl -- A dead numeric local allocates a home and shifts later allocation identities. /-- @@ -359,7 +363,7 @@ example : MethodEmbedsDeadLocals /-- Returns argument 0 past an opaque 128-bit vector local it never reads. -/ @[cil_code] def vectorKept : Method := { - code := [.arg 0, .ret] + code := [.ldarg 0, .ret] locals := [.unmodeled], localKinds := [.vector128], opaqueVectorLocals := [0] returnsValue := true } @@ -389,13 +393,13 @@ theorem vector_dead_returns (memory : Safety.Memory) (value : W32) : error: cil_embed: the expected body does not embed into the extracted method method index 0 expected pc 2 maps to actual pc 2 - expected: CIL.Op.arg 0 none - actual: CIL.Op.arg 1 none + expected: CIL.Instruction.ldarg 0 + actual: CIL.Instruction.ldarg 1 the instructions differ -/ #guard_msgs (substring := true) in example : MethodEmbedsDeadLocals - [{ storedDead with code := [.local 0, .pop, .arg 1, .ret] }] 0 stored := by + [{ storedDead with code := [.ldloc 0, .pop, .ldarg 1, .ret] }] 0 stored := by cil_embed /-- A numeric dead local fails the kernel-checked condition itself, not only the tactic. -/ @@ -497,3 +501,244 @@ end DeadLocals #guard_msgs (whitespace := lax) in #print axioms numeric_dead_rejected end Tests.MethodEmbedding + +/-! Declared signatures, environments and link tables are embedding fields: an embedding that + differs in any of them is rejected, and the diagnostic names the first one. -/ +namespace Tests.MethodEmbedding.Metadata + +open CIL + +/-- Calls method 1 at pc 1 and enters a virtual table at pc 3. -/ +def caller : Method := { + code := [.ldarg 0, .call (Tests.callee 1) {}, .pop, .callvirt (.arity 0), .ret] + links := [(1, .direct 1), (3, .virtual [(.void, 2)])] + locals := [], returnsValue := false } + +/-- The pc map of a body onto itself. -/ +private def identityMap : List Nat := [0, 1, 2, 3, 4] + +/-- The body states both the direct body and the virtual table. -/ +theorem caller_links : caller.links = [(1, .direct 1), (3, .virtual [(.void, 2)])] := rfl + +#print axioms caller_links + +/-- A table written out in a literal wins over the default. -/ +example : ({ caller with links := [] } : Method).linkAt 1 = none := rfl + +/-- The first entry for a pc wins, as in `step`. -/ +example : ({ code := [], locals := [], returnsValue := false, links := + [(2, .direct 5), (2, .direct 6)] } : Method).linkAt 2 = some (.direct 5) := rfl + +example : caller.linkAt 0 = none := rfl +example : caller.linkAt 1 = some (.direct 1) := rfl + +/-- Copying a body with new code keeps its table, so a rewrite of the code states its links. -/ +example : ({ caller with code := [] } : Method).links = caller.links := rfl + +/-- The declared type of a 32-bit integer parameter. -/ +def int32 : Extraction.TypeExpr := .atom Extraction.runtimeScope "System.Int32" + +/-- An instance method's declared types start after `this`. -/ +def instanceSignature : DeclaredSignature := { + receiver := some .object, parameters := [int32], returnType := .void } + +example : instanceSignature.argumentType 0 = none := rfl +example : instanceSignature.argumentType 1 = some int32 := rfl +example : instanceSignature.argumentType 2 = none := rfl + +/-- A static method's first argument is its first declared parameter. -/ +example : { instanceSignature with receiver := none }.argumentType 0 = + some int32 := rfl +example : { instanceSignature with receiver := none }.argumentType 1 = none := rfl + +theorem caller_embeds : MethodEmbeds [caller] 0 caller := by cil_embed + +#print axioms caller_embeds + +/-- caller with another declared signature. -/ +private def otherSignature : Method := { caller with declared := instanceSignature } +/-- caller with other layout tables. -/ +private def otherEnvironment : Method := + { caller with environment := { layouts := #[⟨⟨4, []⟩, 4⟩] } } +/-- caller with another link for its call. -/ +private def otherLink : Method := + { caller with links := [(1, .direct 9), (3, .virtual [(.void, 2)])] } + +theorem declared_rejected : ¬ Embeds caller otherSignature identityMap := + fun embedded => absurd embedded.declared (by decide) + +theorem environment_rejected : ¬ Embeds caller otherEnvironment identityMap := + fun embedded => absurd embedded.environment (by decide) + +theorem links_rejected : ¬ Embeds caller otherLink identityMap := + fun embedded => absurd embedded.links (by decide +kernel) + +/-- info: 'Tests.MethodEmbedding.Metadata.declared_rejected' does not depend on any axioms -/ +#guard_msgs in #print axioms declared_rejected +/-- info: 'Tests.MethodEmbedding.Metadata.environment_rejected' depends on axioms: + [propext, Quot.sound] -/ +#guard_msgs (whitespace := lax) in #print axioms environment_rejected +/-- info: 'Tests.MethodEmbedding.Metadata.links_rejected' depends on axioms: [propext] -/ +#guard_msgs in #print axioms links_rejected + +/-- The diagnostic of a link difference names the actual pc of the call. -/ +example : (MethodBinding.embedFailure caller otherLink identityMap).map Prod.snd = + some (some 1) := by decide +kernel + +example : (MethodBinding.embedFailure caller otherSignature identityMap).map Prod.snd = + some none := by decide +kernel + +example : (MethodBinding.embedFailure caller otherEnvironment identityMap).map Prod.snd = + some none := by decide +kernel + +example : MethodBinding.embedFailure caller caller identityMap = none := by decide +kernel + +/-- A literal that does not bind is told which field differs, and what each side has. -/ +example : (MethodBinding.methodDifferences caller otherSignature).length = 1 := by + decide +kernel +example : (MethodBinding.methodDifferences caller otherEnvironment).length = 1 := by + decide +kernel +example : (MethodBinding.methodDifferences caller otherLink).length = 1 := by decide +kernel + +example : (MethodBinding.methodDifferences caller otherSignature).head?.map + (·.startsWith "field declared:") = some true := by decide +kernel +example : (MethodBinding.methodDifferences caller otherEnvironment).head?.map + (·.startsWith "field environment:") = some true := by decide +kernel +example : (MethodBinding.methodDifferences caller otherLink).head?.map + (·.startsWith "field links:") = some true := by decide +kernel + +/-- What an extraction emits when a dead block puts the call at pc 3. -/ +def moved : Method := { + code := [.ldarg 0, .br 3, .unadmitted 0, .call (Tests.callee 1) {}, .ret] + links := [(3, .direct 1)] + locals := [], returnsValue := true } + +/-- The body a proof uses, with its call at pc 2: `withCode` and that pc's link. -/ +def viaWithCode : Method := + { moved.withCode [.ldarg 0, .br 2, .call (Tests.callee 1), .ret] with links := [(2, .direct 1)] } + +/-- The same code through a structure update, which keeps the link table of the call at pc 3. -/ +def viaUpdate : Method := { moved with code := [.ldarg 0, .br 2, .call (Tests.callee 1), .ret] } + +theorem with_code_embeds : MethodEmbeds [moved] 0 viaWithCode := by cil_embed + +#print axioms with_code_embeds + +example : viaWithCode.linkAt 2 = some (.direct 1) := rfl +example : viaWithCode.linkAt 3 = none := rfl +example : viaUpdate.linkAt 2 = none := rfl + +/-- The update leaves the link at pc 3, so its call has no link and the embedding fails. -/ +theorem update_rejected : ¬ Embeds viaUpdate moved [0, 1, 3, 4] := + fun embedded => absurd embedded.links (by decide +kernel) + +#print axioms update_rejected + +-- The diagnostic of the rejected update names `withCode`. +#guard ((MethodBinding.embedFailure viaUpdate moved [0, 1, 3, 4]).map fun failure => + (failure.1.splitOn "withCode").length) == some 2 + +#guard ((MethodBinding.methodDifferences moved viaWithCode).map fun line => + (line.splitOn "withCode").length) == [1, 2] + +/-- The field operand of the static reads below. -/ +def staticFieldRef : CIL.Extraction.FieldRef := + { owner := .atom "Fixture" "Fixture", index := 0, type := .atom "Fixture" "System.Int64", + name := name!"Value" } + +/-- The static field the fixtures below read. -/ +def staticField : StaticDescriptor := + { identity := 1, field := staticFieldRef, bytes := List.replicate 8 0 } + +/-- What an extraction emits when a dead block puts a static read at pc 2. -/ +def staticMoved : Method := { + code := [.br 2, .unadmitted 0, + .ldsflda staticFieldRef, .pop, .ret] + locals := [], returnsValue := false, staticSites := [(2, staticField)] } + +/-- The body a proof uses, with its static read at pc 1; `withCode` leaves it no static sites. -/ +def staticWritten : Method := + staticMoved.withCode [.br 1, .ldsflda staticFieldRef, .pop, .ret] + +/-- Whether the first embedding failure of `expected` contains each of `phrases`. -/ +def failureMentions (expected : Method) (phrases : List String) : Bool := + match MethodBinding.embedFailure expected staticMoved [0, 2, 3, 4] with + | some (message, _) => phrases.all fun phrase => (message.splitOn phrase).length > 1 + | none => false + +-- Without sites the descriptor lists differ, and the message says what to supply. +#guard failureMentions staticWritten ["static descriptors", "staticSites :="] + +-- A site kept at the extraction's pc resolves no descriptor at the body's read. +#guard failureMentions { staticWritten with staticSites := [(2, staticField)] } + ["different static descriptor", "staticSites :="] + +theorem static_sites_embed : + MethodEmbeds [staticMoved] 0 { staticWritten with staticSites := [(1, staticField)] } := by + cil_embed + +#print axioms static_sites_embed + +/-- A table whose keys do not ascend, with a repeated pc whose first entry wins. -/ +def unsortedTable : List (Nat × Link) := [(3, .direct 1), (1, .direct 2), (3, .direct 9)] + +/-- The table takes the scan, since its keys do not ascend. -/ +example : Extraction.keysAscend 0 unsortedTable = false := by decide + +/-- The scan answers each query with the first entry for it, in query order. -/ +example : Extraction.linkReads unsortedTable [1, 3, 2] = + [some (.direct 2), some (.direct 1), none] := by decide + +/-- The walk over this table would stop at `(3, .direct 1)` for pc 1 and miss `(1, .direct 2)`, + which is why a table whose keys do not ascend takes the scan. -/ +example : Extraction.linkWalk unsortedTable unsortedTable 0 [1] ≠ + [1].map (linkLookup unsortedTable) := by decide + +/-- An ascending table read with decreasing queries, as a map that reorders blocks gives: the + walk starts again from the head at pc 1, below pc 3, and finds the entry it had passed. -/ +example : Extraction.linkReads [(1, .direct 2), (3, .direct 1)] [3, 1, 2] = + [some (.direct 1), some (.direct 2), none] := by decide + +/-- A table with a link at every even pc. -/ +def evenTable : List (Nat × Link) := (List.range 20).map fun index => (2 * index, .direct index) + +/-- Queries that descend at every other step, below and above each key, read as scans would. -/ +example : Extraction.linkWalk evenTable evenTable 0 + [7, 2, 30, 0, 39, 38, 1, 12, 11, 40, 5, 4] = + [7, 2, 30, 0, 39, 38, 1, 12, 11, 40, 5, 4].map (linkLookup evenTable) := by decide + +/-- Fully descending queries read as scans would. -/ +example : Extraction.linkWalk evenTable evenTable 0 (List.range 42).reverse = + (List.range 42).reverse.map (linkLookup evenTable) := by decide + +/-- An extraction that places the block the head branches to before the block after the head. -/ +def reorderedExtraction : Method := { + code := [.br 1, .ldarg 1, .call (Tests.callee 1) {}, .pop, .ret, .ldarg 0, + .call (Tests.callee 1) {}, .pop, .ret] + links := [(2, .direct 2), (6, .direct 1)] + locals := [], returnsValue := false } + +/-- The body a proof uses, with the blocks in source order. -/ +def reorderedWritten : Method := + { reorderedExtraction.withCode [.br 5, .ldarg 0, .call (Tests.callee 1), .pop, .ret, .ldarg 1, + .call (Tests.callee 1), .pop, .ret] with + links := [(2, .direct 1), (6, .direct 2)] } + +-- The map sends the second block back to pc 1, so the link reads descend once. +#guard MethodBinding.alignLive reorderedWritten.code.toArray reorderedExtraction.code.toArray == + #[0, 5, 6, 7, 8, 1, 2, 3, 4] + +theorem reordered_embeds : MethodEmbeds [reorderedExtraction] 0 reorderedWritten := by cil_embed + +#print axioms reordered_embeds + +/-- Where the walk never descends it answers as the scan does. -/ +example : Extraction.linkReads [(1, .direct 2), (3, .direct 1)] [1, 2, 3] = + [1, 2, 3].map (linkLookup [(1, .direct 2), (3, .direct 1)]) := by decide + +/-- Whichever branch runs, the reads are the per-query lookups. -/ +example : Extraction.linkReads unsortedTable [1, 3, 2] = + [1, 3, 2].map (linkLookup unsortedTable) := + Extraction.linkReads_eq unsortedTable _ + +end Tests.MethodEmbedding.Metadata diff --git a/Tests/MethodIdentity.cs b/Tests/MethodIdentity.cs index e0fe20115..96818d287 100644 --- a/Tests/MethodIdentity.cs +++ b/Tests/MethodIdentity.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.RegularExpressions; @@ -18,7 +23,8 @@ string Build(string name, string body) var source = Path.Combine(root, name); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Identity.csproj"), """ - net10.0true + net11.0 + true """); File.WriteAllText(Path.Combine(source, "Identity.cs"), "namespace Identity; public static class Calls { " + body + " }"); Run(["dotnet", "build", Path.Combine(source, "Identity.csproj"), "-c", "Release"]); @@ -130,3 +136,4 @@ end ExampleProof } if (!failed) throw new InvalidOperationException("A positional proof survived renumbering; the reordered build does not exercise identity."); Console.WriteLine("PASS positional indices break on renumbering; signatures do not"); +Run(["dotnet", "run", "--file", At("Tests/Identities.cs")], streamOutput: true, progress: "Typed identity display"); diff --git a/Tests/MethodIdentity.lean b/Tests/MethodIdentity.lean index 8afa0ff43..b983fd2cd 100644 --- a/Tests/MethodIdentity.lean +++ b/Tests/MethodIdentity.lean @@ -1,5 +1,6 @@ import CIL.MethodIdentity import CIL.StructuralLabels +import Tests.Support /-! A synthetic extraction: `Root` calls two helpers with the same arity, `Right` first. -/ namespace Extracted @@ -11,8 +12,13 @@ def method1Signature : String := "System.UInt64 Examples.Identity::Left(System.U def method2Signature : String := "System.UInt64 Examples.Identity::Right(System.UInt64,System.UInt64)" def method3Signature : String := "System.UInt64 Examples.Overloads::Pick(System.UInt64)" def method4Signature : String := "System.UInt32 Examples.Overloads::Pick(System.UInt32)" -def helper (marker : Nat) : Method := { code := [.arg 0, .arg 1, .const64 (BitVec.ofNat 64 marker), .ret], locals := [], returnsValue := true } -def method0 : Method := { code := [.arg 0, .arg 0, .call 2 2, .arg 0, .call 1 2, .ret], locals := [], returnsValue := true } +def helper (marker : Nat) : Method := + { code := [.ldarg 0, .ldarg 1, .ldc_i8 (BitVec.ofNat 64 marker), .ret], locals := [], + returnsValue := true } +def method0 : Method := + { code := [.ldarg 0, .ldarg 0, .call (Tests.callee 2) {}, .ldarg 0, .call (Tests.callee 2) {}, + .ret], links := [(2, .direct 2), (4, .direct 1)], locals := [], + returnsValue := true } def method1 : Method := helper 1 def method2 : Method := helper 2 def method3 : Method := helper 3 @@ -39,13 +45,14 @@ bind_callee right rightBody := "System.UInt64 Examples.Identity::Right(System.UI -- Same arity, called in the other order: each name still reaches its own method and site. example : left = 1 ∧ right = 2 := ⟨rfl, rfl⟩ example : leftBody = Extracted.helper 1 ∧ rightBody = Extracted.helper 2 := ⟨rfl, rfl⟩ -example : rootBody.code[4]? = some (.call left 2) := left.call -example : rootBody.code[2]? = some (.call right 2) := right.call +example : rootBody.code[4]? = some (.call (Tests.callee 2)) := left.call +example : rootBody.code[2]? = some (.call (Tests.callee 2)) := right.call +example : rootBody.linkAt 4 = some (.direct left) := left.link example : Extracted.signatures[right]? = some "System.UInt64 Examples.Identity::Right(System.UInt64,System.UInt64)" := right.signature -- Generated bindings are kernel-checked `rfl`, never elaborator-only evidence. run_cmd do - for name in [``left.signature, ``left.found, ``left.call] do + for name in [``left.signature, ``left.found, ``left.call, ``left.link] do let some (.thmInfo info) := (← Lean.getEnv).find? name | throwError "missing {name}" unless info.value.isAppOf ``Eq.refl || info.value.isAppOf ``rfl do throwError "{name} is not rfl: {info.value}" @@ -87,7 +94,7 @@ bind_callee pick pickBody := "System.UInt64 Examples.Overloads::Pick(System.UInt /-- The second two-argument callee. -/ bind_callee second secondBody := call 2 from rootBody [1] example : second = left ∧ secondBody = leftBody := ⟨rfl, rfl⟩ -example : rootBody.code[4]? = some (.call second 2) := second.call +example : rootBody.code[4]? = some (.call (Tests.callee 2)) := second.call example : Extracted.signatures[second]? = some "System.UInt64 Examples.Identity::Left(System.UInt64,System.UInt64)" := second.signature run_cmd do @@ -244,7 +251,9 @@ end BatchCase0 namespace ImportCase.Extracted def method0Signature : String := "System.Void Examples.Imports::Root(System.UInt64&,System.UInt64&)" -def method0 : CIL.Method := { code := [.arg 0, .arg 1, .arg 0, .call 1 3, .ret], locals := [], returnsValue := false } +def method0 : CIL.Method := + { code := [.ldarg 0, .ldarg 1, .ldarg 0, .call (Tests.callee 3) {}, .ret], + links := [(3, .direct 1)], locals := [], returnsValue := false } def program : CIL.Program := [method0] def signatures : List String := [method0Signature] def importSignatures : List String := ["System.Void Library.Adder::AddTo(System.UInt64&,System.UInt64&,System.UInt64&)"] @@ -257,6 +266,6 @@ bind_method root rootBody := "System.Void Examples.Imports::Root(System.UInt64&, bind_import addTo := "System.Void Library.Adder::AddTo(System.UInt64&,System.UInt64&,System.UInt64&)" bind_call addSite := addTo from rootBody example : addTo = 1 ∧ addSite = 3 := ⟨rfl, rfl⟩ -example : rootBody.code[addSite]? = some (.call addTo 3) := addSite.call +example : rootBody.code[addSite]? = some (.call (Tests.callee 3)) := addSite.call end ImportCase diff --git a/Tests/MethodSelectors.cs b/Tests/MethodSelectors.cs index dbf8940ec..d61097332 100644 --- a/Tests/MethodSelectors.cs +++ b/Tests/MethodSelectors.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; diff --git a/Tests/MixedNativeOpcodes.lean b/Tests/MixedNativeOpcodes.lean new file mode 100644 index 000000000..cb2d91478 --- /dev/null +++ b/Tests/MixedNativeOpcodes.lean @@ -0,0 +1,253 @@ +import CIL.Safety.Execution +import CIL.Extraction.Scalars + +/-! +# Mixed native arithmetic + +Bare decoded arithmetic instructions widen admitted mixed operands. The reference step and the +checked step agree on every instruction `widenMixed` widens, in both operand orders, and on +division and overflow-checked arithmetic. Scalar admission keeps native integers distinct from +fixed-width integers and rejects mixed unsigned branches. +-/ + +namespace Tests.MixedNativeOpcodes + +open CIL CIL.Safety CIL.Extraction + +/-- A scalar body with no frame storage. -/ +private def body : Method := { returnsValue := true, locals := [], code := [] } + +/-- The empty activation used by scalar steps. -/ +private def frame : Frame := { activation := 0, locals := [], owned := [] } + +/-- Mixed addition sign-extends the int32 operand in either stack position. -/ +theorem add_mixed : + step body .add 0 [] frame [.scalar (.i64 5), .scalar (.i32 0xFFFFFFFF)] Memory.empty = + .ok (.next 1 [.scalar (.i64 4)] frame Memory.empty) ∧ + step body .add 0 [] frame [.scalar (.i32 0xFFFFFFFF), .scalar (.i64 5)] Memory.empty = + .ok (.next 1 [.scalar (.i64 4)] frame Memory.empty) := by + constructor <;> rfl +#print axioms add_mixed + +/-- Unsigned checked multiplication zero-extends the int32 operand in either position. -/ +theorem multiply_unsigned_mixed : + step body .mul_ovf_un 0 [] frame [.scalar (.i32 0x80000000), .scalar (.i64 2)] + Memory.empty = .ok (.next 1 [.scalar (.i64 0x100000000)] frame Memory.empty) ∧ + step body .mul_ovf_un 0 [] frame [.scalar (.i64 2), .scalar (.i32 0x80000000)] + Memory.empty = .ok (.next 1 [.scalar (.i64 0x100000000)] frame Memory.empty) := by + constructor <;> rfl +#print axioms multiply_unsigned_mixed + +/-- Unsigned division sign-extends the mixed int32 numerator before dividing. -/ +theorem divide_unsigned_mixed : + step body .div_un 0 [] frame [.scalar (.i64 2), .scalar (.i32 0xFFFFFFFF)] + Memory.empty = .ok (.next 1 [.scalar (.i64 0x7FFFFFFFFFFFFFFF)] frame Memory.empty) := by + rfl +#print axioms divide_unsigned_mixed + +/-- A mixed unsigned comparison branch remains unsupported. -/ +theorem unsigned_branch_refused : + step body (.blt_un 4) 0 [] frame [.scalar (.i64 2), .scalar (.i32 1)] Memory.empty = + .error .invalidState := by + rfl +#print axioms unsigned_branch_refused + +/-- Mixed signed division retains both arithmetic fault conditions. -/ +theorem divide_mixed_faults : + step body .div 0 [] frame [.scalar (.i32 0), .scalar (.i64 7)] Memory.empty = + .error (.arithmetic .divideByZero) ∧ + step body .div 0 [] frame [.scalar (.i32 0xFFFFFFFF), .scalar (.i64 0x8000000000000000)] + Memory.empty = .error (.arithmetic .overflow) := by + constructor <;> rfl +#print axioms divide_mixed_faults + +/-- Mixed unsigned remainder sign-extends the int32 numerator. -/ +theorem remainder_unsigned_mixed : + step body .rem_un 0 [] frame [.scalar (.i64 2), .scalar (.i32 0xFFFFFFFF)] + Memory.empty = .ok (.next 1 [.scalar (.i64 1)] frame Memory.empty) := by + rfl +#print axioms remainder_unsigned_mixed + +/-- Mixed checked addition preserves signed and unsigned overflow faults. -/ +theorem addition_mixed_overflow : + step body .add_ovf 0 [] frame [.scalar (.i32 1), .scalar (.i64 0x7FFFFFFFFFFFFFFF)] + Memory.empty = .error (.arithmetic .overflow) ∧ + step body .add_ovf_un 0 [] frame [.scalar (.i64 0xFFFFFFFFFFFFFFFF), .scalar (.i32 1)] + Memory.empty = .error (.arithmetic .overflow) := by + constructor <;> rfl +#print axioms addition_mixed_overflow + +/-- Bare addition admits native/int32 pairs and rejects fixed-width int64/int32 pairs. -/ +theorem addition_admission : + scalarOpMatches none ⟨0, 88, .none⟩ [.word32, .native] .add = true ∧ + scalarOpMatches none ⟨0, 88, .none⟩ [.native, .word32] .add = true ∧ + scalarOpMatches none ⟨0, 88, .none⟩ [.word32, .word64] .add = false ∧ + scalarOpMatches none ⟨0, 88, .none⟩ [.word64, .word32] .add = false := by + decide +#print axioms addition_admission + +/-- The reference step and the checked step both replace `right` over `left` with `result` + and resume at the next instruction, whatever lies beneath the pair. -/ +def Agree (op : Instruction) (left right result : CIL.Value) : Prop := + (∀ (pc : Nat) (rest : List CIL.Value) (memory : CIL.Memory), + CIL.step op false none pc (right :: left :: rest) memory = + some (.next (pc + 1) (result :: rest) memory)) ∧ + ∀ (body : Method) (pc : Nat) (args : List Safety.Value) (frame : Frame) + (rest : List Safety.Value) (memory : Safety.Memory), + step body op pc args frame (.scalar right :: .scalar left :: rest) memory = + .ok (.next (pc + 1) (.scalar result :: rest) frame memory) + +/-- The reference step and the checked step of the compare-and-branch `op` both jump to its + target exactly when `taken` holds for `right` over `left`, consuming the pair. -/ +def Branches (op : Nat → Instruction) (left right : CIL.Value) (taken : Prop) [Decidable taken] : + Prop := + (∀ (target pc : Nat) (rest : List CIL.Value) (memory : CIL.Memory), + CIL.step (op target) false none pc (right :: left :: rest) memory = + some (.next (if taken then target else pc + 1) rest memory)) ∧ + ∀ (body : Method) (target pc : Nat) (args : List Safety.Value) (frame : Frame) + (rest : List Safety.Value) (memory : Safety.Memory), + step body (op target) pc args frame (.scalar right :: .scalar left :: rest) memory = + .ok (.next (if taken then target else pc + 1) rest frame memory) + +/-- Wrapping arithmetic and bitwise operations sign-extend the int32 operand of a mixed pair in + either stack position and compute at native width (ECMA-335 III.1.5). -/ +theorem signed_arithmetic (int32 : W32) (native : W64) : + Agree .add (.i32 int32) (.i64 native) (.i64 (int32.signExtend 64 + native)) ∧ + Agree .add (.i64 native) (.i32 int32) (.i64 (native + int32.signExtend 64)) ∧ + Agree .sub (.i32 int32) (.i64 native) (.i64 (int32.signExtend 64 - native)) ∧ + Agree .sub (.i64 native) (.i32 int32) (.i64 (native - int32.signExtend 64)) ∧ + Agree .mul (.i32 int32) (.i64 native) (.i64 (int32.signExtend 64 * native)) ∧ + Agree .mul (.i64 native) (.i32 int32) (.i64 (native * int32.signExtend 64)) ∧ + Agree .and (.i32 int32) (.i64 native) (.i64 (int32.signExtend 64 &&& native)) ∧ + Agree .and (.i64 native) (.i32 int32) (.i64 (native &&& int32.signExtend 64)) ∧ + Agree .or (.i32 int32) (.i64 native) (.i64 (int32.signExtend 64 ||| native)) ∧ + Agree .or (.i64 native) (.i32 int32) (.i64 (native ||| int32.signExtend 64)) ∧ + Agree .xor (.i32 int32) (.i64 native) (.i64 (int32.signExtend 64 ^^^ native)) ∧ + Agree .xor (.i64 native) (.i32 int32) (.i64 (native ^^^ int32.signExtend 64)) := by + unfold Agree + and_intros <;> intros <;> rfl +#print axioms signed_arithmetic + +/-- Signed comparisons compare the sign-extended int32 operand with the native operand. -/ +theorem signed_comparisons (int32 : W32) (native : W64) : + Agree .ceq (.i32 int32) (.i64 native) + (.i32 (if int32.signExtend 64 = native then 1 else 0)) ∧ + Agree .ceq (.i64 native) (.i32 int32) + (.i32 (if native = int32.signExtend 64 then 1 else 0)) ∧ + Agree .clt (.i32 int32) (.i64 native) + (.i32 (if (int32.signExtend 64).toInt < native.toInt then 1 else 0)) ∧ + Agree .clt (.i64 native) (.i32 int32) + (.i32 (if native.toInt < (int32.signExtend 64).toInt then 1 else 0)) ∧ + Agree .cgt (.i32 int32) (.i64 native) + (.i32 (if native.toInt < (int32.signExtend 64).toInt then 1 else 0)) ∧ + Agree .cgt (.i64 native) (.i32 int32) + (.i32 (if (int32.signExtend 64).toInt < native.toInt then 1 else 0)) := by + unfold Agree + and_intros <;> intros <;> rfl +#print axioms signed_comparisons + +/-- Unsigned comparisons also sign-extend the int32 operand, then compare unsigned. -/ +theorem unsigned_comparisons (int32 : W32) (native : W64) : + Agree .clt_un (.i32 int32) (.i64 native) + (.i32 (if int32.signExtend 64 < native then 1 else 0)) ∧ + Agree .clt_un (.i64 native) (.i32 int32) + (.i32 (if native < int32.signExtend 64 then 1 else 0)) ∧ + Agree .cgt_un (.i32 int32) (.i64 native) + (.i32 (if native < int32.signExtend 64 then 1 else 0)) ∧ + Agree .cgt_un (.i64 native) (.i32 int32) + (.i32 (if int32.signExtend 64 < native then 1 else 0)) := by + unfold Agree + and_intros <;> intros <;> rfl +#print axioms unsigned_comparisons + +/-- Signed compare-and-branch instructions compare the sign-extended int32 operand. -/ +theorem signed_branches (int32 : W32) (native : W64) : + Branches .beq (.i32 int32) (.i64 native) (int32.signExtend 64 = native) ∧ + Branches .beq (.i64 native) (.i32 int32) (native = int32.signExtend 64) ∧ + Branches .blt (.i32 int32) (.i64 native) ((int32.signExtend 64).toInt < native.toInt) ∧ + Branches .blt (.i64 native) (.i32 int32) (native.toInt < (int32.signExtend 64).toInt) ∧ + Branches .ble (.i32 int32) (.i64 native) ((int32.signExtend 64).toInt ≤ native.toInt) ∧ + Branches .ble (.i64 native) (.i32 int32) (native.toInt ≤ (int32.signExtend 64).toInt) ∧ + Branches .bgt (.i32 int32) (.i64 native) (native.toInt < (int32.signExtend 64).toInt) ∧ + Branches .bgt (.i64 native) (.i32 int32) ((int32.signExtend 64).toInt < native.toInt) ∧ + Branches .bge (.i32 int32) (.i64 native) (native.toInt ≤ (int32.signExtend 64).toInt) ∧ + Branches .bge (.i64 native) (.i32 int32) ((int32.signExtend 64).toInt ≤ native.toInt) := by + unfold Branches + and_intros <;> intros <;> rfl +#print axioms signed_branches + +/-- Overflow-checked arithmetic sign-extends the int32 operand of a mixed pair for the signed + forms and zero-extends it for the unsigned forms; both interpreters agree in either order. -/ +theorem checked_arithmetic : + Agree .add_ovf (.i32 0xFFFFFFFF) (.i64 5) (.i64 4) ∧ + Agree .add_ovf (.i64 5) (.i32 0xFFFFFFFF) (.i64 4) ∧ + Agree .add_ovf_un (.i32 0xFFFFFFFF) (.i64 5) (.i64 0x100000004) ∧ + Agree .add_ovf_un (.i64 5) (.i32 0xFFFFFFFF) (.i64 0x100000004) ∧ + Agree .sub_ovf (.i32 0xFFFFFFFF) (.i64 5) (.i64 0xFFFFFFFFFFFFFFFA) ∧ + Agree .sub_ovf (.i64 5) (.i32 0xFFFFFFFF) (.i64 6) ∧ + Agree .sub_ovf_un (.i32 0xFFFFFFFF) (.i64 5) (.i64 0xFFFFFFFA) ∧ + Agree .sub_ovf_un (.i64 0x100000005) (.i32 0xFFFFFFFF) (.i64 6) ∧ + Agree .mul_ovf (.i32 0xFFFFFFFF) (.i64 5) (.i64 0xFFFFFFFFFFFFFFFB) ∧ + Agree .mul_ovf (.i64 5) (.i32 0xFFFFFFFF) (.i64 0xFFFFFFFFFFFFFFFB) ∧ + Agree .mul_ovf_un (.i32 0xFFFFFFFF) (.i64 5) (.i64 0x4FFFFFFFB) ∧ + Agree .mul_ovf_un (.i64 5) (.i32 0xFFFFFFFF) (.i64 0x4FFFFFFFB) := by + unfold Agree + and_intros <;> intros <;> rfl +#print axioms checked_arithmetic + +/-- Division and remainder sign-extend the int32 operand of a mixed pair, as numerator or + divisor, for the signed and the unsigned forms; both interpreters agree in either order. -/ +theorem division : + Agree .div (.i32 0xFFFFFFF9) (.i64 2) (.i64 0xFFFFFFFFFFFFFFFD) ∧ + Agree .div (.i64 7) (.i32 0xFFFFFFFE) (.i64 0xFFFFFFFFFFFFFFFD) ∧ + Agree .div_un (.i32 0xFFFFFFF9) (.i64 2) (.i64 0x7FFFFFFFFFFFFFFC) ∧ + Agree .div_un (.i64 0xFFFFFFFFFFFFFFFF) (.i32 0xFFFFFFFE) (.i64 1) ∧ + Agree .rem (.i32 0xFFFFFFF9) (.i64 2) (.i64 0xFFFFFFFFFFFFFFFF) ∧ + Agree .rem (.i64 7) (.i32 0xFFFFFFFE) (.i64 1) ∧ + Agree .rem_un (.i32 0xFFFFFFF9) (.i64 2) (.i64 1) ∧ + Agree .rem_un (.i64 0xFFFFFFFFFFFFFFFF) (.i32 0xFFFFFFFE) (.i64 1) := by + unfold Agree + and_intros <;> intros <;> rfl +#print axioms division + +/-- Outside the eleven listed instructions and the five compare-and-branch forms, `widenMixed` + leaves every stack as it is. -/ +theorem widened_instructions (op : Instruction) (stack : List CIL.Value) : + (op ∈ [.add, .sub, .mul, .and, .or, .xor, .ceq, .clt, .cgt, .clt_un, .cgt_un] ∨ + ∃ target, op ∈ [.beq target, .blt target, .ble target, .bgt target, .bge target]) ∨ + widenMixed op stack = stack := by + cases op <;> simp [widenMixed, nativePureBinary] +#print axioms widened_instructions + +/-- An instruction `widenMixed` widens leaves the stack as it is unless a 64-bit operand lies + on top of a 32-bit one or the reverse, so equal kinds, and a float, reference or vector on + top, are unchanged whatever lies beneath. -/ +theorem widened_unchanged_unless_mixed (op : Instruction) (widened : nativePureBinary op = true) + (top below : CIL.Value) (rest : List CIL.Value) + (nativeOverInt32 : ∀ native int32, ¬(top = .i64 native ∧ below = .i32 int32)) + (int32OverNative : ∀ int32 native, ¬(top = .i32 int32 ∧ below = .i64 native)) : + widenMixed op (top :: below :: rest) = top :: below :: rest := by + unfold widenMixed + rw [widened] + split + · next _ _ _ heq => + cases heq + exact absurd ⟨rfl, rfl⟩ (nativeOverInt32 _ _) + · next _ _ _ heq => + cases heq + exact absurd ⟨rfl, rfl⟩ (int32OverNative _ _) + · rfl +#print axioms widened_unchanged_unless_mixed + +/-- Unsigned branches refuse a mixed pair in both interpreters and both operand orders. -/ +theorem unsigned_branches_refused (int32 : W32) (native : W64) : + ([Instruction.bne_un 9, .blt_un 9, .bgt_un 9, .ble_un 9, .bge_un 9].all fun op => + (CIL.step op false none 0 [.i32 int32, .i64 native] (fun _ => none)).isNone && + (CIL.step op false none 0 [.i64 native, .i32 int32] (fun _ => none)).isNone && + !(step body op 0 [] frame [.scalar (.i32 int32), .scalar (.i64 native)] Memory.empty).isOk && + !(step body op 0 [] frame [.scalar (.i64 native), .scalar (.i32 int32)] Memory.empty).isOk) = + true := by + rfl +#print axioms unsigned_branches_refused + +end Tests.MixedNativeOpcodes diff --git a/Tests/ModelOperandChecks.cs b/Tests/ModelOperandChecks.cs new file mode 100644 index 000000000..e6b02683d --- /dev/null +++ b/Tests/ModelOperandChecks.cs @@ -0,0 +1,99 @@ +using Mono.Cecil; + +// Each witness starts from the caller's real MemberRef and changes one field per refusal. +internal static class ModelOperandChecks +{ + internal static void Append(string proof, MethodDefinition method, Func expectedModel, + bool needsFeature = false) + { + File.AppendAllText(proof, """ + + namespace Extracted + open CIL + /-- The raw operands of the example, indexed by instruction position. -/ + private def fixtureOperandCode : Array Extraction.RawInstruction := method0Closed.code.toArray + end Extracted + + """); + bool witnessed = false; + foreach (var (pc, instruction) in method.Body.Instructions.Index()) + { + if (instruction.Operand is not MethodReference reference || expectedModel(reference) is not { } model) + continue; + witnessed = true; + var availability = needsFeature ? "refusedUnder .scalar site &&" : ""; + File.AppendAllText(proof, $$""" + + namespace Extracted.Site{{pc}} + open CIL CIL.Extraction + /-- The decoder keeps this live call and refuses each altered operand as a live hole at pc 0. -/ + theorem operand_checked : + (match (fixtureOperandCode[{{pc}}]? : Option RawInstruction) with + | some { operand := .call site, .. } => + let source := fun (operand : CallSite) => + { method0Closed with code := [RawInstruction.mk 0 40 (.call operand)] } + let decode := fun features operand => + (Method.ofRaw? features environment [] [true] (source operand) []).map Method.code + let refusal := fun features operand => + match Method.ofClosed? features environment [] [true] (source operand) + method0RawArguments with + | .error error => some error + | .ok _ => none + let refusedUnder := fun features operand => + decide (decode features operand = none) && + decide (refusal features operand = some (.liveHole 0 40)) && + decide (methodCode features environment [] [true] (source operand) = [.unadmitted 40]) + let foreign := match site.method.owner with + | .atom _ name => TypeExpr.atom "Foreign" name + | .apply (.constructor _ name) element => + .apply (.constructor "Foreign" name) element + | owner => owner + let floating := TypeExpr.atom runtimeScope "System.Double" + let floatingSite : Option CallSite := do + let result <- substituteType [floating] site.declared.returnType + let parameters <- site.declared.parameters.mapM (substituteType [floating]) + return { site with + method.returnType := result, method.parameters := parameters, + method.methodArguments := [floating], typeArguments := [floating] } + let ownerElement : Option CallSite := do + let .apply head (.atom _ original) := site.method.owner | none + let .external identity := site.target | none + let changed := TypeExpr.apply head floating + let close := fun type => if type == site.method.owner then changed else type + return { site with + method.owner := changed, + method.returnType := close site.method.returnType, + method.parameters := site.parameterTypes.map close, + target := .external (identity.replace original "System.Double") } + let refused := refusedUnder profile + let bad : List CallSite := [ + { site with method.owner := foreign }, + { site with name := "Unknown" }, { site with method.name := name!"Unknown" }, + { site with method.genericArity := site.method.genericArity + 1 }, + { site with method.parameters := (.pointer .void) :: site.parameterTypes.drop 1 }, + { site with method.returnType := .pointer .void }, + { site with declared.convention := 32 }, + { site with declared.parameters := .void :: site.declared.parameters }, + { site with typeArguments := [.atom runtimeScope "System.Double"] }, + { site with target := .external "Other overload" }] + decide (decode profile site = some [.call site.method {}]) && {{availability}} + bad.all refused && ownerElement.all refused && + (site.method.genericArity == 0 || floatingSite.any refused) + | _ => false) = true := by cil_kernel_rfl + #print axioms operand_checked + /-- The generated model fact names the expected checked operation and all its operands. -/ + theorem checked_model : + (match (fixtureOperandCode[{{pc}}]? : Option RawInstruction) with + | some { operand := .call site, .. } => runtimeModel environment .call site.method + | _ => none) = some ({{model}}) := by + -- The elaborator would index the literal code list to its end, so the kernel compares the sides. + have _fact : runtimeModel environment .call _ = some ({{model}}) := method0_model{{pc}} + cil_kernel_rfl + #print axioms checked_model + end Extracted.Site{{pc}} + + """); + } + if (!witnessed) throw new InvalidDataException($"No model operands in {method.FullName}"); + } +} diff --git a/Tests/ModifierIdentities.cs b/Tests/ModifierIdentities.cs new file mode 100644 index 000000000..46ba21ac3 --- /dev/null +++ b/Tests/ModifierIdentities.cs @@ -0,0 +1,264 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:package Mono.Cecil +#:include PinnedMetadata.cs + +using System.Collections; +using System.Diagnostics; +using System.Reflection; +using Mono.Cecil; + +var root = Path.GetFullPath("artifacts/ModifierIdentities"); +Directory.CreateDirectory(root); +var extractor = Assembly.LoadFrom(Path.GetFullPath("Extractor/bin/Nethermind.LeanCIL.dll")); +const BindingFlags Static = BindingFlags.Static | BindingFlags.NonPublic; +const BindingFlags Instance = BindingFlags.Instance | BindingFlags.NonPublic; +var inUnwrap = extractor.GetType("Nethermind.LeanCIL.InReferences", true)! + .GetMethod("Unwrap", Static)!.CreateDelegate>(); +var initUnwrap = extractor.GetType("Nethermind.LeanCIL.InitAccessors", true)! + .GetMethod("Unwrap", Static)!.CreateDelegate>(); + +void Require(bool condition, string message) +{ + if (!condition) throw new Exception(message); +} + +AssemblyNameReference Runtime(ModuleDefinition module, int version = 11) +{ + var runtime = new AssemblyNameReference("System.Runtime", new(version, 0, 0, 0)) + { + PublicKeyToken = Convert.FromHexString("b03f5f7f11d50a3a") + }; + module.AssemblyReferences.Add(runtime); + return runtime; +} + +void Reject(Func action) +{ + try { action(); } + catch (InvalidDataException) { return; } + throw new Exception("Missing or malformed provenance was accepted"); +} + +void CheckMarkers(bool initialization) +{ + string space = initialization ? "System.Runtime.CompilerServices" : "System.Runtime.InteropServices"; + string name = initialization ? "IsExternalInit" : "InAttribute"; + string path = Path.Combine(root, initialization ? "Init.dll" : "In.dll"); + using (var assembly = AssemblyDefinition.CreateAssembly(new("Modifiers", new(1, 0)), + "Modifiers", ModuleKind.Dll)) + { + var module = assembly.MainModule; + var runtime = Runtime(module); + var owner = new TypeDefinition("Fixture", "Owner", Mono.Cecil.TypeAttributes.Public); + module.Types.Add(owner); + TypeReference element = initialization ? module.TypeSystem.Void : new ByReferenceType(module.TypeSystem.Int32); + void Field(string label, TypeReference marker, TypeReference? shape = null) => + owner.Fields.Add(new(label, Mono.Cecil.FieldAttributes.Public, + new RequiredModifierType(marker, shape ?? element))); + var genuine = new TypeReference(space, name, module, runtime); + Field("Genuine", genuine); + Field("Flattened", new TypeReference("", space + "." + name, module, runtime)); + Field("WrongVersion", new TypeReference(space, name, module, Runtime(module, 9))); + var foreign = new AssemblyNameReference("Other", new(1, 0)); + module.AssemblyReferences.Add(foreign); + Field("Foreign", new TypeReference(space, name, module, foreign)); + Field("NestedRef", new TypeReference(space, name, module, runtime) + { + DeclaringType = new TypeReference("Fixture", "Outer", module, runtime) + }); + var local = new TypeDefinition(space, name, Mono.Cecil.TypeAttributes.Public); + module.Types.Add(local); + Field("Local", local); + var nested = new TypeDefinition(space, name, Mono.Cecil.TypeAttributes.NestedPublic); + owner.NestedTypes.Add(nested); + Field("NestedDef", nested); + Field("WrongShape", genuine, module.TypeSystem.Int32); + owner.Fields.Add(new("Optional", Mono.Cecil.FieldAttributes.Public, + new OptionalModifierType(genuine, element))); + if (!initialization) + Field("InnerModifier", genuine, + new ByReferenceType(new OptionalModifierType(genuine, module.TypeSystem.Int32))); + assembly.Write(path); + } + TypeReference escaped; + using (var pinned = new PinnedMetadata(extractor, path)) + { + bool Accepted(TypeReference type) => + initialization ? initUnwrap(type) is not null : inUnwrap(type) is not null; + var fields = pinned.Module.GetType("Fixture.Owner").Fields; + foreach (var field in fields) + { + bool expected = field.Name == "Genuine" || initialization && + field.Name is "Local" or "WrongVersion" or "Foreign"; + bool accepted = Accepted(field.FieldType); + Require(accepted == expected, $"{name}: {field.Name} accepted={accepted}, expected={expected}"); + } + escaped = fields[0].FieldType; + var genuine = ((RequiredModifierType)escaped).ModifierType; + genuine.Namespace = "Mutated"; + genuine.Name = "Unrelated"; + genuine.Scope = pinned.Module; + Require(Accepted(escaped), "Mutable name or scope changed the genuine raw marker identity"); + var collisionType = fields.Single(field => field.Name == "Flattened").FieldType; + var collision = ((RequiredModifierType)collisionType).ModifierType; + collision.Namespace = space; + collision.Name = name; + collision.Scope = pinned.Module.AssemblyReferences.First(reference => reference.Name == "System.Runtime"); + Require(!Accepted(collisionType), "Mutable display fields supplied missing raw identity"); + var token = genuine.MetadataToken; + foreach (uint invalid in new uint[] { 0x01000000, 0x01ffffff, 0x06000001 }) + { + genuine.MetadataToken = new MetadataToken(invalid); + Reject(() => Accepted(escaped)); + } + genuine.MetadataToken = token; + } + Reject(() => initialization ? initUnwrap(escaped) is not null : inUnwrap(escaped) is not null); + Console.WriteLine($"PASS {name}: raw columns, scopes, nesting, shapes, mutations and provenance"); +} + +CheckMarkers(false); +CheckMarkers(true); + +RowFixture PrepareRows(int count, int nameLength) +{ + string path = Path.Combine(root, $"Rows-{count}-{nameLength}.dll"); + using (var assembly = AssemblyDefinition.CreateAssembly(new("Rows", new(1, 0)), "Rows", ModuleKind.Dll)) + { + var module = assembly.MainModule; + var runtime = Runtime(module); + var owner = new TypeDefinition("Fixture", "Owner", Mono.Cecil.TypeAttributes.Public); + module.Types.Add(owner); + for (int index = 0; index < count; index++) + { + var marker = new TypeReference("Fixture", new string('X', nameLength) + index, module, runtime); + owner.Fields.Add(new("F" + index, Mono.Cecil.FieldAttributes.Public, + new RequiredModifierType(marker, module.TypeSystem.Void))); + } + assembly.Write(path); + } + var pinned = new PinnedMetadata(extractor, path); + try + { + var metadataMethod = pinned.Context.GetType().GetMethod("IdentityMetadata", Instance)!; + object metadata = metadataMethod.Invoke(pinned.Context, [pinned.Module])!; + var query = metadata.GetType().GetMethod("TopLevelTypeName", Instance)! + .CreateDelegate>(metadata); + var cache = (IDictionary)metadata.GetType().GetField("typeNames", Instance)!.GetValue(metadata)!; + uint[] tokens = pinned.Module.GetType("Fixture.Owner").Fields + .Select(field => ((RequiredModifierType)field.FieldType).ModifierType.MetadataToken.ToUInt32()).ToArray(); + return new(pinned, query, cache, tokens); + } + catch + { + pinned.Dispose(); + throw; + } +} + +long CheckAllocations(RowFixture fixture) +{ + long start = GC.GetAllocatedBytesForCurrentThread(); + fixture.Pass(cold: true); + long allocated = GC.GetAllocatedBytesForCurrentThread() - start; + Require(fixture.Cache.Count == fixture.Tokens.Length, "Each raw row must have exactly one cache entry"); + long repeatStart = GC.GetAllocatedBytesForCurrentThread(); + for (int repeat = 0; repeat < 16; repeat++) fixture.Pass(cold: false); + long repeatAllocated = GC.GetAllocatedBytesForCurrentThread() - repeatStart; + Require(repeatAllocated < 1024, "Repeated row queries allocate metadata names"); + Require(fixture.Cache.Count == fixture.Tokens.Length, "Repeated row queries grew the cache"); + Console.WriteLine($"Rows={fixture.Tokens.Length}, allocated={allocated}, cached allocated={repeatAllocated}"); + return allocated; +} + +bool GrowthAccepted(double[] batches) +{ + for (int index = 1; index < batches.Length; index++) + if (batches[index] > batches[index - 1] * 6 + 1) return false; + return true; +} + +Require(GrowthAccepted([25, 100, 400]), "Linear batch growth was rejected"); +Require(GrowthAccepted([25, 125, 600]), "N log n batch growth was rejected"); +Require(!GrowthAccepted([25, 400, 6400]), "Quadratic batch growth was accepted"); + +double Batch(RowFixture fixture, bool cold, int repetitions) +{ + long started = Stopwatch.GetTimestamp(); + for (int repeat = 0; repeat < repetitions; repeat++) fixture.Pass(cold); + return Stopwatch.GetElapsedTime(started).TotalMilliseconds; +} + +void CheckTimings(RowFixture[] fixtures, bool cold, int nameLength) +{ + const int rounds = 9; + const double minimumBatchMilliseconds = 25; + foreach (var fixture in fixtures) Batch(fixture, cold, 16); + int repetitions = 1; + while (Batch(fixtures[0], cold, repetitions) < minimumBatchMilliseconds) + repetitions = checked(repetitions * 2); + double[][] samples = fixtures.Select(_ => new double[rounds]).ToArray(); + for (int round = 0; round < rounds; round++) + for (int position = 0; position < fixtures.Length; position++) + { + int index = round % 2 == 0 ? position : fixtures.Length - 1 - position; + samples[index][round] = Batch(fixtures[index], cold, repetitions); + } + double[] minima = samples.Select(values => values.Min()).ToArray(); + for (int index = 0; index < fixtures.Length; index++) + Console.WriteLine($"Rows={fixtures[index].Tokens.Length}, name bytes={nameLength}, cold={cold}, " + + $"passes={repetitions}, minimum batch ms={minima[index]:F3}, " + + $"minimum pass ms={minima[index] / repetitions:F6}"); + Require(GrowthAccepted(minima), $"Modifier query CPU growth exceeded 6x+1ms: cold={cold}, bytes={nameLength}"); +} + +foreach (int nameLength in new[] { 32, 512 }) +{ + var fixtures = new List(); + try + { + foreach (int count in new[] { 256, 1024, 4096 }) fixtures.Add(PrepareRows(count, nameLength)); + long previous = 0; + foreach (var fixture in fixtures) + { + long allocated = CheckAllocations(fixture); + Require(previous == 0 || allocated <= previous * 5 + 16384, + "Raw row allocations grew superlinearly"); + previous = allocated; + } + CheckTimings(fixtures.ToArray(), cold: true, nameLength); + CheckTimings(fixtures.ToArray(), cold: false, nameLength); + } + finally + { + for (int index = fixtures.Count - 1; index >= 0; index--) fixtures[index].Dispose(); + } +} + +/// One pinned row set with explicit nested-context ownership and retained cache identities. +sealed class RowFixture(PinnedMetadata pinned, Func query, IDictionary cache, uint[] tokens) + : IDisposable +{ + readonly object?[] rows = new object?[tokens.Length]; + internal IDictionary Cache => cache; + internal uint[] Tokens => tokens; + + internal void Pass(bool cold) + { + if (cold) + { + cache.Clear(); + Array.Clear(rows); + } + for (int index = 0; index < tokens.Length; index++) + { + object? row = query(tokens[index]); + if (cold) rows[index] = row; + else if (!ReferenceEquals(row, rows[index])) + throw new Exception("Repeated query decoded another row"); + } + } + + public void Dispose() => pinned.Dispose(); +} diff --git a/Tests/ModularStaticExports.cs b/Tests/ModularStaticExports.cs index 76ba80254..55da12b80 100644 --- a/Tests/ModularStaticExports.cs +++ b/Tests/ModularStaticExports.cs @@ -2,8 +2,14 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../Extractor/StaticIdentity.cs #:include ImportReviewRegressions.cs +#:include PinnedMetadata.cs using System.Reflection; using System.Text.Json; @@ -27,10 +33,14 @@ string caller = Path.Combine(root, "Caller"); foreach (string path in new[] { libRoot, appRoot, package, caller }) Directory.CreateDirectory(path); File.WriteAllText(Path.Combine(libRoot, "Lib.csproj"), """ -net10.0StaticFixture.Lib1.0.0.0true +net11.0 +StaticFixture.Lib1.0.0.0true + """); File.WriteAllText(Path.Combine(appRoot, "App.csproj"), """ -net10.0StaticFixture.Apptrue +net11.0 +StaticFixture.Apptrue + """); File.WriteAllText(Path.Combine(libRoot, "Library.cs"), """ namespace StaticFixture; @@ -122,14 +132,19 @@ void CreatePackage(string path, string name, params string[] modules) } CreatePackage(package, "staticproducer", "PublishedTable", "LibraryProof"); CreatePackage(caller, "staticconsumer", "CallerProof"); +// The published descriptor names the field as the library extraction's static site does. +string extractedLibrary = File.ReadAllText(Path.Combine(libArtifact, "Extracted.lean")); +int fieldStart = extractedLibrary.IndexOf("field := {", StringComparison.Ordinal) + "field := ".Length; +if (fieldStart < "field := ".Length) + throw new InvalidOperationException("The library extraction names no static field."); +string libField = extractedLibrary[fieldStart..(extractedLibrary.IndexOf('}', fieldStart) + 1)]; string contract = Path.Combine(package, "PublishedTable.lean"); File.WriteAllText(contract, $$""" import CIL.Safety.MethodContract namespace PublishedTable open CIL.Safety def table : CIL.StaticDescriptor := - { identity := {{libKey}}, assemblyIdentity := {{JsonSerializer.Serialize(libStatic["assemblyIdentity"]!.GetValue())}}, - fieldName := {{JsonSerializer.Serialize(libStatic["fieldName"]!.GetValue())}}, + { identity := {{libKey}}, field := {{libField}}, bytes := [17#8, 0#8, 0#8, 0#8, 0#8, 0#8, 0#8, 0#8] } def read : MethodContract Unit CIL.W64 where args _ := [] @@ -163,30 +178,31 @@ rw [fixed transparent] at actual change read result reference 8 1 = .ok descriptor.bytes at actual simpa only [contents] using actual have load := load_word64_of_read loaded - have addressed : staticInstruction Extracted.method0 0 (.staticAddress descriptor.bytes) [] memory = - .ok (result, [.reference (.address reference)]) := by + have addressed : staticAddress Extracted.method0 0 memory = + .ok (result, .reference (.address reference)) := by change (do let (updated, address) ← staticReference descriptor memory - pure (updated, [CIL.Safety.Value.reference (.address address)]) : - ExecutionResult (CIL.Safety.Memory × List CIL.Safety.Value)) = _ + pure (updated, CIL.Safety.Value.reference (.address address)) : + ExecutionResult (CIL.Safety.Memory × CIL.Safety.Value)) = _ rw [resolved] rfl let frame : Frame := { activation := memory.nextIdentity, locals := [], owned := [] } - have first : step Extracted.method0 (.memory (.staticAddress descriptor.bytes)) 0 [] frame [] memory = + have first : ∀ field, step Extracted.method0 (.ldsflda field) 0 [] frame [] memory = .ok (.next 1 [.reference (.address reference)] frame result) := by - rw [step_memory_eq, addressed] - rfl - have second : step Extracted.method0 .load64 1 [] frame [.reference (.address reference)] result = + intro field + rw [step_ldsflda_eq, addressed] + have second : step Extracted.method0 (.ldind .i8) 1 [] frame [.reference (.address reference)] result = .ok (.next 2 [.scalar (.i64 17)] frame result) := by - rw [step_load64_eq] - simp only [instructionStep, instruction, load, Bind.bind, Except.bind, Pure.pure, Except.pure] + rw [step_ldind_eq] + simp only [instructionStep, instruction, CIL.NumericKind.width, CIL.NumericKind.signed, load, + Bind.bind, Except.bind, Pure.pure, Except.pure] rfl have returned : step Extracted.method0 .ret 2 [] frame [.scalar (.i64 17)] result = .ok (.returned [.scalar (.i64 17)] result) := rfl have preserved := staticReference_preserves_static_world _ _ _ _ _ formed world (by simp) resolved rw [same] at preserved apply WP.enter (body := Extracted.method0) (by rfl) (by rfl) (by rfl) - apply WP.step (by rfl) (by rfl) first + apply WP.step (by rfl) (by rfl) (first _) apply WP.step (by rfl) (by rfl) second apply WP.returned (by rfl) (by rfl) returned (by rfl) exact ⟨Confined.ofMemoryBelow (staticReference_preserves_memory_below _ _ _ _ resolved) @@ -233,7 +249,7 @@ open CIL.Safety theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => PublishedTable.read.Holds linked 0) := by intro linked links imported input memory permitted - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : PublishedTable.read.Holds linked 1 := imported apply WP.enter found (by rfl) (by rfl) apply WP.call found (by rfl) (by cil_checked_step []) (holds input memory permitted) @@ -251,7 +267,7 @@ string[] ExtractApp(string output) => [.. cli, "extract", app, "StaticFixture.Ap var originalLib = File.ReadAllBytes(lib); string[] FullCommand(string artifact, string callerSource, bool package) => [.. cli, "verify", artifact, "--proof", callerSource, .. package ? new[] { "--proof-root", caller } : [], "--force-full-verify"]; -void FullVerify(string artifact, string callerSource, bool package, int slots) +string[] FullVerify(string artifact, string callerSource, bool package, int slots) { var trace = Path.Combine(root, "trace-" + Guid.NewGuid().ToString("N")); string? previousTrace = Environment.GetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY"); @@ -287,6 +303,7 @@ void FullVerify(string artifact, string callerSource, bool package, int slots) !verified.GetProperty("compositionAudit").GetString()!.Contains("linkChecked", StringComparison.Ordinal)) throw new InvalidOperationException( "Full verification did not publish the checked static composition: " + verified); + return stages; } // Each rejection names the one check expected to fire first. void RejectFull(string artifact, string marker) @@ -337,8 +354,24 @@ string Republish(Action editExport, Action editSidecar) FullTransitiveStatic(); return; } - FullVerify(appArtifact, callerProof, package: true, slots: 1); - Console.WriteLine("PASS the separate caller returns the RVA-table value under full verification"); + // A private library cache meets the first forced run empty, which builds the caller's library, and the + // last one holding it; that run opens the hash-checked entry and must trace the open like any other. + static bool Flag(string name) => + string.Equals(Environment.GetEnvironmentVariable(name), "true", StringComparison.OrdinalIgnoreCase); + var cacheReachable = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("LEANCIL_CERTIFIED_LIBRARY")) && + !Flag("CI") && !Flag("GITHUB_ACTIONS"); + var libraryCache = Path.Combine(root, "library-cache"); + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", libraryCache); + bool CacheHoldsEntry() => Directory.Exists(libraryCache) && Directory.EnumerateDirectories(libraryCache) + .Any(entry => File.Exists(Path.Combine(entry, "cache-receipt.hmac"))); + if (!cacheReachable) + Console.WriteLine("NOTE a supplied certified library or CI keeps the local library cache unread, so the " + + "cold and warm cache runs below both open the supplied library"); + const string openStage = "Opening certified Lean verification library"; + if (FullVerify(appArtifact, callerProof, package: true, slots: 1).Contains(openStage) && cacheReachable) + throw new InvalidOperationException("Precondition: the first forced run met a warm library cache."); + Console.WriteLine("PASS the separate caller returns the RVA-table value under full verification with a cold " + + "library cache"); // A cooperating publisher's bundle whose extraction record drops the table fails before any build. try @@ -412,9 +445,12 @@ bool ChecksProof(string[] stages) => stages.Any(stage => stage.StartsWith("Checking Proof:", StringComparison.Ordinal)); if (!ChecksProof(OrdinaryStages("trace-cold")) || ChecksProof(OrdinaryStages("trace-warm"))) throw new InvalidOperationException("Precondition: the repeated ordinary verification restored no receipt."); - FullVerify(mixedArtifact, mixedProof, package: false, slots: 2); + if (cacheReachable && !CacheHoldsEntry()) + throw new InvalidOperationException("Precondition: the ordinary verification cached no certified library."); + if (!FullVerify(mixedArtifact, mixedProof, package: false, slots: 2).Contains(openStage) && cacheReachable) + throw new InvalidOperationException("The forced run with a warm library cache did not open its entry."); Console.WriteLine("PASS distinct same-index app and library tables and one shared table across two " + - "imported slots, with a warm receipt present"); + "imported slots, with a warm receipt and a warm library cache present"); FullTransitiveStatic(); return; } @@ -445,7 +481,9 @@ void FullTransitiveStatic() var topRoot = Path.Combine(root, "Top"); Directory.CreateDirectory(topRoot); File.WriteAllText(Path.Combine(topRoot, "Top.csproj"), """ - net10.0StaticFixture.Toptrue + net11.0 + StaticFixture.Toptrue + """); File.WriteAllText(Path.Combine(topRoot, "Top.cs"), "namespace StaticFixture; public static class Top { public static ulong Entry() => Application.Entry(); }"); diff --git a/Tests/ModularStatics.lean b/Tests/ModularStatics.lean index 046a723f0..f52d77efa 100644 --- a/Tests/ModularStatics.lean +++ b/Tests/ModularStatics.lean @@ -2,6 +2,7 @@ import CIL.Safety.StaticLinking import CIL.Safety.StaticMemoryBelow import CIL.Safety.WordMemory import CIL.Safety.Execution +import Tests.Support /-! Checked static-table linking and loaded-world premises for modular consumer contracts. -/ @@ -9,17 +10,23 @@ namespace Tests.ModularStatics open CIL CIL.Safety +/-- The field of `owner`'s table, an eight-byte static. -/ +private def tableRef (owner : String) : Extraction.FieldRef := + { owner := .atom owner (owner ++ ".Holder"), index := 0, type := .atom owner (owner ++ ".Table"), + name := name!"Table" } + private def appField : StaticDescriptor := - { identity := 101, assemblyIdentity := "App", fieldName := "App::Table", bytes := numberBytes 7 8 } + { identity := 101, field := tableRef "App", bytes := numberBytes 7 8 } private def libField : StaticDescriptor := - { identity := 202, assemblyIdentity := "Lib", fieldName := "Lib::Table", bytes := numberBytes 17 8 } + { identity := 202, field := tableRef "Lib", bytes := numberBytes 17 8 } @[cil_code] def libBody : Method := { returnsValue := true, locals := [], staticSites := [(0, libField)], - code := [.memory (.staticAddress (numberBytes 17 8)), .load64, .ret] } + code := [.ldsflda (tableRef "Lib"), .ldind .i8, .ret] } @[cil_code] def appBody : Method := { returnsValue := true, locals := [], staticSites := [(0, appField)], - code := [.memory (.staticAddress (numberBytes 7 8)), .pop, .call 1 0, .ret] } + code := [.ldsflda (tableRef "App"), .pop, .call (Tests.callee 0) {}, + .ret], links := [(2, .direct 1)] } def library : Library := { entry := libBody } def linked : CIL.Program := link [appBody] [library] @@ -77,23 +84,17 @@ private theorem libBody_reads (memory : Safety.Memory) (formed : memory.WellForm rw [same rfl] at readable simpa [libField, numberBytes] using readable have load := load_word64_of_read loaded - have addressed : staticInstruction libBody 0 (.staticAddress (numberBytes 17 8)) [] memory = - .ok (result, [.reference (.address reference)]) := by - change (do - let (updated, address) ← staticReference libField memory - pure (updated, [Safety.Value.reference (.address address)]) : - ExecutionResult (Safety.Memory × List Safety.Value)) = _ - rw [resolved] - rfl + have addressed : staticAddress libBody 0 memory = + .ok (result, .reference (.address reference)) := staticAddress_resolved rfl resolved let frame : Frame := { activation := memory.nextIdentity, locals := [], owned := [] } - have first : step libBody (.memory (.staticAddress (numberBytes 17 8))) 0 [] frame [] memory = + have first : step libBody (.ldsflda (tableRef "Lib")) 0 [] frame [] memory = .ok (.next 1 [.reference (.address reference)] frame result) := by - rw [step_memory_eq, addressed] - rfl - have second : step libBody .load64 1 [] frame [.reference (.address reference)] result = + rw [step_ldsflda_eq, addressed] + have second : step libBody (.ldind .i8) 1 [] frame [.reference (.address reference)] result = .ok (.next 2 [.scalar (.i64 17)] frame result) := by - rw [step_load64_eq] - simp only [instructionStep, instruction, load, Bind.bind, Except.bind, Pure.pure, Except.pure] + rw [step_ldind_eq] + simp only [instructionStep, instruction, CIL.NumericKind.width, CIL.NumericKind.signed, load, + Bind.bind, Except.bind, Pure.pure, Except.pure] rfl have returned : step libBody .ret 2 [] frame [.scalar (.i64 17)] result = .ok (.returned [.scalar (.i64 17)] result) := rfl @@ -160,8 +161,17 @@ example : ∃ fuel result values, invoke linked fuel 1 [] afterApp = .ok (result private def collision : Library := { entry := { libBody with staticSites := [(0, { libField with identity := 101 })] } } -example : linkChecked [appBody] [collision] = - .error "Static identity collision 101: App::App::Table and Lib::Lib::Table" := rfl +/-- The application's table and the library's under its identity collide, in that order. -/ +theorem colliding_identity_rejected : + linkChecked [appBody] [collision] = + .error { left := appField, right := { libField with identity := 101 } } := + linkChecked_of_collision (by decide +kernel) + +-- The report names both fields; the interpreter checks it, as the kernel does not reduce `repr`. +#guard match linkChecked [appBody] [collision] with + | .error collision => collision.message == "Static identity collision 101: " ++ + reprStr (tableRef "App") ++ " and " ++ reprStr (tableRef "Lib") + | .ok _ => false /-- `library` with the table's bytes changed under the same identity and field. -/ private def changedTable : Library := @@ -172,15 +182,17 @@ theorem shared_table_links : linkChecked [appBody] [library, library] = .ok (link [appBody] [library, library]) := linkChecked_of_compatible (by decide +kernel) -/-- Reusing an identity with different table bytes prevents checked linking. -/ -theorem changed_shared_table_rejected : linkChecked [appBody] [library, changedTable] = - .error "Static identity collision 202: Lib::Lib::Table and Lib::Lib::Table" := by - have incompatible : ¬ StaticDescriptorsCompatible - (programStaticDescriptors (link [appBody] [library, changedTable])) := by decide +kernel - unfold linkChecked - rw [ite_eq_right incompatible] - apply congrArg Except.error - decide +kernel +/-- Reusing an identity with different table bytes prevents checked linking, and the collision + is the library's descriptor beside the changed one. -/ +theorem changed_shared_table_rejected : + linkChecked [appBody] [library, changedTable] = + .error { left := libField, right := { libField with bytes := numberBytes 18 8 } } := + linkChecked_of_collision (by decide +kernel) + +#guard match linkChecked [appBody] [library, changedTable] with + | .error collision => collision.message == "Static identity collision 202: " ++ + reprStr (tableRef "Lib") ++ " and " ++ reprStr (tableRef "Lib") + | .ok _ => false /-- A loaded table with different bytes cannot satisfy a contract's static-world premise. -/ theorem changed_loaded_table_not_valid : @@ -196,6 +208,7 @@ theorem changed_loaded_table_not_valid : exact incompatible (Except.ok.inj (actual.symm.trans readable)) #print axioms shared_table_links +#print axioms colliding_identity_rejected #print axioms changed_shared_table_rejected #print axioms changed_loaded_table_not_valid #print axioms readTable_holds diff --git a/Tests/MulScalarFixture.lean b/Tests/MulScalarFixture.lean index bb3b4526e..928137453 100644 --- a/Tests/MulScalarFixture.lean +++ b/Tests/MulScalarFixture.lean @@ -1,7 +1,10 @@ +import Tests.Support.IntrinsicMethods import CIL.Instructions +import Tests.Support /-! Plain-data copies of a four-limb 256-bit multiply body: `actual` is a scalar-profile - extraction of `MultiplyLimbs4x4`; `fullBody` is an older proof literal. -/ + extraction of `MultiplyLimbs4x4` whose holes carry opcode 0 in place of their + raw opcodes; `fullBody` is an older proof literal. -/ namespace Tests.MulScalarFixture @@ -9,400 +12,509 @@ open CIL def profile : FeatureProfile := { architecture := .scalar, nativeWidth := 64, littleEndian := true, advSimd := false, sse2 := false, ssse3 := false, sse42 := false, avx := false, avx2 := false, avx512F := false, avx512FVL := false, bmi1 := false, sse41 := false, avx512DQ := false, avx512DQVL := false, bmi2 := false, armBase64 := false, vector256Accelerated := false } -def actual : Method := { profile := profile, returnsValue := false, locals := [.unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled], localKinds := [.word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .vector256, .vector256, .vector256, .vector256], parameterHomes := [], staticSites := [], code := [ - .arg 0, - .fieldAt 0 8, - .setLocal 0, - .arg 1, - .fieldAt 0 8, - .setLocal 1, - .arg 0, - .fieldAt 8 8, - .setLocal 2, - .arg 1, - .fieldAt 8 8, - .setLocal 3, - .arg 0, - .fieldAt 16 8, - .setLocal 4, - .arg 1, - .fieldAt 16 8, - .setLocal 5, - .feature .avx512DQVL, - .brzero 42, - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .feature .avx2, - .brzero 90, - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .unsupported "Unreachable under selected profile", - .local 0, - .arg 1, - .fieldAt 24 8, +/-- The checked four-limb operand of the vector paths. -/ +private def limbsEnvironment : LayoutEnvironment := + let layout : Extraction.RawLayout := + { identity := "Fixture::Limbs", explicit := false, packing := 0, declaredSize := 0, + fields := List.replicate 4 { type := .unsigned64 } } + let fields : List FieldLayout := + [⟨0, 8, false⟩, ⟨8, 8, false⟩, ⟨16, 8, false⟩, ⟨24, 8, false⟩] + { rawLayouts := [layout], layouts := #[⟨⟨32, fields⟩, 8⟩] } + +/-- A UInt64 member of the four-word fixture, selected by its metadata field index. -/ +private def limbField (index : Nat) (name : MetadataName) : Extraction.FieldRef := + { owner := .aggregate "Fixture::Limbs" 0, index, name, + type := .atom Extraction.runtimeScope "System.UInt64" } + +/-- The readonly limb argument is formed with AsRef before As retypes it as a vector. -/ +private def limbsCall (asVector : Bool) : Instruction := + let limbs := Extraction.TypeExpr.aggregate "Fixture::Limbs" 0 + let vector := Extraction.TypeExpr.apply + (.constructor Extraction.intrinsicsScope "System.Runtime.Intrinsics.Vector256`1") + (.atom Extraction.runtimeScope "System.UInt64") + let method : Extraction.MethodRef := + { owner := Extraction.unsafeOwner, name := if asVector then name!"As" else name!"AsRef", + hasThis := false, genericArity := if asVector then 2 else 1, + returnType := .byref (if asVector then vector else limbs), parameters := [.byref limbs], + methodArguments := if asVector then [limbs, vector] else [limbs] } + .call method {} + +/-- The scalar extraction of the four-limb multiply. -/ +def actual : Method := { + environment := limbsEnvironment, + links := [(111, .direct 9), (116, .direct 9), (121, .direct 9), (129, .direct 5), + (134, .direct 5), (144, .direct 5), (149, .direct 5), (154, .direct 9), (159, .direct 5), + (168, .direct 9), (173, .direct 5), (182, .direct 9), (187, .direct 5), (200, .direct 7)], + profile := profile, returnsValue := false, locals := [.unmodeled, .unmodeled, + .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, + .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, + .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, + .unmodeled], localKinds := [.word64, .word64, .word64, .word64, .word64, .word64, .word64, + .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, + .word64, .word64, .word64, .word64, .word64, .vector256, .vector256, .vector256, .vector256], + parameterHomes := [], staticSites := [], code := [ + .ldarg 0, + .ldfld (limbField 0 name!"Limb0"), + .stloc 0, + .ldarg 1, + .ldfld (limbField 0 name!"Limb0"), + .stloc 1, + .ldarg 0, + .ldfld (limbField 1 name!"Limb1"), + .stloc 2, + .ldarg 1, + .ldfld (limbField 1 name!"Limb1"), + .stloc 3, + .ldarg 0, + .ldfld (limbField 2 name!"Limb2"), + .stloc 4, + .ldarg 1, + .ldfld (limbField 2 name!"Limb2"), + .stloc 5, + .call (Tests.supportedGetter "X86.Avx512DQ/VL"), + .brfalse 42, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .call (Tests.supportedGetter "X86.Avx2"), + .brfalse 90, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .unadmitted 0, + .ldloc 0, + .ldarg 1, + .ldfld (limbField 3 name!"Limb3"), .mul, - .local 2, - .local 5, + .ldloc 2, + .ldloc 5, .mul, .add, - .local 4, - .local 3, + .ldloc 4, + .ldloc 3, .mul, .add, - .arg 0, - .fieldAt 24 8, - .local 1, + .ldarg 0, + .ldfld (limbField 3 name!"Limb3"), + .ldloc 1, .mul, .add, - .setLocal 6, - .local 0, - .local 1, - .localAddr 8, - .call 9 3, - .setLocal 7, - .local 0, - .local 3, - .localAddr 10, - .call 9 3, - .setLocal 9, - .local 2, - .local 1, - .localAddr 12, - .call 9 3, - .setLocal 11, - .const32 0, - .convI8, - .setLocal 13, - .local 7, - .local 10, - .localAddr 13, - .call 5 3, - .setLocal 14, - .local 14, - .local 12, - .localAddr 13, - .call 5 3, - .setLocal 14, - .local 13, - .setLocal 15, - .const32 0, - .convI8, - .setLocal 13, - .local 15, - .local 9, - .localAddr 13, - .call 5 3, - .setLocal 15, - .local 15, - .local 11, - .localAddr 13, - .call 5 3, - .setLocal 15, - .local 0, - .local 5, - .localAddr 17, - .call 9 3, - .setLocal 16, - .local 15, - .local 17, - .localAddr 13, - .call 5 3, - .setLocal 15, - .local 6, - .local 16, + .stloc 6, + .ldloc 0, + .ldloc 1, + .ldloca 8, + .call (Tests.callee 3) {}, + .stloc 7, + .ldloc 0, + .ldloc 3, + .ldloca 10, + .call (Tests.callee 3) {}, + .stloc 9, + .ldloc 2, + .ldloc 1, + .ldloca 12, + .call (Tests.callee 3) {}, + .stloc 11, + .ldc_i4 0, + .conv_i8, + .stloc 13, + .ldloc 7, + .ldloc 10, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 14, + .ldloc 14, + .ldloc 12, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 14, + .ldloc 13, + .stloc 15, + .ldc_i4 0, + .conv_i8, + .stloc 13, + .ldloc 15, + .ldloc 9, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 15, + .ldloc 11, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 0, + .ldloc 5, + .ldloca 17, + .call (Tests.callee 3) {}, + .stloc 16, + .ldloc 15, + .ldloc 17, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 6, + .ldloc 16, .add, - .setLocal 6, - .local 2, - .local 3, - .localAddr 19, - .call 9 3, - .setLocal 18, - .local 15, - .local 19, - .localAddr 13, - .call 5 3, - .setLocal 15, - .local 6, - .local 18, + .stloc 6, + .ldloc 2, + .ldloc 3, + .ldloca 19, + .call (Tests.callee 3) {}, + .stloc 18, + .ldloc 15, + .ldloc 19, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 6, + .ldloc 18, .add, - .setLocal 6, - .local 4, - .local 1, - .localAddr 21, - .call 9 3, - .setLocal 20, - .local 15, - .local 21, - .localAddr 13, - .call 5 3, - .setLocal 15, - .local 6, - .local 20, - .local 13, + .stloc 6, + .ldloc 4, + .ldloc 1, + .ldloca 21, + .call (Tests.callee 3) {}, + .stloc 20, + .ldloc 15, + .ldloc 21, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 6, + .ldloc 20, + .ldloc 13, .add, .add, - .setLocal 6, - .arg 2, - .local 8, - .local 14, - .local 15, - .local 6, - .call 7 5, + .stloc 6, + .ldarg 2, + .ldloc 8, + .ldloc 14, + .ldloc 15, + .ldloc 6, + .call (Tests.callee 5) {}, .ret] } -def fullBody (profile : CIL.FeatureProfile) (wordIndex countIndex storageIndex : Nat) (alternative : Bool := false) : CIL.Method := { profile := profile, returnsValue := false, locals := [.unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled], localKinds := [.word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .vector256, .vector256, .vector256, .vector256], parameterHomes := [], staticSites := [], code := [ - .arg 0, - .fieldAt 0 8, - .setLocal 0, - .arg 1, - .fieldAt 0 8, - .setLocal 1, - .arg 0, - .fieldAt 8 8, - .setLocal 2, - .arg 1, - .fieldAt 8 8, - .setLocal 3, - .arg 0, - .fieldAt 16 8, - .setLocal 4, - .arg 1, - .fieldAt 16 8, - .setLocal 5, - .feature .avx512DQVL, - .brzero 36, - (if profile.avx512DQVL then .arg 0 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .memory .asRef else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .memory .asRef else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .memory .load256 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .arg 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .memory .asRef else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .memory .asRef else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .memory .load256 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .setLocal 22 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .local 22 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .const32 (BitVec.ofInt 32 (27)) else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .intrinsic (.avx2 .permute4x64) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .intrinsic (.avx512DQ .mul64) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .intrinsic (.vector (.sum64 256)) 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .setLocal 6 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .branch 96 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else .feature .avx2), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else .brzero 78), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .arg 0 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .memory .asRef else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .memory .asRef else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .memory .load256 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .setLocal 23 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .arg 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .memory .asRef else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .memory .asRef else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .memory .load256 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .const32 (BitVec.ofInt 32 (27)) else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.avx2 .permute4x64) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .setLocal 24 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .local 23 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.vector (.reinterpret 256)) 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .local 24 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .const32 (BitVec.ofInt 32 (32)) else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.avx2 .shr64) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.vector (.reinterpret 256)) 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.avx2 .multiplyEven32) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .local 23 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .const32 (BitVec.ofInt 32 (32)) else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.avx2 .shr64) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.vector (.reinterpret 256)) 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .local 24 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.vector (.reinterpret 256)) 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.avx2 .multiplyEven32) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.avx2 .add64) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .setLocal 25 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .local 23 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.vector (.reinterpret 256)) 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .local 24 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.vector (.reinterpret 256)) 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.avx2 .multiplyEven32) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .local 25 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .const32 (BitVec.ofInt 32 (32)) else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.avx2 .shl64) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.avx2 .add64) 2 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .intrinsic (.vector (.sum64 256)) 1 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .setLocal 6 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL then .unsupported "Unreachable under selected profile" else if profile.avx2 then .branch 96 else .unsupported "Unreachable under selected profile"), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .local 0), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .arg 1), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .fieldAt 24 8), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .mul), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .local 2), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .local 5), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .mul), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .add), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .local 4), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .local 3), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .mul), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .add), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .arg 0), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .fieldAt 24 8), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .local 1), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .mul), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .add), - (if profile.avx512DQVL || profile.avx2 then .unsupported "Unreachable under selected profile" else .setLocal 6), - .local 0, - .local 1, - .localAddr 8, - .call wordIndex 3, - .setLocal 7, - .local 0, - .local 3, - .localAddr 10, - .call wordIndex 3, - .setLocal 9, - .local 2, - .local 1, - .localAddr 12, - .call wordIndex 3, - .setLocal 11, - .const32 0, - .convI8, - .setLocal 13, - .local 7, - .local (if alternative then 12 else 10), - .localAddr 13, - .call countIndex 3, - .setLocal 14, - .local 14, - .local (if alternative then 10 else 12), - .localAddr 13, - .call countIndex 3, - .setLocal 14, - .local 13, - .setLocal 15, - .const32 0, - .convI8, - .setLocal 13, - .local 15, - .local 9, - .localAddr 13, - .call countIndex 3, - .setLocal 15, - .local 15, - .local 11, - .localAddr 13, - .call countIndex 3, - .setLocal 15, - .local 0, - .local 5, - .localAddr 17, - .call wordIndex 3, - .setLocal 16, - .local 15, - .local 17, - .localAddr 13, - .call countIndex 3, - .setLocal 15, - .local 6, - .local 16, +def fullBody (profile : CIL.FeatureProfile) (wordIndex countIndex storageIndex : Nat) + (alternative : Bool := false) : CIL.Method := { + environment := limbsEnvironment, + links := [(99, .direct wordIndex), (104, .direct wordIndex), (109, .direct wordIndex), + (117, .direct countIndex), (122, .direct countIndex), (132, .direct countIndex), + (137, .direct countIndex), (142, .direct wordIndex), (147, .direct countIndex), + (156, .direct wordIndex), (161, .direct countIndex), (170, .direct wordIndex), + (175, .direct countIndex), (188, .direct storageIndex)], + profile := profile, returnsValue := false, locals := [.unmodeled, .unmodeled, + .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, + .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, + .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, .unmodeled, + .unmodeled], localKinds := [.word64, .word64, .word64, .word64, .word64, .word64, .word64, + .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, .word64, + .word64, .word64, .word64, .word64, .word64, .vector256, .vector256, .vector256, .vector256], + parameterHomes := [], staticSites := [], code := [ + .ldarg 0, + .ldfld (limbField 0 name!"Limb0"), + .stloc 0, + .ldarg 1, + .ldfld (limbField 0 name!"Limb0"), + .stloc 1, + .ldarg 0, + .ldfld (limbField 1 name!"Limb1"), + .stloc 2, + .ldarg 1, + .ldfld (limbField 1 name!"Limb1"), + .stloc 3, + .ldarg 0, + .ldfld (limbField 2 name!"Limb2"), + .stloc 4, + .ldarg 1, + .ldfld (limbField 2 name!"Limb2"), + .stloc 5, + .call (Tests.supportedGetter "X86.Avx512DQ/VL"), + .brfalse 36, + (if profile.avx512DQVL then .ldarg 0 else .unadmitted 0), + (if profile.avx512DQVL then limbsCall false else .unadmitted 0), + (if profile.avx512DQVL then limbsCall true else .unadmitted 0), + (if profile.avx512DQVL then .ldobj Tests.IntrinsicMethods.vector256UInt64 else .unadmitted 0), + (if profile.avx512DQVL then .ldarg 1 else .unadmitted 0), + (if profile.avx512DQVL then limbsCall false else .unadmitted 0), + (if profile.avx512DQVL then limbsCall true else .unadmitted 0), + (if profile.avx512DQVL then .ldobj Tests.IntrinsicMethods.vector256UInt64 else .unadmitted 0), + (if profile.avx512DQVL then .stloc 22 else .unadmitted 0), + (if profile.avx512DQVL then .ldloc 22 else .unadmitted 0), + (if profile.avx512DQVL then .ldc_i4 (BitVec.ofInt 32 (27)) else .unadmitted 0), + (if profile.avx512DQVL then .call Tests.IntrinsicMethods.permute else .unadmitted 0), + (if profile.avx512DQVL then .call Tests.IntrinsicMethods.multiplyLow else .unadmitted 0), + (if profile.avx512DQVL then .call Tests.IntrinsicMethods.sum else .unadmitted 0), + (if profile.avx512DQVL then .stloc 6 else .unadmitted 0), + (if profile.avx512DQVL then .br 96 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 else .call (Tests.supportedGetter "X86.Avx2")), + (if profile.avx512DQVL then .unadmitted 0 else .brfalse 78), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldarg 0 + else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then limbsCall false else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then limbsCall true else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldobj Tests.IntrinsicMethods.vector256UInt64 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .stloc 23 + else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldarg 1 + else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then limbsCall false else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then limbsCall true else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldobj Tests.IntrinsicMethods.vector256UInt64 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldc_i4 (BitVec.ofInt 32 (27)) else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.permute else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .stloc 24 + else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldloc 23 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.asUInt32 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldloc 24 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldc_i4 (BitVec.ofInt 32 (32)) else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.shiftRight else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.asUInt32 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.multiplyEven32 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldloc 23 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldc_i4 (BitVec.ofInt 32 (32)) else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.shiftRight else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.asUInt32 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldloc 24 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.asUInt32 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.multiplyEven32 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.add64 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .stloc 25 + else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldloc 23 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.asUInt32 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldloc 24 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.asUInt32 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.multiplyEven32 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldloc 25 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .ldc_i4 (BitVec.ofInt 32 (32)) else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.shiftLeft else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.add64 else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .call Tests.IntrinsicMethods.sum else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 + else if profile.avx2 then .stloc 6 + else .unadmitted 0), + (if profile.avx512DQVL then .unadmitted 0 else if profile.avx2 then .br 96 else .unadmitted 0), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 + else .ldloc 0), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 + else .ldarg 1), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 else .ldfld (limbField 3 name!"Limb3")), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 else .mul), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 + else .ldloc 2), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 + else .ldloc 5), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 else .mul), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 else .add), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 + else .ldloc 4), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 + else .ldloc 3), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 else .mul), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 else .add), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 + else .ldarg 0), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 else .ldfld (limbField 3 name!"Limb3")), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 + else .ldloc 1), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 else .mul), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 else .add), + (if profile.avx512DQVL || profile.avx2 then .unadmitted 0 + else .stloc 6), + .ldloc 0, + .ldloc 1, + .ldloca 8, + .call (Tests.callee 3) {}, + .stloc 7, + .ldloc 0, + .ldloc 3, + .ldloca 10, + .call (Tests.callee 3) {}, + .stloc 9, + .ldloc 2, + .ldloc 1, + .ldloca 12, + .call (Tests.callee 3) {}, + .stloc 11, + .ldc_i4 0, + .conv_i8, + .stloc 13, + .ldloc 7, + .ldloc (if alternative then 12 else 10), + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 14, + .ldloc 14, + .ldloc (if alternative then 10 else 12), + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 14, + .ldloc 13, + .stloc 15, + .ldc_i4 0, + .conv_i8, + .stloc 13, + .ldloc 15, + .ldloc 9, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 15, + .ldloc 11, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 0, + .ldloc 5, + .ldloca 17, + .call (Tests.callee 3) {}, + .stloc 16, + .ldloc 15, + .ldloc 17, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 6, + .ldloc 16, .add, - .setLocal 6, - .local 2, - .local 3, - .localAddr 19, - .call wordIndex 3, - .setLocal 18, - .local 15, - .local 19, - .localAddr 13, - .call countIndex 3, - .setLocal 15, - .local 6, - .local 18, + .stloc 6, + .ldloc 2, + .ldloc 3, + .ldloca 19, + .call (Tests.callee 3) {}, + .stloc 18, + .ldloc 15, + .ldloc 19, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 6, + .ldloc 18, .add, - .setLocal 6, - .local 4, - .local 1, - .localAddr 21, - .call wordIndex 3, - .setLocal 20, - .local 15, - .local 21, - .localAddr 13, - .call countIndex 3, - .setLocal 15, - .local 6, - .local 20, - .local 13, + .stloc 6, + .ldloc 4, + .ldloc 1, + .ldloca 21, + .call (Tests.callee 3) {}, + .stloc 20, + .ldloc 15, + .ldloc 21, + .ldloca 13, + .call (Tests.callee 3) {}, + .stloc 15, + .ldloc 6, + .ldloc 20, + .ldloc 13, .add, .add, - .setLocal 6, - .arg 2, - .local 8, - .local 14, - .local 15, - .local 6, - .call storageIndex 5, + .stloc 6, + .ldarg 2, + .ldloc 8, + .ldloc 14, + .ldloc 15, + .ldloc 6, + .call (Tests.callee 5) {}, .ret ] } diff --git a/Tests/MultiAssembly.cs b/Tests/MultiAssembly.cs index e46ddadaf..a5cac0ae4 100644 --- a/Tests/MultiAssembly.cs +++ b/Tests/MultiAssembly.cs @@ -5,16 +5,33 @@ #:include GeneratedEvidence.cs #:include MultiAssemblyFixture.cs #:include MultiAssemblyGenerics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/DeepCheckInputs.cs +#:include StrictBindingFacts.cs +#:include StrictBindingDecoding.cs +#:include StrictBindingScaling.cs +#:include DeepBinding.cs using System.Text.Json; using System.Text.Json.Nodes; // Two SDK-built assemblies: App calls into Lib, and Lib is passed as a pinned `--reference`. MultiAssemblyFixture fixture = new("MultiAssembly"); +Run(["dotnet", "run", "--file", "Tests/MetadataAssemblyIndexes.cs"]); +Run(["dotnet", "run", "--file", "Tests/ExactMetadataBytes.cs"]); +Run(["dotnet", "run", "--file", "Tests/StrictBindingFixture.cs"]); string root = fixture.Root; string[] cli = fixture.Cli; const string libName = "MultiFixture.Lib"; const string unsigned = ", Culture=neutral, PublicKeyToken=null"; +Run(["dotnet", "run", "--file", At("Tests/DeepCheckInputs.cs")]); +Run(["dotnet", "run", "--file", At("Tests/DeepSession.cs")]); +Run(["dotnet", "run", "--file", At("Tests/DeepVerify.cs")]); +Run(["dotnet", "run", "--file", At("Tests/DeepDiagnostics.cs")]); +Run(["dotnet", "run", "--file", At("Tests/ConsumerDeepChecks.cs")]); const string libraryText = """ namespace MultiFixture; public struct Cell { public ulong Value; } @@ -40,6 +57,8 @@ public interface IGet { ulong Get(); } public struct Holder : IGet { public ulong Value; public ulong Get() => Value + 1; } public struct GenericOn : IFlag where T : unmanaged { public static bool IsActive => true; } public struct GenericPlain : IScale where T : unmanaged { } +// Last, so its constructor does not renumber the methods before it, whose tokens the strict audit cases name. +public sealed class Marker { } """; string Library(string name, string step, string properties = "") => @@ -50,7 +69,8 @@ string Library(string name, string step, string properties = "") => string substitute = Library("Substitute", "3 + 2"); string versioned = Library("Versioned", "3 + 1", "2.0.0.0"); // An assembly App references but extraction never pins. -fixture.Build("Other", "MultiFixture.Other", "namespace MultiFixture.Other;\npublic interface IMarker { }\n"); +string other = fixture.Build("Other", "MultiFixture.Other", + "namespace MultiFixture.Other;\npublic interface IMarker { }\n"); string appDirectory = fixture.Project("App", "MultiFixture.App", "", """"""); File.WriteAllText(Path.Combine(appDirectory, "Application.cs"), """ @@ -59,6 +79,7 @@ namespace MultiFixture; public static class Application { public static ulong Direct(ulong x) => Mix.Step(x) + 1; + public static ulong Take(Marker marker, ulong x) => x; public static ulong Nested(ulong x) => Mix.Twice(x); public static ulong Local(ulong x) { Cell cell = default; cell.Value = x; Mix.Bump(ref cell); return cell.Value; } public static ulong Size(ulong x) where T : struct => x + (ulong)Unsafe.SizeOf(); @@ -71,6 +92,7 @@ public static class Application public static ulong RunGenericOn(ulong x) => Run>(x); public static ulong RunAppGenericOff(ulong x) => Run>(x); public static ulong ScaledGenericPlain(ulong x) => Scaled>(x); + public static string Text() => "carried user string"; } // App structs and interfaces built on Lib's interfaces: dispatch reads both pinned assemblies. public struct AppScale : IScale { public static ulong Scale(ulong x) => x + 9; } @@ -81,6 +103,17 @@ public interface IAppScale : IScale { static ulong IScale.Scale(ulong x) => x * public struct AppDerived : IAppScale { } public struct Marked : IScale, MultiFixture.Other.IMarker { } public struct AppGenericOff : IFlag where T : unmanaged { public static bool IsActive => false; } +public static class CounterCaller { public static ulong Read(ulong x) => Counter.Next(x); } +public struct Counter +{ + public ulong Value; + public static ulong Next(ulong x) + { + Counter cell = default; + cell.Value = x; + return Mix.Step(cell.Value); + } +} """); Run(["dotnet", "build", Path.Combine(appDirectory, "App.csproj"), "-c", "Release"]); string app = Path.Combine(appDirectory, "bin", "MultiFixture.App.dll"); @@ -105,7 +138,7 @@ string Extract(string name, string selector, bool referencedBody, string[] extra throw new InvalidOperationException("Artifact did not record the pinned reference: " + pin); string extracted = File.ReadAllText(Path.Combine(artifact, "Extracted.lean")); if (!extracted.Contains("def assembly1 : Extraction.AssemblyPin", StringComparison.Ordinal) || - extracted.Contains("assembly := assembly1", StringComparison.Ordinal) != referencedBody) + extracted.Contains("assembly := assembly1, signature", StringComparison.Ordinal) != referencedBody) throw new InvalidOperationException("Certificate misattributes the referenced assembly's bodies: " + name); // The certificate depends on the program alone; byte hashes and MVIDs stay in identity.json. string[] rebuildSensitive = [libSha, Sha(app), pin.GetProperty("mvid").GetString()!, @@ -142,6 +175,45 @@ void Decided(params (string Name, bool Stepped)[] runs) } Verified("Direct", "MultiFixture.Application::Direct(ulong)", "Tests/MultiAssembly/Direct.lean", referencedBody: true); +Proved("Counter", "MultiFixture.CounterCaller::Read(ulong)", "Tests/MultiAssembly/Counter.lean", referencedBody: true); +// A refutation of a callee from the reference names that callee with its own assembly and digest. +_ = Extract("StepRefuted", "MultiFixture.Application::Direct(ulong)", referencedBody: true, []); +string stepRefutation = Path.Combine(root, "StepRefuted.lean"); +File.WriteAllText(stepRefutation, """ + import Extracted + import CIL.Safety.MethodContract + + /-! A counterexample about Lib's `Mix.Step`, a callee of App's `Direct`. -/ + + open CIL.Safety + namespace MultiAssemblyProof + + /-- Promises that `Step` never returns. -/ + def neverReturns : MethodContract CIL.W64 CIL.W64 where + args x := [.scalar (.i64 x)] + results v := [.scalar (.i64 v)] + pre _ _ := True + footprint _ := [] + post _ _ _ _ := False + + /-- `Step 0` returns. -/ + theorem refuted : neverReturns.Refuted Extracted.program 1 := + .of_return (input := 0) (initial := Memory.empty) trivial + (rfl : invoke Extracted.program 16 1 (neverReturns.args 0) Memory.empty = .ok _) + (fun holds => holds.2.elim fun _ returned => returned.2) + + #print axioms refuted + end MultiAssemblyProof + """); +var refutedStep = Execute([.. cli, "verify", Artifact("StepRefuted"), "--proof", stepRefutation, "--theorem", + "MultiAssemblyProof.refuted"]); +var refutedMethod = JsonDocument.Parse(File.ReadAllText(Path.Combine(Artifact("StepRefuted"), "failed.json"))) + .RootElement.GetProperty("method"); +if (refutedMethod.GetProperty("entry").GetString() != "System.UInt64 MultiFixture.Mix::Step(System.UInt64)" || + refutedMethod.GetProperty("assembly").GetString()?.StartsWith(libName + ", ", StringComparison.Ordinal) != true || + refutedMethod.GetProperty("assemblySha256").GetString() != libSha) + throw new InvalidOperationException($"A refuted callee was reported with another assembly:\n{refutedStep.Output}"); +Console.WriteLine("PASS a refuted callee from a reference is reported with that assembly's identity and digest"); Proved("Nested", "MultiFixture.Application::Nested(ulong)", "Tests/MultiAssembly/Nested.lean", referencedBody: true); Proved("Local", "MultiFixture.Application::Local(ulong)", "Tests/MultiAssembly/Local.lean", referencedBody: true); // Only the layout comes from Lib here, so no body is attributed to it. @@ -187,6 +259,37 @@ void Decided(params (string Name, bool Stepped)[] runs) Proved("ScaledGenericPlain", "MultiFixture.Application::ScaledGenericPlain(ulong)", "Tests/MultiAssembly/Scaled.lean", referencedBody: true); +// Compile the caller against the original facade, then supply the facade that forwards the type. +(string Caller, string[] References) ProveForwardedCall() +{ + const string facadeName = "ForwardFacade"; + fixture.Build("OriginalFacade", facadeName, + "namespace Forward; public static class Value { public static int Next(int input) => input; }"); + var caller = fixture.Build("ForwardCaller", "ForwardCaller", + "namespace Forward; public static class Entry { public static int Run(int value) => Value.Next(value); }", + references: """"""); + var implementation = fixture.Build("ForwardImplementation", "ForwardImplementation", + "namespace Forward; public static class Value { public static int Next(int input) => input + 1; }"); + var facade = fixture.Build("ForwardFacade", facadeName, + "[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(Forward.Value))]", + references: """"""); + var artifact = Artifact("ForwardedCall"); + Run([.. cli, "extract", caller, "Entry::Run(int)", artifact, .. Reference(facade), .. Reference(implementation)]); + var proof = Path.Combine(root, "accepted-ForwardedCall.lean"); + File.WriteAllText(proof, File.ReadAllText(Path.Combine(artifact, "Extracted.lean")) + """ + + open CIL in + theorem forwarded_call (value : W32) : + (Safety.invoke Extracted.program 16 0 [.scalar (.i32 value)] Safety.Memory.empty).map Prod.snd = + .ok [.scalar (.i32 (value + 1))] := rfl + #print axioms forwarded_call + """); + evidence.Add(proof, "PASS a facade binds the implementation used by a compiled caller"); + return (caller, [facade, implementation]); +} + +var forwarded = ProveForwardedCall(); + evidence.Check(); void Rejected(string name, string marker, string[] command) => MultiAssemblyFixture.Rejected(name, marker, command); @@ -210,6 +313,77 @@ string RejectedExtract(string name, string marker, string selector, params strin RejectedExtract("unpinned receiver interface", "Unpinned assembly reference: MultiFixture.Other", scaled, [.. Reference(lib), "--type-argument", "MultiFixture.Marked"]); +// A pinned type its assembly does not define is refused (by the class check, ahead of the metadata slice's own +// "Unresolved pinned type" backstop), unlike a custom modifier, which needs no definition at all. +string gone = fixture.Build("Gone", libName, libraryText.Replace("STEP", "3 + 1") + .Replace("public sealed class Marker { }", "")); +RejectedExtract("pinned type without definition", "Unresolved class: MultiFixture.Marker", + "MultiFixture.Application::Take(MultiFixture.Marker,ulong)", Reference(gone)); +// No extraction reaches the metadata slice's own refusal, since the aggregate and class checks run first, so the +// collector is called directly on the method whose signature names the pinned type the library no longer defines. +{ + const System.Reflection.BindingFlags Internal = System.Reflection.BindingFlags.Instance | + System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public; + var extractor = System.Reflection.Assembly.LoadFrom(ExtractorPath); + var pinnedType = extractor.GetType("Nethermind.LeanCIL.PinnedAssemblies", throwOnError: true)!; + var sourceType = pinnedType.GetNestedType("Source", System.Reflection.BindingFlags.NonPublic | + System.Reflection.BindingFlags.Public)!; + var sources = (System.Collections.IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(sourceType))!; + sources.Add(Activator.CreateInstance(sourceType, gone, null)!); + var pinned = Activator.CreateInstance(pinnedType, Internal, null, + [File.ReadAllBytes(app), Sha(app), sources, null], null)!; + var slices = Activator.CreateInstance(extractor.GetType("Nethermind.LeanCIL.MetadataSlices", throwOnError: true)!, + pinned)!; + var selected = (Mono.Cecil.ModuleDefinition)pinnedType.GetProperty("Selected", Internal)!.GetValue(pinned)!; + var original = (Mono.Cecil.ModuleDefinition)pinnedType.GetMethod("Original", Internal)! + .Invoke(pinned, [selected])!; + var take = original.GetType("MultiFixture.Application").Methods.Single(method => method.Name == "Take"); + var collect = slices.GetType().GetMethod("Method", Internal)!; + try + { + collect.Invoke(slices, [take, Array.Empty(), false, Array.Empty()]); + throw new InvalidOperationException("The metadata slice accepted a pinned type with no definition."); + } + catch (System.Reflection.TargetInvocationException refusal) when ( + refusal.InnerException is InvalidDataException { Message: "Unresolved pinned type: MultiFixture.Marker" }) + { + Console.WriteLine("PASS the metadata slice refuses a pinned non-modifier type with no definition"); + } +} + +// Extraction is deterministic across processes. The order of --reference options numbers the pins in the +// certificate, so only extractions with the same order write the same files; every order pins the same assemblies. +void Deterministic(string name, string selector, string[][] referenceOrders, params string[] extra) +{ + string[] outputs = [.. referenceOrders.Select((references, index) => + { + string output = Artifact("determinism-" + name + "-" + index); + Run([.. cli, "extract", app, selector, output, .. references.SelectMany(Reference), .. extra]); + return output; + })]; + string[] Pins(string output) => [.. JsonSerializer.Deserialize( + File.ReadAllText(Path.Combine(output, "identity.json"))).GetProperty("references").EnumerateArray() + .Select(pin => pin.GetProperty("sha256").GetString()!).Order(StringComparer.Ordinal)]; + for (int index = 1; index < outputs.Length; index++) + { + int same = Array.FindIndex(referenceOrders, order => order.SequenceEqual(referenceOrders[index])); + if (same < index && FirstDifference(outputs[same], outputs[index]) is { } difference) + throw new InvalidOperationException($"Extraction of {name} is not deterministic: {difference}"); + if (!Pins(outputs[0]).SequenceEqual(Pins(outputs[index]))) + throw new InvalidOperationException($"Reference order changed the pinned assemblies of {name}"); + } + Console.WriteLine($"PASS cross-assembly {name}: {outputs.Length} extractions write identical files per order"); +} + +string[][] twice = [[lib], [lib]]; +string[][] bothOrders = [[lib, other], [lib, other], [other, lib], [other, lib]]; +Deterministic("Direct", direct, twice); +Deterministic("Size", "MultiFixture.Application::Size(ulong)", twice, + "--type-argument", "MultiFixture.Cell, " + libName); +Deterministic("RunGenericOn", "MultiFixture.Application::RunGenericOn(ulong)", twice); +Deterministic("PlainBothPins", scaled, bothOrders, "--type-argument", "MultiFixture.Plain, " + libName); +Deterministic("MarkedBothPins", scaled, bothOrders, "--type-argument", "MultiFixture.Marked"); + // Proofs bind bodies by unscoped signature, so a same-named type in two pinned assemblies is rejected. string aliasDirectory = fixture.Project("Alias", "MultiFixture.Alias", "", """"""); @@ -276,7 +450,7 @@ void Edit(string path, Action change) throw new InvalidOperationException("Precondition: the certificate must pin " + libIdentity); foreach ((string name, string from, string to) in new[] { - ("misattributed body", "assembly := assembly1", "assembly := assembly0"), + ("misattributed body", "assembly := assembly1, signature", "assembly := assembly0, signature"), ("duplicate certificate identity", $"identity := \"{libIdentity}\"", $"identity := \"{appIdentity}\"") }) { @@ -365,4 +539,18 @@ void Edit(string path, Action change) Console.WriteLine("PASS pinned referenced assemblies: proofs, missing, duplicate, substituted, misattributed"); // Generic structs and generic class statics run in the same job with their own Lib and App. -CheckGenericAssemblies(); +var generic = CheckGenericAssemblies(); + +// The strict binding library decodes images as an independent reader does, and its own tests pass. +Run(["lake", "build", "StrictBinding", "Tests.StrictBinding"], FixturePackage(), + progress: "Building strict binding tests"); +Console.WriteLine("PASS strict binding library: SHA-256, SHA-1, container and table decoding tests"); +CheckBindingDecoding(Path.Combine(root, "binding")); +// The slices the extractor carries for every method of the fixture and of both assembly pairs. +string strictFixture = Path.Combine(root, "StrictFixture.dll"); +File.WriteAllBytes(strictFixture, StrictBindingFacts.ReadImage(File.ReadAllText(Fixture))); +CheckExtractedSlices(Path.Combine(root, "binding-slices"), FixturePackage(), + (strictFixture, []), (app, [lib]), (lib, []), + (generic.App, [generic.Lib]), (generic.Lib, []), forwarded); +CheckBindingScaling(Path.Combine(root, "binding-scaling")); +CheckDeepBinding(root, cli, Artifact("Direct"), app, lib, libName, Verify(Artifact("Direct"))); diff --git a/Tests/MultiAssembly/Counter.lean b/Tests/MultiAssembly/Counter.lean new file mode 100644 index 000000000..9ec14df44 --- /dev/null +++ b/Tests/MultiAssembly/Counter.lean @@ -0,0 +1,36 @@ +import Extracted +import CIL.Safety.DecodedSummary + +/-! A direct call through a local struct into a second pinned assembly. -/ + +open CIL CIL.Safety CIL.Extraction +namespace MultiAssemblyProof + +/-- Calling the library through the local struct returns three times five plus one. -/ +theorem correctness : + (invoke Extracted.program 64 0 [.scalar (.i64 5)] Safety.Memory.empty).map Prod.snd = + .ok [.scalar (.i64 16)] := by rfl + +#print axioms correctness + +/-- The struct's declaring type binds its method to the application assembly and definition token. -/ +theorem counter_callee {pin : AssemblyPin} {token : Nat} {arguments : List TypeExpr} + (bound : DecodedAt Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment 1 pin token arguments) : + pin.identity = Extracted.assembly0.identity ∧ token = Extracted.method1Original.token ∧ + arguments = [] := + bound.direct_site Extracted.attribution_validated Extracted.method0_calls_validated + (pc := 1) rfl rfl rfl rfl rfl rfl rfl (by decide +kernel) + +#print axioms counter_callee + +/-- The referenced library cannot supply the application's struct method. -/ +theorem foreign_pin_refused {token : Nat} {arguments : List TypeExpr} : + ¬ DecodedAt Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment 1 Extracted.assembly1 token arguments := by + intro bound + exact (by decide : Extracted.assembly1.identity ≠ Extracted.assembly0.identity) + (counter_callee bound).1 + +#print axioms foreign_pin_refused +end MultiAssemblyProof diff --git a/Tests/MultiAssembly/Direct.lean b/Tests/MultiAssembly/Direct.lean index b219df73a..e55a7bf8c 100644 --- a/Tests/MultiAssembly/Direct.lean +++ b/Tests/MultiAssembly/Direct.lean @@ -1,4 +1,6 @@ import Extracted +import CIL.Safety.DecodedSummary +import CIL.Safety.SymbolicExecution /-! Direct call from the selected assembly into a pinned referenced assembly's static method. -/ @@ -12,4 +14,83 @@ theorem correctness (x : W64) : rfl #print axioms correctness +open Safety Extraction + +/-- The library method returns three times its input plus one and preserves caller memory. -/ +def stepContract : MethodContract W64 W64 where + args x := [.scalar (.i64 x)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post x initial final value := value = x * 3 + 1 ∧ final = initial + +/-- The compiled definition of Step is the second method, from the pinned reference. -/ +theorem step_summary : + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment 1 Extracted.assembly1 Extracted.method1Original.token [] + stepContract := by + apply DecodedSummary.of_translation Extracted.translation_valid ⟨1, by decide⟩ + Extracted.program_method1 + intro x memory _ + exact MethodContract.Holds.intro memory (x * 3 + 1) ⟨8, rfl⟩ (Confined.refl _ _) ⟨rfl, rfl⟩ + +#print axioms step_summary + +/-- The caller's MemberRef resolves to Step's MethodDef in the referenced library's slice. -/ +theorem direct_member_resolution : + ∃ env site position, + Extracted.method0Body.environment = .ok env ∧ + env.methodSite Extracted.method0Body.tokens.operands[0].1 = .ok site ∧ + site.definition = some (position, Extracted.method1Original.token) ∧ + Extracted.method0Body.identities[position]? = + some (bytesOf Extracted.assembly1.identity) := by + obtain ⟨token, env, frame, site, position, listed, built, _, resolved, _, definition⟩ := + Extracted.method0_body_validated.callManagedTarget (pc := 1) + (target := Extracted.method1Original.token) rfl rfl rfl + have singleton : Extracted.method0Body.tokens.operands = + [Extracted.method0Body.tokens.operands[0]] := rfl + rw [singleton, List.mem_singleton] at listed + have same := congrArg Prod.fst listed + change token = _ at same + subst token + refine ⟨env, site, position, built, resolved, definition, ?_⟩ + cases built + cases resolved + cases definition + rfl + +#print axioms direct_member_resolution + +/-- At App.Direct's call, the bound callee must belong to Lib. Both assemblies use the same + MethodDef token for their respective methods. -/ +theorem direct_callee_identity {pin : AssemblyPin} {token : Nat} {arguments : List TypeExpr} + (bound : DecodedAt Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment 1 pin token arguments) : + pin.identity = Extracted.assembly1.identity ∧ token = Extracted.method0Original.token ∧ + arguments = [] := + bound.direct_site Extracted.attribution_validated Extracted.method0_calls_validated + (pc := 1) rfl rfl rfl rfl rfl rfl rfl (by decide +kernel) + +#print axioms direct_callee_identity + +/-- The identical token in the calling assembly cannot certify the summary of the library call. -/ +theorem app_pin_refused : + ¬ DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment 1 Extracted.assembly0 Extracted.method0Original.token [] + stepContract := by + intro summary + exact (by decide : Extracted.assembly0.identity ≠ Extracted.assembly1.identity) + (direct_callee_identity summary.bound).1 + +#print axioms app_pin_refused + +/-- The caller uses the library summary and returns the incremented result in unchanged memory. -/ +theorem direct_uses_summary (x : W64) (memory : Safety.Memory) : + WP (Execution (fun fuel => invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory)) + (fun (final, values) => final = memory ∧ values = [.scalar (.i64 (x * 3 + 1 + 1))]) := by + cil_contract_steps [step_summary.holds, stepContract] with after value result + obtain ⟨rfl, rfl⟩ := result.post + exact ⟨rfl, rfl⟩ + +#print axioms direct_uses_summary end MultiAssemblyProof diff --git a/Tests/MultiAssemblyFixture.cs b/Tests/MultiAssemblyFixture.cs index 8dd8631a4..dd7429cc8 100644 --- a/Tests/MultiAssemblyFixture.cs +++ b/Tests/MultiAssemblyFixture.cs @@ -8,13 +8,13 @@ sealed class MultiAssemblyFixture(string name) internal string[] Cli { get; } = ["dotnet", ExtractorPath]; - /// Writes a Release net10.0 project and returns its directory. + /// Writes a Release net11.0 project and returns its directory. internal string Project(string name, string assembly, string properties = "", string references = "") { string directory = Path.Combine(Root, name); Directory.CreateDirectory(directory); File.WriteAllText(Path.Combine(directory, name + ".csproj"), $""" - net10.0 + net11.0 {assembly}true{properties} {references} """); diff --git a/Tests/MultiAssemblyGenerics.cs b/Tests/MultiAssemblyGenerics.cs index 65080b0e8..79d5c1cd0 100644 --- a/Tests/MultiAssemblyGenerics.cs +++ b/Tests/MultiAssemblyGenerics.cs @@ -4,8 +4,8 @@ partial class Program { /// Generic structs and static methods of generic classes across their own SDK-built Lib and - /// App pair, run by the assemblies suite after the non-generic cases. - static void CheckGenericAssemblies() + /// App pair, run by the assemblies suite after the non-generic cases; returns the App and Lib it built. + static (string App, string Lib) CheckGenericAssemblies() { const string libName = "GenericFixture.Lib"; const string appName = "GenericFixture.App"; @@ -150,7 +150,7 @@ string Extract(string name, string selector, bool referencedBody, string[] extra string artifact = fixture.Artifact(name); Run([.. cli, "extract", app, selector, artifact, .. Reference(lib), .. extra]); string extracted = File.ReadAllText(Path.Combine(artifact, "Extracted.lean")); - if (extracted.Contains("assembly := assembly1", StringComparison.Ordinal) != referencedBody) + if (extracted.Contains("assembly := assembly1, signature", StringComparison.Ordinal) != referencedBody) throw new InvalidOperationException("Certificate misattributes the referenced bodies: " + name); return extracted; } @@ -165,7 +165,7 @@ void Proved(string name, string selector, ulong result, bool referencedBody, par theorem generic_result : (CIL.Safety.invoke Extracted.program 64 0 [.scalar (.i64 5)] CIL.Safety.Memory.empty).map Prod.snd = - .ok [.scalar (.i64 {{result}})] := by rfl + .ok [.scalar (.i64 {{result}})] := by cil_kernel_rfl #print axioms generic_result """); evidence.Add(path, "PASS cross-assembly generic " + name + ": pinned layouts and bodies with proof"); @@ -242,13 +242,13 @@ [.atom CIL.Extraction.runtimeScope "System.UInt64"]⟩ /-- A catalogued handler runs; `LocalAlgo.Run`, addressed by no extracted body, does not. -/ theorem correctness : (CIL.Safety.invoke Extracted.program 64 0 - [.methodTable ⟨signature, some #[{{identity}}], none⟩, {{arguments}}] + [.methodTable ⟨signature, some #[{{identity}}], none, 1, by decide⟩, {{arguments}}] CIL.Safety.Memory.empty).map Prod.snd = .ok [.scalar (.i64 {{result}})] ∧ (match CIL.Safety.invoke Extracted.program 64 0 - [.methodTable ⟨signature, some #[{{uncatalogued}}], none⟩, {{arguments}}] + [.methodTable ⟨signature, some #[{{uncatalogued}}], none, 1, by decide⟩, {{arguments}}] CIL.Safety.Memory.empty with | .error fault => some fault.fault | .ok _ => none) = some .invalidState := - ⟨by rfl, by rfl⟩ + ⟨by cil_kernel_rfl, by cil_kernel_rfl⟩ #print axioms correctness """; @@ -345,5 +345,6 @@ void RejectedExtract(string name, string marker, string assembly, string selecto throw new InvalidOperationException("Lean accepted a moved generic layout owner: " + kernel.Output); Console.WriteLine("PASS Lean rejected moved generic owner"); Console.WriteLine("PASS cross-assembly generics: structs, generic classes, entries, addresses and rejections"); + return (app, lib); } } diff --git a/Tests/NarrowIntegers.cs b/Tests/NarrowIntegers.cs index 2eab93bf2..43e6b37bd 100644 --- a/Tests/NarrowIntegers.cs +++ b/Tests/NarrowIntegers.cs @@ -2,6 +2,13 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include EmptyReturnVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Numerics; @@ -235,18 +242,16 @@ void VerifyCases(string dll, string selector, string output, int inputWidth, module.Write(incorrectDll); } -var rejected = false; -try -{ - verification.Verify(incorrectDll, "Examples", "NarrowIntegers::UnsignedShort(int)", - At("Examples/Proofs/UnsignedShort.lean"), incorrect); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - rejected = true; -} - -if (!rejected || File.Exists(Path.Combine(incorrect, "verified.json"))) - throw new InvalidOperationException("Incorrect signed conversion accepted"); +verification.VerifyEmptyReturn(assembly, "NarrowIntegers::UnsignedShort(int)", + At("Examples/Proofs/UnsignedShort.lean"), incorrectDll, "NarrowIntegers::UnsignedShort(int)", incorrect, + new("W32", "[.scalar (.i32 input)]", ".scalar (.i32 (BitVec.ofNat 32 (input.toNat % 65536)))", 3, + """ + obtain ⟨result, ran, value⟩ := ExampleProof.correctness input + have same : result = BitVec.ofNat 32 (input.toNat % 65536) := by + apply BitVec.eq_of_toNat_eq + simp only [BitVec.toNat_ofNat] + omega + exact same ▸ ran + """, "32768")); Console.WriteLine($"Narrow integer proofs passed: {cases} boundary cases; incorrect signed conversion rejected."); diff --git a/Tests/NativeIntegers.cs b/Tests/NativeIntegers.cs index 0a67f2329..f86afc6a8 100644 --- a/Tests/NativeIntegers.cs +++ b/Tests/NativeIntegers.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include NativeShiftCounts.cs diff --git a/Tests/NativeManagedDispatch.cs b/Tests/NativeManagedDispatch.cs index a32051202..219405acd 100644 --- a/Tests/NativeManagedDispatch.cs +++ b/Tests/NativeManagedDispatch.cs @@ -2,11 +2,15 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs -using System.Text.RegularExpressions; using Mono.Cecil; using Mono.Cecil.Cil; @@ -14,6 +18,8 @@ Directory.CreateDirectory(root); var dll = Path.Combine(root, "NativeManagedDispatch.dll"); string selector; +int loadPc; +int addPc; using (var module = ModuleDefinition.ReadModule(At("Examples/Assembly/bin/Examples.dll"))) { var methods = module.GetType("Examples.NativeManagedDispatch").Methods; @@ -35,21 +41,52 @@ processor.InsertBefore(first, Instruction.Create(OpCodes.Stloc, numeric)); processor.InsertBefore(first, Instruction.Create(OpCodes.Ldloc, numeric)); processor.InsertBefore(first, Instruction.Create(OpCodes.Pop)); + var instructions = dispatch.Body.Instructions; + var load = instructions.Single(instruction => instruction.OpCode.Code == Code.Ldind_I); + if (load.Previous?.OpCode.Code != Code.Add) throw new InvalidOperationException("Missing native table offset"); + loadPc = instructions.IndexOf(load); + addPc = loadPc - 1; selector = dispatch.FullName; module.Write(dll); } var output = Path.Combine(root, "extracted"); Run(["dotnet", ExtractorPath, "extract", dll, selector, output]); var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); -if (Regex.Matches(source, @"(?m)^def method\d+ : Method :=").Count != 2) +if (DefinedMethods(source).Length != 2) throw new InvalidOperationException("Native dispatch did not retain a bounded cyclic catalog"); var scalar = NarrowEvidence(source, "method0_scalars_validated"); -var proof = source + "\n" + CheckedEvidenceControl(source, scalar, ["method0_scalars_validated"], 0) + "\n" + """ +var indirect = SingleLink(source, 0, ".indirect "); +var decode = $$""" + + open CIL CIL.Extraction in + example : + let code := Extracted.method0Closed.code + let pc := {{indirect.Pc}} + let before := rawBefore (fun index => code[index]?) pc + ((code[pc]?).bind (Instruction.ofRaw? before) matches + some (.calli _ true)) = true := by decide +kernel + + """; +var control = CheckedEvidenceControl(source, scalar, ["method0_scalars_validated"], 0); +var proof = $$""" +{{source}} +{{decode}} +{{control}} + open CIL CIL.Safety private def nativeSignature : ManagedSignature := ⟨Extracted.method1Closed.returnType, Extracted.method1Closed.parameters⟩ private def nativeIdentity : ManagedIdentity := ⟨Extracted.method1Closed.owner, Extracted.method1Closed.token, []⟩ +/-- The decoded calli accepts the handler's slot and refuses its identity paired with slot zero. -/ +theorem native_catalog_binding : + let records := Extracted.methodEvidence + let changed := records.modify 0 fun item => { item with links := item.links.map fun entry => + if entry.1 == {{indirect.Pc}} then (entry.1, .indirect [⟨nativeIdentity, 0⟩]) else entry } + let decode := Extraction.decodedProgram? Extracted.profile Extracted.environment + decode records = some Extracted.program ∧ decode changed = none := by + constructor <;> cil_kernel_rfl +#print axioms native_catalog_binding private def nativeMemory : CIL.Safety.Memory := { allocations := fun index => if index == 0 then some { kind := .immutableStatic, layout := ⟨8, 8, []⟩ } else none @@ -58,7 +95,7 @@ open CIL CIL.Safety nextIdentity := 1 staticBindings := [(42, ⟨0, 0⟩)] } private def nativeTable : MethodTable := - ⟨nativeSignature, some #[nativeIdentity], some ⟨42, 0⟩⟩ + ⟨nativeSignature, some #[nativeIdentity], some ⟨42, 0⟩, 1, by decide⟩ private def nativeRun (table : MethodTable) (index : Nat) (marker : BitVec 64) (memory : CIL.Safety.Memory := nativeMemory) := CIL.Safety.invoke Extracted.program 128 0 @@ -72,7 +109,7 @@ by decide +kernel /-- The same supplied native entry may terminate with its named modeled exception. -/ theorem native_throw : (match nativeRun nativeTable 0 (-1) with | .error fault => some fault.fault | .ok _ => none) = - some (.thrown "System.InvalidOperationException") := by decide +kernel + some (.thrown .invalidOperation) := by decide +kernel #print axioms native_throw /-- A numeric local beside the authenticated target keeps its ordinary scalar representation. -/ theorem numeric_local : Extracted.method0.localKinds[1]? = some .word64 := by decide +kernel @@ -92,8 +129,10 @@ by decide +kernel #print axioms native_invalid_storage /-- The catalog reauthenticates opaque identities obtained from supplied storage. -/ theorem native_forged_identity : - (nativeRun { nativeTable with targets := some #[{ nativeIdentity with token := 0 }] } 0 0).isOk = - false := by decide +kernel + (nativeRun + { nativeTable with + targets := some #[{ nativeIdentity with token := 0 }], counted := by decide } + 0 0).isOk = false := by decide +kernel #print axioms native_forged_identity """; RequireLeanFixtures("CIL.Extraction.Translation", "CIL.SymbolicExecution", "Tests.NativeMethodTables"); @@ -102,15 +141,17 @@ by decide +kernel File.WriteAllText(path, proof); evidence.Add(path, "PASS native supplied-table cycle, exception, scalar local and storage checks"); var rejected = new RejectedEvidence(root); -var load = Regex.Match(scalar, @"(?m)^ \.managed \(\.tableLoad [^\r\n]+$").Value; -if (load.Length == 0) throw new InvalidOperationException("Missing native table load"); +// The decoder still reads the genuine body when a mutation changes the closed evidence. +string ChangeRaw(string narrowed, int pc, string field) => + ForgeClosedCode(narrowed, 0, $$"""modify {{pc}} (fun raw => { raw with {{field}} })"""); foreach (var (name, mutated) in new[] { - ("signature", scalar.Replace(load, load.Replace("System.Int32", "System.UInt32", StringComparison.Ordinal), - StringComparison.Ordinal)), - ("raw-load", scalar.Replace("opcode := 77, operand := .none", "opcode := 76, operand := .none", - StringComparison.Ordinal)), - ("offset", scalar.Replace(".managed .tableOffset", ".nativeBinary (.add)", StringComparison.Ordinal)), + ("load-kind", ForgeDecodedInstruction(scalar, 0, loadPc, "fun _ => .ldind .i8")), + ("raw-load", ChangeRaw(scalar, loadPc, "opcode := 76")), + ("emitted-sub", ForgeDecodedInstruction(scalar, 0, addPc, "fun _ => .sub")), + ("emitted-add-ovf", ForgeDecodedInstruction(scalar, 0, addPc, "fun _ => .add_ovf")), + ("raw-sub", ChangeRaw(scalar, addPc, "opcode := 89")), + ("raw-operand", ChangeRaw(scalar, addPc, "operand := .local 0")), ("input", scalar.Replace(".pointer (.atom Extraction.runtimeScope \"System.IntPtr\")", ".pointer (.atom Extraction.runtimeScope \"System.Int64\")", StringComparison.Ordinal)) }) @@ -121,6 +162,19 @@ by decide +kernel rejected.Add(rejectedPath, "PASS independently rejected native dispatch evidence: " + name, "Extraction.ScalarFlowValid"); } +// No emitted instruction carries the table's signature; the call-site check binds the calli signature to the raw +// operand, so a changed signature is rejected there. +var calls = NarrowEvidence(source, "method0_calls_validated"); +const string changeCall = """ + fun op => match op with + | .calli signature tail => .calli { signature with result := .void } tail + | _ => op + """; +var changedCalls = ForgeDecodedInstruction(calls, 0, indirect.Pc, changeCall.ReplaceLineEndings(" ")); +var signaturePath = Path.Combine(root, "Rejected-call-signature.lean"); +File.WriteAllText(signaturePath, changedCalls); +rejected.Add(signaturePath, "PASS independently rejected native dispatch evidence: call-signature", + "ManagedCallsValid"); var numericDll = Path.Combine(root, "NumericTarget.dll"); using (var module = ModuleDefinition.ReadModule(dll)) { @@ -139,10 +193,10 @@ by decide +kernel { var dispatch = module.GetType("Examples.NativeManagedDispatch").Methods.Single(method => method.Name == "Dispatch"); var addition = dispatch.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Add); - var tableLoad = dispatch.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Ldarg_0); + var tableArgument = dispatch.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Ldarg_0); var processor = dispatch.Body.GetILProcessor(); - processor.Remove(tableLoad); - processor.InsertBefore(addition, tableLoad); + processor.Remove(tableArgument); + processor.InsertBefore(addition, tableArgument); module.Write(commutedDll); } var commutedOutput = Path.Combine(root, "Commuted"); @@ -164,9 +218,7 @@ open CIL CIL.Extraction #print axioms commuted_native_kind """); evidence.Add(commutedProof, "PASS commuted native table extraction, certificate and exact offset kinds"); -var wrongAddition = commutedScalar.Replace("opcode := 88, operand := .none", - "opcode := 89, operand := .none", StringComparison.Ordinal); -if (wrongAddition == commutedScalar) throw new InvalidOperationException("Missing commuted native addition"); +var wrongAddition = ChangeRaw(commutedScalar, addPc, "opcode := 89"); var wrongAdditionPath = Path.Combine(root, "Rejected-CommutedAddition.lean"); File.WriteAllText(wrongAdditionPath, wrongAddition); rejected.Add(wrongAdditionPath, "PASS independently rejected commuted subtraction evidence", diff --git a/Tests/NativeMethodTables.lean b/Tests/NativeMethodTables.lean index 0fec49aaa..a2c109210 100644 --- a/Tests/NativeMethodTables.lean +++ b/Tests/NativeMethodTables.lean @@ -1,6 +1,8 @@ -import CIL.Safety.Execution +import CIL.Safety.MemorySteps +import CIL.Extraction.Scalars -/-! Native handler tables preserve opaque identities across checked backing-memory accesses. -/ +/-! Native handler tables preserve opaque identities across checked backing-memory accesses, and +the scalar transfer admits a managed table only to `ldlen` and its pointer-typed element reads. -/ namespace Tests.NativeMethodTables @@ -20,7 +22,7 @@ private def memory : CIL.Safety.Memory := nextIdentity := 1 staticBindings := [(42, ⟨0, 0⟩)] } -private def table : NativeMethodTable := ⟨signature, #[first, second], ⟨0, 0⟩, ⟨0, 0⟩⟩ +private def table : NativeMethodTable := ⟨signature, #[first, second], ⟨0, 0⟩, ⟨0, 0⟩, 2, rfl⟩ /-- Adding an entry stride selects the next authenticated identity. -/ theorem native_load : @@ -81,50 +83,418 @@ theorem unrelated_cursor : #print axioms unrelated_cursor private def opaqueTable : MethodTable := - ⟨signature, some #[first, second], some ⟨42, 0⟩⟩ + { signature, targets := some #[first, second], backing := some ⟨42, 0⟩, entryCount := 2 } private def frame : Frame := { activation := 0, locals := [], owned := [] } +/-- A body with no code; `step` reads only its profile and its static and link tables. -/ +private def body : CIL.Method := { code := [], locals := [], returnsValue := false } + +/-- The opaque table with its cursor `byteOffset` bytes past the table origin. -/ +private def cursorAt (byteOffset : Nat) : MethodTable := + { opaqueTable with backing := some ⟨42, byteOffset⟩ } + +/-- One decoded `add` over `stack` at pc 0 in `storage`. -/ +private def addStep (stack : List CIL.Safety.Value) (storage : CIL.Safety.Memory := memory) : + ExecutionResult FrameAction := + step body .add 0 [] frame stack storage + +/-- A native table fault, attributed to a null reference and the eight-byte entry width. -/ +private def tableFault (rule : Fault) : ExecutionResult FrameAction := + .error (.memory rule .null nativeMethodWidth) + /-- The native `add` followed by `ldind.i` preserves a checked, callable identity. -/ theorem native_dispatch_load : - managedStep .tableOffset 0 frame [.scalar (.i64 8), .methodTable opaqueTable] memory = - .ok (.next 1 [.methodTable { opaqueTable with backing := some ⟨42, 8⟩ }] frame memory) ∧ - managedStep (.tableLoad signature) 1 frame - [.methodTable { opaqueTable with backing := some ⟨42, 8⟩ }] memory = - .ok (.next 2 [.methodPointer ⟨signature, some second⟩] frame memory) := ⟨rfl, rfl⟩ + addStep [.scalar (.i64 8), .methodTable opaqueTable] = + .ok (.next 1 [.methodTable (cursorAt 8)] frame memory) ∧ + step body (.ldind .i) 1 [] frame [.methodTable (cursorAt 8)] memory = + .ok (.next 2 [.methodPointer ⟨signature, some second⟩] frame memory) := ⟨rfl, rfl⟩ #print axioms native_dispatch_load -/-- Commuted native table addition uses the same checked cursor operation for every offset. -/ -theorem native_add_commutes (offset : CIL.Value) (table : MethodTable) - (memory : CIL.Safety.Memory) (frame : Frame) (rest : List CIL.Safety.Value) (pc : Nat) : - managedStep .tableOffset pc frame (.methodTable table :: .scalar offset :: rest) memory = - managedStep .tableOffset pc frame (.scalar offset :: .methodTable table :: rest) memory := by +/-- Both operand orders and both offset widths select the same cursor. -/ +theorem native_add_orders : + addStep [.scalar (.i32 8), .methodTable opaqueTable] = + .ok (.next 1 [.methodTable (cursorAt 8)] frame memory) ∧ + addStep [.methodTable opaqueTable, .scalar (.i32 8)] = + .ok (.next 1 [.methodTable (cursorAt 8)] frame memory) ∧ + addStep [.methodTable opaqueTable, .scalar (.i64 8)] = + .ok (.next 1 [.methodTable (cursorAt 8)] frame memory) := ⟨rfl, rfl, rfl⟩ +#print axioms native_add_orders + +/-- A negative int32 offset is sign-extended and moves a nonzero cursor back to the origin. -/ +theorem native_add_negative : + addStep [.scalar (.i32 (-8)), .methodTable (cursorAt 8)] = + .ok (.next 1 [.methodTable (cursorAt 0)] frame memory) := rfl +#print axioms native_add_negative + +/-- Commuted native table addition is the same step for every offset, including rejected ones. -/ +theorem native_add_commutes (offset : CIL.Value) (table : MethodTable) (code : CIL.Method) + (memory : CIL.Safety.Memory) (frame : Frame) (args rest : List CIL.Safety.Value) (pc : Nat) : + step code .add pc args frame (.methodTable table :: .scalar offset :: rest) memory = + step code .add pc args frame (.scalar offset :: .methodTable table :: rest) memory := by cases offset <;> rfl #print axioms native_add_commutes -/-- Commuting the operands cannot admit a cursor outside the authenticated allocation. -/ -theorem commuted_invalid_offset : - (managedStep .tableOffset 0 frame [.methodTable opaqueTable, .scalar (.i64 24)] memory).isOk = - false := by rfl -#print axioms commuted_invalid_offset +/-- Cursor arithmetic cannot leave the table in either operand order: underflow and a cursor + past the one-past position fault with the cursor rule. -/ +theorem native_add_out_of_range : + addStep [.scalar (.i32 (-8)), .methodTable opaqueTable] = tableFault .invalidReference ∧ + addStep [.scalar (.i64 24), .methodTable opaqueTable] = tableFault .invalidReference ∧ + addStep [.methodTable opaqueTable, .scalar (.i64 24)] = tableFault .invalidReference := + ⟨rfl, rfl, rfl⟩ +#print axioms native_add_out_of_range + +/-- A one-past cursor and a misaligned interior cursor can be formed but not loaded. -/ +theorem native_add_unloadable : + addStep [.scalar (.i64 16), .methodTable opaqueTable] = + .ok (.next 1 [.methodTable (cursorAt 16)] frame memory) ∧ + step body (.ldind .i) 1 [] frame [.methodTable (cursorAt 16)] memory = + tableFault .outsideAllocation ∧ + addStep [.scalar (.i32 1), .methodTable opaqueTable] = + .ok (.next 1 [.methodTable (cursorAt 1)] frame memory) ∧ + step body (.ldind .i) 1 [] frame [.methodTable (cursorAt 1)] memory = + tableFault .alignment := ⟨rfl, rfl, rfl, rfl⟩ +#print axioms native_add_unloadable + +/-- Addition checks the storage binding and allocation, but not read authority or + initialization, which the following load checks. -/ +theorem native_add_storage : + addStep [.scalar (.i64 8), .methodTable opaqueTable] + { memory with allocations := (fun _ => + some { kind := .immutableStatic, layout := ⟨16, 8, []⟩, live := false }) } = + tableFault .expiredLifetime ∧ + addStep [.scalar (.i64 8), .methodTable opaqueTable] + { memory with allocations := (fun _ => + some { kind := .managedHeap, layout := ⟨16, 8, []⟩ }) } = + tableFault .unsupportedLayout ∧ + addStep [.scalar (.i64 8), .methodTable { opaqueTable with backing := none }] = + tableFault .invalidReference ∧ + addStep [.scalar (.i64 8), + .methodTable { opaqueTable with targets := none, counted := nofun }] = + tableFault .nullDereference ∧ + addStep [.scalar (.i64 8), .methodTable opaqueTable] { memory with staticBindings := [] } = + tableFault .missingAllocation ∧ + addStep [.scalar (.i64 8), .methodTable opaqueTable] { memory with views := [] } = + .ok (.next 1 [.methodTable (cursorAt 8)] frame { memory with views := [] }) ∧ + step body (.ldind .i) 1 [] frame [.methodTable (cursorAt 8)] + { memory with views := [] } = tableFault .unreadable ∧ + addStep [.scalar (.i64 8), .methodTable opaqueTable] + { memory with cells := fun _ _ => ⟨0, false⟩ } = + .ok (.next 1 [.methodTable (cursorAt 8)] frame + { memory with cells := fun _ _ => ⟨0, false⟩ }) ∧ + step body (.ldind .i) 1 [] frame [.methodTable (cursorAt 8)] + { memory with cells := fun _ _ => ⟨0, false⟩ } = tableFault .uninitialized := + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ +#print axioms native_add_storage + +/-- Offsets other than int32 and int64, short stacks and two tables are rejected; unspecified + offset bits are rejected before the offset is read. -/ +theorem native_add_rejected : + addStep [.scalar (.i8 8), .methodTable opaqueTable] = .error .invalidState ∧ + addStep [.methodTable opaqueTable, .scalar (.i8 8)] = .error .invalidState ∧ + addStep [.scalar (.v128Low64 8), .methodTable opaqueTable] = + .error (.unsupported "Instruction observes unspecified vector bits") ∧ + addStep [.methodTable opaqueTable] = .error .invalidState ∧ + addStep [.methodTable opaqueTable, .methodTable opaqueTable] = .error .invalidState := + ⟨rfl, rfl, rfl, rfl, rfl⟩ +#print axioms native_add_rejected /-- A native array element load validates the same backing storage as pointer loading. -/ theorem native_array_load : - managedStep (.tableElement signature) 0 frame - [.scalar (.i32 1), .methodTable opaqueTable] memory = - .ok (.next 1 [.methodPointer ⟨signature, some second⟩] frame memory) := rfl + instruction (.ldelem .i) [.scalar (.i32 1), .methodTable opaqueTable] memory = + .ok (memory, [.methodPointer ⟨signature, some second⟩]) := rfl #print axioms native_array_load +/-- A caller's signature that differs from the table's. -/ +private def otherSignature : ManagedSignature := + ⟨.atom Extraction.runtimeScope "System.Int64", [.atom Extraction.runtimeScope "System.Int32"]⟩ + +/-- An unhomed table argument loads an entry under the table's own signature, and a call under + any other signature selects no target from it. -/ +theorem argument_table_load : + let argumentLoad : CIL.Method := + { code := [.ldarg 0, .ldind .i, .ret], locals := [], returnsValue := true } + step argumentLoad (.ldarg 0) 0 [.methodTable (cursorAt 8)] frame [] memory = + .ok (.next 1 [.methodTable (cursorAt 8)] frame memory) ∧ + step argumentLoad (.ldind .i) 1 [.methodTable (cursorAt 8)] frame + [.methodTable (cursorAt 8)] memory = + .ok (.next 2 [.methodPointer ⟨signature, some second⟩] frame memory) ∧ + indirectTarget otherSignature [{ identity := second, method := 0 }] + ⟨signature, some second⟩ = none := ⟨rfl, rfl, rfl⟩ +#print axioms argument_table_load + /-- An opaque table cannot manufacture a native cursor without a valid storage binding. -/ theorem missing_binding : loadNativeTable opaqueTable { memory with staticBindings := [] } = .error .missingAllocation := rfl #print axioms missing_binding -/-- A scalar address remains uncallable after the new native load operation is introduced. -/ +/-- A native load of a scalar address is refused rather than producing a callable identity. -/ theorem numeric_load_rejected : - managedStep (.tableLoad signature) 0 frame [.scalar (.i64 0)] memory = - .error .invalidState := rfl + (step body (.ldind .i) 0 [] frame [.scalar (.i64 0)] memory).isOk = false := rfl #print axioms numeric_load_rejected +section Admission + +open CIL.Extraction + +/-- The extraction stack kind of a pointer into the native table of `signature`. -/ +private def nativeKind : ScalarKind := .nativeMethodTable signature + +/-- Native table addition admits only a decoded `add` without a raw operand and with an int32 or + native offset in either order; ordinary integer addition keeps its admission. -/ +theorem native_add_admission : + scalarEmissionMatches none none ⟨0, 88, .none⟩ [.word32, nativeKind] .add = true ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [nativeKind, .native] .add = true ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [.native, nativeKind] .add = true ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [nativeKind, .word32] .add = true ∧ + scalarEmissionMatches none none ⟨0, 88, .local 0⟩ [.word32, nativeKind] .add = false ∧ + scalarEmissionMatches none none ⟨0, 89, .none⟩ [.word32, nativeKind] .add = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [.word32, nativeKind] .sub = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [.word32, nativeKind] .add_ovf = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [.word64, nativeKind] .add = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [.other, nativeKind] .add = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [nativeKind, .word64] .add = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [nativeKind, .other] .add = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [nativeKind, .reference] .add = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [nativeKind, nativeKind] .add = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [.word32, .methodTable signature] + .add = false ∧ + scalarEmissionMatches none none ⟨0, 88, .none⟩ [.word32, .word32] .add = true := by + decide +kernel +#print axioms native_add_admission + +/-- A load through a native table cursor admits only the decoded `ldind.i`. -/ +theorem native_load_admission : + scalarEmissionMatches none none ⟨0, 77, .none⟩ [nativeKind] (.ldind .i) = true ∧ + scalarEmissionMatches none none ⟨0, 77, .none⟩ [nativeKind] (.ldind .i8) = false := by + decide +kernel +#print axioms native_load_admission + +end Admission + +section TableTransfer + +open CIL.Extraction + +/-- A static method without arguments; the transfers below read none of its fields. -/ +private def holder : RawMethod := { + token := 0x06000001, owner := "Scope::Fixture", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [], locals := [], initLocals := true, maxStack := 3, genericParameters := [], + code := [] } + +/-- The extraction stack kind of a managed handler table of `signature`. -/ +private def tableKind : ScalarKind := .methodTable signature + +/-- The stack kinds the raw instruction `opcode` with `operand` leaves on `stack`. -/ +private def transfer (opcode : Nat) (operand : RawOperand) (stack : List ScalarKind) : + Option (List ScalarKind) := + scalarTransfer holder ⟨0, opcode, operand⟩ stack + +/-- `ldlen` of a handler table gives a native int, and `ldelem.i` or `ldelem` of the table's + own pointer type at an int32 or native index gives a pointer of the table's signature. -/ +theorem table_reads_admitted : + transfer 142 .none [tableKind] = some [.native] ∧ + transfer 151 .none [.word32, tableKind] = some [.methodPointer signature] ∧ + transfer 151 .none [.native, tableKind] = some [.methodPointer signature] ∧ + transfer 163 (.type signature.typeExpr) [.word32, tableKind] = + some [.methodPointer signature] ∧ + transfer 163 (.type signature.typeExpr) [.native, tableKind] = + some [.methodPointer signature] := by + decide +kernel +#print axioms table_reads_admitted + +/-- An element read at an index that is not an int32 or native int is refused. -/ +theorem table_index_kinds_refused : + [ScalarKind.word64, .other, .reference, .object, .nullObject, .methodPointer signature, + tableKind].all (fun index => + transfer 151 .none [index, tableKind] == none && + transfer 163 (.type signature.typeExpr) [index, tableKind] == none) = true := by + decide +kernel +#print axioms table_index_kinds_refused + +/-- `ldelem` must name the table's own pointer type: another signature, a non-pointer type and a + missing type operand are refused. -/ +theorem table_element_type_refused : + transfer 163 (.type otherSignature.typeExpr) [.word32, tableKind] = none ∧ + transfer 163 (.type (.atom runtimeScope "System.IntPtr")) [.word32, tableKind] = none ∧ + transfer 163 .none [.word32, tableKind] = none := by + decide +kernel +#print axioms table_element_type_refused + +/-- No other array opcode reads or writes a handler table: the sized loads, `ldelem.ref`, + `ldelema` and every store are refused. -/ +theorem table_other_array_opcodes_refused : + ([144, 145, 146, 147, 148, 149, 150, 154].all (fun opcode => + transfer opcode .none [.word32, tableKind] == none) && + transfer 143 (.type signature.typeExpr) [.word32, tableKind] == none && + [155, 156, 157, 158, 159, 162].all (fun opcode => + [ScalarKind.native, .word32, .word64, .methodPointer signature].all (fun value => + transfer opcode .none [value, .word32, tableKind] == none)) && + transfer 164 (.type signature.typeExpr) [.methodPointer signature, .word32, tableKind] == + none) = true := by + decide +kernel +#print axioms table_other_array_opcodes_refused + +/-- A native table cursor is no managed table: `ldlen` and the element reads refuse it. -/ +theorem native_cursor_table_reads_refused : + transfer 142 .none [nativeKind] = none ∧ + transfer 151 .none [.word32, nativeKind] = none ∧ + transfer 163 (.type signature.typeExpr) [.word32, nativeKind] = none := by + decide +kernel +#print axioms native_cursor_table_reads_refused + +/-- `ldlen`, `ldelem.i` and `ldelem` of a handler table emit their decoded instructions, not + another instruction's decoding. -/ +theorem table_reads_emit_decoded : + scalarEmissionMatches none none ⟨0, 142, .none⟩ [tableKind] .ldlen = true ∧ + scalarEmissionMatches none none ⟨0, 151, .none⟩ [.word32, tableKind] (.ldelem .i) = true ∧ + scalarEmissionMatches none none ⟨0, 163, .type signature.typeExpr⟩ [.word32, tableKind] + (.ldelem_any signature.typeExpr) = true ∧ + scalarEmissionMatches none none ⟨0, 151, .none⟩ [.word32, tableKind] + (.ldelem_any signature.typeExpr) = false ∧ + scalarEmissionMatches none none ⟨0, 163, .type signature.typeExpr⟩ [.word32, tableKind] + (.ldelem .i) = false := by + decide +kernel +#print axioms table_reads_emit_decoded + +end TableTransfer + +section EntryCount + +/-- `NativeMethodTable.check` reading the entry count by walking `targets`. -/ +private def walkedCheck (table : NativeMethodTable) (memory : CIL.Safety.Memory) : + Checked Unit := do + let allocation ← liveAllocation memory table.origin.allocation + if allocation.kind != .immutableStatic then .error .unsupportedLayout else pure () + if table.targets.size >= 2^31 || + table.origin.offset + nativeMethodWidth * table.targets.size > allocation.layout.size then + .error .outsideAllocation else pure () + if allocation.layout.alignment % nativeMethodWidth != 0 || + table.origin.offset % nativeMethodWidth != 0 then .error .alignment else pure () + if table.cursor.allocation != table.origin.allocation || + table.cursor.offset < table.origin.offset || + table.cursor.offset > table.origin.offset + nativeMethodWidth * table.targets.size then + .error .invalidReference else pure () + +/-- `NativeMethodTable.addBytes` with `walkedCheck`. -/ +private def walkedAddBytes (table : NativeMethodTable) (memory : CIL.Safety.Memory) + (offset : BitVec 64) : Checked NativeMethodTable := do + walkedCheck table memory + let cursor := { table.cursor with offset := + (BitVec.ofNat 64 table.cursor.offset + offset).toNat } + let result := { table with cursor } + walkedCheck result memory + return result + +/-- `NativeMethodTable.load` with `walkedCheck`, reading the entry with `targets[index]?`, whose + bound check walks `targets`. -/ +private def walkedLoadView (table : NativeMethodTable) (memory : CIL.Safety.Memory) : + Checked CIL.MethodPointer := do + walkedCheck table memory + let relative := table.cursor.offset - table.origin.offset + if relative % nativeMethodWidth != 0 then .error .alignment else pure () + let index := relative / nativeMethodWidth + let some identity := table.targets[index]? | .error .outsideAllocation + let _ ← read memory table.cursor nativeMethodWidth nativeMethodWidth + return ⟨table.signature, some identity⟩ + +/-- `nativeTableView` counting the view's entries by walking them; it reads no table count. -/ +private def walkedView (table : MethodTable) (memory : CIL.Safety.Memory) : + Checked NativeMethodTable := do + let some targets := table.targets | .error .nullDereference + let some backing := table.backing | .error .invalidReference + let some (_, origin) := memory.staticBindings.find? (·.1 == backing.identity) | + .error .missingAllocation + let view : NativeMethodTable := ⟨table.signature, targets, origin, + { origin with offset := origin.offset + backing.byteOffset }, targets.size, rfl⟩ + walkedCheck view memory + return view + +/-- `addNativeTable` over the walked view and checks. -/ +private def walkedAdd (table : MethodTable) (memory : CIL.Safety.Memory) (offset : BitVec 64) : + Checked MethodTable := do + let view ← walkedView table memory + let moved ← walkedAddBytes view memory offset + let some backing := table.backing | .error .invalidReference + return { table with backing := some { backing with + byteOffset := moved.cursor.offset - moved.origin.offset } } + +/-- `loadNativeTable` over the walked view and checks. -/ +private def walkedLoad (table : MethodTable) (memory : CIL.Safety.Memory) : + Checked CIL.MethodPointer := do + let view ← walkedView table memory + walkedLoadView view memory + +/-- A view's carried count is its entries' size, so its geometry check is the walked one. -/ +private theorem check_eq_walked (view : NativeMethodTable) (memory : CIL.Safety.Memory) : + view.check memory = walkedCheck view memory := by + rcases view with ⟨signature, targets, origin, cursor, count, counted⟩ + subst counted + rfl + +/-- The view a table resolves to is the one that counts its entries by walking them. -/ +private theorem nativeTableView_eq_walked (table : MethodTable) (memory : CIL.Safety.Memory) : + nativeTableView table memory = walkedView table memory := by + rcases table with ⟨signature, _ | entries, _ | backing, count, counted⟩ + · rfl + · rfl + · rfl + · obtain rfl : entries.size = count := counted entries rfl + unfold nativeTableView walkedView + cases found : memory.staticBindings.find? (·.1 == backing.identity) with + | none => simp [found] + | some binding => simp [found, check_eq_walked] + +/-- Reading the carried entry count moves a native cursor exactly as walking the entries does, + for every table, memory and offset. -/ +theorem addNativeTable_eq_walked : addNativeTable = walkedAdd := by + funext table memory offset + unfold addNativeTable walkedAdd NativeMethodTable.addBytes walkedAddBytes + simp only [nativeTableView_eq_walked, check_eq_walked] + rfl +#print axioms addNativeTable_eq_walked + +/-- Bounding the entry index by the carried count loads exactly the entry, or the fault, that the + walked bound check gives, for every view and memory. -/ +theorem load_eq_walked : NativeMethodTable.load = walkedLoadView := by + funext view memory + rcases view with ⟨signature, targets, origin, cursor, count, counted⟩ + subst counted + unfold NativeMethodTable.load walkedLoadView + simp only [check_eq_walked] + by_cases inside : (cursor.offset - origin.offset) / nativeMethodWidth < targets.size + · simp only [inside, dite_true, Array.getElem?_eq_getElem] + · simp only [inside, dite_false, Array.getElem?_eq_none (Nat.le_of_not_lt inside)] +#print axioms load_eq_walked + +/-- Reading the carried entry count loads a native entry exactly as walking the entries does. -/ +theorem loadNativeTable_eq_walked : loadNativeTable = walkedLoad := by + funext table memory + unfold loadNativeTable walkedLoad + simp only [nativeTableView_eq_walked, load_eq_walked] +#print axioms loadNativeTable_eq_walked + +/-- An int32 offset added to a native table steps as the addition that walks the entries. -/ +theorem step_add_table_i32_walked (code : CIL.Method) (offset : CIL.W32) (table : MethodTable) + (memory : CIL.Safety.Memory) (frame : Frame) (args rest : List CIL.Safety.Value) (pc : Nat) : + step code .add pc args frame (.scalar (.i32 offset) :: .methodTable table :: rest) memory = do + let selected ← checkedAt .null nativeMethodWidth + (walkedAdd table memory (offset.signExtend 64)) + return .next (pc + 1) (.methodTable selected :: rest) frame memory := by + rw [step_add_table_i32_eq, addNativeTable_eq_walked] +#print axioms step_add_table_i32_walked + +/-- A native-int offset added to a native table steps as the addition that walks the entries. -/ +theorem step_add_table_i64_walked (code : CIL.Method) (offset : CIL.W64) (table : MethodTable) + (memory : CIL.Safety.Memory) (frame : Frame) (args rest : List CIL.Safety.Value) (pc : Nat) : + step code .add pc args frame (.scalar (.i64 offset) :: .methodTable table :: rest) memory = do + let selected ← checkedAt .null nativeMethodWidth (walkedAdd table memory offset) + return .next (pc + 1) (.methodTable selected :: rest) frame memory := by + rw [step_add_table_i64_eq, addNativeTable_eq_walked] +#print axioms step_add_table_i64_walked + +end EntryCount + end Tests.NativeMethodTables diff --git a/Tests/NativeProbe/NativeProbe.csproj b/Tests/NativeProbe/NativeProbe.csproj index 1aae446b8..0ad3c31a7 100644 --- a/Tests/NativeProbe/NativeProbe.csproj +++ b/Tests/NativeProbe/NativeProbe.csproj @@ -1,11 +1,11 @@ Exe - net10.0 + net11.0 enable enable Disable - 10.0.0 + 11.0.0 diff --git a/Tests/NestedStructs.cs b/Tests/NestedStructs.cs index 678ecf242..7e01b7a5a 100644 --- a/Tests/NestedStructs.cs +++ b/Tests/NestedStructs.cs @@ -2,6 +2,13 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include EmptyReturnVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -115,17 +122,11 @@ int RuntimeOffset(Type type, FieldInfo field) var contract = At("Examples/Proofs/NestedSnapshot.lean"); verification.Verify(assembly, "Examples", "Nested::Extract(Examples.NestedRecord)", contract, Path.Combine(root, "UniversalSnapshot")); var wrongOutput = Path.Combine(root, "WrongSnapshot"); -var failed = false; -try -{ - verification.Verify(assembly, "Examples", "Nested::WrongExtract(Examples.NestedRecord)", contract, wrongOutput); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - failed = true; -} -if (!failed || File.Exists(Path.Combine(wrongOutput, "verified.json"))) - throw new InvalidOperationException("Incorrect nested snapshot accepted"); +verification.VerifyEmptyReturn(assembly, "Nested::Extract(Examples.NestedRecord)", + contract, assembly, "Nested::WrongExtract(Examples.NestedRecord)", wrongOutput, + new("List (BitVec 8)", "[.scalar (.aggregate input)]", + ".scalar (.aggregate ((input.drop 8).take 16))", 3, + "exact ExampleProof.correctness input permitted", "(List.replicate 32 1)", "input.length = 32")); // Recursive and transitively unsupported layouts must fail before emission. var mutations = new[] { "self", "mutual", "reference", "float", "pack", "size", "auto" }; diff --git a/Tests/NullReferences.cs b/Tests/NullReferences.cs index e9a57ec39..98e6acf04 100644 --- a/Tests/NullReferences.cs +++ b/Tests/NullReferences.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -73,29 +78,38 @@ string Claim(string name, object[] input, string args, string state, string faul File.WriteAllText(proof, "import Extracted\nimport Tests.Support\nnamespace ExampleProof\n" + memory + "\ntheorem correctness :\n" + string.Join(" ∧\n", item.Claims) + " := by\n exact " + (item.Claims.Length == 1 ? "rfl" : "⟨" + string.Join(", ", item.Claims.Select(_ => "rfl")) + "⟩") + "\nend ExampleProof\n"); + if (item.Name is "IsNull" or "IsLive") + { + var (pc, operation, forgery) = item.Name == "IsNull" + ? (0, ".nullRef", ".ldc_i8 0") + : (1, ".isNullRef", ".ldnull"); + File.AppendAllText(proof, $$""" + + namespace ExampleProof + open CIL CIL.Extraction + /-- The compiled null helper keeps its operand and rejects the forged stack operation. -/ + theorem nullHelperCall : + let raw := Extracted.method0Closed.code[{{pc}}]'(by decide +kernel) + let kept := Extracted.method0.code[{{pc}}]'(by decide +kernel) + let check := fun emitted => runtimeCatalog.siteAgrees Extracted.environment + Extracted.profile raw emitted none true + And ((match kept with + | .call method _ => runtimeModel Extracted.environment .call method + | _ => none) = some (.memory {{operation}})) + (check kept && !check ({{forgery}}) && (match kept with + | .call method _ => !check (.call method { tail := true }) + | _ => false)) := by + decide +kernel + #print axioms nullHelperCall + end ExampleProof + + """); + } return new GeneratedVerification(methods.Single(m => m.Name == item.Name).FullName, proof, output); }).ToArray(); verification.VerifyGeneratedBatch(assembly, "Examples", requests); foreach (var item in cases) Console.WriteLine($"PASS {item.Name}: {item.Claims.Length} runtime comparisons"); -// An operation with the helper's stack effect cannot stand in for it: only the call binding rejects these. -foreach (var (name, helper, forgery) in new[] -{ - ("IsNull", ".memory .nullRef", ".const64 0"), - ("IsLive", ".memory .isNullRef", ".memory .asRef") -}) -{ - var extracted = File.ReadAllText(Path.Combine(root, name, "Extracted.lean")); - if (!extracted.Contains(helper)) throw new InvalidOperationException($"{name} does not emit {helper}"); - var mutated = Path.Combine(root, name + "-forged.lean"); - File.WriteAllText(mutated, extracted.Replace(helper, forgery)); - var result = Execute(["lake", "env", "lean", mutated], cwd: verification.CheckoutPackage()); - if (result.Code == 0 || !result.Output.Contains("Extraction.UnsafeCallsValid rawLayouts method0Closed") || - !result.Output.Contains("is false") || result.Output.Contains("FeatureFlowValid")) - throw new InvalidOperationException($"Forged {helper} was accepted for {name}\n{result.Output}"); - Console.WriteLine($"PASS {name}: forged {helper} rejected by the call binding"); -} - foreach (var (name, selector) in new[] { ("Read", "NullReferences::Read()"), ("IsLive", "NullReferences::IsLive(ulong&)") }) foreach (var mutation in new[] { "class", "float", "void", "byref", "pointer", "arity", "parameter", "return", "instance", "scope", "version" }) { diff --git a/Tests/Objects.cs b/Tests/Objects.cs index 1334d0e3c..74a597a59 100644 --- a/Tests/Objects.cs +++ b/Tests/Objects.cs @@ -2,8 +2,16 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BatchVerification.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include RejectedEvidence.cs +#:include GeneratedEvidence.cs +#:include ContractVariants.cs using Mono.Cecil; using Mono.Cecil.Cil; @@ -18,6 +26,8 @@ var root = At("artifacts/Objects"); Directory.CreateDirectory(root); +// One batch elaborates, replays and audits every proof; a pass is reported only after the whole batch is certified. +var queue = new GeneratedQueue(verification, assembly); foreach (var (proof, entry) in new[] { ("SameObject", "Objects::Same(Examples.Account,Examples.Account)"), @@ -37,7 +47,6 @@ ("OpaqueTag", "Objects::CopyTag(Examples.Tagged,Examples.Tagged)"), ("BaseField", "Objects::IdOf(Examples.Shape)"), ("DerivedObject", "Objects::Describe(System.UInt64,System.UInt64)"), - ("AnimalLegs", "VirtualCalls::LegsOf(Examples.Animal)"), ("AnimalLoudness", "VirtualCalls::LoudnessOf(Examples.Animal)"), ("FreshDogLegs", "VirtualCalls::DogLegs()"), ("DogTest", "ClassTests::IsDog(Examples.Animal)"), @@ -60,10 +69,60 @@ ("TileHold", "TileArrays::Hold(Examples.AggregateArrays/Pair[],System.Runtime.Intrinsics.Vector128`1[])") }) + queue.Add(new(entry, At($"Examples/Proofs/{proof}.lean"), Path.Combine(root, proof)), + () => Console.WriteLine($"PASS {proof}: {entry}")); +queue.Flush(); + +const string animalScope = "Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; +const string legsSelector = "VirtualCalls::LegsOf(Examples.Animal)"; +var wrongLegs = Path.Combine(root, "TwoLeggedDog"); +Directory.CreateDirectory(wrongLegs); +var wrongLegsAssembly = Path.Combine(wrongLegs, "Examples.dll"); +using (var variant = ModuleDefinition.ReadModule(assembly)) { - verification.Verify(assembly, "Examples", entry, At($"Examples/Proofs/{proof}.lean"), Path.Combine(root, proof)); - Console.WriteLine($"PASS {proof}: {entry}"); + var dogLegs = variant.GetType("Examples.Dog").Methods.Single(method => method.Name == "Legs"); + dogLegs.Body.Instructions.Single(site => site.OpCode == OpCodes.Ldc_I4_4).OpCode = OpCodes.Ldc_I4_2; + variant.Write(wrongLegsAssembly); } +verification.VerifyContractVariant(assembly, "Examples", legsSelector, Path.Combine(root, "AnimalLegs"), + wrongLegsAssembly, legsSelector, wrongLegs, $$""" + /-- Both runtime classes have the same abstract base in the selected assembly. -/ + private def animalClass (isDog : Bool) : RuntimeType := + ⟨.object "{{animalScope}}" (if isDog then "Examples.Dog" else "Examples.Bird"), + [.object "{{animalScope}}" "Examples.Animal"]⟩ + + /-- The virtual call returns the leg count of its live receiver and preserves caller memory. -/ + def legs : MethodContract (Bool × Reference) W64 where + args input := [.object (.address input.2)] + results value := [.scalar (.i64 value)] + pre input memory := formObject memory (.address input.2) = .ok (.object (.address input.2)) ∧ + runtimeTypeOf memory input.2 = .ok (animalClass input.1) + footprint _ := [] + post input before after value := before = after ∧ value = (if input.1 then 4 else 2) + """, """ + /-- Dispatch selects the corresponding override for every formed dog or bird. -/ + theorem correctness : legs.Holds Extracted.program 0 := by + rintro ⟨isDog, receiver⟩ memory ⟨formed, typed⟩ + cases isDog + · exact MethodContract.Holds.intro (c := legs) (input := (false, receiver)) memory 2 + (ExampleProof.bird_legs memory receiver formed typed) (Confined.refl _ _) ⟨rfl, rfl⟩ + · exact MethodContract.Holds.intro (c := legs) (input := (true, receiver)) memory 4 + (ExampleProof.dog_legs memory receiver formed typed) (Confined.refl _ _) ⟨rfl, rfl⟩ + """, """ + /-- Allocation zero contains a live dog whose virtual override the caller must select. -/ + private def dogMemory : Memory := { Memory.empty with + allocations := fun id => if id = 0 then some + ⟨.object (animalClass true), ⟨1, 1, []⟩, true, []⟩ else none + nextIdentity := 1 } + + /-- The compiled caller returns two for this dog after its override is changed. -/ + theorem correctness : legs.Refuted Extracted.program 0 := + .of_return (input := (true, ⟨0, 0⟩)) (initial := dogMemory) ⟨rfl, rfl⟩ + (rfl : invoke Extracted.program 16 0 (legs.args (true, ⟨0, 0⟩)) dogMemory = + .ok (dogMemory, [.scalar (.i64 2)])) + (by rintro ⟨-, value, encoded, -, rfl⟩; revert encoded; decide) + """, example: At("Examples/Proofs/AnimalLegs.lean")); +Console.WriteLine("PASS virtual caller contract and certified two-legged Dog override"); // Each named field is checked against the operation it names, so a view with another offset, width or // element fails its own theorem instead of naming storage the code never touches. @@ -78,10 +137,9 @@ "def Value : Safety.WordField 4 := ⟨type, 0⟩", "Value.accessedBy"), ("element", "SampleValues", "def Values : Safety.ArrayField .signed32 := ⟨type, 0⟩", "def Values : Safety.ArrayField .unsigned32 := ⟨type, 0⟩", "Values.accessedBy"), - // A struct element's width is its layout's, so a field operation with another width fails its layout use. + // A struct element's width is its layout's, so a view with another width fails its own theorem. ("structWidth", "TilePairs", "PublicKeyToken=null::Examples.AggregateArrays/Pair\" 16)", - "PublicKeyToken=null::Examples.AggregateArrays/Pair\" 8)", - "Extraction.MethodLayoutsValid rawLayouts computedLayouts method0 method0LayoutUses") + "PublicKeyToken=null::Examples.AggregateArrays/Pair\" 8)", "Pairs.accessedBy") }) { var extracted = File.ReadAllText(Path.Combine(root, proof, "Extracted.lean")); @@ -94,18 +152,17 @@ // Lean re-resolves every dispatch table entry from the catalog and requires every concrete class // deriving from the slot's class, so a table that swaps bodies or omits a class fails its check. var dispatched = File.ReadAllText(Path.Combine(root, "AnimalLegs", "Extracted.lean")); -const string animalScope = "Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"; var birdEntry = $"(.object \"{animalScope}\" \"Examples.Bird\", "; var dogEntry = $"(.object \"{animalScope}\" \"Examples.Dog\", "; -var table = Regex.Match(dispatched, @"\.callVirtual \[\(.*?\)\] 1").Value; -var entries = Regex.Matches(table, @"(\(\.object [^)]*?, )(\d+)\)").Select(entry => entry.Groups).ToArray(); +var table = DispatchPatterns.Table.Match(dispatched).Value; +var entries = DispatchPatterns.Entry.Matches(table).Select(entry => entry.Groups).ToArray(); if (entries.Length != 2 || !table.Contains(birdEntry + entries[0][2].Value, StringComparison.Ordinal) || !table.Contains(dogEntry + entries[1][2].Value, StringComparison.Ordinal)) throw new InvalidOperationException("The extraction does not list Bird and Dog in its dispatch table: " + table); foreach (var (name, forged) in new[] { - ("swapped", $".callVirtual [{birdEntry}{entries[1][2].Value}), {dogEntry}{entries[0][2].Value})] 1"), - ("omitted", $".callVirtual [{dogEntry}{entries[1][2].Value})] 1") + ("swapped", $".virtual [{birdEntry}{entries[1][2].Value}), {dogEntry}{entries[0][2].Value})]"), + ("omitted", $".virtual [{dogEntry}{entries[1][2].Value})]") }) { var path = Path.Combine(root, $"ForgedTable-{name}.lean"); @@ -114,35 +171,22 @@ "Extraction.ConstrainedCallsValid method0DispatchTypes"); } // Lean binds a type test to its raw opcode and class, so an emitted test of another class or with -// the other outcome for a failed cast fails the scalar flow check. +// the other outcome for a failed cast fails the scalar flow check. The method is defined from its decoder, so +// the forgery edits the decoder's input and leaves the closed definition the check reads. var cast = File.ReadAllText(Path.Combine(root, "DogCast", "Extracted.lean")); -var castTest = $".objectTest ({dogEntry.TrimStart('(').TrimEnd(',', ' ')}) true"; -if (!cast.Contains(castTest, StringComparison.Ordinal)) - throw new InvalidOperationException("The extraction does not test the cast against Dog: " + castTest); +var castTest = $"opcode := 116, operand := .type ({dogEntry.TrimStart('(').TrimEnd(',', ' ')})"; foreach (var (name, forged) in new[] { ("class", castTest.Replace("Examples.Dog", "Examples.Bird", StringComparison.Ordinal)), - ("outcome", castTest[..^" true".Length] + " false") + ("outcome", castTest.Replace("opcode := 116", "opcode := 117", StringComparison.Ordinal)) }) { var path = Path.Combine(root, $"ForgedCast-{name}.lean"); - File.WriteAllText(path, cast.Replace(castTest, forged, StringComparison.Ordinal)); + File.WriteAllText(path, ForgeDecodedOriginal(cast, 0, castTest, forged)); forgedViews.Add(path, $"PASS forged type test rejected: {name}", "Extraction.ScalarFlowValid"); } forgedViews.Check(); -// The entry's call proof simplifies with the constructor's closed body; without it the proof -// falls back to a whole-method kernel decision. -var constructed = File.ReadAllLines(Path.Combine(root, "FreshObject", "Extracted.lean")); -var construction = constructed.Single(line => line.StartsWith(" .newObject ", StringComparison.Ordinal)); -var constructor = construction.Split(' ', StringSplitOptions.RemoveEmptyEntries)[1]; -var entryCalls = constructed.Single(line => - line.StartsWith("theorem method0_calls_validated ", StringComparison.Ordinal)); -if (!entryCalls.Contains("by cil_managed_calls [", StringComparison.Ordinal) || - !entryCalls.Contains($"method{constructor}Closed", StringComparison.Ordinal)) - throw new InvalidOperationException("The constructing method's call proof omits the constructor:\n" + entryCalls); -Console.WriteLine("PASS construction call proof names the constructor"); - foreach (var mutation in new[] { "abstract", "base", "baseRejected", "explicit", "finalizer", "interface", "byref", "address", "reference", @@ -437,3 +481,12 @@ "construction, base class field, class reference array, value-array field, virtual dispatch and type " + "test proofs, and named rejection of unmodelled class shapes, field and element accesses, calls, " + "dispatch tables and type tests"); + +static partial class DispatchPatterns +{ + [GeneratedRegex(@"\.virtual \[\(.*?\)\]")] + internal static partial Regex Table { get; } + + [GeneratedRegex(@"(\(\.object [^)]*?, )(\d+)\)")] + internal static partial Regex Entry { get; } +} diff --git a/Tests/Objects.lean b/Tests/Objects.lean index e08586d92..58e8275b6 100644 --- a/Tests/Objects.lean +++ b/Tests/Objects.lean @@ -28,6 +28,21 @@ private def frame : Frame := { activation := 0, locals := [], owned := [] } private def body : CIL.Method := { returnsValue := true, locals := [], code := [] } private def live : ManagedReference := .address ⟨0, 0⟩ +/-- Receiver calls reject partial operands before link lookup and check null after lookup. -/ +theorem receiver_call_fault_order : + let operation := Instruction.callvirt (.arity 1) + let runCall := fun (link : List (Nat × Link)) stack => + step { body with links := link } operation 0 [] frame stack memory + let defined := CIL.Safety.Value.scalar (.i32 0) + runCall [] [.scalar (.v128Low64 0), .object .null] = + .error (.unsupported "Instruction observes unspecified vector bits") ∧ + runCall [] [defined, .object .null] = .error unlinkedCall ∧ + runCall [(0, .direct 1)] [defined, .object .null] = .error (.thrown .nullReference) ∧ + runCall [(0, .virtual [(account.identity, 2)])] [defined, .object live] = + .ok (.call 2 [.object live, defined] [] memory) := by + exact ⟨rfl, rfl, rfl, rfl⟩ +#print axioms receiver_call_fault_order + /-- A live object allocation at offset 0 forms as itself. -/ theorem live_object_forms : formObject memory live = .ok (.object live) := rfl #print axioms live_object_forms @@ -50,28 +65,34 @@ theorem expired_object_faults : /-- `ceq` of an object with itself is 1 and with null is 0. -/ theorem identity_comparisons : - CIL.Safety.step body .eq 0 [] frame [.object live, .object live] memory = + CIL.Safety.step body .ceq 0 [] frame [.object live, .object live] memory = .ok (.next 1 [.scalar (.i32 1)] frame memory) ∧ - CIL.Safety.step body .eq 0 [] frame [.object .null, .object live] memory = + CIL.Safety.step body .ceq 0 [] frame [.object .null, .object live] memory = .ok (.next 1 [.scalar (.i32 0)] frame memory) := ⟨rfl, rfl⟩ #print axioms identity_comparisons /-- `cgt.un` on two objects is defined only against null. -/ theorem ordering_rejected : - CIL.Safety.step body .gtu 0 [] frame [.object live, .object live] memory = + CIL.Safety.step body .cgt_un 0 [] frame [.object live, .object live] memory = .error .invalidState := rfl #print axioms ordering_rejected /-- Object and byref nulls are distinct: neither compares with the other. -/ theorem byref_null_not_object : - CIL.Safety.step body .eq 0 [] frame [.object .null, .reference .null] memory = + CIL.Safety.step body .ceq 0 [] frame [.object .null, .reference .null] memory = .error .invalidState ∧ storeLocal memory (.object none) (.reference .null) = .error .invalidState := ⟨rfl, rfl⟩ #print axioms byref_null_not_object +/-- `ldnull` pushes the object null above the stack and leaves memory unchanged. -/ +theorem ldnull_pushes_object_null : + CIL.Safety.step body .ldnull 0 [] frame [.object live] memory = + .ok (.next 1 [.object .null, .object live] frame memory) := rfl +#print axioms ldnull_pushes_object_null + /-- An object home never exposes an address. -/ theorem object_local_has_no_address : - localAddress memory (.object (some live)) = + localAddress memory 0 (.object (some live)) = .error (.unsupported "Address of object reference local") := rfl #print axioms object_local_has_no_address @@ -105,7 +126,7 @@ theorem base_fields_reached : /-- A field access through null throws, as `ldfld` and `stfld` do at run time. -/ theorem null_field_throws : objectFieldAddress memory .null account.identity 0 = - .error (.thrown "System.NullReferenceException") := rfl + .error (.thrown .nullReference) := rfl #print axioms null_field_throws /-- Byte storage that is not an object has no fields, whatever offset is asked for. -/ @@ -131,9 +152,10 @@ theorem allocation_over_budget : which then also waits on the caller's stack. -/ private def creator : CIL.Method := { returnsValue := true, locals := [], - code := [.newObject 1 0 account.identity [] 8, .ret none] } + code := [.newobj { CIL.Extraction.MethodRef.arity 0 with owner := account.identity }, .ret], + links := [(0, .object 1 [] 8)] } private def constructor : CIL.Method := - { returnsValue := false, locals := [], code := [.ret none] } + { returnsValue := false, locals := [], code := [.ret] } /-- The constructed instance is returned to the caller and outlives the constructor's frame. -/ theorem construction_returns_instance : @@ -160,63 +182,75 @@ private def nodes : CIL.Safety.Memory := private def first : ManagedReference := .address ⟨0, 0⟩ private def second : ManagedReference := .address ⟨1, 0⟩ +/-- `Node.next`, the `Node` field 1, in the reference slot at model offset 8. -/ +private def nextNode : Extraction.FieldRef := + { owner := node.identity, index := 1, type := node.identity, name := name!"next" } + /-- A class-typed field holds the stored reference itself; no byte of the object changes. -/ theorem reference_field_reads_back : - ((objectFieldInstruction (.setObjectReferenceField node.identity 8) - [.object second, .object first] nodes).bind fun stored => - objectFieldInstruction (.objectReferenceField node.identity 8) [.object first] - stored.1).map Prod.snd = .ok [.object second] ∧ - (objectFieldInstruction (.setObjectReferenceField node.identity 8) - [.object second, .object first] nodes).map (fun stored => stored.1.cells 0 8) = - .ok ⟨0, true⟩ := ⟨rfl, rfl⟩ + ((fieldStore body nextNode [.object second, .object first] nodes).bind fun stored => + fieldLoad body nextNode [.object first] stored.1).map Prod.snd = .ok [.object second] ∧ + (fieldStore body nextNode [.object second, .object first] nodes).map + (fun stored => stored.1.cells 0 8) = .ok ⟨0, true⟩ := ⟨rfl, rfl⟩ #print axioms reference_field_reads_back /-- A fresh field holds null. -/ theorem reference_field_starts_null : - (objectFieldInstruction (.objectReferenceField node.identity 8) [.object first] nodes).map - Prod.snd = .ok [.object .null] := rfl + (fieldLoad body nextNode [.object first] nodes).map Prod.snd = .ok [.object .null] := rfl #print axioms reference_field_starts_null /-- A null receiver throws on both the load and the store, before the value is checked. -/ theorem reference_field_null_receiver_throws : - objectFieldInstruction (.objectReferenceField node.identity 8) [.object .null] nodes = - .error (.thrown "System.NullReferenceException") ∧ - objectFieldInstruction (.setObjectReferenceField node.identity 8) - [.object (.address ⟨2, 0⟩), .object .null] nodes = - .error (.thrown "System.NullReferenceException") := ⟨rfl, rfl⟩ + fieldLoad body nextNode [.object .null] nodes = .error (.thrown .nullReference) ∧ + fieldStore body nextNode [.object (.address ⟨2, 0⟩), .object .null] nodes = + .error (.thrown .nullReference) := ⟨rfl, rfl⟩ #print axioms reference_field_null_receiver_throws /-- Only an object or null may be stored: a byte buffer is not one. -/ theorem reference_field_rejects_buffer : - objectFieldInstruction (.setObjectReferenceField node.identity 8) - [.object (.address ⟨2, 0⟩), .object first] nodes = .error .invalidState := rfl + fieldStore body nextNode [.object (.address ⟨2, 0⟩), .object first] nodes = + .error .invalidState := rfl #print axioms reference_field_rejects_buffer /-- A store needs write authority over the field's slot, and the slot must lie in the object. -/ theorem reference_field_checks_slot : - objectFieldInstruction (.setObjectReferenceField node.identity 8) - [.object first, .object second] nodes = + fieldStore body nextNode [.object first, .object second] nodes = .error (.memory .unwritable (.address ⟨1, 8⟩) 8) ∧ - objectFieldInstruction (.objectReferenceField node.identity 16) [.object first] nodes = + fieldLoad body { nextNode with index := 2 } [.object first] nodes = .error (.memory .outsideAllocation (.address ⟨0, 16⟩) 8) := ⟨rfl, rfl⟩ #print axioms reference_field_checks_slot /-! ## Failure records of object steps -/ +/-- `Account.limit`, the `int64` field 0, in the slot at model offset 0. -/ +private def limitField : Extraction.FieldRef := + { owner := account.identity, index := 0, type := .atom Extraction.runtimeScope "System.Int64", + name := name!"limit" } + +/-- `Account.next`, the `Account` field 1, in the reference slot at model offset 8. -/ +private def nextAccount : Extraction.FieldRef := + { owner := account.identity, index := 1, type := account.identity, name := name!"next" } + +/-- The receiver-only slot that the fixture links to method 4. -/ +private def slot : Extraction.MethodRef := .arity 0 + /-- `callvirt` of method 4 on argument 0. -/ @[cil_code] def depositing : CIL.Method := - { returnsValue := false, locals := [], code := [.arg 0, .callInstance 4 1, .ret] } + { returnsValue := false, locals := [], code := [.ldarg 0, .callvirt slot, .ret], + links := [(1, .direct 4)] } /-- `ldfld` of a scalar field of argument 0. -/ @[cil_code] def fieldReading : CIL.Method := { returnsValue := true, locals := [], - code := [.arg 0, .memory (.objectField account.identity 0 8 false), .ret] } + code := [.ldarg 0, .ldfld limitField, .ret] } /-- `ldfld` of a class-typed field of argument 0. -/ @[cil_code] def slotReading : CIL.Method := { returnsValue := true, locals := [], - code := [.arg 0, .memory (.objectReferenceField account.identity 8), .ret] } + code := [.ldarg 0, .ldfld nextAccount, .ret] } /-- `newobj` of an 8-byte instance whose constructor is method 4. -/ @[cil_code] def creating : CIL.Method := - { returnsValue := true, locals := [], code := [.newObject 4 0 account.identity [] 8, .ret] } + { returnsValue := true, locals := [], + code := [.newobj { CIL.Extraction.MethodRef.arity 0 with owner := account.identity }, .ret], + links := [(0, .object 4 [] 8)] } /-- A constructor or instance method that does nothing. -/ @[cil_code] def doingNothing : CIL.Method := { returnsValue := false, locals := [], code := [.ret] } @@ -225,7 +259,7 @@ theorem reference_field_checks_slot : [depositing, fieldReading, slotReading, creating, doingNothing] -- A walk stopped at a receiver-checked call lacks the callee's contract, as at a direct call. -/-- "instruction":"CIL.Op.callInstance 4 1","method":0,"missing":"calleeContract" -/ +/-- CIL.Instruction.callvirt Tests.Objects.slot","method":0,"missing":"calleeContract" -/ #guard_msgs (substring := true) in set_option cil.diagnostics true in example (memory : CIL.Safety.Memory) (object : Reference) (frame : Frame) @@ -270,28 +304,76 @@ example (memory : CIL.Safety.Memory) (frame : Frame) set_option cil.diagnostics true in example (memory : CIL.Safety.Memory) (object : Reference) : invoke observed 100 0 [.object (.address object)] memory = - .error ⟨0, 1, .thrown "System.NullReferenceException"⟩ := by + .error ⟨0, 1, .thrown .nullReference⟩ := by cil_checked_steps -/-- A signed `ldfld` of a 16-bit field holding `0xFFFF` pushes -1, sign-extended to 32 bits, and - an unsigned one pushes 65535: `signedFacts` and `facts` must not be interchangeable. -/ +/-- `Account.offset`, an `int16` field 0. -/ +private def signedShort : Extraction.FieldRef := + { owner := account.identity, index := 0, type := .atom Extraction.runtimeScope "System.Int16", + name := name!"offset" } + +/-- The same slot read as a `uint16`. -/ +private def unsignedShort : Extraction.FieldRef := + { owner := account.identity, index := 0, type := .atom Extraction.runtimeScope "System.UInt16", + name := name!"offset" } + +/-- The 2-byte word in the slot at model offset 0 of an `Account`. -/ +private def shortWord : WordField 2 := ⟨account.identity, 0⟩ + +/-- A `ldfld` of an `int16` field holding `0xFFFF` pushes -1, sign-extended to 32 bits, and one + of a `uint16` field pushes 65535: `signedFacts` and `facts` must not be interchangeable. -/ theorem narrow_field_loads_extend_by_sign {memory : CIL.Safety.Memory} {object : Reference} - {field : WordField 2} (readable : field.Readable memory object) - (held : field.of memory object = 0xFFFF) : - CIL.Safety.step body (.memory (.objectField field.owner field.offset 2 true)) 0 [] frame - [.object (.address object)] memory = + (readable : shortWord.Readable memory object) (held : shortWord.of memory object = 0xFFFF) : + CIL.Safety.step body (.ldfld signedShort) 0 [] frame [.object (.address object)] memory = .ok (.next 1 [.scalar (.i32 0xFFFFFFFF)] frame memory) ∧ - CIL.Safety.step body (.memory (.objectField field.owner field.offset 2 false)) 0 [] frame - [.object (.address object)] memory = + CIL.Safety.step body (.ldfld unsignedShort) 0 [] frame [.object (.address object)] memory = .ok (.next 1 [.scalar (.i32 0xFFFF)] frame memory) := by obtain ⟨_, located, signedLoad⟩ := readable.signedFacts obtain ⟨_, _, unsignedLoad⟩ := readable.facts rw [held] at signedLoad unsignedLoad - constructor <;> rw [step_objectField_2_eq] <;> + constructor <;> simp only [cil_step, Nat.reduceAdd] <;> + simp only [shortWord] at located signedLoad unsignedLoad <;> simp only [located, signedLoad, unsignedLoad, Bind.bind, Except.bind, Pure.pure, Except.pure] <;> rfl #print axioms narrow_field_loads_extend_by_sign +/-! ## Class field steps by shape -/ + +/-- The layouts of the class field steps; a class slot reads none. -/ +@[cil_code] def accounts : LayoutEnvironment := {} + +/-- `Account.balance`, the `int64` field 1, in the slot at model offset 8. -/ +def balanceField : Extraction.FieldRef := + { owner := account.identity, index := 1, type := .atom Extraction.runtimeScope "System.Int64", + name := name!"balance" } + +cil_field_shape accounts balanceField + +/-- `ldfld` of the balance of argument 0. -/ +@[cil_code] def balanceReading : CIL.Method := + { returnsValue := true, locals := [], environment := accounts, + code := [.ldarg 0, .ldfld balanceField, .ret] } + +/-- Stepping a class field load resolves its slot from the field's shape fact and shows the + owner as the operand writes it, so a fact stated over `account.identity` matches. -/ +theorem balance_load_shows_owner (memory : CIL.Safety.Memory) (receiver : ManagedReference) + (frame : Frame) : + step balanceReading (.ldfld balanceField) 1 [] frame [.object receiver] memory = do + let address ← objectFieldAddress memory receiver account.identity 8 + let value ← loadValue memory (.address address) 8 false + return .next 2 [.scalar value] frame memory := by + simp only [cil_step, Nat.reduceAdd] +#print axioms balance_load_shows_owner + +/-- A class field load steps to the value its located slot holds. -/ +theorem balance_load_steps (memory : CIL.Safety.Memory) (object field : Reference) (frame : Frame) + (located : objectFieldAddress memory (.address object) account.identity 8 = .ok field) + (loaded : loadValue memory (.address field) 8 false = .ok (.i64 7)) : + step balanceReading (.ldfld balanceField) 1 [] frame [.object (.address object)] memory = + .ok (.next 2 [.scalar (.i64 7)] frame memory) := by + cil_checked_step [located, loaded] +#print axioms balance_load_steps + /-- The return check forms an object the method created, or any other object its frame does not own, in memory after the frame is left. -/ theorem unowned_object_returns (memory : CIL.Safety.Memory) (object : Reference) (frame : Frame) @@ -352,7 +434,8 @@ theorem unrelated_classes_need_catalog : /-- A one-method program whose only `newobj` creates an instance of `type`. -/ private def createsOne (type : RuntimeType) : CIL.Program := [{ returnsValue := true, locals := [], - code := [.newObject 1 0 type.identity type.bases 16, .ret] }] + code := [.newobj { CIL.Extraction.MethodRef.arity 0 with owner := type.identity }, .ret], + links := [(0, .object 1 type.bases 16)] }] /-- The site check accepts a program creating a cataloged class with its own chain, and rejects one whose `newobj` records the lineage no CLR object can have, so execution of it is not @@ -385,9 +468,8 @@ theorem self_linked_no_segment {field : ReferenceField} {linked : CIL.Safety.Mem /-- A reference load followed by a word load through the object it produced. -/ @[cil_code] def linkedReading : CIL.Method := - { returnsValue := true, locals := [], code := [.arg 0, - .memory (.objectReferenceField account.identity 8), - .memory (.objectField account.identity 0 8 false), .ret] } + { returnsValue := true, locals := [], code := [.ldarg 0, .ldfld nextAccount, .ldfld limitField, + .ret] } /-- The linked-object reader alone. -/ @[cil_code] def linkedProgram : CIL.Program := [linkedReading] @@ -434,10 +516,14 @@ theorem named_reference_load_evidence (memory otherMemory : CIL.Safety.Memory) /-- A receiver check selects a Boolean-returning instance call or an explicit exception. -/ @[cil_code] private def checkedReceiverProgram : CIL.Program := [ - { returnsValue := true, locals := [], code := [ - .arg 0, .brnonzero 3, .throw "MissingReceiver", .arg 0, .const32 1, - .callInstance 1 2, .ret (some .unsigned8)] }, - { returnsValue := true, locals := [], code := [.arg 1, .ret (some .unsigned8)] }] + { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [], code := [ + .ldarg 0, .brtrue 3, .throw .nullReference, .ldarg 0, .ldc_i4 1, + .callvirt (.arity 1), .ret], links := [(5, .direct 1)] }, + { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + returnsValue := true, locals := [], code := [.ldarg 1, .ret] }] /-- A symbolic live receiver decides the branch and passes the call's closed argument guard. -/ theorem checked_receiver_call (memory : CIL.Safety.Memory) (receiver : Reference) @@ -451,22 +537,33 @@ theorem checked_receiver_call (memory : CIL.Safety.Memory) (receiver : Reference /-- A null receiver takes the explicit exception path without entering the callee. -/ theorem checked_receiver_throws (memory : CIL.Safety.Memory) (frame : Frame) : run checkedReceiverProgram 12 0 0 [.object .null] frame [] memory = - .error ⟨0, 2, .thrown "MissingReceiver"⟩ := by + .error ⟨0, 2, .thrown .nullReference⟩ := by cil_checked_steps #print axioms checked_receiver_throws -/-- A narrow Boolean parameter is written through its receiver's one-byte field. -/ -@[cil_code] private def narrowStoreProgram (kind : NarrowKind) : CIL.Program := [ - { returnsValue := false, locals := [], code := [ - .arg 0, .arg 1 (some kind), .memory (.setObjectField account.identity 0 1), .ret] }] +/-- `Account.open`, the Boolean field 0, in the slot at model offset 0. -/ +private def openField : Extraction.FieldRef := + { owner := account.identity, index := 0, type := .atom Extraction.runtimeScope "System.Boolean", + name := name!"open" } + +/-- A Boolean parameter, read narrowed, is written through its receiver's one-byte field. -/ +@[cil_code] private def narrowStore : CIL.Method := + { returnsValue := false, locals := [], + declared := { parameters := [.atom CIL.Extraction.runtimeScope "System.Object", + .atom CIL.Extraction.runtimeScope "System.Boolean"] }, + code := [.ldarg 0, .ldarg 1, .stfld openField, .ret] } + +/-- The narrowing store alone. -/ +@[cil_code] private def narrowStoreProgram : CIL.Program := [narrowStore] /-- Narrowing a literal Boolean preserves the payload of a supplied checked field write. -/ -theorem narrowed_boolean_store (kind : NarrowKind) (memory stored : CIL.Safety.Memory) +theorem narrowed_boolean_store (memory stored : CIL.Safety.Memory) (receiver : Reference) (frame : Frame) (formed : formObject memory (.address receiver) = .ok (.object (.address receiver))) - (written : memoryInstruction (.setObjectField account.identity 0 1) - [.scalar (.i32 1), .object (.address receiver)] memory = .ok (stored, [])) : - run (narrowStoreProgram kind) 5 0 0 [.object (.address receiver), .scalar (.i32 1)] + (written : step narrowStore (.stfld openField) 2 + [.object (.address receiver), .scalar (.i32 1)] frame + [.scalar (.i32 1), .object (.address receiver)] memory = .ok (.next 3 [] frame stored)) : + run narrowStoreProgram 5 0 0 [.object (.address receiver), .scalar (.i32 1)] frame [] memory = .ok (leaveFrame frame stored, []) := by cil_checked_steps [formed, written] #print axioms narrowed_boolean_store diff --git a/Tests/OleanAudit.cs b/Tests/OleanAudit.cs index 27da40462..592d514c9 100644 --- a/Tests/OleanAudit.cs +++ b/Tests/OleanAudit.cs @@ -1,18 +1,25 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/ConsumerVerification.cs #:include ../scripts/LeanDiagnostics.cs #:include ../scripts/LibraryAudit.cs using System.Text.Json; -Run(["dotnet", "run", "--file", "Tests/ConsumerInspection.cs"]); +var listsOnly = args is ["--lists-only"]; +if (!listsOnly) Run(["dotnet", "run", "--file", "Tests/ConsumerInspection.cs"]); using var verification = new Program(); var root = At("artifacts/OleanAudit"); if (Directory.Exists(root)) Directory.Delete(root, true); Directory.CreateDirectory(root); +if (!listsOnly) +{ // Each proof below certifies the addition contract for the subtraction method, so any acceptance is unsound. var assembly = At("Examples/Assembly/bin/Examples.dll"); const string wrongAdd = "System.UInt64 Examples.Arithmetic::WrongAdd(System.UInt64,System.UInt64)"; @@ -98,22 +105,28 @@ open Lean Elab Command in unless out.exitCode == 0 do throwError m!"{out.stdout}{out.stderr}" IO.Process.exit (α := Unit) 0 """, "Compiled artifacts changed during verification"); +} // Checker-level cases: modules compiled directly, audited without elaborating any source. var fixtures = Path.Combine(root, "fixtures"); Directory.CreateDirectory(fixtures); +verification.Library(Inputs(At("CIL.lean"))); +var fixtureSearch = fixtures + Path.PathSeparator + Path.Combine(verification.library!, ".lake/build/lib/lean"); var environment = new Dictionary { ["LEAN_SYSROOT"] = verification.Toolchain }; void Compile(string module, string source) { - var path = Path.Combine(fixtures, module + ".lean"); + var path = Path.Combine(fixtures, module.Replace('.', Path.DirectorySeparatorChar) + ".lean"); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); File.WriteAllText(path, source); - Run([verification.LeanExecutable, "--root=" + fixtures, "-o", Path.Combine(fixtures, module + ".olean"), path], fixtures, fixtures, environment: environment); + Run([verification.LeanExecutable, "--root=" + fixtures, "-o", Path.ChangeExtension(path, ".olean"), path], + fixtures, fixtureSearch, environment: environment); } string Elaborated(string source) { var path = Path.Combine(fixtures, "Elaborated.lean"); File.WriteAllText(path, source); - return Run([verification.LeanExecutable, "--root=" + fixtures, path], fixtures, fixtures, environment: environment); + return Run([verification.LeanExecutable, "--root=" + fixtures, path], fixtures, fixtureSearch, + environment: environment); } AuditedOleans Audit(string module, AuditRequest[] theorems, AuditConstantRequest[]? constants = null) => verification.AuditOleans(fixtures, [module], theorems, constants); @@ -127,6 +140,198 @@ void Rejected(AuditedTheorem audited, string marker) object App(params object[] parts) => new { app = parts }; object Nat(int n) => new { nat = n }; +object ListPattern(object type, object level, params object[] items) => new { list = new { type, level, items } }; +object NestedList(object type, int level, params object[] items) => + items.Reverse().Aggregate(App(Const("List.nil", level), type), + (tail, item) => App(Const("List.cons", level), type, item, tail)); +var listNat = Const("Nat"); +Compile("Lists", """ + def Lists.empty : List Nat := [] + def Lists.single : List Nat := [1] + def Lists.many : List Nat := [1, 2, 3] + def Lists.alias : List Nat := Lists.many + def Lists.append : List Nat := [1] ++ [2, 3] + def Lists.types : List Type := [Nat, Bool] + """); +var listCases = new (string Name, object Pattern, bool Accept)[] +{ + ("empty", ListPattern(listNat, 0), true), + ("single", ListPattern(listNat, 0, Nat(1)), true), + ("many", ListPattern(listNat, 0, Nat(1), Nat(2), Nat(3)), true), + ("types", ListPattern(new { sort = 1 }, 1, Const("Nat"), Const("Bool")), true), + ("many", ListPattern(Const("Bool"), 0, Nat(1), Nat(2), Nat(3)), false), + ("many", ListPattern(listNat, 1, Nat(1), Nat(2), Nat(3)), false), + ("many", ListPattern(listNat, 0, Nat(2), Nat(1), Nat(3)), false), + ("many", ListPattern(listNat, 0, Nat(1), Nat(2)), false), + ("many", ListPattern(listNat, 0, Nat(1), Nat(2), Nat(3), Nat(4)), false), + ("many", ListPattern(listNat, 0, Nat(1), Nat(2), Nat(4)), false), + ("empty", ListPattern(Const("Bool"), 0), false), + ("empty", ListPattern(listNat, 1), false), + ("alias", ListPattern(listNat, 0, Nat(1), Nat(2), Nat(3)), false), + ("append", ListPattern(listNat, 0, Nat(1), Nat(2), Nat(3)), false) +}; +var listResults = Audit("Lists", [], [.. listCases.Select(test => + new AuditConstantRequest("Lists." + test.Name, test.Pattern))]).constants; +for (var index = 0; index < listCases.Length; index++) + if ((listResults[index].error is null) != listCases[index].Accept) + throw new InvalidOperationException("List structural audit: " + JsonSerializer.Serialize(listResults[index])); + +// These expressions exercise constructor shape independently of Lean's type checker. +string Expected(object pattern) => "(Json.parse " + LeanString(JsonSerializer.Serialize(pattern)) + + ").toOption.get!"; +var flat = Expected(ListPattern(listNat, 0, new { lit = 1 }, "_")); +var nestedList = Expected(NestedList(listNat, 0, new { lit = 1 }, "_")); +var flatAnyType = Expected(ListPattern("_", 0, new { lit = 1 }, "_")); +var nestedAnyType = Expected(NestedList("_", 0, new { lit = 1 }, "_")); +var malformedPatterns = new object[] +{ + new { list = new { level = 0, items = Array.Empty() } }, + new { list = new { type = listNat, items = Array.Empty() } }, + new { list = new { type = listNat, level = 0 } }, + new { list = new { type = listNat, level = "bad", items = Array.Empty() } }, + new { list = new { type = listNat, level = 0, items = "bad" } }, + new { list = new { type = 42, level = 0, items = Array.Empty() } } +}; +var malformedChecks = string.Join("\n", malformedPatterns.Select(pattern => + " unless (LeanCILAudit.fits " + Expected(pattern) + + " nil).toOption.isNone do throw <| IO.userError \"fields\"")); +Elaborated($$""" + import CIL.OleanAudit + open Lean + #eval show IO Unit from do + let ty := mkConst ``Nat + let nil := mkApp (mkConst ``List.nil [.zero]) ty + let cons := fun ty head tail => mkApp3 (mkConst ``List.cons [.zero]) ty head tail + let one := mkRawNatLit 1 + let two := mkRawNatLit 2 + let good := cons ty one (cons ty two nil) + let bad := #[nil, cons ty one nil, cons ty two (cons ty one nil), + cons (mkConst ``Bool) one (cons ty two nil), + cons ty one (cons (mkConst ``Bool) two nil), + cons ty one (cons ty two (mkApp (mkConst ``List.nil [.zero]) (mkConst ``Bool))), + mkApp3 (mkConst ``List.cons [.succ .zero]) ty one (cons ty two nil), + mkApp3 (mkConst ``List.cons []) ty one (cons ty two nil), + mkApp3 (mkConst ``List.cons [.zero, .zero]) ty one (cons ty two nil), + cons ty one (cons ty two (mkApp (mkConst ``List.nil [.succ .zero]) ty)), + cons ty one (cons ty two (mkApp (mkConst ``List.nil []) ty)), + cons ty one (cons ty two (mkApp (mkConst ``List.nil [.zero, .zero]) ty)), + mkApp good one, mkApp2 (mkConst ``List.cons [.zero]) ty one, + cons ty one (cons ty two (mkConst ``List.nil [.zero])), + cons ty one (cons ty two (mkApp nil one)), + mkMData {} good] + unless (LeanCILAudit.fits {{flat}} good).toOption == some true do throw <| IO.userError "good" + for expr in bad do + unless (LeanCILAudit.fits {{flat}} expr).toOption == some false do throw <| IO.userError "shape" + for expr in bad.push good do + unless (LeanCILAudit.fits {{flat}} expr).toOption == + (LeanCILAudit.fits {{nestedList}} expr).toOption do + throw <| IO.userError "nested equivalence" + unless (LeanCILAudit.fits {{flatAnyType}} expr).toOption == + (LeanCILAudit.fits {{nestedAnyType}} expr).toOption do + throw <| IO.userError "wildcard type equivalence" + {{malformedChecks}} + """); +Console.WriteLine("PASS flat lists check every constructor type, universe, item, arity and exact tail"); + +int previousRequestSize = 0; +Compile("StrictBinding.Image", """ + namespace StrictBinding + structure PinnedImage where + identity : String + image : Nat + end StrictBinding + """); +int? requestDepth = null; +int Depth(JsonElement element) => element.ValueKind switch +{ + JsonValueKind.Array => 1 + element.EnumerateArray().Select(Depth).DefaultIfEmpty().Max(), + JsonValueKind.Object => 1 + element.EnumerateObject().Select(property => Depth(property.Value)) + .DefaultIfEmpty().Max(), + _ => 0 +}; +foreach (var count in new[] { 16, 128, 1024 }) +{ + BindingImagePin[] pins = [.. Enumerable.Range(0, count).Select(index => + new BindingImagePin("Identity" + index, + Sha256Digest.Parse((index % 2).ToString("x16") + new string('0', 48), "a test pin"), []))]; + BindingPlan plan = new("", pins, 0); + foreach (var pin in pins.DistinctBy(pin => pin.Module)) + Compile(pin.Module, "def " + pin.Module + ".image : Nat := 0\n"); + Compile(ImagesModule, ImagesModuleSource(plan)); + AuditedAxioms(Audit(ImagesModule, [BindingRequests(plan)[1].Audit]).theorems[0]); + var serialized = JsonSerializer.Serialize(BindingRequests(plan).Select(request => request.Audit), AuditJson); + using var parsed = JsonDocument.Parse(serialized); + var depth = Depth(parsed.RootElement); + requestDepth ??= depth; + if (depth != requestDepth) throw new InvalidOperationException("Binding request depth grows with pin count"); + var items = parsed.RootElement[1].GetProperty("type").GetProperty("app")[3] + .GetProperty("list").GetProperty("items"); + if (items.GetArrayLength() != count || + items.EnumerateArray().Where((item, index) => + item.GetProperty("app")[1].GetProperty("str").GetString() != pins[index].Identity || + item.GetProperty("app")[2].GetProperty("const").GetString() != pins[index].Module + ".image").Any() || + previousRequestSize != 0 && serialized.Length > previousRequestSize * 9) + throw new InvalidOperationException("Binding list serialization growth or order"); + previousRequestSize = serialized.Length; + var module = "ListScale" + count; + Compile(module, "set_option maxRecDepth 10000\ndef " + module + ".values : List Nat := " + + string.Concat(Enumerable.Range(0, count).Select(index => "@List.cons Nat " + index + " (")) + + "@List.nil Nat" + new string(')', count) + "\n"); + var audited = Audit(module, [], [new(module + ".values", + ListPattern(listNat, 0, [.. Enumerable.Range(0, count).Select(Nat)]))]); + if (audited.constants[0].error is not null) + throw new InvalidOperationException("Large list audit: " + audited.constants[0].error); + Console.WriteLine($"PASS {count} pins: {serialized.Length} characters, depth {depth}, list audited"); +} +Console.WriteLine(Elaborated(""" + import CIL.OleanAudit + open Lean + #eval show IO Unit from do + let ty := mkConst ``Nat + let nil := mkApp (mkConst ``List.nil [.zero]) ty + let cons := fun head tail => mkApp3 (mkConst ``List.cons [.zero]) ty head tail + let mut previous := 0 + let mut iterations := 1 + let mut baseline := 0 + let minimumNanos := 25000000 + for count in [16, 128, 1024] do + let mut tail := nil + for index in [:count - 1] do tail := cons (mkNatLit index) tail + let pattern := Json.mkObj [("list", Json.mkObj [ + ("type", Json.mkObj [("const", toJson "Nat")]), ("level", toJson (0 : Nat)), + ("items", toJson (Array.replicate count (Json.str "_")))])] + let started <- IO.getNumHeartbeats + for iteration in [:1000] do + unless (LeanCILAudit.fits pattern (cons (mkNatLit iteration) tail)).toOption == some true do + throw <| IO.userError "matcher scaling result" + let used := (<- IO.getNumHeartbeats) - started + IO.println s!"PASS list matcher {count}: {used} heartbeats for 1000 matches" + if previous > 0 && used > previous * 12 then + throw <| IO.userError "list matcher growth exceeds linear allowance" + previous := used + let measure := fun repeats => do + let mut best := 1000000000000 + for _ in [:5] do + let start <- IO.monoNanosNow + for iteration in [:repeats] do + let result := LeanCILAudit.fits pattern (cons (mkNatLit iteration) tail) + unless result.toOption == some true do + throw <| IO.userError "matcher timing result" + best := min best ((<- IO.monoNanosNow) - start) + return best + let mut elapsed <- measure iterations + if count == 16 then + while elapsed < minimumNanos do + iterations := iterations * 2 + elapsed <- measure iterations + baseline := elapsed + -- Allocation-free quadratic traversal costs 4096x at 64x items, above the 256x bound. + if count == 1024 && elapsed > baseline * 256 then + throw <| IO.userError "list matcher time exceeds linear allowance" + IO.println s!"PASS list matcher {count}: {elapsed} ns for {iterations} matches" + """)); +if (listsOnly) return; + // These fixtures elaborate successfully, so rejection exercises the audit rather than a syntax error. Compile("Overrides", """ import Lean @@ -164,12 +369,12 @@ AuditRequest Equation(string theorem, int left, int right) => AuditedAxioms(Audit("Canonical", [Equation("Canonical.claim", 1, 1)]).theorems[0]); Console.WriteLine("PASS retargeted numeral instance fails the structural statement check"); -var refutationLine = 4; +// A same-named predicate of another module is no contract, so stating it is no refutation. Compile("Shapes", """ def Shapes.value : Nat := 3 theorem Shapes.wild : (3 : Nat) = 3 := rfl - namespace CIL.Safety.MethodContract def Holds (n : Nat) : Prop := n = 0 end CIL.Safety.MethodContract - theorem Shapes.refuted : ¬ CIL.Safety.MethodContract.Holds 1 := Nat.succ_ne_zero 0 + namespace CIL.Safety.MethodContract def Refuted (n : Nat) : Prop := n = 1 end CIL.Safety.MethodContract + theorem Shapes.refuted : CIL.Safety.MethodContract.Refuted 1 := rfl """); var shapes = Audit("Shapes", [ProofTheorem("Shapes.value"), ProofTheorem("Shapes.missing"), new("Shapes.wild", App(Const("Eq", 1), "_", "_", "_"), ApprovedAxioms), @@ -180,12 +385,58 @@ namespace CIL.Safety.MethodContract def Holds (n : Nat) : Prop := n = 0 end CIL. Rejected(shapes.theorems[1], "Expected a theorem"); AuditedAxioms(shapes.theorems[2]); Rejected(shapes.theorems[3], "Theorem statement mismatch"); -if (shapes.theorems[4].refutes is not { contract: "CIL.Safety.MethodContract.Holds", column: 8 } refutes || refutes.line != refutationLine || - shapes.theorems[5].refutes is not null || !AuditText(shapes.theorems[4]).Contains($"LeanCIL refutes: CIL.Safety.MethodContract.Holds at {refutationLine}:8\n")) - throw new InvalidOperationException("Refutation not located: " + JsonSerializer.Serialize(shapes.theorems[4])); +if (shapes.theorems[4].refutes is not null || shapes.theorems[5].refutes is not null) + throw new InvalidOperationException("A same-named predicate counted as a refutation: " + + JsonSerializer.Serialize(shapes.theorems[4])); if (shapes.constants[0].error is not null || shapes.constants[1].error is null || shapes.constants[2].error is null) throw new InvalidOperationException("Constant value checks: " + JsonSerializer.Serialize(shapes.constants)); -Console.WriteLine("PASS theorem kind, structural wildcards, constant values and refutation locations"); +Console.WriteLine("PASS theorem kind, structural wildcards, constant values and same-named predicates"); + +// Only the library's counterexample predicate is a refutation. A requested theorem stating the +// negation of a contract is refused, since a run the model does not cover proves it; a lemma +// stating it that is not requested is checked like any other. +const string RefutationSource = """ + import CIL.Safety.MethodContract + open CIL.Safety + /-- Promises a return from any memory. -/ + def Refutations.returns : MethodContract Unit := + { args := fun _ => [], pre := fun _ _ => True, footprint := fun _ => [], post := fun _ _ _ _ => True } + theorem Refutations.refuted : Refutations.returns.Refuted [] 0 := + .of_fault (input := ()) (initial := Memory.empty) (observed := 1) + (fault := ⟨0, 0, .invalidState⟩) trivial (by decide) rfl + theorem Refutations.negated : ¬ Refutations.returns.Holds [] 0 := Refutations.refuted.not_holds + theorem Refutations.uses : True := (fun _ : ¬ Refutations.returns.Holds [] 0 => trivial) Refutations.negated + def Refutations.negatedAlias : Prop := Not (Refutations.returns.Holds [] 0) + theorem Refutations.aliasNegated : Refutations.negatedAlias := Refutations.negated + abbrev Refutations.refutedAlias : Prop := Refutations.returns.Refuted [] 0 + theorem Refutations.aliasRefuted : Refutations.refutedAlias := Refutations.refuted + """; +Compile("Refutations", RefutationSource); +var refutedLine = Array.FindIndex(RefutationSource.Split('\n'), line => line.StartsWith("theorem Refutations.refuted", + StringComparison.Ordinal)) + 1; +var refutations = verification.AuditOleans(fixtureSearch, ["Refutations"], [ProofTheorem("Refutations.refuted"), + ProofTheorem("Refutations.negated"), ProofTheorem("Refutations.uses"), ProofTheorem("Refutations.aliasNegated"), + ProofTheorem("Refutations.aliasRefuted")]); +AuditedAxioms(refutations.theorems[0]); +// The audit reports the program only as a constant, so the empty list names none, and the method as its numeral. +if (refutations.theorems[0].refutes is not + { contract: "CIL.Safety.MethodContract.Holds", program: null, method: 0, column: 8 } refutes || + refutes.line != refutedLine) + throw new InvalidOperationException("Refutation not located: " + + JsonSerializer.Serialize(refutations.theorems[0])); +Rejected(refutations.theorems[1], "which a run the model does not cover can prove"); +AuditedAxioms(refutations.theorems[2]); +if (refutations.theorems[2].refutes is not null) + throw new InvalidOperationException("A theorem using a negated contract counted as a refutation"); +// Classification unfolds definitions as the kernel comparison does: an alias of a negated contract is refused, +// and an alias of a counterexample predicate is the refutation it unfolds to. +Rejected(refutations.theorems[3], "which a run the model does not cover can prove"); +AuditedAxioms(refutations.theorems[4]); +if (refutations.theorems[4].refutes is not { contract: "CIL.Safety.MethodContract.Holds", program: null, method: 0 }) + throw new InvalidOperationException($"An alias of a refutation was not classified as one: { + JsonSerializer.Serialize(refutations.theorems[4])}"); +Console.WriteLine("PASS counterexample refutations are located, negated contracts refused, lemmas stating them " + + "unchanged"); // Builtin notation such as `-1`, `/` and `bif` has formatters only in unloaded imports; printing // it must fall back to the raw expression instead of failing the audit or evaluating imports. @@ -358,14 +609,14 @@ end StatementBase .Select(name => new StatementRequest(true).Apply(ProofTheorem(name)))]).theorems; if (production.Any(theorem => theorem.term is null || theorem.termError is not null)) throw new InvalidOperationException("Statement terms: " + JsonSerializer.Serialize(production)); -AuditedTheorem Compared(string module, string theorem, AuditedTheorem expected, bool negated = false) => - Audit(module, [new StatementRequest(false, expected.term!.Value.GetRawText(), negated) +AuditedTheorem Compared(string module, string theorem, AuditedTheorem expected, bool refuted = false) => + Audit(module, [new StatementRequest(false, expected.term!.Value.GetRawText(), refuted) .Apply(ProofTheorem(theorem))]) .theorems[0]; var unfolded = Compared("StatementVariant", "Variant.claim", production[0]); var weaker = Compared("StatementVariant", "Variant.weaker", production[0]); var inlined = Compared("StatementVariant", "Variant.one", production[2]); -var forged = Compared("StatementForged", "Forged.refuted", production[1], negated: true); +var forged = Compared("StatementForged", "Forged.refuted", production[1], refuted: true); // A numeral above 2^64 crosses Lean and C# JSON exactly, so only the equal literal matches. var big = Compared("StatementVariant", "Variant.big", production[3]); var near = Compared("StatementVariant", "Variant.near", production[3]); @@ -376,8 +627,98 @@ AuditedTheorem Compared(string module, string theorem, AuditedTheorem expected, lone.mismatch?.Contains("does not declare StatementBase.zero", StringComparison.Ordinal) != true) throw new InvalidOperationException("Statement comparisons: " + JsonSerializer.Serialize(new[] { unfolded, weaker, inlined, forged, big, near, lone })); +// A comparison states whether it is with the counterexample predicate: a missing or non-boolean flag would compare +// a refutation request with Holds, so the audit refuses it. +foreach (object expected in new object[] + { + new { term = production[0].term!.Value }, new { term = production[0].term!.Value, refuted = "true" } + }) + Rejected(Audit("StatementVariant", [ProofTheorem("Variant.claim") with { expected = expected }]).theorems[0], + "expected.refuted"); +Rejected(Audit("StatementVariant", [ProofTheorem("Variant.claim") with { expected = new { refuted = false } }]) + .theorems[0], "expected.term"); +var extraField = new { term = production[0].term!.Value, refuted = false, negated = true }; +Rejected(Audit("StatementVariant", [ProofTheorem("Variant.claim") with { expected = extraField }]).theorems[0], + "negated"); + +void RejectDuplicate(string key, string json) +{ + var request = Path.Combine(root, $"duplicate-{key}.json"); + var result = Path.ChangeExtension(request, ".result.json"); + File.WriteAllText(request, json); + var audit = Execute([verification.CheckedAuditExecutable(), request, result], fixtures, fixtures, + environment: environment); + if (audit.Code == 0 || !audit.Stderr.Contains($"Duplicate JSON key: {key}", StringComparison.Ordinal)) + throw new InvalidOperationException($"Duplicate {key}: audit exited {audit.Code}. {audit.Output}"); +} +RejectDuplicate("refuted", $$""" + { "modules": ["StatementVariant"], "theorems": [{ + "name": "Variant.claim", + "expected": { "term": {{production[0].term!.Value.GetRawText()}}, "refuted": true, "refuted": false } + }] } + """); +RejectDuplicate("modules", """{ "modules": ["StatementVariant"], "modul\u0065s": ["StatementVariant"] }"""); +Console.WriteLine(Run([verification.LeanExecutable, At("Tests/AuditKeyScaling.lean")], + fixtures, fixtureSearch, environment: environment)); Console.WriteLine("PASS the kernel compares statements by unfolding, refuses weaker ones and forged constants"); +// A refutation compared with production's statement refutes production's subject, whatever form the variant +// states it in, so the audit reports that subject for verification to bind to the extraction. +string TargetProgram(string methods) => $$""" + import CIL.Safety.MethodContract + /-- Returns at once. -/ + def TargetProgram.stop : CIL.Method := { code := [.ret], locals := [], returnsValue := false } + /-- The program both legs name, with the methods this build gives it. -/ + def TargetProgram.program : CIL.Program := {{methods}} + """; +Compile("TargetProgram", TargetProgram("[TargetProgram.stop, TargetProgram.stop]")); +Compile("TargetProduction", """ + import TargetProgram + import Refutations + open CIL.Safety + theorem TargetProduction.holds : Refutations.returns.Holds (TargetProgram.program.drop 1) 0 := + fun _ memory _ => MethodContract.Holds.intro (c := Refutations.returns) memory [] + (Exists.intro 1 rfl) (Confined.refl _ memory) trivial + """); +var targetProduction = verification.AuditOleans(fixtureSearch, ["TargetProduction"], + [new StatementRequest(true).Apply(ProofTheorem("TargetProduction.holds"))]).theorems[0]; +// The variant's build leaves the slot production names empty, and its theorem is a redex no reading classifies. +Compile("TargetProgram", TargetProgram("[TargetProgram.stop]")); +Compile("TargetVariant", """ + import TargetProgram + import Refutations + theorem TargetVariant.refuted : + (fun program => Refutations.returns.Refuted program 0) (TargetProgram.program.drop 1) := + Refutations.refuted + """); +var target = verification.AuditOleans(fixtureSearch, ["TargetVariant"], + [new StatementRequest(false, targetProduction.term!.Value.GetRawText(), Refuted: true) + .Apply(ProofTheorem("TargetVariant.refuted"))]).theorems[0]; +if (target.matches != true || target.refutes is not { contract: "CIL.Safety.MethodContract.Holds", program: null, + method: 0 }) + throw new InvalidOperationException($"A compared refutation did not report production's subject: { + JsonSerializer.Serialize(target)}"); +Console.WriteLine("PASS a compared refutation reports the subject of the statement it was compared with"); + +// A program named through definitions whose value is a constant is reported as that constant, and Nat.zero as 0. +Compile("AliasProgram", """ + import Refutations + /-- The program the aliases name. -/ + def AliasProgram.base : CIL.Program := [] + /-- `AliasProgram.base` under another name. -/ + def AliasProgram.alias : CIL.Program := AliasProgram.base + /-- `AliasProgram.alias` under another name. -/ + def AliasProgram.again : CIL.Program := AliasProgram.alias + theorem AliasProgram.refuted : Refutations.returns.Refuted AliasProgram.again Nat.zero := + Refutations.refuted + """); +var aliased = verification.AuditOleans(fixtureSearch, ["AliasProgram"], [ProofTheorem("AliasProgram.refuted")]) + .theorems[0]; +if (aliased.refutes is not { contract: "CIL.Safety.MethodContract.Holds", program: "AliasProgram.base", method: 0 }) + throw new InvalidOperationException($"An aliased program was not reported as the constant it names: { + JsonSerializer.Serialize(aliased)}"); +Console.WriteLine("PASS a refutation's program is read through aliases and its method through Nat.zero"); + // A user module cannot replace a toolchain module on the audit's search path. Compile("Init", "prelude\n"); var resolved = verification.AuditOleans(fixtures, ["Init"], []).modules["Init"]; diff --git a/Tests/OpaqueClassFields.lean b/Tests/OpaqueClassFields.lean new file mode 100644 index 000000000..c73386919 --- /dev/null +++ b/Tests/OpaqueClassFields.lean @@ -0,0 +1,228 @@ +import CIL.Extraction.Translation +import Tests.BodyEncoding + +/-! +# Class fields of types outside the model + +`Fixture` of `T` holds fields typed `object`, `List`, `IList`, `Func`, the value +type `ValueTuple`, a generic parameter, a modified `object` and a `Fixture`, and +`Holder` a static `object`. Binding admits each generic class instance as an `apply` chain over a +class head and `object` as `System.Object` of the primitive assembly, and refuses a head of the +other kind, a wrong argument, a claimed `string`, a parameter, a modifier and the static. A load +of such a field pushes an opaque reference, which another such field stores, which a type test +turns into an object, and which no receiver, catalog-typed field or array operation takes. +-/ + +namespace Tests.OpaqueClassFields + +open CIL CIL.Extraction Tests.MetadataBuilder + +private def pin : AssemblyPin := + { identity := "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } +private def int32 : TypeExpr := .atom runtimeScope "System.Int32" +private def int64 : TypeExpr := .atom runtimeScope "System.Int64" +private def fixture : TypeExpr := .object pin.identity "Fixture" +private def holder : TypeExpr := .object pin.identity "Holder" +private def systemObject : TypeExpr := .object runtimeScope "System.Object" + +/-- A generic class instance of the primitive assembly, `name` closed at `arguments`. -/ +private def classOf (name : String) (arguments : List TypeExpr) : TypeExpr := + arguments.foldl .apply (.objectConstructor runtimeScope name) + +/-- The same instance stated with a value head. -/ +private def valueOf (name : String) (arguments : List TypeExpr) : TypeExpr := + arguments.foldl .apply (.constructor runtimeScope name) + +private def listName : String := "System.Collections.Generic.List`1" +private def tupleName : String := "System.ValueTuple`2" + +/-- The tokens of the fields of `Fixture`, in declaration order, and of the static of `Holder`. -/ +private structure Tokens where + object : Nat + list : Nat + interfaceList : Nat + function : Nat + tuple : Nat + parameter : Nat + modified : Nat + catalog : Nat + static : Nat + +/-- The coded `TypeDefOrRef` of a `TypeRef` token. -/ +private def referenceCoded (token : Nat) : Nat := (token - 0x01000000) * 4 + 1 + +/-- `T` with the classes `Fixture` and `Holder` and the fields this file describes. -/ +private def world : Step Tokens := do + let runtimeName ← string "System.Runtime" + let publicKeyToken ← blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtimeName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let systemName ← string "System" + let collectionsName ← string "System.Collections.Generic" + let servicesName ← string "System.Runtime.CompilerServices" + let objectRef ← row Table.typeRef [1 * 4 + 2, ← string "Object", systemName] + let listRef ← row Table.typeRef [1 * 4 + 2, ← string "List`1", collectionsName] + let interfaceRef ← row Table.typeRef [1 * 4 + 2, ← string "IList`1", collectionsName] + let functionRef ← row Table.typeRef [1 * 4 + 2, ← string "Func`1", systemName] + let tupleRef ← row Table.typeRef [1 * 4 + 2, ← string "ValueTuple`2", systemName] + let volatileRef ← row Table.typeRef [1 * 4 + 2, ← string "IsVolatile", servicesName] + let _ ← row Table.typeDef [0, ← string "", 0, 0, 1, 1] + let base := referenceCoded objectRef + let owner ← row Table.typeDef [1, ← string "Fixture", 0, base, 1, 1] + let instanceFlags := 0x0006 + let typed (name : String) (signature : List Nat) : Step Nat := do + let field ← row Table.field [instanceFlags, ← string name, ← blob (0x06 :: signature)] + link linkDeclaring field 0 owner + pure field + let object ← typed "O" [0x1C] + let list ← typed "L" [0x15, 0x12, referenceCoded listRef, 1, 0x08] + let interfaceList ← typed "I" [0x15, 0x12, referenceCoded interfaceRef, 1, 0x08] + let function ← typed "F" [0x15, 0x12, referenceCoded functionRef, 1, 0x08] + let tuple ← typed "V" [0x15, 0x11, referenceCoded tupleRef, 2, 0x08, 0x08] + let parameter ← typed "P" [0x13, 0] + let modified ← typed "M" [0x1F, referenceCoded volatileRef, 0x1C] + let catalog ← typed "C" [0x12, (owner - 0x02000000) * 4] + let holderType ← row Table.typeDef [1, ← string "Holder", 0, base, 9, 1] + let static ← row Table.field [0x0016, ← string "S", ← blob [0x06, 0x1C]] + link linkDeclaring static 0 holderType + pure { object, list, interfaceList, function, tuple, parameter, modified, catalog, static } + +private def slice : MetadataSlice × Tokens := build world (bytesOf "T") pin.identity + +/-- A type of `System.Runtime`'s `System.Collections.Generic` namespace of this kind. -/ +private def collectionType (name : String) (kind : PlatformKind) : PlatformDefinition := + { scope := runtimeScopeBytes, parts := [bytesOf "System.Collections.Generic", bytesOf name], + kind } + +/-- The platform definitions of the types the fields and the classes' base name. -/ +private def fieldPlatform : PlatformTable := platform_table% + [runtimeType "Object" .objectRoot, collectionType "List`1" (.genericClass 1), + collectionType "IList`1" (.genericInterface 1), runtimeType "Func`1" (.genericClass 1), + runtimeType "ValueTuple`2" .valuePair] + +/-- The error binding `token` to `operand` gives, or `none` when it is accepted. -/ +private def check (token : Nat) (operand : RawOperand) : Option MetadataError := + let env := (MetadataEnv.ofSlices [·] (platform := fieldPlatform)) <$> slice.1.index + match (do checkEntry (← env) 0 token operand) with + | .ok _ => none + | .error found => some found + +/-- The instance field operand of `Fixture` at `index` holding `type`. -/ +private def field (index : Nat) (type : TypeExpr) (name : MetadataName) : RawOperand := + .field fixture index type name false + +private def tokens : Tokens := slice.2 + +/-- Each generic class instance binds as an `apply` chain over a class head and `object` as + `System.Object` of the primitive assembly, whichever instruction uses the token. -/ +theorem opaque_class_fields_bound : + check tokens.object (field 0 systemObject name!"O") = none ∧ + check tokens.list (field 1 (classOf listName [int32]) name!"L") = none ∧ + check tokens.interfaceList + (field 2 (classOf "System.Collections.Generic.IList`1" [int32]) name!"I") = none ∧ + check tokens.function (field 3 (classOf "System.Func`1" [int32]) name!"F") = none ∧ + check tokens.tuple (field 4 (valueOf tupleName [int32, int32]) name!"V") = none ∧ + check tokens.catalog (field 7 fixture name!"C") = none := by decide +kernel +#print axioms opaque_class_fields_bound + +/-- A static `object`, `object` claimed as `string`, `ValueTuple` under a class + head, `List` under a value head or as `List`, a generic parameter and a modified + `object` are each refused on their token. -/ +theorem misstated_class_fields_refused : + check tokens.static (.field holder 0 systemObject name!"S" true) = + some (.operandMismatch tokens.static) ∧ + check tokens.static (.field holder 0 (.atom runtimeScope "System.Object") name!"S" true) = + none ∧ + check tokens.object (field 0 (.object runtimeScope "System.String") name!"O") = + some (.operandMismatch tokens.object) ∧ + check tokens.tuple (field 4 (classOf tupleName [int32, int32]) name!"V") = + some (.operandMismatch tokens.tuple) ∧ + check tokens.list (field 1 (valueOf listName [int32]) name!"L") = + some (.operandMismatch tokens.list) ∧ + check tokens.list (field 1 (classOf listName [int64]) name!"L") = + some (.operandMismatch tokens.list) ∧ + check tokens.parameter (field 5 (.parameter 0) name!"P") = + some (.operandMismatch tokens.parameter) ∧ + check tokens.parameter (field 5 (.object runtimeScope "T") name!"P") = + some (.operandMismatch tokens.parameter) ∧ + check tokens.modified (field 6 systemObject name!"M") = + some (.operandMismatch tokens.modified) := by decide +kernel +#print axioms misstated_class_fields_refused + +/-! ## Stack kinds -/ + +private def i (opcode : Nat) (operand : RawOperand := .none) : RawInstruction := + ⟨0, opcode, operand⟩ + +/-- An instance method of `Fixture` taking a `Fixture`, with `code`. -/ +private def method (code : List RawInstruction) : RawMethod := + { token := 0x06000001, owner := pin.identity ++ "::Fixture", hasThis := true, + objectReceiver := true, explicitThis := false, callingConvention := 0, attributes := 0, + implementation := 0, returnType := .void, parameters := [fixture], locals := [], + initLocals := false, maxStack := 2, genericParameters := [], code } + +private def unknown (count : Nat) : Option FeatureState := some ⟨List.replicate count none, []⟩ + +/-- The field of `Fixture` at `index` holding `type`, as an instruction names it. -/ +private def fieldRef (index : Nat) (type : TypeExpr) (name : MetadataName) : FieldRef := + { owner := fixture, index := index, type := type, name := name } + +/-- `this.F = other.F` for the field `F` at `index` holding `type`, with an opaque reference + between the load and the store. -/ +private def copies (index : Nat) (type : TypeExpr) (name : MetadataName) : Bool := + let operand := field index type name + let raw := method [i 2, i 3, i 123 operand, i 125 operand, i 42] + let reference := fieldRef index type name + decide (ScalarFlowValid raw [unknown 0, unknown 1, unknown 2, unknown 2, unknown 0] + [some [], some [.object], some [.object, .object], some [.opaque, .object], some []] + [.ldarg 0, .ldarg 1, .ldfld reference, .stfld reference, .ret]) + +/-- A record copy of each such field loads an opaque reference and stores it. -/ +theorem opaque_fields_copied : + copies 0 systemObject name!"O" = true ∧ + copies 1 (classOf listName [int32]) name!"L" = true ∧ + copies 2 (classOf "System.Collections.Generic.IList`1" [int32]) name!"I" = true ∧ + copies 3 (classOf "System.Func`1" [int32]) name!"F" = true := by decide +kernel +#print axioms opaque_fields_copied + +/-- Whether `stfld` of the field at `index` holding `type` takes `value` on an object. -/ +private def stores (index : Nat) (type : TypeExpr) (name : MetadataName) (value : ScalarKind) : + Bool := + scalarEmissionMatches none (some (i 3)) (i 125 (field index type name)) [value, .object] + (.stfld (fieldRef index type name)) + +/-- The method `Fixture::Run()`, called with a receiver. -/ +private def runMethod : MethodRef := + { owner := fixture, name := name!"Run", hasThis := true, genericArity := 0, + returnType := .void, parameters := [] } + +/-- The call of `runMethod`, linked to the second method of `T`. -/ +private def run : CallSite := + { method := runMethod, declared := { convention := 0x20 }, + target := .managed 0x06000002, typeArguments := [], + parameterCount := 0, returnsValue := false } + +/-- An opaque reference is no `Fixture` to store, call or read through, and no array to measure; + `castclass Fixture` turns it into an object that is. `object` also stores an object, a + string or a class array, but not a value array or what `box` leaves. -/ +theorem opaque_references_not_read_through : + stores 7 fixture name!"C" .opaque = false ∧ + scalarTransfer (method []) (i 116 (.type fixture)) [.opaque] = some [.object] ∧ + scalarTransfer (method []) (i 117 (.type fixture)) [.opaque] = some [.object] ∧ + stores 7 fixture name!"C" .object = true ∧ + scalarTransfer (method []) (i 111 (.call run)) [.opaque] = none ∧ + scalarTransfer (method []) (i 111 (.call run)) [.object] = some [] ∧ + scalarTransfer (method []) (i 123 (field 7 fixture name!"C")) [.opaque] = some [.object] ∧ + scalarEmissionMatches none (some (i 3)) (i 123 (field 7 fixture name!"C")) [.opaque] + (.ldfld (fieldRef 7 fixture name!"C")) = false ∧ + scalarTransfer (method []) (i 142) [.opaque] = none ∧ + scalarTransfer (method []) (i 142) [.object] = some [.native] ∧ + stores 0 systemObject name!"O" .object = true ∧ + stores 0 systemObject name!"O" .nullObject = true ∧ + stores 0 systemObject name!"O" (.array .signed32) = false ∧ + scalarTransfer (method []) (i 140 (.type int32)) [.word32] = some [.other] ∧ + stores 0 systemObject name!"O" .other = false ∧ + stores 1 (classOf listName [int32]) name!"L" .object = false := by decide +kernel +#print axioms opaque_references_not_read_through + +end Tests.OpaqueClassFields diff --git a/Tests/OperationFactAdapters.lean b/Tests/OperationFactAdapters.lean index 2a610c182..273a8181f 100644 --- a/Tests/OperationFactAdapters.lean +++ b/Tests/OperationFactAdapters.lean @@ -18,7 +18,7 @@ theorem local_address {memory other : Memory} {reference : Reference} {kind : CI (slot : frame.locals[index]? = some (.bytes kind reference)) (wrongMemory : form other reference = .ok reference) (formed : form memory reference = .ok reference) : - step body (.localAddr index) pc args frame rest memory = + step body (.ldloca index) pc args frame rest memory = .ok (.next (pc + 1) (.reference (.address reference) :: rest) frame memory) := by fail_if_success solve | cil_checked_step [slot, wrongMemory] cil_checked_step [slot, formed] @@ -28,25 +28,27 @@ theorem local_address {memory other : Memory} {reference : Reference} {kind : CI theorem signed_byte {memory : Memory} {source : Reference} {bits : BitVec 8} (loaded : read memory source 1 1 = .ok (numberBytes bits.toNat 1)) (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadScalar 1 true) pc args frame (.reference (.address source) :: rest) memory = + step body (.ldind .i1) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 (bits.signExtend 32)) :: rest) frame memory) := by cil_checked_step [loaded] #print axioms signed_byte -/-- A signed two-byte load extends both initialized bytes to the CIL stack width. -/ +/-- A signed two-byte ldobj through a readonly reference extends both initialized bytes. -/ theorem signed_short {memory : Memory} {source : Reference} {bits : BitVec 16} (loaded : read memory source 2 1 = .ok (numberBytes bits.toNat 2)) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadScalar 2 true) pc args frame (.reference (.address source) :: rest) memory = + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .primitive 2 true) (pc : Nat) (args : List Value) + (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.readOnlyReference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 (bits.signExtend 32)) :: rest) frame memory) := by - cil_checked_step [loaded] + cil_checked_step [shape, loaded] #print axioms signed_short /-- The read adapter selects equations inside nested conjunctions. -/ theorem conjoined_read {memory : Memory} {source : Reference} {bits : CIL.W64} (facts : True ∧ (read memory source 8 1 = .ok (numberBytes bits.toNat 8) ∧ True)) (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body .load64 pc args frame (.reference (.address source) :: rest) memory = + step body (.ldind .i8) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i64 bits) :: rest) frame memory) := by cil_checked_step [facts] #print axioms conjoined_read @@ -55,7 +57,7 @@ theorem conjoined_read {memory : Memory} {source : Reference} {bits : CIL.W64} theorem conjoined_write {memory after : Memory} {target : Reference} {bits : CIL.W64} (facts : True ∧ (write memory target (numberBytes bits.toNat 8) 1 = .ok after ∧ True)) (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body .store64 pc args frame + step body (.stind .i8) pc args frame (.scalar (.i64 bits) :: .reference (.address target) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by cil_checked_step [facts] @@ -68,7 +70,7 @@ theorem quantified_signed_read {memory : Memory} (source : Nat → Reference) .ok (numberBytes (bits index).toNat 2)) (index : Nat) (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadScalar 2 true) pc args frame + step body (.ldind .i2) pc args frame (.reference (.address (source index)) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 ((bits index).signExtend 32)) :: rest) frame memory) := by @@ -80,9 +82,8 @@ theorem quantified_vector_write {memory : Memory} (after : Nat → Memory) (target : Nat → Reference) (bits : Nat → BitVec 256) (facts : ∀ index, write memory (target index) (numberBytes (bits index).toNat 32) 1 = .ok (after index) ∧ True) - (index : Nat) (body : CIL.Method) (pc : Nat) (args : List Value) - (frame : Frame) (rest : List Value) : - step body (.memory .store256) pc args frame + (index : Nat) (body : CIL.Method) (pc : Nat) (frame : Frame) (rest : List Value) : + modelStep (some (.memory .store256)) body pc frame (.scalar (.v256 (bits index)) :: .reference (.address (target index)) :: rest) memory = .ok (.next (pc + 1) rest frame (after index)) := by cil_checked_step [facts] @@ -91,23 +92,25 @@ theorem quantified_vector_write {memory : Memory} (after : Nat → Memory) /-- Aggregate reads accept symbolic widths and byte lists inside a conjunction. -/ theorem conjoined_aggregate_read {memory : Memory} {source : Reference} {width : Nat} {bytes : List (BitVec 8)} (facts : True ∧ read memory source width 1 = .ok bytes) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate width) pc args frame (.reference (.address source) :: rest) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate width) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.aggregate bytes) :: rest) frame memory) := by - cil_checked_step [facts] + cil_checked_step [shape, facts] #print axioms conjoined_aggregate_read /-- A readonly aggregate preserves the initialization mask of a quantified snapshot fact. -/ theorem quantified_readonly_snapshot {memory : Memory} (source : Nat → Reference) (width : Nat) (bytes : Nat → List (BitVec 8 × Bool)) (facts : ∀ index, True ∧ readSnapshot memory (source index) width 1 = .ok (bytes index)) - (index : Nat) (body : CIL.Method) (pc : Nat) (args : List Value) + (index : Nat) (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .aggregate width) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadAggregate width) pc args frame + step body (.ldobj type) pc args frame (.readOnlyReference (.address (source index)) :: rest) memory = .ok (.next (pc + 1) (.scalar (snapshotValue (.aggregate width) (bytes index)) :: rest) frame memory) := by - cil_checked_step [facts] + cil_checked_step [shape, facts] #print axioms quantified_readonly_snapshot /-- Other memory, reference, width or write authority cannot replace an initialized read. -/ @@ -121,7 +124,7 @@ theorem read_requires_matching_evidence {memory other after : Memory} (_onlySnapshot : True ∧ readSnapshot memory source 2 1 = .ok bytes) (loaded : read memory source 2 1 = .ok (numberBytes bits.toNat 2)) (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadScalar 2 true) pc args frame (.reference (.address source) :: rest) memory = + step body (.ldind .i2) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 (bits.signExtend 32)) :: rest) frame memory) := by fail_if_success cil_checked_step [_wrongMemory] fail_if_success cil_checked_step [_wrongSource] @@ -141,24 +144,26 @@ theorem field_store_literal_conversion (left right : CIL.W64) (numberBytes (left + right + (1#32).signExtend 64).toNat 8) 1 = .ok final) (written : write memory reference (numberBytes (left + right + (1#32).signExtend 64).toNat 8) 1 = .ok final) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) : - step body (.setFieldAt 0 8) pc args frame + (body : CIL.Method) (operand : CIL.Extraction.FieldRef) + (shape : body.fieldShape operand = .value 0 8 false) + (pc : Nat) (args : List Value) (frame : Frame) : + step body (.stfld operand) pc args frame [.scalar (.i64 (left + right + 1#64)), .reference (.address reference)] memory = .ok (.next (pc + 1) [] frame final) := by - fail_if_success cil_checked_step [added, _wrongMemory] - fail_if_success cil_checked_step [added, _wrongReference] - cil_checked_step [added, written] + fail_if_success cil_checked_step [shape, added, _wrongMemory] + fail_if_success cil_checked_step [shape, added, _wrongReference] + cil_checked_step [shape, added, written] #print axioms field_store_literal_conversion /-- A truth-tested branch selects one of two distinct scalar results. -/ -@[cil_code] def truthBranchBody (branch : CIL.Op) : CIL.Method := { +@[cil_code] def truthBranchBody (branch : CIL.Instruction) : CIL.Method := { locals := [], returnsValue := true, - code := [.arg 0, branch, .const64 1, .ret, .const64 2, .ret] } + code := [.ldarg 0, branch, .ldc_i8 1, .ret, .ldc_i8 2, .ret] } /-- A negative 64-bit case fact decides the next pc before it becomes a conditional term. -/ theorem branch_nonzero_literal (word unrelated : CIL.W64) (memory : Memory) (_unrelated : unrelated ≠ 0#64) (nonzero : word ≠ 0#64) : - invoke [truthBranchBody (.brnonzero 4)] 5 0 [.scalar (.i64 word)] memory = + invoke [truthBranchBody (.brtrue 4)] 5 0 [.scalar (.i64 word)] memory = .ok (memory, [.scalar (.i64 2)]) := by fail_if_success (cil_checked_steps [_unrelated]; done) cil_checked_steps [nonzero] @@ -166,7 +171,7 @@ theorem branch_nonzero_literal (word unrelated : CIL.W64) (memory : Memory) /-- A positive 32-bit case fact uses the same literal spelling for a zero branch. -/ theorem branch_zero_literal (word : CIL.W32) (memory : Memory) (zero : word = 0#32) : - invoke [truthBranchBody (.brzero 4)] 5 0 [.scalar (.i32 word)] memory = + invoke [truthBranchBody (.brfalse 4)] 5 0 [.scalar (.i32 word)] memory = .ok (memory, [.scalar (.i64 2)]) := by cil_checked_steps [zero] #print axioms branch_zero_literal @@ -175,7 +180,7 @@ theorem branch_zero_literal (word : CIL.W32) (memory : Memory) (zero : word = 0# theorem branch_truth_fact (word unrelated : CIL.W32) (memory : Memory) (_unrelated : CIL.truth (.i32 unrelated) = some false) (truth : CIL.truth (.i32 word) = some false) : - invoke [truthBranchBody (.brzero 4)] 5 0 [.scalar (.i32 word)] memory = + invoke [truthBranchBody (.brfalse 4)] 5 0 [.scalar (.i32 word)] memory = .ok (memory, [.scalar (.i64 2)]) := by fail_if_success (cil_checked_steps [_unrelated]; done) cil_checked_steps [truth] @@ -184,14 +189,14 @@ theorem branch_truth_fact (word unrelated : CIL.W32) (memory : Memory) /-- An unrelated truth equation does not prevent a matching zero comparison from deciding. -/ theorem branch_truth_fact_fallback (word unrelated : CIL.W64) (memory : Memory) (_unrelated : CIL.truth (.i64 unrelated) = some false) (nonzero : word ≠ 0#64) : - invoke [truthBranchBody (.brnonzero 4)] 5 0 [.scalar (.i64 word)] memory = + invoke [truthBranchBody (.brtrue 4)] 5 0 [.scalar (.i64 word)] memory = .ok (memory, [.scalar (.i64 2)]) := by cil_checked_steps [_unrelated, nonzero] #print axioms branch_truth_fact_fallback /-- A closed zero operand decides the negative branch condition without a supplied guard. -/ theorem branch_closed_zero (memory : Memory) : - invoke [truthBranchBody (.brnonzero 4)] 5 0 [.scalar (.i32 0)] memory = + invoke [truthBranchBody (.brtrue 4)] 5 0 [.scalar (.i32 0)] memory = .ok (memory, [.scalar (.i64 1)]) := by cil_checked_steps [] #print axioms branch_closed_zero @@ -199,7 +204,7 @@ theorem branch_closed_zero (memory : Memory) : /-- A signed comparison follows a narrowing conversion of a symbolic natural literal. -/ @[cil_code] def narrowedBranchBody : CIL.Method := { locals := [], returnsValue := true, - code := [.arg 0, .arg 1, .convI4, .blt 6, .const64 1, .ret, .const64 2, .ret] } + code := [.ldarg 0, .ldarg 1, .conv_i4, .blt 6, .ldc_i8 1, .ret, .ldc_i8 2, .ret] } /-- Narrowed branch operands match the supplied comparison without expanding symbolic arithmetic. -/ theorem branch_narrowed_literal (index : CIL.W32) (length : Nat) (memory : Memory) @@ -226,7 +231,7 @@ theorem named_report_write (condition : Prop) [Decidable condition] {memory after : Memory} {target : Reference} (written : write memory target (numberBytes (reportedWord condition).toNat 8) 1 = .ok after) (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body .store64 pc args frame + step body (.stind .i8) pc args frame (.scalar (.i64 ((if condition then (1 : BitVec 32) else 0).signExtend 64)) :: .reference (.address target) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by @@ -238,7 +243,9 @@ def forwardedResults (values : List Value) : List Value := values /-- Narrowed return equations accept a stack hidden behind a named result projection. -/ theorem projected_return (memory : Memory) (frame : Frame) : - step { code := [], locals := [], returnsValue := true } (.ret (some .unsigned8)) + step + { code := [], locals := [], returnsValue := true, + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Byte" } } .ret 1 [] frame (forwardedResults [.scalar (.i32 1)] ++ []) memory = .ok (.returned [.scalar (.i32 1)] memory) := by cil_checked_step [] diff --git a/Tests/OperationFacts.lean b/Tests/OperationFacts.lean index 9acf24daf..9221dc50b 100644 --- a/Tests/OperationFacts.lean +++ b/Tests/OperationFacts.lean @@ -13,8 +13,8 @@ open CIL.Safety /-- A vector load consumes the read fact for the same memory, reference and width. -/ theorem vector_load {memory : Memory} {source : Reference} {bits : BitVec 256} (loaded : read memory source 32 1 = .ok (numberBytes bits.toNat 32)) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.memory .load256) pc args frame (.reference (.address source) :: rest) memory = + (body : CIL.Method) (pc : Nat) (frame : Frame) (rest : List Value) : + modelStep (some (.memory .load256)) body pc frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.v256 bits) :: rest) frame memory) := by cil_checked_step [loaded] #print axioms vector_load @@ -22,8 +22,8 @@ theorem vector_load {memory : Memory} {source : Reference} {bits : BitVec 256} /-- A vector store consumes its successful write, without asking for any previous snapshot. -/ theorem vector_store {memory after : Memory} {destination : Reference} {bits : BitVec 256} (written : write memory destination (numberBytes bits.toNat 32) 1 = .ok after) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.memory .store256) pc args frame + (body : CIL.Method) (pc : Nat) (frame : Frame) (rest : List Value) : + modelStep (some (.memory .store256)) body pc frame (.scalar (.v256 bits) :: .reference (.address destination) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by cil_checked_step [written] @@ -33,7 +33,7 @@ theorem vector_store {memory after : Memory} {destination : Reference} {bits : B theorem word_load {memory : Memory} {source : Reference} {bits : CIL.W64} (loaded : read memory source 8 1 = .ok (numberBytes bits.toNat 8)) (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body .load64 pc args frame (.reference (.address source) :: rest) memory = + step body (.ldind .i8) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i64 bits) :: rest) frame memory) := by cil_checked_step [loaded] #print axioms word_load @@ -42,7 +42,7 @@ theorem word_load {memory : Memory} {source : Reference} {bits : CIL.W64} theorem word_store {memory after : Memory} {destination : Reference} {bits : CIL.W64} (written : write memory destination (numberBytes bits.toNat 8) 1 = .ok after) (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body .store64 pc args frame + step body (.stind .i8) pc args frame (.scalar (.i64 bits) :: .reference (.address destination) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by cil_checked_step [written] @@ -51,29 +51,84 @@ theorem word_store {memory after : Memory} {destination : Reference} {bits : CIL /-- An initialized byte load zero-extends the checked byte encoding. -/ theorem byte_load {memory : Memory} {source : Reference} {bits : BitVec 8} (loaded : read memory source 1 1 = .ok (numberBytes bits.toNat 1)) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.loadScalar 1 false) pc args frame (.reference (.address source) :: rest) memory = + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} + (shape : body.valueShape type = .primitive 1 false) (pc : Nat) (args : List Value) + (frame : Frame) (rest : List Value) : + step body (.ldobj type) pc args frame (.reference (.address source) :: rest) memory = .ok (.next (pc + 1) (.scalar (.i32 (bits.setWidth 32)) :: rest) frame memory) := by - cil_checked_step [loaded] + cil_checked_step [shape, loaded] #print axioms byte_load /-- A byte store consumes write authority without requiring old initialized contents. -/ theorem byte_store {memory after : Memory} {destination : Reference} {bits : CIL.W32} (written : write memory destination (numberBytes bits.toNat 1) 1 = .ok after) + (body : CIL.Method) {type : CIL.Extraction.TypeExpr} {signed : Bool} + (shape : body.valueShape type = .primitive 1 signed) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.stobj type) pc args frame + (.scalar (.i32 bits) :: .reference (.address destination) :: rest) memory = + .ok (.next (pc + 1) rest frame after) := by + cil_checked_step [shape, written] +#print axioms byte_store + +/-- A four-byte `stind` consumes the write fact for its width; its kind decides the width. -/ +theorem int_store {memory after : Memory} {destination : Reference} {bits : CIL.W32} + (written : write memory destination (numberBytes bits.toNat 4) 1 = .ok after) (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body (.storeScalar 1) pc args frame + step body (.stind .i4) pc args frame (.scalar (.i32 bits) :: .reference (.address destination) :: rest) memory = .ok (.next (pc + 1) rest frame after) := by cil_checked_step [written] -#print axioms byte_store +#print axioms int_store + +/-- Stores its second argument as two bytes through its first. -/ +@[cil_code] def storeShortMethod : CIL.Method := { + code := [.ldarg 0, .ldarg 1, .stind .i2, .ret] + locals := [], returnsValue := false } + +/-- A body run reaches a two-byte `stind` and steps it with the fact derived from its write. -/ +theorem stored_short {memory after : Memory} {destination : Reference} {bits : CIL.W32} + (formed : form memory destination = .ok destination) + (written : write memory destination (numberBytes bits.toNat 2) 1 = .ok after) : + invoke [storeShortMethod] 5 0 [.reference (.address destination), .scalar (.i32 bits)] + memory = .ok (after, []) := by + cil_checked_steps [formed, written] +#print axioms stored_short + +/-- A one-byte write of a one-byte literal, as an object's flag field stores it, gives no int + store step; the run beside it kernel-checks. -/ +theorem stored_beside_flag_write {memory after other : Memory} {destination : Reference} + {bits : CIL.W32} + (formed : form memory destination = .ok destination) + (written : write memory destination (numberBytes bits.toNat 2) 1 = .ok after) + (flagged : write memory destination (numberBytes (1 : BitVec (8 * 1)).toNat 1) 1 = .ok other) : + invoke [storeShortMethod] 5 0 [.reference (.address destination), .scalar (.i32 bits)] + memory = .ok (after, []) := by + cil_checked_steps [formed, written, flagged] +#print axioms stored_beside_flag_write + +/-- A one-byte `stind` steps with its own write while a write of a narrowed int, as a narrow + struct field stores it, is supplied beside it; the step kernel-checks. -/ +theorem byte_stind_beside_narrowed_write {memory after other : Memory} {destination : Reference} + {bits value : CIL.W32} + (written : write memory destination (numberBytes bits.toNat 1) 1 = .ok after) + (narrowed : write memory destination (numberBytes (value.setWidth 8).toNat 1) 1 = .ok other) + (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : + step body (.stind .i1) pc args frame + (.scalar (.i32 bits) :: .reference (.address destination) :: rest) memory = + .ok (.next (pc + 1) rest frame after) := by + cil_checked_step [written, narrowed] +#print axioms byte_stind_beside_narrowed_write /-- A symbolic reference with an established formation result can be skipped without a write. -/ theorem skip_formed_reference {memory : Memory} {reference : ManagedReference} {value : Value} (formed : formValue memory reference = .ok value) - (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) : - step body .skipInit pc args frame (.reference reference :: rest) memory = + (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (rest : List Value) + {method : CIL.Extraction.MethodRef} (link : body.linkAt pc = none) + (model : CIL.Extraction.runtimeModel body.environment .call method = some .skipInit) : + step body (.call method {}) pc args frame (.reference reference :: rest) memory = .ok (.next (pc + 1) rest frame memory) := by - cil_checked_step [formed] + rw [step_call_skipInit_reference (link := link) (found := model) (direct := rfl), formed] + rfl #print axioms skip_formed_reference end Tests.OperationFacts diff --git a/Tests/OutcomeContract.cs b/Tests/OutcomeContract.cs index be98317be..fb243622e 100644 --- a/Tests/OutcomeContract.cs +++ b/Tests/OutcomeContract.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // The outcome proofs verify against the compiled build; an off-by-one threshold or a different // exception type must fail them and provably violate the callee's contract. @@ -15,7 +20,8 @@ string Build(string name, string percent) var source = Path.Combine(root, name); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Outcomes.csproj"), """ - net10.0true + net11.0 + true """); File.WriteAllText(Path.Combine(source, "Outcomes.cs"), "using System; namespace Outcomes; public static class Calls { " + percent + " " + Callers + " }"); @@ -59,24 +65,20 @@ string Proof(string name, string body) """; const string RefuteThreshold = Empty + """ /-- This build throws for `100`; the contract requires it to return. -/ -theorem correctness : ¬ percentContract.Holds Extracted.program percent := by - intro holds - obtain ⟨_, _, _, ran, -⟩ := holds.returning (input := 100) (initial := empty) trivial (by decide) - have thrown : invoke Extracted.program 16 percent (percentContract.args 100) empty = - .error ⟨percent, 6, .thrown "System.ArgumentOutOfRangeException"⟩ := rfl - cases invoke_unique (by trivial) (by simp [Settled]) ran thrown +theorem correctness : percentContract.Refuted Extracted.program percent := + .of_fault (input := 100) (initial := empty) trivial (by decide) + (rfl : invoke Extracted.program 16 percent (percentContract.args 100) empty = + .error ⟨percent, 6, .thrown .argumentOutOfRange⟩) + (by simp [OutcomeContract.Violates, MethodContract.Violates, percentContract]) """; const string RefuteException = Empty + """ /-- This build throws `ArgumentException` for `101`; the contract names `ArgumentOutOfRangeException`. -/ -theorem correctness : ¬ percentContract.Holds Extracted.program percent := by - intro holds - obtain ⟨_, fault, ran, named⟩ := holds.raising (input := 101) (initial := empty) - (exception := "System.ArgumentOutOfRangeException") trivial (by decide) - have thrown : invoke Extracted.program 16 percent (percentContract.args 101) empty = - .error ⟨percent, 6, .thrown "System.ArgumentException"⟩ := rfl - cases invoke_unique (by simp [Settled, named]) (by simp [Settled]) ran thrown - simp at named +theorem correctness : percentContract.Refuted Extracted.program percent := + .of_fault (input := 101) (initial := empty) trivial (by decide) + (rfl : invoke Extracted.program 16 percent (percentContract.args 101) empty = + .error ⟨percent, 6, .thrown .argument⟩) + (by simp [OutcomeContract.Violates, percentContract]) """; foreach (var (name, assembly, refutation) in new[] { ("Threshold", threshold, RefuteThreshold), ("Exception", exception, RefuteException) }) diff --git a/Tests/OutcomeContract.lean b/Tests/OutcomeContract.lean index 9ec9c9e16..abea6c3cb 100644 --- a/Tests/OutcomeContract.lean +++ b/Tests/OutcomeContract.lean @@ -1,6 +1,7 @@ import CIL.MethodIdentity import CIL.StructuralLabels import CIL.Safety.OutcomeContract +import Tests.Support /-! A synthetic extraction with the bodies the C# fixture compiles to: `Percent(v) => v <= 100 ? v : throw new ArgumentOutOfRangeException(...)`, @@ -12,13 +13,16 @@ open CIL def method0Signature : String := "System.UInt64 Outcomes.Calls::Root(System.UInt64)" def method1Signature : String := "System.UInt64 Outcomes.Calls::Strict(System.UInt64)" def method2Signature : String := "System.UInt64 Outcomes.Calls::Percent(System.UInt64)" -@[cil_code] def method0 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .call 1 1, .const32 1, .convI8, .add, .ret] } -@[cil_code] def method1 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .call 2 1, .brzero 5, .arg 0, .ret, .nop, .nop, .throw "System.ArgumentException"] } +@[cil_code] def method0 : Method := { + returnsValue := true, locals := [], links := [(1, .direct 1)], code := + [.ldarg 0, .call (Tests.callee 1) {}, .ldc_i4 1, .conv_i8, .add, .ret] } +@[cil_code] def method1 : Method := { links := [(1, .direct 2)], returnsValue := true, + locals := [], code := + [.ldarg 0, .call (Tests.callee 1) {}, .brfalse 5, .ldarg 0, .ret, .nop, .nop, + .throw .argument] } @[cil_code] def method2 : Method := { returnsValue := true, locals := [], code := - [.arg 0, .const32 (BitVec.ofInt 32 100), .convI8, .bleu 7, .nop, .nop, - .throw "System.ArgumentOutOfRangeException", .arg 0, .ret] } + [.ldarg 0, .ldc_i4 (BitVec.ofInt 32 100), .conv_i8, .ble_un 7, .nop, .nop, + .throw .argumentOutOfRange, .ldarg 0, .ret] } @[cil_code] def program : Program := [method0, method1, method2] def signatures : List String := [method0Signature, method1Signature, method2Signature] @@ -40,11 +44,11 @@ def percentContract : OutcomeContract CIL.W64 CIL.W64 where pre _ _ := True footprint _ := [] post v _ _ out := out = v - raises v _ := if v ≤ 100 then none else some "System.ArgumentOutOfRangeException" + raises v _ := if v ≤ 100 then none else some .argumentOutOfRange -def strictRaises (v : CIL.W64) : Option String := - if v ≤ 100 then if v = 0 then some "System.ArgumentException" else none - else some "System.ArgumentOutOfRangeException" +def strictRaises (v : CIL.W64) : Option CIL.ExceptionType := + if v ≤ 100 then if v = 0 then some .argument else none + else some .argumentOutOfRange def strictContract : OutcomeContract CIL.W64 CIL.W64 where args v := [.scalar (.i64 v)] @@ -169,7 +173,7 @@ theorem root_formed (strictHolds : strictContract.Holds Extracted.program strict /-- The raising-after form: zero returns from `Percent` and then raises here. -/ example (percentHolds : percentContract.Holds Extracted.program percent) (m : Memory) (formed : m.WellFormed) : - Raises Extracted.program strict [.scalar (.i64 0)] m "System.ArgumentException" := by + Raises Extracted.program strict [.scalar (.i64 0)] m .argument := by cil_raise_steps [] cil_formed_outcome_call percentHolds 0 at percentSite [] case precondition => trivial @@ -184,18 +188,17 @@ example (percentHolds : percentContract.Holds Extracted.program percent) (m : Me /-! A raised outcome is exactly `.thrown`: no other fault satisfies it at any fuel. -/ -@[cil_code] def stuck : CIL.Method := { returnsValue := false, locals := [], code := [.unsupported "fixture"] } -@[cil_code] def spin : CIL.Method := { returnsValue := false, locals := [], code := [.branch 0] } +@[cil_code] def stuck : CIL.Method := + { returnsValue := false, locals := [], code := [.unadmitted 0] } +@[cil_code] def spin : CIL.Method := { returnsValue := false, locals := [], code := [.br 0] } -example (m : Memory) : ¬ Raises [stuck] 0 [] m "System.Exception" := by - rintro ⟨fuel, fault, ran, thrown⟩ - cases fuel with - | zero => - rw [show invoke [stuck] 0 0 [] m = .error ⟨0, 0, .fuelExhausted⟩ from rfl] at ran - cases ran; simp at thrown - | succ fuel => - rw [show invoke [stuck] (fuel + 1) 0 [] m = .error ⟨0, 0, .unsupported "Not a memory instruction"⟩ from rfl] at ran - cases ran; simp at thrown +/-- A hole gives an unsupported fault and leaves the method's exception behavior unknown. -/ +example (m : Memory) : + invoke [stuck] 1 0 [] m = .error ⟨0, 0, .unsupported "Opcode not admitted"⟩ ∧ + (∀ exception, ExecutionFault.unsupported "Opcode not admitted" ≠ .thrown exception) ∧ + ¬ Settled (.error ⟨0, 0, .unsupported "Opcode not admitted"⟩ : + Except LocatedFault (Memory × List Value)) := + ⟨rfl, fun _ => by simp, by decide⟩ theorem spin_exhausts (m : Memory) (frame : Frame) (fuel : Nat) : run [spin] fuel 0 0 [] frame [] m = .error ⟨0, 0, .fuelExhausted⟩ := by @@ -203,7 +206,7 @@ theorem spin_exhausts (m : Memory) (frame : Frame) (fuel : Nat) : | zero => rfl | succ fuel ih => exact (show run [spin] (fuel + 1) 0 0 [] frame [] m = run [spin] fuel 0 0 [] frame [] m from rfl).trans ih -example (m : Memory) : ¬ Raises [spin] 0 [] m "System.Exception" := by +example (m : Memory) : ¬ Raises [spin] 0 [] m .baseException := by rintro ⟨fuel, fault, ran, thrown⟩ change run [spin] fuel 0 0 [] _ [] m = _ at ran rw [spin_exhausts] at ran diff --git a/Tests/OutcomeEmbedding.lean b/Tests/OutcomeEmbedding.lean index bc46323fa..888a90e36 100644 --- a/Tests/OutcomeEmbedding.lean +++ b/Tests/OutcomeEmbedding.lean @@ -1,6 +1,7 @@ import CIL.Embedding.Tactic import CIL.Safety.Embedding.Outcomes import CIL.Safety.SymbolicExecution +import Tests.Support /-! Outcome contracts proved over literal bodies transfer to an extraction that inserts an excluded dead block in the throwing helper and renumbers its throw. -/ @@ -8,19 +9,21 @@ namespace Tests.OutcomeEmbedding open CIL CIL.Safety -def outOfRange : String := "System.ArgumentOutOfRangeException" +def outOfRange : ExceptionType := .argumentOutOfRange /-- `Outer(v) => Check(v)`: any exception comes from the helper. -/ -@[cil_code] def outer : Method := { returnsValue := true, locals := [], code := [.arg 0, .call 1 1, .ret] } +@[cil_code] def outer : Method := + { returnsValue := true, locals := [], code := [.ldarg 0, .call (Tests.callee 1) {}, .ret], + links := [(1, .direct 1)] } /-- `Check(v) => v > 100 ? throw new ArgumentOutOfRangeException() : v`, as the proof states it. -/ @[cil_code] def check : Method := { returnsValue := true, locals := [], code := - [.arg 0, .const32 100, .convI8, .bgtu 6, .arg 0, .ret, .throw outOfRange] } + [.ldarg 0, .ldc_i4 100, .conv_i8, .bgt_un 6, .ldarg 0, .ret, .throw outOfRange] } /-- `check` as the profile-specific extraction emits it: a dead block moves the throw to pc 7. -/ @[cil_code] def extractedCheck : Method := { returnsValue := true, locals := [], code := - [.arg 0, .const32 100, .convI8, .bgtu 7, .arg 0, .ret, .unsupported "Unreachable under selected profile", - .throw outOfRange] } + [.ldarg 0, .ldc_i4 100, .conv_i8, .bgt_un 7, .ldarg 0, .ret, + .unadmitted 0, .throw outOfRange] } @[cil_code] def literal : Program := [outer, check] @[cil_code] def extracted : Program := [outer, extractedCheck] @@ -29,7 +32,7 @@ theorem literal_set : literal = extracted.set 1 check := rfl theorem check_embeds : MethodEmbeds extracted 1 check := by cil_embed -def raisesAbove (v : W64) : Option String := if v ≤ 100 then none else some outOfRange +def raisesAbove (v : W64) : Option ExceptionType := if v ≤ 100 then none else some outOfRange def checkContract : OutcomeContract W64 W64 where args v := [.scalar (.i64 v)] @@ -69,7 +72,7 @@ theorem check_in_range : checkInRange.Holds literal 1 := by -- The method contract transfers to the extraction without reading `extractedCheck`. theorem extracted_in_range : checkInRange.Holds extracted 1 := check_embeds.method_holds (literal_set ▸ check_in_range) -theorem outer_call : outer.code[1]? = some (.call 1 1) := rfl +theorem outer_call : outer.code[1]? = some (.call (Tests.callee 1)) := rfl /-- `Outer` propagates the helper's exception and returns its result, read off the helper's contract. -/ theorem outer_holds (checkHolds : checkContract.Holds literal 1) : outerContract.Holds literal 0 := by @@ -139,15 +142,16 @@ example : (⟨1, 6, .thrown outOfRange⟩ : LocatedFault).relocate /-! A changed reachable throw condition or exception type neither embeds nor satisfies the contract. -/ -def renamedException : Method := { extractedCheck with code := extractedCheck.code.set 7 (.throw "System.ArgumentException") } -def lowerBound : Method := { extractedCheck with code := extractedCheck.code.set 1 (.const32 50) } +def renamedException : Method := + { extractedCheck with code := extractedCheck.code.set 7 (.throw .argument) } +def lowerBound : Method := { extractedCheck with code := extractedCheck.code.set 1 (.ldc_i4 50) } /-- error: cil_embed: the expected body does not embed into the extracted method method index 1 expected pc 6 maps to actual pc 7 - expected: CIL.Op.throw "System.ArgumentOutOfRangeException" - actual: CIL.Op.throw "System.ArgumentException" + expected: CIL.Instruction.throw (CIL.ExceptionType.argumentOutOfRange) + actual: CIL.Instruction.throw (CIL.ExceptionType.argument) the instructions differ -/ #guard_msgs (substring := true) in @@ -157,8 +161,8 @@ example : MethodEmbeds [outer, renamedException] 1 check := by cil_embed error: cil_embed: the expected body does not embed into the extracted method method index 1 expected pc 1 maps to actual pc 1 - expected: CIL.Op.const32 0x00000064#32 - actual: CIL.Op.const32 0x00000032#32 + expected: CIL.Instruction.ldc_i4 0x00000064#32 + actual: CIL.Instruction.ldc_i4 0x00000032#32 the instructions differ -/ #guard_msgs (substring := true) in @@ -170,8 +174,8 @@ example : ¬ checkContract.Holds [outer, renamedException] 1 := by obtain ⟨_, fault, ran, named⟩ := holds.raising (input := 101) (initial := empty) (exception := outOfRange) trivial (by decide) have thrown : Safety.invoke [outer, renamedException] 10 1 (checkContract.args 101) empty = - .error ⟨1, 7, .thrown "System.ArgumentException"⟩ := rfl - cases invoke_unique (by simp [Settled, named]) (by simp [Settled]) ran thrown + .error ⟨1, 7, .thrown .argument⟩ := rfl + cases invoke_unique (by simp [Stable, named]) (by simp [Stable]) ran thrown simp [outOfRange] at named /-- The lowered bound throws for 60, which the contract says returns. -/ @@ -180,29 +184,27 @@ example : ¬ checkContract.Holds [outer, lowerBound] 1 := by obtain ⟨_, _, _, ran, -⟩ := holds.returning (input := 60) (initial := empty) trivial (by decide) have thrown : Safety.invoke [outer, lowerBound] 10 1 (checkContract.args 60) empty = .error ⟨1, 7, .thrown outOfRange⟩ := rfl - cases invoke_unique (by trivial) (by simp [Settled]) ran thrown + cases invoke_unique (by trivial) (by simp [Stable]) ran thrown /-! Only named throws are transported: fuel exhaustion and unsupported execution are not exceptions. -/ -/-- A literal body that leaves the throw unsupported still embeds: dead code constrains nothing. -/ -def unsupportedThrow : Method := { check with code := check.code.set 6 (.unsupported "fixture") } +/-- A literal body that leaves a hole at the throw still embeds: dead code constrains nothing. -/ +def holeThrow : Method := { check with code := check.code.set 6 (.unadmitted 0x7A) } -theorem unsupported_embeds : MethodEmbeds extracted 1 unsupportedThrow := by cil_embed +theorem hole_embeds : MethodEmbeds extracted 1 holeThrow := by cil_embed -theorem unsupported_faults : - Safety.invoke (extracted.set 1 unsupportedThrow) 10 1 [.scalar (.i64 101)] empty = - .error ⟨1, 6, .unsupported "Not a memory instruction"⟩ := rfl +theorem hole_faults : + Safety.invoke (extracted.set 1 holeThrow) 10 1 [.scalar (.i64 101)] empty = + .error ⟨1, 6, .unsupported "Opcode not admitted"⟩ := rfl -example : ¬ Transported (.error ⟨1, 6, .unsupported "Not a memory instruction"⟩ : +example : ¬ Transported (.error ⟨1, 6, .unsupported "Opcode not admitted"⟩ : Except LocatedFault (Safety.Memory × List Safety.Value)) := by rintro ⟨_, thrown⟩ cases thrown -/-- The extraction raises, but the literal body proves nothing about it: it never raises. -/ -example : ¬ Raises (extracted.set 1 unsupportedThrow) 1 [.scalar (.i64 101)] empty outOfRange := by - rintro ⟨_, fault, ran, thrown⟩ - cases invoke_unique (by simp [Settled, thrown]) (by simp [Settled]) ran unsupported_faults - simp at thrown +/-- The hole gives an unsupported model outcome, which cannot witness a contract refutation. -/ +example : ¬ Settled (.error ⟨1, 6, .unsupported "Opcode not admitted"⟩ : + Except LocatedFault (Safety.Memory × List Safety.Value)) := by decide theorem literal_exhausted : Safety.invoke literal 0 1 [.scalar (.i64 101)] empty = .error ⟨1, 0, .fuelExhausted⟩ := rfl diff --git a/Tests/OutlineStops.cs b/Tests/OutlineStops.cs new file mode 100644 index 000000000..e4a02a293 --- /dev/null +++ b/Tests/OutlineStops.cs @@ -0,0 +1,313 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:package Mono.Cecil +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs + +using Mono.Cecil; +using Mono.Cecil.Cil; +using MethodAttributes = Mono.Cecil.MethodAttributes; + +// Each case edits a method of the examples so that its outline must stop, and checks the stop names the +// instruction it stopped at instead of failing the generator or outlining a value it does not model. +var source = At("Examples/Assembly/bin/Examples.dll"); +var root = At("artifacts/OutlineStops"); +Directory.CreateDirectory(root); + +(int Code, string Output, string Proof) Outline(string name, string selector, Action edit) +{ + var directory = Path.Combine(root, name); + Directory.CreateDirectory(directory); + var dll = Path.Combine(directory, "Examples.dll"); + using (var module = ModuleDefinition.ReadModule(source)) + { + edit(module); + module.Write(dll); + } + var extraction = Path.Combine(directory, "Extraction"); + Run(["dotnet", ExtractorPath, "extract", dll, selector, extraction]); + var proof = Path.Combine(directory, "Proof.lean"); + File.Delete(proof); + var outline = Execute(["dotnet", ExtractorPath, "new-proof", extraction, proof]); + return (outline.Code, outline.Output, File.Exists(proof) ? File.ReadAllText(proof) : ""); +} + +MethodDefinition Added(ModuleDefinition module, string name, TypeReference result, params TypeReference[] parameters) +{ + var method = new MethodDefinition(name, MethodAttributes.Public | MethodAttributes.Static, result); + foreach (var parameter in parameters) method.Parameters.Add(new ParameterDefinition(parameter)); + module.GetType("Examples.NativeIntegers").Methods.Add(method); + return method; +} + +// Loop callee selection excludes narrow result types even when the helper always throws. +var throwOnly = Outline("NarrowThrowOnly", "Loops::CountUntil(uint,uint)", module => +{ + var helper = module.GetType("Examples.Loops").Methods.Single(method => method.Name == "Bump"); + var constructor = (MethodReference)helper.Body.Instructions.Single(instruction => + instruction.OpCode == OpCodes.Newobj).Operand; + helper.ReturnType = module.TypeSystem.Byte; + helper.Body.Instructions.Clear(); + helper.Body.Variables.Clear(); + helper.Body.Instructions.Add(Instruction.Create(OpCodes.Newobj, constructor)); + helper.Body.Instructions.Add(Instruction.Create(OpCodes.Throw)); +}); +if (throwOnly.Code == 0 || !throwOnly.Output.Contains("callee Bump returns System.Byte", StringComparison.Ordinal)) + throw new InvalidOperationException("A narrow throw-only helper bypassed callee result selection:\n" + + throwOnly.Output + throwOnly.Proof); +Console.WriteLine("PASS narrow throw-only helper retains its callee selection diagnostic"); +foreach (var (owner, methodName, selector) in new[] +{ + ("Arithmetic", "Larger", "Arithmetic::Larger(int,int)"), + ("PrimitiveArrays", "ReadInt32", "PrimitiveArrays::ReadInt32(int[],int)"), + ("Loops", "Put", "Loops::Put(System.Span`1,uint)"), + ("Counter", "Read", "Counter::Read()"), + ("OutlineCountdown", "Drain", "OutlineCountdown::Drain()") +}) +{ + var positive = Outline("WideReturn" + owner, selector, _ => { }); + if (positive.Code != 0 || positive.Proof.Contains("-- not outlined:", StringComparison.Ordinal)) + throw new InvalidOperationException("Wide return control failed: " + selector + "\n" + positive.Output); + var narrow = Outline("NarrowReturn" + owner, selector, module => + { + var method = module.GetType("Examples." + owner).Methods.Single(method => method.Name == methodName); + if (method.ReturnType.MetadataType == MetadataType.Void) + { + var processor = method.Body.GetILProcessor(); + foreach (var returned in method.Body.Instructions.Where(instruction => + instruction.OpCode == OpCodes.Ret).ToArray()) + { + returned.OpCode = OpCodes.Ldc_I4_0; + processor.InsertAfter(returned, Instruction.Create(OpCodes.Ret)); + } + } + method.ReturnType = module.TypeSystem.Byte; + }); + if (narrow.Code == 0 && !narrow.Proof.Contains("-- not outlined:", StringComparison.Ordinal)) + throw new InvalidOperationException("Narrow return was outlined: " + selector + "\n" + narrow.Proof); + Console.WriteLine("PASS declared narrow return outline rejection and wide control: " + owner); +} +if (args is ["--return-only"]) return; + +// A parameter of a narrow declared type is loaded narrowed by its step, which no outline reader models, so each +// reader refuses it and names it; the method before the retyping is the wide control. +var unnamed = new List(); +foreach (var (owner, methodName, parameter, selector, narrowSelector, prepare) in + new (string, string, int, string, string, Action?)[] +{ + ("Arithmetic", "Larger", 0, "Arithmetic::Larger(int,int)", "Arithmetic::Larger(ushort,int)", null), + ("PrimitiveArrays", "ReadInt32", 1, "PrimitiveArrays::ReadInt32(int[],int)", + "PrimitiveArrays::ReadInt32(int[],ushort)", null), + ("Loops", "Put", 1, "Loops::Put(System.Span`1,uint)", + "Loops::Put(System.Span`1,ushort)", null), + ("Loops", "CountUntil", 1, "Loops::CountUntil(uint,uint)", "Loops::CountUntil(uint,ushort)", null), + ("Objects", "SetLimit", 1, "Objects::SetLimit(Examples.Account,uint)", + "Objects::SetLimit(Examples.Account,ushort)", null), + ("NarrowTotals", "WriteRead", 0, "NarrowTotals::WriteRead(uint)", "NarrowTotals::WriteRead(ushort)", null), + ("OutlineCountdown", "Drain", 0, "OutlineCountdown::Drain(ulong)", "OutlineCountdown::Drain(ushort)", + CountBy) +}) +{ + MethodDefinition Target(ModuleDefinition module) + { + var method = module.GetType("Examples." + owner).Methods.Single(method => method.Name == methodName); + prepare?.Invoke(method); + return method; + } + var wide = Outline("WideArgument" + owner + methodName, selector, module => Target(module)); + if (wide.Code != 0 || wide.Proof.Contains("-- not outlined:", StringComparison.Ordinal)) + throw new InvalidOperationException("Wide argument control failed: " + selector + "\n" + wide.Output); + var name = ""; + var narrow = Outline("NarrowArgument" + owner + methodName, narrowSelector, module => + { + var retyped = Target(module).Parameters[parameter]; + retyped.ParameterType = module.TypeSystem.UInt16; + name = retyped.Name; + }); + var refused = narrow.Code != 0 || narrow.Proof.Contains("-- not outlined:", StringComparison.Ordinal); + if (!refused || !(narrow.Output + narrow.Proof).Contains($"`{name}`", StringComparison.Ordinal)) + unnamed.Add( + $"Narrow argument `{name}` was outlined or not named: {narrowSelector}\n{narrow.Output}{narrow.Proof}"); + else Console.WriteLine($"PASS declared narrow argument outline rejection and wide control: {owner}::{methodName}"); +} +if (unnamed.Count > 0) throw new InvalidOperationException(string.Join("\n", unnamed)); + +// The receiver loop adds its one word argument to the count instead of one, so the loop body reads an argument. +static void CountBy(MethodDefinition drain) +{ + drain.Parameters.Add(new ParameterDefinition("step", Mono.Cecil.ParameterAttributes.None, + drain.Module.TypeSystem.UInt64)); + var one = drain.Body.Instructions.Where(instruction => instruction.OpCode == OpCodes.Ldc_I4_1).ElementAt(1); + one.OpCode = OpCodes.Ldarg_1; + one.Next.OpCode = OpCodes.Conv_U8; +} + +// A conditional branch with mixed native/int32 operands, in a method over a class instance, is a hole that names +// the mixed-width branch. +var wrapped = Outline("WrappedObject", + "System.UInt64 Examples.NativeIntegers::Wrapped(Examples.Account,System.Int32[])", module => + { + var account = module.GetType("Examples.Account"); + var method = Added(module, "Wrapped", module.TypeSystem.UInt64, account, + new ArrayType(module.TypeSystem.Int32)); + var code = method.Body.Instructions; + var zero = Instruction.Create(OpCodes.Ldc_I4_0); + code.Add(Instruction.Create(OpCodes.Ldarg_1)); + code.Add(Instruction.Create(OpCodes.Ldlen)); + code.Add(Instruction.Create(OpCodes.Ldc_I4_5)); + code.Add(Instruction.Create(OpCodes.Blt, zero)); + code.Add(Instruction.Create(OpCodes.Ldarg_0)); + code.Add(Instruction.Create(OpCodes.Ldfld, account.Fields.Single(field => field.Name == "Balance"))); + code.Add(Instruction.Create(OpCodes.Ret)); + code.Add(zero); + code.Add(Instruction.Create(OpCodes.Conv_U8)); + code.Add(Instruction.Create(OpCodes.Ret)); + }); +if (wrapped.Code != 0 || !wrapped.Proof.Contains("not outlined: ", StringComparison.Ordinal) || + !wrapped.Proof.Contains("`IL_0003: blt IL_000f`", StringComparison.Ordinal)) + throw new Exception("A mixed native branch did not leave a hole naming it in the object outline:\n" + + wrapped.Output + wrapped.Proof); +Console.WriteLine("PASS a mixed native and int32 branch is a hole that names it in the object outline"); + +// Mixed tests retain explicit diagnostics at both a loop latch and an internal branch. +foreach (var atLatch in new[] { true, false }) +{ + var mixed = Outline(atLatch ? "MixedLatch" : "MixedInternal", + "System.Int32 Examples.NativeIntegers::MixedLoop(System.Int32)", module => + { + var method = Added(module, "MixedLoop", module.TypeSystem.Int32, module.TypeSystem.Int32); + method.Body.Variables.Add(new VariableDefinition(module.TypeSystem.Int32)); + var code = method.Body.Instructions; + var head = Instruction.Create(OpCodes.Ldc_I4_0); + var increment = Instruction.Create(OpCodes.Ldloc_0); + code.Add(Instruction.Create(OpCodes.Ldc_I4_0)); + code.Add(Instruction.Create(OpCodes.Stloc_0)); + code.Add(head); + code.Add(Instruction.Create(OpCodes.Conv_I)); + code.Add(Instruction.Create(OpCodes.Ldarg_0)); + if (atLatch) + { + code.Add(Instruction.Create(OpCodes.Blt, head)); + } + else + { + code.Add(Instruction.Create(OpCodes.Beq, increment)); + code.Add(Instruction.Create(OpCodes.Nop)); + code.Add(increment); + code.Add(Instruction.Create(OpCodes.Ldc_I4_1)); + code.Add(Instruction.Create(OpCodes.Add)); + code.Add(Instruction.Create(OpCodes.Stloc_0)); + code.Add(Instruction.Create(OpCodes.Ldloc_0)); + code.Add(Instruction.Create(OpCodes.Ldarg_0)); + code.Add(Instruction.Create(OpCodes.Blt, head)); + } + code.Add(Instruction.Create(OpCodes.Ldloc_0)); + code.Add(Instruction.Create(OpCodes.Ret)); + }); + if (atLatch ? mixed.Code == 0 || !mixed.Output.Contains("at its back edge", StringComparison.Ordinal) + : mixed.Code != 0 || !mixed.Proof.Contains("not outlined:", StringComparison.Ordinal)) + throw new Exception("Mixed test lacks an outline diagnostic:\n" + mixed.Output + mixed.Proof); + if (!(mixed.Output + mixed.Proof).Contains("operands of different widths", StringComparison.Ordinal)) + throw new Exception("Mixed test stopped before inspecting its operands:\n" + mixed.Output + mixed.Proof); + Console.WriteLine("PASS mixed-width outline diagnostic: " + (atLatch ? "latch" : "internal branch")); +} + +// The element a span read loads must be as wide as the span's elements. +string readInstruction = ""; +var read = Outline("WrongWidth", + "System.Boolean Examples.Loops::AllZero(System.ReadOnlySpan`1)", module => +{ + var method = module.GetType("Examples.Loops").Methods.Single(candidate => candidate.Name == "AllZero"); + var load = method.Body.Instructions.Single(instruction => instruction.OpCode == OpCodes.Ldind_U4); + load.OpCode = OpCodes.Ldind_I8; + readInstruction = $"not outlined: `{load}`"; +}); +if (read.Code != 0 || !read.Proof.Contains(readInstruction, StringComparison.Ordinal)) + throw new Exception("A read wider than the span's elements was outlined as a read of its element:\n" + + read.Output + read.Proof); +Console.WriteLine("PASS a span read of another width than its elements is a hole"); + +// A long operand with a line break must stay inside the generated comment. +var literal = Outline("Literal", "System.Int32 Examples.NativeIntegers::Literal()", module => +{ + var code = Added(module, "Literal", module.TypeSystem.Int32).Body.Instructions; + var operand = $"""first{Environment.NewLine}{new string('x', 150)}last"""; + code.Add(Instruction.Create(OpCodes.Ldstr, operand)); + code.Add(Instruction.Create(OpCodes.Pop)); + code.Add(Instruction.Create(OpCodes.Ldc_I4_0)); + code.Add(Instruction.Create(OpCodes.Ret)); +}); +var literalLines = literal.Proof.Split('\n'); +if (literal.Code != 0 || !literal.Proof.Contains("IL_0000: ldstr", StringComparison.Ordinal) || + !literal.Proof.Contains(@"first\n", StringComparison.Ordinal) || + !literalLines.Any(line => line.Contains("last", StringComparison.Ordinal)) || + literalLines.Where(line => line.Contains("xxxx", StringComparison.Ordinal)) + .Any(line => !line.TrimStart().StartsWith("--", StringComparison.Ordinal) || line.TrimEnd().Length > 100)) + throw new Exception($"Expected the raw string instruction in wrapped comments.\n{literal.Output}{literal.Proof}"); + +// A local of a narrow integer type stays the author's in every outline family, which reads it from the local's +// type: the straight-line outline does not step its store, the loop outline refuses its kind and the object +// outline refuses a numeric frame that holds it. +MethodDefinition NarrowLocal(ModuleDefinition module, string name, TypeReference parameter) +{ + var method = Added(module, name, module.TypeSystem.Int32, parameter); + method.Body.Variables.Add(new VariableDefinition(module.TypeSystem.Byte)); + method.Body.InitLocals = true; + return method; +} + +var straight = Outline("NarrowStraight", "System.Int32 Examples.NativeIntegers::NarrowStraight(System.Int32)", + module => +{ + var code = NarrowLocal(module, "NarrowStraight", module.TypeSystem.Int32).Body.Instructions; + code.Add(Instruction.Create(OpCodes.Ldarg_0)); + code.Add(Instruction.Create(OpCodes.Stloc_0)); + code.Add(Instruction.Create(OpCodes.Ldloc_0)); + code.Add(Instruction.Create(OpCodes.Ret)); +}); +if (straight.Code != 0 || !straight.Proof.Contains("not outlined: `IL_0001: stloc.0`", StringComparison.Ordinal)) + throw new Exception("A store to a byte local was stepped by the straight-line outline:\n" + + straight.Output + straight.Proof); +Console.WriteLine("PASS a store to a byte local is a hole in the straight-line outline"); + +var looped = Outline("NarrowLoop", "System.Int32 Examples.NativeIntegers::NarrowLoop(System.Int32)", module => +{ + var code = NarrowLocal(module, "NarrowLoop", module.TypeSystem.Int32).Body.Instructions; + var head = Instruction.Create(OpCodes.Ldloc_0); + code.Add(Instruction.Create(OpCodes.Ldc_I4_0)); + code.Add(Instruction.Create(OpCodes.Stloc_0)); + code.Add(head); + code.Add(Instruction.Create(OpCodes.Ldc_I4_1)); + code.Add(Instruction.Create(OpCodes.Add)); + code.Add(Instruction.Create(OpCodes.Stloc_0)); + code.Add(Instruction.Create(OpCodes.Ldloc_0)); + code.Add(Instruction.Create(OpCodes.Ldarg_0)); + code.Add(Instruction.Create(OpCodes.Blt, head)); + code.Add(Instruction.Create(OpCodes.Ldloc_0)); + code.Add(Instruction.Create(OpCodes.Ret)); +}); +if (looped.Code == 0 || + !looped.Output.Contains("local `local0` has kind .narrow .unsigned8", StringComparison.Ordinal)) + throw new Exception("A loop over a byte local was not refused by its kind:\n" + looped.Output); +Console.WriteLine("PASS a loop over a byte local is refused by its kind in the loop outline"); + +var framed = Outline("NarrowObject", "System.Int32 Examples.NativeIntegers::NarrowObject(Examples.Account)", module => +{ + var account = module.GetType("Examples.Account"); + var code = NarrowLocal(module, "NarrowObject", account).Body.Instructions; + code.Add(Instruction.Create(OpCodes.Ldarg_0)); + code.Add(Instruction.Create(OpCodes.Ldfld, account.Fields.Single(field => field.Name == "Balance"))); + code.Add(Instruction.Create(OpCodes.Conv_U1)); + code.Add(Instruction.Create(OpCodes.Stloc_0)); + code.Add(Instruction.Create(OpCodes.Ldloc_0)); + code.Add(Instruction.Create(OpCodes.Ret)); +}); +if (framed.Code != 0 || + !framed.Proof.Contains("a class frame with mixed numeric homes and unsupported local", StringComparison.Ordinal)) + throw new Exception("A byte local beside an object argument was stepped by the object outline:\n" + + framed.Output + framed.Proof); +Console.WriteLine("PASS a byte local in a method over an object is a hole in the object outline"); diff --git a/Tests/ParameterEvidence.cs b/Tests/ParameterEvidence.cs index 7bf5c2d39..72d74378f 100644 --- a/Tests/ParameterEvidence.cs +++ b/Tests/ParameterEvidence.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include RejectedEvidence.cs #:include GeneratedEvidence.cs @@ -16,11 +21,13 @@ RequireLeanFixtures("CIL.Extraction.Translation", "CIL.SymbolicExecution", "CIL.Safety.Execution"); RequireLeanFixtures("Tests.ExtractionParameters"); var assembly = At("Examples/Assembly/bin/Examples.dll"); +const string instanceParameter = "Counter::Parameter(byte)"; +var instanceControl = Path.Combine(output, "StaticFieldLoad"); var samples = new[] { "Arguments::Overwrite(uint,uint)", "Arguments::Alias(uint)", "Arguments::Address(uint)", "Arguments::Native(nint)", "Arguments::Narrow(byte)", - "Arguments::ReassignReference(uint&,uint&)", "Arguments::Struct(Examples.Packet)", "Counter::Parameter(byte)" + "Arguments::ReassignReference(uint&,uint&)", "Arguments::Struct(Examples.Packet)", instanceParameter }; ExtractBatch("extract", "Examples", samples.Select((selector, index) => new GeneratedExtraction(assembly, selector, Path.Combine(output, index.ToString())))); @@ -46,10 +53,19 @@ else if (instruction.OpCode.Code == Code.Starg_S) instruction.OpCode = OpCodes.Starg; else if (instruction.OpCode.Code == Code.Ldarga_S) instruction.OpCode = OpCodes.Ldarga; } + // The decoded instance method homes argument 1 after the platform field load. + var counter = module.GetType("Examples.Counter").Methods.Single(method => method.Name == "Parameter"); + var processor = counter.Body.GetILProcessor(); + var entry = counter.Body.Instructions[0]; + var endian = module.GetType("Examples.Capabilities").Methods.Single(method => method.Name == "Endian"); + var load = endian.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Ldsfld); + processor.InsertBefore(entry, processor.Create(OpCodes.Ldsfld, (FieldReference)load.Operand)); + processor.InsertBefore(entry, processor.Create(OpCodes.Pop)); var longAssembly = Path.Combine(output, "LongArguments.dll"); module.Write(longAssembly); ExtractBatch("extract", "Examples", samples.Take(3).Select((selector, index) => - new GeneratedExtraction(longAssembly, selector, Path.Combine(output, "Long" + index)))); + new GeneratedExtraction(longAssembly, selector, Path.Combine(output, "Long" + index))) + .Append(new GeneratedExtraction(longAssembly, instanceParameter, instanceControl))); foreach (var selector in samples.Take(3)) { var folder = Path.Combine(output, "Long" + Array.IndexOf(samples, selector)); @@ -57,21 +73,34 @@ } } +evidence.Add(Path.Combine(instanceControl, "Extracted.lean"), "PASS decoded instance parameter after a field load"); evidence.Check(); -var baseline = File.ReadAllText(Path.Combine(output, "0", "Extracted.lean")); -void Reject(string name, string mutated) +var decoded = File.ReadAllText(Path.Combine(output, "0", "Extracted.lean")); +var baseline = File.ReadAllText(Path.Combine(instanceControl, "Extracted.lean")); +if (!IsDecodedMethod(decoded, 0) || !IsDecodedMethod(baseline, 0)) + throw new InvalidOperationException("Both parameter controls must decode."); + +const string obligation = "method0_parameters_validated"; +void Reject(string name, string source, string before, string after) { - if (mutated == baseline) throw new InvalidOperationException("Mutation did not change generated source: " + name); + var control = NarrowEvidence(source, obligation); + var mutated = control.Replace(before, after, StringComparison.Ordinal); + if (mutated == control) + throw new InvalidOperationException($"Missing parameter obligation for {name}"); var path = Path.Combine(output, name + ".lean"); File.WriteAllText(path, mutated); - rejected.Add(path, "PASS parameter evidence rejects " + name, "decide"); + rejected.Add(path, $"PASS parameter evidence rejects {name}", "ParameterHomesValid"); } -// Mutate lookup lemmas along with code so their rfl proofs cannot cause the rejection. -Reject("omitted-home", baseline.Replace("[(0, .word32)]", "[]")); -Reject("wrong-slot", baseline.Replace(".setLocal 0", ".setLocal 1").Replace(".local 0", ".local 1")); -Reject("omitted-redirection", baseline.Replace(".local 0", ".arg 0")); -Reject("wrong-home-type", baseline.Replace("[(0, .word32)]", "[(0, .word64)]")); -Reject("omitted-raw-site", baseline.Replace(".load 1, .store 0, .load 0, .other", ".load 1, .other, .load 0, .other")); +// Argument 1 is the homed byte and argument 0 is the receiver. +void RejectMethod(string name, string update) => + Reject(name, baseline, "method0Reachable method0 :=", $$"""method0Reachable { method0 with {{update}} } :="""); +RejectMethod("omitted-home", "parameterHomes := []"); +RejectMethod("wrong-home-type", "parameterHomes := [(1, .word32)]"); +RejectMethod("wrong-argument", "code := method0.code.set 6 (.starg 0)"); +RejectMethod("home-slot-store", "code := method0.code.set 6 (.stloc 0)"); +RejectMethod("home-slot-load", "code := method0.code.set 2 (.ldloc 0)"); +Reject("omitted-raw-site", decoded, "computedLayouts method0RawArguments method0Reachable", + "computedLayouts { method0RawArguments with code := method0RawArguments.code.set 1 .other } method0Reachable"); rejected.Check(); Console.WriteLine("PASS complete parameter-home mapping, argument types, instance indices and long/short encodings"); diff --git a/Tests/PinnedIndexScaling.lean b/Tests/PinnedIndexScaling.lean new file mode 100644 index 000000000..f1d5a951d --- /dev/null +++ b/Tests/PinnedIndexScaling.lean @@ -0,0 +1,149 @@ +import CIL.Extraction.BodyDecoding +import Lean + +/-! Package pin construction and independent method checks have separate cost bounds. -/ + +namespace Tests.PinnedIndexScaling +open CIL.Extraction +set_option maxHeartbeats 0 +set_option maxRecDepth 32768 + +/-- Fixed-width identities keep total pin bytes proportional to the pin count. -/ +private def identities (count : Nat) : List (List Nat) := + (List.range count).map fun position => [65, position / 256, position % 256, 44] + +/-- One checked package index shared by every small-package method. -/ +private def small : PinnedIndex := cil_pin_index identities 128 +#print axioms small + +/-- One checked package index shared by every medium-package method. -/ +private def medium : PinnedIndex := cil_pin_index identities 512 +#print axioms medium + +/-- One checked package index shared by every large-package method. -/ +private def large : PinnedIndex := cil_pin_index identities 2048 +#print axioms large + +/-- Each independent method has fixed metadata and queries the package's checked pin index. -/ +private def methodCheck (pinned : PinnedIndex) (position : Nat) : Bool := + let metadata : MethodMetadata := { own := { corlib := position } } + let tables : IndexTables := + { rows := .leaf, strings := .leaf, blobs := .leaf, + userStrings := .leaf, links := .leaf, instanceBefore := .leaf, instanceTotal := .leaf, + genericOwners := .leaf } + match metadata.environmentWith [[88]] [tables] pinned {} with + | .error _ => false + | .ok env => + env.pinned.matches [65, 0, 0, 44, 99] && env.exactPins.contains [65, 0, 0, 44] && + !env.exactPins.contains [65, 0, 0, 44, 99] + +/-- Each batch repeats complete package construction and independent method checks. -/ +private def runtimeCost (pinCount methodCount repetitions : Nat) : IO (Nat × Nat) := do + let start ← IO.monoNanosNow + let startHeartbeats ← IO.getNumHeartbeats + for _ in [:repetitions] do + let pinned := PinnedIndex.ofIdentities (identities pinCount) + unless (List.range methodCount).all (methodCheck pinned) do + throw (IO.userError "a package pin disappeared") + let heartbeats := (← IO.getNumHeartbeats) - startHeartbeats + return (((← IO.monoNanosNow) - start) / repetitions, heartbeats / repetitions) + +#eval do + let sizes := #[128, 512, 2048] + let combinations := sizes.flatMap fun pins => sizes.map fun methods => (pins, methods) + let repetitions := 20 + let rounds := 9 + for (pins, methods) in combinations do + let _ ← runtimeCost pins methods repetitions + let mut samples : Array (Array Nat × Array Nat) := combinations.map fun _ => (#[], #[]) + for round in [:rounds] do + let indices := Array.range combinations.size + for index in (if round % 2 == 0 then indices else indices.reverse) do + let (pins, methods) := combinations[index]! + let (elapsed, heartbeats) ← runtimeCost pins methods repetitions + let (times, allocations) := samples[index]! + samples := samples.set! index (times.push elapsed, allocations.push heartbeats) + let mut previousHeartbeats := 0 + for index in [:combinations.size] do + let (pinCount, methodCount) := combinations[index]! + let (times, allocations) := samples[index]! + let median := (times.qsort (· < ·))[rounds / 2]! + let heartbeats := (allocations.qsort (· < ·))[rounds / 2]! + IO.println s!"pin runtime: {pinCount} pins, {methodCount} methods, {median} ns" + -- Runtime heartbeats count allocation work, independently of scheduler delays. + IO.println (s!"pin allocation work: {pinCount} pins, {methodCount} methods, " ++ + s!"{heartbeats} heartbeats") + if pinCount == methodCount then + if previousHeartbeats != 0 && heartbeats > previousHeartbeats * 6 then + throw (IO.userError "package pin allocation work grew faster than package size") + previousHeartbeats := heartbeats + +open Lean Meta Elab Command in +set_option maxHeartbeats 0 in +run_cmd do + let mut previousQuotation := 0 + let mut previousConstruction := 0 + let mut previousTotal := 0 + for (pinCount, indexName) in ([(128, ``small), (512, ``medium), + (2048, ``large)] : List (Nat × Name)) do + let start ← IO.getNumHeartbeats + let _ ← liftTermElabM do + let expressionSyntax ← `(cil_pin_index identities $(quote pinCount)) + Term.elabTerm expressionSyntax (some (mkConst ``PinnedIndex)) + let quotation := (← IO.getNumHeartbeats) - start + logInfo m!"pin quotation: {pinCount} pins, {quotation} heartbeats" + if previousQuotation != 0 && quotation > previousQuotation * 6 then + throwError "pin quotation grew faster than package size" + previousQuotation := quotation + let index := (← getConstInfo indexName).value! + let start ← IO.getNumHeartbeats + match Kernel.check (← getEnv) {} + (Expr.letE `checked (mkConst ``PinnedIndex) index (.bvar 0) false) with + | .error error => throwKernelException error + | .ok _ => pure () + let construction := (← IO.getNumHeartbeats) - start + logInfo m!"pin construction: {pinCount} pins, {construction} heartbeats" + if previousConstruction != 0 && construction > previousConstruction * 6 then + throwError "pin construction grew faster than indexed construction" + previousConstruction := construction + let mut previousMethods := 0 + for methodCount in [128, 512, 2048] do + let mut cost := 0 + for position in [:methodCount] do + let (statement, proof) ← liftTermElabM do + let statement ← mkEq + (mkApp2 (mkConst ``methodCheck) (mkConst indexName) (mkNatLit position)) + (mkConst ``Bool.true) + pure (statement, ← mkEqRefl (mkConst ``Bool.true)) + let start ← IO.getNumHeartbeats + match Kernel.check (← getEnv) {} + (Expr.letE `checked statement proof (.bvar 0) false) with + | .error error => throwKernelException error + | .ok _ => pure () + cost := cost + ((← IO.getNumHeartbeats) - start) + logInfo m!"pin methods: {pinCount} pins, {methodCount} methods, {cost} heartbeats" + if previousMethods != 0 && cost > previousMethods * 6 then + throwError "independent method pin checks grew faster than method count" + previousMethods := cost + if methodCount == pinCount then + let total := construction + cost + if previousTotal != 0 && total > previousTotal * 6 then + throwError "package pin checks grew faster than package size" + previousTotal := total + +/-- Materialized indexes retain every complete identity's prefix. -/ +theorem materialized_pin_present : small.prefixes.matches [65, 0, 0, 44, 99] = true := by + decide +kernel +#print axioms materialized_pin_present + +/-- An empty trie cannot certify a package containing a nonempty pin. -/ +theorem omitted_prefix_rejected : + ({} : AssemblyPrefixes) ≠ ([([65] : List Nat)].map assemblyStem).foldl + AssemblyPrefixes.insert {} := by + intro same + have query := congrArg (fun trie => trie.matches [65]) same + have impossible : false = true := query + cases impossible +#print axioms omitted_prefix_rejected + +end Tests.PinnedIndexScaling diff --git a/Tests/PinnedMetadata.cs b/Tests/PinnedMetadata.cs new file mode 100644 index 000000000..e202144a8 --- /dev/null +++ b/Tests/PinnedMetadata.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Security.Cryptography; +using Mono.Cecil; + +/// A reflection fixture whose Cecil module and identity rows share one pinned image. +sealed class PinnedMetadata : IDisposable +{ + readonly IDisposable pinned; + + internal PinnedMetadata(Assembly extractor, string path) + { + const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + var type = extractor.GetType("Nethermind.LeanCIL.PinnedAssemblies", throwOnError: true)!; + var source = type.GetNestedType("Source", BindingFlags.Public | BindingFlags.NonPublic)!; + byte[] bytes = File.ReadAllBytes(path); + pinned = (IDisposable)Activator.CreateInstance(type, flags, null, + [bytes, Convert.ToHexStringLower(SHA256.HashData(bytes)), Array.CreateInstance(source, 0), null], null)!; + Module = (ModuleDefinition)type.GetProperty("Selected", flags)!.GetValue(pinned)!; + } + + internal ModuleDefinition Module { get; } + + internal object Context => pinned; + + public void Dispose() => pinned.Dispose(); +} diff --git a/Tests/PlatformDefinitions.lean b/Tests/PlatformDefinitions.lean new file mode 100644 index 000000000..20d876094 --- /dev/null +++ b/Tests/PlatformDefinitions.lean @@ -0,0 +1,509 @@ +import Tests.MetadataWorld + +/-! +# Platform definitions the metadata checks rely on + +Each check that relies on a type no carried slice defines reads the type's platform definition, +so a list with the entry removed or stating another kind is refused. A type reference outside the +carried slices takes its kind from its definition and refuses a signature that reads it as the +other kind; an external enum admits only the integer element type its definition states; a class +chain ends at the definition of `System.Object`; and a type is a value type or an enum through +its base only when the base is the definition of `System.ValueType` or `System.Enum` in the +core library's scope, so a `System.Enum` of the fixture's own assembly or of another assembly +makes no enum. The pair's items and the byte-order constant are tested with their own worlds, +in `Tests.ImportedFieldIndex` and `Tests.StaticFieldSites`. +-/ + +namespace Tests.PlatformDefinitions + +open CIL CIL.Extraction Tests.MetadataBuilder Tests.MetadataWorld + +/-- A local root can supply runtime storage only under the recognized neutral publisher. -/ +theorem carried_core_scope_checks_publisher : + isCorlibScope (bytesOf "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") = true ∧ + isCorlibScope (bytesOf "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=null") = false ∧ + isCorlibScope (bytesOf "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e") = false ∧ + isCorlibScope (bytesOf "System.Runtime, Version=11.0.0.0, Culture=fr, PublicKeyToken=b03f5f7f11d50a3a") = false := by + decide +kernel +#print axioms carried_core_scope_checks_publisher + +/-- Three carried class definitions. The root ends the chain and its direct child owns a field. -/ +private def carriedClasses (scope rootName : String) (rootFlags : Nat) : + Except MetadataError MetadataEnv := do + let rows : Step Unit := do + let space ← string "System" + let root ← string rootName + let parent ← string "Exception" + let child ← string "SystemException" + let fieldName ← string "_HResult" + let fieldType ← blob [0x06, 0x08] + let _ ← row Table.typeDef [rootFlags, root, space, 0, 1, 1] + let _ ← row Table.typeDef [1, parent, space, 4, 1, 1] + let _ ← row Table.typeDef [1, child, space, 8, 2, 1] + let _ ← row Table.field [1, fieldName, fieldType] + let slice := (build rows (bytesOf "System.Private.CoreLib") scope 11).1 + let index ← slice.indexWith (← slice.tables) (bytesOf scope) + return MetadataEnv.ofSlices [index] + +/-- Only the checked root ends the lineage. Its child's field counts towards the grandchild. -/ +theorem carried_object_lineage : + (do let env ← carriedClasses coreLibraryScope "Object" 1 + let lineage ← env.classChain resolveFuel 0 0x02000003 + let fields ← env.chainFieldCount lineage + let frame ← env.frame 0 0 + return (lineage, fields, frame.corlib)) = + .ok ([(0, 0x02000002)], 1, some (bytesOf coreLibraryScope)) := by rfl +#print axioms carried_object_lineage + +/-- The defining CoreLib identity supplies Int32 storage even without a facade reference. -/ +theorem carried_primitive_storage : + typeStorageAt (fun _ => none) (.atom coreLibraryScope "System.Int32") = some (.field .signed32) ∧ + typeStorageAt (fun _ => none) (.atom ownIdentity "System.Int32") = none := + ⟨rfl, rfl⟩ +#print axioms carried_primitive_storage + +/-- A facade, another name or an interface cannot stand in for the defining root class. -/ +theorem carried_object_rejects_other_definitions : + let check := fun scope name flags => do + let env ← carriedClasses scope name flags + env.classChain resolveFuel 0 0x02000003 + check runtimeScope "Object" 1 = .error (.unsupportedBase 0x02000001) ∧ + check coreLibraryScope "Other" 1 = .error (.unsupportedBase 0x02000001) ∧ + check coreLibraryScope "Object" 0x21 = .error (.unsupportedBase 0x02000001) := + ⟨rfl, rfl, rfl⟩ +#print axioms carried_object_rejects_other_definitions + +/-! ## A type reference outside the carried slices -/ + +/-- `System.Console` of the world resolved under `platform`, read with `hint`. -/ +private def console (platform : PlatformTable) (hint : Option Bool) : + Except MetadataError NamedType := do + let env := MetadataEnv.ofSlices [← own.1.index, ← lib.index] (platform := platform) + typeRefNamed env 0 resolveFuel tokens.consoleRef hint + +/-- Whether `result` is a type of this kind. -/ +private def resolvedAs (result : Except MetadataError NamedType) (kind : Extraction.TypeKind) : + Bool := + match result with + | .ok named => named.kind == kind + | .error _ => false + +/-- Whether `result` failed with `error`. -/ +private def failedWith {α : Type} (result : Except MetadataError α) (error : MetadataError) : + Bool := + match result with + | .error found => found == error + | .ok _ => false + +/-- `Console` is the class its definition states, read with no hint or as a class; without the + definition it is refused, and a definition that states a value type refuses the class hint and + makes the unhinted reference a value type. -/ +theorem references_take_their_kind_from_the_definition : + resolvedAs (console worldPlatform none) (.external (some false)) = true ∧ + resolvedAs (console worldPlatform (some false)) (.external (some false)) = true ∧ + failedWith (console worldPlatform (some true)) (.platformKindMismatch tokens.consoleRef) = + true ∧ + failedWith (console (removing worldPlatform "Console") none) + (.missingPlatformDefinition tokens.consoleRef) = true ∧ + failedWith (console (restating worldPlatform "Console" .valueType) (some false)) + (.platformKindMismatch tokens.consoleRef) = true ∧ + resolvedAs (console (restating worldPlatform "Console" .valueType) none) + (.external (some true)) = true := by decide +kernel +#print axioms references_take_their_kind_from_the_definition + +/-- Removing the declaring definition leaves the ordinary method reference and type unresolved. -/ +theorem missing_owner_requires_link : + (do + let env := MetadataEnv.ofSlices [<- own.1.index, <- lib.index] + (platform := removing worldPlatform "Console") + let missing := MetadataError.missingPlatformDefinition tokens.consoleRef + let type := SigType.named false tokens.consoleRef + let head := ArgumentHead.platform runtimeScopeBytes [systemNamespace, bytesOf "Console"] + return failedWith (env.methodSite tokens.writeLine) missing && + failedWith (env.resolve 0 type) missing && + failedWith (env.resolveToken 0 tokens.consoleRef) missing && + failedWith (head.resolve env tokens.writeLine 0) + (.missingPlatformDefinition tokens.writeLine)).toOption = some true := by + decide +kernel + +#print axioms missing_owner_requires_link + +/-- A pinned base cannot be classified through a platform entry, for any scope or table. -/ +theorem pinned_base_requires_its_definition (env : MetadataEnv) (index : MetadataIndex) + (token base : Nat) (scope : List Nat) (parts : List (List Nat)) (core : Bool) + (pinned : env.pinned.matches scope = true) : + env.externalBaseKind index token base scope parts core = .error (.missingSlice base) := by + simp [MetadataEnv.externalBaseKind, pinned] +#print axioms pinned_base_requires_its_definition + +/-- The metadata of a class with a closed generic base and an ordinary static method owner. -/ +private def derivedOwner : Step Nat := do + let runtime <- string "System.Runtime" + let system <- string "System" + let object <- string "Object" + let key <- blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ <- row Table.assemblyRef [11, 0, 0, 0, 0, key, runtime, 0, 0] + let _ <- row Table.typeRef [6, object, system] + let baseName <- string "Base`1" + let base <- row Table.typeDef [1, baseName, 0, 5, 1, 1] + let parameter <- string "T" + let _ <- row Table.genericParam [0, 0, 2, parameter] + let signature <- blob [0x15, 0x12, 4, 1, 8] + let _ <- row Table.typeSpec [signature] + let derived <- string "Derived" + let owner <- row Table.typeDef [1, derived, 0, 6, 1, 1] + link linkGenericParams base 0 0x2A000001 + return owner + +/-- A static owner can derive from the closed class Base. -/ +theorem closed_class_base_classifies : + (do + let (image, owner) := build derivedOwner (bytesOf "T") ownIdentity + let env := MetadataEnv.ofSlices [<- image.index] + return (<- typeDefNamed env 0 resolveFuel owner).kind).toOption = some .classType := by + decide +kernel +#print axioms closed_class_base_classifies + +/-- Each class derives from Pair of two copies of the previous class, with distinct TypeSpecs. -/ +private def repeatedBases (depth : Nat) : Step Nat := do + let runtime <- string "System.Runtime" + let key <- blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ <- row Table.assemblyRef [11, 0, 0, 0, 0, key, runtime, 0, 0] + let object <- row Table.typeRef [6, <- string "Object", <- string "System"] + let base := tokenRow object * 4 + 1 + let _ <- row Table.typeDef [0, <- string "", 0, 0, 1, 1] + let pair <- row Table.typeDef [1, <- string "Pair`2", 0, base, 1, 1] + let first <- row Table.genericParam [0, 0, tokenRow pair * 2, <- string "A"] + let _ <- row Table.genericParam [1, 0, tokenRow pair * 2, <- string "B"] + link linkGenericParams pair 0 first + let mut owner <- row Table.typeDef [1, <- string "D0", 0, base, 1, 1] + for level in [:depth] do + let argument := tokenRow owner * 4 + let signature <- blob [0x15, 0x12, tokenRow pair * 4, 2, 0x12, argument, 0x12, argument] + let base <- row Table.typeSpec [signature] + owner <- row Table.typeDef [1, <- string s!"D{level + 1}", 0, tokenRow base * 4 + 2, 1, 1] + return owner + +/-- The same number of owner resolutions at each depth, after metadata indexing. -/ +private def measureOwners (depth : Nat) : IO (Prod Nat Nat) := do + let (image, owner) := build (repeatedBases depth) (bytesOf "T") ownIdentity + let .ok index := image.index | throw (IO.userError "invalid repeated base fixture") + let env := MetadataEnv.ofSlices [index] + let repetitions := 512 + let start <- IO.monoNanosNow + let before <- IO.getNumHeartbeats + for sample in [:repetitions] do + unless resolvedAs (typeDefNamed env 0 (resolveFuel + sample % 2) owner) .classType do + throw (IO.userError "constructed base owner did not resolve as a class") + let work := (<- IO.getNumHeartbeats) - before + return (((<- IO.monoNanosNow) - start) / repetitions, work / repetitions) + +#eval do + let mut atEight := 0 + for depth in [8, 10, 12, 14] do + let (nanos, work) <- measureOwners depth + IO.println s!"constructed base depth {depth}: {nanos} ns, {work} heartbeats per resolution" + if depth == 8 then atEight := work + if work == 0 || work * 8 > atEight * depth then + throw (IO.userError "owner resolution grew faster than the depth of the metadata") + +/-- A TypeSpec base gives a class owner without inspecting its signature or a platform entry. -/ +theorem constructed_base_kind (env : MetadataEnv) (index : MetadataIndex) (pos owner : Nat) + (row : TypeDefRow) (parts : List (List Nat)) (classFlag : row.flags &&& 0x20 = 0) + (constructed : metadataTableOf row.extends_ = Table.typeSpec) : + typeDefKind env index pos owner row parts = .ok .classType := by + simp [typeDefKind, classFlag, constructed, Table.typeSpec, bind, Except.bind, pure, Except.pure] +#print axioms constructed_base_kind + +/-- A constructed base supplies no instance layout for a receiver or an inherited field. -/ +theorem constructed_base_has_no_layout (env : MetadataEnv) (index : MetadataIndex) + (position owner fuel : Nat) (row : TypeDefRow) + (found : env.slice position owner = .ok index) (entry : index.typeDef owner = .ok row) + (constructed : metadataTableOf row.extends_ = Table.typeSpec) : + env.classChain (fuel + 1) position owner = .error (.unsupportedBase owner) := by + simp [MetadataEnv.classChain, found, entry, constructed, bind, Except.bind, Table.typeSpec] + +#print axioms constructed_base_has_no_layout + +/-- A platform head accepts exactly the parameter count carried by its resolved definition. -/ +theorem platform_arity_matches (env : MetadataEnv) (named : NamedType) (position token count : Nat) + (external : named.pinned = false) : + (env.requireArity position token named count).isOk = (named.platformArity == count) := by + cases equal : named.platformArity == count <;> + simp [MetadataEnv.requireArity, external, equal] <;> rfl + +#print axioms platform_arity_matches + +/-- A closed argument from a definition requires the complete assembly identity among the pins. -/ +theorem definition_requires_pin (env : MetadataEnv) (scope : List Nat) (method token count : Nat) + (absent : env.exactPins.contains scope = false) : + ArgumentHead.resolve env method count (.definition scope token) = + .error (.missingSlice token) := by + simp [ArgumentHead.resolve, absent] + rfl + +#print axioms definition_requires_pin + +/-- Named types with fixed runtime storage cannot use an aggregate layout. -/ +theorem fixed_storage_excludes_layout (frame : Frame) (named : NamedType) (layout : Nat) + (identity : String) (fixed : named.fixedStorage = true) : + frame.admits (.named named) (.aggregate identity layout) = false := by + simp [Frame.admits, typeAdmits, fixed] + +#print axioms fixed_storage_excludes_layout + +/-- An opaque declaring name supplies no type expression for a local, field or type argument. -/ +theorem imported_owner_has_no_storage (frame : Frame) (named : NamedType) (expr : TypeExpr) + (opaqueKind : named.kind = .importedOwner) : + frame.admits (.named named) expr = false := by + cases expr <;> + simp [Frame.admits, typeAdmits, opaqueKind, objectNamed, valueLike, NamedType.isEnum] + +#print axioms imported_owner_has_no_storage + +/-- The interface flag determines the kind even if the image states a base. -/ +theorem interface_from_flags (env : MetadataEnv) (index : MetadataIndex) (pos owner : Nat) + (row : TypeDefRow) (parts : List (List Nat)) (flag : row.flags &&& 0x20 != 0) : + typeDefKind env index pos owner row parts = .ok .interfaceType := by + simp [typeDefKind, flag] + rfl + +#print axioms interface_from_flags + +/-- The identity of a constructed layout includes the defining scope of its named argument. -/ +theorem aggregate_argument_keeps_scope (head argument : NamedType) (corlib : Option (List Nat)) : + aggregateSpelling corlib 4 (.genericInst head (.cons (.named argument) .nil)) = + some (head.scope ++ [58, 58] ++ head.name ++ [60] ++ + (argument.scope ++ [58, 58] ++ argument.name) ++ [62]) := rfl + +#print axioms aggregate_argument_keeps_scope + +/-! ## An external enum -/ + +/-- The platform enum `Number` of `E` over `int`. -/ +private def numberPlatform : PlatformTable := platform_table% + [{ scope := bytesOf "E", parts := [[], bytesOf "Number"], kind := .enumType 8 }] + +/-- Whether the type `spelled` of `E` with the name parts of `Number`, resolved outside the + carried slices, is admitted under `platform` as an enum over the runtime's type `name`. -/ +private def admitsNumber (platform : PlatformTable) (name : String) (spelled := "Number") : Bool := + let frame : Frame := + { corlib := some runtimeScopeBytes, anchor := runtimeScopeBytes, arity := 0, ownerArity := 0 } + let env := MetadataEnv.ofSlices [] (platform := platform) + match env.externalNamed 0 (bytesOf "E") (bytesOf spelled) [[], bytesOf "Number"] none with + | .ok named => frame.admits (.named named) (.enum ("E::" ++ spelled) (.atom runtimeScope name)) + | .error _ => false + +/-- A platform enum cannot use aggregate storage at any layout index. -/ +theorem enums_refuse_aggregate_storage (frame : Frame) (named : NamedType) (code index : Nat) + (identity : String) (kind : named.kind = .enumType code) : + frame.admits (.named named) (.aggregate identity index) = false := by + simp [Frame.admits, typeAdmits, kind, valueLike] +#print axioms enums_refuse_aggregate_storage + +/-- A missing entry for a closed platform argument preserves the diagnostic and method token. -/ +theorem missing_closed_argument_definition (method : Nat) : + (InstanceArgument.named (.platform (bytesOf "E") [[], bytesOf "Number"])).resolve + (MetadataEnv.ofSlices []) method resolveFuel = + .error (.missingPlatformDefinition method) := by + rfl +#print axioms missing_closed_argument_definition + +/-- The eight integer element types, by code from `0x04`, and the other scalar ones. -/ +private def integerNames : List (Nat × String) := + [(0x04, "System.SByte"), (0x05, "System.Byte"), (0x06, "System.Int16"), + (0x07, "System.UInt16"), (0x08, "System.Int32"), (0x09, "System.UInt32"), + (0x0A, "System.Int64"), (0x0B, "System.UInt64")] +/-- Scalar names whose element codes cannot supply enum storage. -/ +private def otherNames : List (Nat × String) := + [(0x02, "System.Boolean"), (0x03, "System.Char"), (0x0C, "System.Single"), + (0x0D, "System.Double"), (0x18, "System.IntPtr"), (0x19, "System.UIntPtr")] + +/-- An external enum admits the element type its definition states and no other: none without + the definition, none when it states a value type, none when it states an enum over a type no + enum may have, and none for a type whose parts name another type's definition. -/ +theorem external_enums_take_their_underlying_type_from_the_definition : + admitsNumber numberPlatform "System.Int32" = true ∧ + admitsNumber numberPlatform "System.Byte" = false ∧ + admitsNumber numberPlatform "System.Int32" (spelled := "Other") = false ∧ + admitsNumber (removing numberPlatform "Number") "System.Int32" = false ∧ + admitsNumber (restating numberPlatform "Number" .valueType) "System.Int32" = false ∧ + (integerNames.all fun (code, name) => + admitsNumber (restating numberPlatform "Number" (.enumType code)) name) = true ∧ + (otherNames.any fun (code, name) => + admitsNumber (restating numberPlatform "Number" (.enumType code)) name) = false := by + decide +kernel +#print axioms external_enums_take_their_underlying_type_from_the_definition + +/-! ## The end of a class chain -/ + +/-- Whether checking `operand` against `token` of the world gives `error` under `platform`, or + passes when `error` is `none`. -/ +private def checks (platform : PlatformTable) (token : Nat) (operand : RawOperand) + (error : Option MetadataError) : Bool := + match (do + let env := MetadataEnv.ofSlices [← own.1.index, ← lib.index] (platform := platform) + checkEntry env 0 token operand) with + | .ok _ => error.isNone + | .error found => error == some found + +/-- `count`, an instance field of the class `Program`, whose chain ends at `System.Object`. -/ +private def countOperand : RawOperand := + .field (.object ownIdentity "Program") 0 int32 name!"count" false + +/-- An instance field of a class is bound only when the chain ends at the definition of + `System.Object`: without it the class's base is refused, and as a plain class the chain is. -/ +theorem class_chains_end_at_the_object_definition : + checks worldPlatform tokens.countField countOperand none = true ∧ + checks (removing worldPlatform "Object") tokens.countField countOperand + (some (.missingPlatformDefinition tokens.objectRef)) = true ∧ + checks (restating worldPlatform "Object" .classType) tokens.countField countOperand + (some (.platformKindMismatch tokens.objectRef)) = true := by decide +kernel +#print axioms class_chains_end_at_the_object_definition + +/-! ## The kind a base gives -/ + +/-- `Lib`'s reference to `System.ValueType`, the base of `Lib.Pair`. -/ +private def valueTypeRef : Nat := 0x01000002 + +/-- `Lib.Pair` is a value type only through the definition of `System.ValueType`: without it the + base is refused, and a definition that states a plain class or the root of enums is refused + for that name. -/ +theorem value_types_come_from_the_value_type_definition : + checks worldPlatform tokens.pairRef (.type (.atom libScope "Lib.Pair")) none = true ∧ + checks worldPlatform tokens.pairRef (.type (.object libScope "Lib.Pair")) + (some (.operandMismatch tokens.pairRef)) = true ∧ + checks (removing worldPlatform "ValueType") tokens.pairRef (.type (.atom libScope "Lib.Pair")) + (some (.missingPlatformDefinition valueTypeRef)) = true ∧ + checks (restating worldPlatform "ValueType" .classType) tokens.pairRef + (.type (.atom libScope "Lib.Pair")) (some (.platformKindMismatch valueTypeRef)) = true ∧ + checks (restating worldPlatform "ValueType" .enumRoot) tokens.pairRef + (.type (.atom libScope "Lib.Pair")) (some (.platformKindMismatch valueTypeRef)) = true := by + decide +kernel +#print axioms value_types_come_from_the_value_type_definition + +/-- Where the base `System.Enum` of `Program` is defined: in `T` itself, or in the assembly + `Ext` outside the carried slices. -/ +private inductive EnumHome where + | own + | ext + +/-- `T` whose `Program`, with an `int` instance field `value__` and an instance method `Run`, + extends a `System.Enum` of `home`. Returns the token of `Run`. -/ +private def enumWorld (home : EnumHome) (rootName := "Enum") : Step Nat := do + let runtime ← string "System.Runtime" + let extName ← string "Ext" + let system ← string "System" + let objectName ← string "Object" + let enumName <- string rootName + let program ← string "Program" + let publicKeyToken ← blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtime, 0, 0] + let _ ← row Table.assemblyRef [1, 0, 0, 0, 0, 0, extName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let objectRef ← row Table.typeRef [1 * 4 + 2, objectName, system] + let extEnum ← row Table.typeRef [2 * 4 + 2, enumName, system] + let moduleName ← string "" + let _ ← row Table.typeDef [0, moduleName, 0, 0, 1, 1] + let ownEnum ← row Table.typeDef [1, enumName, system, (objectRef - 0x01000000) * 4 + 1, 1, 1] + let base := match home with + | .own => (ownEnum - 0x02000000) * 4 + | .ext => (extEnum - 0x01000000) * 4 + 1 + let owner ← row Table.typeDef [1, program, 0, base, 1, 1] + let valueName ← string "value__" + let fieldSignature ← blob [0x06, 0x08] + let field ← row Table.field [0x606, valueName, fieldSignature] + link linkDeclaring field 0 owner + let methodName ← string "Run" + let signature ← blob [0x20, 0, 1] + let method ← row Table.methodDef [0, 0, 0x86, methodName, signature, 1] + link linkDeclaring method 0 owner + pure method + +/-- `Ext`'s `System.Enum` as the platform definition of this kind, beside `System.Object`. -/ +private def extEnumPlatform (kind : PlatformKind) : PlatformTable := + restating (platform_table% [runtimeType "Object" .objectRoot, + { scope := bytesOf "Ext, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", + parts := [bytesOf "System", bytesOf "Enum"], kind := .classType }]) "Enum" kind + +/-- Checking `Run` with its receiver claimed an object of a class with the one field `value__` + or a managed reference. -/ +private def receiver (home : EnumHome) (platform : PlatformTable) (objectReceiver : Bool) : + Except MetadataError MethodNames := do + let fixture := build (enumWorld home) (bytesOf "T") ownIdentity + let method : RawMethod := + { token := fixture.2, owner := ownIdentity ++ "::Program", hasThis := true, + explicitThis := false, callingConvention := 0, attributes := 0x86, implementation := 0, + returnType := .void, parameters := [], locals := [], initLocals := false, maxStack := 8, + genericParameters := [], code := [], objectReceiver, + objectFieldCount := if objectReceiver then 1 else 0 } + let env := MetadataEnv.ofSlices [← fixture.1.index] (platform := platform) + methodIdentityValid env method [] [] + +/-- The tokens of `Run` and `Program` in both worlds. -/ +private def runToken : Nat := 0x06000001 +private def programToken : Nat := 0x02000003 + +/-- A type that extends a `System.Enum` of its own assembly, or of an assembly other than the + core library, is a class, never an enum whose receiver is a managed reference: the first is an + object, and the second's chain ends at no core `System.Object`. -/ +theorem only_the_core_library_enum_makes_an_enum : + (receiver .own objectPlatform true).isOk = true ∧ + failedWith (receiver .own objectPlatform false) (.methodMismatch runToken) = true ∧ + failedWith (receiver .ext (extEnumPlatform .classType) true) + (.unsupportedBase programToken) = true ∧ + failedWith (receiver .ext (extEnumPlatform .classType) false) (.methodMismatch runToken) = + true ∧ + failedWith (receiver .ext (extEnumPlatform .enumRoot) false) + (.methodMismatch runToken) = true := by decide +kernel +#print axioms only_the_core_library_enum_makes_an_enum + +/-- The core root definitions, a struct and an enum in the same assembly. -/ +private def coreDefinitions : Step Unit := do + let system <- string "System" + let _ <- row Table.typeDef [0, <- string "", 0, 0, 1, 1] + let object <- row Table.typeDef [1, <- string "Object", system, 0, 1, 1] + let value <- row Table.typeDef [1, <- string "ValueType", system, tokenRow object * 4, 1, 1] + let root <- row Table.typeDef [1, <- string "Enum", system, tokenRow value * 4, 1, 1] + let _ <- row Table.typeDef [1, <- string "Struct", 0, tokenRow value * 4, 1, 1] + let flag <- row Table.typeDef [1, <- string "Flag", 0, tokenRow root * 4, 1, 1] + let field <- row Table.field [0x606, <- string "value__", <- blob [0x06, 0x08]] + link linkDeclaring field 0 flag + +/-- Resolve the same declarations with and without the ECMA publisher key in the Assembly row. -/ +private def carriedKinds (keyed : Bool) : Except MetadataError (List Extraction.TypeKind) := do + let token := if keyed then "b77a5c561934e089" else "null" + let identity := "mscorlib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=" ++ token + let (image, _) := build coreDefinitions (bytesOf "mscorlib") identity + let assembly := image.assembly + let ecmaKey := List.replicate 8 0 ++ [4] ++ List.replicate 7 0 + let assembly := if keyed then + { assembly with + assemblyRow := (assembly.assemblyRow.set 12 1).set 16 1 + keyToken := bytesOf token + blobs := [(0, [0]), (1, 16 :: ecmaKey)] } + else assembly + let env := MetadataEnv.ofSlices [← ({ image with assembly } : MetadataSlice).index] + let kind := fun token => (typeDefNamed env 0 resolveFuel token).map NamedType.kind + return [← kind 0x02000001, ← kind 0x02000002, ← kind 0x02000004, + ← kind 0x02000005, ← kind 0x02000006] + +/-- Only the published core identity supplies the value and enum roots in its own image. -/ +theorem carried_core_roots : + (carriedKinds true).toOption = + some [.classType, .classType, .classType, .valueType, .enumType 8] ∧ + (carriedKinds false).toOption = + some [.classType, .classType, .classType, .classType, .classType] := by decide +kernel +#print axioms carried_core_roots + +/-- An external System.ValueType in another scope supplies an ordinary class base. -/ +theorem foreign_value_type_is_a_class_base : + (do + let (image, _) := build (enumWorld .ext "ValueType") (bytesOf "T") ownIdentity + let env := MetadataEnv.ofSlices [<- image.index] + return (<- typeDefNamed env 0 resolveFuel programToken).kind).toOption = + some .classType := by + decide +kernel +#print axioms foreign_value_type_is_a_class_base + +end Tests.PlatformDefinitions diff --git a/Tests/PointerOperations.cs b/Tests/PointerOperations.cs index 059e19df7..b8ebf0313 100644 --- a/Tests/PointerOperations.cs +++ b/Tests/PointerOperations.cs @@ -3,6 +3,11 @@ #:property AllowUnsafeBlocks=true #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs @@ -10,6 +15,9 @@ using System.Reflection; using Mono.Cecil; +var returnOnly = args is ["--return-only"]; +if (args.Length != 0 && !returnOnly) + throw new ArgumentException("Usage: PointerOperations.cs [--return-only]"); var assembly = At("Examples/Assembly/bin/Examples.dll"); var root = At("artifacts/PointerOperations"); Directory.CreateDirectory(root); @@ -21,6 +29,7 @@ foreach (var name in new[] { "Subtract", "SubtractNative", "Difference", "Less", "Greater", "LessEqual", "GreaterEqual" }) { + if (returnOnly) break; var subtraction = name.StartsWith("Subtract", StringComparison.Ordinal); var native = name == "SubtractNative"; var signature = subtraction ? "System.Byte*," + (native ? "System.IntPtr" : "System.Int32") : @@ -63,9 +72,9 @@ CIL.Safety.invoke Extracted.program 64 0 """ + "\n" + control); evidence.Add(proof, "PASS runtime, certificate and checked pointer execution: " + name); if (name != "Difference") continue; - if (!source.Contains(".sub", StringComparison.Ordinal)) throw new Exception("Missing difference mutation target"); + // The method is defined from its decoder, so the forgery changes the sub (89) the decoder reads to add (88). var forged = Path.Combine(root, "ForgedDifference.lean"); - File.WriteAllText(forged, differenceEvidence!.Replace(".sub", ".add", StringComparison.Ordinal)); + File.WriteAllText(forged, ForgeDecodedOriginal(differenceEvidence!, 0, "opcode := 89,", "opcode := 88,")); rejected.Add(forged, "PASS independent forged pointer difference rejection", "ScalarFlowValid"); } @@ -75,6 +84,7 @@ CIL.Safety.invoke Extracted.program 64 0 ("WriteEnum", "Examples.Unsigned64*,System.UInt64") }) { + if (returnOnly && name != "ReadEnum") continue; var output = Path.Combine(root, name); Run(["dotnet", ExtractorPath, "extract", assembly, $"PointerOperations::{name}({parameters})", output]); var write = name == "WriteEnum"; @@ -137,18 +147,23 @@ by decide +kernel } if (name == "ReadEnum") { - var returnEvidence = NarrowEvidence(source, "method0_shape_validated"); + var returnEvidence = NarrowEvidence(source, "method0_declared_validated"); File.AppendAllText(proof, "\n" + CheckedEvidenceControl(source, returnEvidence, - ["method0_shape_validated"], 1)); - foreach (var replacement in new[] { ".ret (some .signed8)", ".ret (some .unsigned16)" }) + ["method0_declared_validated"], 1)); + foreach (var replacement in new[] { "System.Int32", "System.UInt16" }) { - const string target = ".ret (some .signed16)"; - if (!returnEvidence.Contains(target, StringComparison.Ordinal)) - throw new InvalidOperationException("Missing enum return narrowing"); - var widthChanged = replacement.Contains("signed8", StringComparison.Ordinal); + // The method is defined from its decoder, so the forged declared return is a forged decoder's value. + var forgedSource = ForgeDecodedReturnType(returnEvidence, 0, "\"System.Int16\"", $"\"{replacement}\""); + var widthChanged = replacement == "System.Int32"; var forged = Path.Combine(output, "ForgedReturn" + widthChanged + ".lean"); - File.WriteAllText(forged, returnEvidence.Replace(target, replacement, StringComparison.Ordinal)); - rejected.Add(forged, "PASS independent enum return width/signedness rejection", "MethodShapeValid"); + File.WriteAllText(forged, forgedSource); + rejected.Add(forged, "PASS independent enum return width/signedness rejection", "DeclaredValid"); + // The `rfl` rejection is a failed elaboration; the kernel must also prove the obligation false. + var refuted = Path.Combine(output, "RefutedReturn" + widthChanged + ".lean"); + File.WriteAllText(refuted, KernelRefutation(forgedSource, "method0_declared_validated", + "method0_declared_refuted")); + evidence.Add(refuted, "PASS the kernel proves the forged enum return declaration false", + "method0_declared_refuted"); } } evidence.Add(proof, "PASS runtime, certificate and checked pointee storage: " + name); @@ -156,6 +171,7 @@ by decide +kernel foreach (var mutation in new[] { "float", "nested", "reference-field", "enum-storage", "enum-identity", "enum-scope" }) { + if (returnOnly) break; var output = Path.Combine(root, "Reject-" + mutation); Directory.CreateDirectory(output); var dll = Path.Combine(output, "Examples.dll"); diff --git a/Tests/PointerOperations.lean b/Tests/PointerOperations.lean index ebad72338..0091324a2 100644 --- a/Tests/PointerOperations.lean +++ b/Tests/PointerOperations.lean @@ -28,7 +28,7 @@ private def memory : Memory := { nextIdentity := 2 } private def subtract : CIL.Method := { - code := [.arg 0, .arg 1, .sub, .ret], locals := [], returnsValue := true } + code := [.ldarg 0, .ldarg 1, .sub, .ret], locals := [], returnsValue := true } /-- Pointer subtraction checks positions without requiring readable storage. -/ theorem subtract_bytes : @@ -101,26 +101,26 @@ theorem interior_before_end : /-- Pointer subtraction cannot be certified as scalar addition. -/ theorem changed_subtraction_rejected : - CIL.Extraction.scalarOpMatches ⟨0, 89, .none⟩ [.native, .reference] .add = false := + CIL.Extraction.scalarOpMatches none ⟨0, 89, .none⟩ [.native, .reference] .add = false := by decide +kernel #print axioms changed_subtraction_rejected /-- A difference must retain the exact raw subtraction operation. -/ theorem difference_encoding : - CIL.Extraction.scalarOpMatches ⟨0, 89, .none⟩ [.reference, .reference] .sub = true := + CIL.Extraction.scalarOpMatches none ⟨0, 89, .none⟩ [.reference, .reference] .sub = true := by decide +kernel #print axioms difference_encoding -private def comparison (operation : CIL.Op) : CIL.Method := { - code := [.arg 0, .arg 1, operation, .ret], locals := [], returnsValue := true } +private def comparison (operation : CIL.Instruction) : CIL.Method := { + code := [.ldarg 0, .ldarg 1, operation, .ret], locals := [], returnsValue := true } -private def branch (operation : CIL.Op) : CIL.Method := { - code := [.arg 0, .arg 1, operation, .const32 0, .ret, .const32 1, .ret] +private def branch (operation : CIL.Instruction) : CIL.Method := { + code := [.ldarg 0, .ldarg 1, operation, .ldc_i4 0, .ret, .ldc_i4 1, .ret] locals := [], returnsValue := true } /-- Both unsigned comparisons agree with offset ordering and preserve memory. -/ theorem unsigned_comparisons : - [CIL.Op.ltu, .gtu].map (fun operation => + [CIL.Instruction.clt_un, .cgt_un].map (fun operation => (invoke [comparison operation] 8 0 [.reference (.address ⟨0, 2⟩), .readOnlyReference (.address ⟨0, 5⟩)] memory).map Prod.snd) = [.ok [.scalar (.i32 1)], .ok [.scalar (.i32 0)]] := rfl @@ -128,7 +128,7 @@ theorem unsigned_comparisons : /-- All four unsigned branches select the proper successor for the same pair of pointers. -/ theorem unsigned_branches : - [CIL.Op.bltu 5, .bleu 5, .bgtu 5, .bgeu 5].map (fun operation => + [CIL.Instruction.blt_un 5, .ble_un 5, .bgt_un 5, .bge_un 5].map (fun operation => (invoke [branch operation] 10 0 [.reference (.address ⟨0, 2⟩), .reference (.address ⟨0, 5⟩)] memory).map Prod.snd) = [.ok [.scalar (.i32 1)], .ok [.scalar (.i32 1)], @@ -148,7 +148,7 @@ theorem identical_position_not_less : /-- Signed ordering cannot be certified from managed-reference operand kinds. -/ theorem signed_order_rejected : - CIL.Extraction.scalarOpMatches ⟨0, 65028, .none⟩ [.reference, .reference] .lt = false := + CIL.Extraction.scalarOpMatches none ⟨0, 65028, .none⟩ [.reference, .reference] .clt = false := by decide +kernel #print axioms signed_order_rejected /-- Expired storage cannot participate in a pointer difference. -/ @@ -161,7 +161,7 @@ theorem expired_difference_rejected : /-- Integer-minus-pointer is not a pointer operation. -/ theorem reversed_operands_rejected : - CIL.Extraction.scalarOpMatches ⟨0, 89, .none⟩ [.reference, .native] .sub = false := + CIL.Extraction.scalarOpMatches none ⟨0, 89, .none⟩ [.reference, .native] .sub = false := by decide +kernel #print axioms reversed_operands_rejected @@ -179,7 +179,7 @@ theorem subtraction_kind (raw : CIL.Extraction.RawMethod) : /-- Non-strict branches include equality, while strict branches exclude it. -/ theorem equal_position_branches : - [CIL.Op.bltu 5, .bleu 5, .bgtu 5, .bgeu 5].map (fun operation => + [CIL.Instruction.blt_un 5, .ble_un 5, .bgt_un 5, .bge_un 5].map (fun operation => (invoke [branch operation] 10 0 [.reference (.address ⟨0, 2⟩), .reference (.address ⟨0, 2⟩)] memory).map Prod.snd) = [.ok [.scalar (.i32 0)], .ok [.scalar (.i32 1)], diff --git a/Tests/PreparedFixtures.cs b/Tests/PreparedFixtures.cs index 09018edf2..a38fc1351 100644 --- a/Tests/PreparedFixtures.cs +++ b/Tests/PreparedFixtures.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Runtime.InteropServices; using System.Text.Json; @@ -48,10 +53,28 @@ void Rejected(Action action) throw new Exception("Prepared fixture arguments changed"); var corpus = Path.Combine(directory, "isolated"); Directory.CreateDirectory(Path.Combine(corpus, "scripts")); - File.Copy(At("scripts/Verification.cs"), Path.Combine(corpus, "scripts", "Verification.cs")); + var probeScripts = File.ReadLines(At("Tests/PreparedFixtures.cs")) + .Where(line => line.StartsWith("#:include ../scripts/", StringComparison.Ordinal)) + .Select(line => line["#:include ../".Length..]).ToArray(); + // An included script may include further sources by paths relative to its own folder. + var copiedSources = new HashSet(StringComparer.Ordinal); + var pendingSources = new Queue(probeScripts); + while (pendingSources.TryDequeue(out var script)) + { + var relativeSource = Path.GetRelativePath(Root, Path.GetFullPath(At(script))); + if (!copiedSources.Add(relativeSource)) continue; + var copy = Path.Combine(corpus, relativeSource); + Directory.CreateDirectory(Path.GetDirectoryName(copy)!); + File.Copy(At(relativeSource), copy); + foreach (var included in File.ReadLines(At(relativeSource)) + .Where(line => line.StartsWith("#:include ", StringComparison.Ordinal)) + .Select(line => line["#:include ".Length..].Trim())) + pendingSources.Enqueue(Path.Combine(Path.GetDirectoryName(relativeSource) ?? "", included)); + } var probe = Path.Combine(corpus, "Probe.cs"); File.WriteAllText(probe, "#!/usr/bin/env dotnet\n#:property PublishAot=false\n" + - "#:include scripts/Verification.cs\nConsole.WriteLine(Root);\n"); + string.Join("\n", probeScripts.Select(script => "#:include " + script)) + + "\nConsole.WriteLine(Root);\n"); Environment.SetEnvironmentVariable("LEANCIL_FIXTURE_ROOT", Root); var probeOutput = Path.Combine(directory, "probe-bin"); Run(["dotnet", "build", probe, "-c", "Release", "--output", probeOutput, diff --git a/Tests/PreparedLeanFixtures.cs b/Tests/PreparedLeanFixtures.cs index b6689b9d8..8c14d202d 100644 --- a/Tests/PreparedLeanFixtures.cs +++ b/Tests/PreparedLeanFixtures.cs @@ -20,12 +20,20 @@ void PrepareLeanFixtures(params string[] modules) var compiled = Path.Combine(session, ".lake/build/lib/lean"); var records = new ConcurrentDictionary(StringComparer.Ordinal); foreach (var module in modules) - if (!Regex.IsMatch(module, @"\ATests\.[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*\z")) + if (!FixtureModule.IsMatch(module)) throw new ArgumentException("Expected a test fixture module: " + module); - var dependencies = modules.ToDictionary(module => module, module => Regex.Matches( - File.ReadAllText(At(module.Replace('.', '/') + ".lean")), @"(?m)^import[ \t]+(Tests\.[A-Za-z0-9_.]+)") - .Select(match => match.Groups[1].Value).ToArray(), StringComparer.Ordinal); - var pending = modules.ToHashSet(StringComparer.Ordinal); + // A fixture's imports of other test modules are compiled and recorded with it, so no list has to name them. + var dependencies = new Dictionary(StringComparer.Ordinal); + var unread = new Stack(modules); + while (unread.TryPop(out var module)) + { + if (dependencies.ContainsKey(module)) continue; + var text = File.ReadAllText(At(module.Replace('.', '/') + ".lean")); + dependencies[module] = [.. TestImport.Matches(text) + .Select(match => match.Groups[1].Value)]; + foreach (var dependency in dependencies[module]) unread.Push(dependency); + } + var pending = dependencies.Keys.ToHashSet(StringComparer.Ordinal); while (pending.Count != 0) { var ready = pending.Where(module => dependencies[module].All(dependency => !pending.Contains(dependency))) @@ -99,4 +107,10 @@ static void CheckLeanFixture(string module) if (!PreparedLeanFixturesMatch([module])) Run(["lake", "env", "lean", At(module.Replace('.', '/') + ".lean")], FixturePackage()); } + + [GeneratedRegex(@"(?m)^import[ \t]+(Tests\.[A-Za-z0-9_.]+)")] + private static partial Regex TestImport { get; } + + [GeneratedRegex(@"\ATests\.[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*\z")] + private static partial Regex FixtureModule { get; } } diff --git a/Tests/PreparedLeanFixturesTests.cs b/Tests/PreparedLeanFixturesTests.cs index f10f7907d..5c50684b3 100644 --- a/Tests/PreparedLeanFixturesTests.cs +++ b/Tests/PreparedLeanFixturesTests.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs using System.Text.Json; @@ -10,8 +15,16 @@ var source = At(module.Replace('.', '/') + ".lean"); const string fixture = "import CIL\ntheorem fixture : (1 : Nat) = 1 := rfl\n#print axioms fixture\n"; File.WriteAllText(source, fixture); +// A fixture that imports another test module, which the prepared list does not name. +var dependent = "Tests.PreparedDependent" + Guid.NewGuid().ToString("N"); +var dependentSource = At(dependent.Replace('.', '/') + ".lean"); +File.WriteAllText(dependentSource, + $"import CIL\nimport {module}\ntheorem dependent : (1 : Nat) = 1 := fixture\n#print axioms dependent\n"); try { + verification.PrepareLeanFixtures(dependent); + // The import is compiled before its importer and bound to its source like a listed fixture. + RequireLeanFixtures(dependent, module); verification.PrepareLeanFixtures(module); RequireLeanFixtures("CIL.Extraction.Translation", module); CheckLeanFixture(module); @@ -29,8 +42,10 @@ finally { File.Delete(source); + File.Delete(dependentSource); } -Console.WriteLine("PASS fresh fixture compilation, source/output binding and missing-module rejection"); +Console.WriteLine("PASS fresh fixture compilation, imported fixtures, source/output binding and " + + "missing-module rejection"); void Reject(string name, Action action) { diff --git a/Tests/PrimitiveArrays.cs b/Tests/PrimitiveArrays.cs index 68402ce77..21ca59993 100644 --- a/Tests/PrimitiveArrays.cs +++ b/Tests/PrimitiveArrays.cs @@ -2,10 +2,17 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs +#:include BulkCallEvidence.cs #:include RejectedEvidence.cs +using System.Globalization; using System.Reflection; using Mono.Cecil; @@ -75,13 +82,13 @@ theorem null_array_fault : (match CIL.Safety.invoke Extracted.program 64 0 [.object .null, .scalar (.i32 0)] inputMemory with - | .error fault => fault.fault == .thrown "System.NullReferenceException" + | .error fault => fault.fault == .thrown .nullReference | .ok _ => false) = true := by decide +kernel #print axioms null_array_fault theorem array_bounds_fault : (match CIL.Safety.invoke Extracted.program 64 0 [.array ⟨.address ⟨0, 0⟩, 2, .signed32⟩, .scalar (.i32 2)] inputMemory with - | .error fault => fault.fault == .thrown "System.IndexOutOfRangeException" + | .error fault => fault.fault == .thrown .indexOutOfRange | .ok _ => false) = true := by decide +kernel #print axioms array_bounds_fault """); @@ -97,16 +104,10 @@ } } evidence.Add(proof, "PASS runtime, certificate and checked array execution: " + name); - if (name != "ReadSByte") continue; - foreach (var (mutation, replacement) in new[] - { ("signedness", ".arrayLoad 1 false"), ("width", ".arrayLoad 2 true") }) - { - const string target = ".arrayLoad 1 true"; - if (!source.Contains(target, StringComparison.Ordinal)) throw new Exception("Missing mutation target"); - var forged = Path.Combine(root, "ForgedArray-" + mutation + ".lean"); - File.WriteAllText(forged, source.Replace(target, replacement, StringComparison.Ordinal)); - rejected.Add(forged, "PASS forged array operation: " + replacement, "ScalarFlowValid"); - } + // The decoder reads each element access from the body, so no emitted kind is left to forge; the + // mutations of Tests/ValueDecoding.lean reject another sign or width at the opcode. + if (!source.Contains("cil_decoded_method method0 ", StringComparison.Ordinal)) + throw new Exception("Element accessor is not decoded from its body: " + name); } } var compositions = new (string Name, string Signature, string Arguments, string Expected)[] @@ -224,7 +225,7 @@ inputMemory with #print axioms empty_array_is_live theorem negative_array_overflows : (match CIL.Safety.invoke Extracted.program 32 0 [.scalar (.i32 (-1))] inputMemory with - | .error fault => fault.fault == .thrown "System.OverflowException" + | .error fault => fault.fault == .thrown .overflow | .ok _ => false) = true := by decide +kernel #print axioms negative_array_overflows """); @@ -235,12 +236,17 @@ inputMemory with } catch (TargetInvocationException fault) when (fault.InnerException is OverflowException) { } } + // The decoder reads each allocation from the body, so a forgery names another element type at its raw token, + // which the certified stack states bind. + if (name is "Allocate" or "AllocateSigned8" or "AllocatePacked" && + !source.Contains("cil_decoded_method method0 ", StringComparison.Ordinal)) + throw new Exception("Allocation is not decoded from its body: " + name); if (name == "Allocate") { foreach (var (mutation, replacement) in new[] - { ("element", ".newArray .unsigned32"), ("operation", ".nop") }) + { ("element", ".type (.atom Extraction.runtimeScope \"System.UInt32\")") }) { - const string target = ".newArray .signed32"; + const string target = ".type (.atom Extraction.runtimeScope \"System.Int32\")"; if (!source.Contains(target, StringComparison.Ordinal)) throw new Exception("Missing newarr mutation"); var forged = Path.Combine(root, "ForgedAllocation-" + mutation + ".lean"); File.WriteAllText(forged, source.Replace(target, replacement, StringComparison.Ordinal)); @@ -249,16 +255,20 @@ inputMemory with } if (name == "AllocateSigned8") { - var target = $".newArray (.{element})"; + // The token names the enum and its underlying type; its storage is resolved from them. + var identity = System.Text.Json.JsonSerializer.Serialize(runtime.Assembly.FullName + "::Examples.Signed8"); + const string underlying = ".atom Extraction.runtimeScope \"System.SByte\""; + var target = $".type (.enum {identity} ({underlying}))"; foreach (var (mutation, replacement) in new[] { ("identity", target.Replace("Examples.Signed8", "Examples.Unsigned8", StringComparison.Ordinal)), - ("storage", target.Replace(".signed8)", ".unsigned8)", StringComparison.Ordinal)), - ("width", target.Replace(".signed8)", ".signed16)", StringComparison.Ordinal)), - ("primitive", ".newArray .signed8") + ("underlying", target.Replace("System.SByte", "System.Byte", StringComparison.Ordinal)), + ("width", target.Replace("System.SByte", "System.Int16", StringComparison.Ordinal)), + ("primitive", $".type ({underlying})") }) { if (!source.Contains(target, StringComparison.Ordinal)) throw new Exception("Missing enum newarr target"); + if (replacement == target) throw new Exception("Enum allocation mutation changes nothing: " + mutation); var forged = Path.Combine(root, "ForgedEnumAllocation-" + mutation + ".lean"); File.WriteAllText(forged, source.Replace(target, replacement, StringComparison.Ordinal)); rejected.Add(forged, "PASS forged enum allocation: " + mutation, "ScalarFlowValid"); @@ -266,26 +276,26 @@ inputMemory with } if (name == "AllocatePacked") { - var target = $".newArray (.{element})"; var identity = System.Text.Json.JsonSerializer.Serialize(runtime.Assembly.FullName + "::Examples.PackedLeaf"); var otherIdentity = identity.Replace("PackedLeaf", "Leaf", StringComparison.Ordinal); + // The token names the struct by identity and layout index; its stride is the checked layout's. + var prefix = $".type (.aggregate {identity} "; + var start = source.IndexOf(prefix, StringComparison.Ordinal); + if (start < 0) throw new Exception("Missing composite newarr token"); + var index = int.Parse(source.AsSpan(start + prefix.Length, + source.IndexOf(')', start) - start - prefix.Length), CultureInfo.InvariantCulture); + var target = $"{prefix}{index})"; var controls = new HashSet(StringComparer.Ordinal); foreach (var (mutation, before, after, obligation, diagnostic) in new[] { - ("width", target, target.Replace(" 5)", " 6)", StringComparison.Ordinal), - "method0_layouts_validated", "MethodLayoutsValid"), ("identity", target, target.Replace("PackedLeaf", "Leaf", StringComparison.Ordinal), "method0_scalars_validated", "ScalarFlowValid"), + ("index", target, $"{prefix}{index + 1})", "method0_coverage_validated", "LayoutUsesComplete"), ("layout-identity", $".aggregate {identity}", $".aggregate {otherIdentity}", - "method0_coverage_validated", "LayoutUsesComplete"), - ("byref-like", $"identity := {identity},", $"identity := {identity}, byRefLike := true,", - "method0_layouts_validated", "MethodLayoutsValid"), - ("zero-width", target, target.Replace(" 5)", " 0)", StringComparison.Ordinal), - "method0_scalars_validated", "ScalarFlowValid"), - ("oversized", target, target.Replace(" 5)", " 65536)", StringComparison.Ordinal), - "method0_scalars_validated", "ScalarFlowValid") + "method0_coverage_validated", "LayoutUsesComplete") }) { + if (before == after) throw new Exception("Composite allocation mutation changes nothing: " + mutation); var narrowed = NarrowEvidence(source, obligation); if (controls.Add(obligation)) File.AppendAllText(proof, @@ -320,7 +330,9 @@ inputMemory with var output = Path.Combine(root, name); var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); var proof = Path.Combine(root, name + ".lean"); - File.WriteAllText(proof, source + $$""" + File.WriteAllText(proof, $$""" + import CIL.Safety.StepComposition + {{source}} private def fillMemory : CIL.Safety.Memory := { allocations := fun identity => if identity == 0 then @@ -356,12 +368,12 @@ match CIL.Safety.invoke Extracted.program 128 0 match CIL.Safety.invoke Extracted.program 128 0 [.array ⟨.address ⟨0, 4⟩, 4, .signed32⟩, .scalar (.i32 7), .scalar (.i32 start), .scalar (.i32 count)] fillMemory with - | .error fault => fault.fault == .thrown "System.ArgumentOutOfRangeException" + | .error fault => fault.fault == .thrown .argumentOutOfRange | .ok _ => false) = true ∧ ([.object .null, .array ⟨.null, 0, .signed32⟩].all fun input => match CIL.Safety.invoke Extracted.program 128 0 [input, .scalar (.i32 7), .scalar (.i32 (-1)), .scalar (.i32 (-1))] fillMemory with - | .error fault => fault.fault == .thrown "System.ArgumentNullException" + | .error fault => fault.fault == .thrown .argumentNull | .ok _ => false) = true ∧ (match CIL.Safety.invoke Extracted.program 128 0 [.array ⟨.address ⟨0, 4⟩, 4, .signed32⟩, .scalar (.i32 7), @@ -390,24 +402,6 @@ match CIL.Safety.arrayFillInstruction .signed32 counted constructor <;> decide +kernel #print axioms malformed_fill_states """); - var obligation = "method1_scalars_validated"; - var narrowed = NarrowEvidence(source, obligation); - File.AppendAllText(proof, "\n" + CheckedEvidenceControl(source, narrowed, [obligation], 0)); - foreach (var (label, before, after) in new[] - { - ("range", ".arrayFill .signed32 true", ".arrayFill .signed32 false"), - ("index-type", ".atom Extraction.runtimeScope \"System.Int32\", " + - ".atom Extraction.runtimeScope \"System.Int32\"]", - ".atom Extraction.runtimeScope \"System.UInt32\", " + - ".atom Extraction.runtimeScope \"System.Int32\"]") - }) - { - if (!narrowed.Contains(before, StringComparison.Ordinal)) - throw new Exception("Missing range fill mutation: " + label); - var forged = Path.Combine(root, "ForgedFillRange-" + label + ".lean"); - File.WriteAllText(forged, narrowed.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(forged, "PASS forged array fill range: " + label, "ScalarFlowValid"); - } foreach (var (start, count) in new[] { (1, 2), (0, 0), (4, 0), (-1, 0), (0, -1), (5, 0), (3, 2), (1, int.MaxValue) }) { @@ -441,7 +435,7 @@ match CIL.Safety.arrayFillInstruction .signed32 counted | .ok result => result.2 == [] | .error _ => false) = true ∧ ([.object .null, .array ⟨.null, 0, .signed32⟩].all fun input => match CIL.Safety.invoke Extracted.program 128 0 [input, .scalar (.i32 7)] fillMemory with - | .error fault => fault.fault == .thrown "System.ArgumentNullException" + | .error fault => fault.fault == .thrown .argumentNull | .ok _ => false) = true ∧ (match CIL.Safety.invoke Extracted.program 128 0 [.array ⟨.address ⟨0, 8⟩, 2, .signed32⟩, .scalar (.i32 7)] @@ -451,25 +445,6 @@ match CIL.Safety.arrayFillInstruction .signed32 counted all_goals decide +kernel #print axioms empty_and_null_fill """); - var obligation = "method1_scalars_validated"; - var narrowed = NarrowEvidence(source, obligation); - File.AppendAllText(proof, "\n" + CheckedEvidenceControl(source, narrowed, [obligation], 0)); - foreach (var (label, before, after) in new[] - { - ("element", ".arrayFill .signed32 false", ".arrayFill .unsigned32 false"), - ("width", ".arrayFill .signed32 false", ".arrayFill .signed64 false"), - ("operation", ".arrayFill .signed32 false", ".spanFill 4 false"), - ("arity", "parameterCount := 2, hasThis := false, returnsValue := false", - "parameterCount := 4, hasThis := false, returnsValue := false"), - ("scope", ".atom Extraction.runtimeScope \"System.Array\"", ".atom \"Wrong.Scope\" \"System.Array\"") - }) - { - if (!narrowed.Contains(before, StringComparison.Ordinal)) - throw new Exception("Missing array fill mutation: " + label); - var forged = Path.Combine(root, "ForgedFill-" + label + ".lean"); - File.WriteAllText(forged, narrowed.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(forged, "PASS forged array fill: " + label, "ScalarFlowValid"); - } var values = new[] { 1, 2 }; runtime.GetMethod(name)!.Invoke(null, [values, 7]); if (!values.SequenceEqual([7, 7])) throw new Exception("Runtime array fill mismatch"); @@ -489,29 +464,10 @@ all_goals decide +kernel runtime.GetMethod(name)!.Invoke(null, counted ? [values, value, 0, 2] : [values, value]); if (!Equals(values.GetValue(0), value) || !Equals(values.GetValue(1), value)) throw new Exception("Runtime enum array fill mismatch"); - if (counted) - { - evidence.Add(proof, "PASS generic array fill: " + name); - continue; - } - var obligation = "method1_scalars_validated"; - var narrowed = NarrowEvidence(source, obligation); - File.AppendAllText(proof, "\n" + CheckedEvidenceControl(source, narrowed, [obligation], 0)); - var mode = counted ? "true" : "false"; - var target = $".arrayFill (.enumeration {fillEnum} .unsigned8) {mode}"; - if (!narrowed.Contains(target, StringComparison.Ordinal)) - throw new Exception("Missing enum array fill descriptor"); - foreach (var (label, replacement) in new[] - { - ("identity", $".arrayFill (.enumeration \"Wrong.Enum\" .unsigned8) {mode}"), - ("backing", $".arrayFill (.enumeration {fillEnum} .signed8) {mode}") - }) - { - var forged = Path.Combine(root, "Forged" + name + "-" + label + ".lean"); - File.WriteAllText(forged, narrowed.Replace(target, replacement, StringComparison.Ordinal)); - rejected.Add(forged, "PASS forged enum array fill: " + label, "ScalarFlowValid"); - } } + var shape = element.StartsWith("enumeration", StringComparison.Ordinal) ? $"(.{element})" : $".{element}"; + var mode = counted ? "true" : "false"; + File.AppendAllText(proof, BulkCallEvidence(1, counted ? 4 : 2, $".arrayFill {shape} {mode}")); evidence.Add(proof, "PASS generic array fill: " + name); } evidence.Check(); diff --git a/Tests/PrimitiveArrays.lean b/Tests/PrimitiveArrays.lean index 550515a05..0d9cf12ed 100644 --- a/Tests/PrimitiveArrays.lean +++ b/Tests/PrimitiveArrays.lean @@ -1,4 +1,6 @@ import CIL.Safety.Execution +import CIL.Safety.ObjectSteps +import CIL.Safety.SymbolicExecution import CIL.Extraction.Translation /-! # Supplied-array operations retain null, bounds, initialization and authority checks -/ @@ -9,35 +11,66 @@ open CIL.Safety private def rawArray (opcode : Nat) (operand : CIL.Extraction.RawOperand := .none) : CIL.Extraction.RawInstruction := ⟨0, opcode, operand⟩ -/-- A typed load cannot claim an element token differing from the supplied array type. -/ +/-- The element type `System.Int32`. -/ +private def int32 : CIL.Extraction.TypeExpr := .atom CIL.Extraction.runtimeScope "System.Int32" + +/-- The struct `Record`, at layout index 0. -/ +private def record : CIL.Extraction.TypeExpr := .aggregate "Record" 0 + +/-- The element type `System.UInt32`. -/ +private def uint32 : CIL.Extraction.TypeExpr := .atom CIL.Extraction.runtimeScope "System.UInt32" + +/-- A static method without arguments; the stack transfers below read none of its fields. -/ +private def holder : CIL.Extraction.RawMethod := { + token := 0x06000001, owner := "Scope::Fixture", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [], locals := [], initLocals := true, maxStack := 3, genericParameters := [], + code := [] } + +/-- The stack kinds the raw array instruction `opcode` leaves on `stack`. -/ +private def transfer (opcode : Nat) (stack : List CIL.Extraction.ScalarKind) : + Option (List CIL.Extraction.ScalarKind) := + CIL.Extraction.scalarTransfer holder (rawArray opcode) stack + +/-- A typed load cannot claim an element token differing from the supplied array type, and emits + exactly the element type its token names. -/ theorem wrong_element_token : - CIL.Extraction.scalarOpMatches - (rawArray 163 (.type (.atom CIL.Extraction.runtimeScope "System.UInt32"))) - [.word32, .array .signed32] (.memory (.arrayLoad 4 false)) = false := by decide +kernel + CIL.Extraction.scalarTransfer holder (rawArray 163 (.type uint32)) + [.word32, .array .signed32] = none ∧ + CIL.Extraction.scalarTransfer holder (rawArray 163 (.type int32)) + [.word32, .array .signed32] = some [.word32] ∧ + CIL.Extraction.scalarOpMatches none (rawArray 163 (.type uint32)) [.word32, .array .unsigned32] + (.ldelem_any int32) = false ∧ + CIL.Extraction.scalarOpMatches none (rawArray 163 (.type uint32)) [.word32, .array .unsigned32] + (.ldelem_any uint32) = true := by decide +kernel #print axioms wrong_element_token /-- Equal physical widths do not make native and fixed-width array accesses interchangeable. -/ theorem native_element_mismatch : - CIL.Extraction.scalarOpMatches (rawArray 151) [.word32, .array .signed64] - (.memory (.arrayLoad 8 false)) = false := by decide +kernel + transfer 151 [.word32, .array .nativeInt] = some [.native] ∧ + transfer 151 [.word32, .array .signed64] = none ∧ + transfer 150 [.word32, .array .nativeInt] = none := by decide +kernel #print axioms native_element_mismatch /-- A native-width array store requires a native stack value, not an int64 claim. -/ theorem native_store_mismatch : - CIL.Extraction.scalarOpMatches (rawArray 155) [.word64, .word32, .array .nativeInt] - (.memory (.arrayStore 8)) = false := by decide +kernel + transfer 155 [.native, .word32, .array .nativeInt] = some [] ∧ + transfer 155 [.word64, .word32, .array .nativeInt] = none := by decide +kernel #print axioms native_store_mismatch /-- Unsupported floating-point loads cannot certify an integer load via equal stack effects. -/ theorem float_load_forgery : - CIL.Extraction.scalarEmissionMatches #[] none (rawArray 152) [.word32, .array .signed32] - (.memory (.arrayLoad 4 false)) = false := by decide +kernel + CIL.Extraction.scalarEmissionMatches none none (rawArray 152) [.word32, .array .signed32] + (.ldelem .i4) = false ∧ + CIL.Extraction.scalarEmissionMatches none none (rawArray 152) [.word32, .array .signed32] + (.ldelem_any int32) = false := by decide +kernel #print axioms float_load_forgery /-- An ordinary int64 is not a valid array index in raw CIL stack evidence. -/ theorem invalid_index_kind : - CIL.Extraction.scalarOpMatches (rawArray 148) [.word64, .array .signed32] - (.memory (.arrayLoad 4 false)) = false := by decide +kernel + transfer 148 [.word32, .array .signed32] = some [.word32] ∧ + transfer 148 [.native, .array .signed32] = some [.word32] ∧ + transfer 148 [.word64, .array .signed32] = none := by decide +kernel #print axioms invalid_index_kind private def memory : Memory := { @@ -53,123 +86,186 @@ private def bytes : ArrayView := ⟨.address ⟨0, 0⟩, 16, .signed8⟩ private def noAuthority : Memory := { memory with views := [] } private def uninitialized : Memory := { memory with cells := fun _ _ => ⟨0, false⟩ } +/-- Distinct high-bit bytes distinguish load widths, byte order and narrow sign extension. -/ +private def numericMemory : Memory := + { memory with cells := fun _ offset => + { bits := BitVec.ofNat 8 (128 + offset), initialized := true } } + +/-- Every admitted numeric suffix, including distinct signed and unsigned encodings. -/ +private def numericKinds : List CIL.NumericKind := [.i1, .u1, .i2, .u2, .i4, .u4, .i8, .i] + +/-- The `ldind` of a kind. -/ +private def numericLoad (kind : CIL.NumericKind) : MemoryStep := instruction (.ldind kind) + +/-- Every suffix loads its exact value, with sign or zero extension for the narrow kinds. -/ +theorem numeric_kind_load_values : + numericKinds.map (fun kind => + (numericLoad kind [.reference (.address { allocation := 0, offset := 0 })] + numericMemory).map Prod.snd) = + [.ok [.scalar (.i32 0xFFFFFF80)], .ok [.scalar (.i32 0x80)], + .ok [.scalar (.i32 0xFFFF8180)], .ok [.scalar (.i32 0x8180)], + .ok [.scalar (.i32 0x83828180)], .ok [.scalar (.i32 0x83828180)], + .ok [.scalar (.i64 0x8786858483828180)], + .ok [.scalar (.i64 0x8786858483828180)]] := rfl +#print axioms numeric_kind_load_values + +/-- The `stind` of a kind. -/ +private def numericStore (kind : CIL.NumericKind) : MemoryStep := instruction (.stind kind) + +/-- Each store keeps the low bytes in little-endian order and preserves the following bytes. -/ +theorem numeric_kind_store_bytes : + ([(.i1, .i32 0x44332211), (.u1, .i32 0x44332211), + (.i2, .i32 0x44332211), (.u2, .i32 0x44332211), + (.i4, .i32 0x44332211), (.u4, .i32 0x44332211), + (.i8, .i64 0x8877665544332211), (.i, .i64 0x8877665544332211)] : + List (Prod CIL.NumericKind CIL.Value)).map (fun (kind, value) => do + let (stored, _) <- numericStore kind + [.scalar value, .reference (.address { allocation := 0, offset := 0 })] numericMemory + checkedAt (.address { allocation := 0, offset := 0 }) 8 + (read stored { allocation := 0, offset := 0 } 8 1)) = + [.ok [0x11, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87], + .ok [0x11, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87], + .ok [0x11, 0x22, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87], + .ok [0x11, 0x22, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87], + .ok [0x11, 0x22, 0x33, 0x44, 0x84, 0x85, 0x86, 0x87], + .ok [0x11, 0x22, 0x33, 0x44, 0x84, 0x85, 0x86, 0x87], + .ok [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88], + .ok [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]] := rfl +#print axioms numeric_kind_store_bytes + /-- Length is native-sized and does not read array data or require read authority. -/ theorem length_without_authority : - memoryInstruction .arrayLength [.array words] noAuthority = + elementInstruction .ldlen [.array words] noAuthority = .ok (noAuthority, [.scalar (.i64 4)]) := rfl #print axioms length_without_authority /-- A non-null empty array has length zero. -/ theorem empty_length : - memoryInstruction .arrayLength [.array ⟨.address ⟨1, 0⟩, 0, .signed32⟩] memory = + elementInstruction .ldlen [.array ⟨.address ⟨1, 0⟩, 0, .signed32⟩] memory = .ok (memory, [.scalar (.i64 0)]) := rfl #print axioms empty_length /-- Null is distinct from an empty array and raises the named runtime exception. -/ theorem null_length : - memoryInstruction .arrayLength [.array ⟨.null, 0, .signed32⟩] memory = - .error (.thrown "System.NullReferenceException") := rfl + elementInstruction .ldlen [.array ⟨.null, 0, .signed32⟩] memory = + .error (.thrown .nullReference) := rfl #print axioms null_length /-- The raw null literal has the same runtime array-null behavior. -/ theorem literal_null_length : - memoryInstruction .arrayLength [.object .null] memory = - .error (.thrown "System.NullReferenceException") := rfl + elementInstruction .ldlen [.object .null] memory = + .error (.thrown .nullReference) := rfl #print axioms literal_null_length /-- A null byref, as `Unsafe.NullRef` produces, is not an array and has no length. -/ theorem byref_null_not_array : - memoryInstruction .arrayLength [.reference .null] memory = .error .invalidState := rfl + elementInstruction .ldlen [.reference .null] memory = .error .invalidState := rfl #print axioms byref_null_not_array /-- Narrow signed loads extend the stored byte. -/ theorem signed_byte_read : - memoryInstruction (.arrayLoad 1 true) [.scalar (.i32 0), .array bytes] memory = + typedElementInstruction (.value .signed8) false [.scalar (.i32 0), .array bytes] memory = .ok (memory, [.scalar (.i32 4294967295)]) := rfl #print axioms signed_byte_read /-- The unsigned encoding reads the same byte without sign extension. -/ theorem unsigned_byte_read : - memoryInstruction (.arrayLoad 1 false) [.scalar (.i32 0), .array bytes] memory = + typedElementInstruction (.value .unsigned8) false [.scalar (.i32 0), .array bytes] memory = .ok (memory, [.scalar (.i32 255)]) := rfl #print axioms unsigned_byte_read /-- Native indices are accepted without truncating them to int32. -/ theorem native_index : - memoryInstruction (.arrayLoad 4 false) [.scalar (.i64 0), .array words] memory = + typedElementInstruction (.value .signed32) false [.scalar (.i64 0), .array words] memory = .ok (memory, [.scalar (.i32 255)]) := rfl #print axioms native_index /-- Index equal to length is outside the array even if its address could be a sentinel. -/ theorem end_index_rejected : - memoryInstruction (.arrayLoad 4 false) [.scalar (.i32 4), .array words] memory = - .error (.thrown "System.IndexOutOfRangeException") := rfl + typedElementInstruction (.value .signed32) false [.scalar (.i32 4), .array words] memory = + .error (.thrown .indexOutOfRange) := rfl #print axioms end_index_rejected /-- Negative int32 indexes are out of bounds. -/ theorem negative_index_rejected : - memoryInstruction (.arrayLoad 4 false) [.scalar (.i32 4294967295), .array words] memory = - .error (.thrown "System.IndexOutOfRangeException") := rfl + typedElementInstruction (.value .signed32) false + [.scalar (.i32 4294967295), .array words] memory = + .error (.thrown .indexOutOfRange) := rfl #print axioms negative_index_rejected /-- A high native index cannot wrap into the array. -/ theorem high_native_index_rejected : - memoryInstruction (.arrayLoad 4 false) + typedElementInstruction (.value .signed32) false [.scalar (.i64 18446744073709551615), .array words] memory = - .error (.thrown "System.IndexOutOfRangeException") := rfl + .error (.thrown .indexOutOfRange) := rfl #print axioms high_native_index_rejected /-- Null is checked before index bounds. -/ theorem null_before_bounds : - memoryInstruction (.arrayLoad 4 false) + typedElementInstruction (.value .signed32) false [.scalar (.i32 4294967295), .array ⟨.null, 0, .signed32⟩] memory = - .error (.thrown "System.NullReferenceException") := rfl + .error (.thrown .nullReference) := rfl #print axioms null_before_bounds /-- Array shape does not grant read authority. -/ theorem unreadable_rejected : - memoryInstruction (.arrayLoad 4 false) [.scalar (.i32 0), .array words] noAuthority = + typedElementInstruction (.value .signed32) false [.scalar (.i32 0), .array words] noAuthority = .error (.memory .unreadable (.address ⟨0, 0⟩) 4) := rfl #print axioms unreadable_rejected /-- An array value cannot make uninitialized data readable. -/ theorem uninitialized_rejected : - memoryInstruction (.arrayLoad 4 false) [.scalar (.i32 0), .array words] uninitialized = + typedElementInstruction (.value .signed32) false + [.scalar (.i32 0), .array words] uninitialized = .error (.memory .uninitialized (.address ⟨0, 0⟩) 4) := rfl #print axioms uninitialized_rejected /-- Writes need authority even though the array itself has no readonly-view flag. -/ theorem unwritable_rejected : - memoryInstruction (.arrayStore 4) [.scalar (.i32 7), .scalar (.i32 1), .array words] + typedElementInstruction (.value .signed32) true + [.scalar (.i32 7), .scalar (.i32 1), .array words] noAuthority = .error (.memory .unwritable (.address ⟨0, 4⟩) 4) := rfl #print axioms unwritable_rejected /-- The selected element is initialized by a store without reading its previous contents. -/ theorem store_initializes_element : (do - let (updated, _) ← memoryInstruction (.arrayStore 4) + let (updated, _) ← typedElementInstruction (.value .signed32) true [.scalar (.i32 305419896), .scalar (.i32 1), .array words] uninitialized - memoryInstruction (.arrayLoad 4 false) [.scalar (.i32 1), .array words] updated : + typedElementInstruction (.value .signed32) false [.scalar (.i32 1), .array words] updated : ExecutionResult (Memory × List Value)).map Prod.snd = .ok [.scalar (.i32 305419896)] := rfl #print axioms store_initializes_element /-- Bytes on both sides of the updated element retain their initial values. -/ theorem store_preserves_neighbors : - (memoryInstruction (.arrayStore 4) + (typedElementInstruction (.value .signed32) true [.scalar (.i32 7), .scalar (.i32 1), .array words] memory).map (fun (updated, _) => (updated.cells 0 0, updated.cells 0 8)) = .ok (memory.cells 0 0, memory.cells 0 8) := rfl #print axioms store_preserves_neighbors +/-- A typed access to an element of no inline storage faults: a class element is an ill-typed + state, and a by-ref-like struct or floating-point element is unsupported. -/ +theorem typed_shapes_refused : + typedElementInstruction (.reference int32) false [.scalar (.i32 0), .array words] memory = + .error .invalidState ∧ + typedElementInstruction (.unsupported CIL.ElementShape.byRefLikeElement) false + [.scalar (.i32 0), .array words] memory = + .error (.unsupported CIL.ElementShape.byRefLikeElement) ∧ + typedElementInstruction (.unsupported CIL.ElementShape.unsupportedType) true + [.scalar (.i32 0), .scalar (.i32 0), .array words] memory = + .error (.unsupported CIL.ElementShape.unsupportedType) := ⟨rfl, rfl, rfl⟩ +#print axioms typed_shapes_refused + /-- A valid array cannot be read using an unrelated element width. -/ theorem wrong_width_rejected : - memoryInstruction (.arrayLoad 8 false) [.scalar (.i32 0), .array words] memory = + typedElementInstruction (.value .signed64) false [.scalar (.i32 0), .array words] memory = .error .invalidState := rfl #print axioms wrong_width_rejected /-- Invalid backing extents fail as model errors, not as expected runtime exceptions. -/ theorem invalid_extent_rejected : - memoryInstruction .arrayLength [.array { words with length := 5 }] memory = + elementInstruction .ldlen [.array { words with length := 5 }] memory = .error (.memory .outsideAllocation (.address ⟨0, 0⟩) 0) := rfl #print axioms invalid_extent_rejected @@ -213,7 +309,7 @@ theorem allocation_identity_reuse_is_not_exception : /-- Native negative lengths retain the CLI overflow exception. -/ theorem native_negative_array_length : (match newArray memory .signed32 (.i64 (2^63)) with - | .error fault => fault == .thrown "System.OverflowException" + | .error fault => fault == .thrown .overflow | .ok _ => false) = true := by decide +kernel #print axioms native_negative_array_length @@ -253,12 +349,57 @@ theorem byref_like_layout_containment : /-- Native length operands certify, while raw int64 stack kinds do not. -/ theorem allocation_native_kind : - CIL.Extraction.scalarOpMatches + CIL.Extraction.scalarOpMatches none (rawArray 141 (.type (.atom CIL.Extraction.runtimeScope "System.Int32"))) - [.native] (.newArray .signed32) = true ∧ - CIL.Extraction.scalarOpMatches + [.native] (.newarr int32) = true ∧ + CIL.Extraction.scalarOpMatches none (rawArray 141 (.type (.atom CIL.Extraction.runtimeScope "System.Int32"))) - [.word64] (.newArray .signed32) = false := by decide +kernel + [.word64] (.newarr int32) = false := by decide +kernel #print axioms allocation_native_kind +/-- `newarr` carries the raw token itself: another element type, even one of equal width, or + the same struct under another identity or layout index, does not match it. -/ +theorem allocation_token_mismatch : + CIL.Extraction.scalarOpMatches none (rawArray 141 (.type int32)) [.word32] + (.newarr (.atom CIL.Extraction.runtimeScope "System.UInt32")) = false ∧ + CIL.Extraction.scalarOpMatches none (rawArray 141 (.type record)) [.word32] + (.newarr (.aggregate "Other" 0)) = false ∧ + CIL.Extraction.scalarOpMatches none (rawArray 141 (.type record)) [.word32] + (.newarr (.aggregate "Record" 1)) = false ∧ + CIL.Extraction.scalarOpMatches none (rawArray 141 (.type record)) [.word32] + (.newarr record) = true := by decide +kernel +#print axioms allocation_token_mismatch + +/-- Checked layouts holding one 5-byte struct under the identity `Record`. -/ +private def recordEnvironment : CIL.LayoutEnvironment := + { rawLayouts := + [{ identity := "Record", explicit := false, packing := 0, declaredSize := 0, fields := [] }] + layouts := #[⟨⟨5, []⟩, 1⟩] } + +/-- `newarr` of two `Record` elements in a body over `recordEnvironment`. -/ +@[cil_code] def aggregating : CIL.Method := + { returnsValue := true, locals := [], environment := recordEnvironment, + code := [.ldc_i4 2, .newarr record, .ret] } + +/-- A struct array takes its stride from the body's checked layout, not from the instruction; + without a layout entry for the struct the allocation is outside the model. -/ +theorem aggregate_stride_from_environment : + (match invoke [aggregating] 8 0 [] memory with + | .ok (after, [.array view]) => view.elementType == .aggregate "Record" 5 && + view.length == 2 && (view.validate after).isOk + | _ => false) = true ∧ + invoke [{ aggregating with environment := { recordEnvironment with layouts := #[] } }] + 8 0 [] memory = .error ⟨0, 1, .unsupported CIL.ElementShape.outsideLayouts⟩ := + ⟨by decide +kernel, rfl⟩ +#print axioms aggregate_stride_from_environment + +/-- A `newarr` step reduces through the body's element shape to a value allocation of the + checked stride. -/ +theorem aggregate_allocation_step (frame : Frame) (rest : List Value) : + step aggregating (.newarr record) 1 [] frame (.scalar (.i32 2) :: rest) memory = do + let (view, allocated) ← newArray memory (.aggregate "Record" 5) (.i32 2) + return .next 2 (.array view :: rest) frame allocated := by + simp only [cil_step, bind_assoc, pure_bind, Nat.reduceAdd] +#print axioms aggregate_allocation_step + end Tests.PrimitiveArrays diff --git a/Tests/PrimitiveOperations.cs b/Tests/PrimitiveOperations.cs index 36158cd0a..8a912d7c7 100644 --- a/Tests/PrimitiveOperations.cs +++ b/Tests/PrimitiveOperations.cs @@ -2,6 +2,13 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include StoreReturnVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Buffers.Binary; @@ -338,19 +345,11 @@ string FixtureSelector(string dll) method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); }); var wrongOutput = Path.Combine(root, "WrongWrite"); -var failed = false; -try -{ - verification.Verify(wrongDll, "Examples", "PrimitiveOperations::ReadWrite(int&,int)", - At("Examples/Proofs/PrimitiveReadWrite.lean"), wrongOutput); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - failed = true; -} -if (!failed || File.Exists(Path.Combine(wrongOutput, "verified.json"))) - throw new InvalidOperationException("Incorrect write accepted"); +verification.VerifyStoreReturn(assembly, "PrimitiveOperations::ReadWrite(int&,int)", + At("Examples/Proofs/PrimitiveReadWrite.lean"), + wrongDll, "PrimitiveOperations::ReadWrite(int&,int)", wrongOutput, 32); queue.Flush(); Console.WriteLine($"Primitive operation proofs passed: {cases} cases; incorrect write rejected."); +Run(["dotnet", "run", "--file", At("Tests/Strings.cs")], streamOutput: true, progress: "Strings as opaque references"); diff --git a/Tests/ProfileContract.lean b/Tests/ProfileContract.lean index 382f59e1f..336cb056a 100644 --- a/Tests/ProfileContract.lean +++ b/Tests/ProfileContract.lean @@ -1,4 +1,6 @@ import CIL.Safety.ProfileContract +import Tests.Support +import Tests.Support.IntrinsicMethods /-! A contract proved once under one feature profile and carried to other profiles by `MethodContract.Holds.reprofile` and its outcome forms, with the profile agreement decided @@ -22,9 +24,10 @@ def avx512Profile : FeatureProfile := /-- x86-64-v3 with 256-bit vectors disabled: agrees on AVX2, differs in acceleration. -/ def narrowProfile : FeatureProfile := { avx2Profile with vector256Accelerated := false } -/-- The x86-64 baseline: AVX2 is off, so the feature test answers differently. -/ +/-- The x86-64-v2 baseline: AVX2 is off, so the feature test answers differently. -/ def baselineProfile : FeatureProfile := - { architecture := .x64, x86Base64 := true, sse2 := true, vector128Accelerated := true } + { architecture := .x64, x86Base64 := true, sse2 := true, ssse3 := true, sse41 := true, + sse42 := true, vector128Accelerated := true } /-- Every profile the contract is carried between is a supported runtime configuration. -/ theorem profiles_valid : avx2Profile.Valid ∧ avx512Profile.Valid ∧ narrowProfile.Valid ∧ @@ -33,9 +36,9 @@ theorem profiles_valid : avx2Profile.Valid ∧ avx512Profile.Valid ∧ narrowPro /-- Method 0 is `IsSupported(Avx2) ? 1 : 0`; method 1 always throws. Both use `avx2Profile`. -/ @[cil_code] def program : Program := [{ profile := avx2Profile, returnsValue := true, locals := [], - code := [.feature .avx2, .ret] }, + code := [.call (Tests.supportedGetter "X86.Avx2"), .ret] }, { profile := avx2Profile, returnsValue := false, locals := [], - code := [.throw "System.PlatformNotSupportedException"] }] + code := [.throw .notSupported] }] /-- Calling with no arguments returns `1` and changes no memory. -/ def avx2Contract : MethodContract Unit where @@ -65,9 +68,9 @@ theorem narrow_holds : avx2Contract.Holds (reprofile program narrowProfile) 0 := /-- The same bodies written with their own profile, as a per-profile extraction would be. -/ @[cil_code] def avx512Program : Program := [{ profile := avx512Profile, returnsValue := true, locals := [], - code := [.feature .avx2, .ret] }, + code := [.call (Tests.supportedGetter "X86.Avx2"), .ret] }, { profile := avx512Profile, returnsValue := false, locals := [], - code := [.throw "System.PlatformNotSupportedException"] }] + code := [.throw .notSupported] }] /-- A per-profile program is certified once it is shown to be the reprofiling. -/ theorem extraction_holds : avx2Contract.Holds avx512Program 0 := by @@ -78,8 +81,8 @@ theorem extraction_holds : avx2Contract.Holds avx512Program 0 := by theorem avx2_family_holds (target : FeatureProfile) (wide : target.avx2 = true) : avx2Contract.Holds (reprofile program target) 0 := avx2_holds.reprofile avx2Profile uniform (Program.ProfileAgreement.of_flags _ _ _ (by - simp [Program.profileReads, Op.profileReads, program, avx2Profile, FeatureProfile.evaluate, - wide])) + change ∀ f ∈ [Feature.avx2], avx2Profile.evaluate f = target.evaluate f + simpa [avx2Profile, FeatureProfile.evaluate] using wide.symm)) /-- The normal-return outcome form carries over: method 0 never raises. -/ theorem outcome_holds : @@ -94,7 +97,7 @@ def throwContract : OutcomeContract Unit where pre _ _ := True footprint _ := [] post _ _ _ _ := False - raises _ _ := some "System.PlatformNotSupportedException" + raises _ _ := some .notSupported /-- Under `avx2Profile` method 1 raises the named exception. -/ theorem throw_holds : throwContract.Holds program 1 := by @@ -139,23 +142,21 @@ def emptyMemory : CIL.Safety.Memory := { allocations := fun _ => none, cells := fun _ _ => ⟨0, false⟩, views := [], nextIdentity := 0 } /-- The contract really depends on AVX2: on the baseline the method returns `0`. -/ -theorem baseline_rejected : ¬ avx2Contract.Holds (reprofile program baselineProfile) 0 := by - intro holds - obtain ⟨fuel, ⟨final, values⟩, invoked, -, out, encoded, returned⟩ := - holds () emptyMemory trivial +theorem baseline_rejected : avx2Contract.Refuted (reprofile program baselineProfile) 0 := by have baselineRun : CIL.Safety.invoke (reprofile program baselineProfile) 10 0 [] emptyMemory = Except.ok (emptyMemory, [.scalar (.i32 0)]) := by cil_checked_steps [reprofile, program, baselineProfile] - have same := invoke_ok_unique invoked baselineRun + refine .of_return (input := ()) (initial := emptyMemory) trivial baselineRun ?_ + rintro ⟨-, out, encoded, returned⟩ simp only [avx2Contract] at encoded returned - subst encoded returned - simp at same + subst returned + simp at encoded /-- An AVX-512 DQ multiply needs four flags, all of which the agreement check reads. -/ def intrinsicProgram : Program := [{ profile := avx512Profile, returnsValue := true, locals := [], - code := [.arg 0, .arg 1, .intrinsic (.avx512DQ .mul64) 2, .ret] }] + code := [.ldarg 0, .ldarg 1, .call Tests.IntrinsicMethods.multiplyLow, .ret] }] /-- Profiles that agree on the four flags agree on the multiply. -/ theorem intrinsic_agrees : intrinsicProgram.ProfileAgreement avx512Profile @@ -173,11 +174,13 @@ theorem intrinsic_check_rejects : ¬ ∀ featureId ∈ intrinsicProgram.profileR theorem intrinsic_disagrees : ¬ intrinsicProgram.ProfileAgreement avx512Profile { avx512Profile with avx512DQVL := false } := by intro agreement - have available := agreement _ (List.mem_singleton_self _) (.intrinsic (.avx512DQ .mul64) 2) + have available := agreement _ (List.mem_singleton_self _) (.call Tests.IntrinsicMethods.multiplyLow) (by simp) - simp [Op.ProfileAgreement, Intrinsic.available, avx512Profile, avx2Profile] at available + have enabled := available _ (Tests.IntrinsicMethods.multiplyLow_model _) + change true = false at enabled + contradiction -/-- info: 'Tests.ProfileContract.uniform' does not depend on any axioms -/ +/-- info: 'Tests.ProfileContract.uniform' depends on axioms: [propext] -/ #guard_msgs in #print axioms uniform /-- info: 'Tests.ProfileContract.avx2_holds' depends on axioms: [propext, Quot.sound] -/ #guard_msgs in #print axioms avx2_holds diff --git a/Tests/ProfileEmbedding.lean b/Tests/ProfileEmbedding.lean index 26e04866b..cffe88ac5 100644 --- a/Tests/ProfileEmbedding.lean +++ b/Tests/ProfileEmbedding.lean @@ -1,4 +1,5 @@ import CIL.Embedding.Tactic +import Tests.Support /-! # Profile-parametric method embeddings @@ -28,29 +29,29 @@ theorem profiles_valid : portable.Valid ∧ narrow.Valid ∧ wide.Valid := by de #print axioms profiles_valid /-- Code removed by profile extraction has no live instruction to match. -/ -abbrev unreachable : Op := .unsupported "Unreachable under selected profile" +abbrev unreachable : Instruction := .unadmitted 0 /-- One body over two feature flags and a callee, shared by all extraction profiles. -/ def template (profile : FeatureProfile) (sse42 avx2 : Bool) (callee : Nat) : Method := { - profile, returnsValue := true, locals := [], code := [ - .feature .avx2, .brzero 6, - if avx2 then .arg 0 else unreachable, - if avx2 then .arg 1 else unreachable, - if avx2 then .call callee 2 else unreachable, + profile, returnsValue := true, locals := [], links := [(4, .direct callee)], code := [ + .call (Tests.supportedGetter "X86.Avx2"), .brfalse 6, + if avx2 then .ldarg 0 else unreachable, + if avx2 then .ldarg 1 else unreachable, + if avx2 then .call (Tests.callee 2) else unreachable, if avx2 then .ret else unreachable, - if avx2 then unreachable else if sse42 then .arg 1 else .arg 0, + if avx2 then unreachable else if sse42 then .ldarg 1 else .ldarg 0, if avx2 then unreachable else .ret] } /-- An extracted layout has two extra dead instructions before the scalar branch target. -/ def extracted (profile : FeatureProfile) (callee : Nat) : Method := { - profile, returnsValue := true, locals := [], code := [ - .feature .avx2, .brzero 8, - if profile.avx2 then .arg 0 else unreachable, - if profile.avx2 then .arg 1 else unreachable, - if profile.avx2 then .call callee 2 else unreachable, + profile, returnsValue := true, locals := [], links := [(4, .direct callee)], code := [ + .call (Tests.supportedGetter "X86.Avx2"), .brfalse 8, + if profile.avx2 then .ldarg 0 else unreachable, + if profile.avx2 then .ldarg 1 else unreachable, + if profile.avx2 then .call (Tests.callee 2) else unreachable, if profile.avx2 then .ret else unreachable, unreachable, unreachable, - if profile.avx2 then unreachable else if profile.sse42 then .arg 1 else .arg 0, + if profile.avx2 then unreachable else if profile.sse42 then .ldarg 1 else .ldarg 0, if profile.avx2 then unreachable else .ret] } /-- Local equalities bind an open template to each of three profile extractions. -/ @@ -120,7 +121,7 @@ example (callee : Nat) (called : callee = 4) : /-- error: cil_embed: the expected body does not embed -/ #guard_msgs (substring := true) in example : MethodEmbeds - [{ extracted portable 3 with code := (extracted portable 3).code.set 1 (.brzero 9) }] 0 + [{ extracted portable 3 with code := (extracted portable 3).code.set 1 (.brfalse 9) }] 0 (template portable false false 3) := by cil_embed diff --git a/Tests/ProfileFamilies.cs b/Tests/ProfileFamilies.cs index ba30444a8..103a375ae 100644 --- a/Tests/ProfileFamilies.cs +++ b/Tests/ProfileFamilies.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ConsumerTraceDirectory.cs using System.Text.Json; @@ -12,7 +17,7 @@ Directory.CreateDirectory(assemblyRoot); Directory.CreateDirectory(package); File.WriteAllText(Path.Combine(assemblyRoot, "Family.csproj"), """ -net10.0 +net11.0 true """); File.WriteAllText(Path.Combine(assemblyRoot, "Family.cs"), """ @@ -30,7 +35,7 @@ public static class InitializedFamily """); Run(["dotnet", "build", Path.Combine(assemblyRoot, "Family.csproj"), "-c", "Release"]); var assembly = Path.Combine(assemblyRoot, "bin/Family.dll"); -var profiles = new[] { "x64-sse2", "x64-sse42", "x64-v3", "scalar" }; +var profiles = new[] { "x64-sse42", "x64-v3", "x64-v4", "scalar" }; var artifacts = profiles.Select(profile => Path.Combine(root, profile)).ToArray(); var proof = Path.Combine(package, "FamilyProof.lean"); for (var index = 0; index < profiles.Length; index++) @@ -128,7 +133,7 @@ void Export(JsonObject manifest, string? rejection = null) Directory.CreateDirectory(app); Directory.CreateDirectory(callerPackage); File.WriteAllText(Path.Combine(app, "App.csproj"), """ -net10.0 +net11.0 true """); @@ -230,7 +235,7 @@ theorem FamilyProof.impossible (profile : CIL.FeatureProfile) (impossible : Fals Export(outcome); Console.WriteLine("PASS one outcome-family entry certifies two profiles"); var pruned = new[] { Path.Combine(root, "PrunedReference"), Path.Combine(root, "PrunedWide") }; -foreach (var (index, profile) in new[] { "x64-sse2", "x64-v3" }.Index()) +foreach (var (index, profile) in new[] { profiles[0], profiles[2] }.Index()) { Run(Extract("extract", assembly, "Family", "Family::Pruned()", pruned[index], profile)); File.Copy(Path.Combine(artifacts[0], "verification.json"), Path.Combine(pruned[index], "verification.json"), true); @@ -246,7 +251,7 @@ theorem FamilyProof.impossible (profile : CIL.FeatureProfile) (impossible : Fals File.WriteAllText(reference, referenceSource); Console.WriteLine("PASS independently checked pruning difference rejected"); var initialized = new[] { Path.Combine(root, "InitializedReference"), Path.Combine(root, "InitializedWide") }; -foreach (var (index, profile) in new[] { "x64-sse2", "x64-v3" }.Index()) +foreach (var (index, profile) in new[] { profiles[0], profiles[2] }.Index()) { Run(["dotnet", ExtractorPath, "extract", assembly, "InitializedFamily::Supported()", initialized[index], profile, "--assume-initialized", "InitializedFamily"]); diff --git a/Tests/ProfileTemplates.cs b/Tests/ProfileTemplates.cs index 7acc7a15a..24265298e 100644 --- a/Tests/ProfileTemplates.cs +++ b/Tests/ProfileTemplates.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; @@ -10,7 +15,7 @@ Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Dispatch.csproj"), """ -net10.0true +net11.0truetrue """); File.WriteAllText(Path.Combine(source, "Dispatch.cs"), """ @@ -34,11 +39,44 @@ public static uint Home(uint value) return value; } } +public sealed class Blank { } public static class StaticDispatch { public static readonly uint Bias = 7; public static uint Read() => Bias; } +public abstract class Slot { public abstract uint Value(); } +public sealed class Single : Slot { public override uint Value() => 1; } +public sealed class Double : Slot { public override uint Value() => 2; } +// Each method names other methods only on the branch of one feature, so a profile without it leaves them out. +public static unsafe class Kinds +{ + static uint Bump(uint value) => value + 1; + static uint Twice(uint value) => value * 2; + public static void Register(delegate* managed[] table) { table[0] = &Bump; table[1] = &Twice; } + public static uint Virtual(Slot slot) { if (Avx2.IsSupported) return slot.Value(); return 0; } + public static uint Address(uint value) + { + if (Avx2.IsSupported) return ((delegate* managed)&Bump)(value); + return value; + } + public static uint Indirect(delegate* managed[] table, int index, uint value) + { + if (Avx2.IsSupported) return table[index](value); + return value; + } + public static uint Initial() + { + var scaled = Scaling.Scaled; + if (Avx2.IsSupported) return Bump(scaled); + return scaled; + } +} +public static class Scaling +{ + static uint Scale(uint value) => value; + public static readonly uint Scaled = Scale(3); +} """); Run(["dotnet", "build", Path.Combine(source, "Dispatch.csproj"), "-c", "Release"]); var assembly = Path.Combine(source, "bin/Dispatch.dll"); @@ -50,12 +88,18 @@ public static class StaticDispatch Run(Extract("extract", assembly, "Dispatch", "Dispatch::Wrapper(uint,uint)", wrapper, "x64-v3")); Run(["dotnet", ExtractorPath, "templates", shared, .. extractions, wrapper]); var templates = File.ReadAllText(Path.Combine(shared, "Templates.lean")); -if (templates.Split("@[cil_code] def method").Length != 4 || +if (templates.Split("code := cil_template_code%").Length != 4 || !templates.Contains("(sse42 : Bool) (avx2 : Bool) (callee0 : Nat)", StringComparison.Ordinal) || !templates.Contains("cil_instruction_equations method0", StringComparison.Ordinal)) throw new InvalidOperationException("Expected one generated body per method with flags, callees and equations."); +if (!templates.Contains("cil_decoded_method method0 := Method.ofClosed?", StringComparison.Ordinal) || + !templates.Contains("code := cil_template_code%", StringComparison.Ordinal) || + !templates.Contains("=> callee0 | index => index)).links", StringComparison.Ordinal) || + !templates.Contains("theorem method0_input0_links", StringComparison.Ordinal)) + throw new InvalidOperationException("The Choose template needs decoded code and a parameterized call link."); var renamed = File.ReadAllText(Path.Combine(shared, "TemplateBindings3.lean")); -if (!renamed.Contains("Templates.method0 Extracted.profile true true 2", StringComparison.Ordinal)) +if (!renamed.Contains("Templates.method0 Extracted.profile Extracted.environment true true 2", + StringComparison.Ordinal)) throw new InvalidOperationException("Wrapper extraction did not exercise changed managed callee indices."); var library = args switch { @@ -104,7 +148,7 @@ open CIL theorem selected_result (memory : Safety.Memory) (left right : W32) : Safety.invoke Extracted.program 30 {{entry}} [.scalar (.i32 left), .scalar (.i32 right)] memory = .ok (memory, [.scalar (.i32 {{(index == 1 ? "right" : "left")}})]) := - TemplateProof.choose_returns Extracted.program {{entry}} {{callee}} Extracted.profile + TemplateProof.choose_returns Extracted.program {{entry}} {{callee}} Extracted.profile Extracted.environment {{(sse ? "true" : "false")}} {{(avx ? "true" : "false")}} rfl rfl (by decide) TemplateBindings.method{{entry}}_template {{helper}} memory left right #print axioms selected_result @@ -131,13 +175,74 @@ match Kernel.check (← getEnv) {} proof with Console.WriteLine("PASS exact binding, consumer theorem and independent kernel replay: " + extracted); } -void RejectLean(string name, string text, string path) +// Calls through a virtual slot, a method address, a callable pointer catalog and a static initializer name their +// methods by the template's callee parameters, which must elaborate and bind to each profile's own indices. +var kinds = new (string Name, string Selector, string Spelling)[] +{ + ("Virtual", "Kinds::Virtual(Slot)", ".virtual [("), + ("Address", "Kinds::Address(uint)", ": Link := .address "), + ("Indirect", "Kinds::Indirect(method System.UInt32 *(System.UInt32)[],System.Int32,System.UInt32)", + ".indirect [{ identity := "), + ("Initial", "Kinds::Initial()", "initializer := some { method := callee1") +}; +foreach (var (name, selector, spelling) in kinds) +{ + var kindProfiles = new[] { "scalar", "x64-v3" }; + var kindExtractions = kindProfiles.Select(profile => Path.Combine(directory, name + "-" + profile)).ToArray(); + for (var index = 0; index < kindProfiles.Length; index++) + Run([.. Extract("extract", assembly, "Dispatch", selector, kindExtractions[index], kindProfiles[index]), + "--assume-initialized", "Scaling"]); + var kindShared = Path.Combine(directory, name + "Shared"); + Run(["dotnet", ExtractorPath, "templates", kindShared, .. kindExtractions]); + var kindTemplates = File.ReadAllText(Path.Combine(kindShared, "Templates.lean")); + if (!kindTemplates.Contains(spelling, StringComparison.Ordinal) || + !kindTemplates.Contains("(callee0 : Nat)", StringComparison.Ordinal)) + throw new InvalidOperationException($"The {name} template does not name its method by a callee parameter:\n" + + kindTemplates); + var kindPath = kindShared + Path.PathSeparator + library; + Compile(kindShared, "Templates", kindPath); + for (var index = 0; index < kindProfiles.Length; index++) + { + var extracted = kindExtractions[index]; + var path = extracted + Path.PathSeparator + kindPath; + if (name == "Initial") RequireDecodedEntry(extracted); + Compile(extracted, "Extracted", path); + var binding = "TemplateBindings" + index; + File.Copy(Path.Combine(kindShared, binding + ".lean"), Path.Combine(extracted, binding + ".lean"), + overwrite: true); + Compile(extracted, binding, path); + } + if (name == "Address") + { + File.WriteAllText(Path.Combine(kindShared, "AddressTarget.lean"), """ + import Templates + open CIL + + /-- Taking Bump's address names the target index supplied by the template caller. -/ + theorem address_target (profile : FeatureProfile) (environment : LayoutEnvironment) + (bump twice : Nat) : + (Templates.method0 profile environment true bump twice).linkAt 2 = + some (.address bump) := rfl + #print axioms address_target + """); + Compile(kindShared, "AddressTarget", kindPath); + using Program verifier = new(); + verifier.ReplayCheckedModules(["Templates", "AddressTarget"], kindPath, + "Kernel replay of the decoded method-address template"); + } + Console.WriteLine($"PASS a {name} template elaborates and binds to each profile's own method indices"); +} + +void RejectLean(string name, string text, string path, string diagnostic = "Not a definitional equality") { var file = Path.Combine(directory, name + ".lean"); File.WriteAllText(file, text); var result = Execute(["lean", file], leanPath: path); - if (result.Code == 0 || !result.Output.Contains("rfl", StringComparison.Ordinal)) - throw new InvalidOperationException("Expected exact equality rejection: " + name + "\n" + result.Output); + if (result.Code == 0 || !result.Output.Contains(diagnostic, StringComparison.OrdinalIgnoreCase)) + throw new InvalidOperationException($""" + Expected {diagnostic} checking {name}. + {result.Output} + """); Console.WriteLine("PASS rejection: " + name); } @@ -149,15 +254,23 @@ void RejectLean(string name, string text, string path) StringComparison.Ordinal), widePath); RejectLean("WrongEquation", """ import Templates -example (profile : CIL.FeatureProfile) (callee : Nat) : - (Templates.method0 profile true true callee).code[1]? = some (.brzero 6) := by rfl -""", sharedPath); -var changed = Path.Combine(directory, "ChangedBranch"); -Directory.CreateDirectory(changed); -File.WriteAllText(Path.Combine(changed, "Templates.lean"), - templates.Replace(".brzero 5", ".brzero 6", StringComparison.Ordinal)); -Compile(changed, "Templates", changed + Path.PathSeparator + library); -RejectLean("WrongBranch", wideBinding, changed + Path.PathSeparator + widePath); +example (profile : CIL.FeatureProfile) (environment : CIL.LayoutEnvironment) (callee : Nat) : + (Templates.method0 profile environment true true callee).code[1]? = some (.brfalse 6) := by rfl +""", sharedPath, "is not definitionally equal to the right-hand side"); +var wrongBranch = templates.Replace("Input2.method0.code)", "(Input2.method0.code.set 1 (.brfalse 6)))", + StringComparison.Ordinal); +RejectLean("WrongBranch", wrongBranch, sharedPath); +var wrongLinks = templates.Split('\n').Select(line => line.StartsWith(" links := ", StringComparison.Ordinal) + ? line.Replace("=> callee0 ", "=> callee0 + 1 ", StringComparison.Ordinal) : line); +RejectLean("WrongRelocatedLinks", string.Join('\n', wrongLinks), sharedPath); +RejectLean("UnequalTemplateLengths", """ + import CIL.Extraction.TemplateCode + example (flag : Bool) : List CIL.Instruction := cil_template_code% (if flag then [.ret] else []) + """, sharedPath, "template alternatives have different lengths"); +RejectLean("OpenTemplateCode", """ + import CIL.Extraction.TemplateCode + example (body : CIL.Method) : List CIL.Instruction := cil_template_code% body.code + """, sharedPath, "a template alternative must have a closed instruction list"); var original = File.ReadAllText(Path.Combine(extractions[0], "Extracted.lean")); try @@ -203,6 +316,50 @@ import Templates Compile(generic, "TemplateBindings0", genericPath); Console.WriteLine("PASS distinct closed generic callee identities and exact bindings"); +// A class constructor calls the System.Object constructor with no link. The template steps that call through its +// runtime model, so a proof over the template needs no fact an extraction states. +var constructed = Path.Combine(directory, "Constructor"); +Run(Extract("extract", assembly, "Dispatch", "Blank::.ctor()", constructed)); +var constructorShared = Path.Combine(directory, "ConstructorShared"); +Run(["dotnet", ExtractorPath, "templates", constructorShared, constructed]); +var constructorPath = constructed + Path.PathSeparator + constructorShared + Path.PathSeparator + library; +Compile(constructorShared, "Templates", constructorPath); +File.WriteAllText(Path.Combine(constructorShared, "ConstructorProof.lean"), """ + import Templates + import CIL.Safety.SymbolicExecution + import CIL.Safety.ObjectSteps + + /-! A proof over a generated constructor template. -/ + namespace ConstructorProof + open CIL CIL.Safety + + example (profile : FeatureProfile) (environment : LayoutEnvironment) : + (Templates.method0 profile environment).links = [] := rfl + + example (profile : FeatureProfile) (environment : LayoutEnvironment) : + (Templates.method0 profile environment).code[1]? = some (.call { + owner := .atom Extraction.runtimeScope "System.Object", name := name!".ctor", + hasThis := true, genericArity := 0, returnType := .void, parameters := [], + methodArguments := [] } {}) := rfl + + /-- The template constructor of a class deriving from `System.Object` returns, for any profile. -/ + theorem constructs (program : Program) (index : Nat) (profile : FeatureProfile) + (environment : LayoutEnvironment) + (found : program[index]? = some (Templates.method0 profile environment)) + (memory : Safety.Memory) (object : Reference) + (formed : formObject memory (.address object) = .ok (.object (.address object))) : + invoke program 10 index [.object (.address object)] memory = .ok (memory, []) := by + cil_checked_steps [formed] + #print axioms constructs + end ConstructorProof + """); +Compile(constructorShared, "ConstructorProof", constructorPath); +Compile(constructed, "Extracted", constructorPath); +File.Copy(Path.Combine(constructorShared, "TemplateBindings0.lean"), + Path.Combine(constructed, "TemplateBindings0.lean"), overwrite: true); +Compile(constructed, "TemplateBindings0", constructorPath); +Console.WriteLine("PASS a constructor template steps its unlinked System.Object constructor call"); + var staticOutputs = new[] { "scalar", "x64-v3" }.Select(profile => Path.Combine(directory, "Static-" + profile)).ToArray(); for (var index = 0; index < staticOutputs.Length; index++) @@ -211,7 +368,7 @@ import Templates var staticShared = Path.Combine(directory, "StaticShared"); Run(["dotnet", ExtractorPath, "templates", staticShared, .. staticOutputs]); var staticTemplate = File.ReadAllText(Path.Combine(staticShared, "Templates.lean")); -if (!staticTemplate.Contains("fieldName := \"System.UInt32 StaticDispatch::Bias\"", StringComparison.Ordinal)) +if (!staticTemplate.Contains("name := name!\"Bias\" }", StringComparison.Ordinal)) throw new InvalidOperationException("Static template omitted its field identity."); var staticFiles = Directory.GetFiles(staticShared).ToDictionary(path => Path.GetFileName(path)!, File.ReadAllText); Run(["dotnet", ExtractorPath, "templates", staticShared, .. staticOutputs]); @@ -221,27 +378,39 @@ import Templates for (var index = 0; index < staticOutputs.Length; index++) { var path = staticOutputs[index] + Path.PathSeparator + staticShared + Path.PathSeparator + library; + RequireDecodedEntry(staticOutputs[index]); Compile(staticOutputs[index], "Extracted", path); var binding = "TemplateBindings" + index; File.Copy(Path.Combine(staticShared, binding + ".lean"), Path.Combine(staticOutputs[index], binding + ".lean"), overwrite: true); Compile(staticOutputs[index], binding, path); } +void RequireDecodedEntry(string directory) +{ + if (!File.ReadLines(Path.Combine(directory, "Extracted.lean")) + .Any(line => line.StartsWith("cil_decoded_method method0 ", StringComparison.Ordinal))) + throw new InvalidOperationException($"The static entry in {directory} has no decoder equation."); +} + var staticBinding = File.ReadAllText(Path.Combine(staticShared, "TemplateBindings0.lean")); var staticPath = staticOutputs[0] + Path.PathSeparator + library; var changedStatic = Path.Combine(directory, "ChangedStatic"); Directory.CreateDirectory(changedStatic); +var siteStart = staticTemplate.IndexOf("staticSites := [", StringComparison.Ordinal); +if (siteStart < 0) throw new InvalidOperationException("Static template has no descriptor list."); +var codeStart = staticTemplate.IndexOf(" code := cil_template_code%", siteStart, StringComparison.Ordinal); +var descriptors = staticTemplate[siteStart..codeStart]; +var changedField = descriptors.Replace("name := name!\"Bias\" }, bytes", "name := name!\"Changed\" }, bytes", + StringComparison.Ordinal); +if (descriptors == changedField) throw new InvalidOperationException("Missing static field identity."); File.WriteAllText(Path.Combine(changedStatic, "Templates.lean"), - staticTemplate.Replace("StaticDispatch::Bias", "StaticDispatch::Changed", StringComparison.Ordinal)); + $"""{staticTemplate[..siteStart]}{changedField}{staticTemplate[codeStart..]}"""); Compile(changedStatic, "Templates", changedStatic + Path.PathSeparator + library); RejectLean("ChangedStaticIdentity", staticBinding, changedStatic + Path.PathSeparator + staticPath); -var siteStart = staticTemplate.IndexOf("staticSites := [", StringComparison.Ordinal); -var siteEnd = staticTemplate.IndexOf("], code := [", siteStart, StringComparison.Ordinal); -if (siteStart < 0 || siteEnd < 0) throw new InvalidOperationException("Static template has no descriptor list."); var missingStatic = Path.Combine(directory, "MissingStatic"); Directory.CreateDirectory(missingStatic); File.WriteAllText(Path.Combine(missingStatic, "Templates.lean"), - staticTemplate[..siteStart] + "staticSites := [" + staticTemplate[siteEnd..]); + $"""{staticTemplate[..siteStart]}staticSites := [],{"\n"}{staticTemplate[codeStart..]}"""); Compile(missingStatic, "Templates", missingStatic + Path.PathSeparator + library); RejectLean("MissingStaticMetadata", staticBinding, missingStatic + Path.PathSeparator + staticPath); Console.WriteLine("PASS deterministic static metadata, exact profile bindings " + diff --git a/Tests/ProfileTemplatesProof.lean b/Tests/ProfileTemplatesProof.lean index 469348555..35c37b470 100644 --- a/Tests/ProfileTemplatesProof.lean +++ b/Tests/ProfileTemplatesProof.lean @@ -5,9 +5,20 @@ import CIL.Safety.SymbolicExecution namespace TemplateProof open CIL CIL.Safety +-- A consumer matching an instruction equation sees a numeral at every excluded pc. +open Lean in +run_cmd do + for pc in [:11] do + let equation := `Templates ++ Name.mkSimple s!"method0_instruction{pc}" + let type := (← getConstInfo equation).type + if (type.find? fun term => term.isAppOfArity ``Instruction.unadmitted 1 && + (natLiteral? term.appArg!).isNone).isSome then + throwError "{equation} has a nonliteral hole opcode" + /-- The generated helper returns its argument for any profile. -/ theorem helper_returns (program : Program) (index : Nat) (profile : FeatureProfile) - (found : program[index]? = some (Templates.method1 profile)) + (environment : LayoutEnvironment) + (found : program[index]? = some (Templates.method1 profile environment)) (memory : Safety.Memory) (value : W32) : invoke program 10 index [.scalar (.i32 value)] memory = .ok (memory, [.scalar (.i32 value)]) := by @@ -16,10 +27,11 @@ theorem helper_returns (program : Program) (index : Nat) (profile : FeatureProfi /-- All selected dispatch combinations share this proof and the generated body. -/ theorem choose_returns (program : Program) (index callee : Nat) (profile : FeatureProfile) - (sse42 avx2 : Bool) (sse : profile.sse42 = sse42) (avx : profile.avx2 = avx2) + (environment : LayoutEnvironment) (sse42 avx2 : Bool) (sse : profile.sse42 = sse42) + (avx : profile.avx2 = avx2) (covered : avx2 = true → sse42 = true) - (found : program[index]? = some (Templates.method0 profile sse42 avx2 callee)) - (helper : avx2 = true → program[callee]? = some (Templates.method1 profile)) + (found : program[index]? = some (Templates.method0 profile environment sse42 avx2 callee)) + (helper : avx2 = true → program[callee]? = some (Templates.method1 profile environment)) (memory : Safety.Memory) (left right : W32) : invoke program 30 index [.scalar (.i32 left), .scalar (.i32 right)] memory = .ok (memory, [.scalar (.i32 (if avx2 then left else if sse42 then right else left))]) := by @@ -32,8 +44,9 @@ theorem choose_returns (program : Program) (index callee : Nat) (profile : Featu #print axioms choose_returns /-- An unselected combination cannot start executing the generated dispatch template. -/ -theorem unselected_flags (profile : FeatureProfile) (callee : Nat) : - (Templates.method0 profile false true callee).code[0]? = - some (.unsupported "Profile combination not selected for template") := rfl +theorem unselected_flags (profile : FeatureProfile) (environment : LayoutEnvironment) + (callee : Nat) : + (Templates.method0 profile environment false true callee).code[0]? = + some (.unadmitted 0x28) := rfl #print axioms unselected_flags end TemplateProof diff --git a/Tests/ProgramClosures.cs b/Tests/ProgramClosures.cs new file mode 100644 index 000000000..b5616d4f0 --- /dev/null +++ b/Tests/ProgramClosures.cs @@ -0,0 +1,18 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs + +// Entry uses AddTwo's contract after relocation. Equal executable bodies may share an index, +// while each decoded call still has to name the target's metadata token. +var fixture = At("Tests/ProgramClosures"); +Run(["dotnet", "build", Path.Combine(fixture, "Assembly/ProgramClosures.csproj"), "-c", "Release"]); +using var verification = new Program(); +verification.Verify(Path.Combine(fixture, "Assembly/bin/ProgramClosures.dll"), "ProgramClosures", + "System.UInt64 Closure::Entry(System.UInt64)", Path.Combine(fixture, "Proof.lean"), + At("artifacts/ProgramClosures")); +Console.WriteLine("PASS AddTwo's contract relocates across equal bodies and decoding rejects mismatched method tokens"); diff --git a/Tests/ProgramClosures/Assembly/Closure.cs b/Tests/ProgramClosures/Assembly/Closure.cs new file mode 100644 index 000000000..08eab14e6 --- /dev/null +++ b/Tests/ProgramClosures/Assembly/Closure.cs @@ -0,0 +1,20 @@ +using System.Runtime.CompilerServices; + +/// +/// AddTwo calls Bump and Nudge, whose bodies are the same, so a program may hold +/// both at one index. +/// +public static class Closure +{ + [MethodImpl(MethodImplOptions.NoInlining)] + static ulong Bump(ulong value) => unchecked(value + 1); + + [MethodImpl(MethodImplOptions.NoInlining)] + static ulong Nudge(ulong value) => unchecked(value + 1); + + [MethodImpl(MethodImplOptions.NoInlining)] + static ulong AddTwo(ulong value) => Nudge(Bump(value)); + + /// Returns plus two through AddTwo. + public static ulong Entry(ulong x) => AddTwo(x); +} diff --git a/Tests/ProgramClosures/Assembly/ProgramClosures.csproj b/Tests/ProgramClosures/Assembly/ProgramClosures.csproj new file mode 100644 index 000000000..08177348d --- /dev/null +++ b/Tests/ProgramClosures/Assembly/ProgramClosures.csproj @@ -0,0 +1,9 @@ + + + net11.0 + ProgramClosures + enable + true + $(NoWarn);CS1591 + + diff --git a/Tests/ProgramClosures/Proof.lean b/Tests/ProgramClosures/Proof.lean new file mode 100644 index 000000000..ef9c824c3 --- /dev/null +++ b/Tests/ProgramClosures/Proof.lean @@ -0,0 +1,329 @@ +import Extracted +import Lean +import CIL +import CIL.MethodIdentity + +/-! +# A summary proved once over its closure and applied in other programs + +`Closure::AddTwo` returns its argument plus two by calling `Bump` and `Nudge`, which have the +same body. Its contract is stepped once in the extraction, carried to the closure of `AddTwo`, +bound there to the closure's records, and carried from the closure back to the extraction, to a +program that holds the closure at other indices. The executable contract also holds when +`Bump` and `Nudge` share an index, but their distinct tokens prevent decoding that binding. +Relocations into a program with other bytes at an index +`AddTwo` links to, and into one holding `AddTwo` and `Bump`, whose bodies differ, at one index, +are refuted. +-/ + +namespace ExampleProof +open CIL CIL.Safety CIL.Extraction + +bind_method addTwo addTwoBody := "System.UInt64 Closure::AddTwo(System.UInt64)" + +/-- `AddTwo` returns its argument plus two and changes no memory. -/ +def addTwoContract : MethodContract W64 W64 where + args x := [.scalar (.i64 x)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post x initial final value := value = x + 2 ∧ final = initial + +/-- `AddTwo` meets its contract in the extraction, stepping both calls. -/ +theorem addTwo_holds : addTwoContract.Holds Extracted.program addTwo := by + rintro x memory - + refine MethodContract.Holds.intro memory (x + 1 + 1) ?_ (Confined.refl _ _) ⟨by bv_omega, rfl⟩ + cil_checked_steps +#print axioms addTwo_holds + +/-- Every record's pinned bytes decode to the method at its index of the extraction. -/ +theorem bodies_decode : + decodedProgram? Extracted.profile Extracted.environment Extracted.methodEvidence = + some Extracted.program := + decodedProgram?_eq_some Extracted.translation_valid.programBindingsValid + (.cons ((Method.ofBody?_of_decodes Extracted.method0_body_validated).trans + Extracted.method0_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method1_body_validated).trans + Extracted.method1_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method2_body_validated).trans + Extracted.method2_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method3_body_validated).trans + Extracted.method3_decoded) .nil)))) +#print axioms bodies_decode + +/-- The methods `AddTwo` reaches, renumbered from `AddTwo` at 0, with their indices here. -/ +def closure : Program × List Nat := + (closureWith Extracted.programIndex.get? Extracted.programLength addTwo).getD ([], []) + +/-- The closure of `AddTwo` is found. -/ +theorem closure_found : Extracted.program.closure addTwo = some closure := by + rw [Program.closure_eq_closureWith Extracted.programIndex_get? Extracted.programLength_eq] + cil_kernel_rfl +#print axioms closure_found + +/-- The closure holds `AddTwo`, then its callees in the order its link table enters them. -/ +theorem closure_indices : closure.2 = [1, 3, 2] := by decide +kernel +#print axioms closure_indices + +/-- The index in the extraction of each method of the closure. -/ +def here (slot : Nat) : Nat := closure.2.getD slot 0 + +/-- The closure's first method is `AddTwo`. -/ +theorem here_first : here 0 = addTwo := by + unfold here + rw [closure_indices] + rfl +#print axioms here_first + +/-- The closure's records, read from the extraction's in closure order. -/ +def records : List MethodEvidence := + (Extracted.program.closureEvidence Extracted.methodEvidence addTwo).getD [] + +/-- The closure's records are found. -/ +theorem records_found : + Extracted.program.closureEvidence Extracted.methodEvidence addTwo = some records := by + cil_kernel_rfl +#print axioms records_found + +/-- The closure has one record per method. -/ +theorem records_length : records.length = 3 := by decide +kernel +#print axioms records_length + +/-- The closure's records decode to the closure. -/ +theorem closure_decodes : + decodedProgram? Extracted.profile Extracted.environment records = some closure.1 := + Program.closureEvidence_decodes bodies_decode closure_found records_found +#print axioms closure_decodes + +/-- The contract of `AddTwo` holds in its closure alone. -/ +theorem closure_holds : addTwoContract.Holds closure.1 0 := + MethodContract.Holds.restrict (f := here) (here_first ▸ addTwo_holds) + (Program.closure_closed closure_found) (Program.closure_relocates closure_found).1 + (by decide +kernel) +#print axioms closure_holds + +/-- The record of `AddTwo` in the closure: the extraction's, with its links renumbered. -/ +def addTwoRecord : MethodEvidence := + (Extracted.methodEvidence[addTwo]'(by decide)).relocateLinks + (Extracted.program.closureSearch addTwo).position + +/-- The closure's first record is `AddTwo`'s. -/ +theorem first_record : records[0]? = some addTwoRecord := by cil_kernel_rfl +#print axioms first_record + +/-- The summary of `AddTwo`, proved once over its closure. -/ +theorem closure_summary : + DecodedSummary closure.1 records Extracted.profile Extracted.environment 0 + addTwoRecord.assembly addTwoRecord.original.token addTwoRecord.arguments addTwoContract := + .of_decoded closure_decodes first_record ⟨_, Extracted.method1_body_validated⟩ + Extracted.method1_specialization_validated.1 + closure_holds +#print axioms closure_summary + +/-- The summary of the closure is a summary of `AddTwo` in the extraction. -/ +theorem summary_here : + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment (here 0) addTwoRecord.assembly addTwoRecord.original.token + addTwoRecord.arguments addTwoContract := + closure_summary.relocate (Program.closure_closed closure_found) + (Program.closure_relocates closure_found).1 bodies_decode fun item found => by + obtain rfl := Option.some.inj (first_record.symm.trans found) + refine ⟨Extracted.methodEvidence[addTwo]'(by decide), ?_, rfl, rfl, rfl, rfl, ⟨_, Extracted.method1_body_validated⟩⟩ + rw [here_first] + exact List.getElem?_eq_getElem _ +#print axioms summary_here + +/-- `Entry` returns its argument plus two and leaves memory as it was, through the summary of + `AddTwo` carried from its closure. -/ +theorem correctness (memory : Safety.Memory) (x : W64) : + ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory = + .ok (memory, [.scalar (.i64 (x + 2))]) := by + have held : addTwoContract.Holds Extracted.program addTwo := here_first ▸ summary_here.holds + refine (WP.exact_iff (execution := Execution fun fuel => + invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory)).mp ?_ + cil_contract_steps [held, addTwoContract] with final value added + obtain ⟨rfl, rfl⟩ := added.post + rfl +#print axioms correctness + +/-- The record at `slot` of the closure. -/ +def record (slot : Nat) (bound : slot < 3) : MethodEvidence := + records[slot]'(records_length ▸ bound) + +/-- The index of each closure method in a second program: `AddTwo` at 3 and its callees at 0 + and 2, with `Entry` at 1. -/ +def elsewhere (slot : Nat) : Nat := [3, 0, 2].getD slot 0 + +/-- The records of the second program, each with its links renamed into it. -/ +def elsewhereEvidence : List MethodEvidence := + [(record 1 (by decide)).relocateLinks elsewhere, + (Extracted.methodEvidence[0]'(by decide)).relocateLinks fun index => [1, 3, 2, 0].getD index 0, + (record 2 (by decide)).relocateLinks elsewhere, + addTwoRecord.relocateLinks elsewhere] + +/-- The second program: `Bump`, `Entry` calling index 3, `Nudge`, and `AddTwo` calling indices + 0 and 2. -/ +def elsewhereProgram : Program := + [Extracted.method3, { Extracted.method0 with links := [(1, .direct 3)] }, Extracted.method2, + { Extracted.method1 with links := [(1, .direct 0), (2, .direct 2)] }] + +/-- The second program's records decode to it. -/ +theorem elsewhere_decodes : + decodedProgram? Extracted.profile Extracted.environment elsewhereEvidence = + some elsewhereProgram := by + cil_kernel_rfl +#print axioms elsewhere_decodes + +/-- The second program holds each closure method at the index `elsewhere` gives it. -/ +theorem elsewhere_relocates : RelocatesInto closure.1 elsewhereProgram elsewhere := + RelocatesInto.of_decoded closure_decodes elsewhere_decodes fun slot bound => by + rw [records_length] at bound + match slot, bound with + | 0, _ => exact ⟨addTwoRecord, addTwoRecord.relocateLinks elsewhere, first_record, rfl, + MethodEvidence.ofBody?_relocateLinks _ _ _ _⟩ + | 1, bound | 2, bound => exact ⟨record _ bound, (record _ bound).relocateLinks elsewhere, + List.getElem?_eq_getElem _, rfl, MethodEvidence.ofBody?_relocateLinks _ _ _ _⟩ +#print axioms elsewhere_relocates + +/-- The summary of the closure is a summary of `AddTwo` at index 3 of the second program. -/ +theorem summary_elsewhere : + DecodedSummary elsewhereProgram elsewhereEvidence Extracted.profile Extracted.environment + (elsewhere 0) addTwoRecord.assembly addTwoRecord.original.token addTwoRecord.arguments + addTwoContract := + closure_summary.relocate (Program.closure_closed closure_found) elsewhere_relocates + elsewhere_decodes fun item found => by + obtain rfl := Option.some.inj (first_record.symm.trans found) + exact ⟨addTwoRecord.relocateLinks elsewhere, rfl, rfl, rfl, rfl, rfl, + ⟨_, Extracted.method1_body_validated⟩⟩ +#print axioms summary_elsewhere + +/-- In the second program, `Entry` returns its argument plus two and leaves memory as it was, + through the contract of `AddTwo` carried there from its closure. -/ +theorem correctness_elsewhere (memory : Safety.Memory) (x : W64) : + ∃ fuel, invoke elsewhereProgram fuel 1 [.scalar (.i64 x)] memory = + .ok (memory, [.scalar (.i64 (x + 2))]) := by + have held : addTwoContract.Holds elsewhereProgram 3 := summary_elsewhere.holds + refine (WP.exact_iff (execution := Execution fun fuel => + invoke elsewhereProgram fuel 1 [.scalar (.i64 x)] memory)).mp ?_ + cil_contract_steps [held, addTwoContract] with final value added + obtain ⟨rfl, rfl⟩ := added.post + rfl +#print axioms correctness_elsewhere + +/-- The two callees of `AddTwo` decode to the same method. -/ +theorem callees_agree : closure.1[1]? = closure.1[2]? := by cil_kernel_rfl +#print axioms callees_agree + +/-- The index of each closure method in a third program: `AddTwo` at 2 and both its callees at + 0, with `Entry` at 1. -/ +def collapse (slot : Nat) : Nat := [2, 0, 0].getD slot 0 + +/-- The records of the third program, each with its links renamed into it. -/ +def collapsedEvidence : List MethodEvidence := + [(record 1 (by decide)).relocateLinks collapse, + (Extracted.methodEvidence[0]'(by decide)).relocateLinks fun index => [1, 2, 0, 0].getD index 0, + addTwoRecord.relocateLinks collapse] + +/-- The third program: `Bump`, `Entry` calling index 2, and `AddTwo` calling index 0 twice. -/ +def collapsedProgram : Program := + [Extracted.method3, { Extracted.method0 with links := [(1, .direct 2)] }, + { Extracted.method1 with links := [(1, .direct 0), (2, .direct 0)] }] + +/-- Collapsing distinct metadata tokens onto one slot fails the decoded call check. -/ +theorem collapsed_bindings_refused : + decodedProgram? Extracted.profile Extracted.environment collapsedEvidence = none := by + cil_kernel_rfl +#print axioms collapsed_bindings_refused + +/-- The two callees still have identical executable bodies after renumbering. -/ +theorem collapsed_relocates : RelocatesInto closure.1 collapsedProgram collapse := by + intro slot bound + have size : closure.1.length = 3 := by decide +kernel + rw [size] at bound + match slot, bound with + | 0, _ | 1, _ | 2, _ => rfl +#print axioms collapsed_relocates + +/-- In the third program, `Entry` returns its argument plus two and leaves memory as it was, + through the contract of `AddTwo` carried there from its closure. -/ +theorem correctness_collapsed (memory : Safety.Memory) (x : W64) : + ∃ fuel, invoke collapsedProgram fuel 1 [.scalar (.i64 x)] memory = + .ok (memory, [.scalar (.i64 (x + 2))]) := by + have held : addTwoContract.Holds collapsedProgram 2 := + closure_holds.relocate (Program.closure_closed closure_found) collapsed_relocates + refine (WP.exact_iff (execution := Execution fun fuel => + invoke collapsedProgram fuel 1 [.scalar (.i64 x)] memory)).mp ?_ + cil_contract_steps [held, addTwoContract] with final value added + obtain ⟨rfl, rfl⟩ := added.post + rfl +#print axioms correctness_collapsed + +/-- A fourth program that holds the closure where the second does, except that `Entry`'s + record stands at index 0, where `elsewhere` sends `Bump`. -/ +def otherBytesEvidence : List MethodEvidence := + [(Extracted.methodEvidence[0]'(by decide)).relocateLinks fun index => [1, 3, 2, 0].getD index 0, + (Extracted.methodEvidence[0]'(by decide)).relocateLinks fun index => [1, 3, 2, 0].getD index 0, + (record 2 (by decide)).relocateLinks elsewhere, + addTwoRecord.relocateLinks elsewhere] + +/-- The fourth program with Entry's executable body in Bump's slot. -/ +def otherBytesProgram : Program := + elsewhereProgram.set 0 { Extracted.method0 with links := [(1, .direct 3)] } + +/-- The call to Bump cannot bind to Entry's record. -/ +theorem other_bytes_bindings_refused : + decodedProgram? Extracted.profile Extracted.environment otherBytesEvidence = none := by + cil_kernel_rfl +#print axioms other_bytes_bindings_refused + +/-- No relocation into the fourth program sends `Bump` to index 0, whose bytes are `Entry`'s. -/ +theorem other_bytes_refused : ¬ RelocatesInto closure.1 otherBytesProgram elsewhere := by + intro relocates + have bump := congrArg (Option.map fun method => method.code.length) (relocates 1 (by decide)) + revert bump + decide +kernel +#print axioms other_bytes_refused + +/-- The index of each closure method in a fifth program: `AddTwo` and `Bump`, whose bodies + differ, at 2, and `Nudge` at 0. -/ +def merge (slot : Nat) : Nat := [2, 2, 0].getD slot 0 + +/-- The records of the fifth program, each with its links renamed into it. -/ +def mergedEvidence : List MethodEvidence := + [(record 2 (by decide)).relocateLinks merge, + (Extracted.methodEvidence[0]'(by decide)).relocateLinks fun index => [1, 2, 2, 0].getD index 0, + addTwoRecord.relocateLinks merge] + +/-- The fifth program, with AddTwo occupying the slot of its first callee. -/ +def mergedProgram : Program := + [Extracted.method2, { Extracted.method0 with links := [(1, .direct 2)] }, + { Extracted.method1 with links := [(1, .direct 2), (2, .direct 0)] }] + +/-- The call to Bump cannot bind to AddTwo's record. -/ +theorem merged_bindings_refused : + decodedProgram? Extracted.profile Extracted.environment mergedEvidence = none := by + cil_kernel_rfl +#print axioms merged_bindings_refused + +/-- No relocation sends `AddTwo` and `Bump` to one index, since their bodies differ. -/ +theorem differing_merge_refused : ¬ RelocatesInto closure.1 mergedProgram merge := by + intro relocates + have bump := congrArg (Option.map fun method => method.code.length) (relocates 1 (by decide)) + revert bump + decide +kernel +#print axioms differing_merge_refused + +-- A theorem left unproved would still print its axioms; this check fails instead. +open Lean in +run_cmd do + for name in [``addTwo_holds, ``bodies_decode, ``closure_found, ``closure_indices, ``here_first, + ``records_found, ``records_length, ``closure_decodes, ``closure_holds, ``first_record, + ``closure_summary, ``summary_here, ``correctness, ``elsewhere_decodes, + ``elsewhere_relocates, ``summary_elsewhere, ``correctness_elsewhere, ``callees_agree, + ``collapsed_bindings_refused, ``collapsed_relocates, ``correctness_collapsed, + ``other_bytes_bindings_refused, ``other_bytes_refused, ``merged_bindings_refused, + ``differing_merge_refused] do + if (← collectAxioms name).contains ``sorryAx then + throwError "{name} depends on sorryAx" + +end ExampleProof diff --git a/Tests/ProofBindingCatalog.cs b/Tests/ProofBindingCatalog.cs index c07002f31..79a918c35 100644 --- a/Tests/ProofBindingCatalog.cs +++ b/Tests/ProofBindingCatalog.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json.Nodes; diff --git a/Tests/ProofBindingFamilies.cs b/Tests/ProofBindingFamilies.cs index b792a59bf..57e1c8712 100644 --- a/Tests/ProofBindingFamilies.cs +++ b/Tests/ProofBindingFamilies.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Diagnostics; using System.Text.Json; @@ -36,7 +41,7 @@ string Build(string name, bool changed) Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Methods.csproj"), """ - net10.0true + net11.0true """); var methods = File.ReadAllText(At("Tests/BindingFamilies/Methods.cs")); @@ -79,38 +84,89 @@ string Build(string name, bool changed) throw new InvalidOperationException("Generated binding lost source identity or ordinary audit."); } -// Each mutation binds a family's proof with one wrong field, or the right proof to a changed body. +// The negative catalog binds a family's proof with a wrong width or signedness to the production method. foreach (var file in new[] { "lean-toolchain", "lakefile.toml", "Support.lean", "Binding.lean.in" }) File.Copy(Path.Combine(package, file), Path.Combine(negativePackage, file), overwrite: true); File.Copy(At("Tests/BindingFamilies/Negative.json"), Path.Combine(negativePackage, "Negative.json"), overwrite: true); Run(["lake", "update"], negativePackage); Run([.. cli, "bindings", Path.Combine(negativePackage, "Negative.json")]); +// A wrong width or signedness is a claim about the managed signature, which the binding's `signature` theorem +// decides; a wrong polarity or a changed body is a wrong result, which a verified refutation shows at an input. var mutations = new[] { - (Name: "WrongWidth", Method: "Equal(uint,uint)", Assembly: assembly), - (Name: "WrongSignedness", Method: "Equal(uint,uint)", Assembly: assembly), - (Name: "WrongPolarity", Method: "Equal(uint,uint)", Assembly: assembly), - (Name: "ChangedBody", Method: "Xor(ulong,ulong)", Assembly: changedAssembly) + (Name: "WrongWidth", Method: "Equal(uint,uint)", Assembly: assembly, Refutation: (string?)null), + (Name: "WrongSignedness", Method: "Equal(uint,uint)", Assembly: assembly, Refutation: null), + (Name: "WrongPolarity", Method: "Equal(uint,uint)", Assembly: assembly, + Refutation: Refutation("32", ".i32 (if input.1 ≠ input.2 then 1 else 0)", 0, 0)), + (Name: "ChangedBody", Method: "Xor(ulong,ulong)", Assembly: changedAssembly, + Refutation: Refutation("64", ".i64 (input.1 ^^^ input.2)", 1, 1)) }; +Directory.CreateDirectory(Path.Combine(negativePackage, "Refutation")); var negativeRequests = mutations.Select(mutation => { var output = Path.Combine(directory, mutation.Name); ExtractBatch("extract", null, [new(mutation.Assembly, "BindingMethods::" + mutation.Method, output, "scalar")], cli[1]); - return new - { - output, proof = Path.Combine(negativePackage, "Negative", mutation.Name + ".lean"), - theorem = "Generated.correctness", expect = "rejected" - }; + if (mutation.Refutation is null) + return new { output, proof = Path.Combine(negativePackage, "Negative", mutation.Name + ".lean"), + theorem = "Generated.correctness" }; + var refutation = Path.Combine(negativePackage, "Refutation", mutation.Name + ".lean"); + File.WriteAllText(refutation, mutation.Refutation); + return new { output, proof = refutation, theorem = "Refutation.refuted" }; }).ToArray(); var requests = Path.Combine(directory, "requests.json"); File.WriteAllText(requests, JsonSerializer.Serialize(new { schema = 1, proofRoot = negativePackage, requests = negativeRequests })); -Run([.. cli, "verify-batch", requests, "--results", Path.Combine(directory, "negative-results.json")]); -if (negativeRequests.Any(request => File.Exists(Path.Combine(request.output, "verified.json")))) - throw new InvalidOperationException("A rejected catalog binding retained a certificate."); +var negativeResults = Path.Combine(directory, "negative-results.json"); +var negative = Execute([.. cli, "verify-batch", requests, "--results", negativeResults]); +var outcomes = JsonDocument.Parse(File.ReadAllText(negativeResults)).RootElement.EnumerateArray().ToArray(); +foreach (var ((name, _, _, refuting), outcome) in mutations.Zip(outcomes)) +{ + var output = outcome.GetProperty("output").GetString()!; + var failure = outcome.GetProperty("failure"); + var entry = JsonDocument.Parse(File.ReadAllText(Path.Combine(output, "identity.json"))).RootElement + .GetProperty("entry").GetString(); + var met = refuting is not null + ? outcome.GetProperty("status").GetString() == "verified" && + failure.GetProperty("kind").GetString() == "refutation" && + failure.GetProperty("method").GetProperty("entry").GetString() == entry + : outcome.GetProperty("status").GetString() == "failed" && + outcome.GetProperty("stage").GetString() == "proof checking" && + failure.GetProperty("diagnostics")[0].GetProperty("message").GetString()! + .Contains("Extracted.method0Signature\nis not definitionally equal", StringComparison.Ordinal) && + !File.Exists(Path.Combine(output, "verified.json")); + if (!met) + throw new InvalidOperationException($"{name} was not refuted or rejected at its signature: {outcome}\n{ + negative.Output}"); +} +if (negative.Code != 1) + throw new InvalidOperationException("The batch of wrong bindings did not fail its signature checks: " + + negative.Output); + +// The claim a mutated binding makes about `width`-bit arguments, refuted where the method returns another value. +static string Refutation(string width, string value, int left, int right) => $$""" + import Extracted + import CIL.Safety.MethodContract + open CIL.Safety + namespace Refutation + /-- The result the mutated binding claims for every pair of words. -/ + def contract : MethodContract (CIL.W{{width}} × CIL.W{{width}}) where + args input := [.scalar (.i{{width}} input.1), .scalar (.i{{width}} input.2)] + pre _ _ := True + footprint _ := [] + post input initial final values := final = initial ∧ values = [.scalar ({{value}})] + /-- The extracted method returns another value at `({{left}}, {{right}})`. -/ + theorem refuted : contract.Refuted Extracted.program 0 := + .of_return (input := ({{left}}, {{right}})) (initial := Memory.empty) trivial + (rfl : invoke Extracted.program 16 0 [.scalar (.i{{width}} {{left}}), .scalar (.i{{width}} {{right}})] + Memory.empty = .ok _) + (by rintro ⟨-, out, rfl, -, wrong⟩; revert wrong; decide) + #print axioms refuted + end Refutation + + """; int Lines(string path) => File.ReadLines(path).Count(); var generatedFiles = Directory.GetFiles(Path.Combine(package, "Bindings"), "*.lean"); @@ -133,4 +189,4 @@ string Build(string name, bool changed) File.WriteAllText(Path.Combine(directory, "sizes.json"), JsonSerializer.Serialize(sizes, new JsonSerializerOptions { WriteIndented = true }) + "\n"); Console.WriteLine(JsonSerializer.Serialize(sizes)); -Console.WriteLine("PASS equality/bitwise bindings, exact signed widths, polarity and changed-body rejection"); +Console.WriteLine("PASS equality/bitwise bindings, exact signed widths, refuted polarity and changed body"); diff --git a/Tests/ProofCostDiscovery.lean b/Tests/ProofCostDiscovery.lean new file mode 100644 index 000000000..583f52ed8 --- /dev/null +++ b/Tests/ProofCostDiscovery.lean @@ -0,0 +1,102 @@ +import scripts.ProofCost + +/-! Regression checks for incremental discovery of kernel constants in the cost frontend. -/ + +open Lean ProofCost + +/-- Synthetic map entries exercise discovery independently of elaboration and kernel checking. -/ +def discoveryInfo (name : Name) : ConstantInfo := + .defnInfo { + name, levelParams := [], type := mkConst ``Nat, value := mkNatLit 0 + hints := .abbrev, safety := .safe } + +/-- Incremental discovery must return exactly the reference fold's entries, in the same order. -/ +unsafe def checkDiscovery (previous current : PHashMap Name ConstantInfo) (known : NameSet) : + IO Unit := do + let actual := addedConstants previous current known |>.map (·.1) + let expected := current.foldl (init := #[]) fun names name _ => + if known.contains name then names else names.push name + unless actual == expected do + throw <| IO.userError s!"constant discovery differs: {actual} != {expected}" + +/-- Discovery is exact across trie promotions, unchanged commands and restored environments. -/ +unsafe def discoveryCases : IO Unit := do + let mut current : PHashMap Name ConstantInfo := {} + let mut known : NameSet := {} + for index in [:1000] do + let previous := current + let name := .num `discovery index + current := current.insert name (discoveryInfo name) + checkDiscovery previous current known + known := known.insert name + checkDiscovery current current known + let restored : PHashMap Name ConstantInfo := {} + checkDiscovery current restored known + checkDiscovery restored current known + let name := `afterRollback + let resumed := restored.insert name (discoveryInfo name) + checkDiscovery current resumed known + let keys := #[`collisionA, `collisionB] + let values := keys.map discoveryInfo + let collision : PHashMap Name ConstantInfo := + { root := .collision keys values (by simp [values]) } + checkDiscovery {} collision {} + let collisionKnown := ({} : NameSet).insert `collisionA |>.insert `collisionB + checkDiscovery collision collision collisionKnown + let expanded : PHashMap Name ConstantInfo := + { root := .collision (keys.push name) (values.push (discoveryInfo name)) (by simp [values]) } + checkDiscovery collision expanded collisionKnown + IO.println "PASS constant discovery matches the fold across promotions, collisions and rollback" + +/-- Total discovery heartbeats for one constant added by each of `count` commands. -/ +unsafe def discoveryCost (count : Nat) : IO Nat := do + let mut current : PHashMap Name ConstantInfo := {} + let mut known : NameSet := {} + let mut heartbeats := 0 + for index in [:count] do + let previous := current + let name := .num `scaling index + current := current.insert name (discoveryInfo name) + let start ← IO.getNumHeartbeats + let added := addedConstants previous current known + unless added.size == 1 && added[0]!.1 == name do + throw <| IO.userError "discovery lost or repeated a constant" + heartbeats := heartbeats + ((← IO.getNumHeartbeats) - start) + known := known.insert name + return heartbeats + +/-- Median discovery nanoseconds, with persistent inputs constructed before the timed region. -/ +unsafe def discoveryTime (count : Nat) : IO Nat := do + let mut inputs : Array (PHashMap Name ConstantInfo × PHashMap Name ConstantInfo × NameSet) := #[] + let mut current : PHashMap Name ConstantInfo := {} + let mut known : NameSet := {} + for index in [:count] do + let previous := current + let name := .num `timing index + current := current.insert name (discoveryInfo name) + inputs := inputs.push (previous, current, known) + known := known.insert name + let mut samples : Array Nat := #[] + for iteration in [:4] do + let start ← IO.monoNanosNow + let mut found := 0 + for (before, after, seen) in inputs do + found := found + (addedConstants before after seen).size + let elapsed := (← IO.monoNanosNow) - start + unless found == count do throw <| IO.userError "timed discovery lost constants" + if iteration != 0 then samples := samples.push elapsed + return (samples.qsort (· < ·))[1]! + +/-- Ten times as many declarations must stay below quadratic discovery growth. -/ +unsafe def discoveryScaling : IO Unit := do + let small ← discoveryCost 1000 + let large ← discoveryCost 10000 + IO.println s!"discovery heartbeats: 1000={small}, 10000={large}" + let smallTime ← discoveryTime 1000 + let largeTime ← discoveryTime 10000 + IO.println s!"discovery nanoseconds: 1000={smallTime}, 10000={largeTime}" + unless largeTime < 25 * smallTime do + throw <| IO.userError "constant discovery exceeded discovery growth bound" + +#eval discoveryCases +#eval discoveryScaling diff --git a/Tests/ProofCostFrontend.cs b/Tests/ProofCostFrontend.cs index 78f02ea88..a11d4c3ae 100644 --- a/Tests/ProofCostFrontend.cs +++ b/Tests/ProofCostFrontend.cs @@ -1,12 +1,30 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include ../scripts/ProofCostReport.cs +#:include ../scripts/CertifiedLibraryDonor.cs +#:include ../scripts/ExportVerificationSuites.cs +#:include ../scripts/VerificationGroupRunner.cs +#:include ../scripts/VerificationSuites.cs using System.Text.Json; // Failed imports and earlier commands must not publish a successful measurement. var directory = At("artifacts/ProofCostFrontend"); Directory.CreateDirectory(directory); +var tooling = Path.Combine(directory, "scripts"); +Directory.CreateDirectory(tooling); +var compile = Execute(["lake", "env", "lean", "-o", Path.Combine(tooling, "ProofCost.olean"), + At("scripts/ProofCost.lean")]); +if (compile.Code != 0) throw new InvalidOperationException(compile.Output); +var discovery = Execute(["lake", "env", "lean", At("Tests/ProofCostDiscovery.lean")], leanPath: directory); +if (discovery.Code != 0) throw new InvalidOperationException(discovery.Output); +Console.WriteLine(discovery.Output); var fixtures = new (string Name, string? Error, string Source)[] { ("ValidCostImport", null, """ @@ -22,6 +40,15 @@ theorem auxiliary (value : Nat) : value = value := by theorem subsequent : True := True.intro #print axioms subsequent + private theorem hidden : True := True.intro + #print axioms hidden + + example : True := True.intro + + structure MeasuredPair where + left : Nat + right : Nat + /-- error: expected cost diagnostic -/ #guard_msgs in run_cmd Lean.logError "expected cost diagnostic" @@ -84,7 +111,8 @@ theorem timedOut (value : Nat) : value = value := by throw new InvalidOperationException("A valid proof was not measured:\n" + result.Output); using var measured = JsonDocument.Parse(File.ReadAllText(report)); var commands = measured.RootElement.GetProperty("commands").EnumerateArray().ToArray(); - foreach (var (label, constants) in new[] { ("measured", 1), ("auxiliary", 2), ("subsequent", 1) }) + foreach (var (label, constants) in new[] + { ("measured", 1), ("auxiliary", 2), ("subsequent", 1), ("hidden", 1) }) { var command = commands.Single(command => command.GetProperty("label").GetString() == label); if (command.GetProperty("constants").GetInt32() != constants || @@ -92,9 +120,62 @@ theorem timedOut (value : Nat) : value = value := by command.GetProperty("kernelHeartbeats").GetInt64() <= 0) throw new InvalidOperationException("Missing theorem measurement: " + label); } + if (commands.Count(command => command.GetProperty("kind").GetString() == "example") != 1 || + commands.Single(command => command.GetProperty("label").GetString() == "MeasuredPair") + .GetProperty("constants").GetInt32() < 3) + throw new InvalidOperationException("Examples or generated structure declarations were lost."); if (commands.Where(command => command.GetProperty("kind").GetString() == "command") .Any(command => command.GetProperty("constants").GetInt32() != 0)) throw new InvalidOperationException("A diagnostic command counted declarations again."); } } Console.WriteLine("PASS cost measurement includes auxiliary proofs and rejects failed imports and commands"); + +// Eight importers must await the compiled dependency and publish exactly one row per module. +var tree = Path.Combine(directory, "Scheduling"); +var library = Path.Combine(tree, "library"); +Directory.CreateDirectory(library); +File.WriteAllText(Path.Combine(library, "CIL.olean"), "unused library marker"); +File.WriteAllText(Path.Combine(tree, "Shared.lean"), """ + import Lean + theorem shared : True := True.intro + #print axioms shared + """); +var modules = Enumerable.Range(0, 8).Select(index => "Importer" + index).ToArray(); +foreach (var module in modules) + File.WriteAllText(Path.Combine(tree, module + ".lean"), $$""" + import Shared + theorem {{module}} : True := shared + #print axioms {{module}} + """); +ProofCostReport.Measurement? serial = null; +foreach (var jobs in new[] { 1, 8, 8 }) +{ + var measurement = ProofCostReport.Measure(tree, library, Path.Combine(tree, "work" + jobs), + false, [], modules, jobs, "shared imports"); + if (measurement.Unmeasured.Count != 0) + throw new InvalidOperationException(string.Join(Environment.NewLine, measurement.Unmeasured)); + var measured = measurement.Rows.Where(row => row.Kind == ProofCostReport.ModuleKind).ToArray(); + if (measured.Length != modules.Length + 1 || + measured.Select(row => row.Group).Distinct().Count() != measured.Length) + throw new InvalidOperationException("Shared imports produced duplicate or missing modules."); + serial ??= measurement; + if (serial.Rows.Count != measurement.Rows.Count || serial.Rows.Zip(measurement.Rows).Any(pair => + pair.First.Group != pair.Second.Group || pair.First.Name != pair.Second.Name || + pair.First.Measures.Any(metric => metric.Key switch + { + "elapsedMs" or "kernelMs" => false, + "heartbeats" => Math.Abs(metric.Value - pair.Second.Measures[metric.Key]) > 1_000, + _ => metric.Value != pair.Second.Measures[metric.Key] + }))) + throw new InvalidOperationException("Parallel measurement changed the measured costs."); + Console.WriteLine($"PASS {jobs} jobs measured each of {measured.Length} modules once with matching costs"); +} +File.WriteAllText(Path.Combine(tree, "Left.lean"), "import Right"); +File.WriteAllText(Path.Combine(tree, "Right.lean"), "import Left"); +var cycle = ProofCostReport.Measure(tree, library, Path.Combine(tree, "cycle"), + false, [], ["Left", "Right"], 8, "cyclic imports"); +if (cycle.Rows.Count != 0 || cycle.Unmeasured.Count != 2 || + cycle.Unmeasured.Any(row => !row.Reason.Contains("import cycle", StringComparison.Ordinal))) + throw new InvalidOperationException("Cyclic imports did not produce a bounded failure."); +Console.WriteLine("PASS cyclic imports are rejected before measurement"); diff --git a/Tests/ProofGates.cs b/Tests/ProofGates.cs index db8f319f6..43fbed4ee 100644 --- a/Tests/ProofGates.cs +++ b/Tests/ProofGates.cs @@ -1,9 +1,22 @@ #!/usr/bin/env dotnet #:property PublishAot=false +#:property AssemblyName=LeanCIL.IdentityTests +#:project ../Extractor/Extractor.csproj #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +extern alias IdentityExtractor; + +using System.Diagnostics; using System.Text.Json; +CheckContractSpineScaling(); +if (args is ["--spine-only"]) return; + // One fixture project builds every case of `First` from its FixtureCase property; `Second` is a prebuilt // assembly from another project, so selection by project directory has something to leave out. var cli = new[] { "dotnet", ExtractorPath }; @@ -15,7 +28,7 @@ File.WriteAllText(Path.Combine(fixture, "Gates.csproj"), """ - net10.0 + net11.0 true $(DefineConstants);$(FixtureCase) @@ -41,7 +54,7 @@ public static class Methods """); File.WriteAllText(Path.Combine(second, "Second.csproj"), """ - net10.0true + net11.0true """); File.WriteAllText(Path.Combine(second, "Second.cs"), @@ -72,13 +85,23 @@ open Lean Elab Tactic in string Correctness(string name, string support, string operation, string prefix = "", string tactic = "") => $$""" import Extracted import CIL.Safety.SymbolicExecution + import CIL.Safety.MethodContract import {{support}}Support open CIL.Safety {{prefix}} - /-- The extracted method computes wrapping arithmetic in arbitrary caller memory. -/ - theorem {{name}}.correctness (left right : CIL.W64) (memory : Memory) : - ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 left), .scalar (.i64 right)] - memory = .ok (memory, [.scalar (.i64 (left {{operation}} right))]) := by + /-- The extracted method returns wrapping arithmetic of its arguments and keeps caller memory. -/ + def {{name}}.contract : MethodContract (CIL.W64 × CIL.W64) where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + pre _ _ := True + footprint _ := [] + post input initial final values := + final = initial ∧ values = [.scalar (.i64 (input.1 {{operation}} input.2))] + /-- The extracted method meets its contract in arbitrary caller memory. -/ + theorem {{name}}.correctness : {{name}}.contract.Holds Extracted.program 0 := by + rintro ⟨left, right⟩ memory - + refine MethodContract.Holds.intro (c := {{name}}.contract) (input := (left, right)) memory + [.scalar (.i64 (left {{operation}} right))] ?_ (Confined.refl _ memory) ⟨rfl, rfl⟩ + dsimp only [{{name}}.contract] {{tactic}}cil_checked_steps [] all_goals simp only [BitVec.add_comm] #print axioms {{name}}.correctness @@ -106,29 +129,47 @@ import Extracted #print axioms Trivial.holds """); -// Weaker.lean adds a premise to production's statement, so the kernel must not accept it as production's. +// Weaker.lean strengthens production's precondition, so the kernel must not accept it as production's. File.WriteAllText(Path.Combine(package, "Weaker.lean"), Correctness("Weaker", "First", "+") - .Replace("(memory : Memory) :", "(memory : Memory) (_small : left = 0) :")); + .Replace("pre _ _ := True", "pre input _ := input.1 = 0")); +// Raw.lean states the run itself, which names no contract, so no variant of its gate can be rejected. +File.WriteAllText(Path.Combine(package, "Raw.lean"), """ + import Extracted + import CIL.Safety.SymbolicExecution + open CIL.Safety + /-- The extracted method computes the wrapping sum in arbitrary caller memory. -/ + theorem Raw.correctness (left right : CIL.W64) (memory : Memory) : + ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 left), .scalar (.i64 right)] + memory = .ok (memory, [.scalar (.i64 (left + right))]) := by + cil_checked_steps [] + #print axioms Raw.correctness + + """); // Definitional.lean names production's `0` through a definition, which the kernel unfolds. File.WriteAllText(Path.Combine(package, "Definitional.lean"), """ import Extracted import CIL.Safety.SymbolicExecution + import CIL.Safety.MethodContract import FirstSupport open CIL.Safety /-- The entry method's index. -/ def Definitional.entry : Nat := 0 - /-- Production's statement, proved directly. -/ - theorem Definitional.original (left right : CIL.W64) (memory : Memory) : - ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 left), .scalar (.i64 right)] - memory = .ok (memory, [.scalar (.i64 (left + right))]) := by + /-- Production's contract, declared again. -/ + def Definitional.contract : MethodContract (CIL.W64 × CIL.W64) where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + pre _ _ := True + footprint _ := [] + post input initial final values := + final = initial ∧ values = [.scalar (.i64 (input.1 + input.2))] + /-- Production's statement with the entry index named by `Definitional.entry`. -/ + theorem Definitional.correctness : + Definitional.contract.Holds Extracted.program Definitional.entry := by + rintro ⟨left, right⟩ memory - + refine MethodContract.Holds.intro (c := Definitional.contract) (input := (left, right)) memory + [.scalar (.i64 (left + right))] ?_ (Confined.refl _ memory) ⟨rfl, rfl⟩ + dsimp only [Definitional.contract, Definitional.entry] cil_checked_steps [] all_goals simp only [BitVec.add_comm] - /-- Production's statement with the entry index named by `Definitional.entry`. -/ - theorem Definitional.correctness (left right : CIL.W64) (memory : Memory) : - ∃ fuel, invoke Extracted.program fuel Definitional.entry - [.scalar (.i64 left), .scalar (.i64 right)] - memory = .ok (memory, [.scalar (.i64 (left + right))]) := - Definitional.original left right memory #print axioms Definitional.correctness """); @@ -144,23 +185,32 @@ Definitional.original left right memory """); // A recorded witness gives inputs whose wrong result differs from the wrapping sum. var template = Path.Combine(package, "Refutation.lean.in"); -File.WriteAllText(template, """ +string Witness(string name, string statement, string proof, string operation = "+") => $$""" import Extracted import CIL.Safety.MethodContract open CIL.Safety - /-- The witness inputs make the extracted method return other than their wrapping sum. -/ - theorem FixtureWitness.refuted : ¬ ∀ (left right : CIL.W64) (memory : Memory), - ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 left), .scalar (.i64 right)] - memory = .ok (memory, [.scalar (.i64 (left + right))]) := by - intro holds - obtain ⟨fuel, ran⟩ := holds @LEFT@ @RIGHT@ Memory.empty - have returned := congrArg Prod.snd (invoke_ok_unique ran (rfl : invoke Extracted.program 16 0 - [.scalar (.i64 @LEFT@), .scalar (.i64 @RIGHT@)] Memory.empty = .ok _)) - revert returned - decide - #print axioms FixtureWitness.refuted + /-- Production's contract, declared again. -/ + def {{name}}.contract : MethodContract (CIL.W64 × CIL.W64) where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + pre _ _ := True + footprint _ := [] + post input initial final values := + final = initial ∧ values = [.scalar (.i64 (input.1 {{operation}} input.2))] + /-- The witness inputs make the extracted method return other than the contract's result. -/ + theorem {{name}}.refuted : {{statement}} := + {{proof}}.of_return (input := (@LEFT@, @RIGHT@)) (initial := Memory.empty) trivial + (rfl : invoke Extracted.program 16 0 [.scalar (.i64 @LEFT@), .scalar (.i64 @RIGHT@)] Memory.empty = .ok _) + (by rintro ⟨-, out, rfl, -, wrong⟩; revert wrong; decide) + #print axioms {{name}}.refuted - """); + """; +File.WriteAllText(template, Witness("FixtureWitness", "FixtureWitness.contract.Refuted Extracted.program 0", "")); +// Unrelated.lean.in refutes a contract other than production's: the wrapping product, which XOR also misses. +File.WriteAllText(Path.Combine(package, "Unrelated.lean.in"), + Witness("Unrelated", "Unrelated.contract.Refuted Extracted.program 0", "", "*")); +// Negated.lean.in states the negation of production's contract, which a run the model does not cover can prove. +File.WriteAllText(Path.Combine(package, "Negated.lean.in"), Witness("Negated", + "¬ Negated.contract.Holds Extracted.program 0", "MethodContract.Refuted.not_holds <| MethodContract.Refuted")); var witnesses = new Dictionary { ["WrongXor"] = (1, 1), ["WrongSub"] = (0, 1), ["Agreeing"] = (0, 0) @@ -216,7 +266,8 @@ void Manifest(bool controls, params object[] extra) new { name = "production-verifies", build = Build("Reordered"), profiles = scalar, expect = "rejected", refutation = Trivial }, new { name = "unrelated-refutation", build = Build("WrongXor"), profiles = scalar, expect = "rejected", - refutation = Trivial }, + refutation = new { template = "Unrelated.lean.in", theorem = "Unrelated.refuted", + values = new Dictionary { ["LEFT"] = "1", ["RIGHT"] = "1" } } }, new { name = "weaker-statement", build = Build("Baseline"), profiles = scalar, expect = "certifies", proof = "Weaker.lean", theorem = "Weaker.correctness" }, new { name = "program-differs", build = Build("WrongXor"), profiles = scalar, expect = "certifies", @@ -235,7 +286,10 @@ void Manifest(bool controls, params object[] extra) new { name = "extraction-fails", build = Build("Absent"), profiles = scalar, expect = "rejected", refutation = Refute("WrongXor") }, new { name = "internal-error", build = Build("WrongXor"), profiles = scalar, expect = "rejected", - proof = "Panicking.lean", theorem = "Panicking.correctness", refutation = Refute("WrongXor") } + proof = "Panicking.lean", theorem = "Panicking.correctness", refutation = Refute("WrongXor") }, + new { name = "negated-refutation", build = Build("WrongXor"), profiles = scalar, expect = "rejected", + refutation = new { template = "Negated.lean.in", theorem = "Negated.refuted", + values = new Dictionary { ["LEFT"] = "1", ["RIGHT"] = "1" } } } ]); File.WriteAllText(manifest, JsonSerializer.Serialize(new { @@ -263,7 +317,16 @@ void Manifest(bool controls, params object[] extra) new { name = "second", assembly = "Second/output/Second.dll", method = "Methods::Second(ulong,ulong)", profiles = new[] { "scalar", "x64-v3", "plain" }, proof = "Second.lean", theorem = "Second.correctness" } - } + }.Concat(controls ? new object[] + { + new { name = "raw-first", build = new { project = "fixture" }, method = "Methods::First(ulong,ulong)", + profiles = scalar, proof = "Raw.lean", theorem = "Raw.correctness", + variants = new object[] + { + new { name = "raw-rejected", build = Build("WrongXor"), expect = "rejected", + program = "different", refutation = Refute("WrongXor") } + } } + } : []).ToArray() })); } JsonElement[] Gates(int expectedCode, params string[] options) @@ -342,7 +405,7 @@ string Assembly(JsonElement row) { var generated = File.ReadAllText(Path.Combine(package, "Generated", "first", profile, variant, "Refutation.lean")); Expect(!generated.Contains('@') && !generated.Contains('\r') && - generated.Contains($"holds {witnesses[witness].Left} {witnesses[witness].Right} Memory.empty"), + generated.Contains($"(input := ({witnesses[witness].Left}, {witnesses[witness].Right}))"), "A template was not instantiated with its witness: " + variant); } @@ -419,10 +482,12 @@ void Failed(string variant, string message, Func leg) row.GetProperty("refutation").GetProperty("status").GetString() == "verified" && StatementStatus(row) == "different"); // The refutation and the rejection both hold, so only the statement comparison fails these rows. -Failed("unrelated-refutation", "A refutation of an unrelated statement counted as a rejection", row => +// A genuine counterexample to another contract verifies and still fails the gate at statement identity. +Failed("unrelated-refutation", "A refutation of another contract counted as a rejection", row => Text(row, "stage") == "statement identity" && StatementStatus(row) == "different" && - Text(row, "error")!.Contains("does not state the negation", StringComparison.Ordinal) && - row.GetProperty("statement").GetProperty("variant").GetString()!.StartsWith("Trivial.holds : True")); + row.GetProperty("refutation").GetProperty("status").GetString() == "verified" && + Text(row, "error")!.Contains("does not state the counterexample predicate", StringComparison.Ordinal) && + row.GetProperty("statement").GetProperty("variant").GetString()!.StartsWith("Unrelated.refuted : ")); Failed("weaker-statement", "A weaker own statement certified the variant", row => Text(row, "stage") == "statement identity" && StatementStatus(row) == "different" && !File.Exists(Path.Combine(Text(row, "output")!, "verified.json"))); @@ -444,6 +509,17 @@ void Failed(string variant, string message, Func leg) Text(row, "stage") == "extraction" && Text(row, "build") == "built" && Text(row, "extraction") == "failed"); Failed("internal-error", "A proof that panics counted as a rejection", row => Text(row, "error")!.Contains("Lean reported an internal error", StringComparison.Ordinal)); +// A negated contract follows from a run the model does not cover, so the audit refuses it as a refutation. +Failed("negated-refutation", "A negated contract counted as a refutation", row => + row.GetProperty("refutation").GetProperty("status").GetString() == "failed" && + row.GetProperty("refutation").GetProperty("error").GetString()!.Contains( + "which a run the model does not cover can prove", StringComparison.Ordinal)); +// A statement of the run itself names no contract, so it has no counterexample predicate to refute. +var raw = Row(controls, "raw-rejected", gate: "raw-first"); +Expect(Text(Row(controls, "production", gate: "raw-first"), "status") == "verified" && + Text(raw, "status") == "failed" && Text(raw, "stage") == "statement identity" && + Text(raw, "error")!.Contains("does not state a contract's Holds", StringComparison.Ordinal), + "A rejected variant of a gate that states no contract passed or was misreported", raw); NoRejectedCertificates(controls); // A profile the gate does not list is a manifest error, before anything is built. @@ -470,3 +546,70 @@ void Invalid(string message, string expected) Edit("\"plain\":\"@plain.json\"", "\"scalar\":\"@plain.json\""); Invalid("A named profile shadowed a built-in profile", "Profile scalar has the name of a built-in profile."); Console.WriteLine("PASS build, extraction, program, template, proof and internal failures never count as rejections"); + +static void CheckContractSpineScaling() +{ + const int shortSpine = 2_000; + const int longSpine = 32_000; + const double maximumGrowth = 2.0 * longSpine / shortSpine; + var shortTerm = Statement(shortSpine); + var longTerm = Statement(longSpine); + var warmup = Stopwatch.StartNew(); + do + { + Walk(shortTerm); + Walk(longTerm); + } while (warmup.ElapsedMilliseconds < 250); + + var shorter = Measure(shortTerm); + var longer = Measure(longTerm); + var growth = longer / shorter; + var timing = $"""{shortSpine}: {shorter:F3} ms, {longSpine}: {longer:F3} ms, growth {growth:F1}x"""; + if (growth >= maximumGrowth) + throw new InvalidOperationException($"""Contract spine walk exceeded its {maximumGrowth}x limit: {timing}"""); + Console.WriteLine($"""PASS contract spine walk: {timing}"""); + + static JsonElement Statement(int applications) + { + object[][] nodes = new object[applications + 2][]; + string[] module = ["CIL", "Safety", "MethodContract"]; + string[] name = [.. module, "Holds"]; + nodes[0] = ["const", module, name, Array.Empty()]; + nodes[1] = ["nat", 0]; + int root = 0; + for (int index = 2; index < nodes.Length; index++) + { + nodes[index] = ["app", root, 1]; + root = index; + } + return JsonSerializer.SerializeToElement(new { levelParams = Array.Empty(), nodes, root }); + } + + static double Measure(JsonElement term) + { + const int samples = 5; + var minimumSampleTime = TimeSpan.FromMilliseconds(20); + var times = new double[samples]; + for (int sample = 0; sample < samples; sample++) + { + long start = Stopwatch.GetTimestamp(); + int walks = 0; + TimeSpan elapsed; + do + { + Walk(term); + walks++; + elapsed = Stopwatch.GetElapsedTime(start); + } while (elapsed < minimumSampleTime); + times[sample] = elapsed.TotalMilliseconds / walks; + } + Array.Sort(times); + return times[samples / 2]; + } + + static void Walk(JsonElement term) + { + if (!IdentityExtractor::Program.StatesContract(term)) + throw new InvalidOperationException("Contract spine did not resolve to MethodContract.Holds"); + } +} diff --git a/Tests/ProofOutlines.cs b/Tests/ProofOutlines.cs index 712122f68..9ca4d5980 100644 --- a/Tests/ProofOutlines.cs +++ b/Tests/ProofOutlines.cs @@ -2,6 +2,12 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; using System.Text.RegularExpressions; @@ -31,6 +37,7 @@ var catalog = new (string Name, string Selector, string Group)[] { ("Countdown", "Loops::Countdown(uint)", "control"), + ("WordByte", "Nethermind.LeanCIL.StaticValues/<>c__DisplayClass10_0::b__0(System.Int32)", "control"), ("FillWords", "Loops::FillWords(ulong&,ulong,int)", "control"), ("CountUntil", "Loops::CountUntil(uint,uint)", "control"), ("CountCalls", "Loops::CountCalls(uint)", "control"), @@ -38,11 +45,21 @@ ("SpanAllZero", "Loops::AllZero(System.ReadOnlySpan`1)", "control"), ("SpanSum", "Loops::Sum(System.ReadOnlySpan`1)", "control"), ("TallyTwice", "Loops::TallyTwice(uint)", "control"), + ("TwoStructs", "Loops::TwoStructs(uint)", "control"), + ("InnerCount", "Loops::InnerCount(Examples.Loops/Outer,uint)", "control"), + ("FillWordsDown", "Loops::FillWordsDown(ulong&,ulong,int)", "control"), ("CopyUntilZero", "Loops::CopyUntilZero(System.ReadOnlySpan`1,uint&)", "control"), ("Choose", "Arithmetic::Choose(bool,ulong,ulong)", "control"), + // A SizeOf call steps as the constant of its element's size, alone and tested by a branch. + ("SizeOfInt", "UnsafeLayouts::Size6()", "control"), + ("SizeOfBranch", "Arithmetic::WordAdjusted(int)", "control"), + ("SizeOfEqual", "Arithmetic::WordSized(int)", "control"), ("Narrow", "Arguments::Narrow(byte)", "control"), ("ReverseI16", "Bits::ReverseI16(short)", "control"), ("PowerU32", "Bits::PowerU32(uint)", "control"), + // A bit count of BitOperations and the same count an integer type declares. + ("Population64", "Bits::Population64(ulong)", "control"), + ("PopulationU32", "Bits::PrimitivePopulationU32(uint)", "control"), ("Deposit", "Account::Deposit(System.UInt64)", "objects"), ("Headroom", "Account::get_Headroom()", "objects"), ("SetLimit", "Objects::SetLimit(Examples.Account,System.UInt32)", "objects"), @@ -59,6 +76,18 @@ ("Withdraw", "Objects::Withdraw(Examples.Account,System.UInt64)", "objects"), ("LinkedValue", "Objects::LinkedValue(Examples.Node,Examples.Node)", "objects"), ("IsIdle", "ItemPool::IsIdle(Examples.PooledItem)", "objects"), + // A field tested against a small constant, in a signed, an equality and an unsigned branch. + ("BelowFive", "Gauge::BelowFive()", "objects"), + ("IsFive", "Gauge::IsFive()", "objects"), + ("UnderFive", "Objects::UnderFive(Examples.Account)", "objects"), + // The same tests of words over a loop, a branching helper and straight-line code. + ("CountToFive", "Loops::CountToFive(int)", "control"), + ("CountUpToFive", "Loops::CountUpToFive(uint)", "control"), + ("ReachFive", "Loops::ReachFive(uint)", "control"), + ("AtLeastFive", "Arithmetic::AtLeastFive(int)", "control"), + ("AtMostFive", "Arithmetic::AtMostFive(uint)", "control"), + ("FiveToOne", "Arithmetic::FiveToOne(int)", "control"), + ("Floored", "Gauge::Floored(int)", "control"), // A static helper that tests its word arguments gets a contract stub: its result returned, stored into a // field after the call, and returned by a caller over words alone. ("HelperResult", "Gauge::Current()", "objects"), @@ -122,6 +151,9 @@ ("ArrayReadInt32", "PrimitiveArrays::ReadInt32(int[],int)", "buffers"), ("ArrayReadUInt64", "PrimitiveArrays::ReadUInt64(ulong[],int)", "buffers"), ("ArrayRead", "ArrayElementReferences::Read(int[],int)", "buffers"), + // A generic read specialized to a word, whose typed `ldelem` steps from the element type's shape fact. + ("TypedReadUInt32", "PrimitiveArrays::ReadGeneric(T[],int)", "buffers"), + ("TypedReadUInt64", "PrimitiveArrays::ReadGeneric(T[],int)", "buffers"), ("ArraySum", "Loops::SumIndexed(ulong[])", "buffers"), ("ArrayFill", "Loops::FillIndexed(uint[],uint)", "buffers"), // The receiver of a class instance method beside the array it indexes. @@ -143,6 +175,12 @@ ("PairLength", "AggregateArrays::ConstructedLength(Examples.AggregateArrays/Pair[],System.Int32,System.Int32)", "buffers") }; +// The type arguments a generic entry is extracted at. +var typeArguments = new Dictionary(StringComparer.Ordinal) +{ + ["TypedReadUInt32"] = ["--type-argument", "System.UInt32"], + ["TypedReadUInt64"] = ["--type-argument", "System.UInt64"] +}; var outlined = catalog.Where(entry => (group is null || entry.Group == group) && (cases is null || cases.Contains(entry.Name, StringComparer.Ordinal))).ToArray(); if (catalog.Select(entry => entry.Name).Distinct(StringComparer.Ordinal).Count() != catalog.Length) @@ -172,7 +210,22 @@ var scratch = Path.Combine(outlineRoot, name); Directory.CreateDirectory(scratch); var extraction = Path.Combine(scratch, "Extraction"); - Run(Extract("extract", assembly, "Examples", selector, extraction)); + var sourceAssembly = name == "WordByte" ? At("Extractor/bin/Nethermind.LeanCIL.dll") : assembly; + if (name == "TypedReadUInt32") + { + // The live generic read and the unreachable byte allocation have different closed element types. + sourceAssembly = Path.Combine(scratch, "Examples.dll"); + using var image = ModuleDefinition.ReadModule(assembly); + var read = image.GetType("Examples.PrimitiveArrays").Methods.Single(method => method.Name == "ReadGeneric"); + var il = read.Body.GetILProcessor(); + il.Emit(OpCodes.Ldc_I4_0); + il.Emit(OpCodes.Newarr, image.TypeSystem.Byte); + il.Emit(OpCodes.Pop); + il.Emit(OpCodes.Br, read.Body.Instructions[0]); + image.Write(sourceAssembly); + } + Run([.. Extract("extract", sourceAssembly, name == "WordByte" ? null : "Examples", selector, extraction), + .. typeArguments.GetValueOrDefault(name, [])]); var outline = Path.Combine(scratch, "Proof.lean"); Run(Cli("new-proof", extraction, outline)); Run(Cli("new-proof", extraction, Path.Combine(scratch, "Again.lean"))); @@ -203,22 +256,60 @@ if (Selected("PairLength")) { - // The element definition must read back as the extraction's own element, or the outline states another array type, - // and a body the outline does not step may write the array, so its footprint is the author's to state. - var pairOutline = File.ReadAllText(Path.Combine(outlineRoot, "PairLength", "Proof.lean")); + // The element definition must read back as the element the constructor call's model resolves from the + // extraction's layouts, or the outline states another array type; a changed width must be refused. + // A body the outline does not step may write the array, so its footprint is the author's to state. + var pairScratch = Path.Combine(outlineRoot, "PairLength"); + var pairOutline = File.ReadAllText(Path.Combine(pairScratch, "Proof.lean")); var pairElement = Regex.Match(pairOutline, @"@\[reducible\] def sourceElement : CIL\.ArrayElement :=\n (?(?:[^\n]*\\\n)+[^\n]*)\n"); - var pairExtraction = File.ReadAllText(Path.Combine(outlineRoot, "PairLength", "Extraction", "Extracted.lean")); if (!pairElement.Success || - !pairExtraction.Contains($"({Regex.Replace(pairElement.Groups["term"].Value, @"\\\n\s*", "")})", - StringComparison.Ordinal) || !pairOutline.Contains(" sorry -- caller memory the method may write", StringComparison.Ordinal)) throw new InvalidOperationException("The PairLength outline does not name its struct element across lines " + "or leaves its footprint stated:\n" + pairOutline); - Console.WriteLine("PASS a struct element wider than a line reads back as the extraction's element, and an " + - "unstepped body over a value array leaves its footprint to the author"); + var pairTerm = Regex.Replace(pairElement.Groups["term"].Value, @"\\\n\s*", ""); + var widened = Regex.Replace(pairTerm, @"\d+$", digits => (int.Parse(digits.Value) + 1).ToString()); + (int Code, string Output) ResolvedElement(string element) + { + var check = Path.Combine(pairScratch, "Element.lean"); + File.WriteAllText(check, pairOutline + $""" + + open CIL in + /-- The constructor call's resolved model builds spans of the outline's element. -/ + example : (Extracted.program[0]?.bind fun body => body.code.findSome? fun + | .call method _ => Extraction.runtimeModel body.environment .call method + | _ => none) = some (.spanInPlace (.array {element} true .counted)) := by + decide +kernel + + """); + var result = Execute([verification.LeanExecutable, check], + leanPath: pairScratch + Path.PathSeparator + libraryPath); + return (result.Code, result.Output); + } + var resolved = ResolvedElement("ExampleProof.sourceElement"); + var changed = ResolvedElement($"({widened})"); + if (resolved.Code != 0 || changed.Code == 0 || !changed.Output.Contains("is false", StringComparison.Ordinal)) + throw new InvalidOperationException("The PairLength element is not the one its constructor resolves:\n" + + resolved.Output + "\n" + changed.Output); + Console.WriteLine("PASS a struct element wider than a line reads back as the element its constructor resolves, " + + "and an unstepped body over a value array leaves its footprint to the author"); } +string[] sizeCases = ["SizeOfInt", "SizeOfBranch", "SizeOfEqual"]; +foreach (var name in sizeCases.Where(Selected)) +{ + // A SizeOf call runs as the constant of its element's size, so the outline steps through it as a literal. + var sizeOutline = File.ReadAllText(Path.Combine(outlineRoot, name, "Proof.lean")); + if (sizeOutline.Contains("-- not outlined:", StringComparison.Ordinal) || + !sizeOutline.Contains("cil_checked_steps", StringComparison.Ordinal)) + throw new InvalidOperationException($"The {name} outline does not step its SizeOf call:\n{sizeOutline}"); + // A test of two literals is decided by the outline rather than split into an infeasible side. + if (name == "SizeOfEqual" && sizeOutline.Contains("by_cases", StringComparison.Ordinal)) + throw new InvalidOperationException($"The {name} outline splits on a test of literals:\n{sizeOutline}"); +} +if (sizeCases.Any(Selected)) + Console.WriteLine("PASS an outline steps SizeOf as a constant, tested by an argument or decided on literals"); + if (Selected("DepositFrom")) { // A loop asks write authority only of the object whose field it or a callee stores; the other is only read. @@ -232,6 +323,35 @@ "Readable of the one it reads"); } +{ + // A literal operand is written as `simp` normalizes it, so the split's fact rewrites the simplified goal: + // by its word for an equality or an unsigned order, and by its value for a signed one. + (string Case, string Fact)[] branches = + [ + ("BelowFive", "branch1 : (Gauge.Level.of initial gauge).toInt < 5"), + ("IsFive", "branch1 : (Gauge.Level.of initial gauge) = 5#32"), + ("UnderFive", "branch1 : (Account.Limit.of initial account) < 5#32"), + ("CountToFive", "guard : count.toInt < 5"), + ("CountUpToFive", "guard : count < 5#32"), + ("ReachFive", "guard : count = 5#32"), + ("AtLeastFive", "branch1 : x.toInt < 5"), + ("AtMostFive", "branch1 : 5#32 < x"), + ("FiveToOne", "branch1 : x = 5#32"), + ("Floored", "branch1 : value.toInt < 5") + ]; + var checkedBranches = 0; + foreach (var (name, fact) in branches.Where(branch => Selected(branch.Case))) + { + checkedBranches++; + if (!File.ReadAllText(Path.Combine(outlineRoot, name, "Proof.lean")) + .Contains($"by_cases {fact}\n", StringComparison.Ordinal)) + throw new InvalidOperationException($"The {name} outline does not split on `{fact}`"); + } + if (checkedBranches > 0) + Console.WriteLine($"PASS {checkedBranches} outlines write a branch against a small constant as simp " + + "normalizes it"); +} + if (Selected("IsIdle")) { // An object local changes its frame slot at each store, so the head must name the slot from the loop state, and its @@ -1522,6 +1642,8 @@ def Postcondition (target1 : SpanView) (value : W32) (_ final : Memory) : Prop : (" sorry -- Precondition excludes an empty `target1`", " simp [Precondition, target1Count] at assumed"), (" case postcondition => sorry", " case postcondition => exact target1Filled")]), + ("TypedReadUInt32", ArrayReadSpecification("W32", "")), + ("TypedReadUInt64", ArrayReadSpecification("W64", "")), ("ArrayReadInt32", ArrayReadSpecification("W32", "")), ("ArrayReadUInt64", ArrayReadSpecification("W64", "")), ("ArrayRead", ArrayReadSpecification("W32", ", leaveFrame")), @@ -1824,7 +1946,7 @@ cases restAddress if (Selected("PacketValue")) { - // The same filled packet proof must reject a changed stored value for all initial byte patterns. + // Compare the filled outline and the changed store against the same packet contract. var wrongPacket = Path.Combine(outlineRoot, "WrongPacket.dll"); using (var module = ModuleDefinition.ReadModule(assembly)) { @@ -1833,14 +1955,32 @@ cases restAddress module.Write(wrongPacket); } var wrongPacketOutput = Path.Combine(outlineRoot, "WrongPacket"); - Run(Extract("extract", wrongPacket, "Examples", "Arguments::Struct(Examples.Packet)", wrongPacketOutput)); - var wrongPacketResult = Execute(Cli("verify", wrongPacketOutput, "--proof", - Path.Combine(outlineRoot, "PacketValue", "Filled.lean"))); - if (wrongPacketResult.Code == 0 || !wrongPacketResult.Output.Contains("Stage: proof checking") || - File.Exists(Path.Combine(wrongPacketOutput, "verified.json"))) - throw new InvalidOperationException("The filled packet outline accepted a wrong stored value:\n" + - wrongPacketResult.Output); - Console.WriteLine("PASS the filled packet outline rejects a wrong stored value"); + const string packetSelector = "Arguments::Struct(Examples.Packet)"; + verification.VerifyContractVariant(assembly, "Examples", packetSelector, + Path.Combine(outlineRoot, "PacketContract"), + wrongPacket, packetSelector, wrongPacketOutput, """ + /-- The returned payload is 23 for every twelve-byte packet, including its padding. -/ + def packet : MethodContract (Vector (BitVec 8) 12) W64 where + args value := [.scalar (.aggregate (List.ofFn value.get))] + results value := [.scalar (.i64 value)] + pre _ memory := memory = Memory.empty + footprint _ := [] + post _ _ _ value := value = 23 + """, """ + /-- The filled outline proves the payload contract for all initial byte patterns. -/ + theorem correctness : packet.Holds Extracted.program 0 := by + rintro value _ rfl + obtain ⟨fuel, final, values, ran, result, encoded, exactValue⟩ := + ExampleProof.correctness value _ _ ⟨rfl, rfl, trivial⟩ + exact ⟨fuel, (final, values), ran, by constructor <;> simp [Memory.empty], + result, encoded, exactValue⟩ + """, """ + /-- The mutant writes and returns 24 for an all-zero packet. -/ + theorem correctness : packet.Refuted Extracted.program 0 := + .of_return (input := Vector.replicate 12 0) (initial := Memory.empty) rfl + (rfl : invoke Extracted.program 32 0 (packet.args (Vector.replicate 12 0)) Memory.empty = .ok _) + (by rintro ⟨-, _, encoded, rfl⟩; revert encoded; decide) + """, example: Path.Combine(outlineRoot, "PacketValue", "Filled.lean")); // A source parameter named index must remain the byte vector, not a generated byte-index binder. var indexedPacket = Path.Combine(outlineRoot, "IndexedPacket.dll"); @@ -1904,6 +2044,17 @@ cases restAddress "fails naming null"); } +foreach (var name in new[] { "Population64", "PopulationU32" }) +{ + if (!Selected(name)) continue; + // A bit-count call steps as its total intrinsic, so the outline takes the whole body in one run. + var countOutline = File.ReadAllText(Path.Combine(outlineRoot, name, "Proof.lean")); + if (countOutline.Contains("-- not outlined:", StringComparison.Ordinal) || + !countOutline.Contains("case run => cil_checked_steps", StringComparison.Ordinal)) + throw new InvalidOperationException($"The {name} outline does not step its bit-count calls:\n" + countOutline); + Console.WriteLine($"PASS the {name} outline steps its bit-count calls"); +} + if (Selected("ReceiverArraySum")) { // An array beside the receiver of a class instance method keeps its element view, so the indexed loop steps. @@ -1992,16 +2143,13 @@ cases restAddress string OutlineExtraction(string name) => Path.Combine(outlineRoot, name, "Extraction"); -if (group == "control") +if (Selected("WordByte")) { // LeanCIL's own closure that lays out a static word's bytes: its outline states the getter's contract // and steps through it, and the checked-in proof fills only the outline's holes and verifies. var wordByte = Path.Combine(outlineRoot, "WordByte"); - var wordByteExtraction = Path.Combine(wordByte, "Extraction"); - Run(Cli("extract", At("Extractor/bin/Nethermind.LeanCIL.dll"), - "Nethermind.LeanCIL.StaticValues/<>c__DisplayClass10_0::b__0(System.Int32)", wordByteExtraction)); + var wordByteExtraction = OutlineExtraction("WordByte"); var wordByteOutline = Path.Combine(wordByte, "Proof.lean"); - Run(Cli("new-proof", wordByteExtraction, wordByteOutline)); var wordByteSource = File.ReadAllText(wordByteOutline); if (wordByteSource.Contains("-- not outlined:", StringComparison.Ordinal) || !wordByteSource.Contains("theorem get_Bits_holds", StringComparison.Ordinal)) diff --git a/Tests/ProofWorkspace.cs b/Tests/ProofWorkspace.cs index 8c997c981..054028d0d 100644 --- a/Tests/ProofWorkspace.cs +++ b/Tests/ProofWorkspace.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Collections.Concurrent; using System.Diagnostics; @@ -25,7 +30,7 @@ var project = Path.Combine(consumer, "Workspace.csproj"); File.WriteAllText(project, """ - net10.0true + net11.0true """); var wide = Path.Combine(consumer, "Wide.cs"); @@ -51,6 +56,15 @@ public static class Arith public static ulong Twice(ulong x) => x + x; } """); +// Compiled between the other two: editing the first body moves the later ones, whose evidence must not change. +File.WriteAllText(Path.Combine(consumer, "Third.cs"), """ + namespace Workspace; + + public static class Third + { + public static ulong Thrice(ulong x) => x + x + x; + } + """); Console.WriteLine("Building the workspace consumer fixture"); Run(["dotnet", "build", project, "-c", "Release"]); var assembly = Path.Combine(consumer, "bin", "Workspace.dll"); @@ -141,7 +155,7 @@ void RequirePinned(string owner) "^srcDir = \"([^\"]+)\"$", RegexOptions.Multiline).Groups[1].Value; var pins = Directory.EnumerateFiles(Path.GetDirectoryName(required)!, Path.GetFileName(required) + ".pin-*") .Select(File.ReadAllText).ToArray(); - var cached = KeyName().IsMatch(Path.GetFileName(required)) && + var cached = KeyName.IsMatch(Path.GetFileName(required)) && SamePath(Path.GetDirectoryName(required)!, LibraryCacheRoot); if (cached && !pins.Contains(frameworkPackage)) throw new InvalidOperationException("No pin beside " + required + " names the package " + frameworkPackage); @@ -548,6 +562,17 @@ void RequireCertifiedUnwritten(string after) File.WriteAllText(twiceProof, "-- written once the module name is known\n"); var twiceKey = KeyOf(Run([.. cli, "workspace", assembly, twiceMethod, "--profile", "scalar", "--proof", twiceProof, "--theorem", "TwiceProof.length"])); +var thriceProof = Path.Combine(package, "ThriceProof.lean"); +File.WriteAllText(thriceProof, "-- written once the module name is known\n"); +var thriceKey = KeyOf(Run([.. cli, "workspace", assembly, "Workspace.Third::Thrice(ulong)", "--profile", "scalar", + "--proof", thriceProof, "--theorem", "ThriceProof.length"])); +File.WriteAllText(thriceProof, $""" +import Extracted.{thriceKey} +namespace ThriceProof +theorem length : Extracted.method0.code.length = {CodeLength(thriceKey)} := by decide +#print axioms length +end ThriceProof +"""); void WriteTwiceProof(int length) => File.WriteAllText(twiceProof, $""" import Extracted.{twiceKey} namespace TwiceProof @@ -560,6 +585,7 @@ end TwiceProof // Both proofs are open at once: each checks in place against its own method's extraction. CheckedProof(); if (CheckProof(twiceProof).Code != 0) throw new InvalidOperationException("The second proof does not check in place."); +if (CheckProof(thriceProof).Code != 0) throw new InvalidOperationException("The third proof does not check in place."); string[] watch = [.. cli, "watch", package, "--project", project, "--format", "json"]; // Scratch extractions on the package's drive, as on a single-drive machine, so a path recorded relative to one @@ -611,6 +637,7 @@ string[] Refreshed(JsonElement cycle) => var (first, firstTime) = WatchOnce(passes: true); Expect(first, "Proof.lean", "verified", rechecked: true); Expect(first, "TwiceProof.lean", "verified", rechecked: true); +Expect(first, "ThriceProof.lean", "verified", rechecked: true); // Each proof is verified against an extraction made in its own directory, whose recorded assembly path is // relative to that directory; a rooted recorded path names itself wherever the extraction lies. foreach (var identity in Directory.EnumerateFiles(Path.Combine(editor, "selections"), "identity.json", @@ -628,6 +655,7 @@ string[] Refreshed(JsonElement cycle) => var (quiet, quietTime) = WatchOnce(passes: true); Expect(quiet, "Proof.lean", "verified", rechecked: false); Expect(quiet, "TwiceProof.lean", "verified", rechecked: false); +Expect(quiet, "ThriceProof.lean", "verified", rechecked: false); if (Refreshed(quiet).Length != 0) throw new InvalidOperationException("An unchanged build refreshed: " + quiet); Console.WriteLine($"PASS watch verifies both proofs ({firstTime.TotalSeconds:F1}s), then reruns nothing when " + $"nothing changed ({quietTime.TotalSeconds:F1}s)"); @@ -639,10 +667,12 @@ string[] Refreshed(JsonElement cycle) => var (edited, editedTime) = WatchOnce(passes: false); // Exactly the edited method's extraction is refreshed and its proof rechecked; the other one is not touched. if (!Refreshed(edited).Contains("Extracted." + twiceKey) || - Refreshed(edited).Any(module => module.EndsWith("." + key, StringComparison.Ordinal))) + Refreshed(edited).Any(module => module.EndsWith("." + key, StringComparison.Ordinal) || + module.EndsWith("." + thriceKey, StringComparison.Ordinal))) throw new InvalidOperationException("The edit did not refresh exactly the edited method: " + edited); Expect(edited, "TwiceProof.lean", "failed", rechecked: true); Expect(edited, "Proof.lean", "verified", rechecked: false); +Expect(edited, "ThriceProof.lean", "verified", rechecked: false); var located = Outcome(edited, "TwiceProof.lean").GetProperty("report").GetProperty("failures")[0].GetProperty("source"); if (Path.GetFullPath(located.GetProperty("file").GetString()!) != Path.GetFullPath(twiceProof) || located.GetProperty("line").GetInt32() != 3) @@ -767,3 +797,7 @@ JsonElement Next() { if (!watcher.HasExited) watcher.Kill(entireProcessTree: true); } + +// An edit that moves later bodies leaves their generated evidence unchanged, which the refreshes above rely on. +Run(["dotnet", "run", "--file", At("Tests/IncrementalEvidence.cs")], streamOutput: true, + progress: "Incremental evidence of unedited methods"); diff --git a/Tests/PublicProofSurface.lean b/Tests/PublicProofSurface.lean index e91e04c0a..a864e66e0 100644 --- a/Tests/PublicProofSurface.lean +++ b/Tests/PublicProofSurface.lean @@ -20,7 +20,10 @@ def successor : MethodContract CIL.W32 CIL.W32 where /-- The caller forwards one checked scalar and returns the helper's output. -/ @[cil_code] def forwarding : CIL.Method := - { locals := [], returnsValue := true, code := [.call 1 1, .ret] } + { locals := [], returnsValue := true, + code := [.call { owner := .void, name := ⟨1⟩, hasThis := false, genericArity := 0, + returnType := .void, parameters := [.void] } {}, .ret], + links := [(0, .direct 1)] } /-- A call continuation selects postcondition and preservation facts by their names. -/ theorem named_call_continuation (program : CIL.Program) (memory : Memory) (value : CIL.W32) @@ -42,10 +45,33 @@ theorem named_call_continuation (program : CIL.Program) (memory : Memory) (value exact ⟨final_formed, by rw [output_eq]⟩ #print axioms named_call_continuation +open CIL.Extraction in +private def wordVector (width : Nat) : TypeExpr := + .apply (.constructor intrinsicsScope s!"System.Runtime.Intrinsics.Vector{width}`1") + (.atom runtimeScope "System.UInt64") + +open CIL.Extraction in +private def permuteMethod : MethodRef := + { owner := .atom intrinsicsScope "System.Runtime.Intrinsics.X86.Avx2", name := name!"Permute4x64", + hasThis := false, genericArity := 0, returnType := wordVector 256, + parameters := [wordVector 256, .atom runtimeScope "System.Byte"] } + +open CIL.Extraction in +private def zeroMethod : MethodRef := + { owner := wordVector 256, name := name!"get_Zero", hasThis := false, + genericArity := 0, returnType := wordVector 256, parameters := [] } + +open CIL.Extraction in +private def equalityMethod (width : Nat) : MethodRef := + { owner := wordVector width, name := name!"op_Equality", hasThis := false, + genericArity := 0, returnType := .atom runtimeScope "System.Boolean", + parameters := [wordVector width, wordVector width] } + /-- The vector control is an ordinary literal in the emitted instructions. -/ @[cil_code] def reverseVector : CIL.Method := { profile := { architecture := .x64, avx2 := true }, locals := [], returnsValue := true, - code := [.const32 27, .intrinsic (.avx2 .permute4x64) 2, .ret] } + code := [.ldc_i4 27, .call permuteMethod, .ret] } +cil_runtime_models reverseVector /-- A rewrite stated with a literal still matches the vector value left by stepping. -/ theorem vector_literal_rewrite (memory : Memory) (frame : Frame) (vector reversed : BitVec 256) @@ -60,12 +86,26 @@ theorem vector_literal_rewrite (memory : Memory) (frame : Frame) (vector reverse rw [reverse] #print axioms vector_literal_rewrite +open CIL.Extraction in +/-- The closed Unsafe.BitCast from the fixture's 32-byte aggregate to Vector256. -/ +private def bytesToVector : MethodRef := + let source := TypeExpr.aggregate "Fixture::Bytes" 0 + let target := wordVector 256 + { owner := unsafeOwner, name := name!"BitCast", hasThis := false, genericArity := 2, + methodArguments := [source, target], parameters := [source], returnType := target } + /-- An aggregate is reinterpreted as a vector and compared with zero before a narrow return. -/ @[cil_code] def aggregateZero : CIL.Method := - { locals := [], returnsValue := true, code := [.loadAggregate 32, - .memory (.bitcastValue (.aggregate 32) .vector256), - .intrinsic (.vector (.zero 256)) 0, .intrinsic (.vector (.equalsAll 256)) 2, - .ret (some .unsigned8)] } + { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + environment := { + rawLayouts := [{ identity := "Fixture::Bytes", explicit := true, packing := 1, + declaredSize := 32, fields := [] }] + layouts := #[⟨{ size := 32, fields := [] }, 1⟩] }, + locals := [], returnsValue := true, code := [.ldobj (.aggregate "Fixture::Bytes" 0), + .call bytesToVector, + .call zeroMethod, .call (equalityMethod 256), .ret] } +cil_runtime_models aggregateZero /-- Snapshot map rewrites and symbolic intrinsic results remain usable after checked stepping. -/ theorem aggregate_boolean_return (memory : Memory) (frame : Frame) (input : Reference) @@ -81,8 +121,11 @@ theorem aggregate_boolean_return (memory : Memory) (frame : Frame) (input : Refe /-- A vector comparison returns its scalar Boolean through the narrow return instruction. -/ @[cil_code] def equalVector : CIL.Method := - { locals := [], returnsValue := true, - code := [.intrinsic (.vector (.equalsAll 128)) 2, .ret (some .unsigned8)] } + { + declared := { returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" }, + locals := [], returnsValue := true, + code := [.call (equalityMethod 128), .ret] } +cil_runtime_models equalVector /-- An intrinsic's private result helper does not obstruct a symbolic scalar return. -/ theorem vector_boolean_return (memory : Memory) (frame : Frame) (left right : BitVec 128) : @@ -96,18 +139,51 @@ theorem checked_conversion_result (body : CIL.Method) (frame : Frame) (memory : (input : CIL.W64) (calculated : CIL.convertInteger 32 false true (.i64 input) = .ok (.i32 (BitVec.ofNat 32 input.toNat))) : - step body (.checkedConvert 32 false true) 0 [] frame [.scalar (.i64 input)] memory = + step body (.conv_ovf_un .u4) 0 [] frame [.scalar (.i64 input)] memory = .ok (.next 1 [.scalar (.i32 (BitVec.ofNat 32 input.toNat))] frame memory) := by cil_checked_step [calculated] #print axioms checked_conversion_result +/-- A fact about a 64-bit signed range drives the conversion to the native `int` target. -/ +theorem native_conversion_result (body : CIL.Method) (frame : Frame) (memory : Memory) + (input : CIL.W64) + (calculated : CIL.convertInteger 64 true true (.i64 input) = .ok (.i64 input)) : + step body (.conv_ovf_un .i) 0 [] frame [.scalar (.i64 input)] memory = + .ok (.next 1 [.scalar (.i64 input)] frame memory) := by + cil_checked_step [calculated] +#print axioms native_conversion_result + /-- A two-word aggregate argument is described independently of any callee body. -/ def pairView (reference : Reference) : ArgumentView := ⟨reference, 16⟩ +/-- The checked call that accepts the output byref before the two field reads. -/ +private def skipWord : CIL.Extraction.MethodRef := + let word := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.UInt64" + { owner := CIL.Extraction.unsafeOwner, name := name!"SkipInit", hasThis := false, + genericArity := 1, parameters := [.byref word], methodArguments := [word], returnType := .void } + +private def pairEnvironment : CIL.LayoutEnvironment := + { rawLayouts := [ + { identity := "Pair", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .unsigned64 }, { type := .unsigned64 }] }] + layouts := #[⟨⟨16, [⟨0, 8, false⟩, ⟨8, 8, false⟩]⟩, 8⟩] } + +private def low : CIL.Extraction.FieldRef := + { owner := .aggregate "Pair" 0, index := 0, name := name!"Low", + type := .atom CIL.Extraction.runtimeScope "System.UInt64" } + +private def high : CIL.Extraction.FieldRef := { low with index := 1, name := name!"High" } + +cil_field_shape pairEnvironment low +cil_field_shape pairEnvironment high + /-- Both source fields are loaded before any output could overwrite them. -/ @[cil_code] def readPair : CIL.Method := - { locals := [], returnsValue := true, code := [.arg 2, .skipInit, - .arg 0, .fieldAt 0 8, .arg 1, .fieldAt 8 8, .add, .ret] } + { environment := pairEnvironment, locals := [], returnsValue := true, code := [.ldarg 2, .call skipWord {}, + .ldarg 0, .ldfld low, .ldarg 1, .ldfld high, .add, .ret] } + +cil_instruction_equations Tests.PublicProofSurface.readPair +cil_runtime_models Tests.PublicProofSurface.readPair /-- Named call validity supplies field reads even when output aliases an entire input. -/ theorem overlapping_fields (memory : Memory) (left right : Reference) @@ -130,7 +206,7 @@ theorem mixed_local_snapshot {caller current final : Memory} {word vector : Refe (confined : Confined [(word, 8)] current final) (formed : final.WellFormed) (loaded : read final word 8 1 = .ok (numberBytes value.toNat 8)) (body : CIL.Method) (pc : Nat) (arguments stack : List Value) : - step body (.local 1) pc arguments + step body (.ldloc 1) pc arguments { activation := 0, locals := [.bytes .word64 word, .bytes .vector256 vector], owned := [] } stack final = .ok (.next (pc + 1) (.scalar (.v256 saved) :: stack) { activation := 0, locals := [.bytes .word64 word, .bytes .vector256 vector], owned := [] } @@ -143,7 +219,7 @@ theorem mixed_local_snapshot {caller current final : Memory} {word vector : Refe #print axioms mixed_local_snapshot /-- One reusable arithmetic prefix adds one to the top word. -/ -def incrementCode : List CIL.Op := [.const32 1, .add] +def incrementCode : List CIL.Instruction := [.ldc_i4 1, .add] /-- A placed segment computes its stack value through the shared checked stepper. -/ theorem increment_segment {program : CIL.Program} {method start : Nat} {body : CIL.Method} @@ -169,7 +245,7 @@ theorem repeated_segment {program : CIL.Program} {method : Nat} {body : CIL.Meth /-- A countdown body has one return and one continuing edge. -/ @[cil_code] def countdown : CIL.Method := { locals := [], returnsValue := true, - code := [.dup, .brzero 5, .const32 1, .sub, .branch 0, .ret] } + code := [.dup, .brfalse 5, .ldc_i4 1, .sub, .br 0, .ret] } /-- The checked continuing path retains the exact stack expression at the loop head. -/ theorem loop_cutpoint (memory : Memory) (frame : Frame) (value : CIL.W32) diff --git a/Tests/ReadOnlyArrayReferences.cs b/Tests/ReadOnlyArrayReferences.cs index 8aa9c8d5a..6466bc585 100644 --- a/Tests/ReadOnlyArrayReferences.cs +++ b/Tests/ReadOnlyArrayReferences.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs @@ -78,28 +83,37 @@ """); evidence.Add(proof, "PASS certified readonly reference composition: " + name); if (name != "Element") continue; - const string target = ".arrayAddress .signed32 false"; - if (!source.Contains(target, StringComparison.Ordinal)) throw new Exception("Missing readonly mutation target"); + // The address flag is decoded from the raw prefix, so a writable forgery drops the prefix from the raw body, + // which its bytes bind. + if (!source.Contains("cil_decoded_method method0 ", StringComparison.Ordinal)) + throw new Exception("Readonly element address is not decoded from its body"); + const string target = "opcode := 65054, operand := .none }"; + var body = NarrowEvidence(source, "method0_body_validated"); + if (!body.Contains(target, StringComparison.Ordinal)) throw new Exception("Missing readonly mutation target"); var forged = Path.Combine(root, "ForgedWritable.lean"); - File.WriteAllText(forged, source.Replace(target, ".arrayAddress .signed32 true", StringComparison.Ordinal)); + File.WriteAllText(forged, body.Replace(target, "opcode := 0, operand := .none }", StringComparison.Ordinal)); var rejection = Execute(["lake", "env", "lean", forged], cwd: FixturePackage()); - if (rejection.Code == 0 || !rejection.Output.Contains("ScalarFlowValid", StringComparison.Ordinal)) - throw new Exception("Writable forgery did not fail scalar validation: " + rejection.Output); + const string bodyDecisionFailed = "the decoded method does not match its body bytes or metadata"; + if (rejection.Code == 0 || !rejection.Output.Contains(bodyDecisionFailed, StringComparison.Ordinal)) + throw new Exception("Writable forgery did not fail body decoding: " + rejection.Output); } -foreach (var branch in new[] { false, true }) +foreach (var mutation in new[] { "separated", "branch", "dead-branch" }) { + var branch = mutation != "separated"; using var module = ModuleDefinition.ReadModule(assembly); var method = module.GetType("Examples.ArrayElementReferences").Methods.Single(method => method.Name == "Element"); var prefix = method.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Readonly); var address = prefix.Next; var il = method.Body.GetILProcessor(); - if (branch) il.InsertBefore(prefix, Instruction.Create(OpCodes.Br, address)); + if (mutation == "branch") il.InsertBefore(prefix, Instruction.Create(OpCodes.Br, address)); + // An unreachable branch after the final return still may not enter the prefixed address. + else if (mutation == "dead-branch") il.Append(Instruction.Create(OpCodes.Br, address)); else il.InsertAfter(prefix, Instruction.Create(OpCodes.Nop)); - var malformed = Path.Combine(root, branch ? "Branch.dll" : "Separated.dll"); + var malformed = Path.Combine(root, mutation + ".dll"); module.Write(malformed); var rejection = Execute(["dotnet", ExtractorPath, "extract", malformed, "ArrayElementReferences::Element(System.Int32[],System.Int32)", Path.Combine(root, "Rejected")]); - var expected = branch ? "Branch enters an array address" : "Readonly prefix requires"; + var expected = branch ? "Branch enters the instruction after a prefix" : "Readonly prefix requires"; if (rejection.Code == 0 || !rejection.Output.Contains(expected, StringComparison.Ordinal)) throw new Exception("Wrong readonly-prefix rejection: " + rejection.Output); Console.WriteLine("PASS malformed readonly prefix: " + expected); diff --git a/Tests/ReadOnlyArrayReferences.lean b/Tests/ReadOnlyArrayReferences.lean index 24213cc15..86fb61226 100644 --- a/Tests/ReadOnlyArrayReferences.lean +++ b/Tests/ReadOnlyArrayReferences.lean @@ -1,5 +1,6 @@ import CIL.Safety.Execution -import CIL.Extraction.Translation +import CIL.Extraction.DecodedMethod +import Tests.Support.RawDecoding /-! # Readonly element references retain their restriction through frames and certificates -/ @@ -15,7 +16,7 @@ private def memory : Memory := { /-- The prefix restricts the returned reference even with writable backing storage. -/ theorem restricted_address : - memoryInstruction (.arrayAddress .signed32 false) + elementAddressInstruction (.value .signed32) true [.scalar (.i32 1), .array ⟨.address ⟨0, 0⟩, 2, .signed32⟩] memory = .ok (memory, [.readOnlyReference (.address ⟨0, 4⟩)]) := rfl #print axioms restricted_address @@ -29,14 +30,14 @@ theorem local_preserves_restriction : /-- Stores through restricted references fail despite the backing allocation's write authority. -/ theorem restricted_store_rejected : - instruction (.storeScalar 4) + instruction (.stind .i4) [.scalar (.i32 7), .readOnlyReference (.address ⟨0, 4⟩)] memory = .error (.memory .unwritable (.address ⟨0, 4⟩) 4) := rfl #print axioms restricted_store_rejected /-- Restricted references still permit initialized scalar reads. -/ theorem restricted_load : - instruction (.loadScalar 4 false) [.readOnlyReference (.address ⟨0, 4⟩)] memory = + instruction (.ldind .u4) [.readOnlyReference (.address ⟨0, 4⟩)] memory = .ok (memory, [.scalar (.i32 0)]) := rfl #print axioms restricted_load @@ -44,7 +45,7 @@ theorem restricted_load : theorem alias_update_visible : (do let stored ← checkedAt (.address ⟨0, 4⟩) 4 (write memory ⟨0, 4⟩ (numberBytes 7 4) 1) - let (_, values) ← instruction (.loadScalar 4 false) + let (_, values) ← instruction (.ldind .u4) [.readOnlyReference (.address ⟨0, 4⟩)] stored pure values) = (Except.ok [.scalar (.i32 7)] : ExecutionResult (List Value)) := rfl #print axioms alias_update_visible @@ -75,29 +76,29 @@ theorem view_reference_outside : /-- Field loads and field addresses through a restricted reference stay readable and restricted. -/ theorem restricted_field_reads : - instruction (.fieldAt 4 4 false) [.readOnlyReference (.address ⟨0, 0⟩)] memory = + loadValueField 4 4 false [.readOnlyReference (.address ⟨0, 0⟩)] memory = .ok (memory, [.scalar (.i32 0)]) ∧ - instruction (.fieldAddress 4) [.readOnlyReference (.address ⟨0, 0⟩)] memory = + structFieldAddress 4 [.readOnlyReference (.address ⟨0, 0⟩)] memory = .ok (memory, [.readOnlyReference (.address ⟨0, 4⟩)]) ∧ - instruction (.aggregateFieldAt 4 4) [.readOnlyReference (.address ⟨0, 0⟩)] memory = + loadAggregateField 4 4 [.readOnlyReference (.address ⟨0, 0⟩)] memory = .ok (memory, [.scalar (.aggregate [0, 0, 0, 0])]) := ⟨rfl, rfl, rfl⟩ #print axioms restricted_field_reads /-- Field stores through a restricted reference fail, naming the struct reference and field width, despite the backing allocation's write authority. -/ theorem restricted_field_stores_rejected : - instruction (.setFieldAt 4 4) [.scalar (.i32 7), .readOnlyReference (.address ⟨0, 0⟩)] + storeValueField 4 4 [.scalar (.i32 7), .readOnlyReference (.address ⟨0, 0⟩)] memory = .error (.memory .unwritable (.address ⟨0, 0⟩) 4) ∧ - instruction (.setAggregateFieldAt 4 4) + storeAggregateField 4 4 [.scalar (.aggregate [7, 0, 0, 0]), .readOnlyReference (.address ⟨0, 0⟩)] memory = .error (.memory .unwritable (.address ⟨0, 0⟩) 4) := ⟨rfl, rfl⟩ #print axioms restricted_field_stores_rejected /-- A readonly field access through null faults before any field offset is formed. -/ theorem restricted_null_field : - instruction (.fieldAt 4 4 false) [.readOnlyReference .null] memory = + loadValueField 4 4 false [.readOnlyReference .null] memory = .error (.memory .nullDereference .null 0) ∧ - instruction (.fieldAddress 4) [.readOnlyReference .null] memory = + structFieldAddress 4 [.readOnlyReference .null] memory = .error (.memory .nullDereference .null 0) := ⟨rfl, rfl⟩ #print axioms restricted_null_field @@ -115,23 +116,195 @@ private def features : CIL.Extraction.FeatureCertificate := private def states : CIL.Extraction.ScalarCertificate := [some [], some [.array .signed32], some [.word32, .array .signed32], some [.word32, .array .signed32], some [.reference], none] -private def code (writable : Bool) : List CIL.Op := - [.arg 0, .arg 1, .nop, .memory (.arrayAddress .signed32 writable), .ret, - .unsupported "unreachable"] +private def code (readOnly : Bool) : List CIL.Instruction := + [.ldarg 0, .ldarg 1, .nop, .ldelema intType readOnly, .ret, .unadmitted 43] /-- A branch to the prefix itself does not bypass the restriction. -/ -theorem prefix_certificate : CIL.Extraction.ScalarFlowValid #[] (raw 2) features states (code false) := - by decide +kernel +theorem prefix_certificate : + Tests.DecodedFlow (raw 2) features states (code true) := by decide +kernel #print axioms prefix_certificate -/-- Independently forging a writable address under the same readonly raw CIL fails. -/ +/-- Independently forging a writable address under the same readonly raw CIL fails the code + equation. -/ theorem writable_forgery_rejected : - ¬ CIL.Extraction.ScalarFlowValid #[] (raw 2) features states (code true) := by decide +kernel + ¬ Tests.DecodedFlow (raw 2) features states (code false) := by decide +kernel #print axioms writable_forgery_rejected -/-- Changing only the branch target to skip the prefix is rejected, even for a dead branch. -/ +/-- Changing only the branch target to skip the prefix is rejected, even for a dead branch: + the scalar check leaves branch entry to body decoding, whose `PrefixEntryFree` refuses it. -/ theorem branch_into_address_rejected : - ¬ CIL.Extraction.ScalarFlowValid #[] (raw 3) features states (code false) := by decide +kernel + CIL.Extraction.ScalarFlowValid (raw 3) features states (code true) ∧ + ¬ CIL.Extraction.PrefixEntryFree (raw 3).code := by decide +kernel #print axioms branch_into_address_rejected +/-- The accepted body with only its prefix given an operand. -/ +private def operandRaw : CIL.Extraction.RawMethod := + { raw 2 with code := (raw 2).code.set 2 ⟨2, 65054, .integer 0⟩ } + +/-- A prefix that carries an operand has no decoding, and the certificate is rejected. -/ +theorem prefix_operand_rejected : + ¬ CIL.Extraction.ScalarFlowValid operandRaw features states (code true) := by + decide +kernel +#print axioms prefix_operand_rejected + +/-! ## The decoded readonly rule + +Each body below is an element address body the scalar check accepts, changed in one place; every +refusal comes from decoding, the rule that only `ldelema` follows `readonly.`, and successor +reachability. -/ + +section DecodedReadOnly +open CIL.Extraction + +/-- A body of `ldarg.0; ldarg.1` then `rest`, with the signature of `raw`. -/ +private def body (rest : List RawInstruction) : RawMethod := + { raw 2 with code := ⟨0, 2, .none⟩ :: ⟨1, 3, .none⟩ :: rest } + +/-- Feature states at the stack depths `depths`, one pc each. -/ +private def depthsOf (depths : List Nat) : FeatureCertificate := + depths.map fun depth => some ⟨List.replicate depth none, []⟩ + +/-- The scalar stacks of an address body: the array and index pushed, held through `held` pcs, + then the reference the address leaves, returned. -/ +private def addressStates (held : Nat) : ScalarCertificate := + [some [], some [.array .signed32]] ++ List.replicate held (some [.word32, .array .signed32]) ++ + [some [.reference]] + +/-- The `readonly.` prefix, `ldelema int32` and `ret` at the offsets they hold after two + one-byte loads. -/ +private def prefixRaw : RawInstruction := ⟨2, 65054, .none⟩ +private def addressRaw (offset : Nat) : RawInstruction := ⟨offset, 143, .type intType⟩ +private def retRaw (offset : Nat) : RawInstruction := ⟨offset, 42, .none⟩ + +/-- The raw follower must be a typed ldelema even when the reachability mask excludes it. -/ +theorem readonly_requires_element_address : + let decode := fun follower => + (decodeRaw .scalar {} [] [true, true, true, false] + (body [prefixRaw, follower]) []).map CIL.Method.code + decode (addressRaw 4) = .ok [.ldarg 0, .ldarg 1, .nop, .unadmitted 143] ∧ + decode ⟨4, 0, .none⟩ = .error (.method .invalidPrefixes) := by + exact ⟨rfl, rfl⟩ +#print axioms readonly_requires_element_address + +/-- Whether the scalar check and the code equation accept a body with no layouts. -/ +private def accepted (body : RawMethod) (features : FeatureCertificate) + (states : ScalarCertificate) (code : List CIL.Instruction) : Bool := + decide (Tests.DecodedFlow body features states code) + +/-- A readonly and a writable element address, each emitted as it decodes, pass the check. -/ +theorem decoded_addresses_accepted : + accepted (body [prefixRaw, addressRaw 4, retRaw 9]) (depthsOf [0, 1, 2, 2, 1]) + (addressStates 2) [.ldarg 0, .ldarg 1, .nop, .ldelema intType true, .ret] ∧ + accepted (body [addressRaw 2, retRaw 7]) (depthsOf [0, 1, 2, 1]) (addressStates 1) + [.ldarg 0, .ldarg 1, .ldelema intType false, .ret] := by + decide +kernel +#print axioms decoded_addresses_accepted + +/-- An address whose flag disagrees with the raw prefix, or whose element differs from its + operand, is not the decoding of its raw instruction. -/ +theorem forged_addresses_refused : + !accepted (body [prefixRaw, addressRaw 4, retRaw 9]) (depthsOf [0, 1, 2, 2, 1]) + (addressStates 2) [.ldarg 0, .ldarg 1, .nop, .ldelema intType false, .ret] ∧ + !accepted (body [addressRaw 2, retRaw 7]) (depthsOf [0, 1, 2, 1]) (addressStates 1) + [.ldarg 0, .ldarg 1, .ldelema intType true, .ret] ∧ + !accepted (body [prefixRaw, addressRaw 4, retRaw 9]) (depthsOf [0, 1, 2, 2, 1]) + (addressStates 2) + [.ldarg 0, .ldarg 1, .nop, .ldelema (.atom runtimeScope "System.UInt32") true, + .ret] := by + decide +kernel +#print axioms forged_addresses_refused + +/-- A reachable `readonly.` that ends the body has no `ldelema` after it to take it. -/ +theorem final_prefix_refused : + !readOnlyPrefixMatches none prefixRaw && + !accepted (body [prefixRaw]) (depthsOf [0, 1, 2]) + [some [], some [.array .signed32], some [.word32, .array .signed32]] + [.ldarg 0, .ldarg 1, .nop] := by + decide +kernel +#print axioms final_prefix_refused + +/-- A second `readonly.`, a `nop` or an `ldelem` after `readonly.` does not take it, so the + prefix site refuses it. -/ +theorem wrong_followers_refused : + [⟨4, 65054, .none⟩, ⟨4, 0, .none⟩, ⟨4, 148, .none⟩].all + (fun following => !readOnlyPrefixMatches (some following) prefixRaw) ∧ + !accepted (body [prefixRaw, ⟨4, 65054, .none⟩, addressRaw 6, retRaw 11]) + (depthsOf [0, 1, 2, 2, 2, 1]) (addressStates 3) + [.ldarg 0, .ldarg 1, .nop, .nop, .ldelema intType true, .ret] ∧ + !accepted (body [prefixRaw, ⟨4, 0, .none⟩, addressRaw 5, retRaw 10]) + (depthsOf [0, 1, 2, 2, 2, 1]) (addressStates 3) + [.ldarg 0, .ldarg 1, .nop, .nop, .ldelema intType false, .ret] ∧ + !accepted (body [prefixRaw, ⟨4, 148, .none⟩, retRaw 5]) (depthsOf [0, 1, 2, 2, 1]) + [some [], some [.array .signed32], some [.word32, .array .signed32], + some [.word32, .array .signed32], some [.word32]] + [.ldarg 0, .ldarg 1, .nop, .ldelem .i4, .ret] := by + decide +kernel +#print axioms wrong_followers_refused + +/-- A body whose `readonly.` is followed by `follower`, then `ldelema int32` and `ret`. -/ +private def followedBody (follower : RawInstruction) : RawMethod := + body [prefixRaw, follower, addressRaw 5, retRaw 10] + +/-- The code a forger writes for `followedBody`: the prefix and its `nop` follower as `nop`. -/ +private def followedCode : List CIL.Instruction := + [.ldarg 0, .ldarg 1, .nop, .nop, .ldelema intType false, .ret] + +/-- With the prefix emitted as `nop`, which the scalar check does not compare at an ordinary + site, the scalar check still refuses a `readonly.` that ends the body, whose fall-through + successor does not exist, and one that no `ldelema` follows, at the follower through + `invalidPredecessor`; `readOnlyPrefixMatches` refuses both as well. The decoder gives such a + prefix no instruction, so the code equation refuses the same code. -/ +theorem misplaced_prefix_refused : + ¬ ScalarFlowValid (body [prefixRaw]) (depthsOf [0, 1, 2]) + [some [], some [.array .signed32], some [.word32, .array .signed32]] + [.ldarg 0, .ldarg 1, .nop] ∧ + ¬ ScalarFlowValid (followedBody ⟨4, 0, .none⟩) (depthsOf [0, 1, 2, 2, 2, 1]) (addressStates 3) + followedCode ∧ + methodCode .scalar {} [] (List.replicate 4 true) (followedBody ⟨4, 0, .none⟩) ≠ + followedCode := by + decide +kernel +#print axioms misplaced_prefix_refused + +/-- `ldelema` takes no prefix but `readonly.`: after `unaligned.` it has no decoding. -/ +theorem unaligned_address_refused : + !accepted (body [⟨2, 65042, .integer 1⟩, addressRaw 5, retRaw 10]) (depthsOf [0, 1, 2, 2, 1]) + (addressStates 2) [.ldarg 0, .ldarg 1, .nop, .ldelema intType false, .ret] := by + decide +kernel +#print axioms unaligned_address_refused + +/-- A static call of `Fixture.Sink::Take(int32)`. -/ +private def takeSite : CallSite := { + declared := { convention := 0, parameters := [intType] }, + method := { + owner := .object "Fixture" "Fixture.Sink", name := name!"Take", + hasThis := false, genericArity := 0, returnType := .void, parameters := [intType] }, + name := "Take", target := .managed 0x06000001, typeArguments := [], parameterCount := 1, + returnsValue := false } + +/-- A call decodes alone but not after `readonly.`, and the `readonly.` before it is refused at + the prefix site. -/ +theorem prefixed_call_refused : + (valueEncodingMatches none ⟨4, 40, .call takeSite⟩ (.call takeSite.method) && + (CIL.Instruction.ofRaw? (some prefixRaw) ⟨4, 40, .call takeSite⟩).isNone && + !readOnlyPrefixMatches (some ⟨4, 40, .call takeSite⟩) prefixRaw) = true := by + decide +kernel +#print axioms prefixed_call_refused + +/-- A void body whose first instruction branches over a dead `readonly.` to the `ret` after it. -/ +private def deadPrefixRaw : RawMethod := + { raw 2 with + returnType := .void + code := [⟨0, 43, .target 2⟩, ⟨2, 65054, .none⟩, ⟨4, 42, .none⟩] } + +/-- Scalar validation refuses the return after a dead readonly modifier. Branch entry also + fails because the branch targets the modifier's follower. -/ +theorem dead_prefix_follower_refused : + (¬ ScalarFlowValid deadPrefixRaw [some ⟨[], []⟩, none, some ⟨[], []⟩] + [some [], none, some []] [.br 2, .unadmitted 65054, .ret]) ∧ + ¬ PrefixEntryFree deadPrefixRaw.code := by + decide +kernel +#print axioms dead_prefix_follower_refused + +end DecodedReadOnly + end Tests.ReadOnlyArrayReferences diff --git a/Tests/ReceiverLoopOutlines.cs b/Tests/ReceiverLoopOutlines.cs index 6053f4d58..b5c76b6d9 100644 --- a/Tests/ReceiverLoopOutlines.cs +++ b/Tests/ReceiverLoopOutlines.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.RegularExpressions; using Mono.Cecil; diff --git a/Tests/ReferenceAddOutlines.cs b/Tests/ReferenceAddOutlines.cs new file mode 100644 index 000000000..b7dce1f94 --- /dev/null +++ b/Tests/ReferenceAddOutlines.cs @@ -0,0 +1,114 @@ +using System.Text.RegularExpressions; +using Mono.Cecil; +using Mono.Cecil.Cil; + +partial class Program +{ + /// Checks outline recognition without elaborating author-supplied proof obligations. + static void CheckReferenceAddOutlines(string root) + { + foreach (var variant in new[] { "Callee", "Direct", "Commuted", "Native", "Unsafe32", "Unsafe64" }) + { + var directory = Path.Combine(root, "Outlines", variant); + Directory.CreateDirectory(directory); + var assembly = Path.Combine(directory, "Examples.dll"); + using (var module = ModuleDefinition.ReadModule(At("Examples/Assembly/bin/Examples.dll"))) + { + AddReferenceOutlineLoop(module, variant); + module.Write(assembly); + } + var extraction = Path.Combine(directory, "Extracted"); + var word = variant == "Unsafe64" ? "System.UInt64" : "System.UInt32"; + Run(["dotnet", ExtractorPath, "extract", assembly, + $"Loops::ReferenceOutline({word}&,{word},System.Int32)", extraction]); + var proof = Path.Combine(directory, "Proof.lean"); + File.Delete(proof); + Run(["dotnet", ExtractorPath, "new-proof", extraction, proof]); + var source = File.ReadAllText(proof); + if (variant is "Commuted" or "Native") + RequireOutlineText(source, + "a reference addition other than a reference argument followed by a 32-bit index " + + "before a call in the iteration", variant); + else if (variant == "Direct") + RequireOutlineText(source, + "a store through `Unsafe.Add` of a value the outline cannot name at the element's width", variant); + else + { + RequireOutlineText(source, "import CIL.Safety.LoopObligations", variant); + RequireOutlineText(source, "add_int_element", variant); + var stride = variant == "Callee" ? 1 : variant == "Unsafe32" ? 4 : 8; + RequireOutlineText(source, $"destination.element {stride} ", variant); + if (source.Contains("-- not outlined:", StringComparison.Ordinal)) + throw new InvalidOperationException($"{variant} outline stopped:\n{source}"); + } + Console.WriteLine($"PASS reference-add outline recognition: {variant} (text only)"); + } + } + + /// Builds a counter loop with reference addition followed by a word store. + static void AddReferenceOutlineLoop(ModuleDefinition module, string variant) + { + var type = module.GetType("Examples.Loops"); + var word = variant == "Unsafe64" ? module.TypeSystem.UInt64 : module.TypeSystem.UInt32; + var attributes = Mono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.Static; + var store = new MethodDefinition("StoreWord", attributes, module.TypeSystem.Void); + store.Parameters.Add(new ParameterDefinition("slot", ParameterAttributes.None, new ByReferenceType(word))); + store.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, word)); + type.Methods.Add(store); + var storeBody = store.Body.GetILProcessor(); + storeBody.Emit(OpCodes.Ldarg_0); + storeBody.Emit(OpCodes.Ldarg_1); + storeBody.Emit(variant == "Unsafe64" ? OpCodes.Stind_I8 : OpCodes.Stind_I4); + storeBody.Emit(OpCodes.Ret); + var method = new MethodDefinition("ReferenceOutline", attributes, module.TypeSystem.Void); + method.Parameters.Add(new ParameterDefinition("destination", ParameterAttributes.None, + new ByReferenceType(word))); + method.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, word)); + method.Parameters.Add(new ParameterDefinition("count", ParameterAttributes.None, module.TypeSystem.Int32)); + type.Methods.Add(method); + method.Body.InitLocals = true; + method.Body.Variables.Add(new VariableDefinition(module.TypeSystem.Int32)); + var body = method.Body.GetILProcessor(); + var start = Instruction.Create(variant == "Commuted" ? OpCodes.Ldloc_0 : OpCodes.Ldarg_0); + var condition = Instruction.Create(OpCodes.Ldloc_0); + body.Emit(OpCodes.Ldc_I4_0); + body.Emit(OpCodes.Stloc_0); + body.Emit(OpCodes.Br, condition); + body.Append(start); + body.Emit(variant == "Commuted" ? OpCodes.Ldarg_0 : OpCodes.Ldloc_0); + if (variant == "Native") body.Emit(OpCodes.Conv_I); + if (variant.StartsWith("Unsafe", StringComparison.Ordinal)) + { + var original = (GenericInstanceMethod)type.Methods.Single(method => method.Name == "FillWords") + .Body.Instructions.Single(instruction => instruction.OpCode == OpCodes.Call).Operand; + var add = new GenericInstanceMethod(original.ElementMethod); + add.GenericArguments.Add(word); + body.Emit(OpCodes.Call, add); + } + else body.Emit(OpCodes.Add); + body.Emit(OpCodes.Ldarg_1); + if (variant == "Direct") body.Emit(OpCodes.Stind_I4); + else body.Emit(OpCodes.Call, store); + body.Emit(OpCodes.Ldloc_0); + body.Emit(OpCodes.Ldc_I4_1); + body.Emit(OpCodes.Add); + body.Emit(OpCodes.Stloc_0); + body.Append(condition); + body.Emit(OpCodes.Ldarg_2); + body.Emit(OpCodes.Blt, start); + body.Emit(OpCodes.Ret); + } + + /// Requires a specific outline term or unsupported-shape diagnosis. + static void RequireOutlineText(string source, string expected, string variant) + { + var unwrapped = WrappedComment.Replace(source, " "); + if (!unwrapped.Contains(expected, StringComparison.Ordinal)) + throw new InvalidOperationException($"{variant} outline lacks '{expected}':\n{source}"); + } + + // A line break and the comment marker that continues a wrapped outline comment. + [GeneratedRegex(@"\r?\n\s*--\s*")] + private static partial Regex WrappedComment { get; } +} + diff --git a/Tests/ReferenceArrays.lean b/Tests/ReferenceArrays.lean index a9a201fba..d94523500 100644 --- a/Tests/ReferenceArrays.lean +++ b/Tests/ReferenceArrays.lean @@ -1,4 +1,6 @@ import CIL.Safety.ObjectSteps +import CIL.Safety.SymbolicExecution +import CIL.Safety.HeapObjectFacts /-! `newarr` of a class allocates an object of the array type `E[]` on the heap budget, with one null reference slot per element. Its length is decoded like a primitive array's, and an @@ -15,7 +17,7 @@ private def budgeted : CIL.Safety.Memory := { Memory.empty with heapBudget := 32 private def creator : CIL.Method := { returnsValue := true, locals := [], - code := [.const32 3, .newReferenceArray node, .ret none] } + code := [.ldc_i4 3, .newarr node, .ret] } /-- Three references take the next identity, three slots of budget, and every slot is null. -/ theorem allocation_charges_budget : @@ -32,7 +34,7 @@ theorem allocation_returns_array : /-- A negative length throws `OverflowException`; an unaffordable one is a model fault. -/ theorem allocation_faults : - newReferenceArray budgeted node (.i32 (-1)) = .error (.thrown "System.OverflowException") ∧ + newReferenceArray budgeted node (.i32 (-1)) = .error (.thrown .overflow) ∧ newReferenceArray budgeted node (.i64 5) = .error (.allocationFailure 40 32) ∧ newReferenceArray budgeted node (.i64 0x80000000) = .error (.unsupported "Array length outside supported extent") := ⟨rfl, rfl, rfl⟩ @@ -55,67 +57,67 @@ private def holdingSquare : CIL.Safety.Memory := objectMemory budgeted ⟨square private def squareObject : CIL.Safety.Value := .object (.address ⟨0, 0⟩) /-- Runs straight-line `code` returning one value, with the given arguments. -/ -private def runs (code : List CIL.Op) (arguments : List CIL.Safety.Value) +private def runs (code : List CIL.Instruction) (arguments : List CIL.Safety.Value) (memory : CIL.Safety.Memory) : Except LocatedFault (CIL.Safety.Memory × List CIL.Safety.Value) := invoke [{ returnsValue := true, locals := [], code := code }] 16 0 arguments memory /-- `ldlen` reads the length back from the allocation's size. -/ theorem length_of_fresh : - (runs [.const32 3, .newReferenceArray node, .memory .referenceArrayLength, .ret none] [] + (runs [.ldc_i4 3, .newarr node, .ldlen, .ret] [] budgeted).map Prod.snd = .ok [.scalar (.i64 3)] := rfl #print axioms length_of_fresh /-- `ldlen`, `ldelem.ref` and `stelem.ref` of a null array throw `NullReferenceException`. -/ theorem null_array_throws : - runs [.memory .nullObject, .memory .referenceArrayLength, .ret none] [] budgeted = - .error ⟨0, 1, .thrown "System.NullReferenceException"⟩ ∧ - runs [.memory .nullObject, .const32 0, .memory .referenceArrayLoad, .ret none] [] budgeted = - .error ⟨0, 2, .thrown "System.NullReferenceException"⟩ ∧ - runs [.memory .nullObject, .const32 0, .memory .nullObject, .memory .referenceArrayStore, - .const32 0, .ret none] [] budgeted = - .error ⟨0, 3, .thrown "System.NullReferenceException"⟩ := ⟨rfl, rfl, rfl⟩ + runs [.ldnull, .ldlen, .ret] [] budgeted = + .error ⟨0, 1, .thrown .nullReference⟩ ∧ + runs [.ldnull, .ldc_i4 0, .ldelem_ref, .ret] [] budgeted = + .error ⟨0, 2, .thrown .nullReference⟩ ∧ + runs [.ldnull, .ldc_i4 0, .ldnull, .stelem_ref, + .ldc_i4 0, .ret] [] budgeted = + .error ⟨0, 3, .thrown .nullReference⟩ := ⟨rfl, rfl, rfl⟩ #print axioms null_array_throws /-- An index at the length, or a negative one read unsigned, throws `IndexOutOfRangeException` before a stored value's class is checked. -/ theorem index_out_of_range : - runs [.const32 2, .newReferenceArray node, .const32 2, .memory .referenceArrayLoad, - .ret none] [] budgeted = .error ⟨0, 3, .thrown "System.IndexOutOfRangeException"⟩ ∧ - runs [.const32 2, .newReferenceArray circle, .const32 (-1), .arg 0 none, - .memory .referenceArrayStore, .const32 0, .ret none] [squareObject] holdingSquare = - .error ⟨0, 4, .thrown "System.IndexOutOfRangeException"⟩ := ⟨rfl, rfl⟩ + runs [.ldc_i4 2, .newarr node, .ldc_i4 2, .ldelem_ref, + .ret] [] budgeted = .error ⟨0, 3, .thrown .indexOutOfRange⟩ ∧ + runs [.ldc_i4 2, .newarr circle, .ldc_i4 (-1), .ldarg 0, + .stelem_ref, .ldc_i4 0, .ret] [squareObject] holdingSquare = + .error ⟨0, 4, .thrown .indexOutOfRange⟩ := ⟨rfl, rfl⟩ #print axioms index_out_of_range /-- A fresh element is null, and null is storable into any array of class references. -/ theorem null_elements : - (runs [.const32 1, .newReferenceArray node, .const32 0, .memory .referenceArrayLoad, - .ret none] [] budgeted).map Prod.snd = .ok [.object .null] ∧ - (runs [.const32 1, .newReferenceArray circle, .dup, .const32 0, .memory .nullObject, - .memory .referenceArrayStore, .const32 0, .memory .referenceArrayLoad, .ret none] [] + (runs [.ldc_i4 1, .newarr node, .ldc_i4 0, .ldelem_ref, + .ret] [] budgeted).map Prod.snd = .ok [.object .null] ∧ + (runs [.ldc_i4 1, .newarr circle, .dup, .ldc_i4 0, .ldnull, + .stelem_ref, .ldc_i4 0, .ldelem_ref, .ret] [] budgeted).map Prod.snd = .ok [.object .null] := ⟨rfl, rfl⟩ #print axioms null_elements /-- A `Square` is storable into an array of `Shape` and loads back as itself. -/ theorem covariant_store : - (runs [.const32 1, .newReferenceArray shape, .dup, .const32 0, .arg 0 none, - .memory .referenceArrayStore, .const32 0, .memory .referenceArrayLoad, .ret none] + (runs [.ldc_i4 1, .newarr shape, .dup, .ldc_i4 0, .ldarg 0, + .stelem_ref, .ldc_i4 0, .ldelem_ref, .ret] [squareObject] holdingSquare).map Prod.snd = .ok [squareObject] := rfl #print axioms covariant_store /-- Storing a `Square` into an array of `Circle` throws `ArrayTypeMismatchException`, and the element keeps what it held. -/ theorem mismatched_store : - runs [.const32 1, .newReferenceArray circle, .const32 0, .arg 0 none, - .memory .referenceArrayStore, .const32 0, .ret none] [squareObject] holdingSquare = - .error ⟨0, 4, .thrown "System.ArrayTypeMismatchException"⟩ := rfl + runs [.ldc_i4 1, .newarr circle, .ldc_i4 0, .ldarg 0, + .stelem_ref, .ldc_i4 0, .ret] [squareObject] holdingSquare = + .error ⟨0, 4, .thrown .arrayTypeMismatch⟩ := rfl #print axioms mismatched_store /-- Storing an array, whose class is no admitted class, is outside the model rather than an exception the CLR might not throw. -/ theorem array_store_unsupported : - runs [.const32 1, .newReferenceArray shape, .dup, .const32 0, .const32 1, - .newReferenceArray shape, .memory .referenceArrayStore, .const32 0, .ret none] [] + runs [.ldc_i4 1, .newarr shape, .dup, .ldc_i4 0, .ldc_i4 1, + .newarr shape, .stelem_ref, .ldc_i4 0, .ret] [] budgeted = .error ⟨0, 6, .unsupported "Array store outside the admitted classes"⟩ := rfl #print axioms array_store_unsupported @@ -129,8 +131,8 @@ private def holdingText : CIL.Safety.Memory := /-- A string stores into an array of strings and loads back as itself: the element class is the string's own class. -/ theorem opaque_store : - (runs [.const32 1, .newReferenceArray text, .dup, .const32 0, .arg 0 none, - .memory .referenceArrayStore, .const32 0, .memory .referenceArrayLoad, .ret none] + (runs [.ldc_i4 1, .newarr text, .dup, .ldc_i4 0, .ldarg 0, + .stelem_ref, .ldc_i4 0, .ldelem_ref, .ret] [.object (.address ⟨0, 0⟩)] holdingText).map Prod.snd = .ok [.object (.address ⟨0, 0⟩)] := rfl #print axioms opaque_store @@ -138,12 +140,43 @@ theorem opaque_store : /-- A `Square` stored into an array of strings throws `ArrayTypeMismatchException`, and a string stored into an array of `Shape` does too: neither class derives from the other. -/ theorem opaque_mismatch : - runs [.const32 1, .newReferenceArray text, .const32 0, .arg 0 none, - .memory .referenceArrayStore, .const32 0, .ret none] [.object (.address ⟨1, 0⟩)] - holdingText = .error ⟨0, 4, .thrown "System.ArrayTypeMismatchException"⟩ ∧ - runs [.const32 1, .newReferenceArray shape, .const32 0, .arg 0 none, - .memory .referenceArrayStore, .const32 0, .ret none] [.object (.address ⟨0, 0⟩)] - holdingText = .error ⟨0, 4, .thrown "System.ArrayTypeMismatchException"⟩ := ⟨rfl, rfl⟩ + runs [.ldc_i4 1, .newarr text, .ldc_i4 0, .ldarg 0, + .stelem_ref, .ldc_i4 0, .ret] [.object (.address ⟨1, 0⟩)] + holdingText = .error ⟨0, 4, .thrown .arrayTypeMismatch⟩ ∧ + runs [.ldc_i4 1, .newarr shape, .ldc_i4 0, .ldarg 0, + .stelem_ref, .ldc_i4 0, .ret] [.object (.address ⟨0, 0⟩)] + holdingText = .error ⟨0, 4, .thrown .arrayTypeMismatch⟩ := ⟨rfl, rfl⟩ #print axioms opaque_mismatch +/-- `System.Object`, whose arrays may hold interface and boxed value instances. -/ +private def rootObject : CIL.Extraction.TypeExpr := + .object Extraction.runtimeScope ElementShape.rootObjectName + +/-- An array of `System.Object` has no shape, so `newarr` of it is outside the model, before + any allocation; a class of the same name in another assembly is still a class element. -/ +theorem root_object_refused : + runs [.ldc_i4 1, .newarr rootObject, .ret] [] budgeted = + .error ⟨0, 1, .unsupported ElementShape.rootObjectElement⟩ ∧ + (runs [.ldc_i4 1, .newarr (.object "Examples" "Examples.Object"), .ret] [] + budgeted).map Prod.snd = .ok [.object (.address ⟨0, 0⟩)] := ⟨rfl, rfl⟩ +#print axioms root_object_refused + +/-- `newarr` of two `Node` references. -/ +@[cil_code] def allocating : CIL.Method := + { returnsValue := true, locals := [], code := [.ldc_i4 2, .newarr node, .ret] } + +/-- The steps resolve the element shape of a class and allocate through + `newReferenceArray`, so its budget fact discharges the allocation and the fresh array + forms where it is returned. -/ +theorem stepped_allocation (memory : CIL.Safety.Memory) (formed : memory.WellFormed) + (budget : objectFieldSlot * 2 ≤ memory.heapBudget) : + ∃ fuel, invoke [allocating] fuel 0 [] memory = + .ok (objectMemory memory (referenceArrayType node) (objectFieldSlot * 2), + [.object (.address ⟨memory.nextIdentity, 0⟩)]) := by + have allocated := newReferenceArray_ok formed node (value := .i32 2) (length := 2) + (Or.inl rfl) (by decide) (by decide) budget + simp only [node] + cil_checked_steps [allocated, node, objectMemory_formObject] +#print axioms stepped_allocation + end Tests.ReferenceArrays diff --git a/Tests/ReferenceInstructions.cs b/Tests/ReferenceInstructions.cs index 97cea802e..48f373910 100644 --- a/Tests/ReferenceInstructions.cs +++ b/Tests/ReferenceInstructions.cs @@ -3,9 +3,15 @@ #:property AllowUnsafeBlocks=true #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs +#:include ReferenceAddOutlines.cs using System.Reflection; using Mono.Cecil; @@ -13,6 +19,8 @@ var root = At("artifacts/ReferenceInstructions"); Directory.CreateDirectory(root); +CheckReferenceAddOutlines(root); +if (args is ["--outlines-only"]) return; var evidence = new GeneratedEvidence(root); var rejected = new RejectedEvidence(root); var assembly = Path.Combine(root, "Examples.dll"); @@ -70,14 +78,15 @@ } il.Emit(OpCodes.Ret); } + // The commuted reference fixture also covers an addition in a method with a dead tail. foreach (var (name, native, pointer) in additions) AddOffset(name, native ? module.TypeSystem.IntPtr : module.TypeSystem.Int32, - pointer, OpCodes.Add); + pointer, OpCodes.Add, deadTail: name == "Offset32Reference"); AddOffset("InvalidInt64Offset", module.TypeSystem.Int64, true, OpCodes.Add); AddOffset("InvalidReferenceSum", new ByReferenceType(module.TypeSystem.Int64), false, OpCodes.Add); AddOffset("InvalidReversedSubtract", module.TypeSystem.Int32, true, OpCodes.Sub); - void AddOffset(string name, TypeReference offset, bool pointer, OpCode operation) + void AddOffset(string name, TypeReference offset, bool pointer, OpCode operation, bool deadTail = false) { var method = new MethodDefinition(name, Mono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Int32); @@ -91,6 +100,9 @@ void AddOffset(string name, TypeReference offset, bool pointer, OpCode operation body.Emit(operation); body.Emit(OpCodes.Ldind_U1); body.Emit(OpCodes.Ret); + if (!deadTail) return; + body.Emit(OpCodes.Ldc_I4_0); + body.Emit(OpCodes.Ret); } var throws = new MethodDefinition("NullMessageThrow", @@ -139,9 +151,9 @@ void AddOffset(string name, TypeReference offset, bool pointer, OpCode operation { var narrowed = NarrowEvidence(source, "method0_scalars_validated"); File.AppendAllText(proof, "\n" + CheckedEvidenceControl(source, narrowed, ["method0_scalars_validated"], 0)); - if (!narrowed.Contains(".eq", StringComparison.Ordinal)) throw new Exception("Missing equality mutation target"); + // The method is defined from its decoder, so the forgery changes the ceq (0xFE01) it reads to clt (0xFE04). var forged = Path.Combine(root, "ForgedEquality.lean"); - File.WriteAllText(forged, narrowed.Replace(".eq", ".lt", StringComparison.Ordinal)); + File.WriteAllText(forged, ForgeDecodedOriginal(narrowed, 0, "opcode := 65025,", "opcode := 65028,")); rejected.Add(forged, "PASS independent forged reference equality rejection", "ScalarFlowValid"); } evidence.Add(proof, "PASS runtime, certificate and reference execution: " + name); @@ -191,19 +203,37 @@ by decide +kernel """); if (name == "Offset32Reference") { - var narrowed = NarrowEvidence(source, "method0_scalars_validated"); - File.AppendAllText(proof, "\n" + CheckedEvidenceControl(source, narrowed, ["method0_scalars_validated"], 0)); - const string target = ".memory (.add 1 true)"; + File.AppendAllText(proof, """ + + namespace Extracted + /-- The scalar checker validates emitted instructions against the commuted operands. -/ + private abbrev scalarFits (code : List CIL.Instruction) := + CIL.Extraction.ScalarFlowValid method0Closed method0FeatureStates + method0ScalarStates code + + /-- The unchanged commuted addition satisfies the explicit scalar encoding. -/ + theorem original_add_valid : scalarFits method0.code := by decide +kernel + #print axioms original_add_valid + end Extracted + + """); foreach (var (mutation, replacement) in new[] { - ("scalar", ".add"), ("subtract", ".sub"), - ("stride", ".memory (.add 2 true)"), ("unsigned", ".memory (.add 1 false)") + ("signedOverflow", ".add_ovf"), ("subtract", ".sub"), + ("shift", ".shl"), ("unsignedOverflow", ".add_ovf_un") }) { - if (!narrowed.Contains(target, StringComparison.Ordinal)) throw new Exception("Missing commuted add target"); - var forged = Path.Combine(root, "ForgedAdd-" + mutation + ".lean"); - File.WriteAllText(forged, narrowed.Replace(target, replacement, StringComparison.Ordinal)); - rejected.Add(forged, "PASS independent forged commuted addition: " + mutation, "ScalarFlowValid"); + File.AppendAllText(proof, $$""" + + namespace Extracted + /-- The scalar operands exclude the substituted instruction. -/ + theorem forged_{{mutation}}_rejected : + Not (scalarFits (method0.code.map fun op => match op with + | .add => {{replacement}} | _ => op)) := by decide +kernel + #print axioms forged_{{mutation}}_rejected + end Extracted + + """); } } evidence.Add(proof, "PASS runtime, certificate and commuted reference addition: " + name); @@ -236,7 +266,7 @@ by decide +kernel theorem null_message_raises : CIL.Safety.invoke Extracted.program 16 0 [] CIL.Safety.Memory.empty = - .error ⟨0, 2, .thrown "System.DivideByZeroException"⟩ := rfl + .error ⟨0, 2, .thrown .divideByZero⟩ := rfl #print axioms null_message_raises """); evidence.Add(throwProof, "PASS existing null-message throw folding, runtime and certificate"); diff --git a/Tests/ReferenceTests.lean b/Tests/ReferenceTests.lean index 11e60537d..9699c9af5 100644 --- a/Tests/ReferenceTests.lean +++ b/Tests/ReferenceTests.lean @@ -68,7 +68,7 @@ theorem readonly_reference_observed : #print axioms readonly_reference_observed private def compare : CIL.Method := { - code := [.arg 0, .arg 1, .eq, .ret], locals := [], returnsValue := true } + code := [.ldarg 0, .ldarg 1, .ceq, .ret], locals := [], returnsValue := true } /-- Compiled equality observes readonly and writable views without changing memory. -/ theorem mixed_capability_equality : @@ -76,24 +76,24 @@ theorem mixed_capability_equality : .reference (.address ⟨0, 3⟩)] memory = .ok (memory, [.scalar (.i32 1)]) := rfl #print axioms mixed_capability_equality -private def branch (operation : CIL.Op) : CIL.Method := { - code := [.arg 0, operation, .const32 0, .ret, .const32 1, .ret] +private def branch (operation : CIL.Instruction) : CIL.Method := { + code := [.ldarg 0, operation, .ldc_i4 0, .ret, .ldc_i4 1, .ret] locals := [], returnsValue := true } /-- Null reference branches select the zero arm. -/ theorem null_branch : - invoke [branch (.brzero 4)] 8 0 [.reference .null] memory = + invoke [branch (.brfalse 4)] 8 0 [.reference .null] memory = .ok (memory, [.scalar (.i32 1)]) := rfl #print axioms null_branch /-- Non-null readonly references select the nonzero arm without byte access authority. -/ theorem readonly_branch : - invoke [branch (.brnonzero 4)] 8 0 [.readOnlyReference (.address ⟨0, 3⟩)] memory = + invoke [branch (.brtrue 4)] 8 0 [.readOnlyReference (.address ⟨0, 3⟩)] memory = .ok (memory, [.scalar (.i32 1)]) := rfl #print axioms readonly_branch -private def compareBranch (operation : CIL.Op) : CIL.Method := { - code := [.arg 0, .arg 1, operation, .const32 0, .ret, .const32 1, .ret] +private def compareBranch (operation : CIL.Instruction) : CIL.Method := { + code := [.ldarg 0, .ldarg 1, operation, .ldc_i4 0, .ret, .ldc_i4 1, .ret] locals := [], returnsValue := true } /-- Equality branches use the same checked comparison as ceq. -/ @@ -104,11 +104,11 @@ theorem reference_beq : #print axioms reference_beq /-- Inequality branches distinguish two positions within the same allocation. -/ -theorem reference_bne : - invoke [compareBranch (.bne 5)] 9 0 +theorem reference_bne_un : + invoke [compareBranch (.bne_un 5)] 9 0 [.reference (.address ⟨0, 3⟩), .reference (.address ⟨0, 4⟩)] memory = .ok (memory, [.scalar (.i32 1)]) := rfl -#print axioms reference_bne +#print axioms reference_bne_un /-- Existing numeric equality still follows scalar semantics. -/ theorem scalar_equality_unchanged : @@ -118,24 +118,24 @@ theorem scalar_equality_unchanged : /-- An object null cannot serve as a metadata-proven managed reference in a comparison. -/ theorem object_null_pointer_mix_rejected : - CIL.Extraction.scalarOpMatches ⟨0, 65025, .none⟩ [.reference, .nullObject] .eq = false := + CIL.Extraction.scalarOpMatches none ⟨0, 65025, .none⟩ [.reference, .nullObject] .ceq = false := by decide +kernel #print axioms object_null_pointer_mix_rejected /-- The equality certificate checks the operation, not just its stack effect. -/ theorem changed_reference_comparison_rejected : - CIL.Extraction.scalarOpMatches ⟨0, 65025, .none⟩ [.reference, .reference] .lt = false := + CIL.Extraction.scalarOpMatches none ⟨0, 65025, .none⟩ [.reference, .reference] .clt = false := by decide +kernel #print axioms changed_reference_comparison_rejected /-- The null literal cannot be replaced by a numeric zero with the same stack effect. -/ theorem numeric_null_forgery_rejected : - CIL.Extraction.scalarOpMatches ⟨0, 20, .none⟩ [] (.const32 0) = false := by decide +kernel + CIL.Extraction.scalarOpMatches none ⟨0, 20, .none⟩ [] (.ldc_i4 0) = false := by decide +kernel #print axioms numeric_null_forgery_rejected /-- The truth-branch certificate binds the original target. -/ theorem changed_reference_branch_rejected : - CIL.Extraction.scalarOpMatches ⟨0, 45, .target 4⟩ [.reference] (.brnonzero 3) = false := + CIL.Extraction.scalarOpMatches none ⟨0, 45, .target 4⟩ [.reference] (.brtrue 3) = false := by decide +kernel #print axioms changed_reference_branch_rejected @@ -200,7 +200,7 @@ private def writableMemory : Memory := { memory with /-- Offset then store a word; only the base reference's capability varies. -/ private def offsetStore : CIL.Method := { - code := [.arg 0, .const32 4, .memory (.add 1 true), .const32 7, .storeScalar 4, .ret] + code := [.ldarg 0, .ldc_i4 4, .add, .ldc_i4 7, .stind .i4, .ret] locals := [], returnsValue := false } /-- A store through an offset readonly reference faults, while the writable base stores. -/ @@ -215,27 +215,59 @@ theorem readonly_derived_store_faults : /-- Only int32/native offsets certify in either order; pointer sums and reversed subtraction fail. -/ theorem reference_arithmetic_kinds : - CIL.Extraction.scalarOpMatches ⟨0, 88, .none⟩ [.reference, .word32] - (.memory (.add 1 true)) = true ∧ - CIL.Extraction.scalarOpMatches ⟨0, 88, .none⟩ [.reference, .native] - (.memory (.add 1 true)) = true ∧ - CIL.Extraction.scalarOpMatches ⟨0, 88, .none⟩ [.reference, .word64] - (.memory (.add 1 true)) = false ∧ - CIL.Extraction.scalarOpMatches ⟨0, 88, .none⟩ [.reference, .reference] - (.memory (.add 1 true)) = false ∧ - CIL.Extraction.scalarOpMatches ⟨0, 89, .none⟩ [.reference, .word32] .sub = false := + CIL.Extraction.scalarOpMatches none ⟨0, 88, .none⟩ [.reference, .word32] + (.add) = true ∧ + CIL.Extraction.scalarOpMatches none ⟨0, 88, .none⟩ [.reference, .native] + (.add) = true ∧ + CIL.Extraction.scalarOpMatches none ⟨0, 88, .none⟩ [.reference, .word64] + (.add) = false ∧ + CIL.Extraction.scalarOpMatches none ⟨0, 88, .none⟩ [.reference, .reference] + (.add) = false ∧ + CIL.Extraction.scalarOpMatches none ⟨0, 89, .none⟩ [.reference, .word32] .sub = false := by decide +kernel #print axioms reference_arithmetic_kinds section SymbolicAddition +/-- Bare reference addition retains all checked byte-offset outcomes in either operand order. -/ +theorem reference_add_byte_offsets (body : CIL.Method) (pc : Nat) (args : List Value) + (frame : Frame) (memory : Memory) (reference : ManagedReference) (offset : CIL.Value) + (rest : List Value) : + step body .add pc args frame (.scalar offset :: .reference reference :: rest) memory = + modelStep (some (.memory (.add 1 true))) body pc frame + (.scalar offset :: .reference reference :: rest) memory ∧ + step body .add pc args frame (.reference reference :: .scalar offset :: rest) memory = + modelStep (some (.memory (.add 1 true))) body pc frame + (.reference reference :: .scalar offset :: rest) memory ∧ + step body .add pc args frame (.scalar offset :: .readOnlyReference reference :: rest) memory = + modelStep (some (.memory (.add 1 true))) body pc frame + (.scalar offset :: .readOnlyReference reference :: rest) memory ∧ + step body .add pc args frame (.readOnlyReference reference :: .scalar offset :: rest) memory = + modelStep (some (.memory (.add 1 true))) body pc frame + (.readOnlyReference reference :: .scalar offset :: rest) memory := by + cases unspecified : offset.hasUnspecified <;> + simp [modelStep_memory, step, staticInstruction, memoryInstruction, observerCount, + CIL.MemoryOp.argumentCount, unspecifiedOperands, Value.hasUnspecified, unspecified, + viewOperandCount, viewOperands] +#print axioms reference_add_byte_offsets + +/-- Reference addition rejects a substituted subtraction and a spurious encoded operand. -/ +theorem reference_add_encoding : + CIL.Extraction.scalarOpMatches none ⟨0, 88, .none⟩ [.word32, .reference] .add = true ∧ + CIL.Extraction.scalarOpMatches none ⟨0, 88, .none⟩ [.native, .reference] .add = true ∧ + CIL.Extraction.scalarOpMatches none ⟨0, 88, .none⟩ [.word32, .reference] + (.sub) = false ∧ + CIL.Extraction.scalarOpMatches none ⟨0, 88, .target 1⟩ [.word32, .reference] .add = false := + by decide +kernel +#print axioms reference_add_encoding + attribute [local irreducible] step /-- Registered stepping equations handle symbolic commuted int32 offsets without unfolding step. -/ theorem commuted_i32_step (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (memory : Memory) (reference : ManagedReference) (offset : CIL.W32) (rest : List Value) : - step body (.memory (.add 1 true)) pc args frame + step body (.add) pc args frame (.reference reference :: .scalar (.i32 offset) :: rest) memory = do let address ← referenceAt reference let result ← checkedAt reference 0 (add memory address 1 (BitVec.ofInt 64 offset.toInt)) @@ -248,7 +280,7 @@ theorem commuted_i32_step (body : CIL.Method) (pc : Nat) (args : List Value) theorem commuted_native_step (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) (memory : Memory) (reference : ManagedReference) (offset : CIL.W64) (rest : List Value) : - step body (.memory (.add 1 true)) pc args frame + step body (.add) pc args frame (.reference reference :: .scalar (.i64 offset) :: rest) memory = do let address ← referenceAt reference let result ← checkedAt reference 0 (add memory address 1 (BitVec.ofInt 64 offset.toInt)) @@ -258,10 +290,10 @@ theorem commuted_native_step (body : CIL.Method) (pc : Nat) (args : List Value) #print axioms commuted_native_step /-- Registered equations step a commuted readonly offset to a readonly result symbolically. -/ -theorem commuted_readonly_step (body : CIL.Method) (pc : Nat) (args : List Value) +theorem commuted_readonly_step (body : CIL.Method) (pc : Nat) (frame : Frame) (memory : Memory) (reference : ManagedReference) (offset : CIL.W32) (rest : List Value) : - step body (.memory (.add 8 true)) pc args frame + modelStep (some (.memory (.add 8 true))) body pc frame (.readOnlyReference reference :: .scalar (.i32 offset) :: rest) memory = do let address ← referenceAt reference let result ← checkedAt reference 0 (add memory address 8 (BitVec.ofInt 64 offset.toInt)) diff --git a/Tests/RegionRun.lean b/Tests/RegionRun.lean index 1547976fd..2c77890d8 100644 --- a/Tests/RegionRun.lean +++ b/Tests/RegionRun.lean @@ -1,23 +1,29 @@ import CIL.Safety.RegionRun import CIL.Safety.Embedding.Simulation import CIL.Safety.Embedding.Suffix +import Tests.Support /-! A suffix run moves to a body whose prefix is a call instead of inline code. -/ namespace Tests.RegionRun -open CIL (Method Op Embeds) +open CIL (Method Instruction Embeds) open CIL.Safety -def helper : Method := { code := [.arg 0, .const32 6, .shr, .ret], locals := [], returnsValue := true } +def helper : Method := + { code := [.ldarg 0, .ldc_i4 6, .shr, .ret], locals := [], returnsValue := true } /-- Computes `count >> 6` inline, then adds one. -/ -def expected : Method := { code := [.arg 0, .const32 6, .shr, .const32 1, .add, .ret], locals := [], returnsValue := true } +def expected : Method := + { code := [.ldarg 0, .ldc_i4 6, .shr, .ldc_i4 1, .add, .ret], locals := [], + returnsValue := true } /-- The same suffix after a compiled helper call, one pc earlier. -/ -def actual : Method := { code := [.arg 0, .call 1 1, .const32 1, .add, .ret], locals := [], returnsValue := true } +def actual : Method := + { code := [.ldarg 0, .call (Tests.callee 1) {}, .ldc_i4 1, .add, .ret], + links := [(1, .direct 1)], locals := [], returnsValue := true } def masked : Method := - { expected with code := List.replicate 3 (.unsupported "Outside the region") ++ expected.code.drop 3 } + { expected with code := List.replicate 3 (.unadmitted 0) ++ expected.code.drop 3 } abbrev suffix : Nat → Nat → Prop := fun method pc => inStarts [(0, 3)] method pc = true @@ -39,8 +45,12 @@ theorem suffix_moves (fuel : Nat) (args stack : List Value) (frame : Frame) (m : fuel 0 3 args frame stack m result ran /-- A region may call into a whole method listed from pc 0. -/ -def caller : Method := { code := [.const32 5, .ret, .arg 0, .call 1 1, .ret], locals := [], returnsValue := true } -def rewritten : Method := { caller with code := [.unsupported "Outside the region", .throw "System.Exception", .arg 0, .call 1 1, .ret] } +def caller : Method := + { code := [.ldc_i4 5, .ret, .ldarg 0, .call (Tests.callee 1) {}, .ret], + links := [(3, .direct 1)], locals := [], returnsValue := true } +def rewritten : Method := + { caller with code := [.unadmitted 0, .throw .baseException, + .ldarg 0, .call (Tests.callee 1) {}, .ret], links := [(3, .direct 1)] } abbrev calling : Nat → Nat → Prop := fun method pc => inStarts [(0, 2), (1, 0)] method pc = true @@ -51,29 +61,40 @@ theorem calling_run (fuel : Nat) (args stack : List Value) (frame : Frame) (m : code_agrees_of_drop (lower := 2) rfl (by simpa [calling, inStarts] using inside)) _ _ _ _ _ _ _ (by decide) /-- A branch back into the prefix leaves the region. -/ -example : regionClosedCheck [{ caller with code := [.const32 0, .ret, .branch 0] }] [(0, 2)] = false := by decide +example : regionClosedCheck [caller.withCode [.ldc_i4 0, .ret, .br 0]] [(0, 2)] = false := by + decide /-- A call into a method the region does not cover from pc 0 leaves it. -/ example : regionClosedCheck [caller, helper] [(0, 2)] = false := by decide /-- A whole method of straight-line code is closed. -/ -example : regionClosedCheck [{ caller with code := [.const32 0, .const32 1, .ret] }] [(0, 0)] = true := by decide +example : regionClosedCheck [caller.withCode [.ldc_i4 0, .ldc_i4 1, .ret]] [(0, 0)] = true := by + decide -def longSuffix : List Op := - List.replicate 96 .nop ++ [.brzero 102, .switch [3, 102], .nativeBinary (.bge 102), .ret] +def longSuffix : List Instruction := + List.replicate 96 .nop ++ [.brfalse 102, .switch [3, 102], .bge 102, .ret] def longExpected (profile : CIL.FeatureProfile) : Method := - { code := [.arg 0, .const32 6, .shr] ++ longSuffix, locals := [], returnsValue := true, profile := profile } + { code := [.ldarg 0, .ldc_i4 6, .shr] ++ longSuffix, locals := [], returnsValue := true, + profile := profile } def longMasked (profile : CIL.FeatureProfile) : Method := - { longExpected profile with code := List.replicate 3 (.unsupported "Outside the region") ++ longSuffix } + (longExpected profile).withCode + (List.replicate 3 (.unadmitted 0) ++ longSuffix) def longActual (profile : CIL.FeatureProfile) (callee : Nat) : Method := - { longExpected profile with - code := [.arg 0, .call callee 1] ++ longSuffix.map (Op.relabel (fun pc => pc - 3 + 2)) } + { (longExpected profile).withCode ([.ldarg 0, .call (Tests.callee 1)] ++ longSuffix.map + (Instruction.relabel (fun pc => pc - 3 + 2))) with + links := [(1, .direct callee)] } theorem longSuffix_length : longSuffix.length = 100 := by simp [longSuffix] +/-- The expected body calls nothing, so its link table is empty. -/ +theorem longExpected_links (profile : CIL.FeatureProfile) : + (longExpected profile).links = [] := rfl + +#print axioms longExpected_links + theorem longSuffix_refines (profile : CIL.FeatureProfile) (callee : Nat) : CIL.Embedding.BodyRefines (longMasked profile) (longActual profile callee) (CIL.Embedding.suffixRename 3 2) := by @@ -82,22 +103,61 @@ theorem longSuffix_refines (profile : CIL.FeatureProfile) (callee : Nat) : · rfl · omega · intro pc op before found - have lookup : (longMasked profile).code[pc]? = some (.unsupported "Outside the region") := by - exact (List.getElem?_append_left (l₁ := List.replicate 3 (.unsupported "Outside the region")) + have lookup : (longMasked profile).code[pc]? = some (.unadmitted 0) := by + exact (List.getElem?_append_left (l₁ := List.replicate 3 (.unadmitted 0)) (l₂ := longSuffix) (by simpa only [List.length_replicate] using before)).trans (List.getElem?_replicate_of_lt before) rw [lookup] at found cases found rfl - · simp [longActual, longMasked] - · simp only [longMasked, List.drop_append, List.length_replicate, Nat.sub_self, List.drop_zero, - List.drop_replicate, List.replicate_zero, List.nil_append] + · simp [longActual, longMasked, Method.withCode] + · simp only [longMasked, Method.withCode, List.drop_append, List.length_replicate, + Nat.sub_self, List.drop_zero, List.drop_replicate, List.replicate_zero, List.nil_append] intro op member target inside simp only [longSuffix, List.mem_append, List.mem_replicate, List.mem_cons, List.not_mem_nil, or_false] at member - rcases member with ⟨_, rfl⟩ | rfl | rfl | rfl | rfl <;> simp_all [Op.targets] <;> omega + rcases member with ⟨_, rfl⟩ | rfl | rfl | rfl | rfl <;> + simp_all [Instruction.targets] <;> omega · intro pc after rfl + · exact CIL.Embedding.links_of_shifted_table (expectedBefore := []) (expectedAfter := []) + (actualBefore := [(1, .direct callee)]) rfl rfl nofun (by simp) nofun + +/-- A call inside the relocated suffix keeps its link at the shifted pc. -/ +def callExpected : Method := + { code := [.ldarg 0, .ldc_i4 6, .shr, .ldarg 1, .call (Tests.callee 1) {}, .ret], + links := [(4, .direct 2)], locals := [], returnsValue := true } + +def callMasked : Method := + { callExpected.withCode (List.replicate 3 (.unadmitted 0) ++ callExpected.code.drop + 3) with links := callExpected.links } + +/-- A call to method 1 replaces the masked prefix, and the suffix's call moves to pc 3. -/ +def callActual : Method := + { callExpected.withCode ([.ldarg 0, .call (Tests.callee 1)] ++ callExpected.code.drop + 3) with links := [(1, .direct 1), (3, .direct 2)] } + +theorem callSuffix_refines : + CIL.Embedding.BodyRefines callMasked callActual (CIL.Embedding.suffixRename 3 2) := by + apply CIL.Embedding.BodyRefines.of_relocated_suffix + · rfl + · rfl + · omega + · intro pc op before found + have unmasked : pc = 0 ∨ pc = 1 ∨ pc = 2 := by omega + rcases unmasked with rfl | rfl | rfl <;> + (simp [callMasked, Method.withCode] at found; subst found; rfl) + · rfl + · intro op member target inside + simp [callMasked, callExpected, Method.withCode] at member + rcases member with rfl | rfl | rfl <;> simp [Instruction.targets] at inside + · intro pc after + rfl + · exact CIL.Embedding.links_of_shifted_table (expectedBefore := []) + (expectedAfter := [(4, .direct 2)]) (actualBefore := [(1, .direct 1)]) rfl rfl nofun + (by simp) (by simp) + +#print axioms callSuffix_refines theorem longSuffix_moves (profile : CIL.FeatureProfile) (callee fuel : Nat) (args stack : List Value) (frame : Frame) (m : Memory) (result : Memory × List Value) @@ -106,7 +166,7 @@ theorem longSuffix_moves (profile : CIL.FeatureProfile) (callee fuel : Nat) (arg have closed : RegionClosed [longExpected profile] suffix := RegionClosed.of_check (by rfl) have agrees : AgreesOn [longExpected profile] [longMasked profile] suffix := AgreesOn.set (program := [longExpected profile]) (index := 0) rfl (longMasked profile).code fun _ inside => - code_agrees_of_drop (lower := 3) (by simp [longMasked, longExpected]) + code_agrees_of_drop (lower := 3) (by simp [longMasked, longExpected, Method.withCode]) (by simpa [suffix, inStarts] using inside) rw [← run_congr_region closed agrees _ _ _ _ _ _ _ (by decide)] at ran exact run_refines ((ProgramRefines.refl [longActual profile callee]).set_body (index := 0) rfl @@ -128,7 +188,7 @@ theorem duplicateStarts : /-- A later duplicate cannot hide the earlier suffix's escaping branch. -/ theorem earlierStartRejects : regionClosedCheck - [{ caller with code := [.ret, .branch 0, .ret] }] [(0, 2), (0, 1)] = false := by + [caller.withCode [.ret, .br 0, .ret]] [(0, 2), (0, 1)] = false := by decide +kernel #print axioms earlierStartRejects @@ -139,7 +199,8 @@ theorem absentAndEmpty : regionClosedCheck [caller, helper] [(99, 0), (0, 999)] /-- A listed absent callee still belongs to the region; execution separately rejects it. -/ theorem absentCallee : regionClosedCheck - [{ caller with code := [.call 99 0, .ret] }] [(0, 0), (99, 0)] = true := by + [{ caller.withCode [.call (Tests.callee 0), .ret] with links := [(0, .direct 99)] }] + [(0, 0), (99, 0)] = true := by decide +kernel #print axioms absentCallee @@ -148,6 +209,6 @@ theorem emptyStarts : regionClosedCheck [caller, helper] [] = true := by decide #print axioms emptyStarts /-- Region membership extends past the body; execution checks instruction availability. -/ theorem finalFallthrough : regionClosedCheck - [{ caller with code := [.nop] }] [(0, 0)] = true := by decide +kernel + [caller.withCode [.nop]] [(0, 0)] = true := by decide +kernel #print axioms finalFallthrough end Tests.RegionRun diff --git a/Tests/RegionScaling.lean b/Tests/RegionScaling.lean index a310136b8..b69f82ebd 100644 --- a/Tests/RegionScaling.lean +++ b/Tests/RegionScaling.lean @@ -10,15 +10,17 @@ open CIL CIL.Safety set_option maxRecDepth 65536 set_option maxHeartbeats 0 -/-- Reference implementation with repeated instruction and region list scans. -/ +/-- Reference implementation with repeated instruction, link and region list scans. -/ def baselineRegionClosedCheck (program : CIL.Program) (starts : List (Nat × Nat)) : Bool := starts.all fun start => match program[start.1]? with | none => true - | some body => (List.range body.code.length).all fun pc => + | some body => ((List.range body.code.length).all fun pc => !(start.2 ≤ pc) || match body.code[pc]? with | none => true | some op => (!op.continues || inStarts starts start.1 (pc + 1)) && - op.targets.all (inStarts starts start.1) && op.callees.all (inStarts starts · 0) + op.targets.all (inStarts starts start.1)) && + body.links.all fun entry => + !(start.2 ≤ entry.1) || entry.2.callees.all (inStarts starts · 0) /-- A long method with a single full-method region. -/ diff --git a/Tests/ReinterpretedSpans.cs b/Tests/ReinterpretedSpans.cs index 7cb24766b..179686b1e 100644 --- a/Tests/ReinterpretedSpans.cs +++ b/Tests/ReinterpretedSpans.cs @@ -2,9 +2,14 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs -#:include RejectedEvidence.cs +#:include MarshalModelEvidence.cs using System.Reflection; using System.Runtime.InteropServices; @@ -44,14 +49,19 @@ string Run(int length, bool empty = false) => claims.Add($"({Run(length, length == 0)}).map Prod.snd = " + $".ok [{View(length * sourceWidth / targetWidth, targetWidth, length == 0)}]"); if (sourceWidth > targetWidth) - claims.Add($"({Run(int.MaxValue)}).map Prod.snd = .error ⟨0, 1, .thrown \"System.OverflowException\"⟩"); + claims.Add($"({Run(int.MaxValue)}).map Prod.snd = .error ⟨0, 1, .thrown .overflow⟩"); var uninitialized = Run(7).Replace("] memory", "] { memory with cells := fun _ _ => ⟨0, false⟩ }", StringComparison.Ordinal); claims.Add($"({uninitialized}).map Prod.snd = .ok [{View(7 * sourceWidth / targetWidth, targetWidth)}]"); var noAuthority = Run(7).Replace("] memory", "] { memory with views := [] }", StringComparison.Ordinal); claims.Add($"({noAuthority}).map Prod.snd = .ok [{View(7 * sourceWidth / targetWidth, targetWidth)}]"); } - File.WriteAllText(proof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + """ + var correctness = claims.Count == 1 ? "by rfl" + : $"by exact ⟨{string.Join(", ", claims.Select(_ => "by rfl"))}⟩"; + File.WriteAllText(proof, $$""" + import Extracted + import CIL.Safety.Execution + namespace ExampleProof /-- Sparse caller storage permits boundary lengths without allocating their byte contents. -/ def memory : CIL.Safety.Memory := { CIL.Safety.Memory.empty with allocations := fun identity => if identity == 0 then @@ -60,31 +70,22 @@ string Run(int length, bool empty = false) => views := [⟨0, 0, 2^35, true, true⟩] nextIdentity := 1 } /-- Empty, truncated, overflow and unreadable views retain their exact runtime behavior. -/ - """ + "\ntheorem correctness :\n" + string.Join(" ∧\n", claims) + - " := " + (claims.Count == 1 ? "by rfl" : - "by exact ⟨" + string.Join(", ", claims.Select(_ => "by rfl")) + "⟩") + - "\n#print axioms correctness\nend ExampleProof\n"); + theorem correctness : + {{string.Join(" ∧\n", claims)}} := {{correctness}} + #print axioms correctness + end ExampleProof + + """); queue.Add(new(method.FullName, proof, output), () => Console.WriteLine("PASS reinterpreted span: " + name)); } queue.Flush(); -var rejected = new RejectedEvidence(Path.Combine(root, "RejectedEvidence")); -var index = 0; -foreach (var (name, before, after) in new[] -{ - ("Bytes", ".spanReinterpret 4 1 true", ".spanReinterpret 8 1 true"), - ("Words", ".spanReinterpret 1 4 true", ".spanReinterpret 1 8 true"), - ("ReadOnlyWords", ".spanReinterpret 1 2 false", ".spanReinterpret 1 2 true"), - ("SameWidth", ".spanReinterpret 4 4 true", ".spanReadOnly") -}) -{ - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_scalars_validated"); - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing reinterpret mutation"); - var path = Path.Combine(root, $"Rejected{index++}.lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS exact reinterpret evidence rejection: " + name, "ScalarFlowValid"); -} -rejected.Check(); +CheckMarshalModels(root, +[ + ("Bytes", 1, ".spanReinterpret 4 1 true", ".spanReinterpret 2 1 true"), + ("Words", 1, ".spanReinterpret 1 4 true", ".spanReinterpret 2 4 true"), + ("ReadOnlyWords", 1, ".spanReinterpret 1 2 false", ".spanReinterpret 2 2 false"), + ("SameWidth", 1, ".spanReinterpret 4 4 true", ".spanReinterpret 2 4 true") +]); partial class Program { @@ -114,5 +115,3 @@ static void RuntimeChecks(Type runtime) } } - - diff --git a/Tests/RejectedEvidence.cs b/Tests/RejectedEvidence.cs index d2684da47..7fe01d9fb 100644 --- a/Tests/RejectedEvidence.cs +++ b/Tests/RejectedEvidence.cs @@ -3,6 +3,33 @@ partial class Program { + const string Isolation = """ + set_option Elab.async false + namespace EvidenceRejectionHarness + open Lean Elab Command + /-- Check invalid fixtures without letting their declarations or attributes escape. -/ + elab "#isolated_rejection" "{" commands:command* "}" : command => do + let saved ← get + let mut messages := saved.messages + for command in commands do + modify ({ · with messages := {} }) + messages := messages ++ + (← Lean.Elab.Tactic.GuardMsgs.runAndCollectMessages command) + set { saved with messages := messages } + end EvidenceRejectionHarness + """ + "\n"; + + // The mutated source with `theorem` stated as its own negation, `¬ (statement)`, proved by the kernel: the + // obligation a mutation breaks is shown false, not merely unprovable by the tactic that proves it when true. + static string KernelRefutation(string mutated, string theorem, string refutation, bool prefixOnly = false) + { + var obligation = SingleLineTheorem.Matches(mutated).FirstOrDefault(match => match.Groups[1].Value == theorem) + ?? throw new InvalidOperationException("Missing obligation to refute: " + theorem); + var proof = $"theorem {refutation} : ¬ ({obligation.Groups["statement"].Value}) := by decide +kernel"; + return prefixOnly ? mutated[..obligation.Index] + proof + $"\n#print axioms {refutation}\n" + : mutated.Replace(obligation.Value, proof); + } + // Each mutation must produce its own expected diagnostic. A different failing case // or an error outside the case cannot make a valid mutation appear rejected. sealed class RejectedEvidence(string directory) @@ -25,29 +52,13 @@ public void Add(string path, string passed, Func diagnostic, param { if (!item.Source.Contains("namespace Extracted\n", StringComparison.Ordinal)) throw new InvalidOperationException("Expected generated evidence: " + item.Path); - bodies.Add(Regex.Replace(item.Source, @"^import[ \t]+([A-Za-z_][A-Za-z0-9_.]*)[ \t]*\r?$", - match => { imports.Add(match.Groups[1].Value); return ""; }, RegexOptions.Multiline)); + bodies.Add(RejectedImport.Replace(item.Source, + match => { imports.Add(match.Groups[1].Value); return ""; })); } - const string isolation = """ - set_option maxErrors 0 - set_option Elab.async false - namespace EvidenceRejectionHarness - open Lean Elab Command - /-- Check invalid fixtures without letting their declarations or attributes escape. -/ - elab "#isolated_rejection" "{" commands:command* "}" : command => do - let saved ← get - let mut messages := saved.messages - for command in commands do - modify ({ · with messages := {} }) - messages := messages ++ - (← Lean.Elab.Tactic.GuardMsgs.runAndCollectMessages command) - set { saved with messages := messages } - end EvidenceRejectionHarness - """ + "\n"; var source = new StringBuilder(string.Join('\n', imports.Select(module => "import " + module)) + - "\n" + isolation); + "\n" + Isolation); var ranges = new List<(int Start, int End)>(); - var line = imports.Count + 1 + isolation.Count(character => character == '\n'); + var line = imports.Count + 1 + Isolation.Count(character => character == '\n'); for (var index = 0; index < cases.Count; index++) { source.Append($"#isolated_rejection {{\nnamespace RejectedCase{index}\n"); @@ -69,17 +80,20 @@ public void Check(string? leanPath = null) Directory.CreateDirectory(directory); var path = Path.GetFullPath(Path.Combine(directory, "RejectedBatch.lean")); File.WriteAllText(path, source); - var result = Execute(["lake", "env", "lean", path], cwd: FixturePackage(), leanPath: leanPath); - var diagnostics = Regex.Matches(result.Output, - @"(?m)^" + Regex.Escape(path.Replace('\\', '/')) + - @":(\d+):\d+: (error|warning|information)(?:\([^)\r\n]+\))?:"); - if (diagnostics.Count == 0) - diagnostics = Regex.Matches(result.Output, - @"(?m)^" + Regex.Escape(path) + - @":(\d+):\d+: (error|warning|information)(?:\([^)\r\n]+\))?:"); + // Lean caps errors at the command line's maxErrors, not the file's, and drops every later diagnostic. + var result = Execute(["lake", "env", "lean", "-DmaxErrors=0", path], cwd: FixturePackage(), + leanPath: leanPath); + // Lean names the file with either separator. + var located = LocatedDiagnostic.Matches(result.Output); + Match[] diagnostics = [.. located.Where(match => match.Groups["file"].Value == path.Replace('\\', '/'))]; + if (diagnostics.Length == 0) + diagnostics = [.. located.Where(match => match.Groups["file"].Value == path)]; var starts = ranges.Select(range => range.Start).ToArray(); var errors = cases.Select(_ => new StringBuilder()).ToArray(); - for (var diagnostic = 0; diagnostic < diagnostics.Count; diagnostic++) + var sourceLines = source.Split('\n'); + var firstLines = Enumerable.Repeat(int.MaxValue, cases.Count).ToArray(); + var refutable = new string?[cases.Count]; + for (var diagnostic = 0; diagnostic < diagnostics.Length; diagnostic++) { var match = diagnostics[diagnostic]; if (match.Groups[2].Value != "error") continue; @@ -87,8 +101,17 @@ public void Check(string? leanPath = null) var index = Array.BinarySearch(starts, location); if (index < 0) index = ~index - 1; if (index < 0 || location >= ranges[index].End) continue; - var end = diagnostic + 1 < diagnostics.Count ? diagnostics[diagnostic + 1].Index : result.Output.Length; - errors[index].Append(result.Output.AsSpan(match.Index, end - match.Index)); + var end = diagnostic + 1 < diagnostics.Length + ? diagnostics[diagnostic + 1].Index + : result.Output.Length; + var message = result.Output.AsSpan(match.Index, end - match.Index); + errors[index].Append(message); + if (location >= firstLines[index]) continue; + firstLines[index] = location; + var theorem = SingleLineTheorem.Match(sourceLines[location - 1]); + var decidedFalse = message.Contains("proved that the proposition", StringComparison.Ordinal) && + message.Contains("is false", StringComparison.Ordinal); + refutable[index] = theorem.Success && decidedFalse ? theorem.Groups[1].Value : null; } for (var index = 0; index < cases.Count; index++) { @@ -101,8 +124,58 @@ public void Check(string? leanPath = null) if (File.ReadAllText(cases[index].Path) != cases[index].Source) throw new InvalidOperationException("Mutation changed during checking: " + cases[index].Path); } + RefuteSemanticRejections(refutable, leanPath); foreach (var item in cases) Console.WriteLine(item.Passed); cases.Clear(); } + + // A rejection the evaluator reports as a decided falsehood must also be refuted by the kernel: the obligation + // is stated as its negation and proved by `decide +kernel`, so the mutation is shown false, not merely + // unprovable. Rejections of another kind (a name that no longer resolves, a malformed term) have no negation + // to prove and are counted, not hidden. + void RefuteSemanticRejections(string?[] theorems, string? leanPath) + { + var refutations = new List(); + var imports = new SortedSet(StringComparer.Ordinal); + var unrefuted = new List(); + for (var index = 0; index < cases.Count; index++) + { + if (theorems[index] is not { } theorem) + { + unrefuted.Add(Path.GetFileName(cases[index].Path)); + continue; + } + var body = RejectedImport.Replace( + KernelRefutation(cases[index].Source, theorem, "kernelRefutation", prefixOnly: true), + match => { imports.Add(match.Groups[1].Value); return ""; }); + // Only the declarations preceding the failed theorem are premises of its refutation. + refutations.Add($"#isolated_rejection {{\n{body}\n}}\n"); + } + if (refutations.Count > 0) + { + var path = Path.GetFullPath(Path.Combine(directory, "KernelRefutations.lean")); + imports.Add("Lean"); + File.WriteAllText(path, string.Join('\n', imports.Select(module => "import " + module)) + "\n" + + Isolation + string.Concat(refutations)); + var proved = Execute(["lake", "env", "lean", path], cwd: FixturePackage(), leanPath: leanPath); + if (proved.Code != 0 || proved.Output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException( + "The kernel did not refute a mutation the evaluator found false:\n" + proved.Output); + } + Console.WriteLine($"PASS kernel refutations of {refutations.Count} decided rejections" + + (unrefuted.Count == 0 ? "" : $"; {unrefuted.Count} rejections of another kind have none: " + + string.Join(", ", unrefuted))); + } } + + [GeneratedRegex(@"^import[ \t]+([A-Za-z_][A-Za-z0-9_.]*)[ \t]*\r?$", RegexOptions.Multiline)] + private static partial Regex RejectedImport { get; } + + // A theorem stated and proved on one line; a Lean name may end in ?, ! or '. + [GeneratedRegex(@"(?m)^(?:@\[[^\r\n]*\] )?theorem ([\w?!']+) : " + + @"(?:(?[^\r\n]*) := by decide(?: \+kernel)?[ \t]*|(?[^\r\n]*?) := [^\r\n]*)(?=\r?$)")] + private static partial Regex SingleLineTheorem { get; } + + [GeneratedRegex(@"(?m)^(?[^\r\n]*?):(\d+):\d+: (error|warning|information)(?:\([^)\r\n]+\))?:")] + private static partial Regex LocatedDiagnostic { get; } } diff --git a/Tests/RejectedEvidenceTests.cs b/Tests/RejectedEvidenceTests.cs index 9a9e76f26..68f5a6cf4 100644 --- a/Tests/RejectedEvidenceTests.cs +++ b/Tests/RejectedEvidenceTests.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include RejectedEvidence.cs #:include GeneratedEvidence.cs diff --git a/Tests/ReplayDiagnostics.lean b/Tests/ReplayDiagnostics.lean new file mode 100644 index 000000000..0cfc53911 --- /dev/null +++ b/Tests/ReplayDiagnostics.lean @@ -0,0 +1,240 @@ +import CIL.Replay + +/-! Typed replay classifications preserve resource failures and join every deferred task. -/ + +open CIL.Replay + +/-- Resource and generic failures cannot identify a semantic rejection. -/ +def excluded : IO Unit := do + for error in [Lean.Kernel.Exception.other "type mismatch", .deterministicTimeout, + .excessiveMemory, .deepRecursion, .interrupted] do + unless (rejectionKind error).isNone do throw <| IO.userError "Resource misclassified" + +#eval excluded + +/-- Every approved kernel exception constructor retains its distinct typed category. -/ +def included : IO Unit := do + let environment ← Lean.mkEmptyEnvironment + let env := environment.toKernelEnv + let expression := Lean.mkSort .zero + let declaration := Lean.Declaration.axiomDecl + { name := `Test, levelParams := [], type := expression, isUnsafe := false } + let cases : List (Lean.Kernel.Exception × RejectionKind) := [ + (.unknownConstant env `Test, .unknownConstant), + (.alreadyDeclared env `Test, .alreadyDeclared), + (.declTypeMismatch env declaration expression, .declTypeMismatch), + (.declHasMVars env `Test expression, .declHasMVars), + (.declHasFVars env `Test expression, .declHasFVars), + (.funExpected env {} expression, .funExpected), + (.typeExpected env {} expression, .typeExpected), + (.letTypeMismatch env {} `Test expression expression, .letTypeMismatch), + (.exprTypeMismatch env {} expression expression, .exprTypeMismatch), + (.appTypeMismatch env {} expression expression expression, .appTypeMismatch), + (.invalidProj env {} expression, .invalidProj), + (.thmTypeIsNotProp env `Test expression, .thmTypeIsNotProp)] + for (error, expected) in cases do + unless rejectionKind error == some expected do throw <| IO.userError "Kernel category lost" + +#eval included + +/-- An earlier unavailable check defeats a later semantic result, and every task is joined. -/ +def unavailableFirst : IO Unit := do + for first in [Except.error (IO.userError "rendering failed"), + Except.ok (CheckOutcome.rejected "resource exhausted" none)] do + let finished ← IO.mkRef false + let check ← IO.asTask do + finished.set true + return CheckOutcome.rejected "bad theorem" (some .declTypeMismatch) + let checks ← IO.mkRef #[(`first, Task.pure first), (`second, check)] + let rejection ← IO.mkRef none + unless (← awaitChecks checks rejection).isSome && (← rejection.get).isNone && + (← finished.get) do + throw <| IO.userError "Unavailable first result or task join lost" + +#eval unavailableFirst + +/-- Cancellation alone supplies no semantic rejection classification. -/ +def cancellation : IO Unit := do + let checks ← IO.mkRef #[(`cancelled, Task.pure (.ok CheckOutcome.cancelled))] + let rejection ← IO.mkRef none + discard <| awaitChecks checks rejection + unless (← rejection.get).isNone do throw <| IO.userError "Cancellation misclassified" + +#eval cancellation + +/-- Sequential kernel failures carry a kind; missing postponed records remain unavailable. -/ +def sequentialChecks : IO Unit := do + let env ← Lean.mkEmptyEnvironment + let cancel ← IO.CancelToken.new + let checks ← IO.mkRef #[] + let rejection ← IO.mkRef none + let context : ReplayContext := { newConstants := {}, cancel, checks, rejection } + let declaration := Lean.Declaration.inductDecl [] 0 + [{ name := `Broken, type := Lean.mkConst `Missing, ctors := [] }] false + let mut failed := false + try + discard <| (addDecl declaration).run context |>.run { env := env.toKernelEnv } + catch _ => failed := true + unless failed && (← rejection.get).any (·.kind == .unknownConstant) do + throw <| IO.userError "Sequential rejection lost" + rejection.set none + try + discard <| checkPostponed.run context |>.run + { env := env.toKernelEnv, postponedConstructors := ({} : Lean.NameSet).insert `Missing } + catch _ => pure () + unless (← rejection.get).isNone do throw <| IO.userError "Missing record misclassified" + +#eval sequentialChecks + +/-- A malformed sequential block keeps deferred semantic failures unavailable. -/ +def sequentialUnavailable : IO Unit := do + let environment ← Lean.mkEmptyEnvironment + let badProof : Lean.TheoremVal := { + name := `Dependency, levelParams := [], type := Lean.mkSort .zero, value := Lean.mkSort .zero } + let cancel ← IO.CancelToken.new + match ← checkDeclaration environment.toKernelEnv (.thmDecl badProof) cancel with + | .rejected _ (some _) => pure () + | _ => throw <| IO.userError "Expected a semantic deferred failure" + let malformed : Lean.InductiveVal := { (default : Lean.InductiveVal) with + name := `Block, type := Lean.mkConst `Dependency, all := [`Missing] } + let constants : Std.HashMap Lean.Name Lean.ConstantInfo := + ({} : Std.HashMap Lean.Name Lean.ConstantInfo) + |>.insert `Block (.inductInfo malformed) |>.insert `Dependency (.thmInfo badProof) + let rejection ← IO.mkRef none + let mut failed := false + try discard <| replay constants environment.toKernelEnv (some rejection) + catch _ => failed := true + unless failed && (← rejection.get).isNone do + throw <| IO.userError "Sequential unavailability was overwritten by a deferred rejection" + +#eval sequentialUnavailable + +/-- Constructor and recursor mismatches require both records with the expected kind. -/ +def postponedChecks : IO Unit := do + let env ← Lean.mkEmptyEnvironment + let declaration := Lean.Declaration.inductDecl [] 0 + [{ name := `Single, type := Lean.mkSort (.succ .zero), + ctors := [{ name := `Single.mk, type := Lean.mkConst `Single }] }] false + let kernel ← match env.toKernelEnv.addDeclCore 0 0 declaration none with + | .ok kernel => pure kernel + | .error error => throw <| IO.userError (← kernelMessage error) + let some (.ctorInfo ctor) := kernel.find? `Single.mk + | throw <| IO.userError "Missing generated constructor" + let some (.recInfo recursor) := kernel.find? `Single.rec + | throw <| IO.userError "Missing generated recursor" + let cases := #[(Lean.ConstantInfo.ctorInfo { ctor with numFields := ctor.numFields + 1 }, + RejectionKind.constructorMismatch), + (Lean.ConstantInfo.recInfo { recursor with numMinors := recursor.numMinors + 1 }, + RejectionKind.recursorMismatch)] + for (info, expected) in cases do + let cancel ← IO.CancelToken.new + let checks ← IO.mkRef #[] + let rejection ← IO.mkRef none + let context : ReplayContext := + { newConstants := ({} : Std.HashMap Lean.Name Lean.ConstantInfo).insert info.name info, + cancel, checks, rejection } + let names := ({} : Lean.NameSet).insert info.name + let state : ReplayState := { + env := kernel + postponedConstructors := (if expected == .constructorMismatch then names else {}) + postponedRecursors := (if expected == .recursorMismatch then names else {}) } + try discard <| checkPostponed.run context |>.run state + catch _ => pure () + unless (← rejection.get).any (·.kind == expected) do + throw <| IO.userError "Postponed mismatch lost" + +#eval postponedChecks + +/-- Ownership rejects duplicate names, missing mutual members and imported declarations. -/ +unsafe def ownership : IO Unit := do + let env ← Lean.mkEmptyEnvironment + let info : Lean.ConstantInfo := .axiomInfo + { name := `Owned, levelParams := [], type := Lean.mkSort .zero, isUnsafe := false } + let base : Lean.ModuleData := + { (default : Lean.ModuleData) with constNames := #[info.name], constants := #[info] } + discard <| diagnosticConstants base env + let mutualInfo : Lean.ConstantInfo := .thmInfo { + name := `Owned, levelParams := [], type := Lean.mkSort .zero, + value := Lean.mkSort .zero, all := [`Unowned] } + let ownedDeclaration := Lean.Declaration.axiomDecl + { name := `Owned, levelParams := [], type := Lean.mkSort .zero, isUnsafe := false } + let imported ← match env.addDeclCore 0 0 ownedDeclaration none with + | .ok imported => pure imported + | .error error => throw <| IO.userError (← kernelMessage error) + for (data, environment) in #[(base, imported), + ({ base with constNames := #[`Owned, `Owned], constants := #[info, info] }, env), + ({ base with constNames := #[`Other] }, env), + ({ base with constNames := #[] }, env), + ({ base with extraConstNames := #[`Extra, `Extra] }, env), + ({ base with constants := #[mutualInfo] }, env)] do + let mut failed := false + try discard <| diagnosticConstants data environment + catch _ => failed := true + unless failed do throw <| IO.userError "Ambiguous ownership accepted" + +#eval ownership + +/-- The owner index grows with the declaration inventory, without per-declaration rescans. -/ +unsafe def ownershipScaling : IO Unit := do + let env ← Lean.mkEmptyEnvironment + let mut previous := 0 + for count in [64, 256, 1024] do + let constants := (List.range count).toArray.map fun index => + Lean.ConstantInfo.axiomInfo { + name := .num `Owned index, levelParams := [] + type := Lean.mkSort .zero, isUnsafe := false } + let data : Lean.ModuleData := { (default : Lean.ModuleData) with + constNames := constants.map (·.name), constants } + discard <| diagnosticConstants data env + let before ← IO.getNumHeartbeats + discard <| diagnosticConstants data env + let used := (← IO.getNumHeartbeats) - before + if previous > 0 && used > previous * 6 then + throw <| IO.userError "Owner inventory grew faster than its linear bound" + IO.println s!"Owner inventory {count}: {used} heartbeats" + previous := used + +#eval ownershipScaling + +/-- Shared complete mutual lists require work proportional to their distinct physical cells. -/ +unsafe def mutualOwnershipScaling : IO Unit := do + let env ← Lean.mkEmptyEnvironment + let mut previous := 0 + for count in [64, 256, 1024] do + let names := (List.range count).map (Lean.Name.num `Mutual) + let constants := names.toArray.map fun name => + Lean.ConstantInfo.thmInfo { + name, levelParams := [], type := Lean.mkSort .zero, + value := Lean.mkSort .zero, all := names } + let data : Lean.ModuleData := { (default : Lean.ModuleData) with + constNames := names.toArray, constants } + discard <| diagnosticConstants data env + let before ← IO.getNumHeartbeats + discard <| diagnosticConstants data env + let used := (← IO.getNumHeartbeats) - before + if previous > 0 && used > previous * 6 then + throw <| IO.userError "Shared mutual inventory grew faster than its linear bound" + IO.println s!"Shared mutual inventory {count}: {used} heartbeats" + previous := used + +#eval mutualOwnershipScaling + +/-- Equal heads with physically divergent tails do not share ownership validation. -/ +unsafe def divergentOwnership : IO Unit := do + let env ← Lean.mkEmptyEnvironment + for reverse in [false, true] do + let owned : Lean.ConstantInfo := .thmInfo { + name := `Owned, levelParams := [], type := Lean.mkSort .zero, + value := Lean.mkSort .zero, all := [`Owned, `Other] } + let divergent : Lean.ConstantInfo := .thmInfo { + name := `Other, levelParams := [], type := Lean.mkSort .zero, + value := Lean.mkSort .zero, all := [`Owned, `Unowned] } + let constants := if reverse then #[divergent, owned] else #[owned, divergent] + let data : Lean.ModuleData := { (default : Lean.ModuleData) with + constNames := constants.map (·.name), constants } + let mut failed := false + try discard <| diagnosticConstants data env + catch _ => failed := true + unless failed do throw <| IO.userError "Divergent unowned mutual tail accepted" + +#eval divergentOwnership diff --git a/Tests/ResolutionFacts.lean b/Tests/ResolutionFacts.lean new file mode 100644 index 000000000..fd4603f4f --- /dev/null +++ b/Tests/ResolutionFacts.lean @@ -0,0 +1,1107 @@ +import CIL.Resolution +import CIL.Safety.ValueSteps +import CIL.Safety.StepTransport +import CIL.Extraction.Translation +import CIL.Safety.ArrayContents + +/-! +# Declared narrowing + +Pins the narrowing a declared type implies, which `ret` applies to a method's return value: +each primitive below 32 bits narrows to its own width and signedness and wider primitives do +not; an enum narrows as its underlying primitive only where `typeStorage` admits that primitive; +struct, object, pointer, array and span types never narrow; a misspelled name or a primitive +name from another assembly is not a primitive; and a method that returns nothing never narrows, +whatever its declared return type says. The method facts are proved by lemma application, as a +generated fact is, so no proof below evaluates a type name except the misspelling cases. + +Pins how a method reads each argument: an argument without a home narrowed as its declared type +requires, `this` as it arrives, and a homed argument from the slot after the declared locals, +which is the slot the argument check expects. `cil_resolution_facts` states the same for static +and instance methods and across suffix blocks, refuses homes out of argument order, and the +simplifier reads a literal body without facts and leaves a symbolic one alone. + +Pins the shape of array elements: a primitive, an enum, a class and a function pointer by lemma, +a struct of the size its checked layout gives, and no shape for a by-ref-like struct or one the +layouts do not hold. `cil_resolution_facts` declares one fact per distinct element type a raw +method names, reads struct layouts across suffix blocks shared by an environment's methods, and +the simplifier reads a literal body without facts. + +Pins the runtime model facts: `cil_runtime_models` declares none for a call no model stands for, +and the simplifier reads a literal body's model lookup without a fact. +-/ + +namespace Tests.ResolutionFacts + +open CIL CIL.Extraction + +-- A primitive below 32 bits narrows to its own width and signedness; the extractor's own table +-- agrees head by head (Tests/InstructionIndexes.cs). +example : declaredNarrow [] (.atom runtimeScope "System.SByte") = some .signed8 := + declaredNarrow_primitive [] .sbyte +example : declaredNarrow [] (.atom runtimeScope "System.Byte") = some .unsigned8 := + declaredNarrow_primitive [] .byte +example : declaredNarrow [] (.atom runtimeScope "System.Boolean") = some .unsigned8 := + declaredNarrow_primitive [] .boolean +example : declaredNarrow [] (.atom runtimeScope "System.Int16") = some .signed16 := + declaredNarrow_primitive [] .int16 +example : declaredNarrow [] (.atom runtimeScope "System.UInt16") = some .unsigned16 := + declaredNarrow_primitive [] .uint16 +example : declaredNarrow [] (.atom runtimeScope "System.Char") = some .unsigned16 := + declaredNarrow_primitive [] .char + +-- A primitive of 32 bits or more, or a floating-point one, is returned as it is. +example : declaredNarrow [] (.atom runtimeScope "System.Int32") = none := + declaredNarrow_primitive [] .int32 +example : declaredNarrow [] (.atom runtimeScope "System.UInt32") = none := + declaredNarrow_primitive [] .uint32 +example : declaredNarrow [] (.atom runtimeScope "System.Int64") = none := + declaredNarrow_primitive [] .int64 +example : declaredNarrow [] (.atom runtimeScope "System.UInt64") = none := + declaredNarrow_primitive [] .uint64 +example : declaredNarrow [] (.atom runtimeScope "System.IntPtr") = none := + declaredNarrow_primitive [] .intPtr +example : declaredNarrow [] (.atom runtimeScope "System.UIntPtr") = none := + declaredNarrow_primitive [] .uintPtr +example : declaredNarrow [] (.atom runtimeScope "System.Single") = none := + declaredNarrow_primitive [] .single +example : declaredNarrow [] (.atom runtimeScope "System.Double") = none := + declaredNarrow_primitive [] .double + +-- An enum narrows as its underlying integer type. +example : declaredNarrow [] (.enum "Examples.Small" (.atom runtimeScope "System.Int16")) = + some .signed16 := + (declaredNarrow_enum [] "Examples.Small" .int16).trans rfl +example : declaredNarrow [] (.enum "Examples.Flags" (.atom runtimeScope "System.Byte")) = + some .unsigned8 := + (declaredNarrow_enum [] "Examples.Flags" .byte).trans rfl +example : declaredNarrow [] (.enum "Examples.Signed" (.atom runtimeScope "System.SByte")) = + some .signed8 := + (declaredNarrow_enum [] "Examples.Signed" .sbyte).trans rfl +example : declaredNarrow [] (.enum "Examples.Wide" (.atom runtimeScope "System.UInt16")) = + some .unsigned16 := + (declaredNarrow_enum [] "Examples.Wide" .uint16).trans rfl +example : declaredNarrow [] (.enum "Examples.Plain" (.atom runtimeScope "System.Int32")) = none := + (declaredNarrow_enum [] "Examples.Plain" .int32).trans rfl + +-- An enum over a type `typeStorage` does not admit as an enum's underlying type has no storage. +example : declaredNarrow [] (.enum "Examples.Truth" (.atom runtimeScope "System.Boolean")) = + none := + (declaredNarrow_enum [] "Examples.Truth" .boolean).trans rfl + +/-- A struct layout for the aggregate return below. -/ +def pointLayout : RawLayout := + { identity := "Examples.Point", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .signed8 }, { type := .signed8 }] } + +-- A struct, even one of byte fields, and every other non-primitive head do not narrow. +example : declaredNarrow [pointLayout] (.aggregate "Examples.Point" 0) = none := + declaredNarrow_of_not_candidate _ _ rfl +example : declaredNarrow [] (.object "Examples" "Examples.Box") = none := + declaredNarrow_of_not_candidate _ _ rfl +example : declaredNarrow [] (.pointer (.atom runtimeScope "System.Byte")) = none := + declaredNarrow_of_not_candidate _ _ rfl +example : declaredNarrow [] (.array (.atom runtimeScope "System.Byte")) = none := + declaredNarrow_of_not_candidate _ _ rfl +example : declaredNarrow [] + (.apply (.constructor runtimeScope "System.Span`1") (.atom runtimeScope "System.Byte")) = + none := + declaredNarrow_of_not_candidate _ _ rfl + +-- A name that only resembles a primitive's, or a primitive's name in another assembly, is not +-- that primitive and does not narrow. +example : declaredNarrow [] (.atom runtimeScope "SByte") = none := by rfl +/-- The assembly scope of the example application. -/ +def examplesScope : String := "Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + +example : declaredNarrow [] (.atom examplesScope "System.Byte") = none := by rfl + +/-- A method that returns a byte. -/ +def byteReturn : Method := + { code := [.ldc_i4 7, .ret], locals := [], returnsValue := true, + declared := { returnType := .atom runtimeScope "System.Byte" } } + +/-- A method whose return value narrows to a byte. -/ +theorem byteReturn_returnNarrow : byteReturn.returnNarrow = some .unsigned8 := + Method.returnNarrow_of_declared rfl rfl (declaredNarrow_primitive _ .byte) + +/-- info: 'Tests.ResolutionFacts.byteReturn_returnNarrow' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms byteReturn_returnNarrow + +-- A narrowed method returns a value. +example : byteReturn.returnsValue = true := + Method.returnsValue_of_returnNarrow byteReturn_returnNarrow + +/-- A method returning an enum over `System.Int16`. -/ +def enumReturn : Method := + { code := [.ldc_i4 7, .ret], locals := [], returnsValue := true, + declared := { returnType := .enum "Examples.Small" (.atom runtimeScope "System.Int16") } } + +/-- A method whose return value narrows as its enum's underlying type. -/ +theorem enumReturn_returnNarrow : enumReturn.returnNarrow = some .signed16 := + Method.returnNarrow_of_declared rfl rfl ((declaredNarrow_enum _ _ .int16).trans rfl) + +/-- info: 'Tests.ResolutionFacts.enumReturn_returnNarrow' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms enumReturn_returnNarrow + +/-- A method returning a struct. -/ +def pointReturn : Method := + { code := [.ret], locals := [], returnsValue := true, + declared := { returnType := .aggregate "Examples.Point" 0 }, + environment := { rawLayouts := [pointLayout] } } + +/-- A method returning a struct does not narrow its return value. -/ +theorem pointReturn_returnNarrow : pointReturn.returnNarrow = none := + Method.returnNarrow_of_declared rfl rfl (declaredNarrow_of_not_candidate _ _ rfl) + +/-- info: 'Tests.ResolutionFacts.pointReturn_returnNarrow' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms pointReturn_returnNarrow + +/-- A method that returns nothing, with a declared return type that would narrow. -/ +def voidReturn : Method := + { code := [.ret], locals := [], returnsValue := false, + declared := { returnType := .atom runtimeScope "System.Byte" } } + +/-- A method that returns nothing does not narrow, whatever its declared return type. -/ +theorem voidReturn_returnNarrow : voidReturn.returnNarrow = none := + Method.returnNarrow_of_void rfl + +/-- info: 'Tests.ResolutionFacts.voidReturn_returnNarrow' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms voidReturn_returnNarrow + +-- The lemmas generated facts apply stay within the kernel's standard axioms. +/-- info: 'CIL.Extraction.declaredNarrow_primitive' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms declaredNarrow_primitive + +/-- info: 'CIL.Extraction.declaredNarrow_enum' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms declaredNarrow_enum + +/-- info: 'CIL.Extraction.declaredNarrow_of_not_candidate' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms declaredNarrow_of_not_candidate + +namespace Generated + +open Lean Elab Command in +/-- A named signature fixture with both generated and simplifier proofs. -/ +elab "resolution_fixture " name:ident " : " type:term " returns " returns:term + " narrows " expected:term : command => do + let returnsName := mkIdent (name.getId.appendAfter "_returnsValue") + let declaredName := mkIdent (name.getId.appendAfter "_declared") + let narrowedName := mkIdent (name.getId.appendAfter "_returnNarrow") + let simplifiedName := mkIdent (name.getId.appendAfter "_simplified") + elabCommand (← `(def $name : Method := + { code := [], locals := [], returnsValue := $returns, declared := { returnType := $type }, + environment := { rawLayouts := [pointLayout] } })) + elabCommand (← `(theorem $returnsName : ($name).returnsValue = $returns := rfl)) + elabCommand (← `(theorem $declaredName : ($name).declared = { returnType := $type } := rfl)) + elabCommand (← `(cil_resolution_facts $name)) + elabCommand (← `(example : ($name).returnNarrow = $expected := $narrowedName)) + elabCommand (← `(theorem $simplifiedName : ($name).returnNarrow = $expected := by + simp only [cil_step])) + elabCommand (← `(#print axioms $returnsName)) + elabCommand (← `(#print axioms $declaredName)) + elabCommand (← `(#print axioms $narrowedName)) + elabCommand (← `(#print axioms $simplifiedName)) + +resolution_fixture primitive : (.atom runtimeScope "System.Byte") + returns true narrows (some .unsigned8) +resolution_fixture enumeration : (.enum "Small" (.atom runtimeScope "System.Int16")) + returns true narrows (some .signed16) +resolution_fixture invalidEnumeration : (.enum "Truth" (.atom runtimeScope "System.Boolean")) + returns true narrows none +resolution_fixture nestedEnumeration : + (.enum "Outer" (.enum "Inner" (.atom runtimeScope "System.Byte"))) + returns true narrows none +resolution_fixture noValue : (.atom runtimeScope "System.Byte") returns false narrows none +resolution_fixture aggregate : (.aggregate "Examples.Point" 0) returns true narrows none +resolution_fixture foreign : (.atom "Other" "System.Byte") returns true narrows none +resolution_fixture misspelled : (.atom runtimeScope "System.Bytex") returns true narrows none + +/-- Enum storage over an aggregate stays unresolved as storage, but its narrowing is known. -/ +def aggregateEnum (layouts : List RawLayout) : Method := + { code := [], locals := [], returnsValue := true, environment := { rawLayouts := layouts }, + declared := { returnType := .enum "Bad" (.aggregate "Point" 0) } } + +/-- The aggregate enum fixture returns a value. -/ +theorem aggregateEnum_returnsValue (layouts : List RawLayout) : + (aggregateEnum layouts).returnsValue = true := rfl +#print axioms aggregateEnum_returnsValue + +/-- The aggregate enum fixture retains its exact declared type. -/ +theorem aggregateEnum_declared (layouts : List RawLayout) : + (aggregateEnum layouts).declared = + { returnType := .enum "Bad" (.aggregate "Point" 0) } := rfl +#print axioms aggregateEnum_declared + +cil_resolution_facts aggregateEnum +#print axioms aggregateEnum_returnNarrow + +/-- Enum narrowing over an aggregate does not inspect a symbolic layout table. -/ +theorem aggregateEnum_simplified (layouts : List RawLayout) : + (aggregateEnum layouts).returnNarrow = none := by simp only [cil_step] +#print axioms aggregateEnum_simplified + +/-- Nested enum metadata over an aggregate array retains an arbitrary layout table. -/ +def arrayEnum (layouts : List RawLayout) : Method := + { code := [], locals := [], returnsValue := true, environment := { rawLayouts := layouts }, + declared := { returnType := .enum "Outer" (.enum "Bad" (.array (.aggregate "Point" 0))) } } + +/-- The aggregate array enum fixture returns a value. -/ +theorem arrayEnum_returnsValue (layouts : List RawLayout) : + (arrayEnum layouts).returnsValue = true := rfl +#print axioms arrayEnum_returnsValue + +/-- The aggregate array enum fixture retains its exact declared type. -/ +theorem arrayEnum_declared (layouts : List RawLayout) : + (arrayEnum layouts).declared = + { returnType := .enum "Outer" (.enum "Bad" (.array (.aggregate "Point" 0))) } := rfl +#print axioms arrayEnum_declared + +cil_resolution_facts arrayEnum +#print axioms arrayEnum_returnNarrow + +/-- Nested enum narrowing over an aggregate array does not inspect a symbolic layout table. -/ +theorem arrayEnum_simplified (layouts : List RawLayout) : + (arrayEnum layouts).returnNarrow = none := by simp only [cil_step] +#print axioms arrayEnum_simplified + +/-- info: 'Tests.ResolutionFacts.Generated.primitive_returnNarrow' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms primitive_returnNarrow + +/-- A symbolic signature retains its unknown return narrowing. -/ +def unknown (type : TypeExpr) : Method := + { code := [], locals := [], returnsValue := true, declared := { returnType := type } } + +/-- The symbolic method always returns a value. -/ +theorem unknown_returnsValue (type : TypeExpr) : (unknown type).returnsValue = true := rfl +#print axioms unknown_returnsValue + +/-- The symbolic method preserves its declared type. -/ +theorem unknown_declared (type : TypeExpr) : + (unknown type).declared = { returnType := type } := rfl +#print axioms unknown_declared + +/-- +error: cil_resolution_facts: return narrowing of +Tests.ResolutionFacts.Generated.unknown is unresolved +-/ +#guard_msgs (whitespace := lax) in +cil_resolution_facts unknown + +example (type : TypeExpr) (expected : Option NarrowKind) + (known : (unknown type).returnNarrow = expected) : + (unknown type).returnNarrow = expected := by + fail_if_success simp only [cil_step] + exact known + +/-- Differing return narrowing prevents metadata transport even when both methods return values. -/ +theorem differing_narrowing_rejected : ¬ CIL.Safety.StepMetadata primitive enumeration := by + intro metadata + have mismatch := metadata.returnNarrow + rw [primitive_returnNarrow, enumeration_returnNarrow] at mismatch + cases mismatch +#print axioms differing_narrowing_rejected + +/-- A body without code under the empty layout environment. -/ +def emptyEnvironment : Method := { code := [], locals := [], returnsValue := false } + +/-- The same body under an environment that holds one struct layout. -/ +def pointEnvironment : Method := + { emptyEnvironment with environment := { rawLayouts := [pointLayout] } } + +/-- The bodies agree on every metadata field apart from their layout environments. -/ +theorem matching_environment_independent_metadata : + pointEnvironment.profile = emptyEnvironment.profile /\ + pointEnvironment.returnsValue = emptyEnvironment.returnsValue /\ + pointEnvironment.staticSites = emptyEnvironment.staticSites /\ + pointEnvironment.returnNarrow = emptyEnvironment.returnNarrow /\ + (forall index, exists narrow, emptyEnvironment.argumentAccess index = .value narrow /\ + pointEnvironment.argumentAccess index = .value narrow) := + And.intro rfl (And.intro rfl (And.intro rfl (And.intro rfl + (fun _ => Exists.intro _ (And.intro rfl rfl))))) +#print axioms matching_environment_independent_metadata + +/-- Differing layout environments prevent metadata transport, since a step may read a layout. -/ +theorem differing_environment_rejected : + Not (CIL.Safety.StepMetadata emptyEnvironment pointEnvironment) := by + intro metadata + have mismatch := congrArg LayoutEnvironment.rawLayouts metadata.environment + cases mismatch +#print axioms differing_environment_rejected + +/-- Signature and environment evidence suffice without an instruction-shaped hypothesis. -/ +theorem shape_narrowing {rawLayouts : List RawLayout} {layouts : Array ComputedLayout} + {closed : RawMethod} {method : Method} + (shape : MethodShapeValid closed method) (declared : DeclaredValid closed method) + (environment : MethodEnvironmentValid rawLayouts layouts method) : + method.returnNarrow = declaredNarrow rawLayouts closed.returnType := + returnNarrow_of_shape shape declared environment +#print axioms shape_narrowing + +end Generated + + +/-! ## Argument access -/ + +/-- A byte parameter and an int parameter, returning an int. -/ +def twoParameterSignature : DeclaredSignature := + { parameters := [.atom runtimeScope "System.Byte", .atom runtimeScope "System.Int32"], + returnType := .atom runtimeScope "System.Int32" } + +/-- A static method with a byte parameter read as it arrives and an int parameter that the body + stores to, so it has a home after the method's one local. -/ +def twoParameters : Method := + { code := [.ldc_i4 0, .starg 1, .ldarg 0, .ret], locals := [.i32 0], + localKinds := [.word32], parameterHomes := [(1, .word32)], returnsValue := true, + declared := twoParameterSignature } + +-- The byte argument is read narrowed to its declared width. +example : twoParameters.argumentAccess 0 = .value (some .unsigned8) := + Method.argumentAccess_value rfl rfl (declaredNarrow_primitive _ .byte) + +-- The stored argument is read from its home, the slot after the one declared local. +example : twoParameters.argumentAccess 1 = .home 1 := Method.argumentAccess_home rfl rfl + +-- An index past the declared parameters has no type and is read as it arrives. +example : twoParameters.argumentAccess 2 = .value none := Method.argumentAccess_undeclared rfl rfl + +/-- The return fact the facts command also reads. -/ +theorem twoParameters_returnsValue : twoParameters.returnsValue = true := rfl +#print axioms twoParameters_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem twoParameters_declared : twoParameters.declared = twoParameterSignature := rfl +#print axioms twoParameters_declared + +cil_resolution_facts twoParameters + +example : twoParameters.argumentAccess 0 = .value (some .unsigned8) := + twoParameters_argumentAccess0 +example : twoParameters.argumentAccess 1 = .home 1 := twoParameters_argumentAccess1 +example : twoParameters.argumentAccess 1 = .home 1 := by simp only [cil_code] + +section ArgumentSteps + +variable (pc : Nat) (args : List CIL.Safety.Value) (frame : CIL.Safety.Frame) + (memory : CIL.Safety.Memory) (value : CIL.Safety.Value) (stack : List CIL.Safety.Value) + +open CIL.Safety in +-- A store to the homed argument steps as the store to its home slot, by the declared fact. +example : step twoParameters (.starg 1) pc args frame (value :: stack) memory = (do + let some home := frame.locals[1]? | .error .invalidState + let (home, memory) ← storeLocal memory home value + return FrameAction.next (pc + 1) stack { frame with locals := frame.locals.set 1 home } + memory) := by + simp only [reduceArgumentStore] + rfl + +open CIL.Safety in +-- A store to an argument without a home faults. +example : step twoParameters (.starg 0) pc args frame (value :: stack) memory = + .error (.unsupported "Store to an argument without a home") := by + simp only [reduceArgumentStore] + +open CIL.Safety in +-- The homed argument's address is its home slot's. +example : step twoParameters (.ldarga 1) pc args frame stack memory = (do + let some home := frame.locals[1]? | .error .invalidState + return FrameAction.next (pc + 1) ((← localAddress memory 1 home) :: stack) frame + memory) := by + simp only [reduceArgumentAddress] + rfl + +open CIL.Safety in +-- An argument without a home has no address. +example : step twoParameters (.ldarga 0) pc args frame stack memory = + .error (.unsupported "Address of an argument without a home") := by + simp only [reduceArgumentAddress] + +open CIL.Safety in +-- Under a home, each argument instruction is the local instruction of the home slot. +example : step twoParameters (.starg 1) pc args frame (value :: stack) memory = + step twoParameters (.stloc 1) pc args frame (value :: stack) memory := + step_starg_home_stloc _ _ _ _ _ _ _ _ _ twoParameters_argumentAccess1 + +open CIL.Safety in +example : step twoParameters (.ldarg 1) pc args frame stack memory = + step twoParameters (.ldloc 1) pc args frame stack memory := + step_ldarg_home_ldloc _ _ _ _ _ _ _ _ twoParameters_argumentAccess1 + +open CIL.Safety in +example : step twoParameters (.ldarga 1) pc args frame stack memory = + step twoParameters (.ldloca 1) pc args frame stack memory := + step_ldarga_home_ldloca _ _ _ _ _ _ _ _ twoParameters_argumentAccess1 + +end ArgumentSteps + +/-- +info: 'Tests.ResolutionFacts.twoParameters_argumentAccess0' depends on axioms: +[propext, Quot.sound] +-/ +#guard_msgs (whitespace := lax) in +#print axioms twoParameters_argumentAccess0 + +/-- +info: 'Tests.ResolutionFacts.twoParameters_argumentAccess1' depends on axioms: +[propext, Quot.sound] +-/ +#guard_msgs (whitespace := lax) in +#print axioms twoParameters_argumentAccess1 + +/-- A value-type instance method's signature: a short and a byte-backed enum. -/ +def instanceSignature : DeclaredSignature := + { receiver := some .reference, + parameters := [.atom runtimeScope "System.Int16", + .enum "Examples.Flags" (.atom runtimeScope "System.Byte")] } + +/-- An instance method: `this` is stored to, so it has the home; its parameters follow. -/ +def instanceMethod : Method := + { code := [.ret], locals := [], parameterHomes := [(0, .reference)], returnsValue := false, + declared := instanceSignature } + +/-- The return fact the facts command also reads. -/ +theorem instanceMethod_returnsValue : instanceMethod.returnsValue = false := rfl +#print axioms instanceMethod_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem instanceMethod_declared : instanceMethod.declared = instanceSignature := rfl +#print axioms instanceMethod_declared + +cil_resolution_facts instanceMethod + +-- Parameter i of an instance method is argument i + 1. +example : instanceMethod.argumentAccess 0 = .home 0 := instanceMethod_argumentAccess0 +example : instanceMethod.argumentAccess 1 = .value (some .signed16) := + instanceMethod_argumentAccess1 +example : instanceMethod.argumentAccess 2 = .value (some .unsigned8) := + instanceMethod_argumentAccess2 + +/-- An unhomed `this` is read as it arrives. -/ +def unhomedThis : Method := + { code := [.ret], locals := [], returnsValue := false, + declared := { receiver := some .object, parameters := [.atom runtimeScope "System.Char"] } } + +/-- The return fact the facts command also reads. -/ +theorem unhomedThis_returnsValue : unhomedThis.returnsValue = false := rfl +#print axioms unhomedThis_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem unhomedThis_declared : unhomedThis.declared = + { receiver := some .object, parameters := [.atom runtimeScope "System.Char"] } := rfl +#print axioms unhomedThis_declared + +cil_resolution_facts unhomedThis + +example : unhomedThis.argumentAccess 0 = .value none := unhomedThis_argumentAccess0 +example : unhomedThis.argumentAccess 1 = .value (some .unsigned16) := unhomedThis_argumentAccess1 + +/-- Forty parameters, so the facts read three blocks of parameter types, with homes before the + first block boundary, on both sides of it, at the last index of a block and at the end. -/ +def wide : Method := + { code := [.ret], locals := [.i32 0, .i32 0], + localKinds := [.word32, .word32], returnsValue := false, + parameterHomes := [(3, .word32), (15, .word32), (16, .word32), (17, .word32), (31, .word32), + (39, .narrow .unsigned16)], + declared := { parameters := (List.replicate 39 (.atom runtimeScope "System.Int32")) ++ + [.atom runtimeScope "System.Char"] } } + +/-- The return fact the facts command also reads. -/ +theorem wide_returnsValue : wide.returnsValue = false := rfl +#print axioms wide_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem wide_declared : wide.declared = { parameters := + (List.replicate 39 (.atom runtimeScope "System.Int32")) ++ + [.atom runtimeScope "System.Char"] } := rfl +#print axioms wide_declared + +cil_resolution_facts wide + +-- Each fact agrees with the definition, decided by evaluation. +example : wide.argumentAccess 0 = .value none := wide_argumentAccess0 +example : wide.argumentAccess 3 = .home 2 := wide_argumentAccess3 +example : wide.argumentAccess 4 = .value none := wide_argumentAccess4 +example : wide.argumentAccess 15 = .home 3 := wide_argumentAccess15 +example : wide.argumentAccess 16 = .home 4 := wide_argumentAccess16 +example : wide.argumentAccess 17 = .home 5 := wide_argumentAccess17 +example : wide.argumentAccess 18 = .value none := wide_argumentAccess18 +example : wide.argumentAccess 32 = .value none := wide_argumentAccess32 +example : wide.argumentAccess 39 = .home 7 := wide_argumentAccess39 +example : ∀ index, index < 40 → wide.argumentAccess index = + if index ∈ [3, 15, 16, 17, 31, 39] then .home (2 + [3, 15, 16, 17, 31, 39].idxOf index) + else .value none := by decide + +/-- A method whose homes do not ascend; a fact would otherwise depend on their order. -/ +def unordered : Method := + { code := [.ret], locals := [], parameterHomes := [(1, .word32), (0, .word32)], + returnsValue := false, declared := { parameters := [.atom runtimeScope "System.Int32", + .atom runtimeScope "System.Int32"] } } + +/-- The return fact the facts command also reads. -/ +theorem unordered_returnsValue : unordered.returnsValue = false := rfl +#print axioms unordered_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem unordered_declared : unordered.declared = { parameters := + [.atom runtimeScope "System.Int32", .atom runtimeScope "System.Int32"] } := rfl +#print axioms unordered_declared + +/-- +error: cil_resolution_facts: the parameter homes of Tests.ResolutionFacts.unordered +do not ascend +-/ +#guard_msgs (whitespace := lax) in +cil_resolution_facts unordered + +-- The simplifier reads a literal body's argument access without generated facts. +example : twoParameters.argumentAccess 0 = .value (some .unsigned8) := by simp only [cil_step] +example : wide.argumentAccess 39 = .home 7 := by simp only [cil_step] + +/-- A method whose homes are not known. -/ +def symbolicHomes (homes : List (Nat × LocalKind)) : Method := + { code := [], locals := [], parameterHomes := homes, returnsValue := false, + declared := twoParameterSignature } + +example : (symbolicHomes []).argumentAccess 1 = .value none := by simp only [cil_step] + +-- A symbolic home list leaves the access for a caller's fact. +example (homes : List (Nat × LocalKind)) (access : ArgumentAccess) + (known : (symbolicHomes homes).argumentAccess 0 = access) : + (symbolicHomes homes).argumentAccess 0 = access := by + fail_if_success simp only [cil_step] + exact known + +open CIL.Safety in +-- A store whose access stays unknown steps to `storeArgument`, which a later fact decides. +example (homes : List (Nat × LocalKind)) (pc : Nat) (args : List CIL.Safety.Value) + (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) (value : CIL.Safety.Value) + (rest : List CIL.Safety.Value) : + step (symbolicHomes homes) (.starg 0) pc args frame (value :: rest) memory = (do + let (frame, memory) ← storeArgument (symbolicHomes homes) 0 value frame memory + return FrameAction.next (pc + 1) rest frame memory) := by + simp only [reduceArgumentStore] + +/-- Two methods that differ only in their locals' initial values read arguments alike. -/ +example : ({ twoParameters with locals := [.i32 7] } : Method).argumentAccess = + twoParameters.argumentAccess := Method.argumentAccess_congr rfl rfl rfl rfl + +/-- The raw view of `twoParameters`: a byte and an int argument, one int local, and a body that + stores to the int argument and loads the byte. -/ +def twoParametersRaw : RawArguments := + { arguments := [.field .unsigned8, .field .signed32], locals := [.field .signed32], + code := [.other, .store 1, .load 0, .other] } + +/-- The argument check accepts `twoParameters` against its raw view. -/ +theorem twoParameters_homesValid : + ParameterHomesValid #[] twoParametersRaw [true, true, true, true] twoParameters := by decide +#print axioms twoParameters_homesValid + +-- The home slot the check expects is the slot `argumentAccess` reads. +example : twoParameters.argumentAccess 1 = .home (1 + 0) := + argumentAccess_of_homed twoParameters_homesValid (by decide) + +/-- A static method without parameters. A fact for argument 0 would state a read the method + never makes, so the facts command declares none. -/ +def noParameters : Method := + { code := [.ret], locals := [], returnsValue := false, declared := {} } + +/-- The return fact the facts command also reads. -/ +theorem noParameters_returnsValue : noParameters.returnsValue = false := rfl +#print axioms noParameters_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem noParameters_declared : noParameters.declared = {} := rfl +#print axioms noParameters_declared + +cil_resolution_facts noParameters + +/-- error: Unknown identifier `noParameters_argumentAccess0` -/ +#guard_msgs in #check noParameters_argumentAccess0 +/-- error: Unknown identifier `noParameters_parameterSuffix0` -/ +#guard_msgs in #check noParameters_parameterSuffix0 +/-- error: Unknown identifier `noParameters_sitesAscend` -/ +#guard_msgs in #check noParameters_sitesAscend + +/-- The descriptor of the static site with index `k`, for the `k`th static field of `Fixture`. -/ +def siteDescriptor (k : Nat) : StaticDescriptor := + let field : Extraction.FieldRef := + { owner := .atom "Fixture" "Fixture", index := k, + type := .atom Extraction.runtimeScope "System.Byte", name := name!"Field" } + { identity := k, field, bytes := [BitVec.ofNat 8 k] } + +/-- A method with eighteen static sites at the even pcs, so its facts cross a suffix block. -/ +def staticReads : Method := + { code := [.ret], locals := [], returnsValue := false, declared := {}, + staticSites := [(0, siteDescriptor 0), (2, siteDescriptor 1), (4, siteDescriptor 2), + (6, siteDescriptor 3), (8, siteDescriptor 4), (10, siteDescriptor 5), + (12, siteDescriptor 6), (14, siteDescriptor 7), (16, siteDescriptor 8), + (18, siteDescriptor 9), (20, siteDescriptor 10), (22, siteDescriptor 11), + (24, siteDescriptor 12), (26, siteDescriptor 13), (28, siteDescriptor 14), + (30, siteDescriptor 15), (32, siteDescriptor 16), (34, siteDescriptor 17)] } + +/-- The return fact the facts command also reads. -/ +theorem staticReads_returnsValue : staticReads.returnsValue = false := rfl +#print axioms staticReads_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem staticReads_declared : staticReads.declared = {} := rfl +#print axioms staticReads_declared + +cil_resolution_facts staticReads + +-- Each site's fact names its own descriptor, in the first block and past it. +example : staticReads.staticAt 0 = some (siteDescriptor 0) := staticReads_staticAt0 +example : staticReads.staticAt 30 = some (siteDescriptor 15) := staticReads_staticAt30 +example : staticReads.staticAt 34 = some (siteDescriptor 17) := staticReads_staticAt34 +#print axioms staticReads_staticAt34 +-- A pc without a site has no fact. +/-- error: Unknown identifier `staticReads_staticAt1` -/ +#guard_msgs in #check staticReads_staticAt1 + +/-- Sites out of pc order, which the emitter never writes; the first-match lookup would hide + the later site at pc 2. -/ +def unorderedSites : Method := + { code := [.ret], locals := [], returnsValue := false, declared := {}, + staticSites := [(2, siteDescriptor 0), (0, siteDescriptor 1), (2, siteDescriptor 2)] } + +/-- The return fact the facts command also reads. -/ +theorem unorderedSites_returnsValue : unorderedSites.returnsValue = false := rfl +#print axioms unorderedSites_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem unorderedSites_declared : unorderedSites.declared = {} := rfl +#print axioms unorderedSites_declared + +/-- +error: cil_resolution_facts: the static sites of Tests.ResolutionFacts.unorderedSites do not ascend +-/ +#guard_msgs in +cil_resolution_facts unorderedSites + +-- One fact per declared argument and no more: argument 1 is the last of `twoParameters`. +/-- error: Unknown identifier `twoParameters_argumentAccess2` -/ +#guard_msgs in #check twoParameters_argumentAccess2 + +/-- A home keyed past the one declared parameter, which the argument check would refuse. The + facts cover the declared argument alone, so a stray home cannot add a fact for an argument + the signature does not have. -/ +def strayHome : Method := + { code := [.ret], locals := [], parameterHomes := [(3, .word32)], returnsValue := false, + declared := { parameters := [.atom runtimeScope "System.Int16"] } } + +/-- The return fact the facts command also reads. -/ +theorem strayHome_returnsValue : strayHome.returnsValue = false := rfl +#print axioms strayHome_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem strayHome_declared : strayHome.declared = + { parameters := [.atom runtimeScope "System.Int16"] } := rfl +#print axioms strayHome_declared + +cil_resolution_facts strayHome + +example : strayHome.argumentAccess 0 = .value (some .signed16) := strayHome_argumentAccess0 +/-- error: Unknown identifier `strayHome_argumentAccess3` -/ +#guard_msgs in #check strayHome_argumentAccess3 + +/-- Two homes for one argument. A fact would pick one of the two slots by search order, so the + facts command refuses the method as it refuses descending homes. -/ +def duplicateHomes : Method := + { code := [.ret], locals := [], parameterHomes := [(0, .word32), (0, .word32)], + returnsValue := false, declared := { parameters := [.atom runtimeScope "System.Int32"] } } + +/-- The return fact the facts command also reads. -/ +theorem duplicateHomes_returnsValue : duplicateHomes.returnsValue = false := rfl +#print axioms duplicateHomes_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem duplicateHomes_declared : duplicateHomes.declared = + { parameters := [.atom runtimeScope "System.Int32"] } := rfl +#print axioms duplicateHomes_declared + +/-- +error: cil_resolution_facts: the parameter homes of Tests.ResolutionFacts.duplicateHomes +do not ascend +-/ +#guard_msgs (whitespace := lax) in +cil_resolution_facts duplicateHomes + +/-- An array and a pointer parameter. Neither type is a narrowing candidate, so their facts + read each argument as it arrives without consulting the layouts. -/ +def referenceParameters : Method := + { code := [.ret], locals := [], returnsValue := false, + declared := { parameters := [.array (.atom runtimeScope "System.Byte"), + .pointer (.atom runtimeScope "System.Int16")] } } + +/-- The return fact the facts command also reads. -/ +theorem referenceParameters_returnsValue : referenceParameters.returnsValue = false := rfl +#print axioms referenceParameters_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem referenceParameters_declared : referenceParameters.declared = + { parameters := [.array (.atom runtimeScope "System.Byte"), + .pointer (.atom runtimeScope "System.Int16")] } := rfl +#print axioms referenceParameters_declared + +cil_resolution_facts referenceParameters + +example : referenceParameters.argumentAccess 0 = .value none := + referenceParameters_argumentAccess0 +example : referenceParameters.argumentAccess 1 = .value none := + referenceParameters_argumentAccess1 + +/-- A symbolic argument index leaves the access for a caller's fact: the simproc decides only + literal indices, so it cannot rewrite one argument's access into another's. -/ +example (index : Nat) (access : ArgumentAccess) + (known : twoParameters.argumentAccess index = access) : + twoParameters.argumentAccess index = access := by + fail_if_success simp only [cil_step] + exact known + +/-- Twenty parameters and seventeen homes, so the home suffix crosses a 16-entry block. The + facts past the boundary must read the second home block, not the first. -/ +def manyHomes : Method := + { code := [.ret], locals := [.i32 0], localKinds := [.word32], returnsValue := false, + parameterHomes := (List.range 17).map (·, .word32), + declared := { parameters := List.replicate 20 (.atom runtimeScope "System.Int32") } } + +/-- The return fact the facts command also reads. -/ +theorem manyHomes_returnsValue : manyHomes.returnsValue = false := rfl +#print axioms manyHomes_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem manyHomes_declared : manyHomes.declared = + { parameters := List.replicate 20 (.atom runtimeScope "System.Int32") } := rfl +#print axioms manyHomes_declared + +cil_resolution_facts manyHomes + +example : manyHomes.argumentAccess 15 = .home 16 := manyHomes_argumentAccess15 +example : manyHomes.argumentAccess 16 = .home 17 := manyHomes_argumentAccess16 +example : manyHomes.argumentAccess 17 = .value none := manyHomes_argumentAccess17 +example : manyHomes.argumentAccess 19 = .value none := manyHomes_argumentAccess19 +example : manyHomes_homeSuffix16 = [(16, .word32)] := rfl + +/-! ## Array element shapes -/ + +/-- The `int32` type as the extractor writes it. -/ +def int32Type : TypeExpr := .atom runtimeScope "System.Int32" + +/-- A function pointer type of the managed convention taking and returning an `int32`. -/ +def unaryPointer : TypeExpr := + .functionPointer int32Type (.signatureParameter int32Type .void) 0 false false + +-- A primitive array holds the primitive's storage and a floating-point array has no shape, +-- whatever the method's layouts, without evaluating the type name. +example (body : Method) : body.elementShape int32Type = .value .signed32 := + Method.elementShape_primitive body .int32 +example (body : Method) : body.elementShape (.atom runtimeScope "System.Double") = + .unsupported ElementShape.unsupportedType := + Method.elementShape_primitive body .double + +-- An enum array holds the enum's storage over its underlying integer type, and an enum over a +-- type no enum may have as its underlying type has no shape. +example (body : Method) : + body.elementShape (.enum "Examples.Small" (.atom runtimeScope "System.Int16")) = + .value (.enumeration "Examples.Small" .signed16) := + Method.elementShape_enum body _ .int16 +example (body : Method) : + body.elementShape (.enum "Examples.Truth" (.atom runtimeScope "System.Boolean")) = + .unsupported ElementShape.unsupportedType := + Method.elementShape_enum body _ .boolean + +-- A class array holds references. +example (body : Method) : + body.elementShape (.object "Examples" "Examples.Box") = + .reference (.object "Examples" "Examples.Box") := + Method.elementShape_object body (by decide) + +-- A function-pointer array holds method-table entries of the pointer's managed signature. +example (body : Method) : body.elementShape unaryPointer = .method ⟨int32Type, [int32Type]⟩ := + rfl + +/-- A by-ref-like struct, which no heap array holds. -/ +def spanLikeLayout : RawLayout := + { identity := "Examples.SpanLike", explicit := false, packing := 0, declaredSize := 0, + byRefLike := true, fields := [{ type := .signed64 }] } + +/-- The checked layout of `pointLayout`: two bytes. -/ +def pointComputed : ComputedLayout := + { layout := { size := 2, fields := [{ offset := 0, width := 1 }, { offset := 1, width := 1 }] }, + alignment := 1 } + +/-- The checked layout of `spanLikeLayout`: eight bytes. -/ +def spanLikeComputed : ComputedLayout := + { layout := { size := 8, fields := [{ offset := 0, width := 8 }] }, alignment := 8 } + +/-- The layouts every array method below shares. -/ +def arraysEnvironment : LayoutEnvironment := + { rawLayouts := [pointLayout, spanLikeLayout], layouts := #[pointComputed, spanLikeComputed] } + +/-- A method whose arrays hold the element types `arraysClosed` names. -/ +def arrays : Method := + { code := [.ret], locals := [], returnsValue := false, environment := arraysEnvironment } + +-- A struct array holds values of the size the checked layout gives, which the same struct in +-- a table of another size changes. +example : arrays.elementShape (.aggregate "Examples.Point" 0) = + .value (.aggregate "Examples.Point" 2) := + Method.elementShape_aggregate (metadata := pointLayout) (layout := pointComputed) rfl rfl rfl rfl +example : ({ arrays with environment := { arraysEnvironment with + layouts := #[{ pointComputed with layout := { pointComputed.layout with size := 6 } }] } } + : Method).elementShape (.aggregate "Examples.Point" 0) = + .value (.aggregate "Examples.Point" 6) := + Method.elementShape_aggregate (metadata := pointLayout) + (layout := { pointComputed with layout := { pointComputed.layout with size := 6 } }) + rfl rfl rfl rfl + +-- A by-ref-like struct array has no shape, even with a checked layout. +example : arrays.elementShape (.aggregate "Examples.SpanLike" 1) = + .unsupported ElementShape.byRefLikeElement := + Method.elementShape_byRefLike (metadata := spanLikeLayout) rfl rfl rfl + +/-- The raw view of `arrays`: one instruction per element opcode, a repeated element type and a + type operand of an opcode that names no element type. -/ +def arraysClosed : RawMethod := + { token := 1, owner := "Examples", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [], locals := [], initLocals := true, maxStack := 3, genericParameters := [], + code := [⟨0, 141, .type int32Type⟩, ⟨5, 163, .type int32Type⟩, + ⟨10, 163, .type (.enum "Examples.Small" (.atom runtimeScope "System.Int16"))⟩, + ⟨15, 164, .type (.aggregate "Examples.Point" 0)⟩, + ⟨20, 143, .type (.aggregate "Examples.SpanLike" 1)⟩, + ⟨25, 141, .type (.object "Examples" "Examples.Box")⟩, + ⟨30, 140, .type (.atom runtimeScope "System.Int64")⟩, + ⟨35, 163, .type unaryPointer⟩, ⟨40, 42, .none⟩] } + +/-- The return fact the facts command also reads. -/ +theorem arrays_returnsValue : arrays.returnsValue = false := rfl +#print axioms arrays_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem arrays_declared : arrays.declared = {} := rfl +#print axioms arrays_declared + +cil_resolution_facts arrays + +example : arrays.elementShape int32Type = .value .signed32 := arrays_elementShape0 +example : arrays.elementShape (.enum "Examples.Small" (.atom runtimeScope "System.Int16")) = + .value (.enumeration "Examples.Small" .signed16) := arrays_elementShape1 +example : arrays.elementShape (.aggregate "Examples.Point" 0) = + .value (.aggregate "Examples.Point" 2) := arrays_elementShape2 +example : arrays.elementShape (.aggregate "Examples.SpanLike" 1) = + .unsupported ElementShape.byRefLikeElement := arrays_elementShape3 +example : arrays.elementShape (.object "Examples" "Examples.Box") = + .reference (.object "Examples" "Examples.Box") := arrays_elementShape4 +example : arrays.elementShape unaryPointer = .method ⟨int32Type, [int32Type]⟩ := + arrays_elementShape5 +example : arrays.elementShape (.aggregate "Examples.Point" 0) = + .value (.aggregate "Examples.Point" 2) := by simp only [cil_code] + +/-- info: 'Tests.ResolutionFacts.arrays_elementShape0' depends on axioms: [propext] -/ +#guard_msgs in #print axioms arrays_elementShape0 +/-- info: 'Tests.ResolutionFacts.arrays_elementShape1' depends on axioms: [propext] -/ +#guard_msgs in #print axioms arrays_elementShape1 +/-- info: 'Tests.ResolutionFacts.arrays_elementShape2' depends on axioms: [propext] -/ +#guard_msgs in #print axioms arrays_elementShape2 +/-- info: 'Tests.ResolutionFacts.arrays_elementShape3' depends on axioms: [propext] -/ +#guard_msgs in #print axioms arrays_elementShape3 +/-- info: 'Tests.ResolutionFacts.arrays_elementShape4' depends on axioms: [propext] -/ +#guard_msgs in #print axioms arrays_elementShape4 +/-- info: 'Tests.ResolutionFacts.arrays_elementShape5' depends on axioms: [propext] -/ +#guard_msgs in #print axioms arrays_elementShape5 + +-- A shape fact steps a typed store of its width: the `int32` shape a four-byte one and a `uint64` +-- shape an eight-byte one, which the `int32` shape cannot step. +open CIL.Safety in +example {memory after : Safety.Memory} {view : ArrayView} {index : W32} {reference : Reference} + {bits : W32} (found : arrayElement memory view index.toNat 4 = .ok reference) + (written : write memory reference (numberBytes bits.toNat 4) 1 = .ok after) : True := by + have := step_stelem_any_of_write (index := .i32 index) arrays_elementShape0 rfl rfl found written + trivial +open CIL.Safety in +example {memory after : Safety.Memory} {view : ArrayView} {index : W32} {reference : Reference} + {bits : W64} (found : arrayElement memory view index.toNat 8 = .ok reference) + (written : write memory reference (numberBytes bits.toNat 8) 1 = .ok after) : True := by + have := step_stelem_any_of_wide_write (index := .i32 index) + (Method.elementShape_primitive arrays .uint64) rfl rfl found written + fail_if_success + have := step_stelem_any_of_wide_write (index := .i32 index) + arrays_elementShape0 rfl rfl found written + trivial + +-- One fact per distinct element type, not per instruction: `int32` is named twice, and the +-- `int64` operand of an opcode that names no element type has no fact. +/-- error: Unknown identifier `arrays_elementShape6` -/ +#guard_msgs in #check arrays_elementShape6 + +-- The layout suffixes belong to the shared environment constant, so a second method of the same +-- environment reads them instead of declaring its own. +example : arraysEnvironment_rawLayoutSuffix0 = [pointLayout, spanLikeLayout] := rfl + +/-- A second method of the shared layouts, named as the extractor names them. -/ +def moreArrays : Method := + { code := [.nop, .ret], locals := [], returnsValue := false, environment := arraysEnvironment } + +/-- The raw view of `moreArrays`: one struct element. -/ +def moreArraysClosed : RawMethod := + { arraysClosed with code := [⟨0, 164, .type (.aggregate "Examples.Point" 0)⟩] } + +/-- The return fact the facts command also reads. -/ +theorem moreArrays_returnsValue : moreArrays.returnsValue = false := rfl +#print axioms moreArrays_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem moreArrays_declared : moreArrays.declared = {} := rfl +#print axioms moreArrays_declared + +cil_resolution_facts moreArrays + +example : moreArrays.elementShape (.aggregate "Examples.Point" 0) = + .value (.aggregate "Examples.Point" 2) := moreArrays_elementShape0 +/-- error: Unknown identifier `moreArrays_rawLayoutSuffix0` -/ +#guard_msgs in #check moreArrays_rawLayoutSuffix0 + +/-- Eighteen one-field structs, so the layout suffixes cross a 16-entry block. -/ +def manyLayoutsEnvironment : LayoutEnvironment := + { rawLayouts := (List.range 18).map fun index => + { identity := s!"Examples.Cell{index}", explicit := false, packing := 0, + declaredSize := 0, fields := [{ type := .signed32 }] } + layouts := ((List.range 18).map fun index => + { layout := { size := 4 * (index + 1), fields := [] }, alignment := 4 }).toArray } + +/-- A method whose arrays hold the structs on both sides of the block boundary. -/ +def manyLayouts : Method := + { code := [.ret], locals := [], returnsValue := false, environment := manyLayoutsEnvironment } + +/-- The raw view of `manyLayouts`. -/ +def manyLayoutsClosed : RawMethod := + { arraysClosed with code := [⟨0, 141, .type (.aggregate "Examples.Cell15" 15)⟩, + ⟨5, 141, .type (.aggregate "Examples.Cell17" 17)⟩] } + +/-- The return fact the facts command also reads. -/ +theorem manyLayouts_returnsValue : manyLayouts.returnsValue = false := rfl +#print axioms manyLayouts_returnsValue + +/-- The declared signature the facts command also reads. -/ +theorem manyLayouts_declared : manyLayouts.declared = {} := rfl +#print axioms manyLayouts_declared + +cil_resolution_facts manyLayouts + +example : manyLayouts.elementShape (.aggregate "Examples.Cell15" 15) = + .value (.aggregate "Examples.Cell15" 64) := manyLayouts_elementShape0 +example : manyLayouts.elementShape (.aggregate "Examples.Cell17" 17) = + .value (.aggregate "Examples.Cell17" 72) := manyLayouts_elementShape1 + +/-- A method of the shared layouts without a raw view, so it has no generated shape facts. -/ +def literalArrays : Method := { arrays with code := [] } + +-- The simplifier reads a literal body's element shapes without generated facts. +example : literalArrays.elementShape (.aggregate "Examples.Point" 0) = + .value (.aggregate "Examples.Point" 2) := by simp only [cil_step] +example : literalArrays.elementShape (.aggregate "Examples.SpanLike" 1) = + .unsupported ElementShape.byRefLikeElement := by simp only [cil_step] +example : literalArrays.elementShape int32Type = .value .signed32 := by simp only [cil_step] +example : literalArrays.elementShape unaryPointer = + .method ⟨.atom runtimeScope "System.Int32", [.atom runtimeScope "System.Int32"]⟩ := by + simp only [cil_step] +example : literalArrays.elementShape + (.apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") int32Type) = + .value (.vector "System.Runtime.Intrinsics.Vector128`1" 16) := by + simp only [cil_step] + +-- A struct index the layouts do not hold, or an identity other than the one held there, has +-- no shape. +example : literalArrays.elementShape (.aggregate "Examples.Point" 5) = + .unsupported ElementShape.outsideLayouts := by simp only [cil_step] +example : literalArrays.elementShape (.aggregate "Examples.Other" 0) = + .unsupported ElementShape.outsideLayouts := by simp only [cil_step] + +/-- A method whose raw layouts hold `pointLayout` without a checked layout for it. -/ +def uncomputedArrays : Method := + { code := [.ret], locals := [], returnsValue := false, + environment := { rawLayouts := [pointLayout], layouts := #[] } } + +-- A struct the raw layouts hold under its identity, but that has no checked layout, has no shape. +example : uncomputedArrays.elementShape (.aggregate "Examples.Point" 0) = + .unsupported ElementShape.outsideLayouts := by simp only [cil_step] + +/-- A managed function pointer taking a struct at a layout index that no raw layout holds. -/ +def unboundPointer : TypeExpr := + .functionPointer .void (.signatureParameter (.aggregate "Examples.Missing" 7) .void) 0 false + false + +-- A function pointer whose struct parameter no layout binds has no shape, as it has no storage. +example : literalArrays.elementShape unboundPointer = + .unsupported ElementShape.outsideLayouts := by simp only [cil_step] + +/-- A method over layouts that are not known. -/ +def symbolicLayouts (environment : LayoutEnvironment) : Method := + { code := [], locals := [], returnsValue := false, environment } + +-- A symbolic element type or layout table leaves the shape for a caller's fact. +example (type : TypeExpr) (shape : ElementShape) + (known : literalArrays.elementShape type = shape) : + literalArrays.elementShape type = shape := by + fail_if_success simp only [cil_step] + exact known +example (environment : LayoutEnvironment) (shape : ElementShape) + (known : (symbolicLayouts environment).elementShape (.aggregate "Examples.Point" 0) = shape) : + (symbolicLayouts environment).elementShape (.aggregate "Examples.Point" 0) = shape := by + fail_if_success simp only [cil_step] + exact known + +-- A call the runtime table does not model gets no model fact, and the simplifier reads a literal +-- body's lookup without one. +/-- A static method of no parameters that no runtime model stands for. -/ +private def unmodelledMethod : MethodRef := + { owner := .atom "Tests" "Models", name := name!"Unmodelled", hasThis := false, + genericArity := 0, returnType := .void, parameters := [] } +/-- A body whose only call is unmodelled. -/ +def unmodelledCaller : CIL.Method := + { locals := [], returnsValue := false, code := [.call unmodelledMethod {}, .ret] } +cil_instruction_equations unmodelledCaller +cil_runtime_models unmodelledCaller +/-- error: Unknown identifier `unmodelledCaller_model0` -/ +#guard_msgs in #check unmodelledCaller_model0 +example : + runtimeModel ({ locals := [], returnsValue := false, code := [] } : CIL.Method).environment + .call unmodelledMethod = none := by + simp only [CIL.Safety.reduceRuntimeModel] + +/-- A decided comparison, an unresolved forwarding scope, and an unmodelled getter. -/ +def comparedTypes : CIL.Method := + let word := TypeExpr.atom runtimeScope "System.UInt64" + let token := CIL.Instruction.ldtoken (.type word) + let getter := CIL.Instruction.call getTypeFromHandle.method {} + let test := CIL.Instruction.call (runtimeTypeComparison true).method {} + { locals := [], returnsValue := false, code := [ + token, getter, token, getter, test, + .ldtoken (.type (.atom "Forwarder" "System.UInt64")), getter, token, getter, test, + token, .call unmodelledMethod {}, token, getter, test] } + +cil_instruction_equations comparedTypes +cil_runtime_models comparedTypes + +/-- The two equal operands give one checked identity for consumers of the comparison. -/ +theorem wordComparison : + sameRuntimeType (.atom runtimeScope "System.UInt64") (.atom runtimeScope "System.UInt64") = + some true := comparedTypes_typeIdentity4 + +#print axioms wordComparison + +/-- error: Unknown identifier `comparedTypes_typeIdentity9` -/ +#guard_msgs in #check comparedTypes_typeIdentity9 +/-- error: Unknown identifier `comparedTypes_typeIdentity14` -/ +#guard_msgs in #check comparedTypes_typeIdentity14 + +end Tests.ResolutionFacts diff --git a/Tests/RetainedNumericLocals.lean b/Tests/RetainedNumericLocals.lean index 8290716f6..10cdfe30b 100644 --- a/Tests/RetainedNumericLocals.lean +++ b/Tests/RetainedNumericLocals.lean @@ -1,5 +1,6 @@ import CIL.Embedding.Tactic import CIL.Safety.Embedding.Outcomes +import Tests.Support /-! # Retaining dead numeric declarations during embedding @@ -24,22 +25,23 @@ def expected : Method := { locals := [.unmodeled, .nullRef, .unmodeled] localKinds := [.word32, .reference, .word64] returnsValue := true - code := [.branch 3, .unsupported "dead", .unsupported "dead", - .local 1, .pop, .call 1 0, .ret] } + code := [.br 3, .unadmitted 0, .unadmitted 0, + .ldloc 1, .pop, .call (Tests.callee 0) {}, .ret], links := [(5, .direct 1)] } /-- A separately specified body moves the live block past extra unreachable instructions. -/ def actual : Method := { locals := [.unmodeled, .nullRef, .unmodeled] localKinds := [.word32, .reference, .word64] returnsValue := true - code := [.branch 4, .const32 9, .pop, .nop, .local 1, .pop, .call 1 0, .ret] } + code := [.br 4, .ldc_i4 9, .pop, .nop, .ldloc 1, .pop, .call (Tests.callee 0) {}, + .ret], links := [(6, .direct 1)] } /-- The child allocates its own numeric home after the parent's retained dead homes. -/ def child : Method := { locals := [.i64 7] localKinds := [.word64] returnsValue := true - code := [.local 0, .ret] } + code := [.ldloc 0, .ret] } /-- Retained numeric declarations need ordinary method embedding, even when interspersed. -/ theorem embeds : MethodEmbeds [actual, child] 0 expected := by cil_embed @@ -71,8 +73,8 @@ def removed : Method := { locals := [.nullRef, .unmodeled] localKinds := [.reference, .word64] returnsValue := true - code := [.branch 3, .unsupported "dead", .unsupported "dead", - .local 0, .pop, .call 1 0, .ret] } + code := [.br 3, .unadmitted 0, .unadmitted 0, + .ldloc 0, .pop, .call (Tests.callee 0) {}, .ret], links := [(5, .direct 1)] } /-- Deleting a dead numeric local changes later allocation identities despite equal scalars. -/ theorem removed_allocations : @@ -93,7 +95,7 @@ def thrower : Method := { locals := [.unmodeled] localKinds := [.word64] returnsValue := true - code := [.throw "System.InvalidOperationException"] } + code := [.throw .invalidOperation] } /-- The same parent embedding also transports calls to a throwing child. -/ theorem throwing_embeds : MethodEmbeds [actual, thrower] 0 expected := by cil_embed @@ -101,13 +103,13 @@ theorem throwing_embeds : MethodEmbeds [actual, thrower] 0 expected := by cil_em /-- The expected invocation raises from the allocated child frame. -/ theorem expected_raises : - Safety.Raises [expected, thrower] 0 [] empty "System.InvalidOperationException" := - ⟨10, ⟨1, 0, .thrown "System.InvalidOperationException"⟩, rfl, rfl⟩ + Safety.Raises [expected, thrower] 0 [] empty .invalidOperation := + ⟨10, ⟨1, 0, .thrown .invalidOperation⟩, rfl, rfl⟩ #print axioms expected_raises /-- Named exceptions transfer with the retained numeric allocation layout. -/ theorem actual_raises : - Safety.Raises [actual, thrower] 0 [] empty "System.InvalidOperationException" := + Safety.Raises [actual, thrower] 0 [] empty .invalidOperation := throwing_embeds.raises expected_raises #print axioms actual_raises diff --git a/Tests/ReturnMetadataBinding.lean b/Tests/ReturnMetadataBinding.lean new file mode 100644 index 000000000..90e358eb2 --- /dev/null +++ b/Tests/ReturnMetadataBinding.lean @@ -0,0 +1,146 @@ +import Tests.MetadataBuilder + +/-! +# Metadata signature restrictions + +Method definitions reject modified returns and modified parameters even when the claimed type is an +admissible opaque atom; an `in` reference's own modifier is the one a parameter may carry. +-/ + +namespace Tests.ReturnMetadataBinding + +open CIL CIL.Extraction Tests.MetadataBuilder + +/-- A static method with an encoded return and parameter signature and a runtime modifier + reference in `modifierNamespace`. -/ +private def signatureWorld (result : List Nat) (parameters : List (List Nat)) + (modifierNamespace modifierName : String) : Step Nat := do + let runtime ← string "System.Runtime" + let system ← string "System" + let objectName ← string "Object" + let namespaceName ← string modifierNamespace + let modifier ← string modifierName + let program ← string "Program" + let methodName ← string "Run" + let publicKeyToken ← blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtime, 0, 0] + modify fun builder => { builder with corlib := 1 } + let _ ← row Table.typeRef [6, objectName, system] + let _ ← row Table.typeRef [6, modifier, namespaceName] + let owner ← row Table.typeDef [1, program, 0, 5, 1, 1] + let signature ← blob ([0, parameters.length] ++ result ++ parameters.flatten) + let method ← row Table.methodDef [0, 0, 0x96, methodName, signature, 1] + link linkDeclaring method 0 owner + pure method + +/-- Bind a method's declared return and parameters to its actual signature bytes. -/ +private def acceptsSignature (result : List Nat) (claimed : TypeExpr) + (parameters : List (List Nat)) (claimedParameters : List TypeExpr) + (modifierNamespace modifierName : String) : Bool := + (do + let fixture := build (signatureWorld result parameters modifierNamespace modifierName) + (bytesOf "T") + "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + let method : RawMethod := + { token := fixture.2, owner := "T::Program", hasThis := false, + explicitThis := false, callingConvention := 0, attributes := 0x96, + implementation := 0, returnType := claimed, parameters := claimedParameters, locals := [], + initLocals := false, maxStack := 8, genericParameters := [], code := [] } + let env := MetadataEnv.ofSlices [← fixture.1.index] (platform := objectPlatform) + methodIdentityValid env method [] []).isOk + +/-- Bind a parameterless method's declared return to its actual signature bytes. -/ +private def acceptsReturn (result : List Nat) (claimed : TypeExpr) + (modifierName : String := "OtherModifier") : Bool := + acceptsSignature result claimed [] [] "System.Runtime.CompilerServices" modifierName + +/-- Bind an int32 method's one declared parameter to its actual signature bytes. -/ +private def acceptsParameter (parameter : List Nat) (claimed : TypeExpr) + (modifierNamespace : String := "System.Runtime.CompilerServices") + (modifierName : String := "OtherModifier") : Bool := + acceptsSignature [8] (.atom runtimeScope "System.Int32") [parameter] [claimed] modifierNamespace + modifierName + +/-- An opaque atom cannot conceal an unsupported required return modifier. -/ +theorem rejects_opaque_modified_return : + acceptsReturn [0x1F, 9, 8] + (.atom runtimeScope "System.Int32 modreq(System.Runtime.CompilerServices.OtherModifier)") = + false := by decide +kernel + +#print axioms rejects_opaque_modified_return + +/-- Ordinary integer and the exact required init-void return remain admitted. -/ +theorem accepts_declared_returns : + acceptsReturn [8] (.atom runtimeScope "System.Int32") = true ∧ + acceptsReturn [0x1F, 9, 1] .void "IsExternalInit" = true := by decide +kernel + +#print axioms accepts_declared_returns + +/-- Optional, nested and non-void init modifiers remain unsupported return signatures. -/ +theorem rejects_other_modified_returns : + acceptsReturn [0x20, 9, 8] + (.atom runtimeScope "System.Int32 modopt(System.Runtime.CompilerServices.OtherModifier)") = + false ∧ + acceptsReturn [0x10, 0x1F, 9, 8] + (.byref (.atom runtimeScope + "System.Int32 modreq(System.Runtime.CompilerServices.OtherModifier)")) = false ∧ + acceptsReturn [0x1F, 9, 8] + (.atom runtimeScope "System.Int32 modreq(System.Runtime.CompilerServices.IsExternalInit)") + "IsExternalInit" = false ∧ + acceptsReturn [0x20, 9, 1] + (.atom runtimeScope "System.Void modopt(System.Runtime.CompilerServices.IsExternalInit)") + "IsExternalInit" = false := by decide +kernel + +#print axioms rejects_other_modified_returns + +/-- A modified parameter is refused: the argument read narrows by the declared type, which an + opaque atom would hide. -/ +theorem rejects_modified_parameters : + acceptsParameter [0x1F, 9, 5] + (.atom runtimeScope "System.Byte modreq(System.Runtime.CompilerServices.OtherModifier)") = + false ∧ + acceptsParameter [0x20, 9, 5] + (.atom runtimeScope "System.Byte modopt(System.Runtime.CompilerServices.OtherModifier)") = + false ∧ + acceptsParameter [0x10, 0x1F, 9, 8] + (.byref (.atom runtimeScope + "System.Int32 modreq(System.Runtime.CompilerServices.OtherModifier)")) = false ∧ + acceptsParameter [0x1F, 9, 0x10, 8] + (.atom runtimeScope + "System.Int32& modreq(System.Runtime.CompilerServices.OtherModifier)") = false := by + decide +kernel + +#print axioms rejects_modified_parameters + +/-- A plain parameter and an `in` reference, whose `InAttribute` modifier it loses at run time, + remain admitted. -/ +theorem accepts_declared_parameters : + acceptsParameter [5] (.atom runtimeScope "System.Byte") = true ∧ + acceptsParameter [0x1F, 9, 0x10, 8] (.byref (.atom runtimeScope "System.Int32")) + "System.Runtime.InteropServices" "InAttribute" = true := by decide +kernel + +#print axioms accepts_declared_parameters + +/-- Metadata for a value__ field whose signature supplies the enum's underlying primitive. -/ +private def enumWorld (code : Nat) : MetadataSlice := + (build (do + let name ← string "Number" + let owner ← row Table.typeDef [1, name, 0, 0, 1, 1] + let fieldName ← string "value__" + let signature ← blob [0x06, code] + let field ← row Table.field [0x606, fieldName, signature] + link linkDeclaring field 0 owner) + (bytesOf "T") "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null").1 + +/-- Enum storage is read through the actual metadata index and field signature decoder. -/ +private def acceptsEnum (code : Nat) : Bool := + (do enumUnderlying (← (enumWorld code).index) 0x02000001).isOk + +/-- Enum metadata admits the eight integer widths and rejects other scalar primitives. -/ +theorem enum_underlying_codes : + ([4, 5, 6, 7, 8, 9, 10, 11].all acceptsEnum) = true ∧ + ([2, 3, 12, 13, 0x18, 0x19].any acceptsEnum) = false := by decide +kernel + +#print axioms enum_underlying_codes + +end Tests.ReturnMetadataBinding diff --git a/Tests/ReturnOpcode.lean b/Tests/ReturnOpcode.lean new file mode 100644 index 000000000..b303c2b31 --- /dev/null +++ b/Tests/ReturnOpcode.lean @@ -0,0 +1,179 @@ +import CIL.Safety.Execution +import CIL.Safety.ValueSteps +import CIL.Resolution + +/-! +# Declared return narrowing + +The return instruction obtains its narrowing from the method's declared metadata. A body in the +emitted form, with a struct return and a non-empty layout environment, still evaluates by `rfl` +under the default recursion limit, as generated boundary claims are proved. +-/ + +namespace Tests.ReturnOpcode + +open CIL CIL.Safety + +/-- A byte-returning method whose stack value exceeds the declared storage width. -/ +private def byteBody : Method := + { code := [.ldc_i4 300, .ret], locals := [], returnsValue := true, + declared := { returnType := .atom Extraction.runtimeScope "System.Byte" } } + +/-- A return narrows the result according to the method declaration. -/ +theorem byte_return (pc : Nat) (args : List Safety.Value) (frame : Frame) + (memory : Safety.Memory) : + Safety.step byteBody .ret pc args frame [.scalar (.i32 300)] memory = + .ok (.returned [.scalar (.i32 44)] memory) := by + rfl + +#print axioms byte_return + +/-- Handwritten bodies simplify return storage without generated projection facts. -/ +theorem byte_return_simplified (frame : Frame) (memory : Safety.Memory) : + Safety.step byteBody .ret 0 [] frame [.scalar (.i32 300)] memory = + .ok (.returned [.scalar (.i32 44)] memory) := by + simp only [step_ret_eq, cil_step, show byteBody.returnsValue = true from rfl] + rfl +#print axioms byte_return_simplified + +/-- A one-instruction method with a declared primitive return type. -/ +private def primitiveBody (type : PrimitiveType) : Method := + { code := [.ret], locals := [], returnsValue := true, + declared := { returnType := type.typeExpr } } + +/-- Each primitive's return storage is applied by the checked dispatcher. -/ +theorem primitive_return (type : PrimitiveType) (bits : W32) (frame : Frame) + (memory : Safety.Memory) : + Safety.step (primitiveBody type) .ret 0 [] frame [.scalar (.i32 bits)] + memory = .ok (.returned + [.scalar (.i32 (match type.narrow with | some kind => kind.extend bits | none => bits))] + memory) := by + cases type <;> rfl +#print axioms primitive_return + +/-- A declared narrow return requires exactly one 32-bit integer. -/ +theorem invalid_narrow_returns (frame : Frame) (memory : Safety.Memory) : + Safety.step byteBody .ret 0 [] frame [] memory = .error .invalidState ∧ + Safety.step byteBody .ret 0 [] frame [.scalar (.i64 300)] memory = .error .invalidState ∧ + Safety.step byteBody .ret 0 [] frame [.scalar (.i32 1), .scalar (.i32 2)] memory = + .error .invalidState := by + exact ⟨rfl, rfl, rfl⟩ +#print axioms invalid_narrow_returns + +/-- Void methods ignore declared narrow metadata and require an empty return stack. -/ +theorem void_return (frame : Frame) (memory : Safety.Memory) : + Safety.step { byteBody with returnsValue := false } .ret 0 [] frame [] memory = + .ok (.returned [] memory) ∧ + Safety.step { byteBody with returnsValue := false } .ret 0 [] frame + [.scalar (.i32 1)] memory = .error .invalidState := by + exact ⟨rfl, rfl⟩ +#print axioms void_return + +/-- Unspecified vector bits are rejected before return storage conversion. -/ +theorem unspecified_return (frame : Frame) (memory : Safety.Memory) : + Safety.step byteBody .ret 0 [] frame [.scalar (.v128Low64 0)] memory = + .error (.unsupported "Instruction observes unspecified vector bits") := rfl +#print axioms unspecified_return + +/-- The reference interpreter receives narrowing explicitly. -/ +theorem reference_return : + CIL.step .ret true (some .unsigned8) 0 [.i32 300] (fun _ => none) .scalar = + some (.returned [.i32 44] (fun _ => none)) := rfl +#print axioms reference_return + +/-- Both standalone interpreters reject a narrow return paired with void arity. -/ +theorem inconsistent_void_return (memory : Safety.Memory) : + CIL.step .ret false (some .unsigned8) 0 [] (fun _ => none) .scalar = none ∧ + returnFrom (some .unsigned8) false [.scalar (.i32 1)] memory = .error .invalidState := + ⟨rfl, rfl⟩ +#print axioms inconsistent_void_return + +/-- Returning a reference still checks that its allocation exists. -/ +theorem invalid_reference_return (frame : Frame) : + Safety.step (primitiveBody .int32) .ret 0 [] frame [.reference (.address ⟨0, 0⟩)] + Safety.Memory.empty = .error (.memory .missingAllocation (.address ⟨0, 0⟩) 0) := rfl +#print axioms invalid_reference_return + +/-- A byte declaration cannot satisfy the full-width bitwise helper's return premise. -/ +theorem byte_not_full_width : + byteBody.declared.returnType ≠ .atom Extraction.runtimeScope "System.Int32" := by + decide +#print axioms byte_not_full_width + +/-- A struct of two 32-bit fields, recorded with the generic identity an extraction writes; + the identity is as long as a closed generic struct's, whose comparison once exhausted the + elaborator's recursion limit. -/ +private def pairLayouts : List Extraction.RawLayout := [ + { identity := Extraction.integerPairIdentity "System.Int32", + explicit := false, auto := true, packing := -1, declaredSize := -1, + fields := [{ type := .signed32, offset := none }, { type := .signed32, offset := none }] }] + +/-- The layout environment an extraction records for `pairLayouts`. -/ +private def pairEnvironment : LayoutEnvironment := + { rawLayouts := pairLayouts, layouts := #[⟨⟨8, [⟨0, 4, false⟩, ⟨4, 4, false⟩]⟩, 4⟩] } + +private def mathDivRem (element : String) : Extraction.MethodRef := + let word := Extraction.TypeExpr.atom Extraction.runtimeScope element + { owner := .atom Extraction.runtimeScope "System.Math", name := name!"DivRem", + genericArity := 0, hasThis := false, parameters := [word, word], + returnType := .aggregate (Extraction.integerPairIdentity element) 0 } + +/-- `Math.DivRem(int, int)` in the emitted form: a declared struct return over a non-empty + environment, with the identity written out as a literal as an extraction writes it. -/ +private def pairDivRem : Method := + { profile := .scalar, returnsValue := true, locals := [], environment := pairEnvironment, + declared := { + parameters := [.atom Extraction.runtimeScope "System.Int32", + .atom Extraction.runtimeScope "System.Int32"], + returnType := .aggregate "System.Runtime, Version=11.0.0.0, Culture=neutral, \ + PublicKeyToken=b03f5f7f11d50a3a::System.ValueTuple`2" 0 } + code := [.ldarg 0, .ldarg 1, .call (mathDivRem "System.Int32"), .ret] } + +set_option maxRecDepth 4096 in +/-- A closed tuple-returning method produces the expected bytes through its checked call. -/ +theorem pair_return_evaluates : + (invoke [pairDivRem] 8 0 [.scalar (.i32 7), .scalar (.i32 2)] Memory.empty).map Prod.snd = + .ok [.scalar (.aggregate [3, 0, 0, 0, 1, 0, 0, 0])] := by + rfl +#print axioms pair_return_evaluates + +set_option maxRecDepth 4096 in +/-- The existential form of a generated struct claim elaborates by the same evaluation. -/ +theorem pair_return_exists : + ∃ bytes, (invoke [pairDivRem] 8 0 [.scalar (.i32 7), .scalar (.i32 2)] + Memory.empty).map Prod.snd = .ok [.scalar (.aggregate bytes)] ∧ bytes.length = 8 := + ⟨_, rfl, by decide⟩ +#print axioms pair_return_exists + +private def remainderField : Extraction.FieldRef := + { owner := .aggregate (Extraction.integerPairIdentity "System.SByte") 0, index := 1, name := name!"Item2", + type := .atom Extraction.runtimeScope "System.SByte" } + +/-- `Math.DivRem(sbyte, sbyte).Remainder` over a two-byte struct layout, whose + narrowing return compares only its runtime scope and primitive name. -/ +private def sbyteRemainder : Method := + { pairDivRem with + environment := { + rawLayouts := [ + { identity := Extraction.integerPairIdentity "System.SByte", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .signed8 }, { type := .signed8 }] }] + layouts := #[⟨⟨2, [⟨0, 1, false⟩, ⟨1, 1, false⟩]⟩, 1⟩] } + declared := { + parameters := [.atom Extraction.runtimeScope "System.SByte", + .atom Extraction.runtimeScope "System.SByte"], + returnType := .atom Extraction.runtimeScope "System.SByte" } + code := [.ldarg 0, .ldarg 1, + .call (mathDivRem "System.SByte"), .ldfld remainderField, .ret] } + +set_option maxRecDepth 4096 in +/-- A narrowing return over a non-empty environment is proved by evaluation, and the remainder + of -7 by 2 comes back sign-extended. -/ +theorem sbyte_return_evaluates : + (invoke [sbyteRemainder] 8 0 [.scalar (.i32 4294967289), .scalar (.i32 2)] + Memory.empty).map Prod.snd = .ok [.scalar (.i32 4294967295)] := by + rfl +#print axioms sbyte_return_evaluates + +end Tests.ReturnOpcode diff --git a/Tests/RuntimeBits.cs b/Tests/RuntimeBits.cs index 34d0684ca..c88beebd3 100644 --- a/Tests/RuntimeBits.cs +++ b/Tests/RuntimeBits.cs @@ -2,6 +2,13 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include EmptyReturnVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -63,6 +70,42 @@ object Value(Type type, ulong bits) _ => throw new InvalidOperationException("Unexpected value: " + value) }; +// Each group proves its comparisons at the default limits and preserves the remaining conjunction. +static string BoundaryProof(List claims) +{ + const int comparisonsPerGroup = 16; + const string conjunction = " \u2227\n"; + var groups = claims.SkipLast(1).Chunk(comparisonsPerGroup).ToArray(); + var declarations = groups.Select((group, index) => + { + var witnesses = string.Join(", ", Enumerable.Repeat("rfl", group.Length).Append("rest")); + var tuple = $"\u27e8{witnesses}\u27e9"; + return $$""" + /-- These boundary comparisons hold for any proved continuation. -/ + private theorem boundary{{index}} {tail : Prop} (rest : tail) : + {{string.Join(conjunction, group.Append("tail"))}} := {{tuple}} + #print axioms boundary{{index}} + """; + }); + var names = groups.Select((_, index) => $"boundary{index}").Append("lastBoundary"); + var witness = $"{string.Join(" (", names)}{new string(')', groups.Length)}"; + return $""" + import Extracted + import CIL.Safety.Execution + namespace ExampleProof + {string.Join("\n\n", declarations)} + /-- The final input gives the runtime result. -/ + private theorem lastBoundary : {claims[^1]} := rfl + #print axioms lastBoundary + /-- Each compiled call returns the value obtained from the runtime. -/ + theorem correctness : + {string.Join(conjunction, claims)} := {witness} + #print axioms correctness + end ExampleProof + + """; +} + // Every supported overload is extracted from compiled C#, then compared with .NET. foreach (var method in runtime.GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(m => !m.Name.Contains("RoundTrip") && !m.Name.StartsWith("Wrong", StringComparison.Ordinal))) @@ -106,18 +149,7 @@ object Value(Type type, ulong bits) """); } var proof = Path.Combine(output, "BoundaryProof.lean"); - File.WriteAllText(proof, $""" - import Extracted - import CIL.Safety.Execution - set_option maxRecDepth 8192 - namespace ExampleProof - theorem correctness : - {string.Join(" ∧\n", claims)} := by - exact ⟨{string.Join(", ", claims.Select(_ => "rfl"))}⟩ - #print axioms correctness - end ExampleProof - - """); + File.WriteAllText(proof, BoundaryProof(claims)); var selector = $"Bits::{method.Name}({string.Join(",", parameters.Select(p => p.ParameterType.FullName))})"; var comparisons = claims.Count; queue.Add(new(selector, proof, output), () => @@ -132,32 +164,20 @@ end ExampleProof var contract = At("Examples/Proofs/RotateRoundTrip.lean"); verification.Verify(assembly, "Examples", "Bits::RoundTrip(ulong)", contract, Path.Combine(root, "RoundTrip")); var wrongOutput = Path.Combine(root, "WrongRoundTrip"); -var failed = false; -try -{ - verification.Verify(assembly, "Examples", "Bits::WrongRoundTrip(ulong)", contract, wrongOutput); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - failed = true; -} -if (!failed || File.Exists(Path.Combine(wrongOutput, "verified.json"))) - throw new InvalidOperationException("Incorrect rotation inverse accepted"); +verification.VerifyEmptyReturn(assembly, "Bits::RoundTrip(ulong)", + contract, assembly, "Bits::WrongRoundTrip(ulong)", wrongOutput, + new("W64", "[.scalar (.i64 input)]", + ".scalar (.i64 input)", 8, + "exact ExampleProof.correctness input", "1")); var logarithm = At("Examples/Proofs/IntegerLog2.lean"); verification.Verify(assembly, "Examples", "Bits::Log64(ulong)", logarithm, Path.Combine(root, "UniversalLog")); var wrongLog = Path.Combine(root, "WrongLog"); -failed = false; -try -{ - verification.Verify(assembly, "Examples", "Bits::WrongLog64(ulong)", logarithm, wrongLog); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - failed = true; -} -if (!failed || File.Exists(Path.Combine(wrongLog, "verified.json"))) - throw new InvalidOperationException("Incorrect integer logarithm accepted"); +verification.VerifyEmptyReturn(assembly, "Bits::Log64(ulong)", + logarithm, assembly, "Bits::WrongLog64(ulong)", wrongLog, + new("W64", "[.scalar (.i64 input)]", + ".scalar (.i32 (BitVec.ofNat 32 input.toNat.log2))", 8, + "exact ExampleProof.correctness input", "0")); // Preserve exact runtime identity and signature checks for the new allowlist. foreach (var mutation in new[] { "scope", "version", "instance", "return", "argument", "generic", "unsupported", diff --git a/Tests/RuntimeExceptions.cs b/Tests/RuntimeExceptions.cs index 6aab403b7..1cb404689 100644 --- a/Tests/RuntimeExceptions.cs +++ b/Tests/RuntimeExceptions.cs @@ -1,6 +1,12 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ExceptionMembers.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -9,7 +15,8 @@ string root = At("artifacts/RuntimeExceptions"); Directory.CreateDirectory(root); File.WriteAllText(Path.Combine(root, "RuntimeFaults.csproj"), """ -net10.0true +net11.0 +true """); File.WriteAllText(Path.Combine(root, "RuntimeFaults.cs"), """ namespace RuntimeFaults; @@ -44,18 +51,18 @@ public static class Cases foreach ((string name, string selector, string values, string fault, int pc, object[] inputs) in cases) { - string exception = fault == "overflow" ? "System.OverflowException" : "System.DivideByZeroException"; - string wrong = fault == "overflow" ? "System.DivideByZeroException" : "System.OverflowException"; - bool observed = false; + Type expected = fault == "overflow" ? typeof(OverflowException) : typeof(DivideByZeroException); + string wrong = fault == "overflow" ? "divideByZero" : "overflow"; + string? exception = null; try { runtimeType.GetMethod(name)!.Invoke(null, inputs); } - catch (TargetInvocationException error) when (error.InnerException?.GetType().FullName == exception) + catch (TargetInvocationException error) when (error.InnerException?.GetType() == expected) { - observed = true; + exception = ExceptionMember(error.InnerException); } - if (!observed) throw new InvalidOperationException($"{name} did not raise {exception}"); + if (exception is null) throw new InvalidOperationException($"{name} did not raise {expected.FullName}"); string proof = Path.Combine(root, name + ".lean"); File.WriteAllText(proof, $$""" @@ -63,7 +70,7 @@ import Extracted import CIL.Safety.RuntimeExceptions open CIL.Safety namespace ExampleProof -def contract (exception : String) : OutcomeContract Unit where +def contract (exception : CIL.ExceptionType) : OutcomeContract Unit where args _ := {{values}} pre _ _ := True footprint _ := [] @@ -74,8 +81,8 @@ def contract (exception : String) : OutcomeContract Unit where theorem invoked (memory : Memory) : invoke Extracted.program 4 0 {{values}} memory = .error ⟨0, {{pc}}, .arithmetic .{{fault}}⟩ := rfl theorem correctness : - (contract "{{exception}}").HoldsCLR Extracted.program 0 ∧ - ¬ (contract "{{wrong}}").HoldsCLR Extracted.program 0 := by + (contract .{{exception}}).HoldsCLR Extracted.program 0 ∧ + ¬ (contract .{{wrong}}).HoldsCLR Extracted.program 0 := by constructor · intro _ memory _ exact ⟨4, ⟨0, {{pc}}, .arithmetic .{{fault}}⟩, invoked memory, rfl⟩ diff --git a/Tests/RuntimeExceptions.lean b/Tests/RuntimeExceptions.lean index ec577add8..16c6c457e 100644 --- a/Tests/RuntimeExceptions.lean +++ b/Tests/RuntimeExceptions.lean @@ -1,5 +1,9 @@ +import CIL.ExceptionType +import CIL.Extraction.Folds import CIL.Safety.RuntimeExceptions +/-! These tests check how CLR exception contracts classify observed arithmetic faults. -/ + namespace Tests.RuntimeExceptions open CIL.Safety @@ -8,15 +12,15 @@ inductive Case where | add | sub | mul | convert | divide | remainder | signedDivide | signedRemainder deriving DecidableEq -def operation : Case → CIL.Op - | .add => .checkedArithmetic .add true - | .sub => .checkedArithmetic .sub true - | .mul => .checkedArithmetic .mul true - | .convert => .checkedConvert 8 false false - | .divide => .divide false false - | .remainder => .divide false true - | .signedDivide => .divide true false - | .signedRemainder => .divide true true +def operation : Case → CIL.Instruction + | .add => .add_ovf + | .sub => .sub_ovf + | .mul => .mul_ovf + | .convert => .conv_ovf .u1 + | .divide => .div_un + | .remainder => .rem_un + | .signedDivide => .div + | .signedRemainder => .rem def arguments : Case → List CIL.Safety.Value | .add => [.scalar (.i32 2147483647), .scalar (.i32 1)] @@ -27,7 +31,8 @@ def arguments : Case → List CIL.Safety.Value | .signedDivide | .signedRemainder => [.scalar (.i32 2147483648), .scalar (.i32 4294967295)] def method (which : Case) : CIL.Method := { - code := (match which with | .convert => [.arg 0] | _ => [.arg 0, .arg 1]) ++ [operation which, .ret] + code := (match which with | .convert => [.ldarg 0] | _ => [.ldarg 0, .ldarg 1]) ++ + [operation which, .ret] locals := [], returnsValue := true } def instruction : Case → Nat @@ -38,15 +43,15 @@ def arithmeticFault : Case → CIL.ArithmeticFault | .divide | .remainder => .divideByZero | _ => .overflow -def exceptionName : Case → String - | .divide | .remainder => "System.DivideByZeroException" - | _ => "System.OverflowException" +def exceptionName : Case → CIL.ExceptionType + | .divide | .remainder => .divideByZero + | _ => .overflow -def wrongName : Case → String - | .divide | .remainder => "System.OverflowException" - | _ => "System.DivideByZeroException" +def wrongName : Case → CIL.ExceptionType + | .divide | .remainder => .overflow + | _ => .divideByZero -def contract (which : Case) (exception : String) : OutcomeContract Unit where +def contract (which : Case) (exception : CIL.ExceptionType) : OutcomeContract Unit where args _ := arguments which pre _ _ := True footprint _ := [] @@ -72,21 +77,53 @@ theorem wrong_outcomes (which : Case) : ¬ (contract which (wrongName which)).Ho exact RaisesCLR.not_of_invoke (by simp [Settled]) (invoked which empty) (by cases which <;> decide) raised +/-- A contract that promises the observed CLR exception admits no counterexample. -/ +theorem not_refuted_named (which : Case) : + Not ((contract which (exceptionName which)).RefutedCLR [method which] 0) := + fun refuted => refuted.not_holds (named_outcomes which) + +/-- +info: 'Tests.RuntimeExceptions.not_refuted_named' depends on axioms: [propext, Quot.sound] +-/ +#guard_msgs in +#print axioms not_refuted_named + +/-- The observed arithmetic fault satisfies the named exception for every initial memory. -/ +theorem no_violation_named (which : Case) (memory : Memory) : + Not ((contract which (exceptionName which)).ViolatesCLR () memory + (invoke [method which] 4 0 (arguments which) memory)) := by + rw [invoked] + intro different + exact different (by cases which <;> rfl) + +#print axioms no_violation_named + +/-- The same fault refutes a contract that promises the other arithmetic exception. -/ +theorem refuted_wrong (which : Case) : + (contract which (wrongName which)).RefutedCLR [method which] 0 := by + apply OutcomeContract.RefutedCLR.of_fault (input := ()) trivial (by simp) (invoked which empty) + change Not ((ExecutionFault.arithmetic (arithmeticFault which)).clrException = + some (wrongName which)) + cases which <;> decide + +#print axioms refuted_wrong + def zeroDivisorMethod (remainder : Bool) : CIL.Method := { - code := [.arg 0, .arg 1, .divide false remainder, .ret], locals := [], returnsValue := true } + code := [.ldarg 0, .ldarg 1, if remainder then .rem_un else .div_un, .ret], locals := [], + returnsValue := true } def zeroDivisorContract : OutcomeContract CIL.W32 where args value := [.scalar (.i32 value), .scalar (.i32 0)] pre _ _ := True footprint _ := [] post _ _ _ _ := False - raises _ _ := some "System.DivideByZeroException" + raises _ _ := some .divideByZero theorem divide_and_mod_zero (remainder : Bool) : zeroDivisorContract.HoldsCLR [zeroDivisorMethod remainder] 0 := by intro value memory _ - exact ⟨4, ⟨0, 2, .arithmetic .divideByZero⟩, rfl, rfl⟩ + cases remainder <;> exact ⟨4, ⟨0, 2, .arithmetic .divideByZero⟩, rfl, rfl⟩ -theorem unnamed_faults (exception : String) : +theorem unnamed_faults (exception : CIL.ExceptionType) : ExecutionFault.clrException (.arithmetic .invalidOperands) ≠ some exception ∧ ExecutionFault.clrException (.arithmetic .invalidBounds) ≠ some exception ∧ ExecutionFault.clrException .invalidState ≠ some exception ∧ @@ -95,31 +132,86 @@ theorem unnamed_faults (exception : String) : simp [ExecutionFault.clrException] theorem memory_faults_never_named (rule : Fault) (reference : ManagedReference) (width : Nat) - (exception : String) : ExecutionFault.clrException (.memory rule reference width) ≠ some exception := by + (exception : CIL.ExceptionType) : + ExecutionFault.clrException (.memory rule reference width) ≠ some exception := by simp [ExecutionFault.clrException] -theorem unsupported_never_named (exception : String) : - ¬ RaisesCLR [{ code := [.unsupported "operation"], locals := [], returnsValue := false }] 0 [] empty exception := by - apply RaisesCLR.not_of_invoke (fuel := 1) - (fault := ⟨0, 0, .unsupported "Not a memory instruction"⟩) - · simp [Settled] - · rfl - · simp [ExecutionFault.clrException] - -theorem invalid_state_never_named (exception : String) : ¬ RaisesCLR [] 0 [] empty exception := by +/-- A hole's fault names no exception and is not settled, so `RaisesCLR.not_of_invoke` cannot use + it: a run that reaches a hole neither raises nor refutes an expected exception. -/ +theorem unsupported_not_settled : + invoke [{ code := [.unadmitted 0], locals := [], returnsValue := false }] 1 0 [] empty = + .error ⟨0, 0, .unsupported "Opcode not admitted"⟩ ∧ + ExecutionFault.clrException (.unsupported "Opcode not admitted") = none ∧ + ¬ Settled (.error ⟨0, 0, .unsupported "Opcode not admitted"⟩ : + Except LocatedFault (Memory × List Value)) := + ⟨rfl, rfl, by decide⟩ + +theorem invalid_state_never_named (exception : CIL.ExceptionType) : + ¬ RaisesCLR [] 0 [] empty exception := by apply RaisesCLR.not_of_invoke (fuel := 1) (fault := ⟨0, 0, .invalidState⟩) · simp [Settled] · rfl · simp [ExecutionFault.clrException] -theorem thrown_still_named (exception : String) : +theorem thrown_still_named (exception : CIL.ExceptionType) : RaisesCLR [{ code := [.throw exception], locals := [], returnsValue := false }] 0 [] empty exception := RaisesCLR.of_raises ⟨1, ⟨0, 0, .thrown exception⟩, rfl, rfl⟩ #print axioms named_outcomes #print axioms wrong_outcomes #print axioms divide_and_mod_zero -#print axioms unsupported_never_named +#print axioms unsupported_not_settled #print axioms invalid_state_never_named +-- A throw raises its member, and a checked overflow surfaces as the overflow member. +example : Raises [{ code := [.throw .overflow], locals := [], returnsValue := false }] 0 [] + empty .overflow := + ⟨1, ⟨0, 0, .thrown .overflow⟩, rfl, rfl⟩ + +example : (ExecutionFault.arithmetic .overflow).clrException = some .overflow := rfl + +-- Goals show the fault with its member by name. +/-- +trace: ⊢ ExecutionFault.thrown CIL.ExceptionType.overflow ≠ + ExecutionFault.thrown CIL.ExceptionType.format +-/ +#guard_msgs (whitespace := lax) in +example : ExecutionFault.thrown .overflow ≠ ExecutionFault.thrown .format := by + trace_state + decide + +/-- +info: 'CIL.Extraction.exceptionOwner?_eq_some' depends on axioms: [propext] +-/ +#guard_msgs (whitespace := lax) in +#print axioms CIL.Extraction.exceptionOwner?_eq_some + +-- The runtime assembly displays as the scope text extraction compares. +#guard CIL.Extraction.runtimeAssembly.display == CIL.Extraction.runtimeScope + +-- Each member's owner name is its namespace and name as a reader writes them. +#guard CIL.ExceptionType.all.all fun raised => + raised.ownerName == + raised.typeIdentity.typeNamespace.display ++ "." ++ raised.typeIdentity.name.display + +example : CIL.ExceptionType.all.Nodup := by decide + +/-- info: 'CIL.ExceptionType.typeIdentity_injective' does not depend on any axioms -/ +#guard_msgs in +#print axioms CIL.ExceptionType.typeIdentity_injective + +/-- info: 'CIL.ExceptionType.ofTypeIdentity?_typeIdentity' does not depend on any axioms -/ +#guard_msgs in +#print axioms CIL.ExceptionType.ofTypeIdentity?_typeIdentity + +/-- info: 'CIL.ExceptionType.ownerName_spells' does not depend on any axioms -/ +#guard_msgs in +#print axioms CIL.ExceptionType.ownerName_spells + +/-- +info: 'CIL.Extraction.runtimeScope_spells_runtimeAssembly' depends on axioms: [propext, Quot.sound] +-/ +#guard_msgs in +#print axioms CIL.Extraction.runtimeScope_spells_runtimeAssembly + end Tests.RuntimeExceptions diff --git a/Tests/RuntimeModelInventory.cs b/Tests/RuntimeModelInventory.cs index fdad5d772..8028b6d18 100644 --- a/Tests/RuntimeModelInventory.cs +++ b/Tests/RuntimeModelInventory.cs @@ -2,15 +2,23 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include CompilerToolchain.cs +#:include PinnedMetadata.cs using System.Reflection; using System.Text.Json; using Mono.Cecil; +using Mono.Cecil.Cil; if (args is not ([] or ["--update"])) throw new ArgumentException("Usage: RuntimeModelInventory.cs [--update]"); var extractor = Assembly.LoadFrom(ExtractorPath); var flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; -var translate = extractor.GetType("Nethermind.LeanCIL.RuntimeModels", true)!.GetMethod("Translate", flags)!; +var callModel = extractor.GetType("Nethermind.LeanCIL.RuntimeModels", true)!.GetMethod("CallModel", flags)!; +var identityImage = extractor.GetType("Nethermind.LeanCIL.RuntimeModels", true)!.GetMethod("IdentityImage", flags)!; var select = extractor.GetType("Nethermind.LeanCIL.FeatureProfile", true)!.GetMethod("Select", flags)!; // No built-in declares AES, so each architecture's widest built-in is also probed with its AES flag. object Declaring(string name, string flag) @@ -27,24 +35,38 @@ object Declaring(string name, string flag) var profiles = new[] { "scalar", "x64-v4", "arm64-advsimd" }.Select(name => select.Invoke(null, [name])) .Append(Declaring("x64-v4", "aes")).Append(Declaring("arm64-advsimd", "armAes")).ToArray(); var toolchain = RestoreCompilerToolchain(); -using var probe = ModuleDefinition.CreateModule("RuntimeModelInventory", ModuleKind.Dll); var admitted = new SortedDictionary>(StringComparer.Ordinal); var instances = new SortedSet(StringComparer.Ordinal); +var primitiveNames = new[] { "Byte", "SByte", "Int16", "UInt16", "Int32", "UInt32", "Int64", "UInt64", + "IntPtr", "UIntPtr", "Boolean", "Char", "Single", "Double" }; +const int batchSize = 512; +bool checkedMissingContext = false; +int unsupported = 0; +var conventionChecks = new HashSet(StringComparer.Ordinal); +var acceptedConventions = new List(); foreach (var assembly in new[] { "System.Runtime", "System.Memory", "System.Runtime.Intrinsics" }) { using var reference = ModuleDefinition.ReadModule(Path.Combine(toolchain.References, assembly + ".dll")); - var runtime = AssemblyNameReference.Parse( - "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); - var elements = new[] { "Byte", "SByte", "Int16", "UInt16", "Int32", "UInt32", "Int64", "UInt64", - "IntPtr", "UIntPtr", "Boolean", "Char", "Single", "Double" } - .Select(name => new TypeReference("System", name, probe, runtime, true)).ToArray(); - foreach (var type in reference.GetTypes()) - foreach (var definition in type.Methods.Where(method => method.IsPublic && method.GenericParameters.Count <= 2)) + var candidates = from type in reference.GetTypes() + where type.GenericParameters.Count <= 1 + from definition in type.Methods + where definition.IsPublic && definition.GenericParameters.Count <= 2 + from owner in Arguments(type.HasGenericParameters, primitiveNames) + from first in Arguments(definition.HasGenericParameters, primitiveNames) + from second in Arguments(definition.GenericParameters.Count == 2, primitiveNames) + select (type, definition, owner, first, second); + foreach (var batch in candidates.Chunk(batchSize)) { - if (type.GenericParameters.Count > 1) continue; - foreach (var owner in Arguments(type.HasGenericParameters, elements)) - foreach (var first in Arguments(definition.HasGenericParameters, elements)) - foreach (var second in Arguments(definition.GenericParameters.Count == 2, elements)) + using var probe = ModuleDefinition.CreateModule("RuntimeModelInventory", ModuleKind.Dll); + var runtime = AssemblyNameReference.Parse( + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); + probe.AssemblyReferences.Add(runtime); + var carrier = new MethodDefinition("References", Mono.Cecil.MethodAttributes.Static, probe.TypeSystem.Void); + probe.Types[0].Methods.Add(carrier); + var elements = primitiveNames.ToDictionary(name => name, + name => new TypeReference("System", name, probe, runtime, true), StringComparer.Ordinal); + var labels = new List<(string Family, string Definition, string Instance)>(batch.Length); + foreach (var (type, definition, owner, first, second) in batch) { MethodReference method = probe.ImportReference(definition); foreach (var parameter in method.GenericParameters) parameter.Name = "!!" + parameter.Position; @@ -52,32 +74,70 @@ object Declaring(string name, string flag) if (owner is not null) { var closed = new GenericInstanceType(method.DeclaringType); - closed.GenericArguments.Add(owner); + closed.GenericArguments.Add(elements[owner]); method.DeclaringType = closed; } if (first is not null) { var generic = new GenericInstanceMethod(method); - generic.GenericArguments.Add(first); - if (second is not null) generic.GenericArguments.Add(second); + generic.GenericArguments.Add(elements[first]); + if (second is not null) generic.GenericArguments.Add(elements[second]); method = generic; } - foreach (var profile in profiles) + carrier.Body.Instructions.Add(Instruction.Create(OpCodes.Ldtoken, method)); + carrier.Body.Instructions.Add(Instruction.Create(OpCodes.Pop)); + labels.Add((type.FullName, assembly + "::" + definition.FullName, assembly + "::" + method.FullName)); + } + carrier.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); + var probePath = At("artifacts/RuntimeModelInventory/Probe.dll"); + probe.Write(probePath); + MethodReference contextProbe; + using (var pinned = new PinnedMetadata(extractor, probePath)) + { + var instructions = pinned.Module.Types[0].Methods.Single().Body.Instructions; + contextProbe = (MethodReference)instructions[0].Operand; + for (int index = 0; index < labels.Count; index++) + { + var method = (MethodReference)instructions[index * 2].Operand; + var label = labels[index]; + foreach (var profile in profiles) + { + if (!HasPinnedModel(method, profile)) continue; + var probeConvention = (method.DeclaringType.GetElementType().FullName, method.Name) is + ("System.Object", ".ctor") or ("System.Math", "Min") or + ("System.Numerics.BitOperations", "PopCount") or + ("System.Buffers.Binary.BinaryPrimitives", "ReverseEndianness") or + ("System.Runtime.Intrinsics.Vector128", "CreateScalarUnsafe" or "GetElement") or + ("System.Span`1" or "System.ReadOnlySpan`1", "get_Item" or "Slice" or "get_Length" or + "get_IsEmpty" or "GetPinnableReference" or "get_Empty" or "op_Implicit"); + if (probeConvention && conventionChecks.Add(method.Name)) + CheckConventions(method, profile); + if (!admitted.TryGetValue(label.Family, out var definitions)) + admitted.Add(label.Family, definitions = new(StringComparer.Ordinal)); + definitions.Add(label.Definition); + instances.Add(label.Instance); + } + } + } + if (!checkedMissingContext) + { + try { - string? operation; - try { operation = (string?)translate.Invoke(null, [method, profile]); } - catch (TargetInvocationException exception) when (exception.InnerException is - InvalidDataException or NotSupportedException or AssemblyResolutionException) { continue; } - if (operation is null) continue; - if (!admitted.TryGetValue(type.FullName, out var definitions)) - admitted.Add(type.FullName, definitions = new(StringComparer.Ordinal)); - definitions.Add(assembly + "::" + definition.FullName); - instances.Add(assembly + "::" + method.FullName); + HasPinnedModel(contextProbe, profiles[0]); + throw new InvalidOperationException( + "A missing metadata context was recorded as an unsupported method."); } + catch (TargetInvocationException exception) when (exception.InnerException is InvalidDataException + { Message: "Type identity without a pinned module image" }) { checkedMissingContext = true; } } } } var entries = admitted.Values.SelectMany(values => values).Order(StringComparer.Ordinal).ToArray(); +if (conventionChecks.Count != 13 || acceptedConventions.Count != 0) + throw new InvalidOperationException( + $"Checked {conventionChecks.Count} names. Accepted conventions: {string.Join(", ", acceptedConventions)}"); +if (entries.Length == 0 || instances.Count == 0 || unsupported == 0 || !checkedMissingContext) + throw new InvalidOperationException("The runtime inventory must exercise admission and unsupported methods."); var baseline = At("Tests/RuntimeModelInventory.txt"); var output = At("artifacts/RuntimeModelInventory"); Directory.CreateDirectory(output); @@ -96,5 +156,50 @@ object Declaring(string name, string flag) Console.WriteLine($"PASS runtime dispatcher catalog: {entries.Length} definitions, {admitted.Count} API families, " + $"{instances.Count} admitted closed instances in the fixed primitive domain"); -static IEnumerable Arguments(bool generic, TypeReference[] elements) => - generic ? elements : [null]; +bool HasPinnedModel(MethodReference method, object? profile) +{ + identityImage.Invoke(null, [method.DeclaringType]); + try + { + if (callModel.Invoke(null, [method, profile]) is not null) return true; + } + catch (TargetInvocationException exception) when (exception.InnerException is + InvalidDataException or NotSupportedException or AssemblyResolutionException) + { + // The pinned candidate has an unsupported signature or scope. + } + catch (TargetInvocationException exception) + { + throw new InvalidOperationException("Inventory candidate: " + method.FullName, exception); + } + unsupported++; + return false; +} + +void CheckConventions(MethodReference method, object? profile) +{ + var member = method.GetElementMethod(); + var convention = member.CallingConvention; + var hasThis = member.HasThis; + foreach (byte header in new byte[] { 0x40, 0x60, 0x01 }) + { + member.ExplicitThis = (header & 0x40) != 0; + member.HasThis = (header & 0x20) != 0; + member.CallingConvention = (MethodCallingConvention)(header & 0x1f); + if (callModel.Invoke(null, [method, profile]) is not null) + acceptedConventions.Add($"{method.Name} 0x{header:X2}"); + } + // Keep the generic flag while checking EXPLICITTHIS on a genuine generic MethodSpec. + member.CallingConvention = convention; + member.HasThis = hasThis; + if (method is GenericInstanceMethod) + { + member.ExplicitThis = true; + if (callModel.Invoke(null, [method, profile]) is not null) + acceptedConventions.Add($"{method.Name} ExplicitThis"); + } + member.ExplicitThis = false; +} + +static IEnumerable Arguments(bool generic, string[] elements) => + generic ? elements : new string?[] { null }; diff --git a/Tests/RuntimeModelInventory.txt b/Tests/RuntimeModelInventory.txt index 7f5bad451..73cb43480 100644 --- a/Tests/RuntimeModelInventory.txt +++ b/Tests/RuntimeModelInventory.txt @@ -75,13 +75,37 @@ System.Memory::System.Void System.Buffers.Binary.BinaryPrimitives::WriteUInt64Bi System.Memory::System.Void System.Buffers.Binary.BinaryPrimitives::WriteUInt64LittleEndian(System.Span`1,System.UInt64) System.Memory::System.Void System.Buffers.Binary.BinaryPrimitives::WriteUIntPtrBigEndian(System.Span`1,System.UIntPtr) System.Memory::System.Void System.Buffers.Binary.BinaryPrimitives::WriteUIntPtrLittleEndian(System.Span`1,System.UIntPtr) +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Arm.AdvSimd/Arm64::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Arm.AdvSimd::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Arm.Aes/Arm64::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Arm.Aes::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Arm.ArmBase/Arm64::get_IsSupported() System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Vector128::EqualsAll(System.Runtime.Intrinsics.Vector128`1,System.Runtime.Intrinsics.Vector128`1) +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Vector128::get_IsHardwareAccelerated() System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Vector128`1::op_Equality(System.Runtime.Intrinsics.Vector128`1,System.Runtime.Intrinsics.Vector128`1) System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Vector256::EqualsAll(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1) +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Vector256::get_IsHardwareAccelerated() System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Vector256`1::op_Equality(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1) System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Vector512::EqualsAll(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1) System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.Vector512`1::op_Equality(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1) +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Aes/X64::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Aes::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Avx2::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Avx512DQ/VL::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Avx512DQ::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Avx512F/VL::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Avx512F::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Avx512Vbmi/VL::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Avx512Vbmi::get_IsSupported() System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Avx::TestZ(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1) +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Avx::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Bmi1::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Bmi2/X64::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Sse2::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Sse41::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Sse42::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.Ssse3::get_IsSupported() +System.Runtime.Intrinsics::System.Boolean System.Runtime.Intrinsics.X86.X86Base/X64::get_IsSupported() System.Runtime.Intrinsics::System.Int32 System.Runtime.Intrinsics.X86.Avx512DQ::MoveMask(System.Runtime.Intrinsics.Vector256`1) System.Runtime.Intrinsics::System.Int32 System.Runtime.Intrinsics.X86.Avx512DQ::MoveMask(System.Runtime.Intrinsics.Vector512`1) System.Runtime.Intrinsics::System.Int32 System.Runtime.Intrinsics.X86.Avx::MoveMask(System.Runtime.Intrinsics.Vector256`1) @@ -192,6 +216,7 @@ System.Runtime.Intrinsics::T System.Runtime.Intrinsics.Vector128::Sum(System.Run System.Runtime.Intrinsics::T System.Runtime.Intrinsics.Vector256::GetElement(System.Runtime.Intrinsics.Vector256`1,System.Int32) System.Runtime.Intrinsics::T System.Runtime.Intrinsics.Vector256::Sum(System.Runtime.Intrinsics.Vector256`1) System.Runtime::System.Boolean System.Byte::IsPow2(System.Byte) +System.Runtime::System.Boolean System.Environment::get_Is64BitProcess() System.Runtime::System.Boolean System.Int16::IsPow2(System.Int16) System.Runtime::System.Boolean System.Int32::IsPow2(System.Int32) System.Runtime::System.Boolean System.Int64::IsPow2(System.Int64) @@ -254,6 +279,7 @@ System.Runtime::System.Int32 System.Int32::Sign(System.Int32) System.Runtime::System.Int32 System.Int32::TrailingZeroCount(System.Int32) System.Runtime::System.Int32 System.Int64::Sign(System.Int64) System.Runtime::System.Int32 System.IntPtr::Sign(System.IntPtr) +System.Runtime::System.Int32 System.IntPtr::get_Size() System.Runtime::System.Int32 System.Math::Abs(System.Int32) System.Runtime::System.Int32 System.Math::Clamp(System.Int32,System.Int32,System.Int32) System.Runtime::System.Int32 System.Math::DivRem(System.Int32,System.Int32,System.Int32&) @@ -280,6 +306,7 @@ System.Runtime::System.Int32 System.ReadOnlySpan`1::get_Length() System.Runtime::System.Int32 System.Runtime.CompilerServices.Unsafe::SizeOf() System.Runtime::System.Int32 System.SByte::Sign(System.SByte) System.Runtime::System.Int32 System.Span`1::get_Length() +System.Runtime::System.Int32 System.UIntPtr::get_Size() System.Runtime::System.Int64 System.Int64::Abs(System.Int64) System.Runtime::System.Int64 System.Int64::Clamp(System.Int64,System.Int64,System.Int64) System.Runtime::System.Int64 System.Int64::LeadingZeroCount(System.Int64) @@ -418,6 +445,7 @@ System.Runtime::System.Void System.ArgumentOutOfRangeException::ThrowIfNotEqual( System.Runtime::System.Void System.ArgumentOutOfRangeException::ThrowIfZero(T,System.String) System.Runtime::System.Void System.Array::Fill(T[],T) System.Runtime::System.Void System.Array::Fill(T[],T,System.Int32,System.Int32) +System.Runtime::System.Void System.Object::.ctor() System.Runtime::System.Void System.ReadOnlySpan`1::.ctor(System.Void*,System.Int32) System.Runtime::System.Void System.ReadOnlySpan`1::.ctor(T&) System.Runtime::System.Void System.ReadOnlySpan`1::.ctor(T[]) diff --git a/Tests/RuntimeModels.lean b/Tests/RuntimeModels.lean new file mode 100644 index 000000000..f16539828 --- /dev/null +++ b/Tests/RuntimeModels.lean @@ -0,0 +1,1077 @@ +import CIL.Safety.Execution +import CIL.Safety.StepComposition +import CIL.Safety.BoundaryStepRenaming +import CIL.Extraction.CallAgreement +import CIL.Extraction.DecodedDefinition +import CIL.Extraction.UnsafeCalls +import CIL.Extraction.KernelEquation +import CIL.ProfileEquivalence +import Tests.Support +import Tests.LoweredCallRefusal + +/-! +# Runtime model tests + +A typed static span runs in `memoryInstruction`, so a runtime model's memory operation needs no +static field metadata. Each runtime model runs as the instruction the extractor emits for its +call: `runModel` on a concrete stack equals `step` on that instruction, and the model observes +as many operands as the instruction does. A model table resolves typed method references, and +the agreement check ties each call site's emission to the model its reference stands for. +-/ + +namespace Tests.RuntimeModels +open CIL.Safety + +/-- One readable 16-byte immutable static field at allocation 0, aligned to 8, whose bytes + are all 7. -/ +private def staticMemory : Memory := { + allocations := fun id => + if id = 0 then some ⟨.immutableStatic, ⟨16, 8, []⟩, true, [16]⟩ else none + cells := fun _ _ => ⟨7, true⟩ + views := [⟨0, 0, 16, true, false⟩] + nextIdentity := 1 } + +/-- A typed span over the whole field has one element per four bytes. -/ +example : memoryInstruction (.createSpan 4) [.reference (.address ⟨0, 0⟩)] staticMemory = + .ok (staticMemory, [.span (.address ⟨0, 0⟩) 4]) := rfl + +/-- A method with no code, on the scalar profile. -/ +private def body : CIL.Method := { code := [], locals := [], returnsValue := false } + +private def frame : Frame := { activation := 0, locals := [], owned := [] } + +/-- One writable 16-byte heap allocation at 0, aligned to 8, whose bytes are all 7. -/ +private def heapMemory : Memory := { + allocations := fun id => + if id = 0 then some ⟨.managedHeap, ⟨16, 8, []⟩, true, []⟩ else none + cells := fun _ _ => ⟨7, true⟩ + views := [⟨0, 0, 16, true, true⟩] + nextIdentity := 1 } + +/-- The field's start and its fourth byte, as references. -/ +private def start : Value := .reference (.address ⟨0, 0⟩) +private def fourth : Value := .reference (.address ⟨0, 4⟩) + +-- Each model against the instruction emitted for its call today, on a stack it accepts. +example : runModel body (.intrinsic (.scalar (.rotateLeft 32)) 2) 3 frame + [.scalar (.i32 1), .scalar (.i32 0x80000000)] staticMemory = + intrinsicCall body.profile (.scalar (.rotateLeft 32)) 2 3 frame + [.scalar (.i32 1), .scalar (.i32 0x80000000)] staticMemory := rfl +example : runModel body (.memory (.createSpan 4)) 3 frame [start] staticMemory = + modelStep (some (.memory (.createSpan 4))) body 3 frame [start] staticMemory := rfl +example : runModel body (.feature .avx2) 3 frame [start] staticMemory = + featureCall body.profile .avx2 3 frame [start] staticMemory := rfl +example : runModel body (.constant 8) 3 frame [start] staticMemory = + step body (.ldc_i4 8) 3 [] frame [start] staticMemory := rfl +example : runModel body (.referenceCompare .same) 3 frame [fourth, start] staticMemory = + step body .ceq 3 [] frame [fourth, start] staticMemory := rfl +example : runModel body (.referenceCompare .less) 3 frame [fourth, start] staticMemory = + step body .clt_un 3 [] frame [fourth, start] staticMemory := rfl +example : runModel body (.referenceCompare .greater) 3 frame [fourth, start] staticMemory = + step body .cgt_un 3 [] frame [fourth, start] staticMemory := rfl +example : runModel body .referenceSubtract 3 frame [.scalar (.i32 4), fourth] staticMemory = + step body .sub 3 [] frame [.scalar (.i32 4), fourth] staticMemory := rfl +example : runModel body (.typedTransfer (.scalar 4 false) false) 3 frame [start] staticMemory = + step body (.ldind .u4) 3 [] frame [start] staticMemory := rfl +example : runModel body .skipInit 3 frame [start] staticMemory = + .ok (.next 4 [] frame staticMemory) := rfl +example : runModel body .objectConstructor 3 frame [.object .null] staticMemory = + step body .pop 3 [] frame [.object .null] staticMemory := rfl + +-- Every typed transfer family runs as its typed load or store. +example : runModel body (.typedTransfer (.scalar 4 false) true) 3 frame + [.scalar (.i32 9), start] heapMemory = + step body (.stind .i4) 3 [] frame [.scalar (.i32 9), start] heapMemory := rfl +example : runModel body (.typedTransfer (.snapshot (.aggregate 16)) false) 3 frame [start] + heapMemory = + .ok (.next 4 [.scalar (.aggregate (List.replicate 16 7))] frame heapMemory) := rfl +example : runModel body (.typedTransfer (.snapshot (.aggregate 16)) true) 3 frame + [.scalar (.aggregate (List.replicate 16 1)), start] heapMemory = + .ok (.next 4 [] frame ((write heapMemory ⟨0, 0⟩ (List.replicate 16 1) 1).toOption.getD + Memory.empty)) := rfl +example : runModel body (.typedTransfer (.snapshot .vector128) false) 3 frame [start] + heapMemory = + modelStep (some (.memory .load128)) body 3 frame [start] heapMemory := rfl +example : runModel body (.typedTransfer (.snapshot .vector128) true) 3 frame + [.scalar (.v128 5), start] heapMemory = + modelStep (some (.memory .store128)) body 3 frame [.scalar (.v128 5), start] heapMemory := rfl + +-- The equalities above compare successes, not two copies of one fault. +example : (runModel body (.intrinsic (.scalar (.rotateLeft 32)) 2) 3 frame + [.scalar (.i32 1), .scalar (.i32 0x80000000)] staticMemory).toBool := rfl +example : (runModel body (.memory (.createSpan 4)) 3 frame [start] staticMemory).toBool := rfl +example : (runModel body (.referenceCompare .same) 3 frame [fourth, start] + staticMemory).toBool := rfl +example : (runModel body (.referenceCompare .less) 3 frame [fourth, start] + staticMemory).toBool := rfl +example : (runModel body (.referenceCompare .greater) 3 frame [fourth, start] + staticMemory).toBool := rfl +example : (runModel body .referenceSubtract 3 frame [.scalar (.i32 4), fourth] + staticMemory).toBool := rfl +example : (runModel body (.typedTransfer (.scalar 4 false) true) 3 frame + [.scalar (.i32 9), start] heapMemory).toBool := rfl +example : (runModel body (.typedTransfer (.snapshot (.aggregate 16)) true) 3 frame + [.scalar (.aggregate (List.replicate 16 1)), start] heapMemory).toBool := rfl +example : (runModel body (.typedTransfer (.snapshot .vector128) false) 3 frame [start] + heapMemory).toBool := rfl +example : (runModel body (.typedTransfer (.snapshot .vector128) true) 3 frame + [.scalar (.v128 5), start] heapMemory).toBool := rfl +example : (runModel body .skipInit 3 frame [start] staticMemory).toBool := rfl + +-- A model faults where its instruction faults. +example : runModel body .objectConstructor 3 frame [] staticMemory = + step body .pop 3 [] frame [] staticMemory := rfl +example : runModel body .skipInit 3 frame [.scalar (.i32 0)] staticMemory = + .error (.unsupported "Not a memory instruction") := rfl + +-- The model, unlike the opcode, takes only references: scalar operands are refused. +example : runModel body (.referenceCompare .same) 3 frame [.scalar (.i32 1), .scalar (.i32 1)] + staticMemory = .error .invalidState := rfl +example : runModel body .referenceSubtract 3 frame [.scalar (.i32 4), .scalar (.i32 9)] + staticMemory = .error .invalidState := rfl + +-- Spans outside immutable static data, or with a native length, are refused. +example : memoryInstruction (.createSpan 4) [start] heapMemory = + .error (.unsupported "Typed span outside a whole extracted static field") := rfl +example : memoryInstruction .spanCreate [.scalar (.i32 4), start] heapMemory = + .error (.unsupported "Span pointer constructor outside extracted static data") := rfl +example : modelStep (some (.memory .spanCreate)) body 3 frame [.scalar (.i64 4), start] staticMemory = + .error .invalidState := rfl + +-- Guarded by its observer count, a model rejects unspecified bits as its instruction does. +example : observedCall (CIL.RuntimeModel.referenceCompare .same).observerCount + [.scalar (.v128Low64 0), start] + (fun _ => runModel body (.referenceCompare .same) 3 frame + [.scalar (.v128Low64 0), start] staticMemory) = + step body .ceq 3 [] frame [.scalar (.v128Low64 0), start] staticMemory := rfl + +-- The same checked memory model accepts a specified null and rejects unspecified vector bits. +example : modelStep (some (.memory .isNullRef)) body 3 frame [.reference .null] staticMemory = + .ok (.next 4 [.scalar (.i32 1)] frame staticMemory) := by + rw [modelStep_memory] + rfl +example : modelStep (some (.memory .isNullRef)) body 3 frame [.scalar (.v128Low64 0)] + staticMemory = + .error (.unsupported "Instruction observes unspecified vector bits") := by + rw [modelStep_memory] + rfl + +-- Each model observes the operands its emitted instruction observes. +example : (CIL.RuntimeModel.intrinsic (.scalar (.rotateLeft 32)) 2).observerCount = 0 := rfl +example : (CIL.RuntimeModel.memory (.createSpan 4)).observerCount = + CIL.MemoryOp.argumentCount (.createSpan 4) := rfl +example : (CIL.RuntimeModel.feature .avx2).observerCount = 0 := rfl +example : (CIL.RuntimeModel.constant 8).observerCount = observerCount (.ldc_i4 8) := rfl +example : (CIL.RuntimeModel.referenceCompare .less).observerCount = observerCount .clt_un := rfl +example : CIL.RuntimeModel.referenceSubtract.observerCount = observerCount .sub := rfl +example : (CIL.RuntimeModel.typedTransfer (.scalar 4 false) true).observerCount = + observerCount (.stind .i4) := rfl +example : CIL.RuntimeModel.skipInit.observerCount = 1 := rfl +example : CIL.RuntimeModel.objectConstructor.observerCount = observerCount .pop := rfl + +private def element : CIL.ArrayElement := .signed32 + +-- The memory operations only a call reaches, then the opcode meanings, blocks among them. +example : [CIL.MemoryOp.observeView .spanEmpty, .binaryRead 4 false false false, + .binaryWrite 4 false false, .spanReinterpret 1 4 false, .spanCopy 1 false, .spanFill 1 false, + .arrayFill element false, .bigMul64 false, .divRemOut 32, .vector128Unsafe false false, + .asRef, .add 1 true, .byteOffset, + .marshalRead (.scalar 4 false) false, .marshalWrite (.scalar 4 false) false, + .pointerTransfer (.scalar 4 false) 4 false, .bitcastByteBool, + .bitcastValue .vector128 .vector128, .spanCreate, .spanReference, .createSpan 4, + .spanItem 4, .spanLength, .spanEmpty 4 false, .spanFromReference 4 false, + .spanSingleReference 4 false, .spanSlice false, .spanReadOnly, + .arraySpan element false .whole, .nullRef, .isNullRef, .throwIfNull, + .zeroGuard .negative 32 true, .comparisonGuard .greaterThan 32 true].all + CIL.MemoryOp.callReached := rfl +example : [CIL.MemoryOp.copyBlock 1, .initBlock 1, .load128, .load256, .load512, .store128, + .store256, .store512].any + CIL.MemoryOp.callReached = false := rfl + + +namespace Table +open CIL CIL.Extraction + +private def runtimeType (name : String) : TypeExpr := .atom runtimeScope name +private def word : TypeExpr := runtimeType "System.UInt64" + +/-- `Math.BigMul(UInt64, UInt64, out UInt64)` as a closed call site states it. -/ +private def bigMul : MethodRef := + { owner := runtimeType "System.Math", name := name!"BigMul", hasThis := false, + genericArity := 0, returnType := word, parameters := [word, word, .byref word] } + +private def long : TypeExpr := runtimeType "System.Int64" + +/-- `Math.DivRem(Int64, Int64, out Int64)`, whose model the table below does not hold. -/ +private def divRem : MethodRef := + { bigMul with + name := name!"DivRem", returnType := long, parameters := [long, long, .byref long] } + +/-- The signed overload `Math.BigMul(Int64, Int64, out Int64)`, which the table does not model. -/ +private def signedBigMul : MethodRef := + { bigMul with returnType := long, parameters := [long, long, .byref long] } + +/-- `Span.get_Length`. -/ +private def spanLength (element : TypeExpr) : MethodRef := + { owner := .apply (.constructor runtimeScope "System.Span`1") element, + name := name!"get_Length", hasThis := true, genericArity := 0, + returnType := runtimeType "System.Int32", parameters := [] } + +/-- `get_Length` of every span instance, an instance method of no parameters. -/ +private def spanLengthEntry : ModelEntry := + .single .call (.constructor runtimeScope "System.Span`1") name!"get_Length" + (.declaration (· == { convention := 0x20, returnType := runtimeType "System.Int32" })) + (fun _ method => + if method.hasThis && method.parameters.isEmpty then some (.memory .spanLength) else none) + +/-- BigMul by its exact reference, and `get_Length` of every span instance. -/ +private def entries : List ModelEntry := + [ModelEntry.exact .call bigMul (bigMul.signature 0) (.memory (.bigMul64 false)), spanLengthEntry] + +/-- The table of `entries`, accounting for the wide arithmetic and span length emissions. -/ +private def catalog : ModelCatalog where + table := model_table% entries + +/-- A call or `newobj` of `method`. -/ +private def site (method : MethodRef) (opcode : Nat := 40) + (declared := method.signature (if method.hasThis then 0x20 else 0)) : RawInstruction := + ⟨0, opcode, .call { + method, declared, target := .external "", typeArguments := [], + parameterCount := method.parameters.length, returnsValue := true }⟩ + +-- The literal table is the balanced table of its entries. +example : catalog.table = ModelTable.ofEntries entries := rfl + +-- A modelled call keeps its method, including when a memory operation implements its semantics. +example : catalog.siteAgrees {} {} (site bigMul) (.newobj bigMul) = false := by + decide +kernel +example : catalog.siteAgrees {} {} (site bigMul) (.call divRem) = false := by + decide +kernel +-- A call keeps its operand; `ManagedCallsValid` decides whether a model or a link enters it. +example : catalog.siteAgrees {} {} (site bigMul) (.call bigMul) = true := by decide +kernel +example : catalog.siteAgrees {} {} (site bigMul) (.call signedBigMul) = false := by + decide +kernel + +-- An entry reached by name, opcode and owner still refuses a reference its resolve rejects. +example : catalog.table.find {} .call signedBigMul = none := by decide +kernel +example : catalog.siteAgrees {} {} (site signedBigMul) (.call signedBigMul) = false := by + decide +kernel +example : catalog.table.find {} .call { spanLength word with hasThis := false } = none := by + decide +kernel + +/-- A missing raw claim rejects every live emission. -/ +theorem unclaimedLiveRefused (models : ModelCatalog) (environment : LayoutEnvironment) + (profile : FeatureProfile) (raw : RawInstruction) (emitted : CIL.Instruction) + (called : methodTokenCall raw.opcode = true) + (missing : siteClaim environment none raw models.table.claims = .refused) : + models.siteAgrees environment profile raw emitted none true = false := by + simp [ModelCatalog.siteAgrees, ModelCatalog.agreesAt, + ModelCatalog.callRequirement, called, missing, + CallRequirement.accepts] + +#print axioms unclaimedLiveRefused + +/-- A retained modelled direct call names the member of its raw operand. -/ +theorem retainedModelNamesMember (models : ModelCatalog) (environment : LayoutEnvironment) + (profile : FeatureProfile) (raw : RawInstruction) (member : CallSite) + (target : MethodRef) (prefixes : CallPrefixes) + (called : raw.opcode = 40) (operand : raw.operand = .call member) + (model : siteClaim environment none raw models.table.claims = .model) + (accepted : models.siteAgrees environment profile raw (.call target prefixes) = true) : + target = member.method := by + simp only [ModelCatalog.siteAgrees, ModelCatalog.agreesAt, + ModelCatalog.callRequirement, callLayouts_environment, model, operand, called, + methodTokenCall, callOpcode?, Bool.true_or, beq_self_eq_true, ite_true] at accepted + split at accepted <;> simp_all [CallRequirement.accepts] + +#print axioms retainedModelNamesMember + +-- An entry filed under another name's key is not selected for that name. +/-- A tree that files the span length entry under the key of `get_IsEmpty`. -/ +private def misfiled : ModelTable := .node .leaf (name!"get_IsEmpty").code [spanLengthEntry] .leaf +private def spanIsEmpty : MethodRef := + { spanLength word with name := name!"get_IsEmpty", returnType := runtimeType "System.Boolean" } +example : misfiled.find {} .call spanIsEmpty = none := by decide +kernel + +-- An unlinked call without a named family has no resolved instruction. +example : catalog.siteAgrees {} {} (site divRem) (.call divRem) = false := by decide +kernel +example : catalog.siteAgrees {} {} (site divRem) (.call bigMul) = false := by + decide +kernel + +-- The opcode and the owner are part of the key; a generic owner matches by its head. +example : catalog.table.find {} .newobj bigMul = none := by decide +kernel +example : catalog.table.find {} .call { bigMul with owner := runtimeType "System.Maths" } = + none := by decide +kernel +example : catalog.siteAgrees {} {} (site (spanLength word)) (.call (spanLength (runtimeType "System.Byte"))) = false := by + decide +kernel +example : catalog.table.find {} .call + { spanLength word with owner := runtimeType "System.String" } = none := by decide +kernel +example : catalog.siteAgrees {} {} (site (spanLength (runtimeType "System.Byte"))) + (.call (spanLength word)) = false := by decide +kernel +example : catalog.siteAgrees {} {} (site bigMul 115) (.call bigMul) = false := by + decide +kernel + +/-- A method whose only instruction is `instruction`. -/ +private def method (instruction : RawInstruction) : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := true, maxStack := 3, genericParameters := [], code := [instruction] } + +-- A dead method-token call carries the raw opcode in its hole. +example : ModelsAgree catalog {} {} (method (site bigMul)) [false] [.unadmitted 40] := by + decide +kernel +example : ¬ ModelsAgree catalog {} {} (method (site bigMul)) [false] [.unadmitted 115] := by + decide +kernel +example : ¬ ModelsAgree catalog {} {} (method (site bigMul)) [true] [.call divRem] := by + decide +kernel + +-- Two entries for one slot are refused when the table is built. +/-- error: model table: two entries share an opcode, owner and name -/ +#guard_msgs in +example : ModelTable := + model_table% [ModelEntry.exact .call bigMul (bigMul.signature 0) (.memory (.bigMul64 false)), + ModelEntry.exact .call bigMul (bigMul.signature 0) (.memory (.bigMul64 true))] + +/-- A multiply member outside the runtime owner has no model. -/ +private def unknown := { bigMul with owner := .atom "Fixture" "System.Math" } + +-- The runtime table distinguishes the exact constructor and wide multiplication calls. +example : runtimeModel {} .call objectConstructorMethod = some .objectConstructor := by + decide +kernel +example : runtimeModel {} .call bigMul = some (.memory (.bigMul64 false)) := by decide +kernel +example : runtimeModel {} .newobj objectConstructorMethod = none := by decide +kernel +example : runtimeModel {} .call + { objectConstructorMethod with owner := .atom "Fixture" "System.Object" } = none := by + decide +kernel +/-- `System.Object` as another assembly declares it. -/ +private def coreObject : TypeExpr := .atom "System.Private.CoreLib" "System.Object" +example : runtimeModel {} .call { objectConstructorMethod with owner := coreObject } = none := by + decide +kernel +example : runtimeModel {} .call { objectConstructorMethod with hasThis := false } = none := by + decide +kernel +example : runtimeModel {} .call { objectConstructorMethod with parameters := [word] } = none := by + decide +kernel +example : RuntimeModelsAgree (method (site unknown)) [true] + { body with code := [.call (Tests.callee 3) {}], links := [(0, .direct 7)] } := by + decide +kernel +example : ¬ RuntimeModelsAgree (method + (site bigMul)) [true] + { body with code := [.call (Tests.callee 3) {}], links := [(0, .direct 7)] } := by + decide +kernel + +-- An entry filed under `newobj` does not model a `call` of its method. +example : ModelTable.find + (model_table% [ModelEntry.exact .newobj objectConstructorMethod + { convention := 0x20 } .objectConstructor]) + {} .call objectConstructorMethod = none := by decide +kernel + +/-- A body whose only instruction is `emitted`, with the link table `links`. -/ +private def emitting (emitted : CIL.Instruction) (links : List (Nat × CIL.Link)) : CIL.Method := + { body with code := [emitted], links } + +-- A model site keeps its call and states no link; a link there, or another emission, is refused. +example : + ManagedCallsValid [] [] (callModelled body) (method (site objectConstructorMethod)) [true] + (emitting (.call objectConstructorMethod {}) []) := by decide +kernel +example : ¬ ManagedCallsValid [⟨[], method (site bigMul)⟩] [] (callModelled body) + (method (site objectConstructorMethod)) [true] + (emitting (.call objectConstructorMethod {}) [(0, .direct 0)]) := by decide +kernel +example : ¬ ManagedCallsValid [] [] (callModelled body) (method (site objectConstructorMethod)) + [true] (emitting .pop []) := by decide +kernel +example : ¬ RuntimeModelsAgree (method (site objectConstructorMethod)) [true] + (emitting .nop []) := by decide +kernel +example : ¬ ManagedCallsValid [] [] (callModelled body) (method (site objectConstructorMethod)) + [true] (emitting (.call objectConstructorMethod { tail := true }) []) := by decide +kernel +example : ¬ ManagedCallsValid [] [] (callModelled body) (method (site objectConstructorMethod)) + [true] (emitting (.call bigMul {}) []) := by decide +kernel + +example : ¬ ManagedCallsValid [] [] (callModelled body) (method ⟨0, 40, .call { + method := objectConstructorMethod, declared := { convention := 0x20 }, + target := .managed 0, typeArguments := [], + parameterCount := 0, returnsValue := false }⟩) [true] + (emitting (.call objectConstructorMethod {}) []) := by decide +kernel + +/-- `Span(ref int)`, a construction the runtime table models. -/ +private def spanReference : MethodRef := + let int := runtimeType "System.Int32" + { owner := .apply (.constructor runtimeScope "System.Span`1") int, name := name!".ctor", + hasThis := true, genericArity := 0, returnType := .void, parameters := [.byref int] } + +/-- The linked call rule at a `newobj` of `spanReference` with the given link. -/ +private def constructionFits (live : Bool) (link : Option CIL.Link) : Bool := + validateLinkedCallWith (callModelled body) 1 (fun _ => none) (fun _ => none) + (site spanReference 115) live link (.newobj spanReference) + +-- A modelled construction keeps its newobj without a link. A link there is refused, live or dead, +-- so a forged link cannot make the construction enter a callee body. +example : callModelled body (.newobj, spanReference) ∧ constructionFits true none ∧ + constructionFits false none := by decide +kernel +example : [CIL.Link.aggregate 0 8, .object 0 [] 8, .direct 0].all fun link => + !constructionFits true (some link) && !constructionFits false (some link) := by decide +kernel + +-- `pop` stands for no model, so a model site does not emit it, and no call site of any opcode +-- emits it, since it would drop the call. +example : + ¬ RuntimeModelsAgree (method (site objectConstructorMethod)) [true] (emitting .pop []) := by + decide +kernel +example : callEmitsNoPop (site objectConstructorMethod) .pop = false := by decide +example : callEmitsNoPop (site bigMul) .pop = false := by decide +example : + callEmitsNoPop (site { objectConstructorMethod with hasThis := false } 111) .pop = false := by + decide +example : callEmitsNoPop (site bigMul 115) .pop = false := by decide +example : callEmitsNoPop ⟨0, 38, .none⟩ .pop = true := by decide +example : + callEmitsNoPop (site objectConstructorMethod) (.call objectConstructorMethod {}) = true := by + decide +-- The method check applies the rule: a live call emitting `pop` fails it, the same call emitting +-- its call passes, and a raw `pop` still emits `pop`. +example : ¬ UnsafeCallsValid (method (site bigMul)) [true] [.pop] := by decide +kernel +example : UnsafeCallsValid (method (site bigMul)) [true] [.call bigMul {}] := by decide +kernel +example : ¬ UnsafeCallsValid (method (site objectConstructorMethod)) [true] [.pop] := by + decide +kernel +example : UnsafeCallsValid (method ⟨0, 38, .none⟩) [true] [.pop] := by decide +kernel + +-- An external call the table does not model needs a link. +example : ¬ ManagedCallsValid [] [] (callModelled body) (method (site unknown)) [true] + (emitting (.call unknown {}) []) := by decide +kernel + +-- The integer helpers of `System.Math` and of each integer type. +private def int : TypeExpr := runtimeType "System.Int32" +private def mathOwner : TypeExpr := runtimeType "System.Math" + +/-- `owner::name` taking `arity` values of `type` and returning `result`. -/ +private def helper (name : MetadataName) (type : TypeExpr) (arity : Nat) + (result : TypeExpr := type) (owner : TypeExpr := mathOwner) : MethodRef := + { owner, name, hasThis := false, genericArity := 0, returnType := result, + parameters := List.replicate arity type } + +private def mathMin : MethodRef := helper name!"Min" int 2 +private def mathMax : MethodRef := helper name!"Max" int 2 +private def mathClamp : MethodRef := helper name!"Clamp" int 3 +private def mathAbs : MethodRef := helper name!"Abs" int 1 +private def mathSign : MethodRef := helper name!"Sign" (runtimeType "System.Int64") 1 int + +-- Each helper resolves to its scalar intrinsic at the width and signedness of its type. +example : runtimeModel {} .call mathMin = some (.intrinsic (.scalar (.math .min 32 true)) 2) := by + decide +kernel +example : runtimeModel {} .call mathMax = some (.intrinsic (.scalar (.math .max 32 true)) 2) := by + decide +kernel +example : runtimeModel {} .call mathClamp = + some (.intrinsic (.scalar (.math .clamp 32 true)) 3) := by decide +kernel +example : runtimeModel {} .call mathAbs = some (.intrinsic (.scalar (.math .abs 32 true)) 1) := by + decide +kernel +example : runtimeModel {} .call mathSign = + some (.intrinsic (.scalar (.math .sign 64 true)) 1) := by decide +kernel +example : runtimeModel {} .call (helper name!"Min" word 2) = + some (.intrinsic (.scalar (.math .min 64 false)) 2) := by decide +kernel +example : runtimeModel {} .call (helper name!"Max" (runtimeType "System.IntPtr") 2) = + some (.intrinsic (.scalar (.math .max 64 true)) 2) := by decide +kernel +example : runtimeModel {} .call (helper name!"Max" (runtimeType "System.Byte") 2) = + some (.intrinsic (.scalar (.math .max 8 false)) 2) := by decide +kernel +-- The integer type that declares a helper is modelled like `System.Math`. +example : runtimeModel {} .call (helper name!"Max" int 2 (owner := int)) = + some (.intrinsic (.scalar (.math .max 32 true)) 2) := by decide +kernel + +-- A receiver, a generic arity or method argument, a wrong arity, mixed or unmodelled types, a +-- wrong result, an unsigned `Abs`, or another owner, assembly or opcode has no model. +example : runtimeModel {} .call { mathMax with hasThis := true } = none := by decide +kernel +example : runtimeModel {} .call { mathMax with genericArity := 1 } = none := by decide +kernel +example : runtimeModel {} .call { mathMax with methodArguments := [int] } = none := by + decide +kernel +example : runtimeModel {} .call (helper name!"Max" int 3) = none := by decide +kernel +example : runtimeModel {} .call (helper name!"Abs" int 2) = none := by decide +kernel +example : runtimeModel {} .call { mathMax with parameters := [int, runtimeType "System.Int64"] } = + none := by decide +kernel +example : runtimeModel {} .call (helper name!"Max" (runtimeType "System.Boolean") 2) = none := by + decide +kernel +example : runtimeModel {} .call (helper name!"Max" (.atom "Fixture" "System.Int32") 2) = none := by + decide +kernel +example : runtimeModel {} .call { mathMax with returnType := runtimeType "System.Int64" } = + none := by decide +kernel +example : runtimeModel {} .call (helper name!"Sign" int 1) = some + (.intrinsic (.scalar (.math .sign 32 true)) 1) := by decide +kernel +example : runtimeModel {} .call (helper name!"Sign" (runtimeType "System.Int16") 1) = none := by + decide +kernel +example : runtimeModel {} .call (helper name!"Abs" word 1) = none := by decide +kernel +example : runtimeModel {} .call (helper name!"Abs" word 1 (owner := word)) = none := by + decide +kernel +example : runtimeModel {} .call (helper name!"Max" int 2 (owner := runtimeType "System.Int64")) = + none := by decide +kernel +example : runtimeModel {} .call + (helper name!"Max" int 2 (owner := .atom "Fixture" "System.Math")) = none := by decide +kernel +example : runtimeModel {} .call + (helper name!"Max" int 2 (owner := .atom "System.Private.CoreLib" "System.Math")) = none := by + decide +kernel +example : runtimeModel {} .call (helper name!"Max" int 2 (owner := runtimeType "System.Maths")) = + none := by decide +kernel +example : runtimeModel {} .newobj mathMax = none := by decide +kernel + +-- Each helper site requires the closed method reference of its own declaration. +example : RuntimeModelsAgree (method (site mathMax)) [true] (emitting (.call mathMax {}) []) := by + decide +kernel +example : ¬ RuntimeModelsAgree (method (site mathMax)) [true] (emitting (.call mathMin) []) := by + decide +kernel +example : ¬ RuntimeModelsAgree (method (site mathMin)) [true] (emitting (.call mathMax) []) := by + decide +kernel +example : ¬ ModelsAgree { table := model_table% [objectConstructorEntry] } + {} {} (method (site mathMin)) [true] [.call mathMax] := by decide +kernel +example : ManagedCallsValid [] [] (callModelled body) (method (site mathMin)) [true] + (emitting (.call mathMin {}) []) := by decide +kernel +example : ¬ ManagedCallsValid [] [] (callModelled body) (method (site mathMin)) [true] + (emitting (.call mathMax {}) []) := by decide +kernel +example : ¬ ManagedCallsValid [] [] (callModelled body) (method (site mathMin)) [true] + (emitting (.call mathMin {}) [(0, .direct 0)]) := by decide +kernel + +-- A modelled intrinsic must be available in the method's profile: an AVX2 entry is refused at a +-- site whose profile lacks AVX2 and accepted where it has it. +/-- `Avx2.And` on two 256-bit vectors, which a table below models. -/ +private def avx2And : MethodRef := + { owner := runtimeType "System.Runtime.Intrinsics.X86.Avx2", name := name!"And", + hasThis := false, genericArity := 0, returnType := word, parameters := [word, word] } +private def avx2Catalog : ModelCatalog := + { table := model_table% [ModelEntry.exact .call avx2And (avx2And.signature 0) + (.intrinsic (.avx2 .band) 2)] } +example : avx2Catalog.siteAgrees {} {} (site avx2And) (.call avx2And {}) = false := by + decide +kernel +example : avx2Catalog.siteAgrees {} { avx2 := true } (site avx2And) (.call avx2And {}) = true := by + decide +kernel + +/-- A linked pc must also satisfy the profile requirements of its resolved model. -/ +theorem linkedProfileRequired : + avx2Catalog.siteAgrees {} {} (site avx2And) (.call avx2And {}) (some (.direct 0)) + true = false := by decide +kernel + +#print axioms linkedProfileRequired + +-- The profile query of a call is its model's: the helpers read no flag, so every two profiles +-- agree on a program that calls one. +/-- A method that returns `Math.Max` of its two arguments. -/ +private def maxProgram : CIL.Program := + [{ body with code := [.ldarg 0, .ldarg 1, .call mathMax {}, .ret], returnsValue := true }] +example : CIL.Program.profileReads maxProgram = [] := by decide +kernel +example : CIL.Program.ProfileAgreement maxProgram {} { avx2 := true } := + CIL.Program.ProfileAgreement.of_flags _ _ _ (by decide +kernel) + +-- Each vector helper resolves to its intrinsic. +example : runtimeModel {} .call createScalarUnsafeMethod = + some (.intrinsic (.vector .createScalarUnsafe64) 1) := by decide +kernel +example : runtimeModel {} .call getElement128Method = + some (.intrinsic (.vector (.extract64 128)) 2) := by decide +kernel +example : runtimeModel {} .call getElement256Method = + some (.intrinsic (.vector (.extract64 256)) 2) := by decide +kernel + +-- A receiver, another generic arity, method argument, element type, vector width, arity or +-- result, or another assembly or opcode has no model. +/-- `Vector128` and `Vector256`. -/ +private def vector128 : TypeExpr := ulongVectorOf "System.Runtime.Intrinsics.Vector128`1" +private def vector256 : TypeExpr := ulongVectorOf "System.Runtime.Intrinsics.Vector256`1" +example : runtimeModel {} .call { createScalarUnsafeMethod with hasThis := true } = none := by + decide +kernel +example : runtimeModel {} .call { createScalarUnsafeMethod with parameters := [int] } = none := by + decide +kernel +example : runtimeModel {} .call { createScalarUnsafeMethod with returnType := vector256 } = + none := by decide +kernel +example : runtimeModel {} .call { getElement128Method with genericArity := 0 } = none := by + decide +kernel +example : runtimeModel {} .call { getElement128Method with methodArguments := [int] } = none := by + decide +kernel +example : runtimeModel {} .call { getElement128Method with parameters := [vector256, int] } = + none := by decide +kernel +example : runtimeModel {} .call { getElement128Method with parameters := [vector128] } = none := by + decide +kernel +example : runtimeModel {} .call { getElement128Method with returnType := int } = none := by + decide +kernel +example : runtimeModel {} .call + { getElement128Method with owner := runtimeType "System.Runtime.Intrinsics.Vector128" } = + none := by decide +kernel +example : runtimeModel {} .call { getElement128Method with owner := getElement256Method.owner } = + none := by decide +kernel +example : runtimeModel {} .call + { createScalarUnsafeMethod with owner := getElement256Method.owner } = none := by + decide +kernel +example : runtimeModel {} .call + { createScalarUnsafeMethod with owner := runtimeType "System.Runtime.Intrinsics.Vector128" } = + none := by decide +kernel +example : runtimeModel {} .newobj createScalarUnsafeMethod = none := by decide +kernel + +/-- A call of the generic lane reader with its member declaration. -/ +private def laneSite := site getElement128Method 40 + (getElementSignature "System.Runtime.Intrinsics.Vector128`1") + +/-- A call of the 256-bit lane reader with its member declaration. -/ +private def lane256Site := site getElement256Method 40 + (getElementSignature "System.Runtime.Intrinsics.Vector256`1") + +/-- Whether the model accepts a kept call with this member declaration. -/ +private def accepts (method : MethodRef) (declared : OpenSignature) : Bool := + runtimeCatalog.siteAgrees {} {} (site method 40 declared) (.call method {}) + +/-- Changing the convention alone invalidates each member declaration. -/ +private def refusesConventions (method : MethodRef) (signature : OpenSignature) : Bool := + [0x40, 0x60, 0x01].all fun convention => !accepts method { signature with convention } + +example : refusesConventions objectConstructorMethod { convention := 0x20 } := by decide +kernel +example : refusesConventions mathMin (mathMin.signature 0) := by decide +kernel +example : refusesConventions getElement128Method + (getElementSignature "System.Runtime.Intrinsics.Vector128`1") := by decide +kernel +example : refusesConventions createScalarUnsafeMethod (createScalarUnsafeMethod.signature 0) := by + decide +kernel + +/-- The decision after the caller specializes a member whose return is its type variable. -/ +private def specializedCall (catalog : ModelCatalog) (reference : MethodRef) : Option Bool := do + let openRef := { reference with returnType := .parameter 0 } + let operand := (site openRef 40 (openRef.signature 0)).operand + let .call closed ← substituteOperand [reference.returnType] [] 1 operand | none + pure (catalog.siteAgrees {} {} ⟨0, 40, .call closed⟩ (.call closed.method {})) + +example : specializedCall runtimeCatalog mathMin = some false := by decide +kernel +example : specializedCall catalog bigMul = some false := by decide +kernel +example : accepts getElement128Method { getElementSignature "System.Runtime.Intrinsics.Vector128`1" + with returnType := word } = false := by decide +kernel +example : catalog.siteAgrees {} {} (site bigMul 40 + { bigMul.signature 0 with returnType := .parameter 0 }) (.call bigMul) = false := + by decide +kernel + +-- A vector helper site refuses a call with another member or vector width. +example : RuntimeModelsAgree (method laneSite) [true] + (emitting (.call getElement128Method {}) []) := by decide +kernel +example : ¬ RuntimeModelsAgree (method laneSite) [true] + (emitting (.call getElement256Method) []) := by decide +kernel +example : ¬ ModelsAgree { table := model_table% [objectConstructorEntry] } + {} {} (method laneSite) [true] [.call getElement256Method] := by decide +kernel +example : ¬ RuntimeModelsAgree (method (site createScalarUnsafeMethod)) [true] + (emitting (.call getElement128Method) []) := by decide +kernel +example : ¬ ModelsAgree { table := model_table% [objectConstructorEntry] } + {} {} (method (site createScalarUnsafeMethod)) [true] [.call getElement128Method] := by decide +kernel +/-- Keeping a valid 256-bit call still requires its own generic member declaration. -/ +example : ¬ RuntimeModelsAgree (method (site getElement256Method 40 + { getElementSignature "System.Runtime.Intrinsics.Vector256`1" with returnType := word })) + [true] (emitting (.call getElement256Method {}) []) := by decide +kernel + +example : ManagedCallsValid [] [] (callModelled body) (method lane256Site) [true] + (emitting (.call getElement256Method {}) []) := by decide +kernel +example : ¬ ManagedCallsValid [] [] (callModelled body) (method lane256Site) [true] + (emitting (.call getElement256Method {}) [(0, .direct 0)]) := by decide +kernel + +end Table + +/-- The receiver an object constructor call consumes. -/ +private def receiver : Value := .object (.address ⟨0, 0⟩) + +/-- A call of the `System.Object` constructor at a pc with no link steps as `pop` does. -/ +example : step { body with code := [.call CIL.Extraction.objectConstructorMethod {}] } + (.call CIL.Extraction.objectConstructorMethod {}) 0 [] frame [receiver] heapMemory = + step body .pop 0 [] frame [receiver] heapMemory := by + rfl + +/-- The method loads its arguments, executes one unlinked runtime call and returns. -/ +private def invokeCall (method : CIL.Extraction.MethodRef) (args : List Value) + (returnsValue : Bool) : Except LocatedFault (List Value) := + let loads := (List.range args.length).map CIL.Instruction.ldarg + let wrapper := { body with code := loads ++ [.call method {}, .ret], returnsValue } + (invoke [wrapper] 8 0 args Tests.caller).map Prod.snd + +example : invokeCall CIL.Extraction.objectConstructorMethod [.object .null] false = + .ok [] := by rfl + +example : invokeCall Table.mathMin [.scalar (.i32 3), .scalar (.i32 1)] true = + .ok [.scalar (.i32 1)] := by rfl + +example : invokeCall (Table.spanLength Table.word) [.span (.address ⟨0, 0⟩) 1] true = + .ok [.scalar (.i32 1)] := by rfl + +example : invokeCall Table.divRem [.scalar (.i64 7), .scalar (.i64 3), Tests.reference] true = + .ok [.scalar (.i64 2)] := by rfl + +example : invokeCall Table.divRem [.scalar (.i64 7), .scalar (.i64 0), Tests.reference] true = + .error ⟨0, 3, .arithmetic .divideByZero⟩ := by decide + +/-- The null guard accepts an object and its parameter name. -/ +private def nullGuard : CIL.Extraction.MethodRef := + { owner := Table.runtimeType "System.ArgumentNullException", name := name!"ThrowIfNull", + hasThis := false, genericArity := 0, returnType := .void, + parameters := [Table.runtimeType "System.Object", .object CIL.Extraction.runtimeScope + "System.String"] } + +example : invokeCall nullGuard [.object .null, .object .null] false = + .error ⟨0, 2, .thrown .argumentNull⟩ := by rfl + +namespace ClosedMembers +open CIL CIL.Extraction + +/-- The fixture has no aggregate layout rows. -/ +private def layouts : LayoutEnvironment := {} + +/-- Each wrapper loads the member's arguments, calls it without a link, and returns its result. -/ +private def wrapper (profile : FeatureProfile) (environment : LayoutEnvironment) + (member : MethodRef) : Option Method := + let count := member.parameters.length + member.hasThis.toNat + let code := (List.range count).map Instruction.ldarg ++ [.call member {}, .ret] + let returnsValue := member.returnType != .void + some { body with profile, environment, code, returnsValue } + +/-- The scalar invocation exposes either the returned values or the located fault. -/ +private def run (method : Method) (args : List Safety.Value) (memory := Tests.caller) := + (Safety.invoke [method] 8 0 args memory).map Prod.snd + +/-- The closed signature supplies the static member's parameters and method type arguments. -/ +private def member (owner : TypeExpr) (name : MetadataName) (result : TypeExpr) + (parameters : List TypeExpr := []) (arguments : List TypeExpr := []) : MethodRef := + { owner, name, hasThis := false, genericArity := arguments.length, + returnType := result, parameters, methodArguments := arguments } + +/-- The scalar element in the generic memory helpers is a runtime byte. -/ +private def byte := TypeExpr.atom runtimeScope "System.Byte" + +/-- The byte spans carry the closed element type in their return or parameter signature. -/ +private def span (writable : Bool) := TypeExpr.apply (spanHead writable) byte + +/-- The runtime helpers share the same static owner. -/ +private def helpers := TypeExpr.atom runtimeScope "System.Runtime.CompilerServices.RuntimeHelpers" + +cil_decoded_method platformBody := wrapper FeatureProfile.scalar layouts + (member (.atom intrinsicsScope "System.Runtime.Intrinsics.X86.Avx2") name!"get_IsSupported" + booleanType) + +/-- The scalar profile reports AVX2 as unavailable. -/ +theorem platform_result : run platformBody [] = + .ok [.scalar (.i32 0)] := rfl + +#print axioms platform_result + +cil_decoded_method binaryReadBody := wrapper FeatureProfile.scalar layouts + (member (.atom spanMemoryScope "System.Buffers.Binary.BinaryPrimitives") + name!"ReadInt16BigEndian" (.atom runtimeScope "System.Int16") [span false]) + +/-- Reading the two static bytes as a big-endian Int16 gives 0x0707. -/ +theorem binaryRead_result : + run binaryReadBody [.bufferSpan ⟨.address ⟨0, 0⟩, 2, 1, false⟩] staticMemory = + .ok [.scalar (.i32 1799)] := rfl + +#print axioms binaryRead_result + +cil_decoded_method vectorLaneBody := wrapper FeatureProfile.scalar layouts + getElement128Method + +/-- The vector helper reads the initialized low lane. -/ +theorem vectorLane_result : run vectorLaneBody [.scalar (.v128Low64 7), .scalar (.i32 0)] = + .ok [.scalar (.i64 7)] := rfl + +#print axioms vectorLane_result + +cil_decoded_method vectorBroadcastBody := wrapper FeatureProfile.scalar layouts + broadcast256Method + +/-- Broadcasting the word places it in all four lanes. -/ +theorem vectorBroadcast_result : run vectorBroadcastBody [.scalar (.i64 3)] = + .ok [.scalar (.v256 (CIL.Vector.pack256 3 3 3 3))] := rfl + +#print axioms vectorBroadcast_result + +cil_decoded_method populationBody := wrapper FeatureProfile.scalar layouts + (member (.atom runtimeScope "System.Numerics.BitOperations") name!"PopCount" int32Type + [Table.word]) + +/-- The population count of seven is three. -/ +theorem population_result : run populationBody [.scalar (.i64 7)] = + .ok [.scalar (.i32 3)] := rfl + +#print axioms population_result + +cil_decoded_method asSpanBody := wrapper FeatureProfile.scalar layouts + (member (.atom spanMemoryScope "System.MemoryExtensions") name!"AsSpan" (span true) + [.array byte] [byte]) + +/-- The whole-array extension returns an empty writable span for a null array. -/ +theorem asSpan_result : run asSpanBody [.object .null] = + .ok [.bufferSpan ⟨.null, 0, 1, true⟩] := rfl + +#print axioms asSpan_result + +cil_decoded_method fillArrayBody := wrapper FeatureProfile.scalar layouts + (member (.atom runtimeScope "System.Array") name!"Fill" .void [.array byte, byte] [byte]) + +/-- Filling a null array faults at the call. -/ +theorem fillArray_result : run fillArrayBody [.object .null, .scalar (.i32 7)] = + .error ⟨0, 2, .thrown .argumentNull⟩ := rfl + +#print axioms fillArray_result + +cil_decoded_method staticSpanBody := wrapper FeatureProfile.scalar layouts + (member helpers name!"CreateSpan" (span false) [.atom runtimeScope "System.RuntimeFieldHandle"] + [byte]) + +/-- The field handle exposes sixteen static bytes. -/ +theorem staticSpan_result : run staticSpanBody [Tests.reference] staticMemory = + .ok [.span (.address ⟨0, 0⟩) 16] := rfl + +#print axioms staticSpan_result + +cil_decoded_method guardNullBody := wrapper FeatureProfile.scalar layouts + nullGuard + +/-- The decoded guard reports the null argument at the call. -/ +theorem guardNull_result : run guardNullBody [.object .null, .object .null] = + .error ⟨0, 2, .thrown .argumentNull⟩ := rfl + +#print axioms guardNull_result + +cil_decoded_method spanLengthBody := wrapper FeatureProfile.scalar layouts + (Table.spanLength Table.word) + +/-- The span getter returns the element count. -/ +theorem spanLength_result : run spanLengthBody [.span (.address ⟨0, 0⟩) 2] = + .ok [.scalar (.i32 2)] := rfl + +#print axioms spanLength_result + +cil_decoded_method referenceContentBody := wrapper FeatureProfile.scalar layouts + (member helpers name!"IsReferenceOrContainsReferences" booleanType [] [byte]) + +/-- A byte contains no managed references. -/ +theorem referenceContent_result : run referenceContentBody [] = + .ok [.scalar (.i32 0)] := rfl + +#print axioms referenceContent_result + +cil_decoded_method marshalReferenceBody := wrapper FeatureProfile.scalar layouts + (member (.atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal") name!"GetReference" + (.byref byte) [span true] [byte]) + +/-- The marshal helper returns the start of the span. -/ +theorem marshalReference_result : + run marshalReferenceBody [.bufferSpan ⟨.address ⟨0, 0⟩, 1, 1, true⟩] = + .ok [Tests.reference] := rfl + +#print axioms marshalReference_result + +cil_decoded_method sizeOfBody := wrapper FeatureProfile.scalar layouts + (member unsafeOwner name!"SizeOf" int32Type [] [byte]) + +/-- A byte occupies one byte. -/ +theorem sizeOf_result : run sizeOfBody [] = + .ok [.scalar (.i32 1)] := rfl + +#print axioms sizeOf_result + +cil_decoded_method reinterpretBody := wrapper FeatureProfile.scalar layouts + (member unsafeOwner name!"As" (.byref byte) [.byref byte] [byte, byte]) + +/-- Reinterpreting a byte reference preserves its address. -/ +theorem reinterpret_result : run reinterpretBody [Tests.reference] = + .ok [Tests.reference] := rfl + +#print axioms reinterpret_result + +cil_decoded_method nullRefBody := wrapper FeatureProfile.scalar layouts + (member unsafeOwner name!"NullRef" (.byref byte) [] [byte]) + +/-- The null reference helper returns a null managed reference. -/ +theorem nullRef_result : run nullRefBody [] = + .ok [.reference .null] := rfl + +#print axioms nullRef_result + +cil_decoded_method offsetBody := wrapper FeatureProfile.scalar layouts + (member unsafeOwner name!"ByteOffset" (.atom runtimeScope "System.IntPtr") + [.byref byte, .byref byte] [byte]) + +/-- The two references differ by four bytes. -/ +theorem offset_result : run offsetBody [start, fourth] = + .ok [.scalar (.i64 4)] := rfl + +#print axioms offset_result + +cil_decoded_method loadByteBody := wrapper FeatureProfile.scalar layouts + (member unsafeOwner name!"ReadUnaligned" byte [.byref byte] [byte]) + +/-- The unaligned transfer reads the initialized static byte. -/ +theorem loadByte_result : run loadByteBody [Tests.reference] staticMemory = + .ok [.scalar (.i32 7)] := rfl + +#print axioms loadByte_result + +cil_decoded_method blockInitBody := wrapper FeatureProfile.scalar layouts + (member unsafeOwner name!"InitBlockUnaligned" .void + [.byref byte, byte, .atom runtimeScope "System.UInt32"]) + +/-- A one-byte block initialization succeeds in writable caller memory. -/ +theorem blockInit_result : + run blockInitBody [Tests.reference, .scalar (.i32 9), .scalar (.i32 1)] = .ok [] := rfl + +#print axioms blockInit_result + +end ClosedMembers + +-- Each helper call steps as the scalar intrinsic it was lowered to, faults included. +/-- A body whose only instruction is a call of `method`, with no link. -/ +private def calling (method : CIL.Extraction.MethodRef) : CIL.Method := + { body with code := [.call method {}] } +private def int32 (value : Int) : Value := .scalar (.i32 (BitVec.ofInt 32 value)) +example : step (calling Table.mathMin) (.call Table.mathMin {}) 0 [] frame [int32 3, int32 (-2)] + heapMemory = + intrinsicCall body.profile (.scalar (.math .min 32 true)) 2 0 frame [int32 3, int32 (-2)] + heapMemory := by rfl +example : step (calling Table.mathMax) (.call Table.mathMax {}) 0 [] frame [int32 3, int32 (-2)] + heapMemory = + intrinsicCall body.profile (.scalar (.math .max 32 true)) 2 0 frame [int32 3, int32 (-2)] + heapMemory := by rfl +example : step (calling Table.mathClamp) (.call Table.mathClamp {}) 0 [] frame + [int32 9, int32 1, int32 5] heapMemory = + intrinsicCall body.profile (.scalar (.math .clamp 32 true)) 3 0 frame + [int32 9, int32 1, int32 5] heapMemory := by rfl +example : step (calling Table.mathAbs) (.call Table.mathAbs {}) 0 [] frame [int32 (-7)] + heapMemory = + intrinsicCall body.profile (.scalar (.math .abs 32 true)) 1 0 frame [int32 (-7)] + heapMemory := by rfl +example : step (calling Table.mathSign) (.call Table.mathSign {}) 0 [] frame + [.scalar (.i64 (BitVec.ofInt 64 (-7)))] heapMemory = + intrinsicCall body.profile (.scalar (.math .sign 64 true)) 1 0 frame + [.scalar (.i64 (BitVec.ofInt 64 (-7)))] heapMemory := by rfl +example : (step (calling Table.mathMax) (.call Table.mathMax {}) 0 [] frame + [int32 3, int32 (-2)] heapMemory).toBool := by rfl +-- Bounds out of order and the most negative `Abs` fault as the intrinsic does. +example : step (calling Table.mathClamp) (.call Table.mathClamp {}) 0 [] frame + [int32 1, int32 5, int32 9] heapMemory = + intrinsicCall body.profile (.scalar (.math .clamp 32 true)) 3 0 frame + [int32 1, int32 5, int32 9] heapMemory := by rfl +example : (step (calling Table.mathClamp) (.call Table.mathClamp {}) 0 [] frame + [int32 1, int32 5, int32 9] heapMemory).toBool = false := by rfl +example : step (calling Table.mathAbs) (.call Table.mathAbs {}) 0 [] frame + [int32 (-2147483648)] heapMemory = + intrinsicCall body.profile (.scalar (.math .abs 32 true)) 1 0 frame [int32 (-2147483648)] + heapMemory := by rfl +-- A modelled call leaves unspecified operands to its model, so it faults as the intrinsic does. +example : step (calling Table.mathMax) (.call Table.mathMax {}) 0 [] frame + [.scalar (.v128Low64 7), int32 1] heapMemory = + intrinsicCall body.profile (.scalar (.math .max 32 true)) 2 0 frame + [.scalar (.v128Low64 7), int32 1] heapMemory := by rfl + +section +open CIL.Extraction (createScalarUnsafeMethod getElement128Method getElement256Method) + +-- Each vector helper call steps as its intrinsic. `GetElement` reads lane 0 of a vector known +-- only in its low lane, where the call faulted before it left its operands to its model. +example : step (calling getElement128Method) (.call getElement128Method {}) 0 [] frame + [int32 0, .scalar (.v128Low64 7)] heapMemory = + intrinsicCall body.profile (.vector (.extract64 128)) 2 0 frame + [int32 0, .scalar (.v128Low64 7)] heapMemory := by rfl +example : step (calling getElement128Method) (.call getElement128Method {}) 0 [] frame + [int32 0, .scalar (.v128Low64 7)] heapMemory = + .ok (.next 1 [.scalar (.i64 7)] frame heapMemory) := by rfl +example : step (calling getElement128Method) (.call getElement128Method {}) 0 [] frame + [int32 1, .scalar (.v128Low64 7)] heapMemory = + intrinsicCall body.profile (.vector (.extract64 128)) 2 0 frame + [int32 1, .scalar (.v128Low64 7)] heapMemory := by rfl +/-- A 256-bit vector whose four 64-bit lanes hold 1, 2, 3 and 4, lowest first. -/ +private def lanes : BitVec 256 := CIL.Vector.pack256 1 2 3 4 +example : step (calling getElement256Method) (.call getElement256Method {}) 0 [] frame + [int32 2, .scalar (.v256 lanes)] heapMemory = + intrinsicCall body.profile (.vector (.extract64 256)) 2 0 frame + [int32 2, .scalar (.v256 lanes)] heapMemory := by rfl +example : step (calling getElement256Method) (.call getElement256Method {}) 0 [] frame + [int32 2, .scalar (.v256 lanes)] heapMemory = + .ok (.next 1 [.scalar (.i64 3)] frame heapMemory) := by rfl +example : step (calling getElement256Method) (.call getElement256Method {}) 0 [] frame + [int32 4, .scalar (.v256 lanes)] heapMemory = + intrinsicCall body.profile (.vector (.extract64 256)) 2 0 frame + [int32 4, .scalar (.v256 lanes)] heapMemory := by rfl +example : step (calling createScalarUnsafeMethod) (.call createScalarUnsafeMethod {}) 0 [] frame + [.scalar (.i64 7)] heapMemory = + intrinsicCall body.profile (.vector .createScalarUnsafe64) 1 0 frame [.scalar (.i64 7)] + heapMemory := by rfl + +/-- `GetElement(CreateScalarUnsafe(value), 0)` as two calls, as an extraction emits it. -/ +private def unsafeLow : CIL.Program := + [{ body with + code := [.ldarg 0, .call createScalarUnsafeMethod {}, .ldc_i4 0, + .call getElement128Method {}, .ret], + returnsValue := true }] +example : (invoke unsafeLow 8 0 [.scalar (.i64 7)] Memory.empty).map Prod.snd = + .ok [.scalar (.i64 7)] := by rfl + +end + +section +open CIL.Extraction (ModelEntry) + +-- Every memory operation's model transports under an allocation renaming: a read, a write and +-- an operation with an out operand among them. +example : (CIL.RuntimeModel.memory (.arraySpan .signed32 true .whole)).StepRenamed := + memory_stepRenamed _ +example : (CIL.RuntimeModel.memory (.spanCopy 4 true)).StepRenamed := memory_stepRenamed _ +example : (CIL.RuntimeModel.memory (.divRemOut 64)).StepRenamed := memory_stepRenamed _ + +/-- A family of entries that resolve only memory operations transports under every renaming, + which is the case such a family adds to `runtimeModel_stepRenamed`. -/ +theorem memoryEntries_stepRenamed (entries : List ModelEntry) + (memoryOnly : ∀ entry ∈ entries, + entry.Yields fun model => ∃ operation, model = .memory operation) : + ∀ entry ∈ entries, entry.Yields CIL.RuntimeModel.StepRenamed := by + intro entry member environment method model found + obtain ⟨operation, rfl⟩ := memoryOnly entry member environment method model found + exact memory_stepRenamed operation +/-- +info: 'Tests.RuntimeModels.memoryEntries_stepRenamed' depends on axioms: [propext, Quot.sound] +-/ +#guard_msgs in +#print axioms memoryEntries_stepRenamed + +end + +open CIL CIL.Extraction + +/-- A virtual call with a link belongs to the totality obligation and satisfies it. -/ +theorem linkedVirtualClaim (environment : LayoutEnvironment) (link : Link) (offset : Nat) + (site : CallSite) : + SiteClaimTotal environment (some link) ⟨offset, 111, .call site⟩ true ∧ + siteClaim environment (some link) ⟨offset, 111, .call site⟩ = .link := by + constructor + · intro _ _ + simp [siteClaim, methodTokenCall] + · rfl + +#print axioms linkedVirtualClaim + +/-- A live, unlinked virtual call has no claim. -/ +theorem unknownVirtualRefused (environment : LayoutEnvironment) (offset : Nat) (site : CallSite) : + ¬ SiteClaimTotal environment none ⟨offset, 111, .call site⟩ true := by + simp [SiteClaimTotal, foldOwnsSite, siteClaim, methodTokenCall, callOpcode?] + +#print axioms unknownVirtualRefused + +/-- Whether a fold role fixes its entire instruction, stated per constructor: a role outside + the head and interior of a throw sequence must leave a live call to the claim check. -/ +private def ownsWholeSite : Option FoldRole → Bool + | none => false + | some .head => true + | some .interior => true + | some .typeHead | some .typeOperand | some (.result _) => false + | some (.typeBranch _) => false + | some (.raise _) => false + | some .argument => false + | some .tailCall => false + | some .blocked => false + +/-- Only throw head and interior roles exempt a site from its decoder and claim check. -/ +theorem foldOwnsSite_roles (role : Option FoldRole) : foldOwnsSite role = ownsWholeSite role := by + cases role with + | none => rfl + | some part => cases part <;> rfl + +#print axioms foldOwnsSite_roles + +/-- An unlinked method-token site that the table does not claim is refused. -/ +theorem unclaimedSiteRefused (environment : LayoutEnvironment) (raw : RawInstruction) + (site : CallSite) (token : methodTokenCall raw.opcode = true) + (operand : raw.operand = .call site) + (claims : LayoutEnvironment → CallOpcode → MethodRef → Bool) + (unclaimed : (callOpcode? raw.opcode).any + (fun op => claims environment op site.method) = false) : + siteClaim environment none raw claims = .refused := by + simp [siteClaim, token, operand, unclaimed] + +#print axioms unclaimedSiteRefused + +end Tests.RuntimeModels diff --git a/Tests/SavedSummaries.cs b/Tests/SavedSummaries.cs new file mode 100644 index 000000000..4d3ecd4dd --- /dev/null +++ b/Tests/SavedSummaries.cs @@ -0,0 +1,212 @@ +using System.Reflection.Metadata; +using System.Reflection.PortableExecutable; + +partial class Program +{ + void CheckSavedSummaries(string directory, string assembly) + { + const string signature = "::Add(System.UInt64,System.UInt64)"; + const string addition = "System.UInt64 Examples.Arithmetic" + signature; + Directory.CreateDirectory(directory); + var changed = Path.Combine(directory, "Examples.dll"); + WriteInstructionChange(assembly, changed, "Add", [0x02, 0x03, 0x58, 0x2a], 2, 0x59); + CheckSavedSummary(Path.Combine(directory, "Addition"), assembly, addition, + At("Tests/SummaryModules/SavedSummary.lean"), + ("OtherToken", assembly, "System.UInt64 SelectorExamples.One.Collision" + signature, "token"), + ("OtherBody", changed, addition, "program")); + CheckSavedSummary(Path.Combine(directory, "Vector"), assembly, + "System.UInt64 Examples.DivisionIntrinsics::UnsafeLow(System.UInt64)", + At("Tests/SummaryModules/Vector/SavedSummary.lean")); + CheckSavedSummary(Path.Combine(directory, "Memory"), assembly, + "System.Void Examples.Arithmetic::Mutate(System.UInt64&)", + At("Tests/SummaryModules/Memory/SavedSummary.lean")); + CheckSavedBranches(Path.Combine(directory, "Branch"), assembly); + CheckSavedLoop(Path.Combine(directory, "Loop"), assembly); + } + + void CompileSummaryModule(string source, string destination, string search) + { + var module = Path.GetFileNameWithoutExtension(source); + var result = Run([LeanExecutable, "--root=" + Path.GetDirectoryName(source), "-o", + Path.Combine(destination, module + ".olean"), source], leanPath: search); + if (result.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidDataException($"The compiled module {module} depends on sorryAx."); + } + + // Publish only compiled modules. A consumer must discharge the target's own byte and metadata checks. + string PublishSummary(string directory, string assembly, string entry, string summarySource) + { + var published = Path.Combine(directory, "Published"); + Directory.CreateDirectory(published); + var producer = Path.Combine(directory, "Producer"); + Run(Extract("extract", assembly, "Examples", entry, producer)); + var reference = Path.Combine(producer, "Reference.lean"); + var generated = File.ReadAllText(Path.Combine(producer, "Extracted.lean")); + File.WriteAllText(reference, generated.Replace("Extracted", "Reference", StringComparison.Ordinal)); + var search = published + Path.PathSeparator + CheckoutLeanPath(); + CompileSummaryModule(reference, published, search); + CompileSummaryModule(summarySource, published, search); + return published; + } + + void CheckSavedBranches(string directory, string assembly) + { + const string entry = "System.UInt64 Examples.Arithmetic::Choose(System.Boolean,System.UInt64,System.UInt64)"; + var published = PublishSummary(directory, assembly, entry, At("Tests/SummaryModules/Branch/SavedArms.lean")); + var changed = Path.Combine(directory, "Examples.dll"); + WriteInstructionChange(assembly, changed, "Choose", [0x02, 0x2d, 0x02, 0x04, 0x2a, 0x03, 0x2a], 1, 0x2c); + var consumer = Path.Combine(directory, "Reversed"); + Run(Extract("extract", changed, "Examples", entry, consumer)); + var search = string.Join(Path.PathSeparator, consumer, published, CheckoutLeanPath()); + CompileSummaryModule(Path.Combine(consumer, "Extracted.lean"), consumer, search); + CompileSummaryModule(At("Tests/SummaryModules/Branch/ReversedBranch.lean"), consumer, search); + ReplayCheckedModules(["Reference", "SavedArms", "Extracted", "ReversedBranch"], search, + "Kernel replay of both saved return regions and the reversed branch"); + Console.WriteLine("PASS reversing the branch reuses both return regions and refutes the original result"); + + VerifyContractVariant(assembly, "Examples", entry, Path.Combine(directory, "OriginalContract"), changed, + entry, Path.Combine(directory, "WrongContract"), """ + /-- Choose returns the requested argument and preserves every caller memory. -/ + def choose : MethodContract (Bool × W64 × W64) W64 where + args := fun (flag, x, y) => + [.scalar (.i32 (if flag then 1 else 0)), .scalar (.i64 x), .scalar (.i64 y)] + results word := [.scalar (.i64 word)] + pre _ _ := True + footprint _ := [] + post := fun (flag, x, y) initial final result => + final = initial ∧ result = (if flag then x else y) + """, """ + /-- Both input choices satisfy the original compiled entry's contract. -/ + theorem correctness : choose.Holds Extracted.program 0 := by + rintro ⟨flag, x, y⟩ memory _ + obtain ⟨fuel, ran⟩ := ExampleProof.correctness memory flag x y + exact MethodContract.Holds.intro memory (if flag then x else y) ⟨fuel, ran⟩ + (Confined.refl [] memory) ⟨rfl, rfl⟩ + """, """ + /-- The changed condition selects 22 on an input requiring 11. -/ + theorem correctness : choose.Refuted Extracted.program 0 := by + apply MethodContract.Refuted.of_return (input := (true, 11, 22)) (initial := Memory.empty) + (fuel := 4) (final := Memory.empty) (values := [.scalar (.i64 22)]) trivial rfl + rintro ⟨_, result, encoded, _, rfl⟩ + exact (by decide : ¬ ([CIL.Safety.Value.scalar (.i64 22)] = [.scalar (.i64 11)])) encoded + """, example: At("Examples/Proofs/Choose.lean")); + } + + void CheckSavedLoop(string directory, string assembly) + { + const string entry = "System.UInt32 Examples.Loops::CountCalls(System.UInt32)"; + var published = Path.Combine(directory, "Published"); + Directory.CreateDirectory(published); + var producer = Path.Combine(directory, "Producer"); + Run(Extract("extract", assembly, "Examples", entry, producer)); + var search = published + Path.PathSeparator + CheckoutLeanPath(); + CompileSummaryModule(Path.Combine(producer, "Extracted.lean"), published, search); + CompileSummaryModule(At("Examples/Proofs/CountCallsSummary.lean"), published, search); + + var changed = Path.Combine(directory, "Examples.dll"); + WriteInstructionChange(assembly, changed, "CountCalls", [0x02, 0x0a, 0x16, 0x0b], 2, 0x17, + typeName: "Loops", matchPrefix: true); + var consumer = Path.Combine(directory, "Consumer"); + Run(Extract("extract", changed, "Examples", entry, consumer)); + var changedSource = Path.Combine(consumer, "Changed.lean"); + File.WriteAllText(changedSource, File.ReadAllText(Path.Combine(consumer, "Extracted.lean")) + .Replace("Extracted", "Changed", StringComparison.Ordinal)); + CompileSummaryModule(changedSource, published, search); + CompileSummaryModule(At("Tests/SummaryModules/Loop/ChangedLoop.lean"), published, search); + ReplayCheckedModules(["Extracted", "CountCallsSummary", "Changed", "ChangedLoop"], search, + "Kernel replay of the saved loop, its callee and the changed accumulator"); + Console.WriteLine("PASS the changed entry reuses the compiled CountCalls invariant and termination proof"); + + VerifyContractVariant(assembly, "Examples", entry, Path.Combine(directory, "OriginalContract"), changed, + entry, Path.Combine(directory, "WrongContract"), """ + /-- CountCalls returns its input from empty memory. -/ + def count : MethodContract W32 where + args input := [.scalar (.i32 input)] + pre _ memory := memory = Memory.empty + footprint _ := [] + post input _ _ values := values = [.scalar (.i32 input)] + """, """ + /-- The original loop returns the number of calls for every input. -/ + theorem correctness : count.Holds Extracted.program 0 := by + rintro value _ rfl + obtain ⟨fuel, final, values, ran, result⟩ := ExampleProof.correctness _ _ ⟨value, rfl, rfl⟩ + exact ⟨fuel, (final, values), ran, by constructor <;> simp [Memory.empty], values, rfl, result⟩ + """, """ + /-- The changed initializer returns one for zero calls. -/ + theorem correctness : count.Refuted Extracted.program 0 := by + apply MethodContract.Refuted.of_return (input := 0) (initial := Memory.empty) + (fuel := 12) (values := [.scalar (.i32 1)]) rfl rfl + rintro ⟨_, values, rfl, wrong⟩ + exact (by decide : ¬ ([CIL.Safety.Value.scalar (.i32 1)] = [.scalar (.i32 0)])) wrong + """, example: At("Examples/Proofs/CountCalls.lean")); + } + + void CheckSavedSummary(string directory, string assembly, string entry, string summarySource, + params (string Name, string Image, string Entry, string Mismatch)[] variants) + { + var published = PublishSummary(directory, assembly, entry, summarySource); + var consumerSource = At("Tests/SummaryModules/SummaryConsumer.lean"); + var producerPath = published + Path.PathSeparator + CheckoutLeanPath(); + var savedProof = Path.Combine(published, "SavedSummary.olean"); + var savedHash = Sha(savedProof); + CheckConsumer("SameEntry", assembly, entry); + foreach (var variant in variants) + CheckConsumer(variant.Name, variant.Image, variant.Entry, variant.Mismatch); + if (savedHash != Sha(savedProof)) + throw new InvalidDataException($"The published summary for {entry} changed during reuse."); + + void CheckConsumer(string name, string image, string entry, string? mismatch = null) + { + var consumer = Path.Combine(directory, name); + Run(Extract("extract", image, "Examples", entry, consumer, "x64-v3")); + var search = consumer + Path.PathSeparator + producerPath; + CompileSummaryModule(Path.Combine(consumer, "Extracted.lean"), consumer, search); + if (mismatch is null) + { + CompileSummaryModule(consumerSource, consumer, search); + ReplayCheckedModules(["Reference", "SavedSummary", "Extracted", "SummaryConsumer"], search, + $"Joint kernel replay of the saved summary and x64-v3 consumer of {entry}"); + Console.WriteLine($"PASS the x64-v3 extraction reuses the compiled scalar summary of {entry}"); + return; + } + + var lines = File.ReadAllLines(consumerSource); + int Line(string fact) => Array.FindIndex(lines, + line => line.StartsWith(" have " + fact + " :", StringComparison.Ordinal)) + 1; + var result = Execute([LeanExecutable, "--root=" + Path.GetDirectoryName(consumerSource), consumerSource], + leanPath: search); + var errors = result.Output.Split('\n').Where(line => line.Contains(": error:", StringComparison.Ordinal)).ToArray(); + bool RejectedAt(string fact) => errors.Any(line => + line.Contains($"SummaryConsumer.lean:{Line(fact)}:", StringComparison.Ordinal) && + line.Contains("cil_kernel_rfl: the kernel does not accept the two sides as equal", StringComparison.Ordinal)); + if (result.Code == 0 || errors.Length != 2 || !RejectedAt(mismatch) || !RejectedAt("record")) + throw new InvalidDataException($"Unexpected saved summary result for {name}: {result.Output}"); + Console.WriteLine($"PASS {name} rejects the saved summary at its {mismatch} and evidence checks"); + } + } + + // Replace one opcode in the selected method, preserving its header, operands, token and image length. + static void WriteInstructionChange(string assembly, string output, string name, byte[] expected, + int position, byte opcode, string typeName = "Arithmetic", bool matchPrefix = false) + { + var bytes = File.ReadAllBytes(assembly); + using var pe = new PEReader(new MemoryStream(bytes)); + var metadata = pe.GetMetadataReader(); + var type = metadata.TypeDefinitions.Select(metadata.GetTypeDefinition).Single(type => + metadata.StringComparer.Equals(type.Namespace, "Examples") && metadata.StringComparer.Equals(type.Name, typeName)); + var method = type.GetMethods().Select(metadata.GetMethodDefinition).Single(method => + metadata.StringComparer.Equals(method.Name, name) && + Matches(pe.GetMethodBody(method.RelativeVirtualAddress).GetILBytes()!)); + bool Matches(byte[] code) => matchPrefix ? code.AsSpan().StartsWith(expected) : + code.AsSpan().SequenceEqual(expected); + int rva = method.RelativeVirtualAddress; + var section = pe.PEHeaders.SectionHeaders[pe.PEHeaders.GetContainingSectionIndex(rva)]; + int body = section.PointerToRawData + rva - section.VirtualAddress; + var header = (bytes[body] & 3) == 2 ? 1 : (bytes[body + 1] >> 4) * 4; + var il = pe.GetMethodBody(rva).GetILBytes()!; + if ((uint)position >= expected.Length || !bytes.AsSpan(body + header, il.Length).SequenceEqual(il)) + throw new InvalidDataException($"Unexpected method body for {name}."); + bytes[body + header + position] = opcode; + File.WriteAllBytes(output, bytes); + } +} diff --git a/Tests/SavedWordReaderOutlines.cs b/Tests/SavedWordReaderOutlines.cs index 434ca6060..bf9564ce8 100644 --- a/Tests/SavedWordReaderOutlines.cs +++ b/Tests/SavedWordReaderOutlines.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include WordReaderOutlineSupport.cs using System.Text.RegularExpressions; diff --git a/Tests/ScalarEvidence.lean b/Tests/ScalarEvidence.lean index d3d9cd741..a7eda245c 100644 --- a/Tests/ScalarEvidence.lean +++ b/Tests/ScalarEvidence.lean @@ -1,5 +1,8 @@ import CIL.Extraction.Translation import CIL.Safety.Frames +import Tests.BodyEncoding +import Tests.Support.ModelOperands +import Tests.Support.RawDecoding /-! Certificate regression fixtures, evaluated against the checked CIL extraction model. @@ -8,22 +11,30 @@ Each conjunction retains its positive and negative obligations. -/ namespace Tests.ScalarEvidence -open CIL CIL.Extraction +open CIL CIL.Extraction Tests.BodyEncoding +open Tests.MetadataBuilder (objectPlatform) /-- The primitive assembly identity with its checked byte representation. -/ private def runtime := runtimeScope +example : scalarType (.atom coreLibraryScope "System.Int32") = .word32 ∧ + scalarType (.atom coreLibraryScope "System.Int64") = .word64 ∧ + scalarType (.atom coreLibraryScope "System.IntPtr") = .native ∧ + scalarType (.atom "System.Private.CoreLib" "System.Int32") = .other := by decide +kernel + private def word : TypeExpr := .atom runtime "System.UInt64" private def intType : TypeExpr := .atom runtime "System.Int32" private def nativeType : TypeExpr := .atom runtime "System.IntPtr" private def i (opcode : Nat) (operand : RawOperand := .none) : RawInstruction := ⟨0, opcode, operand⟩ /-- The pinned assembly owning `Scope::Fixture`; every certificate attributes its bodies to it. -/ private def scope : AssemblyPin := - { identity := "Scope" } + { identity := "Scope, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } private def raw (code : List RawInstruction) (parameters : List TypeExpr := [word, word]) : RawMethod := { - token := 1, owner := "Scope::Fixture", hasThis := false, explicitThis := false, - callingConvention := 0, attributes := 0, implementation := 0, + token := 0x06000001, owner := scope.identity ++ "::Fixture", hasThis := false, + explicitThis := false, callingConvention := 0, attributes := 0, implementation := 0, returnType := word, parameters, locals := [], initLocals := true, maxStack := 2, genericParameters := [], code } +/-- The declared signature of the two-word-parameter, word-returning methods `raw` builds. -/ +private def wordPair : DeclaredSignature := { parameters := [word, word], returnType := word } private def state (stack : List (Option W32)) : Option FeatureState := some ⟨stack, []⟩ private def unknown (count : Nat) := state (List.replicate count none) @@ -37,164 +48,278 @@ private theorem nativeShiftKinds : /-- Native-count certificates bind both the exact shift operation and the count's metadata. -/ private theorem nativeShiftCertificates : - ([(98, Op.shl), (99, Op.shr), (100, Op.shrUn)].all fun (opcode, operation) => + ([(98, Instruction.shl), (99, Instruction.shr), (100, Instruction.shr_un)].all + fun (opcode, operation) => let method := raw [i 2, i 3, i opcode, i 42] [word, nativeType] let features := [unknown 0, unknown 1, unknown 2, unknown 1] let states := [some [], some [ScalarKind.word64], some [.native, .word64], some [.word64]] - let code := [.arg 0, .arg 1, operation, .ret] - decide (ScalarFlowValid #[] method features states code) && - !decide (ScalarFlowValid #[] { method with parameters := [word, word] } features states code) && - !decide (ScalarFlowValid #[] method features states (code.set 2 .add))) = true := by decide +kernel + let code := [.ldarg 0, .ldarg 1, operation, .ret] + decide (ScalarFlowValid method features states code) && + !decide (ScalarFlowValid { method with parameters := [word, word] } features states code) && + !decide (ScalarFlowValid method features states (code.set 2 .add))) = true := by + decide +kernel #print axioms nativeShiftCertificates -private def arithmetic : List (Nat × Op) := [ - (88, .add), (89, .sub), (90, .mul), (91, .divide true false), (92, .divide false false), - (93, .divide true true), (94, .divide false true), (95, .band), (96, .bor), (97, .bxor), - (98, .shl), (99, .shr), (100, .shrUn), (214, .checkedArithmetic .add true), - (215, .checkedArithmetic .add false), (216, .checkedArithmetic .mul true), - (217, .checkedArithmetic .mul false), (218, .checkedArithmetic .sub true), - (219, .checkedArithmetic .sub false), (65025, .eq), (65026, .gt), (65027, .gtu), - (65028, .lt), (65029, .ltu)] -example : arithmetic.all (fun (opcode, operation) => scalarOpMatches (i opcode) [.word32, .word32] operation) = true := by decide +private def arithmetic : List (Nat × Instruction) := [ + (88, .add), (89, .sub), (90, .mul), (91, .div), (92, .div_un), (93, .rem), (94, .rem_un), + (95, .and), (96, .or), (97, .xor), (98, .shl), (99, .shr), (100, .shr_un), (214, .add_ovf), + (215, .add_ovf_un), (216, .mul_ovf), (217, .mul_ovf_un), (218, .sub_ovf), + (219, .sub_ovf_un), (65025, .ceq), (65026, .cgt), (65027, .cgt_un), + (65028, .clt), (65029, .clt_un)] +example : arithmetic.all (fun (opcode, operation) => + scalarOpMatches none (i opcode) [.word32, .word32] operation) = true := by decide example : arithmetic.all (fun (opcode, _operation) => - !scalarOpMatches (i opcode) [.word32, .word32] (if opcode == 88 then .sub else .add)) = true := by decide -example : scalarOpMatches (i 101) [.word64] .neg = true := by decide -example : scalarOpMatches (i 102) [.word64] .bnot = true := by decide -example : scalarOpMatches (i 101) [.word64] .bnot = false := by decide -example : scalarOpMatches (i 88 (.integer 0)) [.word64, .word64] .add = false := by decide -example : scalarOpMatches (i 88) [.other, .other] .add = false := by decide -example : scalarOpMatches (i 88) [.reference, .word32] .add = false := by decide -example : scalarOpMatches (i 88) [.native, .word64] (.nativeBinary .add) = false := by decide -example : scalarOpMatches (i 88) [.word32, .native] (.nativeBinary .add) = true := by decide -example : scalarOpMatches (i 88) [.word32, .native] .add = false := by decide -example : scalarOpMatches (i 88) [.word64, .word64] (.nativeBinary .add) = false := by decide -example : scalarOpMatches (i 88) [.word32, .native] (.nativeBinary .sub) = false := by decide - -private def arithmeticFlagMutations : List (Nat × Op) := [ - (91, .divide false false), (91, .divide true true), (91, .divide false true), - (92, .divide true false), (92, .divide false true), (92, .divide true true), - (93, .divide false true), (93, .divide true false), (93, .divide false false), - (94, .divide true true), (94, .divide false false), (94, .divide true false), - (214, .checkedArithmetic .add false), (215, .checkedArithmetic .add true), - (216, .checkedArithmetic .mul false), (217, .checkedArithmetic .mul true), - (218, .checkedArithmetic .sub false), (219, .checkedArithmetic .sub true)] + !scalarOpMatches none (i opcode) [.word32, .word32] (if opcode == 88 then .sub else .add)) = + true := by decide +example : scalarOpMatches none (i 101) [.word64] .neg = true := by decide +example : scalarOpMatches none (i 102) [.word64] .not = true := by decide +example : scalarOpMatches none (i 101) [.word64] .not = false := by decide +example : scalarOpMatches none (i 88 (.integer 0)) [.word64, .word64] .add = false := by decide +example : scalarOpMatches none (i 88) [.other, .other] .add = false := by decide +example : scalarOpMatches none (i 88) [.reference, .word32] .add = true := by decide +example : scalarOpMatches none (i 88) [.native, .word64] .add = false := by decide +example : scalarOpMatches none (i 88) [.word32, .native] .add = true := by decide +example : scalarOpMatches none (i 88) [.word64, .word64] .sub = false := by decide +example : scalarOpMatches none (i 88) [.word32, .native] .sub = false := by decide + +private def arithmeticFlagMutations : List (Nat × Instruction) := [ + (91, .div_un), (91, .rem), (91, .rem_un), (92, .div), (92, .rem_un), (92, .rem), + (93, .rem_un), (93, .div), (93, .div_un), (94, .rem), (94, .div_un), (94, .div), + (214, .add_ovf_un), (215, .add_ovf), (216, .mul_ovf_un), (217, .mul_ovf), + (218, .sub_ovf_un), (219, .sub_ovf)] example : arithmeticFlagMutations.all (fun (opcode, operation) => - !scalarOpMatches (i opcode) [.word64, .word64] operation) = true := by decide - -private def conversions : List (Nat × Op) := [ - (103, .convI1), (104, .convI2), (105, .convI4), (106, .convI8), (109, .convU4), (110, .convU8), - (130, .checkedConvert 8 true true), (131, .checkedConvert 16 true true), - (132, .checkedConvert 32 true true), (133, .checkedConvert 64 true true), - (134, .checkedConvert 8 false true), (135, .checkedConvert 16 false true), - (136, .checkedConvert 32 false true), (137, .checkedConvert 64 false true), - (138, .checkedConvert 64 true true), (139, .checkedConvert 64 false true), - (179, .checkedConvert 8 true false), (180, .checkedConvert 8 false false), - (181, .checkedConvert 16 true false), (182, .checkedConvert 16 false false), - (183, .checkedConvert 32 true false), (184, .checkedConvert 32 false false), - (185, .checkedConvert 64 true false), (186, .checkedConvert 64 false false), - (209, .convU2), (210, .convU1), (211, .convI8), (212, .checkedConvert 64 true false), - (213, .checkedConvert 64 false false), (224, .convU)] -example : conversions.all (fun (opcode, operation) => scalarOpMatches (i opcode) [.word64] operation) = true := by decide -example : scalarOpMatches (i 106) [.word32] .convU8 = false := by decide -example : scalarOpMatches (i 133) [.word32] (.checkedConvert 64 true false) = false := by decide -example : scalarOpMatches (i 133) [.word32] (.checkedConvert 64 false true) = false := by decide -example : scalarOpMatches (i 133) [.word32] .convI8 = false := by decide -example : scalarOpMatches (i 106) [.word32] (.checkedConvert 64 true false) = false := by decide -example : scalarOpMatches (i 183) [.word64] (.checkedConvert 16 true false) = false := by decide - -example : scalarOpMatches (i 21) [] (.const32 (BitVec.ofInt 32 (-1))) = true := by decide -example : scalarOpMatches (i 30) [] (.const32 8) = true := by decide -example : scalarOpMatches (i 31 (.integer (-128))) [] (.const32 (BitVec.ofInt 32 (-128))) = true := by decide -example : scalarOpMatches (i 31 (.integer 128)) [] (.const32 128) = false := by decide -example : scalarOpMatches (i 32 (.integer (-2147483648))) [] (.const32 2147483648) = true := by decide -example : scalarOpMatches (i 32 (.integer 2147483648)) [] (.const32 2147483648) = false := by decide -example : scalarOpMatches (i 33 (.integer (-9223372036854775808))) [] (.const64 9223372036854775808) = true := by decide -example : scalarOpMatches (i 33 (.integer 9223372036854775807)) [] (.const64 9223372036854775807) = true := by decide -example : scalarOpMatches (i 33 (.integer (-1))) [] (.const64 18446744073709551615) = true := by decide -example : scalarOpMatches (i 33 (.integer (-9223372036854775808))) [] (.const64 0) = false := by decide -example : scalarOpMatches (i 33 (.integer (-9223372036854775808))) [] (.const64 9223372036854775809) = false := by decide -example : scalarOpMatches (i 33 (.integer 9223372036854775808)) [] (.const64 9223372036854775808) = false := by decide -example : scalarOpMatches (i 33) [] (.const64 0) = false := by decide + !scalarOpMatches none (i opcode) [.word64, .word64] operation) = true := by decide + +private def conversions : List (Nat × Instruction) := [ + (103, .conv_i1), (104, .conv_i2), (105, .conv_i4), (106, .conv_i8), (109, .conv_u4), + (110, .conv_u8), + (130, .conv_ovf_un .i1), (131, .conv_ovf_un .i2), (132, .conv_ovf_un .i4), + (133, .conv_ovf_un .i8), (134, .conv_ovf_un .u1), (135, .conv_ovf_un .u2), + (136, .conv_ovf_un .u4), (137, .conv_ovf_un .u8), (138, .conv_ovf_un .i), + (139, .conv_ovf_un .u), + (179, .conv_ovf .i1), (180, .conv_ovf .u1), (181, .conv_ovf .i2), (182, .conv_ovf .u2), + (183, .conv_ovf .i4), (184, .conv_ovf .u4), (185, .conv_ovf .i8), (186, .conv_ovf .u8), + (209, .conv_u2), (210, .conv_u1), (211, .conv_i), (212, .conv_ovf .i), (213, .conv_ovf .u), + (224, .conv_u)] +example : conversions.all (fun (opcode, operation) => + scalarOpMatches none (i opcode) [.word64] operation) = true := by decide +example : scalarOpMatches none (i 106) [.word32] .conv_u8 = false := by decide +example : scalarOpMatches none (i 133) [.word32] (.conv_ovf .i8) = false := by decide +example : scalarOpMatches none (i 133) [.word32] (.conv_ovf_un .u8) = false := by decide +example : scalarOpMatches none (i 133) [.word32] .conv_i8 = false := by decide +example : scalarOpMatches none (i 106) [.word32] (.conv_ovf .i8) = false := by decide +example : scalarOpMatches none (i 183) [.word64] (.conv_ovf .i2) = false := by decide +-- A native-sized opcode and its 64-bit sibling have the same semantics but distinct constructors, +-- so the decoded opcode is recoverable from the emitted instruction. +private def nativeSiblings : List (Nat × Instruction) := [ + (133, .conv_ovf_un .i), (138, .conv_ovf_un .i8), (137, .conv_ovf_un .u), + (139, .conv_ovf_un .u8), (185, .conv_ovf .i), (212, .conv_ovf .i8), (186, .conv_ovf .u), + (213, .conv_ovf .u8), (106, .conv_i), (211, .conv_i8)] +example : nativeSiblings.all (fun (opcode, operation) => + !scalarOpMatches none (i opcode) [.word64] operation) = true := by decide +kernel + +/-- The ECMA-335 III constructor of each division, overflow-checked, wrapping arithmetic and + integer conversion opcode with two stack operands. -/ +private def binaryTable : List (Nat × Instruction) := [ + (88, .add), (89, .sub), (90, .mul), (91, .div), (92, .div_un), (93, .rem), (94, .rem_un), + (214, .add_ovf), (215, .add_ovf_un), (216, .mul_ovf), (217, .mul_ovf_un), (218, .sub_ovf), + (219, .sub_ovf_un)] +/-- The same for the conversion opcodes, which take one stack operand. -/ +private def unaryTable : List (Nat × Instruction) := [ + (103, .conv_i1), (104, .conv_i2), (105, .conv_i4), (106, .conv_i8), (109, .conv_u4), + (110, .conv_u8), (209, .conv_u2), (210, .conv_u1), (211, .conv_i), (224, .conv_u), + (130, .conv_ovf_un .i1), (131, .conv_ovf_un .i2), (132, .conv_ovf_un .i4), + (133, .conv_ovf_un .i8), (134, .conv_ovf_un .u1), (135, .conv_ovf_un .u2), + (136, .conv_ovf_un .u4), (137, .conv_ovf_un .u8), (138, .conv_ovf_un .i), + (139, .conv_ovf_un .u), + (179, .conv_ovf .i1), (180, .conv_ovf .u1), (181, .conv_ovf .i2), (182, .conv_ovf .u2), + (183, .conv_ovf .i4), (184, .conv_ovf .u4), (185, .conv_ovf .i8), (186, .conv_ovf .u8), + (212, .conv_ovf .i), (213, .conv_ovf .u)] +/-- The opcode and emitted constructor pairs, over every opcode of both tables and every + constructor they name, where acceptance at `kind` operands differs from the table. The + tables name each constructor once, so a row's own position identifies its constructor. -/ +private def tableMismatches (kind : ScalarKind) : List (Nat × Instruction) := + let table := binaryTable ++ unaryTable + let stacks := binaryTable.map (fun _ => [kind, kind]) ++ unaryTable.map fun _ => [kind] + (table.zip stacks).zipIdx.flatMap fun (((opcode, _), stack), row) => + table.zipIdx.filterMap fun ((_, candidate), column) => + if scalarOpMatches none (i opcode) stack candidate == (row == column) then none + else some (opcode, candidate) +/-- Each opcode accepts exactly its own constructor among all 43, at both word widths, so every + constructor has its opcode and rejects every sibling. -/ +private theorem scalarTableExact : + tableMismatches .word64 = [] ∧ tableMismatches .word32 = [] := by decide +kernel +#print axioms scalarTableExact + +example : scalarOpMatches none (i 21) [] (.ldc_i4 (BitVec.ofInt 32 (-1))) = true := by decide +example : scalarOpMatches none (i 30) [] (.ldc_i4 8) = true := by decide +example : scalarOpMatches none (i 31 (.integer (-128))) [] + (.ldc_i4 (BitVec.ofInt 32 (-128))) = true := by decide +example : scalarOpMatches none (i 31 (.integer 128)) [] (.ldc_i4 128) = false := by decide +example : scalarOpMatches none (i 32 (.integer (-2147483648))) [] + (.ldc_i4 2147483648) = true := by decide +example : scalarOpMatches none (i 32 (.integer 2147483648)) [] + (.ldc_i4 2147483648) = false := by decide +example : scalarOpMatches none (i 33 (.integer (-9223372036854775808))) [] + (.ldc_i8 9223372036854775808) = true := by decide +example : scalarOpMatches none (i 33 (.integer 9223372036854775807)) [] + (.ldc_i8 9223372036854775807) = true := by decide +example : scalarOpMatches none (i 33 (.integer (-1))) [] + (.ldc_i8 18446744073709551615) = true := by decide +example : scalarOpMatches none (i 33 (.integer (-9223372036854775808))) [] + (.ldc_i8 0) = false := by decide +example : scalarOpMatches none (i 33 (.integer (-9223372036854775808))) [] + (.ldc_i8 9223372036854775809) = false := by decide +example : scalarOpMatches none (i 33 (.integer 9223372036854775808)) [] + (.ldc_i8 9223372036854775808) = false := by decide +example : scalarOpMatches none (i 33) [] (.ldc_i8 0) = false := by decide private def addRaw := raw [i 2, i 3, i 88, i 42] private def addFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 2, unknown 1] private def addStates : ScalarCertificate := [some [], some [.word64], some [.word64, .word64], some [.word64]] -private def addCode : List Op := [.arg 0, .arg 1, .add, .ret] -private def addEvidence : MethodEvidence := { - original := addRaw, arguments := [], closed := addRaw, +private def addCode : List Instruction := [.ldarg 0, .ldarg 1, .add, .ret] +private def addEvidence : MethodEvidence := withBody { + body := noBody, original := addRaw, arguments := [], closed := addRaw, parameterInput := { arguments := [.field .unsigned64, .field .unsigned64], locals := [], code := [.load 0, .load 1, .other, .other] }, reachable := List.replicate 4 true, featureStates := addFeatures, scalarStates := addStates, layoutUses := [], assembly := scope } -private def addMethod : Method := { locals := [], returnsValue := true, code := addCode } - -example : TranslationValid [] #[] [addEvidence] [] [addMethod] := by decide -example : ¬ TranslationValid [] #[] [addEvidence] [] [{ addMethod with code := addCode.set 2 .sub }] := by decide -example : ¬ TranslationValid [] #[] [{ addEvidence with scalarStates := addStates.set 2 (some [.native, .word32]) }] [] - [{ addMethod with code := addCode.set 2 (.nativeBinary .add) }] := by decide -example : ¬ ScalarFlowValid #[] { addRaw with maxStack := 1 } addFeatures addStates addCode := by decide -example : ¬ ScalarFlowValid #[] addRaw addFeatures (addStates.set 0 (some [.word64])) addCode := by decide -example : ¬ ScalarFlowValid #[] addRaw addFeatures (addStates.drop 1) addCode := by decide -example : ¬ ScalarFlowValid #[] addRaw addFeatures addStates (addCode.drop 1) := by decide -example : ¬ ScalarFlowValid #[] addRaw (addFeatures.drop 1) addStates addCode := by decide +private def addMethod : Method := + { locals := [], returnsValue := true, declared := wordPair, code := addCode } + +example : TranslationValid [] #[] [] objectPlatform [addEvidence] [] [addMethod] := by + decide +kernel +example : ¬ TranslationValid [] #[] [] objectPlatform [addEvidence] [] + [{ addMethod with code := addCode.set 2 .sub }] := by decide +kernel +/-- A method with the adder’s code and an extra home cannot be the result of its body decode. -/ +theorem addition_rejects_extra_home : + ¬ MethodEvidenceValid [] #[] [] objectPlatform [addEvidence] [] addEvidence + { addMethod with parameterHomes := [(0, .word64)] } := by + intro valid + have body := valid.ofBody? + have original : Method.ofBody? addMethod.profile addMethod.environment addEvidence.links + addEvidence.reachable addEvidence.body addEvidence.original addEvidence.arguments = + some addMethod := by decide +kernel + rw [original] at body + cases body + +#print axioms addition_rejects_extra_home + +-- The declared signature and the layout environment are conjuncts of their own. +example : ¬ DeclaredValid addRaw { addMethod with declared := {} } := by decide +example : ¬ TranslationValid [] #[] [] objectPlatform [addEvidence] [] + [{ addMethod with declared := {} }] := by + decide +kernel +example : ¬ MethodEnvironmentValid [] #[] + { addMethod with environment := { layouts := #[⟨⟨4, []⟩, 4⟩] } } := by decide +example : ¬ TranslationValid [] #[] [] objectPlatform [addEvidence] [] + [{ addMethod with environment := { layouts := #[⟨⟨4, []⟩, 4⟩] } }] := by decide +kernel + +/-- `addMethod` declaring `signature` instead of the `wordPair` its metadata implies. -/ +private def declaring (signature : DeclaredSignature) : Method := + { addMethod with declared := signature } +/-- A changed declaration passes the shape check and fails the signature and evidence checks. -/ +private def wrongDeclarationRejected (signature : DeclaredSignature) : Prop := + MethodShapeValid addRaw (declaring signature) ∧ ¬ DeclaredValid addRaw (declaring signature) ∧ + ¬ MethodEvidenceValid [] #[] [] objectPlatform [addEvidence] [] addEvidence (declaring signature) +instance (signature : DeclaredSignature) : Decidable (wrongDeclarationRejected signature) := + inferInstanceAs (Decidable (_ ∧ _ ∧ _)) + +-- One component of the signature differs in each case. +example : wrongDeclarationRejected { wordPair with returnType := intType } := by decide +kernel +example : wrongDeclarationRejected { wordPair with parameters := [word, intType] } := by + decide +kernel +example : wrongDeclarationRejected { wordPair with receiver := some .reference } := by decide +kernel +example : wrongDeclarationRejected { wordPair with parameters := [word] } := by decide +kernel + +example : ¬ TranslationValid [] #[] [] objectPlatform + [{ addEvidence with scalarStates := addStates.set 2 (some [.native, .word32]) }] [] + [addMethod] := by decide +kernel +example : ¬ ScalarFlowValid { addRaw with maxStack := 1 } addFeatures addStates addCode := by decide +example : ¬ ScalarFlowValid addRaw addFeatures (addStates.set 0 (some [.word64])) addCode := by + decide +example : ¬ ScalarFlowValid addRaw addFeatures (addStates.drop 1) addCode := by decide +example : ¬ ScalarFlowValid addRaw addFeatures addStates (addCode.drop 1) := by decide +example : ¬ ScalarFlowValid addRaw (addFeatures.drop 1) addStates addCode := by decide private def binaryEvidence (opcode : Nat) : MethodEvidence := - { addEvidence with original := { addRaw with code := addRaw.code.set 2 (i opcode) }, closed := { addRaw with code := addRaw.code.set 2 (i opcode) } } -private def binaryMethod (operation : Op) : Method := { addMethod with code := addCode.set 2 operation } -private def arithmeticFlagCases : List (Nat × Op) := [ - (91, .divide true false), (92, .divide false false), (93, .divide true true), (94, .divide false true), - (214, .checkedArithmetic .add true), (215, .checkedArithmetic .add false), - (216, .checkedArithmetic .mul true), (217, .checkedArithmetic .mul false), - (218, .checkedArithmetic .sub true), (219, .checkedArithmetic .sub false)] + withBody { addEvidence with original := { addRaw with code := addRaw.code.set 2 (i opcode) }, closed := { addRaw with code := addRaw.code.set 2 (i opcode) } } +private def binaryMethod (operation : Instruction) : Method := + { addMethod with code := addCode.set 2 operation } +private def arithmeticFlagCases : List (Nat × Instruction) := [ + (91, .div), (92, .div_un), (93, .rem), (94, .rem_un), (214, .add_ovf), (215, .add_ovf_un), + (216, .mul_ovf), (217, .mul_ovf_un), (218, .sub_ovf), (219, .sub_ovf_un)] example : arithmeticFlagCases.all (fun (opcode, operation) => - decide (TranslationValid [] #[] [binaryEvidence opcode] [] [binaryMethod operation])) = true := by decide + decide (TranslationValid [] #[] [] objectPlatform [binaryEvidence opcode] [] + [binaryMethod operation])) = true := by decide +kernel example : arithmeticFlagMutations.all (fun (opcode, operation) => - decide (¬ TranslationValid [] #[] [binaryEvidence opcode] [] [binaryMethod operation])) = true := by decide + decide (¬ TranslationValid [] #[] [] objectPlatform [binaryEvidence opcode] [] + [binaryMethod operation])) = true := by decide +kernel private def literalRaw := raw [i 33 (.integer (-9223372036854775808)), i 42] [] private def literalFeatures : FeatureCertificate := [unknown 0, unknown 1] private def literalStates : ScalarCertificate := [some [], some [.word64]] -private def literalEvidence : MethodEvidence := { - original := literalRaw, arguments := [], closed := literalRaw, +private def literalEvidence : MethodEvidence := withBody { + body := noBody, original := literalRaw, arguments := [], closed := literalRaw, parameterInput := { arguments := [], locals := [], code := [.other, .other] }, reachable := [true, true], featureStates := literalFeatures, scalarStates := literalStates, layoutUses := [], assembly := scope } -private def literalMethod : Method := { locals := [], returnsValue := true, code := [.const64 9223372036854775808, .ret] } -example : TranslationValid [] #[] [literalEvidence] [] [literalMethod] := by decide -example : ¬ TranslationValid [] #[] [literalEvidence] [] - [{ literalMethod with code := [.const64 9223372036854775809, .ret] }] := by decide +private def literalMethod : Method := { + locals := [], returnsValue := true, declared := { returnType := word }, + code := [.ldc_i8 9223372036854775808, .ret] } +example : TranslationValid [] #[] [] objectPlatform [literalEvidence] [] [literalMethod] := by + decide +kernel +example : ¬ TranslationValid [] #[] [] objectPlatform [literalEvidence] [] + [{ literalMethod with code := [.ldc_i8 9223372036854775809, .ret] }] := by decide +kernel private def convertRaw := raw [i 2, i 133, i 42] [word] private def convertFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 1] private def convertStates : ScalarCertificate := [some [], some [.word64], some [.word64]] -private def convertEvidence : MethodEvidence := { - original := convertRaw, arguments := [], closed := convertRaw, +private def convertEvidence : MethodEvidence := withBody { + body := noBody, original := convertRaw, arguments := [], closed := convertRaw, parameterInput := { arguments := [.field .unsigned64], locals := [], code := [.load 0, .other, .other] }, reachable := [true, true, true], featureStates := convertFeatures, scalarStates := convertStates, layoutUses := [], assembly := scope } -private def convertMethod : Method := { locals := [], returnsValue := true, code := [.arg 0, .checkedConvert 64 true true, .ret] } -example : TranslationValid [] #[] [convertEvidence] [] [convertMethod] := by decide -example : ¬ TranslationValid [] #[] [convertEvidence] [] - [{ convertMethod with code := [.arg 0, .checkedConvert 64 false true, .ret] }] := by decide -example : ¬ TranslationValid [] #[] [convertEvidence] [] - [{ convertMethod with code := [.arg 0, .checkedConvert 64 true false, .ret] }] := by decide -example : ¬ TranslationValid [] #[] [convertEvidence] [] - [{ convertMethod with code := [.arg 0, .convI8, .ret] }] := by decide +private def convertMethod : Method := { + locals := [], returnsValue := true, declared := { parameters := [word], returnType := word }, + code := [.ldarg 0, .conv_ovf_un .i8, .ret] } +example : TranslationValid [] #[] [] objectPlatform [convertEvidence] [] [convertMethod] := by + decide +kernel +example : ¬ TranslationValid [] #[] [] objectPlatform [convertEvidence] [] + [{ convertMethod with code := [.ldarg 0, .conv_ovf_un .u8, .ret] }] := by decide +kernel +example : ¬ TranslationValid [] #[] [] objectPlatform [convertEvidence] [] + [{ convertMethod with code := [.ldarg 0, .conv_ovf .i8, .ret] }] := by decide +kernel +example : ¬ TranslationValid [] #[] [] objectPlatform [convertEvidence] [] + [{ convertMethod with code := [.ldarg 0, .conv_ovf_un .i, .ret] }] := by decide +kernel +example : ¬ TranslationValid [] #[] [] objectPlatform [convertEvidence] [] + [{ convertMethod with code := [.ldarg 0, .conv_i8, .ret] }] := by decide +kernel private def floatRaw (opcode : Nat) := raw [i 23, i opcode, i 106, i 42] [] private def floatFeatures : FeatureCertificate := [unknown 0, state [some 1], unknown 1, unknown 1] private def floatStates : ScalarCertificate := [some [], some [.word32], some [.word32], some [.word64]] -private def floatEvidence (opcode : Nat) : MethodEvidence := { - original := floatRaw opcode, arguments := [], closed := floatRaw opcode, +private def floatEvidence (opcode : Nat) : MethodEvidence := withBody { + body := noBody, original := floatRaw opcode, arguments := [], closed := floatRaw opcode, parameterInput := { arguments := [], locals := [], code := List.replicate 4 .other }, reachable := List.replicate 4 true, featureStates := floatFeatures, scalarStates := floatStates, layoutUses := [], assembly := scope } -private def floatMethod : Method := { locals := [], returnsValue := true, code := [.const32 1, .neg, .convI8, .ret] } -example : FeatureFlowValid #[] (fun _ => none) (floatRaw 107) .scalar floatFeatures +private def floatMethod : Method := { + locals := [], returnsValue := true, declared := { returnType := word }, + code := [.ldc_i4 1, .neg, .conv_i8, .ret] } +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) (floatRaw 107) .scalar floatFeatures (List.replicate 4 true) floatMethod.code := by decide -example : FeatureFlowValid #[] (fun _ => none) (floatRaw 108) .scalar floatFeatures +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) (floatRaw 108) .scalar floatFeatures (List.replicate 4 true) floatMethod.code := by decide example : scalarTransfer (floatRaw 107) (i 107) [.word32] = some [.other] := by decide example : scalarTransfer (floatRaw 108) (i 108) [.word32] = some [.other] := by decide -example : ¬ TranslationValid [] #[] [floatEvidence 107] [] [floatMethod] := by decide -example : ¬ TranslationValid [] #[] [floatEvidence 108] [] [floatMethod] := by decide -example : ¬ ScalarFlowValid #[] (floatRaw 107) floatFeatures (floatStates.set 2 (some [.other])) floatMethod.code := by decide -example : ¬ ScalarFlowValid #[] (floatRaw 108) floatFeatures (floatStates.set 2 (some [.other])) floatMethod.code := by decide +example : ¬ TranslationValid [] #[] [] objectPlatform [floatEvidence 107] [] [floatMethod] := by + decide +kernel +example : ¬ TranslationValid [] #[] [] objectPlatform [floatEvidence 108] [] [floatMethod] := by + decide +kernel +-- The scalar flow itself rejects them, since the declared signature is right. +example : ¬ ScalarFlowValid (floatRaw 107) floatFeatures floatStates floatMethod.code := by + decide +example : ¬ ScalarFlowValid (floatRaw 108) floatFeatures floatStates floatMethod.code := by + decide +example : MethodShapeValid (floatRaw 107) floatMethod := by decide +example : ¬ ScalarFlowValid (floatRaw 107) floatFeatures (floatStates.set 2 (some [.other])) + floatMethod.code := by decide +example : ¬ ScalarFlowValid (floatRaw 108) floatFeatures (floatStates.set 2 (some [.other])) + floatMethod.code := by decide example : scalarTransfer addRaw (i 80) [.reference] = some [.other] := by decide example : scalarTransfer addRaw (i 150) [.word32, .reference] = none := by decide -- `ldelem.ref` and `stelem.ref` are covered: only an object or null array of class references. @@ -215,119 +340,143 @@ example : scalarTransfer addRaw (i 42) [.word64] = some [] := by decide private def localRaw := { raw [i 23, i 107, i 10, i 6, i 106, i 42] [] with locals := [intType] } private def localFeatures : FeatureCertificate := - ([[], [some 1], [none], [], [none], [none]] : List (List (Option W32))).map fun stack => some ⟨stack, [none]⟩ + ([[], [some 1], [none], [], [none], [none]] : List (List (Option W32))).map fun stack => + some ⟨stack, []⟩ private def localStates : ScalarCertificate := [some [], some [.word32], some [.other], some [], some [.word32], some [.word64]] -private def localEvidence : MethodEvidence := { - original := localRaw, arguments := [], closed := localRaw, +private def localEvidence : MethodEvidence := withBody { + body := noBody, original := localRaw, arguments := [], closed := localRaw, parameterInput := { arguments := [], locals := [.field .signed32], code := List.replicate 6 .other }, reachable := List.replicate 6 true, featureStates := localFeatures, scalarStates := localStates, layoutUses := [], assembly := scope } private def localMethod : Method := { locals := [.i32 0], localKinds := [.word32], returnsValue := true, - code := [.const32 1, .neg, .setLocal 0, .local 0, .convI8, .ret] } -example : FeatureFlowValid #[] (fun _ => none) localRaw .scalar localFeatures + declared := { returnType := word }, + code := [.ldc_i4 1, .neg, .stloc 0, .ldloc 0, .conv_i8, .ret] } +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) localRaw .scalar localFeatures (List.replicate 6 true) localMethod.code := by decide -example : ¬ TranslationValid [] #[] [localEvidence] [] [localMethod] := by decide +example : ¬ TranslationValid [] #[] [] objectPlatform [localEvidence] [] [localMethod] := by decide +kernel +example : ¬ ScalarFlowValid localRaw localFeatures localStates localMethod.code := by decide +example : MethodShapeValid localRaw localMethod := by decide private def argumentRaw := raw [i 23, i 108, i 16 (.argument 0), i 2, i 106, i 42] [intType] private def argumentFeatures : FeatureCertificate := [unknown 0, state [some 1], unknown 1, unknown 0, unknown 1, unknown 1] -private def argumentEvidence : MethodEvidence := { - original := argumentRaw, arguments := [], closed := argumentRaw, +private def argumentEvidence : MethodEvidence := withBody { + body := noBody, original := argumentRaw, arguments := [], closed := argumentRaw, parameterInput := { arguments := [.field .signed32], locals := [], code := [.other, .other, .store 0, .load 0, .other, .other] }, reachable := List.replicate 6 true, featureStates := argumentFeatures, scalarStates := localStates, layoutUses := [], assembly := scope } private def argumentMethod : Method := { locals := [], parameterHomes := [(0, .word32)], returnsValue := true, - code := [.const32 1, .neg, .setLocal 0, .local 0, .convI8, .ret] } -example : FeatureFlowValid #[] (fun _ => none) argumentRaw .scalar argumentFeatures + declared := { parameters := [intType], returnType := word }, + code := [.ldc_i4 1, .neg, .starg 0, .ldarg 0, .conv_i8, .ret] } +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) argumentRaw .scalar argumentFeatures (List.replicate 6 true) argumentMethod.code := by decide -example : ¬ TranslationValid [] #[] [argumentEvidence] [] [argumentMethod] := by decide +example : ¬ TranslationValid [] #[] [] objectPlatform [argumentEvidence] [] [argumentMethod] := by decide +kernel +example : ¬ ScalarFlowValid argumentRaw argumentFeatures localStates argumentMethod.code := by + decide +example : MethodShapeValid argumentRaw argumentMethod := by decide private def mixedRaw := { raw [i 2, i 3, i 88, i 42] [nativeType, intType] with returnType := nativeType } private def mixedStates : ScalarCertificate := [some [], some [.native], some [.word32, .native], some [.native]] -private def mixedCode : List Op := [.arg 0, .arg 1, .nativeBinary .add, .ret] -example : ScalarFlowValid #[] mixedRaw addFeatures mixedStates mixedCode := by decide -example : ¬ ScalarFlowValid #[] mixedRaw addFeatures mixedStates (mixedCode.set 2 .add) := by decide -example : ¬ ScalarFlowValid #[] addRaw addFeatures addStates (addCode.set 2 (.nativeBinary .add)) := by decide -example : ¬ ScalarFlowValid #[] { mixedRaw with parameters := [nativeType, word] } addFeatures mixedStates mixedCode := by decide +private def mixedCode : List Instruction := [.ldarg 0, .ldarg 1, .add, .ret] +example : ScalarFlowValid mixedRaw addFeatures mixedStates mixedCode := by decide +example : ¬ ScalarFlowValid mixedRaw addFeatures mixedStates (mixedCode.set 2 .sub) := by decide +example : ¬ ScalarFlowValid addRaw addFeatures addStates (addCode.set 2 .sub) := by decide +example : ¬ ScalarFlowValid { mixedRaw with parameters := [nativeType, word] } addFeatures + mixedStates mixedCode := by decide private def site : CallSite := { - declaringType := .atom "Scope" "Fixture", - target := .managed 1, typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true, returnType := word, parameterTypes := [word, word] } -example : validateManagedCall [⟨[], addRaw⟩] [] (i 40 (.call site)) true (.call 0 2) = true := by decide -example : validateManagedCall [⟨[], addRaw⟩] [] (i 40 (.call { site with returnType := intType })) true (.call 0 2) = false := by decide -example : validateManagedCall [⟨[], { addRaw with returnType := intType }⟩] [] (i 40 (.call site)) true (.call 0 2) = false := by decide -example : scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.Int32 Owner::M()", parameterCount := 0, parameterTypes := [], returnType := word })) [] = some [.other] := by decide +kernel -theorem external_call_validated : scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.UInt64 Owner::M()", parameterCount := 0, parameterTypes := [] })) [] = some [.word64] := by decide +kernel + declared := { convention := 0, returnType := word, parameters := [word, word] }, + method := { + owner := .atom scope.identity "Fixture", name := name!"", hasThis := false, genericArity := 0, + returnType := word, parameters := [word, word] }, + target := .managed 0x06000001, typeArguments := [], parameterCount := 2, returnsValue := true } +/-- The call linked to method 0 that names `site`'s method, as the extractor emits it. -/ +private def emittedCall (site : CallSite) : Instruction := .call site.method +private def linkedZero : Option Link := some (.direct 0) +example : validateManagedCall [⟨[], addRaw⟩] [] (i 40 (.call site)) true linkedZero + (emittedCall site) = true := by decide +example : validateManagedCall [⟨[], addRaw⟩] [] (i 40 + (.call { site with method.returnType := intType })) + true linkedZero (emittedCall { site with method.returnType := intType }) = false := by decide +example : validateManagedCall [⟨[], { addRaw with returnType := intType }⟩] [] + (i 40 (.call site)) true linkedZero (emittedCall site) = false := by decide +example : scalarTransfer addRaw (i 40 (.call { site with + target := .external "Scope::System.Int32 Owner::M()", parameterCount := 0, + method.parameters := [], method.returnType := word })) [] = some [.other] := by decide +kernel +theorem external_call_validated : scalarTransfer addRaw (i 40 (.call { site with + target := .external "Scope::System.UInt64 Owner::M()", parameterCount := 0, + method.parameters := [] })) [] = some [.word64] := by decide +kernel /-- Generic return kinds agree with the substituted method argument. -/ private theorem generic_return_cases : (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::!!0 Owner::M()", parameterCount := 0, - parameterTypes := [], + method.parameters := [], typeArguments := [word] })) [] = some [.word64]) ∧ (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::!!0 Owner::M()", parameterCount := 0, - parameterTypes := [], + method.parameters := [], typeArguments := [intType] })) [] = some [.other]) := by decide +kernel #print axioms generic_return_cases example : validateManagedCall [⟨[], addRaw⟩] [] - (i 40 (.call { site with parameterTypes := [intType, word] })) true (.call 0 2) = false := by decide + (i 40 (.call { site with method.parameters := [intType, word] })) true linkedZero + (emittedCall { site with method.parameters := [intType, word] }) = false := by decide example : scalarTransfer addRaw (i 40 (.call site)) [.word64, .word64] = some [.word64] := by decide example : scalarTransfer addRaw (i 40 (.call site)) [.other, .word64] = none := by decide /-- Call signatures retain parameter order, receiver kinds and generic substitutions. -/ private theorem call_signature_cases : (scalarTransfer addRaw (i 40 (.call { site with - parameterTypes := [intType, word] })) [.word64, .word32] = some [.word64]) ∧ + method.parameters := [intType, word] })) [.word64, .word32] = some [.word64]) ∧ (scalarTransfer addRaw (i 40 (.call { site with - parameterTypes := [intType, word] })) [.word32, .word64] = none) ∧ + method.parameters := [intType, word] })) [.word32, .word64] = none) ∧ (scalarTransfer addRaw (i 40 (.call { site with - hasThis := true, - parameterTypes := [intType, word] })) [.word64, .word32, .reference] = some [.word64]) ∧ + method.hasThis := true, + method.parameters := [intType, word] })) [.word64, .word32, .reference] = some [.word64]) ∧ (scalarTransfer addRaw (i 40 (.call { site with - hasThis := true, - parameterTypes := [intType, word] })) [.word64, .word32, .other] = none) ∧ + method.hasThis := true, + method.parameters := [intType, word] })) [.word64, .word32, .other] = none) ∧ (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.UInt64 Owner::M(System.Int32)", parameterCount := 1, - parameterTypes := [intType] })) [.word32] = some [.word64]) ∧ + method.parameters := [intType] })) [.word32] = some [.word64]) ∧ (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.UInt64 Owner::M(System.Int32)", parameterCount := 1, - parameterTypes := [word] })) [.word64] = none) ∧ + method.parameters := [word] })) [.word64] = none) ∧ (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.UInt64 Owner::M(!!0)", typeArguments := [intType], parameterCount := 1, - parameterTypes := [intType] })) [.word32] = some [.word64]) ∧ + method.parameters := [intType] })) [.word32] = some [.word64]) ∧ (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.UInt64 Owner::M(!!0)", typeArguments := [intType], parameterCount := 1, - parameterTypes := [word] })) [.word64] = none) ∧ + method.parameters := [word] })) [.word64] = none) ∧ (scalarTransfer addRaw (i 40 (.call { site with - declaringType := .apply (.constructor "Scope" "Owner`1") intType, + method.owner := .apply (.constructor "Scope" "Owner`1") intType, target := .external "Scope::!0 Owner`1::M(!0)", parameterCount := 1, - parameterTypes := [intType], - returnType := intType })) [.word32] = some [.word32]) ∧ + method.parameters := [intType], + method.returnType := intType })) [.word32] = some [.word32]) ∧ (scalarTransfer addRaw (i 40 (.call { site with - declaringType := .apply (.constructor "Scope" "Owner`1") intType, + method.owner := .apply (.constructor "Scope" "Owner`1") intType, target := .external "Scope::!0 Owner`1::M(!0)", parameterCount := 1, - parameterTypes := [word], - returnType := intType })) [.word64] = none) ∧ + method.parameters := [word], + method.returnType := intType })) [.word64] = none) ∧ (scalarTransfer addRaw (i 40 (.call { site with - declaringType := .aggregate "Scope::Owner`2" 0, + method.owner := .aggregate "Scope::Owner`2" 0, target := .external "Scope::!1 Owner`2::M(!0,!1)", - parameterTypes := [intType, word] })) [.word64, .word32] = some [.word64]) ∧ + method.parameters := [intType, word] })) [.word64, .word32] = some [.word64]) ∧ (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.UInt64 Owner::M(Container`2,System.Int32)", - parameterTypes := [.aggregate "Scope::Container`2" 0, + method.parameters := [.aggregate "Scope::Container`2" 0, intType] })) [.word32, .other] = some [.word64]) := by decide +kernel @@ -335,27 +484,32 @@ private theorem call_signature_cases : private def faultLocalRaw := { raw [i 2, i 10, i 6, i 42] [word] with locals := [intType], returnType := intType } private def faultLocalFeatures : FeatureCertificate := - ([[], [none], [], [none]] : List (List (Option W32))).map fun stack => some ⟨stack, [none]⟩ + ([[], [none], [], [none]] : List (List (Option W32))).map fun stack => some ⟨stack, []⟩ private def faultLocalStates : ScalarCertificate := [some [], some [.word64], some [], some [.word32]] -private def faultLocalEvidence : MethodEvidence := { - original := faultLocalRaw, arguments := [], closed := faultLocalRaw, +private def faultLocalEvidence : MethodEvidence := withBody { + body := noBody, original := faultLocalRaw, arguments := [], closed := faultLocalRaw, parameterInput := { arguments := [.field .unsigned64], locals := [.field .signed32], code := [.load 0, .other, .other, .other] }, reachable := List.replicate 4 true, featureStates := faultLocalFeatures, scalarStates := faultLocalStates, layoutUses := [], assembly := scope } private def faultLocalMethod : Method := { locals := [.i32 0], localKinds := [.word32], returnsValue := true, - code := [.arg 0, .setLocal 0, .local 0, .ret] } -example : TranslationValid [] #[] [faultLocalEvidence] [] [faultLocalMethod] := by decide +kernel + declared := { parameters := [word], returnType := intType }, + code := [.ldarg 0, .stloc 0, .ldloc 0, .ret] } +example : TranslationValid [] #[] [] objectPlatform [faultLocalEvidence] [] + [faultLocalMethod] := by + decide +kernel example : scalarTransfer faultLocalRaw (i 10) [.word64] = none := by decide -example : scalarStoreFault faultLocalRaw (i 10) [.word64] (.setLocal 0) = true := by decide -example : scalarStoreFault faultLocalRaw (i 10) [.native] (.setLocal 0) = true := by decide -example : scalarStoreFault faultLocalRaw (i 10) [.other] (.setLocal 0) = false := by decide -example : scalarStoreFault faultLocalRaw (i 10) [.reference] (.setLocal 0) = false := by decide -example : scalarStoreFault faultLocalRaw (i 10) [.word64] (.setLocal 1) = false := by decide +example : scalarStoreFault faultLocalRaw (i 10) [.word64] (.stloc 0) = true := by decide +example : scalarStoreFault faultLocalRaw (i 10) [.native] (.stloc 0) = true := by decide +example : scalarStoreFault faultLocalRaw (i 10) [.other] (.stloc 0) = false := by decide +example : scalarStoreFault faultLocalRaw (i 10) [.reference] (.stloc 0) = false := by decide +example : scalarStoreFault faultLocalRaw (i 10) [.word64] (.stloc 1) = false := by decide example : scalarStoreFault faultLocalRaw (i 10) [.word64] .pop = false := by decide -example : scalarStoreFault { faultLocalRaw with locals := [] } (i 10) [.word64] (.setLocal 0) = false := by decide -example : ¬ ScalarFlowValid #[] faultLocalRaw faultLocalFeatures faultLocalStates - (faultLocalMethod.code.set 1 (.setLocal 1)) := by decide -example : ¬ TranslationValid [] #[] [faultLocalEvidence] [] +example : + scalarStoreFault { faultLocalRaw with locals := [] } (i 10) [.word64] (.stloc 0) = false := by + decide +example : ¬ ScalarFlowValid faultLocalRaw faultLocalFeatures faultLocalStates + (faultLocalMethod.code.set 1 (.stloc 1)) := by decide +example : ¬ TranslationValid [] #[] [] objectPlatform [faultLocalEvidence] [] [{ faultLocalMethod with code := faultLocalMethod.code.set 1 .pop }] := by decide example (memory : Safety.Memory) (reference : Safety.Reference) (bits : W64) : Safety.storeLocal memory (.bytes .word32 reference) (.scalar (.i64 bits)) = .error .invalidState := rfl @@ -365,52 +519,163 @@ example (memory : Safety.Memory) (reference : Safety.Reference) (kind : NarrowKi private def nativeHomeRaw := raw [i 3, i 16 (.argument 0), i 2, i 110, i 42] [nativeType, word] private def nativeHomeFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 0, unknown 1, unknown 1] private def nativeHomeStates : ScalarCertificate := [some [], some [.word64], some [], some [.native], some [.word64]] -private def nativeHomeEvidence : MethodEvidence := { - original := nativeHomeRaw, arguments := [], closed := nativeHomeRaw, +private def nativeHomeEvidence : MethodEvidence := withBody { + body := noBody, original := nativeHomeRaw, arguments := [], closed := nativeHomeRaw, parameterInput := { arguments := [.field .nativeInt, .field .unsigned64], locals := [], code := [.load 1, .store 0, .load 0, .other, .other] }, reachable := List.replicate 5 true, featureStates := nativeHomeFeatures, scalarStates := nativeHomeStates, layoutUses := [], assembly := scope } private def nativeHomeMethod : Method := { locals := [], parameterHomes := [(0, .word64)], returnsValue := true, - code := [.arg 1, .setLocal 0, .local 0, .convU8, .ret] } -example : TranslationValid [] #[] [nativeHomeEvidence] [] [nativeHomeMethod] := by decide +kernel + declared := { parameters := [nativeType, word], returnType := word }, + code := [.ldarg 1, .starg 0, .ldarg 0, .conv_u8, .ret] } +example : TranslationValid [] #[] [] objectPlatform [nativeHomeEvidence] [] + [nativeHomeMethod] := by + decide +kernel example : scalarTransfer nativeHomeRaw (i 16 (.argument 0)) [.word64] = some [] := by decide example : scalarTransfer nativeHomeRaw (i 16 (.argument 0)) [.other] = none := by decide example : scalarTransfer nativeHomeRaw (i 16 (.argument 0)) [.word32] = none := by decide -example : ¬ ScalarFlowValid #[] nativeHomeRaw nativeHomeFeatures +example : ¬ ScalarFlowValid nativeHomeRaw nativeHomeFeatures (nativeHomeStates.set 1 (some [.other])) nativeHomeMethod.code := by decide private def nativeReturnRaw := raw [i 2, i 77, i 42] [.byref word] private def nativeReturnFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 1] private def nativeReturnStates : ScalarCertificate := [some [], some [.reference], some [.native]] -private def nativeReturnEvidence : MethodEvidence := { - original := nativeReturnRaw, arguments := [], closed := nativeReturnRaw, +private def nativeReturnEvidence : MethodEvidence := withBody { + body := noBody, original := nativeReturnRaw, arguments := [], closed := nativeReturnRaw, parameterInput := { arguments := [.reference], locals := [], code := [.load 0, .other, .other] }, reachable := List.replicate 3 true, featureStates := nativeReturnFeatures, scalarStates := nativeReturnStates, layoutUses := [], assembly := scope } -private def nativeReturnMethod : Method := { locals := [], returnsValue := true, code := [.arg 0, .loadScalar 8, .ret] } -example : TranslationValid [] #[] [nativeReturnEvidence] [] [nativeReturnMethod] := by decide +kernel +private def nativeReturnMethod : Method := { + locals := [], returnsValue := true, + declared := { parameters := [.byref word], returnType := word }, + code := [.ldarg 0, .ldind .i, .ret] } +example : TranslationValid [] #[] [] objectPlatform [nativeReturnEvidence] [] + [nativeReturnMethod] := by + decide +kernel example : scalarTransfer nativeReturnRaw (i 42) [.native] = some [] := by decide example : scalarTransfer { nativeReturnRaw with returnType := nativeType } (i 42) [.word64] = some [] := by decide example : scalarTransfer nativeReturnRaw (i 42) [.other] = none := by decide example : scalarTransfer nativeReturnRaw (i 42) [.word32] = none := by decide example : scalarTransfer nativeReturnRaw (i 42) [.native, .word64] = none := by decide -example : ¬ ScalarFlowValid #[] { nativeReturnRaw with returnType := intType } +example : ¬ ScalarFlowValid { nativeReturnRaw with returnType := intType } nativeReturnFeatures nativeReturnStates nativeReturnMethod.code := by decide +/-- Local slot reads as `ScalarFlowValid` evaluates them, from indexes built once per body. -/ +private def indexedLocals (method : RawMethod) : ScalarLocals := + ScalarLocals.indexed method (nativeDispatchContext method) + +/-- `push; stloc.s slot; ldloc.0; ret` over one int32 local. -/ +private def slotRaw (push : RawInstruction) (slot : Nat) : RawMethod := + { raw [push, i 19 (.local slot), i 6, i 42] [] with locals := [intType], returnType := intType } +private def slotFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 0, unknown 1] +private def slotStates (pushed : ScalarKind) : ScalarCertificate := + [some [], some [pushed], some [], some [.word32]] +private def slotCode (push : Instruction) (slot : Nat) : List Instruction := + [push, .stloc slot, .ldloc 0, .ret] + +/-- The indexed local reads accept an int32 store to the declared local and reject a store or + load past the declared locals and a null store into an int32 local, at the site and in the + certificate, as the metadata lists do. -/ +theorem local_slot_sites : + let constant := slotRaw (i 22) 0 + scalarTransfer constant (i 19 (.local 0)) [.word32] (locals := indexedLocals constant) = + some [] ∧ + scalarTransfer constant (i 19 (.local 1)) [.word32] (locals := indexedLocals constant) = + none ∧ + scalarTransfer constant (i 19 (.local 1)) [.word32] = none ∧ + scalarTransfer constant (i 17 (.local 1)) [] (locals := indexedLocals constant) = none ∧ + scalarTransfer constant (i 17 (.local 1)) [] = none ∧ + scalarStoreFault constant (i 19 (.local 1)) [.word64] (.stloc 1) + (indexedLocals constant) = false ∧ + scalarTransfer constant (i 19 (.local 0)) [.nullObject] + (locals := indexedLocals constant) = none ∧ + scalarTransfer constant (i 19 (.local 0)) [.nullObject] = none ∧ + ScalarFlowValid constant slotFeatures (slotStates .word32) (slotCode (.ldc_i4 0) 0) ∧ + ¬ ScalarFlowValid (slotRaw (i 22) 1) slotFeatures (slotStates .word32) + (slotCode (.ldc_i4 0) 1) ∧ + ¬ ScalarFlowValid (slotRaw (i 20) 0) slotFeatures (slotStates .nullObject) + (slotCode .ldnull 0) := by + decide +kernel +#print axioms local_slot_sites + +/-- A static method of no parameters whose address `ldftn` takes and `calli` calls. -/ +private def handler : CallSite := + { declared := { convention := 0 }, + method := { + owner := .void, name := name!"", hasThis := false, genericArity := 0, returnType := .void, + parameters := [] }, + target := .managed 2, typeArguments := [], parameterCount := 0, returnsValue := false } +private def handlerSignature : ManagedSignature := ⟨.void, []⟩ + +/-- `ldftn; stloc store; ldloc.1; calli; ret` over two native-int locals, of which `calli` + consumes only local 1. -/ +private def pointerRaw (store : Nat) : RawMethod := + { raw [i 65030 (.call handler), i store, i 7, i 41 (.indirectCall .void [] false false 0), + i 42] [] with locals := [nativeType, nativeType], returnType := .void } +private def pointerFeatures : FeatureCertificate := + [unknown 0, unknown 1, unknown 0, unknown 1, unknown 0] +private def pointerStates : ScalarCertificate := + [some [], some [.methodPointer handlerSignature], some [], + some [.methodPointer handlerSignature], some []] +private def pointerCode (slot : Nat) : List Instruction := + [.ldftn ⟨"Scope::Fixture", 2, []⟩ handlerSignature, .stloc slot, .ldloc 1, + .calli handlerSignature false, .ret] + +/-- The indexed pointer locals give a method pointer home only to the native-int local a `calli` + consumes: storing a method address into the other local, or taking the consumed local's + address, is rejected at the site and in the certificate, as the metadata lists do. -/ +theorem pointer_local_sites : + let method := pointerRaw 11 + let pointer := ScalarKind.methodPointer handlerSignature + scalarTransfer method (i 11) [pointer] (locals := indexedLocals method) = some [] ∧ + scalarTransfer method (i 10) [pointer] (locals := indexedLocals method) = none ∧ + scalarTransfer method (i 10) [pointer] = none ∧ + scalarTransfer method (i 6) [] (locals := indexedLocals method) = some [.native] ∧ + scalarTransfer method (i 7) [] (locals := indexedLocals method) = some [pointer] ∧ + scalarTransfer method (i 18 (.local 0)) [] (locals := indexedLocals method) = + some [.reference] ∧ + scalarTransfer method (i 18 (.local 1)) [] (locals := indexedLocals method) = none ∧ + scalarTransfer method (i 18 (.local 1)) [] = none ∧ + ScalarFlowValid method pointerFeatures pointerStates (pointerCode 1) ∧ + ¬ ScalarFlowValid (pointerRaw 10) pointerFeatures pointerStates (pointerCode 0) := by + decide +kernel +#print axioms pointer_local_sites + +/-- The closed argument storage, reading pointer locals from a set built once per body, gives a + method pointer home to the local `calli` consumes and native integer storage to the other, + as scanning the pointer local list does; a pointer test that admits no local gives neither + a method pointer home. -/ +theorem pointer_local_storage : + let method := pointerRaw 11 + let context := nativeDispatchContext method + (closedArguments [] method).map (·.locals) = + some [.field .nativeInt, .methodPointer handlerSignature] ∧ + (closedArgumentsWith [] method context context.pointerLocals.contains).map (·.locals) = + some [.field .nativeInt, .methodPointer handlerSignature] ∧ + (closedArgumentsWith [] method context (fun _ => false)).map (·.locals) = + some [.field .nativeInt, .field .nativeInt] := by + decide +kernel +#print axioms pointer_local_storage + private def byteSpanType : TypeExpr := .apply (.constructor runtime "System.ReadOnlySpan`1") (.atom runtime "System.Byte") private def byteSpanSite : CallSite := { - declaringType := byteSpanType, name := ".ctor", target := .external - (runtime ++ "::System.Void System.ReadOnlySpan`1::.ctor(System.Void*,System.Int32)"), - typeArguments := [], parameterCount := 2, parameterTypes := [.pointer .void, intType], - hasThis := true, returnsValue := false, returnType := .void } + declared := { convention := 0x20, parameters := [.pointer .void, intType] }, + method := { + owner := byteSpanType, name := name!".ctor", hasThis := true, genericArity := 0, + returnType := .void, parameters := [.pointer .void, intType] }, + name := ".ctor", + target := .external + (runtime ++ "::System.Void System.ReadOnlySpan`1::.ctor(System.Void*,\ + System.Int32)"), + typeArguments := [], parameterCount := 2, returnsValue := false } private def byteSpanRaw := { raw [i 2, i 31 (.integer 16), i 115 (.call byteSpanSite), i 42] [.byref word] with returnType := byteSpanType } private def byteSpanFeatures : FeatureCertificate := [unknown 0, unknown 1, state [some 16, none], unknown 1] private def byteSpanStates : ScalarCertificate := [some [], some [.reference], some [.word32, .reference], some [.other]] -private def byteSpanCode : List Op := [.arg 0, .const32 16, .memory .spanCreate, .ret] +private def byteSpanCode : List Instruction := + [.ldarg 0, .ldc_i4 16, .newobj byteSpanSite.method, .ret] /-- Span construction checks its exact signature and managed backing reference. -/ private theorem span_constructor_cases : - (ScalarFlowValid #[] byteSpanRaw byteSpanFeatures byteSpanStates byteSpanCode) ∧ - (¬ ScalarFlowValid #[] byteSpanRaw byteSpanFeatures byteSpanStates (byteSpanCode.set 2 (.memory - (.spanItem 1)))) ∧ + (ScalarFlowValid byteSpanRaw byteSpanFeatures byteSpanStates byteSpanCode) ∧ + (¬ ScalarFlowValid byteSpanRaw byteSpanFeatures (byteSpanStates.set 2 (some [.word32, .other])) + byteSpanCode) ∧ (scalarTransfer byteSpanRaw (i 115 (.call byteSpanSite)) [.word32, .reference] = some [.other]) ∧ (scalarTransfer byteSpanRaw (i 115 (.call byteSpanSite)) [.word32, .other] = none) ∧ @@ -420,9 +685,9 @@ private theorem span_constructor_cases : (scalarTransfer byteSpanRaw (i 115 (.call { byteSpanSite with name := "Other" })) [.word32, .reference] = none) ∧ (scalarTransfer byteSpanRaw (i 115 (.call { byteSpanSite with - declaringType := .atom runtime "Other" })) [.word32, .reference] = none) ∧ + method.owner := .atom runtime "Other" })) [.word32, .reference] = none) ∧ (scalarTransfer byteSpanRaw (i 115 (.call { byteSpanSite with - parameterTypes := [.pointer word, intType] })) [.word32, .reference] = none) ∧ + method.parameters := [.pointer word, intType] })) [.word32, .reference] = none) ∧ (scalarTransfer byteSpanRaw (i 40 (.call byteSpanSite)) [.word32, .reference, .reference] = some []) ∧ (scalarTransfer byteSpanRaw (i 40 (.call byteSpanSite)) [.word32, .reference, .other] = @@ -432,63 +697,86 @@ private theorem span_constructor_cases : #print axioms span_constructor_cases private def tupleType : TypeExpr := .aggregate - (runtime ++ "::System.ValueTuple`2") 0 + (s!"{runtime}::System.ValueTuple`2<{runtime}::System.UInt64,{runtime}::System.UInt64>") 0 private def tupleSite : CallSite := { - declaringType := .atom "System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" "System.Runtime.Intrinsics.X86.X86Base/X64", - name := "DivRem", target := .external - "System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51::System.ValueTuple`2 System.Runtime.Intrinsics.X86.X86Base/X64::DivRem(System.UInt64,System.UInt64,System.UInt64)", - typeArguments := [], parameterCount := 3, parameterTypes := [word, word, word], - hasThis := false, returnsValue := true, returnType := tupleType } + declared := { convention := 0, returnType := tupleType, parameters := [word, word, word] }, + method := { + owner := .atom + "System.Runtime.Intrinsics, Version=11.0.0.0, Culture=neutral, \ + PublicKeyToken=cc7b13ffcd2ddd51" + "System.Runtime.Intrinsics.X86.X86Base/X64", name := name!"DivRem", hasThis := false, + genericArity := 0, returnType := tupleType, parameters := [word, word, word] }, + name := "DivRem", + target := .external + "System.Runtime.Intrinsics, Version=11.0.0.0, Culture=neutral, \ + PublicKeyToken=cc7b13ffcd2ddd51::System.ValueTuple`2 \ + System.Runtime.Intrinsics.X86.X86Base/X64::DivRem\ + (System.UInt64,System.UInt64,System.UInt64)", typeArguments := [], parameterCount := 3, + returnsValue := true } private def tupleRaw := { raw [i 2, i 3, i 4, i 40 (.call tupleSite), i 42] [word, word, word] with returnType := tupleType, maxStack := 3 } /-- Tuple results retain their declared input kinds. -/ private theorem tuple_result_cases : - (ScalarFlowValid #[] tupleRaw [unknown 0, unknown 1, unknown 2, unknown 3, unknown 1] [some [], + (ScalarFlowValid tupleRaw [unknown 0, unknown 1, unknown 2, unknown 3, unknown 1] [some [], some [.word64], some [.word64, .word64], some [.word64, .word64, .word64], some [.other]] - [.arg 0, .arg 1, .arg 2, .intrinsic (.x86Base .divRem64) 3, .ret]) ∧ + [.ldarg 0, .ldarg 1, .ldarg 2, .call tupleSite.method {}, .ret]) ∧ (scalarTransfer tupleRaw (i 40 (.call { tupleSite with - parameterTypes := [word, intType, word] })) [.word64, .word32, .word64] = none) := by + method.parameters := [word, intType, word] })) [.word64, .word32, .word64] = none) := by decide +kernel #print axioms tuple_result_cases -private def tupleFieldRaw := raw [i 2, i 123 (.field tupleType 0 word false), i 42] [.byref tupleType] +private def tupleFieldRaw := + raw [i 2, i 123 (.field tupleType 0 word name!"Item1" false), i 42] [.byref tupleType] +/-- The tuple's first field, as its raw operand names it. -/ +private def tupleItem1 : FieldRef := + { owner := tupleType, index := 0, type := word, name := name!"Item1" } /-- Tuple field loads preserve the declared scalar result kind. -/ private theorem tuple_field_cases : - (ScalarFlowValid #[] tupleFieldRaw [unknown 0, unknown 1, unknown 1] [some [], some - [.reference], some [.word64]] [.arg 0, .fieldAt 0 8, .ret]) ∧ - (¬ ScalarFlowValid #[] tupleFieldRaw [unknown 0, unknown 1, unknown 1] [some [], some - [.reference], some [.other]] [.arg 0, .fieldAt 0 8, .ret]) := by + (ScalarFlowValid tupleFieldRaw [unknown 0, unknown 1, unknown 1] [some [], some + [.reference], some [.word64]] [.ldarg 0, .ldfld tupleItem1, .ret]) ∧ + (¬ ScalarFlowValid tupleFieldRaw [unknown 0, unknown 1, unknown 1] [some [], some + [.reference], some [.other]] [.ldarg 0, .ldfld tupleItem1, .ret]) := by decide +kernel #print axioms tuple_field_cases private def spanType : TypeExpr := .apply (.constructor runtime "System.ReadOnlySpan`1") (.atom runtime "System.UInt32") private def spanSite : CallSite := { - declaringType := spanType, name := "get_Item", target := .external - (runtime ++ "::!0& modreq(System.Runtime.InteropServices.InAttribute) System.ReadOnlySpan`1::get_Item(System.Int32)"), - typeArguments := [], parameterCount := 1, parameterTypes := [intType], hasThis := true, returnsValue := true, - returnType := .atom runtime "!0& modreq(System.Runtime.InteropServices.InAttribute)" } + declared := { + convention := 0x20, + returnType := .modified true runtime "System.Runtime.InteropServices.InAttribute" + (.byref (.ownerParameter 0)), parameters := [intType] }, + method := { + owner := spanType, name := name!"get_Item", hasThis := true, genericArity := 0, + returnType := .atom runtime "!0& modreq(System.Runtime.InteropServices.InAttribute)", + parameters := [intType] }, + name := "get_Item", + target := .external + (runtime ++ "::!0& modreq(System.Runtime.InteropServices.InAttribute) \ + System.ReadOnlySpan`1::get_Item(System.Int32)"), + typeArguments := [], parameterCount := 1, returnsValue := true } private def spanRaw := { raw [i 2, i 3, i 40 (.call spanSite), i 113 (.type intType), i 42] [.byref spanType, intType] with returnType := intType } /-- Span and external signatures reject malformed metadata and distinguish Unicode names. -/ private theorem span_signature_cases : - (ScalarFlowValid #[] spanRaw [unknown 0, unknown 1, unknown 2, unknown 1, unknown 1] [some [], - some [.reference], some [.word32, .reference], some [.reference], some [.word32]] [.arg 0, - .arg 1, .memory (.spanItem 4), .loadScalar 4, .ret]) ∧ + (ScalarFlowValid spanRaw [unknown 0, unknown 1, unknown 2, unknown 1, unknown 1] [some [], + some [.reference], some [.word32, .reference], some [.reference], some [.word32]] [.ldarg 0, + .ldarg 1, .call spanSite.method {}, .ldobj intType, .ret]) ∧ (scalarTransfer spanRaw (i 40 (.call { spanSite with - parameterTypes := [word] })) [.word64, .reference] = none) ∧ + method.parameters := [word] })) [.word64, .reference] = none) ∧ (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.UInt64 Owner::M(garbage(System.Int32)", parameterCount := 1, - parameterTypes := [intType] })) [.word32] = none) ∧ + method.parameters := [intType] })) [.word32] = none) ∧ (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.UInt64 Owner::M(Container\u0394)", parameterCount := 1, - parameterTypes := [.aggregate "Scope::Container\u0394" 0] })) [.other] = some [.word64]) ∧ + method.parameters := [.aggregate "Scope::Container\u0394" 0] })) [.other] = + some [.word64]) ∧ (scalarTransfer addRaw (i 40 (.call { site with target := .external "Scope::System.UInt64 Owner::M(Container\u0394)", parameterCount := 1, - parameterTypes := [.aggregate "Scope::Container\u03a9" 0] })) [.other] = none) := by + method.parameters := [.aggregate "Scope::Container\u03a9" 0] })) [.other] = none) := by decide +kernel #print axioms span_signature_cases @@ -496,27 +784,34 @@ private theorem span_signature_cases : private def joinRaw := { raw [i 2, i 45 (.target 4), i 22, i 43 (.target 5), i 22, i 42] [intType] with returnType := intType } private def joinFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 0, state [some 0], unknown 0, unknown 1] private def joinStates : ScalarCertificate := [some [], some [.word32], some [], some [.word32], some [], some [.word32]] -private def joinCode : List Op := [.arg 0, .brnonzero 4, .const32 0, .branch 5, .const32 0, .ret] -example : ScalarFlowValid #[] joinRaw joinFeatures joinStates joinCode := by decide -example : ¬ ScalarFlowValid #[] { joinRaw with code := joinRaw.code.set 4 (i 33 (.integer 0)) } - joinFeatures joinStates (joinCode.set 4 (.const64 0)) := by decide -example : ¬ ScalarFlowValid #[] { joinRaw with code := joinRaw.code.set 4 (i 33 (.integer 0)) } - joinFeatures (joinStates.set 5 (some [.word64])) (joinCode.set 4 (.const64 0)) := by decide +private def joinCode : List Instruction := + [.ldarg 0, .brtrue 4, .ldc_i4 0, .br 5, .ldc_i4 0, .ret] +example : ScalarFlowValid joinRaw joinFeatures joinStates joinCode := by decide +example : ¬ ScalarFlowValid { joinRaw with code := joinRaw.code.set 4 (i 33 (.integer 0)) } + joinFeatures joinStates (joinCode.set 4 (.ldc_i8 0)) := by decide +example : ¬ ScalarFlowValid { joinRaw with code := joinRaw.code.set 4 (i 33 (.integer 0)) } + joinFeatures (joinStates.set 5 (some [.word64])) (joinCode.set 4 (.ldc_i8 0)) := by decide private def loopRaw := raw [i 22, i 38, i 43 (.target 0)] [] private def loopFeatures : FeatureCertificate := [unknown 0, state [some 0], unknown 0] private def loopStates : ScalarCertificate := [some [], some [.word32], some []] -private def loopCode : List Op := [.const32 0, .pop, .branch 0] -example : ScalarFlowValid #[] loopRaw loopFeatures loopStates loopCode := by decide -example : ¬ ScalarFlowValid #[] loopRaw loopFeatures (loopStates.set 1 (some [.word64])) loopCode := by decide -example : ¬ ScalarFlowValid #[] { loopRaw with code := [i 22, i 0, i 43 (.target 0)] } +private def loopCode : List Instruction := [.ldc_i4 0, .pop, .br 0] +example : ScalarFlowValid loopRaw loopFeatures loopStates loopCode := by decide +example : ¬ ScalarFlowValid loopRaw loopFeatures (loopStates.set 1 (some [.word64])) + loopCode := by decide +example : ¬ ScalarFlowValid { loopRaw with code := [i 22, i 0, i 43 (.target 0)] } [unknown 0, state [some 0], state [some 0]] [some [], some [.word32], some [.word32]] - [.const32 0, .nop, .branch 0] := by decide + [.ldc_i4 0, .nop, .br 0] := by decide -- Defaulted signature fields are accepted only where they are the true signature. private def unsignedSite : CallSite := { - target := .managed 1, typeArguments := [], parameterCount := 2, hasThis := false, returnsValue := true } -example : validateManagedCall [⟨[], addRaw⟩] [] (i 40 (.call unsignedSite)) true (.call 0 2) = false := by decide + declared := { convention := 0 }, + method := { + owner := .void, name := name!"", hasThis := false, genericArity := 0, returnType := .void, + parameters := [] }, + target := .managed 0x06000001, typeArguments := [], parameterCount := 2, returnsValue := true } +example : validateManagedCall [⟨[], addRaw⟩] [] (i 40 (.call unsignedSite)) true linkedZero + (emittedCall unsignedSite) = false := by decide example : scalarTransfer addRaw (i 40 (.call unsignedSite)) [.word64, .word64] = none := by decide /-- External unsigned signatures retain their exact parameter and return metadata. -/ private theorem unsigned_signature_cases : @@ -536,23 +831,29 @@ private def holeRaw (opcode : Nat) (operand : RawOperand := .none) := raw [i 2, private def holeFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 2, unknown 1, unknown 0, unknown 1] private def holeStates : ScalarCertificate := [some [], some [.word64], some [.word64, .word64], some [.other], some [], some [.word64]] -private def holeCode (emitted : Op) : List Op := [.arg 0, .arg 1, emitted, .pop, .arg 0, .ret] -example : ScalarFlowValid #[] (holeRaw 88) holeFeatures (holeStates.set 3 (some [.word64])) (holeCode .add) := by decide +private def holeCode (emitted : Instruction) : List Instruction := + [.ldarg 0, .ldarg 1, emitted, .pop, .ldarg 0, .ret] +example : ScalarFlowValid (holeRaw 88) holeFeatures (holeStates.set 3 (some [.word64])) + (holeCode .add) := by decide -- Array loads now require exact array evidence rather than accepting an equal stack effect. -example : ¬ ScalarFlowValid #[] (holeRaw 149) holeFeatures holeStates (holeCode (.memory (.spanItem 4))) := by decide -example : ¬ ScalarFlowValid #[] (holeRaw 149) holeFeatures holeStates (holeCode .add) := by decide -example : ¬ ScalarFlowValid #[] (holeRaw 143 (.type intType)) holeFeatures holeStates (holeCode (.divide true false)) := by decide -example : ¬ ScalarFlowValid #[] (holeRaw 149) holeFeatures holeStates (holeCode (.nativeBinary .add)) := by decide -private def holeEvidence (opcode : Nat) (states : ScalarCertificate) : MethodEvidence := { - original := holeRaw opcode, arguments := [], closed := holeRaw opcode, +example : ¬ ScalarFlowValid (holeRaw 149) holeFeatures holeStates + (holeCode (.ldelema intType false)) := by decide +example : ¬ ScalarFlowValid (holeRaw 149) holeFeatures holeStates (holeCode .add) := by decide +example : ¬ ScalarFlowValid (holeRaw 143 (.type intType)) holeFeatures holeStates + (holeCode .div) := by decide +private def holeEvidence (opcode : Nat) (states : ScalarCertificate) : MethodEvidence := withBody { + body := noBody, original := holeRaw opcode, arguments := [], closed := holeRaw opcode, parameterInput := { arguments := [.field .unsigned64, .field .unsigned64], locals := [], code := [.load 0, .load 1, .other, .other, .load 0, .other] }, reachable := List.replicate 6 true, featureStates := holeFeatures, scalarStates := states, layoutUses := [], assembly := scope } -private def holeMethod (emitted : Op) : Method := { locals := [], returnsValue := true, code := holeCode emitted } +private def holeMethod (emitted : Instruction) : Method := + { locals := [], returnsValue := true, declared := wordPair, code := holeCode emitted } /-- A scalar opcode cannot be replaced by an unrelated instruction with the same stack effect. -/ private theorem scalar_opcode_cases : - (TranslationValid [] #[] [holeEvidence 88 (holeStates.set 3 (some [.word64]))] [] + (TranslationValid [] #[] [] objectPlatform + [holeEvidence 88 (holeStates.set 3 (some [.word64]))] [] [holeMethod .add]) ∧ - (¬ TranslationValid [] #[] [holeEvidence 149 holeStates] [] [holeMethod .add]) := by + (¬ TranslationValid [] #[] [] objectPlatform [holeEvidence 149 holeStates] [] + [holeMethod .add]) := by decide +kernel #print axioms scalar_opcode_cases @@ -560,152 +861,531 @@ private theorem scalar_opcode_cases : private def unaryHoleRaw (opcode : Nat) := raw [i 2, i opcode, i 38, i 2, i 42] [word] private def unaryHoleFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 1, unknown 0, unknown 1] private def unaryHoleStates : ScalarCertificate := [some [], some [.word64], some [.other], some [], some [.word64]] -private def unaryHoleCode (emitted : Op) : List Op := [.arg 0, emitted, .pop, .arg 0, .ret] -example : ScalarFlowValid #[] (unaryHoleRaw 108) unaryHoleFeatures unaryHoleStates (unaryHoleCode (.loadScalar 8)) := by decide -example : ¬ ScalarFlowValid #[] (unaryHoleRaw 108) unaryHoleFeatures unaryHoleStates (unaryHoleCode .convI8) := by decide -example : ¬ ScalarFlowValid #[] (unaryHoleRaw 107) unaryHoleFeatures unaryHoleStates - (unaryHoleCode (.checkedConvert 64 true false)) := by decide +private def unaryHoleCode (emitted : Instruction) : List Instruction := + [.ldarg 0, emitted, .pop, .ldarg 0, .ret] +-- An access form is reserved for the opcodes that decide its width, so no conversion emits one. +example : ¬ ScalarFlowValid (unaryHoleRaw 108) unaryHoleFeatures unaryHoleStates + (unaryHoleCode (.ldind .i8)) := by decide +example : ¬ ScalarFlowValid (unaryHoleRaw 108) unaryHoleFeatures unaryHoleStates + (unaryHoleCode .conv_i8) := by decide +-- `conv.r4` is uncovered: the site admits no access form, conversion or faulting arithmetic. +example : ¬ ScalarFlowValid (unaryHoleRaw 107) unaryHoleFeatures unaryHoleStates + (unaryHoleCode .conv_i) := by decide +kernel +example : ¬ ScalarFlowValid (unaryHoleRaw 107) unaryHoleFeatures unaryHoleStates + (unaryHoleCode (.conv_ovf .i)) := by decide +kernel +example : ¬ ScalarFlowValid (unaryHoleRaw 107) unaryHoleFeatures unaryHoleStates + (unaryHoleCode (.conv_ovf_un .u)) := by decide +kernel +example : ¬ ScalarFlowValid (unaryHoleRaw 107) unaryHoleFeatures unaryHoleStates + (unaryHoleCode (.ldind .i8)) := by decide +kernel +example : ¬ ScalarFlowValid (unaryHoleRaw 107) unaryHoleFeatures unaryHoleStates + (unaryHoleCode .rem_un) := by decide +kernel +example : ([Instruction.div, .div_un, .rem, .add_ovf, .add_ovf_un, .sub_ovf, .sub_ovf_un, .mul_ovf, + .mul_ovf_un].all fun emitted => !decide (ScalarFlowValid (unaryHoleRaw 107) + unaryHoleFeatures unaryHoleStates (unaryHoleCode emitted))) = true := by decide +kernel + +/-! ## Indirect accesses + +A body pushes `pops` copies of its argument, runs the access after its prefixes, pops what the +access pushed and returns the argument. Each raw `ldind`, `stind`, `ldobj`, `stobj`, `cpblk` +and `initblk` admits exactly the emission its opcode, operand type and prefix give. -/ + +/-- One access with its prefixes and the emission a certificate claims for it. -/ +private structure IndirectCase where + pops : Nat + /-- The kind the access pushes, or `none` for a store or block operation. -/ + pushed : Option ScalarKind + prefixes : List RawInstruction := [] + access : RawInstruction + emitted : Instruction + +private def IndirectCase.pushes (access : IndirectCase) : Nat := + if access.pushed.isSome then 1 else 0 +private def IndirectCase.middle (access : IndirectCase) : List RawInstruction := + access.prefixes ++ [access.access] +private def IndirectCase.rawMethod (access : IndirectCase) : RawMethod := + { raw (List.replicate access.pops (i 2) ++ access.middle ++ + List.replicate access.pushes (i 38) ++ [i 2, i 42]) [word] with maxStack := 3 } +private def IndirectCase.code (access : IndirectCase) : List Instruction := + List.replicate access.pops (.ldarg 0) ++ access.prefixes.map (fun _ => .nop) ++ + [access.emitted] ++ List.replicate access.pushes .pop ++ [.ldarg 0, .ret] +private def IndirectCase.features (access : IndirectCase) : FeatureCertificate := + (List.range access.pops).map unknown ++ access.middle.map (fun _ => unknown access.pops) ++ + List.replicate access.pushes (unknown 1) ++ [unknown 0, unknown 1] +private def IndirectCase.states (access : IndirectCase) : ScalarCertificate := + (List.range access.pops).map (fun depth => some (List.replicate depth .word64)) ++ + access.middle.map (fun _ => some (List.replicate access.pops .word64)) ++ + access.pushed.toList.map (fun kind => some [kind]) ++ [some [], some [.word64]] +private def IndirectCase.reachable (access : IndirectCase) : List Bool := + List.replicate access.rawMethod.code.length true +private def IndirectCase.uses (access : IndirectCase) : List ArgumentOp := + List.replicate access.pops (.load 0) ++ access.middle.map (fun _ => .other) ++ + List.replicate access.pushes .other ++ [.load 0, .other] +private def IndirectCase.evidence (access : IndirectCase) : MethodEvidence := withBody { + body := noBody, original := access.rawMethod, arguments := [], closed := access.rawMethod, + parameterInput := { arguments := [.field .unsigned64], locals := [], code := access.uses }, + reachable := access.reachable, featureStates := access.features, + scalarStates := access.states, layoutUses := [], assembly := scope } +private def IndirectCase.method (access : IndirectCase) : Method := + { locals := [], returnsValue := true, declared := { parameters := [word], returnType := word }, + code := access.code } +/-- Whether the feature and scalar checks accept the claimed emission. -/ +private def IndirectCase.flows (access : IndirectCase) : Bool := + let (method, code, states) := match access.access.operand with + | .type type => + let store := access.pops == 2 + let before := if store then [scalarType type, .reference] else [.reference] + let inputs := if store then [i 2, i 3] else [i 2] + let loads := if store then [Instruction.ldarg 0, .ldarg 1] else [.ldarg 0] + let method := { access.rawMethod with + parameters := [.byref type, type, word] + code := inputs ++ access.middle ++ List.replicate access.pushes (i 38) ++ [i 4, i 42] } + let code := loads ++ access.prefixes.map (fun _ => .nop) ++ [access.emitted] ++ + List.replicate access.pushes .pop ++ [.ldarg 2, .ret] + let states := [some []] ++ (if store then [some [.reference]] else []) ++ + access.middle.map (fun _ => some before) ++ access.pushed.toList.map (fun k => some [k]) ++ + [some [], some [.word64]] + (method, code, states) + | _ => (access.rawMethod, access.code, access.states) + decide (FeatureFlowValid #[] (fun _ => none) (fun _ => none) method .scalar + access.features + access.reachable code) && + decide (ScalarFlowValid method access.features states code) +/-- Whether the indirect access rule accepts the emission at the access itself. -/ +private def IndirectCase.window (access : IndirectCase) : Bool := + indirectSiteWindow (access.rawMethod.code[·]?) (access.pops + access.prefixes.length) + access.access access.emitted +/-- Whether every check of the method's evidence accepts it. -/ +private def IndirectCase.translates (access : IndirectCase) : Bool := + decide (MethodEvidenceValid [] #[] [] objectPlatform [access.evidence] + [] access.evidence access.method) + +private def load (opcode : Nat) (emitted : Instruction) (kind : ScalarKind := .word32) + (prefixes : List RawInstruction := []) (operand : RawOperand := .none) : IndirectCase := + { pops := 1, pushed := some kind, prefixes, access := i opcode operand, emitted } +private def store (opcode : Nat) (emitted : Instruction) (prefixes : List RawInstruction := []) + (operand : RawOperand := .none) : IndirectCase := + { pops := 2, pushed := none, prefixes, access := i opcode operand, emitted } +private def block (opcode : Nat) (emitted : Instruction) (prefixes : List RawInstruction := []) : + IndirectCase := + { pops := 3, pushed := none, prefixes, access := i opcode, emitted } +private def unaligned (alignment : Int) : RawInstruction := i 65042 (.integer alignment) +private def sbyteType : TypeExpr := .atom runtime "System.SByte" +private def aggregateType : TypeExpr := .aggregate "S" 0 +private def enumType : TypeExpr := .enum "E" (.atom runtime "System.Int16") +private def vectorType : TypeExpr := + .apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") intType +/-- An `ldobj` (113) or `stobj` (129) of `type`, pushing the kind the scalar check gives it. -/ +private def typed (opcode : Nat) (type : TypeExpr) (emitted : Instruction) : IndirectCase := + if opcode == 113 then load 113 emitted (scalarType type) (operand := .type type) + else store 129 emitted (operand := .type type) +/-- A static call taking one word and returning one, the stack effect of a 64-bit load. -/ +private def loadSite : CallSite := + { declared := { convention := 0, returnType := word, parameters := [word] }, + method := { + owner := .void, name := name!"", hasThis := false, genericArity := 0, returnType := word, + parameters := [word] }, + target := .managed 0x06000002, typeArguments := [], parameterCount := 1, returnsValue := true } +/-- A static call taking two words and returning nothing, the stack effect of a 64-bit store. -/ +private def storeSite : CallSite := + { declared := { convention := 0, parameters := [word, word] }, + method := { + owner := .void, name := name!"", hasThis := false, genericArity := 0, + returnType := .void, parameters := [word, word] }, + target := .managed 0x06000002, typeArguments := [], parameterCount := 2, + returnsValue := false } + +/-- The emissions extraction gives these accesses, none of which carries a metadata token. -/ +private def indirectEmitted : List IndirectCase := [ + load 70 (.ldind .i1), load 71 (.ldind .u1), load 72 (.ldind .i2), load 73 (.ldind .u2), + load 74 (.ldind .i4), load 75 (.ldind .u4), load 76 (.ldind .i8) .word64, + load 77 (.ldind .i) .native, store 82 (.stind .i1), store 83 (.stind .i2), + store 84 (.stind .i4), store 85 (.stind .i8), store 223 (.stind .i), + block 65047 (.cpblk 8), block 65048 (.initblk 8), + block 65047 (.cpblk 1) [unaligned 1], + block 65048 (.initblk 2) [unaligned 2], + block 65047 (.cpblk 4) [unaligned 4], + load 74 (.ldind .i4) (prefixes := [unaligned 1]), + store 85 (.stind .i8) [unaligned 2]] + +/-- `ldobj` and `stobj` of primitive, aggregate, enum and vector types, whose type tokens the + test encoder cannot write. -/ +private def objectEmitted : List IndirectCase := [ + typed 113 intType (.ldobj intType), typed 113 sbyteType (.ldobj sbyteType), + typed 113 word (.ldobj word), typed 129 intType (.stobj intType), + typed 129 sbyteType (.stobj sbyteType), typed 113 aggregateType (.ldobj aggregateType), + typed 129 aggregateType (.stobj aggregateType), typed 113 enumType (.ldobj enumType), + typed 113 vectorType (.ldobj vectorType)] + +/-- Each emits other than the decoded instruction: a wrong width, sign, alignment or operation, + a runtime-model load or store, a kind or prefix extraction refuses, and an access form at an + unrelated opcode. -/ +private def indirectForged : List IndirectCase := [ + -- Width. + load 70 (.ldind .i4), load 73 (.ldind .u1), load 74 (.ldind .i8), + load 76 (.ldind .i4) .word64, load 77 (.ldind .i8) .native, + load 70 (.ldobj vectorType), store 82 (.stind .i4), store 84 (.stind .i8), + store 85 (.stind .i4), store 223 (.stind .i2), + -- Sign. + load 70 (.ldind .u1), load 71 (.ldind .i1), load 72 (.ldind .u2), load 73 (.ldind .i2), + -- Typed object accesses of the right width, which no raw `ldind` or `stind` decodes to. + load 74 (.ldobj intType), load 70 (.ldobj sbyteType), store 84 (.stobj intType), + -- Alignment and operation. + block 65047 (.cpblk 1), block 65048 (.initblk 4), + block 65047 (.cpblk 8) [unaligned 1], + block 65048 (.initblk 2) [unaligned 4], + block 65047 (.initblk 8), block 65048 (.cpblk 8), + -- Kinds and prefixes extraction refuses. + block 65047 (.cpblk 3) [unaligned 3], + block 65047 (.cpblk 8) [i 0xFE1E], + block 65048 (.initblk 8) [i 0xFE14], + load 74 (.ldind .i4) (prefixes := [i 65043]), + load 78 (.ldind .i4) .other, load 79 (.ldind .i8) .other, + load 80 (.ldind .i) .other, store 81 (.stind .i8), store 86 (.stind .i4), + store 87 (.stind .i8), + -- Access forms at other opcodes. + load 108 (.ldobj word) .other, load 108 (.ldind .i8) .other, + load 107 (.ldind .i4) .other] + +/-- Forged emissions at operands the test encoder cannot write: wrong widths and signs of `ldobj` + and `stobj`, field accesses at operand types extraction refuses, and calls emitting an + `ldind` or `stind`. -/ +private def objectForged : List IndirectCase := [ + load 113 (.ldobj (.atom runtime "System.UInt16")) (operand := .type intType), + load 113 (.ldobj (.atom runtime "System.UInt32")) (operand := .type intType), + load 113 (.ldobj (.atom runtime "System.Byte")) (operand := .type sbyteType), + load 113 (.ldind .i8) .word64 (operand := .type word), + store 129 (.stind .i1) (operand := .type intType), + store 129 (.stind .i8) (operand := .type sbyteType), + typed 113 aggregateType (.ldind .u4), typed 129 aggregateType (.ldobj aggregateType), + typed 113 enumType (.ldind .u2), typed 113 vectorType (.ldobj (.apply + (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector256`1") intType)), + -- Floating-point and pointer operands, which extraction refuses. + typed 113 (.atom runtime "System.Double") (.ldfld tupleItem1), + typed 129 (.atom runtime "System.Single") (.stfld tupleItem1), + typed 113 (.pointer intType) (.ldfld tupleItem1), + load 40 (.ldind .i8) .word64 (operand := .call loadSite), + store 40 (.stind .i8) (operand := .call storeSite)] + +/-- Every emission extraction gives passes the indirect access rule, both flow checks and, where + the body has no token, every check of the method's evidence. -/ +private theorem indirect_emissions_accepted : + (indirectEmitted.all fun access => access.window && access.flows && access.translates) ∧ + (objectEmitted.all fun access => access.window && access.flows) := by + decide +kernel + +#print axioms indirect_emissions_accepted + +/-- Every forged emission fails the indirect access rule, so both flow checks and the method's + evidence check reject it. -/ +private theorem indirect_forgeries_rejected : + (indirectForged.all fun access => !access.window && !access.flows && !access.translates) ∧ + (objectForged.all fun access => !access.window && !access.flows) := by + decide +kernel + +#print axioms indirect_forgeries_rejected + +-- `unaligned.` takes only an access, with an alignment of 1, 2 or 4, and emits nothing. +example : indirectSiteWindow ([unaligned 1, i 105][·]?) 0 (unaligned 1) .nop = false := by decide +example : indirectSiteWindow ([unaligned 1, i 74][·]?) 0 (unaligned 1) .nop = true := by decide +example : indirectSiteWindow ([unaligned 8, i 74][·]?) 0 (unaligned 8) .nop = false := by decide +example : indirectSiteWindow ([unaligned 1][·]?) 0 (unaligned 1) .nop = false := by decide +example : indirectSiteWindow ([i 65043, i 74][·]?) 0 (i 65043) .nop = false := by decide private def pushHoleRaw (opcode : Nat) := raw [i opcode, i 38, i 2, i 42] [word] private def pushHoleFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 0, unknown 1] private def pushHoleStates : ScalarCertificate := [some [], some [.other], some [], some [.word64]] -private def pushHoleCode (emitted : Op) : List Op := [emitted, .pop, .arg 0, .ret] -example : ScalarFlowValid #[] (pushHoleRaw 20) pushHoleFeatures - (pushHoleStates.set 1 (some [.nullObject])) (pushHoleCode (.memory .nullObject)) := by decide --- `ldnull` has one canonical null; the byref null of `Unsafe.NullRef` is a different value. -example : ¬ ScalarFlowValid #[] (pushHoleRaw 20) pushHoleFeatures - (pushHoleStates.set 1 (some [.nullObject])) (pushHoleCode (.memory .nullRef)) := by decide -example : ¬ ScalarFlowValid #[] (pushHoleRaw 20) pushHoleFeatures (pushHoleStates.set 1 (some [.nullObject])) (pushHoleCode (.const64 0)) := by decide -example : ¬ ScalarFlowValid #[] (pushHoleRaw 35) pushHoleFeatures pushHoleStates (pushHoleCode (.const64 0)) := by decide -example : ¬ ScalarFlowValid #[] (pushHoleRaw 35) pushHoleFeatures pushHoleStates (pushHoleCode (.const32 0)) := by decide +private def pushHoleCode (emitted : Instruction) : List Instruction := + [emitted, .pop, .ldarg 0, .ret] +example : ScalarFlowValid (pushHoleRaw 20) pushHoleFeatures + (pushHoleStates.set 1 (some [.nullObject])) (pushHoleCode .ldnull) := by decide +/-- The closed Unsafe.NullRef returns a managed reference. -/ +private def nullReference : MethodRef := + { owner := unsafeOwner, name := name!"NullRef", hasThis := false, genericArity := 1, + methodArguments := [intType], parameters := [], returnType := .byref intType } + +-- The byref returned by Unsafe.NullRef has a different stack kind from the object null. +example : ¬ ScalarFlowValid (pushHoleRaw 20) pushHoleFeatures + (pushHoleStates.set 1 (some [.nullObject])) (pushHoleCode (.call nullReference)) := by decide +example : ¬ ScalarFlowValid (pushHoleRaw 20) pushHoleFeatures + (pushHoleStates.set 1 (some [.nullObject])) (pushHoleCode (.ldc_i8 0)) := by decide +example : ¬ ScalarFlowValid (pushHoleRaw 35) pushHoleFeatures pushHoleStates + (pushHoleCode (.ldc_i8 0)) := by decide +example : ¬ ScalarFlowValid (pushHoleRaw 35) pushHoleFeatures pushHoleStates + (pushHoleCode (.ldc_i4 0)) := by decide /-- A class with a field at raw index 1, read by `ldarg.0; ldfld; ret`. -/ private def account : TypeExpr := .object "Scope" "Scope.Account" -private def fieldRaw : RawMethod := { raw [i 2, i 123 (.field account 1 word false), i 42] [account] with - maxStack := 1 } +private def balance : FieldRef := + { owner := account, index := 1, type := word, name := name!"balance" } +private def fieldRaw : RawMethod := + { raw [i 2, i 123 (.field account 1 word name!"balance" false), i 42] [account] with + maxStack := 1 } private def fieldFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 1] private def fieldStates : ScalarCertificate := [some [], some [.object], some [.word64]] -private def fieldCode (operation : MemoryOp) : List Op := [.arg 0, .memory operation, .ret] --- A class field load names the field's declaring class, model offset, width and extension. -example : ScalarFlowValid #[] fieldRaw fieldFeatures fieldStates - (fieldCode (.objectField account 8 8 false)) := by decide -example : ¬ ScalarFlowValid #[] fieldRaw fieldFeatures fieldStates - (fieldCode (.objectField account 0 8 false)) := by decide -example : ¬ ScalarFlowValid #[] fieldRaw fieldFeatures fieldStates - (fieldCode (.objectField (.object "Scope" "Scope.Other") 8 8 false)) := by decide -example : ¬ ScalarFlowValid #[] fieldRaw fieldFeatures fieldStates - (fieldCode (.objectField account 8 4 false)) := by decide +private def fieldCode (access : Instruction) : List Instruction := [.ldarg 0, access, .ret] +-- The scalar check and the code equation accept the load of the field the raw operand names. +example : Tests.DecodedFlow fieldRaw fieldFeatures fieldStates (fieldCode (.ldfld balance)) := by + decide +kernel +example : ¬ ScalarFlowValid fieldRaw fieldFeatures fieldStates + (fieldCode (.stfld balance)) := by decide /-- A class-typed field at raw index 1 of `Scope.Node`, holding another node. -/ private def node : TypeExpr := .object "Scope" "Scope.Node" -private def nextField : RawOperand := .field node 1 node false +private def nextField : RawOperand := .field node 1 node name!"next" false +private def next : FieldRef := { owner := node, index := 1, type := node, name := name!"next" } +/-- A scalar field at raw index 0 of `Scope.Node`. -/ +private def valueField : RawOperand := .field node 0 word name!"value" false +private def value : FieldRef := { owner := node, index := 0, type := word, name := name!"value" } private def nextRaw : RawMethod := { raw [i 2, i 123 nextField, i 42] [node] with returnType := node, maxStack := 1 } private def nextStates : ScalarCertificate := [some [], some [.object], some [.object]] --- A class-typed field moves the reference its slot holds, at the field's model offset; it is --- never read as bytes, and a scalar field is never read as a slot. -example : ScalarFlowValid #[] nextRaw fieldFeatures nextStates - (fieldCode (.objectReferenceField node 8)) := by decide -example : ¬ ScalarFlowValid #[] nextRaw fieldFeatures nextStates - (fieldCode (.objectReferenceField node 0)) := by decide -example : ¬ ScalarFlowValid #[] nextRaw fieldFeatures nextStates - (fieldCode (.objectReferenceField account 8)) := by decide -example : scalarEmissionMatches #[] none (i 123 nextField) [.object] - (.memory (.objectField node 8 8 false)) = false := by decide -example : scalarEmissionMatches #[] none (i 123 (.field node 0 word false)) [.object] - (.memory (.objectReferenceField node 0)) = false := by decide --- A store takes an object or null as its value, and only as a slot store. -example : scalarEmissionMatches #[] none (i 125 nextField) [.object, .object] - (.memory (.setObjectReferenceField node 8)) = true := by decide -example : scalarEmissionMatches #[] none (i 125 nextField) [.nullObject, .object] - (.memory (.setObjectReferenceField node 8)) = true := by decide -example : scalarEmissionMatches #[] none (i 125 nextField) [.word64, .object] - (.memory (.setObjectReferenceField node 8)) = false := by decide -example : scalarEmissionMatches #[] none (i 125 nextField) [.object, .object] - (.memory (.setObjectField node 8 8)) = false := by decide -example : scalarEmissionMatches #[] none (i 125 (.field node 0 word false)) [.word64, .object] - (.memory (.setObjectReferenceField node 0)) = false := by decide --- A field typed as an array of a class holds the array's reference in its slot; a value array --- field is never a reference slot. -private def itemsField : RawOperand := .field node 1 (.array node) false -example : scalarEmissionMatches #[] none (i 123 itemsField) [.object] - (.memory (.objectReferenceField node 8)) = true := by decide -example : scalarEmissionMatches #[] none (i 125 itemsField) [.nullObject, .object] - (.memory (.setObjectReferenceField node 8)) = true := by decide -example : scalarEmissionMatches #[] none (i 123 (.field node 1 (.array word) false)) [.object] - (.memory (.objectReferenceField node 8)) = false := by decide -example : scalarEmissionMatches #[] none (i 125 (.field node 1 (.array word) false)) - [.object, .object] (.memory (.setObjectReferenceField node 8)) = false := by decide --- A value-array field is an array slot of the field's element type; it stores only an array --- of that element, and a class-array field is never one. -private def samplesField : RawOperand := .field node 1 (.array word) false -example : scalarEmissionMatches #[] none (i 123 samplesField) [.object] - (.memory (.arrayField node 8 .unsigned64)) = true := by decide -example : scalarEmissionMatches #[] none (i 123 samplesField) [.object] - (.memory (.arrayField node 8 .signed64)) = false := by decide -example : scalarEmissionMatches #[] none (i 123 samplesField) [.object] - (.memory (.arrayField node 0 .unsigned64)) = false := by decide -example : scalarEmissionMatches #[] none (i 123 itemsField) [.object] - (.memory (.arrayField node 8 .unsigned64)) = false := by decide -example : scalarEmissionMatches #[] none (i 125 samplesField) [.array .unsigned64, .object] - (.memory (.setArrayField node 8 .unsigned64)) = true := by decide -example : scalarEmissionMatches #[] none (i 125 samplesField) [.array .signed32, .object] - (.memory (.setArrayField node 8 .unsigned64)) = false := by decide -example : scalarEmissionMatches #[] none (i 125 samplesField) [.object, .object] - (.memory (.setArrayField node 8 .unsigned64)) = false := by decide +-- A class-typed field load names its own field; the step reads the slot the field's type gives. +example : Tests.DecodedFlow nextRaw fieldFeatures nextStates (fieldCode (.ldfld next)) := by + decide +kernel + +/-- `raw` as the evidence of a method whose every pc is reachable. -/ +private def reachableEvidence (raw : RawMethod) : MethodEvidence := + { addEvidence with closed := raw, reachable := raw.code.map fun _ => true } + +/-- The evidence of `raw`, every pc reachable, refuses a method emitting `code`. -/ +private def EvidenceRefuses (raw : RawMethod) (code : List Instruction) : Prop := + ¬ MethodEvidenceValid [] #[] [] objectPlatform [reachableEvidence raw] [] (reachableEvidence raw) + { addMethod with code } + +/-- Code other than the decoding of `raw` is refused by the code equation conjunct alone. -/ +private theorem refused_by_code_equation {raw : RawMethod} {code : List Instruction} + (different : code ≠ methodCode addMethod.profile addMethod.environment addMethod.links + (raw.code.map fun _ => true) raw) : EvidenceRefuses raw code := + fun valid => different valid.code_eq +#print axioms refused_by_code_equation + +/-- Evidence refuses a class field load naming another owner, index, held type or name than its + raw operand. The scalar check skips the operand at this ordinary site, so each refusal is + proved from the code equation conjunct. -/ +theorem forged_field_loads_refused : + EvidenceRefuses fieldRaw (fieldCode (.ldfld { balance with index := 0 })) ∧ + EvidenceRefuses fieldRaw + (fieldCode (.ldfld { balance with owner := .object "Scope" "Scope.Other" })) ∧ + EvidenceRefuses fieldRaw + (fieldCode (.ldfld { balance with type := .atom runtime "System.Int32" })) ∧ + EvidenceRefuses fieldRaw (fieldCode (.ldfld { balance with name := name!"limit" })) ∧ + EvidenceRefuses nextRaw (fieldCode (.ldfld { next with index := 0 })) ∧ + EvidenceRefuses nextRaw (fieldCode (.ldfld { next with owner := account })) := + ⟨refused_by_code_equation (by decide +kernel), refused_by_code_equation (by decide +kernel), + refused_by_code_equation (by decide +kernel), refused_by_code_equation (by decide +kernel), + refused_by_code_equation (by decide +kernel), refused_by_code_equation (by decide +kernel)⟩ +#print axioms forged_field_loads_refused +example : scalarEmissionMatches none none (i 123 nextField) [.object] + (.ldfld value) = false := by + decide +example : scalarEmissionMatches none none (i 123 valueField) [.object] + (.ldfld next) = false := by + decide +-- A store to a class-typed field takes an object or null as its value; a load is no store. +example : scalarEmissionMatches none none (i 125 nextField) [.object, .object] + (.stfld next) = true := by decide +example : scalarEmissionMatches none none (i 125 nextField) [.nullObject, .object] + (.stfld next) = true := by decide +example : scalarEmissionMatches none none (i 125 nextField) [.word64, .object] + (.stfld next) = false := by decide +example : scalarEmissionMatches none none (i 125 nextField) [.object, .object] + (.ldfld next) = false := by decide +example : scalarEmissionMatches none none (i 125 valueField) [.word64, .object] + (.stfld next) = false := by decide +-- The receiver of a field access is an object or null. +example : scalarEmissionMatches none none (i 123 valueField) [.word64] + (.ldfld value) = false := by + decide +example : scalarEmissionMatches none none (i 123 valueField) [.reference] + (.ldfld value) = false := + by decide +example : scalarEmissionMatches none none (i 124 valueField) [.word64] + (.ldflda value) = false := by + decide +example : scalarEmissionMatches none none (i 124 valueField) [.reference] + (.ldflda value) = false := + by decide +example : scalarEmissionMatches none none (i 125 valueField) [.word64, .object] + (.stfld value) = true := by decide +example : scalarEmissionMatches none none (i 125 valueField) [.word64, .word64] + (.stfld value) = false := by decide +/-- No incoming stack can admit the address of a field on either class owner. This holds + both when checking a literal emission and when the code already equals its decoding. -/ +theorem class_field_address_refused (stack : List ScalarKind) (skipEncoding : Bool) : + let generic := TypeExpr.apply (.objectConstructor "Scope" "Scope.Node`1") word + scalarEmissionMatches none none (i 124 valueField) stack (.ldflda value) skipEncoding = false ∧ + scalarEmissionMatches none none (i 124 (.field generic 0 word name!"value" false)) stack + (.ldflda { value with owner := generic }) skipEncoding = false := by + cases skipEncoding <;> cases stack <;> exact ⟨rfl, rfl⟩ + +#print axioms class_field_address_refused + +/-- Even when the method emits the exact raw field instruction and its stack carries a byref + result, its scalar certificate cannot admit a live class field address. -/ +example : + let body := { raw [i 2, i 124 valueField, i 42] [node] with + returnType := .byref word, maxStack := 1 } + ¬ ScalarFlowValid body fieldFeatures [some [], some [.object], some [.reference]] + (fieldCode (.ldflda value)) := by decide +kernel +/-- Whether a `stfld` of a `Scope.Node` field at raw index 0 holding `type` admits a stored + `value` of that kind. -/ +private def storesInto (type : TypeExpr) (value : ScalarKind) : Bool := + scalarEmissionMatches none none (i 125 (.field node 0 type name!"value" false)) + [value, .object] (.stfld { owner := node, index := 0, type, name := name!"value" }) +/-- The runtime primitive type `name`. -/ +private def primitive (name : String) : TypeExpr := .atom runtime name +-- A stored integer has the field's stack kind, `int32` up to 4 bytes and `int64` or native for +-- 8, since the step reads an `int32` only into a narrow slot and an `int64` only into a wide one. +example : storesInto (primitive "System.SByte") .word64 = false := by decide +example : storesInto (primitive "System.Int16") .word64 = false := by decide +example : storesInto (primitive "System.Int32") .word64 = false := by decide +example : storesInto (primitive "System.Int32") .native = false := by decide +example : storesInto (primitive "System.Int64") .word32 = false := by decide +example : storesInto (primitive "System.IntPtr") .word32 = false := by decide +example : storesInto (.enum "E" (primitive "System.Int64")) .word32 = false := by decide +example : storesInto (primitive "System.Int32") .word32 = true := by decide +example : storesInto (primitive "System.Boolean") .word32 = true := by decide +example : storesInto (primitive "System.Char") .word32 = true := by decide +example : storesInto (.enum "E" (primitive "System.Int16")) .word32 = true := by decide +example : storesInto (primitive "System.Int64") .native = true := by decide +example : storesInto (primitive "System.IntPtr") .word64 = true := by decide +-- A field typed as an array of a class holds the array's reference, stored as an object or null; +-- a value array field stores only an array. +private def itemsField : RawOperand := .field node 1 (.array node) name!"items" false +private def items : FieldRef := + { owner := node, index := 1, type := .array node, name := name!"items" } +/-- A value-array field at raw index 1 of `Scope.Node`. -/ +private def samplesField : RawOperand := .field node 1 (.array word) name!"samples" false +private def samples : FieldRef := + { owner := node, index := 1, type := .array word, name := name!"samples" } +example : scalarEmissionMatches none none (i 123 itemsField) [.object] + (.ldfld items) = true := by + decide +example : scalarEmissionMatches none none (i 125 itemsField) [.nullObject, .object] + (.stfld items) = true := by decide +example : scalarEmissionMatches none none (i 123 samplesField) [.object] + (.ldfld items) = false := by decide +example : scalarEmissionMatches none none (i 125 samplesField) + [.object, .object] (.stfld samples) = false := by decide +-- A value-array field names its element type; it stores only an array of that element. +example : scalarEmissionMatches none none (i 123 samplesField) [.object] + (.ldfld samples) = true := by decide +example : scalarEmissionMatches none none (i 123 samplesField) [.object] + (.ldfld { samples with type := .array (.atom runtime "System.Int64") }) = false := by + decide +example : scalarEmissionMatches none none (i 123 samplesField) [.object] + (.ldfld { samples with index := 0 }) = false := by decide +example : scalarEmissionMatches none none (i 123 itemsField) [.object] + (.ldfld samples) = false := by decide +example : scalarEmissionMatches none none (i 125 samplesField) [.array .unsigned64, .object] + (.stfld samples) = true := by decide +example : scalarEmissionMatches none none (i 125 samplesField) [.array .signed32, .object] + (.stfld samples) = false := by decide +example : scalarEmissionMatches none none (i 125 samplesField) [.object, .object] + (.stfld samples) = false := by decide + +/-- `Scope.Pair`, a struct at layout 0, with fields of each kind the step resolves: an integer, + an enum, a vector, a nested struct and a generic struct instance. -/ +private def pairType : TypeExpr := .aggregate "Scope::Scope.Pair" 0 +private def modeType : TypeExpr := .enum "Scope::Scope.Mode" intType +private def laneType : TypeExpr := + .apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") word +private def innerType : TypeExpr := .aggregate "Scope::Scope.Inner" 1 +/-- The field of `Scope.Pair` at raw index `index` holding `type`, as a reference. -/ +private def pairField (index : Nat) (type : TypeExpr) : FieldRef := + { owner := pairType, index, type, name := name!"item" } +/-- Whether a struct field instruction of `opcode` on the field at `index` holding `type` + emits `emitted` over `stack`. -/ +private def structSite (opcode index : Nat) (type : TypeExpr) (stack : List ScalarKind) + (emitted : Instruction) : Bool := + scalarEmissionMatches none none (i opcode (.field pairType index type name!"item" false)) + stack emitted + +/-- A struct field site emits the decoded access of the field its operand names: a load through + a reference or from a value, an address and a store through a reference, for a field of each + kind, storing a value of the field's stack kind. -/ +theorem struct_field_sites_accepted : + structSite 123 0 word [.reference] (.ldfld (pairField 0 word)) = true ∧ + structSite 123 0 word [.other] (.ldfld (pairField 0 word)) = true ∧ + structSite 124 0 word [.reference] (.ldflda (pairField 0 word)) = true ∧ + structSite 125 0 word [.word64, .reference] (.stfld (pairField 0 word)) = true ∧ + structSite 125 1 modeType [.word32, .reference] (.stfld (pairField 1 modeType)) = true ∧ + structSite 125 2 laneType [.other, .reference] (.stfld (pairField 2 laneType)) = true ∧ + structSite 125 3 innerType [.other, .reference] (.stfld (pairField 3 innerType)) = true ∧ + structSite 125 4 tupleType [.other, .reference] (.stfld (pairField 4 tupleType)) = true := by + decide +kernel +#print axioms struct_field_sites_accepted + +/-- A struct field site refuses each forged emission: a load where the raw instruction is a + store, a receiver of the wrong kind, a stored value of another stack kind, and a reference + naming another index, owner, held type or name than the operand. -/ +theorem struct_field_forgeries_refused : + structSite 125 0 word [.word64, .reference] (.ldfld (pairField 0 word)) = false ∧ + structSite 123 0 word [.object] (.ldfld (pairField 0 word)) = false ∧ + structSite 124 0 word [.other] (.ldflda (pairField 0 word)) = false ∧ + structSite 125 0 word [.word64, .other] (.stfld (pairField 0 word)) = false ∧ + structSite 125 0 word [.word32, .reference] (.stfld (pairField 0 word)) = false ∧ + structSite 123 0 word [.reference] (.ldfld (pairField 1 word)) = false ∧ + structSite 123 0 word [.reference] (.ldfld { pairField 0 word with owner := innerType }) = + false ∧ + structSite 123 0 word [.reference] (.ldfld (pairField 0 intType)) = false ∧ + structSite 123 0 word [.reference] (.ldfld { pairField 0 word with name := name!"other" }) = + false := by + decide +kernel +#print axioms struct_field_forgeries_refused -- `ldstr` loads exactly its own text, nothing else loads a literal, and a folded throw's -- message emits nothing. -example : scalarEmissionMatches #[] none (i 114 (.text "a")) [] - (.memory (.stringLiteral "a")) = true := by decide -example : scalarEmissionMatches #[] none (i 114 (.text "a")) [] - (.memory (.stringLiteral "b")) = false := by decide -example : scalarEmissionMatches #[] none (i 0) [] (.memory (.stringLiteral "a")) = false := by +example : scalarEmissionMatches none none (i 114 (.text "a")) [] + (.ldstr "a") = true := by decide +example : scalarEmissionMatches none none (i 114 (.text "a")) [] + (.ldstr "b") = false := by decide +example : scalarEmissionMatches none none (i 0) [] (.ldstr "a") = false := by decide -example : scalarEmissionMatches #[] (some .interior) (i 114 (.text "a")) [] - (.memory (.stringLiteral "a")) = false := by decide -example : scalarEmissionMatches #[] none (i 114 (.text "a")) [] .nop = false := by decide - --- A guard emission comes only from its exact call: the type argument fixes width and signedness, --- and another owner, overload or type argument emits nothing. -private def guardSite (owner methodName suffix : String) (arguments parameters : List TypeExpr) : - RawInstruction := - ⟨0, 40, .call - { declaringType := .atom runtimeScope owner, name := methodName, - target := .external (runtimeScope ++ "::System.Void " ++ owner ++ "::" ++ suffix), - typeArguments := arguments, ownerArgumentCount := 0, parameterCount := parameters.length, - hasThis := false, returnsValue := false, returnType := .void, - parameterTypes := parameters }⟩ - -private def parameterName : TypeExpr := .object runtimeScope "System.String" -private def uint32 : TypeExpr := .atom runtimeScope "System.UInt32" -private def negative (argument : TypeExpr) : RawInstruction := - guardSite "System.ArgumentOutOfRangeException" "ThrowIfNegative" - "ThrowIfNegative(!!0,System.String)" [argument] [argument, parameterName] - -example : guardCallMatches (negative uint32) (.memory (.zeroGuard .negative 32 false)) = true := by - decide +kernel -example : guardCallMatches (negative uint32) (.memory (.zeroGuard .negative 32 true)) = false := by - decide +kernel -example : guardCallMatches (negative uint32) - (.memory (.zeroGuard .negativeOrZero 32 false)) = false := by decide +kernel -example : guardCallMatches (negative uint32) .nop = false := by decide +kernel -example : guardCallMatches (negative (.atom runtimeScope "System.Double")) - (.memory (.zeroGuard .negative 64 true)) = false := by decide +kernel -example : guardCallMatches (negative (.atom runtimeScope "System.Char")) - (.memory (.zeroGuard .negative 16 false)) = false := by decide +kernel -example : guardCallMatches - (guardSite "System.ArgumentException" "ThrowIfNegative" "ThrowIfNegative(!!0,System.String)" - [uint32] [uint32, parameterName]) (.memory (.zeroGuard .negative 32 false)) = false := by decide +kernel -example : guardCallMatches (i 0) (.memory .throwIfNull) = false := by decide +kernel -example : guardCallMatches - (guardSite "System.ArgumentNullException" "ThrowIfNull" - "ThrowIfNull(System.Object,System.String)" [] [.atom runtimeScope "System.Object", parameterName]) - (.memory .throwIfNull) = true := by decide +kernel -example : guardCallMatches - (guardSite "System.ArgumentNullException" "ThrowIfNull" - "ThrowIfNull(System.Void*,System.String)" [] [.pointer .void, parameterName]) - (.memory .throwIfNull) = false := by decide +kernel +example : scalarEmissionMatches (some .interior) none (i 114 (.text "a")) [] + (.ldstr "a") = false := by decide +example : scalarEmissionMatches none none (i 114 (.text "a")) [] .nop = false := by decide + +/-- A genuine open guard declaration with the element substituted in the closed reference. -/ +private def guardSite (name : String) (element : TypeExpr) (comparison := false) : CallSite := + let key := (MetadataName.ofBytes? (bytesOf name)).getD ⟨0⟩ + let null := name == "ThrowIfNull" + let label := TypeExpr.object runtimeScope "System.String" + let owner := if null then "System.ArgumentNullException" else "System.ArgumentOutOfRangeException" + let parameters (type : TypeExpr) := if null then [.atom runtimeScope "System.Object", label] + else if comparison then [type, type, label] else [type, label] + let signature := if null then "System.Object,System.String" + else if comparison then "!!0,!!0,System.String" else "!!0,System.String" + let arguments := if null then [] else [element] + { declared := { convention := if null then 0 else 0x10, parameters := parameters (.parameter 0) } + method := + { owner := .atom runtimeScope owner, name := key, hasThis := false, + genericArity := arguments.length, returnType := .void, parameters := parameters element, + methodArguments := arguments } + name, target := .external s!"{runtimeScope}::System.Void {owner}::{name}({signature})" + typeArguments := arguments, parameterCount := (parameters element).length, + returnsValue := false } + +/-- Every guard keeps its exact call. A different shape or a floating element is refused. -/ +theorem guard_decoding : + let uint := TypeExpr.atom runtimeScope "System.UInt32" + let unary := ["ThrowIfZero", "ThrowIfNegative", "ThrowIfNegativeOrZero"] + let binary := ["ThrowIfEqual", "ThrowIfNotEqual", "ThrowIfGreaterThan", "ThrowIfLessThan", + "ThrowIfGreaterThanOrEqual", "ThrowIfLessThanOrEqual"] + Tests.ModelOperands.checks (guardSite "ThrowIfNull" uint) = true ∧ + (unary.all (fun name => Tests.ModelOperands.checks (guardSite name uint))) = true ∧ + (binary.all (fun name => Tests.ModelOperands.checks (guardSite name uint true))) = true ∧ + (unary ++ binary).all (fun name => decide (Tests.ModelOperands.decoded + (guardSite name (.atom runtimeScope "System.Double") (binary.contains name)) = + liveHoleAt 0 40)) = true ∧ + runtimeModel {} .call (guardSite "ThrowIfNegative" uint).method = + some (.memory (.zeroGuard .negative 32 false)) ∧ + runtimeModel {} .call (guardSite "ThrowIfNegative" (.atom runtimeScope "System.Int32")).method = + some (.memory (.zeroGuard .negative 32 true)) ∧ + runtimeModel {} .call (guardSite "ThrowIfNull" uint).method = some (.memory .throwIfNull) := by + decide +kernel + +#print axioms guard_decoding -- A narrow guard reads the low bits of its stack slot with the declared signedness, and an -- operand of another width is ill-typed. example : (guardOperand 8 true (.i32 0xFF#32)).map ZeroGuard.negative.throws = some true := by @@ -721,31 +1401,73 @@ example : guardOperand 64 true (.i32 0#32) = none := by decide example : guardOperand 32 true (.i64 0#64) = none := by decide example : (guardOperand 64 false (.i64 0xFFFFFFFFFFFFFFFF#64)).map ZeroGuard.negative.throws = some false := by decide --- Size constants are the only uncovered literal producers; primitive widths are bound here. +-- A typed size instruction and a retained SizeOf call have different literal encodings. private def sizeRaw (instruction : RawInstruction) := raw [instruction, i 38, i 2, i 42] [word] private def sizeStates : ScalarCertificate := [some [], some [.word32], some [], some [.word64]] -private def sizeCode (bits : W32) : List Op := [.const32 bits, .pop, .arg 0, .ret] +private def sizeCode (bits : W32) : List Instruction := [.ldc_i4 bits, .pop, .ldarg 0, .ret] private def sizeAt (instruction : RawInstruction) (bits : W32) : Bool := - decide (ScalarFlowValid #[] (sizeRaw instruction) pushHoleFeatures sizeStates (sizeCode bits)) + decide (ScalarFlowValid (sizeRaw instruction) pushHoleFeatures sizeStates (sizeCode bits)) +/-- A typed size site must bind the operand and resolve to the stated byte width. -/ +private def typedSizeAt (type emitted : TypeExpr) (bits : Nat) : Bool := + let environment : LayoutEnvironment := { + rawLayouts := [{ identity := "Fixture::Packet", explicit := false, packing := 0, + declaredSize := 24, fields := [] }] + layouts := #[⟨⟨24, []⟩, 1⟩] } + decide (ScalarFlowValid (sizeRaw (i 65052 (.type type))) pushHoleFeatures sizeStates + [.sizeof emitted, .pop, .ldarg 0, .ret]) && (environment.valueShape type).size? == some bits + +/-- The decoder keeps a sizeof operand: with equal layout sizes, the scalar check and the code + equation accept the operand's own struct and refuse another. -/ +private theorem sizeof_equal_layouts : + let a := TypeExpr.aggregate "Fixture::A" 0 + let b := TypeExpr.aggregate "Fixture::B" 1 + let row : RawLayout := { + identity := "Fixture::A", explicit := false, packing := 0, declaredSize := 0 + fields := [{ type := .signed32 }] } + let layout : ComputedLayout := ⟨⟨4, [⟨0, 4, false⟩]⟩, 4⟩ + let rows := [row, { row with identity := "Fixture::B" }] + let environment : LayoutEnvironment := { rawLayouts := rows, layouts := #[layout, layout] } + let input := sizeRaw (i 65052 (.type a)) + let valid := fun type => Tests.DecodedFlow input pushHoleFeatures sizeStates + [.sizeof type, .pop, .ldarg 0, .ret] + LayoutsValid rows environment.layouts .x64 ∧ environment.valueShape a = .aggregate 4 ∧ + environment.valueShape b = .aggregate 4 ∧ valid a ∧ ¬ valid b := by decide +kernel +#print axioms sizeof_equal_layouts + +/-- Evidence refuses a sizeof naming another struct than its raw operand, through the code + equation conjunct. -/ +theorem sizeof_other_struct_refused : + EvidenceRefuses (sizeRaw (i 65052 (.type (.aggregate "Fixture::A" 0)))) + [.sizeof (.aggregate "Fixture::B" 1), .pop, .ldarg 0, .ret] := + refused_by_code_equation (by decide +kernel) +#print axioms sizeof_other_struct_refused + private def sizeOfSite (type : TypeExpr) : CallSite := { - declaringType := .atom runtime "System.Runtime.CompilerServices.Unsafe", name := "SizeOf", - target := .external (runtime ++ "::System.Int32 System.Runtime.CompilerServices.Unsafe::SizeOf()"), - typeArguments := [type], parameterCount := 0, hasThis := false, returnsValue := true, - returnType := intType, parameterTypes := [] } + declared := { convention := 0x10, returnType := intType }, + method := { + owner := .atom runtime "System.Runtime.CompilerServices.Unsafe", name := name!"SizeOf", + hasThis := false, genericArity := 1, returnType := intType, parameters := [], + methodArguments := [type] }, + name := "SizeOf", + target := .external (runtime ++ "::System.Int32 System.Runtime.CompilerServices.Unsafe::SizeOf(\ + )"), + typeArguments := [type], parameterCount := 0, returnsValue := true } private def vector128 : TypeExpr := .apply - (.constructor "System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" + (.constructor + "System.Runtime.Intrinsics, Version=11.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" "System.Runtime.Intrinsics.Vector128`1") word private def packet : TypeExpr := .aggregate "Fixture::Packet" 0 -/-- Size instructions retain exact widths and reject unsupported pointer and void types. -/ +/-- `sizeof` retains exact widths and rejects unsupported pointer and void types; a call of + `Unsafe.SizeOf` keeps its call, so a constant emitted for it is refused at any width. -/ private theorem size_instruction_cases : - (sizeAt (i 65052 (.type intType)) 4 = true) ∧ - (sizeAt (i 65052 (.type nativeType)) 8 = true) ∧ - (sizeAt (i 65052 (.type packet)) 24 = true) ∧ - (sizeAt (i 65052 (.type intType)) 7 = false) ∧ - (sizeAt (i 65052 (.type (.pointer word))) 8 = false) ∧ - (sizeAt (i 40 (.call (sizeOfSite intType))) 4 = true) ∧ - (sizeAt (i 40 (.call (sizeOfSite vector128))) 16 = true) ∧ - (sizeAt (i 40 (.call (sizeOfSite packet))) 24 = true) ∧ + (typedSizeAt intType intType 4 = true) ∧ + (typedSizeAt nativeType nativeType 8 = true) ∧ + (typedSizeAt packet packet 24 = true) ∧ + (typedSizeAt intType intType 7 = false) ∧ + (typedSizeAt (.pointer word) (.pointer word) 8 = false) ∧ + (sizeAt (i 40 (.call (sizeOfSite intType))) 4 = false) ∧ + (sizeAt (i 40 (.call (sizeOfSite vector128))) 16 = false) ∧ + (sizeAt (i 40 (.call (sizeOfSite packet))) 24 = false) ∧ (sizeAt (i 40 (.call (sizeOfSite intType))) 5 = false) := by decide +kernel @@ -755,8 +1477,10 @@ private def otherSite : CallSite := { sizeOfSite intType with /-- Size intrinsics require the expected declaring identity. -/ private theorem size_identity_cases : (sizeAt (i 40 (.call otherSite)) 4 = false) ∧ + (sizeAt (i 40 (.call { sizeOfSite intType with declared.returnType := .parameter 0 })) + 4 = false) ∧ (sizeAt (i 40 (.call { sizeOfSite intType with - declaringType := .atom "Forged" "System.Runtime.CompilerServices.Unsafe" })) + method.owner := .atom "Forged" "System.Runtime.CompilerServices.Unsafe" })) 4 = false) := by decide +kernel @@ -767,27 +1491,27 @@ private def branchRaw (opcode : Nat) (parameters : List TypeExpr) := { raw [i 2, i 3, i opcode (.target 3), i 42] parameters with returnType := .void } private def branchFeatures : FeatureCertificate := [unknown 0, unknown 1, unknown 2, unknown 0] private def branchStates (left right : ScalarKind) : ScalarCertificate := [some [], some [left], some [right, left], some []] -private def branchCode (emitted : Op) : List Op := [.arg 0, .arg 1, emitted, .ret] -private def branchAt (opcode : Nat) (left right : ScalarKind) (emitted : Op) : Bool := +private def branchCode (emitted : Instruction) : List Instruction := + [.ldarg 0, .ldarg 1, emitted, .ret] +private def branchAt (opcode : Nat) (left right : ScalarKind) (emitted : Instruction) : Bool := let parameters := [left, right].map fun | .native => nativeType | .word32 => intType | .word64 => word | _ => .byref word - decide (ScalarFlowValid #[] (branchRaw opcode parameters) branchFeatures (branchStates left right) (branchCode emitted)) -private def branches : List (Nat × Op) := [ - (46, .beq 3), (47, .bge 3), (48, .bgt 3), (49, .ble 3), (50, .blt 3), (51, .bne 3), (52, .bgeu 3), - (53, .bgtu 3), (54, .bleu 3), (55, .bltu 3), (59, .beq 3), (60, .bge 3), (61, .bgt 3), (62, .ble 3), - (63, .blt 3), (64, .bne 3), (65, .bgeu 3), (66, .bgtu 3), (67, .bleu 3), (68, .bltu 3)] + decide (ScalarFlowValid (branchRaw opcode parameters) branchFeatures (branchStates left right) + (branchCode emitted)) +private def branches : List (Nat × Instruction) := [ + (46, .beq 3), (47, .bge 3), (48, .bgt 3), (49, .ble 3), (50, .blt 3), (51, .bne_un 3), + (52, .bge_un 3), (53, .bgt_un 3), (54, .ble_un 3), (55, .blt_un 3), (59, .beq 3), (60, .bge 3), + (61, .bgt 3), (62, .ble 3), (63, .blt 3), (64, .bne_un 3), (65, .bge_un 3), (66, .bgt_un 3), + (67, .ble_un 3), (68, .blt_un 3)] /-- Branch encodings retain operand kinds and the raw opcode family. -/ private theorem branch_encoding_cases : (branches.all (fun (opcode, emitted) => branchAt opcode .word64 .word64 emitted) = true) ∧ (branches.all (fun (opcode, emitted) => branchAt opcode .word32 .word32 emitted) = true) ∧ - (branches.all (fun (opcode, emitted) => !branchAt opcode .word64 .word64 (.nativeBinary - emitted)) = true) ∧ (branches.all (fun (opcode, _) => !branchAt opcode .word64 .word64 (if opcode ∈ [50, 63] then .bge 3 else .blt 3)) = true) ∧ - (branches.all (fun (opcode, emitted) => branchAt opcode .native .word32 (.nativeBinary - emitted) == (opcode ∈ [46, 47, 48, 49, 50, 59, 60, 61, 62, 63])) = true) ∧ - (branches.all (fun (opcode, emitted) => branchAt opcode .word32 .native (.nativeBinary - emitted) == (opcode ∈ [46, 47, 48, 49, 50, 59, 60, 61, 62, 63])) = true) ∧ - (branches.all (fun (opcode, emitted) => !branchAt opcode .native .word32 emitted) = true) ∧ + (branches.all (fun (opcode, emitted) => branchAt opcode .native .word32 + emitted == (opcode ∈ [46, 47, 48, 49, 50, 59, 60, 61, 62, 63])) = true) ∧ + (branches.all (fun (opcode, emitted) => branchAt opcode .word32 .native + emitted == (opcode ∈ [46, 47, 48, 49, 50, 59, 60, 61, 62, 63])) = true) ∧ (branches.all (fun (opcode, emitted) => !branchAt opcode .native .word64 emitted) = true) ∧ (branches.all (fun (opcode, emitted) => !branchAt opcode .word32 .word64 emitted) = true) ∧ (branches.all (fun (opcode, emitted) => branchAt opcode .reference .reference emitted == @@ -796,9 +1520,10 @@ private theorem branch_encoding_cases : #print axioms branch_encoding_cases -example (valid : ScalarFlowValid #[] addRaw addFeatures addStates addCode) : - scalarEmissionMatches #[] none (i 88) [.word64, .word64] .add = true := - valid.instruction_checked (pc := 2) rfl rfl rfl rfl rfl +example (valid : ScalarFlowValid addRaw addFeatures addStates addCode) : + scalarEmissionMatches none none (i 88) [.word64, .word64] .add = true := + valid.instruction_checked (live := fun _ => true) (decodeSite := fun _ => Instruction.ofRaw) + (pc := 2) (by decide) rfl rfl rfl rfl rfl #print axioms external_call_validated @@ -806,13 +1531,16 @@ example (valid : ScalarFlowValid #[] addRaw addFeatures addStates addCode) : theorem closed_generic_fill_signature : let element := TypeExpr.aggregate ("Scope::Pair`1<" ++ runtime ++ "::System.UInt64>") 0 let site : CallSite := { - declaringType := .apply (.constructor runtime "System.Span`1") element, - name := "Fill", target := .external (runtime ++ - "::System.Void System.Span`1>::Fill(!0)"), - typeArguments := [], parameterCount := 1, hasThis := true, - returnsValue := false, returnType := .void, parameterTypes := [element] } + declared := { convention := 0x20, parameters := [element] }, + method := { + owner := .apply (.constructor runtime "System.Span`1") element, name := name!"Fill", + hasThis := true, genericArity := 0, returnType := .void, parameters := [element] }, + name := "Fill", + target := .external (runtime ++ + "::System.Void System.Span`1>::Fill(!0)"), typeArguments := [], + parameterCount := 1, returnsValue := false } scalarTransfer (raw []) (i 40 (.call site)) [.other, .reference] = some [] ∧ - scalarTransfer (raw []) (i 40 (.call { site with parameterTypes := [word] })) + scalarTransfer (raw []) (i 40 (.call { site with method.parameters := [word] })) [.word64, .reference] = none := by decide +kernel #print axioms closed_generic_fill_signature @@ -822,7 +1550,7 @@ theorem no_implicit_native_coercion : let unsignedNative : TypeExpr := .atom runtime "System.UIntPtr" ([nativeType, unsignedNative].all fun home => let method := { addRaw with locals := [home], parameters := [home], returnType := home } - let call := i 40 (.call { site with parameterCount := 1, parameterTypes := [home] }) + let call := i 40 (.call { site with parameterCount := 1, method.parameters := [home] }) scalarTransfer method (i 10) [.word32] == none && scalarTransfer method (i 10) [.native] == some [] && scalarTransfer method (i 16 (.argument 0)) [.word32] == none && @@ -841,4 +1569,132 @@ theorem native_home_rejects_int32 : ⟨rfl, rfl⟩ #print axioms native_home_rejects_int32 +/-- Every evidence record in this file carries the bytes of its own original body, so the + certificate checks above fail for the reason they state and not for a missing body. -/ +theorem fixtures_carry_their_bodies : + ([addEvidence, literalEvidence, convertEvidence, floatEvidence 107, floatEvidence 108, + localEvidence, argumentEvidence, faultLocalEvidence, nativeHomeEvidence, + nativeReturnEvidence, holeEvidence 88 holeStates, holeEvidence 149 holeStates] ++ + (arithmeticFlagCases ++ arithmeticFlagMutations).map fun entry => + binaryEvidence entry.1).all (fun item => bodyMatches item.body item.original []) = + true := by + decide +kernel +#print axioms fixtures_carry_their_bodies + +/-! ## Only the kernel decides the body and metadata check + +The elaborator evaluates a call by name, so a certificate whose evidence carries a computed +metadata slice would be unfolded again at every use. `bodyMatches` is irreducible, so `decide` +refuses at once, within a small heartbeat budget, and `decide +kernel` decides. -/ + +set_option maxHeartbeats 20000 in +/-- error: Tactic `decide` failed to reduce -/ +#guard_msgs (substring := true) in +example : BodyDecodes [] objectPlatform scope addEvidence.body addEvidence.original + [] addEvidence.signature := by + decide + +set_option maxHeartbeats 20000 in +/-- error: Tactic `decide` failed to reduce -/ +#guard_msgs (substring := true) in +example : TranslationValid [] #[] [] objectPlatform [addEvidence] [] [addMethod] := by decide + +set_option maxHeartbeats 20000 in +/-- error: Tactic `decide` failed to reduce -/ +#guard_msgs (substring := true) in +example : MethodEvidenceValid [] #[] [] objectPlatform [addEvidence] [] addEvidence addMethod := by + decide + +/-- A method with eight times the parameters and four times the locals of `addRaw`. -/ +private def wideRaw : RawMethod := + { addRaw with parameters := List.replicate 16 word, locals := List.replicate 8 intType } + +-- The fixture encoder reduces in the elaborator, within a small budget, at both sizes. +set_option maxHeartbeats 100000 in +example : (encodeBody scope addRaw).isSome = true := by decide + +set_option maxHeartbeats 100000 in +example : (encodeBody scope wideRaw).isSome = true := by decide +example : (encodeBody scope addRaw).isSome = true := by decide +kernel +example : BodyDecodes [] objectPlatform scope addEvidence.body addEvidence.original + [] addEvidence.signature := by + decide +kernel +example : MethodEvidenceValid [] #[] [] objectPlatform [addEvidence] [] addEvidence addMethod := by + decide +kernel +example : MethodEvidenceValid [] #[] [] objectPlatform [addEvidence] [] addEvidence addMethod := by decide +kernel + +/-- A method that fills a block through its byref argument with an unprefixed `initblk`. -/ +private def blockInit : RawMethod := + { raw [i 2, i 22, i 22, i 65048, i 42] [.byref intType] with + maxStack := 3, returnType := .void } + +/-- A hole at a reachable block initialization is refused: the scalar check compares it with + the block operation the decoder gives that pc. -/ +theorem decoded_block_hole_refused : + ¬ ScalarFlowValid blockInit ([0, 1, 2, 3, 0].map unknown) + [some [], some [.reference], some [.word32, .reference], + some [.word32, .word32, .reference], some []] + ((Instruction.ofRawCode none blockInit.code).set 3 (.unadmitted 65048)) := by + decide +kernel +#print axioms decoded_block_hole_refused + +/-- The certificate refused above is valid over the code the decoder gives `blockInit`, so the + refusal comes from the hole at pc 3. -/ +theorem decoded_block_init_valid : + ScalarFlowValid blockInit ([0, 1, 2, 3, 0].map unknown) + [some [], some [.reference], some [.word32, .reference], + some [.word32, .word32, .reference], some []] + (Instruction.ofRawCode none blockInit.code) := by + decide +kernel +#print axioms decoded_block_init_valid + +/-- Evidence refuses an extra static site on a method whose body has none. -/ +theorem addition_rejects_extra_static_site : + let site : StaticDescriptor := { identity := 1, field := value, bytes := [] } + let altered := { addMethod with staticSites := [(0, site)] } + ¬ MethodEvidenceValid [] #[] [] objectPlatform [addEvidence] [] addEvidence altered := by + decide +kernel +#print axioms addition_rejects_extra_static_site + +/-- Valid evidence gives the full scalar encoding check at every reachable pc, from its scalar + check and its code equation conjunct. -/ +theorem checked_method_encodings {rawLayouts : List RawLayout} {layouts : Array ComputedLayout} + {pins : List AssemblyPin} {platform : PlatformTable} {all : List MethodEvidence} {imports : List ImportSlot} + {item : MethodEvidence} {method : Method} + (valid : MethodEvidenceValid rawLayouts layouts pins platform all imports item method) + {pc : Nat} {instruction : RawInstruction} {role : Option FoldRole} {feature : FeatureState} + {stack : List ScalarKind} {emitted : Instruction} + (rawAt : item.closed.code[pc]? = some instruction) + (roleAt : (foldRoles item.closed.code)[pc]? = some role) + (featureAt : item.featureStates[pc]? = some (some feature)) + (stackAt : item.scalarStates[pc]? = some (some stack)) + (emittedAt : method.code[pc]? = some emitted) : + scalarEmissionMatches role (rawBefore (item.closed.code[·]?) pc) instruction stack emitted = + true := by + obtain ⟨_, _, _, _, _, _, _, _, _, _, _, ⟨flow, binding⟩, _⟩ := valid + exact flow.instruction_checked (binding.trans (methodCode_eq ..)) rawAt roleAt featureAt stackAt + emittedAt + +#print axioms checked_method_encodings + +/-- A literal load with a different owner layout cannot use decoded evidence. -/ +theorem wrong_decoded_owner_refused (item : MethodEvidence) : + let owner := TypeExpr.aggregate "Fixture" 0 + let source := raw [i 123 (.field owner 0 intType (name!"Field") false)] + let evidence := { item with closed := source } + let field : FieldRef := ⟨.aggregate "Fixture" 1, 0, intType, name!"Field"⟩ + ¬ MethodEvidenceValid [] #[] [] objectPlatform [evidence] [] evidence + { addMethod with code := [.ldfld field] } := by + intro owner source evidence field valid + have different : [Instruction.ldfld field] ≠ + methodCode addMethod.profile addMethod.environment addMethod.links + item.reachable source := by + rw [methodCode_eq] + simp only [methodInstruction, indexedRead?_eq] + change [Instruction.ldfld field] ≠ [if !item.reachable.toArray[0]?.getD false then + .unadmitted 123 else .ldfld { field with owner := owner }] + cases item.reachable.toArray[0]?.getD false <;> decide +kernel + exact different valid.code_eq +#print axioms wrong_decoded_owner_refused + end Tests.ScalarEvidence diff --git a/Tests/Segment.lean b/Tests/Segment.lean index 2efe49d52..c11647baa 100644 --- a/Tests/Segment.lean +++ b/Tests/Segment.lean @@ -1,3 +1,4 @@ +import Tests.Support.IntrinsicMethods import CIL.Safety.SegmentLocals /-! @@ -11,14 +12,38 @@ and successful memory operations remain explicit premises. namespace Tests.Segment open CIL.Safety +/-- The byref overload of Unsafe.AsRef closed over UInt64. -/ +def asRefUInt64 : CIL.Extraction.MethodRef := + let element := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.UInt64" + { owner := CIL.Extraction.unsafeOwner, name := name!"AsRef", hasThis := false, + genericArity := 1, methodArguments := [element], parameters := [.byref element], + returnType := .byref element } + +/-- Both AsRef calls in the placed prefix use the runtime model. -/ +def prefixUnlinked (body : CIL.Method) (start : Nat) : Prop := + body.linkAt (start + 1) = none ∧ body.linkAt (start + 2) = none + /-- The argument formation sequence used by the vector input prefixes. -/ -def referencePrefix (index : Nat) : List CIL.Op := - [.arg index, .memory .asRef, .memory .asRef] +def referencePrefix (index : Nat) : List CIL.Instruction := + [.ldarg index, .call asRefUInt64, .call asRefUInt64] + +/-- A linked-table miss and a resolved AsRef preserve an already formed reference. -/ +private theorem asRef_step (body : CIL.Method) (pc : Nat) (arguments stack : List Value) + (frame : Frame) (memory : Memory) (reference : Reference) (unlinked : body.linkAt pc = none) + (model : CIL.Extraction.runtimeModel body.environment .call asRefUInt64 = some (.memory .asRef)) + (formed : form memory reference = .ok reference) : + step body (.call asRefUInt64) pc arguments frame (.reference (.address reference) :: stack) memory = + .ok (.next (pc + 1) (.reference (.address reference) :: stack) frame memory) := by + rw [step_call_model _ _ _ _ _ unlinked model rfl _] + cil_checked_step [formed] /-- Argument formation is a reusable segment at any placed position. -/ theorem reference_prefix {program : CIL.Program} {method start : Nat} {body : CIL.Method} (found : program[method]? = some body) (index : Nat) (placed : CodeAt body start (referencePrefix index)) + (unlinked : prefixUnlinked body start) + (model : CIL.Extraction.runtimeModel body.environment .call asRefUInt64 = some (.memory .asRef)) + (plain : body.argumentAccess index = .value none) (arguments : List Value) (frame : Frame) (stack : List Value) (memory : Memory) (reference : Reference) (argument : arguments[index]? = some (.reference (.address reference))) @@ -26,14 +51,23 @@ theorem reference_prefix {program : CIL.Program} {method start : Nat} {body : CI Segment program method start (start + 3) (SegmentState.exact arguments ⟨frame, stack, memory⟩) (SegmentState.exact arguments ⟨frame, .reference (.address reference) :: stack, memory⟩) := by - cil_segment 3 [found, placed, argument, formed] + apply Segment.ofRun (cost := 3) + intro fuel + have load : step body (.ldarg index) start arguments frame stack memory = + .ok (.next (start + 1) (.reference (.address reference) :: stack) frame memory) := by + cil_checked_step [plain, argument, formed] + exact (run_next (fuel := fuel + 2) found (placed.lookup 0 rfl) load).trans + ((run_next (fuel := fuel + 1) found (placed.lookup 1 rfl) + (asRef_step _ _ _ _ _ _ _ unlinked.1 model formed)).trans + (run_next (fuel := fuel) found (placed.lookup 2 rfl) + (asRef_step _ _ _ _ _ _ _ unlinked.2 model formed))) #print axioms reference_prefix /-- Saving the right reference and duplicating the left finishes Add's eight-op prefix. -/ theorem save_right {program : CIL.Program} {method start : Nat} {body : CIL.Method} (found : program[method]? = some body) - (placed : CodeAt body start [.setLocal 0, .dup]) + (placed : CodeAt body start [.stloc 0, .dup]) (arguments : List Value) (frame : Frame) (slots : List LocalSlot) (initial : Option ManagedReference) (memory : Memory) (left right : Reference) (layout : frame.locals = .root initial :: slots) @@ -51,7 +85,11 @@ theorem save_right {program : CIL.Program} {method start : Nat} {body : CIL.Meth /-- The positional Add Vector128 reference signature is a composition of three contracts. -/ theorem add_vector128_inputs {program : CIL.Program} {method : Nat} {body : CIL.Method} (found : program[method]? = some body) - (placed : CodeAt body 0 (referencePrefix 0 ++ referencePrefix 1 ++ [.setLocal 0, .dup])) + (leftUnlinked : prefixUnlinked body 0) (rightUnlinked : prefixUnlinked body 3) + (model : CIL.Extraction.runtimeModel body.environment .call asRefUInt64 = some (.memory .asRef)) + (placed : CodeAt body 0 (referencePrefix 0 ++ referencePrefix 1 ++ [.stloc 0, .dup])) + (leftPlain : body.argumentAccess 0 = .value none) + (rightPlain : body.argumentAccess 1 = .value none) (arguments : List Value) (frame : Frame) (slots : List LocalSlot) (initial : Option ManagedReference) (memory : Memory) (left right : Reference) (layout : frame.locals = .root initial :: slots) @@ -62,10 +100,10 @@ theorem add_vector128_inputs {program : CIL.Program} {method : Nat} {body : CIL. (SegmentState.exact arguments ⟨{ frame with locals := .root (some (.address right)) :: slots }, [.reference (.address left), .reference (.address left)], memory⟩) := by - have first := reference_prefix found 0 placed.append_left.append_left arguments frame [] - memory left leftArgument leftFormed - have second := reference_prefix found 1 placed.append_left.append_right arguments frame - [.reference (.address left)] memory right rightArgument rightFormed + have first := reference_prefix found 0 placed.append_left.append_left leftUnlinked model leftPlain arguments + frame [] memory left leftArgument leftFormed + have second := reference_prefix found 1 placed.append_left.append_right rightUnlinked model rightPlain arguments + frame [.reference (.address left)] memory right rightArgument rightFormed have third := save_right found placed.append_right arguments frame slots initial memory left right layout rightFormed exact (first.trans second).trans third @@ -77,7 +115,10 @@ theorem add_vector128_inputs {program : CIL.Program} {method : Nat} {body : CIL. /-- A placed input-reference formation and Vector256 load uses the original checked read. -/ theorem load256_prefix {program : CIL.Program} {method start : Nat} {body : CIL.Method} (found : program[method]? = some body) (index : Nat) - (placed : CodeAt body start (referencePrefix index ++ [.memory .load256])) + (placed : CodeAt body start (referencePrefix index ++ [.ldobj Tests.IntrinsicMethods.vector256UInt64])) + (unlinked : prefixUnlinked body start) + (model : CIL.Extraction.runtimeModel body.environment .call asRefUInt64 = some (.memory .asRef)) + (plain : body.argumentAccess index = .value none) (arguments : List Value) (frame : Frame) (memory : Memory) (reference : Reference) (vector : BitVec 256) (argument : arguments[index]? = some (.reference (.address reference))) @@ -86,15 +127,21 @@ theorem load256_prefix {program : CIL.Program} {method start : Nat} {body : CIL. Segment program method start (start + 4) (SegmentState.exact arguments ⟨frame, [], memory⟩) (SegmentState.exact arguments ⟨frame, [.scalar (.v256 vector)], memory⟩) := by - cil_segment 4 [found, placed, argument, formed, loaded] + apply (reference_prefix found index placed.append_left unlinked model plain arguments frame [] + memory reference argument formed).trans + apply Segment.ofRun (cost := 1) + intro fuel + apply run_next found (placed.lookup 3 rfl) + rw [step_ldobj_vector32 _ _ _ _ _ (by rfl) _] + cil_checked_step [loaded] #print axioms load256_prefix /-- One local store retains its actual checked step equation and memory transition. -/ theorem store256 {program : CIL.Program} {method start index : Nat} {body : CIL.Method} - (found : program[method]? = some body) (placed : CodeAt body start [.setLocal index]) + (found : program[method]? = some body) (placed : CodeAt body start [.stloc index]) (arguments : List Value) (frame : Frame) (before after : Memory) (vector : BitVec 256) - (stored : step body (.setLocal index) start arguments frame [.scalar (.v256 vector)] before = + (stored : step body (.stloc index) start arguments frame [.scalar (.v256 vector)] before = .ok (.next (start + 1) [] frame after)) : Segment program method start (start + 1) (SegmentState.exact arguments ⟨frame, [.scalar (.v256 vector)], before⟩) @@ -106,19 +153,22 @@ theorem store256 {program : CIL.Program} {method start index : Nat} {body : CIL. /-- The Multiply AVX2 left-input stage composes load and store at the original positions. -/ theorem multiply_left_input {program : CIL.Program} {method : Nat} {body : CIL.Method} (found : program[method]? = some body) - (placed : CodeAt body 44 (referencePrefix 0 ++ [.memory .load256, .setLocal 23])) + (leftUnlinked : prefixUnlinked body 44) + (model : CIL.Extraction.runtimeModel body.environment .call asRefUInt64 = some (.memory .asRef)) + (placed : CodeAt body 44 (referencePrefix 0 ++ [.ldobj Tests.IntrinsicMethods.vector256UInt64, .stloc 23])) + (plain : body.argumentAccess 0 = .value none) (arguments : List Value) (frame : Frame) (before after : Memory) (left : Reference) (vector : BitVec 256) (argument : arguments[0]? = some (.reference (.address left))) (formed : form before left = .ok left) (loaded : loadValue before (.address left) 32 = .ok (.v256 vector)) - (stored : step body (.setLocal 23) 48 arguments frame [.scalar (.v256 vector)] before = + (stored : step body (.stloc 23) 48 arguments frame [.scalar (.v256 vector)] before = .ok (.next 49 [] frame after)) : Segment program method 44 49 (SegmentState.exact arguments ⟨frame, [], before⟩) (SegmentState.exact arguments ⟨frame, [], after⟩) := by - have split : CodeAt body 44 ((referencePrefix 0 ++ [.memory .load256]) ++ [.setLocal 23]) := + have split : CodeAt body 44 ((referencePrefix 0 ++ [.ldobj Tests.IntrinsicMethods.vector256UInt64]) ++ [.stloc 23]) := placed - exact (load256_prefix found 0 split.append_left arguments frame before left vector + exact (load256_prefix found 0 split.append_left leftUnlinked model plain arguments frame before left vector argument formed loaded).trans (store256 found split.append_right arguments frame before after vector stored) @@ -128,7 +178,8 @@ theorem multiply_left_input {program : CIL.Program} {method : Nat} {body : CIL.M /-- The AVX2 permutation stage retains availability and exact intrinsic semantics. -/ theorem reverse256 {program : CIL.Program} {method start : Nat} {body : CIL.Method} (found : program[method]? = some body) - (placed : CodeAt body start [.const32 27, .intrinsic (.avx2 .permute4x64) 2]) + (placed : CodeAt body start [.ldc_i4 27, .call Tests.IntrinsicMethods.permute]) + (unlinked : body.linkAt (start + 1) = none) (arguments : List Value) (frame : Frame) (memory : Memory) (vector reversed : BitVec 256) (available : (CIL.Intrinsic.avx2 .permute4x64).available body.profile = true) (permuted : checkedIntrinsic (.avx2 .permute4x64) [.v256 vector, .i32 27] = @@ -137,27 +188,33 @@ theorem reverse256 {program : CIL.Program} {method start : Nat} {body : CIL.Meth (SegmentState.exact arguments ⟨frame, [.scalar (.v256 vector)], memory⟩) (SegmentState.exact arguments ⟨frame, [.scalar (.v256 reversed)], memory⟩) := by apply Segment.ofRun (cost := 2) - have reversedStep : step body (.intrinsic (.avx2 .permute4x64) 2) (start + 1) + have reversedStep : step body (.call Tests.IntrinsicMethods.permute) (start + 1) arguments frame [.scalar (.i32 27), .scalar (.v256 vector)] memory = .ok (.next (start + 2) [.scalar (.v256 reversed)] frame memory) := by - rw [step_intrinsic_eq] - simp only [available, Bool.not_true, Bool.false_eq_true, ↓reduceIte] + rw [step_call_intrinsic _ _ _ _ _ unlinked (Tests.IntrinsicMethods.permute_model _) rfl] + simp only [intrinsicCall, available, Bool.not_true, Bool.false_eq_true, ↓reduceIte] change (do let value ← checkedIntrinsic (.avx2 .permute4x64) [.v256 vector, .i32 27] let checked ← checkedValue memory (.scalar value) pure (FrameAction.next (start + 2) [checked] frame memory)) = _ rw [permuted] rfl - cil_segment_steps [found, placed, reversedStep] + cil_segment_steps [found, placed, reversedStep, unlinked] + all_goals exact run_next found (placed.lookup 1 rfl) reversedStep #print axioms reverse256 /-- Multiply's two vector input stages compose through the checked intermediate memory. -/ theorem multiply_inputs {program : CIL.Program} {method : Nat} {body : CIL.Method} (found : program[method]? = some body) - (leftPlaced : CodeAt body 44 (referencePrefix 0 ++ [.memory .load256, .setLocal 23])) - (rightPlaced : CodeAt body 49 (referencePrefix 1 ++ [.memory .load256, - .const32 27, .intrinsic (.avx2 .permute4x64) 2, .setLocal 24])) + (unlinked : body.linkAt 54 = none) + (leftUnlinked : prefixUnlinked body 44) (rightUnlinked : prefixUnlinked body 49) + (model : CIL.Extraction.runtimeModel body.environment .call asRefUInt64 = some (.memory .asRef)) + (leftPlaced : CodeAt body 44 (referencePrefix 0 ++ [.ldobj Tests.IntrinsicMethods.vector256UInt64, .stloc 23])) + (rightPlaced : CodeAt body 49 (referencePrefix 1 ++ [.ldobj Tests.IntrinsicMethods.vector256UInt64, + .ldc_i4 27, .call Tests.IntrinsicMethods.permute, .stloc 24])) + (leftPlain : body.argumentAccess 0 = .value none) + (rightPlain : body.argumentAccess 1 = .value none) (arguments : List Value) (frame : Frame) (before middle after : Memory) (left right : Reference) (leftVector rightVector reversed : BitVec 256) (leftArgument : arguments[0]? = some (.reference (.address left))) @@ -165,22 +222,22 @@ theorem multiply_inputs {program : CIL.Program} {method : Nat} {body : CIL.Metho (leftFormed : form before left = .ok left) (rightFormed : form middle right = .ok right) (leftLoaded : loadValue before (.address left) 32 = .ok (.v256 leftVector)) (rightLoaded : loadValue middle (.address right) 32 = .ok (.v256 rightVector)) - (leftStored : step body (.setLocal 23) 48 arguments frame [.scalar (.v256 leftVector)] before = + (leftStored : step body (.stloc 23) 48 arguments frame [.scalar (.v256 leftVector)] before = .ok (.next 49 [] frame middle)) - (rightStored : step body (.setLocal 24) 55 arguments frame [.scalar (.v256 reversed)] middle = + (rightStored : step body (.stloc 24) 55 arguments frame [.scalar (.v256 reversed)] middle = .ok (.next 56 [] frame after)) (available : (CIL.Intrinsic.avx2 .permute4x64).available body.profile = true) (permuted : checkedIntrinsic (.avx2 .permute4x64) [.v256 rightVector, .i32 27] = .ok (.v256 reversed)) : Segment program method 44 56 (SegmentState.exact arguments ⟨frame, [], before⟩) (SegmentState.exact arguments ⟨frame, [], after⟩) := by - have first := multiply_left_input found leftPlaced arguments frame before middle left + have first := multiply_left_input found leftUnlinked model leftPlaced leftPlain arguments frame before middle left leftVector leftArgument leftFormed leftLoaded leftStored - have split : CodeAt body 49 (((referencePrefix 1 ++ [.memory .load256]) ++ - [.const32 27, .intrinsic (.avx2 .permute4x64) 2]) ++ [.setLocal 24]) := rightPlaced - have loaded := load256_prefix found 1 split.append_left.append_left arguments frame middle - right rightVector rightArgument rightFormed rightLoaded - have reversed := reverse256 found split.append_left.append_right arguments frame middle + have split : CodeAt body 49 (((referencePrefix 1 ++ [.ldobj Tests.IntrinsicMethods.vector256UInt64]) ++ + [.ldc_i4 27, .call Tests.IntrinsicMethods.permute]) ++ [.stloc 24]) := rightPlaced + have loaded := load256_prefix found 1 split.append_left.append_left rightUnlinked model rightPlain arguments + frame middle right rightVector rightArgument rightFormed rightLoaded + have reversed := reverse256 found split.append_left.append_right unlinked arguments frame middle rightVector reversed available permuted have stored := store256 found split.append_right arguments frame middle after _ rightStored exact ((first.trans loaded).trans reversed).trans stored @@ -195,7 +252,8 @@ theorem multiply_inputs {program : CIL.Program} {method : Nat} {body : CIL.Metho /-- Two actual vector stores preserve every other remembered local in one frame-rule use. -/ -theorem multiply_untouched {before middle after entered : Memory} {boundary : Nat} +theorem multiply_untouched {body : CIL.Method} {before middle after entered : Memory} + {boundary : Nat} {kinds : List CIL.LocalKind} {slots : List LocalSlot} {known : Nat → Option CIL.Value} {leftHome rightHome : Reference} (homes : WritableHomes entered boundary kinds slots) @@ -205,10 +263,10 @@ theorem multiply_untouched {before middle after entered : Memory} {boundary : Na (leftVector rightVector : BitVec 256) (leftWritten : write before leftHome (numberBytes leftVector.toNat 32) 1 = .ok middle) (rightWritten : write middle rightHome (numberBytes rightVector.toNat 32) 1 = .ok after) : - LocalSnapshots after slots (untouchedLocals [.setLocal 23, .setLocal 24] known) := by - have first : SegmentLocalEffects [.setLocal 23] slots before middle := + LocalSnapshots after slots (untouchedLocals body [.stloc 23, .stloc 24] known) := by + have first : SegmentLocalEffects body [.stloc 23] slots before middle := .store 23 .vector256 leftHome _ 1 (by simp [segmentStores]) leftSlot leftWritten - have second : SegmentLocalEffects [.setLocal 24] slots middle after := + have second : SegmentLocalEffects body [.stloc 24] slots middle after := .store 24 .vector256 rightHome _ 1 (by simp [segmentStores]) rightSlot rightWritten exact (first.append second).frame homes snapshots.untouched @@ -224,8 +282,8 @@ theorem aliased_homes_rejected (memory : Memory) (boundary : Nat) (home : Refere #print axioms aliased_homes_rejected /-- A region with no direct stores cannot certify a hidden memory mutation. -/ -theorem no_hidden_write {slots : List LocalSlot} {before after : Memory} - (effects : SegmentLocalEffects [.store64] slots before after) : before = after := by +theorem no_hidden_write {body : CIL.Method} {slots : List LocalSlot} {before after : Memory} + (effects : SegmentLocalEffects body [.stind .i8] slots before after) : before = after := by induction effects with | refl => rfl | store index kind home bytes alignment destination => simp [segmentStores] at destination @@ -234,13 +292,31 @@ theorem no_hidden_write {slots : List LocalSlot} {before after : Memory} #print axioms no_hidden_write /-- A declared destination cannot preserve its own old snapshot through the frame mask. -/ -theorem stored_local_not_framed (known : Nat → Option CIL.Value) : - untouchedLocals [.setLocal 23, .setLocal 24] known 23 = none ∧ - untouchedLocals [.setLocal 23, .setLocal 24] known 7 = known 7 := by +theorem stored_local_not_framed (body : CIL.Method) (known : Nat → Option CIL.Value) : + untouchedLocals body [.stloc 23, .stloc 24] known 23 = none ∧ + untouchedLocals body [.stloc 23, .stloc 24] known 7 = known 7 := by simp [untouchedLocals, segmentStores] #print axioms stored_local_not_framed +/-- A body whose one argument has its home in slot 0, as a store to the argument requires. -/ +private def homedArgumentBody : CIL.Method := + { locals := [], parameterHomes := [(0, .word32)], returnsValue := false, + code := [.ldc_i4 7, .starg 0, .ret] } + +/-- A store to a homed argument names its home slot: its write is a segment effect, and the + slot's old snapshot is not framed, as for the `stloc` of that slot. -/ +theorem argument_store_effect {slots : List LocalSlot} {before after : Memory} + {home : Reference} {bytes : List (BitVec 8)} {alignment : Nat} + (known : Nat → Option CIL.Value) (slot : slots[0]? = some (.bytes .word32 home)) + (written : write before home bytes alignment = .ok after) : + SegmentLocalEffects homedArgumentBody [.ldc_i4 7, .starg 0] slots before after ∧ + untouchedLocals homedArgumentBody [.ldc_i4 7, .starg 0] known 0 = none := + ⟨.store 0 .word32 home bytes alignment (by decide) slot written, + by rw [untouchedLocals, if_pos (by decide)]⟩ + +#print axioms argument_store_effect + /-- Segment composition requires the exact intermediate position and postcondition. -/ theorem composition_boundaries {program : CIL.Program} {method : Nat} {pre middle post other : List Value → SegmentState → Prop} @@ -255,7 +331,7 @@ theorem composition_boundaries {program : CIL.Program} {method : Nat} /-- Wrong opcodes, endpoint states and missing memory facts do not yield a segment proof. -/ theorem checked_boundaries {program : CIL.Program} {method : Nat} {body : CIL.Method} - (_found : program[method]? = some body) (_placed : CodeAt body 0 [.arg 0]) + (_found : program[method]? = some body) (_placed : CodeAt body 0 [.ldarg 0]) (_frame : Frame) (_memory : Memory) : True := by fail_if_success have : (SegmentRun program method 0 1 1 [.scalar (.i64 4)] @@ -278,7 +354,7 @@ theorem checked_boundaries {program : CIL.Program} {method : Nat} {body : CIL.Me /-- A placed numeric-store contract gains untouched snapshots from its actual write effect. -/ theorem framed_store {program : CIL.Program} {method start : Nat} {body : CIL.Method} - (found : program[method]? = some body) (placed : CodeAt body start [.setLocal 23]) + (found : program[method]? = some body) (placed : CodeAt body start [.stloc 23]) (arguments : List Value) (frame : Frame) (before after entered : Memory) (boundary : Nat) (kinds : List CIL.LocalKind) (home : Reference) (vector : BitVec 256) (known : Nat → Option CIL.Value) @@ -286,12 +362,13 @@ theorem framed_store {program : CIL.Program} {method start : Nat} {body : CIL.Me (snapshots : LocalSnapshots before frame.locals known) (slot : frame.locals[23]? = some (.bytes .vector256 home)) (written : write before home (numberBytes vector.toNat 32) 1 = .ok after) - (stored : step body (.setLocal 23) start arguments frame [.scalar (.v256 vector)] before = + (stored : step body (.stloc 23) start arguments frame [.scalar (.v256 vector)] before = .ok (.next (start + 1) [] frame after)) : Segment program method start (start + 1) (SegmentState.exact arguments ⟨frame, [.scalar (.v256 vector)], before⟩) (fun actual state => SegmentState.exact arguments ⟨frame, [], after⟩ actual state ∧ - LocalSnapshots state.memory state.frame.locals (untouchedLocals [.setLocal 23] known)) := by + LocalSnapshots state.memory state.frame.locals + (untouchedLocals body [.stloc 23] known)) := by apply Segment.frameLocals found placed rfl homes snapshots apply (store256 found placed arguments frame before after vector stored).consequence (fun _ _ valid => valid) @@ -305,8 +382,8 @@ theorem framed_store {program : CIL.Program} {method start : Nat} {body : CIL.Me /-- A frame-rule placement must belong to the running method and match its end boundary. -/ theorem framing_boundaries {program : CIL.Program} {method start : Nat} {body other : CIL.Method} (found : program[method]? = some body) - (_wrongBody : CodeAt other start [.setLocal 23]) - (placed : CodeAt body start [.setLocal 23]) + (_wrongBody : CodeAt other start [.stloc 23]) + (placed : CodeAt body start [.stloc 23]) {entered initial : Memory} {boundary : Nat} {kinds : List CIL.LocalKind} {slots : List LocalSlot} {known : Nat → Option CIL.Value} {pre post : List Value → SegmentState → Prop} @@ -314,13 +391,13 @@ theorem framing_boundaries {program : CIL.Program} {method start : Nat} (snapshots : LocalSnapshots initial slots known) (contract : Segment program method start (start + 1) pre fun arguments state => post arguments state ∧ state.frame.locals = slots ∧ - SegmentLocalEffects [.setLocal 23] slots initial state.memory) + SegmentLocalEffects body [.stloc 23] slots initial state.memory) (_wrongEnd : Segment program method start (start + 2) pre fun arguments state => post arguments state ∧ state.frame.locals = slots ∧ - SegmentLocalEffects [.setLocal 23] slots initial state.memory) : + SegmentLocalEffects body [.stloc 23] slots initial state.memory) : Segment program method start (start + 1) pre fun arguments state => post arguments state ∧ LocalSnapshots state.memory state.frame.locals - (untouchedLocals [.setLocal 23] known) := by + (untouchedLocals body [.stloc 23] known) := by fail_if_success have := Segment.frameLocals found _wrongBody rfl homes snapshots contract fail_if_success diff --git a/Tests/SegmentContract.lean b/Tests/SegmentContract.lean new file mode 100644 index 000000000..9d17f88f4 --- /dev/null +++ b/Tests/SegmentContract.lean @@ -0,0 +1,483 @@ +import CIL +import Tests.Support + +/-! Checked segment contracts bind placed opcodes and preserve interpreter outcomes. -/ + +namespace Tests.SegmentContract +open CIL.Safety + +/-- A scalar boundary with arbitrary caller memory and frame. -/ +def scalarState (frame : Frame) (memory : Memory) (value : CIL.W32) : SegmentState := + { frame := frame, stack := [.scalar (.i32 value)], memory := memory } + +/-- An increment retains the frame, arguments and memory at its continuation. -/ +def increment (finish : Nat) (arguments : List Value) (frame : Frame) (memory : Memory) + (value : CIL.W32) : CIL.Safety.SegmentContract := + .continuing finish ((scalarState frame memory value).exact arguments) + (fun actual _ after => (scalarState frame memory (value + 1)).exact arguments actual after) + (fun _ _ => []) + +/-- One placement proof binds both scalar instructions at every use of this contract. -/ +theorem scalar_region {program : CIL.Program} {method start : Nat} {body : CIL.Method} + (found : program[method]? = some body) (placed : CodeAt body start [.ldc_i4 1, .add]) + (arguments : List Value) (frame : Frame) (memory : Memory) (value : CIL.W32) : + (increment (start + 2) arguments frame memory value).Holds program method start := by + apply Segment.contract (by + dsimp only [scalarState] + cil_segment 2 [found, placed]) (by omega) + intro actual before after permitted satisfied + rcases permitted.1 with rfl + rcases permitted.2 with rfl + rcases satisfied.2 with rfl + exact Confined.refl [] memory + +#print axioms scalar_region + +/-- Changing the arithmetic instruction invalidates the original placement proof. -/ +def changed : CIL.Method := + { returnsValue := true, locals := [], code := [.ldc_i4 1, .sub, .ret] } + +/-- Kernel-checked placement rejects reuse after an instruction changes. -/ +theorem mutation_rejected : Not (CodeAt changed 0 [.ldc_i4 1, .add]) := by + fail_if_success have placed : CodeAt changed 0 [.ldc_i4 1, .add] := by rfl + simp [CodeAt, changed] + +#print axioms mutation_rejected + +/-- Division by zero provides an arithmetic fault with a CLR exception name. -/ +def division : CIL.Method := { returnsValue := true, locals := [], code := [.div] } + +/-- A divide-by-zero contract accepts the exact incoming scalar stack. -/ +def dividing (arguments : List Value) (frame : Frame) (memory : Memory) : + CIL.Safety.SegmentContract where + pre := SegmentState.exact arguments + { frame := frame, stack := [.scalar (.i32 0), .scalar (.i32 1)], memory := memory } + post _ _ exit := exit.Raises .divideByZero + +/-- The exception contract is witnessed by the checked interpreter's arithmetic fault. -/ +theorem raises_region (arguments : List Value) (frame : Frame) (memory : Memory) : + (dividing arguments frame memory).Holds [division] 0 0 := by + intro actual before permitted + rcases permitted.1 with rfl + rcases permitted.2 with rfl + refine Exists.intro (SegmentExit.faults + (LocatedFault.mk 0 0 (.arithmetic .divideByZero))) (And.intro ?_ (And.intro trivial ?_)) + next => + exact And.intro (by decide) (Exists.intro 1 (by rfl)) + next => + exact Exists.intro _ (And.intro rfl rfl) + +#print axioms raises_region + +/-- The exception bridge preserves the CLR name at the run boundary. -/ +theorem raises_run (arguments : List Value) (frame : Frame) (memory : Memory) : + RunRaisesCLR [division] 0 0 arguments frame + [.scalar (.i32 0), .scalar (.i32 1)] memory .divideByZero := by + exact (raises_region arguments frame memory).raises (And.intro rfl rfl) + (fun _ result => Or.inl result.post) + +#print axioms raises_run + +/-- The scalar prefix is followed by an ordinary value return. -/ +def returning : CIL.Method := + { returnsValue := true, locals := [], code := [.ldc_i4 1, .add, .ret] } + +/-- Consuming a reaching summary proves the actual returned scalar and released frame memory. -/ +theorem scalar_return (arguments : List Value) (frame : Frame) (memory : Memory) + (value : CIL.W32) : + WP (Execution (fun fuel => run [returning] fuel 0 0 arguments frame + [.scalar (.i32 value)] memory)) + (fun result => result = (leaveFrame frame memory, [.scalar (.i32 (value + 1))])) := by + apply (scalar_region (program := [returning]) (method := 0) (start := 0) + (by rfl) (by rfl) arguments frame memory value).wp (And.intro rfl rfl) + intro exit result + cases exit with + | reaches pc after => + have satisfied := result.post + rcases satisfied.1 with rfl + rcases satisfied.2.2 with rfl + exact Exists.intro 1 (Exists.intro _ (And.intro (by rfl) rfl)) + | returns memory values => exact False.elim result.post + | faults fault => exact False.elim result.post + | diverges => exact False.elim result.post + +#print axioms scalar_return + +/-- A terminating scalar body cannot exhibit a positive-cost return to its unchanged entry. -/ +theorem empty_region_refused (arguments : List Value) (frame : Frame) (memory : Memory) + (value : CIL.W32) : + Not ((CIL.Safety.SegmentContract.continuing 0 + ((scalarState frame memory value).exact arguments) + (fun _ before after => after = before) (fun _ _ => [])).Holds [returning] 0 0) := by + intro holds + cases holds arguments (scalarState frame memory value) (And.intro rfl rfl) + rename_i exit witness + have exhibited := witness.1 + have post := witness.2.2 + cases exit with + | reaches pc after => + rcases post.1 with rfl + rcases post.2 with rfl + cases exhibited + rename_i cost progress + have positive := progress.1 + have reached := progress.2 + have returned : run [returning] 3 0 0 arguments frame [.scalar (.i32 value)] memory = + .ok (leaveFrame frame memory, [.scalar (.i32 (value + 1))]) := by rfl + have spent := reached.spends 3 + (.ok (leaveFrame frame memory, [.scalar (.i32 (value + 1))])) trivial returned + have bound := spent.1 + have continued := spent.2 + have remaining : 3 - cost = 0 \/ 3 - cost = 1 \/ 3 - cost = 2 := by omega + rcases remaining with zero | one | two + all_goals simp only [*] at continued; cases continued + | returns memory values => exact post + | faults fault => exact post + | diverges => exact post + +#print axioms empty_region_refused + +namespace DistinctArguments + +/-- The caller increments the result of a two-argument call. -/ +def caller : CIL.Method := + { returnsValue := true, locals := [], code := [.call (Tests.callee 2) {}, .ldc_i4 1, .add, .ret], + links := [(0, .direct 1)] } + +/-- Subtraction distinguishes the callee's argument order. -/ +def callee : CIL.Method := + { returnsValue := true, locals := [], code := [.ldarg 0, .ldarg 1, .sub, .ret] } + +/-- The top-first stack places the second argument above the first argument. -/ +def before (frame : Frame) (memory : Memory) : SegmentState := + { frame := frame, stack := [.scalar (.i32 2), .scalar (.i32 7)], memory := memory } + +/-- The call reaches its continuation with first minus second on the stack. -/ +theorem call_region (arguments : List Value) (frame : Frame) (memory : Memory) : + (CIL.Safety.SegmentContract.continuing 1 ((before frame memory).exact arguments) + (fun actual _ after => (scalarState frame memory 5).exact arguments actual after) + (fun _ _ => [])).Holds [caller, callee] 0 0 := by + intro actual state permitted + rcases permitted.1 with rfl + rcases permitted.2 with rfl + have reached : ReachesRun [caller, callee] 0 0 1 1 actual + (before frame memory) (scalarState frame memory 5) := by + refine ReachesRun.call (program := [caller, callee]) (method := 0) (pc := 0) (callee := 1) + (body := caller) (child := callee) (op := .call (Tests.callee 2)) + (values := [.scalar (.i32 7), .scalar (.i32 2)]) (results := [.scalar (.i32 5)]) + (rest := []) (childFrame := { activation := memory.nextIdentity, locals := [], owned := [] }) + (updated := memory) (entered := memory) (calleeFuel := 4) ?_ ?_ ?_ ?_ ?_ ?_ + all_goals rfl + exact Exists.intro (.reaches 1 (scalarState frame memory 5)) + (And.intro (Exists.intro 1 (And.intro (by decide) reached)) + (And.intro (Confined.refl [] memory) (And.intro rfl (And.intro rfl rfl)))) + +#print axioms call_region + +/-- A call summary composes with its arithmetic continuation without repeating the callee proof. -/ +theorem call_composition (arguments : List Value) (frame : Frame) (memory : Memory) : + (CIL.Safety.SegmentContract.continuing 3 ((before frame memory).exact arguments) + (fun actual _ final => Exists fun after => + And ((scalarState frame memory 5).exact arguments actual after) + ((scalarState frame memory (5 + 1)).exact arguments actual final)) + (fun _ _ => [])).Holds [caller, callee] 0 0 := by + exact (call_region arguments frame memory).then + (scalar_region (program := [caller, callee]) (method := 0) (start := 1) + (by rfl) (by rfl) arguments frame memory 5) + (by intro actual state after _ satisfied; exact satisfied) + (by intro actual state after _ _; exact And.intro (by simp) (by simp)) + +#print axioms call_composition + +end DistinctArguments + +/-- A backward branch retains the same boundary state for every iteration. -/ +def spin : CIL.Method := { returnsValue := false, locals := [], code := [.br 0] } + +/-- Every fuel budget for the backward branch ends in exhaustion. -/ +theorem diverging_region (arguments : List Value) (state : SegmentState) : + ({ pre := state.exact arguments + post := fun _ _ exit => exit = .diverges } : + CIL.Safety.SegmentContract).Holds [spin] 0 0 := by + intro actual before permitted + rcases permitted.1 with rfl + rcases permitted.2 with rfl + refine Exists.intro .diverges (And.intro ?_ (And.intro trivial rfl)) + intro fuel + refine Exists.intro (LocatedFault.mk 0 0 .fuelExhausted) (And.intro rfl ?_) + induction fuel with + | zero => rfl + | succ fuel previous => + cil_checked_steps 1 [spin] + exact previous + +#print axioms diverging_region + +/-- Two scalar summaries retain their intermediate state without repeating instruction proofs. -/ +theorem scalar_composition {program : CIL.Program} {method start : Nat} {body : CIL.Method} + (found : program[method]? = some body) + (placed : CodeAt body start ([.ldc_i4 1, .add] ++ [.ldc_i4 1, .add])) + (arguments : List Value) (frame : Frame) (memory : Memory) (value : CIL.W32) : + (CIL.Safety.SegmentContract.continuing (start + 4) + ((scalarState frame memory value).exact arguments) + (fun actual _before final => Exists fun after => + And ((scalarState frame memory (value + 1)).exact arguments actual after) + ((scalarState frame memory (value + 1 + 1)).exact arguments actual final)) + (fun _ _ => [])).Holds program method start := by + have first := scalar_region found placed.append_left arguments frame memory value + have second := scalar_region found placed.append_right arguments frame memory (value + 1) + have joined := first.then (footprint := fun _ _ => []) second + (by intro actual before after _ satisfied; exact satisfied) + (by intro actual before after _ _; exact And.intro (by simp) (by simp)) + simpa [increment, Nat.add_assoc] using joined + +#print axioms scalar_composition + +/-- A duplicate retains a scalar for the arithmetic region after a conditional branch. -/ +def duplicated (frame : Frame) (memory : Memory) (value : CIL.W32) : SegmentState := + { frame := frame, stack := [.scalar (.i32 value), .scalar (.i32 value)], memory := memory } + +/-- Both successors increment the retained scalar and reach their respective region boundaries. -/ +def branchIncrement (start : Nat) (arguments : List Value) (frame : Frame) (memory : Memory) + (value : CIL.W32) : CIL.Safety.SegmentContract where + pre := (duplicated frame memory value).exact arguments + post _ _ exit := exit = .reaches (if value = 0#32 then start + 6 else start + 4) + (scalarState frame memory (value + 1)) + +/-- A branch and its two arithmetic successors compose using checked placement facts. -/ +theorem branch_composition {program : CIL.Program} {method start : Nat} {body : CIL.Method} + (found : program[method]? = some body) + (placed : CodeAt body start + [.dup, .brfalse (start + 4), .ldc_i4 1, .add, .ldc_i4 1, .add]) + (arguments : List Value) (frame : Frame) (memory : Memory) (value : CIL.W32) : + (branchIncrement start arguments frame memory value).Holds program method (start + 1) := by + let next := fun pc => if pc = start + 4 then + increment (start + 6) arguments frame memory value else if pc = start + 2 then + increment (start + 4) arguments frame memory value else + CIL.Safety.SegmentContract.unreachable + have takenRegion := scalar_region found (by + simpa using placed.drop 4) arguments frame memory value + have otherRegion := scalar_region found (by + simpa using (placed.drop 2).take 2) arguments frame memory value + have rest : forall pc, (next pc).Holds program method pc := by + intro pc + by_cases taken : pc = start + 4 + next => + subst pc + simpa [next, Nat.add_assoc] using takenRegion + next => + by_cases other : pc = start + 2 + next => + subst pc + simpa [next, Nat.add_assoc] using otherRegion + next => + simpa [next, taken, other] using + CIL.Safety.SegmentContract.unreachable_holds program method pc + apply CIL.Safety.SegmentContract.branch_seq (target := start + 4) + ((duplicated frame memory value).exact arguments) (fun _ _ => value = 0#32) + (fun _ _ => [.scalar (.i32 value)]) found (placed.lookup 1 (by rfl)) + (next := next) (rest := rest) + next => + intro actual before permitted + rcases permitted.1 with rfl + rcases permitted.2 with rfl + by_cases zero : value = 0#32 <;> + simp [duplicated, step_brfalse_i32_eq, CIL.truth, zero, Nat.add_assoc] <;> rfl + next => + intro actual before permitted + exact permitted + next => + intro actual before permitted + rcases permitted.1 with rfl + rcases permitted.2 with rfl + by_cases zero : value = 0#32 <;> + simp [next, zero, increment, CIL.Safety.SegmentContract.continuing, + scalarState, duplicated, SegmentState.exact, Nat.add_assoc] + next => + intro actual before exit permitted satisfied framed + rcases permitted.1 with rfl + rcases permitted.2 with rfl + by_cases zero : value = 0#32 + all_goals + simp [next, zero, Nat.add_assoc] at satisfied framed + cases exit with + | reaches pc after => + rcases satisfied.1 with rfl + rcases satisfied.2.2 with rfl + exact And.intro (Confined.refl [] memory) (by + simp [branchIncrement, zero, scalarState, duplicated]) + | returns memory values => exact False.elim satisfied + | faults fault => exact False.elim satisfied + | diverges => exact False.elim satisfied + +#print axioms branch_composition + +/-- Duplicating the scalar before the branch composes with both validated successor summaries. -/ +theorem duplicate_branch {program : CIL.Program} {method start : Nat} {body : CIL.Method} + (found : program[method]? = some body) + (placed : CodeAt body start + [.dup, .brfalse (start + 4), .ldc_i4 1, .add, .ldc_i4 1, .add]) + (arguments : List Value) (frame : Frame) (memory : Memory) (value : CIL.W32) : + ({ branchIncrement start arguments frame memory value with + pre := (scalarState frame memory value).exact arguments } : + CIL.Safety.SegmentContract).Holds program method start := by + have duplicate : Segment program method start (start + 1) + ((scalarState frame memory value).exact arguments) + ((duplicated frame memory value).exact arguments) := by + dsimp only [scalarState, duplicated] + cil_segment 1 [found, placed] + have head := duplicate.contract (by omega) (footprint := fun _ _ => []) (by + intro actual before after permitted satisfied + rcases permitted.1 with rfl + rcases permitted.2 with rfl + rcases satisfied.2 with rfl + exact Confined.refl [] memory) + let next := fun pc => if pc = start + 1 then + branchIncrement start arguments frame memory value else + CIL.Safety.SegmentContract.unreachable + apply head.seq (next := next) + next => + intro pc + by_cases equal : pc = start + 1 + next => + subst pc + simpa [next] using branch_composition found placed arguments frame memory value + next => + simpa [next, equal] using + CIL.Safety.SegmentContract.unreachable_holds program method pc + next => + intro actual before permitted + exact permitted + next => + intro actual before pc after _ satisfied + rcases satisfied.1 with rfl + have satisfied := satisfied.2 + simpa [next, branchIncrement] using satisfied + next => + intro actual before exit _ stopped satisfied + cases exit <;> simp_all [CIL.Safety.SegmentContract.continuing] + next => + intro actual before pc after exit permitted satisfied _ post framed + rcases permitted.1 with rfl + rcases permitted.2 with rfl + rcases satisfied.1 with rfl + rcases satisfied.2.2 with rfl + simpa only [next, ite_eq_left rfl, branchIncrement, scalarState, duplicated, + SegmentExit.Confined] using (And.intro framed post) + +#print axioms duplicate_branch + +namespace StoredReturn + +/-- One writable caller allocation whose four bytes initially contain zero. -/ +def initial : Memory where + allocations := fun index => if index = 0 then + some { kind := .callerStack, layout := { size := 4, alignment := 1 } } else none + cells := fun _ _ => { bits := 0, initialized := true } + views := [{ allocation := 0, start := 0, length := 4, readable := true, writable := true }] + nextIdentity := 1 + +/-- The byte home used by the store. -/ +def home : Reference := { allocation := 0, offset := 0 } + +/-- The store writes one to the first byte and keeps the other three bytes zero. -/ +def finalMemory : Memory := + match write initial home [1, 0, 0, 0] 1 with + | .ok memory => memory + | .error _ => initial + +/-- The store succeeds against the caller's writable home. -/ +theorem written : write initial home [1, 0, 0, 0] 1 = .ok finalMemory := by rfl +#print axioms written + +/-- The store is confined to its four-byte footprint. -/ +theorem confined : Confined [(home, 4)] initial finalMemory := + Confined.write written (by decide) +#print axioms confined + +/-- The store changes an existing byte, so an empty footprint cannot describe it. -/ +theorem not_empty : Not (Confined [] initial finalMemory) := by + intro framed + have same := framed.cells 0 (by decide) 0 (by simp [Outside]) + have bits := congrArg Cell.bits same + change (1 : BitVec 8) = 0 at bits + contradiction +#print axioms not_empty + +/-- The frame exposes caller storage as a local without owning its allocation. -/ +def frame : Frame := { activation := 1, locals := [.bytes .word32 home], owned := [] } + +/-- A local store returns the caller allocation with its written byte. -/ +def body : CIL.Method := { returnsValue := false, locals := [], code := [.stloc 0, .ret] } + +/-- The boundary state supplies the scalar to store. -/ +def before : SegmentState := + { frame := frame, stack := [.scalar (.i32 1)], memory := initial } + +/-- The actual interpreter returns the modified memory. -/ +theorem returned : + run [body] 2 0 0 [] before.frame before.stack before.memory = .ok (finalMemory, []) := by + rfl +#print axioms returned + +/-- The returning store permits exactly its four-byte home and its actual return. -/ +def contract : CIL.Safety.SegmentContract where + pre := before.exact [] + footprint _ state := state.localFootprint body body.code + post _ _ exit := exit = .returns finalMemory [] + +/-- The stored byte is retained in the contract's returned memory. -/ +theorem holds : contract.Holds [body] 0 0 := by + intro arguments state permitted + rcases permitted.1 with rfl + rcases permitted.2 with rfl + exact Exists.intro (.returns finalMemory []) + (And.intro (Exists.intro 2 returned) (And.intro confined rfl)) + +#print axioms holds + +/-- A returning store cannot satisfy a contract that omits the written caller home. -/ +theorem empty_footprint_refused : + Not (({ contract with footprint := fun _ _ => [] } : + CIL.Safety.SegmentContract).Holds [body] 0 0) := by + intro holds + cases holds [] before (And.intro rfl rfl) + rename_i exit witness + have framed := witness.2.1 + have post := witness.2.2 + change exit = .returns finalMemory [] at post + subst exit + exact not_empty framed + +#print axioms empty_footprint_refused + +/-- Sequence preserves a returning head's actual footprint when no successor is reachable. -/ +theorem composed_return : contract.Holds [body] 0 0 := by + apply holds.seq (next := fun _ => CIL.Safety.SegmentContract.unreachable) + next => exact fun pc => CIL.Safety.SegmentContract.unreachable_holds [body] 0 pc + next => exact fun _ _ permitted => permitted + next => intro arguments state pc after _ post; cases post + next => exact fun _ _ _ _ _ post framed => And.intro framed post + next => intro arguments state pc after exit _ post; cases post + +#print axioms composed_return + +end StoredReturn + +/-- A two-instruction backedge with an empty stack. -/ +private def repeatBody : CIL.Method := + { returnsValue := false, locals := [], code := [.nop, .br 0] } + +/-- Running out of fuel at either instruction witnesses divergence at every budget. -/ +theorem repeat_diverges (frame : Frame) (memory : Memory) : + SegmentExit.Exhibited [repeatBody] 0 0 [] ⟨frame, [], memory⟩ .diverges := by + apply SegmentExit.diverges_of_recurrent + (invariant := fun pc state => pc = 0 ∧ state = ⟨frame, [], memory⟩) _ ⟨rfl, rfl⟩ + rintro _ _ ⟨rfl, rfl⟩ + refine ⟨0, ⟨frame, [], memory⟩, 2, by decide, SegmentRun.reaches ?_, rfl, rfl⟩ + intro fuel + rfl + +#print axioms repeat_diverges + +end Tests.SegmentContract diff --git a/Tests/SelfAdmission.cs b/Tests/SelfAdmission.cs index ecc232874..31942b545 100644 --- a/Tests/SelfAdmission.cs +++ b/Tests/SelfAdmission.cs @@ -2,26 +2,33 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include StableNames.cs using System.Diagnostics; using System.Text.Json; -using System.Text.RegularExpressions; using Mono.Cecil; // Admission of LeanCIL's own assembly under the scalar profile against its baseline of admitted methods. // Fails on any difference, so a lost admission, an unrecorded gain or a removed method all need review. -const string Usage = "Usage: SelfAdmission.cs [--update] [--baseline baseline.txt] [--reuse-report]"; +const string Usage = "Usage: SelfAdmission.cs [--update] [--baseline baseline.txt] [--reuse-report] [--lean-check]"; const string Profile = "scalar"; const int ListedBlockers = 10; const int ListedMethods = 20; var baselinePath = At("Tests/SelfAdmissionBaseline.txt"); -bool update = false, reuse = false; +bool update = false, reuse = false, leanCheck = false; for (var index = 0; index < args.Length; index++) switch (args[index]) { case "--update": update = true; break; // The gate's own tests compare an existing report with altered baselines. case "--reuse-report": reuse = true; break; + // Run by hand: elaborates the extraction of every admitted method, hours of CPU time in all. + case "--lean-check": leanCheck = true; break; case "--baseline" when index + 1 < args.Length: baselinePath = Path.GetFullPath(args[++index]); break; default: throw new ArgumentException(Usage); } @@ -54,6 +61,8 @@ }; if (current.Count != signatures.Length) throw new InvalidDataException($"Report has {current.Count} entries for {signatures.Length} methods."); +if (leanCheck) CheckAdmittedInLean(current.Where(pair => pair.Value == 'A').Select(pair => pair.Key).ToArray()); + // The baseline lists admitted methods only; whether a non-admitted method is new does not matter. A // generated method counts toward its source method, so renumbering by unrelated edits changes nothing. var admitted = current.Where(pair => pair.Value == 'A').Select(pair => StableNames.Of(pair.Key)) @@ -119,45 +128,39 @@ void List(string heading, IEnumerable methods) "`dotnet run --file Tests/SelfAdmission.cs -- --update`."); Console.WriteLine($"PASS self-admission: {Count('A')} of {current.Count} methods admitted, matching the baseline"); -/// -/// The baseline's names: a method written in source, or the source method a generated one belongs to. -/// -static partial class StableNames +// Extracts each admitted method alone and elaborates it against the checkout's CIL; an admission the Lean +// checks refuse is a drift between the extractor and the checker, so any refusal fails. +void CheckAdmittedInLean(string[] methods) { - /// - /// itself for a method written in source. A compiler-generated method - /// (closure, lambda, local function, iterator or async state machine) is named by the source method - /// it was generated for, since the compiler numbers it by positions that shift with unrelated edits. - /// Generated top-level types share one name, as their numbers count across the assembly. - /// - internal static string Of(string signature) + using var verification = new Program(); + Run(["lake", "build", "CIL"], verification.CheckoutPackage()); + var leanPath = verification.CheckoutLeanPath(); + var root = Path.Combine(directory, "lean"); + if (Directory.Exists(root)) Directory.Delete(root, recursive: true); + var refusals = new System.Collections.Concurrent.ConcurrentBag<(string Method, string Reason)>(); + var finished = 0; + var clock = Stopwatch.StartNew(); + // Each elaboration peaks near 1 GB, so a quarter of the cores bounds memory as well as time. + var options = new ParallelOptions { MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount / 4) }; + Parallel.ForEach(methods.Index(), options, entry => { - if (signature.EndsWith(Generated, StringComparison.Ordinal)) return signature; - var member = signature[(signature.IndexOf(' ', StringComparison.Ordinal) + 1)..]; - var separator = member.IndexOf("::", StringComparison.Ordinal); - var declaring = member[..separator]; - if (declaring.StartsWith('<')) return GeneratedTypes; - var nested = declaring.IndexOf("/<", StringComparison.Ordinal); - var method = member[(separator + 2)..]; - if (nested < 0 && !method.StartsWith('<')) return signature; - var outer = nested < 0 ? declaring : declaring[..nested]; - var source = SourceName().Match((nested < 0 ? "" : declaring[nested..]) + "::" + method).Groups["name"].Value; - return source.Length == 0 ? $"{outer} closure classes{Generated}" : $"{outer}::{source}{Generated}"; - } - - /// The suffix of every name that stands for compiler-generated methods. - const string Generated = " (compiler-generated)"; - - /// The one name of every method of a compiler-generated top-level type. - const string GeneratedTypes = "top-level types" + Generated; - - /// How many times each name occurs. - internal static Dictionary Counts(IEnumerable names) => - names.GroupBy(name => name, StringComparer.Ordinal) - .ToDictionary(group => group.Key, group => group.Count(), StringComparer.Ordinal); - - // The first nonempty bracketed name, which the compiler sets to the method it generated code for; - // a closure class's own constructor has none and counts toward its outer type's closure classes. - [GeneratedRegex(@"<(?[^<>]+)>")] - private static partial Regex SourceName(); -} \ No newline at end of file + var output = Path.Combine(root, entry.Index.ToString(System.Globalization.CultureInfo.InvariantCulture)); + var result = Execute(["dotnet", ExtractorPath, "extract", ExtractorPath, entry.Item, output, Profile]); + if (result.Code == 0) + result = Execute([verification.LeanExecutable, "--root=" + output, Path.Combine(output, "Extracted.lean")], + leanPath: leanPath); + if (result.Code == 0 && !result.Output.Contains("sorryAx", StringComparison.Ordinal)) + Directory.Delete(output, recursive: true); + else + refusals.Add((entry.Item, result.Output.Split('\n').FirstOrDefault(line => line.Contains(": error: ", + StringComparison.Ordinal))?.Trim() ?? result.Output.Split('\n')[0].Trim())); + if (Interlocked.Increment(ref finished) % 100 == 0) + Console.WriteLine($"Lean check: {finished} of {methods.Length} in {clock.Elapsed.TotalMinutes:F0} min"); + }); + var refused = refusals.OrderBy(refusal => refusal.Method, StringComparer.Ordinal).ToArray(); + foreach (var (method, reason) in refused) Console.WriteLine($"REFUSED {method}\n {reason}"); + if (refused.Length != 0) + throw new InvalidOperationException($"Lean refuses {refused.Length} of {methods.Length} admitted methods."); + Console.WriteLine($"PASS Lean check: {methods.Length} admitted methods elaborate, " + + $"{clock.Elapsed.TotalMinutes:F0} min"); +} diff --git a/Tests/SelfAdmissionBaseline.txt b/Tests/SelfAdmissionBaseline.txt index e7ee10cf2..fe0526018 100644 --- a/Tests/SelfAdmissionBaseline.txt +++ b/Tests/SelfAdmissionBaseline.txt @@ -1,4 +1,20 @@ +BindingImage closure classes (compiler-generated) +BindingImage::Compress (compiler-generated) +DeepCheckInputs Program/DeepCheckRun::get_Inputs() +DeepCheckInputs closure classes (compiler-generated) +DeepCheckInputs closure classes (compiler-generated) +DeepCheckInputs/Image DeepCheckInputs/Pin::get_Image() FailureReports closure classes (compiler-generated) +FailureReports/BindingFailure FailureReports/Report::get_binding() +FailureReports/BindingFailure::Clone (compiler-generated) +FailureReports/BindingFailureDigest FailureReports/BindingFailure::get_digests() +FailureReports/BindingFailureDigest::Clone (compiler-generated) +FailureReports/BindingFailureFact::Clone (compiler-generated) +FailureReports/BindingFailureFact[] FailureReports/BindingFailure::get_facts() +FailureReports/BindingFailureImage::Clone (compiler-generated) +FailureReports/BindingFailureImage[] FailureReports/BindingFailure::get_images() +FailureReports/BindingFailureStatus FailureReports/BindingFailureFact::get_status() +FailureReports/BindingFailureStatus FailureReports/BindingFailureImage::get_status() FailureReports/Diagnostic[] FailureReports/Report::get_diagnostics() FailureReports/Method FailureReports/Report::get_method() FailureReports/Method::Clone (compiler-generated) @@ -12,12 +28,22 @@ LeanOutput closure classes (compiler-generated) LeanOutput closure classes (compiler-generated) LeanOutput/InternalError::Clone (compiler-generated) Nethermind.LeanCIL.Arrays closure classes (compiler-generated) +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.ClassViews/Field::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.ClassViews/FieldOperation::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.LocalKind/Array::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.LocalKind/EnumSpan::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.ProofOutline/Parameter::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.ProofOutline/ViewField::get_Element() Nethermind.LeanCIL.Arrays/Element::Clone (compiler-generated) +Nethermind.LeanCIL.Arrays/NumericAccess Nethermind.LeanCIL.ProofOutline/ArrayAccess::get_Load() +Nethermind.LeanCIL.Arrays/NumericAccess Nethermind.LeanCIL.ProofOutline/WordRead::get_Load() Nethermind.LeanCIL.Arrays::StackKind (compiler-generated) +Nethermind.LeanCIL.AssemblyName Nethermind.LeanCIL.TypeIdentity::get_Assembly() Nethermind.LeanCIL.Blockers closure classes (compiler-generated) Nethermind.LeanCIL.Blockers closure classes (compiler-generated) Nethermind.LeanCIL.Blockers closure classes (compiler-generated) Nethermind.LeanCIL.Blockers closure classes (compiler-generated) +Nethermind.LeanCIL.Blockers::Run (compiler-generated) Nethermind.LeanCIL.CallReturnType closure classes (compiler-generated) Nethermind.LeanCIL.ClassViews closure classes (compiler-generated) Nethermind.LeanCIL.ClassViews closure classes (compiler-generated) @@ -32,6 +58,8 @@ Nethermind.LeanCIL.ClassViews/View::Clone (compiler-generated) Nethermind.LeanCIL.ClassViews/View[] Nethermind.LeanCIL.ProofOutline/Extraction::get_Views() Nethermind.LeanCIL.ClassViews::Views (compiler-generated) Nethermind.LeanCIL.CliDiagnostics closure classes (compiler-generated) +Nethermind.LeanCIL.ComputedLayout::Clone (compiler-generated) +Nethermind.LeanCIL.ComputedLayout[] Nethermind.LeanCIL.ProofOutline/Extraction::get_Layouts() Nethermind.LeanCIL.ConstantReturns closure classes (compiler-generated) Nethermind.LeanCIL.ConstantReturns closure classes (compiler-generated) Nethermind.LeanCIL.ConstantReturns/Closed::Clone (compiler-generated) @@ -91,20 +119,37 @@ Nethermind.LeanCIL.ContractType[] Nethermind.LeanCIL.ContractMethodSignature::ge Nethermind.LeanCIL.ContractType[] Nethermind.LeanCIL.ContractMethodSignature::get_Parameters() Nethermind.LeanCIL.EnumTypes closure classes (compiler-generated) Nethermind.LeanCIL.ExceptionLint closure classes (compiler-generated) +Nethermind.LeanCIL.ExceptionLint closure classes (compiler-generated) Nethermind.LeanCIL.ExceptionLint/Declaration::Clone (compiler-generated) Nethermind.LeanCIL.ExceptionLint/Site::Clone (compiler-generated) Nethermind.LeanCIL.ExceptionLint::RuntimeExceptions (compiler-generated) Nethermind.LeanCIL.ExceptionLint::RuntimeExceptions (compiler-generated) Nethermind.LeanCIL.ExceptionLint::RuntimeExceptions (compiler-generated) +Nethermind.LeanCIL.ExceptionLint::RuntimeExceptions (compiler-generated) +Nethermind.LeanCIL.ExceptionType Nethermind.LeanCIL.ExceptionLint/Site::get_Exception() Nethermind.LeanCIL.FeatureEvidence closure classes (compiler-generated) Nethermind.LeanCIL.FeatureProfile Nethermind.LeanCIL.TemplateEmitter/Variant::get_Profile() +Nethermind.LeanCIL.Feature[] Nethermind.LeanCIL.TemplateEmitter/Method::get_Features() Nethermind.LeanCIL.Feature[] Nethermind.LeanCIL.TemplateEmitter/Template::get_Features() +Nethermind.LeanCIL.FieldRef Nethermind.LeanCIL.StaticSite::get_Field() +Nethermind.LeanCIL.FieldRef::Clone (compiler-generated) +Nethermind.LeanCIL.FrameMetadata Nethermind.LeanCIL.ProofOutline/Method::get_Frame() +Nethermind.LeanCIL.FrameMetadata Nethermind.LeanCIL.TemplateEmitter/Method::get_Frame() +Nethermind.LeanCIL.FrameMetadata Nethermind.LeanCIL.TemplateEmitter/Row::get_Frame() +Nethermind.LeanCIL.FrameMetadata Nethermind.LeanCIL.TemplateEmitter/Template::get_Frame() +Nethermind.LeanCIL.FrameMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.FrameMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.FrameMetadata::Clone (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::Clone (compiler-generated) +Nethermind.LeanCIL.GenericEvidence/RefusalKind Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::get_Kind() Nethermind.LeanCIL.GenericStructs closure classes (compiler-generated) Nethermind.LeanCIL.GenericStructs closure classes (compiler-generated) Nethermind.LeanCIL.ImportOptions closure classes (compiler-generated) @@ -142,18 +187,59 @@ Nethermind.LeanCIL.InitializationAssumption::Clone (compiler-generated) Nethermind.LeanCIL.InitializationAssumption[] Nethermind.LeanCIL.ImportResolution/Binding::get_InitializationAssumptions() Nethermind.LeanCIL.InitializationAssumption[] Nethermind.LeanCIL.ProfileInitializationAssumptions::get_Types() Nethermind.LeanCIL.InitializationAssumption[] Program/PreparedContractExport::get_InitializationAssumptions() -Nethermind.LeanCIL.InstructionTranslation closure classes (compiler-generated) Nethermind.LeanCIL.InterfaceModule::Clone (compiler-generated) Nethermind.LeanCIL.InterfaceModule[] Program/PreparedContractExport::get_ImportedInterfaces() Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.LayoutField::Clone (compiler-generated) +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Append::get_Left() +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Append::get_Right() +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Group::get_Body() +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Item::get_Document() +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Nest::get_Body() +Nethermind.LeanCIL.LeanDoc closure classes (compiler-generated) +Nethermind.LeanCIL.LeanDoc/Flatten Nethermind.LeanCIL.LeanDoc/WorkGroup::get_Flatten() +Nethermind.LeanCIL.LeanDoc/Item::Clone (compiler-generated) +Nethermind.LeanCIL.LeanDoc/WorkGroup::Clone (compiler-generated) Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.LocalKind Nethermind.LeanCIL.ParameterHome::get_Kind() +Nethermind.LeanCIL.LocalKind Nethermind.LeanCIL.ProofOutline/LoopOutline::SpanKind(Nethermind.LeanCIL.ProofOutline/Parameter) +Nethermind.LeanCIL.LocalKind Nethermind.LeanCIL.ProofOutline/Slot::get_Kind() +Nethermind.LeanCIL.LocalKind/Aggregate::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Array::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/EnumSpan::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/MethodPointer::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/MethodTable::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Narrow::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/NativeMethodTable::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Object::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Reference::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Span::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Vector::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Word32::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Word64::Clone (compiler-generated) Nethermind.LeanCIL.ManagedPointers closure classes (compiler-generated) +Nethermind.LeanCIL.ManagedTarget::Clone (compiler-generated) +Nethermind.LeanCIL.MetadataImage Nethermind.LeanCIL.StructuralSignatures/Primitive::get_Image() +Nethermind.LeanCIL.MetadataImage/NamedRow::Clone (compiler-generated) +Nethermind.LeanCIL.MetadataImage/TypeNameRow::Clone (compiler-generated) +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.AssemblyName::get_Culture() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.AssemblyName::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.FieldRef::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.MethodRef::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.TypeIdentity::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.TypeIdentity::get_Namespace() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.TypeLevel::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.TypeLevel::get_Namespace() +Nethermind.LeanCIL.MetadataSlices/Slice closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataSlices/Slice closure classes (compiler-generated) Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) @@ -175,18 +261,31 @@ Nethermind.LeanCIL.MetadataValidation/AggregateInfo::Clone (compiler-generated) Nethermind.LeanCIL.MetadataValidation/InstanceField::Clone (compiler-generated) Nethermind.LeanCIL.MetadataValidation/LayoutContext Nethermind.LeanCIL.LayoutEvidence::get_Metadata() Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::Clone (compiler-generated) +Nethermind.LeanCIL.MethodBodyBytes/Body::Clone (compiler-generated) +Nethermind.LeanCIL.MethodRef::Clone (compiler-generated) Nethermind.LeanCIL.MethodSelector closure classes (compiler-generated) Nethermind.LeanCIL.MethodSpecialization closure classes (compiler-generated) Nethermind.LeanCIL.MethodSpecialization/Origin::Clone (compiler-generated) Nethermind.LeanCIL.ModifierTwins closure classes (compiler-generated) +Nethermind.LeanCIL.Narrowing Nethermind.LeanCIL.LocalKind/Narrow::get_Narrowing() Nethermind.LeanCIL.NativeDispatch closure classes (compiler-generated) Nethermind.LeanCIL.NativeOperands/Certificate::Clone (compiler-generated) Nethermind.LeanCIL.NativeOperands/Kind Nethermind.LeanCIL.Arrays/Element::get_Kind() +Nethermind.LeanCIL.ObjectOperandProof closure classes (compiler-generated) +Nethermind.LeanCIL.Operation[] Nethermind.LeanCIL.ProofOutline/Body::get_Operations() +Nethermind.LeanCIL.Operation[] Nethermind.LeanCIL.ProofOutline/Method::get_Operations() Nethermind.LeanCIL.ParameterEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.ParameterHome::Clone (compiler-generated) +Nethermind.LeanCIL.PinnedAssemblies Nethermind.LeanCIL.MetadataSlices/Collection::get_Pinned() Nethermind.LeanCIL.PinnedAssemblies closure classes (compiler-generated) -Nethermind.LeanCIL.PinnedAssemblies closure classes (compiler-generated) -Nethermind.LeanCIL.PinnedAssemblies closure classes (compiler-generated) +Nethermind.LeanCIL.PinnedAssemblies/Pin Nethermind.LeanCIL.PinnedAssemblies::get_SelectedPin() Nethermind.LeanCIL.PinnedAssemblies/Source::Clone (compiler-generated) +Nethermind.LeanCIL.PlatformAssemblies closure classes (compiler-generated) +Nethermind.LeanCIL.PlatformDefinition closure classes (compiler-generated) +Nethermind.LeanCIL.PlatformDefinitions closure classes (compiler-generated) +Nethermind.LeanCIL.PlatformKind Nethermind.LeanCIL.PlatformDefinition::get_Kind() +Nethermind.LeanCIL.PrimitiveType Nethermind.LeanCIL.TypeKind/EnumType::get_Underlying() +Nethermind.LeanCIL.PrimitiveType closure classes (compiler-generated) Nethermind.LeanCIL.ProfileInitializationAssumptions::Clone (compiler-generated) Nethermind.LeanCIL.ProfileInitializationAssumptions[] Nethermind.LeanCIL.ContractExport::get_InitializationAssumptions() Nethermind.LeanCIL.Program closure classes (compiler-generated) @@ -217,6 +316,7 @@ Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/Access Nethermind.LeanCIL.ProofOutline/CallStep::get_Writes() Nethermind.LeanCIL.ProofOutline/Access::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/AccessKind Nethermind.LeanCIL.ProofOutline/Access::get_Kind() @@ -225,7 +325,7 @@ Nethermind.LeanCIL.ProofOutline/AddedReference::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/Allocation Nethermind.LeanCIL.ProofOutline/ObjectWalk::get_Fresh() Nethermind.LeanCIL.ProofOutline/Allocation::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/ArrayAccess::Clone (compiler-generated) -Nethermind.LeanCIL.ProofOutline/ArrayLength::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/ArrayUse::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/Body closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/Body::BlockStart (compiler-generated) @@ -245,7 +345,7 @@ Nethermind.LeanCIL.ProofOutline/HeldArray Nethermind.LeanCIL.ProofOutline/ArrayU Nethermind.LeanCIL.ProofOutline/HeldArray Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values::get_Array() Nethermind.LeanCIL.ProofOutline/HeldArray::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/LocalAccess::Clone (compiler-generated) -Nethermind.LeanCIL.ProofOutline/LocalAddress::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LocalAddressValue::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/LocalValue::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) @@ -265,8 +365,12 @@ Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline::Branch (compiler-generated) Nethermind.LeanCIL.ProofOutline/LoopOutline::Step (compiler-generated) -Nethermind.LeanCIL.ProofOutline/Number::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline::Step (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Method Nethermind.LeanCIL.ProofOutline/Body::get_Method() +Nethermind.LeanCIL.ProofOutline/Method[] Nethermind.LeanCIL.ProofOutline/Extraction::get_Emitted() Nethermind.LeanCIL.ProofOutline/ObjectFrame::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/ObjectFrameEntry::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) @@ -282,6 +386,7 @@ Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/ObjectPlan Nethermind.LeanCIL.ProofOutline/Callee::get_Plan() Nethermind.LeanCIL.ProofOutline/ObjectPlan Nethermind.LeanCIL.ProofOutline/Specification::get_Plan() Nethermind.LeanCIL.ProofOutline/ObjectTerm::Clone (compiler-generated) @@ -323,7 +428,7 @@ Nethermind.LeanCIL.ProofOutline/ObjectWalk::Walk (compiler-generated) Nethermind.LeanCIL.ProofOutline/Outline::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/ArrayAccess::get_Array() Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/ArrayAccess::get_Index() -Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/ArrayLength::get_Array() +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::get_Array() Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/Component::get_Span() Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/HeldArray::get_Words() Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/SpanArgument::get_Span() @@ -349,6 +454,7 @@ Nethermind.LeanCIL.ProofOutline/ReceiverLoop closure classes (compiler-generated Nethermind.LeanCIL.ProofOutline/ReceiverLoop closure classes (compiler-generated) Nethermind.LeanCIL.ProofOutline/ReceiverLoop::Generate (compiler-generated) Nethermind.LeanCIL.ProofOutline/ReceiverLoop::Generate (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ReceiverLoop::Walk (compiler-generated) Nethermind.LeanCIL.ProofOutline/ReferenceArgument::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/Result Nethermind.LeanCIL.ProofOutline/Specification::get_Result() Nethermind.LeanCIL.ProofOutline/Result::Clone (compiler-generated) @@ -359,11 +465,10 @@ Nethermind.LeanCIL.ProofOutline/ScalarSplit::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/ScalarStep Nethermind.LeanCIL.ProofOutline/ScalarCheck::get_Next() Nethermind.LeanCIL.ProofOutline/ScalarStep Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_Fails() Nethermind.LeanCIL.ProofOutline/ScalarStep Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_Holds() -Nethermind.LeanCIL.ProofOutline/ScalarWord::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/Sliced::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/Slot::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/SpanArgument::Clone (compiler-generated) -Nethermind.LeanCIL.ProofOutline/SpanLength::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/SpanLengthValue::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/SpanTerm::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/SpannedArray::Clone (compiler-generated) Nethermind.LeanCIL.ProofOutline/Specification Nethermind.LeanCIL.ProofOutline/Callee::get_Spec() @@ -386,6 +491,7 @@ Nethermind.LeanCIL.ProofOutline::ScalarPaths (compiler-generated) Nethermind.LeanCIL.ProofOutline::WalkStruct (compiler-generated) Nethermind.LeanCIL.ProofOutline::WalkStruct (compiler-generated) Nethermind.LeanCIL.Reachability closure classes (compiler-generated) +Nethermind.LeanCIL.ResolvedCallSites closure classes (compiler-generated) Nethermind.LeanCIL.RuntimeAsset::Clone (compiler-generated) Nethermind.LeanCIL.RuntimeAssetSelection closure classes (compiler-generated) Nethermind.LeanCIL.RuntimeAssetSelection closure classes (compiler-generated) @@ -394,15 +500,18 @@ Nethermind.LeanCIL.RuntimeAssetSelection closure classes (compiler-generated) Nethermind.LeanCIL.RuntimeModels closure classes (compiler-generated) Nethermind.LeanCIL.RuntimeModels closure classes (compiler-generated) Nethermind.LeanCIL.ScalarEvidence closure classes (compiler-generated) -Nethermind.LeanCIL.Spans/Analysis::Clone (compiler-generated) Nethermind.LeanCIL.StaticData closure classes (compiler-generated) Nethermind.LeanCIL.StaticData closure classes (compiler-generated) +Nethermind.LeanCIL.StaticDataBlock::Clone (compiler-generated) +Nethermind.LeanCIL.StaticIdentityRecord Nethermind.LeanCIL.StaticSite::get_Identity() Nethermind.LeanCIL.StaticIdentityRecord::Clone (compiler-generated) Nethermind.LeanCIL.StaticIdentityRecord[] Nethermind.LeanCIL.ContractCertification::get_StaticIdentities() Nethermind.LeanCIL.StaticIdentityRecord[] Nethermind.LeanCIL.ImportResolution/Binding::get_StaticIdentities() Nethermind.LeanCIL.StaticIdentityRecord[] Program/FullLibrary::get_Statics() Nethermind.LeanCIL.StaticIdentityRecord[] Program/FullSlot::get_Statics() Nethermind.LeanCIL.StaticIdentityRecord[] Program/PreparedContractExport::get_StaticIdentities() +Nethermind.LeanCIL.StaticSite::Clone (compiler-generated) +Nethermind.LeanCIL.StaticSiteDetail Nethermind.LeanCIL.StaticSite::get_Detail() Nethermind.LeanCIL.StaticValues closure classes (compiler-generated) Nethermind.LeanCIL.StaticValues closure classes (compiler-generated) Nethermind.LeanCIL.StaticValues/Context closure classes (compiler-generated) @@ -415,10 +524,16 @@ Nethermind.LeanCIL.StaticValues::Bytes (compiler-generated) Nethermind.LeanCIL.StaticValues::Closure (compiler-generated) Nethermind.LeanCIL.StaticValues::Closure (compiler-generated) Nethermind.LeanCIL.StaticValues::Computed (compiler-generated) +Nethermind.LeanCIL.StructuralSignatures Nethermind.LeanCIL.PinnedAssemblies::get_Signatures() +Nethermind.LeanCIL.StructuralSignatures closure classes (compiler-generated) +Nethermind.LeanCIL.StructuralSignatures closure classes (compiler-generated) +Nethermind.LeanCIL.StructuralSignatures/Primitive::Clone (compiler-generated) +Nethermind.LeanCIL.StructuralSignatures/Primitive[] Nethermind.LeanCIL.PinnedAssemblies/Registration::get_Primitives() Nethermind.LeanCIL.Symbols closure classes (compiler-generated) Nethermind.LeanCIL.Symbols closure classes (compiler-generated) Nethermind.LeanCIL.Symbols closure classes (compiler-generated) Nethermind.LeanCIL.Symbols closure classes (compiler-generated) +Nethermind.LeanCIL.Symbols/PdbFile::Clone (compiler-generated) Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) @@ -428,11 +543,7 @@ Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) -Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) -Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) -Nethermind.LeanCIL.TemplateEmitter/Method Nethermind.LeanCIL.TemplateEmitter/Row::get_Method() Nethermind.LeanCIL.TemplateEmitter/Method::Clone (compiler-generated) -Nethermind.LeanCIL.TemplateEmitter/Method[] Nethermind.LeanCIL.ProofOutline/Extraction::get_Emitted() Nethermind.LeanCIL.TemplateEmitter/Method[] Nethermind.LeanCIL.TemplateEmitter/Variant::get_Methods() Nethermind.LeanCIL.TemplateEmitter/Options::Clone (compiler-generated) Nethermind.LeanCIL.TemplateEmitter/Row::Clone (compiler-generated) @@ -440,13 +551,16 @@ Nethermind.LeanCIL.TemplateEmitter/Row[] Nethermind.LeanCIL.TemplateEmitter/Temp Nethermind.LeanCIL.TemplateEmitter/Template::Clone (compiler-generated) Nethermind.LeanCIL.TemplateEmitter/Variant Nethermind.LeanCIL.TemplateEmitter/Row::get_Variant() Nethermind.LeanCIL.TemplateEmitter/Variant::Clone (compiler-generated) -Nethermind.LeanCIL.TemplateEmitter::Instruction (compiler-generated) +Nethermind.LeanCIL.Throws closure classes (compiler-generated) Nethermind.LeanCIL.Throws closure classes (compiler-generated) Nethermind.LeanCIL.Throws/Analysis::Clone (compiler-generated) +Nethermind.LeanCIL.TypeLevel Nethermind.LeanCIL.MetadataImage/TypeNameRow::get_Level() +Nethermind.LeanCIL.TypeLevel::Clone (compiler-generated) Nethermind.LeanCIL.TypeTests/Analysis::Clone (compiler-generated) Nethermind.LeanCIL.TypeTests/Node Nethermind.LeanCIL.TypeTests/Node::get_Left() Nethermind.LeanCIL.TypeTests/Node Nethermind.LeanCIL.TypeTests/Node::get_Right() Nethermind.LeanCIL.TypeTests/Node::Clone (compiler-generated) +Nethermind.LeanCIL.UserString::Clone (compiler-generated) Nethermind.LeanCIL.ValueBitCasts closure classes (compiler-generated) Nethermind.LeanCIL.ValueTuples closure classes (compiler-generated) Nethermind.LeanCIL.VirtualDispatch closure classes (compiler-generated) @@ -645,10 +759,25 @@ Program closure classes (compiler-generated) Program closure classes (compiler-generated) Program closure classes (compiler-generated) Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) Program/AuditConstantRequest::Clone (compiler-generated) Program/AuditNamespaces::Clone (compiler-generated) Program/AuditRefutation Program/AuditedTheorem::get_refutes() -Program/AuditRefutation::Clone (compiler-generated) +Program/AuditRequest Program/BindingRequest::get_Audit() Program/AuditRequest::Clone (compiler-generated) Program/AuditRequest[] Program/ConsumerExportAudit::get_Theorems() Program/AuditedClosure::Clone (compiler-generated) @@ -662,11 +791,32 @@ Program/AuditedTheorem[] Program/AuditedNamespaces::get_rejected() Program/AuditedTheorem[] Program/AuditedOleans::get_constants() Program/AuditedTheorem[] Program/AuditedOleans::get_theorems() Program/BatchManifest::Clone (compiler-generated) -Program/BatchRequest::Clone (compiler-generated) Program/BatchRequest[] Program/BatchManifest::get_requests() Program/BatchResult[] Program/BatchRun::get_Results() Program/BatchRun::Clone (compiler-generated) Program/BindingCatalog::Clone (compiler-generated) +Program/BindingData Program/BindingPublication::get_Closure() +Program/BindingData Program/BindingPublication::get_Sidecar() +Program/BindingFact Program/BindingFactResult::get_id() +Program/BindingFact Program/BindingSubject/Fact::get_Value() +Program/BindingFactResult::Clone (compiler-generated) +Program/BindingFactResult[] Program/BindingSection::get_facts() +Program/BindingImagePin Program/BindingPublication::get_Producer() +Program/BindingImagePin[] Program/BindingPlan::get_Images() +Program/BindingLibraryPreparation::Clone (compiler-generated) +Program/BindingMode Program/BindingSection::get_mode() +Program/BindingPublication::Clone (compiler-generated) +Program/BindingPublication[] Program/BindingPlan::get_Imports() +Program/BindingRequest::Clone (compiler-generated) +Program/BindingRequest[] Program/BindingTargets::get_Requests() +Program/BindingRequest[] Program/DeepSessionContribution::get_Requests() +Program/BindingSection Program/Report::get_binding() +Program/BindingSection::Clone (compiler-generated) +Program/BindingStatus Program/BindingFactResult::get_status() +Program/BindingStatus Program/ImageResult::get_status() +Program/BindingSubject Program/BindingRequest::get_Subject() +Program/BindingSubject/Fact::Clone (compiler-generated) +Program/BindingTargets Program/DeepSessionContribution::get_Targets() Program/BuildDiagnostic::Clone (compiler-generated) Program/CatalogAssign::Clone (compiler-generated) Program/CatalogAssign[] Program/CatalogFamily::get_assign() @@ -690,15 +840,24 @@ Program/ConsumerSession::Clone (compiler-generated) Program/ConsumerSupportManifest Program/TrustedSupport::get_Manifest() Program/ConsumerSupportManifest::Clone (compiler-generated) Program/ConsumerSupportModule::Clone (compiler-generated) +Program/DeepCheckAttempt Program/DeepCheckRun::get_Attempt() +Program/DeepCheckAttempt closure classes (compiler-generated) +Program/DeepCheckRun Program/DeepCheckRun::Claim(DeepCheckInputs,Program/DeepCheckRun) +Program/DeepSessionContribution closure classes (compiler-generated) +Program/DeepSessionContribution::Clone (compiler-generated) Program/DeploymentDependency::Clone (compiler-generated) Program/DeploymentDependency[] Program/FullImportResult::get_Dependencies() Program/DeploymentDependency[] Program/Report::get_contractDependencies() +Program/DigestFile Program/BindingSection::get_digests() +Program/DigestFile::Clone (compiler-generated) Program/EditorProof::Clone (compiler-generated) Program/EditorProof[] Program/EditorSelection::get_Proofs() Program/EditorSelection::Clone (compiler-generated) Program/EnvironmentEntry::Clone (compiler-generated) Program/ExportClosure Program/CheckedContractExport::get_Closure() Program/ExportClosure::Clone (compiler-generated) +Program/ExtractedMethod Program/VerifiedRefutation::get_method() +Program/ExtractedMethod::Clone (compiler-generated) Program/ExtractionComponents::Clone (compiler-generated) Program/ExtractionIdentity closure classes (compiler-generated) Program/FingerprintInput::Clone (compiler-generated) @@ -736,6 +895,9 @@ Program/GateTemplate::Clone (compiler-generated) Program/GateTemplateUse::Clone (compiler-generated) Program/GateVariant[] Program/ProofGateEntry::get_variants() Program/GeneratedExtraction::Clone (compiler-generated) +Program/ImageResult::Clone (compiler-generated) +Program/ImageResult[] Program/BindingSection::get_images() +Program/ImageRole Program/ImageResult::get_role() Program/LibraryAudit::Clone (compiler-generated) Program/LoadedGates::Clone (compiler-generated) Program/PreparedContractExport Program/CheckedContractExport::get_Export() @@ -745,21 +907,31 @@ Program/ProofGateEntry[] Program/GateManifest::get_gates() Program/ProofMessage::Clone (compiler-generated) Program/PublishedImport[] Program/FullImportResult::get_Imports() Program/PublishedImport[] Program/Report::get_imports() +Program/ReplayRejection::Clone (compiler-generated) +Program/ReplayRejectionKind Program/ReplayRejection::get_Kind() Program/Report[] Program/BatchRun::get_Verified() +Program/StandaloneCachePromotion::Clone (compiler-generated) Program/StatementDefinition::Clone (compiler-generated) Program/StatementDefinition[] Program/Report::get_specification() Program/StatementDefinition[] Program/StatementView::get_definitions() Program/StatementIdentity Program/Report::get_statementIdentity() Program/StatementRequest Program/ConsumerRequest::get_Statement() Program/StatementRequest::Clone (compiler-generated) +Program/StatementSources Program/BindingSection::get_sources() +Program/StatementSources::Clone (compiler-generated) Program/StatementSubject[] Program/StatementView::get_subjects() Program/StatementView::Clone (compiler-generated) Program/TrustedSupport::Clone (compiler-generated) Program/VerificationSettings::Clone (compiler-generated) +Program/VerifiedRefutation Program/Report::get_refutation() +Program/VerifiedRefutation::Clone (compiler-generated) Program/WatchResult::Clone (compiler-generated) Program/WatchSettings::Clone (compiler-generated) Program/WatchState::Clone (compiler-generated) Program/WorkspaceSettings::Clone (compiler-generated) +Program::BindingLines (compiler-generated) +Program::BindingLines (compiler-generated) +Program::BindingLines (compiler-generated) Program::ConsumerGeneratedImports (compiler-generated) Program::ConsumerSources (compiler-generated) Program::ConsumerSources (compiler-generated) @@ -773,6 +945,8 @@ Program::ProjectSources (compiler-generated) Program::ReachableGraph (compiler-generated) Program::ReachableGraph (compiler-generated) Program::RunBatch (compiler-generated) +Program::RunBatch (compiler-generated) +Program::RunBatch (compiler-generated) Program::RunGateRows (compiler-generated) Program::Specifications (compiler-generated) Program::SubjectLines (compiler-generated) @@ -792,7 +966,13 @@ Program::VerifyContractExports (compiler-generated) Program::VerifyContractExports (compiler-generated) SourceCopies Program/ConsumerSession::get_Sources() SourceCopies closure classes (compiler-generated) +System.Boolean DeepCheckInputs/Pin::get_Platform() +System.Boolean DeepCheckInputs/Pin::get_Reference() +System.Boolean Nethermind.LeanCIL.Arrays/Element::get_Compound() System.Boolean Nethermind.LeanCIL.Arrays/Element::get_Signed() +System.Boolean Nethermind.LeanCIL.Arrays/NumericAccess::get_Signed() +System.Boolean Nethermind.LeanCIL.Arrays/NumericAccess::get_Store() +System.Boolean Nethermind.LeanCIL.AssemblyName/KeyColumns::get_Valid() System.Boolean Nethermind.LeanCIL.ClassViews/FieldOperation::get_Signed() System.Boolean Nethermind.LeanCIL.ClassViews/FieldOperation::get_Store() System.Boolean Nethermind.LeanCIL.CliDiagnostics::IsBidiControl(System.Char) @@ -801,20 +981,35 @@ System.Boolean Nethermind.LeanCIL.ContractExport::get_ReturnsValue() System.Boolean Nethermind.LeanCIL.ContractLayout::get_Auto() System.Boolean Nethermind.LeanCIL.ContractLayout::get_Explicit() System.Boolean Nethermind.LeanCIL.ContractLayout::get_IsValueType() +System.Boolean Nethermind.LeanCIL.FrameMetadata::get_ReturnsValue() System.Boolean Nethermind.LeanCIL.ImportResolution/Binding::get_IsReferenceAssembly() System.Boolean Nethermind.LeanCIL.ImportResolution/Binding::get_ReturnsValue() System.Boolean Nethermind.LeanCIL.ImportResolution/Node::get_IsReferenceAssembly() System.Boolean Nethermind.LeanCIL.ImportResolution/Source::get_Discovered() +System.Boolean Nethermind.LeanCIL.InitializedStaticValue::get_Construct() +System.Boolean Nethermind.LeanCIL.LayoutField::get_IsAggregate() +System.Boolean Nethermind.LeanCIL.LeanDoc/Space::get_FoundLine() +System.Boolean Nethermind.LeanCIL.LocalKind/EnumSpan::get_ByReference() +System.Boolean Nethermind.LeanCIL.LocalKind/EnumSpan::get_Writable() +System.Boolean Nethermind.LeanCIL.LocalKind/Span::get_Writable() System.Boolean Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::get_IsByRefLike() +System.Boolean Nethermind.LeanCIL.MethodRef::get_HasThis() +System.Boolean Nethermind.LeanCIL.NumericKinds::Signed(Nethermind.LeanCIL.NumericKind) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetByReferenceType(System.Boolean) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetModifiedType(System.Boolean,System.Boolean,System.Boolean) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetPinnedType(System.Boolean) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetPointerType(System.Boolean) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetSZArrayType(System.Boolean) System.Boolean Nethermind.LeanCIL.ProofOutline/Access::get_Called() System.Boolean Nethermind.LeanCIL.ProofOutline/Access::get_Opaque() System.Boolean Nethermind.LeanCIL.ProofOutline/Access::get_Signed() System.Boolean Nethermind.LeanCIL.ProofOutline/ArrayUse::get_Length() System.Boolean Nethermind.LeanCIL.ProofOutline/Callee::get_Open() +System.Boolean Nethermind.LeanCIL.ProofOutline/ElementAccess::get_LiteralIndex() System.Boolean Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Reread() System.Boolean Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Store() -System.Boolean Nethermind.LeanCIL.ProofOutline/Number::get_Literal() System.Boolean Nethermind.LeanCIL.ProofOutline/Number::get_Narrow() +System.Boolean Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::get_Numeral() System.Boolean Nethermind.LeanCIL.ProofOutline/ObjectWalk::get_NumericHomes() System.Boolean Nethermind.LeanCIL.ProofOutline/Parameter::get_Listed() System.Boolean Nethermind.LeanCIL.ProofOutline/Parameter::get_Signed() @@ -831,20 +1026,14 @@ System.Boolean Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Caseable() System.Boolean Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Written() System.Boolean Nethermind.LeanCIL.ProofOutline/Specification::get_Classes() System.Boolean Nethermind.LeanCIL.ProofOutline/Specification::get_Symbolic() -System.Boolean Nethermind.LeanCIL.ProofOutline/StructField::get_Stored() +System.Boolean Nethermind.LeanCIL.ProofOutline/StructScalar::get_Stored() System.Boolean Nethermind.LeanCIL.ProofOutline/ViewField::get_Stored() System.Boolean Nethermind.LeanCIL.ProofOutline/WordRead::get_CallerIndex() System.Boolean Nethermind.LeanCIL.RuntimeAsset::get_AppLocal() System.Boolean Nethermind.LeanCIL.StaticValues/Value::get_Construct() System.Boolean Nethermind.LeanCIL.StaticValues/Value::get_Opaque() -System.Boolean Nethermind.LeanCIL.TemplateEmitter/Method::get_HasStaticSites() System.Boolean Nethermind.LeanCIL.TemplateEmitter/Options::get_Check() System.Boolean Nethermind.LeanCIL.TemplateEmitter/Options::get_OmitEquations() -System.Boolean Nethermind.LeanCIL.VectorSignatures::GetByReferenceType(System.Boolean) -System.Boolean Nethermind.LeanCIL.VectorSignatures::GetModifiedType(System.Boolean,System.Boolean,System.Boolean) -System.Boolean Nethermind.LeanCIL.VectorSignatures::GetPinnedType(System.Boolean) -System.Boolean Nethermind.LeanCIL.VectorSignatures::GetPointerType(System.Boolean) -System.Boolean Nethermind.LeanCIL.VectorSignatures::GetSZArrayType(System.Boolean) System.Boolean Program/AuditRequest::get_refutation() System.Boolean Program/AuditRequest::get_statement() System.Boolean Program/AuditRequest::get_term() @@ -853,15 +1042,17 @@ System.Boolean Program/BatchOptions::get_Fresh() System.Boolean Program/BatchOptions::get_Json() System.Boolean Program/BatchOptions::get_RebuildLibrary() System.Boolean Program/BatchOptions::get_Trace() +System.Boolean Program/BindingSubject/Fact::get_Complete() System.Boolean Program/ClosureEntry::get_Built() System.Boolean Program/ConsumerRequest::get_ExpectRejection() +System.Boolean Program/DeepSessionContribution::get_Restored() System.Boolean Program/DeploymentDependency::get_conditional() System.Boolean Program/EnvironmentEntry::get_SetByLake() System.Boolean Program/GateRow::get_Failed() System.Boolean Program/PreparedContractExport::get_Conditional() System.Boolean Program/PreparedContractExport::get_ReturnsValue() System.Boolean Program/PublishedImport::get_certificationAuthenticated() -System.Boolean Program/StatementRequest::get_Negated() +System.Boolean Program/StatementRequest::get_Refuted() System.Boolean Program/StatementRequest::get_Term() System.Boolean Program/StatementSubject::get_extracted() System.Boolean Program/WatchResult::get_Rechecked() @@ -869,26 +1060,65 @@ System.Boolean Program/WatchSettings::get_Goals() System.Boolean Program/WatchSettings::get_Json() System.Boolean Program/WatchSettings::get_Once() System.Boolean Program/WorkspaceSettings::get_CreatedPackage() +System.Byte[] Nethermind.LeanCIL.MethodBodyBytes/Body::get_Bytes() System.Byte[] Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot::get_Bytes() System.Byte[] Nethermind.LeanCIL.StaticValues/Self::get_Data() System.Byte[] Nethermind.LeanCIL.StaticValues/Snapshot::get_Data() System.Byte[] Nethermind.LeanCIL.StaticValues/Value::get_Bytes() +System.Byte[] Nethermind.LeanCIL.Symbols/PdbFile::get_Bytes() +System.Byte[] Program/BindingData::get_Bytes() System.Int32 FailureReports/Report::get_schema() System.Int32 Nethermind.LeanCIL.Arrays/Element::get_Width() +System.Int32 Nethermind.LeanCIL.Arrays/NumericAccess::get_Width() +System.Int32 Nethermind.LeanCIL.AssemblyName/Columns::get_Culture() +System.Int32 Nethermind.LeanCIL.AssemblyName/Columns::get_Name() System.Int32 Nethermind.LeanCIL.ClassViews/Access::get_Method() System.Int32 Nethermind.LeanCIL.ClassViews/Access::get_Pc() System.Int32 Nethermind.LeanCIL.ClassViews/Field::get_Offset() System.Int32 Nethermind.LeanCIL.ClassViews/FieldOperation::get_Offset() +System.Int32 Nethermind.LeanCIL.ComputedLayout::get_Size() System.Int32 Nethermind.LeanCIL.ContractBundleManifest::get_Schema() System.Int32 Nethermind.LeanCIL.ContractExport::get_ParameterCount() System.Int32 Nethermind.LeanCIL.ContractLayout::get_DeclaredSize() System.Int32 Nethermind.LeanCIL.ContractLayout::get_Packing() System.Int32 Nethermind.LeanCIL.ExceptionLint/Site::get_IlOffset() +System.Int32 Nethermind.LeanCIL.FieldRef::get_Index() +System.Int32 Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::get_Pc() System.Int32 Nethermind.LeanCIL.ImportResolution/Binding::get_ParameterCount() System.Int32 Nethermind.LeanCIL.ImportResolution/Binding::get_Slot() +System.Int32 Nethermind.LeanCIL.LayoutField::get_Offset() +System.Int32 Nethermind.LeanCIL.LayoutField::get_Width() +System.Int32 Nethermind.LeanCIL.LeanDoc/Append::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Group::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Item::get_Indent() +System.Int32 Nethermind.LeanCIL.LeanDoc/Line::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Nest::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Nest::get_Indent() +System.Int32 Nethermind.LeanCIL.LeanDoc/Space::get_Width() +System.Int32 Nethermind.LeanCIL.LeanDoc/Text::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Text::get_Width() +System.Int32 Nethermind.LeanCIL.LeanEmitter::InstructionCheckHeartbeats(System.Int32,System.Int32) System.Int32 Nethermind.LeanCIL.LeanEmitter::RecursionDepth(System.Int32) +System.Int32 Nethermind.LeanCIL.LocalKind/Aggregate::get_Size() +System.Int32 Nethermind.LeanCIL.LocalKind/Span::get_Width() +System.Int32 Nethermind.LeanCIL.LocalKind/Vector::get_Bits() +System.Int32 Nethermind.LeanCIL.ManagedTarget::get_Method() +System.Int32 Nethermind.LeanCIL.MetadataImage/NamedRow::get_Name() +System.Int32 Nethermind.LeanCIL.MetadataImage/NamedRow::get_Namespace() +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice/TokenCollector::GetByReferenceType(System.Int32) +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice/TokenCollector::GetPointerType(System.Int32) +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice/TokenCollector::GetSZArrayType(System.Int32) +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice::RowOf(System.UInt32) +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice::get_PositionOf() System.Int32 Nethermind.LeanCIL.MetadataValidation/AggregateInfo::get_Alignment() System.Int32 Nethermind.LeanCIL.MetadataValidation/AggregateInfo::get_Size() +System.Int32 Nethermind.LeanCIL.MethodBodyBytes/Body::get_HeaderLength() +System.Int32 Nethermind.LeanCIL.MethodRef::get_GenericArity() +System.Int32 Nethermind.LeanCIL.NumericKinds::Width(Nethermind.LeanCIL.NumericKind) +System.Int32 Nethermind.LeanCIL.ParameterHome::get_Argument() +System.Int32 Nethermind.LeanCIL.PlatformDefinition::get_Arity() +System.Int32 Nethermind.LeanCIL.PlatformDefinition::get_Underlying() +System.Int32 Nethermind.LeanCIL.PlatformDefinitions::RecursionDepth(System.Int32) System.Int32 Nethermind.LeanCIL.ProofOutline/Access::get_Stores() System.Int32 Nethermind.LeanCIL.ProofOutline/AddedReference::get_Width() System.Int32 Nethermind.LeanCIL.ProofOutline/Allocation::get_Length() @@ -907,7 +1137,7 @@ System.Int32 Nethermind.LeanCIL.ProofOutline/ElementHelper::get_Index() System.Int32 Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Index() System.Int32 Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Stores() System.Int32 Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Width() -System.Int32 Nethermind.LeanCIL.ProofOutline/LocalAddress::get_Slot() +System.Int32 Nethermind.LeanCIL.ProofOutline/LocalAddressValue::get_Slot() System.Int32 Nethermind.LeanCIL.ProofOutline/LocalValue::get_Slot() System.Int32 Nethermind.LeanCIL.ProofOutline/Number::get_Width() System.Int32 Nethermind.LeanCIL.ProofOutline/ObjectFrame::get_Locals() @@ -916,6 +1146,7 @@ System.Int32 Nethermind.LeanCIL.ProofOutline/Outline::get_Obligations() System.Int32 Nethermind.LeanCIL.ProofOutline/Outline::get_Paths() System.Int32 Nethermind.LeanCIL.ProofOutline/Parameter::get_Width() System.Int32 Nethermind.LeanCIL.ProofOutline/ReceiverField::get_Offset() +System.Int32 Nethermind.LeanCIL.ProofOutline/Result::get_Width() System.Int32 Nethermind.LeanCIL.ProofOutline/ScalarCheck::get_Pc() System.Int32 Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_FailsPc() System.Int32 Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_HoldsPc() @@ -924,15 +1155,17 @@ System.Int32 Nethermind.LeanCIL.ProofOutline/ScalarWord::get_Bits() System.Int32 Nethermind.LeanCIL.ProofOutline/Sliced::get_Component() System.Int32 Nethermind.LeanCIL.ProofOutline/Slot::get_Index() System.Int32 Nethermind.LeanCIL.ProofOutline/Slot::get_Layout() -System.Int32 Nethermind.LeanCIL.ProofOutline/SpanLength::get_Component() -System.Int32 Nethermind.LeanCIL.ProofOutline/StructField::get_Offset() -System.Int32 Nethermind.LeanCIL.ProofOutline/StructField::get_Width() +System.Int32 Nethermind.LeanCIL.ProofOutline/SpanLengthValue::get_Component() +System.Int32 Nethermind.LeanCIL.ProofOutline/StructScalar::get_Offset() +System.Int32 Nethermind.LeanCIL.ProofOutline/StructScalar::get_Width() System.Int32 Nethermind.LeanCIL.ProofOutline/Stub::get_Index() System.Int32 Nethermind.LeanCIL.ProofOutline/Stub::get_Result() System.Int32 Nethermind.LeanCIL.ProofOutline/StubCall::get_Number() System.Int32 Nethermind.LeanCIL.ProofOutline/StubCall::get_Stores() System.Int32 Nethermind.LeanCIL.ProofOutline/ViewField::get_Offset() System.Int32 Nethermind.LeanCIL.ProofOutline/WordRead::get_Stores() +System.Int32 Nethermind.LeanCIL.StaticDataBlock::get_Alignment() +System.Int32 Nethermind.LeanCIL.StaticSite::get_Pc() System.Int32 Nethermind.LeanCIL.StaticValues/ConstructorIndex::get_FirstControlTransfer() System.Int32 Nethermind.LeanCIL.StaticValues/Occurrence::get_Order() System.Int32 Nethermind.LeanCIL.StaticValues/Word::get_Width() @@ -940,12 +1173,16 @@ System.Int32 Program/AuditRefutation::get_column() System.Int32 Program/AuditRefutation::get_line() System.Int32 Program/AuditedNamespaces::get_count() System.Int32 Program/BatchManifest::get_schema() +System.Int32 Program/BindingPlan::get_Methods() System.Int32 Program/BuildDiagnostic::get_Column() System.Int32 Program/BuildDiagnostic::get_Line() System.Int32 Program/CatalogFile::get_schema() System.Int32 Program/CertifiedFingerprintManifest::get_Schema() System.Int32 Program/CertifiedReleaseAsset::get_Schema() System.Int32 Program/ConsumerSupportManifest::get_Schema() +System.Int32 Program/DeepClosureWork::get_Hashed() +System.Int32 Program/DeepClosureWork::get_Headers() +System.Int32 Program/DeepClosureWork::get_Launches() System.Int32 Program/FullImport::get_Id() System.Int32 Program/FullLibrary::get_Id() System.Int32 Program/GateManifest::get_schema() @@ -956,6 +1193,8 @@ System.Int32 Program/ProofMessage::get_line() System.Int32 Program/PublishedImport::get_slot() System.Int32 Program/Report::get_schema() System.Int32 Program/VerificationSettings::get_schema() +System.Int32 Program/VerifiedRefutation::get_column() +System.Int32 Program/VerifiedRefutation::get_line() System.Int32 VerifyReports/SourcePoint::get_Column() System.Int32 VerifyReports/SourcePoint::get_Line() System.Int32 VerifyReports/SourcePoint::get_Pc() @@ -965,9 +1204,34 @@ System.Int32[] Nethermind.LeanCIL.ContractMethodSignature::get_ParameterAttribut System.Int32[] Nethermind.LeanCIL.ProofOutline/Stub::get_Widths() System.Int32[] Nethermind.LeanCIL.ProofOutline/ViewField::get_Loaders() System.Int32[] Nethermind.LeanCIL.ProofOutline/ViewField::get_Storers() +System.Int32[] Nethermind.LeanCIL.TemplateEmitter/Method::get_LinkedMethods() +System.Int32[] Nethermind.LeanCIL.TemplateEmitter/Method::get_Opcodes() +System.Int32[] Nethermind.LeanCIL.TemplateEmitter/Template::get_Opcodes() System.Int32[] Program/FullLibrary::get_Dependencies() System.Int32[] Program/PreparedContractExport::get_LayoutIndices() +System.Int64 Nethermind.LeanCIL.MetadataImage::get_SigningBytes() +System.Int64 Nethermind.LeanCIL.MetadataImage::get_SigningReads() +System.Int64 Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::get_Literal() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_EdgeVisits() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_HeapBytes() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_NodeVisits() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_RawRows() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_TrieProbes() System.Int64 Program/CertifiedReleaseAsset::get_ArchiveBytes() +System.Int64 Program/DeepClosureWork::get_HashedBytes() +System.Int64[] Nethermind.LeanCIL.StructuralSignatures/Snapshot::get_Prefix() +System.ReadOnlySpan`1 DeepCheckInputs/Image::get_Bytes() +System.ReadOnlySpan`1 Nethermind.LeanCIL.UserString::get_Units() +System.String DeepCheckInputs/Image::get_Path() +System.String DeepCheckInputs/Pin::get_Identity() +System.String DeepCheckInputs::get_Path() +System.String FailureReports/BindingFailureDigest::get_path() +System.String FailureReports/BindingFailureDigest::get_sha256() +System.String FailureReports/BindingFailureFact::get_id() +System.String FailureReports/BindingFailureFact::get_reason() +System.String FailureReports/BindingFailureImage::get_identity() +System.String FailureReports/BindingFailureImage::get_reason() +System.String FailureReports/BindingFailureImage::get_sha256() System.String FailureReports/Diagnostic::get_file() System.String FailureReports/Diagnostic::get_kind() System.String FailureReports/Diagnostic::get_message() @@ -993,6 +1257,8 @@ System.String LeanOutput/InternalError::get_Location() System.String LeanOutput/InternalError::get_Message() System.String LeanOutput/InternalError::get_Module() System.String Nethermind.LeanCIL.Arrays/Element::get_Lean() +System.String Nethermind.LeanCIL.Arrays/NumericAccess::get_Shape() +System.String Nethermind.LeanCIL.Arrays/NumericAccess::get_Typed() System.String Nethermind.LeanCIL.Blockers/Finding::get_Construct() System.String Nethermind.LeanCIL.Blockers/Finding::get_Id() System.String Nethermind.LeanCIL.Blockers/Outcome::get_Method() @@ -1006,13 +1272,12 @@ System.String Nethermind.LeanCIL.Blockers/Site::get_Instruction() System.String Nethermind.LeanCIL.Blockers/Site::get_Message() System.String Nethermind.LeanCIL.Blockers/Site::get_Method() System.String Nethermind.LeanCIL.Blockers/Site::get_Source() -System.String Nethermind.LeanCIL.ClassViews/Field::get_Element() System.String Nethermind.LeanCIL.ClassViews/Field::get_Name() -System.String Nethermind.LeanCIL.ClassViews/FieldOperation::get_Element() System.String Nethermind.LeanCIL.ClassViews/FieldOperation::get_Owner() System.String Nethermind.LeanCIL.ClassViews/View::get_FullName() System.String Nethermind.LeanCIL.ClassViews/View::get_Path() System.String Nethermind.LeanCIL.ClassViews/View::get_Type() +System.String Nethermind.LeanCIL.ComputedLayout::get_Identity() System.String Nethermind.LeanCIL.ConstantReturns/Closed::get_Architecture() System.String Nethermind.LeanCIL.ContractBundleManifest::get_FrameworkKey() System.String Nethermind.LeanCIL.ContractBundleManifest::get_LeanCilRevision() @@ -1057,9 +1322,12 @@ System.String Nethermind.LeanCIL.ContractType::get_Name() System.String Nethermind.LeanCIL.ContractType::get_Scope() System.String Nethermind.LeanCIL.ExceptionLint/Declaration::get_Exception() System.String Nethermind.LeanCIL.ExceptionLint/Declaration::get_Text() -System.String Nethermind.LeanCIL.ExceptionLint/Site::get_Exception() System.String Nethermind.LeanCIL.ExceptionLint/Site::get_Method() System.String Nethermind.LeanCIL.ExceptionLint/Site::get_Origin() +System.String Nethermind.LeanCIL.FieldRef::get_Owner() +System.String Nethermind.LeanCIL.FieldRef::get_Type() +System.String Nethermind.LeanCIL.FrameMetadata::get_Declared() +System.String Nethermind.LeanCIL.FrameMetadata::get_ReturnsValueLean() System.String Nethermind.LeanCIL.ImportResolution/Binding::get_CertificationStatement() System.String Nethermind.LeanCIL.ImportResolution/Binding::get_CertificationStatementDigest() System.String Nethermind.LeanCIL.ImportResolution/Binding::get_CertificationTheorem() @@ -1094,15 +1362,29 @@ System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_AssetRuntimeIdenti System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_Implementation() System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_Library() System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_Sidecar() -System.String Nethermind.LeanCIL.ImportedLayoutMetadata::Bool(System.Boolean) System.String Nethermind.LeanCIL.InitializationAssumption::get_AssemblyIdentity() System.String Nethermind.LeanCIL.InitializationAssumption::get_TypeName() +System.String Nethermind.LeanCIL.InitializedStaticValue::get_Literal() System.String Nethermind.LeanCIL.InterfaceModule::get_Name() System.String Nethermind.LeanCIL.InterfaceModule::get_Source() +System.String Nethermind.LeanCIL.LayoutEvidence::IdentityTerm(System.Int32) System.String Nethermind.LeanCIL.LayoutEvidence::get_Architecture() +System.String Nethermind.LeanCIL.LeanDoc/Text::get_Value() +System.String Nethermind.LeanCIL.LeanText::Bool(System.Boolean) +System.String Nethermind.LeanCIL.LocalKind/MethodPointer::get_Signature() +System.String Nethermind.LeanCIL.LocalKind/MethodTable::get_Signature() +System.String Nethermind.LeanCIL.LocalKind/NativeMethodTable::get_Signature() +System.String Nethermind.LeanCIL.LocalKind/Object::ToLean() +System.String Nethermind.LeanCIL.LocalKind/Reference::ToLean() +System.String Nethermind.LeanCIL.LocalKind/Word32::ToLean() +System.String Nethermind.LeanCIL.LocalKind/Word64::ToLean() +System.String Nethermind.LeanCIL.ManagedTarget::get_Identity() System.String Nethermind.LeanCIL.MetadataValidation/InstanceField::get_Name() System.String Nethermind.LeanCIL.MetadataValidation/LayoutContext::get_Architecture() +System.String Nethermind.LeanCIL.MethodRef::get_Owner() +System.String Nethermind.LeanCIL.MethodRef::get_ReturnType() System.String Nethermind.LeanCIL.MethodSpecialization/Origin::get_Definition() +System.String Nethermind.LeanCIL.NarrowingText::LeanName(Nethermind.LeanCIL.Narrowing) System.String Nethermind.LeanCIL.PinnedAssemblies/Pin::get_Identity() System.String Nethermind.LeanCIL.PinnedAssemblies/Pin::get_Sha256() System.String Nethermind.LeanCIL.PinnedAssemblies/Source::get_ExpectedSha256() @@ -1133,7 +1415,6 @@ System.String Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Slot() System.String Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Value() System.String Nethermind.LeanCIL.ProofOutline/ElementHelper::get_Name() System.String Nethermind.LeanCIL.ProofOutline/Extraction::get_Architecture() -System.String Nethermind.LeanCIL.ProofOutline/Extraction::get_Source() System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Element() System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Held() System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Length() @@ -1141,13 +1422,13 @@ System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Name() System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Whole() System.String Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Value() System.String Nethermind.LeanCIL.ProofOutline/LoopOutline::get_Memory() -System.String Nethermind.LeanCIL.ProofOutline/LoopOutline::get_Source() System.String Nethermind.LeanCIL.ProofOutline/Number::get_Stacked() System.String Nethermind.LeanCIL.ProofOutline/Number::get_Term() System.String Nethermind.LeanCIL.ProofOutline/ObjectFrame::get_Entry() System.String Nethermind.LeanCIL.ProofOutline/ObjectFrame::get_Footprint() System.String Nethermind.LeanCIL.ProofOutline/ObjectTerm::get_Term() System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/ArrayArgument::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/ElementIndex::get_Term() System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::get_Formed() System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::get_Term() System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::get_Term() @@ -1178,7 +1459,6 @@ System.String Nethermind.LeanCIL.ProofOutline/ScalarCheck::get_Operation() System.String Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_Condition() System.String Nethermind.LeanCIL.ProofOutline/ScalarWord::get_Term() System.String Nethermind.LeanCIL.ProofOutline/Slot::get_Home() -System.String Nethermind.LeanCIL.ProofOutline/Slot::get_Kind() System.String Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Checked() System.String Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Head() System.String Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Remaining() @@ -1187,11 +1467,11 @@ System.String Nethermind.LeanCIL.ProofOutline/SpanTerm::get_View() System.String Nethermind.LeanCIL.ProofOutline/Specification::get_ResultWord() System.String Nethermind.LeanCIL.ProofOutline/Split::get_Condition() System.String Nethermind.LeanCIL.ProofOutline/Split::get_Fact() -System.String Nethermind.LeanCIL.ProofOutline/StructField::get_Address() -System.String Nethermind.LeanCIL.ProofOutline/StructField::get_Initial() -System.String Nethermind.LeanCIL.ProofOutline/StructField::get_Name() -System.String Nethermind.LeanCIL.ProofOutline/StructField::get_Owned() -System.String Nethermind.LeanCIL.ProofOutline/StructField::get_Read() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Address() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Initial() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Owned() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Read() System.String Nethermind.LeanCIL.ProofOutline/StructWalk::get_Returned() System.String Nethermind.LeanCIL.ProofOutline/StructWalk::get_Stopped() System.String Nethermind.LeanCIL.ProofOutline/Stub::get_Name() @@ -1199,7 +1479,6 @@ System.String Nethermind.LeanCIL.ProofOutline/StubCall::get_Input() System.String Nethermind.LeanCIL.ProofOutline/StubCall::get_Where() System.String Nethermind.LeanCIL.ProofOutline/Thrown::get_Where() System.String Nethermind.LeanCIL.ProofOutline/Unknown::get_What() -System.String Nethermind.LeanCIL.ProofOutline/ViewField::get_Element() System.String Nethermind.LeanCIL.ProofOutline/ViewField::get_Name() System.String Nethermind.LeanCIL.ProofOutline/ViewField::get_Path() System.String Nethermind.LeanCIL.ProofOutline/WordRead::get_Index() @@ -1215,14 +1494,15 @@ System.String Nethermind.LeanCIL.StaticIdentityRecord::get_AssemblyIdentity() System.String Nethermind.LeanCIL.StaticIdentityRecord::get_FieldName() System.String Nethermind.LeanCIL.StaticIdentityRecord::get_Identity() System.String Nethermind.LeanCIL.StaticValues/Value::get_Literal() +System.String Nethermind.LeanCIL.StructuralSignatures/Primitive::get_Name() +System.String Nethermind.LeanCIL.Symbols/PdbFile::get_Sha256() System.String Nethermind.LeanCIL.TemplateEmitter/Method::get_Identity() -System.String Nethermind.LeanCIL.TemplateEmitter/Method::get_Metadata() System.String Nethermind.LeanCIL.TemplateEmitter/Options::get_ModuleName() System.String Nethermind.LeanCIL.TemplateEmitter/Options::get_ModulePath() System.String Nethermind.LeanCIL.TemplateEmitter/Options::get_Namespace() System.String Nethermind.LeanCIL.TemplateEmitter/Options::get_Output() +System.String Nethermind.LeanCIL.TemplateEmitter/Row::get_ComparisonBody() System.String Nethermind.LeanCIL.TemplateEmitter/Template::get_Identity() -System.String Nethermind.LeanCIL.TemplateEmitter/Template::get_Metadata() System.String Nethermind.LeanCIL.TemplateEmitter/Variant::get_AssemblyHash() System.String Nethermind.LeanCIL.TemplateEmitter/Variant::get_Directory() System.String Nethermind.LeanCIL.TypeTests/Node::get_Kind() @@ -1230,6 +1510,7 @@ System.String Nethermind.LeanCIL.TypeTests/Node::get_Name() System.String Nethermind.LeanCIL.TypeTests/Node::get_Scope() System.String Program/AuditConstantRequest::get_name() System.String Program/AuditRefutation::get_contract() +System.String Program/AuditRefutation::get_program() System.String Program/AuditRequest::get_name() System.String Program/AuditedClosure::get_canonical() System.String Program/AuditedClosure::get_error() @@ -1258,6 +1539,19 @@ System.String Program/BatchResult::get_status() System.String Program/BatchResult::get_theorem() System.String Program/BindingCatalog::get_GeneratedRoot() System.String Program/BindingCatalog::get_Path() +System.String Program/BindingFactResult::get_reason() +System.String Program/BindingFactResult::get_theorem() +System.String Program/BindingImagePin::get_Identity() +System.String Program/BindingLibraryPreparation::get_Library() +System.String Program/BindingLibraryPreparation::get_LibraryKey() +System.String Program/BindingLibraryPreparation::get_Toolchain() +System.String Program/BindingPlan::get_Output() +System.String Program/BindingPublication::get_Contract() +System.String Program/BindingPublication::get_Identity() +System.String Program/BindingPublication::get_InterfaceDigest() +System.String Program/BindingPublication::get_Profile() +System.String Program/BindingRequest::get_CoveragePredicate() +System.String Program/BindingRequest::get_Module() System.String Program/BuildDiagnostic::get_Code() System.String Program/BuildDiagnostic::get_File() System.String Program/BuildDiagnostic::get_Message() @@ -1300,11 +1594,16 @@ System.String Program/ConsumerSupportManifest::get_Library() System.String Program/ConsumerSupportManifest::get_Policy() System.String Program/ConsumerSupportManifest::get_Toolchain() System.String Program/ConsumerSupportManifest::get_Tools() +System.String Program/DeepCheckAttempt::get_IdentitySha256() +System.String Program/DeepSessionContribution::get_ResultKey() +System.String Program/DeepSessionContribution::get_SourceDirectory() System.String Program/DeploymentDependency::get_acceptedVia() System.String Program/DeploymentDependency::get_assemblyIdentity() System.String Program/DeploymentDependency::get_implementationSha256() System.String Program/DeploymentDependency::get_interfaceDigest() System.String Program/DeploymentDependency::get_sourceBundleDigest() +System.String Program/DigestFile::get_path() +System.String Program/DigestFile::get_sha256() System.String Program/EditorProof::get_Proof() System.String Program/EditorProof::get_Theorem() System.String Program/EditorSelection::get_Assembly() @@ -1317,6 +1616,9 @@ System.String Program/ExportClosure::get_Canonical() System.String Program/ExportClosure::get_Contract() System.String Program/ExportClosure::get_Digest() System.String Program/ExportClosure::get_Kind() +System.String Program/ExtractedMethod::get_assembly() +System.String Program/ExtractedMethod::get_assemblySha256() +System.String Program/ExtractedMethod::get_signature() System.String Program/ExtractionComponents::get_Aggregate() System.String Program/ExtractionComponents::get_InputHash() System.String Program/ExtractionIdentity::get_Output() @@ -1405,6 +1707,10 @@ System.String Program/GeneratedExtraction::get_Assembly() System.String Program/GeneratedExtraction::get_Entry() System.String Program/GeneratedExtraction::get_Output() System.String Program/GeneratedExtraction::get_Profile() +System.String Program/ImageResult::get_identity() +System.String Program/ImageResult::get_reason() +System.String Program/ImageResult::get_sha256() +System.String Program/ImageResult::get_theorem() System.String Program/LoadedGates::get_Digest() System.String Program/LoadedGates::get_Directory() System.String Program/LoadedGates::get_OutputRoot() @@ -1435,6 +1741,7 @@ System.String Program/PublishedImport::get_interfaceDigest() System.String Program/PublishedImport::get_profile() System.String Program/PublishedImport::get_sidecarSha256() System.String Program/PublishedImport::get_sourceBundleDigest() +System.String Program/ReplayRejection::get_Declaration() System.String Program/Report::get_assemblySha256() System.String Program/Report::get_batchSourceSha256() System.String Program/Report::get_batchTheorem() @@ -1447,11 +1754,19 @@ System.String Program/Report::get_libraryBuild() System.String Program/Report::get_libraryKey() System.String Program/Report::get_theorem() System.String Program/Report::get_theoremAudit() +System.String Program/StandaloneCachePromotion::get_ExtractionKey() +System.String Program/StandaloneCachePromotion::get_ExtractionSuffix() +System.String Program/StandaloneCachePromotion::get_FullLibraryKey() +System.String Program/StandaloneCachePromotion::get_ProofKey() +System.String Program/StandaloneCachePromotion::get_ProofSuffix() System.String Program/StatementDefinition::get_name() System.String Program/StatementDefinition::get_text() System.String Program/StatementIdentity::get_mismatch() System.String Program/StatementIdentity::get_termError() System.String Program/StatementRequest::get_Expected() +System.String Program/StatementSources::get_lean() +System.String Program/StatementSources::get_leanToolchain() +System.String Program/StatementSources::get_strictBinding() System.String Program/StatementSubject::get_head() System.String Program/StatementSubject::get_role() System.String Program/StatementSubject::get_signature() @@ -1460,6 +1775,7 @@ System.String Program/TrustedSupport::get_Path() System.String Program/VerificationSettings::get_proof() System.String Program/VerificationSettings::get_proofRoot() System.String Program/VerificationSettings::get_theorem() +System.String Program/VerifiedRefutation::get_contract() System.String Program/WatchResult::get_Message() System.String Program/WatchResult::get_Method() System.String Program/WatchResult::get_Module() @@ -1482,6 +1798,7 @@ System.String Program/WorkspaceSettings::get_Theorem() System.String VerifyReports/ManifestMethod::get_Signature() System.String VerifyReports/SourcePoint::get_File() System.String[] FailureReports/Report::get_rerun() +System.String[] Nethermind.LeanCIL.Blockers/Outcome::get_DecodedMethods() System.String[] Nethermind.LeanCIL.Blockers/Outcome::get_TypeArguments() System.String[] Nethermind.LeanCIL.ContractExport::get_Profiles() System.String[] Nethermind.LeanCIL.ContractExport::get_TypeArguments() @@ -1490,15 +1807,11 @@ System.String[] Nethermind.LeanCIL.ImportResolution/Binding::get_TypeArguments() System.String[] Nethermind.LeanCIL.MethodSpecialization/Origin::get_TypeArguments() System.String[] Nethermind.LeanCIL.Program/ExtractionRequest::get_InitializedTypes() System.String[] Nethermind.LeanCIL.Program/ExtractionRequest::get_TypeArguments() -System.String[] Nethermind.LeanCIL.ProofOutline/Body::get_Ops() -System.String[] Nethermind.LeanCIL.ProofOutline/Extraction::get_LayoutIdentities() System.String[] Nethermind.LeanCIL.ProofOutline/ObjectFrame::get_Formed() System.String[] Nethermind.LeanCIL.ProofOutline/Stub::get_Inputs() System.String[] Nethermind.LeanCIL.RuntimeAsset::get_ReferenceAssets() System.String[] Nethermind.LeanCIL.StaticValues/Value::get_Arguments() -System.String[] Nethermind.LeanCIL.TemplateEmitter/Method::get_Ops() System.String[] Nethermind.LeanCIL.TemplateEmitter/Options::get_Extractions() -System.String[] Nethermind.LeanCIL.TemplateEmitter/Row::get_Ops() System.String[] Nethermind.LeanCIL.TemplateEmitter/Template::get_Callees() System.String[] Program/AuditConstantRequest::get_axioms() System.String[] Program/AuditNamespaces::get_axioms() @@ -1516,6 +1829,7 @@ System.String[] Program/ClosureEntry::get_Modules() System.String[] Program/ConsumerExportAudit::get_Closures() System.String[] Program/ConsumerSupportModule::get_Files() System.String[] Program/ConsumerSupportModule::get_Imports() +System.String[] Program/DeepSessionContribution::get_Modules() System.String[] Program/DeploymentDependency::get_dependencies() System.String[] Program/EditorSelection::get_InitializedTypes() System.String[] Program/GateDefaults::get_initializedTypes() @@ -1551,7 +1865,26 @@ System.String[] Program/Report::get_staticValues() System.String[] Program/WatchSettings::get_Projects() System.String[] Program/WorkspaceSettings::get_InitializedTypes() System.String[] VerifyReports/ManifestMethod::get_Instructions() +System.UInt16 Nethermind.LeanCIL.AssemblyVersion::get_Build() +System.UInt16 Nethermind.LeanCIL.AssemblyVersion::get_Major() +System.UInt16 Nethermind.LeanCIL.AssemblyVersion::get_Minor() +System.UInt16 Nethermind.LeanCIL.AssemblyVersion::get_Revision() +System.UInt32 Nethermind.LeanCIL.MetadataImage/NamedRow::get_Assembly() +System.UInt32 Nethermind.LeanCIL.MetadataImage/NamedRow::get_Parent() +System.UInt32 Nethermind.LeanCIL.MetadataImage/TypeNameRow::get_ResolutionScope() +System.UInt32 Nethermind.LeanCIL.MetadataSlices/Slice::GenericOwnerCode(System.UInt32) +System.UInt32 Nethermind.LeanCIL.MetadataSlices::OmittedHeapOffset(System.Int32) +System.UInt32 Nethermind.LeanCIL.StructuralSignatures/Primitive::get_Assembly() +System.UInt32 Nethermind.LeanCIL.StructuralSignatures/Primitive::get_Token() System.UInt64 Nethermind.LeanCIL.StaticValues/Word::get_Bits() +System.Void FailureReports/BindingFailure::.ctor(FailureReports/BindingFailure) +System.Void FailureReports/BindingFailure::.ctor(FailureReports/BindingFailureDigest,FailureReports/BindingFailureImage[],FailureReports/BindingFailureFact[]) +System.Void FailureReports/BindingFailureDigest::.ctor(FailureReports/BindingFailureDigest) +System.Void FailureReports/BindingFailureDigest::.ctor(System.String,System.String) +System.Void FailureReports/BindingFailureFact::.ctor(FailureReports/BindingFailureFact) +System.Void FailureReports/BindingFailureFact::.ctor(System.String,FailureReports/BindingFailureStatus,System.String) +System.Void FailureReports/BindingFailureImage::.ctor(FailureReports/BindingFailureImage) +System.Void FailureReports/BindingFailureImage::.ctor(System.String,System.String,FailureReports/BindingFailureStatus,System.String) System.Void FailureReports/Method::.ctor(FailureReports/Method) System.Void FailureReports/Method::.ctor(System.String,System.String,System.String) System.Void LeanDiagnostics/Diagnostic::.ctor(LeanDiagnostics/Diagnostic) @@ -1560,11 +1893,14 @@ System.Void LeanDiagnostics/Report::.ctor(System.String,System.String) System.Void LeanOutput/InternalError::.ctor(LeanOutput/InternalError) System.Void LeanOutput/InternalError::.ctor(System.String,System.String,System.String) System.Void Nethermind.LeanCIL.Arrays/Element::.ctor(Nethermind.LeanCIL.Arrays/Element) -System.Void Nethermind.LeanCIL.Arrays/Element::.ctor(System.String,System.Int32,System.Boolean,Nethermind.LeanCIL.NativeOperands/Kind) +System.Void Nethermind.LeanCIL.Arrays/Element::.ctor(System.String,System.Int32,System.Boolean,Nethermind.LeanCIL.NativeOperands/Kind,System.Boolean) +System.Void Nethermind.LeanCIL.AssemblyVersion::.ctor(System.UInt16,System.UInt16,System.UInt16,System.UInt16) +System.Void Nethermind.LeanCIL.AssemblyVersion::Deconstruct(System.UInt16&,System.UInt16&,System.UInt16&,System.UInt16&) System.Void Nethermind.LeanCIL.Blockers/Finding::set_Id(System.String) System.Void Nethermind.LeanCIL.ClassViews/Access::.ctor(Nethermind.LeanCIL.ClassViews/Access) System.Void Nethermind.LeanCIL.ClassViews/View::.ctor(Nethermind.LeanCIL.ClassViews/View) System.Void Nethermind.LeanCIL.ClassViews/View::.ctor(System.String,System.String,System.String,Nethermind.LeanCIL.ClassViews/Field[]) +System.Void Nethermind.LeanCIL.ComputedLayout::.ctor(Nethermind.LeanCIL.ComputedLayout) System.Void Nethermind.LeanCIL.ConstantReturns/Closed::.ctor(Nethermind.LeanCIL.ConstantReturns/Closed) System.Void Nethermind.LeanCIL.ContractCertification::.ctor(Nethermind.LeanCIL.ContractCertification) System.Void Nethermind.LeanCIL.ContractCertification::.ctor(System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,Nethermind.LeanCIL.StaticIdentityRecord[]) @@ -1587,7 +1923,11 @@ System.Void Nethermind.LeanCIL.ContractType::.ctor(System.String,System.String,S System.Void Nethermind.LeanCIL.ExceptionLint/Declaration::.ctor(Nethermind.LeanCIL.ExceptionLint/Declaration) System.Void Nethermind.LeanCIL.ExceptionLint/Declaration::.ctor(System.String,System.String) System.Void Nethermind.LeanCIL.ExceptionLint/Site::.ctor(Nethermind.LeanCIL.ExceptionLint/Site) -System.Void Nethermind.LeanCIL.ExceptionLint/Site::.ctor(System.String,System.Int32,System.String,System.String) +System.Void Nethermind.LeanCIL.ExceptionLint/Site::.ctor(System.String,System.Int32,Nethermind.LeanCIL.ExceptionType,System.String) +System.Void Nethermind.LeanCIL.FieldRef::.ctor(Nethermind.LeanCIL.FieldRef) +System.Void Nethermind.LeanCIL.FieldRef::.ctor(System.String,System.Int32,System.String,Nethermind.LeanCIL.MetadataName) +System.Void Nethermind.LeanCIL.FrameMetadata::.ctor(Nethermind.LeanCIL.FrameMetadata) +System.Void Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::.ctor(Nethermind.LeanCIL.GenericEvidence/DecodeRefusal) System.Void Nethermind.LeanCIL.ImportResolution/Binding::.ctor(Nethermind.LeanCIL.ImportResolution/Binding) System.Void Nethermind.LeanCIL.ImportResolution/Binding::.ctor(System.Int32,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.Int32,System.Boolean,System.String,System.String,System.String,System.String,System.String,System.Boolean,Nethermind.LeanCIL.StaticIdentityRecord[],Nethermind.LeanCIL.InitializationAssumption[],System.String,System.String[],Nethermind.LeanCIL.ContractMethodSignature,Nethermind.LeanCIL.ContractLayout[],Nethermind.LeanCIL.ContractLayout[],System.String) System.Void Nethermind.LeanCIL.ImportResolution/Library::.ctor(Nethermind.LeanCIL.ImportResolution/Library) @@ -1601,14 +1941,73 @@ System.Void Nethermind.LeanCIL.InitializationAssumption::.ctor(Nethermind.LeanCI System.Void Nethermind.LeanCIL.InitializationAssumption::.ctor(System.String,System.String) System.Void Nethermind.LeanCIL.InterfaceModule::.ctor(Nethermind.LeanCIL.InterfaceModule) System.Void Nethermind.LeanCIL.InterfaceModule::.ctor(System.String,System.String) +System.Void Nethermind.LeanCIL.LayoutField::.ctor(Nethermind.LeanCIL.LayoutField) +System.Void Nethermind.LeanCIL.LayoutField::.ctor(System.Int32,System.Int32,System.Boolean) +System.Void Nethermind.LeanCIL.LayoutField::Deconstruct(System.Int32&,System.Int32&,System.Boolean&) +System.Void Nethermind.LeanCIL.LeanDoc/Item::.ctor(Nethermind.LeanCIL.LeanDoc,System.Int32) +System.Void Nethermind.LeanCIL.LeanDoc/Item::.ctor(Nethermind.LeanCIL.LeanDoc/Item) +System.Void Nethermind.LeanCIL.LeanDoc/Space::.ctor(System.Boolean,System.Int32) +System.Void Nethermind.LeanCIL.LeanDoc/Space::Deconstruct(System.Boolean&,System.Int32&) +System.Void Nethermind.LeanCIL.LeanDoc/WorkGroup::.ctor(Nethermind.LeanCIL.LeanDoc/WorkGroup) +System.Void Nethermind.LeanCIL.LocalKind/Aggregate::.ctor(Nethermind.LeanCIL.LocalKind/Aggregate) +System.Void Nethermind.LeanCIL.LocalKind/Aggregate::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.LocalKind/Aggregate::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.LocalKind/Array::.ctor(Nethermind.LeanCIL.Arrays/Element) +System.Void Nethermind.LeanCIL.LocalKind/Array::.ctor(Nethermind.LeanCIL.LocalKind/Array) +System.Void Nethermind.LeanCIL.LocalKind/EnumSpan::.ctor(Nethermind.LeanCIL.Arrays/Element,System.Boolean,System.Boolean) +System.Void Nethermind.LeanCIL.LocalKind/EnumSpan::.ctor(Nethermind.LeanCIL.LocalKind/EnumSpan) +System.Void Nethermind.LeanCIL.LocalKind/MethodPointer::.ctor(Nethermind.LeanCIL.LocalKind/MethodPointer) +System.Void Nethermind.LeanCIL.LocalKind/MethodPointer::.ctor(System.String) +System.Void Nethermind.LeanCIL.LocalKind/MethodTable::.ctor(Nethermind.LeanCIL.LocalKind/MethodTable) +System.Void Nethermind.LeanCIL.LocalKind/MethodTable::.ctor(System.String) +System.Void Nethermind.LeanCIL.LocalKind/Narrow::.ctor(Nethermind.LeanCIL.LocalKind/Narrow) +System.Void Nethermind.LeanCIL.LocalKind/Narrow::.ctor(Nethermind.LeanCIL.Narrowing) +System.Void Nethermind.LeanCIL.LocalKind/Narrow::Deconstruct(Nethermind.LeanCIL.Narrowing&) +System.Void Nethermind.LeanCIL.LocalKind/NativeMethodTable::.ctor(Nethermind.LeanCIL.LocalKind/NativeMethodTable) +System.Void Nethermind.LeanCIL.LocalKind/NativeMethodTable::.ctor(System.String) +System.Void Nethermind.LeanCIL.LocalKind/Object::.ctor() +System.Void Nethermind.LeanCIL.LocalKind/Object::.ctor(Nethermind.LeanCIL.LocalKind/Object) +System.Void Nethermind.LeanCIL.LocalKind/Reference::.ctor() +System.Void Nethermind.LeanCIL.LocalKind/Reference::.ctor(Nethermind.LeanCIL.LocalKind/Reference) +System.Void Nethermind.LeanCIL.LocalKind/Span::.ctor(Nethermind.LeanCIL.LocalKind/Span) +System.Void Nethermind.LeanCIL.LocalKind/Span::.ctor(System.Int32,System.Boolean) +System.Void Nethermind.LeanCIL.LocalKind/Span::Deconstruct(System.Int32&,System.Boolean&) +System.Void Nethermind.LeanCIL.LocalKind/Vector::.ctor(Nethermind.LeanCIL.LocalKind/Vector) +System.Void Nethermind.LeanCIL.LocalKind/Vector::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.LocalKind/Vector::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.LocalKind/Word32::.ctor() +System.Void Nethermind.LeanCIL.LocalKind/Word32::.ctor(Nethermind.LeanCIL.LocalKind/Word32) +System.Void Nethermind.LeanCIL.LocalKind/Word64::.ctor() +System.Void Nethermind.LeanCIL.LocalKind/Word64::.ctor(Nethermind.LeanCIL.LocalKind/Word64) +System.Void Nethermind.LeanCIL.LocalKind::.ctor() +System.Void Nethermind.LeanCIL.LocalKind::.ctor(Nethermind.LeanCIL.LocalKind) +System.Void Nethermind.LeanCIL.ManagedTarget::.ctor(Nethermind.LeanCIL.ManagedTarget) +System.Void Nethermind.LeanCIL.ManagedTarget::.ctor(System.String,System.Int32) +System.Void Nethermind.LeanCIL.MetadataImage/NamedRow::.ctor(Nethermind.LeanCIL.MetadataImage/NamedRow) +System.Void Nethermind.LeanCIL.MetadataImage/NamedRow::.ctor(System.Int32,System.Int32,System.UInt32,System.UInt32) +System.Void Nethermind.LeanCIL.MetadataImage/NamedRow::Deconstruct(System.Int32&,System.Int32&,System.UInt32&,System.UInt32&) +System.Void Nethermind.LeanCIL.MetadataImage/TypeNameRow::.ctor(Nethermind.LeanCIL.MetadataImage/TypeNameRow) +System.Void Nethermind.LeanCIL.MetadataImage/TypeNameRow::.ctor(Nethermind.LeanCIL.TypeLevel,System.UInt32) +System.Void Nethermind.LeanCIL.MetadataImage::RecordSigningBytes(System.Int32) +System.Void Nethermind.LeanCIL.MetadataImage::set_SigningBytes(System.Int64) +System.Void Nethermind.LeanCIL.MetadataImage::set_SigningReads(System.Int64) +System.Void Nethermind.LeanCIL.MetadataSlices::.ctor(Nethermind.LeanCIL.PinnedAssemblies) System.Void Nethermind.LeanCIL.MetadataValidation/AggregateInfo::.ctor(Nethermind.LeanCIL.MetadataValidation/AggregateInfo) System.Void Nethermind.LeanCIL.MetadataValidation/InstanceField::.ctor(Nethermind.LeanCIL.MetadataValidation/InstanceField) System.Void Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::.ctor(Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata) System.Void Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::.ctor(System.Boolean) System.Void Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::Deconstruct(System.Boolean&) +System.Void Nethermind.LeanCIL.MethodBodyBytes/Body::.ctor(Nethermind.LeanCIL.MethodBodyBytes/Body) +System.Void Nethermind.LeanCIL.MethodBodyBytes/Body::.ctor(System.Byte[],System.Int32) +System.Void Nethermind.LeanCIL.MethodRef::.ctor(Nethermind.LeanCIL.MethodRef) System.Void Nethermind.LeanCIL.MethodSpecialization/Origin::.ctor(Nethermind.LeanCIL.MethodSpecialization/Origin) System.Void Nethermind.LeanCIL.MethodSpecialization/Origin::.ctor(System.String,System.String[]) System.Void Nethermind.LeanCIL.NativeOperands/Certificate::.ctor(Nethermind.LeanCIL.NativeOperands/Certificate) +System.Void Nethermind.LeanCIL.Operation::.ctor() +System.Void Nethermind.LeanCIL.Operation::.ctor(Nethermind.LeanCIL.Operation) +System.Void Nethermind.LeanCIL.ParameterHome::.ctor(Nethermind.LeanCIL.ParameterHome) +System.Void Nethermind.LeanCIL.ParameterHome::.ctor(System.Int32,Nethermind.LeanCIL.LocalKind) +System.Void Nethermind.LeanCIL.PinnedAssemblies/IdentityRow::.ctor(Nethermind.LeanCIL.MetadataImage,System.UInt32) System.Void Nethermind.LeanCIL.PinnedAssemblies/Source::.ctor(Nethermind.LeanCIL.PinnedAssemblies/Source) System.Void Nethermind.LeanCIL.PinnedAssemblies/Source::.ctor(System.String,System.String) System.Void Nethermind.LeanCIL.ProfileInitializationAssumptions::.ctor(Nethermind.LeanCIL.ProfileInitializationAssumptions) @@ -1624,12 +2023,12 @@ System.Void Nethermind.LeanCIL.ProofOutline/AddedReference::.ctor(System.String, System.Void Nethermind.LeanCIL.ProofOutline/Allocation::.ctor(Nethermind.LeanCIL.ProofOutline/Allocation) System.Void Nethermind.LeanCIL.ProofOutline/Allocation::.ctor(System.String,System.String,System.String,System.Int32,System.Int32) System.Void Nethermind.LeanCIL.ProofOutline/ArrayAccess::.ctor(Nethermind.LeanCIL.ProofOutline/ArrayAccess) -System.Void Nethermind.LeanCIL.ProofOutline/ArrayAccess::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter,Nethermind.LeanCIL.ProofOutline/Parameter,Nethermind.LeanCIL.ProofOutline/ElementHelper) -System.Void Nethermind.LeanCIL.ProofOutline/ArrayLength::.ctor(Nethermind.LeanCIL.ProofOutline/ArrayLength) -System.Void Nethermind.LeanCIL.ProofOutline/ArrayLength::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void Nethermind.LeanCIL.ProofOutline/ArrayAccess::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter,Nethermind.LeanCIL.ProofOutline/Parameter,Nethermind.LeanCIL.ProofOutline/ElementHelper,Nethermind.LeanCIL.Arrays/NumericAccess) +System.Void Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::.ctor(Nethermind.LeanCIL.ProofOutline/ArrayLengthValue) +System.Void Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter) System.Void Nethermind.LeanCIL.ProofOutline/ArrayUse::.ctor(Nethermind.LeanCIL.ProofOutline/ArrayUse) System.Void Nethermind.LeanCIL.ProofOutline/ArrayUse::.ctor(Nethermind.LeanCIL.ProofOutline/HeldArray,System.Int32,System.Boolean) -System.Void Nethermind.LeanCIL.ProofOutline/Body::.ctor(System.String[]) +System.Void Nethermind.LeanCIL.ProofOutline/Body::.ctor(Nethermind.LeanCIL.ProofOutline/Method) System.Void Nethermind.LeanCIL.ProofOutline/Callee::.ctor(Nethermind.LeanCIL.ProofOutline/Callee) System.Void Nethermind.LeanCIL.ProofOutline/Callee::.ctor(System.Int32,System.String,Nethermind.LeanCIL.ProofOutline/Specification,Nethermind.LeanCIL.ProofOutline/ObjectWalk,Nethermind.LeanCIL.ProofOutline/ObjectPlan,System.Boolean) System.Void Nethermind.LeanCIL.ProofOutline/Component::.ctor(Nethermind.LeanCIL.ProofOutline/Component) @@ -1638,20 +2037,18 @@ System.Void Nethermind.LeanCIL.ProofOutline/Element::.ctor(Nethermind.LeanCIL.Pr System.Void Nethermind.LeanCIL.ProofOutline/Element::.ctor(System.Int32) System.Void Nethermind.LeanCIL.ProofOutline/Element::Deconstruct(System.Int32&) System.Void Nethermind.LeanCIL.ProofOutline/ElementAccess::.ctor(Nethermind.LeanCIL.ProofOutline/ElementAccess) -System.Void Nethermind.LeanCIL.ProofOutline/ElementAccess::.ctor(System.String,Nethermind.LeanCIL.ProofOutline/ElementKind,System.Int32,System.String,System.String,Nethermind.LeanCIL.ProofOutline/Reached,System.String,System.String,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/ElementAccess::.ctor(System.String,Nethermind.LeanCIL.ProofOutline/ElementKind,System.Int32,System.String,System.String,Nethermind.LeanCIL.ProofOutline/Reached,System.String,System.String,System.Boolean,System.Boolean) System.Void Nethermind.LeanCIL.ProofOutline/ElementHelper::.ctor(Nethermind.LeanCIL.ProofOutline/ElementHelper) System.Void Nethermind.LeanCIL.ProofOutline/HeldArray::.ctor(Nethermind.LeanCIL.ProofOutline/HeldArray) System.Void Nethermind.LeanCIL.ProofOutline/HeldArray::.ctor(System.String,System.String,System.String,System.String,System.String,Nethermind.LeanCIL.ProofOutline/Reached,Nethermind.LeanCIL.ProofOutline/ViewField,Nethermind.LeanCIL.ProofOutline/Parameter) System.Void Nethermind.LeanCIL.ProofOutline/LocalAccess::.ctor(Nethermind.LeanCIL.ProofOutline/LocalAccess) System.Void Nethermind.LeanCIL.ProofOutline/LocalAccess::.ctor(System.Int32,System.String,System.Int32,System.Int32,System.Boolean) -System.Void Nethermind.LeanCIL.ProofOutline/LocalAddress::.ctor(Nethermind.LeanCIL.ProofOutline/LocalAddress) -System.Void Nethermind.LeanCIL.ProofOutline/LocalAddress::.ctor(System.Int32) -System.Void Nethermind.LeanCIL.ProofOutline/LocalAddress::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.ProofOutline/LocalAddressValue::.ctor(Nethermind.LeanCIL.ProofOutline/LocalAddressValue) +System.Void Nethermind.LeanCIL.ProofOutline/LocalAddressValue::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/LocalAddressValue::Deconstruct(System.Int32&) System.Void Nethermind.LeanCIL.ProofOutline/LocalValue::.ctor(Nethermind.LeanCIL.ProofOutline/LocalValue) System.Void Nethermind.LeanCIL.ProofOutline/LocalValue::.ctor(System.Int32) System.Void Nethermind.LeanCIL.ProofOutline/LocalValue::Deconstruct(System.Int32&) -System.Void Nethermind.LeanCIL.ProofOutline/Number::.ctor(Nethermind.LeanCIL.ProofOutline/Number) -System.Void Nethermind.LeanCIL.ProofOutline/Number::.ctor(System.String,System.Int32,System.Boolean,System.Boolean,System.String) System.Void Nethermind.LeanCIL.ProofOutline/ObjectFrame::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectFrame) System.Void Nethermind.LeanCIL.ProofOutline/ObjectFrame::.ctor(System.String,System.String,System.Int32,System.String[]) System.Void Nethermind.LeanCIL.ProofOutline/ObjectFrameEntry::.ctor() @@ -1671,7 +2068,7 @@ System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::.ctor(System.Stri System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values::.ctor(Nethermind.LeanCIL.ProofOutline/HeldArray) System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values) System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word) -System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::.ctor(System.String,System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::.ctor(System.String,System.Int32,System.Int64,System.Boolean) System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk::set_Fresh(Nethermind.LeanCIL.ProofOutline/Allocation) System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk::set_Returned(System.String) System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk::set_Stopped(System.String) @@ -1685,7 +2082,7 @@ System.Void Nethermind.LeanCIL.ProofOutline/ReceiverField::.ctor(System.String,S System.Void Nethermind.LeanCIL.ProofOutline/ReferenceArgument::.ctor(Nethermind.LeanCIL.ProofOutline/ReferenceArgument) System.Void Nethermind.LeanCIL.ProofOutline/ReferenceArgument::.ctor(System.String) System.Void Nethermind.LeanCIL.ProofOutline/Result::.ctor(Nethermind.LeanCIL.ProofOutline/Result) -System.Void Nethermind.LeanCIL.ProofOutline/Result::.ctor(System.String,System.String,System.Boolean,System.Boolean,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/Result::.ctor(System.String,System.Int32,System.Boolean,System.Boolean,System.Boolean) System.Void Nethermind.LeanCIL.ProofOutline/Returning::.ctor(Nethermind.LeanCIL.ProofOutline/Returning) System.Void Nethermind.LeanCIL.ProofOutline/Returning::.ctor(System.String,System.Boolean,System.String,System.Boolean,System.Boolean) System.Void Nethermind.LeanCIL.ProofOutline/ScalarCheck::.ctor(Nethermind.LeanCIL.ProofOutline/ScalarCheck) @@ -1696,28 +2093,26 @@ System.Void Nethermind.LeanCIL.ProofOutline/ScalarSplit::.ctor(Nethermind.LeanCI System.Void Nethermind.LeanCIL.ProofOutline/ScalarSplit::.ctor(System.String,System.Int32,System.Int32,Nethermind.LeanCIL.ProofOutline/ScalarStep,System.Int32,Nethermind.LeanCIL.ProofOutline/ScalarStep) System.Void Nethermind.LeanCIL.ProofOutline/ScalarStep::.ctor() System.Void Nethermind.LeanCIL.ProofOutline/ScalarStep::.ctor(Nethermind.LeanCIL.ProofOutline/ScalarStep) -System.Void Nethermind.LeanCIL.ProofOutline/ScalarWord::.ctor(Nethermind.LeanCIL.ProofOutline/ScalarWord) -System.Void Nethermind.LeanCIL.ProofOutline/ScalarWord::.ctor(System.String,System.Int32) System.Void Nethermind.LeanCIL.ProofOutline/Sliced::.ctor(Nethermind.LeanCIL.ProofOutline/Sliced) System.Void Nethermind.LeanCIL.ProofOutline/Sliced::.ctor(System.Int32) System.Void Nethermind.LeanCIL.ProofOutline/Sliced::Deconstruct(System.Int32&) System.Void Nethermind.LeanCIL.ProofOutline/Slot::.ctor(Nethermind.LeanCIL.ProofOutline/Slot) -System.Void Nethermind.LeanCIL.ProofOutline/Slot::.ctor(System.Int32,System.String,System.String,System.Int32,Nethermind.LeanCIL.ProofOutline/Component[],System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/Slot::.ctor(System.Int32,Nethermind.LeanCIL.LocalKind,System.String,System.Int32,Nethermind.LeanCIL.ProofOutline/Component[],System.Boolean) System.Void Nethermind.LeanCIL.ProofOutline/SpanArgument::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter) System.Void Nethermind.LeanCIL.ProofOutline/SpanArgument::.ctor(Nethermind.LeanCIL.ProofOutline/SpanArgument) -System.Void Nethermind.LeanCIL.ProofOutline/SpanLength::.ctor(Nethermind.LeanCIL.ProofOutline/SpanLength) -System.Void Nethermind.LeanCIL.ProofOutline/SpanLength::.ctor(System.Int32) -System.Void Nethermind.LeanCIL.ProofOutline/SpanLength::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.ProofOutline/SpanLengthValue::.ctor(Nethermind.LeanCIL.ProofOutline/SpanLengthValue) +System.Void Nethermind.LeanCIL.ProofOutline/SpanLengthValue::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/SpanLengthValue::Deconstruct(System.Int32&) System.Void Nethermind.LeanCIL.ProofOutline/SpanTerm::.ctor(Nethermind.LeanCIL.ProofOutline/SpanTerm) System.Void Nethermind.LeanCIL.ProofOutline/SpanTerm::.ctor(System.String,System.String,System.String,System.Boolean,System.String,System.String,System.Boolean) System.Void Nethermind.LeanCIL.ProofOutline/SpannedArray::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter) System.Void Nethermind.LeanCIL.ProofOutline/SpannedArray::.ctor(Nethermind.LeanCIL.ProofOutline/SpannedArray) System.Void Nethermind.LeanCIL.ProofOutline/Specification::set_NonNull(Nethermind.LeanCIL.ProofOutline/Parameter[]) System.Void Nethermind.LeanCIL.ProofOutline/Specification::set_Plan(Nethermind.LeanCIL.ProofOutline/ObjectPlan) -System.Void Nethermind.LeanCIL.ProofOutline/StructField::set_Initial(System.String) -System.Void Nethermind.LeanCIL.ProofOutline/StructField::set_Owned(System.String) -System.Void Nethermind.LeanCIL.ProofOutline/StructField::set_Read(System.String) -System.Void Nethermind.LeanCIL.ProofOutline/StructField::set_Stored(System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/StructScalar::set_Initial(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/StructScalar::set_Owned(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/StructScalar::set_Read(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/StructScalar::set_Stored(System.Boolean) System.Void Nethermind.LeanCIL.ProofOutline/StructWalk::set_Returned(System.String) System.Void Nethermind.LeanCIL.ProofOutline/StructWalk::set_Stopped(System.String) System.Void Nethermind.LeanCIL.ProofOutline/Stub::.ctor(Nethermind.LeanCIL.ProofOutline/Stub) @@ -1733,9 +2128,14 @@ System.Void Nethermind.LeanCIL.ProofOutline/WordArrayArgument::.ctor(Nethermind. System.Void Nethermind.LeanCIL.ProofOutline/WordArrayArgument::.ctor(Nethermind.LeanCIL.ProofOutline/WordArrayArgument) System.Void Nethermind.LeanCIL.RuntimeAsset::.ctor(Nethermind.LeanCIL.RuntimeAsset) System.Void Nethermind.LeanCIL.RuntimeAsset::.ctor(System.String,System.String,System.String,System.String,System.String,System.Boolean,System.String,System.String,System.String[]) -System.Void Nethermind.LeanCIL.Spans/Analysis::.ctor(Nethermind.LeanCIL.Spans/Analysis) +System.Void Nethermind.LeanCIL.StaticDataBlock::.ctor(Nethermind.LeanCIL.StaticDataBlock) +System.Void Nethermind.LeanCIL.StaticDataBlock::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.StaticDataBlock::Deconstruct(System.Int32&) System.Void Nethermind.LeanCIL.StaticIdentityRecord::.ctor(Nethermind.LeanCIL.StaticIdentityRecord) System.Void Nethermind.LeanCIL.StaticIdentityRecord::.ctor(System.String,System.String,System.String) +System.Void Nethermind.LeanCIL.StaticSite::.ctor(Nethermind.LeanCIL.StaticSite) +System.Void Nethermind.LeanCIL.StaticSiteDetail::.ctor() +System.Void Nethermind.LeanCIL.StaticSiteDetail::.ctor(Nethermind.LeanCIL.StaticSiteDetail) System.Void Nethermind.LeanCIL.StaticValues/FirstMethods::.ctor() System.Void Nethermind.LeanCIL.StaticValues/Occurrence::.ctor(Nethermind.LeanCIL.StaticValues/Occurrence) System.Void Nethermind.LeanCIL.StaticValues/Self::.ctor(Nethermind.LeanCIL.StaticValues/Self) @@ -1748,25 +2148,35 @@ System.Void Nethermind.LeanCIL.StaticValues/Value::.ctor(Nethermind.LeanCIL.Stat System.Void Nethermind.LeanCIL.StaticValues/Word::.ctor(Nethermind.LeanCIL.StaticValues/Word) System.Void Nethermind.LeanCIL.StaticValues/Word::.ctor(System.Int32,System.UInt64) System.Void Nethermind.LeanCIL.StaticValues/Word::Deconstruct(System.Int32&,System.UInt64&) +System.Void Nethermind.LeanCIL.StructuralSignatures/Primitive::.ctor(Nethermind.LeanCIL.StructuralSignatures/Primitive) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_EdgeVisits(System.Int64) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_HeapBytes(System.Int64) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_NodeVisits(System.Int64) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_RawRows(System.Int64) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_TrieProbes(System.Int64) +System.Void Nethermind.LeanCIL.Symbols/PdbFile::.ctor(Nethermind.LeanCIL.Symbols/PdbFile) +System.Void Nethermind.LeanCIL.Symbols/PdbFile::.ctor(System.Byte[],System.String) System.Void Nethermind.LeanCIL.TemplateEmitter/Method::.ctor(Nethermind.LeanCIL.TemplateEmitter/Method) -System.Void Nethermind.LeanCIL.TemplateEmitter/Method::.ctor(System.String,System.String,System.String[],System.Boolean) +System.Void Nethermind.LeanCIL.TemplateEmitter/Method::.ctor(System.String,Nethermind.LeanCIL.FrameMetadata,Nethermind.LeanCIL.Feature[],System.Int32[],System.Int32[]) System.Void Nethermind.LeanCIL.TemplateEmitter/Options::.ctor(Nethermind.LeanCIL.TemplateEmitter/Options) System.Void Nethermind.LeanCIL.TemplateEmitter/Options::.ctor(System.String,System.String[],System.String,System.Boolean,System.String,System.String,System.Boolean) System.Void Nethermind.LeanCIL.TemplateEmitter/Row::.ctor(Nethermind.LeanCIL.TemplateEmitter/Row) -System.Void Nethermind.LeanCIL.TemplateEmitter/Row::.ctor(Nethermind.LeanCIL.TemplateEmitter/Variant,Nethermind.LeanCIL.TemplateEmitter/Method,System.String[]) +System.Void Nethermind.LeanCIL.TemplateEmitter/Row::.ctor(Nethermind.LeanCIL.TemplateEmitter/Variant,Nethermind.LeanCIL.FrameMetadata,System.String) System.Void Nethermind.LeanCIL.TemplateEmitter/Template::.ctor(Nethermind.LeanCIL.TemplateEmitter/Template) -System.Void Nethermind.LeanCIL.TemplateEmitter/Template::.ctor(System.String,System.String,Nethermind.LeanCIL.Feature[],System.String[],Nethermind.LeanCIL.TemplateEmitter/Row[]) +System.Void Nethermind.LeanCIL.TemplateEmitter/Template::.ctor(System.String,Nethermind.LeanCIL.FrameMetadata,Nethermind.LeanCIL.Feature[],System.String[],Nethermind.LeanCIL.TemplateEmitter/Row[],System.Int32[]) System.Void Nethermind.LeanCIL.TemplateEmitter/Variant::.ctor(Nethermind.LeanCIL.TemplateEmitter/Variant) System.Void Nethermind.LeanCIL.TemplateEmitter/Variant::.ctor(System.String,System.String,Nethermind.LeanCIL.FeatureProfile,Nethermind.LeanCIL.TemplateEmitter/Method[]) System.Void Nethermind.LeanCIL.Throws/Analysis::.ctor(Nethermind.LeanCIL.Throws/Analysis) +System.Void Nethermind.LeanCIL.TypeLevel::.ctor(Nethermind.LeanCIL.MetadataName,Nethermind.LeanCIL.MetadataName) +System.Void Nethermind.LeanCIL.TypeLevel::.ctor(Nethermind.LeanCIL.TypeLevel) System.Void Nethermind.LeanCIL.TypeTests/Analysis::.ctor(Nethermind.LeanCIL.TypeTests/Analysis) System.Void Nethermind.LeanCIL.TypeTests/Node::.ctor(Nethermind.LeanCIL.TypeTests/Node) System.Void Nethermind.LeanCIL.TypeTests/Node::.ctor(System.String,System.String,System.String,Nethermind.LeanCIL.TypeTests/Node,Nethermind.LeanCIL.TypeTests/Node) +System.Void Nethermind.LeanCIL.UserString::.ctor(Nethermind.LeanCIL.UserString) +System.Void Nethermind.LeanCIL.UserString::.ctor(System.Char[]) System.Void Program/AuditConstantRequest::.ctor(Program/AuditConstantRequest) System.Void Program/AuditNamespaces::.ctor(Program/AuditNamespaces) System.Void Program/AuditNamespaces::.ctor(System.String[],System.String[]) -System.Void Program/AuditRefutation::.ctor(Program/AuditRefutation) -System.Void Program/AuditRefutation::.ctor(System.String,System.Int32,System.Int32) System.Void Program/AuditRequest::.ctor(Program/AuditRequest) System.Void Program/AuditedClosure::.ctor(Program/AuditedClosure) System.Void Program/AuditedClosure::.ctor(System.String,System.String,System.String,System.String) @@ -1775,10 +2185,22 @@ System.Void Program/AuditedInterfaces::.ctor(System.String) System.Void Program/AuditedNamespaces::.ctor(Program/AuditedNamespaces) System.Void Program/BatchManifest::.ctor(Program/BatchManifest) System.Void Program/BatchManifest::.ctor(System.Int32,System.String,Program/BatchRequest[]) -System.Void Program/BatchRequest::.ctor(Program/BatchRequest) -System.Void Program/BatchRequest::.ctor(System.String,System.String,System.String,System.String) System.Void Program/BatchRun::.ctor(Program/BatchRun) System.Void Program/BindingCatalog::.ctor(Program/BindingCatalog) +System.Void Program/BindingFactResult::.ctor(Program/BindingFact,Program/BindingStatus,System.String,System.String) +System.Void Program/BindingFactResult::.ctor(Program/BindingFactResult) +System.Void Program/BindingLibraryPreparation::.ctor(Program/BindingLibraryPreparation) +System.Void Program/BindingPublication::.ctor(Program/BindingPublication) +System.Void Program/BindingPublication::.ctor(System.String,Program/BindingImagePin,System.String,System.String,System.String,Program/BindingData,Program/BindingData) +System.Void Program/BindingRequest::.ctor(Program/BindingRequest) +System.Void Program/BindingRequest::.ctor(System.String,Program/AuditRequest,Program/BindingSubject,System.String) +System.Void Program/BindingSection::.ctor(Program/BindingMode,Program/DigestFile,Program/StatementSources,Program/ImageResult[],Program/BindingFactResult[]) +System.Void Program/BindingSection::.ctor(Program/BindingSection) +System.Void Program/BindingSubject/Fact::.ctor(Program/BindingFact,System.Boolean) +System.Void Program/BindingSubject/Fact::.ctor(Program/BindingSubject/Fact) +System.Void Program/BindingSubject/Fact::Deconstruct(Program/BindingFact&,System.Boolean&) +System.Void Program/BindingSubject::.ctor() +System.Void Program/BindingSubject::.ctor(Program/BindingSubject) System.Void Program/BuildDiagnostic::.ctor(Program/BuildDiagnostic) System.Void Program/BuildDiagnostic::.ctor(System.String,System.Int32,System.Int32,System.String,System.String) System.Void Program/CatalogAssign::.ctor(Program/CatalogAssign) @@ -1804,8 +2226,15 @@ System.Void Program/ConsumerRequest::.ctor(System.String,System.String,System.St System.Void Program/ConsumerSession::.ctor(Program/ConsumerSession) System.Void Program/ConsumerSupportManifest::.ctor(Program/ConsumerSupportManifest) System.Void Program/ConsumerSupportModule::.ctor(Program/ConsumerSupportModule) +System.Void Program/DeepCheckRun::.ctor(DeepCheckInputs) +System.Void Program/DeepCheckRun::set_Attempt(Program/DeepCheckAttempt) +System.Void Program/DeepClosureWork::.ctor(System.Int32,System.Int32,System.Int32,System.Int64) +System.Void Program/DeepClosureWork::Deconstruct(System.Int32&,System.Int32&,System.Int32&,System.Int64&) +System.Void Program/DeepSessionContribution::.ctor(Program/DeepSessionContribution) System.Void Program/DeploymentDependency::.ctor(Program/DeploymentDependency) System.Void Program/DeploymentDependency::.ctor(System.String,System.String,System.String,System.String[],System.Boolean,System.String,System.String) +System.Void Program/DigestFile::.ctor(Program/DigestFile) +System.Void Program/DigestFile::.ctor(System.String,System.String) System.Void Program/EditorProof::.ctor(Program/EditorProof) System.Void Program/EditorProof::.ctor(System.String,System.String) System.Void Program/EditorSelection::.ctor(Program/EditorSelection) @@ -1814,6 +2243,8 @@ System.Void Program/EnvironmentEntry::.ctor(Program/EnvironmentEntry) System.Void Program/EnvironmentEntry::.ctor(System.String,System.String,System.Boolean) System.Void Program/ExportClosure::.ctor(Program/ExportClosure) System.Void Program/ExportClosure::.ctor(System.String,System.String,System.String,System.String) +System.Void Program/ExtractedMethod::.ctor(Program/ExtractedMethod) +System.Void Program/ExtractedMethod::.ctor(System.String,System.String,System.String) System.Void Program/ExtractionComponents::.ctor(Program/ExtractionComponents) System.Void Program/FingerprintInput::.ctor(Program/FingerprintInput) System.Void Program/FullImport::.ctor(Program/FullImport) @@ -1848,6 +2279,8 @@ System.Void Program/GateTemplate::.ctor(Program/GateTemplate) System.Void Program/GateTemplateUse::.ctor(Program/GateTemplateUse) System.Void Program/GeneratedExtraction::.ctor(Program/GeneratedExtraction) System.Void Program/GeneratedExtraction::.ctor(System.String,System.String,System.String,System.String,System.String[],System.String[]) +System.Void Program/ImageResult::.ctor(Program/ImageResult) +System.Void Program/ImageResult::.ctor(System.String,System.String,Program/ImageRole,Program/BindingStatus,System.String,System.String) System.Void Program/JobCount::.ctor() System.Void Program/LibraryAudit::.ctor(Program/LibraryAudit) System.Void Program/LibraryAudit::.ctor(System.String[],System.String[],Program/AuditedNamespaces) @@ -1859,16 +2292,23 @@ System.Void Program/ProofGateEntry::.ctor(Program/ProofGateEntry) System.Void Program/ProofGateEntry::.ctor(System.String,System.String,System.String[],System.String,System.String,System.String,Program/GateBuild,Program/GateVariant[],System.String[],System.String[]) System.Void Program/ProofMessage::.ctor(Program/ProofMessage) System.Void Program/ProofMessage::.ctor(System.String,System.Int32,System.Int32,System.String) +System.Void Program/ReplayRejection::.ctor(Program/ReplayRejection) +System.Void Program/ReplayRejection::.ctor(System.String,Program/ReplayRejectionKind) +System.Void Program/StandaloneCachePromotion::.ctor(Program/StandaloneCachePromotion) System.Void Program/StatementDefinition::.ctor(Program/StatementDefinition) System.Void Program/StatementDefinition::.ctor(System.String,System.String) System.Void Program/StatementRequest::.ctor(Program/StatementRequest) System.Void Program/StatementRequest::.ctor(System.Boolean,System.String,System.Boolean) +System.Void Program/StatementSources::.ctor(Program/StatementSources) +System.Void Program/StatementSources::.ctor(System.String,System.String,System.String) System.Void Program/StatementView::.ctor(Program/StatementView) System.Void Program/StatementView::.ctor(System.String,Program/StatementSubject[],Program/StatementDefinition[]) System.Void Program/TrustedSupport::.ctor(Program/ConsumerSupportManifest,System.String) System.Void Program/TrustedSupport::.ctor(Program/TrustedSupport) System.Void Program/VerificationSettings::.ctor(Program/VerificationSettings) System.Void Program/VerificationSettings::.ctor(System.Int32,System.String,System.String,System.String) +System.Void Program/VerifiedRefutation::.ctor(Program/VerifiedRefutation) +System.Void Program/VerifiedRefutation::.ctor(System.String,Program/ExtractedMethod,System.Int32,System.Int32) System.Void Program/WatchResult::.ctor(Program/WatchResult) System.Void Program/WatchSettings::.ctor(Program/WatchSettings) System.Void Program/WatchSettings::.ctor(System.String,System.String[],System.String,System.Boolean,System.Boolean,System.Boolean) @@ -1879,6 +2319,22 @@ System.Void VerifyReports/ManifestMethod::.ctor(System.String,System.String[],Ve System.Void VerifyReports/ManifestMethod::.ctor(VerifyReports/ManifestMethod) System.Void VerifyReports/SourcePoint::.ctor(System.Int32,System.String,System.Int32,System.Int32) System.Void VerifyReports/SourcePoint::.ctor(VerifyReports/SourcePoint) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) DeepCheckInputs/Pin::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) DeepCheckInputs/Pin::set_Image(DeepCheckInputs/Image) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) DeepCheckInputs/Pin::set_Platform(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) DeepCheckInputs/Pin::set_Reference(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailure::set_digests(FailureReports/BindingFailureDigest) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailure::set_facts(FailureReports/BindingFailureFact[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailure::set_images(FailureReports/BindingFailureImage[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureDigest::set_path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureDigest::set_sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureFact::set_id(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureFact::set_reason(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureFact::set_status(FailureReports/BindingFailureStatus) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureImage::set_identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureImage::set_reason(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureImage::set_sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureImage::set_status(FailureReports/BindingFailureStatus) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Diagnostic::set_file(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Diagnostic::set_kind(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Diagnostic::set_message(System.String) @@ -1887,6 +2343,7 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReport System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Method::set_assembly(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Method::set_assemblySha256(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Method::set_entry(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_binding(FailureReports/BindingFailure) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_diagnostics(FailureReports/Diagnostic[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_file(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_kind(System.String) @@ -1907,10 +2364,25 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanDiagnosti System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanOutput/InternalError::set_Location(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanOutput/InternalError::set_Message(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanOutput/InternalError::set_Module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Compound(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Kind(Nethermind.LeanCIL.NativeOperands/Kind) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Lean(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Signed(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/NumericAccess::set_Shape(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/NumericAccess::set_Signed(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/NumericAccess::set_Store(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/NumericAccess::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName/Columns::set_Culture(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName/Columns::set_Name(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName/KeyColumns::set_Valid(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName::set_Culture(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyVersion::set_Build(System.UInt16) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyVersion::set_Major(System.UInt16) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyVersion::set_Minor(System.UInt16) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyVersion::set_Revision(System.UInt16) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_DecodedMethods(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_Method(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_Profile(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_Reason(System.String) @@ -1924,13 +2396,13 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Access::set_Method(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Access::set_Operation(Nethermind.LeanCIL.ClassViews/FieldOperation) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Access::set_Pc(System.Int32) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Element(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Element(Nethermind.LeanCIL.Arrays/Element) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Kind(Nethermind.LeanCIL.ClassViews/FieldKind) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Loaders(System.Int32[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Name(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Offset(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Storers(System.Int32[]) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Element(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Element(Nethermind.LeanCIL.Arrays/Element) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Kind(Nethermind.LeanCIL.ClassViews/FieldKind) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Offset(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Owner(System.String) @@ -1940,6 +2412,8 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/View::set_FullName(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/View::set_Path(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/View::set_Type(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ComputedLayout::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ComputedLayout::set_Size(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ConstantReturns/Closed::set_Architecture(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractBundleManifest::set_FrameworkKey(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractBundleManifest::set_LeanCilRevision(System.String) @@ -2010,10 +2484,18 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractType::set_Scope(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Declaration::set_Exception(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Declaration::set_Text(System.String) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Site::set_Exception(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Site::set_Exception(Nethermind.LeanCIL.ExceptionType) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Site::set_IlOffset(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Site::set_Method(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Site::set_Origin(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FieldRef::set_Index(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FieldRef::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FieldRef::set_Owner(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FieldRef::set_Type(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FrameMetadata::set_Declared(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FrameMetadata::set_ReturnsValue(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::set_Kind(Nethermind.LeanCIL.GenericEvidence/RefusalKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::set_Pc(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ActualLayouts(Nethermind.LeanCIL.ContractLayout[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_CertificationStatement(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_CertificationStatementDigest(System.String) @@ -2065,18 +2547,67 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolver/Resolved::set_Sidecar(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InitializationAssumption::set_AssemblyIdentity(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InitializationAssumption::set_TypeName(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InitializedStaticValue::set_Construct(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InitializedStaticValue::set_Literal(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InterfaceModule::set_Name(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InterfaceModule::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LayoutField::set_IsAggregate(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LayoutField::set_Offset(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LayoutField::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Append::set_Left(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Append::set_Right(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Group::set_Body(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Item::set_Document(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Item::set_Indent(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Nest::set_Body(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Nest::set_Indent(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Space::set_FoundLine(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Space::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Text::set_Value(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/WorkGroup::set_Flatten(Nethermind.LeanCIL.LeanDoc/Flatten) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Aggregate::set_Size(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Array::set_Element(Nethermind.LeanCIL.Arrays/Element) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/EnumSpan::set_ByReference(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/EnumSpan::set_Element(Nethermind.LeanCIL.Arrays/Element) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/EnumSpan::set_Writable(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/MethodPointer::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/MethodTable::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Narrow::set_Narrowing(Nethermind.LeanCIL.Narrowing) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/NativeMethodTable::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Span::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Span::set_Writable(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Vector::set_Bits(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ManagedTarget::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ManagedTarget::set_Method(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/NamedRow::set_Assembly(System.UInt32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/NamedRow::set_Name(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/NamedRow::set_Namespace(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/NamedRow::set_Parent(System.UInt32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/TypeNameRow::set_Level(Nethermind.LeanCIL.TypeLevel) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/TypeNameRow::set_ResolutionScope(System.UInt32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataValidation/AggregateInfo::set_Alignment(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataValidation/AggregateInfo::set_Size(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataValidation/InstanceField::set_Name(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::set_IsByRefLike(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodBodyBytes/Body::set_Bytes(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodBodyBytes/Body::set_HeaderLength(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_GenericArity(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_HasThis(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_Owner(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_ReturnType(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodSpecialization/Origin::set_Definition(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodSpecialization/Origin::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ParameterHome::set_Argument(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ParameterHome::set_Kind(Nethermind.LeanCIL.LocalKind) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Pin::set_Identity(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Pin::set_Sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Registration::set_Primitives(Nethermind.LeanCIL.StructuralSignatures/Primitive[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Source::set_ExpectedSha256(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Source::set_Path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PlatformDefinition::set_Arity(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PlatformDefinition::set_Kind(Nethermind.LeanCIL.PlatformKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PlatformDefinition::set_Underlying(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProfileInitializationAssumptions::set_Profile(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProfileInitializationAssumptions::set_Types(Nethermind.LeanCIL.InitializationAssumption[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot::set_Bytes(System.Byte[]) @@ -2109,7 +2640,8 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayAccess::set_Array(Nethermind.LeanCIL.ProofOutline/Parameter) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayAccess::set_Helper(Nethermind.LeanCIL.ProofOutline/ElementHelper) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayAccess::set_Index(Nethermind.LeanCIL.ProofOutline/Parameter) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayLength::set_Array(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayAccess::set_Load(Nethermind.LeanCIL.Arrays/NumericAccess) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::set_Array(Nethermind.LeanCIL.ProofOutline/Parameter) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayUse::set_Array(Nethermind.LeanCIL.ProofOutline/HeldArray) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayUse::set_Length(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayUse::set_Stores(System.Int32) @@ -2139,6 +2671,7 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Formed(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Index(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Kind(Nethermind.LeanCIL.ProofOutline/ElementKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_LiteralIndex(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Reread(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Slot(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Stores(System.Int32) @@ -2147,9 +2680,8 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementHelper::set_Index(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementHelper::set_Name(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Architecture(System.String) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Emitted(Nethermind.LeanCIL.TemplateEmitter/Method[]) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_LayoutIdentities(System.String[]) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Emitted(Nethermind.LeanCIL.ProofOutline/Method[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Layouts(Nethermind.LeanCIL.ComputedLayout[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Views(Nethermind.LeanCIL.ClassViews/View[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Element(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Field(Nethermind.LeanCIL.ProofOutline/ViewField) @@ -2164,9 +2696,8 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAccess::set_Stores(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAccess::set_Value(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAccess::set_Width(System.Int32) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAddress::set_Slot(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAddressValue::set_Slot(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalValue::set_Slot(System.Int32) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Number::set_Literal(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Number::set_Narrow(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Number::set_Stacked(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Number::set_Term(System.String) @@ -2177,10 +2708,13 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectFrame::set_Locals(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectTerm::set_Term(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/ArrayArgument::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/ElementIndex::set_Term(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Instance::set_Object(Nethermind.LeanCIL.ProofOutline/Reached) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::set_Formed(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::set_Term(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values::set_Array(Nethermind.LeanCIL.ProofOutline/HeldArray) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::set_Literal(System.Int64) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::set_Numeral(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::set_Term(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::set_Width(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Outline::set_Obligations(System.Int32) @@ -2188,6 +2722,7 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Outline::set_Signature(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Outline::set_Source(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_ArrayElement(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Element(Nethermind.LeanCIL.Arrays/Element) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_ElementDefinition(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Fields(Nethermind.LeanCIL.ProofOutline/ViewField[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Lean(System.String) @@ -2209,9 +2744,9 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ReferenceArgument::set_Name(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Array(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Boolean(System.Boolean) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Constructor(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Lean(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Object(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Width(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Returning::set_Contract(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Returning::set_Kept(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Returning::set_Open(System.Boolean) @@ -2235,10 +2770,10 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Components(Nethermind.LeanCIL.ProofOutline/Component[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Home(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Index(System.Int32) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Kind(Nethermind.LeanCIL.LocalKind) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Layout(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanArgument::set_Span(Nethermind.LeanCIL.ProofOutline/Parameter) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanLength::set_Component(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanLengthValue::set_Component(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_Caseable(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_Checked(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_Head(System.String) @@ -2261,7 +2796,7 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/StubCall::set_Where(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Thrown::set_Where(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Unknown::set_What(System.String) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Element(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Element(Nethermind.LeanCIL.Arrays/Element) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Kind(Nethermind.LeanCIL.ClassViews/FieldKind) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Loaders(System.Int32[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Name(System.String) @@ -2273,6 +2808,7 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Array(Nethermind.LeanCIL.ProofOutline/Parameter) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_CallerIndex(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Index(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Load(Nethermind.LeanCIL.Arrays/NumericAccess) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Name(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Stores(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_AppLocal(System.Boolean) @@ -2284,9 +2820,14 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_PackagePath(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_ReferenceAssets(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_Type(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticDataBlock::set_Alignment(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticIdentityRecord::set_AssemblyIdentity(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticIdentityRecord::set_FieldName(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticIdentityRecord::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticSite::set_Detail(Nethermind.LeanCIL.StaticSiteDetail) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticSite::set_Field(Nethermind.LeanCIL.FieldRef) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticSite::set_Identity(Nethermind.LeanCIL.StaticIdentityRecord) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticSite::set_Pc(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Occurrence::set_Order(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Self::set_Data(System.Byte[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Snapshot::set_Data(System.Byte[]) @@ -2297,10 +2838,18 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Value::set_Opaque(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Word::set_Bits(System.UInt64) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Word::set_Width(System.Int32) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_HasStaticSites(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Primitive::set_Assembly(System.UInt32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Primitive::set_Image(Nethermind.LeanCIL.MetadataImage) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Primitive::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Primitive::set_Token(System.UInt32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Snapshot::set_Prefix(System.Int64[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Symbols/PdbFile::set_Bytes(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Symbols/PdbFile::set_Sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Features(Nethermind.LeanCIL.Feature[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Frame(Nethermind.LeanCIL.FrameMetadata) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Identity(System.String) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Metadata(System.String) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Ops(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_LinkedMethods(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Opcodes(System.Int32[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_Check(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_Extractions(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_ModuleName(System.String) @@ -2308,18 +2857,25 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.Le System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_Namespace(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_OmitEquations(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_Output(System.String) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Row::set_Method(Nethermind.LeanCIL.TemplateEmitter/Method) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Row::set_Ops(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Row::set_ComparisonBody(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Row::set_Frame(Nethermind.LeanCIL.FrameMetadata) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Row::set_Variant(Nethermind.LeanCIL.TemplateEmitter/Variant) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Callees(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Features(Nethermind.LeanCIL.Feature[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Frame(Nethermind.LeanCIL.FrameMetadata) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Identity(System.String) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Metadata(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Opcodes(System.Int32[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Rows(Nethermind.LeanCIL.TemplateEmitter/Row[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Variant::set_AssemblyHash(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Variant::set_Directory(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Variant::set_Methods(Nethermind.LeanCIL.TemplateEmitter/Method[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Variant::set_Profile(Nethermind.LeanCIL.FeatureProfile) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeIdentity::set_Assembly(Nethermind.LeanCIL.AssemblyName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeIdentity::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeIdentity::set_Namespace(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeKind/EnumType::set_Underlying(Nethermind.LeanCIL.PrimitiveType) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeLevel::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeLevel::set_Namespace(Nethermind.LeanCIL.MetadataName) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeTests/Node::set_Kind(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeTests/Node::set_Left(Nethermind.LeanCIL.TypeTests/Node) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeTests/Node::set_Name(System.String) @@ -2332,6 +2888,7 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Audit System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRefutation::set_column(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRefutation::set_contract(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRefutation::set_line(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRefutation::set_program(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRequest::set_allowedModules(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRequest::set_axioms(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRequest::set_name(System.String) @@ -2388,6 +2945,37 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Batch System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingCatalog::set_Gates(Program/CatalogGate[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingCatalog::set_GeneratedRoot(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingCatalog::set_Path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingData::set_Bytes(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingFactResult::set_id(Program/BindingFact) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingFactResult::set_reason(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingFactResult::set_status(Program/BindingStatus) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingFactResult::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingImagePin::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingLibraryPreparation::set_Library(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingLibraryPreparation::set_LibraryKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingLibraryPreparation::set_Toolchain(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPlan::set_Images(Program/BindingImagePin[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPlan::set_Imports(Program/BindingPublication[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPlan::set_Methods(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPlan::set_Output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Closure(Program/BindingData) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Contract(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_InterfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Producer(Program/BindingImagePin) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Sidecar(Program/BindingData) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingRequest::set_Audit(Program/AuditRequest) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingRequest::set_CoveragePredicate(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingRequest::set_Module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingRequest::set_Subject(Program/BindingSubject) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_digests(Program/DigestFile) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_facts(Program/BindingFactResult[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_images(Program/ImageResult[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_mode(Program/BindingMode) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_sources(Program/StatementSources) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSubject/Fact::set_Complete(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSubject/Fact::set_Value(Program/BindingFact) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BuildDiagnostic::set_Code(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BuildDiagnostic::set_Column(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BuildDiagnostic::set_File(System.String) @@ -2455,6 +3043,15 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Consu System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportManifest::set_Tools(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportModule::set_Files(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportModule::set_Imports(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepClosureWork::set_Hashed(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepClosureWork::set_HashedBytes(System.Int64) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepClosureWork::set_Headers(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepClosureWork::set_Launches(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_Modules(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_Restored(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_ResultKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_SourceDirectory(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_Targets(Program/BindingTargets) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_acceptedVia(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_assemblyIdentity(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_conditional(System.Boolean) @@ -2462,6 +3059,8 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Deplo System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_implementationSha256(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_interfaceDigest(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_sourceBundleDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DigestFile::set_path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DigestFile::set_sha256(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorProof::set_Proof(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorProof::set_Theorem(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorSelection::set_Assembly(System.String) @@ -2477,6 +3076,9 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Expor System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExportClosure::set_Contract(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExportClosure::set_Digest(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExportClosure::set_Kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractedMethod::set_assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractedMethod::set_assemblySha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractedMethod::set_signature(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractionComponents::set_Aggregate(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractionComponents::set_InputHash(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractionIdentity::set_Output(System.String) @@ -2611,6 +3213,12 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Gener System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GeneratedExtraction::set_Output(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GeneratedExtraction::set_Profile(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GeneratedExtraction::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_reason(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_role(Program/ImageRole) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_status(Program/BindingStatus) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_theorem(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LibraryAudit::set_Modules(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LibraryAudit::set_Replayed(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LibraryAudit::set_Result(Program/AuditedNamespaces) @@ -2668,10 +3276,13 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Publi System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_slot(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_sourceBundleDigest(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_typeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ReplayRejection::set_Declaration(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ReplayRejection::set_Kind(Program/ReplayRejectionKind) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_assemblySha256(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_axioms(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_batchSourceSha256(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_batchTheorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_binding(Program/BindingSection) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_compositionAudit(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_compositionTheorem(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_consumerSupportSha256(System.String) @@ -2685,19 +3296,28 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Repor System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_libraryKey(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_libraryModules(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_opaqueStaticValues(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_refutation(Program/VerifiedRefutation) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_schema(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_specification(Program/StatementDefinition[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_statementIdentity(Program/StatementIdentity) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_staticValues(System.String[]) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_theorem(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_theoremAudit(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_ExtractionKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_ExtractionSuffix(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_FullLibraryKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_ProofKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_ProofSuffix(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementDefinition::set_name(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementDefinition::set_text(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementIdentity::set_mismatch(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementIdentity::set_termError(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementRequest::set_Expected(System.String) -System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementRequest::set_Negated(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementRequest::set_Refuted(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementRequest::set_Term(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSources::set_lean(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSources::set_leanToolchain(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSources::set_strictBinding(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSubject::set_extracted(System.Boolean) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSubject::set_head(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSubject::set_role(System.String) @@ -2711,6 +3331,10 @@ System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Verif System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerificationSettings::set_proofRoot(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerificationSettings::set_schema(System.Int32) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerificationSettings::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerifiedRefutation::set_column(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerifiedRefutation::set_contract(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerifiedRefutation::set_line(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerifiedRefutation::set_method(Program/ExtractedMethod) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Message(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Method(System.String) System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Module(System.String) diff --git a/Tests/Semantics.lean b/Tests/Semantics.lean index 627a94131..aa544d042 100644 --- a/Tests/Semantics.lean +++ b/Tests/Semantics.lean @@ -2,6 +2,9 @@ import Tests.AggregateSetup import Tests.ValueBitCasts import Tests.ViewReuse import Tests.Support +import Tests.Support.IntrinsicMethods +import Tests.MixedNativeOpcodes +import CIL.Safety.StepComposition import CIL.Safety.ExecutionInvariants import CIL.Safety.NumericLocalLoad @@ -14,13 +17,26 @@ theorem vector_lane_add : some (.v128 (CIL.Vector.pack128 4 6)) := rfl theorem disabled_intrinsic : - (CIL.step (.intrinsic (.avx2 .add64) 2) false 0 [] 0 - [.v256 1, .v256 2] (fun _ => none)).isSome = false := rfl + (CIL.Intrinsic.avx2 .add64).available CIL.FeatureProfile.scalar = false := rfl + +/-- A memory whose every address holds an initialized word, so only the instruction can fail. -/ +private def initializedMemory : CIL.Memory := fun _ => some (.i32 1) + +-- Locals have no meaning in `CIL.step`: only the checked step reads and writes their slots. +theorem local_unstepped : + (CIL.step (.ldloc 0) false none 0 [] initializedMemory).isSome = false := rfl +#print axioms local_unstepped +theorem stloc_unstepped : + (CIL.step (.stloc 0) false none 0 [.i32 1] initializedMemory).isSome = false := rfl +#print axioms stloc_unstepped +theorem ldloca_unstepped : + (CIL.step (.ldloca 0) false none 0 [] initializedMemory).isSome = false := rfl +#print axioms ldloca_unstepped theorem integer_wrap : CIL.binary .add (.i32 4294967295) (.i32 1) = some (.i32 0) := rfl theorem unsigned_compare_wraps : - CIL.binary .ltu (.i64 (BitVec.ofNat 64 (2^64-1))) (.i64 0) = some (.i32 0) := by decide + CIL.binary .clt_un (.i64 (BitVec.ofNat 64 (2^64-1))) (.i64 0) = some (.i32 0) := by decide theorem add_wraps_at_width : CIL.binary .add (.i64 (BitVec.ofNat 64 (2^64-1))) (.i64 1) = some (.i64 0) := by decide @@ -36,25 +52,91 @@ theorem write_overwrites (m : CIL.Memory) (a : CIL.Address) (x y : CIL.Value) : private def scalarBody : CIL.Method := { code := [], locals := [], returnsValue := true } private def scalarFrame : Frame := { activation := 0, locals := [], owned := [] } +/-- A method whose one parameter is declared `System.Byte`. -/ +private def byteParameter : CIL.Method := + { code := [.ldarg 0, .ret], locals := [], returnsValue := true, + declared := { parameters := [.atom CIL.Extraction.runtimeScope "System.Byte"] } } + +/-- `ldarg` narrows an argument as its declared type requires: a Byte keeps its low 8 bits. -/ +theorem ldarg_narrows_declared_byte : + step byteParameter (.ldarg 0) 0 [.scalar (.i32 0x1FF)] scalarFrame [] Memory.empty = + .ok (.next 1 [.scalar (.i32 0xFF)] scalarFrame Memory.empty) := rfl +#print axioms ldarg_narrows_declared_byte + +/-- An argument declared narrow whose vector bits are unspecified faults instead of narrowing. -/ +theorem ldarg_rejects_unspecified_narrow (low : CIL.W64) : + step byteParameter (.ldarg 0) 0 [.scalar (.v128Low64 low)] scalarFrame [] Memory.empty = + .error (.unsupported "Instruction observes unspecified vector bits") := rfl +#print axioms ldarg_rejects_unspecified_narrow + +/-- A method whose one argument lives in a frame home, which the body overwrites with 9. -/ +private def homedParameter : CIL.Method := + { code := [.ldc_i4 9, .stloc 0, .ldarg 0, .ret], locals := [], returnsValue := true, + parameterHomes := [(0, .word32)], + declared := { parameters := [.atom CIL.Extraction.runtimeScope "System.Int32"] } } + +/-- `ldarg` of a homed argument reads the home, not the value the caller passed. -/ +theorem ldarg_reads_home : + (invoke [homedParameter] 8 0 [.scalar (.i32 5)] Memory.empty).map Prod.snd = + .ok [.scalar (.i32 9)] := rfl +#print axioms ldarg_reads_home + +/-- A method whose one argument is homed: it stores 7 to the argument and returns the argument. -/ +private def storedParameter : CIL.Method := + { homedParameter with code := [.ldc_i4 7, .starg 0, .ldarg 0, .ret] } + +/-- `starg` of a homed argument writes the home that `ldarg` reads back. -/ +theorem starg_writes_home : + (invoke [storedParameter] 8 0 [.scalar (.i32 5)] Memory.empty).map Prod.snd = + .ok [.scalar (.i32 7)] := rfl +#print axioms starg_writes_home + +/-- A method that writes 11 through the address of its homed argument, then returns it. -/ +private def addressedParameter : CIL.Method := + { homedParameter with code := [.ldarga 0, .ldc_i4 11, .stind .i4, .ldarg 0, .ret] } + +/-- `ldarga` of a homed argument is the address of the home that `ldarg` reads. -/ +theorem ldarga_addresses_home : + (invoke [addressedParameter] 8 0 [.scalar (.i32 5)] Memory.empty).map Prod.snd = + .ok [.scalar (.i32 11)] := rfl +#print axioms ldarga_addresses_home + +/-- An argument without a home has no slot to write, so a store to it faults. -/ +theorem starg_without_home_unsupported : + step byteParameter (.starg 0) 0 [.scalar (.i32 1)] scalarFrame [.scalar (.i32 3)] + Memory.empty = .error (.unsupported "Store to an argument without a home") := rfl +#print axioms starg_without_home_unsupported + +/-- An argument without a home has no address, so taking one faults. -/ +theorem ldarga_without_home_unsupported : + step byteParameter (.ldarga 0) 0 [.scalar (.i32 1)] scalarFrame [] Memory.empty = + .error (.unsupported "Address of an argument without a home") := rfl +#print axioms ldarga_without_home_unsupported + theorem checked_disabled_intrinsic (body : CIL.Method) (operation : CIL.Intrinsic) (disabled : operation.available body.profile = false) (argc pc : Nat) + (method : CIL.Extraction.MethodRef) (link : body.linkAt pc = none) + (found : CIL.Extraction.runtimeModel body.environment .call method = + some (.intrinsic operation argc)) (args stack : List Value) (frame : Frame) (memory : Memory) : - step body (.intrinsic operation argc) pc args frame stack memory = + step body (.call method) pc args frame stack memory = .error (.unsupported "Intrinsic unavailable in target profile") := by - simp [step, disabled, observerCount, unspecifiedOperands] + rw [step_call_intrinsic _ _ _ _ _ link found rfl] + simp [intrinsicCall, disabled] theorem checked_avx2_enabled : - step { scalarBody with profile := { avx2 := true } } (.intrinsic (.avx2 .add64) 2) + step { scalarBody with profile := { avx2 := true } } (.call Tests.IntrinsicMethods.add64) 0 [] scalarFrame [.scalar (.v256 1), .scalar (.v256 2)] Memory.empty = .ok (.next 1 [.scalar (.v256 3)] scalarFrame Memory.empty) := rfl theorem checked_avx512_disabled : - step scalarBody (.intrinsic (.avx512 .ltu64) 2) 0 [] scalarFrame + step scalarBody (.call Tests.IntrinsicMethods.compareLessThan) 0 [] scalarFrame [.scalar (.v256 1), .scalar (.v256 2)] Memory.empty = .error (.unsupported "Intrinsic unavailable in target profile") := rfl theorem checked_avx512_invocation_disabled (memory : Memory) : - invoke [{ code := [.arg 0, .arg 1, .intrinsic (.avx512 .ltu64) 2, .ret], locals := [], returnsValue := true }] 4 0 + invoke [{ code := [.ldarg 0, .ldarg 1, .call Tests.IntrinsicMethods.compareLessThan, .ret], locals := [], + returnsValue := true }] 4 0 [.scalar (.v256 1), .scalar (.v256 2)] memory = .error ⟨0, 2, .unsupported "Intrinsic unavailable in target profile"⟩ := rfl @@ -68,64 +150,80 @@ theorem neg_underflow : theorem neg_reference_rejected : (step scalarBody .neg 0 [] scalarFrame [Tests.reference] Tests.caller).isOk = false := rfl -theorem gt_mixed_width_rejected : - (step scalarBody .gt 0 [] scalarFrame [.scalar (.i32 0), .scalar (.i64 0)] Memory.empty).isOk = false := rfl +/-- Signed scalar comparison widens mixed native/int32 operands. -/ +theorem cgt_mixed_width : + (step scalarBody .cgt 0 [] scalarFrame [.scalar (.i32 0), .scalar (.i64 0)] + Memory.empty).isOk = true := rfl +#print axioms cgt_mixed_width theorem branches_reject_underflow : - ([CIL.Op.bgt 1, .ble 1, .bleu 1].all fun op => + ([CIL.Instruction.bgt 1, .ble 1, .ble_un 1].all fun op => !(step scalarBody op 0 [] scalarFrame [.scalar (.i32 0)] Memory.empty).isOk) = true := rfl theorem signed_branches_reject_references : - ([CIL.Op.bgt 1, .ble 1].all fun op => + ([CIL.Instruction.bgt 1, .ble 1].all fun op => !(step scalarBody op 0 [] scalarFrame [Tests.reference, Tests.reference] Tests.caller).isOk) = true := rfl #print axioms signed_branches_reject_references -theorem branches_reject_mixed_widths : - ([CIL.Op.bgt 1, .ble 1, .bleu 1].all fun op => +/-- Mixed unsigned comparison branches remain unsupported. -/ +theorem unsigned_branches_reject_mixed_widths : + ([CIL.Instruction.bgt_un 1, .ble_un 1].all fun op => !(step scalarBody op 0 [] scalarFrame [.scalar (.i32 0), .scalar (.i64 0)] Memory.empty).isOk) = true := rfl +#print axioms unsigned_branches_reject_mixed_widths theorem narrow_conversions_reject_references : - ([CIL.Op.convI1, .convI2, .convU2].all fun op => + ([CIL.Instruction.conv_i1, .conv_i2, .conv_u2].all fun op => !(step scalarBody op 0 [] scalarFrame [Tests.reference] Tests.caller).isOk) = true := rfl theorem narrow_read_bounds : loadValue Tests.caller (.address ⟨0, 7⟩) 2 true = .error (.memory .outsideAllocation (.address ⟨0, 7⟩) 2) := rfl +/-- An eight-byte `stind` refuses an int32 value rather than zero-extending it into eight bytes, + and a four-byte `stind` refuses a native value. -/ +theorem stind_value_width_mismatch : + instruction (.stind .i8) [.scalar (.i32 0xFFFFFFFF), Tests.reference] Tests.caller = + .error .invalidState ∧ + instruction (.stind .i) [.scalar (.i32 0xFFFFFFFF), Tests.reference] Tests.caller = + .error .invalidState ∧ + instruction (.stind .i4) [.scalar (.i64 7), Tests.reference] Tests.caller = + .error (.unsupported "Not a memory instruction") := ⟨rfl, rfl, rfl⟩ +#print axioms stind_value_width_mismatch + theorem scalar_load_rejects_snapshot : - (instruction (.loadScalar 2) [.scalar (.aggregate [1, 2])] Tests.caller).isOk = false := rfl + (instruction (.ldind .u2) [.scalar (.aggregate [1, 2])] Tests.caller).isOk = false := rfl theorem scalar_width_rejected : - instruction (.loadScalar 16) [Tests.reference] Tests.caller = + observedTransfer (.scalar 16 false) false [Tests.reference] Tests.caller = .error (.unsupported "Scalar memory width") := rfl theorem scalar_load_null : - instruction (.loadScalar 4) [.reference .null] Tests.caller = + instruction (.ldind .u4) [.reference .null] Tests.caller = .error (.memory .nullDereference .null 4) := rfl theorem scalar_store_null : - instruction (.storeScalar 4) [.scalar (.i32 7), .reference .null] Tests.caller = + instruction (.stind .i4) [.scalar (.i32 7), .reference .null] Tests.caller = .error (.memory .nullDereference .null 0) := rfl theorem scalar_store_wrong_width : - instruction (.storeScalar 2) [.scalar (.i64 7), Tests.reference] Tests.caller = + observedTransfer (.scalar 2 false) true [.scalar (.i64 7), Tests.reference] Tests.caller = .error .invalidState := rfl theorem scalar_store_bounds : - instruction (.storeScalar 4) [.scalar (.i32 7), .reference (.address ⟨0, 6⟩)] Tests.caller = + instruction (.stind .i4) [.scalar (.i32 7), .reference (.address ⟨0, 6⟩)] Tests.caller = .error (.memory .outsideAllocation (.address ⟨0, 6⟩) 4) := rfl theorem scalar_store_lifetime : - instruction (.storeScalar 2) [.scalar (.i32 7), Tests.reference] (expire Tests.caller 0) = + instruction (.stind .i2) [.scalar (.i32 7), Tests.reference] (expire Tests.caller 0) = .error (.memory .expiredLifetime (.address ⟨0, 0⟩) 2) := rfl theorem scalar_store_load {memory : Memory} {reference : Reference} (bits : CIL.W32) (ready : access memory reference 4 1 true = .ok ()) : ∃ result, - instruction (.storeScalar 4) [.scalar (.i32 bits), .reference (.address reference)] memory = + instruction (.stind .i4) [.scalar (.i32 bits), .reference (.address reference)] memory = .ok (result, []) ∧ - instruction (.loadScalar 4) [.reference (.address reference)] result = + instruction (.ldind .u4) [.reference (.address reference)] result = .ok (result, [.scalar (.i32 bits)]) := by obtain ⟨result, stored, loaded⟩ := store_load_numeric_local .word32 (.i32 bits) bits.toNat rfl ready change (do @@ -133,13 +231,14 @@ theorem scalar_store_load {memory : Memory} {reference : Reference} (bits : CIL. pure (Value.scalar (.i32 (BitVec.ofNat 32 (byteNumber bytes)))) : ExecutionResult Value) = _ at loaded refine ⟨result, ?_, ?_⟩ · cases hw : checkedAt (.address reference) 4 (write memory reference (numberBytes bits.toNat 4) 1) <;> - simp_all [instruction, storeLocal, localNumber, referenceAt, localWidth, ordinaryAccessAlignment, + simp_all [instruction, CIL.NumericKind.width, storeLocal, localNumber, referenceAt, + localWidth, ordinaryAccessAlignment, observerCount, unspecifiedOperands, Value.hasUnspecified, CIL.Value.hasUnspecified, Bind.bind, Except.bind, Pure.pure, Except.pure] · cases hr : checkedAt (.address reference) 4 (read result reference 4 1) <;> - simp_all [instruction, loadValue, localResult, dereference, ordinaryAccessAlignment, - observerCount, unspecifiedOperands, Value.hasUnspecified, CIL.Value.hasUnspecified, - Bind.bind, Except.bind, Pure.pure, Except.pure] + simp_all [instruction, CIL.NumericKind.width, CIL.NumericKind.signed, loadValue, localResult, dereference, ordinaryAccessAlignment, + observerCount, unspecifiedOperands, Value.hasUnspecified, Bind.bind, Except.bind, + Pure.pure, Except.pure] theorem switch_unsigned_default : step scalarBody (.switch [10, 20]) 3 [] scalarFrame [.scalar (.i32 4294967295)] Memory.empty = @@ -153,7 +252,7 @@ theorem narrow_read_lifetime : .error (.memory .expiredLifetime (.address ⟨0, 0⟩) 2) := rfl theorem narrow_write_permission : - instruction (.setFieldAt 0 2) [.scalar (.i32 65535), Tests.reference] + storeValueField 0 2 [.scalar (.i32 65535), Tests.reference] { Tests.caller with views := [⟨0, 0, 8, true, false⟩] } = .error (.memory .unwritable (.address ⟨0, 0⟩) 2) := rfl @@ -188,7 +287,14 @@ theorem gc_slot_rejected : ⟨0, 0⟩ 8 1 = .error .unsupportedLayout := rfl theorem unknown_instruction_rejected : - (instruction (.unsupported "test") [] Memory.empty).isOk = false := rfl + (instruction (.unadmitted 0) [] Memory.empty).isOk = false := rfl + +/-- A hole faults on every stack, so no frame, memory or operand makes it a step. -/ +theorem unadmitted_faults (body : CIL.Method) (pc : Nat) (args stack : List Value) + (frame : Frame) (memory : Memory) : + CIL.Safety.step body (.unadmitted 123) pc args frame stack memory = + .error (.unsupported "Opcode not admitted") := rfl +#print axioms unadmitted_faults -- Generic round-trip theorem now covers arbitrary bounded aggregate lengths. theorem aggregate_roundtrip {memory : Memory} {reference : Reference} @@ -233,45 +339,53 @@ def copyMemory : Memory := { def copyStack : List Value := [.reference (.address ⟨0, 0⟩), .reference (.address ⟨1, 0⟩)] theorem copy_uninitialized : - (instruction (.copyObject 8) copyStack { copyMemory with cells := fun _ _ => ⟨0, false⟩ }).map Prod.snd = + (copyInitializedBytes 8 copyStack { copyMemory with + cells := fun _ _ => ⟨0, false⟩ }).map Prod.snd = .error (.memory .uninitialized (.address ⟨0, 0⟩) 8) := rfl theorem copy_unreadable : - (instruction (.copyObject 8) copyStack { copyMemory with views := [⟨1, 0, 12, true, true⟩] }).map Prod.snd = + (copyInitializedBytes 8 copyStack { copyMemory with + views := [⟨1, 0, 12, true, true⟩] }).map Prod.snd = .error (.memory .unreadable (.address ⟨0, 0⟩) 8) := rfl theorem copy_unwritable : - (instruction (.copyObject 8) copyStack { copyMemory with views := [⟨0, 0, 12, true, true⟩] }).map Prod.snd = + (copyInitializedBytes 8 copyStack { copyMemory with + views := [⟨0, 0, 12, true, true⟩] }).map Prod.snd = .error (.memory .unwritable (.address ⟨1, 0⟩) 8) := rfl theorem copy_expired : - (instruction (.copyObject 8) copyStack { copyMemory with + (copyInitializedBytes 8 copyStack { copyMemory with allocations := fun _ => some ⟨.callerStack, ⟨12, 1, []⟩, false, []⟩ }).map Prod.snd = .error (.memory .expiredLifetime (.address ⟨0, 0⟩) 8) := rfl theorem copy_gc_slot : - (instruction (.copyObject 8) copyStack { copyMemory with + (copyInitializedBytes 8 copyStack { copyMemory with allocations := fun _ => some ⟨.callerStack, ⟨12, 1, [(0, 8)]⟩, true, []⟩ }).map Prod.snd = .error (.memory .unsupportedLayout (.address ⟨0, 0⟩) 8) := rfl theorem copy_source_bounds : - (instruction (.copyObject 8) [.reference (.address ⟨0, 12⟩), .reference (.address ⟨1, 0⟩)] copyMemory).map Prod.snd = + (copyInitializedBytes 8 [.reference (.address ⟨0, 12⟩), .reference (.address ⟨1, 0⟩)] + copyMemory).map Prod.snd = .error (.memory .outsideAllocation (.address ⟨0, 12⟩) 8) := rfl theorem copy_destination_bounds : - (instruction (.copyObject 8) [.reference (.address ⟨0, 0⟩), .reference (.address ⟨1, 12⟩)] copyMemory).map Prod.snd = + (copyInitializedBytes 8 [.reference (.address ⟨0, 0⟩), .reference (.address ⟨1, 12⟩)] + copyMemory).map Prod.snd = .error (.memory .outsideAllocation (.address ⟨1, 12⟩) 8) := rfl theorem copy_null_source : - (instruction (.copyObject 8) [.reference .null, .reference (.address ⟨1, 0⟩)] copyMemory).map Prod.snd = + (copyInitializedBytes 8 [.reference .null, .reference (.address ⟨1, 0⟩)] + copyMemory).map Prod.snd = .error (.memory .nullDereference .null 0) := rfl theorem copy_null_destination : - (instruction (.copyObject 8) [.reference (.address ⟨0, 0⟩), .reference .null] copyMemory).map Prod.snd = + (copyInitializedBytes 8 [.reference (.address ⟨0, 0⟩), .reference .null] + copyMemory).map Prod.snd = .error (.memory .nullDereference .null 0) := rfl theorem copy_partial_overlap : - (instruction (.copyObject 8) [.reference (.address ⟨0, 0⟩), .reference (.address ⟨0, 4⟩)] copyMemory).map Prod.snd = + (copyInitializedBytes 8 [.reference (.address ⟨0, 0⟩), .reference (.address ⟨0, 4⟩)] + copyMemory).map Prod.snd = .error (.unsupported "Partially overlapping typed copy") := rfl theorem scalar_helper_wrong_width : @@ -297,148 +411,198 @@ theorem scalar_helper_zero_counts : #print axioms scalar_helper_zero_counts theorem nested_snapshot_bounds : - (instruction (.aggregateFieldAt 3 2) [.scalar (.aggregate [1, 2, 3, 4])] Memory.empty).map Prod.snd = + (loadAggregateField 3 2 [.scalar (.aggregate [1, 2, 3, 4])] Memory.empty).map Prod.snd = .error .invalidState := rfl +-- A nested struct of a snapshot is its slice: a plain value when initialized, else a snapshot. +theorem nested_of_snapshot : + (loadAggregateField 0 2 + [.scalar (.snapshot (.aggregate 4) [(1, true), (2, true), (3, false), (4, true)])] + Memory.empty).map Prod.snd = .ok [.scalar (.aggregate [1, 2])] ∧ + (loadAggregateField 1 2 + [.scalar (.snapshot (.aggregate 4) [(1, true), (2, true), (3, false), (4, true)])] + Memory.empty).map Prod.snd = + .ok [.scalar (.snapshot (.aggregate 2) [(2, true), (3, false)])] ∧ + (loadAggregateField 3 2 + [.scalar (.snapshot (.aggregate 4) [(1, true), (2, true), (3, false), (4, true)])] + Memory.empty).map Prod.snd = .error .invalidState := ⟨rfl, rfl, rfl⟩ + +/-- Storing a nested struct snapshot requires both its declared size and its byte count to + match the field width. -/ +theorem nested_snapshot_store_size : + (storeAggregateField 0 8 [.scalar (.snapshot (.aggregate 8) [(1, false)]), + Tests.reference] Tests.caller).map Prod.snd = .error .invalidState ∧ + (storeAggregateField 0 8 + [.scalar (.snapshot (.aggregate 1) (List.replicate 8 (1, false))), Tests.reference] + Tests.caller).map Prod.snd = .error .invalidState := ⟨rfl, rfl⟩ +#print axioms nested_of_snapshot +#print axioms nested_snapshot_store_size + theorem nested_store_size : - (instruction (.setAggregateFieldAt 0 8) [.scalar (.aggregate [1]), Tests.reference] Tests.caller).map Prod.snd = + (storeAggregateField 0 8 [.scalar (.aggregate [1]), Tests.reference] + Tests.caller).map Prod.snd = .error .invalidState := rfl theorem nested_null : - (instruction (.aggregateFieldAt 0 8) [.reference .null] Memory.empty).map Prod.snd = + (loadAggregateField 0 8 [.reference .null] Memory.empty).map Prod.snd = .error (.memory .nullDereference .null 0) := rfl theorem null_reference_pushed : - instruction (.memory .nullRef) [.scalar (.i32 7)] Memory.empty = + memoryInstruction .nullRef [.scalar (.i32 7)] Memory.empty = .ok (Memory.empty, [.reference .null, .scalar (.i32 7)]) := rfl theorem null_reference_detected : - instruction (.memory .isNullRef) [.reference .null] Memory.empty = + memoryInstruction .isNullRef [.reference .null] Memory.empty = .ok (Memory.empty, [.scalar (.i32 1)]) := rfl theorem live_reference_not_null : - instruction (.memory .isNullRef) [Tests.reference] Tests.caller = + memoryInstruction .isNullRef [Tests.reference] Tests.caller = .ok (Tests.caller, [.scalar (.i32 0)]) := rfl -- Testing performs no access, so an expired reference is still not null. theorem expired_reference_not_null : - instruction (.memory .isNullRef) [Tests.reference] (expire Tests.caller 0) = + memoryInstruction .isNullRef [Tests.reference] (expire Tests.caller 0) = .ok (expire Tests.caller 0, [.scalar (.i32 0)]) := rfl theorem null_test_requires_reference : - instruction (.memory .isNullRef) [.scalar (.i64 0)] Memory.empty = .error .invalidState := rfl + memoryInstruction .isNullRef [.scalar (.i64 0)] Memory.empty = .error .invalidState := rfl -- Every load and store through `NullRef` faults as a null dereference. theorem null_reference_accesses_fault : - instructions [.memory .nullRef, .load64] [] Memory.empty = + (do + let (memory, stack) ← memoryInstruction .nullRef [] Memory.empty + instructions [.ldind .i8] stack memory) = .error (.memory .nullDereference .null 8) ∧ - instructions [.memory .nullRef, .loadScalar 4 true] [] Memory.empty = + (do + let (memory, stack) ← memoryInstruction .nullRef [] Memory.empty + instructions [.ldind .i4] stack memory) = .error (.memory .nullDereference .null 4) ∧ - instructions [.memory .nullRef, .const64 7, .store64] [] Memory.empty = + (do + let (memory, stack) ← memoryInstruction .nullRef [] Memory.empty + instructions [.ldc_i8 7, .stind .i8] stack memory) = .error (.memory .nullDereference .null 0) ∧ - instructions [.memory .nullRef, .const32 7, .storeScalar 4] [] Memory.empty = + (do + let (memory, stack) ← memoryInstruction .nullRef [] Memory.empty + instructions [.ldc_i4 7, .stind .i4] stack memory) = .error (.memory .nullDereference .null 0) ∧ - instructions [.memory .nullRef, .loadAggregate 32] [] Memory.empty = + (do + let (memory, stack) ← memoryInstruction .nullRef [] Memory.empty + transferObject (.aggregate 32) false stack memory) = .error (.memory .nullDereference .null 32) ∧ - instructions [.memory .nullRef, .initAggregate 32] [] Memory.empty = + (do + let (memory, stack) ← memoryInstruction .nullRef [] Memory.empty + initializeBytes 32 stack memory) = .error (.memory .nullDereference .null 0) ∧ - instructions [.memory .nullRef, .memory .load256] [] Memory.empty = + (do + let (memory, stack) ← memoryInstruction .nullRef [] Memory.empty + memoryInstruction .load256 stack memory) = .error (.memory .nullDereference .null 32) ∧ - instructions [.memory .nullRef, .fieldAt 8 8] [] Memory.empty = - .error (.memory .nullDereference .null 0) ∧ - instructions [.memory .nullRef, .fieldAddress 8] [] Memory.empty = - .error (.memory .nullDereference .null 0) ∧ - instructions [.memory .nullRef, .const64 7, .setFieldAt 8 8] [] Memory.empty = - .error (.memory .nullDereference .null 0) ∧ - instructions [.memory .nullRef, .lane 1] [] Memory.empty = + (do + let (memory, stack) ← memoryInstruction .nullRef [] Memory.empty + loadValueField 8 8 false stack memory) = .error (.memory .nullDereference .null 0) ∧ - instructions [.memory .nullRef, .laneAddress 1] [] Memory.empty = + structFieldAddress 8 [.reference .null] Memory.empty = .error (.memory .nullDereference .null 0) ∧ - instructions [.memory .nullRef, .const64 7, .setLane 1] [] Memory.empty = + (do + let (memory, stack) ← memoryInstruction .nullRef [] Memory.empty + let (memory, stack) ← instruction (.ldc_i8 7) stack memory + storeValueField 8 8 stack memory) = .error (.memory .nullDereference .null 0) := - ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ +-- A nested struct copied from uninitialized bytes keeps them uninitialized instead of faulting. theorem nested_uninitialized : - (instruction (.aggregateFieldAt 0 8) [Tests.reference] + (loadAggregateField 0 8 [Tests.reference] { Tests.caller with cells := fun _ _ => ⟨0, false⟩ }).map Prod.snd = - .error (.memory .uninitialized (.address ⟨0, 0⟩) 8) := rfl + .ok [.scalar (.snapshot (.aggregate 8) (List.replicate 8 (0, false)))] := rfl +#print axioms nested_uninitialized theorem nested_bounds : - (instruction (.aggregateFieldAt 4 8) [Tests.reference] Tests.caller).map Prod.snd = + (loadAggregateField 4 8 [Tests.reference] Tests.caller).map Prod.snd = .error (.memory .outsideAllocation (.address ⟨0, 4⟩) 8) := rfl theorem nested_unwritable : - (instruction (.setAggregateFieldAt 0 8) [.scalar (.aggregate (List.replicate 8 0)), Tests.reference] + (storeAggregateField 0 8 [.scalar (.aggregate (List.replicate 8 0)), Tests.reference] { Tests.caller with views := [⟨0, 0, 8, true, false⟩] }).map Prod.snd = .error (.memory .unwritable (.address ⟨0, 0⟩) 8) := rfl theorem nested_expired : - (instruction (.aggregateFieldAt 0 8) [Tests.reference] { Tests.caller with + (loadAggregateField 0 8 [Tests.reference] { Tests.caller with allocations := fun _ => some ⟨.callerStack, ⟨8, 1, []⟩, false, []⟩ }).map Prod.snd = .error (.memory .expiredLifetime (.address ⟨0, 0⟩) 0) := rfl theorem constructor_zero_size : - step { returnsValue := true, locals := [], code := [] } (.newAggregate 1 0 0) - 0 [] ⟨0, [], [], [], 0⟩ [] Memory.empty = .error .invalidState := rfl + step { returnsValue := true, locals := [], code := [.newobj (.arity 0)], + links := [(0, .aggregate 1 0)] } + (.newobj (.arity 0)) 0 [] ⟨0, [], [], 0⟩ [] Memory.empty = .error .invalidState := rfl theorem constructor_stack_underflow : - step { returnsValue := true, locals := [], code := [] } (.newAggregate 1 1 3) - 0 [] ⟨0, [], [], [], 0⟩ [] Memory.empty = .error .invalidState := rfl + step { returnsValue := true, locals := [], code := [.newobj (.arity 1)], + links := [(0, .aggregate 1 3)] } + (.newobj (.arity 1)) 0 [] ⟨0, [], [], 0⟩ [] Memory.empty = .error .invalidState := rfl def constructorCaller : CIL.Method := { - returnsValue := true, locals := [], code := [.newAggregate 1 0 3, .ret] } + returnsValue := true, locals := [], code := [.newobj (.arity 0), .ret], + links := [(0, .aggregate 1 3)] } theorem constructor_zero_initialization : (invoke [constructorCaller, { returnsValue := false, locals := [], code := [.ret] }] 8 0 [] Memory.empty).map Prod.snd = .ok [.scalar (.aggregate [0, 0, 0])] := rfl theorem constructor_must_return_void : - (invoke [constructorCaller, { returnsValue := true, locals := [], code := [.const32 7, .ret] }] + (invoke [constructorCaller, { returnsValue := true, locals := [], code := [.ldc_i4 7, .ret] }] 8 0 [] Memory.empty).map Prod.snd = .error ⟨0, 0, .invalidState⟩ := rfl end Tests namespace Tests -theorem division_mixed_widths_rejected : - CIL.divideValues false false (.i32 6) (.i64 2) = .error .invalidOperands := rfl +/-- Division widens a mixed int32 numerator. -/ +theorem division_mixed_widths : + CIL.divideValues false false (.i32 6) (.i64 2) = .ok (.i64 3) := rfl +#print axioms division_mixed_widths -theorem checked_mixed_widths_rejected : - CIL.checkedValues .add false (.i32 6) (.i64 2) = .error .invalidOperands := rfl +/-- Checked addition widens a mixed int32 operand. -/ +theorem checked_mixed_widths : + CIL.checkedValues .add false (.i32 6) (.i64 2) = .ok (.i64 8) := rfl +#print axioms checked_mixed_widths theorem checked_conversion_bad_width : CIL.convertInteger 7 false true (.i32 1) = .error .invalidOperands := rfl -theorem native_memory_dispatch_rejected : - CIL.nativeBinaryValues .load64 0 (.i64 0) (.i32 0) = .error .invalidOperands := rfl - -theorem native_nested_dispatch_rejected : - CIL.nativeBinaryValues (.nativeBinary .add) 0 (.i64 0) (.i32 0) = .error .invalidOperands := rfl +/-- Memory operations do not widen scalar operands. -/ +theorem native_memory_unchanged : + CIL.widenMixed (.ldind .i8) [.i32 0, .i64 0] = [.i32 0, .i64 0] := rfl +#print axioms native_memory_unchanged -theorem native_reference_operand_rejected : - CIL.nativeBinaryValues .add 0 .nullRef (.i32 0) = .error .invalidOperands := rfl +/-- Reference operands do not qualify for integer widening. -/ +theorem native_reference_unchanged : + CIL.widenMixed .add [.i32 0, .nullRef] = [.i32 0, .nullRef] := rfl +#print axioms native_reference_unchanged +/-- Pure mixed arithmetic sign-extends int32. -/ theorem native_mixed_signed_extension : - CIL.nativeBinaryValues .add 0 (.i64 1) (.i32 4294967295) = .ok (1, [.i64 0]) := rfl + CIL.widenMixed .add [.i32 4294967295, .i64 1] = + [.i64 18446744073709551615, .i64 1] := rfl +#print axioms native_mixed_signed_extension +/-- Unsigned checked arithmetic zero-extends int32. -/ theorem native_checked_unsigned_extension : - CIL.nativeBinaryValues (.checkedArithmetic .add false) 0 (.i64 1) (.i32 4294967295) = - .ok (1, [.i64 4294967296]) := rfl - -theorem native_unsigned_branch_rejected : - CIL.nativeBinaryValues (.bne 9) 0 (.i64 18446744073709551615) (.i32 4294967295) = - .error .invalidOperands := rfl + CIL.checkedValues .add false (.i64 1) (.i32 4294967295) = .ok (.i64 4294967296) := rfl +#print axioms native_checked_unsigned_extension +/-- The reference step refuses a mixed pair for every unsigned branch. -/ theorem native_unsigned_branch_family_rejected : - CIL.nativeBinaryValues (.bltu 9) 0 (.i64 0) (.i32 4294967295) = .error .invalidOperands ∧ - CIL.nativeBinaryValues (.bgtu 9) 0 (.i64 0) (.i32 4294967295) = .error .invalidOperands ∧ - CIL.nativeBinaryValues (.bleu 9) 0 (.i64 0) (.i32 4294967295) = .error .invalidOperands ∧ - CIL.nativeBinaryValues (.bgeu 9) 0 (.i64 0) (.i32 4294967295) = .error .invalidOperands := - ⟨rfl, rfl, rfl, rfl⟩ + ([CIL.Instruction.bne_un 9, .blt_un 9, .bgt_un 9, .ble_un 9, .bge_un 9].all fun op => + (CIL.step op false none 0 [.i32 4294967295, .i64 0] (fun _ => none)).isNone) = true := + rfl +#print axioms native_unsigned_branch_family_rejected +/-- Unsigned scalar comparisons sign-extend int32 before comparing. -/ theorem native_unsigned_compare_extension : - CIL.nativeBinaryValues .gtu 0 (.i64 18446744073709551615) (.i32 4294967295) = - .ok (1, [.i32 0]) := rfl + CIL.step .cgt_un false none 0 [.i32 4294967295, .i64 18446744073709551615] + (fun _ => none) = some (.next 1 [.i32 0] (fun _ => none)) := rfl +#print axioms native_unsigned_compare_extension theorem checked_conversion_reference_rejected : CIL.convertInteger 32 false true .nullRef = .error .invalidOperands := rfl @@ -468,11 +632,11 @@ theorem division_zero (signed remainder : Bool) (x : CIL.W64) : theorem division_underflow : (CIL.Safety.step { returnsValue := true, locals := [], code := [] } - (.divide false false) 0 [] ⟨0, [], [], [], 0⟩ [] Memory.empty).isOk = false := rfl + .div_un 0 [] ⟨0, [], [], 0⟩ [] Memory.empty).isOk = false := rfl theorem division_managed_reference_rejected : (CIL.Safety.step { returnsValue := true, locals := [], code := [] } - (.divide false false) 0 [] ⟨0, [], [], [], 0⟩ + .div_un 0 [] ⟨0, [], [], 0⟩ [.scalar (.i32 2), .reference .null] Memory.empty).isOk = false := rfl @@ -483,17 +647,32 @@ theorem divRem_overflow : CIL.X86.divRem64 0 3 3 = .error .overflow := rfl theorem divRem_zero : CIL.X86.divRem64 7 0 0 = .error .divideByZero := rfl theorem divRem_operands : CIL.X86.divRemValue [.i64 1, .i32 0, .i64 3] = .error .invalidOperands := rfl -def divRemMethod (profile : CIL.FeatureProfile) : CIL.Method := - { profile, returnsValue := true, locals := [], code := [.intrinsic (.x86Base .divRem64) 3] } +private def hardwareDivRem : CIL.Extraction.MethodRef := + let word := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.UInt64" + { owner := .atom CIL.Extraction.intrinsicsScope "System.Runtime.Intrinsics.X86.X86Base/X64", + name := name!"DivRem", genericArity := 0, hasThis := false, + returnType := .aggregate (CIL.Extraction.integerPairIdentity "System.UInt64") 0, + parameters := [word, word, word] } +def divRemMethod (profile : CIL.FeatureProfile) : CIL.Method := + { profile, returnsValue := true, locals := [], code := [.call hardwareDivRem], + environment := { + rawLayouts := [{ + identity := CIL.Extraction.integerPairIdentity "System.UInt64" + explicit := false, auto := true, packing := 0, declaredSize := 0 + fields := [⟨.unsigned64, none⟩, ⟨.unsigned64, none⟩] }] + layouts := #[⟨⟨16, [⟨0, 8, false⟩, ⟨8, 8, false⟩]⟩, 8⟩] } } + +set_option maxRecDepth 4096 in theorem divRem_checked_fault : - CIL.Safety.step (divRemMethod { architecture := .x64, x86Base64 := true }) (.intrinsic (.x86Base .divRem64) 3) 0 [] - ⟨0, [], [], [], 0⟩ [.scalar (.i64 3), .scalar (.i64 3), .scalar (.i64 0)] Memory.empty = + CIL.Safety.step (divRemMethod { architecture := .x64, x86Base64 := true }) (.call hardwareDivRem) 0 [] + ⟨0, [], [], 0⟩ [.scalar (.i64 3), .scalar (.i64 3), .scalar (.i64 0)] Memory.empty = .error (.arithmetic .overflow) := rfl +set_option maxRecDepth 4096 in theorem divRem_requires_x86Base : - CIL.Safety.step (divRemMethod CIL.FeatureProfile.scalar) (.intrinsic (.x86Base .divRem64) 3) 0 [] - ⟨0, [], [], [], 0⟩ [.scalar (.i64 3), .scalar (.i64 0), .scalar (.i64 7)] Memory.empty = + CIL.Safety.step (divRemMethod CIL.FeatureProfile.scalar) (.call hardwareDivRem) 0 [] + ⟨0, [], [], 0⟩ [.scalar (.i64 3), .scalar (.i64 0), .scalar (.i64 7)] Memory.empty = .error (.unsupported "Intrinsic unavailable in target profile") := rfl theorem divRem_pure_fault : diff --git a/Tests/SequentialLayout.cs b/Tests/SequentialLayout.cs index bec4e1367..353a52da6 100644 --- a/Tests/SequentialLayout.cs +++ b/Tests/SequentialLayout.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -14,10 +19,56 @@ var assembly = At("Examples/Assembly/bin/Examples.dll"); var root = At("artifacts/SequentialLayout"); Directory.CreateDirectory(root); +CheckIdentityLiterals(); +if (args.Contains("--identity-literals-only", StringComparer.Ordinal)) return; var queue = new GeneratedQueue(verification, assembly); if (IntPtr.Size != 8) throw new InvalidOperationException("Layout comparisons require a 64-bit runtime"); var cases = 0; +void CheckIdentityLiterals() +{ + var source = new System.Text.StringBuilder("import CIL.Extraction.Layout\nopen CIL\n"); + foreach (int count in new[] { 1, 2 }) + { + var output = Path.Combine(root, $"IdentityLiterals{count}"); + Directory.CreateDirectory(output); + var dll = Path.Combine(output, "LayoutNames.dll"); + using var module = ModuleDefinition.ReadModule(assembly); + module.Assembly.Name.Name = "Layout\U00010400"; + var record = module.GetType("Examples.Record"); + var method = new MethodDefinition("Sizes", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Int32); + module.GetType("Examples.Sequential").Methods.Add(method); + var il = method.Body.GetILProcessor(); + var expected = new List(); + for (int index = 0; index < count; index++) + { + var name = $"Row{char.ConvertFromUtf32(0x10400 + index)}\"\\\u0001"; + var row = new TypeDefinition("Examples", name, record.Attributes, record.BaseType); + module.Types.Add(row); + il.Emit(OpCodes.Sizeof, row); + if (index != 0) il.Emit(OpCodes.Add); + var identity = $"{module.Assembly.Name.FullName}::Examples.{name}"; + expected.Add($"[{string.Join(", ", identity.EnumerateRunes().Select(rune => rune.Value))}]"); + } + il.Emit(OpCodes.Ret); + module.Write(dll); + Run(["dotnet", ExtractorPath, "extract", dll, "Sequential::Sizes()", output]); + var emitted = File.ReadAllText(Path.Combine(output, "Extracted.lean")); + int start = emitted.IndexOf("def rawLayouts :", StringComparison.Ordinal); + int end = emitted.IndexOf("def computedLayouts :", start, StringComparison.Ordinal); + source.AppendLine($$""" + namespace Table{{count}} + {{emitted[start..end]}} + #guard (rawLayouts.map fun row => row.identity.toList.map Char.toNat) == [{{string.Join(", ", expected)}}] + end Table{{count}} + """); + } + var path = Path.Combine(root, "IdentityLiterals.lean"); + File.WriteAllText(path, source.ToString()); + Run(["lake", "env", "lean", path], cwd: FixturePackage()); + Console.WriteLine("PASS single and split layout identities preserve every metadata scalar"); +} + void Prove(string dll, string selector, string name, IEnumerable statements) { var output = Path.Combine(root, name); diff --git a/Tests/ShuffleContract.lean b/Tests/ShuffleContract.lean new file mode 100644 index 000000000..da4b60400 --- /dev/null +++ b/Tests/ShuffleContract.lean @@ -0,0 +1,116 @@ +import CIL.Safety.MethodContract + +/-! Contracts for the compiled SSSE3 and AVX2 byte shuffles. The source and control spans +may start at arbitrary addresses and may overlap. -/ + +namespace Tests.ShuffleContract +open CIL.Safety + +/-- A byte span and the vector of initialized bytes at its start. -/ +structure InputSpan (bitsize : Nat) where + address : Reference + length : Nat + bits : BitVec bitsize + +/-- A readonly span of bytes in caller memory. -/ +def byteSpan (reference : Reference) (length : Nat) : SpanView := + ⟨.address reference, length, 1, false⟩ + +/-- The span contains a readable, initialized vector of the required shape. -/ +def readable {n : Nat} (shape : CIL.SnapshotShape) (pack : BitVec n → CIL.Value) + (span : InputSpan n) (memory : Memory) : Prop := + (byteSpan span.address span.length).validate memory = .ok () ∧ shape.width ≤ span.length ∧ + loadSnapshot memory (.address span.address) shape = .ok (pack span.bits) + +private def shuffleContract {n : Nat} (shape : CIL.SnapshotShape) (pack : BitVec n → CIL.Value) + (permute : BitVec n → BitVec n → BitVec n) : + MethodContract (InputSpan n × InputSpan n) (BitVec n) where + args input := [.bufferSpan (byteSpan input.1.address input.1.length), + .bufferSpan (byteSpan input.2.address input.2.length)] + results value := [.scalar (pack value)] + pre input memory := readable shape pack input.1 memory ∧ readable shape pack input.2 memory + footprint _ := [] + post input before after value := after = before ∧ + value = permute input.1.bits input.2.bits + +/-- AVX2 shuffles the two 128-bit halves independently. -/ +def shuffle := shuffleContract .vector256 .v256 CIL.Vector.shuffleBytes256 + +/-- SSSE3 selects a source byte by the low nibble of each control byte and zeros masked bytes. -/ +def shuffle128 := shuffleContract .vector128 .v128 CIL.Vector.shuffleBytes128 + +/-- Vector construction from a readable span leaves memory and the rest of the stack intact. -/ +theorem read_span (memory : Memory) (s : Reference) (n : Nat) (value : CIL.Value) {shape : CIL.SnapshotShape} + (valid : (byteSpan s n).validate memory = .ok ()) (enough : shape.width ≤ n) + (loaded : loadSnapshot memory (.address s) shape = .ok value) + (rest : List Value) : + memoryInstruction (.marshalRead (.snapshot shape) false) + (.bufferSpan (byteSpan s n) :: rest) + memory = .ok (memory, .scalar value :: rest) := by + unfold byteSpan at valid + simp [memoryInstruction, otherMemoryInstruction, loadTransfer, valid, checkedAt, + byteSpan, CIL.MemoryOp.argumentCount, unspecifiedOperands, Value.hasUnspecified, + CIL.TransferKind.width, Nat.not_lt.mpr enough, loaded] + rfl +#print axioms read_span + +/-- A vector with the same byte at every position of each 128-bit half. -/ +def halves (low high : BitVec 8) : BitVec 256 := + let repeated (byte : BitVec 8) : BitVec 128 := + byte.zeroExtend 128 * 0x01010101010101010101010101010101 + repeated high ++ repeated low + +/-- Source bytes are one in the low half and two in the high half. The low control half masks + every byte and the high control half selects its first source byte. -/ +def witness : InputSpan 256 × InputSpan 256 := + (⟨⟨0, 0⟩, 32, halves 1 2⟩, ⟨⟨1, 0⟩, 32, halves 0x80 0⟩) + +/-- The source and control occupy separate live readonly regions of 32 initialized bytes. -/ +def witnessMemory : Memory := { Memory.empty with + allocations := fun id => if id < 2 then some ⟨.callerStack, ⟨32, 1, []⟩, true, [32]⟩ else none + cells := fun id offset => ⟨if id == 0 then (if offset < 16 then 1 else 2) + else (if offset < 16 then 0x80 else 0), true⟩ + views := [⟨0, 0, 32, true, false⟩, ⟨1, 0, 32, true, false⟩] + nextIdentity := 2 } + +/-- Both spans satisfy the contract's bounds and initialized-read requirements. -/ +theorem witness_ready : shuffle.pre witness witnessMemory := + ⟨⟨rfl, by decide, rfl⟩, ⟨rfl, by decide, rfl⟩⟩ +#print axioms witness_ready + +/-- The wrong vector from a terminating compiled call refutes the contract at this input. -/ +theorem refuted_of_shuffle {program : CIL.Program} {fuel : Nat} {actual : BitVec 256} + (different : actual ≠ halves 0 2) + (ran : invoke program fuel 0 (shuffle.args witness) witnessMemory = + .ok (witnessMemory, [.scalar (.v256 actual)])) : shuffle.Refuted program 0 := by + apply MethodContract.Refuted.of_return (c := shuffle) (input := witness) witness_ready ran + rintro ⟨_, value, encoded, _, rfl⟩ + have expected : CIL.Vector.shuffleBytes256 witness.1.bits witness.2.bits = halves 0 2 := by + decide +kernel + change [Value.scalar (.v256 actual)] = + [Value.scalar (.v256 (CIL.Vector.shuffleBytes256 witness.1.bits witness.2.bits))] at encoded + rw [expected] at encoded + exact different (by simpa using encoded) +#print axioms refuted_of_shuffle + +/-- The 16-byte source contains ones and the control masks every byte. -/ +def witness128 : InputSpan 128 × InputSpan 128 := + (⟨⟨0, 0⟩, 16, 0x01010101010101010101010101010101⟩, + ⟨⟨1, 0⟩, 16, 0x80808080808080808080808080808080⟩) + +/-- Reversing the operands selects masked control bytes as data and returns them unchanged. -/ +theorem refuted_of_reversed128 {program : CIL.Program} {fuel : Nat} + (ran : invoke program fuel 0 (shuffle128.args witness128) witnessMemory = + .ok (witnessMemory, [.scalar (.v128 witness128.2.bits)])) : + shuffle128.Refuted program 0 := by + have ready : shuffle128.pre witness128 witnessMemory := + ⟨⟨rfl, by decide, rfl⟩, ⟨rfl, by decide, rfl⟩⟩ + apply MethodContract.Refuted.of_return (c := shuffle128) (input := witness128) ready ran + rintro ⟨_, value, encoded, _, rfl⟩ + change [Value.scalar (.v128 witness128.2.bits)] = + [Value.scalar (.v128 (CIL.Vector.shuffleBytes128 witness128.1.bits witness128.2.bits))] at encoded + exact (by decide +kernel : witness128.2.bits ≠ + CIL.Vector.shuffleBytes128 witness128.1.bits witness128.2.bits) (by simpa using encoded) +#print axioms refuted_of_reversed128 + +end Tests.ShuffleContract diff --git a/Tests/SignWindowContract.lean b/Tests/SignWindowContract.lean new file mode 100644 index 000000000..92bba9ad5 --- /dev/null +++ b/Tests/SignWindowContract.lean @@ -0,0 +1,69 @@ +import CIL.Safety.MethodContract +import CIL.SIMD.PackedLanes + +/-! The arithmetic sign window selects each output lane from four source limbs or their sign fill. -/ + +namespace Tests.SignWindowContract +open CIL (W64) +open CIL.Vector +open CIL.Safety + +/-- Four quadwords in least-significant-lane order. -/ +structure Limbs where + a : W64 + b : W64 + c : W64 + d : W64 + +/-- The sign extension uses the high limb. Bits 1:0 of an index select a limb + and bit 2 selects the sign extension. Higher index bits have no effect. -/ +def selected (value : Limbs) (index : W64) : W64 := + if index.getLsbD 2 then mask64 (value.d.slt 0) + else lane64 (pack256 value.a value.b value.c value.d) (index.toNat % 4) + +/-- The public entry returns four selected limbs and preserves all caller memory. -/ +def signWindow : MethodContract (Limbs × Limbs) V256 where + args | (v, i) => [.scalar (.i64 v.a), .scalar (.i64 v.b), .scalar (.i64 v.c), + .scalar (.i64 v.d), .scalar (.i64 i.a), .scalar (.i64 i.b), + .scalar (.i64 i.c), .scalar (.i64 i.d)] + results value := [.scalar (.v256 value)] + pre _ memory := memory.WellFormed + footprint _ := [] + post input _ _ result := + let (v, i) := input + result = pack256 (selected v i.a) (selected v i.b) (selected v i.c) (selected v i.d) + +/-- Each of the four lanes of the sign-fill table contains the same word. -/ +private theorem select_fill (v : Limbs) (index : W64) : + let fill := mask64 (v.d.slt 0) + permuteLane4x64x2 (pack256 v.a v.b v.c v.d) (pack256 fill fill fill fill) index = + selected v index := by + simp only [permuteLane4x64x2, selected] + split + · have bound := Nat.mod_lt index.toNat (by decide : 0 < 4) + have cases : index.toNat % 4 = 0 ∨ index.toNat % 4 = 1 ∨ + index.toNat % 4 = 2 ∨ index.toNat % 4 = 3 := by omega + rcases cases with h | h | h | h <;> simp [h] + · rfl +#print axioms select_fill + +/-- The sign broadcast takes lane three of the comparison with zero. -/ +private theorem fill_high (v : Limbs) : + permute4x64 (zip256 (fun x y => mask64 (x.slt y)) (pack256 v.a v.b v.c v.d) 0) 255 = + let fill := mask64 (v.d.slt 0) + pack256 fill fill fill fill := by + change permute4x64 (zip256 _ _ (pack256 0 0 0 0)) _ = _ + rw [zip256_pack256] + simp [permute4x64] +#print axioms fill_high + +/-- The indexed permutation of the computed sign-fill table returns all four selected limbs. -/ +theorem signWindow_result (v i : Limbs) : + let value := pack256 v.a v.b v.c v.d + let fill := permute4x64 (zip256 (fun x y => mask64 (x.slt y)) value 0) 255 + permuteVar4x64x2 value (pack256 i.a i.b i.c i.d) fill = + pack256 (selected v i.a) (selected v i.b) (selected v i.c) (selected v i.d) := by + simp only [fill_high, permuteVar4x64x2, map256_pack256, select_fill] +#print axioms signWindow_result + +end Tests.SignWindowContract diff --git a/Tests/SimdCarryCalls.lean b/Tests/SimdCarryCalls.lean index 95a9ea179..6798d7e15 100644 --- a/Tests/SimdCarryCalls.lean +++ b/Tests/SimdCarryCalls.lean @@ -1,6 +1,8 @@ +import Tests.Support.IntrinsicMethods import CIL.Safety.MethodContract import CIL.Safety.CheckedMemoryFacts import CIL.SIMD.PackedLanes +import Tests.Support /-! # Checked SIMD carry and borrow composition @@ -44,20 +46,20 @@ def cascade (subtract : Bool) (left right : V256) : W32 := (propagate subtract left right + (generate subtract left right <<< 1)) /-- CIL evaluates both by-value operands before applying their lane arithmetic. -/ -@[cil_code] def laneCode (subtract : Bool) : List Op := - [.arg 0, .arg 1, - .intrinsic (if subtract then .vector (.sub64 256) else .avx2 .add64) 2] +@[cil_code] def laneCode (subtract : Bool) : List Instruction := + [.ldarg 0, .ldarg 1, + .call (if subtract then Tests.IntrinsicMethods.subtract else Tests.IntrinsicMethods.add64)] /-- A checked helper computes the packed flag cascade, retaining its outgoing flag bit. -/ @[cil_code] def maskBody (subtract : Bool) : Method := { profile, locals := [], returnsValue := true, code := - (if subtract then [.arg 0, .arg 1] else - laneCode false ++ [.const64 (~~~0), .intrinsic (.vector (.create64 256)) 1]) ++ - [.intrinsic (.avx2 .eq64) 2, .intrinsic (.avx512DQ .moveMask64) 1, .dup] ++ - (if subtract then [.arg 0, .arg 1] else laneCode false ++ [.arg 0]) ++ - [.intrinsic (.avx512 .ltu64) 2, .intrinsic (.avx512DQ .moveMask64) 1, - .const32 1, .shl, .add, .bxor, .ret] } + (if subtract then [.ldarg 0, .ldarg 1] else + laneCode false ++ [.ldc_i8 (~~~0), .call CIL.Extraction.broadcast256Method]) ++ + [.call Tests.IntrinsicMethods.compareEqual, .call Tests.IntrinsicMethods.moveMask, .dup] ++ + (if subtract then [.ldarg 0, .ldarg 1] else laneCode false ++ [.ldarg 0]) ++ + [.call Tests.IntrinsicMethods.compareLessThan, .call Tests.IntrinsicMethods.moveMask, + .ldc_i4 1, .shl, .add, .xor, .ret] } /-- The mask helper returns the exact cascade and leaves every caller byte unchanged. -/ def maskContract (subtract : Bool) : MethodContract (V256 × V256) W32 where @@ -106,17 +108,17 @@ theorem flag_lanes (flags : W32) : #print axioms flag_lanes /-- The CIL scalar extraction uses an unsigned shift, masks one bit and widens it. -/ -@[cil_code] def flagCode (index : W32) : List Op := - [.arg 0, .const32 index, .shrUn, .const32 1, .band, .convU8] +@[cil_code] def flagCode (index : W32) : List Instruction := + [.ldarg 0, .ldc_i4 index, .shr_un, .ldc_i4 1, .and, .conv_u8] /-- The repair helper adds or subtracts all three incoming flags in their own lanes. -/ @[cil_code] def repairBody (subtract : Bool) : Method := { profile, locals := [], returnsValue := true, - code := [.arg 1, .arg 2, - .intrinsic (if subtract then .vector (.sub64 256) else .avx2 .add64) 2, - .const64 0] ++ flagCode 1 ++ flagCode 2 ++ flagCode 3 ++ - [.intrinsic (.vector (.create64 256)) 4, - .intrinsic (if subtract then .vector (.sub64 256) else .avx2 .add64) 2, .ret] } + code := [.ldarg 1, .ldarg 2, + .call (if subtract then Tests.IntrinsicMethods.subtract else Tests.IntrinsicMethods.add64), + .ldc_i8 0] ++ flagCode 1 ++ flagCode 2 ++ flagCode 3 ++ + [.call Tests.IntrinsicMethods.createFour, + .call (if subtract then Tests.IntrinsicMethods.subtract else Tests.IntrinsicMethods.add64), .ret] } /-- The corrected SIMD result before applying the shared whole-vector arithmetic theorem. -/ def repaired (subtract : Bool) (flags : W32) (left right : V256) : V256 := @@ -163,7 +165,8 @@ theorem repaired_subtract (left right : V256) : /-- A checked caller obtains the cascade from one helper and supplies it to the repair helper. -/ @[cil_code] def callerBody : Method := { profile, locals := [], returnsValue := true, - code := [.arg 0, .arg 1, .call 1 2, .arg 0, .arg 1, .call 2 3, .ret] } + code := [.ldarg 0, .ldarg 1, .call (Tests.callee 2) {}, .ldarg 0, .ldarg 1, .call + (Tests.callee 3) {}, .ret], links := [(2, .direct 1), (5, .direct 2)] } /-- The public contract specifies whole-vector wrapping arithmetic and unchanged memory. -/ def arithmeticContract (subtract : Bool) : MethodContract (V256 × V256) V256 where @@ -219,8 +222,8 @@ structure StoredInput where /-- Both reads precede the helper call and the only store, so any input/output overlap is valid. -/ @[cil_code] def storedBody : Method := { profile, locals := [], returnsValue := false, - code := [.arg 0, .arg 1, .memory .load256, .arg 2, .memory .load256, - .call 0 2, .memory .store256, .ret] } + code := [.ldarg 0, .ldarg 1, .ldobj Tests.IntrinsicMethods.vector256UInt64, .ldarg 2, .ldobj Tests.IntrinsicMethods.vector256UInt64, + .call (Tests.callee 2) {}, .stobj Tests.IntrinsicMethods.vector256UInt64, .ret], links := [(5, .direct 0)] } /-- The output becomes the exact arithmetic result, with no separation or initial-output premise. -/ def storedContract (subtract : Bool) : MethodContract StoredInput Unit where @@ -269,16 +272,16 @@ private def carrying : V256 := pack256 (~~~0) (~~~0) 5 0 /-- This incorrect helper shifts generated flags without cascading them through full lanes. -/ @[cil_code] private def singleHopBody (subtract : Bool) : Method := { maskBody subtract with - code := (if subtract then [.arg 0, .arg 1] else laneCode false ++ [.arg 0]) ++ [ - .intrinsic (.avx512 .ltu64) 2, .intrinsic (.avx512DQ .moveMask64) 1, - .const32 1, .shl, .ret] } + code := (if subtract then [.ldarg 0, .ldarg 1] else laneCode false ++ [.ldarg 0]) ++ [ + .call Tests.IntrinsicMethods.compareLessThan, .call Tests.IntrinsicMethods.moveMask, + .ldc_i4 1, .shl, .ret] } /-- This incorrect helper repairs lane two from lane one's incoming flag. -/ @[cil_code] private def wrongLaneBody : Method := { repairBody false with - code := [.arg 1, .arg 2, .intrinsic (.avx2 .add64) 2, .const64 0] ++ + code := [.ldarg 1, .ldarg 2, .call Tests.IntrinsicMethods.add64, .ldc_i8 0] ++ flagCode 1 ++ flagCode 1 ++ flagCode 3 ++ - [.intrinsic (.vector (.create64 256)) 4, .intrinsic (.avx2 .add64) 2, .ret] } + [.call Tests.IntrinsicMethods.createFour, .call Tests.IntrinsicMethods.add64, .ret] } /-- Checked invocation exposes the returned vector independently of the memory representation. -/ private def observedVector (mask repair : Method) (left right : V256) : Option V256 := @@ -354,8 +357,9 @@ private def overlapArguments : List CIL.Safety.Value := /-- This incorrect caller overwrites the first input before taking its snapshot. -/ @[cil_code] private def earlyStoreBody : Method := - { storedBody with code := [.arg 0, .const64 0, .intrinsic (.vector (.create64 256)) 1, - .memory .store256] ++ storedBody.code } + { storedBody.withCode ([.ldarg 0, .ldc_i8 0, .call CIL.Extraction.broadcast256Method, + .stobj Tests.IntrinsicMethods.vector256UInt64] ++ storedBody.code) with + links := storedBody.links.map fun entry => (entry.1 + 4, entry.2) } /-- Observe the output bytes only after the real checked invocation returns. -/ private def observedStore (body : Method) := diff --git a/Tests/SimdLanes.lean b/Tests/SimdLanes.lean index e9b927547..ab69dd6c9 100644 --- a/Tests/SimdLanes.lean +++ b/Tests/SimdLanes.lean @@ -1,3 +1,4 @@ +import Tests.Support.IntrinsicMethods import CIL.Safety.SymbolicExecution import CIL.SIMD.PackedLanes @@ -338,13 +339,6 @@ example (x y : V256) {i : Nat} (lane : i < 4) (value : V256) cases evaluated exact lane64_signedgt256 x y lane -/-- The portable lane multiply evaluates to the wrapping lanewise products. -/ -example (a0 a1 a2 a3 b0 b1 b2 b3 : W64) : - evalIntrinsic (.vector (.mul64 256)) - [.v256 (pack256 a0 a1 a2 a3), .v256 (pack256 b0 b1 b2 b3)] = - some (.v256 (pack256 (a0 * b0) (a1 * b1) (a2 * b2) (a3 * b3))) := by - rw [intrinsic_mul256, zip256_pack256] - /-- AVX-512DQ `MoveMask` of a packed mask vector gathers the four conditions as bits. -/ example (lane0 lane1 lane2 lane3 : Bool) : evalIntrinsic (.avx512DQ .moveMask64) @@ -359,7 +353,7 @@ example (lane0 lane1 lane2 lane3 : Bool) : another: one partial-product row of a multi-limb multiply. -/ @[cil_code] def partialProductMethod : CIL.Method := { profile := { architecture := .x64, avx2 := true } - code := [.intrinsic (.avx2 .shr64) 2, .intrinsic (.avx2 .multiplyEven32) 2, .ret] + code := [.call Tests.IntrinsicMethods.shiftRight, .call Tests.IntrinsicMethods.multiplyEven32, .ret] locals := [], returnsValue := true } /-- The program holding `partialProductMethod`. -/ diff --git a/Tests/SkipInitialization.cs b/Tests/SkipInitialization.cs index 5f52d1539..8d5ed1b22 100644 --- a/Tests/SkipInitialization.cs +++ b/Tests/SkipInitialization.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using Mono.Cecil; diff --git a/Tests/SnapshotWriteScaling.lean b/Tests/SnapshotWriteScaling.lean new file mode 100644 index 000000000..5cf2ec64c --- /dev/null +++ b/Tests/SnapshotWriteScaling.lean @@ -0,0 +1,149 @@ +import CIL.Safety.SymbolicExecution + +/-! Standalone kernel, checked stepping and compiled workloads for snapshot write/readback. -/ + +namespace Tests.SnapshotWriteScaling + +open CIL.Safety + +/-- A writable allocation with room for one leading byte before the copied range. -/ +private def memory (width : Nat) : Memory := { Memory.empty with + allocations := fun id => if id == 0 then + some (.mk .callerStack (.mk (width + 1) 1 []) true []) else none + views := [.mk 0 0 (width + 1) true true] + nextIdentity := 1 } + +/-- The copy starts one byte into allocation zero. -/ +private def target : Reference := { allocation := 0, offset := 1 } + +/-- Each 256-byte block starts one value later. Initialization flags alternate. -/ +private def payload (width : Nat) (seed : Nat := 0) : List (Prod (BitVec 8) Bool) := + (List.range width).map fun index => + (BitVec.ofNat 8 (index + index / 256 + seed), index % 2 == 0) + +/-- Read back every byte and flag after the checked write. -/ +private def roundTrip (width : Nat) (seed : Nat := 0) : Bool := + let bytes := payload width seed + match writeSnapshot (memory width) target bytes 1 with + | .ok stored => + match readSnapshot stored target width 1 with + | .ok copied => copied == bytes + | .error _ => false + | .error _ => false + +/-- Time 256 compiled copies, varying the payload on each iteration. -/ +private def timeCopies (width : Nat) : IO Unit := do + let start <- IO.monoNanosNow + for seed in [:256] do + unless roundTrip width seed do + throw (IO.userError s!"readback failed at width {width}, seed {seed}") + let elapsed := (<- IO.monoNanosNow) - start + IO.println s!"{width} bytes, 256 copies, {elapsed} ns" + +/-- Add one full sweep of the stored cell function to the running byte sum. -/ +@[noinline] private def sumCells (stored : Memory) (width total : Nat) : Nat := Id.run do + let mut sum := total + for index in [:width] do + sum := sum + (stored.cells target.allocation (target.offset + index)).bits.toNat + return sum + +/-- Measure complete sweeps of a successful store for at least 20 ms, in nanoseconds per byte. -/ +private def nanosPerByte (width : Nat) : IO Nat := do + let bytes := payload width + let .ok stored := writeSnapshot (memory width) target bytes 1 + | throw (IO.userError s!"snapshot write failed at width {width}") + let expected := bytes.foldl (fun sum cell => sum + cell.1.toNat) 0 + let mut sum := 0 + let mut sweeps := 0 + let start <- IO.monoNanosNow + repeat + for _ in [:1024 / width] do + sum := sumCells stored width sum + sweeps := sweeps + 1 + let elapsed := (<- IO.monoNanosNow) - start + if elapsed >= 20_000_000 then + unless sum == sweeps * expected do + throw (IO.userError s!"cell sum failed at width {width}") + let count := sweeps * width + let cost := elapsed / count + IO.println s!"{width}-byte store: {elapsed} ns / {count} cell reads = {cost} ns/byte" + return cost + +/-- At least two of three paired samples must stay within twice the small store's cost per byte. -/ +private def checkCompiledCost : IO Unit := do + let mut slow := 0 + for _ in [:3] do + let small <- nanosPerByte 16 + let large <- nanosPerByte 1024 + if large > 2 * small then slow := slow + 1 + if slow >= 2 then + throw (IO.userError s!"compiled cell reads exceeded 2x the 16-byte cost in {slow}/3 samples") + +/-- Both instructions operate on the same stack address and execution frame. -/ +private def Stepped (width : Nat) : Prop := + let type := CIL.Extraction.TypeExpr.aggregate "Fixture::Bytes" 0 + let raw : CIL.Extraction.RawLayout := + { identity := "Fixture::Bytes", explicit := true, packing := 1, declaredSize := width, fields := [] } + let body : CIL.Method := + { code := [.stobj type, .ldobj type], locals := [], returnsValue := false + environment := { rawLayouts := [raw], layouts := #[⟨{ size := width, fields := [] }, 1⟩] } } + let frame : Frame := Frame.mk 0 [] [] 0 + let address := Value.reference (.address target) + let value := Value.scalar (.snapshot (.aggregate width) (payload width)) + let stored := (writeSnapshot (memory width) target (payload width) 1).toOption.getD Memory.empty + And + (step body (.stobj type) 0 [] frame [value, address] (memory width) = + .ok (.next 1 [] frame stored)) + (step body (.ldobj type) 1 [] frame [address] stored = + .ok (.next 2 [value] frame stored)) + +/-- The store and load each discharge a checked instruction goal. -/ +local macro "check_copy" : tactic => `(tactic| + (unfold Stepped + dsimp only + constructor <;> cil_checked_step [])) + +set_option maxRecDepth 65536 +set_option maxHeartbeats 60000 + +/-- Kernel readback of 16 bytes. -/ +theorem kernel16 : roundTrip 16 = true := by decide +kernel +#print axioms kernel16 + +/-- Checked store and load of a 16-byte aggregate. -/ +theorem stepped16 : Stepped 16 := by check_copy +#print axioms stepped16 + +#eval timeCopies 16 + +/-- Kernel readback of 64 bytes. -/ +theorem kernel64 : roundTrip 64 = true := by decide +kernel +#print axioms kernel64 + +/-- Checked store and load of a 64-byte aggregate. -/ +theorem stepped64 : Stepped 64 := by check_copy +#print axioms stepped64 + +#eval timeCopies 64 + +/-- Kernel readback of 256 bytes stays within six million raw heartbeats. -/ +theorem kernel256 : roundTrip 256 = true := by + run_tac + let started <- IO.getNumHeartbeats + Lean.Elab.Tactic.evalTactic (<- `(tactic| decide +kernel)) + let cost := (<- IO.getNumHeartbeats) - started + if cost > 6000000 then + Lean.throwError m!"256-byte kernel readback used {cost} raw heartbeats" +#print axioms kernel256 + +/-- Checked store and load of a 256-byte aggregate. -/ +theorem stepped256 : Stepped 256 := by check_copy +#print axioms stepped256 + +#eval timeCopies 256 + +#eval timeCopies 1024 + +#eval checkCompiledCost + +end Tests.SnapshotWriteScaling diff --git a/Tests/SpanConstructors.cs b/Tests/SpanConstructors.cs index b52100621..7fc633651 100644 --- a/Tests/SpanConstructors.cs +++ b/Tests/SpanConstructors.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs @@ -16,15 +21,24 @@ Directory.CreateDirectory(root); using var metadata = ModuleDefinition.ReadModule(assembly); var queue = new GeneratedQueue(verification, assembly); +// The fixtures whose generated model fact is stated, at the pc of their construction or Empty read. +string[] modelled = ["Single", "ReadOnlySingle", "Aggregate", "Pointer", "AggregateLocal", "Empty", "ReadOnlyEmpty"]; +var modelSites = modelled.ToDictionary(name => name, name => +{ + var code = metadata.GetType("Examples.SpanConstructors").Methods.Single(method => method.Name == name) + .Body.Instructions; + return code.IndexOf(code.Single(instruction => instruction.OpCode.Code is Code.Newobj or Code.Call && + instruction.Operand is MethodReference { Name: ".ctor" or "get_Empty" })); +}, StringComparer.Ordinal); foreach (var method in metadata.GetType("Examples.SpanConstructors").Methods.Where(method => method.IsPublic)) -foreach (var profile in method.Name == "LayoutLocal" ? new[] { "x64-sse2", "arm64-advsimd" } : ["scalar"]) +foreach (var profile in method.Name == "LayoutLocal" ? new[] { "x64-sse42", "arm64-advsimd" } : ["scalar"]) { var name = method.Name; var width = name switch { "Aggregate" or "AggregateLocal" or "Pointer" or "ReadOnlyEmpty" or "ReadOnlyDefaultReset" => 32, "Vector" or "VectorPointer" => 16, - "LayoutLocal" => profile == "x64-sse2" ? 64 : 48, + "LayoutLocal" => profile == "x64-sse42" ? 64 : 48, _ => 4 }; var counted = name.Contains("Pointer", StringComparison.Ordinal) || name == "LayoutLocal"; @@ -54,7 +68,7 @@ /-- No element is addressable through an empty view, regardless of its write capability. -/ theorem emptyIndex : CIL.Safety.memoryInstruction (.spanItem 4) [.scalar (.i32 0), .bufferSpan ⟨.null, 0, 4, true⟩] memory = - .error (.thrown "System.IndexOutOfRangeException") := by rfl + .error (.thrown .indexOutOfRange) := by rfl #print axioms emptyIndex """ : "") + (name == "Single" ? """ /-- View construction rejects expired storage, even when no bytes are read. -/ @@ -80,83 +94,131 @@ /-- A negative pointer length raises the specified runtime exception. -/ theorem negative : CIL.Safety.memoryInstruction (.spanFromReference 32 true) [.scalar (.i32 (-1)), .reference (.address ⟨0, 0⟩)] memory = - .error (.thrown "System.ArgumentOutOfRangeException") := by rfl + .error (.thrown .argumentOutOfRange) := by rfl #print axioms negative """ : "") + "\nend ExampleProof\n"); + if (modelSites.TryGetValue(name, out var modelPc)) + { + var capability = writable ? "true" : "false"; + var form = empty ? "spanEmpty" : counted ? "spanFromReference" : "spanSingleReference"; + File.AppendAllText(proof, StatedModelFact(modelPc, $".memory (.{form} {width} {capability})")); + } queue.Add(new(method.FullName, proof, output, Profile: profile), () => Console.WriteLine("PASS span constructor: " + name + " (" + profile + ")")); } queue.Flush(); var rejected = new RejectedEvidence(Path.Combine(root, "RejectedEvidence")); -foreach (var (name, before, after, theorem, marker) in new[] { - ("Empty", ".spanEmpty 4 true", ".spanEmpty 8 true", - "method0_scalars_validated", "ScalarFlowValid"), - ("ReadOnlyEmpty", ".spanEmpty 32 false", ".spanEmpty 32 true", - "method0_scalars_validated", "ScalarFlowValid"), - ("ReadOnlyEmpty", ".spanEmpty 32 false", ".spanEmpty 24 false", - "method0_layouts_validated", "MethodLayoutsValid"), - ("DefaultReset", ".empty 4 true", ".empty 8 true", - "method0_features_validated", "FeatureFlowValid"), - ("Single", ".spanSingleReference 4 true", ".spanSingleReference 8 true", - "method0_scalars_validated", "ScalarFlowValid"), - ("ReadOnlySingle", ".spanSingleReference 4 false", ".spanSingleReference 4 true", - "method0_scalars_validated", "ScalarFlowValid"), - ("Aggregate", ".spanSingleReference 32 true", ".spanSingleReference 24 true", - "method0_layouts_validated", "MethodLayoutsValid"), - ("Pointer", ".spanFromReference 32 true", ".spanFromReference 16 true", - "method0_layouts_validated", "MethodLayoutsValid"), - ("LayoutLocal-x64-sse2", ".reference 64 true", ".reference 48 true", - "method0_layouts_validated", "MethodLayoutsValid"), - ("AggregateLocal", ".spanSingleReference 32 false", ".spanSingleReference 16 false", - "method0_layouts_validated", "MethodLayoutsValid"), - ("AggregateLocal", ".spanSingleReference 32 false", ".spanSingleReference 32 true", - "method0_scalars_validated", "ScalarFlowValid") + // A reset and an in-place constructor take their width from the home's slot, whose kind the closed + // method's declared locals fix. + const string name = "DefaultReset"; + var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), + "method0_specialization_validated"); + const string kinds = "locals := [.span 4 true, .span 4 true]"; + var changed = source.Replace(kinds, "locals := [.span 8 true, .span 4 true]", StringComparison.Ordinal); + if (changed == source) throw new InvalidOperationException("Missing span local kinds"); + var path = Path.Combine(root, "DefaultResetRejected.lean"); + File.WriteAllText(path, changed); + rejected.Add(path, "PASS span home width rejection", + ClosedArgumentsMismatch); +} +{ + // The checked layouts give an in-place constructor its width, so the site admits only the modelled call. + const string name = "LayoutLocal-x64-sse42"; + var body = metadata.GetType("Examples.SpanConstructors").Methods.Single(method => method.Name == "LayoutLocal") + .Body; + var pc = body.Instructions.IndexOf(body.Instructions.Single(instruction => + instruction.OpCode.Code == Code.Call && instruction.Operand is MethodReference { Name: ".ctor" })); + var source = File.ReadAllText(Path.Combine(root, name, "Extracted.lean")); + var path = Path.Combine(root, "LayoutLocalRejected.lean"); + File.WriteAllText(path, ForgeConstructorOpcode(source, pc)); + rejected.Add(path, "PASS forged in-place constructor refused", "RuntimeModelsAgree", "is false"); +} +// Each forgery changes the width or capability of a generated model fact, so it elaborates exactly when the +// runtime table resolves the site to the forged model. +foreach (var (name, model) in new[] +{ + ("Single", ".memory (.spanSingleReference 8 true)"), ("ReadOnlySingle", ".memory (.spanSingleReference 4 true)"), + ("Aggregate", ".memory (.spanSingleReference 24 true)"), ("Pointer", ".memory (.spanFromReference 16 true)"), + ("AggregateLocal", ".memory (.spanSingleReference 16 false)"), + ("AggregateLocal", ".memory (.spanSingleReference 32 true)"), + ("Empty", ".memory (.spanEmpty 8 true)"), ("ReadOnlyEmpty", ".memory (.spanEmpty 32 true)"), + ("ReadOnlyEmpty", ".memory (.spanEmpty 24 false)") }) { - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), theorem); - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing constructor mutation: " + name); - var path = Path.Combine(root, name + theorem + "Rejected.lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS constructor evidence rejection: " + name, marker, "is false"); + var source = File.ReadAllText(Path.Combine(root, name, "Extracted.lean")); + var path = Path.Combine(root, $"{name}-{model}-rejected.lean"); + File.WriteAllText(path, ForgeModelFact(source, modelSites[name], model)); + rejected.Add(path, $"PASS forged span model fact refused: {name} {model}", ModelFactMismatch(modelSites[name])); +} +{ + // The constructor operand must retain its original opcode. + var source = File.ReadAllText(Path.Combine(root, "Single", "Extracted.lean")); + var path = Path.Combine(root, "Single-substituted-rejected.lean"); + File.WriteAllText(path, ForgeConstructorOpcode(source, modelSites["Single"])); + rejected.Add(path, "PASS substituted constructor opcode refused: Single", "RuntimeModelsAgree", "is false"); } -foreach (var name in new[] { "ReadOnlyEmpty", "ReadOnlyDefaultReset" }) { + // A reset takes its width from its home and its operand from the decoded body, so a forged element identity + // names no operand the body decodes. + const string name = "ReadOnlyDefaultReset"; var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_coverage_validated"); - var changed = Regex.Replace(source, @"\.aggregate ""[^""]+"" 0", ".aggregate \"Wrong.Identity\" 0"); + "method0_scalars_validated"); + var operand = source.Split('\n').Single(line => line.StartsWith("def valueType0 :", StringComparison.Ordinal)); + var changed = source.Replace(operand, + AggregateIdentity.Replace(operand, ".aggregate \"Wrong.Identity\" 0"), StringComparison.Ordinal); if (changed == source) throw new InvalidOperationException("Missing aggregate identity: " + name); var path = Path.Combine(root, name + "IdentityRejected.lean"); File.WriteAllText(path, changed); - rejected.Add(path, "PASS empty span identity rejection: " + name, "LayoutUsesComplete", "is false"); + rejected.Add(path, "PASS empty span identity rejection: " + name, + "cil_decoded_method: the decoded method has no type operand"); } rejected.Check(); -foreach (var mutation in new[] { "reset-scope", "reset-interior" }) +foreach (var mutation in new[] { "reset-scope", "reset-unscoped", "reset-interior" }) { using var altered = ModuleDefinition.ReadModule(assembly); var method = altered.GetType("Examples.SpanConstructors").Methods.Single(method => method.Name == "DefaultReset"); var reset = method.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Initobj); - if (mutation == "reset-scope") + if (mutation is "reset-scope" or "reset-unscoped") { var original = (GenericInstanceType)reset.Operand; - var owner = new TypeReference("System", "Span`1", altered, - new AssemblyNameReference("System.Runtime", new Version(9, 0, 0, 0)), true); + // The forged scope is written as an AssemblyRef row; a null scope needs an ExportedType row the image lacks. + AssemblyNameReference? scope = null; + if (mutation == "reset-scope") + { + scope = new AssemblyNameReference("System.Runtime", new Version(9, 0, 0, 0)); + altered.AssemblyReferences.Add(scope); + } + var owner = new TypeReference("System", "Span`1", altered, scope, true); var changed = new GenericInstanceType(owner); changed.GenericArguments.Add(original.GenericArguments[0]); reset.Operand = changed; } else { - var branch = method.Body.Instructions.Single(instruction => instruction.Operand == reset.Previous); - branch.Operand = reset; + // The branch carries the same home the fall-through path pushes, so only the reset's target check refuses it. + var processor = method.Body.GetILProcessor(); + var address = processor.Create(OpCodes.Ldloca_S, (VariableDefinition)reset.Previous.Operand); + processor.InsertBefore(method.Body.Instructions[0], address); + processor.InsertAfter(address, processor.Create(OpCodes.Br, reset)); } var path = Path.Combine(root, mutation + ".dll"); altered.Write(path); var result = Execute(Extract("extract", path, null, method.FullName, Path.Combine(root, mutation))); - var expected = mutation == "reset-interior" ? "Branch into a folded span receiver" : - "Span default type does not match its local"; + var expected = mutation switch + { + "reset-interior" => "Unsupported span reset", + "reset-unscoped" => "Unresolved aggregate", + _ => "Unsupported type identity" + }; if (result.Code == 0 || !result.Output.Contains(expected, StringComparison.Ordinal)) throw new InvalidOperationException("Wrong empty span rejection: " + result.Output); Console.WriteLine("PASS empty span extraction rejection: " + mutation); } + +partial class Program +{ + [GeneratedRegex(@"\.aggregate ""[^""]+"" 0")] + private static partial Regex AggregateIdentity { get; } +} diff --git a/Tests/SpanCopies.cs b/Tests/SpanCopies.cs index 016faca50..5b62f342a 100644 --- a/Tests/SpanCopies.cs +++ b/Tests/SpanCopies.cs @@ -2,8 +2,14 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs +#:include BulkCallEvidence.cs #:include RejectedEvidence.cs using System.Reflection; @@ -28,7 +34,6 @@ Directory.CreateDirectory(output); var proof = Path.Combine(output, "Proof.lean"); var claims = new List(); - var proofs = new List(); string Run(int source, int length, int target, int capacity, string memory = "memory", bool destinationWritable = true) => "CIL.Safety.invoke Extracted.program 32 0 [" + @@ -44,7 +49,6 @@ void Success(int source, int length, int target, int capacity) claims.Add($"({Run(source, length, target, capacity)}).map (fun result => " + $"(result.2, (CIL.Safety.read result.1 ⟨0, 0⟩ 32 1).toOption)) = " + $".ok ({returned}, some [{string.Join(", ", bytes)}])"); - proofs.Add("by apply result_of_toOption; decide +kernel"); } Success(0, 2, 16, 2); if (name == "TryCopyReadOnly") @@ -57,31 +61,23 @@ void Success(int source, int length, int target, int capacity) claims.Add("(CIL.Safety.invoke Extracted.program 32 0 " + "[.bufferSpan ⟨.null, 0, 1, false⟩, .bufferSpan ⟨.null, 0, 1, true⟩] memory).map Prod.snd = " + ".ok [.scalar (.i32 1)]"); - proofs.Add("by apply result_of_toOption; decide +kernel"); claims.Add($"({Run(2, 5, 20, 3, "uninitialized")}).map Prod.snd = .ok [.scalar (.i32 0)]"); - proofs.Add("by apply result_of_toOption; decide +kernel"); claims.Add($"({Run(2, 5, 20, 5, "uninitialized")}).map Prod.snd = " + ".error ⟨0, 2, .memory .uninitialized (.address ⟨0, 2⟩) 5⟩"); - proofs.Add("by rfl"); claims.Add($"({Run(2, 5, 20, 5, "readOnlyMemory")}).map Prod.snd = " + ".error ⟨0, 2, .memory .unwritable (.address ⟨0, 20⟩) 5⟩"); - proofs.Add("by rfl"); claims.Add($"({Run(2, 5, 20, 5, destinationWritable: false)}).map Prod.snd = " + ".error ⟨0, 2, .invalidState⟩"); - proofs.Add("by rfl"); } if (name == "CopyReadOnly") { claims.Add($"({Run(2, 5, 20, 3, "uninitialized")}).map Prod.snd = " + - ".error ⟨0, 2, .thrown \"System.ArgumentException\"⟩"); - proofs.Add("by rfl"); + ".error ⟨0, 2, .thrown .argument⟩"); } - File.WriteAllText(proof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + """ - /-- Successful observations identify the checked execution result. -/ - private theorem result_of_toOption {error value : Type} {result : Except error value} - {expected : value} (observed : result.toOption = some expected) : result = .ok expected := by - cases result <;> simp_all [Except.toOption] - #print axioms result_of_toOption +File.WriteAllText(proof, $$""" + import Extracted + import CIL.Safety.StepComposition + namespace ExampleProof /-- One allocation exposes overlapping and disjoint slices plus neighboring bytes. -/ def memory : CIL.Safety.Memory := { CIL.Safety.Memory.empty with allocations := fun identity => if identity == 0 then @@ -95,35 +91,16 @@ void Success(int source, int length, int target, int capacity) /-- A writable view value never creates write authority. -/ def readOnlyMemory : CIL.Safety.Memory := { memory with views := [⟨0, 0, 32, true, false⟩] } /-- Copying preserves exact snapshot bytes, surrounding storage, and failure behavior. -/ - """ + "\ntheorem correctness :\n" + string.Join(" ∧\n", claims) + " := by exact " + - (claims.Count == 1 ? proofs[0] : "⟨" + string.Join(", ", proofs) + "⟩") + - "\n#print axioms correctness\nend ExampleProof\n"); + theorem correctness : + {{string.Join(" \u2227\n", claims)}} := by and_intros <;> rfl + #print axioms correctness + end ExampleProof + """); + var mode = trying ? "true" : "false"; + File.AppendAllText(proof, BulkCallEvidence(0, 2, $".spanCopy {width} {mode}")); queue.Add(new(method.FullName, proof, output), () => Console.WriteLine("PASS checked span copy: " + name)); } queue.Flush(); -var rejected = new RejectedEvidence(Path.Combine(root, "RejectedEvidence")); -var index = 0; -foreach (var (name, before, after) in new[] -{ - ("Copy", ".spanCopy 1 false", ".spanCopy 2 false"), - ("TryCopy", ".spanCopy 1 true", ".spanCopy 1 false"), - ("CopyReadOnly", ".spanCopy 1 false", ".spanFill 1 false"), - ("TryCopyReadOnly", "target := .external externalIdentity0", "target := .external \"forged\""), - ("TryCopyEnum", "System.Span\\u00601", "System.ReadOnlySpan\\u00601"), - ("TryCopyEnum", "parameterTypes := [.apply (.constructor Extraction.runtimeScope \"System.Span\\u00601\") " + - "(.enum \"Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null::Examples.CopyCode\"", - "parameterTypes := [.apply (.constructor Extraction.runtimeScope \"System.Span\\u00601\") " + - "(.enum \"Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null::Examples.OtherCopyCode\"") -}) -{ - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_scalars_validated"); - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing copy mutation: " + before); - var path = Path.Combine(root, $"Rejected{index++}.lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS exact span copy evidence rejection: " + name, "ScalarFlowValid"); -} -rejected.Check(); partial class Program { diff --git a/Tests/SpanEvidence.lean b/Tests/SpanEvidence.lean index 27058ca46..75c893ffa 100644 --- a/Tests/SpanEvidence.lean +++ b/Tests/SpanEvidence.lean @@ -1,5 +1,7 @@ import CIL.Extraction.Features -import CIL.Extraction.SpanCalls +import CIL.Extraction.DecodedMethod +import CIL.Safety.StepComposition +import Tests.Support.RawDecoding set_option maxRecDepth 4096 @@ -16,10 +18,20 @@ private def span (scope element : String) : TypeExpr := .apply (.constructor scope "System.ReadOnlySpan`1") (.atom runtime element) private def words := span runtime "System.UInt32" -private def getItem : RawOperand := .call { - declaringType := words, name := "get_Item", - target := .external (runtime ++ "::!0& modreq(System.Runtime.InteropServices.InAttribute) System.ReadOnlySpan`1::get_Item(System.Int32)"), - typeArguments := [], parameterCount := 1, hasThis := true, returnsValue := true } +/-- The readonly indexer at the local receiver. -/ +private def getItem : CallSite := { + declared := { + convention := 0x20, parameters := [intType], + returnType := .modified true runtime "System.Runtime.InteropServices.InAttribute" + (.byref (.ownerParameter 0)) }, + method := { + owner := words, name := name!"get_Item", hasThis := true, genericArity := 0, + returnType := .atom runtime "!0& modreq(System.Runtime.InteropServices.InAttribute)", + parameters := [intType] }, + name := "get_Item", + target := .external (runtime ++ "::!0& modreq(System.Runtime.InteropServices.InAttribute) \ + System.ReadOnlySpan`1::get_Item(System.Int32)"), + typeArguments := [], parameterCount := 1, returnsValue := true } private def i (opcode : Nat) (operand : RawOperand := .none) : RawInstruction := ⟨0, opcode, operand⟩ private def raw (locals : List TypeExpr) (code : List RawInstruction) : RawMethod := { @@ -27,7 +39,7 @@ private def raw (locals : List TypeExpr) (code : List RawInstruction) : RawMetho callingConvention := 0, attributes := 0, implementation := 0, returnType := intType, parameters := [words, intType], locals, initLocals := true, maxStack := 8, genericParameters := [], code } -private def state (stack : List (Option W32)) : Option FeatureState := some ⟨stack, [none]⟩ +private def state (stack : List (Option W32)) : Option FeatureState := some ⟨stack, []⟩ -- Only a primitive runtime span names a span local. example : spanLocal words ∧ spanLocal (span runtime "System.Byte") := by decide @@ -42,181 +54,439 @@ theorem primitive_span_widths : spanStorage (span runtime "System.Boolean") = some (.span 1 false) := by decide #print axioms primitive_span_widths -/-- A closed int32 array extension call with its exact defining assembly. -/ -private def arraySpanCall : CallSite := { - declaringType := .atom spanMemoryScope "System.MemoryExtensions" - name := "AsSpan" - target := .external (spanMemoryScope ++ - "::System.Span`1 System.MemoryExtensions::AsSpan(!!0[],System.Int32)") - typeArguments := [intType], parameterCount := 2, hasThis := false, returnsValue := true - returnType := .apply (.constructor runtime "System.Span`1") intType - parameterTypes := [.array intType, intType] } - -/-- Array conversion certificates bind element identity, range arity and all call metadata. -/ +/-- Each range has the MemberRef declaration of the corresponding array overload. -/ +private def arraySpanCall (range : ArraySpanRange) (element : TypeExpr) : CallSite := + let bounds := match range with + | .whole => [] | .fromStart => [intType] | .counted => [intType, intType] + let targetParams := match range with + | .whole => "!!0[]" | .fromStart => "!!0[],System.Int32" + | .counted => "!!0[],System.Int32,System.Int32" + let view := TypeExpr.apply (.constructor runtime "System.Span`1") + { declared := ⟨0x10, view (.parameter 0), .array (.parameter 0) :: bounds⟩ + method := { + owner := .atom spanMemoryScope "System.MemoryExtensions", name := name!"AsSpan", + hasThis := false, genericArity := 1, returnType := view element, + parameters := .array element :: bounds, methodArguments := [element] } + name := "AsSpan" + target := .external (spanMemoryScope ++ + s!"::System.Span`1 System.MemoryExtensions::AsSpan({targetParams})") + typeArguments := [element], parameterCount := 1 + bounds.length, returnsValue := true } + +/-- Decode a single live call under the production model table. -/ +private def decodeArrayCall (site : CallSite) (layouts : LayoutEnvironment := {}) := + (decodeRaw .scalar layouts [] [true] (raw [] [i 40 (.call site)]) []).map Method.code + +/-- Each range retains its call and rejects a mismatched raw or closed field. -/ theorem exact_array_span_call : - spanCallMatches (i 40 (.call arraySpanCall)) - (.memory (.arraySpan .signed32 true .fromStart)) = true ∧ - spanCallMatches (i 40 (.call arraySpanCall)) - (.memory (.arraySpan .unsigned32 true .fromStart)) = false ∧ - spanCallMatches (i 40 (.call arraySpanCall)) - (.memory (.arraySpan .signed32 false .fromStart)) = false ∧ - spanCallMatches (i 40 (.call arraySpanCall)) - (.memory (.arraySpan .signed32 true .whole)) = false ∧ - spanCallMatches (i 115 (.call arraySpanCall)) - (.memory (.arraySpan .signed32 true .fromStart)) = false ∧ - spanCallMatches (i 40 (.call { arraySpanCall with hasThis := true })) - (.memory (.arraySpan .signed32 true .fromStart)) = false ∧ - spanCallMatches (i 40 (.call { arraySpanCall with - declaringType := .atom runtime "System.MemoryExtensions" })) - (.memory (.arraySpan .signed32 true .fromStart)) = false ∧ - spanCallMatches (i 40 (.call { arraySpanCall with parameterTypes := [intType, intType] })) - (.memory (.arraySpan .signed32 true .fromStart)) = false := by decide + [ArraySpanRange.whole, .fromStart, .counted].all (fun range => + let site := arraySpanCall range intType + let method := site.method + let wrongOpen := site.declared.parameters.set 0 (.array (.ownerParameter 0)) + let refused := [ { site with method.owner := .atom runtime "System.MemoryExtensions" }, + { site with name := "Another" }, { site with method.name := name!"Another" }, + { site with method.genericArity := 2 }, { site with method.hasThis := true }, + { site with method.parameters := method.parameters.set 0 intType }, + { site with method.returnType := .void }, { site with declared.convention := 0x20 }, + { site with declared.parameters := wrongOpen }, { site with target := .external "Another" }, + arraySpanCall range (.atom runtime "System.Double"), + arraySpanCall range (.aggregate "Missing::Layout" 0) ] + decide (decodeArrayCall site = .ok [.call method {}]) && + (runtimeModel {} .call method == some (.memory (.arraySpan .signed32 true range))) && + !runtimeCatalog.siteAgrees {} .scalar (i 40 (.call site)) + (.call method { tail := true }) none true && + refused.all (fun other => decide (decodeArrayCall other = liveHoleAt 0 40))) := by + decide +kernel #print axioms exact_array_span_call --- 0: ldarg.0; 1: stloc.0; 2: ldloca.s 0; 3: ldarg.1; 4: get_Item; 5: ldind.u4; 6: ret -private def itemCode := [i 2, i 10, i 18 (.local 0), i 3, i 40 getItem, i 74, i 42] +/-- Aggregate strides come from each environment, and byref-like elements cannot form arrays. -/ +theorem arraySpanLayouts : + let element := TypeExpr.aggregate "Fixture::Words" 0 + let env := fun count byRefLike => + let rows : List RawLayout := [{ + identity := "Fixture::Words", explicit := false, + packing := 0, declaredSize := 0, byRefLike, + fields := List.replicate count ⟨.unsigned32, none⟩ }] + ({ rawLayouts := rows, layouts := (computeLayouts rows).getD #[] } : LayoutEnvironment) + [ArraySpanRange.whole, .fromStart, .counted].all (fun range => + let site := arraySpanCall range element + decide (decodeArrayCall site (env 1 true) = liveHoleAt 0 40) && + [1, 3].all (fun count => + let storage := ArrayElement.aggregate "Fixture::Words" (count * 4) + runtimeModel (env count false) .call site.method == + some (.memory (.arraySpan storage true range)))) := by + decide +kernel +#print axioms arraySpanLayouts + +/-- A kept array factory has the same result and faults as its checked memory operation. -/ +theorem arraySpanStep (body : Method) (pc : Nat) (method : MethodRef) (range : ArraySpanRange) + (element : ArrayElement) (args stack : List Safety.Value) (frame : Safety.Frame) + (memory : Safety.Memory) (link : body.linkAt pc = none) + (found : runtimeModel body.environment .call method = some (.memory (.arraySpan element true + range))) : + Safety.step body (.call method {}) pc args frame stack memory = + Safety.modelStep (some (.memory (.arraySpan element true range))) body pc frame stack memory := + Safety.step_call_model body pc args frame memory link found rfl stack +#print axioms arraySpanStep + +/-- Each fixture pairs a constructor declaration with the operation it must execute. -/ +private def constructors (writable : Bool) (element : TypeExpr) (name : String) : + List (CallSite × MemoryOp) := Id.run do + let owner := if writable then "System.Span`1" else "System.ReadOnlySpan`1" + let array := fun type => [TypeExpr.array type] + let range := fun type => [TypeExpr.array type, intType, intType] + let pointer := fun (_ : TypeExpr) => [TypeExpr.pointer .void, intType] + let reference := fun type => [TypeExpr.byref type] + let forms : List ((TypeExpr → List TypeExpr) × String × MemoryOp) := + [(array, "!0[]", .arraySpan .signed32 writable .whole), + (range, "!0[],System.Int32,System.Int32", .arraySpan .signed32 writable .counted), + (pointer, "System.Void*,System.Int32", .spanFromReference 4 writable), + (reference, "!0&", .spanSingleReference 4 writable)] + return forms.map fun (parameters, target, operation) => + let method : MethodRef := { + owner := .apply (.constructor runtime owner) element, name := name!".ctor", hasThis := true, + genericArity := 0, returnType := .void, parameters := parameters element } + let site : CallSite := { + declared := ⟨0x20, .void, parameters (.ownerParameter 0)⟩, method, name := ".ctor" + target := .external s!"{runtime}::System.Void {owner}<{name}>::.ctor({target})" + typeArguments := [], parameterCount := (parameters element).length, returnsValue := false } + (site, operation) + +/-- The production decoder keeps a resolving constructor in the method body. -/ +private def decodeConstructor (site : CallSite) (layouts : LayoutEnvironment := {}) := + (decodeRaw .scalar layouts [] [true] (raw [] [i 115 (.call site)]) []).map Method.code + +/-- A refused constructor leaves a newobj hole and prevents admission of the live method. -/ +private def refusesConstructor (site : CallSite) (layouts : LayoutEnvironment := {}) : Bool := + let source := raw [] [i 115 (.call site)] + methodCode .scalar layouts [] [true] source == [.unadmitted 115] && + decide (decodeConstructor site layouts = liveHoleAt 0 115) + +/-- Both owners bind the eight declarations, the exact external target and the closed arguments. -/ +theorem constructor_models : + [true, false].all (fun writable => + (constructors writable intType "System.Int32").all (fun (site, op) => + let method := site.method + let otherHead := TypeExpr.constructor owner + (if writable then "System.Span`1" else "System.ReadOnlySpan`1") + let refused := [{ site with method.owner := .apply otherHead intType }, + { site with name := "Other" }, { site with method.name := name!"Other" }, + { site with method.genericArity := 1 }, { site with method.hasThis := false }, + { site with parameterCount := 0 }, { site with returnsValue := true }, + { site with method.returnType := intType }, { site with method.parameters := [intType] }, + { site with declared.parameters := [] }, { site with declared.convention := 0 }, + { site with target := .external "Other" }] + decide (decodeConstructor site = .ok [.newobj method]) && + (runtimeModel {} .newobj method == some (.memory op)) && + (method.parameters.length == op.argumentCount) && + !runtimeCatalog.siteAgrees {} .scalar (i 115 (.call site)) (.call method) none true && + refused.all (fun other => refusesConstructor other)) && + [(TypeExpr.atom runtime "System.Double", "System.Double"), + (.aggregate "Missing::Layout" 0, "Layout")].all (fun (element, name) => + (constructors writable element name).all (fun (site, _) => + refusesConstructor site))) := by + decide +kernel +#print axioms constructor_models + +/-- Both array overloads reject a byref-like element even when its aggregate layout has a size. -/ +theorem constructor_array_storage : + let element := TypeExpr.aggregate "Fixture::Words" 0 + let rows : List RawLayout := [{ + identity := "Fixture::Words", explicit := false, packing := 0, declaredSize := 0, + byRefLike := true, fields := [⟨.unsigned32, none⟩] }] + let env : LayoutEnvironment := { rawLayouts := rows, layouts := (computeLayouts rows).getD #[] } + [true, false].all (fun writable => + ((constructors writable element "Words").take 2).all (fun (site, _) => + refusesConstructor site env)) := by + decide +kernel +#print axioms constructor_array_storage + +/-- The readonly byte pointer overload uses the checked byte-span construction. -/ +theorem byte_pointer_constructor : + ((constructors false (.atom runtime "System.Byte") "System.Byte")[2]?).any (fun (site, _) => + runtimeModel {} .newobj site.method == some (.memory .spanCreate)) := by + decide +kernel +#print axioms byte_pointer_constructor + +-- 0: ldarg.0; 1: stloc.0; 2: ldloca.s 0; 3: ldarg.1; 4: get_Item; 5: ldind.i4; 6: ret +private def itemCode := [i 2, i 10, i 18 (.local 0), i 3, i 40 (.call getItem), i 74, i 42] private def itemStates : FeatureCertificate := [state [], state [none], state [], state [none], state [none, none], state [none], state [none]] -private def itemOps : List Op := - [.arg 0, .nop, .nop, .arg 1, .memory (.spanItem 4), .loadScalar 4, .ret none] - -example : foldRoles [words] itemCode = [none, some .head, some .interior, none, none, none, none] := by decide -example : foldRoles [intType] itemCode = List.replicate 7 none := by decide -example : foldRoles [words] [i 10, i 18 (.local 1)] = [none, none] := by decide - -/-- A third decoded local use disallows receiver folding anywhere in the body. -/ -theorem extra_local_use : foldRoles [words] (itemCode ++ [i 6]) = - List.replicate 8 none := by decide +kernel -#print axioms extra_local_use - -/-- A malformed local-load operand does not count as a decoded use of local zero. -/ -theorem malformed_local_operand : - foldRoles [words] (itemCode ++ [i 65036 (.argument 0)]) = - [none, some .head, some .interior, none, none, none, none, none] := by decide +kernel -#print axioms malformed_local_operand --- Only the store may be entered from elsewhere. -example : foldEntry (foldRoles [words] itemCode)[1]? ∧ - ¬ foldEntry (foldRoles [words] itemCode)[2]? := by decide - -example : FeatureFlowValid #[] (fun _ => none) (raw [words] itemCode) .scalar itemStates +private def itemOps : List Instruction := + [.ldarg 0, .stloc 0, .ldloca 0, .ldarg 1, .call getItem.method {}, .ldind .i4, .ret] + +/-- Storing and addressing a span receiver leaves every instruction outside a fold. -/ +theorem length_receiver_roles : + foldRoles [i 10, i 18 (.local 0), i 40 (.call { + declared := { convention := 0x20, returnType := intType }, + method := { + owner := words, name := name!"get_Length", hasThis := true, genericArity := 0, + returnType := intType, parameters := [] }, + name := "get_Length", + target := .external (runtime ++ + "::System.Int32 System.ReadOnlySpan`1::get_Length()"), + typeArguments := [], parameterCount := 0, returnsValue := true })] = + [none, none, none] := by decide +kernel +#print axioms length_receiver_roles + +/-- Indexed span receivers retain every raw instruction outside a fold. -/ +theorem item_receiver_roles : foldRoles itemCode = List.replicate 7 none := by + decide +kernel +#print axioms item_receiver_roles + +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [words] itemCode) .scalar + itemStates (List.replicate 7 true) itemOps := by decide --- The receiver must stay on the stack: a real store or address is not the fold. -example : ¬ FeatureFlowValid #[] (fun _ => none) (raw [words] itemCode) .scalar itemStates +-- Erasing either receiver instruction fails its raw opcode check. +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [words] itemCode) .scalar + itemStates (List.replicate 7 true) - (itemOps.set 1 (.setLocal 0 none)) := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) (raw [words] itemCode) .scalar itemStates + (itemOps.set 1 .nop) := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [words] itemCode) .scalar + itemStates (List.replicate 7 true) - (itemOps.set 2 (.localAddr 0)) := by decide + (itemOps.set 2 .nop) := by decide --- Span locals outside a receiver fold retain ordinary load/store flow. +-- Span locals retain ordinary load/store flow. private def loadCode := [i 2, i 10, i 6, i 42] -private def loadOps : List Op := [.arg 0, .setLocal 0 none, .local 0 none, .ret none] +private def loadOps : List Instruction := [.ldarg 0, .stloc 0, .ldloc 0, .ret] private def loadStates : FeatureCertificate := [state [], state [none], state [], state [none]] -example : FeatureFlowValid #[] (fun _ => none) (raw [intType] loadCode) .scalar loadStates +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [intType] loadCode) .scalar + loadStates (List.replicate 4 true) loadOps := by decide -example : FeatureFlowValid #[] (fun _ => none) (raw [words] loadCode) .scalar loadStates +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [words] loadCode) .scalar + loadStates (List.replicate 4 true) loadOps := by decide -- `ldtoken` of a field is static data; only a type handle belongs to a type test. -example : ¬ typeTestInstruction (i 208 (.field (.atom owner "") 0 intType true)) ∧ +example : ¬ typeTestInstruction + (i 208 (.field (.atom owner "") 0 intType name!"Data" true)) ∧ typeTestInstruction (i 208 (.type intType)) := by decide -/-- The exact writable int32 span member used by whole-region initialization. -/ -private def fillCall : CallSite := { - declaringType := .apply (.constructor runtime "System.Span`1") intType - name := "Fill" - target := .external (runtime ++ "::System.Void System.Span`1::Fill(!0)") - typeArguments := [], parameterCount := 1, hasThis := true, returnsValue := false - returnType := .void, parameterTypes := [intType] } - -/-- Whole-span stores bind the operation, element width, mutability and complete signature. -/ -theorem exact_span_fill_call : - spanCallMatches (i 40 (.call fillCall)) (.memory (.spanFill 4 false)) = true ∧ - spanCallMatches (i 40 (.call fillCall)) (.memory (.spanFill 8 false)) = false ∧ - spanCallMatches (i 40 (.call fillCall)) (.memory (.spanFill 4 true)) = false ∧ - spanCallMatches (i 40 (.call fillCall)) (.memory (.spanSlice false)) = false ∧ - spanCallMatches (i 40 (.call { fillCall with - declaringType := .apply (.constructor runtime "System.ReadOnlySpan`1") intType })) - (.memory (.spanFill 4 false)) = false ∧ - spanCallMatches (i 40 (.call { fillCall with - parameterTypes := [.atom runtime "System.UInt32"] })) - (.memory (.spanFill 4 false)) = false ∧ - spanCallMatches (i 40 (.call { fillCall with hasThis := false })) - (.memory (.spanFill 4 false)) = false ∧ - spanCallMatches (i 40 (.call { fillCall with returnType := intType, returnsValue := true })) - (.memory (.spanFill 4 false)) = false := by decide +kernel -#print axioms exact_span_fill_call - -/-- Exact array constructor used by an in-place local initialization. -/ +/-- The eight bulk declarations instantiated at the named element type. -/ +private def bulkSites (element : TypeExpr) (elementName : String) : List CallSite := Id.run do + let writable := spanHead true + let boolean := TypeExpr.atom runtime "System.Boolean" + let var := TypeExpr.ownerParameter 0 + let row (owner : TypeExpr) (ownerName name : String) (key : MetadataName) + (result : TypeExpr) (resultName params : String) (closed openParams : List TypeExpr) + (array := false) : CallSite := + let args := if array then [element] else [] + { method := ⟨owner, key, !array, if array then 1 else 0, result, closed, args⟩ + declared := ⟨if array then 0x10 else 0x20, result, openParams⟩ + name, typeArguments := args, parameterCount := closed.length, + returnsValue := result != .void + target := .external s!"{runtime}::{resultName} {ownerName}::{name}({params})" } + let owner := TypeExpr.apply writable element + let spelling := s!"System.Span`1<{elementName}>" + let copies := [true, false].flatMap fun writableSource => + let source := TypeExpr.apply (spanHead writableSource) element + let head := if writableSource then "System.Span`1" else "System.ReadOnlySpan`1" + let sourceName := s!"{head}<{elementName}>" + [row source sourceName "CopyTo" name!"CopyTo" .void "System.Void" "System.Span`1" + [owner] [.apply writable var], + row source sourceName "TryCopyTo" name!"TryCopyTo" boolean "System.Boolean" + "System.Span`1" [owner] [.apply writable var]] + let arrays := [false, true].map fun counted => + let tail := if counted then [intType, intType] else [] + let params := if counted then "!!0[],!!0,System.Int32,System.Int32" else "!!0[],!!0" + row (.atom runtime "System.Array") "System.Array" "Fill" name!"Fill" .void "System.Void" + params ([.array element, element] ++ tail) ([.array (.parameter 0), .parameter 0] ++ tail) + true + return [row owner spelling "Clear" name!"Clear" .void "System.Void" "" [] [], + row owner spelling "Fill" name!"Fill" .void "System.Void" "!0" [element] [var]] ++ + copies ++ arrays + +/-- The instruction at the sole reachable call in the fixture. -/ +private def bulkCode (site : CallSite) : Except RawDecodeError (List Instruction) := + let body := { raw [] [i 40 (.call site)] with parameters := [] } + (decodeRaw .scalar {} [] [true] body []).map Method.code + +/-- Every genuine overload retains its call. One incorrect operand field refuses the method. -/ +theorem bulk_call_operands : + ((bulkSites intType "System.Int32").all fun site => + let method := site.method + let wrongOwner := match method.owner with + | .apply (.constructor _ name) element => .apply (.constructor owner name) element + | _ => TypeExpr.atom owner "System.Array" + let wrongResult := if method.returnType == .void then intType else .void + let wrongParams := match method.parameters with + | [] => [intType] | _ :: rest => TypeExpr.void :: rest + decide (bulkCode site = .ok [.call method {}]) && + [{ site with method.owner := wrongOwner }, { site with method.name := name!"Other" }, + { site with method.genericArity := method.genericArity + 1 }, + { site with method.parameters := wrongParams }, + { site with method.hasThis := !method.hasThis }, + { site with method.returnType := wrongResult }, { site with declared.convention := 0 }, + { site with declared.parameters := .void :: site.declared.parameters }, + { site with name := "Other" }, { site with target := .external "Other" }, + { site with parameterCount := site.parameterCount + 1 }, + { site with returnsValue := !site.returnsValue }, + { site with ownerArgumentCount := 1 }, { site with typeArguments := [TypeExpr.void] }].all + fun changed => decide (bulkCode changed = liveHoleAt 0 40)) := by + decide +kernel + +#print axioms bulk_call_operands + +/-- A well-formed overload over a floating-point element has no bulk memory model. -/ +theorem bulk_call_elements : + ((bulkSites (.atom runtime "System.Single") "System.Single").all fun site => + decide (bulkCode site = liveHoleAt 0 40)) := by + decide +kernel + +#print axioms bulk_call_elements + +/-- The signed backing and nominal identity survive both array fill overloads. -/ +theorem bulk_enum_models : + let element := TypeExpr.enum "Fixture::Tag" (.atom runtime "System.SByte") + let storage := ArrayElement.enumeration "Fixture::Tag" .signed8 + let operations : List MemoryOp := [.spanFill 1 true, .spanFill 1 false, .spanCopy 1 false, + .spanCopy 1 true, .spanCopy 1 false, .spanCopy 1 true, .arrayFill storage false, + .arrayFill storage true] + ((bulkSites element "Tag").zip operations).all fun (site, operation) => + runtimeModel {} .call site.method == some (.memory operation) && + !(runtimeCatalog.siteAgrees {} .scalar (i 40 (.call site)) + (.call site.method { tail := true }) none true) + := by decide +kernel + +#print axioms bulk_enum_models + +/-- Span copy and fill admit integer vectors. Both array fill overloads reject them. -/ +theorem bulk_vector_models : + let vector := TypeExpr.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1" + let element := TypeExpr.apply vector intType + (bulkSites element "System.Runtime.Intrinsics.Vector128`1").all fun site => + let expected := if site.hasThis then .ok [.call site.method {}] else liveHoleAt 0 40 + decide (bulkCode site = expected) + := by decide +kernel + +#print axioms bulk_vector_models + +/-- Span copy, clear and fill admit a laid-out struct. Both array fill overloads refuse it. -/ +theorem bulk_aggregate_models : + let element := TypeExpr.aggregate "Fixture::Words" 0 + let rows : List RawLayout := [{ + identity := "Fixture::Words", explicit := false, + packing := 0, declaredSize := 0, byRefLike := false, + fields := [⟨.unsigned32, none⟩] }] + let layouts : LayoutEnvironment := + { rawLayouts := rows, layouts := (computeLayouts rows).getD #[] } + (bulkSites element "Words").all fun site => + let body := { raw [] [i 40 (.call site)] with parameters := [] } + let expected := if site.hasThis then .ok [.call site.method {}] else liveHoleAt 0 40 + decide ((decodeRaw .scalar layouts [] [true] body []).map Method.code = expected) + := by decide +kernel + +#print axioms bulk_aggregate_models + +/-- `ReadOnlySpan(uint[])`, the array constructor a call runs on a span home. -/ private def localConstructor : CallSite := { - declaringType := words, name := ".ctor" + declared := { convention := 0x20, parameters := [.array (.parameter 0)] }, + method := { + owner := words, name := name!".ctor", hasThis := true, genericArity := 0, returnType := .void, + parameters := [.array (.atom runtime "System.UInt32")] }, + name := ".ctor", target := .external (runtime ++ - "::System.Void System.ReadOnlySpan`1::.ctor(!0[])") - typeArguments := [], parameterCount := 1, hasThis := true, returnsValue := false - returnType := .void, parameterTypes := [.array (.atom runtime "System.UInt32")] } + "::System.Void System.ReadOnlySpan`1::.ctor(!0[])"), typeArguments := [], + parameterCount := 1, returnsValue := false } +/-- The address of span local 0, a null array and the constructor call. -/ private def constructorCode := - [i 18 (.local 0), i 2, i 40 (.call localConstructor), i 18 (.local 0)] - -/-- Reused local addresses do not disable exact constructor folding. -/ -theorem constructor_fold : foldRoles [words] constructorCode = - [some .head, some .argument, some (.spanConstructor 0 (.array .unsigned32 false .whole)), - none] := by decide +kernel -#print axioms constructor_fold - -/-- Constructor arguments and the constructor itself cannot be external branch targets. -/ -theorem constructor_no_interior_entry : - foldEntry (foldRoles [words] constructorCode)[0]? = true ∧ - foldEntry (foldRoles [words] constructorCode)[1]? = false ∧ - foldEntry (foldRoles [words] constructorCode)[2]? = false := by decide +kernel -#print axioms constructor_no_interior_entry - -/-- Duplicating the local address or changing its declared span type prevents the fold. -/ -theorem constructor_alias_rejected : - foldRoles [words] [i 18 (.local 0), i 37, i 2, i 40 (.call localConstructor)] = - List.replicate 4 none ∧ - foldRoles [span runtime "System.Int32"] constructorCode = List.replicate 4 none := by + [i 18 (.local 0), i 20, i 40 (.call localConstructor), i 42] + +/-- Whether object admission accepts the constructor windows and span resets of `source`, with + every instruction live. -/ +private def admitted (source : RawMethod) : Bool := + (objectSites source (source.code.map fun _ => true) + { code := source.code.map fun _ => .nop, locals := [], returnsValue := false }).isSome + +/-- A constructor call runs on the address of a local or parameter of the constructed span type + followed by one pure push per parameter. -/ +theorem constructor_window_admitted : + admitted (raw [words] constructorCode) = true ∧ + admitted (raw [words, .array (.atom runtime "System.UInt32")] + [i 18 (.local 0), i 7, i 40 (.call localConstructor), i 42]) = true ∧ + admitted (raw [] [i 15 (.argument 0), i 20, i 40 (.call localConstructor), i 42]) = true := by + decide +kernel +#print axioms constructor_window_admitted + +/-- Admission refuses a constructor whose receiver is not a home of the constructed span type: a + store or a duplicate inside the window, a local of another span type, an `in` parameter + loaded by value, and a branch to an argument push or to the call. -/ +theorem constructor_window_refused : + admitted (raw [words, .array (.atom runtime "System.UInt32")] + [i 18 (.local 0), i 20, i 11, i 7, i 40 (.call localConstructor), i 42]) = false ∧ + admitted (raw [words] [i 18 (.local 0), i 37, i 20, i 40 (.call localConstructor)]) = false ∧ + admitted (raw [span runtime "System.Int32"] constructorCode) = false ∧ + admitted { raw [] [i 2, i 20, i 40 (.call localConstructor), i 42] with + parameters := [.byref words] } = false ∧ + admitted (raw [words] (constructorCode ++ [i 43 (.target 1)])) = false ∧ + admitted (raw [words] (constructorCode ++ [i 43 (.target 2)])) = false := by decide +kernel +#print axioms constructor_window_refused + +/-- A branch may enter at the address itself, since the window starts there. -/ +theorem constructor_window_entry_at_address : + admitted (raw [words] (constructorCode ++ [i 43 (.target 0)])) = true := by decide +kernel +#print axioms constructor_window_entry_at_address + +/-- A span `initobj` resets the home its preceding raw instruction addresses, of its own type, and + is not a branch target. -/ +theorem span_reset_evidence : + admitted (raw [words] [i 18 (.local 0), i 65045 (.type words), i 42]) = true ∧ + admitted (raw [] [i 15 (.argument 0), i 65045 (.type words), i 42]) = true ∧ + admitted (raw [words] [i 18 (.local 0), i 0, i 65045 (.type words), i 42]) = false ∧ + admitted (raw [span runtime "System.Int32"] + [i 18 (.local 0), i 65045 (.type words), i 42]) = false ∧ + admitted (raw [words] + [i 18 (.local 0), i 65045 (.type words), i 42, i 43 (.target 1)]) = false := by decide +kernel -#print axioms constructor_alias_rejected - -/-- The emitted local and constructor capability are both bound by the raw sequence. -/ -theorem constructor_forgery_rejected : - featureOpMatchesAt #[] (raw [words] constructorCode) - (some (.spanConstructor 0 (.array .unsigned32 false .whole))) - (i 40 (.call localConstructor)) - (.initializeSpanLocal 1 (.array .unsigned32 false .whole)) = false ∧ - featureOpMatchesAt #[] (raw [words] constructorCode) - (some (.spanConstructor 0 (.array .unsigned32 false .whole))) - (i 40 (.call localConstructor)) - (.initializeSpanLocal 0 (.array .unsigned32 true .whole)) = false := by decide +kernel -#print axioms constructor_forgery_rejected -/-- A readonly cast binds both element types and the exact runtime overload. -/ -private def reinterpretCall : CallSite := { - declaringType := .atom runtime "System.Runtime.InteropServices.MemoryMarshal" - name := "Cast" - target := .external (runtime ++ "::System.ReadOnlySpan`1 " ++ - "System.Runtime.InteropServices.MemoryMarshal::Cast(System.ReadOnlySpan`1)") - typeArguments := [.atom runtime "System.Byte", intType] - parameterCount := 1, hasThis := false, returnsValue := true - returnType := span runtime "System.Int32" - parameterTypes := [span runtime "System.Byte"] } - -/-- Width, readonly status, result identity and the declaring assembly cannot be forged. -/ -theorem exact_reinterpret_call : - spanCallMatches (i 40 (.call reinterpretCall)) - (.memory (.spanReinterpret 1 4 false)) = true ∧ - spanCallMatches (i 40 (.call reinterpretCall)) - (.memory (.spanReinterpret 1 8 false)) = false ∧ - spanCallMatches (i 40 (.call reinterpretCall)) - (.memory (.spanReinterpret 1 4 true)) = false ∧ - spanCallMatches (i 40 (.call reinterpretCall)) (.memory .spanReadOnly) = false ∧ - spanCallMatches (i 40 (.call { reinterpretCall with - returnType := span runtime "System.UInt32" })) - (.memory (.spanReinterpret 1 4 false)) = false ∧ - spanCallMatches (i 40 (.call { reinterpretCall with - declaringType := .atom owner "System.Runtime.InteropServices.MemoryMarshal" })) - (.memory (.spanReinterpret 1 4 false)) = false := by decide +kernel -#print axioms exact_reinterpret_call +#print axioms span_reset_evidence + +/-- The byte-span constructor decodes to the address of its home and a model call, consumes its + argument and returns with an empty stack. -/ +theorem decoded_span_constructor_returns : + let byte := TypeExpr.atom runtimeScope "System.Byte" + let bytes := TypeExpr.apply (.constructor runtimeScope "System.Span`1") byte + let ctor := { localConstructor with + method.owner := bytes, + method.parameters := [.byref byte], declared.parameters := [.byref (.ownerParameter 0)], + target := .external (runtimeScope ++ + "::System.Void System.Span`1::.ctor(!0&)") } + let source := { raw [bytes] [i 18 (.local 0), i 2, i 40 (.call ctor), i 42] with + parameters := [.byref byte], returnType := .void } + let method := Method.ofRaw? .scalar {} [] [true, true, true, true] source [] + let memory : Safety.Memory := { Safety.Memory.empty with + allocations := fun id => if id == 0 then + some ⟨.callerStack, ⟨1, 1, []⟩, true, []⟩ else none, + views := [⟨0, 0, 1, true, true⟩], nextIdentity := 1 } + method.map Method.code = some [.ldloca 0, .ldarg 0, .call ctor.method, .ret] ∧ + (method.bind fun body => (Safety.invoke [body] 5 0 [.reference (.address ⟨0, 0⟩)] + memory).toOption.map (·.2)) = some [] := by + exact ⟨rfl, rfl⟩ + +#print axioms decoded_span_constructor_returns + +/-- Resetting an aggregate span decodes to `initobj` on its home and returns normally; without the + element layout the method is refused. -/ +theorem decoded_span_reset_returns : + let identity := "Fixture::Word" + let element := TypeExpr.aggregate identity 0 + let spanType := TypeExpr.apply (.constructor runtimeScope "System.Span`1") element + let row : RawLayout := { + identity, explicit := false, packing := 0, declaredSize := 0, + fields := [⟨.unsigned64, none⟩] } + let environment : LayoutEnvironment := { + rawLayouts := [row], layouts := #[⟨⟨8, [⟨0, 8, false⟩]⟩, 8⟩] } + let source := { raw [spanType] [i 18 (.local 0), i 65045 (.type spanType), i 42] with + parameters := [], returnType := .void } + let method := Method.ofRaw? .scalar environment [] [true, true, true] source [] + method.map Method.code = some [.ldloca 0, .initobj spanType, .ret] ∧ + (method.bind fun body => (Safety.invoke [body] 5 0 [] Safety.Memory.empty).toOption.map + (·.2)) = some [] ∧ + Method.ofRaw? .scalar { environment with layouts := #[] } [] [true, true, true] source [] = + none := by + exact ⟨rfl, rfl, rfl⟩ + +#print axioms decoded_span_reset_returns /-- Runtime names omit scopes without discarding nested or adjacent generic arguments. -/ theorem closed_generic_element_names : spanElementName (.aggregate ("Examples::Pair`1<" ++ runtimeScope ++ "::System.UInt64>") 0) = diff --git a/Tests/SpanHomeReads.cs b/Tests/SpanHomeReads.cs new file mode 100644 index 000000000..29663ef2e --- /dev/null +++ b/Tests/SpanHomeReads.cs @@ -0,0 +1,87 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:package Mono.Cecil +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include PreparedLeanFixtures.cs +#:include GeneratedEvidence.cs + +using System.Reflection; +using Mono.Cecil; +using Mono.Cecil.Cil; + +// SavedLength stores its first span in a local, takes the local's address, stores the second span there and reads +// the length through the address. The CLR reads the slot when the getter runs, so the result is the second length. +var root = At("artifacts/SpanHomeReads"); +Directory.CreateDirectory(root); +var evidence = new GeneratedEvidence(root); +var assembly = Path.Combine(root, "Examples.dll"); +using (var module = ModuleDefinition.ReadModule(At("Examples/Assembly/bin/Examples.dll"))) +{ + var type = module.GetType("Examples.MemorySpans"); + var byRef = type.Methods.Single(method => method.Name == "ByRefSpan"); + var call = byRef.Body.Instructions.Single(instruction => instruction.OpCode == OpCodes.Call); + var getter = (MethodReference)call.Operand; + var span = ((ByReferenceType)byRef.Parameters[0].ParameterType).ElementType; + var savedLength = new MethodDefinition("SavedLength", + Mono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Int32); + savedLength.Parameters.Add(new ParameterDefinition(span)); + savedLength.Parameters.Add(new ParameterDefinition(span)); + var local = new VariableDefinition(span); + savedLength.Body.Variables.Add(local); + savedLength.Body.InitLocals = true; + var il = savedLength.Body.GetILProcessor(); + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Stloc_0); + il.Emit(OpCodes.Ldloca_S, local); + il.Emit(OpCodes.Ldarg_1); + il.Emit(OpCodes.Stloc_0); + il.Emit(OpCodes.Call, getter); + il.Emit(OpCodes.Ret); + type.Methods.Add(savedLength); + module.Write(assembly); +} +RequireLeanFixtures("CIL.Extraction.Translation"); +var runtime = Assembly.LoadFile(assembly).GetType("Examples.MemorySpans")!.GetMethod("SavedLength")! + .CreateDelegate(); +int[] storage = [0, 0]; +(int First, int Second)[] cases = [(1, 2), (2, 1)]; +foreach (var (first, second) in cases) +{ + var length = runtime(storage.AsSpan(0, first), storage.AsSpan(0, second)); + if (length != second) throw new Exception($"CLR SavedLength({first}, {second}) returned {length}"); +} +var output = Path.Combine(root, "SavedLength"); +Run(["dotnet", ExtractorPath, "extract", assembly, + "MemorySpans::SavedLength(System.Span`1,System.Span`1)", output]); +var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); +var proof = Path.Combine(root, "SavedLength.lean"); +var theorems = string.Concat(cases.Select(lengths => $$""" + + theorem saved_view_length_{{lengths.First}}_{{lengths.Second}} : + (match CIL.Safety.invoke Extracted.program 32 0 + [view {{lengths.First}}, view {{lengths.Second}}] inputMemory with + | .ok outcome => outcome.2 == [.scalar (.i32 {{lengths.Second}})] + | .error _ => false) = true := by decide +kernel + #print axioms saved_view_length_{{lengths.First}}_{{lengths.Second}} + """)); +File.WriteAllText(proof, source + $$""" + + /-- One live 16-byte caller allocation; the length getter reads no element bytes. -/ + private def inputMemory : CIL.Safety.Memory := { CIL.Safety.Memory.empty with + allocations := fun identity => if identity == 0 then + some ⟨.callerStack, ⟨16, 1, []⟩, true, [16]⟩ else none + nextIdentity := 1 } + + /-- A writable int32 view of `length` elements at the start of allocation 0. -/ + private def view (length : Nat) : CIL.Safety.Value := .bufferSpan ⟨.address ⟨0, 0⟩, length, 4, true⟩ + """ + theorems + "\n"); +evidence.Add(proof, "PASS span local read through its address after a later store: SavedLength"); +evidence.Check(replay: false); + +/// The rewritten SavedLength, called with spans the CLR cannot box for reflection. +delegate int SavedLength(Span first, Span second); diff --git a/Tests/SpanHomes.lean b/Tests/SpanHomes.lean new file mode 100644 index 000000000..c5c7e9243 --- /dev/null +++ b/Tests/SpanHomes.lean @@ -0,0 +1,435 @@ +import CIL.Extraction.Scalars +import CIL.Safety.ExecutionInvariants +import CIL.Safety.ValueRenaming +import CIL.Safety.SpanHomeRenaming +import Tests.Support + +/-! # Span locals read through their homes + +`ldloca` of a span local pushes a home that names the slot, and each consumer reads the slot when +it runs. These programs pin that reading against what the CLR returns: a store to the local after +`ldloca` is visible through the address, a callee that stores to its own slot 0 leaves the +caller's slot alone, an uninitialized slot faults where it is read, and no home crosses a frame +boundary. Admission refuses an address where a callee declares a span by value, so a call +projects a home only where the callee takes the span by reference. -/ + +namespace Tests.SpanHomes +open CIL CIL.Extraction CIL.Safety + +set_option maxRecDepth 8192 + +/-- `System.Span`, the type of every span local and parameter below. -/ +private def intSpan : TypeExpr := + .apply (.constructor runtimeScope "System.Span`1") (.atom runtimeScope "System.Int32") + +/-- `Span.get_Length`, whose runtime model reads the view at its receiver. -/ +private def getLength : MethodRef := { + owner := intSpan, name := name!"get_Length", hasThis := true, genericArity := 0, + returnType := .atom runtimeScope "System.Int32", parameters := [] } + +/-- One live 16-byte caller allocation; the length getter reads no element bytes. -/ +private def memory : Safety.Memory := { Memory.empty with + allocations := fun identity => if identity == 0 then + some ⟨.callerStack, ⟨16, 1, []⟩, true, [16]⟩ else none + nextIdentity := 1 } + +/-- A writable int32 view of `length` elements at the start of allocation 0. -/ +private def view (length : Nat) : Safety.Value := .bufferSpan ⟨.address ⟨0, 0⟩, length, 4, true⟩ + +/-- `local = first; ref local; local = second; return local.Length` through the saved address. -/ +private def savedLength : Method := { + code := [.ldarg 0, .stloc 0, .ldloca 0, .ldarg 1, .stloc 0, .call getLength {}, .ret] + locals := [.nullRef], localKinds := [.span 4 true], returnsValue := true } + +/-- The length is read when `get_Length` runs, after the second store, as the CLR reads it. -/ +theorem saved_view_length : + (invoke [savedLength] 16 0 [view 1, view 2] memory).map Prod.snd = + .ok [.scalar (.i32 2)] := rfl +#print axioms saved_view_length + +/-- With the arguments swapped the same body returns the other length. -/ +theorem saved_view_length_swapped : + (invoke [savedLength] 16 0 [view 2, view 1] memory).map Prod.snd = + .ok [.scalar (.i32 1)] := rfl +#print axioms saved_view_length_swapped + +/-- `Recurse(a, b, depth)` stores `a` in its slot 0 and, when `depth` is nonzero, leaves the + address of that slot on the stack while it calls `Recurse(b, a, 0)`, which stores `b` in + its own slot 0. The caller then reads the length through the address it kept. -/ +private def recurse : Method := { + code := [.ldarg 0, .stloc 0, .ldarg 2, .brfalse 12, .ldloca 0, .ldarg 1, .ldarg 0, .ldc_i4 0, + .call (Tests.callee 3) {}, .pop, .call getLength {}, .ret, + .ldloca 0, .call getLength {}, .ret] + locals := [.nullRef], localKinds := [.span 4 true], returnsValue := true + links := [(8, .direct 0)] } + +/-- The address kept across the recursive call names the caller's own slot 0, which still holds + `a`; the callee's store went to the callee's fresh slot. -/ +theorem recursive_store_leaves_caller_slot : + (invoke [recurse] 32 0 [view 1, view 2, .scalar (.i32 1)] memory).map Prod.snd = + .ok [.scalar (.i32 1)] ∧ + (invoke [recurse] 32 0 [view 2, view 1, .scalar (.i32 1)] memory).map Prod.snd = + .ok [.scalar (.i32 2)] := ⟨rfl, rfl⟩ +#print axioms recursive_store_leaves_caller_slot + +/-- A home passed to `invoke` names a slot of no frame, and entry refuses it. -/ +theorem forged_home_refused_at_entry : + (invoke [savedLength] 16 0 [.spanHome 0, view 2] memory).map Prod.snd = + .error ⟨0, 0, .invalidState⟩ := rfl +#print axioms forged_home_refused_at_entry + +/-- A span local of a method without `.locals init`. -/ +private def uninitialized (code : List Instruction) : Method := { + code, locals := [.unmodeled], localKinds := [.span 4 true], returnsValue := true } + +/-- Taking the address of an uninitialized span local reads nothing; reading the slot through the + address faults where the read runs, at the `get_Length` call. -/ +theorem uninitialized_slot_read_through_home : + (invoke [uninitialized [.ldloca 0, .pop, .ldc_i4 0, .ret]] 16 0 [] memory).map Prod.snd = + .ok [.scalar (.i32 0)] ∧ + (invoke [uninitialized [.ldloca 0, .call getLength {}, .ret]] 16 0 [] memory).map + Prod.snd = .error ⟨0, 1, .invalidState⟩ := ⟨rfl, rfl⟩ +#print axioms uninitialized_slot_read_through_home + +/-- `initobj` through the home of an uninitialized span local stores the empty view. -/ +theorem initobj_through_home : + (invoke [uninitialized [.ldloca 0, .initobj intSpan, .ldloca 0, .call getLength {}, .ret]] + 16 0 [] memory).map Prod.snd = .ok [.scalar (.i32 0)] := rfl +#print axioms initobj_through_home + +/-- `Span(ref int)`, the single-element constructor a call runs on a span home. -/ +private def singleConstructor : MethodRef := { + owner := intSpan, name := name!".ctor", hasThis := true, genericArity := 0, + returnType := .void, parameters := [.byref (.atom runtimeScope "System.Int32")] } + +/-- The address of the first int32 of allocation 0, the element a single-element span views. -/ +private def element : Safety.Value := .reference (.address ⟨0, 0⟩) + +/-- A constructor call on the home of a span local replaces the view in the slot: the local held + a two-element view, or nothing, and the length read through the home afterwards is 1. -/ +theorem constructor_through_local_home : + (invoke [{ savedLength with + code := [.ldarg 0, .stloc 0, .ldloca 0, .ldarg 1, .call singleConstructor {}, .ldloca 0, + .call getLength {}, .ret] }] 16 0 [view 2, element] memory).map Prod.snd = + .ok [.scalar (.i32 1)] ∧ + (invoke [uninitialized [.ldloca 0, .ldarg 0, .call singleConstructor {}, .ldloca 0, + .call getLength {}, .ret]] 16 0 [element] memory).map Prod.snd = + .ok [.scalar (.i32 1)] := ⟨rfl, rfl⟩ +#print axioms constructor_through_local_home + +/-- A home cannot leave its frame by `ret` or be stored as a value in another local. -/ +theorem home_stays_in_frame : + (invoke [{ savedLength with code := [.ldarg 0, .stloc 0, .ldloca 0, .ret] }] 16 0 + [view 1, view 2] memory).map Prod.snd = .error ⟨0, 3, .invalidState⟩ ∧ + (invoke [{ savedLength with + code := [.ldarg 0, .stloc 0, .ldloca 0, .stloc 1, .ldc_i4 0, .ret], + locals := [.nullRef, .nullRef], localKinds := [.span 4 true, .span 4 true] }] 16 0 + [view 1, view 2] memory).map Prod.snd = .error ⟨0, 3, .invalidState⟩ := ⟨rfl, rfl⟩ +#print axioms home_stays_in_frame + +/-- Returns the length of the span it receives. -/ +private def reader : Method := { + code := [.ldarg 0, .call getLength {}, .ret], locals := [], returnsValue := true } + +/-- Passes the address of its span local to `reader` after storing the second view there. -/ +private def passesHome : Method := { + code := [.ldarg 0, .stloc 0, .ldloca 0, .ldarg 1, .stloc 0, .call (Tests.callee 1) {}, .ret] + locals := [.nullRef], localKinds := [.span 4 true], returnsValue := true + links := [(5, .direct 1)] } + +/-- A linked call reads the home when the call runs, so the callee receives the second view. -/ +theorem linked_call_receives_view : + (invoke [passesHome, reader] 32 0 [view 1, view 2] memory).map Prod.snd = + .ok [.scalar (.i32 2)] ∧ + (invoke [passesHome, reader] 32 0 [view 2, view 1] memory).map Prod.snd = + .ok [.scalar (.i32 1)] := ⟨rfl, rfl⟩ +#print axioms linked_call_receives_view + +/-- Takes the address of its by-value span parameter, stores the second argument to it with + `starg` and reads the length through the address. -/ +private def homedParameter : Method := { + code := [.ldarga 0, .ldarg 1, .starg 0, .call getLength {}, .ret] + locals := [], parameterHomes := [(0, .span 4 true)], returnsValue := true } + +/-- `ldarga` of a homed span parameter is a home, so the read sees the `starg`. -/ +theorem parameter_home_sees_store : + (invoke [homedParameter] 16 0 [view 1, view 2] memory).map Prod.snd = + .ok [.scalar (.i32 2)] := rfl +#print axioms parameter_home_sees_store + +/-- A constructor call on the home of a by-value span parameter replaces the argument's view. -/ +theorem constructor_through_parameter_home : + (invoke [{ homedParameter with + code := [.ldarga 0, .ldarg 1, .call singleConstructor {}, .ldarga 0, .call getLength {}, + .ret] }] 16 0 [view 2, element] memory).map Prod.snd = .ok [.scalar (.i32 1)] := rfl +#print axioms constructor_through_parameter_home + + + +/-- A store of a span through a home faults: only `initobj` and a constructor write the slot. -/ +theorem stobj_through_home_faults : + (invoke [{ savedLength with + code := [.ldarg 0, .stloc 0, .ldloca 0, .ldarg 1, .stobj intSpan, .ldc_i4 0, .ret] }] 16 0 + [view 1, view 2] memory).map Prod.snd = + .error ⟨0, 4, .unsupported "Object transfer shape"⟩ := rfl +#print axioms stobj_through_home_faults + +/-- `passesHome` with a dead local before its span local, as a local deletion map relates it. -/ +private def passesHomeShifted : Method := { + code := [.ldarg 0, .stloc 1, .ldloca 1, .ldarg 1, .stloc 1, .call (Tests.callee 1) {}, .ret] + locals := [.nullRef, .nullRef], localKinds := [.span 4 true, .span 4 true] + returnsValue := true, links := [(5, .direct 1)] } + +/-- The deletion map that skips target slot 0 and keeps the span local. -/ +private def shift : LocalIndexMap := .skip (.keep .nil) + +/-- The frame of `passesHome` once its span local holds the two-element view. -/ +private def sourceFrame : Frame := + { activation := 0, locals := [.span 4 true (some ⟨.address ⟨0, 0⟩, 2, 4, true⟩)], owned := [] } + +/-- The frame of `passesHomeShifted` at the same point, with the dead slot first. -/ +private def targetFrame : Frame := + { sourceFrame with locals := .span 4 true none :: sourceFrame.locals } + +/-- `shift` relates the two frames under the identity allocation map of `memory`. -/ +private theorem shiftedFrames : + Frame.Renamed shift (.identity memory.nextIdentity) Eq sourceFrame targetFrame := + ⟨rfl, .skip _ (.keep ⟨fun _ held => by cases held; exact Nat.zero_lt_one, rfl⟩ .nil), .nil, rfl⟩ + +/-- `Frame.Renamed.spanView` reads the home of source slot 0 and its relocation, the home of + target slot 1, as one renamed view; both reads give the view the slot holds, and the shifted + program returns what `passesHome` returns through the linked call and the model call. -/ +theorem shifted_home_renamed : + ResultRenamed (.identity memory.nextIdentity) + (fun left right => right = (AllocationRenaming.identity memory.nextIdentity).value left ∧ + left.Below (AllocationRenaming.identity memory.nextIdentity).sourceLimit) + (spanView sourceFrame memory (.spanHome 0)) + (spanView targetFrame memory (shift.value (.identity memory.nextIdentity) (.spanHome 0))) ∧ + shift.value (.identity memory.nextIdentity) (.spanHome 0) = .spanHome 1 ∧ + spanView targetFrame memory (.spanHome 1) = .ok (view 2) ∧ + (invoke [passesHomeShifted, reader] 32 0 [view 1, view 2] memory).map Prod.snd = + .ok [.scalar (.i32 2)] ∧ + (invoke [passesHomeShifted, reader] 32 0 [view 2, view 1] memory).map Prod.snd = + .ok [.scalar (.i32 1)] := + ⟨shiftedFrames.spanView (Memory.Renamed.refl memory Eq fun _ member => nomatch member) + (.spanHome 0) trivial, rfl, rfl, rfl, rfl⟩ +#print axioms shifted_home_renamed + +/-- `System.ReadOnlySpan`, whose pointer constructor views bytes. -/ +private def byteSpan : TypeExpr := + .apply (.constructor runtimeScope "System.ReadOnlySpan`1") (.atom runtimeScope "System.Byte") + +/-- `ReadOnlySpan(void*, int)`. -/ +private def bytePointerConstructor : MethodRef := { + owner := byteSpan, name := name!".ctor", hasThis := true, genericArity := 0, + returnType := .void, parameters := [.pointer .void, .atom runtimeScope "System.Int32"] } + +/-- `ReadOnlySpan.get_Length`. -/ +private def byteLength : MethodRef := { + owner := byteSpan, name := name!"get_Length", hasThis := true, genericArity := 0, + returnType := .atom runtimeScope "System.Int32", parameters := [] } + +/-- A method with one read-only byte span local. -/ +private def byteLocal (code : List Instruction) : Method := { + code, locals := [.nullRef], localKinds := [.span 1 false], returnsValue := true } + +/-- In place, `ReadOnlySpan(void*, int)` views the bytes the pointer names, in the caller's + storage or in `localloc` storage, and the length reads 3 or 8. `newobj` of the same + constructor views only extracted static data, so over caller storage it faults. -/ +theorem byte_pointer_constructor_in_place : + (invoke [byteLocal [.ldloca 0, .ldarg 0, .ldc_i4 3, .call bytePointerConstructor {}, + .ldloca 0, .call byteLength {}, .ret]] 16 0 [element] memory).map Prod.snd = + .ok [.scalar (.i32 3)] ∧ + (invoke [{ byteLocal [.ldc_i4 8, .localloc, .stloc 1, .ldloca 0, .ldloc 1, .ldc_i4 8, + .call bytePointerConstructor {}, .ldloca 0, .call byteLength {}, .ret] with + locals := [.nullRef, .nullRef], localKinds := [.span 1 false, .reference] }] 16 0 [] + { memory with stackLimit := 64 }).map Prod.snd = .ok [.scalar (.i32 8)] ∧ + (invoke [byteLocal [.ldarg 0, .ldc_i4 3, .newobj bytePointerConstructor, .stloc 0, + .ldloca 0, .call byteLength {}, .ret]] 16 0 [element] memory).map Prod.snd = + .error ⟨0, 2, .unsupported "Span pointer constructor outside extracted static data"⟩ := + ⟨rfl, rfl, rfl⟩ +#print axioms byte_pointer_constructor_in_place + +/-- Whether a step succeeds. -/ +private def succeeds : ExecutionResult FrameAction → Bool + | .ok _ => true + | .error _ => false + +/-- The step itself refuses a constructor whose spans differ from the home's slot in width or + access, whatever admission decided: a byte or writable view into a read-only int32 slot. -/ +theorem in_place_constructor_matches_slot : + let frame : Frame := { activation := 0, locals := [.span 4 false none], owned := [] } + let arguments := [.scalar (.i32 4), element, .spanHome 0] + let mismatch : ExecutionResult FrameAction := + .error (.unsupported "Span constructor does not build the span of its home") + spanInPlaceCall (.reference 1 false) 0 frame arguments memory = mismatch ∧ + spanInPlaceCall (.reference 4 true) 0 frame arguments memory = mismatch ∧ + succeeds (spanInPlaceCall (.reference 4 false) 0 frame arguments memory) := ⟨rfl, rfl, rfl⟩ +#print axioms in_place_constructor_matches_slot + +/-- `System.ReadOnlySpan`. -/ +private def readOnlyIntSpan : TypeExpr := + .apply (.constructor runtimeScope "System.ReadOnlySpan`1") (.atom runtimeScope "System.Int32") + +/-- `ReadOnlySpan(in int)`, the read-only single-element constructor. -/ +private def readOnlySingleConstructor : MethodRef := { + owner := readOnlyIntSpan, name := name!".ctor", hasThis := true, genericArity := 0, + returnType := .void, parameters := [.byref (.atom runtimeScope "System.Int32")] } + +/-- `ReadOnlySpan.get_Length`. -/ +private def readOnlyLength : MethodRef := { getLength with owner := readOnlyIntSpan } + +/-- A read-only int32 view of two elements at the start of allocation 0. -/ +private def readOnlyView : Safety.Value := .bufferSpan ⟨.address ⟨0, 0⟩, 2, 4, false⟩ + +/-- An `in` or `ref readonly` span parameter arrives as the view, which names no slot, so a + constructor called on it faults. On the home of a homed read-only span parameter the + writable constructor faults at the step and the read-only one replaces the view. -/ +theorem constructor_on_read_only_parameter : + (invoke [{ reader with + code := [.ldarg 0, .ldarg 1, .call singleConstructor {}, .ldc_i4 0, .ret] }] 16 0 + [view 2, element] memory).map Prod.snd = .error ⟨0, 2, .invalidState⟩ ∧ + (invoke [{ homedParameter with + code := [.ldarga 0, .ldarg 1, .call singleConstructor {}, .ldc_i4 0, .ret], + parameterHomes := [(0, .span 4 false)] }] 16 0 + [readOnlyView, element] memory).map Prod.snd = + .error ⟨0, 2, .unsupported "Span constructor does not build the span of its home"⟩ ∧ + (invoke [{ homedParameter with + code := [.ldarga 0, .ldarg 1, .call readOnlySingleConstructor {}, .ldarga 0, + .call readOnlyLength {}, .ret], + parameterHomes := [(0, .span 4 false)] }] 16 0 + [readOnlyView, element] memory).map Prod.snd = .ok [.scalar (.i32 1)] := + ⟨rfl, rfl, rfl⟩ +#print axioms constructor_on_read_only_parameter + +/-- A method with one span parameter and one span local, in the raw form admission checks. -/ +private def spanRaw : RawMethod := { + token := 1, owner := "Fixture", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, + returnType := .atom runtimeScope "System.Int32", parameters := [intSpan], locals := [intSpan], + initLocals := true, maxStack := 2, genericParameters := [], code := [] } + +/-- A managed call to a void method taking one argument of type `parameter`. -/ +private def takes (parameter : TypeExpr) : RawInstruction := ⟨0, 40, .call { + declared := { convention := 0, parameters := [parameter] }, + method := { + owner := .atom "Fixture" "Helpers", name := name!"Use", hasThis := false, + genericArity := 0, returnType := .void, parameters := [parameter] }, + name := "Use", target := .managed 2, typeArguments := [], parameterCount := 1, + returnsValue := false }⟩ + +/-- `Span.CopyTo(Span)`, a framework call whose receiver is an address and whose + destination is a span taken by value. -/ +private def copyTo : RawInstruction := ⟨0, 40, .call { + declared := { convention := 0x20, parameters := [intSpan] }, + method := { + owner := intSpan, name := name!"CopyTo", hasThis := true, genericArity := 0, + returnType := .void, parameters := [intSpan] }, + name := "CopyTo", target := .external (runtimeScope ++ + "::System.Void System.Span`1::CopyTo(System.Span`1)"), + typeArguments := [], parameterCount := 1, returnsValue := false }⟩ + +/-- An object type whose members take a span by value. -/ +private def holder : TypeExpr := .object "Fixture" "Fixture.Holder" + +/-- A managed instance call under `opcode` to `member` of `holder`, taking one `parameter`. -/ +private def instanceTakes (opcode : Nat) (member : MetadataName) (name : String) + (parameter : TypeExpr) : RawInstruction := ⟨0, opcode, .call { + declared := { convention := 0x20, parameters := [parameter] }, + method := { + owner := holder, name := member, hasThis := true, genericArity := 0, + returnType := .void, parameters := [parameter] }, + name, target := .managed 2, typeArguments := [], parameterCount := 1, + returnsValue := false }⟩ + +/-- `ldloca` and `ldarga` of a span leave an address, which admission accepts at a by-reference + span parameter and refuses at a by-value one or as a value stored to a span local. A + framework call is checked the same way: `CopyTo` takes its receiver by address and refuses + an address as its by-value destination. `callvirt` and `newobj` refuse an address at a + by-value span parameter as `call` does. -/ +theorem home_refused_where_span_is_by_value : + scalarTransfer spanRaw ⟨0, 18, .local 0⟩ [] = some [.reference] ∧ + scalarTransfer spanRaw ⟨0, 15, .argument 0⟩ [] = some [.reference] ∧ + scalarTransfer spanRaw (takes intSpan) [.reference] = none ∧ + scalarTransfer spanRaw ⟨0, 10, .none⟩ [.reference] = none ∧ + scalarTransfer spanRaw (takes intSpan) [scalarType intSpan] = some [] ∧ + scalarTransfer spanRaw (takes (.byref intSpan)) [.reference] = some [] ∧ + scalarTransfer spanRaw copyTo [scalarType intSpan, .reference] = some [] ∧ + scalarTransfer spanRaw copyTo [.reference, .reference] = none ∧ + scalarTransfer spanRaw (instanceTakes 111 name!"Use" "Use" intSpan) [scalarType intSpan, + scalarType holder] = some [] ∧ + scalarTransfer spanRaw (instanceTakes 111 name!"Use" "Use" intSpan) [.reference, + scalarType holder] = none ∧ + scalarTransfer spanRaw (instanceTakes 115 name!".ctor" ".ctor" intSpan) [scalarType intSpan] = + some [scalarType holder] ∧ + scalarTransfer spanRaw (instanceTakes 115 name!".ctor" ".ctor" intSpan) [.reference] = + none := by + decide +kernel +#print axioms home_refused_where_span_is_by_value + +/-- An address followed by `call` and `ret`: the IL that would hand a home to a by-value span + parameter of `call`. -/ +private def byValueRaw (address call : RawInstruction) : RawMethod := + { spanRaw with returnType := .void, code := [address, call, ⟨0, 42, .none⟩] } + +/-- No scalar certificate admits `byValueRaw address call` when `address` pushes an address: pc 0 + must hand pc 1 exactly that address, and `call` refuses it at the by-value parameter. -/ +private theorem byValue_refused {address call : RawInstruction} + (roles : foldRoles (byValueRaw address call).code = + [none, none, none]) + (pushes : scalarTransfer (byValueRaw address call) address [] = some [.reference]) + (calls : scalarTransfer (byValueRaw address call) call [.reference] = none) + (successor : ∀ stack, featureSuccessors none 0 address stack = some [1]) + (notStore : ∀ stack emitted, + scalarStoreFault (byValueRaw address call) address stack emitted = false) + (callNotStore : ∀ stack emitted, + scalarStoreFault (byValueRaw address call) call stack emitted = false) + (features : FeatureCertificate) (certificate : ScalarCertificate) + (code : List Instruction) : + ¬ ScalarFlowValid (byValueRaw address call) features certificate code := by + intro valid + have lengths := valid + obtain ⟨featureLength, scalarLength, codeLength, entry, _⟩ := lengths + simp only [byValueRaw, List.length_cons, List.length_nil] at featureLength codeLength + have siteAt (pc : Nat) (bound : pc < 3) : + ∃ feature emitted, features[pc]? = some feature ∧ code[pc]? = some emitted := + ⟨features[pc]'(by omega), code[pc]'(by omega), List.getElem?_eq_getElem _, + List.getElem?_eq_getElem _⟩ + obtain ⟨feature0, emitted0, featureAt0, emittedAt0⟩ := siteAt 0 (by omega) + obtain ⟨feature1, emitted1, featureAt1, emittedAt1⟩ := siteAt 1 (by omega) + obtain ⟨state0, _, first⟩ := valid.transfer_at rfl (by rw [roles]; rfl) featureAt0 entry + emittedAt0 + rw [notStore] at first + obtain ⟨after, successors, transferred, successorsAt, reached⟩ := + first.resolve_left Bool.false_ne_true + rw [pushes, Option.some.injEq] at transferred + rw [successor, Option.some.injEq] at successorsAt + subst transferred successorsAt + obtain ⟨_, _, second⟩ := valid.transfer_at rfl (by rw [roles]; rfl) featureAt1 + (reached 1 (List.mem_singleton_self 1)) emittedAt1 + rw [callNotStore] at second + obtain ⟨_, _, refused, _⟩ := second.resolve_left Bool.false_ne_true + rw [calls] at refused + cases refused +#print axioms byValue_refused + +/-- Admission refuses `ldloca` or `ldarga` of a span followed by a `call`, or `ldloca` followed by + a `newobj`, that takes the span by value, for every certificate, so a linked call or + construction never receives a home at such a position. -/ +theorem home_at_by_value_parameter_refused (features : FeatureCertificate) + (certificate : ScalarCertificate) (code : List Instruction) : + ¬ ScalarFlowValid (byValueRaw ⟨0, 18, .local 0⟩ (takes intSpan)) features certificate code ∧ + ¬ ScalarFlowValid (byValueRaw ⟨0, 15, .argument 0⟩ (takes intSpan)) features certificate + code ∧ + ¬ ScalarFlowValid + (byValueRaw ⟨0, 18, .local 0⟩ (instanceTakes 115 name!".ctor" ".ctor" intSpan)) + features certificate code := + ⟨byValue_refused (by decide +kernel) (by decide +kernel) (by decide +kernel) + (fun _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) features certificate code, + byValue_refused (by decide +kernel) (by decide +kernel) (by decide +kernel) + (fun _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) features certificate code, + byValue_refused (by decide +kernel) (by decide +kernel) (by decide +kernel) + (fun _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) features certificate code⟩ +#print axioms home_at_by_value_parameter_refused + +end Tests.SpanHomes diff --git a/Tests/SpanModels.lean b/Tests/SpanModels.lean new file mode 100644 index 000000000..5a1048408 --- /dev/null +++ b/Tests/SpanModels.lean @@ -0,0 +1,225 @@ +import CIL.Extraction.DecodedMethod +import CIL.Safety.StepComposition +import Tests.Support.RawDecoding + +/-! +# Core span call agreement + +The fixtures instantiate the MemberRef signatures on a primitive, an enum, an integer vector +and an aggregate. Each overload has a distinct declaration mutation with unchanged closed types. +-/ + +namespace Tests.SpanModels +open CIL CIL.Extraction + +/-- The fixture's primitive and span type identities. -/ +private def int32 : TypeExpr := .atom runtimeScope "System.Int32" + +/-- A closed or open span of the specified element. -/ +private def span (writable : Bool) (element : TypeExpr) : TypeExpr := + let name := if writable then "System.Span`1" else "System.ReadOnlySpan`1" + .apply (.constructor runtimeScope name) element + +/-- One overload with its closed method, declaration, forged declaration and memory operation. -/ +private structure Overload where + name : String + method : MethodRef + declared : OpenSignature + forged : OpenSignature + operation : MemoryOp + +/-- Genuine span signatures and single substitutions in an open result or parameter. -/ +private def overloads (writable : Bool) (element : TypeExpr) (width : Nat) : List Overload := + let owner := span writable element + let var := TypeExpr.ownerParameter 0 + let openOwner := span writable var + let boolean := TypeExpr.atom runtimeScope "System.Boolean" + let modified := fun type => TypeExpr.modified true runtimeScope + "System.Runtime.InteropServices.InAttribute" (.byref type) + let openRef := if writable then TypeExpr.byref var else modified var + let closedRef := if writable then TypeExpr.byref element else + .atom runtimeScope "!0& modreq(System.Runtime.InteropServices.InAttribute)" + let row (text : String) (name : MetadataName) (convention : Nat) + (result : TypeExpr) (params : List TypeExpr) (declared forged : OpenSignature) + (op : MemoryOp) : Overload := + ⟨text, ⟨owner, name, convention == 0x20, 0, result, params, []⟩, declared, forged, op⟩ + [row "get_Item" name!"get_Item" 0x20 closedRef [int32] ⟨0x20, openRef, [int32]⟩ + ⟨0x20, if writable then modified var else .byref var, [int32]⟩ (.spanItem width), + row "get_Length" name!"get_Length" 0x20 int32 [] ⟨0x20, int32, []⟩ + ⟨0x20, var, []⟩ .spanLength, + row "Slice" name!"Slice" 0x20 owner [int32] ⟨0x20, openOwner, [int32]⟩ + ⟨0x20, span (!writable) var, [int32]⟩ (.spanSlice false), + row "Slice" name!"Slice" 0x20 owner [int32, int32] ⟨0x20, openOwner, [int32, int32]⟩ + ⟨0x20, openOwner, [int32, var]⟩ (.spanSlice true), + row "get_IsEmpty" name!"get_IsEmpty" 0x20 boolean [] ⟨0x20, boolean, []⟩ + ⟨0x20, int32, []⟩ (.observeView .spanEmpty), + row "GetPinnableReference" name!"GetPinnableReference" 0x20 closedRef [] ⟨0x20, openRef, []⟩ + ⟨0x20, if writable then .byref (.parameter 0) else modified (.parameter 0), []⟩ + (.observeView .spanPinnable), + row "get_Empty" name!"get_Empty" 0 owner [] ⟨0, openOwner, []⟩ + ⟨0, span writable (.parameter 0), []⟩ (.spanEmpty width writable)] ++ + [row "op_Implicit" name!"op_Implicit" 0 owner [.array element] ⟨0, openOwner, [.array var]⟩ + ⟨0, openOwner, [.array (.parameter 0)]⟩ + (.arraySpan ((arrayElementType element width).getD .signed32) writable .whole)] ++ + (if writable then [row "op_Implicit" name!"op_Implicit" 0 (span false element) [owner] + ⟨0, span false var, [openOwner]⟩ ⟨0, span false var, [span true (.parameter 0)]⟩ + .spanReadOnly] else []) + +/-- Both owners' model operations for the same element. -/ +private def rows (element : TypeExpr) (width : Nat) : List Overload := + overloads true element width ++ overloads false element width + +/-- A nominal aggregate whose size differs between the two checked layout environments. -/ +private def aggregate : TypeExpr := .aggregate "Fixture::Words" 0 + +/-- The raw and computed layout for an aggregate containing the given number of int32 fields. -/ +private def environment (count : Nat) (byRefLike := false) : LayoutEnvironment := + let raw : List RawLayout := [{ + identity := "Fixture::Words", explicit := false, packing := 0, + declaredSize := 0, byRefLike, fields := List.replicate count ⟨.unsigned32, none⟩ }] + { rawLayouts := raw, layouts := (computeLayouts raw).getD #[] } + +/-- Resolution gives the expected memory operation at every overload. -/ +private def resolves (env : LayoutEnvironment) (element : TypeExpr) (width : Nat) : Bool := + (rows element width).all fun row => + runtimeModel env .call row.method == some (.memory row.operation) + +/-- Primitive, enum, vector and aggregate elements retain their own storage widths. -/ +theorem elementWidths : + resolves {} int32 4 ∧ + resolves {} (.enum "Fixture::Small" (.atom runtimeScope "System.Byte")) 1 ∧ + resolves {} (.apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") + int32) 16 ∧ resolves (environment 1) aggregate 4 ∧ + resolves (environment 3) aggregate 12 := by + decide +kernel + +#print axioms elementWidths + +/-- Every rejected element, including an unbound aggregate, misses the production model table. -/ +theorem unsupportedElements : + ([TypeExpr.void, .object runtimeScope "System.String", .byref int32, .array int32, + .atom runtimeScope "System.Double", .atom "Fixture" "System.Int32", + .enum "Fixture::Bad" (.atom runtimeScope "System.Boolean"), + .apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") + (.atom runtimeScope "System.Single"), .aggregate "Other::Words" 0, + .aggregate "Fixture::Words" 1].all fun type => + (rows type 0).all fun row => (runtimeModel (environment 1) .call row.method).isNone) := by + decide +kernel + +#print axioms unsupportedElements + +/-- The external signature spelling of an open type in the fixtures. -/ +private def spelling : TypeExpr → String + | .array (.ownerParameter 0) => "!0[]" + | .byref (.ownerParameter 0) => "!0&" + | .modified true _ name (.byref (.ownerParameter 0)) => s!"!0& modreq({name})" + | .apply (.constructor _ owner) (.ownerParameter 0) => s!"{owner}" + | .atom _ name => name + | _ => "?" + +/-- The raw call operand used to check a live emission and its metadata declaration. -/ +private def Overload.raw (row : Overload) (declared : OpenSignature) : RawInstruction := + let owner := String.ofList ((ownerTypeName 0 row.method.owner).getD []) + let result := spelling row.declared.returnType + let params := ",".intercalate (row.declared.parameters.map spelling) + ⟨0, 40, .call { + method := row.method, declared, name := row.name, typeArguments := [], + target := .external s!"{runtimeScope}::{result} {owner}::{row.name}({params})", + parameterCount := row.method.parameters.length, returnsValue := true }⟩ + +/-- The single decoded instruction in a caller of the given runtime member, or the stage that + refuses the caller. -/ +private def decoded (raw : RawInstruction) (layouts : LayoutEnvironment := {}) : + Except RawDecodeError (List Instruction) := + let body : RawMethod := { + token := 1, owner := "Fixture", hasThis := false, explicitThis := false, + callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := false, maxStack := 3, genericParameters := [], code := [raw] } + (decodeRaw .scalar layouts [] [true] body []).map Method.code + +/-- Only array conversions reject an otherwise valid byref-like aggregate layout. -/ +theorem byRefLikeArrays : + (rows aggregate 4).all (fun row => + let raw := row.raw row.declared + let call := Instruction.call row.method {} + let expected := match row.operation with + | .arraySpan .. => liveHoleAt 0 40 + | _ => .ok [call] + decide (decoded raw (environment 1) = .ok [call]) && + decide (decoded raw (environment 1 true) = expected)) = true := by + decide +kernel + +#print axioms byRefLikeArrays + +/-- A closed operand in the wrong slot or with the wrong calling convention has no model. -/ +theorem closedRefusals : + ((rows int32 4).all fun row => + let method := row.method + let misses := fun reference => (runtimeModel {} .call reference).isNone + (runtimeModel {} .newobj method).isNone && + misses { method with hasThis := !method.hasThis } && + misses { method with genericArity := 1 } && + misses { method with methodArguments := [int32] } && + misses { method with owner := .apply (.constructor runtimeScope "Another") int32 } && + misses { method with owner := .apply (.constructor "Fixture" "System.Span`1") int32 }) ∧ + (runtimeModel {} .call { + owner := span false int32, name := name!"op_Implicit", hasThis := false, genericArity := 0, + parameters := [span false int32], returnType := span false int32 }).isNone := by + decide +kernel + +#print axioms closedRefusals + +/-- A live site requires the kept call and the open declaration. Wrong conventions and a + forged declaration reject both the original call and one with an added tail prefix. -/ +theorem liveAgreement : + ((rows int32 4).all fun row => + let accepts := fun sig op => + runtimeCatalog.siteAgrees {} .scalar (row.raw sig) op none true + let call := Instruction.call row.method {} + accepts row.declared call && !accepts row.declared (.call row.method { tail := true }) && + !accepts row.forged call && !accepts row.forged (.call row.method { tail := true }) && + ([0, 0x20, 1, 0x10, 0x11, 0x30, 0x40, 0x50, 0x60, 0x70].all fun header => + header == row.declared.convention || + !accepts { row.declared with convention := header } call)) := by + decide +kernel + +#print axioms liveAgreement + +/-- Changing only the declaration of a resolving call yields an opcode-tagged hole. -/ +theorem decodedOverloads : + ((rows int32 4).all fun row => + decide (decoded (row.raw row.declared) = .ok [.call row.method {}]) && + decide (decoded (row.raw row.forged) = liveHoleAt 0 40)) := by + decide +kernel + +#print axioms decodedOverloads + +/-- A mismatched target, name, stack count or type argument gives an opcode-tagged hole. -/ +theorem operandRefusals : + ((rows int32 4).all fun row => Id.run do + let raw := row.raw row.declared + let .call site := raw.operand | return false + return [{ site with target := .external "Fixture::Other" }, { site with name := "Other" }, + { site with parameterCount := site.parameterCount + 1 }, + { site with returnsValue := false }, + { site with typeArguments := [int32] }, { site with ownerArgumentCount := 1 }].all + fun changed => decide (decoded { raw with operand := .call changed } = + liveHoleAt 0 40)) := by + decide +kernel + +#print axioms operandRefusals + +/-- Every resolved span call has its runtime model's exact frame, stack, memory and fault + for all receiver values, including those from argument homes, local addresses and byrefs. -/ +theorem stepOfSpanCall (body : Method) (pc : Nat) (args : List Safety.Value) (frame : Safety.Frame) + (memory : Safety.Memory) (stack : List Safety.Value) (row : Overload) + (link : body.linkAt pc = none) + (found : runtimeModel body.environment .call row.method = some (.memory row.operation)) : + Safety.step body (.call row.method {}) pc args frame stack memory = + Safety.modelStep (some (.memory row.operation)) body pc frame stack memory := + Safety.step_call_model body pc args frame memory link found rfl stack + +#print axioms stepOfSpanCall + +end Tests.SpanModels diff --git a/Tests/SpanReferenceContracts.lean b/Tests/SpanReferenceContracts.lean index 66df6582d..5ece0de5d 100644 --- a/Tests/SpanReferenceContracts.lean +++ b/Tests/SpanReferenceContracts.lean @@ -71,9 +71,9 @@ theorem read_holds : readContract.Holds Extracted.program 0 := by memory input.expected ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ cil_supply_fuel dsimp only [readContract] - simp only [Safety.invoke, Extracted.lookup0] + simp only [Safety.invoke_eq, Extracted.program_method0] simp [checkedValue, numericValue, valid, enterFrame, Extracted.method0, makeLocals, - makeArgumentHomes, makeParameterHomes, checkedAt, Except.mapError, Bind.bind, Except.bind, + makeParameterHomes, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, List.mapM_cons, List.mapM_nil] cil_checked_steps [readContract, ReadInput.view, valid, element, scalar, bound, numericOfWidth, formed, item, extend_byte] diff --git a/Tests/SpanReferenceSecondContract.lean b/Tests/SpanReferenceSecondContract.lean index 703cb9f49..f3d33084f 100644 --- a/Tests/SpanReferenceSecondContract.lean +++ b/Tests/SpanReferenceSecondContract.lean @@ -50,9 +50,9 @@ theorem second_holds : secondContract.Holds Extracted.program 0 := by memory input.second ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ cil_supply_fuel dsimp only [secondContract] - simp only [Safety.invoke, Extracted.lookup0] + simp only [Safety.invoke_eq, Extracted.program_method0] simp [checkedValue, valid, enterFrame, Extracted.method0, makeLocals, - makeArgumentHomes, makeParameterHomes, checkedAt, Except.mapError, Bind.bind, Except.bind, + makeParameterHomes, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, List.mapM_cons, List.mapM_nil] cil_checked_steps [secondContract, SecondInput.view, valid, word, formed, offset, referenceAt] diff --git a/Tests/SpanReferenceWordContract.lean b/Tests/SpanReferenceWordContract.lean index 478539aa7..937e022ec 100644 --- a/Tests/SpanReferenceWordContract.lean +++ b/Tests/SpanReferenceWordContract.lean @@ -46,9 +46,9 @@ theorem word_holds : wordContract.Holds Extracted.program 0 := by memory input.first ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ cil_supply_fuel dsimp only [wordContract] - simp only [Safety.invoke, Extracted.lookup0] + simp only [Safety.invoke_eq, Extracted.program_method0] simp [checkedValue, valid, enterFrame, Extracted.method0, makeLocals, - makeArgumentHomes, makeParameterHomes, checkedAt, Except.mapError, Bind.bind, Except.bind, + makeParameterHomes, checkedAt, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, List.mapM_cons, List.mapM_nil] cil_checked_steps [wordContract, WordInput.view, valid, word, formed] diff --git a/Tests/SpanReferences.cs b/Tests/SpanReferences.cs index 1b011a917..5a1789376 100644 --- a/Tests/SpanReferences.cs +++ b/Tests/SpanReferences.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs @@ -194,14 +199,15 @@ .ok [.scalar (.i64 0)] := by rfl var forged = Path.Combine(root, "Forged-argument.lean"); File.WriteAllText(forged, source.Replace(argumentEvidence, "arguments := [.span 1 false]", StringComparison.Ordinal)); rejected.Add(forged, "PASS span reference argument evidence rejection", - "⊢ Extraction.closedArguments rawLayouts method0Closed = some method0RawArguments"); -// A span copy may emit only the empty operation; any load would reinterpret the view as bytes. -var copy = NarrowEvidence(File.ReadAllText(Path.Combine(root, "Copy", "Extracted.lean")), - "method0_features_validated"); -const string copied = " .arg 0,\n .nop,"; -if (!copy.Contains(copied, StringComparison.Ordinal)) throw new InvalidOperationException("Missing span copy"); + ClosedArgumentsMismatch); +// Replacing the decoded span load by an eight-byte indirect load must fail feature validation. +var copy = File.ReadAllText(Path.Combine(root, "Copy", "Extracted.lean")); +var forgedCopy = IsDecodedMethod(copy, 0) + ? ForgeDecodedMethod(copy, 0, "code := method.code.set 1 (.ldind .i8)") + : copy.Replace(" .ldobj (valueType0),", " .ldind .i8,", StringComparison.Ordinal); +if (copy == forgedCopy) throw new InvalidOperationException("Missing span copy"); var loaded = Path.Combine(root, "Forged-copy.lean"); -File.WriteAllText(loaded, copy.Replace(copied, " .arg 0,\n .loadScalar 8,", StringComparison.Ordinal)); +File.WriteAllText(loaded, NarrowEvidence(forgedCopy, "method0_features_validated")); rejected.Add(loaded, "PASS span copy evidence rejection", "FeatureFlowValid", "is false"); rejected.Check(); @@ -222,7 +228,7 @@ .ok [.scalar (.i64 0)] := by rfl foreach (var (mutation, marker) in new[] { ("store", "Unsupported store to a span home"), - ("reset", "Unsupported store to a span home"), + ("reset", "Unsupported span reset"), ("rebind", "Unsupported store to or address of a readonly span reference"), ("address", "Unsupported store to or address of a readonly span reference"), ("writable", "Unsupported writable by-reference span parameter"), diff --git a/Tests/SpanTables.cs b/Tests/SpanTables.cs index fed38374c..627595595 100644 --- a/Tests/SpanTables.cs +++ b/Tests/SpanTables.cs @@ -2,10 +2,16 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include RejectedEvidence.cs #:include GeneratedEvidence.cs #:include ../scripts/BatchVerification.cs +#:include ExceptionMembers.cs using System.Reflection; using System.Text.RegularExpressions; @@ -19,6 +25,7 @@ var rejected = new RejectedEvidence(root); var queue = new GeneratedQueue(verification, assembly); var runtime = Assembly.Load(File.ReadAllBytes(assembly)).GetType("Examples.SpanTables")!; +using var metadata = ModuleDefinition.ReadModule(assembly); RequireLeanFixtures("CIL.Extraction.Translation", "CIL.Safety.Execution"); string Scalar(object value) => value switch @@ -32,8 +39,18 @@ void Prove(string dll, string selector, string folder, IReadOnlyList cla string control = "") { var proof = Path.Combine(root, folder + ".lean"); - File.WriteAllText(proof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\ntheorem correctness :\n" + - string.Join(" ∧\n", claims) + " := by\n and_intros <;> rfl\nend ExampleProof\n" + control); + var claimsText = string.Join(" \u2227\n", claims); + File.WriteAllText(proof, $$""" + import Extracted + import CIL.Safety.Execution + namespace ExampleProof + theorem correctness : + {{claimsText}} := by + and_intros <;> rfl + #print axioms correctness + end ExampleProof + {{control}} + """); queue.Add(new(selector, proof, Path.Combine(root, folder), Assembly: dll), passed); } @@ -49,36 +66,47 @@ void Prove(string dll, string selector, string folder, IReadOnlyList cla var folder = Path.Combine(root, name); Run(Extract("extract", assembly, "Examples", selector, folder)); var source = File.ReadAllText(Path.Combine(folder, "Extracted.lean")); - var instructions = ExtractedInstructions(source); - var site = instructions.FirstOrDefault(instruction => - Regex.IsMatch(instruction.Op, @"\A\.memory \(\.spanItem \d\)\z")); + var code = metadata.GetType("Examples.SpanTables").Methods.Single(method => method.Name == name).Body.Instructions; + var indexer = code.SingleOrDefault(instruction => instruction.OpCode.Code == Code.Call && + instruction.Operand is MethodReference { Name: "get_Item" }); var claims = inputs.Select(input => { try { return Claim(assembly, name, input, $".ok [{Scalar(runtime.GetMethod(name)!.Invoke(null, [.. input.Cast()])!)}]"); } - catch (TargetInvocationException error) when (site is not null) + catch (TargetInvocationException error) when (indexer is not null) { - return Claim(assembly, name, input, $".error ⟨{site.Method}, {site.Pc}, .thrown \"{error.InnerException!.GetType().FullName}\"⟩"); + string member = ExceptionMember(error.InnerException!); + return Claim(assembly, name, input, $".error ⟨0, {code.IndexOf(indexer)}, .thrown .{member}⟩"); } }).ToList(); // The RVA descriptor carries the declared packing Roslyn aligned the table to. var packing = name is "Word" or "Count" ? 4 : 8; - if (!source.Contains($"alignment := {packing} }})", StringComparison.Ordinal)) + if (!source.Contains($"alignment := {packing} }}", StringComparison.Ordinal)) throw new InvalidOperationException($"{name}: missing static data alignment {packing}"); - var folded = instructions.FirstOrDefault(instruction => instruction.Op == ".nop"); - var obligation = $"method{folded?.Method}_features_validated"; - var control = folded is not null - ? CheckedEvidenceControl(source, NarrowEvidence(source, obligation), [obligation], 0) : ""; + var store = code.SingleOrDefault(instruction => instruction.OpCode.Code == Code.Stloc_0); + if (name != "First" && store?.Next.OpCode.Code != Code.Ldloca_S) + throw new InvalidOperationException($"{name}: missing span receiver store and address"); + const string obligation = "method0_features_validated"; + var control = store is null ? "" : + CheckedEvidenceControl(source, NarrowEvidence(source, obligation), [obligation], 0); + if (store is not null) + { + var pc = code.IndexOf(store); + claims.Add($"Extracted.method0.code[{pc}]? = some (.stloc 0)"); + claims.Add($"Extracted.method0.code[{pc + 1}]? = some (.ldloca 0)"); + } Prove(assembly, selector, name + "-Verified", claims, () => Console.WriteLine($"PASS {name}: {claims.Count} runtime comparisons"), control); - // The receiver fold is bound by the feature certificate, independently of the instruction lemmas. - if (folded is null) continue; + // The feature certificate binds each receiver instruction to its raw opcode. + if (store is null) continue; var forged = Path.Combine(root, name + "-forged.lean"); - File.WriteAllText(forged, NarrowEvidence(source.Replace(".nop", ".localAddr 0"), obligation)); + var narrowed = NarrowEvidence(source, obligation); + File.WriteAllText(forged, narrowed.Replace("method0Reachable method0.code", + $"method0Reachable (method0.code.set {code.IndexOf(store)} .nop)", StringComparison.Ordinal)); rejected.Add(forged, $"PASS {name}: forged span receiver rejected by the feature certificate", diagnostic => Regex.IsMatch(diagnostic, - @"Extraction\.FeatureFlowValid rawLayouts\.toArray \(Extraction\.evidenceConstants methodEvidence\) " + - @"method\d+Closed"), "is false"); + @"Extraction\.FeatureFlowValid rawLayouts\.toArray \(Extraction\.evidenceConstants methodEvidence\)\s+" + + @"\(Extraction\.linkIndex method\d+\)\s+method\d+Closed"), "is false"); } // The proved value comes from the assembly's RVA blob through ldtoken. @@ -96,13 +124,8 @@ void Prove(string dll, string selector, string folder, IReadOnlyList cla Prove(tampered, "SpanTables::Word(int)", "Tampered", [Claim(tampered, "Word", [0], ".ok [.scalar (.i32 1891)]")], () => Console.WriteLine("PASS tampered RVA blob changes the proved word")); -// Reused locals and computed indexes retain storage; entering an erased receiver is invalid. -foreach (var (mutation, marker) in new[] -{ - ("second-read", ""), - ("computed-index", ""), - ("interior-branch", "Branch into a folded span receiver") -}) +// Reused locals and computed indexes retain typed span storage. +foreach (var mutation in new[] { "second-read", "computed-index" }) { var output = Path.Combine(root, "Reject-" + mutation); Directory.CreateDirectory(output); @@ -125,24 +148,13 @@ void Prove(string dll, string selector, string folder, IReadOnlyList cla il.InsertAfter(code[3], il.Create(OpCodes.Ldc_I4_0)); il.InsertAfter(code[4], il.Create(OpCodes.Add)); break; - case "interior-branch": - il.InsertBefore(code[1], il.Create(OpCodes.Br, code[2])); - break; } module.Write(dll); } - if (marker.Length == 0) - { - var expected = Scalar(runtime.GetMethod("Word")!.Invoke(null, [0])!); - Prove(dll, "SpanTables::Word(int)", "Supported-" + mutation, - [Claim(dll, "Word", [0], $".ok [{expected}]")], - () => Console.WriteLine("PASS stored static span: " + mutation)); - continue; - } - var result = Execute(Extract("extract", dll, "Examples", "SpanTables::Word(int)", output)); - if (result.Code == 0 || File.Exists(Path.Combine(output, "Extracted.lean")) || !result.Output.Contains(marker)) - throw new InvalidOperationException("Incorrect span rejection: " + mutation + "\n" + result.Output); - Console.WriteLine("PASS rejection: " + mutation); + var expected = Scalar(runtime.GetMethod("Word")!.Invoke(null, [0])!); + Prove(dll, "SpanTables::Word(int)", "Supported-" + mutation, + [Claim(dll, "Word", [0], $".ok [{expected}]")], + () => Console.WriteLine("PASS stored static span: " + mutation)); } // CoreCLR's CreateSpan throws on data not 4-byte aligned; packing 2 promises only 2 bytes. const string misalignedSpan = "Typed span element width does not divide the field alignment"; @@ -164,13 +176,16 @@ void Prove(string dll, string selector, string folder, IReadOnlyList cla } Run(Extract("extract", narrowDll, "Examples", "SpanTables::Word(int)", narrowFolder)); var narrowSource = File.ReadAllText(Path.Combine(narrowFolder, "Extracted.lean")); -var createSite = ExtractedInstructions(narrowSource) - .FirstOrDefault(instruction => instruction.Op == ".memory (.createSpan 4)"); -if (createSite is null || !narrowSource.Contains("alignment := 2 })", StringComparison.Ordinal)) - throw new InvalidOperationException("Narrow packing did not reach the extracted CreateSpan site"); +var getterIndex = DirectLinks(narrowSource, 1).Single(); +var getterCode = metadata.GetType("Examples.SpanTables").Methods.Single(method => method.Name == "get_Words") + .Body.Instructions; +var createPc = getterCode.IndexOf(getterCode.Single(instruction => + instruction.Operand is GenericInstanceMethod { Name: "CreateSpan" })); +if (getterIndex.Method != 0 || !IsDecodedMethod(narrowSource, 1) || + !narrowSource.Contains("alignment := 2 }", StringComparison.Ordinal)) + throw new InvalidOperationException("Narrow packing did not reach the decoded span getter"); Prove(narrowDll, "SpanTables::Word(int)", "Narrow-packing-Verified", - [Claim(narrowDll, "Word", [0], $".error ⟨{createSite.Method}, {createSite.Pc}, " + - $".unsupported \"{misalignedSpan}\"⟩")], + [Claim(narrowDll, "Word", [0], $".error \u27e81, {createPc}, .unsupported \"{misalignedSpan}\"\u27e9")], () => Console.WriteLine("PASS CreateSpan over a narrower packing is unsupported, never a success")); // An RVA off its declared packing is rejected before extraction; Cecil re-aligns, so patch the table. diff --git a/Tests/StableNames.cs b/Tests/StableNames.cs new file mode 100644 index 000000000..f6d728275 --- /dev/null +++ b/Tests/StableNames.cs @@ -0,0 +1,45 @@ +using System.Text.RegularExpressions; + +/// +/// Names of methods that survive unrelated edits: a method written in source, or the source method a generated +/// one belongs to. +/// +static partial class StableNames +{ + /// + /// itself for a method written in source. A compiler-generated method + /// (closure, lambda, local function, iterator or async state machine) is named by the source method + /// it was generated for, since the compiler numbers it by positions that shift with unrelated edits. + /// Generated top-level types share one name, as their numbers count across the assembly. + /// + internal static string Of(string signature) + { + if (signature.EndsWith(Generated, StringComparison.Ordinal)) return signature; + var member = signature[(signature.IndexOf(' ', StringComparison.Ordinal) + 1)..]; + var separator = member.IndexOf("::", StringComparison.Ordinal); + var declaring = member[..separator]; + if (declaring.StartsWith('<')) return GeneratedTypes; + var nested = declaring.IndexOf("/<", StringComparison.Ordinal); + var method = member[(separator + 2)..]; + if (nested < 0 && !method.StartsWith('<')) return signature; + var outer = nested < 0 ? declaring : declaring[..nested]; + var source = SourceName.Match((nested < 0 ? "" : declaring[nested..]) + "::" + method).Groups["name"].Value; + return source.Length == 0 ? $"{outer} closure classes{Generated}" : $"{outer}::{source}{Generated}"; + } + + /// The suffix of every name that stands for compiler-generated methods. + const string Generated = " (compiler-generated)"; + + /// The one name of every method of a compiler-generated top-level type. + const string GeneratedTypes = "top-level types" + Generated; + + /// How many times each name occurs. + internal static Dictionary Counts(IEnumerable names) => + names.GroupBy(name => name, StringComparer.Ordinal) + .ToDictionary(group => group.Key, group => group.Count(), StringComparer.Ordinal); + + // The first nonempty bracketed name, which the compiler sets to the method it generated code for; + // a closure class's own constructor has none and counts toward its outer type's closure classes. + [GeneratedRegex(@"<(?[^<>]+)>")] + private static partial Regex SourceName { get; } +} diff --git a/Tests/StackAllocation.cs b/Tests/StackAllocation.cs index b2e6c836a..3edc02e99 100644 --- a/Tests/StackAllocation.cs +++ b/Tests/StackAllocation.cs @@ -1,6 +1,12 @@ #!/usr/bin/env dotnet #:property PublishAot=false +#:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include GeneratedEvidence.cs @@ -66,26 +72,47 @@ if (name == "fill") { - const string span = ".spanFromReference 4 true"; - if (!source.Contains(span, StringComparison.Ordinal)) throw new Exception("Missing span constructor"); - foreach (var replacement in new[] { ".spanFromReference 8 true", ".spanFromReference 4 false" }) + using var module = Mono.Cecil.ModuleDefinition.ReadModule(runtimeFixture.Assembly.Location); + var method = module.GetType("Examples.StackAllocation").Methods.Single(item => item.Name == "FillAndRead"); + var instructions = method.Body.Instructions; + var constructorPc = instructions.IndexOf(instructions.Single(instruction => + instruction.OpCode.Code == Mono.Cecil.Cil.Code.Newobj)); + // The generated model fact names the constructor's width and capability, and each forgery changes one. + foreach (var (operation, accepted) in new[] { - var forgedSpan = Path.Combine(root, "forged-span.lean"); - File.WriteAllText(forgedSpan, source.Replace(span, replacement, StringComparison.Ordinal)); - var rejectedSpan = Execute(["lake", "env", "lean", forgedSpan], cwd: FixturePackage()); - if (rejectedSpan.Code == 0 || !rejectedSpan.Output.Contains("ScalarFlowValid", StringComparison.Ordinal)) - throw new Exception("Forged span constructor was not rejected: " + rejectedSpan.Output); - Console.WriteLine("PASS forged stack span rejected: " + replacement); + (".spanFromReference 4 true", true), (".spanFromReference 8 true", false), + (".spanFromReference 4 false", false) + }) + { + var stated = Path.Combine(root, "stated-span.lean"); + File.WriteAllText(stated, ForgeModelFact(source, constructorPc, $".memory ({operation})")); + var checkedFact = Execute(["lake", "env", "lean", stated], cwd: FixturePackage()); + var passed = accepted + ? checkedFact.Code == 0 && !checkedFact.Output.Contains("sorryAx", StringComparison.Ordinal) + : checkedFact.Code != 0 && ModelFactMismatch(constructorPc)(checkedFact.Output); + if (!passed) throw new Exception($"Stack span model fact {operation}: {checkedFact.Output}"); + Console.WriteLine($"PASS stack span model fact {(accepted ? "accepted" : "refused")}: {operation}"); } + // The stack span constructor must retain its original opcode. + var forgedSpan = Path.Combine(root, "forged-span.lean"); + File.WriteAllText(forgedSpan, + ForgeConstructorOpcode(source, constructorPc)); + var rejectedSpan = Execute(["lake", "env", "lean", forgedSpan], cwd: FixturePackage()); + if (rejectedSpan.Code == 0 || !rejectedSpan.Output.Contains("RuntimeModelsAgree", StringComparison.Ordinal)) + throw new Exception($"Substituted stack span instruction was not refused: {rejectedSpan.Output}"); + Console.WriteLine("PASS substituted stack span instruction refused"); } if (name != "uninitialized") continue; - const string operation = ".stackAlloc false"; - if (!source.Contains(operation, StringComparison.Ordinal)) throw new Exception("Missing uninitialized allocation"); + // The method is defined from its decoder, so a method that claims localsinit is a forged decoder's value, which + // no longer matches the closed definition's declared signature. + if (!DecodedOriginal(source, 0).Contains("initLocals := false,", StringComparison.Ordinal)) + throw new Exception("Missing uninitialized allocation"); var forged = Path.Combine(root, "forged-initialization.lean"); - File.WriteAllText(forged, source.Replace(operation, ".stackAlloc true", StringComparison.Ordinal)); + File.WriteAllText(forged, + ForgeDecodedMethod(source, 0, "declared := { method.declared with initLocals := true }")); var rejected = Execute(["lake", "env", "lean", forged], cwd: FixturePackage()); - if (rejected.Code == 0 || !rejected.Output.Contains("FeatureFlowValid", StringComparison.Ordinal)) + if (rejected.Code == 0 || !rejected.Output.Contains("DeclaredValid", StringComparison.Ordinal)) throw new Exception("Forged localsinit was not rejected by the Lean certificate: " + rejected.Output); Console.WriteLine("PASS forged stack initialization rejected"); } diff --git a/Tests/StackAllocationEvidence.lean b/Tests/StackAllocationEvidence.lean index ec201b0e3..00771dac5 100644 --- a/Tests/StackAllocationEvidence.lean +++ b/Tests/StackAllocationEvidence.lean @@ -1,4 +1,6 @@ import CIL.Extraction.Scalars +import CIL.Extraction.Translation +import CIL.Safety.Execution /-! # Independent certificate rejection of forged stack allocation metadata -/ @@ -11,20 +13,51 @@ private def raw : RawMethod := { returnType := .void, parameters := [], locals := [], initLocals := false maxStack := 1, genericParameters := [], code := [⟨0, 65039, .none⟩] } +/-- The emitted body: `localloc` under the signature the raw method declares. -/ +private def emitted : Method := + { code := [.localloc], locals := [], returnsValue := false, declared := .ofRaw raw } + +/-- The emitted body's shape and declared signature, localsinit flag included, are the raw + method's. -/ +theorem shape_matches_metadata : MethodShapeValid raw emitted ∧ DeclaredValid raw emitted := by + decide +#print axioms shape_matches_metadata + /-- Raw uninitialized storage cannot be emitted as initialized storage. -/ theorem forged_initialization_rejected : - featureOpMatches #[] raw ⟨0, 65039, .none⟩ (.stackAlloc true) = false := by decide +kernel + ¬ DeclaredValid raw + { emitted with declared := { emitted.declared with initLocals := true } } := by decide #print axioms forged_initialization_rejected -/-- An initialized raw method admits its matching flag. -/ -theorem initialization_matches_metadata : - featureOpMatches #[] { raw with initLocals := true } ⟨0, 65039, .none⟩ (.stackAlloc true) = - true := by decide +kernel -#print axioms initialization_matches_metadata +/-- Empty memory with a stack budget, for stepping `emitted`. -/ +private def memory : CIL.Safety.Memory := { + allocations := fun _ => none + cells := fun _ _ => ⟨0, false⟩ + views := [] + nextIdentity := 0 + stackLimit := 32 } + +/-- A frame with no locals or owned storage. -/ +private def frame : CIL.Safety.Frame := { activation := 0, locals := [], owned := [] } + +/-- `localloc` faults unless the byte count is the only stack item: a size with another item + below it and an empty stack both fault before allocating. -/ +theorem malformed_stack_faults : + CIL.Safety.step emitted .localloc 0 [] frame + ([.scalar (.i32 1), .scalar (.i32 4)] : List CIL.Safety.Value) memory = + .error .invalidState ∧ + CIL.Safety.step emitted .localloc 0 [] frame [] memory = .error .invalidState := by + constructor <;> rfl +#print axioms malformed_stack_faults + +/-- The raw opcode admits the emitted `localloc`. -/ +theorem allocation_matches_opcode : + featureOpMatches #[] raw ⟨0, 65039, .none⟩ .localloc = true := by decide +kernel +#print axioms allocation_matches_opcode /-- Another opcode cannot inject a dynamic allocation into the emitted program. -/ theorem injected_allocation_rejected : - featureOpMatches #[] raw ⟨0, 0, .none⟩ (.stackAlloc false) = false := by decide +kernel + featureOpMatches #[] raw ⟨0, 0, .none⟩ .localloc = false := by decide +kernel #print axioms injected_allocation_rejected /-- localloc requires the size to be the only stack item. -/ diff --git a/Tests/StandaloneCertificateReuse.cs b/Tests/StandaloneCertificateReuse.cs index ddb51ae56..3a339ac66 100644 --- a/Tests/StandaloneCertificateReuse.cs +++ b/Tests/StandaloneCertificateReuse.cs @@ -1,8 +1,14 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ConsumerTraceDirectory.cs +// Standalone measurement of complete CLI requests against a certified library; no suite runs it. using System.Diagnostics; using System.Text.Json; @@ -18,7 +24,9 @@ import Extracted namespace ExampleProof /-- The emitted program satisfies its complete translation evidence. -/ theorem correctness : CIL.Extraction.TranslationValid Extracted.rawLayouts Extracted.computedLayouts - Extracted.methodEvidence Extracted.imports Extracted.program := Extracted.translation_valid + Extracted.assemblies Extracted.platformDefinitions Extracted.methodEvidence Extracted.imports + Extracted.program := + Extracted.translation_valid #print axioms correctness end ExampleProof """); @@ -50,6 +58,13 @@ void Verify(string label, int expectedCompilations, params string[] options) "Joint kernel replay of consumer proof and dependencies").ToArray(); if (expectedCompilations > 0 && replays.Length == 0) throw new InvalidOperationException("Missing independent kernel replay: " + label); + var audits = stages.Where(stage => stage.GetProperty("command") is var command && + command.GetArrayLength() == 3 && + Path.GetFileNameWithoutExtension(command[0].GetString()) == "leancil-audit" && + command[1].GetString()!.EndsWith(".json", StringComparison.Ordinal) && + command[2].GetString()!.EndsWith(".result.json", StringComparison.Ordinal)).ToArray(); + if (audits.Length == 0) + throw new InvalidOperationException("Missing fresh theorem and extraction audit: " + label); measurements.Add(new { label, seconds = timer.Elapsed.TotalSeconds, frontendProcesses = compilations.Length, frontendSeconds = compilations.Sum(stage => stage.GetProperty("durationSeconds").GetDouble()) }); diff --git a/Tests/StaticFieldSites.lean b/Tests/StaticFieldSites.lean new file mode 100644 index 000000000..2418ae43c --- /dev/null +++ b/Tests/StaticFieldSites.lean @@ -0,0 +1,474 @@ +import CIL.Extraction.DecodedMethod +import Tests.BodyEncoding + +/-! +# Static sites bound to the field their token names + +`T` reads the static `Y` with `ldsfld`, `ldsflda` or a field `ldtoken`, or stores it with +`stsfld`. `Y` and `X` are either fields of `T`'s own `Fixture`, which the carried slice defines, +or member references into `Ext.Point` of an assembly no slice carries. The static site at the +access carries the descriptor of `X`, so the model would use `X`'s storage for `Y`: every such +body is refused, while the carried `Y` with its own descriptor is a valid translation for a load +and an address. A store, or a token outside an RVA site, decodes to a hole and is refused with +either descriptor. Of the fields of types no slice carries, only `BitConverter.IsLittleEndian` is +admitted, and only as the `ldsfld` the feature check reads as a constant. + +A static of the generic `Box`1` named through `Box` is refused for each instruction, since +each closed instantiation has its own storage. Across the program an identity names one field and +a field has one identity: two fields read under one identity, in one method or in two, and one +field read under two identities are refused. +-/ + +namespace Tests.StaticFieldSites + +open CIL CIL.Extraction Tests.MetadataBuilder Tests.BodyEncoding + +private def pin : AssemblyPin := + { identity := "T, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } +private def extScope : String := "Ext, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" +private def int32 : TypeExpr := .atom runtimeScope "System.Int32" + +/-- The static field instructions under test. -/ +private def ldsfld : Nat := 0x7E +private def ldsflda : Nat := 0x7F +private def stsfld : Nat := 0x80 +private def ldtoken : Nat := 0xD0 + +/-- The owner of `X` and `Y`: `T`'s `Fixture` when `carried`, else `Ext.Point`. -/ +private def owner (carried : Bool) : TypeExpr := + if carried then .atom pin.identity "Fixture" else .object extScope "Ext.Point" + +/-- `X` and `Y` as the instruction names them; a carried static counts from its owner's first + field. -/ +private def xField (carried : Bool) : FieldRef := + { owner := owner carried, index := 0, type := int32, name := name!"X" } +private def yField (carried : Bool) : FieldRef := + { owner := owner carried, index := if carried then 1 else 0, type := int32, name := name!"Y" } + +/-- The signature bytes of the result of the method using `opcode`: the loaded `int`, a + reference to it, or nothing. -/ +private def resultBytes (opcode : Nat) : List Nat := + if opcode == ldsfld then [0x08] else if opcode == ldsflda then [0x10, 0x08] else [0x01] + +/-- The tokens of `T`'s world: `Y`, `IsLittleEndian`, `X` and `V` of `Box` (0 when absent). -/ +private structure Tokens where + y : Nat + endian : Nat + x : Nat + boxed : Nat + +/-- The generic class `Box`1` of `T`, whose fields start at row `fieldList`, with its parameter + and the static `V : int`, and a reference to `V` through `Box`, whose token it returns. -/ +private def boxWorld (fieldList intField : Nat) : Step Nat := do + let boxName ← string "Box`1" + let box ← row Table.typeDef [1, boxName, 0, 1 * 4 + 1, fieldList, 2] + let parameterName ← string "T" + let parameter ← row Table.genericParam [0, 0, (box - 0x02000000) * 2, parameterName] + link linkGenericParams box 0 parameter + let valueName ← string "V" + let value ← row Table.field [0x0016, valueName, intField] + link linkDeclaring value 0 box + let boxOfInt ← row Table.typeSpec [← blob [0x15, 0x12, (box - 0x02000000) * 4, 1, 0x08]] + let reference ← row Table.memberRef [(boxOfInt - 0x1B000000) * 8 + 4, valueName, intField] + link linkDefinition reference 0 value + pure reference + +/-- `T` with `static M()`, the fields `X` and `Y` of `Fixture` and `Box` when `carried` or + references to those of `Ext.Point` otherwise, and a reference to + `BitConverter.IsLittleEndian`. -/ +private def world (carried : Bool) (result : List Nat) : Step Tokens := do + let runtimeName ← string "System.Runtime" + let extName ← string "Ext" + let publicKeyToken ← blob [0xB0, 0x3F, 0x5F, 0x7F, 0x11, 0xD5, 0x0A, 0x3A] + let _ ← row Table.assemblyRef [11, 0, 0, 0, 0, publicKeyToken, runtimeName, 0, 0] + let _ ← row Table.assemblyRef [1, 0, 0, 0, 0, 0, extName, 0, 0] + modify fun builder => { builder with corlib := 1 } + let objectName ← string "Object" + let systemName ← string "System" + let _ ← row Table.typeRef [1 * 4 + 2, objectName, systemName] + let pointName ← string "Point" + let pointRef ← row Table.typeRef [2 * 4 + 2, pointName, extName] + let converterName ← string "BitConverter" + let converterRef ← row Table.typeRef [1 * 4 + 2, converterName, systemName] + let moduleName ← string "" + let _ ← row Table.typeDef [0, moduleName, 0, 0, 1, 1] + let fixtureName ← string "Fixture" + let fixture ← row Table.typeDef [1, fixtureName, 0, 1 * 4 + 1, 1, 1] + let xName ← string "X" + let yName ← string "Y" + let intField ← blob [0x06, 0x08] + let staticFlags := 0x0016 + let fields ← if carried then do + let x ← row Table.field [staticFlags, xName, intField] + let y ← row Table.field [staticFlags, yName, intField] + pure [x, y] + else pure [] + let boxed ← if carried then boxWorld 3 intField else pure 0 + let methodName ← string "M" + let methodBlob ← blob ([0x00, 0x00] ++ result) + let definition ← row Table.methodDef [0, 0, 0, methodName, methodBlob, 1] + link linkDeclaring definition 0 fixture + for field in fields do link linkDeclaring field 0 fixture + let pointParent := (pointRef - 0x01000000) * 8 + 1 + let xRef ← row Table.memberRef [pointParent, xName, intField] + let yRef ← row Table.memberRef [pointParent, yName, intField] + let endianName ← string "IsLittleEndian" + let booleanField ← blob [0x06, 0x02] + let endian ← row Table.memberRef [(converterRef - 0x01000000) * 8 + 1, endianName, booleanField] + pure { y := fields.getLast?.getD yRef, endian, x := fields.headD xRef, boxed } + +private def slice (carried : Bool) (result : List Nat) : MetadataSlice × Tokens := + build (world carried result) (bytesOf "T") pin.identity + +/-- The platform definitions the world names: `Ext.Point` as a class and `BitConverter`, whose + `IsLittleEndian` is the platform's byte order. -/ +private def sitePlatform : PlatformTable := platform_table% + [runtimeType "Object" .objectRoot, runtimeType "BitConverter" .byteOrder, + { scope := bytesOf extScope, parts := [bytesOf "Ext", bytesOf "Point"], kind := .classType }] + +/-- The static operand of `field`. -/ +private def operandOf (field : FieldRef) : RawOperand := + .field field.owner field.index field.type field.name true + +/-- The raw body `opcode field; ret`, with `ldc.i4.1` before a store and `pop` after a token. -/ +private def raw (opcode : Nat) (field : FieldRef) (returnType : TypeExpr) : RawMethod := + let access := operandOf field + { token := 0x06000001, owner := pin.identity ++ "::Fixture", hasThis := false, + explicitThis := false, callingConvention := 0, attributes := 0, implementation := 0, + returnType, parameters := [], locals := [], initLocals := false, maxStack := 8, + genericParameters := [], + code := + if opcode == stsfld then [⟨0, 0x17, .none⟩, ⟨1, stsfld, access⟩, ⟨6, 0x2A, .none⟩] + else if opcode == ldtoken then [⟨0, ldtoken, access⟩, ⟨5, 0x26, .none⟩, ⟨6, 0x2A, .none⟩] + else [⟨0, opcode, access⟩, ⟨5, 0x2A, .none⟩] } + +/-- The bytes and token table of `raw` with `token` at the access. -/ +private def body (opcode : Nat) (field : FieldRef) (returnType : TypeExpr) (world : MetadataSlice) + (token : Nat) : BodyEvidence := + let encoded := MetadataBuilder.littleEndian 4 token + let bytes := + if opcode == stsfld then [7 * 4 + 2, 0x17, stsfld] ++ encoded ++ [0x2A] + else if opcode == ldtoken then [7 * 4 + 2, ldtoken] ++ encoded ++ [0x26, 0x2A] + else [6 * 4 + 2, opcode] ++ encoded ++ [0x2A] + evidenceOf bytes [(token, operandOf field)] { own := world } (raw opcode field returnType) + sitePlatform + +/-- The result type of the method using `opcode` on an `int`. -/ +private def returnType (opcode : Nat) : TypeExpr := + if opcode == ldsfld then int32 else if opcode == ldsflda then .byref int32 else .void + +/-- The body accessing `Y` of `owner carried` with `opcode`; the decoder records the access of + a carried field. -/ +private def yBody (carried : Bool) (opcode : Nat) : BodyEvidence := + let world := slice carried (resultBytes opcode) + { body opcode (yField carried) (returnType opcode) world.1 world.2.y with + statics := if carried then [(if opcode == stsfld then 1 else 0, world.2.y)] else [] } + +private def unknown (count : Nat) : Option FeatureState := some ⟨List.replicate count none, []⟩ + +/-- The raw static sites that key each pc's descriptor, as the extractor records them. -/ +private def sitesOf (sites : List (Nat × StaticDescriptor)) : List (Nat × StaticFieldSite) := + sites.map fun (pc, descriptor) => (pc, ⟨descriptor, none⟩) + +/-- The evidence of the method accessing `Y`, its raw sites keyed by `sites`: the stack holds the + loaded value or its address after a load, the stored value before a store and the handle after + a token. -/ +private def evidence (carried : Bool) (opcode : Nat) (sites : List (Nat × StaticDescriptor)) : + MethodEvidence := + let three := opcode == stsfld || opcode == ldtoken + let pushed : ScalarKind := + if opcode == ldsfld || opcode == stsfld then .word32 + else if opcode == ldsflda then .reference else .other + let operations : List ArgumentOp := List.replicate (if three then 3 else 2) .other + let original := { raw opcode (yField carried) (returnType opcode) with + staticFields := sitesOf sites } + { original, body := yBody carried opcode, arguments := [], closed := original, + parameterInput := { arguments := [], locals := [], code := operations }, + reachable := List.replicate (if three then 3 else 2) true, + featureStates := if three then [unknown 0, unknown 1, unknown 0] else [unknown 0, unknown 1], + scalarStates := if three then [some [], some [pushed], some []] else [some [], some [pushed]], + layoutUses := [], assembly := pin, + signature := (spelled? (yBody carried opcode).names.signature).getD "" } + +/-- The descriptor of the static readonly `field`, which its constructor sets to `value`, keyed by + `identity`. -/ +private def descriptorOf (field : FieldRef) (identity value : Nat) : StaticDescriptor := + let initializer : StaticInitializer := + { method := 0, literal := some (.i32 (BitVec.ofNat 32 value)), arguments := [] } + { identity, field, bytes := [BitVec.ofNat 8 value, 0, 0, 0], scalar := some false, + initializer := some initializer } + +/-- The emitted method accessing `Y` with `opcode`, whose static site carries `descriptor`. -/ +private def method (carried : Bool) (opcode : Nat) (descriptor : StaticDescriptor) : Method := + let field := yField carried + let code : List Instruction := + if opcode == ldsfld then [.ldsfld field, .ret] + else if opcode == ldsflda then [.ldsflda field, .ret] + else if opcode == stsfld then [.ldc_i4 1, .stsfld field, .ret] + else [.ldtoken (.field field), .pop, .ret] + { locals := [], returnsValue := opcode == ldsfld || opcode == ldsflda, + declared := DeclaredSignature.ofRaw (raw opcode field (returnType opcode)), code, + staticSites := [(if opcode == stsfld then 1 else 0, descriptor)] } + +/-- The method with the site of `Y` carrying `X`'s descriptor or `Y`'s own; the two differ only in + the field they name. -/ +private def withX (carried : Bool) (opcode : Nat) : Method := + method carried opcode (descriptorOf (xField carried) 2 2) +private def withY (carried : Bool) (opcode : Nat) : Method := + method carried opcode (descriptorOf (yField carried) 2 2) + +/-- Whether the extraction of one method accessing `Y`, its evidence recording the emitted + method's static sites, is a valid translation. -/ +private abbrev valid (carried : Bool) (opcode : Nat) (emitted : Method) : Prop := + TranslationValid [] #[] [] sitePlatform [evidence carried opcode emitted.staticSites] [] [emitted] + +/-! ## A carried owner -/ + +/-- Each load of the carried `Y` or of its address is a valid translation with `Y`'s own + descriptor at its site. -/ +theorem carried_access_with_own_descriptor_accepted : + valid true ldsfld (withY true ldsfld) ∧ valid true ldsflda (withY true ldsflda) := by + decide +kernel +#print axioms carried_access_with_own_descriptor_accepted + +/-- A store to the carried `Y`, or its token outside an RVA site, decodes to a hole, so even + with `Y`'s own descriptor at its site the method is no valid translation. -/ +theorem carried_store_and_token_rejected : + ¬ valid true stsfld (withY true stsfld) ∧ ¬ valid true ldtoken (withY true ldtoken) := by + decide +kernel +#print axioms carried_store_and_token_rejected + +/-- Each access to the carried `Y` whose site carries `X`'s descriptor is no valid translation. -/ +theorem carried_access_with_descriptor_of_x_rejected : + ¬ valid true ldsfld (withX true ldsfld) ∧ ¬ valid true ldsflda (withX true ldsflda) ∧ + ¬ valid true stsfld (withX true stsfld) ∧ ¬ valid true ldtoken (withX true ldtoken) := by + decide +kernel +#print axioms carried_access_with_descriptor_of_x_rejected + +/-! ## An owner no slice carries -/ + +/-- Whether decoding the body accessing `Y` of `Ext.Point` fails on the refusal of its token. -/ +private def refusedAtY (opcode : Nat) : Bool := + match checkBody (yBody false opcode) (raw opcode (yField false) (returnType opcode)) [] with + | .error (.metadata (.unboundStaticField token)) => + token == (slice false (resultBytes opcode)).2.y + | _ => false + +/-- Each access to `Y` of `Ext.Point` fails on its token, whichever descriptor its site + carries, and is no valid translation. -/ +theorem uncarried_access_rejected : + ([ldsfld, ldsflda, stsfld, ldtoken].all refusedAtY) = true ∧ + ¬ valid false ldsfld (withX false ldsfld) ∧ ¬ valid false ldsflda (withX false ldsflda) ∧ + ¬ valid false stsfld (withX false stsfld) ∧ ¬ valid false ldtoken (withX false ldtoken) ∧ + ¬ valid false ldsfld (withY false ldsfld) ∧ ¬ valid false ldsflda (withY false ldsflda) ∧ + ¬ valid false stsfld (withY false stsfld) ∧ ¬ valid false ldtoken (withY false ldtoken) := by + decide +kernel +#print axioms uncarried_access_rejected + +/-! ## The platform constant field -/ + +/-- The result type of the method using `opcode` on `IsLittleEndian`, and its signature bytes. -/ +private def endianReturn (opcode : Nat) : TypeExpr := + if opcode == ldsfld then littleEndianField.type + else if opcode == ldsflda then .byref littleEndianField.type else .void +private def endianResult (opcode : Nat) : List Nat := + if opcode == ldsfld then [0x02] else if opcode == ldsflda then [0x10, 0x02] else [0x01] + +/-- The decoding of the body accessing `IsLittleEndian` with `opcode` under `platform`. -/ +private def endianDecoding (opcode : Nat) (platform := sitePlatform) : Option BodyError := + let world := slice false (endianResult opcode) + let decoded := body opcode littleEndianField (endianReturn opcode) world.1 world.2.endian + match checkBody { decoded with platform } (raw opcode littleEndianField (endianReturn opcode)) + [] with + | .ok _ => none + | .error found => some found + +/-- `IsLittleEndian` binds for `ldsfld`, and every other static field instruction fails on its + token. -/ +theorem endian_field_only_loaded : + endianDecoding ldsfld = none ∧ + ([ldsflda, stsfld, ldtoken].all fun opcode => match endianDecoding opcode with + | some (.mismatchedToken _ token) => token == (slice false (endianResult opcode)).2.endian + | _ => false) = true := by + decide +kernel +#print axioms endian_field_only_loaded + +/-- The reference to `BitConverter`, the third of the world. -/ +private def converterRef : Nat := 0x01000003 + +/-- `IsLittleEndian` binds only under a definition of `BitConverter` that states the byte order: + without the definition its owner's reference is refused, and as a plain class its token. -/ +theorem endian_field_needs_the_byte_order_definition : + endianDecoding ldsfld = none ∧ + endianDecoding ldsfld (removing sitePlatform "BitConverter") = + some (.metadata (.missingPlatformDefinition converterRef)) ∧ + endianDecoding ldsfld (restating sitePlatform "BitConverter" .classType) = + some (.metadata (.platformKindMismatch (slice false (endianResult ldsfld)).2.endian)) := by + decide +kernel +#print axioms endian_field_needs_the_byte_order_definition + +/-- The evidence of `ldsfld IsLittleEndian; ret`. -/ +private def endianEvidence : MethodEvidence := + let world := slice false (endianResult ldsfld) + let original := raw ldsfld littleEndianField (endianReturn ldsfld) + let decoded := body ldsfld littleEndianField (endianReturn ldsfld) world.1 world.2.endian + { original, body := decoded, arguments := [], closed := original, + parameterInput := { arguments := [], locals := [], code := [.other, .other] }, + reachable := [true, true], featureStates := [unknown 0, unknown 1], + scalarStates := [some [], some [.word32]], layoutUses := [], assembly := pin, + signature := (spelled? decoded.names.signature).getD "" } + +/-- The same body emitted as the profile's endianness constant, the form the feature check + admits, is a valid translation. -/ +theorem endian_field_as_constant_accepted : + TranslationValid [] #[] [] sitePlatform [endianEvidence] [] + [{ locals := [], returnsValue := true, + declared := DeclaredSignature.ofRaw endianEvidence.original, + code := [.ldc_i4 1, .ret] }] := by + decide +kernel +#print axioms endian_field_as_constant_accepted + +/-- A descriptor of `IsLittleEndian` whose constructor stores 0. -/ +private def endianDescriptor : StaticDescriptor := + { identity := 3, field := littleEndianField, bytes := [0], scalar := some false, + initializer := some { method := 0, literal := some (.i32 0), arguments := [] } } + +/-- `ldsfld IsLittleEndian` emitted as a static load, its site carrying a descriptor of the + field that holds 0, is no valid translation: the feature check admits only the constant. -/ +theorem endian_field_as_static_load_rejected : + ¬ TranslationValid [] #[] [] sitePlatform [endianEvidence] [] + [{ locals := [], returnsValue := true, + declared := DeclaredSignature.ofRaw endianEvidence.original, + code := [.ldsfld littleEndianField, .ret], + staticSites := [(0, endianDescriptor)] }] := by + decide +kernel +#print axioms endian_field_as_static_load_rejected + +/-! ## A static of a generic instantiation -/ + +/-- `V` of `Box` as an instruction names it. -/ +private def boxedField : FieldRef := + { owner := .apply (.objectConstructor pin.identity "Box`1") int32, index := 0, type := int32, + name := name!"V" } + +/-- Whether decoding the body accessing `V` of `Box` with `opcode` fails on the refusal of + a static named through an instantiation, at its token. -/ +private def refusedAsInstantiated (opcode : Nat) : Bool := + let world := slice true (resultBytes opcode) + match checkBody (body opcode boxedField (returnType opcode) world.1 world.2.boxed) + (raw opcode boxedField (returnType opcode)) [] with + | .error (.metadata (.instantiatedStaticField token)) => token == world.2.boxed + | _ => false + +/-- Each static field instruction on `V` of `Box` fails on its token, though a carried row + defines `V`: the model's one storage per field cannot stand for each instantiation's own. -/ +theorem instantiated_owner_static_rejected : + refusedAsInstantiated ldsfld = true ∧ refusedAsInstantiated ldsflda = true ∧ + refusedAsInstantiated stsfld = true ∧ refusedAsInstantiated ldtoken = true := by + decide +kernel +#print axioms instantiated_owner_static_rejected + +/-! ## One identity for one field -/ + +/-- The carried world of a method returning an `int`. -/ +private def intWorld : MetadataSlice × Tokens := slice true [0x08] + +/-- The raw body `ldsflda X; ldsflda Y; ceq; ret`, which compares the two fields' addresses. -/ +private def compareRaw : RawMethod := + { raw ldsflda (yField true) int32 with + code := [⟨0, ldsflda, operandOf (xField true)⟩, ⟨5, ldsflda, operandOf (yField true)⟩, + ⟨10, 0xFE01, .none⟩, ⟨12, 0x2A, .none⟩] } + +/-- The bytes, token table and recorded static accesses of `compareRaw`. -/ +private def compareBody : BodyEvidence := + let x := intWorld.2.x + let y := intWorld.2.y + let encoded := MetadataBuilder.littleEndian 4 + let bytes := [13 * 4 + 2, ldsflda] ++ encoded x ++ [ldsflda] ++ encoded y ++ [0xFE, 0x01, 0x2A] + let table := [(x, operandOf (xField true)), (y, operandOf (yField true))] + { evidenceOf bytes table { own := intWorld.1 } compareRaw sitePlatform with + statics := [(0, x), (1, y)] } + +/-- The emitted comparison, with the sites of `X` and `Y` keyed by the given identities. -/ +private def compareMethod (xIdentity yIdentity : Nat) : Method := + { locals := [], returnsValue := true, declared := DeclaredSignature.ofRaw compareRaw, + code := [.ldsflda (xField true), .ldsflda (yField true), .ceq, .ret], + staticSites := [(0, descriptorOf (xField true) xIdentity 0), + (1, descriptorOf (yField true) yIdentity 0)] } + +/-- The evidence of the comparison with its raw sites keyed as `compareMethod` keys them: two + references, then the `int` that compares them. -/ +private def compareEvidence (xIdentity yIdentity : Nat) : MethodEvidence := + let source := { compareRaw with + staticFields := sitesOf (compareMethod xIdentity yIdentity).staticSites } + { original := source, body := compareBody, arguments := [], closed := source, + parameterInput := { arguments := [], locals := [], code := List.replicate 4 .other }, + reachable := List.replicate 4 true, + featureStates := [unknown 0, unknown 1, unknown 2, unknown 1], + scalarStates := [some [], some [.reference], some [.reference, .reference], some [.word32]], + layoutUses := [], assembly := pin, + signature := (spelled? compareBody.names.signature).getD "" } + +/-- Keyed by one identity, `X` and `Y` would share one storage in the model and their addresses + would compare equal; the evidence check refuses that, and accepts two identities. -/ +theorem shared_identity_in_one_method_rejected : + ¬ StaticIdentitiesValid [compareMethod 1 1] [compareBody] ∧ + ¬ TranslationValid [] #[] [] sitePlatform [compareEvidence 1 1] [] [compareMethod 1 1] ∧ + TranslationValid [] #[] [] sitePlatform [compareEvidence 1 2] [] [compareMethod 1 2] := by + decide +kernel +#print axioms shared_identity_in_one_method_rejected + +/-- The method retains both supplied keys. The pinned program requires one key per field row. -/ +theorem decoded_static_keys_bound : + let check := fun key => + let expected := compareMethod 1 key + let item := compareEvidence 1 key + let decoded := Method.ofClosed? .scalar {} [] item.reachable item.closed item.parameterInput + let kept := match decoded with + | .ok method => method.staticSites == expected.staticSites + | .error _ => false + (kept, (decodedProgram? .scalar {} [item]).isSome) + check 2 = (true, true) ∧ check 1 = (true, false) := by + decide +kernel + +#print axioms decoded_static_keys_bound + +/-- The evidence of a method of its own that loads the carried `field` with `token`, its site + keyed by `identity`. -/ +private def loadEvidence (field : FieldRef) (token identity : Nat) : MethodEvidence := + let original := { raw ldsfld field int32 with + staticFields := sitesOf [(0, descriptorOf field identity 2)] } + let decoded := { body ldsfld field int32 intWorld.1 token with statics := [(0, token)] } + { original, body := decoded, arguments := [], closed := original, + parameterInput := { arguments := [], locals := [], code := [.other, .other] }, + reachable := [true, true], featureStates := [unknown 0, unknown 1], + scalarStates := [some [], some [.word32]], layoutUses := [], assembly := pin, + signature := (spelled? decoded.names.signature).getD "" } + +/-- The emitted `ldsfld field; ret`, its site keyed by `identity`. -/ +private def loadMethod (field : FieldRef) (identity : Nat) : Method := + { locals := [], returnsValue := true, + declared := DeclaredSignature.ofRaw (raw ldsfld field int32), + code := [.ldsfld field, .ret], staticSites := [(0, descriptorOf field identity 2)] } + +/-- A program of one load method per entry: the field, its token and the identity of its site. -/ +private abbrev loadsValid (loads : List (FieldRef × Nat × Nat)) : Prop := + TranslationValid [] #[] [] sitePlatform + (loads.map fun (field, token, identity) => loadEvidence field token identity) [] + (loads.map fun (field, _, identity) => loadMethod field identity) + +/-- Two methods may not key `X` and `Y` by one identity, and may key them by two. -/ +theorem shared_identity_across_methods_rejected : + ¬ loadsValid [(xField true, intWorld.2.x, 1), (yField true, intWorld.2.y, 1)] ∧ + loadsValid [(xField true, intWorld.2.x, 1), (yField true, intWorld.2.y, 2)] := by + decide +kernel +#print axioms shared_identity_across_methods_rejected + +/-- Two methods may not key `Y` by two identities, and may share its one identity. -/ +theorem one_field_with_two_identities_rejected : + ¬ loadsValid [(yField true, intWorld.2.y, 1), (yField true, intWorld.2.y, 2)] ∧ + loadsValid [(yField true, intWorld.2.y, 2), (yField true, intWorld.2.y, 2)] := by + decide +kernel +#print axioms one_field_with_two_identities_rejected + +end Tests.StaticFieldSites diff --git a/Tests/StaticScalarValues.cs b/Tests/StaticScalarValues.cs index 1ea7a841e..64284967a 100644 --- a/Tests/StaticScalarValues.cs +++ b/Tests/StaticScalarValues.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include RejectedEvidence.cs #:include GeneratedEvidence.cs @@ -104,9 +109,9 @@ void Reject(string name, string dll, string method, string marker, bool assume = var original = File.ReadAllText(Path.Combine(root, "ReadSignedByte", "Extracted.lean")); foreach (var (name, before, after, marker) in new[] { - ("signedness", "scalar := some true", "scalar := some false", "MethodShapeValid"), - ("width", "[(BitVec.ofNat 8 128)]", "[(BitVec.ofNat 8 128), (BitVec.ofNat 8 255)]", "MethodShapeValid"), - ("literal", "literal := some (.i32 4294967168)", "literal := some (.i32 127)", "StaticValuesValid") + ("signedness", "scalar := some true", "scalar := some false", "MethodDecodeError.liveHole"), + ("width", "[(BitVec.ofNat 8 128)]", "[(BitVec.ofNat 8 128), (BitVec.ofNat 8 255)]", "MethodDecodeError.liveHole"), + ("literal", "literal := some (.i32 4294967168)", "literal := some (.i32 127)", "MethodDecodeError.liveHole") }) { if (!original.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing tamper target: " + name); @@ -135,7 +140,7 @@ string[] ExtractSeeded(string dll, string method, string output, bool assume = t Console.WriteLine("PASS hash proved for every seed; a fixed seed is refuted and unbound seeds fault"); var mixSource = File.ReadAllText(Path.Combine(mix, "Extracted.lean")); -const string seedBytes = "SeededHash::Seed0\", bytes := [(BitVec.ofNat 8 0)"; +const string seedBytes = "name := name!\"Seed0\" }, bytes := [(BitVec.ofNat 8 0)"; if (!mixSource.Contains(seedBytes, StringComparison.Ordinal)) throw new Exception("Missing unknown seed descriptor"); var guessedProof = Path.Combine(root, "Opaque-guessed.lean"); // A load from the zero placeholder faults unless the caller supplies a binding. @@ -149,7 +154,7 @@ string[] ExtractSeeded(string dll, string method, string output, bool assume = t var forgedSeed = Path.Combine(root, "Opaque-forged.lean"); File.WriteAllText(forgedSeed, mixSource.Replace(seedBytes, seedBytes[..^2] + "5)", StringComparison.Ordinal)); rejected.Add(forgedSeed, "PASS Lean evidence rejects a value written into an unknown seed", - "StaticValuesValid"); + "MethodDecodeError.liveHole"); void RejectSeeded(string name, string dll, string method, string marker, bool assume = true) { diff --git a/Tests/StaticSpans.cs b/Tests/StaticSpans.cs index 080bf9221..2ae164fdd 100644 --- a/Tests/StaticSpans.cs +++ b/Tests/StaticSpans.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using Mono.Cecil; @@ -68,7 +73,8 @@ string Result(string value, string args = "[]") => // Unsupported element types, identities and by-reference spans remain rejected. foreach (var (mutation, marker) in new[] { - ("element", "Unsupported type: System.ReadOnlySpan`1"), ("scope", "Unresolved aggregate"), + ("element", "Unsupported type: System.ReadOnlySpan`1"), + ("scope", "Unsupported type identity: System.ReadOnlySpan`1 [Foreign.Runtime,"), ("value-type", "Unsupported type identity"), ("byref-return", "Unsupported by-reference span") }) { @@ -85,7 +91,12 @@ string Result(string value, string args = "[]") => switch (mutation) { case "element": span.GenericArguments[0] = module.TypeSystem.Double; break; - case "scope": span.ElementType.Scope = new AssemblyNameReference("Foreign.Runtime", new Version(10, 0, 0, 0)); break; + case "scope": + // The foreign scope is a written AssemblyRef row, so the identity check rejects it. + var foreign = new AssemblyNameReference("Foreign.Runtime", new Version(10, 0, 0, 0)); + module.AssemblyReferences.Add(foreign); + span.ElementType.Scope = foreign; + break; case "value-type": span.IsValueType = false; break; case "byref-return": getter.ReturnType = new ByReferenceType(span); break; } diff --git a/Tests/StaticValueEvidence.lean b/Tests/StaticValueEvidence.lean index d8688c0fc..3b25b6b21 100644 --- a/Tests/StaticValueEvidence.lean +++ b/Tests/StaticValueEvidence.lean @@ -9,46 +9,75 @@ private def zeros : List (BitVec 8) := [0, 0] /-- Returns its zero-initialized two-byte aggregate local. -/ private def initializer : Method := { - code := [.local 0, .ret none], locals := [.aggregate zeros], localKinds := [.aggregate 2], returnsValue := true } + code := [.ldloc 0, .ret], locals := [.aggregate zeros], localKinds := [.aggregate 2], + returnsValue := true } -private def descriptor (bytes : List (BitVec 8)) (init : Option StaticInitializer) : StaticDescriptor := - { identity := 0, fieldName := "Fixture::Zero", bytes, initializer := init } +/-- The static field the fixtures load, as the raw operand of `rawStatic` names it. -/ +private def field : FieldRef := + { owner := .atom "Fixture" "Holder", index := 0, type := .atom "Fixture" "Pair", + name := name!"Zero" } -private def loader (op bytes : List (BitVec 8)) (init : Option StaticInitializer) : Method := - { code := [.memory (.staticValue op), .ret none], locals := [], returnsValue := true, +private def descriptor (bytes : List (BitVec 8)) (init : Option StaticInitializer) : + StaticDescriptor := + { identity := 0, field, bytes, initializer := init } + +/-- A second field of the same holder and type. -/ +private def otherField : FieldRef := { field with index := 1, name := name!"One" } + +/-- The field's index on another holder. -/ +private def otherOwner : FieldRef := { field with owner := .atom "Fixture" "Other" } + +/-- The field's holder and index with another stored type. -/ +private def otherType : FieldRef := { field with type := .atom "Fixture" "Word" } + +/-- The field's holder, index and type under another name. -/ +private def otherName : FieldRef := { field with name := name!"Other" } + +private def loader (bytes : List (BitVec 8)) (init : Option StaticInitializer) : Method := + { code := [.ldsfld field, .ret], locals := [], returnsValue := true, staticSites := [(0, descriptor bytes init)] } -private def load (op bytes : List (BitVec 8)) +private def load (bytes : List (BitVec 8)) (init : Option StaticInitializer := some ⟨1, [], none, false⟩) : Program := - [loader op bytes init, initializer] + [loader bytes init, initializer] private def run (program : Program) := (Safety.invoke program 8 0 [] initializerMemory).map Prod.snd -- A load pushes the descriptor's bytes and leaves memory unchanged. -example : run (load zeros zeros) = .ok [.scalar (.aggregate zeros)] := rfl -example : Safety.staticInstruction (loader zeros zeros (some ⟨1, [], none, false⟩)) 0 (.staticValue zeros) [] initializerMemory = - .ok (initializerMemory, [.scalar (.aggregate zeros)]) := rfl +example : run (load zeros) = .ok [.scalar (.aggregate zeros)] := rfl +example : Safety.staticLoad (loader zeros (some ⟨1, [], none, false⟩)) 0 initializerMemory = + .ok (initializerMemory, .scalar (.aggregate zeros)) := rfl -- The certificate reruns the initializer: matching bytes pass, forged bytes fail. -example : StaticValuesValid (load zeros zeros) := by decide +kernel -example : ¬ StaticValuesValid (load [1, 0] [1, 0]) := by decide +kernel +example : StaticValuesValid (load zeros) := by decide +kernel +example : ¬ StaticValuesValid (load [1, 0]) := by decide +kernel -- An initializer that is not in the program, or returns no value, is rejected too. -example : ¬ StaticValuesValid (load zeros zeros (some ⟨5, [], none, false⟩)) := by decide +kernel -example : ¬ StaticValuesValid (load zeros zeros (some ⟨0, [], none, false⟩)) := by decide +kernel +example : ¬ StaticValuesValid (load zeros (some ⟨5, [], none, false⟩)) := by decide +kernel +example : ¬ StaticValuesValid (load zeros (some ⟨0, [], none, false⟩)) := by decide +kernel + +/-- Loading RVA data, a descriptor with neither initializer nor opaque value, faults: the model + has no value for it. -/ +theorem rva_value_load : + run (load zeros none) = .error ⟨0, 0, .unsupported "Static field value is not extracted"⟩ := + rfl +#print axioms rva_value_load --- The emitted bytes must equal the descriptor's, and the descriptor must name an initializer. -example : run (load [1, 0] zeros) = .error ⟨0, 0, .invalidState⟩ := rfl -example : run (load zeros zeros none) = .error ⟨0, 0, .invalidState⟩ := rfl -- RVA descriptors without an initializer impose nothing on the certificate. -example : StaticValuesValid (load zeros [7] none) := by decide +kernel +example : StaticValuesValid (load [7] none) := by decide +kernel + +/-- A load whose pc has no site faults instead of reading some other field's descriptor. -/ +theorem missing_site_faults : + run [{ loader zeros none with staticSites := [] }] = + .error ⟨0, 0, .unsupported "Missing extracted static field identity"⟩ := rfl +#print axioms missing_site_faults /-- A constructor writes through the ordinary checked aggregate temporary. -/ private def constructor : Method := { - code := [.arg 0, .const32 7, .storeScalar 1, .ret none] + code := [.ldarg 0, .ldc_i4 7, .stind .i1, .ret] locals := [], returnsValue := false } private def constructed (bytes : List (BitVec 8)) (callee : Method := constructor) : Program := - [loader bytes bytes (some { method := 1, arguments := [], construct := true }), callee] + [loader bytes (some { method := 1, arguments := [], construct := true }), callee] /-- Construction starts with initialized zero bytes and preserves untouched bytes. -/ theorem constructor_bytes : StaticValuesValid (constructed [7, 0]) := by decide +kernel @@ -63,19 +92,54 @@ theorem returning_constructor : ¬ StaticValuesValid (constructed zeros initiali decide +kernel #print axioms returning_constructor -/-- Raw address and value loads have the same stack height but different result kinds. -/ -private def rawStatic (opcode : Nat) : RawMethod := { +/-- A raw method whose instructions are `before` and then `opcode` naming `field`, static + when `isStatic` holds. -/ +private def rawStatic (opcode : Nat) (isStatic : Bool := true) + (before : List RawInstruction := []) : RawMethod := { token := 0, owner := "Fixture", hasThis := false, explicitThis := false callingConvention := 0, attributes := 0, implementation := 0, returnType := .void parameters := [], locals := [], initLocals := true, maxStack := 1, genericParameters := [] - code := [⟨0, opcode, .field (.atom "Fixture" "Holder") 0 (.atom "Fixture" "Pair") true⟩] } + code := before ++ [⟨0, opcode, .field field.owner field.index field.type field.name isStatic⟩] } /-- Constructor-initialized addresses retain their original raw opcode. -/ theorem static_address_opcode : - let address := { loader zeros zeros (some ⟨1, [], none, false⟩) with - code := [.memory (.staticAddress zeros)] } - methodShapeInstructions [] (rawStatic 127) address = true ∧ - methodShapeInstructions [] (rawStatic 126) address = false := by decide +kernel + let address := { loader zeros (some ⟨1, [], none, false⟩) with code := [.ldsflda field] } + methodShapeInstructions (rawStatic 127) address = true ∧ + methodShapeInstructions (rawStatic 126) address = false := by decide +kernel #print axioms static_address_opcode +/-- A static instruction names the field of its raw instruction: another index, owner, stored + type or name, an instance operand or another opcode is rejected, for loads, addresses and + tokens. -/ +theorem static_field_named : + let method (code : List Instruction) := { loader zeros (some ⟨1, [], none, false⟩) with code } + methodShapeInstructions (rawStatic 126) (method [.ldsfld field]) = true ∧ + methodShapeInstructions (rawStatic 126) (method [.ldsfld otherField]) = false ∧ + methodShapeInstructions (rawStatic 126) (method [.ldsfld otherOwner]) = false ∧ + methodShapeInstructions (rawStatic 126) (method [.ldsfld otherType]) = false ∧ + methodShapeInstructions (rawStatic 126) (method [.ldsfld otherName]) = false ∧ + methodShapeInstructions (rawStatic 126 false) (method [.ldsfld field]) = false ∧ + methodShapeInstructions (rawStatic 127) (method [.ldsflda otherField]) = false ∧ + methodShapeInstructions (rawStatic 208) (method [.ldtoken (.field field)]) = true ∧ + methodShapeInstructions (rawStatic 208) (method [.ldtoken (.field otherField)]) = false ∧ + methodShapeInstructions (rawStatic 127) (method [.ldtoken (.field field)]) = false := by + decide +kernel +#print axioms static_field_named + +/-- Raw `ret`. -/ +private def rawRet : RawInstruction := ⟨0, 42, .none⟩ + +/-- An `stsfld` after `ret` names its raw field and validates; stepping it faults, since no + static field is writable. -/ +theorem stsfld_faults : + let stored : Method := { loader zeros none with code := [.ret, .stsfld field] } + let frame : Safety.Frame := { activation := 0, locals := [], owned := [] } + methodShapeInstructions (rawStatic 128 true [rawRet]) stored = true ∧ + methodShapeInstructions (rawStatic 128 true [rawRet]) + { stored with code := [.ret, .stsfld otherField] } = false ∧ + Safety.step stored (.stsfld field) 1 [] frame [.scalar (.i32 0)] initializerMemory = + .error (.unsupported "Static field store") := by + refine ⟨by decide +kernel, by decide +kernel, rfl⟩ +#print axioms stsfld_faults + end Tests.StaticValueEvidence diff --git a/Tests/StaticValueIndexes.cs b/Tests/StaticValueIndexes.cs index 7c0ffa82b..635504aa5 100644 --- a/Tests/StaticValueIndexes.cs +++ b/Tests/StaticValueIndexes.cs @@ -2,6 +2,12 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include PinnedMetadata.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Reflection; using Mono.Cecil; @@ -30,8 +36,9 @@ MethodDefinition Method(TypeDefinition type, string name, params Instruction[] c type.Methods.Add(method); return method; } -using (var module = ModuleDefinition.ReadModule(assembly)) +using (var pinned = new PinnedMetadata(extractor, assembly)) { + var module = pinned.Module; var context = Context(module); foreach (var field in module.GetType("Examples.StaticScalars").Fields) { @@ -53,7 +60,8 @@ MethodDefinition Method(TypeDefinition type, string name, params Instruction[] c } foreach (var scenario in new[] { "twice", "flow", "branch", "switch", "foreign", "outside", "address", "fresh" }) { - using var module = ModuleDefinition.ReadModule(assembly); + using var pinned = new PinnedMetadata(extractor, assembly); + var module = pinned.Module; var type = module.GetType("Examples.StaticScalars"); var field = type.Fields.Single(field => field.Name == "Byte"); var constructor = type.Methods.Single(method => method.IsConstructor); @@ -103,12 +111,20 @@ MethodDefinition Method(TypeDefinition type, string name, params Instruction[] c Reject(() => Resolve(context, field), marker); } Console.WriteLine("PASS scalar copies, control flow, switch/foreign targets, outside writes and fresh contexts"); +var missingReferenceImage = At("artifacts/StaticValueIndexes/missing-reference.dll"); +Directory.CreateDirectory(Path.GetDirectoryName(missingReferenceImage)!); +using (var source = ModuleDefinition.ReadModule(assembly)) +{ + source.AssemblyReferences.Add(new("MissingStaticIndexAssembly", new Version(1, 0))); + source.Write(missingReferenceImage); +} foreach (var errorFirst in new[] { false, true }) { - using var module = ModuleDefinition.ReadModule(assembly); + using var pinned = new PinnedMetadata(extractor, missingReferenceImage); + var module = pinned.Module; var type = module.GetType("Examples.StaticScalars"); var field = type.Fields.Single(field => field.Name == "Byte"); - var absent = new AssemblyNameReference("MissingStaticIndexAssembly", new Version(1, 0)); + var absent = module.AssemblyReferences.Single(reference => reference.Name == "MissingStaticIndexAssembly"); var unknown = new FieldReference("Unknown", module.TypeSystem.Int32, new TypeReference("Missing", "Owner", module, absent)); var broken = Method(type, "BrokenWriter", Instruction.Create(OpCodes.Stsfld, unknown), diff --git a/Tests/StaticValues.cs b/Tests/StaticValues.cs index 9ae4aac20..c97cafc92 100644 --- a/Tests/StaticValues.cs +++ b/Tests/StaticValues.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include PreparedLeanFixtures.cs @@ -146,14 +151,24 @@ end ExampleProof // The certificate recomputes the initializer: forged bytes in the extracted program fail translation_valid. var source = File.ReadAllText(Path.Combine(root, "One", "Extracted.lean")); var one = "bytes := [(BitVec.ofNat 8 1), "; -if (!source.Contains(one) || !source.Contains(".staticValue [(BitVec.ofNat 8 1), ")) +// The decoded load names the field whose descriptor holds the initializer bytes. +if (!source.Contains(one) || !source.Contains("cil_decoded_method method0 ") || + !source.Contains("name := name!\"One\" }")) throw new InvalidOperationException("Missing One descriptor in the extraction"); -var forged = Path.Combine(root, "One-forged.lean"); -File.WriteAllText(forged, source.Replace("[(BitVec.ofNat 8 1), ", "[(BitVec.ofNat 8 2), ")); -var forgedResult = Execute(["lake", "env", "lean", forged], cwd: verification.CheckoutPackage()); -if (forgedResult.Code == 0 || !forgedResult.Output.Contains("Extraction.StaticValuesValid program")) - throw new InvalidOperationException("Forged static value was accepted\n" + forgedResult.Output); -Console.WriteLine("PASS forged static value rejected by the extraction certificate"); +void RejectInitializer(string name, string original, string changed) +{ + if (!source.Contains(original, StringComparison.Ordinal)) + throw new InvalidOperationException($"Missing {name} mutation target."); + var forged = Path.Combine(root, $"One-forged-{name}.lean"); + File.WriteAllText(forged, source.Replace(original, changed, StringComparison.Ordinal)); + var result = Execute(["lake", "env", "lean", forged], cwd: verification.CheckoutPackage()); + if (result.Code == 0 || !result.Output.Contains("Extraction.StaticValuesValid program")) + throw new InvalidOperationException($"Expected StaticValuesValid to reject {name}.\n{result.Output}"); +} + +RejectInitializer("bytes", one, "bytes := [(BitVec.ofNat 8 2), "); +RejectInitializer("callee", "initializer := some { method := 1,", "initializer := some { method := 0,"); +Console.WriteLine("PASS changed initializer bytes and callee rejected by the extraction certificate"); void Reject(string dll, string selector, string name, string marker, bool assume = true) { diff --git a/Tests/StepFlow.lean b/Tests/StepFlow.lean index fb4e03e8c..462ad0cd1 100644 --- a/Tests/StepFlow.lean +++ b/Tests/StepFlow.lean @@ -1,9 +1,10 @@ import CIL.Safety.StepFlow +import Tests.Support /-! The pc a step resumes at and the action shape it produces, read off the instruction alone. -/ namespace Tests.StepFlow -open CIL (Method Op) +open CIL (Method Instruction) open CIL.Safety variable {body : Method} {pc : Nat} {args stack stack' : List Value} {frame frame' : Frame} {m m' : Memory} @@ -11,24 +12,25 @@ variable {body : Method} {pc : Nat} {args stack stack' : List Value} {frame fram example (h : step body (.bge 7) pc args frame stack m = .ok (.next target stack' frame' m')) : target = pc + 1 ∨ target = 7 := by - simpa [Op.continues, Op.targets] using step_next_target h + simpa [Instruction.continues, Instruction.targets] using step_next_target h /-- An unconditional branch does not continue, so it can only resume at its target. -/ -example (h : step body (.branch 9) pc args frame stack m = .ok (.next target stack' frame' m')) : target = 9 := by - simpa [Op.continues, Op.targets] using step_next_target h +example (h : step body (.br 9) pc args frame stack m = .ok (.next target stack' frame' m')) : target = 9 := by + simpa [Instruction.continues, Instruction.targets] using step_next_target h /-- An out-of-range switch index falls through, so the fallthrough stays possible. -/ example (h : step body (.switch [3, 5]) pc args frame stack m = .ok (.next target stack' frame' m')) : target = pc + 1 ∨ target = 3 ∨ target = 5 := by - simpa [Op.continues, Op.targets] using step_next_target h + simpa [Instruction.continues, Instruction.targets] using step_next_target h -example (h : step body (.nativeBinary (.blt 4)) pc args frame stack m = .ok (.next target stack' frame' m')) : +example (h : step body (.blt 4) pc args frame stack m = .ok (.next target stack' frame' m')) : target = pc + 1 ∨ target = 4 := by - simpa [Op.continues, Op.targets] using step_next_target h + simpa [Instruction.continues, Instruction.targets] using step_next_target h /-- A return never resumes in the frame. -/ -example : step body (.ret none) pc args frame stack m ≠ .ok (.next target stack' frame' m') := fun h => by - simpa [Op.continues, Op.targets] using step_next_target h +example : step body (.ret) pc args frame stack m ≠ .ok (.next target stack' frame' m') := + fun h => by + simpa [Instruction.continues, Instruction.targets] using step_next_target h /-- Both branch outcomes occur, so the disjunction above is not vacuous. -/ example : step body (.bge 7) 3 args frame [.scalar (.i32 0), .scalar (.i32 1)] m = .ok (.next 7 [] frame m) := by @@ -36,20 +38,66 @@ example : step body (.bge 7) 3 args frame [.scalar (.i32 0), .scalar (.i32 1)] m example : step body (.bge 7) 3 args frame [.scalar (.i32 1), .scalar (.i32 0)] m = .ok (.next 4 [] frame m) := by rfl -example {r : Reference} (h : step body (.call 2 1) pc args frame stack m = .ok (.construct 2 args stack frame' r .word64 m')) : +example {r : Reference} + (h : step body (.call (Tests.callee 1)) pc args frame stack m = + .ok (.construct 2 args stack frame' r .word64 m')) : False := by - obtain ⟨_, _, _, contradiction⟩ := step_call_action h - cases contradiction + rcases step_call_action h with ⟨_, _, plain⟩ | ⟨_, _, _, _, _, contradiction, _⟩ + · rw [if_neg (by decide)] at plain + obtain ⟨_, _, _, contradiction⟩ := plain + cases contradiction + · cases contradiction -example {argc} (h : step body (.newVector256 2 argc) pc args frame stack m = .ok (.call 2 args stack m')) : +example {constructor : CIL.Extraction.MethodRef} {size} + (linked : body.linkAt pc = some (.aggregate 2 size)) + (h : step body (.newobj constructor) pc args frame stack m = + .ok (.call 2 args stack m')) : False := by - obtain ⟨_, _, _, _, _, _, contradiction⟩ := step_newVector256_action h + obtain ⟨_, _, _, _, _, _, contradiction⟩ := step_newobj_aggregate_action linked h cases contradiction +/-- A parameterless call at a pc whose link names method 2. -/ +def relinked : Method := + { code := [.call (Tests.callee 0) {}, .ret], locals := [], returnsValue := false, + links := [(0, .direct 2)] } + +/-- The call enters the method its link names. -/ +theorem relinked_call : + step relinked (.call (Tests.callee 0)) 0 args frame [] m = .ok (.call 2 [] [] m) := rfl + +/-- A call site the table gives no link faults instead of entering any method. -/ +theorem unlinked_call : + step { relinked with links := [] } (.call (Tests.callee 0)) 0 args frame [] m = + .error unlinkedCall := rfl + +/-- A tail call the table gives no link faults before its stack check, which an empty stack under + one argument would fail with `invalidState`. -/ +theorem unlinked_tail_call : + step { + code := [.call (Tests.callee 1) { tail := true }], locals := [], returnsValue := false, + links := [] } + (.call (Tests.callee 1) { tail := true }) 0 args frame [] m = .error unlinkedCall := rfl + +/-- A construction the table gives no link faults before its size check, which size 0 would fail + with `invalidState`. -/ +theorem unlinked_aggregate : + step { code := [.newobj (.arity 0)], locals := [], returnsValue := true, links := [] } + (.newobj (.arity 0)) 0 args frame [] m = .error unlinkedCall := rfl + +/-- A construction under a link of a call kind faults; only a constructor link allocates. -/ +theorem miskinded_construction : + step { code := [.newobj (.arity 0)], locals := [], returnsValue := true, + links := [(0, .direct 1)] } + (.newobj (.arity 0)) 0 args frame [] m = .error unlinkedCall := rfl + +#print axioms relinked_call +#print axioms unlinked_call +#print axioms unlinked_tail_call +#print axioms unlinked_aggregate +#print axioms miskinded_construction #print axioms step_next_target #print axioms step_call_action -#print axioms step_newVector256_action -#print axioms step_newAggregate_action +#print axioms step_newobj_aggregate_action #print axioms step_callee end Tests.StepFlow diff --git a/Tests/SteppingScaling.cs b/Tests/SteppingScaling.cs index 38b6a6e1b..3c7d821cf 100644 --- a/Tests/SteppingScaling.cs +++ b/Tests/SteppingScaling.cs @@ -1,25 +1,54 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Diagnostics; using System.Runtime.InteropServices; using System.Text.Json; const double WorkGrowthLimit = 1.35; -const double TimeGrowthLimit = 2.0; +// Most heartbeats one more hypothesis in scope may add to one instruction, which costs about 120,000. +// Unifying each lookup with every equation hypothesis added 600 to 1,950. A lookup outside a stepping +// driver reads each hypothesis once, about 25 heartbeats; building an index for it took about 410. +const double HypothesisCostLimit = 100.0; +// Limit the frontend and kernel heartbeats for checked fields and intrinsic calls relative to their references. +const double DecodedFrontendLimit = 1.03; +const double DecodedKernelLimit = 1.08; const int Repetitions = 3; +const int LocalCount = 8; +const int HypothesisBodyCycles = 30; +const int VirtualSites = 16; +const string Int32Type = ".atom CIL.Extraction.runtimeScope \"System.Int32\""; +const string Int64Type = ".atom CIL.Extraction.runtimeScope \"System.Int64\""; +const string NodeType = ".object \"Examples\" \"Examples.Node\""; +const string PairType = ".aggregate \"Examples.Pair\" 0"; +const string PairElement = ".aggregate \"Examples.Pair\" 8"; +var localSlots = string.Join(", ", Enumerable.Repeat(".root (some target)", LocalCount)); ScalingChecks(); if (args is ["--self-test"]) { return; } +string[]? selectedWorkloads = null; +if (Array.IndexOf(args, "--workloads") is var selection && selection >= 0) +{ + if (selection != args.Length - 2) + throw new ArgumentException("--workloads requires a final comma-separated list."); + selectedWorkloads = args[^1].Split(',', StringSplitOptions.RemoveEmptyEntries); + args = args[..selection]; +} var package = args switch { [] => FixturePackage(), ["--package", var path] => Path.GetFullPath(path), - _ => throw new ArgumentException("Usage: SteppingScaling.cs [--package ] | --self-test") + _ => throw new ArgumentException( + "Usage: SteppingScaling.cs [--package ] [--workloads ] | --self-test") }; var directory = At("artifacts/SteppingScaling"); Directory.CreateDirectory(directory); @@ -29,9 +58,37 @@ File.WriteAllText(Path.Combine(directory, "source.patch"), sourcePatch); File.Copy(At("Tests/SteppingScaling.cs"), Path.Combine(directory, "Runner.cs"), overwrite: true); var rows = new List(); -foreach (var workload in new[] { "scalar", "memory" }) - foreach (var requested in new[] { 16, 64, 256 }) - rows.Add(Measure(workload, requested)); +// Sizes are instruction counts, except for hypotheses, where they count the hypotheses in scope. A +// reduced measurement compiles once and checks heartbeats only, which are exactly repeatable. +var workloads = new (string Name, int[] Sizes, bool Full)[] +{ + ("scalar", [16, 64, 256], true), ("memory", [16, 64, 256], true), + ("reference", [16, 64, 256], true), ("table", [16, 64, 256], true), + ("accumulate", [16, 64, 256], true), ("accumulate", [1024], false), + ("locals", [16, 64, 256], true), ("contract", [16, 64, 256], false), ("loop", [16, 64, 256], false), + ("loopfacts", [16, 64, 256], false), ("outcome", [16, 64, 256], false), + ("lemma", [16, 64, 256], false), ("depth", [16, 64, 256], false), ("hypotheses", [16, 64, 256], false), + ("hypothesescontract", [16, 64, 256], false), ("hypothesesloop", [16, 64, 256], false), + ("hypotheseslookup", [16, 64, 256], false), ("hypothesesdirect", [16, 256], false), + ("snapshots", [10, 20, 40, 80], false), ("faulting", [16, 64], false), ("arguments", [16, 64, 256], false), + ("homes", [16, 64, 256], false), ("allocation", [16, 64, 256], false), ("calls", [16, 64, 256], true), + ("tablecalls", [16, 64, 256], true), ("objects", [16, 64, 256], false), ("statics", [16, 64, 256], false), + ("constructions", [16, 64, 256], false), ("objectconstructors", [16, 64, 256], false), + ("elements", [16, 64, 256], false), ("typedelements", [16, 64, 256], false), + ("typedsteps", [16, 64, 256], false), ("addresses", [16, 64, 256], false), + ("addresssteps", [16, 64, 256], false), ("instance", [16, 64, 256], false), ("virtual", [1, 16, 64], false), + ("intrinsiccalls", [16, 64, 256], false), ("structfields", [16, 64, 256], false), + ("structfieldscontract", [16, 64, 256], false) +}; +if (selectedWorkloads is not null) +{ + if (selectedWorkloads.Length == 0 || selectedWorkloads.Any(name => !workloads.Any(row => row.Name == name))) + throw new ArgumentException("Unknown or empty stepping workload selection."); + workloads = workloads.Where(row => selectedWorkloads.Contains(row.Name, StringComparer.Ordinal)).ToArray(); +} +foreach (var (workload, sizes, full) in workloads) + foreach (var requested in sizes) + rows.Add(Measure(workload, requested, full)); var report = new { @@ -50,44 +107,444 @@ TimingIsolation = "External machine load is not controlled; wall times are observations", Measurement = "Frontend includes its initial kernel check; kernel columns are separate rechecks. " + "Module command time excludes imports. Compiled proof bytes subtract the same body without its proof.", - Budgets = new { WorkGrowthLimit, TimeGrowthLimit }, + Budgets = new { WorkGrowthLimit, HypothesisCostLimit }, Rows = rows }; File.WriteAllText(reportPath, JsonSerializer.Serialize(report, new JsonSerializerOptions { WriteIndented = true }) + "\n"); foreach (var workload in rows.GroupBy(row => row.Workload)) { - var measurements = workload.OrderBy(row => row.Instructions).ToArray(); - for (var index = 1; index < measurements.Length; index++) - CheckGrowth(measurements[index - 1], measurements[index]); + var measurements = workload.OrderBy(row => row.Size).ToArray(); + // A lookup outside a driver reads every hypothesis in scope, so only the hypothesis limit bounds it. + for (var index = 1; index < measurements.Length && workload.Key != "hypothesesdirect"; index++) + CheckGrowth(measurements[index - 1], measurements[index], workload.Key is "arguments" or "homes", + workload.Key == "virtual"); + if (workload.Key.StartsWith("hypotheses")) + CheckHypothesisCost(measurements[0], measurements[^1]); } -Console.WriteLine("PASS scalar and memory stepping growth budgets; results: " + reportPath); +foreach (var decoded in rows.Where(row => row.Workload is "structfields" or "structfieldscontract" or "intrinsiccalls")) + CheckReferenceBudget(decoded); +Console.WriteLine("PASS stepping growth budgets for " + + string.Join(", ", rows.Select(row => row.Workload).Distinct()) + "; results: " + reportPath); -SteppingMeasurement Measure(string workload, int requested) +// The instructions that open a body, repeat per cycle and end it. The accumulate, contract, lemma, +// outcome and loop bodies keep one value on the stack that grows by an operand per cycle, and the +// loop bodies end in a `nop` at the state their path reaches. The locals body stores a reference +// into each of eight reference locals per cycle. The table body offsets a native method table by a +// symbolic int32 and pops the selected cursor. The hypotheses bodies are accumulate, contract and +// loop bodies of fixed length; the direct one looks up each of their instructions outside a stepping +// driver. The faulting body divides, takes a remainder, adds with an overflow check and converts +// with one, each from the arguments, so each step reads a fact of constant size. The arguments body +// declares half as many int32 parameters as it has instructions and adds each, read once, to the sum. +// The homes body gives each of as many reference parameters a home and loads each argument once, +// which reads its home. The allocation body allocates an array of two class references per cycle and +// pops it, so each `newarr` resolves its element shape and reads one allocation fact. The calls body +// passes its argument through one call per cycle to a callee that returns it, so each call reads its +// target from the body's link table, which it names as a tagged definition, as an extraction's decoded +// method does; the tablecalls body writes that table out, as a template does. +// The objects body loads an int64 field of its object argument and pops it per cycle, so each `ldfld` +// reads the field's shape fact and two memory facts of constant size. +// The statics body loads one static field and pops it per cycle, with one static site per load. +// The constructions body constructs a class instance and pops it per cycle, so each `newobj` reads its +// `.object` link from the body's table and one allocation fact and enters a constructor that returns. +// The objectconstructors body calls the `System.Object` constructor on its object argument per cycle, so +// each call reads that its pc has no link and the runtime model fact of its pc. +// The elements body loads and pops one int32 element of its array argument, then stores its index +// argument into that element, per cycle, so each access reads one element fact of constant size. +// The typed elements body does the same through `ldelem` and `stelem` with an `int32` type operand, +// then copies one struct element of a second array onto itself. Each step evaluates the typed access +// from element facts, its shape from the literal body's layouts; the generated shape facts are in scope +// but leave the cost unchanged. The typed steps body is the same body stepped by one fact per access. +// The instance body makes one receiver-checked instance call on its object argument per cycle, into a +// method that returns at once, through its direct link. The virtual body makes +// `VirtualSites` virtual calls on it through a named dispatch table whose size is the row's size, with +// the receiver's class last, so each call reads one runtime type fact and looks its class up in the table. +// The intrinsic calls body computes `Math.Max` of its two int64 arguments and pops it per cycle, so each +// call reads that its pc has no link and the runtime model fact of its pc, a scalar intrinsic. +// The struct fields body loads and pops the int64 field at byte 8 of the struct its byref argument +// addresses, then stores its int64 argument there, per cycle; the contract body is the same body under +// contract stepping. Each access reads the field's shape fact, then its address and its load or write +// from supplied facts. +// The addresses body forms a writable and a readonly address of one int32 element of its array argument +// and pops each, per cycle, each stepped by a fact derived from the element fact and the generated shape +// fact; the address steps body steps the same body by one supplied fact per address. +static (string[] Opening, string[] Cycle, string[] Ending) Shape(string workload, int field = 0) => workload switch +{ + "calls" or "tablecalls" => ([".ldarg 0"], [".call calleeMethod"], [".ret"]), + "memory" => ([], [".ldarg 0", ".ldarg 1", ".stind .i8", ".ldarg 0", ".ldind .i8", ".pop"], + [".ldarg 0", ".ldind .i8", ".ret"]), + "reference" or "table" => ([], [".ldarg 0", ".ldarg 1", ".add", ".pop"], [".ldarg 0", ".ret"]), + "accumulate" or "contract" or "hypotheses" or "hypothesescontract" or "hypotheseslookup" + or "hypothesesdirect" or "lemma" or "outcome" => ([".ldarg 0"], [".ldarg 1", ".add"], [".ret"]), + "loop" or "loopfacts" or "hypothesesloop" => ([".ldarg 0"], [".ldarg 1", ".add"], [".nop"]), + "locals" => ([], Enumerable.Range(0, LocalCount).SelectMany(slot => new[] { ".ldarg 0", $".stloc {slot}" }) + .ToArray(), [".ldloc 0", ".ret"]), + "statics" => ([], [".ldsfld staticField", ".pop"], [".ldsfld staticField", ".ret"]), + "faulting" => ([], [".ldarg 0", ".ldarg 1", ".div", ".pop", ".ldarg 0", ".ldarg 1", ".rem", ".pop", + ".ldarg 0", ".ldarg 1", ".add_ovf", ".pop", ".ldarg 0", ".conv_ovf .i4", ".pop"], [".ldarg 0", ".ret"]), + "allocation" => ([], [".ldc_i4 2", $".newarr ({NodeType})", ".pop"], [".ldc_i4 0", ".ret"]), + // The constructor reference is a literal, as an extraction writes it. + "constructions" => ([], [$".newobj {{ owner := {NodeType}, name := name!\".ctor\", hasThis := true, " + + "genericArity := 0, returnType := .void, parameters := [] }", ".pop"], [".ldc_i4 0", ".ret"]), + "objects" => ([], [".ldarg 0", ".ldfld counter", ".pop"], [".ldc_i4 0", ".ret"]), + "objectconstructors" => ([], [".ldarg 0", ".call CIL.Extraction.objectConstructorMethod {}"], + [".ldc_i4 0", ".ret"]), + "instance" or "virtual" => ([], [".ldarg 0", ".callvirt (.arity 0)"], [".ldc_i4 0", ".ret"]), + "intrinsiccalls" => ([], [".ldarg 0", ".ldarg 1", ".call maxMethod {}", ".pop"], [".ldarg 0", ".ret"]), + "elements" => ([], [".ldarg 0", ".ldarg 1", ".ldelem .i4", ".pop", ".ldarg 0", ".ldarg 1", + ".ldarg 1", ".stelem .i4"], [".ldc_i4 0", ".ret"]), + "typedelements" or "typedsteps" => ([], [".ldarg 0", ".ldarg 1", $".ldelem_any ({Int32Type})", ".pop", ".ldarg 0", + ".ldarg 1", ".ldarg 1", $".stelem_any ({Int32Type})", ".ldarg 2", ".ldarg 1", ".ldarg 2", ".ldarg 1", + $".ldelem_any ({PairType})", $".stelem_any ({PairType})"], [".ldc_i4 0", ".ret"]), + "structfields" or "structfieldscontract" => ([], [".ldarg 0", $".ldfld word{field}", ".pop", + ".ldarg 0", ".ldarg 1", $".stfld word{field}"], [".ldc_i4 0", ".ret"]), + "addresses" or "addresssteps" => ([], [".ldarg 0", ".ldarg 1", $".ldelema ({Int32Type}) false", ".pop", + ".ldarg 0", ".ldarg 1", ".nop", $".ldelema ({Int32Type}) true", ".pop"], [".ldc_i4 0", ".ret"]), + _ => ([], [".ldarg 0", ".ldarg 1", ".add", ".pop"], [".ldarg 0", ".ret"]) +}; + +// The numeric locals of the snapshot body; the first stores give each one a remembered value. +const int SnapshotLocalCount = 8; + +// Stores `left` into each local, then updates one local per store from its remembered value, and +// returns local 0. Both the remembered values and the facts a walk carries grow with the stores. +static string[] SnapshotOperations(int stores) +{ + var operations = new List(); + for (var local = 0; local < SnapshotLocalCount; local++) + operations.AddRange([".ldarg 0", $".stloc {local}"]); + for (var store = SnapshotLocalCount; store < stores; store++) + operations.AddRange([$".ldloc {store % SnapshotLocalCount}", ".ldarg 1", ".add", + $".stloc {store % SnapshotLocalCount}"]); + operations.AddRange([".ldloc 0", ".ret"]); + return [.. operations]; +} + +// The value local 0 holds at the end of the snapshot body: `left` plus `right` once per update. +static string SnapshotResult(int stores) => + "left" + string.Concat(Enumerable.Repeat(" + right", (stores - 1) / SnapshotLocalCount)); + +// The value a stack of `depth` copies of `right` adds up to, associated to the right as `.add` pops them. +static string DepthSum(int depth) => + "right" + string.Concat(Enumerable.Repeat(" + (right", depth - 1)) + new string(')', depth - 1); + +// An explicit layout gives each named word the same address, so each cycle uses the same memory facts. +static string StructFieldDefinitions(int count) +{ + var definitions = Enumerable.Range(0, count).Select(index => $$""" + /-- The word selected by field index {{index}}. -/ + def word{{index}} : CIL.Extraction.FieldRef := + { owner := {{PairType}}, index := {{index}}, type := {{Int64Type}}, name := name!"Word{{index}}" } + cil_field_shape environment word{{index}} + """); + return $$""" + /-- An explicit struct with one named word per cycle, all at byte eight. -/ + def pairLayout : CIL.Extraction.RawLayout := + { identity := "Examples.Pair", explicit := true, packing := 0, declaredSize := 16, + fields := List.replicate {{count}} { type := .signed64, offset := some 8 } } + /-- The sixteen-byte layout shared by every field access. -/ + def pairComputed : CIL.Extraction.ComputedLayout := + { layout := { size := 16, fields := List.replicate {{count}} { offset := 8, width := 8 } }, + alignment := 8 } + /-- The method environment containing the struct's raw and computed layouts. -/ + @[cil_code] def environment : CIL.LayoutEnvironment := + { rawLayouts := [pairLayout], layouts := #[pairComputed] } + {{string.Join('\n', definitions)}} + set_option cil.requireFieldShapeFacts true + + """; +} + +SteppingMeasurement Measure(string workload, int requested, bool full) { var memory = workload == "memory"; - string[] cycle = memory - ? [".arg 0", ".arg 1", ".store64", ".arg 0", ".load64", ".pop"] - : [".arg 0", ".arg 1", ".add", ".pop"]; - string[] ending = memory ? [".arg 0", ".load64", ".ret"] : [".arg 0", ".ret"]; - var cycles = Math.Max(1, (requested - ending.Length) / cycle.Length); - var operations = Enumerable.Range(0, cycles).SelectMany(_ => cycle).Concat(ending).ToArray(); + var depth = Math.Max(2, requested / 2); + var (opening, cycle, ending) = Shape(workload); + var hypotheses = workload.StartsWith("hypotheses"); + var snapshots = workload == "snapshots"; + var parameters = Math.Max(2, requested / 2); + var cycles = hypotheses ? HypothesisBodyCycles + : workload == "depth" ? depth - 1 + : workload is "arguments" or "homes" ? parameters - 1 + : snapshots ? requested - SnapshotLocalCount + : workload == "virtual" ? VirtualSites + : Math.Max(1, (requested - opening.Length - ending.Length) / cycle.Length); + var operations = workload switch + { + "depth" => Enumerable.Repeat(".ldarg 1", depth).Concat(Enumerable.Repeat(".add", depth - 1)).Append(".ret") + .ToArray(), + "arguments" => Enumerable.Range(1, parameters - 1).SelectMany(index => new[] { $".ldarg {index}", ".add" }) + .Prepend(".ldarg 0").Append(".ret").ToArray(), + "homes" => Enumerable.Range(1, parameters - 1).SelectMany(index => new[] { $".ldarg {index}", ".pop" }) + .Prepend(".ldarg 0").Append(".ret").ToArray(), + "snapshots" => SnapshotOperations(requested), + "structfields" or "structfieldscontract" => Enumerable.Range(0, cycles) + .SelectMany(index => Shape(workload, index).Cycle).Concat(ending).ToArray(), + _ => opening.Concat(Enumerable.Range(0, cycles).SelectMany(_ => cycle)).Concat(ending).ToArray() + }; + var parameterType = workload == "homes" ? $".byref ({Int32Type})" : Int32Type; + var parameterTypes = string.Join(", ", Enumerable.Repeat(parameterType, parameters)); + var signature = $"{{ parameters := [{parameterTypes}], returnType := {parameterType} }}"; + var homes = string.Join(", ", Enumerable.Range(0, parameters).Select(index => $"({index}, .reference)")); + var declaredField = workload switch + { + "arguments" => $" declared := {signature},", + "homes" => $" declared := {signature}, parameterHomes := [{homes}],", + "objects" or "typedelements" or "typedsteps" or "structfields" or "structfieldscontract" => + " environment := environment,", + _ => "" + }; + var statics = workload == "statics"; + var staticField = statics + ? "def staticField : CIL.Extraction.FieldRef :=\n" + + " { owner := .atom \"Fixture\" \"Holder\", index := 0, type := .atom \"Fixture\" \"System.Int32\",\n" + + " name := name!\"Value\" }\n" + + "def staticValue : CIL.StaticDescriptor :=\n" + + " { identity := 0, field := staticField, bytes := [7, 0, 0, 0],\n" + + " initializer := some { method := 1, arguments := [] }, scalar := some true }\n" + : ""; + // One site per load, so every step resolves its own pc among all the body's sites. + var sitesField = statics + ? " staticSites := [" + string.Join(", ", operations.Select((operation, pc) => (operation, pc)) + .Where(entry => entry.operation.StartsWith(".ldsfld", StringComparison.Ordinal)) + .Select(entry => $"({entry.pc}, staticValue)")) + "]," + : ""; + var linkTable = "[" + string.Join(", ", Enumerable.Range(1, cycles).Select(pc => $"({pc}, .direct 1)")) + "]"; + var receiverLink = workload == "instance" ? ".direct 1" : ".virtual dispatch"; + var linksField = workload switch + { + "instance" or "virtual" => $""" links := [{string.Join(", ", Enumerable.Range(0, cycles) + .Select(cycle => $"({2 * cycle + 1}, {receiverLink})"))}],""", + "tablecalls" => $" links := {linkTable},", + "calls" => " links := callLinks,", + "constructions" => " links := [" + + string.Join(", ", Enumerable.Range(0, cycles).Select(cycle => $"({cycle * 2}, .object 1 [] 8)")) + "],", + _ => "" + }; + var callDefinitions = workload is "calls" or "tablecalls" ? $$""" + /-- The method every call names: one `int64` argument and an `int64` result. -/ + def calleeMethod : CIL.Extraction.MethodRef := + { owner := .atom "Tests" "Stepping", name := name!"Identity", hasThis := false, genericArity := 0, + returnType := {{Int64Type}}, parameters := [{{Int64Type}}] } + """ + "\n" + (workload == "calls" ? $$""" + /-- The body's link table, every call entering method 1. -/ + @[cil_code] def callLinks : List (Nat × CIL.Link) := {{linkTable}} + """ + "\n" : "") : ""; + // The dispatch table of the virtual body: one class per entry, each entering method 1. + var dispatchDefinition = workload == "virtual" ? $$""" + /-- The virtual body's dispatch table; the receiver's class is its last entry. -/ + @[cil_code] def dispatch : List (CIL.Extraction.TypeExpr × Nat) := + [{{string.Join(", ", Enumerable.Range(0, requested).Select(entry => $"({VirtualClass(entry)}, 1)"))}}] + """ + "\n" : ""; + var maxDefinition = workload == "intrinsiccalls" ? $$""" + /-- `Math.Max(long, long)`, which the runtime table models as a scalar intrinsic. -/ + def maxMethod : CIL.Extraction.MethodRef := + { owner := .atom CIL.Extraction.runtimeScope "System.Math", name := name!"Max", hasThis := false, + genericArity := 0, returnType := {{Int64Type}}, parameters := [{{Int64Type}}, {{Int64Type}}] } + """ + "\n" : ""; var module = "Stepping" + workload + requested; var path = Path.Combine(directory, module + ".lean"); - var header = $$""" + var extraImports = workload switch + { + "contract" or "loop" or "loopfacts" or "hypothesescontract" or "hypothesesloop" or "hypotheseslookup" + or "structfieldscontract" => + "import CIL.Safety.MethodContract\nimport CIL.Safety.LoopAutomation\n", + "outcome" => "import CIL.Safety.LoopOutcome\nimport CIL.Safety.LoopFrameSteps\n", + "snapshots" => "import CIL.Safety.CallingFields\nimport CIL.Safety.LocalSnapshotSteps\n", + "allocation" or "constructions" => "import CIL.Safety.ObjectSteps\nimport CIL.Safety.SymbolicExecution\n", + "objects" or "objectconstructors" or "instance" or "virtual" => + "import CIL.Safety.ObjectSteps\nimport CIL.Safety.ResolutionFacts\nimport CIL.Safety.SymbolicExecution\n", + "elements" => "import CIL.Safety.ArrayContents\n", + "intrinsiccalls" => "import CIL.Safety.ResolutionFacts\n", + "typedelements" or "typedsteps" or "addresses" or "addresssteps" => + "import CIL.Safety.ArrayContents\nimport CIL.Safety.ResolutionFacts\n", + _ => "" + }; + // The objects body reads field 1 of a class, an int64 in the slot at model offset 8; a step that + // evaluates the shape instead of reading the fact fails the row. + var fieldDefinitions = workload switch + { + "objects" => $$""" + @[cil_code] def environment : CIL.LayoutEnvironment := {} + /-- The `int64` field 1 of a class. -/ + def counter : CIL.Extraction.FieldRef := + { owner := {{NodeType}}, index := 1, type := {{Int64Type}}, name := name!"count" } + cil_field_shape environment counter + set_option cil.requireFieldShapeFacts true + """ + "\n", + "structfields" or "structfieldscontract" => StructFieldDefinitions(cycles), + "typedelements" or "typedsteps" => """ + /-- A struct of two `int32` fields. -/ + def pairLayout : CIL.Extraction.RawLayout := + { identity := "Examples.Pair", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .signed32 }, { type := .signed32 }] } + /-- The checked layout of `pairLayout`: eight bytes. -/ + def pairComputed : CIL.Extraction.ComputedLayout := + { layout := { size := 8, fields := [{ offset := 0, width := 4 }, { offset := 4, width := 4 }] }, + alignment := 4 } + /-- The one struct the body's typed accesses name. -/ + def environment : CIL.LayoutEnvironment := { rawLayouts := [pairLayout], layouts := #[pairComputed] } + """ + "\n", + _ => "" + }; + // The raw view names each element type a typed access of the body names, as an extraction's does. + var rawView = workload is "addresses" or "addresssteps" ? $$""" + /-- The element address opcode of `body` with its type operand. -/ + def bodyClosed : CIL.Extraction.RawMethod := + { token := 1, owner := "Examples", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := true, maxStack := 2, genericParameters := [], + code := [⟨0, 143, .type ({{Int32Type}})⟩] } + """ + "\n" : workload is "typedelements" or "typedsteps" ? $$""" + /-- The element opcodes of `body` with their type operands. -/ + def bodyClosed : CIL.Extraction.RawMethod := + { token := 1, owner := "Examples", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := true, maxStack := 4, genericParameters := [], + code := [⟨0, 163, .type ({{Int32Type}})⟩, + ⟨5, 164, .type ({{Int32Type}})⟩, + ⟨10, 163, .type ({{PairType}})⟩, + ⟨15, 164, .type ({{PairType}})⟩] } + """ + "\n" : ""; + var localKinds = string.Join(", ", Enumerable.Repeat(".word64", SnapshotLocalCount)); + var localValues = string.Join(", ", Enumerable.Repeat(".i64 0", SnapshotLocalCount)); + var bodyDefinition = snapshots ? $$""" + /-- Stores into numeric locals, each later update reading the local's remembered value. -/ + def body : CIL.Method := { + code := [{{string.Join(", ", operations)}}] + locals := [{{localValues}}] + localKinds := [{{localKinds}}], returnsValue := true } + cil_instruction_equations body + """ : $$""" + /-- A synthetic body in the emitter's explicit instruction-list shape. -/ + def body : CIL.Method := { + locals := [], returnsValue := true,{{declaredField}}{{linksField}}{{sitesField}} + code := [{{string.Join(",\n ", operations)}}] } + cil_instruction_equations body + """; + var header = extraImports + $$""" import CIL.InstructionEquations import CIL.Safety.LoopFrames open CIL.Safety set_option maxRecDepth 8192 set_option maxHeartbeats 800000 - /-- A synthetic body in the emitter's explicit instruction-list shape. -/ - def body : CIL.Method := { - locals := [], returnsValue := true, - code := [{{string.Join(",\n ", operations)}}] } - cil_instruction_equations body - """ + "\n"; - var statement = memory - ? $$""" + {{fieldDefinitions}}{{staticField}}{{callDefinitions}}{{dispatchDefinition}}{{maxDefinition}}{{bodyDefinition}} + """ + "\n" + (workload is "calls" or "tablecalls" ? """ + /-- The callee every call of the calls body enters; it returns its argument. -/ + def callee : CIL.Method := { locals := [], returnsValue := true, code := [.ldarg 0, .ret] } + cil_instruction_equations callee + """ + "\n" : "") + (workload == "constructions" ? """ + /-- The constructor every construction of the constructions body enters; it returns at once. -/ + def constructor : CIL.Method := { locals := [], returnsValue := false, code := [.ret] } + cil_instruction_equations constructor + """ + "\n" : "") + (workload is "instance" or "virtual" ? """ + /-- The instance method every call of the instance and virtual bodies enters; it returns at once. -/ + def instanceMethod : CIL.Method := { locals := [], returnsValue := false, code := [.ret] } + cil_instruction_equations instanceMethod + """ + "\n" : "") + (workload is "objectconstructors" or "intrinsiccalls" ? """ + cil_runtime_models body + """ + "\n" : "") + (workload is "arguments" or "homes" or "typedelements" or "typedsteps" or "addresses" or + "addresssteps" || statics ? $$""" + theorem body_returnsValue : body.returnsValue = true := rfl + theorem body_declared : body.declared = {{(workload is "arguments" or "homes" ? signature : "{}")}} := rfl + {{rawView}}cil_resolution_facts body + """ + "\n" : ""); + var incoming = string.Join(", ", + Enumerable.Range(0, parameters).Select(index => $".scalar (.i32 (argument {index}))")); + var sum = string.Join(" + ", Enumerable.Range(0, parameters).Select(index => $"argument {index}")); + var homeSlots = string.Join(", ", Enumerable.Repeat(".root (some target)", parameters)); + // Odd hypotheses are equations headed by an operator and even ones by a local variable; the direct + // body has only the first kind. The lookup hypotheses are instruction lookups like those stepping + // makes, which a lookup that unified with every equation hypothesis would try at each instruction. + var hypothesisLines = string.Join("\n", Enumerable.Range(1, hypotheses ? requested : 0).Select(index => + workload == "hypotheseslookup" + ? $" have hypothesis{index} : ([.ldc_i4 {index}] : List CIL.Instruction)[0]? = " + + $"some (.ldc_i4 {index}) := rfl" + : index % 2 == 1 || workload == "hypothesesdirect" + ? $" have hypothesis{index} : right + {index} = right + {index} := rfl" + : $" obtain ⟨witness{index}, hypothesis{index}⟩ : ∃ witness : CIL.W64, " + + $"witness = right + {index} := ⟨_, rfl⟩")); + var runningSum = $"left{string.Concat(Enumerable.Repeat(" + right", cycles))}"; + // The tactic that steps a hypotheses body; the setup measurement replaces it by `sorry`. + string HypothesesStatement(bool setup) => workload switch + { + "hypothesescontract" or "hypotheseslookup" => $$""" + /-- Equations in scope before contract stepping, which it must not scan at each step. -/ + theorem checked (memory : Memory) (left right : CIL.W64) : + WP (Execution (fun fuel => invoke [body] fuel 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory)) + (fun (final, values) => final = memory /\ values = [.scalar (.i64 ({{runningSum}}))]) := by + {{hypothesisLines}} + {{(setup ? "sorry" : "cil_contract_steps []\n exact And.intro rfl rfl")}} + """, + "hypothesesdirect" => $$""" + /-- Equations in scope before lookups outside a stepping driver, which scan them. -/ + theorem checked (right : CIL.W64) : + {{string.Join(" ∧\n ", operations.Select((operation, pc) => + $"body.code[{pc}]? = some (CIL.Instruction{operation})"))}} := by + {{hypothesisLines}} + {{(setup ? "sorry" : $"refine ⟨{string.Join(", ", operations.Select(_ => "?_"))}⟩\n" + + " all_goals cil_lookup []")}} + """, + "hypothesesloop" => $$""" + /-- Equations in scope before loop stepping, which it must not scan at each step. -/ + theorem checked (memory : Memory) (left right : CIL.W64) (frame : Frame) : + LoopReaches [body] 0 [.scalar (.i64 left), .scalar (.i64 right)] + (LoopState.mk 0 frame [] memory) + (LoopState.mk {{operations.Length - 1}} frame [.scalar (.i64 ({{runningSum}}))] memory) := by + {{hypothesisLines}} + {{(setup ? "sorry" : "cil_loop_steps")}} + """, + _ => $$""" + /-- Equations in scope before the run, which stepping must not scan again at each step. -/ + theorem checked (memory : Memory) (left right : CIL.W64) : + invoke [body] {{operations.Length + 1}} 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory = + .ok (memory, [.scalar (.i64 ({{runningSum}}))]) := by + {{hypothesisLines}} + {{(setup ? "sorry" : "cil_checked_steps")}} + """ + }; + var statement = workload switch + { + "hypotheses" or "hypothesescontract" or "hypothesesloop" or "hypotheseslookup" or "hypothesesdirect" => + HypothesesStatement(false), + "snapshots" => $$""" + /-- Numeric locals stored and reloaded through snapshots, each store updating one local. -/ + theorem checked (memory : Memory) (slots : List LocalSlot) (boundary : Nat) + (homes : CompleteWritableHomes memory boundary body.localKinds slots) + (inputs outputs : List ArgumentView) (valid : ValidCall memory inputs outputs) + (left right : BitVec 64) : + WP (Execution (fun fuel => run [body] fuel 0 0 + [.scalar (.i64 left), .scalar (.i64 right)] ⟨0, slots, [], 0⟩ [] memory)) + (fun result => result.2 = [.scalar (.i64 ({{SnapshotResult(requested)}}))]) := by + have formed := valid.wellFormed + have preserved := MemoryBelow.refl boundary memory + have authority := (MemoryBelow.refl memory.nextIdentity memory).accessBelow + have snapshots := LocalSnapshots.empty memory slots + cil_contract_steps (snapshots := snapshots) [] + exact rfl + """, + "reference" => $$""" + /-- Repeated byte offsets retain checked reference formation and displacement. -/ + theorem checked (memory : Memory) (target moved : Reference) (offset : CIL.W32) + (formed : formValue memory (.address target) = .ok (.reference (.address target))) + (displaced : add memory target 1 (BitVec.ofInt 64 offset.toInt) = .ok moved) : + invoke [body] {{operations.Length + 1}} 0 + [.reference (.address target), .scalar (.i32 offset)] memory = + .ok (memory, [.reference (.address target)]) := by + cil_checked_steps [formed, displaced] + """, + "table" => $$""" + /-- Repeated native table offsets need only the checked cursor addition of each step. -/ + theorem checked (memory : Memory) (table selected : CIL.MethodTable) (offset : CIL.W32) + (added : addNativeTable table memory (offset.signExtend 64) = .ok selected) : + invoke [body] {{operations.Length + 1}} 0 + [.methodTable table, .scalar (.i32 offset)] memory = + .ok (memory, [.methodTable table]) := by + cil_checked_steps [added] + """, + "memory" => $$""" /-- Checked reads and writes advance from reusable operation facts through the full body. -/ theorem checked (memory : Memory) (target : Reference) (word : CIL.W64) (loaded : read memory target 8 1 = .ok (numberBytes word.toNat 8)) @@ -97,45 +554,336 @@ invoke [body] {{operations.Length + 1}} 0 .ok (memory, [.scalar (.i64 word)]) := by have formed := access_reference_valid _ _ _ _ _ (read_access loaded) have loadFact := load_word64_of_read loaded - have storeFact := step_store64_of_write written + have storeFact := step_stind_i8_of_write written cil_checked_steps [formed, loadFact, storeFact] - """ - : $$""" + """, + "accumulate" => $$""" + /-- The running sum stays on the stack, so each step's state holds every earlier addition. -/ + theorem checked (memory : Memory) (left right : CIL.W64) : + invoke [body] {{operations.Length + 1}} 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory = + .ok (memory, [.scalar (.i64 (left{{string.Concat(Enumerable.Repeat(" + right", cycles))}}))]) := by + cil_checked_steps + """, + "contract" => $$""" + /-- Contract stepping keeps the running sum on the stack. -/ + theorem checked (memory : Memory) (left right : CIL.W64) : + WP (Execution (fun fuel => invoke [body] fuel 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory)) + (fun (final, values) => final = memory /\ + values = [.scalar (.i64 (left{{string.Concat(Enumerable.Repeat(" + right", cycles))}}))]) := by + cil_contract_steps [] + exact And.intro rfl rfl + """, + "loop" => $$""" + /-- Loop stepping walks a path whose stack holds the running sum. -/ + theorem checked (memory : Memory) (left right : CIL.W64) (frame : Frame) : + LoopReaches [body] 0 [.scalar (.i64 left), .scalar (.i64 right)] + (LoopState.mk 0 frame [] memory) + (LoopState.mk {{operations.Length - 1}} frame + [.scalar (.i64 (left{{string.Concat(Enumerable.Repeat(" + right", cycles))}}))] memory) := by + cil_loop_steps + """, + "loopfacts" => $$""" + /-- A case fact in the context makes every instruction read a second group of facts. -/ + theorem checked (memory : Memory) (left right : CIL.W64) (frame : Frame) (different : left ≠ right) : + LoopReaches [body] 0 [.scalar (.i64 left), .scalar (.i64 right)] + (LoopState.mk 0 frame [] memory) + (LoopState.mk {{operations.Length - 1}} frame + [.scalar (.i64 (left{{string.Concat(Enumerable.Repeat(" + right", cycles))}}))] memory) := by + cil_loop_steps + """, + "outcome" => $$""" + /-- The outcome walk steps every instruction to the return, which closes the goal. -/ + theorem checked (memory : Memory) (left right : CIL.W64) (frame : Frame) : + LoopOutcome [body] 0 [.scalar (.i64 left), .scalar (.i64 right)] + (fun _ values => values = [.scalar (.i64 (left{{string.Concat(Enumerable.Repeat(" + right", cycles))}}))]) + none (LoopState.mk 0 frame [] memory) := by + cil_loop_outcome_advance [] + rw [loop_outcome_none] + cil_loop_finish + """, + "lemma" => $$""" + /-- A universe-polymorphic lemma among the facts keeps the record, so the cost per step is flat. -/ + theorem checked (memory : Memory) (left right : CIL.W64) : + invoke [body] {{operations.Length + 1}} 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory = + .ok (memory, [.scalar (.i64 (left{{string.Concat(Enumerable.Repeat(" + right", cycles))}}))]) := by + cil_checked_steps [List.length_append] + """, + "depth" => $$""" + /-- The stack grows to {{depth}} values, then every one is added. -/ + theorem checked (memory : Memory) (left right : CIL.W64) : + invoke [body] {{operations.Length + 1}} 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory = + .ok (memory, [.scalar (.i64 ({{DepthSum(depth)}}))]) := by + cil_checked_steps + """, + "faulting" => $$""" + /-- Each faulting instruction advances from the fact that its operands do not fault. -/ + theorem checked (memory : Memory) (left right quotient remainder sum : CIL.W64) (narrowed : CIL.W32) + (divided : CIL.divideValues true false (.i64 left) (.i64 right) = .ok (.i64 quotient)) + (reduced : CIL.divideValues true true (.i64 left) (.i64 right) = .ok (.i64 remainder)) + (added : CIL.checkedValues .add true (.i64 left) (.i64 right) = .ok (.i64 sum)) + (converted : CIL.convertInteger 32 true false (.i64 left) = .ok (.i32 narrowed)) : + invoke [body] {{operations.Length + 1}} 0 + [.scalar (.i64 left), .scalar (.i64 right)] memory = + .ok (memory, [.scalar (.i64 left)]) := by + cil_checked_steps [divided, reduced, added, converted] + """, + "statics" => $$""" + /-- Every static load reads the descriptor its pc's site binds and leaves memory unchanged. -/ + theorem checked (memory : Memory) : + invoke [body] {{operations.Length + 1}} 0 [] memory = .ok (memory, [.scalar (.i32 7)]) := by + cil_checked_steps + rfl + """, + "arguments" => $$""" + /-- Each argument is read once and added to the running sum of the ones before it. -/ + theorem checked (memory : Memory) (argument : Nat -> CIL.W32) : + invoke [body] {{operations.Length + 1}} 0 [{{incoming}}] memory = + .ok (memory, [.scalar (.i32 ({{sum}}))]) := by + cil_checked_steps + """, + "allocation" => $$""" + /-- Every `newarr` resolves its class element and allocates by one fact that keeps the memory. -/ + theorem checked (memory : Memory) (array : Reference) + (allocated : newReferenceArray memory ({{NodeType}}) (.i32 2) = .ok (array, memory)) : + invoke [body] {{operations.Length + 1}} 0 [] memory = .ok (memory, [.scalar (.i32 0)]) := by + cil_checked_steps [allocated] + """, + "constructions" => $$""" + /-- Every `newobj` allocates by one fact that keeps the memory and enters the constructor. -/ + theorem checked (memory : Memory) (object : Reference) + (allocated : allocateObject memory ⟨{{NodeType}}, []⟩ 8 = .ok (object, memory)) + (formed : formObject memory (.address object) = .ok (.object (.address object))) : + invoke [body, constructor] {{operations.Length + 3}} 0 [] memory = + .ok (memory, [.scalar (.i32 0)]) := by + cil_checked_steps [allocated, formed] + """, + "objects" => $$""" + /-- Every `ldfld` reads its slot from the field's shape fact and two memory facts of its object. -/ + theorem checked (memory : Memory) (object field : Reference) (word : CIL.W64) + (formed : formObject memory (.address object) = .ok (.object (.address object))) + (located : objectFieldAddress memory (.address object) ({{NodeType}}) 8 = .ok field) + (loaded : loadValue memory (.address field) 8 false = .ok (.i64 word)) : + invoke [body] {{operations.Length + 1}} 0 [.object (.address object)] memory = + .ok (memory, [.scalar (.i32 0)]) := by + cil_checked_steps [formed, located, loaded] + """, + "structfields" => $$""" + /-- Every struct field load and store reads its address and memory access from supplied facts. -/ + theorem checked (memory : Memory) (target field : Reference) (word : CIL.W64) + (formed : form memory target = .ok target) + (located : add memory target 1 (BitVec.ofNat 64 8) = .ok field) + (loaded : loadValue memory (.address field) 8 false = .ok (.i64 word)) + (written : write memory field (numberBytes word.toNat 8) ordinaryAccessAlignment = .ok memory) : + invoke [body] {{operations.Length + 1}} 0 + [.reference (.address target), .scalar (.i64 word)] memory = + .ok (memory, [.scalar (.i32 0)]) := by + cil_checked_steps [formed, located, loaded, written] + """, + "structfieldscontract" => $$""" + /-- Contract stepping reads each struct field access from the same supplied facts. -/ + theorem checked (memory : Memory) (target field : Reference) (word : CIL.W64) + (formed : form memory target = .ok target) + (located : add memory target 1 (BitVec.ofNat 64 8) = .ok field) + (loaded : loadValue memory (.address field) 8 false = .ok (.i64 word)) + (written : write memory field (numberBytes word.toNat 8) ordinaryAccessAlignment = .ok memory) : + WP (Execution (fun fuel => invoke [body] fuel 0 + [.reference (.address target), .scalar (.i64 word)] memory)) + (fun (final, values) => final = memory /\ values = [.scalar (.i32 0)]) := by + cil_contract_steps [formed, located, loaded, written] + exact And.intro rfl rfl + """, + "objectconstructors" => $$""" + /-- Every call of the `System.Object` constructor consumes the receiver and keeps the memory. -/ + theorem checked (memory : Memory) (object : Reference) + (formed : formObject memory (.address object) = .ok (.object (.address object))) : + invoke [body] {{operations.Length + 1}} 0 [.object (.address object)] memory = + .ok (memory, [.scalar (.i32 0)]) := by + cil_checked_steps [formed] + """, + "instance" => $$""" + /-- Every receiver-checked call enters the method on the live receiver and keeps the memory. -/ + theorem checked (memory : Memory) (object : Reference) + (formed : formObject memory (.address object) = .ok (.object (.address object))) : + invoke [body, instanceMethod] {{operations.Length + 3}} 0 [.object (.address object)] memory = + .ok (memory, [.scalar (.i32 0)]) := by + cil_checked_steps [formed] + """, + "virtual" => $$""" + /-- Every virtual call reads the receiver's class and enters the entry the table lists for it. -/ + theorem checked (memory : Memory) (object : Reference) + (formed : formObject memory (.address object) = .ok (.object (.address object))) + (typed : runtimeTypeOf memory object = + .ok { identity := {{VirtualClass(requested - 1)}}, bases := [] }) : + invoke [body, instanceMethod] {{operations.Length + 3}} 0 [.object (.address object)] memory = + .ok (memory, [.scalar (.i32 0)]) := by + cil_checked_steps [formed, typed] + """, + "elements" => $$""" + /-- Every element access reads the one element the index names, whose store keeps the memory. -/ + theorem checked (memory : Memory) (view : ArrayView) (index : CIL.W32) (element : Reference) + (word : CIL.Value) (valid : view.validate memory = .ok ()) + (found : arrayElement memory view index.toNat 4 = .ok element) + (loaded : loadValue memory (.address element) 4 false = .ok word) + (written : write memory element (numberBytes index.toNat 4) 1 = .ok memory) : + invoke [body] {{operations.Length + 1}} 0 [.array view, .scalar (.i32 index)] memory = + .ok (memory, [.scalar (.i32 0)]) := by + have loadFact := step_ldelem_of_element .i4 (index := .i32 index) rfl found loaded + have storeFact := step_stelem_narrow_of_write .i4 (index := .i32 index) rfl found written + cil_checked_steps [valid, loadFact, storeFact] + """, + "typedelements" => $$""" + /-- Every typed access evaluates to the element the index names, and every store keeps the + memory. -/ + theorem checked (memory : Memory) (view pairs : ArrayView) (index : CIL.W32) (element : Reference) + (word pair : CIL.Value) (valid : view.validate memory = .ok ()) + (pairsValid : pairs.validate memory = .ok ()) + (found : arrayElement memory view index.toNat 4 = .ok element) + (loaded : loadValue memory (.address element) 4 false = .ok word) + (written : write memory element (numberBytes index.toNat 4) 1 = .ok memory) + (specified : pair.hasUnspecified = false) + (pairLoaded : ∀ rest, arrayTransferInstruction ({{PairElement}}) false + (.scalar (.i32 index) :: .array pairs :: rest) memory = .ok (memory, .scalar pair :: rest)) + (pairStored : ∀ rest, arrayTransferInstruction ({{PairElement}}) true + (.scalar pair :: .scalar (.i32 index) :: .array pairs :: rest) memory = .ok (memory, rest)) : + invoke [body] {{operations.Length + 1}} 0 [.array view, .scalar (.i32 index), .array pairs] + memory = .ok (memory, [.scalar (.i32 0)]) := by + have load := step_ldelem_any_of_element (index := .i32 index) body_elementShape0 rfl rfl + found loaded + have store := step_stelem_any_of_write (index := .i32 index) body_elementShape0 rfl rfl + found written + have pairLoad := step_ldelem_any_of_transfer (index := .i32 index) body_elementShape1 rfl rfl + pairLoaded + have pairStore := step_stelem_any_of_transfer (index := .i32 index) body_elementShape1 rfl rfl + specified pairStored + cil_checked_steps [valid, pairsValid, load, store, pairLoad, pairStore] + """, + "typedsteps" => $$""" + /-- Every typed access steps by one supplied fact, and every store keeps the memory. -/ + theorem checked (memory : Memory) (view pairs : ArrayView) (index : CIL.W32) (word pair : CIL.Value) + (valid : view.validate memory = .ok ()) (pairsValid : pairs.validate memory = .ok ()) + (load : ∀ body pc args frame rest, step body (.ldelem_any ({{Int32Type}})) pc args frame + (.scalar (.i32 index) :: .array view :: rest) memory = + .ok (.next (pc + 1) (.scalar word :: rest) frame memory)) + (store : ∀ body pc args frame rest, step body (.stelem_any ({{Int32Type}})) pc args frame + (.scalar (.i32 index) :: .scalar (.i32 index) :: .array view :: rest) memory = + .ok (.next (pc + 1) rest frame memory)) + (pairLoad : ∀ body pc args frame rest, step body (.ldelem_any ({{PairType}})) pc args frame + (.scalar (.i32 index) :: .array pairs :: rest) memory = + .ok (.next (pc + 1) (.scalar pair :: rest) frame memory)) + (pairStore : ∀ body pc args frame rest, step body (.stelem_any ({{PairType}})) pc args frame + (.scalar pair :: .scalar (.i32 index) :: .array pairs :: rest) memory = + .ok (.next (pc + 1) rest frame memory)) : + invoke [body] {{operations.Length + 1}} 0 [.array view, .scalar (.i32 index), .array pairs] + memory = .ok (memory, [.scalar (.i32 0)]) := by + cil_checked_steps [valid, pairsValid, load, store, pairLoad, pairStore] + """, + "addresses" => $$""" + /-- Every element address is the element the index names, writable or read-only. -/ + theorem checked (memory : Memory) (view : ArrayView) (index : CIL.W32) (element : Reference) + (valid : view.validate memory = .ok ()) (typed : view.elementType = .signed32) + (found : arrayElement memory view index.toNat 4 = .ok element) : + invoke [body] {{operations.Length + 1}} 0 [.array view, .scalar (.i32 index)] memory = + .ok (memory, [.scalar (.i32 0)]) := by + have writable := step_ldelema_of_element false body_elementShape0 typed (index := .i32 index) rfl + found + have readOnly := step_ldelema_of_element true body_elementShape0 typed (index := .i32 index) rfl + found + cil_checked_steps [valid, writable, readOnly] + """, + "addresssteps" => $$""" + /-- Every element address steps by one supplied fact. -/ + theorem checked (memory : Memory) (view : ArrayView) (index : CIL.W32) (element : Reference) + (valid : view.validate memory = .ok ()) + (writable : ∀ body pc args frame rest, step body (.ldelema ({{Int32Type}}) false) pc args frame + (.scalar (.i32 index) :: .array view :: rest) memory = + .ok (.next (pc + 1) (.reference (.address element) :: rest) frame memory)) + (readOnly : ∀ body pc args frame rest, step body (.ldelema ({{Int32Type}}) true) pc args frame + (.scalar (.i32 index) :: .array view :: rest) memory = + .ok (.next (pc + 1) (.readOnlyReference (.address element) :: rest) frame memory)) : + invoke [body] {{operations.Length + 1}} 0 [.array view, .scalar (.i32 index)] memory = + .ok (memory, [.scalar (.i32 0)]) := by + cil_checked_steps [valid, writable, readOnly] + """, + "homes" => $$""" + /-- Each reference parameter is read once from the home slot of a literal frame. -/ + theorem checked (memory : Memory) (target : ManagedReference) (activation : Nat) + (formed : formValue memory target = .ok (.reference target)) : + run [body] {{operations.Length + 1}} 0 0 [.reference target] + { activation := activation, locals := [{{homeSlots}}], owned := [] } [] memory = + .ok (memory, [.reference target]) := by + cil_checked_steps [formed] + """, + "calls" or "tablecalls" => $$""" + /-- Each call enters the callee, which returns its argument, so the value passes through. -/ + theorem checked (memory : Memory) (left : CIL.W64) : + invoke [body, callee] {{operations.Length + 3}} 0 [.scalar (.i64 left)] memory = + .ok (memory, [.scalar (.i64 left)]) := by + cil_checked_steps + """, + "locals" => $$""" + /-- Every cycle replaces all eight reference locals of a literal frame. -/ + theorem checked (memory : Memory) (target : ManagedReference) (activation : Nat) + (formed : formValue memory target = .ok (.reference target)) : + run [body] {{operations.Length + 1}} 0 0 [.reference target] + { activation := activation, locals := [{{localSlots}}], owned := [] } [] memory = + .ok (memory, [.reference target]) := by + cil_checked_steps [formed] + """, + _ => $$""" /-- All scalar instructions execute and return the original first operand. -/ theorem checked (memory : Memory) (left right : CIL.W64) : invoke [body] {{operations.Length + 1}} 0 [.scalar (.i64 left), .scalar (.i64 right)] memory = .ok (memory, [.scalar (.i64 left)]) := by cil_checked_steps - """; - const string Audit = """ + """ + }; + // The snapshot lemmas rest on choice, as `LocalSnapshots.store_at` and `LocalSnapshots.load` do. + var axioms = snapshots ? "propext, Classical.choice, Quot.sound" + : workload == "hypothesesdirect" ? "propext" : "propext, Quot.sound"; + var Audit = $$""" - /-- info: 'checked' depends on axioms: [propext, Quot.sound] -/ + /-- info: 'checked' depends on axioms: [{{axioms}}] -/ #guard_msgs (whitespace := lax) in #print axioms checked """; File.WriteAllText(path, header); - var baseline = Compile(module, path, "header"); + var baseline = full ? Compile(module, path, "header") : null; File.WriteAllText(path, header + statement + "\n" + Audit + "\n"); - var samples = Enumerable.Range(0, Repetitions).Select(index => Compile(module, path, "proof" + index)).ToArray(); + var samples = Enumerable.Range(0, full ? Repetitions : 1) + .Select(index => Compile(module, path, "proof" + index)).ToArray(); var commands = samples.Select(sample => sample.Commands.Single(command => command.Label == "checked")).ToArray(); + // The message carries every sample, so a run that differs says which count moved and by how much. if (commands.Select(command => command.Heartbeats).Distinct().Count() != 1 || commands.Select(command => command.KernelHeartbeats).Distinct().Count() != 1 || samples.Select(sample => sample.Bytes).Distinct().Count() != 1) - throw new InvalidOperationException("Stepping work did not repeat exactly for " + module); - var proofBytes = samples[0].Bytes - baseline.Bytes; - if (proofBytes <= 0) throw new InvalidOperationException("Missing compiled proof bytes for " + module); + throw new InvalidOperationException($"Stepping work did not repeat exactly for {module}: heartbeats " + + $"{string.Join(", ", commands.Select(command => command.Heartbeats))}; kernel heartbeats " + + $"{string.Join(", ", commands.Select(command => command.KernelHeartbeats))}; bytes " + + $"{string.Join(", ", samples.Select(sample => sample.Bytes))}"); + var proofBytes = baseline is null ? 0 : samples[0].Bytes - baseline.Bytes; + if (full && proofBytes <= 0) throw new InvalidOperationException("Missing compiled proof bytes for " + module); var row = new SteppingMeasurement(workload, operations.Length, cycles, memory ? 2 * cycles + 1 : 0, commands[0].Heartbeats, commands[0].KernelHeartbeats, proofBytes, Median(commands.Select(command => command.ElapsedMs)), Median(commands.Select(command => command.KernelMs)), Median(samples.Select(sample => sample.Commands.Sum(command => command.ElapsedMs))), - samples.Select(sample => sample.ProcessMs).ToArray(), baseline.ProcessMs) + samples.Select(sample => sample.ProcessMs).ToArray(), baseline?.ProcessMs ?? 0) { + Size = requested, + Full = full, CompiledModuleBytes = samples[0].Bytes, - HeaderModuleBytes = baseline.Bytes, - HeaderCommandsMs = baseline.Commands.Sum(command => command.ElapsedMs), + HeaderModuleBytes = baseline?.Bytes ?? 0, + HeaderCommandsMs = baseline?.Commands.Sum(command => command.ElapsedMs) ?? 0, SourceSha256 = Sha(path) }; + // Elaborating each hypothesis costs the same with or without stepping, so the same statement + // with `sorry` for the stepping tactic is measured and subtracted. + if (hypotheses) + row = WithoutSetup(row, SetupCost(module, header, HypothesesStatement(true))); Console.WriteLine(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"{workload} {row.Instructions} instructions: {row.FrontendHeartbeatsPerInstruction:F0} frontend HB, " + $"{row.KernelHeartbeatsPerInstruction:F0} kernel HB, {row.ProofBytesPerInstruction:F0} proof bytes, " + @@ -143,6 +891,25 @@ invoke [body] {{operations.Length + 1}} 0 return row; } +// What elaborating and checking the statement `statement` costs without stepping. +CommandSample SetupCost(string module, string header, string statement) +{ + var setupModule = module + "Setup"; + var setupPath = Path.Combine(directory, setupModule + ".lean"); + File.WriteAllText(setupPath, header + statement + "\n"); + var sample = Compile(setupModule, setupPath, "setup"); + return sample.Commands.Single(command => command.Label == "checked"); +} + +// The stepping share of `row`: its frontend heartbeats, kernel heartbeats and frontend milliseconds +// less those of `setup`, the same statement closed by `sorry`. +static SteppingMeasurement WithoutSetup(SteppingMeasurement row, CommandSample setup) => row with +{ + FrontendHeartbeats = row.FrontendHeartbeats - setup.Heartbeats, + KernelHeartbeats = row.KernelHeartbeats - setup.KernelHeartbeats, + FrontendMs = Math.Max(0, row.FrontendMs - setup.ElapsedMs) +}; + CompiledSample Compile(string module, string source, string suffix) { var report = Path.Combine(directory, module + "." + suffix + ".json"); @@ -158,48 +925,143 @@ CompiledSample Compile(string module, string source, string suffix) return new(commands, new FileInfo(compiled).Length, timer.ElapsedMilliseconds); } +// The class of entry `entry` of the virtual body's dispatch table. +static string VirtualClass(int entry) => $".object \"Examples\" \"Examples.Class{entry}\""; + static long Median(IEnumerable values) { var sorted = values.Order().ToArray(); return sorted[sorted.Length / 2]; } -static void CheckGrowth(SteppingMeasurement shorter, SteppingMeasurement longer) +// A body whose argument count grows with its length may cost per instruction at most linearly in that +// count, since each read indexes the argument list. A virtual call looks its class up in a literal list, so it +// may cost at most linearly in the table size. Any other body costs a bounded amount per instruction. +static void CheckGrowth(SteppingMeasurement shorter, SteppingMeasurement longer, bool linearInArguments = false, + bool linearInTable = false) { + var workBudget = linearInArguments ? (double)longer.Instructions / shorter.Instructions + : linearInTable ? (double)longer.Size / shorter.Size : WorkGrowthLimit; Check("frontend heartbeats", shorter.FrontendHeartbeatsPerInstruction, - longer.FrontendHeartbeatsPerInstruction, WorkGrowthLimit); + longer.FrontendHeartbeatsPerInstruction, workBudget); Check("kernel heartbeats", shorter.KernelHeartbeatsPerInstruction, - longer.KernelHeartbeatsPerInstruction, WorkGrowthLimit); - Check("compiled proof bytes", shorter.ProofBytesPerInstruction, - longer.ProofBytesPerInstruction, WorkGrowthLimit); - Check("frontend milliseconds", shorter.FrontendMsPerInstruction, - longer.FrontendMsPerInstruction, TimeGrowthLimit); + longer.KernelHeartbeatsPerInstruction, workBudget); + // Proof bytes require the header-only module; timing remains a diagnostic measurement. + if (shorter.Full && longer.Full) + { + Check("compiled proof bytes", shorter.ProofBytesPerInstruction, + longer.ProofBytesPerInstruction, WorkGrowthLimit); + } void Check(string metric, double before, double after, double budget) { if (before <= 0 || after / before > budget) throw new InvalidOperationException(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"{longer.Workload} {metric}/instruction grew from {before:F3} to {after:F3} " + - $"between {shorter.Instructions} and {longer.Instructions} instructions; budget {budget:F2}.")); + $"between sizes {shorter.Size} and {longer.Size} ({shorter.Instructions} and " + + $"{longer.Instructions} instructions); budget {budget:F2}.")); } } +// Heartbeats per hypothesis per instruction between the smallest and the largest number of +// hypotheses, which the limit bounds absolutely so that a cost far under the ratio limit still shows. +static void CheckHypothesisCost(SteppingMeasurement fewest, SteppingMeasurement most) +{ + var added = (double)(most.FrontendHeartbeats - fewest.FrontendHeartbeats) / + ((most.Size - fewest.Size) * most.Instructions); + if (added > HypothesisCostLimit) + throw new InvalidOperationException(string.Create(System.Globalization.CultureInfo.InvariantCulture, + $"{most.Workload}: each hypothesis in scope added {added:F0} heartbeats to each instruction " + + $"between {fewest.Size} and {most.Size} hypotheses; limit {HypothesisCostLimit:F0}.")); +} + static void ScalingChecks() { + var few = new SteppingMeasurement("test", 62, 30, 0, 7_900_000, 1, 0, 0, 0, 0, [], 0) { Size = 16 }; + CheckHypothesisCost(few, few with { Size = 256, FrontendHeartbeats = 7_900_000 + 240L * 62 * 40 }); + var costly = few with { Size = 256, FrontendHeartbeats = 7_900_000 + 240L * 62 * 380 }; + var rejected = false; + try { CheckHypothesisCost(few, costly); } + catch (InvalidOperationException) { rejected = true; } + if (!rejected) + throw new InvalidOperationException("The hypothesis limit accepted a cost of 380 heartbeats per hypothesis."); + var stepped = WithoutSetup(few with { FrontendMs = 900 }, new CommandSample("checked", 400_000, 0, 700, 0)); + if (stepped.FrontendHeartbeats != 7_500_000 || stepped.FrontendMs != 200) + throw new InvalidOperationException("A hypotheses row kept the cost of elaborating its hypotheses."); var baseline = new SteppingMeasurement("test", 16, 4, 0, 16000, 1600, 32000, 160, 16, 200, [], 1); var linear = baseline with { Instructions = 64, FrontendHeartbeats = 64000, KernelHeartbeats = 6400, ProofBytes = 128000, FrontendMs = 640 }; CheckGrowth(baseline, linear); + CheckGrowth(baseline, linear with { FrontendMs = 2560 }); foreach (var mutant in new[] { linear with { FrontendHeartbeats = 256000 }, - linear with { KernelHeartbeats = 25600 }, linear with { ProofBytes = 512000 }, - linear with { FrontendMs = 2560 } }) + linear with { KernelHeartbeats = 25600 }, linear with { ProofBytes = 512000 } }) { try { CheckGrowth(baseline, mutant); } catch (InvalidOperationException) { continue; } throw new InvalidOperationException("Scaling budget accepted a quadratic work mutant."); } - Console.WriteLine("PASS stepping budgets accept linear work and reject each quadratic metric"); + // Per-instruction work four times higher at four times the arguments is linear; sixteen times is not. + var reduced = baseline with { Full = false }; + var linearInArguments = reduced with { Instructions = 64, KernelHeartbeats = 4 * 6400 }; + CheckGrowth(reduced, linearInArguments, linearInArguments: true); + if (!Rejects(() => CheckGrowth(reduced, linearInArguments with { KernelHeartbeats = 16 * 6400 }, true))) + throw new InvalidOperationException("The argument budget accepted a per-instruction quadratic mutant."); + var fieldBody = baseline with { Workload = "structfields", Size = 16, Instructions = 14, + FrontendHeartbeats = 3_763_975, KernelHeartbeats = 86_336 }; + CheckReferenceBudget(fieldBody); + if (!Rejects(() => CheckReferenceBudget(fieldBody with { KernelHeartbeats = 2 * fieldBody.KernelHeartbeats })) || + !Rejects(() => CheckReferenceBudget(fieldBody with { FrontendHeartbeats = 2 * fieldBody.FrontendHeartbeats }))) + throw new InvalidOperationException("The field budget accepted twice the reference cost."); + // With the same instructions at every size, per-instruction cost must stay flat in the size: a + // step that scans each hypothesis in scope grows with it. + var flat = baseline with { Size = 256, FrontendHeartbeats = 17000, KernelHeartbeats = 1700 }; + CheckGrowth(baseline with { Size = 16 }, flat); + var scanning = flat with { FrontendHeartbeats = 24000 }; + try { CheckGrowth(baseline with { Size = 16 }, scanning); } + catch (InvalidOperationException) + { + Console.WriteLine("PASS stepping budgets accept linear work and reject each quadratic metric"); + return; + } + throw new InvalidOperationException("The hypothesis budget accepted a cost that grows with the hypotheses."); +} + +// Lean 4.35.0 checked-theorem heartbeats: field fixtures at 8cc33b38 and typed Math.Max calls at 9c53e7f5. +static void CheckReferenceBudget(SteppingMeasurement decoded) +{ + var (instructions, frontend, kernel) = (decoded.Workload, decoded.Size) switch + { + ("structfields", 16) => (14, 3_763_975, 86_336), + ("structfields", 64) => (62, 9_767_942, 194_175), + ("structfields", 256) => (254, 33_653_612, 625_474), + ("structfieldscontract", 16) => (14, 3_335_564, 86_060), + ("structfieldscontract", 64) => (62, 9_598_624, 192_821), + ("structfieldscontract", 256) => (254, 34_710_632, 619_863), + ("intrinsiccalls", 16) => (14, 3_434_418, 87_411), + ("intrinsiccalls", 64) => (62, 9_828_778, 200_230), + ("intrinsiccalls", 256) => (254, 35_106_686, 651_597), + _ => throw new InvalidOperationException($"Missing reference baseline for {decoded.Workload} {decoded.Size}.") + }; + if (decoded.Instructions != instructions) + throw new InvalidOperationException($"{decoded.Workload} baseline has {instructions} instructions, measured {decoded.Instructions}."); + Check("frontend heartbeats", decoded.FrontendHeartbeats, frontend, DecodedFrontendLimit); + Check("kernel heartbeats", decoded.KernelHeartbeats, kernel, DecodedKernelLimit); + + void Check(string metric, double decodedCost, double referenceCost, double limit) + { + var ratio = decodedCost / referenceCost; + if (referenceCost <= 0 || ratio > limit) + throw new InvalidOperationException(string.Create(System.Globalization.CultureInfo.InvariantCulture, + $"""{decoded.Workload} {metric}: {ratio:F4} of reference at size {decoded.Size}; limit {limit:F4}""")); + } +} + +static bool Rejects(Action check) +{ + try { check(); } + catch (InvalidOperationException) { return true; } + return false; } internal sealed record CommandSample(string Label, long Heartbeats, long KernelHeartbeats, @@ -209,6 +1071,10 @@ internal sealed record SteppingMeasurement(string Workload, int Instructions, in long FrontendHeartbeats, long KernelHeartbeats, long ProofBytes, long FrontendMs, long KernelMs, long ModuleCommandsMs, long[] ProcessMs, long HeaderProcessMs) { + /// The requested size: instructions, or hypotheses in scope for the hypotheses body. + public int Size { get; init; } + /// Whether the row was compiled three times with a header-only baseline. + public bool Full { get; init; } = true; public long CompiledModuleBytes { get; init; } public long HeaderModuleBytes { get; init; } public long HeaderCommandsMs { get; init; } diff --git a/Tests/StoreFacts.lean b/Tests/StoreFacts.lean index 6bae8e7e6..cea9b7061 100644 --- a/Tests/StoreFacts.lean +++ b/Tests/StoreFacts.lean @@ -1,26 +1,36 @@ import CIL.Safety.MethodContract import CIL.Safety.OutcomeContract +import Tests.Support +import Tests.WordFields /-! Store facts stated in their source form, as a carry or borrow helper's proof states them, rather than in the form the checked steps compute. -/ namespace Tests.StoreFacts +open Tests.WordFields open CIL.Safety /-- `*sum = a + b + c; *carry = (a + b + c < a) ? 1 : 0`. -/ @[cil_code] def carryMethod : CIL.Method := { - code := [.arg 3, .arg 0, .arg 1, .add, .arg 2, .add, .store64, - .arg 4, .arg 0, .arg 1, .add, .arg 2, .add, .arg 0, .ltu, .convU8, .store64, .ret] + code := [.ldarg 3, .ldarg 0, .ldarg 1, .add, .ldarg 2, .add, .stind .i8, + .ldarg 4, .ldarg 0, .ldarg 1, .add, .ldarg 2, .add, .ldarg 0, .clt_un, .conv_u8, .stind .i8, + .ret] locals := [], returnsValue := false } @[cil_code] def carryProgram : CIL.Program := [carryMethod] /-- `*difference = a - b - c; *borrow = (a < b) | (c & (a == b))`, with sign-extended flags. -/ @[cil_code] def borrowMethod : CIL.Method := { - code := [.arg 3, .arg 0, .arg 1, .sub, .arg 2, .sub, .store64, - .arg 4, .arg 0, .arg 1, .ltu, .convI8, .arg 2, .arg 0, .arg 1, .eq, .convI8, .band, .bor, .store64, .ret] + code := [.ldarg 3, .ldarg 0, .ldarg 1, .sub, .ldarg 2, .sub, .stind .i8, + .ldarg 4, .ldarg 0, .ldarg 1, .clt_un, .conv_i8, .ldarg 2, .ldarg 0, .ldarg 1, .ceq, .conv_i8, + .and, .or, .stind .i8, .ret] locals := [], returnsValue := false } @[cil_code] def borrowProgram : CIL.Program := [borrowMethod] +/-- `*target = word` through a native-int store. -/ +@[cil_code] def nativeWordMethod : CIL.Method := { + code := [.ldarg 1, .ldarg 0, .stind .i, .ret], locals := [], returnsValue := false } +@[cil_code] def nativeWordProgram : CIL.Program := [nativeWordMethod] + def carryOut (a b c : CIL.W64) : CIL.W64 := (if a + b + c < a then (1 : CIL.W32) else 0).zeroExtend 64 def borrowOut (a b c : CIL.W64) : CIL.W64 := @@ -53,6 +63,16 @@ theorem borrow_stores (a b c : CIL.W64) (difference borrow : Reference) (m m1 m2 #print axioms borrow_stores +/-- An eight-byte write fact steps `stind.i` as it steps `stind.i8`. -/ +theorem native_word_stores (word : CIL.W64) (target : Reference) (m m1 : Memory) + (formed : form m target = .ok target) + (written : write m target (numberBytes word.toNat 8) 1 = .ok m1) : + invoke nativeWordProgram (CIL.executionBound nativeWordProgram 0) 0 + [.scalar (.i64 word), .reference (.address target)] m = .ok (m1, []) := by + cil_checked_steps [formed, written] + +#print axioms native_word_stores + -- The same facts drive `cil_contract_steps` on an existential run. theorem carry_contract_steps (a b c : CIL.W64) (sum carry : Reference) (m m1 m2 : Memory) (formedSum : form m sum = .ok sum) (formedCarry : form m carry = .ok carry) @@ -141,12 +161,13 @@ theorem contract_normalize_once (words : Nat → Nat) (memory : Memory) (_zero : exact ⟨rfl, rfl⟩ @[cil_code] def twentySevenRaises : CIL.Method := - { code := List.replicate 26 .nop ++ [.throw "Test.Exception"], locals := [], returnsValue := false } + { code := List.replicate 26 .nop ++ [.throw .baseException], locals := [], + returnsValue := false } @[cil_code] def twentySevenRaisesProgram : CIL.Program := [twentySevenRaises] theorem raise_normalize_once (words : Nat → Nat) (memory : Memory) (_zero : words 0 = 0) : - Raises twentySevenRaisesProgram 0 [] memory "Test.Exception" := by + Raises twentySevenRaisesProgram 0 [] memory .baseException := by fact_normalizations 1 in cil_raise_steps [_zero] @@ -154,7 +175,8 @@ theorem raise_normalize_once (words : Nat → Nat) (memory : Memory) (_zero : wo { code := [.nop, .ret], locals := [], returnsValue := false } @[cil_code] def normalizationCaller : CIL.Method := - { code := [.call 1 0, .ret], locals := [], returnsValue := false } + { code := [.call (Tests.callee 0) {}, .ret], links := [(0, .direct 1)], locals := [], + returnsValue := false } @[cil_code] def normalizationCallProgram : CIL.Program := [normalizationCaller, normalizationCallee] @@ -169,29 +191,29 @@ theorem large_indexed_fact (words : Fin 65536 → Nat) (all : ∀ index, words i cil_fact_simp [] [all] @[cil_code] def fourLimb : CIL.Method := - { code := [.arg 0, .fieldAt 0 8, .arg 0, .fieldAt 8 8, .arg 0, .fieldAt 16 8, - .arg 0, .fieldAt 24 8, .pop, .pop, .pop, .ret], locals := [], returnsValue := true } + { environment := wordsEnvironment, code := [.ldarg 0, .ldfld word0, .ldarg 0, .ldfld word1, .ldarg 0, .ldfld word2, + .ldarg 0, .ldfld word3, .pop, .pop, .pop, .ret], locals := [], returnsValue := true } @[cil_code] def fourLimbProgram : CIL.Program := [fourLimb] theorem indexed_fields (memory : Memory) (left : Reference) (words : Fin 4 → CIL.W64) (formed : form memory left = .ok left) (limbs : ∀ (index : Fin 4) rest, - instruction (.fieldAt (8 * index.val) 8) (.reference (.address left) :: rest) memory = + loadValueField (8 * index.val) 8 false (.reference (.address left) :: rest) memory = .ok (memory, .scalar (.i64 (words index)) :: rest)) : invoke fourLimbProgram 12 0 [.reference (.address left)] memory = .ok (memory, [.scalar (.i64 (words 0))]) := by cil_checked_steps [formed, limbs] @[cil_code] def literalBranch : CIL.Method := - { code := [.arg 0, .fieldAt 8 8, .brzero 5, .const32 9, .ret, .const32 0, .ret], + { environment := wordsEnvironment, code := [.ldarg 0, .ldfld word1, .brfalse 5, .ldc_i4 9, .ret, .ldc_i4 0, .ret], locals := [], returnsValue := true } @[cil_code] def literalBranchProgram : CIL.Program := [literalBranch] theorem constructor_index_branch (memory : Memory) (left : Reference) (words : Fin 4 → CIL.W64) (bound : 1 < 4) (formed : form memory left = .ok left) - (field : ∀ rest, instruction (.fieldAt 8 8) (.reference (.address left) :: rest) memory = + (field : ∀ rest, loadValueField 8 8 false (.reference (.address left) :: rest) memory = .ok (memory, .scalar (.i64 (words ⟨1, bound⟩)) :: rest)) (zero : words 1 = 0) : invoke literalBranchProgram 7 0 [.reference (.address left)] memory = diff --git a/Tests/StoreReturnVariants.cs b/Tests/StoreReturnVariants.cs new file mode 100644 index 000000000..5ebc8ddd5 --- /dev/null +++ b/Tests/StoreReturnVariants.cs @@ -0,0 +1,40 @@ +partial class Program +{ + /// Checks a store and load through the fixture's reference for every word. + void VerifyStoreReturn(string assembly, string selector, string example, string wrongAssembly, + string wrongSelector, string wrongOutput, int width) + { + var bytes = width / 8; + var contract = $$""" + /-- A store followed by a load returns the word supplied by the caller. -/ + def contract : MethodContract W{{width}} where + args input := [Tests.reference, .scalar (.i{{width}} input)] + pre _ memory := memory = Tests.caller + footprint _ := [(⟨0, 0⟩, {{bytes}})] + post input _ _ values := values = [.scalar (.i{{width}} input)] + """; + var positive = $$""" + /-- Only the bytes at the supplied reference change. -/ + theorem correctness : contract.Holds Extracted.program 0 := by + rintro input _ rfl + refine ⟨8, _, rfl, ?_, _, rfl, ?_⟩ + · exact Confined.write + (rfl : write Tests.caller ⟨0, 0⟩ (numberBytes input.toNat {{bytes}}) 1 = .ok _) + (by simp [contract, numberBytes]) + · exact Except.ok.inj (ExampleProof.correctness input) + """; + const string counterexample = """ + /-- Storing another value changes the value read back at input one. -/ + theorem correctness : contract.Refuted Extracted.program 0 := + .of_return (input := 1) (initial := Tests.caller) rfl + (rfl : invoke Extracted.program 16 0 (contract.args 1) Tests.caller = .ok _) + (by rintro ⟨-, _, rfl, wrong⟩ + dsimp only [contract] at wrong + revert wrong + decide) + """; + VerifyContractVariant(assembly, "Examples", selector, Path.Combine(wrongOutput, "Positive"), wrongAssembly, + wrongSelector, wrongOutput, contract, positive, counterexample, example: example, + imports: "import Tests.Support"); + } +} diff --git a/Tests/StreamingValidationScaling.lean b/Tests/StreamingValidationScaling.lean index f4cf29280..6e2621e38 100644 --- a/Tests/StreamingValidationScaling.lean +++ b/Tests/StreamingValidationScaling.lean @@ -1,5 +1,5 @@ import CIL.Extraction.Constrained -import CIL.Extraction.ExactIntrinsics +import CIL.Extraction.OpaqueLocals /-! Standalone comparisons between indexed and streaming sequential metadata checks. -/ namespace Tests.StreamingValidationScaling @@ -26,8 +26,8 @@ def indexedConstrained (raw : RawMethod) (reachable : List Bool) (emitted : Meth raw.code.length = reachable.length ∧ raw.code.length = emitted.code.length ∧ (List.range raw.code.length).all (constrainedSiteCheck [] (FlowIndex.ofList []).get? (FlowIndex.ofList raw.code).get? (FlowIndex.ofList reachable).get? - (FlowIndex.ofList emitted.code).get? - (Std.TreeSet.ofList (constrainedBranchTargets raw.code)).contains) = true + (FlowIndex.ofList emitted.code).get? emitted.linkAt + (Std.TreeSet.ofList (branchTargets raw.code)).contains) = true /-- Eligible vector-local metadata. -/ def vector : TypeExpr := .apply diff --git a/Tests/StrictBinding.lean b/Tests/StrictBinding.lean new file mode 100644 index 000000000..47f46bf1a --- /dev/null +++ b/Tests/StrictBinding.lean @@ -0,0 +1,198 @@ +import StrictBinding +import Tests.StrictBinding.Container +import Tests.StrictBinding.Bodies +import Tests.StrictBinding.Rows +import Tests.StrictBinding.Slice +import Tests.StrictBinding.Metadata +import Tests.StrictBinding.TypeDefs +import Tests.StrictBinding.EnumEvidence +import Tests.StrictBinding.PlatformKinds +import Tests.StrictBinding.StaticData +import Tests.StrictBinding.LayoutContent +import Tests.StrictBinding.DispatchCatalogs +import Tests.StrictBinding.GenericImports + +/-! +# Strict binding tests: digests + +SHA-256 and SHA-1 as the kernel evaluates them must agree with the FIPS 180-4 vectors and with +reference digests computed by the .NET cryptography library, at every padding boundary: a message +that ends a block exactly (`t = 0`), one that leaves room for the length (`t < 56`), one that +does not (`t >= 56`), and one that spans several blocks. A digest or image that differs by one +bit must be rejected, because the audit trusts the digest of an image constant only through +`DigestIs`. +-/ + +namespace Tests.StrictBinding + +open _root_.StrictBinding + +/-- The message of `count` bytes whose byte at index `i` is `i % 251`. -/ +private def pattern (count : Nat) : List Nat := (List.range count).map (· % 251) + +/-! ## SHA-256 -/ + +theorem sha256_empty : DigestIs (ImageBytes.ofBytes []) + 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 := by decide +kernel +#print axioms sha256_empty + +theorem sha256_abc : DigestIs (ImageBytes.ofBytes (asciiOf "abc")) + 0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad := by decide +kernel +#print axioms sha256_abc + +/-- The 56-byte FIPS message, which leaves no room for the length in its block. -/ +theorem sha256_two_block_message : DigestIs + (ImageBytes.ofBytes (asciiOf "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")) + 0x248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1 := by decide +kernel +#print axioms sha256_two_block_message + +theorem sha256_pattern_3 : DigestIs (ImageBytes.ofBytes (pattern 3)) + 0xae4b3280e56e2faf83f414a6e3dabe9d5fbe18976544c05fed121accb85b53fc := by decide +kernel +#print axioms sha256_pattern_3 + +/-- 55 bytes: the length fits in the first block. -/ +theorem sha256_pattern_55 : DigestIs (ImageBytes.ofBytes (pattern 55)) + 0x463eb28e72f82e0a96c0a4cc53690c571281131f672aa229e0d45ae59b598b59 := by decide +kernel +#print axioms sha256_pattern_55 + +/-- 56 bytes: the length moves to a second block. -/ +theorem sha256_pattern_56 : DigestIs (ImageBytes.ofBytes (pattern 56)) + 0xda2ae4d6b36748f2a318f23e7ab1dfdf45acdc9d049bd80e59de82a60895f562 := by decide +kernel +#print axioms sha256_pattern_56 + +theorem sha256_pattern_63 : DigestIs (ImageBytes.ofBytes (pattern 63)) + 0x29af2686fd53374a36b0846694cc342177e428d1647515f078784d69cdb9e488 := by decide +kernel +#print axioms sha256_pattern_63 + +/-- 64 bytes: a whole block of data and a padding block of its own. -/ +theorem sha256_pattern_64 : DigestIs (ImageBytes.ofBytes (pattern 64)) + 0xfdeab9acf3710362bd2658cdc9a29e8f9c757fcf9811603a8c447cd1d9151108 := by decide +kernel +#print axioms sha256_pattern_64 + +theorem sha256_pattern_65 : DigestIs (ImageBytes.ofBytes (pattern 65)) + 0x4bfd2c8b6f1eec7a2afeb48b934ee4b2694182027e6d0fc075074f2fabb31781 := by decide +kernel +#print axioms sha256_pattern_65 + +/-- 120 bytes: the last block keeps 56 or more bytes, so the length needs a third block. -/ +theorem sha256_pattern_120 : DigestIs (ImageBytes.ofBytes (pattern 120)) + 0xf52b23db1fbb6ded89ef42a23ce0c8922c45f25c50b568a93bf1c075420bbb7c := by decide +kernel +#print axioms sha256_pattern_120 + +/-- 128 bytes: two whole blocks of data. -/ +theorem sha256_pattern_128 : DigestIs (ImageBytes.ofBytes (pattern 128)) + 0x471fb943aa23c511f6f72f8d1652d9c880cfa392ad80503120547703e56a2be5 := by decide +kernel +#print axioms sha256_pattern_128 + +/-- 4096 bytes fill one page exactly, so its padding block joins that page. -/ +theorem sha256_one_full_page : DigestIs (ImageBytes.ofBytes (pattern 4096)) + 0xd67c656e01756650d77717b0839985a056ec28ffe174601d690fc407a2ceffca := by decide +kernel +#print axioms sha256_one_full_page + +/-- One chunk more starts a second page. -/ +theorem sha256_second_page_of_one_chunk : DigestIs (ImageBytes.ofBytes (pattern 4097)) + 0xa16560d668b843fb3be99ace41dbd18471f342bd3255a1d21204b35e43f74436 := by decide +kernel +#print axioms sha256_second_page_of_one_chunk + +/-- The 64 bytes `0, 1, ..., 63` as one chunk. -/ +private def firstChunk : Nat := CIL.Crypto.bytesValue (List.range 64) + +/-- The byte 64 and its zero padding. -/ +private def secondChunk : Nat := 0x40 * 2 ^ 504 + +/-- Two pages of one chunk each, holding `pattern 65`. -/ +private def twoPages : ImageBytes := { length := 65, pages := [[firstChunk], [secondChunk]] } + +/-- The state after the first page, as a generated module states it. -/ +private def afterFirstPage : List Nat := Sha256.hashBlocks Sha256.initialState [firstChunk] + +/-- The state after the second page and its padding block. -/ +private def afterSecondPage : List Nat := + Sha256.hashBlocks afterFirstPage (CIL.Crypto.padded [secondChunk] 65) + +/-- A digest assembled from separate page results equals the one a single evaluation proves. -/ +theorem digest_from_page_results : DigestIs twoPages + 0x4bfd2c8b6f1eec7a2afeb48b934ee4b2694182027e6d0fc075074f2fabb31781 := + digestIs_of_hashes (final := afterSecondPage) + (blocks := [[firstChunk], CIL.Crypto.padded [secondChunk] 65]) (by decide +kernel) + (.cons (next := afterFirstPage) (by decide +kernel) + (.cons (by decide +kernel) (.nil _))) (by decide +kernel) (by decide +kernel) +#print axioms digest_from_page_results + +/-! ## Rejected digests and images -/ + +/-- A digest one above the true one is not the image's digest. -/ +theorem wrong_digest_is_rejected : ¬ DigestIs (ImageBytes.ofBytes (asciiOf "abc")) + (0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad + 1) := by + decide +kernel +#print axioms wrong_digest_is_rejected + +/-- One changed byte changes the digest, so the true digest no longer holds. -/ +theorem flipped_byte_is_rejected : ¬ DigestIs (ImageBytes.ofBytes (0x62 :: asciiOf "bc")) + 0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad := by decide +kernel +#print axioms flipped_byte_is_rejected + +/-- A chunk at or above `2 ^ 512` would hash as one value and read as another. -/ +private def oversizedChunk : ImageBytes := + { length := 3, pages := [[0x616263 * 2 ^ 488 + 2 ^ 512]] } + +theorem oversized_chunk_is_malformed : oversizedChunk.wellFormed = false := by decide +kernel +#print axioms oversized_chunk_is_malformed + +theorem oversized_chunk_has_no_digest : ¬ DigestIs oversizedChunk + 0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad := by decide +kernel +#print axioms oversized_chunk_has_no_digest + +/-- Nonzero padding after the last byte would be read as file content. -/ +theorem nonzero_padding_is_malformed : + ({ length := 3, pages := [[0x616263 * 2 ^ 488 + 1]] } : ImageBytes).wellFormed = false := by + decide +kernel +#print axioms nonzero_padding_is_malformed + +/-- A chunk count that does not match the length is malformed. -/ +theorem short_chunk_list_is_malformed : + ({ length := 65, pages := [[0x616263 * 2 ^ 488]] } : ImageBytes).wellFormed = false := by + decide +kernel +#print axioms short_chunk_list_is_malformed + +/-! ## SHA-1 and key tokens -/ + +theorem sha1_abc : CIL.Crypto.Sha1.ofBytes (asciiOf "abc") = + 0xa9993e364706816aba3e25717850c26c9cd0d89d := by + decide +kernel +#print axioms sha1_abc + +theorem sha1_empty : CIL.Crypto.Sha1.ofBytes [] = 0xda39a3ee5e6b4b0d3255bfef95601890afd80709 := by + decide +kernel +#print axioms sha1_empty + +theorem sha1_pattern_55 : CIL.Crypto.Sha1.ofBytes (pattern 55) = + 0x8ae2d46729cfe68ff927af5eec9c7d1b66d65ac2 := by decide +kernel +#print axioms sha1_pattern_55 + +theorem sha1_pattern_56 : CIL.Crypto.Sha1.ofBytes (pattern 56) = + 0x636e2ec698dac903498e648bd2f3af641d3c88cb := by decide +kernel +#print axioms sha1_pattern_56 + +theorem sha1_pattern_63 : CIL.Crypto.Sha1.ofBytes (pattern 63) = + 0x6d942da0c4392b123528f2905c713a3ce28364bd := by decide +kernel +#print axioms sha1_pattern_63 + +theorem sha1_pattern_65 : CIL.Crypto.Sha1.ofBytes (pattern 65) = + 0x69bd728ad6e13cd76ff19751fde427b00e395746 := by decide +kernel +#print axioms sha1_pattern_65 + +theorem sha1_pattern_64 : CIL.Crypto.Sha1.ofBytes (pattern 64) = + 0xc6138d514ffa2135bfce0ed0b8fac65669917ec7 := by decide +kernel +#print axioms sha1_pattern_64 + +theorem sha1_pattern_120 : CIL.Crypto.Sha1.ofBytes (pattern 120) = + 0xd3dbd653bd8597b7475321b60a36891278e6a04a := by decide +kernel +#print axioms sha1_pattern_120 + +/-- The token of a 160-byte key is the last eight digest bytes in reverse order. -/ +theorem key_token_of_a_160_byte_key : CIL.Crypto.Sha1.keyToken (pattern 160) = + 0x473c444ebb4661a5 := by + decide +kernel +#print axioms key_token_of_a_160_byte_key + +end Tests.StrictBinding diff --git a/Tests/StrictBinding/Bodies.lean b/Tests/StrictBinding/Bodies.lean new file mode 100644 index 000000000..e03c44be9 --- /dev/null +++ b/Tests/StrictBinding/Bodies.lean @@ -0,0 +1,238 @@ +import StrictBinding.Bind +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +/-! +# Strict binding tests: method bodies + +Every method body of the fixture, read by System.Reflection.Metadata, must be found by the audit +at the place its own MethodDef row gives. Each rejection then changes one fact of a file or a +claim that passes and asserts the exact error: a body claimed under another token, bytes that +differ from the file, a row that points elsewhere, an address no section holds, a method without +an IL body, a token outside the table, an identity the file does not spell, an image that is not +well formed and a section table that misdirects the container. With two images pinned, a claim +is checked in the one its identity names. The expected positions come from +`StrictFixture.expectedFacts`, which the reader generated, never from the audit's own decoding. +-/ + +namespace Tests.StrictBinding.Bodies + +open _root_.StrictBinding + +/-- The relative virtual address and file offset the reader reports for MethodDef row `row`. -/ +private def position (row : Nat) : Nat × Nat := + (StrictFixture.expectedFacts.filterMap fun fact => + match fact with + | .method found rva offset => if found == row then some (rva, offset) else none + | _ => none).headD (0, 0) + +/-- The file offset of MethodDef row `row`, from the reader's table position and row size. -/ +private def rowStart (row : Nat) : Nat := + (StrictFixture.expectedFacts.filterMap fun fact => + match fact with + | .table 0x06 _ size offset => some (offset + (row - 1) * size) + | _ => none).headD 0 + +/-- The MethodDef token of row `row`. -/ +private def tokenOfRow (row : Nat) : Nat := tokenOf 0x06 row + +/-- The bytes of the body of the method at index `index` of the fixture's bodies. -/ +private def bodyBytes (index : Nat) : List Nat := (StrictFixture.bodies.getD index default).2.2 + +/-- The fixture image pinned under its own identity. -/ +private def pinned : List PinnedImage := + [{ identity := StrictFixture.identity, image := StrictFixture.image }] + +/-- A claim about the fixture's assembly. -/ +private def claim (token : Nat) (bytes : List Nat) : BodyClaim := + { identity := StrictFixture.identity, token, bytes } + +/-- The pinned fixture with its bytes changed. -/ +private def pinnedWith (image : ImageBytes) : List PinnedImage := + [{ identity := StrictFixture.identity, image }] + +/-- Every method body of the fixture, claimed under its own token. -/ +private def claims : List BodyClaim := + StrictFixture.bodies.map fun (_, token, bytes) => claim token bytes + +theorem every_fixture_body_is_found_at_its_row : ClaimsHold bodyClaimBound pinned claims := by + decide +kernel +#print axioms every_fixture_body_is_found_at_its_row + +/-- The ten claims in four pages of three, composed as a generated module composes its pages. -/ +theorem every_fixture_body_is_found_in_pages : ClaimsHold bodyClaimBound pinned claims := + claimsHold_of_pages (size := 3) (count := 4) (by decide +kernel) (by decide +kernel) + (.succ (by decide +kernel) (.succ (by decide +kernel) (.succ (by decide +kernel) + (.succ (by decide +kernel) .zero)))) +#print axioms every_fixture_body_is_found_in_pages + +/-- The claims with one byte of the fifth body changed. -/ +private def tamperedClaims : List BodyClaim := + claims.mapIdx fun index claim => + if index == 4 then { claim with bytes := claim.bytes.set 1 0xFF } else claim + +/-- A changed body in the second page makes that page false, so the pages do not compose. -/ +theorem rejects_a_changed_body_in_the_second_page : + ¬ ClaimsHold bodyClaimBound pinned (claimPage tamperedClaims 3 1) := by decide +kernel +#print axioms rejects_a_changed_body_in_the_second_page + +/-- The fixture has a tiny and a fat header body, which the claims cover. -/ +theorem fixture_has_tiny_and_fat_bodies : + (bodyBytes 0).head? = some 0x12 ∧ (bodyBytes 1).head? = some 0x13 := by decide +kernel +#print axioms fixture_has_tiny_and_fat_bodies + +/-- Body bytes of another method do not belong to the token: the error names the row's own + address and the offset it maps to. -/ +theorem rejects_the_body_of_another_method : + failsWith (checkAll bodyClaimBound pinned [claim (tokenOfRow 1) (bodyBytes 1)]) + (.bodyMismatch (tokenOfRow 1) (position 1).1 (position 1).2) = true := by + decide +kernel +#print axioms rejects_the_body_of_another_method + +/-- One changed byte of a claimed body is not the file's body. -/ +theorem rejects_a_changed_byte_in_the_claim : + failsWith (checkAll bodyClaimBound pinned [claim (tokenOfRow 1) ((bodyBytes 0).set 4 0x2B)]) + (.bodyMismatch (tokenOfRow 1) (position 1).1 (position 1).2) = true := by + decide +kernel +#print axioms rejects_a_changed_byte_in_the_claim + +/-- A claim that is true of the unchanged file is false of a file with one body byte changed. -/ +theorem rejects_a_changed_byte_in_the_file : + failsWith (checkAll bodyClaimBound + (pinnedWith (flipByte StrictFixture.image ((position 1).2 + 4) 0x01)) + [claim (tokenOfRow 1) (bodyBytes 0)]) + (.bodyMismatch (tokenOfRow 1) (position 1).1 (position 1).2) = true := by + decide +kernel +#print axioms rejects_a_changed_byte_in_the_file + +/-- Where the file says a body is comes from its MethodDef row: when the row of the first method + gives the address of the second, the first method's own bytes are not there. -/ +theorem rejects_a_row_that_points_at_another_body : + failsWith (checkAll bodyClaimBound + (pinnedWith (flipByte StrictFixture.image (rowStart 1) 0x08)) + [claim (tokenOfRow 1) (bodyBytes 0)]) + (.bodyMismatch (tokenOfRow 1) (position 2).1 (position 2).2) = true := by + decide +kernel +#print axioms rejects_a_row_that_points_at_another_body + +/-- An address that no section holds is not mapped. -/ +theorem rejects_a_row_outside_every_section : + failsWith (checkAll bodyClaimBound + (pinnedWith (flipByte StrictFixture.image (rowStart 1 + 2) 0x01)) + [claim (tokenOfRow 1) (bodyBytes 0)]) + (.pe (.unmappedRva ((position 1).1 + 0x10000) (bodyBytes 0).length)) = true := by + decide +kernel +#print axioms rejects_a_row_outside_every_section + +/-- A zero address means the method has no body. -/ +theorem rejects_a_method_without_a_body : + failsWith (checkAll bodyClaimBound (pinnedWith (flipBytes StrictFixture.image + [(rowStart 1, (position 1).1 % 256), (rowStart 1 + 1, (position 1).1 / 256 % 256)])) + [claim (tokenOfRow 1) (bodyBytes 0)]) + (.noIlBody (tokenOfRow 1)) = true := by + decide +kernel +#print axioms rejects_a_method_without_a_body + +/-- A method whose implementation flags say native has no IL body. -/ +theorem rejects_a_native_method : + failsWith (checkAll bodyClaimBound + (pinnedWith (flipByte StrictFixture.image (rowStart 1 + 4) 0x01)) + [claim (tokenOfRow 1) (bodyBytes 0)]) + (.noIlBody (tokenOfRow 1)) = true := by + decide +kernel +#print axioms rejects_a_native_method + +/-- A body claimed under a TypeDef token is rejected. -/ +theorem rejects_a_token_that_is_not_a_method : + failsWith (checkAll bodyClaimBound pinned [claim (tokenOf 0x02 1) (bodyBytes 0)]) + (.notMethodDef (tokenOf 0x02 1)) = true := by + decide +kernel +#print axioms rejects_a_token_that_is_not_a_method + +/-- A MethodDef token past the last row is outside the table. -/ +theorem rejects_a_row_past_the_table : + failsWith (checkAll bodyClaimBound pinned [claim (tokenOfRow 11) (bodyBytes 0)]) + (.tokenOutOfScope (tokenOfRow 11)) = true := by + decide +kernel +#print axioms rejects_a_row_past_the_table + +/-- The identity of the fixture with another version. -/ +private def otherVersion : String := + "StrictFixture, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" + +/-- An image pinned under an identity its Assembly row does not spell is rejected. -/ +theorem rejects_an_identity_the_file_does_not_spell : + failsWith (checkAll bodyClaimBound + [{ identity := otherVersion, image := StrictFixture.image }] claims) + (.identityMismatch otherVersion) = true := by + decide +kernel +#print axioms rejects_an_identity_the_file_does_not_spell + +/-- A claim about an assembly that no image is pinned for is rejected. -/ +theorem rejects_a_claim_about_an_unpinned_assembly : + failsWith (checkAll bodyClaimBound pinned + [{ identity := "Other, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", + token := tokenOfRow 1, bytes := bodyBytes 0 }]) + (.unpinned "Other, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") = true := by + decide +kernel +#print axioms rejects_a_claim_about_an_unpinned_assembly + +/-- Two images with one identity would let a claim name either. -/ +theorem rejects_two_images_with_one_identity : + failsWith (checkAll bodyClaimBound + (pinned ++ pinned) claims) (.duplicateImage StrictFixture.identity) = + true := by + decide +kernel +#print axioms rejects_two_images_with_one_identity + +/-- The file offset of the Assembly row, from the reader's table position. -/ +private def assemblyRowStart : Nat := + (StrictFixture.expectedFacts.filterMap fun fact => + match fact with + | .table 0x20 _ _ offset => some offset + | _ => none).headD 0 + +/-- The fixture with its major version, which follows the four-byte hash algorithm of the + Assembly row, changed from 1 to 2, and one byte of its first body changed. -/ +private def otherImage : ImageBytes := + flipBytes StrictFixture.image [(assemblyRowStart + 4, 0x03), ((position 1).2 + 4, 0x01)] + +/-- The identity the changed fixture's Assembly row spells. -/ +private def otherIdentity : String := + "StrictFixture, Version=2.2.3.4, Culture=neutral, PublicKeyToken=null" + +/-- The fixture and its changed copy pinned under their own identities, in either order. -/ +private def bothPinned (otherFirst : Bool) : List PinnedImage := + let images := [{ identity := StrictFixture.identity, image := StrictFixture.image }, + { identity := otherIdentity, image := otherImage }] + if otherFirst then images.reverse else images + +/-- With two images pinned, each claim is checked in the image its identity names, whatever the + order of the pins: the first body holds in the fixture and is not in the changed copy. -/ +theorem finds_the_image_a_claim_names : ∀ otherFirst : Bool, + ClaimsHold bodyClaimBound (bothPinned otherFirst) claims ∧ + failsWith (checkAll bodyClaimBound (bothPinned otherFirst) + [{ identity := otherIdentity, token := tokenOfRow 1, bytes := bodyBytes 0 }]) + (.bodyMismatch (tokenOfRow 1) (position 1).1 (position 1).2) = true := by + decide +kernel +#print axioms finds_the_image_a_claim_names + +/-- A chunk at or above `2 ^ 512` is rejected before anything is decoded. -/ +theorem rejects_a_chunk_out_of_bounds : + failsWith (checkAll bodyClaimBound (pinnedWith { StrictFixture.image with + pages := [(StrictFixture.image.pages.headD []).set 0 + ((StrictFixture.image.pages.headD []).headD 0 + 2 ^ 512)] }) claims) + .malformedImage = true := by + decide +kernel +#print axioms rejects_a_chunk_out_of_bounds + +/-- The fixture's first section header is at `0x178`; its raw data pointer, `0x200`, moves to + `0x400`. The CLI header is no longer where the section table sends its address. -/ +theorem rejects_a_section_table_that_misdirects_the_container : + failsWith (checkAll bodyClaimBound + (pinnedWith (flipByte StrictFixture.image (0x178 + 21) 0x06)) claims) + (.pe .badCliHeader) = true := by + decide +kernel +#print axioms rejects_a_section_table_that_misdirects_the_container + +end Tests.StrictBinding.Bodies diff --git a/Tests/StrictBinding/Container.lean b/Tests/StrictBinding/Container.lean new file mode 100644 index 000000000..e537ba8a0 --- /dev/null +++ b/Tests/StrictBinding/Container.lean @@ -0,0 +1,323 @@ +import StrictBinding.Dump +import StrictBinding.PublicKeys +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +/-! +# Strict binding tests: container and tables + +The decoding of the fixture's PE container, `#~` header and table positions must equal what +System.Reflection.Metadata reads from the same bytes (`StrictFixture.expectedFacts`, generated by +`Tests/StrictBindingFixture.cs`). Each rejection then changes one byte or one section of a file +that decodes and asserts the exact error: a decoder that skipped a check would accept the changed +file or report another error. +-/ + +namespace Tests.StrictBinding.Container + +open _root_.StrictBinding + +/-- The fixture decodes to exactly the facts an independent reader finds in its bytes. -/ +theorem decodes_as_system_reflection_metadata_reads_it : + ((facts StrictFixture.image).toOption == some StrictFixture.expectedFacts) = true := by + decide +kernel +#print axioms decodes_as_system_reflection_metadata_reads_it + +/-- The error decoding the container and tables of an image gives, when it fails. -/ +private def decodeError (image : ImageBytes) : Option AuditError := + match ImageView.ofImage image >>= decodeMetadata with + | .error error => some error + | .ok _ => none + +/-- The unchanged fixture has a container; the offsets below describe it. -/ +theorem fixture_container_decodes : decodeError StrictFixture.image = none := by decide +kernel +#print axioms fixture_container_decodes + +theorem rejects_a_missing_dos_signature : + decodeError (flipByte StrictFixture.image 0 0xFF) = some (.pe .notDos) := by decide +kernel +#print axioms rejects_a_missing_dos_signature + +/-- The PE header of the SDK-built fixture starts at `0x80`. -/ +theorem rejects_a_missing_pe_signature : + decodeError (flipByte StrictFixture.image 0x80 0xFF) = some (.pe (.badSignature 0x80)) := by + decide +kernel +#print axioms rejects_a_missing_pe_signature + +/-- The optional header magic `0x10B` becomes `0x1F4`. -/ +theorem rejects_an_unknown_optional_header_magic : + decodeError (flipByte StrictFixture.image (0x80 + 24) 0xFF) = + some (.pe (.badMagic 0x1F4)) := by + decide +kernel +#print axioms rejects_an_unknown_optional_header_magic + +/-- The fixture's metadata root is at offset 684. -/ +theorem rejects_a_missing_metadata_signature : + decodeError (flipByte StrictFixture.image 684 0xFF) = some (.pe .badMetadataRoot) := by + decide +kernel +#print axioms rejects_a_missing_metadata_signature + +/-- The fixture's `#~` stream is at offset 792; its heap size flags are the seventh byte. -/ +theorem rejects_unknown_heap_size_flags : + decodeError (flipByte StrictFixture.image (792 + 6) 0x08) = + some (.tables (.unsupportedHeapSizes 8)) := by + decide +kernel +#print axioms rejects_unknown_heap_size_flags + +/-- A `Valid` bit above table `0x2C` has no schema, so row sizes cannot be computed. -/ +theorem rejects_a_table_above_the_schema : + (match decodeError (flipByte StrictFixture.image (792 + 8 + 5) 0x20) with + | some (.tables (.unsupportedTables _)) => true + | _ => false) = true := by + decide +kernel +#print axioms rejects_a_table_above_the_schema + +/-- A pointer table (FieldPtr, bit 3) has other tables index it, so it is refused by name. -/ +theorem rejects_a_pointer_table : + (match decodeError (flipByte StrictFixture.image (792 + 8) 0x08) with + | some (.tables (.pointerTables _)) => true + | _ => false) = true := by + decide +kernel +#print axioms rejects_a_pointer_table + +/-- A Module row count of 65 puts the tables past the end of the stream. -/ +theorem rejects_row_counts_past_the_stream : + decodeError (flipByte StrictFixture.image (792 + 24) 0x40) = + some (.tables .badTableStream) := by + decide +kernel +#print axioms rejects_row_counts_past_the_stream + +/-! ## Metadata version length -/ + +/-- The stream headers from `header` with `extra` added to each stream offset; a header is eight + bytes and a name padded to four. -/ +private def shiftStreams (extra : Nat) : Nat → Nat → List Nat → List Nat + | 0, _, bytes => bytes + | count + 1, header, bytes => + let nameLength := ((bytes.drop (header + 8)).takeWhile (· != 0)).length + let shifted := writeLittle bytes header 4 (readLittle bytes header 4 + extra) + shiftStreams extra count (header + 8 + (nameLength + 4) / 4 * 4) shifted + +/-- The file offset of the fixture's metadata root. -/ +private def fixtureRoot : Nat := 684 + +/-- The bytes of the fixture's metadata, from its root. -/ +private def fixtureMetadataSize : Nat := 0x62C + +/-- The bytes the fixture's metadata root allocates to its version string. -/ +private def fixtureVersionLength : Nat := 12 + +/-- The file offset of the metadata size field in the fixture's CLI header. -/ +private def cliMetadataSize : Nat := 0x214 + +/-- The file offset of the mapped size of the fixture's text section. -/ +private def textVirtualSize : Nat := 0x180 + +/-- The fixture with `extra` more bytes allocated to its version string. The rest of the root and + its streams move up into the unused end of the text section, whose mapped size grows to cover + them, and the stream offsets and the metadata size follow. -/ +private def widenedVersion (extra : Nat) : ImageBytes := + let bytes := imageBytes StrictFixture.image + let versionEnd := fixtureRoot + 16 + fixtureVersionLength + let metadataEnd := fixtureRoot + fixtureMetadataSize + let moved := bytes.take versionEnd ++ List.replicate extra 0 ++ + (bytes.drop versionEnd).take (metadataEnd - versionEnd) ++ bytes.drop (metadataEnd + extra) + let length := fixtureVersionLength + extra + let rooted := writeLittle moved (fixtureRoot + 12) 4 length + let streams := readLittle rooted (fixtureRoot + 18 + length) 2 + let shifted := shiftStreams extra streams (fixtureRoot + 20 + length) rooted + let sized := writeLittle shifted cliMetadataSize 4 (fixtureMetadataSize + extra) + ImageBytes.ofBytes (writeLittle sized textVirtualSize 4 0x800) + +/-- A version field of 256 bytes, the most II.24.2.1 allows (a 255-byte string rounded up to a + multiple of four), is read, and the `#~` stream is found 244 bytes further on. -/ +theorem accepts_a_version_length_of_256 : + ((ImageView.ofImage (widenedVersion 244) >>= decodeMetadata).toOption.map + (·.pe.tables.start)) = some (792 + 244) := by + decide +kernel +#print axioms accepts_a_version_length_of_256 + +/-- A version field of 260 bytes is longer than any version string the format allows. -/ +theorem rejects_a_version_length_of_260 : + decodeError (widenedVersion 248) = some (.pe .badMetadataRoot) := by + decide +kernel +#print axioms rejects_a_version_length_of_260 + +/-! ## Tokens -/ + +/-- The layout of the unchanged fixture. -/ +private def fixtureLayout : Except AuditError (ImageView × MetadataLayout) := do + let view ← ImageView.ofImage StrictFixture.image + pure (view, ← decodeMetadata view) + +/-- The offset of the first MethodDef row, then the row size, from the fixture's facts: the table + starts at 1116 and its rows are 14 bytes. -/ +theorem method_rows_are_positioned_by_token : + (fixtureLayout.bind fun (_, layout) => layout.rowAt (tokenOf 0x06 1)).toOption = + some (1116, 14) ∧ + (fixtureLayout.bind fun (_, layout) => layout.rowAt (tokenOf 0x06 10)).toOption = + some (1116 + 9 * 14, 14) := by + decide +kernel +#print axioms method_rows_are_positioned_by_token + +/-- Row zero and the row after the last are outside the table. -/ +theorem rejects_tokens_outside_their_table : + failsWith (fixtureLayout.bind fun (_, layout) => layout.rowAt (tokenOf 0x06 0)) + (.tokenOutOfScope (tokenOf 0x06 0)) = true ∧ + failsWith (fixtureLayout.bind fun (_, layout) => layout.rowAt (tokenOf 0x06 11)) + (.tokenOutOfScope (tokenOf 0x06 11)) = true := by + decide +kernel +#print axioms rejects_tokens_outside_their_table + +/-- A table the file does not have contains no token. -/ +theorem rejects_tokens_of_an_absent_table : + failsWith (fixtureLayout.bind fun (_, layout) => layout.rowAt (tokenOf 0x2A 1)) + (.tokenOutOfScope (tokenOf 0x2A 1)) = true := by + decide +kernel +#print axioms rejects_tokens_of_an_absent_table + +/-- A column past the schema of its table does not exist. -/ +theorem rejects_a_column_past_the_schema : + failsWith (fixtureLayout.bind fun (_, layout) => layout.cellAt (tokenOf 0x06 1) 6) + (.tables (.noSuchColumn 0x06 6)) = true := by + decide +kernel +#print axioms rejects_a_column_past_the_schema + +/-! ## Relative virtual addresses -/ + +/-- A text section of `0x1000` bytes at `0x2000`, from file offset `0x200`. -/ +private def text : Section := + { virtualSize := 0x1000, virtualAddress := 0x2000, rawSize := 0x1000, rawPointer := 0x200 } + +/-- A section that follows it. -/ +private def data : Section := + { virtualSize := 0x400, virtualAddress := 0x3000, rawSize := 0x200, rawPointer := 0x1200 } + +theorem maps_an_address_into_its_section : + (mapRva [text, data] 0x2010 4).toOption = some 0x210 ∧ + (mapRva [text, data] 0x3004 4).toOption = some 0x1204 := by + decide +kernel +#print axioms maps_an_address_into_its_section + +/-- No section holds an address before the first one or after the last. -/ +theorem rejects_an_unmapped_address : + failsWith (mapRva [text, data] 0x1FFF 1) (.pe (.unmappedRva 0x1FFF 1)) = true ∧ + failsWith (mapRva [text, data] 0x3400 1) (.pe (.unmappedRva 0x3400 1)) = true := by + decide +kernel +#print axioms rejects_an_unmapped_address + +/-- Bytes that leave the section's raw data are unmapped, even inside its mapped size. -/ +theorem rejects_bytes_past_the_raw_data : + failsWith (mapRva [text, data] 0x31FC 8) (.pe (.unmappedRva 0x31FC 8)) = true ∧ + (mapRva [text, data] 0x31FC 4).toOption = some 0x13FC := by + decide +kernel +#print axioms rejects_bytes_past_the_raw_data + +/-- Bytes that leave the mapped size are unmapped, even inside the raw data. -/ +theorem rejects_bytes_past_the_mapped_size : + failsWith (mapRva [{ text with virtualSize := 0x100 }] 0x20FC 8) + (.pe (.unmappedRva 0x20FC 8)) = true := by + decide +kernel +#print axioms rejects_bytes_past_the_mapped_size + +/-- An address inside two sections has no single file position. -/ +theorem rejects_an_address_in_two_sections : + failsWith (mapRva [text, { data with virtualAddress := 0x2800 }] 0x2900 1) + (.pe (.ambiguousRva 0x2900)) = true := by + decide +kernel +#print axioms rejects_an_address_in_two_sections + +/-! ## Heap entries and identity -/ + +/-- The fixture row and blob heap with the selected Assembly flags and key bytes. -/ +private def fixtureWithKey (flags : Nat) (key : List Nat) : + Except AuditError (ImageView × MetadataLayout) := do + let (view, layout) ← fixtureLayout + let (flagsOffset, _) ← layout.cellAt (tokenOf 0x20 1) 5 + let (keyOffset, keyWidth) ← layout.cellAt (tokenOf 0x20 1) 6 + let bytes ← view.bytes 0 StrictFixture.image.length + let bytes := (List.range 4).foldl + (fun bytes index => bytes.set (flagsOffset + index) (flags / 256 ^ index % 256)) bytes + let bytes := (List.range keyWidth).foldl + (fun bytes index => bytes.set (keyOffset + index) 0) bytes + let blob := key.length :: key + let changed ← ImageView.ofImage (ImageBytes.ofBytes (bytes ++ blob)) + let pe := { layout.pe with blobs := { start := bytes.length, size := blob.length } } + return (changed, { layout with pe := pe }) + +/-- The assembly identity spelled by the fixture's key bytes and public-key flag. -/ +private def identityWithKey (flags : Nat) (key : List Nat) : Except AuditError (List Nat) := do + let (view, layout) ← fixtureWithKey flags key + assemblyIdentity view layout + +/-- The publisher token that the definition's key establishes for callers. -/ +private def definitionWithKey (flags : Nat) (key : List Nat) : Except AuditError (Option Nat) := do + let (view, layout) ← fixtureWithKey flags key + let image ← prepareOne ⟨StrictFixture.identity, StrictFixture.image⟩ + definitionKeyToken { image with view, layout } + +/-- The definition hashes its complete key. An unsigned assembly has no publisher token. -/ +theorem definition_key_and_unsigned : + (definitionWithKey 1 (asciiOf "abc")).toOption = some (some 0x9dd8d09c6cc25078) ∧ + (definitionWithKey 0 []).toOption = some none := by decide +kernel +#print axioms definition_key_and_unsigned + +/-- Eight key bytes in a definition are hashed even when a reference would read them as a token. -/ +theorem definition_hashes_key_bytes : + let key := [0x9d, 0xd8, 0xd0, 0x9c, 0x6c, 0xc2, 0x50, 0x78] + (definitionWithKey 0 key).toOption = some (some (CIL.Crypto.Sha1.keyToken key)) ∧ + (identityWithKey 0 key).toOption = (identityWithKey 1 key).toOption ∧ + (definitionWithKey 1 []).toOption = some none := by decide +kernel +#print axioms definition_hashes_key_bytes + +/-- Full and unsigned definition names come from the Assembly row regardless of the flags. -/ +theorem assembly_definition_keys : + (identityWithKey 0x8100 (asciiOf "abc")).toOption = some (asciiOf + "StrictFixture, Version=1.2.3.4, Culture=neutral, PublicKeyToken=9dd8d09c6cc25078") ∧ + (identityWithKey 1 []).toOption = some (asciiOf + "StrictFixture, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null") := by decide +kernel +#print axioms assembly_definition_keys + +/-- AssemblyRef accepts an absent key, a full key or a token including an all-zero token. -/ +theorem reference_key_encodings : + (assemblyKeyToken 0 []).toOption = some none ∧ + (assemblyKeyToken 0x8101 (asciiOf "abc")).toOption = some (some 0x9dd8d09c6cc25078) ∧ + (assemblyKeyToken 0x8100 [0, 0xab, 0, 0, 0, 0, 0, 0xcd]).toOption = + some (some 0x00ab0000000000cd) ∧ + (assemblyKeyToken 0 (List.replicate 8 0)).toOption = some (some 0) := by decide +kernel +#print axioms reference_key_encodings + +/-- Nonempty unflagged AssemblyRef blobs must have exactly eight bytes. -/ +theorem reference_rejects_invalid_token_widths : + failsWith (assemblyKeyToken 0 [1]) (.tables (.invalidPublicKeyToken 1)) = true ∧ + failsWith (assemblyKeyToken 0 (List.replicate 7 0)) + (.tables (.invalidPublicKeyToken 7)) = true ∧ + failsWith (assemblyKeyToken 0 (List.replicate 9 0)) + (.tables (.invalidPublicKeyToken 9)) = true := by decide +kernel +#print axioms reference_rejects_invalid_token_widths + +theorem spells_version_components : + decimal 0 = asciiOf "0" ∧ decimal 7 = asciiOf "7" ∧ decimal 10 = asciiOf "10" ∧ + decimal 65535 = asciiOf "65535" := by + decide +kernel +#print axioms spells_version_components + +theorem spells_a_key_token_with_leading_zeros : + hexDigits 16 0x00ab0000000000cd = asciiOf "00ab0000000000cd" := by decide +kernel +#print axioms spells_a_key_token_with_leading_zeros + +/-- UTF-8 of one to four bytes decodes to code points. -/ +theorem decodes_utf8 : + utf8CodePoints 8 [0x61, 0xC3, 0xA9, 0xE2, 0x82, 0xAC, 0xF0, 0x9F, 0x98, 0x80] = + some [0x61, 0xE9, 0x20AC, 0x1F600] := by + decide +kernel +#print axioms decodes_utf8 + +/-- An overlong form, a lone continuation byte, a surrogate and a truncated sequence are not + UTF-8. -/ +theorem rejects_malformed_utf8 : + utf8CodePoints 4 [0xC0, 0x80] = none ∧ utf8CodePoints 4 [0x80] = none ∧ + utf8CodePoints 4 [0xED, 0xA0, 0x80] = none ∧ utf8CodePoints 4 [0xE2, 0x82] = none := by + decide +kernel +#print axioms rejects_malformed_utf8 + +end Tests.StrictBinding.Container diff --git a/Tests/StrictBinding/DispatchCatalogs.lean b/Tests/StrictBinding/DispatchCatalogs.lean new file mode 100644 index 000000000..a7f4ce7ba --- /dev/null +++ b/Tests/StrictBinding/DispatchCatalogs.lean @@ -0,0 +1,43 @@ +import StrictBinding.DispatchCatalogs +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +/-! The class and method declarations of the physical virtual slot StrictFixture.Shape.Sides. -/ + +namespace Tests.StrictBinding.DispatchCatalogs +open CIL.Extraction _root_.StrictBinding + +/-- The root class and the complete method list of every descendant, in physical row order. -/ +private def declarations (bytes := StrictFixture.image) : + Option (List (TypeExpr × Bool × List (Nat × Bool))) := do + let image ← (prepareOne ⟨StrictFixture.identity, bytes⟩).toOption + let owner ← (methodDeclaringType image 0x06000004).toOption + let some types ← (ClassDispatch.physicalHierarchy image (owner % 2^24)).run.toOption | none + return types.map fun type => + (type.identity, type.abstractType, type.methods.map fun m => (m.token, m.isVirtual)) + +/-- A pinned class name from the fixture. -/ +private def named (name : String) := + TypeExpr.object StrictFixture.identity ("StrictFixture." ++ name) + +/-- The root declares Sides, Count and its constructor. Both derived classes override Sides. -/ +theorem completeDeclarations : + declarations = some [ + (named "Shape", false, [(0x06000004,true), (0x06000005,false), (0x06000006,false)]), + (named "Triangle", false, [(0x06000007,true), (0x06000008,false)]), + (named "Square", false, [(0x06000009,true), (0x0600000a,false)])] := by decide +kernel +#print axioms completeDeclarations + +/-- Mutate one physical metadata cell independently of the claimed declarations. -/ +private def change (token column mask : Nat) : Option Bool := do + let image ← (prepareOne ⟨StrictFixture.identity, StrictFixture.image⟩).toOption + let (offset, _) ← (image.layout.cellAt token column).toOption + let altered ← declarations (Tests.StrictBinding.flipByte StrictFixture.image offset mask) + return some altered != declarations + +/-- Changing the parent, abstract flag or virtual flag changes the reconstructed catalog. -/ +theorem changedDeclarations : + change 0x02000006 3 4 = some true ∧ change 0x02000007 0 128 = some true ∧ + change 0x06000007 2 64 = some true := by decide +kernel +#print axioms changedDeclarations +end Tests.StrictBinding.DispatchCatalogs diff --git a/Tests/StrictBinding/DispatchOverrideCases.lean b/Tests/StrictBinding/DispatchOverrideCases.lean new file mode 100644 index 000000000..ea647a277 --- /dev/null +++ b/Tests/StrictBinding/DispatchOverrideCases.lean @@ -0,0 +1,67 @@ +import Extracted +import DispatchImage +import StrictBinding.DispatchCatalogs +import Tests.StrictBinding.Mutation + +/-! Physical MethodImpl controls for two explicit implementations with identical signatures. -/ + +namespace DispatchOverrideCases +open StrictBinding CIL.Extraction + +private def images (image : ImageBytes) : List PinnedImage := [⟨Extracted.assembly0.identity, image⟩] + +private def audit (image : ImageBytes) (items := Extracted.methodEvidence) := + ClassDispatch.check (images image) Extracted.assemblies items Extracted.imports + +/-- Both explicit slots match the implementation catalog read from the assembly. -/ +theorem original : DispatchCatalogsChecked (images DispatchImage.image) Extracted.assemblies + Extracted.methodEvidence Extracted.imports true := by decide +kernel +#print axioms original + +/-- The unrelated IDisposable implementation names an external declaration by MemberRef. -/ +theorem unrelatedMemberRef : + (do + let image ← (prepareOne ⟨Extracted.assembly0.identity, DispatchImage.image⟩).toOption + let declaration ← (image.layout.cellOfRow image.view 0x19 3 2).toOption + return (image.layout.rowCount 0x19, declaration % 2)) = some (3, 1) := by decide +kernel +#print axioms unrelatedMemberRef + +private def erase (item : MethodEvidence) : MethodEvidence := { item with + dispatchTypes := item.dispatchTypes.map fun type => { type with + methods := type.methods.map fun method => { method with overrides := [] } } } + +private def mismatch (result : Except AuditError Bool) (expected : Nat) : Bool := + match result with + | .error (.dispatchMismatch token) => token == expected + | _ => false + +/-- Omitting the explicit slots from the carried declarations changes the entry catalog. -/ +theorem missingOverrides : + mismatch (audit DispatchImage.image (Extracted.methodEvidence.map erase)) + Extracted.method0Original.token = true := by decide +kernel +#print axioms missingOverrides + +private def crossed (owner : Bool) : Option Bool := do + let image := DispatchImage.image + let prepared ← (prepareOne ⟨Extracted.assembly0.identity, image⟩).toOption + let cell := prepared.layout.cellOfRow prepared.view 0x19 + let body ← (cell 1 1).toOption + let first ← (cell 1 2).toOption + let second ← (cell 2 2).toOption + guard (first != second && first < 256 && second < 256) + let column := if owner then 0 else 2 + let (offset, _) ← (prepared.layout.cellAt 0x19000001 column).toOption + let altered := Tests.StrictBinding.flipByte image offset (if owner then 1 else first ^^^ second) + let expected := if owner then StrictBinding.tokenOf Table.methodDef (body / 2) + else Extracted.method0Original.token + return mismatch (audit altered) expected + +/-- Moving the row to another class contradicts the body MethodDef's physical owner. -/ +theorem crossedOwner : crossed true = some true := by decide +kernel +#print axioms crossedOwner + +/-- Selecting the other declaration changes the catalog even though its signature agrees. -/ +theorem crossedDeclaration : crossed false = some true := by decide +kernel +#print axioms crossedDeclaration + +end DispatchOverrideCases diff --git a/Tests/StrictBinding/DispatchTotal.lean.in b/Tests/StrictBinding/DispatchTotal.lean.in new file mode 100644 index 000000000..19b7f8f4f --- /dev/null +++ b/Tests/StrictBinding/DispatchTotal.lean.in @@ -0,0 +1,47 @@ +import Extracted +import CIL.Safety.MethodContract + +/-! A caller enters both unrelated virtual slots and reuses the inherited shape slot. -/ + +namespace DispatchGate +open CIL CIL.Extraction CIL.Safety + +/-- The classes of the pinned assembly used as receiver types. -/ +private def named (name : String) := + TypeExpr.object Extracted.assembly0.identity ("Geometry." ++ name) + +/-- The caller owns a triangle (possibly an equilateral one) and a distinct cube. -/ +private def caller (equilateral : Bool) : Safety.Memory := { Safety.Memory.empty with + allocations := fun id => + if id == 0 then + let shape := if equilateral then named "Equilateral" else named "Triangle" + let bases := if equilateral then [named "Triangle", named "Shape"] else [named "Shape"] + some ⟨.object ⟨shape, bases⟩, ⟨8,8,[]⟩, true, []⟩ + else if id == 1 then some ⟨.object ⟨named "Cube", [named "Solid"]⟩, ⟨8,8,[]⟩, true, []⟩ + else none + nextIdentity := 2 } + +/-- The two shape calls count three sides each and the solid call counts six faces. -/ +def contract : MethodContract Bool W32 where + args _ := [.object (.address ⟨0,0⟩), .object (.address ⟨1,0⟩)] + results value := [.scalar (.i32 value)] + pre equilateral memory := memory = caller equilateral + footprint _ := [] + post _ initial final value := final = initial ∧ value = 12 + +/-- Both receiver types preserve caller memory across all three virtual calls. -/ +theorem holds (ran : ∀ shape, invoke Extracted.program 50 0 (contract.args shape) (caller shape) = + .ok (caller shape, contract.results 12)) : contract.Holds Extracted.program 0 := by + rintro shape _ rfl + exact MethodContract.Holds.intro (c := contract) _ _ ⟨50, ran shape⟩ + (Confined.refl [] _) ⟨rfl, rfl⟩ +#print axioms holds + +/-- Returning five faces for the cube produces an ordinary return of eleven. -/ +theorem refuted (ran : invoke Extracted.program 50 0 (contract.args true) (caller true) = + .ok (caller true, [.scalar (.i32 11)])) : contract.Refuted Extracted.program 0 := by + apply MethodContract.Refuted.of_return (c := contract) (input := true) rfl ran + rintro ⟨_, value, encoded, _, rfl⟩ + revert encoded + decide +kernel +#print axioms refuted diff --git a/Tests/StrictBinding/EnumEvidence.lean b/Tests/StrictBinding/EnumEvidence.lean new file mode 100644 index 000000000..aa48196c9 --- /dev/null +++ b/Tests/StrictBinding/EnumEvidence.lean @@ -0,0 +1,45 @@ +import StrictBinding.EnumEvidence +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +namespace Tests.StrictBinding.EnumEvidence + +open CIL CIL.Extraction _root_.StrictBinding + +private def small (code : Nat) : PlatformDefinition := + { scope := bytesOf StrictFixture.identity + parts := [asciiOf "StrictFixture", asciiOf "Small"] + kind := .enumType code } + +private def absent : PlatformDefinition := + { small 5 with scope := bytesOf "Absent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } + +private def check (claims : List PlatformDefinition) : Except AuditError Bool := do + let image ← prepareOne ⟨StrictFixture.identity, StrictFixture.image⟩ + coveredEnumEntries [image] claims + +/-- The compiled enum uses UInt8; both the missing assembly and a broken supplied definition + retain their own outcomes, regardless of the order of the input list. -/ +theorem compiled_storage_and_missing_images : + (check [small 5]).toOption = some true ∧ (check [absent, small 5]).toOption = some false ∧ + failsWith (check [absent, small 9]) (.typeDefMismatch 2 .fields) = true ∧ + failsWith (check [small 9, absent]) (.typeDefMismatch 2 .fields) = true := by decide +kernel +#print axioms compiled_storage_and_missing_images + +/-- A matching simple name in a different namespace, or at the wrong nesting level, cannot + supply the row for an enum entry. -/ +theorem rejects_wrong_scope_parts : + failsWith (check [{ small 5 with parts := [[], asciiOf "Small"] }]) (.typeDefMismatch 0 .typeName) = true ∧ + failsWith (check [{ small 5 with parts := [asciiOf "StrictFixture", asciiOf "Small", [], asciiOf "Small"] }]) + (.typeDefMismatch 2 .typeName) = true := by decide +kernel +#print axioms rejects_wrong_scope_parts + +/-- Select the definition using precisely the table lookup used for external signature types. -/ +private def table : PlatformTable := platform_table% [small 5] + +theorem compiled_table_storage : + table.kindOf (small 5).scope (small 5).parts = some (.enumType 5) ∧ + (check table.definitions).toOption = some true := by decide +kernel +#print axioms compiled_table_storage + +end Tests.StrictBinding.EnumEvidence diff --git a/Tests/StrictBinding/Fixture.lean b/Tests/StrictBinding/Fixture.lean new file mode 100644 index 000000000..342b947ae --- /dev/null +++ b/Tests/StrictBinding/Fixture.lean @@ -0,0 +1,360 @@ +import StrictBinding.Dump +import StrictBinding.Sha256 +import CIL.Extraction.MetadataBytes + +/-! +# Strict binding fixture + +An SDK-built library with a tiny-header method, a fat-header method with locals, a byte +enum, an explicit-layout struct with static data and a class with a virtual method and two +derived classes. The bytes, the facts System.Reflection.Metadata reads from them, the +method bodies, the assembly block and a slice an extraction carries are generated by +`Tests/StrictBindingFixture.cs`; do not edit them. +-/ + +namespace StrictFixture + +/-- A 64-byte chunk written as its high and low 32 bytes, so that no row is longer than a line. -/ +def joinHalves (high low : Nat) : Nat := high * 2 ^ 256 + low + +def page0 : List Nat := [ + joinHalves 0x4d5a90000300000004000000ffff0000b8000000000000004000000000000000 + 0x0000000000000000000000000000000000000000000000000000000080000000, + joinHalves 0x0e1fba0e00b409cd21b8014ccd21546869732070726f6772616d2063616e6e6f + 0x742062652072756e20696e20444f53206d6f64652e0d0d0a2400000000000000, + joinHalves 0x504500004c010300fda239bd0000000000000000e00022200b01300000080000 + 0x0006000000000000462700000020000000400000000000100020000000020000, + joinHalves 0x0400000000000000040000000000000000800000000200000000000003006085 + 0x0000100000100000000010000010000000000000100000000000000000000000, + joinHalves 0xf42600004f00000000400000b802000000000000000000000000000000000000 + 0x006000000c000000d82600001c00000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000020000008000000 + 0x0000000000000000082000004800000000000000000000002e74657874000000, + joinHalves 0x5807000000200000000800000002000000000000000000000000000020000060 + 0x2e72737263000000b80200000040000000040000000a00000000000000000000, + joinHalves 0x00000000400000402e72656c6f6300000c0000000060000000020000000e0000 + 0x0000000000000000000000004000004200000000000000000000000000000000, + joinHalves 0x28270000000000004800000002000500ac2000002c0600000100000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x00000000000000000000000000000000120217582a0000001330020010000000 + 0x0100001102195a1d590a061f6431031f640a062a327f060000041e730700000a, + joinHalves 0x2a0a162a1e026f040000062a1e02280800000a2a0a192a1e0228060000062a0a + 0x1a2a1e0228060000062a000042534a4201000100000000000c00000076342e30, + joinHalves 0x2e33303331390000000005006c0000009c020000237e00000803000030020000 + 0x23537472696e6773000000003805000004000000235553003c05000010000000, + joinHalves 0x23475549440000004c050000e000000023426c6f620000000000000002000001 + 0x579da3290900000000fa013300160000010000000b0000000800000006000000, + joinHalves 0x0a00000003000000080000000200000006000000010000000200000001000000 + 0x010000000100000001000000010000000100000001000000010000000000a001, + joinHalves 0x01000000000006003d01d70106005d01d7010600f800c4010f00fd0100000600 + 0x0c017b0106002501d7010600b901b2010600be00b20106001002b20106000100, + joinHalves 0xb2010600dd00d7010000000051000000000001000100010100009a01cf001d00 + 0x01000100110110002302cf00210004000100810110008500cf00250006000100, + joinHalves 0x01001000b200cf00250006000400010010009a00cf0014000600070001001000 + 0xc800cf00140006000900000100005a000000250006000b0006067d0033005680, + joinHalves 0x7900360056807b00360006001d023a00060095013a00330110003d0050200000 + 0x000096002b024000010058200000000096000c02400002007420000000009608, + joinHalves 0x900045000300812000000000c601f7014d000300842000000000960017025100 + 0x03008c20000000008618be0106000400942000000000c600f7014d0004009720, + joinHalves 0x000000008618be01060004009f2000000000c600f7014d000400a22000000000 + 0x8618be010600040000000100210200000100210200000100b8000900be010100, + joinHalves 0x1100be0106001900be010a002900be0110003100be0101005900be0106000c00 + 0xbe011f004900be010600050008002f0005000c00310027002b00d5002e000b00, + joinHalves 0x5f002e00130068002e001b0087002e002300900003013300d000000008000000 + 0x0300000000000400040000000500150004000100000094005700020003000300, + joinHalves 0x1900502700000600048000000100020003000400000000000000cf0000000b00 + 0x000000000000000000002600a30000000000000000526561644f6e6c79537061, + joinHalves 0x6e60310036363834304444413135344538413131334333314444304144333246 + 0x3746334133363641383045383133363937394438463541313031443344323944, + joinHalves 0x36463732003c4d6f64756c653e003c50726976617465496d706c656d656e7461 + 0x74696f6e44657461696c733e004100420076616c75655f5f0041726974686d65, + joinHalves 0x746963006765745f5461626c6500547269616e676c650053797374656d2e5275 + 0x6e74696d650053686170650073686170650056616c7565547970650053717561, + joinHalves 0x7265005374726963744669787475726500436f6d70696c657247656e65726174 + 0x65644174747269627574650044656275676761626c6541747472696275746500, + joinHalves 0x5461726765744672616d65776f726b4174747269627574650052656653616665 + 0x747952756c657341747472696275746500436f6d70696c6174696f6e52656c61, + joinHalves 0x786174696f6e734174747269627574650052756e74696d65436f6d7061746962 + 0x696c6974794174747269627574650053797374656d2e52756e74696d652e5665, + joinHalves 0x7273696f6e696e67004869676800536d616c6c00537472696374466978747572 + 0x652e646c6c0053797374656d00456e756d002e63746f720053797374656d2e44, + joinHalves 0x6961676e6f73746963730053797374656d2e52756e74696d652e436f6d70696c + 0x6572536572766963657300536964657300446562756767696e674d6f64657300, + joinHalves 0x466174004f626a65637400436f756e74004c6f770078004f7665726c61790054 + 0x696e7900000000005a46cccc7bfda843bac53984ce1265e10004200101080320, + joinHalves 0x0001052001011111042001010e03070108051511290105062002010f010808b0 + 0x3f5f7f11d50a3a010101050206050306110802060802060a0400010808070000, + joinHalves 0x1511290105032000080500010812140708001511290105080100080000000000 + 0x1e01000100540216577261704e6f6e457863657074696f6e5468726f77730108, + joinHalves 0x01000200000000003f0100192e4e4554436f72654170702c56657273696f6e3d + 0x7631312e300100540e144672616d65776f726b446973706c61794e616d65092e, + joinHalves 0x4e45542031312e3004010000000801000b000000000000000000000000000000 + 0x00000000100000000000000000000000000000001c2700000000000000000000, + joinHalves 0x3627000000200000000000000000000000000000000000000000000028270000 + 0x000000000000000000005f436f72446c6c4d61696e006d73636f7265652e646c, + joinHalves 0x6c0000000000ff25002000100000000001020304050607080000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000010010000000180000800000000000000000 + 0x0000000000000100010000003000008000000000000000000000000000000100, + joinHalves 0x0000000048000000584000005c02000000000000000000005c02340000005600 + 0x53005f00560045005200530049004f004e005f0049004e0046004f0000000000, + joinHalves 0xbd04effe00000100020001000400030002000100040003003f00000000000000 + 0x0400000002000000000000000000000000000000440000000100560061007200, + joinHalves 0x460069006c00650049006e0066006f0000000000240004000000540072006100 + 0x6e0073006c006100740069006f006e00000000000000b004bc01000001005300, + joinHalves 0x7400720069006e006700460069006c00650049006e0066006f00000098010000 + 0x01003000300030003000300034006200300000002c0002000100460069006c00, + joinHalves 0x65004400650073006300720069007000740069006f006e000000000020000000 + 0x300008000100460069006c006500560065007200730069006f006e0000000000, + joinHalves 0x31002e0032002e0033002e003400000044001200010049006e00740065007200 + 0x6e0061006c004e0061006d006500000053007400720069006300740046006900, + joinHalves 0x780074007500720065002e0064006c006c0000002800020001004c0065006700 + 0x61006c0043006f0070007900720069006700680074000000200000004c001200, + joinHalves 0x01004f0072006900670069006e0061006c00460069006c0065006e0061006d00 + 0x6500000053007400720069006300740046006900780074007500720065002e00, + joinHalves 0x64006c006c000000340008000100500072006f00640075006300740056006500 + 0x7200730069006f006e00000031002e0032002e0033002e003400000038000800, + joinHalves 0x010041007300730065006d0062006c0079002000560065007200730069006f00 + 0x6e00000031002e0032002e0033002e0034000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x002000000c000000483700000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000, + joinHalves 0x0000000000000000000000000000000000000000000000000000000000000000 + 0x0000000000000000000000000000000000000000000000000000000000000000 +] + +def image : StrictBinding.ImageBytes := { length := 4096, pages := [page0] } + +theorem digest : StrictBinding.DigestIs image + 0xa93352ab04c4fb579444797e55f510c70828783c422c46d390b694d0b124bd3d := by decide +kernel +#print axioms digest + +/-- The identity of the assembly the image holds. -/ +def identity : String := + "StrictFixture, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null" + +/-- What System.Reflection.Metadata reads from the image. -/ +def expectedFacts : List StrictBinding.Fact := + [.sectionHeader 1880 8192 2048 512, + .sectionHeader 696 16384 1024 2560, + .sectionHeader 12 24576 512 3584, + .metadata 684 1580, + .stream "tables" 792 668, + .stream "strings" 1460 560, + .stream "userStrings" 2020 4, + .stream "guids" 2024 16, + .stream "blobs" 2040 224, + .heapSizes 0, + .table 0 1 10 892, + .table 1 11 6 902, + .table 2 8 14 968, + .table 4 6 6 1080, + .table 6 10 14 1116, + .table 8 3 6 1256, + .table 10 8 6 1274, + .table 11 2 6 1322, + .table 12 6 6 1334, + .table 15 1 8 1370, + .table 16 2 6 1378, + .table 17 1 2 1390, + .table 21 1 4 1392, + .table 23 1 6 1396, + .table 24 1 6 1402, + .table 27 1 2 1408, + .table 29 1 6 1410, + .table 32 1 22 1416, + .table 35 1 20 1438, + .method 1 8272 592, + .method 2 8280 600, + .method 3 8308 628, + .method 4 8321 641, + .method 5 8324 644, + .method 6 8332 652, + .method 7 8340 660, + .method 8 8343 663, + .method 9 8351 671, + .method 10 8354 674, + .identity + (StrictBinding.asciiOf + "StrictFixture, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null")] + +/-- The method bodies of the image: name, MethodDef token and the bytes of header and code. -/ +def bodies : List (String × Nat × List Nat) := + [("StrictFixture.Arithmetic.Tiny", 0x06000001, + [0x12, 0x02, 0x17, 0x58, 0x2a]), + ("StrictFixture.Arithmetic.Fat", 0x06000002, + [0x13, 0x30, 0x02, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x11, + 0x02, 0x19, 0x5a, 0x1d, 0x59, 0x0a, 0x06, 0x1f, 0x64, 0x31, 0x03, 0x1f, + 0x64, 0x0a, 0x06, 0x2a]), + ("StrictFixture.Arithmetic.get_Table", 0x06000003, + [0x32, 0x7f, 0x06, 0x00, 0x00, 0x04, 0x1e, 0x73, 0x07, 0x00, 0x00, 0x0a, + 0x2a]), + ("StrictFixture.Shape.Sides", 0x06000004, + [0x0a, 0x16, 0x2a]), + ("StrictFixture.Shape.Count", 0x06000005, + [0x1e, 0x02, 0x6f, 0x04, 0x00, 0x00, 0x06, 0x2a]), + ("StrictFixture.Shape..ctor", 0x06000006, + [0x1e, 0x02, 0x28, 0x08, 0x00, 0x00, 0x0a, 0x2a]), + ("StrictFixture.Triangle.Sides", 0x06000007, + [0x0a, 0x19, 0x2a]), + ("StrictFixture.Triangle..ctor", 0x06000008, + [0x1e, 0x02, 0x28, 0x06, 0x00, 0x00, 0x06, 0x2a]), + ("StrictFixture.Square.Sides", 0x06000009, + [0x0a, 0x1a, 0x2a]), + ("StrictFixture.Square..ctor", 0x0600000a, + [0x1e, 0x02, 0x28, 0x06, 0x00, 0x00, 0x06, 0x2a])] + +/-- The assembly block an extraction carries for the image. -/ +def assemblyBlock : CIL.Extraction.AssemblyMetadata := + { identity := "StrictFixture, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null", + tableHeader := + [0, 0, 0, 0, 2, 0, 0, 1, 87, 157, 163, 41, 9, 0, 0, 0, 0, 250, 1, 51, 0, 22, 0, 0, 1, 0, 0, + 0, 11, 0, 0, 0, 8, 0, 0, 0, 6, 0, 0, 0, 10, 0, 0, 0, 3, 0, 0, 0, 8, 0, 0, 0, 2, 0, 0, 0, + 6, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, + 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0], + header := .ofCounts 0 + [(0, 1), (1, 11), (2, 8), (4, 6), (6, 10), (8, 3), (10, 8), (11, 2), (12, 6), (15, 1), + (16, 2), (17, 1), (21, 1), (23, 1), (24, 1), (27, 1), (29, 1), (32, 1), (35, 1)], + assemblyRow := + [4, 128, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], + strings := + [(0, [0]), + (1, [83, 116, 114, 105, 99, 116, 70, 105, 120, 116, 117, 114, 101, 0])], + blobs := + [(0, [0])] } + +/-- A slice an extraction could carry for the image: every row of the carried tables, and the + entries the rows other than Field rows name. -/ +def slice : CIL.Extraction.MetadataSlice := + { assembly := assemblyBlock, + rows := + [(0x01000001, [6, 0, 5, 0, 25, 0]), + (0x01000002, [6, 0, 16, 0, 25, 0]), + (0x01000003, [6, 0, 8, 0, 23, 0]), + (0x01000004, [15, 0, 9, 0, 0, 0]), + (0x01000005, [6, 0, 27, 0, 26, 0]), + (0x01000006, [6, 0, 15, 0, 25, 0]), + (0x01000007, [6, 0, 10, 0, 22, 0]), + (0x01000008, [6, 0, 30, 0, 22, 0]), + (0x01000009, [6, 0, 12, 0, 22, 0]), + (0x0100000a, [6, 0, 14, 0, 22, 0]), + (0x0100000b, [6, 0, 6, 0, 25, 0]), + (0x02000001, [0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0]), + (0x02000002, [1, 1, 0, 0, 19, 0, 21, 0, 29, 0, 1, 0, 1, 0]), + (0x02000003, [17, 1, 16, 0, 13, 0, 21, 0, 33, 0, 4, 0, 1, 0]), + (0x02000004, [129, 1, 16, 0, 4, 0, 21, 0, 37, 0, 6, 0, 1, 0]), + (0x02000005, [1, 0, 16, 0, 17, 0, 21, 0, 37, 0, 6, 0, 4, 0]), + (0x02000006, [1, 0, 16, 0, 29, 0, 21, 0, 20, 0, 6, 0, 7, 0]), + (0x02000007, [1, 0, 16, 0, 20, 0, 21, 0, 20, 0, 6, 0, 9, 0]), + (0x02000008, [0, 1, 0, 0, 3, 0, 0, 0, 37, 0, 6, 0, 11, 0]), + (0x04000001, [6, 6, 255, 255, 255, 255]), + (0x04000002, [86, 128, 255, 255, 255, 255]), + (0x04000003, [86, 128, 255, 255, 255, 255]), + (0x04000004, [6, 0, 255, 255, 255, 255]), + (0x04000005, [6, 0, 255, 255, 255, 255]), + (0x04000006, [51, 1, 255, 255, 255, 255]), + (0x06000001, [0, 0, 0, 0, 0, 0, 150, 0, 28, 0, 4, 0, 1, 0]), + (0x06000002, [0, 0, 0, 0, 0, 0, 150, 0, 11, 0, 4, 0, 2, 0]), + (0x06000003, [0, 0, 0, 0, 0, 0, 150, 8, 31, 0, 11, 0, 3, 0]), + (0x06000004, [0, 0, 0, 0, 0, 0, 198, 1, 18, 0, 3, 0, 3, 0]), + (0x06000005, [0, 0, 0, 0, 0, 0, 150, 0, 7, 0, 7, 0, 3, 0]), + (0x06000006, [0, 0, 0, 0, 0, 0, 134, 24, 1, 0, 2, 0, 4, 0]), + (0x06000007, [0, 0, 0, 0, 0, 0, 198, 0, 18, 0, 3, 0, 4, 0]), + (0x06000008, [0, 0, 0, 0, 0, 0, 134, 24, 1, 0, 2, 0, 4, 0]), + (0x06000009, [0, 0, 0, 0, 0, 0, 198, 0, 18, 0, 3, 0, 4, 0]), + (0x0600000a, [0, 0, 0, 0, 0, 0, 134, 24, 1, 0, 2, 0, 4, 0]), + (0x0a000001, [9, 0, 1, 0, 5, 0]), + (0x0a000002, [17, 0, 1, 0, 2, 0]), + (0x0a000003, [25, 0, 1, 0, 9, 0]), + (0x0a000004, [41, 0, 1, 0, 6, 0]), + (0x0a000005, [49, 0, 1, 0, 5, 0]), + (0x0a000006, [89, 0, 1, 0, 2, 0]), + (0x0a000007, [12, 0, 1, 0, 10, 0]), + (0x0a000008, [73, 0, 1, 0, 2, 0]), + (0x11000001, [1, 0]), + (0x1b000001, [8, 0]), + (0x23000001, [11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 24, 0, 0, 0, 0, 0])], + strings := + [(0, [0]), + (1, [46, 99, 116, 111, 114, 0]), + (2, [60, 77, 111, 100, 117, 108, 101, 62, 0]), + (3, [60, 80, 114, 105, 118, 97, 116, 101, 73, 109, 112, 108, 101, 109, 101, 110, 116, 97, + 116, 105, 111, 110, 68, 101, 116, 97, 105, 108, 115, 62, 0]), + (4, [65, 114, 105, 116, 104, 109, 101, 116, 105, 99, 0]), + (5, [67, 111, 109, 112, 105, 108, 97, 116, 105, 111, 110, 82, 101, 108, 97, 120, 97, 116, + 105, 111, 110, 115, 65, 116, 116, 114, 105, 98, 117, 116, 101, 0]), + (6, [67, 111, 109, 112, 105, 108, 101, 114, 71, 101, 110, 101, 114, 97, 116, 101, 100, 65, + 116, 116, 114, 105, 98, 117, 116, 101, 0]), + (7, [67, 111, 117, 110, 116, 0]), + (8, [68, 101, 98, 117, 103, 103, 97, 98, 108, 101, 65, 116, 116, 114, 105, 98, 117, 116, + 101, 0]), + (9, [68, 101, 98, 117, 103, 103, 105, 110, 103, 77, 111, 100, 101, 115, 0]), + (10, [69, 110, 117, 109, 0]), + (11, [70, 97, 116, 0]), + (12, [79, 98, 106, 101, 99, 116, 0]), + (13, [79, 118, 101, 114, 108, 97, 121, 0]), + (14, [82, 101, 97, 100, 79, 110, 108, 121, 83, 112, 97, 110, 96, 49, 0]), + (15, [82, 101, 102, 83, 97, 102, 101, 116, 121, 82, 117, 108, 101, 115, 65, 116, 116, 114, + 105, 98, 117, 116, 101, 0]), + (16, [82, 117, 110, 116, 105, 109, 101, 67, 111, 109, 112, 97, 116, 105, 98, 105, 108, + 105, 116, 121, 65, 116, 116, 114, 105, 98, 117, 116, 101, 0]), + (17, [83, 104, 97, 112, 101, 0]), + (18, [83, 105, 100, 101, 115, 0]), + (19, [83, 109, 97, 108, 108, 0]), + (20, [83, 113, 117, 97, 114, 101, 0]), + (21, [83, 116, 114, 105, 99, 116, 70, 105, 120, 116, 117, 114, 101, 0]), + (22, [83, 121, 115, 116, 101, 109, 0]), + (23, [83, 121, 115, 116, 101, 109, 46, 68, 105, 97, 103, 110, 111, 115, 116, 105, 99, 115, + 0]), + (24, [83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 0]), + (25, [83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 46, 67, 111, 109, + 112, 105, 108, 101, 114, 83, 101, 114, 118, 105, 99, 101, 115, 0]), + (26, [83, 121, 115, 116, 101, 109, 46, 82, 117, 110, 116, 105, 109, 101, 46, 86, 101, 114, + 115, 105, 111, 110, 105, 110, 103, 0]), + (27, [84, 97, 114, 103, 101, 116, 70, 114, 97, 109, 101, 119, 111, 114, 107, 65, 116, 116, + 114, 105, 98, 117, 116, 101, 0]), + (28, [84, 105, 110, 121, 0]), + (29, [84, 114, 105, 97, 110, 103, 108, 101, 0]), + (30, [86, 97, 108, 117, 101, 84, 121, 112, 101, 0]), + (31, [103, 101, 116, 95, 84, 97, 98, 108, 101, 0])], + blobs := + [(0, [0]), + (1, [3, 7, 1, 8]), + (2, [3, 32, 0, 1]), + (3, [3, 32, 0, 8]), + (4, [4, 0, 1, 8, 8]), + (5, [4, 32, 1, 1, 8]), + (6, [4, 32, 1, 1, 14]), + (7, [5, 0, 1, 8, 18, 20]), + (8, [5, 21, 17, 41, 1, 5]), + (9, [5, 32, 1, 1, 17, 17]), + (10, [6, 32, 2, 1, 15, 1, 8]), + (11, [7, 0, 0, 21, 17, 41, 1, 5]), + (12, [8, 176, 63, 95, 127, 17, 213, 10, 58])] } + +end StrictFixture diff --git a/Tests/StrictBinding/GenericImports.lean b/Tests/StrictBinding/GenericImports.lean new file mode 100644 index 000000000..2bc5a3b0b --- /dev/null +++ b/Tests/StrictBinding/GenericImports.lean @@ -0,0 +1,43 @@ +import StrictBinding.GenericImports + +/-! Reject ambiguous contract sidecar members and substitutions of an audited closure. -/ + +namespace Tests.StrictBinding.GenericImports +open CIL _root_.StrictBinding + +private def image (text : String) : ImageBytes := + ImageBytes.ofBytes (text.toUTF8.data.toList.map UInt8.toNat) + +private def lookup (document : String) : Option String := do + let root ← ContractSidecar.read (image document) + let entry ← ContractSidecar.member root "X" + ContractSidecar.text entry "contractConstant" + +example : lookup (r#"{"exports":[{"identity":"X\u003CT\u003E","contractConstant":"C.id"}]}"#) = + some "C.id" := by decide +kernel +example : lookup (r#"{"exports":[{"identity":"X","contractConstant":"C.id","contract\u0043onstant":"C.other"}]}"#) = + none := by decide +kernel +example : lookup (r#"{"exports":[{"identity":"X"},{"identity":"X","contractConstant":"C.other"}]}"#) = + none := by decide +kernel +example : (ContractSidecar.read (image "{\"exports\":[]}{}")).isNone := by decide +kernel +example : (ContractSidecar.read (image "{\"n\":01}")).isNone := by decide +kernel + +private def sidecar := image <| + "{\"schema\":1,\"formatVersion\":2,\"isReferenceAssembly\":false,\"assemblyIdentity\":\"P\"," ++ + "\"implementationSha256\":\"I\",\"interfaceDigest\":\"D\",\"exports\":[{\"identity\":\"X\"," ++ + "\"profiles\":[\"scalar\"],\"parameterCount\":1,\"hasThis\":false,\"returnsValue\":true," ++ + "\"visibility\":\"public\",\"kind\":\"method\",\"contractConstant\":\"C.id\",\"closureDigest\":\"A\"}]}" + +private def publication : StrictBinding.GenericImports.Publication := + ⟨"X", "P", "I", "D", "scalar", "C.id", "A", sidecar⟩ + +private def slot : ImportSlot := + { identity := "X", closureDigest := "A", parameterCount := 1, hasThis := false, returnsValue := true } + +example : StrictBinding.GenericImports.exportMatches publication slot = true := by decide +kernel +example : StrictBinding.GenericImports.exportMatches { publication with contractName := "C.copy" } slot = + false := by decide +kernel +example : StrictBinding.GenericImports.exportMatches { publication with closureDigest := "B" } + { slot with closureDigest := "B" } = false := by decide +kernel + +end Tests.StrictBinding.GenericImports \ No newline at end of file diff --git a/Tests/StrictBinding/LayoutContent.lean b/Tests/StrictBinding/LayoutContent.lean new file mode 100644 index 000000000..960e576f4 --- /dev/null +++ b/Tests/StrictBinding/LayoutContent.lean @@ -0,0 +1,42 @@ +import StrictBinding.LayoutContent +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +/-! Physical layout checks for the compiled StrictFixture.Overlay and altered metadata. -/ + +namespace Tests.StrictBinding.LayoutContent + +open CIL.Extraction _root_.StrictBinding + +/-- Two 32-bit fields at offsets zero and four occupy the eight-byte explicit Overlay. -/ +private def overlay : RawLayout := { + identity := StrictFixture.identity ++ "::StrictFixture.Overlay", explicit := true, + packing := 0, declaredSize := 8, fields := [⟨.signed32, some 0⟩, ⟨.signed32, some 4⟩] } + +/-- Compare raw layouts against the supplied version of the same compiled assembly. -/ +private def check (layouts : List RawLayout) (bytes := StrictFixture.image) := + checkLayoutContent [⟨StrictFixture.identity, bytes⟩] layouts [] + +/-- Missing images and empty selections leave the fact uncovered. A second conflicting layout + still fails when the first layout lacks a pinned image. -/ +theorem selection : + let missing := {overlay with identity := "Other, Version=1.0.0.0::Missing"} + (check [overlay]).toOption = some true ∧ + (check []).toOption = some false ∧ + (check [missing, overlay]).toOption = some false ∧ + failsWith (check [missing, {overlay with packing := 1}]) + (.layoutMismatch overlay.identity) = true := by decide +kernel +#print axioms selection + +/-- The FieldLayout and ClassLayout rows reject altered offsets and sizes in the physical PE. -/ +theorem altered_tables : + let changed := fun (table column mask : Nat) => StrictFixture.expectedFacts.findSome? fun + | .table number _ _ start => + if number == table then some (flipByte StrictFixture.image (start + column) mask) else none + | _ => none + let rejects := fun bytes => failsWith (check [overlay] bytes) (.layoutMismatch overlay.identity) + (changed 15 2 8).map rejects = some true ∧ + (changed 16 6 4).map rejects = some true := by decide +kernel +#print axioms altered_tables + +end Tests.StrictBinding.LayoutContent diff --git a/Tests/StrictBinding/LayoutRead.lean.in b/Tests/StrictBinding/LayoutRead.lean.in new file mode 100644 index 000000000..99a2ebd55 --- /dev/null +++ b/Tests/StrictBinding/LayoutRead.lean.in @@ -0,0 +1,68 @@ +import Extracted +import CIL.Safety.MethodContract +import CIL.Safety.InstructionSteps + +/-! The same read-only field contract for an original and relocated compiled field. -/ + +namespace LayoutGate +open CIL CIL.Safety + +/-- The caller owns 24 initialized bytes and grants only read access. -/ +def caller (data : Nat → BitVec 8) : Safety.Memory where + allocations := fun id => if id = 0 then some ⟨.callerStack, ⟨24, 1, []⟩, true, [24]⟩ else none + cells := fun _ offset => ⟨data offset, true⟩ + views := [⟨0, 0, 24, true, false⟩] + nextIdentity := 1 + +/-- The four little-endian bytes beginning at the supplied offset in the caller's memory. -/ +def wordAt (offset : Nat) (data : Nat → BitVec 8) : W32 := + BitVec.ofNat 32 (byteNumber [data offset, data (offset+1), data (offset+2), data (offset+3)]) + +/-- The field reader preserves memory and returns the selected four little-endian bytes. -/ +def contract : MethodContract (Nat → BitVec 8) W32 where + args _ := [.reference (.address ⟨0,0⟩)] + results value := [.scalar (.i32 value)] + pre data memory := memory = caller data + footprint _ := [] + post data initial final value := final = initial ∧ value = wordAt FIELD_OFFSET data + +/-- Direct execution suffices for every byte pattern in an original struct. -/ +theorem holds (ran : ∀ data, invoke Extracted.program 8 0 (contract.args data) (caller data) = + .ok (caller data, contract.results (wordAt FIELD_OFFSET data))) : contract.Holds Extracted.program 0 := by + rintro data _ rfl + exact MethodContract.Holds.intro (c := contract) _ _ ⟨8, ran data⟩ (Confined.refl [] _) ⟨rfl, rfl⟩ +#print axioms holds + +/-- The extracted field shape supplies the checked offset and width for every caller memory. -/ +theorem readAtOffset (shape : Extracted.method0.fieldShape Extracted.fieldRef0 = + .value READ_OFFSET 4 false) (data : Nat → BitVec 8) : + invoke Extracted.program 8 0 (contract.args data) (caller data) = + .ok (caller data, contract.results (wordAt READ_OFFSET data)) := by + let frame : Frame := ⟨1, [], [], 0⟩ + let address : Reference := ⟨0, 0⟩ + let args : List Safety.Value := [.reference (.address address)] + let memory := caller data + change invoke Extracted.program 8 0 args memory = .ok (memory, contract.results (wordAt READ_OFFSET data)) + apply invoke_of_run (frame := frame) (entered := memory) Extracted.program_method0 rfl rfl + rw [run_next (fuel := 7) (op := .ldarg 0) (target := 1) (values := args) + (nextFrame := frame) (updated := memory) Extracted.program_method0 rfl rfl] + have read : step Extracted.method0 (.ldfld Extracted.fieldRef0) 1 args frame args memory = + .ok (.next 2 [.scalar (.i32 (wordAt READ_OFFSET data))] frame memory) := by + rw [step_ldfld_value_reference_eq _ _ _ _ _ _ _ _ _ shape address []] + dsimp only [memory, address, frame, wordAt, caller] + rfl + rw [run_next (fuel := 6) Extracted.program_method0 rfl read] + rfl +#print axioms readAtOffset + +/-- The original field's low byte is the only nonzero byte. -/ +def witness (offset : Nat) : BitVec 8 := if offset = FIELD_OFFSET then 128 else 0 + +/-- A read of zero on this input violates the same contract on a successful return. -/ +theorem refuted (ran : invoke Extracted.program 8 0 (contract.args witness) (caller witness) = + .ok (caller witness, [.scalar (.i32 0)])) : contract.Refuted Extracted.program 0 := by + apply MethodContract.Refuted.of_return (c := contract) (input := witness) rfl ran + rintro ⟨_, value, encoded, _, rfl⟩ + revert encoded + decide +kernel +#print axioms refuted diff --git a/Tests/StrictBinding/Metadata.lean b/Tests/StrictBinding/Metadata.lean new file mode 100644 index 000000000..cddff6703 --- /dev/null +++ b/Tests/StrictBinding/Metadata.lean @@ -0,0 +1,94 @@ +import StrictBinding.Evidence +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +/-! Method metadata pages compose over one image preparation, and the distinct Assembly blocks + reject a false block of a later method. -/ + +namespace Tests.StrictBinding.Metadata + +open _root_.StrictBinding +open CIL.Extraction (MethodMetadata MethodEvidence) + +/-- The identity of the fixture with its major version changed from one to zero. -/ +private def otherIdentity : String := + "StrictFixture, Version=0.2.3.4, Culture=neutral, PublicKeyToken=null" + +/-- Two distinct assembly versions, with the changed version after the original. -/ +private def pinned : List PinnedImage := + [{ identity := StrictFixture.identity, image := StrictFixture.image }, + { identity := otherIdentity, image := flipByte StrictFixture.image 1420 1 }] + +/-- A method carrying the fixture's rows and heaps. -/ +private def first : MethodMetadata := { own := StrictFixture.slice } + +/-- A method in the other assembly also carrying metadata from the fixture. -/ +private def second : MethodMetadata := + { own := { assembly := { StrictFixture.assemblyBlock with + identity := otherIdentity, + assemblyRow := flipAt StrictFixture.assemblyBlock.assemblyRow 4 1 } }, + others := [StrictFixture.slice] } + +/-- Two different methods, one on each page. -/ +private def claims : List MethodMetadata := [first, second] + +/-- Both images decode under their own distinct identities. -/ +theorem prepared : ok (prepare pinned) = true := by decide +kernel +#print axioms prepared + +/-- The first page's method metadata belongs to its selected image. -/ +theorem first_page : ClaimsHold methodMetadataBound pinned (claimPage claims 1 0) := by + decide +kernel +#print axioms first_page + +/-- The second page checks both the owning assembly and a referenced assembly. -/ +theorem second_page : ClaimsHold methodMetadataBound pinned (claimPage claims 1 1) := by + decide +kernel +#print axioms second_page + +/-- Independently checked pages cover the complete method metadata claim list. -/ +theorem composed_pages : ClaimsHold methodMetadataBound pinned claims := + claimsHold_of_pages (size := 1) (count := 2) prepared (by decide +kernel) + (.succ second_page (.succ first_page .zero)) +#print axioms composed_pages + +/-- Each distinct Assembly block the two methods carry belongs to its selected image. -/ +theorem blocks : ClaimsHold assemblyBlockBound pinned (assemblyBlocks claims) := by + decide +kernel +#print axioms blocks + +/-- Page results bind the metadata fields of evidence records to selected image bytes. -/ +theorem evidence_from_pages (left right : MethodEvidence) + (leftMetadata : left.body.metadata = first) (rightMetadata : right.body.metadata = second) : + Exists fun images => And (prepare pinned = .ok images) + (forall item, List.Mem item [left, right] -> + forall slice, List.Mem slice item.body.metadata.slices -> + Exists fun image => And (located images slice.assembly.identity = .ok image) + (SliceMatches image slice)) := by + apply sliceMatches_of_metadataRowsBound + have carried : metadataClaims [left, right] = claims := by + simp only [metadataClaims, claims, List.map_cons, List.map_nil, leftMetadata, rightMetadata] + unfold MetadataRowsBound + rw [carried] + exact And.intro blocks composed_pages +#print axioms evidence_from_pages + +/-- The second method falsely carries another version's row for its referenced assembly. -/ +private def falseSecond : MethodMetadata := + { second with others := [{ StrictFixture.slice with + assembly := { StrictFixture.assemblyBlock with + assemblyRow := flipAt StrictFixture.assemblyBlock.assemblyRow 4 1 } }] } + +/-- The changed block keeps the fixture's identity yet stays a distinct block to check, since + blocks are compared whole. -/ +theorem keeps_blocks_of_one_identity : (assemblyBlocks [first, falseSecond]).length = 3 := by + decide +kernel +#print axioms keeps_blocks_of_one_identity + +/-- A valid block of the first method cannot make a changed Assembly row of the second pass. -/ +theorem rejects_false_second_block : + failsWith (checkAll assemblyBlockBound pinned (assemblyBlocks [first, falseSecond])) + (.rowMismatch assemblyToken) = true := by decide +kernel +#print axioms rejects_false_second_block + +end Tests.StrictBinding.Metadata diff --git a/Tests/StrictBinding/Mutation.lean b/Tests/StrictBinding/Mutation.lean new file mode 100644 index 000000000..4243f3823 --- /dev/null +++ b/Tests/StrictBinding/Mutation.lean @@ -0,0 +1,59 @@ +import StrictBinding.Image + +/-! +# Strict binding tests: mutations + +Helpers that read or change the bytes of an image or of carried metadata and inspect the error a +check gives, shared by the tests that reject a changed file or a changed claim. +-/ + +namespace Tests.StrictBinding + +open _root_.StrictBinding + +/-- The image with the byte at `offset` xor-ed with `mask`. -/ +def flipByte (image : ImageBytes) (offset mask : Nat) : ImageBytes := + let rec flipIn : List (List Nat) → Nat → List (List Nat) + | [], _ => [] + | page :: rest, chunk => + if chunk < page.length then + let flipped := Nat.xor (page.getD chunk 0) (mask * 2 ^ (8 * (63 - offset % 64))) + page.set chunk flipped :: rest + else page :: flipIn rest (chunk - page.length) + { image with pages := flipIn image.pages (offset / 64) } + +/-- The image with several bytes changed: each `(offset, mask)` xor-ed in turn. -/ +def flipBytes (image : ImageBytes) (changes : List (Nat × Nat)) : ImageBytes := + changes.foldl (fun changed (offset, mask) => flipByte changed offset mask) image + +/-- The bytes of an image, read one at a time; empty when the image is malformed. -/ +def imageBytes (image : ImageBytes) : List Nat := + match ImageView.ofImage image >>= (·.bytes 0 image.length) with + | .ok bytes => bytes + | .error _ => [] + +/-- The little-endian value of `width` bytes at `offset`. -/ +def readLittle (bytes : List Nat) (offset width : Nat) : Nat := + (List.range width).foldl + (fun value index => value + bytes.getD (offset + index) 0 * 256 ^ index) 0 + +/-- The bytes with `width` bytes at `offset` replaced by the little-endian `value`. -/ +def writeLittle (bytes : List Nat) (offset width value : Nat) : List Nat := + (List.range width).foldl + (fun bytes index => bytes.set (offset + index) (value / 256 ^ index % 256)) bytes + +/-- The bytes with `replacement` written from `offset` on. -/ +def writeBytes (bytes : List Nat) (offset : Nat) (replacement : List Nat) : List Nat := + replacement.zipIdx.foldl (fun bytes (byte, index) => bytes.set (offset + index) byte) bytes + +/-- The bytes with the byte at `index` xor-ed with `mask`. -/ +def flipAt (bytes : List Nat) (index mask : Nat) : List Nat := + bytes.set index (Nat.xor (bytes.getD index 0) mask) + +/-- Whether a check failed with exactly this error. -/ +def failsWith {α : Type} (result : Except AuditError α) (error : AuditError) : Bool := + match result with + | .error found => found == error + | .ok _ => false + +end Tests.StrictBinding diff --git a/Tests/StrictBinding/PlatformKinds.lean b/Tests/StrictBinding/PlatformKinds.lean new file mode 100644 index 000000000..6f28d7553 --- /dev/null +++ b/Tests/StrictBinding/PlatformKinds.lean @@ -0,0 +1,86 @@ +import StrictBinding.GenericConstraints +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +namespace Tests.StrictBinding.PlatformKinds + +open CIL.Extraction _root_.StrictBinding + +private def shape (row : Nat) : Option (_root_.StrictBinding.TypeKind × Nat) := do + let image ← (prepareOne ⟨StrictFixture.identity, StrictFixture.image⟩).toOption + (physicalTypeShape image row).toOption + +/-- Small, Overlay, Shape and Triangle have their CLI kinds in the compiled fixture. -/ +theorem compiled_type_shapes : + shape 2 = some (.enum, 0) ∧ shape 3 = some (.valueType, 0) ∧ + shape 5 = some (.classType, 0) ∧ shape 6 = some (.classType, 0) ∧ + shape 3 ≠ some (.classType, 0) ∧ shape 5 ≠ some (.classType, 1) := by decide +kernel +#print axioms compiled_type_shapes + +/-- The fixture has one final AssemblyRef row. Give its table slot to GenericParam and + set the ordinal and TypeOrMethodDef owner in the resulting physical row. -/ +private def parameterImage (ordinal owner flags : Nat) : Except AuditError PreparedImage := do + let start := StrictFixture.expectedFacts.findSome? fun fact => match fact with + | .stream "tables" offset _ => some offset + | _ => none + let last := StrictFixture.expectedFacts.findSome? fun fact => match fact with + | .table 0x23 _ _ offset => some offset + | _ => none + let some tables := start | throw (.typeDefMismatch 6 .kind) + let some field := last | throw (.typeDefMismatch 6 .kind) + let bytes := flipBytes StrictFixture.image + [(tables + 12, 0x08), (tables + 13, 0x04), (field, 11 ^^^ ordinal), + (field + 2, flags), (field + 4, owner)] + prepareOne ⟨StrictFixture.identity, bytes⟩ + +/-- The physical arity of the sixth TypeDef after assigning the parameter row. -/ +private def withParameter (ordinal owner : Nat) : Except AuditError Nat := do + physicalTypeArity (← parameterImage ordinal owner 0) 6 + +/-- Only the target TypeDef's parameter contributes to its arity. A gap in its ordinals fails. -/ +theorem type_parameter_ownership : + (withParameter 0 12).toOption = some 1 ∧ (withParameter 0 10).toOption = some 0 ∧ + (withParameter 0 13).toOption = some 0 ∧ + failsWith (withParameter 1 12) (.typeDefMismatch 6 .kind) = true := by decide +kernel +#print axioms type_parameter_ownership + +private def overlay (kind : PlatformKind) : PlatformDefinition := + { scope := bytesOf StrictFixture.identity + parts := [asciiOf "StrictFixture", asciiOf "Overlay"] + kind } + +private def checkKinds (claims : List PlatformDefinition) : Except AuditError Bool := do + let image ← prepareOne ⟨StrictFixture.identity, StrictFixture.image⟩ + return (← claims.mapM (coveredPlatformKind [image])).all id + +/-- The pinned Overlay definition is a nongeneric value type. A missing assembly leaves + coverage partial and cannot conceal another entry with the wrong physical kind. -/ +theorem physical_kind_and_partial_coverage : + let absent := { overlay .valueType with scope := bytesOf "Missing" } + (checkKinds [overlay .valueType]).toOption = some true ∧ + (checkKinds [absent, overlay .valueType]).toOption = some false ∧ + failsWith (checkKinds [absent, overlay .classType]) (.typeDefMismatch 3 .kind) = true ∧ + failsWith (checkKinds [overlay (.genericValue 1), absent]) (.typeDefMismatch 3 .kind) = true := by + decide +kernel +#print axioms physical_kind_and_partial_coverage + +/-- The first MethodDef's declaration in an image with one physical generic parameter. -/ +private def methodParameter (ordinal owner flags : Nat) (claims : List GenericConstraint) : + Except AuditError Unit := do + let image ← parameterImage ordinal owner flags + parameterDeclarations image (_root_.StrictBinding.tokenOf Table.methodDef 1) claims + +/-- Moving a parameter to the owning type, altering its flags or ordinal, adding a constraint, + or omitting the parameter cannot reconfirm the original method declaration. -/ +theorem method_parameter_declaration : + let present : GenericConstraint := ⟨24, [], []⟩ + let mismatch := AuditError.constraintMismatch (_root_.StrictBinding.tokenOf Table.methodDef 1) + (methodParameter 0 3 24 [present]).toOption = some () ∧ + failsWith (methodParameter 0 2 24 [present]) mismatch = true ∧ + failsWith (methodParameter 0 3 8 [present]) mismatch = true ∧ + failsWith (methodParameter 1 3 24 [present]) mismatch = true ∧ + failsWith (methodParameter 0 3 24 [{present with types := ["IForged"]}]) mismatch = true ∧ + failsWith (methodParameter 0 3 24 []) mismatch = true := by decide +kernel +#print axioms method_parameter_declaration + +end Tests.StrictBinding.PlatformKinds diff --git a/Tests/StrictBinding/Rows.lean b/Tests/StrictBinding/Rows.lean new file mode 100644 index 000000000..88e2b08c7 --- /dev/null +++ b/Tests/StrictBinding/Rows.lean @@ -0,0 +1,633 @@ +import StrictBinding.Rows +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +/-! +# Strict binding tests: carried metadata + +The assembly block an extraction carries for the fixture (`StrictFixture.assemblyBlock`, read by +System.Reflection.Metadata) is the image's, and `assemblyMatches_of_assemblyBound` turns the check +into the proposition. A block changed in one place is refused with the error that names the place: +the header bytes, the decoded header, the `Assembly` row, a key or an entry. Two changes are made +to the image instead, with the fixture's block carried unchanged. The fixture has no culture and +no public key, so variants whose culture is a suffix of the name, or whose eight-byte key is at +`#Blob` offset 1, cover two numbered string entries and a key entry at a nonzero offset. A carried +entry no row cell holds is refused by the check and by the proposition. + +The rows and entries of `StrictFixture.slice`, every row of the carried tables with the entries +only its Field rows name omitted, are the image's under `rowsMatch_of_rowsBound`. A changed row, +token, key or entry is refused, and an image changed only in a MethodDef RVA is accepted. +-/ + +namespace Tests.StrictBinding.Rows + +open _root_.StrictBinding +open CIL.Extraction (AssemblyMetadata ByteEntry MetadataSlice stringsHeap blobHeap) + +/-- The fixture, prepared as the audit prepares a pinned image. -/ +private def fixture : Except AuditError PreparedImage := + prepareOne { identity := StrictFixture.identity, image := StrictFixture.image } + +/-- The assembly check of a carried block against a prepared image. -/ +private def checked (prepared : Except AuditError PreparedImage) (assembly : AssemblyMetadata) : + Except AuditError Unit := + prepared >>= (assemblyBound · assembly) + +/-- The carried block of the fixture. -/ +private def block : AssemblyMetadata := StrictFixture.assemblyBlock + +/-- The offset of the major version in a carried `Assembly` row. -/ +private def majorVersionAt : Nat := 4 + +/-- The offset of the `PublicKey` key in a carried `Assembly` row with two-byte heap indexes. -/ +private def publicKeyAt : Nat := 16 + +/-- The offset of the `Name` key in a carried `Assembly` row with two-byte heap indexes. -/ +private def nameAt : Nat := 18 + +/-- The offset of the `Culture` key in a carried `Assembly` row with two-byte heap indexes. -/ +private def cultureAt : Nat := 20 + +/-- The fixture's carried `Assembly` row with the two-byte key at `offset` set to `key`. -/ +private def rowWithKey (offset key : Nat) : List Nat := writeLittle block.assemblyRow offset 2 key + +/-! ## The fixture's block -/ + +/-- The fixture's carried block passes the check. -/ +theorem accepts_the_carried_assembly_block : ok (checked fixture block) = true := by + decide +kernel +#print axioms accepts_the_carried_assembly_block + +/-- The block is the fixture's in the sense of `AssemblyMatches`. -/ +theorem the_carried_block_matches_the_fixture {image : PreparedImage} + (prepared : fixture = .ok image) : AssemblyMatches image block := by + apply assemblyMatches_of_assemblyBound (prepareOne_bounded prepared) + have accepted := accepts_the_carried_assembly_block + simp only [checked, prepared, bind, Except.bind] at accepted + cases checkedBlock : assemblyBound image block with + | error _ => simp [checkedBlock, ok] at accepted + | ok _ => rfl +#print axioms the_carried_block_matches_the_fixture + +/-- The TypeRef row count, at byte 28 of the header, is 10 instead of 11. -/ +theorem rejects_a_changed_header_byte : + failsWith (checked fixture { block with tableHeader := flipAt block.tableHeader 28 1 }) + (.headerMismatch StrictFixture.identity) = true := by + decide +kernel +#print axioms rejects_a_changed_header_byte + +/-- The `Valid` mask, from byte 8, also marks table 3 present. -/ +theorem rejects_a_flipped_valid_bit : + failsWith (checked fixture { block with tableHeader := flipAt block.tableHeader 8 0x08 }) + (.headerMismatch StrictFixture.identity) = true := by + decide +kernel +#print axioms rejects_a_flipped_valid_bit + +/-- A header four bytes longer or shorter than the image's `Valid` mask declares. -/ +theorem rejects_a_header_length_off_by_four : + failsWith (checked fixture { block with tableHeader := block.tableHeader ++ [0, 0, 0, 0] }) + (.headerMismatch StrictFixture.identity) = true ∧ + failsWith + (checked fixture + { block with tableHeader := block.tableHeader.take (block.tableHeader.length - 4) }) + (.headerMismatch StrictFixture.identity) = true := by + decide +kernel +#print axioms rejects_a_header_length_off_by_four + +/-- A decoded header with 12 TypeRef rows where the header bytes say 11. -/ +theorem rejects_a_decoded_header_that_differs_from_the_bytes : + failsWith + (checked fixture + { block with header := .ofCounts 0 (block.header.rowCounts.set 1 (0x01, 12)) }) + (.headerMismatch StrictFixture.identity) = true := by + decide +kernel +#print axioms rejects_a_decoded_header_that_differs_from_the_bytes + +/-- The carried `Sorted` mask, from byte 16, differs. Decoding ignores `Sorted`, so only the + comparison with the image bytes refuses it. -/ +theorem rejects_a_changed_sorted_byte : + failsWith (checked fixture { block with tableHeader := flipAt block.tableHeader 16 1 }) + (.headerMismatch StrictFixture.identity) = true := by + decide +kernel +#print axioms rejects_a_changed_sorted_byte + +/-- The file offset of the image's `HashAlgId`, the first byte of its `Assembly` row. -/ +private def hashAlgorithmAt : Nat := 1416 + +/-- The file offset of the image's `Sorted` mask, 16 bytes into its table stream at 792. -/ +private def sortedMaskAt : Nat := 792 + 16 + +/-- The fixture image with the byte at `offset` changed, prepared under the fixture's identity. -/ +private def changedImage (offset : Nat) : Except AuditError PreparedImage := + prepareOne { identity := StrictFixture.identity, image := flipByte StrictFixture.image offset 1 } + +/-- The fixture's carried block against an image whose `HashAlgId` differs. -/ +theorem rejects_an_image_with_a_changed_hash_algorithm : + failsWith (checked (changedImage hashAlgorithmAt) block) + (.rowMismatch assemblyToken) = true := by + decide +kernel +#print axioms rejects_an_image_with_a_changed_hash_algorithm + +/-- The fixture's carried block against an image whose `Sorted` mask differs. -/ +theorem rejects_an_image_with_a_changed_sorted_byte : + failsWith (checked (changedImage sortedMaskAt) block) + (.headerMismatch StrictFixture.identity) = true := by + decide +kernel +#print axioms rejects_an_image_with_a_changed_sorted_byte + +/-- The major version in the `Assembly` row is 0 instead of 1. -/ +theorem rejects_a_changed_assembly_row_byte : + failsWith + (checked fixture { block with assemblyRow := flipAt block.assemblyRow majorVersionAt 1 }) + (.rowMismatch assemblyToken) = true := by + decide +kernel +#print axioms rejects_a_changed_assembly_row_byte + +/-- The first byte of the carried name is `T` instead of `S`. -/ +theorem rejects_a_changed_name_byte : + failsWith + (checked fixture + { block with + strings := block.strings.set 1 (1, flipAt (block.strings.getD 1 (0, [])).2 0 7) }) + (.heapMismatch stringsHeap 1) = true := by + decide +kernel +#print axioms rejects_a_changed_name_byte + +/-- Key 0 says the name is at offset 0, where the image's name is not. -/ +theorem rejects_key_zero_for_a_nonzero_offset : + failsWith (checked fixture { block with assemblyRow := rowWithKey nameAt 0 }) + (.rowMismatch assemblyToken) = true := by + decide +kernel +#print axioms rejects_key_zero_for_a_nonzero_offset + +/-- The omitted-entry marker says the culture is at a nonzero offset, where the image has 0. -/ +theorem rejects_the_marker_for_offset_zero : + failsWith + (checked fixture { block with assemblyRow := rowWithKey cultureAt 0xFFFF }) + (.rowMismatch assemblyToken) = true := by + decide +kernel +#print axioms rejects_the_marker_for_offset_zero + +/-- Name key 1 without the entry it ranks. -/ +theorem rejects_a_string_key_without_its_entry : + failsWith (checked fixture { block with strings := block.strings.filter (·.1 != 1) }) + (.heapNumbering stringsHeap 1) = true := by + decide +kernel +#print axioms rejects_a_string_key_without_its_entry + +/-- Keys 0, for the empty culture and the absent key, need no carried entry. -/ +theorem accepts_zero_keys_without_entries : + ok (checked fixture + { block with strings := block.strings.filter (·.1 != 0), blobs := [] }) = true := by + decide +kernel +#print axioms accepts_zero_keys_without_entries + +/-! ## Entries at a suffix of the name -/ + +/-- The `#Strings` offset of the fixture's name. -/ +private def nameOffset : Nat := 207 + +/-- The `#Strings` offset of `Fixture`, the suffix of the name. -/ +private def suffixOffset : Nat := 213 + +/-- `Fixture` with its terminator. -/ +private def suffixBytes : List Nat := [70, 105, 120, 116, 117, 114, 101, 0] + +/-- The fixture's name entry. -/ +private def nameBytes : List Nat := (block.strings.getD 1 (0, [])).2 + +/-- The fixture's block with the name's suffix carried under key 2, which no row cell holds. -/ +private def suffixBlock : AssemblyMetadata := + { block with strings := block.strings ++ [(2, suffixBytes)] } + +/-- A carried entry under a key no row cell holds. -/ +theorem rejects_an_entry_no_row_names : + failsWith (checked fixture suffixBlock) (.heapNumbering stringsHeap 2) = true := by + decide +kernel +#print axioms rejects_an_entry_no_row_names + +/-- The `#Strings` keys of the fixture's carried row: 1 for the name and 0 for the culture. -/ +private theorem fixture_string_keys : + (match fixture with + | .ok image => + (image.columnsOf (assemblyToken / 2 ^ 24)).map (rowKeys stringsHeap · block.assemblyRow) + | .error _ => none) = some [1, 0] := by + decide +kernel +#print axioms fixture_string_keys + +/-- No numbering makes the suffix block the fixture's, since its key 2 is held by no row cell. -/ +theorem the_suffix_block_does_not_match {image : PreparedImage} (prepared : fixture = .ok image) : + ¬ AssemblyMatches image suffixBlock := by + rintro ⟨_, _, _, _, _, _, _, entries, _, _, _, held, _⟩ + obtain ⟨offset, named, _⟩ := entries (2, suffixBytes) (by simp [suffixBlock]) + rcases named with ⟨two, _⟩ | given + · cases two + · obtain ⟨row, member, columns, found, inRow⟩ := held offset 2 given + simp only [List.mem_singleton] at member + subst member + have keys := fixture_string_keys + simp only [prepared, found, Option.map_some, Option.some.injEq] at keys + change 2 ∈ rowKeys stringsHeap columns block.assemblyRow at inRow + rw [keys] at inRow + simp at inRow +#print axioms the_suffix_block_does_not_match + +/-- The fixture with the culture cell of its `Assembly` row set to `offset`. -/ +private def culturedImage (offset : Nat) : ImageBytes := + match fixture with + | .ok image => + match image.layout.cellAt assemblyToken 8 with + | .ok (cell, width) => + ImageBytes.ofBytes (writeLittle (imageBytes StrictFixture.image) cell width offset) + | .error _ => StrictFixture.image + | .error _ => StrictFixture.image + +/-- The variant whose culture is `culture`, at `offset`, prepared under the identity it spells. -/ +private def cultured (offset : Nat) (culture : String) : Except AuditError PreparedImage := + prepareOne { image := culturedImage offset, identity := + "StrictFixture, Version=1.2.3.4, Culture=" ++ culture ++ ", PublicKeyToken=null" } + +/-- The fixture's carried row with name key `name` and culture key `culture`. -/ +private def rowWithNames (name culture : Nat) : List Nat := + writeLittle (rowWithKey nameAt name) cultureAt 2 culture + +/-- The culture `Fixture` at the suffix of the name: two numbered entries, ranked by their + bytes. -/ +theorem accepts_a_culture_at_a_suffix_of_the_name : + ok (checked (cultured suffixOffset "Fixture") + { block with assemblyRow := rowWithNames 2 1, + strings := [(0, [0]), (1, suffixBytes), (2, nameBytes)] }) = true := by + decide +kernel +#print axioms accepts_a_culture_at_a_suffix_of_the_name + +/-- One key for the name and the culture, which are at different offsets. -/ +theorem rejects_one_key_for_two_offsets : + failsWith (checked (cultured suffixOffset "Fixture") + { block with assemblyRow := rowWithNames 2 2, strings := [(0, [0]), (2, nameBytes)] }) + (.heapNumbering stringsHeap 2) = true := by + decide +kernel +#print axioms rejects_one_key_for_two_offsets + +/-- Two keys for the name and the culture, which are at one offset. -/ +theorem rejects_two_keys_for_one_offset : + failsWith (checked (cultured nameOffset "StrictFixture") + { block with assemblyRow := rowWithNames 1 2, + strings := [(0, [0]), (1, nameBytes), (2, nameBytes)] }) + (.heapNumbering stringsHeap 2) = true := by + decide +kernel +#print axioms rejects_two_keys_for_one_offset + +/-! ## A public key at a nonzero offset -/ + +/-- The eight bytes of the key in the fixture's Assembly definition. -/ +private def keyBytes : List Nat := [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef] + +/-- The fixture with its key blob, length prefix and key, written at `#Blob` offset 1 and named + by its `Assembly` row. -/ +private def keyedImage : ImageBytes := + match fixture with + | .ok image => + match image.layout.cellAt assemblyToken 6 with + | .ok (keyCell, keyWidth) => + let named := writeLittle (imageBytes StrictFixture.image) keyCell keyWidth 1 + ImageBytes.ofBytes + (writeBytes named (image.layout.pe.blobs.start + 1) (keyBytes.length :: keyBytes)) + | .error _ => StrictFixture.image + | .error _ => StrictFixture.image + +/-- The variant, prepared under the identity its key spells. -/ +private def keyed : Except AuditError PreparedImage := + prepareOne { image := keyedImage, identity := + "StrictFixture, Version=1.2.3.4, Culture=neutral, PublicKeyToken=4705f121e18edfe3" } + +/-- The block an extraction carries for the variant: its key is the only blob, ranked 1. -/ +private def keyedBlock : AssemblyMetadata := + { block with assemblyRow := rowWithKey publicKeyAt 1, + blobs := [(1, keyBytes.length :: keyBytes)] } + +/-- The variant's carried block, with its key at `#Blob` offset 1, passes the check. -/ +theorem accepts_a_carried_key_at_a_nonzero_offset : ok (checked keyed keyedBlock) = true := by + decide +kernel +#print axioms accepts_a_carried_key_at_a_nonzero_offset + +/-- Blob key 1 without the entry it ranks. -/ +theorem rejects_a_blob_key_without_its_entry : + failsWith (checked keyed { keyedBlock with blobs := [] }) + (.heapNumbering blobHeap 1) = true := by + decide +kernel +#print axioms rejects_a_blob_key_without_its_entry + +/-- The last key byte is `0xee` instead of `0xef`. -/ +theorem rejects_a_changed_key_byte : + failsWith + (checked keyed { keyedBlock with blobs := [(1, flipAt (keyBytes.length :: keyBytes) 8 1)] }) + (.heapMismatch blobHeap 1) = true := by + decide +kernel +#print axioms rejects_a_changed_key_byte + +/-- Key 0 says the key is at offset 0, where the image's key is not. -/ +theorem rejects_key_zero_for_the_key_offset : + failsWith + (checked keyed { keyedBlock with assemblyRow := rowWithKey publicKeyAt 0 }) + (.rowMismatch assemblyToken) = true := by + decide +kernel +#print axioms rejects_key_zero_for_the_key_offset + +/-- The marker says the key is not carried, which holds; a carried key no row names is refused. -/ +theorem accepts_the_marker_only_without_the_entry : + ok (checked keyed + { keyedBlock with assemblyRow := rowWithKey publicKeyAt 0xFFFF, blobs := [] }) = true ∧ + failsWith + (checked keyed { keyedBlock with assemblyRow := rowWithKey publicKeyAt 0xFFFF }) + (.heapNumbering blobHeap 1) = true := by + decide +kernel +#print axioms accepts_the_marker_only_without_the_entry + +/-! ## Rows of a slice -/ + +/-- The slice an extraction could carry for the fixture. -/ +private def slice : MetadataSlice := StrictFixture.slice + +/-- The rows check of carried rows and entries against a prepared image. -/ +private def rowsChecked (prepared : Except AuditError PreparedImage) + (rows strings blobs : List ByteEntry) : Except AuditError Unit := + prepared >>= (rowsBound · rows strings blobs) + +/-- The slice's rows with the row of `token` changed by `change`. -/ +private def withRow (token : Nat) (change : List Nat → List Nat) : List ByteEntry := + slice.rows.map fun row => if row.1 == token then (row.1, change row.2) else row + +/-- The token of the `Arithmetic.Tiny` MethodDef row. -/ +private def tinyToken : Nat := 0x06000001 + +/-- The token of the `Arithmetic.Fat` MethodDef row. -/ +private def fatToken : Nat := 0x06000002 + +/-- The token of the first `.ctor` MethodDef row. -/ +private def constructorToken : Nat := 0x06000006 + +/-- The token of the `` TypeDef row, whose namespace is at `#Strings` offset 0. -/ +private def moduleTypeToken : Nat := 0x02000001 + +/-- The token of the `Small` TypeDef row. -/ +private def smallToken : Nat := 0x02000002 + +/-- The token of the `Overlay` TypeDef row. -/ +private def overlayToken : Nat := 0x02000003 + +/-- The offset of the RVA in a MethodDef row. -/ +private def rvaAt : Nat := 0 + +/-- The offset of the implementation flags in a MethodDef row, the byte after its RVA. -/ +private def implementationAt : Nat := 4 + +/-- The offset of the flags in a MethodDef row. -/ +private def flagsAt : Nat := 6 + +/-- The offset of the name key in a MethodDef row with two-byte heap indexes. -/ +private def methodNameAt : Nat := 8 + +/-- The offset of the name key in a TypeDef row with two-byte heap indexes. -/ +private def typeNameAt : Nat := 4 + +/-- The offset of the namespace key in a TypeDef row with two-byte heap indexes. -/ +private def typeNamespaceAt : Nat := 6 + +/-- The slice's key of `Small`. -/ +private def smallKey : Nat := 19 + +/-- The slice's key of `Fat`. -/ +private def fatKey : Nat := 11 + +/-- The slice's key of `Tiny`. -/ +private def tinyKey : Nat := 28 + +/-- The first `#Strings` key the slice does not use. -/ +private def unusedKey : Nat := 32 + +/-- `.ctor` with its terminator. -/ +private def constructorBytes : List Nat := [46, 99, 116, 111, 114, 0] + +/-- The fixture with the byte at `column` of the row of `token` changed. -/ +private def changedRow (token column : Nat) : Except AuditError PreparedImage := + match fixture with + | .ok image => + match image.layout.rowAt token with + | .ok (offset, _) => + prepareOne { identity := StrictFixture.identity, + image := flipByte StrictFixture.image (offset + column) 1 } + | .error error => .error error + | .error error => .error error + +/-- The slice's rows, MethodDef RVAs zeroed and Field entries omitted, pass the check. -/ +theorem accepts_the_carried_slice_rows : + ok (rowsChecked fixture slice.rows slice.strings slice.blobs) = true := by + decide +kernel +#print axioms accepts_the_carried_slice_rows + +/-- The slice's rows and entries are the fixture's in the sense of `RowsMatch`. -/ +theorem the_carried_slice_rows_match_the_fixture {image : PreparedImage} + (prepared : fixture = .ok image) : RowsMatch image slice.rows slice.strings slice.blobs := by + apply rowsMatch_of_rowsBound (prepareOne_bounded prepared) + have accepted := accepts_the_carried_slice_rows + simp only [rowsChecked, prepared, bind, Except.bind] at accepted + cases checkedRows : rowsBound image slice.rows slice.strings slice.blobs with + | error _ => simp [checkedRows, ok] at accepted + | ok _ => rfl +#print axioms the_carried_slice_rows_match_the_fixture + +/-- Where a body is placed is not a row fact: an image whose `Tiny` RVA differs still matches. -/ +theorem accepts_an_image_with_a_changed_rva : + ok (rowsChecked (changedRow tinyToken rvaAt) slice.rows slice.strings slice.blobs) = true := by + decide +kernel +#print axioms accepts_an_image_with_a_changed_rva + +/-- The mask is the four RVA bytes: an image change to the last of them is accepted, and a change + to the first byte of ImplFlags after them is refused in the image and in the carried row. -/ +theorem masks_exactly_the_rva : + ok (rowsChecked (changedRow tinyToken (implementationAt - 1)) slice.rows slice.strings + slice.blobs) = true ∧ + failsWith + (rowsChecked (changedRow tinyToken implementationAt) slice.rows slice.strings slice.blobs) + (.rowMismatch tinyToken) = true ∧ + failsWith + (rowsChecked fixture (withRow tinyToken (flipAt · implementationAt 1)) slice.strings + slice.blobs) + (.rowMismatch tinyToken) = true := by + decide +kernel +#print axioms masks_exactly_the_rva + +/-- The mask reaches no other table: a TypeDef flags byte of `Small` changed in the image, or in + the carried row, is refused. -/ +theorem rejects_a_changed_typedef_flags_byte : + failsWith (rowsChecked (changedRow smallToken 0) slice.rows slice.strings slice.blobs) + (.rowMismatch smallToken) = true ∧ + failsWith + (rowsChecked fixture (withRow smallToken (flipAt · 0 1)) slice.strings slice.blobs) + (.rowMismatch smallToken) = true := by + decide +kernel +#print axioms rejects_a_changed_typedef_flags_byte + +/-- A carried MethodDef row holds its RVA as zero. -/ +theorem rejects_a_carried_nonzero_rva : + failsWith + (rowsChecked fixture (withRow tinyToken (flipAt · rvaAt 1)) slice.strings slice.blobs) + (.rowMismatch tinyToken) = true := by + decide +kernel +#print axioms rejects_a_carried_nonzero_rva + +/-- A flags byte of `Tiny` changed in the carried row, or in the image. -/ +theorem rejects_a_changed_flags_byte : + failsWith + (rowsChecked fixture (withRow tinyToken (flipAt · flagsAt 1)) slice.strings slice.blobs) + (.rowMismatch tinyToken) = true ∧ + failsWith (rowsChecked (changedRow tinyToken flagsAt) slice.rows slice.strings slice.blobs) + (.rowMismatch tinyToken) = true := by + decide +kernel +#print axioms rejects_a_changed_flags_byte + +/-- The rows of `Tiny` and `Fat` carried under each other's tokens. -/ +theorem rejects_swapped_row_tokens : + failsWith + (rowsChecked fixture + (slice.rows.map fun row => + if row.1 == tinyToken then (fatToken, row.2) + else if row.1 == fatToken then (tinyToken, row.2) else row) + slice.strings slice.blobs) + (.rowMismatch fatToken) = true := by + decide +kernel +#print axioms rejects_swapped_row_tokens + +/-- The row of `Tiny` one byte short. -/ +theorem rejects_a_row_one_byte_short : + failsWith (rowsChecked fixture (withRow tinyToken (·.dropLast)) slice.strings slice.blobs) + (.rowMismatch tinyToken) = true := by + decide +kernel +#print axioms rejects_a_row_one_byte_short + +/-- A GenericParam row, a table the fixture does not have; an eleventh MethodDef row; MethodDef row + 0; and a row of table 0x2D, above the tables the schema covers. -/ +theorem rejects_a_token_the_tables_do_not_have : + failsWith + (rowsChecked fixture (slice.rows ++ [(0x2A000001, List.replicate 8 0)]) + slice.strings slice.blobs) + (.tokenOutOfScope 0x2A000001) = true ∧ + failsWith + (rowsChecked fixture (slice.rows ++ [(0x0600000B, List.replicate 14 0)]) + slice.strings slice.blobs) + (.tokenOutOfScope 0x0600000B) = true ∧ + failsWith + (rowsChecked fixture (slice.rows ++ [(0x06000000, List.replicate 14 0)]) + slice.strings slice.blobs) + (.tokenOutOfScope 0x06000000) = true ∧ + failsWith + (rowsChecked fixture (slice.rows ++ [(0x2D000001, List.replicate 4 0)]) + slice.strings slice.blobs) + (.tokenOutOfScope 0x2D000001) = true := by + decide +kernel +#print axioms rejects_a_token_the_tables_do_not_have + +/-- Key 0 says the name of `Small` is at offset 0, where it is not. -/ +theorem rejects_key_zero_for_a_nonzero_row_offset : + failsWith + (rowsChecked fixture (withRow smallToken (writeLittle · typeNameAt 2 0)) + slice.strings slice.blobs) + (.rowMismatch smallToken) = true := by + decide +kernel +#print axioms rejects_key_zero_for_a_nonzero_row_offset + +/-- The marker says the namespace of `` is at a nonzero offset, where it is at 0. -/ +theorem rejects_the_marker_for_a_zero_row_offset : + failsWith + (rowsChecked fixture (withRow moduleTypeToken (writeLittle · typeNamespaceAt 2 0xFFFF)) + slice.strings slice.blobs) + (.rowMismatch moduleTypeToken) = true := by + decide +kernel +#print axioms rejects_the_marker_for_a_zero_row_offset + +/-- The key of `Small` for the name of `Overlay`, which is at another offset. -/ +theorem rejects_one_row_key_for_two_offsets : + failsWith + (rowsChecked fixture (withRow overlayToken (writeLittle · typeNameAt 2 smallKey)) + slice.strings slice.blobs) + (.heapNumbering stringsHeap smallKey) = true := by + decide +kernel +#print axioms rejects_one_row_key_for_two_offsets + +/-- A second key for `.ctor`, with its entry carried, in one of the rows that name it. -/ +theorem rejects_two_row_keys_for_one_offset : + failsWith + (rowsChecked fixture (withRow constructorToken (writeLittle · methodNameAt 2 unusedKey)) + (slice.strings ++ [(unusedKey, constructorBytes)]) slice.blobs) + (.heapNumbering stringsHeap unusedKey) = true := by + decide +kernel +#print axioms rejects_two_row_keys_for_one_offset + +/-- An entry under a key no row cell holds. -/ +theorem rejects_a_slice_entry_no_row_names : + failsWith + (rowsChecked fixture slice.rows (slice.strings ++ [(unusedKey, [65, 0])]) slice.blobs) + (.heapNumbering stringsHeap unusedKey) = true := by + decide +kernel +#print axioms rejects_a_slice_entry_no_row_names + +/-- The key of `Fat` without the entry it ranks. -/ +theorem rejects_a_row_key_without_its_entry : + failsWith + (rowsChecked fixture slice.rows (slice.strings.filter (·.1 != fatKey)) slice.blobs) + (.heapNumbering stringsHeap fatKey) = true := by + decide +kernel +#print axioms rejects_a_row_key_without_its_entry + +/-- The name of `Tiny` carried under the omitted-entry marker, in its row cell and as its entry: + no numbering gives the marker, so the entry is one no row cell holds. -/ +theorem rejects_an_entry_at_the_marker : + failsWith + (rowsChecked fixture (withRow tinyToken (writeLittle · methodNameAt 2 0xFFFF)) + (slice.strings.filter (·.1 != tinyKey) ++ [(0xFFFF, [84, 105, 110, 121, 0])]) slice.blobs) + (.heapNumbering stringsHeap 0xFFFF) = true := by + decide +kernel +#print axioms rejects_an_entry_at_the_marker + +/-- The first two `#Blob` entries, keys 0 and 1, carried in descending order. -/ +theorem rejects_entries_out_of_order : + failsWith + (rowsChecked fixture slice.rows slice.strings + ((slice.blobs.take 2).reverse ++ slice.blobs.drop 2)) + (.heapNumbering blobHeap 0) = true := by + decide +kernel +#print axioms rejects_entries_out_of_order + +/-- The signature `int (int)` with its last byte changed, and the name `Fat` with a byte after + its terminator. -/ +theorem rejects_a_changed_slice_entry : + failsWith + (rowsChecked fixture slice.rows slice.strings (slice.blobs.set 3 (3, [3, 32, 0, 9]))) + (.heapMismatch blobHeap 3) = true ∧ + failsWith + (rowsChecked fixture slice.rows (slice.strings.set fatKey (fatKey, [70, 97, 116, 0, 0])) + slice.blobs) + (.heapMismatch stringsHeap fatKey) = true := by + decide +kernel +#print axioms rejects_a_changed_slice_entry + +/-- Where `Tiny` names its own entry with the marker, the entry is one no row cell holds. -/ +theorem rejects_the_marker_for_a_carried_entry : + failsWith + (rowsChecked fixture (withRow tinyToken (writeLittle · methodNameAt 2 0xFFFF)) + slice.strings slice.blobs) + (.heapNumbering stringsHeap tinyKey) = true := by + decide +kernel +#print axioms rejects_the_marker_for_a_carried_entry + +/-- The marker in one `.ctor` row while the other rows name `.ctor` by its key: the offset of + `.ctor` would be both numbered and omitted. -/ +theorem rejects_a_marker_beside_a_key : + failsWith + (rowsChecked fixture (withRow constructorToken (writeLittle · methodNameAt 2 0xFFFF)) + slice.strings slice.blobs) + (.heapNumbering stringsHeap 1) = true := by + decide +kernel +#print axioms rejects_a_marker_beside_a_key + +end Tests.StrictBinding.Rows diff --git a/Tests/StrictBinding/Slice.lean b/Tests/StrictBinding/Slice.lean new file mode 100644 index 000000000..ecb7a4db4 --- /dev/null +++ b/Tests/StrictBinding/Slice.lean @@ -0,0 +1,154 @@ +import StrictBinding.Slice +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +/-! Whole metadata checks and user-string offsets, payloads, prefixes and heap bounds. -/ + +namespace Tests.StrictBinding.Slice + +open _root_.StrictBinding +open CIL.Extraction (ByteEntry MetadataSlice userStringHeap) + +/-- The fixture with the empty user string at heap offset 1. -/ +private def fixture : Except AuditError PreparedImage := + prepareOne + { identity := StrictFixture.identity, image := flipByte StrictFixture.image 2021 1 } + +/-- The fixture's carried rows and independently numbered Assembly block, with its user string. -/ +private def carried : MetadataSlice := + { StrictFixture.slice with userStrings := [(1, [1, 0])] } + +/-- The complete check on the fixture. -/ +private def checked (slice : MetadataSlice) : Except AuditError Unit := + fixture >>= fun image => sliceBound image slice + +/-- The complete carried metadata, including a one-byte length prefix, passes. -/ +theorem accepts_fixture : ok (checked carried) = true := by decide +kernel +#print axioms accepts_fixture + +/-- A changed user-string byte is refused by the complete check. -/ +theorem rejects_changed_byte : + failsWith (checked { carried with userStrings := [(1, [1, 1])] }) + (.heapMismatch userStringHeap 1) = true := by decide +kernel +#print axioms rejects_changed_byte + +/-- A payload shorter than its image length prefix is refused. -/ +theorem rejects_truncated_payload : + failsWith (checked { carried with userStrings := [(1, [1])] }) + (.heapMismatch userStringHeap 1) = true := by decide +kernel +#print axioms rejects_truncated_payload + +/-- An extra byte after the exact payload is refused. -/ +theorem rejects_extra_byte : + failsWith (checked { carried with userStrings := [(1, [1, 0, 0])] }) + (.heapMismatch userStringHeap 1) = true := by decide +kernel +#print axioms rejects_extra_byte + +/-- A key names an image offset, so the same bytes at another offset are refused. -/ +theorem rejects_wrong_offset : + failsWith (checked { carried with userStrings := [(2, [1, 0])] }) + (.heapMismatch userStringHeap 2) = true := by decide +kernel +#print axioms rejects_wrong_offset + +/-- A later user string is checked even when the first entry matches. -/ +theorem rejects_false_second_entry : + failsWith (checked { carried with userStrings := [(1, [1, 0]), (2, [1, 1])] }) + (.heapMismatch userStringHeap 2) = true := by decide +kernel +#print axioms rejects_false_second_entry + +/-- A prepared view with an explicit user-string heap, for prefix and boundary tests. -/ +private def heapImage (bytes : List Nat) (size : Nat) : Except AuditError PreparedImage := do + let image <- fixture + let view <- ImageView.ofImage (ImageBytes.ofBytes bytes) + pure + { image with + view := view + layout := + { image.layout with + pe := { image.layout.pe with userStrings := { start := 0, size := size } } } } + +/-- User-string checks over a bounded heap inside a larger view. -/ +private def checkedHeap (bytes : List Nat) (size : Nat) (entries : List ByteEntry) := + heapImage bytes size >>= fun image => userStringsBound image entries + +/-- Equal prefixes at different offsets still require equal payload bytes. -/ +theorem rejects_wrong_offset_with_same_length : + failsWith (checkedHeap [0, 1, 65, 1, 66] 5 [(3, [1, 65])]) + (.heapMismatch userStringHeap 3) = true := by decide +kernel +#print axioms rejects_wrong_offset_with_same_length + +/-- Bytes inside the image but starting beyond the heap cannot supply an entry. -/ +theorem rejects_offset_past_heap_end : + failsWith (checkedHeap [0, 1, 0, 1, 0] 2 [(3, [1, 0])]) + (.heapMismatch userStringHeap 3) = true := by decide +kernel +#print axioms rejects_offset_past_heap_end + +/-- Matching bytes beyond the declared heap end cannot extend an entry. -/ +theorem rejects_crossing_heap_end : + failsWith (checkedHeap [0, 3, 65, 0, 0] 4 [(1, [3, 65, 0, 0])]) + (.heapMismatch userStringHeap 1) = true := by decide +kernel +#print axioms rejects_crossing_heap_end + +/-- A two-byte prefix whose second byte is outside the heap is refused. -/ +theorem rejects_prefix_at_heap_end : + failsWith (checkedHeap [0, 128, 0] 2 [(1, [128, 0])]) + (.heapMismatch userStringHeap 1) = true := by decide +kernel +#print axioms rejects_prefix_at_heap_end + +/-- A length-prefixed UTF-16 user string containing repeated `A` characters. -/ +private def textEntry (lengthPrefix : List Nat) (characters : Nat) : List Nat := + lengthPrefix ++ (List.replicate characters [65, 0]).flatten ++ [0] + +/-- Sixty-four UTF-16 characters require a two-byte prefix for the 129-byte payload. -/ +private def mediumEntry : List Nat := textEntry [128, 129] 64 + +/-- 8192 UTF-16 characters require a four-byte prefix for the 16385-byte payload. -/ +private def longEntry : List Nat := textEntry [192, 0, 64, 1] 8192 + +/-- The two-byte length prefix and every payload byte match. -/ +theorem accepts_two_byte_prefix : + ok (checkedHeap (0 :: mediumEntry) (mediumEntry.length + 1) [(1, mediumEntry)]) = true := by + decide +kernel +#print axioms accepts_two_byte_prefix + +/-- The four-byte length prefix and every payload byte match. -/ +theorem accepts_four_byte_prefix : + ok (checkedHeap (0 :: longEntry) (longEntry.length + 1) [(1, longEntry)]) = true := by + decide +kernel +#print axioms accepts_four_byte_prefix + +/-- A four-byte prefix requires its entire declared payload. -/ +theorem rejects_truncated_four_byte_payload : + failsWith (checkedHeap (0 :: longEntry) (longEntry.length + 1) [(1, longEntry.dropLast)]) + (.heapMismatch userStringHeap 1) = true := by decide +kernel +#print axioms rejects_truncated_four_byte_payload + +/-- The claim also accepts an interior offset whose bytes form a length-prefixed payload. -/ +theorem accepts_interior_offset : + ok (checkedHeap [0, 3, 1, 0, 0] 5 [(2, [1, 0])]) = true := by decide +kernel +#print axioms accepts_interior_offset + +/-- A second image differs in the Assembly row's major version. -/ +private def otherImage : Except AuditError PreparedImage := + prepareOne + { identity := "StrictFixture, Version=0.2.3.4, Culture=neutral, PublicKeyToken=null", + image := flipByte StrictFixture.image 1420 1 } + +/-- The Assembly block copied from that second image, with its own heap numbering. -/ +private def otherAssembly : CIL.Extraction.AssemblyMetadata := + { StrictFixture.assemblyBlock with + identity := "StrictFixture, Version=0.2.3.4, Culture=neutral, PublicKeyToken=null", + assemblyRow := flipAt StrictFixture.assemblyBlock.assemblyRow 4 1 } + +/-- The second block belongs to its own image. -/ +theorem accepts_other_assembly : + ok (otherImage >>= fun image => assemblyBound image otherAssembly) = true := by decide +kernel +#print axioms accepts_other_assembly + +/-- A block from the second image is refused even though every carried method row is unchanged. -/ +theorem rejects_other_assembly : + failsWith (checked { carried with assembly := otherAssembly }) + (.rowMismatch assemblyToken) = true := by decide +kernel +#print axioms rejects_other_assembly + +end Tests.StrictBinding.Slice diff --git a/Tests/StrictBinding/StaticData.lean b/Tests/StrictBinding/StaticData.lean new file mode 100644 index 000000000..3bcfb61c2 --- /dev/null +++ b/Tests/StrictBinding/StaticData.lean @@ -0,0 +1,51 @@ +import StrictBinding.StaticData +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +/-! The fixture's eight static bytes belong to Field row 6. -/ +namespace Tests.StrictBinding.StaticData +open _root_.StrictBinding + +private def field := tokenOf CIL.Extraction.Table.field 6 +private def bytes : List Nat := [1, 2, 3, 4, 5, 6, 7, 8] + +private def check (raw : ImageBytes) (expected : List Nat := bytes) (token := field) : + Except AuditError Unit := do + let image ← prepareOne ⟨StrictFixture.identity, raw⟩ + let rows ← fieldRvaIndex image + let some row := rows[token]? | throw (.invalidFieldData token) + fieldDataAt image token row 1 expected + +theorem static_bytes_match : ok (check StrictFixture.image) = true := by decide +kernel +#print axioms static_bytes_match + +/-- The FieldRVA of the compiled Table property is 10064 in the section starting at RVA 8192 + and file offset 512. Changing its last byte preserves the PE and every metadata row. -/ +theorem rejects_changed_image_data : + failsWith (check (flipByte StrictFixture.image (512 + 10064 - 8192 + 7) 1)) + (.fieldDataMismatch field 10064 2384) = true := by decide +kernel +#print axioms rejects_changed_image_data + +theorem rejects_changed_descriptor : + failsWith (check StrictFixture.image [1, 2, 3, 4, 5, 6, 7, 0]) + (.fieldDataMismatch field 10064 2384) = true := by decide +kernel +#print axioms rejects_changed_descriptor + +theorem rejects_another_field : + failsWith (check StrictFixture.image bytes (tokenOf 4 5)) (.invalidFieldData (tokenOf 4 5)) = true := by + decide +kernel +#print axioms rejects_another_field + +/-- Moving the RVA back eight bytes keeps the entire range inside the same section. -/ +theorem rejects_another_in_section_position : + failsWith (check (flipByte StrictFixture.image 1410 24)) + (.fieldDataMismatch field 10056 2376) = true := by decide +kernel +#print axioms rejects_another_in_section_position + +/-- Changing the row's Field index from 6 to the absent row 7 is rejected before reading data. -/ +theorem rejects_out_of_range_field : + failsWith (check (flipByte StrictFixture.image 1414 1)) + (.invalidDataTableKey fieldRvaTable 1 7) = true := by decide +kernel +#print axioms rejects_out_of_range_field + +end Tests.StrictBinding.StaticData diff --git a/Tests/StrictBinding/StaticSiteCases.lean b/Tests/StrictBinding/StaticSiteCases.lean new file mode 100644 index 000000000..f79a8c0b8 --- /dev/null +++ b/Tests/StrictBinding/StaticSiteCases.lean @@ -0,0 +1,38 @@ +import Extracted +import RvaImage +import StrictBinding.StaticData + +/-! Check changes to the static descriptor of the compiled table against its original image. -/ +namespace StaticSiteCases +open CIL.Extraction StrictBinding + +private def images : List PinnedImage := [⟨Extracted.assembly0.identity, RvaImage.image⟩] + +private def change (f : StaticFieldSite → StaticFieldSite) : List MethodEvidence := + Extracted.methodEvidence.map fun item => + let sites := item.closed.staticFields.map fun (pc, site) => + (pc, if site.rvaSize.isSome then f site else site) + { item with closed := { item.closed with staticFields := sites } } + +theorem compiled_table_agrees : StaticDataBound images Extracted.methodEvidence := by decide +kernel +#print axioms compiled_table_agrees + +/-- The first byte agrees with the physical storage. Its ClassLayout still requires 32 bytes. -/ +theorem rejects_matching_prefix : (match checkStaticData images (change fun site => + { site with rvaSize := some 1, descriptor.bytes := site.descriptor.bytes.take 1 }) with + | .error (.invalidFieldData _) => true | _ => false) = true := by decide +kernel +#print axioms rejects_matching_prefix + +/-- Swapping the bytes keeps the recorded size, alignment, token and mapped address. -/ +theorem rejects_wrong_bytes : (match checkStaticData images (change fun site => + { site with descriptor.bytes := site.descriptor.bytes.reverse }) with + | .error (.fieldDataMismatch _ _ _) => true | _ => false) = true := by decide +kernel +#print axioms rejects_wrong_bytes + +/-- Removing the RVA marker would classify the same physical field as constructor-initialized. -/ +theorem rejects_ordinary_field_claim : (match checkStaticData images (change fun site => + { site with rvaSize := none }) with + | .error (.invalidFieldData _) => true | _ => false) = true := by decide +kernel +#print axioms rejects_ordinary_field_claim + +end StaticSiteCases diff --git a/Tests/StrictBinding/TypeDefs.lean b/Tests/StrictBinding/TypeDefs.lean new file mode 100644 index 000000000..5954f0025 --- /dev/null +++ b/Tests/StrictBinding/TypeDefs.lean @@ -0,0 +1,264 @@ +import StrictBinding.TypeDefs +import Tests.StrictBinding.Fixture +import Tests.StrictBinding.Mutation + +/-! +# Strict binding tests: type definitions + +Every TypeDef row of the fixture, as System.Reflection.Metadata reads it, must be accepted at its +row: an enum, a struct, classes that extend `System.Object` through a TypeRef, classes that extend +another TypeDef, and a type whose fields run to the end of the Field table. Each rejection then +changes one part of a claim that passes, or one byte of the file, and asserts the exact error: +the name, the namespace, a nesting the row does not have, the kind, the Field rows and the base +type. +-/ + +namespace Tests.StrictBinding.TypeDefs + +open _root_.StrictBinding + +/-- The fixture image pinned under its own identity. -/ +private def pinned : List PinnedImage := + [{ identity := StrictFixture.identity, image := StrictFixture.image }] + +/-- The namespace of the fixture's own types. -/ +private def fixtureNamespace : List Nat := asciiOf "StrictFixture" + +/-- A TypeRef to `System` type `name`, as the fixture's TypeRef row `row` is. -/ +private def systemBase (row : Nat) (name : String) : BaseType := + .named (tokenOf 0x01 row) systemNamespace (asciiOf name) + +/-- A base type naming TypeDef row `row` of the fixture, in its namespace, with `name`. -/ +private def fixtureBase (row : Nat) (name : String) : BaseType := + .named (tokenOf 0x02 row) fixtureNamespace (asciiOf name) + +/-- A claim about TypeDef row `row` of the fixture. -/ +private def claim (row : Nat) (typeNamespace : List Nat) (typeName : String) (kind : TypeKind) + (fields : Nat × Nat) (base : BaseType) : TypeDefClaim := + { identity := StrictFixture.identity, row, typeNamespace, typeName := asciiOf typeName, + nesting := .topLevel 1, kind, fields, base } + +/-- The class `Shape`, which Triangle and Square extend. -/ +private def shape : TypeDefClaim := + claim 5 fixtureNamespace "Shape" .classType (6, 0) (systemBase 9 "Object") + +/-- The class `Triangle`, which extends the TypeDef row of `Shape`. -/ +private def triangle : TypeDefClaim := + claim 6 fixtureNamespace "Triangle" .classType (6, 0) (fixtureBase 5 "Shape") + +/-- The byte enum `Small`, which owns its `value__` field and two constants. -/ +private def small : TypeDefClaim := + claim 2 fixtureNamespace "Small" .enum (1, 3) (systemBase 7 "Enum") + +/-- The explicit-layout struct `Overlay`. -/ +private def overlay : TypeDefClaim := + claim 3 fixtureNamespace "Overlay" .valueType (4, 2) (systemBase 8 "ValueType") + +/-- Every TypeDef row of the fixture, in row order. -/ +private def claims : List TypeDefClaim := + [claim 1 [] "" .classType (1, 0) .none, small, overlay, + claim 4 fixtureNamespace "Arithmetic" .classType (6, 0) (systemBase 9 "Object"), shape, + triangle, { triangle with row := 7, typeName := asciiOf "Square" }, + claim 8 [] "" .classType (6, 1) (systemBase 9 "Object")] + +theorem every_fixture_type_definition_is_found_at_its_row : TypeDefsBound pinned claims := by + decide +kernel +#print axioms every_fixture_type_definition_is_found_at_its_row + +/-- The file offset of a row, from the reference reader's table position and row size. -/ +private def rowStart (table row : Nat) : Nat := + (StrictFixture.expectedFacts.filterMap fun fact => + match fact with + | .table number _ size offset => + if number == table then some (offset + (row - 1) * size) else none + | _ => none).headD 0 + +/-- The result of checking one claim against the fixture. -/ +private def check (claim : TypeDefClaim) : Except AuditError Unit := + checkAll typeDefClaimBound pinned [claim] + +theorem rejects_another_name : + failsWith (check { shape with typeName := asciiOf "Shapes" }) + (.typeDefMismatch 5 .typeName) = true := by decide +kernel +#print axioms rejects_another_name + +theorem rejects_another_namespace : + failsWith (check { shape with typeNamespace := asciiOf "StrictFixtures" }) + (.typeDefMismatch 5 .typeNamespace) = true := by decide +kernel +#print axioms rejects_another_namespace + +/-- A top-level row has a top-level visibility, so it is not nested in any row. -/ +theorem rejects_a_nesting_the_row_does_not_have : + failsWith (check { shape with nesting := .nested 1 4 }) + (.typeDefMismatch 5 .nesting) = true := by decide +kernel +#print axioms rejects_a_nesting_the_row_does_not_have + +/-- The fixture has no NestedClass rows, so the only bracket is the empty table. -/ +theorem rejects_a_bracket_past_the_table : + failsWith (check { shape with nesting := .topLevel 2 }) + (.typeDefMismatch 5 .nesting) = true := by decide +kernel +#print axioms rejects_a_bracket_past_the_table + +/-- An empty NestedClass table is sorted by any key. -/ +theorem accepts_the_empty_nested_class_table : + TablesSorted pinned + [{ identity := StrictFixture.identity, table := 0x29, key := nestedClassKey }] := by + decide +kernel +#print axioms accepts_the_empty_nested_class_table + +theorem rejects_an_enum_claimed_as_a_struct : + failsWith (check { small with kind := .valueType }) (.typeDefMismatch 2 .kind) = true := by + decide +kernel +#print axioms rejects_an_enum_claimed_as_a_struct + +/-- The TypeRef still spells System.Enum when its ResolutionScope changes from the first + AssemblyRef (6) to the first Module (4). That scope cannot certify an enum. -/ +theorem rejects_an_enum_base_in_another_scope : + failsWith (checkAll typeDefClaimBound + [{ identity := StrictFixture.identity, + image := flipByte StrictFixture.image (rowStart 0x01 7) 2 }] [small]) + (.typeDefMismatch 2 .kind) = true := by decide +kernel +#print axioms rejects_an_enum_base_in_another_scope + +/-- Flip one byte of the token in the first AssemblyRef's public-key-or-token blob. -/ +private def changedPublisher : Except AuditError ImageBytes := do + let view ← ImageView.ofImage StrictFixture.image + let layout ← decodeMetadata view + let key ← layout.cell view (tokenOf 0x23 1) 5 + let start := layout.pe.blobs.start + key + let (lengthBytes, _) ← blobPrefix view start + return flipByte StrictFixture.image (start + lengthBytes) 1 + +/-- The enum name and scope row survive a publisher substitution in the physical image. -/ +theorem rejects_an_enum_base_from_another_publisher : + failsWith (changedPublisher >>= fun image => + checkAll typeDefClaimBound [{ identity := StrictFixture.identity, image }] [small]) + (.typeDefMismatch 2 .kind) = true := by decide +kernel +#print axioms rejects_an_enum_base_from_another_publisher + +theorem rejects_a_struct_claimed_as_a_class : + failsWith (check { overlay with kind := .classType }) (.typeDefMismatch 3 .kind) = true := by + decide +kernel +#print axioms rejects_a_struct_claimed_as_a_class + +theorem rejects_a_class_claimed_as_a_struct : + failsWith (check { shape with kind := .valueType }) (.typeDefMismatch 5 .kind) = true := by + decide +kernel +#print axioms rejects_a_class_claimed_as_a_struct + +/-- With the interface bit set in its flags, the row of `Shape` defines an interface. -/ +theorem rejects_a_class_whose_row_is_an_interface : + failsWith (checkAll typeDefClaimBound + [{ identity := StrictFixture.identity, + image := flipByte StrictFixture.image (rowStart 0x02 5) interfaceFlag }] [shape]) + (.typeDefMismatch 5 .kind) = true := by decide +kernel +#print axioms rejects_a_class_whose_row_is_an_interface + +theorem rejects_one_field_fewer : + failsWith (check { overlay with fields := (4, 1) }) (.typeDefMismatch 3 .fields) = true := by + decide +kernel +#print axioms rejects_one_field_fewer + +theorem rejects_a_shifted_field_list : + failsWith (check { overlay with fields := (5, 2) }) (.typeDefMismatch 3 .fields) = true := by + decide +kernel +#print axioms rejects_a_shifted_field_list + +theorem rejects_the_last_row_owning_no_field : + failsWith (check (claim 8 [] "" .classType (6, 0) + (systemBase 9 "Object"))) (.typeDefMismatch 8 .fields) = true := by decide +kernel +#print axioms rejects_the_last_row_owning_no_field + +theorem rejects_another_base_row : + failsWith (check { triangle with base := fixtureBase 4 "Shape" }) + (.typeDefMismatch 6 .base) = true := by decide +kernel +#print axioms rejects_another_base_row + +theorem rejects_another_base_name : + failsWith (check { triangle with base := fixtureBase 5 "Square" }) + (.typeDefMismatch 6 .base) = true := by decide +kernel +#print axioms rejects_another_base_name + +theorem rejects_no_base_for_a_class : + failsWith (check { shape with base := .none }) (.typeDefMismatch 5 .base) = true := by + decide +kernel +#print axioms rejects_no_base_for_a_class + +theorem rejects_a_generic_base_for_a_class : + failsWith (check { shape with base := .spec (tokenOf 0x1B 1) }) + (.typeDefMismatch 5 .base) = true := by decide +kernel +#print axioms rejects_a_generic_base_for_a_class + +theorem rejects_a_row_past_the_table : + failsWith (check { shape with row := 9 }) (.tokenOutOfScope (tokenOf 0x02 9)) = true := by + decide +kernel +#print axioms rejects_a_row_past_the_table + +/-- An oversized claimed row fails before the auditor reads the TypeDef flags. -/ +theorem rejects_a_row_above_24_bits : + failsWith (check { shape with row := 0x1000001 }) (.rowOverflow 0x02 0x1000001) := by + decide +kernel +#print axioms rejects_a_row_above_24_bits + +/-- The fixture uses two-byte heap and table indexes. The base, field list and method list + follow the four flag bytes and the two name indexes in each TypeDef row. -/ +private def checkChanged (claim : TypeDefClaim) (row columnOffset before after : Nat) := + let image := flipByte StrictFixture.image (rowStart 0x02 row + columnOffset) (before ^^^ after) + checkAll typeDefClaimBound [{ identity := StrictFixture.identity, image }] [claim] + +/-- A matching base specification can name the fixture's existing TypeSpec row. -/ +theorem accepts_an_existing_base_specification : + (checkChanged { shape with base := .spec (tokenOf 0x1B 1) } 5 8 37 6).isOk := by + decide +kernel +#print axioms accepts_an_existing_base_specification + +/-- A matching coded index still has to select an existing TypeSpec row. The fixture owns one. -/ +theorem rejects_a_missing_base_specification : + failsWith (checkChanged { shape with base := .spec (tokenOf 0x1B 2) } 5 8 37 10) + (.tokenOutOfScope (tokenOf 0x1B 2)) := by decide +kernel +#print axioms rejects_a_missing_base_specification + +/-- A nil TypeSpec token cannot designate a generic base. -/ +theorem rejects_a_nil_base_specification : + failsWith (checkChanged { shape with base := .spec (tokenOf 0x1B 0) } 5 8 37 2) + (.tokenOutOfScope (tokenOf 0x1B 0)) := by decide +kernel +#print axioms rejects_a_nil_base_specification + +/-- The range cannot start at row zero, even if the claim copies the malformed range. -/ +theorem rejects_a_nil_field_list : + failsWith (checkChanged { shape with fields := (0, 6) } 5 10 6 0) + (.typeDefMismatch 5 .fields) := by decide +kernel +#print axioms rejects_a_nil_field_list + +/-- The next type cannot extend its predecessor's range past the fixture's six Field rows. -/ +theorem rejects_fields_past_the_table : + failsWith (checkChanged { shape with fields := (6, 3) } 6 10 6 9) + (.typeDefMismatch 5 .fields) := by decide +kernel +#print axioms rejects_fields_past_the_table + +/-- Put one NestedClass row in the fixture's final table slot. Its AssemblyRef row starts with + major version 11 and minor version 0. Triangle (row 6) and its base Shape are both local. -/ +private def checkEnclosing (enclosing : Nat) : Except AuditError Unit := + let tables := StrictFixture.expectedFacts.findSome? fun fact => + match fact with + | .stream "tables" offset _ => some offset + | _ => none + let validMask := tables.getD 0 + 8 + let lastTable := rowStart 0x23 1 + let image := flipBytes StrictFixture.image + [(validMask + 4, 0x08), (validMask + 5, 0x02), (rowStart 0x02 6, 1 ^^^ 2), + (lastTable, 11 ^^^ 6), (lastTable + 2, enclosing)] + checkAll typeDefClaimBound [{ identity := StrictFixture.identity, image }] + [{ triangle with nesting := .nested 1 enclosing }] + +/-- The checked row can nest Triangle inside Shape. -/ +theorem accepts_an_earlier_enclosing_type : (checkEnclosing 5).isOk := by decide +kernel +#print axioms accepts_an_earlier_enclosing_type + +/-- Even when the claim repeats the row's bytes, its owner must exist and precede Triangle. -/ +theorem rejects_an_invalid_enclosing_type : + [0, 6, 7, 9].all (fun enclosing => + failsWith (checkEnclosing enclosing) (.typeDefMismatch 6 .nesting)) := by decide +kernel +#print axioms rejects_an_invalid_enclosing_type + +end Tests.StrictBinding.TypeDefs diff --git a/Tests/StrictBindingDecoding.cs b/Tests/StrictBindingDecoding.cs new file mode 100644 index 000000000..d742406bf --- /dev/null +++ b/Tests/StrictBindingDecoding.cs @@ -0,0 +1,324 @@ +using System.Collections; +using System.Collections.Immutable; +using System.Reflection; +using System.Reflection.Emit; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Runtime.InteropServices; +using System.Text; +using System.Text.RegularExpressions; +using MethodDefinition = Mono.Cecil.MethodDefinition; +using ModuleDefinition = Mono.Cecil.ModuleDefinition; + +partial class Program +{ + /// The checked-in Lean module that holds the fixture image. + static string Fixture => At("Tests/StrictBinding/Fixture.lean"); + + /// + /// The container, table positions, heaps, method body offsets and identity that Lean decodes from an + /// image, compared line by line with System.Reflection.Metadata on the same bytes: Examples.dll, the + /// checked-in fixture and the platform reference assembly System.Runtime. + /// + static void CheckBindingDecoding(string root) + { + // The checked-in module is what its own image generates, so its expected facts are the reader's. + var fixtureText = File.ReadAllText(Fixture); + var fixtureImage = StrictBindingFacts.ReadImage(fixtureText); + if (StrictBindingFacts.Module(fixtureImage) != fixtureText.ReplaceLineEndings("\n")) + throw new InvalidOperationException(Fixture + " is not what its image generates; run " + + "`dotnet run --file Tests/StrictBindingFixture.cs -- --update`."); + RequireBoundedDigestLemmas(); + (string Name, byte[] Bytes)[] images = + [ + ("Examples.dll", File.ReadAllBytes(At("Examples/Assembly/bin/Examples.dll"))), + ("the strict binding fixture", fixtureImage), + ("the System.Runtime reference assembly", File.ReadAllBytes(PlatformReference())) + ]; + var package = FixturePackage(); + Run(["lake", "build", "StrictBinding"], package, progress: "Building the strict binding library"); + Directory.CreateDirectory(root); + RequireLargeDigestToElaborate(root, package); + for (var index = 0; index < images.Length; index++) + { + var (name, bytes) = images[index]; + var path = Path.Combine(root, $"Describe{index}.lean"); + File.WriteAllText(path, DescribeModule(bytes)); + var (code, output, stdout, _) = Execute(["lake", "env", "lean", "--run", path], cwd: package); + if (code != 0) throw new InvalidOperationException($"Lean could not describe {name}: {output}"); + string[] lean = stdout.ReplaceLineEndings("\n").Split('\n', StringSplitOptions.RemoveEmptyEntries); + string[] expected = [.. StrictBindingFacts.Read(bytes).Select(fact => fact.Line())]; + var mismatch = Enumerable.Range(0, Math.Min(lean.Length, expected.Length)) + .FirstOrDefault(line => lean[line] != expected[line], -1); + if (mismatch >= 0 || lean.Length != expected.Length) + throw new InvalidOperationException($"Lean and System.Reflection.Metadata disagree on {name}: " + + $"{lean.Length} and {expected.Length} lines" + + (mismatch < 0 ? "" : $"; line {mismatch + 1} is '{lean[mismatch]}' and '{expected[mismatch]}'")); + Console.WriteLine($"PASS Lean decodes {name} as System.Reflection.Metadata does: {lean.Length} facts"); + } + } + + /// + /// Kernel memory for hashing is bounded by the block count of one lemma, so a generated digest proof states one + /// lemma per page of 64 chunks, however large the file, and one evaluation of a whole file is refused. + /// + static void RequireBoundedDigestLemmas() + { + var file = new byte[3 * 4096 + 100]; + new Random(7).NextBytes(file); + var proof = BindingImage.PagedDigestTheorem(file, skipCompileCheck: true); + var definitions = BindingImage.Definitions(file); + var lemmas = HashLemma.Count(proof); + var longest = PageChunkCount.Matches(definitions).Max(page => int.Parse(page.Groups[1].Value, + System.Globalization.CultureInfo.InvariantCulture)); + if (lemmas != 4 || longest > 64) + throw new InvalidOperationException( + $"Digest proof is not one lemma per page: {lemmas} lemmas, {longest} chunks"); + try { BindingImage.SingleDigestTheorem(file); } + catch (ArgumentException) + { + Console.WriteLine("PASS digest proofs state one lemma per page of 64 chunks"); + return; + } + throw new InvalidOperationException("Whole-file kernel hashing was accepted"); + } + + /// + /// A digest proof of a file of 210 pages, about the size of a platform reference assembly, elaborates: the + /// chain of page lemmas has no term nested more deeply than one level. Only elaboration runs, since replay + /// costs minutes. + /// + static void RequireLargeDigestToElaborate(string root, string package) + { + var file = new byte[210 * 4096 - 17]; + new Random(11).NextBytes(file); + var path = Path.Combine(root, "LargeDigest.lean"); + File.WriteAllText(path, "import StrictBinding.Sha256\nimport CIL.Extraction.FixedDigits\n\n" + + "namespace Large\n\n" + BindingImage.Definitions(file) + "\n" + + BindingImage.PagedDigestTheorem(file, skipCompileCheck: true) + "end Large\n"); + var run = Execute(["lake", "env", "lean", path], cwd: package); + if (run.Code != 0) + throw new InvalidOperationException("A digest of 210 pages did not elaborate: " + run.Output); + Console.WriteLine("PASS a digest proof of 210 pages elaborates"); + } + + /// + /// The slices the extractor carries for every method with a body in each selected assembly, with the operand + /// and local signature tokens of the body, hold the images' rows and heap entries, including user strings. + /// Lean checks each complete slice in the image its identity pins. Together they carry rows of every table + /// a slice may carry and at least one omitted-entry marker, so neither is left unchecked. + /// + static void CheckExtractedSlices(string root, string package, + params (string Selected, string[] References)[] extractions) + { + const BindingFlags Internal = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; + var extractor = Assembly.LoadFrom(ExtractorPath); + var pinnedType = extractor.GetType("Nethermind.LeanCIL.PinnedAssemblies", throwOnError: true)!; + var sourceType = pinnedType.GetNestedType("Source", BindingFlags.NonPublic | BindingFlags.Public)!; + var slicesType = extractor.GetType("Nethermind.LeanCIL.MetadataSlices", throwOnError: true)!; + var objectType = extractor.GetType("Nethermind.LeanCIL.ObjectTypes", throwOnError: true)! + .GetMethod("Is", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)!; + HashSet tables = []; + var markers = 0; + var userStrings = 0; + Directory.CreateDirectory(root); + for (var index = 0; index < extractions.Length; index++) + { + var (selected, references) = extractions[index]; + var sources = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(sourceType))!; + foreach (var reference in references) sources.Add(Activator.CreateInstance(sourceType, reference, null)!); + using var pinned = (IDisposable)Activator.CreateInstance(pinnedType, Internal, null, + [File.ReadAllBytes(selected), Sha(selected), sources, null], null)!; + var slices = Activator.CreateInstance(slicesType, pinned)!; + var modules = ((IEnumerable)pinnedType.GetProperty("Modules", Internal)! + .GetValue(pinned)!).ToArray(); + var original = (ModuleDefinition)pinnedType.GetMethod("Original", Internal)!.Invoke(pinned, [modules[0]])!; + var collect = slicesType.GetMethod("Method", Internal)!; + var block = slicesType.GetMethod("AssemblyBlock", Internal)!; + List methods = []; + foreach (var (token, tokens) in BodyTokens(File.ReadAllBytes(selected))) + { + var method = (MethodDefinition)original.LookupToken((int)token); + // As the extractor decides it for a method's evidence: an instance method of a class. + var classOwner = method.HasThis && (bool)objectType.Invoke(null, [method.DeclaringType])!; + var (metadata, _, _) = + ((string, string, int[]))collect.Invoke(slices, + [method, tokens, classOwner, Array.Empty()])!; + methods.Add($"(\"{method.FullName}\", {metadata})"); + } + string[] paths = [selected, .. references]; + var path = Path.Combine(root, $"Slices{index}.lean"); + File.WriteAllText(path, SlicesModule([.. paths.Select(File.ReadAllBytes)], + [.. modules.Select(module => (string)block.Invoke(slices, [module])!)], methods)); + var (code, output, stdout, _) = Execute(["lake", "env", "lean", "--run", path], cwd: package); + string[] lines = stdout.ReplaceLineEndings("\n").Split('\n', StringSplitOptions.RemoveEmptyEntries); + var refused = lines.Where(line => !line.StartsWith("ok ", StringComparison.Ordinal)).ToArray(); + if (code != 0 || refused.Length != 0 || lines.Length != methods.Count) + throw new InvalidOperationException($"Lean refused the slices of {Path.GetFileName(selected)}: " + + (refused.Length == 0 ? output : string.Join("\n", refused))); + var counts = lines.Select(line => line.Split(' ')).Select(words => + (Slices: int.Parse(words[1]), Rows: int.Parse(words[2]), Markers: int.Parse(words[3]), + Tables: words[4].Split(',', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse), + UserStrings: int.Parse(words[5]))).ToArray(); + if (counts.Sum(count => count.Rows) == 0) + throw new InvalidOperationException($"No slice of {Path.GetFileName(selected)} carries a row."); + tables.UnionWith(counts.SelectMany(count => count.Tables)); + markers += counts.Sum(count => count.Markers); + userStrings += counts.Sum(count => count.UserStrings); + Console.WriteLine($"PASS Lean matches the slices of {methods.Count} methods of " + + $"{Path.GetFileName(selected)} to their images: {counts.Sum(count => count.Slices)} slices, " + + $"{counts.Sum(count => count.Rows)} rows, {counts.Sum(count => count.Markers)} marker cells, " + + $"{counts.Sum(count => count.UserStrings)} user strings"); + } + var missing = CarriedTables.Except(tables).ToArray(); + if (missing.Length != 0 || markers == 0 || userStrings == 0) + throw new InvalidOperationException("The extracted slices leave unchecked: " + + string.Join(", ", missing.Select(table => $"table 0x{table:X2}")) + + (markers == 0 ? " omitted-entry markers" : "") + (userStrings == 0 ? " user strings" : "")); + Console.WriteLine($"PASS the extracted slices carry rows of all {CarriedTables.Length} tables and " + + $"{markers} marker cells and {userStrings} user strings"); + } + + /// The tables whose rows a metadata slice carries. + static readonly int[] CarriedTables = [0x01, 0x02, 0x04, 0x06, 0x0A, 0x11, 0x1B, 0x23, 0x27, 0x29, 0x2A, 0x2B]; + + /// + /// The MethodDef token of every method with a body, with the tokens its instructions name and its local + /// signature token, read from the IL bytes as the extractor reads them. + /// + static IEnumerable<(uint Method, uint[] Tokens)> BodyTokens(byte[] file) + { + var opcodes = typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static) + .Select(field => (OpCode)field.GetValue(null)!).ToDictionary(code => (ushort)code.Value); + using PEReader pe = new(ImmutableArray.Create(file)); + var reader = pe.GetMetadataReader(); + foreach (var handle in reader.MethodDefinitions) + { + var rva = reader.GetMethodDefinition(handle).RelativeVirtualAddress; + if (rva == 0) continue; + var body = pe.GetMethodBody(rva); + var il = body.GetILBytes()!; + List tokens = []; + for (var at = 0; at < il.Length;) + { + int value = il[at++]; + if (value == TwoByteOpcode) value = value << 8 | il[at++]; + var operand = opcodes[(ushort)value].OperandType; + if (operand is OperandType.InlineField or OperandType.InlineMethod or OperandType.InlineSig or + OperandType.InlineTok or OperandType.InlineType or OperandType.InlineString) + tokens.Add(BitConverter.ToUInt32(il, at)); + at += operand switch + { + OperandType.InlineNone => 0, + OperandType.ShortInlineBrTarget or OperandType.ShortInlineI or OperandType.ShortInlineVar => 1, + OperandType.InlineVar => 2, + OperandType.InlineI8 or OperandType.InlineR => 8, + OperandType.InlineSwitch => 4 + 4 * BitConverter.ToInt32(il, at), + _ => 4 + }; + } + if (!body.LocalSignature.IsNil) + tokens.Add((uint)MetadataTokens.GetToken(body.LocalSignature)); + yield return ((uint)MetadataTokens.GetToken(handle), [.. tokens]); + } + } + + // The first byte of a two-byte opcode. + const int TwoByteOpcode = 0xFE; + + /// + /// A program that checks each method's slices against the pinned images and prints, per method, `ok`, its + /// slice, row and marker cell counts, the tables of its rows and its name, or its name and the audit error. + /// + static string SlicesModule(byte[][] images, string[] blocks, List methods) + { + var text = new StringBuilder("import StrictBinding.Slice\n\n"); + for (var index = 0; index < images.Length; index++) + text.Append($"namespace Image{index}\n\n{BindingImage.Definitions(images[index])}\nend Image{index}\n\n"); + text.Append("open StrictBinding CIL.Extraction\n\ndef images : List PinnedImage :=\n ["); + text.Append(string.Join(",\n ", images.Select((image, index) => + $"{{ identity := \"{StrictBindingFacts.Identity(image)}\", image := Image{index}.image }}"))); + text.Append("]\n\n"); + for (var index = 0; index < blocks.Length; index++) + text.Append($"def assembly{index}Metadata : AssemblyMetadata := {blocks[index]}\n\n"); + text.Append("def methods : List (String × MethodMetadata) :=\n ["); + text.Append(string.Join(",\n ", methods)).Append("]\n\n"); + text.Append(""" + def checkSlice (prepared : List PreparedImage) (slice : MetadataSlice) : Except AuditError Unit := do + let image ← located prepared slice.assembly.identity + StrictBinding.sliceBound image slice + + def markerCells (prepared : List PreparedImage) (slice : MetadataSlice) : Nat := + match located prepared slice.assembly.identity with + | .error _ => 0 + | .ok image => (slice.rows.map fun row => + match image.columnsOf (row.1 / 2 ^ 24) with + | none => 0 + | some columns => + ((rowKeys stringsHeap columns row.2).filter (· == omittedKey (stringWidth image))).length + + ((rowKeys blobHeap columns row.2).filter (· == omittedKey (blobWidth image))).length).sum + + def main : IO UInt32 := do + match prepare images with + | .error error => + IO.eprintln (repr error) + pure 1 + | .ok prepared => + for (name, metadata) in methods do + let slices := metadata.own :: metadata.others + match slices.forM (checkSlice prepared) with + | .ok _ => + let rows := slices.flatMap (·.rows) + let tables := (rows.map (·.1 / 2 ^ 24)).eraseDups.map toString + let markers := (slices.map (markerCells prepared)).sum + let userStrings := (slices.map (fun slice => slice.userStrings.length)).sum + IO.println + (s!"ok {slices.length} {rows.length} {markers} {",".intercalate tables} " ++ + s!"{userStrings} {name}") + | .error error => IO.println s!"{name}: {repr error}" + pure 0 + + """); + return text.ToString(); + } + + /// A program that prints the description of an image the audit decodes, one fact per line. + static string DescribeModule(byte[] file) => $$""" + import StrictBinding.Dump + import CIL.Extraction.FixedDigits + + namespace StrictDescribe + + {{BindingImage.Definitions(file)}} + end StrictDescribe + + def main : IO UInt32 := do + match StrictBinding.describe StrictDescribe.image with + | .ok lines => + lines.forM IO.println + pure 0 + | .error error => + IO.eprintln (repr error) + pure 1 + """; + + /// The newest platform reference assembly System.Runtime of the .NET 11 reference pack. + static string PlatformReference() + { + var runtime = Path.GetFullPath(Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "..", "..", "..")); + var packs = Path.Combine(Environment.GetEnvironmentVariable("DOTNET_ROOT") ?? runtime, "packs", + "Microsoft.NETCore.App.Ref"); + return Directory.EnumerateDirectories(packs, "11.*") + .Select(directory => Path.Combine(directory, "ref", "net11.0", "System.Runtime.dll")).Where(File.Exists) + .OrderByDescending(path => Version.Parse(Path.GetFileName( + Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(path)))!))) + .FirstOrDefault() ?? throw new FileNotFoundException("No .NET 11 reference pack under " + packs); + } + + [GeneratedRegex(@"(?m)^def page\d+ : List Nat := CIL\.Extraction\.fixedDigitsWithin 512 (\d+) ")] + private static partial Regex PageChunkCount { get; } + + [GeneratedRegex(@"(?m)^set_option debug\.skipKernelTC true in\ntheorem hash\d+ ")] + private static partial Regex HashLemma { get; } +} diff --git a/Tests/StrictBindingFacts.cs b/Tests/StrictBindingFacts.cs new file mode 100644 index 000000000..ccbbb2eb9 --- /dev/null +++ b/Tests/StrictBindingFacts.cs @@ -0,0 +1,541 @@ +using System.Collections.Immutable; +using System.Numerics; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Text; +using System.Text.RegularExpressions; + +/// +/// What System.Reflection.Metadata reads from an assembly file, as the facts and lines that +/// StrictBinding.Dump decodes with Lean from the same bytes. The two readers are independent, so +/// a difference is a defect in the Lean decoding. +/// +static partial class StrictBindingFacts +{ + /// The highest metadata table number the Lean schema covers. + const int LastTable = 0x2C; + + /// The size from which a heap needs four-byte indexes. + const int WideHeap = 1 << 16; + + internal sealed record Fact(string Kind, string Text, long[] Numbers) + { + /// The line `StrictBinding.Fact.line` prints for the same fact. + internal string Line() => Kind == "identity" ? "identity " + Text + : Kind + " " + (Text.Length == 0 ? "" : Text + " ") + string.Join(' ', Numbers); + + /// The Lean constructor application for the fact. + internal string Lean() + { + var numbers = string.Join(' ', Numbers); + return Kind switch + { + "section" => $".sectionHeader {numbers}", + "stream" => $".stream \"{Text}\" {numbers}", + "identity" => $".identity\n (StrictBinding.asciiOf\n \"{Text}\")", + _ => $".{Kind} {numbers}" + }; + } + } + + /// A method with a body: its full name, its MethodDef token and the bytes of header and code. + internal sealed record Body(string Name, int Token, byte[] Bytes); + + /// The facts of an image in the order `StrictBinding.facts` lists them. + internal static List Read(byte[] file) + { + using PEReader pe = new(ImmutableArray.Create(file)); + var headers = pe.PEHeaders; + var reader = pe.GetMetadataReader(); + var metadata = (long)headers.MetadataStartOffset; + List facts = []; + facts.AddRange(headers.SectionHeaders.Select(section => new Fact("section", "", + [section.VirtualSize, section.VirtualAddress, section.SizeOfRawData, section.PointerToRawData]))); + facts.Add(new("metadata", "", [metadata, headers.MetadataSize])); + var streams = Streams(file, (int)metadata); + (string Name, string Header, HeapIndex? Heap)[] kinds = + [("tables", "#~", null), ("strings", "#Strings", HeapIndex.String), + ("userStrings", "#US", HeapIndex.UserString), ("guids", "#GUID", HeapIndex.Guid), + ("blobs", "#Blob", HeapIndex.Blob)]; + foreach (var (name, header, heap) in kinds) + { + if (!streams.TryGetValue(header, out var stream) || stream.Size == 0) continue; + facts.Add(new("stream", name, [stream.Start, stream.Size])); + if (heap is { } index) RequireSameHeap(reader, index, metadata, header, stream); + } + // Wide indexes follow the stream header sizes, which a heap-trimming reader cannot supply. + long Size(string header) => streams.TryGetValue(header, out var stream) ? stream.Size : 0; + facts.Add(new("heapSizes", "", [(Size("#Strings") >= WideHeap ? 1 : 0) | (Size("#GUID") >= WideHeap ? 2 : 0) | + (Size("#Blob") >= WideHeap ? 4 : 0)])); + for (var table = 0; table <= LastTable; table++) + { + var index = (TableIndex)table; + if (reader.GetTableRowCount(index) > 0) + facts.Add(new("table", "", [table, reader.GetTableRowCount(index), reader.GetTableRowSize(index), + metadata + reader.GetTableMetadataOffset(index)])); + } + foreach (var handle in reader.MethodDefinitions) + { + var rva = reader.GetMethodDefinition(handle).RelativeVirtualAddress; + if (rva != 0 && headers.TryGetDirectoryOffset(new DirectoryEntry(rva, 1), out var offset)) + facts.Add(new("method", "", [MetadataTokens.GetRowNumber(handle), rva, offset])); + else if (rva != 0) + throw new InvalidDataException($"System.Reflection.Metadata does not map RVA 0x{rva:x}"); + } + facts.Add(new("identity", reader.GetAssemblyDefinition().GetAssemblyName().FullName, [])); + return facts; + } + + /// + /// The stream headers of the metadata root, read directly: System.Reflection.Metadata does not expose + /// the table stream and reports a heap shorter than its header states when the heap ends in padding. + /// + static Dictionary Streams(byte[] file, int root) + { + var versionLength = BitConverter.ToInt32(file, root + 12); + var count = BitConverter.ToUInt16(file, root + 18 + versionLength); + var offset = root + 20 + versionLength; + Dictionary streams = []; + for (var stream = 0; stream < count; stream++) + { + var end = Array.IndexOf(file, (byte)0, offset + 8); + var name = Encoding.ASCII.GetString(file, offset + 8, end - offset - 8); + streams.Add(name, (root + BitConverter.ToInt32(file, offset), BitConverter.ToInt32(file, offset + 4))); + offset += 8 + (end - offset - 8 + 4) / 4 * 4; + } + return streams; + } + + /// + /// The reader and the stream header agree on where a heap starts and, up to padding, its length. + /// + static void RequireSameHeap(MetadataReader reader, HeapIndex heap, long metadata, string name, + (long Start, long Size) stream) + { + const int padding = 3; + var size = reader.GetHeapSize(heap); + if (metadata + reader.GetHeapMetadataOffset(heap) != stream.Start || size > stream.Size || + stream.Size - size > padding) + throw new InvalidDataException($"System.Reflection.Metadata disagrees with the header of {name}: " + + $"offset {metadata + reader.GetHeapMetadataOffset(heap)}, size {size}; header offset " + + $"{stream.Start}, size {stream.Size}"); + } + + /// Every method that has a body, with the bytes of its header and code. + internal static List Bodies(byte[] file) + { + using PEReader pe = new(ImmutableArray.Create(file)); + var reader = pe.GetMetadataReader(); + List bodies = []; + foreach (var handle in reader.MethodDefinitions) + { + var method = reader.GetMethodDefinition(handle); + if (method.RelativeVirtualAddress == 0) continue; + var type = reader.GetTypeDefinition(method.GetDeclaringType()); + var name = reader.GetString(type.Namespace) + "." + reader.GetString(type.Name) + "." + + reader.GetString(method.Name); + var size = pe.GetMethodBody(method.RelativeVirtualAddress).Size; + var bytes = pe.GetSectionData(method.RelativeVirtualAddress).GetContent(0, size).ToArray(); + bodies.Add(new(name, MetadataTokens.GetToken(handle), bytes)); + } + return bodies; + } + + /// + /// The Lean module for an image: its digest, the facts a reader found, its method bodies and the assembly block + /// an extraction carries for it. + /// + internal static string Module(byte[] file) + { + var text = new StringBuilder(""" + import StrictBinding.Dump + import StrictBinding.Sha256 + import CIL.Extraction.MetadataBytes + + /-! + # Strict binding fixture + + An SDK-built library with a tiny-header method, a fat-header method with locals, a byte + enum, an explicit-layout struct with static data and a class with a virtual method and two + derived classes. The bytes, the facts System.Reflection.Metadata reads from them, the + method bodies, the assembly block and a slice an extraction carries are generated by + `Tests/StrictBindingFixture.cs`; do not edit them. + -/ + + namespace StrictFixture + + /-- A 64-byte chunk written as its high and low 32 bytes, so that no row is longer than a line. -/ + def joinHalves (high low : Nat) : Nat := high * 2 ^ 256 + low + + + """); + text.Append(FixturePage(file)).Append('\n'); + text.Append(BindingImage.SingleDigestTheorem(file)).Append("#print axioms digest\n\n"); + var identity = Read(file).Last().Text; + text.Append("/-- The identity of the assembly the image holds. -/\ndef identity : String :=\n") + .Append($" \"{identity}\"\n\n"); + text.Append("/-- What System.Reflection.Metadata reads from the image. -/\n"); + text.Append("def expectedFacts : List StrictBinding.Fact :=\n ["); + text.Append(string.Join(",\n ", Read(file).Select(fact => fact.Lean()))).Append("]\n\n"); + text.Append("/-- The method bodies of the image: name, MethodDef token and the bytes of header and code. " + + "-/\n"); + text.Append("def bodies : List (String × Nat × List Nat) :=\n ["); + text.Append(string.Join(",\n ", Bodies(file).Select(body => + $"(\"{body.Name}\", 0x{body.Token:x8},\n [{string.Join(",\n ", body.Bytes.Chunk(BodyBytesPerRow) + .Select(row => string.Join(", ", row.Select(Hex))))}])"))); + text.Append("]\n\n"); + text.Append("/-- The assembly block an extraction carries for the image. -/\n") + .Append("def assemblyBlock : CIL.Extraction.AssemblyMetadata :=\n ").Append(AssemblyBlock(file)) + .Append("\n\n"); + text.Append("/-- A slice an extraction could carry for the image: every row of the carried tables, and the\n") + .Append(" entries the rows other than Field rows name. -/\n") + .Append("def slice : CIL.Extraction.MetadataSlice :=\n ").Append(Slice(file)) + .Append("\n\nend StrictFixture\n"); + return text.ToString(); + } + + // The fixed fields of a table stream header before its row counts, and the bytes of each row count. + const int TableHeaderFixedBytes = 24; + const int RowCountBytes = 4; + + // The Assembly row's PublicKey, Name and Culture columns follow its hash algorithm, four version numbers and + // flags. + const int AssemblyHeapColumns = 4 + 4 * 2 + 4; + + // The widest a generated Lean line may be. + const int LeanLineWidth = 96; + + /// + /// The assembly block an extraction carries for an image, as a Lean `CIL.Extraction.AssemblyMetadata`: the table + /// stream header through its last row count, the header decoded, the Assembly row with each heap offset replaced + /// by its key, and the entries the row names. A key is 0 for offset 0 and otherwise the rank of the entry's bytes + /// among the block's entries of its heap, as the extractor numbers them. + /// + internal static string AssemblyBlock(byte[] file) + { + using PEReader pe = new(ImmutableArray.Create(file)); + var reader = pe.GetMetadataReader(); + var metadata = pe.PEHeaders.MetadataStartOffset; + var streams = Streams(file, metadata); + var tables = (int)streams["#~"].Start; + var valid = BitConverter.ToUInt64(file, tables + 8); + var header = file[tables..(tables + TableHeaderFixedBytes + RowCountBytes * BitOperations.PopCount(valid))]; + var heapSizes = header[HeapSizesAt]; + List counts = []; + var countAt = TableHeaderFixedBytes; + for (var table = 0; table < 64; table++) + { + if ((valid >> table & 1) == 0) continue; + counts.Add($"({table}, {BitConverter.ToUInt32(header, countAt)})"); + countAt += RowCountBytes; + } + var assembly = reader.GetAssemblyDefinition(); + var row = file.AsSpan(metadata + reader.GetTableMetadataOffset(TableIndex.Assembly), + reader.GetTableRowSize(TableIndex.Assembly)).ToArray(); + var (stringEntry, blobEntry) = EntryReaders(file, streams); + var key = reader.GetHeapOffset(assembly.PublicKey); + var name = reader.GetHeapOffset(assembly.Name); + var culture = reader.GetHeapOffset(assembly.Culture); + var (blobKeys, blobs) = Ranked([key], blobEntry); + var (stringKeys, strings) = Ranked([name, culture], stringEntry); + var (stringWidth, blobWidth) = HeapWidths(heapSizes); + var column = AssemblyHeapColumns; + (int Offset, Dictionary Keys, int Width)[] cells = + [(key, blobKeys, blobWidth), (name, stringKeys, stringWidth), (culture, stringKeys, stringWidth)]; + foreach (var (offset, keys, width) in cells) + { + Array.Copy(BitConverter.GetBytes(keys[offset]), 0, row, column, width); + column += width; + } + var token = KeyToken(reader, assembly); + var keyToken = token is null ? "" + : $"keyToken := {Packed(Encoding.ASCII.GetBytes(token), " ")},\n "; + return $$""" + { identity := "{{Identity(reader, assembly)}}", + {{keyToken}}tableHeader := + {{Packed(header, " ")}}, + header := .ofCounts {{heapSizes}} + {{Packed(counts, " ")}}, + assemblyRow := + {{Packed(row, " ")}}, + strings := + {{Entries(strings)}}, + blobs := + {{Entries(blobs)}} } + """; + } + + // The bytes of a MethodDef row's RVA column, which a carried row holds as zero. + const int RvaBytes = 4; + + // The tables a carried slice holds rows of, other than Assembly, in token order. + static readonly TableIndex[] SliceTables = [TableIndex.TypeRef, TableIndex.TypeDef, TableIndex.Field, + TableIndex.MethodDef, TableIndex.MemberRef, TableIndex.StandAloneSig, TableIndex.TypeSpec, + TableIndex.AssemblyRef, TableIndex.ExportedType, TableIndex.NestedClass, TableIndex.GenericParam, TableIndex.MethodSpec]; + + /// + /// A slice an extraction could carry for an image, as a Lean `CIL.Extraction.MetadataSlice`: the assembly block, + /// every row of the tables a slice carries with MethodDef RVAs zeroed, or only the rows whose tokens + /// holds, and the entries the rows other than Field rows name. A heap cell holds the key + /// of its entry, numbered as in over the slice's entries, 0 for offset 0, and the + /// omitted-entry marker for an entry only Field rows name. + /// + internal static string Slice(byte[] file, IReadOnlySet? tokens = null) + { + using PEReader pe = new(ImmutableArray.Create(file)); + var reader = pe.GetMetadataReader(); + var metadata = pe.PEHeaders.MetadataStartOffset; + var streams = Streams(file, metadata); + var (stringWidth, blobWidth) = HeapWidths(file[(int)streams["#~"].Start + HeapSizesAt]); + List<(int Token, byte[] Row, (bool Blob, int Offset)[] Cells, int At)> rows = []; + foreach (var table in SliceTables) + { + var size = reader.GetTableRowSize(table); + for (var number = 1; number <= reader.GetTableRowCount(table); number++) + { + var handle = MetadataTokens.EntityHandle(table, number); + if (tokens is not null && !tokens.Contains(MetadataTokens.GetToken(handle))) continue; + var row = file.AsSpan(metadata + reader.GetTableMetadataOffset(table) + (number - 1) * size, size) + .ToArray(); + if (table == TableIndex.MethodDef) Array.Clear(row, 0, RvaBytes); + var cells = HeapCells(reader, handle); + var widths = cells.Sum(cell => cell.Blob ? blobWidth : stringWidth); + rows.Add((MetadataTokens.GetToken(handle), row, cells, HeapColumnsAt(table) ?? size - widths)); + } + } + var named = rows.Where(row => row.Token >> 24 != (int)TableIndex.Field).SelectMany(row => row.Cells).ToArray(); + var (stringEntry, blobEntry) = EntryReaders(file, streams); + int[] Offsets(bool blob) => [.. named.Where(cell => cell.Blob == blob).Select(cell => cell.Offset)]; + var (stringKeys, strings) = Ranked(Offsets(false), stringEntry); + var (blobKeys, blobs) = Ranked(Offsets(true), blobEntry); + foreach (var (_, row, cells, at) in rows) + { + var column = at; + foreach (var (blob, offset) in cells) + { + var (keys, width) = blob ? (blobKeys, blobWidth) : (stringKeys, stringWidth); + var key = keys.TryGetValue(offset, out var rank) ? (uint)rank : uint.MaxValue >> 8 * (4 - width); + Array.Copy(BitConverter.GetBytes(key), 0, row, column, width); + column += width; + } + } + var carried = Packed(rows.Select(row => $"(0x{row.Token:x8}, {Packed(row.Row, " ")})"), " ", + oneEach: true); + return $$""" + { assembly := assemblyBlock, + rows := + {{carried}}, + strings := + {{Entries(strings)}}, + blobs := + {{Entries(blobs)}} } + """; + } + + // Where the heap columns of a row start, for tables whose earlier columns have fixed widths; the heap + // columns of the other carried tables end their rows. + static int? HeapColumnsAt(TableIndex table) => table switch + { + TableIndex.TypeDef => 4, + TableIndex.Field => 2, + TableIndex.MethodDef or TableIndex.ExportedType => 8, + TableIndex.StandAloneSig or TableIndex.TypeSpec => 0, + TableIndex.AssemblyRef => 12, + _ => null + }; + + /// + /// The heap cells of a row in column order, as System.Reflection.Metadata reads them: whether each indexes + /// `#Blob` rather than `#Strings`, and the offset it holds. + /// + static (bool Blob, int Offset)[] HeapCells(MetadataReader reader, EntityHandle handle) + { + (bool, int) Text(StringHandle value) => (false, reader.GetHeapOffset(value)); + (bool, int) Blob(BlobHandle value) => (true, reader.GetHeapOffset(value)); + switch (handle.Kind) + { + case HandleKind.TypeReference: + var reference = reader.GetTypeReference((TypeReferenceHandle)handle); + return [Text(reference.Name), Text(reference.Namespace)]; + case HandleKind.ExportedType: + var exported = reader.GetExportedType((ExportedTypeHandle)handle); + return [Text(exported.Name), Text(exported.Namespace)]; + case HandleKind.TypeDefinition: + var type = reader.GetTypeDefinition((TypeDefinitionHandle)handle); + return [Text(type.Name), Text(type.Namespace)]; + case HandleKind.FieldDefinition: + var field = reader.GetFieldDefinition((FieldDefinitionHandle)handle); + return [Text(field.Name), Blob(field.Signature)]; + case HandleKind.MethodDefinition: + var method = reader.GetMethodDefinition((MethodDefinitionHandle)handle); + return [Text(method.Name), Blob(method.Signature)]; + case HandleKind.MemberReference: + var member = reader.GetMemberReference((MemberReferenceHandle)handle); + return [Text(member.Name), Blob(member.Signature)]; + case HandleKind.StandaloneSignature: + return [Blob(reader.GetStandaloneSignature((StandaloneSignatureHandle)handle).Signature)]; + case HandleKind.TypeSpecification: + return [Blob(reader.GetTypeSpecification((TypeSpecificationHandle)handle).Signature)]; + case HandleKind.AssemblyReference: + var assembly = reader.GetAssemblyReference((AssemblyReferenceHandle)handle); + return [Blob(assembly.PublicKeyOrToken), Text(assembly.Name), Text(assembly.Culture), + Blob(assembly.HashValue)]; + case HandleKind.GenericParameter: + return [Text(reader.GetGenericParameter((GenericParameterHandle)handle).Name)]; + case HandleKind.MethodSpecification: + return [Blob(reader.GetMethodSpecification((MethodSpecificationHandle)handle).Signature)]; + default: + return []; + } + } + + // The heap size flags follow the reserved word and the version bytes of a table stream header. + const int HeapSizesAt = 6; + + /// The widths of `#Strings` and `#Blob` indexes under a table stream's heap size flags. + static (int Text, int Blob) HeapWidths(byte heapSizes) => + ((heapSizes & 1) != 0 ? 4 : 2, (heapSizes & 4) != 0 ? 4 : 2); + + /// + /// Readers of the `#Strings` entry at an offset, through its terminator, and of the `#Blob` entry at an offset, + /// with its length prefix. + /// + static (Func Text, Func Blob) EntryReaders(byte[] file, + Dictionary streams) + { + var stringsStart = (int)streams["#Strings"].Start; + var blobsStart = (int)streams["#Blob"].Start; + byte[] StringEntry(int offset) => + file[(stringsStart + offset)..(Array.IndexOf(file, (byte)0, stringsStart + offset) + 1)]; + byte[] BlobEntry(int offset) + { + var start = blobsStart + offset; + int first = file[start]; + var (prefix, length) = first < 0x80 ? (1, first) + : first < 0xC0 ? (2, (first & 0x3F) << 8 | file[start + 1]) + : (4, (first & 0x1F) << 24 | file[start + 1] << 16 | file[start + 2] << 8 | file[start + 3]); + return file[start..(start + prefix + length)]; + } + return (StringEntry, BlobEntry); + } + + /// Carried heap entries as a Lean list of key and bytes, one entry to a line. + static string Entries(SortedDictionary entries) => Packed(entries.Select(entry => + $"({entry.Key}, {Packed(entry.Value, new string(' ', EntryIndent + $"({entry.Key}, ".Length))})"), + new string(' ', EntryIndent - 1), oneEach: true); + + // The column where each carried entry starts, past the list's bracket. + const int EntryIndent = 7; + + /// + /// The public key token of an Assembly row as Lean spells it: the last eight bytes of the key's SHA-1 digest in + /// reverse when the PublicKey flag is set, the key itself otherwise, and none without a key. The runtime's own + /// computation refuses keys that are not well-formed strong-name keys, which a synthetic image need not have. + /// + static string? KeyToken(MetadataReader reader, AssemblyDefinition assembly) + { + var key = reader.GetBlobBytes(assembly.PublicKey); + if (key.Length == 0) return null; + var token = (assembly.Flags & System.Reflection.AssemblyFlags.PublicKey) != 0 + ? System.Security.Cryptography.SHA1.HashData(key)[^8..].Reverse().ToArray() : key; + return Convert.ToHexStringLower(token); + } + + /// The identity of an Assembly row as Lean spells it, with the token of . + static string Identity(MetadataReader reader, AssemblyDefinition assembly) + { + var culture = reader.GetString(assembly.Culture); + var token = KeyToken(reader, assembly) ?? "null"; + return $"{reader.GetString(assembly.Name)}, Version={assembly.Version}, " + + $"Culture={(culture.Length == 0 ? "neutral" : culture)}, PublicKeyToken={token}"; + } + + /// The identity of the assembly an image defines, as Lean spells it. + internal static string Identity(byte[] file) + { + using PEReader pe = new(ImmutableArray.Create(file)); + var reader = pe.GetMetadataReader(); + return Identity(reader, reader.GetAssemblyDefinition()); + } + + /// + /// Keys for heap offsets, as the extractor numbers an assembly block: 0 for offset 0, and the rank from 1 of each + /// other entry's bytes in hexadecimal order. The entries are keyed the same way. + /// + static (Dictionary Keys, SortedDictionary Entries) Ranked(int[] offsets, + Func entry) + { + var keys = new Dictionary { [0] = 0 }; + SortedDictionary entries = []; + var rank = 1; + foreach (var offset in offsets.Where(offset => offset != 0).Distinct() + .OrderBy(offset => Convert.ToHexString(entry(offset)), StringComparer.Ordinal)) + { + keys[offset] = rank; + entries[rank++] = entry(offset); + } + if (offsets.Contains(0)) entries[0] = entry(0); + return (keys, entries); + } + + /// A Lean list of bytes, wrapped so that no line is wider than a generated line may be. + static string Packed(IEnumerable bytes, string indent) => + Packed(bytes.Select(value => value.ToString(System.Globalization.CultureInfo.InvariantCulture)), indent); + + /// A Lean list of items, wrapped below the line width, or one item to a line. + static string Packed(IEnumerable items, string indent, bool oneEach = false) + { + List lines = []; + var line = new StringBuilder(); + foreach (var item in items) + { + if (line.Length > 0 && (oneEach || indent.Length + line.Length + item.Length + 3 > LeanLineWidth)) + { + lines.Add(line.Append(',').ToString()); + line.Clear(); + } + else if (line.Length > 0) line.Append(", "); + line.Append(item); + } + if (line.Length > 0) lines.Add(line.ToString()); + return "[" + string.Join("\n" + indent + " ", lines) + "]"; + } + + /// + /// The page and image definitions of a fixture image of at most one page: its 64-byte chunks listed, each split + /// into its high and low 32 bytes, so that each line of the checked-in module stays within the line width. The + /// page is the same list of the same type that the audit reads through the same reader as a generated page. + /// + static string FixturePage(byte[] file) + { + const int chunkBytes = 64; + const int halfDigits = chunkBytes; // two hexadecimal digits per byte of a half chunk + var padded = new byte[(file.Length + chunkBytes - 1) / chunkBytes * chunkBytes]; + file.CopyTo(padded, 0); + var rows = padded.Chunk(chunkBytes).Select(chunk => Convert.ToHexStringLower(chunk)) + .Select(hex => $" joinHalves 0x{hex[..halfDigits]}\n 0x{hex[halfDigits..]}"); + return $"def page0 : List Nat := [\n{string.Join(",\n", rows)}\n]\n\n" + + BindingImage.ImageDefinition(file.Length); + } + + /// The bytes of the image a fixture module holds. + internal static byte[] ReadImage(string module) + { + var length = int.Parse(Length.Match(module).Groups[1].Value); + var chunks = Chunk.Matches(module).Select(match => match.Groups[1].Value + match.Groups[2].Value); + var bytes = Convert.FromHexString(string.Concat(chunks)); + return bytes[..length]; + } + + const int BodyBytesPerRow = 12; + + static string Hex(byte value) => "0x" + value.ToString("x2"); + + [GeneratedRegex(@"length := (\d+),")] + private static partial Regex Length { get; } + + [GeneratedRegex(@"^ joinHalves 0x([0-9a-f]{64})\n 0x([0-9a-f]{64}),?$", RegexOptions.Multiline)] + private static partial Regex Chunk { get; } + +} diff --git a/Tests/StrictBindingFixture.cs b/Tests/StrictBindingFixture.cs new file mode 100644 index 000000000..f3cd77ce8 --- /dev/null +++ b/Tests/StrictBindingFixture.cs @@ -0,0 +1,52 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:package Mono.Cecil +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/DeepCheckInputs.cs +#:include ../scripts/BindingImage.cs +#:include StrictBindingFacts.cs +#:include MultiAssemblyFixture.cs + +// Builds the small library the strict binding Lean tests decode and writes Tests/StrictBinding/Fixture.lean +// from it with `--update`; without it, checks that the checked-in module still holds a consistent image. +const string librarySource = """ +using System; +using System.Runtime.InteropServices; +[assembly: System.Reflection.AssemblyVersion("1.2.3.4")] +namespace StrictFixture; +public enum Small : byte { A = 1, B = 5 } +[StructLayout(LayoutKind.Explicit, Size = 8)] +public struct Overlay { [FieldOffset(0)] public int Low; [FieldOffset(4)] public int High; } +public static class Arithmetic +{ + public static int Tiny(int x) => x + 1; + public static int Fat(int x) { int a = x * 3; int b = a - 7; if (b > 100) b = 100; return b; } + public static ReadOnlySpan Table => new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; +} +public class Shape { public virtual int Sides() => 0; public static int Count(Shape shape) => shape.Sides(); } +public class Triangle : Shape { public override int Sides() => 3; } +public class Square : Shape { public override int Sides() => 4; } +"""; + +string module = At("Tests/StrictBinding/Fixture.lean"); +if (args is ["--update"]) +{ + MultiAssemblyFixture fixture = new("StrictBindingFixture"); + string dll = fixture.Build("Fixture", "StrictFixture", librarySource, + "nonefalse"); + byte[] built = File.ReadAllBytes(dll); + Directory.CreateDirectory(Path.GetDirectoryName(module)!); + File.WriteAllText(module, StrictBindingFacts.Module(built), new System.Text.UTF8Encoding(false)); + Console.WriteLine($"Wrote {module}: {built.Length} bytes, SHA-256 {BindingImage.Digest(built)}"); + return; +} +if (args.Length != 0) throw new ArgumentException("Usage: StrictBindingFixture.cs [--update]"); +string checkedIn = File.ReadAllText(module); +byte[] image = StrictBindingFacts.ReadImage(checkedIn); +if (StrictBindingFacts.Module(image) != checkedIn.ReplaceLineEndings("\n")) + throw new InvalidOperationException($"{module} is not what its image generates; run with --update."); +Console.WriteLine($"PASS strict binding fixture module is consistent: {image.Length} bytes, " + + $"SHA-256 {BindingImage.Digest(image)}"); diff --git a/Tests/StrictBindingScaling.cs b/Tests/StrictBindingScaling.cs new file mode 100644 index 000000000..0724c9771 --- /dev/null +++ b/Tests/StrictBindingScaling.cs @@ -0,0 +1,939 @@ +using System.Reflection; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; + +partial class Program +{ + /// The most a claim may cost more, per claim, when the claims grow at a fixed image. + const double MaximumPerClaimGrowth = 2; + + /// The most the check may take longer, as a multiple, for a larger image with the same claims. + const double MaximumImageGrowth = 4; + + /// The most a carried key byte may cost more, per byte, when the carried key grows fourfold. + const double MaximumPerByteGrowth = 1.25; + + /// The most a carried row may cost more, per row, when the carried rows grow fourfold. + const double MaximumPerRowGrowth = 1.5; + + /// The most an added metadata page may cost relative to an earlier added page. + const double MaximumPerPageGrowth = 1.25; + + /// The most a check of fixed claims may cost more, as a multiple, at 16 times the TypeDef rows. + const double MaximumTableGrowth = 1.15; + + /// The most a page of chunks with equal ends may cost, as a multiple of one with random ends. + const double MaximumEqualTailGrowth = 1.1; + + /// + /// Body-check heartbeats grow linearly with claims and cover decoding the image once per page. + /// The fixture's heartbeat count is the fixed cost subtracted from each larger case. + /// + static void CheckBindingScaling(string root) + { + // Memory cannot be measured here, so the page layout that bounds it is pinned: pages of about 250 methods + // until the page cap, then pages that grow with the evidence, and one empty page for no methods. + (int Methods, int Size, int Pages)[] layouts = + [(0, 1, 1), (318, 159, 2), (1272, 212, 6), (2000, 250, 8), (5088, 636, 8), (10767, 1346, 8)]; + foreach (var (methods, size, pages) in layouts) + if (BindingPageSize(methods) != size || BindingPageCount(methods) != pages || + pages * size < methods) + throw new InvalidOperationException($"{methods} methods are paged {BindingPageCount(methods)} x " + + $"{BindingPageSize(methods)}, not {pages} x {size}"); + Console.WriteLine("PASS the body proof pages at about 250 methods up to 8 pages"); + var package = FixturePackage(); + Run(["lake", "build", "StrictBinding.Evidence", "StrictBinding.Sha256"], package, + progress: "Building the strict binding library"); + CheckGeneratedPagedModulesElaborate(root, package); + CheckAssemblyBlockScaling(root, package); + CheckSliceRowsScaling(root, package); + CheckUserStringScaling(root, package); + CheckMetadataPageScaling(root, package); + CheckMetadataRowsModuleScaling(root, package); + CheckTypeDefinitionScaling(root, package); + CheckEqualTailPageCost(root, package); + var examples = File.ReadAllBytes(At("Examples/Assembly/bin/Examples.dll")); + var larger = new byte[examples.Length + 770 * 1024]; + examples.CopyTo(larger, 0); + new Random(5).NextBytes(larger.AsSpan(examples.Length)); + const int smaller = 1272; + const int largerClaims = 5088; + (string Name, byte[] Bytes, int? Claims)[] cases = + [ + ("fixture", StrictBindingFacts.ReadImage(File.ReadAllText(Fixture)), null), + ($"Examples.dll, {smaller} claims", examples, smaller), + ($"Examples.dll and 770 KB more, {smaller} claims", larger, smaller), + ($"Examples.dll, {largerClaims} claims", examples, largerClaims) + ]; + Directory.CreateDirectory(root); + var heartbeats = new long[cases.Length]; + for (var index = 0; index < cases.Length; index++) + { + var path = Path.Combine(root, $"Bodies{index}.lean"); + File.WriteAllText(path, AllBodiesModule(cases[index].Bytes, cases[index].Claims) + + "\n#eval show IO Nat from IO.getNumHeartbeats\n"); + var run = Execute(["lake", "env", "lean", "-DElab.async=false", path], cwd: package); + if (run.Code != 0) + throw new InvalidOperationException($"The bodies of {cases[index].Name} were not found: {run.Output}"); + heartbeats[index] = long.Parse(run.Output.Split('\n', StringSplitOptions.TrimEntries | + StringSplitOptions.RemoveEmptyEntries).Last(), System.Globalization.CultureInfo.InvariantCulture); + } + var costs = cases.Zip(heartbeats, (item, count) => $"{item.Name} {count} heartbeats"); + Console.WriteLine("Body checks: " + string.Join(", ", costs)); + double Above(int index) => heartbeats[index] - heartbeats[0]; + if (Above(1) <= 0) + throw new InvalidOperationException("The body check has no measured work above the fixture"); + var perClaim = Above(3) / largerClaims / (Above(1) / smaller); + var perImage = Above(2) / Above(1); + if (perClaim > MaximumPerClaimGrowth || perImage > MaximumImageGrowth) + throw new InvalidOperationException( + $"The body check used {perClaim:F1} times the heartbeats per claim for " + + $"{largerClaims / smaller} times the claims and {perImage:F1} times for 8.8 times the image"); + Console.WriteLine($"PASS the body check is linear in the claims ({perClaim:F1} times per claim) and " + + $"independent of the image size ({perImage:F1} times for 8.8)"); + } + + /// + /// The check of a carried Assembly block reads each carried entry byte once, so its kernel heartbeats grow + /// linearly with them. Images with a short name and a public key blob of 256, 1024 and 4096 bytes are checked, + /// and the heartbeats of preparing each image are subtracted; the name stays short because preparing spells + /// the identity at a cost quadratic in its length. A byte added from 1024 to 4096 may cost at most 1.25 times + /// a byte added from 256 to 1024, where a cost quadratic in the bytes would give four times; reruns differ by + /// a few heartbeats, far inside that bound. + /// + static void CheckAssemblyBlockScaling(string root, string package) + { + const string report = "#eval show IO Nat from IO.getNumHeartbeats"; + int[] sizes = [256, 1024, 4096]; + Run(["lake", "build", "StrictBinding.Rows"], package, progress: "Building the carried metadata checks"); + var cases = string.Concat(sizes.Select(size => + { + var file = SyntheticAssembly(size); + var pinned = $"{{ identity := \"{StrictBindingFacts.Identity(file)}\", image := image }}"; + return $$""" + namespace Size{{size}} + {{BindingImage.Definitions(file)}} + def pinned : StrictBinding.PinnedImage := {{pinned}} + def block : CIL.Extraction.AssemblyMetadata := + {{StrictBindingFacts.AssemblyBlock(file)}} + {{report}} + theorem prepared : StrictBinding.ok (StrictBinding.prepareOne pinned) = true := by decide +kernel + {{report}} + theorem checked : StrictBinding.ok + (StrictBinding.prepareOne pinned >>= (StrictBinding.assemblyBound · block)) = true := by + decide +kernel + {{report}} + end Size{{size}} + + """; + })); + var path = Path.Combine(root, "AssemblyBlocks.lean"); + File.WriteAllText(path, $"import StrictBinding.Rows\n\n{cases}"); + var measured = RequireGrowth(sizes, CheckHeartbeats(path, package, sizes.Length, "carried Assembly blocks"), + MaximumPerByteGrowth, "carried Assembly block check", "byte"); + Console.WriteLine($"PASS the carried Assembly block check is linear in its key bytes ({measured})"); + } + + /// + /// The rows check sorts the heap cells of the carried rows once per heap and reads each row and entry once, so + /// its kernel heartbeats grow as n log n in the rows. Images with 64, 256 and 1024 MethodDef rows, each with its + /// own name and all with one signature, are checked against a slice of all their rows, and the heartbeats of + /// preparing each image are subtracted. A row added from 256 to 1024 may cost at most 1.5 times a row added + /// from 64 to 256, where n log n gives at most 1.23 and a numbering quadratic in the rows about four. + /// + static void CheckSliceRowsScaling(string root, string package) + { + const string report = "#eval show IO Nat from IO.getNumHeartbeats"; + int[] sizes = [64, 256, 1024]; + var cases = string.Concat(sizes.Select(size => + { + var file = SyntheticMethods(size); + var pinned = $"{{ identity := \"{StrictBindingFacts.Identity(file)}\", image := image }}"; + return $$""" + namespace Rows{{size}} + {{BindingImage.Definitions(file)}} + def pinned : StrictBinding.PinnedImage := {{pinned}} + def assemblyBlock : CIL.Extraction.AssemblyMetadata := + {{StrictBindingFacts.AssemblyBlock(file)}} + -- A list literal of a thousand rows elaborates deeper than the default limit. + set_option maxRecDepth 65536 in + def slice : CIL.Extraction.MetadataSlice := + {{StrictBindingFacts.Slice(file)}} + {{report}} + theorem prepared : StrictBinding.ok (StrictBinding.prepareOne pinned) = true := by decide +kernel + {{report}} + theorem checked : StrictBinding.ok (StrictBinding.prepareOne pinned >>= fun image => + StrictBinding.rowsBound image slice.rows slice.strings slice.blobs) = true := by + decide +kernel + {{report}} + end Rows{{size}} + + """; + })); + var path = Path.Combine(root, "SliceRows.lean"); + File.WriteAllText(path, $"import StrictBinding.Rows\n\n{cases}"); + var measured = RequireGrowth(sizes, CheckHeartbeats(path, package, sizes.Length, "carried rows"), + MaximumPerRowGrowth, "rows check", "row"); + Console.WriteLine($"PASS the rows check grows as n log n in the carried rows ({measured})"); + } + + /// The whole metadata check with one user string grows linearly in its carried bytes. + static void CheckUserStringScaling(string root, string package) + { + const string report = "#eval show IO Nat from IO.getNumHeartbeats"; + int[] sizes = [1024, 4096, 16384]; + var cases = string.Concat(sizes.Select(size => + { + var characters = size / 2; + var file = SyntheticUserString(characters); + var length = 2 * characters + 1; + int[] prefix = length < 0x4000 + ? [0x80 | (length >> 8), length & 255] + : [0xC0 | (length >> 24), (length >> 16) & 255, (length >> 8) & 255, length & 255]; + return $$""" + namespace UserString{{size}} + {{BindingImage.Definitions(file)}} + /-- The synthetic image under its assembly identity. -/ + def pinned : StrictBinding.PinnedImage := + { identity := "{{StrictBindingFacts.Identity(file)}}", image := image } + /-- The independently numbered Assembly block. -/ + def assemblyBlock : CIL.Extraction.AssemblyMetadata := + {{StrictBindingFacts.AssemblyBlock(file)}} + /-- One UTF-16 user string, including its prefix and terminal byte. -/ + def entry : List Nat := [{{string.Join(", ", prefix)}}] ++ + (List.replicate {{characters}} [65, 0]).flatten ++ [0] + /-- Metadata carrying the Assembly block and one user string. -/ + def slice : CIL.Extraction.MetadataSlice := + { assembly := assemblyBlock, userStrings := [(1, entry)] } + {{report}} + /-- The synthetic image has a supported container and metadata layout. -/ + theorem prepared : StrictBinding.ok (StrictBinding.prepareOne pinned) = true := by decide +kernel + {{report}} + /-- Every carried entry belongs to the synthetic image. -/ + theorem checked : + StrictBinding.ok (StrictBinding.prepareOne pinned >>= fun image => + StrictBinding.sliceBound image slice) = true := by + decide +kernel + {{report}} + #print axioms prepared + #print axioms checked + end UserString{{size}} + + """; + })); + Directory.CreateDirectory(root); + var path = Path.Combine(root, "UserStrings.lean"); + File.WriteAllText(path, $"import StrictBinding.Slice\n\n{cases}"); + var measured = RequireGrowth(sizes, CheckHeartbeats(path, package, sizes.Length, "carried user strings"), + MaximumPerByteGrowth, "user-string metadata check", "byte"); + Console.WriteLine($"PASS the complete metadata check is linear in its user-string bytes ({measured})"); + } + + /// + /// Paged proofs of the slice contents, the rows and heaps a method carries without its Assembly block, at 1, 4 and + /// 16 pages have flat marginal cost per page. + /// + static void CheckMetadataPageScaling(string root, string package) + { + const string report = "#eval show IO Nat from IO.getNumHeartbeats"; + const int characters = 512; + int[] sizes = [1, 4, 16]; + var file = SyntheticUserString(characters); + var cases = string.Concat(sizes.Select(pages => + { + var lemmas = string.Concat(Enumerable.Range(0, pages).Select(index => $$""" + /-- The rows and heaps of this page's method metadata are the pinned image's. -/ + theorem metadataPage{{index}} : + StrictBinding.ClaimsHold StrictBinding.methodMetadataBound pinned + (StrictBinding.claimPage claims 1 {{index}}) := by decide +kernel + #print axioms metadataPage{{index}} + + """)); + var chain = string.Concat(Enumerable.Range(0, pages).Reverse() + .Select(index => $"(.succ metadataPage{index} ")); + return $$""" + namespace Pages{{pages}} + {{BindingImage.Definitions(file)}} + /-- The synthetic image under its assembly identity. -/ + def pinned : List StrictBinding.PinnedImage := + [{ identity := "{{StrictBindingFacts.Identity(file)}}", image := image }] + /-- The independently numbered Assembly block. -/ + def assemblyBlock : CIL.Extraction.AssemblyMetadata := + {{StrictBindingFacts.AssemblyBlock(file)}} + /-- A two-byte prefix and a 1025-byte UTF-16 payload, including the terminal byte. -/ + def entry : List Nat := [132, 1] ++ (List.replicate {{characters}} [65, 0]).flatten ++ [0] + /-- Each method carries a complete Assembly block and one user string. -/ + def metadata : CIL.Extraction.MethodMetadata := + { own := { assembly := assemblyBlock, userStrings := [(1, entry)] } } + /-- One method per page keeps the compared byte count fixed per page. -/ + def claims : List CIL.Extraction.MethodMetadata := List.replicate {{pages}} metadata + {{report}} + /-- The pinned image has a supported container and metadata layout. -/ + theorem prepared : StrictBinding.ok (StrictBinding.prepare pinned) = true := by decide +kernel + {{report}} + {{lemmas}} + /-- The pages cover every metadata claim. -/ + theorem checked : StrictBinding.ClaimsHold StrictBinding.methodMetadataBound pinned claims := + StrictBinding.claimsHold_of_pages (size := 1) (count := {{pages}}) prepared + (by decide +kernel) {{chain}}.zero{{new string(')', pages)}} + {{report}} + #print axioms prepared + #print axioms checked + end Pages{{pages}} + + """; + })); + Directory.CreateDirectory(root); + var path = Path.Combine(root, "MetadataPages.lean"); + File.WriteAllText(path, $"import StrictBinding.Evidence\n\n{cases}"); + var measured = RequireGrowth(sizes, CheckHeartbeats(path, package, sizes.Length, "metadata pages"), + MaximumPerPageGrowth, "metadata page check", "page"); + Console.WriteLine($"PASS the paged metadata contents proof has flat cost per page ({measured})"); + } + + /// + /// The pages of the generated metadata rows module, kernel-checked as replay checks them, over synthetic images + /// whose methods each carry their image's Assembly block and their own MethodDef row, at 318, 1272 and 5088 + /// methods of one image and at 1272 methods split over two, paged as a deep run pages them. The evidence's + /// metadata list is given directly, and the heartbeats of one preparation are subtracted. A method selects its + /// image and checks its row once and each distinct block is checked once, so a method may cost at most twice as + /// much at 5088 methods as at 1272, although the image the preparations decode grows with the methods too, and + /// at most twice as much over two images as over one. + /// + static void CheckMetadataRowsModuleScaling(string root, string package) + { + int[] sizes = [318, 1272, 5088]; + Directory.CreateDirectory(root); + var heartbeats = sizes.Select(methods => MetadataRowsModuleHeartbeats(root, package, methods, 1)).ToArray(); + var split = MetadataRowsModuleHeartbeats(root, package, sizes[1], 2); + var measured = string.Join(", ", sizes.Zip(heartbeats, (methods, cost) => $"{cost} heartbeats at {methods}")); + var perMethod = (double)heartbeats[2] / sizes[2] / ((double)heartbeats[1] / sizes[1]); + if (heartbeats[1] <= 0 || perMethod > MaximumPerClaimGrowth) + throw new InvalidOperationException($"The metadata rows module costs {measured}; a method costs " + + $"{perMethod:F2} times as much at {sizes[2]} methods as at {sizes[1]}"); + var perImage = (double)split / heartbeats[1]; + if (split <= 0 || perImage > MaximumPerClaimGrowth) + throw new InvalidOperationException($"The metadata rows module costs {split} heartbeats for {sizes[1]} " + + $"methods over two images and {heartbeats[1]} over one"); + Console.WriteLine($"PASS the metadata rows module is linear in the methods ({measured}; " + + $"{perMethod:F2} times per method; {split} heartbeats at {sizes[1]} over two images, " + + $"{perImage:F2} times)"); + } + + /// + /// The kernel heartbeats of the generated metadata rows module, above one preparation, for `methods` methods + /// split evenly over `images` synthetic images, each method carrying its image's Assembly block and its own + /// MethodDef row. + /// + static long MetadataRowsModuleHeartbeats(string root, string package, int methods, int images) + { + const string report = "#eval show IO Nat from IO.getNumHeartbeats"; + const string imports = "import BindingAudit.Images\nimport Extracted\nimport StrictBinding.Evidence\n"; + const string claimsOfEvidence = "(StrictBinding.metadataClaims Extracted.methodEvidence)"; + const string boundEvidence = "StrictBinding.MetadataRowsBound images Extracted.methodEvidence"; + const string bound = "And (StrictBinding.ClaimsHold StrictBinding.assemblyBlockBound images " + + "(StrictBinding.assemblyBlocks claims))\n" + + " (StrictBinding.ClaimsHold StrictBinding.methodMetadataBound images claims)"; + var size = BindingPageSize(methods); + var source = PagedFactModuleSource(BindingFact.MetadataRows, methods, size).ReplaceLineEndings("\n"); + var pages = BindingPageCount(methods, size); + // The pages and the distinct-block lemma each name the evidence's metadata list. + if (!source.StartsWith(imports, StringComparison.Ordinal) || + source.Split(claimsOfEvidence).Length != pages + 2 || source.Split(boundEvidence).Length != 2) + throw new InvalidOperationException("The generated metadata rows module changed its form"); + // Replay checks each lemma in the kernel; elaborating without the option makes the kernel check it here. + var module = source[imports.Length..].Replace("set_option debug.skipKernelTC true in\n", "") + .Replace(claimsOfEvidence, "claims", StringComparison.Ordinal) + .Replace(boundEvidence, bound, StringComparison.Ordinal); + var perImage = methods / images; + var files = Enumerable.Range(0, images).Select(image => SyntheticMethods(perImage, "Synthetic" + image)) + .ToArray(); + var definitions = string.Concat(files.Select((file, image) => $$""" + namespace Image{{image}} + {{BindingImage.Definitions(file)}} + def assemblyBlock : CIL.Extraction.AssemblyMetadata := + {{StrictBindingFacts.AssemblyBlock(file)}} + {{string.Concat(Enumerable.Range(1, perImage).Select(row => + $"def slice{row} : CIL.Extraction.MetadataSlice :=\n " + + StrictBindingFacts.Slice(file, new HashSet { MetadataTokens.GetToken( + MetadataTokens.MethodDefinitionHandle(row)) }) + + $"\ndef method{row} : CIL.Extraction.MethodMetadata := {{ own := slice{row} }}\n"))}}end Image{{image}} + + """)); + // One list per page keeps each list literal within the elaborator's recursion limit. + var parts = Enumerable.Range(0, images).SelectMany(image => Enumerable.Range(1, perImage) + .Select(row => $"Image{image}.method{row}")).Chunk(size).ToArray(); + var lists = string.Concat(parts.Select((part, page) => + $"def claims{page} : List CIL.Extraction.MethodMetadata :=\n [{string.Join(", ", part)}]\n")); + var joined = Enumerable.Range(0, parts.Length - 1).Reverse() + .Aggregate($"claims{parts.Length - 1}", (rest, page) => $"(claims{page} ++ {rest})"); + var pinned = string.Join(", ", files.Select((file, image) => + $"{{ identity := \"{StrictBindingFacts.Identity(file)}\", image := Image{image}.image }}")); + var name = $"Methods{methods}x{images}"; + var path = Path.Combine(root, $"MetadataRows{methods}x{images}.lean"); + File.WriteAllText(path, $$""" + import StrictBinding.Evidence + + namespace {{name}} + {{definitions}}{{lists}}def claims : List CIL.Extraction.MethodMetadata := {{joined}} + def BindingAudit.images : List StrictBinding.PinnedImage := [{{pinned}}] + {{report}} + theorem preparedOnce : StrictBinding.ok (StrictBinding.prepare BindingAudit.images) = true := by + decide +kernel + {{report}} + {{module}} + {{report}} + #print axioms BindingAudit.metadataRows + end {{name}} + + """); + return CheckHeartbeats(path, package, 1, $"metadata rows of {methods} methods over {images} images")[0]; + } + + /// A library whose user-string heap holds one repeated UTF-16 string at offset 1. + static byte[] SyntheticUserString(int characters) + { + var metadata = new MetadataBuilder(); + metadata.AddAssembly(metadata.GetOrAddString("Synthetic"), new Version(1, 2, 3, 4), default, default, + default, AssemblyHashAlgorithm.Sha1); + metadata.AddModule(0, metadata.GetOrAddString("Synthetic.dll"), metadata.GetOrAddGuid(Guid.Empty), default, + default); + metadata.AddTypeDefinition(default, default, metadata.GetOrAddString(""), default, + MetadataTokens.FieldDefinitionHandle(1), MetadataTokens.MethodDefinitionHandle(1)); + var handle = metadata.GetOrAddUserString(new string('A', characters)); + if (MetadataTokens.GetHeapOffset(handle) != 1) + throw new InvalidOperationException("The synthetic user string must start at heap offset 1."); + var builder = new ManagedPEBuilder(PEHeaderBuilder.CreateLibraryHeader(), new MetadataRootBuilder(metadata), + new BlobBuilder()); + var image = new BlobBuilder(); + builder.Serialize(image); + return image.ToArray(); + } + + /// + /// The kernel heartbeats of each case's check above preparing its image, from a module that reports the count + /// before preparing, after preparing, and after a check that prepares the image again. + /// + static long[] CheckHeartbeats(string path, string package, int cases, string subject) + { + var counts = HeartbeatCounts(path, package, 3 * cases, subject); + return [.. Enumerable.Range(0, cases).Select(index => + counts[3 * index + 2] - 2 * counts[3 * index + 1] + counts[3 * index])]; + } + + /// The heartbeat counts a module reports, of which there must be `reports`, with no sorryAx. + static long[] HeartbeatCounts(string path, string package, int reports, string subject) + { + // Without the flag the kernel checks on another thread and its heartbeats go uncounted. + var output = Run(["lake", "env", "lean", "-DElab.async=false", "-M", "8192", path], cwd: package); + var counts = output.Split('\n').Select(line => line.Trim()) + .Where(line => line.Length > 0 && line.All(char.IsAsciiDigit)) + .Select(line => long.Parse(line, System.Globalization.CultureInfo.InvariantCulture)).ToArray(); + if (counts.Length != reports || output.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException($"Unchecked {subject}:\n{output}"); + return counts; + } + + /// + /// The kernel-checked digest proof of a page whose 64 chunks all end in the same 8 bytes costs about what the + /// proof of the same page with random ends costs. The kernel's caches hash a numeral by its low 64 bits, so + /// separate chunk numerals that end alike would collide there and make the hash quadratic in the chunks. + /// + static void CheckEqualTailPageCost(string root, string package) + { + const int pageBytes = 4096; + const int chunkBytes = 64; + const int tailBytes = 8; + const byte tail = 0xA5; + const string report = "#eval show IO Nat from IO.getNumHeartbeats\n"; + var distinct = new byte[pageBytes]; + new Random(13).NextBytes(distinct); + var equal = (byte[])distinct.Clone(); + for (var end = chunkBytes; end <= pageBytes; end += chunkBytes) + equal.AsSpan(end - tailBytes, tailBytes).Fill(tail); + (string Name, byte[] Bytes)[] pages = [("RandomEnds", distinct), ("EqualEnds", equal)]; + Directory.CreateDirectory(root); + var path = Path.Combine(root, "EqualTailPage.lean"); + File.WriteAllText(path, "import StrictBinding.Sha256\nimport CIL.Extraction.FixedDigits\n\n" + report + + string.Concat(pages.Select(page => + $"namespace {page.Name}\n\n{BindingImage.Definitions(page.Bytes)}\n" + + BindingImage.PagedDigestTheorem(page.Bytes, skipCompileCheck: false) + + $"end {page.Name}\n\n{report}"))); + var counts = HeartbeatCounts(path, package, pages.Length + 1, "digest proofs of one page"); + var (randomEnds, equalEnds) = (counts[1] - counts[0], counts[2] - counts[1]); + var measured = $"{equalEnds} heartbeats with equal ends, {randomEnds} with random ends"; + if (randomEnds <= 0 || (double)equalEnds / randomEnds > MaximumEqualTailGrowth) + throw new InvalidOperationException($"The digest proof of one page costs {measured}"); + Console.WriteLine($"PASS a page whose chunks end alike costs its digest proof what another page costs " + + $"({measured})"); + } + + /// + /// A unit added from the second size to the third costs at most `bound` times a unit added from the first to + /// the second; returns the measurements as text. + /// + static string RequireGrowth(int[] sizes, long[] checks, double bound, string subject, string unit) + { + var smaller = (double)(checks[1] - checks[0]) / (sizes[1] - sizes[0]); + var larger = (double)(checks[2] - checks[1]) / (sizes[2] - sizes[1]); + var measured = string.Join(", ", sizes.Zip(checks, (size, cost) => $"{cost} heartbeats at {size}")); + var growth = (larger / smaller).ToString("F2", System.Globalization.CultureInfo.InvariantCulture); + if (smaller <= 0 || larger / smaller > bound) + throw new InvalidOperationException($"The {subject} costs {measured}; a {unit} costs {growth} times as " + + $"much from {sizes[1]} to {sizes[2]} as from {sizes[0]} to {sizes[1]}"); + return $"{measured}; {growth} times per {unit}"; + } + + /// + /// A type definition claim names its row, the NestedClass rows that place it and its base row, so its check reads + /// a fixed number of cells and entries whatever the size of the tables. Images with 64, 256 and 1024 classes, + /// half of them nested, beside the types of `SyntheticTypes` are checked against nine claims that + /// System.Reflection.Metadata reads from them, and the heartbeats of preparing each image are subtracted. The + /// check at 1024 classes may cost at most `MaximumTableGrowth` times the check at 64. The same images are + /// checked to have a NestedClass table sorted by its nested type column, which reads every row once: a row added + /// at the largest size may cost at most `MaximumPerRowGrowth` times a row added at the smaller ones. The + /// smallest image also rejects claims that each change one part of a claim that holds, a NestedClass row that + /// repeats its neighbour's type, and a NestedClass table out of order. + /// + static void CheckTypeDefinitionScaling(string root, string package) + { + const string report = "#eval show IO Nat from IO.getNumHeartbeats"; + int[] sizes = [64, 256, 1024]; + Run(["lake", "build", "StrictBinding.TypeDefs", "Tests.StrictBinding.Mutation"], package, + progress: "Building the type definition checks"); + var files = sizes.Select(SyntheticTypes).ToArray(); + var nestedRows = files.Select(NestedTypeRows).ToArray(); + var cases = string.Concat(sizes.Select((size, index) => + { + var file = files[index]; + var identity = StrictBindingFacts.Identity(file); + var claims = TypeDefinitionClaims(file, identity, nestedRows[index]); + var rejections = index == 0 ? TypeDefinitionRejections(file, identity, claims, nestedRows[index]) : ""; + return $$""" + namespace Types{{size}} + {{BindingImage.Definitions(file)}} + /-- The synthetic image under its assembly identity. -/ + def pinned : List StrictBinding.PinnedImage := [{ identity := "{{identity}}", image := image }] + /-- The claimed type definitions, as System.Reflection.Metadata reads them. -/ + def claims : List StrictBinding.TypeDefClaim := + [{{string.Join(",\n ", claims.Select(claim => claim.Lean))}}] + {{report}} + theorem prepared : StrictBinding.ok (StrictBinding.prepare pinned) = true := by decide +kernel + {{report}} + theorem checked : StrictBinding.TypeDefsBound pinned claims := by decide +kernel + {{report}} + #print axioms checked + {{rejections}} + end Types{{size}} + + """; + })); + var sorting = string.Concat(sizes.Select((size, index) => $$""" + namespace Sorted{{size}} + /-- The NestedClass table of the image is sorted by its nested type column. -/ + def claims : List StrictBinding.SortedClaim := [{ identity := "{{StrictBindingFacts.Identity(files[index]) + }}", table := {{(int)TableIndex.NestedClass}}, key := StrictBinding.nestedClassKey }] + {{report}} + theorem prepared : StrictBinding.ok (StrictBinding.prepare Types{{size}}.pinned) = true := by + decide +kernel + {{report}} + theorem checked : StrictBinding.TablesSorted Types{{size}}.pinned claims := by decide +kernel + {{report}} + #print axioms checked + end Sorted{{size}} + + """)); + Directory.CreateDirectory(root); + var path = Path.Combine(root, "TypeDefs.lean"); + File.WriteAllText(path, $"import StrictBinding.TypeDefs\nimport Tests.StrictBinding.Mutation\n\n{cases}" + + $"{sorting}{UnsortedNestedClassRejection(files[0])}"); + var measured = CheckHeartbeats(path, package, 2 * sizes.Length, "type definition and sorted table claims"); + long[] checks = measured[..sizes.Length]; + var growth = (double)checks[^1] / checks[0]; + var costs = string.Join(", ", sizes.Zip(checks, (size, cost) => $"{cost} heartbeats at {size} classes")); + if (checks[0] <= 0 || growth > MaximumTableGrowth) + throw new InvalidOperationException($"The type definition check costs {costs}; {growth:F2} times as " + + $"much at {sizes[^1]} classes as at {sizes[0]}"); + Console.WriteLine($"PASS the type definition check reads a fixed part of the tables ({costs}; " + + $"{growth:F2} times at {sizes[^1] / sizes[0]} times the classes)"); + var sortedCost = RequireGrowth([.. nestedRows.Select(rows => rows.Length)], measured[sizes.Length..], + MaximumPerRowGrowth, "NestedClass sortedness check", "row"); + Console.WriteLine($"PASS the sortedness check is linear in the NestedClass rows ({sortedCost})"); + } + + /// The TypeDef rows of the nested types of an image, in ascending order. + static int[] NestedTypeRows(byte[] file) + { + using var reader = new PEReader(System.Collections.Immutable.ImmutableArray.Create(file)); + var metadata = reader.GetMetadataReader(); + int[] rows = [.. metadata.TypeDefinitions.Where(handle => !metadata.GetTypeDefinition(handle) + .GetDeclaringType().IsNil).Select(handle => MetadataTokens.GetRowNumber(handle)).Order()]; + if (rows.Length != metadata.GetTableRowCount(TableIndex.NestedClass)) + throw new InvalidOperationException("Each synthetic nested type must own exactly one NestedClass row."); + return rows; + } + + /// + /// The image with the nested type column of NestedClass row `nestedRow` set to TypeDef row `typeRow`, at the + /// position System.Reflection.Metadata gives the table. + /// + static byte[] WithNestedType(byte[] file, int nestedRow, int typeRow) + { + using var reader = new PEReader(System.Collections.Immutable.ImmutableArray.Create(file)); + var metadata = reader.GetMetadataReader(); + if (metadata.GetTableRowCount(TableIndex.TypeDef) >= 1 << 16) + throw new InvalidOperationException("The nested type column must be two bytes wide."); + var offset = reader.PEHeaders.MetadataStartOffset + metadata.GetTableMetadataOffset(TableIndex.NestedClass) + + (nestedRow - 1) * metadata.GetTableRowSize(TableIndex.NestedClass); + var changed = (byte[])file.Clone(); + changed[offset] = (byte)typeRow; + changed[offset + 1] = (byte)(typeRow >> 8); + return changed; + } + + /// + /// The image with NestedClass row 2 naming TypeDef row 1, below the type row 1 names, is refused as unsorted at + /// row 2. + /// + static string UnsortedNestedClassRejection(byte[] file) + { + var changed = WithNestedType(file, 2, 1); + var identity = StrictBindingFacts.Identity(changed); + return $$""" + namespace Unsorted + {{BindingImage.Definitions(changed)}} + theorem rejects_a_nested_class_table_out_of_order : Tests.StrictBinding.failsWith + (StrictBinding.checkAll StrictBinding.sortedClaimBound + [{ identity := "{{identity}}", image := image }] + [{ identity := "{{identity}}", table := {{(int)TableIndex.NestedClass}}, + key := StrictBinding.nestedClassKey }]) + (.unsortedTable {{(int)TableIndex.NestedClass}} 2) = true := by decide +kernel + #print axioms rejects_a_nested_class_table_out_of_order + end Unsorted + + """; + } + + /// A type definition claim: its row, name, kind and enclosing row, and the claim in Lean. + sealed record TypeDefinitionClaim(int Row, string Name, string Kind, int? Enclosing, string Lean); + + /// + /// Claims about the first class and the last eight rows of an image from `SyntheticTypes`, with every part read + /// by System.Reflection.Metadata. A nested type is placed at its NestedClass row and a type at the top level + /// between the rows that name lower and higher types, given `nestedRows`, the ascending nested type rows. + /// + static TypeDefinitionClaim[] TypeDefinitionClaims(byte[] file, string identity, int[] nestedRows) + { + using var reader = new PEReader(System.Collections.Immutable.ImmutableArray.Create(file)); + var metadata = reader.GetMetadataReader(); + var rows = metadata.GetTableRowCount(TableIndex.TypeDef); + int[] claimed = [2, .. Enumerable.Range(rows - 7, 8)]; + string Bytes(StringHandle text) => + "[" + string.Join(", ", System.Text.Encoding.UTF8.GetBytes(metadata.GetString(text))) + "]"; + (string Namespace, string Name) NamesOf(EntityHandle handle) => handle.Kind switch + { + HandleKind.TypeDefinition => (metadata.GetString(metadata.GetTypeDefinition((TypeDefinitionHandle)handle) + .Namespace), metadata.GetString(metadata.GetTypeDefinition((TypeDefinitionHandle)handle).Name)), + HandleKind.TypeReference => (metadata.GetString(metadata.GetTypeReference((TypeReferenceHandle)handle) + .Namespace), metadata.GetString(metadata.GetTypeReference((TypeReferenceHandle)handle).Name)), + _ => ("", "") + }; + string Ascii(string text) => "[" + string.Join(", ", System.Text.Encoding.UTF8.GetBytes(text)) + "]"; + return [.. claimed.Select(row => + { + var definition = metadata.GetTypeDefinition(MetadataTokens.TypeDefinitionHandle(row)); + var fields = definition.GetFields(); + if (fields.Count == 0) + throw new InvalidOperationException($"Synthetic TypeDef row {row} must own a field."); + var first = MetadataTokens.GetRowNumber(fields.First()); + var baseType = definition.BaseType; + var (baseNamespace, baseName) = baseType.IsNil ? ("", "") : NamesOf(baseType); + var isInterface = (definition.Attributes & TypeAttributes.Interface) != 0; + // Synthetic defines no core library types, including its own type named System.Enum. + var kind = isInterface ? "interface" + : (baseNamespace, baseName) == ("System", "Enum") ? "enum" + : (baseNamespace, baseName) == ("System", "ValueType") ? "valueType" + : "classType"; + var declaring = definition.GetDeclaringType(); + int? enclosing = declaring.IsNil ? null : MetadataTokens.GetRowNumber(declaring); + var nesting = enclosing is { } outer ? $".nested {Array.IndexOf(nestedRows, row) + 1} {outer}" + : $".topLevel {nestedRows.Count(nested => nested < row) + 1}"; + var baseTerm = baseType.IsNil ? ".none" + : baseType.Kind == HandleKind.TypeSpecification ? $".spec {MetadataTokens.GetToken(baseType)}" + : $".named {MetadataTokens.GetToken(baseType)} {Ascii(baseNamespace)} {Ascii(baseName)}"; + return new TypeDefinitionClaim(row, metadata.GetString(definition.Name), kind, enclosing, + $"{{ identity := \"{identity}\", row := {row}, typeNamespace := {Bytes(definition.Namespace)}, " + + $"typeName := {Bytes(definition.Name)}, nesting := {nesting}, kind := .{kind}, " + + $"fields := ({first}, {fields.Count}), base := {baseTerm} }}"); + })]; + } + + /// + /// Rejections that each change one part of a claim that holds: a nested class claimed at the top level, at + /// another NestedClass row and inside another row, the first class claimed nested, `System.Enum` claimed between + /// other NestedClass rows and claimed a core library class, the interface claimed a class, the class over a generic + /// instantiation claimed to extend another TypeSpec row, and the last row claimed to extend another TypeDef. + /// The image whose NestedClass row before the last names the last row's type too refuses the last nested class. + /// + static string TypeDefinitionRejections(byte[] file, string identity, TypeDefinitionClaim[] claims, + int[] nestedRows) + { + var nested = Array.FindIndex(claims, claim => claim.Name == "Inner"); + var lastNested = Array.FindIndex(claims, claim => claim.Name == "Inner2"); + var face = Array.FindIndex(claims, claim => claim.Kind == "interface"); + var localEnum = Array.FindIndex(claims, claim => claim.Name == "Enum"); + var instantiated = Array.FindIndex(claims, claim => claim.Name == "Numbers"); + var last = claims.Length - 1; + string Rejects(string name, int index, string change, string part) => $$""" + theorem {{name}} : Tests.StrictBinding.failsWith (StrictBinding.checkAll + StrictBinding.typeDefClaimBound pinned + [{ ({{claims[index].Lean}} : StrictBinding.TypeDefClaim) with {{change}} }]) + (.typeDefMismatch {{claims[index].Row}} .{{part}}) = true := by decide +kernel + #print axioms {{name}} + + """; + var nestedRow = Array.IndexOf(nestedRows, claims[nested].Row) + 1; + var lastRow = Array.IndexOf(nestedRows, claims[lastNested].Row) + 1; + var duplicated = WithNestedType(file, lastRow - 1, claims[lastNested].Row); + return Rejects("rejects_a_nested_class_at_the_top_level", nested, $"nesting := .topLevel {nestedRow}", + "nesting") + + Rejects("rejects_another_nested_class_row", lastNested, + $"nesting := .nested {lastRow - 1} {claims[lastNested].Enclosing}", "nesting") + + Rejects("rejects_another_enclosing_row", nested, + $"nesting := .nested {nestedRow} {claims[nested].Enclosing + 1}", "nesting") + + Rejects("rejects_a_top_level_class_claimed_nested", 0, "nesting := .nested 1 2", "nesting") + + Rejects("rejects_a_bracket_that_does_not_hold", localEnum, + $"nesting := .topLevel {lastRow + 1}", "nesting") + + $$""" + namespace Duplicated + {{BindingImage.Definitions(duplicated)}} + /-- NestedClass row {{lastRow - 1}} names TypeDef row {{claims[lastNested].Row}}, as the next row does. -/ + theorem rejects_a_nested_class_with_two_rows : Tests.StrictBinding.failsWith (StrictBinding.checkAll + StrictBinding.typeDefClaimBound [{ identity := "{{identity}}", image := image }] + [{{claims[lastNested].Lean}}]) + (.typeDefMismatch {{claims[lastNested].Row}} .nesting) = true := by decide +kernel + #print axioms rejects_a_nested_class_with_two_rows + end Duplicated + + """ + + Rejects("rejects_an_interface_claimed_a_class", face, "kind := .classType", "kind") + + Rejects("rejects_local_system_enum_claimed_a_class", localEnum, "kind := .classType", "kind") + + Rejects("rejects_another_type_spec", instantiated, + $"base := .spec {MetadataTokens.GetToken(MetadataTokens.TypeSpecificationHandle(2))}", "base") + + Rejects("rejects_another_base_definition", last, + $"base := .named {MetadataTokens.GetToken(MetadataTokens.TypeDefinitionHandle(3))} [] []", "base"); + } + + /// + /// A library with `count` classes that each own a field and extend `System.Object`, the odd ones nested in the + /// first, then an enum, a struct, an interface, a class nested in the first class, a `System.Enum` that extends + /// `System.ValueType`, a second class nested in the first, a class that extends `List<int>` and a class that + /// extends the first class. Each of the last eight owns one field, and the last owns the final two rows of the + /// Field table. + /// + static byte[] SyntheticTypes(int count) + { + var metadata = new MetadataBuilder(); + metadata.AddAssembly(metadata.GetOrAddString("Synthetic"), new Version(1, 2, 3, 4), default, default, default, + AssemblyHashAlgorithm.Sha1); + metadata.AddModule(0, metadata.GetOrAddString("Synthetic.dll"), metadata.GetOrAddGuid(Guid.Empty), default, + default); + var runtimeToken = metadata.GetOrAddBlob(Convert.FromHexString("b03f5f7f11d50a3a")); + var runtime = metadata.AddAssemblyReference(metadata.GetOrAddString("System.Runtime"), + new Version(10, 0, 0, 0), default, runtimeToken, default, default); + TypeReferenceHandle SystemType(string name) => + metadata.AddTypeReference(runtime, metadata.GetOrAddString("System"), metadata.GetOrAddString(name)); + var objectType = SystemType("Object"); + var enumType = SystemType("Enum"); + var valueType = SystemType("ValueType"); + var signature = new BlobBuilder(); + new BlobEncoder(signature).Field().Type().Int32(); + var fieldSignature = metadata.GetOrAddBlob(signature); + var listType = metadata.AddTypeReference(runtime, metadata.GetOrAddString("System.Collections.Generic"), + metadata.GetOrAddString("List`1")); + var instance = new BlobBuilder(); + new BlobEncoder(instance).TypeSpecificationSignature().GenericInstantiation(listType, 1, false).AddArgument() + .Int32(); + var listOfInt = metadata.AddTypeSpecification(metadata.GetOrAddBlob(instance)); + var fields = 0; + var typeNamespace = metadata.GetOrAddString("Synthetic"); + TypeDefinitionHandle Define(TypeAttributes attributes, StringHandle space, string name, EntityHandle baseType, + int owned) + { + var first = MetadataTokens.FieldDefinitionHandle(fields + 1); + for (var index = 0; index < owned; index++) + metadata.AddFieldDefinition(FieldAttributes.Public, metadata.GetOrAddString("F" + (fields + index)), + fieldSignature); + fields += owned; + return metadata.AddTypeDefinition(attributes, space, metadata.GetOrAddString(name), baseType, first, + MetadataTokens.MethodDefinitionHandle(1)); + } + metadata.AddTypeDefinition(default, default, metadata.GetOrAddString(""), default, + MetadataTokens.FieldDefinitionHandle(1), MetadataTokens.MethodDefinitionHandle(1)); + var firstClass = Define(TypeAttributes.Public, typeNamespace, "C0", objectType, 1); + for (var index = 1; index < count; index++) + { + var odd = index % 2 == 1; + var defined = Define(odd ? TypeAttributes.NestedPublic : TypeAttributes.Public, + odd ? default : typeNamespace, "C" + index, objectType, 1); + if (odd) + metadata.AddNestedType(defined, firstClass); + } + Define(TypeAttributes.Public | TypeAttributes.Sealed, typeNamespace, "Small", enumType, 1); + Define(TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.SequentialLayout, typeNamespace, "Pair", + valueType, 1); + Define(TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract, typeNamespace, "IShape", + default, 1); + var nestedClass = Define(TypeAttributes.NestedPublic, default, "Inner", objectType, 1); + metadata.AddNestedType(nestedClass, firstClass); + Define(TypeAttributes.Public | TypeAttributes.Abstract, metadata.GetOrAddString("System"), "Enum", valueType, + 1); + metadata.AddNestedType(Define(TypeAttributes.NestedPublic, default, "Inner2", objectType, 1), firstClass); + Define(TypeAttributes.Public, typeNamespace, "Numbers", listOfInt, 1); + Define(TypeAttributes.Public, typeNamespace, "Derived", firstClass, 2); + var builder = new ManagedPEBuilder(PEHeaderBuilder.CreateLibraryHeader(), new MetadataRootBuilder(metadata), + new BlobBuilder()); + var image = new BlobBuilder(); + builder.Serialize(image); + return image.ToArray(); + } + + /// + /// A library named Synthetic whose Assembly row names a public key blob that takes + /// bytes of the `#Blob` heap, with its length prefix. + /// + static byte[] SyntheticAssembly(int size) + { + const int shortBlob = 0x80; + var metadata = new MetadataBuilder(); + var name = metadata.GetOrAddString("Synthetic"); + var keyLength = size <= shortBlob ? size - 1 : size - 2; + var keyBytes = Enumerable.Range(0, keyLength).Select(index => (byte)(index * 7 + 1)).ToArray(); + var key = metadata.GetOrAddBlob(keyBytes); + metadata.AddAssembly(name, new Version(1, 2, 3, 4), default, key, AssemblyFlags.PublicKey, + AssemblyHashAlgorithm.Sha1); + metadata.AddModule(0, metadata.GetOrAddString("Synthetic.dll"), metadata.GetOrAddGuid(Guid.Empty), default, + default); + metadata.AddTypeDefinition(default, default, metadata.GetOrAddString(""), default, + MetadataTokens.FieldDefinitionHandle(1), MetadataTokens.MethodDefinitionHandle(1)); + var builder = new ManagedPEBuilder(PEHeaderBuilder.CreateLibraryHeader(), new MetadataRootBuilder(metadata), + new BlobBuilder()); + var image = new BlobBuilder(); + builder.Serialize(image); + return image.ToArray(); + } + + /// + /// A library named `name` with `count` MethodDef rows without bodies, each named for its row and all with one + /// signature. + /// + static byte[] SyntheticMethods(int count, string name = "Synthetic") + { + var metadata = new MetadataBuilder(); + var assemblyName = metadata.GetOrAddString(name); + metadata.AddAssembly(assemblyName, new Version(1, 2, 3, 4), default, default, default, + AssemblyHashAlgorithm.Sha1); + metadata.AddModule(0, metadata.GetOrAddString(name + ".dll"), metadata.GetOrAddGuid(Guid.Empty), default, + default); + metadata.AddTypeDefinition(default, default, metadata.GetOrAddString(""), default, + MetadataTokens.FieldDefinitionHandle(1), MetadataTokens.MethodDefinitionHandle(1)); + var signature = new BlobBuilder(); + new BlobEncoder(signature).MethodSignature().Parameters(0, returnType => returnType.Void(), _ => { }); + var shared = metadata.GetOrAddBlob(signature); + for (var index = 0; index < count; index++) + metadata.AddMethodDefinition(MethodAttributes.Public | MethodAttributes.Static, default, + metadata.GetOrAddString("M" + index), shared, -1, MetadataTokens.ParameterHandle(1)); + var builder = new ManagedPEBuilder(PEHeaderBuilder.CreateLibraryHeader(), new MetadataRootBuilder(metadata), + new BlobBuilder()); + var image = new BlobBuilder(); + builder.Serialize(image); + return image.ToArray(); + } + + /// + /// Elaborates each paged module the audit generates, also over an extraction with no methods, so a malformed + /// declaration in the generator fails here and not only in an end-to-end audit. + /// + static void CheckGeneratedPagedModulesElaborate(string root, string package) + { + const string imports = "import BindingAudit.Images\nimport Extracted\nimport StrictBinding.Evidence\n"; + const string stubs = """ + import StrictBinding.Evidence + namespace Extracted + def methodEvidence : List CIL.Extraction.MethodEvidence := [] + end Extracted + namespace BindingAudit + def images : List StrictBinding.PinnedImage := [] + end BindingAudit + + """; + Directory.CreateDirectory(root); + foreach (var fact in PagedFacts) + foreach (var (methods, size) in new[] { (3, 1), (0, BindingPageSize(0)) }) + { + var source = PagedFactModuleSource(fact, methods, size).ReplaceLineEndings("\n"); + if (!source.StartsWith(imports, StringComparison.Ordinal)) + throw new InvalidOperationException( + $"The generated {fact} module no longer starts with its imports"); + var path = Path.Combine(root, $"Generated{fact}{methods}.lean"); + File.WriteAllText(path, stubs.ReplaceLineEndings("\n") + source[imports.Length..]); + var run = Execute(["lake", "env", "lean", path], cwd: package); + if (run.Code != 0) + throw new InvalidOperationException( + $"The generated {fact} module for {methods} methods does not elaborate: {run.Output}"); + } + Console.WriteLine("PASS each generated paged module elaborates, also for no methods"); + } + + /// + /// A module stating that every body System.Reflection.Metadata reads is found in the image, composed the way a + /// generated module composes its pages: one theorem per page of claims, each its own kernel evaluation, and the + /// whole from `claimsHold_of_pages`. The page lemmas are checked when the module is built. + /// + static string AllBodiesModule(byte[] file, int? claims) + { + var identity = StrictBindingFacts.Read(file).Last().Text; + var read = StrictBindingFacts.Bodies(file); + // A claim count beyond the bodies the image has repeats them, so the claims can outgrow a fixed image. + var bodies = Enumerable.Range(0, claims ?? read.Count).Select(index => read[index % read.Count]).ToArray(); + var size = BindingPageSize(bodies.Length); + // One definition per claim keeps each list literal small enough for the elaborator's recursion limit. + var parts = bodies.Select(body => + $"{{ identity := \"{identity}\", token := 0x{body.Token:x8}, " + + $"bytes := [{string.Join(", ", body.Bytes)}] }}") + .Chunk(size).ToArray(); + var defs = string.Concat(parts.Select((part, index) => + string.Concat(part.Select((claim, at) => + $"def claim{index}_{at} : StrictBinding.BodyClaim := {claim}\n")) + + $"def claims{index} : List StrictBinding.BodyClaim :=\n " + + $"[{string.Join(", ", part.Select((_, at) => $"claim{index}_{at}"))}]\n\n")); + var joined = Enumerable.Range(0, parts.Length - 1).Reverse().Aggregate($"claims{parts.Length - 1}", + (rest, index) => $"(claims{index} ++ {rest})"); + var lemmas = string.Concat(parts.Select((_, index) => $"theorem bodiesOf{index} : " + + $"StrictBinding.ClaimsHold StrictBinding.bodyClaimBound pinned " + + $"(StrictBinding.claimPage claims {size} {index}) := by\n" + + " decide +kernel\n\n")); + var chain = string.Concat(Enumerable.Range(0, parts.Length).Reverse() + .Select(index => $"(.succ bodiesOf{index} ")); + return $$""" + import StrictBinding.Bind + import CIL.Extraction.FixedDigits + + namespace Scaling + + {{BindingImage.Definitions(file)}} + def pinned : List StrictBinding.PinnedImage := [{ identity := "{{identity}}", image := image }] + + {{defs}}def claims : List StrictBinding.BodyClaim := {{joined}} + + theorem prepared : StrictBinding.ok (StrictBinding.prepare pinned) = true := by decide +kernel + + {{lemmas}}theorem found : StrictBinding.ClaimsHold StrictBinding.bodyClaimBound pinned claims := + StrictBinding.claimsHold_of_pages (size := {{size}}) (count := {{parts.Length}}) prepared + (by decide +kernel) {{chain}}.zero{{new string(')', parts.Length)}} + + end Scaling + """; + } +} diff --git a/Tests/Strings.cs b/Tests/Strings.cs index ceecfde08..9c32457d4 100644 --- a/Tests/Strings.cs +++ b/Tests/Strings.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using Mono.Cecil; using Mono.Cecil.Cil; @@ -13,15 +18,16 @@ var root = At("artifacts/Strings"); Directory.CreateDirectory(root); -foreach (var (proof, entry) in new[] +foreach (var (source, entry) in new[] { - ("StringValues", "Strings::Pick(System.Boolean,System.String,System.String)"), - ("StringLabel", "Strings::Relabel(Examples.Labelled,System.String)"), - ("StringLiterals", "Strings::SameText()"), - ("StringDefault", "Strings::OrNone(System.String)") + ("Examples/Proofs/StringValues.lean", "Strings::Pick(System.Boolean,System.String,System.String)"), + ("Examples/Proofs/StringLabel.lean", "Strings::Relabel(Examples.Labelled,System.String)"), + ("Examples/Proofs/StringLiterals.lean", "Strings::SameText()"), + ("Examples/Proofs/StringDefault.lean", "Strings::OrNone(System.String)") }) { - verification.Verify(assembly, "Examples", entry, At($"Examples/Proofs/{proof}.lean"), Path.Combine(root, proof)); + var proof = Path.GetFileNameWithoutExtension(source); + verification.Verify(assembly, "Examples", entry, At(source), Path.Combine(root, proof)); Console.WriteLine($"PASS {proof}: {entry}"); } diff --git a/Tests/StructMethods.cs b/Tests/StructMethods.cs index b6579223e..f3f6789b6 100644 --- a/Tests/StructMethods.cs +++ b/Tests/StructMethods.cs @@ -2,12 +2,21 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include StoreReturnVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; using Mono.Cecil; using Mono.Cecil.Cil; +Run(["dotnet", "run", "--file", At("Tests/ModifierIdentities.cs")]); + using var verification = new Program(); var assembly = At("Examples/Assembly/bin/Examples.dll"); var root = At("artifacts/StructMethods"); @@ -96,8 +105,8 @@ end ExampleProof Compare(dll, "Arguments", "-" + shortForm); } -// Libraries for older frameworks declare their own IsExternalInit; C# binds the marker by full name -// from any assembly, so an accessor marked by one runs exactly as with System.Runtime's. +// The top-level IsExternalInit marker keeps its namespace and simple name across assemblies. +// An accessor marked by a library's own definition runs exactly as with System.Runtime's. foreach (var scope in new[] { "local-marker", "versioned-marker" }) { var output = Path.Combine(root, "Init-" + scope); @@ -117,9 +126,13 @@ end ExampleProof replacement = local; } else + { + var versioned = new AssemblyNameReference(runtime.Name, new Version(9, 0, 0, 0)) + { PublicKeyToken = runtime.PublicKeyToken }; + module.AssemblyReferences.Add(versioned); replacement = new TypeReference(marker.ModifierType.Namespace, marker.ModifierType.Name, module, - new AssemblyNameReference(runtime.Name, new Version(9, 0, 0, 0)) - { PublicKeyToken = runtime.PublicKeyToken }); + versioned); + } setter.ReturnType = new RequiredModifierType(replacement, marker.ElementType); module.Write(dll); } @@ -131,17 +144,8 @@ end ExampleProof var contract = At("Examples/Proofs/InstanceWriteRead.lean"); verification.Verify(assembly, "Examples", "Counter::WriteRead(ulong)", contract, Path.Combine(root, "UniversalWriteRead")); var wrongOutput = Path.Combine(root, "WrongWriteRead"); -var failed = false; -try -{ - verification.Verify(assembly, "Examples", "Counter::WrongWriteRead(ulong)", contract, wrongOutput); -} -catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) -{ - failed = true; -} -if (!failed || File.Exists(Path.Combine(wrongOutput, "verified.json"))) - throw new InvalidOperationException("Incorrect instance mutation accepted"); +verification.VerifyStoreReturn(assembly, "Counter::WriteRead(ulong)", contract, + assembly, "Counter::WrongWriteRead(ulong)", wrongOutput, 64); // Invalid call metadata must not be mistaken for an instance or static invocation. foreach (var mutation in new[] { "static-call", "explicit-this", "class", "constructor-return" }) @@ -178,7 +182,8 @@ end ExampleProof const string parameterRejection = "except System.Runtime's modreq(InAttribute) directly on a by-reference parameter"; static MethodDefinition Accessor(ModuleDefinition module, string name) => module.GetType("Examples.InitCounter").Methods.Single(method => method.Name == name); -foreach (var mutation in new[] { "other-modreq", "modopt", "non-void", "parameter", "other-namespace" }) +foreach (var mutation in new[] + { "other-modreq", "modopt", "non-void", "parameter", "other-namespace", "flattened-name" }) { var output = Path.Combine(root, "Reject-init-" + mutation); Directory.CreateDirectory(output); @@ -201,6 +206,10 @@ TypeReference ForeignMarker() var target = mutation == "non-void" ? Accessor(module, "get_Value") : setter; switch (mutation) { + case "flattened-name": + setter.ReturnType = new RequiredModifierType(new TypeReference("", + "System.Runtime.CompilerServices.IsExternalInit", module, runtime), marker.ElementType); + break; case "other-modreq": setter.ReturnType = new RequiredModifierType(Modifier("IsVolatile", runtime), marker.ElementType); break; diff --git a/Tests/StructOutlines.cs b/Tests/StructOutlines.cs index 9c445a76b..afd18f075 100644 --- a/Tests/StructOutlines.cs +++ b/Tests/StructOutlines.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.RegularExpressions; diff --git a/Tests/StructuralLabels.cs b/Tests/StructuralLabels.cs index ab795a5f2..8ae98215c 100644 --- a/Tests/StructuralLabels.cs +++ b/Tests/StructuralLabels.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs // One proof file, unedited, must verify against two builds of a method with two loops and // two calls to one helper; the second inserts a statement before the first loop. @@ -13,7 +18,8 @@ string Build(string name, string inserted) var source = Path.Combine(root, name); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Labels.csproj"), """ - net10.0true + net11.0 + true """); File.WriteAllText(Path.Combine(source, "Labels.cs"), "namespace Labels; public static class Calls { " + "public static uint Step(uint x) => x + 1; " + @@ -39,14 +45,14 @@ namespace ExampleProof bind_method root rootBody := "System.UInt32 Labels.Calls::Root(System.UInt32,System.UInt32)" {{bindings}} - -- Each loop body and back edge, and the final call, stated at its label. + -- Each loop body and back edge, and the final call's target and return, stated at its label. theorem correctness : - (rootBody.code.drop {{count}}).take 4 = [.local 1, .const32 1, .sub, .setLocal 1] ∧ - rootBody.code[{{countLatch}}]? = some (.brnonzero {{count}}) ∧ - (rootBody.code.drop {{reduce}}).take 4 = [.local 0, .const32 3, .sub, .setLocal 0] ∧ - rootBody.code[{{reduceLatch}}]? = some (.bgtu {{reduce}}) ∧ - (rootBody.code.drop {{after}}).take 2 = [.call {{callee}} 1, .ret] := - ⟨rfl, rfl, rfl, rfl, rfl⟩ + (rootBody.code.drop {{count}}).take 4 = [.ldarg 1, .ldc_i4 1, .sub, .starg 1] ∧ + rootBody.code[{{countLatch}}]? = some (.brtrue {{count}}) ∧ + (rootBody.code.drop {{reduce}}).take 4 = [.ldarg 0, .ldc_i4 3, .sub, .starg 0] ∧ + rootBody.code[{{reduceLatch}}]? = some (.bgt_un {{reduce}}) ∧ + rootBody.linkAt {{after}} = some (.direct {{callee}}) ∧ rootBody.code[{{after}} + 1]? = some .ret := + ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ end ExampleProof """); diff --git a/Tests/StructuralLabels.lean b/Tests/StructuralLabels.lean index 4df2976ff..56af1de48 100644 --- a/Tests/StructuralLabels.lean +++ b/Tests/StructuralLabels.lean @@ -1,5 +1,6 @@ import Tests.LoopScaffold import CIL.StructuralLabels +import Tests.Support /-! A synthetic extraction: `Root` calls `Step` before two loops and again after them. -/ namespace Extracted @@ -9,12 +10,12 @@ open CIL def method0Signature : String := "System.UInt32 Labels.Calls::Root(System.UInt32)" def method1Signature : String := "System.UInt32 Labels.Calls::Step(System.UInt32)" def method0 : Method := - { code := [.arg 0, .call 1 1, .setLocal 0, - .local 0, .brzero 6, .branch 3, - .local 0, .brnonzero 6, - .arg 0, .call 1 1, .ret], + { code := [.ldarg 0, .call (Tests.callee 1) {}, .stloc 0, + .ldloc 0, .brfalse 6, .br 3, + .ldloc 0, .brtrue 6, + .ldarg 0, .call (Tests.callee 1) {}, .ret], links := [(1, .direct 1), (9, .direct 1)], locals := [.i32 0], returnsValue := true } -def method1 : Method := { code := [.arg 0, .ret], locals := [], returnsValue := true } +def method1 : Method := { code := [.ldarg 0, .ret], locals := [], returnsValue := true } def program : Program := [method0, method1] def signatures : List String := [method0Signature, method1Signature] @@ -24,10 +25,11 @@ namespace Tests.StructuralLabels open CIL -/-- `n` nops, a call to method 1 and a back edge to the call's argument, as one list literal. -/ +/-- `n` nops, a one-argument call and a back edge to the call's argument, as one list literal. -/ macro "long_code% " n:num : term => do - let nops ← (List.range n.getNat).toArray.mapM fun _ => `(Op.nop) - let tail ← #[`(Op.arg 0), `(Op.call 1 1), `(Op.branch $(Lean.quote n.getNat))].mapM id + let nops ← (List.range n.getNat).toArray.mapM fun _ => `(Instruction.nop) + let tail ← #[`(Instruction.ldarg 0), `(Instruction.call (Tests.callee 1)), + `(Instruction.br $(Lean.quote n.getNat))].mapM id `([$(nops ++ tail),*]) bind_method root rootBody := "System.UInt32 Labels.Calls::Root(System.UInt32)" @@ -45,7 +47,7 @@ bind_loop outer := rootBody [0] bind_loop inner := rootBody [1] example : outer = 3 ∧ outer.latch = 5 ∧ inner = 6 ∧ inner.latch = 7 := ⟨rfl, rfl, rfl, rfl⟩ -example : rootBody.code[inner.latch]? = some (.brnonzero inner) := rfl +example : rootBody.code[inner.latch]? = some (.brtrue inner) := rfl example := inner.backEdge /-- @@ -76,9 +78,9 @@ bind_callee step stepBody := "System.UInt32 Labels.Calls::Step(System.UInt32)" f bind_call before := step from rootBody [0] bind_call after := step from rootBody [1] -example : rootBody.code[9]? = some (.call step 1) := step.call +example : rootBody.code[9]? = some (.call (Tests.callee 1)) := step.call example : before = 1 ∧ after = 9 := ⟨rfl, rfl⟩ -example : rootBody.code[before]? = some (.call step 1) := before.call +example : rootBody.code[before]? = some (.call (Tests.callee 1)) := before.call /-- error: rootBody calls @@ -91,12 +93,13 @@ at 2 sites; select one with [i]: bind_call either := step from rootBody -- `relabel` and `targets` agree, so loop detection sees every branch `cil_embed` maps. -example : (Op.switch [3, 0, 7]).targets = [3, 0, 7] ∧ (Op.nativeBinary (.blt 2)).targets = [2] ∧ (Op.call 1 1).targets = [] := +example : (Instruction.switch [3, 0, 7]).targets = [3, 0, 7] ∧ + (Instruction.blt 2).targets = [2] ∧ (Instruction.call (Tests.callee 1)).targets = [] := ⟨rfl, rfl, rfl⟩ -- Extracted bodies run to thousands of ops; labels past the default recursion depth still bind. def longBody : Method := - { code := long_code% 800, locals := [], returnsValue := false } + { code := long_code% 800, links := [(801, .direct 1)], locals := [], returnsValue := false } bind_callee late lateBody := call 1 from longBody /-- The call to the late helper. -/ @@ -105,7 +108,7 @@ bind_call lateCall := late from longBody bind_loop tail := longBody example : lateCall = 801 ∧ tail = 800 ∧ tail.latch = 802 ∧ lateBody = stepBody := ⟨rfl, rfl, rfl, rfl⟩ -example : longBody.code[lateCall]? = some (.call late 1) := lateCall.call +example : longBody.code[lateCall]? = some (.call (Tests.callee 1)) := lateCall.call example := late.call example := tail.backEdge diff --git a/Tests/StructuralSignatures.cs b/Tests/StructuralSignatures.cs new file mode 100644 index 000000000..3f0bd3d37 --- /dev/null +++ b/Tests/StructuralSignatures.cs @@ -0,0 +1,584 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:package Mono.Cecil +#:include PinnedMetadata.cs + +using System.Reflection; +using System.Diagnostics; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using Mono.Cecil; +using Mono.Cecil.Cil; + +var root = Path.GetFullPath("artifacts/StructuralSignatures"); +Directory.CreateDirectory(root); +var extractor = Assembly.LoadFrom(Path.GetFullPath("Extractor/bin/Nethermind.LeanCIL.dll")); +const BindingFlags Instance = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; +const BindingFlags Static = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; +void Require(bool condition, string message) +{ + if (!condition) throw new Exception(message); +} + +string path = Path.Combine(root, "Collision.dll"); +object? escapedSignature = null; +using (var assembly = AssemblyDefinition.CreateAssembly(new("Collision", new(1, 0)), "Collision", ModuleKind.Dll)) +{ + var module = assembly.MainModule; + var runtime = new AssemblyNameReference("System.Runtime", new(11, 0, 0, 0)) + { + PublicKeyToken = Convert.FromHexString("b03f5f7f11d50a3a") + }; + module.AssemblyReferences.Add(runtime); + string argument = module.TypeSystem.Int32.Scope is AssemblyNameReference primitiveScope + ? primitiveScope.FullName + "::System.Int32" : throw new Exception("Primitive scope must be an assembly"); + foreach (string name in new[] { "G`1", "G`1<" + argument + ">" }) + { + var type = new TypeDefinition("Fixture", name, + Mono.Cecil.TypeAttributes.Public | Mono.Cecil.TypeAttributes.Sealed | + Mono.Cecil.TypeAttributes.SequentialLayout, + new TypeReference("System", "ValueType", module, runtime)); + if (name == "G`1") + { + type.GenericParameters.Add(new GenericParameter("T", type)); + var member = new MethodDefinition("ConstantOwner", Mono.Cecil.MethodAttributes.Public | + Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Int32); + type.Methods.Add(member); + member.GenericParameters.Add(new GenericParameter("U", member)); + member.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_1)); + member.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); + } + module.Types.Add(type); + } + var owner = new TypeDefinition("Fixture", "Methods", Mono.Cecil.TypeAttributes.Public); + module.Types.Add(owner); + foreach (var (space, name) in new[] { ("A.B", "C"), ("A", "B.C"), ("Raw", "BadAX"), ("Raw", "BadBX") }) + module.Types.Add(new TypeDefinition(space, name, Mono.Cecil.TypeAttributes.Public)); + owner.Fields.Add(new FieldDefinition("Value", Mono.Cecil.FieldAttributes.Public, module.Types[^4])); + var outer = new TypeDefinition("Fixture", "Outer", Mono.Cecil.TypeAttributes.Public); + outer.NestedTypes.Add(new TypeDefinition("", "Inner", Mono.Cecil.TypeAttributes.NestedPublic)); + outer.NestedTypes.Add(new TypeDefinition("", "Sibling", Mono.Cecil.TypeAttributes.NestedPublic)); + module.Types.Add(outer); + module.Types.Add(new TypeDefinition("Fixture", "Outer/Inner", Mono.Cecil.TypeAttributes.Public)); + var genuine = new TypeReference("System", "Int32", module, module.TypeSystem.Int32.Scope); + owner.Fields.Add(new FieldDefinition("NamedPrimitive", Mono.Cecil.FieldAttributes.Public, genuine)); + var foreign = new AssemblyNameReference("Foreign", new(11, 0, 0, 0)); + module.AssemblyReferences.Add(foreign); + owner.Fields.Add(new FieldDefinition("ForeignPrimitive", Mono.Cecil.FieldAttributes.Public, + new TypeReference("System", "Int32", module, foreign))); + foreach (var (label, scope) in new[] + { + ("RuntimeVersion", new AssemblyNameReference("System.Runtime", new(9, 0, 0, 0)) + { PublicKeyToken = runtime.PublicKeyToken }), + ("CoreLibrary", new AssemblyNameReference("System.Private.CoreLib", new(11, 0, 0, 0)) + { PublicKeyToken = runtime.PublicKeyToken }), + ("AbsentKey", new AssemblyNameReference("Signing", new(1, 0, 0, 0))), + ("ZeroKey", new AssemblyNameReference("Signing", new(1, 0, 0, 0)) { PublicKeyToken = new byte[8] }), + ("CultureA", new AssemblyNameReference("Culture", new(1, 0, 0, 0)) { Culture = "CultAX" }), + ("CultureB", new AssemblyNameReference("Culture", new(1, 0, 0, 0)) { Culture = "CultBX" }) + }) + { + module.AssemblyReferences.Add(scope); + owner.Fields.Add(new FieldDefinition(label, Mono.Cecil.FieldAttributes.Public, + new TypeReference("System", "Int32", module, scope))); + } + var method = new MethodDefinition("Constant", Mono.Cecil.MethodAttributes.Public | + Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Int32); + owner.Methods.Add(method); + method.GenericParameters.Add(new GenericParameter("T", method)); + method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_1)); + method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); + assembly.Write(path); +} +using (var pinned = new PinnedMetadata(extractor, path)) +{ + var generic = new GenericInstanceType(pinned.Module.GetType("Fixture.G`1")); + generic.GenericArguments.Add(pinned.Module.TypeSystem.Int32); + TypeReference[] types = [generic, pinned.Module.Types.Single(type => type.Name.StartsWith("G`1<"))]; + var method = pinned.Module.GetType("Fixture.Methods").Methods.Single(); + var specialization = extractor.GetType("Nethermind.LeanCIL.MethodSpecialization", true)!; + var session = Activator.CreateInstance(specialization, Instance, null, [pinned.Module, "scalar"], null)!; + var instantiate = specialization.GetMethod("Instantiate", Instance)!; + MethodDefinition Close(TypeReference argument) + { + var reference = new GenericInstanceMethod(method); + reference.GenericArguments.Add(argument); + try { return (MethodDefinition)instantiate.Invoke(session, [reference])!; } + catch (TargetInvocationException error) { throw error.InnerException!; } + } + var first = Close(types[0]); + var second = Close(types[1]); + Require(!ReferenceEquals(first, second), "Distinct raw columns reused one specialization clone"); + Require(ReferenceEquals(first, Close(types[0])) && ReferenceEquals(second, Close(types[1])), + "Each exact specialization must reuse its own clone"); + var arguments = specialization.GetMethod("Arguments", Static)!; + Require(ReferenceEquals(((IReadOnlyList)arguments.Invoke(null, [first])!)[0], types[0]) && + ReferenceEquals(((IReadOnlyList)arguments.Invoke(null, [second])!)[0], types[1]), + "Specializations must retain their respective arguments"); + + var signatures = pinned.Context.GetType().GetProperty("Signatures", Instance)!.GetValue(pinned.Context)!; + var signatureType = signatures.GetType(); + var snapshot = signatureType.GetMethod("Type", Instance)!; + object Signature(TypeReference type) + { + try { return snapshot.Invoke(signatures, [type])!; } + catch (TargetInvocationException error) { throw error.InnerException!; } + } + void Equal(TypeReference left, TypeReference right, bool expected, string label) => + Require(Signature(left).Equals(Signature(right)) == expected, label); + void Reject(Action action, string label) + { + try { action(); } + catch (InvalidDataException) { return; } + catch (BadImageFormatException) { return; } + throw new Exception(label); + } + var module = pinned.Module; + var split = module.Types.Where(type => type.Namespace is "A.B" or "A").ToArray(); + Equal(split[0], split[1], false, "Namespace/name columns were flattened"); + Equal(module.GetType("Fixture.Outer").NestedTypes[0], + module.Types.Single(type => type.Name == "Outer/Inner"), false, "Nesting was flattened"); + var owner = module.GetType("Fixture.Methods"); + var fieldCheck = extractor.GetType("Nethermind.LeanCIL.MetadataValidation", true)! + .GetMethod("InstanceFieldOf", Static)!; + void Field(TypeReference type) + { + try { fieldCheck.Invoke(null, [new FieldReference("Value", type, owner)]); } + catch (TargetInvocationException error) { throw error.InnerException!; } + } + Field(split[0]); + Reject(() => Field(split[1]), "Colliding field types passed member/definition validation"); + var constrained = extractor.GetType("Nethermind.LeanCIL.ConstrainedCalls", true)!; + var indexType = constrained.GetNestedType("TargetIndex", Static)!; + var genericOwnerMethod = generic.ElementType.Resolve().Methods.Single(); + var otherOwner = new GenericInstanceType(generic.ElementType); + otherOwner.GenericArguments.Add(module.TypeSystem.UInt32); + MethodDefinition CloseOwner(GenericInstanceType declaring) + { + var member = new MethodReference(genericOwnerMethod.Name, genericOwnerMethod.ReturnType, declaring) + { + CallingConvention = genericOwnerMethod.CallingConvention + }; + member.GenericParameters.Add(new GenericParameter("U", member)); + var reference = new GenericInstanceMethod(member); + reference.GenericArguments.Add(module.TypeSystem.Int64); + try { return (MethodDefinition)instantiate.Invoke(session, [reference])!; } + catch (TargetInvocationException error) { throw error.InnerException!; } + } + var firstOwner = CloseOwner(generic); + var secondOwner = CloseOwner(otherOwner); + Require(!ReferenceEquals(firstOwner, secondOwner), "Distinct owner arguments reused one specialization clone"); + Require(ReferenceEquals(firstOwner, CloseOwner(generic)) && ReferenceEquals(secondOwner, CloseOwner(otherOwner)), + "Closed owners did not retain their respective specialization clones"); + var targetIndex = Activator.CreateInstance(indexType, Instance, null, + [new[] { first, second, firstOwner, secondOwner }], null)!; + var resolutionType = constrained.GetNestedType("Resolution", Static)!; + foreach (var (argument, expected) in new[] { (types[0], first), (types[1], second) }) + { + var resolution = Activator.CreateInstance(resolutionType, Instance, null, + [owner, method, method, owner, new[] { argument }], null)!; + Require(ReferenceEquals(indexType.GetMethod("Find", Instance)!.Invoke(targetIndex, [resolution]), expected), + "Constrained lookup disagrees with specialization identity"); + } + foreach (var (declaring, expected) in new[] { (generic, firstOwner), (otherOwner, secondOwner) }) + { + var resolution = Activator.CreateInstance(resolutionType, Instance, null, + [generic.ElementType.Resolve(), genericOwnerMethod, genericOwnerMethod, declaring, + new[] { module.TypeSystem.Int64 }], null)!; + Require(ReferenceEquals(indexType.GetMethod("Find", Instance)!.Invoke(targetIndex, [resolution]), expected), + "Constrained lookup merged different closed generic owners"); + var retained = (IReadOnlyList)specialization.GetMethod("OwnerArguments", Static)! + .Invoke(null, [expected])!; + Require(retained.Count == 1 && ReferenceEquals(retained[0], declaring.GenericArguments[0]), + "Specialized owner arguments were not retained"); + } + var namedPrimitive = owner.Fields.Single(field => field.Name == "NamedPrimitive").FieldType; + Equal(module.TypeSystem.Int32, namedPrimitive, true, "Equal scoped primitive rows differ"); + Equal(module.TypeSystem.Int32, owner.Fields.Single(field => field.Name == "ForeignPrimitive").FieldType, + false, "Primitive scope was erased"); + foreach (string label in new[] { "RuntimeVersion", "CoreLibrary" }) + Equal(namedPrimitive, owner.Fields.Single(field => field.Name == label).FieldType, false, + "Runtime scopes were unified"); + Equal(owner.Fields.Single(field => field.Name == "AbsentKey").FieldType, + owner.Fields.Single(field => field.Name == "ZeroKey").FieldType, false, "Absent and zero signing keys agree"); + Reject(() => Signature(new TypeReference("System", "Int32", module, module.TypeSystem.Int32.Scope)), + "Unregistered RID-zero primitive was accepted"); + object immutable = Signature(split[0]); + split[0].Namespace = "Mutated"; + split[0].Name = "Name"; + Require(immutable.Equals(Signature(split[0])), "Raw row identity followed mutable names"); + object primitiveIdentity = Signature(module.TypeSystem.Int32); + var primitiveScope = module.TypeSystem.Int32.Scope; + var primitiveToken = module.TypeSystem.Int32.MetadataToken; + module.TypeSystem.Int32.Name = "Edited"; + module.TypeSystem.Int32.Namespace = "Edited"; + module.TypeSystem.Int32.Scope = foreignScope(); + module.TypeSystem.Int32.MetadataToken = new MetadataToken(TokenType.TypeRef, 12345); + Require(primitiveIdentity.Equals(Signature(module.TypeSystem.Int32)), "Registered primitive identity changed"); + module.TypeSystem.Int32.Name = "Int32"; + module.TypeSystem.Int32.Namespace = "System"; + module.TypeSystem.Int32.Scope = primitiveScope; + module.TypeSystem.Int32.MetadataToken = primitiveToken; + IMetadataScope foreignScope() => owner.Fields.Single(field => field.Name == "ForeignPrimitive").FieldType.Scope; + var vector = new ArrayType(split[0]); + var bounded = new ArrayType(split[0], 1); + bounded.Dimensions[0] = new ArrayDimension(0, 9); + Equal(vector, bounded, false, "Vector and bounded array agree"); + var otherBounds = new ArrayType(split[0], 1); + otherBounds.Dimensions[0] = new ArrayDimension(0, 10); + Equal(bounded, otherBounds, false, "Array bounds were erased"); + Equal(new RequiredModifierType(split[0], split[1]), new OptionalModifierType(split[0], split[1]), + false, "Modifier kind was erased"); + Equal(new RequiredModifierType(split[0], split[1]), new RequiredModifierType(split[1], split[0]), + false, "Modifier columns were reordered"); + var pointer = new FunctionPointerType { ReturnType = split[0] }; + var otherPointer = new FunctionPointerType { ReturnType = split[0] }; + Equal(pointer, otherPointer, true, "Equal function pointers differ"); + otherPointer.HasThis = true; + Equal(pointer, otherPointer, false, "Function pointer flags were erased"); + otherPointer.HasThis = false; + otherPointer.CallingConvention = MethodCallingConvention.C; + Equal(pointer, otherPointer, false, "Function pointer convention was erased"); + var wrapper = new GenericInstanceType(generic.ElementType); + wrapper.GenericArguments.Add(split[0]); + object before = Signature(wrapper); + wrapper.GenericArguments[0] = split[1]; + Require(!before.Equals(Signature(wrapper)), "Wrapper mutation was hidden by a cache"); + Equal(method.GenericParameters[0], generic.ElementType.GenericParameters[0], false, + "Method and type parameter positions were merged"); + var otherParameterOwner = new MethodReference("Other", module.TypeSystem.Void, owner); + otherParameterOwner.GenericParameters.Add(new GenericParameter("DifferentName", otherParameterOwner)); + Equal(method.GenericParameters[0], otherParameterOwner.GenericParameters[0], true, + "Positional parameters depend on their owner objects or names"); + var cycle = new GenericInstanceType(generic.ElementType); + cycle.GenericArguments.Add(cycle); + Reject(() => Signature(cycle), "Cyclic signature was accepted"); + var invalid = new TypeReference("Any", "Name", module, module) { MetadataToken = new(TokenType.TypeRef, 999999) }; + Reject(() => Signature(invalid), "Out-of-range type row was accepted"); + invalid.MetadataToken = new MetadataToken(TokenType.Method, 1); + Reject(() => Signature(invalid), "Wrong token kind was accepted"); + var copy = new TypeReference("Ignored", "Ignored", module, module) + { + MetadataToken = split[0].MetadataToken + }; + Equal(copy, split[0], true, "Distinct wrappers of the same raw row differ"); + using (var nested = new PinnedMetadata(extractor, path)) + { + Reject(() => Signature(copy), "Inactive session accepted a signature query"); + var nestedSignatures = nested.Context.GetType().GetProperty("Signatures", Instance)!.GetValue(nested.Context)!; + object nestedValue = snapshot.Invoke(nestedSignatures, [nested.Module.GetType("A.B.C")])!; + Reject(() => nestedValue.Equals(immutable), "Cross-session handles compared successfully"); + } + Require(immutable.Equals(Signature(copy)), "Restoring a nested session lost existing identities"); + var register = pinned.Context.GetType().GetMethod("RegisterIdentityImage", Instance)!; + var unregister = pinned.Context.GetType().GetMethod("UnregisterIdentityImage", Instance)!; + var keyQuery = signatureType.GetMethod("Specialization", Instance)!; + TypeReference removedType; + GenericParameter removedParameter; + using (var imported = ModuleDefinition.ReadModule(path)) + { + register.Invoke(pinned.Context, [imported, File.ReadAllBytes(path)]); + var importedType = imported.Types.Single(type => type.Namespace == "A.B"); + removedType = importedType; + var importedMethod = imported.GetType("Fixture.Methods").Methods.Single(); + removedParameter = importedMethod.GenericParameters[0]; + Equal(method.GenericParameters[0], removedParameter, true, + "Live parameter modules changed positional identity"); + _ = keyQuery.Invoke(signatures, [importedMethod, importedMethod.DeclaringType, new[] { importedType }]); + Equal(copy, importedType, true, "Equal rows in separate images differ"); + try { register.Invoke(pinned.Context, [imported, File.ReadAllBytes(path)]); } + catch (TargetInvocationException error) when (error.InnerException is InvalidDataException) { } + Equal(copy, importedType, true, "Duplicate registration corrupted a live module"); + unregister.Invoke(pinned.Context, [imported]); + Reject(() => Signature(importedType), "Removed module retained a usable signature cache"); + Reject(() => Signature(removedParameter), "Generic-only query bypassed module unregistration"); + try + { + keyQuery.Invoke(signatures, [importedMethod, importedMethod.DeclaringType, new[] { importedType }]); + throw new Exception("Removed module retained a usable specialization key"); + } + catch (TargetInvocationException error) when (error.InnerException is InvalidDataException) { } + } + using (var reopened = ModuleDefinition.ReadModule(path)) + { + register.Invoke(pinned.Context, [reopened, File.ReadAllBytes(path)]); + Equal(copy, reopened.Types.Single(type => type.Namespace == "A.B"), true, + "Reopened image has a different raw identity"); + Reject(() => Signature(removedType), "Reopening revived an earlier module object"); + Reject(() => Signature(removedParameter), "Reopening revived an earlier generic parameter owner"); + unregister.Invoke(pinned.Context, [reopened]); + } + using (var rejected = ModuleDefinition.ReadModule(path)) + { + var originalScope = rejected.TypeSystem.Object.Scope; + rejected.TypeSystem.Object.Scope = new AssemblyNameReference("Fabricated", new(1, 0)); + try + { + register.Invoke(pinned.Context, [rejected, File.ReadAllBytes(path)]); + throw new Exception("Registration accepted an unproven primitive scope"); + } + catch (TargetInvocationException error) when (error.InnerException is InvalidDataException) { } + Reject(() => Signature(rejected.TypeSystem.Int32), "Failed preparation published primitive objects"); + Require(immutable.Equals(Signature(copy)), "Failed registration damaged an existing module"); + rejected.TypeSystem.Object.Scope = originalScope; + register.Invoke(pinned.Context, [rejected, File.ReadAllBytes(path)]); + unregister.Invoke(pinned.Context, [rejected]); + } + + TypeReference Diamond(int depth, TypeReference leaf) + { + for (int index = 0; index < depth; index++) + { + var node = new GenericInstanceType(generic.ElementType); + node.GenericArguments.Add(leaf); + node.GenericArguments.Add(leaf); + leaf = node; + } + return leaf; + } + long Counter(string name) => (long)signatureType.GetProperty(name, Instance)!.GetValue(signatures)!; + var orderedQuery = signatureType.GetMethod("Ordered", Instance)!; + var rootSets = new[] { 64, 256, 1024 }.Select(size => Enumerable.Range(0, size) + .Select(_ => (TypeReference)new RequiredModifierType(split[0], new ArrayType(split[1]))).ToArray()).ToArray(); + foreach (var roots in rootSets) orderedQuery.Invoke(signatures, [roots]); + long previousWork = 0; + long previousAllocation = 0; + for (int index = 0; index < rootSets.Length; index++) + { + long nodes = Counter("NodeVisits"), edges = Counter("EdgeVisits"), probes = Counter("TrieProbes"); + long start = GC.GetAllocatedBytesForCurrentThread(); + orderedQuery.Invoke(signatures, [rootSets[index]]); + long allocated = GC.GetAllocatedBytesForCurrentThread() - start; + long work = Counter("NodeVisits") - nodes + Counter("EdgeVisits") - edges + Counter("TrieProbes") - probes; + Require(previousWork == 0 || work <= previousWork * 6, + "Independent ordered signatures have superlinear traversal growth"); + Require(previousAllocation == 0 || allocated <= previousAllocation * 5 + 16384, + "Independent ordered signatures have superlinear allocation growth"); + Console.WriteLine($"Ordered {rootSets[index].Length}: {work} visits and probes, {allocated} bytes/pass"); + previousWork = work; + previousAllocation = allocated; + } + foreach (int size in new[] { 64, 256, 1024 }) + { + var left = Diamond(size, split[0]); + var right = Diamond(size, split[0]); + long nodes = Counter("NodeVisits"), edges = Counter("EdgeVisits"), probes = Counter("TrieProbes"); + long allocated = GC.GetAllocatedBytesForCurrentThread(); + var timer = Stopwatch.StartNew(); + Equal(left, right, true, "Independently allocated equal DAGs differ"); + timer.Stop(); + Require(Counter("NodeVisits") - nodes == 2 * (size + 2), "DAG nodes were revisited"); + Require(Counter("EdgeVisits") - edges == 6 * size, "DAG edges were expanded"); + Require(Counter("TrieProbes") - probes <= 12 * size + 30, "Signature interning expanded a DAG"); + Equal(left, Diamond(size, split[1]), false, "Unequal DAG leaf was erased"); + var differentSharing = new GenericInstanceType(generic.ElementType); + differentSharing.GenericArguments.Add(Diamond(size - 1, split[0])); + differentSharing.GenericArguments.Add(Diamond(size - 1, split[0])); + Require(!ReferenceEquals(differentSharing.GenericArguments[0], differentSharing.GenericArguments[1]), + "Different-sharing fixture reused its child objects"); + Equal(left, differentSharing, true, "Canonical identity depends on DAG sharing"); + differentSharing.GenericArguments[1] = Diamond(size - 1, split[1]); + Equal(left, differentSharing, false, "Different-sharing comparison erased an unequal leaf"); + var wide = new GenericInstanceType(generic.ElementType); + for (int index = 0; index < size; index++) wide.GenericArguments.Add(left); + long wideNodes = Counter("NodeVisits"), wideEdges = Counter("EdgeVisits"); + _ = Signature(wide); + Require(Counter("NodeVisits") - wideNodes == size + 3 && + Counter("EdgeVisits") - wideEdges == 4 * size + 1, "Wide shared arguments were expanded"); + var ordered = signatureType.GetMethod("Ordered", Instance)!; + long parameterNodes = Counter("NodeVisits"), parameterEdges = Counter("EdgeVisits"); + object orderedTypes = ordered.Invoke(signatures, [Enumerable.Repeat(left, size)])!; + Require(Counter("NodeVisits") - parameterNodes == size + 2 && + Counter("EdgeVisits") - parameterEdges == 3 * size, "Shared parameter roots were traversed repeatedly"); + Require(orderedTypes.Equals(ordered.Invoke(signatures, [Enumerable.Repeat(right, size)])), + "Equal ordered parameters differ"); + Console.WriteLine($"DAG {size}: {timer.Elapsed.TotalMilliseconds:F3} ms, " + + $"{GC.GetAllocatedBytesForCurrentThread() - allocated} bytes"); + } + var foreignOwner = new MethodDefinition("Foreign", Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void); + foreignOwner.GenericParameters.Add(new GenericParameter("T", foreignOwner)); + method.Body.Variables.Add(new VariableDefinition(foreignOwner.GenericParameters[0])); + var freshSpecializations = Activator.CreateInstance(specialization, Instance, null, [module, "scalar"], null)!; + var foreignBody = new GenericInstanceMethod(method); + foreignBody.GenericArguments.Add(types[0]); + try + { + instantiate.Invoke(freshSpecializations, [foreignBody]); + throw new Exception("Foreign generic parameter owner was substituted"); + } + catch (TargetInvocationException error) when (error.InnerException is InvalidDataException failure && + failure.Message.Contains("Foreign generic parameter owner", StringComparison.Ordinal)) { } + method.Body.Variables.Clear(); + escapedSignature = Signature(copy); +} +try +{ + escapedSignature!.Equals(escapedSignature); + throw new Exception("Disposed signature session remained usable"); +} +catch (InvalidDataException) { } + +foreach (int size in new[] { 64, 256, 1024 }) +{ + string nestingPath = Path.Combine(root, $"Nesting{size}.dll"); + using (var assembly = AssemblyDefinition.CreateAssembly(new("Nesting", new(1, 0)), "Nesting", ModuleKind.Dll)) + { + var module = assembly.MainModule; + var parent = new TypeDefinition("Fixture", "Root", Mono.Cecil.TypeAttributes.Public, module.TypeSystem.Object); + module.Types.Add(parent); + for (int index = 1; index < size; index++) + { + var child = new TypeDefinition("", "Child", Mono.Cecil.TypeAttributes.NestedPublic, + module.TypeSystem.Object); + parent.NestedTypes.Add(child); + parent = child; + } + assembly.Write(nestingPath); + } + using var pinned = new PinnedMetadata(extractor, nestingPath); + var signatures = pinned.Context.GetType().GetProperty("Signatures", Instance)!.GetValue(pinned.Context)!; + var snapshot = signatures.GetType().GetMethod("Type", Instance)!; + var pending = new Stack(pinned.Module.Types.Skip(1)); + var levels = new List(); + while (pending.TryPop(out var type)) + { + levels.Add(type); + foreach (var child in type.NestedTypes) pending.Push(child); + } + foreach (var type in levels.AsEnumerable().Reverse()) snapshot.Invoke(signatures, [type]); + long rows = (long)signatures.GetType().GetProperty("RawRows", Instance)!.GetValue(signatures)!; + Require(rows == size, "All nesting prefixes repeatedly decoded their ancestors"); + Console.WriteLine($"Nesting {size}: {rows} raw row visits"); +} + +byte[] invalidNames = File.ReadAllBytes(path); +foreach (var (name, replacement) in new[] { ("BadAX", (byte)0xc0), ("BadBX", (byte)0xc1), + ("CultAX", (byte)0xc0), ("CultBX", (byte)0xc1) }) +{ + int offset = invalidNames.AsSpan().IndexOf(System.Text.Encoding.UTF8.GetBytes(name)); + Require(offset >= 0, "Raw name fixture entry is absent"); + invalidNames[offset + name.Length - 2] = replacement; +} +string invalidPath = Path.Combine(root, "InvalidNames.dll"); +File.WriteAllBytes(invalidPath, invalidNames); +using (var pinned = new PinnedMetadata(extractor, invalidPath)) +{ + var types = pinned.Module.Types.Where(type => type.Namespace == "Raw").ToArray(); + Require(types[0].FullName == types[1].FullName, "Invalid UTF-8 displays must collide"); + var signatures = pinned.Context.GetType().GetProperty("Signatures", Instance)!.GetValue(pinned.Context)!; + var snapshot = signatures.GetType().GetMethod("Type", Instance)!; + Require(!snapshot.Invoke(signatures, [types[0]])!.Equals(snapshot.Invoke(signatures, [types[1]])), + "Distinct raw UTF-8 names were merged"); + var fields = pinned.Module.GetType("Fixture.Methods").Fields; + var cultureA = fields.Single(field => field.Name == "CultureA").FieldType; + var cultureB = fields.Single(field => field.Name == "CultureB").FieldType; + Require(cultureA.Scope.ToString() == cultureB.Scope.ToString(), "Invalid culture displays must collide"); + Require(!snapshot.Invoke(signatures, [cultureA])!.Equals(snapshot.Invoke(signatures, [cultureB])), + "Distinct raw culture bytes were merged"); +} + +foreach (bool duplicate in new[] { true, false }) +{ + byte[] malformed = File.ReadAllBytes(path); + using (var pe = new PEReader(new MemoryStream(malformed))) + { + var reader = System.Reflection.Metadata.PEReaderExtensions.GetMetadataReader(pe); + int start = pe.PEHeaders.MetadataStartOffset + reader.GetTableMetadataOffset(TableIndex.NestedClass); + int width = reader.GetTableRowSize(TableIndex.NestedClass) / 2; + malformed.AsSpan(start, width).CopyTo(malformed.AsSpan(start + (duplicate ? width * 2 : width), width)); + } + string malformedPath = Path.Combine(root, duplicate ? "DuplicateNesting.dll" : "CyclicNesting.dll"); + File.WriteAllBytes(malformedPath, malformed); + using var pinned = new PinnedMetadata(extractor, malformedPath); + var signatures = pinned.Context.GetType().GetProperty("Signatures", Instance)!.GetValue(pinned.Context)!; + var snapshot = signatures.GetType().GetMethod("Type", Instance)!; + var nested = new TypeReference("Ignored", "Ignored", pinned.Module, pinned.Module) + { + MetadataToken = new MetadataToken(TokenType.TypeDef, 8) + }; + using (var pe = new PEReader(new MemoryStream(malformed))) + { + var reader = System.Reflection.Metadata.PEReaderExtensions.GetMetadataReader(pe); + int start = pe.PEHeaders.MetadataStartOffset + reader.GetTableMetadataOffset(TableIndex.NestedClass); + int width = reader.GetTableRowSize(TableIndex.NestedClass) / 2; + uint row = width == 2 ? BitConverter.ToUInt16(malformed, start) : BitConverter.ToUInt32(malformed, start); + nested.MetadataToken = new MetadataToken(TokenType.TypeDef, row); + } + for (int attempt = 0; attempt < 2; attempt++) + { + try { snapshot.Invoke(signatures, [nested]); } + catch (TargetInvocationException error) when (error.InnerException is InvalidDataException) { continue; } + throw new Exception("Malformed nesting was accepted on a repeated query"); + } +} + +foreach (int size in new[] { 64, 256, 1024 }) +{ + string suffixPath = Path.Combine(root, $"Suffix{size}.dll"); + using (var assembly = AssemblyDefinition.CreateAssembly(new("Suffix", new(1, 0)), "Suffix", ModuleKind.Dll)) + { + for (int index = 0; index < size; index++) + assembly.MainModule.Types.Add(new TypeDefinition("Suffix", new string('x', size - index) + "z", + Mono.Cecil.TypeAttributes.Public, assembly.MainModule.TypeSystem.Object)); + assembly.Write(suffixPath); + } + using var stream = File.OpenRead(suffixPath); + using var pe = new PEReader(stream); + var reader = System.Reflection.Metadata.PEReaderExtensions.GetMetadataReader(pe); + int[] offsets = reader.TypeDefinitions.Select(handle => + MetadataTokens.GetHeapOffset(reader.GetTypeDefinition(handle).Name)) + .Skip(1).ToArray(); + Require(offsets.Max() - offsets.Min() == size - 1, "Fixture heap does not contain overlapping suffixes"); + using var pinned = new PinnedMetadata(extractor, suffixPath); + var signatures = pinned.Context.GetType().GetProperty("Signatures", Instance)!.GetValue(pinned.Context)!; + var snapshot = signatures.GetType().GetMethod("Type", Instance)!; + foreach (var type in pinned.Module.Types.Skip(1)) snapshot.Invoke(signatures, [type]); + long visits = (long)signatures.GetType().GetProperty("HeapBytes", Instance)!.GetValue(signatures)!; + Require(visits <= size + 32, "Overlapping string suffixes were copied repeatedly"); + Console.WriteLine($"Suffix {size}: {visits} heap-byte visits"); +} + +foreach (int size in new[] { 64, 256, 1024 }) +{ + string keyPath = Path.Combine(root, $"SharedKey{size}.dll"); + using (var assembly = AssemblyDefinition.CreateAssembly(new("SharedKey", new(1, 0)), "SharedKey", ModuleKind.Dll)) + { + var module = assembly.MainModule; + var owner = new TypeDefinition("Fixture", "Keys", Mono.Cecil.TypeAttributes.Public, module.TypeSystem.Object); + module.Types.Add(owner); + for (int index = 0; index < size; index++) + { + var scope = new AssemblyNameReference("Key" + index, new(1, 0, 0, 0)) { PublicKey = new byte[size] }; + module.AssemblyReferences.Add(scope); + owner.Fields.Add(new FieldDefinition("F" + index, Mono.Cecil.FieldAttributes.Public, + new TypeReference("Fixture", "Value", module, scope))); + } + assembly.Write(keyPath); + } + using var pinned = new PinnedMetadata(extractor, keyPath); + object image = pinned.Context.GetType().GetMethod("IdentityMetadata", Instance)!.Invoke(pinned.Context, + [pinned.Module])!; + var reader = (System.Reflection.Metadata.MetadataReader)image.GetType().GetProperty("Reader", Instance)! + .GetValue(image)!; + var keyHandles = reader.AssemblyReferences.Select(reader.GetAssemblyReference) + .Where(reference => reader.GetString(reference.Name).StartsWith("Key", StringComparison.Ordinal)) + .Select(reference => reference.PublicKeyOrToken).ToArray(); + Require(keyHandles.Length == size && keyHandles.Distinct().Count() == 1, "Signing blobs were not shared"); + long Count(string name) => (long)image.GetType().GetProperty(name, Instance)!.GetValue(image)!; + long bytes = Count("SigningBytes"), reads = Count("SigningReads"); + var signatures = pinned.Context.GetType().GetProperty("Signatures", Instance)!.GetValue(pinned.Context)!; + var snapshot = signatures.GetType().GetMethod("Type", Instance)!; + foreach (var field in pinned.Module.GetType("Fixture.Keys").Fields) snapshot.Invoke(signatures, [field.FieldType]); + Require(Count("SigningBytes") - bytes == size && Count("SigningReads") - reads == 1, + "Shared signing blob was repeatedly decoded or hashed"); + var signingKey = image.GetType().GetMethod("SigningKey", Instance)!; + object invalid = signingKey.Invoke(image, [keyHandles[0], false])!; + Require(!(bool)invalid.GetType().GetProperty("Valid", Instance)!.GetValue(invalid)!, + "Full public key was accepted as an eight-byte token"); + Require(ReferenceEquals(invalid, signingKey.Invoke(image, [keyHandles[0], false])), + "Invalid key width was decoded again"); + Require(Count("SigningBytes") - bytes == 2 * size && Count("SigningReads") - reads == 2, + "Signing interpretation modes shared an invalid normalization or repeated work"); + Console.WriteLine($"Shared key {size}: {size} bytes, one normalization"); +} +Console.WriteLine("Structural signature checks passed"); diff --git a/Tests/SummaryCalls.cs b/Tests/SummaryCalls.cs new file mode 100644 index 000000000..5be84b717 --- /dev/null +++ b/Tests/SummaryCalls.cs @@ -0,0 +1,424 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs +#:include GeneratedEvidence.cs +#:include RejectedEvidence.cs +#:include SavedSummaries.cs +#:include ContractVariants.cs +#:include ../scripts/ProofCostReport.cs +#:include ../scripts/CertifiedLibraryDonor.cs +#:include ../scripts/ExportVerificationSuites.cs +#:include ../scripts/VerificationGroupRunner.cs +#:include ../scripts/VerificationSuites.cs + +using System.Globalization; +using System.Text.RegularExpressions; + +// Calls::Mutate{n} stores its argument in a local and passes the local by reference to Mutate n times. The proof of +// each size states the result twice: by stepping every call, and from Mutate's decoded summary at each call. The +// summary of Decrement in the loop of Examples.Loops::CountCalls is checked against mutations of its own, and the +// summary of the vector callee Examples.DivisionIntrinsics::UnsafeLow against a forged original. +int[] sizes = [1, 4, 16]; +// Most times as much as a call costs through the summary from 1 to 4 calls that it may cost from 4 to 16. +const double AllowedCallGrowth = 1.6; +var directory = At("artifacts/SummaryCalls"); +var source = Path.Combine(directory, "Assembly"); +var proofs = Path.Combine(directory, "Proofs"); +Directory.CreateDirectory(source); +Directory.CreateDirectory(proofs); +File.WriteAllText(Path.Combine(source, "Fixture.csproj"), """ +net11.0 +true +"""); +File.WriteAllText(Path.Combine(source, "Fixture.cs"), FixtureSource(sizes)); +Run(["dotnet", "build", Path.Combine(source, "Fixture.csproj"), "-c", "Release"]); +var assembly = Path.Combine(source, "bin/Fixture.dll"); +using var verification = new Program(); +foreach (var calls in sizes) +{ + var proof = Path.Combine(proofs, $"Mutate{calls}.lean"); + File.WriteAllText(proof, MutateProof(calls)); + verification.Verify(assembly, "Fixture", $"System.UInt64 Calls::Mutate{calls}(System.UInt64)", proof, + Path.Combine(directory, $"Mutate{calls}")); + Console.WriteLine($"PASS Mutate{calls} by stepping every call and through the decoded summary of Mutate"); +} + +// Mutate's summary exists so that a caller pays for Mutate's body once: through it, the caller's proof costs about +// the same per call at any number of calls, and less than stepping every call. Counted heartbeats decide. +verification.Library(CommonInputs()); +var measurement = ProofCostReport.Measure(At("."), verification.library, Path.Combine(directory, "Cost"), + false, [directory], [], 2, "SummaryCalls", output => sizes.Any(calls => output == $"Mutate{calls}")); +if (measurement.Unmeasured.Count != 0) + throw new InvalidOperationException("Unmeasured: " + string.Join("; ", measurement.Unmeasured)); +long Cost(int calls, string theorem, string measure) => measurement.Rows.Single(row => + row.Group == $"Mutate{calls}/Proof" && row.Name == "ExampleProof." + theorem).Measures[measure]; +if (SummaryCallGrowth(sizes, [.. sizes.Select(calls => (long)calls * calls)]) <= AllowedCallGrowth) + throw new InvalidOperationException("The growth bound accepts a caller whose cost is quadratic in its calls"); +foreach (var measure in new[] { "heartbeats", "kernelHeartbeats" }) +{ + var growth = SummaryCallGrowth(sizes, [.. sizes.Select(calls => Cost(calls, "correctness", measure))]); + if (growth > AllowedCallGrowth) + throw new InvalidOperationException(string.Create(CultureInfo.InvariantCulture, + $"Through the summary, a call costs {growth:F2} times as many {measure} from 4 to 16 calls as from 1 to " + + $"4; the limit is {AllowedCallGrowth}")); + var summarized = new[] { "correctness", "mutate_holds", "mutate_summary", "bodies_decode" } + .Sum(theorem => Cost(sizes[^1], theorem, measure)); + var stepped = Cost(sizes[^1], "correctness_stepped", measure); + if (summarized >= stepped) + throw new InvalidOperationException( + $"At {sizes[^1]} calls the proof through the summary costs {summarized} {measure}, stepping {stepped}"); + Console.WriteLine(string.Create(CultureInfo.InvariantCulture, + $"PASS through the summary a call costs {growth:F2} times as many {measure} from 4 to 16 calls as from 1 " + + $"to 4, and {sizes[^1]} calls cost {summarized} against {stepped} stepped")); +} + +// The contract of Mutate without its write, and a caller that leaves Mutate's precondition unproved, are refused +// with an error in the declaration that needs them. +var mutate1 = File.ReadAllText(Path.Combine(proofs, "Mutate1.lean")); +ProofTarget mutateTarget = new(assembly, "Fixture", "System.UInt64 Calls::Mutate1(System.UInt64)"); +RejectedProof("Mutate1Footprint", Changed(mutate1, " footprint := fun (place, _) => [(place, 8)]", + " footprint _ := []"), mutateTarget, "mutate_holds", "unsolved goals"); +RejectedProof("Mutate1Precondition", Changed(mutate1, "frame0.snapshots.read_word64 (index := 0) rfl rfl⟩", "?_⟩"), + mutateTarget, "correctness", "unsolved goals"); + +// In the loop of CountCalls, a summary of Decrement that bounds its result without stating it still proves the +// call and the measure, and leaves the invariant unproved. A summary claimed for CountCalls' own token at the index +// the call enters is refused where the site check reads the call's token. +var countCalls = File.ReadAllText(At("Examples/Proofs/CountCallsSummary.lean")); +ProofTarget countTarget = new(At("Examples/Assembly/bin/Examples.dll"), "Examples", + "System.UInt32 Examples.Loops::CountCalls(System.UInt32)"); +var weakened = Changed(Changed(Changed(countCalls, " post x _ _ v := v = x - 1", + " post x _ _ v := x ≠ 0 → v.toNat < x.toNat"), + "refine MethodContract.Holds.intro m _ ?_ (Confined.refl _ m) rfl", + "refine MethodContract.Holds.intro m (x - 1) ?_ (Confined.refl _ m) measure_falls"), """ + cil_loop_call summary.holds x at decrementSite + -- The frame of the memory before the call is stale once `calledFrame` holds. + clear frame + cil_loop_recurse recurse (x - 1, count + 1) + case invariant => + show count + 1 + (x - 1) = value + """, """ + cil_loop_call keeping summary.holds x at decrementSite + -- The frame of the memory before the call is stale once `calledFrame` holds. + clear frame + cil_loop_recurse recurse (output, count + 1) + case invariant => + show count + 1 + output = value + """); +weakened = Changed(weakened, " case measure => exact measure_falls zero\n#print axioms terminates\n", + " case measure => exact returned zero\n#print axioms terminates\n"); +RejectedProof("CountCallsWeakPost", weakened, countTarget, "terminates", "omega could not prove the goal", + at: "bv_omega"); +RejectedProof("CountCallsOtherToken", Changed(countCalls, " token = Extracted.method1Original.token ∧", + " token = Extracted.method0Original.token ∧"), countTarget, "decrement_named", "Application type mismatch"); + +// With the original of Mutate forged to subtract, the body check refuses the record and Mutate's contract fails, +// so its decoded summary rests on sorryAx. +var summary = mutate1[mutate1.IndexOf("/-- `Mutate` adds two", StringComparison.Ordinal).. + mutate1.IndexOf("/-- The caller returns `x` plus two per call and keeps all memory below its entry, applying", + StringComparison.Ordinal)]; +var forged = Path.Combine(proofs, "ForgedMutate.lean"); +File.WriteAllText(forged, ForgeDecodedOriginal(File.ReadAllText(Path.Combine(directory, "Mutate1", "Extracted.lean")), + 1, "opcode := 88,", "opcode := 89,") + $$""" + + import Lean + import CIL + import CIL.MethodIdentity + namespace ForgedSummary + open CIL CIL.Safety CIL.Extraction + bind_method mutate mutateBody := "System.Void Calls::Mutate(System.UInt64&)" + {{summary}} + open Lean in + run_cmd do + if (← collectAxioms ``mutate_summary).contains ``sorryAx then + throwError "mutate_summary depends on sorryAx" + end ForgedSummary + """); +var rejected = new RejectedEvidence(Path.Combine(directory, "Rejected")); +rejected.Add(forged, "PASS a forged original of Mutate fails its body check, its contract and its decoded summary", + "the decoded method does not match its body bytes or metadata", "unsolved goals", + "mutate_summary depends on sorryAx"); + +// With the original of UnsafeLow forged to read lane 1 of the vector, its record claims a body other than its pinned +// bytes, so the body check refuses it and the decoded summary of Examples/Proofs/VectorSummary.lean rests on sorryAx. +var vectorProof = File.ReadAllText(At("Examples/Proofs/VectorSummary.lean")); +var lowSummary = vectorProof[vectorProof.IndexOf("/-- `UnsafeLow` returns its argument", StringComparison.Ordinal).. + vectorProof.IndexOf("/-- The caller returns its argument plus one and keeps the caller memory, applying", + StringComparison.Ordinal)]; +var vectorExtraction = Path.Combine(directory, "VectorSummary"); +Run(Extract("extract", At("Examples/Assembly/bin/Examples.dll"), "Examples", + "System.UInt64 Examples.DivisionIntrinsics::UnsafeLowNext(System.UInt64)", vectorExtraction)); +var forgedLow = Path.Combine(proofs, "ForgedUnsafeLow.lean"); +File.WriteAllText(forgedLow, ForgeDecodedOriginal(File.ReadAllText(Path.Combine(vectorExtraction, "Extracted.lean")), + 1, "opcode := 22,", "opcode := 23,") + $$""" + + import Lean + import CIL + import CIL.MethodIdentity + namespace ForgedSummary + open CIL CIL.Safety CIL.Extraction + bind_method low lowBody := "System.UInt64 Examples.DivisionIntrinsics::UnsafeLow(System.UInt64)" + {{lowSummary}} + open Lean in + run_cmd do + if (← collectAxioms ``low_summary).contains ``sorryAx then + throwError "low_summary depends on sorryAx" + end ForgedSummary + """); +rejected.Add(forgedLow, "PASS a forged original of UnsafeLow fails its body check and its decoded summary", + "the decoded method does not match its body bytes or metadata", "low_summary depends on sorryAx"); +rejected.Check(); +verification.CheckSavedSummaries(Path.Combine(directory, "Saved"), At("Examples/Assembly/bin/Examples.dll")); + +// How many times as much the caller costs per call between its last two sizes as between its first two. At 1, 4 and +// 16 calls a cost a n^2 + b n grows (20a + b) / (5a + b) times, more than the limit once a > b / 20. +static double SummaryCallGrowth(int[] calls, long[] costs) => + (double)(costs[2] - costs[1]) / (calls[2] - calls[1]) / ((double)(costs[1] - costs[0]) / (calls[1] - calls[0])); + +// `text` with `before`, which must occur once, replaced by `after`. +static string Changed(string text, string before, string after) => + text.IndexOf(before, StringComparison.Ordinal) is var index && index >= 0 && + text.IndexOf(before, index + 1, StringComparison.Ordinal) < 0 + ? text.Replace(before, after, StringComparison.Ordinal) + : throw new InvalidOperationException("Expected one occurrence of: " + before); + +// Verifies `text` as the proof of `target` and requires it to fail with an error inside `theorem`, on the line `at` +// when given, whose message contains `diagnostic`, so an unrelated failure cannot pass as the rejection. +void RejectedProof(string name, string text, ProofTarget target, string theorem, string diagnostic, string? at = null) +{ + var proof = Path.Combine(proofs, $"{name}.lean"); + File.WriteAllText(proof, text); + var lines = text.Split('\n'); + var start = Array.FindIndex(lines, line => line.StartsWith($"theorem {theorem} ", StringComparison.Ordinal)) + 1; + var end = Array.FindIndex(lines, line => line == $"#print axioms {theorem}") + 1; + try + { + verification.Verify(target.Assembly, target.Library, target.Entry, proof, Path.Combine(directory, name)); + } + catch (InvalidOperationException error) + { + // The session checks a copy of the proof, with the same lines, as Proof.lean. + var located = SummaryPatterns.ProofError.Matches(error.Message) + .Select(match => (Line: int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), + Message: match.Groups[2].Value)).ToArray(); + if (start == 0 || end == 0 || !located.Any(found => found.Line >= start && found.Line <= end && + (at is null || lines[found.Line - 1].Trim() == at) && + found.Message.Contains(diagnostic, StringComparison.Ordinal))) + throw new InvalidOperationException($"Expected {diagnostic} in {theorem} ({start}-{end})" + + (at is null ? "" : " at " + at) + $":\n{error.Message}"); + Console.WriteLine($"PASS {name} refused in {theorem}"); + return; + } + throw new InvalidOperationException($"{name} was accepted"); +} + + +// One public caller per size, each passing the same local to the private Mutate as often as its name says. +static string FixtureSource(int[] sizes) +{ + var callers = sizes.Select(calls => $$""" + + public static ulong Mutate{{calls}}(ulong x) + { + ulong local = x; + {{string.Concat(Enumerable.Repeat(" Mutate(ref local);\n", calls))}} return local; + } + """); + return $$""" + using System.Runtime.CompilerServices; + + public static class Calls + { + [MethodImpl(MethodImplOptions.NoInlining)] + static void Mutate(ref ulong value) => value = unchecked(value + 2); + {{string.Concat(callers)}} + } + + """; +} + +// The local's value after the given number of calls. +static string Value(int calls) => calls == 0 ? "x" : $"x + {2 * calls}"; + +// The value stepping computes from the value before the call: `x + 2#64` after the first. +static string SteppedValue(int calls) => calls == 1 ? "x + 2#64" : $"{Value(calls - 1)} + 2#64"; + +// Each store is stated with the value the next read returns and rewritten to the form stepping computes. +static string SteppedProof(int calls) +{ + var lines = new List + { + " obtain ⟨reference, entered, enter⟩ := entry.checked memory x wellFormed", + " have ready0 := enter.numeric0.writable", + " obtain ⟨memory0, write0, store0, read0, -⟩ := store_local_word64 x ready0" + }; + for (var call = 1; call <= calls; call++) + { + lines.Add($" have ready{call} := snapshot_access_after_write ready{call - 1} write{call - 1}"); + lines.Add($" obtain ⟨memory{call}, write{call}, store{call}, read{call}⟩ := " + + $"store_word64 ({Value(call)}) ready{call}"); + lines.Add($" rw [← show {SteppedValue(call)} = {Value(call)} by bv_omega] at store{call}"); + } + var below = "(write_preserves_memory_below _ _ _ _ _ _ enter.numeric0.fresh write0)"; + for (var call = 1; call <= calls; call++) + below = $"({below}.trans\n (write_preserves_memory_below _ _ _ _ _ _ enter.numeric0.fresh write{call}))"; + lines.Add($" have below := MemoryBelow.frame_round_trip enter.setup\n {below}"); + lines.Add(" suffices ran : ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory ="); + lines.Add($" .ok (leaveFrame (entry.frame memory reference) memory{calls}, " + + $"[.scalar (.i64 ({Value(calls)}))]) by"); + lines.Add(" obtain ⟨fuel, ran⟩ := ran"); + lines.Add(" exact WP.adequate.mpr ⟨fuel, _, _, ran, rfl, below⟩"); + var range = Enumerable.Range(1, calls).ToArray(); + string[] facts = + [ + "enter.setup", "entry.frame", "store0", "read0", .. range.Select(call => $"store{call}"), + .. range.Select(call => $"read{call}"), $"load_local_word64_of_read read{calls}", "localAddress", + .. range.Select(call => $"access_reference_valid _ _ _ _ _ ready{call}"), + .. range.Select(call => $"checkedValue_address _ _ (access_reference_valid _ _ _ _ _ ready{call})"), + "numericOfWidth" + ]; + lines.Add($" cil_checked_steps [{string.Join(",\n ", facts)}]"); + return string.Join("\n", lines); +} + +// Each call reads its precondition from the frame before it and leaves the next frame. Clearing the earlier frame +// keeps the context, and so the cost of each call, from growing with the calls before it. +static string SummaryProof(int calls) +{ + var lines = new List + { + " obtain ⟨record, -, summary⟩ := mutate_summary", + " obtain ⟨reference, entered, enter⟩ := entry.checked memory x wellFormed", + " apply WP.enter (by rfl) (by rfl) enter.setup", + " obtain ⟨initial, firstStore, frame0, -⟩ := (enter.frame []).store 0 (.i64 x) rfl", + " have permitted1 : mutateContract.pre (reference, x) initial :=", + " ⟨frame0.homes.writable_at (kind := .word64) (home := reference) (index := 0) rfl,", + " frame0.snapshots.read_word64 (index := 0) rfl rfl⟩" + }; + for (var call = 1; call <= calls; call++) + { + var frame = $"frame{call - 1}"; + if (call > 1) + { + var post = call == 2 ? "mutated1.post" + : $"show {Value(call - 2)} + 2 = {Value(call - 1)} by bv_omega ▸ mutated{call - 1}.post"; + lines.Add($" have permitted{call} : mutateContract.pre (reference, {Value(call - 1)}) " + + $"final{call - 1} :="); + lines.Add($" ⟨{frame}.homes.writable_at (kind := .word64) (home := reference) (index := 0) rfl,"); + lines.Add($" {post}⟩"); + } + lines.Add(call == 1 ? $" cil_contract_steps [firstStore, localAddress, {frame}.form_home 0]" + : $" cil_contract_steps [localAddress, {frame}.form_home 0]"); + lines.Add(" apply WP.exists_execution"); + lines.Add($" apply WP.local_call {frame} summary.holds (reference, {Value(call - 1)}) (by rfl) (by rfl)"); + lines.Add($" (by cil_checked_step [localAddress, {frame}.form_home 0]) permitted{call} 0 .word64 " + + "reference"); + lines.Add($" rfl rfl (fun _ => .i64 ({Value(call)})) (fun _ => ({Value(call)}).toNat) (fun _ => rfl)"); + lines.Add(call == 1 ? " (fun _ _ post => post)" + : $" (fun _ _ post => by rw [← show {Value(call - 1)} + 2 = {Value(call)} by bv_omega]; exact post)"); + lines.Add($" intro final{call} values{call} mutated{call} frame{call}"); + lines.Add($" clear {frame}"); + } + lines.Add($" have loaded := frame{calls}.snapshots.load (index := 0) (.i64 ({Value(calls)})) rfl"); + lines.Add($" (body := Extracted.method0) (pc := {2 * calls + 2}) (args := [.scalar (.i64 x)]) (stack := [])"); + lines.Add(" cil_contract_steps [loaded]"); + lines.Add($" exact ⟨rfl, frame{calls}.leave_kept enter.setup _ rfl⟩"); + return string.Join("\n", lines); +} + +static string MutateProof(int calls) => $$""" + import Extracted + import CIL + import CIL.MethodIdentity + import CIL.Safety.LoopScaffold + + /-! + # {{calls}} byref calls, stepped and through a decoded summary + + `Calls::Mutate{{calls}}` passes one local to `Mutate` {{calls}} times. `correctness_stepped` steps every + call; `correctness` applies the decoded summary of `Mutate` at each call. + -/ + + namespace ExampleProof + open CIL CIL.Safety CIL.Extraction + + bind_entry entry (memory : Safety.Memory) (x : W64) (wellFormed : memory.WellFormed) := + Extracted.method0 with [.scalar (.i64 x)] from memory allocating wellFormed + bind_method mutate mutateBody := "System.Void Calls::Mutate(System.UInt64&)" + + /-- The caller returns `x` plus two per call and keeps all memory below its entry, stepping + every call. -/ + theorem correctness_stepped (memory : Safety.Memory) (x : W64) (wellFormed : memory.WellFormed) : + WP (Execution (fun fuel => invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory)) + (fun (final, values) => values = [.scalar (.i64 ({{Value(calls)}}))] ∧ + MemoryBelow memory.nextIdentity memory final) := by + {{SteppedProof(calls)}} + #print axioms correctness_stepped + + /-- `Mutate` adds two to the word its argument names and writes nothing else. -/ + def mutateContract : MethodContract (Reference × W64) Unit where + args := fun (place, _) => [.reference (.address place)] + results _ := [] + pre := fun (place, old) memory => access memory place 8 1 true = .ok () ∧ + read memory place 8 1 = .ok (numberBytes old.toNat 8) + footprint := fun (place, _) => [(place, 8)] + post := fun (place, old) _ final _ => + read final place 8 1 = .ok (numberBytes (old + 2).toNat 8) + + /-- `Mutate` meets its contract. -/ + theorem mutate_holds : mutateContract.Holds Extracted.program mutate := by + rintro ⟨place, old⟩ memory ⟨ready, loaded⟩ + obtain ⟨final, written, stored, readback⟩ := store_word64 (old + 2) ready + refine MethodContract.Holds.intro final () ?_ + (Confined.write written (by simp [mutateContract])) readback + cil_checked_steps [loaded, stored, access_reference_valid _ _ _ _ _ ready, + checkedValue_address _ _ (access_reference_valid _ _ _ _ _ ready)] + #print axioms mutate_holds + + /-- Every record's pinned bytes decode to the method at its index of the extraction. -/ + theorem bodies_decode : + decodedProgram? Extracted.profile Extracted.environment Extracted.methodEvidence = + some Extracted.program := + decodedProgram?_eq_some Extracted.translation_valid.programBindingsValid + (.cons ((Method.ofBody?_of_decodes Extracted.method0_body_validated).trans + Extracted.method0_decoded) + (.cons ((Method.ofBody?_of_decodes Extracted.method1_body_validated).trans + Extracted.method1_decoded) .nil)) + #print axioms bodies_decode + + /-- The contract of `Mutate`, bound to its record and the bytes it decodes from. -/ + theorem mutate_summary : ∃ record, Extracted.methodEvidence[mutate]? = some record ∧ + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment mutate record.assembly record.original.token record.arguments + mutateContract := + ⟨_, rfl, .of_decoded bodies_decode rfl ⟨_, Extracted.method1_body_validated⟩ + Extracted.method1_specialization_validated.1 mutate_holds⟩ + #print axioms mutate_summary + + /-- The caller returns `x` plus two per call and keeps all memory below its entry, applying + the summary of `Mutate` at each call. -/ + theorem correctness (memory : Safety.Memory) (x : W64) (wellFormed : memory.WellFormed) : + WP (Execution (fun fuel => invoke Extracted.program fuel 0 [.scalar (.i64 x)] memory)) + (fun (final, values) => values = [.scalar (.i64 ({{Value(calls)}}))] ∧ + MemoryBelow memory.nextIdentity memory final) := by + {{SummaryProof(calls)}} + #print axioms correctness + + end ExampleProof + + """; + +// The assembly, library and entry a rejected proof is verified against. +record ProofTarget(string Assembly, string Library, string Entry); + +/// The patterns that read Lean's report of a rejected proof. +static partial class SummaryPatterns +{ + [GeneratedRegex(@"Proof\.lean:(\d+):\d+: error(?:\([^)\r\n]*\))?: ([^\r\n]*)")] + internal static partial Regex ProofError { get; } +} diff --git a/Tests/SummaryModules/Branch/ReversedBranch.lean b/Tests/SummaryModules/Branch/ReversedBranch.lean new file mode 100644 index 000000000..73387ab90 --- /dev/null +++ b/Tests/SummaryModules/Branch/ReversedBranch.lean @@ -0,0 +1,85 @@ +import Extracted +import SavedArms + +/-! Reuse the producer's two compiled return regions after reversing the branch before them. -/ + +namespace ReversedBranch +open CIL CIL.Safety SavedArms + +/-- Both return regions start after the method's condition and branch. -/ +private abbrev suffix (method pc : Nat) := inStarts [(0, 2)] method pc = true + +/-- The new branch instruction precedes both saved return regions. -/ +theorem reused (left flag : Bool) (x y : W64) : + DecodedSegmentSummary Extracted.program Extracted.methodEvidence Extracted.profile + Extracted.environment 0 (if left then 4 else 2) Extracted.assembly0 + Extracted.method0Original.token [] (.ldarg (if left then 1 else 2)) [.ret] + (returnsWord flag x y (if left then x else y)) := by + apply (SavedArms.checked left flag x y).congr_region (region := suffix) + (DecodedAt.of_translation Extracted.translation_valid ⟨0, by decide⟩ Extracted.program_method0) + (RegionClosed.of_check (by decide +kernel)) + · exact AgreesOn.set (program := Reference.program) (index := 0) Reference.program_method0 + Extracted.method0.code fun _ inside => + code_agrees_of_drop (lower := 2) rfl (by simpa [suffix, inStarts] using inside) + · intro i _ + cases left <;> simp [suffix, inStarts] <;> omega + · intro args before pc after _ post + cases post + +#print axioms reused + +/-- The changed condition at the branch selects the input the original method would skip. -/ +def branchContract (flag : Bool) (x y : W64) : SegmentContract where + pre args s := args = arguments flag x y ∧ + s.stack = [.scalar (.i32 (if flag then 1 else 0))] ∧ s.frame.owned = [] + post _ before exit := exit = .returns before.memory [.scalar (.i64 (if flag then y else x))] + +/-- The changed branch selects the other result while preserving arbitrary caller memory. -/ +theorem composed (flag : Bool) (x y : W64) : (branchContract flag x y).Holds Extracted.program 0 1 := by + let next := fun pc => if pc = 4 then returnsWord flag x y x else + if pc = 2 then returnsWord flag x y y else SegmentContract.unreachable + have successors : ∀ pc, (next pc).Holds Extracted.program 0 pc := by + intro pc + by_cases left : pc = 4 + · subst pc + exact (reused true flag x y).holds + by_cases right : pc = 2 + · subst pc + exact (reused false flag x y).holds + simpa [next, left, right] using SegmentContract.unreachable_holds Extracted.program 0 pc + apply SegmentContract.branch_seq (pc := 1) (target := 4) (op := .brfalse 4) + (branchContract flag x y).pre (fun _ _ => flag = false) (fun _ _ => []) + Extracted.program_method0 (by rfl) (next := next) (rest := successors) + · rintro args before ⟨rfl, stack, _⟩ + rw [stack] + cases flag <;> rfl + · exact fun _ _ h => h + · rintro args before ⟨rfl, _, owned⟩ + cases flag <;> exact ⟨rfl, rfl, owned⟩ + · intro args before exit _ post confined + cases flag <;> exact ⟨confined, post⟩ + +#print axioms composed +/-- Both compiled branch arms return the selected word and preserve all caller memory. -/ +theorem invocation (memory : CIL.Safety.Memory) (flag : Bool) (x y : W64) : + ∃ fuel, invoke Extracted.program fuel 0 (arguments flag x y) memory = + .ok (memory, [.scalar (.i64 (if flag then y else x))]) := by + let frame : Frame := ⟨memory.nextIdentity, [], [], 0⟩ + let before : SegmentState := ⟨frame, [.scalar (.i32 (if flag then 1 else 0))], memory⟩ + obtain ⟨exit, ran, _, post⟩ := composed flag x y (arguments flag x y) before ⟨rfl, rfl, rfl⟩ + cases post + obtain ⟨fuel, result⟩ := ran + refine ⟨fuel + 1, ?_⟩ + cases flag <;> exact result + +#print axioms invocation + +/-- The original Choose contract requires 11 on this input. Every successful run returns 22. -/ +theorem rejects_original : ¬ ∃ fuel, invoke Extracted.program fuel 0 (arguments true 11 22) + Memory.empty = .ok (Memory.empty, [.scalar (.i64 11)]) := by + rintro ⟨fuel, original⟩ + obtain ⟨_, changed⟩ := invocation Memory.empty true 11 22 + cases invoke_ok_unique changed original + +#print axioms rejects_original +end ReversedBranch diff --git a/Tests/SummaryModules/Branch/SavedArms.lean b/Tests/SummaryModules/Branch/SavedArms.lean new file mode 100644 index 000000000..360ed465a --- /dev/null +++ b/Tests/SummaryModules/Branch/SavedArms.lean @@ -0,0 +1,36 @@ +import Reference +import CIL.Safety.DecodedSegment + +/-! Separate contracts for the two load-and-return regions of the compiled Choose method. -/ + +namespace SavedArms +open CIL CIL.Safety + +/-- The three arguments in the compiled method's Boolean and word representations. -/ +def arguments (flag : Bool) (x y : W64) : List CIL.Safety.Value := + [.scalar (.i32 (if flag then 1 else 0)), .scalar (.i64 x), .scalar (.i64 y)] + +/-- A return arm consumes an empty stack and leaves the caller's memory intact. -/ +def returnsWord (flag : Bool) (x y result : W64) : SegmentContract where + pre args state := args = arguments flag x y ∧ state.stack = [] ∧ state.frame.owned = [] + post _ before exit := exit = .returns before.memory [.scalar (.i64 result)] + +/-- Each arm binds its two instructions and its result to the producer's method evidence. -/ +theorem checked (left flag : Bool) (x y : W64) : + DecodedSegmentSummary Reference.program Reference.methodEvidence Reference.profile + Reference.environment 0 (if left then 4 else 2) Reference.assembly0 + Reference.method0Original.token [] (.ldarg (if left then 1 else 2)) [.ret] + (returnsWord flag x y (if left then x else y)) := by + refine ⟨DecodedAt.of_translation Reference.translation_valid ⟨0, by decide⟩ + Reference.program_method0, ⟨Reference.method0, Reference.program_method0, ?_⟩, ?_⟩ + · cases left <;> rfl + · rintro args ⟨frame, stack, memory⟩ ⟨rfl, rfl, owned⟩ + refine ⟨.returns memory [.scalar (.i64 (if left then x else y))], ⟨2, ?_⟩, + Confined.refl [] memory, rfl⟩ + change frame.owned = [] at owned + cases left <;> + change Except.ok (leaveFrame frame memory, _) = Except.ok (memory, _) <;> + simp [leaveFrame, owned] + +#print axioms checked +end SavedArms diff --git a/Tests/SummaryModules/Loop/ChangedLoop.lean b/Tests/SummaryModules/Loop/ChangedLoop.lean new file mode 100644 index 000000000..3bec0c908 --- /dev/null +++ b/Tests/SummaryModules/Loop/ChangedLoop.lean @@ -0,0 +1,68 @@ +import Changed +import CountCallsSummary +import CIL.Safety.RegionRun + +/-! Reuse the compiled CountCalls loop after changing its initial accumulator to one. -/ +namespace ChangedLoop +open CIL CIL.Safety CIL.Extraction + +private abbrev region (method pc : Nat) := inStarts [(0, 3), (1, 0)] method pc = true + +/-- Both the loop suffix and all entries it calls stay in the checked region. -/ +private theorem closed : RegionClosed Extracted.program region := + RegionClosed.of_check (by decide +kernel) +#print axioms closed + +/-- Only the constant that precedes the region changes. -/ +private theorem agrees : AgreesOn Extracted.program Changed.program region := by + apply AgreesOn.set Extracted.program_method0 Changed.method0.code + intro pc inside + exact code_agrees_of_drop (a := Changed.method0.code) (b := Extracted.method0.code) + (lower := 3) rfl (by simpa [region, inStarts] using inside) +#print axioms agrees + +/-- The live word locals retain a remaining count and the number of calls already counted. -/ +abbrev State (remaining counted : Reference) := + FrameState [] Memory.empty [.word32, .word32] (W32 × W32) + (fun _ => [.bytes .word32 remaining, .bytes .word32 counted]) + fun values => [some (.i32 values.1), some (.i32 values.2)] + +/-- The producer proves termination by decreasing the remaining count. The + destination agrees on the whole loop and the callee it reaches. -/ +theorem fromGuard (args : List CIL.Safety.Value) (value : W32) {remaining counted : Reference} + (state : State remaining counted) (total : state.values.2 + state.values.1 = value) : + LoopTerminates Changed.program 0 args (fun _ values => values = [.scalar (.i32 value)]) + (ExampleProof.header remaining counted state) := by + obtain ⟨record, _, summary⟩ := ExampleProof.decrement_summary + obtain ⟨fuel, result, ran, post⟩ := ExampleProof.terminates summary args value state total + refine ⟨fuel, result, ?_, post⟩ + have inside : region 0 (ExampleProof.header remaining counted state).pc := by + change region 0 13 + decide +kernel + exact (run_congr_region closed agrees fuel 0 _ args _ _ _ inside).trans ran +#print axioms fromGuard + +/-- Starting the accumulator at one makes the compiled loop return its input plus one, + with 32-bit wraparound, for every input. -/ +theorem correctness : Ensures Changed.program 0 + (fun args initial => ∃ x : W32, args = [.scalar (.i32 x)] ∧ initial = Memory.empty) + (fun args _ _ values => ∃ x : W32, args = [.scalar (.i32 x)] ∧ + values = [.scalar (.i32 (x + 1))]) := by + apply ensures_of_loop + rintro _ _ ⟨x, rfl, rfl⟩ + obtain ⟨remaining, counted, entered, enter⟩ := ExampleProof.entry.checked x + obtain ⟨first, wroteFirst, firstFacts, -⟩ := (enter.frame []).store 0 (.i32 x) rfl + obtain ⟨ready, wroteCount, facts, -⟩ := firstFacts.store 1 (.i32 1) rfl + let state : State remaining counted := ⟨(x, 1), ready, facts⟩ + obtain ⟨fuel, result, ran, post⟩ := fromGuard [.scalar (.i32 x)] (x + 1) state + (by change 1 + x = x + 1; bv_omega) + refine ⟨Changed.method0, _, entered, Changed.program_method0, rfl, enter.setup, + fuel + 6, result, ?_, x, rfl, post⟩ + change run Changed.program fuel 0 13 [.scalar (.i32 x)] + (ExampleProof.entry.frame remaining counted) [.scalar (.i32 x)] ready = .ok result at ran + change run Changed.program (fuel + 6) 0 0 [.scalar (.i32 x)] + (ExampleProof.entry.frame remaining counted) [] entered = .ok result + dsimp only [ExampleProof.entry.frame] at ran ⊢ + cil_checked_steps [wroteFirst, wroteCount, facts.load 0] <;> exact ran +#print axioms correctness +end ChangedLoop diff --git a/Tests/SummaryModules/Memory/SavedSummary.lean b/Tests/SummaryModules/Memory/SavedSummary.lean new file mode 100644 index 000000000..49b1117fd --- /dev/null +++ b/Tests/SummaryModules/Memory/SavedSummary.lean @@ -0,0 +1,31 @@ +import Reference +import CIL.Safety.ProfileContract +import CIL + +/-! Mutate increases the referenced word by two and confines its write to that word. -/ +namespace SavedSummary +open CIL CIL.Safety + +/-- Mutate increases the referenced word by two and confines its write to that word. -/ +def contract : MethodContract (Reference × W64) Unit where + args := fun (place, _) => [.reference (.address place)] + results _ := [] + pre := fun (place, old) memory => access memory place 8 1 true = .ok () ∧ + read memory place 8 1 = .ok (numberBytes old.toNat 8) + footprint := fun (place, _) => [(place, 8)] + post := fun (place, old) _ final _ => + read final place 8 1 = .ok (numberBytes (old + 2).toNat 8) + +/-- The compiled scalar entry satisfies the contract. -/ +theorem checked : + DecodedSummary Reference.program Reference.methodEvidence Reference.profile Reference.environment + 0 Reference.assembly0 Reference.method0Original.token [] contract := by + apply DecodedSummary.of_translation Reference.translation_valid ⟨0, by decide⟩ Reference.program_method0 + rintro ⟨place, old⟩ memory ⟨ready, loaded⟩ + obtain ⟨final, written, stored, readback⟩ := store_word64 (old + 2) ready + refine MethodContract.Holds.intro final () ?_ + (Confined.write written (by simp [contract])) readback + cil_checked_steps [loaded, stored, access_reference_valid _ _ _ _ _ ready, + checkedValue_address _ _ (access_reference_valid _ _ _ _ _ ready)] +#print axioms checked +end SavedSummary diff --git a/Tests/SummaryModules/SavedSummary.lean b/Tests/SummaryModules/SavedSummary.lean new file mode 100644 index 000000000..e948dd239 --- /dev/null +++ b/Tests/SummaryModules/SavedSummary.lean @@ -0,0 +1,26 @@ +import Reference +import CIL.Safety.ProfileContract + +/-! A decoded summary for the compiled unsigned word addition. -/ +namespace SavedSummary +open CIL CIL.Safety + +/-- Addition returns the wrapping sum and preserves the caller's memory. -/ +def contract : MethodContract (W64 × W64) W64 where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post input before after value := after = before ∧ value = input.1 + input.2 + +/-- The saved proof binds the addition contract to its definition and body in the scalar image. -/ +theorem checked : + DecodedSummary Reference.program Reference.methodEvidence Reference.profile Reference.environment + 0 Reference.assembly0 Reference.method0Original.token [] contract := by + apply DecodedSummary.of_translation Reference.translation_valid ⟨0, by decide⟩ Reference.program_method0 + rintro ⟨left, right⟩ memory - + apply MethodContract.Holds.intro (c := contract) (input := (left, right)) memory + (left + right) ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + exact ⟨8, rfl⟩ +#print axioms checked +end SavedSummary diff --git a/Tests/SummaryModules/SummaryConsumer.lean b/Tests/SummaryModules/SummaryConsumer.lean new file mode 100644 index 000000000..a89109aa6 --- /dev/null +++ b/Tests/SummaryModules/SummaryConsumer.lean @@ -0,0 +1,25 @@ +import Extracted +import SavedSummary + +/-! The independently extracted entry reuses the cached decoded summary under this profile. -/ +namespace SummaryConsumer +open CIL CIL.Safety CIL.Extraction + +/-- The destination contains the reprofiled instructions and the same defining record. -/ +theorem checked : + DecodedSummary Extracted.program Extracted.methodEvidence Extracted.profile Extracted.environment + 0 Extracted.assembly0 Extracted.method0Original.token [] SavedSummary.contract := by + have decoded : decodedProgram? Extracted.profile Extracted.environment Extracted.methodEvidence = + some Extracted.program := + decodedProgram?_eq_some Extracted.translation_valid.programBindingsValid + (.cons ((Method.ofBody?_of_decodes Extracted.method0_body_validated).trans + Extracted.method0_decoded) .nil) + have token : Extracted.method0Original.token = Reference.method0Original.token := by cil_kernel_rfl + have program : Extracted.program = reprofile Reference.program Extracted.profile := by cil_kernel_rfl + have record : Extracted.methodEvidence = Reference.methodEvidence := by cil_kernel_rfl + rw [program, record] at decoded ⊢ + rw [token] + exact SavedSummary.checked.reprofile (target := Extracted.profile) (by decide +kernel) + (Program.ProfileAgreement.of_flags _ _ _ (by decide +kernel)) decoded +#print axioms checked +end SummaryConsumer diff --git a/Tests/SummaryModules/Vector/SavedSummary.lean b/Tests/SummaryModules/Vector/SavedSummary.lean new file mode 100644 index 000000000..71d232170 --- /dev/null +++ b/Tests/SummaryModules/Vector/SavedSummary.lean @@ -0,0 +1,26 @@ +import Reference +import CIL.Safety.ProfileContract +import CIL + +/-! The low lane of CreateScalarUnsafe contains its argument. -/ +namespace SavedSummary +open CIL CIL.Safety + +/-- The low lane of CreateScalarUnsafe contains its argument. -/ +def contract : MethodContract W64 W64 where + args x := [.scalar (.i64 x)] + results value := [.scalar (.i64 value)] + pre _ _ := True + footprint _ := [] + post x initial final value := value = x ∧ final = initial + +/-- The compiled scalar entry satisfies the contract. -/ +theorem checked : + DecodedSummary Reference.program Reference.methodEvidence Reference.profile Reference.environment + 0 Reference.assembly0 Reference.method0Original.token [] contract := by + apply DecodedSummary.of_translation Reference.translation_valid ⟨0, by decide⟩ Reference.program_method0 + rintro x memory - + refine MethodContract.Holds.intro memory x ?_ (Confined.refl _ _) ⟨rfl, rfl⟩ + cil_checked_steps +#print axioms checked +end SavedSummary diff --git a/Tests/Support.lean b/Tests/Support.lean index 417e1eff1..92da4a359 100644 --- a/Tests/Support.lean +++ b/Tests/Support.lean @@ -18,4 +18,34 @@ def caller : Memory where /-- A reference to the start of `caller`'s allocation. -/ def reference : Value := .reference (.address ⟨0, 0⟩) +/-- The parameterless Boolean getter on a named ISA class in the pinned intrinsics assembly. -/ +def supportedGetter (isa : String) : CIL.Extraction.MethodRef where + owner := .atom CIL.Extraction.intrinsicsScope ("System.Runtime.Intrinsics." ++ isa) + name := name!"get_IsSupported" + hasThis := false + genericArity := 0 + returnType := .atom CIL.Extraction.runtimeScope "System.Boolean" + parameters := [] + +/-- The checked getter table selects AVX2 in every layout environment. -/ +theorem supportedGetter_avx2_model (environment : CIL.LayoutEnvironment) : + CIL.Extraction.runtimeModel environment .call (supportedGetter "X86.Avx2") = + some (.feature .avx2) := rfl + +/-- A static method of `count` parameters, for a body that calls through its link table and + needs only the call's arity; the owner, name and types are placeholders no check reads. -/ +def callee (count : Nat) : CIL.Extraction.MethodRef where + owner := .void + name := ⟨1⟩ + hasThis := false + genericArity := 0 + returnType := .void + parameters := List.replicate count .void + +/-- A placeholder method takes exactly its parameter count of arguments. -/ +@[simp] theorem callee_argumentCount (count : Nat) : (callee count).argumentCount = count := by + simp [callee, CIL.Extraction.MethodRef.argumentCount] + +#print axioms callee_argumentCount + end Tests diff --git a/Tests/Support/IntrinsicMethods.lean b/Tests/Support/IntrinsicMethods.lean new file mode 100644 index 000000000..0f07fbdb3 --- /dev/null +++ b/Tests/Support/IntrinsicMethods.lean @@ -0,0 +1,133 @@ +import CIL.Safety.ResolutionFacts +import CIL.SymbolicExecution +import CIL.Extraction.RuntimeModels + +/-! Closed runtime member signatures used by the checked SIMD execution fixtures. -/ + +namespace Tests.IntrinsicMethods +open CIL CIL.Extraction + +private def i32 := TypeExpr.atom runtimeScope "System.Int32" +private def byte := TypeExpr.atom runtimeScope "System.Byte" +private def vectorOf (width : Nat) (element : String) := TypeExpr.apply + (.constructor intrinsicsScope s!"System.Runtime.Intrinsics.Vector{width}`1") (.atom runtimeScope element) +private def v32 := vectorOf 256 "System.UInt32" +/-- The closed vector operand shared by the arithmetic calls and typed memory accesses. -/ +def vector256UInt64 := vectorOf 256 "System.UInt64" + +private def layouts : LayoutEnvironment := {} + +cil_value_shape layouts vector256UInt64 + +private def member (owner : String) (name : MetadataName) (result : TypeExpr) + (parameters : List TypeExpr) : MethodRef := + { owner := .atom intrinsicsScope ("System.Runtime.Intrinsics." ++ owner), + name, hasThis := false, genericArity := 0, returnType := result, parameters } + +def add64 := member "X86.Avx2" name!"Add" vector256UInt64 [vector256UInt64, vector256UInt64] + +@[cil_code] theorem add64_model (env : LayoutEnvironment) : + runtimeModel env .call add64 = some (.intrinsic (.avx2 .add64) 2) := rfl + +def multiplyEven32 := member "X86.Avx2" name!"Multiply" vector256UInt64 [v32, v32] + +@[cil_code] theorem multiplyEven32_model (env : LayoutEnvironment) : + runtimeModel env .call multiplyEven32 = some (.intrinsic (.avx2 .multiplyEven32) 2) := rfl + +def shiftRight := member "X86.Avx2" name!"ShiftRightLogical" vector256UInt64 [vector256UInt64, byte] + +@[cil_code] theorem shiftRight_model (env : LayoutEnvironment) : + runtimeModel env .call shiftRight = some (.intrinsic (.avx2 .shr64) 2) := rfl + +def shiftLeft := member "X86.Avx2" name!"ShiftLeftLogical" vector256UInt64 [vector256UInt64, byte] + +@[cil_code] theorem shiftLeft_model (env : LayoutEnvironment) : + runtimeModel env .call shiftLeft = some (.intrinsic (.avx2 .shl64) 2) := rfl + +def permute := member "X86.Avx2" name!"Permute4x64" vector256UInt64 [vector256UInt64, byte] + +@[cil_code] theorem permute_model (env : LayoutEnvironment) : + runtimeModel env .call permute = some (.intrinsic (.avx2 .permute4x64) 2) := rfl + +def compareEqual := member "X86.Avx2" name!"CompareEqual" vector256UInt64 [vector256UInt64, vector256UInt64] + +@[cil_code] theorem compareEqual_model (env : LayoutEnvironment) : + runtimeModel env .call compareEqual = some (.intrinsic (.avx2 .eq64) 2) := rfl + +def compareLessThan := member "X86.Avx512F/VL" name!"CompareLessThan" vector256UInt64 [vector256UInt64, vector256UInt64] + +@[cil_code] theorem compareLessThan_model (env : LayoutEnvironment) : + runtimeModel env .call compareLessThan = some (.intrinsic (.avx512 .ltu64) 2) := rfl + +def moveMask := member "X86.Avx512DQ" name!"MoveMask" i32 [vector256UInt64] + +@[cil_code] theorem moveMask_model (env : LayoutEnvironment) : + runtimeModel env .call moveMask = some (.intrinsic (.avx512DQ .moveMask64) 1) := rfl + +def multiplyLow := member "X86.Avx512DQ/VL" name!"MultiplyLow" vector256UInt64 [vector256UInt64, vector256UInt64] + +@[cil_code] theorem multiplyLow_model (env : LayoutEnvironment) : + runtimeModel env .call multiplyLow = some (.intrinsic (.avx512DQ .mul64) 2) := rfl + +private def word := TypeExpr.atom runtimeScope "System.UInt64" + +def createFour := member "Vector256" name!"Create" vector256UInt64 [word, word, word, word] + +@[cil_code] theorem createFour_model (env : LayoutEnvironment) : + runtimeModel env .call createFour = some (.intrinsic (.vector (.create64 256)) 4) := rfl + +def portableAdd := { member "Vector256" name!"op_Addition" vector256UInt64 [vector256UInt64, vector256UInt64] with owner := vector256UInt64 } + +@[cil_code] theorem portableAdd_model (env : LayoutEnvironment) : + runtimeModel env .call portableAdd = some (.intrinsic (.vector (.add64 256)) 2) := rfl + +def subtract := { member "Vector256" name!"op_Subtraction" vector256UInt64 [vector256UInt64, vector256UInt64] with owner := vector256UInt64 } + +@[cil_code] theorem subtract_model (env : LayoutEnvironment) : + runtimeModel env .call subtract = some (.intrinsic (.vector (.sub64 256)) 2) := rfl + +def sum := { member "Vector256" name!"Sum" word [vector256UInt64] with + genericArity := 1, methodArguments := [word] } + +@[cil_code] theorem sum_model (env : LayoutEnvironment) : + runtimeModel env .call sum = some (.intrinsic (.vector (.sum64 256)) 1) := rfl + +def asUInt32 := { member "Vector256" name!"AsUInt32" v32 [vector256UInt64] with + genericArity := 1, methodArguments := [word] } + +@[cil_code] theorem asUInt32_model (env : LayoutEnvironment) : + runtimeModel env .call asUInt32 = some (.intrinsic (.vector (.reinterpret 256)) 1) := rfl + +/-- The closed vector operand used by the byte intrinsics and typed loads. -/ +def vector128Byte := vectorOf 128 "System.Byte" + +cil_value_shape layouts vector128Byte + +def shiftLeftBytes := member "X86.Sse2" name!"ShiftLeftLogical128BitLane" + (vectorOf 128 "System.UInt64") [vectorOf 128 "System.UInt64", byte] + +theorem shiftLeftBytes_model (env : LayoutEnvironment) : + runtimeModel env .call shiftLeftBytes = some (.intrinsic (.sse .shiftLeftBytes) 2) := rfl + +def armEncrypt := member "Arm.Aes" name!"Encrypt" vector128Byte [vector128Byte, vector128Byte] + +theorem armEncrypt_model (env : LayoutEnvironment) : + runtimeModel env .call armEncrypt = some (.intrinsic (.armAes .encrypt) 2) := rfl + +def ternaryLogic := member "X86.Avx512F/VL" name!"TernaryLogic" vector256UInt64 [vector256UInt64, vector256UInt64, vector256UInt64, byte] + +theorem ternaryLogic_model (env : LayoutEnvironment) : + runtimeModel env .call ternaryLogic = some (.intrinsic (.avx512 .ternaryLogic) 4) := rfl + +def shuffle256 := member "X86.Avx2" name!"Shuffle" (vectorOf 256 "System.Byte") + [vectorOf 256 "System.Byte", vectorOf 256 "System.Byte"] + +theorem shuffle256_model (env : LayoutEnvironment) : + runtimeModel env .call shuffle256 = some (.intrinsic (.avx2 .shuffleBytes) 2) := rfl + +def shuffle128 := member "X86.Ssse3" name!"Shuffle" vector128Byte [vector128Byte, vector128Byte] + +theorem shuffle128_model (env : LayoutEnvironment) : + runtimeModel env .call shuffle128 = some (.intrinsic (.sse .shuffleBytes) 2) := rfl + +end Tests.IntrinsicMethods diff --git a/Tests/Support/ModelOperands.lean b/Tests/Support/ModelOperands.lean new file mode 100644 index 000000000..79e09b857 --- /dev/null +++ b/Tests/Support/ModelOperands.lean @@ -0,0 +1,39 @@ +import Tests.Support.RawDecoding + +/-! Decoder controls and one-field refusals for external runtime model operands. -/ + +namespace Tests.ModelOperands + +open CIL CIL.Extraction + +/-- The code of the single live call of a method with empty local and argument storage, or the + stage that refuses the method. -/ +def decoded (site : CallSite) (environment : LayoutEnvironment := {}) + (profile := FeatureProfile.scalar) : Except RawDecodeError (List Instruction) := + let source : RawMethod := + { token := 0, owner := "Fixture", hasThis := false, explicitThis := false, + callingConvention := 0, attributes := 0, implementation := 0, returnType := .void, + parameters := [], locals := [], initLocals := true, maxStack := 3, genericParameters := [], + code := [⟨0, 40, .call site⟩] } + (decodeRaw profile environment [] [true] source []).map Method.code + +/-- The control keeps its call. Each altered operand leaves the call a live hole at pc 0. -/ +def checks (site : CallSite) : Bool := + let wrong := TypeExpr.atom "Fixture" "Other" + let owner := match site.method.owner with + | .atom _ name => TypeExpr.atom "Fixture" name + | _ => wrong + let parameters := match site.method.parameters with + | [] => [wrong] + | _ :: rest => wrong :: rest + let variants : List CallSite := [ + { site with method.owner := owner }, { site with method.name := name!"Other" }, + { site with name := "Other" }, + { site with method.genericArity := site.method.genericArity + 1 }, + { site with method.hasThis := true }, { site with method.parameters := parameters }, + { site with method.returnType := wrong }, { site with declared.convention := 5 }, + { site with target := .external "Fixture" }] + decide (decoded site = .ok [.call site.method {}]) && + variants.all (fun changed => decide (decoded changed = liveHoleAt 0 40)) + +end Tests.ModelOperands diff --git a/Tests/Support/RawDecoding.lean b/Tests/Support/RawDecoding.lean new file mode 100644 index 000000000..c017015fa --- /dev/null +++ b/Tests/Support/RawDecoding.lean @@ -0,0 +1,71 @@ +import CIL.Extraction.DecodedMethod + +/-! +Raw decoding that keeps the stage refusing a method. `Tests.decodeRaw` runs instantiation, +argument storage and `Method.ofClosed?` as `Method.ofRaw?` does, so a refusal test can state +the pc and opcode of a live hole and tell it apart from a refusal before closed decoding. +`Tests.DecodedFlow` pairs the scalar check with the code equation, which refuses a forged +emission at a site where the scalar check leaves the comparison to the decoder. +-/ + +namespace Tests + +open CIL CIL.Extraction + +/-- The stage that refuses a raw method: instantiation at its type arguments, the storage of + its closed arguments and locals, or closed decoding with the decoder's own error. -/ +inductive RawDecodeError where + | instantiation + | arguments + | method (error : MethodDecodeError) + deriving DecidableEq, Repr + +/-- A decoding result with a decidable value is compared by its refusal or its value. -/ +instance {α : Type} [DecidableEq α] : DecidableEq (Except RawDecodeError α) + | .ok left, .ok right => decidable_of_iff (left = right) ⟨congrArg _, Except.ok.inj⟩ + | .error left, .error right => + decidable_of_iff (left = right) ⟨congrArg _, Except.error.inj⟩ + | .ok _, .error _ | .error _, .ok _ => isFalse (by intro equal; cases equal) + +/-- The method `Method.ofRaw?` builds from a raw definition, or the stage that refuses it. -/ +def decodeRaw (profile : FeatureProfile) (environment : LayoutEnvironment) + (links : List (Nat × Link)) (reachable : List Bool) (source : RawMethod) + (arguments : List TypeExpr) : Except RawDecodeError Method := + match instantiateMethod environment.rawLayouts source arguments with + | none => .error .instantiation + | some closed => + match closedArguments environment.rawLayouts closed with + | none => .error .arguments + | some storage => + (Method.ofClosed? profile environment links reachable closed storage).mapError .method + +/-- The scalar check together with the code equation extraction proves for every method: `code` + is what the decoder gives `raw` at the pcs `features` reaches, with no layouts or links. -/ +abbrev DecodedFlow (raw : RawMethod) (features : FeatureCertificate) + (certificate : ScalarCertificate) (code : List Instruction) : Prop := + ScalarFlowValid raw features certificate code ∧ + code = methodCode .scalar {} [] (features.map Option.isSome) raw + +/-- The refusal of closed decoding at the first live hole, at `pc` with raw `opcode`. -/ +abbrev liveHoleAt {α : Type} (pc opcode : Nat) : Except RawDecodeError α := + .error (.method (.liveHole pc opcode)) + +/-- Forgetting the refusing stage gives exactly the raw decoder's result. -/ +theorem decodeRaw_toOption (profile : FeatureProfile) (environment : LayoutEnvironment) + (links : List (Nat × Link)) (reachable : List Bool) (source : RawMethod) + (arguments : List TypeExpr) : + (decodeRaw profile environment links reachable source arguments).toOption = + Method.ofRaw? profile environment links reachable source arguments := by + unfold decodeRaw Method.ofRaw? + cases instantiateMethod environment.rawLayouts source arguments with + | none => rfl + | some closed => + cases stored : closedArguments environment.rawLayouts closed with + | none => simp only [stored, bind, Option.bind]; rfl + | some storage => + simp only [stored, bind, Option.bind] + cases Method.ofClosed? profile environment links reachable closed storage <;> rfl + +#print axioms decodeRaw_toOption + +end Tests diff --git a/Tests/Support/SpanMethods.lean b/Tests/Support/SpanMethods.lean new file mode 100644 index 000000000..a60aa232e --- /dev/null +++ b/Tests/Support/SpanMethods.lean @@ -0,0 +1,40 @@ +import CIL.Extraction.RuntimeModels + +/-! Closed span member references used by the checked memory and loop fixtures. -/ + +namespace Tests.SpanMethods +open CIL CIL.Extraction + +/-- The index and length type of span members. -/ +def int32 := TypeExpr.atom runtimeScope "System.Int32" + +/-- The element type of the word-summing loop. -/ +def uint64 := TypeExpr.atom runtimeScope "System.UInt64" + +private def span (writable : Bool) (element : TypeExpr) := + TypeExpr.apply (.constructor runtimeScope (if writable then "System.Span`1" else + "System.ReadOnlySpan`1")) element + +private def readOnlyMember (element : TypeExpr) (name : MetadataName) (result : TypeExpr) + (parameters : List TypeExpr) : MethodRef := + { owner := span false element, name, hasThis := true, genericArity := 0, + returnType := result, parameters } + +/-- The readonly indexer carries the required modifier on its returned reference. -/ +def item (element : TypeExpr) := readOnlyMember element name!"get_Item" + (.atom runtimeScope "!0& modreq(System.Runtime.InteropServices.InAttribute)") [int32] + +/-- The selected Slice overload takes a start and optionally a length. -/ +def slice (element : TypeExpr) (counted : Bool) := readOnlyMember element name!"Slice" + (span false element) (if counted then [int32, int32] else [int32]) + +/-- Length of the closed readonly span. -/ +def length (element : TypeExpr) := readOnlyMember element name!"get_Length" int32 [] + +/-- The generic factory constructs a writable span over a managed reference and length. -/ +def createInt32 : MethodRef := + { owner := .atom runtimeScope "System.Runtime.InteropServices.MemoryMarshal", + name := name!"CreateSpan", hasThis := false, genericArity := 1, methodArguments := [int32], + returnType := span true int32, parameters := [.byref int32, int32] } + +end Tests.SpanMethods \ No newline at end of file diff --git a/Tests/SupportExtractionCache.cs b/Tests/SupportExtractionCache.cs new file mode 100644 index 000000000..c3ff7566a --- /dev/null +++ b/Tests/SupportExtractionCache.cs @@ -0,0 +1,74 @@ +#!/usr/bin/env dotnet +#:property PublishAot=false +#:package Mono.Cecil +#:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs + +using System.Text.Json; +using Mono.Cecil; +using Mono.Cecil.Cil; + +var root = At("artifacts/SupportExtractionCache-" + Guid.NewGuid().ToString("N")); +Directory.CreateDirectory(root); +var assembly = Path.Combine(root, "Examples.dll"); +File.Copy(At("Examples/Assembly/bin/Examples.dll"), assembly); +var proof = Path.Combine(root, "Proof.lean"); +File.WriteAllText(proof, """ + import Extracted + import Tests.Support + namespace ExampleProof + theorem correctness : + (CIL.Safety.invoke Extracted.program 8 0 [.scalar (.i64 0), .scalar (.i64 0)] + CIL.Safety.Memory.empty).map Prod.snd = .ok [.scalar (.i64 0)] := rfl + #print axioms correctness + end ExampleProof + """); +var saved = new[] { "LEANCIL_CACHE_DIR", "LEANCIL_DISABLE_SUCCESS_CACHE", "LEANCIL_TRACE_DIRECTORY" } + .ToDictionary(name => name, Environment.GetEnvironmentVariable); +try +{ + Environment.SetEnvironmentVariable("LEANCIL_CACHE_DIR", Path.Combine(root, "Cache")); + Environment.SetEnvironmentVariable("LEANCIL_DISABLE_SUCCESS_CACHE", null); + const string entry = "Arithmetic::Add(ulong,ulong)"; + var output = Path.Combine(root, "Extraction"); + + (string Identity, string Generated) Verify(string name, bool reused) + { + var trace = Path.Combine(root, name); + Environment.SetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY", trace); + using var verifier = new Program(); + verifier.Verify(assembly, "Examples", entry, proof, output); + var built = Directory.EnumerateFiles(trace, "*.jsonl").SelectMany(File.ReadLines) + .Select(line => JsonSerializer.Deserialize(line)) + .Any(stage => stage.GetProperty("stage").GetString() == "Building proof dependencies: " + entry); + if ((verifier.supportCacheHits == 1, built) != (reused, !reused)) + throw new Exception($"{name}: support cache hit={verifier.supportCacheHits}, built={built}; " + + $"expected reuse={reused}"); + var identity = ExtractionIdentity.Read(output); + return (identity.Sha256, identity.Root.GetProperty("generatedSha256").GetString()!); + } + + var initial = Verify("Initial", reused: false); + var unchanged = Verify("Unchanged", reused: true); + if (unchanged != initial) throw new Exception("Unchanged assembly produced a different extraction"); + + // Both opcodes return zero for these inputs; the proof and support sources remain identical. + using (var module = ModuleDefinition.ReadModule(assembly, new ReaderParameters { InMemory = true })) + { + var method = module.GetType("Examples.Arithmetic").Methods.Single(method => method.Name == "Add"); + method.Body.Instructions.Single(instruction => instruction.OpCode == OpCodes.Add).OpCode = OpCodes.Sub; + module.Write(assembly); + } + var changed = Verify("Changed", reused: false); + if (changed.Identity == initial.Identity || changed.Generated == initial.Generated) + throw new Exception("The changed instruction did not change the extraction identity and generated code"); + Console.WriteLine("PASS unchanged extraction reuses support; changed instruction rebuilds support"); +} +finally +{ + foreach (var (name, value) in saved) Environment.SetEnvironmentVariable(name, value); +} diff --git a/Tests/TailCallFixtures.cs b/Tests/TailCallFixtures.cs index f06129834..391eaafe1 100644 --- a/Tests/TailCallFixtures.cs +++ b/Tests/TailCallFixtures.cs @@ -8,6 +8,7 @@ static void AddTailCallFixtures(string assembly, string root, GeneratedEvidence { var names = new[] { "Forward", "ForwardReference", "ForwardVoid", "ForwardThrow", "LocalReference" }; var dll = Path.Combine(root, "TailCalls.dll"); + var callPcs = new Dictionary(StringComparer.Ordinal); using (var module = ModuleDefinition.ReadModule(assembly)) { foreach (var method in module.GetType("Examples.TailCalls").Methods.Where(method => names.Contains(method.Name))) @@ -15,6 +16,7 @@ static void AddTailCallFixtures(string assembly, string root, GeneratedEvidence var call = method.Body.Instructions.Single(instruction => instruction.OpCode.Code == Code.Call); if (call.Next?.OpCode.Code != Code.Ret) throw new InvalidOperationException("Missing tail-call fixture return"); method.Body.GetILProcessor().InsertBefore(call, Instruction.Create(OpCodes.Tail)); + callPcs.Add(method.Name, method.Body.Instructions.IndexOf(call)); } module.Write(dll); } @@ -25,8 +27,9 @@ static void AddTailCallFixtures(string assembly, string root, GeneratedEvidence var output = Path.Combine(root, "Tail-" + name); Run(["dotnet", ExtractorPath, "extract", dll, $"TailCalls::{name}({signature})", output]); var source = File.ReadAllText(Path.Combine(output, "Extracted.lean")); - const string tail = ".tailCall 1 1"; - if (!source.Contains(tail, StringComparison.Ordinal)) + var pc = callPcs[name]; + var tailLinks = DirectLinks(source, 1).Where(call => call.Method == 0).ToArray(); + if (tailLinks.Length != 1 || tailLinks[0].Pc != pc || !IsDecodedMethod(source, 0)) throw new InvalidOperationException("Missing emitted tail transfer: " + name); var argument = byref ? ".reference (.address ⟨0, 0⟩)" : ".scalar (.i32 42)"; var result = name switch @@ -37,7 +40,19 @@ static void AddTailCallFixtures(string assembly, string root, GeneratedEvidence _ => null }; var proof = Path.Combine(root, "Tail-" + name + ".lean"); - File.WriteAllText(proof, source + $$""" + File.WriteAllText(proof, $$""" + {{source}} + + example : Extracted.method0.code[{{pc - 1}}]? = some .nop := + Extracted.method0_instruction{{pc - 1}} + example : Extracted.method0.code[{{pc}}]? = + ((Extracted.method0Closed.code[{{pc}}]?).bind fun raw => + match raw.operand with + | .call site => some (.call site.method { tail := true }) + | _ => none) := by + rw [Extracted.method0_instruction{{pc}}] <;> rfl + example : Extracted.method0.code[{{pc + 1}}]? = some .ret := + Extracted.method0_instruction{{pc + 1}} private def tailMemory : CIL.Safety.Memory := { allocations := fun identity => if identity == 0 then @@ -56,7 +71,7 @@ static void AddTailCallFixtures(string assembly, string root, GeneratedEvidence """); else { - var fault = name == "ForwardThrow" ? ".thrown \"System.InvalidOperationException\"" : + var fault = name == "ForwardThrow" ? ".thrown .invalidOperation" : ".memory .expiredLifetime (.address ⟨1, 0⟩) 0"; File.AppendAllText(proof, $$""" @@ -77,22 +92,25 @@ static void AddTailCallFixtures(string assembly, string root, GeneratedEvidence """); evidence.Add(proof, "PASS compiled tail transfer and lifetime: " + name); if (name != "Forward") continue; - foreach (var (mutation, before, after, marker) in new[] + const string feature = "method0_features_validated"; + var featureSource = NarrowEvidence(source, feature); + var control = Path.Combine(root, "Tail-prefix-control.lean"); + File.WriteAllText(control, featureSource); + evidence.Add(control, "PASS the raw tail prefix agrees with the decoded call", feature); + const string withoutTail = "fun op => match op with | .call target _ => .call target {} | _ => op"; + foreach (var (mutation, changed, markers) in new[] { - ("dropped", tail, ".call 1 1", - "Extraction.FeatureFlowValid rawLayouts.toArray (Extraction.evidenceConstants methodEvidence) " + - "method0Closed"), - ("prefix", "opcode := 65044", "opcode := 0", - "Extraction.FeatureFlowValid rawLayouts.toArray (Extraction.evidenceConstants methodEvidence) " + - "method0Closed"), - ("target", tail, ".tailCall 0 1", "Extraction.ManagedCallsValid") + ("dropped", ForgeDecodedInstruction(source, 0, pc, withoutTail), + new[] { "Extraction.FeatureFlowValid", FeatureObligation(0) }), + ("prefix", ForgeClosedCode(featureSource, 0, + $$"""modify {{pc - 1}} (fun raw => { raw with opcode := 0 })"""), + ["Extraction.FeatureFlowValid", FeatureObligation(0), "is false"]), + ("target", ForgeLink(source, 0, pc, ".direct 0"), ["Extraction.ManagedCallsValid"]) }) { - if (!source.Contains(before, StringComparison.Ordinal)) - throw new InvalidOperationException("Missing tail mutation target: " + mutation); var forged = Path.Combine(root, "Forged-tail-" + mutation + ".lean"); - File.WriteAllText(forged, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(forged, "PASS tail certificate rejection: " + mutation, marker); + File.WriteAllText(forged, changed); + rejected.Add(forged, "PASS tail certificate rejection: " + mutation, markers); } } var excludedDll = Path.Combine(root, "TailExcluded.dll"); @@ -117,11 +135,11 @@ static void AddTailCallFixtures(string assembly, string root, GeneratedEvidence (CIL.Safety.invoke Extracted.program 32 0 [.scalar (.i32 42)] { allocations := fun _ => none, cells := fun _ _ => ⟨0, false⟩, views := [], nextIdentity := 0 }).map Prod.snd = - .ok [.scalar (.i32 42)] := rfl + .ok [.scalar (.i32 42)] := by cil_kernel_rfl #print axioms excluded_indirect_tail """); evidence.Add(excludedProof, "PASS profile-excluded indirect tail"); - foreach (var mutation in new[] { "branch", "return", "missing-pointer", "stack", "type" }) + foreach (var mutation in new[] { "branch", "dead-branch", "return", "missing-pointer", "stack", "type" }) { var changed = Path.Combine(root, "Tail-Rejected-" + mutation + ".dll"); using (var module = ModuleDefinition.ReadModule(dll)) @@ -131,6 +149,8 @@ .ok [.scalar (.i32 42)] := rfl var call = prefix.Next; var processor = method.Body.GetILProcessor(); if (mutation == "branch") processor.InsertBefore(prefix, Instruction.Create(OpCodes.Br, call)); + // An unreachable branch after the final return still may not enter the tail call. + else if (mutation == "dead-branch") processor.Append(Instruction.Create(OpCodes.Br, call)); else if (mutation == "return") processor.InsertAfter(call, Instruction.Create(OpCodes.Nop)); else if (mutation == "stack") processor.InsertBefore(method.Body.Instructions[0], Instruction.Create(OpCodes.Ldc_I4_0)); @@ -148,7 +168,7 @@ .ok [.scalar (.i32 42)] := rfl "TailCalls::Forward(System.Int32)", Path.Combine(root, "Tail-Rejected-" + mutation)]); var marker = mutation switch { - "branch" => "middle of a tail transfer", + "branch" or "dead-branch" => "Branch enters the instruction after a prefix", "missing-pointer" => "Invalid evaluation stack during reachability", "stack" => "Tail call requires exactly its arguments", _ => "Unsupported tail prefix" diff --git a/Tests/TemplateIndexes.cs b/Tests/TemplateIndexes.cs index 71326c20f..bd8347abf 100644 --- a/Tests/TemplateIndexes.cs +++ b/Tests/TemplateIndexes.cs @@ -1,11 +1,25 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Reflection; using System.Text.Json; var assembly = Assembly.LoadFrom(ExtractorPath); +// The reflected CLI reads its bundled verification sources beside the extractor executable. +var savedBase = AppContext.GetData("APP_CONTEXT_BASE_DIRECTORY"); +try +{ + AppContext.SetData("APP_CONTEXT_BASE_DIRECTORY", Path.GetDirectoryName(assembly.Location) + + Path.DirectorySeparatorChar); + System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(assembly.GetType("Program", true)!.TypeHandle); +} +finally { AppContext.SetData("APP_CONTEXT_BASE_DIRECTORY", savedBase); } var emitter = assembly.GetType("Nethermind.LeanCIL.TemplateEmitter", true)!; var variantType = emitter.GetNestedType("Variant", BindingFlags.NonPublic)!; var run = emitter.GetMethod("Run", BindingFlags.Static | BindingFlags.NonPublic)!; @@ -13,23 +27,69 @@ var first = Path.Combine(root, "First"); var second = Path.Combine(root, "Second"); var output = Path.Combine(root, "Shared"); -object Variant(string path, bool avx, object[] methods) => JsonSerializer.Deserialize(JsonSerializer.Serialize(new +Type Model(string name) => assembly.GetType("Nethermind.LeanCIL." + name, true)!; +object Create(string name, params object?[] arguments) => Activator.CreateInstance(Model(name), arguments)!; +Array Typed(Type element, IEnumerable items) { - Directory = path, AssemblyHash = "same", Profile = new + var values = items.ToArray(); + var array = Array.CreateInstance(element, values.Length); + for (var index = 0; index < values.Length; index++) array.SetValue(values[index], index); + return array; +} +object Listed(Type element, IEnumerable items) => + Activator.CreateInstance(Model("ValueList`1").MakeGenericType(element), Typed(element, items))!; +Dictionary bodies = new(ReferenceEqualityComparer.Instance); +object Variant(string path, bool avx, object[] methods) +{ + var profile = JsonSerializer.Deserialize(JsonSerializer.Serialize(new { Name = avx ? "x64-v3" : "scalar", Architecture = avx ? "x64" : "scalar", NativeWidth = 64, LittleEndian = true, Avx2 = avx - }, Methods = methods -}), variantType)!; -object Method(string identity, params string[] ops) => new + }), Model("FeatureProfile"))!; + var variant = Activator.CreateInstance(variantType, path, "same", profile, + Typed(emitter.GetNestedType("Method", BindingFlags.NonPublic)!, methods))!; + var inputs = string.Concat(methods.Select((method, index) => $"def method{index} : Method := {bodies[method]}\n")); + variantType.GetField("DecoderInputs", BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(variant, inputs); + return variant; +} +FixtureSite Site(object instruction) => instruction switch { - Identity = identity, Metadata = "locals := [], returnsValue := false", Ops = ops, HasStaticSites = false + FixtureSite site => site, + "call 0" => new(".call (.arity 0)", ".direct 0"), + "tail call 0" => new(".call (.arity 0) { tail := true }", ".direct 0"), + "tail call 1" => new(".call (.arity 0) { tail := true }", ".direct 1"), + string code => new(code), + _ => throw new ArgumentException("Unrecognised template fixture instruction") }; +object FrameOf(bool returnsValue, params object[] sites) => Create("FrameMetadata", returnsValue, + Listed(Model("InitialValue"), []), Listed(Model("LocalKind"), []), Listed(typeof(int), []), + Listed(Model("ParameterHome"), []), Listed(Model("StaticSite"), sites), + "{ parameters := [], returnType := .void }"); +object MethodIn(string identity, object frame, int[] linked, params object[] ops) +{ + var sites = ops.Select(Site).ToArray(); + var features = sites.Select(site => site.Feature).OfType().Distinct(StringComparer.Ordinal) + .Select(feature => Named(Model("Feature"), feature)).ToArray(); + var method = Activator.CreateInstance(emitter.GetNestedType("Method", BindingFlags.NonPublic)!, + identity, frame, Typed(Model("Feature"), features), new int[sites.Length], linked)!; + var links = string.Join(", ", sites.Index().Where(pair => pair.Item.Link is not null) + .Select(pair => $"({pair.Index}, {pair.Item.Link})")); + var firstOther = Array.FindIndex(sites, site => site.Code != ".nop"); + var prefix = firstOther < 0 ? sites.Length : firstOther; + var code = "[" + string.Join(", ", sites.Skip(prefix).Select(site => site.Code)) + "]"; + if (prefix > 0) code = $"List.replicate {prefix} .nop ++ " + code; + bodies.Add(method, $"{{ code := {code}, locals := [], returnsValue := false, links := [{links}] }}"); + return method; +} +object Method(string identity, params object[] ops) => MethodIn(identity, FrameOf(false), [], ops); +object Linked(string identity, int target, params object[] ops) => MethodIn(identity, FrameOf(false), [target], ops); +var avx2Getter = new FixtureSite(".call { owner := .atom Extraction.intrinsicsScope \"System.Runtime.Intrinsics.X86.Avx2\", " + + "name := name!\"get_IsSupported\", hasThis := false, genericArity := 0, returnType := .atom Extraction.runtimeScope \"System.Boolean\", parameters := [] }", Feature: "Avx2"); var variants = new Dictionary(StringComparer.Ordinal) { - [first] = Variant(first, false, [Method("Caller", ".feature .avx2", ".nop", ".ret")]), + [first] = Variant(first, false, [Method("Caller", avx2Getter, ".nop", ".ret")]), [second] = Variant(second, true, - [Method("Helper", ".ret"), Method("Caller", ".feature .avx2", ".call 0 0", ".ret")]) + [Method("Helper", ".ret"), Linked("Caller", 0, avx2Getter, "call 0", ".ret")]) }; var read = typeof(Program).GetMethod(nameof(ReadVariants), BindingFlags.Static | BindingFlags.NonPublic)! .MakeGenericMethod(variantType).Invoke(null, [variants]); @@ -50,10 +110,14 @@ string Rejected(Action action) var templates = File.ReadAllText(Path.Combine(output, "Templates.lean")); var firstBinding = File.ReadAllText(Path.Combine(output, "TemplateBindings0.lean")); var secondBinding = File.ReadAllText(Path.Combine(output, "TemplateBindings1.lean")); -if (!templates.Contains("if avx2 then (.call callee0 0) else (.nop)", StringComparison.Ordinal) || - !firstBinding.Contains("Templates.method0 Extracted.profile false 0", StringComparison.Ordinal) || - !secondBinding.Contains("Templates.method1 Extracted.profile)", StringComparison.Ordinal) || - !secondBinding.Contains("Templates.method0 Extracted.profile true 0", StringComparison.Ordinal)) +if (!templates.Contains("if avx2 then (Input1.method1.code) else (Input0.method0.code)", StringComparison.Ordinal) || + !templates.Contains("(Input1.method1.reindexLinks (fun | 0 => callee0 | index => index)).links", + StringComparison.Ordinal) || + !firstBinding.Contains("Templates.method0 Extracted.profile Extracted.environment false 0", + StringComparison.Ordinal) || + !secondBinding.Contains("Templates.method1 Extracted.profile Extracted.environment)", StringComparison.Ordinal) || + !secondBinding.Contains("Templates.method0 Extracted.profile Extracted.environment true 0", + StringComparison.Ordinal)) throw new Exception("Template ordering, method remapping or excluded-callee placeholder changed"); var expected = Directory.GetFiles(output).ToDictionary(path => Path.GetFileName(path)!, File.ReadAllText); Emit(); @@ -109,6 +173,172 @@ string Rejected(Action action) rejected.Message == "Template inputs contain ambiguous managed method identities.") { } Console.WriteLine("PASS template order, reordered callees, absent-callee placeholders and duplicate rejection"); +// A tail call's link names its callee by the template's parameter too, whatever index each profile gives it. +var tailRoot = Path.Combine(root, "Tail"); +var (tailFirst, tailSecond, tailOutput) = (Path.Combine(tailRoot, "First"), Path.Combine(tailRoot, "Second"), + Path.Combine(tailRoot, "Shared")); +variants[tailFirst] = Variant(tailFirst, false, [Method("Target", ".ret"), Linked("Tail", 0, "tail call 0")]); +variants[tailSecond] = Variant(tailSecond, true, [Linked("Tail", 1, "tail call 1"), Method("Target", ".ret")]); +run.Invoke(null, [(string[])[tailOutput, tailFirst, tailSecond], read]); +var tailTemplate = File.ReadAllText(Path.Combine(tailOutput, "Templates.lean")); +if (!tailTemplate.Contains("cil_template_code% (Input0.method1.code)", StringComparison.Ordinal) || + !tailTemplate.Contains("links := (Input0.method1.reindexLinks (fun | 0 => callee0 | index => index)).links", + StringComparison.Ordinal) || + !tailTemplate.Contains("(Input1.method0.reindexLinks (fun | 1 => callee0 | index => index)).links := rfl", + StringComparison.Ordinal)) + throw new Exception("A tail call kept its profile's callee index in the template or lost its link"); +Console.WriteLine("PASS template tail calls take the callee parameter of the profile-specific index"); + +// A virtual dispatch, a callable pointer and a static initializer name their methods by the template's callee +// parameters too, whatever index each profile gives them. +const string managedId = "{ owner := \"ID\", token := 1, arguments := [] }"; +const string managedSignature = "{ result := .void, parameters := [] }"; +object InitializedSite(int initializer) => Create("StaticSite", 0, Create("StaticIdentityRecord", "id", "asm", "fld"), + Create("FieldRef", "T", 0, "U", ValueName()), Listed(typeof(byte), []), + Create("InitializedStaticValue", null, initializer, Listed(typeof(string), []), null, false)); +string SharedBody(string name, Func caller) +{ + var directory = Path.Combine(root, name); + var (one, two, shared) = (Path.Combine(directory, "First"), Path.Combine(directory, "Second"), + Path.Combine(directory, "Shared")); + variants[one] = Variant(one, false, [Method("Target", ".ret"), caller(0)]); + variants[two] = Variant(two, true, [caller(1), Method("Target", ".ret")]); + run.Invoke(null, [(string[])[shared, one, two], read]); + return File.ReadAllText(Path.Combine(shared, "Templates.lean")); +} +const string relocatedTarget = "(Input1.method0.reindexLinks (fun | 1 => callee0 | index => index)).links := rfl"; +var numbered = new (string Name, Func Caller, string Expected)[] +{ + ("CallVirtual", target => Linked("Caller", target, + new FixtureSite(".callvirt (.arity 0)", $".virtual [(.void, {target})]"), ".ret"), relocatedTarget), + ("MethodAddress", target => Linked("Caller", target, + new FixtureSite($".ldftn {managedId} {managedSignature}", $".address {target}"), ".ret"), + relocatedTarget), + ("IndirectCall", target => Linked("Caller", target, new FixtureSite($".calli {managedSignature} false", + $".indirect [{{ identity := {managedId}, method := {target} }}]"), ".ret"), relocatedTarget), + ("Initializer", target => MethodIn("Caller", FrameOf(false, InitializedSite(target)), [], ".ret"), + "initializer := some { method := callee0, arguments := []") +}; +foreach (var (name, caller, spelling) in numbered) + if (!SharedBody("Numbered" + name, caller).Contains(spelling, StringComparison.Ordinal)) + throw new Exception($"A {name} kept its profile's method index in the template"); +Console.WriteLine("PASS template dispatch, callable pointer and initializer methods take callee parameters"); + +var dispatchDifference = Rejected(() => SharedBody("DifferentReceiver", target => + Linked("Caller", target, ".nop", new FixtureSite(".callvirt (.arity 0)", + $".virtual [(.atom \"Fixture\" \"Receiver{target}\", {target})]"), ".ret"))); +if (!dispatchDifference.Contains("different instructions: Caller at pc 1", StringComparison.Ordinal)) + throw new Exception("Equal call instructions with different receiver tables were merged: " + dispatchDifference); +Console.WriteLine("PASS differing dispatch metadata names the call pc after target renumbering"); + +// Canonical templates compare the decoded method operands. +string Merge(string identity, object[] left, object[] right) +{ + var directory = Path.Combine(root, "Merge" + identity); + var (one, two, shared) = (Path.Combine(directory, "First"), Path.Combine(directory, "Second"), + Path.Combine(directory, "Shared")); + variants[one] = Variant(one, false, [Method(identity, left)]); + variants[two] = Variant(two, true, [Method(identity, right)]); + run.Invoke(null, [(string[])[shared, one, two], read]); + return File.ReadAllText(Path.Combine(shared, "Templates.lean")); +} +object Named(Type type, string value) => Enum.Parse(type, value); +object ValueName() => Model("MetadataName").GetMethod("OfText", BindingFlags.Static | BindingFlags.NonPublic)! + .Invoke(null, ["value"])!; +// Both template alternatives bind the closed runtime member that the compiled call names. +string ScalarCall(string owner, string name, string result, params string[] parameters) +{ + static string Word(string type) => $".atom Extraction.runtimeScope \"System.{type}\""; + return $$""".call { owner := {{Word(owner)}}, name := name!"{{name}}", hasThis := false, genericArity := 0, returnType := {{Word(result)}}, parameters := [{{string.Join(", ", parameters.Select(Word))}}] }"""; +} +var merged = new (string Identity, string Code)[] +{ + ("BigMul", ScalarCall("Math", "BigMul", "Int64", "Int32", "Int32")), + ("PopCount", ScalarCall("Numerics.BitOperations", "PopCount", "Int32", "UInt32")) +}; +foreach (var (identity, code) in merged) +{ + var body = Merge(identity, [code, ".ret"], [$"id ({code} : Instruction)", ".ret"]); + if (!body.Contains("cil_template_code% (Input0.method0.code)", StringComparison.Ordinal)) + throw new Exception($"The {identity} alternatives must share one decoded body."); +} +// A decoded field reference carries the whole operand, including its metadata name. +string Store(int index, string name) => + $".stfld {{ owner := .object \"Fixture\" \"Owner\", index := {index}, " + + $"type := .atom Extraction.runtimeScope \"System.UInt32\", name := name!\"{name}\" }}"; +var store = Store(0, "word"); +var sharedStore = Merge("Store", [".nop", store, ".ret"], [".nop", $"id ({store} : Instruction)", ".ret"]); +if (!sharedStore.Contains("cil_template_code% (Input0.method0.code)", StringComparison.Ordinal)) + throw new Exception("Equal decoded field stores failed to share a template body."); +foreach (var (identity, different) in new[] { ("FieldIndex", Store(1, "word")), ("FieldName", Store(0, "other")) }) + if (!Rejected(() => Merge(identity, [".nop", store, ".ret"], [".nop", different, ".ret"])) + .Contains($"Profiles with identical observed flags emit different instructions: {identity} at pc 1")) + throw new Exception("A store to a different field shared the template body."); +if (!Rejected(() => Merge("Rotate", [ScalarCall("Numerics.BitOperations", "RotateLeft", "UInt32", "UInt32", "Int32"), ".ret"], + [ScalarCall("Numerics.BitOperations", "RotateLeft", "UInt64", "UInt64", "Int32"), ".ret"])) + .Contains("Profiles with identical observed flags emit different instructions: Rotate at pc 0")) + throw new Exception("Profiles that render a rotate differently merged"); +Console.WriteLine("PASS template merge accepts what the Lean text equates and names the instruction that differs"); + +// Profiles must agree on the frame and the instruction count before any instruction is compared. +string Differing(string identity, object leftFrame, object[] left, object rightFrame, object[] right) +{ + var directory = Path.Combine(root, "Differing" + identity); + var (one, two, shared) = (Path.Combine(directory, "First"), Path.Combine(directory, "Second"), + Path.Combine(directory, "Shared")); + variants[one] = Variant(one, false, [MethodIn(identity, leftFrame, [], left)]); + variants[two] = Variant(two, true, [MethodIn(identity, rightFrame, [], right)]); + return Rejected(() => run.Invoke(null, [(string[])[shared, one, two], read])); +} +if (!Differing("Frame", FrameOf(false), [".ret"], FrameOf(true), [".ret"]) + .Contains("Template metadata or instruction count differs between profiles: Frame") || + !Differing("Count", FrameOf(false), [".ret"], FrameOf(false), [".nop", ".ret"]) + .Contains("Template metadata or instruction count differs between profiles: Count") || + !Differing("Leaf", FrameOf(false), [".nop", ".ret"], FrameOf(false), [".ret", ".ret"]) + .Contains("Profiles with identical observed flags emit different instructions: Leaf at pc 0")) + throw new Exception("Profiles that differ in frame, length or an unflagged instruction were merged"); +Console.WriteLine("PASS template merge rejects differing frames, lengths and unflagged instructions"); + +// At 64x instructions, a quadratic merge or renderer costs 4096x, above the 256x bound. +TimeSpan EmitTime(int length, ref int iterations, bool calibrate) +{ + var scaleRoot = Path.Combine(root, "Scale" + length); + var (one, two) = (Path.Combine(scaleRoot, "First"), Path.Combine(scaleRoot, "Second")); + string[] ops = [.. Enumerable.Repeat(".nop", length - 1), ".ret"]; + variants[one] = Variant(one, false, [Method("Long", ops)]); + variants[two] = Variant(two, true, [Method("Long", ops)]); + TimeSpan Measure(int repeats) + { + var best = TimeSpan.MaxValue; + for (var attempt = 0; attempt < 3; attempt++) + { + var clock = System.Diagnostics.Stopwatch.StartNew(); + for (var iteration = 0; iteration < repeats; iteration++) + run.Invoke(null, [(string[])[Path.Combine(scaleRoot, "Out" + attempt), one, two], read]); + best = TimeSpan.FromTicks(Math.Min(best.Ticks, clock.Elapsed.Ticks)); + } + return best; + } + var elapsed = Measure(iterations); + const double minimumMilliseconds = 25; + while (calibrate && elapsed.TotalMilliseconds < minimumMilliseconds) + { + iterations = checked(iterations * 2); + elapsed = Measure(iterations); + } + return elapsed; +} +var iterations = 1; +var small = EmitTime(1_250, ref iterations, calibrate: true); +var large = EmitTime(80_000, ref iterations, calibrate: false); +if (large.TotalMilliseconds > 256 * small.TotalMilliseconds) + throw new Exception($"Template emission grew faster than linearly: {small.TotalMilliseconds:F0} ms for 1250 " + + $"instructions, {large.TotalMilliseconds:F0} ms for 80000"); +Console.WriteLine("PASS template emission is linear in the instructions " + + $"({small.TotalMilliseconds:F0} ms, {large.TotalMilliseconds:F0} ms for {iterations} emissions)"); + +sealed record FixtureSite(string Code, string? Link = null, string? Feature = null); + partial class Program { static Func ReadVariants(Dictionary variants) => path => (T)variants[path]; diff --git a/Tests/ThrowEvidence.lean b/Tests/ThrowEvidence.lean index f2776ab83..4986ef550 100644 --- a/Tests/ThrowEvidence.lean +++ b/Tests/ThrowEvidence.lean @@ -1,3 +1,4 @@ +import CIL.Extraction.DecodedMethod import CIL.Extraction.Features set_option maxRecDepth 4096 @@ -13,12 +14,17 @@ private def owner := "Fixture, Version=1.0.0.0, Culture=neutral, PublicKeyToken= private def intType : TypeExpr := .atom runtime "System.Int32" private def ctor (scope type : String) (parameters : Nat) : RawOperand := .call { - declaringType := .atom scope type, name := ".ctor", + declared := { + convention := 0x20, + parameters := List.replicate parameters (.object runtime "System.String") }, + method := { + owner := .atom scope type, name := name!".ctor", hasThis := true, genericArity := 0, + returnType := .void, + parameters := List.replicate parameters (.object runtime "System.String") }, + name := ".ctor", target := .external (scope ++ "::System.Void " ++ type ++ "::.ctor(" ++ - ",".intercalate (List.replicate parameters "System.String") ++ ")"), - typeArguments := [], parameterCount := parameters, - parameterTypes := List.replicate parameters (.object runtime "System.String"), - hasThis := true, returnsValue := false } + ",".intercalate (List.replicate parameters "System.String") ++ ")"), typeArguments := [], + parameterCount := parameters, returnsValue := false } private def argument := ctor runtime "System.ArgumentException" 1 private def divide := ctor runtime "System.DivideByZeroException" 0 @@ -31,25 +37,39 @@ private def raw (code : List RawInstruction) : RawMethod := { private def state (stack : List (Option W32)) : Option FeatureState := some ⟨stack, []⟩ -- The exact encoding folds; extra loads, a wrong arity or a non-runtime type do not. -example : foldRoles [] [i 114 (.text "zero"), i 115 argument, i 122] = - [some .head, some .interior, some (.raise "System.ArgumentException")] := by decide -example : foldRoles [] [i 115 divide, i 122] = [some .head, some (.raise "System.DivideByZeroException")] := by decide -example : foldRoles [] [i 20, i 114 (.text "zero"), i 115 argument, i 122] = - [none, some .head, some .interior, some (.raise "System.ArgumentException")] := by decide +example : foldRoles [i 114 (.text "zero"), i 115 argument, i 122] = + [some .head, some .interior, some (.raise .argument)] := by decide +example : foldRoles [i 115 divide, i 122] = [some .head, some (.raise .divideByZero)] := by + decide +example : foldRoles [i 20, i 114 (.text "zero"), i 115 argument, i 122] = + [none, some .head, some .interior, some (.raise .argument)] := by decide example : throwFold [i 115 argument, i 122] = none := by decide example : throwFold [i 115 (ctor owner "Fixture.Failure" 0), i 122] = none := by decide example : throwFold [i 115 divide, i 38, i 122] = none := by decide +/-- A throw message has the operand of a string or null load. -/ +theorem message_operands : + throwFold [i 114, i 115 argument, i 122] = none ∧ + throwFold [i 20 (.text "forged"), i 115 argument, i 122] = none := by decide +#print axioms message_operands + +/-- An ordinary string load retains its text, while a throw consumes its own message. -/ +theorem string_and_throw : + let closed := raw [i 114 (.text "keep"), i 38, i 114 (.text "message"), i 115 argument, i 122] + methodCode .scalar {} [] (List.replicate 5 true) closed = + [.ldstr "keep", .pop, .nop, .nop, .throw .argument] := rfl +#print axioms string_and_throw + /-- Rejecting an oversized message prefix still discovers an admitted fold in its suffix. -/ theorem long_prefix_suffix : - foldRoles [] ([i 20, i 20, i 20] ++ + foldRoles ([i 20, i 20, i 20] ++ [i 115 (ctor runtime "System.ArgumentException" 2), i 122]) = [none, some .head, some .interior, some .interior, - some (.raise "System.ArgumentException")] := by decide +kernel + some (.raise .argument)] := by decide +kernel #print axioms long_prefix_suffix /-- A long message-only body never invents a constructor or throw. -/ -theorem message_only_body : foldRoles [] (List.replicate 128 (i 20)) = +theorem message_only_body : foldRoles (List.replicate 128 (i 20)) = List.replicate 128 none := by decide +kernel #print axioms message_only_body @@ -61,78 +81,173 @@ theorem rejects_missing_constructor : #print axioms rejects_missing_constructor private def wrongParameter : RawOperand := .call { - declaringType := .atom runtime "System.DivideByZeroException", name := ".ctor", - target := .external (runtime ++ "::System.Void System.DivideByZeroException::.ctor(System.Int32)"), - typeArguments := [], parameterCount := 1, hasThis := true, returnsValue := false } - -theorem rejects_wrong_signature : throwFold [i 114 (.text "a"), i 115 wrongParameter, i 122] = none := by decide -#print axioms rejects_wrong_signature + declared := { convention := 0x20, parameters := [intType] }, + method := { + owner := .atom runtime "System.DivideByZeroException", name := name!".ctor", hasThis := true, + genericArity := 0, returnType := .void, parameters := [] }, + name := ".ctor", + target := .external (runtime ++ "::System.Void System.DivideByZeroException::.ctor(\ + System.Int32)"), + typeArguments := [], parameterCount := 1, returnsValue := false } + +/-- A constructor whose declared parameter count differs from its closed parameters is refused. -/ +theorem rejects_parameter_count : + throwFold [i 114 (.text "a"), i 115 wrongParameter, i 122] = none := by decide +#print axioms rejects_parameter_count + +/-- The call operand with one field of its site changed. -/ +private def changeSite (operand : RawOperand) (change : CallSite → CallSite) : RawOperand := + match operand with + | .call site => .call (change site) + | other => other + +/-- Changes to a nongeneric exception constructor that retain the printed external target. -/ +private def alteredConstructors : List RawOperand := [ + changeSite divide fun site => { site with declared.convention := 0x25 }, + changeSite divide fun site => { site with declared.convention := 0x60 }, + changeSite divide fun site => { site with declared.returnType := intType }, + changeSite divide fun site => { site with declared.parameters := [intType] }, + changeSite divide fun site => { site with method.genericArity := 1 }, + changeSite divide fun site => { site with method.methodArguments := [intType] }, + changeSite divide fun site => { site with ownerArgumentCount := 1 }, + changeSite divide fun site => { site with staticVirtualInterfaceSlot := true }] + +/-- The ordinary instance signature is required at the instruction that constructs the exception. -/ +theorem exact_exception_signatures : + throwFold [i 115 divide, i 122] = some (0, .divideByZero) ∧ + alteredConstructors.all (fun operand => (throwFold [i 115 operand, i 122]).isNone) := by + decide +kernel +#print axioms exact_exception_signatures + +/-- The checked feature flow accepts the ordinary constructor and refuses each altered MemberRef. -/ +theorem exact_exception_certificate : + let accepts := fun operand => FeatureFlowValid #[] (fun _ => none) (fun _ => none) + (raw [i 115 operand, i 122]) .scalar [state [], state [none]] [true, true] + [.nop, .throw .divideByZero] + accepts divide ∧ ∀ operand ∈ alteredConstructors, ¬ accepts operand := by decide +kernel +#print axioms exact_exception_certificate + +/-- An Int32 parameter outside the allowlisted constructor signatures is refused. -/ +theorem rejects_int32_parameter : + throwFold [i 114 (.text "a"), i 115 (changeSite wrongParameter fun site => + { site with method.parameters := [intType] }), i 122] = none := by decide +#print axioms rejects_int32_parameter + +/-- An allowlisted exception name in another scope raises nothing, whether the owner or the + external target names the other scope. -/ +theorem rejects_foreign_exception : + throwFold [i 115 (ctor "Fixture" "System.DivideByZeroException" 0), i 122] = none ∧ + throwFold [i 115 (changeSite divide fun site => + { site with method.owner := .atom "Fixture" "System.DivideByZeroException" }), i 122] = + none := by decide +#print axioms rejects_foreign_exception + +/-- The constructor is identified by its metadata name, so a site whose display text alone says + .ctor raises nothing. -/ +theorem rejects_named_other_member : + throwFold [i 115 divide, i 122] = some (0, .divideByZero) ∧ + throwFold [i 115 (changeSite divide fun site => + { site with method.name := name!"Other" }), i 122] = none := by decide +#print axioms rejects_named_other_member theorem accepts_two_strings : throwFold [i 20, i 114 (.text "message"), i 115 (ctor runtime "System.ArgumentOutOfRangeException" 2), i 122] = - some (2, "System.ArgumentOutOfRangeException") := by decide + some (2, .argumentOutOfRange) := by decide #print axioms accepts_two_strings theorem rejects_non_exception_certificate : - ¬ FeatureFlowValid #[] (fun _ => none) + ∀ claimed ∈ ExceptionType.all, ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [i 115 (ctor runtime "System.Object" 0), i 122]) .scalar - [state [], state [none]] [true, true] [.nop, .throw "System.Object"] := by decide + [state [], state [none]] [true, true] [.nop, .throw claimed] := by decide +kernel #print axioms rejects_non_exception_certificate -- 0: ldarg.0; 1: brtrue 5; 2: ldstr; 3: newobj ArgumentException(string); 4: throw; 5: ldarg.0; 6: ret private def guardRaw := raw [i 2, i 45 (.target 5), i 114 (.text "zero"), i 115 argument, i 122, i 2, i 42] private def guardStates : FeatureCertificate := [state [], state [none], state [], state [none], state [none], state [], state [none]] -private def guardCode : List Op := - [.arg 0, .brnonzero 5, .nop, .nop, .throw "System.ArgumentException", .arg 0, .ret none] +private def guardCode : List Instruction := + [.ldarg 0, .brtrue 5, .nop, .nop, .throw .argument, .ldarg 0, .ret] -example : FeatureFlowValid #[] (fun _ => none) guardRaw .scalar guardStates +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) guardRaw .scalar guardStates (List.replicate 7 true) guardCode := by decide -- The raised type is the constructed one, and the fold cannot be emitted as a hole. -example : ¬ FeatureFlowValid #[] (fun _ => none) +example : ∀ other ∈ ExceptionType.all, other ≠ .argument → ¬ FeatureFlowValid #[] (fun _ => none) + (fun _ => none) guardRaw .scalar guardStates (List.replicate 7 true) - (guardCode.set 4 (.throw "System.InvalidOperationException")) := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) + (guardCode.set 4 (.throw other)) := by decide +kernel +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) guardRaw .scalar guardStates (List.replicate 7 true) - (guardCode.set 4 (.unsupported "hidden")) := by decide + (guardCode.set 4 (.unadmitted 122)) := by decide -- A branch to the raw `throw` would raise whatever object it carries, so it is rejected. private def enteredRaw := raw [i 2, i 37, i 45 (.target 6), i 38, i 114 (.text "zero"), i 115 argument, i 122] -example : ¬ FeatureFlowValid #[] (fun _ => none) enteredRaw .scalar +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) enteredRaw .scalar [state [], state [none], state [none, none], state [none], state [], state [none], state [none]] - (List.replicate 7 true) [.arg 0, .dup, .brnonzero 6, .pop, .nop, .nop, .throw "System.ArgumentException"] := by decide + (List.replicate 7 true) [.ldarg 0, .dup, .brtrue 6, .pop, .nop, .nop, + .throw .argument] := by decide /-- An adjacent raw null is the only operand admitted by the null-throw fold. -/ theorem null_throw_encoding : - foldRoles [] [i 20, i 122] = - [some .head, some (.raise "System.NullReferenceException")] ∧ - FeatureFlowValid #[] (fun _ => none) (raw [i 20, i 122]) .scalar - [state [], state [none]] [true, true] [.nop, .throw "System.NullReferenceException"] ∧ - ¬ FeatureFlowValid #[] (fun _ => none) (raw [i 22, i 122]) .scalar - [state [], state [some 0]] [true, true] [.nop, .throw "System.NullReferenceException"] ∧ - ¬ FeatureFlowValid #[] (fun _ => none) (raw [i 20 (.text "forged"), i 122]) .scalar - [state [], state [none]] [true, true] [.nop, .throw "System.NullReferenceException"] ∧ - ¬ FeatureFlowValid #[] (fun _ => none) (raw [i 20, i 0, i 122]) .scalar + foldRoles [i 20, i 122] = + [some .head, some (.raise .nullReference)] ∧ + FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [i 20, i 122]) .scalar + [state [], state [none]] [true, true] [.ldnull, .throw .nullReference] ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [i 20, i 122]) .scalar + [state [], state [none]] [true, true] [.nop, .throw .nullReference] ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [i 22, i 122]) .scalar + [state [], state [some 0]] [true, true] [.ldnull, .throw .nullReference] ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [i 20 (.text "forged"), i 122]) + .scalar + [state [], state [none]] [true, true] [.ldnull, .throw .nullReference] ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw [i 20, i 0, i 122]) .scalar [state [], state [none], state [none]] [true, true, true] - [.nop, .nop, .throw "System.NullReferenceException"] := by decide +kernel + [.ldnull, .nop, .throw .nullReference] := by decide +kernel #print axioms null_throw_encoding + /-- A branch may enter at the null load, but not skip it to reach the folded throw. -/ theorem null_throw_entry : - FeatureFlowValid #[] (fun _ => none) (raw [i 20, i 43 (.target 2), i 20, i 122]) .scalar + FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw + [i 20, i 43 (.target 2), i 20, i 122]) .scalar [state [], state [none], state [none], state [none, none]] [true, true, true, true] - [.memory .nullRef, .branch 2, .nop, .throw "System.NullReferenceException"] ∧ - ¬ FeatureFlowValid #[] (fun _ => none) (raw [i 20, i 43 (.target 3), i 20, i 122]) .scalar + [.ldnull, .br 2, .ldnull, .throw .nullReference] ∧ + ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw + [i 20, i 43 (.target 3), i 20, i 122]) .scalar [state [], state [none], none, state [none]] [true, true, false, true] - [.memory .nullRef, .branch 3, .unsupported "dead", .throw "System.NullReferenceException"] := + [.ldnull, .br 3, .unadmitted 20, .throw .nullReference] := by decide +kernel #print axioms null_throw_entry +/-- A direct edge or switch arm into the throw disables just the entered fold. -/ +theorem decoded_throw_entry : + let source := fun edge => raw + [i 2, i 45 (.target 4), i 20, edge, i 115 divide, i 122, i 115 divide, i 122] + let mask := List.replicate 8 true + let decode := fun edge => (Method.ofRaw? .scalar {} [] mask (source edge) []).map + fun body => (body.code[5]?, body.code[7]?) + let refused := fun edge => + let code := methodCode .scalar {} [] mask (source edge) + decode edge = none ∧ code[5]? = some (.unadmitted 122) ∧ + code[7]? = some (.throw .divideByZero) + let raised := some (Instruction.throw .divideByZero) + decode (i 43 (.target 4)) = some (raised, raised) ∧ + refused (i 43 (.target 5)) ∧ refused (i 69 (.targets [4, 5])) := by decide +kernel + +#print axioms decoded_throw_entry + -- A null throw cannot claim another exception type. -example : ¬ FeatureFlowValid #[] (fun _ => none) - (raw [i 20, i 122]) .scalar [state [], state [none]] - [true, true] - [.nop, .throw "System.Object"] := by decide +example : ∀ other ∈ ExceptionType.all, other ≠ .nullReference → ¬ FeatureFlowValid #[] + (fun _ => none) (fun _ => none) (raw [i 20, i 122]) .scalar [state [], state [none]] [true, + true] + [.ldnull, .throw other] := by decide +kernel + +/-- Types outside the constructor allowlist cannot form a throw fold. -/ +theorem rejects_unadmitted_owners : + foldRoles [i 115 (ctor runtime "System.PlatformNotSupportedException" 0), i 122] = + [none, none] ∧ + foldRoles [i 115 (ctor runtime "System.InvalidCastException" 0), i 122] = [none, none] ∧ + exceptionOwner? (.atom runtimeScope "System.Object") = none := by decide +kernel +#print axioms rejects_unadmitted_owners end Tests.ThrowEvidence diff --git a/Tests/Throws.cs b/Tests/Throws.cs index 9b617d1f8..7d0c3ecf7 100644 --- a/Tests/Throws.cs +++ b/Tests/Throws.cs @@ -2,13 +2,18 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include RejectedEvidence.cs #:include GeneratedEvidence.cs #:include ../scripts/BatchVerification.cs +#:include ExceptionMembers.cs using System.Reflection; -using System.Text.RegularExpressions; using Mono.Cecil; using Mono.Cecil.Cil; @@ -20,6 +25,8 @@ var queue = new GeneratedQueue(verification, assembly); var controls = new GeneratedEvidence(Path.Combine(root, "EvidenceControls")); var runtime = Assembly.Load(File.ReadAllBytes(assembly)).GetType("Examples.Throws")!; +using var exampleModule = ModuleDefinition.ReadModule(assembly); +var throwMethods = exampleModule.GetType("Examples.Throws").Methods; RequireLeanFixtures("CIL.Extraction.Translation", "CIL.Safety.Execution"); // Every fault is located at the folded `throw` and names the type .NET raises. @@ -37,11 +44,13 @@ Run(Extract("extract", assembly, "Examples", selector, folder)); var extracted = Path.Combine(folder, "Extracted.lean"); var source = File.ReadAllText(extracted); - var site = ExtractedInstructions(source) - .Select(instruction => (instruction, Throw: Regex.Match(instruction.Op, @"\A\.throw ""([\w.]+)""\z"))) - .FirstOrDefault(candidate => candidate.Throw.Success); - if (site.instruction is null) throw new InvalidOperationException($"No folded throw in {name}"); - var (method, pc, label) = (site.instruction.Method, site.instruction.Pc, site.Throw.Groups[1].Value); + var entry = throwMethods.Single(candidate => candidate.Name == name); + var method = name is "Null" or "Quotient" ? 1 : 0; + var owner = method == 0 ? entry : ((MethodReference)entry.Body.Instructions + .Single(instruction => instruction.OpCode.Code == Code.Call).Operand).Resolve(); + var body = owner.Body.Instructions; + var pc = body.IndexOf(body.Single(instruction => instruction.OpCode.Code == Code.Throw)); + if (!IsDecodedMethod(source, method)) throw new InvalidOperationException($"Expected decoded {owner.FullName}"); if (name == "Null") { var obligation = $"method{method}_features_validated"; @@ -55,48 +64,100 @@ if (!source.Contains(before, StringComparison.Ordinal)) throw new InvalidOperationException("Missing null-throw operand: " + opcode); var forgedOperand = Path.Combine(folder, $"Operand-{opcode}.lean"); - File.WriteAllText(forgedOperand, source.Replace(before, - $"opcode := {opcode}, operand := .integer 0", StringComparison.Ordinal)); + var rawPc = opcode == 122 ? pc : pc - 1; + File.WriteAllText(forgedOperand, ForgeClosedCode(source, method, + $$"""modify {{rawPc}} (fun raw => { raw with operand := .integer 0 })""")); rejected.Add(forgedOperand, "PASS null-throw operand forgery rejected: " + opcode, - $"Extraction.FeatureFlowValid rawLayouts.toArray (Extraction.evidenceConstants methodEvidence) " + - $"method{method}Closed", "is false"); + "Extraction.FeatureFlowValid", FeatureObligation(method), "is false"); } } + if (name == "Quotient") + { + var ctorPc = body.IndexOf(body.Single(site => site.OpCode.Code == Code.Newobj)); + var obligation = FeatureObligation(method); + var evidence = NarrowEvidence(source, $"method{method}_features_validated"); + var control = Path.Combine(folder, "ConstructorControl.lean"); + File.WriteAllText(control, evidence); + controls.Add(control, "PASS compiled exception constructor signature", $"method{method}_features_validated"); + foreach (var (changed, field) in new[] + { + ("vararg", "declared.convention := 0x25"), + ("explicit-this", "declared.convention := 0x60"), + ("result", "declared.returnType := .atom Extraction.runtimeScope \"System.Int32\""), + ("generic-arity", "method.genericArity := 1") + }) + { + var changedFile = Path.Combine(folder, $"Constructor-{changed}.lean"); + File.WriteAllText(changedFile, ForgeClosedCode(evidence, method, $$""" + modify {{ctorPc}} (fun raw => { raw with operand := match raw.operand with + | .call site => .call { site with {{field}} } + | operand => operand }) + """)); + rejected.Add(changedFile, $"PASS altered {changed} exception constructor rejected", + "Extraction.FeatureFlowValid", obligation, "is false"); + } + } + + string? label = null; var claims = inputs.Select(input => { string outcome; try { outcome = $".ok [.scalar (.i32 {(uint)runtime.GetMethod(name)!.Invoke(null, [.. input.Cast()])!})]"; } - catch (TargetInvocationException error) { outcome = $".error ⟨{method}, {pc}, .thrown \"{error.InnerException!.GetType().FullName}\"⟩"; } + catch (TargetInvocationException error) + { + label = ExceptionMember(error.InnerException!); + outcome = $".error \u27e8{method}, {pc}, .thrown .{label}\u27e9"; + } return $"(CIL.Safety.invoke Extracted.program 64 0 [{string.Join(", ", input.Select(v => $".scalar (.i32 {v})"))}] CIL.Safety.Memory.empty).map Prod.snd = {outcome}"; }).ToList(); + if (label is null) throw new InvalidOperationException($"No runtime input reached the throw in {name}"); + var nullLoads = body.Select((site, index) => (site, index)).Where(item => item.site.OpCode.Code == Code.Ldnull); + var nullEquations = string.Join('\n', nullLoads.Select(item => + $"example : Extracted.method{method}.code[{item.index}]? = some .ldnull := " + + $"Extracted.method{method}_instruction{item.index}")); var proof = Path.Combine(folder, "BoundaryProof.lean"); - File.WriteAllText(proof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\ntheorem correctness :\n" + - string.Join(" ∧\n", claims) + " := by\n exact ⟨" + string.Join(", ", claims.Select(_ => "rfl")) + - "⟩\n#print axioms correctness\nend ExampleProof\n"); + File.WriteAllText(proof, $$""" + import Extracted + import CIL.Safety.Execution + namespace ExampleProof + {{nullEquations}} + example : Extracted.method{{method}}.code[{{pc}}]? = some (.throw .{{label}}) := + Extracted.method{{method}}_instruction{{pc}} + theorem correctness : + {{string.Join(" \u2227\n ", claims)}} := by + repeat' first | apply And.intro | rfl + #print axioms correctness + end ExampleProof + """); queue.Add(new(selector, proof, Path.Combine(root, name + "-Verified")), () => Console.WriteLine($"PASS {name}: {claims.Count} runtime comparisons, {label} located at method {method} pc {pc}")); // A forged type must fail the feature certificate, independently of the instruction lemma. - var forgedLabel = label == "System.InvalidOperationException" - ? "System.DivideByZeroException" : "System.InvalidOperationException"; - var forged = source.Replace($".throw \"{label}\"", $".throw \"{forgedLabel}\""); + var forgedLabel = label == "invalidOperation" ? "divideByZero" : "invalidOperation"; + var forged = ForgeDecodedInstruction(source, method, pc, $"fun _ => .throw .{forgedLabel}"); var mutated = Path.Combine(root, name + "-forged.lean"); File.WriteAllText(mutated, forged); rejected.Add(mutated, $"PASS {name}: forged raised type rejected by the feature certificate", - $"Extraction.FeatureFlowValid rawLayouts.toArray (Extraction.evidenceConstants methodEvidence) " + - $"method{method}Closed", "is false"); + "Extraction.FeatureFlowValid", FeatureObligation(method), "is false"); if (name is "Null" or "Inner" or "ArgumentInner" or "ActualValue") { + var nullPc = body.IndexOf(body.First(site => site.OpCode.Code == Code.Ldnull)); + var erased = Path.Combine(root, name + "-erased-null.lean"); + File.WriteAllText(erased, ForgeDecodedInstruction(source, method, nullPc, "fun _ => .nop")); + rejected.Add(erased, $"PASS {name}: erasing the null load rejected", + "Extraction.FeatureFlowValid", FeatureObligation(method), "is false"); var nonNull = Path.Combine(root, name + "-non-null.lean"); const string nullLoad = "opcode := 20, operand := .none"; if (!source.Contains(nullLoad, StringComparison.Ordinal)) throw new InvalidOperationException("Missing null constructor argument"); - File.WriteAllText(nonNull, source.Replace(nullLoad, - "opcode := 114, operand := .text \"not null\"", StringComparison.Ordinal)); + var changed = """ + map (fun raw => if raw.opcode == 20 then + { raw with opcode := 114, operand := .text "not null" } else raw) + """.ReplaceLineEndings(" "); + File.WriteAllText(nonNull, ForgeClosedCode(source, method, changed)); rejected.Add(nonNull, $"PASS {name}: non-null constructor operand rejected", - $"Extraction.FeatureFlowValid rawLayouts.toArray (Extraction.evidenceConstants methodEvidence) " + - $"method{method}Closed", "is false"); + "Extraction.FeatureFlowValid", FeatureObligation(method), "is false"); } } @@ -116,6 +177,7 @@ ("dead-non-exception", ""), ("missing-constructor", "Unsupported exception constructor"), ("wrong-parameter", "Unsupported exception constructor"), + ("instruction-only", "Unsupported exception constructor"), ("explicit-this", "Unsupported exception constructor"), ("vararg", "Unsupported exception constructor") }) @@ -161,10 +223,16 @@ case "dead-non-exception": case "missing-constructor": case "wrong-parameter": + case "instruction-only": { var original = (MethodReference)code[0].Operand; - var type = new TypeReference("System", mutation.EndsWith("non-exception", StringComparison.Ordinal) ? "Object" : "DivideByZeroException", - module, original.DeclaringType.Scope); + var typeName = mutation switch + { + "non-exception" or "dead-non-exception" => "Object", + "instruction-only" => "InvalidCastException", + _ => "DivideByZeroException" + }; + var type = new TypeReference("System", typeName, module, original.DeclaringType.Scope); var constructor = new MethodReference(".ctor", module.TypeSystem.Void, type) { HasThis = true }; code[0].Operand = constructor; if (mutation is "missing-constructor" or "wrong-parameter") diff --git a/Tests/TransitiveImports.cs b/Tests/TransitiveImports.cs index 25999f325..c6ee9dde3 100644 --- a/Tests/TransitiveImports.cs +++ b/Tests/TransitiveImports.cs @@ -3,7 +3,13 @@ #:include ConsumerTraceDirectory.cs #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ImportReviewRegressions.cs +#:include PinnedMetadata.cs #:include StagedBundleMutation.cs using System.Numerics; @@ -12,6 +18,14 @@ using System.Text.Json; using System.Text.Json.Nodes; +if (args is ["--imports-only", var sidecar, var implementation]) +{ + ImportReviewRegressions.ImportResolution_WhenTwoVersionsShareSimpleName_RejectsAmbiguity( + ExtractorPath, sidecar, implementation, At("artifacts/ImportAmbiguity-" + Guid.NewGuid().ToString("N"))); + Console.WriteLine("PASS identical imports deduplicate and different versions reject ambiguous simple names"); + return; +} + if (args is ["--staged-bundle-mutation", var fixture]) { CheckStagedBundleMutation(fixture); @@ -29,7 +43,7 @@ string Project(string name, params string[] references) string directory = Path.Combine(root, name); Directory.CreateDirectory(directory); File.WriteAllText(Path.Combine(directory, name + ".csproj"), - "net10.0" + + "net11.0" + "ImportFixture." + name + "true" + string.Join("", references.Select(reference => "")) + ""); diff --git a/Tests/TransitiveImports/Conditional.lean b/Tests/TransitiveImports/Conditional.lean index 47dec7f3f..9eff75ae2 100644 --- a/Tests/TransitiveImports/Conditional.lean +++ b/Tests/TransitiveImports/Conditional.lean @@ -7,7 +7,7 @@ open CIL.Safety theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => ImportFixture.addOne.Holds linked 0) := by intro linked links imported input memory formed - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : ImportFixture.addOne.Holds linked 1 := imported dsimp only [ImportFixture.addOne, MethodContract.Returns] apply WP.enter found (by rfl) (by rfl) diff --git a/Tests/TransitiveOutcomes.cs b/Tests/TransitiveOutcomes.cs index f59c5b87d..42428b486 100644 --- a/Tests/TransitiveOutcomes.cs +++ b/Tests/TransitiveOutcomes.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; using System.Text.Json.Nodes; @@ -15,7 +20,7 @@ string Project(string name, string? reference, string body) var directory = Path.Combine(root, name); Directory.CreateDirectory(directory); File.WriteAllText(Path.Combine(directory, name + ".csproj"), - "net10.0" + + "net11.0" + "OutcomeFixture." + name + "true" + (reference is null ? "" : "") + ""); @@ -66,7 +71,7 @@ open CIL.Safety theorem correctness : HoldsGiven Extracted.program Extracted.ImportsHold (fun linked => DerivedInterface.percentBounded.Holds linked 0) := by intro linked links imported value memory formed - have found : linked[0]? = some Extracted.method0 := links.found Extracted.lookup0 + have found : linked[0]? = some Extracted.method0 := links.found Extracted.program_method0 have holds : DerivedInterface.percentBounded.Holds linked 1 := imported dsimp only [DerivedInterface.percentBounded] cases raised : DerivedInterface.percentRaises value with diff --git a/Tests/TranslationAudit.cs b/Tests/TranslationAudit.cs index 92fe677a0..8a3602e13 100644 --- a/Tests/TranslationAudit.cs +++ b/Tests/TranslationAudit.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include CheckedExtractionPrefix.cs #:include RejectedEvidence.cs @@ -15,7 +20,7 @@ RequireLeanFixtures("CIL.Extraction.Translation", "CIL.SymbolicExecution", "CIL.Safety.Execution"); Run(Extract("extract", At("Examples/Assembly/bin/Examples.dll"), "Examples", "Arithmetic::Add(ulong,ulong)", output)); var baseline = File.ReadAllText(Path.Combine(output, "Extracted.lean")); -var declaration = new Regex(@"theorem translation_valid\s*:[\s\S]*?(?=@\[cil_code\] theorem lookup0)"); +var declaration = TranslationPatterns.Certificate; if (declaration.Matches(baseline).Count != 1) throw new InvalidOperationException("Expected exactly one complete extraction certificate"); var prefix = new CheckedExtractionPrefix(baseline, "theorem translation_valid :", output); @@ -35,15 +40,19 @@ void Audit(string name, string source, bool accepted, bool emitted = true) Audit("valid", baseline, true); // Definitionally equal, so elaboration accepts it, but not the statement the extractor emits. -Audit("wrapped-statement", baseline.Replace("theorem translation_valid : Extraction.TranslationValid rawLayouts computedLayouts methodEvidence imports program := by", - "theorem translation_valid : id (Extraction.TranslationValid rawLayouts computedLayouts methodEvidence imports program) := by"), true, emitted: false); +Audit("wrapped-statement", baseline.Replace( + "theorem translation_valid : Extraction.TranslationValid rawLayouts computedLayouts assemblies " + + "platformDefinitions methodEvidence imports program := by", + "theorem translation_valid : id (Extraction.TranslationValid rawLayouts computedLayouts assemblies " + + "platformDefinitions methodEvidence imports program) := by"), true, emitted: false); Audit("missing-certificate", declaration.Replace(baseline, ""), false); Audit("unrelated-true", declaration.Replace(baseline, "theorem translation_valid : True := True.intro"), false); Audit("definition-instead-of-theorem", baseline.Replace("theorem translation_valid", "def translation_valid"), false); // A certificate over another import table does not certify the emitted `Extracted.imports`. Audit("other-import-table", declaration.Replace(baseline, - "theorem translation_valid : Extraction.TranslationValid rawLayouts computedLayouts methodEvidence [{ identity := \"forged\", closureDigest := \"\", parameterCount := 0, hasThis := false, returnsValue := false }] program := " + - "by decide +kernel\n"), false); + "theorem translation_valid : Extraction.TranslationValid rawLayouts computedLayouts assemblies " + + "platformDefinitions methodEvidence [{ identity := \"forged\", closureDigest := \"\", parameterCount := 0, " + + "hasThis := false, returnsValue := false }] program := by decide +kernel\n"), false); string Wrapper(int index) => ExtractionAuditSource.Replace("import Extracted\n", "", StringComparison.Ordinal) .Replace("Extracted.translation_valid", $"AuditCase{index}.Extracted.translation_valid", StringComparison.Ordinal) @@ -54,7 +63,7 @@ string Wrapper(int index) => ExtractionAuditSource.Replace("import Extracted\n", for (var index = 0; index < cases.Count; index++) { batch.AppendLine($"namespace AuditCase{index}\nopen _root_.Extracted"); - batch.AppendLine(Regex.Replace(cases[index].Source, @"(?m)^import [^\r\n]+\r?\n", "")); + batch.AppendLine(TranslationPatterns.ImportLine.Replace(cases[index].Source, "")); batch.AppendLine($"end AuditCase{index}"); requests.Add(ExtractionCertificate($"AuditCase{index}.Extracted.translation_valid")); if (cases[index].Accepted) batch.AppendLine(Wrapper(index)); @@ -94,3 +103,13 @@ string Wrapper(int index) => ExtractionAuditSource.Replace("import Extracted\n", diagnostic.Contains("Type mismatch", StringComparison.Ordinal)); } rejected.Check(prefix.LeanPath); + +/// The certificate and imports of an extracted Lean module. +static partial class TranslationPatterns +{ + [GeneratedRegex(@"theorem translation_valid\s*:[\s\S]*?(?=\r?\ndef programLength)")] + internal static partial Regex Certificate { get; } + + [GeneratedRegex(@"(?m)^import [^\r\n]+\r?\n")] + internal static partial Regex ImportLine { get; } +} diff --git a/Tests/TranslationScaling.cs b/Tests/TranslationScaling.cs index bf2c56ffe..5d7b0193b 100644 --- a/Tests/TranslationScaling.cs +++ b/Tests/TranslationScaling.cs @@ -1,17 +1,28 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include GeneratedEvidence.cs using System.Text.Json; -// Real compiled C# call graph: each method fits the ordinary budget, while the -// former monolithic translation proof repeats all six validators and times out. +const double AllowedKernelGrowth = 1.2; +CheckDeclaredSignatureScaling(); +if (args.Contains("--declared-only", StringComparer.Ordinal)) return; + +BuildLeanTestLibraries("Generic owner index scaling library", "Tests.GenericOwnerScaling"); + +// Each method fits the default budget. Elaborator reduction of the entire six-method certificate fails. var directory = At("artifacts/TranslationScaling"); var source = Path.Combine(directory, "Assembly"); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Scaling.csproj"), """ -net10.0true +net11.0 +true """); var methods = Enumerable.Range(0, 6).Select(index => $"public static ulong M{index}(ulong x) {{ " + @@ -26,39 +37,142 @@ if (!extracted.Contains("method5_evidence_validated") || extracted.Contains("set_option maxHeartbeats")) throw new InvalidOperationException("Expected six independently checked methods at the default heartbeat budget."); var start = extracted.IndexOf("theorem translation_valid :", StringComparison.Ordinal); -var end = extracted.IndexOf("@[cil_code] theorem lookup0", start, StringComparison.Ordinal); +var end = extracted.IndexOf("def programLength", start, StringComparison.Ordinal); var monolithicSource = extracted[..start] + - "theorem translation_valid : Extraction.TranslationValid rawLayouts computedLayouts " + - "methodEvidence imports program := by decide\n" + extracted[end..]; -void RequireHeartbeatRejection(int code, string diagnostic) + "theorem translation_valid : Extraction.TranslationValid rawLayouts computedLayouts assemblies " + + "platformDefinitions methodEvidence imports program := by decide\n" + extracted[end..]; +void RequireElaboratorRefusal(string path, int code, string diagnostic) { + var theoremLine = File.ReadLines(path).TakeWhile(line => + !line.StartsWith("theorem translation_valid :", StringComparison.Ordinal)).Count() + 1; var errors = diagnostic.Split('\n').Where(line => line.Contains(": error:", StringComparison.Ordinal)).ToArray(); + const string timeout = "(deterministic) timeout at `whnf`, maximum number of heartbeats (200000)"; if (code == 0 || errors.Length != 1 || - !errors[0].Contains("maximum number of heartbeats", StringComparison.Ordinal)) + !errors[0].Contains($"{Path.GetFileName(path)}:{theoremLine}:", StringComparison.Ordinal) || + !(errors[0].Contains("Tactic `decide` failed to reduce", StringComparison.Ordinal) || + errors[0].Contains(timeout, StringComparison.Ordinal))) throw new InvalidOperationException( - "Fixture no longer demonstrates the original default-budget failure: " + diagnostic); + "Fixture no longer demonstrates the elaborator refusing the monolithic certificate: " + diagnostic); } if (args.Contains("--measure", StringComparer.Ordinal)) { var trace = Path.Combine(directory, "Trace-" + Guid.NewGuid().ToString("N")); Environment.SetEnvironmentVariable("LEANCIL_TRACE_DIRECTORY", trace); Console.WriteLine("Fixed-workload prefix measurements: " + trace); - Run(["lake", "env", "lean", Path.Combine(output, "Extracted.lean")], cwd: FixturePackage(), + Run(["lake", "env", "lean", "-DElab.async=false", Path.Combine(output, "Extracted.lean")], cwd: FixturePackage(), progress: "Baseline complete compositional extraction"); var original = Path.Combine(directory, "FullMonolithic.lean"); File.WriteAllText(original, monolithicSource); - var result = Execute(["lake", "env", "lean", original], cwd: FixturePackage(), progress: "Baseline complete monolithic rejection"); - RequireHeartbeatRejection(result.Code, result.Output); + var result = Execute(["lake", "env", "lean", "-DElab.async=false", original], cwd: FixturePackage(), + progress: "Baseline complete monolithic refusal"); + RequireElaboratorRefusal(original, result.Code, result.Output); } var old = Path.Combine(directory, "Monolithic.lean"); // The monolithic decision uses the raw definitions, not the already-proved component lemmas. // The production consumer below checks the complete unchanged positive certificate once. File.WriteAllText(old, NarrowEvidence(monolithicSource, "translation_valid")); -var monolithic = Execute(["lake", "env", "lean", old], cwd: FixturePackage(), - progress: "Checking monolithic certificate rejection"); -RequireHeartbeatRejection(monolithic.Code, monolithic.Output); -Console.WriteLine("PASS six-method compiled fixture exceeds the monolithic default budget"); +var monolithic = Execute(["lake", "env", "lean", "-DElab.async=false", old], cwd: FixturePackage(), + progress: "Checking monolithic certificate refusal"); +RequireElaboratorRefusal(old, monolithic.Code, monolithic.Output); +Console.WriteLine("PASS six-method compiled fixture: the elaborator refuses the monolithic certificate"); + +// The kernel decides the monolithic certificate. Its heartbeats and peak memory per added method of the call graph +// may not grow with the number of methods: superlinear growth would make the check unusable on large extractions +// even though only the kernel may run it. Measured: 0.96 in heartbeats, 0.92 in memory. +(long Heartbeats, double Gigabytes) KernelCost(int methodCount) +{ + var work = Path.Combine(directory, "Kernel" + methodCount); + var kernelSource = Path.Combine(work, "Assembly"); + Directory.CreateDirectory(kernelSource); + File.WriteAllText(Path.Combine(kernelSource, "Scaling.csproj"), File.ReadAllText(Path.Combine(source, + "Scaling.csproj"))); + var kernelMethods = Enumerable.Range(0, methodCount).Select(index => + $"public static ulong M{index}(ulong x) {{ " + + string.Join(" ", Enumerable.Range(1, 48).Select(value => $"x = (x + {value}UL) ^ {value + index}UL;")) + + (index == 0 ? " return " + string.Join(" + ", Enumerable.Range(1, + methodCount - 1).Select(call => $"M{call}(x)")) + "; }" + : " return x; }")); + File.WriteAllText(Path.Combine(kernelSource, "Scaling.cs"), "public static class Scaling { " + string.Join('\n', + kernelMethods) + " }"); + Run(["dotnet", "build", Path.Combine(kernelSource, "Scaling.csproj"), "-c", "Release"]); + var extraction = Path.Combine(work, "Extraction"); + Run(Extract("extract", Path.Combine(kernelSource, "bin/Scaling.dll"), "Scaling", "Scaling::M0(ulong)", + extraction)); + var full = File.ReadAllText(Path.Combine(extraction, "Extracted.lean")); + var from = full.IndexOf("theorem translation_valid :", StringComparison.Ordinal); + var to = full.IndexOf("def programLength", from, StringComparison.Ordinal); + var kernelFile = Path.Combine(work, "Monolithic.lean"); + File.WriteAllText(kernelFile, NarrowEvidence(full[..from] + + "theorem translation_valid : Extraction.TranslationValid rawLayouts computedLayouts assemblies " + + "platformDefinitions methodEvidence imports program := by decide +kernel\n" + full[to..], + "translation_valid") + + "\n#eval show IO Nat from IO.getNumHeartbeats\n"); + var (peak, output) = MeasurePeakGigabytes(["lake", "env", "lean", "-DElab.async=false", kernelFile], + FixturePackage()); + var count = HeartbeatCount.Match(output); + if (!count.Success) throw new InvalidOperationException("Missing heartbeat count:\n" + output); + return (long.Parse(count.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture), peak); +} +int[] workloads = [2, 4, 8]; +var costs = workloads.Select(count => (Count: count, Cost: KernelCost(count))).ToArray(); +foreach (var (count, cost) in costs) + Console.WriteLine(FormattableString.Invariant( + $"kernel decide of the {count}-method certificate: {cost.Heartbeats} heartbeats, ") + + FormattableString.Invariant($"peak {cost.Gigabytes:F2} GB")); +// The cost of a method added from the middle to the largest workload against one added from the smallest to the +// middle; the imports and the fixed part of the certificate cancel. An increment below `floor` counts as `floor`. +double IncrementRatio(Func<(long Heartbeats, double Gigabytes), double> measure, double floor) +{ + double Increment(int from) => Math.Max(measure(costs[from + 1].Cost) - measure(costs[from].Cost), floor) / + (costs[from + 1].Count - costs[from].Count); + return Increment(1) / Increment(0); +} +var beatGrowth = IncrementRatio(cost => cost.Heartbeats, 1); +// Peak memory moves by tens of megabytes between runs of one file. +var memoryGrowth = IncrementRatio(cost => cost.Gigabytes, 0.05); +if (beatGrowth > AllowedKernelGrowth || memoryGrowth > AllowedKernelGrowth) + throw new InvalidOperationException(string.Create(System.Globalization.CultureInfo.InvariantCulture, + $"A method added to {workloads[1]} methods costs {beatGrowth:F2} times the heartbeats and " + + $"{memoryGrowth:F2} times the memory of one added to {workloads[0]}; the limit is {AllowedKernelGrowth}.")); +Console.WriteLine(FormattableString.Invariant( + $"PASS the kernel check per added method grows {beatGrowth:F2} times in heartbeats and {memoryGrowth:F2} ") + + FormattableString.Invariant( + $"times in memory from {workloads[0]}-{workloads[1]} to {workloads[1]}-{workloads[2]} methods")); +// Peak working set of `lean` on a file in gigabytes, and its output. Lean runs directly with the options between +// `lean` and the file, in the environment `lake env` prints, so the measured process holds the memory everywhere. +static (double Gigabytes, string Output) MeasurePeakGigabytes(string[] command, string cwd, + string? sourceText = null) +{ + var path = command[^1]; + if (sourceText is not null) File.WriteAllText(path, sourceText); + var environment = Run(["lake", "env"], cwd: cwd).ReplaceLineEndings("\n").Split('\n') + .Select(line => line.Split('=', 2)).Where(pair => pair.Length == 2) + .ToDictionary(pair => pair[0], pair => pair[1]); + var lean = Path.Combine(environment["LEAN_SYSROOT"], "bin", OperatingSystem.IsWindows() ? "lean.exe" : "lean"); + var info = new System.Diagnostics.ProcessStartInfo(lean) { WorkingDirectory = cwd, UseShellExecute = false, + RedirectStandardOutput = true, RedirectStandardError = true }; + foreach (var (name, value) in environment) info.Environment[name] = value; + foreach (var argument in command[3..]) info.ArgumentList.Add(argument); + using var process = System.Diagnostics.Process.Start(info)!; + var output = process.StandardOutput.ReadToEndAsync(); + var errors = process.StandardError.ReadToEndAsync(); + long peak = 0; + while (!process.WaitForExit(200)) + { + // Lean can exit between the wait and the read; the peak is then the last one read. + try + { + process.Refresh(); + peak = Math.Max(peak, process.PeakWorkingSet64); + } + catch (InvalidOperationException) when (process.HasExited) { } + } + if (process.ExitCode != 0 || output.Result.Contains("sorryAx", StringComparison.Ordinal)) + throw new InvalidOperationException( + $"{lean} {path} failed ({process.ExitCode}):\n{output.Result}\n{errors.Result}"); + return (peak / (1024.0 * 1024 * 1024), output.Result); +} var consumer = Path.Combine(directory, "Consumer"); Directory.CreateDirectory(consumer); var revision = Run(["git", "rev-parse", "HEAD"]).Trim(); @@ -70,7 +184,9 @@ void RequireHeartbeatRejection(int code, string diagnostic) import Extracted namespace ExampleProof theorem correctness : CIL.Extraction.TranslationValid Extracted.rawLayouts Extracted.computedLayouts - Extracted.methodEvidence Extracted.imports Extracted.program := Extracted.translation_valid + Extracted.assemblies Extracted.platformDefinitions Extracted.methodEvidence Extracted.imports + Extracted.program := + Extracted.translation_valid #print axioms correctness end ExampleProof """); @@ -81,3 +197,165 @@ end ExampleProof !report.GetProperty("theoremAudit").GetString()!.Contains("ExampleProof.correctness")) throw new InvalidOperationException("Missing independent certificate/theorem audits."); Console.WriteLine("PASS fresh consumer proof-root verifies the multi-method certificate with kernel replay and restricted axioms"); +CheckHeartbeatBudget(Path.Combine(output, "Extracted.lean")); +CheckLayoutTableScaling(extracted); + +// The extraction checks within the default heartbeat budget with the kernel counted on the elaborating thread, and +// the budget binds: the same file fails at 100 heartbeats with a deterministic timeout. +void CheckHeartbeatBudget(string extraction) +{ + Run(["lake", "env", "lean", "-DElab.async=false", extraction], cwd: FixturePackage(), + progress: "Checking the extraction at the default heartbeat budget"); + var starved = Execute(["lake", "env", "lean", "-DElab.async=false", "-DmaxHeartbeats=100", extraction], + cwd: FixturePackage(), progress: "Checking the extraction at 100 heartbeats"); + if (starved.Code == 0 || !starved.Output.Contains("deterministic", StringComparison.Ordinal) || + !starved.Output.Contains("timeout", StringComparison.Ordinal)) + throw new InvalidOperationException("The extraction does not time out at 100 heartbeats: " + starved.Output); + Console.WriteLine("PASS the extraction checks at the default heartbeat budget and times out at 100"); +} + +// The emitted header and method layout theorems over chains of nested and of generic structs: kernel heartbeats +// per layout may grow by at most 1.5 times from 64 to 512 layouts, and 512 layouts must check within 8 GiB. +void CheckLayoutTableScaling(string extraction) +{ + const double allowedGrowth = 1.5; + const int small = 64, large = 512; + const string report = "#eval show IO Nat from IO.getNumHeartbeats"; + const string axioms = "depends on axioms: [propext, Classical.choice, Quot.sound]"; + string Emitted(string name) => extraction.Split('\n').Select(line => line.TrimEnd('\r')) + .Single(line => line.StartsWith($"theorem {name} :", StringComparison.Ordinal)); + // Each row is a namespace holding one table and the emitted theorem it measures. + (string Name, string Namespace, string Theorem)[] rows = + [ + ("the header check of nested structs", "Nested", Emitted("layouts_validated")), + ("the header check of generic structs", "Generic", Emitted("layouts_validated")), + ("a method's check of nested structs", "Nested", Emitted("method0_layouts_validated")) + ]; + string Table(string space, int count, Func raw, Func layout) => $$""" + namespace {{space}} + def rawLayouts : List Extraction.RawLayout := [ + {{string.Join(",\n", Enumerable.Range(0, count).Select(raw))}}] + {{string.Join("\n", Enumerable.Range(0, count).Select(index => $"def layout{index} : AggregateLayout := " + + layout(index)))}} + def computedLayouts : Array Extraction.ComputedLayout := + #[{{string.Join(", ", Enumerable.Range(0, count).Select(index => $"⟨layout{index}, 8⟩"))}}] + def method0 : Method := { code := [.ret], locals := [], returnsValue := false } + def method0LayoutUses : List Extraction.LayoutUse := [] + end {{space}} + """; + double[] PerLayout(int count) + { + // T0 holds a ulong and T(k) holds T(k - 1) and then a byte, so each layout reads the one before it. + var nested = Table("Nested", count, index => + $" {{ identity := \"T{index}\", explicit := false, packing := 0, declaredSize := 0, fields := [" + + (index == 0 ? "⟨.unsigned64, none⟩" : $"⟨.aggregate {index - 1}, none⟩, ⟨.unsigned8, none⟩") + "] }", + index => index == 0 ? "⟨8, [⟨0, 8, false⟩]⟩" + : $"⟨{8 * (index + 1)}, [⟨0, {8 * index}, true⟩, ⟨{8 * index}, 1, false⟩]⟩"); + // Wrap(k) holds Wrap(k - 1), so each layout looks up the previous distinct generic owner. + var generic = Table("Generic", count, index => index == 0 + ? " { identity := \"Leaf\", explicit := false, packing := 0, declaredSize := 0, " + + "fields := [⟨.unsigned64, none⟩] }" + : $" {{ identity := \"S::Wrap{index}`1\", generic := some {{ owner := .constructor \"S\" " + + $"\"Wrap{index}`1\", arity := 1, arguments := [.aggregate \"Leaf\" 0], fields := [" + + (index == 1 ? ".parameter 0" : $".apply (.constructor \"S\" \"Wrap{index - 1}`1\") (.parameter 0)") + + $"], constraints := [(0, [])] }}, explicit := false, packing := 0, declaredSize := 0, " + + $"fields := [⟨.aggregate {index - 1}, none⟩] }}", + index => $"⟨8, [⟨0, 8, {(index > 0 ? "true" : "false")}⟩]⟩"); + var measured = string.Join("\n", rows.Select(row => $"namespace {row.Namespace}\n{row.Theorem}\n" + + $"end {row.Namespace}\n{report}")); + var audits = string.Join("\n", rows.Select(row => + $"#print axioms {row.Namespace}.{row.Theorem.Split(' ')[1]}")); + var path = Path.Combine(directory, $"Layouts{count}.lean"); + File.WriteAllText(path, $""" + import CIL.Extraction.LayoutUses + open CIL + {nested} + {generic} + {report} + {measured} + {audits} + """ + "\n"); + // Without the flag the kernel checks on another thread and its heartbeats go uncounted. + var output = Run(["lake", "env", "lean", "-DElab.async=false", "-M", "8192", path], cwd: FixturePackage()); + var counts = output.Split('\n').Select(line => line.Trim()) + .Where(line => line.Length > 0 && line.All(char.IsAsciiDigit)) + .Select(line => long.Parse(line, System.Globalization.CultureInfo.InvariantCulture)).ToArray(); + if (counts.Length != rows.Length + 1 || output.Contains("sorryAx", StringComparison.Ordinal) || + rows.Any(row => !output.Contains($"'{row.Namespace}.{row.Theorem.Split(' ')[1]}' {axioms}", + StringComparison.Ordinal))) + throw new InvalidOperationException($"Unchecked layout tables of {count} layouts:\n{output}"); + return [.. rows.Select((_, row) => (double)(counts[row + 1] - counts[row]) / count)]; + } + var smallCosts = PerLayout(small); + var largeCosts = PerLayout(large); + for (var row = 0; row < rows.Length; row++) + { + var growth = largeCosts[row] / smallCosts[row]; + var measured = FormattableString.Invariant($"{smallCosts[row]:F0} heartbeats per layout at {small}") + + FormattableString.Invariant($" layouts and {largeCosts[row]:F0} at {large}, {growth:F2} times as many"); + if (growth > allowedGrowth) + throw new InvalidOperationException(FormattableString.Invariant( + $"{rows[row].Name} costs {measured}; the limit is {allowedGrowth}.")); + Console.WriteLine($"PASS {rows[row].Name} costs {measured}"); + } + Console.WriteLine($"PASS {large} nested and {large} generic layouts check within 8 GiB"); +} + +// Only the emitted declaration is measured; fixture construction and other validators are outside the count. +void CheckDeclaredSignatureScaling() +{ + int[] parameterCounts = [1, 4, 16]; + var work = At("artifacts/TranslationScaling/Declared"); + var assemblySource = Path.Combine(work, "Assembly"); + Directory.CreateDirectory(assemblySource); + File.WriteAllText(Path.Combine(assemblySource, "Declared.csproj"), """ + net11.0 + true + """); + var declarations = parameterCounts.Select(count => + $"public static ulong M{count}(" + string.Join(", ", Enumerable.Range(0, count) + .Select(index => $"ulong p{index}")) + ") => p0;"); + File.WriteAllText(Path.Combine(assemblySource, "Declared.cs"), + "public static class Declared { " + string.Join('\n', declarations) + " }"); + Run(["dotnet", "build", Path.Combine(assemblySource, "Declared.csproj"), "-c", "Release"]); + var costs = new List(); + foreach (var count in parameterCounts) + { + var extraction = Path.Combine(work, "Parameters" + count); + var entry = $"Declared::M{count}(" + string.Join(',', Enumerable.Repeat("ulong", count)) + ")"; + Run(Extract("extract", Path.Combine(assemblySource, "bin/Declared.dll"), "Declared", entry, extraction)); + const string theorem = "method0_declared_validated"; + var narrowed = NarrowEvidence(File.ReadAllText(Path.Combine(extraction, "Extracted.lean")), theorem); + var declaration = narrowed.Split('\n').Single(line => line.StartsWith($"theorem {theorem} :", + StringComparison.Ordinal)); + var measuredDeclaration = $$""" + open Lean Elab Command + /-- Elaboration and kernel checking of one declared signature. -/ + elab "#declared_cost " command:command : command => do + let before <- IO.getNumHeartbeats + elabCommand command + let after <- IO.getNumHeartbeats + liftIO <| IO.println (after - before) + #declared_cost {{declaration}} + """; + var measured = narrowed.Replace(declaration, measuredDeclaration, + StringComparison.Ordinal) + $"\n#print axioms Extracted.{theorem}\n"; + var path = Path.Combine(extraction, "DeclaredCost.lean"); + File.WriteAllText(path, measured); + var output = Run(["lake", "env", "lean", "-DElab.async=false", path], cwd: FixturePackage()); + var counts = HeartbeatCount.Matches(output).Select(match => + long.Parse(match.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture)).ToArray(); + if (counts.Length != 1 || counts[0] <= 0 || + !output.Contains($"'Extracted.{theorem}' does not depend on any axioms", StringComparison.Ordinal)) + throw new InvalidOperationException($"Unchecked declared signature with {count} parameters:\n{output}"); + var cost = counts[0]; + costs.Add(cost); + Console.WriteLine($"Declared signature with {count} parameters: {cost} heartbeats per method"); + } + var growth = (double)costs.Max() / costs[0]; + if (growth > AllowedKernelGrowth) + throw new InvalidOperationException(FormattableString.Invariant( + $"Declared signature cost per method grows {growth:F2} times; the limit is {AllowedKernelGrowth}.")); + Console.WriteLine(FormattableString.Invariant( + $"PASS declared signature cost per method grows {growth:F2} times over 1, 4 and 16 parameters")); +} diff --git a/Tests/TupleDivision.cs b/Tests/TupleDivision.cs index 996b38f0b..4f4e7ed44 100644 --- a/Tests/TupleDivision.cs +++ b/Tests/TupleDivision.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -69,7 +74,41 @@ string Build(int start, int count) } return Build(0, entries.Count); } -var requests = new List(); +// A failed attempt to prove the quotient contract also needs a checked counterexample for the compiled method. +GeneratedVerification WrongQuotientRefutation(string method, string selector, int bits, int[] inputs, string expected) +{ + var output = Path.Combine(root, method + "Refutation"); + Directory.CreateDirectory(output); + var path = Path.Combine(output, "Refutation.lean"); + var stack = string.Join(", ", inputs.Select(value => $".scalar (.i{bits} {value})")); + File.WriteAllText(path, $$""" + import Extracted + import Tests.Support + namespace ExampleProof + open CIL CIL.Safety + private def arguments : List CIL.Safety.Value := [{{stack}}] + /-- Seven divided by three returns the remainder one, contradicting the quotient postcondition. -/ + theorem correctness : ¬ ∃ q : W{{bits}}, + (invoke Extracted.program 8 0 arguments Memory.empty).map Prod.snd = .ok [.scalar (.i{{bits}} q)] ∧ + {{expected}} := by + have ran : (invoke Extracted.program 8 0 arguments Memory.empty).map Prod.snd = + .ok [.scalar (.i{{bits}} 1)] := by cil_kernel_rfl + rintro ⟨q, returned, quotient⟩ + rw [ran] at returned + simp at returned + subst q + revert quotient + decide + #print axioms correctness + end ExampleProof + """); + return new GeneratedVerification(selector, path, output); +} + +var requests = new List +{ + WrongQuotientRefutation("WrongQuotient", Selector("WrongQuotient"), 64, [7, 0, 3], "q.toNat = 7 / 3") +}; var cases = 0; foreach (var (name, result) in methods) { @@ -107,10 +146,7 @@ string Build(int start, int count) var output = Path.Combine(root, name); Directory.CreateDirectory(output); var proof = Path.Combine(output, "BoundaryProof.lean"); - File.WriteAllText(proof, "import Extracted\nimport Tests.Support\nnamespace ExampleProof\n" + - string.Join("", claims.Select((claim, index) => $"theorem case{index} : {claim} := by rfl\n")) + - "theorem correctness :\n" + Balanced(claims, false) + " :=\n" + - Balanced(Enumerable.Range(0, claims.Count).Select(index => "case" + index).ToArray(), true) + "\nend ExampleProof\n"); + File.WriteAllText(proof, BoundaryProof(claims)); requests.Add(new GeneratedVerification(Selector(name), proof, output)); cases += claims.Count; } @@ -242,14 +278,26 @@ static byte[] RuntimeBytes(T value) where T : struct => [typeof(nint)] = new object[] { nint.MinValue, (nint)(-7), (nint)(-1), (nint)0, (nint)1, (nint)3, nint.MaxValue }, [typeof(nuint)] = new object[] { (nuint)0, (nuint)1, (nuint)3, (nuint)(1UL << 63), nuint.MaxValue } }; -string BoundaryProof(IReadOnlyList claims) => - "import Extracted\nimport Tests.Support\nnamespace ExampleProof\n" + - "def memory : CIL.Safety.Memory := { Tests.caller with cells := fun _ _ => ⟨93, false⟩ }\n" + - string.Join("", claims.Select((claim, index) => $"theorem case{index} : {claim} := by rfl\n")) + - "theorem correctness :\n" + Balanced(claims, false) + " :=\n" + - Balanced(Enumerable.Range(0, claims.Count).Select(index => "case" + index).ToArray(), true) + - "\nend ExampleProof\n"; -var mathRequests = new List(); +string BoundaryProof(IReadOnlyList claims) => $$""" + import Extracted + import Tests.Support + namespace ExampleProof + def memory : CIL.Safety.Memory := { Tests.caller with cells := fun _ _ => .mk 93 false } + {{string.Join("\n", claims.Select((claim, index) => $""" + theorem case{index} : {claim} := by cil_kernel_rfl + #print axioms case{index} + """))}} + theorem correctness : + {{Balanced(claims, false)}} := + {{Balanced(Enumerable.Range(0, claims.Count).Select(index => $"case{index}").ToArray(), true)}} + #print axioms correctness + end ExampleProof + """; +var mathRequests = new List +{ + WrongQuotientRefutation("MathWrongQuotient", "TupleDivision::MathWrongQuotient(int,int)", 32, + [7, 3], "q.toInt = (7 : Int).tdiv 3") +}; var mathCases = 0; var faultKinds = new SortedSet(StringComparer.Ordinal); void AddBoundary(string name, string selector, List claims) diff --git a/Tests/TypeTestEvidence.lean b/Tests/TypeTestEvidence.lean index 94777f82e..dd90b22bf 100644 --- a/Tests/TypeTestEvidence.lean +++ b/Tests/TypeTestEvidence.lean @@ -1,4 +1,7 @@ +import CIL.Safety.Execution import CIL.Extraction.Scalars +import CIL.Extraction.DecodedMethod +import Tests.Support.RawDecoding set_option maxRecDepth 4096 @@ -15,16 +18,27 @@ private def word : TypeExpr := .atom runtime "System.UInt64" private def marker : TypeExpr := .aggregate (owner ++ "::Fixture.Marker") 0 private def other : TypeExpr := .aggregate (owner ++ "::Fixture.Other") 1 -private def typeCall (name signature : String) (parameters : Nat) : RawOperand := .call { - declaringType := .atom runtime "System.Type", name, - target := .external (runtime ++ "::" ++ signature), - typeArguments := [], parameterCount := parameters, hasThis := false, returnsValue := true, - returnType := .atom runtime (if name == "GetTypeFromHandle" then "System.Type" else "System.Boolean"), - parameterTypes := if name == "GetTypeFromHandle" then [.atom runtime "System.RuntimeTypeHandle"] - else [.atom runtime "System.Type", .atom runtime "System.Type"] } -private def fromHandle := typeCall "GetTypeFromHandle" "System.Type System.Type::GetTypeFromHandle(System.RuntimeTypeHandle)" 1 -private def equality := typeCall "op_Equality" "System.Boolean System.Type::op_Equality(System.Type,System.Type)" 2 -private def inequality := typeCall "op_Inequality" "System.Boolean System.Type::op_Inequality(System.Type,System.Type)" 2 +private def typeCall (name : String) (methodName : MetadataName) (signature : String) + (parameters : Nat) : RawOperand := .call { + declared := { + convention := 0, returnType := .atom runtime + (if name == "GetTypeFromHandle" then "System.Type" else "System.Boolean"), + parameters := if name == "GetTypeFromHandle" then [.atom runtime "System.RuntimeTypeHandle"] + else [.atom runtime "System.Type", .atom runtime "System.Type"] }, + method := { + owner := .atom runtime "System.Type", name := methodName, hasThis := false, genericArity := 0, + returnType := .atom runtime + (if name == "GetTypeFromHandle" then "System.Type" else "System.Boolean"), + parameters := if name == "GetTypeFromHandle" then [.atom runtime "System.RuntimeTypeHandle"] + else [.atom runtime "System.Type", .atom runtime "System.Type"] }, + name, target := .external (runtime ++ "::" ++ signature), + typeArguments := [], parameterCount := parameters, returnsValue := true } +private def fromHandle := typeCall "GetTypeFromHandle" name!"GetTypeFromHandle" + "System.Type System.Type::GetTypeFromHandle(System.RuntimeTypeHandle)" 1 +private def equality := typeCall "op_Equality" name!"op_Equality" + "System.Boolean System.Type::op_Equality(System.Type,System.Type)" 2 +private def inequality := typeCall "op_Inequality" name!"op_Inequality" + "System.Boolean System.Type::op_Inequality(System.Type,System.Type)" 2 private def i (opcode : Nat) (operand : RawOperand := .none) : RawInstruction := ⟨0, opcode, operand⟩ private def test (left right : TypeExpr) (comparison := equality) : List RawInstruction := @@ -50,61 +64,73 @@ example : sameRuntimeType (.parameter 0) word = none := by decide example : sameRuntimeType .void .void = none := by decide example : sameRuntimeType (.aggregate "Fixture::Bad::Name" 0) (.aggregate "Fixture::Bad::Other" 0) = none := by decide -example : foldRoles [] (test marker marker) = - [some .head, some .interior, some .interior, some .interior, some (.result true)] := by decide -example : foldRoles [] (test marker other inequality) = - [some .head, some .interior, some .interior, some .interior, some (.result true)] := by decide -example : foldRoles [] (test (.atom "Forwarder" "System.UInt64") word) = List.replicate 5 none := by decide +example : foldRoles (test marker marker) = + [some .typeHead, some .typeOperand, some .typeOperand, some .typeOperand, some (.result true)] := by decide +example : foldRoles (test marker other inequality) = + [some .typeHead, some .typeOperand, some .typeOperand, some .typeOperand, some (.result true)] := by decide +example : foldRoles (test (.atom "Forwarder" "System.UInt64") word) = List.replicate 5 none := by + decide -- Any instruction between the operands breaks the exact encoding. -example : foldRoles [] ((test marker marker).insertIdx 2 (i 0)) = List.replicate 6 none := by decide +example : foldRoles ((test marker marker).insertIdx 2 (i 0)) = List.replicate 6 none := by decide -- 0: typeof(Marker) == typeof(Marker); 5: brfalse 8; 6: ldc.i4.1; 7: ret; 8: ldc.i4.0; 9: ret private def foldRaw := raw (test marker marker ++ [i 44 (.target 8), i 23, i 42, i 22, i 42]) private def foldStates : FeatureCertificate := [state [], state [none], state [none], state [none, none], state [none, none], state [some 1], state [], state [some 1], none, none] private def foldMask := [true, true, true, true, true, true, true, true, false, false] -private def foldCode : List Op := [.nop, .nop, .nop, .nop, .const32 1, .brzero 8, .const32 1, .ret none, - .unsupported "pruned", .unsupported "pruned"] +private def typeOperands (left right : TypeExpr) : List Instruction := + [.ldtoken (.type left), .call getTypeFromHandle.method, .ldtoken (.type right), + .call getTypeFromHandle.method] -example : FeatureFlowValid #[] (fun _ => none) +private def equalCall : Instruction := .call (runtimeTypeComparison true).method + +private def foldCode : List Instruction := + typeOperands marker marker ++ [equalCall, .brfalse 8, .ldc_i4 1, .ret, .unadmitted 22, + .unadmitted 42] + +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) foldRaw .scalar foldStates foldMask foldCode := by decide -- A forged constant fails even with matching facts, and forged facts cannot prune the live branch. -example : ¬ FeatureFlowValid #[] (fun _ => none) foldRaw .scalar foldStates foldMask - (foldCode.set 4 (.const32 0)) := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) foldRaw .scalar +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) foldRaw .scalar foldStates + foldMask + (foldCode.set 4 (.ldc_i4 0)) := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) foldRaw .scalar (foldStates.set 5 (state [some 0])) foldMask foldCode := by decide -example : ¬ FeatureFlowValid #[] (fun _ => none) foldRaw .scalar foldStates foldMask - (foldCode.set 2 (.const32 0)) := by decide +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) foldRaw .scalar foldStates + foldMask + (foldCode.set 2 (.ldc_i4 0)) := by decide -- Distinct types decide the opposite edge. private def distinctRaw := raw (test marker other ++ [i 44 (.target 8), i 23, i 42, i 22, i 42]) -example : ¬ FeatureFlowValid #[] (fun _ => none) +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) distinctRaw .scalar foldStates foldMask foldCode := by decide -example : FeatureFlowValid #[] (fun _ => none) distinctRaw .scalar +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) distinctRaw .scalar ((foldStates.set 5 (state [some 0])).set 6 none |>.set 7 none |>.set 8 (state []) |>.set 9 (state [some 0])) [true, true, true, true, true, true, false, false, true, true] - [.nop, .nop, .nop, .nop, .const32 0, .brzero 8, .unsupported "pruned", .unsupported "pruned", .const32 0, .ret none] := by decide + (typeOperands marker other ++ [equalCall, .brfalse 8, .unadmitted 23, .unadmitted 42, + .ldc_i4 0, .ret]) := by decide -- A branch into the operands would reach the comparison with a different emitted stack. private def enteredRaw := raw ([i 22, i 43 (.target 4)] ++ test marker marker ++ [i 42]) -example : ¬ FeatureFlowValid #[] (fun _ => none) enteredRaw .scalar +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) enteredRaw .scalar [state [], state [some 0], none, none, state [some 0], state [none, some 0], state [none, some 0], state [some 1]] [true, true, false, false, true, true, true, true] - [.const32 0, .branch 4, .unsupported "", .unsupported "", .nop, .nop, .const32 1, .ret none] := by decide + [.ldc_i4 0, .br 4, .unadmitted 208, .unadmitted 40, .ldtoken (.type marker), + .call getTypeFromHandle.method, equalCall, .ret] := by decide -- The same facts are accepted once the edge enters at the first operand. -example : FeatureFlowValid #[] (fun _ => none) +example : FeatureFlowValid #[] (fun _ => none) (fun _ => none) (raw ([i 22, i 43 (.target 2)] ++ test marker marker ++ [i 42])) .scalar [state [], state [some 0], state [some 0], state [none, some 0], state [none, some 0], state [none, none, some 0], state [none, none, some 0], state [some 1, some 0]] [true, true, true, true, true, true, true, true] - [.const32 0, .branch 2, .nop, .nop, .nop, .nop, .const32 1, .ret none] := by decide + ([.ldc_i4 0, .br 2] ++ typeOperands marker marker ++ [equalCall, .ret]) := by decide --- Undecided comparisons and stray type handles have no admitted translation. +-- Undecided type comparisons cannot certify an emitted constant. private def undecidedRaw := raw (test (.atom "Forwarder" "System.UInt64") word ++ [i 42]) -example : ¬ FeatureFlowValid #[] (fun _ => none) undecidedRaw .scalar +example : ¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) undecidedRaw .scalar [state [], state [none], state [none], state [none, none], state [none, none], state [none]] - (List.replicate 6 true) [.nop, .nop, .nop, .nop, .const32 0, .ret none] := by decide -example : featureOpMatchesAt #[] foldRaw none (i 208 (.type marker)) (.const32 0) = false := by decide -example : featureOpMatchesAt #[] foldRaw none (i 40 equality) (.eq) = false := by decide -example : featureOpMatchesAt #[] foldRaw none (i 40 fromHandle) (.convU4) = false := by decide + (List.replicate 6 true) [.nop, .nop, .nop, .nop, .ldc_i4 0, .ret] := by decide +example : featureOpMatchesAt #[] foldRaw none (i 208 (.type marker)) (.ldc_i4 0) = false := by decide +example : featureOpMatchesAt #[] foldRaw none (i 40 equality) (.ceq) = false := by decide +example : featureOpMatchesAt #[] foldRaw none (i 40 fromHandle) (.conv_u4) = false := by decide /-- The exact reference-equality branch form, with two distinct return paths. -/ private def branchRaw (same : Bool) (opcode : Nat) (target := 7) := @@ -117,19 +143,21 @@ private def branchStates (taken : Bool) : FeatureCertificate := if taken then none else state [], if taken then none else state [some 1], if taken then state [] else none, if taken then state [some 0] else none] -/-- The raw scalar certificate tracks Type objects without exposing them to the interpreter. -/ +/-- The scalar certificate tracks the two type operands consumed by the branch. -/ private def branchKinds (taken : Bool) : ScalarCertificate := [some [], some [.other], some [.other], some [.other, .other], some [.other, .other], if taken then none else some [], if taken then none else some [.word32], if taken then some [] else none, if taken then some [.word32] else none] -/-- Four erased operands and one exact transfer preserve instruction numbering. -/ -private def branchCode (taken : Bool) : List Op := - [.nop, .nop, .nop, .nop, if taken then .branch 7 else .nop, - if taken then .unsupported "pruned" else .const32 1, - if taken then .unsupported "pruned" else .ret none, - if taken then .const32 0 else .unsupported "pruned", - if taken then .ret none else .unsupported "pruned"] +/-- The executed type branch retains both handle loads, both calls and its original target. -/ +private def branchCode (same : Bool) (opcode : Nat) : List Instruction := + let equal := opcode == 46 || opcode == 59 + let taken := same == equal + typeOperands word (if same then word else intType) ++ [if equal then .beq 7 else .bne_un 7, + if taken then .unadmitted 23 else .ldc_i4 1, + if taken then .unadmitted 42 else .ret, + if taken then .ldc_i4 0 else .unadmitted 22, + if taken then .ret else .unadmitted 42] /-- Short and long equality/inequality branches certify both type-identity outcomes. -/ theorem reference_type_branches : @@ -137,9 +165,10 @@ theorem reference_type_branches : let taken := same == (opcode == 46 || opcode == 59) let body := branchRaw same opcode let states := branchStates taken - decide (FeatureFlowValid #[] (fun _ => none) body .scalar states (states.map Option.isSome) - (branchCode taken)) && - decide (ScalarFlowValid #[] body states (branchKinds taken) (branchCode taken))) = true := by + decide (FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar states + (states.map Option.isSome) + (branchCode same opcode)) && + decide (ScalarFlowValid body states (branchKinds taken) (branchCode same opcode))) = true := by decide +kernel #print axioms reference_type_branches @@ -147,19 +176,22 @@ theorem reference_type_branches : theorem reference_type_branch_rejections : let states := branchStates true let mask := states.map Option.isSome - let code := branchCode true + let code := branchCode true 46 let forwarded := raw ((branchRaw true 46).code.set 2 (i 208 (.type (.atom "Forwarder" "System.UInt64")))) - (¬ FeatureFlowValid #[] (fun _ => none) (branchRaw true 46) .scalar states mask - (code.set 4 (.branch 5))) ∧ - (¬ FeatureFlowValid #[] (fun _ => none) (branchRaw true 47) .scalar states mask code) ∧ - (¬ FeatureFlowValid #[] (fun _ => none) (branchRaw true 46) .scalar - (branchStates false) ((branchStates false).map Option.isSome) (branchCode false)) ∧ + (¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (branchRaw true 46) .scalar states + mask (code.set 4 (.beq 5))) ∧ + (¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (branchRaw true 47) .scalar states + mask code) ∧ + (¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) (branchRaw true 46) .scalar + (branchStates false) ((branchStates false).map Option.isSome) (branchCode false 46)) ∧ ([1, 4, 9].all fun target => let untouched := branchStates false - !decide (FeatureFlowValid #[] (fun _ => none) (branchRaw false 46 target) .scalar untouched - (untouched.map Option.isSome) (branchCode false))) = true ∧ - (¬ FeatureFlowValid #[] (fun _ => none) forwarded .scalar states mask code) := by + !decide (FeatureFlowValid #[] (fun _ => none) (fun _ => none) (branchRaw false 46 target) + .scalar untouched + (untouched.map Option.isSome) (branchCode false 46))) = true ∧ + (¬ FeatureFlowValid #[] (fun _ => none) (fun _ => none) forwarded .scalar states mask code) := + by decide +kernel #print axioms reference_type_branch_rejections @@ -170,20 +202,108 @@ theorem reference_type_results : let result := if same then 1 else 0 let states := (branchStates false).take 5 ++ [state [some result]] let kinds := (branchKinds false).take 5 ++ [some [.word32]] - let code := [.nop, .nop, .nop, .nop, .const32 result, .ret none] - decide (FeatureFlowValid #[] (fun _ => none) + let code := typeOperands word (if same then word else intType) ++ [.ceq, .ret] + decide (FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar states (List.replicate 6 true) code) && - decide (ScalarFlowValid #[] body states kinds code) && - !decide (FeatureFlowValid #[] (fun _ => none) { body with code := body.code.set 4 (i 65026) } + decide (ScalarFlowValid body states kinds code) && + !decide (FeatureFlowValid #[] (fun _ => none) (fun _ => none) + { body with code := body.code.set 4 (i 65026) } .scalar states (List.replicate 6 true) code) && - !decide (FeatureFlowValid #[] (fun _ => none) + !decide (FeatureFlowValid #[] (fun _ => none) (fun _ => none) { body with code := body.code.set 4 (i 65025 (.target 5)) } .scalar states (List.replicate 6 true) code) && - !decide (FeatureFlowValid #[] (fun _ => none) body .scalar states (List.replicate 6 true) - (code.set 4 (.const32 (if same then 0 else 1)))) && - !decide (ScalarFlowValid #[] body states kinds - (code.set 4 (.const32 (if same then 0 else 1))))) = true := by + !decide (FeatureFlowValid #[] (fun _ => none) (fun _ => none) body .scalar states + (List.replicate 6 true) + (code.set 4 (.ldc_i4 (if same then 0 else 1)))) && + !decide (ScalarFlowValid body states kinds + (code.set 4 (.ldc_i4 (if same then 0 else 1))))) = true := by decide +kernel #print axioms reference_type_results +private def dog : TypeExpr := .object owner "Fixture.Dog" +private def bird : TypeExpr := .object owner "Fixture.Bird" + +/-- `castclass` and `isinst` of a class match their own raw opcode and class on an object or + null. -/ +theorem class_tests_match : + scalarEmissionMatches none none (i 116 (.type dog)) [.object] (.castclass dog) = true ∧ + scalarEmissionMatches none none (i 117 (.type dog)) [.nullObject] + (.isinst dog) = true := by + decide +#print axioms class_tests_match + +/-- A raw `isinst` emitted as `castclass` is rejected by the scalar check and the decoder: the + two differ on a failed test. -/ +theorem isinst_decoded_as_castclass : + scalarEmissionMatches none none (i 117 (.type dog)) [.object] (.castclass dog) = false ∧ + valueEncodingMatches none (i 117 (.type dog)) (.castclass dog) = false := by + decide +#print axioms isinst_decoded_as_castclass + +/-- A cast emitted against another class than its raw operand is rejected by the scalar check + and the decoder. -/ +theorem cast_other_type : + scalarEmissionMatches none none (i 116 (.type dog)) [.object] (.castclass bird) = false ∧ + valueEncodingMatches none (i 116 (.type dog)) (.castclass bird) = false := by + decide +#print axioms cast_other_type + +/-- The feature check ties each type test to its own raw opcode: a raw `castclass` emitted as + `isinst` and a raw `isinst` emitted as `castclass` are rejected. -/ +theorem type_test_feature_opcodes : + featureOpMatches #[] (raw []) (i 116 (.type dog)) (.castclass dog) = true ∧ + featureOpMatches #[] (raw []) (i 117 (.type dog)) (.isinst dog) = true ∧ + featureOpMatches #[] (raw []) (i 116 (.type dog)) (.isinst dog) = false ∧ + featureOpMatches #[] (raw []) (i 117 (.type dog)) (.castclass dog) = false := by + decide +kernel +#print axioms type_test_feature_opcodes + +/-- Both equality calls retain their method operand and both closed type tokens. -/ +theorem decodedTypeEquality : + ((Method.ofRaw? .scalar {} [] (List.replicate 5 true) + (raw (test word word)) []).map Method.code) = + some (typeOperands word word ++ [equalCall]) ∧ + ((Method.ofRaw? .scalar {} [] (List.replicate 5 true) + (raw (test word intType)) []).map Method.code) = + some (typeOperands word intType ++ [equalCall]) := ⟨rfl, rfl⟩ + +#print axioms decodedTypeEquality + +/-- The call operand with one field of its site changed. -/ +private def changeSite (operand : RawOperand) (change : CallSite → CallSite) : RawOperand := + match operand with + | .call site => .call (change site) + | other => other + +/-- A same-named GetTypeFromHandle or op_Equality outside the runtime's System.Type folds no + type test, so the live ldtoken at pc 0 refuses the method. -/ +theorem foreign_type_members : + let foreignOwner := changeSite fromHandle fun site => + { site with method.owner := .atom "Fixture" "System.Type" } + let signature := "System.Type System.Type::GetTypeFromHandle(System.RuntimeTypeHandle)" + let foreignTarget := changeSite fromHandle fun site => + { site with target := .external ("Fixture::" ++ signature) } + let forwarded := changeSite equality fun site => + { site with method.owner := .atom "Forwarder" "System.Type" } + let control := test marker marker + [control.set 1 (i 40 foreignOwner), control.set 1 (i 40 foreignTarget), + control.set 4 (i 40 forwarded)].all (fun code => + decide (foldRoles code = List.replicate 5 none) && + decide ((decodeRaw .scalar {} [] (List.replicate 5 true) (raw code) []).map + Method.code = liveHoleAt 0 208)) = true := by + decide +kernel + +#print axioms foreign_type_members + +/-- Same-named types in different possible forwarding scopes leave both the operator and + the reference branch inconclusive. Neither operation reports an unequal result. -/ +theorem unresolved_runtime_identity (body : Method) (frame : Safety.Frame) (memory : Safety.Memory) : + let stack : List Safety.Value := [.runtimeType word, .runtimeType (.atom "Forwarder" "System.UInt64")] + let failure : Safety.ExecutionResult Safety.FrameAction := + .error (.unsupported "Type comparison needs a resolved type identity") + Safety.runModel body (.typeEquality true) 0 frame stack memory = failure ∧ + Safety.step body (.bne_un 7) 0 [] frame stack memory = failure := ⟨rfl, rfl⟩ + +#print axioms unresolved_runtime_identity + end Tests.TypeTestEvidence diff --git a/Tests/TypeTests.cs b/Tests/TypeTests.cs index e2c17843e..72e3b7524 100644 --- a/Tests/TypeTests.cs +++ b/Tests/TypeTests.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include PreparedLeanFixtures.cs #:include RejectedEvidence.cs #:include GeneratedEvidence.cs @@ -22,97 +27,272 @@ RequireLeanFixtures("CIL.Extraction.Translation", "CIL.SymbolicExecution", "CIL.Safety.Execution", "Tests.TypeTestEvidence"); -string[] Ops(string source, int method) => ExtractedInstructions(source) - .Where(instruction => instruction.Method == method).Select(instruction => instruction.Op).ToArray(); +// Read each instruction from its generated lookup equation and check the method covers the complete raw body. +string[][] Ops(string folder, int length, params int[] methods) +{ + var source = File.ReadAllText(Path.Combine(folder, "Extracted.lean")); + var probe = Path.Combine(folder, "DecodedInstructions.lean"); + var queries = methods.Select(method => + { + var equations = Enumerable.Range(0, length).Select(pc => $"tag Extracted.method{method}_instruction{pc}"); + return $$""" + example : Extracted.method{{method}}.code.length = {{length}} := rfl + #eval IO.println ("DECODED " ++ String.intercalate "|" [{{string.Join(", ", equations)}}]) + """; + }); + File.WriteAllText(probe, $$""" + {{source}} + + /-- Classify a generated instruction at its proven program counter. -/ + private def tag {code : List CIL.Instruction} {pc : Nat} {op : CIL.Instruction} + (_ : code[pc]? = some op) : String := + match op with + | .nop => ".nop" + | .ldtoken (.type t) => ".ldtoken " ++ String.ofList ((CIL.Extraction.plainTypeName t).getD []) + | .call method _ => ".call " ++ method.name.display + | .ceq => ".ceq" + | .beq target => ".beq " ++ toString target + | .bne_un target => ".bne.un " ++ toString target + | .br target => ".br " ++ toString target + | .ldc_i4 word => ".ldc_i4 " ++ toString word.toNat + | .unadmitted opcode => ".unadmitted " ++ toString opcode + | _ => "other" + + {{string.Join("\n", queries)}} + """); + const string prefix = "DECODED "; + var rows = Run(["lake", "env", "lean", probe], cwd: FixturePackage()).ReplaceLineEndings("\n").Split('\n') + .Where(line => line.StartsWith(prefix, StringComparison.Ordinal)) + .Select(line => line[prefix.Length..].Split('|')).ToArray(); + if (rows.Length != methods.Length) throw new InvalidOperationException("Missing decoded instruction equations"); + return rows; +} -// A fold is exactly four emitted no-ops followed by the decided word. -List<(int Pc, string Word)> Folds(string[] ops) => Enumerable.Range(4, Math.Max(0, ops.Length - 4)) - .Where(pc => ops[(pc - 4)..pc].All(op => op == ".nop") && ops[pc].StartsWith(".const32 ", StringComparison.Ordinal)) - .Select(pc => (pc, ops[pc][".const32 ".Length..])).ToList(); +// Compare each reachable typeof sequence with its source operands, closed generic argument and branch target. +static int[] CheckComparisons(string[] ops, IList body, string argument) +{ + List checkedPcs = []; + for (int pc = 4; pc < ops.Length; pc++) + { + if (body[pc - 4].OpCode.Code != Code.Ldtoken || body[pc - 2].OpCode.Code != Code.Ldtoken || ops[pc].StartsWith(".unadmitted ", StringComparison.Ordinal)) + continue; + string Token(int at) => ".ldtoken " + (body[at].Operand is GenericParameter + ? argument : ((TypeReference)body[at].Operand).FullName); + string comparison = body[pc].OpCode.Code switch + { + Code.Call => ".call " + ((MethodReference)body[pc].Operand).Name, + Code.Ceq => ".ceq", + Code.Beq or Code.Beq_S => ".beq " + body.IndexOf((Instruction)body[pc].Operand), + Code.Bne_Un or Code.Bne_Un_S => ".bne.un " + body.IndexOf((Instruction)body[pc].Operand), + _ => throw new InvalidOperationException($"Unknown comparison at pc {pc}") + }; + string[] expected = [Token(pc - 4), ".call GetTypeFromHandle", Token(pc - 2), ".call GetTypeFromHandle", comparison]; + if (!ops[(pc - 4)..(pc + 1)].SequenceEqual(expected)) + throw new InvalidOperationException($"Changed typeof instruction at pc {pc}: {string.Join(" | ", ops)}"); + checkedPcs.Add(pc); + } + return [.. checkedPcs]; +} + +static string Comparison(string theorem, int pc, bool same, bool equality) +{ + return $$""" + theorem {{theorem}} (args : List Value) (frame : Frame) (stack : List Value) (memory : Memory) : + SegmentRun Extracted.program 1 {{pc}} {{pc + 5}} 5 args ⟨frame, stack, memory⟩ + ⟨frame, .scalar (.i32 {{(same == equality ? 1 : 0)}}) :: stack, memory⟩ := by + exact SegmentRun.compareRuntimeTypes (pc := {{pc}}) (same := {{(same ? "true" : "false")}}) + Extracted.program_method1 Extracted.method1_instruction{{pc}} Extracted.method1_instruction{{pc + 1}} + Extracted.method1_instruction{{pc + 2}} Extracted.method1_instruction{{pc + 3}} + Extracted.method1_instruction{{pc + 4}} Extracted.method1_link{{pc + 1}} Extracted.method1_link{{pc + 3}} + Extracted.method1_link{{pc + 4}} Extracted.method1_model{{pc + 1}} Extracted.method1_model{{pc + 4}} + Extracted.method1_typeIdentity{{pc + 4}} args frame stack memory + #print axioms {{theorem}} + """; +} void QueueRuntime(string name, string folder) { var budget = name == "ReferenceCases" ? 128 : 64; - var claims = new[] { 0, 1, -1, int.MaxValue, int.MinValue }.Select(input => + int[] boundaries = [0, 1, -1, int.MaxValue, int.MinValue]; + var claims = boundaries.Select(input => $"(CIL.Safety.invoke Extracted.program {budget} 0 " + $"[.scalar (.i32 {unchecked((uint)input)})] CIL.Safety.Memory.empty).map Prod.snd = " + $".ok [.scalar (.i32 {unchecked((uint)(int)runtime.GetMethod(name)!.Invoke(null, [input])!)})]").ToList(); + var (initial, result) = name switch + { + "Marker" => ("(initial : CIL.Safety.Memory)", "value + 1"), + "Other" => ("(initial : CIL.Safety.Memory)", "value + 2"), + "ReferenceCases" => ("", "(value + 4 + 1) + (value + 2) + (value + 3)"), + _ => throw new ArgumentOutOfRangeException(nameof(name)) + }; + var memory = initial.Length == 0 ? "CIL.Safety.Memory.empty" : "initial"; var proof = Path.Combine(folder, "BoundaryProof.lean"); - File.WriteAllText(proof, - "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\ntheorem correctness :\n" + - string.Join(" ∧\n", claims) + " := by\n exact ⟨" + string.Join(", ", claims.Select(_ => "rfl")) + - "⟩\n#print axioms correctness\nend ExampleProof\n"); + var allInputs = $"(∀ (value : CIL.W32) {initial}, (CIL.Safety.invoke Extracted.program {budget} 0 " + + $"[.scalar (.i32 value)] {memory}).map Prod.snd = .ok [.scalar (.i32 ({result}))])"; + var comparisonPath = name == "Other" ? """ + apply (firstComparison args frame [] initial 57).trans + apply (run_next (fuel := 56) Extracted.program_method1 Extracted.method1_instruction5 + (show step _ _ 5 args frame [.scalar (.i32 0)] initial = .ok (.next 10 [] frame initial) from rfl)).trans + exact (secondComparison args frame [] initial 51).trans (by rfl) + """ : "exact (firstComparison args frame [] initial 57).trans (by rfl)"; + var general = name == "ReferenceCases" ? """ + by + intro value + have raw : (CIL.Safety.invoke Extracted.program 128 0 [.scalar (.i32 value)] + CIL.Safety.Memory.empty).map Prod.snd = + .ok [.scalar (.i32 ((value + 4 + 1) + (value + 0 + 2) + (value + 0 + 3)))] := rfl + have zero : value + (0 : CIL.W32) = value := BitVec.add_zero value + rw [zero] at raw + exact raw + """ : $$""" + by + intro value initial + let args : List Value := [.scalar (.i32 value)] + let result : List Value := [.scalar (.i32 (value + {{(name == "Marker" ? 1 : 2)}}))] + let frame : Frame := ⟨initial.nextIdentity, [], [], 0⟩ + have child : invoke Extracted.program 62 1 args initial = .ok (initial, result) := by + apply invoke_of_run (frame := frame) (entered := initial) Extracted.program_method1 rfl rfl + {{comparisonPath.Replace("\n", "\n ")}} + have returned : invoke Extracted.program 64 0 args initial = .ok (initial, result) := by + apply invoke_of_run (frame := frame) (entered := initial) Extracted.program_method0 rfl rfl + apply Eq.trans (run_next (fuel := 63) Extracted.program_method0 Extracted.method0_instruction0 + (show step _ _ 0 args frame [] initial = .ok (.next 1 args frame initial) from rfl)) + exact run_call_of_invoke (fuel := 62) (rest := []) Extracted.program_method0 + Extracted.method0_instruction1 rfl child rfl + change (invoke Extracted.program 64 0 args initial).map Prod.snd = .ok result + rw [returned] + rfl + """; + var comparisons = name switch + { + "Marker" => Comparison("firstComparison", 0, same: true, equality: true), + "Other" => Comparison("firstComparison", 0, same: false, equality: true) + "\n" + + Comparison("secondComparison", 10, same: false, equality: false), + _ => "" + }; + File.WriteAllText(proof, $$""" + import Extracted + import CIL.Safety.RuntimeTypeSegment + import CIL.Safety.CallComposition + namespace ExampleProof + open CIL.Safety + {{comparisons}} + theorem allInputs : {{allInputs}} := {{general}} + #print axioms allInputs + theorem correctness : {{allInputs}} ∧ + {{string.Join(" ∧\n ", claims)}} := by + exact ⟨allInputs, {{string.Join(", ", boundaries.Select(input => + $"allInputs {unchecked((uint)input)}" + (initial.Length == 0 ? "" : " Memory.empty")))}}⟩ + #print axioms correctness + end ExampleProof + """); queue.Add(new($"TypeTests::{name}(int)", proof, Path.Combine(root, name + "-Verified")), - () => Console.WriteLine($"PASS {name}: {claims.Count} runtime comparisons")); + () => Console.WriteLine($"PASS {name}: all 32-bit inputs and {claims.Count} runtime comparisons")); } // Matching and non-matching type arguments decide opposite edges and prune the dead code. -foreach (var (name, expected, offset) in new[] { ("Marker", new[] { "1" }, 1), ("Other", new[] { "0", "1" }, 2) }) +using var examples = ModuleDefinition.ReadModule(assembly); +var bodies = examples.GetType("Examples.TypeTests").Methods; +var classify = bodies.Single(method => method.Name == "Classify").Body.Instructions; +var comparisonPcs = classify.Where(op => op.OpCode.Code == Code.Call && op.Operand is MethodReference + { Name: "op_Equality" or "op_Inequality" }).Select(classify.IndexOf).ToArray(); +int longPc = -1; +foreach (var (name, argument, liveTests, offset) in new[] { ("Marker", "Examples.TypeMarker", 1, 1), ("Other", "Examples.OtherMarker", 2, 2) }) { var folder = Path.Combine(root, name); Run(Extract("extract", assembly, "Examples", $"TypeTests::{name}(int)", folder)); var source = File.ReadAllText(Path.Combine(folder, "Extracted.lean")); - var ops = Ops(source, 1); - var folds = Folds(ops); - if (!folds.Select(fold => fold.Word).SequenceEqual(expected) || - !ops.Any(op => op.StartsWith(".unsupported", StringComparison.Ordinal)) || - ops.Where((op, pc) => folds.All(fold => fold.Pc != pc)).Count(op => op.StartsWith(".const32", StringComparison.Ordinal)) != 1 || - !ops.Contains($".const32 {offset}")) - throw new InvalidOperationException($"Unexpected folded body for {name}: {string.Join(" | ", ops)}"); - Console.WriteLine($"PASS {name}: folded {string.Join(", ", expected)}, dead branches pruned and certificate checked"); + var ops = Ops(folder, classify.Count, 1).Single(); + var comparisons = CheckComparisons(ops, classify, argument); + // Each pruned pc is a hole carrying the raw opcode at that pc of the shared generic body. + var holes = Enumerable.Range(0, ops.Length) + .Where(pc => ops[pc].StartsWith(".unadmitted ", StringComparison.Ordinal)).ToArray(); + if (!comparisons.SequenceEqual(comparisonPcs.Take(liveTests)) || + holes.Length == 0 || holes.Any(pc => ops[pc] != $".unadmitted {(ushort)classify[pc].OpCode.Value}") || + ops.Count(op => op.StartsWith(".ldc_i4", StringComparison.Ordinal)) != 1 || + !ops.Contains($".ldc_i4 {offset}")) + throw new InvalidOperationException($"Unexpected decoded body for {name}: {string.Join(" | ", ops)}"); + Console.WriteLine($"PASS {name}: {liveTests} original type comparisons, dead branches pruned and certificate checked"); QueueRuntime(name, folder); + if (name == "Marker") longPc = holes.First(pc => classify[pc].OpCode.Code == Code.Ldarg_0); - // The lookup equations follow the forged definition, so only the feature certificate can reject the word. - var pc = folds[0].Pc; - var lines = source.Split('\n'); - var start = Array.FindIndex(lines, line => line.StartsWith("def method1 : Method :=", StringComparison.Ordinal)); - var forged = expected[0] == "1" ? ".const32 0" : ".const32 1"; - var site = start + 1 + pc; - if (lines[site].Trim().TrimEnd(',') != $".const32 {expected[0]}") - throw new InvalidOperationException("Could not locate the folded site in " + name); - lines[site] = lines[site].Replace($".const32 {expected[0]}", forged); + // The feature certificate must reject replacing the call with its correctly predicted result. + var forged = name == "Marker" ? ".ldc_i4 1" : ".ldc_i4 0"; var mutated = Path.Combine(root, name + "-forged.lean"); const string obligation = "method1_features_validated"; var control = Path.Combine(root, name + "-control.lean"); - File.WriteAllText(control, NarrowEvidence(source, obligation)); - controls.Add(control, "PASS unchanged type-test feature obligation: " + name, obligation); - File.WriteAllText(mutated, NarrowEvidence(string.Join('\n', lines), obligation)); - rejected.Add(mutated, $"PASS {name}: forged type-test word rejected by the feature certificate", - "Extraction.FeatureFlowValid rawLayouts.toArray (Extraction.evidenceConstants methodEvidence) " + - "method1Closed", "is false"); + var evidence = NarrowEvidence(source, obligation); + File.WriteAllText(control, evidence); + controls.Add(control, $"PASS unchanged type-test feature obligation: {name}", obligation); + File.WriteAllText(mutated, ForgeDecodedInstruction(evidence, 1, comparisonPcs[0], + $"fun _ => {forged}")); + rejected.Add(mutated, $"PASS {name}: constant substituted for a type comparison rejected by the feature certificate", + "Extraction.FeatureFlowValid", FeatureObligation(1), "is false"); } +// A two-byte opcode at a pruned pc is written as 0xFE00 plus its second byte, here the long form of ldarg. +const int TwoBytePrefix = 0xFE00, LdargSecondByte = 0x09; +var longFolder = Path.Combine(root, "LongOpcode"); +Directory.CreateDirectory(longFolder); +var longDll = Path.Combine(longFolder, "Examples.dll"); +using (var module = ModuleDefinition.ReadModule(assembly)) +{ + var method = module.GetType("Examples.TypeTests").Methods.Single(m => m.Name == "Classify"); + var code = method.Body.Instructions; + code[longPc].OpCode = OpCodes.Ldarg; + code[longPc].Operand = method.Parameters[0]; + module.Write(longDll); +} +Run(Extract("extract", longDll, "Examples", "TypeTests::Marker(int)", longFolder)); +var longOps = Ops(longFolder, classify.Count, 1).Single(); +if (longOps[longPc] != $".unadmitted {TwoBytePrefix + LdargSecondByte}") + throw new InvalidOperationException($"Two-byte opcode hole at pc {longPc}: {string.Join(" | ", longOps)}"); +Console.WriteLine($"PASS two-byte opcode hole: pc {longPc} carries {TwoBytePrefix + LdargSecondByte}"); + var referenceFolder = Path.Combine(root, "ReferenceCases"); Run(Extract("extract", assembly, "Examples", "TypeTests::ReferenceCases(int)", referenceFolder)); -var referenceSource = File.ReadAllText(Path.Combine(referenceFolder, "Extracted.lean")); -var referenceOps = Enumerable.Range(1, 3).Select(index => Ops(referenceSource, index)).ToArray(); -if (referenceOps.Any(ops => ops.Length == 0) || - !referenceOps.SelectMany(ops => Folds(ops).Select(fold => fold.Word)).Order().SequenceEqual(["0", "0", "1"]) || - referenceOps.Sum(ops => Enumerable.Range(4, ops.Length - 4).Count(pc => - ops[(pc - 4)..pc].All(op => op == ".nop") && - ops[pc].StartsWith(".branch ", StringComparison.Ordinal))) != 3) - throw new InvalidOperationException("Missing exact typeof result or branch folds"); +var reference = bodies.Single(method => method.Name == "ReferenceClassify").Body.Instructions; +var referenceOps = Ops(referenceFolder, reference.Count, 1, 2, 3); +var referenceArguments = new List(); +foreach (var ops in referenceOps) +{ + var firstToken = ops.First(op => op.StartsWith(".ldtoken ", StringComparison.Ordinal)); + var argument = firstToken[".ldtoken ".Length..]; + var comparisons = CheckComparisons(ops, reference, argument); + if (comparisons.Length != (argument == "Examples.TypeMarker" ? 2 : 3) || + ops[comparisons[0]] != ".ceq" || + comparisons.Skip(1).Any(pc => reference[pc].Operand is not Instruction)) + throw new InvalidOperationException($"Missing reference comparisons for {argument}"); + referenceArguments.Add(argument); +} +if (!referenceArguments.Order(StringComparer.Ordinal).SequenceEqual(["Examples.OtherMarker", "Examples.TypeMarker", "System.UInt64"])) + throw new InvalidOperationException("Unexpected closed generic arguments in the reference cases"); QueueRuntime("ReferenceCases", referenceFolder); queue.Flush(); controls.Check(replay: false); rejected.Check(); -// A fold never hides reachable unsupported code. +// A reachable unsupported call after the comparison must stop extraction. var live = Execute(Extract("extract", assembly, "Examples", "TypeTests::Word(int)", Path.Combine(root, "Word"))); if (live.Code == 0 || !live.Output.Contains("External managed dependency: System.Int32 System.Environment::get_ProcessorCount()")) - throw new InvalidOperationException("Reachable unsupported code after a fold was accepted:\n" + live.Output); + throw new InvalidOperationException("Reachable unsupported code after a type comparison was accepted:\n" + live.Output); Console.WriteLine("PASS Word: the live non-matching branch is still validated"); // Anything other than the exact closed encoding stays unsupported. +const string externalTypeCall = "External managed dependency: System.Type System.Type::GetTypeFromHandle"; foreach (var (mutation, marker) in new[] { - ("operand-gap", "External managed dependency: System.Type System.Type::GetTypeFromHandle"), + ("handle-return-scope", externalTypeCall), + ("handle-parameter-scope", externalTypeCall), + ("equality-parameter-scope", externalTypeCall), + ("vararg-handle", externalTypeCall), + ("operand-gap", externalTypeCall), ("type-local", "Unsupported type: System.Type"), - ("forwarded-scope", "External managed dependency: System.Type System.Type::GetTypeFromHandle"), - ("reference-equality", "External managed dependency: System.Type System.Type::GetTypeFromHandle"), + ("forwarded-scope", externalTypeCall), + ("reference-equality", externalTypeCall), ("interior-branch", "Branch into a folded type comparison"), - ("branch-forwarded-scope", "External managed dependency: System.Type System.Type::GetTypeFromHandle"), + ("branch-forwarded-scope", externalTypeCall), ("branch-interior-branch", "Branch into a folded type comparison") }) { @@ -135,6 +315,22 @@ void QueueRuntime(string name, string folder) var fromHandle = (MethodReference)code[at + 1].Operand; switch (branchFold ? mutation["branch-".Length..] : mutation) { + case "handle-return-scope": + case "handle-parameter-scope": + case "equality-parameter-scope": + var wrongScope = new AssemblyNameReference("TypeSignatureFixture", new Version(1, 0, 0, 0)); + module.AssemblyReferences.Add(wrongScope); + var equality = (MethodReference)code[at + 4].Operand; + if (mutation == "handle-parameter-scope") + fromHandle.Parameters[0].ParameterType = new TypeReference("System", "RuntimeTypeHandle", + module, wrongScope, valueType: true); + else if (mutation == "handle-return-scope") + fromHandle.ReturnType = new TypeReference("System", "Type", module, wrongScope); + else equality.Parameters[1].ParameterType = new TypeReference("System", "Type", module, wrongScope); + break; + case "vararg-handle": + fromHandle.CallingConvention = MethodCallingConvention.VarArg; + break; case "operand-gap": il.InsertAfter(code[at + 1], il.Create(OpCodes.Nop)); break; case "type-local": var local = new VariableDefinition(fromHandle.ReturnType); @@ -166,4 +362,4 @@ void QueueRuntime(string name, string folder) throw new InvalidOperationException("Incorrect type-test rejection: " + mutation + "\n" + result.Output); Console.WriteLine("PASS rejection: " + mutation); } -Console.WriteLine("PASS closed typeof folding, pruning, runtime comparisons, forged words and exact-pattern rejection"); +Console.WriteLine("PASS closed typeof instructions, reachability, CLR comparisons and rejection of forged operands"); diff --git a/Tests/TypedCopy.cs b/Tests/TypedCopy.cs index 5cb936c4e..68df1bac9 100644 --- a/Tests/TypedCopy.cs +++ b/Tests/TypedCopy.cs @@ -2,6 +2,12 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ContractVariants.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -208,17 +214,38 @@ object Value(Type type, ulong bits) method.Body.Instructions[1].OpCode = OpCodes.Ldarg_0; wrong.Write(wrongDll); } - var failed = false; - try - { - verification.Verify(wrongDll, "Examples", selector, At("Examples/Proofs/TypedCopy.lean"), wrongOutput); - } - catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) - { - failed = true; - } - if (!failed || File.Exists(Path.Combine(wrongOutput, "verified.json"))) - throw new InvalidOperationException("Incorrect copy direction accepted"); + verification.VerifyContractVariant(dll, "Examples", selector, Path.Combine(root, "CopyContract"), wrongDll, + selector, wrongOutput, """ + /-- The source holds the input word and the writable destination starts uninitialized. -/ + def storage (word : W64) : Memory where + allocations id := if id < 2 then some ⟨.callerStack, ⟨8, 1, []⟩, true, [8]⟩ else none + cells id offset := + if id = 0 then ⟨(numberBytes word.toNat 8)[offset]?.getD 0, true⟩ else ⟨0, false⟩ + views := [⟨0, 0, 8, true, true⟩, ⟨1, 0, 8, true, true⟩] + nextIdentity := 2 + + /-- The copy returns the source word and may write the destination's eight bytes. -/ + def contract : MethodContract W64 where + args _ := [.reference (.address ⟨1, 0⟩), .reference (.address ⟨0, 0⟩)] + pre word memory := memory = storage word + footprint _ := [(⟨1, 0⟩, 8)] + post word _ _ values := values = [.scalar (.i64 word)] + """, """ + /-- Every input survives the copy and load. -/ + theorem correctness : contract.Holds Extracted.program 0 := by + rintro word _ rfl + refine ⟨8, _, rfl, ?_, _, rfl, ?_⟩ + · exact Confined.write + (rfl : write (storage word) ⟨1, 0⟩ (numberBytes word.toNat 8) 1 = .ok _) + (by simp [contract, numberBytes]) + · exact Except.ok.inj (ExampleProof.correctness word) + """, """ + /-- Reversing the copy reads an uninitialized word from allocation one. -/ + theorem correctness : contract.Refuted Extracted.program 0 := + .of_fault (input := 0) (initial := storage 0) rfl (by decide) + (rfl : invoke Extracted.program 8 0 (contract.args 0) (storage 0) = + .error ⟨0, 2, .memory .uninitialized (.address ⟨1, 0⟩) 8⟩) + """, example: At("Examples/Proofs/TypedCopy.lean")); // Invalid tokens must be rejected before any Lean artifact is emitted. foreach (var mutation in new[] { "float", "object", "array", "scope" }) diff --git a/Tests/UnalignedMemory.cs b/Tests/UnalignedMemory.cs index c3659ad20..189c044ae 100644 --- a/Tests/UnalignedMemory.cs +++ b/Tests/UnalignedMemory.cs @@ -3,6 +3,11 @@ #:property AllowUnsafeBlocks=true #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs @@ -144,38 +149,52 @@ end ExampleProof } queue.Flush(); var rejectedIndex = 0; -foreach (var (name, theorem, before, after) in new[] +foreach (var (name, lowered) in new[] { - ("ReadHalf", "method0_unsafe_validated", ".loadScalar 2 true", ".loadScalar 2 false"), - ("ReadWord", "method0_unsafe_validated", ".loadScalar 8 false", ".loadScalar 4 false"), - ("Read128", "method0_unsafe_validated", ".memory .load128", ".memory .load256"), - ("Write512", "method0_unsafe_validated", ".memory .store512", ".memory .store256"), - ("ReadAggregate", "method0_layouts_validated", ".loadAggregate 32", ".loadAggregate 16"), - ("WriteAggregate", "method0_coverage_validated", ".size 2 0 .store", ".size 2 0 .load"), - ("ReadWord", "method0_unsafe_validated", "target := .external externalIdentity0", "target := .external \"forged\""), - ("ReadPointerWord", "method0_unsafe_validated", "target := .external externalIdentity0", "target := .external \"forged\""), - ("ReadPointerAggregate", "method0_layouts_validated", ".loadAggregate 32", ".loadAggregate 16"), - ("ReadAlignedWord", "method0_unsafe_validated", ".pointerTransfer (.scalar 8 false) 8 false", - ".pointerTransfer (.scalar 8 false) 1 false"), - ("ReadAlignedWord", "method0_unsafe_validated", ".pointerTransfer (.scalar 8 false) 8 false", - ".pointerTransfer (.scalar 8 false) 8 true"), - ("ReadAlignedAggregate", "method0_layouts_validated", ".pointerTransfer (.snapshot (.aggregate 32)) 8 false", - ".pointerTransfer (.snapshot (.aggregate 32)) 4 false"), - ("ReadAligned128", "method0_unsafe_validated", ".pointerTransfer (.snapshot .vector128) 16 false", - ".pointerTransfer (.snapshot .vector128) 8 false") + ("ReadHalf", ".ldind .i2"), ("ReadWord", ".ldind .i8"), + ("Read128", ".ldobj method0.declared.returnType"), + ("Write512", ".stobj (method0.declared.parameters[1]?.getD .void)"), + ("ReadAggregate", ".ldobj method0.declared.returnType"), ("WriteAggregate", ".stobj (method0.declared.parameters[1]?.getD .void)"), + ("ReadPointerWord", ".ldind .i8"), ("ReadPointerAggregate", ".ldobj method0.declared.returnType"), + ("ReadAlignedWord", ChangedCallOperand(1, "name := name!\"ReadUnaligned\"")), + ("WriteAlignedWord", ChangedCallOperand(2, "name := name!\"WriteUnaligned\"")), + ("ReadAlignedAggregate", ChangedCallOperand(1, "name := name!\"ReadUnaligned\"")), + ("ReadAligned128", ChangedCallOperand(1, "name := name!\"ReadUnaligned\"")) }.Where(item => !alignedOnly || item.Item1.Contains("Aligned", StringComparison.Ordinal))) { + const string theorem = "method0_models_validated"; + const string statement = "Extraction.RuntimeModelsAgree method0Closed method0Reachable method0"; var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), theorem); - if (!source.Contains(before, StringComparison.Ordinal)) - throw new InvalidOperationException("Missing unaligned mutation target: " + name + "/" + before); - var path = Path.Combine(root, name, "Rejected-" + theorem + "-" + rejectedIndex++ + ".lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS unaligned evidence rejection: " + name + "/" + theorem, - theorem == "method0_unsafe_validated" ? "UnsafeCallsValid" : - theorem == "method0_layouts_validated" ? "MethodLayoutsValid" : "LayoutUsesComplete"); + var pc = name.StartsWith("Read", StringComparison.Ordinal) ? 1 : 2; + var mutated = $$"""{ method0 with code := method0.code.set {{pc}} ({{lowered}}) }"""; + var replacement = $"Extraction.RuntimeModelsAgree method0Closed method0Reachable {mutated}"; + Reject(name, source, "theorem method0_models_validated", statement, replacement, "RuntimeModelsAgree"); + Reject(name, source, "def method0Closed", "declared := { convention := 16", + "declared := { convention := 17", "RuntimeModelsAgree"); +} +foreach (var name in new[] { "ReadWord", "ReadPointerWord", "ReadAlignedWord" } + .Where(name => !alignedOnly || name.Contains("Aligned", StringComparison.Ordinal))) +{ + var generated = File.ReadAllText(Path.Combine(root, name, "Extracted.lean")); + var source = NarrowEvidence(generated, "method0_body_validated"); + Reject(name, source, "def method0Original", "target := .external externalIdentity0", + "target := .external \"forged\"", "the decoded method does not match its body bytes or metadata"); } rejected.Check(); +void Reject(string name, string source, string declaration, string before, string after, string marker) +{ + var start = source.IndexOf($"\n{declaration} ", StringComparison.Ordinal); + var end = start < 0 ? -1 : source.IndexOf('\n', start + 1); + var target = end < 0 ? -1 : source.IndexOf(before, start, end - start, StringComparison.Ordinal); + if (target < 0) + throw new InvalidOperationException($"Missing transfer mutation target in {name}: {before}"); + var path = Path.Combine(root, name, $"Rejected-{rejectedIndex++}.lean"); + var changed = string.Concat(source.AsSpan(0, target), after, source.AsSpan(target + before.Length)); + File.WriteAllText(path, changed); + rejected.Add(path, $"PASS transfer evidence rejection: {name}/{marker}", marker); +} + partial class Program { delegate T Reader(in byte source); diff --git a/Tests/UniversalContracts.cs b/Tests/UniversalContracts.cs index 30261e88c..8015ad82a 100644 --- a/Tests/UniversalContracts.cs +++ b/Tests/UniversalContracts.cs @@ -2,9 +2,15 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; +using System.Text.Json; using Mono.Cecil; using Mono.Cecil.Cil; @@ -24,10 +30,8 @@ () => Console.WriteLine($"PASS universal {name}: arbitrary inputs and caller memory with preservation")); positives.Flush(); -foreach (var (owner, name, selector, proofName) in contracts) +foreach (var (owner, name, selector, _) in contracts) { - var proof = At($"Examples/Proofs/{proofName}.lean"); - var wrong = Path.Combine(root, "Wrong" + name); Directory.CreateDirectory(wrong); var dll = Path.Combine(wrong, "Examples.dll"); @@ -60,19 +64,58 @@ if ((ulong)runtime.Invoke(null, inputs)! == ulong.MaxValue) throw new InvalidOperationException("Mutation did not change the result: " + name); - // Extraction must succeed: the unchanged contract must reject supported, incorrect CIL. + // Extraction must succeed: the contract is refuted for supported, incorrect CIL by a verified counterexample. Run(Extract("extract", dll, "Examples", selector, wrong)); - File.WriteAllText(Path.Combine(wrong, "verified.json"), "stale report"); - var rejected = false; - try - { - verification.Verify(dll, "Examples", selector, proof, wrong, existingArtifacts: true); - } - catch (InvalidOperationException error) when (error.Message.Contains("Proof.lean") && error.Message.Contains("error:")) + var refutation = Path.Combine(wrong, "Refutation.lean"); + File.WriteAllText(refutation, Refutation(name)); + var refuted = verification.Verify(dll, "Examples", selector, refutation, wrong, theorem: "ExampleProof.refuted", + existingArtifacts: true).refutation; + var entry = JsonDocument.Parse(File.ReadAllText(Path.Combine(wrong, "identity.json"))).RootElement + .GetProperty("entry").GetString(); + if (refuted?.method.signature != entry) + throw new InvalidOperationException("Incorrect implementation has no verified refutation: " + name); + Console.WriteLine($"PASS universal {name} contract refuted for incorrect compiled CIL at the runtime witness"); +} + +// The contract each positive proof states, and the input on which the mutant returns another value. +static string Refutation(string name) +{ + const string maximum = "18446744073709551615"; + var (input, witness, args, arguments, violation) = name switch { - rejected = true; - } - if (!rejected || File.Exists(Path.Combine(wrong, "verified.json"))) - throw new InvalidOperationException("Incorrect implementation accepted: " + name); - Console.WriteLine($"PASS unchanged universal {name} contract rejects incorrect compiled CIL"); + "Choose" => ("Bool × CIL.W64 × CIL.W64", $"(true, {maximum}, 0)", + "[.scalar (.i32 (if input.1 then 1 else 0)), .scalar (.i64 input.2.1), .scalar (.i64 input.2.2)]", + $"[.scalar (.i32 1), .scalar (.i64 {maximum}), .scalar (.i64 0)]", "-, out, rfl, -, wrong"), + "Identity64" => ("CIL.W64", maximum, "[.scalar (.i64 input)]", $"[.scalar (.i64 {maximum})]", + "-, out, rfl, -, wrong"), + _ => ("CIL.W64", maximum, "[.scalar (.i64 input)]", $"[.scalar (.i64 {maximum})]", "-, out, rfl, wrong, -") + }; + var (pre, post) = name == "Struct" + ? ("pre _ memory := memory.WellFormed", + "values = [.scalar (.i64 input)] ∧ MemoryBelow initial.nextIdentity initial final") + : ("pre _ _ := True", name == "Choose" + ? "final = initial ∧ values = [.scalar (.i64 (if input.1 then input.2.1 else input.2.2))]" + : "final = initial ∧ values = [.scalar (.i64 input)]"); + var permitted = name == "Struct" ? "Memory.empty_wellFormed" : "trivial"; + return $$""" + import Extracted + import CIL.Safety.MethodContract + import CIL.Safety.MemoryBelow + open CIL.Safety + namespace ExampleProof + /-- The statement the universal proof of `{{name}}` makes, as a contract. -/ + def contract : MethodContract ({{input}}) where + args input := {{args}} + {{pre}} + footprint _ := [] + post input initial final values := {{post}} + /-- The mutant returns another value at the runtime witness. -/ + theorem refuted : contract.Refuted Extracted.program 0 := + .of_return (input := {{witness}}) (initial := Memory.empty) {{permitted}} + (rfl : invoke Extracted.program 64 0 {{arguments}} Memory.empty = .ok _) + (by rintro ⟨{{violation}}⟩; revert wrong; decide) + #print axioms refuted + end ExampleProof + + """; } diff --git a/Tests/UnsafeBlocks.lean b/Tests/UnsafeBlocks.lean new file mode 100644 index 000000000..bdc208c96 --- /dev/null +++ b/Tests/UnsafeBlocks.lean @@ -0,0 +1,70 @@ +import CIL.Extraction.DecodedMethod +import Tests.Support.RawDecoding + +/-! Decoder, operand binding and execution checks for the eight Unsafe block overloads. -/ + +namespace Tests.UnsafeBlocks +open CIL CIL.Extraction + +/-- A MemberRef for a block helper as it occurs in the example assembly. -/ +private def site (name : String) (key : MetadataName) (copy pointer : Bool) : CallSite := + let byte := TypeExpr.atom runtimeScope "System.Byte" + let address := if pointer then TypeExpr.pointer .void else .byref byte + let addressText := if pointer then "System.Void*" else "System.Byte&" + let second := if copy then addressText else "System.Byte" + let owner := "System.Runtime.CompilerServices.Unsafe" + let types := s!"{addressText},{second},System.UInt32" + let parameters := [address, if copy then address else byte, .atom runtimeScope "System.UInt32"] + let method : MethodRef := + { owner := .atom runtimeScope owner, name := key, hasThis := false, + genericArity := 0, returnType := .void, parameters } + { method, name, declared := { convention := 0, parameters }, + target := .external s!"{runtimeScope}::System.Void {owner}::{name}({types})", + typeArguments := [], parameterCount := 3, returnsValue := false } + +/-- The live instruction reconstructed from the raw call. -/ +private def decoded (call : CallSite) := + let raw : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := true, maxStack := 3, genericParameters := [], code := [⟨0, 40, .call call⟩] } + (decodeRaw .scalar {} [] [true] raw []).map Method.code + +/-- Each overload with the copy or fill operation and alignment required by its declaration. -/ +private def cases : List (CallSite × MemoryOp) := [false, true].flatMap fun pointer => + [(site "CopyBlock" name!"CopyBlock" true pointer, .copyBlock 8), + (site "CopyBlockUnaligned" name!"CopyBlockUnaligned" true pointer, .copyBlock 1), + (site "InitBlock" name!"InitBlock" false pointer, .initBlock 8), + (site "InitBlockUnaligned" name!"InitBlockUnaligned" false pointer, .initBlock 1)] + +/-- The decoder keeps each call and the interpreter resolves its exact operation. -/ +theorem block_emission : cases.all (fun (call, operation) => + decide (decoded call = .ok [.call call.method {}]) && + runtimeModel {} .call call.method == some (.memory operation)) = true := by decide +kernel + +#print axioms block_emission + +/-- A malformed identity, closed signature, declaration or external operand emits a call hole. -/ +theorem block_refusals : cases.all (fun (call, _) => + let word := TypeExpr.atom runtimeScope "System.UInt32" + let invalid : List CallSite := [ + { call with method.owner := .atom "Fixture" "System.Runtime.CompilerServices.Unsafe" }, + { call with method.name := name!"Other" }, { call with name := "Other" }, + { call with method.hasThis := true }, { call with method.genericArity := 1 }, + { call with method.parameters := call.method.parameters.set 0 (.byref word) }, + { call with method.returnType := word }, { call with declared.convention := 0x10 }, + { call with typeArguments := [word] }, + { call with parameterCount := 2 }, { call with returnsValue := true }, + { call with ownerArgumentCount := 1 }] + let other := cases.find? fun (site, _) => + site.name == call.name && site.method.parameters != call.method.parameters + other.any fun (overload, _) => + let crossed := [{ call with target := overload.target }, + { call with declared := overload.declared }] + (invalid ++ crossed).all (fun changed => decide (decoded changed = liveHoleAt 0 40))) = + true := by + decide +kernel + +#print axioms block_refusals + +end Tests.UnsafeBlocks diff --git a/Tests/UnsafeCallEvidence.lean b/Tests/UnsafeCallEvidence.lean index da3060f6a..51d2975d4 100644 --- a/Tests/UnsafeCallEvidence.lean +++ b/Tests/UnsafeCallEvidence.lean @@ -1,24 +1,25 @@ -import CIL.Extraction.UnsafeCalls +import CIL.Extraction.DecodedMethod +import Tests.Support.RawDecoding + +/-! Exact runtime operands, modelled reference helpers and the opcodes that can emit null. -/ open CIL CIL.Extraction namespace Tests.UnsafeCallEvidence -private def scope := "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" +private def scope := + "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" private def unsafeType : TypeExpr := .atom scope "System.Runtime.CompilerServices.Unsafe" private def word : TypeExpr := .atom scope "System.UInt64" private def call : CallSite := { - declaringType := unsafeType, name := "SkipInit" - target := .external (scope ++ "::System.Void System.Runtime.CompilerServices.Unsafe::SkipInit(!!0&)") - typeArguments := [word], parameterCount := 1, hasThis := false, returnsValue := false } -private def nullCall : CallSite := { - declaringType := unsafeType, name := "NullRef" - target := .external (scope ++ "::!!0& System.Runtime.CompilerServices.Unsafe::NullRef()") - typeArguments := [word], parameterCount := 0, hasThis := false, returnsValue := true } -private def testCall : CallSite := { - declaringType := unsafeType, name := "IsNullRef" - target := .external (scope ++ "::System.Boolean System.Runtime.CompilerServices.Unsafe::IsNullRef(!!0&)") - typeArguments := [word], parameterCount := 1, hasThis := false, returnsValue := true } + declared := { convention := 0x10, parameters := [.byref (.parameter 0)] }, + method := { + owner := unsafeType, name := name!"SkipInit", hasThis := false, genericArity := 1, + returnType := .void, parameters := [.byref word], methodArguments := [word] }, + name := "SkipInit", + target := .external (scope ++ "::System.Void System.Runtime.CompilerServices.Unsafe::SkipInit(\ + !!0&)"), + typeArguments := [word], parameterCount := 1, returnsValue := false } private def raw (site : CallSite) : RawMethod := { token := 1, owner := "Fixture", hasThis := false, explicitThis := false, callingConvention := 0, attributes := 0, implementation := 0, @@ -26,78 +27,69 @@ private def raw (site : CallSite) : RawMethod := { maxStack := 1, genericParameters := [], code := [⟨0, 0x28, .call site⟩] } private def nonCall : RawMethod := { raw call with code := [⟨0, 0x26, .none⟩] } -example : UnsafeCallsValid [] (raw call) [true] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw call) [true] [.pop] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { call with name := "Other" }) [true] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { call with target := .external "forged" }) [true] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { call with declaringType := .atom "forged" "System.Runtime.CompilerServices.Unsafe" }) [true] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { call with hasThis := true }) [true] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { call with parameterCount := 2 }) [true] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { call with returnsValue := true }) [true] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { call with typeArguments := [.byref word] }) [true] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { call with typeArguments := [] }) [true] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw call) [] [.skipInit] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw call) [true] [] := by decide +kernel +/-- Changing a redundant call field or the typed reference cannot retain the SkipInit call. -/ +theorem skipInitOperand : + let decode := fun operand => + (decodeRaw .scalar {} [] [true] (raw operand) []).map Method.code + decode call = .ok [.call call.method {}] ∧ + [{ call with name := "Other" }, { call with target := .external "forged" }, + { call with method.owner := .atom "Fixture" "System.Runtime.CompilerServices.Unsafe" }, + { call with method.hasThis := true }, { call with parameterCount := 2 }, + { call with returnsValue := true }, { call with typeArguments := [.byref word] }, + { call with typeArguments := [] }, { call with method.genericArity := 2 }].all + (fun changed => decide (decode changed = liveHoleAt 0 40)) = true := by + decide +kernel --- `NullRef` must push the null reference: no other operation with its stack effect. -example : UnsafeCallsValid [] (raw nullCall) [true] [.memory .nullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw nullCall) [true] [.const32 0] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw nullCall) [true] [.memory (.staticAddress [])] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw nullCall) [true] [.memory .isNullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { nullCall with target := .external "forged" }) [true] [.memory .nullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { nullCall with parameterCount := 1 }) [true] [.memory .nullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { nullCall with returnsValue := false }) [true] [.memory .nullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { nullCall with hasThis := true }) [true] [.memory .nullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { nullCall with typeArguments := [.byref word] }) [true] [.memory .nullRef] := by decide +kernel +#print axioms skipInitOperand --- `IsNullRef` must test for null: a constant or a reinterpretation cannot stand in for it. -example : UnsafeCallsValid [] (raw testCall) [true] [.memory .isNullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw testCall) [true] [.memory .asRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw testCall) [true] [.memory .nullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { testCall with name := "NullRef" }) [true] [.memory .isNullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { testCall with target := .external "forged" }) [true] [.memory .isNullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { testCall with parameterCount := 2 }) [true] [.memory .isNullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { testCall with returnsValue := false }) [true] [.memory .isNullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] (raw { testCall with typeArguments := [word, word] }) [true] [.memory .isNullRef] := by decide +kernel +-- A SkipInit call must retain the checked call operand. +example : UnsafeCallsValid (raw call) [true] [.call call.method {}] := by decide +kernel +example : ¬ UnsafeCallsValid (raw call) [true] [.pop] := by decide +kernel +example : ¬ UnsafeCallsValid (raw call) [] [.call call.method {}] := by decide +kernel +example : ¬ UnsafeCallsValid (raw call) [true] [] := by decide +kernel --- A helper operation cannot replace an unrelated instruction. -example : ¬ UnsafeCallsValid [] nonCall [true] [.memory .nullRef] := by decide +kernel -example : ¬ UnsafeCallsValid [] nonCall [true] [.memory .isNullRef] := by decide +kernel -example : UnsafeCallsValid [] nonCall [true] [.pop] := by decide +kernel --- Unreachable sites are emitted as unsupported placeholders and are not bound. -example : UnsafeCallsValid [] (raw nullCall) [false] [.unsupported "excluded"] := by decide +kernel +-- A real SkipInit call and ordinary stack operations retain distinct opcode and metadata evidence. +example : Instruction.ofRaw? none ⟨0, 38, .none⟩ = some .pop ∧ + valueEncodingMatches none ⟨0, 38, .none⟩ (.call call.method {}) = false ∧ + Instruction.ofRaw? none ⟨0, 20, .none⟩ = some .ldnull ∧ + valueEncodingMatches none ⟨0, 20, .none⟩ (.call call.method {}) = false ∧ + Instruction.ofRaw? none ⟨0, 20, .call call⟩ = none := by decide +kernel +-- Unreachable sites are emitted as holes and are not bound. +example : UnsafeCallsValid (raw call) [false] [.unadmitted 0x28] := by decide +kernel end Tests.UnsafeCallEvidence namespace Tests.UnsafeCallEvidence -/-- A genuine managed call to a local lookalike is not reserved as a lowered runtime helper. -/ +/-- A type of the fixture's own assembly with the runtime `Unsafe` class's name. -/ +private def localUnsafe : TypeExpr := .atom "Fixture" "System.Runtime.CompilerServices.Unsafe" + +/-- A managed generic AreSame method in the fixture's assembly. -/ +private def localAreSame : CallSite := { + declared := { + convention := 0x10, returnType := .atom runtimeScope "System.Boolean", + parameters := [.byref (.parameter 0), .byref (.parameter 0)] } + method := { + owner := localUnsafe, name := name!"AreSame", hasThis := false, genericArity := 1, + methodArguments := [word], returnType := .atom runtimeScope "System.Boolean", + parameters := [.byref word, .byref word] } + name := "AreSame", target := .managed 23, typeArguments := [word], + parameterCount := 2, returnsValue := true } + +/-- A managed lookalike retains its operand and the direct link to its instantiated method. -/ theorem local_reference_helper_name : - UnsafeCallsValid [] (raw { call with - declaringType := .atom "Fixture" "System.Runtime.CompilerServices.Unsafe", - name := "AreSame", parameterCount := 2, returnsValue := true }) [true] [.call 1 2] := by + let instruction := RawInstruction.mk 0 40 (.call localAreSame) + let emitted := Instruction.call localAreSame.method {} + let callee : SpecializedMethod := { + arguments := [word], closed := { raw localAreSame with + token := 23, owner := "Fixture::System.Runtime.CompilerServices.Unsafe", + parameters := localAreSame.parameterTypes, returnType := localAreSame.returnType } } + let check := validateManagedCallWith 1 (fun i => if i == 0 then some callee else none) + (fun _ => none) instruction true + UnsafeCallsValid (raw localAreSame) [true] [emitted] ∧ + siteClaim {} (some (.direct 0)) instruction = .link ∧ + check (some (.direct 0)) emitted = true ∧ check (some (.direct 1)) emitted = false := by decide +kernel #print axioms local_reference_helper_name -/-- The `System.Object` constructor a class constructor calls on its receiver. -/ -private def objectConstructor : CallSite := { - declaringType := .atom scope "System.Object", name := ".ctor" - target := .external (scope ++ "::System.Void System.Object::.ctor()") - typeArguments := [], parameterCount := 0, hasThis := true, returnsValue := false } - --- Only the exact `System.Object` constructor may emit `pop`, and it must. -theorem object_constructor_pop : - UnsafeCallsValid [] (raw objectConstructor) [true] [.pop] := by decide +kernel -#print axioms object_constructor_pop -theorem object_constructor_elsewhere_rejected : - ¬ UnsafeCallsValid [] (raw { objectConstructor with - declaringType := .atom "forged" "System.Object" }) [true] [.pop] ∧ - ¬ UnsafeCallsValid [] (raw { objectConstructor with - target := .external "forged" }) [true] [.pop] ∧ - ¬ UnsafeCallsValid [] (raw { objectConstructor with parameterCount := 1 }) [true] [.pop] ∧ - ¬ UnsafeCallsValid [] (raw { objectConstructor with hasThis := false }) [true] [.pop] ∧ - ¬ UnsafeCallsValid [] (raw objectConstructor) [true] [.nop] ∧ - ¬ UnsafeCallsValid [] (raw call) [true] [.pop] := by decide +kernel -#print axioms object_constructor_elsewhere_rejected - end Tests.UnsafeCallEvidence diff --git a/Tests/UnsafeLayouts.cs b/Tests/UnsafeLayouts.cs index 7392484a0..58a43a5f8 100644 --- a/Tests/UnsafeLayouts.cs +++ b/Tests/UnsafeLayouts.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -44,7 +49,11 @@ void Prove(MethodDefinition method, List claims, string definitions = "" foreach (var method in owner.Methods.Where(m => m.Name.StartsWith("Size", StringComparison.Ordinal))) { var size = (int)runtime.GetMethod(method.Name)!.Invoke(null, null)!; - Prove(method, [$"(CIL.Safety.invoke Extracted.program 8 0 [] CIL.Safety.Memory.empty).map Prod.snd = .ok [.scalar (.i32 {size})]"], term: "Tests.UnsafeWrappers.size_ok _ _ _"); + const string run = "(CIL.Safety.invoke Extracted.program 8 0 [] CIL.Safety.Memory.empty).map Prod.snd"; + Prove(method, [$"""{run} = .ok [.scalar (.i32 {size})]"""], term: """ + Tests.UnsafeWrappers.size_ok (declared := Extracted.method0.declared) + rfl Extracted.method0_model0 Extracted.profile CIL.Safety.Memory.empty + """); } // Runtime byte offsets and Lean references must agree for each element type and offset overload. @@ -75,8 +84,14 @@ void Prove(MethodDefinition method, List claims, string definitions = "" // Forming an address beyond the allocation fails before any dereference. claims.Add($"(CIL.Safety.invoke Extracted.program 8 0 [Tests.reference, .scalar (.i{stackWidth} 5)] memory).map Prod.snd = .error ⟨0, 2, .memory .invalidReference (.address ⟨0, 0⟩) 0⟩"); claims.Add($"(CIL.Safety.invoke Extracted.program 8 0 [.reference .null, .scalar (.i{stackWidth} 0)] memory).map Prod.snd = .error ⟨0, 2, .memory .nullDereference .null 0⟩"); - var success = "Tests.UnsafeWrappers.add_ok _ _ _ _ _ _ _ rfl rfl rfl rfl"; - var failure = "Tests.UnsafeWrappers.add_error _ _ _ _ _ _ _ rfl rfl rfl rfl"; + const string success = """ + Tests.UnsafeWrappers.add_ok (declared := Extracted.method0.declared) + rfl rfl rfl Extracted.method0_model2 _ _ _ _ _ rfl rfl rfl rfl + """; + const string failure = """ + Tests.UnsafeWrappers.add_error (declared := Extracted.method0.declared) + rfl rfl Extracted.method0_model2 _ _ _ _ _ rfl rfl rfl rfl + """; Prove(method, claims, memory, "⟨" + string.Join(", ", Enumerable.Repeat(success, 5).Concat([failure, failure])) + "⟩"); } diff --git a/Tests/UnsafeModels.lean b/Tests/UnsafeModels.lean new file mode 100644 index 000000000..fad52794a --- /dev/null +++ b/Tests/UnsafeModels.lean @@ -0,0 +1,459 @@ +import CIL.Extraction.DecodedMethod +import CIL.Safety.StepComposition +import CIL.Safety.ResolutionFacts +import Tests.Support.RawDecoding + +/-! +# Unsafe helper model tests + +Each modelled `Unsafe` helper (`SizeOf`, the three reference comparisons and `SkipInit`) +resolves from its closed reference and checked layouts. Each resolving site retains its +call. A missing layout, a different layout identity or a forged declaration produces a hole. +-/ + +namespace Tests.UnsafeModels +open CIL CIL.Extraction CIL.Safety + +/-- The `System.Runtime` type `name`. -/ +private def runtimeType (name : String) : TypeExpr := .atom runtimeScope name +/-- `System.Int32`. -/ +private def int32 : TypeExpr := runtimeType "System.Int32" + +/-- `int Unsafe.SizeOf()` as a call site instantiates it. -/ +private def sizeOf (element : TypeExpr) : MethodRef := + { owner := unsafeOwner, name := name!"SizeOf", hasThis := false, genericArity := 1, + returnType := int32, parameters := [], methodArguments := [element] } + +/-- The struct `Fixture::Packet`, read from layout index 0. -/ +private def packet : TypeExpr := .aggregate "Fixture::Packet" 0 + +/-- A sequential struct under `identity` of `words` 64-bit fields, `8 * words` bytes. -/ +private def rawPacket (identity : String) (words : Nat) (byRefLike : Bool) : RawLayout := + { identity, explicit := false, packing := 0, declaredSize := 0, + fields := List.replicate words { type := .unsigned64 }, byRefLike } + +/-- Layouts whose entry 0 is `rawPacket identity words`, as `computeLayouts` computes it. -/ +private def sized (words : Nat) (identity := "Fixture::Packet") (byRefLike := false) : + LayoutEnvironment := + let raw := [rawPacket identity words byRefLike] + { rawLayouts := raw, layouts := (computeLayouts raw).getD #[] } + +-- Every fixture environment holds checked layouts, so a size the tests read is one +-- `layouts_validated` would admit. +example : LayoutsValid (sized 1).rawLayouts (sized 1).layouts := by decide +kernel +example : LayoutsValid (sized 3).rawLayouts (sized 3).layouts := by decide +kernel +example : LayoutsValid (sized 2 (byRefLike := true)).rawLayouts + (sized 2 (byRefLike := true)).layouts := by decide +kernel +example : LayoutsValid (sized 3 "Fixture::Other").rawLayouts + (sized 3 "Fixture::Other").layouts := by decide +kernel + +/-- `Vector256`, whose size is its width under every profile. -/ +private def vector256 : TypeExpr := + .apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector256`1") + (runtimeType "System.UInt64") + +-- The size is the checked layout's: one reference, two environments, two sizes. +example : runtimeModel (sized 3) .call (sizeOf packet) = some (.constant 24) := by + decide +kernel +example : runtimeModel (sized 1) .call (sizeOf packet) = some (.constant 8) := by + decide +kernel +-- A by-ref-like struct inside the layouts has its checked size, as `sizeof` gives it. +example : runtimeModel (sized 2 (byRefLike := true)) .call (sizeOf packet) = + some (.constant 16) := by decide +kernel +-- Primitive and vector widths are the element's own, in any environment. +example : runtimeModel {} .call (sizeOf int32) = some (.constant 4) := by decide +kernel +example : runtimeModel {} .call (sizeOf (runtimeType "System.UIntPtr")) = some (.constant 8) := by + decide +kernel +example : runtimeModel {} .call (sizeOf (runtimeType "System.Char")) = some (.constant 2) := by + decide +kernel +example : runtimeModel {} .call (sizeOf (runtimeType "System.Boolean")) = some (.constant 1) := by + decide +kernel +example : runtimeModel {} .call (sizeOf vector256) = some (.constant 32) := by decide +kernel +-- The model reads no profile: a 256-bit element has its size and model under the scalar profile. +example : (runtimeModel {} .call (sizeOf vector256)).any (·.available .scalar) = true := by + decide +kernel + +-- Another owner, assembly or shape of the reference has no model. +example : runtimeModel {} .call { sizeOf int32 with + owner := .atom "Other, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + "System.Runtime.CompilerServices.Unsafe" } = none := by decide +kernel +example : runtimeModel {} .call { sizeOf int32 with owner := runtimeType "System.Unsafe" } = + none := by decide +kernel +example : runtimeModel {} .call { sizeOf int32 with owner := .apply unsafeOwner int32 } = + none := by decide +kernel +example : runtimeModel {} .call { sizeOf int32 with hasThis := true } = none := by decide +kernel +example : runtimeModel {} .call { sizeOf int32 with genericArity := 2 } = none := by + decide +kernel +example : runtimeModel {} .call { sizeOf int32 with methodArguments := [int32, int32] } = + none := by decide +kernel +example : runtimeModel {} .call { sizeOf int32 with parameters := [int32] } = none := by + decide +kernel +example : runtimeModel {} .call { sizeOf int32 with returnType := runtimeType "System.Int64" } = + none := by decide +kernel +example : runtimeModel {} .newobj (sizeOf int32) = none := by decide +kernel + +-- An element without a size has no model: a generic parameter, a pointer, a class, a generic +-- class instance, a span, a floating-point type, a struct outside the layouts, and a struct +-- whose index holds another identity. +example : runtimeModel {} .call (sizeOf (.parameter 0)) = none := by decide +kernel +example : runtimeModel {} .call (sizeOf (.pointer int32)) = none := by decide +kernel +example : runtimeModel {} .call (sizeOf (.object runtimeScope "System.String")) = none := by + decide +kernel +example : runtimeModel {} .call + (sizeOf (.apply (.objectConstructor runtimeScope "System.Collections.Generic.List`1") int32)) = + none := by decide +kernel +example : runtimeModel {} .call + (sizeOf (.apply (.constructor runtimeScope "System.Span`1") int32)) = none := by + decide +kernel +example : runtimeModel {} .call (sizeOf (runtimeType "System.Single")) = none := by + decide +kernel +example : runtimeModel {} .call (sizeOf packet) = none := by decide +kernel +example : runtimeModel (sized 3 "Fixture::Other") .call (sizeOf packet) = none := by + decide +kernel + +/-- The open declaration of `int Unsafe.SizeOf()`. -/ +private def sizeOfDeclared : OpenSignature := { convention := 0x10, returnType := int32 } + +/-- A call of `method` whose member declares `declared`. -/ +private def site (method : MethodRef) (declared : OpenSignature := sizeOfDeclared) : + RawInstruction := + let (name, result, parameters) := + if method.name == name!"SizeOf" then ("SizeOf", "System.Int32", "") + else if method.name == name!"SkipInit" then ("SkipInit", "System.Void", "!!0&") + else + let name := if method.name == name!"AreSame" then "AreSame" + else if method.name == name!"IsAddressLessThan" then "IsAddressLessThan" + else "IsAddressGreaterThan" + (name, "System.Boolean", "!!0&,!!0&") + let owner := "System.Runtime.CompilerServices.Unsafe" + ⟨0, 40, .call { + method, name, declared, + target := .external s!"{runtimeScope}::{result} {owner}::{name}({parameters})", + typeArguments := method.methodArguments, parameterCount := method.parameters.length, + returnsValue := method.returnType != .void }⟩ + +-- A SizeOf call retains its typed reference and refuses a literal of the same width. +example : runtimeCatalog.siteAgrees (sized 3) .scalar (site (sizeOf packet)) + (.call (sizeOf packet) {}) = true := by decide +kernel +example : runtimeCatalog.siteAgrees (sized 3) .scalar (site (sizeOf packet)) (.ldc_i4 24) = + false := by decide +kernel + +-- A member declared with another convention or result has no SizeOf model, so its call is refused. +example : [0x00, 0x11, 0x30, 0x50, 0x70].all (fun convention => + !runtimeCatalog.siteAgrees (sized 3) .scalar + (site (sizeOf packet) { sizeOfDeclared with convention }) (.call (sizeOf packet) {})) = + true := by decide +kernel +example : runtimeCatalog.siteAgrees (sized 3) .scalar + (site (sizeOf packet) { sizeOfDeclared with returnType := .parameter 0 }) + (.call (sizeOf packet) {}) = false := by decide +kernel +example : runtimeCatalog.siteAgrees (sized 3) .scalar + (site (sizeOf packet) { sizeOfDeclared with parameters := [.parameter 0] }) + (.call (sizeOf packet) {}) = false := by decide +kernel + +/-- A method whose only instruction is `instruction`. -/ +private def method (instruction : RawInstruction) : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [int32], + initLocals := true, maxStack := 3, genericParameters := [], code := [instruction] } + +/-- A body under 24-byte layouts whose only instruction is `emitted`, with no link. -/ +private def emitting (emitted : CIL.Instruction) : CIL.Method := + { code := [emitted], locals := [], returnsValue := true, environment := sized 3 } + +/-- A double vector has a runtime SizeOf call, while its raw object load decodes to a hole. -/ +theorem double_vector_size_and_load : + let head := TypeExpr.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1" + let vector := TypeExpr.apply head (runtimeType "System.Double") + let called := sizeOf vector + let load := method ⟨0, 0x71, .type vector⟩ + runtimeModel {} .call called = some (.constant 16) ∧ + runtimeCatalog.siteAgrees {} .scalar (site called) (.call called {}) = true ∧ + methodCode .scalar {} [] [true] (method (site called)) = [.call called {}] ∧ + methodCode .scalar {} [] [true] load = [.unadmitted 0x71] ∧ + methodCode .scalar {} [] [true, false] { load with code := load.code ++ [⟨5, 0, .none⟩] } = + [.unadmitted 0x71, .unadmitted 0] ∧ + ¬ ObjectOperandsValid load [true] (emitting (.ldobj vector)) := by decide +kernel +#print axioms double_vector_size_and_load + +-- The body validator requires the modelled call at the SizeOf site. +example : RuntimeModelsAgree (method (site (sizeOf packet))) [true] + (emitting (.call (sizeOf packet) {})) := by decide +kernel +example : ¬ RuntimeModelsAgree (method (site (sizeOf packet))) [true] (emitting (.ldc_i4 24)) := by + decide +kernel + +/-- `bool Unsafe.(ref element, ref element)` as a call site instantiates it. -/ +private def comparison (name : MetadataName) (element : TypeExpr) : MethodRef := + { owner := unsafeOwner, name, hasThis := false, genericArity := 1, + returnType := runtimeType "System.Boolean", parameters := [.byref element, .byref element], + methodArguments := [element] } + +/-- `void Unsafe.SkipInit(out element)` as a call site instantiates it. -/ +private def skipInitOf (element : TypeExpr) : MethodRef := + { owner := unsafeOwner, name := name!"SkipInit", hasThis := false, genericArity := 1, + returnType := .void, parameters := [.byref element], methodArguments := [element] } + +/-- `Unsafe.AreSame`, which compares two references for identity. -/ +private def areSame := comparison name!"AreSame" +/-- `Unsafe.IsAddressLessThan`, which orders two references by address. -/ +private def isLess := comparison name!"IsAddressLessThan" +/-- `Unsafe.IsAddressGreaterThan`, which orders two references by address. -/ +private def isGreater := comparison name!"IsAddressGreaterThan" + +-- Each comparison resolves its relation, and SkipInit its model, for an element with a size. +example : runtimeModel {} .call (areSame int32) = some (.referenceCompare .same) := by + decide +kernel +example : runtimeModel {} .call (isLess int32) = some (.referenceCompare .less) := by + decide +kernel +example : runtimeModel {} .call (isGreater int32) = some (.referenceCompare .greater) := by + decide +kernel +example : runtimeModel (sized 3) .call (areSame packet) = some (.referenceCompare .same) := by + decide +kernel +example : runtimeModel {} .call (skipInitOf vector256) = some .skipInit := by decide +kernel +example : runtimeModel (sized 3) .call (skipInitOf packet) = some .skipInit := by decide +kernel + +-- Another result, a parameter of another element, another arity or an element without a size +-- has no model; neither does SkipInit with a result or a second parameter. +example : runtimeModel {} .call { areSame int32 with returnType := int32 } = none := by + decide +kernel +example : runtimeModel {} .call + { isLess int32 with parameters := [.byref int32, .byref (runtimeType "System.Int64")] } = + none := by decide +kernel +example : runtimeModel {} .call { isGreater int32 with genericArity := 2 } = none := by + decide +kernel +example : runtimeModel {} .call (areSame (.parameter 0)) = none := by decide +kernel +-- The owner applied to a type argument names no helper, for every entry. +example : runtimeModel {} .call { areSame int32 with owner := .apply unsafeOwner int32 } = + none := by decide +kernel +example : runtimeModel {} .call { isLess int32 with owner := .apply unsafeOwner int32 } = + none := by decide +kernel +example : runtimeModel {} .call { isGreater int32 with owner := .apply unsafeOwner int32 } = + none := by decide +kernel +example : runtimeModel {} .call { skipInitOf int32 with owner := .apply unsafeOwner int32 } = + none := by decide +kernel +example : runtimeModel {} .call (areSame packet) = none := by decide +kernel +example : runtimeModel {} .call + (skipInitOf (.apply (.constructor runtimeScope "System.Span`1") int32)) = none := by + decide +kernel +example : runtimeModel {} .call + { skipInitOf int32 with returnType := runtimeType "System.Boolean" } = none := by + decide +kernel +example : runtimeModel {} .call + { skipInitOf int32 with parameters := [.byref int32, .byref int32] } = none := by + decide +kernel + +/-- The open declaration of `void Unsafe.SkipInit(out T)`. -/ +private def skipInitDeclared : OpenSignature := + unsafeDeclaration (fun element => [.byref element]) (fun _ => .void) + +/-- The comparison and SkipInit sites over `packet`, each with its member's declaration. -/ +private def sameSite := site (areSame packet) unsafeComparisonDeclaration +/-- The less-than comparison at the packet type. -/ +private def lessSite := site (isLess packet) unsafeComparisonDeclaration +/-- The greater-than comparison at the packet type. -/ +private def greaterSite := site (isGreater packet) unsafeComparisonDeclaration +/-- The SkipInit call at the packet type. -/ +private def skipSite := site (skipInitOf packet) skipInitDeclared + +-- A comparison or SkipInit whose member declares another convention or signature has no model. +example : [0x00, 0x11, 0x30, 0x50, 0x70].all (fun convention => + !runtimeCatalog.siteAgrees (sized 3) .scalar + (site (areSame packet) { unsafeComparisonDeclaration with convention }) + (.call (areSame packet) {})) = true := by decide +kernel +example : runtimeCatalog.siteAgrees (sized 3) .scalar + (site (isLess packet) + { unsafeComparisonDeclaration with parameters := [.byref (.parameter 0)] }) + (.call (isLess packet) {}) = false := by decide +kernel +example : runtimeCatalog.siteAgrees (sized 3) .scalar + (site (skipInitOf packet) { skipInitDeclared with returnType := .parameter 0 }) + (.call (skipInitOf packet) {}) = false := by decide +kernel + +-- Each helper must retain its call at a live site. +example : RuntimeModelsAgree (method sameSite) [true] + (emitting (.call (areSame packet) {})) := by decide +kernel +example : ¬ RuntimeModelsAgree (method sameSite) [true] (emitting .ceq) := by + decide +kernel +example : ¬ RuntimeModelsAgree (method lessSite) [true] (emitting .clt_un) := by + decide +kernel +example : ¬ RuntimeModelsAgree (method greaterSite) [true] (emitting .cgt_un) := by + decide +kernel +example : ¬ RuntimeModelsAgree (method skipSite) [true] + (emitting .pop) := by decide +kernel + +/-- The real decoder keeps the control call and emits the opcode's hole for the changed site. + The live site also rejects the supplied instruction with the same stack effect. -/ +private def refuses (control changed : RawInstruction) (reference : MethodRef) + (layouts : LayoutEnvironment) (replacement : Instruction) : Bool := + let decode := fun environment raw => + (decodeRaw .scalar environment [] [true] (method raw) []).map Method.code + decide (decode (sized 3) control = .ok [.call reference {}]) && + decide (siteClaim layouts none changed = .model) && + decide (decode layouts changed = liveHoleAt 0 changed.opcode) && + !runtimeCatalog.siteAgrees layouts .scalar changed replacement none true + +/-- Substituting a method parameter for the declared Int32 result refuses SizeOf. -/ +theorem forged_sizeOf_refused : + let reference := sizeOf int32 + refuses (site reference) (site reference { sizeOfDeclared with returnType := .parameter 0 }) + reference (sized 3) (.call reference {}) = true := by decide +kernel + +#print axioms forged_sizeOf_refused + +/-- At Boolean, a generic result closes to the correct type but refuses the AreSame call. -/ +theorem forged_areSame_refused : + let reference := areSame (runtimeType "System.Boolean") + let control := site reference unsafeComparisonDeclaration + let forged := { unsafeComparisonDeclaration with returnType := .parameter 0 } + refuses control (site reference forged) reference (sized 3) (.call reference {}) = true := by + decide +kernel + +#print axioms forged_areSame_refused + +/-- An absent layout or a different identity at the same index refuses a local load at SizeOf. -/ +theorem unsized_sizeOf_load_refused : + let reference := sizeOf packet + let missing := refuses (site reference) (site reference) reference + missing {} (.ldloc 0) = true ∧ + missing { sized 3 with layouts := #[] } (.ldloc 0) = true ∧ + missing (sized 3 "Fixture::Other") (.ldloc 0) = true := by + decide +kernel + +#print axioms unsized_sizeOf_load_refused + +/-- Removing the element's layout refuses a local store at SkipInit. -/ +theorem unsized_skipInit_store_refused : + refuses skipSite skipSite (skipInitOf packet) {} (.stloc 0) = true := by decide +kernel + +#print axioms unsized_skipInit_store_refused + +/-- A method that calls `Unsafe.SizeOf` under 24-byte layouts, with no link. -/ +private def sizeBody : CIL.Method := + { code := [.call (sizeOf packet) {}], locals := [], returnsValue := true, + environment := sized 3 } + +/-- The frame of a call with no locals and no owned objects. -/ +private def frame : Safety.Frame := { activation := 0, locals := [], owned := [] } + +-- The call steps as `ldc.i4` of the checked size. +example (memory : CIL.Safety.Memory) : + step sizeBody (.call (sizeOf packet) {}) 0 [] frame [] memory = + .ok (.next 1 [.scalar (.i32 24)] frame memory) := by + rw [step_call_model (model := .constant 24) (link := rfl) (found := by decide +kernel) + (direct := rfl)] + rfl + +cil_runtime_models Tests.UnsafeModels.sizeBody + +-- The model fact states the size as the numeral `ldc.i4 24` pushes, so a branch on the result +-- simplifies as a branch on that literal does. +/-- +info: Tests.UnsafeModels.sizeBody_model0 : + runtimeModel sizeBody.environment CallOpcode.call (sizeOf packet) = + some (RuntimeModel.constant 24) +-/ +#guard_msgs (whitespace := lax) in #check sizeBody_model0 + +/-- A method that calls `Unsafe.SkipInit`, with no link. -/ +private def skipInitBody : CIL.Method := + { code := [.call (skipInitOf int32) {}], locals := [], returnsValue := false } + +-- SkipInit pops a reference that forms and writes nothing; a scalar operand faults. +example (memory : CIL.Safety.Memory) : + step skipInitBody (.call (skipInitOf int32) {}) 0 [] frame [.reference .null] memory = + .ok (.next 1 [] frame memory) := by + rw [step_call_model (model := .skipInit) (link := rfl) (found := by decide +kernel) + (direct := rfl)] + rfl +example (memory : CIL.Safety.Memory) : + step skipInitBody (.call (skipInitOf int32) {}) 0 [] frame [.scalar (.i32 0)] memory = + .error (.unsupported "Not a memory instruction") := by + rw [step_call_model (model := .skipInit) (link := rfl) (found := by decide +kernel) + (direct := rfl)] + rfl + +/-- Every single-overload Unsafe entry checks the same raw fields as its exact member binding. -/ +theorem singleton_binding (key : MetadataName) (name result parameters : String) + (declared : OpenSignature) (resolve : CallLayouts → MethodRef → Option RuntimeModel) + (site : CallSite) : + let owner := "System.Runtime.CompilerServices.Unsafe" + (unsafeEntry key name result parameters declared resolve).bindsSite site = + (site.declared == declared && + (site.name == name && + site.target == .external + s!"{runtimeScope}::{result} {owner}::{name}({parameters})" + && site.ownerArgumentCount == 0 && site.typeArguments == site.method.methodArguments && + site.parameterCount == site.method.parameters.length && + site.returnsValue == (site.returnType != .void))) := by + simp [unsafeEntry, unsafeOperand, ModelEntry.single, ModelEntry.bindsSite, ModelEntry.select?, + ModelEntry.select?.select, ModelOperand.bindsSite, Bool.and_assoc] + +#print axioms singleton_binding + +/-- A synthetic overload returns its parameter count as a distinguishable execution result. -/ +private def countedOverload (parameters : List TypeExpr) (target : String) : ModelOverload := + { operand := .external (fun _ => { sizeOfDeclared with parameters }) "Count" + (fun _ => some target), + accepts := fun method => method.parameters == parameters, + resolve := fun _ _ => some (.constant (BitVec.ofNat 32 parameters.length)) } + +/-- A single reserved slot owns the zero- and one-parameter declarations. -/ +private def countedEntry : ModelEntry := + { opcode := .call, owner := unsafeOwner, name := name!"Count", ownsName := true, + overloads := [countedOverload [] "Count()", countedOverload [int32] "Count(Int32)"] } + +/-- The raw call to one of the two Count overloads. -/ +private def countedSite (parameters : List TypeExpr) (target : String) : CallSite := + let method := { sizeOf int32 with name := name!"Count", parameters } + { method, name := "Count", declared := { sizeOfDeclared with parameters }, + target := .external target, returnsValue := true, parameterCount := parameters.length, + typeArguments := method.methodArguments } + +/-- Both signatures resolve. Crossing a target, a declaration or both refuses the closed call. -/ +theorem overload_binding : + let catalog : ModelCatalog := ⟨model_table% [countedEntry]⟩ + let zero := countedSite [] "Count()" + let one := countedSite [int32] "Count(Int32)" + let raw := fun site => RawInstruction.mk 0 40 (.call site) + let needs := fun site => catalog.callRequirement (LayoutEnvironment.callLayouts {}) + .scalar none (raw site) + let unknown := countedSite [int32, int32] "Count(Int32,Int32)" + catalog.table.find {} .call zero.method = some (.constant 0) ∧ + catalog.table.find {} .call one.method = some (.constant 1) ∧ + needs zero = .instruction (.call zero.method {}) ∧ + needs one = .instruction (.call one.method {}) ∧ + needs { one with target := zero.target } = .hole 40 ∧ + needs { one with declared := zero.declared } = .hole 40 ∧ + needs { one with target := zero.target, declared := zero.declared } = .hole 40 ∧ + catalog.table.claims {} .call unknown.method = true ∧ needs unknown = .hole 40 := by + exact ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + +#print axioms overload_binding + +/-- When two open declarations accept the same closed type, neither can supply its semantics. -/ +theorem ambiguous_overload_refused : + let site := countedSite [] "Count()" + let entry := { countedEntry with overloads := + [countedOverload [] "Count()", countedOverload [] "Other()"] } + entry.bindsSite site = false ∧ entry.resolve (LayoutEnvironment.callLayouts {}) site.method = + none := by decide +kernel + +#print axioms ambiguous_overload_refused + +/-- Packing changes alignment independently of size. Raw storage survives a missing computed + layout, and another identity at the same index supplies neither storage nor alignment. -/ +theorem indexed_layout_reads : + let packed := [{ rawPacket "Fixture::Packet" 1 false with packing := 1 }] + let environment : LayoutEnvironment := + { rawLayouts := packed, layouts := (computeLayouts packed).getD #[] } + let reader := environment.callLayouts + let natural := (sized 1).callLayouts + let rawOnly := LayoutEnvironment.callLayouts { rawLayouts := packed } + reader.valueShape packet = .aggregate 8 ∧ natural.valueShape packet = .aggregate 8 ∧ + reader.alignment? packet = some 1 ∧ natural.alignment? packet = some 8 ∧ + rawOnly.storage packet = some (.field (.aggregate 0)) ∧ rawOnly.alignment? packet = none ∧ + reader.storage (.aggregate "Other" 0) = none ∧ + reader.alignment? (.aggregate "Other" 0) = none := by + decide +kernel + +#print axioms indexed_layout_reads + +end Tests.UnsafeModels diff --git a/Tests/UnsafeOffsets.lean b/Tests/UnsafeOffsets.lean new file mode 100644 index 000000000..bea3a2678 --- /dev/null +++ b/Tests/UnsafeOffsets.lean @@ -0,0 +1,333 @@ +import CIL.Extraction.DecodedMethod +import CIL.Safety.StepComposition +import Tests.Support.RawDecoding + +/-! Decoder evidence and checked execution of Unsafe reference offsets. -/ + +namespace Tests.UnsafeOffsets +open CIL CIL.Safety +section +open CIL.Extraction + +/-- The signed native result of the byte displacement call. -/ +private def native := TypeExpr.atom runtimeScope "System.IntPtr" + +/-- The closed reference whose arguments are two references to the same element. -/ +private def byteOffset (element : TypeExpr) : MethodRef := + { owner := unsafeOwner, name := name!"ByteOffset", hasThis := false, genericArity := 1, + methodArguments := [element], returnType := native, + parameters := [.byref element, .byref element] } + +/-- The genuine MemberRef over the open method parameter. -/ +private def byteSite (method : MethodRef) : CallSite := + { method, name := "ByteOffset", typeArguments := method.methodArguments, parameterCount := 2, + returnsValue := true, declared := + { convention := 0x10, returnType := native, + parameters := [.byref (.parameter 0), .byref (.parameter 0)] }, + target := .external (runtimeScope ++ "::System.IntPtr " ++ + "System.Runtime.CompilerServices.Unsafe::ByteOffset(!!0&,!!0&)") } + +/-- The single call in a live raw body. -/ +private def operand (site : CallSite) : RawInstruction := ⟨0, 40, .call site⟩ + +/-- The code the method decoder reconstructs at this call. -/ +private def decoded (site : CallSite) (environment : LayoutEnvironment := {}) := + let raw : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := native, parameters := [], locals := [], + initLocals := true, maxStack := 2, genericParameters := [], code := [operand site] } + (decodeRaw .scalar environment [] [true] raw []).map Method.code + +/-- The decoder keeps a well-formed ByteOffset and refuses either competing live emission. -/ +theorem byteOffset_emission : + let method := byteOffset native + let site := byteSite method + decoded site = .ok [.call method {}] ∧ + runtimeCatalog.siteAgrees {} .scalar (operand site) (.call method { tail := true }) none true = + false ∧ + runtimeCatalog.siteAgrees {} .scalar (operand site) .sub none true = false := by + decide +kernel + +#print axioms byteOffset_emission + +/-- Changing a single identity, signature or convention field yields the call opcode's hole. -/ +theorem byteOffset_bad_site : + let method := byteOffset native + let site := byteSite method + let invalid := [byteSite { method with owner := .apply unsafeOwner native }, + byteSite { method with owner := .atom "Fixture" "System.Runtime.CompilerServices.Unsafe" }, + byteSite { method with hasThis := true }, byteSite { method with genericArity := 2 }, + { site with target := .external "Fixture" }, + { site with declared.returnType := .parameter 0 }] + let conventions := [0x00, 0x11, 0x30, 0x50, 0x70].map fun convention => + { site with declared.convention := convention } + (invalid ++ conventions).all (fun changed => decide (decoded changed = liveHoleAt 0 40)) = + true := by decide +kernel + +#print axioms byteOffset_bad_site + +/-- Closed elements without a checked size decode to the call opcode's hole. -/ +theorem byteOffset_unsized : + let elements : List TypeExpr := [.apply (.constructor runtimeScope "System.Span`1") native, + .pointer native, .byref native, .array native, + .object runtimeScope "System.String", .atom runtimeScope "System.Single", + .aggregate "Fixture::Pair" 0] + elements.all (fun element => decide (decoded (byteSite (byteOffset element)) = + liveHoleAt 0 40)) = true := by decide +kernel + +#print axioms byteOffset_unsized + +/-- A free method parameter has no model and prevents the raw body from closing. -/ +theorem byteOffset_free_parameter : + let method := byteOffset (.parameter 0) + runtimeModel {} .call method = none ∧ decoded (byteSite method) = .error .instantiation := by + decide +kernel + +#print axioms byteOffset_free_parameter + +/-- ByteOffset accepts scalars, vectors and structs with checked storage. -/ +theorem byteOffset_sized : + let layout : RawLayout := + { identity := "Fixture::Pair", explicit := false, packing := 0, declaredSize := 0, + fields := [{ type := .unsigned64 }], byRefLike := false } + let environment : LayoutEnvironment := + { rawLayouts := [layout], layouts := (computeLayouts [layout]).getD #[] } + let elements : List TypeExpr := [native, .aggregate layout.identity 0, + .apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector128`1") + (.atom runtimeScope "System.Byte")] + elements.all (fun element => decide (decoded (byteSite (byteOffset element)) environment = + .ok [.call (byteOffset element) {}])) = true := by decide +kernel + +#print axioms byteOffset_sized + +/-- A retained ByteOffset runs the checked displacement model. -/ +theorem byteOffset_step (body : Method) (pc : Nat) (args stack : List Safety.Value) + (frame : Safety.Frame) (memory : Safety.Memory) (link : body.linkAt pc = none) : + step body (.call (byteOffset native) {}) pc args frame stack memory = + modelStep (some (.memory .byteOffset)) body pc frame stack memory := by + exact step_call_model body pc args frame memory link (by rfl) rfl stack + +#print axioms byteOffset_step + +/-- A stored runtime integer or native integer. -/ +private def atom (name : String) := TypeExpr.atom runtimeScope name + +/-- The runtime types of the signed 32-bit, signed native and unsigned native offsets. -/ +private def int := atom "System.Int32" +/-- The unsigned native offset. -/ +private def unsigned := atom "System.UIntPtr" + +/-- A checked aggregate at index zero, sized by the number of its 64-bit fields. -/ +private def environment (words : Nat) : LayoutEnvironment := + let raw : List RawLayout := [{ + identity := "Fixture::Packet", explicit := false, packing := 0, declaredSize := 0, + fields := List.replicate words { type := .unsigned64 } }] + { rawLayouts := raw, layouts := (computeLayouts raw).getD #[] } + +/-- One closed reference at a static arity-one helper. -/ +private def reference (name : MetadataName) (element result : TypeExpr) + (parameters : List TypeExpr) : MethodRef := + { owner := unsafeOwner, name, hasThis := false, genericArity := 1, + methodArguments := [element], parameters, returnType := result } + +/-- The closed reference and checked operation for each of the thirteen offset overloads. -/ +private def overloads (element : TypeExpr) (width : Nat) : List (MethodRef × RuntimeModel) := + let source := TypeExpr.byref element + let pointer := TypeExpr.pointer .void + let closed := fun name type offset => reference name element type [type, offset] + let elements : List (MethodRef × RuntimeModel) := [int, native, unsigned].flatMap fun offset => + let signed := offset != unsigned + [(closed name!"Add" source offset, .memory (.add width signed)), + (closed name!"Subtract" source offset, .memory (.add (subtractionStride width) signed))] + let bytes : List (MethodRef × RuntimeModel) := [native, unsigned].flatMap fun offset => + [(closed name!"AddByteOffset" source offset, .memory (.add 1 (offset != unsigned))), + (closed name!"SubtractByteOffset" source offset, .referenceSubtract)] + elements ++ bytes ++ [ + (closed name!"Add" pointer int, .memory (.add width true)), + (closed name!"Subtract" pointer int, .memory (.add (subtractionStride width) true)), + (reference name!"ByteOffset" element native [source, source], .memory .byteOffset)] + +/-- Each reference resolves to the operation of its checked width. -/ +private def resolves (layouts : LayoutEnvironment) (element : TypeExpr) (width : Nat) : Bool := + (overloads element width).all fun (method, model) => + runtimeModel layouts .call method == some model + +example : LayoutsValid (environment 1).rawLayouts (environment 1).layouts := by decide +kernel +example : LayoutsValid (environment 3).rawLayouts (environment 3).layouts := by decide +kernel +example : resolves {} int 4 := by decide +kernel +example : resolves {} + (.apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector256`1") int) 32 := by + decide +kernel +example : resolves (environment 1) (.aggregate "Fixture::Packet" 0) 8 := by decide +kernel +example : resolves (environment 3) (.aggregate "Fixture::Packet" 0) 24 := by decide +kernel + +/-- An offset helper refuses an unsupported element even when its result reads no element size. -/ +private def excludes (element : TypeExpr) : Bool := + (overloads element 0).all fun (method, _) => (runtimeModel {} .call method).isNone + +example : [TypeExpr.apply (.constructor runtimeScope "System.Span`1") int, .parameter 0, + .pointer int, .byref int, .object runtimeScope "System.String", .aggregate "Fixture::Packet" 0, + .array int, atom "System.Single", .void].all excludes := by decide +kernel + + +/-- The printed MemberRef name for the declared address or displacement type in this fixture. -/ +private def memberType : TypeExpr → String + | .byref (.parameter 0) => "!!0&" + | .pointer .void => "System.Void*" + | .atom _ name => name + | _ => "unsupported" + +/-- A MemberRef with the external target written by Cecil and its independently closed operand. -/ +private def memberSite (method declared : MethodRef) : CallSite := + let name := if method.name == name!"Add" then "Add" + else if method.name == name!"Subtract" then "Subtract" + else if method.name == name!"AddByteOffset" then "AddByteOffset" else "SubtractByteOffset" + let result := memberType declared.returnType + let parameters := String.intercalate "," (declared.parameters.map memberType) + let owner := "System.Runtime.CompilerServices.Unsafe" + { method, name, declared := + { convention := 0x10, returnType := declared.returnType, parameters := declared.parameters } + target := .external s!"{runtimeScope}::{result} {owner}::{name}({parameters})" + typeArguments := method.methodArguments, parameterCount := 2, returnsValue := true } + +/-- Each closed overload and its generic declaration occupy the same position in the fixture. -/ +private def offsetSites (element : TypeExpr) (width : Nat) : List CallSite := + ((overloads element width).zip (overloads (.parameter 0) width)).filterMap + fun ((method, _), (declared, _)) => + if method.name == name!"ByteOffset" then none else some (memberSite method declared) + +/-- All twelve declarations retain their exact closed call and reject competing emissions. -/ +theorem displacement_emissions : + (offsetSites int 4).all (fun site => + decide (decoded site = .ok [.call site.method {}]) && + !runtimeCatalog.siteAgrees {} .scalar (operand site) .sub none true && + !runtimeCatalog.siteAgrees {} .scalar (operand site) .add none true) = true := by + decide +kernel + +#print axioms displacement_emissions + +/-- An altered operand produces the call opcode’s hole and refuses its live call. -/ +private def refusesOffset (site : CallSite) : Bool := + decide (decoded site = liveHoleAt 0 40) && + !runtimeCatalog.siteAgrees {} .scalar (operand site) (.call site.method {}) none true + +/-- Forging the open signature, convention, identity or receiver invalidates a resolving site. -/ +theorem displacement_binding : + (offsetSites int 4).all (fun site => + let method := site.method + let closedAddress := method.returnType + let forged := if closedAddress == .pointer .void then + { site with declared.parameters := [closedAddress, .parameter 0] } + else { site with declared.returnType := closedAddress } + let changed := [forged, { site with method.owner := .apply unsafeOwner int }, + { site with method.owner := .atom "Fixture" "System.Runtime.CompilerServices.Unsafe" }, + { site with method.hasThis := true }, { site with method.genericArity := 2 }, + { site with target := .external "Fixture" }] + let conventions := [0x00, 0x11, 0x30, 0x50, 0x70].map fun convention => + { site with declared.convention := convention } + (changed ++ conventions).all refusesOffset) = true := by decide +kernel + +#print axioms displacement_binding + +/-- Substituting another genuine overload's operand for the selected overload produces a hole. -/ +theorem displacement_crossed_overloads : + [name!"Add", name!"Subtract", name!"AddByteOffset", name!"SubtractByteOffset"].all (fun name => + let family := (offsetSites int 4).filter fun site => site.method.name == name + match family with + | one :: other :: _ => + refusesOffset { one with target := other.target } && + refusesOffset { one with declared := other.declared } + | _ => false) = true := by decide +kernel + +#print axioms displacement_crossed_overloads + +/-- Replacing the element by a span leaves only the hole, including at an owned, linked call. -/ +theorem span_displacements_refused : + let span := TypeExpr.apply (.constructor runtimeScope "System.Span`1") int + (offsetSites span 0).all (fun site => refusesOffset site && + !runtimeCatalog.siteAgrees {} .scalar (operand site) (.call site.method {}) + (some (.direct 0)) true) = true := by decide +kernel + +#print axioms span_displacements_refused + +/-- An unsized closed argument yields a hole and an unbound method parameter fails closure. -/ +theorem unsized_displacement_code : + let excluded : List TypeExpr := [.pointer int, .byref int, .array int, .void, + .atom runtimeScope "System.Single", .object runtimeScope "System.String", + .aggregate "Fixture::Absent" 0] + excluded.all (fun element => (offsetSites element 0).all fun site => + refusesOffset site) = true ∧ + (offsetSites (.parameter 0) 0).all (fun site => + decide (decoded site = .error .instantiation)) = true := by + decide +kernel + +#print axioms unsized_displacement_code + +/-- An unknown Unsafe member can occupy a dead hole only and cannot retain a live call. -/ +theorem unknown_offset_name : + let control := byteSite (byteOffset native) + let unknown := { control with method.name := name!"UnknownOffset", name := "UnknownOffset" } + decoded control = .ok [.call control.method {}] ∧ + siteClaim {} none (operand unknown) = .refused ∧ + decoded unknown = liveHoleAt 0 40 ∧ + runtimeCatalog.siteAgrees {} .scalar (operand unknown) (.unadmitted 40) = true ∧ + runtimeCatalog.siteAgrees {} .scalar (operand unknown) (.call unknown.method {}) + none true = false := by decide +kernel + +#print axioms unknown_offset_name + +/-- A direct call at pc zero with empty links. -/ +private def body : CIL.Method := { code := [], locals := [], returnsValue := true } + +/-- Each memory overload executes its checked offset operation on the caller's stack. -/ +theorem memoryOverloadStep (method : MethodRef) (op : MemoryOp) + (member : (method, RuntimeModel.memory op) ∈ overloads int 4) (frame : Safety.Frame) + (stack : List CIL.Safety.Value) (memory : Safety.Memory) : + step body (.call method {}) 0 [] frame stack memory = + modelStep (some (.memory op)) body 0 frame stack memory := by + apply step_call_model (link := rfl) (direct := rfl) + exact beq_iff_eq.mp (List.all_eq_true.mp (show resolves {} int 4 from by decide +kernel) + (method, RuntimeModel.memory op) member) + +#print axioms memoryOverloadStep + +/-- SubtractByteOffset takes a native displacement under both signedness declarations. -/ +private def byteSubtract (offset : TypeExpr) := + reference name!"SubtractByteOffset" int (.byref int) [.byref int, offset] + +example (frame : Safety.Frame) (bits : W64) (base : ManagedReference) (memory : Safety.Memory) + (rest : List CIL.Safety.Value) : + let stack := .scalar (.i64 bits) :: .reference base :: rest + step body (.call (byteSubtract native) {}) 0 [] frame stack memory = + step body .sub 0 [] frame stack memory := by + dsimp only + rw [step_call_model (model := .referenceSubtract) (link := rfl) (found := by decide +kernel) + (direct := rfl)] + rfl + +example (frame : Safety.Frame) (bits : W64) (base : ManagedReference) (memory : Safety.Memory) + (rest : List CIL.Safety.Value) : + let stack := .scalar (.i64 bits) :: .readOnlyReference base :: rest + step body (.call (byteSubtract unsigned) {}) 0 [] frame stack memory = + step body .sub 0 [] frame stack memory := by + dsimp only + rw [step_call_model (model := .referenceSubtract) (link := rfl) (found := by decide +kernel) + (direct := rfl)] + rfl + +end + +/-- Signed and unsigned native offsets both arrive as 64-bit words. The model and CIL sub + move the same reference and preserve its readonly restriction for every word. -/ +theorem subtractByteOffset_step (body : Method) + (pc : Nat) (args : List Safety.Value) (frame : Frame) (rest : List Safety.Value) + (memory : Safety.Memory) (base : ManagedReference) (offset : W64) (readOnly : Bool) : + let reference := if readOnly then Safety.Value.readOnlyReference base else .reference base + let stack := .scalar (.i64 offset) :: reference :: rest + modelStep (some .referenceSubtract) body pc frame stack memory = + step body .sub pc args frame stack memory := by + cases readOnly <;> rfl + +#print axioms subtractByteOffset_step + +end Tests.UnsafeOffsets diff --git a/Tests/UnsafeReferences.cs b/Tests/UnsafeReferences.cs index ffeaeba84..dc5905d9b 100644 --- a/Tests/UnsafeReferences.cs +++ b/Tests/UnsafeReferences.cs @@ -3,6 +3,11 @@ #:property AllowUnsafeBlocks=true #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs @@ -28,6 +33,17 @@ void Prove(string name, string selector, string claim) var proof = Path.Combine(output, "Proof.lean"); File.WriteAllText(proof, "import Extracted\nimport Tests.Support\nnamespace ExampleProof\n" + memory + "\ntheorem correctness : " + claim + " := by rfl\n#print axioms correctness\nend ExampleProof\n"); + if (name is "Word" or "Half" or "Vector" or "Mutable" or "Pointer") + File.AppendAllText(proof, """ + + /-- The compiled reinterpretation keeps its call and resolves to reference formation. -/ + theorem ExampleProof.cast_call : + (Extracted.method0.code[1]?).bind (fun + | .call method _ => CIL.Extraction.runtimeModel Extracted.environment .call method + | _ => none) = some (.memory .asRef) := by decide +kernel + #print axioms ExampleProof.cast_call + + """); queue.Add(new(selector, proof, output), () => Console.WriteLine("PASS checked byref reinterpretation: " + name)); } @@ -208,9 +224,10 @@ end ExampleProof var controls = new GeneratedEvidence(Path.Combine(root, "OffsetControls")); foreach (var (name, theorem) in new[] { - ("AddPointer", "method0_unsafe_validated"), ("Store", "method0_unsafe_validated"), - ("AddPointerAggregate", "method0_layouts_validated"), - ("SubtractPointer", "method0_unsafe_validated"), ("SubtractPointerAggregate", "method0_layouts_validated") + ("ByteOffset", "method0_models_validated"), + ("AddPointer", "method0_models_validated"), ("Store", "method0_models_validated"), + ("AddPointerAggregate", "method0_models_validated"), + ("SubtractPointer", "method0_models_validated"), ("SubtractPointerAggregate", "method0_models_validated") }) { var path = Path.Combine(root, name, "OffsetControl.lean"); @@ -220,86 +237,54 @@ end ExampleProof } controls.Check(replay: false); var rejected = new RejectedEvidence(Path.Combine(root, "EvidenceRejections")); -foreach (var (name, before, after) in new[] +const string wrongWidth = "methodArguments := [.atom Extraction.runtimeScope \"System.UInt32\"]"; +const string wordWidth = "methodArguments := [.atom Extraction.runtimeScope \"System.UInt64\"]"; +const string signedOffset = "parameters := method.parameters.set 1 (.atom Extraction.runtimeScope \"System.IntPtr\")"; +const string unsignedOffset = "parameters := method.parameters.set 1 (.atom Extraction.runtimeScope \"System.UIntPtr\")"; +foreach (var (name, pc, fields) in new[] { - ("AreSame", ".eq", ".ltu"), - ("ByteOffset", ".memory .byteOffset", ".sub"), - ("AddSigned", ".memory (.add 1 true)", ".memory (.add 8 true)"), - ("AddUnsigned", ".memory (.add 1 false)", ".memory (.add 1 true)"), - ("Subtract", ".sub", ".add"), - ("SubtractUnsigned", ".sub", ".add"), - ("AddPointer", ".memory (.add 8 true)", ".memory (.add 4 true)"), - ("SubtractPointer", ".memory (.add 18446744073709551608 true)", ".memory (.add 8 true)"), - ("Store", ".memory (.add 8 true)", ".memory (.add 8 false)"), - ("SubtractElements32", ".memory (.add 18446744073709551608 true)", ".memory (.add 8 true)"), - ("SubtractElementsUnsigned", ".memory (.add 18446744073709551608 false)", - ".memory (.add 18446744073709551608 true)"), - ("SubtractVector", ".memory (.add 18446744073709551600 false)", ".memory (.add 16 false)") + ("AddSigned", 2, unsignedOffset), ("AddUnsigned", 2, signedOffset), + ("Subtract", 2, "name := name!\"AddByteOffset\""), ("SubtractUnsigned", 2, signedOffset), + ("AddPointer", 2, wrongWidth), ("SubtractPointer", 2, "name := name!\"Add\""), + ("Store", 7, "name := name!\"Subtract\""), + ("SubtractElements32", 2, signedOffset), ("SubtractElementsUnsigned", 2, "name := name!\"Add\""), + ("SubtractVector", 2, signedOffset), ("SubtractAggregate", 2, wordWidth), + ("AddPointerAggregate", 2, wordWidth), ("SubtractPointerAggregate", 2, wordWidth) }) { var source = File.ReadAllText(Path.Combine(root, name, "Extracted.lean")); - var narrowed = NarrowEvidence(source, "method0_unsafe_validated"); - if (!narrowed.Contains(before, StringComparison.Ordinal)) - throw new InvalidOperationException("Missing mutation target: " + name); var path = Path.Combine(root, name, "WrongOperation.lean"); - File.WriteAllText(path, narrowed.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS exact Unsafe operation rejection: " + name, "UnsafeCallsValid"); + File.WriteAllText(path, ForgeModelEmission(source, pc, ChangedCallOperand(pc, fields))); + rejected.Add(path, $"PASS offset call emission rejection: {name}", "RuntimeModelsAgree"); } -var aggregateSource = File.ReadAllText(Path.Combine(root, "SubtractAggregate", "Extracted.lean")); -var aggregateEvidence = NarrowEvidence(aggregateSource, "method0_layouts_validated"); -const string aggregateStride = ".memory (.add 18446744073709551584 true)"; -if (!aggregateEvidence.Contains(aggregateStride, StringComparison.Ordinal)) - throw new InvalidOperationException("Missing aggregate subtraction stride"); -var aggregateForgery = Path.Combine(root, "SubtractAggregate", "WrongStride.lean"); -File.WriteAllText(aggregateForgery, aggregateEvidence.Replace(aggregateStride, - ".memory (.add 32 true)", StringComparison.Ordinal)); -rejected.Add(aggregateForgery, "PASS exact aggregate subtraction layout", "MethodLayoutsValid"); -foreach (var (methodName, stride) in new[] -{ - ("AddPointer", ".memory (.add 8 true)"), - ("SubtractPointer", ".memory (.add 18446744073709551608 true)") -}) +foreach (var methodName in new[] { "AddPointer", "SubtractPointer" }) { var pointerSource = NarrowEvidence(File.ReadAllText(Path.Combine(root, methodName, "Extracted.lean")), - "method0_unsafe_validated"); + "method0_models_validated"); foreach (var (name, before, after) in new[] { - ("signedness", stride, stride.Replace("true", "false", StringComparison.Ordinal)), ("offset-type", ".atom Extraction.runtimeScope \"System.Int32\"", ".atom Extraction.runtimeScope \"System.UInt32\""), ("return", "returnType := .pointer (.void)", "returnType := .byref (.atom Extraction.runtimeScope \"System.UInt64\")"), ("identity", "target := .external externalIdentity0", "target := .external \"forged\""), - ("scope", "declaringType := .atom Extraction.runtimeScope", "declaringType := .atom \"forged\"") + ("scope", "owner := .atom Extraction.runtimeScope", "owner := .atom \"forged\"") }) { if (!pointerSource.Contains(before, StringComparison.Ordinal)) throw new InvalidOperationException("Missing pointer offset mutation: " + name); var path = Path.Combine(root, methodName, "Wrong-" + name + ".lean"); File.WriteAllText(path, pointerSource.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS exact pointer offset rejection: " + methodName + "/" + name, "UnsafeCallsValid"); + rejected.Add(path, "PASS exact pointer offset rejection: " + methodName + "/" + name, "RuntimeModelsAgree"); } } -foreach (var (name, stride, wrongStride) in new[] -{ - ("AddPointerAggregate", ".memory (.add 32 true)", ".memory (.add 8 true)"), - ("SubtractPointerAggregate", ".memory (.add 18446744073709551584 true)", - ".memory (.add 18446744073709551608 true)") -}) -{ - var pointerLayout = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_layouts_validated"); - if (!pointerLayout.Contains(stride, StringComparison.Ordinal)) - throw new InvalidOperationException("Missing aggregate pointer stride"); - var pointerForgery = Path.Combine(root, name, "WrongStride.lean"); - File.WriteAllText(pointerForgery, pointerLayout.Replace(stride, wrongStride, StringComparison.Ordinal)); - rejected.Add(pointerForgery, "PASS exact aggregate pointer offset layout: " + name, "MethodLayoutsValid"); -} var byteOffsetSource = NarrowEvidence(File.ReadAllText(Path.Combine(root, "ByteOffset", "Extracted.lean")), - "method0_unsafe_validated"); + "method0_models_validated"); foreach (var (name, before, after) in new[] { - ("scope", "declaringType := .atom Extraction.runtimeScope", "declaringType := .atom \"forged\""), + ("operation", "method0Reachable method0 := by decide", + "method0Reachable { method0 with code := method0.code.set 2 .sub } := by decide"), + ("scope", "owner := .atom Extraction.runtimeScope", "owner := .atom \"forged\""), ("identity", "target := .external externalIdentity0", "target := .external \"forged\""), ("result", "returnType := .atom Extraction.runtimeScope \"System.IntPtr\"", "returnType := .atom Extraction.runtimeScope \"System.UIntPtr\""), @@ -313,16 +298,8 @@ end ExampleProof throw new InvalidOperationException("Missing identity mutation target: " + name); var path = Path.Combine(root, "ByteOffset", "Wrong-" + name + ".lean"); File.WriteAllText(path, byteOffsetSource.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS exact Unsafe identity rejection: " + name, "UnsafeCallsValid"); + rejected.Add(path, $"PASS exact ByteOffset evidence rejection: {name}", "RuntimeModelsAgree"); } -var renamedSource = NarrowEvidence(File.ReadAllText(Path.Combine(root, "AreSame", "Extracted.lean")), - "method0_scalars_validated") - .Replace("name := \"AreSame\"", "name := \"Other\"", StringComparison.Ordinal) - .Replace("declaringType := .atom Extraction.runtimeScope \"System.Runtime.CompilerServices.Unsafe\"", - "declaringType := .atom \"Fixture\" \"Lookalike\"", StringComparison.Ordinal); -var renamedPath = Path.Combine(root, "AreSame", "ForgedNameAndOwner.lean"); -File.WriteAllText(renamedPath, renamedSource); -rejected.Add(renamedPath, "PASS forged name and owner cannot emit a scalar comparison", "ScalarFlowValid"); rejected.Check(); foreach (var mutation in new[] { "reference-field", "class", "pointer", "arity", "parameter", "return", "instance", "scope", "version" }) diff --git a/Tests/UnsafeReinterpretations.lean b/Tests/UnsafeReinterpretations.lean new file mode 100644 index 000000000..105698163 --- /dev/null +++ b/Tests/UnsafeReinterpretations.lean @@ -0,0 +1,344 @@ +import CIL.Extraction.RuntimeModels +import CIL.Extraction.DecodedMethod +import CIL.Safety.StepComposition +import Tests.Support.RawDecoding + +/-! # Runtime model admission and execution of Unsafe reinterpretations + +The fixtures exercise reference formation and each representation that BitCast can move. +-/ + +namespace Tests.UnsafeReinterpretations +open CIL CIL.Extraction CIL.Safety + +/-- The primitive byte type used by the scalar casts. -/ +private def byteType := TypeExpr.atom runtimeScope "System.Byte" + +/-- The Boolean result of the scalar BitCast overload. -/ +private def boolean := TypeExpr.atom runtimeScope "System.Boolean" + +/-- The example packet occupies layout slot zero. -/ +private def packet := TypeExpr.aggregate "Test::Packet" 0 + +/-- The packet has one unsigned 64-bit field per word. -/ +private def environment (words : Nat) : LayoutEnvironment := + let layout : RawLayout := + { identity := "Test::Packet", explicit := false, packing := 0, declaredSize := 0, + fields := List.replicate words { type := .unsigned64 } } + let raw := [layout] + { rawLayouts := raw, layouts := (computeLayouts raw).getD #[] } + +/-- An intrinsic vector over the given runtime element type. -/ +private def vector (width : Nat) (element := "System.Byte") : TypeExpr := + .apply (.constructor intrinsicsScope s!"System.Runtime.Intrinsics.Vector{width}`1") + (.atom runtimeScope element) + +/-- A closed static call together with the declaration and external identity of its member. -/ +private def site (name : MetadataName) (text signature : String) (args : List TypeExpr) + (parameters : List TypeExpr) (result : TypeExpr) (declared : OpenSignature) : CallSite := + let method : MethodRef := + { owner := unsafeOwner, name, hasThis := false, genericArity := args.length, + parameters, returnType := result, methodArguments := args } + { method, name := text, declared, target := .external (runtimeScope ++ "::" ++ signature), + typeArguments := args, parameterCount := parameters.length, returnsValue := true } + +/-- The open byref-to-byref cast closed over source and target. -/ +private def asSite (source target : TypeExpr) := + site name!"As" "As" "!!1& System.Runtime.CompilerServices.Unsafe::As(!!0&)" + [source, target] [.byref source] (.byref target) + { convention := 16, parameters := [.byref (.parameter 0)], + returnType := .byref (.parameter 1) } + +/-- The byref and void-pointer overloads of AsRef. -/ +private def asRefSite (element : TypeExpr) (pointer : Bool) := + let parameter := fun arg => if pointer then TypeExpr.pointer .void else .byref arg + let operand := if pointer then "System.Void*" else "!!0&" + site name!"AsRef" "AsRef" s!"!!0& System.Runtime.CompilerServices.Unsafe::AsRef({operand})" + [element] [parameter element] (.byref element) + (unsafeDeclaration (fun arg => [parameter arg]) TypeExpr.byref) + +/-- The generic null reference and its Boolean observer. -/ +private def nullSite (element : TypeExpr) (test : Bool) := + if test then + site name!"IsNullRef" "IsNullRef" "System.Boolean System.Runtime.CompilerServices.Unsafe::\ + IsNullRef(!!0&)" [element] [.byref element] boolean + (unsafeDeclaration (fun arg => [.byref arg]) (fun _ => boolean)) + else + site name!"NullRef" "NullRef" "!!0& System.Runtime.CompilerServices.Unsafe::NullRef()" + [element] [] (.byref element) (unsafeDeclaration (fun _ => []) TypeExpr.byref) + +/-- The closed value cast from source to target. -/ +private def cast (source target : TypeExpr) := + site name!"BitCast" "BitCast" "!!1 System.Runtime.CompilerServices.Unsafe::BitCast(!!0)" + [source, target] [source] target + { convention := 16, parameters := [.parameter 0], returnType := .parameter 1 } + +/-- A span over an element; As refuses it as either type argument, as it refuses an array or + class. -/ +private def span (element : TypeExpr) : TypeExpr := + .apply (.constructor runtimeScope "System.Span`1") element + +/-- The runtime model selected for a call with the supplied layouts. -/ +private def model (call : CallSite) (env := environment 2) := + runtimeModel env .call call.method + +/-- A one-instruction method whose call site is live. -/ +private def caller (call : CallSite) : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := true, maxStack := 1, genericParameters := [], code := [⟨0, 40, .call call⟩] } + +/-- The runtime-site check applies to a live call. -/ +private def accepts (call : CallSite) (emitted : Instruction) : Bool := + runtimeCatalog.siteAgrees (environment 2) .scalar ⟨0, 40, .call call⟩ emitted none true + +/-- One live call as the public raw-method decoder emits it. -/ +private def decoded (call : CallSite) (env := environment 2) := + (decodeRaw .scalar env [] [true] (caller call) []).map Method.code + +/-- A single mutation of a resolving operand leaves precisely its opcode's hole. -/ +private def refuses (control changed : CallSite) : Bool := + decide (decoded control = .ok [.call control.method {}]) && + decide (decoded changed = liveHoleAt 0 40) + +/-- Every pair of snapshot classes and the scalar overload, in a 16-byte layout. -/ +private def values : List (CallSite × MemoryOp) := + [(cast byteType boolean, .bitcastByteBool), + (cast packet packet, .bitcastValue (.aggregate 16) (.aggregate 16)), + (cast (vector 128) (vector 128), .bitcastValue .vector128 .vector128), + (cast packet (vector 128), .bitcastValue (.aggregate 16) .vector128), + (cast (vector 128) packet, .bitcastValue .vector128 (.aggregate 16))] + +/-- Scalar, vector and struct references, one for each storage As and AsRef admit, and the null + helpers at every storage they admit. -/ +private def references : List (CallSite × MemoryOp) := + [byteType, vector 128, packet].map (fun element => (asSite element byteType, .asRef)) ++ + [false, true].flatMap (fun pointer => [byteType, vector 128, packet].map + (fun element => (asRefSite element pointer, .asRef))) ++ + [false, true].flatMap (fun test => [byteType, vector 256, packet, span byteType, + .array byteType, .object "Test" "Class"].map + (fun element => (nullSite element test, if test then .isNullRef else .nullRef))) + +/-- Every admitted call runs its expected operation and keeps its operand at the live site. -/ +theorem resolved : (references ++ values).all (fun (call, operation) => + model call == some (.memory operation) && accepts call (.call call.method {}) && + !accepts call (.call call.method { tail := true }) && !accepts call .nop) := by decide +kernel +#print axioms resolved + +/-- A span, array or class on either side of As has no field or vector storage, so the live site + decodes to the call hole, and a span over a type variable resolves no model. -/ +theorem unsizedRefused : [span byteType, .apply (.constructor runtimeScope "System.ReadOnlySpan`1") + byteType, .array byteType, .object "Test" "Class"].all (fun arg => + refuses (asSite byteType byteType) (asSite byteType arg) && + refuses (asSite byteType byteType) (asSite arg byteType)) && + (model (asSite byteType (span (.parameter 0)))).isNone := by decide +kernel +#print axioms unsizedRefused + +/-- Both layouts compute successfully; BitCast reads their different snapshot sizes. -/ +theorem checkedSizes : LayoutsValid (environment 2).rawLayouts (environment 2).layouts ∧ + LayoutsValid (environment 4).rawLayouts (environment 4).layouts ∧ + model (cast packet packet) (environment 4) = + some (.memory (.bitcastValue (.aggregate 32) (.aggregate 32))) := by decide +kernel +#print axioms checkedSizes + +/-- A root byref, pointer or variable excludes an As argument on either side. -/ +theorem asExclusions : [TypeExpr.byref byteType, .pointer byteType, .parameter 0, + .ownerParameter 0].all (fun arg => (model (asSite arg byteType)).isNone && + (model (asSite byteType arg)).isNone && + !accepts (asSite arg byteType) (.call (asSite arg byteType).method) && + !accepts (asSite byteType arg) (.call (asSite byteType arg).method)) := by decide +kernel +#print axioms asExclusions + +/-- BitCast requires checked equal-width snapshots in both directions, or Byte to Boolean. -/ +theorem bitCastExclusions : + [vector 256, .aggregate "Other" 0, span byteType, .parameter 0, .pointer byteType, + .object "Test" "Class", .array byteType, byteType].all (fun arg => + (model (cast arg packet)).isNone && (model (cast packet arg)).isNone) && + (model (cast boolean byteType)).isNone && + [("System.Single", 2, 128), ("System.Double", 4, 256), + ("System.Double", 2, 128), ("System.Single", 4, 256)].all (fun (element, words, width) => + let arg := vector width element + (model (cast arg packet) (environment words)).isNone && + (model (cast packet arg) (environment words)).isNone) := by decide +kernel +#print axioms bitCastExclusions + +/-- The closed AsRef parameter picks both the open declaration and its exact external target. -/ +theorem asRefStorage : [false, true].all (fun pointer => + let control := asRefSite byteType pointer + let other := asRefSite byteType (!pointer) + refuses control { control with declared := other.declared } && + refuses control { control with target := other.target } && + refuses control { control with declared := other.declared, target := other.target } && + [span byteType, TypeExpr.array byteType, .object "Test" "Class", .byref byteType, + .aggregate "Other" 0].all (fun arg => refuses control (asRefSite arg pointer))) := by + decide +kernel +#print axioms asRefStorage + +/-- The void-pointer overload consumes a reference in the scalar flow certificate. -/ +theorem asRefPointerStack : + let call := asRefSite byteType true + let transfer := scalarTransfer (caller call) ⟨0, 40, .call call⟩ + transfer [.reference] = some [.reference] ∧ transfer [.other] = none := by decide +kernel +#print axioms asRefPointerStack + +/-- Missing computed size preserves a field's raw storage identity for all four overloads. -/ +theorem indexedStorage : + let env := { environment 2 with layouts := #[] } + [asRefSite packet false, asRefSite packet true, + nullSite packet false, nullSite packet true].all + (fun call => decide (decoded call env = .ok [.call call.method {}])) := by decide +kernel +#print axioms indexedStorage + +/-- Recognized reference storage and unsupported elements both refuse the two null helpers. -/ +theorem nullStorage : [false, true].all (fun test => + [TypeExpr.byref byteType, .pointer byteType, .void, .aggregate "Other" 0].all + (fun arg => refuses (nullSite byteType test) (nullSite arg test))) := by decide +kernel +#print axioms nullStorage + +/-- Each closed type refusal differs from a kept reference or snapshot operand in one argument. -/ +theorem excludedOperands : + [TypeExpr.byref byteType, .pointer byteType].all (fun arg => + refuses (asSite byteType byteType) (asSite byteType arg) && + refuses (asSite byteType byteType) (asSite arg byteType)) && + [vector 256, vector 128 "System.Single", vector 128 "System.Double", + .aggregate "Other" 0, span byteType, .pointer byteType, .object "Test" "Class", + .array byteType, byteType].all (fun arg => + refuses (cast (vector 128) packet) (cast arg packet) && + refuses (cast packet (vector 128)) (cast packet arg)) && + refuses (cast byteType boolean) (cast byteType byteType) := by decide +kernel +#print axioms excludedOperands + +/-- Closed operands for mutations that run through the method decoder. -/ +private def controls := [(asSite byteType packet, MemoryOp.asRef), (cast byteType boolean, + MemoryOp.bitcastByteBool), (cast (vector 128) packet, .bitcastValue .vector128 (.aggregate 16)), + (asRefSite packet false, .asRef), (asRefSite packet true, .asRef), + (nullSite byteType false, .nullRef), (nullSite byteType true, .isNullRef)] + +/-- The owner, receiver, generic arity and closed types identify each overload. -/ +theorem exactReference : controls.all (fun (call, _) => + let method := call.method + [ { method with owner := .apply unsafeOwner byteType }, + { method with owner := .atom "Other" "System.Runtime.CompilerServices.Unsafe" }, + { method with hasThis := true }, { method with genericArity := method.genericArity + 1 }, + { method with parameters := method.parameters ++ [byteType] }, + { method with returnType := .void } ].all + (fun other => (runtimeModel (environment 2) .call other).isNone && + refuses call { call with method := other })) && + refuses (nullSite byteType true) { (nullSite byteType true) with + method.returnType := .atom "Other" "System.Boolean" } && + (runtimeModel {} .call { (asSite byteType packet).method with + genericArity := 1, methodArguments := [byteType], parameters := [.object runtimeScope + "System.Object"], returnType := byteType }).isNone := by decide +kernel +#print axioms exactReference + +/-- A forged open return type or convention rejects both the original call and a tail-prefixed call. -/ +theorem exactDeclaration : controls.all (fun (call, _) => + let altered := { call with declared.returnType := .void } + refuses call altered && + refuses call { call with target := .external "Other" } && + refuses call { call with parameterCount := call.parameterCount + 1 } && + refuses call { call with returnsValue := false } && + refuses call { call with typeArguments := [] } && + refuses call { call with name := "Other" } && + refuses call { call with declared.parameters := [.ownerParameter 0] } && + !accepts altered (.call call.method {}) && + !accepts altered (.call call.method { tail := true }) && + [0x00, 0x11, 0x30, 0x50, 0x70].all (fun convention => + refuses call { call with declared.convention := convention })) := by + decide +kernel +#print axioms exactDeclaration + +/-- A failed layout or unknown member has only a hole, which cannot certify a live site. -/ +theorem unadmitted : + let control := cast (vector 128) packet + let unknown := { control with name := "Unknown", method.name := name!"Unknown" } + [(control, cast (vector 256) packet), (asSite byteType byteType, asSite (.pointer byteType) + byteType), (control, unknown)].all (fun (original, call) => + refuses original call && + runtimeCatalog.siteAgrees (environment 2) .scalar ⟨0, 40, .call call⟩ (.unadmitted 40) && + !accepts call (.unadmitted 40) && !accepts call .nop) := by decide +kernel +#print axioms unadmitted + +/-- On every stack and memory these calls step according to their bound runtime model. -/ +theorem callSteps (call : CallSite) (operation : MemoryOp) + (listed : (call, operation) ∈ references ++ values) (pc : Nat) + (args stack : List CIL.Safety.Value) + (frame : CIL.Safety.Frame) (memory : CIL.Safety.Memory) : + let body : Method := + { code := [], locals := [], returnsValue := true, environment := environment 2 } + step body (.call call.method {}) pc args frame stack memory = + modelStep (some (.memory operation)) body pc frame stack memory := by + have found : (references ++ values).all + (fun pair => model pair.1 == some (.memory pair.2)) := by decide +kernel + have modelAt := List.all_eq_true.mp found _ listed + dsimp only + apply step_call_model <;> first | rfl | exact eq_of_beq modelAt + +#print axioms callSteps + +/-- Eight readable and writable caller bytes, all zero. -/ +private def callerMemory : CIL.Safety.Memory where + allocations := fun id => if id = 0 then some ⟨.callerStack, ⟨8, 1, []⟩, true, [8]⟩ else none + cells := fun _ _ => ⟨0, true⟩ + views := [⟨0, 0, 8, true, true⟩] + nextIdentity := 1 + +/-- Loads a span through its by-reference argument and returns it. -/ +private def spanLoader : Method := + { code := [.ldarg 0, .ldobj (span byteType), .ret], locals := [], returnsValue := true, + environment := environment 2 } + +/-- The span load returns a view in place. An address, such as an As cast to a span type forms, + is invalid there, and a null reference, such as NullRef forms, faults as a null dereference. -/ +theorem spanLoads : + let view : SpanView := ⟨.address ⟨0, 0⟩, 8, 1, true⟩ + let run := fun argument => (invoke [spanLoader] 4 0 [argument] callerMemory).map Prod.snd + run (.bufferSpan view) = .ok [.bufferSpan view] ∧ + run (.reference (.address ⟨0, 0⟩)) = .error ⟨0, 1, .invalidState⟩ ∧ + run (.readOnlyReference (.address ⟨0, 0⟩)) = .error ⟨0, 1, .invalidState⟩ ∧ + run (.reference .null) = .error ⟨0, 1, .memory .nullDereference .null 0⟩ ∧ + run (.readOnlyReference .null) = .error ⟨0, 1, .memory .nullDereference .null 0⟩ := by + exact ⟨rfl, rfl, rfl, rfl, rfl⟩ +#print axioms spanLoads + +/-- Whether a step or memory operation stops with a fault rather than returning. -/ +private def faults {α : Type} : ExecutionResult α → Bool + | .error _ => true + | .ok _ => false + +/-- The receivers and operands NullRef and a plain address give a span or object type. -/ +private def nullOrPlain : List CIL.Safety.Value := + [.reference .null, .readOnlyReference .null, .reference (.address ⟨0, 0⟩)] + +/-- Every span member that takes its receiver by reference faults on a null or plain reference, + so NullRef of a span type never yields a span value. -/ +theorem spanReceiversFault : [(MemoryOp.spanLength, ([] : List CIL.Safety.Value)), + (.spanItem 1, [.scalar (.i32 0)]), (.spanSlice false, [.scalar (.i32 0)]), + (.spanSlice true, [.scalar (.i32 0), .scalar (.i32 0)]), (.observeView .spanEmpty, []), + (.observeView .spanPinnable, []), (.spanReadOnly, []), (.spanReference, []), + (.spanCopy 1 false, [.bufferSpan ⟨.address ⟨0, 0⟩, 8, 1, true⟩]), + (.spanCopy 1 true, [.bufferSpan ⟨.address ⟨0, 0⟩, 8, 1, true⟩]), + (.spanFill 1 false, [.scalar (.i32 0)]), (.spanFill 1 true, [])].all (fun (operation, above) => + nullOrPlain.all fun receiver => + faults (memoryInstruction operation (above ++ [receiver]) callerMemory)) := by + decide +kernel +#print axioms spanReceiversFault + +/-- Object instructions fault on a null or plain reference to span, array or class storage, and + a native-integer load, which reads a method pointer, faults on a null reference, so a reference + NullRef forms for them is never read as a value. -/ +theorem objectOperandsFault : + let body : Method := + { code := [], locals := [], returnsValue := true, environment := environment 2 } + let frame : CIL.Safety.Frame := { activation := 0, locals := [], owned := [] } + [span byteType, .array byteType, .object "Test" "Class"].all (fun type => + nullOrPlain.all fun target => + [(Instruction.ldobj type, [target]), (.stobj type, [.scalar (.i32 0), target]), + (.cpobj type, [target, target]), (.initobj type, [target])].all fun (op, stack) => + faults (step body op 0 [] frame stack callerMemory)) ∧ + [CIL.Safety.Value.reference .null, .readOnlyReference .null].all (fun target => + faults (instruction (.ldind .i) [target] callerMemory)) := by + decide +kernel +#print axioms objectOperandsFault + +end Tests.UnsafeReinterpretations diff --git a/Tests/UnsafeTransferModels.lean b/Tests/UnsafeTransferModels.lean new file mode 100644 index 000000000..618fa0199 --- /dev/null +++ b/Tests/UnsafeTransferModels.lean @@ -0,0 +1,307 @@ +import CIL.Extraction.DecodedMethod +import CIL.Safety.StepComposition +import Tests.Support +import Tests.Support.RawDecoding + +/-! +# Checked Unsafe transfer calls + +The entry for each overload binds its element, address and declaration at a live site. +-/ + +namespace Tests.UnsafeTransferModels +open CIL CIL.Extraction CIL.Safety + +/-- The open pointer overload and its external target at an Unsafe call. -/ +private def site (name : MetadataName) (store : Bool) (element : TypeExpr) + (byref : Bool) (convention : Nat) : RawInstruction := + let address := if byref then TypeExpr.byref (.atom runtimeScope "System.Byte") + else .pointer .void + let addressName := if byref then "System.Byte&" else "System.Void*" + let result := fun type => if store then TypeExpr.void else type + let parameters := fun type => if store then [address, type] else [address] + let method : MethodRef := + { owner := unsafeOwner, name, hasThis := false, genericArity := 1, + returnType := result element, parameters := parameters element, + methodArguments := [element] } + let text := name.text?.getD "" + let target := runtimeScope ++ (if store then "::System.Void " else "::!!0 ") ++ + "System.Runtime.CompilerServices.Unsafe::" ++ text ++ + "(" ++ addressName ++ (if store then ",!!0)" else ")") + let declared : OpenSignature := + { convention, returnType := result (.parameter 0), parameters := parameters (.parameter 0) } + let call : CallSite := + { method, declared, name := text, target := .external target, typeArguments := [element], + parameterCount := method.parameters.length, returnsValue := !store } + ⟨0, 40, .call call⟩ + +/-- The decoder's instruction at one reachable, unlinked raw site, or the stage that refuses it. -/ +private def decoded (environment : LayoutEnvironment) (profile : FeatureProfile) + (raw : RawInstruction) : Except RawDecodeError (List Instruction) := + let method : RawMethod := + { token := 0, owner := "", hasThis := false, explicitThis := false, callingConvention := 0, + attributes := 0, implementation := 0, returnType := .void, parameters := [], locals := [], + initLocals := true, maxStack := 2, genericParameters := [], code := [raw] } + (decodeRaw profile environment [] [true] method []).map Method.code + +/-- Floating vector transfers reject both the original call and the integer overload of the same width. -/ +theorem rejectedFloatingSites : + ["128", "256", "512"].all (fun width => + let vector := fun lane => TypeExpr.apply + (.constructor intrinsicsScope ("System.Runtime.Intrinsics.Vector" ++ width ++ "`1")) + (.atom runtimeScope lane) + ["System.Single", "System.Double"].all fun lane => + [false, true].all fun byref => + [(name!"Read", false), (name!"Write", true), + (name!"ReadUnaligned", false), (name!"WriteUnaligned", true)].all fun (name, store) => + let raw := site name store (vector lane) byref 0x10 + let integer := site name store (vector "System.UInt64") byref 0x10 + match raw.operand, integer.operand with + | .call call, .call substitute => + (runtimeModel {} .call call.method).isNone && + decide (decoded {} .scalar raw = liveHoleAt 0 40) && + !runtimeCatalog.siteAgrees {} .scalar raw (.call call.method {}) none true && + !runtimeCatalog.siteAgrees {} .scalar raw (.call substitute.method {}) none true + | _, _ => false) = true := by decide +kernel + +#print axioms rejectedFloatingSites + +/-- The name, access direction and address restriction of each helper. -/ +private def transfers : List (MetadataName × Bool × Bool) := + [(name!"Read", false, true), (name!"Write", true, true), + (name!"ReadUnaligned", false, false), (name!"WriteUnaligned", true, false)] + +/-- A sequential layout of one or more words, with the supplied packing. -/ +private def packet (packing words : Nat) (byRefLike := false) : LayoutEnvironment := + let raw : List RawLayout := + [{ identity := "Fixture::Packet", explicit := false, packing := packing, declaredSize := 0, + byRefLike, fields := List.replicate words { type := .unsigned64 } }] + { rawLayouts := raw, layouts := (computeLayouts raw).getD #[] } + +/-- Each live site keeps its call in every profile, with the width and alignment of its element. -/ +private def resolves (environment : LayoutEnvironment) (element : TypeExpr) + (kind : TransferKind) (alignment : Nat) : Bool := + transfers.all fun (name, store, aligned) => + [true, false].all fun byref => + let raw := site name store element byref 0x10 + match raw.operand with + | .call call => + let model := runtimeModel environment .call call.method + if aligned && byref then + model.isNone && decide (decoded environment .scalar raw = liveHoleAt 0 40) + else + let expected := if aligned then RuntimeModel.memory (.pointerTransfer kind alignment store) + else .typedTransfer kind store + let wrongPrefix := Instruction.call call.method { tail := true } + let x64 : FeatureProfile := + { architecture := .x64, sse2 := true, ssse3 := true, + sse41 := true, sse42 := true, avx := true, avx2 := true, bmi1 := true, bmi2 := true, + x86Base64 := true, vector128Accelerated := true, vector256Accelerated := true } + let arm : FeatureProfile := + { architecture := .arm64, armBase64 := true, advSimd := true, + vector128Accelerated := true } + model == some expected && [FeatureProfile.scalar, x64, arm].all fun profile => + decide (decoded environment profile raw = .ok [.call call.method {}]) && + runtimeCatalog.siteAgrees environment profile raw (.call call.method {}) && + !runtimeCatalog.siteAgrees environment profile raw wrongPrefix + | _ => false + +/-- The integer vector of the indicated width with unsigned 64-bit elements. -/ +private def vector (width : String) : TypeExpr := + .apply (.constructor intrinsicsScope ("System.Runtime.Intrinsics.Vector" ++ width ++ "`1")) + (.atom runtimeScope "System.UInt64") + +/-- The closed signature selects a scalar or snapshot independently of the target profile. -/ +theorem integerTransfers : + resolves {} (.atom runtimeScope "System.UInt64") (.scalar 8 false) 8 && + resolves {} (.atom runtimeScope "System.Int16") (.scalar 2 true) 2 && + resolves {} (.enum "Fixture::Flag" (.atom runtimeScope "System.SByte")) (.scalar 1 true) 1 && + resolves {} (vector "128") (.snapshot .vector128) 16 && + resolves {} (vector "256") (.snapshot .vector256) 32 && + resolves {} (vector "512") (.snapshot .vector512) 64 := by decide +kernel + +#print axioms integerTransfers + +/-- Packing and field count can change independently for the same checked struct identity. -/ +theorem aggregateTransfers : + LayoutsValid (packet 1 1).rawLayouts (packet 1 1).layouts ∧ + LayoutsValid (packet 4 3).rawLayouts (packet 4 3).layouts ∧ + resolves (packet 1 1) (.aggregate "Fixture::Packet" 0) (.snapshot (.aggregate 8)) 1 ∧ + resolves (packet 4 3) (.aggregate "Fixture::Packet" 0) (.snapshot (.aggregate 24)) 4 ∧ + resolves (packet 4 3 true) (.aggregate "Fixture::Packet" 0) (.snapshot (.aggregate 24)) 4 := by + decide +kernel + +#print axioms aggregateTransfers + +/-- Packing alone changes the natural alignment of an eight-byte packet at a fixed index. -/ +theorem sameSizeDifferentAlignment : + let type := TypeExpr.aggregate "Fixture::Packet" 0 + let packed := packet 1 1 + let natural := packet 8 1 + LayoutsValid packed.rawLayouts packed.layouts ∧ + LayoutsValid natural.rawLayouts natural.layouts ∧ + packed.callLayouts.valueShape type = .aggregate 8 ∧ + natural.callLayouts.valueShape type = .aggregate 8 ∧ + resolves packed type (.snapshot (.aggregate 8)) 1 ∧ + resolves natural type (.snapshot (.aggregate 8)) 8 := by decide +kernel + +#print axioms sameSizeDifferentAlignment + +/-- Unsupported storage and malformed method references have no transfer model. -/ +theorem refusedElementsAndReferences : + let word := TypeExpr.atom runtimeScope "System.UInt64" + transfers.all (fun (name, store, _) => + let rejected := fun (type : TypeExpr) (change : MethodRef → MethodRef) => + match (site name store type false 0x10).operand with + | .call call => + let changed := change call.method + let raw := site name store type false 0x10 + let changedRaw := { raw with operand := .call { call with method := changed } } + let expected : Except RawDecodeError (List Instruction) := + if type == .parameter 0 then .error .instantiation else liveHoleAt 0 40 + (runtimeModel {} .call changed).isNone && + decide (decoded {} .scalar changedRaw = expected) + | _ => false + [TypeExpr.apply (.constructor runtimeScope "System.Span`1") word, + .apply (.constructor runtimeScope "System.ReadOnlySpan`1") word, .parameter 0, + .pointer word, .byref word, .object runtimeScope "System.String", .void, + .apply (.objectConstructor runtimeScope "System.Collections.Generic.List`1") word, + .aggregate "Fixture::Missing" 0, .atom runtimeScope "System.Double", .array word].all + (fun type => rejected type id) && + rejected word (fun method => { method with owner := .apply unsafeOwner word }) && + rejected word (fun method => { method with + owner := .atom "Other" "System.Runtime.CompilerServices.Unsafe" }) && + rejected word (fun method => { method with hasThis := true }) && + rejected word (fun method => { method with genericArity := 2 }) && + rejected word (fun method => { method with methodArguments := [] })) = true := by + decide +kernel + +#print axioms refusedElementsAndReferences + +/-- A change to the open signature alone refuses both the kept call and the typed emission. -/ +theorem refusedDeclarations : + let word := TypeExpr.atom runtimeScope "System.UInt64" + transfers.all (fun (name, store, aligned) => + let raw := site name store word false 0x10 + match raw.operand with + | .call call => + let denied := fun (declared : OpenSignature) => + let changed := { raw with operand := .call { call with declared } } + decide (decoded {} .scalar raw = .ok [.call call.method {}]) && + decide (decoded {} .scalar changed = liveHoleAt 0 40) && + !runtimeCatalog.siteAgrees {} .scalar changed (.call call.method {}) && + !runtimeCatalog.siteAgrees {} .scalar changed (.call call.method { tail := true }) + let byteAddress := TypeExpr.byref (.atom runtimeScope "System.Byte") + let forged := if aligned then { call.declared with + parameters := if store then [byteAddress, .parameter 0] else [byteAddress] } + else if store then { call.declared with parameters := [.pointer .void, word] } + else { call.declared with returnType := word } + denied forged && [0x00, 0x11, 0x30, 0x50, 0x70].all fun convention => + denied { call.declared with convention } + | _ => false) = true := by decide +kernel + +#print axioms refusedDeclarations + +/-- Each unaligned overload binds its own target and open declaration to the closed address. -/ +theorem crossedOverloads : + [false, true].all (fun store => + let name := if store then name!"WriteUnaligned" else name!"ReadUnaligned" + let element := TypeExpr.atom runtimeScope "System.UInt64" + [false, true].all fun byref => + let raw := site name store element byref 0x10 + match raw.operand, (site name store element (!byref) 0x10).operand with + | .call call, .call other => + let hole := fun (changed : CallSite) => + let changedRaw := { raw with operand := .call changed } + decide (decoded {} .scalar changedRaw = liveHoleAt 0 40) + decide (decoded {} .scalar raw = .ok [.call call.method {}]) && + hole { call with target := other.target } && + hole { call with declared := other.declared } + | _, _ => false) = true := by decide +kernel + +#print axioms crossedOverloads + +/-- An unknown Unsafe member admits only a dead hole. -/ +theorem reservedEmissions : + let control := site name!"Read" false (.atom runtimeScope "System.UInt64") false 0x10 + let other := site name!"Unknown" false (.atom runtimeScope "System.UInt64") false 0x10 + let operations := [Instruction.ldind .i8, .stind .i8, + .ldobj (.aggregate "Fixture::Packet" 0), .stobj (.aggregate "Fixture::Packet" 0)] ++ + ["128", "256", "512"].flatMap (fun width => [.ldobj (vector width), .stobj (vector width)]) + (match control.operand with + | .call call => decide (decoded {} .scalar control = .ok [.call call.method {}]) + | _ => false) = true ∧ decoded {} .scalar other = liveHoleAt 0 40 ∧ + operations.any (fun op => runtimeCatalog.siteAgrees {} .scalar other op none true) + = false ∧ + runtimeCatalog.siteAgrees {} .scalar other (.unadmitted 40) none false = true ∧ + runtimeCatalog.siteAgrees {} .scalar other (.unadmitted 40) none true = false := by + decide +kernel + +#print axioms reservedEmissions + +/-- A non-call instruction cannot claim the checked pointer transfer of a runtime model. -/ +theorem nonCallTransferRefused : + let read := site name!"Read" false (.atom runtimeScope "System.UInt64") false 0x10 + let nop : RawInstruction := ⟨0, 0, .none⟩ + (match read.operand with + | .call call => decide (decoded {} .scalar read = .ok [.call call.method {}]) && + decide (decoded {} .scalar nop = .ok [.nop]) && + !(valueEncodingMatches none nop (.call call.method {})) + | _ => false) = true := by decide +kernel + +#print axioms nonCallTransferRefused + +/-- A transfer over a pointer or span decodes to a hole and refuses a unary replacement. -/ +theorem unresolvedTransfers : + let byte := TypeExpr.atom runtimeScope "System.Byte" + let control := site name!"ReadUnaligned" false byte false 0x10 + (match control.operand with + | .call call => decide (decoded {} .scalar control = .ok [.call call.method {}]) && + !runtimeCatalog.siteAgrees {} .scalar control (.conv_u1) none true + | _ => false) = true ∧ + [TypeExpr.pointer .void, .apply (.constructor runtimeScope "System.Span`1") byte].all + (fun element => + let raw := site name!"ReadUnaligned" false element false 0x10 + decide (decoded {} .scalar raw = liveHoleAt 0 40) && + !runtimeCatalog.siteAgrees {} .scalar raw (.conv_u1) none true && + runtimeCatalog.siteAgrees {} .scalar raw (.unadmitted 40) none false) = true := by + decide +kernel + +#print axioms unresolvedTransfers + +/-- Each resolved call produces the checked transfer action, including its faults. -/ +theorem callEqualsTransfer (body : Method) (method : MethodRef) (pc alignment : Nat) + (kind : TransferKind) (aligned store : Bool) (args stack : List Safety.Value) + (frame : Safety.Frame) (memory : Safety.Memory) (unlinked : body.linkAt pc = none) + (model : runtimeModel body.environment .call method = some (if aligned then + .memory (.pointerTransfer kind alignment store) else .typedTransfer kind store)) : + step body (.call method {}) pc args frame stack memory = + (if aligned then modelStep (some (.memory (.pointerTransfer kind alignment store))) + body pc frame stack memory else typedTransferCall kind store pc frame stack memory) := by + cases aligned + · exact step_call_typedTransfer body pc args frame memory unlinked model rfl stack + · exact step_call_model body pc args frame memory unlinked model rfl stack + +#print axioms callEqualsTransfer + +/-- A misaligned pointer faults for both aligned transfers on an otherwise writable allocation. -/ +theorem misalignedTransfers : + let body : Method := { code := [], locals := [], returnsValue := false } + let frame : Safety.Frame := { activation := 0, locals := [], owned := [] } + let address := ManagedReference.address ⟨0, 1⟩ + let fault := ExecutionFault.memory .alignment address 8 + [false, true].all (fun store => + let name := if store then name!"Write" else name!"Read" + let raw := site name store (.atom runtimeScope "System.UInt64") false 0x10 + match raw.operand with + | .call call => + let stack : List Safety.Value := if store then [.scalar (.i64 0), .reference address] + else [.reference address] + match step body (.call call.method {}) 0 [] frame stack Tests.caller with + | .error actual => actual == fault + | .ok _ => false + | _ => false) = true := by decide +kernel + +#print axioms misalignedTransfers + +end Tests.UnsafeTransferModels diff --git a/Tests/UnsafeWrappers.lean b/Tests/UnsafeWrappers.lean index affd18a19..c3f85a4aa 100644 --- a/Tests/UnsafeWrappers.lean +++ b/Tests/UnsafeWrappers.lean @@ -1,21 +1,38 @@ import Tests.WrapperProofs +/-! Execution facts that the generated Unsafe layout fixtures apply to extracted bodies. Each body + takes the declared signature and layout environment an extraction records. Its return metadata + must match the reference or integer result that the execution fact guarantees. -/ + namespace Tests.UnsafeWrappers open CIL.Safety -def addMethod (size : Nat) (signed : Bool) (profile : CIL.FeatureProfile) : CIL.Method := - { profile := profile, code := [.arg 0, .arg 1, .memory (.add size signed), .ret], - locals := [], returnsValue := true } +/-- `Unsafe.Add` of a closed element type as an extraction emits it. -/ +def addMethod (method : CIL.Extraction.MethodRef) (profile : CIL.FeatureProfile) + (declared : CIL.DeclaredSignature) (environment : CIL.LayoutEnvironment) : CIL.Method := + { profile := profile, code := [.ldarg 0, .ldarg 1, .call method {}, .ret], + locals := [], returnsValue := true, declared := declared, environment := environment } -theorem add_ok (size : Nat) (signed : Bool) (profile : CIL.FeatureProfile) +/-- An in-bounds `Unsafe.Add` over arguments read whole returns the offset reference. -/ +theorem add_ok {declared : CIL.DeclaredSignature} {environment : CIL.LayoutEnvironment} + {element : CIL.Extraction.TypeExpr} + (returnedType : declared.returnType = .byref element) + (referenceStorage : (declared.argumentType 0).bind + (CIL.Extraction.declaredNarrow environment.rawLayouts) = none) + (offsetStorage : (declared.argumentType 1).bind + (CIL.Extraction.declaredNarrow environment.rawLayouts) = none) + {method : CIL.Extraction.MethodRef} {size : Nat} {signed : Bool} + (found : CIL.Extraction.runtimeModel environment .call method = + some (.memory (.add size signed))) + (profile : CIL.FeatureProfile) (memory : Memory) (reference returned : ManagedReference) (offset : CIL.Value) (valid : checkedValue memory (.reference reference) = .ok (.reference reference)) (numeric : checkedValue memory (.scalar offset) = .ok (.scalar offset)) (operation : memoryInstruction (.add size signed) [.scalar offset, .reference reference] memory = .ok (memory, [.reference returned])) (result : checkedValue memory (.reference returned) = .ok (.reference returned)) : - (invoke [addMethod size signed profile] 8 0 [.reference reference, .scalar offset] memory).map Prod.snd = - .ok [.reference returned] := by + (invoke [addMethod method profile declared environment] 8 0 + [.reference reference, .scalar offset] memory).map Prod.snd = .ok [.reference returned] := by have specified : offset.hasUnspecified = false := by cases unknown : offset.hasUnspecified with | false => rfl @@ -23,63 +40,176 @@ theorem add_ok (size : Nat) (signed : Bool) (profile : CIL.FeatureProfile) have rejected := operation simp [memoryInstruction, CIL.MemoryOp.argumentCount, unspecifiedOperands, Value.hasUnspecified, unknown] at rejected - simp only [invoke, addMethod, List.getElem?_cons_zero, List.mapM_cons, List.mapM_nil, + simp only [invoke_eq, addMethod, List.getElem?_cons_zero, List.mapM_cons, List.mapM_nil, valid, numeric, enterFrame, makeSelectedLocals, List.isEmpty, ↓reduceIte, makeLocals, - makeArgumentHomes, makeParameterHomes, + makeParameterHomes, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, List.map_nil, List.nil_append] - rw [run_next (body := addMethod size signed profile) rfl rfl - (by simp [step, observerCount, unspecifiedOperands, valid] <;> rfl)] - rw [run_next (body := addMethod size signed profile) rfl rfl - (by simp [step, observerCount, unspecifiedOperands, numeric] <;> rfl)] - rw [run_next (body := addMethod size signed profile) rfl rfl - (by simp [step, observerCount, CIL.MemoryOp.argumentCount, unspecifiedOperands, - Value.hasUnspecified, specified, staticInstruction, operation] <;> rfl)] - rw [run_return (body := addMethod size signed profile) rfl rfl - (by simp [step, observerCount, unspecifiedOperands, addMethod, result] <;> rfl) + rw [run_next (body := addMethod method profile declared environment) rfl rfl + (by simp [step, observerCount, unspecifiedOperands, loadArgument, CIL.Method.argumentAccess, + addMethod, referenceStorage, valid] <;> rfl)] + rw [run_next (body := addMethod method profile declared environment) rfl rfl + (by simp [step, observerCount, unspecifiedOperands, loadArgument, CIL.Method.argumentAccess, + addMethod, offsetStorage, numeric] <;> rfl)] + rw [run_next (body := addMethod method profile declared environment) rfl rfl + (by + rw [step_call_model (link := rfl) (found := found) (direct := rfl)] + simp [modelStep_memory, CIL.MemoryOp.argumentCount, unspecifiedOperands, + Value.hasUnspecified, specified, staticInstruction, operation, viewOperandCount, + viewOperands] <;> rfl)] + rw [run_return (body := addMethod method profile declared environment) rfl rfl + (by simp [step, returnFrom, observerCount, unspecifiedOperands, addMethod, result, + CIL.Method.returnNarrow, returnedType, CIL.Extraction.declaredNarrow, + CIL.Extraction.typeStorage] <;> rfl) (by simp [leaveFrame, result] <;> rfl)] rfl -theorem add_error (size : Nat) (signed : Bool) (profile : CIL.FeatureProfile) +/-- A faulting `Unsafe.Add` stops at the add with its fault. -/ +theorem add_error {declared : CIL.DeclaredSignature} {environment : CIL.LayoutEnvironment} + (referenceStorage : (declared.argumentType 0).bind + (CIL.Extraction.declaredNarrow environment.rawLayouts) = none) + (offsetStorage : (declared.argumentType 1).bind + (CIL.Extraction.declaredNarrow environment.rawLayouts) = none) + {method : CIL.Extraction.MethodRef} {size : Nat} {signed : Bool} + (found : CIL.Extraction.runtimeModel environment .call method = + some (.memory (.add size signed))) + (profile : CIL.FeatureProfile) (memory : Memory) (reference : ManagedReference) (offset : CIL.Value) (fault : ExecutionFault) (valid : checkedValue memory (.reference reference) = .ok (.reference reference)) (numeric : checkedValue memory (.scalar offset) = .ok (.scalar offset)) (specified : offset.hasUnspecified = false) (operation : memoryInstruction (.add size signed) [.scalar offset, .reference reference] memory = .error fault) : - (invoke [addMethod size signed profile] 8 0 [.reference reference, .scalar offset] memory).map Prod.snd = - .error ⟨0, 2, fault⟩ := by - simp only [invoke, addMethod, List.getElem?_cons_zero, List.mapM_cons, List.mapM_nil, + (invoke [addMethod method profile declared environment] 8 0 + [.reference reference, .scalar offset] memory).map Prod.snd = .error ⟨0, 2, fault⟩ := by + simp only [invoke_eq, addMethod, List.getElem?_cons_zero, List.mapM_cons, List.mapM_nil, valid, numeric, enterFrame, makeSelectedLocals, List.isEmpty, ↓reduceIte, makeLocals, - makeArgumentHomes, makeParameterHomes, + makeParameterHomes, Except.mapError, Bind.bind, Except.bind, Pure.pure, Except.pure, List.map_nil, List.nil_append] - rw [run_next (body := addMethod size signed profile) rfl rfl - (by simp [step, observerCount, unspecifiedOperands, valid] <;> rfl)] - rw [run_next (body := addMethod size signed profile) rfl rfl - (by simp [step, observerCount, unspecifiedOperands, numeric] <;> rfl)] - rw [Tests.WrapperProofs.run_fault (body := addMethod size signed profile) rfl rfl - (by simp [step, observerCount, CIL.MemoryOp.argumentCount, unspecifiedOperands, - Value.hasUnspecified, specified, staticInstruction, operation] <;> rfl)] + rw [run_next (body := addMethod method profile declared environment) rfl rfl + (by simp [step, observerCount, unspecifiedOperands, loadArgument, CIL.Method.argumentAccess, + addMethod, referenceStorage, valid] <;> rfl)] + rw [run_next (body := addMethod method profile declared environment) rfl rfl + (by simp [step, observerCount, unspecifiedOperands, loadArgument, CIL.Method.argumentAccess, + addMethod, offsetStorage, numeric] <;> rfl)] + rw [Tests.WrapperProofs.run_fault + (body := addMethod method profile declared environment) rfl rfl + (by + rw [step_call_model (link := rfl) (found := found) (direct := rfl)] + simp [modelStep_memory, CIL.MemoryOp.argumentCount, unspecifiedOperands, + Value.hasUnspecified, specified, staticInstruction, operation, viewOperandCount, + viewOperands] <;> rfl)] rfl /-- A partial vector is rejected before the integer-offset adapter runs. -/ -example (body : CIL.Method) (pc : Nat) (args : List Value) (frame : Frame) +example (body : CIL.Method) (pc : Nat) (frame : Frame) (memory : Memory) (reference : ManagedReference) (low : CIL.W64) : memoryInstruction (.add 1 false) [.scalar (.v128Low64 low), .reference reference] memory = .error (.unsupported "Memory operation observes unspecified vector bits") ∧ - step body (.memory (.add 1 false)) pc args frame + modelStep (some (.memory (.add 1 false))) body pc frame [.scalar (.v128Low64 low), .reference reference] memory = .error (.unsupported "Instruction observes unspecified vector bits") := by constructor <;> rfl -/-- The complete checked constant-return wrapper, for every size and caller memory. -/ -theorem size_ok (size : BitVec 32) (profile : CIL.FeatureProfile) (memory : Memory) : - (invoke [{ profile := profile, code := [.const32 size, .ret], locals := [], returnsValue := true }] - 8 0 [] memory).map Prod.snd = .ok [.scalar (.i32 size)] := by +/-- `Unsafe.SizeOf` as an extraction emits it: a call without a link, whose model the layouts + give. -/ +def sizeMethod (method : CIL.Extraction.MethodRef) (profile : CIL.FeatureProfile) + (declared : CIL.DeclaredSignature) (environment : CIL.LayoutEnvironment) : CIL.Method := + { profile := profile, code := [.call method {}, .ret], locals := [], returnsValue := true, + declared := declared, environment := environment } + +/-- The complete checked wrapper of a call the layouts model as a constant, for every caller + memory. -/ +theorem size_ok {declared : CIL.DeclaredSignature} {environment : CIL.LayoutEnvironment} + {method : CIL.Extraction.MethodRef} {size : BitVec 32} + (returnedType : declared.returnType = .atom CIL.Extraction.runtimeScope "System.Int32") + (found : CIL.Extraction.runtimeModel environment .call method = some (.constant size)) + (profile : CIL.FeatureProfile) (memory : Memory) : + (invoke [sizeMethod method profile declared environment] 8 0 [] memory).map Prod.snd = + .ok [.scalar (.i32 size)] := by + let body := sizeMethod method profile declared environment + let frame : Frame := ⟨memory.nextIdentity, [], [], 0⟩ + let values := [Value.scalar (.i32 size)] + have pushes : step body (.call method {}) 0 [] frame [] memory = + .ok (.next 1 values frame memory) := + step_call_model (body := body) (pc := 0) (method := method) (prefixes := {}) + (args := []) (frame := frame) (memory := memory) rfl found rfl [] + have narrow : body.returnNarrow = none := by + simp only [body, sizeMethod, CIL.Method.returnNarrow, returnedType] + rfl + have returns : step body .ret 1 [] frame values memory = .ok (.returned values memory) := by + change returnFrom body.returnNarrow true values memory = _ + rw [narrow] + rfl + have next := run_next (program := [body]) (fuel := 7) (method := 0) rfl rfl pushes + have done := run_return (program := [body]) (fuel := 6) (method := 0) rfl rfl returns + (by rfl) + have entered : enterFrame body [] memory = .ok (frame, memory) := rfl + change (invoke [body] 8 0 [] memory).map Prod.snd = _ + simp only [invoke_eq, List.getElem?_cons_zero, List.mapM_nil, entered, Except.mapError, + Bind.bind, Except.bind, Pure.pure, Except.pure, next, done] rfl #print axioms add_ok #print axioms add_error #print axioms size_ok + +/-! The fixtures apply these facts to extracted bodies, which carry their declared signature and + the extraction's layouts. Each example states a body in the emitted form, with a non-empty + environment, and proves its claim with the term the fixture generator writes. -/ + +/-- A layout environment with one entry, as an extraction that reads a struct records. -/ +private def emittedEnvironment : CIL.LayoutEnvironment := { layouts := #[⟨⟨4, []⟩, 4⟩] } + +/-- `int Unsafe.SizeOf()` as a call site instantiates it. -/ +private def sizeOfInt : CIL.Extraction.MethodRef := + { owner := CIL.Extraction.unsafeOwner, name := name!"SizeOf", hasThis := false, + genericArity := 1, returnType := .atom CIL.Extraction.runtimeScope "System.Int32", + parameters := [], methodArguments := [.atom CIL.Extraction.runtimeScope "System.Int32"] } + +/-- `Unsafe.SizeOf` in the form an extraction emits. -/ +private def emittedSize : CIL.Method := + { profile := .scalar, returnsValue := true, locals := [], localKinds := [], + opaqueVectorLocals := [], parameterHomes := [], staticSites := [], + declared := + { parameters := [], returnType := .atom CIL.Extraction.runtimeScope "System.Int32" }, + environment := emittedEnvironment, links := [], code := [.call sizeOfInt {}, .ret] } + +example : (invoke [emittedSize] 8 0 [] Memory.empty).map Prod.snd = .ok [.scalar (.i32 4)] := by + exact size_ok (declared := emittedSize.declared) (method := sizeOfInt) + (environment := emittedEnvironment) (size := 4) rfl (by decide +kernel) .scalar Memory.empty + +/-- The signed Int32 offset overload at a Boolean element. -/ +private def addBoolean : CIL.Extraction.MethodRef := + let element := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.Boolean" + let offset := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.Int32" + { owner := CIL.Extraction.unsafeOwner, name := name!"Add", hasThis := false, + genericArity := 1, returnType := .byref element, parameters := [.byref element, offset], + methodArguments := [element] } + +/-- `Unsafe.Add` of a byte element in the form an extraction emits. -/ +private def emittedAdd : CIL.Method := + { profile := .scalar, returnsValue := true, locals := [], localKinds := [], + opaqueVectorLocals := [], parameterHomes := [], staticSites := [], + declared := + { parameters := [.byref (.atom CIL.Extraction.runtimeScope "System.Boolean"), + .atom CIL.Extraction.runtimeScope "System.Int32"], + returnType := .byref (.atom CIL.Extraction.runtimeScope "System.Boolean") }, + environment := emittedEnvironment, links := [], + code := [.ldarg 0, .ldarg 1, .call addBoolean {}, .ret] } + +/-- Four caller-stack bytes at allocation 0. -/ +private def emittedMemory : Memory := { Memory.empty with + allocations := fun id => if id = 0 then some ⟨.callerStack, ⟨4, 1, []⟩, true, [4]⟩ else none + nextIdentity := 1 } + +example : + (invoke [emittedAdd] 8 0 [.reference (.address ⟨0, 2⟩), .scalar (.i32 4294967295)] + emittedMemory).map Prod.snd = .ok [.reference (.address ⟨0, 1⟩)] ∧ + (invoke [emittedAdd] 8 0 [.reference .null, .scalar (.i32 0)] emittedMemory).map Prod.snd = + .error ⟨0, 2, .memory .nullDereference .null 0⟩ := by + exact ⟨add_ok rfl rfl rfl rfl _ _ _ _ _ rfl rfl rfl rfl, + add_error rfl rfl rfl _ _ _ _ _ rfl rfl rfl rfl⟩ + end Tests.UnsafeWrappers diff --git a/Tests/ValueBitCasts.cs b/Tests/ValueBitCasts.cs index efa989bc2..a2cf55c34 100644 --- a/Tests/ValueBitCasts.cs +++ b/Tests/ValueBitCasts.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Buffers.Binary; @@ -37,6 +42,17 @@ void Prove(string name, int width, string source, string target) string.Join(" ∧\n", claims) + " := by exact ⟨" + string.Join(", ", claims.Select(_ => "by rfl")) + "⟩\n" + "#print axioms correctness\nend ExampleProof\n"); + if (name.StartsWith("From", StringComparison.Ordinal) || name.StartsWith("To", StringComparison.Ordinal)) + File.AppendAllText(proof, $$""" + + /-- The compiled BitCast keeps its call and resolves the two snapshot shapes. -/ + theorem ExampleProof.cast_call : + (Extracted.method0.code[1]?).bind (fun + | .call method _ => CIL.Extraction.runtimeModel Extracted.environment .call method + | _ => none) = some (.memory (.bitcastValue {{source}} {{target}})) := by decide +kernel + #print axioms ExampleProof.cast_call + + """); pending.Add(new GeneratedVerification(selectors[name], proof, output), () => Console.WriteLine("PASS compiled BitCast full/partial/unknown masks: " + name)); } diff --git a/Tests/ValueDecoding.lean b/Tests/ValueDecoding.lean new file mode 100644 index 000000000..456fed3c1 --- /dev/null +++ b/Tests/ValueDecoding.lean @@ -0,0 +1,776 @@ +import CIL.Extraction.Scalars +import CIL.Extraction.DecodedDefinition + +/-! +# Value decoding coverage + +Every `CIL.Instruction` constructor is produced by `Instruction.ofRaw?` for a named opcode or is +listed as one that no opcode decodes to, `ofRaw?` decodes exactly the named opcodes, and an +emitted instruction that differs from the decoded one in constructor, operand or operand range +does not match. +-/ + +namespace Tests.ValueDecoding + +open CIL CIL.Extraction Lean Meta + +/-- An `int32` instance field of a class, at raw index 1. -/ +def classOperand : RawOperand := + .field (.object "T" "T.Account") 1 (.atom runtimeScope "System.Int32") name!"balance" false + +/-- The field `classOperand` names. -/ +def classField : FieldRef := + { owner := .object "T" "T.Account", index := 1, type := .atom runtimeScope "System.Int32", + name := name!"balance" } + +/-- An `int32` instance field of a struct, at raw index 1. -/ +def structOperand : RawOperand := + .field (.aggregate "T::Pair" 0) 1 (.atom runtimeScope "System.Int32") name!"balance" false + +/-- The field `structOperand` names. -/ +def structField : FieldRef := { classField with owner := .aggregate "T::Pair" 0 } + +/-- The class the type test rows name. -/ +private def dog : TypeExpr := .object "Fixture" "Fixture.Dog" + +/-- The element type `System.Int32`. -/ +private def int32 : TypeExpr := .atom runtimeScope "System.Int32" + +/-- A one-field generic layout whose owner is the runtime span named by `name`. -/ +private def spanLayout (name : String) : RawLayout := + let owner := TypeExpr.constructor runtimeScope name + { identity := (genericOwnerIdentity owner [int32]).getD "" + explicit := false, packing := 0, declaredSize := 0, byRefLike := true + fields := [⟨.signed32, none⟩] + generic := some ⟨owner, 1, [int32], [.parameter 0], [(0, [])]⟩ } + +/-- The span reset fixture has a valid four-byte layout row and decodes to `initobj`. -/ +theorem span_reset_inputs : + ["System.Span`1", "System.ReadOnlySpan`1"].all (fun name => + let span := TypeExpr.apply (.constructor runtimeScope name) int32 + decide (LayoutsValid [spanLayout name] #[⟨⟨4, [⟨0, 4, false⟩]⟩, 4⟩]) && + Instruction.ofRaw? none ⟨0, 0xfe15, .type span⟩ == some (.initobj span)) = true := by + decide +kernel +#print axioms span_reset_inputs + +/-- Specializing a span reset over a matching generic layout preserves its operand and decoding. -/ +theorem span_reset_substitution : + ["System.Span`1", "System.ReadOnlySpan`1"].all (fun name => + let span := TypeExpr.apply (.constructor runtimeScope name) int32 + let closed := substituteOperand [] [spanLayout name] 0 (.type span) + closed == some (.type span) && + (closed.bind fun operand => Instruction.ofRaw? none ⟨0, 0xfe15, operand⟩) == + some (.initobj span)) = true := by decide +kernel +#print axioms span_reset_substitution + +/-- A static call site of `T.Account::Deposit(int32)`. -/ +private def callSite : CallSite := { + declared := { convention := 0, parameters := [.atom runtimeScope "System.Int32"] }, + method := { + owner := .object "T" "T.Account", name := name!"Deposit", hasThis := false, + genericArity := 0, returnType := .void, parameters := [.atom runtimeScope "System.Int32"] }, + name := "Deposit", target := .managed 0x06000001, typeArguments := [], parameterCount := 1, + returnsValue := false } + +/-- A managed site of the `int32` constructor of `T.Account`. -/ +private def accountConstruction : CallSite := { + declared := { convention := 0x20, parameters := [.atom runtimeScope "System.Int32"] }, + method := { + owner := .object "T" "T.Account", name := name!".ctor", hasThis := true, genericArity := 0, + returnType := .void, parameters := [.atom runtimeScope "System.Int32"] }, + name := ".ctor", target := .managed 1, typeArguments := [], parameterCount := 1, + returnsValue := false } + +/-- An instance slot callable with or without a constrained prefix. -/ +private def instanceSite : CallSite := { callSite with method.hasThis := true } + +/-- The prefix carried by a named decode-table row: a constrained receiver before the call at + offset 6, and `unaligned. 2` before the block operations at offset 2. -/ +private def predecessor (raw : RawInstruction) : Option RawInstruction := + if raw.opcode == 111 && raw.offset == 6 then some ⟨0, 0xFE16, .type int32⟩ + else if (raw.opcode == 65047 || raw.opcode == 65048) && raw.offset == 2 then + some ⟨0, 0xFE12, .integer 2⟩ + else none + +/-- One raw instruction per value opcode, every short, long and macro form, with the instruction + ECMA-335 assigns it; constants include the extremes of each form's range and locals the largest + index each width encodes. -/ +def named : List (RawInstruction × Instruction) := [ + -- Stack shuffling and return. + (⟨0, 0, .none⟩, .nop), (⟨0, 37, .none⟩, .dup), (⟨0, 38, .none⟩, .pop), (⟨0, 42, .none⟩, .ret), + -- Constants. + (⟨0, 114, .text "literal"⟩, .ldstr "literal"), + (⟨0, 20, .none⟩, .ldnull), (⟨0, 21, .none⟩, .ldc_i4 (-1)), (⟨0, 22, .none⟩, .ldc_i4 0), + (⟨0, 23, .none⟩, .ldc_i4 1), + (⟨0, 24, .none⟩, .ldc_i4 2), (⟨0, 25, .none⟩, .ldc_i4 3), (⟨0, 26, .none⟩, .ldc_i4 4), + (⟨0, 27, .none⟩, .ldc_i4 5), (⟨0, 28, .none⟩, .ldc_i4 6), (⟨0, 29, .none⟩, .ldc_i4 7), + (⟨0, 30, .none⟩, .ldc_i4 8), (⟨0, 31, .integer (-5)⟩, .ldc_i4 (-5)), + (⟨0, 31, .integer (-128)⟩, .ldc_i4 (-128)), (⟨0, 31, .integer 127⟩, .ldc_i4 127), + (⟨0, 32, .integer 70000⟩, .ldc_i4 70000), + (⟨0, 32, .integer (-2147483648)⟩, .ldc_i4 (-2147483648)), + (⟨0, 32, .integer 2147483647⟩, .ldc_i4 2147483647), + (⟨0, 33, .integer (-1)⟩, .ldc_i8 (-1)), + (⟨0, 33, .integer (-9223372036854775808)⟩, .ldc_i8 (-9223372036854775808)), + (⟨0, 33, .integer 9223372036854775807⟩, .ldc_i8 9223372036854775807), + (⟨0, 208, .type dog⟩, .ldtoken (.type dog)), + -- Arguments. + (⟨0, 2, .none⟩, .ldarg 0), (⟨0, 3, .none⟩, .ldarg 1), (⟨0, 4, .none⟩, .ldarg 2), + (⟨0, 5, .none⟩, .ldarg 3), (⟨0, 14, .argument 5⟩, .ldarg 5), + (⟨0, 14, .argument 255⟩, .ldarg 255), (⟨0, 65033, .argument 300⟩, .ldarg 300), + (⟨0, 15, .argument 5⟩, .ldarga 5), (⟨0, 15, .argument 255⟩, .ldarga 255), + (⟨0, 65034, .argument 65535⟩, .ldarga 65535), (⟨0, 16, .argument 5⟩, .starg 5), + (⟨0, 16, .argument 255⟩, .starg 255), (⟨0, 65035, .argument 65535⟩, .starg 65535), + -- Locals. + (⟨0, 6, .none⟩, .ldloc 0), (⟨0, 7, .none⟩, .ldloc 1), (⟨0, 8, .none⟩, .ldloc 2), + (⟨0, 9, .none⟩, .ldloc 3), (⟨0, 17, .local 4⟩, .ldloc 4), (⟨0, 17, .local 255⟩, .ldloc 255), + (⟨0, 65036, .local 300⟩, .ldloc 300), (⟨0, 65036, .local 65535⟩, .ldloc 65535), + (⟨0, 10, .none⟩, .stloc 0), (⟨0, 11, .none⟩, .stloc 1), (⟨0, 12, .none⟩, .stloc 2), + (⟨0, 13, .none⟩, .stloc 3), (⟨0, 19, .local 4⟩, .stloc 4), (⟨0, 65038, .local 300⟩, .stloc 300), + (⟨0, 18, .local 4⟩, .ldloca 4), (⟨0, 65037, .local 300⟩, .ldloca 300), + -- Branches, short forms then long forms, each to a target named after its opcode. + (⟨0, 43, .target 43⟩, .br 43), (⟨0, 44, .target 44⟩, .brfalse 44), + (⟨0, 45, .target 45⟩, .brtrue 45), (⟨0, 46, .target 46⟩, .beq 46), + (⟨0, 47, .target 47⟩, .bge 47), (⟨0, 48, .target 48⟩, .bgt 48), + (⟨0, 49, .target 49⟩, .ble 49), (⟨0, 50, .target 50⟩, .blt 50), + (⟨0, 51, .target 51⟩, .bne_un 51), (⟨0, 52, .target 52⟩, .bge_un 52), + (⟨0, 53, .target 53⟩, .bgt_un 53), (⟨0, 54, .target 54⟩, .ble_un 54), + (⟨0, 55, .target 55⟩, .blt_un 55), + (⟨0, 56, .target 56⟩, .br 56), (⟨0, 57, .target 57⟩, .brfalse 57), + (⟨0, 58, .target 58⟩, .brtrue 58), (⟨0, 59, .target 59⟩, .beq 59), + (⟨0, 60, .target 60⟩, .bge 60), (⟨0, 61, .target 61⟩, .bgt 61), + (⟨0, 62, .target 62⟩, .ble 62), (⟨0, 63, .target 63⟩, .blt 63), + (⟨0, 64, .target 64⟩, .bne_un 64), (⟨0, 65, .target 65⟩, .bge_un 65), + (⟨0, 66, .target 66⟩, .bgt_un 66), (⟨0, 67, .target 67⟩, .ble_un 67), + (⟨0, 68, .target 68⟩, .blt_un 68), (⟨0, 69, .targets [1, 2]⟩, .switch [1, 2]), + -- Integer indirect loads and stores. + (⟨0, 70, .none⟩, .ldind .i1), (⟨0, 71, .none⟩, .ldind .u1), (⟨0, 72, .none⟩, .ldind .i2), + (⟨0, 73, .none⟩, .ldind .u2), (⟨0, 74, .none⟩, .ldind .i4), (⟨0, 75, .none⟩, .ldind .u4), + (⟨0, 76, .none⟩, .ldind .i8), (⟨0, 77, .none⟩, .ldind .i), (⟨0, 82, .none⟩, .stind .i1), + (⟨0, 83, .none⟩, .stind .i2), (⟨0, 84, .none⟩, .stind .i4), (⟨0, 85, .none⟩, .stind .i8), + (⟨0, 223, .none⟩, .stind .i), + -- Array lengths and integer and class reference element loads and stores. + (⟨0, 142, .none⟩, .ldlen), (⟨0, 144, .none⟩, .ldelem .i1), (⟨0, 145, .none⟩, .ldelem .u1), + (⟨0, 146, .none⟩, .ldelem .i2), (⟨0, 147, .none⟩, .ldelem .u2), (⟨0, 148, .none⟩, .ldelem .i4), + (⟨0, 149, .none⟩, .ldelem .u4), (⟨0, 150, .none⟩, .ldelem .i8), (⟨0, 151, .none⟩, .ldelem .i), + (⟨0, 154, .none⟩, .ldelem_ref), (⟨0, 155, .none⟩, .stelem .i), (⟨0, 156, .none⟩, .stelem .i1), + (⟨0, 157, .none⟩, .stelem .i2), (⟨0, 158, .none⟩, .stelem .i4), (⟨0, 159, .none⟩, .stelem .i8), + (⟨0, 162, .none⟩, .stelem_ref), + -- Arithmetic, bitwise operations and shifts. + (⟨0, 88, .none⟩, .add), (⟨0, 89, .none⟩, .sub), (⟨0, 90, .none⟩, .mul), (⟨0, 91, .none⟩, .div), + (⟨0, 92, .none⟩, .div_un), (⟨0, 93, .none⟩, .rem), (⟨0, 94, .none⟩, .rem_un), + (⟨0, 95, .none⟩, .and), (⟨0, 96, .none⟩, .or), (⟨0, 97, .none⟩, .xor), (⟨0, 98, .none⟩, .shl), + (⟨0, 99, .none⟩, .shr), (⟨0, 100, .none⟩, .shr_un), (⟨0, 101, .none⟩, .neg), + (⟨0, 102, .none⟩, .not), + -- Conversions. + (⟨0, 103, .none⟩, .conv_i1), (⟨0, 104, .none⟩, .conv_i2), (⟨0, 105, .none⟩, .conv_i4), + (⟨0, 106, .none⟩, .conv_i8), (⟨0, 109, .none⟩, .conv_u4), (⟨0, 110, .none⟩, .conv_u8), + (⟨0, 209, .none⟩, .conv_u2), (⟨0, 210, .none⟩, .conv_u1), (⟨0, 211, .none⟩, .conv_i), + (⟨0, 224, .none⟩, .conv_u), + -- Overflow-checked conversions from an unsigned source. + (⟨0, 130, .none⟩, .conv_ovf_un .i1), (⟨0, 131, .none⟩, .conv_ovf_un .i2), + (⟨0, 132, .none⟩, .conv_ovf_un .i4), (⟨0, 133, .none⟩, .conv_ovf_un .i8), + (⟨0, 134, .none⟩, .conv_ovf_un .u1), (⟨0, 135, .none⟩, .conv_ovf_un .u2), + (⟨0, 136, .none⟩, .conv_ovf_un .u4), (⟨0, 137, .none⟩, .conv_ovf_un .u8), + (⟨0, 138, .none⟩, .conv_ovf_un .i), (⟨0, 139, .none⟩, .conv_ovf_un .u), + -- Overflow-checked conversions from a signed source. + (⟨0, 179, .none⟩, .conv_ovf .i1), (⟨0, 180, .none⟩, .conv_ovf .u1), + (⟨0, 181, .none⟩, .conv_ovf .i2), (⟨0, 182, .none⟩, .conv_ovf .u2), + (⟨0, 183, .none⟩, .conv_ovf .i4), (⟨0, 184, .none⟩, .conv_ovf .u4), + (⟨0, 185, .none⟩, .conv_ovf .i8), (⟨0, 186, .none⟩, .conv_ovf .u8), + (⟨0, 212, .none⟩, .conv_ovf .i), (⟨0, 213, .none⟩, .conv_ovf .u), + -- Overflow-checked arithmetic and comparisons. + (⟨0, 214, .none⟩, .add_ovf), (⟨0, 215, .none⟩, .add_ovf_un), (⟨0, 216, .none⟩, .mul_ovf), + (⟨0, 217, .none⟩, .mul_ovf_un), (⟨0, 218, .none⟩, .sub_ovf), (⟨0, 219, .none⟩, .sub_ovf_un), + (⟨0, 65025, .none⟩, .ceq), (⟨0, 65026, .none⟩, .cgt), (⟨0, 65027, .none⟩, .cgt_un), + (⟨0, 65028, .none⟩, .clt), (⟨0, 65029, .none⟩, .clt_un), + -- Instance field accesses of a class and of a struct. + (⟨0, 123, classOperand⟩, .ldfld classField), (⟨0, 124, classOperand⟩, .ldflda classField), + (⟨0, 125, classOperand⟩, .stfld classField), + (⟨0, 123, structOperand⟩, .ldfld structField), (⟨0, 124, structOperand⟩, .ldflda structField), + (⟨0, 125, structOperand⟩, .stfld structField), + -- Typed object operations. + (⟨0, 0x70, .type int32⟩, .cpobj int32), (⟨0, 0x71, .type int32⟩, .ldobj int32), + (⟨0, 0x81, .type int32⟩, .stobj int32), (⟨0, 0xfe15, .type int32⟩, .initobj int32), + (⟨0, 0xfe1c, .type int32⟩, .sizeof int32), + -- Dynamic frame storage. + (⟨0, 65039, .none⟩, .localloc), + -- Block copies and fills, at the default alignment and after `unaligned. 2`. + (⟨0, 65047, .none⟩, .cpblk 8), (⟨0, 65048, .none⟩, .initblk 8), + (⟨2, 65047, .none⟩, .cpblk 2), (⟨2, 65048, .none⟩, .initblk 2), + -- Class type tests. + (⟨0, 116, .type dog⟩, .castclass dog), (⟨0, 117, .type dog⟩, .isinst dog), + -- Array allocations and element accesses that name their element type. + (⟨0, 141, .type int32⟩, .newarr int32), (⟨0, 163, .type int32⟩, .ldelem_any int32), + (⟨0, 164, .type int32⟩, .stelem_any int32), (⟨0, 143, .type int32⟩, .ldelema int32 false), + -- Prefixes recorded by the following access, address or call. + (⟨0, 65054, .none⟩, .nop), (⟨0, 65042, .integer 1⟩, .nop), (⟨0, 65042, .integer 2⟩, .nop), + (⟨0, 65042, .integer 4⟩, .nop), + (⟨0, 0xFE16, .type int32⟩, .nop), + -- Calls and constructions, whose target the link at their pc gives. + (⟨0, 40, .call callSite⟩, .call callSite.method), + (⟨0, 111, .call instanceSite⟩, .callvirt instanceSite.method), + (⟨0, 65030, .call { callSite with typeArguments := [int32], method.genericArity := 1 }⟩, + .ldftn ⟨"T::T.Account", 0x06000001, [callableType int32]⟩ + ⟨.void, [callableType int32]⟩), + (⟨0, 41, .indirectCall .void [int32] false false 0⟩, + .calli ⟨.void, [callableType int32]⟩ false), + (⟨6, 111, .call instanceSite⟩, .call instanceSite.method { constrained := some int32 }), + (⟨0, 115, .call accountConstruction⟩, .newobj accountConstruction.method)] + +/-- Constructors no opcode decodes to: the families outside the value opcodes. -/ +def undecoded : List Name := [ + ``Instruction.throw, + ``Instruction.unadmitted, ``Instruction.ldsfld, ``Instruction.ldsflda, ``Instruction.stsfld] + +/-- What each named opcode decodes to after its predecessor. -/ +def decodedNamed : List (Option Instruction) := + named.map fun (raw, _) => Instruction.ofRaw? (predecessor raw) raw + +/-- A string load binds its text and a type token binds the type it names. Field tokens require site binding. -/ +theorem literal_operands : + let literal : RawInstruction := ⟨0, 114, .text "original"⟩ + valueEncodingMatches none literal (.ldstr "original") = true ∧ + valueEncodingMatches none literal (.ldstr "changed") = false ∧ + valueEncodingMatches none literal .nop = false ∧ + scalarEmissionMatches none none literal [] (.ldstr "changed") = false ∧ + scalarEmissionMatches none none literal [] .nop = false ∧ + Instruction.ofRaw? none ⟨0, 114, .none⟩ = none ∧ + Instruction.ofRaw? none ⟨0, 208, .type dog⟩ = some (.ldtoken (.type dog)) ∧ + valueEncodingMatches none ⟨0, 208, .type dog⟩ (.ldtoken (.type int32)) = false ∧ + Instruction.ofRaw? none ⟨0, 208, classOperand⟩ = none := by decide +kernel +#print axioms literal_operands + +/-- Bare string loads accept ordinary escapes and BMP text, and reject unsupported characters + anywhere in the literal. -/ +theorem literal_characters : + let decode := fun text => Instruction.ofRaw? none ⟨0, 114, .text text⟩ + let supported := "a\t\n\r\uFFFF" + decode supported = some (.ldstr supported) ∧ + decode "a\x08b" = none ∧ decode "a\x0cb" = none ∧ + decode (String.singleton (Char.ofNat 0x10000)) = none := by decide +kernel +#print axioms literal_characters + +/-- Fold results bind their Boolean, branch target or exception. Direct and indirect tail calls + carry the prefix at the previous pc. -/ +theorem role_decodings : + let tailPrefix : RawInstruction := ⟨0, 65044, .none⟩ + let branch : RawInstruction := ⟨1, 46, .target 9⟩ + let call : RawInstruction := ⟨2, 40, .call callSite⟩ + let indirect : RawInstruction := ⟨2, 41, .indirectCall .void [] false false 0⟩ + let decoded := fun role raw => Instruction.ofRole? role none raw + decoded .head call = some .nop ∧ decoded .interior call = some .nop ∧ + decoded .head ⟨0, 20, .none⟩ = some .ldnull ∧ + decoded .interior ⟨0, 20, .none⟩ = some .ldnull ∧ + decoded .head ⟨0, 20, .integer 0⟩ = none ∧ + decoded .blocked call = none ∧ decoded (.result true) call = some (.call callSite.method) ∧ + decoded (.result false) call = some (.call callSite.method) ∧ + decoded (.typeBranch true) branch = some (.beq 9) ∧ + decoded (.typeBranch true) branch ≠ some (.beq 8) ∧ + decoded (.typeBranch false) branch = some (.beq 9) ∧ + decoded (.raise .argument) ⟨0, 122, .none⟩ = some (.throw .argument) ∧ + decoded (.raise .argument) ⟨0, 122, .none⟩ ≠ some (.throw .overflow) ∧ + decoded .argument ⟨0, 2, .none⟩ = some (.ldarg 0) ∧ + Instruction.ofRole? .tailCall (some tailPrefix) call = + some (.call callSite.method { tail := true }) ∧ + Instruction.ofRole? .tailCall (some tailPrefix) indirect = + some (.calli ⟨.void, []⟩ true) := by + repeat' first | apply And.intro | rfl | (intro impossible; cases impossible) +#print axioms role_decodings + +/-- The two opcodes bind different operands. An altered token, signature or tail prefix fails. -/ +theorem pointer_encoding_checks : + let address : RawInstruction := ⟨0, 0xFE06, .call callSite⟩ + let call : RawInstruction := ⟨5, 0x29, .indirectCall .void [int32] false false 0⟩ + let identity : ManagedIdentity := ⟨"T::T.Account", 0x06000001, []⟩ + let signature : ManagedSignature := ⟨.void, [callableType int32]⟩ + valueEncodingMatches none address (.ldftn identity signature) = true ∧ + valueEncodingMatches none address (.ldftn { identity with token := 0x06000002 } signature) = + false ∧ + valueEncodingMatches none address (.ldftn identity ⟨.void, []⟩) = false ∧ + valueEncodingMatches none address (.calli signature false) = false ∧ + valueEncodingMatches none call (.ldftn identity signature) = false ∧ + valueEncodingMatches none call (.calli signature true) = false ∧ + valueEncodingMatches none call (.calli ⟨.void, []⟩ false) = false := by + decide +kernel +#print axioms pointer_encoding_checks + +/-- Closing a generic owner supplies the identity at ldftn and the parameter type at calli. -/ +theorem callable_substitution : + let site := { callSite with method.owner := .parameter 0 } + let indirect := RawOperand.indirectCall .void [.parameter 0] false false 0 + let closed := TypeExpr.object "T" "C" + Instruction.ofRaw? none ⟨0, 65030, .call site⟩ = none ∧ + ((substituteOperand [closed] [] 0 (.call site)).bind fun operand => + Instruction.ofRaw? none ⟨0, 65030, operand⟩) = + some (.ldftn ⟨"T::C", 0x06000001, []⟩ ⟨.void, [callableType int32]⟩) ∧ + ((substituteOperand [closed] [] 0 indirect).bind fun operand => + Instruction.ofRaw? none ⟨0, 41, operand⟩) = + some (.calli ⟨.void, [callableType closed]⟩ false) ∧ + Instruction.ofRaw? none ⟨0, 41, .indirectCall .void [] false false 1⟩ = none := by + decide +kernel +#print axioms callable_substitution + +/-- Taking an instance method's address cannot produce a static parameterless pointer. -/ +theorem instance_address_refused : + let site := { callSite with + declared := { convention := 0x20 }, + method := { callSite.method with name := name!"M", hasThis := true, parameters := [] }, + name := "M", parameterCount := 0 } + Instruction.ofRaw? none ⟨0, 0xfe06, .call site⟩ = none := by decide +kernel +#print axioms instance_address_refused + +/-- A reachable folded argument load names a declared argument and emits its encoded index. -/ +theorem folded_argument_index (localCount index : Nat) (argumentAt : Nat → Option StorageType) + (homeAt : Nat → Option Nat) (emitted : Instruction) : + validateArgumentOpAt localCount argumentAt homeAt (.load index) true emitted = true ↔ + (argumentAt index).isSome = true ∧ emitted = .ldarg index := by + cases found : argumentAt index <;> cases emitted <;> simp [validateArgumentOpAt, found] +#print axioms folded_argument_index + +/-- Raw instructions over every one-byte and two-byte opcode with each operand kind the value + family reads, plus text, a type, a class field and a call site; `ofRaw?` must give `none` + for every opcode outside `Instruction.valueOpcode`. -/ +def probes : List RawInstruction := + ((List.range 256 ++ (List.range 32).map (65024 + ·)).flatMap fun opcode => + ([.none, .integer 1, .argument 1, .local 1, .target 1, .targets [1], .text "", + .type (.atom runtimeScope + "System.Int32"), classOperand, .call callSite] : List RawOperand).map + fun operand => { offset := 0, opcode, operand }) + +/-- `ofRaw?` decodes only value opcodes, every value opcode is named, and every named form + decodes to exactly the instruction ECMA-335 assigns it. -/ +theorem ofRaw?_exact : + probes.all (fun raw => + (Instruction.ofRaw? none raw).isNone || Instruction.valueOpcode raw.opcode) = true ∧ + probes.all (fun raw => !Instruction.valueOpcode raw.opcode || + named.any (·.1.opcode == raw.opcode)) = true ∧ + named.all (fun (raw, expected) => valueEncodingMatches (predecessor raw) raw expected) = + true := by + decide +kernel + +/-- info: 'Tests.ValueDecoding.ofRaw?_exact' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms ofRaw?_exact + +/-- The alignments an `unaligned.` prefix states. -/ +private def alignments : List Int := [1, 2, 4] + +/-- Every indirect load and store opcode the decoder admits, with the instruction it gives. -/ +private def indirectAccesses : List (RawInstruction × Instruction) := [ + (⟨1, 70, .none⟩, .ldind .i1), (⟨1, 71, .none⟩, .ldind .u1), (⟨1, 72, .none⟩, .ldind .i2), + (⟨1, 73, .none⟩, .ldind .u2), (⟨1, 74, .none⟩, .ldind .i4), (⟨1, 75, .none⟩, .ldind .u4), + (⟨1, 76, .none⟩, .ldind .i8), (⟨1, 77, .none⟩, .ldind .i), (⟨1, 82, .none⟩, .stind .i1), + (⟨1, 83, .none⟩, .stind .i2), (⟨1, 84, .none⟩, .stind .i4), (⟨1, 85, .none⟩, .stind .i8), + (⟨1, 223, .none⟩, .stind .i)] + +/-- After `unaligned.` of each alignment, every indirect load and store decodes to the + instruction it decodes to alone, so the instruction carries no alignment. -/ +theorem unaligned_indirect_unchanged : + indirectAccesses.all (fun (raw, decoded) => alignments.all fun alignment => + Instruction.ofRaw? (some ⟨0, 65042, .integer alignment⟩) raw == some decoded) = true := by + decide +kernel +#print axioms unaligned_indirect_unchanged + +/-- `cpblk` and `initblk` decode at alignment 8 with nothing before them, at the operand of an + `unaligned.` 1, 2 or 4 before them, and at 8 after an instruction that is not a prefix, + whatever integer operand it carries. -/ +theorem block_alignments : + let copy : RawInstruction := ⟨2, 65047, .none⟩ + let fill : RawInstruction := ⟨2, 65048, .none⟩ + let aligned (alignment : Int) : Option RawInstruction := some ⟨0, 0xfe12, .integer alignment⟩ + let constant (value : Int) : Option RawInstruction := some ⟨0, 0x1f, .integer value⟩ + (Instruction.ofRaw? none copy == some (.cpblk 8) && + Instruction.ofRaw? none fill == some (.initblk 8) && + Instruction.ofRaw? (aligned 1) copy == some (.cpblk 1) && + Instruction.ofRaw? (aligned 2) copy == some (.cpblk 2) && + Instruction.ofRaw? (aligned 4) copy == some (.cpblk 4) && + Instruction.ofRaw? (aligned 1) fill == some (.initblk 1) && + Instruction.ofRaw? (aligned 2) fill == some (.initblk 2) && + Instruction.ofRaw? (aligned 4) fill == some (.initblk 4) && + Instruction.ofRaw? (constant 1) copy == some (.cpblk 8) && + Instruction.ofRaw? (constant 2) copy == some (.cpblk 8) && + Instruction.ofRaw? (constant 4) copy == some (.cpblk 8) && + Instruction.ofRaw? (constant 1) fill == some (.initblk 8) && + Instruction.ofRaw? (constant 2) fill == some (.initblk 8) && + Instruction.ofRaw? (constant 4) fill == some (.initblk 8)) = true := by + decide +kernel +#print axioms block_alignments + +/-- Only a valid unaligned prefix before an object load or store preserves its decoding. -/ +theorem object_prefixes : + let span := TypeExpr.apply (.constructor runtimeScope "System.Span`1") int32 + let pair := TypeExpr.aggregate "Pair" 0 + let types := [int32, .enum "Signed8" (.atom runtimeScope "System.SByte"), pair, span] ++ + [128, 256, 512].map (fun bits => + .apply (.constructor intrinsicsScope s!"System.Runtime.Intrinsics.Vector{bits}`1") int32) + let check := fun (operand : RawOperand) (type : TypeExpr) => + let rows : List (Nat × Option Instruction) := [(0x70, some (.cpobj type)), + (0x71, some (.ldobj type)), (0x81, some (.stobj type)), + (0xfe15, some (.initobj type)), + (0xfe1c, some (.sizeof type))] + rows.all fun (opcode, expected) => + let raw : RawInstruction := ⟨3, opcode, operand⟩ + Instruction.ofRaw? none raw == expected && alignments.all (fun alignment => + Instruction.ofRaw? (some ⟨0, 0xfe12, .integer alignment⟩) raw == + (if opcode ∈ [0x71, 0x81] then expected else none)) && + ([⟨0, 0xfe12, .integer 8⟩, ⟨0, 0xfe13, .none⟩, ⟨0, 0xfe14, .none⟩, + ⟨0, 0xfe16, .type type⟩, ⟨0, 0xfe19, .integer 1⟩, ⟨0, 0xfe1e, .none⟩] + : List RawInstruction).all (fun modifier => + (Instruction.ofRaw? (some modifier) raw).isNone) + types.all (fun type => check (.type type) type) && + (substituteOperand [pair] [] 1 (.type (.parameter 0))).any (check · pair) := by decide +kernel +#print axioms object_prefixes + +/-- A direct call retains the tail transfer or the static dispatch receiver named by its prefix. -/ +theorem direct_call_prefixes : + let raw : RawInstruction := ⟨3, 40, .call callSite⟩ + let slot := { callSite with staticVirtualInterfaceSlot := true } + valueEncodingMatches (some ⟨0, 0xFE14, .none⟩) raw (.call callSite.method { tail := true }) && + valueEncodingMatches (some ⟨0, 0xFE16, .type int32⟩) { raw with operand := .call slot } + (.call callSite.method { constrained := some int32 }) := by + decide +kernel +#print axioms direct_call_prefixes + +/-- Both constrained call forms decode to the prefix pc and the slot with its receiver. -/ +theorem constrained_call_windows : + let modifier : RawInstruction := ⟨0, 0xFE16, .type int32⟩ + let staticSite := { callSite with staticVirtualInterfaceSlot := true } + Instruction.ofRawCode none [modifier, ⟨6, 111, .call instanceSite⟩] = + [.nop, .call instanceSite.method { constrained := some int32 }] ∧ + Instruction.ofRawCode none [modifier, ⟨6, 40, .call staticSite⟩] = + [.nop, .call callSite.method { constrained := some int32 }] := by + exact ⟨rfl, rfl⟩ +#print axioms constrained_call_windows + +/-- Invalid constrained windows fail the method prefix check, including at the end of a body. -/ +theorem invalid_constrained_windows : + let modifier : RawInstruction := ⟨0, 0xFE16, .type int32⟩ + let instanceCall : RawInstruction := ⟨6, 111, .call instanceSite⟩ + let staticSite := { callSite with staticVirtualInterfaceSlot := true } + let windows := [[modifier, ⟨6, 111, .call staticSite⟩], + [modifier, ⟨6, 40, .call instanceSite⟩], + [modifier, ⟨6, 0, .none⟩], [⟨0, 0xFE1E, .none⟩, modifier, instanceCall], [modifier], + [{ modifier with operand := .integer 1 }, instanceCall], + [⟨0, 0xFE14, .none⟩, modifier, instanceCall, ⟨11, 42, .none⟩]] + windows.all (fun code => !rawPrefixesValid .void code) && + (Instruction.ofRaw? (some ⟨0, 0xFE14, .none⟩) instanceCall).isNone := by + decide +kernel +#print axioms invalid_constrained_windows + +/-- Suppressed checks have no scalar emission for any byte operand. -/ +theorem no_prefix_refused : + (List.range 256).all (fun flags => + !scalarEmissionMatches none none ⟨0, 0xFE19, .integer flags⟩ [] .nop) = true := by + decide +kernel +#print axioms no_prefix_refused + +/-- Reserved check flags cannot authorize an ordinary instance field load. -/ +theorem malformed_field_prefix_refused : + let previous := some (⟨0, 0xFE19, .integer 255⟩ : RawInstruction) + [(classOperand, classField, ScalarKind.object), (structOperand, structField, .reference)].all + (fun (operand, field, receiver) => + let raw : RawInstruction := ⟨3, 123, operand⟩ + (Instruction.ofRaw? previous raw).isNone && + !scalarEmissionMatches none previous raw [receiver] (.ldfld field)) := by + decide +kernel +#print axioms malformed_field_prefix_refused + +/-- A typed indirect load or store binds both its type token and its preceding alignment. -/ +theorem indirect_object_binding : + let other := TypeExpr.atom runtimeScope "System.UInt64" + let unaligned := fun (n : Int) => some (RawInstruction.mk 0 0xfe12 (.integer n)) + [(0x71, Instruction.ldobj), (0x81, Instruction.stobj)].all fun (opcode, operation) => + let raw := RawInstruction.mk 3 opcode (.type int32) + indirectAccessMatches none raw (operation int32) && + [1, 2, 4].all (fun n => indirectAccessMatches (unaligned n) raw (operation int32)) && + !indirectAccessMatches none raw (operation other) && + !indirectAccessMatches none ⟨3, 0, .none⟩ (operation int32) && + !indirectAccessMatches (unaligned 8) raw (operation int32) := by decide +kernel +#print axioms indirect_object_binding + +/-- A forged operand on a block copy cannot emit the aligned copy at its taker. -/ +theorem malformed_block_operand_refused : + !indirectAccessMatches (some ⟨0, 0xFE12, .integer 1⟩) + ⟨3, 0xFE17, .integer 1⟩ (.cpblk 1) := by + decide +kernel +#print axioms malformed_block_operand_refused + +/-- Supported memory and call prefixes pass whole-body validation with their takers. -/ +theorem valid_prefix_sequences : + let ret : RawInstruction := ⟨8, 42, .none⟩ + let call : RawInstruction := ⟨3, 40, .call callSite⟩ + let instanceSite := { callSite with method := { callSite.method with hasThis := true } } + let tailCall : RawInstruction := ⟨0, 0xFE14, .none⟩ + let constrainedCall : RawInstruction := ⟨0, 0xFE16, .type int32⟩ + alignments.all (fun alignment => indirectAccesses.all fun (raw, _) => + prefixSequenceValid .void [⟨0, 0xFE12, .integer alignment⟩, raw]) && + prefixSequenceValid .void [⟨0, 0xFE12, .integer 2⟩, ⟨3, 123, classOperand⟩] && + prefixSequenceValid .void [⟨0, 0xFE1E, .none⟩, ⟨2, 143, .type int32⟩] && + prefixSequenceValid .void [tailCall, call, ret] && + prefixSequenceValid .void [tailCall, ⟨2, 41, .indirectCall .void [] false false 0⟩, ret] && + prefixSequenceValid .void [constrainedCall, + { call with operand := .call { callSite with staticVirtualInterfaceSlot := true } }] && + prefixSequenceValid .void [constrainedCall, ⟨6, 111, .call instanceSite⟩] := by + decide +kernel +#print axioms valid_prefix_sequences + +/-- A class field and a struct field, each with the reference it decodes to. -/ +private def fieldOperands : List (RawOperand × FieldRef) := + [(classOperand, classField), (structOperand, structField)] + +/-- After `unaligned.` of each alignment, `ldfld` and `stfld` of a class or a struct field decode + to the instruction they decode to alone, so a field access carries no alignment. -/ +theorem unaligned_fields_unchanged : + fieldOperands.all (fun (operand, field) => alignments.all fun alignment => + Instruction.ofRaw? (some ⟨0, 65042, .integer alignment⟩) ⟨1, 123, operand⟩ == + some (.ldfld field) && + Instruction.ofRaw? (some ⟨0, 65042, .integer alignment⟩) ⟨1, 125, operand⟩ == + some (.stfld field)) = true := by + decide +kernel +#print axioms unaligned_fields_unchanged + +/-- `ldflda` after `unaligned.`, which ECMA-335 does not let that prefix precede, and `ldfld`, + `ldflda` and `stfld` after `volatile.` do not decode, on a class or a struct field. -/ +theorem prefixed_fields_refused : + fieldOperands.all (fun (operand, _) => + alignments.all (fun alignment => + (Instruction.ofRaw? (some ⟨0, 65042, .integer alignment⟩) ⟨1, 124, operand⟩).isNone) && + [123, 124, 125].all fun opcode => + (Instruction.ofRaw? (some ⟨0, 65043, .none⟩) ⟨1, opcode, operand⟩).isNone) = true := by + decide +kernel +#print axioms prefixed_fields_refused + +/-- The `readonly.` prefix with no operand, the form it takes. -/ +private def readOnlyPrefix : RawInstruction := ⟨0, 65054, .none⟩ + +/-- Each prefix other than `readonly.`, with an operand of the form it takes. -/ +private def otherPrefixes : List RawInstruction := + [⟨0, 65042, .integer 1⟩, ⟨0, 65043, .none⟩, ⟨0, 65044, .none⟩, ⟨0, 65046, .type int32⟩, + ⟨0, 65049, .integer 1⟩] + +/-- `ldelema` records whether `readonly.` precedes it and decodes after no other prefix, and + `readonly.` decodes to `nop` after no prefix at all. -/ +theorem prefixed_address : + (Instruction.ofRaw? (some readOnlyPrefix) ⟨1, 143, .type int32⟩ == + some (.ldelema int32 true) && + otherPrefixes.all (fun before => + (Instruction.ofRaw? (some before) ⟨1, 143, .type int32⟩).isNone) && + (readOnlyPrefix :: otherPrefixes).all (fun before => + (Instruction.ofRaw? (some before) ⟨1, 65054, .none⟩).isNone)) = true := by + decide +kernel +#print axioms prefixed_address + +/-- After `readonly.`, no probe decodes except `ldelema`, whatever its operand. -/ +theorem readOnly_only_address : + probes.all (fun raw => raw.opcode == 143 || + (Instruction.ofRaw? (some readOnlyPrefix) raw).isNone) = true := by + decide +kernel +#print axioms readOnly_only_address + +/-- Emitted instructions that differ from what the raw instruction decodes to: a constant out of + its form's range, another operand, or another constructor with the same stack effect. -/ +def mutations : List (RawInstruction × Instruction) := [ + -- Constants just past the range of their form, far past it, or of another width. + (⟨0, 31, .integer 128⟩, .ldc_i4 128), + (⟨0, 32, .integer (-(2^31) - 1)⟩, .ldc_i4 (BitVec.ofInt 32 (-(2^31) - 1))), + (⟨0, 33, .integer (-(2^63) - 1)⟩, .ldc_i8 (BitVec.ofInt 64 (-(2^63) - 1))), + (⟨0, 31, .integer (-129)⟩, .ldc_i4 (BitVec.ofInt 32 (-129))), + (⟨0, 32, .integer (2^31)⟩, .ldc_i4 (BitVec.ofInt 32 (2^31))), + (⟨0, 33, .integer (2^63)⟩, .ldc_i8 (BitVec.ofInt 64 (2^63))), + (⟨0, 33, .integer (-1)⟩, .ldc_i4 (BitVec.ofInt 32 (-1))), + -- Another constant, argument, local or target than the operand names. + (⟨0, 25, .none⟩, .ldc_i4 2), (⟨0, 31, .integer (-5)⟩, .ldc_i4 5), + (⟨0, 16, .argument 1⟩, .starg 2), (⟨0, 65034, .argument 300⟩, .ldarga 299), + (⟨0, 6, .none⟩, .ldloc 1), (⟨0, 65036, .local 300⟩, .ldloc 299), + (⟨0, 43, .target 1⟩, .br 2), (⟨0, 69, .targets [1, 2]⟩, .switch [2, 1]), + (⟨0, 69, .targets [1, 2]⟩, .switch [1, 2, 3]), + -- Another constructor with the same stack effect. + (⟨0, 88, .none⟩, .sub), (⟨0, 38, .none⟩, .stloc 0), (⟨0, 19, .local 4⟩, .ldloc 4), + (⟨0, 18, .local 4⟩, .ldloc 4), (⟨0, 16, .argument 4⟩, .stloc 4), + (⟨0, 15, .argument 4⟩, .ldloca 4), (⟨0, 58, .target 1⟩, .brfalse 1), + (⟨0, 138, .none⟩, .conv_ovf_un .i8), (⟨0, 211, .none⟩, .conv_i8), + (⟨0, 130, .none⟩, .conv_ovf .i1), (⟨0, 215, .none⟩, .add_ovf), (⟨0, 65027, .none⟩, .cgt), + (⟨0, 144, .none⟩, .ldelem .u1), (⟨0, 144, .none⟩, .ldelem .i2), + (⟨0, 148, .none⟩, .ldelem .u4), (⟨0, 151, .none⟩, .ldelem .i8), (⟨0, 155, .none⟩, .stelem .i8), + (⟨0, 154, .none⟩, .ldelem .i), (⟨0, 162, .none⟩, .stelem .i), (⟨0, 74, .none⟩, .ldelem .i4), + (⟨0, 84, .none⟩, .stelem .i4), (⟨0, 148, .none⟩, .ldind .i4), (⟨0, 142, .none⟩, .ldind .i), + -- A local or argument index wider than its form encodes. + (⟨0, 17, .local 256⟩, .ldloc 256), (⟨0, 65036, .local 65536⟩, .ldloc 65536), + (⟨0, 14, .argument 256⟩, .ldarg 256), (⟨0, 65033, .argument 65536⟩, .ldarg 65536), + (⟨0, 15, .argument 256⟩, .ldarga 256), (⟨0, 65034, .argument 65536⟩, .ldarga 65536), + (⟨0, 16, .argument 256⟩, .starg 256), (⟨0, 65035, .argument 65536⟩, .starg 65536), + -- A malformed operand, another argument, an argument address, or an opcode outside the family. + (⟨0, 42, .integer 0⟩, .ret), (⟨0, 17, .argument 4⟩, .ldloc 4), (⟨0, 6, .local 0⟩, .ldloc 0), + (⟨0, 43, .targets [1]⟩, .br 1), (⟨0, 2, .none⟩, .ldarg 1), (⟨0, 15, .argument 1⟩, .ldarg 1), + (⟨0, 107, .none⟩, .conv_i4), (⟨0, 65039, .integer 0⟩, .localloc), + (⟨0, 116, .none⟩, .castclass dog), (⟨0, 20, .type dog⟩, .ldnull), (⟨0, 20, .none⟩, .ldc_i4 0), + (⟨0, 142, .integer 0⟩, .ldlen), (⟨0, 154, .type dog⟩, .ldelem_ref), + -- Another element type than the token names, the other direction, or a missing token. + (⟨0, 141, .type int32⟩, .newarr dog), (⟨0, 163, .type int32⟩, .ldelem_any dog), + (⟨0, 163, .type int32⟩, .stelem_any int32), (⟨0, 164, .type int32⟩, .ldelem_any int32), + (⟨0, 143, .type int32⟩, .ldelema dog false), (⟨0, 143, .type int32⟩, .ldelema int32 true), + (⟨0, 143, .none⟩, .ldelema int32 false), (⟨0, 65054, .integer 0⟩, .nop), + (⟨0, 163, .none⟩, .ldelem_any int32), (⟨0, 148, .none⟩, .ldelem_any int32), + (⟨0, 158, .none⟩, .stelem_any int32), (⟨0, 141, .none⟩, .newarr int32), + -- Another field, another access to the field, a static field or another owner. + (⟨0, 123, classOperand⟩, .ldfld { classField with index := 2 }), + (⟨0, 123, classOperand⟩, .ldfld { classField with name := name!"limit" }), + (⟨0, 123, classOperand⟩, .ldflda classField), (⟨0, 125, classOperand⟩, .ldfld classField), + (⟨0, 123, .field (.object "T" "T.Account") 1 (.atom runtimeScope "System.Int32") + name!"balance" true⟩, .ldfld classField), + (⟨0, 123, structOperand⟩, .ldfld classField), (⟨0, 123, classOperand⟩, .ldfld structField), + (⟨0, 125, structOperand⟩, + .stfld { structField with type := .atom runtimeScope "System.UInt32" }), + -- Another method or constructor than the site names, a prefix the bytes do not hold, or a call + -- operand under another opcode. + (⟨0, 40, .call callSite⟩, .call { callSite.method with name := name!"Withdraw" }), + (⟨0, 40, .call callSite⟩, .call callSite.method { tail := true }), + (⟨0, 40, .call callSite⟩, .call callSite.method { constrained := some dog }), + (⟨0, 40, .none⟩, .call callSite.method), + (⟨0, 111, .call callSite⟩, .call callSite.method), + (⟨0, 115, .call accountConstruction⟩, + .newobj { accountConstruction.method with parameters := [] }), + (⟨0, 115, .call accountConstruction⟩, + .newobj { accountConstruction.method with owner := .object "T" "T.Other" }), + (⟨0, 40, .call accountConstruction⟩, .newobj accountConstruction.method)] + +/-- No mutation matches its raw instruction. -/ +theorem mutations_rejected : + mutations.all (fun (raw, emitted) => !valueEncodingMatches none raw emitted) = true := by + decide +kernel + +/-- info: 'Tests.ValueDecoding.mutations_rejected' depends on axioms: [propext] -/ +#guard_msgs in +#print axioms mutations_rejected + +/-- An int32 array of the extraction's stack kinds. -/ +private def words : ScalarKind := .array .signed32 + +/-- An array opcode the decoder reads emits only its decoding: the managed table length is + refused, an opcode of a kind emits no typed form, and a typed opcode emits its token's element + type rather than a kind. -/ +theorem element_emission_exact : + scalarEmissionMatches none none ⟨0, 148, .none⟩ [.word32, words] (.ldelem .i4) = true ∧ + scalarEmissionMatches none none ⟨0, 158, .none⟩ [.word32, .word32, words] (.stelem .i4) = + true ∧ + scalarEmissionMatches none none ⟨0, 142, .none⟩ [words] .ldlen = true ∧ + scalarEmissionMatches none none ⟨0, 163, .type int32⟩ [.word32, words] + (.ldelem_any int32) = + true ∧ + scalarEmissionMatches none none ⟨0, 164, .type int32⟩ [.word32, .word32, words] + (.stelem_any int32) = true ∧ + scalarEmissionMatches none none ⟨0, 148, .none⟩ [.word32, words] + (.ldelem_any int32) = false ∧ + scalarEmissionMatches none none ⟨0, 158, .none⟩ [.word32, .word32, words] + (.stelem_any int32) = false ∧ + scalarEmissionMatches none none ⟨0, 148, .none⟩ [.word32, words] (.ldelem .u4) = false ∧ + scalarEmissionMatches none none ⟨0, 142, .none⟩ [words] (.ldind .i) = false ∧ + scalarEmissionMatches none none ⟨0, 163, .type int32⟩ [.word32, words] + (.ldelem .i4) = false ∧ + scalarEmissionMatches none none ⟨0, 164, .type int32⟩ [.word32, .word32, words] + (.stelem .i4) = false := by + decide +kernel + +/-- info: 'Tests.ValueDecoding.element_emission_exact' depends on axioms: [propext, Quot.sound] -/ +#guard_msgs in +#print axioms element_emission_exact + +/-- A decided match gives the decoded instruction, sign extension of a short constant included. -/ +theorem short_constant_decoded : + Instruction.ofRaw? none ⟨0, 31, .integer (-5)⟩ = some (.ldc_i4 (BitVec.ofInt 32 (-5))) := + valueEncodingMatches_sound (by decide +kernel) + +/-- info: 'Tests.ValueDecoding.short_constant_decoded' depends on axioms: [propext, Quot.sound] -/ +#guard_msgs in +#print axioms short_constant_decoded + +/-- info: 'CIL.Instruction.valueOpcode_of_ofRaw?' depends on axioms: [propext, Quot.sound] -/ +#guard_msgs in +#print axioms Instruction.valueOpcode_of_ofRaw? + +/-- info: 'CIL.Instruction.sameValue_eq' depends on axioms: [propext, Quot.sound] -/ +#guard_msgs in +#print axioms Instruction.sameValue_eq + +/-- info: 'CIL.Extraction.valueEncodingMatches_sound' depends on axioms: [propext, Quot.sound] -/ +#guard_msgs in +#print axioms valueEncodingMatches_sound + +/-- +info: 'CIL.Instruction.ofRaw?_substitute' depends on axioms: + [propext, Classical.choice, Quot.sound] +-/ +#guard_msgs (whitespace := lax) in +#print axioms Instruction.ofRaw?_substitute + +/-- Instantiating the type operand of a refused isinst preserves its refusal. -/ +theorem substitute_refused_type : + let previous := some (⟨0, 0xFE1E, .none⟩ : RawInstruction) + let raw : RawInstruction := ⟨2, 117, .type (.parameter 0)⟩ + Instruction.ofRaw? previous raw = none ∧ + Instruction.ofRaw? previous { raw with operand := .type int32 } = none := by + intro previous raw + constructor + · rfl + · exact Instruction.ofRaw?_substitute (previous := previous) (raw := raw) + (operand := .type int32) (arguments := [int32]) (layouts := []) (arity := 1) rfl rfl +#print axioms substitute_refused_type + +/-- `classOperand` holding the first generic argument instead of `int32`. -/ +private def genericOperand : RawOperand := + .field (.object "T" "T.Account") 1 (.parameter 0) name!"balance" false + +-- Instantiating a class field with `int32` decodes to the access of `classField`. +example : ((substituteOperand [.atom runtimeScope "System.Int32"] [] 0 genericOperand).bind + (fun operand => Instruction.ofRaw? none ⟨0, 123, operand⟩)).any + (·.sameValue (.ldfld classField)) = true := by + decide +kernel + +/-- `structOperand` holding the first generic argument instead of `int32`. -/ +private def genericStructOperand : RawOperand := + .field (.aggregate "T::Pair" 0) 1 (.parameter 0) name!"balance" false + +-- Instantiating a struct field with `int32` decodes to the access of `structField`. +example : ((substituteOperand [.atom runtimeScope "System.Int32"] [] 0 genericStructOperand).bind + (fun operand => Instruction.ofRaw? none ⟨0, 125, operand⟩)).any + (·.sameValue (.stfld structField)) = true := by + decide +kernel + +/-- +info: 'CIL.Instruction.ofRaw?_substitute_instance_field' depends on axioms: + [propext, Classical.choice, Quot.sound] +-/ +#guard_msgs (whitespace := lax) in +#print axioms Instruction.ofRaw?_substitute_instance_field + +/-- The constructor at the head of each decoded named opcode, failing on any `none`. -/ +private def decodedHeads : MetaM (List Name) := do + let mut list ← whnf (mkConst ``decodedNamed) + let mut heads := [] + while list.isAppOfArity ``List.cons 3 do + let value ← whnf (list.getArg! 1) + unless value.isAppOfArity ``Option.some 2 do + throwError "a named opcode does not decode: {value}" + let some name := (← whnf (value.getArg! 1)).getAppFn.constName? + | throwError "decoded value has no constructor head" + heads := name :: heads + list ← whnf (list.getArg! 2) + return heads + +/-- info: decoded 96, undecoded 5 of 101 constructors -/ +#guard_msgs in +run_meta do + let constructors := (← getConstInfoInduct ``Instruction).ctors + let decoded := (← decodedHeads).eraseDups + let missing := constructors.filter fun name => !decoded.contains name && !undecoded.contains name + let overlap := undecoded.filter decoded.contains + let stale := undecoded.filter fun name => !constructors.contains name + unless missing.isEmpty && overlap.isEmpty && stale.isEmpty do + throwError "unplaced constructors {missing}; listed but decoded {overlap}; \ + listed but absent {stale}" + unless decoded.length + undecoded.length == constructors.length do + throwError "decoded {decoded.length} + listed {undecoded.length} != {constructors.length}" + logInfo m!"decoded {decoded.length}, undecoded {undecoded.length} of \ + {constructors.length} constructors" + +end Tests.ValueDecoding diff --git a/Tests/VectorFields.cs b/Tests/VectorFields.cs index 055c225cc..8278f4937 100644 --- a/Tests/VectorFields.cs +++ b/Tests/VectorFields.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs using System.Reflection; @@ -68,6 +73,9 @@ void Prove(string name, List claims, string definitions = "") foreach ((string name, string holder, int width, bool write) in new[] { ("Read128", "Holder128", 16, false), ("Read256", "Holder256", 32, false), + // Each other admitted lane, so a swapped lane tag in the extractor fails against Lean. + ("ReadBytes128", "Bytes128", 16, false), ("ReadInts128", "Ints128", 16, false), + ("ReadUInts256", "UInts256", 32, false), ("ReadLongs256", "Longs256", 32, false), ("Write128", "Holder128", 16, true), ("Write256", "Holder256", 32, true), ("WritePacked", "Packed256", 32, true), ("Reinterpret", "Words256", 32, true) }) @@ -96,12 +104,14 @@ void Prove(string name, List claims, string definitions = "") if (holder != "Words256") { claims.Add($"Extracted.layout0.size = {size}"); - claims.Add($"Extracted.method0.code.any (fun op => match op with | .fieldAt actualOffset actualWidth _ => actualOffset == {offset} && actualWidth == {width} | _ => false) = true"); + claims.Add($"""Extracted.environment.fieldShape Extracted.fieldRef0 = .value {offset} {width} false"""); } Prove(name, claims, definitions); object instance = Activator.CreateInstance(type)!; - object vector = width == 16 ? (object)Vector128.Create(0x1122334455667788UL) : Vector256.Create(0x1122334455667788UL); + Type element = owner.GetMethod(name)!.ReturnType.GetGenericArguments()[0]; + object vector = (width == 16 ? typeof(Vector128) : typeof(Vector256)).GetMethod("Create", [element])! + .Invoke(null, [Convert.ChangeType(0x5A, element)])!; if (write) { object[] arguments = [instance, vector]; @@ -175,33 +185,47 @@ void Prove(string name, List claims, string definitions = "") } string baseline = File.ReadAllText(Path.Combine(root, "WritePacked", "Extracted.lean")); -foreach ((string name, string changed) in new[] +// A field operand naming a field the struct lacks resolves to no layout entry and matches no decoded operand. +foreach ((string name, string changed, string marker) in new[] { - ("field-offset", baseline.Replace(".fieldAt 3 32", ".fieldAt 2 32")), - ("vector-storage", baseline.Replace("type := .vector256", "type := .unsigned64")), - ("computed-width", baseline.Replace("⟨3, 32, false⟩", "⟨3, 16, false⟩")), - ("omitted-uses", Regex.Replace(baseline, - @"def method0LayoutUses : List Extraction.LayoutUse := [^\r\n]*", - "def method0LayoutUses : List Extraction.LayoutUse := []")) + ("field-index", baseline.Replace("index := 0, type := .apply", "index := 1, type := .apply"), + "the decoded method has no field operand"), + ("vector-storage", baseline.Replace("type := (.vector256 .unsigned64)", "type := .unsigned64"), "decide"), + ("vector-lane", baseline.Replace("type := (.vector256 .unsigned64)", "type := (.vector256 .signed64)"), "decide"), + ("computed-width", baseline.Replace("⟨3, 32, false⟩", "⟨3, 16, false⟩"), "decide") }) { - if (changed == baseline) throw new InvalidOperationException("Vector evidence mutation did not change source: " + name); - string path = Path.Combine(root, "Evidence-" + name + ".lean"); + if (changed == baseline) + throw new InvalidOperationException($"""Vector evidence mutation did not change source: {name}"""); + string path = Path.Combine(root, $"""Evidence-{name}.lean"""); File.WriteAllText(path, changed); - (int Code, string Output, string Stdout, string Stderr) result = Execute(["lake", "env", "lean", path], cwd: FixturePackage()); - if (result.Code == 0 || !result.Output.Contains("decide")) - throw new InvalidOperationException("Malformed vector field evidence accepted: " + name + "\n" + result.Output); - Console.WriteLine("PASS vector field evidence rejection: " + name); + (int Code, string Output, string Stdout, string Stderr) result = + Execute(["lake", "env", "lean", path], cwd: FixturePackage()); + if (result.Code == 0 || !result.Output.Contains(marker)) + throw new InvalidOperationException($""" + Malformed vector field evidence accepted: {name} + {result.Output} + """); + Console.WriteLine($"""PASS vector field evidence rejection: {name}"""); } string architectureBaseline = File.ReadAllText(Path.Combine(root, "Read256", "Extracted.lean")); string otherArchitecture = RuntimeInformation.ProcessArchitecture == Architecture.Arm64 ? "x64" : "arm64"; -string mismatched = Regex.Replace(architectureBaseline, @"def profile : FeatureProfile := [^\r\n]*", - "def profile : FeatureProfile := { architecture := ." + otherArchitecture + " }"); +string mismatched = ProfileDefinition.Replace(architectureBaseline, + $"def profile : FeatureProfile := {{ architecture := .{otherArchitecture} }}"); if (mismatched == architectureBaseline) throw new InvalidOperationException("Architecture mutation did not change source"); string architecturePath = Path.Combine(root, "Evidence-architecture.lean"); File.WriteAllText(architecturePath, mismatched); (int Code, string Output, string Stdout, string Stderr) architectureResult = Execute(["lake", "env", "lean", architecturePath], cwd: FixturePackage()); -if (architectureResult.Code == 0 || !architectureResult.Output.Contains("decide")) - throw new InvalidOperationException("Vector layout accepted under a different method architecture\n" + architectureResult.Output); +// The layout table is certified at the extraction's architecture, so another profile cannot use that proof. +if (architectureResult.Code == 0 || !architectureResult.Output.Contains("LayoutsValid", StringComparison.Ordinal)) + throw new InvalidOperationException( + $"Vector layout accepted under a different method architecture\n{architectureResult.Output}"); Console.WriteLine("PASS vector field evidence rejects method architecture mismatch"); + +partial class Program +{ + /// The generated feature profile definition, to the end of its line. + [GeneratedRegex(@"def profile : FeatureProfile := [^\r\n]*")] + internal static partial Regex ProfileDefinition { get; } +} diff --git a/Tests/VectorFields.lean b/Tests/VectorFields.lean index 55f4cc983..9cb75e628 100644 --- a/Tests/VectorFields.lean +++ b/Tests/VectorFields.lean @@ -1,5 +1,6 @@ import CIL.Safety.InstructionMemory import CIL.Extraction.LayoutCoverage +import CIL.Resolution import Tests.Support namespace Tests.VectorFields @@ -15,45 +16,59 @@ def memory : CIL.Safety.Memory := { views := [⟨0, 0, 35, true, true⟩] } example : (do - let (stored, _) ← instruction (.setFieldAt 3 32) + let (stored, _) ← storeValueField 3 32 [.scalar (.snapshot .vector256 cells), Tests.reference] memory - instruction (.fieldAt 3 32) [Tests.reference] stored).map Prod.snd = + loadValueField 3 32 false [Tests.reference] stored).map Prod.snd = .ok [.scalar (.snapshot .vector256 cells)] := by rfl -example : instruction (.fieldAt 3 32) +example : loadValueField 3 32 false [.scalar (.snapshot (.aggregate 35) ([(0, true), (0, true), (0, true)] ++ cells))] memory = .ok (memory, [.scalar (.snapshot .vector256 cells)]) := by rfl -example : instruction (.fieldAt 0 32) +example : loadValueField 0 32 false [.scalar (.snapshot (.aggregate 35) cells)] memory = .error .invalidState := by rfl -example : instruction (.fieldAt 4 32) +example : loadValueField 4 32 false [.scalar (.aggregate (List.replicate 35 0))] memory = .error .invalidState := by rfl -example : instruction (.setFieldAt 3 32) [.scalar (.v128 0), Tests.reference] memory = +example : storeValueField 3 32 [.scalar (.v128 0), Tests.reference] memory = .error .invalidState := by rfl -example : instruction (.setFieldAt 4 32) [.scalar (.v256 0), Tests.reference] memory = +example : storeValueField 4 32 [.scalar (.v256 0), Tests.reference] memory = .error (.memory .outsideAllocation (.address ⟨0, 4⟩) 32) := by rfl -example : instruction (.setFieldAt 3 32) [.scalar (.v256 0), .reference .null] memory = +example : storeValueField 3 32 [.scalar (.v256 0), .reference .null] memory = .error (.memory .nullDereference .null 0) := by rfl -example : computeLayouts [{ explicit := true, packing := 1, declaredSize := 35, fields := [⟨.vector256, some 3⟩] }] = +example : computeLayouts [{ + explicit := true, packing := 1, declaredSize := 35, + fields := [⟨.vector256 .unsigned64, some 3⟩] }] = some #[⟨⟨35, [⟨3, 32, false⟩]⟩, 1⟩] := by decide -example : computeLayouts [{ explicit := true, packing := 1, declaredSize := 34, fields := [⟨.vector256, some 3⟩] }] = none := by decide +example : computeLayouts [{ + explicit := true, packing := 1, declaredSize := 34, + fields := [⟨.vector256 .unsigned64, some 3⟩] }] = none := by decide -example : computeLayouts [{ explicit := false, auto := true, packing := 0, declaredSize := -1, fields := [⟨.vector256, none⟩] }] = none := by decide +example : computeLayouts [{ + explicit := false, auto := true, packing := 0, declaredSize := -1, + fields := [⟨.vector256 .unsigned64, none⟩] }] = none := by decide -example : computeLayouts [{ explicit := false, packing := 32, declaredSize := -1, fields := [⟨.vector256, none⟩] }] = none := by decide +example : computeLayouts [{ + explicit := false, packing := 32, declaredSize := -1, + fields := [⟨.vector256 .unsigned64, none⟩] }] = none := by decide -example : computeLayouts [{ explicit := false, packing := 0, declaredSize := -1, fields := [⟨.vector256, none⟩] }] = none := by decide +example : computeLayouts [{ + explicit := false, packing := 0, declaredSize := -1, + fields := [⟨.vector256 .unsigned64, none⟩] }] = none := by decide -example : computeLayouts [{ explicit := false, packing := 0, declaredSize := -1, fields := [⟨.unsigned8, none⟩, ⟨.vector128, none⟩] }] = +example : computeLayouts [{ + explicit := false, packing := 0, declaredSize := -1, + fields := [⟨.unsigned8, none⟩, ⟨.vector128 .unsigned64, none⟩] }] = some #[⟨⟨32, [⟨0, 1, false⟩, ⟨16, 16, false⟩]⟩, 16⟩] := by decide -def defaultVector256 : List RawLayout := [{ explicit := false, packing := 0, declaredSize := -1, fields := [⟨.unsigned8, none⟩, ⟨.vector256, none⟩] }] +def defaultVector256 : List RawLayout := [{ + identity := "VectorHolder", explicit := false, packing := 0, declaredSize := -1, + fields := [⟨.unsigned8, none⟩, ⟨.vector256 .unsigned64, none⟩] }] def x64Layout : Array ComputedLayout := #[⟨⟨64, [⟨0, 1, false⟩, ⟨32, 32, false⟩]⟩, 32⟩] def arm64Layout : Array ComputedLayout := #[⟨⟨48, [⟨0, 1, false⟩, ⟨16, 32, false⟩]⟩, 16⟩] @@ -63,11 +78,28 @@ example : LayoutsValid defaultVector256 arm64Layout .arm64 := by decide example : ¬ LayoutsValid defaultVector256 x64Layout .arm64 := by decide example : ¬ LayoutsValid defaultVector256 arm64Layout .x64 := by decide -example : MethodLayoutsValid defaultVector256 x64Layout - { profile := { architecture := .x64 }, locals := [], returnsValue := true, code := [.fieldAt 32 32] } [.field 0 0 1 .load] := by decide - -example : ¬ MethodLayoutsValid defaultVector256 x64Layout - { profile := { architecture := .arm64 }, locals := [], returnsValue := true, code := [.fieldAt 32 32] } [.field 0 0 1 .load] := by decide +/-- A typed vector field has the same instruction operand under both runtime profiles. -/ +private def lanes : FieldRef := { + owner := .aggregate "VectorHolder" 0, index := 1, name := name!"lanes", + type := .apply (.constructor intrinsicsScope "System.Runtime.Intrinsics.Vector256`1") + (.atom runtimeScope "System.UInt64") } + +private def vectorLoad (architecture : Architecture) (layouts : Array ComputedLayout) : Method := + { profile := { architecture }, environment := { rawLayouts := defaultVector256, layouts }, + locals := [], returnsValue := true, code := [.ldfld lanes] } + +/-- The same load resolves byte 32 on x64 and byte 16 on ARM64. Swapping the checked table + invalidates the method layout for each profile. -/ +theorem vector_load_profiles : + let x64 := vectorLoad .x64 x64Layout + let arm := vectorLoad .arm64 arm64Layout + MethodLayoutsValid defaultVector256 x64Layout x64 [] ∧ + MethodLayoutsValid defaultVector256 arm64Layout arm [] ∧ + x64.fieldShape lanes = .value 32 32 false ∧ arm.fieldShape lanes = .value 16 32 false ∧ + ¬ MethodLayoutsValid defaultVector256 x64Layout (vectorLoad .arm64 x64Layout) [] ∧ + ¬ MethodLayoutsValid defaultVector256 arm64Layout (vectorLoad .x64 arm64Layout) [] := by + decide +kernel +#print axioms vector_load_profiles def nestedVector256 : List RawLayout := defaultVector256 ++ [{ explicit := false, packing := 0, declaredSize := -1, fields := [⟨.unsigned8, none⟩, ⟨.aggregate 0, none⟩] }] @@ -78,21 +110,27 @@ example : computeLayouts nestedVector256 .x64 = example : computeLayouts nestedVector256 .arm64 = some (arm64Layout.push ⟨⟨64, [⟨0, 1, false⟩, ⟨16, 48, true⟩]⟩, 16⟩) := by decide -def raw : List RawLayout := [{ identity := "VectorHolder", explicit := true, packing := 1, declaredSize := 35, fields := [⟨.vector256, some 3⟩] }] +def raw : List RawLayout := [{ + identity := "VectorHolder", explicit := true, packing := 1, + declaredSize := 35, fields := [⟨.vector256 .unsigned64, some 3⟩] }] def vector (scope : String) : TypeExpr := .apply (.constructor scope "System.Runtime.Intrinsics.Vector256`1") - (.atom "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" "System.UInt64") + (.atom "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" + "System.UInt64") example : instructionLayoutUses raw 0 ⟨0, 0x7b, .field (.aggregate "VectorHolder" 0) 0 - (vector "System.Runtime.Intrinsics, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51") false⟩ = - some [.field 0 0 0 .load] := by decide + (vector "System.Runtime.Intrinsics, Version=11.0.0.0, Culture=neutral, \ + PublicKeyToken=cc7b13ffcd2ddd51") + name!"value" false⟩ = + some [] := by decide example : instructionLayoutUses raw 0 ⟨0, 0x7b, .field (.aggregate "VectorHolder" 0) 0 - (vector "Spoofed.Intrinsics") false⟩ = none := by decide + (vector "Spoofed.Intrinsics") name!"value" false⟩ = none := by decide example : instructionLayoutUses raw 0 ⟨0, 0x7b, .field (.aggregate "VectorHolder" 0) 0 - (.atom "System.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" "System.UInt64") - false⟩ = none := by decide + (.atom "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" + "System.UInt64") + name!"value" false⟩ = none := by decide end Tests.VectorFields diff --git a/Tests/VectorLocalCalls.lean b/Tests/VectorLocalCalls.lean index 5163d78e1..99016a326 100644 --- a/Tests/VectorLocalCalls.lean +++ b/Tests/VectorLocalCalls.lean @@ -1,6 +1,8 @@ import CIL.Safety.LocalCalls import CIL.Safety.LoopScaffold import CIL.SIMD.Evaluation256Lemmas +import Tests.Support +import Tests.Support.IntrinsicMethods /-! # Vector helper output in mixed private storage @@ -16,8 +18,8 @@ open CIL.Safety /-- The helper adds four pairs of lanes and writes the vector through its output argument. -/ @[cil_code] def helper : CIL.Method := { locals := [], returnsValue := false, - code := [.arg 2, .arg 0, .arg 1, .intrinsic (.vector (.add64 256)) 2, - .memory .store256, .ret] } + code := [.ldarg 2, .ldarg 0, .ldarg 1, .call Tests.IntrinsicMethods.portableAdd, + .stobj Tests.IntrinsicMethods.vector256UInt64, .ret] } /-- The helper receives two saved vector values and one writable output reference. -/ structure Inputs where @@ -56,7 +58,8 @@ theorem helper_holds {program : CIL.Program} {callee : Nat} @[cil_code] def caller : CIL.Method := { locals := [.i64 42, .unmodeled], localKinds := [.word64, .vector256], returnsValue := true, - code := [.arg 0, .arg 1, .localAddr 1, .call 1 3, .local 1, .ret] } + code := [.ldarg 0, .ldarg 1, .ldloca 1, .call (Tests.callee 3) {}, .ldloc 1, + .ret], links := [(3, .direct 1)] } bind_entry entry (memory : Memory) (left right : BitVec 256) (formed : memory.WellFormed) := caller diff --git a/Tests/VectorStorage.cs b/Tests/VectorStorage.cs index 4434c852f..b53894cf8 100644 --- a/Tests/VectorStorage.cs +++ b/Tests/VectorStorage.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs @@ -9,7 +14,6 @@ using System.Numerics; using System.Reflection; using System.Runtime.Intrinsics; -using System.Text.RegularExpressions; using Mono.Cecil; using Mono.Cecil.Cil; @@ -18,7 +22,8 @@ var source = Path.Combine(root, "Assembly"); Directory.CreateDirectory(source); File.WriteAllText(Path.Combine(source, "Storage.csproj"), """ -net10.0truetrue +net11.0 +truetrue """); var elements = new[] { ("byte", typeof(byte)), ("int", typeof(int)), ("uint", typeof(uint)), ("long", typeof(long)), ("ulong", typeof(ulong)) }; @@ -248,7 +253,7 @@ end ExampleProof // The helper's isolated declarations must match its certificate in the complete caller. var callEvidence = File.ReadAllText(Path.Combine(callOutput, "Extracted.lean")) .Replace("method0", "method1", StringComparison.Ordinal); - const string portableObligation = "method1_intrinsics_validated"; + const string portableObligation = "method1_models_validated"; File.AppendAllText(proof, CheckedEvidenceControl(evidence, NarrowEvidence(callEvidence, portableObligation), [portableObligation], 1)); } @@ -257,64 +262,110 @@ end ExampleProof pending.Flush(); var baseline = File.ReadAllText(Path.Combine(root, "Roundtrip128uint/Extracted.lean")); -const string obligation = "method0_vector_initializations_validated"; +const string shapes = "method0_object_operands_validated"; +const string scalars = "method0_scalars_validated"; var controlPath = Path.Combine(root, "Roundtrip128uint/Control.lean"); -File.WriteAllText(controlPath, baseline + - CheckedEvidenceControl(baseline, NarrowEvidence(baseline, obligation), [obligation], 0)); +File.WriteAllText(controlPath, $""" + {baseline} + {CheckedEvidenceControl(baseline, NarrowEvidence(baseline, shapes), [shapes], 0)} + """); Run(["lake", "env", "lean", controlPath], cwd: verification.CheckoutPackage()); +// At these ordinary value sites the scalar check leaves the emitted operand to the code equation. +const string codeEquation = "method0.code = Extraction.methodCode"; +var scalarControl = Path.Combine(root, "Roundtrip128uint/ScalarControl.lean"); +File.WriteAllText(scalarControl, DecodedScalars(baseline)); +Run(["lake", "env", "lean", scalarControl], cwd: verification.CheckoutPackage()); var rejected = new RejectedEvidence(root); -foreach (var (name, changed) in new[] +const string widerFamily = ".constructor Extraction.intrinsicsScope \"System.Runtime.Intrinsics.Vector256`1\""; +const string wordElement = ".atom Extraction.runtimeScope \"System.UInt32\""; +foreach (var (name, changed, obligation, marker) in new[] { - ("store-width", baseline.Replace(".memory .store128", ".memory .store256")), - ("load-width", baseline.Replace(".memory .load128", ".memory .load256")), - ("init-width", baseline.Replace(".initAggregate 16", ".initAggregate 32")), - ("init-missing", baseline.Replace(".initAggregate 16", ".nop")), - ("element-token", baseline.Replace("\"System.UInt32\"", "\"System.Int16\"")), - ("vector-token", baseline.Replace("Vector128\\u00601", "Vector256\\u00601")) + ("float-element", baseline.Replace("\"System.UInt32\"", "\"System.Double\""), shapes, "ObjectOperandsValid"), + ("element-token", baseline.Replace("\"System.UInt32\"", "\"System.Int16\""), shapes, "ObjectOperandsValid"), + ("vector-token", ForgeTransfer("ldobj", $""".ldobj (.apply ({widerFamily}) ({wordElement}))"""), + scalars, codeEquation), + ("store-width", ForgeTransfer("stobj", ".stobj (.atom Extraction.runtimeScope \"System.UInt64\")"), + scalars, codeEquation), + ("load-width", ForgeTransfer("ldobj", ".ldobj (.atom Extraction.runtimeScope \"System.Int32\")"), + scalars, codeEquation), + ("init-width", ForgeTransfer("initobj", ".initobj (.atom Extraction.runtimeScope \"System.Byte\")"), + scalars, codeEquation), + ("init-missing", ForgeTransfer("initobj", ".nop"), scalars, codeEquation) }) { - if (changed == baseline) throw new InvalidOperationException("Ineffective vector mutation: " + name); - var path = Path.Combine(root, name + ".lean"); - File.WriteAllText(path, NarrowEvidence(changed, obligation)); - rejected.Add(path, "PASS independently rejected vector evidence: " + name, - "Extraction.VectorInitializationsValid method0Closed", "is false"); + if (changed == baseline) throw new InvalidOperationException($"Ineffective vector mutation: {name}"); + if (obligation == shapes) + { + var live = Path.Combine(root, $"{name}-decoder.lean"); + File.WriteAllText(live, NarrowEvidence(changed, shapes)); + rejected.Add(live, $"PASS live vector element refused: {name}", "MethodDecodeError.liveHole 1 65045"); + var element = name == "float-element" ? "System.Double" : "System.Int16"; + var token = $$""" + map (fun raw => match raw.operand with + | .type (.apply head _) => + { raw with operand := .type (.apply head (.atom Extraction.runtimeScope "{{element}}")) } + | _ => raw) + """; + var literal = Path.Combine(root, $"{name}-operand.lean"); + File.WriteAllText(literal, NarrowEvidence(ForgeClosedCode(baseline, 0, token), shapes)); + rejected.Add(literal, $"PASS independently rejected vector evidence: {name}", marker, "is false"); + continue; + } + var path = Path.Combine(root, $"{name}.lean"); + File.WriteAllText(path, DecodedScalars(changed)); + rejected.Add(path, $"PASS independently rejected vector evidence: {name}", marker, "is false"); } -var portableBaseline = File.ReadAllText(Path.Combine(root, "PortableCall128/Extracted.lean")) - .Replace("method0", "method1", StringComparison.Ordinal); -string RawOnly(string text, Func change) => Regex.Replace(text, - @"(?m)^def method1(?:Original|Closed) :[^\r\n]*", match => change(match.Value)); -string UnrelatedPortable(string source, int bits) + +// The scalar obligation with the scalar check and the code equation decided apart, so a rejection names the +// conjunct that refuses it. +string DecodedScalars(string source) { - var unrelated = RawOnly(source, text => Regex.Replace( - text.Replace($"System.Runtime.Intrinsics.Vector{bits}", $"Unrelated.Vector{bits}"), - @"name := ""[^""]*""", "name := \"Unrelated\"")); - return Regex.Replace(unrelated, - $@"\.intrinsic \(\.vector \(\.(?:band|bor|bxor|bnot|equalsAll|zero|ones) {bits}\)\) \d+", ".nop"); + var narrowed = NarrowEvidence(source, scalars); + var statement = $"{scalars} : And (Extraction.ScalarFlowValid method0Closed method0FeatureStates " + + $"method0ScalarStates method0.code) ({codeEquation} "; + const string decided = ") := by decide +kernel"; + var start = narrowed.IndexOf(statement, StringComparison.Ordinal); + var end = start < 0 ? -1 : narrowed.IndexOf(decided, start, StringComparison.Ordinal); + if (end < 0 || narrowed.IndexOf('\n', start, end - start) >= 0) + throw new InvalidOperationException("Missing decoded scalar obligation"); + return narrowed[..end] + ") := ⟨by decide +kernel, by decide +kernel⟩" + narrowed[(end + decided.Length)..]; } -void RejectPortable(string original, string name, string changed) + +string ForgeTransfer(string opcode, string replacement) => ForgeDecodedMethod(baseline, 0, + $"""code := method.code.map fun op => match op with | .{opcode} _ => {replacement} | _ => op"""); +var portableBaseline = File.ReadAllText(Path.Combine(root, "PortableCall128/Extracted.lean")) + .Replace("method0", "method1", StringComparison.Ordinal); +// The byte helper consists of two argument loads followed by the bitwise call. +void RejectPortable(string original, string name, string change) { - if (changed == original) throw new InvalidOperationException("Ineffective portable mutation: " + name); + var changed = ForgeClosedCode(original, 1, $$""" + modify 2 (fun raw => match raw.operand with + | .call site => { raw with operand := .call { site with {{change}} } } + | _ => raw) + """); var path = Path.Combine(root, name + ".lean"); - File.WriteAllText(path, NarrowEvidence(changed, "method1_intrinsics_validated")); - rejected.Add(path, "PASS independently rejected portable vector evidence: " + name, - "Extraction.ExactIntrinsicsValid method1Closed", "is false"); + File.WriteAllText(path, NarrowEvidence(changed, "method1_models_validated")); + rejected.Add(path, $"PASS portable model rejects {name}", "Extraction.RuntimeModelsAgree", "is false"); } -foreach (var (name, changed) in new[] +foreach (var (name, field) in new[] { - ("portable-operation", portableBaseline.Replace(".vector (.band 128)", ".vector (.bor 128)")), - ("portable-element", RawOnly(portableBaseline, text => text.Replace("System.Byte", "System.Int64"))), - ("portable-scope", RawOnly(portableBaseline, text => text.Replace("Version=10.0.0.0", "Version=9.0.0.0"))), - ("portable-width", portableBaseline.Replace(".vector (.band 128)", ".vector (.band 256)")), - ("portable-arity", portableBaseline.Replace(".intrinsic (.vector (.band 128)) 2", - ".intrinsic (.vector (.band 128)) 1")), - ("portable-target-family", UnrelatedPortable(portableBaseline, 128)) + ("portable-operation", """method.name := name!"op_BitwiseOr" """), + ("portable-element", """method.parameters := [.atom CIL.Extraction.runtimeScope "System.Int64"]"""), + ("portable-scope", """method.owner := .atom "Foreign" "System.Runtime.Intrinsics.Vector128`1" """), + ("portable-arity", "parameterCount := 1"), + ("portable-target", """target := .external "Other member" """) }) - RejectPortable(portableBaseline, name, changed); + RejectPortable(portableBaseline, name, field); var wideBaseline = File.ReadAllText(Path.Combine(root, "PortableCall512/Extracted.lean")) .Replace("method0", "method1", StringComparison.Ordinal); -RejectPortable(wideBaseline, "portable512-width", - wideBaseline.Replace(".vector (.band 512)", ".vector (.band 256)")); -RejectPortable(wideBaseline, "portable512-target-family", UnrelatedPortable(wideBaseline, 512)); +const string narrowHead = """ + (.constructor CIL.Extraction.intrinsicsScope "System.Runtime.Intrinsics.Vector256`1") + """; +const string byteElement = """(.atom CIL.Extraction.runtimeScope "System.Byte")"""; +const string narrowOwner = $"""method.owner := .apply {narrowHead} {byteElement}"""; +RejectPortable(portableBaseline, "portable-width", narrowOwner); +RejectPortable(wideBaseline, "portable512-width", narrowOwner); +RejectPortable(wideBaseline, "portable512-target", """target := .external "Other member" """); rejected.Check(); foreach (var mutation in new[] { "element", "scope", "encoding", "field512" }) diff --git a/Tests/VectorStorage.lean b/Tests/VectorStorage.lean index af7e45b27..ad6c6df2a 100644 --- a/Tests/VectorStorage.lean +++ b/Tests/VectorStorage.lean @@ -1,4 +1,4 @@ -import CIL.Extraction.VectorInitialization +import CIL.Extraction.ObjectOperands import CIL.Safety.Execution /-! Exact integer-vector object encodings, independently of generated source or DLL hashes. -/ @@ -15,50 +15,44 @@ private def vectorType (bits : Nat) (element : String) : TypeExpr := .apply (.constructor intrinsicsScope s!"System.Runtime.Intrinsics.Vector{bits}`1") (.atom runtimeScope element) -/-- Each object opcode paired with the operation for its validated vector width. -/ -private def operations (bytes : Nat) : List (Nat × Op) := - [(0x70, .copyObject bytes), (0xfe15, .initAggregate bytes), - (0xfe1c, .const32 (BitVec.ofNat 32 bytes)), - (0x71, .memory (if bytes == 16 then .load128 else if bytes == 32 then .load256 else .load512)), - (0x81, .memory (if bytes == 16 then .store128 else if bytes == 32 then .store256 else .store512))] +/-- Each object instruction retains its exact type operand. -/ +private def operations (type : TypeExpr) : List (Nat × Instruction) := + [(0x70, .cpobj type), (0xfe15, .initobj type), (0xfe1c, .sizeof type), + (0x71, .ldobj type), (0x81, .stobj type)] -/-- All admitted integer types use the same checked bit storage at each vector width. -/ +/-- Every supported integer element decodes to an operation on the vector of that width. -/ theorem integer_storage_checked : - [16, 32, 64].all (fun bytes => elements.all fun element => - (operations bytes).all fun (opcode, operation) => - vectorInitializationMatches ⟨0, opcode, .type (vectorType (bytes * 8) element)⟩ - operation) = true := by decide +kernel - + [128, 256, 512].all (fun bits => elements.all fun element => + let type := vectorType bits element + let shape := ({} : LayoutEnvironment).valueShape type + shape == .vector (bits / 8) && (operations type).all fun (opcode, operation) => + valueEncodingMatches none ⟨0, opcode, .type type⟩ operation && + objectShapeAdmitted opcode type shape) = true := by decide +kernel #print axioms integer_storage_checked -/-- Wrong widths, primitive substitution, unsupported elements and scopes cannot certify. -/ +/-- Site binding rejects altered widths. Admission checks element types and scopes. -/ theorem altered_storage_rejected : - [16, 32, 64].all (fun bytes => - (operations bytes).all fun (opcode, operation) => - !(vectorInitializationMatches - ⟨0, opcode, .type (vectorType (if bytes == 16 then 256 else 128) "System.UInt32")⟩ - operation) && - !(vectorInitializationMatches - ⟨0, opcode, .type (vectorType (bytes * 8) "System.Int16")⟩ operation) && - !(vectorInitializationMatches ⟨0, opcode, .type - (.apply (.constructor "forged" s!"System.Runtime.Intrinsics.Vector{bytes * 8}`1") - (.atom runtimeScope "System.UInt32"))⟩ operation) && - !(vectorInitializationMatches ⟨0, opcode, .none⟩ operation)) = true := by + [128, 256, 512].all (fun bits => + let type := vectorType bits "System.UInt32" + let other := vectorType (if bits == 128 then 256 else 128) "System.UInt32" + let wrong := TypeExpr.apply + (.constructor "forged" s!"System.Runtime.Intrinsics.Vector{bits}`1") + (.atom runtimeScope "System.UInt32") + (operations type).all fun (opcode, operation) => + !valueEncodingMatches none ⟨0, opcode, .type other⟩ operation && + !valueEncodingMatches none ⟨0, opcode, .none⟩ operation && + [vectorType bits "System.Int16", vectorType bits "System.Double", wrong].all fun operand => + !objectShapeAdmitted opcode operand (({} : LayoutEnvironment).valueShape operand)) = + true := by decide +kernel - #print axioms altered_storage_rejected -/-- Stack-effect-equivalent primitive and incorrect object operations are rejected directly. -/ +/-- A different transfer with the same stack depth fails the literal site's encoding check. -/ theorem changed_object_meaning_rejected : - vectorInitializationMatches ⟨0, 0x71, .type (.atom runtimeScope "System.UInt64")⟩ - (.memory .load128) = false ∧ - vectorInitializationMatches ⟨0, 0x70, .type (.atom runtimeScope "System.UInt64")⟩ - (.copyObject 16) = false ∧ - vectorInitializationMatches ⟨0, 0xfe15, .type (vectorType 128 "System.UInt32")⟩ - .nop = false ∧ - vectorInitializationMatches ⟨0, 0x71, .type (vectorType 128 "System.UInt32")⟩ - (.initAggregate 16) = false := by decide +kernel - + let type := vectorType 128 "System.UInt32" + !valueEncodingMatches none ⟨0, 0x71, .type type⟩ (.ldobj (vectorType 256 "System.UInt32")) && + !valueEncodingMatches none ⟨0, 0xfe15, .type type⟩ .nop && + !valueEncodingMatches none ⟨0, 0x71, .type type⟩ (.initobj type) := by decide +kernel #print axioms changed_object_meaning_rejected /-- Whole-vector operations reject incomplete 512-bit snapshots and wrong-width opaque values. -/ diff --git a/Tests/VerificationCache.cs b/Tests/VerificationCache.cs index 113ec0d1c..ee15b9651 100644 --- a/Tests/VerificationCache.cs +++ b/Tests/VerificationCache.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Text.Json; @@ -47,6 +52,117 @@ verifier.PublishSuccess("missing", source); File.Delete(Path.Combine(SuccessCacheRoot, "missing", "files", "Proof.olean")); if (verifier.RestoreSuccess("missing", destination)) throw new Exception("Missing object accepted"); + var originalModules = new SortedDictionary(StringComparer.Ordinal) + { + ["CIL/Base.olean"] = "base", ["CIL/Base.olean.private"] = "private" + }; + var completeModules = new SortedDictionary(originalModules, StringComparer.Ordinal) + { + ["CIL/Extra.olean"] = "extra" + }; + void CheckPromotion(string label, bool expected, string completeIdentity = "full", + Action>? mutateModules = null, Action? mutateEntry = null, + Action? requireStable = null) + { + var extractionKey = "extraction-" + label; + var proofKey = "proof-" + label; + verifier.PublishSuccess(extractionKey, source); + verifier.PublishSuccess(proofKey, source); + mutateEntry?.Invoke(Path.Combine(SuccessCacheRoot, extractionKey)); + mutateEntry?.Invoke(Path.Combine(SuccessCacheRoot, proofKey)); + var modules = new SortedDictionary(completeModules, StringComparer.Ordinal); + mutateModules?.Invoke(modules); + var promotion = new StandaloneCachePromotion("full", originalModules, + extractionKey, label, proofKey, label); + var guards = 0; + verifier.PromoteStandaloneCache(promotion, completeIdentity, modules, () => + { + guards++; + requireStable?.Invoke(); + }); + var environment = verifier.StandaloneEnvironmentKey(completeIdentity, modules); + var promotedExtraction = StandaloneExtractionKey(environment, label); + var promotedProof = StandaloneProofKey(promotedExtraction, label); + if (verifier.RestoreSuccess(promotedExtraction, destination) != expected || + verifier.RestoreSuccess(promotedProof, destination) != expected || expected && guards != 2) + throw new Exception("Incorrect complete-library promotion: " + label); + } + CheckPromotion("identical-extension", true); + CheckPromotion("source-or-toolchain", false, completeIdentity: "changed-full"); + CheckPromotion("changed-dependency", false, mutateModules: modules => modules["CIL/Base.olean"] = "changed"); + CheckPromotion("missing-dependency", false, mutateModules: modules => modules.Remove("CIL/Base.olean")); + CheckPromotion("changed-companion", false, + mutateModules: modules => modules["CIL/Base.olean.private"] = "changed"); + CheckPromotion("added-companion", false, + mutateModules: modules => modules["CIL/Base.olean.server"] = "unchecked"); + CheckPromotion("changed-receipt", false, + mutateEntry: entry => File.AppendAllText(Path.Combine(entry, "receipt.json"), " ")); + CheckPromotion("changed-artifact", false, + mutateEntry: entry => File.AppendAllText(Path.Combine(entry, "files", "Proof.olean"), "corruption")); + var privateLibraryDirectory = Path.Combine(directory, "private-library"); + Directory.CreateDirectory(privateLibraryDirectory); + var privateArtifact = Path.Combine(privateLibraryDirectory, "CIL.olean"); + File.WriteAllText(privateArtifact, "certified"); + using (var privateLibrary = new Program + { + library = privateLibraryDirectory, ownsLibrary = true, + libraryManifest = CacheFiles(privateLibraryDirectory) + }) + { + if (privateLibrary.libraryStamps is not null) + throw new Exception("Private library fixture unexpectedly has a read-in-place guard"); + CheckPromotion("private-library", true, requireStable: privateLibrary.RequireCertifiedLibraryContents); + var timestamp = File.GetLastWriteTimeUtc(privateArtifact); + File.WriteAllText(privateArtifact, "corrupted"); + File.SetLastWriteTimeUtc(privateArtifact, timestamp); + CheckPromotion("changed-private-library", false, + requireStable: privateLibrary.RequireCertifiedLibraryContents); + File.Delete(privateArtifact); + CheckPromotion("missing-private-artifact", false, + requireStable: privateLibrary.RequireCertifiedLibraryContents); + File.WriteAllText(privateArtifact, "certified"); + File.WriteAllText(privateArtifact + ".private", "unchecked"); + CheckPromotion("added-private-artifact", false, + requireStable: privateLibrary.RequireCertifiedLibraryContents); + } + Console.WriteLine("PASS complete-library promotion preserves exact families, identity and " + + "authenticated artifacts"); + long previousAllocation = 0; + long previousComparisons = 0; + foreach (var count in new[] { 256, 1024, 4096 }) + { + long comparisons = 0; + var comparer = Comparer.Create((left, right) => + { + comparisons++; + return string.CompareOrdinal(left, right); + }); + var beforeModules = new SortedDictionary(comparer); + for (var index = 0; index < count; index++) beforeModules[$"CIL/M{index:D5}.olean"] = "hash"; + var afterModules = new SortedDictionary(beforeModules, comparer) + { + ["CIL/Outside.olean"] = "outside" + }; + ModuleArtifactsPreserved(beforeModules, afterModules); + comparisons = 0; + var allocationStart = GC.GetAllocatedBytesForCurrentThread(); + var timer = System.Diagnostics.Stopwatch.StartNew(); + for (var repeat = 0; repeat < 32; repeat++) + { + if (!ModuleArtifactsPreserved(beforeModules, afterModules)) + throw new Exception("Identical module families were rejected"); + verifier.StandaloneEnvironmentKey("full", afterModules); + } + var allocated = GC.GetAllocatedBytesForCurrentThread() - allocationStart; + if (previousAllocation > 0 && allocated > previousAllocation * 6) + throw new Exception("Complete-library environment allocation grew superlinearly"); + if (previousComparisons > 0 && comparisons > previousComparisons * 6) + throw new Exception("Complete-library dependency comparisons grew superlinearly"); + previousAllocation = allocated; + previousComparisons = comparisons; + Console.WriteLine($"PASS complete-library environment {count} modules: {allocated} bytes, " + + $"{comparisons} comparisons, {timer.Elapsed.TotalMilliseconds:F2} ms for 32 checks"); + } // Publishing closure A must not carry unrelated B from the same session. var session = Path.Combine(directory, "session"); var artifacts = Path.Combine(session, ".lake/build/lib/lean/Support"); diff --git a/Tests/VerificationReuse.cs b/Tests/VerificationReuse.cs index 2e7fa7915..d624db1bc 100644 --- a/Tests/VerificationReuse.cs +++ b/Tests/VerificationReuse.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Diagnostics; @@ -132,4 +137,39 @@ set_option debug.skipKernelTC true freshProof.libraryKey != second.libraryKey || freshProof.libraryBuild != second.libraryBuild) throw new Exception("Fresh proof reused proof/support artifacts or bypassed the existing certified library"); Console.WriteLine("PASS fresh proof bypasses proof/support reuse while retaining the certified library"); + +// The session holds no compiled Extracted, so a support module importing it cannot be built, certified or +// cached; a support entry therefore never holds a module bound to one extraction. +{ + var supportOutput = At("artifacts/ExtractedSupport"); + Directory.CreateDirectory(supportOutput); + var supportProof = Path.Combine(supportOutput, "Proof.lean"); + var addProof = File.ReadAllText(At("Examples/Proofs/Add.lean")); + File.WriteAllText(supportProof, "import Tests.ExtractedSupport\n" + addProof); + var lakefilePath = At("lakefile.toml"); + var lakefileBytes = File.ReadAllBytes(lakefilePath); + var supportModule = At("Tests/ExtractedSupport.lean"); + try + { + // Registered only while this check runs: a library that imports Extracted fails every full library build. + File.AppendAllText(lakefilePath, "\n[[lean_lib]]\nname = \"Tests.ExtractedSupport\"\n"); + File.WriteAllText(supportModule, + "import Extracted\n\ntheorem ExtractedSupport.program : True := True.intro\n"); + using var importing = new Program(); + try + { + importing.Verify(assembly, "Examples", entry, supportProof, supportOutput); + throw new Exception("A support module importing Extracted was built and its proof reported verified"); + } + catch (InvalidOperationException error) when (error.Message.Contains("unknown module prefix 'Extracted'")) { } + if (importing.supportCacheHits != 0 || File.Exists(Path.Combine(supportOutput, "verified.json"))) + throw new Exception("A support module importing Extracted was restored or left a successful report"); + } + finally + { + File.WriteAllBytes(lakefilePath, lakefileBytes); + if (File.Exists(supportModule)) File.Delete(supportModule); + } + Console.WriteLine("PASS a support module importing Extracted cannot be built or cached"); +} Console.WriteLine($"PASS certified reuse, proof-edit invalidation, repeated unchecked-theorem rejection and new-session reuse; first={first}ms repeat={repeated}ms new-session={newSession}ms"); diff --git a/Tests/ViewAccessors.cs b/Tests/ViewAccessors.cs index 851d06a4c..57749db49 100644 --- a/Tests/ViewAccessors.cs +++ b/Tests/ViewAccessors.cs @@ -2,9 +2,15 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs #:include ../scripts/BatchVerification.cs #:include GeneratedEvidence.cs #:include RejectedEvidence.cs +#:include MarshalModelEvidence.cs using Mono.Cecil; @@ -27,7 +33,7 @@ $"[.array ⟨.address ⟨0, 0⟩, {length}, .signed32⟩] memory).map Prod.snd = " + ".ok [.reference (.address ⟨0, 0⟩)]"); claims.Add("(match CIL.Safety.invoke Extracted.program 32 0 [.object .null] memory with " + - "| .error ⟨_, _, .thrown \"System.NullReferenceException\"⟩ => true | _ => false) = true"); + "| .error ⟨_, _, .thrown .nullReference⟩ => true | _ => false) = true"); } else { @@ -40,37 +46,30 @@ $"[.bufferSpan ⟨{address}, {length}, 4, {writable}⟩] memory).map Prod.snd = " + $".ok [.scalar (.i32 {(length == 0 && (!nullOnlyWhenNull || address == ".null") ? 1 : 0)})]"); } - File.WriteAllText(proof, "import Extracted\nimport CIL.Safety.Execution\nnamespace ExampleProof\n" + """ - /-- Successful observations identify the checked execution result. -/ - private theorem result_of_toOption {error value : Type} {result : Except error value} - {expected : value} (observed : result.toOption = some expected) : result = .ok expected := by - cases result <;> simp_all [Except.toOption] - #print axioms result_of_toOption - /-- Existing storage has no readable bytes or access authority; observations need neither. -/ + File.WriteAllText(proof, $$""" + import Extracted + import CIL.Safety.Execution + namespace ExampleProof + /-- The caller owns an allocation with no readable bytes or access authority. -/ def memory : CIL.Safety.Memory := { CIL.Safety.Memory.empty with allocations := fun identity => if identity == 0 then some ⟨.callerStack, ⟨16, 1, []⟩, true, [16]⟩ else none nextIdentity := 1 } /-- Empty views, readonly receivers and zero-length arrays retain their distinct results. -/ - """ + "\ntheorem correctness :\n" + string.Join(" ∧\n", claims) + " := by\n repeat constructor\n all_goals first | (apply result_of_toOption; decide +kernel) | decide +kernel\n" + - "#print axioms correctness\nend ExampleProof\n"); + theorem correctness : + {{string.Join(" ∧\n", claims)}} := by + exact ⟨{{string.Join(", ", claims.Select(_ => "by rfl"))}}⟩ + #print axioms correctness + end ExampleProof + + """); queue.Add(new(method.FullName, proof, output), () => Console.WriteLine("PASS view accessor: " + method.Name)); } queue.Flush(); -var rejected = new RejectedEvidence(Path.Combine(root, "RejectedEvidence")); -foreach (var (name, before, after) in new[] -{ - ("Empty", ".observeView .spanEmpty", ".observeView .spanPinnable"), - ("PinnableNull", ".observeView .spanPinnable", ".spanReference"), - ("ReferenceNull", ".memory .spanReference", ".memory (.observeView .spanPinnable)"), - ("ArrayData", ".observeView .arrayData", ".observeView .spanPinnable") -}) -{ - var source = NarrowEvidence(File.ReadAllText(Path.Combine(root, name, "Extracted.lean")), - "method0_unsafe_validated"); - if (!source.Contains(before, StringComparison.Ordinal)) throw new Exception("Missing accessor mutation"); - var path = Path.Combine(root, name + "Rejected.lean"); - File.WriteAllText(path, source.Replace(before, after, StringComparison.Ordinal)); - rejected.Add(path, "PASS exact accessor evidence rejection: " + name, "UnsafeCallsValid"); -} -rejected.Check(); +CheckMarshalModels(root, +[ + ("Empty", 1, ".observeView .spanEmpty", null), + ("PinnableNull", 1, ".observeView .spanPinnable", null), + ("ReferenceNull", 1, ".spanReference", null), + ("ArrayData", 1, ".observeView .arrayData", null) +]); diff --git a/Tests/WeakestPrecondition.lean b/Tests/WeakestPrecondition.lean index 68febdffc..c8f9bc842 100644 --- a/Tests/WeakestPrecondition.lean +++ b/Tests/WeakestPrecondition.lean @@ -13,11 +13,11 @@ open CIL.Safety /-- A scalar addition body used to check postconditions against the actual instructions. -/ @[cil_code] def addBody : CIL.Method := - { locals := [], code := [.arg 0, .arg 1, .add, .ret], returnsValue := true } + { locals := [], code := [.ldarg 0, .ldarg 1, .add, .ret], returnsValue := true } /-- A scalar subtraction body with the same argument and return convention. -/ @[cil_code] def subtractBody : CIL.Method := - { locals := [], code := [.arg 0, .arg 1, .sub, .ret], returnsValue := true } + { locals := [], code := [.ldarg 0, .ldarg 1, .sub, .ret], returnsValue := true } /-- Addition terminates with wrapping arithmetic and exactly preserves caller memory. -/ theorem add (memory : Memory) (left right : CIL.W64) : @@ -117,13 +117,81 @@ def returnContract : MethodContract Unit where footprint _ := [] post _ _ _ _ := True -/-- A located missing-argument fault refutes the contract for all fuel budgets. -/ +/-- A located missing-argument fault refutes the contract. -/ theorem missing_argument_refutes_contract : - ¬ returnContract.Holds [{ locals := [], code := [.arg 0, .ret], returnsValue := false }] 0 := by - apply returnContract.not_holds_of_fault (input := ()) (initial := Memory.empty) - (observed := 1) (fault := ⟨0, 0, .invalidState⟩) trivial (by decide) - rfl + returnContract.Refuted [{ locals := [], code := [.ldarg 0, .ret], returnsValue := false }] + 0 := + .of_fault (input := ()) (initial := Memory.empty) (observed := 1) + (fault := ⟨0, 0, .invalidState⟩) trivial (by decide) rfl #print axioms missing_argument_refutes_contract +/-- `ldnull; unbox.any object; pop; ret` as the decoder gives it: `unbox.any` is a hole. On the + CLR the method returns normally, so it meets `returnContract`. -/ +@[cil_code] def unboxHole : CIL.Method := + { locals := [], code := [.ldnull, .unadmitted 0xA5, .pop, .ret], returnsValue := false } + +/-- The run faults at the hole, and that fault is not definite, so `Refuted.of_fault` cannot + refute `returnContract` from it. -/ +theorem hole_not_definite : + invoke [unboxHole] 4 0 [] Memory.empty = .error ⟨0, 1, .unsupported "Opcode not admitted"⟩ ∧ + (ExecutionFault.unsupported "Opcode not admitted").definite = false := + ⟨rfl, rfl⟩ + +#print axioms hole_not_definite + +-- So the refutation that works for the missing argument fails at its `definite` hypothesis here. +/-- error: Tactic `decide` proved that the proposition -/ +#guard_msgs (substring := true) in +example : returnContract.Refuted [unboxHole] 0 := + .of_fault (input := ()) (initial := Memory.empty) (observed := 4) + (fault := ⟨0, 1, .unsupported "Opcode not admitted"⟩) trivial (by decide) rfl + +/-- `returnContract` from the empty memory only. -/ +def emptyReturnContract : MethodContract Unit := + { returnContract with pre := fun _ initial => initial = Memory.empty } + +/-- The hole's run stops at the same unsupported fault from every larger budget. -/ +theorem hole_stuck (fuel : Nat) : + invoke [unboxHole] (4 + fuel) 0 [] Memory.empty = + .error ⟨0, 1, .unsupported "Opcode not admitted"⟩ := + invoke_mono _ 4 fuel 0 [] _ _ (by simp [Stable]) rfl + +#print axioms hole_stuck + +/-- The negation of the contract follows from the hole's run, although the CLR returns. -/ +theorem hole_negates : ¬ emptyReturnContract.Holds [unboxHole] 0 := by + intro holds + obtain ⟨fuel, result, ran, -⟩ := holds () Memory.empty rfl + have returned := + invoke_mono [unboxHole] fuel 4 0 [] Memory.empty (.ok result) trivial ran + rw [Nat.add_comm, hole_stuck] at returned + cases returned + +#print axioms hole_negates + +/-- The hole's run has no settled outcome and does not diverge, so it is no counterexample. -/ +theorem hole_not_refuted : ¬ emptyReturnContract.Refuted [unboxHole] 0 := by + rintro ⟨_, initial, rfl, counter⟩ + rcases counter with ⟨fuel, outcome, ran, settled, -⟩ | diverges + · change invoke [unboxHole] fuel 0 [] Memory.empty = outcome at ran + have same := invoke_mono _ fuel 4 0 [] _ _ settled.stable ran + rw [Nat.add_comm, hole_stuck] at same + subst same + simp [Settled] at settled + · obtain ⟨fault, ran, exhausted⟩ := diverges 4 + change invoke [unboxHole] (4 + 0) 0 [] Memory.empty = _ at ran + rw [hole_stuck] at ran + cases ran + simp at exhausted + +#print axioms hole_not_refuted + +-- Stepping stops at pc 1, the hole, so no proof that the method returns goes through either. +/-- run [unboxHole] fuel 0 1 [] -/ +#guard_msgs (substring := true) in +example : returnContract.Holds [unboxHole] 0 := by + intro _ initial _ + cil_contract_steps [] + end Tests.WeakestPrecondition diff --git a/Tests/WideProduct.cs b/Tests/WideProduct.cs index f72b4e3dd..b129da89c 100644 --- a/Tests/WideProduct.cs +++ b/Tests/WideProduct.cs @@ -2,6 +2,11 @@ #:property PublishAot=false #:package Mono.Cecil #:include ../scripts/Verification.cs +#:include ../scripts/DeepChecks.cs +#:include ../scripts/FailureReports.cs +#:include ../scripts/LeanDiagnostics.cs +#:include ../scripts/BindingImage.cs +#:include ../scripts/DeepCheckInputs.cs using System.Numerics; using Mono.Cecil; diff --git a/Tests/WordArrayOutlines.cs b/Tests/WordArrayOutlines.cs index 3c73adcd4..0a87eaf1b 100644 --- a/Tests/WordArrayOutlines.cs +++ b/Tests/WordArrayOutlines.cs @@ -10,17 +10,29 @@ void WordArrayOutlines() var assembly = At("Examples/Assembly/bin/Examples.dll"); var root = At("artifacts/WordArrayOutlines"); Directory.CreateDirectory(root); - foreach (var name in new[] - { "Sum", "RefillRead", "CountAfterDeposit", "CountAfterAllocation", "ReadAfterAllocation", - "ReadAfterEcho" }) + foreach (var variant in new[] + { "Sum", "RefillRead", "RefillReadTyped", "CountAfterDeposit", "CountAfterAllocation", + "ReadAfterAllocation", "ReadAfterEcho" }) { - var extraction = Path.Combine(root, name); + var name = variant == "RefillReadTyped" ? "RefillRead" : variant; + var extraction = Path.Combine(root, variant); + var input = assembly; + if (variant == "RefillReadTyped") + { + using var image = ModuleDefinition.ReadModule(assembly); + var method = image.GetType("Examples.SampleArrays").Methods.Single(method => method.Name == name); + var read = method.Body.Instructions.Single(site => site.OpCode.Code == Code.Ldelem_I4); + read.OpCode = OpCodes.Ldelem_Any; + read.Operand = image.TypeSystem.Int32; + input = Path.Combine(root, variant + ".dll"); + image.Write(input); + } var owner = name is "CountAfterDeposit" or "ReadAfterEcho" ? "Account" : name.EndsWith("AfterAllocation", StringComparison.Ordinal) ? "Node" : "Samples"; var indexed = name is "RefillRead" or "ReadAfterEcho"; var selector = $"SampleArrays::{name}(Examples.{owner},int[]{(indexed ? ",int" : "")})"; - Run(Extract("extract", assembly, "Examples", selector, extraction)); - var outline = Path.Combine(root, name + ".lean"); + Run(Extract("extract", input, "Examples", selector, extraction)); + var outline = Path.Combine(root, variant + ".lean"); File.Delete(outline); Run(["dotnet", At("Extractor/bin/Nethermind.LeanCIL.dll"), "new-proof", extraction, outline]); var source = File.ReadAllText(outline); @@ -53,9 +65,9 @@ void WordArrayOutlines() "case invariant => simp [invariant]", StringComparison.Ordinal); source = source.Replace("case invariant => sorry -- invariant again at the loop head", """ case invariant => - have next : (index + 1).toNat = index.toNat + 1 := by - simp [BitVec.toNat_add] - omega + have small : values1Elements.length < 2 ^ 32 := by omega + have next := toNat_succ (count := BitVec.ofNat 32 values1Elements.length) + (by simpa only [BitVec.toNat_ofNat, Nat.mod_eq_of_lt small] using values1Bound) dsimp only [invariant] at holds ⊢ refine ⟨by omega, ?_⟩ rw [next, List.take_add_one, List.getElem?_eq_getElem values1Bound, List.foldl_append, @@ -64,9 +76,9 @@ dsimp only [invariant] at holds ⊢ """.Replace("\n", "\n ", StringComparison.Ordinal), StringComparison.Ordinal); source = source.Replace("case measure => sorry -- measure decreases over this iteration", """ case measure => - have next : (index + 1).toNat = index.toNat + 1 := by - simp [BitVec.toNat_add] - omega + have small : values1Elements.length < 2 ^ 32 := by omega + have next := toNat_succ (count := BitVec.ofNat 32 values1Elements.length) + (by simpa only [BitVec.toNat_ofNat, Nat.mod_eq_of_lt small] using values1Bound) simp only [measure] omega """.Replace("\n", "\n ", StringComparison.Ordinal), StringComparison.Ordinal); @@ -91,16 +103,17 @@ rw [List.take_of_length_le (by omega)] ? "simp [Postcondition, numericOfWidth, BitVec.ofNat_toNat]" : "simp [Postcondition]"); if (Regex.IsMatch(source, @"(? `(tactic| - (simp only [CIL.Safety.step, CIL.Intrinsic.available, Bool.not_true, Bool.false_eq_true, - List.length_cons, List.length_nil, Nat.reduceAdd, ite_false, +/-- Select the checked call model and simplify its arithmetic result. -/ +macro "math_wrapper_step " model:term:max evaluated:term : tactic => `(tactic| + (rw [CIL.Safety.step_call_intrinsic _ _ _ _ _ rfl $model rfl] + simp only [CIL.Safety.intrinsicCall, CIL.Intrinsic.available, Bool.not_true, + Bool.false_eq_true, List.length_cons, List.length_nil, Nat.reduceAdd, ite_false, List.take, List.reverse, List.reverseAux, CIL.Safety.scalars, CIL.Safety.numericValue, ite_true, Bind.bind, Except.bind, Pure.pure, Except.pure, CIL.Safety.checkedIntrinsic, $evaluated:term, Except.mapError, CIL.Safety.checkedValue, List.drop] <;> rfl)) +private def minimumRef : CIL.Extraction.MethodRef := + let int := CIL.Extraction.TypeExpr.atom CIL.Extraction.runtimeScope "System.Int32" + { owner := .atom CIL.Extraction.runtimeScope "System.Math", name := name!"Min", + hasThis := false, genericArity := 0, returnType := int, parameters := [int, int] } + +/-- Returns the signed minimum of its two int arguments through the scalar math intrinsic. -/ +def minimumMethod : CIL.Method := { + code := [.ldarg 0, .ldarg 1, .call minimumRef, .ret] + locals := [], returnsValue := true } + +private theorem minimumModel : + CIL.Extraction.runtimeModel minimumMethod.environment .call minimumRef = + some (.intrinsic (.scalar (.math .min 32 true)) 2) := rfl + +/-- The minimum intrinsic pushes the evaluator's result, stated as a generated wrapper does. -/ +theorem minimum_step (left right result : CIL.W32) + (evaluated : CIL.evalIntegerMath .min 32 true [.i32 left, .i32 right] = .ok (.i32 result)) : + CIL.Safety.step minimumMethod (.call minimumRef) 2 [] + ⟨0, [], [], 0⟩ [.scalar (.i32 right), .scalar (.i32 left)] CIL.Safety.Memory.empty = + .ok (.next 3 [.scalar (.i32 result)] ⟨0, [], [], 0⟩ CIL.Safety.Memory.empty) := by + math_wrapper_step minimumModel evaluated +#print axioms minimum_step + +/-- A faulting evaluation faults the minimum intrinsic with the same arithmetic fault. -/ +theorem minimum_fault (left right : CIL.W32) (fault : CIL.ArithmeticFault) + (evaluated : CIL.evalIntegerMath .min 32 true [.i32 left, .i32 right] = .error fault) : + CIL.Safety.step minimumMethod (.call minimumRef) 2 [] + ⟨0, [], [], 0⟩ [.scalar (.i32 right), .scalar (.i32 left)] CIL.Safety.Memory.empty = + .error (.arithmetic fault) := by + math_wrapper_step minimumModel evaluated +#print axioms minimum_fault + end Tests.WrapperProofs diff --git a/Tests/canonical-examples.txt b/Tests/canonical-examples.txt new file mode 100644 index 000000000..620779b97 --- /dev/null +++ b/Tests/canonical-examples.txt @@ -0,0 +1,6081 @@ +arm64-advsimd Examples.Account Examples.Objects::FirstPresent(Examples.Account,Examples.Account) +arm64-advsimd Examples.AggregateArrays/Pair Examples.AggregateArrays::Read(Examples.AggregateArrays/Pair[],System.Int32) +arm64-advsimd Examples.AggregateArrays/Pair& Examples.AggregateArrays::Address(Examples.AggregateArrays/Pair[],System.Int32) +arm64-advsimd Examples.AggregateArrays/Pair& Examples.AggregateArrays::Data(Examples.AggregateArrays/Pair[]) +arm64-advsimd Examples.AggregateArrays/Pair[] Examples.TileArrays::PairsOf(Examples.Tiles) +arm64-advsimd Examples.AggregateSpans/VectorRecord Examples.AggregateSpans::LayoutRead(System.ReadOnlySpan`1) +arm64-advsimd Examples.AggregateSpans/Words Examples.AggregateSpans::Read(System.ReadOnlySpan`1) +arm64-advsimd Examples.AllocationTag[] Examples.PrimitiveArrays::AllocateUnnamedZero(System.Int32) +arm64-advsimd Examples.CheckedConstructor Examples.Constructors::MakeChecked(System.UInt64) +arm64-advsimd Examples.Counter Examples.Constructors::Return(System.UInt64) +arm64-advsimd Examples.Counter Examples.Constructors::WrongReturn(System.UInt64) +arm64-advsimd Examples.DefaultConstructor Examples.Constructors::MakeDefault() +arm64-advsimd Examples.Dog Examples.ClassTests::AsDog(Examples.Animal) +arm64-advsimd Examples.EnumBuffers/Large Examples.EnumBuffers::Wide(Examples.EnumBuffers/Large[],System.Int32) +arm64-advsimd Examples.EnumBuffers/Small Examples.EnumBuffers::Address(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +arm64-advsimd Examples.EnumBuffers/Small Examples.EnumBuffers::FillClear(Examples.EnumBuffers/Small[]) +arm64-advsimd Examples.EnumBuffers/Small Examples.EnumBuffers::Loop(Examples.EnumBuffers/Small[],System.Int32) +arm64-advsimd Examples.EnumBuffers/Small Examples.EnumBuffers::Read(System.ReadOnlySpan`1) +arm64-advsimd Examples.EnumBuffers/Small Examples.EnumBuffers::Roundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +arm64-advsimd Examples.EnumBuffers/Small Examples.EnumBuffers::SpanRoundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +arm64-advsimd Examples.FourLimbs Examples.MarshaledMemory::ReadAggregate(System.ReadOnlySpan`1) +arm64-advsimd Examples.FourLimbs Examples.UnalignedMemory::ReadAggregate(System.Byte&) +arm64-advsimd Examples.FourLimbs Examples.UnalignedMemory::ReadAlignedAggregate(System.Byte*) +arm64-advsimd Examples.FourLimbs Examples.UnalignedMemory::ReadPointerAggregate(System.Byte*) +arm64-advsimd Examples.FourLimbs& Examples.UnsafeReferences::Mutable(Examples.FourLimbs&) +arm64-advsimd Examples.FourLimbs& Examples.UnsafeReferences::SubtractAggregate(Examples.FourLimbs&,System.IntPtr) +arm64-advsimd Examples.GenericPair`1> Examples.GenericLayouts::Nested(System.UInt32,System.UInt32) +arm64-advsimd Examples.GenericPair`1 Examples.AggregateArrays::ReadGeneric(Examples.GenericPair`1[],System.Int32) +arm64-advsimd Examples.GenericPair`1 Examples.GenericLayouts::Create(System.UInt64,System.UInt64) +arm64-advsimd Examples.IndirectValues/Box`1 Examples.IndirectValues::Generic(Examples.IndirectValues/Box`1) +arm64-advsimd Examples.IndirectValues/Box`1 Examples.IndirectValues::Identity>(Examples.IndirectValues/Box`1) +arm64-advsimd Examples.IndirectValues/Pair Examples.IndirectValues::Identity(Examples.IndirectValues/Pair) +arm64-advsimd Examples.IndirectValues/Pair Examples.IndirectValues::Read(Examples.IndirectValues/Pair&) +arm64-advsimd Examples.IndirectValues/Pair Examples.IndirectValues::Record(Examples.IndirectValues/Pair) +arm64-advsimd Examples.IndirectValues/Pair Examples.IndirectValues::Reference(Examples.IndirectValues/Pair&) +arm64-advsimd Examples.ItemPool Examples.PooledItem::get_Owner() +arm64-advsimd Examples.Leaf Examples.Nested::Extract(Examples.NestedRecord) +arm64-advsimd Examples.Leaf Examples.Nested::WrongExtract(Examples.NestedRecord) +arm64-advsimd Examples.NestedConstructor Examples.Constructors::MakeNested(System.UInt64) +arm64-advsimd Examples.NestedRecord& Examples.UnsafeLayouts::Add15int(Examples.NestedRecord&,System.Int32) +arm64-advsimd Examples.NestedRecord& Examples.UnsafeLayouts::Add15nint(Examples.NestedRecord&,System.IntPtr) +arm64-advsimd Examples.NestedRecord& Examples.UnsafeLayouts::Add15nuint(Examples.NestedRecord&,System.UIntPtr) +arm64-advsimd Examples.NestedRecord[] Examples.PrimitiveArrays::AllocateNested(System.Int32) +arm64-advsimd Examples.Node Examples.NodeArrays::At(Examples.Node[],System.Int32) +arm64-advsimd Examples.Node Examples.NodeArrays::First(Examples.Bucket) +arm64-advsimd Examples.Node Examples.NodeArrays::Install(Examples.Node[],System.Int32,Examples.Node,System.UInt64) +arm64-advsimd Examples.Node Examples.NodeArrays::Pair(Examples.Node,Examples.Node) +arm64-advsimd Examples.Node Examples.NodeArrays::Shift(Examples.Node[],Examples.Node) +arm64-advsimd Examples.Node Examples.Objects::Detach(Examples.Node) +arm64-advsimd Examples.Node[] Examples.NodeArrays::Create(System.Int32) +arm64-advsimd Examples.PackedConstructor Examples.Constructors::MakePacked(System.UInt64) +arm64-advsimd Examples.PackedLeaf& Examples.GenericMethods::Next(Examples.PackedLeaf&,System.Int32) +arm64-advsimd Examples.PackedLeaf& Examples.GenericMethods::NextPacked(Examples.PackedLeaf&,System.Int32) +arm64-advsimd Examples.PackedLeaf& Examples.UnsafeLayouts::Add14int(Examples.PackedLeaf&,System.Int32) +arm64-advsimd Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nint(Examples.PackedLeaf&,System.IntPtr) +arm64-advsimd Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nuint(Examples.PackedLeaf&,System.UIntPtr) +arm64-advsimd Examples.PackedLeaf[] Examples.PrimitiveArrays::AllocatePacked(System.Int32) +arm64-advsimd Examples.Packet& Examples.UnsafeLayouts::Add16int(Examples.Packet&,System.Int32) +arm64-advsimd Examples.Packet& Examples.UnsafeLayouts::Add16nint(Examples.Packet&,System.IntPtr) +arm64-advsimd Examples.Packet& Examples.UnsafeLayouts::Add16nuint(Examples.Packet&,System.UIntPtr) +arm64-advsimd Examples.PooledItem Examples.ItemPool::Rent() +arm64-advsimd Examples.Signed16 Examples.Enums::IdentityS16(Examples.Signed16) +arm64-advsimd Examples.Signed16 Examples.Enums::ThroughCall(Examples.Signed16) +arm64-advsimd Examples.Signed16 Examples.PointerOperations::ReadEnum(Examples.Signed16*,System.Int32) +arm64-advsimd Examples.Signed16[] Examples.PrimitiveArrays::AllocateSigned16(System.Int32) +arm64-advsimd Examples.Signed32 Examples.Enums::IdentityS32(Examples.Signed32) +arm64-advsimd Examples.Signed32[] Examples.PrimitiveArrays::AllocateSigned32(System.Int32) +arm64-advsimd Examples.Signed64 Examples.Enums::IdentityS64(Examples.Signed64) +arm64-advsimd Examples.Signed64[] Examples.PrimitiveArrays::AllocateSigned64(System.Int32) +arm64-advsimd Examples.Signed8 Examples.Enums::Field(Examples.Signed8) +arm64-advsimd Examples.Signed8 Examples.Enums::IdentityS8(Examples.Signed8) +arm64-advsimd Examples.Signed8 Examples.Enums::Read(Examples.EnumRecord&) +arm64-advsimd Examples.Signed8 Examples.GenericMethods::EnumIdentity(Examples.Signed8) +arm64-advsimd Examples.Signed8 Examples.GenericMethods::Identity(Examples.Signed8) +arm64-advsimd Examples.Signed8& Examples.UnsafeLayouts::Add12int(Examples.Signed8&,System.Int32) +arm64-advsimd Examples.Signed8& Examples.UnsafeLayouts::Add12nint(Examples.Signed8&,System.IntPtr) +arm64-advsimd Examples.Signed8& Examples.UnsafeLayouts::Add12nuint(Examples.Signed8&,System.UIntPtr) +arm64-advsimd Examples.Signed8[] Examples.PrimitiveArrays::AllocateSigned8(System.Int32) +arm64-advsimd Examples.Tiles Examples.TileArrays::Hold(Examples.AggregateArrays/Pair[],System.Runtime.Intrinsics.Vector128`1[]) +arm64-advsimd Examples.Unsigned16 Examples.Enums::IdentityU16(Examples.Unsigned16) +arm64-advsimd Examples.Unsigned16[] Examples.PrimitiveArrays::AllocateUnsigned16(System.Int32) +arm64-advsimd Examples.Unsigned32 Examples.Enums::IdentityU32(Examples.Unsigned32) +arm64-advsimd Examples.Unsigned32[] Examples.PrimitiveArrays::AllocateUnsigned32(System.Int32) +arm64-advsimd Examples.Unsigned64 Examples.Enums::IdentityU64(Examples.Unsigned64) +arm64-advsimd Examples.Unsigned64 Examples.Enums::Local(Examples.Unsigned64) +arm64-advsimd Examples.Unsigned64 Examples.Enums::SetFlag(Examples.Unsigned64) +arm64-advsimd Examples.Unsigned64 Examples.Enums::WrongSetFlag(Examples.Unsigned64) +arm64-advsimd Examples.Unsigned64& Examples.UnsafeLayouts::Add13int(Examples.Unsigned64&,System.Int32) +arm64-advsimd Examples.Unsigned64& Examples.UnsafeLayouts::Add13nint(Examples.Unsigned64&,System.IntPtr) +arm64-advsimd Examples.Unsigned64& Examples.UnsafeLayouts::Add13nuint(Examples.Unsigned64&,System.UIntPtr) +arm64-advsimd Examples.Unsigned64[] Examples.PrimitiveArrays::AllocateUnsigned64(System.Int32) +arm64-advsimd Examples.Unsigned8 Examples.Enums::IdentityU8(Examples.Unsigned8) +arm64-advsimd Examples.Unsigned8[] Examples.PrimitiveArrays::AllocateUnsigned8(System.Int32) +arm64-advsimd Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int32(System.Runtime.Intrinsics.Vector256`1) +arm64-advsimd Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int64(System.Runtime.Intrinsics.Vector256`1) +arm64-advsimd Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt32(System.Runtime.Intrinsics.Vector256`1) +arm64-advsimd Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt64(System.Runtime.Intrinsics.Vector256`1) +arm64-advsimd Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ParameterCopy(Examples.ValueBitCasts/Limbs) +arm64-advsimd Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ReadAggregate(Examples.ValueBitCasts/Limbs&) +arm64-advsimd Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::RoundTrip(Examples.ValueBitCasts/Limbs) +arm64-advsimd Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128Byte(System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt32(System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt64(System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Boolean Examples.AggregateSpans::Copy(System.ReadOnlySpan`1,System.Span`1) +arm64-advsimd System.Boolean Examples.AggregateSpans::IsEmpty(System.Span`1) +arm64-advsimd System.Boolean Examples.AggregateSpans::VectorIsEmpty(System.ReadOnlySpan`1>) +arm64-advsimd System.Boolean Examples.Allowance::Spend(Examples.Allowance&,System.UInt64) +arm64-advsimd System.Boolean Examples.Bits::PowerI32(System.Int32) +arm64-advsimd System.Boolean Examples.Bits::PowerI64(System.Int64) +arm64-advsimd System.Boolean Examples.Bits::PowerNative(System.IntPtr) +arm64-advsimd System.Boolean Examples.Bits::PowerU32(System.UInt32) +arm64-advsimd System.Boolean Examples.Bits::PowerU64(System.UInt64) +arm64-advsimd System.Boolean Examples.Bits::PowerUNative(System.UIntPtr) +arm64-advsimd System.Boolean Examples.Bits::PrimitivePower32(System.Int32) +arm64-advsimd System.Boolean Examples.Bits::PrimitivePower64(System.Int64) +arm64-advsimd System.Boolean Examples.Bits::PrimitivePowerNative(System.IntPtr) +arm64-advsimd System.Boolean Examples.Bits::PrimitivePowerU32(System.UInt32) +arm64-advsimd System.Boolean Examples.Bits::PrimitivePowerU64(System.UInt64) +arm64-advsimd System.Boolean Examples.Bits::PrimitivePowerUNative(System.UIntPtr) +arm64-advsimd System.Boolean Examples.ClassTests::IsDog(Examples.Animal) +arm64-advsimd System.Boolean Examples.ComputedFlags::get_Both() +arm64-advsimd System.Boolean Examples.ComputedFlags::get_Middle() +arm64-advsimd System.Boolean Examples.ComputedFlags::get_NotOff() +arm64-advsimd System.Boolean Examples.ComputedFlags::get_Outer() +arm64-advsimd System.Boolean Examples.DivisionIntrinsics::EqualBytes256(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1) +arm64-advsimd System.Boolean Examples.DivisionIntrinsics::EqualSigned512(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1) +arm64-advsimd System.Boolean Examples.DivisionIntrinsics::EqualWords128(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Boolean Examples.DivisionIntrinsics::UnsafeEquals(System.UInt64) +arm64-advsimd System.Boolean Examples.DoublingAllowance::Examples.IAllowance.Spend(Examples.DoublingAllowance&) +arm64-advsimd System.Boolean Examples.DoublingAllowance::Spend(Examples.DoublingAllowance&,System.UInt64) +arm64-advsimd System.Boolean Examples.Forced::get_IsActive() +arm64-advsimd System.Boolean Examples.Gate`1::get_Open() +arm64-advsimd System.Boolean Examples.Gate`1::get_Open() +arm64-advsimd System.Boolean Examples.Hush`1::get_Silent() +arm64-advsimd System.Boolean Examples.IAllowance`1::Spend(Examples.Allowance&) +arm64-advsimd System.Boolean Examples.ILoud::Examples.IQuietByDefault.get_IsActive() +arm64-advsimd System.Boolean Examples.ILoudByDefault::get_IsActive() +arm64-advsimd System.Boolean Examples.IQuiet::Examples.ILoudByDefault.get_IsActive() +arm64-advsimd System.Boolean Examples.IQuietByDefault::get_IsActive() +arm64-advsimd System.Boolean Examples.IRefundingAllowance`1::Examples.IAllowance.Spend(Examples.RefundingAllowance&) +arm64-advsimd System.Boolean Examples.IntegerOperations::Greater32(System.Int32,System.Int32) +arm64-advsimd System.Boolean Examples.IntegerOperations::Greater64(System.Int64,System.Int64) +arm64-advsimd System.Boolean Examples.ItemPool::IsIdle(Examples.PooledItem) +arm64-advsimd System.Boolean Examples.Loops::AllZero(System.ReadOnlySpan`1) +arm64-advsimd System.Boolean Examples.MarshaledMemory::TryRead(System.ReadOnlySpan`1,System.UInt64&) +arm64-advsimd System.Boolean Examples.MarshaledMemory::TryReadAggregate(System.ReadOnlySpan`1,Examples.FourLimbs&) +arm64-advsimd System.Boolean Examples.MarshaledMemory::TryWrite(System.Span`1,System.UInt64&) +arm64-advsimd System.Boolean Examples.MemorySpans::BooleanWrite(System.Span`1,System.Int32,System.Boolean) +arm64-advsimd System.Boolean Examples.NullReferences::IsLive(System.UInt64&) +arm64-advsimd System.Boolean Examples.NullReferences::IsNull() +arm64-advsimd System.Boolean Examples.Objects::HasTag(Examples.Tagged) +arm64-advsimd System.Boolean Examples.Objects::IsNull(Examples.Account) +arm64-advsimd System.Boolean Examples.Objects::IsPresent(Examples.Account) +arm64-advsimd System.Boolean Examples.Objects::Same(Examples.Account,Examples.Account) +arm64-advsimd System.Boolean Examples.Objects::SameTag(Examples.Tagged,Examples.Tagged) +arm64-advsimd System.Boolean Examples.Objects::TwoMarkers() +arm64-advsimd System.Boolean Examples.PointerOperations::Greater(System.Byte*,System.Byte*) +arm64-advsimd System.Boolean Examples.PointerOperations::GreaterEqual(System.Byte*,System.Byte*) +arm64-advsimd System.Boolean Examples.PointerOperations::Less(System.Byte*,System.Byte*) +arm64-advsimd System.Boolean Examples.PointerOperations::LessEqual(System.Byte*,System.Byte*) +arm64-advsimd System.Boolean Examples.PooledItem::get_IsRented() +arm64-advsimd System.Boolean Examples.PrimitiveArrays::ReadBoolean(System.Boolean[],System.Int32) +arm64-advsimd System.Boolean Examples.RefundingAllowance::Spend(Examples.RefundingAllowance&,System.UInt64) +arm64-advsimd System.Boolean Examples.SpanCopies::TryCopy(System.Span`1,System.Span`1) +arm64-advsimd System.Boolean Examples.SpanCopies::TryCopyEnum(System.ReadOnlySpan`1,System.Span`1) +arm64-advsimd System.Boolean Examples.SpanCopies::TryCopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +arm64-advsimd System.Boolean Examples.StepOff::get_IsActive() +arm64-advsimd System.Boolean Examples.StepOn::get_IsActive() +arm64-advsimd System.Boolean Examples.Strings::SameLabel(System.String,System.String) +arm64-advsimd System.Boolean Examples.Strings::SameText() +arm64-advsimd System.Boolean Examples.UnsafeReferences::AreSame(System.Byte&,System.Byte&) +arm64-advsimd System.Boolean Examples.UnsafeReferences::Greater(System.Byte&,System.Byte&) +arm64-advsimd System.Boolean Examples.UnsafeReferences::Less(System.Byte&,System.Byte&) +arm64-advsimd System.Boolean Examples.ViewAccessors::Empty(System.Span`1) +arm64-advsimd System.Boolean Examples.ViewAccessors::PinnableNull(System.Span`1) +arm64-advsimd System.Boolean Examples.ViewAccessors::ReadOnlyEmpty(System.ReadOnlySpan`1) +arm64-advsimd System.Boolean Examples.ViewAccessors::ReadOnlyPinnableNull(System.ReadOnlySpan`1) +arm64-advsimd System.Boolean Examples.ViewAccessors::ReadOnlyReferenceNull(System.ReadOnlySpan`1) +arm64-advsimd System.Boolean Examples.ViewAccessors::ReferenceNull(System.Span`1) +arm64-advsimd System.Boolean Examples.WordFlag`1::get_IsActive() +arm64-advsimd System.Boolean Examples.WordFlag`1::get_IsActive() +arm64-advsimd System.Boolean& Examples.UnsafeLayouts::Add0int(System.Boolean&,System.Int32) +arm64-advsimd System.Boolean& Examples.UnsafeLayouts::Add0nint(System.Boolean&,System.IntPtr) +arm64-advsimd System.Boolean& Examples.UnsafeLayouts::Add0nuint(System.Boolean&,System.UIntPtr) +arm64-advsimd System.Byte Examples.Arguments::Narrow(System.Byte) +arm64-advsimd System.Byte Examples.Bits::ReverseU8(System.Byte) +arm64-advsimd System.Byte Examples.Counter::Parameter(System.Byte) +arm64-advsimd System.Byte Examples.GenericMethods::Identity(System.Byte) +arm64-advsimd System.Byte Examples.IntegerMath::ClampUInt8(System.Byte,System.Byte,System.Byte) +arm64-advsimd System.Byte Examples.IntegerMath::MaxUInt8(System.Byte,System.Byte) +arm64-advsimd System.Byte Examples.IntegerMath::MinUInt8(System.Byte,System.Byte) +arm64-advsimd System.Byte Examples.PrimitiveArrays::ReadByte(System.Byte[],System.Int32) +arm64-advsimd System.Byte Examples.SpanReferences::At(System.ReadOnlySpan`1&,System.Int32) +arm64-advsimd System.Byte Examples.StaticSpans::First() +arm64-advsimd System.Byte Examples.StaticSpans::Last() +arm64-advsimd System.Byte Examples.StaticSpans::PastEnd() +arm64-advsimd System.Byte Examples.StructMethods::Narrow(System.Byte) +arm64-advsimd System.Byte& Examples.UnsafeLayouts::Add3int(System.Byte&,System.Int32) +arm64-advsimd System.Byte& Examples.UnsafeLayouts::Add3nint(System.Byte&,System.IntPtr) +arm64-advsimd System.Byte& Examples.UnsafeLayouts::Add3nuint(System.Byte&,System.UIntPtr) +arm64-advsimd System.Byte& Examples.UnsafeReferences::AddSigned(System.Byte&,System.IntPtr) +arm64-advsimd System.Byte& Examples.UnsafeReferences::AddUnsigned(System.Byte&,System.UIntPtr) +arm64-advsimd System.Byte& Examples.UnsafeReferences::Subtract(System.Byte&,System.IntPtr) +arm64-advsimd System.Byte& Examples.UnsafeReferences::SubtractUnsigned(System.Byte&,System.UIntPtr) +arm64-advsimd System.Byte* Examples.PointerOperations::Subtract(System.Byte*,System.Int32) +arm64-advsimd System.Byte* Examples.PointerOperations::SubtractNative(System.Byte*,System.IntPtr) +arm64-advsimd System.Byte* Examples.UnsafeReferences::AddPointer(System.Byte*,System.Int32) +arm64-advsimd System.Byte* Examples.UnsafeReferences::AddPointerAggregate(System.Byte*,System.Int32) +arm64-advsimd System.Byte* Examples.UnsafeReferences::SubtractPointer(System.Byte*,System.Int32) +arm64-advsimd System.Byte* Examples.UnsafeReferences::SubtractPointerAggregate(System.Byte*,System.Int32) +arm64-advsimd System.Byte[] Examples.PrimitiveArrays::AllocateBytes(System.Int32) +arm64-advsimd System.Char Examples.MemorySpans::CharacterWrite(System.Span`1,System.Int32,System.Char) +arm64-advsimd System.Char Examples.NarrowIntegers::Character(System.Int32) +arm64-advsimd System.Char Examples.PointerOperations::ReadCharacter(System.Char*,System.Int32) +arm64-advsimd System.Char Examples.PrimitiveArrays::ReadChar(System.Char[],System.Int32) +arm64-advsimd System.Char& Examples.UnsafeLayouts::Add1int(System.Char&,System.Int32) +arm64-advsimd System.Char& Examples.UnsafeLayouts::Add1nint(System.Char&,System.IntPtr) +arm64-advsimd System.Char& Examples.UnsafeLayouts::Add1nuint(System.Char&,System.UIntPtr) +arm64-advsimd System.Int16 Examples.Bits::ReverseI16(System.Int16) +arm64-advsimd System.Int16 Examples.IntegerMath::AbsInt16(System.Int16) +arm64-advsimd System.Int16 Examples.IntegerMath::ClampInt16(System.Int16,System.Int16,System.Int16) +arm64-advsimd System.Int16 Examples.IntegerMath::MaxInt16(System.Int16,System.Int16) +arm64-advsimd System.Int16 Examples.IntegerMath::MinInt16(System.Int16,System.Int16) +arm64-advsimd System.Int16 Examples.NarrowIntegers::Identity(System.Int16) +arm64-advsimd System.Int16 Examples.PrimitiveArrays::ReadInt16(System.Int16[],System.Int32) +arm64-advsimd System.Int16 Examples.UnalignedMemory::ReadHalf(System.Byte&) +arm64-advsimd System.Int16& Examples.UnsafeLayouts::Add4int(System.Int16&,System.Int32) +arm64-advsimd System.Int16& Examples.UnsafeLayouts::Add4nint(System.Int16&,System.IntPtr) +arm64-advsimd System.Int16& Examples.UnsafeLayouts::Add4nuint(System.Int16&,System.UIntPtr) +arm64-advsimd System.Int16[] Examples.PrimitiveArrays::AllocateShorts(System.Int32) +arm64-advsimd System.Int32 Examples.AggregateArrays::ConstructedLength(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Arithmetic::AtLeastFive(System.Int32) +arm64-advsimd System.Int32 Examples.Arithmetic::FiveToOne(System.Int32) +arm64-advsimd System.Int32 Examples.Arithmetic::Larger(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Arithmetic::WordAdjusted(System.Int32) +arm64-advsimd System.Int32 Examples.Arithmetic::WordSized(System.Int32) +arm64-advsimd System.Int32 Examples.ArrayElementReferences::Read(System.Int32[],System.Int32) +arm64-advsimd System.Int32 Examples.ArrayElementReferences::Write(System.Int32[],System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.BinarySpans::TryWriteBig(System.Span`1) +arm64-advsimd System.Int32 Examples.BinarySpans::TryWriteLittle(System.Span`1) +arm64-advsimd System.Int32 Examples.Bits::Leading32(System.UInt32) +arm64-advsimd System.Int32 Examples.Bits::Leading64(System.UInt64) +arm64-advsimd System.Int32 Examples.Bits::LeadingNative(System.UIntPtr) +arm64-advsimd System.Int32 Examples.Bits::Log32(System.UInt32) +arm64-advsimd System.Int32 Examples.Bits::Log64(System.UInt64) +arm64-advsimd System.Int32 Examples.Bits::LogNative(System.UIntPtr) +arm64-advsimd System.Int32 Examples.Bits::Population32(System.UInt32) +arm64-advsimd System.Int32 Examples.Bits::Population64(System.UInt64) +arm64-advsimd System.Int32 Examples.Bits::PopulationNative(System.UIntPtr) +arm64-advsimd System.Int32 Examples.Bits::PrimitiveLeading32(System.Int32) +arm64-advsimd System.Int32 Examples.Bits::PrimitiveLeft32(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Bits::PrimitiveNarrow16(System.UInt32) +arm64-advsimd System.Int32 Examples.Bits::PrimitiveNarrow8(System.UInt32) +arm64-advsimd System.Int32 Examples.Bits::PrimitiveNarrowRotate16(System.UInt32,System.Int32) +arm64-advsimd System.Int32 Examples.Bits::PrimitiveNarrowRotate8(System.UInt32,System.Int32) +arm64-advsimd System.Int32 Examples.Bits::PrimitiveNarrowSigned(System.UInt32) +arm64-advsimd System.Int32 Examples.Bits::PrimitivePopulation32(System.Int32) +arm64-advsimd System.Int32 Examples.Bits::PrimitiveRight32(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Bits::PrimitiveTrailing32(System.Int32) +arm64-advsimd System.Int32 Examples.Bits::ReverseI32(System.Int32) +arm64-advsimd System.Int32 Examples.Bits::Trailing32(System.UInt32) +arm64-advsimd System.Int32 Examples.Bits::Trailing64(System.UInt64) +arm64-advsimd System.Int32 Examples.Bits::TrailingNative(System.UIntPtr) +arm64-advsimd System.Int32 Examples.Bits::WrongLog64(System.UInt64) +arm64-advsimd System.Int32 Examples.Capabilities::Endian() +arm64-advsimd System.Int32 Examples.Capabilities::Platform() +arm64-advsimd System.Int32 Examples.Capabilities::ReferenceFree() +arm64-advsimd System.Int32 Examples.Capabilities::VbmiCount(System.UInt64) +arm64-advsimd System.Int32 Examples.CheckedArithmetic::AddSigned(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +arm64-advsimd System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +arm64-advsimd System.Int32 Examples.ConstrainedCalls::ClosedBoth(Examples.ClosedInterfaceCounter&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::ClosedGeneric(Examples.GenericInterfaceCounter`1&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::DirectGeneric(Examples.GenericCounterValue`1&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::Explicit(Examples.ExplicitCounterValue&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::Generic(Examples.GenericCounterValue`1&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::GenericExplicit(Examples.ExplicitGenericCounterValue`1&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::Implicit(Examples.CounterValue&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.CounterValue&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.ExplicitCounterValue&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::IncrementAndRead>(Examples.ExplicitGenericCounterValue`1&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::Mutating(Examples.CounterValue&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::MutatingExplicit(Examples.ExplicitCounterValue&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::Read(Examples.CounterValue&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::Read(Examples.ExplicitCounterValue&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::ReadClosedInt(Examples.ClosedInterfaceCounter&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::ReadClosedInt>(Examples.GenericInterfaceCounter`1&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::ReadClosedLong(Examples.ClosedInterfaceCounter&) +arm64-advsimd System.Int32 Examples.ConstrainedCalls::TwoGenericOwners(Examples.GenericCounterValue`1&,Examples.GenericCounterValue`1&) +arm64-advsimd System.Int32 Examples.CounterValue::Read() +arm64-advsimd System.Int32 Examples.Enums::Branch(Examples.Signed8) +arm64-advsimd System.Int32 Examples.ExceptionDocumentation::Absolute(System.Int32) +arm64-advsimd System.Int32 Examples.ExceptionDocumentation::CheckedNarrow(System.Int64) +arm64-advsimd System.Int32 Examples.ExceptionDocumentation::Clamp(System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.ExplicitCounterValue::Examples.ICounterValue.Read() +arm64-advsimd System.Int32 Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Read() +arm64-advsimd System.Int32 Examples.Gauge::BelowFive() +arm64-advsimd System.Int32 Examples.Gauge::Blend(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Blended(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Bounded(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Capped(Examples.Gauge,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::CappedLevel() +arm64-advsimd System.Int32 Examples.Gauge::CappedSlack() +arm64-advsimd System.Int32 Examples.Gauge::Checked(Examples.Gauge,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::CheckedLevel() +arm64-advsimd System.Int32 Examples.Gauge::Clamp(System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Clamped(System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Current() +arm64-advsimd System.Int32 Examples.Gauge::Doubled(System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Floor(System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Floored(System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Gap(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::IsFive() +arm64-advsimd System.Int32 Examples.Gauge::Margin() +arm64-advsimd System.Int32 Examples.Gauge::Raise(Examples.Gauge,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::RaisedLevel() +arm64-advsimd System.Int32 Examples.Gauge::Saturate(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Slack() +arm64-advsimd System.Int32 Examples.Gauge::Smaller(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Total(System.Int32[]) +arm64-advsimd System.Int32 Examples.Gauge::Twice(System.Int32) +arm64-advsimd System.Int32 Examples.Gauge::Within(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.GenericCounterValue`1::Echo(System.Int32) +arm64-advsimd System.Int32 Examples.GenericCounterValue`1::Read() +arm64-advsimd System.Int32 Examples.GenericCounterValue`1::Read() +arm64-advsimd System.Int32 Examples.GenericInterfaceCounter`1::Read() +arm64-advsimd System.Int32 Examples.GenericLayouts::TupleSize() +arm64-advsimd System.Int32 Examples.GenericLayouts::VectorSize() +arm64-advsimd System.Int32 Examples.GenericMethods::Identity32(System.Int32) +arm64-advsimd System.Int32 Examples.GenericMethods::Identity(System.Int32) +arm64-advsimd System.Int32 Examples.GenericMethods::Mixed(System.Int32,System.Byte) +arm64-advsimd System.Int32 Examples.GenericMethods::PackedSize() +arm64-advsimd System.Int32 Examples.GenericMethods::Read32(System.Int32&) +arm64-advsimd System.Int32 Examples.GenericMethods::Read(System.Int32&) +arm64-advsimd System.Int32 Examples.GenericMethods::Size() +arm64-advsimd System.Int32 Examples.GenericMethods::Size>() +arm64-advsimd System.Int32 Examples.GenericMethods::VectorSize() +arm64-advsimd System.Int32 Examples.GenericMethods::WrongIdentity(System.Int32) +arm64-advsimd System.Int32 Examples.GenericOwners::SpanCount(System.ReadOnlySpan`1) +arm64-advsimd System.Int32 Examples.Guards::Count(System.Int32) +arm64-advsimd System.Int32 Examples.IntegerMath::AbsInt32(System.Int32) +arm64-advsimd System.Int32 Examples.IntegerMath::ClampInt32(System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.IntegerMath::MaxInt32(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.IntegerMath::MinInt32(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.IntegerMath::SignInt16(System.Int16) +arm64-advsimd System.Int32 Examples.IntegerMath::SignInt32(System.Int32) +arm64-advsimd System.Int32 Examples.IntegerMath::SignInt64(System.Int64) +arm64-advsimd System.Int32 Examples.IntegerMath::SignInt8(System.SByte) +arm64-advsimd System.Int32 Examples.IntegerMath::SignNative(System.IntPtr) +arm64-advsimd System.Int32 Examples.IntegerOperations::Negate32(System.Int32) +arm64-advsimd System.Int32 Examples.ItemPool::Trim(System.Int32) +arm64-advsimd System.Int32 Examples.ItemPool::get_Available() +arm64-advsimd System.Int32 Examples.ItemPool::get_Capacity() +arm64-advsimd System.Int32 Examples.ItemPool::get_Rented() +arm64-advsimd System.Int32 Examples.Loops::CountToFive(System.Int32) +arm64-advsimd System.Int32 Examples.Loops::Drain(System.ReadOnlySpan`1) +arm64-advsimd System.Int32 Examples.ManagedDispatch::LocalEscape(System.Int32) +arm64-advsimd System.Int32 Examples.ManagedDispatch::NullLength() +arm64-advsimd System.Int32 Examples.ManagedDispatch::Read(System.Int32&) +arm64-advsimd System.Int32 Examples.MemorySpans::ArraySliceWrite(System.Int32[],System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.MemorySpans::Existing(System.Int32&,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.MemorySpans::InPlaceArrayConstructor(System.Int32[],System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.MemorySpans::InPlaceArrayLength(System.Int32[],System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.MemorySpans::InPlacePointerSpan(System.Int32*,System.Int32) +arm64-advsimd System.Int32 Examples.MemorySpans::Read(System.ReadOnlySpan`1,System.Int32) +arm64-advsimd System.Int32 Examples.MemorySpans::ReferenceWrite(System.Span`1,System.Int32) +arm64-advsimd System.Int32 Examples.MemorySpans::ReinitializeArraySpan(System.Int32[],System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.MemorySpans::SliceRead(System.ReadOnlySpan`1,System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.MemorySpans::SliceWrite(System.Span`1,System.Int32,System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.NarrowIntegers::Call(System.Int32) +arm64-advsimd System.Int32 Examples.NarrowIntegers::Fields(System.Int32) +arm64-advsimd System.Int32 Examples.NarrowIntegers::SignedByte(System.Int32) +arm64-advsimd System.Int32 Examples.NarrowIntegers::SignedShort(System.Int64) +arm64-advsimd System.Int32 Examples.NarrowIntegers::UnsignedShort(System.Int32) +arm64-advsimd System.Int32 Examples.NativeIntegers::Narrow(System.IntPtr) +arm64-advsimd System.Int32 Examples.NativeIntegers::SignedSize() +arm64-advsimd System.Int32 Examples.NativeIntegers::UnsignedSize() +arm64-advsimd System.Int32 Examples.NodeArrays::Count(Examples.Node[]) +arm64-advsimd System.Int32 Examples.PooledItem::get_Id() +arm64-advsimd System.Int32 Examples.PrimitiveArrays::AllocateAndWrite(System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveArrays::AllocatePackedAndWrite(System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveArrays::Length(System.Int32[]) +arm64-advsimd System.Int32 Examples.PrimitiveArrays::ReadAlias(System.Int32[],System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveArrays::ReadGeneric(System.Int32[],System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveArrays::ReadGenericHelper(System.Int32[],System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveArrays::ReadHelper(System.Int32[],System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveArrays::ReadInt32(System.Int32[],System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveArrays::WriteGenericHelper(System.Int32[],System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveOperations::Alias(System.Int16&,System.Int16&,System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveOperations::Choose(System.Int32) +arm64-advsimd System.Int32 Examples.PrimitiveOperations::PacketSize() +arm64-advsimd System.Int32 Examples.PrimitiveOperations::ReadWrite(System.Int32&,System.Int32) +arm64-advsimd System.Int32 Examples.SampleArrays::Count(Examples.Samples) +arm64-advsimd System.Int32 Examples.SampleArrays::CountAfterAllocation(Examples.Node,System.Int32[]) +arm64-advsimd System.Int32 Examples.SampleArrays::CountAfterDeposit(Examples.Account,System.Int32[]) +arm64-advsimd System.Int32 Examples.SampleArrays::CountAfterRefill(Examples.Samples,Examples.Samples,System.Int32[]) +arm64-advsimd System.Int32 Examples.SampleArrays::ReadAfterAllocation(Examples.Node,System.Int32[]) +arm64-advsimd System.Int32 Examples.SampleArrays::ReadAfterEcho(Examples.Account,System.Int32[],System.Int32) +arm64-advsimd System.Int32 Examples.SampleArrays::RecordIndex(Examples.Account,System.Int32) +arm64-advsimd System.Int32 Examples.SampleArrays::RefillRead(Examples.Samples,System.Int32[],System.Int32) +arm64-advsimd System.Int32 Examples.SampleArrays::Share(Examples.Samples,Examples.Samples) +arm64-advsimd System.Int32 Examples.SampleArrays::Sum(Examples.Samples,System.Int32[]) +arm64-advsimd System.Int32 Examples.Sequential::PackedSize() +arm64-advsimd System.Int32 Examples.Sequential::RecordSize() +arm64-advsimd System.Int32 Examples.SpanConstructors::AggregateLocal(Examples.AggregateSpans/Words&) +arm64-advsimd System.Int32 Examples.SpanConstructors::BytePointerLocal(System.Byte*,System.Int32) +arm64-advsimd System.Int32 Examples.SpanConstructors::LayoutLocal(Examples.AggregateSpans/VectorRecord*,System.Int32) +arm64-advsimd System.Int32 Examples.SpanConstructors::Local(System.Int32&) +arm64-advsimd System.Int32 Examples.SpanHelpers::Count(System.ReadOnlySpan`1) +arm64-advsimd System.Int32 Examples.SpanReferences::Argument(System.ReadOnlySpan`1) +arm64-advsimd System.Int32 Examples.SpanReferences::Caller(System.Byte[]) +arm64-advsimd System.Int32 Examples.SpanReferences::Copy(System.ReadOnlySpan`1&) +arm64-advsimd System.Int32 Examples.SpanReferences::First(System.Span`1&) +arm64-advsimd System.Int32 Examples.SpanReferences::Forward(System.ReadOnlySpan`1&) +arm64-advsimd System.Int32 Examples.SpanReferences::Front(System.Span`1&) +arm64-advsimd System.Int32 Examples.SpanReferences::GenericFront(System.Int32[]) +arm64-advsimd System.Int32 Examples.SpanReferences::Length(System.ReadOnlySpan`1&) +arm64-advsimd System.Int32 Examples.SpanReferences::Sum(System.ReadOnlySpan`1&) +arm64-advsimd System.Int32 Examples.SpanReferences::Tail(System.ReadOnlySpan`1&) +arm64-advsimd System.Int32 Examples.SpanReferences::Temporary(System.ReadOnlySpan`1) +arm64-advsimd System.Int32 Examples.SpanTables::Count() +arm64-advsimd System.Int32 Examples.StackAllocation::FillAndRead(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.StackAllocation::Length(System.Int32) +arm64-advsimd System.Int32 Examples.StackAllocation::UninitializedRead() +arm64-advsimd System.Int32 Examples.StackAllocation::WrittenBeforeRead(System.Int32) +arm64-advsimd System.Int32 Examples.StackAllocation::ZeroInitialized() +arm64-advsimd System.Int32 Examples.TailCalls::Excluded(System.Int32) +arm64-advsimd System.Int32 Examples.TailCalls::Forward(System.Int32) +arm64-advsimd System.Int32 Examples.TailCalls::ForwardReference(System.Int32&) +arm64-advsimd System.Int32 Examples.TailCalls::ForwardThrow(System.Int32) +arm64-advsimd System.Int32 Examples.TailCalls::Ignore(System.Int32&) +arm64-advsimd System.Int32 Examples.TailCalls::Increment(System.Int32) +arm64-advsimd System.Int32 Examples.TailCalls::LocalReference(System.Int32) +arm64-advsimd System.Int32 Examples.TailCalls::Raise(System.Int32) +arm64-advsimd System.Int32 Examples.TailCalls::Read(System.Int32&) +arm64-advsimd System.Int32 Examples.TileArrays::PairCount(Examples.Tiles) +arm64-advsimd System.Int32 Examples.TupleDivision::MathOutInt32(System.Int32,System.Int32,System.Int32&) +arm64-advsimd System.Int32 Examples.TupleDivision::MathQuotient(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.TupleDivision::MathWrongQuotient(System.Int32,System.Int32) +arm64-advsimd System.Int32 Examples.TypeTests::Classify(System.Int32) +arm64-advsimd System.Int32 Examples.TypeTests::Classify(System.Int32) +arm64-advsimd System.Int32 Examples.TypeTests::Marker(System.Int32) +arm64-advsimd System.Int32 Examples.TypeTests::Other(System.Int32) +arm64-advsimd System.Int32 Examples.TypeTests::ReferenceCases(System.Int32) +arm64-advsimd System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +arm64-advsimd System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +arm64-advsimd System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size0() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size1() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size10() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size11() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size12() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size13() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size14() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size15() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size16() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size17() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size18() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size2() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size3() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size4() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size5() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size6() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size7() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size8() +arm64-advsimd System.Int32 Examples.UnsafeLayouts::Size9() +arm64-advsimd System.Int32 Examples.VectorFields::NestedSize() +arm64-advsimd System.Int32 Examples.WordReader::Read(System.Int32[]) +arm64-advsimd System.Int32 Examples.WordReader::ReadSaved(System.Int32[]) +arm64-advsimd System.Int32 Examples.WordReader::SkipAndPeek(System.Int32[]) +arm64-advsimd System.Int32& Examples.ArrayElementReferences::Element(System.Int32[],System.Int32) +arm64-advsimd System.Int32& Examples.UnsafeLayouts::Add6int(System.Int32&,System.Int32) +arm64-advsimd System.Int32& Examples.UnsafeLayouts::Add6nint(System.Int32&,System.IntPtr) +arm64-advsimd System.Int32& Examples.UnsafeLayouts::Add6nuint(System.Int32&,System.UIntPtr) +arm64-advsimd System.Int32& Examples.ViewAccessors::ArrayData(System.Int32[]) +arm64-advsimd System.Int32* Examples.StackAllocation::EscapingStorage() +arm64-advsimd System.Int32[] Examples.PrimitiveArrays::Allocate(System.Int32) +arm64-advsimd System.Int32[] Examples.PrimitiveArrays::Identity(System.Int32[]) +arm64-advsimd System.Int32[] Examples.SampleArrays::ValuesOf(Examples.Samples) +arm64-advsimd System.Int64 Examples.Bits::PrimitiveCounts64(System.Int64) +arm64-advsimd System.Int64 Examples.Bits::PrimitiveLeft64(System.Int64,System.Int32) +arm64-advsimd System.Int64 Examples.Bits::PrimitiveRight64(System.Int64,System.Int32) +arm64-advsimd System.Int64 Examples.Bits::ReverseI64(System.Int64) +arm64-advsimd System.Int64 Examples.ExceptionDocumentation::CheckedAdd(System.Int64,System.Int64) +arm64-advsimd System.Int64 Examples.ExceptionDocumentation::Divide(System.Int64,System.Int64) +arm64-advsimd System.Int64 Examples.IntegerMath::AbsInt64(System.Int64) +arm64-advsimd System.Int64 Examples.IntegerMath::ClampInt64(System.Int64,System.Int64,System.Int64) +arm64-advsimd System.Int64 Examples.IntegerMath::MaxInt64(System.Int64,System.Int64) +arm64-advsimd System.Int64 Examples.IntegerMath::MinInt64(System.Int64,System.Int64) +arm64-advsimd System.Int64 Examples.IntegerMath::WrongMin(System.Int64,System.Int64) +arm64-advsimd System.Int64 Examples.IntegerOperations::Negate64(System.Int64) +arm64-advsimd System.Int64 Examples.OutlineCounters::SignedLiteral() +arm64-advsimd System.Int64 Examples.PointerOperations::Difference(System.Byte*,System.Byte*) +arm64-advsimd System.Int64 Examples.PooledItem::get_Payload() +arm64-advsimd System.Int64 Examples.PrimitiveArrays::ReadInt64(System.Int64[],System.Int32) +arm64-advsimd System.Int64 Examples.TupleDivision::MathOutInt64(System.Int64,System.Int64,System.Int64&) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure0int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure0nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure0nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure10int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure10nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure10nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure11int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure11nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure11nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure12int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure12nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure12nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure13int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure13nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure13nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure14int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure14nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure14nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure15int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure15nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure15nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure16int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure16nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure16nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure17int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure17nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure17nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure18int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure18nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure18nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure1int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure1nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure1nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure2int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure2nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure2nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure3int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure3nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure3nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure4int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure4nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure4nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure5int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure5nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure5nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure6int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure6nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure6nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure7int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure7nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure7nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure8int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure8nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure8nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure9int(System.Int32) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure9nint(System.IntPtr) +arm64-advsimd System.Int64 Examples.UnsafeLayouts::Measure9nuint(System.UIntPtr) +arm64-advsimd System.Int64 Examples.WideProduct::BigMul32(System.Int32,System.Int32) +arm64-advsimd System.Int64 Examples.WideProduct::SignedBigMul(System.Int64,System.Int64,System.Int64&) +arm64-advsimd System.Int64& Examples.UnsafeLayouts::Add8int(System.Int64&,System.Int32) +arm64-advsimd System.Int64& Examples.UnsafeLayouts::Add8nint(System.Int64&,System.IntPtr) +arm64-advsimd System.Int64& Examples.UnsafeLayouts::Add8nuint(System.Int64&,System.UIntPtr) +arm64-advsimd System.Int64[] Examples.PrimitiveArrays::AllocateLongs(System.Int32) +arm64-advsimd System.IntPtr Examples.Arguments::Native(System.IntPtr) +arm64-advsimd System.IntPtr Examples.Bits::PrimitiveCountsNative(System.IntPtr) +arm64-advsimd System.IntPtr Examples.Bits::PrimitiveLeftNative(System.IntPtr,System.Int32) +arm64-advsimd System.IntPtr Examples.Bits::PrimitiveRightNative(System.IntPtr,System.Int32) +arm64-advsimd System.IntPtr Examples.Bits::ReverseNative(System.IntPtr) +arm64-advsimd System.IntPtr Examples.GenericMethods::Identity(System.IntPtr) +arm64-advsimd System.IntPtr Examples.GenericMethods::NativeIdentity(System.IntPtr) +arm64-advsimd System.IntPtr Examples.IntegerMath::AbsNative(System.IntPtr) +arm64-advsimd System.IntPtr Examples.IntegerMath::ClampNative(System.IntPtr,System.IntPtr,System.IntPtr) +arm64-advsimd System.IntPtr Examples.IntegerMath::MaxNative(System.IntPtr,System.IntPtr) +arm64-advsimd System.IntPtr Examples.IntegerMath::MinNative(System.IntPtr,System.IntPtr) +arm64-advsimd System.IntPtr Examples.MemorySpans::NativeRead(System.ReadOnlySpan`1,System.Int32) +arm64-advsimd System.IntPtr Examples.NativeIntegers::Add(System.IntPtr,System.IntPtr) +arm64-advsimd System.IntPtr Examples.NativeIntegers::CheckedAdd(System.IntPtr,System.IntPtr) +arm64-advsimd System.IntPtr Examples.NativeIntegers::Choose(System.IntPtr) +arm64-advsimd System.IntPtr Examples.NativeIntegers::Divide(System.IntPtr,System.IntPtr) +arm64-advsimd System.IntPtr Examples.NativeIntegers::Fields(Examples.NativePair&,System.IntPtr,System.UIntPtr) +arm64-advsimd System.IntPtr Examples.NativeIntegers::Increment(System.IntPtr&) +arm64-advsimd System.IntPtr Examples.NativeIntegers::LocalCall(System.IntPtr) +arm64-advsimd System.IntPtr Examples.NativeIntegers::ReadWrite(System.IntPtr&,System.IntPtr) +arm64-advsimd System.IntPtr Examples.NativeIntegers::Widen(System.Int32) +arm64-advsimd System.IntPtr Examples.PrimitiveArrays::ReadIntPtr(System.IntPtr[],System.Int32) +arm64-advsimd System.IntPtr Examples.UnsafeReferences::ByteOffset(System.Byte&,System.Byte&) +arm64-advsimd System.IntPtr& Examples.UnsafeLayouts::Add10int(System.IntPtr&,System.Int32) +arm64-advsimd System.IntPtr& Examples.UnsafeLayouts::Add10nint(System.IntPtr&,System.IntPtr) +arm64-advsimd System.IntPtr& Examples.UnsafeLayouts::Add10nuint(System.IntPtr&,System.UIntPtr) +arm64-advsimd System.ReadOnlySpan`1 Examples.AggregateArrays::ReadOnlyView(Examples.AggregateArrays/Pair[]) +arm64-advsimd System.ReadOnlySpan`1 Examples.AggregateSpans::ReadOnlyFactory(Examples.AggregateSpans/Words&,System.Int32) +arm64-advsimd System.ReadOnlySpan`1 Examples.AggregateSpans::WordsView(System.ReadOnlySpan`1) +arm64-advsimd System.ReadOnlySpan`1 Examples.SpanConstructors::PassReadOnly(System.ReadOnlySpan`1) +arm64-advsimd System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyDefaultReset(Examples.AggregateSpans/Words&) +arm64-advsimd System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyEmpty() +arm64-advsimd System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyBytes(System.ReadOnlySpan`1) +arm64-advsimd System.ReadOnlySpan`1 Examples.StaticSpans::get_Lookup() +arm64-advsimd System.ReadOnlySpan`1 Examples.MemorySpans::ArrayRange(System.Int32[],System.Int32,System.Int32) +arm64-advsimd System.ReadOnlySpan`1 Examples.MemorySpans::ArrayReadOnly(System.Int32[]) +arm64-advsimd System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlySingle(System.Int32&) +arm64-advsimd System.ReadOnlySpan`1> Examples.SpanConstructors::Vector(System.Runtime.Intrinsics.Vector128`1&) +arm64-advsimd System.ReadOnlySpan`1> Examples.SpanConstructors::VectorPointer(System.Runtime.Intrinsics.Vector128`1*,System.Int32) +arm64-advsimd System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyWords(System.ReadOnlySpan`1) +arm64-advsimd System.ReadOnlySpan`1 Examples.SpanTables::get_Words() +arm64-advsimd System.ReadOnlySpan`1 Examples.SpanTables::get_Powers() +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.MarshaledMemory::ReadVector(System.ReadOnlySpan`1) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::Read128(System.Byte&) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::ReadAligned128(System.Byte*) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128Byte(Examples.ValueBitCasts/Pair) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadBytes128(Examples.VectorFields/Bytes128) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadInts128(Examples.VectorFields/Ints128) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt32(Examples.ValueBitCasts/Pair) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.AggregateArrays::ReadVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.AggregateSpans::ReadVector(System.ReadOnlySpan`1>) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::ReverseLimbs(System.UInt64,System.UInt64) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::UnsafeReturn(System.UInt64) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Identity>(System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Vector(System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.UnsafeReferences::ReadHalf(Examples.FourLimbs&) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt64(Examples.ValueBitCasts/Pair) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Read128(Examples.VectorFields/Holder128) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Write128(Examples.VectorFields/Holder128&,System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17int(System.Runtime.Intrinsics.Vector128`1&,System.Int32) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nint(System.Runtime.Intrinsics.Vector128`1&,System.IntPtr) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nuint(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::Half(Examples.FourLimbs&) +arm64-advsimd System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::SubtractVector(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int32(Examples.ValueBitCasts/Limbs) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int64(Examples.ValueBitCasts/Limbs) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadLongs256(Examples.VectorFields/Longs256) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::CreateWords(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt32(Examples.ValueBitCasts/Limbs) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadUInts256(Examples.VectorFields/UInts256) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeReferences::Vector(Examples.FourLimbs&) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::CopyLocal(Examples.ValueBitCasts/Limbs) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::ReadLocal(System.Runtime.Intrinsics.Vector256`1&) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt64(Examples.ValueBitCasts/Limbs) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Read256(Examples.VectorFields/Holder256) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Reinterpret(Examples.VectorFields/Words256&,System.Runtime.Intrinsics.Vector256`1) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Write256(Examples.VectorFields/Holder256&,System.Runtime.Intrinsics.Vector256`1) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::WritePacked(Examples.VectorFields/Packed256&,System.Runtime.Intrinsics.Vector256`1) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18int(System.Runtime.Intrinsics.Vector256`1&,System.Int32) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nint(System.Runtime.Intrinsics.Vector256`1&,System.IntPtr) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nuint(System.Runtime.Intrinsics.Vector256`1&,System.UIntPtr) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1& Examples.VectorFields::Address256(Examples.VectorFields/Holder256&) +arm64-advsimd System.Runtime.Intrinsics.Vector256`1[] Examples.PrimitiveArrays::AllocateVector(System.Int32) +arm64-advsimd System.Runtime.Intrinsics.Vector512`1 Examples.UnalignedMemory::Read512(System.Byte&) +arm64-advsimd System.SByte Examples.Bits::ReverseI8(System.SByte) +arm64-advsimd System.SByte Examples.IntegerMath::AbsInt8(System.SByte) +arm64-advsimd System.SByte Examples.IntegerMath::ClampInt8(System.SByte,System.SByte,System.SByte) +arm64-advsimd System.SByte Examples.IntegerMath::MaxInt8(System.SByte,System.SByte) +arm64-advsimd System.SByte Examples.IntegerMath::MinInt8(System.SByte,System.SByte) +arm64-advsimd System.SByte Examples.PrimitiveArrays::ReadSByte(System.SByte[],System.Int32) +arm64-advsimd System.SByte Examples.TupleDivision::MathSByteRemainder(System.SByte,System.SByte) +arm64-advsimd System.SByte Examples.UnalignedMemory::ReadSigned(System.Byte&) +arm64-advsimd System.SByte& Examples.UnsafeLayouts::Add2int(System.SByte&,System.Int32) +arm64-advsimd System.SByte& Examples.UnsafeLayouts::Add2nint(System.SByte&,System.IntPtr) +arm64-advsimd System.SByte& Examples.UnsafeLayouts::Add2nuint(System.SByte&,System.UIntPtr) +arm64-advsimd System.Span`1 Examples.AggregateArrays::Slice(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +arm64-advsimd System.Span`1 Examples.AggregateArrays::View(Examples.AggregateArrays/Pair[]) +arm64-advsimd System.Span`1 Examples.AggregateSpans::Factory(Examples.AggregateSpans/Words&,System.Int32) +arm64-advsimd System.Span`1 Examples.AggregateSpans::Slice(System.Span`1,System.Int32,System.Int32) +arm64-advsimd System.Span`1 Examples.SpanConstructors::Aggregate(Examples.AggregateSpans/Words&) +arm64-advsimd System.Span`1 Examples.SpanConstructors::Pointer(Examples.AggregateSpans/Words*,System.Int32) +arm64-advsimd System.Span`1 Examples.EnumBuffers::Slice(System.Span`1,System.Int32) +arm64-advsimd System.Span`1 Examples.ReinterpretedSpans::EnumWords(System.Span`1) +arm64-advsimd System.Span`1 Examples.MemorySpans::ArrayAsSpan(System.Boolean[]) +arm64-advsimd System.Span`1 Examples.AggregateSpans::Bytes(System.Span`1) +arm64-advsimd System.Span`1 Examples.AggregateSpans::LayoutBytes(System.Span`1) +arm64-advsimd System.Span`1 Examples.AggregateSpans::VectorBytes(System.Span`1>) +arm64-advsimd System.Span`1 Examples.ReinterpretedSpans::Bytes(System.Span`1) +arm64-advsimd System.Span`1 Examples.ReinterpretedSpans::EnumBytes(System.Span`1) +arm64-advsimd System.Span`1 Examples.ReinterpretedSpans::Expanded(System.Span`1) +arm64-advsimd System.Span`1 Examples.MemorySpans::ArrayConstructor(System.Char[]) +arm64-advsimd System.Span`1 Examples.MemorySpans::ArrayWritable(System.Int32[]) +arm64-advsimd System.Span`1 Examples.ReinterpretedSpans::SameWidth(System.Span`1) +arm64-advsimd System.Span`1 Examples.SpanConstructors::DefaultReset(System.Int32&) +arm64-advsimd System.Span`1 Examples.SpanConstructors::Empty() +arm64-advsimd System.Span`1 Examples.SpanConstructors::Pass(System.Span`1) +arm64-advsimd System.Span`1 Examples.SpanConstructors::Single(System.Int32&) +arm64-advsimd System.Span`1 Examples.MemorySpans::ArrayAsSpanStart(System.IntPtr[],System.Int32) +arm64-advsimd System.Span`1> Examples.AggregateArrays::VectorView(System.Runtime.Intrinsics.Vector128`1[]) +arm64-advsimd System.Span`1 Examples.ReinterpretedSpans::Words(System.Span`1) +arm64-advsimd System.Span`1 Examples.MemorySpans::ArrayAsSpanRange(System.UIntPtr[],System.Int32,System.Int32) +arm64-advsimd System.String Examples.Guards::Required(System.String) +arm64-advsimd System.String Examples.Strings::OrNone(System.String) +arm64-advsimd System.String Examples.Strings::Pick(System.Boolean,System.String,System.String) +arm64-advsimd System.String Examples.Strings::Relabel(Examples.Labelled,System.String) +arm64-advsimd System.String[] Examples.TagArrays::Create(System.Int32) +arm64-advsimd System.UInt16 Examples.Bits::ReverseU16(System.UInt16) +arm64-advsimd System.UInt16 Examples.IntegerMath::ClampUInt16(System.UInt16,System.UInt16,System.UInt16) +arm64-advsimd System.UInt16 Examples.IntegerMath::MaxUInt16(System.UInt16,System.UInt16) +arm64-advsimd System.UInt16 Examples.IntegerMath::MinUInt16(System.UInt16,System.UInt16) +arm64-advsimd System.UInt16 Examples.PrimitiveArrays::ReadUInt16(System.UInt16[],System.Int32) +arm64-advsimd System.UInt16& Examples.UnsafeLayouts::Add5int(System.UInt16&,System.Int32) +arm64-advsimd System.UInt16& Examples.UnsafeLayouts::Add5nint(System.UInt16&,System.IntPtr) +arm64-advsimd System.UInt16& Examples.UnsafeLayouts::Add5nuint(System.UInt16&,System.UIntPtr) +arm64-advsimd System.UInt32 Examples.Arguments::Address(System.UInt32) +arm64-advsimd System.UInt32 Examples.Arguments::Alias(System.UInt32) +arm64-advsimd System.UInt32 Examples.Arguments::CallerIsolation(System.UInt32) +arm64-advsimd System.UInt32 Examples.Arguments::Overwrite(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.Arguments::ReassignReference(System.UInt32&,System.UInt32&) +arm64-advsimd System.UInt32 Examples.Arithmetic::AtMostFive(System.UInt32) +arm64-advsimd System.UInt32 Examples.Bits::Left32(System.UInt32,System.Int32) +arm64-advsimd System.UInt32 Examples.Bits::PrimitiveLeadingU32(System.UInt32) +arm64-advsimd System.UInt32 Examples.Bits::PrimitiveLeftU32(System.UInt32,System.Int32) +arm64-advsimd System.UInt32 Examples.Bits::PrimitiveLogU32(System.UInt32) +arm64-advsimd System.UInt32 Examples.Bits::PrimitivePopulationU32(System.UInt32) +arm64-advsimd System.UInt32 Examples.Bits::PrimitiveRightU32(System.UInt32,System.Int32) +arm64-advsimd System.UInt32 Examples.Bits::PrimitiveTrailingU32(System.UInt32) +arm64-advsimd System.UInt32 Examples.Bits::ReverseU32(System.UInt32) +arm64-advsimd System.UInt32 Examples.Bits::Right32(System.UInt32,System.Int32) +arm64-advsimd System.UInt32 Examples.Bits::RoundUp32(System.UInt32) +arm64-advsimd System.UInt32 Examples.Capabilities::AesFlags() +arm64-advsimd System.UInt32 Examples.Capabilities::ArmFlags() +arm64-advsimd System.UInt32 Examples.Capabilities::Flags() +arm64-advsimd System.UInt32 Examples.Capabilities::VbmiFlags() +arm64-advsimd System.UInt32 Examples.CheckedArithmetic::Add(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.CheckedConversions::Narrow(System.UInt64) +arm64-advsimd System.UInt32 Examples.Division::Quotient(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.ExceptionDocumentation::Profile(System.UInt32) +arm64-advsimd System.UInt32 Examples.ExceptionDocumentation::SpanItem(System.Int32) +arm64-advsimd System.UInt32 Examples.GenericMethods::ClearPacked(System.UInt32) +arm64-advsimd System.UInt32 Examples.GenericMethods::SwapPacked(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.GenericOwners::Address(System.UInt32) +arm64-advsimd System.UInt32 Examples.GenericOwners::Combined(System.UInt32) +arm64-advsimd System.UInt32 Examples.GenericOwners::Identity(System.UInt32) +arm64-advsimd System.UInt32 Examples.GenericOwners::Nested(System.UInt32) +arm64-advsimd System.UInt32 Examples.GenericOwners::NestedHelper(System.UInt32) +arm64-advsimd System.UInt32 Examples.GenericOwners::Repeated(System.UInt32) +arm64-advsimd System.UInt32 Examples.Guards::Index(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.IntegerMath::ClampUInt32(System.UInt32,System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.IntegerMath::MaxUInt32(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.IntegerMath::MinUInt32(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::AddTwo(System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::Bump(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::CopyUntilZero(System.ReadOnlySpan`1,System.UInt32&) +arm64-advsimd System.UInt32 Examples.Loops::CountCalls(System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::CountUntil(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::CountUpToFive(System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::Countdown(System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::Decrement(System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::DownThenUp(System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::FeatureCountdown(System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::InnerCount(Examples.Loops/Outer,System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::ReachFive(System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::TallyTwice(System.UInt32) +arm64-advsimd System.UInt32 Examples.Loops::WrongCountdown(System.UInt32) +arm64-advsimd System.UInt32 Examples.NarrowTotals::ReadSum() +arm64-advsimd System.UInt32 Examples.NarrowTotals::WriteRead(System.UInt32) +arm64-advsimd System.UInt32 Examples.NativeIntegers::NarrowUnsigned(System.UIntPtr) +arm64-advsimd System.UInt32 Examples.Nested::Packed(System.UInt32) +arm64-advsimd System.UInt32 Examples.OwnerHelper`1::Identity(System.UInt32) +arm64-advsimd System.UInt32 Examples.OwnerHelper`1::Select(System.UInt32,System.UInt64) +arm64-advsimd System.UInt32 Examples.OwnerOuter`1/Helper::Identity(System.UInt32) +arm64-advsimd System.UInt32 Examples.OwnerOuter`1/Inner`1::Identity(System.UInt32) +arm64-advsimd System.UInt32 Examples.PrimitiveArrays::ReadUInt32(System.UInt32[],System.Int32) +arm64-advsimd System.UInt32 Examples.ReinterpretedSpans::Aliased(System.Span`1) +arm64-advsimd System.UInt32 Examples.Sequential::ReadWrite(Examples.Record&,System.UInt32) +arm64-advsimd System.UInt32 Examples.SpanTables::Word(System.Int32) +arm64-advsimd System.UInt32 Examples.Throws::ActualValue(System.UInt32) +arm64-advsimd System.UInt32 Examples.Throws::ArgumentInner(System.UInt32) +arm64-advsimd System.UInt32 Examples.Throws::Inner(System.UInt32) +arm64-advsimd System.UInt32 Examples.Throws::Null(System.UInt32) +arm64-advsimd System.UInt32 Examples.Throws::Percent(System.UInt32) +arm64-advsimd System.UInt32 Examples.Throws::Quotient(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32 Examples.WordReader::RangeEnd() +arm64-advsimd System.UInt32 Examples.WordReader::ReplaceRemaining(System.UInt32) +arm64-advsimd System.UInt32 Examples.WordReader::Reserve() +arm64-advsimd System.UInt32 SelectorExamples.One.Collision::Add(System.UInt32,System.UInt32) +arm64-advsimd System.UInt32& Examples.UnsafeLayouts::Add7int(System.UInt32&,System.Int32) +arm64-advsimd System.UInt32& Examples.UnsafeLayouts::Add7nint(System.UInt32&,System.IntPtr) +arm64-advsimd System.UInt32& Examples.UnsafeLayouts::Add7nuint(System.UInt32&,System.UIntPtr) +arm64-advsimd System.UInt64 Examples.Account::get_Headroom() +arm64-advsimd System.UInt64 Examples.AddStep::Apply(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.Aliases::Update(System.UInt64,System.UInt64,System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.Animal::Loudness() +arm64-advsimd System.UInt64 Examples.Arguments::Struct(Examples.Packet) +arm64-advsimd System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.Arithmetic::Alias(System.UInt64&,System.UInt64&) +arm64-advsimd System.UInt64 Examples.Arithmetic::Calls(System.UInt64) +arm64-advsimd System.UInt64 Examples.Arithmetic::Choose(System.Boolean,System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.Arithmetic::Distance(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.Arithmetic::Increment(System.UInt64) +arm64-advsimd System.UInt64 Examples.Arithmetic::LargeStruct(System.UInt64) +arm64-advsimd System.UInt64 Examples.Arithmetic::PastEnd(System.UInt64&) +arm64-advsimd System.UInt64 Examples.Arithmetic::Read(Examples.Packet&) +arm64-advsimd System.UInt64 Examples.Arithmetic::ReadLarge(Examples.LargePacket&) +arm64-advsimd System.UInt64 Examples.Arithmetic::ReadValue(Examples.Packet) +arm64-advsimd System.UInt64 Examples.Arithmetic::Struct(System.UInt64) +arm64-advsimd System.UInt64 Examples.Arithmetic::StructCopy(System.UInt64) +arm64-advsimd System.UInt64 Examples.Arithmetic::TypeCheck(System.UInt64) +arm64-advsimd System.UInt64 Examples.Arithmetic::WrongAdd(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.BinarySpans::ReadBig(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.BinarySpans::ReadLittle(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.BinarySpans::TryReadBig(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.BinarySpans::TryReadLittle(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.Bird::Legs() +arm64-advsimd System.UInt64 Examples.Bits::Left64(System.UInt64,System.Int32) +arm64-advsimd System.UInt64 Examples.Bits::PrimitiveCountsU64(System.UInt64) +arm64-advsimd System.UInt64 Examples.Bits::PrimitiveLeftU64(System.UInt64,System.Int32) +arm64-advsimd System.UInt64 Examples.Bits::PrimitiveRightU64(System.UInt64,System.Int32) +arm64-advsimd System.UInt64 Examples.Bits::ReverseU64(System.UInt64) +arm64-advsimd System.UInt64 Examples.Bits::Right64(System.UInt64,System.Int32) +arm64-advsimd System.UInt64 Examples.Bits::RoundTrip(System.UInt64) +arm64-advsimd System.UInt64 Examples.Bits::RoundUp64(System.UInt64) +arm64-advsimd System.UInt64 Examples.Bits::WrongRoundTrip(System.UInt64) +arm64-advsimd System.UInt64 Examples.Blocks::CopyLocal(System.UInt64,System.UInt32) +arm64-advsimd System.UInt64 Examples.Blocks::CopyParameter(System.UInt64,System.UInt64,System.UInt32) +arm64-advsimd System.UInt64 Examples.Blocks::FillLocal(System.Byte,System.UInt32) +arm64-advsimd System.UInt64 Examples.Blocks::FillParameter(System.UInt64,System.Byte,System.UInt32) +arm64-advsimd System.UInt64 Examples.CheckedArithmetic::Add64(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.ComputedFlags::TraceBoth(System.UInt64) +arm64-advsimd System.UInt64 Examples.ComputedFlags::TraceChained(System.UInt64) +arm64-advsimd System.UInt64 Examples.ComputedFlags::TraceGated(System.UInt64) +arm64-advsimd System.UInt64 Examples.ComputedFlags::TraceNotOff(System.UInt64) +arm64-advsimd System.UInt64 Examples.ComputedFlags::TraceSilent(System.UInt64) +arm64-advsimd System.UInt64 Examples.ConstantFlags::Count(System.UInt64) +arm64-advsimd System.UInt64 Examples.ConstantFlags::CountOn(System.UInt64) +arm64-advsimd System.UInt64 Examples.ConstantFlags::Trace(System.UInt64) +arm64-advsimd System.UInt64 Examples.ConstantFlags::TraceOff(System.UInt64) +arm64-advsimd System.UInt64 Examples.ConstantFlags::TraceThenCount(System.UInt64) +arm64-advsimd System.UInt64 Examples.Constructors::Checked(System.UInt64) +arm64-advsimd System.UInt64 Examples.Constructors::Default() +arm64-advsimd System.UInt64 Examples.Constructors::Nested(System.UInt64) +arm64-advsimd System.UInt64 Examples.Constructors::Packed(System.UInt64) +arm64-advsimd System.UInt64 Examples.Constructors::Read(System.UInt64) +arm64-advsimd System.UInt64 Examples.Counter::Add(System.UInt64) +arm64-advsimd System.UInt64 Examples.Counter::Mix(System.UInt64,System.UInt32,System.UInt16,System.Byte) +arm64-advsimd System.UInt64 Examples.Counter::Read() +arm64-advsimd System.UInt64 Examples.Counter::WriteRead(System.UInt64) +arm64-advsimd System.UInt64 Examples.Counter::WrongWriteRead(System.UInt64) +arm64-advsimd System.UInt64 Examples.DerivedFlags::Count(System.UInt64) +arm64-advsimd System.UInt64 Examples.DerivedFlags::CountLoud(System.UInt64) +arm64-advsimd System.UInt64 Examples.DerivedFlags::Trace(System.UInt64) +arm64-advsimd System.UInt64 Examples.DerivedFlags::TraceQuiet(System.UInt64) +arm64-advsimd System.UInt64 Examples.Division::Quotient64(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.DivisionIntrinsics::DefinedLocal(System.UInt64) +arm64-advsimd System.UInt64 Examples.DivisionIntrinsics::PrunedUnsafeLocal(System.UInt64) +arm64-advsimd System.UInt64 Examples.DivisionIntrinsics::ReadLow(System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.UInt64 Examples.DivisionIntrinsics::UnsafeCall(System.UInt64) +arm64-advsimd System.UInt64 Examples.DivisionIntrinsics::UnsafeHigh(System.UInt64) +arm64-advsimd System.UInt64 Examples.DivisionIntrinsics::UnsafeLow(System.UInt64) +arm64-advsimd System.UInt64 Examples.DivisionIntrinsics::UnsafeLowNext(System.UInt64) +arm64-advsimd System.UInt64 Examples.Dog::Legs() +arm64-advsimd System.UInt64 Examples.Dog::Loudness() +arm64-advsimd System.UInt64 Examples.ExceptionDocumentation::UnsignedDivide(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericEnvelope`1::Read() +arm64-advsimd System.UInt64 Examples.GenericLayouts::Composed(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericLayouts::Field(Examples.GenericPair`1&) +arm64-advsimd System.UInt64 Examples.GenericLayouts::Roundtrip(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericMethods::Choose(System.Boolean,System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericMethods::First(System.UInt64,System.UInt32) +arm64-advsimd System.UInt64 Examples.GenericMethods::Forward(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericMethods::Identity64(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericMethods::Identity(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericMethods::Instance(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericMethods::Nested(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericMethods::Repeated(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericMethods::Select(System.Boolean,System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericMethods::TwoTypes(System.UInt64,System.UInt32) +arm64-advsimd System.UInt64 Examples.GenericOwners::NestedSpanFirst(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.GenericOwners::OwnerSpanFirst(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.GenericOwners::SpanFirst(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.GenericOwners::SpanSecond(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.GenericPair`1::GetSecond() +arm64-advsimd System.UInt64 Examples.GenericPicker::Pick(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::Count>(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::CountWide(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::Pad,System.UInt32>(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::Pad,System.UInt64>(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::PadNarrow(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::PadWide(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::Trace>(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::TraceNarrow(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::Width,System.UInt32>(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::Width,System.UInt64>(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::WidthNarrow(System.UInt64) +arm64-advsimd System.UInt64 Examples.GenericReceivers::WidthWide(System.UInt64) +arm64-advsimd System.UInt64 Examples.IMeasureFive::Examples.IMeasure.Measure(System.UInt64) +arm64-advsimd System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +arm64-advsimd System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +arm64-advsimd System.UInt64 Examples.IScale::Scale(System.UInt64) +arm64-advsimd System.UInt64 Examples.IScaleMore::Examples.IScale.Scale(System.UInt64) +arm64-advsimd System.UInt64 Examples.IScaleMost::Examples.IScale.Scale(System.UInt64) +arm64-advsimd System.UInt64 Examples.IScaleSide::Examples.IScale.Scale(System.UInt64) +arm64-advsimd System.UInt64 Examples.InParameters::Constrained(Examples.InWord&,Examples.InWord&) +arm64-advsimd System.UInt64 Examples.InParameters::ConstrainedSum(Examples.InWord&,System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.InParameters::Sum(Examples.InWord&,System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.InWord::Examples.IInWord.Sum(System.ReadOnlySpan`1& modreq(System.Runtime.InteropServices.InAttribute)) +arm64-advsimd System.UInt64 Examples.InitCounter::get_Value() +arm64-advsimd System.UInt64 Examples.IntegerMath::ClampUInt64(System.UInt64,System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.IntegerMath::MaxUInt64(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.IntegerMath::MinUInt64(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.Loops::Sum(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.Loops::SumIndexed(System.UInt64[]) +arm64-advsimd System.UInt64 Examples.Loops::SumThroughLocal(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.Loops::SumThroughSpan(System.UInt64[]) +arm64-advsimd System.UInt64 Examples.Loops::TwoStructs(System.UInt32) +arm64-advsimd System.UInt64 Examples.MarshaledMemory::Read(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.Measured`1::Width(System.UInt64) +arm64-advsimd System.UInt64 Examples.Measured`1::Width(System.UInt64) +arm64-advsimd System.UInt64 Examples.MostSpecificBodies::Five(System.UInt64) +arm64-advsimd System.UInt64 Examples.MostSpecificBodies::Measured(System.UInt64) +arm64-advsimd System.UInt64 Examples.MostSpecificBodies::More(System.UInt64) +arm64-advsimd System.UInt64 Examples.MostSpecificBodies::Most(System.UInt64) +arm64-advsimd System.UInt64 Examples.MostSpecificBodies::Own(System.UInt64) +arm64-advsimd System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +arm64-advsimd System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +arm64-advsimd System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +arm64-advsimd System.UInt64 Examples.Nested::Alias(System.UInt64) +arm64-advsimd System.UInt64 Examples.Nested::Copy(System.UInt64) +arm64-advsimd System.UInt64 Examples.Nested::Explicit(System.UInt64) +arm64-advsimd System.UInt64 Examples.Nested::Pass(System.UInt64) +arm64-advsimd System.UInt64 Examples.Nested::Read(Examples.Leaf) +arm64-advsimd System.UInt64 Examples.Nested::ReadOuter(Examples.NestedRecord) +arm64-advsimd System.UInt64 Examples.Nested::Sized(System.UInt64) +arm64-advsimd System.UInt64 Examples.Nested::Snapshot(System.UInt64) +arm64-advsimd System.UInt64 Examples.Nested::WrongCopy(System.UInt64) +arm64-advsimd System.UInt64 Examples.NestedConstructor::Sum() +arm64-advsimd System.UInt64 Examples.NullReferences::Optional(Examples.FourLimbs&,System.Boolean) +arm64-advsimd System.UInt64 Examples.NullReferences::Read() +arm64-advsimd System.UInt64 Examples.NullReferences::ReadField() +arm64-advsimd System.UInt64 Examples.NullReferences::Unchecked(Examples.FourLimbs&,System.Boolean) +arm64-advsimd System.UInt64 Examples.Objects::BalanceOf(Examples.Account) +arm64-advsimd System.UInt64 Examples.Objects::Describe(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.Objects::Fresh(System.UInt32) +arm64-advsimd System.UInt64 Examples.Objects::IdOf(Examples.Shape) +arm64-advsimd System.UInt64 Examples.Objects::LinkedValue(Examples.Node,Examples.Node) +arm64-advsimd System.UInt64 Examples.Objects::Replace(Examples.Account,System.UInt64) +arm64-advsimd System.UInt64 Examples.Objects::SecondValue(Examples.Node) +arm64-advsimd System.UInt64 Examples.Objects::UnderFive(Examples.Account) +arm64-advsimd System.UInt64 Examples.Objects::Withdraw(Examples.Account,System.UInt64) +arm64-advsimd System.UInt64 Examples.Odometer::Next() +arm64-advsimd System.UInt64 Examples.OutlineCountdown::Drain() +arm64-advsimd System.UInt64 Examples.OutlineCountdown::DrainAtLeastOnce() +arm64-advsimd System.UInt64 Examples.OutlineCounters::ExchangeAdd(System.UInt64) +arm64-advsimd System.UInt64 Examples.OutlineCounters::UnsignedLiteral() +arm64-advsimd System.UInt64 Examples.OutlineCounters::WrongExchangeAdd(System.UInt64) +arm64-advsimd System.UInt64 Examples.OwnerHelper`1::First(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.OwnerOuter`1/Inner`1::First(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.PackedConstructor::Sum() +arm64-advsimd System.UInt64 Examples.PointerOperations::WriteEnum(Examples.Unsigned64*,System.UInt64) +arm64-advsimd System.UInt64 Examples.PrimitiveArrays::ReadUInt64(System.UInt64[],System.Int32) +arm64-advsimd System.UInt64 Examples.ScaleOwn::Scale(System.UInt64) +arm64-advsimd System.UInt64 Examples.Sequential::Packed(System.UInt64) +arm64-advsimd System.UInt64 Examples.SkipInitialization::Read(System.UInt64&) +arm64-advsimd System.UInt64 Examples.SpanHelpers::First(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanHelpers::Second(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.SpanReferences::Back(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanReferences::BigEndianWord(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanReferences::ByteSecond(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanReferences::Construct(System.ReadOnlySpan`1) +arm64-advsimd System.UInt64 Examples.SpanReferences::CursorSum(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanReferences::Element(System.ReadOnlySpan`1&,System.Int32) +arm64-advsimd System.UInt64 Examples.SpanReferences::FirstHigh(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanReferences::GenericHead(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanReferences::Head(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanReferences::High(System.Span`1&,System.Int32) +arm64-advsimd System.UInt64 Examples.SpanReferences::HighAddress(System.ReadOnlySpan`1&,System.Int32) +arm64-advsimd System.UInt64 Examples.SpanReferences::ReadOnlyHigh(System.ReadOnlySpan`1&,System.Int32) +arm64-advsimd System.UInt64 Examples.SpanReferences::Second(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanReferences::Word(System.ReadOnlySpan`1&) +arm64-advsimd System.UInt64 Examples.SpanTables::First() +arm64-advsimd System.UInt64 Examples.SpanTables::Power(System.Int32) +arm64-advsimd System.UInt64 Examples.StaticInterfaces::AddCounted(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.StaticInterfaces::XorPlain(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.StaticValues::Read(Examples.Word256&) +arm64-advsimd System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.Allowance&) +arm64-advsimd System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.DoublingAllowance&) +arm64-advsimd System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.RefundingAllowance&) +arm64-advsimd System.UInt64 Examples.StaticVirtualDefaults::SpendDefault(Examples.Allowance&) +arm64-advsimd System.UInt64 Examples.StaticVirtualDefaults::SpendOverride(Examples.DoublingAllowance&) +arm64-advsimd System.UInt64 Examples.StaticVirtualDefaults::SpendRefunding(Examples.RefundingAllowance&) +arm64-advsimd System.UInt64 Examples.StructMethods::Arguments(System.UInt64) +arm64-advsimd System.UInt64 Examples.StructMethods::ByValue(System.UInt64) +arm64-advsimd System.UInt64 Examples.StructMethods::ConstructLocal(System.UInt64) +arm64-advsimd System.UInt64 Examples.StructMethods::Initialized(System.UInt64) +arm64-advsimd System.UInt64 Examples.StructMethods::Mutate(System.UInt64) +arm64-advsimd System.UInt64 Examples.StructMethods::Replace(System.UInt64) +arm64-advsimd System.UInt64 Examples.TriplePrice::get_Price() +arm64-advsimd System.UInt64 Examples.TupleDivision::Item1(System.ValueTuple`2&) +arm64-advsimd System.UInt64 Examples.UnalignedMemory::ReadAlignedWord(System.Byte*) +arm64-advsimd System.UInt64 Examples.UnalignedMemory::ReadPointerWord(System.Byte*) +arm64-advsimd System.UInt64 Examples.UnalignedMemory::ReadWord(System.Byte&) +arm64-advsimd System.UInt64 Examples.UnsafeReferences::PastEnd(Examples.FourLimbs&) +arm64-advsimd System.UInt64 Examples.UnsafeReferences::Read(Examples.FourLimbs&) +arm64-advsimd System.UInt64 Examples.UnsafeReferences::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 Examples.VirtualCalls::DogLegs() +arm64-advsimd System.UInt64 Examples.VirtualCalls::LegsOf(Examples.Animal) +arm64-advsimd System.UInt64 Examples.VirtualCalls::LoudnessOf(Examples.Animal) +arm64-advsimd System.UInt64 Examples.WideProduct::BigMul(System.UInt64,System.UInt64,System.UInt64&) +arm64-advsimd System.UInt64 Examples.WideProduct::UnsignedBigMul32(System.UInt32,System.UInt32) +arm64-advsimd System.UInt64 Examples.WordReader::ReplaceRemainingWide(System.UInt32) +arm64-advsimd System.UInt64 Examples.XorStep::Examples.IBinaryStep.Apply(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 SelectorExamples.One.Collision::Add(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64 SelectorExamples.Two.Collision::Add(System.UInt64,System.UInt64) +arm64-advsimd System.UInt64& Examples.Arithmetic::Escape() +arm64-advsimd System.UInt64& Examples.UnsafeLayouts::Add9int(System.UInt64&,System.Int32) +arm64-advsimd System.UInt64& Examples.UnsafeLayouts::Add9nint(System.UInt64&,System.IntPtr) +arm64-advsimd System.UInt64& Examples.UnsafeLayouts::Add9nuint(System.UInt64&,System.UIntPtr) +arm64-advsimd System.UInt64& Examples.UnsafeReferences::Pointer(System.Byte*) +arm64-advsimd System.UInt64& Examples.UnsafeReferences::SubtractElements32(System.UInt64&,System.Int32) +arm64-advsimd System.UInt64& Examples.UnsafeReferences::SubtractElementsNative(System.UInt64&,System.IntPtr) +arm64-advsimd System.UInt64& Examples.UnsafeReferences::SubtractElementsUnsigned(System.UInt64&,System.UIntPtr) +arm64-advsimd System.UInt64& Examples.UnsafeReferences::Word(Examples.FourLimbs&) +arm64-advsimd System.UIntPtr Examples.Bits::LeftNative(System.UIntPtr,System.Int32) +arm64-advsimd System.UIntPtr Examples.Bits::PrimitiveCountsUNative(System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.Bits::PrimitiveLeftUNative(System.UIntPtr,System.Int32) +arm64-advsimd System.UIntPtr Examples.Bits::PrimitiveRightUNative(System.UIntPtr,System.Int32) +arm64-advsimd System.UIntPtr Examples.Bits::ReverseUNative(System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.Bits::RightNative(System.UIntPtr,System.Int32) +arm64-advsimd System.UIntPtr Examples.Bits::RoundUpNative(System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.IntegerMath::ClampUNative(System.UIntPtr,System.UIntPtr,System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.IntegerMath::MaxUNative(System.UIntPtr,System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.IntegerMath::MinUNative(System.UIntPtr,System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.MemorySpans::NativeWrite(System.Span`1,System.Int32,System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.NativeIntegers::AddUnsigned(System.UIntPtr,System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.NativeIntegers::CheckedAddUnsigned(System.UIntPtr,System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.NativeIntegers::DivideUnsigned(System.UIntPtr,System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.NativeIntegers::ReadWriteUnsigned(System.UIntPtr&,System.UIntPtr) +arm64-advsimd System.UIntPtr Examples.NativeIntegers::Shift(System.UIntPtr,System.Int32) +arm64-advsimd System.UIntPtr Examples.NativeIntegers::WidenUnsigned(System.UInt32) +arm64-advsimd System.UIntPtr Examples.PrimitiveArrays::ReadUIntPtr(System.UIntPtr[],System.Int32) +arm64-advsimd System.UIntPtr& Examples.UnsafeLayouts::Add11int(System.UIntPtr&,System.Int32) +arm64-advsimd System.UIntPtr& Examples.UnsafeLayouts::Add11nint(System.UIntPtr&,System.IntPtr) +arm64-advsimd System.UIntPtr& Examples.UnsafeLayouts::Add11nuint(System.UIntPtr&,System.UIntPtr) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathByte(System.Byte,System.Byte) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathInt16(System.Int16,System.Int16) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathInt32(System.Int32,System.Int32) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathInt64(System.Int64,System.Int64) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathIntPtr(System.IntPtr,System.IntPtr) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathSByte(System.SByte,System.SByte) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathUInt16(System.UInt16,System.UInt16) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathUInt32(System.UInt32,System.UInt32) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathUInt64(System.UInt64,System.UInt64) +arm64-advsimd System.ValueTuple`2 Examples.TupleDivision::MathUIntPtr(System.UIntPtr,System.UIntPtr) +arm64-advsimd System.Void Examples.Account::.ctor() +arm64-advsimd System.Void Examples.Account::Deposit(System.UInt64) +arm64-advsimd System.Void Examples.AggregateArrays::Write(Examples.AggregateArrays/Pair[],System.Int32,Examples.AggregateArrays/Pair) +arm64-advsimd System.Void Examples.AggregateArrays::WriteVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32,System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Void Examples.AggregateSpanFill::Aliased(System.Span`1) +arm64-advsimd System.Void Examples.AggregateSpanFill::Generic(System.Span`1>,Examples.GenericPair`1) +arm64-advsimd System.Void Examples.AggregateSpanFill::Vector128(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Void Examples.AggregateSpanFill::Vector256(System.Span`1>,System.Runtime.Intrinsics.Vector256`1) +arm64-advsimd System.Void Examples.AggregateSpanFill::Vector512(System.Span`1>,System.Runtime.Intrinsics.Vector512`1) +arm64-advsimd System.Void Examples.AggregateSpanFill::Words(System.Span`1,Examples.AggregateSpans/Words) +arm64-advsimd System.Void Examples.AggregateSpans::Clear(System.Span`1) +arm64-advsimd System.Void Examples.AggregateSpans::Write(System.Span`1,Examples.AggregateSpans/Words) +arm64-advsimd System.Void Examples.AggregateSpans::WriteVector(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Void Examples.Animal::.ctor() +arm64-advsimd System.Void Examples.Arguments::Increment(System.UInt32&) +arm64-advsimd System.Void Examples.Arithmetic::Mutate(System.UInt64&) +arm64-advsimd System.Void Examples.BinarySpans::WriteBig(System.Span`1) +arm64-advsimd System.Void Examples.BinarySpans::WriteLittle(System.Span`1) +arm64-advsimd System.Void Examples.Bird::.ctor() +arm64-advsimd System.Void Examples.Blocks::Copy(System.Byte&,System.Byte&,System.UInt32) +arm64-advsimd System.Void Examples.Blocks::CopyAligned(System.Byte&,System.Byte&,System.UInt32) +arm64-advsimd System.Void Examples.Blocks::CopyPointer(System.Byte*,System.Byte*,System.UInt32) +arm64-advsimd System.Void Examples.Blocks::CopyPointerAligned(System.Byte*,System.Byte*,System.UInt32) +arm64-advsimd System.Void Examples.Blocks::Fill(System.Byte&,System.Byte,System.UInt32) +arm64-advsimd System.Void Examples.Blocks::FillAligned(System.Byte&,System.Byte,System.UInt32) +arm64-advsimd System.Void Examples.Blocks::FillPointer(System.Byte*,System.Byte,System.UInt32) +arm64-advsimd System.Void Examples.Blocks::FillPointerAligned(System.Byte*,System.Byte,System.UInt32) +arm64-advsimd System.Void Examples.Bucket::.ctor() +arm64-advsimd System.Void Examples.CheckedConstructor::.ctor(System.UInt64) +arm64-advsimd System.Void Examples.Circle::.ctor(System.UInt64,System.UInt64) +arm64-advsimd System.Void Examples.ConstructorLeaf::.ctor(System.UInt32) +arm64-advsimd System.Void Examples.Counter::.ctor(System.UInt64) +arm64-advsimd System.Void Examples.Counter::Increment(System.Byte&) +arm64-advsimd System.Void Examples.Counter::Replace(Examples.Counter) +arm64-advsimd System.Void Examples.Counter::Set(System.UInt64) +arm64-advsimd System.Void Examples.CounterValue::Increment() +arm64-advsimd System.Void Examples.DefaultConstructor::.ctor() +arm64-advsimd System.Void Examples.DivisionIntrinsics::Init128(System.Runtime.Intrinsics.Vector128`1&) +arm64-advsimd System.Void Examples.DivisionIntrinsics::Init256(System.Runtime.Intrinsics.Vector256`1&) +arm64-advsimd System.Void Examples.DivisionIntrinsics::Init512(System.Runtime.Intrinsics.Vector512`1&) +arm64-advsimd System.Void Examples.DivisionIntrinsics::SwapHalves(System.ReadOnlySpan`1,System.Span`1) +arm64-advsimd System.Void Examples.DivisionIntrinsics::UnsafeStore(System.Runtime.Intrinsics.Vector128`1&,System.UInt64) +arm64-advsimd System.Void Examples.Dog::.ctor() +arm64-advsimd System.Void Examples.Enums::Assign(Examples.Unsigned64&,Examples.Unsigned64) +arm64-advsimd System.Void Examples.ExceptionDocumentation::Reference(System.UInt64&) +arm64-advsimd System.Void Examples.ExplicitCounterValue::Examples.ICounterValue.Increment() +arm64-advsimd System.Void Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Increment() +arm64-advsimd System.Void Examples.Gauge::.ctor() +arm64-advsimd System.Void Examples.Gauge::Settle() +arm64-advsimd System.Void Examples.GenericEnvelope`1::.ctor(System.UInt64,System.UInt64) +arm64-advsimd System.Void Examples.GenericMethods::Clear(Examples.PackedLeaf&) +arm64-advsimd System.Void Examples.GenericMethods::Swap32(System.Int32&,System.Int32&) +arm64-advsimd System.Void Examples.GenericMethods::Swap(Examples.PackedLeaf&,Examples.PackedLeaf&) +arm64-advsimd System.Void Examples.GenericMethods::Swap(System.Int32&,System.Int32&) +arm64-advsimd System.Void Examples.GenericMethods::SwapRecord(Examples.PackedLeaf&,Examples.PackedLeaf&) +arm64-advsimd System.Void Examples.GenericPair`1::.ctor(Examples.GenericPair`1,Examples.GenericPair`1) +arm64-advsimd System.Void Examples.GenericPair`1::.ctor(System.UInt32,System.UInt32) +arm64-advsimd System.Void Examples.GenericPair`1::.ctor(System.UInt64,System.UInt64) +arm64-advsimd System.Void Examples.GenericPair`1::SetSecond(System.UInt64) +arm64-advsimd System.Void Examples.InParameters::Add(Examples.InWord&,Examples.InWord&,Examples.InWord&) +arm64-advsimd System.Void Examples.InWord::Examples.IInWord.Add(Examples.InWord& modreq(System.Runtime.InteropServices.InAttribute),Examples.InWord&) +arm64-advsimd System.Void Examples.ItemPool::.ctor(System.Int32) +arm64-advsimd System.Void Examples.ItemPool::Prewarm(System.Int32) +arm64-advsimd System.Void Examples.ItemPool::Return(Examples.PooledItem) +arm64-advsimd System.Void Examples.Labelled::.ctor() +arm64-advsimd System.Void Examples.Loops::Fill(System.Span`1,System.UInt32) +arm64-advsimd System.Void Examples.Loops::FillIndexed(System.UInt32[],System.UInt32) +arm64-advsimd System.Void Examples.Loops::FillThrough(System.Span`1,System.UInt32) +arm64-advsimd System.Void Examples.Loops::FillThroughSpan(System.UInt32[],System.UInt32) +arm64-advsimd System.Void Examples.Loops::FillWords(System.UInt64&,System.UInt64,System.Int32) +arm64-advsimd System.Void Examples.Loops::FillWordsDown(System.UInt64&,System.UInt64,System.Int32) +arm64-advsimd System.Void Examples.Loops::Put(System.Span`1,System.UInt32) +arm64-advsimd System.Void Examples.Loops::Stamp(System.UInt32&,System.UInt32) +arm64-advsimd System.Void Examples.Marker::.ctor() +arm64-advsimd System.Void Examples.MarshaledMemory::Write(System.Span`1,System.UInt64&) +arm64-advsimd System.Void Examples.MarshaledMemory::WriteAggregate(System.Span`1,Examples.FourLimbs&) +arm64-advsimd System.Void Examples.MemorySpans::ClearBytes(System.Span`1) +arm64-advsimd System.Void Examples.MemorySpans::FillCharacters(System.Span`1,System.Char) +arm64-advsimd System.Void Examples.MemorySpans::FillIntegers(System.Span`1,System.Int32) +arm64-advsimd System.Void Examples.MemorySpans::FillLongSlice(System.Span`1,System.Int32,System.Int32,System.Int64) +arm64-advsimd System.Void Examples.MemorySpans::Write(System.Span`1,System.Int32,System.Int32) +arm64-advsimd System.Void Examples.NestedConstructor::.ctor(System.UInt64) +arm64-advsimd System.Void Examples.Node::.ctor() +arm64-advsimd System.Void Examples.NodeArrays::Put(Examples.Shape[],System.Int32,Examples.Shape) +arm64-advsimd System.Void Examples.NodeArrays::Refill(Examples.Bucket,Examples.Node[]) +arm64-advsimd System.Void Examples.NullReferences::Write(System.UInt64) +arm64-advsimd System.Void Examples.NullReferences::WriteField(System.UInt64) +arm64-advsimd System.Void Examples.Objects::Bump(Examples.Account,System.UInt64) +arm64-advsimd System.Void Examples.Objects::BumpBoth(Examples.Account,Examples.Account,System.UInt64) +arm64-advsimd System.Void Examples.Objects::ClearTag(Examples.Tagged) +arm64-advsimd System.Void Examples.Objects::CopyTag(Examples.Tagged,Examples.Tagged) +arm64-advsimd System.Void Examples.Objects::DepositBoth(Examples.Account,Examples.Account,System.UInt64) +arm64-advsimd System.Void Examples.Objects::DepositFrom(Examples.Account,Examples.Account,System.UInt32) +arm64-advsimd System.Void Examples.Objects::DepositTimes(Examples.Account,System.UInt64,System.UInt32) +arm64-advsimd System.Void Examples.Objects::FillTag(Examples.Tagged,Examples.Tagged) +arm64-advsimd System.Void Examples.Objects::Link(Examples.Node,Examples.Node) +arm64-advsimd System.Void Examples.Objects::LinkBoth(Examples.Node,Examples.Node,Examples.Node) +arm64-advsimd System.Void Examples.Objects::Relink(Examples.Node,Examples.Node,Examples.Node) +arm64-advsimd System.Void Examples.Objects::SetLimit(Examples.Account,System.UInt32) +arm64-advsimd System.Void Examples.OutlineCountdown::Clear() +arm64-advsimd System.Void Examples.PackedConstructor::.ctor(System.UInt64) +arm64-advsimd System.Void Examples.PackedConstructor::.ctor(System.UInt64,System.Byte) +arm64-advsimd System.Void Examples.PooledItem::.ctor(Examples.ItemPool,System.Int32) +arm64-advsimd System.Void Examples.PooledItem::set_IsRented(System.Boolean) +arm64-advsimd System.Void Examples.PooledItem::set_Payload(System.Int64) +arm64-advsimd System.Void Examples.PrimitiveArrays::FillEnums(Examples.AllocationTag[],Examples.AllocationTag) +arm64-advsimd System.Void Examples.PrimitiveArrays::FillGeneric(Examples.AllocationTag[],Examples.AllocationTag) +arm64-advsimd System.Void Examples.PrimitiveArrays::FillGeneric(System.Int32[],System.Int32) +arm64-advsimd System.Void Examples.PrimitiveArrays::FillIntegers(System.Int32[],System.Int32) +arm64-advsimd System.Void Examples.PrimitiveArrays::FillRangeEnums(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +arm64-advsimd System.Void Examples.PrimitiveArrays::FillRangeGeneric(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +arm64-advsimd System.Void Examples.PrimitiveArrays::FillRangeGeneric(System.Int32[],System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Void Examples.PrimitiveArrays::FillRangeIntegers(System.Int32[],System.Int32,System.Int32,System.Int32) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteBoolean(System.Boolean[],System.Int32,System.Boolean) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteByte(System.Byte[],System.Int32,System.Byte) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteChar(System.Char[],System.Int32,System.Char) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteGeneric(System.Int32[],System.Int32,System.Int32) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteInt16(System.Int16[],System.Int32,System.Int16) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteInt32(System.Int32[],System.Int32,System.Int32) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteInt64(System.Int64[],System.Int32,System.Int64) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteIntPtr(System.IntPtr[],System.Int32,System.IntPtr) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteSByte(System.SByte[],System.Int32,System.SByte) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteUInt16(System.UInt16[],System.Int32,System.UInt16) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteUInt32(System.UInt32[],System.Int32,System.UInt32) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteUInt64(System.UInt64[],System.Int32,System.UInt64) +arm64-advsimd System.Void Examples.PrimitiveArrays::WriteUIntPtr(System.UIntPtr[],System.Int32,System.UIntPtr) +arm64-advsimd System.Void Examples.SampleArrays::Refill(Examples.Samples,System.Int32[]) +arm64-advsimd System.Void Examples.Samples::.ctor() +arm64-advsimd System.Void Examples.Shape::.ctor(System.UInt64) +arm64-advsimd System.Void Examples.SkipInitialization::Skip(Examples.FourLimbs&) +arm64-advsimd System.Void Examples.SkipInitialization::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +arm64-advsimd System.Void Examples.SpanCopies::Copy(System.Span`1,System.Span`1) +arm64-advsimd System.Void Examples.SpanCopies::CopyLongs(System.ReadOnlySpan`1,System.Span`1) +arm64-advsimd System.Void Examples.SpanCopies::CopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +arm64-advsimd System.Void Examples.SpanReferences/Limbs::.ctor(System.ReadOnlySpan`1&,System.Boolean) +arm64-advsimd System.Void Examples.SpanReferences::SetHigh(System.ReadOnlySpan`1&,System.UInt64) +arm64-advsimd System.Void Examples.SpanReferences::WriteFirst(System.Span`1&,System.Int32) +arm64-advsimd System.Void Examples.SpanReferences::WriteSecond(System.ReadOnlySpan`1&,System.UInt64) +arm64-advsimd System.Void Examples.SpanReferences::WriteWord(System.ReadOnlySpan`1&,System.UInt64) +arm64-advsimd System.Void Examples.Square::.ctor(System.UInt64,System.UInt64) +arm64-advsimd System.Void Examples.StaticSpans::Write() +arm64-advsimd System.Void Examples.StructMethods::ChangeCopy(Examples.Counter) +arm64-advsimd System.Void Examples.TagArrays::Put(System.String[],System.Int32,Examples.Tagged) +arm64-advsimd System.Void Examples.TagArrays::Stamp(System.String[],Examples.Tagged,System.UInt64) +arm64-advsimd System.Void Examples.TagArrays::Take(Examples.Tagged,System.String[],System.Int32) +arm64-advsimd System.Void Examples.Tagged::.ctor() +arm64-advsimd System.Void Examples.TailCalls::ForwardVoid(System.Int32&) +arm64-advsimd System.Void Examples.TailCalls::Store(System.Int32&) +arm64-advsimd System.Void Examples.Tally::.ctor(System.UInt32) +arm64-advsimd System.Void Examples.Throws::ThrowDivideByZero() +arm64-advsimd System.Void Examples.Throws::ThrowNull() +arm64-advsimd System.Void Examples.TileArrays::Relane(Examples.Tiles,System.Runtime.Intrinsics.Vector128`1[]) +arm64-advsimd System.Void Examples.Tiles::.ctor() +arm64-advsimd System.Void Examples.TypedCopy::Packet(Examples.Packet&,Examples.Packet&) +arm64-advsimd System.Void Examples.TypedCopy::Record(Examples.Record&,Examples.Record&) +arm64-advsimd System.Void Examples.TypedCopy::Vector128(System.Runtime.Intrinsics.Vector128`1&,System.Runtime.Intrinsics.Vector128`1&) +arm64-advsimd System.Void Examples.TypedCopy::Vector256(System.Runtime.Intrinsics.Vector256`1&,System.Runtime.Intrinsics.Vector256`1&) +arm64-advsimd System.Void Examples.UnalignedMemory::Write128(System.Byte&,System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Void Examples.UnalignedMemory::Write512(System.Byte&,System.Runtime.Intrinsics.Vector512`1) +arm64-advsimd System.Void Examples.UnalignedMemory::WriteAggregate(System.Byte&,Examples.FourLimbs) +arm64-advsimd System.Void Examples.UnalignedMemory::WriteAligned128(System.Byte*,System.Runtime.Intrinsics.Vector128`1) +arm64-advsimd System.Void Examples.UnalignedMemory::WriteAlignedAggregate(System.Byte*,Examples.FourLimbs) +arm64-advsimd System.Void Examples.UnalignedMemory::WriteAlignedWord(System.Byte*,System.UInt64) +arm64-advsimd System.Void Examples.UnalignedMemory::WriteHalf(System.Byte&,System.Int16) +arm64-advsimd System.Void Examples.UnalignedMemory::WritePointerAggregate(System.Byte*,Examples.FourLimbs) +arm64-advsimd System.Void Examples.UnalignedMemory::WritePointerWord(System.Byte*,System.UInt64) +arm64-advsimd System.Void Examples.UnalignedMemory::WriteSigned(System.Byte&,System.SByte) +arm64-advsimd System.Void Examples.UnalignedMemory::WriteWord(System.Byte&,System.UInt64) +arm64-advsimd System.Void Examples.WordReader::.ctor() +arm64-advsimd System.Void Examples.WordReader::Advance() +arm64-advsimd System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Examples.InitCounter::set_Value(System.UInt64) +arm64-advsimd method System.Int32 *(System.Int32)[] Examples.ManagedDispatch::NullTable() +scalar Examples.Account Examples.Objects::FirstPresent(Examples.Account,Examples.Account) +scalar Examples.AggregateArrays/Pair Examples.AggregateArrays::Read(Examples.AggregateArrays/Pair[],System.Int32) +scalar Examples.AggregateArrays/Pair& Examples.AggregateArrays::Address(Examples.AggregateArrays/Pair[],System.Int32) +scalar Examples.AggregateArrays/Pair& Examples.AggregateArrays::Data(Examples.AggregateArrays/Pair[]) +scalar Examples.AggregateArrays/Pair[] Examples.TileArrays::PairsOf(Examples.Tiles) +scalar Examples.AggregateSpans/Words Examples.AggregateSpans::Read(System.ReadOnlySpan`1) +scalar Examples.AllocationTag[] Examples.PrimitiveArrays::AllocateUnnamedZero(System.Int32) +scalar Examples.CheckedConstructor Examples.Constructors::MakeChecked(System.UInt64) +scalar Examples.Counter Examples.Constructors::Return(System.UInt64) +scalar Examples.Counter Examples.Constructors::WrongReturn(System.UInt64) +scalar Examples.DefaultConstructor Examples.Constructors::MakeDefault() +scalar Examples.Dog Examples.ClassTests::AsDog(Examples.Animal) +scalar Examples.EnumBuffers/Large Examples.EnumBuffers::Wide(Examples.EnumBuffers/Large[],System.Int32) +scalar Examples.EnumBuffers/Small Examples.EnumBuffers::Address(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +scalar Examples.EnumBuffers/Small Examples.EnumBuffers::FillClear(Examples.EnumBuffers/Small[]) +scalar Examples.EnumBuffers/Small Examples.EnumBuffers::Loop(Examples.EnumBuffers/Small[],System.Int32) +scalar Examples.EnumBuffers/Small Examples.EnumBuffers::Read(System.ReadOnlySpan`1) +scalar Examples.EnumBuffers/Small Examples.EnumBuffers::Roundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +scalar Examples.EnumBuffers/Small Examples.EnumBuffers::SpanRoundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +scalar Examples.FourLimbs Examples.MarshaledMemory::ReadAggregate(System.ReadOnlySpan`1) +scalar Examples.FourLimbs Examples.UnalignedMemory::ReadAggregate(System.Byte&) +scalar Examples.FourLimbs Examples.UnalignedMemory::ReadAlignedAggregate(System.Byte*) +scalar Examples.FourLimbs Examples.UnalignedMemory::ReadPointerAggregate(System.Byte*) +scalar Examples.FourLimbs& Examples.UnsafeReferences::Mutable(Examples.FourLimbs&) +scalar Examples.FourLimbs& Examples.UnsafeReferences::SubtractAggregate(Examples.FourLimbs&,System.IntPtr) +scalar Examples.GenericPair`1> Examples.GenericLayouts::Nested(System.UInt32,System.UInt32) +scalar Examples.GenericPair`1 Examples.AggregateArrays::ReadGeneric(Examples.GenericPair`1[],System.Int32) +scalar Examples.GenericPair`1 Examples.GenericLayouts::Create(System.UInt64,System.UInt64) +scalar Examples.IndirectValues/Box`1 Examples.IndirectValues::Generic(Examples.IndirectValues/Box`1) +scalar Examples.IndirectValues/Box`1 Examples.IndirectValues::Identity>(Examples.IndirectValues/Box`1) +scalar Examples.IndirectValues/Pair Examples.IndirectValues::Identity(Examples.IndirectValues/Pair) +scalar Examples.IndirectValues/Pair Examples.IndirectValues::Read(Examples.IndirectValues/Pair&) +scalar Examples.IndirectValues/Pair Examples.IndirectValues::Record(Examples.IndirectValues/Pair) +scalar Examples.IndirectValues/Pair Examples.IndirectValues::Reference(Examples.IndirectValues/Pair&) +scalar Examples.ItemPool Examples.PooledItem::get_Owner() +scalar Examples.Leaf Examples.Nested::Extract(Examples.NestedRecord) +scalar Examples.Leaf Examples.Nested::WrongExtract(Examples.NestedRecord) +scalar Examples.NestedConstructor Examples.Constructors::MakeNested(System.UInt64) +scalar Examples.NestedRecord& Examples.UnsafeLayouts::Add15int(Examples.NestedRecord&,System.Int32) +scalar Examples.NestedRecord& Examples.UnsafeLayouts::Add15nint(Examples.NestedRecord&,System.IntPtr) +scalar Examples.NestedRecord& Examples.UnsafeLayouts::Add15nuint(Examples.NestedRecord&,System.UIntPtr) +scalar Examples.NestedRecord[] Examples.PrimitiveArrays::AllocateNested(System.Int32) +scalar Examples.Node Examples.NodeArrays::At(Examples.Node[],System.Int32) +scalar Examples.Node Examples.NodeArrays::First(Examples.Bucket) +scalar Examples.Node Examples.NodeArrays::Install(Examples.Node[],System.Int32,Examples.Node,System.UInt64) +scalar Examples.Node Examples.NodeArrays::Pair(Examples.Node,Examples.Node) +scalar Examples.Node Examples.NodeArrays::Shift(Examples.Node[],Examples.Node) +scalar Examples.Node Examples.Objects::Detach(Examples.Node) +scalar Examples.Node[] Examples.NodeArrays::Create(System.Int32) +scalar Examples.PackedConstructor Examples.Constructors::MakePacked(System.UInt64) +scalar Examples.PackedLeaf& Examples.GenericMethods::Next(Examples.PackedLeaf&,System.Int32) +scalar Examples.PackedLeaf& Examples.GenericMethods::NextPacked(Examples.PackedLeaf&,System.Int32) +scalar Examples.PackedLeaf& Examples.UnsafeLayouts::Add14int(Examples.PackedLeaf&,System.Int32) +scalar Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nint(Examples.PackedLeaf&,System.IntPtr) +scalar Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nuint(Examples.PackedLeaf&,System.UIntPtr) +scalar Examples.PackedLeaf[] Examples.PrimitiveArrays::AllocatePacked(System.Int32) +scalar Examples.Packet& Examples.UnsafeLayouts::Add16int(Examples.Packet&,System.Int32) +scalar Examples.Packet& Examples.UnsafeLayouts::Add16nint(Examples.Packet&,System.IntPtr) +scalar Examples.Packet& Examples.UnsafeLayouts::Add16nuint(Examples.Packet&,System.UIntPtr) +scalar Examples.PooledItem Examples.ItemPool::Rent() +scalar Examples.Signed16 Examples.Enums::IdentityS16(Examples.Signed16) +scalar Examples.Signed16 Examples.Enums::ThroughCall(Examples.Signed16) +scalar Examples.Signed16 Examples.PointerOperations::ReadEnum(Examples.Signed16*,System.Int32) +scalar Examples.Signed16[] Examples.PrimitiveArrays::AllocateSigned16(System.Int32) +scalar Examples.Signed32 Examples.Enums::IdentityS32(Examples.Signed32) +scalar Examples.Signed32[] Examples.PrimitiveArrays::AllocateSigned32(System.Int32) +scalar Examples.Signed64 Examples.Enums::IdentityS64(Examples.Signed64) +scalar Examples.Signed64[] Examples.PrimitiveArrays::AllocateSigned64(System.Int32) +scalar Examples.Signed8 Examples.Enums::Field(Examples.Signed8) +scalar Examples.Signed8 Examples.Enums::IdentityS8(Examples.Signed8) +scalar Examples.Signed8 Examples.Enums::Read(Examples.EnumRecord&) +scalar Examples.Signed8 Examples.GenericMethods::EnumIdentity(Examples.Signed8) +scalar Examples.Signed8 Examples.GenericMethods::Identity(Examples.Signed8) +scalar Examples.Signed8& Examples.UnsafeLayouts::Add12int(Examples.Signed8&,System.Int32) +scalar Examples.Signed8& Examples.UnsafeLayouts::Add12nint(Examples.Signed8&,System.IntPtr) +scalar Examples.Signed8& Examples.UnsafeLayouts::Add12nuint(Examples.Signed8&,System.UIntPtr) +scalar Examples.Signed8[] Examples.PrimitiveArrays::AllocateSigned8(System.Int32) +scalar Examples.Tiles Examples.TileArrays::Hold(Examples.AggregateArrays/Pair[],System.Runtime.Intrinsics.Vector128`1[]) +scalar Examples.Unsigned16 Examples.Enums::IdentityU16(Examples.Unsigned16) +scalar Examples.Unsigned16[] Examples.PrimitiveArrays::AllocateUnsigned16(System.Int32) +scalar Examples.Unsigned32 Examples.Enums::IdentityU32(Examples.Unsigned32) +scalar Examples.Unsigned32[] Examples.PrimitiveArrays::AllocateUnsigned32(System.Int32) +scalar Examples.Unsigned64 Examples.Enums::IdentityU64(Examples.Unsigned64) +scalar Examples.Unsigned64 Examples.Enums::Local(Examples.Unsigned64) +scalar Examples.Unsigned64 Examples.Enums::SetFlag(Examples.Unsigned64) +scalar Examples.Unsigned64 Examples.Enums::WrongSetFlag(Examples.Unsigned64) +scalar Examples.Unsigned64& Examples.UnsafeLayouts::Add13int(Examples.Unsigned64&,System.Int32) +scalar Examples.Unsigned64& Examples.UnsafeLayouts::Add13nint(Examples.Unsigned64&,System.IntPtr) +scalar Examples.Unsigned64& Examples.UnsafeLayouts::Add13nuint(Examples.Unsigned64&,System.UIntPtr) +scalar Examples.Unsigned64[] Examples.PrimitiveArrays::AllocateUnsigned64(System.Int32) +scalar Examples.Unsigned8 Examples.Enums::IdentityU8(Examples.Unsigned8) +scalar Examples.Unsigned8[] Examples.PrimitiveArrays::AllocateUnsigned8(System.Int32) +scalar Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int32(System.Runtime.Intrinsics.Vector256`1) +scalar Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int64(System.Runtime.Intrinsics.Vector256`1) +scalar Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt32(System.Runtime.Intrinsics.Vector256`1) +scalar Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt64(System.Runtime.Intrinsics.Vector256`1) +scalar Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ParameterCopy(Examples.ValueBitCasts/Limbs) +scalar Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ReadAggregate(Examples.ValueBitCasts/Limbs&) +scalar Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::RoundTrip(Examples.ValueBitCasts/Limbs) +scalar Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128Byte(System.Runtime.Intrinsics.Vector128`1) +scalar Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt32(System.Runtime.Intrinsics.Vector128`1) +scalar Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt64(System.Runtime.Intrinsics.Vector128`1) +scalar System.Boolean Examples.AggregateSpans::Copy(System.ReadOnlySpan`1,System.Span`1) +scalar System.Boolean Examples.AggregateSpans::IsEmpty(System.Span`1) +scalar System.Boolean Examples.AggregateSpans::VectorIsEmpty(System.ReadOnlySpan`1>) +scalar System.Boolean Examples.Allowance::Spend(Examples.Allowance&,System.UInt64) +scalar System.Boolean Examples.Bits::PowerI32(System.Int32) +scalar System.Boolean Examples.Bits::PowerI64(System.Int64) +scalar System.Boolean Examples.Bits::PowerNative(System.IntPtr) +scalar System.Boolean Examples.Bits::PowerU32(System.UInt32) +scalar System.Boolean Examples.Bits::PowerU64(System.UInt64) +scalar System.Boolean Examples.Bits::PowerUNative(System.UIntPtr) +scalar System.Boolean Examples.Bits::PrimitivePower32(System.Int32) +scalar System.Boolean Examples.Bits::PrimitivePower64(System.Int64) +scalar System.Boolean Examples.Bits::PrimitivePowerNative(System.IntPtr) +scalar System.Boolean Examples.Bits::PrimitivePowerU32(System.UInt32) +scalar System.Boolean Examples.Bits::PrimitivePowerU64(System.UInt64) +scalar System.Boolean Examples.Bits::PrimitivePowerUNative(System.UIntPtr) +scalar System.Boolean Examples.ClassTests::IsDog(Examples.Animal) +scalar System.Boolean Examples.ComputedFlags::get_Both() +scalar System.Boolean Examples.ComputedFlags::get_Middle() +scalar System.Boolean Examples.ComputedFlags::get_NotOff() +scalar System.Boolean Examples.ComputedFlags::get_Outer() +scalar System.Boolean Examples.DivisionIntrinsics::EqualBytes256(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1) +scalar System.Boolean Examples.DivisionIntrinsics::EqualSigned512(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1) +scalar System.Boolean Examples.DivisionIntrinsics::EqualWords128(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.Runtime.Intrinsics.Vector128`1) +scalar System.Boolean Examples.DivisionIntrinsics::UnsafeEquals(System.UInt64) +scalar System.Boolean Examples.DoublingAllowance::Examples.IAllowance.Spend(Examples.DoublingAllowance&) +scalar System.Boolean Examples.DoublingAllowance::Spend(Examples.DoublingAllowance&,System.UInt64) +scalar System.Boolean Examples.Forced::get_IsActive() +scalar System.Boolean Examples.Gate`1::get_Open() +scalar System.Boolean Examples.Gate`1::get_Open() +scalar System.Boolean Examples.Hush`1::get_Silent() +scalar System.Boolean Examples.IAllowance`1::Spend(Examples.Allowance&) +scalar System.Boolean Examples.ILoud::Examples.IQuietByDefault.get_IsActive() +scalar System.Boolean Examples.ILoudByDefault::get_IsActive() +scalar System.Boolean Examples.IQuiet::Examples.ILoudByDefault.get_IsActive() +scalar System.Boolean Examples.IQuietByDefault::get_IsActive() +scalar System.Boolean Examples.IRefundingAllowance`1::Examples.IAllowance.Spend(Examples.RefundingAllowance&) +scalar System.Boolean Examples.IntegerOperations::Greater32(System.Int32,System.Int32) +scalar System.Boolean Examples.IntegerOperations::Greater64(System.Int64,System.Int64) +scalar System.Boolean Examples.ItemPool::IsIdle(Examples.PooledItem) +scalar System.Boolean Examples.Loops::AllZero(System.ReadOnlySpan`1) +scalar System.Boolean Examples.MarshaledMemory::TryRead(System.ReadOnlySpan`1,System.UInt64&) +scalar System.Boolean Examples.MarshaledMemory::TryReadAggregate(System.ReadOnlySpan`1,Examples.FourLimbs&) +scalar System.Boolean Examples.MarshaledMemory::TryWrite(System.Span`1,System.UInt64&) +scalar System.Boolean Examples.MemorySpans::BooleanWrite(System.Span`1,System.Int32,System.Boolean) +scalar System.Boolean Examples.NullReferences::IsLive(System.UInt64&) +scalar System.Boolean Examples.NullReferences::IsNull() +scalar System.Boolean Examples.Objects::HasTag(Examples.Tagged) +scalar System.Boolean Examples.Objects::IsNull(Examples.Account) +scalar System.Boolean Examples.Objects::IsPresent(Examples.Account) +scalar System.Boolean Examples.Objects::Same(Examples.Account,Examples.Account) +scalar System.Boolean Examples.Objects::SameTag(Examples.Tagged,Examples.Tagged) +scalar System.Boolean Examples.Objects::TwoMarkers() +scalar System.Boolean Examples.PointerOperations::Greater(System.Byte*,System.Byte*) +scalar System.Boolean Examples.PointerOperations::GreaterEqual(System.Byte*,System.Byte*) +scalar System.Boolean Examples.PointerOperations::Less(System.Byte*,System.Byte*) +scalar System.Boolean Examples.PointerOperations::LessEqual(System.Byte*,System.Byte*) +scalar System.Boolean Examples.PooledItem::get_IsRented() +scalar System.Boolean Examples.PrimitiveArrays::ReadBoolean(System.Boolean[],System.Int32) +scalar System.Boolean Examples.RefundingAllowance::Spend(Examples.RefundingAllowance&,System.UInt64) +scalar System.Boolean Examples.SpanCopies::TryCopy(System.Span`1,System.Span`1) +scalar System.Boolean Examples.SpanCopies::TryCopyEnum(System.ReadOnlySpan`1,System.Span`1) +scalar System.Boolean Examples.SpanCopies::TryCopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +scalar System.Boolean Examples.StepOff::get_IsActive() +scalar System.Boolean Examples.StepOn::get_IsActive() +scalar System.Boolean Examples.Strings::SameLabel(System.String,System.String) +scalar System.Boolean Examples.Strings::SameText() +scalar System.Boolean Examples.UnsafeReferences::AreSame(System.Byte&,System.Byte&) +scalar System.Boolean Examples.UnsafeReferences::Greater(System.Byte&,System.Byte&) +scalar System.Boolean Examples.UnsafeReferences::Less(System.Byte&,System.Byte&) +scalar System.Boolean Examples.ViewAccessors::Empty(System.Span`1) +scalar System.Boolean Examples.ViewAccessors::PinnableNull(System.Span`1) +scalar System.Boolean Examples.ViewAccessors::ReadOnlyEmpty(System.ReadOnlySpan`1) +scalar System.Boolean Examples.ViewAccessors::ReadOnlyPinnableNull(System.ReadOnlySpan`1) +scalar System.Boolean Examples.ViewAccessors::ReadOnlyReferenceNull(System.ReadOnlySpan`1) +scalar System.Boolean Examples.ViewAccessors::ReferenceNull(System.Span`1) +scalar System.Boolean Examples.WordFlag`1::get_IsActive() +scalar System.Boolean Examples.WordFlag`1::get_IsActive() +scalar System.Boolean& Examples.UnsafeLayouts::Add0int(System.Boolean&,System.Int32) +scalar System.Boolean& Examples.UnsafeLayouts::Add0nint(System.Boolean&,System.IntPtr) +scalar System.Boolean& Examples.UnsafeLayouts::Add0nuint(System.Boolean&,System.UIntPtr) +scalar System.Byte Examples.Arguments::Narrow(System.Byte) +scalar System.Byte Examples.Bits::ReverseU8(System.Byte) +scalar System.Byte Examples.Counter::Parameter(System.Byte) +scalar System.Byte Examples.GenericMethods::Identity(System.Byte) +scalar System.Byte Examples.IntegerMath::ClampUInt8(System.Byte,System.Byte,System.Byte) +scalar System.Byte Examples.IntegerMath::MaxUInt8(System.Byte,System.Byte) +scalar System.Byte Examples.IntegerMath::MinUInt8(System.Byte,System.Byte) +scalar System.Byte Examples.PrimitiveArrays::ReadByte(System.Byte[],System.Int32) +scalar System.Byte Examples.SpanReferences::At(System.ReadOnlySpan`1&,System.Int32) +scalar System.Byte Examples.StaticSpans::First() +scalar System.Byte Examples.StaticSpans::Last() +scalar System.Byte Examples.StaticSpans::PastEnd() +scalar System.Byte Examples.StructMethods::Narrow(System.Byte) +scalar System.Byte& Examples.UnsafeLayouts::Add3int(System.Byte&,System.Int32) +scalar System.Byte& Examples.UnsafeLayouts::Add3nint(System.Byte&,System.IntPtr) +scalar System.Byte& Examples.UnsafeLayouts::Add3nuint(System.Byte&,System.UIntPtr) +scalar System.Byte& Examples.UnsafeReferences::AddSigned(System.Byte&,System.IntPtr) +scalar System.Byte& Examples.UnsafeReferences::AddUnsigned(System.Byte&,System.UIntPtr) +scalar System.Byte& Examples.UnsafeReferences::Subtract(System.Byte&,System.IntPtr) +scalar System.Byte& Examples.UnsafeReferences::SubtractUnsigned(System.Byte&,System.UIntPtr) +scalar System.Byte* Examples.PointerOperations::Subtract(System.Byte*,System.Int32) +scalar System.Byte* Examples.PointerOperations::SubtractNative(System.Byte*,System.IntPtr) +scalar System.Byte* Examples.UnsafeReferences::AddPointer(System.Byte*,System.Int32) +scalar System.Byte* Examples.UnsafeReferences::AddPointerAggregate(System.Byte*,System.Int32) +scalar System.Byte* Examples.UnsafeReferences::SubtractPointer(System.Byte*,System.Int32) +scalar System.Byte* Examples.UnsafeReferences::SubtractPointerAggregate(System.Byte*,System.Int32) +scalar System.Byte[] Examples.PrimitiveArrays::AllocateBytes(System.Int32) +scalar System.Char Examples.MemorySpans::CharacterWrite(System.Span`1,System.Int32,System.Char) +scalar System.Char Examples.NarrowIntegers::Character(System.Int32) +scalar System.Char Examples.PointerOperations::ReadCharacter(System.Char*,System.Int32) +scalar System.Char Examples.PrimitiveArrays::ReadChar(System.Char[],System.Int32) +scalar System.Char& Examples.UnsafeLayouts::Add1int(System.Char&,System.Int32) +scalar System.Char& Examples.UnsafeLayouts::Add1nint(System.Char&,System.IntPtr) +scalar System.Char& Examples.UnsafeLayouts::Add1nuint(System.Char&,System.UIntPtr) +scalar System.Int16 Examples.Bits::ReverseI16(System.Int16) +scalar System.Int16 Examples.IntegerMath::AbsInt16(System.Int16) +scalar System.Int16 Examples.IntegerMath::ClampInt16(System.Int16,System.Int16,System.Int16) +scalar System.Int16 Examples.IntegerMath::MaxInt16(System.Int16,System.Int16) +scalar System.Int16 Examples.IntegerMath::MinInt16(System.Int16,System.Int16) +scalar System.Int16 Examples.NarrowIntegers::Identity(System.Int16) +scalar System.Int16 Examples.PrimitiveArrays::ReadInt16(System.Int16[],System.Int32) +scalar System.Int16 Examples.UnalignedMemory::ReadHalf(System.Byte&) +scalar System.Int16& Examples.UnsafeLayouts::Add4int(System.Int16&,System.Int32) +scalar System.Int16& Examples.UnsafeLayouts::Add4nint(System.Int16&,System.IntPtr) +scalar System.Int16& Examples.UnsafeLayouts::Add4nuint(System.Int16&,System.UIntPtr) +scalar System.Int16[] Examples.PrimitiveArrays::AllocateShorts(System.Int32) +scalar System.Int32 Examples.AggregateArrays::ConstructedLength(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +scalar System.Int32 Examples.Arithmetic::AtLeastFive(System.Int32) +scalar System.Int32 Examples.Arithmetic::FiveToOne(System.Int32) +scalar System.Int32 Examples.Arithmetic::Larger(System.Int32,System.Int32) +scalar System.Int32 Examples.Arithmetic::WordAdjusted(System.Int32) +scalar System.Int32 Examples.Arithmetic::WordSized(System.Int32) +scalar System.Int32 Examples.ArrayElementReferences::Read(System.Int32[],System.Int32) +scalar System.Int32 Examples.ArrayElementReferences::Write(System.Int32[],System.Int32,System.Int32) +scalar System.Int32 Examples.BinarySpans::TryWriteBig(System.Span`1) +scalar System.Int32 Examples.BinarySpans::TryWriteLittle(System.Span`1) +scalar System.Int32 Examples.Bits::Leading32(System.UInt32) +scalar System.Int32 Examples.Bits::Leading64(System.UInt64) +scalar System.Int32 Examples.Bits::LeadingNative(System.UIntPtr) +scalar System.Int32 Examples.Bits::Log32(System.UInt32) +scalar System.Int32 Examples.Bits::Log64(System.UInt64) +scalar System.Int32 Examples.Bits::LogNative(System.UIntPtr) +scalar System.Int32 Examples.Bits::Population32(System.UInt32) +scalar System.Int32 Examples.Bits::Population64(System.UInt64) +scalar System.Int32 Examples.Bits::PopulationNative(System.UIntPtr) +scalar System.Int32 Examples.Bits::PrimitiveLeading32(System.Int32) +scalar System.Int32 Examples.Bits::PrimitiveLeft32(System.Int32,System.Int32) +scalar System.Int32 Examples.Bits::PrimitiveNarrow16(System.UInt32) +scalar System.Int32 Examples.Bits::PrimitiveNarrow8(System.UInt32) +scalar System.Int32 Examples.Bits::PrimitiveNarrowRotate16(System.UInt32,System.Int32) +scalar System.Int32 Examples.Bits::PrimitiveNarrowRotate8(System.UInt32,System.Int32) +scalar System.Int32 Examples.Bits::PrimitiveNarrowSigned(System.UInt32) +scalar System.Int32 Examples.Bits::PrimitivePopulation32(System.Int32) +scalar System.Int32 Examples.Bits::PrimitiveRight32(System.Int32,System.Int32) +scalar System.Int32 Examples.Bits::PrimitiveTrailing32(System.Int32) +scalar System.Int32 Examples.Bits::ReverseI32(System.Int32) +scalar System.Int32 Examples.Bits::Trailing32(System.UInt32) +scalar System.Int32 Examples.Bits::Trailing64(System.UInt64) +scalar System.Int32 Examples.Bits::TrailingNative(System.UIntPtr) +scalar System.Int32 Examples.Bits::WrongLog64(System.UInt64) +scalar System.Int32 Examples.Capabilities::ArmCount(System.UInt64) +scalar System.Int32 Examples.Capabilities::Endian() +scalar System.Int32 Examples.Capabilities::Platform() +scalar System.Int32 Examples.Capabilities::ReferenceFree() +scalar System.Int32 Examples.Capabilities::VbmiCount(System.UInt64) +scalar System.Int32 Examples.CheckedArithmetic::AddSigned(System.Int32,System.Int32) +scalar System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +scalar System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +scalar System.Int32 Examples.ConstrainedCalls::ClosedBoth(Examples.ClosedInterfaceCounter&) +scalar System.Int32 Examples.ConstrainedCalls::ClosedGeneric(Examples.GenericInterfaceCounter`1&) +scalar System.Int32 Examples.ConstrainedCalls::DirectGeneric(Examples.GenericCounterValue`1&) +scalar System.Int32 Examples.ConstrainedCalls::Explicit(Examples.ExplicitCounterValue&) +scalar System.Int32 Examples.ConstrainedCalls::Generic(Examples.GenericCounterValue`1&) +scalar System.Int32 Examples.ConstrainedCalls::GenericExplicit(Examples.ExplicitGenericCounterValue`1&) +scalar System.Int32 Examples.ConstrainedCalls::Implicit(Examples.CounterValue&) +scalar System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.CounterValue&) +scalar System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.ExplicitCounterValue&) +scalar System.Int32 Examples.ConstrainedCalls::IncrementAndRead>(Examples.ExplicitGenericCounterValue`1&) +scalar System.Int32 Examples.ConstrainedCalls::Mutating(Examples.CounterValue&) +scalar System.Int32 Examples.ConstrainedCalls::MutatingExplicit(Examples.ExplicitCounterValue&) +scalar System.Int32 Examples.ConstrainedCalls::Read(Examples.CounterValue&) +scalar System.Int32 Examples.ConstrainedCalls::Read(Examples.ExplicitCounterValue&) +scalar System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +scalar System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +scalar System.Int32 Examples.ConstrainedCalls::ReadClosedInt(Examples.ClosedInterfaceCounter&) +scalar System.Int32 Examples.ConstrainedCalls::ReadClosedInt>(Examples.GenericInterfaceCounter`1&) +scalar System.Int32 Examples.ConstrainedCalls::ReadClosedLong(Examples.ClosedInterfaceCounter&) +scalar System.Int32 Examples.ConstrainedCalls::TwoGenericOwners(Examples.GenericCounterValue`1&,Examples.GenericCounterValue`1&) +scalar System.Int32 Examples.CounterValue::Read() +scalar System.Int32 Examples.Enums::Branch(Examples.Signed8) +scalar System.Int32 Examples.ExceptionDocumentation::Absolute(System.Int32) +scalar System.Int32 Examples.ExceptionDocumentation::CheckedNarrow(System.Int64) +scalar System.Int32 Examples.ExceptionDocumentation::Clamp(System.Int32,System.Int32,System.Int32) +scalar System.Int32 Examples.ExplicitCounterValue::Examples.ICounterValue.Read() +scalar System.Int32 Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Read() +scalar System.Int32 Examples.Gauge::BelowFive() +scalar System.Int32 Examples.Gauge::Blend(System.Int32,System.Int32) +scalar System.Int32 Examples.Gauge::Blended(System.Int32,System.Int32) +scalar System.Int32 Examples.Gauge::Bounded(System.Int32,System.Int32) +scalar System.Int32 Examples.Gauge::Capped(Examples.Gauge,System.Int32) +scalar System.Int32 Examples.Gauge::CappedLevel() +scalar System.Int32 Examples.Gauge::CappedSlack() +scalar System.Int32 Examples.Gauge::Checked(Examples.Gauge,System.Int32) +scalar System.Int32 Examples.Gauge::CheckedLevel() +scalar System.Int32 Examples.Gauge::Clamp(System.Int32,System.Int32,System.Int32) +scalar System.Int32 Examples.Gauge::Clamped(System.Int32,System.Int32,System.Int32) +scalar System.Int32 Examples.Gauge::Current() +scalar System.Int32 Examples.Gauge::Doubled(System.Int32) +scalar System.Int32 Examples.Gauge::Floor(System.Int32) +scalar System.Int32 Examples.Gauge::Floored(System.Int32) +scalar System.Int32 Examples.Gauge::Gap(System.Int32,System.Int32) +scalar System.Int32 Examples.Gauge::IsFive() +scalar System.Int32 Examples.Gauge::Margin() +scalar System.Int32 Examples.Gauge::Raise(Examples.Gauge,System.Int32,System.Int32) +scalar System.Int32 Examples.Gauge::RaisedLevel() +scalar System.Int32 Examples.Gauge::Saturate(System.Int32,System.Int32) +scalar System.Int32 Examples.Gauge::Slack() +scalar System.Int32 Examples.Gauge::Smaller(System.Int32,System.Int32) +scalar System.Int32 Examples.Gauge::Total(System.Int32[]) +scalar System.Int32 Examples.Gauge::Twice(System.Int32) +scalar System.Int32 Examples.Gauge::Within(System.Int32,System.Int32) +scalar System.Int32 Examples.GenericCounterValue`1::Echo(System.Int32) +scalar System.Int32 Examples.GenericCounterValue`1::Read() +scalar System.Int32 Examples.GenericCounterValue`1::Read() +scalar System.Int32 Examples.GenericInterfaceCounter`1::Read() +scalar System.Int32 Examples.GenericLayouts::TupleSize() +scalar System.Int32 Examples.GenericLayouts::VectorSize() +scalar System.Int32 Examples.GenericMethods::Identity32(System.Int32) +scalar System.Int32 Examples.GenericMethods::Identity(System.Int32) +scalar System.Int32 Examples.GenericMethods::Mixed(System.Int32,System.Byte) +scalar System.Int32 Examples.GenericMethods::PackedSize() +scalar System.Int32 Examples.GenericMethods::Read32(System.Int32&) +scalar System.Int32 Examples.GenericMethods::Read(System.Int32&) +scalar System.Int32 Examples.GenericMethods::Size() +scalar System.Int32 Examples.GenericMethods::Size>() +scalar System.Int32 Examples.GenericMethods::VectorSize() +scalar System.Int32 Examples.GenericMethods::WrongIdentity(System.Int32) +scalar System.Int32 Examples.GenericOwners::SpanCount(System.ReadOnlySpan`1) +scalar System.Int32 Examples.Guards::Count(System.Int32) +scalar System.Int32 Examples.IntegerMath::AbsInt32(System.Int32) +scalar System.Int32 Examples.IntegerMath::ClampInt32(System.Int32,System.Int32,System.Int32) +scalar System.Int32 Examples.IntegerMath::MaxInt32(System.Int32,System.Int32) +scalar System.Int32 Examples.IntegerMath::MinInt32(System.Int32,System.Int32) +scalar System.Int32 Examples.IntegerMath::SignInt16(System.Int16) +scalar System.Int32 Examples.IntegerMath::SignInt32(System.Int32) +scalar System.Int32 Examples.IntegerMath::SignInt64(System.Int64) +scalar System.Int32 Examples.IntegerMath::SignInt8(System.SByte) +scalar System.Int32 Examples.IntegerMath::SignNative(System.IntPtr) +scalar System.Int32 Examples.IntegerOperations::Negate32(System.Int32) +scalar System.Int32 Examples.ItemPool::Trim(System.Int32) +scalar System.Int32 Examples.ItemPool::get_Available() +scalar System.Int32 Examples.ItemPool::get_Capacity() +scalar System.Int32 Examples.ItemPool::get_Rented() +scalar System.Int32 Examples.Loops::CountToFive(System.Int32) +scalar System.Int32 Examples.Loops::Drain(System.ReadOnlySpan`1) +scalar System.Int32 Examples.ManagedDispatch::LocalEscape(System.Int32) +scalar System.Int32 Examples.ManagedDispatch::NullLength() +scalar System.Int32 Examples.ManagedDispatch::Read(System.Int32&) +scalar System.Int32 Examples.MemorySpans::ArraySliceWrite(System.Int32[],System.Int32,System.Int32,System.Int32) +scalar System.Int32 Examples.MemorySpans::Existing(System.Int32&,System.Int32,System.Int32) +scalar System.Int32 Examples.MemorySpans::InPlaceArrayConstructor(System.Int32[],System.Int32,System.Int32) +scalar System.Int32 Examples.MemorySpans::InPlaceArrayLength(System.Int32[],System.Int32,System.Int32) +scalar System.Int32 Examples.MemorySpans::InPlacePointerSpan(System.Int32*,System.Int32) +scalar System.Int32 Examples.MemorySpans::Read(System.ReadOnlySpan`1,System.Int32) +scalar System.Int32 Examples.MemorySpans::ReferenceWrite(System.Span`1,System.Int32) +scalar System.Int32 Examples.MemorySpans::ReinitializeArraySpan(System.Int32[],System.Int32,System.Int32) +scalar System.Int32 Examples.MemorySpans::SliceRead(System.ReadOnlySpan`1,System.Int32,System.Int32,System.Int32) +scalar System.Int32 Examples.MemorySpans::SliceWrite(System.Span`1,System.Int32,System.Int32,System.Int32,System.Int32) +scalar System.Int32 Examples.NarrowIntegers::Call(System.Int32) +scalar System.Int32 Examples.NarrowIntegers::Fields(System.Int32) +scalar System.Int32 Examples.NarrowIntegers::SignedByte(System.Int32) +scalar System.Int32 Examples.NarrowIntegers::SignedShort(System.Int64) +scalar System.Int32 Examples.NarrowIntegers::UnsignedShort(System.Int32) +scalar System.Int32 Examples.NativeIntegers::Narrow(System.IntPtr) +scalar System.Int32 Examples.NativeIntegers::SignedSize() +scalar System.Int32 Examples.NativeIntegers::UnsignedSize() +scalar System.Int32 Examples.NodeArrays::Count(Examples.Node[]) +scalar System.Int32 Examples.PooledItem::get_Id() +scalar System.Int32 Examples.PrimitiveArrays::AllocateAndWrite(System.Int32,System.Int32,System.Int32) +scalar System.Int32 Examples.PrimitiveArrays::AllocatePackedAndWrite(System.Int32,System.Int32,System.Int32) +scalar System.Int32 Examples.PrimitiveArrays::Length(System.Int32[]) +scalar System.Int32 Examples.PrimitiveArrays::ReadAlias(System.Int32[],System.Int32,System.Int32) +scalar System.Int32 Examples.PrimitiveArrays::ReadGeneric(System.Int32[],System.Int32) +scalar System.Int32 Examples.PrimitiveArrays::ReadGenericHelper(System.Int32[],System.Int32) +scalar System.Int32 Examples.PrimitiveArrays::ReadHelper(System.Int32[],System.Int32) +scalar System.Int32 Examples.PrimitiveArrays::ReadInt32(System.Int32[],System.Int32) +scalar System.Int32 Examples.PrimitiveArrays::WriteGenericHelper(System.Int32[],System.Int32,System.Int32) +scalar System.Int32 Examples.PrimitiveOperations::Alias(System.Int16&,System.Int16&,System.Int32) +scalar System.Int32 Examples.PrimitiveOperations::Choose(System.Int32) +scalar System.Int32 Examples.PrimitiveOperations::PacketSize() +scalar System.Int32 Examples.PrimitiveOperations::ReadWrite(System.Int32&,System.Int32) +scalar System.Int32 Examples.SampleArrays::Count(Examples.Samples) +scalar System.Int32 Examples.SampleArrays::CountAfterAllocation(Examples.Node,System.Int32[]) +scalar System.Int32 Examples.SampleArrays::CountAfterDeposit(Examples.Account,System.Int32[]) +scalar System.Int32 Examples.SampleArrays::CountAfterRefill(Examples.Samples,Examples.Samples,System.Int32[]) +scalar System.Int32 Examples.SampleArrays::ReadAfterAllocation(Examples.Node,System.Int32[]) +scalar System.Int32 Examples.SampleArrays::ReadAfterEcho(Examples.Account,System.Int32[],System.Int32) +scalar System.Int32 Examples.SampleArrays::RecordIndex(Examples.Account,System.Int32) +scalar System.Int32 Examples.SampleArrays::RefillRead(Examples.Samples,System.Int32[],System.Int32) +scalar System.Int32 Examples.SampleArrays::Share(Examples.Samples,Examples.Samples) +scalar System.Int32 Examples.SampleArrays::Sum(Examples.Samples,System.Int32[]) +scalar System.Int32 Examples.Sequential::PackedSize() +scalar System.Int32 Examples.Sequential::RecordSize() +scalar System.Int32 Examples.SpanConstructors::AggregateLocal(Examples.AggregateSpans/Words&) +scalar System.Int32 Examples.SpanConstructors::BytePointerLocal(System.Byte*,System.Int32) +scalar System.Int32 Examples.SpanConstructors::Local(System.Int32&) +scalar System.Int32 Examples.SpanHelpers::Count(System.ReadOnlySpan`1) +scalar System.Int32 Examples.SpanReferences::Argument(System.ReadOnlySpan`1) +scalar System.Int32 Examples.SpanReferences::Caller(System.Byte[]) +scalar System.Int32 Examples.SpanReferences::Copy(System.ReadOnlySpan`1&) +scalar System.Int32 Examples.SpanReferences::First(System.Span`1&) +scalar System.Int32 Examples.SpanReferences::Forward(System.ReadOnlySpan`1&) +scalar System.Int32 Examples.SpanReferences::Front(System.Span`1&) +scalar System.Int32 Examples.SpanReferences::GenericFront(System.Int32[]) +scalar System.Int32 Examples.SpanReferences::Length(System.ReadOnlySpan`1&) +scalar System.Int32 Examples.SpanReferences::Sum(System.ReadOnlySpan`1&) +scalar System.Int32 Examples.SpanReferences::Tail(System.ReadOnlySpan`1&) +scalar System.Int32 Examples.SpanReferences::Temporary(System.ReadOnlySpan`1) +scalar System.Int32 Examples.SpanTables::Count() +scalar System.Int32 Examples.StackAllocation::FillAndRead(System.Int32,System.Int32) +scalar System.Int32 Examples.StackAllocation::Length(System.Int32) +scalar System.Int32 Examples.StackAllocation::UninitializedRead() +scalar System.Int32 Examples.StackAllocation::WrittenBeforeRead(System.Int32) +scalar System.Int32 Examples.StackAllocation::ZeroInitialized() +scalar System.Int32 Examples.TailCalls::Excluded(System.Int32) +scalar System.Int32 Examples.TailCalls::Forward(System.Int32) +scalar System.Int32 Examples.TailCalls::ForwardReference(System.Int32&) +scalar System.Int32 Examples.TailCalls::ForwardThrow(System.Int32) +scalar System.Int32 Examples.TailCalls::Ignore(System.Int32&) +scalar System.Int32 Examples.TailCalls::Increment(System.Int32) +scalar System.Int32 Examples.TailCalls::LocalReference(System.Int32) +scalar System.Int32 Examples.TailCalls::Raise(System.Int32) +scalar System.Int32 Examples.TailCalls::Read(System.Int32&) +scalar System.Int32 Examples.TileArrays::PairCount(Examples.Tiles) +scalar System.Int32 Examples.TupleDivision::MathOutInt32(System.Int32,System.Int32,System.Int32&) +scalar System.Int32 Examples.TupleDivision::MathQuotient(System.Int32,System.Int32) +scalar System.Int32 Examples.TupleDivision::MathWrongQuotient(System.Int32,System.Int32) +scalar System.Int32 Examples.TypeTests::Classify(System.Int32) +scalar System.Int32 Examples.TypeTests::Classify(System.Int32) +scalar System.Int32 Examples.TypeTests::Marker(System.Int32) +scalar System.Int32 Examples.TypeTests::Other(System.Int32) +scalar System.Int32 Examples.TypeTests::ReferenceCases(System.Int32) +scalar System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +scalar System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +scalar System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +scalar System.Int32 Examples.UnsafeLayouts::Size0() +scalar System.Int32 Examples.UnsafeLayouts::Size1() +scalar System.Int32 Examples.UnsafeLayouts::Size10() +scalar System.Int32 Examples.UnsafeLayouts::Size11() +scalar System.Int32 Examples.UnsafeLayouts::Size12() +scalar System.Int32 Examples.UnsafeLayouts::Size13() +scalar System.Int32 Examples.UnsafeLayouts::Size14() +scalar System.Int32 Examples.UnsafeLayouts::Size15() +scalar System.Int32 Examples.UnsafeLayouts::Size16() +scalar System.Int32 Examples.UnsafeLayouts::Size17() +scalar System.Int32 Examples.UnsafeLayouts::Size18() +scalar System.Int32 Examples.UnsafeLayouts::Size2() +scalar System.Int32 Examples.UnsafeLayouts::Size3() +scalar System.Int32 Examples.UnsafeLayouts::Size4() +scalar System.Int32 Examples.UnsafeLayouts::Size5() +scalar System.Int32 Examples.UnsafeLayouts::Size6() +scalar System.Int32 Examples.UnsafeLayouts::Size7() +scalar System.Int32 Examples.UnsafeLayouts::Size8() +scalar System.Int32 Examples.UnsafeLayouts::Size9() +scalar System.Int32 Examples.WordReader::Read(System.Int32[]) +scalar System.Int32 Examples.WordReader::ReadSaved(System.Int32[]) +scalar System.Int32 Examples.WordReader::SkipAndPeek(System.Int32[]) +scalar System.Int32& Examples.ArrayElementReferences::Element(System.Int32[],System.Int32) +scalar System.Int32& Examples.UnsafeLayouts::Add6int(System.Int32&,System.Int32) +scalar System.Int32& Examples.UnsafeLayouts::Add6nint(System.Int32&,System.IntPtr) +scalar System.Int32& Examples.UnsafeLayouts::Add6nuint(System.Int32&,System.UIntPtr) +scalar System.Int32& Examples.ViewAccessors::ArrayData(System.Int32[]) +scalar System.Int32* Examples.StackAllocation::EscapingStorage() +scalar System.Int32[] Examples.PrimitiveArrays::Allocate(System.Int32) +scalar System.Int32[] Examples.PrimitiveArrays::Identity(System.Int32[]) +scalar System.Int32[] Examples.SampleArrays::ValuesOf(Examples.Samples) +scalar System.Int64 Examples.Bits::PrimitiveCounts64(System.Int64) +scalar System.Int64 Examples.Bits::PrimitiveLeft64(System.Int64,System.Int32) +scalar System.Int64 Examples.Bits::PrimitiveRight64(System.Int64,System.Int32) +scalar System.Int64 Examples.Bits::ReverseI64(System.Int64) +scalar System.Int64 Examples.ExceptionDocumentation::CheckedAdd(System.Int64,System.Int64) +scalar System.Int64 Examples.ExceptionDocumentation::Divide(System.Int64,System.Int64) +scalar System.Int64 Examples.IntegerMath::AbsInt64(System.Int64) +scalar System.Int64 Examples.IntegerMath::ClampInt64(System.Int64,System.Int64,System.Int64) +scalar System.Int64 Examples.IntegerMath::MaxInt64(System.Int64,System.Int64) +scalar System.Int64 Examples.IntegerMath::MinInt64(System.Int64,System.Int64) +scalar System.Int64 Examples.IntegerMath::WrongMin(System.Int64,System.Int64) +scalar System.Int64 Examples.IntegerOperations::Negate64(System.Int64) +scalar System.Int64 Examples.OutlineCounters::SignedLiteral() +scalar System.Int64 Examples.PointerOperations::Difference(System.Byte*,System.Byte*) +scalar System.Int64 Examples.PooledItem::get_Payload() +scalar System.Int64 Examples.PrimitiveArrays::ReadInt64(System.Int64[],System.Int32) +scalar System.Int64 Examples.TupleDivision::MathOutInt64(System.Int64,System.Int64,System.Int64&) +scalar System.Int64 Examples.UnsafeLayouts::Measure0int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure0nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure0nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure10int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure10nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure10nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure11int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure11nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure11nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure12int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure12nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure12nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure13int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure13nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure13nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure14int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure14nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure14nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure15int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure15nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure15nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure16int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure16nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure16nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure17int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure17nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure17nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure18int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure18nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure18nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure1int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure1nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure1nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure2int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure2nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure2nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure3int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure3nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure3nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure4int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure4nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure4nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure5int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure5nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure5nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure6int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure6nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure6nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure7int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure7nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure7nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure8int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure8nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure8nuint(System.UIntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure9int(System.Int32) +scalar System.Int64 Examples.UnsafeLayouts::Measure9nint(System.IntPtr) +scalar System.Int64 Examples.UnsafeLayouts::Measure9nuint(System.UIntPtr) +scalar System.Int64 Examples.WideProduct::BigMul32(System.Int32,System.Int32) +scalar System.Int64 Examples.WideProduct::SignedBigMul(System.Int64,System.Int64,System.Int64&) +scalar System.Int64& Examples.UnsafeLayouts::Add8int(System.Int64&,System.Int32) +scalar System.Int64& Examples.UnsafeLayouts::Add8nint(System.Int64&,System.IntPtr) +scalar System.Int64& Examples.UnsafeLayouts::Add8nuint(System.Int64&,System.UIntPtr) +scalar System.Int64[] Examples.PrimitiveArrays::AllocateLongs(System.Int32) +scalar System.IntPtr Examples.Arguments::Native(System.IntPtr) +scalar System.IntPtr Examples.Bits::PrimitiveCountsNative(System.IntPtr) +scalar System.IntPtr Examples.Bits::PrimitiveLeftNative(System.IntPtr,System.Int32) +scalar System.IntPtr Examples.Bits::PrimitiveRightNative(System.IntPtr,System.Int32) +scalar System.IntPtr Examples.Bits::ReverseNative(System.IntPtr) +scalar System.IntPtr Examples.GenericMethods::Identity(System.IntPtr) +scalar System.IntPtr Examples.GenericMethods::NativeIdentity(System.IntPtr) +scalar System.IntPtr Examples.IntegerMath::AbsNative(System.IntPtr) +scalar System.IntPtr Examples.IntegerMath::ClampNative(System.IntPtr,System.IntPtr,System.IntPtr) +scalar System.IntPtr Examples.IntegerMath::MaxNative(System.IntPtr,System.IntPtr) +scalar System.IntPtr Examples.IntegerMath::MinNative(System.IntPtr,System.IntPtr) +scalar System.IntPtr Examples.MemorySpans::NativeRead(System.ReadOnlySpan`1,System.Int32) +scalar System.IntPtr Examples.NativeIntegers::Add(System.IntPtr,System.IntPtr) +scalar System.IntPtr Examples.NativeIntegers::CheckedAdd(System.IntPtr,System.IntPtr) +scalar System.IntPtr Examples.NativeIntegers::Choose(System.IntPtr) +scalar System.IntPtr Examples.NativeIntegers::Divide(System.IntPtr,System.IntPtr) +scalar System.IntPtr Examples.NativeIntegers::Fields(Examples.NativePair&,System.IntPtr,System.UIntPtr) +scalar System.IntPtr Examples.NativeIntegers::Increment(System.IntPtr&) +scalar System.IntPtr Examples.NativeIntegers::LocalCall(System.IntPtr) +scalar System.IntPtr Examples.NativeIntegers::ReadWrite(System.IntPtr&,System.IntPtr) +scalar System.IntPtr Examples.NativeIntegers::Widen(System.Int32) +scalar System.IntPtr Examples.PrimitiveArrays::ReadIntPtr(System.IntPtr[],System.Int32) +scalar System.IntPtr Examples.UnsafeReferences::ByteOffset(System.Byte&,System.Byte&) +scalar System.IntPtr& Examples.UnsafeLayouts::Add10int(System.IntPtr&,System.Int32) +scalar System.IntPtr& Examples.UnsafeLayouts::Add10nint(System.IntPtr&,System.IntPtr) +scalar System.IntPtr& Examples.UnsafeLayouts::Add10nuint(System.IntPtr&,System.UIntPtr) +scalar System.ReadOnlySpan`1 Examples.AggregateArrays::ReadOnlyView(Examples.AggregateArrays/Pair[]) +scalar System.ReadOnlySpan`1 Examples.AggregateSpans::ReadOnlyFactory(Examples.AggregateSpans/Words&,System.Int32) +scalar System.ReadOnlySpan`1 Examples.AggregateSpans::WordsView(System.ReadOnlySpan`1) +scalar System.ReadOnlySpan`1 Examples.SpanConstructors::PassReadOnly(System.ReadOnlySpan`1) +scalar System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyDefaultReset(Examples.AggregateSpans/Words&) +scalar System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyEmpty() +scalar System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyBytes(System.ReadOnlySpan`1) +scalar System.ReadOnlySpan`1 Examples.StaticSpans::get_Lookup() +scalar System.ReadOnlySpan`1 Examples.MemorySpans::ArrayRange(System.Int32[],System.Int32,System.Int32) +scalar System.ReadOnlySpan`1 Examples.MemorySpans::ArrayReadOnly(System.Int32[]) +scalar System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlySingle(System.Int32&) +scalar System.ReadOnlySpan`1> Examples.SpanConstructors::Vector(System.Runtime.Intrinsics.Vector128`1&) +scalar System.ReadOnlySpan`1> Examples.SpanConstructors::VectorPointer(System.Runtime.Intrinsics.Vector128`1*,System.Int32) +scalar System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyWords(System.ReadOnlySpan`1) +scalar System.ReadOnlySpan`1 Examples.SpanTables::get_Words() +scalar System.ReadOnlySpan`1 Examples.SpanTables::get_Powers() +scalar System.Runtime.Intrinsics.Vector128`1 Examples.MarshaledMemory::ReadVector(System.ReadOnlySpan`1) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::Read128(System.Byte&) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::ReadAligned128(System.Byte*) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128Byte(Examples.ValueBitCasts/Pair) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadBytes128(Examples.VectorFields/Bytes128) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadInts128(Examples.VectorFields/Ints128) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt32(Examples.ValueBitCasts/Pair) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.AggregateArrays::ReadVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.AggregateSpans::ReadVector(System.ReadOnlySpan`1>) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::UnsafeReturn(System.UInt64) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Identity>(System.Runtime.Intrinsics.Vector128`1) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Vector(System.Runtime.Intrinsics.Vector128`1) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.UnsafeReferences::ReadHalf(Examples.FourLimbs&) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt64(Examples.ValueBitCasts/Pair) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Read128(Examples.VectorFields/Holder128) +scalar System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Write128(Examples.VectorFields/Holder128&,System.Runtime.Intrinsics.Vector128`1) +scalar System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17int(System.Runtime.Intrinsics.Vector128`1&,System.Int32) +scalar System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nint(System.Runtime.Intrinsics.Vector128`1&,System.IntPtr) +scalar System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nuint(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +scalar System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::Half(Examples.FourLimbs&) +scalar System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::SubtractVector(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +scalar System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int32(Examples.ValueBitCasts/Limbs) +scalar System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int64(Examples.ValueBitCasts/Limbs) +scalar System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::CreateWords(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32) +scalar System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt32(Examples.ValueBitCasts/Limbs) +scalar System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeReferences::Vector(Examples.FourLimbs&) +scalar System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::CopyLocal(Examples.ValueBitCasts/Limbs) +scalar System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::ReadLocal(System.Runtime.Intrinsics.Vector256`1&) +scalar System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt64(Examples.ValueBitCasts/Limbs) +scalar System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::WritePacked(Examples.VectorFields/Packed256&,System.Runtime.Intrinsics.Vector256`1) +scalar System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18int(System.Runtime.Intrinsics.Vector256`1&,System.Int32) +scalar System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nint(System.Runtime.Intrinsics.Vector256`1&,System.IntPtr) +scalar System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nuint(System.Runtime.Intrinsics.Vector256`1&,System.UIntPtr) +scalar System.Runtime.Intrinsics.Vector256`1[] Examples.PrimitiveArrays::AllocateVector(System.Int32) +scalar System.Runtime.Intrinsics.Vector512`1 Examples.UnalignedMemory::Read512(System.Byte&) +scalar System.SByte Examples.Bits::ReverseI8(System.SByte) +scalar System.SByte Examples.IntegerMath::AbsInt8(System.SByte) +scalar System.SByte Examples.IntegerMath::ClampInt8(System.SByte,System.SByte,System.SByte) +scalar System.SByte Examples.IntegerMath::MaxInt8(System.SByte,System.SByte) +scalar System.SByte Examples.IntegerMath::MinInt8(System.SByte,System.SByte) +scalar System.SByte Examples.PrimitiveArrays::ReadSByte(System.SByte[],System.Int32) +scalar System.SByte Examples.TupleDivision::MathSByteRemainder(System.SByte,System.SByte) +scalar System.SByte Examples.UnalignedMemory::ReadSigned(System.Byte&) +scalar System.SByte& Examples.UnsafeLayouts::Add2int(System.SByte&,System.Int32) +scalar System.SByte& Examples.UnsafeLayouts::Add2nint(System.SByte&,System.IntPtr) +scalar System.SByte& Examples.UnsafeLayouts::Add2nuint(System.SByte&,System.UIntPtr) +scalar System.Span`1 Examples.AggregateArrays::Slice(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +scalar System.Span`1 Examples.AggregateArrays::View(Examples.AggregateArrays/Pair[]) +scalar System.Span`1 Examples.AggregateSpans::Factory(Examples.AggregateSpans/Words&,System.Int32) +scalar System.Span`1 Examples.AggregateSpans::Slice(System.Span`1,System.Int32,System.Int32) +scalar System.Span`1 Examples.SpanConstructors::Aggregate(Examples.AggregateSpans/Words&) +scalar System.Span`1 Examples.SpanConstructors::Pointer(Examples.AggregateSpans/Words*,System.Int32) +scalar System.Span`1 Examples.EnumBuffers::Slice(System.Span`1,System.Int32) +scalar System.Span`1 Examples.ReinterpretedSpans::EnumWords(System.Span`1) +scalar System.Span`1 Examples.MemorySpans::ArrayAsSpan(System.Boolean[]) +scalar System.Span`1 Examples.AggregateSpans::Bytes(System.Span`1) +scalar System.Span`1 Examples.AggregateSpans::VectorBytes(System.Span`1>) +scalar System.Span`1 Examples.ReinterpretedSpans::Bytes(System.Span`1) +scalar System.Span`1 Examples.ReinterpretedSpans::EnumBytes(System.Span`1) +scalar System.Span`1 Examples.ReinterpretedSpans::Expanded(System.Span`1) +scalar System.Span`1 Examples.MemorySpans::ArrayConstructor(System.Char[]) +scalar System.Span`1 Examples.MemorySpans::ArrayWritable(System.Int32[]) +scalar System.Span`1 Examples.ReinterpretedSpans::SameWidth(System.Span`1) +scalar System.Span`1 Examples.SpanConstructors::DefaultReset(System.Int32&) +scalar System.Span`1 Examples.SpanConstructors::Empty() +scalar System.Span`1 Examples.SpanConstructors::Pass(System.Span`1) +scalar System.Span`1 Examples.SpanConstructors::Single(System.Int32&) +scalar System.Span`1 Examples.MemorySpans::ArrayAsSpanStart(System.IntPtr[],System.Int32) +scalar System.Span`1> Examples.AggregateArrays::VectorView(System.Runtime.Intrinsics.Vector128`1[]) +scalar System.Span`1 Examples.ReinterpretedSpans::Words(System.Span`1) +scalar System.Span`1 Examples.MemorySpans::ArrayAsSpanRange(System.UIntPtr[],System.Int32,System.Int32) +scalar System.String Examples.Guards::Required(System.String) +scalar System.String Examples.Strings::OrNone(System.String) +scalar System.String Examples.Strings::Pick(System.Boolean,System.String,System.String) +scalar System.String Examples.Strings::Relabel(Examples.Labelled,System.String) +scalar System.String[] Examples.TagArrays::Create(System.Int32) +scalar System.UInt16 Examples.Bits::ReverseU16(System.UInt16) +scalar System.UInt16 Examples.IntegerMath::ClampUInt16(System.UInt16,System.UInt16,System.UInt16) +scalar System.UInt16 Examples.IntegerMath::MaxUInt16(System.UInt16,System.UInt16) +scalar System.UInt16 Examples.IntegerMath::MinUInt16(System.UInt16,System.UInt16) +scalar System.UInt16 Examples.PrimitiveArrays::ReadUInt16(System.UInt16[],System.Int32) +scalar System.UInt16& Examples.UnsafeLayouts::Add5int(System.UInt16&,System.Int32) +scalar System.UInt16& Examples.UnsafeLayouts::Add5nint(System.UInt16&,System.IntPtr) +scalar System.UInt16& Examples.UnsafeLayouts::Add5nuint(System.UInt16&,System.UIntPtr) +scalar System.UInt32 Examples.Arguments::Address(System.UInt32) +scalar System.UInt32 Examples.Arguments::Alias(System.UInt32) +scalar System.UInt32 Examples.Arguments::CallerIsolation(System.UInt32) +scalar System.UInt32 Examples.Arguments::Overwrite(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.Arguments::ReassignReference(System.UInt32&,System.UInt32&) +scalar System.UInt32 Examples.Arithmetic::AtMostFive(System.UInt32) +scalar System.UInt32 Examples.Bits::Left32(System.UInt32,System.Int32) +scalar System.UInt32 Examples.Bits::PrimitiveLeadingU32(System.UInt32) +scalar System.UInt32 Examples.Bits::PrimitiveLeftU32(System.UInt32,System.Int32) +scalar System.UInt32 Examples.Bits::PrimitiveLogU32(System.UInt32) +scalar System.UInt32 Examples.Bits::PrimitivePopulationU32(System.UInt32) +scalar System.UInt32 Examples.Bits::PrimitiveRightU32(System.UInt32,System.Int32) +scalar System.UInt32 Examples.Bits::PrimitiveTrailingU32(System.UInt32) +scalar System.UInt32 Examples.Bits::ReverseU32(System.UInt32) +scalar System.UInt32 Examples.Bits::Right32(System.UInt32,System.Int32) +scalar System.UInt32 Examples.Bits::RoundUp32(System.UInt32) +scalar System.UInt32 Examples.Capabilities::AesFlags() +scalar System.UInt32 Examples.Capabilities::ArmFlags() +scalar System.UInt32 Examples.Capabilities::Flags() +scalar System.UInt32 Examples.Capabilities::VbmiFlags() +scalar System.UInt32 Examples.CheckedArithmetic::Add(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.CheckedConversions::Narrow(System.UInt64) +scalar System.UInt32 Examples.Division::Quotient(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.ExceptionDocumentation::Profile(System.UInt32) +scalar System.UInt32 Examples.ExceptionDocumentation::SpanItem(System.Int32) +scalar System.UInt32 Examples.GenericMethods::ClearPacked(System.UInt32) +scalar System.UInt32 Examples.GenericMethods::SwapPacked(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.GenericOwners::Address(System.UInt32) +scalar System.UInt32 Examples.GenericOwners::Combined(System.UInt32) +scalar System.UInt32 Examples.GenericOwners::Identity(System.UInt32) +scalar System.UInt32 Examples.GenericOwners::Nested(System.UInt32) +scalar System.UInt32 Examples.GenericOwners::NestedHelper(System.UInt32) +scalar System.UInt32 Examples.GenericOwners::Repeated(System.UInt32) +scalar System.UInt32 Examples.Guards::Index(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.IntegerMath::ClampUInt32(System.UInt32,System.UInt32,System.UInt32) +scalar System.UInt32 Examples.IntegerMath::MaxUInt32(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.IntegerMath::MinUInt32(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.Loops::AddTwo(System.UInt32) +scalar System.UInt32 Examples.Loops::Bump(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.Loops::CopyUntilZero(System.ReadOnlySpan`1,System.UInt32&) +scalar System.UInt32 Examples.Loops::CountCalls(System.UInt32) +scalar System.UInt32 Examples.Loops::CountUntil(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.Loops::CountUpToFive(System.UInt32) +scalar System.UInt32 Examples.Loops::Countdown(System.UInt32) +scalar System.UInt32 Examples.Loops::Decrement(System.UInt32) +scalar System.UInt32 Examples.Loops::DownThenUp(System.UInt32) +scalar System.UInt32 Examples.Loops::FeatureCountdown(System.UInt32) +scalar System.UInt32 Examples.Loops::InnerCount(Examples.Loops/Outer,System.UInt32) +scalar System.UInt32 Examples.Loops::ReachFive(System.UInt32) +scalar System.UInt32 Examples.Loops::TallyTwice(System.UInt32) +scalar System.UInt32 Examples.Loops::WrongCountdown(System.UInt32) +scalar System.UInt32 Examples.NarrowTotals::ReadSum() +scalar System.UInt32 Examples.NarrowTotals::WriteRead(System.UInt32) +scalar System.UInt32 Examples.NativeIntegers::NarrowUnsigned(System.UIntPtr) +scalar System.UInt32 Examples.Nested::Packed(System.UInt32) +scalar System.UInt32 Examples.OwnerHelper`1::Identity(System.UInt32) +scalar System.UInt32 Examples.OwnerHelper`1::Select(System.UInt32,System.UInt64) +scalar System.UInt32 Examples.OwnerOuter`1/Helper::Identity(System.UInt32) +scalar System.UInt32 Examples.OwnerOuter`1/Inner`1::Identity(System.UInt32) +scalar System.UInt32 Examples.PrimitiveArrays::ReadUInt32(System.UInt32[],System.Int32) +scalar System.UInt32 Examples.ReinterpretedSpans::Aliased(System.Span`1) +scalar System.UInt32 Examples.Sequential::ReadWrite(Examples.Record&,System.UInt32) +scalar System.UInt32 Examples.SpanTables::Word(System.Int32) +scalar System.UInt32 Examples.Throws::ActualValue(System.UInt32) +scalar System.UInt32 Examples.Throws::ArgumentInner(System.UInt32) +scalar System.UInt32 Examples.Throws::Inner(System.UInt32) +scalar System.UInt32 Examples.Throws::Null(System.UInt32) +scalar System.UInt32 Examples.Throws::Percent(System.UInt32) +scalar System.UInt32 Examples.Throws::Quotient(System.UInt32,System.UInt32) +scalar System.UInt32 Examples.WordReader::RangeEnd() +scalar System.UInt32 Examples.WordReader::ReplaceRemaining(System.UInt32) +scalar System.UInt32 Examples.WordReader::Reserve() +scalar System.UInt32 SelectorExamples.One.Collision::Add(System.UInt32,System.UInt32) +scalar System.UInt32& Examples.UnsafeLayouts::Add7int(System.UInt32&,System.Int32) +scalar System.UInt32& Examples.UnsafeLayouts::Add7nint(System.UInt32&,System.IntPtr) +scalar System.UInt32& Examples.UnsafeLayouts::Add7nuint(System.UInt32&,System.UIntPtr) +scalar System.UInt64 Examples.Account::get_Headroom() +scalar System.UInt64 Examples.AddStep::Apply(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.Aliases::Update(System.UInt64,System.UInt64,System.UInt64,System.UInt64) +scalar System.UInt64 Examples.Animal::Loudness() +scalar System.UInt64 Examples.Arguments::Struct(Examples.Packet) +scalar System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.Arithmetic::Alias(System.UInt64&,System.UInt64&) +scalar System.UInt64 Examples.Arithmetic::Calls(System.UInt64) +scalar System.UInt64 Examples.Arithmetic::Choose(System.Boolean,System.UInt64,System.UInt64) +scalar System.UInt64 Examples.Arithmetic::Distance(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.Arithmetic::Increment(System.UInt64) +scalar System.UInt64 Examples.Arithmetic::LargeStruct(System.UInt64) +scalar System.UInt64 Examples.Arithmetic::PastEnd(System.UInt64&) +scalar System.UInt64 Examples.Arithmetic::Read(Examples.Packet&) +scalar System.UInt64 Examples.Arithmetic::ReadLarge(Examples.LargePacket&) +scalar System.UInt64 Examples.Arithmetic::ReadValue(Examples.Packet) +scalar System.UInt64 Examples.Arithmetic::Struct(System.UInt64) +scalar System.UInt64 Examples.Arithmetic::StructCopy(System.UInt64) +scalar System.UInt64 Examples.Arithmetic::TypeCheck(System.UInt64) +scalar System.UInt64 Examples.Arithmetic::WrongAdd(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.BinarySpans::ReadBig(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.BinarySpans::ReadLittle(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.BinarySpans::TryReadBig(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.BinarySpans::TryReadLittle(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.Bird::Legs() +scalar System.UInt64 Examples.Bits::Left64(System.UInt64,System.Int32) +scalar System.UInt64 Examples.Bits::PrimitiveCountsU64(System.UInt64) +scalar System.UInt64 Examples.Bits::PrimitiveLeftU64(System.UInt64,System.Int32) +scalar System.UInt64 Examples.Bits::PrimitiveRightU64(System.UInt64,System.Int32) +scalar System.UInt64 Examples.Bits::ReverseU64(System.UInt64) +scalar System.UInt64 Examples.Bits::Right64(System.UInt64,System.Int32) +scalar System.UInt64 Examples.Bits::RoundTrip(System.UInt64) +scalar System.UInt64 Examples.Bits::RoundUp64(System.UInt64) +scalar System.UInt64 Examples.Bits::WrongRoundTrip(System.UInt64) +scalar System.UInt64 Examples.Blocks::CopyLocal(System.UInt64,System.UInt32) +scalar System.UInt64 Examples.Blocks::CopyParameter(System.UInt64,System.UInt64,System.UInt32) +scalar System.UInt64 Examples.Blocks::FillLocal(System.Byte,System.UInt32) +scalar System.UInt64 Examples.Blocks::FillParameter(System.UInt64,System.Byte,System.UInt32) +scalar System.UInt64 Examples.CheckedArithmetic::Add64(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.ComputedFlags::TraceBoth(System.UInt64) +scalar System.UInt64 Examples.ComputedFlags::TraceChained(System.UInt64) +scalar System.UInt64 Examples.ComputedFlags::TraceGated(System.UInt64) +scalar System.UInt64 Examples.ComputedFlags::TraceNotOff(System.UInt64) +scalar System.UInt64 Examples.ComputedFlags::TraceSilent(System.UInt64) +scalar System.UInt64 Examples.ConstantFlags::Count(System.UInt64) +scalar System.UInt64 Examples.ConstantFlags::CountOn(System.UInt64) +scalar System.UInt64 Examples.ConstantFlags::Trace(System.UInt64) +scalar System.UInt64 Examples.ConstantFlags::TraceOff(System.UInt64) +scalar System.UInt64 Examples.ConstantFlags::TraceThenCount(System.UInt64) +scalar System.UInt64 Examples.Constructors::Checked(System.UInt64) +scalar System.UInt64 Examples.Constructors::Default() +scalar System.UInt64 Examples.Constructors::Nested(System.UInt64) +scalar System.UInt64 Examples.Constructors::Packed(System.UInt64) +scalar System.UInt64 Examples.Constructors::Read(System.UInt64) +scalar System.UInt64 Examples.Counter::Add(System.UInt64) +scalar System.UInt64 Examples.Counter::Mix(System.UInt64,System.UInt32,System.UInt16,System.Byte) +scalar System.UInt64 Examples.Counter::Read() +scalar System.UInt64 Examples.Counter::WriteRead(System.UInt64) +scalar System.UInt64 Examples.Counter::WrongWriteRead(System.UInt64) +scalar System.UInt64 Examples.DerivedFlags::Count(System.UInt64) +scalar System.UInt64 Examples.DerivedFlags::CountLoud(System.UInt64) +scalar System.UInt64 Examples.DerivedFlags::Trace(System.UInt64) +scalar System.UInt64 Examples.DerivedFlags::TraceQuiet(System.UInt64) +scalar System.UInt64 Examples.Division::Quotient64(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.DivisionIntrinsics::DefinedLocal(System.UInt64) +scalar System.UInt64 Examples.DivisionIntrinsics::PrunedUnsafeLocal(System.UInt64) +scalar System.UInt64 Examples.DivisionIntrinsics::ReadLow(System.Runtime.Intrinsics.Vector128`1) +scalar System.UInt64 Examples.DivisionIntrinsics::UnsafeCall(System.UInt64) +scalar System.UInt64 Examples.DivisionIntrinsics::UnsafeHigh(System.UInt64) +scalar System.UInt64 Examples.DivisionIntrinsics::UnsafeLow(System.UInt64) +scalar System.UInt64 Examples.DivisionIntrinsics::UnsafeLowNext(System.UInt64) +scalar System.UInt64 Examples.Dog::Legs() +scalar System.UInt64 Examples.Dog::Loudness() +scalar System.UInt64 Examples.ExceptionDocumentation::UnsignedDivide(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.GenericEnvelope`1::Read() +scalar System.UInt64 Examples.GenericLayouts::Composed(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.GenericLayouts::Field(Examples.GenericPair`1&) +scalar System.UInt64 Examples.GenericLayouts::Roundtrip(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.GenericMethods::Choose(System.Boolean,System.UInt64,System.UInt64) +scalar System.UInt64 Examples.GenericMethods::First(System.UInt64,System.UInt32) +scalar System.UInt64 Examples.GenericMethods::Forward(System.UInt64) +scalar System.UInt64 Examples.GenericMethods::Identity64(System.UInt64) +scalar System.UInt64 Examples.GenericMethods::Identity(System.UInt64) +scalar System.UInt64 Examples.GenericMethods::Instance(System.UInt64) +scalar System.UInt64 Examples.GenericMethods::Nested(System.UInt64) +scalar System.UInt64 Examples.GenericMethods::Repeated(System.UInt64) +scalar System.UInt64 Examples.GenericMethods::Select(System.Boolean,System.UInt64,System.UInt64) +scalar System.UInt64 Examples.GenericMethods::TwoTypes(System.UInt64,System.UInt32) +scalar System.UInt64 Examples.GenericOwners::NestedSpanFirst(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.GenericOwners::OwnerSpanFirst(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.GenericOwners::SpanFirst(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.GenericOwners::SpanSecond(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.GenericPair`1::GetSecond() +scalar System.UInt64 Examples.GenericPicker::Pick(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::Count>(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::CountWide(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::Pad,System.UInt32>(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::Pad,System.UInt64>(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::PadNarrow(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::PadWide(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::Trace>(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::TraceNarrow(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::Width,System.UInt32>(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::Width,System.UInt64>(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::WidthNarrow(System.UInt64) +scalar System.UInt64 Examples.GenericReceivers::WidthWide(System.UInt64) +scalar System.UInt64 Examples.IMeasureFive::Examples.IMeasure.Measure(System.UInt64) +scalar System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +scalar System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +scalar System.UInt64 Examples.IScale::Scale(System.UInt64) +scalar System.UInt64 Examples.IScaleMore::Examples.IScale.Scale(System.UInt64) +scalar System.UInt64 Examples.IScaleMost::Examples.IScale.Scale(System.UInt64) +scalar System.UInt64 Examples.IScaleSide::Examples.IScale.Scale(System.UInt64) +scalar System.UInt64 Examples.InParameters::Constrained(Examples.InWord&,Examples.InWord&) +scalar System.UInt64 Examples.InParameters::ConstrainedSum(Examples.InWord&,System.ReadOnlySpan`1) +scalar System.UInt64 Examples.InParameters::Sum(Examples.InWord&,System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.InWord::Examples.IInWord.Sum(System.ReadOnlySpan`1& modreq(System.Runtime.InteropServices.InAttribute)) +scalar System.UInt64 Examples.InitCounter::get_Value() +scalar System.UInt64 Examples.IntegerMath::ClampUInt64(System.UInt64,System.UInt64,System.UInt64) +scalar System.UInt64 Examples.IntegerMath::MaxUInt64(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.IntegerMath::MinUInt64(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.Loops::Sum(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.Loops::SumIndexed(System.UInt64[]) +scalar System.UInt64 Examples.Loops::SumThroughLocal(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.Loops::SumThroughSpan(System.UInt64[]) +scalar System.UInt64 Examples.Loops::TwoStructs(System.UInt32) +scalar System.UInt64 Examples.MarshaledMemory::Read(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.Measured`1::Width(System.UInt64) +scalar System.UInt64 Examples.Measured`1::Width(System.UInt64) +scalar System.UInt64 Examples.MostSpecificBodies::Five(System.UInt64) +scalar System.UInt64 Examples.MostSpecificBodies::Measured(System.UInt64) +scalar System.UInt64 Examples.MostSpecificBodies::More(System.UInt64) +scalar System.UInt64 Examples.MostSpecificBodies::Most(System.UInt64) +scalar System.UInt64 Examples.MostSpecificBodies::Own(System.UInt64) +scalar System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +scalar System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +scalar System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +scalar System.UInt64 Examples.Nested::Alias(System.UInt64) +scalar System.UInt64 Examples.Nested::Copy(System.UInt64) +scalar System.UInt64 Examples.Nested::Explicit(System.UInt64) +scalar System.UInt64 Examples.Nested::Pass(System.UInt64) +scalar System.UInt64 Examples.Nested::Read(Examples.Leaf) +scalar System.UInt64 Examples.Nested::ReadOuter(Examples.NestedRecord) +scalar System.UInt64 Examples.Nested::Sized(System.UInt64) +scalar System.UInt64 Examples.Nested::Snapshot(System.UInt64) +scalar System.UInt64 Examples.Nested::WrongCopy(System.UInt64) +scalar System.UInt64 Examples.NestedConstructor::Sum() +scalar System.UInt64 Examples.NullReferences::Optional(Examples.FourLimbs&,System.Boolean) +scalar System.UInt64 Examples.NullReferences::Read() +scalar System.UInt64 Examples.NullReferences::ReadField() +scalar System.UInt64 Examples.NullReferences::Unchecked(Examples.FourLimbs&,System.Boolean) +scalar System.UInt64 Examples.Objects::BalanceOf(Examples.Account) +scalar System.UInt64 Examples.Objects::Describe(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.Objects::Fresh(System.UInt32) +scalar System.UInt64 Examples.Objects::IdOf(Examples.Shape) +scalar System.UInt64 Examples.Objects::LinkedValue(Examples.Node,Examples.Node) +scalar System.UInt64 Examples.Objects::Replace(Examples.Account,System.UInt64) +scalar System.UInt64 Examples.Objects::SecondValue(Examples.Node) +scalar System.UInt64 Examples.Objects::UnderFive(Examples.Account) +scalar System.UInt64 Examples.Objects::Withdraw(Examples.Account,System.UInt64) +scalar System.UInt64 Examples.Odometer::Next() +scalar System.UInt64 Examples.OutlineCountdown::Drain() +scalar System.UInt64 Examples.OutlineCountdown::DrainAtLeastOnce() +scalar System.UInt64 Examples.OutlineCounters::ExchangeAdd(System.UInt64) +scalar System.UInt64 Examples.OutlineCounters::UnsignedLiteral() +scalar System.UInt64 Examples.OutlineCounters::WrongExchangeAdd(System.UInt64) +scalar System.UInt64 Examples.OwnerHelper`1::First(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.OwnerOuter`1/Inner`1::First(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.PackedConstructor::Sum() +scalar System.UInt64 Examples.PointerOperations::WriteEnum(Examples.Unsigned64*,System.UInt64) +scalar System.UInt64 Examples.PrimitiveArrays::ReadUInt64(System.UInt64[],System.Int32) +scalar System.UInt64 Examples.ScaleOwn::Scale(System.UInt64) +scalar System.UInt64 Examples.Sequential::Packed(System.UInt64) +scalar System.UInt64 Examples.SkipInitialization::Read(System.UInt64&) +scalar System.UInt64 Examples.SpanHelpers::First(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanHelpers::Second(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.SpanReferences::Back(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanReferences::BigEndianWord(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanReferences::ByteSecond(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanReferences::Construct(System.ReadOnlySpan`1) +scalar System.UInt64 Examples.SpanReferences::CursorSum(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanReferences::Element(System.ReadOnlySpan`1&,System.Int32) +scalar System.UInt64 Examples.SpanReferences::FirstHigh(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanReferences::GenericHead(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanReferences::Head(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanReferences::High(System.Span`1&,System.Int32) +scalar System.UInt64 Examples.SpanReferences::HighAddress(System.ReadOnlySpan`1&,System.Int32) +scalar System.UInt64 Examples.SpanReferences::ReadOnlyHigh(System.ReadOnlySpan`1&,System.Int32) +scalar System.UInt64 Examples.SpanReferences::Second(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanReferences::Word(System.ReadOnlySpan`1&) +scalar System.UInt64 Examples.SpanTables::First() +scalar System.UInt64 Examples.SpanTables::Power(System.Int32) +scalar System.UInt64 Examples.StaticInterfaces::AddCounted(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.StaticInterfaces::XorPlain(System.UInt64,System.UInt64) +scalar System.UInt64 Examples.StaticValues::Read(Examples.Word256&) +scalar System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.Allowance&) +scalar System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.DoublingAllowance&) +scalar System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.RefundingAllowance&) +scalar System.UInt64 Examples.StaticVirtualDefaults::SpendDefault(Examples.Allowance&) +scalar System.UInt64 Examples.StaticVirtualDefaults::SpendOverride(Examples.DoublingAllowance&) +scalar System.UInt64 Examples.StaticVirtualDefaults::SpendRefunding(Examples.RefundingAllowance&) +scalar System.UInt64 Examples.StructMethods::Arguments(System.UInt64) +scalar System.UInt64 Examples.StructMethods::ByValue(System.UInt64) +scalar System.UInt64 Examples.StructMethods::ConstructLocal(System.UInt64) +scalar System.UInt64 Examples.StructMethods::Initialized(System.UInt64) +scalar System.UInt64 Examples.StructMethods::Mutate(System.UInt64) +scalar System.UInt64 Examples.StructMethods::Replace(System.UInt64) +scalar System.UInt64 Examples.TriplePrice::get_Price() +scalar System.UInt64 Examples.TupleDivision::Item1(System.ValueTuple`2&) +scalar System.UInt64 Examples.UnalignedMemory::ReadAlignedWord(System.Byte*) +scalar System.UInt64 Examples.UnalignedMemory::ReadPointerWord(System.Byte*) +scalar System.UInt64 Examples.UnalignedMemory::ReadWord(System.Byte&) +scalar System.UInt64 Examples.UnsafeReferences::PastEnd(Examples.FourLimbs&) +scalar System.UInt64 Examples.UnsafeReferences::Read(Examples.FourLimbs&) +scalar System.UInt64 Examples.UnsafeReferences::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +scalar System.UInt64 Examples.VirtualCalls::DogLegs() +scalar System.UInt64 Examples.VirtualCalls::LegsOf(Examples.Animal) +scalar System.UInt64 Examples.VirtualCalls::LoudnessOf(Examples.Animal) +scalar System.UInt64 Examples.WideProduct::BigMul(System.UInt64,System.UInt64,System.UInt64&) +scalar System.UInt64 Examples.WideProduct::UnsignedBigMul32(System.UInt32,System.UInt32) +scalar System.UInt64 Examples.WordReader::ReplaceRemainingWide(System.UInt32) +scalar System.UInt64 Examples.XorStep::Examples.IBinaryStep.Apply(System.UInt64,System.UInt64) +scalar System.UInt64 SelectorExamples.One.Collision::Add(System.UInt64,System.UInt64) +scalar System.UInt64 SelectorExamples.Two.Collision::Add(System.UInt64,System.UInt64) +scalar System.UInt64& Examples.Arithmetic::Escape() +scalar System.UInt64& Examples.UnsafeLayouts::Add9int(System.UInt64&,System.Int32) +scalar System.UInt64& Examples.UnsafeLayouts::Add9nint(System.UInt64&,System.IntPtr) +scalar System.UInt64& Examples.UnsafeLayouts::Add9nuint(System.UInt64&,System.UIntPtr) +scalar System.UInt64& Examples.UnsafeReferences::Pointer(System.Byte*) +scalar System.UInt64& Examples.UnsafeReferences::SubtractElements32(System.UInt64&,System.Int32) +scalar System.UInt64& Examples.UnsafeReferences::SubtractElementsNative(System.UInt64&,System.IntPtr) +scalar System.UInt64& Examples.UnsafeReferences::SubtractElementsUnsigned(System.UInt64&,System.UIntPtr) +scalar System.UInt64& Examples.UnsafeReferences::Word(Examples.FourLimbs&) +scalar System.UIntPtr Examples.Bits::LeftNative(System.UIntPtr,System.Int32) +scalar System.UIntPtr Examples.Bits::PrimitiveCountsUNative(System.UIntPtr) +scalar System.UIntPtr Examples.Bits::PrimitiveLeftUNative(System.UIntPtr,System.Int32) +scalar System.UIntPtr Examples.Bits::PrimitiveRightUNative(System.UIntPtr,System.Int32) +scalar System.UIntPtr Examples.Bits::ReverseUNative(System.UIntPtr) +scalar System.UIntPtr Examples.Bits::RightNative(System.UIntPtr,System.Int32) +scalar System.UIntPtr Examples.Bits::RoundUpNative(System.UIntPtr) +scalar System.UIntPtr Examples.IntegerMath::ClampUNative(System.UIntPtr,System.UIntPtr,System.UIntPtr) +scalar System.UIntPtr Examples.IntegerMath::MaxUNative(System.UIntPtr,System.UIntPtr) +scalar System.UIntPtr Examples.IntegerMath::MinUNative(System.UIntPtr,System.UIntPtr) +scalar System.UIntPtr Examples.MemorySpans::NativeWrite(System.Span`1,System.Int32,System.UIntPtr) +scalar System.UIntPtr Examples.NativeIntegers::AddUnsigned(System.UIntPtr,System.UIntPtr) +scalar System.UIntPtr Examples.NativeIntegers::CheckedAddUnsigned(System.UIntPtr,System.UIntPtr) +scalar System.UIntPtr Examples.NativeIntegers::DivideUnsigned(System.UIntPtr,System.UIntPtr) +scalar System.UIntPtr Examples.NativeIntegers::ReadWriteUnsigned(System.UIntPtr&,System.UIntPtr) +scalar System.UIntPtr Examples.NativeIntegers::Shift(System.UIntPtr,System.Int32) +scalar System.UIntPtr Examples.NativeIntegers::WidenUnsigned(System.UInt32) +scalar System.UIntPtr Examples.PrimitiveArrays::ReadUIntPtr(System.UIntPtr[],System.Int32) +scalar System.UIntPtr& Examples.UnsafeLayouts::Add11int(System.UIntPtr&,System.Int32) +scalar System.UIntPtr& Examples.UnsafeLayouts::Add11nint(System.UIntPtr&,System.IntPtr) +scalar System.UIntPtr& Examples.UnsafeLayouts::Add11nuint(System.UIntPtr&,System.UIntPtr) +scalar System.ValueTuple`2 Examples.TupleDivision::MathByte(System.Byte,System.Byte) +scalar System.ValueTuple`2 Examples.TupleDivision::MathInt16(System.Int16,System.Int16) +scalar System.ValueTuple`2 Examples.TupleDivision::MathInt32(System.Int32,System.Int32) +scalar System.ValueTuple`2 Examples.TupleDivision::MathInt64(System.Int64,System.Int64) +scalar System.ValueTuple`2 Examples.TupleDivision::MathIntPtr(System.IntPtr,System.IntPtr) +scalar System.ValueTuple`2 Examples.TupleDivision::MathSByte(System.SByte,System.SByte) +scalar System.ValueTuple`2 Examples.TupleDivision::MathUInt16(System.UInt16,System.UInt16) +scalar System.ValueTuple`2 Examples.TupleDivision::MathUInt32(System.UInt32,System.UInt32) +scalar System.ValueTuple`2 Examples.TupleDivision::MathUInt64(System.UInt64,System.UInt64) +scalar System.ValueTuple`2 Examples.TupleDivision::MathUIntPtr(System.UIntPtr,System.UIntPtr) +scalar System.Void Examples.Account::.ctor() +scalar System.Void Examples.Account::Deposit(System.UInt64) +scalar System.Void Examples.AggregateArrays::Write(Examples.AggregateArrays/Pair[],System.Int32,Examples.AggregateArrays/Pair) +scalar System.Void Examples.AggregateArrays::WriteVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32,System.Runtime.Intrinsics.Vector128`1) +scalar System.Void Examples.AggregateSpanFill::Aliased(System.Span`1) +scalar System.Void Examples.AggregateSpanFill::Generic(System.Span`1>,Examples.GenericPair`1) +scalar System.Void Examples.AggregateSpanFill::Vector128(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +scalar System.Void Examples.AggregateSpanFill::Vector256(System.Span`1>,System.Runtime.Intrinsics.Vector256`1) +scalar System.Void Examples.AggregateSpanFill::Vector512(System.Span`1>,System.Runtime.Intrinsics.Vector512`1) +scalar System.Void Examples.AggregateSpanFill::Words(System.Span`1,Examples.AggregateSpans/Words) +scalar System.Void Examples.AggregateSpans::Clear(System.Span`1) +scalar System.Void Examples.AggregateSpans::Write(System.Span`1,Examples.AggregateSpans/Words) +scalar System.Void Examples.AggregateSpans::WriteVector(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +scalar System.Void Examples.Animal::.ctor() +scalar System.Void Examples.Arguments::Increment(System.UInt32&) +scalar System.Void Examples.Arithmetic::Mutate(System.UInt64&) +scalar System.Void Examples.BinarySpans::WriteBig(System.Span`1) +scalar System.Void Examples.BinarySpans::WriteLittle(System.Span`1) +scalar System.Void Examples.Bird::.ctor() +scalar System.Void Examples.Blocks::Copy(System.Byte&,System.Byte&,System.UInt32) +scalar System.Void Examples.Blocks::CopyAligned(System.Byte&,System.Byte&,System.UInt32) +scalar System.Void Examples.Blocks::CopyPointer(System.Byte*,System.Byte*,System.UInt32) +scalar System.Void Examples.Blocks::CopyPointerAligned(System.Byte*,System.Byte*,System.UInt32) +scalar System.Void Examples.Blocks::Fill(System.Byte&,System.Byte,System.UInt32) +scalar System.Void Examples.Blocks::FillAligned(System.Byte&,System.Byte,System.UInt32) +scalar System.Void Examples.Blocks::FillPointer(System.Byte*,System.Byte,System.UInt32) +scalar System.Void Examples.Blocks::FillPointerAligned(System.Byte*,System.Byte,System.UInt32) +scalar System.Void Examples.Bucket::.ctor() +scalar System.Void Examples.CheckedConstructor::.ctor(System.UInt64) +scalar System.Void Examples.Circle::.ctor(System.UInt64,System.UInt64) +scalar System.Void Examples.ConstructorLeaf::.ctor(System.UInt32) +scalar System.Void Examples.Counter::.ctor(System.UInt64) +scalar System.Void Examples.Counter::Increment(System.Byte&) +scalar System.Void Examples.Counter::Replace(Examples.Counter) +scalar System.Void Examples.Counter::Set(System.UInt64) +scalar System.Void Examples.CounterValue::Increment() +scalar System.Void Examples.DefaultConstructor::.ctor() +scalar System.Void Examples.DivisionIntrinsics::Init128(System.Runtime.Intrinsics.Vector128`1&) +scalar System.Void Examples.DivisionIntrinsics::Init256(System.Runtime.Intrinsics.Vector256`1&) +scalar System.Void Examples.DivisionIntrinsics::Init512(System.Runtime.Intrinsics.Vector512`1&) +scalar System.Void Examples.DivisionIntrinsics::SwapHalves(System.ReadOnlySpan`1,System.Span`1) +scalar System.Void Examples.DivisionIntrinsics::UnsafeStore(System.Runtime.Intrinsics.Vector128`1&,System.UInt64) +scalar System.Void Examples.Dog::.ctor() +scalar System.Void Examples.Enums::Assign(Examples.Unsigned64&,Examples.Unsigned64) +scalar System.Void Examples.ExceptionDocumentation::Reference(System.UInt64&) +scalar System.Void Examples.ExplicitCounterValue::Examples.ICounterValue.Increment() +scalar System.Void Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Increment() +scalar System.Void Examples.Gauge::.ctor() +scalar System.Void Examples.Gauge::Settle() +scalar System.Void Examples.GenericEnvelope`1::.ctor(System.UInt64,System.UInt64) +scalar System.Void Examples.GenericMethods::Clear(Examples.PackedLeaf&) +scalar System.Void Examples.GenericMethods::Swap32(System.Int32&,System.Int32&) +scalar System.Void Examples.GenericMethods::Swap(Examples.PackedLeaf&,Examples.PackedLeaf&) +scalar System.Void Examples.GenericMethods::Swap(System.Int32&,System.Int32&) +scalar System.Void Examples.GenericMethods::SwapRecord(Examples.PackedLeaf&,Examples.PackedLeaf&) +scalar System.Void Examples.GenericPair`1::.ctor(Examples.GenericPair`1,Examples.GenericPair`1) +scalar System.Void Examples.GenericPair`1::.ctor(System.UInt32,System.UInt32) +scalar System.Void Examples.GenericPair`1::.ctor(System.UInt64,System.UInt64) +scalar System.Void Examples.GenericPair`1::SetSecond(System.UInt64) +scalar System.Void Examples.InParameters::Add(Examples.InWord&,Examples.InWord&,Examples.InWord&) +scalar System.Void Examples.InWord::Examples.IInWord.Add(Examples.InWord& modreq(System.Runtime.InteropServices.InAttribute),Examples.InWord&) +scalar System.Void Examples.ItemPool::.ctor(System.Int32) +scalar System.Void Examples.ItemPool::Prewarm(System.Int32) +scalar System.Void Examples.ItemPool::Return(Examples.PooledItem) +scalar System.Void Examples.Labelled::.ctor() +scalar System.Void Examples.Loops::Fill(System.Span`1,System.UInt32) +scalar System.Void Examples.Loops::FillIndexed(System.UInt32[],System.UInt32) +scalar System.Void Examples.Loops::FillThrough(System.Span`1,System.UInt32) +scalar System.Void Examples.Loops::FillThroughSpan(System.UInt32[],System.UInt32) +scalar System.Void Examples.Loops::FillWords(System.UInt64&,System.UInt64,System.Int32) +scalar System.Void Examples.Loops::FillWordsDown(System.UInt64&,System.UInt64,System.Int32) +scalar System.Void Examples.Loops::Put(System.Span`1,System.UInt32) +scalar System.Void Examples.Loops::Stamp(System.UInt32&,System.UInt32) +scalar System.Void Examples.Marker::.ctor() +scalar System.Void Examples.MarshaledMemory::Write(System.Span`1,System.UInt64&) +scalar System.Void Examples.MarshaledMemory::WriteAggregate(System.Span`1,Examples.FourLimbs&) +scalar System.Void Examples.MemorySpans::ClearBytes(System.Span`1) +scalar System.Void Examples.MemorySpans::FillCharacters(System.Span`1,System.Char) +scalar System.Void Examples.MemorySpans::FillIntegers(System.Span`1,System.Int32) +scalar System.Void Examples.MemorySpans::FillLongSlice(System.Span`1,System.Int32,System.Int32,System.Int64) +scalar System.Void Examples.MemorySpans::Write(System.Span`1,System.Int32,System.Int32) +scalar System.Void Examples.NestedConstructor::.ctor(System.UInt64) +scalar System.Void Examples.Node::.ctor() +scalar System.Void Examples.NodeArrays::Put(Examples.Shape[],System.Int32,Examples.Shape) +scalar System.Void Examples.NodeArrays::Refill(Examples.Bucket,Examples.Node[]) +scalar System.Void Examples.NullReferences::Write(System.UInt64) +scalar System.Void Examples.NullReferences::WriteField(System.UInt64) +scalar System.Void Examples.Objects::Bump(Examples.Account,System.UInt64) +scalar System.Void Examples.Objects::BumpBoth(Examples.Account,Examples.Account,System.UInt64) +scalar System.Void Examples.Objects::ClearTag(Examples.Tagged) +scalar System.Void Examples.Objects::CopyTag(Examples.Tagged,Examples.Tagged) +scalar System.Void Examples.Objects::DepositBoth(Examples.Account,Examples.Account,System.UInt64) +scalar System.Void Examples.Objects::DepositFrom(Examples.Account,Examples.Account,System.UInt32) +scalar System.Void Examples.Objects::DepositTimes(Examples.Account,System.UInt64,System.UInt32) +scalar System.Void Examples.Objects::FillTag(Examples.Tagged,Examples.Tagged) +scalar System.Void Examples.Objects::Link(Examples.Node,Examples.Node) +scalar System.Void Examples.Objects::LinkBoth(Examples.Node,Examples.Node,Examples.Node) +scalar System.Void Examples.Objects::Relink(Examples.Node,Examples.Node,Examples.Node) +scalar System.Void Examples.Objects::SetLimit(Examples.Account,System.UInt32) +scalar System.Void Examples.OutlineCountdown::Clear() +scalar System.Void Examples.PackedConstructor::.ctor(System.UInt64) +scalar System.Void Examples.PackedConstructor::.ctor(System.UInt64,System.Byte) +scalar System.Void Examples.PooledItem::.ctor(Examples.ItemPool,System.Int32) +scalar System.Void Examples.PooledItem::set_IsRented(System.Boolean) +scalar System.Void Examples.PooledItem::set_Payload(System.Int64) +scalar System.Void Examples.PrimitiveArrays::FillEnums(Examples.AllocationTag[],Examples.AllocationTag) +scalar System.Void Examples.PrimitiveArrays::FillGeneric(Examples.AllocationTag[],Examples.AllocationTag) +scalar System.Void Examples.PrimitiveArrays::FillGeneric(System.Int32[],System.Int32) +scalar System.Void Examples.PrimitiveArrays::FillIntegers(System.Int32[],System.Int32) +scalar System.Void Examples.PrimitiveArrays::FillRangeEnums(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +scalar System.Void Examples.PrimitiveArrays::FillRangeGeneric(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +scalar System.Void Examples.PrimitiveArrays::FillRangeGeneric(System.Int32[],System.Int32,System.Int32,System.Int32) +scalar System.Void Examples.PrimitiveArrays::FillRangeIntegers(System.Int32[],System.Int32,System.Int32,System.Int32) +scalar System.Void Examples.PrimitiveArrays::WriteBoolean(System.Boolean[],System.Int32,System.Boolean) +scalar System.Void Examples.PrimitiveArrays::WriteByte(System.Byte[],System.Int32,System.Byte) +scalar System.Void Examples.PrimitiveArrays::WriteChar(System.Char[],System.Int32,System.Char) +scalar System.Void Examples.PrimitiveArrays::WriteGeneric(System.Int32[],System.Int32,System.Int32) +scalar System.Void Examples.PrimitiveArrays::WriteInt16(System.Int16[],System.Int32,System.Int16) +scalar System.Void Examples.PrimitiveArrays::WriteInt32(System.Int32[],System.Int32,System.Int32) +scalar System.Void Examples.PrimitiveArrays::WriteInt64(System.Int64[],System.Int32,System.Int64) +scalar System.Void Examples.PrimitiveArrays::WriteIntPtr(System.IntPtr[],System.Int32,System.IntPtr) +scalar System.Void Examples.PrimitiveArrays::WriteSByte(System.SByte[],System.Int32,System.SByte) +scalar System.Void Examples.PrimitiveArrays::WriteUInt16(System.UInt16[],System.Int32,System.UInt16) +scalar System.Void Examples.PrimitiveArrays::WriteUInt32(System.UInt32[],System.Int32,System.UInt32) +scalar System.Void Examples.PrimitiveArrays::WriteUInt64(System.UInt64[],System.Int32,System.UInt64) +scalar System.Void Examples.PrimitiveArrays::WriteUIntPtr(System.UIntPtr[],System.Int32,System.UIntPtr) +scalar System.Void Examples.SampleArrays::Refill(Examples.Samples,System.Int32[]) +scalar System.Void Examples.Samples::.ctor() +scalar System.Void Examples.Shape::.ctor(System.UInt64) +scalar System.Void Examples.SkipInitialization::Skip(Examples.FourLimbs&) +scalar System.Void Examples.SkipInitialization::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +scalar System.Void Examples.SpanCopies::Copy(System.Span`1,System.Span`1) +scalar System.Void Examples.SpanCopies::CopyLongs(System.ReadOnlySpan`1,System.Span`1) +scalar System.Void Examples.SpanCopies::CopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +scalar System.Void Examples.SpanReferences/Limbs::.ctor(System.ReadOnlySpan`1&,System.Boolean) +scalar System.Void Examples.SpanReferences::SetHigh(System.ReadOnlySpan`1&,System.UInt64) +scalar System.Void Examples.SpanReferences::WriteFirst(System.Span`1&,System.Int32) +scalar System.Void Examples.SpanReferences::WriteSecond(System.ReadOnlySpan`1&,System.UInt64) +scalar System.Void Examples.SpanReferences::WriteWord(System.ReadOnlySpan`1&,System.UInt64) +scalar System.Void Examples.Square::.ctor(System.UInt64,System.UInt64) +scalar System.Void Examples.StaticSpans::Write() +scalar System.Void Examples.StructMethods::ChangeCopy(Examples.Counter) +scalar System.Void Examples.TagArrays::Put(System.String[],System.Int32,Examples.Tagged) +scalar System.Void Examples.TagArrays::Stamp(System.String[],Examples.Tagged,System.UInt64) +scalar System.Void Examples.TagArrays::Take(Examples.Tagged,System.String[],System.Int32) +scalar System.Void Examples.Tagged::.ctor() +scalar System.Void Examples.TailCalls::ForwardVoid(System.Int32&) +scalar System.Void Examples.TailCalls::Store(System.Int32&) +scalar System.Void Examples.Tally::.ctor(System.UInt32) +scalar System.Void Examples.Throws::ThrowDivideByZero() +scalar System.Void Examples.Throws::ThrowNull() +scalar System.Void Examples.TileArrays::Relane(Examples.Tiles,System.Runtime.Intrinsics.Vector128`1[]) +scalar System.Void Examples.Tiles::.ctor() +scalar System.Void Examples.TypedCopy::Packet(Examples.Packet&,Examples.Packet&) +scalar System.Void Examples.TypedCopy::Record(Examples.Record&,Examples.Record&) +scalar System.Void Examples.TypedCopy::Vector128(System.Runtime.Intrinsics.Vector128`1&,System.Runtime.Intrinsics.Vector128`1&) +scalar System.Void Examples.TypedCopy::Vector256(System.Runtime.Intrinsics.Vector256`1&,System.Runtime.Intrinsics.Vector256`1&) +scalar System.Void Examples.UnalignedMemory::Write128(System.Byte&,System.Runtime.Intrinsics.Vector128`1) +scalar System.Void Examples.UnalignedMemory::Write512(System.Byte&,System.Runtime.Intrinsics.Vector512`1) +scalar System.Void Examples.UnalignedMemory::WriteAggregate(System.Byte&,Examples.FourLimbs) +scalar System.Void Examples.UnalignedMemory::WriteAligned128(System.Byte*,System.Runtime.Intrinsics.Vector128`1) +scalar System.Void Examples.UnalignedMemory::WriteAlignedAggregate(System.Byte*,Examples.FourLimbs) +scalar System.Void Examples.UnalignedMemory::WriteAlignedWord(System.Byte*,System.UInt64) +scalar System.Void Examples.UnalignedMemory::WriteHalf(System.Byte&,System.Int16) +scalar System.Void Examples.UnalignedMemory::WritePointerAggregate(System.Byte*,Examples.FourLimbs) +scalar System.Void Examples.UnalignedMemory::WritePointerWord(System.Byte*,System.UInt64) +scalar System.Void Examples.UnalignedMemory::WriteSigned(System.Byte&,System.SByte) +scalar System.Void Examples.UnalignedMemory::WriteWord(System.Byte&,System.UInt64) +scalar System.Void Examples.WordReader::.ctor() +scalar System.Void Examples.WordReader::Advance() +scalar System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Examples.InitCounter::set_Value(System.UInt64) +scalar method System.Int32 *(System.Int32)[] Examples.ManagedDispatch::NullTable() +x64-sse42 Examples.Account Examples.Objects::FirstPresent(Examples.Account,Examples.Account) +x64-sse42 Examples.AggregateArrays/Pair Examples.AggregateArrays::Read(Examples.AggregateArrays/Pair[],System.Int32) +x64-sse42 Examples.AggregateArrays/Pair& Examples.AggregateArrays::Address(Examples.AggregateArrays/Pair[],System.Int32) +x64-sse42 Examples.AggregateArrays/Pair& Examples.AggregateArrays::Data(Examples.AggregateArrays/Pair[]) +x64-sse42 Examples.AggregateArrays/Pair[] Examples.TileArrays::PairsOf(Examples.Tiles) +x64-sse42 Examples.AggregateSpans/VectorRecord Examples.AggregateSpans::LayoutRead(System.ReadOnlySpan`1) +x64-sse42 Examples.AggregateSpans/Words Examples.AggregateSpans::Read(System.ReadOnlySpan`1) +x64-sse42 Examples.AllocationTag[] Examples.PrimitiveArrays::AllocateUnnamedZero(System.Int32) +x64-sse42 Examples.CheckedConstructor Examples.Constructors::MakeChecked(System.UInt64) +x64-sse42 Examples.Counter Examples.Constructors::Return(System.UInt64) +x64-sse42 Examples.Counter Examples.Constructors::WrongReturn(System.UInt64) +x64-sse42 Examples.DefaultConstructor Examples.Constructors::MakeDefault() +x64-sse42 Examples.Dog Examples.ClassTests::AsDog(Examples.Animal) +x64-sse42 Examples.EnumBuffers/Large Examples.EnumBuffers::Wide(Examples.EnumBuffers/Large[],System.Int32) +x64-sse42 Examples.EnumBuffers/Small Examples.EnumBuffers::Address(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +x64-sse42 Examples.EnumBuffers/Small Examples.EnumBuffers::FillClear(Examples.EnumBuffers/Small[]) +x64-sse42 Examples.EnumBuffers/Small Examples.EnumBuffers::Loop(Examples.EnumBuffers/Small[],System.Int32) +x64-sse42 Examples.EnumBuffers/Small Examples.EnumBuffers::Read(System.ReadOnlySpan`1) +x64-sse42 Examples.EnumBuffers/Small Examples.EnumBuffers::Roundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +x64-sse42 Examples.EnumBuffers/Small Examples.EnumBuffers::SpanRoundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +x64-sse42 Examples.FourLimbs Examples.MarshaledMemory::ReadAggregate(System.ReadOnlySpan`1) +x64-sse42 Examples.FourLimbs Examples.UnalignedMemory::ReadAggregate(System.Byte&) +x64-sse42 Examples.FourLimbs Examples.UnalignedMemory::ReadAlignedAggregate(System.Byte*) +x64-sse42 Examples.FourLimbs Examples.UnalignedMemory::ReadPointerAggregate(System.Byte*) +x64-sse42 Examples.FourLimbs& Examples.UnsafeReferences::Mutable(Examples.FourLimbs&) +x64-sse42 Examples.FourLimbs& Examples.UnsafeReferences::SubtractAggregate(Examples.FourLimbs&,System.IntPtr) +x64-sse42 Examples.GenericPair`1> Examples.GenericLayouts::Nested(System.UInt32,System.UInt32) +x64-sse42 Examples.GenericPair`1 Examples.AggregateArrays::ReadGeneric(Examples.GenericPair`1[],System.Int32) +x64-sse42 Examples.GenericPair`1 Examples.GenericLayouts::Create(System.UInt64,System.UInt64) +x64-sse42 Examples.IndirectValues/Box`1 Examples.IndirectValues::Generic(Examples.IndirectValues/Box`1) +x64-sse42 Examples.IndirectValues/Box`1 Examples.IndirectValues::Identity>(Examples.IndirectValues/Box`1) +x64-sse42 Examples.IndirectValues/Pair Examples.IndirectValues::Identity(Examples.IndirectValues/Pair) +x64-sse42 Examples.IndirectValues/Pair Examples.IndirectValues::Read(Examples.IndirectValues/Pair&) +x64-sse42 Examples.IndirectValues/Pair Examples.IndirectValues::Record(Examples.IndirectValues/Pair) +x64-sse42 Examples.IndirectValues/Pair Examples.IndirectValues::Reference(Examples.IndirectValues/Pair&) +x64-sse42 Examples.ItemPool Examples.PooledItem::get_Owner() +x64-sse42 Examples.Leaf Examples.Nested::Extract(Examples.NestedRecord) +x64-sse42 Examples.Leaf Examples.Nested::WrongExtract(Examples.NestedRecord) +x64-sse42 Examples.NestedConstructor Examples.Constructors::MakeNested(System.UInt64) +x64-sse42 Examples.NestedRecord& Examples.UnsafeLayouts::Add15int(Examples.NestedRecord&,System.Int32) +x64-sse42 Examples.NestedRecord& Examples.UnsafeLayouts::Add15nint(Examples.NestedRecord&,System.IntPtr) +x64-sse42 Examples.NestedRecord& Examples.UnsafeLayouts::Add15nuint(Examples.NestedRecord&,System.UIntPtr) +x64-sse42 Examples.NestedRecord[] Examples.PrimitiveArrays::AllocateNested(System.Int32) +x64-sse42 Examples.Node Examples.NodeArrays::At(Examples.Node[],System.Int32) +x64-sse42 Examples.Node Examples.NodeArrays::First(Examples.Bucket) +x64-sse42 Examples.Node Examples.NodeArrays::Install(Examples.Node[],System.Int32,Examples.Node,System.UInt64) +x64-sse42 Examples.Node Examples.NodeArrays::Pair(Examples.Node,Examples.Node) +x64-sse42 Examples.Node Examples.NodeArrays::Shift(Examples.Node[],Examples.Node) +x64-sse42 Examples.Node Examples.Objects::Detach(Examples.Node) +x64-sse42 Examples.Node[] Examples.NodeArrays::Create(System.Int32) +x64-sse42 Examples.PackedConstructor Examples.Constructors::MakePacked(System.UInt64) +x64-sse42 Examples.PackedLeaf& Examples.GenericMethods::Next(Examples.PackedLeaf&,System.Int32) +x64-sse42 Examples.PackedLeaf& Examples.GenericMethods::NextPacked(Examples.PackedLeaf&,System.Int32) +x64-sse42 Examples.PackedLeaf& Examples.UnsafeLayouts::Add14int(Examples.PackedLeaf&,System.Int32) +x64-sse42 Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nint(Examples.PackedLeaf&,System.IntPtr) +x64-sse42 Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nuint(Examples.PackedLeaf&,System.UIntPtr) +x64-sse42 Examples.PackedLeaf[] Examples.PrimitiveArrays::AllocatePacked(System.Int32) +x64-sse42 Examples.Packet& Examples.UnsafeLayouts::Add16int(Examples.Packet&,System.Int32) +x64-sse42 Examples.Packet& Examples.UnsafeLayouts::Add16nint(Examples.Packet&,System.IntPtr) +x64-sse42 Examples.Packet& Examples.UnsafeLayouts::Add16nuint(Examples.Packet&,System.UIntPtr) +x64-sse42 Examples.PooledItem Examples.ItemPool::Rent() +x64-sse42 Examples.Signed16 Examples.Enums::IdentityS16(Examples.Signed16) +x64-sse42 Examples.Signed16 Examples.Enums::ThroughCall(Examples.Signed16) +x64-sse42 Examples.Signed16 Examples.PointerOperations::ReadEnum(Examples.Signed16*,System.Int32) +x64-sse42 Examples.Signed16[] Examples.PrimitiveArrays::AllocateSigned16(System.Int32) +x64-sse42 Examples.Signed32 Examples.Enums::IdentityS32(Examples.Signed32) +x64-sse42 Examples.Signed32[] Examples.PrimitiveArrays::AllocateSigned32(System.Int32) +x64-sse42 Examples.Signed64 Examples.Enums::IdentityS64(Examples.Signed64) +x64-sse42 Examples.Signed64[] Examples.PrimitiveArrays::AllocateSigned64(System.Int32) +x64-sse42 Examples.Signed8 Examples.Enums::Field(Examples.Signed8) +x64-sse42 Examples.Signed8 Examples.Enums::IdentityS8(Examples.Signed8) +x64-sse42 Examples.Signed8 Examples.Enums::Read(Examples.EnumRecord&) +x64-sse42 Examples.Signed8 Examples.GenericMethods::EnumIdentity(Examples.Signed8) +x64-sse42 Examples.Signed8 Examples.GenericMethods::Identity(Examples.Signed8) +x64-sse42 Examples.Signed8& Examples.UnsafeLayouts::Add12int(Examples.Signed8&,System.Int32) +x64-sse42 Examples.Signed8& Examples.UnsafeLayouts::Add12nint(Examples.Signed8&,System.IntPtr) +x64-sse42 Examples.Signed8& Examples.UnsafeLayouts::Add12nuint(Examples.Signed8&,System.UIntPtr) +x64-sse42 Examples.Signed8[] Examples.PrimitiveArrays::AllocateSigned8(System.Int32) +x64-sse42 Examples.Tiles Examples.TileArrays::Hold(Examples.AggregateArrays/Pair[],System.Runtime.Intrinsics.Vector128`1[]) +x64-sse42 Examples.Unsigned16 Examples.Enums::IdentityU16(Examples.Unsigned16) +x64-sse42 Examples.Unsigned16[] Examples.PrimitiveArrays::AllocateUnsigned16(System.Int32) +x64-sse42 Examples.Unsigned32 Examples.Enums::IdentityU32(Examples.Unsigned32) +x64-sse42 Examples.Unsigned32[] Examples.PrimitiveArrays::AllocateUnsigned32(System.Int32) +x64-sse42 Examples.Unsigned64 Examples.Enums::IdentityU64(Examples.Unsigned64) +x64-sse42 Examples.Unsigned64 Examples.Enums::Local(Examples.Unsigned64) +x64-sse42 Examples.Unsigned64 Examples.Enums::SetFlag(Examples.Unsigned64) +x64-sse42 Examples.Unsigned64 Examples.Enums::WrongSetFlag(Examples.Unsigned64) +x64-sse42 Examples.Unsigned64& Examples.UnsafeLayouts::Add13int(Examples.Unsigned64&,System.Int32) +x64-sse42 Examples.Unsigned64& Examples.UnsafeLayouts::Add13nint(Examples.Unsigned64&,System.IntPtr) +x64-sse42 Examples.Unsigned64& Examples.UnsafeLayouts::Add13nuint(Examples.Unsigned64&,System.UIntPtr) +x64-sse42 Examples.Unsigned64[] Examples.PrimitiveArrays::AllocateUnsigned64(System.Int32) +x64-sse42 Examples.Unsigned8 Examples.Enums::IdentityU8(Examples.Unsigned8) +x64-sse42 Examples.Unsigned8[] Examples.PrimitiveArrays::AllocateUnsigned8(System.Int32) +x64-sse42 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int32(System.Runtime.Intrinsics.Vector256`1) +x64-sse42 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int64(System.Runtime.Intrinsics.Vector256`1) +x64-sse42 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt32(System.Runtime.Intrinsics.Vector256`1) +x64-sse42 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt64(System.Runtime.Intrinsics.Vector256`1) +x64-sse42 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ParameterCopy(Examples.ValueBitCasts/Limbs) +x64-sse42 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ReadAggregate(Examples.ValueBitCasts/Limbs&) +x64-sse42 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::RoundTrip(Examples.ValueBitCasts/Limbs) +x64-sse42 Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128Byte(System.Runtime.Intrinsics.Vector128`1) +x64-sse42 Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt32(System.Runtime.Intrinsics.Vector128`1) +x64-sse42 Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt64(System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Boolean Examples.AggregateSpans::Copy(System.ReadOnlySpan`1,System.Span`1) +x64-sse42 System.Boolean Examples.AggregateSpans::IsEmpty(System.Span`1) +x64-sse42 System.Boolean Examples.AggregateSpans::VectorIsEmpty(System.ReadOnlySpan`1>) +x64-sse42 System.Boolean Examples.Allowance::Spend(Examples.Allowance&,System.UInt64) +x64-sse42 System.Boolean Examples.Bits::PowerI32(System.Int32) +x64-sse42 System.Boolean Examples.Bits::PowerI64(System.Int64) +x64-sse42 System.Boolean Examples.Bits::PowerNative(System.IntPtr) +x64-sse42 System.Boolean Examples.Bits::PowerU32(System.UInt32) +x64-sse42 System.Boolean Examples.Bits::PowerU64(System.UInt64) +x64-sse42 System.Boolean Examples.Bits::PowerUNative(System.UIntPtr) +x64-sse42 System.Boolean Examples.Bits::PrimitivePower32(System.Int32) +x64-sse42 System.Boolean Examples.Bits::PrimitivePower64(System.Int64) +x64-sse42 System.Boolean Examples.Bits::PrimitivePowerNative(System.IntPtr) +x64-sse42 System.Boolean Examples.Bits::PrimitivePowerU32(System.UInt32) +x64-sse42 System.Boolean Examples.Bits::PrimitivePowerU64(System.UInt64) +x64-sse42 System.Boolean Examples.Bits::PrimitivePowerUNative(System.UIntPtr) +x64-sse42 System.Boolean Examples.ClassTests::IsDog(Examples.Animal) +x64-sse42 System.Boolean Examples.ComputedFlags::get_Both() +x64-sse42 System.Boolean Examples.ComputedFlags::get_Middle() +x64-sse42 System.Boolean Examples.ComputedFlags::get_NotOff() +x64-sse42 System.Boolean Examples.ComputedFlags::get_Outer() +x64-sse42 System.Boolean Examples.DivisionIntrinsics::EqualBytes256(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1) +x64-sse42 System.Boolean Examples.DivisionIntrinsics::EqualSigned512(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1) +x64-sse42 System.Boolean Examples.DivisionIntrinsics::EqualWords128(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Boolean Examples.DivisionIntrinsics::UnsafeEquals(System.UInt64) +x64-sse42 System.Boolean Examples.DoublingAllowance::Examples.IAllowance.Spend(Examples.DoublingAllowance&) +x64-sse42 System.Boolean Examples.DoublingAllowance::Spend(Examples.DoublingAllowance&,System.UInt64) +x64-sse42 System.Boolean Examples.Forced::get_IsActive() +x64-sse42 System.Boolean Examples.Gate`1::get_Open() +x64-sse42 System.Boolean Examples.Gate`1::get_Open() +x64-sse42 System.Boolean Examples.Hush`1::get_Silent() +x64-sse42 System.Boolean Examples.IAllowance`1::Spend(Examples.Allowance&) +x64-sse42 System.Boolean Examples.ILoud::Examples.IQuietByDefault.get_IsActive() +x64-sse42 System.Boolean Examples.ILoudByDefault::get_IsActive() +x64-sse42 System.Boolean Examples.IQuiet::Examples.ILoudByDefault.get_IsActive() +x64-sse42 System.Boolean Examples.IQuietByDefault::get_IsActive() +x64-sse42 System.Boolean Examples.IRefundingAllowance`1::Examples.IAllowance.Spend(Examples.RefundingAllowance&) +x64-sse42 System.Boolean Examples.IntegerOperations::Greater32(System.Int32,System.Int32) +x64-sse42 System.Boolean Examples.IntegerOperations::Greater64(System.Int64,System.Int64) +x64-sse42 System.Boolean Examples.ItemPool::IsIdle(Examples.PooledItem) +x64-sse42 System.Boolean Examples.Loops::AllZero(System.ReadOnlySpan`1) +x64-sse42 System.Boolean Examples.MarshaledMemory::TryRead(System.ReadOnlySpan`1,System.UInt64&) +x64-sse42 System.Boolean Examples.MarshaledMemory::TryReadAggregate(System.ReadOnlySpan`1,Examples.FourLimbs&) +x64-sse42 System.Boolean Examples.MarshaledMemory::TryWrite(System.Span`1,System.UInt64&) +x64-sse42 System.Boolean Examples.MemorySpans::BooleanWrite(System.Span`1,System.Int32,System.Boolean) +x64-sse42 System.Boolean Examples.NullReferences::IsLive(System.UInt64&) +x64-sse42 System.Boolean Examples.NullReferences::IsNull() +x64-sse42 System.Boolean Examples.Objects::HasTag(Examples.Tagged) +x64-sse42 System.Boolean Examples.Objects::IsNull(Examples.Account) +x64-sse42 System.Boolean Examples.Objects::IsPresent(Examples.Account) +x64-sse42 System.Boolean Examples.Objects::Same(Examples.Account,Examples.Account) +x64-sse42 System.Boolean Examples.Objects::SameTag(Examples.Tagged,Examples.Tagged) +x64-sse42 System.Boolean Examples.Objects::TwoMarkers() +x64-sse42 System.Boolean Examples.PointerOperations::Greater(System.Byte*,System.Byte*) +x64-sse42 System.Boolean Examples.PointerOperations::GreaterEqual(System.Byte*,System.Byte*) +x64-sse42 System.Boolean Examples.PointerOperations::Less(System.Byte*,System.Byte*) +x64-sse42 System.Boolean Examples.PointerOperations::LessEqual(System.Byte*,System.Byte*) +x64-sse42 System.Boolean Examples.PooledItem::get_IsRented() +x64-sse42 System.Boolean Examples.PrimitiveArrays::ReadBoolean(System.Boolean[],System.Int32) +x64-sse42 System.Boolean Examples.RefundingAllowance::Spend(Examples.RefundingAllowance&,System.UInt64) +x64-sse42 System.Boolean Examples.SpanCopies::TryCopy(System.Span`1,System.Span`1) +x64-sse42 System.Boolean Examples.SpanCopies::TryCopyEnum(System.ReadOnlySpan`1,System.Span`1) +x64-sse42 System.Boolean Examples.SpanCopies::TryCopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +x64-sse42 System.Boolean Examples.StepOff::get_IsActive() +x64-sse42 System.Boolean Examples.StepOn::get_IsActive() +x64-sse42 System.Boolean Examples.Strings::SameLabel(System.String,System.String) +x64-sse42 System.Boolean Examples.Strings::SameText() +x64-sse42 System.Boolean Examples.UnsafeReferences::AreSame(System.Byte&,System.Byte&) +x64-sse42 System.Boolean Examples.UnsafeReferences::Greater(System.Byte&,System.Byte&) +x64-sse42 System.Boolean Examples.UnsafeReferences::Less(System.Byte&,System.Byte&) +x64-sse42 System.Boolean Examples.ViewAccessors::Empty(System.Span`1) +x64-sse42 System.Boolean Examples.ViewAccessors::PinnableNull(System.Span`1) +x64-sse42 System.Boolean Examples.ViewAccessors::ReadOnlyEmpty(System.ReadOnlySpan`1) +x64-sse42 System.Boolean Examples.ViewAccessors::ReadOnlyPinnableNull(System.ReadOnlySpan`1) +x64-sse42 System.Boolean Examples.ViewAccessors::ReadOnlyReferenceNull(System.ReadOnlySpan`1) +x64-sse42 System.Boolean Examples.ViewAccessors::ReferenceNull(System.Span`1) +x64-sse42 System.Boolean Examples.WordFlag`1::get_IsActive() +x64-sse42 System.Boolean Examples.WordFlag`1::get_IsActive() +x64-sse42 System.Boolean& Examples.UnsafeLayouts::Add0int(System.Boolean&,System.Int32) +x64-sse42 System.Boolean& Examples.UnsafeLayouts::Add0nint(System.Boolean&,System.IntPtr) +x64-sse42 System.Boolean& Examples.UnsafeLayouts::Add0nuint(System.Boolean&,System.UIntPtr) +x64-sse42 System.Byte Examples.Arguments::Narrow(System.Byte) +x64-sse42 System.Byte Examples.Bits::ReverseU8(System.Byte) +x64-sse42 System.Byte Examples.Counter::Parameter(System.Byte) +x64-sse42 System.Byte Examples.GenericMethods::Identity(System.Byte) +x64-sse42 System.Byte Examples.IntegerMath::ClampUInt8(System.Byte,System.Byte,System.Byte) +x64-sse42 System.Byte Examples.IntegerMath::MaxUInt8(System.Byte,System.Byte) +x64-sse42 System.Byte Examples.IntegerMath::MinUInt8(System.Byte,System.Byte) +x64-sse42 System.Byte Examples.PrimitiveArrays::ReadByte(System.Byte[],System.Int32) +x64-sse42 System.Byte Examples.SpanReferences::At(System.ReadOnlySpan`1&,System.Int32) +x64-sse42 System.Byte Examples.StaticSpans::First() +x64-sse42 System.Byte Examples.StaticSpans::Last() +x64-sse42 System.Byte Examples.StaticSpans::PastEnd() +x64-sse42 System.Byte Examples.StructMethods::Narrow(System.Byte) +x64-sse42 System.Byte& Examples.UnsafeLayouts::Add3int(System.Byte&,System.Int32) +x64-sse42 System.Byte& Examples.UnsafeLayouts::Add3nint(System.Byte&,System.IntPtr) +x64-sse42 System.Byte& Examples.UnsafeLayouts::Add3nuint(System.Byte&,System.UIntPtr) +x64-sse42 System.Byte& Examples.UnsafeReferences::AddSigned(System.Byte&,System.IntPtr) +x64-sse42 System.Byte& Examples.UnsafeReferences::AddUnsigned(System.Byte&,System.UIntPtr) +x64-sse42 System.Byte& Examples.UnsafeReferences::Subtract(System.Byte&,System.IntPtr) +x64-sse42 System.Byte& Examples.UnsafeReferences::SubtractUnsigned(System.Byte&,System.UIntPtr) +x64-sse42 System.Byte* Examples.PointerOperations::Subtract(System.Byte*,System.Int32) +x64-sse42 System.Byte* Examples.PointerOperations::SubtractNative(System.Byte*,System.IntPtr) +x64-sse42 System.Byte* Examples.UnsafeReferences::AddPointer(System.Byte*,System.Int32) +x64-sse42 System.Byte* Examples.UnsafeReferences::AddPointerAggregate(System.Byte*,System.Int32) +x64-sse42 System.Byte* Examples.UnsafeReferences::SubtractPointer(System.Byte*,System.Int32) +x64-sse42 System.Byte* Examples.UnsafeReferences::SubtractPointerAggregate(System.Byte*,System.Int32) +x64-sse42 System.Byte[] Examples.PrimitiveArrays::AllocateBytes(System.Int32) +x64-sse42 System.Char Examples.MemorySpans::CharacterWrite(System.Span`1,System.Int32,System.Char) +x64-sse42 System.Char Examples.NarrowIntegers::Character(System.Int32) +x64-sse42 System.Char Examples.PointerOperations::ReadCharacter(System.Char*,System.Int32) +x64-sse42 System.Char Examples.PrimitiveArrays::ReadChar(System.Char[],System.Int32) +x64-sse42 System.Char& Examples.UnsafeLayouts::Add1int(System.Char&,System.Int32) +x64-sse42 System.Char& Examples.UnsafeLayouts::Add1nint(System.Char&,System.IntPtr) +x64-sse42 System.Char& Examples.UnsafeLayouts::Add1nuint(System.Char&,System.UIntPtr) +x64-sse42 System.Int16 Examples.Bits::ReverseI16(System.Int16) +x64-sse42 System.Int16 Examples.IntegerMath::AbsInt16(System.Int16) +x64-sse42 System.Int16 Examples.IntegerMath::ClampInt16(System.Int16,System.Int16,System.Int16) +x64-sse42 System.Int16 Examples.IntegerMath::MaxInt16(System.Int16,System.Int16) +x64-sse42 System.Int16 Examples.IntegerMath::MinInt16(System.Int16,System.Int16) +x64-sse42 System.Int16 Examples.NarrowIntegers::Identity(System.Int16) +x64-sse42 System.Int16 Examples.PrimitiveArrays::ReadInt16(System.Int16[],System.Int32) +x64-sse42 System.Int16 Examples.UnalignedMemory::ReadHalf(System.Byte&) +x64-sse42 System.Int16& Examples.UnsafeLayouts::Add4int(System.Int16&,System.Int32) +x64-sse42 System.Int16& Examples.UnsafeLayouts::Add4nint(System.Int16&,System.IntPtr) +x64-sse42 System.Int16& Examples.UnsafeLayouts::Add4nuint(System.Int16&,System.UIntPtr) +x64-sse42 System.Int16[] Examples.PrimitiveArrays::AllocateShorts(System.Int32) +x64-sse42 System.Int32 Examples.AggregateArrays::ConstructedLength(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Arithmetic::AtLeastFive(System.Int32) +x64-sse42 System.Int32 Examples.Arithmetic::FiveToOne(System.Int32) +x64-sse42 System.Int32 Examples.Arithmetic::Larger(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Arithmetic::WordAdjusted(System.Int32) +x64-sse42 System.Int32 Examples.Arithmetic::WordSized(System.Int32) +x64-sse42 System.Int32 Examples.ArrayElementReferences::Read(System.Int32[],System.Int32) +x64-sse42 System.Int32 Examples.ArrayElementReferences::Write(System.Int32[],System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.BinarySpans::TryWriteBig(System.Span`1) +x64-sse42 System.Int32 Examples.BinarySpans::TryWriteLittle(System.Span`1) +x64-sse42 System.Int32 Examples.Bits::Leading32(System.UInt32) +x64-sse42 System.Int32 Examples.Bits::Leading64(System.UInt64) +x64-sse42 System.Int32 Examples.Bits::LeadingNative(System.UIntPtr) +x64-sse42 System.Int32 Examples.Bits::Log32(System.UInt32) +x64-sse42 System.Int32 Examples.Bits::Log64(System.UInt64) +x64-sse42 System.Int32 Examples.Bits::LogNative(System.UIntPtr) +x64-sse42 System.Int32 Examples.Bits::Population32(System.UInt32) +x64-sse42 System.Int32 Examples.Bits::Population64(System.UInt64) +x64-sse42 System.Int32 Examples.Bits::PopulationNative(System.UIntPtr) +x64-sse42 System.Int32 Examples.Bits::PrimitiveLeading32(System.Int32) +x64-sse42 System.Int32 Examples.Bits::PrimitiveLeft32(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Bits::PrimitiveNarrow16(System.UInt32) +x64-sse42 System.Int32 Examples.Bits::PrimitiveNarrow8(System.UInt32) +x64-sse42 System.Int32 Examples.Bits::PrimitiveNarrowRotate16(System.UInt32,System.Int32) +x64-sse42 System.Int32 Examples.Bits::PrimitiveNarrowRotate8(System.UInt32,System.Int32) +x64-sse42 System.Int32 Examples.Bits::PrimitiveNarrowSigned(System.UInt32) +x64-sse42 System.Int32 Examples.Bits::PrimitivePopulation32(System.Int32) +x64-sse42 System.Int32 Examples.Bits::PrimitiveRight32(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Bits::PrimitiveTrailing32(System.Int32) +x64-sse42 System.Int32 Examples.Bits::ReverseI32(System.Int32) +x64-sse42 System.Int32 Examples.Bits::Trailing32(System.UInt32) +x64-sse42 System.Int32 Examples.Bits::Trailing64(System.UInt64) +x64-sse42 System.Int32 Examples.Bits::TrailingNative(System.UIntPtr) +x64-sse42 System.Int32 Examples.Bits::WrongLog64(System.UInt64) +x64-sse42 System.Int32 Examples.Capabilities::ArmCount(System.UInt64) +x64-sse42 System.Int32 Examples.Capabilities::Endian() +x64-sse42 System.Int32 Examples.Capabilities::Platform() +x64-sse42 System.Int32 Examples.Capabilities::ReferenceFree() +x64-sse42 System.Int32 Examples.Capabilities::VbmiCount(System.UInt64) +x64-sse42 System.Int32 Examples.CheckedArithmetic::AddSigned(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +x64-sse42 System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +x64-sse42 System.Int32 Examples.ConstrainedCalls::ClosedBoth(Examples.ClosedInterfaceCounter&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::ClosedGeneric(Examples.GenericInterfaceCounter`1&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::DirectGeneric(Examples.GenericCounterValue`1&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::Explicit(Examples.ExplicitCounterValue&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::Generic(Examples.GenericCounterValue`1&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::GenericExplicit(Examples.ExplicitGenericCounterValue`1&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::Implicit(Examples.CounterValue&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.CounterValue&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.ExplicitCounterValue&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::IncrementAndRead>(Examples.ExplicitGenericCounterValue`1&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::Mutating(Examples.CounterValue&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::MutatingExplicit(Examples.ExplicitCounterValue&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::Read(Examples.CounterValue&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::Read(Examples.ExplicitCounterValue&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::ReadClosedInt(Examples.ClosedInterfaceCounter&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::ReadClosedInt>(Examples.GenericInterfaceCounter`1&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::ReadClosedLong(Examples.ClosedInterfaceCounter&) +x64-sse42 System.Int32 Examples.ConstrainedCalls::TwoGenericOwners(Examples.GenericCounterValue`1&,Examples.GenericCounterValue`1&) +x64-sse42 System.Int32 Examples.CounterValue::Read() +x64-sse42 System.Int32 Examples.Enums::Branch(Examples.Signed8) +x64-sse42 System.Int32 Examples.ExceptionDocumentation::Absolute(System.Int32) +x64-sse42 System.Int32 Examples.ExceptionDocumentation::CheckedNarrow(System.Int64) +x64-sse42 System.Int32 Examples.ExceptionDocumentation::Clamp(System.Int32,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.ExplicitCounterValue::Examples.ICounterValue.Read() +x64-sse42 System.Int32 Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Read() +x64-sse42 System.Int32 Examples.Gauge::BelowFive() +x64-sse42 System.Int32 Examples.Gauge::Blend(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Blended(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Bounded(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Capped(Examples.Gauge,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::CappedLevel() +x64-sse42 System.Int32 Examples.Gauge::CappedSlack() +x64-sse42 System.Int32 Examples.Gauge::Checked(Examples.Gauge,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::CheckedLevel() +x64-sse42 System.Int32 Examples.Gauge::Clamp(System.Int32,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Clamped(System.Int32,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Current() +x64-sse42 System.Int32 Examples.Gauge::Doubled(System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Floor(System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Floored(System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Gap(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::IsFive() +x64-sse42 System.Int32 Examples.Gauge::Margin() +x64-sse42 System.Int32 Examples.Gauge::Raise(Examples.Gauge,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::RaisedLevel() +x64-sse42 System.Int32 Examples.Gauge::Saturate(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Slack() +x64-sse42 System.Int32 Examples.Gauge::Smaller(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Total(System.Int32[]) +x64-sse42 System.Int32 Examples.Gauge::Twice(System.Int32) +x64-sse42 System.Int32 Examples.Gauge::Within(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.GenericCounterValue`1::Echo(System.Int32) +x64-sse42 System.Int32 Examples.GenericCounterValue`1::Read() +x64-sse42 System.Int32 Examples.GenericCounterValue`1::Read() +x64-sse42 System.Int32 Examples.GenericInterfaceCounter`1::Read() +x64-sse42 System.Int32 Examples.GenericLayouts::TupleSize() +x64-sse42 System.Int32 Examples.GenericLayouts::VectorSize() +x64-sse42 System.Int32 Examples.GenericMethods::Identity32(System.Int32) +x64-sse42 System.Int32 Examples.GenericMethods::Identity(System.Int32) +x64-sse42 System.Int32 Examples.GenericMethods::Mixed(System.Int32,System.Byte) +x64-sse42 System.Int32 Examples.GenericMethods::PackedSize() +x64-sse42 System.Int32 Examples.GenericMethods::Read32(System.Int32&) +x64-sse42 System.Int32 Examples.GenericMethods::Read(System.Int32&) +x64-sse42 System.Int32 Examples.GenericMethods::Size() +x64-sse42 System.Int32 Examples.GenericMethods::Size>() +x64-sse42 System.Int32 Examples.GenericMethods::VectorSize() +x64-sse42 System.Int32 Examples.GenericMethods::WrongIdentity(System.Int32) +x64-sse42 System.Int32 Examples.GenericOwners::SpanCount(System.ReadOnlySpan`1) +x64-sse42 System.Int32 Examples.Guards::Count(System.Int32) +x64-sse42 System.Int32 Examples.IntegerMath::AbsInt32(System.Int32) +x64-sse42 System.Int32 Examples.IntegerMath::ClampInt32(System.Int32,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.IntegerMath::MaxInt32(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.IntegerMath::MinInt32(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.IntegerMath::SignInt16(System.Int16) +x64-sse42 System.Int32 Examples.IntegerMath::SignInt32(System.Int32) +x64-sse42 System.Int32 Examples.IntegerMath::SignInt64(System.Int64) +x64-sse42 System.Int32 Examples.IntegerMath::SignInt8(System.SByte) +x64-sse42 System.Int32 Examples.IntegerMath::SignNative(System.IntPtr) +x64-sse42 System.Int32 Examples.IntegerOperations::Negate32(System.Int32) +x64-sse42 System.Int32 Examples.ItemPool::Trim(System.Int32) +x64-sse42 System.Int32 Examples.ItemPool::get_Available() +x64-sse42 System.Int32 Examples.ItemPool::get_Capacity() +x64-sse42 System.Int32 Examples.ItemPool::get_Rented() +x64-sse42 System.Int32 Examples.Loops::CountToFive(System.Int32) +x64-sse42 System.Int32 Examples.Loops::Drain(System.ReadOnlySpan`1) +x64-sse42 System.Int32 Examples.ManagedDispatch::LocalEscape(System.Int32) +x64-sse42 System.Int32 Examples.ManagedDispatch::NullLength() +x64-sse42 System.Int32 Examples.ManagedDispatch::Read(System.Int32&) +x64-sse42 System.Int32 Examples.MemorySpans::ArraySliceWrite(System.Int32[],System.Int32,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.MemorySpans::Existing(System.Int32&,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.MemorySpans::InPlaceArrayConstructor(System.Int32[],System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.MemorySpans::InPlaceArrayLength(System.Int32[],System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.MemorySpans::InPlacePointerSpan(System.Int32*,System.Int32) +x64-sse42 System.Int32 Examples.MemorySpans::Read(System.ReadOnlySpan`1,System.Int32) +x64-sse42 System.Int32 Examples.MemorySpans::ReferenceWrite(System.Span`1,System.Int32) +x64-sse42 System.Int32 Examples.MemorySpans::ReinitializeArraySpan(System.Int32[],System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.MemorySpans::SliceRead(System.ReadOnlySpan`1,System.Int32,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.MemorySpans::SliceWrite(System.Span`1,System.Int32,System.Int32,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.NarrowIntegers::Call(System.Int32) +x64-sse42 System.Int32 Examples.NarrowIntegers::Fields(System.Int32) +x64-sse42 System.Int32 Examples.NarrowIntegers::SignedByte(System.Int32) +x64-sse42 System.Int32 Examples.NarrowIntegers::SignedShort(System.Int64) +x64-sse42 System.Int32 Examples.NarrowIntegers::UnsignedShort(System.Int32) +x64-sse42 System.Int32 Examples.NativeIntegers::Narrow(System.IntPtr) +x64-sse42 System.Int32 Examples.NativeIntegers::SignedSize() +x64-sse42 System.Int32 Examples.NativeIntegers::UnsignedSize() +x64-sse42 System.Int32 Examples.NodeArrays::Count(Examples.Node[]) +x64-sse42 System.Int32 Examples.PooledItem::get_Id() +x64-sse42 System.Int32 Examples.PrimitiveArrays::AllocateAndWrite(System.Int32,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveArrays::AllocatePackedAndWrite(System.Int32,System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveArrays::Length(System.Int32[]) +x64-sse42 System.Int32 Examples.PrimitiveArrays::ReadAlias(System.Int32[],System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveArrays::ReadGeneric(System.Int32[],System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveArrays::ReadGenericHelper(System.Int32[],System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveArrays::ReadHelper(System.Int32[],System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveArrays::ReadInt32(System.Int32[],System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveArrays::WriteGenericHelper(System.Int32[],System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveOperations::Alias(System.Int16&,System.Int16&,System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveOperations::Choose(System.Int32) +x64-sse42 System.Int32 Examples.PrimitiveOperations::PacketSize() +x64-sse42 System.Int32 Examples.PrimitiveOperations::ReadWrite(System.Int32&,System.Int32) +x64-sse42 System.Int32 Examples.SampleArrays::Count(Examples.Samples) +x64-sse42 System.Int32 Examples.SampleArrays::CountAfterAllocation(Examples.Node,System.Int32[]) +x64-sse42 System.Int32 Examples.SampleArrays::CountAfterDeposit(Examples.Account,System.Int32[]) +x64-sse42 System.Int32 Examples.SampleArrays::CountAfterRefill(Examples.Samples,Examples.Samples,System.Int32[]) +x64-sse42 System.Int32 Examples.SampleArrays::ReadAfterAllocation(Examples.Node,System.Int32[]) +x64-sse42 System.Int32 Examples.SampleArrays::ReadAfterEcho(Examples.Account,System.Int32[],System.Int32) +x64-sse42 System.Int32 Examples.SampleArrays::RecordIndex(Examples.Account,System.Int32) +x64-sse42 System.Int32 Examples.SampleArrays::RefillRead(Examples.Samples,System.Int32[],System.Int32) +x64-sse42 System.Int32 Examples.SampleArrays::Share(Examples.Samples,Examples.Samples) +x64-sse42 System.Int32 Examples.SampleArrays::Sum(Examples.Samples,System.Int32[]) +x64-sse42 System.Int32 Examples.Sequential::PackedSize() +x64-sse42 System.Int32 Examples.Sequential::RecordSize() +x64-sse42 System.Int32 Examples.SpanConstructors::AggregateLocal(Examples.AggregateSpans/Words&) +x64-sse42 System.Int32 Examples.SpanConstructors::BytePointerLocal(System.Byte*,System.Int32) +x64-sse42 System.Int32 Examples.SpanConstructors::LayoutLocal(Examples.AggregateSpans/VectorRecord*,System.Int32) +x64-sse42 System.Int32 Examples.SpanConstructors::Local(System.Int32&) +x64-sse42 System.Int32 Examples.SpanHelpers::Count(System.ReadOnlySpan`1) +x64-sse42 System.Int32 Examples.SpanReferences::Argument(System.ReadOnlySpan`1) +x64-sse42 System.Int32 Examples.SpanReferences::Caller(System.Byte[]) +x64-sse42 System.Int32 Examples.SpanReferences::Copy(System.ReadOnlySpan`1&) +x64-sse42 System.Int32 Examples.SpanReferences::First(System.Span`1&) +x64-sse42 System.Int32 Examples.SpanReferences::Forward(System.ReadOnlySpan`1&) +x64-sse42 System.Int32 Examples.SpanReferences::Front(System.Span`1&) +x64-sse42 System.Int32 Examples.SpanReferences::GenericFront(System.Int32[]) +x64-sse42 System.Int32 Examples.SpanReferences::Length(System.ReadOnlySpan`1&) +x64-sse42 System.Int32 Examples.SpanReferences::Sum(System.ReadOnlySpan`1&) +x64-sse42 System.Int32 Examples.SpanReferences::Tail(System.ReadOnlySpan`1&) +x64-sse42 System.Int32 Examples.SpanReferences::Temporary(System.ReadOnlySpan`1) +x64-sse42 System.Int32 Examples.SpanTables::Count() +x64-sse42 System.Int32 Examples.StackAllocation::FillAndRead(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.StackAllocation::Length(System.Int32) +x64-sse42 System.Int32 Examples.StackAllocation::UninitializedRead() +x64-sse42 System.Int32 Examples.StackAllocation::WrittenBeforeRead(System.Int32) +x64-sse42 System.Int32 Examples.StackAllocation::ZeroInitialized() +x64-sse42 System.Int32 Examples.TailCalls::Excluded(System.Int32) +x64-sse42 System.Int32 Examples.TailCalls::Forward(System.Int32) +x64-sse42 System.Int32 Examples.TailCalls::ForwardReference(System.Int32&) +x64-sse42 System.Int32 Examples.TailCalls::ForwardThrow(System.Int32) +x64-sse42 System.Int32 Examples.TailCalls::Ignore(System.Int32&) +x64-sse42 System.Int32 Examples.TailCalls::Increment(System.Int32) +x64-sse42 System.Int32 Examples.TailCalls::LocalReference(System.Int32) +x64-sse42 System.Int32 Examples.TailCalls::Raise(System.Int32) +x64-sse42 System.Int32 Examples.TailCalls::Read(System.Int32&) +x64-sse42 System.Int32 Examples.TileArrays::PairCount(Examples.Tiles) +x64-sse42 System.Int32 Examples.TupleDivision::MathOutInt32(System.Int32,System.Int32,System.Int32&) +x64-sse42 System.Int32 Examples.TupleDivision::MathQuotient(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.TupleDivision::MathWrongQuotient(System.Int32,System.Int32) +x64-sse42 System.Int32 Examples.TypeTests::Classify(System.Int32) +x64-sse42 System.Int32 Examples.TypeTests::Classify(System.Int32) +x64-sse42 System.Int32 Examples.TypeTests::Marker(System.Int32) +x64-sse42 System.Int32 Examples.TypeTests::Other(System.Int32) +x64-sse42 System.Int32 Examples.TypeTests::ReferenceCases(System.Int32) +x64-sse42 System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +x64-sse42 System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +x64-sse42 System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size0() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size1() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size10() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size11() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size12() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size13() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size14() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size15() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size16() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size17() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size18() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size2() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size3() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size4() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size5() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size6() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size7() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size8() +x64-sse42 System.Int32 Examples.UnsafeLayouts::Size9() +x64-sse42 System.Int32 Examples.VectorFields::NestedSize() +x64-sse42 System.Int32 Examples.WordReader::Read(System.Int32[]) +x64-sse42 System.Int32 Examples.WordReader::ReadSaved(System.Int32[]) +x64-sse42 System.Int32 Examples.WordReader::SkipAndPeek(System.Int32[]) +x64-sse42 System.Int32& Examples.ArrayElementReferences::Element(System.Int32[],System.Int32) +x64-sse42 System.Int32& Examples.UnsafeLayouts::Add6int(System.Int32&,System.Int32) +x64-sse42 System.Int32& Examples.UnsafeLayouts::Add6nint(System.Int32&,System.IntPtr) +x64-sse42 System.Int32& Examples.UnsafeLayouts::Add6nuint(System.Int32&,System.UIntPtr) +x64-sse42 System.Int32& Examples.ViewAccessors::ArrayData(System.Int32[]) +x64-sse42 System.Int32* Examples.StackAllocation::EscapingStorage() +x64-sse42 System.Int32[] Examples.PrimitiveArrays::Allocate(System.Int32) +x64-sse42 System.Int32[] Examples.PrimitiveArrays::Identity(System.Int32[]) +x64-sse42 System.Int32[] Examples.SampleArrays::ValuesOf(Examples.Samples) +x64-sse42 System.Int64 Examples.Bits::PrimitiveCounts64(System.Int64) +x64-sse42 System.Int64 Examples.Bits::PrimitiveLeft64(System.Int64,System.Int32) +x64-sse42 System.Int64 Examples.Bits::PrimitiveRight64(System.Int64,System.Int32) +x64-sse42 System.Int64 Examples.Bits::ReverseI64(System.Int64) +x64-sse42 System.Int64 Examples.ExceptionDocumentation::CheckedAdd(System.Int64,System.Int64) +x64-sse42 System.Int64 Examples.ExceptionDocumentation::Divide(System.Int64,System.Int64) +x64-sse42 System.Int64 Examples.IntegerMath::AbsInt64(System.Int64) +x64-sse42 System.Int64 Examples.IntegerMath::ClampInt64(System.Int64,System.Int64,System.Int64) +x64-sse42 System.Int64 Examples.IntegerMath::MaxInt64(System.Int64,System.Int64) +x64-sse42 System.Int64 Examples.IntegerMath::MinInt64(System.Int64,System.Int64) +x64-sse42 System.Int64 Examples.IntegerMath::WrongMin(System.Int64,System.Int64) +x64-sse42 System.Int64 Examples.IntegerOperations::Negate64(System.Int64) +x64-sse42 System.Int64 Examples.OutlineCounters::SignedLiteral() +x64-sse42 System.Int64 Examples.PointerOperations::Difference(System.Byte*,System.Byte*) +x64-sse42 System.Int64 Examples.PooledItem::get_Payload() +x64-sse42 System.Int64 Examples.PrimitiveArrays::ReadInt64(System.Int64[],System.Int32) +x64-sse42 System.Int64 Examples.TupleDivision::MathOutInt64(System.Int64,System.Int64,System.Int64&) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure0int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure0nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure0nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure10int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure10nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure10nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure11int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure11nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure11nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure12int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure12nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure12nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure13int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure13nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure13nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure14int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure14nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure14nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure15int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure15nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure15nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure16int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure16nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure16nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure17int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure17nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure17nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure18int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure18nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure18nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure1int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure1nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure1nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure2int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure2nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure2nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure3int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure3nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure3nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure4int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure4nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure4nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure5int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure5nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure5nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure6int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure6nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure6nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure7int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure7nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure7nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure8int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure8nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure8nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure9int(System.Int32) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure9nint(System.IntPtr) +x64-sse42 System.Int64 Examples.UnsafeLayouts::Measure9nuint(System.UIntPtr) +x64-sse42 System.Int64 Examples.WideProduct::BigMul32(System.Int32,System.Int32) +x64-sse42 System.Int64 Examples.WideProduct::SignedBigMul(System.Int64,System.Int64,System.Int64&) +x64-sse42 System.Int64& Examples.UnsafeLayouts::Add8int(System.Int64&,System.Int32) +x64-sse42 System.Int64& Examples.UnsafeLayouts::Add8nint(System.Int64&,System.IntPtr) +x64-sse42 System.Int64& Examples.UnsafeLayouts::Add8nuint(System.Int64&,System.UIntPtr) +x64-sse42 System.Int64[] Examples.PrimitiveArrays::AllocateLongs(System.Int32) +x64-sse42 System.IntPtr Examples.Arguments::Native(System.IntPtr) +x64-sse42 System.IntPtr Examples.Bits::PrimitiveCountsNative(System.IntPtr) +x64-sse42 System.IntPtr Examples.Bits::PrimitiveLeftNative(System.IntPtr,System.Int32) +x64-sse42 System.IntPtr Examples.Bits::PrimitiveRightNative(System.IntPtr,System.Int32) +x64-sse42 System.IntPtr Examples.Bits::ReverseNative(System.IntPtr) +x64-sse42 System.IntPtr Examples.GenericMethods::Identity(System.IntPtr) +x64-sse42 System.IntPtr Examples.GenericMethods::NativeIdentity(System.IntPtr) +x64-sse42 System.IntPtr Examples.IntegerMath::AbsNative(System.IntPtr) +x64-sse42 System.IntPtr Examples.IntegerMath::ClampNative(System.IntPtr,System.IntPtr,System.IntPtr) +x64-sse42 System.IntPtr Examples.IntegerMath::MaxNative(System.IntPtr,System.IntPtr) +x64-sse42 System.IntPtr Examples.IntegerMath::MinNative(System.IntPtr,System.IntPtr) +x64-sse42 System.IntPtr Examples.MemorySpans::NativeRead(System.ReadOnlySpan`1,System.Int32) +x64-sse42 System.IntPtr Examples.NativeIntegers::Add(System.IntPtr,System.IntPtr) +x64-sse42 System.IntPtr Examples.NativeIntegers::CheckedAdd(System.IntPtr,System.IntPtr) +x64-sse42 System.IntPtr Examples.NativeIntegers::Choose(System.IntPtr) +x64-sse42 System.IntPtr Examples.NativeIntegers::Divide(System.IntPtr,System.IntPtr) +x64-sse42 System.IntPtr Examples.NativeIntegers::Fields(Examples.NativePair&,System.IntPtr,System.UIntPtr) +x64-sse42 System.IntPtr Examples.NativeIntegers::Increment(System.IntPtr&) +x64-sse42 System.IntPtr Examples.NativeIntegers::LocalCall(System.IntPtr) +x64-sse42 System.IntPtr Examples.NativeIntegers::ReadWrite(System.IntPtr&,System.IntPtr) +x64-sse42 System.IntPtr Examples.NativeIntegers::Widen(System.Int32) +x64-sse42 System.IntPtr Examples.PrimitiveArrays::ReadIntPtr(System.IntPtr[],System.Int32) +x64-sse42 System.IntPtr Examples.UnsafeReferences::ByteOffset(System.Byte&,System.Byte&) +x64-sse42 System.IntPtr& Examples.UnsafeLayouts::Add10int(System.IntPtr&,System.Int32) +x64-sse42 System.IntPtr& Examples.UnsafeLayouts::Add10nint(System.IntPtr&,System.IntPtr) +x64-sse42 System.IntPtr& Examples.UnsafeLayouts::Add10nuint(System.IntPtr&,System.UIntPtr) +x64-sse42 System.ReadOnlySpan`1 Examples.AggregateArrays::ReadOnlyView(Examples.AggregateArrays/Pair[]) +x64-sse42 System.ReadOnlySpan`1 Examples.AggregateSpans::ReadOnlyFactory(Examples.AggregateSpans/Words&,System.Int32) +x64-sse42 System.ReadOnlySpan`1 Examples.AggregateSpans::WordsView(System.ReadOnlySpan`1) +x64-sse42 System.ReadOnlySpan`1 Examples.SpanConstructors::PassReadOnly(System.ReadOnlySpan`1) +x64-sse42 System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyDefaultReset(Examples.AggregateSpans/Words&) +x64-sse42 System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyEmpty() +x64-sse42 System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyBytes(System.ReadOnlySpan`1) +x64-sse42 System.ReadOnlySpan`1 Examples.StaticSpans::get_Lookup() +x64-sse42 System.ReadOnlySpan`1 Examples.MemorySpans::ArrayRange(System.Int32[],System.Int32,System.Int32) +x64-sse42 System.ReadOnlySpan`1 Examples.MemorySpans::ArrayReadOnly(System.Int32[]) +x64-sse42 System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlySingle(System.Int32&) +x64-sse42 System.ReadOnlySpan`1> Examples.SpanConstructors::Vector(System.Runtime.Intrinsics.Vector128`1&) +x64-sse42 System.ReadOnlySpan`1> Examples.SpanConstructors::VectorPointer(System.Runtime.Intrinsics.Vector128`1*,System.Int32) +x64-sse42 System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyWords(System.ReadOnlySpan`1) +x64-sse42 System.ReadOnlySpan`1 Examples.SpanTables::get_Words() +x64-sse42 System.ReadOnlySpan`1 Examples.SpanTables::get_Powers() +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::Shuffle128(System.ReadOnlySpan`1,System.ReadOnlySpan`1) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.MarshaledMemory::ReadVector(System.ReadOnlySpan`1) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::Read128(System.Byte&) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::ReadAligned128(System.Byte*) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128Byte(Examples.ValueBitCasts/Pair) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadBytes128(Examples.VectorFields/Bytes128) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadInts128(Examples.VectorFields/Ints128) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt32(Examples.ValueBitCasts/Pair) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.AggregateArrays::ReadVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.AggregateSpans::ReadVector(System.ReadOnlySpan`1>) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::MultiplyEven128(System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::UnsafeReturn(System.UInt64) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Identity>(System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Vector(System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.UnsafeReferences::ReadHalf(Examples.FourLimbs&) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt64(Examples.ValueBitCasts/Pair) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Read128(Examples.VectorFields/Holder128) +x64-sse42 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Write128(Examples.VectorFields/Holder128&,System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17int(System.Runtime.Intrinsics.Vector128`1&,System.Int32) +x64-sse42 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nint(System.Runtime.Intrinsics.Vector128`1&,System.IntPtr) +x64-sse42 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nuint(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +x64-sse42 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::Half(Examples.FourLimbs&) +x64-sse42 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::SubtractVector(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int32(Examples.ValueBitCasts/Limbs) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int64(Examples.ValueBitCasts/Limbs) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadLongs256(Examples.VectorFields/Longs256) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::CreateWords(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt32(Examples.ValueBitCasts/Limbs) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadUInts256(Examples.VectorFields/UInts256) +x64-sse42 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeReferences::Vector(Examples.FourLimbs&) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::CopyLocal(Examples.ValueBitCasts/Limbs) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::ReadLocal(System.Runtime.Intrinsics.Vector256`1&) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt64(Examples.ValueBitCasts/Limbs) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Read256(Examples.VectorFields/Holder256) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Reinterpret(Examples.VectorFields/Words256&,System.Runtime.Intrinsics.Vector256`1) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Write256(Examples.VectorFields/Holder256&,System.Runtime.Intrinsics.Vector256`1) +x64-sse42 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::WritePacked(Examples.VectorFields/Packed256&,System.Runtime.Intrinsics.Vector256`1) +x64-sse42 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18int(System.Runtime.Intrinsics.Vector256`1&,System.Int32) +x64-sse42 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nint(System.Runtime.Intrinsics.Vector256`1&,System.IntPtr) +x64-sse42 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nuint(System.Runtime.Intrinsics.Vector256`1&,System.UIntPtr) +x64-sse42 System.Runtime.Intrinsics.Vector256`1& Examples.VectorFields::Address256(Examples.VectorFields/Holder256&) +x64-sse42 System.Runtime.Intrinsics.Vector256`1[] Examples.PrimitiveArrays::AllocateVector(System.Int32) +x64-sse42 System.Runtime.Intrinsics.Vector512`1 Examples.UnalignedMemory::Read512(System.Byte&) +x64-sse42 System.SByte Examples.Bits::ReverseI8(System.SByte) +x64-sse42 System.SByte Examples.IntegerMath::AbsInt8(System.SByte) +x64-sse42 System.SByte Examples.IntegerMath::ClampInt8(System.SByte,System.SByte,System.SByte) +x64-sse42 System.SByte Examples.IntegerMath::MaxInt8(System.SByte,System.SByte) +x64-sse42 System.SByte Examples.IntegerMath::MinInt8(System.SByte,System.SByte) +x64-sse42 System.SByte Examples.PrimitiveArrays::ReadSByte(System.SByte[],System.Int32) +x64-sse42 System.SByte Examples.TupleDivision::MathSByteRemainder(System.SByte,System.SByte) +x64-sse42 System.SByte Examples.UnalignedMemory::ReadSigned(System.Byte&) +x64-sse42 System.SByte& Examples.UnsafeLayouts::Add2int(System.SByte&,System.Int32) +x64-sse42 System.SByte& Examples.UnsafeLayouts::Add2nint(System.SByte&,System.IntPtr) +x64-sse42 System.SByte& Examples.UnsafeLayouts::Add2nuint(System.SByte&,System.UIntPtr) +x64-sse42 System.Span`1 Examples.AggregateArrays::Slice(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +x64-sse42 System.Span`1 Examples.AggregateArrays::View(Examples.AggregateArrays/Pair[]) +x64-sse42 System.Span`1 Examples.AggregateSpans::Factory(Examples.AggregateSpans/Words&,System.Int32) +x64-sse42 System.Span`1 Examples.AggregateSpans::Slice(System.Span`1,System.Int32,System.Int32) +x64-sse42 System.Span`1 Examples.SpanConstructors::Aggregate(Examples.AggregateSpans/Words&) +x64-sse42 System.Span`1 Examples.SpanConstructors::Pointer(Examples.AggregateSpans/Words*,System.Int32) +x64-sse42 System.Span`1 Examples.EnumBuffers::Slice(System.Span`1,System.Int32) +x64-sse42 System.Span`1 Examples.ReinterpretedSpans::EnumWords(System.Span`1) +x64-sse42 System.Span`1 Examples.MemorySpans::ArrayAsSpan(System.Boolean[]) +x64-sse42 System.Span`1 Examples.AggregateSpans::Bytes(System.Span`1) +x64-sse42 System.Span`1 Examples.AggregateSpans::LayoutBytes(System.Span`1) +x64-sse42 System.Span`1 Examples.AggregateSpans::VectorBytes(System.Span`1>) +x64-sse42 System.Span`1 Examples.ReinterpretedSpans::Bytes(System.Span`1) +x64-sse42 System.Span`1 Examples.ReinterpretedSpans::EnumBytes(System.Span`1) +x64-sse42 System.Span`1 Examples.ReinterpretedSpans::Expanded(System.Span`1) +x64-sse42 System.Span`1 Examples.MemorySpans::ArrayConstructor(System.Char[]) +x64-sse42 System.Span`1 Examples.MemorySpans::ArrayWritable(System.Int32[]) +x64-sse42 System.Span`1 Examples.ReinterpretedSpans::SameWidth(System.Span`1) +x64-sse42 System.Span`1 Examples.SpanConstructors::DefaultReset(System.Int32&) +x64-sse42 System.Span`1 Examples.SpanConstructors::Empty() +x64-sse42 System.Span`1 Examples.SpanConstructors::Pass(System.Span`1) +x64-sse42 System.Span`1 Examples.SpanConstructors::Single(System.Int32&) +x64-sse42 System.Span`1 Examples.MemorySpans::ArrayAsSpanStart(System.IntPtr[],System.Int32) +x64-sse42 System.Span`1> Examples.AggregateArrays::VectorView(System.Runtime.Intrinsics.Vector128`1[]) +x64-sse42 System.Span`1 Examples.ReinterpretedSpans::Words(System.Span`1) +x64-sse42 System.Span`1 Examples.MemorySpans::ArrayAsSpanRange(System.UIntPtr[],System.Int32,System.Int32) +x64-sse42 System.String Examples.Guards::Required(System.String) +x64-sse42 System.String Examples.Strings::OrNone(System.String) +x64-sse42 System.String Examples.Strings::Pick(System.Boolean,System.String,System.String) +x64-sse42 System.String Examples.Strings::Relabel(Examples.Labelled,System.String) +x64-sse42 System.String[] Examples.TagArrays::Create(System.Int32) +x64-sse42 System.UInt16 Examples.Bits::ReverseU16(System.UInt16) +x64-sse42 System.UInt16 Examples.IntegerMath::ClampUInt16(System.UInt16,System.UInt16,System.UInt16) +x64-sse42 System.UInt16 Examples.IntegerMath::MaxUInt16(System.UInt16,System.UInt16) +x64-sse42 System.UInt16 Examples.IntegerMath::MinUInt16(System.UInt16,System.UInt16) +x64-sse42 System.UInt16 Examples.PrimitiveArrays::ReadUInt16(System.UInt16[],System.Int32) +x64-sse42 System.UInt16& Examples.UnsafeLayouts::Add5int(System.UInt16&,System.Int32) +x64-sse42 System.UInt16& Examples.UnsafeLayouts::Add5nint(System.UInt16&,System.IntPtr) +x64-sse42 System.UInt16& Examples.UnsafeLayouts::Add5nuint(System.UInt16&,System.UIntPtr) +x64-sse42 System.UInt32 Examples.Arguments::Address(System.UInt32) +x64-sse42 System.UInt32 Examples.Arguments::Alias(System.UInt32) +x64-sse42 System.UInt32 Examples.Arguments::CallerIsolation(System.UInt32) +x64-sse42 System.UInt32 Examples.Arguments::Overwrite(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.Arguments::ReassignReference(System.UInt32&,System.UInt32&) +x64-sse42 System.UInt32 Examples.Arithmetic::AtMostFive(System.UInt32) +x64-sse42 System.UInt32 Examples.Bits::Left32(System.UInt32,System.Int32) +x64-sse42 System.UInt32 Examples.Bits::PrimitiveLeadingU32(System.UInt32) +x64-sse42 System.UInt32 Examples.Bits::PrimitiveLeftU32(System.UInt32,System.Int32) +x64-sse42 System.UInt32 Examples.Bits::PrimitiveLogU32(System.UInt32) +x64-sse42 System.UInt32 Examples.Bits::PrimitivePopulationU32(System.UInt32) +x64-sse42 System.UInt32 Examples.Bits::PrimitiveRightU32(System.UInt32,System.Int32) +x64-sse42 System.UInt32 Examples.Bits::PrimitiveTrailingU32(System.UInt32) +x64-sse42 System.UInt32 Examples.Bits::ReverseU32(System.UInt32) +x64-sse42 System.UInt32 Examples.Bits::Right32(System.UInt32,System.Int32) +x64-sse42 System.UInt32 Examples.Bits::RoundUp32(System.UInt32) +x64-sse42 System.UInt32 Examples.Capabilities::AesFlags() +x64-sse42 System.UInt32 Examples.Capabilities::ArmFlags() +x64-sse42 System.UInt32 Examples.Capabilities::Flags() +x64-sse42 System.UInt32 Examples.Capabilities::VbmiFlags() +x64-sse42 System.UInt32 Examples.CheckedArithmetic::Add(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.CheckedConversions::Narrow(System.UInt64) +x64-sse42 System.UInt32 Examples.Division::Quotient(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.ExceptionDocumentation::Profile(System.UInt32) +x64-sse42 System.UInt32 Examples.ExceptionDocumentation::SpanItem(System.Int32) +x64-sse42 System.UInt32 Examples.GenericMethods::ClearPacked(System.UInt32) +x64-sse42 System.UInt32 Examples.GenericMethods::SwapPacked(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.GenericOwners::Address(System.UInt32) +x64-sse42 System.UInt32 Examples.GenericOwners::Combined(System.UInt32) +x64-sse42 System.UInt32 Examples.GenericOwners::Identity(System.UInt32) +x64-sse42 System.UInt32 Examples.GenericOwners::Nested(System.UInt32) +x64-sse42 System.UInt32 Examples.GenericOwners::NestedHelper(System.UInt32) +x64-sse42 System.UInt32 Examples.GenericOwners::Repeated(System.UInt32) +x64-sse42 System.UInt32 Examples.Guards::Index(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.IntegerMath::ClampUInt32(System.UInt32,System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.IntegerMath::MaxUInt32(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.IntegerMath::MinUInt32(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::AddTwo(System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::Bump(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::CopyUntilZero(System.ReadOnlySpan`1,System.UInt32&) +x64-sse42 System.UInt32 Examples.Loops::CountCalls(System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::CountUntil(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::CountUpToFive(System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::Countdown(System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::Decrement(System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::DownThenUp(System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::FeatureCountdown(System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::InnerCount(Examples.Loops/Outer,System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::ReachFive(System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::TallyTwice(System.UInt32) +x64-sse42 System.UInt32 Examples.Loops::WrongCountdown(System.UInt32) +x64-sse42 System.UInt32 Examples.NarrowTotals::ReadSum() +x64-sse42 System.UInt32 Examples.NarrowTotals::WriteRead(System.UInt32) +x64-sse42 System.UInt32 Examples.NativeIntegers::NarrowUnsigned(System.UIntPtr) +x64-sse42 System.UInt32 Examples.Nested::Packed(System.UInt32) +x64-sse42 System.UInt32 Examples.OwnerHelper`1::Identity(System.UInt32) +x64-sse42 System.UInt32 Examples.OwnerHelper`1::Select(System.UInt32,System.UInt64) +x64-sse42 System.UInt32 Examples.OwnerOuter`1/Helper::Identity(System.UInt32) +x64-sse42 System.UInt32 Examples.OwnerOuter`1/Inner`1::Identity(System.UInt32) +x64-sse42 System.UInt32 Examples.PrimitiveArrays::ReadUInt32(System.UInt32[],System.Int32) +x64-sse42 System.UInt32 Examples.ReinterpretedSpans::Aliased(System.Span`1) +x64-sse42 System.UInt32 Examples.Sequential::ReadWrite(Examples.Record&,System.UInt32) +x64-sse42 System.UInt32 Examples.SpanTables::Word(System.Int32) +x64-sse42 System.UInt32 Examples.Throws::ActualValue(System.UInt32) +x64-sse42 System.UInt32 Examples.Throws::ArgumentInner(System.UInt32) +x64-sse42 System.UInt32 Examples.Throws::Inner(System.UInt32) +x64-sse42 System.UInt32 Examples.Throws::Null(System.UInt32) +x64-sse42 System.UInt32 Examples.Throws::Percent(System.UInt32) +x64-sse42 System.UInt32 Examples.Throws::Quotient(System.UInt32,System.UInt32) +x64-sse42 System.UInt32 Examples.WordReader::RangeEnd() +x64-sse42 System.UInt32 Examples.WordReader::ReplaceRemaining(System.UInt32) +x64-sse42 System.UInt32 Examples.WordReader::Reserve() +x64-sse42 System.UInt32 SelectorExamples.One.Collision::Add(System.UInt32,System.UInt32) +x64-sse42 System.UInt32& Examples.UnsafeLayouts::Add7int(System.UInt32&,System.Int32) +x64-sse42 System.UInt32& Examples.UnsafeLayouts::Add7nint(System.UInt32&,System.IntPtr) +x64-sse42 System.UInt32& Examples.UnsafeLayouts::Add7nuint(System.UInt32&,System.UIntPtr) +x64-sse42 System.UInt64 Examples.Account::get_Headroom() +x64-sse42 System.UInt64 Examples.AddStep::Apply(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.Aliases::Update(System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.Animal::Loudness() +x64-sse42 System.UInt64 Examples.Arguments::Struct(Examples.Packet) +x64-sse42 System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.Arithmetic::Alias(System.UInt64&,System.UInt64&) +x64-sse42 System.UInt64 Examples.Arithmetic::Calls(System.UInt64) +x64-sse42 System.UInt64 Examples.Arithmetic::Choose(System.Boolean,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.Arithmetic::Distance(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.Arithmetic::Increment(System.UInt64) +x64-sse42 System.UInt64 Examples.Arithmetic::LargeStruct(System.UInt64) +x64-sse42 System.UInt64 Examples.Arithmetic::PastEnd(System.UInt64&) +x64-sse42 System.UInt64 Examples.Arithmetic::Read(Examples.Packet&) +x64-sse42 System.UInt64 Examples.Arithmetic::ReadLarge(Examples.LargePacket&) +x64-sse42 System.UInt64 Examples.Arithmetic::ReadValue(Examples.Packet) +x64-sse42 System.UInt64 Examples.Arithmetic::Struct(System.UInt64) +x64-sse42 System.UInt64 Examples.Arithmetic::StructCopy(System.UInt64) +x64-sse42 System.UInt64 Examples.Arithmetic::TypeCheck(System.UInt64) +x64-sse42 System.UInt64 Examples.Arithmetic::WrongAdd(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.BinarySpans::ReadBig(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.BinarySpans::ReadLittle(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.BinarySpans::TryReadBig(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.BinarySpans::TryReadLittle(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.Bird::Legs() +x64-sse42 System.UInt64 Examples.Bits::Left64(System.UInt64,System.Int32) +x64-sse42 System.UInt64 Examples.Bits::PrimitiveCountsU64(System.UInt64) +x64-sse42 System.UInt64 Examples.Bits::PrimitiveLeftU64(System.UInt64,System.Int32) +x64-sse42 System.UInt64 Examples.Bits::PrimitiveRightU64(System.UInt64,System.Int32) +x64-sse42 System.UInt64 Examples.Bits::ReverseU64(System.UInt64) +x64-sse42 System.UInt64 Examples.Bits::Right64(System.UInt64,System.Int32) +x64-sse42 System.UInt64 Examples.Bits::RoundTrip(System.UInt64) +x64-sse42 System.UInt64 Examples.Bits::RoundUp64(System.UInt64) +x64-sse42 System.UInt64 Examples.Bits::WrongRoundTrip(System.UInt64) +x64-sse42 System.UInt64 Examples.Blocks::CopyLocal(System.UInt64,System.UInt32) +x64-sse42 System.UInt64 Examples.Blocks::CopyParameter(System.UInt64,System.UInt64,System.UInt32) +x64-sse42 System.UInt64 Examples.Blocks::FillLocal(System.Byte,System.UInt32) +x64-sse42 System.UInt64 Examples.Blocks::FillParameter(System.UInt64,System.Byte,System.UInt32) +x64-sse42 System.UInt64 Examples.CheckedArithmetic::Add64(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.ComputedFlags::TraceBoth(System.UInt64) +x64-sse42 System.UInt64 Examples.ComputedFlags::TraceChained(System.UInt64) +x64-sse42 System.UInt64 Examples.ComputedFlags::TraceGated(System.UInt64) +x64-sse42 System.UInt64 Examples.ComputedFlags::TraceNotOff(System.UInt64) +x64-sse42 System.UInt64 Examples.ComputedFlags::TraceSilent(System.UInt64) +x64-sse42 System.UInt64 Examples.ConstantFlags::Count(System.UInt64) +x64-sse42 System.UInt64 Examples.ConstantFlags::CountOn(System.UInt64) +x64-sse42 System.UInt64 Examples.ConstantFlags::Trace(System.UInt64) +x64-sse42 System.UInt64 Examples.ConstantFlags::TraceOff(System.UInt64) +x64-sse42 System.UInt64 Examples.ConstantFlags::TraceThenCount(System.UInt64) +x64-sse42 System.UInt64 Examples.Constructors::Checked(System.UInt64) +x64-sse42 System.UInt64 Examples.Constructors::Default() +x64-sse42 System.UInt64 Examples.Constructors::Nested(System.UInt64) +x64-sse42 System.UInt64 Examples.Constructors::Packed(System.UInt64) +x64-sse42 System.UInt64 Examples.Constructors::Read(System.UInt64) +x64-sse42 System.UInt64 Examples.Counter::Add(System.UInt64) +x64-sse42 System.UInt64 Examples.Counter::Mix(System.UInt64,System.UInt32,System.UInt16,System.Byte) +x64-sse42 System.UInt64 Examples.Counter::Read() +x64-sse42 System.UInt64 Examples.Counter::WriteRead(System.UInt64) +x64-sse42 System.UInt64 Examples.Counter::WrongWriteRead(System.UInt64) +x64-sse42 System.UInt64 Examples.DerivedFlags::Count(System.UInt64) +x64-sse42 System.UInt64 Examples.DerivedFlags::CountLoud(System.UInt64) +x64-sse42 System.UInt64 Examples.DerivedFlags::Trace(System.UInt64) +x64-sse42 System.UInt64 Examples.DerivedFlags::TraceQuiet(System.UInt64) +x64-sse42 System.UInt64 Examples.Division::Quotient64(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.DivisionIntrinsics::DefinedLocal(System.UInt64) +x64-sse42 System.UInt64 Examples.DivisionIntrinsics::PrunedUnsafeLocal(System.UInt64) +x64-sse42 System.UInt64 Examples.DivisionIntrinsics::ReadLow(System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.UInt64 Examples.DivisionIntrinsics::UnsafeCall(System.UInt64) +x64-sse42 System.UInt64 Examples.DivisionIntrinsics::UnsafeHigh(System.UInt64) +x64-sse42 System.UInt64 Examples.DivisionIntrinsics::UnsafeLow(System.UInt64) +x64-sse42 System.UInt64 Examples.DivisionIntrinsics::UnsafeLowNext(System.UInt64) +x64-sse42 System.UInt64 Examples.Dog::Legs() +x64-sse42 System.UInt64 Examples.Dog::Loudness() +x64-sse42 System.UInt64 Examples.ExceptionDocumentation::DivRem(System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.ExceptionDocumentation::UnsignedDivide(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.GenericEnvelope`1::Read() +x64-sse42 System.UInt64 Examples.GenericLayouts::Composed(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.GenericLayouts::Field(Examples.GenericPair`1&) +x64-sse42 System.UInt64 Examples.GenericLayouts::Roundtrip(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.GenericMethods::Choose(System.Boolean,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.GenericMethods::First(System.UInt64,System.UInt32) +x64-sse42 System.UInt64 Examples.GenericMethods::Forward(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericMethods::Identity64(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericMethods::Identity(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericMethods::Instance(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericMethods::Nested(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericMethods::Repeated(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericMethods::Select(System.Boolean,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.GenericMethods::TwoTypes(System.UInt64,System.UInt32) +x64-sse42 System.UInt64 Examples.GenericOwners::NestedSpanFirst(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.GenericOwners::OwnerSpanFirst(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.GenericOwners::SpanFirst(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.GenericOwners::SpanSecond(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.GenericPair`1::GetSecond() +x64-sse42 System.UInt64 Examples.GenericPicker::Pick(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::Count>(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::CountWide(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::Pad,System.UInt32>(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::Pad,System.UInt64>(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::PadNarrow(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::PadWide(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::Trace>(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::TraceNarrow(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::Width,System.UInt32>(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::Width,System.UInt64>(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::WidthNarrow(System.UInt64) +x64-sse42 System.UInt64 Examples.GenericReceivers::WidthWide(System.UInt64) +x64-sse42 System.UInt64 Examples.IMeasureFive::Examples.IMeasure.Measure(System.UInt64) +x64-sse42 System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +x64-sse42 System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +x64-sse42 System.UInt64 Examples.IScale::Scale(System.UInt64) +x64-sse42 System.UInt64 Examples.IScaleMore::Examples.IScale.Scale(System.UInt64) +x64-sse42 System.UInt64 Examples.IScaleMost::Examples.IScale.Scale(System.UInt64) +x64-sse42 System.UInt64 Examples.IScaleSide::Examples.IScale.Scale(System.UInt64) +x64-sse42 System.UInt64 Examples.InParameters::Constrained(Examples.InWord&,Examples.InWord&) +x64-sse42 System.UInt64 Examples.InParameters::ConstrainedSum(Examples.InWord&,System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.InParameters::Sum(Examples.InWord&,System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.InWord::Examples.IInWord.Sum(System.ReadOnlySpan`1& modreq(System.Runtime.InteropServices.InAttribute)) +x64-sse42 System.UInt64 Examples.InitCounter::get_Value() +x64-sse42 System.UInt64 Examples.IntegerMath::ClampUInt64(System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.IntegerMath::MaxUInt64(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.IntegerMath::MinUInt64(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.Loops::Sum(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.Loops::SumIndexed(System.UInt64[]) +x64-sse42 System.UInt64 Examples.Loops::SumThroughLocal(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.Loops::SumThroughSpan(System.UInt64[]) +x64-sse42 System.UInt64 Examples.Loops::TwoStructs(System.UInt32) +x64-sse42 System.UInt64 Examples.MarshaledMemory::Read(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.Measured`1::Width(System.UInt64) +x64-sse42 System.UInt64 Examples.Measured`1::Width(System.UInt64) +x64-sse42 System.UInt64 Examples.MostSpecificBodies::Five(System.UInt64) +x64-sse42 System.UInt64 Examples.MostSpecificBodies::Measured(System.UInt64) +x64-sse42 System.UInt64 Examples.MostSpecificBodies::More(System.UInt64) +x64-sse42 System.UInt64 Examples.MostSpecificBodies::Most(System.UInt64) +x64-sse42 System.UInt64 Examples.MostSpecificBodies::Own(System.UInt64) +x64-sse42 System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +x64-sse42 System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +x64-sse42 System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +x64-sse42 System.UInt64 Examples.Nested::Alias(System.UInt64) +x64-sse42 System.UInt64 Examples.Nested::Copy(System.UInt64) +x64-sse42 System.UInt64 Examples.Nested::Explicit(System.UInt64) +x64-sse42 System.UInt64 Examples.Nested::Pass(System.UInt64) +x64-sse42 System.UInt64 Examples.Nested::Read(Examples.Leaf) +x64-sse42 System.UInt64 Examples.Nested::ReadOuter(Examples.NestedRecord) +x64-sse42 System.UInt64 Examples.Nested::Sized(System.UInt64) +x64-sse42 System.UInt64 Examples.Nested::Snapshot(System.UInt64) +x64-sse42 System.UInt64 Examples.Nested::WrongCopy(System.UInt64) +x64-sse42 System.UInt64 Examples.NestedConstructor::Sum() +x64-sse42 System.UInt64 Examples.NullReferences::Optional(Examples.FourLimbs&,System.Boolean) +x64-sse42 System.UInt64 Examples.NullReferences::Read() +x64-sse42 System.UInt64 Examples.NullReferences::ReadField() +x64-sse42 System.UInt64 Examples.NullReferences::Unchecked(Examples.FourLimbs&,System.Boolean) +x64-sse42 System.UInt64 Examples.Objects::BalanceOf(Examples.Account) +x64-sse42 System.UInt64 Examples.Objects::Describe(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.Objects::Fresh(System.UInt32) +x64-sse42 System.UInt64 Examples.Objects::IdOf(Examples.Shape) +x64-sse42 System.UInt64 Examples.Objects::LinkedValue(Examples.Node,Examples.Node) +x64-sse42 System.UInt64 Examples.Objects::Replace(Examples.Account,System.UInt64) +x64-sse42 System.UInt64 Examples.Objects::SecondValue(Examples.Node) +x64-sse42 System.UInt64 Examples.Objects::UnderFive(Examples.Account) +x64-sse42 System.UInt64 Examples.Objects::Withdraw(Examples.Account,System.UInt64) +x64-sse42 System.UInt64 Examples.Odometer::Next() +x64-sse42 System.UInt64 Examples.OutlineCountdown::Drain() +x64-sse42 System.UInt64 Examples.OutlineCountdown::DrainAtLeastOnce() +x64-sse42 System.UInt64 Examples.OutlineCounters::ExchangeAdd(System.UInt64) +x64-sse42 System.UInt64 Examples.OutlineCounters::UnsignedLiteral() +x64-sse42 System.UInt64 Examples.OutlineCounters::WrongExchangeAdd(System.UInt64) +x64-sse42 System.UInt64 Examples.OwnerHelper`1::First(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.OwnerOuter`1/Inner`1::First(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.PackedConstructor::Sum() +x64-sse42 System.UInt64 Examples.PointerOperations::WriteEnum(Examples.Unsigned64*,System.UInt64) +x64-sse42 System.UInt64 Examples.PrimitiveArrays::ReadUInt64(System.UInt64[],System.Int32) +x64-sse42 System.UInt64 Examples.ScaleOwn::Scale(System.UInt64) +x64-sse42 System.UInt64 Examples.Sequential::Packed(System.UInt64) +x64-sse42 System.UInt64 Examples.SkipInitialization::Read(System.UInt64&) +x64-sse42 System.UInt64 Examples.SpanHelpers::First(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanHelpers::Second(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.SpanReferences::Back(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanReferences::BigEndianWord(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanReferences::ByteSecond(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanReferences::Construct(System.ReadOnlySpan`1) +x64-sse42 System.UInt64 Examples.SpanReferences::CursorSum(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanReferences::Element(System.ReadOnlySpan`1&,System.Int32) +x64-sse42 System.UInt64 Examples.SpanReferences::FirstHigh(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanReferences::GenericHead(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanReferences::Head(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanReferences::High(System.Span`1&,System.Int32) +x64-sse42 System.UInt64 Examples.SpanReferences::HighAddress(System.ReadOnlySpan`1&,System.Int32) +x64-sse42 System.UInt64 Examples.SpanReferences::ReadOnlyHigh(System.ReadOnlySpan`1&,System.Int32) +x64-sse42 System.UInt64 Examples.SpanReferences::Second(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanReferences::Word(System.ReadOnlySpan`1&) +x64-sse42 System.UInt64 Examples.SpanTables::First() +x64-sse42 System.UInt64 Examples.SpanTables::Power(System.Int32) +x64-sse42 System.UInt64 Examples.StaticInterfaces::AddCounted(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.StaticInterfaces::XorPlain(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.StaticValues::Read(Examples.Word256&) +x64-sse42 System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.Allowance&) +x64-sse42 System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.DoublingAllowance&) +x64-sse42 System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.RefundingAllowance&) +x64-sse42 System.UInt64 Examples.StaticVirtualDefaults::SpendDefault(Examples.Allowance&) +x64-sse42 System.UInt64 Examples.StaticVirtualDefaults::SpendOverride(Examples.DoublingAllowance&) +x64-sse42 System.UInt64 Examples.StaticVirtualDefaults::SpendRefunding(Examples.RefundingAllowance&) +x64-sse42 System.UInt64 Examples.StructMethods::Arguments(System.UInt64) +x64-sse42 System.UInt64 Examples.StructMethods::ByValue(System.UInt64) +x64-sse42 System.UInt64 Examples.StructMethods::ConstructLocal(System.UInt64) +x64-sse42 System.UInt64 Examples.StructMethods::Initialized(System.UInt64) +x64-sse42 System.UInt64 Examples.StructMethods::Mutate(System.UInt64) +x64-sse42 System.UInt64 Examples.StructMethods::Replace(System.UInt64) +x64-sse42 System.UInt64 Examples.TriplePrice::get_Price() +x64-sse42 System.UInt64 Examples.TupleDivision::Deconstruct(System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.TupleDivision::Item1(System.ValueTuple`2&) +x64-sse42 System.UInt64 Examples.TupleDivision::Local(System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.TupleDivision::Quotient(System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.TupleDivision::Remainder(System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.TupleDivision::WrongQuotient(System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.UnalignedMemory::ReadAlignedWord(System.Byte*) +x64-sse42 System.UInt64 Examples.UnalignedMemory::ReadPointerWord(System.Byte*) +x64-sse42 System.UInt64 Examples.UnalignedMemory::ReadWord(System.Byte&) +x64-sse42 System.UInt64 Examples.UnsafeReferences::PastEnd(Examples.FourLimbs&) +x64-sse42 System.UInt64 Examples.UnsafeReferences::Read(Examples.FourLimbs&) +x64-sse42 System.UInt64 Examples.UnsafeReferences::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.UInt64 Examples.VirtualCalls::DogLegs() +x64-sse42 System.UInt64 Examples.VirtualCalls::LegsOf(Examples.Animal) +x64-sse42 System.UInt64 Examples.VirtualCalls::LoudnessOf(Examples.Animal) +x64-sse42 System.UInt64 Examples.WideProduct::BigMul(System.UInt64,System.UInt64,System.UInt64&) +x64-sse42 System.UInt64 Examples.WideProduct::UnsignedBigMul32(System.UInt32,System.UInt32) +x64-sse42 System.UInt64 Examples.WordReader::ReplaceRemainingWide(System.UInt32) +x64-sse42 System.UInt64 Examples.XorStep::Examples.IBinaryStep.Apply(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 SelectorExamples.One.Collision::Add(System.UInt64,System.UInt64) +x64-sse42 System.UInt64 SelectorExamples.Two.Collision::Add(System.UInt64,System.UInt64) +x64-sse42 System.UInt64& Examples.Arithmetic::Escape() +x64-sse42 System.UInt64& Examples.UnsafeLayouts::Add9int(System.UInt64&,System.Int32) +x64-sse42 System.UInt64& Examples.UnsafeLayouts::Add9nint(System.UInt64&,System.IntPtr) +x64-sse42 System.UInt64& Examples.UnsafeLayouts::Add9nuint(System.UInt64&,System.UIntPtr) +x64-sse42 System.UInt64& Examples.UnsafeReferences::Pointer(System.Byte*) +x64-sse42 System.UInt64& Examples.UnsafeReferences::SubtractElements32(System.UInt64&,System.Int32) +x64-sse42 System.UInt64& Examples.UnsafeReferences::SubtractElementsNative(System.UInt64&,System.IntPtr) +x64-sse42 System.UInt64& Examples.UnsafeReferences::SubtractElementsUnsigned(System.UInt64&,System.UIntPtr) +x64-sse42 System.UInt64& Examples.UnsafeReferences::Word(Examples.FourLimbs&) +x64-sse42 System.UIntPtr Examples.Bits::LeftNative(System.UIntPtr,System.Int32) +x64-sse42 System.UIntPtr Examples.Bits::PrimitiveCountsUNative(System.UIntPtr) +x64-sse42 System.UIntPtr Examples.Bits::PrimitiveLeftUNative(System.UIntPtr,System.Int32) +x64-sse42 System.UIntPtr Examples.Bits::PrimitiveRightUNative(System.UIntPtr,System.Int32) +x64-sse42 System.UIntPtr Examples.Bits::ReverseUNative(System.UIntPtr) +x64-sse42 System.UIntPtr Examples.Bits::RightNative(System.UIntPtr,System.Int32) +x64-sse42 System.UIntPtr Examples.Bits::RoundUpNative(System.UIntPtr) +x64-sse42 System.UIntPtr Examples.IntegerMath::ClampUNative(System.UIntPtr,System.UIntPtr,System.UIntPtr) +x64-sse42 System.UIntPtr Examples.IntegerMath::MaxUNative(System.UIntPtr,System.UIntPtr) +x64-sse42 System.UIntPtr Examples.IntegerMath::MinUNative(System.UIntPtr,System.UIntPtr) +x64-sse42 System.UIntPtr Examples.MemorySpans::NativeWrite(System.Span`1,System.Int32,System.UIntPtr) +x64-sse42 System.UIntPtr Examples.NativeIntegers::AddUnsigned(System.UIntPtr,System.UIntPtr) +x64-sse42 System.UIntPtr Examples.NativeIntegers::CheckedAddUnsigned(System.UIntPtr,System.UIntPtr) +x64-sse42 System.UIntPtr Examples.NativeIntegers::DivideUnsigned(System.UIntPtr,System.UIntPtr) +x64-sse42 System.UIntPtr Examples.NativeIntegers::ReadWriteUnsigned(System.UIntPtr&,System.UIntPtr) +x64-sse42 System.UIntPtr Examples.NativeIntegers::Shift(System.UIntPtr,System.Int32) +x64-sse42 System.UIntPtr Examples.NativeIntegers::WidenUnsigned(System.UInt32) +x64-sse42 System.UIntPtr Examples.PrimitiveArrays::ReadUIntPtr(System.UIntPtr[],System.Int32) +x64-sse42 System.UIntPtr& Examples.UnsafeLayouts::Add11int(System.UIntPtr&,System.Int32) +x64-sse42 System.UIntPtr& Examples.UnsafeLayouts::Add11nint(System.UIntPtr&,System.IntPtr) +x64-sse42 System.UIntPtr& Examples.UnsafeLayouts::Add11nuint(System.UIntPtr&,System.UIntPtr) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathByte(System.Byte,System.Byte) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathInt16(System.Int16,System.Int16) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathInt32(System.Int32,System.Int32) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathInt64(System.Int64,System.Int64) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathIntPtr(System.IntPtr,System.IntPtr) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathSByte(System.SByte,System.SByte) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathUInt16(System.UInt16,System.UInt16) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathUInt32(System.UInt32,System.UInt32) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathUInt64(System.UInt64,System.UInt64) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::Pair(System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.ValueTuple`2 Examples.TupleDivision::MathUIntPtr(System.UIntPtr,System.UIntPtr) +x64-sse42 System.Void Examples.Account::.ctor() +x64-sse42 System.Void Examples.Account::Deposit(System.UInt64) +x64-sse42 System.Void Examples.AggregateArrays::Write(Examples.AggregateArrays/Pair[],System.Int32,Examples.AggregateArrays/Pair) +x64-sse42 System.Void Examples.AggregateArrays::WriteVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32,System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Void Examples.AggregateSpanFill::Aliased(System.Span`1) +x64-sse42 System.Void Examples.AggregateSpanFill::Generic(System.Span`1>,Examples.GenericPair`1) +x64-sse42 System.Void Examples.AggregateSpanFill::Vector128(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Void Examples.AggregateSpanFill::Vector256(System.Span`1>,System.Runtime.Intrinsics.Vector256`1) +x64-sse42 System.Void Examples.AggregateSpanFill::Vector512(System.Span`1>,System.Runtime.Intrinsics.Vector512`1) +x64-sse42 System.Void Examples.AggregateSpanFill::Words(System.Span`1,Examples.AggregateSpans/Words) +x64-sse42 System.Void Examples.AggregateSpans::Clear(System.Span`1) +x64-sse42 System.Void Examples.AggregateSpans::Write(System.Span`1,Examples.AggregateSpans/Words) +x64-sse42 System.Void Examples.AggregateSpans::WriteVector(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Void Examples.Animal::.ctor() +x64-sse42 System.Void Examples.Arguments::Increment(System.UInt32&) +x64-sse42 System.Void Examples.Arithmetic::Mutate(System.UInt64&) +x64-sse42 System.Void Examples.BinarySpans::WriteBig(System.Span`1) +x64-sse42 System.Void Examples.BinarySpans::WriteLittle(System.Span`1) +x64-sse42 System.Void Examples.Bird::.ctor() +x64-sse42 System.Void Examples.Blocks::Copy(System.Byte&,System.Byte&,System.UInt32) +x64-sse42 System.Void Examples.Blocks::CopyAligned(System.Byte&,System.Byte&,System.UInt32) +x64-sse42 System.Void Examples.Blocks::CopyPointer(System.Byte*,System.Byte*,System.UInt32) +x64-sse42 System.Void Examples.Blocks::CopyPointerAligned(System.Byte*,System.Byte*,System.UInt32) +x64-sse42 System.Void Examples.Blocks::Fill(System.Byte&,System.Byte,System.UInt32) +x64-sse42 System.Void Examples.Blocks::FillAligned(System.Byte&,System.Byte,System.UInt32) +x64-sse42 System.Void Examples.Blocks::FillPointer(System.Byte*,System.Byte,System.UInt32) +x64-sse42 System.Void Examples.Blocks::FillPointerAligned(System.Byte*,System.Byte,System.UInt32) +x64-sse42 System.Void Examples.Bucket::.ctor() +x64-sse42 System.Void Examples.CheckedConstructor::.ctor(System.UInt64) +x64-sse42 System.Void Examples.Circle::.ctor(System.UInt64,System.UInt64) +x64-sse42 System.Void Examples.ConstructorLeaf::.ctor(System.UInt32) +x64-sse42 System.Void Examples.Counter::.ctor(System.UInt64) +x64-sse42 System.Void Examples.Counter::Increment(System.Byte&) +x64-sse42 System.Void Examples.Counter::Replace(Examples.Counter) +x64-sse42 System.Void Examples.Counter::Set(System.UInt64) +x64-sse42 System.Void Examples.CounterValue::Increment() +x64-sse42 System.Void Examples.DefaultConstructor::.ctor() +x64-sse42 System.Void Examples.DivisionIntrinsics::Init128(System.Runtime.Intrinsics.Vector128`1&) +x64-sse42 System.Void Examples.DivisionIntrinsics::Init256(System.Runtime.Intrinsics.Vector256`1&) +x64-sse42 System.Void Examples.DivisionIntrinsics::Init512(System.Runtime.Intrinsics.Vector512`1&) +x64-sse42 System.Void Examples.DivisionIntrinsics::SwapHalves(System.ReadOnlySpan`1,System.Span`1) +x64-sse42 System.Void Examples.DivisionIntrinsics::UnsafeStore(System.Runtime.Intrinsics.Vector128`1&,System.UInt64) +x64-sse42 System.Void Examples.Dog::.ctor() +x64-sse42 System.Void Examples.Enums::Assign(Examples.Unsigned64&,Examples.Unsigned64) +x64-sse42 System.Void Examples.ExceptionDocumentation::Reference(System.UInt64&) +x64-sse42 System.Void Examples.ExplicitCounterValue::Examples.ICounterValue.Increment() +x64-sse42 System.Void Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Increment() +x64-sse42 System.Void Examples.Gauge::.ctor() +x64-sse42 System.Void Examples.Gauge::Settle() +x64-sse42 System.Void Examples.GenericEnvelope`1::.ctor(System.UInt64,System.UInt64) +x64-sse42 System.Void Examples.GenericMethods::Clear(Examples.PackedLeaf&) +x64-sse42 System.Void Examples.GenericMethods::Swap32(System.Int32&,System.Int32&) +x64-sse42 System.Void Examples.GenericMethods::Swap(Examples.PackedLeaf&,Examples.PackedLeaf&) +x64-sse42 System.Void Examples.GenericMethods::Swap(System.Int32&,System.Int32&) +x64-sse42 System.Void Examples.GenericMethods::SwapRecord(Examples.PackedLeaf&,Examples.PackedLeaf&) +x64-sse42 System.Void Examples.GenericPair`1::.ctor(Examples.GenericPair`1,Examples.GenericPair`1) +x64-sse42 System.Void Examples.GenericPair`1::.ctor(System.UInt32,System.UInt32) +x64-sse42 System.Void Examples.GenericPair`1::.ctor(System.UInt64,System.UInt64) +x64-sse42 System.Void Examples.GenericPair`1::SetSecond(System.UInt64) +x64-sse42 System.Void Examples.InParameters::Add(Examples.InWord&,Examples.InWord&,Examples.InWord&) +x64-sse42 System.Void Examples.InWord::Examples.IInWord.Add(Examples.InWord& modreq(System.Runtime.InteropServices.InAttribute),Examples.InWord&) +x64-sse42 System.Void Examples.ItemPool::.ctor(System.Int32) +x64-sse42 System.Void Examples.ItemPool::Prewarm(System.Int32) +x64-sse42 System.Void Examples.ItemPool::Return(Examples.PooledItem) +x64-sse42 System.Void Examples.Labelled::.ctor() +x64-sse42 System.Void Examples.Loops::Fill(System.Span`1,System.UInt32) +x64-sse42 System.Void Examples.Loops::FillIndexed(System.UInt32[],System.UInt32) +x64-sse42 System.Void Examples.Loops::FillThrough(System.Span`1,System.UInt32) +x64-sse42 System.Void Examples.Loops::FillThroughSpan(System.UInt32[],System.UInt32) +x64-sse42 System.Void Examples.Loops::FillWords(System.UInt64&,System.UInt64,System.Int32) +x64-sse42 System.Void Examples.Loops::FillWordsDown(System.UInt64&,System.UInt64,System.Int32) +x64-sse42 System.Void Examples.Loops::Put(System.Span`1,System.UInt32) +x64-sse42 System.Void Examples.Loops::Stamp(System.UInt32&,System.UInt32) +x64-sse42 System.Void Examples.Marker::.ctor() +x64-sse42 System.Void Examples.MarshaledMemory::Write(System.Span`1,System.UInt64&) +x64-sse42 System.Void Examples.MarshaledMemory::WriteAggregate(System.Span`1,Examples.FourLimbs&) +x64-sse42 System.Void Examples.MemorySpans::ClearBytes(System.Span`1) +x64-sse42 System.Void Examples.MemorySpans::FillCharacters(System.Span`1,System.Char) +x64-sse42 System.Void Examples.MemorySpans::FillIntegers(System.Span`1,System.Int32) +x64-sse42 System.Void Examples.MemorySpans::FillLongSlice(System.Span`1,System.Int32,System.Int32,System.Int64) +x64-sse42 System.Void Examples.MemorySpans::Write(System.Span`1,System.Int32,System.Int32) +x64-sse42 System.Void Examples.NestedConstructor::.ctor(System.UInt64) +x64-sse42 System.Void Examples.Node::.ctor() +x64-sse42 System.Void Examples.NodeArrays::Put(Examples.Shape[],System.Int32,Examples.Shape) +x64-sse42 System.Void Examples.NodeArrays::Refill(Examples.Bucket,Examples.Node[]) +x64-sse42 System.Void Examples.NullReferences::Write(System.UInt64) +x64-sse42 System.Void Examples.NullReferences::WriteField(System.UInt64) +x64-sse42 System.Void Examples.Objects::Bump(Examples.Account,System.UInt64) +x64-sse42 System.Void Examples.Objects::BumpBoth(Examples.Account,Examples.Account,System.UInt64) +x64-sse42 System.Void Examples.Objects::ClearTag(Examples.Tagged) +x64-sse42 System.Void Examples.Objects::CopyTag(Examples.Tagged,Examples.Tagged) +x64-sse42 System.Void Examples.Objects::DepositBoth(Examples.Account,Examples.Account,System.UInt64) +x64-sse42 System.Void Examples.Objects::DepositFrom(Examples.Account,Examples.Account,System.UInt32) +x64-sse42 System.Void Examples.Objects::DepositTimes(Examples.Account,System.UInt64,System.UInt32) +x64-sse42 System.Void Examples.Objects::FillTag(Examples.Tagged,Examples.Tagged) +x64-sse42 System.Void Examples.Objects::Link(Examples.Node,Examples.Node) +x64-sse42 System.Void Examples.Objects::LinkBoth(Examples.Node,Examples.Node,Examples.Node) +x64-sse42 System.Void Examples.Objects::Relink(Examples.Node,Examples.Node,Examples.Node) +x64-sse42 System.Void Examples.Objects::SetLimit(Examples.Account,System.UInt32) +x64-sse42 System.Void Examples.OutlineCountdown::Clear() +x64-sse42 System.Void Examples.PackedConstructor::.ctor(System.UInt64) +x64-sse42 System.Void Examples.PackedConstructor::.ctor(System.UInt64,System.Byte) +x64-sse42 System.Void Examples.PooledItem::.ctor(Examples.ItemPool,System.Int32) +x64-sse42 System.Void Examples.PooledItem::set_IsRented(System.Boolean) +x64-sse42 System.Void Examples.PooledItem::set_Payload(System.Int64) +x64-sse42 System.Void Examples.PrimitiveArrays::FillEnums(Examples.AllocationTag[],Examples.AllocationTag) +x64-sse42 System.Void Examples.PrimitiveArrays::FillGeneric(Examples.AllocationTag[],Examples.AllocationTag) +x64-sse42 System.Void Examples.PrimitiveArrays::FillGeneric(System.Int32[],System.Int32) +x64-sse42 System.Void Examples.PrimitiveArrays::FillIntegers(System.Int32[],System.Int32) +x64-sse42 System.Void Examples.PrimitiveArrays::FillRangeEnums(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +x64-sse42 System.Void Examples.PrimitiveArrays::FillRangeGeneric(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +x64-sse42 System.Void Examples.PrimitiveArrays::FillRangeGeneric(System.Int32[],System.Int32,System.Int32,System.Int32) +x64-sse42 System.Void Examples.PrimitiveArrays::FillRangeIntegers(System.Int32[],System.Int32,System.Int32,System.Int32) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteBoolean(System.Boolean[],System.Int32,System.Boolean) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteByte(System.Byte[],System.Int32,System.Byte) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteChar(System.Char[],System.Int32,System.Char) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteGeneric(System.Int32[],System.Int32,System.Int32) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteInt16(System.Int16[],System.Int32,System.Int16) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteInt32(System.Int32[],System.Int32,System.Int32) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteInt64(System.Int64[],System.Int32,System.Int64) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteIntPtr(System.IntPtr[],System.Int32,System.IntPtr) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteSByte(System.SByte[],System.Int32,System.SByte) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteUInt16(System.UInt16[],System.Int32,System.UInt16) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteUInt32(System.UInt32[],System.Int32,System.UInt32) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteUInt64(System.UInt64[],System.Int32,System.UInt64) +x64-sse42 System.Void Examples.PrimitiveArrays::WriteUIntPtr(System.UIntPtr[],System.Int32,System.UIntPtr) +x64-sse42 System.Void Examples.SampleArrays::Refill(Examples.Samples,System.Int32[]) +x64-sse42 System.Void Examples.Samples::.ctor() +x64-sse42 System.Void Examples.Shape::.ctor(System.UInt64) +x64-sse42 System.Void Examples.SkipInitialization::Skip(Examples.FourLimbs&) +x64-sse42 System.Void Examples.SkipInitialization::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-sse42 System.Void Examples.SpanCopies::Copy(System.Span`1,System.Span`1) +x64-sse42 System.Void Examples.SpanCopies::CopyLongs(System.ReadOnlySpan`1,System.Span`1) +x64-sse42 System.Void Examples.SpanCopies::CopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +x64-sse42 System.Void Examples.SpanReferences/Limbs::.ctor(System.ReadOnlySpan`1&,System.Boolean) +x64-sse42 System.Void Examples.SpanReferences::SetHigh(System.ReadOnlySpan`1&,System.UInt64) +x64-sse42 System.Void Examples.SpanReferences::WriteFirst(System.Span`1&,System.Int32) +x64-sse42 System.Void Examples.SpanReferences::WriteSecond(System.ReadOnlySpan`1&,System.UInt64) +x64-sse42 System.Void Examples.SpanReferences::WriteWord(System.ReadOnlySpan`1&,System.UInt64) +x64-sse42 System.Void Examples.Square::.ctor(System.UInt64,System.UInt64) +x64-sse42 System.Void Examples.StaticSpans::Write() +x64-sse42 System.Void Examples.StructMethods::ChangeCopy(Examples.Counter) +x64-sse42 System.Void Examples.TagArrays::Put(System.String[],System.Int32,Examples.Tagged) +x64-sse42 System.Void Examples.TagArrays::Stamp(System.String[],Examples.Tagged,System.UInt64) +x64-sse42 System.Void Examples.TagArrays::Take(Examples.Tagged,System.String[],System.Int32) +x64-sse42 System.Void Examples.Tagged::.ctor() +x64-sse42 System.Void Examples.TailCalls::ForwardVoid(System.Int32&) +x64-sse42 System.Void Examples.TailCalls::Store(System.Int32&) +x64-sse42 System.Void Examples.Tally::.ctor(System.UInt32) +x64-sse42 System.Void Examples.Throws::ThrowDivideByZero() +x64-sse42 System.Void Examples.Throws::ThrowNull() +x64-sse42 System.Void Examples.TileArrays::Relane(Examples.Tiles,System.Runtime.Intrinsics.Vector128`1[]) +x64-sse42 System.Void Examples.Tiles::.ctor() +x64-sse42 System.Void Examples.TypedCopy::Packet(Examples.Packet&,Examples.Packet&) +x64-sse42 System.Void Examples.TypedCopy::Record(Examples.Record&,Examples.Record&) +x64-sse42 System.Void Examples.TypedCopy::Vector128(System.Runtime.Intrinsics.Vector128`1&,System.Runtime.Intrinsics.Vector128`1&) +x64-sse42 System.Void Examples.TypedCopy::Vector256(System.Runtime.Intrinsics.Vector256`1&,System.Runtime.Intrinsics.Vector256`1&) +x64-sse42 System.Void Examples.UnalignedMemory::Write128(System.Byte&,System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Void Examples.UnalignedMemory::Write512(System.Byte&,System.Runtime.Intrinsics.Vector512`1) +x64-sse42 System.Void Examples.UnalignedMemory::WriteAggregate(System.Byte&,Examples.FourLimbs) +x64-sse42 System.Void Examples.UnalignedMemory::WriteAligned128(System.Byte*,System.Runtime.Intrinsics.Vector128`1) +x64-sse42 System.Void Examples.UnalignedMemory::WriteAlignedAggregate(System.Byte*,Examples.FourLimbs) +x64-sse42 System.Void Examples.UnalignedMemory::WriteAlignedWord(System.Byte*,System.UInt64) +x64-sse42 System.Void Examples.UnalignedMemory::WriteHalf(System.Byte&,System.Int16) +x64-sse42 System.Void Examples.UnalignedMemory::WritePointerAggregate(System.Byte*,Examples.FourLimbs) +x64-sse42 System.Void Examples.UnalignedMemory::WritePointerWord(System.Byte*,System.UInt64) +x64-sse42 System.Void Examples.UnalignedMemory::WriteSigned(System.Byte&,System.SByte) +x64-sse42 System.Void Examples.UnalignedMemory::WriteWord(System.Byte&,System.UInt64) +x64-sse42 System.Void Examples.WordReader::.ctor() +x64-sse42 System.Void Examples.WordReader::Advance() +x64-sse42 System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Examples.InitCounter::set_Value(System.UInt64) +x64-sse42 method System.Int32 *(System.Int32)[] Examples.ManagedDispatch::NullTable() +x64-v3 Examples.Account Examples.Objects::FirstPresent(Examples.Account,Examples.Account) +x64-v3 Examples.AggregateArrays/Pair Examples.AggregateArrays::Read(Examples.AggregateArrays/Pair[],System.Int32) +x64-v3 Examples.AggregateArrays/Pair& Examples.AggregateArrays::Address(Examples.AggregateArrays/Pair[],System.Int32) +x64-v3 Examples.AggregateArrays/Pair& Examples.AggregateArrays::Data(Examples.AggregateArrays/Pair[]) +x64-v3 Examples.AggregateArrays/Pair[] Examples.TileArrays::PairsOf(Examples.Tiles) +x64-v3 Examples.AggregateSpans/VectorRecord Examples.AggregateSpans::LayoutRead(System.ReadOnlySpan`1) +x64-v3 Examples.AggregateSpans/Words Examples.AggregateSpans::Read(System.ReadOnlySpan`1) +x64-v3 Examples.AllocationTag[] Examples.PrimitiveArrays::AllocateUnnamedZero(System.Int32) +x64-v3 Examples.CheckedConstructor Examples.Constructors::MakeChecked(System.UInt64) +x64-v3 Examples.Counter Examples.Constructors::Return(System.UInt64) +x64-v3 Examples.Counter Examples.Constructors::WrongReturn(System.UInt64) +x64-v3 Examples.DefaultConstructor Examples.Constructors::MakeDefault() +x64-v3 Examples.Dog Examples.ClassTests::AsDog(Examples.Animal) +x64-v3 Examples.EnumBuffers/Large Examples.EnumBuffers::Wide(Examples.EnumBuffers/Large[],System.Int32) +x64-v3 Examples.EnumBuffers/Small Examples.EnumBuffers::Address(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +x64-v3 Examples.EnumBuffers/Small Examples.EnumBuffers::FillClear(Examples.EnumBuffers/Small[]) +x64-v3 Examples.EnumBuffers/Small Examples.EnumBuffers::Loop(Examples.EnumBuffers/Small[],System.Int32) +x64-v3 Examples.EnumBuffers/Small Examples.EnumBuffers::Read(System.ReadOnlySpan`1) +x64-v3 Examples.EnumBuffers/Small Examples.EnumBuffers::Roundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +x64-v3 Examples.EnumBuffers/Small Examples.EnumBuffers::SpanRoundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +x64-v3 Examples.FourLimbs Examples.MarshaledMemory::ReadAggregate(System.ReadOnlySpan`1) +x64-v3 Examples.FourLimbs Examples.UnalignedMemory::ReadAggregate(System.Byte&) +x64-v3 Examples.FourLimbs Examples.UnalignedMemory::ReadAlignedAggregate(System.Byte*) +x64-v3 Examples.FourLimbs Examples.UnalignedMemory::ReadPointerAggregate(System.Byte*) +x64-v3 Examples.FourLimbs& Examples.UnsafeReferences::Mutable(Examples.FourLimbs&) +x64-v3 Examples.FourLimbs& Examples.UnsafeReferences::SubtractAggregate(Examples.FourLimbs&,System.IntPtr) +x64-v3 Examples.GenericPair`1> Examples.GenericLayouts::Nested(System.UInt32,System.UInt32) +x64-v3 Examples.GenericPair`1 Examples.AggregateArrays::ReadGeneric(Examples.GenericPair`1[],System.Int32) +x64-v3 Examples.GenericPair`1 Examples.GenericLayouts::Create(System.UInt64,System.UInt64) +x64-v3 Examples.IndirectValues/Box`1 Examples.IndirectValues::Generic(Examples.IndirectValues/Box`1) +x64-v3 Examples.IndirectValues/Box`1 Examples.IndirectValues::Identity>(Examples.IndirectValues/Box`1) +x64-v3 Examples.IndirectValues/Pair Examples.IndirectValues::Identity(Examples.IndirectValues/Pair) +x64-v3 Examples.IndirectValues/Pair Examples.IndirectValues::Read(Examples.IndirectValues/Pair&) +x64-v3 Examples.IndirectValues/Pair Examples.IndirectValues::Record(Examples.IndirectValues/Pair) +x64-v3 Examples.IndirectValues/Pair Examples.IndirectValues::Reference(Examples.IndirectValues/Pair&) +x64-v3 Examples.ItemPool Examples.PooledItem::get_Owner() +x64-v3 Examples.Leaf Examples.Nested::Extract(Examples.NestedRecord) +x64-v3 Examples.Leaf Examples.Nested::WrongExtract(Examples.NestedRecord) +x64-v3 Examples.NestedConstructor Examples.Constructors::MakeNested(System.UInt64) +x64-v3 Examples.NestedRecord& Examples.UnsafeLayouts::Add15int(Examples.NestedRecord&,System.Int32) +x64-v3 Examples.NestedRecord& Examples.UnsafeLayouts::Add15nint(Examples.NestedRecord&,System.IntPtr) +x64-v3 Examples.NestedRecord& Examples.UnsafeLayouts::Add15nuint(Examples.NestedRecord&,System.UIntPtr) +x64-v3 Examples.NestedRecord[] Examples.PrimitiveArrays::AllocateNested(System.Int32) +x64-v3 Examples.Node Examples.NodeArrays::At(Examples.Node[],System.Int32) +x64-v3 Examples.Node Examples.NodeArrays::First(Examples.Bucket) +x64-v3 Examples.Node Examples.NodeArrays::Install(Examples.Node[],System.Int32,Examples.Node,System.UInt64) +x64-v3 Examples.Node Examples.NodeArrays::Pair(Examples.Node,Examples.Node) +x64-v3 Examples.Node Examples.NodeArrays::Shift(Examples.Node[],Examples.Node) +x64-v3 Examples.Node Examples.Objects::Detach(Examples.Node) +x64-v3 Examples.Node[] Examples.NodeArrays::Create(System.Int32) +x64-v3 Examples.PackedConstructor Examples.Constructors::MakePacked(System.UInt64) +x64-v3 Examples.PackedLeaf& Examples.GenericMethods::Next(Examples.PackedLeaf&,System.Int32) +x64-v3 Examples.PackedLeaf& Examples.GenericMethods::NextPacked(Examples.PackedLeaf&,System.Int32) +x64-v3 Examples.PackedLeaf& Examples.UnsafeLayouts::Add14int(Examples.PackedLeaf&,System.Int32) +x64-v3 Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nint(Examples.PackedLeaf&,System.IntPtr) +x64-v3 Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nuint(Examples.PackedLeaf&,System.UIntPtr) +x64-v3 Examples.PackedLeaf[] Examples.PrimitiveArrays::AllocatePacked(System.Int32) +x64-v3 Examples.Packet& Examples.UnsafeLayouts::Add16int(Examples.Packet&,System.Int32) +x64-v3 Examples.Packet& Examples.UnsafeLayouts::Add16nint(Examples.Packet&,System.IntPtr) +x64-v3 Examples.Packet& Examples.UnsafeLayouts::Add16nuint(Examples.Packet&,System.UIntPtr) +x64-v3 Examples.PooledItem Examples.ItemPool::Rent() +x64-v3 Examples.Signed16 Examples.Enums::IdentityS16(Examples.Signed16) +x64-v3 Examples.Signed16 Examples.Enums::ThroughCall(Examples.Signed16) +x64-v3 Examples.Signed16 Examples.PointerOperations::ReadEnum(Examples.Signed16*,System.Int32) +x64-v3 Examples.Signed16[] Examples.PrimitiveArrays::AllocateSigned16(System.Int32) +x64-v3 Examples.Signed32 Examples.Enums::IdentityS32(Examples.Signed32) +x64-v3 Examples.Signed32[] Examples.PrimitiveArrays::AllocateSigned32(System.Int32) +x64-v3 Examples.Signed64 Examples.Enums::IdentityS64(Examples.Signed64) +x64-v3 Examples.Signed64[] Examples.PrimitiveArrays::AllocateSigned64(System.Int32) +x64-v3 Examples.Signed8 Examples.Enums::Field(Examples.Signed8) +x64-v3 Examples.Signed8 Examples.Enums::IdentityS8(Examples.Signed8) +x64-v3 Examples.Signed8 Examples.Enums::Read(Examples.EnumRecord&) +x64-v3 Examples.Signed8 Examples.GenericMethods::EnumIdentity(Examples.Signed8) +x64-v3 Examples.Signed8 Examples.GenericMethods::Identity(Examples.Signed8) +x64-v3 Examples.Signed8& Examples.UnsafeLayouts::Add12int(Examples.Signed8&,System.Int32) +x64-v3 Examples.Signed8& Examples.UnsafeLayouts::Add12nint(Examples.Signed8&,System.IntPtr) +x64-v3 Examples.Signed8& Examples.UnsafeLayouts::Add12nuint(Examples.Signed8&,System.UIntPtr) +x64-v3 Examples.Signed8[] Examples.PrimitiveArrays::AllocateSigned8(System.Int32) +x64-v3 Examples.Tiles Examples.TileArrays::Hold(Examples.AggregateArrays/Pair[],System.Runtime.Intrinsics.Vector128`1[]) +x64-v3 Examples.Unsigned16 Examples.Enums::IdentityU16(Examples.Unsigned16) +x64-v3 Examples.Unsigned16[] Examples.PrimitiveArrays::AllocateUnsigned16(System.Int32) +x64-v3 Examples.Unsigned32 Examples.Enums::IdentityU32(Examples.Unsigned32) +x64-v3 Examples.Unsigned32[] Examples.PrimitiveArrays::AllocateUnsigned32(System.Int32) +x64-v3 Examples.Unsigned64 Examples.Enums::IdentityU64(Examples.Unsigned64) +x64-v3 Examples.Unsigned64 Examples.Enums::Local(Examples.Unsigned64) +x64-v3 Examples.Unsigned64 Examples.Enums::SetFlag(Examples.Unsigned64) +x64-v3 Examples.Unsigned64 Examples.Enums::WrongSetFlag(Examples.Unsigned64) +x64-v3 Examples.Unsigned64& Examples.UnsafeLayouts::Add13int(Examples.Unsigned64&,System.Int32) +x64-v3 Examples.Unsigned64& Examples.UnsafeLayouts::Add13nint(Examples.Unsigned64&,System.IntPtr) +x64-v3 Examples.Unsigned64& Examples.UnsafeLayouts::Add13nuint(Examples.Unsigned64&,System.UIntPtr) +x64-v3 Examples.Unsigned64[] Examples.PrimitiveArrays::AllocateUnsigned64(System.Int32) +x64-v3 Examples.Unsigned8 Examples.Enums::IdentityU8(Examples.Unsigned8) +x64-v3 Examples.Unsigned8[] Examples.PrimitiveArrays::AllocateUnsigned8(System.Int32) +x64-v3 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int32(System.Runtime.Intrinsics.Vector256`1) +x64-v3 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int64(System.Runtime.Intrinsics.Vector256`1) +x64-v3 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt32(System.Runtime.Intrinsics.Vector256`1) +x64-v3 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt64(System.Runtime.Intrinsics.Vector256`1) +x64-v3 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ParameterCopy(Examples.ValueBitCasts/Limbs) +x64-v3 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ReadAggregate(Examples.ValueBitCasts/Limbs&) +x64-v3 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::RoundTrip(Examples.ValueBitCasts/Limbs) +x64-v3 Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128Byte(System.Runtime.Intrinsics.Vector128`1) +x64-v3 Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt32(System.Runtime.Intrinsics.Vector128`1) +x64-v3 Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt64(System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Boolean Examples.AggregateSpans::Copy(System.ReadOnlySpan`1,System.Span`1) +x64-v3 System.Boolean Examples.AggregateSpans::IsEmpty(System.Span`1) +x64-v3 System.Boolean Examples.AggregateSpans::VectorIsEmpty(System.ReadOnlySpan`1>) +x64-v3 System.Boolean Examples.Allowance::Spend(Examples.Allowance&,System.UInt64) +x64-v3 System.Boolean Examples.Bits::PowerI32(System.Int32) +x64-v3 System.Boolean Examples.Bits::PowerI64(System.Int64) +x64-v3 System.Boolean Examples.Bits::PowerNative(System.IntPtr) +x64-v3 System.Boolean Examples.Bits::PowerU32(System.UInt32) +x64-v3 System.Boolean Examples.Bits::PowerU64(System.UInt64) +x64-v3 System.Boolean Examples.Bits::PowerUNative(System.UIntPtr) +x64-v3 System.Boolean Examples.Bits::PrimitivePower32(System.Int32) +x64-v3 System.Boolean Examples.Bits::PrimitivePower64(System.Int64) +x64-v3 System.Boolean Examples.Bits::PrimitivePowerNative(System.IntPtr) +x64-v3 System.Boolean Examples.Bits::PrimitivePowerU32(System.UInt32) +x64-v3 System.Boolean Examples.Bits::PrimitivePowerU64(System.UInt64) +x64-v3 System.Boolean Examples.Bits::PrimitivePowerUNative(System.UIntPtr) +x64-v3 System.Boolean Examples.ClassTests::IsDog(Examples.Animal) +x64-v3 System.Boolean Examples.ComputedFlags::get_Both() +x64-v3 System.Boolean Examples.ComputedFlags::get_Middle() +x64-v3 System.Boolean Examples.ComputedFlags::get_NotOff() +x64-v3 System.Boolean Examples.ComputedFlags::get_Outer() +x64-v3 System.Boolean Examples.DivisionIntrinsics::EqualBytes256(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1) +x64-v3 System.Boolean Examples.DivisionIntrinsics::EqualSigned512(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1) +x64-v3 System.Boolean Examples.DivisionIntrinsics::EqualWords128(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Boolean Examples.DivisionIntrinsics::UnsafeEquals(System.UInt64) +x64-v3 System.Boolean Examples.DoublingAllowance::Examples.IAllowance.Spend(Examples.DoublingAllowance&) +x64-v3 System.Boolean Examples.DoublingAllowance::Spend(Examples.DoublingAllowance&,System.UInt64) +x64-v3 System.Boolean Examples.Forced::get_IsActive() +x64-v3 System.Boolean Examples.Gate`1::get_Open() +x64-v3 System.Boolean Examples.Gate`1::get_Open() +x64-v3 System.Boolean Examples.Hush`1::get_Silent() +x64-v3 System.Boolean Examples.IAllowance`1::Spend(Examples.Allowance&) +x64-v3 System.Boolean Examples.ILoud::Examples.IQuietByDefault.get_IsActive() +x64-v3 System.Boolean Examples.ILoudByDefault::get_IsActive() +x64-v3 System.Boolean Examples.IQuiet::Examples.ILoudByDefault.get_IsActive() +x64-v3 System.Boolean Examples.IQuietByDefault::get_IsActive() +x64-v3 System.Boolean Examples.IRefundingAllowance`1::Examples.IAllowance.Spend(Examples.RefundingAllowance&) +x64-v3 System.Boolean Examples.IntegerOperations::Greater32(System.Int32,System.Int32) +x64-v3 System.Boolean Examples.IntegerOperations::Greater64(System.Int64,System.Int64) +x64-v3 System.Boolean Examples.ItemPool::IsIdle(Examples.PooledItem) +x64-v3 System.Boolean Examples.Loops::AllZero(System.ReadOnlySpan`1) +x64-v3 System.Boolean Examples.MarshaledMemory::TryRead(System.ReadOnlySpan`1,System.UInt64&) +x64-v3 System.Boolean Examples.MarshaledMemory::TryReadAggregate(System.ReadOnlySpan`1,Examples.FourLimbs&) +x64-v3 System.Boolean Examples.MarshaledMemory::TryWrite(System.Span`1,System.UInt64&) +x64-v3 System.Boolean Examples.MemorySpans::BooleanWrite(System.Span`1,System.Int32,System.Boolean) +x64-v3 System.Boolean Examples.NullReferences::IsLive(System.UInt64&) +x64-v3 System.Boolean Examples.NullReferences::IsNull() +x64-v3 System.Boolean Examples.Objects::HasTag(Examples.Tagged) +x64-v3 System.Boolean Examples.Objects::IsNull(Examples.Account) +x64-v3 System.Boolean Examples.Objects::IsPresent(Examples.Account) +x64-v3 System.Boolean Examples.Objects::Same(Examples.Account,Examples.Account) +x64-v3 System.Boolean Examples.Objects::SameTag(Examples.Tagged,Examples.Tagged) +x64-v3 System.Boolean Examples.Objects::TwoMarkers() +x64-v3 System.Boolean Examples.PointerOperations::Greater(System.Byte*,System.Byte*) +x64-v3 System.Boolean Examples.PointerOperations::GreaterEqual(System.Byte*,System.Byte*) +x64-v3 System.Boolean Examples.PointerOperations::Less(System.Byte*,System.Byte*) +x64-v3 System.Boolean Examples.PointerOperations::LessEqual(System.Byte*,System.Byte*) +x64-v3 System.Boolean Examples.PooledItem::get_IsRented() +x64-v3 System.Boolean Examples.PrimitiveArrays::ReadBoolean(System.Boolean[],System.Int32) +x64-v3 System.Boolean Examples.RefundingAllowance::Spend(Examples.RefundingAllowance&,System.UInt64) +x64-v3 System.Boolean Examples.SpanCopies::TryCopy(System.Span`1,System.Span`1) +x64-v3 System.Boolean Examples.SpanCopies::TryCopyEnum(System.ReadOnlySpan`1,System.Span`1) +x64-v3 System.Boolean Examples.SpanCopies::TryCopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +x64-v3 System.Boolean Examples.StepOff::get_IsActive() +x64-v3 System.Boolean Examples.StepOn::get_IsActive() +x64-v3 System.Boolean Examples.Strings::SameLabel(System.String,System.String) +x64-v3 System.Boolean Examples.Strings::SameText() +x64-v3 System.Boolean Examples.UnsafeReferences::AreSame(System.Byte&,System.Byte&) +x64-v3 System.Boolean Examples.UnsafeReferences::Greater(System.Byte&,System.Byte&) +x64-v3 System.Boolean Examples.UnsafeReferences::Less(System.Byte&,System.Byte&) +x64-v3 System.Boolean Examples.ViewAccessors::Empty(System.Span`1) +x64-v3 System.Boolean Examples.ViewAccessors::PinnableNull(System.Span`1) +x64-v3 System.Boolean Examples.ViewAccessors::ReadOnlyEmpty(System.ReadOnlySpan`1) +x64-v3 System.Boolean Examples.ViewAccessors::ReadOnlyPinnableNull(System.ReadOnlySpan`1) +x64-v3 System.Boolean Examples.ViewAccessors::ReadOnlyReferenceNull(System.ReadOnlySpan`1) +x64-v3 System.Boolean Examples.ViewAccessors::ReferenceNull(System.Span`1) +x64-v3 System.Boolean Examples.WordFlag`1::get_IsActive() +x64-v3 System.Boolean Examples.WordFlag`1::get_IsActive() +x64-v3 System.Boolean& Examples.UnsafeLayouts::Add0int(System.Boolean&,System.Int32) +x64-v3 System.Boolean& Examples.UnsafeLayouts::Add0nint(System.Boolean&,System.IntPtr) +x64-v3 System.Boolean& Examples.UnsafeLayouts::Add0nuint(System.Boolean&,System.UIntPtr) +x64-v3 System.Byte Examples.Arguments::Narrow(System.Byte) +x64-v3 System.Byte Examples.Bits::ReverseU8(System.Byte) +x64-v3 System.Byte Examples.Counter::Parameter(System.Byte) +x64-v3 System.Byte Examples.GenericMethods::Identity(System.Byte) +x64-v3 System.Byte Examples.IntegerMath::ClampUInt8(System.Byte,System.Byte,System.Byte) +x64-v3 System.Byte Examples.IntegerMath::MaxUInt8(System.Byte,System.Byte) +x64-v3 System.Byte Examples.IntegerMath::MinUInt8(System.Byte,System.Byte) +x64-v3 System.Byte Examples.PrimitiveArrays::ReadByte(System.Byte[],System.Int32) +x64-v3 System.Byte Examples.SpanReferences::At(System.ReadOnlySpan`1&,System.Int32) +x64-v3 System.Byte Examples.StaticSpans::First() +x64-v3 System.Byte Examples.StaticSpans::Last() +x64-v3 System.Byte Examples.StaticSpans::PastEnd() +x64-v3 System.Byte Examples.StructMethods::Narrow(System.Byte) +x64-v3 System.Byte& Examples.UnsafeLayouts::Add3int(System.Byte&,System.Int32) +x64-v3 System.Byte& Examples.UnsafeLayouts::Add3nint(System.Byte&,System.IntPtr) +x64-v3 System.Byte& Examples.UnsafeLayouts::Add3nuint(System.Byte&,System.UIntPtr) +x64-v3 System.Byte& Examples.UnsafeReferences::AddSigned(System.Byte&,System.IntPtr) +x64-v3 System.Byte& Examples.UnsafeReferences::AddUnsigned(System.Byte&,System.UIntPtr) +x64-v3 System.Byte& Examples.UnsafeReferences::Subtract(System.Byte&,System.IntPtr) +x64-v3 System.Byte& Examples.UnsafeReferences::SubtractUnsigned(System.Byte&,System.UIntPtr) +x64-v3 System.Byte* Examples.PointerOperations::Subtract(System.Byte*,System.Int32) +x64-v3 System.Byte* Examples.PointerOperations::SubtractNative(System.Byte*,System.IntPtr) +x64-v3 System.Byte* Examples.UnsafeReferences::AddPointer(System.Byte*,System.Int32) +x64-v3 System.Byte* Examples.UnsafeReferences::AddPointerAggregate(System.Byte*,System.Int32) +x64-v3 System.Byte* Examples.UnsafeReferences::SubtractPointer(System.Byte*,System.Int32) +x64-v3 System.Byte* Examples.UnsafeReferences::SubtractPointerAggregate(System.Byte*,System.Int32) +x64-v3 System.Byte[] Examples.PrimitiveArrays::AllocateBytes(System.Int32) +x64-v3 System.Char Examples.MemorySpans::CharacterWrite(System.Span`1,System.Int32,System.Char) +x64-v3 System.Char Examples.NarrowIntegers::Character(System.Int32) +x64-v3 System.Char Examples.PointerOperations::ReadCharacter(System.Char*,System.Int32) +x64-v3 System.Char Examples.PrimitiveArrays::ReadChar(System.Char[],System.Int32) +x64-v3 System.Char& Examples.UnsafeLayouts::Add1int(System.Char&,System.Int32) +x64-v3 System.Char& Examples.UnsafeLayouts::Add1nint(System.Char&,System.IntPtr) +x64-v3 System.Char& Examples.UnsafeLayouts::Add1nuint(System.Char&,System.UIntPtr) +x64-v3 System.Int16 Examples.Bits::ReverseI16(System.Int16) +x64-v3 System.Int16 Examples.IntegerMath::AbsInt16(System.Int16) +x64-v3 System.Int16 Examples.IntegerMath::ClampInt16(System.Int16,System.Int16,System.Int16) +x64-v3 System.Int16 Examples.IntegerMath::MaxInt16(System.Int16,System.Int16) +x64-v3 System.Int16 Examples.IntegerMath::MinInt16(System.Int16,System.Int16) +x64-v3 System.Int16 Examples.NarrowIntegers::Identity(System.Int16) +x64-v3 System.Int16 Examples.PrimitiveArrays::ReadInt16(System.Int16[],System.Int32) +x64-v3 System.Int16 Examples.UnalignedMemory::ReadHalf(System.Byte&) +x64-v3 System.Int16& Examples.UnsafeLayouts::Add4int(System.Int16&,System.Int32) +x64-v3 System.Int16& Examples.UnsafeLayouts::Add4nint(System.Int16&,System.IntPtr) +x64-v3 System.Int16& Examples.UnsafeLayouts::Add4nuint(System.Int16&,System.UIntPtr) +x64-v3 System.Int16[] Examples.PrimitiveArrays::AllocateShorts(System.Int32) +x64-v3 System.Int32 Examples.AggregateArrays::ConstructedLength(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Arithmetic::AtLeastFive(System.Int32) +x64-v3 System.Int32 Examples.Arithmetic::FiveToOne(System.Int32) +x64-v3 System.Int32 Examples.Arithmetic::Larger(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Arithmetic::WordAdjusted(System.Int32) +x64-v3 System.Int32 Examples.Arithmetic::WordSized(System.Int32) +x64-v3 System.Int32 Examples.ArrayElementReferences::Read(System.Int32[],System.Int32) +x64-v3 System.Int32 Examples.ArrayElementReferences::Write(System.Int32[],System.Int32,System.Int32) +x64-v3 System.Int32 Examples.BinarySpans::TryWriteBig(System.Span`1) +x64-v3 System.Int32 Examples.BinarySpans::TryWriteLittle(System.Span`1) +x64-v3 System.Int32 Examples.Bits::Leading32(System.UInt32) +x64-v3 System.Int32 Examples.Bits::Leading64(System.UInt64) +x64-v3 System.Int32 Examples.Bits::LeadingNative(System.UIntPtr) +x64-v3 System.Int32 Examples.Bits::Log32(System.UInt32) +x64-v3 System.Int32 Examples.Bits::Log64(System.UInt64) +x64-v3 System.Int32 Examples.Bits::LogNative(System.UIntPtr) +x64-v3 System.Int32 Examples.Bits::Population32(System.UInt32) +x64-v3 System.Int32 Examples.Bits::Population64(System.UInt64) +x64-v3 System.Int32 Examples.Bits::PopulationNative(System.UIntPtr) +x64-v3 System.Int32 Examples.Bits::PrimitiveLeading32(System.Int32) +x64-v3 System.Int32 Examples.Bits::PrimitiveLeft32(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Bits::PrimitiveNarrow16(System.UInt32) +x64-v3 System.Int32 Examples.Bits::PrimitiveNarrow8(System.UInt32) +x64-v3 System.Int32 Examples.Bits::PrimitiveNarrowRotate16(System.UInt32,System.Int32) +x64-v3 System.Int32 Examples.Bits::PrimitiveNarrowRotate8(System.UInt32,System.Int32) +x64-v3 System.Int32 Examples.Bits::PrimitiveNarrowSigned(System.UInt32) +x64-v3 System.Int32 Examples.Bits::PrimitivePopulation32(System.Int32) +x64-v3 System.Int32 Examples.Bits::PrimitiveRight32(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Bits::PrimitiveTrailing32(System.Int32) +x64-v3 System.Int32 Examples.Bits::ReverseI32(System.Int32) +x64-v3 System.Int32 Examples.Bits::Trailing32(System.UInt32) +x64-v3 System.Int32 Examples.Bits::Trailing64(System.UInt64) +x64-v3 System.Int32 Examples.Bits::TrailingNative(System.UIntPtr) +x64-v3 System.Int32 Examples.Bits::WrongLog64(System.UInt64) +x64-v3 System.Int32 Examples.Capabilities::ArmCount(System.UInt64) +x64-v3 System.Int32 Examples.Capabilities::Endian() +x64-v3 System.Int32 Examples.Capabilities::Platform() +x64-v3 System.Int32 Examples.Capabilities::ReferenceFree() +x64-v3 System.Int32 Examples.Capabilities::VbmiCount(System.UInt64) +x64-v3 System.Int32 Examples.CheckedArithmetic::AddSigned(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +x64-v3 System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +x64-v3 System.Int32 Examples.ConstrainedCalls::ClosedBoth(Examples.ClosedInterfaceCounter&) +x64-v3 System.Int32 Examples.ConstrainedCalls::ClosedGeneric(Examples.GenericInterfaceCounter`1&) +x64-v3 System.Int32 Examples.ConstrainedCalls::DirectGeneric(Examples.GenericCounterValue`1&) +x64-v3 System.Int32 Examples.ConstrainedCalls::Explicit(Examples.ExplicitCounterValue&) +x64-v3 System.Int32 Examples.ConstrainedCalls::Generic(Examples.GenericCounterValue`1&) +x64-v3 System.Int32 Examples.ConstrainedCalls::GenericExplicit(Examples.ExplicitGenericCounterValue`1&) +x64-v3 System.Int32 Examples.ConstrainedCalls::Implicit(Examples.CounterValue&) +x64-v3 System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.CounterValue&) +x64-v3 System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.ExplicitCounterValue&) +x64-v3 System.Int32 Examples.ConstrainedCalls::IncrementAndRead>(Examples.ExplicitGenericCounterValue`1&) +x64-v3 System.Int32 Examples.ConstrainedCalls::Mutating(Examples.CounterValue&) +x64-v3 System.Int32 Examples.ConstrainedCalls::MutatingExplicit(Examples.ExplicitCounterValue&) +x64-v3 System.Int32 Examples.ConstrainedCalls::Read(Examples.CounterValue&) +x64-v3 System.Int32 Examples.ConstrainedCalls::Read(Examples.ExplicitCounterValue&) +x64-v3 System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +x64-v3 System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +x64-v3 System.Int32 Examples.ConstrainedCalls::ReadClosedInt(Examples.ClosedInterfaceCounter&) +x64-v3 System.Int32 Examples.ConstrainedCalls::ReadClosedInt>(Examples.GenericInterfaceCounter`1&) +x64-v3 System.Int32 Examples.ConstrainedCalls::ReadClosedLong(Examples.ClosedInterfaceCounter&) +x64-v3 System.Int32 Examples.ConstrainedCalls::TwoGenericOwners(Examples.GenericCounterValue`1&,Examples.GenericCounterValue`1&) +x64-v3 System.Int32 Examples.CounterValue::Read() +x64-v3 System.Int32 Examples.Enums::Branch(Examples.Signed8) +x64-v3 System.Int32 Examples.ExceptionDocumentation::Absolute(System.Int32) +x64-v3 System.Int32 Examples.ExceptionDocumentation::CheckedNarrow(System.Int64) +x64-v3 System.Int32 Examples.ExceptionDocumentation::Clamp(System.Int32,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.ExplicitCounterValue::Examples.ICounterValue.Read() +x64-v3 System.Int32 Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Read() +x64-v3 System.Int32 Examples.Gauge::BelowFive() +x64-v3 System.Int32 Examples.Gauge::Blend(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Gauge::Blended(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Gauge::Bounded(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Gauge::Capped(Examples.Gauge,System.Int32) +x64-v3 System.Int32 Examples.Gauge::CappedLevel() +x64-v3 System.Int32 Examples.Gauge::CappedSlack() +x64-v3 System.Int32 Examples.Gauge::Checked(Examples.Gauge,System.Int32) +x64-v3 System.Int32 Examples.Gauge::CheckedLevel() +x64-v3 System.Int32 Examples.Gauge::Clamp(System.Int32,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Gauge::Clamped(System.Int32,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Gauge::Current() +x64-v3 System.Int32 Examples.Gauge::Doubled(System.Int32) +x64-v3 System.Int32 Examples.Gauge::Floor(System.Int32) +x64-v3 System.Int32 Examples.Gauge::Floored(System.Int32) +x64-v3 System.Int32 Examples.Gauge::Gap(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Gauge::IsFive() +x64-v3 System.Int32 Examples.Gauge::Margin() +x64-v3 System.Int32 Examples.Gauge::Raise(Examples.Gauge,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Gauge::RaisedLevel() +x64-v3 System.Int32 Examples.Gauge::Saturate(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Gauge::Slack() +x64-v3 System.Int32 Examples.Gauge::Smaller(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.Gauge::Total(System.Int32[]) +x64-v3 System.Int32 Examples.Gauge::Twice(System.Int32) +x64-v3 System.Int32 Examples.Gauge::Within(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.GenericCounterValue`1::Echo(System.Int32) +x64-v3 System.Int32 Examples.GenericCounterValue`1::Read() +x64-v3 System.Int32 Examples.GenericCounterValue`1::Read() +x64-v3 System.Int32 Examples.GenericInterfaceCounter`1::Read() +x64-v3 System.Int32 Examples.GenericLayouts::TupleSize() +x64-v3 System.Int32 Examples.GenericLayouts::VectorSize() +x64-v3 System.Int32 Examples.GenericMethods::Identity32(System.Int32) +x64-v3 System.Int32 Examples.GenericMethods::Identity(System.Int32) +x64-v3 System.Int32 Examples.GenericMethods::Mixed(System.Int32,System.Byte) +x64-v3 System.Int32 Examples.GenericMethods::PackedSize() +x64-v3 System.Int32 Examples.GenericMethods::Read32(System.Int32&) +x64-v3 System.Int32 Examples.GenericMethods::Read(System.Int32&) +x64-v3 System.Int32 Examples.GenericMethods::Size() +x64-v3 System.Int32 Examples.GenericMethods::Size>() +x64-v3 System.Int32 Examples.GenericMethods::VectorSize() +x64-v3 System.Int32 Examples.GenericMethods::WrongIdentity(System.Int32) +x64-v3 System.Int32 Examples.GenericOwners::SpanCount(System.ReadOnlySpan`1) +x64-v3 System.Int32 Examples.Guards::Count(System.Int32) +x64-v3 System.Int32 Examples.IntegerMath::AbsInt32(System.Int32) +x64-v3 System.Int32 Examples.IntegerMath::ClampInt32(System.Int32,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.IntegerMath::MaxInt32(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.IntegerMath::MinInt32(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.IntegerMath::SignInt16(System.Int16) +x64-v3 System.Int32 Examples.IntegerMath::SignInt32(System.Int32) +x64-v3 System.Int32 Examples.IntegerMath::SignInt64(System.Int64) +x64-v3 System.Int32 Examples.IntegerMath::SignInt8(System.SByte) +x64-v3 System.Int32 Examples.IntegerMath::SignNative(System.IntPtr) +x64-v3 System.Int32 Examples.IntegerOperations::Negate32(System.Int32) +x64-v3 System.Int32 Examples.ItemPool::Trim(System.Int32) +x64-v3 System.Int32 Examples.ItemPool::get_Available() +x64-v3 System.Int32 Examples.ItemPool::get_Capacity() +x64-v3 System.Int32 Examples.ItemPool::get_Rented() +x64-v3 System.Int32 Examples.Loops::CountToFive(System.Int32) +x64-v3 System.Int32 Examples.Loops::Drain(System.ReadOnlySpan`1) +x64-v3 System.Int32 Examples.ManagedDispatch::LocalEscape(System.Int32) +x64-v3 System.Int32 Examples.ManagedDispatch::NullLength() +x64-v3 System.Int32 Examples.ManagedDispatch::Read(System.Int32&) +x64-v3 System.Int32 Examples.MemorySpans::ArraySliceWrite(System.Int32[],System.Int32,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.MemorySpans::Existing(System.Int32&,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.MemorySpans::InPlaceArrayConstructor(System.Int32[],System.Int32,System.Int32) +x64-v3 System.Int32 Examples.MemorySpans::InPlaceArrayLength(System.Int32[],System.Int32,System.Int32) +x64-v3 System.Int32 Examples.MemorySpans::InPlacePointerSpan(System.Int32*,System.Int32) +x64-v3 System.Int32 Examples.MemorySpans::Read(System.ReadOnlySpan`1,System.Int32) +x64-v3 System.Int32 Examples.MemorySpans::ReferenceWrite(System.Span`1,System.Int32) +x64-v3 System.Int32 Examples.MemorySpans::ReinitializeArraySpan(System.Int32[],System.Int32,System.Int32) +x64-v3 System.Int32 Examples.MemorySpans::SliceRead(System.ReadOnlySpan`1,System.Int32,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.MemorySpans::SliceWrite(System.Span`1,System.Int32,System.Int32,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.NarrowIntegers::Call(System.Int32) +x64-v3 System.Int32 Examples.NarrowIntegers::Fields(System.Int32) +x64-v3 System.Int32 Examples.NarrowIntegers::SignedByte(System.Int32) +x64-v3 System.Int32 Examples.NarrowIntegers::SignedShort(System.Int64) +x64-v3 System.Int32 Examples.NarrowIntegers::UnsignedShort(System.Int32) +x64-v3 System.Int32 Examples.NativeIntegers::Narrow(System.IntPtr) +x64-v3 System.Int32 Examples.NativeIntegers::SignedSize() +x64-v3 System.Int32 Examples.NativeIntegers::UnsignedSize() +x64-v3 System.Int32 Examples.NodeArrays::Count(Examples.Node[]) +x64-v3 System.Int32 Examples.PooledItem::get_Id() +x64-v3 System.Int32 Examples.PrimitiveArrays::AllocateAndWrite(System.Int32,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.PrimitiveArrays::AllocatePackedAndWrite(System.Int32,System.Int32,System.Int32) +x64-v3 System.Int32 Examples.PrimitiveArrays::Length(System.Int32[]) +x64-v3 System.Int32 Examples.PrimitiveArrays::ReadAlias(System.Int32[],System.Int32,System.Int32) +x64-v3 System.Int32 Examples.PrimitiveArrays::ReadGeneric(System.Int32[],System.Int32) +x64-v3 System.Int32 Examples.PrimitiveArrays::ReadGenericHelper(System.Int32[],System.Int32) +x64-v3 System.Int32 Examples.PrimitiveArrays::ReadHelper(System.Int32[],System.Int32) +x64-v3 System.Int32 Examples.PrimitiveArrays::ReadInt32(System.Int32[],System.Int32) +x64-v3 System.Int32 Examples.PrimitiveArrays::WriteGenericHelper(System.Int32[],System.Int32,System.Int32) +x64-v3 System.Int32 Examples.PrimitiveOperations::Alias(System.Int16&,System.Int16&,System.Int32) +x64-v3 System.Int32 Examples.PrimitiveOperations::Choose(System.Int32) +x64-v3 System.Int32 Examples.PrimitiveOperations::PacketSize() +x64-v3 System.Int32 Examples.PrimitiveOperations::ReadWrite(System.Int32&,System.Int32) +x64-v3 System.Int32 Examples.SampleArrays::Count(Examples.Samples) +x64-v3 System.Int32 Examples.SampleArrays::CountAfterAllocation(Examples.Node,System.Int32[]) +x64-v3 System.Int32 Examples.SampleArrays::CountAfterDeposit(Examples.Account,System.Int32[]) +x64-v3 System.Int32 Examples.SampleArrays::CountAfterRefill(Examples.Samples,Examples.Samples,System.Int32[]) +x64-v3 System.Int32 Examples.SampleArrays::ReadAfterAllocation(Examples.Node,System.Int32[]) +x64-v3 System.Int32 Examples.SampleArrays::ReadAfterEcho(Examples.Account,System.Int32[],System.Int32) +x64-v3 System.Int32 Examples.SampleArrays::RecordIndex(Examples.Account,System.Int32) +x64-v3 System.Int32 Examples.SampleArrays::RefillRead(Examples.Samples,System.Int32[],System.Int32) +x64-v3 System.Int32 Examples.SampleArrays::Share(Examples.Samples,Examples.Samples) +x64-v3 System.Int32 Examples.SampleArrays::Sum(Examples.Samples,System.Int32[]) +x64-v3 System.Int32 Examples.Sequential::PackedSize() +x64-v3 System.Int32 Examples.Sequential::RecordSize() +x64-v3 System.Int32 Examples.SpanConstructors::AggregateLocal(Examples.AggregateSpans/Words&) +x64-v3 System.Int32 Examples.SpanConstructors::BytePointerLocal(System.Byte*,System.Int32) +x64-v3 System.Int32 Examples.SpanConstructors::LayoutLocal(Examples.AggregateSpans/VectorRecord*,System.Int32) +x64-v3 System.Int32 Examples.SpanConstructors::Local(System.Int32&) +x64-v3 System.Int32 Examples.SpanHelpers::Count(System.ReadOnlySpan`1) +x64-v3 System.Int32 Examples.SpanReferences::Argument(System.ReadOnlySpan`1) +x64-v3 System.Int32 Examples.SpanReferences::Caller(System.Byte[]) +x64-v3 System.Int32 Examples.SpanReferences::Copy(System.ReadOnlySpan`1&) +x64-v3 System.Int32 Examples.SpanReferences::First(System.Span`1&) +x64-v3 System.Int32 Examples.SpanReferences::Forward(System.ReadOnlySpan`1&) +x64-v3 System.Int32 Examples.SpanReferences::Front(System.Span`1&) +x64-v3 System.Int32 Examples.SpanReferences::GenericFront(System.Int32[]) +x64-v3 System.Int32 Examples.SpanReferences::Length(System.ReadOnlySpan`1&) +x64-v3 System.Int32 Examples.SpanReferences::Sum(System.ReadOnlySpan`1&) +x64-v3 System.Int32 Examples.SpanReferences::Tail(System.ReadOnlySpan`1&) +x64-v3 System.Int32 Examples.SpanReferences::Temporary(System.ReadOnlySpan`1) +x64-v3 System.Int32 Examples.SpanTables::Count() +x64-v3 System.Int32 Examples.StackAllocation::FillAndRead(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.StackAllocation::Length(System.Int32) +x64-v3 System.Int32 Examples.StackAllocation::UninitializedRead() +x64-v3 System.Int32 Examples.StackAllocation::WrittenBeforeRead(System.Int32) +x64-v3 System.Int32 Examples.StackAllocation::ZeroInitialized() +x64-v3 System.Int32 Examples.TailCalls::Excluded(System.Int32) +x64-v3 System.Int32 Examples.TailCalls::Forward(System.Int32) +x64-v3 System.Int32 Examples.TailCalls::ForwardReference(System.Int32&) +x64-v3 System.Int32 Examples.TailCalls::ForwardThrow(System.Int32) +x64-v3 System.Int32 Examples.TailCalls::Ignore(System.Int32&) +x64-v3 System.Int32 Examples.TailCalls::Increment(System.Int32) +x64-v3 System.Int32 Examples.TailCalls::LocalReference(System.Int32) +x64-v3 System.Int32 Examples.TailCalls::Raise(System.Int32) +x64-v3 System.Int32 Examples.TailCalls::Read(System.Int32&) +x64-v3 System.Int32 Examples.TileArrays::PairCount(Examples.Tiles) +x64-v3 System.Int32 Examples.TupleDivision::MathOutInt32(System.Int32,System.Int32,System.Int32&) +x64-v3 System.Int32 Examples.TupleDivision::MathQuotient(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.TupleDivision::MathWrongQuotient(System.Int32,System.Int32) +x64-v3 System.Int32 Examples.TypeTests::Classify(System.Int32) +x64-v3 System.Int32 Examples.TypeTests::Classify(System.Int32) +x64-v3 System.Int32 Examples.TypeTests::Marker(System.Int32) +x64-v3 System.Int32 Examples.TypeTests::Other(System.Int32) +x64-v3 System.Int32 Examples.TypeTests::ReferenceCases(System.Int32) +x64-v3 System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +x64-v3 System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +x64-v3 System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +x64-v3 System.Int32 Examples.UnsafeLayouts::Size0() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size1() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size10() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size11() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size12() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size13() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size14() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size15() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size16() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size17() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size18() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size2() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size3() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size4() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size5() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size6() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size7() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size8() +x64-v3 System.Int32 Examples.UnsafeLayouts::Size9() +x64-v3 System.Int32 Examples.VectorFields::NestedSize() +x64-v3 System.Int32 Examples.WordReader::Read(System.Int32[]) +x64-v3 System.Int32 Examples.WordReader::ReadSaved(System.Int32[]) +x64-v3 System.Int32 Examples.WordReader::SkipAndPeek(System.Int32[]) +x64-v3 System.Int32& Examples.ArrayElementReferences::Element(System.Int32[],System.Int32) +x64-v3 System.Int32& Examples.UnsafeLayouts::Add6int(System.Int32&,System.Int32) +x64-v3 System.Int32& Examples.UnsafeLayouts::Add6nint(System.Int32&,System.IntPtr) +x64-v3 System.Int32& Examples.UnsafeLayouts::Add6nuint(System.Int32&,System.UIntPtr) +x64-v3 System.Int32& Examples.ViewAccessors::ArrayData(System.Int32[]) +x64-v3 System.Int32* Examples.StackAllocation::EscapingStorage() +x64-v3 System.Int32[] Examples.PrimitiveArrays::Allocate(System.Int32) +x64-v3 System.Int32[] Examples.PrimitiveArrays::Identity(System.Int32[]) +x64-v3 System.Int32[] Examples.SampleArrays::ValuesOf(Examples.Samples) +x64-v3 System.Int64 Examples.Bits::PrimitiveCounts64(System.Int64) +x64-v3 System.Int64 Examples.Bits::PrimitiveLeft64(System.Int64,System.Int32) +x64-v3 System.Int64 Examples.Bits::PrimitiveRight64(System.Int64,System.Int32) +x64-v3 System.Int64 Examples.Bits::ReverseI64(System.Int64) +x64-v3 System.Int64 Examples.ExceptionDocumentation::CheckedAdd(System.Int64,System.Int64) +x64-v3 System.Int64 Examples.ExceptionDocumentation::Divide(System.Int64,System.Int64) +x64-v3 System.Int64 Examples.IntegerMath::AbsInt64(System.Int64) +x64-v3 System.Int64 Examples.IntegerMath::ClampInt64(System.Int64,System.Int64,System.Int64) +x64-v3 System.Int64 Examples.IntegerMath::MaxInt64(System.Int64,System.Int64) +x64-v3 System.Int64 Examples.IntegerMath::MinInt64(System.Int64,System.Int64) +x64-v3 System.Int64 Examples.IntegerMath::WrongMin(System.Int64,System.Int64) +x64-v3 System.Int64 Examples.IntegerOperations::Negate64(System.Int64) +x64-v3 System.Int64 Examples.OutlineCounters::SignedLiteral() +x64-v3 System.Int64 Examples.PointerOperations::Difference(System.Byte*,System.Byte*) +x64-v3 System.Int64 Examples.PooledItem::get_Payload() +x64-v3 System.Int64 Examples.PrimitiveArrays::ReadInt64(System.Int64[],System.Int32) +x64-v3 System.Int64 Examples.TupleDivision::MathOutInt64(System.Int64,System.Int64,System.Int64&) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure0int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure0nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure0nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure10int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure10nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure10nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure11int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure11nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure11nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure12int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure12nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure12nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure13int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure13nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure13nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure14int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure14nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure14nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure15int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure15nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure15nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure16int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure16nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure16nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure17int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure17nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure17nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure18int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure18nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure18nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure1int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure1nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure1nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure2int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure2nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure2nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure3int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure3nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure3nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure4int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure4nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure4nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure5int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure5nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure5nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure6int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure6nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure6nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure7int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure7nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure7nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure8int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure8nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure8nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure9int(System.Int32) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure9nint(System.IntPtr) +x64-v3 System.Int64 Examples.UnsafeLayouts::Measure9nuint(System.UIntPtr) +x64-v3 System.Int64 Examples.WideProduct::BigMul32(System.Int32,System.Int32) +x64-v3 System.Int64 Examples.WideProduct::SignedBigMul(System.Int64,System.Int64,System.Int64&) +x64-v3 System.Int64& Examples.UnsafeLayouts::Add8int(System.Int64&,System.Int32) +x64-v3 System.Int64& Examples.UnsafeLayouts::Add8nint(System.Int64&,System.IntPtr) +x64-v3 System.Int64& Examples.UnsafeLayouts::Add8nuint(System.Int64&,System.UIntPtr) +x64-v3 System.Int64[] Examples.PrimitiveArrays::AllocateLongs(System.Int32) +x64-v3 System.IntPtr Examples.Arguments::Native(System.IntPtr) +x64-v3 System.IntPtr Examples.Bits::PrimitiveCountsNative(System.IntPtr) +x64-v3 System.IntPtr Examples.Bits::PrimitiveLeftNative(System.IntPtr,System.Int32) +x64-v3 System.IntPtr Examples.Bits::PrimitiveRightNative(System.IntPtr,System.Int32) +x64-v3 System.IntPtr Examples.Bits::ReverseNative(System.IntPtr) +x64-v3 System.IntPtr Examples.GenericMethods::Identity(System.IntPtr) +x64-v3 System.IntPtr Examples.GenericMethods::NativeIdentity(System.IntPtr) +x64-v3 System.IntPtr Examples.IntegerMath::AbsNative(System.IntPtr) +x64-v3 System.IntPtr Examples.IntegerMath::ClampNative(System.IntPtr,System.IntPtr,System.IntPtr) +x64-v3 System.IntPtr Examples.IntegerMath::MaxNative(System.IntPtr,System.IntPtr) +x64-v3 System.IntPtr Examples.IntegerMath::MinNative(System.IntPtr,System.IntPtr) +x64-v3 System.IntPtr Examples.MemorySpans::NativeRead(System.ReadOnlySpan`1,System.Int32) +x64-v3 System.IntPtr Examples.NativeIntegers::Add(System.IntPtr,System.IntPtr) +x64-v3 System.IntPtr Examples.NativeIntegers::CheckedAdd(System.IntPtr,System.IntPtr) +x64-v3 System.IntPtr Examples.NativeIntegers::Choose(System.IntPtr) +x64-v3 System.IntPtr Examples.NativeIntegers::Divide(System.IntPtr,System.IntPtr) +x64-v3 System.IntPtr Examples.NativeIntegers::Fields(Examples.NativePair&,System.IntPtr,System.UIntPtr) +x64-v3 System.IntPtr Examples.NativeIntegers::Increment(System.IntPtr&) +x64-v3 System.IntPtr Examples.NativeIntegers::LocalCall(System.IntPtr) +x64-v3 System.IntPtr Examples.NativeIntegers::ReadWrite(System.IntPtr&,System.IntPtr) +x64-v3 System.IntPtr Examples.NativeIntegers::Widen(System.Int32) +x64-v3 System.IntPtr Examples.PrimitiveArrays::ReadIntPtr(System.IntPtr[],System.Int32) +x64-v3 System.IntPtr Examples.UnsafeReferences::ByteOffset(System.Byte&,System.Byte&) +x64-v3 System.IntPtr& Examples.UnsafeLayouts::Add10int(System.IntPtr&,System.Int32) +x64-v3 System.IntPtr& Examples.UnsafeLayouts::Add10nint(System.IntPtr&,System.IntPtr) +x64-v3 System.IntPtr& Examples.UnsafeLayouts::Add10nuint(System.IntPtr&,System.UIntPtr) +x64-v3 System.ReadOnlySpan`1 Examples.AggregateArrays::ReadOnlyView(Examples.AggregateArrays/Pair[]) +x64-v3 System.ReadOnlySpan`1 Examples.AggregateSpans::ReadOnlyFactory(Examples.AggregateSpans/Words&,System.Int32) +x64-v3 System.ReadOnlySpan`1 Examples.AggregateSpans::WordsView(System.ReadOnlySpan`1) +x64-v3 System.ReadOnlySpan`1 Examples.SpanConstructors::PassReadOnly(System.ReadOnlySpan`1) +x64-v3 System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyDefaultReset(Examples.AggregateSpans/Words&) +x64-v3 System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyEmpty() +x64-v3 System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyBytes(System.ReadOnlySpan`1) +x64-v3 System.ReadOnlySpan`1 Examples.StaticSpans::get_Lookup() +x64-v3 System.ReadOnlySpan`1 Examples.MemorySpans::ArrayRange(System.Int32[],System.Int32,System.Int32) +x64-v3 System.ReadOnlySpan`1 Examples.MemorySpans::ArrayReadOnly(System.Int32[]) +x64-v3 System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlySingle(System.Int32&) +x64-v3 System.ReadOnlySpan`1> Examples.SpanConstructors::Vector(System.Runtime.Intrinsics.Vector128`1&) +x64-v3 System.ReadOnlySpan`1> Examples.SpanConstructors::VectorPointer(System.Runtime.Intrinsics.Vector128`1*,System.Int32) +x64-v3 System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyWords(System.ReadOnlySpan`1) +x64-v3 System.ReadOnlySpan`1 Examples.SpanTables::get_Words() +x64-v3 System.ReadOnlySpan`1 Examples.SpanTables::get_Powers() +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::Shuffle128(System.ReadOnlySpan`1,System.ReadOnlySpan`1) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.MarshaledMemory::ReadVector(System.ReadOnlySpan`1) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::Read128(System.Byte&) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::ReadAligned128(System.Byte*) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128Byte(Examples.ValueBitCasts/Pair) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadBytes128(Examples.VectorFields/Bytes128) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadInts128(Examples.VectorFields/Ints128) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt32(Examples.ValueBitCasts/Pair) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.AggregateArrays::ReadVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.AggregateSpans::ReadVector(System.ReadOnlySpan`1>) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::MultiplyEven128(System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::UnsafeReturn(System.UInt64) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Identity>(System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Vector(System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.UnsafeReferences::ReadHalf(Examples.FourLimbs&) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt64(Examples.ValueBitCasts/Pair) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Read128(Examples.VectorFields/Holder128) +x64-v3 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Write128(Examples.VectorFields/Holder128&,System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17int(System.Runtime.Intrinsics.Vector128`1&,System.Int32) +x64-v3 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nint(System.Runtime.Intrinsics.Vector128`1&,System.IntPtr) +x64-v3 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nuint(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +x64-v3 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::Half(Examples.FourLimbs&) +x64-v3 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::SubtractVector(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::Shuffle256(System.ReadOnlySpan`1,System.ReadOnlySpan`1) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::ShuffleCrossHalf(System.ReadOnlySpan`1,System.ReadOnlySpan`1) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::ShuffleIgnoreHighBit(System.ReadOnlySpan`1,System.ReadOnlySpan`1) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int32(Examples.ValueBitCasts/Limbs) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int64(Examples.ValueBitCasts/Limbs) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadLongs256(Examples.VectorFields/Longs256) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::CreateWords(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt32(Examples.ValueBitCasts/Limbs) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadUInts256(Examples.VectorFields/UInts256) +x64-v3 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeReferences::Vector(Examples.FourLimbs&) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::CopyLocal(Examples.ValueBitCasts/Limbs) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::ReadLocal(System.Runtime.Intrinsics.Vector256`1&) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt64(Examples.ValueBitCasts/Limbs) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Read256(Examples.VectorFields/Holder256) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Reinterpret(Examples.VectorFields/Words256&,System.Runtime.Intrinsics.Vector256`1) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Write256(Examples.VectorFields/Holder256&,System.Runtime.Intrinsics.Vector256`1) +x64-v3 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::WritePacked(Examples.VectorFields/Packed256&,System.Runtime.Intrinsics.Vector256`1) +x64-v3 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18int(System.Runtime.Intrinsics.Vector256`1&,System.Int32) +x64-v3 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nint(System.Runtime.Intrinsics.Vector256`1&,System.IntPtr) +x64-v3 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nuint(System.Runtime.Intrinsics.Vector256`1&,System.UIntPtr) +x64-v3 System.Runtime.Intrinsics.Vector256`1& Examples.VectorFields::Address256(Examples.VectorFields/Holder256&) +x64-v3 System.Runtime.Intrinsics.Vector256`1[] Examples.PrimitiveArrays::AllocateVector(System.Int32) +x64-v3 System.Runtime.Intrinsics.Vector512`1 Examples.UnalignedMemory::Read512(System.Byte&) +x64-v3 System.SByte Examples.Bits::ReverseI8(System.SByte) +x64-v3 System.SByte Examples.IntegerMath::AbsInt8(System.SByte) +x64-v3 System.SByte Examples.IntegerMath::ClampInt8(System.SByte,System.SByte,System.SByte) +x64-v3 System.SByte Examples.IntegerMath::MaxInt8(System.SByte,System.SByte) +x64-v3 System.SByte Examples.IntegerMath::MinInt8(System.SByte,System.SByte) +x64-v3 System.SByte Examples.PrimitiveArrays::ReadSByte(System.SByte[],System.Int32) +x64-v3 System.SByte Examples.TupleDivision::MathSByteRemainder(System.SByte,System.SByte) +x64-v3 System.SByte Examples.UnalignedMemory::ReadSigned(System.Byte&) +x64-v3 System.SByte& Examples.UnsafeLayouts::Add2int(System.SByte&,System.Int32) +x64-v3 System.SByte& Examples.UnsafeLayouts::Add2nint(System.SByte&,System.IntPtr) +x64-v3 System.SByte& Examples.UnsafeLayouts::Add2nuint(System.SByte&,System.UIntPtr) +x64-v3 System.Span`1 Examples.AggregateArrays::Slice(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +x64-v3 System.Span`1 Examples.AggregateArrays::View(Examples.AggregateArrays/Pair[]) +x64-v3 System.Span`1 Examples.AggregateSpans::Factory(Examples.AggregateSpans/Words&,System.Int32) +x64-v3 System.Span`1 Examples.AggregateSpans::Slice(System.Span`1,System.Int32,System.Int32) +x64-v3 System.Span`1 Examples.SpanConstructors::Aggregate(Examples.AggregateSpans/Words&) +x64-v3 System.Span`1 Examples.SpanConstructors::Pointer(Examples.AggregateSpans/Words*,System.Int32) +x64-v3 System.Span`1 Examples.EnumBuffers::Slice(System.Span`1,System.Int32) +x64-v3 System.Span`1 Examples.ReinterpretedSpans::EnumWords(System.Span`1) +x64-v3 System.Span`1 Examples.MemorySpans::ArrayAsSpan(System.Boolean[]) +x64-v3 System.Span`1 Examples.AggregateSpans::Bytes(System.Span`1) +x64-v3 System.Span`1 Examples.AggregateSpans::LayoutBytes(System.Span`1) +x64-v3 System.Span`1 Examples.AggregateSpans::VectorBytes(System.Span`1>) +x64-v3 System.Span`1 Examples.ReinterpretedSpans::Bytes(System.Span`1) +x64-v3 System.Span`1 Examples.ReinterpretedSpans::EnumBytes(System.Span`1) +x64-v3 System.Span`1 Examples.ReinterpretedSpans::Expanded(System.Span`1) +x64-v3 System.Span`1 Examples.MemorySpans::ArrayConstructor(System.Char[]) +x64-v3 System.Span`1 Examples.MemorySpans::ArrayWritable(System.Int32[]) +x64-v3 System.Span`1 Examples.ReinterpretedSpans::SameWidth(System.Span`1) +x64-v3 System.Span`1 Examples.SpanConstructors::DefaultReset(System.Int32&) +x64-v3 System.Span`1 Examples.SpanConstructors::Empty() +x64-v3 System.Span`1 Examples.SpanConstructors::Pass(System.Span`1) +x64-v3 System.Span`1 Examples.SpanConstructors::Single(System.Int32&) +x64-v3 System.Span`1 Examples.MemorySpans::ArrayAsSpanStart(System.IntPtr[],System.Int32) +x64-v3 System.Span`1> Examples.AggregateArrays::VectorView(System.Runtime.Intrinsics.Vector128`1[]) +x64-v3 System.Span`1 Examples.ReinterpretedSpans::Words(System.Span`1) +x64-v3 System.Span`1 Examples.MemorySpans::ArrayAsSpanRange(System.UIntPtr[],System.Int32,System.Int32) +x64-v3 System.String Examples.Guards::Required(System.String) +x64-v3 System.String Examples.Strings::OrNone(System.String) +x64-v3 System.String Examples.Strings::Pick(System.Boolean,System.String,System.String) +x64-v3 System.String Examples.Strings::Relabel(Examples.Labelled,System.String) +x64-v3 System.String[] Examples.TagArrays::Create(System.Int32) +x64-v3 System.UInt16 Examples.Bits::ReverseU16(System.UInt16) +x64-v3 System.UInt16 Examples.IntegerMath::ClampUInt16(System.UInt16,System.UInt16,System.UInt16) +x64-v3 System.UInt16 Examples.IntegerMath::MaxUInt16(System.UInt16,System.UInt16) +x64-v3 System.UInt16 Examples.IntegerMath::MinUInt16(System.UInt16,System.UInt16) +x64-v3 System.UInt16 Examples.PrimitiveArrays::ReadUInt16(System.UInt16[],System.Int32) +x64-v3 System.UInt16& Examples.UnsafeLayouts::Add5int(System.UInt16&,System.Int32) +x64-v3 System.UInt16& Examples.UnsafeLayouts::Add5nint(System.UInt16&,System.IntPtr) +x64-v3 System.UInt16& Examples.UnsafeLayouts::Add5nuint(System.UInt16&,System.UIntPtr) +x64-v3 System.UInt32 Examples.Arguments::Address(System.UInt32) +x64-v3 System.UInt32 Examples.Arguments::Alias(System.UInt32) +x64-v3 System.UInt32 Examples.Arguments::CallerIsolation(System.UInt32) +x64-v3 System.UInt32 Examples.Arguments::Overwrite(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.Arguments::ReassignReference(System.UInt32&,System.UInt32&) +x64-v3 System.UInt32 Examples.Arithmetic::AtMostFive(System.UInt32) +x64-v3 System.UInt32 Examples.Bits::Left32(System.UInt32,System.Int32) +x64-v3 System.UInt32 Examples.Bits::PrimitiveLeadingU32(System.UInt32) +x64-v3 System.UInt32 Examples.Bits::PrimitiveLeftU32(System.UInt32,System.Int32) +x64-v3 System.UInt32 Examples.Bits::PrimitiveLogU32(System.UInt32) +x64-v3 System.UInt32 Examples.Bits::PrimitivePopulationU32(System.UInt32) +x64-v3 System.UInt32 Examples.Bits::PrimitiveRightU32(System.UInt32,System.Int32) +x64-v3 System.UInt32 Examples.Bits::PrimitiveTrailingU32(System.UInt32) +x64-v3 System.UInt32 Examples.Bits::ReverseU32(System.UInt32) +x64-v3 System.UInt32 Examples.Bits::Right32(System.UInt32,System.Int32) +x64-v3 System.UInt32 Examples.Bits::RoundUp32(System.UInt32) +x64-v3 System.UInt32 Examples.Capabilities::AesFlags() +x64-v3 System.UInt32 Examples.Capabilities::ArmFlags() +x64-v3 System.UInt32 Examples.Capabilities::Flags() +x64-v3 System.UInt32 Examples.Capabilities::VbmiFlags() +x64-v3 System.UInt32 Examples.CheckedArithmetic::Add(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.CheckedConversions::Narrow(System.UInt64) +x64-v3 System.UInt32 Examples.Division::Quotient(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.ExceptionDocumentation::Profile(System.UInt32) +x64-v3 System.UInt32 Examples.ExceptionDocumentation::SpanItem(System.Int32) +x64-v3 System.UInt32 Examples.GenericMethods::ClearPacked(System.UInt32) +x64-v3 System.UInt32 Examples.GenericMethods::SwapPacked(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.GenericOwners::Address(System.UInt32) +x64-v3 System.UInt32 Examples.GenericOwners::Combined(System.UInt32) +x64-v3 System.UInt32 Examples.GenericOwners::Identity(System.UInt32) +x64-v3 System.UInt32 Examples.GenericOwners::Nested(System.UInt32) +x64-v3 System.UInt32 Examples.GenericOwners::NestedHelper(System.UInt32) +x64-v3 System.UInt32 Examples.GenericOwners::Repeated(System.UInt32) +x64-v3 System.UInt32 Examples.Guards::Index(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.IntegerMath::ClampUInt32(System.UInt32,System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.IntegerMath::MaxUInt32(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.IntegerMath::MinUInt32(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.Loops::AddTwo(System.UInt32) +x64-v3 System.UInt32 Examples.Loops::Bump(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.Loops::CopyUntilZero(System.ReadOnlySpan`1,System.UInt32&) +x64-v3 System.UInt32 Examples.Loops::CountCalls(System.UInt32) +x64-v3 System.UInt32 Examples.Loops::CountUntil(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.Loops::CountUpToFive(System.UInt32) +x64-v3 System.UInt32 Examples.Loops::Countdown(System.UInt32) +x64-v3 System.UInt32 Examples.Loops::Decrement(System.UInt32) +x64-v3 System.UInt32 Examples.Loops::DownThenUp(System.UInt32) +x64-v3 System.UInt32 Examples.Loops::InnerCount(Examples.Loops/Outer,System.UInt32) +x64-v3 System.UInt32 Examples.Loops::ReachFive(System.UInt32) +x64-v3 System.UInt32 Examples.Loops::TallyTwice(System.UInt32) +x64-v3 System.UInt32 Examples.Loops::WrongCountdown(System.UInt32) +x64-v3 System.UInt32 Examples.NarrowTotals::ReadSum() +x64-v3 System.UInt32 Examples.NarrowTotals::WriteRead(System.UInt32) +x64-v3 System.UInt32 Examples.NativeIntegers::NarrowUnsigned(System.UIntPtr) +x64-v3 System.UInt32 Examples.Nested::Packed(System.UInt32) +x64-v3 System.UInt32 Examples.OwnerHelper`1::Identity(System.UInt32) +x64-v3 System.UInt32 Examples.OwnerHelper`1::Select(System.UInt32,System.UInt64) +x64-v3 System.UInt32 Examples.OwnerOuter`1/Helper::Identity(System.UInt32) +x64-v3 System.UInt32 Examples.OwnerOuter`1/Inner`1::Identity(System.UInt32) +x64-v3 System.UInt32 Examples.PrimitiveArrays::ReadUInt32(System.UInt32[],System.Int32) +x64-v3 System.UInt32 Examples.ReinterpretedSpans::Aliased(System.Span`1) +x64-v3 System.UInt32 Examples.Sequential::ReadWrite(Examples.Record&,System.UInt32) +x64-v3 System.UInt32 Examples.SpanTables::Word(System.Int32) +x64-v3 System.UInt32 Examples.Throws::ActualValue(System.UInt32) +x64-v3 System.UInt32 Examples.Throws::ArgumentInner(System.UInt32) +x64-v3 System.UInt32 Examples.Throws::Inner(System.UInt32) +x64-v3 System.UInt32 Examples.Throws::Null(System.UInt32) +x64-v3 System.UInt32 Examples.Throws::Percent(System.UInt32) +x64-v3 System.UInt32 Examples.Throws::Quotient(System.UInt32,System.UInt32) +x64-v3 System.UInt32 Examples.WordReader::RangeEnd() +x64-v3 System.UInt32 Examples.WordReader::ReplaceRemaining(System.UInt32) +x64-v3 System.UInt32 Examples.WordReader::Reserve() +x64-v3 System.UInt32 SelectorExamples.One.Collision::Add(System.UInt32,System.UInt32) +x64-v3 System.UInt32& Examples.UnsafeLayouts::Add7int(System.UInt32&,System.Int32) +x64-v3 System.UInt32& Examples.UnsafeLayouts::Add7nint(System.UInt32&,System.IntPtr) +x64-v3 System.UInt32& Examples.UnsafeLayouts::Add7nuint(System.UInt32&,System.UIntPtr) +x64-v3 System.UInt64 Examples.Account::get_Headroom() +x64-v3 System.UInt64 Examples.AddStep::Apply(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.Aliases::Update(System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.Animal::Loudness() +x64-v3 System.UInt64 Examples.Arguments::Struct(Examples.Packet) +x64-v3 System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.Arithmetic::Alias(System.UInt64&,System.UInt64&) +x64-v3 System.UInt64 Examples.Arithmetic::Calls(System.UInt64) +x64-v3 System.UInt64 Examples.Arithmetic::Choose(System.Boolean,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.Arithmetic::Distance(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.Arithmetic::Increment(System.UInt64) +x64-v3 System.UInt64 Examples.Arithmetic::LargeStruct(System.UInt64) +x64-v3 System.UInt64 Examples.Arithmetic::PastEnd(System.UInt64&) +x64-v3 System.UInt64 Examples.Arithmetic::Read(Examples.Packet&) +x64-v3 System.UInt64 Examples.Arithmetic::ReadLarge(Examples.LargePacket&) +x64-v3 System.UInt64 Examples.Arithmetic::ReadValue(Examples.Packet) +x64-v3 System.UInt64 Examples.Arithmetic::Struct(System.UInt64) +x64-v3 System.UInt64 Examples.Arithmetic::StructCopy(System.UInt64) +x64-v3 System.UInt64 Examples.Arithmetic::TypeCheck(System.UInt64) +x64-v3 System.UInt64 Examples.Arithmetic::WrongAdd(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.BinarySpans::ReadBig(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.BinarySpans::ReadLittle(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.BinarySpans::TryReadBig(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.BinarySpans::TryReadLittle(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.Bird::Legs() +x64-v3 System.UInt64 Examples.Bits::Left64(System.UInt64,System.Int32) +x64-v3 System.UInt64 Examples.Bits::PrimitiveCountsU64(System.UInt64) +x64-v3 System.UInt64 Examples.Bits::PrimitiveLeftU64(System.UInt64,System.Int32) +x64-v3 System.UInt64 Examples.Bits::PrimitiveRightU64(System.UInt64,System.Int32) +x64-v3 System.UInt64 Examples.Bits::ReverseU64(System.UInt64) +x64-v3 System.UInt64 Examples.Bits::Right64(System.UInt64,System.Int32) +x64-v3 System.UInt64 Examples.Bits::RoundTrip(System.UInt64) +x64-v3 System.UInt64 Examples.Bits::RoundUp64(System.UInt64) +x64-v3 System.UInt64 Examples.Bits::WrongRoundTrip(System.UInt64) +x64-v3 System.UInt64 Examples.Blocks::CopyLocal(System.UInt64,System.UInt32) +x64-v3 System.UInt64 Examples.Blocks::CopyParameter(System.UInt64,System.UInt64,System.UInt32) +x64-v3 System.UInt64 Examples.Blocks::FillLocal(System.Byte,System.UInt32) +x64-v3 System.UInt64 Examples.Blocks::FillParameter(System.UInt64,System.Byte,System.UInt32) +x64-v3 System.UInt64 Examples.CheckedArithmetic::Add64(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.ComputedFlags::TraceBoth(System.UInt64) +x64-v3 System.UInt64 Examples.ComputedFlags::TraceChained(System.UInt64) +x64-v3 System.UInt64 Examples.ComputedFlags::TraceGated(System.UInt64) +x64-v3 System.UInt64 Examples.ComputedFlags::TraceNotOff(System.UInt64) +x64-v3 System.UInt64 Examples.ComputedFlags::TraceSilent(System.UInt64) +x64-v3 System.UInt64 Examples.ConstantFlags::Count(System.UInt64) +x64-v3 System.UInt64 Examples.ConstantFlags::CountOn(System.UInt64) +x64-v3 System.UInt64 Examples.ConstantFlags::Trace(System.UInt64) +x64-v3 System.UInt64 Examples.ConstantFlags::TraceOff(System.UInt64) +x64-v3 System.UInt64 Examples.ConstantFlags::TraceThenCount(System.UInt64) +x64-v3 System.UInt64 Examples.Constructors::Checked(System.UInt64) +x64-v3 System.UInt64 Examples.Constructors::Default() +x64-v3 System.UInt64 Examples.Constructors::Nested(System.UInt64) +x64-v3 System.UInt64 Examples.Constructors::Packed(System.UInt64) +x64-v3 System.UInt64 Examples.Constructors::Read(System.UInt64) +x64-v3 System.UInt64 Examples.Counter::Add(System.UInt64) +x64-v3 System.UInt64 Examples.Counter::Mix(System.UInt64,System.UInt32,System.UInt16,System.Byte) +x64-v3 System.UInt64 Examples.Counter::Read() +x64-v3 System.UInt64 Examples.Counter::WriteRead(System.UInt64) +x64-v3 System.UInt64 Examples.Counter::WrongWriteRead(System.UInt64) +x64-v3 System.UInt64 Examples.DerivedFlags::Count(System.UInt64) +x64-v3 System.UInt64 Examples.DerivedFlags::CountLoud(System.UInt64) +x64-v3 System.UInt64 Examples.DerivedFlags::Trace(System.UInt64) +x64-v3 System.UInt64 Examples.DerivedFlags::TraceQuiet(System.UInt64) +x64-v3 System.UInt64 Examples.Division::Quotient64(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::And(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::AndNot(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::DefinedLocal(System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::Or(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::PrunedUnsafeLocal(System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::ReadLow(System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::ShiftLeft(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::ShiftRight(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::UnsafeCall(System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::UnsafeHigh(System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::UnsafeLow(System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::UnsafeLowNext(System.UInt64) +x64-v3 System.UInt64 Examples.DivisionIntrinsics::Xor(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.Dog::Legs() +x64-v3 System.UInt64 Examples.Dog::Loudness() +x64-v3 System.UInt64 Examples.ExceptionDocumentation::DivRem(System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.ExceptionDocumentation::UnsignedDivide(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.GenericEnvelope`1::Read() +x64-v3 System.UInt64 Examples.GenericLayouts::Composed(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.GenericLayouts::Field(Examples.GenericPair`1&) +x64-v3 System.UInt64 Examples.GenericLayouts::Roundtrip(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.GenericMethods::Choose(System.Boolean,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.GenericMethods::First(System.UInt64,System.UInt32) +x64-v3 System.UInt64 Examples.GenericMethods::Forward(System.UInt64) +x64-v3 System.UInt64 Examples.GenericMethods::Identity64(System.UInt64) +x64-v3 System.UInt64 Examples.GenericMethods::Identity(System.UInt64) +x64-v3 System.UInt64 Examples.GenericMethods::Instance(System.UInt64) +x64-v3 System.UInt64 Examples.GenericMethods::Nested(System.UInt64) +x64-v3 System.UInt64 Examples.GenericMethods::Repeated(System.UInt64) +x64-v3 System.UInt64 Examples.GenericMethods::Select(System.Boolean,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.GenericMethods::TwoTypes(System.UInt64,System.UInt32) +x64-v3 System.UInt64 Examples.GenericOwners::NestedSpanFirst(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.GenericOwners::OwnerSpanFirst(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.GenericOwners::SpanFirst(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.GenericOwners::SpanSecond(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.GenericPair`1::GetSecond() +x64-v3 System.UInt64 Examples.GenericPicker::Pick(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::Count>(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::CountWide(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::Pad,System.UInt32>(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::Pad,System.UInt64>(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::PadNarrow(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::PadWide(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::Trace>(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::TraceNarrow(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::Width,System.UInt32>(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::Width,System.UInt64>(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::WidthNarrow(System.UInt64) +x64-v3 System.UInt64 Examples.GenericReceivers::WidthWide(System.UInt64) +x64-v3 System.UInt64 Examples.IMeasureFive::Examples.IMeasure.Measure(System.UInt64) +x64-v3 System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +x64-v3 System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +x64-v3 System.UInt64 Examples.IScale::Scale(System.UInt64) +x64-v3 System.UInt64 Examples.IScaleMore::Examples.IScale.Scale(System.UInt64) +x64-v3 System.UInt64 Examples.IScaleMost::Examples.IScale.Scale(System.UInt64) +x64-v3 System.UInt64 Examples.IScaleSide::Examples.IScale.Scale(System.UInt64) +x64-v3 System.UInt64 Examples.InParameters::Constrained(Examples.InWord&,Examples.InWord&) +x64-v3 System.UInt64 Examples.InParameters::ConstrainedSum(Examples.InWord&,System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.InParameters::Sum(Examples.InWord&,System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.InWord::Examples.IInWord.Sum(System.ReadOnlySpan`1& modreq(System.Runtime.InteropServices.InAttribute)) +x64-v3 System.UInt64 Examples.InitCounter::get_Value() +x64-v3 System.UInt64 Examples.IntegerMath::ClampUInt64(System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.IntegerMath::MaxUInt64(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.IntegerMath::MinUInt64(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.Loops::Sum(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.Loops::SumIndexed(System.UInt64[]) +x64-v3 System.UInt64 Examples.Loops::SumThroughLocal(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.Loops::SumThroughSpan(System.UInt64[]) +x64-v3 System.UInt64 Examples.Loops::TwoStructs(System.UInt32) +x64-v3 System.UInt64 Examples.MarshaledMemory::Read(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.Measured`1::Width(System.UInt64) +x64-v3 System.UInt64 Examples.Measured`1::Width(System.UInt64) +x64-v3 System.UInt64 Examples.MostSpecificBodies::Five(System.UInt64) +x64-v3 System.UInt64 Examples.MostSpecificBodies::Measured(System.UInt64) +x64-v3 System.UInt64 Examples.MostSpecificBodies::More(System.UInt64) +x64-v3 System.UInt64 Examples.MostSpecificBodies::Most(System.UInt64) +x64-v3 System.UInt64 Examples.MostSpecificBodies::Own(System.UInt64) +x64-v3 System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +x64-v3 System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +x64-v3 System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +x64-v3 System.UInt64 Examples.Nested::Alias(System.UInt64) +x64-v3 System.UInt64 Examples.Nested::Copy(System.UInt64) +x64-v3 System.UInt64 Examples.Nested::Explicit(System.UInt64) +x64-v3 System.UInt64 Examples.Nested::Pass(System.UInt64) +x64-v3 System.UInt64 Examples.Nested::Read(Examples.Leaf) +x64-v3 System.UInt64 Examples.Nested::ReadOuter(Examples.NestedRecord) +x64-v3 System.UInt64 Examples.Nested::Sized(System.UInt64) +x64-v3 System.UInt64 Examples.Nested::Snapshot(System.UInt64) +x64-v3 System.UInt64 Examples.Nested::WrongCopy(System.UInt64) +x64-v3 System.UInt64 Examples.NestedConstructor::Sum() +x64-v3 System.UInt64 Examples.NullReferences::Optional(Examples.FourLimbs&,System.Boolean) +x64-v3 System.UInt64 Examples.NullReferences::Read() +x64-v3 System.UInt64 Examples.NullReferences::ReadField() +x64-v3 System.UInt64 Examples.NullReferences::Unchecked(Examples.FourLimbs&,System.Boolean) +x64-v3 System.UInt64 Examples.Objects::BalanceOf(Examples.Account) +x64-v3 System.UInt64 Examples.Objects::Describe(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.Objects::Fresh(System.UInt32) +x64-v3 System.UInt64 Examples.Objects::IdOf(Examples.Shape) +x64-v3 System.UInt64 Examples.Objects::LinkedValue(Examples.Node,Examples.Node) +x64-v3 System.UInt64 Examples.Objects::Replace(Examples.Account,System.UInt64) +x64-v3 System.UInt64 Examples.Objects::SecondValue(Examples.Node) +x64-v3 System.UInt64 Examples.Objects::UnderFive(Examples.Account) +x64-v3 System.UInt64 Examples.Objects::Withdraw(Examples.Account,System.UInt64) +x64-v3 System.UInt64 Examples.Odometer::Next() +x64-v3 System.UInt64 Examples.OutlineCountdown::Drain() +x64-v3 System.UInt64 Examples.OutlineCountdown::DrainAtLeastOnce() +x64-v3 System.UInt64 Examples.OutlineCounters::ExchangeAdd(System.UInt64) +x64-v3 System.UInt64 Examples.OutlineCounters::UnsignedLiteral() +x64-v3 System.UInt64 Examples.OutlineCounters::WrongExchangeAdd(System.UInt64) +x64-v3 System.UInt64 Examples.OwnerHelper`1::First(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.OwnerOuter`1/Inner`1::First(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.PackedConstructor::Sum() +x64-v3 System.UInt64 Examples.PointerOperations::WriteEnum(Examples.Unsigned64*,System.UInt64) +x64-v3 System.UInt64 Examples.PrimitiveArrays::ReadUInt64(System.UInt64[],System.Int32) +x64-v3 System.UInt64 Examples.ScaleOwn::Scale(System.UInt64) +x64-v3 System.UInt64 Examples.Sequential::Packed(System.UInt64) +x64-v3 System.UInt64 Examples.SkipInitialization::Read(System.UInt64&) +x64-v3 System.UInt64 Examples.SpanHelpers::First(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanHelpers::Second(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.SpanReferences::Back(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanReferences::BigEndianWord(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanReferences::ByteSecond(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanReferences::Construct(System.ReadOnlySpan`1) +x64-v3 System.UInt64 Examples.SpanReferences::CursorSum(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanReferences::Element(System.ReadOnlySpan`1&,System.Int32) +x64-v3 System.UInt64 Examples.SpanReferences::FirstHigh(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanReferences::GenericHead(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanReferences::Head(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanReferences::High(System.Span`1&,System.Int32) +x64-v3 System.UInt64 Examples.SpanReferences::HighAddress(System.ReadOnlySpan`1&,System.Int32) +x64-v3 System.UInt64 Examples.SpanReferences::ReadOnlyHigh(System.ReadOnlySpan`1&,System.Int32) +x64-v3 System.UInt64 Examples.SpanReferences::Second(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanReferences::Word(System.ReadOnlySpan`1&) +x64-v3 System.UInt64 Examples.SpanTables::First() +x64-v3 System.UInt64 Examples.SpanTables::Power(System.Int32) +x64-v3 System.UInt64 Examples.StaticInterfaces::AddCounted(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.StaticInterfaces::XorPlain(System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.StaticValues::Read(Examples.Word256&) +x64-v3 System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.Allowance&) +x64-v3 System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.DoublingAllowance&) +x64-v3 System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.RefundingAllowance&) +x64-v3 System.UInt64 Examples.StaticVirtualDefaults::SpendDefault(Examples.Allowance&) +x64-v3 System.UInt64 Examples.StaticVirtualDefaults::SpendOverride(Examples.DoublingAllowance&) +x64-v3 System.UInt64 Examples.StaticVirtualDefaults::SpendRefunding(Examples.RefundingAllowance&) +x64-v3 System.UInt64 Examples.StructMethods::Arguments(System.UInt64) +x64-v3 System.UInt64 Examples.StructMethods::ByValue(System.UInt64) +x64-v3 System.UInt64 Examples.StructMethods::ConstructLocal(System.UInt64) +x64-v3 System.UInt64 Examples.StructMethods::Initialized(System.UInt64) +x64-v3 System.UInt64 Examples.StructMethods::Mutate(System.UInt64) +x64-v3 System.UInt64 Examples.StructMethods::Replace(System.UInt64) +x64-v3 System.UInt64 Examples.TriplePrice::get_Price() +x64-v3 System.UInt64 Examples.TupleDivision::Deconstruct(System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.TupleDivision::Item1(System.ValueTuple`2&) +x64-v3 System.UInt64 Examples.TupleDivision::Local(System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.TupleDivision::Quotient(System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.TupleDivision::Remainder(System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.TupleDivision::WrongQuotient(System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.UnalignedMemory::ReadAlignedWord(System.Byte*) +x64-v3 System.UInt64 Examples.UnalignedMemory::ReadPointerWord(System.Byte*) +x64-v3 System.UInt64 Examples.UnalignedMemory::ReadWord(System.Byte&) +x64-v3 System.UInt64 Examples.UnsafeReferences::PastEnd(Examples.FourLimbs&) +x64-v3 System.UInt64 Examples.UnsafeReferences::Read(Examples.FourLimbs&) +x64-v3 System.UInt64 Examples.UnsafeReferences::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.UInt64 Examples.VirtualCalls::DogLegs() +x64-v3 System.UInt64 Examples.VirtualCalls::LegsOf(Examples.Animal) +x64-v3 System.UInt64 Examples.VirtualCalls::LoudnessOf(Examples.Animal) +x64-v3 System.UInt64 Examples.WideProduct::BigMul(System.UInt64,System.UInt64,System.UInt64&) +x64-v3 System.UInt64 Examples.WideProduct::UnsignedBigMul32(System.UInt32,System.UInt32) +x64-v3 System.UInt64 Examples.WordReader::ReplaceRemainingWide(System.UInt32) +x64-v3 System.UInt64 Examples.XorStep::Examples.IBinaryStep.Apply(System.UInt64,System.UInt64) +x64-v3 System.UInt64 SelectorExamples.One.Collision::Add(System.UInt64,System.UInt64) +x64-v3 System.UInt64 SelectorExamples.Two.Collision::Add(System.UInt64,System.UInt64) +x64-v3 System.UInt64& Examples.Arithmetic::Escape() +x64-v3 System.UInt64& Examples.UnsafeLayouts::Add9int(System.UInt64&,System.Int32) +x64-v3 System.UInt64& Examples.UnsafeLayouts::Add9nint(System.UInt64&,System.IntPtr) +x64-v3 System.UInt64& Examples.UnsafeLayouts::Add9nuint(System.UInt64&,System.UIntPtr) +x64-v3 System.UInt64& Examples.UnsafeReferences::Pointer(System.Byte*) +x64-v3 System.UInt64& Examples.UnsafeReferences::SubtractElements32(System.UInt64&,System.Int32) +x64-v3 System.UInt64& Examples.UnsafeReferences::SubtractElementsNative(System.UInt64&,System.IntPtr) +x64-v3 System.UInt64& Examples.UnsafeReferences::SubtractElementsUnsigned(System.UInt64&,System.UIntPtr) +x64-v3 System.UInt64& Examples.UnsafeReferences::Word(Examples.FourLimbs&) +x64-v3 System.UIntPtr Examples.Bits::LeftNative(System.UIntPtr,System.Int32) +x64-v3 System.UIntPtr Examples.Bits::PrimitiveCountsUNative(System.UIntPtr) +x64-v3 System.UIntPtr Examples.Bits::PrimitiveLeftUNative(System.UIntPtr,System.Int32) +x64-v3 System.UIntPtr Examples.Bits::PrimitiveRightUNative(System.UIntPtr,System.Int32) +x64-v3 System.UIntPtr Examples.Bits::ReverseUNative(System.UIntPtr) +x64-v3 System.UIntPtr Examples.Bits::RightNative(System.UIntPtr,System.Int32) +x64-v3 System.UIntPtr Examples.Bits::RoundUpNative(System.UIntPtr) +x64-v3 System.UIntPtr Examples.IntegerMath::ClampUNative(System.UIntPtr,System.UIntPtr,System.UIntPtr) +x64-v3 System.UIntPtr Examples.IntegerMath::MaxUNative(System.UIntPtr,System.UIntPtr) +x64-v3 System.UIntPtr Examples.IntegerMath::MinUNative(System.UIntPtr,System.UIntPtr) +x64-v3 System.UIntPtr Examples.MemorySpans::NativeWrite(System.Span`1,System.Int32,System.UIntPtr) +x64-v3 System.UIntPtr Examples.NativeIntegers::AddUnsigned(System.UIntPtr,System.UIntPtr) +x64-v3 System.UIntPtr Examples.NativeIntegers::CheckedAddUnsigned(System.UIntPtr,System.UIntPtr) +x64-v3 System.UIntPtr Examples.NativeIntegers::DivideUnsigned(System.UIntPtr,System.UIntPtr) +x64-v3 System.UIntPtr Examples.NativeIntegers::ReadWriteUnsigned(System.UIntPtr&,System.UIntPtr) +x64-v3 System.UIntPtr Examples.NativeIntegers::Shift(System.UIntPtr,System.Int32) +x64-v3 System.UIntPtr Examples.NativeIntegers::WidenUnsigned(System.UInt32) +x64-v3 System.UIntPtr Examples.PrimitiveArrays::ReadUIntPtr(System.UIntPtr[],System.Int32) +x64-v3 System.UIntPtr& Examples.UnsafeLayouts::Add11int(System.UIntPtr&,System.Int32) +x64-v3 System.UIntPtr& Examples.UnsafeLayouts::Add11nint(System.UIntPtr&,System.IntPtr) +x64-v3 System.UIntPtr& Examples.UnsafeLayouts::Add11nuint(System.UIntPtr&,System.UIntPtr) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathByte(System.Byte,System.Byte) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathInt16(System.Int16,System.Int16) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathInt32(System.Int32,System.Int32) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathInt64(System.Int64,System.Int64) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathIntPtr(System.IntPtr,System.IntPtr) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathSByte(System.SByte,System.SByte) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathUInt16(System.UInt16,System.UInt16) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathUInt32(System.UInt32,System.UInt32) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathUInt64(System.UInt64,System.UInt64) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::Pair(System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.ValueTuple`2 Examples.TupleDivision::MathUIntPtr(System.UIntPtr,System.UIntPtr) +x64-v3 System.Void Examples.Account::.ctor() +x64-v3 System.Void Examples.Account::Deposit(System.UInt64) +x64-v3 System.Void Examples.AggregateArrays::Write(Examples.AggregateArrays/Pair[],System.Int32,Examples.AggregateArrays/Pair) +x64-v3 System.Void Examples.AggregateArrays::WriteVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32,System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Void Examples.AggregateSpanFill::Aliased(System.Span`1) +x64-v3 System.Void Examples.AggregateSpanFill::Generic(System.Span`1>,Examples.GenericPair`1) +x64-v3 System.Void Examples.AggregateSpanFill::Vector128(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Void Examples.AggregateSpanFill::Vector256(System.Span`1>,System.Runtime.Intrinsics.Vector256`1) +x64-v3 System.Void Examples.AggregateSpanFill::Vector512(System.Span`1>,System.Runtime.Intrinsics.Vector512`1) +x64-v3 System.Void Examples.AggregateSpanFill::Words(System.Span`1,Examples.AggregateSpans/Words) +x64-v3 System.Void Examples.AggregateSpans::Clear(System.Span`1) +x64-v3 System.Void Examples.AggregateSpans::Write(System.Span`1,Examples.AggregateSpans/Words) +x64-v3 System.Void Examples.AggregateSpans::WriteVector(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Void Examples.Animal::.ctor() +x64-v3 System.Void Examples.Arguments::Increment(System.UInt32&) +x64-v3 System.Void Examples.Arithmetic::Mutate(System.UInt64&) +x64-v3 System.Void Examples.BinarySpans::WriteBig(System.Span`1) +x64-v3 System.Void Examples.BinarySpans::WriteLittle(System.Span`1) +x64-v3 System.Void Examples.Bird::.ctor() +x64-v3 System.Void Examples.Blocks::Copy(System.Byte&,System.Byte&,System.UInt32) +x64-v3 System.Void Examples.Blocks::CopyAligned(System.Byte&,System.Byte&,System.UInt32) +x64-v3 System.Void Examples.Blocks::CopyPointer(System.Byte*,System.Byte*,System.UInt32) +x64-v3 System.Void Examples.Blocks::CopyPointerAligned(System.Byte*,System.Byte*,System.UInt32) +x64-v3 System.Void Examples.Blocks::Fill(System.Byte&,System.Byte,System.UInt32) +x64-v3 System.Void Examples.Blocks::FillAligned(System.Byte&,System.Byte,System.UInt32) +x64-v3 System.Void Examples.Blocks::FillPointer(System.Byte*,System.Byte,System.UInt32) +x64-v3 System.Void Examples.Blocks::FillPointerAligned(System.Byte*,System.Byte,System.UInt32) +x64-v3 System.Void Examples.Bucket::.ctor() +x64-v3 System.Void Examples.CheckedConstructor::.ctor(System.UInt64) +x64-v3 System.Void Examples.Circle::.ctor(System.UInt64,System.UInt64) +x64-v3 System.Void Examples.ConstructorLeaf::.ctor(System.UInt32) +x64-v3 System.Void Examples.Counter::.ctor(System.UInt64) +x64-v3 System.Void Examples.Counter::Increment(System.Byte&) +x64-v3 System.Void Examples.Counter::Replace(Examples.Counter) +x64-v3 System.Void Examples.Counter::Set(System.UInt64) +x64-v3 System.Void Examples.CounterValue::Increment() +x64-v3 System.Void Examples.DefaultConstructor::.ctor() +x64-v3 System.Void Examples.DivisionIntrinsics::Init128(System.Runtime.Intrinsics.Vector128`1&) +x64-v3 System.Void Examples.DivisionIntrinsics::Init256(System.Runtime.Intrinsics.Vector256`1&) +x64-v3 System.Void Examples.DivisionIntrinsics::Init512(System.Runtime.Intrinsics.Vector512`1&) +x64-v3 System.Void Examples.DivisionIntrinsics::SwapHalves(System.ReadOnlySpan`1,System.Span`1) +x64-v3 System.Void Examples.DivisionIntrinsics::UnsafeStore(System.Runtime.Intrinsics.Vector128`1&,System.UInt64) +x64-v3 System.Void Examples.Dog::.ctor() +x64-v3 System.Void Examples.Enums::Assign(Examples.Unsigned64&,Examples.Unsigned64) +x64-v3 System.Void Examples.ExceptionDocumentation::Reference(System.UInt64&) +x64-v3 System.Void Examples.ExplicitCounterValue::Examples.ICounterValue.Increment() +x64-v3 System.Void Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Increment() +x64-v3 System.Void Examples.Gauge::.ctor() +x64-v3 System.Void Examples.Gauge::Settle() +x64-v3 System.Void Examples.GenericEnvelope`1::.ctor(System.UInt64,System.UInt64) +x64-v3 System.Void Examples.GenericMethods::Clear(Examples.PackedLeaf&) +x64-v3 System.Void Examples.GenericMethods::Swap32(System.Int32&,System.Int32&) +x64-v3 System.Void Examples.GenericMethods::Swap(Examples.PackedLeaf&,Examples.PackedLeaf&) +x64-v3 System.Void Examples.GenericMethods::Swap(System.Int32&,System.Int32&) +x64-v3 System.Void Examples.GenericMethods::SwapRecord(Examples.PackedLeaf&,Examples.PackedLeaf&) +x64-v3 System.Void Examples.GenericPair`1::.ctor(Examples.GenericPair`1,Examples.GenericPair`1) +x64-v3 System.Void Examples.GenericPair`1::.ctor(System.UInt32,System.UInt32) +x64-v3 System.Void Examples.GenericPair`1::.ctor(System.UInt64,System.UInt64) +x64-v3 System.Void Examples.GenericPair`1::SetSecond(System.UInt64) +x64-v3 System.Void Examples.InParameters::Add(Examples.InWord&,Examples.InWord&,Examples.InWord&) +x64-v3 System.Void Examples.InWord::Examples.IInWord.Add(Examples.InWord& modreq(System.Runtime.InteropServices.InAttribute),Examples.InWord&) +x64-v3 System.Void Examples.ItemPool::.ctor(System.Int32) +x64-v3 System.Void Examples.ItemPool::Prewarm(System.Int32) +x64-v3 System.Void Examples.ItemPool::Return(Examples.PooledItem) +x64-v3 System.Void Examples.Labelled::.ctor() +x64-v3 System.Void Examples.Loops::Fill(System.Span`1,System.UInt32) +x64-v3 System.Void Examples.Loops::FillIndexed(System.UInt32[],System.UInt32) +x64-v3 System.Void Examples.Loops::FillThrough(System.Span`1,System.UInt32) +x64-v3 System.Void Examples.Loops::FillThroughSpan(System.UInt32[],System.UInt32) +x64-v3 System.Void Examples.Loops::FillWords(System.UInt64&,System.UInt64,System.Int32) +x64-v3 System.Void Examples.Loops::FillWordsDown(System.UInt64&,System.UInt64,System.Int32) +x64-v3 System.Void Examples.Loops::Put(System.Span`1,System.UInt32) +x64-v3 System.Void Examples.Loops::Stamp(System.UInt32&,System.UInt32) +x64-v3 System.Void Examples.Marker::.ctor() +x64-v3 System.Void Examples.MarshaledMemory::Write(System.Span`1,System.UInt64&) +x64-v3 System.Void Examples.MarshaledMemory::WriteAggregate(System.Span`1,Examples.FourLimbs&) +x64-v3 System.Void Examples.MemorySpans::ClearBytes(System.Span`1) +x64-v3 System.Void Examples.MemorySpans::FillCharacters(System.Span`1,System.Char) +x64-v3 System.Void Examples.MemorySpans::FillIntegers(System.Span`1,System.Int32) +x64-v3 System.Void Examples.MemorySpans::FillLongSlice(System.Span`1,System.Int32,System.Int32,System.Int64) +x64-v3 System.Void Examples.MemorySpans::Write(System.Span`1,System.Int32,System.Int32) +x64-v3 System.Void Examples.NestedConstructor::.ctor(System.UInt64) +x64-v3 System.Void Examples.Node::.ctor() +x64-v3 System.Void Examples.NodeArrays::Put(Examples.Shape[],System.Int32,Examples.Shape) +x64-v3 System.Void Examples.NodeArrays::Refill(Examples.Bucket,Examples.Node[]) +x64-v3 System.Void Examples.NullReferences::Write(System.UInt64) +x64-v3 System.Void Examples.NullReferences::WriteField(System.UInt64) +x64-v3 System.Void Examples.Objects::Bump(Examples.Account,System.UInt64) +x64-v3 System.Void Examples.Objects::BumpBoth(Examples.Account,Examples.Account,System.UInt64) +x64-v3 System.Void Examples.Objects::ClearTag(Examples.Tagged) +x64-v3 System.Void Examples.Objects::CopyTag(Examples.Tagged,Examples.Tagged) +x64-v3 System.Void Examples.Objects::DepositBoth(Examples.Account,Examples.Account,System.UInt64) +x64-v3 System.Void Examples.Objects::DepositFrom(Examples.Account,Examples.Account,System.UInt32) +x64-v3 System.Void Examples.Objects::DepositTimes(Examples.Account,System.UInt64,System.UInt32) +x64-v3 System.Void Examples.Objects::FillTag(Examples.Tagged,Examples.Tagged) +x64-v3 System.Void Examples.Objects::Link(Examples.Node,Examples.Node) +x64-v3 System.Void Examples.Objects::LinkBoth(Examples.Node,Examples.Node,Examples.Node) +x64-v3 System.Void Examples.Objects::Relink(Examples.Node,Examples.Node,Examples.Node) +x64-v3 System.Void Examples.Objects::SetLimit(Examples.Account,System.UInt32) +x64-v3 System.Void Examples.OutlineCountdown::Clear() +x64-v3 System.Void Examples.PackedConstructor::.ctor(System.UInt64) +x64-v3 System.Void Examples.PackedConstructor::.ctor(System.UInt64,System.Byte) +x64-v3 System.Void Examples.PooledItem::.ctor(Examples.ItemPool,System.Int32) +x64-v3 System.Void Examples.PooledItem::set_IsRented(System.Boolean) +x64-v3 System.Void Examples.PooledItem::set_Payload(System.Int64) +x64-v3 System.Void Examples.PrimitiveArrays::FillEnums(Examples.AllocationTag[],Examples.AllocationTag) +x64-v3 System.Void Examples.PrimitiveArrays::FillGeneric(Examples.AllocationTag[],Examples.AllocationTag) +x64-v3 System.Void Examples.PrimitiveArrays::FillGeneric(System.Int32[],System.Int32) +x64-v3 System.Void Examples.PrimitiveArrays::FillIntegers(System.Int32[],System.Int32) +x64-v3 System.Void Examples.PrimitiveArrays::FillRangeEnums(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +x64-v3 System.Void Examples.PrimitiveArrays::FillRangeGeneric(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +x64-v3 System.Void Examples.PrimitiveArrays::FillRangeGeneric(System.Int32[],System.Int32,System.Int32,System.Int32) +x64-v3 System.Void Examples.PrimitiveArrays::FillRangeIntegers(System.Int32[],System.Int32,System.Int32,System.Int32) +x64-v3 System.Void Examples.PrimitiveArrays::WriteBoolean(System.Boolean[],System.Int32,System.Boolean) +x64-v3 System.Void Examples.PrimitiveArrays::WriteByte(System.Byte[],System.Int32,System.Byte) +x64-v3 System.Void Examples.PrimitiveArrays::WriteChar(System.Char[],System.Int32,System.Char) +x64-v3 System.Void Examples.PrimitiveArrays::WriteGeneric(System.Int32[],System.Int32,System.Int32) +x64-v3 System.Void Examples.PrimitiveArrays::WriteInt16(System.Int16[],System.Int32,System.Int16) +x64-v3 System.Void Examples.PrimitiveArrays::WriteInt32(System.Int32[],System.Int32,System.Int32) +x64-v3 System.Void Examples.PrimitiveArrays::WriteInt64(System.Int64[],System.Int32,System.Int64) +x64-v3 System.Void Examples.PrimitiveArrays::WriteIntPtr(System.IntPtr[],System.Int32,System.IntPtr) +x64-v3 System.Void Examples.PrimitiveArrays::WriteSByte(System.SByte[],System.Int32,System.SByte) +x64-v3 System.Void Examples.PrimitiveArrays::WriteUInt16(System.UInt16[],System.Int32,System.UInt16) +x64-v3 System.Void Examples.PrimitiveArrays::WriteUInt32(System.UInt32[],System.Int32,System.UInt32) +x64-v3 System.Void Examples.PrimitiveArrays::WriteUInt64(System.UInt64[],System.Int32,System.UInt64) +x64-v3 System.Void Examples.PrimitiveArrays::WriteUIntPtr(System.UIntPtr[],System.Int32,System.UIntPtr) +x64-v3 System.Void Examples.SampleArrays::Refill(Examples.Samples,System.Int32[]) +x64-v3 System.Void Examples.Samples::.ctor() +x64-v3 System.Void Examples.Shape::.ctor(System.UInt64) +x64-v3 System.Void Examples.SkipInitialization::Skip(Examples.FourLimbs&) +x64-v3 System.Void Examples.SkipInitialization::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-v3 System.Void Examples.SpanCopies::Copy(System.Span`1,System.Span`1) +x64-v3 System.Void Examples.SpanCopies::CopyLongs(System.ReadOnlySpan`1,System.Span`1) +x64-v3 System.Void Examples.SpanCopies::CopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +x64-v3 System.Void Examples.SpanReferences/Limbs::.ctor(System.ReadOnlySpan`1&,System.Boolean) +x64-v3 System.Void Examples.SpanReferences::SetHigh(System.ReadOnlySpan`1&,System.UInt64) +x64-v3 System.Void Examples.SpanReferences::WriteFirst(System.Span`1&,System.Int32) +x64-v3 System.Void Examples.SpanReferences::WriteSecond(System.ReadOnlySpan`1&,System.UInt64) +x64-v3 System.Void Examples.SpanReferences::WriteWord(System.ReadOnlySpan`1&,System.UInt64) +x64-v3 System.Void Examples.Square::.ctor(System.UInt64,System.UInt64) +x64-v3 System.Void Examples.StaticSpans::Write() +x64-v3 System.Void Examples.StructMethods::ChangeCopy(Examples.Counter) +x64-v3 System.Void Examples.TagArrays::Put(System.String[],System.Int32,Examples.Tagged) +x64-v3 System.Void Examples.TagArrays::Stamp(System.String[],Examples.Tagged,System.UInt64) +x64-v3 System.Void Examples.TagArrays::Take(Examples.Tagged,System.String[],System.Int32) +x64-v3 System.Void Examples.Tagged::.ctor() +x64-v3 System.Void Examples.TailCalls::ForwardVoid(System.Int32&) +x64-v3 System.Void Examples.TailCalls::Store(System.Int32&) +x64-v3 System.Void Examples.Tally::.ctor(System.UInt32) +x64-v3 System.Void Examples.Throws::ThrowDivideByZero() +x64-v3 System.Void Examples.Throws::ThrowNull() +x64-v3 System.Void Examples.TileArrays::Relane(Examples.Tiles,System.Runtime.Intrinsics.Vector128`1[]) +x64-v3 System.Void Examples.Tiles::.ctor() +x64-v3 System.Void Examples.TypedCopy::Packet(Examples.Packet&,Examples.Packet&) +x64-v3 System.Void Examples.TypedCopy::Record(Examples.Record&,Examples.Record&) +x64-v3 System.Void Examples.TypedCopy::Vector128(System.Runtime.Intrinsics.Vector128`1&,System.Runtime.Intrinsics.Vector128`1&) +x64-v3 System.Void Examples.TypedCopy::Vector256(System.Runtime.Intrinsics.Vector256`1&,System.Runtime.Intrinsics.Vector256`1&) +x64-v3 System.Void Examples.UnalignedMemory::Write128(System.Byte&,System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Void Examples.UnalignedMemory::Write512(System.Byte&,System.Runtime.Intrinsics.Vector512`1) +x64-v3 System.Void Examples.UnalignedMemory::WriteAggregate(System.Byte&,Examples.FourLimbs) +x64-v3 System.Void Examples.UnalignedMemory::WriteAligned128(System.Byte*,System.Runtime.Intrinsics.Vector128`1) +x64-v3 System.Void Examples.UnalignedMemory::WriteAlignedAggregate(System.Byte*,Examples.FourLimbs) +x64-v3 System.Void Examples.UnalignedMemory::WriteAlignedWord(System.Byte*,System.UInt64) +x64-v3 System.Void Examples.UnalignedMemory::WriteHalf(System.Byte&,System.Int16) +x64-v3 System.Void Examples.UnalignedMemory::WritePointerAggregate(System.Byte*,Examples.FourLimbs) +x64-v3 System.Void Examples.UnalignedMemory::WritePointerWord(System.Byte*,System.UInt64) +x64-v3 System.Void Examples.UnalignedMemory::WriteSigned(System.Byte&,System.SByte) +x64-v3 System.Void Examples.UnalignedMemory::WriteWord(System.Byte&,System.UInt64) +x64-v3 System.Void Examples.WordReader::.ctor() +x64-v3 System.Void Examples.WordReader::Advance() +x64-v3 System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Examples.InitCounter::set_Value(System.UInt64) +x64-v3 method System.Int32 *(System.Int32)[] Examples.ManagedDispatch::NullTable() +x64-v4 Examples.Account Examples.Objects::FirstPresent(Examples.Account,Examples.Account) +x64-v4 Examples.AggregateArrays/Pair Examples.AggregateArrays::Read(Examples.AggregateArrays/Pair[],System.Int32) +x64-v4 Examples.AggregateArrays/Pair& Examples.AggregateArrays::Address(Examples.AggregateArrays/Pair[],System.Int32) +x64-v4 Examples.AggregateArrays/Pair& Examples.AggregateArrays::Data(Examples.AggregateArrays/Pair[]) +x64-v4 Examples.AggregateArrays/Pair[] Examples.TileArrays::PairsOf(Examples.Tiles) +x64-v4 Examples.AggregateSpans/VectorRecord Examples.AggregateSpans::LayoutRead(System.ReadOnlySpan`1) +x64-v4 Examples.AggregateSpans/Words Examples.AggregateSpans::Read(System.ReadOnlySpan`1) +x64-v4 Examples.AllocationTag[] Examples.PrimitiveArrays::AllocateUnnamedZero(System.Int32) +x64-v4 Examples.CheckedConstructor Examples.Constructors::MakeChecked(System.UInt64) +x64-v4 Examples.Counter Examples.Constructors::Return(System.UInt64) +x64-v4 Examples.Counter Examples.Constructors::WrongReturn(System.UInt64) +x64-v4 Examples.DefaultConstructor Examples.Constructors::MakeDefault() +x64-v4 Examples.Dog Examples.ClassTests::AsDog(Examples.Animal) +x64-v4 Examples.EnumBuffers/Large Examples.EnumBuffers::Wide(Examples.EnumBuffers/Large[],System.Int32) +x64-v4 Examples.EnumBuffers/Small Examples.EnumBuffers::Address(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +x64-v4 Examples.EnumBuffers/Small Examples.EnumBuffers::FillClear(Examples.EnumBuffers/Small[]) +x64-v4 Examples.EnumBuffers/Small Examples.EnumBuffers::Loop(Examples.EnumBuffers/Small[],System.Int32) +x64-v4 Examples.EnumBuffers/Small Examples.EnumBuffers::Read(System.ReadOnlySpan`1) +x64-v4 Examples.EnumBuffers/Small Examples.EnumBuffers::Roundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +x64-v4 Examples.EnumBuffers/Small Examples.EnumBuffers::SpanRoundtrip(Examples.EnumBuffers/Small[],System.Int32,Examples.EnumBuffers/Small) +x64-v4 Examples.FourLimbs Examples.MarshaledMemory::ReadAggregate(System.ReadOnlySpan`1) +x64-v4 Examples.FourLimbs Examples.UnalignedMemory::ReadAggregate(System.Byte&) +x64-v4 Examples.FourLimbs Examples.UnalignedMemory::ReadAlignedAggregate(System.Byte*) +x64-v4 Examples.FourLimbs Examples.UnalignedMemory::ReadPointerAggregate(System.Byte*) +x64-v4 Examples.FourLimbs& Examples.UnsafeReferences::Mutable(Examples.FourLimbs&) +x64-v4 Examples.FourLimbs& Examples.UnsafeReferences::SubtractAggregate(Examples.FourLimbs&,System.IntPtr) +x64-v4 Examples.GenericPair`1> Examples.GenericLayouts::Nested(System.UInt32,System.UInt32) +x64-v4 Examples.GenericPair`1 Examples.AggregateArrays::ReadGeneric(Examples.GenericPair`1[],System.Int32) +x64-v4 Examples.GenericPair`1 Examples.GenericLayouts::Create(System.UInt64,System.UInt64) +x64-v4 Examples.IndirectValues/Box`1 Examples.IndirectValues::Generic(Examples.IndirectValues/Box`1) +x64-v4 Examples.IndirectValues/Box`1 Examples.IndirectValues::Identity>(Examples.IndirectValues/Box`1) +x64-v4 Examples.IndirectValues/Pair Examples.IndirectValues::Identity(Examples.IndirectValues/Pair) +x64-v4 Examples.IndirectValues/Pair Examples.IndirectValues::Read(Examples.IndirectValues/Pair&) +x64-v4 Examples.IndirectValues/Pair Examples.IndirectValues::Record(Examples.IndirectValues/Pair) +x64-v4 Examples.IndirectValues/Pair Examples.IndirectValues::Reference(Examples.IndirectValues/Pair&) +x64-v4 Examples.ItemPool Examples.PooledItem::get_Owner() +x64-v4 Examples.Leaf Examples.Nested::Extract(Examples.NestedRecord) +x64-v4 Examples.Leaf Examples.Nested::WrongExtract(Examples.NestedRecord) +x64-v4 Examples.NestedConstructor Examples.Constructors::MakeNested(System.UInt64) +x64-v4 Examples.NestedRecord& Examples.UnsafeLayouts::Add15int(Examples.NestedRecord&,System.Int32) +x64-v4 Examples.NestedRecord& Examples.UnsafeLayouts::Add15nint(Examples.NestedRecord&,System.IntPtr) +x64-v4 Examples.NestedRecord& Examples.UnsafeLayouts::Add15nuint(Examples.NestedRecord&,System.UIntPtr) +x64-v4 Examples.NestedRecord[] Examples.PrimitiveArrays::AllocateNested(System.Int32) +x64-v4 Examples.Node Examples.NodeArrays::At(Examples.Node[],System.Int32) +x64-v4 Examples.Node Examples.NodeArrays::First(Examples.Bucket) +x64-v4 Examples.Node Examples.NodeArrays::Install(Examples.Node[],System.Int32,Examples.Node,System.UInt64) +x64-v4 Examples.Node Examples.NodeArrays::Pair(Examples.Node,Examples.Node) +x64-v4 Examples.Node Examples.NodeArrays::Shift(Examples.Node[],Examples.Node) +x64-v4 Examples.Node Examples.Objects::Detach(Examples.Node) +x64-v4 Examples.Node[] Examples.NodeArrays::Create(System.Int32) +x64-v4 Examples.PackedConstructor Examples.Constructors::MakePacked(System.UInt64) +x64-v4 Examples.PackedLeaf& Examples.GenericMethods::Next(Examples.PackedLeaf&,System.Int32) +x64-v4 Examples.PackedLeaf& Examples.GenericMethods::NextPacked(Examples.PackedLeaf&,System.Int32) +x64-v4 Examples.PackedLeaf& Examples.UnsafeLayouts::Add14int(Examples.PackedLeaf&,System.Int32) +x64-v4 Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nint(Examples.PackedLeaf&,System.IntPtr) +x64-v4 Examples.PackedLeaf& Examples.UnsafeLayouts::Add14nuint(Examples.PackedLeaf&,System.UIntPtr) +x64-v4 Examples.PackedLeaf[] Examples.PrimitiveArrays::AllocatePacked(System.Int32) +x64-v4 Examples.Packet& Examples.UnsafeLayouts::Add16int(Examples.Packet&,System.Int32) +x64-v4 Examples.Packet& Examples.UnsafeLayouts::Add16nint(Examples.Packet&,System.IntPtr) +x64-v4 Examples.Packet& Examples.UnsafeLayouts::Add16nuint(Examples.Packet&,System.UIntPtr) +x64-v4 Examples.PooledItem Examples.ItemPool::Rent() +x64-v4 Examples.Signed16 Examples.Enums::IdentityS16(Examples.Signed16) +x64-v4 Examples.Signed16 Examples.Enums::ThroughCall(Examples.Signed16) +x64-v4 Examples.Signed16 Examples.PointerOperations::ReadEnum(Examples.Signed16*,System.Int32) +x64-v4 Examples.Signed16[] Examples.PrimitiveArrays::AllocateSigned16(System.Int32) +x64-v4 Examples.Signed32 Examples.Enums::IdentityS32(Examples.Signed32) +x64-v4 Examples.Signed32[] Examples.PrimitiveArrays::AllocateSigned32(System.Int32) +x64-v4 Examples.Signed64 Examples.Enums::IdentityS64(Examples.Signed64) +x64-v4 Examples.Signed64[] Examples.PrimitiveArrays::AllocateSigned64(System.Int32) +x64-v4 Examples.Signed8 Examples.Enums::Field(Examples.Signed8) +x64-v4 Examples.Signed8 Examples.Enums::IdentityS8(Examples.Signed8) +x64-v4 Examples.Signed8 Examples.Enums::Read(Examples.EnumRecord&) +x64-v4 Examples.Signed8 Examples.GenericMethods::EnumIdentity(Examples.Signed8) +x64-v4 Examples.Signed8 Examples.GenericMethods::Identity(Examples.Signed8) +x64-v4 Examples.Signed8& Examples.UnsafeLayouts::Add12int(Examples.Signed8&,System.Int32) +x64-v4 Examples.Signed8& Examples.UnsafeLayouts::Add12nint(Examples.Signed8&,System.IntPtr) +x64-v4 Examples.Signed8& Examples.UnsafeLayouts::Add12nuint(Examples.Signed8&,System.UIntPtr) +x64-v4 Examples.Signed8[] Examples.PrimitiveArrays::AllocateSigned8(System.Int32) +x64-v4 Examples.Tiles Examples.TileArrays::Hold(Examples.AggregateArrays/Pair[],System.Runtime.Intrinsics.Vector128`1[]) +x64-v4 Examples.Unsigned16 Examples.Enums::IdentityU16(Examples.Unsigned16) +x64-v4 Examples.Unsigned16[] Examples.PrimitiveArrays::AllocateUnsigned16(System.Int32) +x64-v4 Examples.Unsigned32 Examples.Enums::IdentityU32(Examples.Unsigned32) +x64-v4 Examples.Unsigned32[] Examples.PrimitiveArrays::AllocateUnsigned32(System.Int32) +x64-v4 Examples.Unsigned64 Examples.Enums::IdentityU64(Examples.Unsigned64) +x64-v4 Examples.Unsigned64 Examples.Enums::Local(Examples.Unsigned64) +x64-v4 Examples.Unsigned64 Examples.Enums::SetFlag(Examples.Unsigned64) +x64-v4 Examples.Unsigned64 Examples.Enums::WrongSetFlag(Examples.Unsigned64) +x64-v4 Examples.Unsigned64& Examples.UnsafeLayouts::Add13int(Examples.Unsigned64&,System.Int32) +x64-v4 Examples.Unsigned64& Examples.UnsafeLayouts::Add13nint(Examples.Unsigned64&,System.IntPtr) +x64-v4 Examples.Unsigned64& Examples.UnsafeLayouts::Add13nuint(Examples.Unsigned64&,System.UIntPtr) +x64-v4 Examples.Unsigned64[] Examples.PrimitiveArrays::AllocateUnsigned64(System.Int32) +x64-v4 Examples.Unsigned8 Examples.Enums::IdentityU8(Examples.Unsigned8) +x64-v4 Examples.Unsigned8[] Examples.PrimitiveArrays::AllocateUnsigned8(System.Int32) +x64-v4 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int32(System.Runtime.Intrinsics.Vector256`1) +x64-v4 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256Int64(System.Runtime.Intrinsics.Vector256`1) +x64-v4 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt32(System.Runtime.Intrinsics.Vector256`1) +x64-v4 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::From256UInt64(System.Runtime.Intrinsics.Vector256`1) +x64-v4 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ParameterCopy(Examples.ValueBitCasts/Limbs) +x64-v4 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::ReadAggregate(Examples.ValueBitCasts/Limbs&) +x64-v4 Examples.ValueBitCasts/Limbs Examples.ValueBitCasts::RoundTrip(Examples.ValueBitCasts/Limbs) +x64-v4 Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128Byte(System.Runtime.Intrinsics.Vector128`1) +x64-v4 Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt32(System.Runtime.Intrinsics.Vector128`1) +x64-v4 Examples.ValueBitCasts/Pair Examples.ValueBitCasts::From128UInt64(System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Boolean Examples.AggregateSpans::Copy(System.ReadOnlySpan`1,System.Span`1) +x64-v4 System.Boolean Examples.AggregateSpans::IsEmpty(System.Span`1) +x64-v4 System.Boolean Examples.AggregateSpans::VectorIsEmpty(System.ReadOnlySpan`1>) +x64-v4 System.Boolean Examples.Allowance::Spend(Examples.Allowance&,System.UInt64) +x64-v4 System.Boolean Examples.Bits::PowerI32(System.Int32) +x64-v4 System.Boolean Examples.Bits::PowerI64(System.Int64) +x64-v4 System.Boolean Examples.Bits::PowerNative(System.IntPtr) +x64-v4 System.Boolean Examples.Bits::PowerU32(System.UInt32) +x64-v4 System.Boolean Examples.Bits::PowerU64(System.UInt64) +x64-v4 System.Boolean Examples.Bits::PowerUNative(System.UIntPtr) +x64-v4 System.Boolean Examples.Bits::PrimitivePower32(System.Int32) +x64-v4 System.Boolean Examples.Bits::PrimitivePower64(System.Int64) +x64-v4 System.Boolean Examples.Bits::PrimitivePowerNative(System.IntPtr) +x64-v4 System.Boolean Examples.Bits::PrimitivePowerU32(System.UInt32) +x64-v4 System.Boolean Examples.Bits::PrimitivePowerU64(System.UInt64) +x64-v4 System.Boolean Examples.Bits::PrimitivePowerUNative(System.UIntPtr) +x64-v4 System.Boolean Examples.ClassTests::IsDog(Examples.Animal) +x64-v4 System.Boolean Examples.ComputedFlags::get_Both() +x64-v4 System.Boolean Examples.ComputedFlags::get_Middle() +x64-v4 System.Boolean Examples.ComputedFlags::get_NotOff() +x64-v4 System.Boolean Examples.ComputedFlags::get_Outer() +x64-v4 System.Boolean Examples.DivisionIntrinsics::EqualBytes256(System.Runtime.Intrinsics.Vector256`1,System.Runtime.Intrinsics.Vector256`1) +x64-v4 System.Boolean Examples.DivisionIntrinsics::EqualSigned512(System.Runtime.Intrinsics.Vector512`1,System.Runtime.Intrinsics.Vector512`1) +x64-v4 System.Boolean Examples.DivisionIntrinsics::EqualWords128(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Boolean Examples.DivisionIntrinsics::UnsafeEquals(System.UInt64) +x64-v4 System.Boolean Examples.DoublingAllowance::Examples.IAllowance.Spend(Examples.DoublingAllowance&) +x64-v4 System.Boolean Examples.DoublingAllowance::Spend(Examples.DoublingAllowance&,System.UInt64) +x64-v4 System.Boolean Examples.Forced::get_IsActive() +x64-v4 System.Boolean Examples.Gate`1::get_Open() +x64-v4 System.Boolean Examples.Gate`1::get_Open() +x64-v4 System.Boolean Examples.Hush`1::get_Silent() +x64-v4 System.Boolean Examples.IAllowance`1::Spend(Examples.Allowance&) +x64-v4 System.Boolean Examples.ILoud::Examples.IQuietByDefault.get_IsActive() +x64-v4 System.Boolean Examples.ILoudByDefault::get_IsActive() +x64-v4 System.Boolean Examples.IQuiet::Examples.ILoudByDefault.get_IsActive() +x64-v4 System.Boolean Examples.IQuietByDefault::get_IsActive() +x64-v4 System.Boolean Examples.IRefundingAllowance`1::Examples.IAllowance.Spend(Examples.RefundingAllowance&) +x64-v4 System.Boolean Examples.IntegerOperations::Greater32(System.Int32,System.Int32) +x64-v4 System.Boolean Examples.IntegerOperations::Greater64(System.Int64,System.Int64) +x64-v4 System.Boolean Examples.ItemPool::IsIdle(Examples.PooledItem) +x64-v4 System.Boolean Examples.Loops::AllZero(System.ReadOnlySpan`1) +x64-v4 System.Boolean Examples.MarshaledMemory::TryRead(System.ReadOnlySpan`1,System.UInt64&) +x64-v4 System.Boolean Examples.MarshaledMemory::TryReadAggregate(System.ReadOnlySpan`1,Examples.FourLimbs&) +x64-v4 System.Boolean Examples.MarshaledMemory::TryWrite(System.Span`1,System.UInt64&) +x64-v4 System.Boolean Examples.MemorySpans::BooleanWrite(System.Span`1,System.Int32,System.Boolean) +x64-v4 System.Boolean Examples.NullReferences::IsLive(System.UInt64&) +x64-v4 System.Boolean Examples.NullReferences::IsNull() +x64-v4 System.Boolean Examples.Objects::HasTag(Examples.Tagged) +x64-v4 System.Boolean Examples.Objects::IsNull(Examples.Account) +x64-v4 System.Boolean Examples.Objects::IsPresent(Examples.Account) +x64-v4 System.Boolean Examples.Objects::Same(Examples.Account,Examples.Account) +x64-v4 System.Boolean Examples.Objects::SameTag(Examples.Tagged,Examples.Tagged) +x64-v4 System.Boolean Examples.Objects::TwoMarkers() +x64-v4 System.Boolean Examples.PointerOperations::Greater(System.Byte*,System.Byte*) +x64-v4 System.Boolean Examples.PointerOperations::GreaterEqual(System.Byte*,System.Byte*) +x64-v4 System.Boolean Examples.PointerOperations::Less(System.Byte*,System.Byte*) +x64-v4 System.Boolean Examples.PointerOperations::LessEqual(System.Byte*,System.Byte*) +x64-v4 System.Boolean Examples.PooledItem::get_IsRented() +x64-v4 System.Boolean Examples.PrimitiveArrays::ReadBoolean(System.Boolean[],System.Int32) +x64-v4 System.Boolean Examples.RefundingAllowance::Spend(Examples.RefundingAllowance&,System.UInt64) +x64-v4 System.Boolean Examples.SpanCopies::TryCopy(System.Span`1,System.Span`1) +x64-v4 System.Boolean Examples.SpanCopies::TryCopyEnum(System.ReadOnlySpan`1,System.Span`1) +x64-v4 System.Boolean Examples.SpanCopies::TryCopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +x64-v4 System.Boolean Examples.StepOff::get_IsActive() +x64-v4 System.Boolean Examples.StepOn::get_IsActive() +x64-v4 System.Boolean Examples.Strings::SameLabel(System.String,System.String) +x64-v4 System.Boolean Examples.Strings::SameText() +x64-v4 System.Boolean Examples.UnsafeReferences::AreSame(System.Byte&,System.Byte&) +x64-v4 System.Boolean Examples.UnsafeReferences::Greater(System.Byte&,System.Byte&) +x64-v4 System.Boolean Examples.UnsafeReferences::Less(System.Byte&,System.Byte&) +x64-v4 System.Boolean Examples.ViewAccessors::Empty(System.Span`1) +x64-v4 System.Boolean Examples.ViewAccessors::PinnableNull(System.Span`1) +x64-v4 System.Boolean Examples.ViewAccessors::ReadOnlyEmpty(System.ReadOnlySpan`1) +x64-v4 System.Boolean Examples.ViewAccessors::ReadOnlyPinnableNull(System.ReadOnlySpan`1) +x64-v4 System.Boolean Examples.ViewAccessors::ReadOnlyReferenceNull(System.ReadOnlySpan`1) +x64-v4 System.Boolean Examples.ViewAccessors::ReferenceNull(System.Span`1) +x64-v4 System.Boolean Examples.WordFlag`1::get_IsActive() +x64-v4 System.Boolean Examples.WordFlag`1::get_IsActive() +x64-v4 System.Boolean& Examples.UnsafeLayouts::Add0int(System.Boolean&,System.Int32) +x64-v4 System.Boolean& Examples.UnsafeLayouts::Add0nint(System.Boolean&,System.IntPtr) +x64-v4 System.Boolean& Examples.UnsafeLayouts::Add0nuint(System.Boolean&,System.UIntPtr) +x64-v4 System.Byte Examples.Arguments::Narrow(System.Byte) +x64-v4 System.Byte Examples.Bits::ReverseU8(System.Byte) +x64-v4 System.Byte Examples.Counter::Parameter(System.Byte) +x64-v4 System.Byte Examples.GenericMethods::Identity(System.Byte) +x64-v4 System.Byte Examples.IntegerMath::ClampUInt8(System.Byte,System.Byte,System.Byte) +x64-v4 System.Byte Examples.IntegerMath::MaxUInt8(System.Byte,System.Byte) +x64-v4 System.Byte Examples.IntegerMath::MinUInt8(System.Byte,System.Byte) +x64-v4 System.Byte Examples.PrimitiveArrays::ReadByte(System.Byte[],System.Int32) +x64-v4 System.Byte Examples.SpanReferences::At(System.ReadOnlySpan`1&,System.Int32) +x64-v4 System.Byte Examples.StaticSpans::First() +x64-v4 System.Byte Examples.StaticSpans::Last() +x64-v4 System.Byte Examples.StaticSpans::PastEnd() +x64-v4 System.Byte Examples.StructMethods::Narrow(System.Byte) +x64-v4 System.Byte& Examples.UnsafeLayouts::Add3int(System.Byte&,System.Int32) +x64-v4 System.Byte& Examples.UnsafeLayouts::Add3nint(System.Byte&,System.IntPtr) +x64-v4 System.Byte& Examples.UnsafeLayouts::Add3nuint(System.Byte&,System.UIntPtr) +x64-v4 System.Byte& Examples.UnsafeReferences::AddSigned(System.Byte&,System.IntPtr) +x64-v4 System.Byte& Examples.UnsafeReferences::AddUnsigned(System.Byte&,System.UIntPtr) +x64-v4 System.Byte& Examples.UnsafeReferences::Subtract(System.Byte&,System.IntPtr) +x64-v4 System.Byte& Examples.UnsafeReferences::SubtractUnsigned(System.Byte&,System.UIntPtr) +x64-v4 System.Byte* Examples.PointerOperations::Subtract(System.Byte*,System.Int32) +x64-v4 System.Byte* Examples.PointerOperations::SubtractNative(System.Byte*,System.IntPtr) +x64-v4 System.Byte* Examples.UnsafeReferences::AddPointer(System.Byte*,System.Int32) +x64-v4 System.Byte* Examples.UnsafeReferences::AddPointerAggregate(System.Byte*,System.Int32) +x64-v4 System.Byte* Examples.UnsafeReferences::SubtractPointer(System.Byte*,System.Int32) +x64-v4 System.Byte* Examples.UnsafeReferences::SubtractPointerAggregate(System.Byte*,System.Int32) +x64-v4 System.Byte[] Examples.PrimitiveArrays::AllocateBytes(System.Int32) +x64-v4 System.Char Examples.MemorySpans::CharacterWrite(System.Span`1,System.Int32,System.Char) +x64-v4 System.Char Examples.NarrowIntegers::Character(System.Int32) +x64-v4 System.Char Examples.PointerOperations::ReadCharacter(System.Char*,System.Int32) +x64-v4 System.Char Examples.PrimitiveArrays::ReadChar(System.Char[],System.Int32) +x64-v4 System.Char& Examples.UnsafeLayouts::Add1int(System.Char&,System.Int32) +x64-v4 System.Char& Examples.UnsafeLayouts::Add1nint(System.Char&,System.IntPtr) +x64-v4 System.Char& Examples.UnsafeLayouts::Add1nuint(System.Char&,System.UIntPtr) +x64-v4 System.Int16 Examples.Bits::ReverseI16(System.Int16) +x64-v4 System.Int16 Examples.IntegerMath::AbsInt16(System.Int16) +x64-v4 System.Int16 Examples.IntegerMath::ClampInt16(System.Int16,System.Int16,System.Int16) +x64-v4 System.Int16 Examples.IntegerMath::MaxInt16(System.Int16,System.Int16) +x64-v4 System.Int16 Examples.IntegerMath::MinInt16(System.Int16,System.Int16) +x64-v4 System.Int16 Examples.NarrowIntegers::Identity(System.Int16) +x64-v4 System.Int16 Examples.PrimitiveArrays::ReadInt16(System.Int16[],System.Int32) +x64-v4 System.Int16 Examples.UnalignedMemory::ReadHalf(System.Byte&) +x64-v4 System.Int16& Examples.UnsafeLayouts::Add4int(System.Int16&,System.Int32) +x64-v4 System.Int16& Examples.UnsafeLayouts::Add4nint(System.Int16&,System.IntPtr) +x64-v4 System.Int16& Examples.UnsafeLayouts::Add4nuint(System.Int16&,System.UIntPtr) +x64-v4 System.Int16[] Examples.PrimitiveArrays::AllocateShorts(System.Int32) +x64-v4 System.Int32 Examples.AggregateArrays::ConstructedLength(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Arithmetic::AtLeastFive(System.Int32) +x64-v4 System.Int32 Examples.Arithmetic::FiveToOne(System.Int32) +x64-v4 System.Int32 Examples.Arithmetic::Larger(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Arithmetic::WordAdjusted(System.Int32) +x64-v4 System.Int32 Examples.Arithmetic::WordSized(System.Int32) +x64-v4 System.Int32 Examples.ArrayElementReferences::Read(System.Int32[],System.Int32) +x64-v4 System.Int32 Examples.ArrayElementReferences::Write(System.Int32[],System.Int32,System.Int32) +x64-v4 System.Int32 Examples.BinarySpans::TryWriteBig(System.Span`1) +x64-v4 System.Int32 Examples.BinarySpans::TryWriteLittle(System.Span`1) +x64-v4 System.Int32 Examples.Bits::Leading32(System.UInt32) +x64-v4 System.Int32 Examples.Bits::Leading64(System.UInt64) +x64-v4 System.Int32 Examples.Bits::LeadingNative(System.UIntPtr) +x64-v4 System.Int32 Examples.Bits::Log32(System.UInt32) +x64-v4 System.Int32 Examples.Bits::Log64(System.UInt64) +x64-v4 System.Int32 Examples.Bits::LogNative(System.UIntPtr) +x64-v4 System.Int32 Examples.Bits::Population32(System.UInt32) +x64-v4 System.Int32 Examples.Bits::Population64(System.UInt64) +x64-v4 System.Int32 Examples.Bits::PopulationNative(System.UIntPtr) +x64-v4 System.Int32 Examples.Bits::PrimitiveLeading32(System.Int32) +x64-v4 System.Int32 Examples.Bits::PrimitiveLeft32(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Bits::PrimitiveNarrow16(System.UInt32) +x64-v4 System.Int32 Examples.Bits::PrimitiveNarrow8(System.UInt32) +x64-v4 System.Int32 Examples.Bits::PrimitiveNarrowRotate16(System.UInt32,System.Int32) +x64-v4 System.Int32 Examples.Bits::PrimitiveNarrowRotate8(System.UInt32,System.Int32) +x64-v4 System.Int32 Examples.Bits::PrimitiveNarrowSigned(System.UInt32) +x64-v4 System.Int32 Examples.Bits::PrimitivePopulation32(System.Int32) +x64-v4 System.Int32 Examples.Bits::PrimitiveRight32(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Bits::PrimitiveTrailing32(System.Int32) +x64-v4 System.Int32 Examples.Bits::ReverseI32(System.Int32) +x64-v4 System.Int32 Examples.Bits::Trailing32(System.UInt32) +x64-v4 System.Int32 Examples.Bits::Trailing64(System.UInt64) +x64-v4 System.Int32 Examples.Bits::TrailingNative(System.UIntPtr) +x64-v4 System.Int32 Examples.Bits::WrongLog64(System.UInt64) +x64-v4 System.Int32 Examples.Capabilities::ArmCount(System.UInt64) +x64-v4 System.Int32 Examples.Capabilities::Endian() +x64-v4 System.Int32 Examples.Capabilities::Platform() +x64-v4 System.Int32 Examples.Capabilities::ReferenceFree() +x64-v4 System.Int32 Examples.Capabilities::VbmiCount(System.UInt64) +x64-v4 System.Int32 Examples.CheckedArithmetic::AddSigned(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +x64-v4 System.Int32 Examples.ClosedInterfaceCounter::Examples.IClosedCounter.Read() +x64-v4 System.Int32 Examples.ConstrainedCalls::ClosedBoth(Examples.ClosedInterfaceCounter&) +x64-v4 System.Int32 Examples.ConstrainedCalls::ClosedGeneric(Examples.GenericInterfaceCounter`1&) +x64-v4 System.Int32 Examples.ConstrainedCalls::DirectGeneric(Examples.GenericCounterValue`1&) +x64-v4 System.Int32 Examples.ConstrainedCalls::Explicit(Examples.ExplicitCounterValue&) +x64-v4 System.Int32 Examples.ConstrainedCalls::Generic(Examples.GenericCounterValue`1&) +x64-v4 System.Int32 Examples.ConstrainedCalls::GenericExplicit(Examples.ExplicitGenericCounterValue`1&) +x64-v4 System.Int32 Examples.ConstrainedCalls::Implicit(Examples.CounterValue&) +x64-v4 System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.CounterValue&) +x64-v4 System.Int32 Examples.ConstrainedCalls::IncrementAndRead(Examples.ExplicitCounterValue&) +x64-v4 System.Int32 Examples.ConstrainedCalls::IncrementAndRead>(Examples.ExplicitGenericCounterValue`1&) +x64-v4 System.Int32 Examples.ConstrainedCalls::Mutating(Examples.CounterValue&) +x64-v4 System.Int32 Examples.ConstrainedCalls::MutatingExplicit(Examples.ExplicitCounterValue&) +x64-v4 System.Int32 Examples.ConstrainedCalls::Read(Examples.CounterValue&) +x64-v4 System.Int32 Examples.ConstrainedCalls::Read(Examples.ExplicitCounterValue&) +x64-v4 System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +x64-v4 System.Int32 Examples.ConstrainedCalls::Read>(Examples.GenericCounterValue`1&) +x64-v4 System.Int32 Examples.ConstrainedCalls::ReadClosedInt(Examples.ClosedInterfaceCounter&) +x64-v4 System.Int32 Examples.ConstrainedCalls::ReadClosedInt>(Examples.GenericInterfaceCounter`1&) +x64-v4 System.Int32 Examples.ConstrainedCalls::ReadClosedLong(Examples.ClosedInterfaceCounter&) +x64-v4 System.Int32 Examples.ConstrainedCalls::TwoGenericOwners(Examples.GenericCounterValue`1&,Examples.GenericCounterValue`1&) +x64-v4 System.Int32 Examples.CounterValue::Read() +x64-v4 System.Int32 Examples.DivisionIntrinsics::Greater(System.UInt64,System.UInt64) +x64-v4 System.Int32 Examples.DivisionIntrinsics::Less(System.UInt64,System.UInt64) +x64-v4 System.Int32 Examples.Enums::Branch(Examples.Signed8) +x64-v4 System.Int32 Examples.ExceptionDocumentation::Absolute(System.Int32) +x64-v4 System.Int32 Examples.ExceptionDocumentation::CheckedNarrow(System.Int64) +x64-v4 System.Int32 Examples.ExceptionDocumentation::Clamp(System.Int32,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.ExplicitCounterValue::Examples.ICounterValue.Read() +x64-v4 System.Int32 Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Read() +x64-v4 System.Int32 Examples.Gauge::BelowFive() +x64-v4 System.Int32 Examples.Gauge::Blend(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Gauge::Blended(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Gauge::Bounded(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Gauge::Capped(Examples.Gauge,System.Int32) +x64-v4 System.Int32 Examples.Gauge::CappedLevel() +x64-v4 System.Int32 Examples.Gauge::CappedSlack() +x64-v4 System.Int32 Examples.Gauge::Checked(Examples.Gauge,System.Int32) +x64-v4 System.Int32 Examples.Gauge::CheckedLevel() +x64-v4 System.Int32 Examples.Gauge::Clamp(System.Int32,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Gauge::Clamped(System.Int32,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Gauge::Current() +x64-v4 System.Int32 Examples.Gauge::Doubled(System.Int32) +x64-v4 System.Int32 Examples.Gauge::Floor(System.Int32) +x64-v4 System.Int32 Examples.Gauge::Floored(System.Int32) +x64-v4 System.Int32 Examples.Gauge::Gap(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Gauge::IsFive() +x64-v4 System.Int32 Examples.Gauge::Margin() +x64-v4 System.Int32 Examples.Gauge::Raise(Examples.Gauge,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Gauge::RaisedLevel() +x64-v4 System.Int32 Examples.Gauge::Saturate(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Gauge::Slack() +x64-v4 System.Int32 Examples.Gauge::Smaller(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.Gauge::Total(System.Int32[]) +x64-v4 System.Int32 Examples.Gauge::Twice(System.Int32) +x64-v4 System.Int32 Examples.Gauge::Within(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.GenericCounterValue`1::Echo(System.Int32) +x64-v4 System.Int32 Examples.GenericCounterValue`1::Read() +x64-v4 System.Int32 Examples.GenericCounterValue`1::Read() +x64-v4 System.Int32 Examples.GenericInterfaceCounter`1::Read() +x64-v4 System.Int32 Examples.GenericLayouts::TupleSize() +x64-v4 System.Int32 Examples.GenericLayouts::VectorSize() +x64-v4 System.Int32 Examples.GenericMethods::Identity32(System.Int32) +x64-v4 System.Int32 Examples.GenericMethods::Identity(System.Int32) +x64-v4 System.Int32 Examples.GenericMethods::Mixed(System.Int32,System.Byte) +x64-v4 System.Int32 Examples.GenericMethods::PackedSize() +x64-v4 System.Int32 Examples.GenericMethods::Read32(System.Int32&) +x64-v4 System.Int32 Examples.GenericMethods::Read(System.Int32&) +x64-v4 System.Int32 Examples.GenericMethods::Size() +x64-v4 System.Int32 Examples.GenericMethods::Size>() +x64-v4 System.Int32 Examples.GenericMethods::VectorSize() +x64-v4 System.Int32 Examples.GenericMethods::WrongIdentity(System.Int32) +x64-v4 System.Int32 Examples.GenericOwners::SpanCount(System.ReadOnlySpan`1) +x64-v4 System.Int32 Examples.Guards::Count(System.Int32) +x64-v4 System.Int32 Examples.IntegerMath::AbsInt32(System.Int32) +x64-v4 System.Int32 Examples.IntegerMath::ClampInt32(System.Int32,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.IntegerMath::MaxInt32(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.IntegerMath::MinInt32(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.IntegerMath::SignInt16(System.Int16) +x64-v4 System.Int32 Examples.IntegerMath::SignInt32(System.Int32) +x64-v4 System.Int32 Examples.IntegerMath::SignInt64(System.Int64) +x64-v4 System.Int32 Examples.IntegerMath::SignInt8(System.SByte) +x64-v4 System.Int32 Examples.IntegerMath::SignNative(System.IntPtr) +x64-v4 System.Int32 Examples.IntegerOperations::Negate32(System.Int32) +x64-v4 System.Int32 Examples.ItemPool::Trim(System.Int32) +x64-v4 System.Int32 Examples.ItemPool::get_Available() +x64-v4 System.Int32 Examples.ItemPool::get_Capacity() +x64-v4 System.Int32 Examples.ItemPool::get_Rented() +x64-v4 System.Int32 Examples.Loops::CountToFive(System.Int32) +x64-v4 System.Int32 Examples.Loops::Drain(System.ReadOnlySpan`1) +x64-v4 System.Int32 Examples.ManagedDispatch::LocalEscape(System.Int32) +x64-v4 System.Int32 Examples.ManagedDispatch::NullLength() +x64-v4 System.Int32 Examples.ManagedDispatch::Read(System.Int32&) +x64-v4 System.Int32 Examples.MemorySpans::ArraySliceWrite(System.Int32[],System.Int32,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.MemorySpans::Existing(System.Int32&,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.MemorySpans::InPlaceArrayConstructor(System.Int32[],System.Int32,System.Int32) +x64-v4 System.Int32 Examples.MemorySpans::InPlaceArrayLength(System.Int32[],System.Int32,System.Int32) +x64-v4 System.Int32 Examples.MemorySpans::InPlacePointerSpan(System.Int32*,System.Int32) +x64-v4 System.Int32 Examples.MemorySpans::Read(System.ReadOnlySpan`1,System.Int32) +x64-v4 System.Int32 Examples.MemorySpans::ReferenceWrite(System.Span`1,System.Int32) +x64-v4 System.Int32 Examples.MemorySpans::ReinitializeArraySpan(System.Int32[],System.Int32,System.Int32) +x64-v4 System.Int32 Examples.MemorySpans::SliceRead(System.ReadOnlySpan`1,System.Int32,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.MemorySpans::SliceWrite(System.Span`1,System.Int32,System.Int32,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.NarrowIntegers::Call(System.Int32) +x64-v4 System.Int32 Examples.NarrowIntegers::Fields(System.Int32) +x64-v4 System.Int32 Examples.NarrowIntegers::SignedByte(System.Int32) +x64-v4 System.Int32 Examples.NarrowIntegers::SignedShort(System.Int64) +x64-v4 System.Int32 Examples.NarrowIntegers::UnsignedShort(System.Int32) +x64-v4 System.Int32 Examples.NativeIntegers::Narrow(System.IntPtr) +x64-v4 System.Int32 Examples.NativeIntegers::SignedSize() +x64-v4 System.Int32 Examples.NativeIntegers::UnsignedSize() +x64-v4 System.Int32 Examples.NodeArrays::Count(Examples.Node[]) +x64-v4 System.Int32 Examples.PooledItem::get_Id() +x64-v4 System.Int32 Examples.PrimitiveArrays::AllocateAndWrite(System.Int32,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.PrimitiveArrays::AllocatePackedAndWrite(System.Int32,System.Int32,System.Int32) +x64-v4 System.Int32 Examples.PrimitiveArrays::Length(System.Int32[]) +x64-v4 System.Int32 Examples.PrimitiveArrays::ReadAlias(System.Int32[],System.Int32,System.Int32) +x64-v4 System.Int32 Examples.PrimitiveArrays::ReadGeneric(System.Int32[],System.Int32) +x64-v4 System.Int32 Examples.PrimitiveArrays::ReadGenericHelper(System.Int32[],System.Int32) +x64-v4 System.Int32 Examples.PrimitiveArrays::ReadHelper(System.Int32[],System.Int32) +x64-v4 System.Int32 Examples.PrimitiveArrays::ReadInt32(System.Int32[],System.Int32) +x64-v4 System.Int32 Examples.PrimitiveArrays::WriteGenericHelper(System.Int32[],System.Int32,System.Int32) +x64-v4 System.Int32 Examples.PrimitiveOperations::Alias(System.Int16&,System.Int16&,System.Int32) +x64-v4 System.Int32 Examples.PrimitiveOperations::Choose(System.Int32) +x64-v4 System.Int32 Examples.PrimitiveOperations::PacketSize() +x64-v4 System.Int32 Examples.PrimitiveOperations::ReadWrite(System.Int32&,System.Int32) +x64-v4 System.Int32 Examples.SampleArrays::Count(Examples.Samples) +x64-v4 System.Int32 Examples.SampleArrays::CountAfterAllocation(Examples.Node,System.Int32[]) +x64-v4 System.Int32 Examples.SampleArrays::CountAfterDeposit(Examples.Account,System.Int32[]) +x64-v4 System.Int32 Examples.SampleArrays::CountAfterRefill(Examples.Samples,Examples.Samples,System.Int32[]) +x64-v4 System.Int32 Examples.SampleArrays::ReadAfterAllocation(Examples.Node,System.Int32[]) +x64-v4 System.Int32 Examples.SampleArrays::ReadAfterEcho(Examples.Account,System.Int32[],System.Int32) +x64-v4 System.Int32 Examples.SampleArrays::RecordIndex(Examples.Account,System.Int32) +x64-v4 System.Int32 Examples.SampleArrays::RefillRead(Examples.Samples,System.Int32[],System.Int32) +x64-v4 System.Int32 Examples.SampleArrays::Share(Examples.Samples,Examples.Samples) +x64-v4 System.Int32 Examples.SampleArrays::Sum(Examples.Samples,System.Int32[]) +x64-v4 System.Int32 Examples.Sequential::PackedSize() +x64-v4 System.Int32 Examples.Sequential::RecordSize() +x64-v4 System.Int32 Examples.SpanConstructors::AggregateLocal(Examples.AggregateSpans/Words&) +x64-v4 System.Int32 Examples.SpanConstructors::BytePointerLocal(System.Byte*,System.Int32) +x64-v4 System.Int32 Examples.SpanConstructors::LayoutLocal(Examples.AggregateSpans/VectorRecord*,System.Int32) +x64-v4 System.Int32 Examples.SpanConstructors::Local(System.Int32&) +x64-v4 System.Int32 Examples.SpanHelpers::Count(System.ReadOnlySpan`1) +x64-v4 System.Int32 Examples.SpanReferences::Argument(System.ReadOnlySpan`1) +x64-v4 System.Int32 Examples.SpanReferences::Caller(System.Byte[]) +x64-v4 System.Int32 Examples.SpanReferences::Copy(System.ReadOnlySpan`1&) +x64-v4 System.Int32 Examples.SpanReferences::First(System.Span`1&) +x64-v4 System.Int32 Examples.SpanReferences::Forward(System.ReadOnlySpan`1&) +x64-v4 System.Int32 Examples.SpanReferences::Front(System.Span`1&) +x64-v4 System.Int32 Examples.SpanReferences::GenericFront(System.Int32[]) +x64-v4 System.Int32 Examples.SpanReferences::Length(System.ReadOnlySpan`1&) +x64-v4 System.Int32 Examples.SpanReferences::Sum(System.ReadOnlySpan`1&) +x64-v4 System.Int32 Examples.SpanReferences::Tail(System.ReadOnlySpan`1&) +x64-v4 System.Int32 Examples.SpanReferences::Temporary(System.ReadOnlySpan`1) +x64-v4 System.Int32 Examples.SpanTables::Count() +x64-v4 System.Int32 Examples.StackAllocation::FillAndRead(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.StackAllocation::Length(System.Int32) +x64-v4 System.Int32 Examples.StackAllocation::UninitializedRead() +x64-v4 System.Int32 Examples.StackAllocation::WrittenBeforeRead(System.Int32) +x64-v4 System.Int32 Examples.StackAllocation::ZeroInitialized() +x64-v4 System.Int32 Examples.TailCalls::Excluded(System.Int32) +x64-v4 System.Int32 Examples.TailCalls::Forward(System.Int32) +x64-v4 System.Int32 Examples.TailCalls::ForwardReference(System.Int32&) +x64-v4 System.Int32 Examples.TailCalls::ForwardThrow(System.Int32) +x64-v4 System.Int32 Examples.TailCalls::Ignore(System.Int32&) +x64-v4 System.Int32 Examples.TailCalls::Increment(System.Int32) +x64-v4 System.Int32 Examples.TailCalls::LocalReference(System.Int32) +x64-v4 System.Int32 Examples.TailCalls::Raise(System.Int32) +x64-v4 System.Int32 Examples.TailCalls::Read(System.Int32&) +x64-v4 System.Int32 Examples.TileArrays::PairCount(Examples.Tiles) +x64-v4 System.Int32 Examples.TupleDivision::MathOutInt32(System.Int32,System.Int32,System.Int32&) +x64-v4 System.Int32 Examples.TupleDivision::MathQuotient(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.TupleDivision::MathWrongQuotient(System.Int32,System.Int32) +x64-v4 System.Int32 Examples.TypeTests::Classify(System.Int32) +x64-v4 System.Int32 Examples.TypeTests::Classify(System.Int32) +x64-v4 System.Int32 Examples.TypeTests::Marker(System.Int32) +x64-v4 System.Int32 Examples.TypeTests::Other(System.Int32) +x64-v4 System.Int32 Examples.TypeTests::ReferenceCases(System.Int32) +x64-v4 System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +x64-v4 System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +x64-v4 System.Int32 Examples.TypeTests::ReferenceClassify(System.Int32) +x64-v4 System.Int32 Examples.UnsafeLayouts::Size0() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size1() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size10() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size11() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size12() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size13() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size14() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size15() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size16() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size17() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size18() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size2() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size3() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size4() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size5() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size6() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size7() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size8() +x64-v4 System.Int32 Examples.UnsafeLayouts::Size9() +x64-v4 System.Int32 Examples.VectorFields::NestedSize() +x64-v4 System.Int32 Examples.WordReader::Read(System.Int32[]) +x64-v4 System.Int32 Examples.WordReader::ReadSaved(System.Int32[]) +x64-v4 System.Int32 Examples.WordReader::SkipAndPeek(System.Int32[]) +x64-v4 System.Int32& Examples.ArrayElementReferences::Element(System.Int32[],System.Int32) +x64-v4 System.Int32& Examples.UnsafeLayouts::Add6int(System.Int32&,System.Int32) +x64-v4 System.Int32& Examples.UnsafeLayouts::Add6nint(System.Int32&,System.IntPtr) +x64-v4 System.Int32& Examples.UnsafeLayouts::Add6nuint(System.Int32&,System.UIntPtr) +x64-v4 System.Int32& Examples.ViewAccessors::ArrayData(System.Int32[]) +x64-v4 System.Int32* Examples.StackAllocation::EscapingStorage() +x64-v4 System.Int32[] Examples.PrimitiveArrays::Allocate(System.Int32) +x64-v4 System.Int32[] Examples.PrimitiveArrays::Identity(System.Int32[]) +x64-v4 System.Int32[] Examples.SampleArrays::ValuesOf(Examples.Samples) +x64-v4 System.Int64 Examples.Bits::PrimitiveCounts64(System.Int64) +x64-v4 System.Int64 Examples.Bits::PrimitiveLeft64(System.Int64,System.Int32) +x64-v4 System.Int64 Examples.Bits::PrimitiveRight64(System.Int64,System.Int32) +x64-v4 System.Int64 Examples.Bits::ReverseI64(System.Int64) +x64-v4 System.Int64 Examples.ExceptionDocumentation::CheckedAdd(System.Int64,System.Int64) +x64-v4 System.Int64 Examples.ExceptionDocumentation::Divide(System.Int64,System.Int64) +x64-v4 System.Int64 Examples.IntegerMath::AbsInt64(System.Int64) +x64-v4 System.Int64 Examples.IntegerMath::ClampInt64(System.Int64,System.Int64,System.Int64) +x64-v4 System.Int64 Examples.IntegerMath::MaxInt64(System.Int64,System.Int64) +x64-v4 System.Int64 Examples.IntegerMath::MinInt64(System.Int64,System.Int64) +x64-v4 System.Int64 Examples.IntegerMath::WrongMin(System.Int64,System.Int64) +x64-v4 System.Int64 Examples.IntegerOperations::Negate64(System.Int64) +x64-v4 System.Int64 Examples.OutlineCounters::SignedLiteral() +x64-v4 System.Int64 Examples.PointerOperations::Difference(System.Byte*,System.Byte*) +x64-v4 System.Int64 Examples.PooledItem::get_Payload() +x64-v4 System.Int64 Examples.PrimitiveArrays::ReadInt64(System.Int64[],System.Int32) +x64-v4 System.Int64 Examples.TupleDivision::MathOutInt64(System.Int64,System.Int64,System.Int64&) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure0int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure0nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure0nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure10int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure10nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure10nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure11int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure11nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure11nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure12int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure12nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure12nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure13int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure13nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure13nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure14int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure14nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure14nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure15int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure15nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure15nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure16int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure16nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure16nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure17int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure17nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure17nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure18int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure18nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure18nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure1int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure1nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure1nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure2int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure2nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure2nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure3int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure3nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure3nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure4int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure4nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure4nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure5int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure5nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure5nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure6int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure6nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure6nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure7int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure7nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure7nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure8int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure8nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure8nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure9int(System.Int32) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure9nint(System.IntPtr) +x64-v4 System.Int64 Examples.UnsafeLayouts::Measure9nuint(System.UIntPtr) +x64-v4 System.Int64 Examples.WideProduct::BigMul32(System.Int32,System.Int32) +x64-v4 System.Int64 Examples.WideProduct::SignedBigMul(System.Int64,System.Int64,System.Int64&) +x64-v4 System.Int64& Examples.UnsafeLayouts::Add8int(System.Int64&,System.Int32) +x64-v4 System.Int64& Examples.UnsafeLayouts::Add8nint(System.Int64&,System.IntPtr) +x64-v4 System.Int64& Examples.UnsafeLayouts::Add8nuint(System.Int64&,System.UIntPtr) +x64-v4 System.Int64[] Examples.PrimitiveArrays::AllocateLongs(System.Int32) +x64-v4 System.IntPtr Examples.Arguments::Native(System.IntPtr) +x64-v4 System.IntPtr Examples.Bits::PrimitiveCountsNative(System.IntPtr) +x64-v4 System.IntPtr Examples.Bits::PrimitiveLeftNative(System.IntPtr,System.Int32) +x64-v4 System.IntPtr Examples.Bits::PrimitiveRightNative(System.IntPtr,System.Int32) +x64-v4 System.IntPtr Examples.Bits::ReverseNative(System.IntPtr) +x64-v4 System.IntPtr Examples.GenericMethods::Identity(System.IntPtr) +x64-v4 System.IntPtr Examples.GenericMethods::NativeIdentity(System.IntPtr) +x64-v4 System.IntPtr Examples.IntegerMath::AbsNative(System.IntPtr) +x64-v4 System.IntPtr Examples.IntegerMath::ClampNative(System.IntPtr,System.IntPtr,System.IntPtr) +x64-v4 System.IntPtr Examples.IntegerMath::MaxNative(System.IntPtr,System.IntPtr) +x64-v4 System.IntPtr Examples.IntegerMath::MinNative(System.IntPtr,System.IntPtr) +x64-v4 System.IntPtr Examples.MemorySpans::NativeRead(System.ReadOnlySpan`1,System.Int32) +x64-v4 System.IntPtr Examples.NativeIntegers::Add(System.IntPtr,System.IntPtr) +x64-v4 System.IntPtr Examples.NativeIntegers::CheckedAdd(System.IntPtr,System.IntPtr) +x64-v4 System.IntPtr Examples.NativeIntegers::Choose(System.IntPtr) +x64-v4 System.IntPtr Examples.NativeIntegers::Divide(System.IntPtr,System.IntPtr) +x64-v4 System.IntPtr Examples.NativeIntegers::Fields(Examples.NativePair&,System.IntPtr,System.UIntPtr) +x64-v4 System.IntPtr Examples.NativeIntegers::Increment(System.IntPtr&) +x64-v4 System.IntPtr Examples.NativeIntegers::LocalCall(System.IntPtr) +x64-v4 System.IntPtr Examples.NativeIntegers::ReadWrite(System.IntPtr&,System.IntPtr) +x64-v4 System.IntPtr Examples.NativeIntegers::Widen(System.Int32) +x64-v4 System.IntPtr Examples.PrimitiveArrays::ReadIntPtr(System.IntPtr[],System.Int32) +x64-v4 System.IntPtr Examples.UnsafeReferences::ByteOffset(System.Byte&,System.Byte&) +x64-v4 System.IntPtr& Examples.UnsafeLayouts::Add10int(System.IntPtr&,System.Int32) +x64-v4 System.IntPtr& Examples.UnsafeLayouts::Add10nint(System.IntPtr&,System.IntPtr) +x64-v4 System.IntPtr& Examples.UnsafeLayouts::Add10nuint(System.IntPtr&,System.UIntPtr) +x64-v4 System.ReadOnlySpan`1 Examples.AggregateArrays::ReadOnlyView(Examples.AggregateArrays/Pair[]) +x64-v4 System.ReadOnlySpan`1 Examples.AggregateSpans::ReadOnlyFactory(Examples.AggregateSpans/Words&,System.Int32) +x64-v4 System.ReadOnlySpan`1 Examples.AggregateSpans::WordsView(System.ReadOnlySpan`1) +x64-v4 System.ReadOnlySpan`1 Examples.SpanConstructors::PassReadOnly(System.ReadOnlySpan`1) +x64-v4 System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyDefaultReset(Examples.AggregateSpans/Words&) +x64-v4 System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlyEmpty() +x64-v4 System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyBytes(System.ReadOnlySpan`1) +x64-v4 System.ReadOnlySpan`1 Examples.StaticSpans::get_Lookup() +x64-v4 System.ReadOnlySpan`1 Examples.MemorySpans::ArrayRange(System.Int32[],System.Int32,System.Int32) +x64-v4 System.ReadOnlySpan`1 Examples.MemorySpans::ArrayReadOnly(System.Int32[]) +x64-v4 System.ReadOnlySpan`1 Examples.SpanConstructors::ReadOnlySingle(System.Int32&) +x64-v4 System.ReadOnlySpan`1> Examples.SpanConstructors::Vector(System.Runtime.Intrinsics.Vector128`1&) +x64-v4 System.ReadOnlySpan`1> Examples.SpanConstructors::VectorPointer(System.Runtime.Intrinsics.Vector128`1*,System.Int32) +x64-v4 System.ReadOnlySpan`1 Examples.ReinterpretedSpans::ReadOnlyWords(System.ReadOnlySpan`1) +x64-v4 System.ReadOnlySpan`1 Examples.SpanTables::get_Words() +x64-v4 System.ReadOnlySpan`1 Examples.SpanTables::get_Powers() +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::Shuffle128(System.ReadOnlySpan`1,System.ReadOnlySpan`1) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.MarshaledMemory::ReadVector(System.ReadOnlySpan`1) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::Read128(System.Byte&) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.UnalignedMemory::ReadAligned128(System.Byte*) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128Byte(Examples.ValueBitCasts/Pair) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadBytes128(Examples.VectorFields/Bytes128) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::ReadInts128(Examples.VectorFields/Ints128) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt32(Examples.ValueBitCasts/Pair) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.AggregateArrays::ReadVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.AggregateSpans::ReadVector(System.ReadOnlySpan`1>) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::MultiplyEven128(System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.DivisionIntrinsics::UnsafeReturn(System.UInt64) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Identity>(System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.IndirectValues::Vector(System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.UnsafeReferences::ReadHalf(Examples.FourLimbs&) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.ValueBitCasts::To128UInt64(Examples.ValueBitCasts/Pair) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Read128(Examples.VectorFields/Holder128) +x64-v4 System.Runtime.Intrinsics.Vector128`1 Examples.VectorFields::Write128(Examples.VectorFields/Holder128&,System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17int(System.Runtime.Intrinsics.Vector128`1&,System.Int32) +x64-v4 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nint(System.Runtime.Intrinsics.Vector128`1&,System.IntPtr) +x64-v4 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeLayouts::Add17nuint(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +x64-v4 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::Half(Examples.FourLimbs&) +x64-v4 System.Runtime.Intrinsics.Vector128`1& Examples.UnsafeReferences::SubtractVector(System.Runtime.Intrinsics.Vector128`1&,System.UIntPtr) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::Shuffle256(System.ReadOnlySpan`1,System.ReadOnlySpan`1) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::ShuffleCrossHalf(System.ReadOnlySpan`1,System.ReadOnlySpan`1) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::ShuffleIgnoreHighBit(System.ReadOnlySpan`1,System.ReadOnlySpan`1) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int32(Examples.ValueBitCasts/Limbs) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256Int64(Examples.ValueBitCasts/Limbs) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadLongs256(Examples.VectorFields/Longs256) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::CreateWords(System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt32(Examples.ValueBitCasts/Limbs) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::ReadUInts256(Examples.VectorFields/UInts256) +x64-v4 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeReferences::Vector(Examples.FourLimbs&) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.DivisionIntrinsics::SignWindow(System.UInt64,System.UInt64,System.UInt64,System.UInt64,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::CopyLocal(Examples.ValueBitCasts/Limbs) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::ReadLocal(System.Runtime.Intrinsics.Vector256`1&) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.ValueBitCasts::To256UInt64(Examples.ValueBitCasts/Limbs) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Read256(Examples.VectorFields/Holder256) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Reinterpret(Examples.VectorFields/Words256&,System.Runtime.Intrinsics.Vector256`1) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::Write256(Examples.VectorFields/Holder256&,System.Runtime.Intrinsics.Vector256`1) +x64-v4 System.Runtime.Intrinsics.Vector256`1 Examples.VectorFields::WritePacked(Examples.VectorFields/Packed256&,System.Runtime.Intrinsics.Vector256`1) +x64-v4 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18int(System.Runtime.Intrinsics.Vector256`1&,System.Int32) +x64-v4 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nint(System.Runtime.Intrinsics.Vector256`1&,System.IntPtr) +x64-v4 System.Runtime.Intrinsics.Vector256`1& Examples.UnsafeLayouts::Add18nuint(System.Runtime.Intrinsics.Vector256`1&,System.UIntPtr) +x64-v4 System.Runtime.Intrinsics.Vector256`1& Examples.VectorFields::Address256(Examples.VectorFields/Holder256&) +x64-v4 System.Runtime.Intrinsics.Vector256`1[] Examples.PrimitiveArrays::AllocateVector(System.Int32) +x64-v4 System.Runtime.Intrinsics.Vector512`1 Examples.UnalignedMemory::Read512(System.Byte&) +x64-v4 System.SByte Examples.Bits::ReverseI8(System.SByte) +x64-v4 System.SByte Examples.IntegerMath::AbsInt8(System.SByte) +x64-v4 System.SByte Examples.IntegerMath::ClampInt8(System.SByte,System.SByte,System.SByte) +x64-v4 System.SByte Examples.IntegerMath::MaxInt8(System.SByte,System.SByte) +x64-v4 System.SByte Examples.IntegerMath::MinInt8(System.SByte,System.SByte) +x64-v4 System.SByte Examples.PrimitiveArrays::ReadSByte(System.SByte[],System.Int32) +x64-v4 System.SByte Examples.TupleDivision::MathSByteRemainder(System.SByte,System.SByte) +x64-v4 System.SByte Examples.UnalignedMemory::ReadSigned(System.Byte&) +x64-v4 System.SByte& Examples.UnsafeLayouts::Add2int(System.SByte&,System.Int32) +x64-v4 System.SByte& Examples.UnsafeLayouts::Add2nint(System.SByte&,System.IntPtr) +x64-v4 System.SByte& Examples.UnsafeLayouts::Add2nuint(System.SByte&,System.UIntPtr) +x64-v4 System.Span`1 Examples.AggregateArrays::Slice(Examples.AggregateArrays/Pair[],System.Int32,System.Int32) +x64-v4 System.Span`1 Examples.AggregateArrays::View(Examples.AggregateArrays/Pair[]) +x64-v4 System.Span`1 Examples.AggregateSpans::Factory(Examples.AggregateSpans/Words&,System.Int32) +x64-v4 System.Span`1 Examples.AggregateSpans::Slice(System.Span`1,System.Int32,System.Int32) +x64-v4 System.Span`1 Examples.SpanConstructors::Aggregate(Examples.AggregateSpans/Words&) +x64-v4 System.Span`1 Examples.SpanConstructors::Pointer(Examples.AggregateSpans/Words*,System.Int32) +x64-v4 System.Span`1 Examples.EnumBuffers::Slice(System.Span`1,System.Int32) +x64-v4 System.Span`1 Examples.ReinterpretedSpans::EnumWords(System.Span`1) +x64-v4 System.Span`1 Examples.MemorySpans::ArrayAsSpan(System.Boolean[]) +x64-v4 System.Span`1 Examples.AggregateSpans::Bytes(System.Span`1) +x64-v4 System.Span`1 Examples.AggregateSpans::LayoutBytes(System.Span`1) +x64-v4 System.Span`1 Examples.AggregateSpans::VectorBytes(System.Span`1>) +x64-v4 System.Span`1 Examples.ReinterpretedSpans::Bytes(System.Span`1) +x64-v4 System.Span`1 Examples.ReinterpretedSpans::EnumBytes(System.Span`1) +x64-v4 System.Span`1 Examples.ReinterpretedSpans::Expanded(System.Span`1) +x64-v4 System.Span`1 Examples.MemorySpans::ArrayConstructor(System.Char[]) +x64-v4 System.Span`1 Examples.MemorySpans::ArrayWritable(System.Int32[]) +x64-v4 System.Span`1 Examples.ReinterpretedSpans::SameWidth(System.Span`1) +x64-v4 System.Span`1 Examples.SpanConstructors::DefaultReset(System.Int32&) +x64-v4 System.Span`1 Examples.SpanConstructors::Empty() +x64-v4 System.Span`1 Examples.SpanConstructors::Pass(System.Span`1) +x64-v4 System.Span`1 Examples.SpanConstructors::Single(System.Int32&) +x64-v4 System.Span`1 Examples.MemorySpans::ArrayAsSpanStart(System.IntPtr[],System.Int32) +x64-v4 System.Span`1> Examples.AggregateArrays::VectorView(System.Runtime.Intrinsics.Vector128`1[]) +x64-v4 System.Span`1 Examples.ReinterpretedSpans::Words(System.Span`1) +x64-v4 System.Span`1 Examples.MemorySpans::ArrayAsSpanRange(System.UIntPtr[],System.Int32,System.Int32) +x64-v4 System.String Examples.Guards::Required(System.String) +x64-v4 System.String Examples.Strings::OrNone(System.String) +x64-v4 System.String Examples.Strings::Pick(System.Boolean,System.String,System.String) +x64-v4 System.String Examples.Strings::Relabel(Examples.Labelled,System.String) +x64-v4 System.String[] Examples.TagArrays::Create(System.Int32) +x64-v4 System.UInt16 Examples.Bits::ReverseU16(System.UInt16) +x64-v4 System.UInt16 Examples.IntegerMath::ClampUInt16(System.UInt16,System.UInt16,System.UInt16) +x64-v4 System.UInt16 Examples.IntegerMath::MaxUInt16(System.UInt16,System.UInt16) +x64-v4 System.UInt16 Examples.IntegerMath::MinUInt16(System.UInt16,System.UInt16) +x64-v4 System.UInt16 Examples.PrimitiveArrays::ReadUInt16(System.UInt16[],System.Int32) +x64-v4 System.UInt16& Examples.UnsafeLayouts::Add5int(System.UInt16&,System.Int32) +x64-v4 System.UInt16& Examples.UnsafeLayouts::Add5nint(System.UInt16&,System.IntPtr) +x64-v4 System.UInt16& Examples.UnsafeLayouts::Add5nuint(System.UInt16&,System.UIntPtr) +x64-v4 System.UInt32 Examples.Arguments::Address(System.UInt32) +x64-v4 System.UInt32 Examples.Arguments::Alias(System.UInt32) +x64-v4 System.UInt32 Examples.Arguments::CallerIsolation(System.UInt32) +x64-v4 System.UInt32 Examples.Arguments::Overwrite(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.Arguments::ReassignReference(System.UInt32&,System.UInt32&) +x64-v4 System.UInt32 Examples.Arithmetic::AtMostFive(System.UInt32) +x64-v4 System.UInt32 Examples.Bits::Left32(System.UInt32,System.Int32) +x64-v4 System.UInt32 Examples.Bits::PrimitiveLeadingU32(System.UInt32) +x64-v4 System.UInt32 Examples.Bits::PrimitiveLeftU32(System.UInt32,System.Int32) +x64-v4 System.UInt32 Examples.Bits::PrimitiveLogU32(System.UInt32) +x64-v4 System.UInt32 Examples.Bits::PrimitivePopulationU32(System.UInt32) +x64-v4 System.UInt32 Examples.Bits::PrimitiveRightU32(System.UInt32,System.Int32) +x64-v4 System.UInt32 Examples.Bits::PrimitiveTrailingU32(System.UInt32) +x64-v4 System.UInt32 Examples.Bits::ReverseU32(System.UInt32) +x64-v4 System.UInt32 Examples.Bits::Right32(System.UInt32,System.Int32) +x64-v4 System.UInt32 Examples.Bits::RoundUp32(System.UInt32) +x64-v4 System.UInt32 Examples.Capabilities::AesFlags() +x64-v4 System.UInt32 Examples.Capabilities::ArmFlags() +x64-v4 System.UInt32 Examples.Capabilities::Flags() +x64-v4 System.UInt32 Examples.Capabilities::VbmiFlags() +x64-v4 System.UInt32 Examples.CheckedArithmetic::Add(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.CheckedConversions::Narrow(System.UInt64) +x64-v4 System.UInt32 Examples.Division::Quotient(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.ExceptionDocumentation::Profile(System.UInt32) +x64-v4 System.UInt32 Examples.ExceptionDocumentation::SpanItem(System.Int32) +x64-v4 System.UInt32 Examples.GenericMethods::ClearPacked(System.UInt32) +x64-v4 System.UInt32 Examples.GenericMethods::SwapPacked(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.GenericOwners::Address(System.UInt32) +x64-v4 System.UInt32 Examples.GenericOwners::Combined(System.UInt32) +x64-v4 System.UInt32 Examples.GenericOwners::Identity(System.UInt32) +x64-v4 System.UInt32 Examples.GenericOwners::Nested(System.UInt32) +x64-v4 System.UInt32 Examples.GenericOwners::NestedHelper(System.UInt32) +x64-v4 System.UInt32 Examples.GenericOwners::Repeated(System.UInt32) +x64-v4 System.UInt32 Examples.Guards::Index(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.IntegerMath::ClampUInt32(System.UInt32,System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.IntegerMath::MaxUInt32(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.IntegerMath::MinUInt32(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.Loops::AddTwo(System.UInt32) +x64-v4 System.UInt32 Examples.Loops::Bump(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.Loops::CopyUntilZero(System.ReadOnlySpan`1,System.UInt32&) +x64-v4 System.UInt32 Examples.Loops::CountCalls(System.UInt32) +x64-v4 System.UInt32 Examples.Loops::CountUntil(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.Loops::CountUpToFive(System.UInt32) +x64-v4 System.UInt32 Examples.Loops::Countdown(System.UInt32) +x64-v4 System.UInt32 Examples.Loops::Decrement(System.UInt32) +x64-v4 System.UInt32 Examples.Loops::DownThenUp(System.UInt32) +x64-v4 System.UInt32 Examples.Loops::InnerCount(Examples.Loops/Outer,System.UInt32) +x64-v4 System.UInt32 Examples.Loops::ReachFive(System.UInt32) +x64-v4 System.UInt32 Examples.Loops::TallyTwice(System.UInt32) +x64-v4 System.UInt32 Examples.Loops::WrongCountdown(System.UInt32) +x64-v4 System.UInt32 Examples.NarrowTotals::ReadSum() +x64-v4 System.UInt32 Examples.NarrowTotals::WriteRead(System.UInt32) +x64-v4 System.UInt32 Examples.NativeIntegers::NarrowUnsigned(System.UIntPtr) +x64-v4 System.UInt32 Examples.Nested::Packed(System.UInt32) +x64-v4 System.UInt32 Examples.OwnerHelper`1::Identity(System.UInt32) +x64-v4 System.UInt32 Examples.OwnerHelper`1::Select(System.UInt32,System.UInt64) +x64-v4 System.UInt32 Examples.OwnerOuter`1/Helper::Identity(System.UInt32) +x64-v4 System.UInt32 Examples.OwnerOuter`1/Inner`1::Identity(System.UInt32) +x64-v4 System.UInt32 Examples.PrimitiveArrays::ReadUInt32(System.UInt32[],System.Int32) +x64-v4 System.UInt32 Examples.ReinterpretedSpans::Aliased(System.Span`1) +x64-v4 System.UInt32 Examples.Sequential::ReadWrite(Examples.Record&,System.UInt32) +x64-v4 System.UInt32 Examples.SpanTables::Word(System.Int32) +x64-v4 System.UInt32 Examples.Throws::ActualValue(System.UInt32) +x64-v4 System.UInt32 Examples.Throws::ArgumentInner(System.UInt32) +x64-v4 System.UInt32 Examples.Throws::Inner(System.UInt32) +x64-v4 System.UInt32 Examples.Throws::Null(System.UInt32) +x64-v4 System.UInt32 Examples.Throws::Percent(System.UInt32) +x64-v4 System.UInt32 Examples.Throws::Quotient(System.UInt32,System.UInt32) +x64-v4 System.UInt32 Examples.WordReader::RangeEnd() +x64-v4 System.UInt32 Examples.WordReader::ReplaceRemaining(System.UInt32) +x64-v4 System.UInt32 Examples.WordReader::Reserve() +x64-v4 System.UInt32 SelectorExamples.One.Collision::Add(System.UInt32,System.UInt32) +x64-v4 System.UInt32& Examples.UnsafeLayouts::Add7int(System.UInt32&,System.Int32) +x64-v4 System.UInt32& Examples.UnsafeLayouts::Add7nint(System.UInt32&,System.IntPtr) +x64-v4 System.UInt32& Examples.UnsafeLayouts::Add7nuint(System.UInt32&,System.UIntPtr) +x64-v4 System.UInt64 Examples.Account::get_Headroom() +x64-v4 System.UInt64 Examples.AddStep::Apply(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.Aliases::Update(System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.Animal::Loudness() +x64-v4 System.UInt64 Examples.Arguments::Struct(Examples.Packet) +x64-v4 System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.Arithmetic::Alias(System.UInt64&,System.UInt64&) +x64-v4 System.UInt64 Examples.Arithmetic::Calls(System.UInt64) +x64-v4 System.UInt64 Examples.Arithmetic::Choose(System.Boolean,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.Arithmetic::Distance(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.Arithmetic::Increment(System.UInt64) +x64-v4 System.UInt64 Examples.Arithmetic::LargeStruct(System.UInt64) +x64-v4 System.UInt64 Examples.Arithmetic::PastEnd(System.UInt64&) +x64-v4 System.UInt64 Examples.Arithmetic::Read(Examples.Packet&) +x64-v4 System.UInt64 Examples.Arithmetic::ReadLarge(Examples.LargePacket&) +x64-v4 System.UInt64 Examples.Arithmetic::ReadValue(Examples.Packet) +x64-v4 System.UInt64 Examples.Arithmetic::Struct(System.UInt64) +x64-v4 System.UInt64 Examples.Arithmetic::StructCopy(System.UInt64) +x64-v4 System.UInt64 Examples.Arithmetic::TypeCheck(System.UInt64) +x64-v4 System.UInt64 Examples.Arithmetic::WrongAdd(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.BinarySpans::ReadBig(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.BinarySpans::ReadLittle(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.BinarySpans::TryReadBig(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.BinarySpans::TryReadLittle(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.Bird::Legs() +x64-v4 System.UInt64 Examples.Bits::Left64(System.UInt64,System.Int32) +x64-v4 System.UInt64 Examples.Bits::PrimitiveCountsU64(System.UInt64) +x64-v4 System.UInt64 Examples.Bits::PrimitiveLeftU64(System.UInt64,System.Int32) +x64-v4 System.UInt64 Examples.Bits::PrimitiveRightU64(System.UInt64,System.Int32) +x64-v4 System.UInt64 Examples.Bits::ReverseU64(System.UInt64) +x64-v4 System.UInt64 Examples.Bits::Right64(System.UInt64,System.Int32) +x64-v4 System.UInt64 Examples.Bits::RoundTrip(System.UInt64) +x64-v4 System.UInt64 Examples.Bits::RoundUp64(System.UInt64) +x64-v4 System.UInt64 Examples.Bits::WrongRoundTrip(System.UInt64) +x64-v4 System.UInt64 Examples.Blocks::CopyLocal(System.UInt64,System.UInt32) +x64-v4 System.UInt64 Examples.Blocks::CopyParameter(System.UInt64,System.UInt64,System.UInt32) +x64-v4 System.UInt64 Examples.Blocks::FillLocal(System.Byte,System.UInt32) +x64-v4 System.UInt64 Examples.Blocks::FillParameter(System.UInt64,System.Byte,System.UInt32) +x64-v4 System.UInt64 Examples.CheckedArithmetic::Add64(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.ComputedFlags::TraceBoth(System.UInt64) +x64-v4 System.UInt64 Examples.ComputedFlags::TraceChained(System.UInt64) +x64-v4 System.UInt64 Examples.ComputedFlags::TraceGated(System.UInt64) +x64-v4 System.UInt64 Examples.ComputedFlags::TraceNotOff(System.UInt64) +x64-v4 System.UInt64 Examples.ComputedFlags::TraceSilent(System.UInt64) +x64-v4 System.UInt64 Examples.ConstantFlags::Count(System.UInt64) +x64-v4 System.UInt64 Examples.ConstantFlags::CountOn(System.UInt64) +x64-v4 System.UInt64 Examples.ConstantFlags::Trace(System.UInt64) +x64-v4 System.UInt64 Examples.ConstantFlags::TraceOff(System.UInt64) +x64-v4 System.UInt64 Examples.ConstantFlags::TraceThenCount(System.UInt64) +x64-v4 System.UInt64 Examples.Constructors::Checked(System.UInt64) +x64-v4 System.UInt64 Examples.Constructors::Default() +x64-v4 System.UInt64 Examples.Constructors::Nested(System.UInt64) +x64-v4 System.UInt64 Examples.Constructors::Packed(System.UInt64) +x64-v4 System.UInt64 Examples.Constructors::Read(System.UInt64) +x64-v4 System.UInt64 Examples.Counter::Add(System.UInt64) +x64-v4 System.UInt64 Examples.Counter::Mix(System.UInt64,System.UInt32,System.UInt16,System.Byte) +x64-v4 System.UInt64 Examples.Counter::Read() +x64-v4 System.UInt64 Examples.Counter::WriteRead(System.UInt64) +x64-v4 System.UInt64 Examples.Counter::WrongWriteRead(System.UInt64) +x64-v4 System.UInt64 Examples.DerivedFlags::Count(System.UInt64) +x64-v4 System.UInt64 Examples.DerivedFlags::CountLoud(System.UInt64) +x64-v4 System.UInt64 Examples.DerivedFlags::Trace(System.UInt64) +x64-v4 System.UInt64 Examples.DerivedFlags::TraceQuiet(System.UInt64) +x64-v4 System.UInt64 Examples.Division::Quotient64(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::And(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::AndNot(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::DefinedLocal(System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::Or(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::PrunedUnsafeLocal(System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::ReadLow(System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::ShiftLeft(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::ShiftRight(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::UnsafeCall(System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::UnsafeHigh(System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::UnsafeLow(System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::UnsafeLowNext(System.UInt64) +x64-v4 System.UInt64 Examples.DivisionIntrinsics::Xor(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.Dog::Legs() +x64-v4 System.UInt64 Examples.Dog::Loudness() +x64-v4 System.UInt64 Examples.ExceptionDocumentation::DivRem(System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.ExceptionDocumentation::UnsignedDivide(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.GenericEnvelope`1::Read() +x64-v4 System.UInt64 Examples.GenericLayouts::Composed(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.GenericLayouts::Field(Examples.GenericPair`1&) +x64-v4 System.UInt64 Examples.GenericLayouts::Roundtrip(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.GenericMethods::Choose(System.Boolean,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.GenericMethods::First(System.UInt64,System.UInt32) +x64-v4 System.UInt64 Examples.GenericMethods::Forward(System.UInt64) +x64-v4 System.UInt64 Examples.GenericMethods::Identity64(System.UInt64) +x64-v4 System.UInt64 Examples.GenericMethods::Identity(System.UInt64) +x64-v4 System.UInt64 Examples.GenericMethods::Instance(System.UInt64) +x64-v4 System.UInt64 Examples.GenericMethods::Nested(System.UInt64) +x64-v4 System.UInt64 Examples.GenericMethods::Repeated(System.UInt64) +x64-v4 System.UInt64 Examples.GenericMethods::Select(System.Boolean,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.GenericMethods::TwoTypes(System.UInt64,System.UInt32) +x64-v4 System.UInt64 Examples.GenericOwners::NestedSpanFirst(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.GenericOwners::OwnerSpanFirst(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.GenericOwners::SpanFirst(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.GenericOwners::SpanSecond(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.GenericPair`1::GetSecond() +x64-v4 System.UInt64 Examples.GenericPicker::Pick(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::Count>(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::CountWide(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::Pad,System.UInt32>(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::Pad,System.UInt64>(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::PadNarrow(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::PadWide(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::Trace>(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::TraceNarrow(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::Width,System.UInt32>(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::Width,System.UInt64>(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::WidthNarrow(System.UInt64) +x64-v4 System.UInt64 Examples.GenericReceivers::WidthWide(System.UInt64) +x64-v4 System.UInt64 Examples.IMeasureFive::Examples.IMeasure.Measure(System.UInt64) +x64-v4 System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +x64-v4 System.UInt64 Examples.IPadded`1::Pad(System.UInt64) +x64-v4 System.UInt64 Examples.IScale::Scale(System.UInt64) +x64-v4 System.UInt64 Examples.IScaleMore::Examples.IScale.Scale(System.UInt64) +x64-v4 System.UInt64 Examples.IScaleMost::Examples.IScale.Scale(System.UInt64) +x64-v4 System.UInt64 Examples.IScaleSide::Examples.IScale.Scale(System.UInt64) +x64-v4 System.UInt64 Examples.InParameters::Constrained(Examples.InWord&,Examples.InWord&) +x64-v4 System.UInt64 Examples.InParameters::ConstrainedSum(Examples.InWord&,System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.InParameters::Sum(Examples.InWord&,System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.InWord::Examples.IInWord.Sum(System.ReadOnlySpan`1& modreq(System.Runtime.InteropServices.InAttribute)) +x64-v4 System.UInt64 Examples.InitCounter::get_Value() +x64-v4 System.UInt64 Examples.IntegerMath::ClampUInt64(System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.IntegerMath::MaxUInt64(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.IntegerMath::MinUInt64(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.Loops::Sum(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.Loops::SumIndexed(System.UInt64[]) +x64-v4 System.UInt64 Examples.Loops::SumThroughLocal(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.Loops::SumThroughSpan(System.UInt64[]) +x64-v4 System.UInt64 Examples.Loops::TwoStructs(System.UInt32) +x64-v4 System.UInt64 Examples.MarshaledMemory::Read(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.Measured`1::Width(System.UInt64) +x64-v4 System.UInt64 Examples.Measured`1::Width(System.UInt64) +x64-v4 System.UInt64 Examples.MostSpecificBodies::Five(System.UInt64) +x64-v4 System.UInt64 Examples.MostSpecificBodies::Measured(System.UInt64) +x64-v4 System.UInt64 Examples.MostSpecificBodies::More(System.UInt64) +x64-v4 System.UInt64 Examples.MostSpecificBodies::Most(System.UInt64) +x64-v4 System.UInt64 Examples.MostSpecificBodies::Own(System.UInt64) +x64-v4 System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +x64-v4 System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +x64-v4 System.UInt64 Examples.MostSpecificBodies::Scaled(System.UInt64) +x64-v4 System.UInt64 Examples.Nested::Alias(System.UInt64) +x64-v4 System.UInt64 Examples.Nested::Copy(System.UInt64) +x64-v4 System.UInt64 Examples.Nested::Explicit(System.UInt64) +x64-v4 System.UInt64 Examples.Nested::Pass(System.UInt64) +x64-v4 System.UInt64 Examples.Nested::Read(Examples.Leaf) +x64-v4 System.UInt64 Examples.Nested::ReadOuter(Examples.NestedRecord) +x64-v4 System.UInt64 Examples.Nested::Sized(System.UInt64) +x64-v4 System.UInt64 Examples.Nested::Snapshot(System.UInt64) +x64-v4 System.UInt64 Examples.Nested::WrongCopy(System.UInt64) +x64-v4 System.UInt64 Examples.NestedConstructor::Sum() +x64-v4 System.UInt64 Examples.NullReferences::Optional(Examples.FourLimbs&,System.Boolean) +x64-v4 System.UInt64 Examples.NullReferences::Read() +x64-v4 System.UInt64 Examples.NullReferences::ReadField() +x64-v4 System.UInt64 Examples.NullReferences::Unchecked(Examples.FourLimbs&,System.Boolean) +x64-v4 System.UInt64 Examples.Objects::BalanceOf(Examples.Account) +x64-v4 System.UInt64 Examples.Objects::Describe(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.Objects::Fresh(System.UInt32) +x64-v4 System.UInt64 Examples.Objects::IdOf(Examples.Shape) +x64-v4 System.UInt64 Examples.Objects::LinkedValue(Examples.Node,Examples.Node) +x64-v4 System.UInt64 Examples.Objects::Replace(Examples.Account,System.UInt64) +x64-v4 System.UInt64 Examples.Objects::SecondValue(Examples.Node) +x64-v4 System.UInt64 Examples.Objects::UnderFive(Examples.Account) +x64-v4 System.UInt64 Examples.Objects::Withdraw(Examples.Account,System.UInt64) +x64-v4 System.UInt64 Examples.Odometer::Next() +x64-v4 System.UInt64 Examples.OutlineCountdown::Drain() +x64-v4 System.UInt64 Examples.OutlineCountdown::DrainAtLeastOnce() +x64-v4 System.UInt64 Examples.OutlineCounters::ExchangeAdd(System.UInt64) +x64-v4 System.UInt64 Examples.OutlineCounters::UnsignedLiteral() +x64-v4 System.UInt64 Examples.OutlineCounters::WrongExchangeAdd(System.UInt64) +x64-v4 System.UInt64 Examples.OwnerHelper`1::First(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.OwnerOuter`1/Inner`1::First(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.PackedConstructor::Sum() +x64-v4 System.UInt64 Examples.PointerOperations::WriteEnum(Examples.Unsigned64*,System.UInt64) +x64-v4 System.UInt64 Examples.PrimitiveArrays::ReadUInt64(System.UInt64[],System.Int32) +x64-v4 System.UInt64 Examples.ScaleOwn::Scale(System.UInt64) +x64-v4 System.UInt64 Examples.Sequential::Packed(System.UInt64) +x64-v4 System.UInt64 Examples.SkipInitialization::Read(System.UInt64&) +x64-v4 System.UInt64 Examples.SpanHelpers::First(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanHelpers::Second(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.SpanReferences::Back(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanReferences::BigEndianWord(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanReferences::ByteSecond(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanReferences::Construct(System.ReadOnlySpan`1) +x64-v4 System.UInt64 Examples.SpanReferences::CursorSum(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanReferences::Element(System.ReadOnlySpan`1&,System.Int32) +x64-v4 System.UInt64 Examples.SpanReferences::FirstHigh(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanReferences::GenericHead(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanReferences::Head(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanReferences::High(System.Span`1&,System.Int32) +x64-v4 System.UInt64 Examples.SpanReferences::HighAddress(System.ReadOnlySpan`1&,System.Int32) +x64-v4 System.UInt64 Examples.SpanReferences::ReadOnlyHigh(System.ReadOnlySpan`1&,System.Int32) +x64-v4 System.UInt64 Examples.SpanReferences::Second(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanReferences::Word(System.ReadOnlySpan`1&) +x64-v4 System.UInt64 Examples.SpanTables::First() +x64-v4 System.UInt64 Examples.SpanTables::Power(System.Int32) +x64-v4 System.UInt64 Examples.StaticInterfaces::AddCounted(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.StaticInterfaces::Step(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.StaticInterfaces::XorPlain(System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.StaticValues::Read(Examples.Word256&) +x64-v4 System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.Allowance&) +x64-v4 System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.DoublingAllowance&) +x64-v4 System.UInt64 Examples.StaticVirtualDefaults::Spend(Examples.RefundingAllowance&) +x64-v4 System.UInt64 Examples.StaticVirtualDefaults::SpendDefault(Examples.Allowance&) +x64-v4 System.UInt64 Examples.StaticVirtualDefaults::SpendOverride(Examples.DoublingAllowance&) +x64-v4 System.UInt64 Examples.StaticVirtualDefaults::SpendRefunding(Examples.RefundingAllowance&) +x64-v4 System.UInt64 Examples.StructMethods::Arguments(System.UInt64) +x64-v4 System.UInt64 Examples.StructMethods::ByValue(System.UInt64) +x64-v4 System.UInt64 Examples.StructMethods::ConstructLocal(System.UInt64) +x64-v4 System.UInt64 Examples.StructMethods::Initialized(System.UInt64) +x64-v4 System.UInt64 Examples.StructMethods::Mutate(System.UInt64) +x64-v4 System.UInt64 Examples.StructMethods::Replace(System.UInt64) +x64-v4 System.UInt64 Examples.TriplePrice::get_Price() +x64-v4 System.UInt64 Examples.TupleDivision::Deconstruct(System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.TupleDivision::Item1(System.ValueTuple`2&) +x64-v4 System.UInt64 Examples.TupleDivision::Local(System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.TupleDivision::Quotient(System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.TupleDivision::Remainder(System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.TupleDivision::WrongQuotient(System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.UnalignedMemory::ReadAlignedWord(System.Byte*) +x64-v4 System.UInt64 Examples.UnalignedMemory::ReadPointerWord(System.Byte*) +x64-v4 System.UInt64 Examples.UnalignedMemory::ReadWord(System.Byte&) +x64-v4 System.UInt64 Examples.UnsafeReferences::PastEnd(Examples.FourLimbs&) +x64-v4 System.UInt64 Examples.UnsafeReferences::Read(Examples.FourLimbs&) +x64-v4 System.UInt64 Examples.UnsafeReferences::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.UInt64 Examples.VirtualCalls::DogLegs() +x64-v4 System.UInt64 Examples.VirtualCalls::LegsOf(Examples.Animal) +x64-v4 System.UInt64 Examples.VirtualCalls::LoudnessOf(Examples.Animal) +x64-v4 System.UInt64 Examples.WideProduct::BigMul(System.UInt64,System.UInt64,System.UInt64&) +x64-v4 System.UInt64 Examples.WideProduct::UnsignedBigMul32(System.UInt32,System.UInt32) +x64-v4 System.UInt64 Examples.WordReader::ReplaceRemainingWide(System.UInt32) +x64-v4 System.UInt64 Examples.XorStep::Examples.IBinaryStep.Apply(System.UInt64,System.UInt64) +x64-v4 System.UInt64 SelectorExamples.One.Collision::Add(System.UInt64,System.UInt64) +x64-v4 System.UInt64 SelectorExamples.Two.Collision::Add(System.UInt64,System.UInt64) +x64-v4 System.UInt64& Examples.Arithmetic::Escape() +x64-v4 System.UInt64& Examples.UnsafeLayouts::Add9int(System.UInt64&,System.Int32) +x64-v4 System.UInt64& Examples.UnsafeLayouts::Add9nint(System.UInt64&,System.IntPtr) +x64-v4 System.UInt64& Examples.UnsafeLayouts::Add9nuint(System.UInt64&,System.UIntPtr) +x64-v4 System.UInt64& Examples.UnsafeReferences::Pointer(System.Byte*) +x64-v4 System.UInt64& Examples.UnsafeReferences::SubtractElements32(System.UInt64&,System.Int32) +x64-v4 System.UInt64& Examples.UnsafeReferences::SubtractElementsNative(System.UInt64&,System.IntPtr) +x64-v4 System.UInt64& Examples.UnsafeReferences::SubtractElementsUnsigned(System.UInt64&,System.UIntPtr) +x64-v4 System.UInt64& Examples.UnsafeReferences::Word(Examples.FourLimbs&) +x64-v4 System.UIntPtr Examples.Bits::LeftNative(System.UIntPtr,System.Int32) +x64-v4 System.UIntPtr Examples.Bits::PrimitiveCountsUNative(System.UIntPtr) +x64-v4 System.UIntPtr Examples.Bits::PrimitiveLeftUNative(System.UIntPtr,System.Int32) +x64-v4 System.UIntPtr Examples.Bits::PrimitiveRightUNative(System.UIntPtr,System.Int32) +x64-v4 System.UIntPtr Examples.Bits::ReverseUNative(System.UIntPtr) +x64-v4 System.UIntPtr Examples.Bits::RightNative(System.UIntPtr,System.Int32) +x64-v4 System.UIntPtr Examples.Bits::RoundUpNative(System.UIntPtr) +x64-v4 System.UIntPtr Examples.IntegerMath::ClampUNative(System.UIntPtr,System.UIntPtr,System.UIntPtr) +x64-v4 System.UIntPtr Examples.IntegerMath::MaxUNative(System.UIntPtr,System.UIntPtr) +x64-v4 System.UIntPtr Examples.IntegerMath::MinUNative(System.UIntPtr,System.UIntPtr) +x64-v4 System.UIntPtr Examples.MemorySpans::NativeWrite(System.Span`1,System.Int32,System.UIntPtr) +x64-v4 System.UIntPtr Examples.NativeIntegers::AddUnsigned(System.UIntPtr,System.UIntPtr) +x64-v4 System.UIntPtr Examples.NativeIntegers::CheckedAddUnsigned(System.UIntPtr,System.UIntPtr) +x64-v4 System.UIntPtr Examples.NativeIntegers::DivideUnsigned(System.UIntPtr,System.UIntPtr) +x64-v4 System.UIntPtr Examples.NativeIntegers::ReadWriteUnsigned(System.UIntPtr&,System.UIntPtr) +x64-v4 System.UIntPtr Examples.NativeIntegers::Shift(System.UIntPtr,System.Int32) +x64-v4 System.UIntPtr Examples.NativeIntegers::WidenUnsigned(System.UInt32) +x64-v4 System.UIntPtr Examples.PrimitiveArrays::ReadUIntPtr(System.UIntPtr[],System.Int32) +x64-v4 System.UIntPtr& Examples.UnsafeLayouts::Add11int(System.UIntPtr&,System.Int32) +x64-v4 System.UIntPtr& Examples.UnsafeLayouts::Add11nint(System.UIntPtr&,System.IntPtr) +x64-v4 System.UIntPtr& Examples.UnsafeLayouts::Add11nuint(System.UIntPtr&,System.UIntPtr) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathByte(System.Byte,System.Byte) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathInt16(System.Int16,System.Int16) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathInt32(System.Int32,System.Int32) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathInt64(System.Int64,System.Int64) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathIntPtr(System.IntPtr,System.IntPtr) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathSByte(System.SByte,System.SByte) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathUInt16(System.UInt16,System.UInt16) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathUInt32(System.UInt32,System.UInt32) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathUInt64(System.UInt64,System.UInt64) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::Pair(System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.ValueTuple`2 Examples.TupleDivision::MathUIntPtr(System.UIntPtr,System.UIntPtr) +x64-v4 System.Void Examples.Account::.ctor() +x64-v4 System.Void Examples.Account::Deposit(System.UInt64) +x64-v4 System.Void Examples.AggregateArrays::Write(Examples.AggregateArrays/Pair[],System.Int32,Examples.AggregateArrays/Pair) +x64-v4 System.Void Examples.AggregateArrays::WriteVector(System.Runtime.Intrinsics.Vector128`1[],System.Int32,System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Void Examples.AggregateSpanFill::Aliased(System.Span`1) +x64-v4 System.Void Examples.AggregateSpanFill::Generic(System.Span`1>,Examples.GenericPair`1) +x64-v4 System.Void Examples.AggregateSpanFill::Vector128(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Void Examples.AggregateSpanFill::Vector256(System.Span`1>,System.Runtime.Intrinsics.Vector256`1) +x64-v4 System.Void Examples.AggregateSpanFill::Vector512(System.Span`1>,System.Runtime.Intrinsics.Vector512`1) +x64-v4 System.Void Examples.AggregateSpanFill::Words(System.Span`1,Examples.AggregateSpans/Words) +x64-v4 System.Void Examples.AggregateSpans::Clear(System.Span`1) +x64-v4 System.Void Examples.AggregateSpans::Write(System.Span`1,Examples.AggregateSpans/Words) +x64-v4 System.Void Examples.AggregateSpans::WriteVector(System.Span`1>,System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Void Examples.Animal::.ctor() +x64-v4 System.Void Examples.Arguments::Increment(System.UInt32&) +x64-v4 System.Void Examples.Arithmetic::Mutate(System.UInt64&) +x64-v4 System.Void Examples.BinarySpans::WriteBig(System.Span`1) +x64-v4 System.Void Examples.BinarySpans::WriteLittle(System.Span`1) +x64-v4 System.Void Examples.Bird::.ctor() +x64-v4 System.Void Examples.Blocks::Copy(System.Byte&,System.Byte&,System.UInt32) +x64-v4 System.Void Examples.Blocks::CopyAligned(System.Byte&,System.Byte&,System.UInt32) +x64-v4 System.Void Examples.Blocks::CopyPointer(System.Byte*,System.Byte*,System.UInt32) +x64-v4 System.Void Examples.Blocks::CopyPointerAligned(System.Byte*,System.Byte*,System.UInt32) +x64-v4 System.Void Examples.Blocks::Fill(System.Byte&,System.Byte,System.UInt32) +x64-v4 System.Void Examples.Blocks::FillAligned(System.Byte&,System.Byte,System.UInt32) +x64-v4 System.Void Examples.Blocks::FillPointer(System.Byte*,System.Byte,System.UInt32) +x64-v4 System.Void Examples.Blocks::FillPointerAligned(System.Byte*,System.Byte,System.UInt32) +x64-v4 System.Void Examples.Bucket::.ctor() +x64-v4 System.Void Examples.CheckedConstructor::.ctor(System.UInt64) +x64-v4 System.Void Examples.Circle::.ctor(System.UInt64,System.UInt64) +x64-v4 System.Void Examples.ConstructorLeaf::.ctor(System.UInt32) +x64-v4 System.Void Examples.Counter::.ctor(System.UInt64) +x64-v4 System.Void Examples.Counter::Increment(System.Byte&) +x64-v4 System.Void Examples.Counter::Replace(Examples.Counter) +x64-v4 System.Void Examples.Counter::Set(System.UInt64) +x64-v4 System.Void Examples.CounterValue::Increment() +x64-v4 System.Void Examples.DefaultConstructor::.ctor() +x64-v4 System.Void Examples.DivisionIntrinsics::Init128(System.Runtime.Intrinsics.Vector128`1&) +x64-v4 System.Void Examples.DivisionIntrinsics::Init256(System.Runtime.Intrinsics.Vector256`1&) +x64-v4 System.Void Examples.DivisionIntrinsics::Init512(System.Runtime.Intrinsics.Vector512`1&) +x64-v4 System.Void Examples.DivisionIntrinsics::SwapHalves(System.ReadOnlySpan`1,System.Span`1) +x64-v4 System.Void Examples.DivisionIntrinsics::UnsafeStore(System.Runtime.Intrinsics.Vector128`1&,System.UInt64) +x64-v4 System.Void Examples.Dog::.ctor() +x64-v4 System.Void Examples.Enums::Assign(Examples.Unsigned64&,Examples.Unsigned64) +x64-v4 System.Void Examples.ExceptionDocumentation::Reference(System.UInt64&) +x64-v4 System.Void Examples.ExplicitCounterValue::Examples.ICounterValue.Increment() +x64-v4 System.Void Examples.ExplicitGenericCounterValue`1::Examples.ICounterValue.Increment() +x64-v4 System.Void Examples.Gauge::.ctor() +x64-v4 System.Void Examples.Gauge::Settle() +x64-v4 System.Void Examples.GenericEnvelope`1::.ctor(System.UInt64,System.UInt64) +x64-v4 System.Void Examples.GenericMethods::Clear(Examples.PackedLeaf&) +x64-v4 System.Void Examples.GenericMethods::Swap32(System.Int32&,System.Int32&) +x64-v4 System.Void Examples.GenericMethods::Swap(Examples.PackedLeaf&,Examples.PackedLeaf&) +x64-v4 System.Void Examples.GenericMethods::Swap(System.Int32&,System.Int32&) +x64-v4 System.Void Examples.GenericMethods::SwapRecord(Examples.PackedLeaf&,Examples.PackedLeaf&) +x64-v4 System.Void Examples.GenericPair`1::.ctor(Examples.GenericPair`1,Examples.GenericPair`1) +x64-v4 System.Void Examples.GenericPair`1::.ctor(System.UInt32,System.UInt32) +x64-v4 System.Void Examples.GenericPair`1::.ctor(System.UInt64,System.UInt64) +x64-v4 System.Void Examples.GenericPair`1::SetSecond(System.UInt64) +x64-v4 System.Void Examples.InParameters::Add(Examples.InWord&,Examples.InWord&,Examples.InWord&) +x64-v4 System.Void Examples.InWord::Examples.IInWord.Add(Examples.InWord& modreq(System.Runtime.InteropServices.InAttribute),Examples.InWord&) +x64-v4 System.Void Examples.ItemPool::.ctor(System.Int32) +x64-v4 System.Void Examples.ItemPool::Prewarm(System.Int32) +x64-v4 System.Void Examples.ItemPool::Return(Examples.PooledItem) +x64-v4 System.Void Examples.Labelled::.ctor() +x64-v4 System.Void Examples.Loops::Fill(System.Span`1,System.UInt32) +x64-v4 System.Void Examples.Loops::FillIndexed(System.UInt32[],System.UInt32) +x64-v4 System.Void Examples.Loops::FillThrough(System.Span`1,System.UInt32) +x64-v4 System.Void Examples.Loops::FillThroughSpan(System.UInt32[],System.UInt32) +x64-v4 System.Void Examples.Loops::FillWords(System.UInt64&,System.UInt64,System.Int32) +x64-v4 System.Void Examples.Loops::FillWordsDown(System.UInt64&,System.UInt64,System.Int32) +x64-v4 System.Void Examples.Loops::Put(System.Span`1,System.UInt32) +x64-v4 System.Void Examples.Loops::Stamp(System.UInt32&,System.UInt32) +x64-v4 System.Void Examples.Marker::.ctor() +x64-v4 System.Void Examples.MarshaledMemory::Write(System.Span`1,System.UInt64&) +x64-v4 System.Void Examples.MarshaledMemory::WriteAggregate(System.Span`1,Examples.FourLimbs&) +x64-v4 System.Void Examples.MemorySpans::ClearBytes(System.Span`1) +x64-v4 System.Void Examples.MemorySpans::FillCharacters(System.Span`1,System.Char) +x64-v4 System.Void Examples.MemorySpans::FillIntegers(System.Span`1,System.Int32) +x64-v4 System.Void Examples.MemorySpans::FillLongSlice(System.Span`1,System.Int32,System.Int32,System.Int64) +x64-v4 System.Void Examples.MemorySpans::Write(System.Span`1,System.Int32,System.Int32) +x64-v4 System.Void Examples.NestedConstructor::.ctor(System.UInt64) +x64-v4 System.Void Examples.Node::.ctor() +x64-v4 System.Void Examples.NodeArrays::Put(Examples.Shape[],System.Int32,Examples.Shape) +x64-v4 System.Void Examples.NodeArrays::Refill(Examples.Bucket,Examples.Node[]) +x64-v4 System.Void Examples.NullReferences::Write(System.UInt64) +x64-v4 System.Void Examples.NullReferences::WriteField(System.UInt64) +x64-v4 System.Void Examples.Objects::Bump(Examples.Account,System.UInt64) +x64-v4 System.Void Examples.Objects::BumpBoth(Examples.Account,Examples.Account,System.UInt64) +x64-v4 System.Void Examples.Objects::ClearTag(Examples.Tagged) +x64-v4 System.Void Examples.Objects::CopyTag(Examples.Tagged,Examples.Tagged) +x64-v4 System.Void Examples.Objects::DepositBoth(Examples.Account,Examples.Account,System.UInt64) +x64-v4 System.Void Examples.Objects::DepositFrom(Examples.Account,Examples.Account,System.UInt32) +x64-v4 System.Void Examples.Objects::DepositTimes(Examples.Account,System.UInt64,System.UInt32) +x64-v4 System.Void Examples.Objects::FillTag(Examples.Tagged,Examples.Tagged) +x64-v4 System.Void Examples.Objects::Link(Examples.Node,Examples.Node) +x64-v4 System.Void Examples.Objects::LinkBoth(Examples.Node,Examples.Node,Examples.Node) +x64-v4 System.Void Examples.Objects::Relink(Examples.Node,Examples.Node,Examples.Node) +x64-v4 System.Void Examples.Objects::SetLimit(Examples.Account,System.UInt32) +x64-v4 System.Void Examples.OutlineCountdown::Clear() +x64-v4 System.Void Examples.PackedConstructor::.ctor(System.UInt64) +x64-v4 System.Void Examples.PackedConstructor::.ctor(System.UInt64,System.Byte) +x64-v4 System.Void Examples.PooledItem::.ctor(Examples.ItemPool,System.Int32) +x64-v4 System.Void Examples.PooledItem::set_IsRented(System.Boolean) +x64-v4 System.Void Examples.PooledItem::set_Payload(System.Int64) +x64-v4 System.Void Examples.PrimitiveArrays::FillEnums(Examples.AllocationTag[],Examples.AllocationTag) +x64-v4 System.Void Examples.PrimitiveArrays::FillGeneric(Examples.AllocationTag[],Examples.AllocationTag) +x64-v4 System.Void Examples.PrimitiveArrays::FillGeneric(System.Int32[],System.Int32) +x64-v4 System.Void Examples.PrimitiveArrays::FillIntegers(System.Int32[],System.Int32) +x64-v4 System.Void Examples.PrimitiveArrays::FillRangeEnums(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +x64-v4 System.Void Examples.PrimitiveArrays::FillRangeGeneric(Examples.AllocationTag[],Examples.AllocationTag,System.Int32,System.Int32) +x64-v4 System.Void Examples.PrimitiveArrays::FillRangeGeneric(System.Int32[],System.Int32,System.Int32,System.Int32) +x64-v4 System.Void Examples.PrimitiveArrays::FillRangeIntegers(System.Int32[],System.Int32,System.Int32,System.Int32) +x64-v4 System.Void Examples.PrimitiveArrays::WriteBoolean(System.Boolean[],System.Int32,System.Boolean) +x64-v4 System.Void Examples.PrimitiveArrays::WriteByte(System.Byte[],System.Int32,System.Byte) +x64-v4 System.Void Examples.PrimitiveArrays::WriteChar(System.Char[],System.Int32,System.Char) +x64-v4 System.Void Examples.PrimitiveArrays::WriteGeneric(System.Int32[],System.Int32,System.Int32) +x64-v4 System.Void Examples.PrimitiveArrays::WriteInt16(System.Int16[],System.Int32,System.Int16) +x64-v4 System.Void Examples.PrimitiveArrays::WriteInt32(System.Int32[],System.Int32,System.Int32) +x64-v4 System.Void Examples.PrimitiveArrays::WriteInt64(System.Int64[],System.Int32,System.Int64) +x64-v4 System.Void Examples.PrimitiveArrays::WriteIntPtr(System.IntPtr[],System.Int32,System.IntPtr) +x64-v4 System.Void Examples.PrimitiveArrays::WriteSByte(System.SByte[],System.Int32,System.SByte) +x64-v4 System.Void Examples.PrimitiveArrays::WriteUInt16(System.UInt16[],System.Int32,System.UInt16) +x64-v4 System.Void Examples.PrimitiveArrays::WriteUInt32(System.UInt32[],System.Int32,System.UInt32) +x64-v4 System.Void Examples.PrimitiveArrays::WriteUInt64(System.UInt64[],System.Int32,System.UInt64) +x64-v4 System.Void Examples.PrimitiveArrays::WriteUIntPtr(System.UIntPtr[],System.Int32,System.UIntPtr) +x64-v4 System.Void Examples.SampleArrays::Refill(Examples.Samples,System.Int32[]) +x64-v4 System.Void Examples.Samples::.ctor() +x64-v4 System.Void Examples.Shape::.ctor(System.UInt64) +x64-v4 System.Void Examples.SkipInitialization::Skip(Examples.FourLimbs&) +x64-v4 System.Void Examples.SkipInitialization::Store(Examples.FourLimbs&,System.UInt64,System.UInt64,System.UInt64,System.UInt64) +x64-v4 System.Void Examples.SpanCopies::Copy(System.Span`1,System.Span`1) +x64-v4 System.Void Examples.SpanCopies::CopyLongs(System.ReadOnlySpan`1,System.Span`1) +x64-v4 System.Void Examples.SpanCopies::CopyReadOnly(System.ReadOnlySpan`1,System.Span`1) +x64-v4 System.Void Examples.SpanReferences/Limbs::.ctor(System.ReadOnlySpan`1&,System.Boolean) +x64-v4 System.Void Examples.SpanReferences::SetHigh(System.ReadOnlySpan`1&,System.UInt64) +x64-v4 System.Void Examples.SpanReferences::WriteFirst(System.Span`1&,System.Int32) +x64-v4 System.Void Examples.SpanReferences::WriteSecond(System.ReadOnlySpan`1&,System.UInt64) +x64-v4 System.Void Examples.SpanReferences::WriteWord(System.ReadOnlySpan`1&,System.UInt64) +x64-v4 System.Void Examples.Square::.ctor(System.UInt64,System.UInt64) +x64-v4 System.Void Examples.StaticSpans::Write() +x64-v4 System.Void Examples.StructMethods::ChangeCopy(Examples.Counter) +x64-v4 System.Void Examples.TagArrays::Put(System.String[],System.Int32,Examples.Tagged) +x64-v4 System.Void Examples.TagArrays::Stamp(System.String[],Examples.Tagged,System.UInt64) +x64-v4 System.Void Examples.TagArrays::Take(Examples.Tagged,System.String[],System.Int32) +x64-v4 System.Void Examples.Tagged::.ctor() +x64-v4 System.Void Examples.TailCalls::ForwardVoid(System.Int32&) +x64-v4 System.Void Examples.TailCalls::Store(System.Int32&) +x64-v4 System.Void Examples.Tally::.ctor(System.UInt32) +x64-v4 System.Void Examples.Throws::ThrowDivideByZero() +x64-v4 System.Void Examples.Throws::ThrowNull() +x64-v4 System.Void Examples.TileArrays::Relane(Examples.Tiles,System.Runtime.Intrinsics.Vector128`1[]) +x64-v4 System.Void Examples.Tiles::.ctor() +x64-v4 System.Void Examples.TypedCopy::Packet(Examples.Packet&,Examples.Packet&) +x64-v4 System.Void Examples.TypedCopy::Record(Examples.Record&,Examples.Record&) +x64-v4 System.Void Examples.TypedCopy::Vector128(System.Runtime.Intrinsics.Vector128`1&,System.Runtime.Intrinsics.Vector128`1&) +x64-v4 System.Void Examples.TypedCopy::Vector256(System.Runtime.Intrinsics.Vector256`1&,System.Runtime.Intrinsics.Vector256`1&) +x64-v4 System.Void Examples.UnalignedMemory::Write128(System.Byte&,System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Void Examples.UnalignedMemory::Write512(System.Byte&,System.Runtime.Intrinsics.Vector512`1) +x64-v4 System.Void Examples.UnalignedMemory::WriteAggregate(System.Byte&,Examples.FourLimbs) +x64-v4 System.Void Examples.UnalignedMemory::WriteAligned128(System.Byte*,System.Runtime.Intrinsics.Vector128`1) +x64-v4 System.Void Examples.UnalignedMemory::WriteAlignedAggregate(System.Byte*,Examples.FourLimbs) +x64-v4 System.Void Examples.UnalignedMemory::WriteAlignedWord(System.Byte*,System.UInt64) +x64-v4 System.Void Examples.UnalignedMemory::WriteHalf(System.Byte&,System.Int16) +x64-v4 System.Void Examples.UnalignedMemory::WritePointerAggregate(System.Byte*,Examples.FourLimbs) +x64-v4 System.Void Examples.UnalignedMemory::WritePointerWord(System.Byte*,System.UInt64) +x64-v4 System.Void Examples.UnalignedMemory::WriteSigned(System.Byte&,System.SByte) +x64-v4 System.Void Examples.UnalignedMemory::WriteWord(System.Byte&,System.UInt64) +x64-v4 System.Void Examples.WordReader::.ctor() +x64-v4 System.Void Examples.WordReader::Advance() +x64-v4 System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Examples.InitCounter::set_Value(System.UInt64) +x64-v4 method System.Int32 *(System.Int32)[] Examples.ManagedDispatch::NullTable() diff --git a/Tests/canonical-methods.txt b/Tests/canonical-methods.txt new file mode 100644 index 000000000..fe0526018 --- /dev/null +++ b/Tests/canonical-methods.txt @@ -0,0 +1,3378 @@ +BindingImage closure classes (compiler-generated) +BindingImage::Compress (compiler-generated) +DeepCheckInputs Program/DeepCheckRun::get_Inputs() +DeepCheckInputs closure classes (compiler-generated) +DeepCheckInputs closure classes (compiler-generated) +DeepCheckInputs/Image DeepCheckInputs/Pin::get_Image() +FailureReports closure classes (compiler-generated) +FailureReports/BindingFailure FailureReports/Report::get_binding() +FailureReports/BindingFailure::Clone (compiler-generated) +FailureReports/BindingFailureDigest FailureReports/BindingFailure::get_digests() +FailureReports/BindingFailureDigest::Clone (compiler-generated) +FailureReports/BindingFailureFact::Clone (compiler-generated) +FailureReports/BindingFailureFact[] FailureReports/BindingFailure::get_facts() +FailureReports/BindingFailureImage::Clone (compiler-generated) +FailureReports/BindingFailureImage[] FailureReports/BindingFailure::get_images() +FailureReports/BindingFailureStatus FailureReports/BindingFailureFact::get_status() +FailureReports/BindingFailureStatus FailureReports/BindingFailureImage::get_status() +FailureReports/Diagnostic[] FailureReports/Report::get_diagnostics() +FailureReports/Method FailureReports/Report::get_method() +FailureReports/Method::Clone (compiler-generated) +FailureReports/Report Program/BatchResult::get_failure() +FailureReports/Report Program/GateResult::get_failure() +LeanDiagnostics/Diagnostic::Clone (compiler-generated) +LeanDiagnostics/Report::Clone (compiler-generated) +LeanDiagnostics::Entries (compiler-generated) +LeanDiagnostics::Entries (compiler-generated) +LeanOutput closure classes (compiler-generated) +LeanOutput closure classes (compiler-generated) +LeanOutput/InternalError::Clone (compiler-generated) +Nethermind.LeanCIL.Arrays closure classes (compiler-generated) +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.ClassViews/Field::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.ClassViews/FieldOperation::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.LocalKind/Array::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.LocalKind/EnumSpan::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.ProofOutline/Parameter::get_Element() +Nethermind.LeanCIL.Arrays/Element Nethermind.LeanCIL.ProofOutline/ViewField::get_Element() +Nethermind.LeanCIL.Arrays/Element::Clone (compiler-generated) +Nethermind.LeanCIL.Arrays/NumericAccess Nethermind.LeanCIL.ProofOutline/ArrayAccess::get_Load() +Nethermind.LeanCIL.Arrays/NumericAccess Nethermind.LeanCIL.ProofOutline/WordRead::get_Load() +Nethermind.LeanCIL.Arrays::StackKind (compiler-generated) +Nethermind.LeanCIL.AssemblyName Nethermind.LeanCIL.TypeIdentity::get_Assembly() +Nethermind.LeanCIL.Blockers closure classes (compiler-generated) +Nethermind.LeanCIL.Blockers closure classes (compiler-generated) +Nethermind.LeanCIL.Blockers closure classes (compiler-generated) +Nethermind.LeanCIL.Blockers closure classes (compiler-generated) +Nethermind.LeanCIL.Blockers::Run (compiler-generated) +Nethermind.LeanCIL.CallReturnType closure classes (compiler-generated) +Nethermind.LeanCIL.ClassViews closure classes (compiler-generated) +Nethermind.LeanCIL.ClassViews closure classes (compiler-generated) +Nethermind.LeanCIL.ClassViews closure classes (compiler-generated) +Nethermind.LeanCIL.ClassViews/Access::Clone (compiler-generated) +Nethermind.LeanCIL.ClassViews/FieldKind Nethermind.LeanCIL.ClassViews/Field::get_Kind() +Nethermind.LeanCIL.ClassViews/FieldKind Nethermind.LeanCIL.ClassViews/FieldOperation::get_Kind() +Nethermind.LeanCIL.ClassViews/FieldKind Nethermind.LeanCIL.ProofOutline/ViewField::get_Kind() +Nethermind.LeanCIL.ClassViews/FieldOperation Nethermind.LeanCIL.ClassViews/Access::get_Operation() +Nethermind.LeanCIL.ClassViews/Field[] Nethermind.LeanCIL.ClassViews/View::get_Fields() +Nethermind.LeanCIL.ClassViews/View::Clone (compiler-generated) +Nethermind.LeanCIL.ClassViews/View[] Nethermind.LeanCIL.ProofOutline/Extraction::get_Views() +Nethermind.LeanCIL.ClassViews::Views (compiler-generated) +Nethermind.LeanCIL.CliDiagnostics closure classes (compiler-generated) +Nethermind.LeanCIL.ComputedLayout::Clone (compiler-generated) +Nethermind.LeanCIL.ComputedLayout[] Nethermind.LeanCIL.ProofOutline/Extraction::get_Layouts() +Nethermind.LeanCIL.ConstantReturns closure classes (compiler-generated) +Nethermind.LeanCIL.ConstantReturns closure classes (compiler-generated) +Nethermind.LeanCIL.ConstantReturns/Closed::Clone (compiler-generated) +Nethermind.LeanCIL.ConstrainedCalls closure classes (compiler-generated) +Nethermind.LeanCIL.ConstrainedCalls closure classes (compiler-generated) +Nethermind.LeanCIL.ConstrainedCalls closure classes (compiler-generated) +Nethermind.LeanCIL.ConstrainedCalls closure classes (compiler-generated) +Nethermind.LeanCIL.ConstrainedCalls closure classes (compiler-generated) +Nethermind.LeanCIL.ConstrainedCalls closure classes (compiler-generated) +Nethermind.LeanCIL.ConstrainedCalls closure classes (compiler-generated) +Nethermind.LeanCIL.ConstrainedCalls closure classes (compiler-generated) +Nethermind.LeanCIL.ContractBundleManifest Program/FullSelection::get_Inventory() +Nethermind.LeanCIL.ContractCertification Program/FullImport::get_Certification() +Nethermind.LeanCIL.ContractCertification::Clone (compiler-generated) +Nethermind.LeanCIL.ContractDependency::Clone (compiler-generated) +Nethermind.LeanCIL.ContractDependency[] Nethermind.LeanCIL.ImportResolution/Node::get_Dependencies() +Nethermind.LeanCIL.ContractDependency[] Program/PreparedContractExport::get_Dependencies() +Nethermind.LeanCIL.ContractExport Program/FullImport::get_Export() +Nethermind.LeanCIL.ContractExport::Clone (compiler-generated) +Nethermind.LeanCIL.ContractExportManifest closure classes (compiler-generated) +Nethermind.LeanCIL.ContractExportManifest closure classes (compiler-generated) +Nethermind.LeanCIL.ContractExportManifest closure classes (compiler-generated) +Nethermind.LeanCIL.ContractExportManifest::Expand (compiler-generated) +Nethermind.LeanCIL.ContractExportManifest::Expand (compiler-generated) +Nethermind.LeanCIL.ContractExportManifest::Expand (compiler-generated) +Nethermind.LeanCIL.ContractExportProfile::Clone (compiler-generated) +Nethermind.LeanCIL.ContractExportProfile[] Nethermind.LeanCIL.ContractExportRequest::get_Profiles() +Nethermind.LeanCIL.ContractExportRequest Program/FullSlot::get_Request() +Nethermind.LeanCIL.ContractExportRequest Program/PreparedContractExport::get_Request() +Nethermind.LeanCIL.ContractExportRequest::Clone (compiler-generated) +Nethermind.LeanCIL.ContractInterface Nethermind.LeanCIL.ImportResolution/Library::get_Interface() +Nethermind.LeanCIL.ContractInterface Program/FullSelection::get_Contract() +Nethermind.LeanCIL.ContractInterface closure classes (compiler-generated) +Nethermind.LeanCIL.ContractLayout::Clone (compiler-generated) +Nethermind.LeanCIL.ContractLayoutField[] Nethermind.LeanCIL.ContractLayout::get_Fields() +Nethermind.LeanCIL.ContractLayout[] Nethermind.LeanCIL.ContractExport::get_Layouts() +Nethermind.LeanCIL.ContractLayout[] Nethermind.LeanCIL.ImportResolution/Binding::get_ActualLayouts() +Nethermind.LeanCIL.ContractLayout[] Nethermind.LeanCIL.ImportResolution/Binding::get_PublishedLayouts() +Nethermind.LeanCIL.ContractLayout[] Program/PreparedContractExport::get_Layouts() +Nethermind.LeanCIL.ContractMethodMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ContractMethodMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ContractMethodMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ContractMethodMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ContractMethodSignature Nethermind.LeanCIL.ContractExport::get_TypedSignature() +Nethermind.LeanCIL.ContractMethodSignature Nethermind.LeanCIL.ImportResolution/Binding::get_TypedSignature() +Nethermind.LeanCIL.ContractMethodSignature Program/PreparedContractExport::get_TypedSignature() +Nethermind.LeanCIL.ContractMethodSignature::Clone (compiler-generated) +Nethermind.LeanCIL.ContractSource::Clone (compiler-generated) +Nethermind.LeanCIL.ContractSource[] Program/FullBundle::get_Interfaces() +Nethermind.LeanCIL.ContractType Nethermind.LeanCIL.ContractLayout::get_BaseType() +Nethermind.LeanCIL.ContractType Nethermind.LeanCIL.ContractLayoutField::get_Type() +Nethermind.LeanCIL.ContractType Nethermind.LeanCIL.ContractMethodSignature::get_Owner() +Nethermind.LeanCIL.ContractType Nethermind.LeanCIL.ContractMethodSignature::get_ReturnType() +Nethermind.LeanCIL.ContractType Nethermind.LeanCIL.ContractType::get_Element() +Nethermind.LeanCIL.ContractType::Clone (compiler-generated) +Nethermind.LeanCIL.ContractType[] Nethermind.LeanCIL.ContractMethodSignature::get_Arguments() +Nethermind.LeanCIL.ContractType[] Nethermind.LeanCIL.ContractMethodSignature::get_Parameters() +Nethermind.LeanCIL.EnumTypes closure classes (compiler-generated) +Nethermind.LeanCIL.ExceptionLint closure classes (compiler-generated) +Nethermind.LeanCIL.ExceptionLint closure classes (compiler-generated) +Nethermind.LeanCIL.ExceptionLint/Declaration::Clone (compiler-generated) +Nethermind.LeanCIL.ExceptionLint/Site::Clone (compiler-generated) +Nethermind.LeanCIL.ExceptionLint::RuntimeExceptions (compiler-generated) +Nethermind.LeanCIL.ExceptionLint::RuntimeExceptions (compiler-generated) +Nethermind.LeanCIL.ExceptionLint::RuntimeExceptions (compiler-generated) +Nethermind.LeanCIL.ExceptionLint::RuntimeExceptions (compiler-generated) +Nethermind.LeanCIL.ExceptionType Nethermind.LeanCIL.ExceptionLint/Site::get_Exception() +Nethermind.LeanCIL.FeatureEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.FeatureProfile Nethermind.LeanCIL.TemplateEmitter/Variant::get_Profile() +Nethermind.LeanCIL.Feature[] Nethermind.LeanCIL.TemplateEmitter/Method::get_Features() +Nethermind.LeanCIL.Feature[] Nethermind.LeanCIL.TemplateEmitter/Template::get_Features() +Nethermind.LeanCIL.FieldRef Nethermind.LeanCIL.StaticSite::get_Field() +Nethermind.LeanCIL.FieldRef::Clone (compiler-generated) +Nethermind.LeanCIL.FrameMetadata Nethermind.LeanCIL.ProofOutline/Method::get_Frame() +Nethermind.LeanCIL.FrameMetadata Nethermind.LeanCIL.TemplateEmitter/Method::get_Frame() +Nethermind.LeanCIL.FrameMetadata Nethermind.LeanCIL.TemplateEmitter/Row::get_Frame() +Nethermind.LeanCIL.FrameMetadata Nethermind.LeanCIL.TemplateEmitter/Template::get_Frame() +Nethermind.LeanCIL.FrameMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.FrameMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.FrameMetadata::Clone (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::Clone (compiler-generated) +Nethermind.LeanCIL.GenericEvidence/RefusalKind Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::get_Kind() +Nethermind.LeanCIL.GenericStructs closure classes (compiler-generated) +Nethermind.LeanCIL.GenericStructs closure classes (compiler-generated) +Nethermind.LeanCIL.ImportOptions closure classes (compiler-generated) +Nethermind.LeanCIL.ImportOptions closure classes (compiler-generated) +Nethermind.LeanCIL.ImportOptions closure classes (compiler-generated) +Nethermind.LeanCIL.ImportOptions closure classes (compiler-generated) +Nethermind.LeanCIL.ImportOptions closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolution closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolution closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolution closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolution closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolution closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolution closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolution/Binding::Clone (compiler-generated) +Nethermind.LeanCIL.ImportResolution/Library::Clone (compiler-generated) +Nethermind.LeanCIL.ImportResolution/Node::Clone (compiler-generated) +Nethermind.LeanCIL.ImportResolution/Source Nethermind.LeanCIL.ImportResolution/Library::get_Source() +Nethermind.LeanCIL.ImportResolution/Source::Clone (compiler-generated) +Nethermind.LeanCIL.ImportResolver closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolver closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolver closure classes (compiler-generated) +Nethermind.LeanCIL.ImportResolver/Resolved::Clone (compiler-generated) +Nethermind.LeanCIL.ImportResolver::ResolveImport (compiler-generated) +Nethermind.LeanCIL.ImportResolver::ResolveImport (compiler-generated) +Nethermind.LeanCIL.ImportResolver::ResolveImport (compiler-generated) +Nethermind.LeanCIL.ImportedLayoutMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ImportedLayoutMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ImportedLayoutMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ImportedLayoutMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ImportedLayoutMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ImportedLayoutMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.ImportedLayoutMetadata closure classes (compiler-generated) +Nethermind.LeanCIL.InitializationAssumption closure classes (compiler-generated) +Nethermind.LeanCIL.InitializationAssumption::Clone (compiler-generated) +Nethermind.LeanCIL.InitializationAssumption[] Nethermind.LeanCIL.ImportResolution/Binding::get_InitializationAssumptions() +Nethermind.LeanCIL.InitializationAssumption[] Nethermind.LeanCIL.ProfileInitializationAssumptions::get_Types() +Nethermind.LeanCIL.InitializationAssumption[] Program/PreparedContractExport::get_InitializationAssumptions() +Nethermind.LeanCIL.InterfaceModule::Clone (compiler-generated) +Nethermind.LeanCIL.InterfaceModule[] Program/PreparedContractExport::get_ImportedInterfaces() +Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.LayoutEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.LayoutField::Clone (compiler-generated) +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Append::get_Left() +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Append::get_Right() +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Group::get_Body() +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Item::get_Document() +Nethermind.LeanCIL.LeanDoc Nethermind.LeanCIL.LeanDoc/Nest::get_Body() +Nethermind.LeanCIL.LeanDoc closure classes (compiler-generated) +Nethermind.LeanCIL.LeanDoc/Flatten Nethermind.LeanCIL.LeanDoc/WorkGroup::get_Flatten() +Nethermind.LeanCIL.LeanDoc/Item::Clone (compiler-generated) +Nethermind.LeanCIL.LeanDoc/WorkGroup::Clone (compiler-generated) +Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.LeanEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.LocalKind Nethermind.LeanCIL.ParameterHome::get_Kind() +Nethermind.LeanCIL.LocalKind Nethermind.LeanCIL.ProofOutline/LoopOutline::SpanKind(Nethermind.LeanCIL.ProofOutline/Parameter) +Nethermind.LeanCIL.LocalKind Nethermind.LeanCIL.ProofOutline/Slot::get_Kind() +Nethermind.LeanCIL.LocalKind/Aggregate::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Array::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/EnumSpan::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/MethodPointer::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/MethodTable::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Narrow::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/NativeMethodTable::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Object::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Reference::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Span::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Vector::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Word32::Clone (compiler-generated) +Nethermind.LeanCIL.LocalKind/Word64::Clone (compiler-generated) +Nethermind.LeanCIL.ManagedPointers closure classes (compiler-generated) +Nethermind.LeanCIL.ManagedTarget::Clone (compiler-generated) +Nethermind.LeanCIL.MetadataImage Nethermind.LeanCIL.StructuralSignatures/Primitive::get_Image() +Nethermind.LeanCIL.MetadataImage/NamedRow::Clone (compiler-generated) +Nethermind.LeanCIL.MetadataImage/TypeNameRow::Clone (compiler-generated) +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.AssemblyName::get_Culture() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.AssemblyName::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.FieldRef::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.MethodRef::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.TypeIdentity::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.TypeIdentity::get_Namespace() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.TypeLevel::get_Name() +Nethermind.LeanCIL.MetadataName Nethermind.LeanCIL.TypeLevel::get_Namespace() +Nethermind.LeanCIL.MetadataSlices/Slice closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataSlices/Slice closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation closure classes (compiler-generated) +Nethermind.LeanCIL.MetadataValidation/AggregateInfo::Clone (compiler-generated) +Nethermind.LeanCIL.MetadataValidation/InstanceField::Clone (compiler-generated) +Nethermind.LeanCIL.MetadataValidation/LayoutContext Nethermind.LeanCIL.LayoutEvidence::get_Metadata() +Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::Clone (compiler-generated) +Nethermind.LeanCIL.MethodBodyBytes/Body::Clone (compiler-generated) +Nethermind.LeanCIL.MethodRef::Clone (compiler-generated) +Nethermind.LeanCIL.MethodSelector closure classes (compiler-generated) +Nethermind.LeanCIL.MethodSpecialization closure classes (compiler-generated) +Nethermind.LeanCIL.MethodSpecialization/Origin::Clone (compiler-generated) +Nethermind.LeanCIL.ModifierTwins closure classes (compiler-generated) +Nethermind.LeanCIL.Narrowing Nethermind.LeanCIL.LocalKind/Narrow::get_Narrowing() +Nethermind.LeanCIL.NativeDispatch closure classes (compiler-generated) +Nethermind.LeanCIL.NativeOperands/Certificate::Clone (compiler-generated) +Nethermind.LeanCIL.NativeOperands/Kind Nethermind.LeanCIL.Arrays/Element::get_Kind() +Nethermind.LeanCIL.ObjectOperandProof closure classes (compiler-generated) +Nethermind.LeanCIL.Operation[] Nethermind.LeanCIL.ProofOutline/Body::get_Operations() +Nethermind.LeanCIL.Operation[] Nethermind.LeanCIL.ProofOutline/Method::get_Operations() +Nethermind.LeanCIL.ParameterEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.ParameterHome::Clone (compiler-generated) +Nethermind.LeanCIL.PinnedAssemblies Nethermind.LeanCIL.MetadataSlices/Collection::get_Pinned() +Nethermind.LeanCIL.PinnedAssemblies closure classes (compiler-generated) +Nethermind.LeanCIL.PinnedAssemblies/Pin Nethermind.LeanCIL.PinnedAssemblies::get_SelectedPin() +Nethermind.LeanCIL.PinnedAssemblies/Source::Clone (compiler-generated) +Nethermind.LeanCIL.PlatformAssemblies closure classes (compiler-generated) +Nethermind.LeanCIL.PlatformDefinition closure classes (compiler-generated) +Nethermind.LeanCIL.PlatformDefinitions closure classes (compiler-generated) +Nethermind.LeanCIL.PlatformKind Nethermind.LeanCIL.PlatformDefinition::get_Kind() +Nethermind.LeanCIL.PrimitiveType Nethermind.LeanCIL.TypeKind/EnumType::get_Underlying() +Nethermind.LeanCIL.PrimitiveType closure classes (compiler-generated) +Nethermind.LeanCIL.ProfileInitializationAssumptions::Clone (compiler-generated) +Nethermind.LeanCIL.ProfileInitializationAssumptions[] Nethermind.LeanCIL.ContractExport::get_InitializationAssumptions() +Nethermind.LeanCIL.Program closure classes (compiler-generated) +Nethermind.LeanCIL.Program closure classes (compiler-generated) +Nethermind.LeanCIL.Program closure classes (compiler-generated) +Nethermind.LeanCIL.Program closure classes (compiler-generated) +Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot::Clone (compiler-generated) +Nethermind.LeanCIL.Program/ExtractionRequest::Clone (compiler-generated) +Nethermind.LeanCIL.Program::ReadTemplate (compiler-generated) +Nethermind.LeanCIL.Program::Run (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Access Nethermind.LeanCIL.ProofOutline/CallStep::get_Writes() +Nethermind.LeanCIL.ProofOutline/Access::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/AccessKind Nethermind.LeanCIL.ProofOutline/Access::get_Kind() +Nethermind.LeanCIL.ProofOutline/Access[] Nethermind.LeanCIL.ProofOutline/CallStep::get_Needs() +Nethermind.LeanCIL.ProofOutline/AddedReference::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Allocation Nethermind.LeanCIL.ProofOutline/ObjectWalk::get_Fresh() +Nethermind.LeanCIL.ProofOutline/Allocation::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ArrayAccess::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ArrayUse::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Body closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Body::BlockStart (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Callee Nethermind.LeanCIL.ProofOutline/CallStep::get_Callee() +Nethermind.LeanCIL.ProofOutline/Callee::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Component::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Component[] Nethermind.LeanCIL.ProofOutline/Slot::get_Components() +Nethermind.LeanCIL.ProofOutline/Element::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ElementAccess::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ElementHelper Nethermind.LeanCIL.ProofOutline/ArrayAccess::get_Helper() +Nethermind.LeanCIL.ProofOutline/ElementHelper::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ElementKind Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Kind() +Nethermind.LeanCIL.ProofOutline/FieldViews closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/FieldViews closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/HeldArray Nethermind.LeanCIL.ProofOutline/Access::get_Array() +Nethermind.LeanCIL.ProofOutline/HeldArray Nethermind.LeanCIL.ProofOutline/ArrayUse::get_Array() +Nethermind.LeanCIL.ProofOutline/HeldArray Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values::get_Array() +Nethermind.LeanCIL.ProofOutline/HeldArray::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LocalAccess::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LocalAddressValue::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LocalValue::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline::Branch (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline::Step (compiler-generated) +Nethermind.LeanCIL.ProofOutline/LoopOutline::Step (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Method Nethermind.LeanCIL.ProofOutline/Body::get_Method() +Nethermind.LeanCIL.ProofOutline/Method[] Nethermind.LeanCIL.ProofOutline/Extraction::get_Emitted() +Nethermind.LeanCIL.ProofOutline/ObjectFrame::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectFrameEntry::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPath closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectPlan Nethermind.LeanCIL.ProofOutline/Callee::get_Plan() +Nethermind.LeanCIL.ProofOutline/ObjectPlan Nethermind.LeanCIL.ProofOutline/Specification::get_Plan() +Nethermind.LeanCIL.ProofOutline/ObjectTerm::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk Nethermind.LeanCIL.ProofOutline/Callee::get_Walk() +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk/ArrayArgument::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk/Instance::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk/Null::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ObjectWalk::Walk (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Outline::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/ArrayAccess::get_Array() +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/ArrayAccess::get_Index() +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::get_Array() +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/Component::get_Span() +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/HeldArray::get_Words() +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/SpanArgument::get_Span() +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/SpannedArray::get_Array() +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/WordArrayArgument::get_Array() +Nethermind.LeanCIL.ProofOutline/Parameter Nethermind.LeanCIL.ProofOutline/WordRead::get_Array() +Nethermind.LeanCIL.ProofOutline/Parameter::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ParameterShape Nethermind.LeanCIL.ProofOutline/Parameter::get_Shape() +Nethermind.LeanCIL.ProofOutline/Parameter[] Nethermind.LeanCIL.ProofOutline/Specification::get_NonNull() +Nethermind.LeanCIL.ProofOutline/Parameter[] Nethermind.LeanCIL.ProofOutline/Specification::get_Parameters() +Nethermind.LeanCIL.ProofOutline/Part Nethermind.LeanCIL.ProofOutline/Component::get_Part() +Nethermind.LeanCIL.ProofOutline/Reached Nethermind.LeanCIL.ProofOutline/Access::get_Object() +Nethermind.LeanCIL.ProofOutline/Reached Nethermind.LeanCIL.ProofOutline/Access::get_Target() +Nethermind.LeanCIL.ProofOutline/Reached Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Target() +Nethermind.LeanCIL.ProofOutline/Reached Nethermind.LeanCIL.ProofOutline/HeldArray::get_Holder() +Nethermind.LeanCIL.ProofOutline/Reached Nethermind.LeanCIL.ProofOutline/ObjectWalk/Instance::get_Object() +Nethermind.LeanCIL.ProofOutline/Reached::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Reached[] Nethermind.LeanCIL.ProofOutline/CallStep::get_Live() +Nethermind.LeanCIL.ProofOutline/ReceiverField::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ReceiverLoop closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ReceiverLoop closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ReceiverLoop closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ReceiverLoop closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ReceiverLoop::Generate (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ReceiverLoop::Generate (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ReceiverLoop::Walk (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ReferenceArgument::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Result Nethermind.LeanCIL.ProofOutline/Specification::get_Result() +Nethermind.LeanCIL.ProofOutline/Result::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Returning::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ScalarCheck::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ScalarReturn::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ScalarSplit::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ScalarStep Nethermind.LeanCIL.ProofOutline/ScalarCheck::get_Next() +Nethermind.LeanCIL.ProofOutline/ScalarStep Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_Fails() +Nethermind.LeanCIL.ProofOutline/ScalarStep Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_Holds() +Nethermind.LeanCIL.ProofOutline/Sliced::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Slot::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/SpanArgument::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/SpanLengthValue::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/SpanTerm::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/SpannedArray::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Specification Nethermind.LeanCIL.ProofOutline/Callee::get_Spec() +Nethermind.LeanCIL.ProofOutline/Specification closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Specification closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Specification closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Specification closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Specification closure classes (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Stub Nethermind.LeanCIL.ProofOutline/StubCall::get_Stub() +Nethermind.LeanCIL.ProofOutline/Stub::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/StubCall::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Thrown::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/Unknown::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline/ViewField Nethermind.LeanCIL.ProofOutline/Access::get_Field() +Nethermind.LeanCIL.ProofOutline/ViewField Nethermind.LeanCIL.ProofOutline/HeldArray::get_Field() +Nethermind.LeanCIL.ProofOutline/ViewField[] Nethermind.LeanCIL.ProofOutline/Parameter::get_Fields() +Nethermind.LeanCIL.ProofOutline/ViewField[] Nethermind.LeanCIL.ProofOutline/Reached::get_Fields() +Nethermind.LeanCIL.ProofOutline/WordArrayArgument::Clone (compiler-generated) +Nethermind.LeanCIL.ProofOutline::ScalarPaths (compiler-generated) +Nethermind.LeanCIL.ProofOutline::WalkStruct (compiler-generated) +Nethermind.LeanCIL.ProofOutline::WalkStruct (compiler-generated) +Nethermind.LeanCIL.Reachability closure classes (compiler-generated) +Nethermind.LeanCIL.ResolvedCallSites closure classes (compiler-generated) +Nethermind.LeanCIL.RuntimeAsset::Clone (compiler-generated) +Nethermind.LeanCIL.RuntimeAssetSelection closure classes (compiler-generated) +Nethermind.LeanCIL.RuntimeAssetSelection closure classes (compiler-generated) +Nethermind.LeanCIL.RuntimeAssetSelection closure classes (compiler-generated) +Nethermind.LeanCIL.RuntimeAssetSelection closure classes (compiler-generated) +Nethermind.LeanCIL.RuntimeModels closure classes (compiler-generated) +Nethermind.LeanCIL.RuntimeModels closure classes (compiler-generated) +Nethermind.LeanCIL.ScalarEvidence closure classes (compiler-generated) +Nethermind.LeanCIL.StaticData closure classes (compiler-generated) +Nethermind.LeanCIL.StaticData closure classes (compiler-generated) +Nethermind.LeanCIL.StaticDataBlock::Clone (compiler-generated) +Nethermind.LeanCIL.StaticIdentityRecord Nethermind.LeanCIL.StaticSite::get_Identity() +Nethermind.LeanCIL.StaticIdentityRecord::Clone (compiler-generated) +Nethermind.LeanCIL.StaticIdentityRecord[] Nethermind.LeanCIL.ContractCertification::get_StaticIdentities() +Nethermind.LeanCIL.StaticIdentityRecord[] Nethermind.LeanCIL.ImportResolution/Binding::get_StaticIdentities() +Nethermind.LeanCIL.StaticIdentityRecord[] Program/FullLibrary::get_Statics() +Nethermind.LeanCIL.StaticIdentityRecord[] Program/FullSlot::get_Statics() +Nethermind.LeanCIL.StaticIdentityRecord[] Program/PreparedContractExport::get_StaticIdentities() +Nethermind.LeanCIL.StaticSite::Clone (compiler-generated) +Nethermind.LeanCIL.StaticSiteDetail Nethermind.LeanCIL.StaticSite::get_Detail() +Nethermind.LeanCIL.StaticValues closure classes (compiler-generated) +Nethermind.LeanCIL.StaticValues closure classes (compiler-generated) +Nethermind.LeanCIL.StaticValues/Context closure classes (compiler-generated) +Nethermind.LeanCIL.StaticValues/Occurrence::Clone (compiler-generated) +Nethermind.LeanCIL.StaticValues/Self::Clone (compiler-generated) +Nethermind.LeanCIL.StaticValues/Snapshot::Clone (compiler-generated) +Nethermind.LeanCIL.StaticValues/Value::Clone (compiler-generated) +Nethermind.LeanCIL.StaticValues/Word::Clone (compiler-generated) +Nethermind.LeanCIL.StaticValues::Bytes (compiler-generated) +Nethermind.LeanCIL.StaticValues::Closure (compiler-generated) +Nethermind.LeanCIL.StaticValues::Closure (compiler-generated) +Nethermind.LeanCIL.StaticValues::Computed (compiler-generated) +Nethermind.LeanCIL.StructuralSignatures Nethermind.LeanCIL.PinnedAssemblies::get_Signatures() +Nethermind.LeanCIL.StructuralSignatures closure classes (compiler-generated) +Nethermind.LeanCIL.StructuralSignatures closure classes (compiler-generated) +Nethermind.LeanCIL.StructuralSignatures/Primitive::Clone (compiler-generated) +Nethermind.LeanCIL.StructuralSignatures/Primitive[] Nethermind.LeanCIL.PinnedAssemblies/Registration::get_Primitives() +Nethermind.LeanCIL.Symbols closure classes (compiler-generated) +Nethermind.LeanCIL.Symbols closure classes (compiler-generated) +Nethermind.LeanCIL.Symbols closure classes (compiler-generated) +Nethermind.LeanCIL.Symbols closure classes (compiler-generated) +Nethermind.LeanCIL.Symbols/PdbFile::Clone (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter closure classes (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter/Method::Clone (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter/Method[] Nethermind.LeanCIL.TemplateEmitter/Variant::get_Methods() +Nethermind.LeanCIL.TemplateEmitter/Options::Clone (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter/Row::Clone (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter/Row[] Nethermind.LeanCIL.TemplateEmitter/Template::get_Rows() +Nethermind.LeanCIL.TemplateEmitter/Template::Clone (compiler-generated) +Nethermind.LeanCIL.TemplateEmitter/Variant Nethermind.LeanCIL.TemplateEmitter/Row::get_Variant() +Nethermind.LeanCIL.TemplateEmitter/Variant::Clone (compiler-generated) +Nethermind.LeanCIL.Throws closure classes (compiler-generated) +Nethermind.LeanCIL.Throws closure classes (compiler-generated) +Nethermind.LeanCIL.Throws/Analysis::Clone (compiler-generated) +Nethermind.LeanCIL.TypeLevel Nethermind.LeanCIL.MetadataImage/TypeNameRow::get_Level() +Nethermind.LeanCIL.TypeLevel::Clone (compiler-generated) +Nethermind.LeanCIL.TypeTests/Analysis::Clone (compiler-generated) +Nethermind.LeanCIL.TypeTests/Node Nethermind.LeanCIL.TypeTests/Node::get_Left() +Nethermind.LeanCIL.TypeTests/Node Nethermind.LeanCIL.TypeTests/Node::get_Right() +Nethermind.LeanCIL.TypeTests/Node::Clone (compiler-generated) +Nethermind.LeanCIL.UserString::Clone (compiler-generated) +Nethermind.LeanCIL.ValueBitCasts closure classes (compiler-generated) +Nethermind.LeanCIL.ValueTuples closure classes (compiler-generated) +Nethermind.LeanCIL.VirtualDispatch closure classes (compiler-generated) +Nethermind.LeanCIL.VirtualDispatch closure classes (compiler-generated) +Nethermind.LeanCIL.VirtualDispatch closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program closure classes (compiler-generated) +Program/AuditConstantRequest::Clone (compiler-generated) +Program/AuditNamespaces::Clone (compiler-generated) +Program/AuditRefutation Program/AuditedTheorem::get_refutes() +Program/AuditRequest Program/BindingRequest::get_Audit() +Program/AuditRequest::Clone (compiler-generated) +Program/AuditRequest[] Program/ConsumerExportAudit::get_Theorems() +Program/AuditedClosure::Clone (compiler-generated) +Program/AuditedClosure[] Program/AuditedOleans::get_closures() +Program/AuditedInterfaces Program/AuditedOleans::get_interfaces() +Program/AuditedInterfaces::Clone (compiler-generated) +Program/AuditedNamespaces Program/AuditedOleans::get_namespaces() +Program/AuditedNamespaces Program/LibraryAudit::get_Result() +Program/AuditedNamespaces::Clone (compiler-generated) +Program/AuditedTheorem[] Program/AuditedNamespaces::get_rejected() +Program/AuditedTheorem[] Program/AuditedOleans::get_constants() +Program/AuditedTheorem[] Program/AuditedOleans::get_theorems() +Program/BatchManifest::Clone (compiler-generated) +Program/BatchRequest[] Program/BatchManifest::get_requests() +Program/BatchResult[] Program/BatchRun::get_Results() +Program/BatchRun::Clone (compiler-generated) +Program/BindingCatalog::Clone (compiler-generated) +Program/BindingData Program/BindingPublication::get_Closure() +Program/BindingData Program/BindingPublication::get_Sidecar() +Program/BindingFact Program/BindingFactResult::get_id() +Program/BindingFact Program/BindingSubject/Fact::get_Value() +Program/BindingFactResult::Clone (compiler-generated) +Program/BindingFactResult[] Program/BindingSection::get_facts() +Program/BindingImagePin Program/BindingPublication::get_Producer() +Program/BindingImagePin[] Program/BindingPlan::get_Images() +Program/BindingLibraryPreparation::Clone (compiler-generated) +Program/BindingMode Program/BindingSection::get_mode() +Program/BindingPublication::Clone (compiler-generated) +Program/BindingPublication[] Program/BindingPlan::get_Imports() +Program/BindingRequest::Clone (compiler-generated) +Program/BindingRequest[] Program/BindingTargets::get_Requests() +Program/BindingRequest[] Program/DeepSessionContribution::get_Requests() +Program/BindingSection Program/Report::get_binding() +Program/BindingSection::Clone (compiler-generated) +Program/BindingStatus Program/BindingFactResult::get_status() +Program/BindingStatus Program/ImageResult::get_status() +Program/BindingSubject Program/BindingRequest::get_Subject() +Program/BindingSubject/Fact::Clone (compiler-generated) +Program/BindingTargets Program/DeepSessionContribution::get_Targets() +Program/BuildDiagnostic::Clone (compiler-generated) +Program/CatalogAssign::Clone (compiler-generated) +Program/CatalogAssign[] Program/CatalogFamily::get_assign() +Program/CatalogFamily[] Program/CatalogFile::get_families() +Program/CatalogFile::Clone (compiler-generated) +Program/CatalogGate::Clone (compiler-generated) +Program/CatalogGatePattern Program/CatalogFamily::get_gate() +Program/CatalogGatePattern::Clone (compiler-generated) +Program/CatalogGate[] Program/BindingCatalog::get_Gates() +Program/CertifiedFingerprintManifest::Clone (compiler-generated) +Program/CertifiedFingerprintModule::Clone (compiler-generated) +Program/CertifiedReleaseAsset::Clone (compiler-generated) +Program/CheckedContractExport::Clone (compiler-generated) +Program/ClosureEntry::Clone (compiler-generated) +Program/ConsumerDependency Program/FullBundle::get_Dependency() +Program/ConsumerDependency::Clone (compiler-generated) +Program/ConsumerExportAudit Program/ConsumerRequest::get_ExportAudit() +Program/ConsumerExportAudit::Clone (compiler-generated) +Program/ConsumerRequest::Clone (compiler-generated) +Program/ConsumerSession::Clone (compiler-generated) +Program/ConsumerSupportManifest Program/TrustedSupport::get_Manifest() +Program/ConsumerSupportManifest::Clone (compiler-generated) +Program/ConsumerSupportModule::Clone (compiler-generated) +Program/DeepCheckAttempt Program/DeepCheckRun::get_Attempt() +Program/DeepCheckAttempt closure classes (compiler-generated) +Program/DeepCheckRun Program/DeepCheckRun::Claim(DeepCheckInputs,Program/DeepCheckRun) +Program/DeepSessionContribution closure classes (compiler-generated) +Program/DeepSessionContribution::Clone (compiler-generated) +Program/DeploymentDependency::Clone (compiler-generated) +Program/DeploymentDependency[] Program/FullImportResult::get_Dependencies() +Program/DeploymentDependency[] Program/Report::get_contractDependencies() +Program/DigestFile Program/BindingSection::get_digests() +Program/DigestFile::Clone (compiler-generated) +Program/EditorProof::Clone (compiler-generated) +Program/EditorProof[] Program/EditorSelection::get_Proofs() +Program/EditorSelection::Clone (compiler-generated) +Program/EnvironmentEntry::Clone (compiler-generated) +Program/ExportClosure Program/CheckedContractExport::get_Closure() +Program/ExportClosure::Clone (compiler-generated) +Program/ExtractedMethod Program/VerifiedRefutation::get_method() +Program/ExtractedMethod::Clone (compiler-generated) +Program/ExtractionComponents::Clone (compiler-generated) +Program/ExtractionIdentity closure classes (compiler-generated) +Program/FingerprintInput::Clone (compiler-generated) +Program/FullImport Program/FullSlot::get_Import() +Program/FullImport::Clone (compiler-generated) +Program/FullImportResult::Clone (compiler-generated) +Program/FullLibrary::Clone (compiler-generated) +Program/FullSelection Program/FullBundle::get_Selection() +Program/FullSelection Program/FullImport::get_Selection() +Program/GateBuild Program/GateDefaults::get_build() +Program/GateBuild Program/GateVariant::get_build() +Program/GateBuild Program/ProofGateEntry::get_build() +Program/GateBuild::Clone (compiler-generated) +Program/GateBuildPlan::Clone (compiler-generated) +Program/GateDefaults Program/GateManifest::get_defaults() +Program/GateDefaults::Clone (compiler-generated) +Program/GateGenerated::Clone (compiler-generated) +Program/GateManifest::Clone (compiler-generated) +Program/GatePlan::Clone (compiler-generated) +Program/GatePlanGroup[] Program/GatePlan::get_groups() +Program/GatePlanSelection Program/GatePlan::get_selection() +Program/GatePlanSelection::Clone (compiler-generated) +Program/GateProject::Clone (compiler-generated) +Program/GateRefutation Program/GateVariant::get_refutation() +Program/GateRefutation::Clone (compiler-generated) +Program/GateRefutationResult Program/GateResult::get_refutation() +Program/GateRefutationResult::Clone (compiler-generated) +Program/GateResult[] Program/GateResults::get_rows() +Program/GateResults::Clone (compiler-generated) +Program/GateSelectionRules Program/GateManifest::get_selection() +Program/GateSelectionRules::Clone (compiler-generated) +Program/GateStatement Program/GateResult::get_statement() +Program/GateStatement::Clone (compiler-generated) +Program/GateTemplate::Clone (compiler-generated) +Program/GateTemplateUse::Clone (compiler-generated) +Program/GateVariant[] Program/ProofGateEntry::get_variants() +Program/GeneratedExtraction::Clone (compiler-generated) +Program/ImageResult::Clone (compiler-generated) +Program/ImageResult[] Program/BindingSection::get_images() +Program/ImageRole Program/ImageResult::get_role() +Program/LibraryAudit::Clone (compiler-generated) +Program/LoadedGates::Clone (compiler-generated) +Program/PreparedContractExport Program/CheckedContractExport::get_Export() +Program/PreparedContractExport::Clone (compiler-generated) +Program/ProofGateEntry::Clone (compiler-generated) +Program/ProofGateEntry[] Program/GateManifest::get_gates() +Program/ProofMessage::Clone (compiler-generated) +Program/PublishedImport[] Program/FullImportResult::get_Imports() +Program/PublishedImport[] Program/Report::get_imports() +Program/ReplayRejection::Clone (compiler-generated) +Program/ReplayRejectionKind Program/ReplayRejection::get_Kind() +Program/Report[] Program/BatchRun::get_Verified() +Program/StandaloneCachePromotion::Clone (compiler-generated) +Program/StatementDefinition::Clone (compiler-generated) +Program/StatementDefinition[] Program/Report::get_specification() +Program/StatementDefinition[] Program/StatementView::get_definitions() +Program/StatementIdentity Program/Report::get_statementIdentity() +Program/StatementRequest Program/ConsumerRequest::get_Statement() +Program/StatementRequest::Clone (compiler-generated) +Program/StatementSources Program/BindingSection::get_sources() +Program/StatementSources::Clone (compiler-generated) +Program/StatementSubject[] Program/StatementView::get_subjects() +Program/StatementView::Clone (compiler-generated) +Program/TrustedSupport::Clone (compiler-generated) +Program/VerificationSettings::Clone (compiler-generated) +Program/VerifiedRefutation Program/Report::get_refutation() +Program/VerifiedRefutation::Clone (compiler-generated) +Program/WatchResult::Clone (compiler-generated) +Program/WatchSettings::Clone (compiler-generated) +Program/WatchState::Clone (compiler-generated) +Program/WorkspaceSettings::Clone (compiler-generated) +Program::BindingLines (compiler-generated) +Program::BindingLines (compiler-generated) +Program::BindingLines (compiler-generated) +Program::ConsumerGeneratedImports (compiler-generated) +Program::ConsumerSources (compiler-generated) +Program::ConsumerSources (compiler-generated) +Program::DependencyClosure (compiler-generated) +Program::DependencyOrder (compiler-generated) +Program::FullModuleArtifactPaths (compiler-generated) +Program::FullModuleArtifactPaths (compiler-generated) +Program::IndependentlyVerifiedImports (compiler-generated) +Program::ProjectSources (compiler-generated) +Program::ProjectSources (compiler-generated) +Program::ReachableGraph (compiler-generated) +Program::ReachableGraph (compiler-generated) +Program::RunBatch (compiler-generated) +Program::RunBatch (compiler-generated) +Program::RunBatch (compiler-generated) +Program::RunGateRows (compiler-generated) +Program::Specifications (compiler-generated) +Program::SubjectLines (compiler-generated) +Program::SubjectLines (compiler-generated) +Program::SubjectLines (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyConsumerRequests (compiler-generated) +Program::VerifyContractExports (compiler-generated) +Program::VerifyContractExports (compiler-generated) +SourceCopies Program/ConsumerSession::get_Sources() +SourceCopies closure classes (compiler-generated) +System.Boolean DeepCheckInputs/Pin::get_Platform() +System.Boolean DeepCheckInputs/Pin::get_Reference() +System.Boolean Nethermind.LeanCIL.Arrays/Element::get_Compound() +System.Boolean Nethermind.LeanCIL.Arrays/Element::get_Signed() +System.Boolean Nethermind.LeanCIL.Arrays/NumericAccess::get_Signed() +System.Boolean Nethermind.LeanCIL.Arrays/NumericAccess::get_Store() +System.Boolean Nethermind.LeanCIL.AssemblyName/KeyColumns::get_Valid() +System.Boolean Nethermind.LeanCIL.ClassViews/FieldOperation::get_Signed() +System.Boolean Nethermind.LeanCIL.ClassViews/FieldOperation::get_Store() +System.Boolean Nethermind.LeanCIL.CliDiagnostics::IsBidiControl(System.Char) +System.Boolean Nethermind.LeanCIL.ContractExport::get_HasThis() +System.Boolean Nethermind.LeanCIL.ContractExport::get_ReturnsValue() +System.Boolean Nethermind.LeanCIL.ContractLayout::get_Auto() +System.Boolean Nethermind.LeanCIL.ContractLayout::get_Explicit() +System.Boolean Nethermind.LeanCIL.ContractLayout::get_IsValueType() +System.Boolean Nethermind.LeanCIL.FrameMetadata::get_ReturnsValue() +System.Boolean Nethermind.LeanCIL.ImportResolution/Binding::get_IsReferenceAssembly() +System.Boolean Nethermind.LeanCIL.ImportResolution/Binding::get_ReturnsValue() +System.Boolean Nethermind.LeanCIL.ImportResolution/Node::get_IsReferenceAssembly() +System.Boolean Nethermind.LeanCIL.ImportResolution/Source::get_Discovered() +System.Boolean Nethermind.LeanCIL.InitializedStaticValue::get_Construct() +System.Boolean Nethermind.LeanCIL.LayoutField::get_IsAggregate() +System.Boolean Nethermind.LeanCIL.LeanDoc/Space::get_FoundLine() +System.Boolean Nethermind.LeanCIL.LocalKind/EnumSpan::get_ByReference() +System.Boolean Nethermind.LeanCIL.LocalKind/EnumSpan::get_Writable() +System.Boolean Nethermind.LeanCIL.LocalKind/Span::get_Writable() +System.Boolean Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::get_IsByRefLike() +System.Boolean Nethermind.LeanCIL.MethodRef::get_HasThis() +System.Boolean Nethermind.LeanCIL.NumericKinds::Signed(Nethermind.LeanCIL.NumericKind) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetByReferenceType(System.Boolean) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetModifiedType(System.Boolean,System.Boolean,System.Boolean) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetPinnedType(System.Boolean) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetPointerType(System.Boolean) +System.Boolean Nethermind.LeanCIL.PlatformSignatures::GetSZArrayType(System.Boolean) +System.Boolean Nethermind.LeanCIL.ProofOutline/Access::get_Called() +System.Boolean Nethermind.LeanCIL.ProofOutline/Access::get_Opaque() +System.Boolean Nethermind.LeanCIL.ProofOutline/Access::get_Signed() +System.Boolean Nethermind.LeanCIL.ProofOutline/ArrayUse::get_Length() +System.Boolean Nethermind.LeanCIL.ProofOutline/Callee::get_Open() +System.Boolean Nethermind.LeanCIL.ProofOutline/ElementAccess::get_LiteralIndex() +System.Boolean Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Reread() +System.Boolean Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Store() +System.Boolean Nethermind.LeanCIL.ProofOutline/Number::get_Narrow() +System.Boolean Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::get_Numeral() +System.Boolean Nethermind.LeanCIL.ProofOutline/ObjectWalk::get_NumericHomes() +System.Boolean Nethermind.LeanCIL.ProofOutline/Parameter::get_Listed() +System.Boolean Nethermind.LeanCIL.ProofOutline/Parameter::get_Signed() +System.Boolean Nethermind.LeanCIL.ProofOutline/Parameter::get_Writable() +System.Boolean Nethermind.LeanCIL.ProofOutline/ReceiverField::get_Written() +System.Boolean Nethermind.LeanCIL.ProofOutline/Result::get_Array() +System.Boolean Nethermind.LeanCIL.ProofOutline/Result::get_Boolean() +System.Boolean Nethermind.LeanCIL.ProofOutline/Result::get_Object() +System.Boolean Nethermind.LeanCIL.ProofOutline/Returning::get_Kept() +System.Boolean Nethermind.LeanCIL.ProofOutline/Returning::get_Open() +System.Boolean Nethermind.LeanCIL.ProofOutline/Returning::get_Unit() +System.Boolean Nethermind.LeanCIL.ProofOutline/Slot::get_Argument() +System.Boolean Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Caseable() +System.Boolean Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Written() +System.Boolean Nethermind.LeanCIL.ProofOutline/Specification::get_Classes() +System.Boolean Nethermind.LeanCIL.ProofOutline/Specification::get_Symbolic() +System.Boolean Nethermind.LeanCIL.ProofOutline/StructScalar::get_Stored() +System.Boolean Nethermind.LeanCIL.ProofOutline/ViewField::get_Stored() +System.Boolean Nethermind.LeanCIL.ProofOutline/WordRead::get_CallerIndex() +System.Boolean Nethermind.LeanCIL.RuntimeAsset::get_AppLocal() +System.Boolean Nethermind.LeanCIL.StaticValues/Value::get_Construct() +System.Boolean Nethermind.LeanCIL.StaticValues/Value::get_Opaque() +System.Boolean Nethermind.LeanCIL.TemplateEmitter/Options::get_Check() +System.Boolean Nethermind.LeanCIL.TemplateEmitter/Options::get_OmitEquations() +System.Boolean Program/AuditRequest::get_refutation() +System.Boolean Program/AuditRequest::get_statement() +System.Boolean Program/AuditRequest::get_term() +System.Boolean Program/BatchOptions::get_CheckOnly() +System.Boolean Program/BatchOptions::get_Fresh() +System.Boolean Program/BatchOptions::get_Json() +System.Boolean Program/BatchOptions::get_RebuildLibrary() +System.Boolean Program/BatchOptions::get_Trace() +System.Boolean Program/BindingSubject/Fact::get_Complete() +System.Boolean Program/ClosureEntry::get_Built() +System.Boolean Program/ConsumerRequest::get_ExpectRejection() +System.Boolean Program/DeepSessionContribution::get_Restored() +System.Boolean Program/DeploymentDependency::get_conditional() +System.Boolean Program/EnvironmentEntry::get_SetByLake() +System.Boolean Program/GateRow::get_Failed() +System.Boolean Program/PreparedContractExport::get_Conditional() +System.Boolean Program/PreparedContractExport::get_ReturnsValue() +System.Boolean Program/PublishedImport::get_certificationAuthenticated() +System.Boolean Program/StatementRequest::get_Refuted() +System.Boolean Program/StatementRequest::get_Term() +System.Boolean Program/StatementSubject::get_extracted() +System.Boolean Program/WatchResult::get_Rechecked() +System.Boolean Program/WatchSettings::get_Goals() +System.Boolean Program/WatchSettings::get_Json() +System.Boolean Program/WatchSettings::get_Once() +System.Boolean Program/WorkspaceSettings::get_CreatedPackage() +System.Byte[] Nethermind.LeanCIL.MethodBodyBytes/Body::get_Bytes() +System.Byte[] Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot::get_Bytes() +System.Byte[] Nethermind.LeanCIL.StaticValues/Self::get_Data() +System.Byte[] Nethermind.LeanCIL.StaticValues/Snapshot::get_Data() +System.Byte[] Nethermind.LeanCIL.StaticValues/Value::get_Bytes() +System.Byte[] Nethermind.LeanCIL.Symbols/PdbFile::get_Bytes() +System.Byte[] Program/BindingData::get_Bytes() +System.Int32 FailureReports/Report::get_schema() +System.Int32 Nethermind.LeanCIL.Arrays/Element::get_Width() +System.Int32 Nethermind.LeanCIL.Arrays/NumericAccess::get_Width() +System.Int32 Nethermind.LeanCIL.AssemblyName/Columns::get_Culture() +System.Int32 Nethermind.LeanCIL.AssemblyName/Columns::get_Name() +System.Int32 Nethermind.LeanCIL.ClassViews/Access::get_Method() +System.Int32 Nethermind.LeanCIL.ClassViews/Access::get_Pc() +System.Int32 Nethermind.LeanCIL.ClassViews/Field::get_Offset() +System.Int32 Nethermind.LeanCIL.ClassViews/FieldOperation::get_Offset() +System.Int32 Nethermind.LeanCIL.ComputedLayout::get_Size() +System.Int32 Nethermind.LeanCIL.ContractBundleManifest::get_Schema() +System.Int32 Nethermind.LeanCIL.ContractExport::get_ParameterCount() +System.Int32 Nethermind.LeanCIL.ContractLayout::get_DeclaredSize() +System.Int32 Nethermind.LeanCIL.ContractLayout::get_Packing() +System.Int32 Nethermind.LeanCIL.ExceptionLint/Site::get_IlOffset() +System.Int32 Nethermind.LeanCIL.FieldRef::get_Index() +System.Int32 Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::get_Pc() +System.Int32 Nethermind.LeanCIL.ImportResolution/Binding::get_ParameterCount() +System.Int32 Nethermind.LeanCIL.ImportResolution/Binding::get_Slot() +System.Int32 Nethermind.LeanCIL.LayoutField::get_Offset() +System.Int32 Nethermind.LeanCIL.LayoutField::get_Width() +System.Int32 Nethermind.LeanCIL.LeanDoc/Append::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Group::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Item::get_Indent() +System.Int32 Nethermind.LeanCIL.LeanDoc/Line::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Nest::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Nest::get_Indent() +System.Int32 Nethermind.LeanCIL.LeanDoc/Space::get_Width() +System.Int32 Nethermind.LeanCIL.LeanDoc/Text::get_FlatWidth() +System.Int32 Nethermind.LeanCIL.LeanDoc/Text::get_Width() +System.Int32 Nethermind.LeanCIL.LeanEmitter::InstructionCheckHeartbeats(System.Int32,System.Int32) +System.Int32 Nethermind.LeanCIL.LeanEmitter::RecursionDepth(System.Int32) +System.Int32 Nethermind.LeanCIL.LocalKind/Aggregate::get_Size() +System.Int32 Nethermind.LeanCIL.LocalKind/Span::get_Width() +System.Int32 Nethermind.LeanCIL.LocalKind/Vector::get_Bits() +System.Int32 Nethermind.LeanCIL.ManagedTarget::get_Method() +System.Int32 Nethermind.LeanCIL.MetadataImage/NamedRow::get_Name() +System.Int32 Nethermind.LeanCIL.MetadataImage/NamedRow::get_Namespace() +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice/TokenCollector::GetByReferenceType(System.Int32) +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice/TokenCollector::GetPointerType(System.Int32) +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice/TokenCollector::GetSZArrayType(System.Int32) +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice::RowOf(System.UInt32) +System.Int32 Nethermind.LeanCIL.MetadataSlices/Slice::get_PositionOf() +System.Int32 Nethermind.LeanCIL.MetadataValidation/AggregateInfo::get_Alignment() +System.Int32 Nethermind.LeanCIL.MetadataValidation/AggregateInfo::get_Size() +System.Int32 Nethermind.LeanCIL.MethodBodyBytes/Body::get_HeaderLength() +System.Int32 Nethermind.LeanCIL.MethodRef::get_GenericArity() +System.Int32 Nethermind.LeanCIL.NumericKinds::Width(Nethermind.LeanCIL.NumericKind) +System.Int32 Nethermind.LeanCIL.ParameterHome::get_Argument() +System.Int32 Nethermind.LeanCIL.PlatformDefinition::get_Arity() +System.Int32 Nethermind.LeanCIL.PlatformDefinition::get_Underlying() +System.Int32 Nethermind.LeanCIL.PlatformDefinitions::RecursionDepth(System.Int32) +System.Int32 Nethermind.LeanCIL.ProofOutline/Access::get_Stores() +System.Int32 Nethermind.LeanCIL.ProofOutline/AddedReference::get_Width() +System.Int32 Nethermind.LeanCIL.ProofOutline/Allocation::get_Length() +System.Int32 Nethermind.LeanCIL.ProofOutline/Allocation::get_Stores() +System.Int32 Nethermind.LeanCIL.ProofOutline/ArrayUse::get_Stores() +System.Int32 Nethermind.LeanCIL.ProofOutline/CallStep::get_FieldMemory() +System.Int32 Nethermind.LeanCIL.ProofOutline/CallStep::get_Number() +System.Int32 Nethermind.LeanCIL.ProofOutline/CallStep::get_Stores() +System.Int32 Nethermind.LeanCIL.ProofOutline/Callee::get_Index() +System.Int32 Nethermind.LeanCIL.ProofOutline/Component::get_Offset() +System.Int32 Nethermind.LeanCIL.ProofOutline/Component::get_Slot() +System.Int32 Nethermind.LeanCIL.ProofOutline/Component::get_Width() +System.Int32 Nethermind.LeanCIL.ProofOutline/Element::get_Component() +System.Int32 Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Stores() +System.Int32 Nethermind.LeanCIL.ProofOutline/ElementHelper::get_Index() +System.Int32 Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Index() +System.Int32 Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Stores() +System.Int32 Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Width() +System.Int32 Nethermind.LeanCIL.ProofOutline/LocalAddressValue::get_Slot() +System.Int32 Nethermind.LeanCIL.ProofOutline/LocalValue::get_Slot() +System.Int32 Nethermind.LeanCIL.ProofOutline/Number::get_Width() +System.Int32 Nethermind.LeanCIL.ProofOutline/ObjectFrame::get_Locals() +System.Int32 Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::get_Width() +System.Int32 Nethermind.LeanCIL.ProofOutline/Outline::get_Obligations() +System.Int32 Nethermind.LeanCIL.ProofOutline/Outline::get_Paths() +System.Int32 Nethermind.LeanCIL.ProofOutline/Parameter::get_Width() +System.Int32 Nethermind.LeanCIL.ProofOutline/ReceiverField::get_Offset() +System.Int32 Nethermind.LeanCIL.ProofOutline/Result::get_Width() +System.Int32 Nethermind.LeanCIL.ProofOutline/ScalarCheck::get_Pc() +System.Int32 Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_FailsPc() +System.Int32 Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_HoldsPc() +System.Int32 Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_Pc() +System.Int32 Nethermind.LeanCIL.ProofOutline/ScalarWord::get_Bits() +System.Int32 Nethermind.LeanCIL.ProofOutline/Sliced::get_Component() +System.Int32 Nethermind.LeanCIL.ProofOutline/Slot::get_Index() +System.Int32 Nethermind.LeanCIL.ProofOutline/Slot::get_Layout() +System.Int32 Nethermind.LeanCIL.ProofOutline/SpanLengthValue::get_Component() +System.Int32 Nethermind.LeanCIL.ProofOutline/StructScalar::get_Offset() +System.Int32 Nethermind.LeanCIL.ProofOutline/StructScalar::get_Width() +System.Int32 Nethermind.LeanCIL.ProofOutline/Stub::get_Index() +System.Int32 Nethermind.LeanCIL.ProofOutline/Stub::get_Result() +System.Int32 Nethermind.LeanCIL.ProofOutline/StubCall::get_Number() +System.Int32 Nethermind.LeanCIL.ProofOutline/StubCall::get_Stores() +System.Int32 Nethermind.LeanCIL.ProofOutline/ViewField::get_Offset() +System.Int32 Nethermind.LeanCIL.ProofOutline/WordRead::get_Stores() +System.Int32 Nethermind.LeanCIL.StaticDataBlock::get_Alignment() +System.Int32 Nethermind.LeanCIL.StaticSite::get_Pc() +System.Int32 Nethermind.LeanCIL.StaticValues/ConstructorIndex::get_FirstControlTransfer() +System.Int32 Nethermind.LeanCIL.StaticValues/Occurrence::get_Order() +System.Int32 Nethermind.LeanCIL.StaticValues/Word::get_Width() +System.Int32 Program/AuditRefutation::get_column() +System.Int32 Program/AuditRefutation::get_line() +System.Int32 Program/AuditedNamespaces::get_count() +System.Int32 Program/BatchManifest::get_schema() +System.Int32 Program/BindingPlan::get_Methods() +System.Int32 Program/BuildDiagnostic::get_Column() +System.Int32 Program/BuildDiagnostic::get_Line() +System.Int32 Program/CatalogFile::get_schema() +System.Int32 Program/CertifiedFingerprintManifest::get_Schema() +System.Int32 Program/CertifiedReleaseAsset::get_Schema() +System.Int32 Program/ConsumerSupportManifest::get_Schema() +System.Int32 Program/DeepClosureWork::get_Hashed() +System.Int32 Program/DeepClosureWork::get_Headers() +System.Int32 Program/DeepClosureWork::get_Launches() +System.Int32 Program/FullImport::get_Id() +System.Int32 Program/FullLibrary::get_Id() +System.Int32 Program/GateManifest::get_schema() +System.Int32 Program/GatePlanSelection::get_count() +System.Int32 Program/PreparedContractExport::get_ParameterCount() +System.Int32 Program/ProofMessage::get_column() +System.Int32 Program/ProofMessage::get_line() +System.Int32 Program/PublishedImport::get_slot() +System.Int32 Program/Report::get_schema() +System.Int32 Program/VerificationSettings::get_schema() +System.Int32 Program/VerifiedRefutation::get_column() +System.Int32 Program/VerifiedRefutation::get_line() +System.Int32 VerifyReports/SourcePoint::get_Column() +System.Int32 VerifyReports/SourcePoint::get_Line() +System.Int32 VerifyReports/SourcePoint::get_Pc() +System.Int32[] Nethermind.LeanCIL.ClassViews/Field::get_Loaders() +System.Int32[] Nethermind.LeanCIL.ClassViews/Field::get_Storers() +System.Int32[] Nethermind.LeanCIL.ContractMethodSignature::get_ParameterAttributes() +System.Int32[] Nethermind.LeanCIL.ProofOutline/Stub::get_Widths() +System.Int32[] Nethermind.LeanCIL.ProofOutline/ViewField::get_Loaders() +System.Int32[] Nethermind.LeanCIL.ProofOutline/ViewField::get_Storers() +System.Int32[] Nethermind.LeanCIL.TemplateEmitter/Method::get_LinkedMethods() +System.Int32[] Nethermind.LeanCIL.TemplateEmitter/Method::get_Opcodes() +System.Int32[] Nethermind.LeanCIL.TemplateEmitter/Template::get_Opcodes() +System.Int32[] Program/FullLibrary::get_Dependencies() +System.Int32[] Program/PreparedContractExport::get_LayoutIndices() +System.Int64 Nethermind.LeanCIL.MetadataImage::get_SigningBytes() +System.Int64 Nethermind.LeanCIL.MetadataImage::get_SigningReads() +System.Int64 Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::get_Literal() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_EdgeVisits() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_HeapBytes() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_NodeVisits() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_RawRows() +System.Int64 Nethermind.LeanCIL.StructuralSignatures::get_TrieProbes() +System.Int64 Program/CertifiedReleaseAsset::get_ArchiveBytes() +System.Int64 Program/DeepClosureWork::get_HashedBytes() +System.Int64[] Nethermind.LeanCIL.StructuralSignatures/Snapshot::get_Prefix() +System.ReadOnlySpan`1 DeepCheckInputs/Image::get_Bytes() +System.ReadOnlySpan`1 Nethermind.LeanCIL.UserString::get_Units() +System.String DeepCheckInputs/Image::get_Path() +System.String DeepCheckInputs/Pin::get_Identity() +System.String DeepCheckInputs::get_Path() +System.String FailureReports/BindingFailureDigest::get_path() +System.String FailureReports/BindingFailureDigest::get_sha256() +System.String FailureReports/BindingFailureFact::get_id() +System.String FailureReports/BindingFailureFact::get_reason() +System.String FailureReports/BindingFailureImage::get_identity() +System.String FailureReports/BindingFailureImage::get_reason() +System.String FailureReports/BindingFailureImage::get_sha256() +System.String FailureReports/Diagnostic::get_file() +System.String FailureReports/Diagnostic::get_kind() +System.String FailureReports/Diagnostic::get_message() +System.String FailureReports/Diagnostic::get_module() +System.String FailureReports/Diagnostic::get_resource() +System.String FailureReports/Method::get_assembly() +System.String FailureReports/Method::get_assemblySha256() +System.String FailureReports/Method::get_entry() +System.String FailureReports/Report::get_file() +System.String FailureReports/Report::get_kind() +System.String FailureReports/Report::get_message() +System.String FailureReports/Report::get_module() +System.String FailureReports/Report::get_output() +System.String FailureReports/Report::get_proof() +System.String FailureReports/Report::get_resource() +System.String FailureReports/Report::get_stage() +System.String FailureReports/Report::get_theorem() +System.String LeanDiagnostics/Diagnostic::get_Location() +System.String LeanDiagnostics/Diagnostic::get_Module() +System.String LeanDiagnostics/Report::get_Shortened() +System.String LeanDiagnostics/Report::get_Text() +System.String LeanOutput/InternalError::get_Location() +System.String LeanOutput/InternalError::get_Message() +System.String LeanOutput/InternalError::get_Module() +System.String Nethermind.LeanCIL.Arrays/Element::get_Lean() +System.String Nethermind.LeanCIL.Arrays/NumericAccess::get_Shape() +System.String Nethermind.LeanCIL.Arrays/NumericAccess::get_Typed() +System.String Nethermind.LeanCIL.Blockers/Finding::get_Construct() +System.String Nethermind.LeanCIL.Blockers/Finding::get_Id() +System.String Nethermind.LeanCIL.Blockers/Outcome::get_Method() +System.String Nethermind.LeanCIL.Blockers/Outcome::get_Profile() +System.String Nethermind.LeanCIL.Blockers/Outcome::get_Reason() +System.String Nethermind.LeanCIL.Blockers/Outcome::get_Signature() +System.String Nethermind.LeanCIL.Blockers/Outcome::get_Status() +System.String Nethermind.LeanCIL.Blockers/Reach::get_Profile() +System.String Nethermind.LeanCIL.Blockers/Reach::get_Signature() +System.String Nethermind.LeanCIL.Blockers/Site::get_Instruction() +System.String Nethermind.LeanCIL.Blockers/Site::get_Message() +System.String Nethermind.LeanCIL.Blockers/Site::get_Method() +System.String Nethermind.LeanCIL.Blockers/Site::get_Source() +System.String Nethermind.LeanCIL.ClassViews/Field::get_Name() +System.String Nethermind.LeanCIL.ClassViews/FieldOperation::get_Owner() +System.String Nethermind.LeanCIL.ClassViews/View::get_FullName() +System.String Nethermind.LeanCIL.ClassViews/View::get_Path() +System.String Nethermind.LeanCIL.ClassViews/View::get_Type() +System.String Nethermind.LeanCIL.ComputedLayout::get_Identity() +System.String Nethermind.LeanCIL.ConstantReturns/Closed::get_Architecture() +System.String Nethermind.LeanCIL.ContractBundleManifest::get_FrameworkKey() +System.String Nethermind.LeanCIL.ContractBundleManifest::get_LeanCilRevision() +System.String Nethermind.LeanCIL.ContractCertification::get_ClosureDigest() +System.String Nethermind.LeanCIL.ContractCertification::get_Identity() +System.String Nethermind.LeanCIL.ContractCertification::get_ImplementationSha256() +System.String Nethermind.LeanCIL.ContractCertification::get_InterfaceDigest() +System.String Nethermind.LeanCIL.ContractCertification::get_Profile() +System.String Nethermind.LeanCIL.ContractCertification::get_Statement() +System.String Nethermind.LeanCIL.ContractCertification::get_StatementDigest() +System.String Nethermind.LeanCIL.ContractCertification::get_Theorem() +System.String Nethermind.LeanCIL.ContractDependency::get_AssemblyIdentity() +System.String Nethermind.LeanCIL.ContractDependency::get_ImplementationSha256() +System.String Nethermind.LeanCIL.ContractDependency::get_InterfaceDigest() +System.String Nethermind.LeanCIL.ContractExport::get_ClosureDigest() +System.String Nethermind.LeanCIL.ContractExport::get_ContractConstant() +System.String Nethermind.LeanCIL.ContractExport::get_DefinitionSignature() +System.String Nethermind.LeanCIL.ContractExport::get_Identity() +System.String Nethermind.LeanCIL.ContractExport::get_Kind() +System.String Nethermind.LeanCIL.ContractExport::get_Signature() +System.String Nethermind.LeanCIL.ContractExport::get_Visibility() +System.String Nethermind.LeanCIL.ContractExportProfile::get_Artifact() +System.String Nethermind.LeanCIL.ContractExportProfile::get_Profile() +System.String Nethermind.LeanCIL.ContractExportRequest::get_Artifact() +System.String Nethermind.LeanCIL.ContractExportRequest::get_Contract() +System.String Nethermind.LeanCIL.ContractExportRequest::get_Method() +System.String Nethermind.LeanCIL.ContractExportRequest::get_Program() +System.String Nethermind.LeanCIL.ContractExportRequest::get_ReferenceArtifact() +System.String Nethermind.LeanCIL.ContractExportRequest::get_ReferenceProfile() +System.String Nethermind.LeanCIL.ContractExportRequest::get_Theorem() +System.String Nethermind.LeanCIL.ContractLayout::get_Identity() +System.String Nethermind.LeanCIL.ContractLayout::get_Name() +System.String Nethermind.LeanCIL.ContractLayout::get_Scope() +System.String Nethermind.LeanCIL.ContractLayoutField::get_Name() +System.String Nethermind.LeanCIL.ContractLayoutField::get_Storage() +System.String Nethermind.LeanCIL.ContractMethodMetadata/Selection::get_Identity() +System.String Nethermind.LeanCIL.ContractMethodSignature::get_Name() +System.String Nethermind.LeanCIL.ContractSource::get_Module() +System.String Nethermind.LeanCIL.ContractSource::get_Source() +System.String Nethermind.LeanCIL.ContractType::get_Kind() +System.String Nethermind.LeanCIL.ContractType::get_Name() +System.String Nethermind.LeanCIL.ContractType::get_Scope() +System.String Nethermind.LeanCIL.ExceptionLint/Declaration::get_Exception() +System.String Nethermind.LeanCIL.ExceptionLint/Declaration::get_Text() +System.String Nethermind.LeanCIL.ExceptionLint/Site::get_Method() +System.String Nethermind.LeanCIL.ExceptionLint/Site::get_Origin() +System.String Nethermind.LeanCIL.FieldRef::get_Owner() +System.String Nethermind.LeanCIL.FieldRef::get_Type() +System.String Nethermind.LeanCIL.FrameMetadata::get_Declared() +System.String Nethermind.LeanCIL.FrameMetadata::get_ReturnsValueLean() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_CertificationStatement() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_CertificationStatementDigest() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_CertificationTheorem() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_ClosureDigest() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_ComputedLayouts() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_ContractConstant() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_ContractKind() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_DefinitionSignature() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_Identity() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_ImplementationPath() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_ImplementationSha256() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_InterfaceDigest() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_Profile() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_SidecarPath() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_SidecarSha256() +System.String Nethermind.LeanCIL.ImportResolution/Binding::get_Signature() +System.String Nethermind.LeanCIL.ImportResolution/Library::get_SidecarSha256() +System.String Nethermind.LeanCIL.ImportResolution/Node::get_AssemblyIdentity() +System.String Nethermind.LeanCIL.ImportResolution/Node::get_ExpectedInterfaceDigest() +System.String Nethermind.LeanCIL.ImportResolution/Node::get_ImplementationPath() +System.String Nethermind.LeanCIL.ImportResolution/Node::get_ImplementationSha256() +System.String Nethermind.LeanCIL.ImportResolution/Node::get_InterfaceDigest() +System.String Nethermind.LeanCIL.ImportResolution/Node::get_SidecarPath() +System.String Nethermind.LeanCIL.ImportResolution/Source::get_ExpectedInterfaceDigest() +System.String Nethermind.LeanCIL.ImportResolution/Source::get_ExpectedSha256() +System.String Nethermind.LeanCIL.ImportResolution/Source::get_ImplementationPath() +System.String Nethermind.LeanCIL.ImportResolution/Source::get_SidecarPath() +System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_AdvertisedInterfaceDigest() +System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_Assembly() +System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_AssetPath() +System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_AssetRuntimeIdentifier() +System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_Implementation() +System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_Library() +System.String Nethermind.LeanCIL.ImportResolver/Resolved::get_Sidecar() +System.String Nethermind.LeanCIL.InitializationAssumption::get_AssemblyIdentity() +System.String Nethermind.LeanCIL.InitializationAssumption::get_TypeName() +System.String Nethermind.LeanCIL.InitializedStaticValue::get_Literal() +System.String Nethermind.LeanCIL.InterfaceModule::get_Name() +System.String Nethermind.LeanCIL.InterfaceModule::get_Source() +System.String Nethermind.LeanCIL.LayoutEvidence::IdentityTerm(System.Int32) +System.String Nethermind.LeanCIL.LayoutEvidence::get_Architecture() +System.String Nethermind.LeanCIL.LeanDoc/Text::get_Value() +System.String Nethermind.LeanCIL.LeanText::Bool(System.Boolean) +System.String Nethermind.LeanCIL.LocalKind/MethodPointer::get_Signature() +System.String Nethermind.LeanCIL.LocalKind/MethodTable::get_Signature() +System.String Nethermind.LeanCIL.LocalKind/NativeMethodTable::get_Signature() +System.String Nethermind.LeanCIL.LocalKind/Object::ToLean() +System.String Nethermind.LeanCIL.LocalKind/Reference::ToLean() +System.String Nethermind.LeanCIL.LocalKind/Word32::ToLean() +System.String Nethermind.LeanCIL.LocalKind/Word64::ToLean() +System.String Nethermind.LeanCIL.ManagedTarget::get_Identity() +System.String Nethermind.LeanCIL.MetadataValidation/InstanceField::get_Name() +System.String Nethermind.LeanCIL.MetadataValidation/LayoutContext::get_Architecture() +System.String Nethermind.LeanCIL.MethodRef::get_Owner() +System.String Nethermind.LeanCIL.MethodRef::get_ReturnType() +System.String Nethermind.LeanCIL.MethodSpecialization/Origin::get_Definition() +System.String Nethermind.LeanCIL.NarrowingText::LeanName(Nethermind.LeanCIL.Narrowing) +System.String Nethermind.LeanCIL.PinnedAssemblies/Pin::get_Identity() +System.String Nethermind.LeanCIL.PinnedAssemblies/Pin::get_Sha256() +System.String Nethermind.LeanCIL.PinnedAssemblies/Source::get_ExpectedSha256() +System.String Nethermind.LeanCIL.PinnedAssemblies/Source::get_Path() +System.String Nethermind.LeanCIL.ProfileInitializationAssumptions::get_Profile() +System.String Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot::get_Hash() +System.String Nethermind.LeanCIL.Program/ExtractionBatch::get_ExtractorHash() +System.String Nethermind.LeanCIL.Program/ExtractionRequest::get_Assembly() +System.String Nethermind.LeanCIL.Program/ExtractionRequest::get_Entry() +System.String Nethermind.LeanCIL.Program/ExtractionRequest::get_Identity() +System.String Nethermind.LeanCIL.Program/ExtractionRequest::get_Output() +System.String Nethermind.LeanCIL.Program/ExtractionRequest::get_Profile() +System.String Nethermind.LeanCIL.ProofOutline/Access::get_Formed() +System.String Nethermind.LeanCIL.ProofOutline/Access::get_Value() +System.String Nethermind.LeanCIL.ProofOutline/AddedReference::get_Base() +System.String Nethermind.LeanCIL.ProofOutline/AddedReference::get_Index() +System.String Nethermind.LeanCIL.ProofOutline/Allocation::get_Array() +System.String Nethermind.LeanCIL.ProofOutline/Allocation::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/Allocation::get_Scope() +System.String Nethermind.LeanCIL.ProofOutline/CallStep::get_Input() +System.String Nethermind.LeanCIL.ProofOutline/CallStep::get_Where() +System.String Nethermind.LeanCIL.ProofOutline/Callee::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/Component::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Array() +System.String Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Formed() +System.String Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Index() +System.String Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Slot() +System.String Nethermind.LeanCIL.ProofOutline/ElementAccess::get_Value() +System.String Nethermind.LeanCIL.ProofOutline/ElementHelper::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/Extraction::get_Architecture() +System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Element() +System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Held() +System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Length() +System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/HeldArray::get_Whole() +System.String Nethermind.LeanCIL.ProofOutline/LocalAccess::get_Value() +System.String Nethermind.LeanCIL.ProofOutline/LoopOutline::get_Memory() +System.String Nethermind.LeanCIL.ProofOutline/Number::get_Stacked() +System.String Nethermind.LeanCIL.ProofOutline/Number::get_Term() +System.String Nethermind.LeanCIL.ProofOutline/ObjectFrame::get_Entry() +System.String Nethermind.LeanCIL.ProofOutline/ObjectFrame::get_Footprint() +System.String Nethermind.LeanCIL.ProofOutline/ObjectTerm::get_Term() +System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/ArrayArgument::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/ElementIndex::get_Term() +System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::get_Formed() +System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::get_Term() +System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::get_Term() +System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk::get_Returned() +System.String Nethermind.LeanCIL.ProofOutline/ObjectWalk::get_Stopped() +System.String Nethermind.LeanCIL.ProofOutline/Outline::get_Signature() +System.String Nethermind.LeanCIL.ProofOutline/Outline::get_Source() +System.String Nethermind.LeanCIL.ProofOutline/Parameter::get_ArrayElement() +System.String Nethermind.LeanCIL.ProofOutline/Parameter::get_ElementDefinition() +System.String Nethermind.LeanCIL.ProofOutline/Parameter::get_Lean() +System.String Nethermind.LeanCIL.ProofOutline/Parameter::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/Parameter::get_Source() +System.String Nethermind.LeanCIL.ProofOutline/Reached::get_Held() +System.String Nethermind.LeanCIL.ProofOutline/Reached::get_Holder() +System.String Nethermind.LeanCIL.ProofOutline/Reached::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/ReceiverField::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/ReceiverField::get_OwnedName() +System.String Nethermind.LeanCIL.ProofOutline/ReceiverField::get_ReadName() +System.String Nethermind.LeanCIL.ProofOutline/ReceiverLoop::get_receiver() +System.String Nethermind.LeanCIL.ProofOutline/ReferenceArgument::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/Result::get_Constructor() +System.String Nethermind.LeanCIL.ProofOutline/Result::get_Lean() +System.String Nethermind.LeanCIL.ProofOutline/Returning::get_Contract() +System.String Nethermind.LeanCIL.ProofOutline/Returning::get_Post() +System.String Nethermind.LeanCIL.ProofOutline/ScalarCheck::get_Fits() +System.String Nethermind.LeanCIL.ProofOutline/ScalarCheck::get_Lemma() +System.String Nethermind.LeanCIL.ProofOutline/ScalarCheck::get_Operation() +System.String Nethermind.LeanCIL.ProofOutline/ScalarSplit::get_Condition() +System.String Nethermind.LeanCIL.ProofOutline/ScalarWord::get_Term() +System.String Nethermind.LeanCIL.ProofOutline/Slot::get_Home() +System.String Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Checked() +System.String Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Head() +System.String Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Remaining() +System.String Nethermind.LeanCIL.ProofOutline/SpanTerm::get_Tail() +System.String Nethermind.LeanCIL.ProofOutline/SpanTerm::get_View() +System.String Nethermind.LeanCIL.ProofOutline/Specification::get_ResultWord() +System.String Nethermind.LeanCIL.ProofOutline/Split::get_Condition() +System.String Nethermind.LeanCIL.ProofOutline/Split::get_Fact() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Address() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Initial() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Owned() +System.String Nethermind.LeanCIL.ProofOutline/StructScalar::get_Read() +System.String Nethermind.LeanCIL.ProofOutline/StructWalk::get_Returned() +System.String Nethermind.LeanCIL.ProofOutline/StructWalk::get_Stopped() +System.String Nethermind.LeanCIL.ProofOutline/Stub::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/StubCall::get_Input() +System.String Nethermind.LeanCIL.ProofOutline/StubCall::get_Where() +System.String Nethermind.LeanCIL.ProofOutline/Thrown::get_Where() +System.String Nethermind.LeanCIL.ProofOutline/Unknown::get_What() +System.String Nethermind.LeanCIL.ProofOutline/ViewField::get_Name() +System.String Nethermind.LeanCIL.ProofOutline/ViewField::get_Path() +System.String Nethermind.LeanCIL.ProofOutline/WordRead::get_Index() +System.String Nethermind.LeanCIL.ProofOutline/WordRead::get_Name() +System.String Nethermind.LeanCIL.RuntimeAsset::get_AssetPath() +System.String Nethermind.LeanCIL.RuntimeAsset::get_AssetRid() +System.String Nethermind.LeanCIL.RuntimeAsset::get_Library() +System.String Nethermind.LeanCIL.RuntimeAsset::get_LocalPath() +System.String Nethermind.LeanCIL.RuntimeAsset::get_Name() +System.String Nethermind.LeanCIL.RuntimeAsset::get_PackagePath() +System.String Nethermind.LeanCIL.RuntimeAsset::get_Type() +System.String Nethermind.LeanCIL.StaticIdentityRecord::get_AssemblyIdentity() +System.String Nethermind.LeanCIL.StaticIdentityRecord::get_FieldName() +System.String Nethermind.LeanCIL.StaticIdentityRecord::get_Identity() +System.String Nethermind.LeanCIL.StaticValues/Value::get_Literal() +System.String Nethermind.LeanCIL.StructuralSignatures/Primitive::get_Name() +System.String Nethermind.LeanCIL.Symbols/PdbFile::get_Sha256() +System.String Nethermind.LeanCIL.TemplateEmitter/Method::get_Identity() +System.String Nethermind.LeanCIL.TemplateEmitter/Options::get_ModuleName() +System.String Nethermind.LeanCIL.TemplateEmitter/Options::get_ModulePath() +System.String Nethermind.LeanCIL.TemplateEmitter/Options::get_Namespace() +System.String Nethermind.LeanCIL.TemplateEmitter/Options::get_Output() +System.String Nethermind.LeanCIL.TemplateEmitter/Row::get_ComparisonBody() +System.String Nethermind.LeanCIL.TemplateEmitter/Template::get_Identity() +System.String Nethermind.LeanCIL.TemplateEmitter/Variant::get_AssemblyHash() +System.String Nethermind.LeanCIL.TemplateEmitter/Variant::get_Directory() +System.String Nethermind.LeanCIL.TypeTests/Node::get_Kind() +System.String Nethermind.LeanCIL.TypeTests/Node::get_Name() +System.String Nethermind.LeanCIL.TypeTests/Node::get_Scope() +System.String Program/AuditConstantRequest::get_name() +System.String Program/AuditRefutation::get_contract() +System.String Program/AuditRefutation::get_program() +System.String Program/AuditRequest::get_name() +System.String Program/AuditedClosure::get_canonical() +System.String Program/AuditedClosure::get_error() +System.String Program/AuditedClosure::get_kind() +System.String Program/AuditedClosure::get_name() +System.String Program/AuditedInterfaces::get_error() +System.String Program/AuditedNamespaces::get_error() +System.String Program/AuditedTheorem::get_error() +System.String Program/AuditedTheorem::get_mismatch() +System.String Program/AuditedTheorem::get_name() +System.String Program/AuditedTheorem::get_statement() +System.String Program/AuditedTheorem::get_termError() +System.String Program/BatchManifest::get_proofRoot() +System.String Program/BatchOptions::get_ResultsPath() +System.String Program/BatchRequest::get_expect() +System.String Program/BatchRequest::get_output() +System.String Program/BatchRequest::get_proof() +System.String Program/BatchRequest::get_theorem() +System.String Program/BatchResult::get_error() +System.String Program/BatchResult::get_expect() +System.String Program/BatchResult::get_libraryBuild() +System.String Program/BatchResult::get_output() +System.String Program/BatchResult::get_proof() +System.String Program/BatchResult::get_stage() +System.String Program/BatchResult::get_status() +System.String Program/BatchResult::get_theorem() +System.String Program/BindingCatalog::get_GeneratedRoot() +System.String Program/BindingCatalog::get_Path() +System.String Program/BindingFactResult::get_reason() +System.String Program/BindingFactResult::get_theorem() +System.String Program/BindingImagePin::get_Identity() +System.String Program/BindingLibraryPreparation::get_Library() +System.String Program/BindingLibraryPreparation::get_LibraryKey() +System.String Program/BindingLibraryPreparation::get_Toolchain() +System.String Program/BindingPlan::get_Output() +System.String Program/BindingPublication::get_Contract() +System.String Program/BindingPublication::get_Identity() +System.String Program/BindingPublication::get_InterfaceDigest() +System.String Program/BindingPublication::get_Profile() +System.String Program/BindingRequest::get_CoveragePredicate() +System.String Program/BindingRequest::get_Module() +System.String Program/BuildDiagnostic::get_Code() +System.String Program/BuildDiagnostic::get_File() +System.String Program/BuildDiagnostic::get_Message() +System.String Program/CatalogFamily::get_template() +System.String Program/CatalogFile::get_generatedRoot() +System.String Program/CatalogGate::get_Method() +System.String Program/CatalogGate::get_Name() +System.String Program/CatalogGate::get_Proof() +System.String Program/CatalogGate::get_Theorem() +System.String Program/CatalogGatePattern::get_method() +System.String Program/CatalogGatePattern::get_name() +System.String Program/CatalogGatePattern::get_proof() +System.String Program/CatalogGatePattern::get_theorem() +System.String Program/CertifiedFingerprintManifest::get_Tools() +System.String Program/CertifiedReleaseAsset::get_Archive() +System.String Program/CertifiedReleaseAsset::get_ArchiveSha256() +System.String Program/CertifiedReleaseAsset::get_LibraryKey() +System.String Program/CertifiedReleaseAsset::get_ManifestSha256() +System.String Program/CertifiedReleaseAsset::get_Runtime() +System.String Program/CertifiedReleaseAsset::get_Tag() +System.String Program/CheckedContractExport::get_AuditSource() +System.String Program/CheckedContractExport::get_AuthorModule() +System.String Program/CheckedContractExport::get_Statement() +System.String Program/CheckedContractExport::get_StatementDigest() +System.String Program/ClosureEntry::get_Directory() +System.String Program/ClosureEntry::get_Key() +System.String Program/ConsumerDependency::get_Directory() +System.String Program/ConsumerDependency::get_Name() +System.String Program/ConsumerDependency::get_Revision() +System.String Program/ConsumerDependency::get_Source() +System.String Program/ConsumerDependency::get_Url() +System.String Program/ConsumerExportAudit::get_Source() +System.String Program/ConsumerRequest::get_Assembly() +System.String Program/ConsumerRequest::get_Output() +System.String Program/ConsumerRequest::get_Proof() +System.String Program/ConsumerRequest::get_Settings() +System.String Program/ConsumerRequest::get_Theorem() +System.String Program/ConsumerSession::get_Generated() +System.String Program/ConsumerSupportManifest::get_Library() +System.String Program/ConsumerSupportManifest::get_Policy() +System.String Program/ConsumerSupportManifest::get_Toolchain() +System.String Program/ConsumerSupportManifest::get_Tools() +System.String Program/DeepCheckAttempt::get_IdentitySha256() +System.String Program/DeepSessionContribution::get_ResultKey() +System.String Program/DeepSessionContribution::get_SourceDirectory() +System.String Program/DeploymentDependency::get_acceptedVia() +System.String Program/DeploymentDependency::get_assemblyIdentity() +System.String Program/DeploymentDependency::get_implementationSha256() +System.String Program/DeploymentDependency::get_interfaceDigest() +System.String Program/DeploymentDependency::get_sourceBundleDigest() +System.String Program/DigestFile::get_path() +System.String Program/DigestFile::get_sha256() +System.String Program/EditorProof::get_Proof() +System.String Program/EditorProof::get_Theorem() +System.String Program/EditorSelection::get_Assembly() +System.String Program/EditorSelection::get_AssemblyIdentity() +System.String Program/EditorSelection::get_Method() +System.String Program/EditorSelection::get_Profile() +System.String Program/EnvironmentEntry::get_Name() +System.String Program/EnvironmentEntry::get_Value() +System.String Program/ExportClosure::get_Canonical() +System.String Program/ExportClosure::get_Contract() +System.String Program/ExportClosure::get_Digest() +System.String Program/ExportClosure::get_Kind() +System.String Program/ExtractedMethod::get_assembly() +System.String Program/ExtractedMethod::get_assemblySha256() +System.String Program/ExtractedMethod::get_signature() +System.String Program/ExtractionComponents::get_Aggregate() +System.String Program/ExtractionComponents::get_InputHash() +System.String Program/ExtractionIdentity::get_Output() +System.String Program/ExtractionIdentity::get_Sha256() +System.String Program/FingerprintInput::get_Key() +System.String Program/FingerprintInput::get_Name() +System.String Program/FingerprintInput::get_Source() +System.String Program/FullBundle::get_Consumer() +System.String Program/FullBundle::get_Staging() +System.String Program/FullImportResult::get_Audit() +System.String Program/FullImportResult::get_Theorem() +System.String Program/FullLibrary::get_AssemblyIdentity() +System.String Program/FullLibrary::get_BundleDigest() +System.String Program/FullLibrary::get_Contract() +System.String Program/FullLibrary::get_ExpectedModule() +System.String Program/FullLibrary::get_GeneratedSource() +System.String Program/FullLibrary::get_Kind() +System.String Program/FullLibrary::get_Module() +System.String Program/FullLibrary::get_TransferSource() +System.String Program/FullSelection::get_Bundle() +System.String Program/FullSelection::get_Digest() +System.String Program/FullSelection::get_Implementation() +System.String Program/FullSelection::get_ImplementationSha256() +System.String Program/FullSelection::get_Sidecar() +System.String Program/FullSelection::get_SidecarSha256() +System.String Program/FullSlot::get_Artifact() +System.String Program/FullSlot::get_Author() +System.String Program/FullSlot::get_Generated() +System.String Program/FullSlot::get_GeneratedSha256() +System.String Program/GateBuild::get_project() +System.String Program/GateBuildPlan::get_AssemblyName() +System.String Program/GateBuildPlan::get_Directory() +System.String Program/GateBuildPlan::get_Project() +System.String Program/GateDefaults::get_assembly() +System.String Program/GateGenerated::get_Path() +System.String Program/GateGenerated::get_Stage() +System.String Program/GateGenerated::get_Template() +System.String Program/GateManifest::get_generatedRoot() +System.String Program/GateManifest::get_outputRoot() +System.String Program/GateManifest::get_proofRoot() +System.String Program/GatePlan::get_manifest() +System.String Program/GatePlan::get_revision() +System.String Program/GatePlanGroup::get_id() +System.String Program/GatePlanSelection::get_method() +System.String Program/GatePlanSelection::get_profile() +System.String Program/GateProject::get_assembly() +System.String Program/GateProject::get_path() +System.String Program/GateRefutation::get_proof() +System.String Program/GateRefutation::get_template() +System.String Program/GateRefutation::get_theorem() +System.String Program/GateRefutationResult::get_error() +System.String Program/GateRefutationResult::get_proof() +System.String Program/GateRefutationResult::get_stage() +System.String Program/GateRefutationResult::get_status() +System.String Program/GateRefutationResult::get_theorem() +System.String Program/GateResult::get_build() +System.String Program/GateResult::get_error() +System.String Program/GateResult::get_expect() +System.String Program/GateResult::get_extraction() +System.String Program/GateResult::get_gate() +System.String Program/GateResult::get_id() +System.String Program/GateResult::get_libraryBuild() +System.String Program/GateResult::get_output() +System.String Program/GateResult::get_profile() +System.String Program/GateResult::get_program() +System.String Program/GateResult::get_proof() +System.String Program/GateResult::get_stage() +System.String Program/GateResult::get_status() +System.String Program/GateResult::get_theorem() +System.String Program/GateResult::get_variant() +System.String Program/GateResults::get_manifest() +System.String Program/GateResults::get_revision() +System.String Program/GateStatement::get_production() +System.String Program/GateStatement::get_status() +System.String Program/GateStatement::get_variant() +System.String Program/GateTemplate::get_path() +System.String Program/GateTemplateUse::get_template() +System.String Program/GateVariant::get_assembly() +System.String Program/GateVariant::get_expect() +System.String Program/GateVariant::get_name() +System.String Program/GateVariant::get_program() +System.String Program/GateVariant::get_programOf() +System.String Program/GateVariant::get_statement() +System.String Program/GateVariant::get_theorem() +System.String Program/GeneratedExtraction::get_Assembly() +System.String Program/GeneratedExtraction::get_Entry() +System.String Program/GeneratedExtraction::get_Output() +System.String Program/GeneratedExtraction::get_Profile() +System.String Program/ImageResult::get_identity() +System.String Program/ImageResult::get_reason() +System.String Program/ImageResult::get_sha256() +System.String Program/ImageResult::get_theorem() +System.String Program/LoadedGates::get_Digest() +System.String Program/LoadedGates::get_Directory() +System.String Program/LoadedGates::get_OutputRoot() +System.String Program/LoadedGates::get_Path() +System.String Program/LoadedGates::get_ProofRoot() +System.String Program/PreparedContractExport::get_ComputedLayouts() +System.String Program/PreparedContractExport::get_DefinitionSignature() +System.String Program/PreparedContractExport::get_Identity() +System.String Program/PreparedContractExport::get_Profile() +System.String Program/PreparedContractExport::get_Proof() +System.String Program/PreparedContractExport::get_Signature() +System.String Program/PreparedContractExport::get_Visibility() +System.String Program/ProofGateEntry::get_assembly() +System.String Program/ProofGateEntry::get_method() +System.String Program/ProofGateEntry::get_name() +System.String Program/ProofGateEntry::get_proof() +System.String Program/ProofGateEntry::get_theorem() +System.String Program/ProofMessage::get_severity() +System.String Program/ProofMessage::get_text() +System.String Program/PublishedImport::get_acceptedVia() +System.String Program/PublishedImport::get_certificationStatement() +System.String Program/PublishedImport::get_certificationStatementDigest() +System.String Program/PublishedImport::get_certificationTheorem() +System.String Program/PublishedImport::get_deploymentObligation() +System.String Program/PublishedImport::get_identity() +System.String Program/PublishedImport::get_implementationSha256() +System.String Program/PublishedImport::get_interfaceDigest() +System.String Program/PublishedImport::get_profile() +System.String Program/PublishedImport::get_sidecarSha256() +System.String Program/PublishedImport::get_sourceBundleDigest() +System.String Program/ReplayRejection::get_Declaration() +System.String Program/Report::get_assemblySha256() +System.String Program/Report::get_batchSourceSha256() +System.String Program/Report::get_batchTheorem() +System.String Program/Report::get_compositionAudit() +System.String Program/Report::get_compositionTheorem() +System.String Program/Report::get_consumerSupportSha256() +System.String Program/Report::get_extractionAudit() +System.String Program/Report::get_identitySha256() +System.String Program/Report::get_libraryBuild() +System.String Program/Report::get_libraryKey() +System.String Program/Report::get_theorem() +System.String Program/Report::get_theoremAudit() +System.String Program/StandaloneCachePromotion::get_ExtractionKey() +System.String Program/StandaloneCachePromotion::get_ExtractionSuffix() +System.String Program/StandaloneCachePromotion::get_FullLibraryKey() +System.String Program/StandaloneCachePromotion::get_ProofKey() +System.String Program/StandaloneCachePromotion::get_ProofSuffix() +System.String Program/StatementDefinition::get_name() +System.String Program/StatementDefinition::get_text() +System.String Program/StatementIdentity::get_mismatch() +System.String Program/StatementIdentity::get_termError() +System.String Program/StatementRequest::get_Expected() +System.String Program/StatementSources::get_lean() +System.String Program/StatementSources::get_leanToolchain() +System.String Program/StatementSources::get_strictBinding() +System.String Program/StatementSubject::get_head() +System.String Program/StatementSubject::get_role() +System.String Program/StatementSubject::get_signature() +System.String Program/StatementView::get_statement() +System.String Program/TrustedSupport::get_Path() +System.String Program/VerificationSettings::get_proof() +System.String Program/VerificationSettings::get_proofRoot() +System.String Program/VerificationSettings::get_theorem() +System.String Program/VerifiedRefutation::get_contract() +System.String Program/WatchResult::get_Message() +System.String Program/WatchResult::get_Method() +System.String Program/WatchResult::get_Module() +System.String Program/WatchResult::get_Profile() +System.String Program/WatchResult::get_Proof() +System.String Program/WatchResult::get_Status() +System.String Program/WatchResult::get_Theorem() +System.String Program/WatchSettings::get_Configuration() +System.String Program/WatchSettings::get_Root() +System.String Program/WatchState::get_Extracted() +System.String Program/WatchState::get_Status() +System.String Program/WatchState::get_Symbols() +System.String Program/WorkspaceSettings::get_Assembly() +System.String Program/WorkspaceSettings::get_AssemblyIdentity() +System.String Program/WorkspaceSettings::get_Method() +System.String Program/WorkspaceSettings::get_Profile() +System.String Program/WorkspaceSettings::get_Proof() +System.String Program/WorkspaceSettings::get_ProofRoot() +System.String Program/WorkspaceSettings::get_Theorem() +System.String VerifyReports/ManifestMethod::get_Signature() +System.String VerifyReports/SourcePoint::get_File() +System.String[] FailureReports/Report::get_rerun() +System.String[] Nethermind.LeanCIL.Blockers/Outcome::get_DecodedMethods() +System.String[] Nethermind.LeanCIL.Blockers/Outcome::get_TypeArguments() +System.String[] Nethermind.LeanCIL.ContractExport::get_Profiles() +System.String[] Nethermind.LeanCIL.ContractExport::get_TypeArguments() +System.String[] Nethermind.LeanCIL.ContractExportRequest::get_TypeArguments() +System.String[] Nethermind.LeanCIL.ImportResolution/Binding::get_TypeArguments() +System.String[] Nethermind.LeanCIL.MethodSpecialization/Origin::get_TypeArguments() +System.String[] Nethermind.LeanCIL.Program/ExtractionRequest::get_InitializedTypes() +System.String[] Nethermind.LeanCIL.Program/ExtractionRequest::get_TypeArguments() +System.String[] Nethermind.LeanCIL.ProofOutline/ObjectFrame::get_Formed() +System.String[] Nethermind.LeanCIL.ProofOutline/Stub::get_Inputs() +System.String[] Nethermind.LeanCIL.RuntimeAsset::get_ReferenceAssets() +System.String[] Nethermind.LeanCIL.StaticValues/Value::get_Arguments() +System.String[] Nethermind.LeanCIL.TemplateEmitter/Options::get_Extractions() +System.String[] Nethermind.LeanCIL.TemplateEmitter/Template::get_Callees() +System.String[] Program/AuditConstantRequest::get_axioms() +System.String[] Program/AuditNamespaces::get_axioms() +System.String[] Program/AuditNamespaces::get_prefixes() +System.String[] Program/AuditRequest::get_allowedModules() +System.String[] Program/AuditRequest::get_axioms() +System.String[] Program/AuditedNamespaces::get_axioms() +System.String[] Program/AuditedOleans::get_replayed() +System.String[] Program/AuditedTheorem::get_axioms() +System.String[] Program/BatchResult::get_axioms() +System.String[] Program/CatalogGate::get_Inputs() +System.String[] Program/CatalogGate::get_Profiles() +System.String[] Program/CatalogGatePattern::get_profiles() +System.String[] Program/ClosureEntry::get_Modules() +System.String[] Program/ConsumerExportAudit::get_Closures() +System.String[] Program/ConsumerSupportModule::get_Files() +System.String[] Program/ConsumerSupportModule::get_Imports() +System.String[] Program/DeepSessionContribution::get_Modules() +System.String[] Program/DeploymentDependency::get_dependencies() +System.String[] Program/EditorSelection::get_InitializedTypes() +System.String[] Program/GateDefaults::get_initializedTypes() +System.String[] Program/GateDefaults::get_typeArguments() +System.String[] Program/GateManifest::get_catalogs() +System.String[] Program/GatePlan::get_selected() +System.String[] Program/GatePlanGroup::get_rows() +System.String[] Program/GatePlanSelection::get_changed() +System.String[] Program/GatePlanSelection::get_only() +System.String[] Program/GateRefutationResult::get_axioms() +System.String[] Program/GateResult::get_axioms() +System.String[] Program/GateSelectionRules::get_all() +System.String[] Program/GateSelectionRules::get_ignore() +System.String[] Program/GateVariant::get_initializedTypes() +System.String[] Program/GateVariant::get_profiles() +System.String[] Program/GeneratedExtraction::get_InitializedTypes() +System.String[] Program/GeneratedExtraction::get_TypeArguments() +System.String[] Program/LibraryAudit::get_Modules() +System.String[] Program/LibraryAudit::get_Replayed() +System.String[] Program/LoadedGates::get_All() +System.String[] Program/LoadedGates::get_Ignore() +System.String[] Program/PreparedContractExport::get_TypeArguments() +System.String[] Program/ProofGateEntry::get_initializedTypes() +System.String[] Program/ProofGateEntry::get_profiles() +System.String[] Program/ProofGateEntry::get_typeArguments() +System.String[] Program/PublishedImport::get_typeArguments() +System.String[] Program/Report::get_axioms() +System.String[] Program/Report::get_contractImports() +System.String[] Program/Report::get_initializedTypes() +System.String[] Program/Report::get_libraryModules() +System.String[] Program/Report::get_opaqueStaticValues() +System.String[] Program/Report::get_staticValues() +System.String[] Program/WatchSettings::get_Projects() +System.String[] Program/WorkspaceSettings::get_InitializedTypes() +System.String[] VerifyReports/ManifestMethod::get_Instructions() +System.UInt16 Nethermind.LeanCIL.AssemblyVersion::get_Build() +System.UInt16 Nethermind.LeanCIL.AssemblyVersion::get_Major() +System.UInt16 Nethermind.LeanCIL.AssemblyVersion::get_Minor() +System.UInt16 Nethermind.LeanCIL.AssemblyVersion::get_Revision() +System.UInt32 Nethermind.LeanCIL.MetadataImage/NamedRow::get_Assembly() +System.UInt32 Nethermind.LeanCIL.MetadataImage/NamedRow::get_Parent() +System.UInt32 Nethermind.LeanCIL.MetadataImage/TypeNameRow::get_ResolutionScope() +System.UInt32 Nethermind.LeanCIL.MetadataSlices/Slice::GenericOwnerCode(System.UInt32) +System.UInt32 Nethermind.LeanCIL.MetadataSlices::OmittedHeapOffset(System.Int32) +System.UInt32 Nethermind.LeanCIL.StructuralSignatures/Primitive::get_Assembly() +System.UInt32 Nethermind.LeanCIL.StructuralSignatures/Primitive::get_Token() +System.UInt64 Nethermind.LeanCIL.StaticValues/Word::get_Bits() +System.Void FailureReports/BindingFailure::.ctor(FailureReports/BindingFailure) +System.Void FailureReports/BindingFailure::.ctor(FailureReports/BindingFailureDigest,FailureReports/BindingFailureImage[],FailureReports/BindingFailureFact[]) +System.Void FailureReports/BindingFailureDigest::.ctor(FailureReports/BindingFailureDigest) +System.Void FailureReports/BindingFailureDigest::.ctor(System.String,System.String) +System.Void FailureReports/BindingFailureFact::.ctor(FailureReports/BindingFailureFact) +System.Void FailureReports/BindingFailureFact::.ctor(System.String,FailureReports/BindingFailureStatus,System.String) +System.Void FailureReports/BindingFailureImage::.ctor(FailureReports/BindingFailureImage) +System.Void FailureReports/BindingFailureImage::.ctor(System.String,System.String,FailureReports/BindingFailureStatus,System.String) +System.Void FailureReports/Method::.ctor(FailureReports/Method) +System.Void FailureReports/Method::.ctor(System.String,System.String,System.String) +System.Void LeanDiagnostics/Diagnostic::.ctor(LeanDiagnostics/Diagnostic) +System.Void LeanDiagnostics/Report::.ctor(LeanDiagnostics/Report) +System.Void LeanDiagnostics/Report::.ctor(System.String,System.String) +System.Void LeanOutput/InternalError::.ctor(LeanOutput/InternalError) +System.Void LeanOutput/InternalError::.ctor(System.String,System.String,System.String) +System.Void Nethermind.LeanCIL.Arrays/Element::.ctor(Nethermind.LeanCIL.Arrays/Element) +System.Void Nethermind.LeanCIL.Arrays/Element::.ctor(System.String,System.Int32,System.Boolean,Nethermind.LeanCIL.NativeOperands/Kind,System.Boolean) +System.Void Nethermind.LeanCIL.AssemblyVersion::.ctor(System.UInt16,System.UInt16,System.UInt16,System.UInt16) +System.Void Nethermind.LeanCIL.AssemblyVersion::Deconstruct(System.UInt16&,System.UInt16&,System.UInt16&,System.UInt16&) +System.Void Nethermind.LeanCIL.Blockers/Finding::set_Id(System.String) +System.Void Nethermind.LeanCIL.ClassViews/Access::.ctor(Nethermind.LeanCIL.ClassViews/Access) +System.Void Nethermind.LeanCIL.ClassViews/View::.ctor(Nethermind.LeanCIL.ClassViews/View) +System.Void Nethermind.LeanCIL.ClassViews/View::.ctor(System.String,System.String,System.String,Nethermind.LeanCIL.ClassViews/Field[]) +System.Void Nethermind.LeanCIL.ComputedLayout::.ctor(Nethermind.LeanCIL.ComputedLayout) +System.Void Nethermind.LeanCIL.ConstantReturns/Closed::.ctor(Nethermind.LeanCIL.ConstantReturns/Closed) +System.Void Nethermind.LeanCIL.ContractCertification::.ctor(Nethermind.LeanCIL.ContractCertification) +System.Void Nethermind.LeanCIL.ContractCertification::.ctor(System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,Nethermind.LeanCIL.StaticIdentityRecord[]) +System.Void Nethermind.LeanCIL.ContractDependency::.ctor(Nethermind.LeanCIL.ContractDependency) +System.Void Nethermind.LeanCIL.ContractDependency::.ctor(System.String,System.String,System.String) +System.Void Nethermind.LeanCIL.ContractExport::.ctor(Nethermind.LeanCIL.ContractExport) +System.Void Nethermind.LeanCIL.ContractExport::.ctor(System.String,System.String,System.Int32,System.Boolean,System.Boolean,System.String,System.String,System.String,System.String[],System.String,System.String,System.String[],Nethermind.LeanCIL.ContractMethodSignature,Nethermind.LeanCIL.ContractLayout[],Nethermind.LeanCIL.ProfileInitializationAssumptions[]) +System.Void Nethermind.LeanCIL.ContractExportProfile::.ctor(Nethermind.LeanCIL.ContractExportProfile) +System.Void Nethermind.LeanCIL.ContractExportProfile::.ctor(System.String,System.String) +System.Void Nethermind.LeanCIL.ContractExportRequest::.ctor(Nethermind.LeanCIL.ContractExportRequest) +System.Void Nethermind.LeanCIL.ContractExportRequest::.ctor(System.String,System.String,System.String,System.String,System.String[],Nethermind.LeanCIL.ContractExportProfile[],System.String,System.String) +System.Void Nethermind.LeanCIL.ContractLayout::.ctor(Nethermind.LeanCIL.ContractLayout) +System.Void Nethermind.LeanCIL.ContractLayout::.ctor(System.String,System.String,System.String,System.Boolean,Nethermind.LeanCIL.ContractType,System.Boolean,System.Boolean,System.Int32,System.Int32,Nethermind.LeanCIL.ContractLayoutField[]) +System.Void Nethermind.LeanCIL.ContractMethodSignature::.ctor(Nethermind.LeanCIL.ContractMethodSignature) +System.Void Nethermind.LeanCIL.ContractMethodSignature::.ctor(Nethermind.LeanCIL.ContractType,System.String,Nethermind.LeanCIL.ContractType[],Nethermind.LeanCIL.ContractType,Nethermind.LeanCIL.ContractType[],System.Int32[]) +System.Void Nethermind.LeanCIL.ContractSource::.ctor(Nethermind.LeanCIL.ContractSource) +System.Void Nethermind.LeanCIL.ContractSource::.ctor(System.String,System.String) +System.Void Nethermind.LeanCIL.ContractType::.ctor(Nethermind.LeanCIL.ContractType) +System.Void Nethermind.LeanCIL.ContractType::.ctor(System.String,System.String,System.String,Nethermind.LeanCIL.ContractType) +System.Void Nethermind.LeanCIL.ExceptionLint/Declaration::.ctor(Nethermind.LeanCIL.ExceptionLint/Declaration) +System.Void Nethermind.LeanCIL.ExceptionLint/Declaration::.ctor(System.String,System.String) +System.Void Nethermind.LeanCIL.ExceptionLint/Site::.ctor(Nethermind.LeanCIL.ExceptionLint/Site) +System.Void Nethermind.LeanCIL.ExceptionLint/Site::.ctor(System.String,System.Int32,Nethermind.LeanCIL.ExceptionType,System.String) +System.Void Nethermind.LeanCIL.FieldRef::.ctor(Nethermind.LeanCIL.FieldRef) +System.Void Nethermind.LeanCIL.FieldRef::.ctor(System.String,System.Int32,System.String,Nethermind.LeanCIL.MetadataName) +System.Void Nethermind.LeanCIL.FrameMetadata::.ctor(Nethermind.LeanCIL.FrameMetadata) +System.Void Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::.ctor(Nethermind.LeanCIL.GenericEvidence/DecodeRefusal) +System.Void Nethermind.LeanCIL.ImportResolution/Binding::.ctor(Nethermind.LeanCIL.ImportResolution/Binding) +System.Void Nethermind.LeanCIL.ImportResolution/Binding::.ctor(System.Int32,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.Int32,System.Boolean,System.String,System.String,System.String,System.String,System.String,System.Boolean,Nethermind.LeanCIL.StaticIdentityRecord[],Nethermind.LeanCIL.InitializationAssumption[],System.String,System.String[],Nethermind.LeanCIL.ContractMethodSignature,Nethermind.LeanCIL.ContractLayout[],Nethermind.LeanCIL.ContractLayout[],System.String) +System.Void Nethermind.LeanCIL.ImportResolution/Library::.ctor(Nethermind.LeanCIL.ImportResolution/Library) +System.Void Nethermind.LeanCIL.ImportResolution/Node::.ctor(Nethermind.LeanCIL.ImportResolution/Node) +System.Void Nethermind.LeanCIL.ImportResolution/Node::.ctor(System.String,System.String,System.String,System.String,System.String,System.String,System.Boolean,Nethermind.LeanCIL.ContractDependency[]) +System.Void Nethermind.LeanCIL.ImportResolution/Source::.ctor(Nethermind.LeanCIL.ImportResolution/Source) +System.Void Nethermind.LeanCIL.ImportResolution/Source::.ctor(System.String,System.String,System.String,System.String,System.Boolean) +System.Void Nethermind.LeanCIL.ImportResolver/Resolved::.ctor(Nethermind.LeanCIL.ImportResolver/Resolved) +System.Void Nethermind.LeanCIL.ImportResolver/Resolved::.ctor(System.String,System.String,System.String,System.String,System.String,System.String,System.String) +System.Void Nethermind.LeanCIL.InitializationAssumption::.ctor(Nethermind.LeanCIL.InitializationAssumption) +System.Void Nethermind.LeanCIL.InitializationAssumption::.ctor(System.String,System.String) +System.Void Nethermind.LeanCIL.InterfaceModule::.ctor(Nethermind.LeanCIL.InterfaceModule) +System.Void Nethermind.LeanCIL.InterfaceModule::.ctor(System.String,System.String) +System.Void Nethermind.LeanCIL.LayoutField::.ctor(Nethermind.LeanCIL.LayoutField) +System.Void Nethermind.LeanCIL.LayoutField::.ctor(System.Int32,System.Int32,System.Boolean) +System.Void Nethermind.LeanCIL.LayoutField::Deconstruct(System.Int32&,System.Int32&,System.Boolean&) +System.Void Nethermind.LeanCIL.LeanDoc/Item::.ctor(Nethermind.LeanCIL.LeanDoc,System.Int32) +System.Void Nethermind.LeanCIL.LeanDoc/Item::.ctor(Nethermind.LeanCIL.LeanDoc/Item) +System.Void Nethermind.LeanCIL.LeanDoc/Space::.ctor(System.Boolean,System.Int32) +System.Void Nethermind.LeanCIL.LeanDoc/Space::Deconstruct(System.Boolean&,System.Int32&) +System.Void Nethermind.LeanCIL.LeanDoc/WorkGroup::.ctor(Nethermind.LeanCIL.LeanDoc/WorkGroup) +System.Void Nethermind.LeanCIL.LocalKind/Aggregate::.ctor(Nethermind.LeanCIL.LocalKind/Aggregate) +System.Void Nethermind.LeanCIL.LocalKind/Aggregate::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.LocalKind/Aggregate::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.LocalKind/Array::.ctor(Nethermind.LeanCIL.Arrays/Element) +System.Void Nethermind.LeanCIL.LocalKind/Array::.ctor(Nethermind.LeanCIL.LocalKind/Array) +System.Void Nethermind.LeanCIL.LocalKind/EnumSpan::.ctor(Nethermind.LeanCIL.Arrays/Element,System.Boolean,System.Boolean) +System.Void Nethermind.LeanCIL.LocalKind/EnumSpan::.ctor(Nethermind.LeanCIL.LocalKind/EnumSpan) +System.Void Nethermind.LeanCIL.LocalKind/MethodPointer::.ctor(Nethermind.LeanCIL.LocalKind/MethodPointer) +System.Void Nethermind.LeanCIL.LocalKind/MethodPointer::.ctor(System.String) +System.Void Nethermind.LeanCIL.LocalKind/MethodTable::.ctor(Nethermind.LeanCIL.LocalKind/MethodTable) +System.Void Nethermind.LeanCIL.LocalKind/MethodTable::.ctor(System.String) +System.Void Nethermind.LeanCIL.LocalKind/Narrow::.ctor(Nethermind.LeanCIL.LocalKind/Narrow) +System.Void Nethermind.LeanCIL.LocalKind/Narrow::.ctor(Nethermind.LeanCIL.Narrowing) +System.Void Nethermind.LeanCIL.LocalKind/Narrow::Deconstruct(Nethermind.LeanCIL.Narrowing&) +System.Void Nethermind.LeanCIL.LocalKind/NativeMethodTable::.ctor(Nethermind.LeanCIL.LocalKind/NativeMethodTable) +System.Void Nethermind.LeanCIL.LocalKind/NativeMethodTable::.ctor(System.String) +System.Void Nethermind.LeanCIL.LocalKind/Object::.ctor() +System.Void Nethermind.LeanCIL.LocalKind/Object::.ctor(Nethermind.LeanCIL.LocalKind/Object) +System.Void Nethermind.LeanCIL.LocalKind/Reference::.ctor() +System.Void Nethermind.LeanCIL.LocalKind/Reference::.ctor(Nethermind.LeanCIL.LocalKind/Reference) +System.Void Nethermind.LeanCIL.LocalKind/Span::.ctor(Nethermind.LeanCIL.LocalKind/Span) +System.Void Nethermind.LeanCIL.LocalKind/Span::.ctor(System.Int32,System.Boolean) +System.Void Nethermind.LeanCIL.LocalKind/Span::Deconstruct(System.Int32&,System.Boolean&) +System.Void Nethermind.LeanCIL.LocalKind/Vector::.ctor(Nethermind.LeanCIL.LocalKind/Vector) +System.Void Nethermind.LeanCIL.LocalKind/Vector::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.LocalKind/Vector::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.LocalKind/Word32::.ctor() +System.Void Nethermind.LeanCIL.LocalKind/Word32::.ctor(Nethermind.LeanCIL.LocalKind/Word32) +System.Void Nethermind.LeanCIL.LocalKind/Word64::.ctor() +System.Void Nethermind.LeanCIL.LocalKind/Word64::.ctor(Nethermind.LeanCIL.LocalKind/Word64) +System.Void Nethermind.LeanCIL.LocalKind::.ctor() +System.Void Nethermind.LeanCIL.LocalKind::.ctor(Nethermind.LeanCIL.LocalKind) +System.Void Nethermind.LeanCIL.ManagedTarget::.ctor(Nethermind.LeanCIL.ManagedTarget) +System.Void Nethermind.LeanCIL.ManagedTarget::.ctor(System.String,System.Int32) +System.Void Nethermind.LeanCIL.MetadataImage/NamedRow::.ctor(Nethermind.LeanCIL.MetadataImage/NamedRow) +System.Void Nethermind.LeanCIL.MetadataImage/NamedRow::.ctor(System.Int32,System.Int32,System.UInt32,System.UInt32) +System.Void Nethermind.LeanCIL.MetadataImage/NamedRow::Deconstruct(System.Int32&,System.Int32&,System.UInt32&,System.UInt32&) +System.Void Nethermind.LeanCIL.MetadataImage/TypeNameRow::.ctor(Nethermind.LeanCIL.MetadataImage/TypeNameRow) +System.Void Nethermind.LeanCIL.MetadataImage/TypeNameRow::.ctor(Nethermind.LeanCIL.TypeLevel,System.UInt32) +System.Void Nethermind.LeanCIL.MetadataImage::RecordSigningBytes(System.Int32) +System.Void Nethermind.LeanCIL.MetadataImage::set_SigningBytes(System.Int64) +System.Void Nethermind.LeanCIL.MetadataImage::set_SigningReads(System.Int64) +System.Void Nethermind.LeanCIL.MetadataSlices::.ctor(Nethermind.LeanCIL.PinnedAssemblies) +System.Void Nethermind.LeanCIL.MetadataValidation/AggregateInfo::.ctor(Nethermind.LeanCIL.MetadataValidation/AggregateInfo) +System.Void Nethermind.LeanCIL.MetadataValidation/InstanceField::.ctor(Nethermind.LeanCIL.MetadataValidation/InstanceField) +System.Void Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::.ctor(Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata) +System.Void Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::.ctor(System.Boolean) +System.Void Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::Deconstruct(System.Boolean&) +System.Void Nethermind.LeanCIL.MethodBodyBytes/Body::.ctor(Nethermind.LeanCIL.MethodBodyBytes/Body) +System.Void Nethermind.LeanCIL.MethodBodyBytes/Body::.ctor(System.Byte[],System.Int32) +System.Void Nethermind.LeanCIL.MethodRef::.ctor(Nethermind.LeanCIL.MethodRef) +System.Void Nethermind.LeanCIL.MethodSpecialization/Origin::.ctor(Nethermind.LeanCIL.MethodSpecialization/Origin) +System.Void Nethermind.LeanCIL.MethodSpecialization/Origin::.ctor(System.String,System.String[]) +System.Void Nethermind.LeanCIL.NativeOperands/Certificate::.ctor(Nethermind.LeanCIL.NativeOperands/Certificate) +System.Void Nethermind.LeanCIL.Operation::.ctor() +System.Void Nethermind.LeanCIL.Operation::.ctor(Nethermind.LeanCIL.Operation) +System.Void Nethermind.LeanCIL.ParameterHome::.ctor(Nethermind.LeanCIL.ParameterHome) +System.Void Nethermind.LeanCIL.ParameterHome::.ctor(System.Int32,Nethermind.LeanCIL.LocalKind) +System.Void Nethermind.LeanCIL.PinnedAssemblies/IdentityRow::.ctor(Nethermind.LeanCIL.MetadataImage,System.UInt32) +System.Void Nethermind.LeanCIL.PinnedAssemblies/Source::.ctor(Nethermind.LeanCIL.PinnedAssemblies/Source) +System.Void Nethermind.LeanCIL.PinnedAssemblies/Source::.ctor(System.String,System.String) +System.Void Nethermind.LeanCIL.ProfileInitializationAssumptions::.ctor(Nethermind.LeanCIL.ProfileInitializationAssumptions) +System.Void Nethermind.LeanCIL.ProfileInitializationAssumptions::.ctor(System.String,Nethermind.LeanCIL.InitializationAssumption[]) +System.Void Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot::.ctor(Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot) +System.Void Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot::.ctor(System.Byte[],System.String) +System.Void Nethermind.LeanCIL.Program/ExtractionRequest::.ctor(Nethermind.LeanCIL.Program/ExtractionRequest) +System.Void Nethermind.LeanCIL.Program/ExtractionRequest::.ctor(System.String,System.String,System.String,System.String,System.String,System.String[],System.String[]) +System.Void Nethermind.LeanCIL.ProofOutline/Access::.ctor(Nethermind.LeanCIL.ProofOutline/Access) +System.Void Nethermind.LeanCIL.ProofOutline/Access::.ctor(Nethermind.LeanCIL.ProofOutline/Reached,Nethermind.LeanCIL.ProofOutline/ViewField,Nethermind.LeanCIL.ProofOutline/AccessKind,System.Boolean,System.Int32,System.String,Nethermind.LeanCIL.ProofOutline/Reached,System.String,Nethermind.LeanCIL.ProofOutline/HeldArray,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/AddedReference::.ctor(Nethermind.LeanCIL.ProofOutline/AddedReference) +System.Void Nethermind.LeanCIL.ProofOutline/AddedReference::.ctor(System.String,System.String,System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/Allocation::.ctor(Nethermind.LeanCIL.ProofOutline/Allocation) +System.Void Nethermind.LeanCIL.ProofOutline/Allocation::.ctor(System.String,System.String,System.String,System.Int32,System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/ArrayAccess::.ctor(Nethermind.LeanCIL.ProofOutline/ArrayAccess) +System.Void Nethermind.LeanCIL.ProofOutline/ArrayAccess::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter,Nethermind.LeanCIL.ProofOutline/Parameter,Nethermind.LeanCIL.ProofOutline/ElementHelper,Nethermind.LeanCIL.Arrays/NumericAccess) +System.Void Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::.ctor(Nethermind.LeanCIL.ProofOutline/ArrayLengthValue) +System.Void Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void Nethermind.LeanCIL.ProofOutline/ArrayUse::.ctor(Nethermind.LeanCIL.ProofOutline/ArrayUse) +System.Void Nethermind.LeanCIL.ProofOutline/ArrayUse::.ctor(Nethermind.LeanCIL.ProofOutline/HeldArray,System.Int32,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/Body::.ctor(Nethermind.LeanCIL.ProofOutline/Method) +System.Void Nethermind.LeanCIL.ProofOutline/Callee::.ctor(Nethermind.LeanCIL.ProofOutline/Callee) +System.Void Nethermind.LeanCIL.ProofOutline/Callee::.ctor(System.Int32,System.String,Nethermind.LeanCIL.ProofOutline/Specification,Nethermind.LeanCIL.ProofOutline/ObjectWalk,Nethermind.LeanCIL.ProofOutline/ObjectPlan,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/Component::.ctor(Nethermind.LeanCIL.ProofOutline/Component) +System.Void Nethermind.LeanCIL.ProofOutline/Component::.ctor(System.String,Nethermind.LeanCIL.ProofOutline/Part,System.Int32,System.Int32,System.Int32,Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void Nethermind.LeanCIL.ProofOutline/Element::.ctor(Nethermind.LeanCIL.ProofOutline/Element) +System.Void Nethermind.LeanCIL.ProofOutline/Element::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/Element::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.ProofOutline/ElementAccess::.ctor(Nethermind.LeanCIL.ProofOutline/ElementAccess) +System.Void Nethermind.LeanCIL.ProofOutline/ElementAccess::.ctor(System.String,Nethermind.LeanCIL.ProofOutline/ElementKind,System.Int32,System.String,System.String,Nethermind.LeanCIL.ProofOutline/Reached,System.String,System.String,System.Boolean,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/ElementHelper::.ctor(Nethermind.LeanCIL.ProofOutline/ElementHelper) +System.Void Nethermind.LeanCIL.ProofOutline/HeldArray::.ctor(Nethermind.LeanCIL.ProofOutline/HeldArray) +System.Void Nethermind.LeanCIL.ProofOutline/HeldArray::.ctor(System.String,System.String,System.String,System.String,System.String,Nethermind.LeanCIL.ProofOutline/Reached,Nethermind.LeanCIL.ProofOutline/ViewField,Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void Nethermind.LeanCIL.ProofOutline/LocalAccess::.ctor(Nethermind.LeanCIL.ProofOutline/LocalAccess) +System.Void Nethermind.LeanCIL.ProofOutline/LocalAccess::.ctor(System.Int32,System.String,System.Int32,System.Int32,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/LocalAddressValue::.ctor(Nethermind.LeanCIL.ProofOutline/LocalAddressValue) +System.Void Nethermind.LeanCIL.ProofOutline/LocalAddressValue::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/LocalAddressValue::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.ProofOutline/LocalValue::.ctor(Nethermind.LeanCIL.ProofOutline/LocalValue) +System.Void Nethermind.LeanCIL.ProofOutline/LocalValue::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/LocalValue::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectFrame::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectFrame) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectFrame::.ctor(System.String,System.String,System.Int32,System.String[]) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectFrameEntry::.ctor() +System.Void Nethermind.LeanCIL.ProofOutline/ObjectFrameEntry::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectFrameEntry) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectTerm::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectTerm) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectTerm::.ctor(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/ArrayArgument::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectWalk/ArrayArgument) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/ArrayArgument::.ctor(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Instance::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectWalk/Instance) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Instance::.ctor(Nethermind.LeanCIL.ProofOutline/Reached) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Item::.ctor() +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Item::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectWalk/Item) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Null::.ctor() +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Null::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectWalk/Null) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::.ctor(System.String,System.String) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values::.ctor(Nethermind.LeanCIL.ProofOutline/HeldArray) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::.ctor(Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::.ctor(System.String,System.Int32,System.Int64,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk::set_Fresh(Nethermind.LeanCIL.ProofOutline/Allocation) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk::set_Returned(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/ObjectWalk::set_Stopped(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/Outline::.ctor(Nethermind.LeanCIL.ProofOutline/Outline) +System.Void Nethermind.LeanCIL.ProofOutline/Outline::.ctor(System.String,System.String,System.Int32,System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/Parameter::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void Nethermind.LeanCIL.ProofOutline/Reached::.ctor(Nethermind.LeanCIL.ProofOutline/Reached) +System.Void Nethermind.LeanCIL.ProofOutline/Reached::.ctor(System.String,Nethermind.LeanCIL.ProofOutline/ViewField[],System.String,System.String) +System.Void Nethermind.LeanCIL.ProofOutline/ReceiverField::.ctor(Nethermind.LeanCIL.ProofOutline/ReceiverField) +System.Void Nethermind.LeanCIL.ProofOutline/ReceiverField::.ctor(System.String,System.String,System.String,System.Int32,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/ReferenceArgument::.ctor(Nethermind.LeanCIL.ProofOutline/ReferenceArgument) +System.Void Nethermind.LeanCIL.ProofOutline/ReferenceArgument::.ctor(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/Result::.ctor(Nethermind.LeanCIL.ProofOutline/Result) +System.Void Nethermind.LeanCIL.ProofOutline/Result::.ctor(System.String,System.Int32,System.Boolean,System.Boolean,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/Returning::.ctor(Nethermind.LeanCIL.ProofOutline/Returning) +System.Void Nethermind.LeanCIL.ProofOutline/Returning::.ctor(System.String,System.Boolean,System.String,System.Boolean,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/ScalarCheck::.ctor(Nethermind.LeanCIL.ProofOutline/ScalarCheck) +System.Void Nethermind.LeanCIL.ProofOutline/ScalarCheck::.ctor(System.String,System.String,System.String,System.Int32,Nethermind.LeanCIL.ProofOutline/ScalarStep) +System.Void Nethermind.LeanCIL.ProofOutline/ScalarReturn::.ctor() +System.Void Nethermind.LeanCIL.ProofOutline/ScalarReturn::.ctor(Nethermind.LeanCIL.ProofOutline/ScalarReturn) +System.Void Nethermind.LeanCIL.ProofOutline/ScalarSplit::.ctor(Nethermind.LeanCIL.ProofOutline/ScalarSplit) +System.Void Nethermind.LeanCIL.ProofOutline/ScalarSplit::.ctor(System.String,System.Int32,System.Int32,Nethermind.LeanCIL.ProofOutline/ScalarStep,System.Int32,Nethermind.LeanCIL.ProofOutline/ScalarStep) +System.Void Nethermind.LeanCIL.ProofOutline/ScalarStep::.ctor() +System.Void Nethermind.LeanCIL.ProofOutline/ScalarStep::.ctor(Nethermind.LeanCIL.ProofOutline/ScalarStep) +System.Void Nethermind.LeanCIL.ProofOutline/Sliced::.ctor(Nethermind.LeanCIL.ProofOutline/Sliced) +System.Void Nethermind.LeanCIL.ProofOutline/Sliced::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/Sliced::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.ProofOutline/Slot::.ctor(Nethermind.LeanCIL.ProofOutline/Slot) +System.Void Nethermind.LeanCIL.ProofOutline/Slot::.ctor(System.Int32,Nethermind.LeanCIL.LocalKind,System.String,System.Int32,Nethermind.LeanCIL.ProofOutline/Component[],System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/SpanArgument::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void Nethermind.LeanCIL.ProofOutline/SpanArgument::.ctor(Nethermind.LeanCIL.ProofOutline/SpanArgument) +System.Void Nethermind.LeanCIL.ProofOutline/SpanLengthValue::.ctor(Nethermind.LeanCIL.ProofOutline/SpanLengthValue) +System.Void Nethermind.LeanCIL.ProofOutline/SpanLengthValue::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.ProofOutline/SpanLengthValue::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.ProofOutline/SpanTerm::.ctor(Nethermind.LeanCIL.ProofOutline/SpanTerm) +System.Void Nethermind.LeanCIL.ProofOutline/SpanTerm::.ctor(System.String,System.String,System.String,System.Boolean,System.String,System.String,System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/SpannedArray::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void Nethermind.LeanCIL.ProofOutline/SpannedArray::.ctor(Nethermind.LeanCIL.ProofOutline/SpannedArray) +System.Void Nethermind.LeanCIL.ProofOutline/Specification::set_NonNull(Nethermind.LeanCIL.ProofOutline/Parameter[]) +System.Void Nethermind.LeanCIL.ProofOutline/Specification::set_Plan(Nethermind.LeanCIL.ProofOutline/ObjectPlan) +System.Void Nethermind.LeanCIL.ProofOutline/StructScalar::set_Initial(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/StructScalar::set_Owned(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/StructScalar::set_Read(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/StructScalar::set_Stored(System.Boolean) +System.Void Nethermind.LeanCIL.ProofOutline/StructWalk::set_Returned(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/StructWalk::set_Stopped(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/Stub::.ctor(Nethermind.LeanCIL.ProofOutline/Stub) +System.Void Nethermind.LeanCIL.ProofOutline/StubCall::.ctor(Nethermind.LeanCIL.ProofOutline/Stub,System.String,System.Int32,System.Int32,System.String) +System.Void Nethermind.LeanCIL.ProofOutline/StubCall::.ctor(Nethermind.LeanCIL.ProofOutline/StubCall) +System.Void Nethermind.LeanCIL.ProofOutline/Thrown::.ctor(Nethermind.LeanCIL.ProofOutline/Thrown) +System.Void Nethermind.LeanCIL.ProofOutline/Thrown::.ctor(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/Unknown::.ctor(Nethermind.LeanCIL.ProofOutline/Unknown) +System.Void Nethermind.LeanCIL.ProofOutline/Unknown::.ctor(System.String) +System.Void Nethermind.LeanCIL.ProofOutline/Value::.ctor() +System.Void Nethermind.LeanCIL.ProofOutline/Value::.ctor(Nethermind.LeanCIL.ProofOutline/Value) +System.Void Nethermind.LeanCIL.ProofOutline/WordArrayArgument::.ctor(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void Nethermind.LeanCIL.ProofOutline/WordArrayArgument::.ctor(Nethermind.LeanCIL.ProofOutline/WordArrayArgument) +System.Void Nethermind.LeanCIL.RuntimeAsset::.ctor(Nethermind.LeanCIL.RuntimeAsset) +System.Void Nethermind.LeanCIL.RuntimeAsset::.ctor(System.String,System.String,System.String,System.String,System.String,System.Boolean,System.String,System.String,System.String[]) +System.Void Nethermind.LeanCIL.StaticDataBlock::.ctor(Nethermind.LeanCIL.StaticDataBlock) +System.Void Nethermind.LeanCIL.StaticDataBlock::.ctor(System.Int32) +System.Void Nethermind.LeanCIL.StaticDataBlock::Deconstruct(System.Int32&) +System.Void Nethermind.LeanCIL.StaticIdentityRecord::.ctor(Nethermind.LeanCIL.StaticIdentityRecord) +System.Void Nethermind.LeanCIL.StaticIdentityRecord::.ctor(System.String,System.String,System.String) +System.Void Nethermind.LeanCIL.StaticSite::.ctor(Nethermind.LeanCIL.StaticSite) +System.Void Nethermind.LeanCIL.StaticSiteDetail::.ctor() +System.Void Nethermind.LeanCIL.StaticSiteDetail::.ctor(Nethermind.LeanCIL.StaticSiteDetail) +System.Void Nethermind.LeanCIL.StaticValues/FirstMethods::.ctor() +System.Void Nethermind.LeanCIL.StaticValues/Occurrence::.ctor(Nethermind.LeanCIL.StaticValues/Occurrence) +System.Void Nethermind.LeanCIL.StaticValues/Self::.ctor(Nethermind.LeanCIL.StaticValues/Self) +System.Void Nethermind.LeanCIL.StaticValues/Self::.ctor(System.Byte[]) +System.Void Nethermind.LeanCIL.StaticValues/Slot::.ctor() +System.Void Nethermind.LeanCIL.StaticValues/Slot::.ctor(Nethermind.LeanCIL.StaticValues/Slot) +System.Void Nethermind.LeanCIL.StaticValues/Snapshot::.ctor(Nethermind.LeanCIL.StaticValues/Snapshot) +System.Void Nethermind.LeanCIL.StaticValues/Snapshot::.ctor(System.Byte[]) +System.Void Nethermind.LeanCIL.StaticValues/Value::.ctor(Nethermind.LeanCIL.StaticValues/Value) +System.Void Nethermind.LeanCIL.StaticValues/Word::.ctor(Nethermind.LeanCIL.StaticValues/Word) +System.Void Nethermind.LeanCIL.StaticValues/Word::.ctor(System.Int32,System.UInt64) +System.Void Nethermind.LeanCIL.StaticValues/Word::Deconstruct(System.Int32&,System.UInt64&) +System.Void Nethermind.LeanCIL.StructuralSignatures/Primitive::.ctor(Nethermind.LeanCIL.StructuralSignatures/Primitive) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_EdgeVisits(System.Int64) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_HeapBytes(System.Int64) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_NodeVisits(System.Int64) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_RawRows(System.Int64) +System.Void Nethermind.LeanCIL.StructuralSignatures::set_TrieProbes(System.Int64) +System.Void Nethermind.LeanCIL.Symbols/PdbFile::.ctor(Nethermind.LeanCIL.Symbols/PdbFile) +System.Void Nethermind.LeanCIL.Symbols/PdbFile::.ctor(System.Byte[],System.String) +System.Void Nethermind.LeanCIL.TemplateEmitter/Method::.ctor(Nethermind.LeanCIL.TemplateEmitter/Method) +System.Void Nethermind.LeanCIL.TemplateEmitter/Method::.ctor(System.String,Nethermind.LeanCIL.FrameMetadata,Nethermind.LeanCIL.Feature[],System.Int32[],System.Int32[]) +System.Void Nethermind.LeanCIL.TemplateEmitter/Options::.ctor(Nethermind.LeanCIL.TemplateEmitter/Options) +System.Void Nethermind.LeanCIL.TemplateEmitter/Options::.ctor(System.String,System.String[],System.String,System.Boolean,System.String,System.String,System.Boolean) +System.Void Nethermind.LeanCIL.TemplateEmitter/Row::.ctor(Nethermind.LeanCIL.TemplateEmitter/Row) +System.Void Nethermind.LeanCIL.TemplateEmitter/Row::.ctor(Nethermind.LeanCIL.TemplateEmitter/Variant,Nethermind.LeanCIL.FrameMetadata,System.String) +System.Void Nethermind.LeanCIL.TemplateEmitter/Template::.ctor(Nethermind.LeanCIL.TemplateEmitter/Template) +System.Void Nethermind.LeanCIL.TemplateEmitter/Template::.ctor(System.String,Nethermind.LeanCIL.FrameMetadata,Nethermind.LeanCIL.Feature[],System.String[],Nethermind.LeanCIL.TemplateEmitter/Row[],System.Int32[]) +System.Void Nethermind.LeanCIL.TemplateEmitter/Variant::.ctor(Nethermind.LeanCIL.TemplateEmitter/Variant) +System.Void Nethermind.LeanCIL.TemplateEmitter/Variant::.ctor(System.String,System.String,Nethermind.LeanCIL.FeatureProfile,Nethermind.LeanCIL.TemplateEmitter/Method[]) +System.Void Nethermind.LeanCIL.Throws/Analysis::.ctor(Nethermind.LeanCIL.Throws/Analysis) +System.Void Nethermind.LeanCIL.TypeLevel::.ctor(Nethermind.LeanCIL.MetadataName,Nethermind.LeanCIL.MetadataName) +System.Void Nethermind.LeanCIL.TypeLevel::.ctor(Nethermind.LeanCIL.TypeLevel) +System.Void Nethermind.LeanCIL.TypeTests/Analysis::.ctor(Nethermind.LeanCIL.TypeTests/Analysis) +System.Void Nethermind.LeanCIL.TypeTests/Node::.ctor(Nethermind.LeanCIL.TypeTests/Node) +System.Void Nethermind.LeanCIL.TypeTests/Node::.ctor(System.String,System.String,System.String,Nethermind.LeanCIL.TypeTests/Node,Nethermind.LeanCIL.TypeTests/Node) +System.Void Nethermind.LeanCIL.UserString::.ctor(Nethermind.LeanCIL.UserString) +System.Void Nethermind.LeanCIL.UserString::.ctor(System.Char[]) +System.Void Program/AuditConstantRequest::.ctor(Program/AuditConstantRequest) +System.Void Program/AuditNamespaces::.ctor(Program/AuditNamespaces) +System.Void Program/AuditNamespaces::.ctor(System.String[],System.String[]) +System.Void Program/AuditRequest::.ctor(Program/AuditRequest) +System.Void Program/AuditedClosure::.ctor(Program/AuditedClosure) +System.Void Program/AuditedClosure::.ctor(System.String,System.String,System.String,System.String) +System.Void Program/AuditedInterfaces::.ctor(Program/AuditedInterfaces) +System.Void Program/AuditedInterfaces::.ctor(System.String) +System.Void Program/AuditedNamespaces::.ctor(Program/AuditedNamespaces) +System.Void Program/BatchManifest::.ctor(Program/BatchManifest) +System.Void Program/BatchManifest::.ctor(System.Int32,System.String,Program/BatchRequest[]) +System.Void Program/BatchRun::.ctor(Program/BatchRun) +System.Void Program/BindingCatalog::.ctor(Program/BindingCatalog) +System.Void Program/BindingFactResult::.ctor(Program/BindingFact,Program/BindingStatus,System.String,System.String) +System.Void Program/BindingFactResult::.ctor(Program/BindingFactResult) +System.Void Program/BindingLibraryPreparation::.ctor(Program/BindingLibraryPreparation) +System.Void Program/BindingPublication::.ctor(Program/BindingPublication) +System.Void Program/BindingPublication::.ctor(System.String,Program/BindingImagePin,System.String,System.String,System.String,Program/BindingData,Program/BindingData) +System.Void Program/BindingRequest::.ctor(Program/BindingRequest) +System.Void Program/BindingRequest::.ctor(System.String,Program/AuditRequest,Program/BindingSubject,System.String) +System.Void Program/BindingSection::.ctor(Program/BindingMode,Program/DigestFile,Program/StatementSources,Program/ImageResult[],Program/BindingFactResult[]) +System.Void Program/BindingSection::.ctor(Program/BindingSection) +System.Void Program/BindingSubject/Fact::.ctor(Program/BindingFact,System.Boolean) +System.Void Program/BindingSubject/Fact::.ctor(Program/BindingSubject/Fact) +System.Void Program/BindingSubject/Fact::Deconstruct(Program/BindingFact&,System.Boolean&) +System.Void Program/BindingSubject::.ctor() +System.Void Program/BindingSubject::.ctor(Program/BindingSubject) +System.Void Program/BuildDiagnostic::.ctor(Program/BuildDiagnostic) +System.Void Program/BuildDiagnostic::.ctor(System.String,System.Int32,System.Int32,System.String,System.String) +System.Void Program/CatalogAssign::.ctor(Program/CatalogAssign) +System.Void Program/CatalogFile::.ctor(Program/CatalogFile) +System.Void Program/CatalogFile::.ctor(System.Int32,System.String,Program/CatalogFamily[]) +System.Void Program/CatalogGate::.ctor(Program/CatalogGate) +System.Void Program/CatalogGate::.ctor(System.String,System.String,System.String[],System.String,System.String,System.String[]) +System.Void Program/CatalogGatePattern::.ctor(Program/CatalogGatePattern) +System.Void Program/CatalogGatePattern::.ctor(System.String,System.String,System.String[],System.String,System.String) +System.Void Program/CertifiedFingerprintManifest::.ctor(Program/CertifiedFingerprintManifest) +System.Void Program/CertifiedFingerprintModule::.ctor(Program/CertifiedFingerprintModule) +System.Void Program/CertifiedReleaseAsset::.ctor(Program/CertifiedReleaseAsset) +System.Void Program/CertifiedReleaseAsset::.ctor(System.Int32,System.String,System.String,System.String,System.String,System.Int64,System.String,System.String) +System.Void Program/CheckedContractExport::.ctor(Program/CheckedContractExport) +System.Void Program/CheckedContractExport::.ctor(Program/PreparedContractExport,Program/ExportClosure,System.String,System.String,System.String,System.String) +System.Void Program/ClosureEntry::.ctor(Program/ClosureEntry) +System.Void Program/ConsumerDependency::.ctor(Program/ConsumerDependency) +System.Void Program/ConsumerDependency::.ctor(System.String,System.String,System.String,System.String,System.String) +System.Void Program/ConsumerExportAudit::.ctor(Program/ConsumerExportAudit) +System.Void Program/ConsumerOutcome::.ctor() +System.Void Program/ConsumerRequest::.ctor(Program/ConsumerRequest) +System.Void Program/ConsumerRequest::.ctor(System.String,System.String,System.String,System.String,System.String,System.Boolean,Program/ConsumerExportAudit,Program/StatementRequest) +System.Void Program/ConsumerSession::.ctor(Program/ConsumerSession) +System.Void Program/ConsumerSupportManifest::.ctor(Program/ConsumerSupportManifest) +System.Void Program/ConsumerSupportModule::.ctor(Program/ConsumerSupportModule) +System.Void Program/DeepCheckRun::.ctor(DeepCheckInputs) +System.Void Program/DeepCheckRun::set_Attempt(Program/DeepCheckAttempt) +System.Void Program/DeepClosureWork::.ctor(System.Int32,System.Int32,System.Int32,System.Int64) +System.Void Program/DeepClosureWork::Deconstruct(System.Int32&,System.Int32&,System.Int32&,System.Int64&) +System.Void Program/DeepSessionContribution::.ctor(Program/DeepSessionContribution) +System.Void Program/DeploymentDependency::.ctor(Program/DeploymentDependency) +System.Void Program/DeploymentDependency::.ctor(System.String,System.String,System.String,System.String[],System.Boolean,System.String,System.String) +System.Void Program/DigestFile::.ctor(Program/DigestFile) +System.Void Program/DigestFile::.ctor(System.String,System.String) +System.Void Program/EditorProof::.ctor(Program/EditorProof) +System.Void Program/EditorProof::.ctor(System.String,System.String) +System.Void Program/EditorSelection::.ctor(Program/EditorSelection) +System.Void Program/EditorSelection::.ctor(System.String,System.String,System.String,System.String,System.String[],Program/EditorProof[]) +System.Void Program/EnvironmentEntry::.ctor(Program/EnvironmentEntry) +System.Void Program/EnvironmentEntry::.ctor(System.String,System.String,System.Boolean) +System.Void Program/ExportClosure::.ctor(Program/ExportClosure) +System.Void Program/ExportClosure::.ctor(System.String,System.String,System.String,System.String) +System.Void Program/ExtractedMethod::.ctor(Program/ExtractedMethod) +System.Void Program/ExtractedMethod::.ctor(System.String,System.String,System.String) +System.Void Program/ExtractionComponents::.ctor(Program/ExtractionComponents) +System.Void Program/FingerprintInput::.ctor(Program/FingerprintInput) +System.Void Program/FullImport::.ctor(Program/FullImport) +System.Void Program/FullImport::.ctor(Program/FullSelection,Nethermind.LeanCIL.ContractExport,Nethermind.LeanCIL.ContractCertification,System.Int32) +System.Void Program/FullImportResult::.ctor(Program/FullImportResult) +System.Void Program/FullImportResult::.ctor(System.String,System.String,Program/PublishedImport[],Program/DeploymentDependency[]) +System.Void Program/FullLibrary::.ctor(Program/FullLibrary) +System.Void Program/FullLibrary::.ctor(System.Int32,System.String,System.String,System.String,System.String,System.String,System.String,System.String,Nethermind.LeanCIL.StaticIdentityRecord[],System.String,System.Int32[]) +System.Void Program/GateBuild::.ctor(Program/GateBuild) +System.Void Program/GateBuildPlan::.ctor(Program/GateBuildPlan) +System.Void Program/GateDefaults::.ctor(Program/GateDefaults) +System.Void Program/GateDefaults::.ctor(System.String,Program/GateBuild,System.String[],System.String[]) +System.Void Program/GateGenerated::.ctor(Program/GateGenerated) +System.Void Program/GateManifest::.ctor(Program/GateManifest) +System.Void Program/GatePlan::.ctor(Program/GatePlan) +System.Void Program/GatePlan::.ctor(System.String,System.String,Program/GatePlanSelection,System.String[],Program/GatePlanGroup[]) +System.Void Program/GatePlanSelection::.ctor(Program/GatePlanSelection) +System.Void Program/GatePlanSelection::.ctor(System.String,System.String,System.String[],System.String[],System.Int32) +System.Void Program/GateProject::.ctor(Program/GateProject) +System.Void Program/GateRefutation::.ctor(Program/GateRefutation) +System.Void Program/GateRefutationResult::.ctor(Program/GateRefutationResult) +System.Void Program/GateRefutationResult::.ctor(System.String,System.String,System.String,System.String,System.String,System.String[]) +System.Void Program/GateResults::.ctor(Program/GateResults) +System.Void Program/GateResults::.ctor(System.String,System.String,Program/GateResult[]) +System.Void Program/GateRow::.ctor() +System.Void Program/GateRow::Fail(System.String,System.String) +System.Void Program/GateSelectionRules::.ctor(Program/GateSelectionRules) +System.Void Program/GateSelectionRules::.ctor(System.String[],System.String[]) +System.Void Program/GateStatement::.ctor(Program/GateStatement) +System.Void Program/GateStatement::.ctor(System.String,System.String,System.String) +System.Void Program/GateTemplate::.ctor(Program/GateTemplate) +System.Void Program/GateTemplateUse::.ctor(Program/GateTemplateUse) +System.Void Program/GeneratedExtraction::.ctor(Program/GeneratedExtraction) +System.Void Program/GeneratedExtraction::.ctor(System.String,System.String,System.String,System.String,System.String[],System.String[]) +System.Void Program/ImageResult::.ctor(Program/ImageResult) +System.Void Program/ImageResult::.ctor(System.String,System.String,Program/ImageRole,Program/BindingStatus,System.String,System.String) +System.Void Program/JobCount::.ctor() +System.Void Program/LibraryAudit::.ctor(Program/LibraryAudit) +System.Void Program/LibraryAudit::.ctor(System.String[],System.String[],Program/AuditedNamespaces) +System.Void Program/LoadedGates::.ctor(Program/LoadedGates) +System.Void Program/PassReport::.ctor() +System.Void Program/PreparedContractExport::.ctor(Nethermind.LeanCIL.ContractExportRequest,System.String,System.String,System.String,System.String,System.Int32,System.Boolean,System.String,System.Boolean,Nethermind.LeanCIL.ContractDependency[],Nethermind.LeanCIL.InterfaceModule[],Nethermind.LeanCIL.StaticIdentityRecord[],System.String,System.String[],Nethermind.LeanCIL.ContractMethodSignature,Nethermind.LeanCIL.ContractLayout[],System.String,System.Int32[],Nethermind.LeanCIL.InitializationAssumption[]) +System.Void Program/PreparedContractExport::.ctor(Program/PreparedContractExport) +System.Void Program/ProofGateEntry::.ctor(Program/ProofGateEntry) +System.Void Program/ProofGateEntry::.ctor(System.String,System.String,System.String[],System.String,System.String,System.String,Program/GateBuild,Program/GateVariant[],System.String[],System.String[]) +System.Void Program/ProofMessage::.ctor(Program/ProofMessage) +System.Void Program/ProofMessage::.ctor(System.String,System.Int32,System.Int32,System.String) +System.Void Program/ReplayRejection::.ctor(Program/ReplayRejection) +System.Void Program/ReplayRejection::.ctor(System.String,Program/ReplayRejectionKind) +System.Void Program/StandaloneCachePromotion::.ctor(Program/StandaloneCachePromotion) +System.Void Program/StatementDefinition::.ctor(Program/StatementDefinition) +System.Void Program/StatementDefinition::.ctor(System.String,System.String) +System.Void Program/StatementRequest::.ctor(Program/StatementRequest) +System.Void Program/StatementRequest::.ctor(System.Boolean,System.String,System.Boolean) +System.Void Program/StatementSources::.ctor(Program/StatementSources) +System.Void Program/StatementSources::.ctor(System.String,System.String,System.String) +System.Void Program/StatementView::.ctor(Program/StatementView) +System.Void Program/StatementView::.ctor(System.String,Program/StatementSubject[],Program/StatementDefinition[]) +System.Void Program/TrustedSupport::.ctor(Program/ConsumerSupportManifest,System.String) +System.Void Program/TrustedSupport::.ctor(Program/TrustedSupport) +System.Void Program/VerificationSettings::.ctor(Program/VerificationSettings) +System.Void Program/VerificationSettings::.ctor(System.Int32,System.String,System.String,System.String) +System.Void Program/VerifiedRefutation::.ctor(Program/VerifiedRefutation) +System.Void Program/VerifiedRefutation::.ctor(System.String,Program/ExtractedMethod,System.Int32,System.Int32) +System.Void Program/WatchResult::.ctor(Program/WatchResult) +System.Void Program/WatchSettings::.ctor(Program/WatchSettings) +System.Void Program/WatchSettings::.ctor(System.String,System.String[],System.String,System.Boolean,System.Boolean,System.Boolean) +System.Void Program/WatchState::.ctor(Program/WatchState) +System.Void Program/WorkspaceSettings::.ctor(Program/WorkspaceSettings) +System.Void Program/WorkspaceSettings::.ctor(System.String,System.String,System.String,System.String,System.String,System.String,System.String[],System.String,System.Boolean) +System.Void VerifyReports/ManifestMethod::.ctor(System.String,System.String[],VerifyReports/SourcePoint[]) +System.Void VerifyReports/ManifestMethod::.ctor(VerifyReports/ManifestMethod) +System.Void VerifyReports/SourcePoint::.ctor(System.Int32,System.String,System.Int32,System.Int32) +System.Void VerifyReports/SourcePoint::.ctor(VerifyReports/SourcePoint) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) DeepCheckInputs/Pin::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) DeepCheckInputs/Pin::set_Image(DeepCheckInputs/Image) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) DeepCheckInputs/Pin::set_Platform(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) DeepCheckInputs/Pin::set_Reference(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailure::set_digests(FailureReports/BindingFailureDigest) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailure::set_facts(FailureReports/BindingFailureFact[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailure::set_images(FailureReports/BindingFailureImage[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureDigest::set_path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureDigest::set_sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureFact::set_id(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureFact::set_reason(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureFact::set_status(FailureReports/BindingFailureStatus) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureImage::set_identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureImage::set_reason(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureImage::set_sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/BindingFailureImage::set_status(FailureReports/BindingFailureStatus) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Diagnostic::set_file(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Diagnostic::set_kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Diagnostic::set_message(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Diagnostic::set_module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Diagnostic::set_resource(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Method::set_assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Method::set_assemblySha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Method::set_entry(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_binding(FailureReports/BindingFailure) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_diagnostics(FailureReports/Diagnostic[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_file(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_message(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_method(FailureReports/Method) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_rerun(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_resource(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_stage(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) FailureReports/Report::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanDiagnostics/Diagnostic::set_Location(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanDiagnostics/Diagnostic::set_Module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanDiagnostics/Report::set_Shortened(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanDiagnostics/Report::set_Text(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanOutput/InternalError::set_Location(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanOutput/InternalError::set_Message(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) LeanOutput/InternalError::set_Module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Compound(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Kind(Nethermind.LeanCIL.NativeOperands/Kind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Lean(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Signed(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/Element::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/NumericAccess::set_Shape(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/NumericAccess::set_Signed(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/NumericAccess::set_Store(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Arrays/NumericAccess::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName/Columns::set_Culture(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName/Columns::set_Name(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName/KeyColumns::set_Valid(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName::set_Culture(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyName::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyVersion::set_Build(System.UInt16) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyVersion::set_Major(System.UInt16) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyVersion::set_Minor(System.UInt16) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.AssemblyVersion::set_Revision(System.UInt16) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_DecodedMethods(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_Method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_Reason(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_Status(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Outcome::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Site::set_Instruction(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Site::set_Message(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Site::set_Method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Blockers/Site::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Access::set_Method(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Access::set_Operation(Nethermind.LeanCIL.ClassViews/FieldOperation) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Access::set_Pc(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Element(Nethermind.LeanCIL.Arrays/Element) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Kind(Nethermind.LeanCIL.ClassViews/FieldKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Loaders(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Offset(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/Field::set_Storers(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Element(Nethermind.LeanCIL.Arrays/Element) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Kind(Nethermind.LeanCIL.ClassViews/FieldKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Offset(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Owner(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Signed(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/FieldOperation::set_Store(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/View::set_Fields(Nethermind.LeanCIL.ClassViews/Field[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/View::set_FullName(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/View::set_Path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ClassViews/View::set_Type(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ComputedLayout::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ComputedLayout::set_Size(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ConstantReturns/Closed::set_Architecture(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractBundleManifest::set_FrameworkKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractBundleManifest::set_LeanCilRevision(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractBundleManifest::set_Schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractCertification::set_ClosureDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractCertification::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractCertification::set_ImplementationSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractCertification::set_InterfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractCertification::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractCertification::set_Statement(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractCertification::set_StatementDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractCertification::set_StaticIdentities(Nethermind.LeanCIL.StaticIdentityRecord[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractCertification::set_Theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractDependency::set_AssemblyIdentity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractDependency::set_ImplementationSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractDependency::set_InterfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_ClosureDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_ContractConstant(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_DefinitionSignature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_HasThis(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_InitializationAssumptions(Nethermind.LeanCIL.ProfileInitializationAssumptions[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_Kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_Layouts(Nethermind.LeanCIL.ContractLayout[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_ParameterCount(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_Profiles(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_ReturnsValue(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_TypedSignature(Nethermind.LeanCIL.ContractMethodSignature) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExport::set_Visibility(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportProfile::set_Artifact(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportProfile::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportRequest::set_Artifact(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportRequest::set_Contract(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportRequest::set_Method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportRequest::set_Profiles(Nethermind.LeanCIL.ContractExportProfile[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportRequest::set_Program(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportRequest::set_ReferenceArtifact(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportRequest::set_ReferenceProfile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportRequest::set_Theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractExportRequest::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_Auto(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_BaseType(Nethermind.LeanCIL.ContractType) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_DeclaredSize(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_Explicit(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_Fields(Nethermind.LeanCIL.ContractLayoutField[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_IsValueType(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_Packing(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayout::set_Scope(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayoutField::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayoutField::set_Storage(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractLayoutField::set_Type(Nethermind.LeanCIL.ContractType) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractMethodMetadata/Selection::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractMethodSignature::set_Arguments(Nethermind.LeanCIL.ContractType[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractMethodSignature::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractMethodSignature::set_Owner(Nethermind.LeanCIL.ContractType) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractMethodSignature::set_ParameterAttributes(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractMethodSignature::set_Parameters(Nethermind.LeanCIL.ContractType[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractMethodSignature::set_ReturnType(Nethermind.LeanCIL.ContractType) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractSource::set_Module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractSource::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractType::set_Element(Nethermind.LeanCIL.ContractType) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractType::set_Kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractType::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ContractType::set_Scope(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Declaration::set_Exception(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Declaration::set_Text(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Site::set_Exception(Nethermind.LeanCIL.ExceptionType) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Site::set_IlOffset(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Site::set_Method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ExceptionLint/Site::set_Origin(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FieldRef::set_Index(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FieldRef::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FieldRef::set_Owner(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FieldRef::set_Type(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FrameMetadata::set_Declared(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.FrameMetadata::set_ReturnsValue(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::set_Kind(Nethermind.LeanCIL.GenericEvidence/RefusalKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.GenericEvidence/DecodeRefusal::set_Pc(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ActualLayouts(Nethermind.LeanCIL.ContractLayout[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_CertificationStatement(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_CertificationStatementDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_CertificationTheorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ClosureDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ComputedLayouts(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ContractConstant(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ContractKind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_DefinitionSignature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ImplementationPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ImplementationSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_InitializationAssumptions(Nethermind.LeanCIL.InitializationAssumption[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_InterfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_IsReferenceAssembly(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ParameterCount(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_PublishedLayouts(Nethermind.LeanCIL.ContractLayout[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_ReturnsValue(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_SidecarPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_SidecarSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_Slot(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_StaticIdentities(Nethermind.LeanCIL.StaticIdentityRecord[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Binding::set_TypedSignature(Nethermind.LeanCIL.ContractMethodSignature) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Library::set_Interface(Nethermind.LeanCIL.ContractInterface) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Library::set_SidecarSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Library::set_Source(Nethermind.LeanCIL.ImportResolution/Source) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Node::set_AssemblyIdentity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Node::set_Dependencies(Nethermind.LeanCIL.ContractDependency[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Node::set_ExpectedInterfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Node::set_ImplementationPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Node::set_ImplementationSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Node::set_InterfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Node::set_IsReferenceAssembly(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Node::set_SidecarPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Source::set_Discovered(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Source::set_ExpectedInterfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Source::set_ExpectedSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Source::set_ImplementationPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolution/Source::set_SidecarPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolver/Resolved::set_AdvertisedInterfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolver/Resolved::set_Assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolver/Resolved::set_AssetPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolver/Resolved::set_AssetRuntimeIdentifier(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolver/Resolved::set_Implementation(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolver/Resolved::set_Library(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ImportResolver/Resolved::set_Sidecar(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InitializationAssumption::set_AssemblyIdentity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InitializationAssumption::set_TypeName(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InitializedStaticValue::set_Construct(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InitializedStaticValue::set_Literal(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InterfaceModule::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.InterfaceModule::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LayoutField::set_IsAggregate(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LayoutField::set_Offset(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LayoutField::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Append::set_Left(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Append::set_Right(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Group::set_Body(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Item::set_Document(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Item::set_Indent(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Nest::set_Body(Nethermind.LeanCIL.LeanDoc) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Nest::set_Indent(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Space::set_FoundLine(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Space::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/Text::set_Value(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LeanDoc/WorkGroup::set_Flatten(Nethermind.LeanCIL.LeanDoc/Flatten) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Aggregate::set_Size(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Array::set_Element(Nethermind.LeanCIL.Arrays/Element) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/EnumSpan::set_ByReference(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/EnumSpan::set_Element(Nethermind.LeanCIL.Arrays/Element) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/EnumSpan::set_Writable(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/MethodPointer::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/MethodTable::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Narrow::set_Narrowing(Nethermind.LeanCIL.Narrowing) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/NativeMethodTable::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Span::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Span::set_Writable(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.LocalKind/Vector::set_Bits(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ManagedTarget::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ManagedTarget::set_Method(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/NamedRow::set_Assembly(System.UInt32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/NamedRow::set_Name(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/NamedRow::set_Namespace(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/NamedRow::set_Parent(System.UInt32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/TypeNameRow::set_Level(Nethermind.LeanCIL.TypeLevel) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataImage/TypeNameRow::set_ResolutionScope(System.UInt32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataValidation/AggregateInfo::set_Alignment(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataValidation/AggregateInfo::set_Size(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataValidation/InstanceField::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MetadataValidation/RefLikeMetadata::set_IsByRefLike(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodBodyBytes/Body::set_Bytes(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodBodyBytes/Body::set_HeaderLength(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_GenericArity(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_HasThis(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_Owner(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodRef::set_ReturnType(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodSpecialization/Origin::set_Definition(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.MethodSpecialization/Origin::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ParameterHome::set_Argument(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ParameterHome::set_Kind(Nethermind.LeanCIL.LocalKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Pin::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Pin::set_Sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Registration::set_Primitives(Nethermind.LeanCIL.StructuralSignatures/Primitive[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Source::set_ExpectedSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PinnedAssemblies/Source::set_Path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PlatformDefinition::set_Arity(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PlatformDefinition::set_Kind(Nethermind.LeanCIL.PlatformKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.PlatformDefinition::set_Underlying(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProfileInitializationAssumptions::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProfileInitializationAssumptions::set_Types(Nethermind.LeanCIL.InitializationAssumption[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot::set_Bytes(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionBatch/AssemblySnapshot::set_Hash(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionRequest::set_Assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionRequest::set_Entry(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionRequest::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionRequest::set_InitializedTypes(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionRequest::set_Output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionRequest::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Program/ExtractionRequest::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Array(Nethermind.LeanCIL.ProofOutline/HeldArray) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Called(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Field(Nethermind.LeanCIL.ProofOutline/ViewField) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Formed(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Kind(Nethermind.LeanCIL.ProofOutline/AccessKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Object(Nethermind.LeanCIL.ProofOutline/Reached) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Signed(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Stores(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Target(Nethermind.LeanCIL.ProofOutline/Reached) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Access::set_Value(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/AddedReference::set_Base(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/AddedReference::set_Index(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/AddedReference::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Allocation::set_Array(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Allocation::set_Length(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Allocation::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Allocation::set_Scope(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Allocation::set_Stores(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayAccess::set_Array(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayAccess::set_Helper(Nethermind.LeanCIL.ProofOutline/ElementHelper) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayAccess::set_Index(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayAccess::set_Load(Nethermind.LeanCIL.Arrays/NumericAccess) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayLengthValue::set_Array(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayUse::set_Array(Nethermind.LeanCIL.ProofOutline/HeldArray) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayUse::set_Length(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ArrayUse::set_Stores(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/CallStep::set_Callee(Nethermind.LeanCIL.ProofOutline/Callee) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/CallStep::set_FieldMemory(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/CallStep::set_Input(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/CallStep::set_Live(Nethermind.LeanCIL.ProofOutline/Reached[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/CallStep::set_Needs(Nethermind.LeanCIL.ProofOutline/Access[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/CallStep::set_Number(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/CallStep::set_Stores(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/CallStep::set_Where(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/CallStep::set_Writes(Nethermind.LeanCIL.ProofOutline/Access) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Callee::set_Index(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Callee::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Callee::set_Open(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Callee::set_Plan(Nethermind.LeanCIL.ProofOutline/ObjectPlan) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Callee::set_Spec(Nethermind.LeanCIL.ProofOutline/Specification) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Callee::set_Walk(Nethermind.LeanCIL.ProofOutline/ObjectWalk) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Component::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Component::set_Offset(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Component::set_Part(Nethermind.LeanCIL.ProofOutline/Part) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Component::set_Slot(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Component::set_Span(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Component::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Element::set_Component(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Array(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Formed(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Index(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Kind(Nethermind.LeanCIL.ProofOutline/ElementKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_LiteralIndex(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Reread(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Slot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Stores(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Target(Nethermind.LeanCIL.ProofOutline/Reached) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementAccess::set_Value(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementHelper::set_Index(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ElementHelper::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Architecture(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Emitted(Nethermind.LeanCIL.ProofOutline/Method[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Layouts(Nethermind.LeanCIL.ComputedLayout[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Extraction::set_Views(Nethermind.LeanCIL.ClassViews/View[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Element(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Field(Nethermind.LeanCIL.ProofOutline/ViewField) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Held(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Holder(Nethermind.LeanCIL.ProofOutline/Reached) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Length(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Whole(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/HeldArray::set_Words(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAccess::set_Index(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAccess::set_Store(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAccess::set_Stores(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAccess::set_Value(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAccess::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalAddressValue::set_Slot(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/LocalValue::set_Slot(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Number::set_Narrow(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Number::set_Stacked(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Number::set_Term(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Number::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectFrame::set_Entry(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectFrame::set_Footprint(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectFrame::set_Formed(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectFrame::set_Locals(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectTerm::set_Term(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/ArrayArgument::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/ElementIndex::set_Term(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Instance::set_Object(Nethermind.LeanCIL.ProofOutline/Reached) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::set_Formed(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Opaque::set_Term(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Values::set_Array(Nethermind.LeanCIL.ProofOutline/HeldArray) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::set_Literal(System.Int64) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::set_Numeral(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::set_Term(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ObjectWalk/Word::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Outline::set_Obligations(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Outline::set_Paths(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Outline::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Outline::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_ArrayElement(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Element(Nethermind.LeanCIL.Arrays/Element) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_ElementDefinition(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Fields(Nethermind.LeanCIL.ProofOutline/ViewField[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Lean(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Shape(Nethermind.LeanCIL.ProofOutline/ParameterShape) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Signed(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Parameter::set_Writable(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Reached::set_Fields(Nethermind.LeanCIL.ProofOutline/ViewField[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Reached::set_Held(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Reached::set_Holder(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Reached::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ReceiverField::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ReceiverField::set_Offset(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ReceiverField::set_OwnedName(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ReceiverField::set_ReadName(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ReceiverField::set_Written(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ReferenceArgument::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Array(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Boolean(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Lean(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Object(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Result::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Returning::set_Contract(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Returning::set_Kept(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Returning::set_Open(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Returning::set_Post(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Returning::set_Unit(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarCheck::set_Fits(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarCheck::set_Lemma(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarCheck::set_Next(Nethermind.LeanCIL.ProofOutline/ScalarStep) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarCheck::set_Operation(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarCheck::set_Pc(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarSplit::set_Condition(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarSplit::set_Fails(Nethermind.LeanCIL.ProofOutline/ScalarStep) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarSplit::set_FailsPc(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarSplit::set_Holds(Nethermind.LeanCIL.ProofOutline/ScalarStep) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarSplit::set_HoldsPc(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarSplit::set_Pc(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarWord::set_Bits(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ScalarWord::set_Term(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Sliced::set_Component(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Argument(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Components(Nethermind.LeanCIL.ProofOutline/Component[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Home(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Index(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Kind(Nethermind.LeanCIL.LocalKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Slot::set_Layout(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanArgument::set_Span(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanLengthValue::set_Component(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_Caseable(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_Checked(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_Head(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_Remaining(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_Tail(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_View(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpanTerm::set_Written(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/SpannedArray::set_Array(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Split::set_Condition(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Split::set_Fact(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Stub::set_Index(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Stub::set_Inputs(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Stub::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Stub::set_Result(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Stub::set_Widths(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/StubCall::set_Input(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/StubCall::set_Number(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/StubCall::set_Stores(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/StubCall::set_Stub(Nethermind.LeanCIL.ProofOutline/Stub) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/StubCall::set_Where(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Thrown::set_Where(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/Unknown::set_What(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Element(Nethermind.LeanCIL.Arrays/Element) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Kind(Nethermind.LeanCIL.ClassViews/FieldKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Loaders(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Offset(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Stored(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/ViewField::set_Storers(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordArrayArgument::set_Array(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Array(Nethermind.LeanCIL.ProofOutline/Parameter) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_CallerIndex(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Index(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Load(Nethermind.LeanCIL.Arrays/NumericAccess) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.ProofOutline/WordRead::set_Stores(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_AppLocal(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_AssetPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_AssetRid(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_Library(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_LocalPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_PackagePath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_ReferenceAssets(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.RuntimeAsset::set_Type(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticDataBlock::set_Alignment(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticIdentityRecord::set_AssemblyIdentity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticIdentityRecord::set_FieldName(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticIdentityRecord::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticSite::set_Detail(Nethermind.LeanCIL.StaticSiteDetail) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticSite::set_Field(Nethermind.LeanCIL.FieldRef) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticSite::set_Identity(Nethermind.LeanCIL.StaticIdentityRecord) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticSite::set_Pc(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Occurrence::set_Order(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Self::set_Data(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Snapshot::set_Data(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Value::set_Arguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Value::set_Bytes(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Value::set_Construct(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Value::set_Literal(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Value::set_Opaque(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Word::set_Bits(System.UInt64) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StaticValues/Word::set_Width(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Primitive::set_Assembly(System.UInt32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Primitive::set_Image(Nethermind.LeanCIL.MetadataImage) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Primitive::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Primitive::set_Token(System.UInt32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.StructuralSignatures/Snapshot::set_Prefix(System.Int64[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Symbols/PdbFile::set_Bytes(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.Symbols/PdbFile::set_Sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Features(Nethermind.LeanCIL.Feature[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Frame(Nethermind.LeanCIL.FrameMetadata) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_LinkedMethods(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Method::set_Opcodes(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_Check(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_Extractions(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_ModuleName(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_ModulePath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_Namespace(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_OmitEquations(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Options::set_Output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Row::set_ComparisonBody(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Row::set_Frame(Nethermind.LeanCIL.FrameMetadata) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Row::set_Variant(Nethermind.LeanCIL.TemplateEmitter/Variant) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Callees(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Features(Nethermind.LeanCIL.Feature[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Frame(Nethermind.LeanCIL.FrameMetadata) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Opcodes(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Template::set_Rows(Nethermind.LeanCIL.TemplateEmitter/Row[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Variant::set_AssemblyHash(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Variant::set_Directory(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Variant::set_Methods(Nethermind.LeanCIL.TemplateEmitter/Method[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TemplateEmitter/Variant::set_Profile(Nethermind.LeanCIL.FeatureProfile) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeIdentity::set_Assembly(Nethermind.LeanCIL.AssemblyName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeIdentity::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeIdentity::set_Namespace(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeKind/EnumType::set_Underlying(Nethermind.LeanCIL.PrimitiveType) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeLevel::set_Name(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeLevel::set_Namespace(Nethermind.LeanCIL.MetadataName) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeTests/Node::set_Kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeTests/Node::set_Left(Nethermind.LeanCIL.TypeTests/Node) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeTests/Node::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeTests/Node::set_Right(Nethermind.LeanCIL.TypeTests/Node) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Nethermind.LeanCIL.TypeTests/Node::set_Scope(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditConstantRequest::set_axioms(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditConstantRequest::set_name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditNamespaces::set_axioms(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditNamespaces::set_prefixes(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRefutation::set_column(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRefutation::set_contract(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRefutation::set_line(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRefutation::set_program(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRequest::set_allowedModules(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRequest::set_axioms(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRequest::set_name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRequest::set_refutation(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRequest::set_statement(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditRequest::set_term(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedClosure::set_canonical(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedClosure::set_error(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedClosure::set_kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedClosure::set_name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedInterfaces::set_error(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedNamespaces::set_axioms(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedNamespaces::set_count(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedNamespaces::set_error(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedNamespaces::set_rejected(Program/AuditedTheorem[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedOleans::set_closures(Program/AuditedClosure[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedOleans::set_constants(Program/AuditedTheorem[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedOleans::set_interfaces(Program/AuditedInterfaces) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedOleans::set_namespaces(Program/AuditedNamespaces) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedOleans::set_replayed(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedOleans::set_theorems(Program/AuditedTheorem[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedTheorem::set_axioms(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedTheorem::set_error(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedTheorem::set_mismatch(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedTheorem::set_name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedTheorem::set_refutes(Program/AuditRefutation) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedTheorem::set_statement(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/AuditedTheorem::set_termError(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchManifest::set_proofRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchManifest::set_requests(Program/BatchRequest[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchManifest::set_schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchOptions::set_CheckOnly(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchOptions::set_Fresh(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchOptions::set_Json(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchOptions::set_RebuildLibrary(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchOptions::set_ResultsPath(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchOptions::set_Trace(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchRequest::set_expect(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchRequest::set_output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchRequest::set_proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchRequest::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_axioms(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_error(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_expect(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_failure(FailureReports/Report) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_libraryBuild(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_stage(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_status(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchResult::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchRun::set_Results(Program/BatchResult[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BatchRun::set_Verified(Program/Report[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingCatalog::set_Gates(Program/CatalogGate[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingCatalog::set_GeneratedRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingCatalog::set_Path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingData::set_Bytes(System.Byte[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingFactResult::set_id(Program/BindingFact) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingFactResult::set_reason(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingFactResult::set_status(Program/BindingStatus) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingFactResult::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingImagePin::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingLibraryPreparation::set_Library(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingLibraryPreparation::set_LibraryKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingLibraryPreparation::set_Toolchain(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPlan::set_Images(Program/BindingImagePin[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPlan::set_Imports(Program/BindingPublication[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPlan::set_Methods(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPlan::set_Output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Closure(Program/BindingData) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Contract(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_InterfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Producer(Program/BindingImagePin) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingPublication::set_Sidecar(Program/BindingData) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingRequest::set_Audit(Program/AuditRequest) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingRequest::set_CoveragePredicate(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingRequest::set_Module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingRequest::set_Subject(Program/BindingSubject) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_digests(Program/DigestFile) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_facts(Program/BindingFactResult[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_images(Program/ImageResult[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_mode(Program/BindingMode) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSection::set_sources(Program/StatementSources) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSubject/Fact::set_Complete(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BindingSubject/Fact::set_Value(Program/BindingFact) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BuildDiagnostic::set_Code(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BuildDiagnostic::set_Column(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BuildDiagnostic::set_File(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BuildDiagnostic::set_Line(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/BuildDiagnostic::set_Message(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogFamily::set_assign(Program/CatalogAssign[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogFamily::set_gate(Program/CatalogGatePattern) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogFamily::set_template(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogFile::set_families(Program/CatalogFamily[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogFile::set_generatedRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogFile::set_schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGate::set_Inputs(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGate::set_Method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGate::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGate::set_Profiles(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGate::set_Proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGate::set_Theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGatePattern::set_method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGatePattern::set_name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGatePattern::set_profiles(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGatePattern::set_proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CatalogGatePattern::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedFingerprintManifest::set_Schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedFingerprintManifest::set_Tools(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedReleaseAsset::set_Archive(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedReleaseAsset::set_ArchiveBytes(System.Int64) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedReleaseAsset::set_ArchiveSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedReleaseAsset::set_LibraryKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedReleaseAsset::set_ManifestSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedReleaseAsset::set_Runtime(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedReleaseAsset::set_Schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CertifiedReleaseAsset::set_Tag(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CheckedContractExport::set_AuditSource(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CheckedContractExport::set_AuthorModule(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CheckedContractExport::set_Closure(Program/ExportClosure) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CheckedContractExport::set_Export(Program/PreparedContractExport) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CheckedContractExport::set_Statement(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/CheckedContractExport::set_StatementDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ClosureEntry::set_Built(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ClosureEntry::set_Directory(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ClosureEntry::set_Key(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ClosureEntry::set_Modules(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerDependency::set_Directory(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerDependency::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerDependency::set_Revision(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerDependency::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerDependency::set_Url(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerExportAudit::set_Closures(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerExportAudit::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerExportAudit::set_Theorems(Program/AuditRequest[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerRequest::set_Assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerRequest::set_ExpectRejection(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerRequest::set_ExportAudit(Program/ConsumerExportAudit) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerRequest::set_Output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerRequest::set_Proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerRequest::set_Settings(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerRequest::set_Statement(Program/StatementRequest) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerRequest::set_Theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSession::set_Generated(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSession::set_Sources(SourceCopies) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportManifest::set_Library(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportManifest::set_Policy(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportManifest::set_Schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportManifest::set_Toolchain(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportManifest::set_Tools(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportModule::set_Files(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ConsumerSupportModule::set_Imports(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepClosureWork::set_Hashed(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepClosureWork::set_HashedBytes(System.Int64) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepClosureWork::set_Headers(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepClosureWork::set_Launches(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_Modules(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_Restored(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_ResultKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_SourceDirectory(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeepSessionContribution::set_Targets(Program/BindingTargets) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_acceptedVia(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_assemblyIdentity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_conditional(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_dependencies(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_implementationSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_interfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DeploymentDependency::set_sourceBundleDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DigestFile::set_path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/DigestFile::set_sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorProof::set_Proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorProof::set_Theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorSelection::set_Assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorSelection::set_AssemblyIdentity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorSelection::set_InitializedTypes(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorSelection::set_Method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorSelection::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EditorSelection::set_Proofs(Program/EditorProof[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EnvironmentEntry::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EnvironmentEntry::set_SetByLake(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/EnvironmentEntry::set_Value(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExportClosure::set_Canonical(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExportClosure::set_Contract(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExportClosure::set_Digest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExportClosure::set_Kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractedMethod::set_assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractedMethod::set_assemblySha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractedMethod::set_signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractionComponents::set_Aggregate(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractionComponents::set_InputHash(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractionIdentity::set_Output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ExtractionIdentity::set_Sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FingerprintInput::set_Key(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FingerprintInput::set_Name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FingerprintInput::set_Source(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullBundle::set_Consumer(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullBundle::set_Dependency(Program/ConsumerDependency) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullBundle::set_Interfaces(Nethermind.LeanCIL.ContractSource[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullBundle::set_Selection(Program/FullSelection) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullBundle::set_Staging(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullImport::set_Certification(Nethermind.LeanCIL.ContractCertification) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullImport::set_Export(Nethermind.LeanCIL.ContractExport) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullImport::set_Id(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullImport::set_Selection(Program/FullSelection) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullImportResult::set_Audit(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullImportResult::set_Dependencies(Program/DeploymentDependency[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullImportResult::set_Imports(Program/PublishedImport[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullImportResult::set_Theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_AssemblyIdentity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_BundleDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_Contract(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_Dependencies(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_ExpectedModule(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_GeneratedSource(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_Id(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_Kind(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_Module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_Statics(Nethermind.LeanCIL.StaticIdentityRecord[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullLibrary::set_TransferSource(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSelection::set_Bundle(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSelection::set_Contract(Nethermind.LeanCIL.ContractInterface) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSelection::set_Digest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSelection::set_Implementation(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSelection::set_ImplementationSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSelection::set_Inventory(Nethermind.LeanCIL.ContractBundleManifest) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSelection::set_Sidecar(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSelection::set_SidecarSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSlot::set_Artifact(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSlot::set_Author(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSlot::set_Generated(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSlot::set_GeneratedSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSlot::set_Import(Program/FullImport) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSlot::set_Request(Nethermind.LeanCIL.ContractExportRequest) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/FullSlot::set_Statics(Nethermind.LeanCIL.StaticIdentityRecord[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateBuild::set_project(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateBuildPlan::set_AssemblyName(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateBuildPlan::set_Directory(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateBuildPlan::set_Project(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateDefaults::set_assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateDefaults::set_build(Program/GateBuild) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateDefaults::set_initializedTypes(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateDefaults::set_typeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateGenerated::set_Path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateGenerated::set_Stage(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateGenerated::set_Template(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateManifest::set_catalogs(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateManifest::set_defaults(Program/GateDefaults) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateManifest::set_gates(Program/ProofGateEntry[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateManifest::set_generatedRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateManifest::set_outputRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateManifest::set_proofRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateManifest::set_schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateManifest::set_selection(Program/GateSelectionRules) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlan::set_groups(Program/GatePlanGroup[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlan::set_manifest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlan::set_revision(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlan::set_selected(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlan::set_selection(Program/GatePlanSelection) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlanGroup::set_id(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlanGroup::set_rows(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlanSelection::set_changed(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlanSelection::set_count(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlanSelection::set_method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlanSelection::set_only(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GatePlanSelection::set_profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateProject::set_assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateProject::set_path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateRefutation::set_proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateRefutation::set_template(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateRefutation::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateRefutationResult::set_axioms(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateRefutationResult::set_error(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateRefutationResult::set_proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateRefutationResult::set_stage(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateRefutationResult::set_status(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateRefutationResult::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_axioms(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_build(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_error(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_expect(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_extraction(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_failure(FailureReports/Report) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_gate(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_id(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_libraryBuild(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_program(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_refutation(Program/GateRefutationResult) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_stage(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_statement(Program/GateStatement) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_status(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResult::set_variant(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResults::set_manifest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResults::set_revision(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateResults::set_rows(Program/GateResult[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateSelectionRules::set_all(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateSelectionRules::set_ignore(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateStatement::set_production(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateStatement::set_status(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateStatement::set_variant(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateTemplate::set_path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateTemplateUse::set_template(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_build(Program/GateBuild) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_expect(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_initializedTypes(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_profiles(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_program(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_programOf(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_refutation(Program/GateRefutation) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_statement(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GateVariant::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GeneratedExtraction::set_Assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GeneratedExtraction::set_Entry(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GeneratedExtraction::set_InitializedTypes(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GeneratedExtraction::set_Output(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GeneratedExtraction::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/GeneratedExtraction::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_reason(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_role(Program/ImageRole) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_sha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_status(Program/BindingStatus) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ImageResult::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LibraryAudit::set_Modules(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LibraryAudit::set_Replayed(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LibraryAudit::set_Result(Program/AuditedNamespaces) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LoadedGates::set_All(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LoadedGates::set_Digest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LoadedGates::set_Directory(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LoadedGates::set_Ignore(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LoadedGates::set_OutputRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LoadedGates::set_Path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/LoadedGates::set_ProofRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_ComputedLayouts(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_Conditional(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_DefinitionSignature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_Dependencies(Nethermind.LeanCIL.ContractDependency[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_Identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_ImportedInterfaces(Nethermind.LeanCIL.InterfaceModule[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_InitializationAssumptions(Nethermind.LeanCIL.InitializationAssumption[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_LayoutIndices(System.Int32[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_Layouts(Nethermind.LeanCIL.ContractLayout[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_ParameterCount(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_Proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_Request(Nethermind.LeanCIL.ContractExportRequest) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_ReturnsValue(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_StaticIdentities(Nethermind.LeanCIL.StaticIdentityRecord[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_TypeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_TypedSignature(Nethermind.LeanCIL.ContractMethodSignature) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PreparedContractExport::set_Visibility(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_build(Program/GateBuild) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_initializedTypes(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_profiles(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_typeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofGateEntry::set_variants(Program/GateVariant[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofMessage::set_column(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofMessage::set_line(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofMessage::set_severity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ProofMessage::set_text(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_acceptedVia(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_certificationAuthenticated(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_certificationStatement(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_certificationStatementDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_certificationTheorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_deploymentObligation(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_identity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_implementationSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_interfaceDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_sidecarSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_slot(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_sourceBundleDigest(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/PublishedImport::set_typeArguments(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ReplayRejection::set_Declaration(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/ReplayRejection::set_Kind(Program/ReplayRejectionKind) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_assemblySha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_axioms(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_batchSourceSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_batchTheorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_binding(Program/BindingSection) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_compositionAudit(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_compositionTheorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_consumerSupportSha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_contractDependencies(Program/DeploymentDependency[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_contractImports(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_extractionAudit(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_identitySha256(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_imports(Program/PublishedImport[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_initializedTypes(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_libraryBuild(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_libraryKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_libraryModules(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_opaqueStaticValues(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_refutation(Program/VerifiedRefutation) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_specification(Program/StatementDefinition[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_statementIdentity(Program/StatementIdentity) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_staticValues(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/Report::set_theoremAudit(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_ExtractionKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_ExtractionSuffix(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_FullLibraryKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_ProofKey(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StandaloneCachePromotion::set_ProofSuffix(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementDefinition::set_name(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementDefinition::set_text(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementIdentity::set_mismatch(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementIdentity::set_termError(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementRequest::set_Expected(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementRequest::set_Refuted(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementRequest::set_Term(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSources::set_lean(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSources::set_leanToolchain(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSources::set_strictBinding(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSubject::set_extracted(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSubject::set_head(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSubject::set_role(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementSubject::set_signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementView::set_definitions(Program/StatementDefinition[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementView::set_statement(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/StatementView::set_subjects(Program/StatementSubject[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/TrustedSupport::set_Manifest(Program/ConsumerSupportManifest) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/TrustedSupport::set_Path(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerificationSettings::set_proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerificationSettings::set_proofRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerificationSettings::set_schema(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerificationSettings::set_theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerifiedRefutation::set_column(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerifiedRefutation::set_contract(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerifiedRefutation::set_line(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/VerifiedRefutation::set_method(Program/ExtractedMethod) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Message(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Module(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Rechecked(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Status(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchResult::set_Theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchSettings::set_Configuration(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchSettings::set_Goals(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchSettings::set_Json(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchSettings::set_Once(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchSettings::set_Projects(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchSettings::set_Root(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchState::set_Extracted(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchState::set_Status(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WatchState::set_Symbols(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WorkspaceSettings::set_Assembly(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WorkspaceSettings::set_AssemblyIdentity(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WorkspaceSettings::set_CreatedPackage(System.Boolean) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WorkspaceSettings::set_InitializedTypes(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WorkspaceSettings::set_Method(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WorkspaceSettings::set_Profile(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WorkspaceSettings::set_Proof(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WorkspaceSettings::set_ProofRoot(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) Program/WorkspaceSettings::set_Theorem(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) VerifyReports/ManifestMethod::set_Instructions(System.String[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) VerifyReports/ManifestMethod::set_Points(VerifyReports/SourcePoint[]) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) VerifyReports/ManifestMethod::set_Signature(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) VerifyReports/SourcePoint::set_Column(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) VerifyReports/SourcePoint::set_File(System.String) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) VerifyReports/SourcePoint::set_Line(System.Int32) +System.Void modreq(System.Runtime.CompilerServices.IsExternalInit) VerifyReports/SourcePoint::set_Pc(System.Int32) +VerifyReports closure classes (compiler-generated) +VerifyReports closure classes (compiler-generated) +VerifyReports closure classes (compiler-generated) +VerifyReports closure classes (compiler-generated) +VerifyReports/ManifestMethod::Clone (compiler-generated) +VerifyReports/SourcePoint::Clone (compiler-generated) +VerifyReports/SourcePoint[] VerifyReports/ManifestMethod::get_Points() +VerifyReports::Failure (compiler-generated) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 5c36c586e..484a28549 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -52,7 +52,8 @@ order. - Contract refinement derives a public interface from a stronger proved contract; that interface can be exported and imported by callers. - `cil_embed [facts]` binds a shared body template to an extraction using proved feature flag and - callee equalities. Local definitions also work without a fact list; relevant flags left + callee equalities; the template carries the extraction's declared signature and layout environment. + Local definitions also work without a fact list; relevant flags left unconstrained are rejected, and the kernel checks the complete instruction map. - `leancil templates shared scalar-extraction sse42-extraction v3-extraction` generates the shared bodies from compiled CIL, including per-instruction equations and a binding module for diff --git a/docs/CI.md b/docs/CI.md index 41fbf1a73..1184bc919 100644 --- a/docs/CI.md +++ b/docs/CI.md @@ -9,7 +9,7 @@ the workflow, the selection scripts or a documentation marker. ## Jobs -All jobs run on `ubuntu-24.04`. A newer run on the same ref cancels the one in progress. Uploaded +All jobs run on `ubuntu-26.04`. A newer run on the same ref cancels the one in progress. Uploaded artifacts are kept for 14 days. | Job | Name | Limit | Does | @@ -66,8 +66,9 @@ concurrent jobs. measured time runs alone. - `dotnet run --file scripts/EstimateVerificationSchedule.cs -- --jobs` prints the jobs of a full run with their estimated minutes. -- A job's time limit is the largest of its members': 90 minutes for `imports`, 120 for each - `exports` partition and 30 for any other suite. +- A job's time limit is the largest of its members' (90 minutes for `imports`, 120 for each + `exports` partition and 30 for any other suite) and at least twice the job's measured time, + rounded up to ten minutes. - A job is named for its suites, or for its longest suite and the number of others when it runs more than three, so job names change when the durations do. Only `Build and verify` is a stable check name. @@ -239,7 +240,8 @@ A source is measured on each tree by the run that verifies it: reported as unmeasured. Each extracted method's instruction equations are one row, named for its `cil_instruction_equations` command. A directory is measured only while it holds the report of an accepted proof, so a suite that also checks rejected proofs against an extraction verifies - the accepted one last. + the accepted one last. A report that is not a certificate a default `verify` accepts, by its + schema and binding section, leaves its directory unmeasured. ### Row measurement @@ -350,7 +352,9 @@ Selection runs `dotnet run --file scripts/CheckDocReferences.cs`, which needs no functions: `step`, `run`, `invoke`, `pureArity`, `scalars`, `instructionStep`, `staticInstruction`, `checkedIntrinsic`, `checkedValue`, `numericValue`, `checkedAt`, `formValue`, `storeValue`, `instruction`, `memoryInstruction`, `otherMemoryInstruction`, - `observerCount`, `unspecifiedOperands` or `hasUnspecified`, or the monad plumbing + `observerCount`, `unspecifiedOperands`, `hasUnspecified`, the runtime model runner `runModel` + and its helpers `observedCall`, `intrinsicCall`, `featureCall`, `referenceCompareCall`, + `referenceSubtractCall`, `typedTransferCall` and `skipInitCall`, or the monad plumbing `Except.mapError`, `Bind.bind`, `Except.bind`, `Pure.pure` or `Except.pure`, by any suffix of its full name. Such a proof runs the method by unfolding the interpreter; step with `cil_checked_steps` and pass a fact about the operation instead. A name the block binds with @@ -362,6 +366,15 @@ Selection runs `dotnet run --file scripts/CheckDocReferences.cs`, which needs no [public proof surface](PROOFS.md#public-proof-surface). - A document declares at most one audience, `users` or `maintainers`, and closes every code fence. A Lean block's info string starts with exactly `lean`; another spelling of it fails. +- No file that git tracks with the extension `.lean`, `.cs`, `.md`, `.toml`, `.json`, `.yml`, + `.txt`, `.tsv` or `.csv` holds a byte below 0x20 other than tab, line feed and carriage return; + the failure names the file, line and byte. A shell that expands a backtick escape writes such a + byte into a docstring, and git then treats the file as binary. +- No such file holds a character above ASCII that shows nothing, shows as a space or reorders the + text around it: a format character such as a zero-width space, a byte order mark or a + bidirectional override, a line or paragraph separator, a space other than U+0020, or a control + character. The failure names the file, line and code point. A diff does not show such a + character, so source spells it as an escape such as `\u200B`. ### Lean blocks diff --git a/docs/CLI.md b/docs/CLI.md index 18b99cd27..f0fdc7cee 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -9,6 +9,7 @@ modes. For one complete verification, read the [getting-started guide](GETTING-S | `leancil extract` | Validate a method and its reachable calls and translate them to Lean | [Extract](#extract) | | `leancil blockers` | Report everything that stops `extract` for a list of methods | [Report extraction blockers](#report-extraction-blockers) | | `leancil check` | Compare extraction artifacts with their assembly and settings | [Check](#check) | +| `leancil verify --deep-checks` | Reconfirm in the kernel that an extraction's method bodies and carried metadata rows are those of assembly files with stated digests | [Deep verification](#deep-verification) | | `leancil new-proof` | Write a starting proof with marked holes | [Start a proof](#start-a-proof) | | `leancil verify` | Check, kernel-replay and audit one theorem | [Verify](#verify) | | `leancil verify-batch` | Verify many requests in one session | [Batch verification](CONSUMERS.md#batch-verification) | @@ -113,7 +114,7 @@ The output directory holds: | File | Content | | --- | --- | -| `Extracted.lean` | The program: here `arg 0`, `arg 1`, `add`, `ret`, with the exact method signature, metadata, profile and kernel-checked fetch equations. The entry is method zero; reachable managed callees follow in dependency order. | +| `Extracted.lean` | The program: here `arg 0`, `arg 1`, `add`, `ret`, with the exact method signature, metadata, profile and kernel-checked fetch equations. The entry is method zero; reachable managed callees follow in dependency order. Every method, Add included, is defined from its raw definition by [`cil_decoded_method`](PROOFS-BINDING.md#methods-defined-from-their-decoder); extraction refuses a method whose decoder refuses a live instruction. | | `identity.json` | The manifest: assembly path, identity and SHA-256, entry, profile, assumptions, references, imports and extractor hash. | | `Symbols.lean` | PDB names and source locations, written only when the assembly has a matching portable PDB. | @@ -170,6 +171,8 @@ identity only, as an `Extraction.AssemblyPin` attributed to every body it suppli replays the recorded references and rejects a changed hash or identity. Assemblies resolve only against this pinned set and the runtime's platform assemblies, so an unpinned reference, a name pinned under another version, a duplicate identity or a framework DLL passed as a reference fails. +For a top-level type forwarded by a referenced facade, pass both the facade DLL and its defining DLL +with `--reference`. Lean checks the `ExportedType` name, destination `AssemblyRef` and public `TypeDef`. One assembly cannot be both a typed import and a reference. A referenced assembly's static methods are admitted as direct `call` and `ldftn` targets, and its @@ -252,6 +255,12 @@ whatever the entries' status. Each entry has one status: | `blocked` | At least one blocker is listed. An entry the selector resolves but `extract` rejects by its shape, such as an `init` setter or a member of an open generic type, is `blocked`, and its definition is analysed as an entry. | | `notChecked` | Nothing was analysed, for the reason given: a selector that matches no method or several, a malformed type argument, an unknown profile, or an assembly, import or assumption that fails before any method is checked. | +An admitted entry lists its extracted methods in `decodedMethods`; the extraction defines each +from its decoder. A reachable method whose decoder refuses a live pc blocks its entry: the +blocker's site names that method, the IL offset and instruction, and its message the pc and why +the decoder refuses it (an unsupported opcode, or a call or construction whose translation differs +from the decoder's instruction). Dead pcs decode to holes carrying their raw opcodes. + Each blocker is one unsupported construct (instruction, call, type, layout, method metadata or static constructor). It lists every site, with method, IL offset, instruction and the PDB source location when one is present, and every affected entry with the call chains from it to those @@ -306,6 +315,130 @@ or extractor, or a saved file that differs from the regenerated one, fails at th `extraction integrity` stage; run `leancil extract` again. A manifest whose `schema` is not `3` is rejected the same way. +## Deep verification + +`leancil verify extraction-directory --deep-checks digests.sha256` jointly checks the proof, +extraction certificate and image binding theorems in one audited environment. The option also +works with `--proof-root`. Supply a sha256sum file containing the selected assembly and every +reference pinned by the extraction: + +```text +<64 hexadecimal digits> path/to/App.dll +<64 hexadecimal digits> path/to/Library.dll +``` + +Paths are relative to the digest file. Each supplied file is read and checked against its stated +digest before Lean preparation. Missing pins, malformed records and mismatching digests fail as +arguments. The verifier retains the checked bytes and matches pins after any required extraction +rebuild. Each request must explicitly supply the option; saved verification settings contain no +deep-check input. Batch verification, gates, watch and workspace reject the option. + +The report lists twelve binding facts. Default verification lists them as `trusted` and performs +no image binding work. Deep verification reconfirms `body-bytes`, `metadata-rows`, `static-data` and the +digests of pinned images. For RVA fields the audit reads the field signature, packing, class +size and relative virtual address, then compares the entire byte range in the mapped section. +Constructor-initialized fields must have no RVA. + +To check `object-fields`, add the platform reference assembly used at compilation, such as +`ref/net11.0/System.Runtime.dll`, to the digest file. The audit resolves the version, culture +and public key token of the core scopes in the certified method evidence. In the corresponding +image it finds the public `System.Object` class and checks the flags of each field the class +declares. A facade that forwards the type also needs the destination image in the digest file. +The audit follows the exact assembly reference recorded in each forwarding row. Static fields are allowed. An instance field fails verification even when the supplied +image matches its stated digest. Without the required platform image, `object-fields` has +status `not covered`. + +For a selected `System.ValueTuple` layout, the same option checks +`value-tuple-field-order`. Supply the defining image or the complete forwarding chain for +the pair's own System.Runtime reference. The audit checks the two generic parameter slots +and reads the instance fields in declaration order: `Item1` has type `T1` and `Item2` has +type `T2`. The object and pair can have different forwarding destinations. A missing pair +destination leaves this fact uncovered. Runtime byte offsets remain a layout assumption. + +For `platform-type-kinds`, supply the defining assembly and any intervening facades for every +external type in the extraction's platform table. Lean checks each scoped type's flags, base +and generic arity against its TypeDef and GenericParam rows. A missing assembly leaves the +fact uncovered. A conflicting supplied definition fails verification even when another assembly +is missing. + +For `enum-underlying-types`, supply the assemblies that define the selected enums and any +facades that forward them. Lean follows the scoped name, including the enclosing type for a +nested enum, and reads the integer signature of the defining row's instance field `value__`. +An extraction with no enum or a missing destination reports `not covered`. A supplied +definition with the wrong storage code fails verification. + +For `layout-content`, Lean reads each selected struct from the pinned image. It checks +sequential or explicit layout, packing, declared size, instance field order and integer field +signatures, and explicit field offsets. The certified extraction uses those same raw layouts +to compute sizes, alignments and offsets for its architecture. An empty selection, imports, +missing images, or structs with unhandled fields, generics, custom attributes or interfaces +leave the fact `not covered`. A conflicting supported layout fails the audit. + +For `dispatch-catalogs`, Lean checks ordinary virtual slots of nongeneric classes in a single +pinned assembly. It reads the physical parent of every TypeDef, enumerates all descendants of +each slot owner, and checks their class flags, declared methods and direct local nongeneric +interfaces against the catalog. Each interface must resolve to its unique physical TypeDef. +Every reachable virtual site must dispatch to the implementations selected by these declarations. +Empty selections, multiple audited images, imports, constrained calls, casts, interface dispatch, +explicit method overrides, additional metadata modules and unsupported interface types, ancestry +or signatures leave the fact `not covered`. A different catalog or target table for a supported +slot fails verification. + +For `generic-constraints`, Lean locates each selected method and its declaring type in the +pinned image. It compares each parameter's ordinal, flags and complete constraint list, +including the argument types of locally defined generic interfaces instantiated with primitive +types. For value-type layouts it compares the direct interface implementations. When a struct +has primitive type arguments, Lean reads its own parameter declarations and substitutes those +arguments at the positions specified by the interface signature. A conflicting declaration fails +the check. An extraction with no generic selection, an import, an unsupported layout argument +or an unsupported constraint signature reports `not covered`. + +For `public-key-tokens`, include the image for each direct assembly reference of the selected +assembly and its pinned references. Lean reads each physical AssemblyRef, compares the tokens +carried by the extraction with those rows, and computes each publisher token from the full key +in the referenced image. An empty definition key denotes an unsigned assembly. A missing +target leaves the fact `not covered`. The report records additional images +used for this check with role `reference`. + +A fact is reconfirmed exactly when a theorem stating it passed the joint audit. Supplied images that no +pin or checked claim uses are listed as unused and not covered. Successful reports include the +digest file hash, StrictBinding source key and Lean toolchain. Unchecked facts remain assumptions. + +Generated image modules hold checked SHA-256 theorems. Each page of an image is one +`CIL.Extraction.fixedDigitsWithin` term that splits a code joined from numerals of at most 256 +bytes into the page's 64-byte chunks, so chunks that end in the same bytes cost the kernel no more +than others. The image association theorem ties each +pinned identity to its image. `StrictBinding.BodiesBound` relates the method evidence to the +bytes at the offsets decoded from the PE container and MethodDef rows. +`StrictBinding.MetadataRowsBound` checks each metadata block the evidence carries against the +pinned image its identity names: its table header, Assembly row, table rows and `#Strings`, +`#Blob` and `#US` entries are the image's, with MethodDef RVAs zeroed, under one numbering of the +`#Strings` and `#Blob` offsets per heap for the table rows and another for the Assembly row. The +assembly identity, key token, core library, links, the `#GUID` heap, the `Module` table and the +RVAs of MethodDefs outside the evidence are not part of that check. Both facts are checked in pages +of about 250 methods, capped at eight pages, and each page decodes the pinned images again; the +metadata check also checks each distinct Assembly block once, in one more lemma. These +theorems join the extraction certificate and the selected proof for kernel replay, expected-type +checks and axiom checks. Source, artifact, identity and toolchain guards run before successful +publication. + +A failed request produces no successful certificate. Once selection and pin matching succeed, +its failure report includes all facts and matched images. A named rejected binding theorem marks +only its own subject `failed`; this means the check was rejected. Unclassified build/replay, +resource and integrity failures leave subjects `not covered` with the failure stage and diagnostic. +Selection, digest parsing, pin matching and full-import preflight failures precede the attempt +and have no binding payload. A failed attempt never reports a fact as trusted or reconfirmed. + +The image decoder supports metadata tables through `0x2C`, without pointer or edit-and-continue +tables, and the `#~`, `#Strings`, `#US`, `#GUID` and `#Blob` streams. Unsupported image structure +rejects the request. A deep request reuses the generated modules of an earlier successful request +in the same certified environment, with the same StrictBinding sources, toolchain, pins and +generated sources, which hold the image bytes, and the same bytes of every compiled module they +import, transitively, outside the Lean toolchain: the extraction, StrictBinding and `CIL` +modules. It skips their build and kernel replay but still audits every deep theorem's type and +axioms against the current extraction. Under `--proof-root` the key also binds the certified +library. + ## Start a proof Write a starting proof of an extraction's entry method: @@ -592,9 +725,10 @@ closed from `pre` by `cil_loop_obligation`. The outline handles these shapes: stores a field beside a test: the hole names the callee's method index and the `MethodContract` to write and prove with `Holds Extracted.program` at that index. -Where the outline cannot step a path, it leaves a hole whose comment starts with `not outlined:`, -names what it met and what that step needs, and gives the source line to continue from. The -`Holes` line counts these apart from the obligations. The unedited file elaborates with only +Where the outline cannot step a path, it leaves a hole whose comment starts with `not outlined:` +and explains what that step needs. When the comment names the instruction, it gives the IL offset, +CIL opcode and operand, for example `IL_0001: stloc.0`. Source lines are included when available. +The `Holes` line counts these paths apart from the obligations. The unedited file elaborates with only warnings that a declaration uses `sorry`, and `leancil verify` rejects it at the theorem audit for `sorryAx` until every hole is filled. @@ -636,7 +770,7 @@ abbrev State (home0 : Reference) := -- The cutpoint is the loop's back-edge test at Loops.cs:38. bind_cutpoint header (home0 : Reference) (state : State home0) := - Extracted.method0 at loop.latch matching (· matches .brnonzero _) + Extracted.method0 at loop.latch matching (· matches .brtrue _) with entry.frame home0, [.scalar (.i32 state.values)], state.memory set_option linter.unusedVariables false in @@ -656,7 +790,7 @@ theorem correctness (value : W32) : Ensures Extracted.program 0 obtain ⟨home0, entered, enter⟩ := entry.checked value have setup := enter.setup have entryFrame := enter.frame - refine invoke_loop_satisfies Extracted.lookup0 rfl setup (header home0) (invariant value) + refine invoke_loop_satisfies Extracted.program_method0 rfl setup (header home0) (invariant value) (measure value) (fun start => ?_) ?_ · have entryFrame := entryFrame [] cil_loop_recurse start value @@ -713,6 +847,7 @@ Lean and Lake must be installed. | `--proof` | The proof file. The default is the saved setting, else `Proof.lean` in the output directory. | | `--theorem` | The theorem to check. The default is the saved setting, else `ExampleProof.correctness`. | | `--proof-root` | The Lake package holding the proof's own modules; see [consumer proof packages](CONSUMERS.md#consumer-proof-packages). | +| `--deep-checks digests.sha256` | Joint image binding checks using consumer-stated digests; see [deep verification](#deep-verification). | | `--fresh` | Bypass proof and support reuse; the source-bound library cache is kept. | | `--rebuild-library` | Rebuild the library as well. CI bypasses the local library cache automatically. | | `--goals` | Print the goal at each hole; see [goals of an unfinished proof](#goals-of-an-unfinished-proof). | @@ -754,14 +889,20 @@ certifies every proof and proof dependency for one framework version, and buildi several minutes. It is cached in the directory `LEANCIL_CACHE_DIR` names, by default `/LeanCIL/libraries`, under a key of the framework sources, toolchain and platform. A cache miss first prints one line saying what is being built and kernel-replayed -and where later runs reuse it from. +and where later runs reuse it from. A run that uses a library it did not build, whether supplied, +cached, or downloaded and kernel-replayed into the cache, checks the library's files against its +manifest and traces the stage `Opening certified Lean verification library`; a run under +`--force-full-verify` opens a cached entry the same way. An entry of a whole library holds about 1.2 GB; a closure entry holds only its modules. Every `verify`, `workspace` and `watch` process that reads an entry in place records its use in the entry directory's write time and holds a lock on a file beside the entry (`.use`) until it ends; the lock files are never deleted. A process that cannot create the lock file for want of permission runs without it. `LEANCIL_CACHE_DIR` should name an absolute directory that only -LeanCIL uses. +LeanCIL uses. On Windows Lake cannot read a file whose full path is longer than 259 characters, +so a run refuses a library, cached or supplied, that holds a compiled module file beyond that +length and names the file; for a whole library this limits the cache directory's path to about +120 characters. A run that publishes an entry removes cache directories that it judges unused, once, before it continues; there is no setting that turns this off. When the pass chooses any directory, fails or @@ -891,8 +1032,9 @@ Every invocation validates the request's identity and required proof evidence. A caches can reuse unchanged certified builds and an unchanged proof's result (see [local support cache](CONSUMERS.md#local-support-cache)); source and hash checks, audits and required kernel replay apply regardless. `--fresh`, `--rebuild-library`, `--force-full-verify` and -`LEANCIL_DISABLE_SUCCESS_CACHE=true` each bypass the reuse. A failed proof removes -the previous `verified.json`. +`LEANCIL_DISABLE_SUCCESS_CACHE=true` each bypass the reuse of support builds and proof results; +only `--rebuild-library` and CI also bypass the certified [library cache](#library-cache). A failed +proof removes the previous `verified.json`. When the assembly or the extractor changed since extraction, `verify` first extracts again from the settings `identity.json` records (assembly path, method, profile, assumptions, references and @@ -937,6 +1079,17 @@ Verified: ExampleProof.addTwo_holds its arguments or recurses is printed as Lean elaborates it, in terms of the auxiliary definitions it compiles to, and the field accessors of a structure the proof declares are not listed. Definitions of modules the proof imports are named in the statement and not printed. +- `verified.json` is the certificate, schema 7. Its `binding` section has the run's `mode`, the + `digests` file, statement `sources` and `images` the run read, and, as `facts`, every fact about + the assembly image that verification trusts, each with its `id`, `status`, `theorem` and + `reason`, in this order: `body-bytes`, `metadata-rows`, `platform-type-kinds`, + `enum-underlying-types`, `value-tuple-field-order`, `object-fields`, `layout-content`, + `dispatch-catalogs`, `static-data`, `generic-constraints`, `public-key-tokens` and + `import-identities`. `verify` writes `mode` `default`, reads no digest file, sources or image, + and lists every fact as `trusted`, with no theorem or reason. + `verify` accepts no certificate of another schema, mode or fact list, nor one with a missing, + repeated or reordered fact or an unknown status: a cached result whose certificate it refuses + is verified again. - `Lean messages` lists the informational and warning messages of the proof file, such as `#check`, `#eval` and `#print axioms` output, at `file:line:column` in the proof. The first 60 lines are shown; `proof-messages.txt` in the artifact directory holds the full text, and the @@ -994,7 +1147,7 @@ its result is cached apart from one without it. With `--format json` the goals a Method and call validation failures name the rejected item and the method containing it. A rejected call reports its callee, caller, IL offset and declaring assembly scope. For example, a -reachable `Environment.ProcessorCount` call is outside the admitted runtime models. Exact closed +reachable `System.Environment.ProcessorCount` call is outside the admitted runtime models. Exact closed `typeof` comparisons are supported; a `GetTypeFromHandle` call outside those patterns is unsupported. The diagnostic identifies metadata/CIL validation as the failing stage and links to the supported subset. @@ -1105,7 +1258,7 @@ output (for the last request of an output, as with `verified.json`) and as the | `unsolved-goal` | A Lean `unsolved goals` error, or a `verify --goals` run that stopped at the goals it found open | | `resource-limit` | Heartbeats (`(deterministic) timeout`, `(kernel) deterministic timeout`) or recursion depth; `resource` names which | | `replay-or-audit-rejected` | Kernel replay or the theorem and axiom audit rejected the proof | -| `refutation` | The theorem verified and states `Not (contract.Holds ...)` for a `MethodContract` or `OutcomeContract` | +| `refutation` | The theorem verified and states a contract's counterexample, `MethodContract.Refuted`, `OutcomeContract.Refuted` or `OutcomeContract.RefutedCLR` | | `invalid-request` | The proof or theorem selection is invalid | | `unclassified` | Anything else, such as a missing tool; read `message` | @@ -1115,19 +1268,29 @@ message block, so an internal error gives the request its kind. Lines are 1-base columns 0-based, as Lean prints them. Locations in the session's copies cite their originals, as in the text report. `message` is the original output, unmodified; `rerun` reruns just that request. Classification uses only the stage, LeanCIL's own messages and Lean's error prefixes -above. Only a verified theorem whose elaborated statement is `Not` applied to a contract's `Holds` -is a refutation, as reported by the theorem audit; an unsolved goal is never a refutation. A -verified refutation keeps its `verified.json`, the certificate of the refutation, and also -writes `failed.json` with kind `refutation`, located at the theorem's name. +above. Only a verified theorem whose elaborated statement is a contract's `Refuted` is a +refutation, as reported by the theorem audit; an unsolved goal is never a refutation. The audit +rejects a requested theorem that states `Not` applied to a contract's `Holds`, as kind +`replay-or-audit-rejected`: a run the model does not cover, such as an `unsupported` fault, proves +such a negation without any fault of the method. A lemma the requested theorem uses may state it. A +refutation names `Extracted.program`, directly or through definitions whose value is that constant, +and one of the extraction's methods, callees included; `verify` refuses any other subject at stage +`theorem audit`. A verified refutation keeps its `verified.json`, the certificate of the +refutation, whose `refutation` records the contract, the refuted method's signature with the +identity and SHA-256 of the assembly defining it, and the theorem's position. It also writes +`failed.json` with kind `refutation`, located at the theorem's name, whose `method` is that +method and assembly. ### Machine-readable verify output `leancil verify ... --format json` prints one JSON report on stdout instead of the text report, and nothing on stderr except [stage progress](#stage-progress); the exit code is the same as -without it. `--format text` is the default. The report has `schema` 1, `result` (`verified` or +without it. `--format text` is the default. The report has `schema` 2, `result` (`verified` or `failed`), the `stage` that failed, the `theorem`, `method`, `proof` and `output`, and on success the `axioms`, the `statement` and its `specification`, the definitions -[`Specification:`](#reading-the-report) prints, each with its `name` and `text`. The proof's own +[`Specification:`](#reading-the-report) prints, each with its `name` and `text`, and the +certificate's [`binding`](#reading-the-report) section; a run that issued no certificate has +`binding` `null` and says so in `unknown.binding`. The proof's own Lean messages are in `messages`, and `rerun` repeats the run. With `--goals`, `goals` lists each open goal with its `file`, `line`, `column`, `kind` (`sorry`, `failing tactic` or `unsolved`) and `goal`; without it `goals` is `null` and `unknown.goals` says why, as it does when the run stopped @@ -1182,7 +1345,9 @@ the pc, read from the extraction's `Symbols.lean` whether or not the proof imports `ExtractedSymbols`; an extraction without a matching PDB has no `Symbols.lean`, and `unknown` says so. `missing.shape` names the kind of fact the step lacks, such as `unsafeAdd`, `spanValidity` or `targetState`, `calleeContract` at a -`call` or `callvirt`, or on a class instance `objectForm`, `objectSlot` or +`call` or `callvirt` that enters a callee, the shape of its runtime model's step at a `call` +or `newobj` without a link, `linkOrModel` at a `call` or `newobj` with neither a link nor a +runtime model, or on a class instance `objectForm`, `objectSlot` or `objectAllocation`, and `residual` the terms it must prove: the step rewritten by the proof's step equations and the `_ = .ok _` and `_ = .error _` facts in scope. A side condition, such as an unfolded step a tactic could not close, has a shape when it names one @@ -1239,7 +1404,7 @@ library implementation proof source, artifact or full bundle is required. [Contract interfaces](CONTRACT-INTERFACES.md) gives the `extract` options and required pins, the sidecar schema and its checks, and the form the caller theorem must have. -A schema-4 `verified.json` contains `imports[]` with `acceptedVia` set to +A `verified.json` contains `imports[]` with `acceptedVia` set to `published-certification`, the implementation SHA-256, interface digest, certification theorem/statement/digest, `sidecarSha256`, `certificationAuthenticated` (always `false`: no pinned digest covers the certification text) and deployment @@ -1268,8 +1433,9 @@ Malformed documentation or stale extraction fails and removes an old lint report The report keeps potential modeled sites and XML declarations separate. It compares the selected entry's declarations with its profile-reachable method closure. It includes folded throws, integer divide/overflow, signed Abs, X86 -DivRem and span indexing faults. Clamp invalid bounds remain a model fault; -this experiment does not map them to a CLR exception. Memory faults, invalid +DivRem and span indexing faults. For an arithmetic or span fault, the site's origin includes the IL +offset, opcode and operand. A call operand names the overload. Clamp invalid bounds remain a model +fault without a CLR exception mapping. Memory faults, invalid states, unsupported operations and fuel exhaustion have no CLR name here. The lint is a conservative site analysis. It does not prove feasibility under a contract, exception conditions, or complete runtime behavior. diff --git a/docs/COMPILER-COVERAGE.md b/docs/COMPILER-COVERAGE.md index 1a88fddbb..843a16a10 100644 --- a/docs/COMPILER-COVERAGE.md +++ b/docs/COMPILER-COVERAGE.md @@ -7,7 +7,7 @@ extractor admits, and how to review and record a change to either. Read it when baselines fails. The `dispatch` suite checks a pinned source corpus against Roslyn 4.14.0, 5.0.0 and 5.9.0, -using the same .NET 10.0.0 reference pack and optimized compilation. Each compiler runs at its +using the same .NET 11.0.0 reference pack and optimized compilation. Each compiler runs at its default language version and at `latest`; compilers supporting C# 14 also run explicitly at `14`. The default column omits `/langversion`, so it tests the compiler default, not an SDK-selected language version. Compiler binaries @@ -82,7 +82,8 @@ The `blockers` suite also runs `leancil blockers` under the scalar profile over the extractor's own `Nethermind.LeanCIL.dll`, a realistic program that changes with every PR. [SelfAdmission.cs](../Tests/SelfAdmission.cs) generates the manifest from the built assembly's metadata and compares the report with the checked-in -[baseline](../Tests/SelfAdmissionBaseline.txt), the list of admitted methods. It needs no Lean. +[baseline](../Tests/SelfAdmissionBaseline.txt), the list of admitted methods. It needs no Lean; its +opt-in Lean check of every admitted method is described in [validation](VALIDATION.md). The gate checks admission and ignores blocker totals: it requires the admitted set to match the baseline exactly. A method the baseline admits that is no longer admitted, an admission the @@ -96,3 +97,18 @@ method it was generated for, one line per admitted method, since the compiler nu positions that unrelated edits shift. Gaining or losing one still changes its source method's count and fails the gate. Any change to a source compiled into the extractor, or to the baseline, selects the suite. + +[CanonicalMethods.cs](../Tests/CanonicalMethods.cs) then reads the same report and lists the +admitted methods the extraction defines from their decoder by `cil_decoded_method` rather than as +a literal. It prints how many of the methods extracted for admitted entries that is, and a table +that counts the other methods by their first live refusal: an opcode family that decodes to a hole +or a call translated to another instruction. It compares the list with the +checked-in [list](../Tests/canonical-methods.txt), named as the baseline names methods. A method +that leaves the list fails the scan; a method that joins it fails until +`dotnet run --file Tests/CanonicalMethods.cs -- --update` records it, so the list only grows. + +The same script runs `leancil blockers` over every method `leancil list` prints for the examples +assembly, under the `scalar`, `x64-sse42`, `x64-v3`, `x64-v4` and `arm64-advsimd` profiles at once, +and compares the methods each defines from its decoder, as ` ` lines, with a second checked-in +[list](../Tests/canonical-examples.txt), by the same rules. The runs only read the built extractor, and the check fails +if its hash changes while they run. diff --git a/docs/CONSUMERS.md b/docs/CONSUMERS.md index a1d677b7e..c9435fc16 100644 --- a/docs/CONSUMERS.md +++ b/docs/CONSUMERS.md @@ -146,7 +146,7 @@ included. `program` compares the method bodies of the variant's `Extracted.lean` with those of production's extraction, or of the `programOf` variant's, for the same gate and profile: `same` when the bodies -and the method signatures are equal, `renamed` when only the signatures differ, `different` +(for a method defined from its decoder, its raw definition) and the method signatures are equal, `renamed` when only the signatures differ, `different` otherwise. Assembly identities, digests, paths and metadata tokens are ignored. A comparison that does not hold fails the variant at stage `program comparison`, and its proof is not checked. @@ -167,23 +167,33 @@ proof. import Extracted import CIL.Safety.MethodContract open CIL.Safety -theorem Witness.refuted : ¬ ∀ (left right : CIL.W64) (memory : Memory), - ∃ fuel, invoke Extracted.program fuel 0 [.scalar (.i64 left), .scalar (.i64 right)] - memory = .ok (memory, [.scalar (.i64 (left + right))]) := by - intro holds - obtain ⟨fuel, ran⟩ := holds @LEFT@ @RIGHT@ Memory.empty - have returned := congrArg Prod.snd (invoke_ok_unique ran (rfl : invoke Extracted.program 16 0 - [.scalar (.i64 @LEFT@), .scalar (.i64 @RIGHT@)] Memory.empty = .ok _)) - revert returned - decide +/-- Production's contract: the wrapping sum, with caller memory unchanged. -/ +def Witness.contract : MethodContract (CIL.W64 × CIL.W64) where + args input := [.scalar (.i64 input.1), .scalar (.i64 input.2)] + pre _ _ := True + footprint _ := [] + post input initial final values := + final = initial ∧ values = [.scalar (.i64 (input.1 + input.2))] +/-- The witness inputs make the extracted method return other than their wrapping sum. -/ +theorem Witness.refuted : Witness.contract.Refuted Extracted.program 0 := + .of_return (input := (@LEFT@, @RIGHT@)) (initial := Memory.empty) trivial + (rfl : invoke Extracted.program 16 0 [.scalar (.i64 @LEFT@), .scalar (.i64 @RIGHT@)] Memory.empty = .ok _) + (by rintro ⟨-, out, rfl, -, wrong⟩; revert wrong; decide) ``` +Production's theorem states `MethodContract.Holds`, `OutcomeContract.Holds` or +`OutcomeContract.HoldsCLR` of a contract, and a refutation states the matching `Refuted` with +the same arguments: a definite counterexample, as in +[Refute a contract](PROOFS-CONTRACTS.md#refute-a-contract). A gate whose production theorem states +anything else has no refutation to compare, so each of its `rejected` variants fails at stage +`statement identity`. + A variant passes as follows: | `expect` | Passes when | | --- | --- | | `certifies` | Its proof verifies against its extraction, as for production, at production's statement unless `statement` is `independent`. | -| `rejected` | Its refutation verifies against its extraction, with the same kernel replay and axiom audit as any theorem, at the negation of production's statement, and its proof fails at proof checking. | +| `rejected` | Its refutation verifies against its extraction, with the same kernel replay and axiom audit as any theorem, at the `Refuted` form of production's contract statement, and its proof fails at proof checking. | | `unchanged` | Its program comparison holds. No proof is requested. | A build, extraction, program comparison or template failure, a missing proof, and a proof whose @@ -198,7 +208,8 @@ Production's theorem audit reports its statement `T` as a term in which each con the module that declares it, and a definition or theorem of production's own proof file is replaced by its value. Production verifies first; the variants compared with it verify in a second batch, where the theorem audit decodes `T` in the variant's replayed environment and the -kernel checks the variant's theorem, or its refutation, at `T` or `¬ T`. The check is +kernel checks the variant's theorem at `T`, or its refutation at `T` with the contract's `Holds` +replaced by its `Refuted`. The check is structural first and then definitional, so a variant may state `T` through definitions, such as `Extracted.entryIndex` for `0`, that unfold to the same term. A constant of `T` that the variant's environment lacks, or declares in another module, fails the check, and so does a check that @@ -646,15 +657,16 @@ leancil verify-batch requests.json [--check] [--fresh] [--rebuild-library] [--tr "proofRoot": ".", "requests": [ { "output": "artifacts/Add", "proof": "proofs/Add.lean", "theorem": "MyProof.correctness" }, - { "output": "artifacts/Mutant", "proof": "proofs/Refutation.lean", "theorem": "Mutant.refuted" }, - { "output": "artifacts/Mutant", "proof": "proofs/Add.lean", "theorem": "MyProof.correctness", "expect": "rejected" } + { "output": "artifacts/Mutant", "proof": "proofs/Refutation.lean", "theorem": "Mutant.refuted", + "expect": "rejected", "refutes": 0 } ] } ``` Paths are relative to the manifest. Each `output` is an extraction directory written by `leancil extract` and must lie outside the consumer sources (for example under `artifacts/`). -A request's optional `expect` is `verified` (the default) or `rejected`. Unknown manifest +A request's optional `expect` is `verified` (the default) or `rejected`; a `rejected` request +names in `refutes` the index, from 0, of a `verified` request of the same manifest. Unknown manifest properties are rejected. `verify-batch` does not extract again: an output whose assembly changed is reported as a stale input. @@ -682,9 +694,15 @@ expectation. workspace cleanup. Requests that share work can finish together, so do not sum these times as independent costs. Normal verification and compile checks report the same measure. -A request with `"expect": "rejected"` passes only when its proof fails during proof checking -with unchanged inputs and an intact extraction. It stops there, never writes a certificate, -removes any earlier `verified.json` and writes `failed.json` describing the rejection. +A request with `"expect": "rejected"` passes only when its theorem is a verified refutation of the +contract that the request it `refutes` proves: the counterexample predicate +(`MethodContract.Refuted`) of that request's `Holds` statement, accepted by the kernel and bound +to a method of the rejected request's own extraction. Requests it refutes run first, in the same +session. Its line reads `PASS rejected` with the refuted contract and method signature. A failed +extraction, a proof that does not build, a refutation of another contract, a theorem that states +no counterexample predicate or a target that did not verify fails it, and the `FAIL` line ends +with `no verified refutation of request N`. A failed refutation removes its `verified.json` and +writes `failed.json`. The session takes one source snapshot, restores certified support once and checks each extraction's integrity. Requests share work as follows: @@ -923,8 +941,8 @@ compose. An export that can run after its tables are loaded, including a repeated call, should take `StaticWorldValid published memory` as its precondition and restate it for the final memory in -its postcondition. `published` lists literal descriptors (identity, assembly identity, field -name and bytes) in an interface module. A published descriptor that differs from the extracted +its postcondition. `published` lists literal descriptors (identity, the field as the reading +instruction names it, and bytes) in an interface module. A published descriptor that differs from the extracted one leaves the author proof unprovable, so no separate equality audit is needed. `StaticIdentitiesAbsent` suits only a first-use contract: after the first call the field is loaded and the premise is false. diff --git a/docs/CONTRACT-INTERFACES.md b/docs/CONTRACT-INTERFACES.md index f9d0c8303..c2c267236 100644 --- a/docs/CONTRACT-INTERFACES.md +++ b/docs/CONTRACT-INTERFACES.md @@ -70,7 +70,7 @@ leancil resolve-imports bin/App.dll --library Lib \ | `--profile name` or `@profile.json` | The execution profile; see the architecture rule below. | | `--output file` | Write the result as JSON there. The default is standard output. | -Only `net10.0` targets are accepted. The command does not restore, use the network, or infer a +Only `net11.0` targets are accepted. The command does not restore, use the network, or infer a RID from the host. Selection follows the .NET 8+ host and NuGet. RID fallback is NuGet's breadth-first @@ -106,7 +106,7 @@ Required fields of a sidecar: - `frameworkKey`: SHA-256 of the sorted framework source manifest described below. - `assemblyIdentity`, `mvid`, `targetFramework`, `implementationSha256`, and `isReferenceAssembly`: exact implementation metadata and bytes. Target framework - is `.NETCoreApp,Version=v10.0`. Reference assemblies are authoring-only; caller proofs can be + is `.NETCoreApp,Version=v11.0`. Reference assemblies are authoring-only; caller proofs can be checked, but no successful verification report is published. - `interfaceModules`: module names and full Lean source strings. Names are plain dotted identifiers. Framework, toolchain, generated, example and test module @@ -236,7 +236,7 @@ an interface macro or elaborator cannot forge a closure. The compiled interface artifacts are hashed before replay and must keep those hashes after the audit and before each later step. Consumer workspaces also reject source-name collisions and include their hashes in snapshot checks. -Schema-4 reports list `imports` accepted via `published-certification`, as well +A `verified.json` certificate lists `imports` accepted via `published-certification`, as well as `contractImports`; CLI output states the conditional assumptions and deployment obligations. Hashes and certification records never discharge those assumptions. diff --git a/docs/EXAMPLES.md b/docs/EXAMPLES.md index ac227b564..f262a85e5 100644 --- a/docs/EXAMPLES.md +++ b/docs/EXAMPLES.md @@ -20,10 +20,13 @@ sentences name the wrong implementations that each unchanged proof rejects. - `Add`: universal modular arithmetic correctness in the specified empty initial memory. - `AddModel`: carries a theorem about a pure Lean model to the compiled `Add` through one theorem that the method returns the model's result, without re-proving the model theorem. -- `CheckedArithmetic.Add64`: universal checked addition for every pair whose sum fits, over - arbitrary caller memory, with exact memory preservation. The checked automation follows generated - instruction lookups; the same contract is tested after inserting NOPs and rejected for - subtraction. +- `DecodedAdd`: the `Add` contract for the program `Extraction.decodedProgram?` reads from the + extraction's body bytes, proved by evaluating the decoded method without the translator's output. + Its instruction-region proof states the stack result of `add` at pc 2 and preserves the frame and memory. +- `CheckedArithmetic.Add` and `Add64` return the unsigned sum whenever it fits and preserve + arbitrary caller memory. The 64-bit proof also holds with NOPs inserted before each instruction. + In each width, replacing addition with subtraction returns one for inputs two and one. The + verifier certifies that result as a counterexample to the contract, which requires three. - `Choose`: universal checked selection for both Boolean inputs and every pair of 64-bit values, preserving arbitrary caller memory exactly. - `GenericIdentity`: universal identity through direct, repeated and nested generic calls in C#, and @@ -35,6 +38,23 @@ sentences name the wrong implementations that each unchanged proof rejects. - `Loops.AllZero` and `Loops.Sum`: an early-exit predicate and a wrapping fold over the symbolic contents of an arbitrary-length `ReadOnlySpan` or `ReadOnlySpan`. - `Calls`: a managed-call chain with an address-taken mutable local. +- `CallsSummary`: `Calls` for every input over well-formed caller memory, returning `x + 3` and + keeping all memory below its entry, proved once by stepping `Increment` and the byref `Mutate` + and once from a `DecodedSummary` of each. +- `IdentitySummary` and `RepeatedSummary`: the identity of `GenericIdentity` for `Identity64` and + `Repeated`, proved once by stepping `Identity` and once from a `DecodedSummary` of it that + each call applies without stepping its body. Each file also proves that every call names the + metadata token and type arguments the summary is bound to + ([decoded summaries](PROOFS-CONTRACTS.md#bind-a-contract-to-a-decoded-method)). +- `CountCallsSummary`: `Loops.CountCalls` returns its input for every `uint`, proved once by + stepping `Decrement` at the call of each iteration and once from a `DecodedSummary` of + `Decrement` that `cil_loop_call` applies at that call. The file also proves that every summary + of the index the call enters is bound to the token of `Decrement`, and refutes one bound to + the token of `CountCalls`. +- `VectorSummary`: `DivisionIntrinsics.UnsafeLowNext` returns its input plus one for every + `ulong`, proved once by stepping `UnsafeLow`, whose `Vector128.CreateScalarUnsafe` and + `GetElement` calls run through their runtime models, and once from a `DecodedSummary` of + `UnsafeLow` that the call applies without stepping its body. - `IntegerOperations.Negate32` / `Negate64`: checked-execution proofs that negation returns the modular additive inverse for every input, including the signed minimum. - `NarrowIntegers`: signed/unsigned casts, character values, calls and an eight-byte struct @@ -54,7 +74,7 @@ sentences name the wrong implementations that each unchanged proof rejects. - `PastEnd`: a permitted end sentinel whose dereference is rejected as outside the allocation, for every fuel budget. - `Escape`: a reference to an expired local is rejected on return, for every fuel budget. -- `WrongAdd`: a mathematical counterexample to the intended addition contract. +- `WrongAdd`: a verified refutation of the addition contract at inputs `(0, 1)`. - A separate `Examples.Hardware` assembly, built from the file-based library `Examples/Hardware/Hardware.cs`, tests runtime capability extraction. - A Cecil-generated independent fixture tests extraction-bound static RVA bytes. @@ -108,8 +128,9 @@ sentences name the wrong implementations that each unchanged proof rejects. - `Guards` calls the framework argument guards. `GuardCount.lean` proves `ThrowIfNegative` raises `ArgumentOutOfRangeException` exactly for a negative count, `GuardIndex.lean` proves the unsigned `ThrowIfGreaterThanOrEqual` compares as unsigned, and `GuardRequired.lean` proves `ThrowIfNull` - raises `ArgumentNullException` exactly for null. The `guards` test shows `GuardCount.lean` - fails once the guard becomes `ThrowIfNegativeOrZero`. + raises `ArgumentNullException` exactly for null. Replacing the compiled guard with + `ThrowIfNegativeOrZero` raises `ArgumentOutOfRangeException` at zero. The refutation certificate + binds that counterexample to the changed entry and its assembly. Concrete example proofs establish their displayed inputs. Only explicitly universally quantified theorems claim arbitrary-input correctness. `CheckedAdd64`, `Choose` and `GenericIdentity` quantify @@ -154,7 +175,8 @@ and `cil_loop_call`, which names the returned count and keeps both locals of the across the call through `LoopFrame.after_confined`; `cil_loop_recurse` stores them and steps back to the head, and the invariant states only `count + remaining = value`. The suite verifies the unchanged loop proof against a build whose helper computes `value + 0xFFFFFFFF`, re-proving only -the helper contract, and rejects builds that discard the helper result or count twice. +the helper contract. It rejects a build that discards the helper result and refutes the counting +contract for a build that counts twice. [The struct-local loop proof](../Examples/Proofs/TallyTwice.lean) shows that `Loops.TallyTwice` returns twice its input for every `uint` and allocates nothing the caller sees. Its loop state is a @@ -235,8 +257,8 @@ leancil verify artifacts/FSharpAdd --proof Examples/Proofs/Add.lean ``` The theorem proves `(x + y) mod 2^64` for every pair of 64-bit inputs. The `fsharp` suite also -checks universal store/load and constructor contracts and rejects subtraction under the unchanged -addition contract. +checks universal store/load and constructor contracts. At inputs zero and one, the compiled +subtraction returns the largest unsigned word. The kernel checks this counterexample to addition. These examples cover F# programs that compile into the supported CIL subset. F# lists, closures, discriminated unions and arbitrary FSharp.Core calls are not generally supported; the suite diff --git a/docs/FULL_VERIFICATION.md b/docs/FULL_VERIFICATION.md index 9eb2f2430..9d5ed3437 100644 --- a/docs/FULL_VERIFICATION.md +++ b/docs/FULL_VERIFICATION.md @@ -85,8 +85,10 @@ privileges: macros, `initialize` and `run_cmd` can read and write any file the verifier can, start processes, and use the network. Hash checks do not restrict that code. The verifier snapshots the inputs, caller artifacts and module resolution once before and once after each run that elaborates producer-derived code. Caller -artifacts are compared by size and modification time rather than hashed. These -snapshots detect accidental mutation only; they omit the trusted toolchain. Run +artifacts are compared by SHA-256 content hash, so a same-length rewrite that +restores the modification time is detected. Producer-derived code can still change +a file and restore it between two checks, so these snapshots detect accidental +mutation only; they omit the trusted toolchain. Run full verification of an untrusted bundle in an isolated environment: a read-only toolchain, extractor and caller, no network, and a workspace discarded afterwards. @@ -99,9 +101,12 @@ requirement still applies to ongoing or hostile producer processes. Reports retain the original conditional caller theorem and add the composed theorem and audit, source bundle identities, and `acceptedVia: -independently-verified`. Faithful DLL decoding and CIL/runtime correspondence -remain trust boundaries. These checks establish CIL theorems. They do not establish -correctness of CoreCLR or a JIT. +independently-verified`. Lean decodes each admitted method and checks its operands against the +carried metadata. A reachable decoding refusal prevents certification. Full verification retains the +trust in the PE reader, platform definitions and runtime models described in [Soundness](SOUNDNESS.md). +To reconfirm each carried body and metadata slice against the library image, run +[`leancil verify --deep-checks`](CLI.md#deep-verification) with a sha256sum file naming the DLL and +every pinned reference. The certificate states the result for the CIL model. Imported libraries may depend on each other in any acyclic graph, such as a chain (a caller importing A, which imports B) or a diamond (a caller importing A and C, which both import B). A diff --git a/docs/GETTING-STARTED.md b/docs/GETTING-STARTED.md index 5eb609cc0..79dcba43b 100644 --- a/docs/GETTING-STARTED.md +++ b/docs/GETTING-STARTED.md @@ -47,6 +47,19 @@ Verified: ExampleProof.correctness Extraction entry: System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64) Proof: Examples/Proofs/Add.lean Axioms: propext, Quot.sound + Binding: default + body-bytes: trusted + metadata-rows: trusted + platform-type-kinds: trusted + enum-underlying-types: trusted + value-tuple-field-order: trusted + object-fields: trusted + layout-content: trusted + dispatch-catalogs: trusted + static-data: trusted + generic-constraints: trusted + public-key-tokens: trusted + import-identities: trusted Settings: artifacts/Add/verification.json Report: artifacts/Add/verified.json Statement: @@ -62,6 +75,10 @@ Verified: ExampleProof.correctness Examples/Proofs/Add.lean:15:0: info: 'ExampleProof.correctness' depends on axioms: [propext, Quot.sound] ``` +`Binding:` lists the facts this run trusts about the assembly image. With the assembly digests, +`verify --deep-checks` reconfirms `body-bytes`, `metadata-rows` and `static-data`. Supplying the +matching platform reference images also checks `object-fields`. See [deep verification](CLI.md#deep-verification). + `Subject:` is the method the theorem's conclusion is about and `Extraction entry:` the method the extraction started from; see [reading the report](CLI.md#reading-the-report). @@ -87,8 +104,8 @@ leancil check artifacts/Add ``` `Examples/Proofs/Add.lean` derives execution from the generated program for all inputs. -`Examples/Proofs/WrongAdd.lean` proves a counterexample to the addition contract for a subtracting -implementation, for every successful fuel budget. +`Examples/Proofs/WrongAdd.lean` proves the addition contract `Refuted` for a subtracting +implementation: at inputs `(0, 1)` it returns `2^64 - 1`, not `1`. ## Break the implementation diff --git a/docs/OPCODES.md b/docs/OPCODES.md index 7aa4993f6..404a930aa 100644 --- a/docs/OPCODES.md +++ b/docs/OPCODES.md @@ -13,27 +13,32 @@ opcode/operand combination has its own regression test, or that the model is equ Unsupported operand shapes can fail during checked execution even when translation succeeds. - Extraction rejects every opcode the table does not list. -Every row is subject to the common target restrictions: Release IL-only .NET 10 assemblies, +Every row is subject to the common target restrictions: Release IL-only .NET 11 assemblies, 64-bit little-endian execution, admitted types, no exception handlers and validated reachable control flow. Ordinary managed calls must have an acyclic extraction order; checked tail transfers may form cycles. A translated method still requires a proof of the desired property; exhausted fuel, invalid states and unsupported operations are failures, never implicit successes. Release configuration does not guarantee that a compiler emits only supported instructions: -reachable exception handling, for example, is rejected. +reachable exception handling, for example, is rejected. A runtime-async method, which carries +the `Async` implementation flag, is rejected as an entry and as a callee: its body returns the +awaited result and the runtime builds the declared task. ## Translated opcodes Ranges include every numbered form between the endpoints, and `.s` is the short encoding of the -same operation. Native `nint`/`nuint` values use `i64` on the validated 64-bit target. For mixed -native/int32 arithmetic and comparisons, extraction tracks stack types and emits an explicit -widening wrapper that sign-extends the int32 operand, except that unsigned checked arithmetic -zero-extends it; scalar `clt.un`/`cgt.un` sign-extend it too, matching the admitted runtime. The -wrapper preserves arithmetic faults and branch targets and cannot dispatch memory or calls. -Invalid native/int64 mixtures are rejected. Mixed native/int32 unsigned branches (`bne.un`, -`blt.un`, `bgt.un`, `ble.un`, `bge.un`, including short forms) are rejected because CoreCLR -widening depends on operand provenance; insert an explicit `conv.i` or `conv.u` to select -same-width semantics. The rows below describe scalar operands after this widening; other mixed -widths are unsupported. +same operation. Native `nint`/`nuint` values use `i64` on the validated 64-bit target. Extraction +tracks stack types and emits mixed native/int32 operations unchanged. For mixed wrapping +arithmetic, bitwise operations, comparisons and signed compare-and-branch, `CIL.step` +sign-extends the int32 operand before it evaluates the instruction (`CIL.widenMixed`), and the +checked interpreter reaches that widening only through `CIL.step`; scalar `clt.un`/`cgt.un` +sign-extend it too, matching the admitted runtime. Division and remainder sign-extend the int32 +operand; overflow-checked arithmetic sign-extends it for the signed forms and zero-extends it for +the unsigned forms. Memory operations and calls never widen their operands, and arithmetic faults +and branch targets are those of the same-width instruction. Invalid native/int64 mixtures are +rejected. Mixed native/int32 unsigned branches (`bne.un`, `blt.un`, `bgt.un`, `ble.un`, `bge.un`, +including short forms) are rejected because CoreCLR widening depends on operand provenance; insert +an explicit `conv.i` or `conv.u` to select same-width semantics. The rows below describe scalar +operands after this widening; other mixed widths are unsupported. `i32` and `i64` refer to the model's 32- and 64-bit integer stack values; managed references are separate values and are not integer addresses. @@ -46,12 +51,12 @@ external enum dependencies are not supported. | Opcodes | Status | Implemented behaviour and restrictions | | --- | --- | --- | -| `newarr` | Partial | Fresh zero-initialized primitive integer, same-assembly enum, validated reference-free aggregate and integer-vector arrays, including native integers, `bool` and `char`. Composite element sizes must be positive and fit the CoreCLR 16-bit component-size field; byref-like elements are rejected. Int32/native lengths must be nonnegative and at most the .NET 10 CoreCLR ceiling `0x7fffffc7`; negative lengths raise `System.OverflowException`. Identity and resource premises are in [arrays, views and bulk memory](SOUNDNESS-RUNTIME.md#arrays-views-and-bulk-memory). `newarr` of an admitted class, or of a non-generic class outside the model other than `object`, such as `string`, allocates an [array of class references](SOUNDNESS-RUNTIME.md#arrays-of-class-references) charged to the heap budget; a short budget is the model's `allocationFailure`. | +| `newarr` | Partial | Fresh zero-initialized primitive integer, same-assembly enum, validated reference-free aggregate and integer-vector arrays, including native integers, `bool` and `char`. Composite element sizes must be positive and fit the CoreCLR 16-bit component-size field; byref-like elements are rejected. Int32/native lengths must be nonnegative and at most the .NET 10 CoreCLR ceiling `0x7fffffc7`; negative lengths raise `System.OverflowException`. Identity and resource premises are in [arrays, views and bulk memory](SOUNDNESS-RUNTIME.md#arrays-views-and-bulk-memory). `newarr` of an admitted class or of `string` allocates an [array of class references](SOUNDNESS-RUNTIME.md#arrays-of-class-references) charged to the heap budget; a short budget is the model's `allocationFailure`. | | `ldlen` | Partial | Length of a supplied integer, enum, reference-free aggregate or integer-vector array, including one loaded from a [value-array field](SOUNDNESS-RUNTIME.md#value-array-fields), or typed handler table, returned as a native integer. Null raises `System.NullReferenceException`. Also the length of an [array of class references](SOUNDNESS-RUNTIME.md#arrays-of-class-references). | -| `ldelema` | Partial | Checked element reference into a supplied integer, enum, reference-free aggregate or integer-vector array. The type token must match the array element type; null and bounds checks precede address formation. Grants no read/write authority and does not make uninitialized contents readable. References retain allocation lifetime across locals, calls and returns. Rejected by name on an array of class references, whose element slots hold references. | -| `readonly.` | Partial | Immediately precedes an admitted `ldelema`; branches into the following address instruction are rejected. Produces a restricted reference that remains readonly through byref locals, arguments and returns. Scalar and field reads are allowed and field addresses and element or byte offsets keep the restriction, while stores, field stores and adapters that would discard the restriction fail. Other authorized aliases can still update the backing bytes. | +| `ldelema` | Partial | Checked element reference into a supplied integer, enum, reference-free aggregate or integer-vector array, decoded from the body with its type token as the element type; the method's checked layouts give the element's width. The type token must match the array element type; null and bounds checks precede address formation. Grants no read/write authority and does not make uninitialized contents readable. References retain allocation lifetime across locals, calls and returns. Rejected by name on an array of class references, whose element slots hold references. | +| `readonly.` | Partial | Immediately precedes an admitted `ldelema`, which records it; it decodes to `nop`, no other instruction decodes or is admitted after it, a body ending in it is rejected, and branches into the following address instruction are rejected. Produces a restricted reference that remains readonly through byref locals, arguments and returns. Scalar and field reads are allowed and field addresses and element or byte offsets keep the restriction, while stores, field stores and adapters that would discard the restriction fail. Other authorized aliases can still update the backing bytes. | | `ldelem.i1`, `ldelem.u1`, `ldelem.i2`, `ldelem.u2`, `ldelem.i4`, `ldelem.u4`, `ldelem.i8`, `ldelem.i`, `ldelem` | Partial | Checked integer array reads; narrow loads extend according to the opcode. Typed `ldelem` also supports validated aggregate and integer-vector tokens, matching the exact array element identity and width; it is rejected by name on an array of class references. Scalar reads require initialized bytes; aggregate/vector snapshot reads retain initialization masks. All reads require read authority. `ldelem.i` and typed `ldelem` also select typed entries from supplied handler tables. | -| `ldelem.ref`, `stelem.ref` | Partial | Element loads and stores of an [array of class references](SOUNDNESS-RUNTIME.md#arrays-of-class-references). Null raises `System.NullReferenceException`, then an index read unsigned at or past the length `System.IndexOutOfRangeException`; a store then raises `System.ArrayTypeMismatchException` when the stored instance's class does not derive from the element class, and null is always storable. Elements of an array of a class outside the model are opaque references. Storing a value whose runtime type is not a class, such as an array, is unsupported. | +| `ldelem.ref`, `stelem.ref` | Partial | Element loads and stores of an [array of class references](SOUNDNESS-RUNTIME.md#arrays-of-class-references). Null raises `System.NullReferenceException`, then an index read unsigned at or past the length `System.IndexOutOfRangeException`; a store then raises `System.ArrayTypeMismatchException` when the stored instance's class does not derive from the element class, and null is always storable. Elements of a `string` array are opaque references. Storing a value whose runtime type is not a class, such as an array, is unsupported. | | `stelem.i1`, `stelem.i2`, `stelem.i4`, `stelem.i8`, `stelem.i`, `stelem` | Partial | Checked integer array stores; narrow stores truncate. Typed `stelem` requires a matching integer, enum, aggregate or integer-vector token and is rejected by name on an array of class references. Stores require write authority; scalar stores initialize the selected element, while aggregate/vector stores preserve snapshot initialization masks. | | `ldc.i4`, `ldc.i4.s`, `ldc.i4.m1`, `ldc.i4.0` to `ldc.i4.8` | Complete within the admitted model | Push the encoded 32-bit integer constant. | | `ldc.i8` | Complete within the admitted model | Push the encoded 64-bit integer constant. | @@ -59,19 +64,19 @@ external enum dependencies are not supported. | `ldstr` | Partial | Push the object the caller bound to the literal's text in `Memory.literals`; an unbound text stops as unsupported. Memory is unchanged. The value is an opaque [string](SOUNDNESS-RUNTIME.md#string-literals): passed, stored, compared for identity and tested against null, never read. Inside a folded `throw new E(...)` it emits `nop`. A text containing a surrogate, backspace or form feed is rejected. | | `nop` | Complete within the admitted model | Advance without changing the stack or memory. | | `dup`, `pop` | Complete within the admitted model | Duplicate or discard a supported stack value; stack underflow fails. | -| `ldarg`, `ldarg.s`, `ldarg.0` to `ldarg.3` | Partial | Load admitted argument values; struct instance methods have a managed-reference `this` at index zero, and instance methods of admitted classes an object-reference `this`; a `string` argument is an opaque object reference (see [class instances](SOUNDNESS-RUNTIME.md#class-instances)); narrow integers are truncated and sign- or zero-extended to `i32`. Reference values undergo validity checks. | +| `ldarg`, `ldarg.s`, `ldarg.0` to `ldarg.3` | Partial | Load admitted argument values; struct instance methods have a managed-reference `this` at index zero, and instance methods of admitted classes an object-reference `this`; a `string` argument is an opaque object reference (see [class instances](SOUNDNESS-RUNTIME.md#class-instances)); the step truncates a parameter declared as a narrow integer, or as an enum over one, to that width and sign- or zero-extends it to `i32`, reading the width from the method's signature. Reference values undergo validity checks. | | `starg`, `starg.s` | Partial | Assign a private parameter home using its declared storage type. Narrow stores truncate; byref assignment changes the private reference slot. Does not overwrite the caller's by-value source. | -| `ldarga`, `ldarga.s` | Partial | Address the same private parameter home read by `ldarg` and written by `starg`. Addresses expire when the frame returns. Taking the address of a byref parameter is rejected; nested-reference storage is unsupported. | +| `ldarga`, `ldarga.s` | Partial | Address the same private parameter home read by `ldarg` and written by `starg`; for a span parameter, push a home that names the parameter's slot, whose view each consumer reads when it runs. Addresses expire when the frame returns. Taking the address of a byref parameter is rejected; nested-reference storage is unsupported. | | `ldloc`, `ldloc.s`, `ldloc.0` to `ldloc.3` | Partial | Load admitted scalar, vector, aggregate or managed-reference locals. Narrow integers are sign- or zero-extended to `i32`; initialization and lifetime rules apply. | | `stloc`, `stloc.s`, `stloc.0` to `stloc.3` | Partial | Store values compatible with the declared local storage kind; narrow integer locals truncate an `i32` to their physical width. No general object or reference-bearing aggregate storage. | -| `ldloca`, `ldloca.s` | Partial | Form a checked reference to byte-backed local storage. Taking the address of a managed-reference local is explicitly unsupported. | +| `ldloca`, `ldloca.s` | Partial | Form a checked reference to byte-backed local storage; for a span local, push a home that names the slot, whose view each consumer reads when it runs, so a later store to the local is visible through the address. Taking the address of a managed-reference local is explicitly unsupported. | | `neg` | Partial | Wrapping negation of an `i32` or `i64`; the signed minimum maps to itself. No floating-point or reference operands. | | `add` | Partial | Wrapping arithmetic on two `i32` values or two `i64` values, plus the mixed native/int32 cases described above. Also accepts a checked reference and an int32 or native integer byte offset in either operand order, preserving allocation identity and its readonly restriction and checking lifetime and bounds when forming the result. Supplied immutable native handler tables also admit checked byte-offset addition. No floating-point operands or arbitrary numeric address fabrication. | | `sub` | Partial | Wrapping scalar subtraction; also subtract an int32/native byte offset from a checked reference, preserving its readonly restriction, or compute the native byte difference of two live references within one allocation. Null, cross-allocation differences and invalid resulting positions fail. | | `mul` | Partial | Wrapping arithmetic on two `i32` values or two `i64` values, plus the mixed native/int32 cases described above. No floating-point or reference/pointer operands. | -| `div`, `div.un`, `rem`, `rem.un` | Partial | Same-width `i32`/`i64` quotient or remainder. Signed quotient truncates toward zero; signed remainder follows the dividend sign. Zero divisors fault. Signed minimum with divisor -1 faults for both signed operations under the admitted runtime assumption. No floating-point, mixed-width operands or exception handlers. | -| `add.ovf`, `sub.ovf`, `mul.ovf`, `add.ovf.un`, `sub.ovf.un`, `mul.ovf.un` | Partial | Checked signed/unsigned arithmetic on same-width `i32`/`i64`. Compute the mathematical integer result, then reject values outside the destination range with a located overflow fault. No floating-point, reference arithmetic or exception handlers. | -| `and`, `or`, `xor`, `not` | Partial | Bitwise operations on `i32`/`i64`; binary operands must have the same width. | +| `div`, `div.un`, `rem`, `rem.un` | Partial | Same-width `i32`/`i64` quotient or remainder. Signed quotient truncates toward zero; signed remainder follows the dividend sign. Zero divisors fault. Signed minimum with divisor -1 faults for both signed operations under the admitted runtime assumption. Also the mixed native/int32 cases described above. No floating-point operands, other mixed widths or exception handlers. | +| `add.ovf`, `sub.ovf`, `mul.ovf`, `add.ovf.un`, `sub.ovf.un`, `mul.ovf.un` | Partial | Checked signed/unsigned arithmetic on same-width `i32`/`i64`, plus the mixed native/int32 cases described above. Compute the mathematical integer result, then reject values outside the destination range with a located overflow fault. No floating-point, reference arithmetic or exception handlers. | +| `and`, `or`, `xor`, `not` | Partial | Bitwise operations on `i32`/`i64`. `and`, `or` and `xor` also take the mixed native/int32 cases described above, sign-extending the int32 operand; `not` has one operand. No other mixed widths. | | `shl`, `shr`, `shr.un` | Partial | Shift an `i32`, `i64` or native integer by an `i32` or native integer count, masked modulo the value width under the admitted runtime. `shr` is arithmetic; `shr.un` is logical. Native counts normalize to their low 32 bits; ordinary `int64` counts and reference operands are rejected by extraction and its certificate. | | `conv.ovf.i1`, `conv.ovf.u1`, `conv.ovf.i2`, `conv.ovf.u2`, `conv.ovf.i4`, `conv.ovf.u4`, `conv.ovf.i8`, `conv.ovf.u8`, `conv.ovf.i`, `conv.ovf.u`, and each corresponding `.un` form | Partial | Checked conversion from `i32`/`i64`, preserving the mathematical value or producing a located overflow fault. `.un` interprets the input as unsigned; the destination controls the accepted range. Narrow results occupy `i32`; native destinations use the validated 64-bit profile. No floating-point or reference conversions. | | `conv.i4`, `conv.u4` | Partial | `i64` to low 32 bits; `i32` identity. No floating-point or reference conversions. | @@ -82,45 +87,51 @@ external enum dependencies are not supported. | `conv.i8` | Partial | Sign-extend `i32` to `i64`, or preserve an `i64`. No floating-point or reference conversions. | | `conv.i` | Partial | Sign-extend an integer `i32` to the admitted 64-bit native width, or preserve an `i64`. No pointer/reference or floating-point conversion. | | `conv.u` | Partial | Model native unsigned integers as 64-bit: zero-extend `i32`, or preserve `i64`. Does not convert a managed reference to an integer address. | -| `clt`, `clt.un`, `cgt`, `cgt.un`, `ceq` | Partial | Same-width `i32`/`i64` comparisons, returning `i32` zero or one. `clt` and `cgt` are signed; `.un` comparisons are unsigned. `ceq` additionally supports checked reference equality, identity of class instance references and exact closed `typeof` reference results as described below; `cgt.un` tests a class instance reference against null; `clt.un` and `cgt.un` additionally order live references within one allocation. Signed reference ordering is unsupported. No floating-point/unordered comparisons. | +| `clt`, `clt.un`, `cgt`, `cgt.un`, `ceq` | Partial | Same-width `i32`/`i64` comparisons, returning `i32` zero or one, plus the mixed native/int32 cases described above, which sign-extend the int32 operand for all five instructions including `clt.un` and `cgt.un`. `clt` and `cgt` are signed; `.un` comparisons are unsigned. `ceq` additionally supports checked reference equality, identity of class instance references and exact closed `typeof` reference results as described below; `cgt.un` tests a class instance reference against null; `clt.un` and `cgt.un` additionally order live references within one allocation. Signed reference ordering is unsupported. No floating-point/unordered comparisons. | | `br`, `br.s` | Partial | Unconditional branch to an admitted instruction boundary, including backward edges and self-loops. | | `switch` | Partial | Pop an `i32` unsigned table index; branch to its target or fall through when outside the table. Empty and duplicate-target tables are supported. Every encoded target must be a valid instruction boundary and fold entry. Known int32 selectors prune untaken arms; otherwise every arm and the fallthrough participate in dependency and stack analysis. | | `sizeof` | Partial | Push the validated storage width as `i32` for admitted integer types (including Boolean/char), same-assembly explicit- or sequential-layout reference-free aggregates, or the integer vectors listed below. Native integers use eight bytes under the validated profile. Closed same-assembly generic structs require validated sequential layouts. No reference types, floating-point or unsupported generic layouts. | | `brfalse`, `brfalse.s`, `brtrue`, `brtrue.s` | Partial | Zero/nonzero tests of `i32` or `i64`. Also accepts checked references, including readonly references and null. Admitted forward and backward successors. | -| `blt`, `blt.s`, `bge`, `bge.s`, `bgt`, `bgt.s`, `ble`, `ble.s` | Partial | Signed comparison of same-width `i32`/`i64`, branching to admitted forward or backward targets. | +| `blt`, `blt.s`, `bge`, `bge.s`, `bgt`, `bgt.s`, `ble`, `ble.s` | Partial | Signed comparison of same-width `i32`/`i64`, plus the mixed native/int32 cases described above, which sign-extend the int32 operand, branching to admitted forward or backward targets. | | `blt.un`, `blt.un.s`, `bge.un`, `bge.un.s`, `bgt.un`, `bgt.un.s`, `ble.un`, `ble.un.s` | Partial | Unsigned comparison of same-width `i32`/`i64` or two live references within one allocation, branching to admitted forward or backward targets. Cross-allocation reference ordering is unsupported. | -| `beq`, `beq.s`, `bne.un`, `bne.un.s` | Partial | Equality/inequality of same-width `i32`/`i64`, branching to admitted forward or backward targets. Also supports checked reference equality and exact closed `typeof` reference comparisons as described below. No floating-point comparison. | +| `beq`, `beq.s`, `bne.un`, `bne.un.s` | Partial | Equality/inequality of same-width `i32`/`i64`, branching to admitted forward or backward targets. `beq` and `beq.s` also take the mixed native/int32 cases described above, sign-extending the int32 operand; `bne.un` and `bne.un.s` reject a mixed pair. Also supports checked reference equality and exact closed `typeof` reference comparisons as described below. No floating-point comparison. | | `ldind.i`, `stind.i` | Partial | Native-width integer load/store through checked managed references; eight bytes on the admitted target, with the same allocation, permission and lifetime checks. A native handler-table cursor also supports authenticated pointer loads through `ldind.i`; stores to these immutable tables are unsupported. | | `ldind.i8`, `stind.i8` | Partial | Eight-byte integer load/store through a checked reference. Allocation bounds, permissions, initialization and lifetime rules apply; arbitrary numeric addresses are not accepted. | | `ldind.i1`, `ldind.u1`, `ldind.i2`, `ldind.u2`, `ldind.i4`, `ldind.u4` | Partial | One-, two- or four-byte integer loads through a checked reference. Signed narrow loads sign-extend and unsigned loads zero-extend to `i32`; both four-byte forms return the same `i32` bits. Aggregate snapshots and numeric addresses are rejected. | | `stind.i1`, `stind.i2`, `stind.i4` | Partial | Store the low one, two or four bytes of an `i32` through a checked reference. Allocation bounds, write permissions and lifetime apply; adjacent bytes remain unchanged. | -| `ldfld`, `ldflda`, `stfld` | Partial | Fields in same-assembly, validated explicit- or sequential-layout, reference-free value types, and `Item1`/`Item2` of an equal-width primitive `ValueTuple` (see [value tuples](SOUNDNESS-RUNTIME.md#value-tuples)). Supported field storage is `sbyte`, byte/Boolean, `short`, `ushort`/`char`, 32-bit integer, 64-bit integer, native integer, integer-backed enum or nested reference-free aggregate. Nested snapshots and assignments copy all field bytes, including padding. Narrow stores truncate; narrow loads sign- or zero-extend to `i32` according to field metadata. `ldfld` accepts an aggregate byte snapshot or reference, including a readonly reference; `ldflda` of a readonly reference yields a readonly field reference, and `stfld` through one faults as unwritable. On an admitted [class instance](SOUNDNESS-RUNTIME.md#class-instances), `ldfld` and `stfld` access integer, Boolean, Char and enum fields of its class or any base, fields typed as a class or an array of a class (opaque when the class is outside the model), and [value-array fields](SOUNDNESS-RUNTIME.md#value-array-fields) typed as a single-dimensional array of integers, Booleans, chars, enums, reference-free structs or integer vectors; a null receiver throws `NullReferenceException`. `ldflda` on a class and other class field types are rejected. No other managed objects, auto-layout structs or static fields through these opcodes. | -| `ldobj`, `stobj` | Partial | Load/store admitted integer types through checked references, with narrow truncation/extension derived from the validated type token. Also copy validated aggregates of 1 to 65,536 bytes or admitted Vector128/256/512 integer values, retaining initialization masks. No general objects, reference-bearing storage or floating-point tokens. Native-integer tokens use eight-byte storage. `ldobj` of an admitted span type copies the view addressed by the immediately preceding argument, parameter-address or local-address load; span `stobj` is rejected. | +| `ldfld`, `ldflda`, `stfld` | Partial | Fields in same-assembly, validated explicit- or sequential-layout, reference-free value types, and `Item1`/`Item2` of an equal-width primitive `ValueTuple` (see [value tuples](SOUNDNESS-RUNTIME.md#value-tuples)). Supported field storage is `sbyte`, byte/Boolean, `short`, `ushort`/`char`, 32-bit integer, 64-bit integer, native integer, integer-backed enum or nested reference-free aggregate. Nested snapshots and assignments copy all field bytes, including padding, and a nested struct loaded through a reference keeps uninitialized bytes such as padding as a snapshot with their flags. A scalar field of a snapshot loads when its own bytes are initialized and is `unsupported` otherwise. Narrow stores truncate; narrow loads sign- or zero-extend to `i32` according to field metadata. `ldfld` accepts an aggregate byte snapshot or reference, including a readonly reference; `ldflda` of a readonly reference yields a readonly field reference, and `stfld` through one faults as unwritable. On an admitted [class instance](SOUNDNESS-RUNTIME.md#class-instances), `ldfld` and `stfld` access integer, Boolean, Char and enum fields of its class or any base, fields typed as a class (opaque when the class is outside the model) or as an array of an admitted class or `string`, and [value-array fields](SOUNDNESS-RUNTIME.md#value-array-fields) typed as a single-dimensional array of integers, Booleans, chars, enums, reference-free structs or integer vectors; a null receiver throws `NullReferenceException`. `ldflda` on a class, and a class field of any other type, are refused at admission and fault as `unsupported` when stepped. No other managed objects, auto-layout structs or static fields through these opcodes. | +| `ldobj`, `stobj` | Partial | Load/store admitted integer types through checked references, with narrow truncation/extension derived from the validated type token. Also copy validated aggregates of 1 to 65,536 bytes or admitted Vector128/256/512 integer values, retaining initialization masks. No general objects, reference-bearing storage or floating-point tokens. Native-integer tokens use eight-byte storage. `ldobj` of an admitted span type copies the view addressed by the immediately preceding argument, parameter-address or local-address load, reading a local or parameter home when the load runs; it faults on any operand other than a span view, a null reference with `nullDereference`; span `stobj` is rejected. | | `cpobj` | Partial | Copy validated integer, aggregate or admitted Vector128/256/512 integer storage through checked references. Requires initialized readable source and writable destination; self-copy is supported, partial overlap is rejected. No reference-bearing or floating-point tokens. | | `initobj` | Partial | Zero the validated width of an admitted integer type, same-assembly explicit- or sequential-layout aggregate, or admitted Vector128/256/512 integer value. A checked `ldloca; initobj` pair resets an admitted span local to its empty view. Byte initialization leaves neighboring storage unchanged. No managed-object initialization. | | `ldsfld` | Partial | Value of a same-assembly `static readonly` integer, enum or reference-free struct field that its static constructor fixes, with the declaring type named by `--assume-initialized` (see [static readonly values](#static-readonly-values)). An integer or enum field whose stored value extraction cannot compute is read from the caller's static binding instead (see [unknown readonly values](#unknown-readonly-values)). System.Runtime's `BitConverter.IsLittleEndian` is the profile constant `true`. Other static fields are rejected. | | `ldsflda` | Partial | Address of validated same-assembly static readonly RVA data, or a constructor-initialized readonly value under the initialization assumption. Uses identity-keyed immutable storage with checked write denial. See [static span tables](#static-span-tables) and [static readonly values](#static-readonly-values). Not arbitrary static fields or mutable global state. | | `call` | Partial | Acyclic calls to supported static methods, reference-free struct instance methods (including direct constructor calls) or instance methods and constructors of admitted classes, which run the named body on an object-reference `this` ([class instances](SOUNDNESS-RUNTIME.md#class-instances)), in the same assembly or a pinned `--reference` assembly ([CLI](CLI.md#extract-from-referenced-assemblies)), concrete generic-method specializations, or exact runtime/intrinsic signatures in the allowlist, including integer BinaryPrimitives byte-span transfers and no-read span/array accessors. Closed generic declaring types are admitted for static calls, including nested owners; validated closed sequential structs also admit direct instance methods and constructors. No arbitrary external calls, instance methods of classes that are not admitted or virtual dispatch through `call`. | -| `newobj` | Partial | Same-assembly constructors of validated reference-free structs: zero-initialize caller-owned storage, invoke the constructor with managed-reference `this`, then return a checked value snapshot. Also admitted `Span` and `ReadOnlySpan` constructors over checked existing storage for supported integer, enum, aggregate and integer-vector elements, from a single reference, `(void*, int)`, an array, or `(array, start, length)`. Also constructors of admitted non-abstract classes, which allocate a fresh [class instance](SOUNDNESS-RUNTIME.md#class-instances) charged to `Memory.heapBudget` (`allocationFailure` when short) and push it once the constructor returns; the constructor's `System.Object::.ctor()` call only pops its receiver. Unsupported generic constructors and cyclic dependencies are rejected. | -| `ldtoken` | Partial | A type token as an operand of a decided closed type test, which emits a no-op, or a static data field token for `RuntimeHelpers.CreateSpan`, which emits the field's address (see below). Other tokens are rejected. | -| `throw` | Partial | Exact `throw new E(...)` with `ldstr`/`ldnull` arguments raises a located `thrown "E"` fault. Adjacent `ldnull; throw` raises `System.NullReferenceException`. Other throws are rejected. | +| `newobj` | Partial | Same-assembly constructors of validated reference-free structs: zero-initialize caller-owned storage, invoke the constructor with managed-reference `this`, then return a checked value snapshot. An unlinked `newobj` runs the exact external constructor model and pushes its result. This covers `Span` and `ReadOnlySpan` constructors over checked existing storage for supported integer, enum, aggregate and integer-vector elements, from a single reference, `(void*, int)`, an array, or `(array, start, length)`. Also constructors of admitted non-abstract classes, which allocate a fresh [class instance](SOUNDNESS-RUNTIME.md#class-instances) charged to `Memory.heapBudget` (`allocationFailure` when short) and push it once the constructor returns; the constructor's `System.Object::.ctor()` call runs a runtime model that only consumes its receiver. Unsupported generic constructors and cyclic dependencies are rejected. | +| `ldtoken` | Partial | A type token pushes its scoped runtime handle in a decided closed type test. A static data field token supplies the field address for `RuntimeHelpers.CreateSpan` (see below). Other tokens are rejected. | +| `throw` | Partial | Exact `throw new E(...)` with `ldstr`/`ldnull` arguments raises a located `.thrown raised` fault, where `raised` is the `CIL.ExceptionType` member for `E`. Adjacent `ldnull; throw` raises `.nullReference`. Other throws are rejected. | | `constrained.`, `callvirt` | Partial | Adjacent prefix and `callvirt` (instance slot) or `call` (static interface slot) resolving to a concrete struct implementation or, for a static slot, the most specific interface-level body, within the extracted and pinned assemblies; see [constrained struct calls](#constrained-struct-calls). No boxing. Without the prefix, `callvirt` of a non-virtual method of an admitted class calls it after a receiver null check that throws `System.NullReferenceException` ([class instances](SOUNDNESS-RUNTIME.md#class-instances)); `callvirt` of a virtual method of an admitted class dispatches on the receiver's runtime type through a table Lean rechecks ([virtual dispatch](SOUNDNESS-EXECUTION.md#virtual-dispatch)). Interface calls and slots declared outside the extracted assemblies are rejected by name. | -| `castclass`, `isinst` | Partial | Test an object reference against an admitted class other than `System.Object`: null passes, and an instance whose lineage lacks the class gives null (`isinst`) or throws `System.InvalidCastException` (`castclass`); see [class instances](SOUNDNESS-RUNTIME.md#class-instances). Interfaces, `System.Object`, value types and classes that are not admitted are rejected by name. | +| `castclass`, `isinst` | Partial | Test an object reference, or an opaque reference a field typed `object` or as a generic class instance held, against an admitted class other than `System.Object`: null passes, and an instance whose lineage lacks the class gives null (`isinst`) or throws `System.InvalidCastException` (`castclass`); see [class instances](SOUNDNESS-RUNTIME.md#class-instances). Interfaces, `System.Object`, value types and classes that are not admitted are rejected by name. | | `ldftn` | Partial | Exact static same-assembly managed method addresses, retaining scoped identity and closed signature. Callable values have no numeric address representation. See typed indirect dispatch below. | | `calli` | Partial | Static default-managed signatures over admitted integer/enum, reference-free aggregate and integer-vector values, and managed references to these types. The pointer must identify an authenticated catalog entry with the exact signature. Ordinary indirect calls select a forward target; tail calls permit cycles. Supplied immutable native tables also admit checked byte offsets and `ldind.i` with authenticated target provenance. No unmanaged conventions or arbitrary numeric targets. | -| `tail.` | Partial | Adjacent `tail. call ret` or `tail. calli ret` with exactly its arguments and optional function pointer on the stack and the same return type. Retires caller storage before entering the callee and revalidates argument lifetimes. Admitted same-assembly transfers may form cycles; virtual, imported and intrinsic tail calls are unsupported. | +| `tail.` | Partial | Every tail call must return the enclosing method's result type and immediately precede `ret`. Reachable `call` and `calli` transfers take exactly their arguments and optional function pointer, retire caller storage and revalidate argument lifetimes. Admitted same-assembly transfers may form cycles. A reachable virtual, imported or intrinsic tail call is unsupported. An unreachable `tail. callvirt ret` with an instance target is admitted with a hole at each dead instruction. | | `ret` | Partial | Return from an admitted method with exactly zero or one result according to its signature. Narrow integer results are truncated and sign- or zero-extended to `i32`. The checked model validates returned references and rejects references that escape expired local storage. | -| `initblk`, `cpblk` | Partial | Managed references and unsigned 32-bit counts; checked block alignment and access rules. Nonempty overlapping copies are rejected. | -| `unaligned.` | Partial | Values 1, 2 and 4 before supported ordinary or block memory accesses; malformed prefix sequences and interior branch targets are rejected. | +| `initblk`, `cpblk` | Partial | The decoded `initblk` and `cpblk` instructions carry the alignment of an immediately preceding `unaligned.` prefix, or eight without one. Execution checks managed references, the unsigned 32-bit count, alignment and access. Nonempty overlapping copies are rejected. | +| `unaligned.` | Partial | Values 1, 2 and 4 before supported ordinary or block memory accesses, where it decodes to `nop`; any other value, an instruction it does not modify, a body ending in it, malformed prefix sequences and interior branch targets are rejected. | | `localloc` | Partial | Frame-owned storage within an explicit per-activation quota; checked initialization, alignment and expiry. See [dynamic stack allocation](#dynamic-stack-allocation). | ## Typed indirect dispatch Managed function pointers can be held in typed locals or supplied as parameters. Supplied -immutable single-dimensional handler tables support `ldlen` and `ldelem.i`; each entry has one +immutable single-dimensional handler tables support `ldlen`, `ldelem.i` and `ldelem` of the table's +own pointer type; each entry has one exact signature and must name a non-null catalog target. A null table raises `NullReferenceException`, and an invalid index raises `IndexOutOfRangeException`. +An `ldftn` instruction carries the metadata identity and signature. Its address link records +where the extractor checked that identity. Taking an address adds no callee to program closure. +A `calli` instruction carries its signature and tail flag. Its indirect link supplies the catalog. +Relocation updates the indices of callable targets and preserves the address link's extraction index. + The catalog holds closed same-assembly `ldftn` targets and already extracted methods with matching signatures. An open address such as `&Algo.Run` in a generic body is catalogued only through a specialized body that takes it, so a supplied table can reach a generic target @@ -153,8 +164,8 @@ fresh allocation-relative storage owned by the current activation. The caller su request charges its size rounded to 16 bytes. Insufficient quota produces a distinct `stackAllocationFailure`; a zero request returns null and consumes no quota. -The raw method's `localsinit` flag controls zero initialization. Otherwise bytes remain unreadable -until written. Checked pointer offsets, typed loads and stores, and admitted span construction +The method's `localsinit` flag, which the extracted body records as `DeclaredSignature.initLocals`, +controls zero initialization. Otherwise bytes remain unreadable until written. Checked pointer offsets, typed loads and stores, and admitted span construction retain allocation bounds and authority. Returning a reference or span into expired stack storage fails. Numeric address fabrication and general pointer casts are unsupported. @@ -176,10 +187,14 @@ as a managed pointer. `Unsafe.NullRef` values are managed references and can reference comparisons. Numeric zero never becomes a reference. The managed-reference overloads of `Unsafe.AreSame`, `IsAddressLessThan` and -`IsAddressGreaterThan` use these checked comparisons. Ordering and -`Unsafe.ByteOffset(origin, target)` require two live references in the same allocation; -`Unsafe.ByteOffset` returns the native-width byte displacement from origin to target. -`Unsafe.AddByteOffset` and `SubtractByteOffset` accept `nint` or `nuint` offsets. +`IsAddressGreaterThan` use these checked comparisons; a call keeps its `call`, which runs the +comparison the runtime model table gives its exact closed reference. Ordering and +`Unsafe.ByteOffset(origin, target)` require two live references in the same allocation. +The retained `Unsafe.ByteOffset` call runs the runtime model for its exact generic declaration +and returns the native-width byte displacement from origin to target. +The retained `Unsafe.AddByteOffset` and `SubtractByteOffset` calls move a reference by a +`nint` or `nuint` byte count. The retained `Unsafe.Add` and `Subtract` calls use the checked +element size as their stride. Each overload binds its own open declaration and external target. `Unsafe.Subtract` accepts `int`, `nint` or `nuint` element offsets for admitted scalar, aggregate and vector storage. Scaling and subtraction wrap at the native 64-bit width, and the resulting reference must still be live and in bounds. These operations grant no read or write @@ -203,18 +218,21 @@ memory and permit null references. The lifetime, bounds and authority checks are Unprefixed block operations require eight-byte alignment. Model-owned 32-bit and 64-bit scalar local and parameter homes have four- and eight-byte alignment, so aligned block access can succeed on a 64-bit scalar home; aggregate, vector and narrow homes have byte alignment. An -immediately preceding `unaligned. 1`, `2` or `4` selects that requirement. A branch cannot enter -between a prefix and its instruction, and prefix combinations with unsupported modifiers are -rejected. +immediately preceding `unaligned. 1`, `2` or `4` selects that requirement, and the decoder reads +it from the raw instruction before the block opcode. A branch cannot enter between a prefix and +its instruction, and prefix combinations with unsupported modifiers are rejected. `unaligned.` is also accepted before supported integer `ldind.*`/`stind.*`, `ldobj`/`stobj` and -`ldfld`/`stfld`. These accesses already use byte alignment, so the prefix lowers to a no-op and -bypasses no safety check. +`ldfld`/`stfld`. These accesses already use byte alignment, so the prefix decodes to a no-op and +bypasses no safety check: `ldfld` and `stfld` after it decode to the same field instruction as +without it. `ldflda` after `unaligned.`, and any field instruction after `volatile.`, are +rejected. -The exact managed-reference and pointer overloads of `Unsafe.InitBlock`, `InitBlockUnaligned`, -`CopyBlock` and `CopyBlockUnaligned` use these same operations. Pointer arguments must come from -admitted allocation-relative references; arbitrary integer addresses are unsupported. The -certificate binds each helper's exact signature, operation and alignment. +Calls to `Unsafe.InitBlock`, `InitBlockUnaligned`, `CopyBlock` and `CopyBlockUnaligned` retain +their call instruction. The runtime model accepts two static overloads per name, one taking byte +references and one taking void pointers. Both use the checked block operation with alignment +eight for the aligned helpers and one for the unaligned helpers. Pointer arguments must come +from admitted allocation-relative references. ## Pointer subtraction and ordering @@ -244,10 +262,10 @@ examples of rejected opcodes and forms. | Area | Examples rejected by extraction | | --- | --- | -| Debugger and prefixes | `break`, `volatile.`; `tail.` outside admitted same-assembly calls; `readonly.` outside the checked array-element subset; `constrained.` outside the checked struct-call subset | +| Debugger and prefixes | `break`, `volatile.`, and every `no.` flag combination. Every prefix requires a valid operand and an immediate taker, including in unreachable code. Repeated, combined and trailing prefixes are rejected. | | Other conversions | Floating-point conversions | | Other indirect/block memory | `ldind.ref`, floating-point `ldind.*`, `stind.ref`, floating-point `stind.*` | -| Objects, arrays and metadata | `ldstr` of a text containing a surrogate, backspace or form feed, `box`, `unbox`, `unbox.any`, `castclass` and `isinst` of interfaces, `System.Object`, value types or classes that are not admitted, byref-like, floating-point and non-class reference `newarr`, floating-point `ldelem.*`/`stelem.*` and `ldelem.ref`/`stelem.ref` outside arrays of classes, interface and `object` arrays, `ldtoken` of a method or of a type outside a decided type test | +| Objects, arrays and metadata | `ldstr` of a text containing a surrogate, backspace or form feed, `box`, `unbox`, `unbox.any`, `castclass` and `isinst` of interfaces, `System.Object`, value types or classes that are not admitted, byref-like, floating-point and non-class reference `newarr`, floating-point `ldelem.*`/`stelem.*` and `ldelem.ref`/`stelem.ref` outside arrays of classes, arrays of interfaces, `object` or other classes outside the model, `ldtoken` of a method or of a type outside a decided type test | | Static field values | `stsfld`, `ldsfld` and `ldsflda` of fields outside the admitted readonly subset | | Dispatch and exceptions | Unconstrained `callvirt` of interface methods, of methods declared outside the extracted assemblies, or of virtual methods whose hierarchy the table cannot close, `jmp`, `ldvirtftn`, `throw` outside a folded throw, `rethrow`, `leave`, `leave.s`, `endfinally`, `endfilter` | | Floating-point constants | `ldc.r4`, `ldc.r8` | @@ -256,13 +274,13 @@ examples of rejected opcodes and forms. Closed `typeof(A) == typeof(B)` and `!=` tests use two adjacent `ldtoken; call Type::GetTypeFromHandle` pairs followed by `Type::op_Equality` or -`op_Inequality`, with exact `System.Runtime` signatures. The four operands emit `nop` -and the comparison emits its decided Boolean. `ReferenceEquals(typeof(A), typeof(B))` -used as a Boolean value ends in operandless `ceq`, which emits the same decided Boolean. -Its negation uses the ordinary integer comparison of that result with zero. Reference-equality -branches use the same operands followed by short or long `beq` or `bne.un`; they emit -an unconditional branch to the original target or a fallthrough `nop`. No instruction -slots are inserted. Feature and scalar certificates share the selected successor. +`op_Inequality`. The extractor checks a static, nongeneric default calling convention and the +`System.Runtime` identities of `Type` and `RuntimeTypeHandle` in the signatures. Each `ldtoken` pushes the handle of its closed type. Each `GetTypeFromHandle` call consumes +that handle and pushes the corresponding runtime type. The operator compares both runtime types +and returns an `i32` Boolean. `ReferenceEquals(typeof(A), typeof(B))` uses operandless `ceq` +for a Boolean result, or short or long `beq` or `bne.un` to branch directly. Execution retains +the instruction positions and branch targets. The feature and scalar certificates use the known +comparison result to prune the unreachable successor. Types are equal when their metadata is identical, and different when their full names differ; one full name in two scopes, decorated names and `void` stay undecided. Every @@ -274,14 +292,15 @@ outside this subset. ### Throws `throw new E(...)` compiles to one `ldstr` or `ldnull` per constructor argument, `newobj` of -`E`'s constructor and `throw`. For an exact allowlisted `System.Runtime` constructor, the loads -and `newobj` emit `nop` and `throw` emits `.throw "E"`, which ends execution with a fault located -at the `throw`. A `[DoesNotReturn]` helper containing only this pattern is an ordinary call. +`E`'s constructor and `throw`. For an exact allowlisted `System.Runtime` constructor, each +`ldnull` pushes the null object reference. The string loads and `newobj` emit `nop`. The +`throw` raises the `CIL.ExceptionType` for `E` at the original instruction position. A `[DoesNotReturn]` +helper containing only this pattern is an ordinary call. Other arguments, any instruction between the parts, a branch past the first part, rethrowing a stored exception and exception handlers are rejected. -The adjacent `ldnull; throw` form emits `nop` followed by the same named-fault operation for -`System.NullReferenceException`, located at the raw `throw`. A branch may target the null load, +In the adjacent `ldnull; throw` form, `ldnull` pushes the null object reference and the +following `throw` raises `.nullReference` at its original position. A branch may target the null load, but cannot enter at the throw. Nulls passed through locals, arguments or intervening instructions do not satisfy this exact fold. @@ -289,12 +308,16 @@ In addition to the admitted string constructors, `Exception`, `InvalidOperationE `ArgumentException` and `ArgumentOutOfRangeException` admit a message with a null inner exception. `ArgumentException` also admits message, parameter name and null inner exception; `ArgumentOutOfRangeException` admits parameter name, null actual value and message. -Only the thrown type is observed. Constructor signatures and null operands are checked -independently; expressions with effects or faults are not discarded. Prefix recognition inspects -at most four loads. +The model observes the thrown type. Lean checks the declared instance calling convention, return +type and parameter types against the nongeneric constructor. A separate check requires null for +inner exceptions and actual values. Prefix recognition inspects at most four loads. ### Constrained struct calls +The decoder represents `constrained. T` as a no-op at the prefix pc. The adjacent call names the +slot and carries the closed receiver type `T`. Lean checks the link at the call pc against the +implementation selected for that receiver. An unprefixed `callvirt` still requires literal emission. + An adjacent `constrained. T; callvirt` pair supports a concrete, reference-free struct `T` and a method declared on that struct or a directly implemented interface. `T`, the slot's interface and, for an interface-level body, every interface of `T` must be declared in the extracted assembly or a @@ -302,7 +325,10 @@ for an interface-level body, every interface of `T` must be declared in the extr struct may implement a slot of a referenced interface. An interface of any other assembly is never read for dispatch, so a body that needs it is rejected. Closed sequential generic structs admit direct slots and directly implemented interface slots, -including closed generic interfaces. The slot's closed declaring owner must match exactly. +including closed generic interfaces. For a direct slot or an implementation on the struct, the +generic owner must have a value-type head and match the receiver's closed layout identity. +An interface slot uses its full declaring type, including its generic arguments. Dispatch rejects +a slot whose class or interface head gives the same closed identity as the struct receiver. Implicit implementations must uniquely match the public name and signature; explicit `MethodImpl` overrides take precedence and retain the closed interface owner as well as the method token. Slots with [`in` parameters](#in-parameters-of-virtual-members) match only implementations @@ -369,6 +395,10 @@ checks signed input ranges and permits an empty view at the allocation end. Gene are outside this support; see [span reference parameters](#span-reference-parameters) for `in` and `ref readonly` spans. +Indexing, length, both `Slice` overloads, `IsEmpty`, `GetPinnableReference` and `Empty` keep their +direct call. The same rule applies to conversions from an array to either span type and from +a writable span to a readonly span. Each overload executes its corresponding memory operation. + Aggregate span widths come from the validated target-specific layout, including vector-field alignment; vectors use their exact storage width. Indexing yields a checked element reference for typed loads and stores. Single-reference constructors form one-element views over live @@ -378,12 +408,19 @@ references. `Span.Empty` and `ReadOnlySpan.Empty` return a null, zero-length view with the element width and readonly capability of the span type. Explicit `default` initialization or -reassignment of a span local accepts the exact `ldloca; initobj` pair, including resetting a -nonempty local; the initialized type must match the local, and branches cannot enter at -`initobj`. +reassignment of a span local or homed span parameter decodes `ldloca; initobj` or +`ldarga; initobj` to `initobj` through the home, including resetting a nonempty local. The +initialized type must match the local or parameter, `initobj` must directly follow the address, +and no branch may target it. The reset stores the empty view of the slot's checked element width. +A span local whose aggregate element layout is unresolved has no checked width, so its method +does not decode. Span `IsEmpty` and `GetPinnableReference` do not read the view's bytes. An empty span's pinnable reference is null, and readonly views return readonly references. +The decoder keeps supported `MemoryMarshal` calls. The runtime model checks the generic +declaration and closed element types, then derives the memory operation from the method's +layouts. Vector elements must have integer lanes, including the target of a cast. + `MemoryMarshal.GetReference(Span)` returns a writable span's start reference, including for an empty span. The `ReadOnlySpan` overload returns a reference restricted as `readonly.` results are, although .NET types it `ref T`: reads and field reads through it succeed and stores @@ -395,14 +432,20 @@ keeps its data reference, and a null array raises `NullReferenceException`. Array constructors, implicit array conversions and the array overloads of `MemoryExtensions.AsSpan` form views over supplied integer, enum, aggregate and integer-vector arrays. Enum views keep their exact enum identity through locals, slices, calls and returns; -equal-width enums are not interchangeable. `AsSpan` accepts a whole array, a start index, or a +equal-width enums are not interchangeable. The decoder keeps `AsSpan` as a call. Its runtime +model accepts a whole array, a start index, or a start and length. A null array produces an empty span only for a zero range; an invalid range raises `ArgumentOutOfRangeException`. Non-array `AsSpan` overloads are unsupported. -In-place construction of a span local accepts `ldloca`, up to three simple argument loads or -integer constants, and its exact constructor call. The address cannot escape or be duplicated, -and control flow cannot enter the sequence after its first instruction. The local may be -reused or reinitialized; construction faults remain at the call instruction. +In-place construction of a span local or homed span parameter is a call on its home: `ldloca` or +`ldarga` of a local or parameter of the constructed type, one local or argument load, `ldnull` +or int32 constant per constructor parameter (at most three), and the exact constructor call, +with no branch to an argument or the call. The call's runtime model builds the view as the +overload's `newobj` model does and stores it in the slot the home names, except that +`ReadOnlySpan(void*, int)` views any readable bytes the pointer names, where `newobj` +requires extracted static data. The step faults unless the constructor builds spans of the +slot's width and access. The local may be reused or reinitialized; construction faults remain +at the call instruction. `Array.Fill(T[], T)` and its `(T[], T, int startIndex, int count)` overload support primitive integers, Boolean, character and integer-backed enums. A null array throws @@ -410,9 +453,10 @@ integers, Boolean, character and integer-backed enums. A null array throws array, throws `ArgumentOutOfRangeException`. Empty ranges, including at the array end, need no write authority. Reference-bearing, aggregate and vector elements are not supported. -`Span.Clear` initializes the whole view, including aggregate and vector elements. `Fill` -repeats a captured primitive, enum, aggregate or integer-vector value; unspecified -low-lane-only vector values are rejected. A filled aggregate or vector snapshot keeps its +The decoder retains span and array bulk calls. The runtime model uses the closed element type +when executing the call. `Span.Clear` initializes the whole view, including aggregate and +vector elements. `Fill` repeats a captured primitive, enum, aggregate or integer-vector value. +A fill rejects unspecified low-lane-only vector values. A filled aggregate or vector snapshot keeps its initialization mask, so unknown bytes stay uninitialized. Both need write authority but not initialized or readable destination bytes, access no bytes for an empty span and preserve neighbouring bytes, including other slices of the same allocation. Readonly views do not admit these members. @@ -434,28 +478,32 @@ aggregate or integer-vector values through byte spans. Short `Read`/`Write` call reference and returns false; a short `TryWrite` returns false without reading the input value or modifying memory. Successful transfers preserve snapshot initialization masks and retain the checked model's bounds, lifetime and authority requirements. `Vector128.Create` and -`Vector256.Create` over a `ReadOnlySpan` are the same checked `Read` of the vector. +`Vector256.Create` over a `ReadOnlySpan` retain the call and run the same checked +`Read` of the vector. The table also resolves scalar vector construction, whole-vector +equality and the byte-reference overloads of `Vector128.LoadUnsafe` and `Vector128.StoreUnsafe`. A collection-expression property such as `static ReadOnlySpan Table => [...]` compiles to `ldtoken ; call RuntimeHelpers::CreateSpan` over a static data field (`ldsflda` rules, packing 1, 2, 4 or 8, with an RVA that is a multiple of the packing, inside one section's raw data, in a section whose virtual address and file offset agree modulo 8 so that flat and -mapped loads keep the alignment). The span covers the whole field and has `size / sizeof(T)` +mapped loads keep the alignment). The method decoder preserves the RVA field token or address. +The runtime model binds the open field-handle declaration and the closed element type of +`RuntimeHelpers.CreateSpan`. The span covers the whole field and has `size / sizeof(T)` elements, for any primitive integer `T`. As in CoreCLR, which throws `ArgumentException` for data misaligned for `T`, `sizeof(T)` must divide the field's packing; otherwise the call is an `unsupported` fault. Roslyn emits each such field with packing `sizeof(T)` at an 8-byte-aligned RVA, in a section whose virtual address and file offset are multiples of 512. A caller reads it -as `stloc s; ldloca s; [index;] call get_Item` or `stloc s; ldloca s; call get_Length`: the store -and address emit `nop`, so the span stays on the stack as the receiver. `get_Item` faults with +as `stloc s; ldloca s; [index;] call get_Item` or `stloc s; ldloca s; call get_Length`: `stloc` +stores the typed span view, `ldloca` pushes a home for the local, and the call reads the view +from the slot as its receiver. `get_Item` faults with `System.IndexOutOfRangeException` at the call when the index, compared unsigned as in .NET, is -out of range. `MemoryMarshal.GetReference` also accepts these spans. The fold is used only for a -single-use receiver with a single-load or constant index; other supported uses keep typed span -storage. Branches into a folded receiver are rejected. +out of range. `MemoryMarshal.GetReference` also accepts these spans. Receiver locals retain typed +span storage for repeated reads and computed indexes. ### Span reference parameters `in` and `ref readonly` parameters of an admitted `Span` or `ReadOnlySpan` type receive -the referenced view, as `ldloca` and `ldarga` of a span local or parameter do. Callers pass a +the referenced view, which the call reads from the caller's span local or parameter when it runs. Callers pass a span local, span parameter or temporary by address, and callees read the length, index, slice, copy or forward the reference to another readonly span parameter. The view keeps its element width, write capability and allocation, so element writes through `in Span` still need write @@ -475,8 +523,9 @@ span parameters are rejected. C# encodes an `in` parameter of a virtual, abstract or interface member, and of its explicit implementations, as `T& modreq(System.Runtime.InteropServices.InAttribute)`. The modifier is -admitted only directly on a by-reference parameter and only with the exact `System.Runtime` -scope, and the parameter then behaves as a non-virtual `in` parameter of the same type: an +admitted only directly on a by-reference parameter, with a top-level metadata reference whose +namespace is `System.Runtime.InteropServices`, name is `InAttribute` and raw assembly scope is +exactly `System.Runtime`. The parameter behaves as a non-virtual `in` parameter of the same type: an ordinary reference, or the referenced view for an admitted span type (see [span reference parameters](#span-reference-parameters)). Selectors, recorded signatures, specialization keys and constrained-slot matching keep the modifier. A method whose declaring @@ -491,8 +540,9 @@ signatures admit no modifier. C# marks an `init` accessor by returning `System.Void modreq(System.Runtime.CompilerServices.IsExternalInit)`, and the accessor runs as the void method it is, as an entry or behind a call. The modifier is admitted only directly on -`System.Void`. Like C#, the extractor recognizes the marker by its full name from any assembly, -so a library that declares its own `IsExternalInit` for an older framework is admitted too. +`System.Void`. The extractor requires a top-level metadata type whose namespace is +`System.Runtime.CompilerServices` and name is `IsExternalInit`, from any assembly. A library +that declares its own marker for an older framework is admitted too. Selectors, recorded entries and the Lean signature string keep the modifier; a method whose declaring type also has a modifier-only twin is rejected ([arrays, views and bulk memory](SOUNDNESS-RUNTIME.md#arrays-views-and-bulk-memory)). @@ -508,9 +558,10 @@ initializers are a checked helper call, a copy of an earlier field, a direct int a direct struct construction, described in that order below. `public static readonly Word256 One = 1ul;` compiles to -`ldc.i4.1; conv.i8; call op_Implicit(ulong); stsfld One` in the static constructor. A reachable -`ldsfld` of such a field emits `.staticValue bytes`, which pushes the field's value as an -aggregate and touches no memory. The static constructor must store the field exactly once, +`ldc.i4.1; conv.i8; call op_Implicit(ulong); stsfld One` in the static constructor. For a reachable +`ldsfld`, the method decoder matches the typed descriptor at the instruction index against the +static field operand. The resulting `.ldsfld field` pushes the aggregate bytes recorded for that +field. The static constructor must store the field exactly once, unconditionally (no control flow before the store and no branch back to it). A helper-call initializer uses a same-assembly static call whose arguments are integer constants (`ldc.i4*`, `ldc.i8` with `conv.i4`/`conv.u4`/`conv.i8`/`conv.u8`/`conv.i`/`conv.u`). A field may instead @@ -518,7 +569,7 @@ copy (`ldsfld G; stsfld F`) such a field stored earlier, as in `MinValue = Zero` method and its callees become part of the extracted program and must not access static fields. The static site records the call, and the extraction certificate reruns it in the checked interpreter; -`Extracted.translation_valid` fails unless the result is exactly the emitted bytes. +`Extracted.translation_valid` fails unless the result is exactly the descriptor's bytes. Primitive integer fields (including Boolean, Char and 64-bit native integers) and same-assembly integer enums also admit a direct integer literal, optionally widened with the supported integer @@ -534,14 +585,16 @@ Closed constructors use the ordinary checked generic specialization. How Lean ch value, and what the initialization assumption covers, is in [static storage](SOUNDNESS-RUNTIME.md#static-storage). -`ldsflda` of an admitted readonly field uses the same identity-keyed immutable storage as RVA -data. Repeated accesses share one allocation; distinct fields remain distinct even when their -bytes agree. The reference can be passed to supported helpers and instance methods. Checked -writes through it fail, including writes through `Unsafe.AsRef`. +For an admitted readonly field, the decoder binds `ldsflda` to the field and its descriptor. +Repeated accesses share one immutable allocation, as they do for RVA data. Distinct fields have +separate storage even when their bytes agree. Supported helpers and instance methods can use the +reference. Checked writes through it fail, including writes through `Unsafe.AsRef`. -Extraction rejects a declaring type without the assumption; aggregate values built by other code -shapes; a second store, a store outside the static constructor, or `ldsflda` of the field during -initialization; and `stsfld` in an extracted method. +Extraction requires the declaring type's initialization assumption and one of the supported +aggregate initializer forms. A second store, a store outside the static constructor, or taking +the field's address during initialization prevents extraction. Extraction rejects a reachable +store, the method decoder refuses a method that has one, and the interpreter reports an +unsupported store if one occurs in a hand-built method. #### Unknown readonly values @@ -564,8 +617,9 @@ aggregate fields whose value cannot be computed. Each such field is listed in ## Runtime calls and SIMD -SIMD and hardware features enter compiled CIL mostly through `call`, rather than dedicated SIMD -CIL opcodes. [SIMD](SIMD.md) lists the admitted vector and hardware-intrinsic overloads with +Hardware intrinsics keep their CIL `call`. The runtime model table selects the operation and +argument count from the closed method reference. The decoder checks the open declaration, +external target and feature profile before admitting the call. [SIMD](SIMD.md) lists the admitted vector and hardware-intrinsic overloads with their element types and profile requirements, and links the extractor sources that define them. The portable bitwise and equality forms admit no floating-point equality and no observation of [unspecified lanes](SOUNDNESS-RUNTIME.md#unspecified-vector-lanes). @@ -614,22 +668,23 @@ by that size and wrap at the validated native width. Forming the resulting refer allocation identity, lifetime and bounds, including admitted one-past positions, and grants no read or write permission; an offset from a readonly reference stays readonly, so stores through it fault ([pointer element offsets](SOUNDNESS-RUNTIME.md#pointer-element-offsets)). Classes, -pointer/byref type arguments and unsupported layouts fail extraction. +pointer/byref type arguments and unsupported layouts fail extraction. A call of `Unsafe.SizeOf()` +keeps its `call`, which pushes the size the runtime model table reads from the element's checked +shape, as `sizeof` does. `Unsafe.SkipInit(out T)` admits the same storage types. It validates the managed reference and leaves memory, initialization flags and access permissions unchanged, so reads of the output -still require initialized bytes. Extraction evidence checks the exact runtime call identity and -its translation to `skipInit`. +still require initialized bytes. A call keeps its `call`, which runs the model the runtime model +table gives its exact closed reference. -`Unsafe.NullRef()` and `Unsafe.IsNullRef(ref readonly T)` admit the same -storage types. `NullRef` pushes the model's null managed reference, which a +`Unsafe.NullRef()` and `Unsafe.IsNullRef(ref readonly T)` keep their calls. Their models +require recognized storage for `T` and exclude byref storage. `NullRef` pushes the model's null managed reference, which a byref local or argument can hold like any other reference. `IsNullRef` returns 1 for it and 0 for every other reference, live or not, without accessing memory. -Every load, store, copy, initialization, field or lane access of one or more +Every load, store, copy, initialization or field access of one or more bytes through null faults with `nullDereference`, the model's `NullReferenceException`, and no address is formed from it; zero-length block -operations access nothing. Extraction evidence binds each call to its operation in both -directions, as for `SkipInit`. +operations access nothing. The model table binds the open declaration and external target of each call. Reference `add` admits either operand order, matching the [.NET 10 importer](https://github.com/dotnet/runtime/blob/v10.0.0/src/coreclr/jit/importer.cpp#L5093-L5106); @@ -642,7 +697,7 @@ translations into reference, memory or vector operations; they do not make those supported as a whole. Managed byte-reference and pointer `Unsafe.ReadUnaligned` and `WriteUnaligned` support -admitted integer, aggregate and integer-vector storage. They are the typed loads and stores +admitted integer, aggregate and integer-vector storage. The calls run typed load and store models with byte alignment, keeping bounds, lifetime, access authority and snapshot initialization masks. Readonly source references are accepted; writes require writable references and byte authority. Pointer overloads require allocation-relative references, not numeric addresses. @@ -651,12 +706,20 @@ check. Primitive integers require their storage width; aggregates require their alignment, including packing. Integer vectors conservatively require alignment to their full byte width. This subset can reject vector addresses that a particular runtime accepts; the unaligned overloads remain available. Alignment checks do not repeat the per-byte authority scan. -`Unsafe.AsRef(void*)` preserves that provenance and forms a live reference without reading or -initializing bytes or granting access authority. Reference-bearing value types are unsupported. +Each transfer keeps its call. The decoder emits an opcode-tagged hole when the closed signature, +open declaration, external target or element layout fails to resolve. +The checked interpreter runs `Unsafe.As(ref TFrom)` and both `Unsafe.AsRef` +overloads as reference formation. They preserve the address, lifetime and access authority. +`As` requires field or vector storage for both type arguments. The extractor rejects a span, +array, class, byref, pointer or type variable as a type argument of `As` or `AsRef`, and the +decoder emits the call hole for an `As` site whose arguments lack that storage. +Both `AsRef` overloads keep their call. Each checks its own external target and open declaration +and accepts field or vector storage for its element. `Unsafe.BitCast` is admitted between equal-size validated unmanaged aggregates and `Vector128`/`Vector256`/`Vector512` values with `byte`, `int`, `uint`, `long` or `ulong` -elements, in both directions and under the scalar profile. Byte snapshots keep their +elements, including struct/struct and vector/vector pairs of equal size under any profile. +The Byte-to-Boolean overload accepts 0 and 1 and rejects other representations. Snapshots keep their initialization flags through the cast ([arrays, views and bulk memory](SOUNDNESS-RUNTIME.md#arrays-views-and-bulk-memory)), so numeric reads and vector arithmetic cannot consume unknown bytes of a partially initialized result. @@ -671,17 +734,20 @@ emitted as unsupported placeholders ([feature getters and profiles](SIMD.md#feature-getters-profiles-and-execution-restrictions)). Execution that reaches a placeholder fails. -Exact `IntPtr.Size`, `UIntPtr.Size` and `Environment.Is64BitProcess` getters produce `8`, `8` -and `true` ([runtime assumptions](SOUNDNESS-RUNTIME.md#runtime-assumptions)). -`ldsfld BitConverter.IsLittleEndian` produces `true`, since every valid profile is +Hardware feature getters keep their call and read the selected profile through a runtime model. +The calls to `IntPtr.Size`, `UIntPtr.Size` and `System.Environment.Is64BitProcess` run constant +models returning `8`, `8` and `true` ([runtime assumptions](SOUNDNESS-RUNTIME.md#runtime-assumptions)). +The decoder emits the word 1 for the exact System.Runtime Boolean field +`BitConverter.IsLittleEndian` when the profile is little-endian. Every valid profile is little-endian. The extractor checks the exact System.Runtime field and that it is `static readonly`, and rejects same-named types and fields from other assemblies and other -`BitConverter` fields. The certificate binds the raw call signature, or the raw field owner, type -and static load, and the emitted value. Raw field operands carry no member name, so the -certificate relies on -`IsLittleEndian` being the only static Boolean field of System.Runtime's `BitConverter`. +`BitConverter` fields. A getter binds its open declaration, closed signature and external target. +The field certificate binds the raw owner, name, type and static load to the emitted value. The +body check compares the operand's name with the name of the field row its token identifies. -These constants feed the feature-flow analysis. Known int32 words propagate through bitwise +The reference-content query keeps its call. Its model returns zero for checked reference-free +elements, including an aggregate whose identity matches its layout index. These constants +feed the feature-flow analysis. Known int32 words propagate through bitwise operations and `ceq`, `cgt`, `cgt.un`, `clt` and `clt.un`. Single-condition branches and all short/long signed or unsigned comparison branches select an edge when their operands are known. Unknown operands, including native and int64 values, keep @@ -689,6 +755,8 @@ both edges; joins and backedges discard conflicting constants. A known int32 swi chooses its indexed arm, or falls through when its unsigned value is out of range. Unknown selectors retain every successor. Every encoded branch or switch target remains bounds-checked and must enter a folded sequence or tail transfer at its start, even when unselected. +The raw decoder checks all encoded targets before applying a fold. An edge into a folded +throw after its head leaves the raw `throw` as an opcode-tagged hole. `RuntimeHelpers.IsReferenceOrContainsReferences()` produces `false` for supported primitive integer, Boolean, character, integer-enum and integer-vector types and for reference-free @@ -697,17 +765,15 @@ A validated byref-like aggregate without reference or byref fields is admitted; spans, byref/pointer arguments and unsupported storage are rejected. What the query does and does not establish is in [runtime assumptions](SOUNDNESS-RUNTIME.md#runtime-assumptions). -## Lean-only operations and implementation sources - -The Lean `CIL.Op` type and memory/SIMD modules contain operations that the extractor does not -emit, including vector argument homes, fixed vector-lane helpers, `newVector256` and additional -memory helpers. Their existence is not evidence that the corresponding arbitrary CIL or .NET API -is extractable. The checked memory interpreter implements generic aggregate operations. +## Implementation sources The implementation this inventory describes is: -- [InstructionTranslation.cs](../Extractor/InstructionTranslation.cs): opcode translation and - default rejection. +- The extractor checks each reachable CIL instruction against its operands and rejects unsupported + opcodes before emission. [Instruction admission](../Extractor/InstructionAdmission.cs) and + [operand checks](../Extractor/InstructionOperands.cs). +- The literal emitter and proof outlines use the C# representation of each instruction. + [Instruction translation](../Extractor/InstructionTranslation.cs). - [MetadataValidation.cs](../Extractor/MetadataValidation.cs), [Reachability.cs](../Extractor/Reachability.cs) and [StaticData.cs](../Extractor/StaticData.cs): type, call, control-flow and static-data restrictions. @@ -740,10 +806,12 @@ every admitted hardware profile. | `System.UInt32`, `System.UInt64`, `System.UIntPtr` | Static `Log2` | Floor base-two logarithm in the input's integer type; zero input returns zero. | | `System.Numerics.BitOperations` | `RoundUpToPowerOf2` on `uint`, `ulong` or `nuint` | Least power of two at least the input; zero for zero input or overflow | | `System.Buffers.Binary.BinaryPrimitives` | `ReverseEndianness` on `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nint` or `nuint` | Reverse bytes within the value; preserve signed return conventions; byte/sbyte are identity operations | -| `System.ArgumentNullException` | `ThrowIfNull(object, string)` | Raises `ArgumentNullException` for a null argument and otherwise pops both operands; the parameter name is never read | -| `System.ArgumentOutOfRangeException` | `ThrowIfZero`, `ThrowIfNegative`, `ThrowIfNegativeOrZero`, `ThrowIfEqual`, `ThrowIfNotEqual`, `ThrowIfGreaterThan`, `ThrowIfGreaterThanOrEqual`, `ThrowIfLessThan`, `ThrowIfLessThanOrEqual` on `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nint` or `nuint` | Raises `ArgumentOutOfRangeException` exactly when the integer test holds, comparing unsigned types as unsigned, and otherwise pops its operands; see [argument guards](SOUNDNESS-RUNTIME.md#argument-guards) | +| `System.ArgumentNullException` | `ThrowIfNull(object, string)` | The call runs a checked guard that raises `ArgumentNullException` for a null argument and otherwise pops both operands; the parameter name is never read | +| `System.ArgumentOutOfRangeException` | `ThrowIfZero`, `ThrowIfNegative`, `ThrowIfNegativeOrZero`, `ThrowIfEqual`, `ThrowIfNotEqual`, `ThrowIfGreaterThan`, `ThrowIfGreaterThanOrEqual`, `ThrowIfLessThan`, `ThrowIfLessThanOrEqual` on `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nint` or `nuint` | The call runs a checked guard that raises `ArgumentOutOfRangeException` exactly when the integer test holds, comparing unsigned types as unsigned, and otherwise pops its operands; see [argument guards](SOUNDNESS-RUNTIME.md#argument-guards) | -Signed 8/16-bit rotations and primitive signed `Log2` are unsupported. +Signed 8/16-bit rotations and primitive signed `Log2` are unsupported. A call of a `BitOperations` +helper, of the same helper on an integer type or of `ReverseEndianness` keeps its `call`, which runs +the scalar intrinsic the runtime model table gives its method reference. `System.Math.Min`, `Max` and `Clamp` support signed and unsigned 8-, 16-, 32-, 64-bit and native integers. `Abs` and `Sign` support the signed types; `Sign` returns an `int`. `Abs` of the @@ -754,6 +822,12 @@ exception handlers are not modelled. The corresponding static `Min`, `Max` and `Clamp` methods on `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nint` and `nuint` use the same operations. Signed types also admit `Abs` and `Sign`, with the same overflow behavior and `int` sign result. +A call of any of these helpers keeps its `call`, which runs the scalar intrinsic the runtime model +table gives its method reference. + +The decoder keeps the calls to `System.Math.BigMul` and `System.Math.DivRem` on each profile. +The runtime table selects the overload using the closed signature and binds the raw MemberRef +name, declaration and external target. An unmatched overload at a reachable call prevents admission. `System.Math.BigMul(ulong, ulong, out ulong)` returns the high 64 bits of the unsigned product and writes the low 64 bits through the checked output reference. The destination must be live, @@ -783,13 +857,13 @@ and `divRemBits_zero` and `divRemBits_overflow` the faults. Native-width overloads use the validated 64-bit target. The 128-bit (`Int128` and `UInt128` `BigMul`), generic-math and other unlisted helper overloads are unsupported. The extractor -validates the exact .NET 10 runtime assembly identity, declaring type, static calling convention +validates the exact .NET 11 runtime assembly identity, declaring type, static calling convention and signature. Correspondence to the runtime's implementation is an assumption; see [portable bit helpers](SOUNDNESS-RUNTIME.md#portable-bit-helpers). `BinaryPrimitives.Read{Int,UInt}{16,32,64}{Little,Big}Endian`, their `TryRead` forms, native-width `IntPtr`/`UIntPtr` counterparts, `Write` forms and `TryWrite` forms, 64 exact -signatures in all, operate on checked byte spans. Reads sign- or zero-extend according to the +signatures in all, keep the call and run its memory model on a checked byte span. Reads sign- or zero-extend according to the named integer type after applying byte order, and writes store exactly that width. Short ordinary transfers raise `ArgumentOutOfRangeException`; short try-reads write zero to the output and return false, and short try-writes return false without modifying memory diff --git a/docs/PROOFS-BINDING.md b/docs/PROOFS-BINDING.md index 0e8a3395c..85baa2c5e 100644 --- a/docs/PROOFS-BINDING.md +++ b/docs/PROOFS-BINDING.md @@ -13,7 +13,7 @@ instruction from an extracted method: ```lean extracted bind_entry entry := Extracted.method0 with [] from CIL.Safety.Memory.empty -bind_cutpoint cutpoint := Extracted.method0 matching (· matches .brnonzero _) with entry.frame +bind_cutpoint cutpoint := Extracted.method0 matching (· matches .brtrue _) with entry.frame ``` `entry.frame`, `entry.memory` and `entry.home0` (and subsequent numeric homes) come from @@ -34,7 +34,7 @@ Binders parameterize the generated declarations, including memory stores for sym import CIL.Safety.LoopScaffold /-- Returns its 32-bit argument. -/ -def body : CIL.Method := { code := [.arg 0, .ret], locals := [], returnsValue := true } +def body : CIL.Method := { code := [.ldarg 0, .ret], locals := [], returnsValue := true } bind_entry entry (value : CIL.W32) := body with [.scalar (.i32 value)] from CIL.Safety.Memory.empty @@ -112,8 +112,7 @@ Open it with `obtain ⟨home0, entered, enter⟩ := entry.checked ...` and read `madeN` and `kept`. A fact `valid : view.validate memory = .ok ()` is also supplied in the memory after the homes through [`SpanView.validate_below`](../CIL/Safety/FrameMemoryBelow.lean), so an assigned span parameter stores without restating its validity. A local that allocates a home -needs a fitting initializer or `.unmodeled`, and a vector argument home or a numeric parameter -home is rejected. +needs a fitting initializer or `.unmodeled`, and a numeric parameter home is rejected. [SpanSum](../Examples/Proofs/SpanSum.lean), [FillWords](../Examples/Proofs/FillWords.lean), [Struct](../Examples/Proofs/Struct.lean), [Countdown](../Examples/Proofs/Countdown.lean), [CountCalls](../Examples/Proofs/CountCalls.lean) and @@ -144,7 +143,7 @@ with frame` checks that particular instruction rather than requiring a unique pr bind_loop outer := Extracted.method0 [0] bind_loop inner := Extracted.method0 [1] bind_cutpoint cutpoint (frame : Frame) := Extracted.method0 at inner.latch matching - (· matches .brnonzero _) with frame + (· matches .brtrue _) with frame ``` Structural loop occurrences are ordered by head index. Out-of-range locations and wrong instruction @@ -174,21 +173,31 @@ contracts](PROOFS-BUFFERS.md) describes its memory preconditions. ## Move a run that starts inside a region Embedding relates whole bodies. When only a suffix is shared, `CIL.Safety.RegionRun` removes the -rest first. `RegionClosed program region` says fallthroughs, targets and callee entries of region -instructions stay in the region; `regionClosedCheck` decides it for a region listed as `(method, +rest first. `RegionClosed program region` says fallthroughs and targets of region instructions, +and the callee entries of every link-table entry at a region pc, stay in the region; `regionClosedCheck` decides it for a region listed as `(method, lower)` suffixes (`inStarts`). `AgreesOn program other region` says `other` has the same methods and -metadata and the same instruction at every region pc; `AgreesOn.set` and `code_agrees_of_drop` build -it for one replaced body. Then `run_congr_region` gives equal runs from any region pc, faults -included. Masking the code outside the region with `.unsupported` makes it dead for `Embeds`, so +metadata, link table included, and the same instruction at every region pc; `AgreesOn.set` and +`code_agrees_of_drop` build it for one replaced body. Then `run_congr_region` gives equal runs from any region pc, faults +included. Masking the code outside the region with holes (`.unadmitted`) makes it dead for `Embeds`, so `run_refines` carries the suffix run on to a body with a different prefix, for example a suffix after inline code to the same suffix after a helper call, one pc earlier. +A changed loop initializer can reuse a compiled termination proof. In `CountCalls`, the region +starts after the accumulator is initialized and includes the whole `Decrement` callee. Check +closure and agreement on that region, then apply `run_congr_region` at the loop guard with +the fuel and return value from the original proof. The consumer must still step the new prefix +and establish the invariant at the guard. Starting the accumulator at one establishes that +the remaining count plus the accumulator equals the input plus one modulo 2³². The original +decreasing measure proves termination for every input. + For a long suffix, import `CIL.Safety.Embedding.Suffix` and use -`CIL.Embedding.BodyRefines.of_relocated_suffix`. It uses one list equality: `actual.code.drop start -= (expected.code.drop lower).map (Op.relabel (fun pc => pc - lower + start))`. The expected prefix -must contain only dead instructions. All branch targets in the suffix must be at least `lower`. The -constructor also requires equal method metadata, equal static descriptor lists, and equal static -lookups at each moved pc. If `lower = 0`, `start` must also be zero to preserve method entry. +`CIL.Embedding.BodyRefines.of_relocated_suffix`. It uses one list equality: +`actual.code.drop start += (expected.code.drop lower).map (Instruction.relabel (fun pc => pc - lower + start))`. +The expected prefix must contain only dead instructions. All branch targets in the suffix must be at +least `lower`. The constructor also requires equal method metadata except the static sites and link +table, equal static descriptor lists, and equal static lookups and call links at each moved pc. If +`lower = 0`, `start` must also be zero to preserve method entry. The resulting map, `suffixRename lower start`, sends prefix pcs to zero and suffix pcs to `pc - lower + start`. Pass the proof to `ProgramRefines.set_body` and then `run_refines`. This avoids @@ -206,14 +215,87 @@ import CIL.MethodBinding /-- The body a proof expects at index 1 of the program. -/ def double : CIL.Method := - { code := [.arg 0, .arg 0, .add, .ret], locals := [], returnsValue := true } + { code := [.ldarg 0, .ldarg 0, .add, .ret], locals := [], returnsValue := true } def program : CIL.Program := [{ code := [.ret], locals := [], returnsValue := false }, double] --- Against an extraction the goal reads `Extracted.program[1]? = some double`. theorem found : program[1]? = some double := by cil_body ``` +An extracted method also carries its declared signature (`declared`), the layout tables shared by +the whole extraction (`environment`) and a call-link table (`links`), and an equality or an +embedding compares all of them with the extraction's. A bare literal such as `double` has the +default signature, with no parameters, a void return and localsinit, so it binds only to a method +declared that way. Start from the extraction's method and replace its code with `Method.withCode`, +which keeps the metadata and clears the pc-indexed link and static-site tables. Supply the links +for the instructions in the replacement body. + +```lean extracted +import Extracted +import CIL.MethodBinding + +/-- The extraction's method at index 1 with the body this proof expects. -/ +def double : CIL.Method := Extracted.method1.withCode [.ldarg 0, .ldarg 0, .add, .ret] + +theorem found : Extracted.program[1]? = some double := by cil_body +``` + +Do not write `{ Extracted.method1 with code := ... }`: a structure update copies the extraction's +link table and static sites, which are keyed by the extraction's pcs, so the update agrees with the +new code only where no call or static access moved. A body that reads statics sets the sites after +`withCode`, as in `{ Extracted.method1.withCode code with staticSites := [...] }`, one entry per +static access at the body's pc with the extraction's descriptor for it. A body with calls, constructions or +managed addresses states its link table after `withCode`, +in pc order, as in `{ Extracted.method1.withCode code with links := [(pc, link), ...] }`. +Each entry gives the extraction's binding at the corresponding pc. This is also the form for a body embedded into an +extraction with dead blocks, where the pcs of the calls differ: + +```lean +import CIL.Embedding.Tactic + +open CIL + +/-- The one-argument method both bodies call. -/ +def helper : Extraction.MethodRef := + { owner := .atom "Doc" "Doc", name := name!"Helper", hasThis := false, genericArity := 0, + returnType := .atom "Doc" "Word", parameters := [.atom "Doc" "Word"] } + +/-- What an extraction emits: a dead block puts the call, linked to method 1, at pc 3. -/ +def emitted : Method := + { code := [.ldarg 0, .br 3, .unadmitted 0, .call helper, .ret], links := [(3, .direct 1)], + locals := [], returnsValue := true } + +/-- The body the proof uses; its call is at pc 2, so its link is keyed by pc 2. -/ +def literal : Method := + { emitted.withCode [.ldarg 0, .br 2, .call helper, .ret] with links := [(2, .direct 1)] } + +theorem literal_embeds : MethodEmbeds [emitted] 0 literal := by cil_embed +``` + +A literal that does not bind is reported field by field. A difference in `declared`, `environment`, +`staticSites` or `links` prints `field declared:`, `field environment:`, `field staticSites:` or +`field links:` with the value on each side, and the layout tables as their counts. The `links` line +says to build the body with `withCode` from the extraction's method and to supply the links of its +calls after it, and the `staticSites` line says that `withCode` resets the sites and what entries to +supply after it. `cil_embed` adds the same +text when the static descriptor lists differ or a moved pc resolves another descriptor. The +`declared` and `environment` lines name no remedy: take both from the extraction's method. + +A proof that steps through a call or a construction reads the link at its pc. +`cil_instruction_equations` states that read as `body_link{pc} : body.linkAt pc = some link` for +each keyed pc. The command checks once that the keys ascend (`method{n}_links_ascend` in an extraction) and proves each fact from the +table's entry +(`Extraction.linkAt_of_position`), read through named suffixes of the table as the code is. At +a `call` the table does not key, the command states `body.linkAt pc = none` from the suffix of the +block where that pc would sit (`Extraction.linkAt_eq_readHead_suffix`), after the entry before +that block, whose key is below the pc, so the kernel walks at most two blocks of the table. The +`cil_code` simp set unfolds an extracted method's named link `methodNLinkPC` to its constructor. +For a pc without a call the table is read by its listed keys, which costs the number of calls and +never gets stuck (`Method.linkAt_of_unlisted`). A template whose table a feature flag decides +gets no link facts. The stepping tactics read a call's link from these facts; without one they +evaluate the table of a literal body or of a named body that unfolds, which costs the table's +length per call, and only an opaque body needs the link as a supplied fact. + `cil_body` runs `rfl`, so the proof is the same kernel-checked `Eq.refl`. When `rfl` fails, it evaluates both closed sides and reports, for each differing `Method` field, the first differing index, both elements and both lengths: @@ -222,8 +304,8 @@ index, both elements and both lengths: cil_body: rfl could not prove the method-body binding method index 1 field code: first difference at index 1 (left length 6, right length 6) - left: CIL.Op.brzero 10 - right: CIL.Op.brzero 4 + left: CIL.Instruction.brfalse 10 + right: CIL.Instruction.brfalse 4 ``` The left side is the extraction's body. An index past the end of the program is reported with the @@ -257,9 +339,10 @@ theorem decrement_holds : decrementContract.Holds Extracted.program decrement := `bind_method` defines `countCalls` (the index), `countBody` (the extracted body) and the `rfl` theorems `countCalls.signature` and `countCalls.found` (`@[cil_code]`). `bind_callee` also requires -the parent to call that method and adds `decrement.call`, the instruction at its call site -(`from countBody [k]` selects one of several); expected bodies take the index as a parameter, so -`.call decrement 1` survives renumbering. A statement about the callee alone runs it by that index, +the parent to call that method, by the link of the call site, and adds `decrement.call`, the +instruction at its call site, a `call` over the method it names, and `decrement.link`, its link +(`from countBody [k]` selects one of several); expected bodies take the index as a parameter of their +link table, so `links := [(pc, .direct decrement)]` survives renumbering. A statement about the callee alone runs it by that index, as in `invoke Extracted.program 8 decrement [.scalar (.i32 x)] memory`, which returns `.ok (memory, [.scalar (.i32 (x - 1))])`. [CountCalls](../Examples/Proofs/CountCalls.lean) proves the contract above and the loop that calls the helper through it. @@ -296,12 +379,12 @@ import CIL.StructuralLabels bind_loop reduce := rootBody [1] -- the second loop head in pc order bind_call after := step from rootBody [1] -theorem body : (rootBody.code.drop reduce).take 4 = [.local 0, .const32 3, .sub, .setLocal 0] := rfl +theorem body : (rootBody.code.drop reduce).take 4 = [.ldloc 0, .ldc_i4 3, .sub, .stloc 0] := rfl ``` A loop head is the target of a branch at the same or a later pc. `bind_loop` defines `reduce` and `reduce.latch`, the last pc branching back to it, and `reduce.backEdge` checks both. `bind_call` -names a `.call` to a bound method and adds `after.call`. The `[k]` is needed only when there are +names a `.call` to a bound method, by its link, and adds `after.call` and `after.link`. The `[k]` is needed only when there are several; without it every candidate is listed: ```text @@ -324,7 +407,7 @@ proof that imports it can name locals by their C# names, resolved in the scope t import ExtractedSymbols theorem stores : - sumBody.code[atWide + 1]? = some (.setLocal (local_named sum "carry" at (atWide + 1))) := rfl + sumBody.code[atWide + 1]? = some (.stloc (local_named sum "carry" at (atWide + 1))) := rfl ``` A name used for one slot needs no `at`; a name the PDB gives two slots, such as `carry` declared in @@ -349,12 +432,15 @@ documents share. Without a PDB every message is unchanged. ### Bodies modulo dead code -A profile can make the extractor emit dead `.unsupported` blocks that shift every later branch -target, which breaks a `cil_body` equality even though reachable behaviour is unchanged. `cil_embed` +A profile can make the extractor emit dead blocks of holes, `.unadmitted` with the raw opcode of +each pc, that shift every later branch target. That breaks a `cil_body` equality even though +reachable behaviour is unchanged, so `cil_embed` (from `CIL.Embedding.Tactic`) proves the weaker `MethodEmbeds` relation instead: every live instruction of the literal body appears in the extracted method at a mapped pc, with its code -targets mapped the same way and its fallthrough kept adjacent. Keep the proof over the literal body -and transport it with the lemmas in `CIL.Safety.Embedding.Simulation`: +targets mapped the same way and its fallthrough kept adjacent. The literal body must carry the +extraction's `declared` and `environment`, and for a body with calls the links of its own code, so +build it with `Method.withCode` from the extraction's method as shown above. Keep the proof over +the literal body and transport it with the lemmas in `CIL.Safety.Embedding.Simulation`: ```lean import CIL.Embedding.Tactic @@ -366,13 +452,13 @@ open CIL (Method MethodEmbeds) /-- Returns argument 1 on both paths; pc 4 is reached only by the branch. -/ @[cil_code] def body : Method := { - code := [.arg 0, .brzero 4, .arg 1, .ret, .arg 1, .ret] + code := [.ldarg 0, .brfalse 4, .ldarg 1, .ret, .ldarg 1, .ret] locals := [], returnsValue := true } /-- `body` as a profile-specific extraction emits it, with a dead block before the branch target. -/ def extracted : Method := { - code := [.arg 0, .brzero 6, .arg 1, .ret, - .unsupported "Unreachable under selected profile", .const32 9, .arg 1, .ret] + code := [.ldarg 0, .brfalse 6, .ldarg 1, .ret, + .unadmitted 0, .ldc_i4 9, .ldarg 1, .ret] locals := [], returnsValue := true } theorem bound : MethodEmbeds [extracted] 0 body := by cil_embed @@ -399,7 +485,7 @@ static descriptor list; `ProgramRefines.set` composes several bound methods. Nam transfer too: `bound.raises` transports `Raises`, `bound.holds` an `OutcomeContract.Holds` with both clauses (from `CIL.Safety.Embedding.Outcomes`), and `Embeds.invoke_raise` states the actual fault, whose pc is the one the map assigns in the throwing method. Other faults, including fuel exhaustion -and `.unsupported`, do not transfer. The pc map is computed outside the kernel and then checked by +and the fault of a hole, do not transfer. The pc map is computed outside the kernel and then checked by it, so the kernel rejects a wrong map without trusting it. A changed reachable instruction or an inserted live one is reported by expected and actual pc: @@ -407,10 +493,10 @@ inserted live one is reported by expected and actual pc: cil_embed: the expected body does not embed into the extracted method method index 0 expected pc 1 maps to actual pc 1 - expected: CIL.Op.brzero 4 - renamed: CIL.Op.brzero 6 - actual: CIL.Op.brzero 4 - code target differs: expected target 4 maps to actual pc 6, but the actual instruction targets 4; actual pc 4 (CIL.Op.const32 0x00000009#32) is not the image of any live expected pc + expected: CIL.Instruction.brfalse 4 + renamed: CIL.Instruction.brfalse 6 + actual: CIL.Instruction.brfalse 4 + code target differs: expected target 4 maps to actual pc 6, but the actual instruction targets 4; actual pc 4 (CIL.Instruction.ldc_i4 0x00000009#32) is not the image of any live expected pc ``` The kernel check takes about 0.12 s for a 202-instruction body and 1.3 s for 1,414 instructions. See @@ -432,7 +518,7 @@ open CIL /-- Reads its one local, a null reference, then returns argument 0. -/ @[cil_code] def stored : Method := { - code := [.local 0, .pop, .arg 0, .ret] + code := [.ldloc 0, .pop, .ldarg 0, .ret] locals := [.nullRef], localKinds := [.reference], returnsValue := true } /-- `stored` with two unused reference locals after its own, as a profile can extract it. -/ @@ -463,7 +549,7 @@ create or change any allocation. For numeric or interspersed dead locals, keep the extracted declarations and original local indices in the expected body and use ordinary `MethodEmbeds`. Unreachable code can still be omitted or -marked `.unsupported`. This route preserves the identities of later parent and callee allocations +marked as holes (`.unadmitted`). This route preserves the identities of later parent and callee allocations and so retains exact final memory; use it when a postcondition names fresh references. To omit numeric or interspersed declarations from the proof body, import @@ -475,12 +561,14 @@ initializer to satisfy `InitializerFits` for its kind. `LocalDeclarations.skip` initializer and this proof before the remaining declarations. It admits valid scalar, vector and aggregate values, null managed slots and unmodeled values. Private initialized homes are written outside the entire mapped allocation image, preserving caller bytes and -permissions. `MethodRenaming` checks the target code's relocated local operands; method indices, -call targets, profiles, return metadata and static sites stay exact. `ProgramRenaming` supplies this +permissions. `MethodRenaming` checks the target code's relocated local operands, and a home on the stack +names the relocated slot (`LocalIndexMap.value`); method indices, +call targets, profiles, return metadata, layout environments and static sites stay exact. +`ProgramRenaming` supplies this correspondence for every source method. The target is the extraction with the additional dead declarations. -This entry route requires `OrdinaryFrame` on both sides: `opaqueVectorLocals`, `vectorArgs` and +This entry route requires `OrdinaryFrame` on both sides: `opaqueVectorLocals` and `parameterHomes` are empty. Retained initializers stay exact, including initialized numeric helpers; their successful setup is derived from the actual source execution. Local widths must fit the native range; span element widths must be positive and less than `2^64`. These metadata conditions are @@ -496,7 +584,7 @@ reference, while a field holding a fresh object may name another identity in the initial memory to the target's final memory. `ProgramRenaming.raises` transfers a named exception and additionally requires valid initial arguments. These theorems use the proved complete dispatcher internally. The allocation relation extends through local setup, nested calls, -constructors, static allocation and `stackAlloc`; frame ownership and expiry remain checked. +constructors, static allocation and `localloc`; frame ownership and expiry remain checked. `MethodContract.Holds.of_allocationRenaming` transfers the original complete contract, including its precondition and footprint. Supply proofs that the precondition establishes well-formed memory, @@ -527,14 +615,33 @@ leancil templates shared scalar sse42 v3 ``` The command reruns exact extraction checking for every input and requires the same assembly bytes. -`shared/Templates.lean` defines each method once, with a profile parameter, parameters for the -feature flags it reads, and managed callee indices keyed by full method identity. Every original IL -instruction keeps its pc. Conditional instructions and excluded paths become `if` expressions and -unsupported padding; `cil_instruction_equations` generates kernel-checked equations for every pc. +`shared/Templates.lean` defines each method once, with a profile parameter, an environment parameter +for the extraction's layout tables, parameters for the feature flags it reads, and managed callee +indices keyed by full method identity. Each `Templates.InputN` namespace contains the closed raw +bodies, argument metadata, links, layout environment and reachability masks for that input. +Lean defines each input method with `cil_decoded_method` over `Method.ofClosed?`. +The templates command checks each extraction before it writes the shared module. A decoder +refusal identifies the method and its first refused live instruction. When profiles have the same +observed flags, Lean compares their decoded instructions and their links under one numbering of +the callees. A disagreement identifies its first pc before the command writes the templates. +The shared method takes each instruction from those decoded bodies. Every IL instruction keeps +its pc. At a missing flag combination each pc contains a hole with its original opcode. +`cil_instruction_equations` generates kernel-checked equations for each position. +For each input containing the method, `Templates.methodN_inputJ_code` proves by `rfl` that its +flag assignment selects that input's decoded code for arbitrary profile, environment and callee +parameters. `Templates.methodN_inputJ_links` checks the link table for the same assignment. +Each link comes from the selected decoded body, with its extracted method indices relocated to +the caller's callee parameters. Here `J` counts the inputs containing that method, in the order +supplied. `TemplateBindings0.lean`, `TemplateBindings1.lean`, and so on each import `Extracted` and `Templates` and prove `Extracted.program[i]? = some (Templates.methodN ...)` by `rfl`, including -all metadata and callee indices. Compile a binding only against its corresponding extraction, +all metadata and callee indices. The binding passes `Extracted.profile` and `Extracted.environment`, +and supplies the original indices for the callees. The exact binding ties the entire instantiated method to +the independently checked extraction. The extraction carries the byte, specialization and decoder +evidence, including `Extracted.method0_decoder_eq`, `Extracted.method0_decoded` and +`Extracted.method0_decoded_code` for the entry method. +Compile a binding only against its corresponding extraction, recorded in `templates.json`. Add the shared module and selected binding module to your proof package, import that binding from the consumer proof, and run `leancil verify` with that extraction and proof package. The templates command generates proof source; it does not replace @@ -554,16 +661,21 @@ produce unsupported instructions, and no correctness claim about another extract generating a template. The full profile remains an explicit parameter; a behavioral proof must relate its feature fields to the flag arguments it uses. Profiles that produce different metadata, different instruction counts, or different instructions under identical observed flags are rejected. +Template comparison uses typed operation records. For scalar helpers it compares the function, +arity and the operand widths or signedness that affect that function. Lean decodes the selected +body and checks its exact instruction equations against the corresponding input extraction. Static descriptors are retained when their complete metadata agrees across the selected profiles, including field identities, bytes, initializer indices and arguments. Different static metadata is -rejected rather than remapped. Imported callee slots and the model's `newVector256` constructor form -are not supported in template generation. +rejected rather than remapped. Imported callee slots are not supported in template generation. + +For inputs with 128-bit vector locals, the templates command reads the opaque slot lists +from Lean before comparing the profiles' frames. ### Instruction equations for parametric bodies A body written once for every profile takes its feature flags and callee indices as parameters and keeps each feature-dependent instruction as an `if`, padding the other profile's side with -`.unsupported` so every profile has the same pcs. `cil_instruction_equations` (from +a hole (`.unadmitted`) so every profile has the same pcs. `cil_instruction_equations` (from `CIL.InstructionEquations`) states one `@[cil_code ↓]` equation per pc for such a body, the same `code[pc]? = some op` equations it states for each method of an extraction, so a lookup rewrites by one equation instead of unfolding the body: @@ -572,28 +684,157 @@ one equation instead of unfolding the body: import CIL.InstructionEquations import CIL.Safety.SymbolicExecution -abbrev unreachable : CIL.Op := .unsupported "Unreachable under selected profile" +abbrev unreachable : CIL.Instruction := .unadmitted 0 + +/-- The parameterless method the body calls under AVX2. -/ +def helper : CIL.Extraction.MethodRef := + { owner := .atom "Doc" "Doc", name := name!"Helper", hasThis := false, genericArity := 0, + returnType := .atom "Doc" "Word", parameters := [] } + +/-- The external AVX2 feature getter that the call resolves in the model. -/ +def avx2Getter : CIL.Extraction.MethodRef := + { owner := .atom CIL.Extraction.intrinsicsScope "System.Runtime.Intrinsics.X86.Avx2", + name := name!"get_IsSupported", hasThis := false, genericArity := 0, + returnType := .atom CIL.Extraction.runtimeScope "System.Boolean", parameters := [] } def body (sse42 avx2 : Bool) (callee : Nat) : CIL.Method := - { profile := { sse42, avx2 }, returnsValue := true, locals := [], code := [ - .feature .avx2, .brzero 4, - if avx2 then .call callee 0 else unreachable, + { profile := { sse42, avx2 }, returnsValue := true, locals := [], + links := [(2, .direct callee)], code := [ + .call avx2Getter, .brfalse 4, + if avx2 then .call helper else unreachable, if avx2 then .ret else unreachable, if avx2 then unreachable else .ret] } cil_instruction_equations body -- body_instruction2 (sse42 avx2 : Bool) (callee : Nat) : --- (body sse42 avx2 callee).code[2]? = some (if avx2 = true then .call callee 0 else unreachable) +-- (body sse42 avx2 callee).code[2]? = some (if avx2 = true then .call helper else unreachable) -- cil_lookup fetches through the equation and decides the `if` from the fact. example (sse42 avx2 : Bool) (callee : Nat) (wide : avx2 = true) : - ∃ op, (body sse42 avx2 callee).code[2]? = some op ∧ op = .call callee 0 := + ∃ op, (body sse42 avx2 callee).code[2]? = some op ∧ op = .call helper := ⟨_, by cil_lookup [wide], by with_reducible rfl⟩ ``` A body whose code length depends on a parameter, such as an `if` around a whole block, is rejected with the first pc that is not determined. Only the kernel checks the equations. The command also -declares `body_suffix{start}`, the code from every 16th pc on, and `body_suffix{start}_placed`, and -proves each equation from the suffix that starts its block, so the equations cost time linear in +states `body_link{pc} : body.linkAt pc = some link` for each call site, `.direct callee` at pc 2 +above, when the body's table is the one its code implies or an explicit table whose keys are +literals. It declares `body_suffix{start}`, the code from every 16th pc +on, and `body_suffix{start}_placed`, and proves each equation from the suffix that starts its block, so the equations cost time linear in the body's length: about 6,400 heartbeats each from 250 to 4,000 instructions, as [instruction lookups](performance/InstructionLookups.md) records. + +### Field and value shapes + +An extraction states once how its methods resolve each distinct operand of a reachable `ldfld`, +`ldflda` or `stfld`, and of a reachable `ldobj`, `stobj`, `initobj`, `cpobj` or `sizeof`. +`Extracted.fieldRef{k}` holds a field operand as a `CIL.Extraction.FieldRef` and +`Extracted.valueType{k}` a type operand. `cil_field_shape` and `cil_value_shape` (from +`CIL.Safety.ResolutionFacts`) declare `Extracted.fieldRef{k}_shape` and +`Extracted.valueType{k}_shape`, which give `CIL.Method.fieldShape` or `CIL.Method.valueShape` of +the operand for every method whose environment is `Extracted.environment`. Each fact costs one +kernel check, however many sites and methods use the operand, and the `cil_code` simprocs +`reduceFieldShape` and `reduceValueShape` read a method's shape of the operand from it. The +`cil_step` simprocs `reduceFieldLoad`, `reduceFieldStore` and `reduceFieldAddress` step a class +field access by the equation for its slot, with the owner shown as the operand writes it, and a +struct load or store by the offset and width of its shape. For `ldflda`, the shape fixes the +offset used by the checked pointer addition. The receiver determines whether the resulting +reference is writable or readonly. A closed method and operand without a fact resolve by +compiled evaluation to the literal shape a fact would state, which the kernel checks again. +`set_option cil.requireFieldShapeFacts true` reports a missing fact as an error, so a proof can require that every field step reads a fact. `cil_checked_steps`, +`cil_contract_steps` and the loop steppers use the same resolved field shape. For a field address, supply the +pointer-addition fact directly. + +The extraction lists a `LayoutUse` for each struct construction, local and parameter home that +requires one. A field operand identifies its owner, field index, metadata name and held type. +`Method.fieldShape` reads the offset and width from the checked layout for the selected profile. + +For a closed generic struct, including a runtime value tuple, `Extracted.layoutIdentity{k}` +stores the checked UTF-8 bytes of its fully scoped name at layout index `k`. The layout row +and decoded operands, including call signatures, share that definition. Decoded owners and +signature types also share the runtime, intrinsics and memory assembly scopes with the model table, +including types with no layout row. Closed execution proofs can use `rfl` for these comparisons. +After projecting the returned values or another decidable result from `CIL.Safety.invoke`, +`decide` also compares the result or the located fault. + +`CIL.LayoutEnvironment.fieldShape` resolves a struct field to the offset and width of its +checked layout entry when the operand holds the field's declared tag. For an enum, +`Extraction.fieldKind?` requires an immediate runtime integer atom from the eight supported types +`System.SByte`, `System.Byte`, `System.Int16`, `System.UInt16`, `System.Int32`, `System.UInt32`, +`System.Int64` or `System.UInt64`. The enum has that integer's field kind. Other struct fields +must match their primitive kind, struct index, or vector width and element lane. A class instance +field resolves to the model slot of its index. `valueShape` gives runtime models the storage width +of integer and floating-point vectors. A vector operand on a raw object instruction must have a +supported integer element. + +Every object instruction names the `valueType{k}` constant of its type operand. A primitive +load or store steps through its width and signedness, a struct through its checked size, and +an integer vector through its transfer width. A span load forwards a span view and steps as +`nop` when the stack's top is one; a null reference faults as a null dereference and any other +operand as an invalid state. + +The first fact of an environment names suffixes of its layout lists, +`Extracted.environment_rawLayoutSuffix{start}` and `Extracted.environment_layoutSuffix{start}` +with their `_placed` lemmas, as `cil_instruction_equations` does for code. A struct field, a +class field holding an array of a struct and a struct type operand are then checked from their +layout entries alone, read from the suffix that starts the entry's block, so the facts of an +extraction cost time linear in its layouts and distinct operands. The compiled resolver proposes +each shape and the kernel checks it; a fact that misstates a shape fails to elaborate. + +### Methods defined from their decoder + +For every method with valid evidence, `MethodEvidenceValid.decoded` states that closed decoding +produces that method, including its frame, code, links and static sites. The theorem +`MethodEvidenceValid.ofBody?` gives the same result from the pinned body and metadata. + +`cil_decoded_method name := decoder` (from `CIL.Extraction.DecodedDefinition`) defines `name` as the +method returned by a closed decoder. `Method.ofClosed?` returns a typed error for an invalid +frame or prefix, or the pc and opcode of the first reachable hole. `Method.ofRaw?` returns an +optional method. The decoder takes the link table as given: a `call` and a +`newobj` name their method's metadata, and the table states the method index, and for a +construction the allocation, each enters, which `Extraction.ManagedCallsValid` checks against the +raw site. Compiled code evaluates the decoder, and the command writes the result as constructors +over numerals, except that a field whose type is that of a constant passed to the decoder, such as +the profile, the environment and the link table, is written as that constant, and a +zero-initialized struct local is written as `.aggregate (List.replicate size 0)`, the term an +template's frame and `make_fitting_local` hypotheses name. In a method with a constrained call or a dead site, +the command writes aggregate identities with the expressions from the environment's raw layouts. +The kernel then checks by reduction the theorem, named with the suffix `_decoder_eq`, that +`decoder = .ok name` for a closed method or `decoder = some name` for an optional method, +and the `@[cil_code]` facts named with the suffixes `_profile`, `_locals`, +`_localKinds`, `_returnsValue`, `_staticSites`, `_declared`, `_environment`, `_links`, +`_opaqueVectorLocals` and `_parameterHomes`; for an extraction's first method they are +`Extracted.method0_decoder_eq`, `Extracted.method0_profile` and so on. For a `Method.ofClosed?` +decoder, the `declared` field and the `Extracted.method0_declared` fact use the signature +constructor with the closed method's parameter and return type expressions. The extraction derives +`Extracted.method0_decoded` from the closed equation and the validated specialization. +It states that decoding the original method at its type arguments gives the same method. +The command requires a successful decoder result and kernel confirmation that it reduces to the +evaluated method. Admission checks the instructions at live positions. The form +`cil_decoded_method name [operand, ...] := decoder` writes each matching field or closed type +operand as its listed constant. The method can then use the facts from `cil_field_shape` or +`cil_value_shape`. The command rejects a listed constant that names no operand. + +The extractor defines every method this way. It refuses a method with a live instruction that does +not decode, or with a live call or construction whose operation differs from the one its decoder +gives, and names that instruction's IL offset, opcode and pc. A constrained prefix decodes with its +adjacent instance `callvirt` or static `call`. A bare `ldstr` decodes to a string literal with +the text bound to its metadata token. The decoder refuses backspace, form feed and characters +that require UTF-16 surrogate pairs. A recognized throw erases its message and constructor, +then raises the constructed exception at the raw `throw` pc. A folded type test over closed +type arguments produces its decided word or branch. A branch or switch into a fold's interior +disables that fold. A direct managed tail call retains its prefix and target. An indirect call +requires a callable catalog, and a field token requires the method's static site. + +Each dead pc carries `.unadmitted` with its raw opcode. Argument homes use the same certified +mask. The generated definition has this form + +```text +cil_decoded_method method0 := Method.ofClosed? profile environment method0Links method0Reachable + method0Closed method0RawArguments +``` + +The optional list before `:=` names the operands used by the decoded instructions, for example +`[fieldRef0, valueType0]` for a field access and an object instruction. The extractor follows the +definition with `cil_instruction_equations method0` and `cil_resolution_facts method0`. Stepping +reads the method through its `cil_code` facts and `Extracted.program_method{n}` equations. diff --git a/docs/PROOFS-BUFFERS.md b/docs/PROOFS-BUFFERS.md index f841ca74e..03163b17f 100644 --- a/docs/PROOFS-BUFFERS.md +++ b/docs/PROOFS-BUFFERS.md @@ -92,18 +92,33 @@ index below `elements.length` is in bounds of a non-null array. [`SpanContents.array_load`](../CIL/Safety/ArrayContents.lean), applied as contents.array_load valid scalar bound, gives the element's address `arrayElement memory values index width = .ok item` and its load, the element's value. -`step_arrayLoad_of_element` steps `ldelem` from those two facts and `step_arrayAddress_of_element` -steps `ldelema` from the address; `arrayElement_formed` forms that address, so a helper may return -it. An index outside the array throws, which a precondition bounding the index rules out. +`step_ldelem_of_element` steps an integer `ldelem.*` from those two facts. A typed `ldelem` of +an integer, enum, `bool` or `char` element steps to `arrayLoad` at the width its element type's +shape gives, which `arrayLoad_of_element` rewrites from the same two facts. +`step_ldelem_any_of_element` steps that typed `ldelem` from the two facts and the shape fact +`cil_resolution_facts` declares for its type, and `step_ldelem_any_of_transfer` steps one of a +struct or vector element from the shape fact and its `arrayTransferInstruction` fact. An outline +of a generic read specialized to a word uses `cil_element_shape% Extracted.method0 2` for a +typed load at pc 2. Lean reads the element type from that instruction's checked equation. +`step_ldelema_of_element` steps an `ldelema` whose type the method's layouts give that element +shape from the address, writable or read-only as a `readonly.` prefix records, and +`arrayAddress_of_element` gives the same reference once it steps to `arrayAddress`; +`arrayElement_formed` forms that address, so a helper may return it. An index outside the array throws, which a +precondition bounding the index rules out. A loop indexing the array reads it the way a span loop does, through the storage span: the caller's `CheckedSpan initial values.storage width true elements` carries into each iteration's frame by `CheckedSpan.within`, and `array_load` of its contents names the element. A loop storing into the array holds `SpanWritable` authority over `values.storage` instead; with the array addressed at `base`, `arrayElement_of_access` turns write authority over `base.element width index` and the -bound into the element `arrayElement` returns, and `step_arrayStore_of_write` steps `stelem` to the -written memory. The bound test compares the index with `values.Length` as signed words; -`SpanView.length_bounded` of the storage span keeps the length below `2 ^ 31`, and +bound into the element `arrayElement` returns. `step_stelem_narrow_of_write` steps a one-, two- or +four-byte `stelem.*` to the written memory, `step_stelem_wide_of_write` steps `stelem.i8` and +`stelem.i`, and a typed `stelem` steps to `arrayStore`, which `arrayStore_of_write` and +`arrayStore_i64_of_write` rewrite. `step_stelem_any_of_write` steps a one-, two- or four-byte +typed `stelem` from the shape fact and the write, `step_stelem_any_of_wide_write` an +eight-byte one, and `step_stelem_any_of_transfer` one of a struct or vector element from the +shape fact and its transfer. The bound test compares the index +with `values.Length` as signed words; `SpanView.length_bounded` of the storage span keeps the length below `2 ^ 31`, and `toInt_ofNat_of_lt`, which `cil_loop_obligation` applies, reads that comparison as unsigned. Converting the whole array to a span, as `ReadOnlySpan rest = values` does, pushes @@ -168,17 +183,21 @@ sequence-content lemmas above. Vector loads and stores preserve initialization m copies require readable, initialized source bytes. View creation and reinterpretation alone establish neither initialization nor read/write authority. -Single-reference and pointer span constructors, empty getters and in-place local resets use the -same views. +After a successful `writeSnapshot`, rewrite the stored memory's cell function with +`writeSnapshot_cells_eq`. Inside the destination it returns the source byte and its +initialization flag. Other offsets and allocations retain their previous cells. + +Single-reference and pointer span constructors, empty getters and span resets use the same views. `memoryInstruction_emptySpan` proves that every supported element width and capability produces -a null, zero-length view without changing memory. `SpanConstructor.empty` uses that same operation -when `initializeSpanLocal` replaces a local's previous view. -An `in` or `ref readonly` span parameter is passed as the referenced `.bufferSpan` view, so a -contract states the same view argument as a by-value span and reuses `SpanView.validate`, +a null, zero-length view without changing memory. `initializeSpanHome` stores that view in the +slot a span `initobj` addresses, and `spanInPlaceCall` stores the view a constructor builds in +the slot its receiver home names once `SpanConstructor.shape` matches the slot's width and access. +An `in` or `ref readonly` span parameter is passed as the `.bufferSpan` view the caller's home +holds when the call runs, so a contract states the same view argument as a by-value span and reuses `SpanView.validate`, `Contents` and the span element lemmas, whether the caller passes a span local or a temporary. -`MemoryMarshal.GetReference` of a readonly view steps by `step_memory_spanReference_readOnlyView_eq` -to a `.readOnlyReference`; loads, field loads and `ldflda` accept it, so the first-element load -is proved with the same lemmas. +`MemoryMarshal.GetReference` of a readonly view keeps its call. `step_call_model` exposes its bound memory +operation, and `step_memory_spanReference_readOnlyView_eq` gives its `.readOnlyReference`. +Loads, field loads and `ldflda` accept that reference. Element and byte offsets of a readonly reference step by the `step_memory_add_readOnly_*` equations to a `.readOnlyReference` with the writable form's address, so `add_element` applies unchanged, including for `Unsafe.Add(..., 1)`. Integer byte-order transfers and view observations @@ -195,9 +214,13 @@ introduced. Constrained interface calls on these closed owners, with implicit or implementations, use the same direct-call proof interface after the certificate resolves and binds the exact implementation. -For supplied native dispatch tables, provide an immutable allocation binding and exact catalog -target identities in `MethodTable.backing`. Checked byte offsets and `ldind.i` preserve -authenticated callable provenance; numeric address bytes alone are insufficient. These tables +For supplied native dispatch tables, provide an immutable allocation binding in +`MethodTable.backing`, the exact catalog target identities in `MethodTable.targets` and their +number in `MethodTable.entryCount`. The default proof of `MethodTable.counted` checks that number +once where the table is built, so a table step, and the bound check of an `ldind.i` load, read +the count without walking the entries. +Checked byte offsets and `ldind.i` preserve authenticated callable provenance; numeric address +bytes alone are insufficient. These tables require a unique supported managed indirect-call signature in each body; handler signatures cannot carry native table pointers. Ordinary call and tail-call lifetime and fuel obligations still apply. @@ -219,7 +242,10 @@ check, so these laws do not apply to it and its proofs stay at the byte level. A struct local is tracked in the same `LocalSnapshots` set as numeric locals, remembered as its bytes. `LocalSnapshots.zero_aggregate` returns the step equation for `initobj` through the local's -address. `store_field` and `load_field` cover int, word and 16- or 32-byte vector fields, +address. Pass that equation to `cil_checked_steps` or `cil_contract_steps`. The stepper uses the +generated operand shape to prove that the byte width of `initobj` equals the size of the home. +The loop frame stepper uses the same fact. `store_field` and `load_field` cover int, word and +16- or 32-byte vector fields, `load_signed_field` sign extends a 1- or 2-byte field as `signedOfWidth`, and `store_aggregate_field` and `load_aggregate_field` cover a nested struct field, whose value is the number of its bytes. Zeroing and stores also return the updated snapshots, well-formedness, @@ -386,9 +412,10 @@ theorem Filled.extend {xs : List (BitVec (8 * width))} {room index : Nat} Filled m base width (xs ++ [x]) room := by ``` -`Writable.write_one` with `step_storeScalar_of_write` (1-, 2- or 4-byte `int` stores) or -`step_storeScalar_i64_of_write` ([LoopFrames](../CIL/Safety/LoopFrames.lean)) proves a one-word -store helper's contract, as `Stamp`'s in CopyUntilZero. +`Writable.write_one` with `step_stind_narrow_of_write` (1-, 2- or 4-byte `stind` of an `int`), +`step_stind_i8_of_write` or `step_stind_i_of_write` +([CheckedMemoryFacts](../CIL/Safety/CheckedMemoryFacts.lean)) proves a one-word store helper's +contract, as `Stamp`'s in CopyUntilZero. ## Track a struct local by its fields in a loop @@ -407,10 +434,17 @@ valid fieldIndex value` steps `stfld` to a frame remembering `fields.set fieldIn equation, the frame after it and `MemoryBelow`; the slot, the field and its width are checked by `rfl` and `decide`, so a field outside the layout or a value of another width does not elaborate. A by-value read, `ldloc` then `ldfld`, loads the struct and reads the field with -`step_fieldAt_encode`. A 1- or 2-byte field holds an `.i32` and reads back truncated to its width -and zero or sign extended, as `narrowLoad width signed bits`: `step_fieldAt_encode_narrow` reads +`step_ldfld_encode`. A 1- or 2-byte field holds an `.i32` and reads back truncated to its width +and zero or sign extended, as `narrowLoad width signed bits`: `step_ldfld_encode_narrow` reads it by value and `load_narrow_field` through the address. +For a nested struct, `ldfld` uses the offset and size in the method's checked field shape. +`step_ldfld_aggregate_value_eq` reads initialized bytes from a struct on the stack. +`step_ldfld_aggregate_snapshot_eq` also preserves the initialization state of padding. +`step_ldfld_aggregate_reference_eq` reads the field through a reference to the containing struct. +`step_stfld_aggregate_reference_eq` and `step_stfld_aggregate_snapshot_eq` write initialized bytes +and snapshots through a writable reference. Both require the stored width to match the field. + The loop tactics gather the field reads and the home's `form` from each frame in scope and take the zeroing, field stores and numeric local stores themselves (see [loop tactics](PROOFS-STEPPING.md#compose-a-helper-call-inside-a-loop)), so a proof names none of them. diff --git a/docs/PROOFS-CONTRACTS.md b/docs/PROOFS-CONTRACTS.md index 4574c8167..6b4bd9274 100644 --- a/docs/PROOFS-CONTRACTS.md +++ b/docs/PROOFS-CONTRACTS.md @@ -103,12 +103,12 @@ local pushes, or `none`. | Need | Use | | --- | --- | -| Step a `.local` load | `snapshots.load value specified`, a step fact the stepping tactics take | +| Step a `.ldloc` load | `snapshots.load value specified`, a step fact the stepping tactics take | | Step a numeric store and get the updated snapshots | `LocalSnapshots.store` | | Keep snapshots across a call confined apart from the homes | `LocalSnapshots.after_confined` | | Keep snapshots across a write, or memory that keeps old allocations | `after_write_apart`, `after_below` | | The typed read of a remembered local whose slot is known | `snapshots.read_at specified slot` | -| That read for a vector | `read_vector128`, `read_vector256`, `read_vector512` | +| That read for a 64-bit word or a vector | `read_word64`, `read_vector128`, `read_vector256`, `read_vector512` | The vector reads are stated as `read memory home 16 1 = .ok (numberBytes bits.toNat 16)` and its wider forms. A snapshot that overlaps a callee's footprint is not kept: restate it from the @@ -135,6 +135,8 @@ Pass the `writable` projection to the general snapshot lemmas. The snapshot forms of the stepping tactics (`CIL.Safety.LocalSnapshotSteps`, part of `import CIL`) take numeric local stores and loads through a `LocalSnapshots` hypothesis and pass the updated snapshots to each later step. A load pushes the latest remembered value; a store replaces it. +A homed argument's `starg` and `ldarg` are the store and load of its home slot, so the snapshots +remember the argument at that slot. #### Tactic forms @@ -152,8 +154,7 @@ The two forms differ: | | Checked form | Contract form | | --- | --- | --- | -| Local instructions taken from the snapshots | `.local`, `.setLocal` | `.local`, `.setLocal`, `.vectorLocal`, `.setVectorLocal` | -| Other instructions | Ordinary checked steps and the return | Ordinary contract steps: supplied step facts, calls and returns | +| Instructions other than `.ldloc` and `.stloc` | Ordinary checked steps and the return | Ordinary contract steps: supplied step facts, calls and returns | | Finds each store's home | From the premises in context | From a homes hypothesis ([required context](#required-context)) | | Restates context facts after a store | `access` equations only; byte-value facts stay at the old memory | `access` equations and the [caller facts](#facts-the-contract-form-carries) | | Prepares step facts again after a store | No | Yes | @@ -245,9 +246,21 @@ the memory written to at the new memory, each under the name it already has: A fact whose condition is not shown stays at the old memory. +A restated fact replaces the fact it restates when that one is stated at a memory the walk +introduced, the `storedLocal` of an earlier store: the hypothesis the new one shadows is cleared, so +a walk adds the names of each store and nothing that grows with the stores before it. The walk +records the variables it introduces, so a fact stated at the memory the walk entered with, such as +`valid`, `homes` or `preserved`, stays in scope next to its restatement whatever that memory is +named. A `MemoryBelow` or `AccessBelow` fact that starts at a `storedLocal` memory is restated only +when homes are stated at that memory, as after a step that changes memory without a store; the ones +that start at the memory the walk entered with, such as `MemoryBelow boundary memory`, follow every +store, and the `localBelow` and `localAccess` of a store state the step from the memory before it. +The access a store finds for its own home is read by that store only, and the walk clears it with +the other facts the store alone uses. + The contract form then prepares its step facts again at `storedLocal`. The carried `ValidCall` forms the by-reference arguments and reads their fields there, and the restated reads supply their loads, so -`.arg`, `.fieldAt` and memory loads of caller data continue in the same call. After a store, a +`.ldarg`, `.ldfld` and memory loads of caller data continue in the same call. After a store, a word load through a `ValidCall` is stated at that store's memory, as `view.word storedLocal offset`. `ArgumentView.word_below localBelow view old` restates a word at the memory the store wrote to, and `ArgumentView.bytes_below` the whole range. @@ -283,7 +296,8 @@ the `MethodContract` and `OutcomeContract` lemmas for a contract indexed by `programStaticDescriptors`. `CIL.Program.ProfileAgreement.of_flags` reduces agreement to the flags in `program.profileReads`, -which `decide` checks for concrete profiles, so one proof covers every profile in a family. For a +the flags its feature tests and intrinsics read, a call's being those of its method's runtime +model, which `decide` checks for concrete profiles, so one proof covers every profile in a family. For a program with no profile reads, `empty : program.profileReads = []` discharges the premise with `by simp [empty]`. A separate per-profile extraction is covered once it is shown equal to the reprofiled program, by `rfl` when only the profiles differ. @@ -296,11 +310,11 @@ reprofiled program, by `rfl` when only the profiles differ. "contract": "Public.supported", "theorem": "Proof.family", "program": "Reference.program", - "referenceProfile": "x64-sse2", + "referenceProfile": "x64-sse42", "profiles": [ - { "profile": "x64-sse2", "artifact": "extractions/sse2" }, { "profile": "x64-sse42", "artifact": "extractions/sse42" }, - { "profile": "x64-v3", "artifact": "extractions/v3" } + { "profile": "x64-v3", "artifact": "extractions/v3" }, + { "profile": "x64-v4", "artifact": "extractions/v4" } ] } ``` @@ -373,13 +387,17 @@ def mixContract : MethodContract (CIL.W64 × CIL.W64) CIL.W64 where ### Step a caller through its callees `cil_contract_steps [facts]` steps an existential-fuel goal through ordinary instructions and -calls. At a call it applies: +calls. At an unlinked call with a runtime model, it uses the generated link and model facts to +execute the operation in the caller's frame. Supply the facts that `cil_checked_step` needs for +that operation, such as a span's validity and the result of slicing it. At a managed call it uses +one of the following proofs. - a proved `MethodContract.Holds` theorem marked `@[cil_contract]`, or one whose proof is in `facts`. Contracts merely present in the local context are not selected. The theorem's program and method binding must match the actual call; required binding premises are checked; -- a supplied successful child invocation, with fixed or existential fuel, or an - `InvocationCertificate`, without unfolding the helper body. +- a supplied or local successful child invocation, with fixed or existential fuel, a + `CallResult` or an `InvocationCertificate`, without unfolding the helper body. A local fact + that starts from another memory variable than the call is not tried. Input fields, each component of a tuple input included, are inferred from the encoded arguments; an input that is one word or one reference is the argument as the caller wrote it. The @@ -411,6 +429,10 @@ each defined contract unfolded, at entry and in each call's continuation, so the encoded output. A fact read from `call.post` keeps the contract folded until `dsimp only [mixContract] at` it. +A proof that composes calls with `WP.local_call` gets a `LoopFrame` after each call. `clear` the +frame of the call before once the next one is introduced: the stepping tactics read every frame +in scope at each step, so frames left in scope make each call cost more than the one before. + The tactic has these variants: | Form | Behaviour | @@ -471,7 +493,8 @@ the helper's contract. Confinement, access permissions and content snapshots req proved effects; a cell-only footprint does not imply `Confined`. `WP.load_call_store` composes a placed `loadCallStoreCode` segment with any proved scalar helper -contract. Both input words are loaded before the call and the output write, so the argument ranges +contract. Each `ldfld` operand must resolve to an eight-byte unsigned word at offset zero. The +call site must link to the specified helper. Both loads precede the call and the output write, so the argument ranges may overlap. The continuation receives the helper's `CallResult` and the actual checked write, and retains the helper's full postcondition and footprint without unfolding the helper body. @@ -488,6 +511,74 @@ A contract whose precondition implies well-formed memory and a valid static worl | `InvocationCertificate.of_invoke` | Certifies any successful `invoke` from those two premises | | `InvocationCertificate.of_run` | Certifies a run of the frame that checked arguments enter | +### Bind a contract to a decoded method + +`CIL.Safety.DecodedSummary` ties a contract to the method it was proved for. +`DecodedSummary program evidence profile environment index pin token arguments contract` holds +when `contract.Holds program index` and record `index` of `evidence` is the method with +metadata token `token` of the assembly pinned by `pin`, closed at type arguments `arguments`, +whose pinned body bytes decode to the method present at that index of `program`. The record's +feature certificate validates that method's reachability mask and decoded instructions. Its +`BodyDecodes` proof ties the assembly and signature to the names and tables read by the body +decoder. Relocation requires this evidence for the destination record. +`MethodContract.Certified` names only an index, which another extraction can fill with another +body. A summary also names the metadata method and the bytes. + +`DecodedOutcomeSummary` binds an `OutcomeContract` to the same evidence. Its `holds` field +supplies the normal result and memory footprint or the named exception for each permitted input. +For example, the compiled `Bump` updates a live account's balance and raises +`NullReferenceException` on a null account. Both outcomes use one pinned method body. + +| Name | Use | +| --- | --- | +| `BodiesDecode profile environment evidence program` | Each record's bytes, with its links, decode to the method at the same index | +| `decodedProgram?_eq_some` | Proves the decoded program from `programBindingsValid evidence program = true` and `BodiesDecode` | +| `decodedProgram?_getElem` | Reads the method at one index of a decoded program from its record | +| `DecodedSummary.of_translation` | Binds a proved contract to its record using the translation certificate and the method lookup at the contract index | +| `DecodedOutcomeSummary.of_translation` | Binds normal returns and named exceptions using the same method lookup | +| `DecodedOutcomeSummary.reprofile` | Preserves those outcomes at an agreeing profile when the same evidence decodes to the reprofiled program | +| `DecodedSummary.of_decoded` | Builds the summary of an index from the decoded program, the record found there, its `BodyDecodes` evidence, its instantiation and `Holds` | +| `DecodedSummary.holds` | The contract, for `cil_contract_steps` at the call or `cil_loop_call` at a call in a loop | +| `DecodedSummary.reprofile` | Reuses the contract and method identity at an agreeing profile once the same evidence decodes to the reprofiled program | +| `DecodedSummary.site` | At a live direct managed call, identifies the summary's MethodDef token and type arguments. | +| `DecodedAt.direct_site` | For a declaring type with an explicit assembly scope, identifies the callee's pin, MethodDef token and type arguments using the call link and validated assembly attribution. Apply it to the `bound` field of a method or outcome summary. | +| `BodyDecodes.callManagedTarget` | Resolves the caller's operand through its checked metadata to a MethodDef token and slice position. The caller's MemberRef and the target MethodDef can have different token values. | + +For an extracted method, `DecodedSummary.of_translation` takes `Extracted.translation_valid`, the +record index, the method lookup and the contract proof. The certificate supplies the body decode +and the feature mask used by the summary. + +A separate Lean module can import a compiled `DecodedSummary` and apply `reprofile` to an +independently extracted program. Compile the producer's extraction and summary, then put both +compiled modules on the consumer's Lean search path. Prove that the consumer's MethodDef token +and method evidence agree with the producer's, that its program equals the reprofiled program, +and that the profiles agree on the features the program observes. Replay the two extractions, +the saved summary and the consumer proof together. This works for scalar and vector returns +and for a by-reference write with a proved footprint. + +Use `Extracted.translation_valid.programBindingsValid` for the mask and call checks. Each record's +feature certificate describes its pinned, instantiated body. The decoder checks managed and +constrained calls against the methods and links it returns. For a program with imports, pass +`Extracted.imports` as the final argument to `decodedProgram?` and `programBindingsValid`. +An extraction supplies each `BodiesDecode.cons` premise from its generated facts: +`(Method.ofBody?_of_decodes Extracted.method{n}_body_validated).trans Extracted.method{n}_decoded` +for a method defined from its decoder, and `site` its `ManagedCallsValid` premise from +`Extracted.method{n}_calls_validated` of the caller and its instruction and link premises from a +`bind_call` site's `call` and `link` facts. For a call into a reference assembly, pass +`Extracted.attribution_validated` to `DecodedAt.direct_site` and identify the declaring type's +`headScope`. The scope must be nonempty and contain no colon. This distinguishes definitions in +different assemblies even when both use the same MethodDef token. For an aggregate, Lean reads +the assembly scope before the first `::`, even when generic arguments name types in another assembly. + +The proof of `decodedProgram?_eq_some` adds one +constructor per record, so its cost is linear in the methods of the extraction +([scaling](performance/InstructionLookups.md)). +A summary whose bound names another token, or whose record's bytes decode to another method, does +not type-check, and `site` at a call that names another token proves `False` from the summary. +A call that runs through a runtime model, such as `Vector128.CreateScalarUnsafe` or `GetElement`, +has no link and enters no method of the program, so a callee that makes such calls decodes and is +summarized like any other body. + ### Pass byref arguments with ValidCall `ValidCall memory inputs outputs` states that memory is well formed, every input buffer is @@ -517,8 +608,7 @@ contents separately. For a word store through a named output, `cil_contract_steps` first uses supplied checked operation facts. If none establishes the store, it derives a successful write from the current -`ValidCall` and the output bounds. This covers `store64`, eight-byte `storeScalar` and eight-byte -`setFieldAt`. The continuation names: +`ValidCall` and the output bounds. This covers `stind.i8`, `stind.i`, primitive eight-byte `stobj` and `stfld` of a checked eight-byte value field. The continuation names: | Name | Holds | | --- | --- | @@ -583,11 +673,29 @@ successful snapshot copy does not establish initialization. ### Refute a contract +A refutation states `MethodContract.Refuted`, `OutcomeContract.Refuted` or +`OutcomeContract.RefutedCLR` of the contract, the program and the method, with the arguments +`Holds` or `HoldsCLR` takes. Each carries a counterexample (`CIL.Safety.Counterexample`): an +input and memory meeting the precondition, and either a settled outcome at some fuel that the +contract does not allow, or fuel exhaustion at every budget (`CIL.Safety.Diverges`). Its +`not_holds` gives the negation of the contract. + | Lemma | Use | | --- | --- | +| `MethodContract.Refuted.of_fault` | A checked located definite fault (`ExecutionFault.definite`) on an input meeting the precondition | +| `MethodContract.Refuted.of_return` | A checked return outside the footprint or the postcondition | +| `MethodContract.Refuted.of_diverges` | Fuel exhaustion at every budget, such as a loop that never exits | +| `OutcomeContract.Refuted.of_fault`, `OutcomeContract.Refuted.of_return`, `OutcomeContract.Refuted.of_diverges` | The same for an outcome contract, where a fault other than the named exception, or any return when one is named, violates it; `OutcomeContract.RefutedCLR` has the same three for the CLR reading | | `invoke_ok_unique` | Ties a computed run to the contract's existential one | | `invoke_ok_observation_unique` | The same for one observation of the run, such as the returned values, so the refutation never compares memories | -| `MethodContract.not_holds_of_fault` | Refutes a contract from a checked located fault on an input meeting its precondition. It uses fuel monotonicity to rule out success at every budget; the witness must be a semantic fault; fuel exhaustion is not a witness | + +Every settled outcome is a success or a fault other than fuel exhaustion and `unsupported` +(`Settled`). An `unsupported` fault, such as a hole (`.unadmitted`), marks a run the model does +not cover, and one exhausted budget says nothing about a larger one, so neither is a +counterexample, and no stepping tactic steps past a hole to a return. Such a run can still prove +`¬ c.Holds program method`: no fuel returns. That negation is evidence of a gap in the model, not +of a fault in the method, so `leancil verify` rejects a requested theorem that states it (see +[failure reports](CLI.md#failure-reports)); a lemma that states it is checked like any other. ## Frame class instances @@ -596,8 +704,8 @@ fields instead of offsets. For every field of an admitted class that its methods extraction emits a descriptor under `Extracted.Classes`, such as `Account.Balance : WordField 8`, `Node.Next : ReferenceField` or `Samples.Values : ArrayField .signed32`, carrying the class and the model offset the extractor validated. Each comes with a theorem, checked by -`decide`, that the descriptor matches the operands of an instruction it names -(`Account.Balance_accessed`). A field name is a label; only the class, offset and width or +`decide`, that the method's `Method.fieldShape` of the field an instruction it names accesses is +the descriptor's slot (`Account.Balance_accessed`). A field name is a label; only the class, offset and width or element are relied on. `CIL.Safety.ObjectFields` gives each descriptor its predicates and lemmas: @@ -696,25 +804,25 @@ or references it may change. | --- | --- | | Confine a store to that entry | `Confined.setSlot`, `Confined.storeSlot` | | Keep the reference a field of a different, older object holds | `Confined.objectSlot_of_ne` | -| Step the field accesses | `step_objectReferenceField_eq`, `step_setObjectReferenceField_eq` | +| Step the field accesses | the `cil_step` simprocs, by `step_ldfld_reference_eq` and `step_stfld_reference_eq` | | Reduce them, given the slot's access check, to the held reference and to `setSlot` | `loadSlot_of_access`, `storeSlot_of_access` | | Read `setSlot` back | `setSlot_slots_same`, `setSlot_slots_of_ne` | | `setSlot` leaves bytes, authority and field locations unchanged | `read_setSlot`, `access_setSlot`, `objectFieldAddress_setSlot` | An instance call compiled as `callvirt` steps like a call once its receiver forms as a live -object (`step_callInstance_eq`, `receiverCall_address`), so `cil_contract_steps` composes it +object (`step_callvirt_eq`, `receiverCall_address`), so `cil_contract_steps` composes it through the callee's contract. A null receiver steps to `NullReferenceException` at the `callvirt` itself (`receiverCall_null`). A virtual call steps the same way once the receiver's runtime type is known. Pass a fact `runtimeTypeOf memory receiver = .ok type`, with the definitions of `type` and its assembly -scope; stepping looks the class up in the call's table (`step_callVirtual_eq`, +scope; stepping looks the class up in the call's table (`step_callvirt_eq`, `virtualCall_address`) and enters the listed body ([AnimalLegs](../Examples/Proofs/AnimalLegs.lean)). For an instance the method constructs, the fact is `objectMemory_runtimeTypeOf` ([FreshDogLegs](../Examples/Proofs/FreshDogLegs.lean)). A class the table omits steps to the model fault `outsideDispatchTable` (`virtualCall_outside`). -`castclass` and `isinst` step on the same fact (`step_objectTest_eq`): with +`castclass` and `isinst` step on the same fact (`step_castclass_eq`, `step_isinst_eq`): with `RuntimeType.lineage` and the definitions of the type, its bases and their scope, stepping decides whether the tested class is in the lineage, so the reference passes, becomes null or throws `InvalidCastException` ([DogTest](../Examples/Proofs/DogTest.lean), @@ -781,7 +889,7 @@ predicates in the `ReferenceArray` namespace: | Carry a covariance check across an allocation from well-formed memory | `storableIn_of_confined` | | Let `Confined.forget_fresh` drop the elements of an array the method allocated | `entries_young` | | A fresh array from `newReferenceArray_ok`, live with every element owned and null | `ReferenceArray.fresh` | -| Step the three instructions | `step_referenceArrayLength_eq`, `step_referenceArrayLoad_eq`, `step_referenceArrayStore_eq` | +| Step the three instructions | `step_ldlen_object_eq`, `step_ldelem_ref_i32_eq`, `step_stelem_ref_i32_eq` | | Example | Shows | | --- | --- | @@ -808,9 +916,9 @@ one, whose view validates. | Keep an array whole across the same changes, whatever its elements then hold | `WholeArray.after_confined`, `after_below`, `after_kept` | | Pass a whole array as a call argument, from its view's `valid` | `checkedValue_array` | | Move it through a local, from its view's `valid` | `storeLocal_array`, `loadLocal_array` | -| Step `ldfld` and `stfld` | `step_arrayField_eq`, `step_setArrayField_eq` | -| Step `ldlen` of the loaded array to its length, once its view validates (`whole.valid`) | `step_memory_arrayLength_address_eq` | -| Step `ldlen` of the null array to its throw | `step_memory_arrayLength_null_eq` | +| Step `ldfld` and `stfld` | the `cil_step` simprocs, by `step_ldfld_array_eq` and `step_stfld_array_eq` | +| Step `ldlen` of the loaded array to its length, once its view validates (`whole.valid`) | `step_ldlen_address_eq` | +| Step `ldlen` of the null array to its throw | `step_ldlen_null_eq` | | Example | Shows | | --- | --- | @@ -858,8 +966,9 @@ theorem pair_constructs : ``` The storage does not exist before the step, so a caller names the contract input as a function of -it. `cil_contract_construct holds constructs input [facts]` applies `run_newAggregate_of_contract` -at the fetched `newobj` and leaves: +it. `cil_contract_construct holds constructs input [facts]` applies `run_newobj_aggregate_of_contract` +at the fetched `newobj`, whose `.aggregate` link gives the constructor and the size of the storage, +and leaves: - the caller memory's `WellFormed`; - the precondition over the fresh storage; @@ -883,9 +992,11 @@ struct. ## Name exceptional outcomes -An `OutcomeContract` (`CIL.Safety.OutcomeContract`) adds `raises : Input → Memory → Option String` +An `OutcomeContract` (`CIL.Safety.OutcomeContract`) adds `raises : Input → Memory → Option ExceptionType` to a `MethodContract`: where it is `some exception` the method must end in exactly `.thrown -exception` (`Raises`), and elsewhere the normal clause applies. Fuel exhaustion, unsupported +exception` (`Raises`), and elsewhere the normal clause applies. `CIL.ExceptionType` lists the +exceptions the model raises, such as `.argumentOutOfRange`; each member stands for one type of +namespace `System` in the runtime assembly (`CIL.ExceptionType.typeIdentity`). Fuel exhaustion, unsupported instructions and memory faults never satisfy `Raises`, so a contract cannot be met by getting stuck. Memory after a throw is not observable to the caller, so `Raises` states none; runtime exceptions count only where the semantics models them as `.thrown`. @@ -901,7 +1012,7 @@ def percentContract : OutcomeContract CIL.W64 CIL.W64 where pre _ _ := True footprint _ := [] post v _ _ out := out = v - raises v _ := if v ≤ 100 then none else some "System.ArgumentOutOfRangeException" + raises v _ := if v ≤ 100 then none else some .argumentOutOfRange ``` | Name | Use | @@ -910,10 +1021,13 @@ def percentContract : OutcomeContract CIL.W64 CIL.W64 where | `cil_outcome_call holds input at site [facts]` | Continues after a call the callee's contract says returns, whether the caller then returns or raises | | `cil_outcome_raise holds input at site [facts]` | Closes a `Raises` goal by propagating the exception the callee's contract names | | `MethodContract.Holds.outcomes` | Lifts a normal-only contract | -| `invoke_unique` | Ties a computed fault to the contract's existential one | +| `invoke_unique` | Proves that two stable outcomes of the same invocation agree across fuel budgets, including an interpreter stop at an unsupported operation | + +For a CLR contract refutation, a fault must also be definite. An unsupported operation leaves +the CLR outcome unknown at every fuel budget. [CountUntil](../Examples/Proofs/CountUntil.lean) proves `bumpContract`, which raises -`System.InvalidOperationException` when its two arguments are equal, with `cil_raise_steps` and +`.invalidOperation` when its two arguments are equal, with `cil_raise_steps` and `cil_checked_steps`, then proves its loop returns or propagates that exception through the contract alone. @@ -925,8 +1039,8 @@ normal-return clause, footprint and `raises` field unchanged. Its fixed | Fault | Named CLR exception | | --- | --- | | `.thrown exception` | `exception` | -| `.arithmetic .overflow` | `System.OverflowException` | -| `.arithmetic .divideByZero` | `System.DivideByZeroException` | +| `.arithmetic .overflow` | `.overflow` | +| `.arithmetic .divideByZero` | `.divideByZero` | | Every other fault | None | Checked add, subtract, multiply and integer conversions report overflow through this mapping. @@ -936,7 +1050,7 @@ arithmetic operands, invalid bounds, memory faults, unsupported instructions, in exhaustion have no name in this mode. The mode does not change `Raises` or `.thrown`. `OutcomeContract.Holds.clr` lifts a thrown-outcome proof (`Holds`) to `HoldsCLR`. -For a wide-integer `Divide` or `Mod` contract, select `some "System.DivideByZeroException"` in +For a wide-integer `Divide` or `Mod` contract, select `some .divideByZero` in `raises` when its divisor is zero and prove `HoldsCLR`. Both an explicit throw and the mapped arithmetic failure can establish this exact outcome. The proof must reach that fault through the actual extracted program. `run_fault_raisesCLR`, `run_next_raisesCLR` and `raisesCLR_of_run` @@ -1004,31 +1118,35 @@ yields a normal-only contract. Both `Refines` are reflexive and transitive. ## Inherit a callee's contract in a forwarder -Overload forwarders and instance-to-static wrappers compile to `ldarg*; call k; ret`. +Overload forwarders and instance-to-static wrappers compile to `ldarg*; call m; ret`. `MethodContract.Holds.forward` (`CIL.Safety.Forwarders`) gives such a method its callee's contract with the forwarder's own argument encoding, `contract.withArgs forwarded`, without stepping either body. Given `add_holds : addContract.Holds Extracted.program addTo`: ```lean extracted theorem sum_forwards : sumContract.Holds Extracted.program sum := - add_holds.forward (loads := plainLoads [0, 1, 2]) (narrow := none) sum.found addTo.found - ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) (fun _ _ _ => plainLoads_unchanged _ _) + add_holds.forward (loads := [0, 1, 2]) (narrow := none) sum.found addTo.found + ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun _ => rfl) + (fun _ _ _ => plainLoads_unchanged (by decide) _) (fun _ _ _ _ member => .inl member) (fun _ _ _ => rfl) (fun _ _ _ _ _ => trivial) ``` -`Forwards body calleeBody loads callee narrow` fixes the body to `forwarderCode loads callee narrow` -with no locals, vector argument homes or parameter homes, so frame entry and exit leave memory +`Forwards body calleeBody loads method callee narrow` fixes the body to `forwarderCode loads method`, +a `call` of `method` taking one argument per load (`arity`) and linked to `callee` (`linked`), with `body.returnNarrow = narrow` and no locals or parameter homes, so frame entry and exit leave memory unchanged and the callee's precondition, footprint and postcondition carry over verbatim. The loads -are data: each is an argument index and the narrowing the extractor attaches to `bool`, `byte`, -`sbyte`, `short` and `ushort` parameters (`plainLoads` for wider ones). The lemma has these premises: - -- The callee's arguments are the forwarder's at those indices, each kept by its load's narrowing - (`LoadsUnchanged`). -- Every forwarder argument reaches the callee or is valid under the precondition, since the - forwarder's own invocation checks them all. +are data: each is an argument index, read as `Method.argumentAccess` says, which narrows `bool`, +`char`, `byte`, `sbyte`, `short` and `ushort` parameters, and enums over them, to their declared +width. The lemma has these +premises: + +- The callee's arguments are the forwarder's at those indices, each kept by the forwarder's read + (`LoadsUnchanged body index value`). `plainLoads_unchanged` discharges it for arguments read + without narrowing, and `LoadsUnchanged.narrow` for an i32 already in its declared range. +- Every forwarder argument reaches the callee or is valid and not a span home under the + precondition, since the forwarder's own invocation checks them all. - The call exposes no unspecified vector bits. -- A narrowed `ret (some kind)` does not change the callee's result (`ReturnsUnchanged`), which +- A return with declared narrowing `some kind` does not change the callee's result (`ReturnsUnchanged`), which holds for a boolean. Aggregate results retain their complete byte list. Use `Holds.weaken` afterwards to export a @@ -1047,8 +1165,8 @@ premises over those memories: - The callee's arguments are the forwarder's selected by the loads followed by the home, each kept by its load's narrowing and none with unspecified vector bits. -- `outer.pre` gives a well-formed memory in which every forwarder argument is valid (`checked`), - since the forwarder checks them all. +- `outer.pre` gives a well-formed memory in which every forwarder argument is valid and not a + span home (`checked`), since the forwarder checks them all. - `inner.pre` follows from `outer.pre` with the home fresh, writable and holding its initializer (`LocalHolds`). - The `inner` footprint names fresh storage or lies inside `outer.footprint`. The lemma checks @@ -1105,10 +1223,11 @@ rule; the next section describes relocating a library into the slot. A library extracted on its own calls its helpers by its own method indices. `CIL.Safety.Linking` relocates it into a linked program: -- `Op.relocate f` renames only the method index of `.call`, `.newVector256` and `.newAggregate` - (`Op.callees`); pcs, locals, profiles and static sites are unchanged. -- `Closed lib` says every callee index in `lib` is one of its methods (`Closed.of_check` decides - it). +- `Method.relocate f` renames only the method indices of the link table: the callees of calls, + constructions and managed-call catalogs. Code, pcs, locals, profiles and static sites are + unchanged, since each call reads its target from the table. +- `Closed lib` says every callee of every link-table entry in `lib` is one of its methods + (`Closed.of_check` decides it). - `RelocatesInto lib linked f` says method `i` sits at `f i` relocated by `f`. - `run_relocate` and `invoke_relocate` then give exactly the same result: the same memory and values on return, and on a fault the same pc and fault with only its method index renamed to @@ -1134,6 +1253,36 @@ library exports only its index-0 method. Static identities are not renamed: two that use the same identity for different bytes fault `invalidState` at the second static access, so a collision can make a proof fail and cannot prove a false statement. +## Carry a summary to other programs + +`CIL.Safety.ProgramClosure` takes out of a program the methods one method reaches, so that a +summary proved there binds wherever those methods appear: + +- `Program.closure program root` is `some (lib, indices)`: the methods `root` reaches through link + tables, renumbered from `root` at 0, and the index in `program` of each. It is `none` when a + link of a reached method enters a method outside them. It is `closureWith (program[·]?) + program.length root`, which reads each method from the list, so a search over n methods with + e link callees in a program of P methods costs O(n * P + e) kernel steps. An extraction states + `Extracted.programIndex`, a literal index of its methods, with `Extracted.programLength` and + `Extracted.programIndex_get?`; rewriting by + `Program.closure_eq_closureWith Extracted.programIndex_get? Extracted.programLength_eq` + before `cil_kernel_rfl` or `decide +kernel` brings the search to O(n * log P + e). The + [lookup scaling suite](performance/InstructionLookups.md) pins both costs. +- `Program.closure_closed` gives `Closed lib`, and `Program.closure_relocates` gives + `RelocatesInto lib program` at the indices, with `root` first. +- `Program.closureEvidence` renumbers the records' links and checks the feature certificates + against the closure's method indices. `Program.closureEvidence_decodes` says the records + decode to `lib` when the program's records decode to `program`. Relocation preserves the + reachability mask and the set of pcs occupied by links. `Method.ofBody?_relocate` therefore + applies to the renumbered records. +- `MethodContract.Holds.restrict` carries a contract proved in `program` to the closure, where + `DecodedSummary.of_decoded` binds it to the closure's record. +- `RelocatesInto.of_decoded` places `lib` in a target program whose records decode, at the + indices `f` gives, to the relocated library methods. A map may send two library methods to + one index when both decode to the method there: a call into either runs the method both denote. +- `DecodedSummary.relocate` carries a summary over `lib` to the method `f` places in the target, + bound to the target's record there when it names the same pinned method and type arguments. + ## Reuse framework contracts When extending generic safety proofs, reuse the facts established by the shared dispatch or diff --git a/docs/PROOFS-STEPPING.md b/docs/PROOFS-STEPPING.md index f0197316a..1c228d149 100644 --- a/docs/PROOFS-STEPPING.md +++ b/docs/PROOFS-STEPPING.md @@ -21,7 +21,7 @@ import CIL.Safety.SymbolicExecution namespace ProofGuide @[cil_code] def arithmeticMethod : CIL.Method := { - code := [.arg 0, .arg 1, .const32 1, .shl, .add, .const32 0, .gtu, .ret] + code := [.ldarg 0, .ldarg 1, .ldc_i4 1, .shl, .add, .ldc_i4 0, .cgt_un, .ret] locals := [], returnsValue := true } theorem checked_arithmetic (memory : CIL.Safety.Memory) (left right : CIL.W32) : @@ -92,7 +92,8 @@ starts at the caller's next instruction. `cil_checked_steps`, `cil_contract_step stepping there. With literal fuel and a target equal only up to rewriting, use `cil_checked_steps n [facts]` and prove the residual `run` equation. `MemoryBelow.frame_round_trip` carries caller memory across `enterFrame` and `leaveFrame` when - the callee body keeps it. + the callee body keeps it. For a method contract with an empty footprint, `Confined.frame_round_trip` + uses the same preservation fact and the growth of the allocation counter during the call. ### Lookups @@ -153,7 +154,7 @@ strategy: | Unknown opcode heads | The equation path | `cil_contract_step [facts]` prepares `facts` as `cil_checked_step` does, so a supplied `read` steps -a `.loadAggregate` through a managed-reference address there as well. +a decoded `ldobj` through a managed-reference address when its type has aggregate storage. Pass facts about formation and successful memory operations instead of evaluator definitions or monadic binds. The tactics accept these fact forms: @@ -161,8 +162,9 @@ monadic binds. The tactics accept these fact forms: | Situation | Fact | | --- | --- | | A branch on a symbolic condition | `v = 0` or `Not (v = 0)` on the condition value; no `CIL.truth` restatement is needed. A literal condition, such as the `1` a decided feature test pushes, decides itself and continues at a numeral program counter | -| An opcode index stated as closed arithmetic, such as `.local (↑(6 : Fin 10) + 1)` | Fact preparation also states it at the numeral, `.local 7`, the form a fetched opcode has | +| An opcode index stated as closed arithmetic, such as `.ldloc (↑(6 : Fin 10) + 1)` | Fact preparation also states it at the numeral, `.ldloc 7`, the form a fetched opcode has | | A read stated at a closed width, such as `read memory home (localWidth (.aggregate 32)) 1` | Fact preparation derives its load at the numeral width an opcode carries | +| A successful write, such as `write memory target (numberBytes word.toNat 8) 1 = .ok after` | Fact preparation derives the step of each store that writes those bytes: `stind.i8` for eight bytes, plus `stind.i` when a body the goal or a hypothesis reaches may hold one; narrow `stind` and typed scalar transfers for one, two or four | | A value known only as `argument : CIL.Value`, loaded as an argument or passed to a call | `numericValue argument = true`; preparation also states it over the unfolded definition, the form the checked value test has when the step reaches it | | `bitcastByteBool`, whose operand must be `0` or `1` | None for a comparison result `if c then 1 else 0`; for any other int32, `bits.toNat ≤ 1`. The fact may bound a value inside width changes, as `field.toNat ≤ 1` does for the operand `(field.setWidth 8).setWidth 32` | | A symbolic reference | Its formation equation; the equation stays usable without splitting the reference | @@ -195,6 +197,36 @@ with numeric locals is not a closed term; `bind_entry ... allocating` supplies i and home facts instead (see [loop scaffolding](PROOFS-BINDING.md#generate-loop-entry-and-cutpoint-scaffolding)). +A closed run through an indirect call through a method pointer, `calli`, is proved by +`cil_kernel_rfl` on the equation `(CIL.Safety.invoke program fuel method args memory).map +Prod.snd = .ok results`: the kernel evaluates the run, including the comparison of the pointer's +owner string with each catalog entry's, and the elaborator evaluates none of it. `CIL.Safety.step_ldftn_eq` pushes the pointer’s checked metadata identity and signature. +An indirect call uses `CIL.Safety.step_managed_call_eq`, which states the step over `CIL.Safety.indirectTarget`: +the catalog body selected for the pointer, or `none`, which faults with `invalidState`. Supply +the selection as a fact `CIL.Safety.indirectTarget signature catalog pointer = some method` +proved by `cil_kernel_rfl`. `simp only [cil_step, fact]` then leaves the operand guard, the +arity check and the checked arguments, which reduce as for a direct call once the signature's +parameter count is a numeral. + +A closed proof by `rfl` or `cil_kernel_rfl` walks the method list at each interpreter step. +Use `rw [CIL.Safety.invoke_eq_invokeWith Extracted.programIndex_get?]` before evaluating an +invocation, or `CIL.Safety.run_eq_runWith` for a run. Each method lookup then takes logarithmic +time in the program length. Stepping simplifies method lookups with the generated +`Extracted.program_method{n}` equations, other `cil_code` rules and the facts supplied to the +tactic. A rule can describe the lookup function before it receives its index. Supplying the +program name keeps its method equations available without expanding the list for each lookup. +If simplification leaves the goal open, the stepper tries the registered equation at that index +against the original lookup. For proofs, use the list equations `CIL.Safety.run_zero`, +`CIL.Safety.run_succ` and `CIL.Safety.invoke_eq`. For functional +induction over a list program, specialize the induction principle to its method lookup. + +```lean +import CIL.Safety.Execution + +variable (program : CIL.Program) +#check CIL.Safety.runWith.induct (lookup := fun i => program[i]?) +``` + At a fetched `.ret`, narrow returns included, the tactic leaves the equation between the computed checked result and the stated result as a goal over the returned values, with no unexecuted `run`. The goal closes automatically only when `with_reducible rfl` succeeds after frame cleanup, list @@ -217,6 +249,9 @@ unchanged. The tactic keeps the lemma's result form, then removes literal stack `reverse` and append expressions before the next step. These rewrites do not simplify consumer arithmetic or unfold consumer value definitions. +`step_ldstr_eq` reads the caller’s binding for the decoded string. A bound literal pushes its +object reference and preserves memory. An unbound literal stops with an unsupported outcome. + Value-only operations (checked arithmetic, conversions and division) have one equation per i32/i64 operand shape, generated by `cil_value_steps` when the library builds. The stepper has no dispatcher-unfolding fallback for them. For a hand-written step, use the corresponding equation @@ -224,9 +259,12 @@ directly or `simp only [cil_step, facts]`. Reference and field instructions have one `@[cil_instruction_step]` equation per operand shape in [InstructionSteps](../CIL/Safety/InstructionSteps.lean), which rewrites a step to checked memory -and formation operations. They cover the `instruction` arms of `load64`, `store64` and -`storeScalar` through a reference, `fieldAt` and `fieldAddress` on an address, `setFieldAt` of -width 1, 2, 4 or 8, `fieldAt` of width 4 or 8 on a struct value, `asRef`, `skipInit` and `initobj`. +and formation operations. They cover the `instruction` arms of `ldind` and `stind` through a reference, `ldind.i` on a native method table cursor, `ldlen` and +`ldelem.i` of a managed handler table, `skipInit` and `initobj`. +For `ldfld` and `stfld`, the stepper resolves the field operand in the method's layout environment. +The checked shape supplies the offset, width and signedness. For a +struct `ldflda`, the checked field offset and a pointer-addition fact give the resulting writable +or readonly reference. - Supply successful encoded byte reads and writes directly: the stepper derives the scalar or vector bridge before stepping. Narrow scalar reads support signed and unsigned results. Facts @@ -238,10 +276,30 @@ width 1, 2, 4 or 8, `fieldAt` of width 4 or 8 on a struct value, `asRef`, `skipI abstraction directly. - The set is separate from `cil_step`: a hand-written `simp only [cil_step, facts]` stops at `instructionStep`. A `.call` step has its own `cil_step` equation, so a call checks its - arguments without unfolding the dispatcher. + arguments without unfolding the dispatcher. Its target is the body's link at the call's pc, + which the stepper reads from the generated link fact of a named body or by evaluating the table + of a literal body or of a named body that unfolds; only an opaque body needs the link as a + supplied fact. Every call-family step (`.call`, `.callvirt`, `.newobj` + and `.calli`) rewrites with its link fact, the generated one or else a supplied + `body.linkAt pc = link`, in the same pass as its equation, so only the arm of the link's kind is + simplified. A `.call` with no link runs its method's runtime model; `cil_runtime_models body` + states `body_model{pc} : runtimeModel body.environment .call method = some model` at each such + pc of a named body, and the `reduceRuntimeModel` simproc evaluates the lookup for a literal or + template body, or for any environment when the method reference is closed. With both facts the stepper rewrites by `step_call_model`, and a directly linked + `.call` by `step_call_direct`, so the kernel checks one equation whose right side no longer + reads the link and a linked call never mentions the model; `step_call_eq` remains for a call + whose link no fact decides. `cil_checked_steps` steps a call without a link on in the same + method. For a resolved intrinsic, `step_call_intrinsic` rewrites the decoded call to + `intrinsicCall`. It checks availability in the method's profile and the operands the + intrinsic observes. The stepper applies supplied evaluation facts to symbolic operands. For a checked call to `Unsafe.SkipInit`, the + stepper uses the caller's address-formation fact to pop the byref argument and preserve memory. + For a symbolic managed reference, `step_call_skipInit_reference` exposes the formation check. Aggregate loads and stores use the address-shape equations in -[AggregateMemorySteps](../CIL/Safety/AggregateMemorySteps.lean). A checked `readSnapshot` supplies +[AggregateMemorySteps](../CIL/Safety/AggregateMemorySteps.lean). For an aggregate `stobj`, +the operand type's checked layout determines its width. `step_stobj_initialized_eq` uses the +ordinary byte write and `step_stobj_snapshot_eq` preserves the initialization flag of each byte. +A checked `readSnapshot` supplies the actual initialization mask; it does not establish that every byte is initialized. Initialized byte reads and writes reuse the corresponding snapshot bridges. Null faults retain the instruction's requested width. @@ -259,7 +317,7 @@ checked `read`, `write`, `add` and formation equations instead of evaluator defi `add` fact on a known address needs no extra reference-conversion fact. - Manual proofs can apply the matching opcode or operation equation directly. -A `.feature f` step pushes `body.profile.evaluate f`. For a known feature, +A decoded call to a hardware feature getter pushes `1` or `0` according to `body.profile.evaluate f`. For a known feature, `cil_step` reduces that expression to the profile field; a symbolic `f` stays `body.profile.evaluate f`, so a fact about it applies as stated. `cil_step` unfolds a method definition or literal only as far as its profile, so a fact such as `profile.avx2 = true` decides @@ -269,6 +327,38 @@ profile projection, including one quantified over every feature, is normalized t Elsewhere, including `simp only [cil_step, facts]` and `cil_checked_step_equations`, state the fact on the field or first run `simp only [CIL.Safety.reduceMethodProfile, cil_feature] at h`. +A `.ldarg index` step reads the argument as `body.argumentAccess index` says. `cil_step` takes that +access from the method's `cil_resolution_facts` fact, from a literal body's homes and signature, or +from a supplied fact such as `body.argumentAccess 0 = .value none`, and steps by +`step_ldarg_value_eq`, `step_ldarg_narrow_eq` or `step_ldarg_home_eq` +([StepComposition](../CIL/Safety/StepComposition.lean)), so the goal shows the incoming value, +its narrowing or the load of the home slot. An access that none of these decides leaves the match +of `loadArgument` on `body.argumentAccess index`, which a later fact decides. A `.starg index` or +`.ldarga index` step reads the access the same way: a home steps by `step_starg_home_eq` or +`step_ldarga_home_eq` to the store to or address of the home slot, an argument without a home by +`step_starg_value_eq` or `step_ldarga_value_eq` to the unsupported fault, and an undecided access +to `storeArgument` or `addressArgument`. Under a home, `step_ldarg_home_ldloc`, +`step_starg_home_stloc` and `step_ldarga_home_ldloca` state each argument instruction as the +local instruction of its slot. `step_ldloca_eq` pushes what `localAddress` gives, which for a +span slot is `.spanHome index`; a span model that reads the home steps by its `_home_eq` +equation, such as `step_memory_spanLength_home_eq`, to the same model on the slot's view, and a +call argument steps through `callArgument`, which `cil_checked` reduces to `checkedValue` for +every value but a home. A supplied `memoryInstruction` or `staticInstruction` fact for a span +operation states the slot's view in place of the home: stepping matches the fact against the stack +with each home read from its frame slot, and reads the home before the fact rewrites the +operation. + +A `.ldsfld field` step pushes the value `staticLoad body pc memory` returns, and `.ldsflda field` +or `.ldtoken (.field field)` the reference `staticAddress body pc memory` returns +([StepComposition](../CIL/Safety/StepComposition.lean)); `.stsfld` faults. Both read the +descriptor `body.staticAt pc` binds to the pc. `cil_resolution_facts` declares +`methodN_staticAtPC : methodN.staticAt PC = some descriptor` for each static site. From it the +stepping tactics state a load's result with `staticLoad_initialized` when extraction fixed the +value, and an address's with `staticAddress_site` +([StaticMemoryLemmas](../CIL/Safety/StaticMemoryLemmas.lean)), so no step evaluates the site +table. A load of an opaque value read from the caller's memory takes its result from a fact, +supplied or, for the loop tactics, in the context; a supplied fact on the read wins over the site. + ### Consumer stepping regressions The public tactics handle store and load facts, branch conditions, memory operations, profile @@ -342,13 +432,87 @@ Neither tactic assumes a memory or availability premise. `LoopReaches` uses the relation with an exact-target postcondition. For numeric-local framing, record successful write equations in `SegmentLocalEffects`. The -destinations must occur in `segmentStores code`, read from the placed instructions. -`Segment.frameLocals` combines these effects with the initial `LocalSnapshots` and `WritableHomes` -to derive snapshots for `untouchedLocals code known`. Effects compose through `append` or `trans`; +destinations must occur in `segmentStores body code`, read from the placed instructions: the slot +of each `stloc` and the home slot of each `starg` to a homed argument. `Segment.frameLocals` +combines these effects with the initial `LocalSnapshots` and `WritableHomes` to derive snapshots +for `untouchedLocals body code known`. Effects compose through `append` or `trans`; one frame-rule application replaces separate disjoint-read proofs for all retained locals. The numeric slot identities must remain fixed, and the frame-rule placement must belong to the running method and end at the contract boundary. Indirect writes and calls need a proved memory effect; -absence of a direct `setLocal` is not evidence that an aliased byte-backed local keeps its value. +absence of a direct `stloc` is not evidence that an aliased byte-backed local keeps its value. + +`SegmentRun.compareRuntimeTypes` composes two `ldtoken` and `GetTypeFromHandle` pairs with a +checked type comparison call. Supply the five instruction lookups, the call links and runtime +model equations, and the scoped comparison fact `methodN_typeIdentityP` for its final pc `P`. +The result gives the comparison bit on the original stack and preserves the frame and memory +for every continuation fuel. + +### Contracts for segment exits + +`CIL.Safety.SegmentContract` describes an incoming boundary state, permitted memory writes and +an exit predicate. Its precondition can constrain the arguments, stack, frame, allocation +lifetimes and memory authority. `SegmentContract.Holds` requires interpreter evidence for every +permitted input, together with confinement to the declared footprint and the postcondition. +See [segment contracts](../CIL/Safety/SegmentContract.lean). + +`SegmentExit` distinguishes a continuing boundary, a return, a located fault and divergence. +`SegmentExit.Raises` names CLR exceptions through `ExecutionFault.clrException`, retaining +the distinction between thrown and arithmetic faults. Fault exits are definite faults, so they +exclude fuel exhaustion and `unsupported` faults; divergence requires exhaustion at every fuel budget. Returns compare the memory after frame +release. `SegmentState.localFootprint body code` names the byte homes and widths of the slots that +`segmentStores body code` lists. + +`ReachesRun` relates stable outcomes (`Stable`: any outcome but fuel exhaustion) before and after +a prefix, including prefixes with calls. +`ReachesRun.call` takes the caller stack and the callee arguments separately. Its checked step +premise establishes argument order and the retained stack, including a constructor's receiver. +A reaching exit supplies continuation equivalence and a positive fuel lower bound. Its boundary +state need not be an observed execution state, and any promised state invariant belongs in the +postcondition. `SegmentContract.Holds.wp` requires a successful continuation or return to prove +success; `SegmentContract.Holds.raises` consumes a named exception or a raising continuation. +`Segment.contract` converts an exact segment between increasing pcs when its postcondition +justifies the footprint. `ReachesRun.congr_region` transports reaching through the existing +closed-region agreement rule. + +`SegmentExit.diverges_of_recurrent` proves divergence when each state satisfying a loop invariant +reaches another such state at a positive fuel cost. The counter, frame, stack and memory may +change between visits. `Diverges.of_reaches` carries this result from a reachable instruction +back to method entry so `MethodContract.Refuted.of_diverges` can use it. + +`DecodedSegmentSummary` binds a nonempty instruction sequence to its starting position and +certified method record. Its `holds` field gives the segment contract with its original frame, +stack, footprint and exits. Use `DecodedSegmentSummary.congr_region` with a checked binding for +the destination, agreement on the closed region and a proof that every permitted continuation +lies inside it. A consumer can reuse compiled summaries of both arms when a conditional branch +changes before them. Apply `SegmentContract.branch_seq` to the new branch's step equation and +the two destination contracts. Reversing `brtrue` to `brfalse` selects the opposite result while +each load-and-return region preserves the same caller memory. The agreement includes method metadata, +links and every instruction the region can execute. Use `DecodedSegmentSummary.then` for +adjacent regions when the first reaches the second's precondition. Supply a footprint covering +both regions' writes. The result binds their concatenated instructions and relates the initial +and final states through the state at the join. + +A reusable summary takes the containing method and a `CodeAt` premise for its exact instructions. +Instructions that read the method's declared signature, environment, links or runtime profile +require the corresponding facts in the theorem. Branch targets depend on the placement pc. +Changing a required instruction or fact makes the proof application fail kernel checking. +The theorem also depends on the library definitions of `run` and `step`. + +`SegmentContract.Holds.seq` composes a head with a contract at each reachable pc. The link +premise proves the successor's precondition. For a stopping exit, the stopping premise receives +confinement to the head's footprint and must establish both confinement to the composed footprint +and the composed postcondition. For a reaching exit, the join premise receives both contracts' +confinement facts and must establish the same two conclusions. Unused pcs can use +`SegmentContract.unreachable`. + +`SegmentContract.Holds.then` specializes composition to two continuing contracts. It requires +the composed footprint to contain each part's footprint, with the second evaluated at the +intermediate state. Composition applies the named contract proofs without stepping their code. + +`SegmentContract.branch` uses a conditional instruction's checked step equation to expose its +selected successor while preserving memory. `SegmentContract.branch_seq` composes that branch +with the successor contracts. Its join premise receives successor confinement and establishes +confinement to the composed footprint together with the composed postcondition. ## Step loops @@ -427,8 +591,11 @@ The tactics of [LoopFrameSteps](../CIL/Safety/LoopFrameSteps.lean), which `CIL.Safety.LoopScaffold` imports, walk these goals while keeping the loop's `LoopFrame` current. All gather facts as the other loop tactics do, so their lists are optional: -- `cil_loop_advance [facts]` steps to a call or return and `cil_loop_finish [facts]` through a - return to its postcondition. Each takes a struct zeroing, field store or numeric local store on +- `cil_loop_advance [facts]` steps through runtime-model calls and stops at a linked call or + return. Where the step of a `call` or `newobj` without a link does not close, it shows the + step of the site's runtime model, and at a site with neither a link nor a runtime model it fails, + naming the pc. `cil_loop_finish [facts]` steps through a return to its postcondition. + Each takes a struct zeroing, field store or numeric local store on the way as `cil_loop_recurse` does and fails as `cil_loop_steps` does on anything else. At the return `cil_loop_finish` names what leaving the frame keeps, `exitConfined` and `exitBelow` (`LoopFrame.leave`), carries the caller-buffer facts to the exit memory, and closes each @@ -436,7 +603,7 @@ All gather facts as the other loop tactics do, so their lists are optional: facts prove; the rest are left as goals, tagged by their place among the conjuncts (see [obligation tags](#obligation-tags)), with those facts in context. - `cil_loop_call holds input at site [facts]` applies the contract (`loop_terminates_call`) at - the `bind_call` site or, without `at site`, at the fetched call. It leaves the precondition, + the `bind_call` site or, without `at site`, at the fetched linked call. It leaves the precondition, tagged `precondition.`, unless `trivial` or a hypothesis proves it, then continues with these names in context: the returned memory `called`, the typed output `output` and, from the call's `CallResult`, its invocation `invoked`, footprint fact `confined` and postcondition @@ -461,7 +628,9 @@ All gather facts as the other loop tactics do, so their lists are optional: Each store names its memory, step and frame (`stored`, `storing`, `storedFrame`, `storedBelow`), and the tactic restates each `Contents`, `Filled` and `Writable` hypothesis about the stored-over memory, or about the caller's, at the new one under the same name, so an invariant reads them at -the memory it is stated at. +the memory it is stated at. The restated fact replaces the earlier one when the stored-over memory +is one the walk introduced; a fact about a memory the theorem or the author introduced stays in +scope, whatever that memory is named. [CountCalls](../Examples/Proofs/CountCalls.lean) proves a counted loop this way without unfolding the helper or naming its fuel. @@ -553,7 +722,7 @@ invariant state of smaller measure. | Exit at the loop's own `throw` | `loop_outcome_throw` | | Compose paths in either outcome | `loop_outcome_next`, `loop_reaches_outcome` | | Lift the head outcome through frame setup | `ensures_outcome_of_loop`, to `EnsuresOutcome pre post raises`, which names the outcome of every permitted input; with `raises` constantly `none` it is `Ensures` (`ensures_outcome_none`) | -| Refute an expected exception | `loop_outcome_some_refuted`, from any settled run that does not throw it | +| Refute an expected exception | `loop_outcome_some_refuted`, from any settled run that does not throw it; a run that ends in fuel exhaustion or an `unsupported` fault is not settled | The normal-only rules are the `none` instances of the outcome rules. Unsupported execution, invalid state and fuel exhaustion never satisfy an expected loop exception. @@ -580,7 +749,7 @@ type, so an unrelated context adds nothing to simp. It gathers: - the declarations in scope with the `cil_loop_unfold` simp attribute, which `bind_cutpoint` gives its state and `bind_entry` its frame and normalized result and memory; - equations, possibly quantified or conjoined, about `step`, `instruction`, `memoryInstruction`, - `staticInstruction`, `loadLocal`, `storeLocal`, `loadValue`, `storeValue`, `form`, `add` and + `staticInstruction`, `staticLoad`, `staticAddress`, `loadLocal`, `storeLocal`, `loadValue`, `storeValue`, `form`, `add` and the span `element`, `slice`, `validate`, `width` and `writable` checks; - for a [`CheckedSpan`](../CIL/Safety/SpanContents.lean), its `facts`, `length_nil` or `length_cons` when its values are `[]` or a cons, and, for each `LoopFrame` from its memory @@ -597,10 +766,10 @@ type, so an unrelated context adds nothing to simp. It gathers: - `elementWidth` of a `SpanContents`; - for a [`LoopFrame`](../CIL/Safety/LoopFrames.lean) over `knownLocals` of a list, and for the entry frame `bind_entry ... allocating` proves for every footprint, the `form` of each home - (`LoopFrame.form_home`), so `ldloca` steps, and the load of each remembered home through - `LoopFrame.load`, with `localResult`; + (`LoopFrame.form_home`), so `ldloca` and a homed `ldarga` step, and the load of each + remembered home through `LoopFrame.load`, with `localResult`; - for a struct local remembered as `.aggregate (layout.encode fields)`, the read of each field by - value (`ldloc` then `ldfld`, [`step_fieldAt_encode`](../CIL/Safety/FieldValues.lean)) and by + value (`ldloc` then `ldfld`, [`step_ldfld_encode`](../CIL/Safety/FieldValues.lean)) and by address (`LoopFrame.load_field`), as that field's value; - [`LoopFrame.leave_kept`](../CIL/Safety/LoopFrames.lean) of each `LoopFrame []` with the entry's `setup` equation, when the goal is an exit; @@ -609,7 +778,7 @@ type, so an unrelated context adds nothing to simp. It gathers: Only hypotheses reachable by name are read: a shadowed state of an earlier iteration and hygienic hypotheses are skipped. A fact is kept only when stepping can meet every variable it is about. [`loopGoalScope`](../CIL/Safety/LoopFacts.lean) starts from the goal's states and -postcondition, and from the method arguments only when the program reads one with `.arg`; the +postcondition, and from the method arguments only when the program reads one with `.ldarg`; the right side of each kept equation then extends it. A derived fact must be about variables reached without an equation. A fact about a memory that no state of the goal holds and no kept equation produces, such as the caller's memory once the frame has stored, is dropped; a frame fact at a @@ -618,9 +787,9 @@ carries, such as a span's validity, so a span local loads after a word local's s facts are added as hygienic hypotheses, and a statement already gathered is not added again. `SpanSum` names no fact but its invariant equation. -Each instruction is given only the facts it can read: home forms reach only `ldloca`, field reads -only `ldfld`, and the negative sides of case splits only instructions that test or compute on -data, while `ret` reads them all. +Each instruction is given only the facts it can read: home forms reach only `ldloca` and +`ldarga`, field reads only `ldfld`, and the negative sides of case splits only instructions that +test or compute on data, while `ret` reads them all. A loop over symbolic caller memory keeps the frame part of its state as the [`LoopFrame`](../CIL/Safety/LoopFrames.lean) that `bind_entry ... allocating` returns, in a @@ -682,20 +851,80 @@ The lemmas of `CIL.Safety.StepFlow` give the possible successors of a successful unfolding it. `step_next_target` proves that an action that stays in the frame resumes at `pc + 1` of a continuing instruction or at one of `op.targets`. Given a successful action and its selected -callee, `step_callee` proves membership in `Op.callees`: a singleton for a direct call or -construction, or all authenticated targets for an indirect call. Tail transfers enter a callee -without continuing in the current frame. `step_call_action` and `step_newVector256_action` / -`step_newAggregate_action` separate a call from a construction. The module imports only the step -simulation, so a consumer reasoning about reachable pcs does not compile the import and linking -layers. +callee, `step_callee` proves that the step's instruction reads its link (`Instruction.readsLink`) and +that the callee is among `body.calleesAt pc`, the callees of the link at the step's pc: a singleton +for a direct call or construction, the dispatch table's bodies for a virtual call, or all +authenticated targets for an indirect call. Tail transfers enter a callee +without continuing in the current frame. `step_call_action` and +`step_newobj_aggregate_action`, which takes the `.aggregate` link of a `newobj`, separate a call from a +construction; `step_call_action` also gives a call whose pc states no link, whose runtime model +continues at `pc + 1` in the same frame, or, when the model runs a span constructor in place, with +the span slot its home names set to a span of the slot's width and access (`modelStep_frame`). +The module imports only the step simulation, so a consumer reasoning about reachable pcs does +not compile the import and linking layers. ## Proof cost Prefer a small `change` or an instruction equation over repeatedly unfolding `run`, `CIL.Safety.step` and `CIL.step`. For generic opcode proofs, split the opcode and stack shape before -splitting operand constructors, and close definitionally equal cases early. Share the remaining -family-level reasoning. For concrete execution, compose steps first and simplify the resulting value -equation separately when needed. +splitting operand constructors, and close definitionally equal cases early. `CIL.step` matches on +the instruction and on `CIL.widenMixed` applied to the stack, so `simp only [CIL.step]` needs +`CIL.widenMixed` and `CIL.nativePureBinary` in its simp set for every arm that matches the stack; +the smallest case is `.div` on `b :: a :: rest`. Share the remaining family-level reasoning. For +concrete execution, compose steps first and simplify the resulting value equation separately when +needed. + +`cil_checked_steps` in every form (including the snapshot forms), `cil_contract_steps` in every +form, `cil_raise_steps`, `cil_loop_steps`, `cil_loop_advance`, `cil_loop_finish`, +`cil_loop_recurse` and `cil_loop_outcome_advance` keep, for each simplification of a run, the +terms that simplification visited and left unchanged. A later step of the same run returns such a +term as it is instead of visiting it again, so the simplifications of a step visit the terms it +produced and not the values the run has built. The goals a step leaves are those that plain +simplification leaves. `set_option cil.stepping.record false` turns the record off; the +simplifications then still run through `cil_simp_only`, so their messages and info trees differ +from those of `simp only`. A run starts to use the record after `cil.stepping.warmup` steps (12 +by default), since the goal of a short run is small; a step is one stepping call, and a loop +instruction takes up to four. + +The record holds one set of terms for each list of supplied facts the run steps with, so a loop +walk whose instructions read different groups of facts keeps them all. Each set is keyed by the +facts as elaborated, a lemma with implicit arguments or universe parameters included, and by the +hypotheses in scope when it was made. A step that only adds hypotheses, as a call does, keeps the +set; a step that rewrites, renames or clears a hypothesis, which a snapshot or loop-frame store +does when it supersedes a fact, a step that fails and the end of the tactic drop it. A fact that +does not elaborate makes its step run without the record, which `trace.CIL.record` reports. A +step run outside these tactics, such as a single `cil_checked_step`, keeps none. + +The simplifications of a step prove an equation theorem's side conditions from the simp set and +never from a hypothesis, so a simplification gives the same result whatever else is in scope. A +run builds its simplification contexts once, indexes the equation hypotheses that `cil_lookup` +tries in a discrimination tree over their left sides as each is added, and runs the tactics of a +step on goals of their own instead of inside `by` blocks of a term, which Lean would rebuild the +whole local context for. A lookup in a step tries only the hypotheses whose left side may unify +with it at reducible transparency. The tree reads a term after the reducible unfolding of local +and reducible definitions that unification applies. A left side or lookup that unification may +equate with a term the tree keys apart is not read by the tree: natural-number arithmetic that +unification evaluates, a structure constructor it matches against projections, and a value of a +type with one value. Such a hypothesis, or every hypothesis for such a lookup, is tried when the +head of its left side may meet the lookup's, so a lookup in a step finds every hypothesis that a +lookup outside a run finds. A lookup outside a run, or with `cil.stepping.record` off, reads each +hypothesis in scope once and tries those whose left side has the lookup's head, a head that is not +a constant, or a head that reducible unfolding replaces; it builds no index, which would cost more +than one lookup. Each read of the record checks that the goal's local context is the one +the record was made for; a goal that keeps that context object, as the goals within a step do, +passes without reading a hypothesis. A step that adds or clears hypotheses, as a store or a call +does, compares the whole context a few times, so the work of a step is at most linear in the +hypotheses in scope with a small constant. The `stepping` suite bounds that constant at 100 +heartbeats per hypothesis per instruction and the cost per instruction between sizes at 1.35 +times, and bounds a lookup outside a run at 100 heartbeats per hypothesis in scope. Comparing contexts costs time but no heartbeats, so the automation tests also count the +context positions a walk compares and allow at most one whole context per instruction. + +A snapshot or loop-frame walk adds the same hypotheses at every store: the store's own seven +named hypotheses, or its memory, step and frame facts, and the memory variable, with a number of +restated facts that does not depend on the stores before it. Each restated fact replaces the one +at a memory the walk made, which the walk clears; a fact at the caller's memory stays. On a body +whose values grow, each step also unifies the step lemma with the whole state, so the cost per +instruction rises slowly with the program counter. To compare the cost of two versions of a proof, measure each with fresh Lean processes and prebuilt dependencies, alternating the runs serially on the same machine. Record elapsed time, CPU diff --git a/docs/PROOFS.md b/docs/PROOFS.md index 5c14cefd0..9ca454886 100644 --- a/docs/PROOFS.md +++ b/docs/PROOFS.md @@ -77,7 +77,7 @@ input and `site` a `bind_call` name. | A `Raises` goal | `cil_raise_steps [facts]` | [Outcomes](PROOFS-CONTRACTS.md#name-exceptional-outcomes) | | A call whose callee has an `OutcomeContract` | `cil_outcome_call`, `cil_formed_outcome_call`, `cil_outcome_raise` | [Outcomes](PROOFS-CONTRACTS.md#name-exceptional-outcomes) | | A loop path to the next head | `cil_loop_recurse hypothesis values [facts]` | [Loops](PROOFS-STEPPING.md#compose-a-helper-call-inside-a-loop) | -| A loop path to a call or return | `cil_loop_advance [facts]` | [Loops](PROOFS-STEPPING.md#compose-a-helper-call-inside-a-loop) | +| A loop path to a linked call or return | `cil_loop_advance [facts]` | [Loops](PROOFS-STEPPING.md#compose-a-helper-call-inside-a-loop) | | A call inside a loop | `cil_loop_call holds input at site [facts]` | [Loops](PROOFS-STEPPING.md#compose-a-helper-call-inside-a-loop) | | A loop exit and its postcondition | `cil_loop_finish [facts]` | [Loops](PROOFS-STEPPING.md#step-loops) | | The progress goal of a loop verification condition | `cil_loop_continue next [facts]`, `cil_loop_finish [facts]` | [Loop VCs](PROOFS-STEPPING.md#loop-verification-conditions) | @@ -99,6 +99,8 @@ These commands name parts of an extraction: | A checked frame entry and a loop cutpoint | `bind_entry`, `bind_cutpoint` | [Loop scaffolding](PROOFS-BINDING.md#generate-loop-entry-and-cutpoint-scaffolding) | | A local by its C# name | `local_named sum "carry"` | [PDB names](PROOFS-BINDING.md#pdb-local-names-and-source-locations) | | Per-pc instruction equations of a parametric body | `cil_instruction_equations` | [Instruction equations](PROOFS-BINDING.md#instruction-equations-for-parametric-bodies) | +| A method defined from its decoder, with kernel-checked facts | `cil_decoded_method` | [Decoded methods](PROOFS-BINDING.md#methods-defined-from-their-decoder) | +| How every method resolves a field or type operand | `cil_field_shape`, `cil_value_shape` | [Field and value shapes](PROOFS-BINDING.md#field-and-value-shapes) | ## Public proof surface @@ -112,7 +114,7 @@ import-closure size guarantee. Import these separately when a proof uses them: | --- | --- | | State checked execution | `step`, `run`, `invoke`, `Execution`, `WP`, `Ensures` | | Compose instruction and segment proofs | `cil_checked_step`, `cil_checked_steps`, `CodeAt`, `Segment`, `Segment.trans`, `cil_segment` | -| Use a proved helper | `MethodContract`, `CallResult`, `FrameInvariants`, `run_call_of_contract_formed`, `cil_contract_steps` | +| Use a proved helper | `MethodContract`, `CallResult`, `FrameInvariants`, `run_call_of_contract_formed`, `cil_contract_steps`, `DecodedSummary` | | Describe byref arguments and overlap | `ArgumentView`, `ValidCall`, the `CallingFields` lemmas | | Preserve mixed local contents | `LocalSnapshots`, `LoopFrame`, `LoopFrame.after_local_write` | | Prove checked loop paths | `LoopReaches`, `LoopSpec`, `cil_loop_steps` | diff --git a/docs/RUNTIME-PROFILES.md b/docs/RUNTIME-PROFILES.md index f46b3541b..0888f59f4 100644 --- a/docs/RUNTIME-PROFILES.md +++ b/docs/RUNTIME-PROFILES.md @@ -11,11 +11,18 @@ the selected profile: the deployment must match the recorded capabilities. ## Evidence -The runtime sources pin this policy to .NET 10: - +The runtime sources pin this policy to .NET 10, except for the x64 baseline, which follows .NET 11: + +- [InstructionSetDesc.txt, v11.0.0](https://github.com/dotnet/runtime/blob/v11.0.0/src/coreclr/tools/Common/JitInterface/ThunkGenerator/InstructionSetDesc.txt) + puts SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2 and POPCNT in X86Base, and its base x64 group is + `x86-64-v2`. [cpufeatures.c, v11.0.0](https://github.com/dotnet/runtime/blob/v11.0.0/src/native/minipal/cpufeatures.c) marks a CPU that + lacks any of them invalid, and [codeman.cpp, v11.0.0](https://github.com/dotnet/runtime/blob/v11.0.0/src/coreclr/vm/codeman.cpp) then stops + the runtime with an error naming them. On other CPUs codeman.cpp enables X86Base whenever + EnableHWIntrinsic is set, and [clrconfigvalues.h, v11.0.0](https://github.com/dotnet/runtime/blob/v11.0.0/src/coreclr/inc/clrconfigvalues.h) + defines no EnableSSE42 switch. A .NET 11 x64 process therefore reports SSE2, SSSE3, SSE4.1 and + SSE4.2 all on, or all off under `DOTNET_EnableHWIntrinsic=0`, never SSE2 alone. - [InstructionSetDesc.txt, v10.0.13](https://github.com/dotnet/runtime/blob/v10.0.13/src/coreclr/tools/Common/JitInterface/ThunkGenerator/InstructionSetDesc.txt) - assigns SSE/SSE2 to X86Base, SSSE3/SSE4.1/SSE4.2 to one group, BMI1/BMI2 to AVX2, - and AVX-512 F/DQ/VL to one group. Avx512Vbmi and Avx512Vbmi_VL belong to the separate + assigns BMI1/BMI2 to AVX2 and AVX-512 F/DQ/VL to one group. Avx512Vbmi and Avx512Vbmi_VL belong to the separate AVX512v2 group, which implies AVX512; the `x86-64-v4` group is `x86-64-v3` plus AVX512 only. - [codeman.cpp, v10.0.13](https://github.com/dotnet/runtime/blob/v10.0.13/src/coreclr/vm/codeman.cpp) enables groups from CPU capabilities and runtime switches. On ARM64 it enables @@ -36,7 +43,7 @@ The runtime sources pin this policy to .NET 10: Fresh-process observations are checked in: -- [Windows x64, .NET 10.0.14, AMD Ryzen 9 9950X](evidence/runtime-profiles/windows-x64-net10.json). +- [Windows x64, .NET 11.0.0, AMD Ryzen 9 9950X](evidence/runtime-profiles/windows-x64-net11.json). - [macOS ARM64, .NET 10.0.14, GitHub `macos-15` runner](evidence/runtime-profiles/macos-arm64-net10.json). Each record includes the actual runtime version, process architecture, OS, switch @@ -49,16 +56,15 @@ dotnet run --file scripts/ProbeRuntimeProfiles.cs -- observations.json The probe launches a new runtime process for each configuration and removes the tested DOTNET_/COMPlus_ switches before applying that case. It records observations; it does not certify a runtime, install one, or assume missing hardware is present. -These measurements do not claim coverage of Mono, NativeAOT, .NET 11 or every -future .NET 10 servicing release. Recheck runtime changes before extending the +These measurements do not claim coverage of Mono, NativeAOT or every future +servicing release. Recheck runtime changes before extending the supported deployment boundary. ## Constraints | Capabilities | Constraint | | --- | --- | -| X86Base.X64, SSE2 | Equal; x64 only | -| SSSE3, SSE4.1, SSE4.2 | Equal; require SSE2 | +| X86Base.X64, SSE2, SSSE3, SSE4.1, SSE4.2 | Equal; x64 only | | AVX | Requires SSE4.2; may be enabled without AVX2 | | AVX2, BMI1, BMI2 | Equal; require AVX | | AVX-512 F, F.VL, DQ, DQ.VL | Equal; require AVX2 | @@ -69,12 +75,12 @@ supported deployment boundary. | Vector128 acceleration | Equal to SSE2 or AdvSimd | | Vector256 acceleration | Requires AVX2; may be disabled independently | -On the measured x64 runtime, EnableSSE42, EnableAVX, EnableAVX2 and EnableAVX512 -disable whole groups and their dependents. EnableAVX512v2=0 disables VBMI and VBMI.VL +On the measured x64 runtime, EnableAVX, EnableAVX2 and EnableAVX512 disable whole groups and +their dependents, and EnableSSE2, EnableSSSE3, EnableSSE41 and EnableSSE42 have no effect. EnableAVX512v2=0 disables VBMI and VBMI.VL and leaves AVX-512 F/DQ/VL enabled, which is exactly x64-v4; EnableAVX512=0 disables both. The default configuration on the measured Ryzen 9 9950X reports VBMI, so it matches x64-v4 plus `avx512Vbmi`, not x64-v4. It also reports AES by default; EnableAES=0 disables AES and -AES.X64 alone, leaving SSE4.2, AVX2 and AVX-512 enabled, and EnableSSE42=0 leaves AES enabled. +AES.X64 alone, leaving SSE4.2, AVX2 and AVX-512 enabled. Legacy individual switches such as EnableBMI2 and EnableAVX512DQ have no effect. PreferredVectorBitWidth=128 disables Vector256 acceleration without disabling AVX2. On the measured ARM64 runtime, @@ -84,11 +90,13 @@ together; the tested individual ARM switches do not split that group. ## Profile selection Pass a built-in name or `@profile.json` as the profile argument of `leancil extract` -([CLI](CLI.md)); the default is `scalar`. The built-in profiles are `scalar`, `x64-sse2`, +([CLI](CLI.md)); the default is `scalar`. The built-in profiles are `scalar`, `x64-sse42`, `x64-v3`, `x64-v4` and `arm64-advsimd`; the [profile table](SIMD.md#feature-getters-profiles-and-execution-restrictions) lists the flags each -enables. `scalar` is the no-hardware-intrinsics profile, and `x64-sse2` is the x64 baseline -with SSE4.2 and higher groups disabled. +enables. `scalar` is the no-hardware-intrinsics profile, and `x64-sse42` is the x64 baseline +(`x86-64-v2`) with AVX and higher groups disabled. Both validators reject an x64 profile that +sets `x86Base64` without all of `sse2`, `ssse3`, `sse41` and `sse42`, and the extractor's +message names the missing sets. The built-in list does not name every valid configuration. Use custom JSON for SSE4.2+AVX without AVX2 and for v3/v4 with @@ -138,8 +146,8 @@ AES group, is unclassified. Custom profiles must obey the same constraints and supply every capability field, including `avx512Vbmi`, `aes` and `armAes`, each with the value the deployment reports. Set AVX2/BMI1/BMI2 together; set all four modeled AVX-512 flags together; set `avx512Vbmi` only with -them; set `aes` only on x64 and `armAes` only on ARM64; set all three modeled SSE4.2-group flags -together. ARM64 profiles must set ArmBase/AdvSimd/Vector128 together and cannot enable Vector256 +them; set `aes` only on x64 and `armAes` only on ARM64; set `x86Base64`, `sse2`, `ssse3`, +`sse41` and `sse42` together. ARM64 profiles must set ArmBase/AdvSimd/Vector128 together and cannot enable Vector256 acceleration. A reduced-width x64 profile may set `vector256Accelerated: false` with the full AVX2 group enabled. diff --git a/docs/SIMD.md b/docs/SIMD.md index b6cbb6b83..b89fed0ff 100644 --- a/docs/SIMD.md +++ b/docs/SIMD.md @@ -58,6 +58,9 @@ the model's semantics and makes no claim about how a JIT implements them. | `Vector128.Sum`, `Vector256.Sum` | Vector of `ulong` | Wrapping sum of the 64-bit lanes | | `AsByte`, `AsUInt32`, `AsInt64`, `AsDouble`, `AsUInt64`, `AsInt32`, `AsSingle` on `Vector128` / `Vector256` | Source element type is `ulong`, `byte`, `uint`, `long`, `int` or `float`; target is the named type | Preserve the bit pattern. The extractor does not accept `double` as a source for these casts | +A `CreateScalarUnsafe` or `GetElement` call keeps its `call`, which runs the vector operation the +runtime model table gives its method reference; every other row is emitted as its operation. + No other element types or operators are implied. In particular, portable vector multiplication is present as a Lean operation but has no generic vector-operator mapping in the extractor. There is no Vector64 support, Vector512 is limited to bitwise/equality operators, the rows naming it and the [vector storage](OPCODES.md#vector-storage-and-fields) instructions, and there is no general vector load/store API coverage beyond the byte-span `Create` and `Vector128` `LoadUnsafe`/`StoreUnsafe` rows. `Vector256.Create(ReadOnlySpan)` and the other element types of `LoadUnsafe`/`StoreUnsafe` are not mapped. The specific vector `ldobj`/`stobj` cases are documented in the [opcode table](OPCODES.md). ## SSE @@ -143,7 +146,7 @@ The pointer-output `MultiplyNoFlags` overload is not mapped. The .NET 10 CoreCLR | --- | --- | --- | --- | | `X86Base.X64.DivRem` | `ulong lower, ulong upper, ulong divisor` -> `ValueTuple` | `X86Base64` | Divide `upper * 2^64 + lower` by `divisor`; Item1 (`Quotient`) is the quotient and Item2 (`Remainder`) the remainder | -The precondition is `upper < divisor`, so the quotient fits in 64 bits. Otherwise the instruction raises #DE, and the checked interpreter fails with a located arithmetic fault, never a wrapped result: `.divideByZero` for a zero divisor and `.overflow` for any other `upper >= divisor`, matching CoreCLR's `DivideByZeroException` and `OverflowException`. `CIL.Safety.divRem_fields` proves that Item1 and Item2 of a successful call are the `Nat` quotient and remainder, and `CIL.Safety.divRem_fault` that every input violating the precondition faults. The result is a 16-byte aggregate with Item1 at offset 0 and Item2 at offset 8; extraction requires the recomputed `ValueTuple` layout to match it. The 32-bit `X86Base.DivRem` and the signed overloads are not mapped. The API is marked experimental (`SYSLIB5004`) in .NET 10, and only this exact signature is recognised. +The precondition is `upper < divisor`, so the quotient fits in 64 bits. Otherwise the instruction raises #DE, and the checked interpreter fails with a located arithmetic fault, never a wrapped result: `.divideByZero` for a zero divisor and `.overflow` for any other `upper >= divisor`, matching CoreCLR's `DivideByZeroException` and `OverflowException`. `CIL.Safety.divRem_fields` proves that Item1 and Item2 of a successful call are the `Nat` quotient and remainder, and `CIL.Safety.divRem_fault` that every input violating the precondition faults. The result is a 16-byte aggregate with Item1 at offset 0 and Item2 at offset 8; extraction requires the recomputed `ValueTuple` layout to match it. The 32-bit `X86Base.DivRem` and the signed overloads are not mapped. The API is marked experimental (`SYSLIB5004`) in .NET 11, and only this exact signature is recognised. ## AES @@ -163,7 +166,7 @@ Byte `i` of the vector is AES state byte `i` (row `i % 4`, column `i / 4`), as w | `AdvSimd.ReverseElement8` | `V128` -> `V128` | `AdvSimd` | REV64 on bytes: reverse the bytes of each 64-bit lane, as `BinaryPrimitives.ReverseEndianness` does, without moving the lanes | | `ArmBase.Arm64.MultiplyHigh` | `ulong, ulong` -> `ulong` | `ArmBase64` | High 64 bits of the unsigned 128-bit product | -No other AdvSimd operations or `ReverseElement8` overloads, signed `MultiplyHigh` overload or ARM ISA family besides [AES](#aes) is mapped. No ARM64 host runs the REV64 check; the intrinsics suite compares it with `ReverseEndianness` of each lane. Portable Vector128 operations can be used under an ARM profile when their signatures match the first table. +No other AdvSimd operations or `ReverseElement8` overloads, signed `MultiplyHigh` overload or ARM ISA family besides [AES](#aes) is mapped. The intrinsics suite checks REV64 against `ReverseEndianness` within each lane and runs the compiled method on hosts that support AdvSimd. Portable Vector128 operations can be used under an ARM profile when their signatures match the first table. ## Feature getters, profiles and execution restrictions @@ -172,14 +175,13 @@ Recognised `IsSupported` getters are `X86Base.X64`, `Sse2`, `Ssse3`, `Sse41`, `S | Built-in profile | Enabled capabilities | | --- | --- | | `scalar` | No hardware feature flags | -| `x64-sse2` | `X86Base64`, `Sse2`, `Vector128Accelerated` | -| `x64-sse42` | The above plus `Ssse3`, `Sse41`, `Sse42` | +| `x64-sse42` | `X86Base64`, `Sse2`, `Ssse3`, `Sse41`, `Sse42`, `Vector128Accelerated` | | `x64-v3` | The above plus `Avx`, `Avx2`, `Bmi1`, `Bmi2`, `Vector256Accelerated` | | `x64-v4` | The above plus `Avx512F`, `Avx512FVL`, `Avx512DQ`, `Avx512DQVL` | | `arm64-advsimd` | `ArmBase64`, `AdvSimd`, `Vector128Accelerated` | Profiles target .NET 10 CoreCLR, and both the extractor and the Lean certificate reject capability -sets that split its runtime groups. [Runtime profiles](RUNTIME-PROFILES.md) gives the group +sets that split its runtime groups or, on x64, fall below the .NET 11 baseline (`x86-64-v2`). [Runtime profiles](RUNTIME-PROFILES.md) gives the group constraints, the custom-profile rules and the measured x64 and arm64 runtimes. Changing a profile requires fresh extraction and proof verification; certificates from another profile do not acquire its capabilities. diff --git a/docs/SOUNDNESS-EVIDENCE.md b/docs/SOUNDNESS-EVIDENCE.md index c311a0f6d..fbb71a26c 100644 --- a/docs/SOUNDNESS-EVIDENCE.md +++ b/docs/SOUNDNESS-EVIDENCE.md @@ -4,36 +4,429 @@ ## Independently checked extraction evidence -`TranslationValid rawLayouts computedLayouts methodEvidence imports program` checks one record -per emitted method in program order. The verifier kernel-replays and structurally audits -`Extracted.translation_valid`; an unrelated theorem, non-theorem or different program/import table +`TranslationValid rawLayouts computedLayouts assemblies platformDefinitions methodEvidence imports +program` checks one record per emitted method in program order. It checks every body against the +same pinned assemblies and platform definitions. The verifier kernel-replays and structurally +audits `Extracted.translation_valid`; an unrelated theorem, non-theorem or different program/import table cannot substitute for it. | Evidence | Independently checked relation | Remaining assumption or limit | | --- | --- | --- | -| Layouts | Recompute layouts from raw fields, packing and size; check required use sites and bounds | Faithful metadata/serialization and runtime layout correspondence | +| Layouts | Require one row per type identity, recompute field placement and size, and check use sites and bounds | Faithful raw metadata, including the byref-like declaration marker, and runtime layout correspondence | | Specialization | Substitute closed method/owner arguments through signatures, locals, operands and calls, including the owner segment of an external call identity (`scope::result owner::member`, open owners spelled `!!n`/`!n`); preserve control flow | Faithful raw definitions and admitted constraints | | Feature flow | Entry, transfers, joins, required successors, reachable mask and feature-dependent operations | No complete concrete CLI simulation for every opcode | -| Scalar flow | Complete stack kinds, raw maximum stack, joins/backedges, call arguments/results, homes and returns | Unknown types remain opaque; decoding is not proved | +| Method body decoding | Decode the bytes the extractor emitted for a body (tiny or fat header, every standard opcode, branch targets as instruction indices) through the token table and compare `maxStack`, `initLocals`, the locals and every instruction with the original definition | That the bytes are the method's body in the image (reconfirmed by the opt-in [deep verification](CLI.md#deep-verification)) and the PE container stay trusted; bodies with exception sections are rejected | +| Metadata binding | Decode the table rows, heap entries and signature blobs each method carries and check every token-table entry, every local type and the method's own definition against them, including generic import descriptors and their open signatures and closed arguments | That the carried bytes are the image's, that each platform definition states its type as the platform assembly defines it, public key tokens, imported implementation definitions, raw layout content, dispatch catalogs, static data and generic parameter constraints | +| Scalar flow | Complete stack kinds, raw maximum stack, joins/backedges, call arguments/results, homes and returns | Unknown types remain opaque | | Scalar translations | Exact literal values, arithmetic, comparisons, conversion flags and branch targets | Other operations require specialized checks or retain stack-effect fallback | | Managed/imported calls | Target slot, scoped identity, full signature, argument count, receiver and result | An import slot does not prove its implementation contract | +| Call links | The emitted link table's keys ascend and are pcs of the code, and the link each pc reads from it, by one walk beside the code, is the one the instruction there implies; so the table is the one the emitted instructions imply, entry by entry | The interpreter does not read the link table, so a link adds no behavioral claim beyond its instruction | +| Declared signature | The method's receiver kind, parameter types and return type equal those of its closed raw method | Faithful raw metadata | +| Environment | Every method's environment is the program's raw and computed layout tables, which `LayoutsValid` recomputes | Layout correspondence, as for Layouts | | Constrained calls | Resolve serialized interface/override declarations to the emitted closed direct target | Complete metadata serialization; no general vtable/boxing model | | Static values | Checked execution yields emitted bytes without circular reads of the values being justified; unknown values have no initializer and are read only from the caller's binding | Initializer recognition and deployment initialization/immutability | +For each external call operand in the body's token table, `BodyDecodes.externalTarget` binds +the typed reference and the target identity to the same resolved metadata member. A generic +import may use the closed contract spelling of that member. Hardware family selection reads the +closed owner, metadata name, signature and type arguments. The hardware row binds its own open +declaration and external spelling before the decoder keeps the call. + +For a supported `MemoryMarshal` member, `RuntimeModelsAgree` checks the kept call against its +declaration, closed operand and checked layouts. Both element widths of `Cast` and the transfer +kind of the read and write operations come from the model. At a site with no link, a missing +overload or layout leaves an opcode-tagged hole that the validator accepts only at an unreachable +site. At a linked site the decoder keeps the call, and `ManagedCallsValid` refuses the method +because no import slot binds a generic `MemoryMarshal` member. + +The extractor binds reachable managed calls from the resolved target graph. It records runtime +model sites during metadata validation and reads class field descriptors from their CIL operands. +Ordinary extraction emits the decoder inputs and evidence. Shared templates and proof outlines +also use per-instruction descriptors to compare methods and name their operands. For arithmetic, +comparisons, conversions and stack operations, proof outlines read the opcode from Cecil. The Lean +decoder checks the corresponding method bytes. + +The evidence proves that each method's entire instruction list equals the decoding of its closed +body. At a reachable instruction outside a fold, the scalar validator uses this equality for +value encodings when the decoder supplies an instruction. Folded sites retain their explicit +site checks. +Closed method construction refuses the first reachable hole with its pc and raw opcode. +Dead sites retain their opcode-tagged holes. + +`MethodEvidenceValid.modelsAgree` derives runtime call agreement for the whole method. +Closed decoding rules out reachable holes. The feature certificate fixes the mask and code +lengths, and `RuntimeModelsAgree.of_decoded_site` relates each instruction to its catalog entry. +A span +reset must have its matching local address for the decoder to produce an initialization. + +Lean derives opaque vector locals from the closed raw body and the reachability mask. +A live call to the exact `Vector128.CreateScalarUnsafe(ulong)` overload selects +unaddressed `Vector128` slots in declaration order. Even a dead address +instruction excludes its slot. `OpaqueLocalsValid` independently checks the result against the +emitted code and slot kinds. + Exact scalar matching covers integer literals, wrapping/checked add/subtract/multiply, signed and unsigned divide/remainder, bitwise operations, shifts, negation, integer comparisons, admitted integer conversions and all short/long compare-and-branch encodings. Literal ranges and high-bit-set int64 values, conversion width/signedness/checkedness and exact branch targets cannot -pass by stack effect alone. Non-covered raw opcodes cannot emit these scalar operations, except -feature-bound type-test words and size constants bound to exact primitive/vector widths or -validated layouts. +pass by stack effect alone. Type-test folds and runtime queries can emit scalar constants when their site checks pass. +A `sizeof` instruction pushes the width of its closed operand. An `Unsafe.SizeOf` call retains +its call instruction and obtains its result from its runtime model. + +For a span indexer or pinnable reference, scalar flow checks the same open declaration, closed +signature and external target as the model before classifying the result as a reference. The +readonly declaration requires the `InAttribute` modifier on the by-reference result. -Mixed native/int32 operations require the exact `nativeBinary` wrapper; homogeneous operands forbid -it. Native/int64 arithmetic mixtures are rejected. The 64-bit representation interchange at homes +Indirect accesses are matched exactly too. Each integer `ldind` and `stind` must emit the +`ldind` or `stind` of the kind its opcode names, which `Instruction.ofRaw?` decodes, and so must +each `newarr`, `ldlen`, integer `ldelem.*` and `stelem.*`, typed `ldelem` and `stelem`, `ldelema`, +`ldelem.ref` and `stelem.ref`, a handler table's `ldlen`, `ldelem.i` and `ldelem` included. A typed +access or element address takes its element's width from the element shape of the method's checked +layouts. An `ldelema` decodes read-only exactly when `readonly.` precedes it, and `readonly.` +decodes to `nop`. Closed method construction requires the next raw instruction to be +`ldelema` with a type operand, even at a dead site. The scalar check also applies +`readOnlyPrefixMatches` to every reachable prefix. Object loads, stores, initializations, copies +and sizes decode with their type tokens. +`ObjectOperandsValid` checks the storage shape for each distinct live opcode and operand. +For methods with aggregate object instructions, the extractor supplies the named shape fact at +each site in instruction order. `ObjectOperandsValid.of_sites` checks this sequence against the +raw body and proves that it covers every distinct operand. +The scalar validator binds each literal opcode and type to its site. Only a proved decoding +can skip that comparison. A span load requires a direct source of the same span type. A span +reset uses the checked local fold. Block copies and initializations retain the alignment of +their prefix, with an alignment of eight for an unprefixed operation. `volatile.`, an +`unaligned.` alignment other than 1, 2 or 4, and `unaligned.` before an instruction that does +not take it are rejected. + +Each `ldfld`, `ldflda` and `stfld` of an instance field of a struct, or of a class that is not +a generic instance, must emit the field instruction of the same opcode over the operand's field +reference, which `Instruction.ofRaw?` decodes, and the step resolves the field's offset and +width from the method's checked layouts (`CIL.Method.fieldShape`). `ldfld` and `stfld` decode +after `unaligned.` to the same instruction as without it, since the model gives a field access no +alignment of its own; `ldflda` after `unaligned.` and any of the three after `volatile.` do not +decode. The scalar check requires a struct field load to read a struct reference or value, a +struct field address or store to go through a reference, and a stored value to suit the field. + +Mixed native/int32 operations execute the decoded arithmetic opcode with operand widening. +Native/int64 arithmetic mixtures are rejected. The 64-bit representation interchange at homes and returns does not justify arithmetic interchange. Native shift counts use their low 32 bits; the normalization theorem does not establish behavior on arbitrary CLI implementations. See [runtime assumptions](SOUNDNESS-RUNTIME.md#runtime-assumptions). +### Method body decoding + +`CIL.Extraction.decodeSingleCatch` reads a fat method body with one small typed catch clause. +It checks the class token against the supplied metadata and the protected and handler ranges +against the decoded instruction boundaries. It accepts aligned sections with nonempty, +disjoint ranges. The public execution checker still rejects methods with exception sections. + +Each method's evidence holds the bytes the extractor emitted for its body (the header and the +code, not extra sections) and a token table. `BodyDecodes` decodes the bytes in the kernel +([BodyDecoding](../CIL/Extraction/BodyDecoding.lean)) and requires the result to equal the +original definition's `maxStack`, `initLocals` and `code`, and checks the definition and locals +against the metadata it carries ([Metadata binding](#metadata-binding)): + +- The header is a tiny header or a fat header of three words that sets at most the init-locals + flag. A body with extra sections (exception handlers) is rejected, and the supplied bytes must + be exactly the header and the declared code size. +- Every opcode of the ECMA-335 table has one operand encoding. The table is transcribed from the + standard; `operand_kinds_agree_with_cecil` in `Tests/BodyDecoding.lean` compares it with a + static list generated once from `Mono.Cecil.Cil.OpCodes`. An unknown opcode byte, a truncated + operand or a token missing from the table is an error. +- A branch must land on the first byte of an instruction inside the body, and its operand + becomes that instruction's index. A target outside the body, inside an instruction or on the + instruction after a prefix opcode (`unaligned.`, `volatile.`, `tail.`, `constrained.`, `no.`, + `readonly.`) is an error, whether or not any path reaches the branch. Decoded code therefore + satisfies `PrefixEntryFree` over instruction indices (`checkBody_prefixEntryFree`), and so + does the instantiated code of every method whose evidence is valid + (`MethodEvidenceValid.prefixEntryFree`). The extractor refuses such a body with the blocker + "Branch enters the instruction after a prefix". +- Direct raw method construction enforces the same branch-entry rule. Both raw construction + and method shape evidence check each prefix and its taker, including unreachable sites. + A constrained static call requires a static virtual method on an interface. Lean binds that + claim to the declaring type and method flags in the carried metadata. A tail transfer + requires matching return types and an adjacent return. +- A token operand is read from the four bytes in the body, looked up in the table, and must + belong to a metadata table the opcode accepts. A token has one entry, and a field access names + a static or instance field as its opcode requires. + +Only the kernel decides `BodyDecodes`: the elaborator's `decide` is refused at once with `failed to reduce`. +Extractions prove it per method as `method{n}_body_validated`, whose `bodyMatches` part uses `cil_body_decodes`: +it runs `decide +kernel` and, when the kernel refutes it, reports the method and either the error the check +raised or the part of the decoded body that differs from the emitted definition. The kernel keeps nothing between +declarations, so the lookup tables of the method's own metadata slice are written out as a literal (see below) +and the method's check reads them by name: its cost is its own bytes and tokens times a logarithm of its slice, +whatever the number of methods or rows of the extraction. + +In the default flow Lean does not establish that the emitted bytes are the method's body in the +assembly image: System.Reflection.Metadata locates the body in the PE image, and the PE container +stays trusted. The opt-in [deep verification](CLI.md#deep-verification) reconfirms it in the +kernel for assemblies whose digests you state: each body's bytes must be the bytes at the file +offset that the section table gives for the RVA in the image's own MethodDef row for that method. +The carried rows have that RVA column zeroed, so the audit's comparison of a carried row with the +image's masks it. What the tokens, the locals and the method definition mean is checked against the +metadata rows the body carries, as the next section describes. + +A closed generic instantiation carries the bytes of its open definition, in its own `method{n}Bytes`, +and its own metadata slice, so its component names no other method. A token used at several sites +has one table entry, and the extractor refuses a token whose sites resolve to different operands. +An external call spells its identity as a materialized name at a reachable site and as a literal +at an unreachable one; the kernel decides that the two are equal strings, so one entry serves +both. + +### Metadata binding + +Each method carries its own `method{n}Metadata`, a `MethodMetadata` +([MetadataBytes](../CIL/Extraction/MetadataBytes.lean)): the raw bytes of the table rows and heap +entries that its own tokens, definition, signature and locals reach, with one slice per assembly it +reaches. Each pinned assembly has one small block, `assembly{k}Metadata`, with its table stream header +bytes, the header they decode to (heap sizes and the row count of each present table) and its `Assembly` +row, a function of that assembly alone, so every extraction of it writes the same bytes. +`assembly{k}_identified` proves once per assembly that the bytes decode to the stated header, which every +method's checks then read without decoding it again, and that the identity its `Assembly` row spells is +the string the block states (`assembly{k}Identity` holds those bytes). A method's `BodyDecodes` equates its own +assembly's identity with its pin, lists the identity of every pinned assembly (`pinned`, equated with +`Extracted.assemblies`) and names the identity each of its slices spells. + +Every byte list of this evidence (body bytes, rows, heap entries, header bytes and UTF-8 names) is written +as `CIL.Extraction.fixedBytes count code` ([FixedDigits](../CIL/Extraction/FixedDigits.lean), specified in +[ByteLiterals](../CIL/Extraction/ByteLiterals.lean)): its length +and one number holding the bytes big-endian. The code of a list of at most 256 bytes is one hexadecimal numeral, +and a longer list's code joins numerals of at most 256 bytes with `Nat.lor` and `Nat.shiftLeft`. Elaborating a +numeral costs a few allocations, where a list literal costs a numeral and a constructor per byte; the kernel +computes the list when a check reads it, splitting the code in halves down to words of eight bytes, and the +checks see the same list values. + +### Names tied to bytes by equations + +A name the extraction states is a `String`; Lean reads the same name as heap bytes. A decision that +compared them would evaluate the string literal in the kernel, about ten thousand heartbeats for each +character, so the comparison is an equation instead: `spelled?` turns bytes into the string they spell as +well-formed UTF-8 in one pass ([KernelEquation](../CIL/Extraction/KernelEquation.lean)), and the tactic +`cil_kernel_rfl` proves `spelled? bytes = some name` by `Eq.refl`, which the kernel accepts when both sides +reduce to the same term. The tactic adds the proof as an auxiliary lemma first, assigns it only after the +kernel accepts it, and otherwise fails at the goal with both sides. `spelled?` agrees with `bytesOf`, the +bytes of a string literal, so the two kinds of comparison in the checker mean the same. Indices, ranges, flags +and blobs stay kernel decisions. The equations cover the owner and base class names of a method +(`MethodNames.Spell`), the method's own assembly against its pin, and each assembly's identity; the method's +signature string is equated the same way. +The checker and the raw-image audit both require a row to fit in 24 bits before forming its +token. This also applies to simple index columns and to rows computed while walking a table. +A method's evidence is a function of the method and its assemblies and of nothing else in the extraction, +so a verified method's component is reused by every extraction that holds the method. The `RVA` column of every carried `MethodDef` row is zero: where a +body sits in the image moves whenever an earlier body changes length, so no method's evidence may depend +on it. The same holds for `#Strings` and `#Blob` offsets, which move when an earlier name changes length: +the extractor renumbers them, in the rows and the heap keys, to the rank of each carried entry among the +carried entries by bytes (the image's own entry order is a hash or suffix sort that one changed name can +reshuffle), and Lean reads a heap entry only through the offset a row names. (`#US` offsets are named by the body's own `ldstr` +tokens and stay the image's.) The extractor records each body's address as `bodyAddress` in `identity.json`, +outside the Lean evidence, for the strict audit. A slice also holds links, each naming for a token the definition it resolves to, its declaring type, +its `NestedClass` row or its first `GenericParam` row. Lean reads the rows by the widths the header +fixes, decodes strings, blobs and user strings, and decodes the signature blobs +([Signatures](../CIL/Extraction/Signatures.lean)). `MetadataResolution` and `MetadataBinding` then +check what the extractor states. A coded index must have a valid table tag and a row number +that fits in 24 bits. Both readers require each signature to consume its entire blob. + +Building the lookup trees of a slice costs its size, and the kernel keeps no cache between declarations. The +extractor therefore writes them out as the literal `method{n}Tables` (balanced search trees of the rows, heap +entries, links and instance field counts), and `method{n}_tables_valid` states that `tablesMatch`: the decoder +builds exactly that literal from `method{n}Metadata`. That decision costs the method's carried rows times a +logarithm; `method{n}_body_validated` uses it as a premise. Each closed instantiation of a generic method has +its own copy, built from the open definition and the assembly, so reuse does not depend on which +instantiations an extraction holds or in which order. + +A field access carries the `Field` rows of its owner from the first up to the accessed field, and the +owner's and the next type's `TypeDef` rows; rows carried only to be counted keep their flag bytes and name +no heap entry. Lean counts the instance fields before the accessed one over the carried rows, which must +run unbroken from the owner's first field, so a gap fails at the lookup. The total number of instance +fields of a type is recorded only when every field is carried: base classes of a class receiver, the +owner of an instance method, and enums carry their whole field lists. + +What `MetadataResolution` and `MetadataBinding` check: + +- The scope of every type is the assembly identity its `AssemblyRef` row spells (name, version, + culture, key token); nested types are named `Outer/Inner`, and the namespace and simple names of a + type and of each type enclosing it stay apart in the comparison between a reference and its + definition, so namespace `A.B` with name `C` is not namespace `A` with name `B.C`. A type defined + in a carried slice has the value type, enum (with its integer type), class or interface kind its + flags and base give. To recognize `System.ValueType`, `System.Enum` or the `System.Object` at + the end of a class hierarchy, Lean checks the base TypeRef's own AssemblyRef. It must name + `System.Runtime`, `netstandard`, `mscorlib` or `System.Private.CoreLib` with neutral culture + and the publisher token for that name. The full reference version is part of the type identity. + For a carried .NET 11 CoreLib definition, the chain ends at the public, nongeneric, fieldless + `System.Object` whose `TypeDef` has no base. CoreLib supplies the scope of its own primitive + signatures. In a method declaration or call signature, an `ELEMENT_TYPE_OBJECT` parameter + admits an object reference named `System.Object` in that signature's primitive scope. + For a nongeneric call into another pinned module, Lean checks primitive parameters and + results against the caller's core assembly in the MemberRef declaration and the callee's + core assembly in the closed signature. + The field index counts the instance fields of each class below `System.Object`. +- A reference whose scope has the name of a pinned assembly, at any version, culture or key token, + needs the pinned assembly's slice among the method's slices; without it the check fails with + `missingSlice`, so a pinned type or call, or one of another version of a pinned assembly, is + never read as a platform one. +- Each `.type`, `.field`, `.call`, `.indirectCall` and string operand of the token table is + checked against its token. The extractor chooses among `atom`, `object`, `aggregate` and `enum` + for a type, and Lean accepts the choices that name the same scoped type with a form its kind + allows. For a closed generic aggregate, the identity must match the resolved instance, and every + argument must be a primitive value, an enum, a value-like named type or another value-like + instance whose arguments meet the same rule. Lean rejects an argument it resolves as a class at + any depth, including when an array or a byref contains the aggregate. For an unpinned platform + type, Lean trusts the signature's kind marker. A `VALUETYPE` marker can therefore admit a class + from that assembly as a value argument when the aggregate identity matches. + An instance field access needs its owner's definition in a carried slice, which gives the + field's index. Lean also binds the two fields of a platform ``ValueTuple`2`` when its generic + signature marks the head as a value type and its own `TypeRef` names the supported + `System.Runtime` assembly with the exact version, culture and public key token. + The extractor reads the marker on each signature use. Both arguments must use primitive + element codes for integers, booleans or chars of the same storage width + (1, 2, 4 or 8 bytes, including 8 for native integers). + The field references must declare `Item1` as `!0` and `Item2` as `!1`, or the check fails + with `operandMismatch`. Any other instance field of an owner outside the + carried slices, and any other member of the pair, fails with `unboundInstanceField`. A static + field access by `ldsfld`, `ldsflda`, `stsfld` or a field `ldtoken` likewise needs its owner's + definition in a carried slice and otherwise fails with `unboundStaticField`; the one exception is + System.Runtime's `BitConverter.IsLittleEndian`, admitted only as an `ldsfld`, which the feature + check reads as the profile's endianness constant. A static field named through a generic + instantiation of its owner fails with `instantiatedStaticField`, since each closed instantiation + has its own storage. The descriptor of the static site at an access must name the instruction's + field operand as its `field`. For ordinary statics, `Method.ofClosed?` reads the descriptor + from `RawMethod.staticFields` and checks its owner, field index, type, name and scalar storage. + It checks a literal against its bytes. An initializer call needs the complete program, where + `StaticValuesValid` executes the recorded callee and checks the result against those bytes. + Both `decodedProgram?` and `TranslationValid` require `StaticValuesValid` and + `StaticIdentitiesValid` for the complete program. A standalone closure must include each + initializer needed to reproduce its static bytes at the index the descriptor records. +- The decoder records the instruction index and token of each static access whose field a carried + row defines, and the body must list the same ones (`BodyEvidence.statics`). Over the whole + program, `StaticIdentitiesValid` requires two static sites to share an identity exactly when + their tokens name the same `Field` row of the same assembly, so the model's storage keyed by + identity is one storage per field. Two sorts of the S sites decide it in O(S log S) comparisons, + and `StaticIdentitiesValid.pairs` states what it guarantees. + A field's name, index and static flag, + the generic arguments of a type or method instantiation, + the full calling-convention byte and parameter and return types before and after generic + substitution, the definition a member reference resolves to + in a pinned assembly (its name, its signature and its place in its type's members) and the + external identity of a call (the scope and Cecil's `result Owner::name(parameters)`) are + recomputed from the bytes. + A call to a generic member requires a `MethodSpec` with exactly as many arguments as the + member declares. A call to a nongeneric member names the member directly. + At each live modelled call, the runtime model entry also checks the member's open declaration. + An open declaration distinguishes an owner's `!0` from a method's `!!0` even when both + instantiate to the same type. The generic vector lane reader must return its method parameter. + Each Unsafe transfer overload binds the raw target and open declaration to the closed address + type. The unaligned overloads distinguish a byte reference from a void pointer. + The entries for Unsafe.As and Unsafe.BitCast bind both method variables. Each resolving site + must emit its call. The closed parameter of Unsafe.AsRef selects its byref or void-pointer + declaration and external target. Unsafe.NullRef and Unsafe.IsNullRef each bind their single + declaration and target. All four reference names check element storage through the indexed + layouts, and As and AsRef admit only field or vector storage. +- The local variable types are checked against the standalone signature the body header names. +- The method's own identity is checked against four columns of its `MethodDef` row. `Flags` and + `ImplFlags` must equal the evidence's attributes and implementation flags. The `Signature` blob + gives the calling convention, generic arity and parameter and return types. `Name`, with the + signature, gives Cecil's full name of the method, for a generic instance at the type arguments + its evidence states, and `BodyDecodes` equates it with the evidence's `signature`, the string + `Extracted.signatures` lists and proofs select the method by. The owner is the `TypeDef` whose + method range holds the row, and its `GenericParam` rows and the method's give the generic + parameter counts and flags; a class receiver also checks the class layout facts. A row whose + flags give it no IL body (abstract, platform invoke, internal call, or a code type other than + IL) is refused, and so is a runtime-async row (`ImplFlags` 0x2000), whose body returns the + result its signature states as a task. An `EXPLICITTHIS` method is refused because the model + requires an implicit receiver outside the parameter list. Instantiation also refuses this + flag, including for methods decoded directly from raw definitions. + Each named component of a closed type argument resolves from an exactly pinned definition + or a scoped platform entry with the required generic arity. An instance receiver or inherited field + still requires a class chain with a supported layout. Constructed aggregate identities include + the scope of each argument. A platform `ValueTuple` pair records the primitive assembly of + both arguments, including when the pair is nested inside another layout. Runtime scalars and + strings retain their fixed storage. + The `RVA` and `ParamList` columns are not read. +- Generic parameter counts come from `GenericParam` rows. An owner's carried rows must form its + whole zero-based run. An owner with none carried must be shown to have none by its neighbours in + the table, which ECMA-335 requires sorted by coded owner: the first carried row above the + owner's coded value is row 1 or follows a carried row below it, or the table's last row lies + below it, or the table is empty. A method whose signature lacks the GENERIC flag and that has + no carried run has none, as ECMA-335 requires of its rows. A `TypeDef` or pinned `TypeRef` that + names a type alone, a class's base, and the declaring type of a `Field` or `MethodDef` token + used as an operand have no generic parameters, and the head of a generic instance has one per + argument. A type outside the carried slices has no rows to count. +- A table or signature form the checker does not read is an explicit error: tables other than + those the checker names, general arrays, `typedref`, pinned locals and vararg conventions, + modifiers other than `modreq(InAttribute)` on an `in` parameter and `modreq(IsExternalInit)` on a + `void` return, module references and `AssemblyRef` rows with a public key flag. +- A top-level `TypeRef` may follow one forwarder between pinned assemblies. Lean reads the facade’s + `ExportedType` row, verifies the namespace, name, forwarding flag and destination `AssemblyRef`, + and checks that the defining slice contains the public `TypeDef` of the same name. Nested exports, + file exports and multi-hop forwarding chains are outside this binding. +- A custom modifier binds its assembly and full name, including the namespace of each nested + segment. A local nested `TypeDef` requires its enclosing `NestedClass` rows. Each enclosing + type occupies a nonzero, earlier row in the same `TypeDef` table. A `TypeRef` + follows its resolution scopes and needs no link to the modifier's definition + (`modifierNamed` in [MetadataResolution](../CIL/Extraction/MetadataResolution.lean) and the extractor's + slice collector agree). A type used any other way keeps the link, and a pinned type that does not resolve + there stops the extraction with `Unresolved pinned type`. + +Lean takes on trust that each carried row, heap entry and body is the image's bytes at that position (a wrong +copy cannot be detected without the whole stream; every row must still have the length the header's +widths give, every blob its length prefix and every string one terminating zero), the PE container +where a body sits in the image and the mapping of its address to its bytes, definitions in platform assemblies (so whether a +platform type is an enum, the field order of `ValueTuple`, and that `System.Object` declares no +instance fields), public key tokens of referenced assemblies, imported implementation definitions, +raw layout content, dispatch catalog completeness, static data bytes and generic parameter constraints. +For a generic contract import, Lean reconstructs the open definition key from the caller's +metadata, encodes its bytes in uppercase hexadecimal and checks the closed arguments and +signature. The implementation and interface digests belong to the deployment record. +The import slot carries the contract closure digest separately. The verifier hashes the audited +canonical closure and compares it with that digest. The deep audit does not yet bind the selected +generic MethodDef in the imported implementation and its published contract to the import slot. +[Deep verification](CLI.md#deep-verification) reconfirms each body at its RVA and each carried +row and heap entry, under one numbering of the `#Strings` and `#Blob` offsets per heap and with +MethodDef RVAs zeroed; the assembly identity, key token, core library, links, `#GUID` heap, +`Module` table and the RVAs of MethodDefs outside the evidence are not part of that check. The audit also +reconfirms the initial bytes of RVA fields by checking the physical field signature, class size, +packing and mapped file range. When supplied with the matching platform images, it resolves +`System.Object` through the core AssemblyRef and any forwarding rows, checks the publisher token +against each platform image's public key, and checks that every field the root class declares is +static. For the selected `System.ValueTuple` layout, the audit follows its own scoped +TypeRef and forwarding chain. It checks the defining TypeDef's two generic parameters and +the names and signatures of its two instance fields in physical declaration order. +For explicit and sequential scalar structs without custom attributes or interfaces, the audit +compares the entire raw layout with the defining TypeDef, ClassLayout, Field and FieldLayout +rows. It checks instance field signatures and order, packing, declared size and explicit offsets. +For a supported generic selection, Lean reads the GenericParam and GenericParamConstraint +rows of each MethodDef and its declaring TypeDef. It resolves the constraint identities, +including the primitive arguments of a closed local interface, and compares the InterfaceImpl +rows of concrete value-type arguments against the extraction's layout declarations. A closed +struct with primitive arguments also binds its parameter flags and constraints to its TypeDef. +A type parameter in an implemented interface takes the argument at its physical VAR ordinal. +For each external type selected in a method's platform table, Lean follows the scoped +definition and checks its flags, base and the ordinals of its generic parameters. The +platform-kind theorem binds these checks to each certified method's metadata environment. +For enums, Lean checks the physical TypeDef and its instance field `value__` against the +storage code from the certified method environment. A nested enum is identified by its +enclosing TypeDefs. An external enum may reach its definition through several forwarding +assemblies. Each must be supplied for complete coverage. +For local virtual class slots in a single image, Lean follows each physical TypeDef parent, +including abstract intermediates, and checks the MethodDefs and signatures of every descendant. +It compares the complete declarations of each method's catalog and validates each reachable +target table against the physical hierarchy. For an explicit implementation of a direct local +interface slot, the MethodImpl row must name the implementing class and two local MethodDefs +with matching nongeneric instance signatures. The catalog records the interface and the exact +declaration token. A class outside the selected hierarchy may implement an external interface +when its body MethodDef belongs to that class. Casts, constrained calls, interface dispatch, +cross-image dispatch, other override forms on selected classes, additional metadata modules +and unsupported signatures leave this fact uncovered. +With all direct reference images supplied, the audit checks each AssemblyRef against its target's +full publisher key or unsigned definition. It also compares each carried AssemblyRef token with +the physical row in its source assembly. For scalar static generic calls on top-level classes with +primitive type arguments, supply both the caller and the producer images. Lean reads the call +operand, follows its MethodSpec, MemberRef, TypeRef and AssemblyRef, and requires a unique +matching MethodDef in the producer. Each selected export in the pinned contracts sidecar must +match the imported slot and the independently audited declaration closure. Every slot must have a +physical call site. Missing images, unsupported signatures and ambiguous targets leave +`import-identities` uncovered. `check` hashes each assembly and regenerates the extraction. ### Folded instruction patterns A fold applies only to its recognized raw sequence with checked entry edges. Different compiler @@ -44,23 +437,29 @@ output can lose admission without invalidating a theorem over an already admitte | Closed `typeof` equality/inequality calls, reference `ceq` and comparison branches | Exact type handles, runtime helper identities, substituted types, result and selected target; all encoded targets validated and no branch into the interior | Forwarding/scoped identity ambiguities, decorated names and arbitrary runtime `System.Type` use remain unsupported. Reference comparison relies on [CoreCLR runtime type equality](https://github.com/dotnet/runtime/blob/v10.0.0/src/libraries/System.Private.CoreLib/src/System/Type.cs) and [GetTypeFromHandle](https://github.com/dotnet/runtime/blob/v10.0.0/src/coreclr/System.Private.CoreLib/src/System/Type.CoreCLR.cs). | | `throw new E(...)` | Allowlisted type/constructor, permitted literal/null arguments and final throw; no branch into the interior | No exception-object identity, observable constructor effects, handlers or resource-exhaustion model. | | `ldnull; throw` | Adjacent operand-free null load and throw, bound to `System.NullReferenceException`; no branch into the throw | No general exception-object evaluation or handlers. | -| Single-use span receiver | Exact local uses and permitted Length/Item receiver sequence; protected entry edges | No escaping or aliased receiver addresses. | -| In-place span constructor | Exact receiver/argument positions, constructor identity, element layout/width and capability | Unrecognized argument expressions and interior branch entries are rejected. | + +An in-place span constructor runs as a runtime-model call on a span home. +`ObjectOperandsValid` requires its receiver to be the address of a local or parameter of the +constructed type followed by one local or argument load, `ldnull` or int32 constant per +parameter, at most three, with no branch to an argument or the call. It requires a span +`initobj` to directly follow the address of a local or parameter of its type with no branch to +it. The [compiler corpus](COMPILER-COVERAGE.md) checks admission changes, not compiler correctness or every possible folded pattern. The runtime-call inventory does not count these folds. -[OPCODES](OPCODES.md) records the admitted forms. Metadata decoding, raw-input fidelity, -static-data serialization and runtime-helper correspondence remain trusted even when evidence -validates. +[OPCODES](OPCODES.md) records the admitted forms. Lean checks token operands against the carried +rows and signatures. Default verification trusts the positions of those bytes in the pinned image, +the platform definitions used during resolution, static-data contents and runtime-helper behavior. ## Negative evidence Direct Lean mutations exercise validators independently of hashes/regeneration: scalar meanings, stack kinds/joins, layouts, homes, generic substitution, pruning, call slots and folds. Positive controls establish that fixtures reach the intended obligation. See -[validation](VALIDATION-COVERAGE.md). Wrong-addition has an execution witness and an -all-successful-fuel refutation; bounds/lifetime examples use `invoke_fault_refutes_success`. A -failed positive proof or resource exhaustion alone is not a semantic counterexample. +[validation](VALIDATION-COVERAGE.md). Wrong-addition is refuted by `MethodContract.Refuted` at a +concrete input; bounds/lifetime examples use `invoke_fault_refutes_success`, which +takes only a definite fault. A failed positive proof, fuel exhaustion or an `unsupported` fault +alone is not a semantic counterexample. ## Caller proofs and published library contracts @@ -148,8 +547,15 @@ carries it, so the kernel link is never vacuous for an admitted local. Truth tests and equality check formation/lifetime without reading bytes or granting authority. Same-allocation equality uses offsets. Different-allocation interiors are distinct; comparisons involving end pointers across allocations are unsupported because adjacent addresses may coincide. -Pointer difference and unsigned ordering require a single live allocation and assume a contiguous, -non-wrapping runtime address interval. Native displacement arithmetic retains provenance and +The four element-displacement overloads of each of `Unsafe.Add` and `Subtract` and the two +byte-displacement overloads of each of `AddByteOffset` and `SubtractByteOffset` keep their +calls. The closed address and displacement types select one overload. Its open signature and +external MemberRef must match that same overload. The runtime model reads the element shape +from the indexed layouts, including for byte offsets. At a live `Unsafe.ByteOffset` call, the decoder checks the external target, the generic +declaration, both closed reference parameters, the native result and the checked size of `T`. +The site requires the original call. Pointer difference and unsigned ordering require a single +live allocation and assume a contiguous, non-wrapping runtime address interval. +Native displacement arithmetic retains provenance and readonly restrictions; no ordering between allocation bases is inferred. Object `ldnull` has a distinct scalar-flow kind. The certificate admits it in truth tests, in @@ -162,10 +568,9 @@ rejected. The raw literal, comparison operation and branch target remain exactly Each `ldstr` emits the string literal of exactly its own text, typed as an object; no other raw opcode emits a literal, and inside a folded `throw` the load emits nothing. -Each argument guard operation is decoded again from its raw `call`: the owner, method name, integer -type argument, parameter types and external identity fix the guard, its width and its signedness -(`CIL.Extraction.guardCallMatches`). No other instruction emits a guard, and a `ThrowIfNull` -argument is typed as an object reference. +Each argument guard keeps its call. The runtime table binds the open declaration and external +identity to the closed owner, member and parameter types. The type argument determines integer +width and signedness. The scalar certificate requires an object reference for the null guard. ## Exception documentation lint diff --git a/docs/SOUNDNESS-EXECUTION.md b/docs/SOUNDNESS-EXECUTION.md index 7340ff977..5629a46e1 100644 --- a/docs/SOUNDNESS-EXECUTION.md +++ b/docs/SOUNDNESS-EXECUTION.md @@ -8,7 +8,9 @@ Value-only instructions delegate to `CIL.step`. Memory operations and frame transitions use checked rules; unknown instructions cannot fall through to unchecked accesses. -Successful execution excludes model faults on its path. Under their stated initial premises, +Successful execution excludes model faults on its path. An `unsupported` fault marks a run the +model does not cover: stepping stops at it, and the refutation lemmas take only settled outcomes +(`Settled`), so none of them refutes a contract from it. Under their stated initial premises, the library proves well-formedness, allocation extension, live values, frame ownership and expiry, caller-memory preservation and static-world consistency. `InvocationCertificate` packages these properties with execution. Valid initial storage, references and authority are caller obligations. @@ -52,7 +54,7 @@ implementations. Heap-array lifetime and resource assumptions are in ### Virtual dispatch -`callvirt` of a virtual method of an admitted class extracts to `.callVirtual targets argc`. +`callvirt` of a virtual method of an admitted class extracts to `.callvirt method` with a `.virtual targets` link. `targets` pairs each class with the body the CLR selects for its instances: the nearest override on its chain up to the slot's class, or the slot's own body. A null receiver throws `NullReferenceException`. A live receiver enters the body listed for its exact runtime class; @@ -70,9 +72,9 @@ in another assembly, a method that hides the slot with `newslot`, a concrete cla an abstract slot, an explicit `MethodImpl`, and generic virtual methods. The closed world is a checked premise; it is not an assumption: a run that -reaches the call with an instance of an outside class ends in `outsideDispatchTable`, which is -neither a return nor a named exception, and more fuel does not change it. Interface calls are not -modelled. +reaches the call with an instance of an outside class ends in `outsideDispatchTable`, an +`unsupported` fault that is neither a return nor a named exception and that no refutation lemma +takes. Interface calls are not modelled. ## Aggregate storage @@ -84,11 +86,13 @@ not CLR correspondence. See [layout assumptions](SOUNDNESS-RUNTIME.md#sequential ## Method-body embedding -`MethodEmbeds` checks execution metadata, static descriptors and live instructions under a PC map. -Entry, branch/call targets and resumable fallthroughs must match. Expected `.unsupported` locations -constrain no actual code and cannot occur on a successful expected path. Even a live inserted `nop` -is outside this exact relation. `cil_embed` computes a candidate map; the kernel checks every -field. +`MethodEmbeds` checks execution metadata, the declared signature, the layout environment, static +descriptors, call links and live instructions under a PC map. A body built as +`extracted.withCode code` keeps the signature and environment of `extracted` and clears its links and +static sites. Supply both tables at the replacement body's pcs. Entry, branch/call targets and resumable +fallthroughs must match. Expected holes (`.unadmitted`) constrain no actual code and cannot +occur on a successful expected path. Even a live inserted `nop` is outside this exact relation. +`cil_embed` computes a candidate map; the kernel checks every field. `ProgramRefines` transfers successful runs with the same fuel and final memory. Outcome variants transfer named throws with relocated PCs, preserving method and exception. They do not transfer @@ -99,7 +103,7 @@ fuel exhaustion, unsupported-operation, invalid-state, memory or arithmetic faul | Relation | Preconditions | Guarantee and limit | | --- | --- | --- | | `MethodEmbedsDeadLocals` | Trailing null/unmodeled reference or bounded span slots; matching retained local/kind lengths; no parameter homes; opaque slots stay retained | Same memory, results and named throws. No invocation-certificate transfer or general `ProgramRefines` composition. | -| `AllocationRefinement` | Checked local renaming/initializers; ordinary frames without opaque/vector/parameter argument homes; well-formed memory and bounded statics | Scalar results and caller-prefix preservation of allocations, bytes and authority (`BytesBelow`); caller fields agree wherever the source holds null or a caller reference (`SlotsAgreeBelow`); named throws require valid arguments. Fresh identities/counters, caller fields holding fresh objects and arbitrary reference-valued postconditions need not match. | +| `AllocationRefinement` | Checked local renaming/initializers; ordinary frames without opaque vector locals or parameter homes; well-formed memory and bounded statics | Scalar results and caller-prefix preservation of allocations, bytes and authority (`BytesBelow`); caller fields agree wherever the source holds null or a caller reference (`SlotsAgreeBelow`); named throws require valid arguments. Fresh identities/counters, caller fields holding fresh objects and arbitrary reference-valued postconditions need not match. | Allocation refinement proves the complete dispatcher simulation with injective retained-allocation and activation mappings. Extra private homes lie outside the mapped image. Contract transport also diff --git a/docs/SOUNDNESS-RUNTIME.md b/docs/SOUNDNESS-RUNTIME.md index b1c7d9686..9225ba204 100644 --- a/docs/SOUNDNESS-RUNTIME.md +++ b/docs/SOUNDNESS-RUNTIME.md @@ -10,9 +10,9 @@ require their features; portable operations use software semantics. Profile vali but the verifier does not discover a deployment's actual CPU, runtime or resource availability. See [runtime profiles](RUNTIME-PROFILES.md). -`IntPtr.Size`, `UIntPtr.Size` and `Environment.Is64BitProcess` use the profile's required -64-bit process assumption. Their exact getters translate to constants `8`, `8` and `true`; -they do not inspect the verifier host or establish a deployment's actual process width. +`IntPtr.Size`, `UIntPtr.Size` and `System.Environment.Is64BitProcess` use the profile's required +64-bit process assumption. The interpreter runs their calls as constant models returning 8, 8 and true. +Hardware getter models read their named flag from the method's profile, including a false flag. `RuntimeHelpers.IsReferenceOrContainsReferences()` returns `false` only for supported primitive integers, booleans, characters, integer enums and vectors, or a reference-free aggregate admitted @@ -21,11 +21,11 @@ aggregate is admissible when its validated fields contain neither references nor arrays, classes, open generic arguments and unsupported storage are rejected. The query establishes neither initialized padding nor memory access, lifetime or escape authority. -The exact runtime signature and generic argument bind the constant independently of extraction. -Feature and scalar certificates receive `rawLayouts.toArray`, including types used only by queries; -the enclosing translation certificate checks the complete layout table. Aggregate queries compare -their index and identity without repeating layout computation. Runtime correspondence assumes the -.NET 10 helper's reference-and-byref classification, rather than inspecting the verifier host. +The call keeps its member reference. The runtime model checks the open declaration and closed +signature, then reads the queried element through the indexed layout reader. Feature-flow +validation uses the same storage test to propagate zero. An aggregate must match the raw +layout identity at its index. The deployment's runtime helper must agree with this classification of +references and byrefs. | Area | Model convention / deployment premise | Excluded claim | | --- | --- | --- | @@ -51,28 +51,108 @@ assumes .NET 10's native-width multiplication by `sizeof(T)` followed by additio They admit existing storage for supported integer, enum, vector and reference-free aggregate types; neither fabricates addresses nor grants authority. Negative offsets wrap at native width before the resulting reference is checked for lifetime and -bounds. Null and invalid resulting references fail. Exact signature checks bind the pointer overload; -primitive/vector stride and signedness are checked directly, and aggregate stride uses layout evidence. +bounds. Null and invalid resulting references fail. The pointer overload binds the exact external +target and generic declaration. Its signed Int32 displacement scales by the element size from +the indexed layout reader. ### Unspecified vector lanes Portable 128-, 256- and 512-bit integer vector bitwise operators, whole-vector equality, `Zero` and `AllBitsSet` share their bit-level semantics for byte, signed/unsigned 32-bit and signed/unsigned -64-bit elements. The intrinsic certificate checks the exact runtime call, element, width, generic -arguments and emitted operation. Floating-point equality and unspecified-lane observations are excluded. +64-bit elements. Hardware model entries bind the open declaration and external identity of each overload. +The closed reference selects its operation and arity. `runtimeModels_hardware` proves that every +hardware reference, `DivRem` among them, resolves through the rows of its own slot, and +`runtimeModels_exactHardware` that every exact-family reference resolves to `exactIntrinsic` in +every layout environment. +The decoder checks availability under the method's profile. A live unresolved or unavailable +call prevents construction of the decoded method. Portable +vector constructors and byte-reference loads and stores retain their calls. Their model entries +bind the exact external target, open declaration, closed element and operand types. Floating-point equality and unspecified-lane observations are excluded. `v128Low64 low` represents every vector with that low lane. `checkedIntrinsic` admits only consumers proved independent of the upper lane: reinterpretation, lane-zero extraction and the count operands of the admitted AVX2 logical shifts. Reinterpretation preserves uncertainty. Other observations, -byte stores, bitcasts, returns and call/constructor arguments reject unspecified bits. Internal +byte stores, bitcasts, returns, constructor arguments and the arguments of a linked call reject +unspecified bits. A call without a link checks only the operands its runtime model observes, so a +call modelled as an intrinsic reads its operands as that intrinsic does. Internal dup/pop and eligible opaque locals may forward them. `invoke_return_known` and `step_call_known` state the boundary guarantees; a contract cannot expose an unknown lane as a concrete value. -`OpaqueLocalsValid` requires a reachable admitted producer and independently recomputes eligibility +`OpaqueLocalsValid` requires a reachable call whose runtime model is `CreateScalarUnsafe` and +independently recomputes eligibility from raw local uses, excluding address-taken locals. Opaque by-value helper parameters are unsupported. Agreement between the permitted consumers and the actual JIT remains a runtime assumption. See [intrinsic semantics and coverage](SIMD.md) and the [runtime-call inventory](COMPILER-COVERAGE.md). +`runtimeModel` resolves the typed method reference of a `call` or `newobj` through a table of +model entries, searched by method name; at the leaf the entry's name, opcode and owner head are +compared and the entry resolves the whole reference. A `call` the table models keeps its call +and states no link, and the checked interpreter runs the model when the pc has no link; +`ManagedCallsValid` checks a retained call's link to another method. At a live unlinked modelled +call, `RuntimeModelsAgree` requires the same method reference and an available model. Its entry +also checks the member's calling-convention byte and its return and parameter types before +generic substitution. For an overloaded name, the closed reference selects one overload. +The validator checks that overload's declaration and raw operand, and execution uses its resolver. +An exact external operand binds the member name, target, argument counts and return flag. +A reserved name produces the opcode's hole when its overload fails to resolve. Both validation +and execution read storage, shape and aggregate alignment from the same indexed layout metadata. +The entries for `Unsafe.SizeOf`, `AreSame`, `IsAddressLessThan`, +`IsAddressGreaterThan`, `ByteOffset`, `Add`, `Subtract`, `AddByteOffset`, `SubtractByteOffset` +and `SkipInit` claim their names on the exact runtime owner. Each checks +its static generic declaration and closed operand. Each requires a sized element. For a struct, +the raw layout at the stated index must name that struct and the computed layout must exist. +`SizeOf` returns its checked byte size. The comparisons observe allocation-relative references. +`ByteOffset` returns the signed native displacement between two references in the same allocation. +Element offsets use the checked width of the selected type. Byte offsets use a unit stride. +The unsigned native overload zero-extends its offset. Subtraction wraps at native width. +`SkipInit` forms its reference without writing to it. The table also holds +the `System.Object` constructor, `Vector128.CreateScalarUnsafe(ulong)`, `GetElement` of +`Vector128` and `Vector256`, and the integer helpers `Min`, `Max`, `Clamp`, `Abs` and +`Sign` of `System.Math` and of each integer type that declares them, the bit helpers of +`System.Numerics.BitOperations` and of each integer type that declares them, and +`BinaryPrimitives.ReverseEndianness`, whose calls keep their calls, so the table accounts for no +lowered emission. `Unsafe.Read` and `Write` run pointer transfers at the element's natural +alignment. `ReadUnaligned` and `WriteUnaligned` run typed loads and stores at byte alignment. +These models support integer vectors of 16, 32 and 64 bytes under every profile. +A helper entry gives the scalar intrinsic of its integer type only to a static, +non-generic reference of that owner and exact runtime assembly whose parameters and result have +that type; a bit count's result is `int` under `BitOperations` and the integer type under the +type that declares it. A model the table gives a kept call must be +available in the method's profile, and `Instruction.ProfileAgreement` reads a call through the +model its method has; `ManagedCallsValid` requires a live call of a modelled method to keep its call, and +`UnsafeCallsValid` rejects `pop` at every `call`, `calli`, `callvirt` or `newobj` site, where it +would drop the call. A proof about the models the table gives (`Extraction.runtimeModel_yields`) +holds for each entry's models. + +The core span entries cover indexing, length, slicing, emptiness, pinnable references, the +empty span and implicit conversions. Both span types accept an array. A writable span also +converts to a readonly span. Each overload binds its open declaration, closed signature and +exact external target. Aggregate elements require a matching indexed layout and computed width. +Array conversions also reject byref-like elements. The three array overloads of +`System.MemoryExtensions.AsSpan` bind the open method variable in the array and return +type and require the exact System.Memory target. Their calls construct writable views using +the checked array element storage and layout width. + +The bulk entries bind `Clear` and `Fill` on writable spans and both copy members on writable +and readonly spans. The receiver is the view on the evaluation stack. The checked memory +operation uses the closed element width for copies, fills and clears under every profile. +Both `Array.Fill` overloads bind the method parameter in the open declaration and require +a primitive or enum element. The range overload also requires two int32 bounds. + +The runtime table binds ten static `System.Runtime.InteropServices.MemoryMarshal` members. +The open declaration distinguishes writable and readonly spans and fixes the positions of +the method parameters. The closed reference fixes their types. `transferKind?` reads the +scalar width and signedness, integer-vector width or aggregate size from the layout environment. +`step_call_model` executes the memory operation bound to the resolved method. + +The decoder reads the checked fold role before resolving a call. An exact runtime +`System.Type` comparison supplies its Boolean result. The constructor of an allowlisted +exception emits `nop` inside the matching throw sequence. + +At other unlinked sites, `siteClaim` refuses a call that no model claims, and the decoder emits +a hole tagged with the raw opcode, which closed method construction refuses at a live site. An +unknown member or unsupported overload therefore cannot occur at a live site. + ### Arrays, views and bulk memory Supplied arrays carry exact element identity, length and allocation-relative backing storage. @@ -83,7 +163,11 @@ Valid null/index failures raise their named exceptions; invalid descriptors, exp missing authority remain model faults and cannot discharge those exceptional contracts. `newarr` allocates primitive integer/bool/char/native arrays, same-assembly integer-backed enums, -validated reference-free aggregates and supported integer vectors. Enum payloads start at zero +validated reference-free aggregates and supported integer vectors. The instruction carries its raw +type token, which the extraction check compares exactly, and the step reads the element's shape +from the body's checked layouts (`Method.elementShape`): a struct's stride is the size of its +checked layout, and a struct the layouts do not hold, a by-ref-like struct or `System.Object` has +no shape, so its `newarr` is unsupported. Enum payloads start at zero even when the enum declares no zero member. Fresh non-null identities survive frame return, including for empty arrays. Payloads start zeroed and writable. Negative lengths raise `OverflowException`; lengths above `0x7fffffc7`, invalid native extents and @@ -121,16 +205,22 @@ and write capability remain bound to the exact span type; aggregate widths and i checked against the layout table. Resetting a local replaces its view without modifying backing memory, granting authority or extending a lifetime. -A readonly span reference parameter (`in` or `ref readonly`) carries the referenced view rather -than the address of the caller's span home, as span-local addresses already do. Carrying the view -is sound only because the home cannot change during the call: the extractor rejects `starg` and +`ldloca` and `ldarga` of a span local or homed span parameter push a home, a value that names +the frame's slot, and each consumer reads the slot's view when it runs, so a store to the local +after its address was taken is visible through the address, as in the CLR. A home never leaves its +frame: call, return and entry arguments are checked values, which a home is not +(`FrameAction.HomeFree`, `invokeWith_spanHome`), and admission refuses an address where a callee +takes a span by value. A readonly span reference parameter (`in` or `ref readonly`) therefore +carries the view the caller's home holds when the call runs rather than the address of that home. +Carrying the view is sound only because the home cannot change during the call: the extractor rejects `starg` and `ldarga` of such a parameter, span `stobj`, `initobj` and `cpobj` outside the checked local reset, `ref` and `out` span parameters, and span references in locals, returns and fields, so no admitted code can rebind, address or overwrite a span home. The parameter's readonly attributes, which a generic specialization copies from its definition, and these rejections are trusted -extractor decisions; the Lean validators still bind the raw argument to -a managed reference, reject its address and accept an empty emission for `ldobj` only at a span -type. Loads, stores and comparisons that expect a byte reference fault on a view rather than +extractor decisions. Lean binds the raw argument to a managed reference and rejects its address. +A decoded span `ldobj` forwards a span value and reads a local or parameter home as the view its +slot holds when the load runs. Its preceding instruction must load or address that +exact span home, and the copy must have no incoming branch. Loads, stores and comparisons that expect a byte reference fault on a view rather than reading it as an address. An `in` parameter of a virtual member carries `modreq(InAttribute)` on its reference. The @@ -147,6 +237,13 @@ signature string keeps it. The extractor admits the modifier only directly on `S naming `System.Runtime.CompilerServices.IsExternalInit` from any assembly, as C# does; and the same twin rejection covers a method equal to the accessor once the modifier is erased. +The model of `Unsafe.As` forms a reference and preserves its address between field or vector +storage. A span, array or class reference is not the address of bytes the other type can name, +so an `As` site naming one decodes to the call hole. +Both `Unsafe.AsRef` overloads keep their call and form a reference for field or vector storage. +The closed parameter selects the byref or void-pointer overload and its exact external target. +`Unsafe.NullRef` and `Unsafe.IsNullRef` accept an indexed storage kind other than byref storage. +Their models push the null managed reference and test a managed reference for null, respectively. Equal-size aggregate/vector `Unsafe.BitCast` uses the same snapshot representation for validated `byte`, `int`, `uint`, `long` and `ulong` vector elements. Element signedness does not initialize unknown bytes or change their bits. Aggregate/vector loads and stores preserve initialization masks. @@ -164,13 +261,21 @@ Nonempty destinations need write authority, not prior readability. Copy requires bytes. Empty operations grant no authority. Empty spans have null pinnable references; empty arrays retain their data reference. Null array data-reference access raises its named exception. +The four Unsafe block helpers retain their calls. The closed signature selects the byte-reference +or void-pointer overload. The raw operand must match its external target and open declaration. +Each member takes a `uint` byte count and returns void. Copy takes two addresses of the same type, +and initialization takes an address and a byte. The aligned helpers require eight-byte alignment +and the unaligned helpers require one-byte alignment on every supported profile. + Block copies require disjoint regions; `cpobj` permits identical addresses but rejects partial overlap. Nonempty block accesses check lifetime, bounds, permissions, initialization, alignment and GC-reference exclusion. Block alignment is eight unless a valid `unaligned.` prefix selects 1/2/4. Zero-byte blocks access no memory, including through null. Prefix operands/followers and entry edges are validated; correspondence to CoreCLR remains assumed. -`BinaryPrimitives` span operations bind width, endian order, signedness and try behavior exactly. +The runtime table binds each endian member of `BinaryPrimitives` to its open declaration, +closed signature and external target. The retained call executes the checked span operation +for its width, byte order, signedness and try behavior. Native integer overloads use eight-byte transfers under the validated 64-bit deployment profile. Short ordinary operations raise `ArgumentOutOfRangeException`; short try-reads initialize the output to zero and short try-writes preserve memory. Reads capture source before writing an aliased output. @@ -179,9 +284,24 @@ runtime implementations. ### Static storage +The method decoder retains the storage descriptor at each static load or address. An opaque +scalar load requires the caller's binding. A reachable `stsfld` fails extraction and is refused +by the method decoder, and executing a hand-built store returns the unsupported static field +store error. + RVA storage is immutable and extraction-bound. Static keys hash full assembly/field identities; -hash injectivity is not assumed: extraction/import checks reject collisions, and composed full -verification proves `linkChecked`. Raw `link` alone supplies no collision guarantee. +hash injectivity is not assumed. Within one extraction, `TranslationValid` requires each identity +to name exactly one field and each field to have one identity, by the metadata row its token +resolves to (`StaticIdentitiesValid`). Across imports, extraction/import checks reject +collisions, and composed full verification proves `linkChecked`, which compares the linked +descriptors and fails with the first `StaticCollision`. Raw `link` alone supplies no collision +guarantee. + +At a live RVA field token or address, the decoder compares the operand's owner, index, type and +name with the descriptor at that instruction. It checks that the blob has the declared nonzero +size. The extractor supplies the PE bytes, class size and packing. The runtime model for +`RuntimeHelpers.CreateSpan` checks the generic declaration and selects the element width +from the closed scalar type. Execution checks the storage alignment before constructing the view. An RVA field's descriptor alignment is its packing. The extractor checks the RVA, and that its section's virtual address and file offset agree modulo 8; the runtime premise is that CoreCLR @@ -235,34 +355,40 @@ locals and parameters are roots without bytes and have no address. bytes in the object's allocation (`objectFieldAddress`). The model chooses the layout independently of CoreCLR: one 8-byte slot per instance field, the fields of the furthest base first and each class's own in declaration order after its base's (`objectFieldOffset`), so a -field keeps its offset in every derived instance. Lean rechecks the offset against each -instruction's raw field index and type; that the raw index counts the fields of the declaring -class's bases first is metadata the extractor decodes. The model's layout needs no -correspondence with CoreCLR's layout because code cannot observe it: `ldflda` on a class is -rejected, and object references have no integer form. Each access checks that the field's +field keeps its offset in every derived instance. Each `ldfld` and `stfld` carries the field +reference its raw operand names, and its step resolves the slot from the field's index and type +(`Method.fieldShape`); that the raw index counts the fields of the declaring class's bases first +is metadata the extractor decodes. The model's layout needs no correspondence with CoreCLR's +layout because code cannot observe it: the extractor refuses a method with `ldflda` on a class +field at admission, a hand-written body holding one faults as `unsupported` when stepped, and +object references have no integer form. Each access checks that the field's declaring class is in the allocation's lineage, so a field of a base class is reached on every derived instance and a field of an unrelated class on none, and a null receiver throws `NullReferenceException`, as `ldfld` and `stfld` do; a null byref keeps its distinct `nullDereference` fault. -`newobj` of an admitted class (`.newObject`) checks the constructor's arguments, then allocates -a fresh live instance (`allocateObject`): an allocation of kind `.object` with the class and its +`newobj` of an admitted class (`.newobj` of the constructor's `MethodRef`, under an `.object` link +in the method's table) checks the constructor's arguments, then allocates a fresh live instance +(`allocateObject`): an allocation of kind `.object` with the constructor's class and the link's bases as its runtime type, one zeroed 8-byte slot per instance field, inherited fields included, and authority over all of it. It charges the instance's size to `Memory.heapBudget`, the bytes the caller grants for objects, and fails with `allocationFailure` when the budget is short; that fault is a model resource fault and is not a CLR exception, so no proof of an expected exception can rest on it. The budget never grows back: it is a conservative bound that ignores collection. The constructor then runs with the -instance as its receiver, and the instance is pushed once it returns. Lean rechecks that the -call site's declaring class is the instance's class, that its bases are those the constructor's -raw method records (`RawMethod.objectBases`), that the constructor takes an object receiver, and -that the size is one slot per instance field of the constructor's class, inherited fields -included. `newobj` of an abstract class is rejected by name, as the runtime rejects it. A derived +instance as its receiver, and the instance is pushed once it returns. Lean rechecks +(`validateConstructionWith`) that the raw instruction is `newobj` of a managed site naming exactly +the instruction's constructor, that the linked method is the one the site names, that a class +constructor takes an object receiver and is linked `.object` while a value-type constructor is +linked `.aggregate`, that the link's bases are those the constructor's raw method records +(`RawMethod.objectBases`), and that the size is one slot per instance field of the constructor's +class, inherited fields included. `newobj` of an abstract class is rejected by name, as the runtime rejects it. A derived constructor's chained call to its base's constructor is an ordinary instance `call` with the new instance as receiver ([DerivedObject](../Examples/Proofs/DerivedObject.lean)). Inside an admitted constructor, `call System.Object::.ctor()` with the exact `System.Runtime` -scope only pops the receiver: the runtime's constructor has no effect on an instance, and Lean -rechecks that exact target wherever `.pop` stands for a call. No frame owns +scope stays a call with no link, and its runtime model only consumes the receiver: the runtime's +constructor has no effect on an instance. The model entry matches that exact method reference, so +a constructor of another scope or signature has no model. No frame owns an instance, so leaving the method that created it never expires it: an instance lives as long as a reference can observe it, which is the reachability the collector maintains. @@ -280,9 +406,9 @@ instances of two unrelated cataloged classes. A caller states the premise once, for its entry memory, and execution keeps it ([execution classes](../CIL/Safety/ExecutionClasses.lean)). Only `newobj` and `newarr` of a class -create instances: `newobj` of the class and bases it records, `newarr` an array of its element -type. Frame homes, statics, value arrays and expiry create none. `ObjectSitesConsistent catalog -program` checks every such site of the program text against the catalog, once per extraction by +create instances: `newobj` of its constructor's class with the bases its `.object` link states, +`newarr` an array of its element type. Frame homes, statics, value arrays and expiry create none. `ObjectSitesConsistent catalog +program` checks every such site of the program text and link tables against the catalog, once per extraction by `decide`. Given it, `step_preserves_classes`, `run_preserves_classes` and `invoke_preserves_classes` keep the premise through every step, nested call and frame exit. Formed calls and constructions hand their continuation the same @@ -296,8 +422,8 @@ held reference to name an identity already issued, so a later allocation can nev dangling reference live; `Confined`, `MemoryBelow`, `Memory.Equivalent` and `Memory.Renamed` each relate field references alongside bytes, the last through the allocation map. -`ldfld` and `stfld` of a field whose type is an admitted class (`.objectReferenceField`, -`.setObjectReferenceField`) locate the field like a scalar one, so a null receiver throws +`ldfld` and `stfld` of a field whose type is an admitted class (a `.reference` slot) locate the +field like a scalar one, so a null receiver throws `NullReferenceException` and the owner is checked against the runtime type, then check the field's 8-byte slot for read or write authority inside the object. A load pushes the reference the slot holds once it forms again (`loadSlot`); a store requires the value to form, that is to @@ -317,6 +443,13 @@ runtime type the model never inspects. Fields of generic parameter type or a mod rejected, as are fields holding multi-dimensional arrays, whose shape a slot cannot recover from the allocation it names. +Such a field names a generic class instance as an `apply` chain over a class head +(`TypeExpr.objectConstructor`), which binding checks against a class, interface or delegate +definition, and `object` as `System.Object` of the primitive assembly. A load of either pushes the +stack kind `opaque`, which only a store into a field of either type, `castclass`, `isinst` and a +null test take, so the reference reaches a receiver, an admitted class field or an array operation +only through a type test, which checks its runtime type. + `System.String` of `System.Runtime`, matched exactly, is the one such class that is also admitted as the type of an argument, a local and a result ([StringValues](../Examples/Proofs/StringValues.lean), [StringLabel](../Examples/Proofs/StringLabel.lean)). A string is an object reference on the stack @@ -334,33 +467,26 @@ receiver (`objectReceiver`), from which Lean derives the receiver's storage and rejects the address of the receiver and of any class-typed local or argument, so no byref can stand in for an object receiver. -`callvirt` without a `constrained.` prefix, which C# emits for every instance call on a class, -is admitted when its target is a non-virtual method of an admitted class. It has exactly one -target, so it is a call (`callInstance`) that first checks its receiver: a null receiver throws -`System.NullReferenceException` at the `callvirt`, in the caller, before the callee is entered -(ECMA-335 III.3.19), and a receiver that is no object reference is ill-typed. Lean admits a -`callInstance` only at `callvirt`, only into a method whose receiver is an object, counting the -receiver among its arguments, and rejects a plain `call` emitted for `callvirt`, which would skip -the check. It also rejects a `callInstance` into a method whose metadata attributes mark it -virtual, since `callvirt` selects such a body by runtime type; the attributes are metadata the -extractor decodes, as it decodes `hasThis`. - -`callvirt` of a virtual method of an admitted class is a `callVirtual` whose table names the body -for each class of the extraction's assemblies that derives from the slot's class -([virtual dispatch](SOUNDNESS-EXECUTION.md#virtual-dispatch)). It checks the receiver for null -like `callInstance`, then enters the body listed for the receiver's runtime type, which the -allocation records. Lean admits a `callVirtual` only at `callvirt` and only into virtual methods -with the slot's signature and an object receiver -([AnimalLegs](../Examples/Proofs/AnimalLegs.lean)). - -`castclass` and `isinst` of an admitted class other than `System.Object` are an `objectTest` of -that class. It keeps a reference whose runtime type, its own class or one of the admitted bases +An unprefixed `callvirt` names the method in its raw operand. A direct link selects a +non-virtual method of an admitted class. Lean checks the method reference, the callee's +metadata and its object receiver, and counts the receiver among the arguments. A null +receiver throws `System.NullReferenceException` in the caller (ECMA-335 III.3.19). + +A virtual link maps each admitted class derived from the slot's class to the body for its +instances. Lean checks the slot reference, the signature and virtual attributes of its +implementations, and the selected body for every concrete class. Execution checks the receiver +for null and reads its exact runtime class from its allocation to select the implementation. +See [virtual dispatch](SOUNDNESS-EXECUTION.md#virtual-dispatch) and +[AnimalLegs](../Examples/Proofs/AnimalLegs.lean). + +`castclass` and `isinst` of an admitted class other than `System.Object` test against that +class. Each keeps a reference whose runtime type, its own class or one of the admitted bases the allocation records, is the class, passes null through, and otherwise gives null (`isinst`) or throws `System.InvalidCastException` (`castclass`) (ECMA-335 III.4.3, III.4.6). Those classes are the instance's whole lineage below `System.Object`, so the test is exact. For an instance the method does not construct, the runtime type, class and bases alike, is the theorem's hypothesis, as for field access and dispatch; one it constructs records its constructor's bases. -Lean admits an `objectTest` only at `castclass` or `isinst` of the same class, on an object reference, and only +Lean admits each only at the raw opcode of the same name and class, on an object reference, and only against a pinned catalog class that is no interface ([DogTest](../Examples/Proofs/DogTest.lean), [DogCast](../Examples/Proofs/DogCast.lean)). @@ -386,8 +512,8 @@ proofs. ## Arrays of class references -An array of a class `E` is itself a class instance: `.newReferenceArray E` allocates, -through `allocateObject`, an allocation of kind `.object` whose runtime type is `E[]` +An array of a class `E` is itself a class instance: `.newarr E` allocates, through +`allocateObject`, an allocation of kind `.object` whose runtime type is `E[]` (`referenceArrayType`) with no bases, one 8-byte slot per element and nothing else, so its length is its size over the slot width. Its elements are references held in `Memory.slots`, as class-typed fields are, and fresh elements are null. The array is charged @@ -396,8 +522,8 @@ does not lay out, and a short budget is the model's `allocationFailure`. The len `newarr` decodes it for value arrays: a negative `int32` or native length throws `System.OverflowException`, and one above `0x7fffffc7` is unsupported. -`ldlen`, `ldelem.ref` and `stelem.ref` on such an array (`.referenceArrayLength`, -`.referenceArrayLoad`, `.referenceArrayStore`) locate it with `referenceArrayOf`: a null array +`ldlen`, `ldelem.ref` and `stelem.ref` on such an array (the object arms of `elementInstruction`) +locate it with `referenceArrayOf`: a null array throws `NullReferenceException`, and a reference to anything but offset 0 of a live array of class references is an ill-typed state. An index read unsigned at or past the length then throws `IndexOutOfRangeException` (`referenceArraySlot`), so a negative int32 index is out of @@ -416,7 +542,7 @@ index, then type. ## Value-array fields -A class field typed as an array of a value element `E` (`.arrayField`, `.setArrayField`) holds +A class field typed as an array of a value element `E` (an `.array` slot) holds the array's reference in the object's slot for that field, as a class-typed field does; the array's bytes stay in their own allocation. The field is located like any other, so a null receiver throws `NullReferenceException`, and the slot needs read or write authority. A store @@ -435,27 +561,29 @@ so the field's declared element is what the load trusts. The extractor emits these operations for a field typed as a single-dimensional array of an integer, Boolean, char, integer-backed enum, reference-free struct or integer vector, and Lean rechecks the emitted element against the raw field token, and a stored value against an array of -that element on the evaluation stack. A vector's width is fixed by its type. A struct's comes -from its layout: the raw body names the field access as a layout use of the struct -(`LayoutUsesComplete`), and `MethodLayoutsValid` requires the emitted width to be the size Lean -computes for that layout, as for any other array of the struct -([TilePairs](../Examples/Proofs/TilePairs.lean)). - -The extractor admits a single-dimensional array of an admitted class, or of a non-generic class -outside the model other than `System.Object`, such as `string`, as a parameter, local, return or -`newarr` type, typed as an object on the evaluation stack. An array of such a static type holds -elements of a class derived from it at run time, so its element type is a class, as the -covariance check needs; an array typed by an interface or `object` may be an interface array and -is rejected. An element of an array of a class outside the model is an opaque reference, which -is loaded, stored, compared and tested for null but never read through. The extractor names the -element class as an object in the raw array type, and Lean rechecks each -`newarr`, `ldlen`, `ldelem.ref` and `stelem.ref` against its raw opcode and operand kinds. An +that element on the evaluation stack. A vector's type fixes its width. For an array of structs, +`Method.fieldShape` resolves the field through the method's checked layout environment and reads +the element size from the struct's computed layout. See [TilePairs](../Examples/Proofs/TilePairs.lean). + +The extractor admits a single-dimensional array of an admitted class or of `string` as a +parameter, local, return or `newarr` type, typed as an object on the evaluation stack. An array of +such a static type holds elements of a class derived from it at run time, so its element type is +a class, as the covariance check needs. Lean binds a type named as an object, outside the type a +class instance field holds, only to a class of a pinned assembly or to `string` +(`objectNamed`), so no signature, local, type operand or generic argument names an array of an +interface, of `object` or of another class outside the pins, any of which may type an interface +array. An element of a `string` array is an opaque reference, which is loaded, stored, compared +and tested for null but never read through. The extractor names the +element class as an object in the raw array type and the `newarr` token, and Lean requires each +`newarr` to carry that token and rechecks `ldlen`, `ldelem.ref` and `stelem.ref` against their raw +opcode and operand kinds. An element address (`ldelema`) or a typed `ldelem`/`stelem` on such an array is rejected by name, since a byref to a reference slot is outside the model, as is the address of an argument or local holding such an array. A class field typed as such an array holds the array's reference in its slot and is read and written as a class-typed field is, the raw field type rechecked as -an array of a class. Multi-dimensional arrays, arrays of interfaces, `object`, generic -instantiations or value types other than the admitted value arrays are rejected. +an array of a class. Multi-dimensional arrays, arrays of interfaces, `object`, classes outside +the model other than `string`, generic instantiations or value types other than the admitted +value arrays are rejected. ## String literals @@ -487,7 +615,7 @@ surrogate, backspace or form feed, which a Lean string literal cannot carry exac `ThrowIfEqual`, `ThrowIfNotEqual`, `ThrowIfGreaterThan`, `ThrowIfGreaterThanOrEqual`, `ThrowIfLessThan` and `ThrowIfLessThanOrEqual` run as checked operations ([CIL/ArgumentGuards.lean](../CIL/ArgumentGuards.lean)). Each ends in the named outcome -`.thrown "System.ArgumentNullException"` or `.thrown "System.ArgumentOutOfRangeException"` exactly +`.thrown .argumentNull` or `.thrown .argumentOutOfRange` exactly when its condition holds, and otherwise pops its operands and leaves memory unchanged ([GuardCount](../Examples/Proofs/GuardCount.lean), [GuardIndex](../Examples/Proofs/GuardIndex.lean), [GuardRequired](../Examples/Proofs/GuardRequired.lean)). The contracts follow the .NET 10 sources: @@ -510,9 +638,8 @@ integers take the 64-bit target. Only the exception type is claimed. Its message The `paramName` argument, a compiler-supplied literal, is popped unread, so a proof binds that literal like any other. -The extractor maps each exact signature (`Extractor/ArgumentGuards.cs`) and Lean decodes each -emitted guard again from the raw call's owner, name, type argument, parameter types and external -identity (`CIL.Extraction.guardCallMatches`); no other instruction may emit a guard. Other +Each guard keeps its call. The runtime model binds the open declaration and the exact external +target before selecting the checked memory operation at the closed type and signedness. Other instantiations, among them `char`, floating-point and `decimal`, the `void*` overload of `ThrowIfNull`, and the string guards such as `ThrowIfNullOrEmpty`, which read characters, are rejected calls. `ThrowIfNull` on a value array or any other operand that is not a class instance or @@ -522,13 +649,14 @@ null is rejected by name, since the model tests only object references. Admitted layouts are reference-free and bounded by 65,536 bytes. Sequential fields include supported integers/enums, nested aggregates and admitted vectors. Closed same-assembly generic owners bind -their original fields, arguments and constraints to substituted layouts. Field order, packing, size -and target architecture determine offsets. Unset packing is a limit of 64 bytes; nested alignment +their original fields, arguments and constraints to substituted layouts. A generic layout's owner +is a struct's generic definition, and a class application never closes to a layout entry. Field +order, packing, size and target architecture determine offsets. Unset packing is a limit of 64 bytes; nested alignment comes from the child layout, capped by packing. Inferred sizes include tail padding, declared size cannot truncate fields, and empty structs occupy at least one byte. Explicit overlap follows byte storage. -Open generic owners, explicit/auto generic layouts, reference/float fields, inline arrays and -recursive/depth-over-128 layouts are rejected. Vector512 fields are excluded because their aggregate +Open or class generic owners, explicit/auto generic layouts, reference/float fields, inline arrays +and recursive/depth-over-128 layouts are rejected. Vector512 fields are excluded because their aggregate alignment is not modeled. Storage is managed layout and is not interop marshalling. Correspondence to [CoreCLR layout](https://github.com/dotnet/runtime/blob/v10.0.0/src/coreclr/vm/classlayoutinfo.cpp) @@ -544,7 +672,7 @@ assumptions; [tuple tests](../Tests/TupleDivision.cs) probe both admitted and re ## Typed copies -`copyObject_writes_snapshot` proves destination bytes equal the source snapshot and are initialized, +`cpobj_writes_snapshot` proves destination bytes equal the source snapshot and are initialized, including padding. Access and initialization premises remain mandatory. References have provenance and offsets, not nominal CLR type tags: relating `cpobj` to the runtime additionally assumes both storage regions are compatible with its validated type token. Runtime tests are not a JIT proof. @@ -552,8 +680,13 @@ storage regions are compatible with its validated type token. Runtime tests are ## Struct instance calls Admitted direct instance calls use a managed-reference `this`; by-value arguments have private homes. -Existing-storage constructors do not allocate or bypass initialization checks. Aggregate `newobj` -uses zeroed caller-owned temporary storage, a void constructor and checked snapshot readback. +Existing-storage constructors use the receiver's storage and retain its initialization checks. +An unlinked `newobj` for either span type resolves its constructor by opcode, closed signature +and open declaration. The model consumes the array and optional bounds, pointer and length, or +single reference and pushes the checked view. Array views require admissible array storage. The +owner determines write capability and the element layout determines stride. An absent model faults +with `unlinkedCall` after the operand observation check. Aggregate `newobj` uses zeroed +caller-owned temporary storage, a void constructor and checked snapshot readback. Padding/layout correspondence and resource availability remain assumptions. Tail transfers retire the caller frame before checking callee arguments, so references into retired @@ -597,6 +730,11 @@ overloads remains a runtime assumption backed by exact signature checks and boun comparisons. See [SIMD](SIMD.md) and [OPCODES](OPCODES.md). +The runtime table owns the static `System.Math.DivRem` and `System.Math.BigMul` call entries. +Each overload binds the raw MemberRef declaration to the selected closed signature and external +target. A tuple result requires both raw fields to match the declared integer, default packing, +natural alignment, and two consecutive element-width fields in the checked layout. + `Math.DivRem` and the 32-bit and signed 64-bit `Math.BigMul` overloads are modelled from the pinned .NET 10 `Math.cs` source: the quotient reuses CIL `div`, narrow operands divide in `int` and wrap, and the remainder is `left - quotient * right`. The equal-width tuple layout is diff --git a/docs/SOUNDNESS-SPEC.md b/docs/SOUNDNESS-SPEC.md index 171d4c8d9..a0a7596a1 100644 --- a/docs/SOUNDNESS-SPEC.md +++ b/docs/SOUNDNESS-SPEC.md @@ -22,15 +22,15 @@ and never a named CLR exception, so "rejected at execution" excludes the path fr | Module initializer | Not modeled; static constructors, including `..cctor`, are never extracted. Methods on a type with a static constructor need `--assume-initialized`; for a generic class the open name, such as ``Seeded`1``, assumes every instantiation's initializer ran. A referenced assembly's type with one is rejected; a readonly static stored by any other module method, including a module initializer, is rejected. | [`MetadataValidation.Validate`](../Extractor/MetadataValidation.cs), [`StaticValues.Compute`](../Extractor/StaticValues.cs); [static initialization assumption](SOUNDNESS.md#trusted-components-and-deployment-assumptions) | | Default interface methods | Modeled only for static virtual slots: when the receiver records no `MethodImpl` override and has no public same-signature method, `constrained. T; call` runs the most specific interface-level body, the runtime's rule: among the slot's own body and every interface `MethodImpl` of the slot on an interface of the receiver, the one whose interface inherits every other candidate's, instantiated at its closed interface and the site's method type arguments. A recorded override wins; an unrecorded candidate beside a slot body is ambiguous and rejected. Two unrelated overriding interfaces (no unique winner) and a re-abstracting winner are rejected. The receiver's interface list must be closed under inheritance and fully present in the dispatch catalog, so a receiver with an interface outside the extracted and pinned assemblies is rejected. Instance default methods are rejected, since their body would need a boxed receiver. | [`resolveConstrained`](../CIL/Extraction/Dispatch.lean), [`mostSpecific_spec`](../CIL/Extraction/Dispatch.lean), [`ConstrainedCalls.Resolve`](../Extractor/ConstrainedCalls.cs); tests [`default_interface_slot_rejected`](../Tests/ConstrainedEvidence.lean), [`static_default_valid`](../Tests/ConstrainedEvidence.lean), [`static_override_precedes_default`](../Tests/ConstrainedEvidence.lean), [`static_default_ambiguity_rejected`](../Tests/ConstrainedEvidence.lean), [`static_derived_body_valid`](../Tests/ConstrainedEvidence.lean), [`static_interface_closure_rejected`](../Tests/ConstrainedEvidence.lean), [`derived_override_valid`](../Tests/ConstrainedEvidence.lean), [`most_derived_body_valid`](../Tests/ConstrainedEvidence.lean), [`diamond_rejected`](../Tests/ConstrainedEvidence.lean), [`reabstraction_rejected`](../Tests/ConstrainedEvidence.lean) | | Static interface methods | Modeled: `constrained. T; call` resolves a static virtual slot of a directly implemented interface, including a generic method slot, to `T`'s static implementation or to the most specific interface-level body, called without a receiver. For a closed generic struct `T`, the interface is the one its `InterfaceImpl` names instantiated at `T`'s type arguments, which the catalog entry must state exactly, and the selected method is closed at them. `T` and the interface may come from different extracted or pinned assemblies; every dispatch catalog entry names its pin, which must scope its type. Method type arguments must be closed and match the slot's arity, and the emitted instance must carry them exactly. `callvirt` on a static slot, `call` on an instance slot, `constrained. ldftn`, generic instance slots and open receivers fail. | [`constrainedSiteWindow`](../CIL/Extraction/Constrained.lean), [`resolveConstrained`](../CIL/Extraction/Dispatch.lean), [`ConstrainedCalls.Resolve`](../Extractor/ConstrainedCalls.cs); tests [`static_call_valid`](../Tests/ConstrainedEvidence.lean), [`call_opcode_mismatch_rejected`](../Tests/ConstrainedEvidence.lean), [`static_slot_mismatches_rejected`](../Tests/ConstrainedEvidence.lean), [`static_default_arguments_rejected`](../Tests/ConstrainedEvidence.lean), [`cross_pin_implementation_valid`](../Tests/ConstrainedEvidence.lean), [`unpinned_catalog_rejected`](../Tests/ConstrainedEvidence.lean) | -| Constant static flag getters | Modeled for branch selection only: a parameterless static call, plain or `constrained. T; call`, to a program method returning `System.Boolean` whose own feature certificate shows the same 0 or 1 on top of the stack at every reachable `ret`, and at least one reachable `ret`, supplies that word to feature flow, which prunes the block it rules out. The getter may compute its word from other such calls, type tests and feature tests at any depth. The call is still emitted, and the callee's certificate is validated by its own obligation in the same whole-program certificate. Pruned pcs are `.unsupported` holes with no step, so a wrong word, including a self-consistent claim on a call cycle, only sends execution into a hole: it can make a successful outcome unreachable, never provable. Imports and calls past the program are never constant; the extractor also leaves cycles, tail calls and certificates without one 0 or 1 word unknown. The getter's type initialization follows the `--assume-initialized` rule for extracted methods. | [`constantReturn`, `calleeConstant`](../CIL/Extraction/Features.lean), [`evidenceConstants`](../CIL/Extraction/Translation.lean), [`ConstantReturns`](../Extractor/ConstantReturns.cs); tests [`forged_constant_rejected`](../Tests/ConstantCalls.lean), [`constant_swap_rejected`](../Tests/ConstantCalls.lean), [`constant_body_rejected`](../Tests/ConstantCalls.lean) | +| Constant static flag getters | Modeled for branch selection only: a parameterless static call, plain or `constrained. T; call`, to a program method returning `System.Boolean` whose own feature certificate shows the same 0 or 1 on top of the stack at every reachable `ret`, and at least one reachable `ret`, supplies that word to feature flow, which prunes the block it rules out. The getter may compute its word from other such calls, type tests and feature tests at any depth. The call is still emitted, and the callee's certificate is validated by its own obligation in the same whole-program certificate. Pruned pcs are holes, `.unadmitted` with the raw opcode of the pc, whose step faults, so a wrong word, including a self-consistent claim on a call cycle, only sends execution into a hole: it can make a successful outcome unreachable, never provable, and no refutation lemma takes the hole's fault. Imports and calls past the program are never constant; the extractor also leaves cycles, tail calls and certificates without one 0 or 1 word unknown. The getter's type initialization follows the `--assume-initialized` rule for extracted methods. | [`constantReturn`, `calleeConstant`](../CIL/Extraction/Features.lean), [`evidenceConstants`](../CIL/Extraction/Translation.lean), [`ConstantReturns`](../Extractor/ConstantReturns.cs); tests [`forged_constant_rejected`](../Tests/ConstantCalls.lean), [`constant_swap_rejected`](../Tests/ConstantCalls.lean), [`constant_body_rejected`](../Tests/ConstantCalls.lean) | | Covariant return types | Rejected: C# emits a covariant override as a `newslot` method with an explicit `MethodImpl`, which class dispatch rejects by name, a class override must match the slot signature exactly, and so must an interface override. | [`VirtualDispatch.Table`](../Extractor/VirtualDispatch.cs), [`resolveVirtualWith`](../CIL/Extraction/Dispatch.lean), [`ConstrainedCalls.Resolve`](../Extractor/ConstrainedCalls.cs), [`resolveConstrained`](../CIL/Extraction/Dispatch.lean) | -| Function pointer type identity | Stricter than the augment: only the default managed convention is callable, compared exactly; unmanaged conventions are rejected. | [`managedSignature`](../CIL/Extraction/Generics.lean), [`ManagedPointers.Validate`](../Extractor/ManagedPointers.cs) | +| Function pointer type identity | Stricter than the augment: only the default managed convention is callable, compared exactly; unmanaged conventions are rejected. | [`managedSignature`](../CIL/Extraction/TypeStorage.lean), [`ManagedPointers.Validate`](../Extractor/ManagedPointers.cs) | | `conv.ovf..un` with float source | Not reachable: values have no floating-point form, float constants and conversions are untranslated, and scalar flow requires an integer operand. | [`Value`](../CIL/Types.lean), [`convertInteger`](../CIL/IntegerArithmetic.lean), [`InstructionTranslation.TranslateCore`](../Extractor/InstructionTranslation.cs); [`floatRaw`](../Tests/ScalarEvidence.lean) cases | | Ref fields | Rejected: a byref field type is unsupported. `Span` and `ReadOnlySpan` are runtime-modeled views and are not modeled as structs with a ref field. | [`MetadataValidation.AggregateLayout`](../Extractor/MetadataValidation.cs), [`SpanView`](../CIL/Safety/SpanViews.lean) | | Managed pointers: null and one past the end | Modeled conservatively. Null is a managed-reference value that can be stored, passed and compared; dereferencing or offsetting it is a `nullDereference` model fault and is not the augment's `NullReferenceException`. End positions form only as declared sentinels and grant no access. | [`ManagedReference`](../CIL/Safety/Memory.lean), [`validPosition`](../CIL/Safety/Memory.lean), [`access`](../CIL/Safety/Memory.lean), [`referenceAt`](../CIL/Safety/InstructionMemory.lean), [`instruction`](../CIL/Safety/InstructionMemory.lean), [`ExecutionFault.clrException`](../CIL/Safety/RuntimeExceptions.lean) | | `RefSafetyRulesAttribute` (escaping `this`) | Not checked. The admitted subset has no `box`, so a struct receiver is always caller-owned storage and the boxing elimination the rule licenses cannot arise. | [OPCODES](OPCODES.md#unsupported-opcodes) | | ByRefLike types in generics (`AllowByRefLike` 0x20) | Modeled: the flag is accepted and every generic method is checked as its closed instantiation. Byref-like elements cannot be heap array elements or fields of heap-storable structs. | [`MethodSpecialization.Instantiate`](../Extractor/MethodSpecialization.cs), [`byRefLikeFieldsValid`](../CIL/Extraction/Layout.lean), [`Arrays.Storage`](../Extractor/Arrays.cs) | -| Rules for IL rewriters (RVA alignment) | Modeled: the extractor rejects static data whose RVA is not a multiple of its `.pack` (1, 2, 4 or 8). It also rejects data that does not lie within one section's raw data and mapped size, and data in a section whose virtual address and file offset differ by a non-multiple of 8, since the PE format only recommends a 512-byte file alignment and CoreCLR may read an IL-only image flat, at its base plus the file offset. Mapped and converted layouts place sections at a page-aligned base plus their RVA; a flat layout's base is page-aligned for `Assembly.Load(byte[])` and files, and 64-byte or wider for single-file bundles ([loader](https://github.com/dotnet/runtime/blob/72104d3bf44e12fe7778ca4ffb0a95609fb6147d/src/coreclr/vm/peimagelayout.cpp), [bundler](https://github.com/dotnet/runtime/blob/72104d3bf44e12fe7778ca4ffb0a95609fb6147d/src/installer/managed/Microsoft.NET.HostModel/Bundle/TargetInfo.cs)). A flat image the host hands CoreCLR by external probing is used at the host's address, which is assumed 8-byte aligned (see [static storage](SOUNDNESS-RUNTIME.md#static-storage)). The descriptor's `alignment` is that packing and becomes the static allocation's layout alignment. `CreateSpan` succeeds only when `sizeof(T)` divides it, which for these powers of two means `.pack >= sizeof(T)` and so an RVA that is a multiple of `sizeof(T)`; a narrower packing is an `unsupported` fault and is never the success the CLR would replace with `ArgumentException`. | [`StaticData.Validate`](../Extractor/StaticData.cs), [`StaticData.RequireSectionAligned`](../Extractor/StaticData.cs), [`StaticDescriptor`](../CIL/Instructions.lean), [`staticReference`](../CIL/Safety/StaticMemory.lean), [`staticSpanAligned`](../CIL/Safety/StaticMemory.lean), [`staticInstruction`](../CIL/Safety/StaticMemory.lean), [`createSpan_aligned_at_placement`](../CIL/Safety/StaticSpanAlignment.lean); tests [`"Narrow-packing"`](../Tests/SpanTables.cs), [`"Reject-misaligned-rva"`](../Tests/SpanTables.cs) and [`"Reject-section-file-offset"`](../Tests/SpanTables.cs) | +| Rules for IL rewriters (RVA alignment) | Modeled: the extractor rejects static data whose RVA is not a multiple of its `.pack` (1, 2, 4 or 8). It also rejects data that does not lie within one section's raw data and mapped size, and data in a section whose virtual address and file offset differ by a non-multiple of 8, since the PE format only recommends a 512-byte file alignment and CoreCLR may read an IL-only image flat, at its base plus the file offset. Mapped and converted layouts place sections at a page-aligned base plus their RVA; a flat layout's base is page-aligned for `Assembly.Load(byte[])` and files, and 64-byte or wider for single-file bundles ([loader](https://github.com/dotnet/runtime/blob/72104d3bf44e12fe7778ca4ffb0a95609fb6147d/src/coreclr/vm/peimagelayout.cpp), [bundler](https://github.com/dotnet/runtime/blob/72104d3bf44e12fe7778ca4ffb0a95609fb6147d/src/installer/managed/Microsoft.NET.HostModel/Bundle/TargetInfo.cs)). A flat image the host hands CoreCLR by external probing is used at the host's address, which is assumed 8-byte aligned (see [static storage](SOUNDNESS-RUNTIME.md#static-storage)). The descriptor's `alignment` is that packing and becomes the static allocation's layout alignment. `CreateSpan` succeeds only when `sizeof(T)` divides it, which for these powers of two means `.pack >= sizeof(T)` and so an RVA that is a multiple of `sizeof(T)`; a narrower packing is an `unsupported` fault and is never the success the CLR would replace with `ArgumentException`. | [`StaticData.Validate`](../Extractor/StaticData.cs), [`StaticData.RequireSectionAligned`](../Extractor/StaticData.cs), [`StaticDescriptor`](../CIL/Instructions.lean), [`staticReference`](../CIL/Safety/StaticMemory.lean), [`staticSpanAligned`](../CIL/Safety/InstructionMemory.lean), [`staticSpanInstruction`](../CIL/Safety/InstructionMemory.lean), [`createSpan_aligned_at_placement`](../CIL/Safety/StaticSpanAlignment.lean); tests [`"Narrow-packing"`](../Tests/SpanTables.cs), [`"Reject-misaligned-rva"`](../Tests/SpanTables.cs) and [`"Reject-section-file-offset"`](../Tests/SpanTables.cs) | | Checked user-defined operators | Not applicable: operators are ordinary calls resolved by token or exact runtime signature, never by name. | [RuntimeModels.cs](../Extractor/RuntimeModels.cs) | | Atomic reads and writes | Not applicable: the model is single-threaded and `volatile.` is unsupported. | [SOUNDNESS](SOUNDNESS.md#verification-guarantees) | | Backward branch constraints (deleted) | Modeled: stack kinds come from a worklist over all reachable edges; every successor must see the same stack, and inconsistent joins are rejected. | [`NativeOperands.Certify`](../Extractor/NativeOperands.cs), [`ScalarFlowValid`](../CIL/Extraction/Scalars.lean); fixture [`"BackwardSwitch"`](../Tests/Loops.cs) and mutations [`"stack-height"`](../Tests/Loops.cs), [`"stack-type"`](../Tests/Loops.cs), [`"local-fact"`](../Tests/Loops.cs) and [`"stack-fact"`](../Tests/Loops.cs) | @@ -39,7 +39,7 @@ and never a named CLR exception, so "rejected at execution" excludes the path fr | API documentation | Not applicable: runtime methods are admitted only through exact signature models. | [Runtime assumptions](SOUNDNESS-RUNTIME.md#runtime-assumptions) | | Debug Interchange Format | Not applicable to semantics: Portable PDB supplies local names for proofs only. | [DebugNames.lean](../CIL/DebugNames.lean) | | Extended layout (0x18) | Rejected: aggregates must be sequential or explicit, enums and value tuples auto. This relies on Mono.Cecil's masked layout flags. | [`MetadataValidation.AggregateLayout`](../Extractor/MetadataValidation.cs), [`EnumTypes.ValidateStorage`](../Extractor/EnumTypes.cs) | -| Implicit argument coercion (int32 to `native unsigned int`) | Rejected: no implicit int32-to-native coercion exists. Call arguments, `stloc`, `starg` and `ret` require the slot's kind, and checked stores into eight-byte homes require an `i64`. Code needs an explicit `conv.i` or `conv.u`. | [`storageCompatible`](../CIL/Extraction/Scalars.lean), [`scalarTransfer`](../CIL/Extraction/Scalars.lean), [`localNumber`](../CIL/Safety/Frames.lean), [`storeTransfer`](../CIL/Safety/InstructionMemory.lean), [`otherMemoryInstruction`](../CIL/Safety/InstructionMemory.lean); tests [`no_implicit_native_coercion`](../Tests/ScalarEvidence.lean), [`native_home_rejects_int32`](../Tests/ScalarEvidence.lean) | +| Implicit argument coercion (int32 to `native unsigned int`) | Rejected: no implicit int32-to-native coercion exists. Call arguments, `stloc`, `starg`, `ret` and `stfld` of a class instance integer field require the slot's kind, and checked stores into eight-byte homes require an `i64`. Code needs an explicit `conv.i` or `conv.u`. | [`storageCompatible`](../CIL/Extraction/Scalars.lean), [`scalarTransfer`](../CIL/Extraction/Scalars.lean), [`localNumber`](../CIL/Safety/Frames.lean), [`storeTransfer`](../CIL/Safety/InstructionMemory.lean), [`otherMemoryInstruction`](../CIL/Safety/InstructionMemory.lean); tests [`no_implicit_native_coercion`](../Tests/ScalarEvidence.lean), [`native_home_rejects_int32`](../Tests/ScalarEvidence.lean) | | Init-only static fields | Modeled: `stsfld` is never extracted, and a readonly value is admitted only when its own static constructor is the sole writer. A scalar whose stored value extraction cannot compute is admitted as unknown and read from the caller's static binding. Writes by unmodeled code are an explicit assumption. | [`InstructionTranslation.TranslateCore`](../Extractor/InstructionTranslation.cs), [`StaticValues.Compute`](../Extractor/StaticValues.cs); mutations [`"store"`](../Tests/StaticValues.cs) and [`"twice"`](../Tests/StaticValues.cs); unknown seeds [`"Opaque-guessed.lean"`](../Tests/StaticScalarValues.cs) | Mixed native/int32 binary operands are widened under III.1.5, not III.1.6, and are listed in diff --git a/docs/SOUNDNESS-VERIFICATION.md b/docs/SOUNDNESS-VERIFICATION.md index a9f48e6e6..360c81702 100644 --- a/docs/SOUNDNESS-VERIFICATION.md +++ b/docs/SOUNDNESS-VERIFICATION.md @@ -55,9 +55,11 @@ statement, inputs and library identity; their prose is not an independent specif | `leancil check` | Extraction artifact integrity | No user-theorem verification. | | `leancil list` | Metadata method discovery | Listing does not imply admission or correctness. | | `audit-library` | Kernel-replayed compiled declarations and namespace theorem audit | Not proof that a package build directory was compiled from its current sources. | +| `verify --deep-checks` | Kernel-checked binding of the extraction's method bodies and carried metadata rows to assembly files with stated digests | Covers only the items it reports as reconfirmed; every fact it does not check stays trusted and is reported as not covered, and all pinned images require consumer-stated digests. The meaning of the audited statements rests on the `StrictBinding` sources and the Lean toolchain, whose hash and version the report prints. | -`Extracted.signatures` selects method indices and is hash-bound; it is not part of -`TranslationValid`. +`Extracted.signatures` selects method indices and is hash-bound. It lists the `signature` of each +`methodEvidence` record, which `TranslationValid` equates with the full name the method's +`MethodDef` row spells. Bindings are kernel-checked against the actual program. PDB names and locations are similarly untrusted selection aids: they cannot change `Extracted.lean` or its translation evidence. Proofs importing `Symbols.lean` compile/replay it and bind its hash; forged names cannot make the @@ -74,8 +76,27 @@ to replace them, the driver or the checker is outside the model. Consumer support reuse requires authenticated receipts, matching source and compiled-import closures, file hashes and unchanged artifacts after building. Imports of `Extracted` and `ExtractedSymbols` do not gain extraction-independent reuse authority. Restored -extraction-dependent modules and closed generated certificates still undergo required kernel replay -when proof checking. Certificate/theorem audits and extraction regeneration remain mandatory. +extraction-dependent support modules and closed generated certificates still undergo required +kernel replay when proof checking. Certificate/theorem audits and extraction regeneration remain +mandatory. + +A deep result is published only after its whole request succeeds. Its key binds the certified +environment and extraction key, the prepared StrictBinding sources and toolchain, the generated +sources with the image bytes they hold, the pins and requests, and the content hash of every part +of every compiled module the generated modules import, transitively, outside the Lean toolchain. +The closure is read from compiled import headers and resolved in search-path order, as Lean +resolves it; it holds the extraction, StrictBinding and `CIL` modules and no contract interface, +`ExtractedImports` or proof module. A restored result skips build and kernel replay of the +generated modules, since its publisher replayed those bytes against the same imported bytes; +their theorem audit runs on every request. Under `--proof-root` the environment is the verifier +and the certified library. A process hashes each certified library part and reads its header once; +its later requests check the library's file lengths and write times instead. + +The single-file verifier's support audit key binds the audit schema, verification policy, +certified library identity, extraction identity digest, ordered support imports and support +source hashes. The digest comes from the extraction identity already read for verification. +Support results are reusable only for the same extraction identity. A changed extraction +misses entries certified for a different extraction. Canonical fingerprints deserialize every available olean part with the pinned Lean toolchain, remove only the diagnostic `Lean.Linter.lintLogExt`, and reserialize. Constants, private diff --git a/docs/SOUNDNESS.md b/docs/SOUNDNESS.md index 79fd14b68..0dc11dd9a 100644 --- a/docs/SOUNDNESS.md +++ b/docs/SOUNDNESS.md @@ -11,8 +11,8 @@ A concrete-input theorem is not a universal method contract. | --- | --- | --- | | Lean kernel and foundations | Lean 4.35.0 checks proof terms correctly; admitted foundational axioms are `propext`, `Quot.sound` and `Classical.choice`. | [Theorem audit](SOUNDNESS-VERIFICATION.md#theorem-audit); [toolchain pin](../lean-toolchain) | | CIL and memory semantics | The definitions correspond to the admitted CLR behavior. This correspondence is reviewed; it is not proved against a CLR implementation. | [Execution guarantees](SOUNDNESS-EXECUTION.md#definitions-and-proved-properties); [checked execution](../CIL/Safety/Execution.lean); [specification baseline](SOUNDNESS-SPEC.md) | -| Metadata decoding and extraction | Mono.Cecil and the extractor faithfully decode and serialize the assembly; checks cover only their stated translation families. | [Independent translation evidence](SOUNDNESS-EVIDENCE.md#independently-checked-extraction-evidence); [opcode coverage](OPCODES.md) | -| Referenced assemblies | Each `--reference` DLL is a trusted input exactly like the selected assembly: pinned by identity, MVID and SHA-256 in `identity.json`, and decoded by the same Cecil and extractor code. Lean checks which assembly each body came from; `check` hashes the bytes. | [Pinned referenced assemblies](SOUNDNESS-EVIDENCE.md#pinned-referenced-assemblies); [CLI](CLI.md#extract-from-referenced-assemblies) | +| Metadata decoding and extraction | Mono.Cecil and System.Reflection.Metadata read the metadata tables and locate each method body in the PE image. The extractor copies the table rows and heap entries each method uses, with the address column of each `MethodDef` row zeroed, and the bytes of each body, into the extraction. Lean decodes the body and those rows and checks every token operand, local type and the method's own definition against them, including names, scopes, signatures, generic arguments, field indices and resolution into pinned references, so Cecil's reading of a body's header and instructions needs no trust. A name the extraction states as a string (the owner and base classes of a method, the identity of each assembly) is tied to the bytes it was read from by an equation the kernel closes with `cil_kernel_rfl`, and a type is a struct, enum or class by its base type's own reference to the core library. A reference into a pinned assembly, or into another version of one, whose slice the evidence does not carry is refused, and so is a method whose row says it has no IL body. Every admitted method equals the Lean decoder's result for its closed body, including its frame and links. Certification requires a decoding rule for every reachable instruction. Default verification trusts the extractor to locate each body, row and heap entry in the pinned PE image. It also trusts the platform definitions used during resolution. The opt-in [deep verification](CLI.md#deep-verification) reconfirms in the kernel, from a SHA-256 digest you state, that each body is the bytes at the place its own MethodDef row gives in the selected assembly and in each pinned reference, and that the copied rows and heap entries of each method are that assembly's, under one numbering of the `#Strings` and `#Blob` offsets per heap and with MethodDef RVAs zeroed (the assembly identity, key token, core library, links, `#GUID` heap, `Module` table and the RVAs of MethodDefs outside the evidence are not part of that check); it also checks a static RVA field's signature, packing, size and bytes against the field's mapped address in its pinned assembly. When the platform definitions are supplied, the audit also checks that the selected System.Object declares no instance fields and that the selected System.ValueTuple pair declares Item1 and Item2 in generic parameter order. With the images of all direct assembly references, the audit also checks their publisher tokens against the keys in those images. For selected enums, the audit follows the declaring scope and any forwarders and checks the underlying integer type against the physical `value__` field. For platform types, the audit follows each scoped definition and checks its CLI kind and generic arity in the defining image. For supported generic selections, Lean checks the physical parameter declarations of each method and its declaring type, and the direct interfaces of concrete value-type layouts. For supported scalar structs, Lean checks field declarations, offsets, size and packing. For supported local class dispatch in one image, Lean reconstructs the complete virtual slot catalog and checks the selected targets. Other constraint signatures, layouts and dispatch forms, and generic import definitions remain trusted. | [Method body decoding](SOUNDNESS-EVIDENCE.md#method-body-decoding); [deep verification](CLI.md#deep-verification); [Independent translation evidence](SOUNDNESS-EVIDENCE.md#independently-checked-extraction-evidence); [opcode coverage](OPCODES.md) | +| Referenced assemblies | Each `--reference` DLL is a trusted input exactly like the selected assembly: pinned by identity, MVID and SHA-256 in `identity.json`, and read by the same Cecil and extractor code. Lean checks which assembly each body came from and decodes each body, and the metadata rows it uses, from the bytes the extractor read from that assembly; `check` hashes the bytes. The [deep verification](CLI.md#deep-verification) can reconfirm each reference's bodies from a SHA-256 digest you state. | [Pinned referenced assemblies](SOUNDNESS-EVIDENCE.md#pinned-referenced-assemblies); [CLI](CLI.md#extract-from-referenced-assemblies) | | Runtime helpers and intrinsics | Each admitted external method implements the modeled behavior on the selected runtime and architecture. | [Runtime assumptions](SOUNDNESS-RUNTIME.md#runtime-assumptions); [intrinsic inventory](SIMD.md); [argument guards](SOUNDNESS-RUNTIME.md#argument-guards); [runtime bindings](../Extractor/RuntimeModels.cs) | | Deployment profile | The actual runtime version, architecture, endianness and feature availability satisfy the selected profile. | [Runtime profiles](RUNTIME-PROFILES.md); [profile validation](../CIL/Features.lean) | | Initial memory and authority | Caller-supplied allocations, live references, byte contents, initialization and access authority accurately describe reality, and each string literal the caller binds names the object `ldstr` loads for that text. | [Argument storage](SOUNDNESS-RUNTIME.md#argument-storage); [memory model](../CIL/Safety/Memory.lean) | @@ -43,7 +43,9 @@ see [stage progress](CLI.md#stage-progress). Checked execution tracks allocation identity, lifetime, bounds, permissions, initialization and alignment. Its successful executions satisfy the proved preservation properties. Named runtime exceptions are distinct from unsupported instructions, invalid states and fuel exhaustion. -An expected-exception proof cannot use these model failures as its witness. +An expected-exception proof cannot use these model failures as its witness. An `unsupported` +fault, such as a hole, marks a run the model does not cover, so the library's refutation lemmas +do not take it as a counterexample either. A virtual call dispatches over a closed world: its table lists every class of the extraction's assemblies that derives from the slot's class, each with the body the CLR would select. Lean diff --git a/docs/VALIDATION-COVERAGE.md b/docs/VALIDATION-COVERAGE.md index a4764cbce..467ec60f5 100644 --- a/docs/VALIDATION-COVERAGE.md +++ b/docs/VALIDATION-COVERAGE.md @@ -44,7 +44,12 @@ modified output files. reuse it within that phase, so no analysis is cached across method mutation or profile changes. Feature evidence uses the same incoming states as instruction selection. `Tests/ExtractionBatch.cs` compares direct and batch output across assemblies, scalar and SIMD - profiles, feature queries and a loop backedge, and checks source/assembly tampering. + profiles, feature queries and a loop backedge, and checks source/assembly tampering. Two outputs + that name one directory, also through a directory link, reject the batch. It also extracts + scalar, generic-layout and static-initialization methods twice into separate directories, in separate + processes and in reversed batch and `--assume-initialized` order, and requires every file to match byte + for byte. `Tests/MultiAssembly.cs` does the same with pinned references, where only the same + `--reference` order yields identical files because the order numbers the pins. ### Feature evidence @@ -74,6 +79,10 @@ that omit or split `aesX64` or `armAes64` are rejected. `BitConverter.IsLittleEn big-endian branch on those profiles; forged owners and values are rejected by both certificates, and lookalike scopes, fields and same-named module types are rejected by the extractor. +The runtime profile cases include an x64 set with `x86Base64` and `sse2` on and SSSE3, SSE4.1 and +SSE4.2 off, which no .NET 11 process reports: the extractor rejects it with a message naming the +three missing sets, and a generated kernel-checked theorem proves it is not `FeatureProfile.Valid`. + ### Exact intrinsics `Tests/ExactIntrinsics.cs` compares each intrinsic below with the host CPU, except where the row @@ -82,12 +91,14 @@ model from them: | Intrinsic | Inputs | Must separate the model from | Rejections | | --- | --- | --- | --- | -| `Avx2.Shuffle` and `Ssse3.Shuffle`, fed by `Vector256.Create` and `Vector128.Create` over byte spans | Twelve seeded span pairs each, one a 32-byte reversal mask; both short-span calls raise `ArgumentOutOfRangeException` in the runtime and the model | Shuffles that ignore bit 7 or cross 128-bit halves | A profile without the feature; forged load widths; substituted intrinsics | -| `Sse2.Multiply` on `Vector128.Create` lanes reinterpreted as `uint` | Twelve argument sets, two with set high halves and both 32-bit extremes | Odd lanes, a signed product, a full 64-bit product | `scalar`; forged evidence substituting the 256-bit multiply or another exact SSE call | -| A portable AES hash round on `Aes.Encrypt` | Eight seeded blocks; the extraction returns the same vectors under a custom x64 profile declaring `aes` and under a custom ARM64 profile declaring `armAes`, where it uses AESE and AESMC | | Every built-in profile rejects the reachable ARM64 calls; forged evidence substituting another AES step | -| A 256-bit arithmetic shift window: `Avx512F.VL.PermuteVar4x64x2` over a sign fill from `Vector256.LessThan` | The five windows such a shift loads and seven random index vectors; the x64-v4 extraction returns the same vectors | A bit-3 table select and swapped tables | See below | -| `Vector256.Create` of eight `uint`s | Compared the same way | | | -| `AdvSimd.ReverseElement8` under arm64-advsimd | Checked against `BinaryPrimitives.ReverseEndianness` of each lane, since no ARM64 host runs it | | See below | +| `Avx2.Shuffle` on `Vector256.Create` | Twelve seeded span pairs, including a 32-byte reversal mask. A short source or control raises `ArgumentOutOfRangeException` | The contract preserves caller memory for every pair of readable spans of at least 32 bytes. Swapping the source halves returns byte one in the high half. Clearing mask bit 7 returns byte one in the low half. The contract requires a zero low half and byte two in the high half | Both compiled variants have checked refutations. A missing feature, forged read width or substituted intrinsic is rejected | +| `Ssse3.Shuffle` on `Vector128.Create` | Twelve seeded span pairs. A short source or control raises `ArgumentOutOfRangeException` | The contract preserves caller memory for every pair of readable spans of at least 16 bytes, including overlapping spans. Given sixteen source bytes of `0x01` and sixteen control bytes of `0x80`, the compiled operand reversal returns sixteen `0x80` bytes. The contract requires zeros | Checked refutation under `x64-sse42`. The native comparison also runs on SSSE3 hosts | +| `Sse2.Multiply` on `Vector128.Create` lanes reinterpreted as `uint` | The compiled entry returns both unsigned low-half products for all four input words and preserves caller memory. Twelve cases also run on the host | Shifting all four inputs by 32 selects the odd lanes and returns `(5, 21)` where the contract requires `(12, 32)`. Other inputs distinguish signed and full-width products | Checked odd-lane refutation under `x64-sse42`. The scalar profile and forged intrinsic substitutions are rejected | +| `Avx512F.CompareLessThan` followed by `Avx512DQ.MoveMask` | The compiled broadcast returns 255 for unsigned `left < right` and 0 otherwise, for every pair of words in well-formed caller memory | Replacing the comparison with `CompareGreaterThan` returns 0 for `(1, 2⁶³)` where the contract requires 255 | Checked refutation under `x64-v4`. Both binaries also run on hosts with AVX-512 F and DQ | +| A portable AES hash round on `Aes.Encrypt` | Every 128-bit state and key, with eight seeded blocks compared against hardware. The x64 AESENC and ARM64 AESE/AESMC paths preserve caller storage | Loading the low key half twice drops bit 64 when only that bit is set. The modified assembly returns `0x63` in every byte for the zero state | Both AES profiles check the same contract and its refutation. Built-in profiles reject a reachable missing instruction, and forged AES call evidence fails | +| `Avx512F.VL.PermuteVar4x64x2` over a sign fill from `Vector256.LessThan` | For every four-limb value and every selector vector, the result draws from the four limbs or the high limb's sign fill and preserves caller memory. Twelve cases also run against a scalar reference | With limbs (0, 1, 2, 2⁶³) and selectors (4, 5, 6, 7), the contract requires four all-one words. Broadcasting the low sign returns four zeros. Exchanging tables or selecting the table with bit 3 returns the four input limbs | All three compiled variants have checked refutations under `x64-v4`. On AVX-512 VL hosts, each variant also runs against its scalar reference on the twelve inputs and the counterexample | +| `Vector256.Create` of eight `uint`s | The compiled method packs all eight arguments in lane order and preserves caller memory. One byte-for-byte case also runs on the host | Swapping the first two argument loads puts the only nonzero word in lane one instead of lane zero | Checked refutation under the scalar profile | +| `AdvSimd.ReverseElement8` under arm64-advsimd | Every pair of 64-bit lanes, with native checks on AdvSimd hosts | Swapping the two compiled argument loads exchanges the lanes at `(1, 2)` | Checked refutation under `arm64-advsimd`. See below | | A method that swaps two halves through `Vector128.LoadUnsafe` and `StoreUnsafe`, with and without an offset | Matches the host and faults on the second store into a 31-byte destination | | See below | For the last four rows, profiles without AdvSimd or AVX-512 VL reject the reachable calls, and @@ -115,6 +126,67 @@ workflow. batch compilation/replay time. - `GenericEvidence` also checks that an invalid proof fails batch compilation and that an unchecked declaration reaches, and fails, independent kernel replay. +- `GenericEvidence` mutates the emitted `declared` signature (a parameter, the return type, the + receiver and its kind), the call-link table (a changed callee, a dropped entry, an entry at the + wrong pc or at a non-call) and the shared layout environment. Each fails its own conjunct with the + kernel's failed-decision or definitional-equality message. A changed call target moves its link + definition too, so only the call binding can reject it, and each mutation fails the fixture when its + text replacement matched nothing. +- `GenericEvidence` mutates the body evidence of its first sample: a body byte, the header, a token + operand byte, a token table key, a token table operand, an emitted instruction offset, `maxStack` and + a branch index. Each must fail a body obligation with the kernel's failed-decision message, so a + mutation that only breaks parsing does not count, and the unmutated obligation is the positive + control. + `Tests/BodyDecoding.lean` decodes hand-made tiny and fat bodies with short and long branches, a + switch, two-byte opcodes, token operands and float constants, rejects each kind of malformed + body separately with its specific error, and compares the operand table with a static list + generated once from the opcodes of Mono.Cecil. + `Tests/BodyEncoding.lean` writes bytes and the metadata slice for the hand-built methods of + other Lean fixtures. + `Tests/MetadataDecoding.lean` decodes rows, headers, heap entries, signature blobs and + assembly names copied from `Examples.dll` (two-byte columns) and from the runtime's + `System.Private.CoreLib.dll` (four-byte columns) and compares them with the values + System.Reflection.Metadata and Mono.Cecil read from the same positions, and rejects malformed + rows, headers, heaps and signatures with their specific errors. + `Tests/MetadataBinding.lean` checks every operand form, the locals and a method definition + against a hand-built pair of pinned assemblies, and changes one row byte, blob byte, heap + offset, scope, instantiation, field index, local or link at a time, each with its exact error. + It checks each claim of a struct field operand, including the owner kind and the arity of a + generic struct owner. + `Tests/MetadataDifferential.cs` writes every reference, type definition, type specification and + method specification row of `Examples.dll`, with the links that locate nested types and the + definitions of its own methods and Cecil's full name for each method reference, and Lean must + spell the same names from the bytes and refuse every reference that names a general array. The + kernel decides 128 references per theorem, one theorem at a time, so the memory of a run does + not grow with the assembly, and the cost per added reference must stay within 1.25 times from + 64 to 128 references. + Tests/BodyLiteral.lean writes the evidence of one method with `fixedBytes` byte literals, with + literals over 32 bytes split into 32-byte ones to keep lines short, proves in the kernel that + each kind of literal equals its list literal, proves its body check with the kernel, rejects a + changed byte of the body literal, and requires the elaborator's decide to refuse the check within + a small heartbeat budget. Its cost rows write literals as the extractor does, one code joined + from numerals of at most 256 bytes. Elaborating the literal of a body of 16384, 65536 and 262144 + `nop`s must stay within 4.5 times the heartbeats and, taking the fastest of three runs, 8 times + the time at 4 times the body, and within 200 heartbeats per byte; a numeral parse quadratic in + its digits grows 16 times. Body-checking the literal of a body of 128, 512 and 2048 `nop`s must stay within 4.5 + times the heartbeats at 4 times the body. Bodies of 5, 10 and 20 KB of `ldc.i4` and `pop` + pairs, once with every 256-byte numeral ending in the same eight `nop`s and once with random + operands, must cost the kernel at most 1.15 times as much per byte as at the size before, both + for a fold over the bytes and for the body check. The kernel's caches hash a numeral by its low + 64 bits, so the same bodies written as separate numerals joined by `++` cost about twice as much + per byte at each doubling. Tests/BodyLiteral.lean also fails if the import closure of + `CIL.Extraction.FixedDigits`, the only CIL module an image module imports, reaches a module of + `Lean`. Tests/StrictBindingScaling.cs proves the SHA-256 digest of a 4 KB + image page whose 64-byte chunks all end in the same eight bytes within 1.1 times the heartbeats + of the same page with random ends; written as one numeral per chunk, it costs 22 times as much. + Tests/ScalarEvidence.lean requires the same + refusal and kernel decision for a whole certificate, and reduces the fixture encoder in the + elaborator at two method sizes. + Tests/LookupScaling.cs also compares the metadata check's cost per token for 125 and 500 user + string and field tokens and for 25 and 100 call tokens, for the whole extraction and for + `Extracted.method0_calls_validated` alone, within 1.15 times, and the body check's cost per + instruction for a method of 125, 500 and 1,000 string tokens, within 1.1 times of the size + before. - Runtime bit tests narrow each source pattern to the parameter type before removing duplicates, so each distinct typed input runs once; every rotation count runs as supplied, including negative and out-of-width counts. Replay fallback tests check rejection and attribution in one @@ -125,7 +197,7 @@ workflow. dotnet run --file Tests/PrimitiveArrays.cs -- --measure ``` -The positive controls for generic, constrained-call, feature-flow and folded-span mutations follow +The positive controls for generic, constrained-call and feature-flow mutations follow these rules: - Generic and constrained-call positive controls keep their narrowed declarations and exact @@ -146,7 +218,7 @@ these rules: - Regression cases cover removed declarations used by definitions or obligation types, changed definitions, qualified references and opened namespaces. Rejection diagnostics keep their case boundaries with or without Lean's diagnostic classification codes. -- Feature-flow and folded-span mutations use the same checked controls and keep only the intended +- Feature-flow mutations use the same checked controls and keep only the intended feature-flow obligation in each negative fixture. Every rejection must identify `FeatureFlowValid` as false. Full positive certificates, runtime comparisons and consumer replay run as well; folded instructions with an unrecognized theorem cannot silently skip a test. @@ -227,10 +299,11 @@ processes, each with its private process and worker checks. Run one with | `--check` name | Fixture | Checks | | --- | --- | --- | | `source-resolution` | `Tests/ConsumerSourceResolution.cs` | Consumer source resolution | +| `package-paths` | `Tests/ConsumerPackagePaths.cs` | Package containment decided with links followed on both sides: a package reached through a directory link holds its own directories in either spelling, an audited module reported with links followed is the checked file and a shadow elsewhere is not, containment, identity and compiled module names decide on the location rather than the spelling, a case variant of a directory is the same location exactly where the file system finds it, each name is matched under the case rule of the directory holding it, which the operating system reports where it can (the volume on macOS, the case-sensitive flag on Windows, the case-folding flag on Linux) and the directory's entries show otherwise, an unknown rule never admits a name in another case into a package while it keeps two outputs apart, an answer is kept only for a directory that existed when asked, a sibling whose name differs only in case lies outside the package on a case-sensitive volume (a disk image on macOS, a directory with the case-sensitive flag on Windows, the temporary directory on Linux), a path with `..` after a link is kept only where reading the `..` after following the link, as POSIX does, and as spelled, as Windows does, name one location inside the package, and a path that leaves it through `..`, an absolute path, a link (also a dangling one) or a cycle of links is rejected. On Windows the link cases print SKIP where no symbolic link or junction can be made; the case-sensitive volume check prints SKIP where fsutil cannot set the flag on Windows or where the Linux temporary directory ignores case. | | `compile-check` | `Tests/ConsumerComponents.cs` | The components fixture run with `--compile-check` | | `support-union` | `Tests/ConsumerSupportUnion.cs` | A fixed three-extraction consumer batch in two orders. Support imported only by later groups must build and replay once; direct and indirect extraction-dependent modules must replay in every consuming group, including repeated import commands on one line. Compiled import headers are authoritative when scheduling misses an import. | | `certificate-reuse` | `Tests/ConsumerCertificateReuse.cs` | Four independent verifier processes with distinct proof sources. Unchanged generated certificates keep their authenticated build outputs and run no certificate `lake build`. Reuse requires the complete certificate module parts and traces, unchanged authenticated restored artifacts, and the source/import closure binding. Changed extraction text and self-rehashed corrupt artifacts force rebuilding `Extracted`. Every successful request independently replays `Extracted` and the proof and audits the certificate. Expected rejection groups keep the separate `ExtractionAudit` module. Generated-source tampering is rejected before receipt reuse, and proof code cannot replace restored or freshly built certificate artifacts; both rejection paths remove a stale report. | -| `components` | `Tests/ConsumerComponents.cs` | Four independently certified wrappers around one helper. The common profile/layout and helper modules must compile and replay once, while all four complete extraction certificates must replay. Profile, helper and call-target changes invalidate their component identities; a consumer source shadowing a generated component must fail without a certificate. | +| `components` | `Tests/ConsumerComponents.cs` | Four independently certified wrappers around one helper. The common profile/layout and helper modules must compile and replay once, while all four complete extraction certificates must replay. Profile, helper and call-target changes invalidate their component identities. A helper whose callee sits at another index in a second extraction shares only the header with it, and with method numbers and imports erased its two texts differ only in its call and its link; a consumer source shadowing a generated component must fail without a certificate. | | `fingerprints` | `Tests/ConsumerFingerprintReuse.cs` | Authenticated canonical fingerprints across independent verifier processes, using two framework modules and a multipart fixture: corrupt receipts, changed or missing parts, module names, auditor bytes, fresh and disabled caches, diagnostic normalization, source/import invalidation and kernel replay. | | `certified-fingerprints` | `Tests/CertifiedFingerprints.cs` | Direct normalization compared with an authenticated prepared-library table while local receipts are disabled: identical canonical results across independent receivers, relocation, renamed modules, changed parts and native tools, metadata mutation before publication and consumption, and self-rehashed corruption. The fixture uses two framework modules plus one consumer module. | @@ -300,10 +373,22 @@ performance fixture is never supplied to a verifier as a certified library. checked module. - CLI regressions cover unchecked-theorem and transitive-helper rejection, stale-report removal and changed-code proof failure. -- `Tests/TranslationScaling.cs` checks the extraction prefix once, then imports it into the - compositional certificate and a deliberately failing monolithic certificate. `--measure` also +- `Tests/TranslationScaling.cs` requires the elaborator's `decide` to fail on the six-method + monolithic certificate, either at a reduction it cannot perform or at the default heartbeat + limit while reducing to weak head normal form. `--measure` also checks both complete files and records command timings for the identical six-method workload. - The consumer verifies the original extraction. + The kernel decides the monolithic certificate of call graphs of 2, 4 and 8 methods, and the + heartbeats and peak memory of a method added from 4 to 8 may be at most 1.2 times those of one + added from 2 to 4. The consumer verifies the original extraction, the extraction checks at the + default heartbeat budget with the kernel counted on the elaborating thread, and the same file + fails at 100 heartbeats with a deterministic timeout. The fixture then checks the emitted + `Extracted.layouts_validated` theorem over a chain of nested structs, each holding the one + before it, and over a chain of generic structs, each holding the previous distinct closed + generic owner, and the emitted `Extracted.method0_layouts_validated` theorem over the nested + chain. It compiles them with + `-DElab.async=false` so kernel heartbeats are counted and under `lean -M 8192`, and requires + axioms `[propext, Classical.choice, Quot.sound]`; heartbeats per layout may grow by at most 1.5 + times from chains of 64 to chains of 512. ### Proof workspace @@ -326,6 +411,17 @@ For checked-step and loop tactics, see [proof automation](VALIDATION-PROOFS.md#p states, out-of-bounds targets and mismatched certificate lengths. The feature and scalar equivalence theorems keep the list-indexed rules, including rejection when the feature and scalar certificate lengths differ. +- `Tests/ScalarEvidence.lean` evaluates the local slot reads of `ScalarFlowValid`, which come + from indexes built once per body, at single sites and in whole certificates. They reject a + store or load past the declared locals, a null store into an int32 local, a method address + stored into a native-int local that no `calli` consumes and the address of a local that a + `calli` consumes, as the metadata lists do. `closedArguments`, which reads pointer locals from a + set built once per body, gives a method pointer home only to the native-int local a `calli` + consumes, as scanning the pointer local list does. It accepts a struct field site through each + receiver kind and for an integer, enum, vector, nested struct and generic instance field, and + refuses each forged struct field site: a load where the raw opcode stores, a load from an object + receiver, a field address or store through a value, a stored value of the wrong kind, and a field + of another index, owner, held type or name. It runs in the `evidence` suite. - `Tests/LocalSnapshotSteps.lean` checks opt-in local snapshot stepping: - Vector and int32 stores, reloads and overwrites with abstract homes and memory, plus signed-byte narrowing. The mixed-width case carries Vector256, Vector128 and int32 locals @@ -361,18 +457,34 @@ For checked-step and loop tactics, see [proof automation](VALIDATION-PROOFS.md#p indices, initialization, authority, invalid extents, local storage and neighboring-byte preservation. Both run in the `evidence` suite. - `Tests/ArrayElementReferences.cs` checks compiled `ldelema` returns, helper reads and writes - against runtime behavior, preserving neighboring contents and rejecting a forged element type. + against runtime behavior, preserving neighboring contents and rejecting a forged element type at + the raw token the address is decoded from. `Tests/ArrayElementReferences.lean` covers null, empty and out-of-range accesses, expired references and address formation without granting memory authority. Both run in the `evidence` suite. +- `Tests/SpanHomeReads.cs` adds a method that stores a span local, takes its address, stores + another span and reads the length through the address, checks the CLR result and proves that the + extracted method returns the second length for both argument orders. `Tests/SpanHomes.lean` + covers a recursive callee that stores to its own slot while the caller holds a home, a linked + callee receiving the view, a homed parameter, uninitialized slots, a relocated home, homes refused + at entry, return, `stloc` and `stobj`, and shows that `ScalarFlowValid` refuses an address at a + by-value span parameter for every certificate. The first runs in the `evidence` suite and the + second in the `core` suite. - `Tests/ReadOnlyArrayReferences.cs` inserts `readonly.` prefixes into compiled fixtures, checks returned and locally stored references, and proves that caller stores fail despite - writable backing memory. It rejects independently forged writable evidence, separated prefixes - and branches that skip a prefix. `Tests/ReadOnlyArrayReferences.lean` also checks alias updates, - read access and rejection of an adapter that would remove the restriction, plus readonly - `GetReference` results for nonempty, empty and out-of-extent views and field loads, field - addresses and rejected field stores through restricted references. Both run in the `evidence` - suite. + writable backing memory. It rejects a raw body with the prefix removed, whose bytes body decoding + refuses, separated prefixes and branches that skip a prefix. `Tests/ReadOnlyArrayReferences.lean` + also checks alias updates, read access and rejection of an adapter that would remove the + restriction, plus readonly `GetReference` results for nonempty, empty and out-of-extent views and + field loads, field addresses and rejected field stores through restricted references. It shows + that the equation of the emitted code with its decoding rejects a writable address under a + prefix, that `ScalarFlowValid` rejects a prefix with an operand, + and leaves a branch into the prefixed address, or into a live instruction after a dead + `readonly.`, to `PrefixEntryFree`, which refuses it. Against a readonly and a writable address + body that the scalar check and that equation accept, it shows that the pair refuses a forged + flag or element type, a final `readonly.`, a second `readonly.`, a `nop` or an `ldelem` after + `readonly.`, and `ldelema` after `unaligned.`, and that a call after `readonly.` has no + decoding and the `readonly.` before it is refused at its own site. Both run in the `evidence` suite. - `Tests/StaticScalarValues.cs` compares 16 compiled readonly-field loads with the runtime, covering integer widths, Boolean/Char, native integers, signed/unsigned enums, helper initialization and copies. It rejects missing initialization assumptions, mutable fields, @@ -416,7 +528,16 @@ Its CLI regressions cover initial proof selection and theorem output, an addition-to-subtraction source edit and rebuild, recovery using settings kept after the failed proof, the invalid-memory-access theorem, the integer opcode, narrow-storage and primitive-operation matrices, `new-proof` rejecting an existing file, a stale extraction and a -struct parameter, and an isolated installation of the packaged .NET tool. +struct parameter, and an isolated installation of the packaged .NET tool. They also check that a +default certificate lists every binding fact as trusted, in order, and that a run without settings +or `--proof` takes no proof from an earlier certificate. + +The `core` suite checks that `verify` refuses a certificate of the previous schema, one without a +binding section, one whose binding facts are missing, reordered, repeated or unknown, one with an +unknown, numeric, absent or misplaced status, one whose default section names a digest file or lacks +its images, and one whose binding section has mode `deep`. Its consumer test republishes an +authenticated proof receipt with an unknown status and checks that the next run verifies afresh. +`scripts/TestProofCost.cs` checks that the proof cost measurement refuses the same certificates. The runner, the tool and library preparation reject every run of Lean, Lake or the audit executable whose output holds an [internal error](CLI.md#diagnostics), so a panic while building @@ -472,8 +593,10 @@ These tests cover preparation: the CLI document, a package whose lakefile names the entry keeps it however old its pin and however the cache directory is spelled, and no pass raises. A lakefile that never answers, and an injected reader that blocks, delay a pass by a bounded time; the named-pipe check prints SKIP - on Windows. Opening an entry evicts nothing, and a publication reports its removal as a stage - with the number removed and nothing else on stderr. Malformed settings keep the defaults and + on Windows. The pass limit is checked on an injected clock, so a read under way when the limit + comes is cut off there and the pass gives up whatever the machine's load. Opening an entry + evicts nothing, and a publication reports its removal as a stage with the number removed and + nothing else on stderr. Malformed settings keep the defaults and huge ones clamp. The link checks print SKIP when no symbolic link or junction can be made. The `workspace` suite checks that both package kinds pin the entry they require. - The `workspace` suite checks that a full audit, contract imports and a package import are never @@ -499,15 +622,15 @@ These tests cover preparation: | Block memory and alignment prefixes | 251 checks; ten malformed-prefix/type cases rejected; universal out-of-bounds fill rejection | | Layout-based Unsafe helpers | 418 checks across 19 element types and all three managed offset overloads; ten malformed generic calls rejected | | Checked Unsafe value transfers | Runtime comparisons and checked execution proofs cover signed and unsigned scalars, aggregates and integer vectors through byte references and pointers. Aligned pointer cases use physically aligned runtime buffers, while unaligned cases access byte offset one. Shared boundary proofs cover initialization masks, unreadable scalars, bounds and readonly destinations; aligned cases additionally check misalignment and write-only authority. Certificate mutations reject incorrect widths, signedness, helper identity, transfer direction, scalar/vector alignment and aggregate layout alignment. | -| Byte-span value transfers | Eight compiled MemoryMarshal wrappers cover reads, writes and their try forms for scalars, aggregates and integer vectors. Nineteen checked behavior claims include short-span exceptions, default-initialized out values, write-free failed try-writes, write authority and initialization masks. Seven certificate mutations reject changed signatures, operations, flags and scalar/vector/aggregate widths. | +| Byte-span value transfers | Eight compiled MemoryMarshal wrappers cover reads, writes and their try forms for scalars, aggregates and integer vectors. Nineteen checked behavior claims include short-span exceptions, default-initialized out values, write-free failed try-writes, write authority and initialization masks. Seven call-site checks bind open signatures, operations, flags and scalar/vector/aggregate widths. | | Span reinterpretation | Compiled mutable and readonly AsBytes/Cast wrappers cover enum types, aliasing writes, empty and uninitialized storage, truncated lengths and overflow. Lean certificate mutations reject changed widths, capability and operation. | | Overlapping span copies | Six compiled wrappers and seventeen checked claims cover mutable and readonly sources, enums and 64-bit elements, both overlap directions, self-copy, disjoint slices within one allocation, empty spans, short destinations, initialization, write authority and neighboring bytes. Six independent certificate mutations reject changed widths, operation, try flag, external identity, readonly destination and enum identity. Runtime checks compare copied contents with snapshot copying. | | Typed indirect dispatch | Compiled pointer locals and supplied handler tables cover ordinary calls, indirect tail cycles, return/throw outcomes, null tables, bounds, unknown targets and local-reference expiration. Independent Lean certificate mutations reject forged targets, identities, signatures, numeric pointers, conventions and dropped tail semantics. Malformed calling conventions and branches into a tail transfer are rejected during extraction. | | Aggregate and vector spans (`AggregateSpans`) | Reference-free aggregate buffers and integer vectors cover views, slicing, typed reads/writes, reinterpretation, overlap-safe copying and clearing. Vector masks and neighboring bytes are checked; vector-field layouts are certified for x64 and ARM64. Evidence mutations reject forged widths and write capabilities. | | Aggregate/vector fills (`AggregateSpanFill`) | Five compiled aggregate, Vector128/256/512 and aliased-source wrappers check repeated values, initialization masks, unknown-lane rejection, empty views, write authority and neighboring bytes. Independent evidence mutations reject changed aggregate/vector widths and clear/fill flags. | -| Span constructors (`SpanConstructors`) | Single-reference and pointer constructors cover primitive, aggregate and integer-vector elements, mutable/readonly views and in-place local initialization without readable bytes. Empty getters and explicit default resets cover returns, helper calls and empty slices. Vector-field aggregate layouts are checked on x64 and ARM64; evidence mutations reject changed widths, capabilities and aggregate identities. Wrong scoped reset types and branches into reset folds are rejected. | +| Span constructors (`SpanConstructors`) | Single-reference and pointer constructors cover primitive, aggregate and integer-vector elements, mutable/readonly views and in-place local initialization without readable bytes, including a read-only byte span over caller storage. Empty getters and explicit default resets cover returns, helper calls and empty slices. Vector-field aggregate layouts are checked on x64 and ARM64; evidence mutations reject changed widths, capabilities and aggregate identities. Wrong scoped reset types and branches to a reset are rejected. | | Span reference parameters (`SpanReferences`, `SpanReferenceContracts`, `SpanReferenceWordContract`, `SpanReferenceSecondContract`) | `in` and `ref readonly` span parameters cover loops, indexing, slicing, element writes, copies, forwarding, caller-side local, parameter and temporary addresses, closed generic helpers, a struct constructor, field reads and field addresses through writable and readonly aggregate elements, and `MemoryMarshal.GetReference` of readonly views, including a byte-order read and `Unsafe.Add`, `Subtract` and `AddByteOffset` offsets and an advancing cursor loop that stay readonly; symbolic `Contents` contracts prove an indexed read, a first-word read and a second-word read through `Unsafe.Add`. Kernel fixtures check expired backing storage, readonly element stores, writes and field stores through readonly `GetReference` results and their offsets against writable controls, one-past, beyond-end and negative readonly offsets, empty and out-of-extent readonly views and forged reference arguments. Evidence mutations reject a span-valued raw argument and a load in place of a span copy; rebinding, addressing or storing to the parameter, writable span parameters (including a generic helper's `ref` span and a generic definition stripped of its readonly markers), span reference returns and indirect copy sources are rejected during extraction. | -| Supplied aggregate/vector arrays (`AggregateArrays`, `AggregateArrayStorage`) | Typed element addresses, loads/stores, lengths, span conversions and data references cover reference-free structs and integer vectors over existing storage. Kernel fixtures check masks, authority, bounds, lifetime and readonly views; independent evidence mutations reject changed element descriptors and operations. | +| Supplied aggregate/vector arrays (`AggregateArrays`, `AggregateArrayStorage`) | Typed element addresses, writable and after an inserted `readonly.`, loads/stores, lengths, span conversions and data references cover reference-free structs and integer vectors over existing storage. Kernel fixtures check masks, authority, bounds, lifetime and readonly views; independent evidence mutations reject changed element descriptors and operations and a readonly struct address whose raw prefix is removed. | | Closed generic structs (`GenericLayouts`) | Sequential same-assembly reference-free instantiations cover nested layouts, fields, direct instance methods and constructors through checked owner substitution and ordinary aggregate storage. Unsupported reference-bearing, open, non-sequential and interface/reference-constrained generic layouts remain rejected. | | Byte-order span transfers (`BinarySpans`) | Compiled wrappers exercise all 64 signed/unsigned 16-, 32-, 64-bit and native integer read/write/try endian overloads, short spans, read/write authority and uninitialized reads. Evidence mutations reject altered widths, endian choices, signedness and try behavior. | | No-read view observations (`ViewAccessors`) | Span emptiness, pinnable references, `MemoryMarshal.GetReference` of writable and readonly spans and array data references cover empty/default/readonly views, empty and null arrays and observations without readable bytes or byte authority. Evidence mutations reject mismatched accessors and capabilities. | @@ -516,8 +639,8 @@ These tests cover preparation: | Null managed references | `IsNullRef` of `NullRef` and of a live argument, reads and writes through `NullRef` (indirect and field) and an optional `ref` local: ten outcomes compared with .NET, each `NullReferenceException` matched to a located null-dereference fault; forged `NullRef`/`IsNullRef` translations rejected by the call binding; 22 malformed generic calls rejected | | Reference/null observations | Emitted CIL fixtures compare .NET and checked Lean execution for `ceq`, `brtrue`, `brfalse`, `beq`, `bne.un` and bounded `ldnull`; an independently changed equality operation must fail scalar evidence. Semantic fixtures cover readonly views, null, lifetime, invalid addresses, end pointers and disjoint allocation interiors. A null-message throw fixture preserves existing folding. | | Integer Math helpers | 21,804 runtime comparisons across 40 overloads; universal signed-minimum proof; incorrect implementation and seven malformed calls rejected | -| `X86Base.X64.DivRem` through `ValueTuple` Item1/Item2 reads | Runtime tuple offset probe; 500 checked-execution boundary cases across five read shapes compared with hardware and a `UInt128` oracle, including divide-by-zero and overflow faults; universal quotient and fault proof; remainder substituted for quotient rejected; eight forged calls, scopes and tuple layouts rejected | -| `Math.DivRem` tuple and `out` overloads | Runtime equal-width tuple offset probe; 562 checked-execution comparisons with .NET over the ten tuple overloads, a narrow Item2 read and both `out` overloads, including divide-by-zero, wide `MinValue / -1` overflow, wrapping narrow `MinValue / -1` and null output faults; universal `int` quotient, divide-by-zero and overflow proof; remainder substituted for quotient rejected; forged mixed-width tuple rejected | +| `X86Base.X64.DivRem` through `ValueTuple` Item1/Item2 reads | The runtime field offsets are 0 and 8 bytes. Across five read shapes, 500 boundary cases agree with hardware and a `UInt128` oracle. The universal proof covers the quotient, divide-by-zero and overflow. Returning the remainder for 7 divided by 3 refutes the quotient postcondition. Eight forged calls, scopes and tuple layouts are rejected. | +| `Math.DivRem` tuple and `out` overloads | Runtime probes check the equal-width tuple offsets. Across ten tuple overloads, a narrow Item2 read and both `out` overloads, 562 cases agree with .NET. The cases include divide-by-zero, wide `MinValue / -1` overflow, narrow wrapping and null output faults. The universal `int` proof covers the quotient and arithmetic faults. Returning the remainder for 7 divided by 3 refutes its quotient postcondition. A forged mixed-width tuple is rejected. | | Signed and 32-bit `Math.BigMul` | Universal signed high/low proof with exact `Int` recomposition and 81 signed boundary cases with uninitialized output storage; unsigned high-word contract rejected for the signed call; universal exact `BigMul(int, int)` proof; 36 signed and 36 unsigned 32-bit boundary cases compared with .NET | | Library, scalar/SIMD semantics, allocation, permissions, initialization, lifetime and symbolic automation | Regression checks | | Ten extracted arithmetic, branching, call, aliasing, aggregate and semantic-negative proofs, each with artifact checks | Regression checks | @@ -549,11 +672,12 @@ These tests cover preparation: | Installed `leancil verify`: explicit proof, printed theorem, portable settings, source mutation, failure/recovery, memory fault theorem and stdout/stderr | Four CLI regression stages | | Method discovery includes canonical selectors and unsupported candidates without claiming support | Regression checks | | Malformed settings, missing proof, unsupported CIL, changed assembly and missing Lake produce stage-specific guidance | Regression checks | -| Subtraction rejected by the unchanged universal addition contract | Rejects the implementation and removes any successful report | +| Subtraction refuted against the addition contract | Verifies `MethodContract.Refuted` at inputs `(0, 1)`, bound to the subtracting method's extraction | | Reference-free array allocation | Checked primitive/enum, packed/nested aggregate and vector zero initialization, payload alignment, empty/non-null identity, negative/native lengths, helper returns and packed field stores; forged element identity, layout identity/width and byref-like certificates, malformed markers and unsupported extents rejected | | Integer `div`, `div.un`, `rem`, `rem.un`, both widths | 648 comparisons against .NET execution and corresponding Lean proofs, including divide-by-zero and signed overflow | | Extracted unsigned quotient for arbitrary inputs with a nonzero divisor | Universal proofs for 32 and 64 bits; multiplication substituted for division rejected by the unchanged contract | -| Division with mixed widths, references, stack underflow or a zero divisor | Semantic rejection tests | +| Division with references, stack underflow or a zero divisor | Semantic rejection tests | +| Division and remainder of a mixed native/int32 pair, both operand orders, including the zero-divisor and signed-overflow faults | Semantic tests that both interpreters sign-extend the int32 operand and agree | | Checked add/subtract/multiply, signed and unsigned at both widths | 2,028 comparisons against .NET results or overflow exceptions and corresponding Lean proofs | | Checked addition with a sum that fits | Universal 32-/64-bit natural-number addition proofs; subtraction substituted for addition rejected | | All 20 integer checked-conversion encodings, from both input widths | 762 comparisons against .NET execution and corresponding Lean proofs, including native destinations and overflow | @@ -561,7 +685,7 @@ These tests cover preparation: | Native signatures, locals, calls, fields, references, arithmetic, shifts, casts and `sizeof`, plus exact mixed native/int32 operations in both operand orders | 2,511 comparisons against .NET results or arithmetic exceptions and corresponding Lean proofs; all 60 ambiguous mixed unsigned-branch forms rejected unless explicitly widened | | Extracted native addition on the admitted 64-bit target | Universal modular-addition proof | | Required-32-bit assembly, forged native type identity, native field overrun, native/int64 operands and 32-bit execution profile | Five intended extraction rejections | -| Native widening wrapper with memory instructions, nested wrappers or references | Semantic rejection tests; unsigned branch/comparison widening differences checked explicitly | +| Mixed native/int32 operands of every widened instruction, overflow-checked arithmetic, memory instructions, unsigned branches or references | Both interpreters give the sign- or zero-extended result in both operand orders; memory instructions and references keep their operands; unsigned branches refused; a test proves the widened instruction list complete | | Argument assignment/addressing, both full and short encodings, all admitted integer storage types, aliases, calls, byref reassignment and structs | 639 runtime/memory cases, including 636 .NET comparisons | | Overwriting an argument with a replacement value | Universal proof for arbitrary inputs; assignment to the wrong parameter rejected | | Returning an address into a parameter home | Lifetime fault proved for every input; successful execution ruled out for every fuel budget | @@ -571,7 +695,7 @@ These tests cover preparation: | Invalid packing, oversized layout, auto layout, reference/float fields, inline arrays, generics, explicit offsets on sequential fields, nested reference-bearing and recursive fields | Ten intended metadata rejections | | Synchronized methods, wrong framework/identity, invalid field layout, unsupported locals, static initializer and forged runtime scope | Seven intended metadata rejections | | Identity-bound static RVA data | Regression checks | -| Static `ReadOnlySpan`/`` tables | Indexed reads, length and first element compared with .NET, including negative and past-end indices faulting with `System.IndexOutOfRangeException`; forged receiver folds fail the feature certificate; tampered RVA bytes change the proved word; branches into the fold rejected | +| Static `ReadOnlySpan`/`` tables | Indexed reads, length and first element compared with .NET, including negative and past-end indices faulting with `System.IndexOutOfRangeException`; receiver stores and addresses remain local instructions, and erased stores fail the feature certificate; tampered RVA bytes change the proved word | | Memory-backed primitive spans | Compiled readonly and writable slices to helpers; sequence-content read and update contracts, and readonly/writable caller composition without unfolding helper bodies; creation over an existing reference; forged element widths and write capabilities rejected by specialization evidence; empty end slices, invalid indexes and slices, alias visibility, missing write authority, uninitialized reads and expired local storage; by-reference and floating-point span signatures rejected | | Static readonly struct values | `Zero`, `One` and `MinValue` limbs proved and compared with the values .NET's static constructor stored, also through an out parameter; a changed constructor argument changes the proved limbs; forged value bytes fail `Extracted.translation_valid`; a missing initialization assumption, a `newobj` initializer, `ldsflda`, a store outside the static constructor and a second store rejected; Lean regressions for the load semantics and a self-justifying initializer | | Static `ReadOnlySpan` lookup tables | First and last byte proved; past-end read and write to immutable data rejected; tampered RVA bytes change the proved value; four intended span metadata rejections and positive stored-span parameter/local fixtures | @@ -581,12 +705,14 @@ The `loops` suite checks cyclic control flow and termination: - A universal checked countdown proof holds for every `uint`, including zero and the maximum value, with 17 runtime comparisons, and holds unchanged after inserted NOPs and reordered - blocks. An incorrect implementation is rejected under the unchanged contract, and an infinite - loop is proved to exhaust every fuel budget. Backward switch, native arithmetic and - fixed-feature loops are checked, and eight malformed or unsupported fixtures are rejected. + blocks. On input 1, one incorrect implementation returns 1 and another subtracts zero + on every iteration. The latter exhausts every fuel budget. Each refutation binds the + compiled entry. The suite also checks backward switch, native arithmetic and fixed-feature + loops, and rejects eight malformed or unsupported fixtures. - `CountCalls` is proved through its helper's contract, unchanged for a helper reimplemented as - `value + 0xFFFFFFFF` with only its contract re-proved; builds that discard the helper result or - count twice are rejected. On the correct build, measuring by the count fails the decrease + `value + 0xFFFFFFFF` with only its contract re-proved. Discarding the helper result keeps the + guard true on input 1 and exhausts every fuel budget. Counting twice returns 6 on input 3. + Both counterexamples refute the counting contract. On the correct build, measuring by the count fails the decrease obligation, and the non-inductive invariant `count + remaining = value + count * remaining` fails only its preservation. - `CountUntil`, which returns or raises its helper's exception after earlier calls, is proved for @@ -613,13 +739,13 @@ The outline suites check `new-proof` outlines: element of the span it is passed, one summing and one filling an array by index, and three slicing a span local, which sum a span and an array and fill an array; - straight-line methods with `bool`, narrow, writable span and array parameters, a `bool` - result, unsigned and signed checked adds, a bit count and splits on unsigned and signed word - tests; + result, unsigned and signed checked adds, a `BitOperations` bit count and the same count an + integer type declares, each stepped whole, and splits on unsigned and signed word tests; - straight-line methods over class instances, string fields and arrays of class references, including element facts carried across element and field stores and an array the method allocates; - methods over value arrays of words, structs and vectors that class fields hold, including a - struct element named across lines; + struct element named across lines that equals the element its span constructor resolves; - struct instance methods storing fields. - The countdown outline is rejected for `sorryAx` and verifies once only its holes are filled, as do filled outlines for array/class composition, multiple struct-field stores, signed and @@ -643,8 +769,8 @@ The outline suites check `new-proof` outlines: The addition theorem proves modular arithmetic correctness for arbitrary 64-bit inputs in the checked execution model. The unsigned-short theorem proves reduction modulo 65,536 for arbitrary 32-bit inputs. The invalid-access and expired-reference theorems classify faults and rule out -successful execution for every fuel budget. The subtraction theorem independently refutes the -intended addition result. Other example claims have the quantifiers and inputs displayed in their +successful execution for every fuel budget. The subtraction theorem proves the addition contract +`Refuted` at inputs `(0, 1)`. Other example claims have the quantifiers and inputs displayed in their theorem statements; concrete examples do not establish universal method correctness. Audited example theorems meet the axiom rules of the @@ -696,15 +822,15 @@ missing arguments, zero initialization and a non-void constructor result. F# validation compares .NET execution for addition, unsigned branching, managed calls, mutable structs and by-reference stores across 324 cases. The addition, store/load and constructor -contracts are checked against the F# assembly. Subtraction must fail the addition contract, -and a list-returning function must fail extraction. These examples use the shared CIL semantics. +contracts are checked against the F# assembly. The subtraction fixture must prove a counterexample +to addition at zero and one. A list-returning function must fail extraction. ## Contracts and extraction evidence `Tests/StackAllocation.cs` extracts compiled pointer and span stack allocations, checks results with and without localsinit, rejects uninitialized reads and escaped storage, and checks -insufficient-budget failure. Mutating the emitted initialization flag is rejected by the Lean -certificate. `Tests/StackAllocation.lean` covers alignment, repeated allocations and expiry; +insufficient-budget failure. Mutating the localsinit flag of the emitted declared signature is +rejected by the Lean certificate. `Tests/StackAllocation.lean` covers alignment, repeated allocations and expiry; `Tests/StackAllocationEvidence.lean` rejects forged flags, injected allocations, extra stack items and non-native 64-bit size operands. These run in the `dispatch` suite. @@ -742,8 +868,33 @@ These fixtures cover constrained and constant calls, all in the `dispatch` suite The core and universal-contract suites cover checked execution and memory preservation. Choose and GenericIdentity preserve arbitrary caller memory exactly; Struct preserves preexisting caller -memory under its well-formedness hypothesis. Each unchanged contract rejects its incorrect -compiled implementation. +memory under its well-formedness hypothesis. A verified refutation of each contract, at an input +where a mutant returns another value, is bound to that incorrect compiled implementation. IdentitySummary and RepeatedSummary prove the statement of +GenericIdentity for one and two calls twice: by stepping `Identity`, and from a +`DecodedSummary` of it bound to its pinned bytes, metadata token and type arguments. +CallsSummary does the same for `Calls`, whose second callee writes through a reference, and +`Tests/SummaryCalls.cs` for callers that pass one local to that callee 1, 4 and 16 times. It +fails when, through the summary, a call costs more than 1.6 times as many frontend or kernel +heartbeats from 4 to 16 calls as from 1 to 4, or when 16 calls through it cost at least as much +as stepping them. It refuses a contract of the callee without its write, a caller that leaves the callee's +precondition unproved, and, for a callee whose original is forged, its record, its contract and +the summary built on them. +CountCallsSummary proves `Loops.CountCalls` twice: by stepping `Decrement` at the call in its +loop, and from a `DecodedSummary` of it. `Tests/SummaryCalls.cs` refuses that loop with a summary +that bounds `Decrement`'s result without stating it, at the invariant, and with a summary claimed +for the caller's own token, at the site check. +VectorSummary proves `DivisionIntrinsics.UnsafeLowNext` twice: by stepping `UnsafeLow`, whose +`Vector128.CreateScalarUnsafe` and `GetElement` calls run through their runtime models, and from a +`DecodedSummary` of it. `Tests/SummaryCalls.cs` refuses that summary for a record of `UnsafeLow` +whose original is forged to read the other lane, so that it claims a body other than its pinned +bytes. +`Tests/DecodedSummary.lean` refutes a summary bound to another token than the call names. +`Tests/ProgramClosures.cs` proves the contract of a method whose two callees have the same body +once in its extraction, carries it to the method's closure and binds it there as a +`DecodedSummary`. It carries that summary back to the extraction, to a program that holds the +closure at other indices, and to one that holds both callees at one index, and steps a caller +through it in each. It refutes a relocation into a program whose method at an index the closure +links to has other bytes, and one that sends two methods with different bodies to one index. Extraction evidence validation covers Lean regressions, compiled certificate fixtures, generated-evidence mutations and mandatory-certificate positive/negative checks. diff --git a/docs/VALIDATION-EXPORTS.md b/docs/VALIDATION-EXPORTS.md index 97b1386af..d5360da56 100644 --- a/docs/VALIDATION-EXPORTS.md +++ b/docs/VALIDATION-EXPORTS.md @@ -17,10 +17,10 @@ Run one partition with `dotnet run --file scripts/Test.cs -- --suite NAME`: | Name | Cases | | --- | --- | -| `exports` | Production, wrapper-True rejection, generic/virtual/static-value restrictions and caller reuse | +| `exports` | Production, wrapper-True rejection, generic/virtual/static-value restrictions, caller reuse after a body-only rebuild and rejection of a cached exact postcondition after its imported contract is weakened | | `exportsprotected` | SDK-built protected static export, separately compiled derived caller and rehashed visibility mismatch rejection | | `exportsaudit` | Proof-source and compiled-wrapper callback mutations | -| `exportsresolution` | Same loaded files and changed loaded module | +| `exportsresolution` | Same loaded files, changed loaded module and same-length caller artifact rewrite | | `exportsfullconsumer` | Explicit and repeated consumer full verification; stale-report removal; caller and library proof-build stages required separately for each invocation | | `exportsfullordinary` | Ordinary caller full verification, requiring its direct proof check and the imported proof build | | `exportsfullpair` | Two imported slots from one bundle, requiring both imported proof builds and the caller check | @@ -32,7 +32,7 @@ Run one partition with `dotnet run --file scripts/Test.cs -- --suite NAME`: | `exportsmutationsource` | Executed consumer caller source mutation | | `exportsmutationshadow` | Executed original-root module shadow | | `exportsstatics` | Standalone modular static exports with their own SDK setup and assertions | -| `exportsfullstatics` | The same fixture with `--full-composition`: forced full verification of app and library tables with one local index and one table shared by two imported slots, run after a repeated ordinary verification restored a receipt; a republished bundle with forged static records fails in preflight and a re-certified changed table fails the independent library proof; a chain of Top, App and library independently composes the table reader with its static-world premise | +| `exportsfullstatics` | The same fixture with `--full-composition`: forced full verification of app and library tables with one local index and one table shared by two imported slots, run after a repeated ordinary verification restored a receipt, with a private library cache that the first forced run meets empty and builds into, and the last meets holding the library and opens, tracing the open; a republished bundle with forged static records fails in preflight and a re-certified changed table fails the independent library proof; a chain of Top, App and library independently composes the table reader with its static-world premise | | `exportstransitive` | A chain of App, A and B, and a four-node diamond where A and C share B; full mode rechecks each selected export despite warm receipts, rejects changed deployment pins and cycles before building, rejects forged dependency composition, and detects B changing staged A before A builds | | `exportstransitiveoutcomes` | A chain of App, C, A and B independently preserves normal returns and the named exceptional outcome through three forwarders | | `exportsdiscovery` | Real producer SDK/publication bootstrap, then the six-argument discovery fixture | @@ -46,7 +46,7 @@ and their case IDs. [Suite jobs](CI.md#suite-jobs) gives their CI time limits. ### Case ledger -- The base ledger has 49 cases: 22 core, 3 protected, 2 callback, 2 resolution and 20 full cases. +- The base ledger covers 51 cases across 23 core, 3 protected, 2 callback, 3 resolution and 20 full cases. Each partition rejects an omitted, repeated or unexpected completion. - The artifact-mode rejection runs after consumer requests that succeed, so it checks removal of a successful report that a request wrote. @@ -56,11 +56,11 @@ and their case IDs. [Suite jobs](CI.md#suite-jobs) gives their CI time limits. standalone and discovery partitions cannot enter it. - `exportsstatics`, `exportsfullstatics`, `exportstransitive`, `exportstransitiveoutcomes`, `exportstyped`, `exportsderived`, `exportsinitialized` and `exportsprofiles` run directly, with - no core export bootstrap or nested post-call. Their fixture assertions are separate from the 49 + no core export bootstrap or nested post-call. Their fixture assertions are separate from the 51 base case IDs, and a standalone fixture cannot use an empty base completion ledger to claim coverage. - `exportsdiscovery` records one separate `discovery.fixture` completion after the child - succeeds; the child's behaviors are outside the 49 base cases. The partition builds the + succeeds; the child's behaviors are outside the 51 base cases. The partition builds the producer library and creates its proof packages and publication. The child builds, extracts and verifies its own caller and needs no baseline caller artifact or verified report. The partition runs no core caller verification, restrictions, callbacks or full cases. @@ -85,13 +85,14 @@ Full integration results come from the CI partition jobs. The routing tests, must reject subtraction forged as addition. The Lean fixtures cover negative offsets and differences, end positions, readonly preservation, every unsigned branch, and null, cross-allocation, invalid-position and signed-comparison rejection. -- `Tests/ExtractionGenerics.lean` and the 15 generated generic-method samples in - `Tests/GenericEvidence.cs` cover managed-call proof construction. Direct tactic rejection checks - cover changed signedness, assembly scope, generic arguments and argument count. +- `Tests/ExtractionGenerics.lean` decides the managed-call check of a generic call site, and the 15 + generated generic-method samples in `Tests/GenericEvidence.cs` cover the generated managed-call + checks. Its rejection checks cover changed signedness, assembly scope, generic arguments and + argument count. - `Tests/GenericEvidence.cs` also checks exact external-call identity sharing, repeated emission, pruned-only identities and Unicode/escaped characters. Changed bytes, versions and tokens fail - the generated equality proof. Replacing a raw target with another correctly encoded identity, or - changing its emitted intrinsic, fails the unchanged division certificate. The specialization and + the generated equality proof. The body decoder rejects a scalar-unsafe reference paired with the external + identity of Avx2.And. Model agreement rejects a different instruction at a kept call. The specialization and raw/translated metadata rejection cases run alongside. - `Tests/AllocationImports.cs`, in `core`, checks that the CIL umbrella omits allocation refinement, builds both explicit allocation acceptance modules, and jointly replays every @@ -116,7 +117,7 @@ equivalence theorem or property stated for each index and the regression that co | Region suffix membership | Fallthrough closure is automatic, since `lower ≤ pc` implies `lower ≤ pc + 1`, so the checker looks up only explicit branch targets and callees. Its equivalence theorem covers every instruction, including a final fallthrough beyond the body; instruction availability is a separate execution obligation. | `Tests.RegionRun` | | Constrained validation and lowering | Balanced instruction/reachability indexes and a set of all decoded branch destinations, including unreachable instructions and switch arms; lowering also indexes predecessor instructions. `lowerConstrainedRaw_eq_original` proves identical output for arbitrary raw bodies, including malformed prefixes. `ConstrainedCallsValid_iff_original` preserves the list-based rules. | Constrained evidence suites | | Sequential constrained evidence | Validation consumes the raw, reachability and emitted lists together, keeping the previous instruction and reading the next from the tail; lowering keeps the raw predecessor before rewriting it. Malformed-neighbor and length rejection, method-target lookup, branch membership and dispatch metadata resolution are unchanged checks. | Constrained evidence suites | -| Managed-call validation | Indexes extracted methods and imported slots once per method check. `ManagedCallsValid_iff_original` preserves the list-based rules, including absent entries, import offsets and malformed lengths. `cil_managed_calls` uses structural metadata equalities before its kernel-decision fallback. | Import evidence suites | +| Managed-call validation | Indexes extracted methods and imported slots once per method check, requires the link table's keys to ascend and to be pcs of the code, and reads each pc's link by one walk beside the code. `ManagedCallsValid_iff_original` shows the check accepts exactly the bodies whose calls and constructions pass the list-based rules with the link a scan of the table gives each pc, including absent entries, import offsets and malformed lengths. `ManagedCallsValid.linkFits` shows that such a table gives a `call` pc no link when the call's method has a runtime model and a direct link otherwise, and each other pc except a `newobj` the link its instruction implies, and `validated_managed_call` that each live pc passes the linked rule, which at a `newobj` ties the link to the raw site (`validateConstructionWith`). Generated `methodN_calls_validated` theorems decide this check in the kernel, so each pc's link is read by the walk rather than by a scan of the table. | Import evidence suites; the `lookups` call check row | | Home discovery, static sites, opaque locals | Balanced indexes: `Std.TreeSet`/`Std.TreeMap` for keyed membership and `FlowIndex` for positional lookup. Home discovery collects live stores and addresses once and keeps argument order while filtering the argument range. Static sites keep the first descriptor at a duplicate pc. Opaque-local discovery considers addresses in the complete raw body, including dead instructions, and reads each type from `zipIdx`. Equivalence theorems cover arbitrary input lists, including malformed indexes, duplicate sites and truncated masks; they require no new assumptions. | `Tests.ExtractionHomeIndexes` | | Parameter home enumeration | Enumerates the collected ordered set and filters by the argument bound. The proved list equality keeps ascending order, duplicate elimination, out-of-range filtering and truncated reachability semantics. | `Tests/HomeEnumerationScaling.lean` compares both algorithms | | Argument instruction validation | Shares indexes for argument types and appended home slots; expected home kinds use the argument-type index. The home-slot map keeps the first occurrence for duplicate home lists; missing argument indexes, hidden home accesses and unsupported replacements are rejected. Exact lookup/check equivalence theorems preserve the semantic consequence statements. | `Tests.ExtractionHomeIndexes` | @@ -138,9 +139,16 @@ These regressions cover the extractor's own C# indexes: mutations are visible. `Tests/InstructionIndexes.cs` includes those rejection and mutation cases. - Template emission writes templates and callees in sorted order and profiles in input order, - and rejects duplicate method identities before emission. An absent callee in an excluded - profile gets a zero placeholder. `Tests/TemplateIndexes.cs` checks reordered methods, - conditional absent callees, deterministic output and duplicate rejection without compiling Lean. + and rejects duplicate method identities before emission. Every method a body or frame names (calls, + tail calls, virtual dispatch targets, callable pointer targets and static initializers) becomes a callee + parameter, and an absent callee in an excluded profile gets a zero placeholder. Profiles merge when + their renumbered instructions agree at every pc for the same observed flags. Lean reads the + selected instruction lists from decoded methods and proves each selected leaf equals its input's + code. Each input must pass extraction checking. Differing frames, lengths or unflagged + instructions cause rejection. + `Tests/TemplateIndexes.cs` checks reordered methods, each kind of callee, conditional absent + callees, those merge rules, deterministic output, duplicate rejection and linear growth in the + instructions without compiling Lean. - Loaded contract libraries index exports by exact identity and certifications by exact (identity, profile). The production reader rejects duplicates before constructing either index. `Tests/ImportIndexes.cs` checks case-sensitive lookup, multiple profiles and the reader's exact @@ -164,8 +172,9 @@ These regressions cover the extractor's own C# indexes: mutation visibility, distinct x64/arm64 vector layouts and nesting rejection after warming a child. - C# argument translation indexes argument home positions once per method in validation, - emission and exception lint. Duplicate inputs keep their first position, missing homes stay - absent, and raw locals are added to the home position when emitting local-slot instructions. + emission and exception lint. Duplicate inputs keep their first position and missing homes stay + absent. A homed argument's load, store and address keep the argument index, and a store or + address of an argument without a home is refused. `Tests/InstructionIndexes.cs` exercises loads, stores, addresses, duplicate homes and missing-store rejection. - Interface-module merging detects spelling collisions ignoring ordinal case and writes its @@ -184,9 +193,27 @@ These regressions cover the extractor's own C# indexes: resolve as unknown while random seeds do. - Layout computation accumulates field placements in reverse and restores declaration order once. `computeLayout_eq_append` proves equality to the complete list-based computation for - arbitrary layouts, architectures and nested-layout tables. `Tests.LayoutAccumulation` covers + arbitrary layouts, architectures and nested-layout lookups. `Tests.LayoutAccumulation` covers order, explicit overlaps, empty layouts, missing children, packing/offset failures and vector architecture restrictions. +- The `Decidable` instance of `LayoutsValid` runs `layoutsValidIndexed` on the raw + and claimed tables for `Extracted.layouts_validated`, import catalogs and projections. Each + method reuses the extraction's table proof and checks its own layout uses. + Each identity names one row. Validation checks uniqueness with an ordered set of UTF-8 + keys. Two successful reads with equal identities have equal indices. The field check + reads raw entries and claimed layouts through balanced indexes built once, lets each entry read + only claimed layouts at earlier positions, and finds closed generic owners through an ordered + map keyed by closed type that returns the first match. For `L` layouts with `A` fields in total + it costs `O((L + A) log L)` lookups, plus, for each `.apply` node of a generic layout's argument + and original field types, one owner lookup keyed by that node's closed subtree. + `layoutsValidIndexed_iff` proves the pass equivalent to `LayoutsValid` for arbitrary inputs, + and `LayoutTable.indexed_eq` proves the indexed reads equal to the list scans that + `computeLayouts` uses. `Tests.LayoutAccumulation` checks that a wrong offset, a wrong size, a + child at, after or past its parent, and a wrong length are rejected by both checks. Of the + generic cases, a class owner and a mismatched owner identity are rejected before any owner + lookup, while a missing owner, an owner at another position and the wrong one of two + same-shaped owners are rejected through the owner index, which also finds the first of two + equal closed types. A class application never closes to a layout entry that claims it. - Constrained dispatch validation and lowering each build ordered dispatch candidate indexes once per invocation and reuse them across call sites, never across invocations. The indexes support type identity, method token/name and explicit interface override lookup. Candidate @@ -198,11 +225,10 @@ These regressions cover the extractor's own C# indexes: `constrainedSiteWindow`, `constrainedSites`, `lowerConstrainedInstruction`, `lowerConstrainedCode` and their streaming equivalence theorems accept a resolver function. - Public layout projection validation indexes artifact selection and nested public-index - expansion, and uses ordered sets for its three uniqueness checks. + expansion. Both catalogs satisfy `LayoutsValid`, and the selected artifact indices are distinct. `LayoutProjectionValid_iff_list` proves exact equivalence to all list-based conditions for arbitrary inputs, including malformed indices and catalogs. The unfolded proposition exposes - `projectionDistinct ... = true` and `projectionMetadataMatches`; recover the list-based - uniqueness and metadata conditions through `LayoutProjectionValid_iff_list`. + `orderedDistinct` for the indices and `projectionMetadataMatches` for the layout rows. `Tests.LayoutProjectionIndexes` checks reordering, empty catalogs, duplicate identities/indices, case-sensitive identity, missing indices and nested expansion. @@ -345,15 +371,15 @@ These regressions cover joint replay: cases. - Generic substitution, managed-call and method-shape mutations select their corresponding emitted validator; type-test mutations select feature-flow validation. External-call mutation fixtures - check only the intended division or intrinsic validator, with a complete positive certificate + check body binding and runtime model agreement, with a complete positive certificate and a checked unchanged-obligation control in the replayed evidence batch. Unchanged controls compile first, with repeated generic controls deduplicated. Byte-backed identity definitions keep their UTF-8 witnesses. Evidence-list omission and reordering check the complete certificate, because they test agreement between records and no single validator. - Closed typeof comparisons cover value-producing calls, operandless `ceq` and short/long reference-equality branches. The branch cases check both identity outcomes in shared - feature/scalar certificates; forged targets, wrong opcodes, forwarded scopes and branches into - erased operands fail independently. Runtime comparisons exercise three closed generic + feature and scalar certificates. Forged targets, wrong opcodes, forwarded scopes and branches + into the middle of a type comparison fail independently. Runtime comparisons exercise three closed generic instantiations in one generated proof batch. ### Full verification and publication @@ -407,15 +433,13 @@ These regressions cover joint replay: ### Fold discovery -Fold discovery counts decoded local references once. `foldRoles_eq_filter` proves exact equality -to the filter-based specification, including malformed operands and out-of-range local indexes. -Methods without locals skip count construction. The positional lookup of local types is linear, -so fold discovery as a whole is not linear. +Fold discovery reads local types from a `FlowIndex` built once per body. Span receiver stores and +addresses have no fold roles; their raw opcodes bind the emitted local instructions independently. Exception folds inspect at most three message loads, since admitted constructors accept at most two. `throwFold_eq_takeWhile` proves equality to the unrestricted prefix scan. A long rejected -prefix does not prevent an admitted fold starting later. Span and throw evidence tests cover extra -uses, malformed local operands, oversized prefixes and valid suffix folds. +prefix does not prevent an admitted fold starting later. Span and throw evidence tests cover exact +constructors, receiver instructions, oversized prefixes and valid suffix folds. ### Transitive composition replay @@ -439,8 +463,10 @@ and traces match exactly. `Subtract` use the checked allocation-relative reference operations, whose admitted forms [OPCODES](OPCODES.md#reference-observations) lists and whose limits [reference observations](SOUNDNESS-EVIDENCE.md#reference-observations) states. Generic storage -types must have a supported managed layout. Lean translation evidence checks exact runtime -identity, closed signatures, offset signedness and byte scaling. `ByteOffset` accepts readonly +types must have a supported managed layout. The comparisons and offsets keep their calls. Lean binds each closed overload to its external +identity and generic declaration. The model uses the selected displacement type for signedness +and the indexed element size for scaling. +`ByteOffset` accepts readonly views, and byte and element offsets from a readonly reference stay readonly. Tests compare native wraparound with the runtime and reject forged strides and signedness in Lean diff --git a/docs/VALIDATION-PROOFS.md b/docs/VALIDATION-PROOFS.md index 530b167f1..956fd2c45 100644 --- a/docs/VALIDATION-PROOFS.md +++ b/docs/VALIDATION-PROOFS.md @@ -17,8 +17,8 @@ library and tactic regressions kernel-check the resulting proofs. ### Stepping -- `Tests.Automation` steps an eight-instruction arithmetic method (`arg, arg, const32, shl, add, - const32, gtu, ret`) with symbolic inputs and arbitrary caller memory using `cil_checked_steps`; +- `Tests.Automation` steps an eight-instruction arithmetic method (`arg, arg, ldc_i4, shl, add, + ldc_i4, cgt_un, ret`) with symbolic inputs and arbitrary caller memory using `cil_checked_steps`; its axiom assertion permits only `propext` and `Quot.sound`. It checks both paths of a symbolic branch from a supplied truth hypothesis, and that failed candidate summaries do not escape their transaction. Memory and static dispatch facts keep abstract bitcast results in inferred stacks; @@ -28,17 +28,26 @@ library and tactic regressions kernel-check the resulting proofs. use checked invocation and the shared feature evaluator. - `Tests.StoreFacts` checks symbolic carry and borrow stores whose write facts state `a + b + c`, `a - b - c` and the flag words in source form, through both `cil_checked_steps` and - `cil_contract_steps`. A wrong stored value, address or source memory leaves the run stuck at that - store, with the exact unsolved goal asserted. It also counts exactly 12 - `trace.CIL.factNormalize` events for a 27-instruction run. + `cil_contract_steps`, and a native-int `stind.i` store stepped from the same kind of eight-byte + write fact. A wrong stored value, address or source memory leaves the run stuck at that store, + with the exact unsolved goal asserted. It also counts exactly 12 `trace.CIL.factNormalize` + events for a 27-instruction run. - `Tests.AggregateSetup` and `Tests.Semantics` cover frame setup and interpreter behavior. +- `Tests.BodyDecoding` checks the method body decoder against hand-made bodies, one rejection per + failure kind and the operand table against a static list generated from Mono.Cecil. +- `Tests.MetadataDecoding` and `Tests.MetadataBinding` check the metadata table, heap and + signature decoders and the binding of operands, locals and method definitions to carried + metadata, as [VALIDATION-COVERAGE](VALIDATION-COVERAGE.md) describes. - `Tests.InterpreterEquations` checks that each shared equation lemma is stored by its LeanCIL module, not rebuilt by the importer, and that a tactic macro naming `CIL.step.eq_def` elaborates. - `Tests.InstructionEquations` checks that `cil_instruction_equations` states one equation per pc of a body over two feature flags and a callee index, keeping each feature test as an `if`, and of bodies with no parameters, with implicit and hypothesis parameters, and composed with `++` and - `List.replicate`; that `cil_lookup` fetches through them from a sealed body; that facts or + `List.replicate`; that it states a link fact for each call site whose instruction fixes its link + and none for other pcs; that `cil_lookup` fetches through them from a sealed body; that a step of + a sealed body reads a call's link from the generated fact or an extraction's named one, and faults + at a call whose link is of another kind; that a run faults at a call site its table omits; that facts or literal flags decide the test, in lookups and in a checked run; and that a run proved over the flags binds two profiles. A body whose length depends on a flag, at its first pc or a later one, is rejected with nothing declared. @@ -67,10 +76,11 @@ library and tactic regressions kernel-check the resulting proofs. opaque vector locals. It checks empty and unmodeled span slots, both mutabilities and every supported width. It rejects unsupported widths, scalar span initializers, numeric dead locals (in the kernel and in the tactic) and non-trailing dead locals, and reports a changed live - instruction under dead locals. + instruction under dead locals. It also rejects an embedding whose declared signature, + layout environment or call link differs, and names the pc of a differing link. - `Tests.OutcomeEmbedding` checks transport of named exceptions and outcome contracts and the - relocated fault pc, and that changed throws, unsupported execution and fuel exhaustion do not - transfer. + relocated fault pc, that changed throws, unsupported execution and fuel exhaustion do not + transfer, and that a hole's fault is not settled, so no lemma refutes a raise from it. - `Tests.RetainedNumericLocals` keeps dead numeric declarations before and after a live local, transfers exact-memory executions and named exceptions through nested allocating calls, and checks that deleting a numeric local changes the allocation watermark and fails ordinary @@ -97,6 +107,7 @@ library and tactic regressions kernel-check the resulting proofs. - `Tests.LoopOutcome` derives the normal-only loop induction, loop call and `Ensures` as the `none` instances of the outcome rules, and proves that unsupported execution, invalid state, fuel exhaustion, normal return and a different exception never satisfy an expected loop exception. + All but unsupported execution refute one; a hole's fault is not settled. - `Tests.LoopScaffold` proves a countdown and a span fold over symbolic memory through their `LoopFrame`s, naming no frame or span fact, and shows for each that a constant measure fails at the decrease goal and an invariant that is not preserved fails at its preservation goal. @@ -127,12 +138,13 @@ library and tactic regressions kernel-check the resulting proofs. one word, with no frame lemma, memory or store named: the loop tactics step through the local's stores, carry the `Unsafe.Add` result and write authority, and cross the call with the frame after it. Without the `Unsafe.Add` fact the tactics stop at the add and the failure - names that fact; the tactics stop at a narrowing local store; without the index bound the - failure at the call names the footprint side condition; for an unformed argument the failure - names the argument check; and for a contract input whose arguments differ from the stack the - failure names both the arguments and the stack. + names that fact; without the index bound the failure at the call names the footprint side + condition; for an unformed argument the failure names the argument check; and for a contract + input whose arguments differ from the stack the failure names both the arguments and the stack. - At a return `cil_loop_finish` closes the results and confinement and leaves the rest of the postcondition open. + - `cil_loop_finish` steps a store of 0x1FF to a byte local, its load and the return, and closes + the postcondition that the result is 0xFF, the low byte the local's slot keeps. - The proofs select what the tactics leave by its obligation tag, so a dropped or renamed tag fails the build: `invariant` and `measure` after `cil_loop_recurse`, `precondition.stampMethod` for a named callee's open precondition, the third conjunct's @@ -179,8 +191,9 @@ every case; the prefix theorem checks the widened value. - `Tests.OutcomeContract` proves every outcome of a two-level chain from the callees' contracts. `Tests/OutcomeContract.cs` verifies it against the compiled build and refutes the callee's contract on builds with an off-by-one threshold or a different exception type. -- `Tests.RuntimeExceptions` proves every mapped CLR exception, refutes wrong names and proves - unsigned division and remainder by zero for every dividend and caller memory; +- `Tests.RuntimeExceptions` proves every mapped CLR exception, refutes wrong names, shows that a + hole's fault names no exception and is not settled, and proves unsigned division and remainder + by zero for every dividend and caller memory; `Tests/RuntimeExceptions.cs` checks each against its compiled C# method. - `Tests.ContractConsequence` derives public method and outcome contracts from proved internal ones without re-executing the callee, composes callers through the derived contracts, and proves @@ -210,7 +223,8 @@ every case; the prefix theorem checks the widened value. implementation, shows the unlinked program faults at the import's call, and composes an imported raising `Percent`. - `Tests.Linking` links an `AddTo` library whose entry calls its own relocated `Sum` helper and a - `Percent` library whose raise reaches `Root` through two relocated calls. + `Percent` library whose raise reaches `Root` through two relocated calls, and checks that relocating a + body renames its link table with its code. - `Tests.ProfileContract` proves a contract for a method that tests AVX2 once, under x86-64-v3, and carries it to x86-64-v4, to v3 without 256-bit acceleration and to every profile with AVX2 on. A raising method's outcome contract carries over too, in outcome, CLR-named and @@ -303,7 +317,9 @@ These checks cover the behavior that [binding and transport](PROOFS-BINDING.md) changes the managed callee numbering through a wrapper entry point. The suite replays the generated equations, extraction certificates, bindings and consumer proofs in the kernel, and rejects changed flag values, callees, branch targets, instruction equations, stale extraction - source and incompatible profile-dependent parameter homes. + source and incompatible profile-dependent parameter homes. It also compiles a template and + both profile bindings for a virtual call, a method address, a callable pointer call and a + static initializer, each of whose method indices differs between the two profiles. ## Store view reuse diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index bf21b1040..7c91f2bed 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -27,6 +27,18 @@ dotnet run --file scripts/Test.cs -- --suite outlines-objects - A C# change requires analyzer validation, including changed file-based tests. - Any `sorryAx`, unexpected diagnostic or failed proof is a failure. +The blockers suite checks dead instructions in methods admitted from the extractor assembly under +the scalar profile. Its hole corpus baseline pins argument indices requiring homes only at dead +instructions, dead prefix positions and opcodes other than `unaligned.`, and the count of refused +methods skipped. The per-method lists are written to `artifacts/HoleCorpus/methods.json`; admission +results are reused from the self-admission check. + +`dotnet run --file Tests/SelfAdmission.cs -- --lean-check` also extracts each admitted method alone and +elaborates it against the checkout's `CIL`, printing each method Lean refuses and failing when there is +one, which finds an admission the extractor grants and the Lean checks refuse. It is run by hand when a +change can move verdicts between the two, never by a suite: over the roughly 3750 admitted methods it +takes about 2.7 hours of CPU time. + | Changed area | Relevant suites or checks | | --- | --- | | Extraction certificates and metadata | `evidence`, plus the affected opcode or runtime-helper suite | @@ -53,13 +65,14 @@ come from [VerificationSuites.cs](../scripts/VerificationSuites.cs) and | Suite | Checks | | --- | --- | -| `core` | the runner's own checks: example proofs and their rejections, the shared Lean test libraries including the public proof surface, binding catalogs and certified integer binding families, gate plans and required results, method selectors, caches, batched extraction and joint kernel replay | +| `core` | the runner's own checks: example proofs and their rejections, the shared Lean test libraries including the public proof surface, replay diagnostics, return narrowing and metadata restrictions, value opcode decoding, the extractor's table of decoded opcodes and decoding bodies with unreachable instructions and checking their frames and links, methods decoded from body bytes and defined by evaluating the decoder, Add extracted with no method literal, object array steps, embedding retention and profiles, full transfer, SHA-1 and assembly and pin index scaling, binding catalogs and certified integer binding families, gate plans and required results, method selectors, caches, support audit reuse bound to extraction identity, batched extraction and joint kernel replay | | `templates` | shared profile templates, exact instruction equations and checked bindings | | `workspace` | per-method editor extractions, watch refresh and ordinary fresh certification | | `imports` | implementation-bound contract imports and conditional caller verification | +| `closures` | summaries at relocated indices, contract reuse for identical executable bodies, and rejection of decoded links to different method tokens | | `exceptionlint` | diagnostic XML exception drift without proof claims | | `contracts` | universal checked contracts, caller memory preservation and incorrect implementations | -| `evidence` | independent extraction certificates and malformed evidence rejection | +| `evidence` | independent extraction certificates, malformed evidence rejection, the metadata decoder's method names against Cecil's and framework argument guards | | `dispatch` | constrained and managed dispatch, constant calls, stack allocation and pinned compiler coverage | | `loops` | cyclic control-flow validation, universal termination, negative contracts and class/word-array composition | | `outlines-control` | generated outlines for control flow, arithmetic and value types, completed proofs and rejection checks | @@ -73,7 +86,7 @@ come from [VerificationSuites.cs](../scripts/VerificationSuites.cs) and | `blocks` | block memory, alignment prefixes, runtime comparisons and safety faults | | `unsafe` | layout-based Unsafe sizes, reference offsets and metadata validation | | `math` | integer Math helpers, BigMul, runtime faults and universal minimum | -| `intrinsics` | exact SIMD call signatures, byte shuffles, AES rounds, unspecified lanes, opaque locals and profile rejection | +| `intrinsics` | exact SIMD call signatures, 128-, 256- and 512-bit equality contracts with swapped-word and repeated-operand counterexamples, 32-byte half swaps over arbitrary bytes at eight source/destination placements with a repeated-load counterexample, byte shuffles, AES rounds, unspecified lanes, opaque locals, profile rejection and the library of generated runtime comparisons | | `divrem` | X86Base.X64.DivRem and Math.DivRem through ValueTuple fields, divide faults and layout rejection | | `fsharp` | F# arithmetic, branches, calls, references and struct construction | | `constructors` | struct construction, initialized temporaries and constructor faults | @@ -84,7 +97,7 @@ come from [VerificationSuites.cs](../scripts/VerificationSuites.cs) and | `failures` | failure reports that classify and locate each failed obligation | | `integer` | integer opcode encodings, boundary cases and universal negation proofs | | `narrow` | narrow integer conversions, storage, method boundaries and rejection proofs | -| `primitive` | primitive memory, switch, sizeof and rejection proofs | +| `primitive` | primitive memory, switch, sizeof and rejection proofs, and strings as opaque references | | `division` | integer division and remainder runtime comparisons and proofs | | `checked` | checked integer arithmetic runtime comparisons and proofs | | `conversions` | checked integer conversions runtime comparisons and proofs | @@ -93,21 +106,21 @@ come from [VerificationSuites.cs](../scripts/VerificationSuites.cs) and | `copy` | typed copies, aliases, byte snapshots and rejection proofs | | `arguments` | argument assignment, addressing, aliases and lifetime proofs | | `native` | native integer storage, arithmetic and runtime comparisons | -| `identity` | methods bound by signature or call shape that survive renumbering and renaming | +| `identity` | methods bound by signature or call shape that survive renumbering and renaming, and typed identity display | | `labels` | loop-head and call-site labels that survive inserted statements | | `names` | PDB local names and source locations that never change extraction | | `symbols` | consumer proofs naming PDB locals through helper modules, bound only where imported | | `callers` | caller proofs that survive reimplemented helpers through their contracts | | `construction` | caller proofs that survive reimplemented constructors through their contracts | | `outcomes` | outcome contracts that name exactly when each exception is thrown | -| `scaling` | compositional certificate for a six-method call graph and its consumer replay | +| `scaling` | compositional certificate for a six-method call graph and its consumer replay, declared signature checks at flat per-method cost over 1, 4 and 16 parameters, header and per-method struct layout checks at a per-layout cost that does not grow with the table, and the generic owner index library | | `excluded` | hundreds of excluded and live sites at the default heartbeat budget | -| `lookups` | lookup equations of extracted methods, hand-written bodies and templates at a per-instruction cost that does not grow with the method | +| `lookups` | lookup equations of extracted methods, hand-written bodies and templates, and the scalar certificate of array element addresses, loaded and addressed integer locals and native-int locals consumed by calli, the closed argument storage of those native-int locals, and the feature certificate of local loads, stores and addresses, at a per-instruction cost that does not grow with the method or with locals holding no known word and grows linearly with the known facts, embeddings of bodies with many calls at a per-call kernel cost that does not grow with the calls, type identity equality at a cost that does not grow with the name, the method name clause of the call operand check and the encoding of a metadata name at a per-byte cost that does not grow with the name, the bytes of a name stored as its bytes at a per-byte cost that does not grow with the name and, with the name's definition, below the cost of the bytes of its literal, and one native method table step and one load of its first entry at a kernel cost that does not grow with the table's entries; the managed-call check of a body with many call sites at a per-site cost that does not grow with the call sites; the call check and the evidence theorem of a body with no call at a per-statement and a total cost that do not grow with the body; the proof that 8, 32 and 128 evidence records decode to their methods at a per-record cost that does not grow with the records; the closure of a method in a program at a cost per unit of methods times program length plus link callees that does not grow with the methods, the callees or the program; the argument facts of `cil_resolution_facts` and the reads through them at a per-argument cost that does not grow with the parameters, and its static site facts and the reads through them at a per-site cost that does not grow with the sites; the program's static identity check at a per-site cost that grows less than 1.35 times from 8 to 32 to 128 sites; the array element shape facts of `cil_resolution_facts` and the reads through them at a per-element-type cost that does not grow with the layouts or the sites naming each type; a runtime model lookup at a kernel cost that does not grow with the model table, and the runtime model agreement check at a per-site cost that does not grow with the call sites; the shape facts of `cil_field_shape` and the reads through them at a per-field cost that grows less than 1.35 times from 8 to 32 to 128 fields in one struct and from 8 to 32 to 128 to 512 fields in a struct each, and the facts of `cil_value_shape` and their reads at a per-type cost that grows less than 1.35 times from 8 to 32 to 128 to 512 struct types; locating a class field on an instance, and rejecting a field whose owner is outside the instance's lineage, at a per-base cost that grows less than 1.35 times from 2 to 8 to 32 bases; the kernel check of one body at a cost independent of the methods and metadata rows of its extraction, and at a per-instruction cost that does not grow with the string tokens of the body; the kernel's selection of a calli body at a cost linear in the length of the pointer's owner; raw value opcode decoding and the method decoded from a raw body at a per-instruction kernel cost that does not grow with the body, and the method `cil_decoded_method` defines from that body at a per-instruction cost that does not grow with the body; and the certificate index, feature getter, layout accumulation and projection, and extraction home index libraries | | `gates` | manifest project builds, extraction, named profiles, per-variant and templated proofs, witness refutations, kernel statement comparison, binding catalogs, planned groups, changed-source selection and mutation expectations | -| `stepping` | full scalar and checked-memory bodies with per-instruction heartbeat, proof-size and latency budgets | -| `assemblies` | bodies, layouts and generic instantiations from pinned referenced assemblies, attribution and substitution rejection | +| `stepping` | full scalar, checked-memory, reference-offset, native-table-offset, running-sum, locals, contract, loop, loop-facts, outcome, lemma, depth, hypotheses, snapshot, faulting, arguments, homes, array-allocation, calls, table-calls, class-field-load, statics, construction, object-constructor and array-element bodies with per-instruction heartbeat and proof-size budgets and a per-hypothesis heartbeat limit | +| `assemblies` | bodies, layouts and generic instantiations from pinned referenced assemblies, attribution and substitution rejection, the strict binding library and audit, and the checked-in strict binding fixture module | | `pool` | object pool reuse, creation, exhaustion, null-pool and free-list search proofs and the mutations they reject | -| `blockers` | admission reports with deduplicated blockers, call chains, profile-excluded branches and self-admission | +| `blockers` | admission reports with deduplicated blockers, call chains, profile-excluded branches, runtime-async refusal, self-admission, the admitted hole corpus and the self-admitted, example and fixture methods defined from their decoder | | `audit` | theorem audits of replayed oleans that proof elaborators, initializers and substituted artifacts cannot forge | | `docs` | documentation Lean snippets, cited library names and documented commands against the library and tool | | `exports` | contract production, restrictions and caller reuse | @@ -139,6 +152,10 @@ partition. ## Proof cost +The `lookups` closed-argument row checks primitive storage at 1, 4 and 16 parameters with +8, 32 and 128 unrelated layouts. It bounds the cost per parameter and rejects growth with +unrelated layouts. The row accepts `--closed-arguments-only` for isolated measurement. + For a change to shared tactics or the library, also compare what each proof costs with the base. Measure both checkouts on one machine: @@ -173,14 +190,104 @@ accept an increase. dotnet run --file scripts/Test.cs -- --suite stepping ``` -The `stepping` suite generates explicit scalar and checked-memory bodies near 16, 64 and 256 -instructions and proves each invocation end to end. The memory bodies repeat an idempotent -checked store and reload, so the operation facts stay constant as the instruction count grows. -The suite uses the proof-cost measuring frontend, with three sequential proof compilations per -body. +The `stepping` suite generates explicit scalar, checked-memory, reference, table, accumulate, +locals, contract, loop, loop-facts, outcome, lemma, depth, hypotheses, snapshot, faulting, +arguments, homes, allocation, calls, table-calls, objects, statics, constructions, +object-constructor, elements, typed-elements and typed-steps bodies and proves each end to end. +The scalar, memory, reference, table, accumulate and locals bodies have 16, 64 and 256 +instructions, and the accumulate body also 1,024. The memory bodies repeat an idempotent checked +store and reload, so the operation facts stay constant as the +instruction count grows. The reference and table bodies repeatedly `add` a symbolic int32 byte +offset to a reference argument or a native method table argument and pop the result, each step +reading one supplied fact that the offset succeeds. The accumulate, contract, loop, loop-facts, +outcome and lemma bodies keep a running sum on the stack, so the symbolic value a step reads grows +with the program counter. The contract body is proved by `cil_contract_steps`, the loop body by +`cil_loop_steps` along a path, the loop-facts body by the same with a case fact in the context that +every instruction reads, the outcome body by `cil_loop_outcome_advance`, and the lemma body by +`cil_checked_steps` with a universe-polymorphic lemma among its facts. The locals bodies store a +reference into each of eight locals of a literal frame per cycle. The depth bodies push 8, 32 and +128 values and add them all. The hypotheses bodies are 62-instruction accumulate, contract and loop +bodies proved with 16, 64 and 256 hypotheses in scope, a contract body with as many +instruction-lookup equations in scope, and the lookups of every instruction of the accumulate body +made by `cil_lookup` outside a stepping run, with 16 and 256 equations in scope. The faulting bodies +have 17 and 62 instructions and repeat `div`, `rem`, `add.ovf` and `conv.ovf.i4` over the arguments, +each advanced by a supplied fact that the operation does not fault. The snapshot bodies store into +eight numeric locals and then update one local per store from its remembered value, with 10, 20, 40 +and 80 stores, proved by `cil_contract_steps (snapshots := ..)` with caller memory facts in the +context. Each store clears the facts it supersedes, so the hypotheses in scope stay level, while the +value a store writes grows with the number of updates to its local; the cost per instruction rises +slowly with the stores. The arguments bodies have 16, 64 and 256 instructions and add 8, 32 and 128 +`int32` arguments of a declared signature, each read once, with the method's `cil_resolution_facts` +in scope. The homes bodies have 16, 64 and 256 instructions and read each of 8, 32 and 128 reference +parameters once from its home in a literal frame. The allocation bodies have 16, 64 and 256 +instructions and allocate and pop an array of two class references per cycle, each `newarr` +resolving its element shape and reading one supplied allocation fact. The calls bodies have 16, 64 +and 256 instructions and pass their argument through 14, 62 and 254 calls of a callee that returns +it, each call reading its target from a link fact that `cil_instruction_equations` states; the +table-calls bodies are the same bodies with their link table written out, as a template writes it. +The objects bodies have 16, 64 and 256 instructions and load and pop an `int64` field of their +object argument per cycle, each `ldfld` reading the field's `cil_field_shape` fact and two supplied +facts that locate and read its slot; `cil.requireFieldShapeFacts` fails the body if a step +evaluates the shape instead of reading the fact. The struct fields bodies have 16, 64 and 256 +instructions and load and pop the `int64` field at byte 8 of the struct their reference argument +addresses, then store their `int64` argument there, per cycle; each `ldfld` and `stfld` reads the +field's `cil_field_shape` fact and supplied facts that locate the field and read or write it, and +the struct fields contract bodies step the same bodies under contract stepping. At each size, both workloads compare the checked theorem's heartbeats with the recorded +cost of the lowered body at `8cc33b38` on Lean 4.35.0. The instruction counts must match. +The intrinsic call workload compares repeated typed `Math.Max(long, long)` calls against the +same program at `9c53e7f5`. Both field and intrinsic call workloads allow 1.03 times the reference +frontend heartbeats and 1.08 times the reference kernel heartbeats. The statics bodies have 16, 64 and 256 +instructions and load a static field and pop it per cycle, with one static site per load, so each +step resolves its pc among all the body's sites. The constructions bodies have 16, 64 and 256 +instructions and construct and pop a class instance per cycle, each `newobj` reading its link and +one supplied allocation fact and entering a constructor that returns. The object-constructor +bodies have 16, 64 and 256 instructions and call the `System.Object` constructor on their object +argument per cycle, each call reading that its pc has no link and the runtime model its pc's +`cil_runtime_models` fact states. The intrinsic-calls bodies have 14, 62 and 254 instructions and +call `Math.Max` on their two `int64` arguments and pop the result per cycle, each call reading the +same two facts and then stepping as its scalar intrinsic. The instance bodies have 16, 64 and 256 +instructions and make one receiver-checked instance call on their object argument per cycle into a +method that returns at once. The virtual bodies make 16 virtual calls on it through a dispatch +table of 1, 16 or 64 classes, the receiver's class last, each call reading one supplied runtime +type fact and looking the class up in the table. The elements bodies have 16, +64 and 256 instructions and per cycle load and pop an `int32` element of their array argument and +store their index argument into it, each access reading one supplied element fact. The +typed-elements bodies have 16, 58 and 254 instructions and per cycle do the same through `ldelem` +and `stelem` with an `int32` type operand, then copy one struct element of a second array onto +itself. Each typed access steps by the fact `step_ldelem_any_of_element`, +`step_stelem_any_of_write`, `step_ldelem_any_of_transfer` or `step_stelem_any_of_transfer` +derives from supplied element facts and the generated shape fact of the body. The typed-steps +bodies are the same bodies with one supplied step fact per access. +The addresses bodies have 11, 56 and 254 instructions and per cycle form and pop a writable and a +`readonly.` address of an `int32` element of their array argument, each `ldelema` stepped by the +fact `step_ldelema_of_element` derives from one supplied element fact and the generated shape +fact of the body. The address-steps bodies are the same bodies with one supplied step fact per +address. +The suite uses the proof-cost measuring frontend, with three sequential proof compilations and a +header-only baseline per scalar, memory, reference, table, accumulate, locals, calls and table-calls +body up to 256 instructions, and one compilation for every other body. -- For each successive size, frontend heartbeats, kernel heartbeats and compiled proof bytes per - instruction may grow by at most 1.35 times, and median frontend milliseconds by at most twice. +- For each successive size, frontend and kernel heartbeats per instruction may grow by at most + 1.35 times. The arguments and homes bodies read one of a list of arguments or home slots that + grows with the body, so their heartbeats per instruction may grow at most by the ratio of the + sizes, linearly in the number of arguments. The virtual body makes 16 virtual calls through a + dispatch table whose size is the row's size, and each call looks its receiver's class up in that + list, so its heartbeats per instruction may grow at most by the ratio of the table sizes. For + the bodies with three compilations, compiled + proof bytes per instruction may also grow by at most 1.35 times. Frontend milliseconds are + recorded for comparison between runs. +- For the hypotheses bodies the sizes are the numbers of hypotheses: in the accumulate, contract + and loop bodies half of them are equations headed by an operator and half by a local variable, + in the lookup body each is an instruction lookup like those stepping makes, + `([.ldc_i4 i] : List CIL.Instruction)[0]? = some (.ldc_i4 i)`, and in the direct lookups + each is an equation headed by an operator. The heartbeats and frontend milliseconds counted are + those of the proof less those of the same statement closed by `sorry`, so that elaborating the + hypotheses does not count as stepping. The same ratio limit applies, except to the direct + lookups, which read every hypothesis in scope once each. One more hypothesis in scope may add + at most 100 heartbeats to an instruction, which costs about 120,000, or to a direct lookup, + between the smallest and the largest size. A lookup that tries to unify with every equation + hypothesis adds 600 to 1,950 and fails that limit, as does a direct lookup that builds an index + of the hypotheses, which adds about 410; reading each hypothesis once adds about 25. - Heartbeats and bytes must repeat exactly across the three runs. - Frontend heartbeats include the initial kernel check; the kernel columns measure a separate recheck. Compiled proof bytes subtract the same module containing only the body and its lookup diff --git a/docs/evidence/native/README.md b/docs/evidence/native/README.md index bd84748fa..77a7fca19 100644 --- a/docs/evidence/native/README.md +++ b/docs/evidence/native/README.md @@ -50,8 +50,8 @@ manifest's `historicalDivergences` field: the expectation is the zero-extension the extractor rejects the case. `NativeOperands` rejects mixed `bne.un`, `blt.un`, `bgt.un`, `ble.un`, and `bge.un`, including -every short form. The checked native wrapper rejects them too. The diagnostic requires explicit -`conv.i` or `conv.u`; those same-width forms are supported and tested. Accepting argument-only +every short form. `CIL.step` and the checked interpreter reject them too. The diagnostic requires +explicit `conv.i` or `conv.u`; those same-width forms are supported and tested. Accepting argument-only cases would require an extra runtime assumption: inlining can expose constant actual arguments, and propagation can change local provenance. Neither inlining nor propagation is part of the verification contract. @@ -93,7 +93,7 @@ isolated directory. Run its `dotnet` executable, rather than a system installati /path/to/isolated-runtime/dotnet Tests/NativeProbe/bin/NativeProbe.dll /path/to/results ``` -The project pins runtime 10.0.0 and disables roll-forward. To run on a second machine without +The project pins runtime 11.0.0 and disables roll-forward. To run on a second machine without installing an SDK, copy `NativeProbe.dll`, `NativeProbe.deps.json`, `NativeProbe.runtimeconfig.json`, and `Mono.Cecil.dll` from the build directory. The generated fixture uses a deterministic MVID and timestamp. An admitted mismatch returns exit code 1; diff --git a/docs/evidence/runtime-profiles/windows-x64-net10.json b/docs/evidence/runtime-profiles/windows-x64-net11.json similarity index 83% rename from docs/evidence/runtime-profiles/windows-x64-net10.json rename to docs/evidence/runtime-profiles/windows-x64-net11.json index 92f45c6e7..618d77e08 100644 --- a/docs/evidence/runtime-profiles/windows-x64-net10.json +++ b/docs/evidence/runtime-profiles/windows-x64-net11.json @@ -2,10 +2,10 @@ { "configuration": "default", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -35,10 +35,10 @@ { "configuration": "EnableHWIntrinsic=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -68,10 +68,10 @@ { "configuration": "EnableSSE2=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -101,10 +101,10 @@ { "configuration": "EnableSSSE3=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -134,10 +134,10 @@ { "configuration": "EnableSSE41=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -167,43 +167,43 @@ { "configuration": "EnableSSE42=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, "x86Base64": true, "sse2": true, - "ssse3": false, - "sse41": false, - "sse42": false, - "avx": false, - "avx2": false, - "bmi1": false, - "bmi2": false, - "avx512F": false, - "avx512FVL": false, - "avx512DQ": false, - "avx512DQVL": false, - "avx512Vbmi": false, - "avx512VbmiVL": false, + "ssse3": true, + "sse41": true, + "sse42": true, + "avx": true, + "avx2": true, + "bmi1": true, + "bmi2": true, + "avx512F": true, + "avx512FVL": true, + "avx512DQ": true, + "avx512DQVL": true, + "avx512Vbmi": true, + "avx512VbmiVL": true, "aes": true, "aesX64": true, "armAes": false, "armAes64": false, "vector128Accelerated": true, - "vector256Accelerated": false + "vector256Accelerated": true } }, { "configuration": "EnableAVX=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -233,10 +233,10 @@ { "configuration": "EnableAVX2=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -266,10 +266,10 @@ { "configuration": "EnableBMI1=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -299,10 +299,10 @@ { "configuration": "EnableBMI2=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -332,10 +332,10 @@ { "configuration": "EnableAVX512=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -365,10 +365,10 @@ { "configuration": "EnableAVX512DQ=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -398,10 +398,10 @@ { "configuration": "EnableAVX512v2=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -431,10 +431,10 @@ { "configuration": "EnableAVX512F_VL=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -464,10 +464,10 @@ { "configuration": "EnableAES=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -497,10 +497,10 @@ { "configuration": "EnableArm64AdvSimd=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -530,10 +530,10 @@ { "configuration": "EnableArm64Aes=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -563,10 +563,10 @@ { "configuration": "EnableArm64=0", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -596,10 +596,10 @@ { "configuration": "PreferredVectorBitWidth=128", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, @@ -629,10 +629,10 @@ { "configuration": "PreferredVectorBitWidth=64", "observed": { - "runtime": ".NET 10.0.14", - "runtimeVersion": "10.0.14", + "runtime": ".NET 11.0.0", + "runtimeVersion": "11.0.0", "architecture": "X64", - "os": "Microsoft Windows 10.0.26220", + "os": "Microsoft Windows 10.0.26320", "advSimd": false, "advSimd64": false, "armBase64": false, diff --git a/docs/performance/InstructionLookups.md b/docs/performance/InstructionLookups.md index a575f545e..608c9eded 100644 --- a/docs/performance/InstructionLookups.md +++ b/docs/performance/InstructionLookups.md @@ -33,6 +33,14 @@ instruction of the longest method when that is more, because Lean compiles and c list one nested cell per instruction. At a fixed 4096, a method of 2,002 instructions fails to compile. +The kernel decides a method's instruction checks once per site. A body of 4,006 arithmetic +instructions spends 60 heartbeats per instruction in the unit of `maxHeartbeats`, and a body of +802 instructions with a linked call at every second one spends 168, of which the model agreement +check takes 104, so a call site adds 216. `Extracted.lean` therefore states +`set_option maxHeartbeats` on those checks when 120 per instruction plus 432 per call, newobj, +callvirt or calli exceeds Lean's default of 200,000; any other method keeps the default. At the +default, the checks of the 4,006-instruction body run out. + ## The `lookups` suite ```sh @@ -42,18 +50,341 @@ dotnet run --file scripts/Test.cs -- --suite lookups The suite (`Tests/LookupScaling.cs`) extracts one method, `word = word * y + x;` repeated, at about 500 and about 2,000 instructions, and compiles three files at each length: the extraction, its code as a body with a parameter under `cil_instruction_equations`, and its -generated template. Heartbeats are read by `IO.getNumHeartbeats` at the end of each file, less -the heartbeats of a file that holds the imports alone. Each file prints one line: +generated template. The suite reads `IO.getNumHeartbeats` after the final lookup assertions +and subtracts the heartbeats of the imports alone. Each row prints one line: ```text PASS compiling an extracted method costs heartbeats per instruction at 502 instructions and at 2002, times as many ``` -The suite fails when a ratio exceeds 1.25. Linear equations measure 0.99 for the extraction and -1.01 for the body and the template; equations proved against the whole list measure 3.09 for the -extraction and about 3.7 for the body. A ratio that rises toward 1.25 means some proof or -elaboration step walks the code list again. The suite also fails when a `cil_lookup` at a -computed pc costs more than 1.25 times as much per case at 16 repeated segments as at 4. +The suite fails when a ratio exceeds 1.15. The ratios are 0.98 for the extraction, 1.02 for the +hand-written body and 0.95 for the decoded template. At 502 and 2,002 instructions the template +costs 52,427 and 49,854 heartbeats per instruction. Proving each equation against the whole list +costs 3.09 times as much per instruction for the extraction and about 3.7 for the hand-written +body at the longer length. The suite also fails when a `cil_lookup` at a +computed pc costs more than 1.15 times as much per case at 16 repeated segments as at 4. + +The lookup fixture checks the hand-written multiply-add code against the entire decoded template +after reading its heartbeat count. A second fixture keeps eight branch sites and varies either +the number of input profiles or the number of distinct feature getters over 2, 4 and 8, holding +the other dimension at 4. It measures the whole shared module after imports and the public +selection, leaf proofs and instruction equations after the closed decoder inputs. Each doubling +may cost at most 1.15 times as much per instruction per input or per distinct flag. +The input row adds a distinct observed flag assignment for each input. The flag row uses the +same four profiles at every size, with two observed assignments. A template with no flags uses +its decoded input's code directly. With flags, each conditional instruction compiles separately +so the compiler can handle one pc without tracing the branches at later pcs. + +The same method at 125, 250 and 500 statements also reports, from its extraction, the heartbeats +of `Extracted.method0_calls_validated` and of `Extracted.method0_evidence_validated` alone. The +suite fails when the call check costs more than 1.15 times as much per statement as at the length +before, or when the evidence theorem costs more than 1.15 times as much in total. The evidence +theorem only assembles the conjuncts' theorems, so it costs about 260,000 heartbeats at every +length; a conjunct stated through another term than `CIL.Extraction.MethodEvidenceValid` states +makes the elaborator reduce the whole decoded body, about 390,000 heartbeats per instruction. + +The suite also measures the body obligation of one method that passes 125, 500 and 1,000 distinct +string literals to another method, less the same file with the proof left open, per instruction of +the method's 2n + 2. A body over 256 bytes is one `CIL.Extraction.fixedBytes` literal whose code +joins numerals of at most 256 bytes, and the check costs about 88,900, 92,700 and 99,800 +heartbeats per instruction. The suite fails when the cost per instruction is more than 1.1 times +the cost at the size before. The kernel's caches hash a numeral by its low 64 bits, so numerals +with equal last bytes collide there: the same bodies written as separate numerals joined by `++` +cost about 89,600, 136,300 and 201,500 heartbeats per instruction. + +The suite also measures `decide +kernel` on two `CIL.TypeIdentity` values whose names are 20 or 80 +characters long and differ only in their last character, less the same statement closed with +`sorry`. A name is one code, so the decision costs about 7,500 heartbeats for equal names and +8,300 for unequal ones at either length; one enclosing type adds about 2,500. The suite fails when the 80-character case costs more +than 1.05 times the 20-character case or when either costs more than 9,500 heartbeats. This +check takes about 5 seconds. + +Exception decisions compare `CIL.ExceptionType` constructors. The equality, inequality, +CLR mapping and optional outcome rows each allow 2,000 to 9,500 kernel heartbeats. Using +`applicationException` instead of `format` may increase the cost by at most 1.05 times. + +The suite also measures `decide +kernel` on one `add` of an int32 offset to a native method +table of 16, 64 and 256 entries, less the same statement closed with `sorry`. Each table and its +static storage are definitions outside the decision, so the proof of `MethodTable.counted` is +checked once there and not in the measured statement. The step reads `MethodTable.entryCount`, +not the entries, and costs about 24,700 heartbeats at each size; geometry checks that read +`targets.size` cost 29,500, 41,800 and 90,900. The suite fails when the cost at 64 or 256 entries +exceeds 1.15 times the cost at 16. + +The same files measure `loadNativeTable` of the table's first and of its last entry. The load +bounds the entry index by `MethodTable.entryCount`, so the first entry costs about 27,400 +heartbeats at each size; a bound read from `targets[index]?`, which walks the array to its size, +costs 30,100, 42,700 and 92,800. Reading entry i still walks i cells of the literal array, which +the kernel holds as a list, so the last entry costs 36,000, 63,700 and 174,300, about 576 +heartbeats per index at each size. The suite fails when the first entry's cost at 64 or 256 +entries exceeds 1.15 times the cost at 16, or when the last entry's cost beyond the first's, +divided by its index, does. Run these rows alone with +`dotnet run --file Tests/LookupScaling.cs -- --native-table-only`. + +The suite also measures `decide +kernel` on the name clause of the call and field operand checks, +`MetadataName.spells`, for names of 20, 80 and 320 bytes that share all but their last byte, less +the same statements closed with `sorry`. The clause reads the name's code back into bytes: about +41,300, 151,400 and 589,600 heartbeats, or 2,070, 1,890 and 1,840 per byte. The suite fails when +the cost per byte grows more than 1.15 times from one length to the next. Run this row alone with +`dotnet run --file Tests/LookupScaling.cs -- --metadata-name-only`. + +The suite also measures `decide +kernel` on runtime model lookups of the first, middle and last +entry of a table of 16 and of 256 entries, less the same statements closed with `sorry`. +`model_table%` writes the table as a literal balanced tree keyed by method name code, so a lookup +walks at most four more levels in the larger table and compares one method reference at the leaf: +66,364 and 68,298 heartbeats for the first entry, 64,693 and 64,700 for the middle entry (the +root), and 66,319 and 68,478 for the last. The suite fails when any position costs more than +1.05 times as much at 256 entries as at 16. + +The same file measures 8, 32 and 128 live sites, each calling a distinct modelled method. +Checking rejection at every site costs 169,266, 521,797 and 1,938,694 heartbeats. +The suite permits up to 1.15 times the previous rejection cost per site. +Checking the complete body with `ModelsAgree` costs 253,782, 840,794 and 3,195,521 heartbeats +when every site retains its call. The cost per retained call must stay flat or decrease. +It falls from 31,723 to 26,275 to 24,965 heartbeats. + +The compiled checks interleave sizes across nine trials. The table gives median times in +milliseconds. Each size doubles the instruction count or both the tuple field count and query +count. Tuple queries retain the same computed layout and change only the indexed raw row. + +| Production check | 512 | 1,024 | 2,048 | +| --- | ---: | ---: | ---: | +| `RuntimeModelsAgree`, live nops | 1.811 | 3.515 | 7.257 | +| `RuntimeModelsAgree`, dead nops with reverse-ordered links | 1.514 | 2.906 | 6.203 | +| `integerPairResult`, oversized DivRem tuple | 0.486 | 0.973 | 1.908 | + +Each doubling permits up to 1.15 times the preceding median cost per instruction, or 1.25 +times the preceding median cost per tuple query. Run these rows alone with +`dotnet run --file Tests/LookupScaling.cs -- --model-table-only`. + +The platform lookup benchmark measures the kernel cost of resolving a reference, admitting an +enum, checking a pair field, ending a class chain, checking the byte-order constant and +classifying a base. Each table contains the six definitions these checks use and 16, 128 or +1,024 filler definitions. Separate rows look up an absent type and a simple name that occurs +in several namespaces. A further row looks up identical name parts in distinct assembly scopes. +It checks two entries with different kinds and a third, absent scope. The three lookups together +cost about 82,000, 90,000 and 101,000 heartbeats at the three sizes. + +When names repeat across scopes, the literal index includes scope bytes in the key. The suite +compares each lookup at consecutive table sizes and allows up to 1.15 times the previous cost. +The same growth limit applies to elaboration heartbeats per definition. Run these rows with +`dotnet run --file Tests/LookupScaling.cs -- --platform-table-only`. + +The suite also measures `cil_kernel_rfl` on `CIL.Safety.indirectTarget`, the selection of a +`calli` body, for a pointer whose owner is 64, 256 or 1,024 bytes long and a catalog whose second +entry holds the same owner, less the same statement closed with `sorry`. The kernel compares +the owners byte by byte, at 8,300 to 8,900 heartbeats per byte: 572,000, 2,165,000 and 8,533,000 +heartbeats. The suite fails when the cost grows more than 1.1 times as fast as the length from one +size to the next. Run this row alone with +`dotnet run --file Tests/LookupScaling.cs -- --indirect-target-only`. + +The suite also checks the kernel heartbeats of `ScalarFlowValid` on generated bodies of 128, 256 +and 512 array element addresses, each followed by a branch, with and without a `readonly.` +prefix, less the heartbeats of stating the same certificate. Each instruction of these bodies, as +of the local bodies whose `ScalarFlowValid` is checked below, carries the byte offset extraction +writes for it. It fails when the cost per address +at 256 or 512 exceeds 1.15 times the cost at 128. The check reads the raw instructions next to a +site from a `FlowIndex` built once per body, only at the sites that need them, and measures +1.01 for plain and 1.02 for prefixed addresses. + +The prefix measurements use bodies of 500, 2,000 and 8,000 instructions. Each segment +contains `readonly.`, `ldelema`, `pop` and a branch to a segment's prefix. +The body evidence row measures `prefixSequenceValid`. Three raw construction rows measure +`rawPrefixesValid` with the same destinations in ascending, descending and shuffled order. +Each branch or switch destination requires one lookup of its preceding instruction. Compiled +code reads a single array for the method. Kernel reduction shares the suffix at each pc. +Each row permits at most 25% growth in heartbeats per instruction when the size quadruples. +Each reordered row must also stay within 2% of ascending at each size. Run the rows with +`dotnet run --file Tests/LookupScaling.cs -- --prefix-entry-only`. + +The method-decoding and decoded-definition rows each use 500, 2,000 and 8,000 instructions. +A fully live body with no method-token calls or possible fold heads uses the ordinary decoder, +which carries the preceding instruction for prefix handling. Other bodies consult the reachability +mask and fold roles at each pc. The kernel shares the preceding lookup's list suffix, and +compiled code reads the array directly. A body without prefixes needs no branch-target lookups +for prefix validation. Run these rows individually with `--method-decode-only` and +`--decoded-definition-only`. + +It also checks the kernel heartbeats per instruction of `ScalarFlowValid` on generated bodies of +128, 256 and 512 locals, each stored once and then loaded or addressed once, less the heartbeats +of stating the same certificate. Integer locals take a constant and are popped after a load or +after `ldloca`; native-int locals take a method +address and are consumed by `calli`, which makes each a pointer local. For the pointer locals it +also checks `closedArguments`. The suite fails when the cost per instruction at 256 or 512 locals +exceeds 1.25 times the cost at 128. The scalar check and fold discovery read local types from a +`FlowIndex` and pointer locals from a `Std.TreeSet`, built once per body, as does the closed +argument storage. They measure 1.01 for loaded, 1.01 for addressed and 1.07 for pointer locals, +and 1.17 for the closed argument storage, whose set lookups cost the logarithm of the locals. +Checks that walk the locals list or the pointer local list at each site measure 1.33, 1.54, 1.62 +and 2.33. + +The suite also checks the kernel heartbeats of `FeatureFlowValid` on generated bodies of 256, 1,024 +and 4,096 instructions that cycle `ldloc.s`, `stloc.s`, `ldloca.s` and `pop` over four `int32` +locals, less the heartbeats of stating the same certificate. It fails when the cost per instruction +at 1,024 or 4,096 exceeds 1.25 times the cost at 256. A local load, store or address is checked by +its opcode and index, and the cost per instruction measures 1.00 at 1,024 and 1.05 at 4,096. + +It checks `FeatureFlowValid` on bodies of 2,049 instructions over 16, 64 and 256 `int32` locals as +well. Each segment loads, stores and addresses a local, then addresses the argument and stores a +constant into it. A feature state lists only the locals that hold a known word, as (local index, +word) pairs in increasing index order, so none of these states holds a local fact. The suite fails +when the cost per instruction at 64 or 256 locals exceeds 1.25 times the cost at 16; it measures +1.01 at 64 and 1.02 at 256. + +A last body over 64 `int32` locals stores zero into the first 4, 16 or 64 of them. Then, in each +of 128 rounds, it stores a new word into one of those locals in turn and loads it back, so each of +the 512 loop states holds every stored fact. Its certificate is written out as literal states, as +the extractor writes them. The suite subtracts the cost of the stores alone, whose states together +hold about the square of the facts, and fails when the increment from 16 to 64 facts exceeds 4.5 +times the increment from 4 to 16. Linear growth in the facts gives four; at the measured cost per +fact, a quadratic term of one heartbeat per pair of facts in each state exceeds 4.5, and a check +that also compares every pair of facts in each state measures 10.6. Reads, stores and weakening +walk the known facts, and each fact's eligibility is a lookup in a balanced index built once per +body. The ratio measures 4.05, about 400 kernel heartbeats per fact per state, or about 13 million +for 64 facts over 512 states. + +The suite embeds a hand-written body built with `Method.withCode` and its own link table, one call per segment, into an +extraction with a dead block after each return, at 512 and 2,048 calls, and reads the kernel +heartbeats to recheck the lemma `cil_embed` states. It fails when the heartbeats per call grow +more than 1.15 times. The recheck costs about 56,800 heartbeats per call at both sizes, 1.00 +times; link reads whose cost grows with the square of the calls measure 1.29. The suite also +embeds a body whose head branches over its first block to its second into an extraction that +places the second block first, at 256 and 1,024 calls, under the same limit. Its pc map descends +once, and the link reads and code lookups start again from the head of the table and the code +there; it costs about 53,800 heartbeats per call at both sizes. + +The suite declares `cil_resolution_facts` for methods of 8, 32 and 128 `int32` parameters, once +with no parameter homes and once with every other argument homed, and reads every argument's +`Method.argumentAccess` through the facts with `simp only [cil_code]`. The facts walk the parameter +types and homes once, from named 16-entry suffixes built as the code suffixes are. The suite fails +when the heartbeats per argument of the facts, or of the reads, grow more than 1.35 times from +one size to the next. The facts cost about 7,400, 8,400 and 8,400 heartbeats per argument without +homes and 8,300, 10,500 and 10,800 with homes; a size below one block walks fewer entries, which +accounts for the lower cost at 8. The reads cost 11,200 to 12,500 heartbeats per argument at every +size. + +The suite also declares `cil_resolution_facts` for methods of 8, 32 and 128 static sites at even +pcs and reads every site's `Method.staticAt` through the facts, under the same 1.35 limit per +site. The facts cost about 5,500, 7,000 and 7,000 heartbeats per site, lower at 8 for the same +reason, and the reads 22,400 to 23,800 heartbeats per site at every size. + +The suite also decides `StaticIdentitiesValid` for one method of 8, 32 and 128 static sites, each +of its own field and identity, under the same 1.35 limit per site. The check walks the recorded +accesses beside the sites once and sorts the pairs of identity and field both ways, and costs +about 22,900, 21,000 and 22,700 heartbeats per site. + +The suite also declares `cil_resolution_facts` for a method whose raw method names one struct +element type per layout, at 8, 32 and 128 layouts, and reads every element's +`Method.elementShape` through the facts with `simp only [cil_code]`. Each struct fact reads its +raw and computed layout entries from named 16-entry suffixes of the layout lists, declared once +for the environment constant. The suite fails when the heartbeats per element type of the facts, +or of the reads, grow more than 1.35 times from one size to the next. The facts cost about 8,700, +11,300 and 11,400 heartbeats per element type, and the reads 14,200, 13,100 and 14,000; a size +below one block walks fewer entries, which accounts for the lower cost of the facts at 8. +The suite also names 8 element types from 8, 32 and 128 sites, so that most sites repeat a type, +and fails when the total heartbeats of the facts, or of the reads, grow more than 1.35 times from +one site count to the next. The facts cost about 69,700, 71,200 and 77,200 heartbeats and the +reads 113,700 at every site count, since the facts state one shape per element type. +Run this row alone with `dotnet run --file Tests/LookupScaling.cs -- --element-shape-only`. + +The suite declares `cil_field_shape` for 8, 32 and 128 `int32` fields of one struct and for 8, +32, 128 and 512 fields each in a struct of its own, and `cil_value_shape` for 8, 32, 128 and 512 +struct types, and reads every operand's `Method.fieldShape` or `Method.valueShape` through the +facts with `simp only [cil_code]` from a 512-instruction body. It fails when the heartbeats per +operand of the facts, or of the reads, grow more than 1.35 times from one size to the next. The +first fact of an environment names 16-entry suffixes of its layout lists, about 517,000 +heartbeats at 512 layouts, and each fact reads its struct's entries from the suffix that starts +the entry's block, so an entry further into its block costs about 750 heartbeats more. The +compiled resolver that proposes each shape evaluates the environment once. The field facts cost +about 269,000, 280,000 and 326,000 heartbeats per field in one struct, where each fact walks the +struct's field list to its index, and 269,000, 271,000, 272,000 and 272,000 with a struct per +field; most of a field fact is the kernel deciding the storage of the field's `int32` type. The +value facts cost about 13,300, 15,900, 16,000 and 16,100 heartbeats per type. Facts that walk the +layout lists to the struct instead cost 293,000, 313,000, 386,000 and 672,000 heartbeats per +field and 38,000, 58,000, 131,000 and 417,000 per type. The reads cost 11,100 to 14,700 +heartbeats per operand at every size. + +The suite locates a field of the furthest base on a fresh instance whose class has 2, 8 and 32 +bases, and rejects a field whose owner is outside that lineage, deciding membership in the +lineage with `decide`. It fails when the heartbeats per base of either grow more than 1.35 times +from one size to the next. Rejecting costs about 146,400, 65,900 and 45,000 heartbeats per base +and locating 135,800, 69,300 and 51,100, so the total grows linearly with the bases. Run this row alone with +`dotnet run --file Tests/LookupScaling.cs -- --lineage-only`. + +The suite decides `ManagedCallsValid` on bodies of 64, 256 and 1,024 call sites, each followed by a +`nop`, with the literal link table the extractor writes, and reads the heartbeats of that one +command. It fails when the heartbeats per call site grow more than 1.35 times from one size to the +next. The check reads each pc's link by one walk beside the code and costs about 10,900, 9,500 and +9,100 heartbeats per call site; a scan of the table at each pc costs 41,500 and 133,900 at 64 and +256 sites and exhausts a 16 GB memory limit at 1,024. Run this row alone with +`dotnet run --file Tests/LookupScaling.cs -- --call-check-only`. + +The suite states and proves with `decodedProgram?_eq_some` that 8, 32 and 128 evidence records +decode to their methods, given a proof for the program's masks and one decoding fact per record. +It reads the heartbeats for that command and fails when the cost per record grows more than 1.25 +times from one size to the next. With body facts alone, the measured costs were about 14,300, +13,000 and 14,200 heartbeats per record. Run this row alone with +`dotnet run --file Tests/LookupScaling.cs -- --decoded-program-only`. + +The suite decides with `decide +kernel` the closure of method 0 in four shapes of program: a chain +of 8, 32 and 128 methods each calling the next, whose closure is the whole program; the same chain +at 128, 256 and 512 methods, the scale of the largest closures of the corpus; one method whose +link table enters seven others 32, 128 and 512 times; and a closure of four methods at the start +of a program of 128, 512 and 2,048 methods. Each program states its index with `program_index%`, +as an extraction does, and the closure is decided as `closureWith` over that index. A search over +n methods with e link callees in a program of P methods reads each method from the index and +gives each callee one packed table entry, so it costs O(n * log P + e). The suite fails when, in +any shape, the heartbeats each added unit of n * log P + e costs grow more than 1.15 times from +the first two sizes to the last two, and checks that the bound refuses the n * P of list reads +in a growing whole-program closure. For the three shapes measured through the list, the same +1.15 limit applies to the cost per added unit of n * P + e. + +| Shape | Sizes | Through the index | Through the list | +| --- | --- | --- | --- | +| chain | 8 / 32 / 128 | 87,300 / 335,424 / 1,423,146 | 90,411 / 437,548 / 3,533,801 | +| long chain | 128 / 256 / 512 | 1,422,768 / 3,013,631 / 6,569,794 | not measured | +| fan | 32 / 128 / 512 | 140,789 / 367,239 / 1,272,991 | 144,007 / 370,505 / 1,276,294 | +| tail | 128 / 512 / 2,048 | 55,798 / 58,520 / 61,171 | 81,738 / 179,808 / 571,770 | + +The chain through the list grows with the square of its methods, since its closure is the whole +program; through the index each added unit costs 0.84 times as much at the larger sizes, and the +long chain 1.02 times. The suite fails unless the tail, a closure of four methods, costs less +through the index than through the list at every size. Building each tail program's literal index +and proving its length and lookup equations costs 323,143, 934,058 and 3,383,553 heartbeats. +Each additional method costs about 1,591 heartbeats from 128 to 512 and 1,595 from 512 to 2,048. +The suite limits the ratio to 1.15 and audits all three certificate theorems after timing them. + +The native elaboration check times the same literal index at 2,048 and 32,768 cells. It takes the +fastest of three runs at each size and allows up to 64 times the smaller time. This includes list +traversal that allocates no memory and therefore consumes no heartbeats. Run the closure checks with +`dotnet run --file Tests/LookupScaling.cs -- --closure-only`. + +Three callers each execute a direct call and return. Their callees occupy positions 1, n/2 and n +in programs of 16, 64 and 256 methods. Each callee pushes its own index and returns. Each proof +covers four CIL instructions. The closed proofs evaluate the list interpreter or rewrite the +invocation with `invoke_eq_invokeWith` and evaluate the index. The symbolic proofs use +`cil_checked_steps` with an arbitrary initial memory and prove that the call preserves it. + +| Proof route | 16 methods | 64 methods | 256 methods | +| --- | --- | --- | --- | +| List, `rfl` | 269,536 / 277,919 / 287,544 | 269,390 / 301,940 / 335,187 | 270,547 / 398,335 / 526,622 | +| Indexed, `rfl` | 297,495 / 298,328 / 298,798 | 304,040 / 305,652 / 306,059 | 310,995 / 312,991 / 313,378 | +| List, `cil_kernel_rfl` | 87,144 / 89,896 / 93,132 | 87,416 / 98,959 / 110,968 | 88,286 / 134,968 / 182,133 | +| Indexed, `cil_kernel_rfl` | 106,285 / 106,520 / 106,657 | 107,905 / 108,369 / 108,531 | 110,145 / 110,840 / 110,978 | +| Symbolic, `cil_checked_steps` | 1,452,716 / 1,406,526 / 1,406,648 | 1,404,382 / 1,406,963 / 1,406,683 | 1,404,730 / 1,407,064 / 1,406,423 | + +These are raw heartbeats for the statement and proof. Divide by four for the cost per executed +instruction. For each indexed proof route and for symbolic stepping, the most expensive of +the nine calls must cost at most 1.15 times the cheapest. The list measurements include the +walk to the callee on each fetch. Constructing all method equations costs 648,941, 2,649,459 and +10,822,764 heartbeats at the three sizes. The suite bounds growth per added unit of P * log2(P) +at 1.15 times. Printing their shared axiom report costs 29,166, 99,872 and 383,823 heartbeats, +with the same growth limit per additional method. The suite checks that the report names each +method equation and all three index certificates exactly once. A separate interval measures one +`cil_lookup [program]` proof at every method position and bounds marginal cost per added method +at 1.15 times its value in the smaller interval. The proofs cost 561,305, 2,249,666 and +9,137,207 heartbeats, with a marginal cost ratio of 1.020. Run the comparison with +`dotnet run --file Tests/LookupScaling.cs -- --run-only`. ## Reference figures diff --git a/docs/performance/LibraryDistribution.md b/docs/performance/LibraryDistribution.md index bc8d5b322..9932ecd68 100644 --- a/docs/performance/LibraryDistribution.md +++ b/docs/performance/LibraryDistribution.md @@ -92,7 +92,8 @@ dotnet run --file scripts/TestLibraryArchive.cs -c Release ``` The tests cover complete payloads, hidden files, executable permissions, hard-link contents, -source symlink rejection, missing and wrong digests, truncated compressed streams, raw-directory -artifacts, traversal, tar links and devices, and duplicate entries. Every unsafe tar has a valid -manifest and must fail for its specific unsafe entry, not for a missing manifest. The hard-link -and source symlink cases run on Linux only. +source symlink rejection, an archive inside the library or reached through a link into it, +missing and wrong digests, truncated compressed streams, raw-directory artifacts, traversal, tar +links and devices, and duplicate entries. Every unsafe tar has a valid manifest and must fail for +its specific unsafe entry, not for a missing manifest. The hard-link and source symlink cases run +on Linux only; the archive through a link prints SKIP where no directory link can be made. diff --git a/docs/performance/ProofCost.md b/docs/performance/ProofCost.md index 52be99925..1092eefee 100644 --- a/docs/performance/ProofCost.md +++ b/docs/performance/ProofCost.md @@ -64,6 +64,157 @@ heartbeats, proof objects and olean bytes, so a base checkout in another directo on no other row; the other 29 rows of the module move by at most 0.2%. - Elapsed times depend on load and never decide a comparison. +## Reusing a wide vector equality contract + +On Lean 4.35.0 the scalar-profile extractions of `EqualBytes256` and `EqualSigned512` each +load two arguments, call vector equality and return a Boolean. For each method the consumer +proves that zero compared with a vector containing only its highest bit returns false and +preserves arbitrary caller memory. Both proofs allow any sufficient fuel. The direct proof +supplies 16 and reduces the invocation. The other proof obtains the fuel, return value and +memory equality from the universal contract. + +| Vector width | Proof | Frontend heartbeats | Kernel heartbeats | Proof objects | Elapsed ms | +| ---: | --- | ---: | ---: | ---: | ---: | +| 256 | Universal contract | 4,611,894 | 768,547 | 312 | 441 | +| 256 | Direct consumer | 2,744,041 | 767,330 | 129 | 276 | +| 256 | Consumer using contract | 73,207 | 6,015 | 535 | 11 | +| 512 | Universal contract | 5,608,075 | 978,331 | 312 | 536 | +| 512 | Direct consumer | 3,346,702 | 977,114 | 129 | 389 | +| 512 | Consumer using contract | 73,249 | 6,015 | 535 | 10 | + +The universal contract incurs its kernel cost once per extracted method. At either width, +checking it once and using it twice costs fewer kernel heartbeats than reducing two such +invocations. The proof that uses the contract has more proof objects. The command-level +measurements use the proof-cost frontend described below. Elapsed times are from one run. + +## Reusing an in-place vector swap contract + +On Lean 4.35.0, the `arm64-advsimd` extraction of `DivisionIntrinsics.SwapHalves` exchanges +two 16-byte halves of one writable buffer. The contract covers every initial byte pattern. +The following runs use the base at +[27953edb](https://github.com/NethermindEth/LeanCIL/commit/27953edb) and the canonical interpreter. +The two Release assemblies target .NET 10 and .NET 11 respectively. All 19 instruction offsets, +opcodes, resolved operands and local signature bytes agree. The six generic calls have different +MethodSpec row numbers in the two assemblies. + +Only input byte 16 is set for the consumer proofs. Both require output byte zero to equal one +for some sufficient fuel. The direct proof reduces the invocation. The other applies the +universal contract. Append the [consumer proofs](../../Tests/HalfSwapCost.lean.in) to the original +half-swap proof from [the intrinsics fixture](../../Tests/ExactIntrinsics.cs) and measure that +module with the proof-cost driver. Both modules passed standalone kernel replay. The legacy +frame includes an empty argument-home list in the intermediate state. + +| Interpreter | Proof | Frontend heartbeats | Kernel heartbeats | Proof objects | Elapsed ms | +| --- | --- | ---: | ---: | ---: | ---: | +| Base | Direct consumer | 1,982,309 | 550,666 | 98 | 143 | +| Canonical | Direct consumer | 5,621,328 | 1,684,575 | 98 | 432 | +| Base | Consumer using contract | 87,678 | 4,929 | 387 | 7 | +| Canonical | Consumer using contract | 87,670 | 4,929 | 387 | 10 | + +Proving the four execution regions once costs 761,169 kernel heartbeats on the base and +3,545,916 on canonical. The same composition with the footprint and byte-order proofs costs +4,614 on each interpreter. The direct consumer costs 3.06 times as much on canonical. +Applying the universal contract costs the same on both. + +## Reusing a compiled field-read contract + +The scalar-profile methods `Gates.Reader.Read` and `ReadPacked` read four bytes by reference. +The first reads an explicit-layout field at offset 8. The second reads a sequential field at +offset 2 with one-byte packing. Each contract accepts arbitrary initialized contents of a +24-byte caller allocation, grants read access only and requires the entire memory to remain +equal after the call. Deep verification checks both layouts against the pinned assembly. + +The consumer sets the field's most significant byte to 128 and all other bytes to zero. It +proves that some fuel returns `0x80000000` with identical caller memory. The direct proof +supplies eight fuel and reduces the invocation. The proof using the contract obtains the fuel, +return and memory equality from the universal theorem. All measurements use Lean 4.35.0. + +| Layout | Proof | Frontend heartbeats | Kernel heartbeats | Proof objects | Elapsed ms | +| --- | --- | ---: | ---: | ---: | ---: | +| Explicit | Universal contract | 3,737,167 | 1,246,785 | 49 | 337 | +| Explicit | Direct consumer | 3,501,647 | 1,249,312 | 91 | 293 | +| Explicit | Consumer using contract | 56,417 | 7,700 | 371 | 6 | +| Sequential | Universal contract | 3,700,831 | 1,232,107 | 49 | 318 | +| Sequential | Direct consumer | 3,465,669 | 1,234,634 | 91 | 297 | +| Sequential | Consumer using contract | 56,401 | 7,695 | 371 | 7 | + +The universal theorem uses a shared contract-introduction lemma costing another 2,893 kernel +heartbeats for each layout. With that cost included, checking the contract once and applying +it twice uses fewer kernel heartbeats than reducing two reads for either layout. The table +gives command-level measurements after extraction. Image hashing and layout replay run separately. + +A second fixture, `LayoutProbe.Reader`, compares the interpreter at +[27953edb](https://github.com/NethermindEth/LeanCIL/commit/27953edb) +with canonical execution at `4d72d8c3`. The .NET 10 and .NET 11 builds produce +identical eight-byte method bodies for each reader, including the field token and tiny header. +Both interpreters use the same positive contract proof and consumer sources. + +| Layout | Proof | CIL.Op kernel heartbeats | Canonical kernel heartbeats | Proof objects in both | +| --- | --- | ---: | ---: | ---: | +| Explicit | Universal contract | 62,159 | 1,276,911 | 49 | +| Explicit | Direct consumer | 64,722 | 1,279,438 | 91 | +| Explicit | Consumer using contract | 7,700 | 7,700 | 371 | +| Sequential | Universal contract | 62,159 | 1,261,729 | 49 | +| Sequential | Direct consumer | 64,722 | 1,264,256 | 91 | +| Sequential | Consumer using contract | 7,695 | 7,695 | 371 | + +Direct reduction costs 19.8 times as many kernel heartbeats for the explicit field and 19.5 +times as many for the sequential field. The universal proof incurs a comparable increase once +per layout. Applying it to the same caller memory costs exactly the same in both models. +The direct consumers took 24 and 25 ms to elaborate on the historical revision and 291 and 279 ms with +canonical execution in single runs. The explicit contract application took 6 ms with each. + +The explicit `LayoutProbe.Reader` resolves its field to offset 8 and width 4 in 1,218,000 +kernel heartbeats. Checking the owner against the indexed raw layout accounts for 1,106,363 +of those heartbeats when measured separately. Extraction already proves the field shape once. + +The `Gates.Reader` proofs can apply that shape theorem at the field load and compose the +argument load, field load and return. The following costs exclude the extraction and measure +the reusable execution lemma plus the final contract statement. Both negative cases use the +same contract as their original and return zero on the byte pattern that requires 128. + +| Layout and outcome | Execution lemma, kernel heartbeats | Final theorem, kernel heartbeats | Combined elaboration, ms | +| --- | ---: | ---: | ---: | +| Explicit, verified contract | 168,181 | 297 | 72 | +| Sequential, verified contract | 177,298 | 299 | 82 | +| Explicit, refuted contract | 168,177 | 3,670 | 77 | +| Sequential, refuted contract | 177,262 | 3,675 | 76 | + +## Reusing the compiled Countdown loop contract + +`Examples.Loops.Countdown(uint)` returns zero for every UInt32 input from empty entry memory. +Its proof uses the remaining unsigned count as a strictly decreasing measure. The consumer +proves normal return for inputs 4 and 16, either by reducing the compiled method or by +instantiating that universal theorem. The same consumer source runs against both models. +[Loop proof](../../Examples/Proofs/Countdown.lean) · [Consumer](../../Tests/LoopVariants/CountdownReuse.lean.in) + +The baseline uses the `Op` interpreter from commit `27953edb` with its .NET 10 Release assembly. The +canonical extractor uses .NET 11. The two DLLs have identical 25-byte Countdown method bodies, +including all 13 instruction bytes, and identical local signature blobs. Each extractor selected +its own DLL. Lean 4.35.0 and the same proof-cost frontend measured both runs. Times below are +single-run elaboration times in milliseconds. + +| Proof | Baseline kernel heartbeats | Canonical kernel heartbeats | Baseline proof objects | Canonical proof objects | Baseline ms | Canonical ms | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | +| Universal loop theorem | 265,854 | 206,646 | 8,066 | 8,693 | 773 | 900 | +| Shared consumer lemma | 3,773 | 3,773 | 352 | 352 | 7 | 6 | +| Direct reduction, input 4 | 485,216 | 643,032 | 94 | 94 | 154 | 200 | +| Contract application, input 4 | 145 | 145 | 18 | 18 | 1 | 2 | +| Direct reduction, input 16 | 1,530,394 | 1,783,100 | 94 | 94 | 467 | 519 | +| Contract application, input 16 | 145 | 145 | 18 | 18 | 1 | 1 | + +Direct canonical reduction costs 33% more kernel heartbeats at input 4 and 17% more at input +16. Each contract application costs 145 in either model. Including the entry and loop-header +facts, the universal proof module takes 344,696 kernel heartbeats on the baseline and 270,141 +with canonical CIL. Its compiled size is 408,960 and 433,776 bytes respectively. The consumer +module is 25,808 bytes in both. Standalone kernel replay checked each extraction, proof and +consumer together. + +To reproduce the comparison, extract `Examples.Loops::Countdown(uint)` under the scalar +profile in each checkout, use its loop proof as `Proof.lean` and the consumer above as +`Consumer.lean`. Use the frontend below to measure `Proof` and `Consumer` with that checkout's +compiled library and extraction on the Lean search path. + ## Measuring one module of another package The frontend runs without the C# driver, from any Lake package that depends on LeanCIL. From the diff --git a/docs/performance/ProofWork.md b/docs/performance/ProofWork.md index f15994633..f8d5db49d 100644 --- a/docs/performance/ProofWork.md +++ b/docs/performance/ProofWork.md @@ -63,5 +63,5 @@ same module. | Extract the successful result of a bind | Expanding the bind's impossible error branch | included above | | `dsimp` to unfold `observerCount` in `step_observers`, where definitional equality suffices | `simp`, which builds congruence proofs over the dispatcher | `Step.lean` 26% to 29% slower | | Simplify the observer guard in the goal only; the hypothesis from `observer_ok` or `observer_thrown` does not contain it | Simplifying the normalized hypothesis as well | `Step.lean` 6% to 10% slower | -| Rewrite with `step_memory_eq` in `stepMemory` | Unfolding the instruction dispatcher | 5,451 heartbeats against 602 | +| Rewrite a memory step with its dispatch equation | Unfolding the instruction dispatcher | 5,451 heartbeats against 602 | | Rewrite the outer guard directly and try the write correspondence before the read correspondence in `instructionResult` | The reverse order, which reduces a write while matching a read lemma that cannot apply | 264,632 heartbeats against 227,277 | diff --git a/docs/performance/README.md b/docs/performance/README.md index 59a048063..e5d0e277c 100644 --- a/docs/performance/README.md +++ b/docs/performance/README.md @@ -33,9 +33,9 @@ Four suites fail when cost grows faster than the size of the input. Run one with | Suite | Fails when | | --- | --- | -| `lookups` | Heartbeats per instruction grow more than 1.25 times from about 500 to about 2,000 instructions; see [instruction lookups](InstructionLookups.md) | +| `lookups` | Heartbeats per instruction grow more than 1.15 times from about 500 to about 2,000 instructions, kernel heartbeats per array element address grow more than 1.15 times from 128 to 512 addresses, kernel heartbeats per instruction of the scalar certificate or closed argument storage of bodies of 128 to 512 loaded, addressed or `calli` pointer locals grow more than 1.25 times, kernel heartbeats per instruction of the feature certificate of local loads, stores and addresses grow more than 1.25 times from 256 to 4,096 instructions or from 16 to 256 locals at a fixed length, the feature certificate's cost grows more than 4.5 times as much from 16 to 64 known local facts as from 4 to 16, the kernel heartbeats of one native method table step or one load of its first entry at 64 or 256 entries exceed 1.15 times those at 16 entries, the kernel heartbeats per index of a load of its last entry grow more than 1.15 times from 16 to 256 entries, the kernel heartbeats per byte of the call check's method name clause or of a metadata name's encoding grow more than 1.15 times from 20 to 80 or from 80 to 320 bytes, the kernel heartbeats per call of an embedding grow more than 1.15 times at four times the calls, or the heartbeats per call site of the managed-call check grow more than 1.35 times at four times the call sites; see [instruction lookups](InstructionLookups.md) | | `stepping` | Checked stepping exceeds its per-instruction growth budgets; see [validation](../VALIDATION.md#proof-cost) | -| `scaling` | The certificate of a six-method call graph exceeds the default heartbeat limit | +| `scaling` | The certificate of a six-method call graph exceeds the default heartbeat limit, kernel heartbeats per layout of the header struct layout check over nested or over generic structs, or of a method's layout check over nested structs, grow more than 1.5 times from 64 to 512 structs, or 512 structs of either kind do not check within 8 GiB | | `excluded` | Certificates with hundreds of excluded and live sites exceed the default heartbeat limit | ## Suite times @@ -53,25 +53,18 @@ A job's summary and each suite's transcript report the suite's verification time ## Fold discovery -Two files measure the kernel evaluating `foldRoles`, which -[fold discovery](../VALIDATION-EXPORTS.md#fold-discovery) keeps from scanning the whole body once -per local or per message load. Each proves its statement by `decide +kernel`: +`docs/performance/fold-roles/ThrowScaling.lean` measures the kernel evaluating `foldRoles` on +4,096 message loads with no admitted throw fold. The statement uses `decide +kernel`; +[fold discovery](../VALIDATION-EXPORTS.md#fold-discovery) bounds the scan at each message load. -| File | Workload | -| --- | --- | -| `docs/performance/fold-roles/CountScaling.lean` | 256 span locals, each stored once and loaded by address once before a receiver call; includes building the local-use index | -| `docs/performance/fold-roles/ThrowScaling.lean` | 4,096 message loads with no admitted throw fold | - -Build the module they import, then time each file: +Build the imported module, then time the file: ```sh lake build CIL.Extraction.Folds -lake env lean -Dprofiler=true docs/performance/fold-roles/CountScaling.lean lake env lean -Dprofiler=true docs/performance/fold-roles/ThrowScaling.lean ``` -To check scaling, double the size (`256` or `4096`, at each occurrence in the file) in a scratch -copy and compare. The time of `ThrowScaling.lean` should about double; a fourfold increase means -a scan per load has returned. The time of `CountScaling.lean` grows faster than double, because -each local's type is looked up by position in the list of locals. Compare it with the same -doubling on the base commit. There are no reference figures for these files. +To check scaling, double the size (`4096`, at each occurrence in the file) in a scratch copy +and compare. The time should about double; a fourfold increase indicates an unbounded scan per +load. There are no reference figures for this file. The `lookups` suite checks addressed locals +with a failing limit. diff --git a/docs/performance/fold-roles/CountScaling.lean b/docs/performance/fold-roles/CountScaling.lean deleted file mode 100644 index 784d9782e..000000000 --- a/docs/performance/fold-roles/CountScaling.lean +++ /dev/null @@ -1,23 +0,0 @@ -import CIL.Extraction.Folds -/-! Kernel benchmark for complete span-receiver role discovery, including count construction. -/ -set_option maxRecDepth 100000 -set_option maxHeartbeats 0 -open CIL CIL.Extraction -/-- A supported byte span. -/ -def span : TypeExpr := .apply (.constructor runtimeScope "System.Span`1") - (.atom runtimeScope "System.Byte") -/-- A receiver call that completes a span fold. -/ -def spanLength : RawInstruction := ⟨0, 40, .call { - declaringType := span, - name := "get_Length", target := .external "", typeArguments := [], parameterCount := 0, - hasThis := true, returnsValue := true}⟩ -/-- Each local has exactly one store and one address load. -/ -def code (size : Nat) : List RawInstruction := (List.range size).flatMap fun index => - [⟨0, 19, .local index⟩, ⟨0, 18, .local index⟩, spanLength] -/-- Every receiver pair folds and each following call retains its ordinary role. -/ -def expected (size : Nat) : List (Option FoldRole) := - (List.replicate size [some .head, some .interior, none]).flatten -/-- Full role discovery includes constructing and querying the local-use index. -/ -theorem countedReceivers : - foldRoles (List.replicate 256 span) (code 256) = expected 256 := by decide +kernel -#print axioms countedReceivers diff --git a/docs/performance/fold-roles/ThrowScaling.lean b/docs/performance/fold-roles/ThrowScaling.lean index 2643387a2..8e8da231a 100644 --- a/docs/performance/fold-roles/ThrowScaling.lean +++ b/docs/performance/fold-roles/ThrowScaling.lean @@ -4,6 +4,6 @@ set_option maxRecDepth 100000 set_option maxHeartbeats 0 open CIL CIL.Extraction /-- Message loads alone never establish an exception fold. -/ -theorem messagePrefix : foldRoles [] (List.replicate 4096 (⟨0, 20, .none⟩ : RawInstruction)) = +theorem messagePrefix : foldRoles (List.replicate 4096 (⟨0, 20, .none⟩ : RawInstruction)) = List.replicate 4096 none := by decide +kernel #print axioms messagePrefix diff --git a/global.json b/global.json index c8cb1a012..a4bb4cf23 100644 --- a/global.json +++ b/global.json @@ -1 +1 @@ -{"sdk":{"version":"10.0.403","rollForward":"patch"}} +{"sdk":{"version":"11.0.100","rollForward":"patch"}} diff --git a/lakefile.toml b/lakefile.toml index 36523bc91..dbdfa49cf 100644 --- a/lakefile.toml +++ b/lakefile.toml @@ -17,15 +17,121 @@ srcDir = ".lake/leancil" roots = ["Extracted", "ExtractedSymbols"] defaultFacets = [] +# Opt-in binding audit; no CIL module imports it. +[[lean_lib]] +name = "StrictBinding" + [[lean_lib]] name = "Tests.Support" +[[lean_lib]] +name = "Tests.ReplayDiagnostics" + [[lean_lib]] name = "Tests.Semantics" +[[lean_lib]] +name = "Tests.MixedNativeOpcodes" + +[[lean_lib]] +name = "Tests.BodyEncoding" + +[[lean_lib]] +name = "Tests.BodyDecoding" + +[[lean_lib]] +name = "Tests.BodyLiteral" + +[[lean_lib]] +name = "Tests.KernelEquation" + +[[lean_lib]] +name = "Tests.MetadataBuilder" + +[[lean_lib]] +name = "Tests.MetadataDecoding" + +[[lean_lib]] +name = "Tests.MetadataWorld" + +[[lean_lib]] +name = "Tests.MetadataBinding" + +[[lean_lib]] +name = "Tests.ImportedFieldIndex" + +[[lean_lib]] +name = "Tests.OpaqueClassFields" + +[[lean_lib]] +name = "Tests.StaticFieldSites" + +[[lean_lib]] +name = "Tests.GenericOwnerScaling" + [[lean_lib]] name = "Tests.LiteralDisplay" +[[lean_lib]] +name = "Tests.Identity" + +[[lean_lib]] +name = "Tests.ResolutionFacts" + +[[lean_lib]] +name = "Tests.SpanHomes" + +[[lean_lib]] +name = "Tests.ValueDecoding" + +[[lean_lib]] +name = "Tests.DecodedMethod" + +[[lean_lib]] +name = "Tests.DecodedSummary" + +[[lean_lib]] +name = "Tests.DecodedSegment" + +[[lean_lib]] +name = "Tests.FieldRef" + +[[lean_lib]] +name = "Tests.FieldShape" + +[[lean_lib]] +name = "Tests.RuntimeModels" + +[[lean_lib]] +name = "Tests.LoweredCallRefusal" + +[[lean_lib]] +name = "Tests.BitModels" + +[[lean_lib]] +name = "Tests.UnsafeModels" + +[[lean_lib]] +name = "Tests.UnsafeTransferModels" + +[[lean_lib]] +name = "Tests.UnsafeReinterpretations" + +[[lean_lib]] +name = "Tests.UnsafeOffsets" + +[[lean_lib]] +name = "Tests.UnsafeBlocks" + +[[lean_lib]] +name = "Tests.ReturnOpcode" + +[[lean_lib]] +name = "Tests.ReturnMetadataBinding" + +[[lean_lib]] +name = "Tests.PlatformDefinitions" + [[lean_lib]] name = "Tests.Automation" @@ -179,6 +285,12 @@ name = "Tests.ThrowEvidence" [[lean_lib]] name = "Tests.SpanEvidence" +[[lean_lib]] +name = "Tests.SpanModels" + +[[lean_lib]] +name = "Tests.MarshalModels" + [[lean_lib]] name = "Tests.ConstrainedEvidence" @@ -218,6 +330,18 @@ name = "Tests.ContractClosure" [[lean_lib]] name = "Tests.ExactIntrinsics" +[[lean_lib]] +name = "Tests.ShuffleContract" + +[[lean_lib]] +name = "Tests.HalfSwapContract" + +[[lean_lib]] +name = "Tests.AesRoundContract" + +[[lean_lib]] +name = "Tests.SignWindowContract" + [[lean_lib]] name = "Tests.ScalarEvidence" @@ -286,6 +410,9 @@ name = "Tests.FeatureGetter" [[lean_lib]] name = "Tests.Segment" +[[lean_lib]] +name = "Tests.SegmentContract" + [[lean_lib]] name = "Tests.AllocationRenaming" @@ -319,8 +446,23 @@ name = "Tests.WeakestPrecondition" [[lean_lib]] name = "Tests.LocalCalls" +[[lean_lib]] +name = "Tests.WordFields" + [[lean_lib]] name = "Tests.CallingFields" [[lean_lib]] name = "Tests.PublicProofSurface" + +[[lean_lib]] +name = "Tests.StrictBinding" + +[[lean_lib]] +name = "Tests.Crypto" + +[[lean_lib]] +name = "Tests.MetadataAssemblies" + +[[lean_lib]] +name = "Tests.PinnedIndexScaling" diff --git a/scripts/BatchVerification.cs b/scripts/BatchVerification.cs index 6a5a2442a..6fe9f93ae 100644 --- a/scripts/BatchVerification.cs +++ b/scripts/BatchVerification.cs @@ -1,3 +1,4 @@ +using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.Text.RegularExpressions; @@ -43,7 +44,8 @@ void VerifyGeneratedBatch(string assembly, string identity, IReadOnlyList r with { Proof = Path.GetFullPath(r.Proof), Output = Path.TrimEndingDirectorySeparator(Path.GetFullPath(r.Output)), Assembly = Path.GetFullPath(r.Assembly ?? assembly) }).ToArray(); - if (requests.Select(r => Path.GetFullPath(r.Output)).Distinct(StringComparer.OrdinalIgnoreCase).Count() != requests.Count) + // Outputs in another case are distinct only where the volume is known to tell case apart; a link never is. + if (requests.Select(r => ResolvedPath(r.Output)).Distinct(CollidingLocationComparer).Count() != requests.Count) throw new ArgumentException("Batch outputs must be distinct"); foreach (var request in requests) { @@ -172,20 +174,24 @@ string Body(string source) var theorem = $"BatchCase{i}.{request.Theorem}"; var axioms = AuditedAxioms(audited.theorems[2 * i]); AuditedAxioms(audited.theorems[2 * i + 1]); + // The manifest a refutation is bound to is the bytes whose digest matched, read once. + var manifest = File.ReadAllBytes(Path.Combine(request.Output, "identity.json")); if (!Same(before[i], Inputs(commonAfter, request.Proof)) || - hashes[i] != Sha(Path.Combine(request.Output, "identity.json"))) + hashes[i] != Convert.ToHexStringLower(SHA256.HashData(manifest))) throw new InvalidOperationException("Batch inputs changed during verification"); - var extraction = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(request.Output, "identity.json"))); + var extraction = JsonSerializer.Deserialize(manifest); var initializedTypes = extraction.TryGetProperty("initializedTypes", out var assumptions) ? assumptions.Deserialize() : []; - reports[i] = new Report(4, Sha(request.Assembly!), hashes[i], request.Theorem, axioms, AuditText(audited.theorems[2 * i]), before[i], - libraryKey, libraryBuild, AuditText(audited.theorems[2 * i + 1]), initializedTypes, - Sha(sources[shardOf[i]]), theorem, + reports[i] = new Report(CertificateSchema, Sha(request.Assembly!), hashes[i], request.Theorem, axioms, + AuditText(audited.theorems[2 * i]), before[i], BindingSection.Default, libraryKey, libraryBuild, + AuditText(audited.theorems[2 * i + 1]), initializedTypes, Sha(sources[shardOf[i]]), theorem, staticValues: StaticValueAssumptions(extraction), opaqueStaticValues: OpaqueStaticValues(extraction), imports: PublishedImports(extraction), contractDependencies: DeploymentDependencies(extraction), - initializationAssumptions: InitializationAssumptions(extraction)); + initializationAssumptions: InitializationAssumptions(extraction), + refutation: ExtractedRefutation(audited.theorems[2 * i], extraction, + $"BatchCase{i}.{ExtractedProgram}")); } // A package may require the same library by path and have built into it since the last snapshot. RequireUnchangedLibrary(); diff --git a/scripts/BenchmarkExternalIdentities.cs b/scripts/BenchmarkExternalIdentities.cs index bb67bc78f..ea6740e5b 100644 --- a/scripts/BenchmarkExternalIdentities.cs +++ b/scripts/BenchmarkExternalIdentities.cs @@ -1,6 +1,11 @@ #!/usr/bin/env dotnet #:property PublishAot=false #:include Verification.cs +#:include DeepChecks.cs +#:include FailureReports.cs +#:include LeanDiagnostics.cs +#:include BindingImage.cs +#:include DeepCheckInputs.cs #:include ../Extractor/ExternalIdentityEvidence.cs using System.Diagnostics; diff --git a/scripts/BindingImage.cs b/scripts/BindingImage.cs new file mode 100644 index 000000000..beab4ab31 --- /dev/null +++ b/scripts/BindingImage.cs @@ -0,0 +1,208 @@ +using System.Buffers.Binary; +using System.Globalization; +using System.Numerics; +using System.Security.Cryptography; +using System.Text; + +/// +/// Lean source for an assembly file as a `StrictBinding.ImageBytes` constant, and for the SHA-256 +/// proof of that constant as separate per-page lemmas, so no single kernel evaluation holds more +/// than one page of hashing. The generated text is never trusted: the kernel rejects a page lemma +/// or an image whose digest is not the stated one. +/// +static class BindingImage +{ + const int ChunkBytes = 64; + const int ChunkBits = 8 * ChunkBytes; + const int PageChunks = 64; + const int PageBytes = ChunkBytes * PageChunks; + + static readonly uint[] InitialState = + [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]; + + static readonly uint[] RoundConstants = + [ + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + ]; + + /// The lowercase hexadecimal SHA-256 digest of a file. + internal static string Digest(byte[] file) => Convert.ToHexStringLower(SHA256.HashData(file)); + + /// + /// The page and image definitions of a file, in the namespace the caller has opened; the module must import + /// `CIL.Extraction.FixedDigits`. Each page splits one code of its zero-padded bytes into its chunks, so chunks + /// that end in the same bytes are not separate numerals that collide in the kernel's caches. + /// + internal static string Definitions(byte[] file) + { + var text = new StringBuilder(); + for (var page = 0; page < PageCount(file.Length); page++) + { + var first = page * PageBytes; + var present = Math.Min(file.Length - first, PageBytes); + var chunks = (present + ChunkBytes - 1) / ChunkBytes; + var bytes = new byte[chunks * ChunkBytes]; + file.AsSpan(first, present).CopyTo(bytes); + text.Append(CultureInfo.InvariantCulture, + $"def page{page} : List Nat := CIL.Extraction.fixedDigitsWithin {ChunkBits} {chunks} {chunks} "); + NumeralCode.Append(text, bytes); + text.Append(" []\n\n"); + } + return text.Append(ImageDefinition(file.Length)).ToString(); + } + + /// The image definition of a file of bytes over its page definitions. + internal static string ImageDefinition(int length) + { + var names = string.Join(", ", Enumerable.Range(0, PageCount(length)).Select(page => "page" + page)); + return $"def image : StrictBinding.ImageBytes := {{ length := {length}, pages := [{names}] }}\n"; + } + + /// + /// The digest theorem proved by one kernel evaluation: only for files of a few kilobytes, since + /// the kernel keeps every intermediate term of the evaluation. + /// + internal static string SingleDigestTheorem(byte[] file) => file.Length > PageBytes + ? throw new ArgumentException( + "A single kernel evaluation is only for one page of file; use the paged theorem.") + : $"theorem digest : StrictBinding.DigestIs image\n 0x{Digest(file)} := by decide +kernel\n"; + + /// + /// The digest theorem proved page by page: one lemma per page states the hash state it leaves, + /// each checked by its own kernel evaluation, and `digest` assembles them. With + /// the lemmas are not checked when the module compiles, so a + /// caller must kernel-replay the module. + /// + internal static string PagedDigestTheorem(byte[] file, bool skipCompileCheck) + { + var pages = PageCount(file.Length); + var states = PageStates(file, pages); + var text = new StringBuilder(); + var skip = skipCompileCheck ? "set_option debug.skipKernelTC true in\n" : ""; + for (var page = 0; page < pages; page++) + { + var from = page == 0 ? "StrictBinding.Sha256.initialState" : Words(states[page]); + text.Append($"{skip}theorem hash{page} : StrictBinding.Sha256.hashBlocks {from} " + + $"{PageTerm(page, pages, file.Length)} = {Words(states[page + 1])} := by decide +kernel\n"); + } + // Each suffix of the page list and each proof of its hash is its own declaration, so no term nests + // deeper than one level however many pages there are. + text.Append($"def blocks{pages - 1} : List (List Nat) := [{PageTerm(pages - 1, pages, file.Length)}]\n"); + for (var page = pages - 2; page >= 0; page--) + text.Append($"def blocks{page} : List (List Nat) := " + + $"{PageTerm(page, pages, file.Length)} :: blocks{page + 1}\n"); + var final = Words(states[pages]); + text.Append($"theorem tail{pages - 1} : StrictBinding.Sha256.PagesHash {Words(states[pages - 1])} " + + $"blocks{pages - 1} {final} := .cons hash{pages - 1} (.nil _)\n"); + for (var page = pages - 2; page >= 0; page--) + { + var from = page == 0 ? "StrictBinding.Sha256.initialState" : Words(states[page]); + text.Append($"theorem tail{page} : StrictBinding.Sha256.PagesHash {from} blocks{page} {final} := " + + $".cons hash{page} tail{page + 1}\n"); + } + text.Append($"theorem digest : StrictBinding.DigestIs image 0x{Digest(file)} :=\n"); + text.Append($" StrictBinding.digestIs_of_hashes (final := {final}) (blocks := blocks0)\n"); + text.Append(" (by decide +kernel) tail0 (by decide +kernel) (by decide +kernel)\n"); + return text.ToString(); + } + + static int PageCount(int length) => (length + PageBytes - 1) / PageBytes; + + /// The page as hashed: every page but the last as it is, and the last with its padding. + static string PageTerm(int page, int pages, int length) => + page + 1 < pages ? "page" + page : $"(CIL.Crypto.padded page{page} {length})"; + + static string Words(uint[] state) => "[" + string.Join(", ", state.Select(word => $"0x{word:x8}")) + "]"; + + /// The hash state before page k, for k up to the page count: the state after the last page. + static uint[][] PageStates(byte[] file, int pages) + { + var padding = (int)(64 - (file.LongLength + 9) % 64) % 64; + var message = new byte[file.Length + 1 + padding + 8]; + file.CopyTo(message, 0); + message[file.Length] = 0x80; + BinaryPrimitives.WriteUInt64BigEndian(message.AsSpan(message.Length - 8), (ulong)file.Length * 8); + var states = new uint[pages + 1][]; + var state = InitialState; + states[0] = state; + for (var page = 0; page < pages; page++) + { + var end = page + 1 < pages ? (page + 1) * PageBytes : message.Length; + for (var offset = page * PageBytes; offset < end; offset += ChunkBytes) + state = Compress(state, message.AsSpan(offset, ChunkBytes)); + states[page + 1] = state; + } + var final = new byte[32]; + for (var index = 0; index < 8; index++) + BinaryPrimitives.WriteUInt32BigEndian(final.AsSpan(4 * index), state[index]); + if (Convert.ToHexStringLower(final) != Digest(file)) + throw new InvalidOperationException("SHA-256 midstates disagree with the file digest"); + return states; + } + + static uint Rotate(uint word, int amount) => BitOperations.RotateRight(word, amount); + + static uint[] Compress(uint[] state, ReadOnlySpan block) + { + Span schedule = stackalloc uint[64]; + for (var index = 0; index < 16; index++) + schedule[index] = BinaryPrimitives.ReadUInt32BigEndian(block[(4 * index)..]); + for (var index = 16; index < 64; index++) + { + var near = schedule[index - 15]; + var far = schedule[index - 2]; + var low = Rotate(near, 7) ^ Rotate(near, 18) ^ (near >> 3); + var high = Rotate(far, 17) ^ Rotate(far, 19) ^ (far >> 10); + schedule[index] = schedule[index - 16] + low + schedule[index - 7] + high; + } + var (a, b, c, d, e, f, g, h) = + (state[0], state[1], state[2], state[3], state[4], state[5], state[6], state[7]); + for (var index = 0; index < 64; index++) + { + var upperE = Rotate(e, 6) ^ Rotate(e, 11) ^ Rotate(e, 25); + var first = h + upperE + ((e & f) ^ (~e & g)) + RoundConstants[index] + schedule[index]; + var upperA = Rotate(a, 2) ^ Rotate(a, 13) ^ Rotate(a, 22); + var second = upperA + ((a & b) ^ (a & c) ^ (b & c)); + (h, g, f, e, d, c, b, a) = (g, f, e, d + first, c, b, a, first + second); + } + uint[] sum = [a, b, c, d, e, f, g, h]; + return [.. sum.Select((word, index) => state[index] + word)]; + } +} + +/// +/// Lean source for bytes as one big-endian number: one hexadecimal numeral of at most +/// bytes, or the codes of its first half of numerals and of the rest joined by +/// Nat.lor and Nat.shiftLeft. One joined term keeps numerals with equal low bytes from colliding in +/// the kernel's caches, which hash a numeral by its low 64 bits. +/// +static class NumeralCode +{ + /// + /// The most bytes one numeral holds: the elaborator parses a numeral in time quadratic in its digits. + /// + internal const int LiteralBytes = 256; + + /// Appends the code of . + internal static void Append(StringBuilder text, ReadOnlySpan data) + { + if (data.Length <= LiteralBytes) + { + text.Append("0x").Append(Convert.ToHexString(data)); + return; + } + var highBytes = (data.Length + LiteralBytes - 1) / LiteralBytes / 2 * LiteralBytes; + text.Append("(Nat.lor (Nat.shiftLeft "); + Append(text, data[..highBytes]); + text.Append(CultureInfo.InvariantCulture, $" {8 * (data.Length - highBytes)}) "); + Append(text, data[highBytes..]); + text.Append(')'); + } +} diff --git a/scripts/CertificateCases.cs b/scripts/CertificateCases.cs new file mode 100644 index 000000000..357bfa751 --- /dev/null +++ b/scripts/CertificateCases.cs @@ -0,0 +1,121 @@ +using System.Text.Json.Nodes; + +/// +/// Certificates that differ from a valid default certificate in one way every certificate reader must refuse, and +/// the form a deep run would give a valid one. +/// +static class CertificateCases +{ + static JsonArray Facts(JsonNode report) => report["binding"]!["facts"]!.AsArray(); + + /// + /// The valid default certificate `valid` as a deep run writes it: the digest file and sources named, one + /// pinned image and each fact reconfirmed by a theorem. + /// + internal static string Deep(string valid) + { + var report = JsonNode.Parse(valid)!; + var binding = report["binding"]!; + binding["mode"] = "deep"; + binding["digests"] = new JsonObject { ["path"] = "pins.sha256", ["sha256"] = new string('0', 64) }; + binding["sources"] = new JsonObject + { + ["strictBinding"] = new string('a', 64), ["leanToolchain"] = "leanprover/lean4:v4", + ["lean"] = new string('1', 64) + }; + binding["images"] = new JsonArray(new JsonObject + { + ["identity"] = "Examples", ["sha256"] = new string('2', 64), ["role"] = "pinned", + ["status"] = "reconfirmed", ["theorem"] = "BindingAudit.I0.digest", ["reason"] = null + }); + foreach (var fact in Facts(report)) + { + fact!["status"] = "reconfirmed"; + fact["theorem"] = "BindingAudit.bodies"; + } + return report.ToJsonString(); + } + + /// Independent malformed deep metadata values, each applied to a valid certificate. + internal static IEnumerable<(string Case, string Text)> RefusedDeep(string valid) + { + (string Section, string Field, bool Hash)[] fields = + [ + ("digests", "path", false), ("digests", "sha256", true), + ("sources", "strictBinding", true), ("sources", "leanToolchain", false), + ("sources", "lean", true), ("images", "identity", false), ("images", "sha256", true) + ]; + foreach (var (section, field, hash) in fields) + { + string?[] values = hash + ? [null, "", " \t\r\n", new string('a', 63), new string('a', 65), new string('g', 64), + new string('a', 63) + "\n", " " + new string('a', 64)] + : [null, "", " \t\r\n"]; + foreach (var (index, value) in values.Index()) + { + var report = JsonNode.Parse(Deep(valid))!; + var target = report["binding"]![section]!; + if (section == "images") target = target[0]!; + target[field] = value; + yield return ($"{section}.{field} malformed value {index}", report.ToJsonString()); + } + } + } + + /// Deep certificates with valid mixed-case digests and each settled image and fact status. + internal static IEnumerable AcceptedDeep(string valid) + { + var report = JsonNode.Parse(Deep(valid))!; + var binding = report["binding"]!; + const string digest = "0123456789abcdef" + "0123456789ABCDEF" + "0123456789abcdef" + "0123456789ABCDEF"; + binding["digests"]!["sha256"] = digest; + binding["sources"]!["strictBinding"] = digest; + binding["sources"]!["lean"] = digest; + binding["images"]![0]!["sha256"] = digest; + yield return report.ToJsonString(); + foreach (var fact in Facts(report).Append(binding["images"]![0])) + { + fact!["status"] = "not covered"; + fact["theorem"] = null; + fact["reason"] = "An image was not supplied."; + } + yield return report.ToJsonString(); + binding["images"] = new JsonArray(); + yield return report.ToJsonString(); + } + + /// Each variant of the valid default certificate `valid` that a default reader refuses, by name. + /// + internal static IEnumerable<(string Case, string Text)> Refused(string valid) + { + (string, string) Changed(string name, Action change) + { + var report = JsonNode.Parse(valid)!; + change(report); + return (name, report.ToJsonString()); + } + const int previous = Program.CertificateSchema - 1; + yield return Changed($"schema {previous}", report => report["schema"] = previous); + yield return Changed("no binding section", report => report.AsObject().Remove("binding")); + yield return Changed("a missing fact", report => Facts(report).RemoveAt(3)); + yield return Changed("reordered facts", report => + { + var facts = Facts(report); + var first = facts[0]; + facts.RemoveAt(0); + facts.Add(first); + }); + yield return Changed("a repeated fact", report => Facts(report).Add(Facts(report)[0]!.DeepClone())); + yield return Changed("an unknown fact", report => Facts(report)[0]!["id"] = "body-offsets"); + yield return Changed("an unknown status", report => Facts(report)[0]!["status"] = "assumed"); + yield return Changed("a status given as a number", report => Facts(report)[0]!["status"] = 0); + yield return Changed("a fact without a status", report => Facts(report)[0]!.AsObject().Remove("status")); + yield return Changed("a trusted fact naming a theorem", + report => Facts(report)[0]!["theorem"] = "BindingAudit.bodies"); + yield return Changed("a default section naming a digest file", report => report["binding"]!["digests"] = + JsonNode.Parse(Deep(valid))!["binding"]!["digests"]!.DeepClone()); + yield return Changed("a default section without its images", + report => report["binding"]!.AsObject().Remove("images")); + yield return ("a deep run's certificate", Deep(valid)); + } +} diff --git a/scripts/CertifiedLibraryArchive.cs b/scripts/CertifiedLibraryArchive.cs index be1926476..edeb0b327 100644 --- a/scripts/CertifiedLibraryArchive.cs +++ b/scripts/CertifiedLibraryArchive.cs @@ -61,9 +61,8 @@ public static string Pack(string source, string archive, string manifestDigest) source = Path.GetFullPath(source); archive = Path.GetFullPath(archive); RequireDigest(Path.Combine(source, "cache-manifest.json"), manifestDigest); - var relativeArchive = Path.GetRelativePath(source, archive); - if (File.Exists(archive) || Directory.Exists(archive) || (!Path.IsPathRooted(relativeArchive) && - !relativeArchive.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal))) + // An archive reached through a link into the library would be packed into itself. + if (File.Exists(archive) || Directory.Exists(archive) || Program.Inside(source, archive)) throw new InvalidDataException("Archive must be new and outside the library"); Directory.CreateDirectory(Path.GetDirectoryName(archive)!); using var process = Start(["-T2", "-6", "-q", "-o", archive], input: true); @@ -137,8 +136,8 @@ public static void Restore(string download, string destination, string manifestD try { using var reader = new TarReader(process.StandardOutput.BaseStream, leaveOpen: true); - var seen = new HashSet(OperatingSystem.IsWindows() - ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal); + // Entry names differing only in case are one file unless the staging volume tells case apart. + var seen = new HashSet(Program.CollidingLocationComparer); while (reader.GetNextEntry() is { } entry) { var path = EntryPath(staging, entry.Name); diff --git a/scripts/CheckDocReferences.cs b/scripts/CheckDocReferences.cs index 8617acf5b..82e0fc31b 100644 --- a/scripts/CheckDocReferences.cs +++ b/scripts/CheckDocReferences.cs @@ -12,13 +12,16 @@ // citations drift silently, so they fail too. Snippet elaboration needs Lean and runs in the docs suite. // The validation guide's suite table must list exactly the suites the runner accepts. The example proofs // are what users copy, so each prints the axioms of its theorems and names no evaluator internal in a -// simp list, and neither does a Lean block of the documentation. +// simp list, and neither does a Lean block of the documentation. No tracked text file holds a control +// byte other than tab and line ends, or an invisible character such as a zero-width space, a +// bidirectional override or a no-break space. var root = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(SourcePath())!, "..")); var pages = Documentation.Documents(root).Select(path => Documentation.Read(root, path)).ToArray(); var (failures, references, blocks) = Documentation.CheckReferences(root, pages); var suitePage = pages.Single(page => Documentation.Relative(root, page.Path) == Documentation.SuiteDocument); failures.AddRange(Documentation.CheckSuiteTable(root, suitePage, VerificationSuites.Names())); failures.AddRange(Documentation.CheckExamples(root)); +failures.AddRange(Documentation.ControlCharacterProblems(root, TrackedFiles(root))); if (failures.Count != 0) { Console.Error.WriteLine($"Stale documentation references ({failures.Count}):"); @@ -29,4 +32,16 @@ Console.WriteLine($"PASS {references} code references and {blocks} Lean blocks in {pages.Length} documents"); return 0; +// The files git tracks, relative to the root; an untracked build output is not source. +static string[] TrackedFiles(string root) +{ + var start = new System.Diagnostics.ProcessStartInfo("git") { RedirectStandardOutput = true }; + foreach (var argument in new[] { "-C", root, "ls-files", "-z" }) start.ArgumentList.Add(argument); + using var git = System.Diagnostics.Process.Start(start)!; + var listing = git.StandardOutput.ReadToEnd(); + git.WaitForExit(); + return git.ExitCode == 0 ? listing.Split('\0', StringSplitOptions.RemoveEmptyEntries) + : throw new InvalidOperationException("git ls-files failed in " + root); +} + static string SourcePath([CallerFilePath] string path = "") => path; diff --git a/scripts/ConsumerVerification.cs b/scripts/ConsumerVerification.cs index f8c531863..c12b2e5a2 100644 --- a/scripts/ConsumerVerification.cs +++ b/scripts/ConsumerVerification.cs @@ -188,7 +188,7 @@ static void TraceConsumerFingerprints(string session, DateTimeOffset started, Ti // Receipts reuse only the linter-stripped byte transformation. Source/import binding, // certificate eligibility and replay remain the callers' independent obligations. Dictionary> PortableConsumerModuleParts( - string session, Dictionary modules) + string session, Dictionary modules, bool publishReceipts = true) { var started = DateTimeOffset.UtcNow; var elapsed = System.Diagnostics.Stopwatch.StartNew(); @@ -262,12 +262,15 @@ Dictionary> PortableConsumerModuleParts { if (!Same(item.Input.Parts, ConsumerModuleParts(item.Source))) throw new InvalidDataException("Fingerprint input snapshot changed: " + item.Input.Name); - Directory.CreateDirectory(item.Receipt); - File.WriteAllText(Path.Combine(item.Receipt, "fingerprints.json"), - JsonSerializer.Serialize(results[item.Input.Name])); - PublishSuccess(item.Input.Key, item.Receipt); + if (publishReceipts) + { + Directory.CreateDirectory(item.Receipt); + File.WriteAllText(Path.Combine(item.Receipt, "fingerprints.json"), + JsonSerializer.Serialize(results[item.Input.Name])); + PublishSuccess(item.Input.Key, item.Receipt); + } } - if (UseSuccessCache && Directory.Exists(SuccessCacheRoot)) EvictStaleSupport(); + if (publishReceipts && UseSuccessCache && Directory.Exists(SuccessCacheRoot)) EvictStaleSupport(); RequireCertifiedFingerprints(certified, modules, canonicalTools); TraceConsumerFingerprints(session, started, elapsed.Elapsed, certified.Count + inputs.Length - missing.Count, missing.Count); @@ -296,9 +299,11 @@ sealed class ConsumerSupportExport // Called only after the workspace's replay, audits, rechecks and source-stability checks passed. void AddExportedSupport(ConsumerSupportExport export, string session, string root, SortedDictionary inputs, - Dictionary independent, Dictionary graph, string? collected) + Dictionary independent, Dictionary graph, string? collected, + bool publishReceipts = true, Action? requireStable = null) { - var fingerprints = PortableConsumerModuleParts(session, independent); + var fingerprints = PortableConsumerModuleParts(session, independent, publishReceipts); + requireStable?.Invoke(); var record = collected is null ? null : ReadConsumerSupportRecord(Path.Combine(collected, ConsumerSupportRecordName)); var sources = PortableConsumerSources(root, inputs); lock (export.Gate) @@ -448,11 +453,20 @@ void RestorePortableSupport(string session, TrustedSupport[] trusted) if (restored > 0) CacheProgress($"portable consumer support library ({restored} modules)"); } - HashSet ImportedConsumerSupport(string session, TrustedSupport[] trusted, Dictionary modules) + static Dictionary ConsumerSupportModules(IEnumerable> manifests, + Dictionary modules) + { + var names = manifests.SelectMany(manifest => manifest).ToHashSet(StringComparer.Ordinal); + return modules.Where(pair => names.Contains(pair.Key)).ToDictionary(StringComparer.Ordinal); + } + + HashSet ImportedConsumerSupport(string session, TrustedSupport[] trusted, + Dictionary modules, bool publishReceipts = true) { if (trusted.Length == 0) return []; var fingerprints = PortableConsumerModuleParts(session, - modules.Where(pair => trusted.Any(support => support.Manifest.Modules.ContainsKey(pair.Key))).ToDictionary()); + ConsumerSupportModules(trusted.Select(support => support.Manifest.Modules.Keys), modules), + publishReceipts); var result = new HashSet(StringComparer.Ordinal); foreach (var (manifest, _) in trusted) { @@ -487,97 +501,22 @@ Dictionary ReplayConsumerModules(string session, IEnumerable ReplayConsumerModulesSeparately(string session, string[] targets, string leanPath, bool profile) - { - var configuredThreads = Environment.GetEnvironmentVariable("LEAN_NUM_THREADS"); - var threads = string.IsNullOrWhiteSpace(configuredThreads) ? "2" : configuredThreads; - if (!uint.TryParse(threads, out var count) || count == 0) - throw new InvalidDataException("LEAN_NUM_THREADS must be a positive integer."); - var runner = Path.Combine(Path.GetTempPath(), "leancil-replay-" + Guid.NewGuid().ToString("N") + ".lean"); - var targetsPath = Path.ChangeExtension(runner, ".json"); - var native = !profile && auditExecutable is not null; - try - { - // A literal target list exceeds Lean's recursion limit for large module sets. - File.WriteAllText(targetsPath, JsonSerializer.Serialize(targets)); - // Use the pinned toolchain's checker, not a consumer implementation - // of kernel replay. Explicit targets avoid repeated search-path scans. - if (!native) File.WriteAllText(runner, """ -import LeanChecker -unsafe def replayConsumerModule (name : String) : IO Nat := do - let started ← IO.monoMsNow - replayFromImports name.toName - return (← IO.monoMsNow) - started -def reportConsumerModule (name : String) (elapsed : Nat) : IO Unit := - IO.println (Lean.Json.mkObj [("module", Lean.toJson name), ("milliseconds", Lean.toJson elapsed)]).compress -def reportConsumerFailure (name : String) (error : IO.Error) : IO Unit := - IO.println (Lean.Json.mkObj [("module", Lean.toJson name), ("error", Lean.toJson (toString error))]).compress -unsafe def replayConsumerModules (args : List String) (profile : Bool) : IO Unit := do - Lean.initSearchPath (← Lean.findSysroot) - let mut failed := false - if profile then - for name in args do - try - reportConsumerModule name (← replayConsumerModule name) - catch error => - reportConsumerFailure name error - failed := true - else - let tasks ← args.toArray.mapM fun name => do - return (name, ← IO.asTask (replayConsumerModule name)) - for (name, task) in tasks do - match task.get with - | .ok elapsed => reportConsumerModule name elapsed - | .error error => - reportConsumerFailure name error - failed := true - if failed then - throw (IO.userError "Kernel replay rejected at least one consumer module") -""" + "\n#eval do\n let json ← IO.FS.readFile (String.fromUTF8! (ByteArray.mk #[" + - string.Join(",", System.Text.Encoding.UTF8.GetBytes(targetsPath)) + - "]))\n let args ← IO.ofExcept <| (Lean.Json.parse json).bind Lean.fromJson?\n replayConsumerModules args " + - profile.ToString().ToLowerInvariant() + "\n"); - // Unlike leanchecker, the lean frontend defaults to hardware - // concurrency rather than honoring LEAN_NUM_THREADS itself. - string[] command = native ? [CheckedAuditExecutable(), "--replay-separately", targetsPath] - : [LeanExecutable, "-j" + threads, runner]; - var environment = new Dictionary(LeanEnvironment) { ["LEAN_NUM_THREADS"] = threads }; - var result = Execute(command, session, leanPath, progress: profile - ? "Profiled kernel replay of consumer modules" : "Kernel replay of consumer proof and dependencies", - environment: environment); - if (native) CheckedAuditExecutable(); - var replayed = new HashSet(StringComparer.Ordinal); - var failures = new Dictionary(StringComparer.Ordinal); - foreach (var line in result.Stdout.Split('\n', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)) - { - if (!line.StartsWith('{')) continue; - JsonElement record; - try { record = JsonSerializer.Deserialize(line); } - catch (JsonException) { continue; } - if (record.ValueKind != JsonValueKind.Object || !record.TryGetProperty("module", out var module) || - module.ValueKind != JsonValueKind.String) continue; - if (record.TryGetProperty("error", out var error)) failures[module.GetString()!] = error.ToString(); - else replayed.Add(module.GetString()!); - } - if ((result.Code == 0) != (failures.Count == 0) || replayed.Overlaps(failures.Keys) || - !targets.All(name => replayed.Contains(name) || failures.ContainsKey(name))) - throw new InvalidOperationException($"Command failed ({result.Code}): {string.Join(' ', command)}\n{result.Output}"); - return failures; - } - finally { File.Delete(runner); File.Delete(targetsPath); } - } /// A Lake dependency of a consumer package: a git checkout pinned by a full revision. sealed record ConsumerDependency(string Name, string Directory, string Source, string Revision, string Url); ConsumerDependency[]? fullBundleDependencies { get; init; } + /// + /// `relative`, read against `root`, spelled under `root` as given. Containment is decided after links are + /// followed on both sides, so Lake's resolved spelling of a package directory matches the root as given. + /// static string Within(string root, string relative) { - var path = Path.GetFullPath(relative, root); - var back = Path.GetRelativePath(root, path); - if (Path.IsPathRooted(back) || back == ".." || back.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal)) - throw new InvalidDataException("Consumer path escapes its package: " + relative); - return path; + var spelledRoot = Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)); + // Not made full first: Path.GetFullPath would drop a `..` before the link it follows is read. + var inside = ResolvedRelativePath(spelledRoot, Path.Combine(spelledRoot, relative)) + ?? throw new InvalidDataException("Consumer path escapes its package: " + relative); + return inside == "." ? spelledRoot : Path.Join(spelledRoot, inside); } /// A directory name whose contents are never consumer sources, such as build outputs. @@ -586,7 +525,8 @@ static bool ExcludedConsumerDirectory(string name) => static IEnumerable ConsumerSources(string root, string? excludedDirectory = null) { - if (excludedDirectory is not null && Path.GetFullPath(root) == Path.GetFullPath(excludedDirectory)) yield break; + if (excludedDirectory is not null && + LocationComparer.Equals(Path.GetFullPath(root), Path.GetFullPath(excludedDirectory))) yield break; foreach (var path in Directory.EnumerateFileSystemEntries(root).Order(StringComparer.Ordinal)) { if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != 0) @@ -695,7 +635,7 @@ [.. ConsumerGeneratedImports(proof, snapshot, root).Where(import => SelectionMod while (pending.TryDequeue(out var path)) { if (!seen.Add(path)) continue; - foreach (Match import in SourceImport().Matches(File.ReadAllText(path))) + foreach (Match import in SourceImport.Matches(File.ReadAllText(path))) { var name = import.Groups[1].Value; if (name == "ExtractedSymbols" || SelectionModules.Is(name)) yield return (name, path); @@ -746,12 +686,13 @@ static string UseCertifiedFramework(string session, string certified) return generated; } - static void AddConsumerLibrary(string directory, string name, string[]? leanArguments = null) + internal static void AddConsumerLibrary(string directory, string name, string[]? leanArguments = null) { var toml = Path.Combine(directory, "lakefile.toml"); - // TOML and Lean string literals both accept a JSON-quoted string. - var quoted = string.Join(", ", (leanArguments ?? []).Select(argument => JsonSerializer.Serialize(argument))); - if (File.Exists(toml)) + bool useToml = File.Exists(toml); + Func quote = useToml ? value => JsonSerializer.Serialize(value) : LeanString; + var quoted = string.Join(", ", (leanArguments ?? []).Select(quote)); + if (useToml) File.AppendAllText(toml, $"\n[[lean_lib]]\nname = \"{name}\"\n" + (quoted.Length == 0 ? "" : $"moreLeanArgs = [{quoted}]\n")); else @@ -763,6 +704,21 @@ static void AddConsumerLibrary(string directory, string name, string[]? leanArgu static string CertifiedModuleDirectory(string certified) => Path.TrimEndingDirectorySeparator(Path.GetFullPath(Path.Combine(certified, ".lake/build/lib/lean"))); + /// + /// The import directories in `leanPath`, read against `session`, without the trusted toolchain's: the certified + /// base spelled as `certified` and every other directory spelled under `session`. A directory that lies + /// outside the session once links are followed is refused, so no import is read from a directory the session + /// does not hold. + /// + static string[] ValidatedConsumerImportDirectories( + string session, string trusted, string? certified, string leanPath) => + [.. leanPath.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries) + .Select(path => Path.Combine(session, path)) + // Lake reports directories with links followed, so each is compared after following links too. + .Where(path => !SameLocation(trusted, path)) + .Select(path => certified is not null && SameLocation(certified, path) ? certified : Within(session, path)) + .Distinct(LocationComparer)]; + (string[] Directories, string LeanPath) ConsumerImportDirectories(string session) { // Ask the same Lake environment used by the checker. Package buildDir and @@ -771,15 +727,9 @@ static string CertifiedModuleDirectory(string certified) => .Where(line => line.StartsWith("LEAN_PATH=", StringComparison.Ordinal)).ToArray(); if (lines.Length != 1) throw new InvalidDataException("Could not resolve consumer Lean import paths."); var leanPath = lines[0]["LEAN_PATH=".Length..]; - var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; var trusted = Path.GetFullPath(Path.Combine(Toolchain, "lib/lean")); var certified = library is null ? null : CertifiedModuleDirectory(library); - return (leanPath.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries) - .Select(path => Path.TrimEndingDirectorySeparator(Path.GetFullPath(path, session))) - .Where(path => !path.Equals(trusted, comparison)) - // Keep the certified base in place; map other imports into the session. - .Select(path => path.Equals(certified, comparison) ? path : Within(session, Path.GetRelativePath(session, path))) - .Distinct(OperatingSystem.IsWindows() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal).ToArray(), leanPath); + return (ValidatedConsumerImportDirectories(session, trusted, certified, leanPath), leanPath); } // Variables read by Lean, Lake and elan, plus any a consumer's elaborators read @@ -1050,12 +1000,12 @@ sealed record ConsumerSession(string Generated, Dictionary Copie sealed record ExtractionComponents(string InputHash, string Aggregate, Dictionary Sources); [GeneratedRegex(@"(?m)^set_option maxRecDepth [0-9]+\r?$")] - private static partial Regex ExtractionRecursionDepth(); + private static partial Regex ExtractionRecursionDepth { get; } // Components keep the extraction's own recursion depth, which follows its longest method; // a fixed depth would reject bodies the complete extraction compiles. static string ExtractionComponentContext(string source) => - "namespace Extracted\nopen CIL\n" + ExtractionRecursionDepth().Match(source).Value.TrimEnd('\r') + "\n\n"; + "namespace Extracted\nopen CIL\n" + ExtractionRecursionDepth.Match(source).Value.TrimEnd('\r') + "\n\n"; // Split only the extractor's declaration boundaries. Lean still checks every imported // declaration and the aggregate certificate against the complete program. @@ -1067,7 +1017,7 @@ static ExtractionComponents SplitExtractionComponents(string source) if (methods.Count < 2 || aggregate <= methods[^1].Index) return new(ContentKey(source), source, new(StringComparer.Ordinal)); var localValidation = new Regex(@"(?m)^theorem method([0-9]+)_" + - @"(?:coverage|shape|unsafe|division|opaque|vector_initializations)_validated[^\r\n]*\r?\n"); + @"(?:coverage|shape|declared|unsafe|division|opaque|object_operands)_validated[^\r\n]*\r?\n"); var tail = source[aggregate..]; var validations = localValidation.Matches(tail).GroupBy(match => int.Parse(match.Groups[1].Value)) .ToDictionary(group => group.Key, group => string.Concat(group.Select(match => match.Value))); @@ -1096,8 +1046,9 @@ string Component(string text) var name = Component(string.Join("\n", imports.Select(module => "import " + module)) + "\n\n" + context + body + "\nend Extracted\n"); modules.Add(name); + // A decoded method is declared by `cil_decoded_method`, so later components that name it import it. foreach (Match declaration in Regex.Matches(body, - @"(?m)^(?:@\[[^\r\n]*\]\s+)?(?:def|theorem) ([A-Za-z_][A-Za-z0-9_]*)")) + @"(?m)^(?:@\[[^\r\n]*\]\s+)?(?:def|theorem|cil_decoded_method) ([A-Za-z_][A-Za-z0-9_]*)")) declarations.Add(declaration.Groups[1].Value, modules.Count - 1); } return new(ContentKey(source), string.Join("\n", modules.Select(module => "import " + module)) + @@ -1130,7 +1081,7 @@ static void RejectContractSourceShadow(string module, IEnumerable source // Source imports schedule work only; compiled import headers decide what can be shared. [GeneratedRegex(@"(? ConsumerSourceImports(string source) { @@ -1160,7 +1111,7 @@ static IEnumerable ConsumerSourceImports(string source) if (blockDepth > 0 || lineComment) text[index] = ' '; } } - return ConsumerImportCommand().Matches(new string(text)).Select(import => import.Groups[1].Value); + return ConsumerImportCommand.Matches(new string(text)).Select(import => import.Groups[1].Value); } static string[] ConsumerSupportCandidates(IEnumerable proofs, string[] sourceDirectories, @@ -1168,6 +1119,9 @@ static string[] ConsumerSupportCandidates(IEnumerable proofs, string[] s { var excluded = generatedModules.Concat(["Extracted", "ExtractedSymbols", "ExtractionAudit"]) .ToHashSet(StringComparer.Ordinal); + // Lake lists source directories with links followed, as macOS reaches /var through /private/var, while + // package roots are spelled as given: one location counts once, or every module there looks ambiguous. + sourceDirectories = [.. sourceDirectories.DistinctBy(ResolvedPath, LocationComparer)]; var imports = new Dictionary(StringComparer.Ordinal); var pending = new Queue(); foreach (var proof in proofs) @@ -1191,6 +1145,26 @@ static string[] ConsumerSupportCandidates(IEnumerable proofs, string[] s return imports.Keys.Where(module => !dependent.Contains(module)).Order(StringComparer.Ordinal).ToArray(); } + // Git marks objects read-only on Windows. The session owns each copy and removes it after replay. + static void CloneConsumerDependency(string source, string destination) + { + try { Run(["git", "clone", "--no-hardlinks", "--no-checkout", source, destination]); } + finally + { + string objects = Path.Combine(destination, ".git", "objects"); + if (OperatingSystem.IsWindows() && Directory.Exists(objects)) + { + var options = new EnumerationOptions + { + RecurseSubdirectories = true, + AttributesToSkip = FileAttributes.ReparsePoint + }; + foreach (FileInfo file in new DirectoryInfo(objects).EnumerateFiles("*", options)) + if (file.IsReadOnly) file.IsReadOnly = false; + } + } + } + ConsumerSession PrepareConsumerSession(string session, string root, ConsumerDependency[] dependencies, SortedDictionary snapshot, string certifiedLibrary, ExtractionIdentity extraction, IReadOnlyList<(string Module, string Proof, string Output)> proofs, string? symbols, @@ -1204,7 +1178,7 @@ ConsumerSession PrepareConsumerSession(string session, string root, ConsumerDepe Directory.CreateDirectory(Path.GetDirectoryName(destination)!); if (fullBundleDependencies is null) { - Run(["git", "clone", "--no-hardlinks", "--no-checkout", dependency.Directory, destination]); + CloneConsumerDependency(dependency.Directory, destination); Run(["git", "remote", "set-url", "origin", dependency.Url], destination); } else Directory.CreateDirectory(destination); @@ -1375,12 +1349,12 @@ sealed class ConsumerOutcome } Report VerifyConsumer(string assembly, string proof, string output, string theorem, string root, - string? settings, Action? stage) + string? settings, Action? stage, DeepCheckInputs? deepChecks = null, DeepCheckRun? deepRun = null) { ProofMessages = null; VerifiedStatement = null; var outcome = VerifyConsumerRequests(root, [new(assembly, output, proof, theorem, settings)], - (_, value) => stage?.Invoke(value), batch: false)[0]; + (_, value) => stage?.Invoke(value), batch: false, deepChecks: deepChecks, deepRun: deepRun)[0]; ProofMessages = outcome.ProofMessages(proof); VerifiedStatement = outcome.Statement; if (outcome.Located() is { } error) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(error); @@ -1400,6 +1374,31 @@ static SortedDictionary ConsumerArtifactSnapshot(string director .Where(path => Path.GetFileName(path).Contains(".olean", StringComparison.Ordinal)) .Select(path => (Path.GetRelativePath(directory, path).Replace('\\', '/'), path))); + /// Counts each compiled module's recorded parts in one pass. + static Dictionary ConsumerArtifactCounts(IEnumerable paths) + { + Dictionary counts = new(StringComparer.Ordinal); + foreach (var path in paths) + { + var module = ModuleName(path); + counts[module] = counts.GetValueOrDefault(module) + 1; + } + return counts; + } + + /// Consumer source roots cannot supply the verifier's binding namespaces. + static void RequireConsumerDeepNamespaces(IEnumerable sourceDirectories) + { + foreach (var directory in sourceDirectories) + foreach (var module in new[] { "StrictBinding", "BindingAudit" }) + { + var source = Path.Combine(directory, module); + if (File.Exists(source + ".lean") || (Directory.Exists(source) && + Directory.EnumerateFiles(source, "*.lean", SearchOption.AllDirectories).Any())) + throw new InvalidDataException("Consumer source uses a reserved deep-check namespace: " + source); + } + } + // The replay manifest binds exactly the parts consumed by Lean. static SortedDictionary ConsumerSnapshotParts( IReadOnlyDictionary artifacts, string modulePath, string path) @@ -1418,8 +1417,13 @@ static SortedDictionary ConsumerSnapshotParts( // `finished` reports each request once its outcome is final, possibly from parallel workspaces. ConsumerOutcome[] VerifyConsumerRequests(string root, IReadOnlyList requests, Action? stage, bool batch, int parallelism = 1, - Action? finished = null, bool checkOnly = false) + Action? finished = null, bool checkOnly = false, DeepCheckInputs? deepChecks = null, + DeepCheckRun? deepRun = null) { + deepRun = DeepCheckRun.Claim(deepChecks, deepRun); + if (deepChecks is not null && (batch || checkOnly || requests.Count != 1 || + requests[0].ExpectRejection || requests[0].ExportAudit is not null)) + throw new InvalidDataException("Deep checks require one ordinary consumer verification request."); var requestClock = System.Diagnostics.Stopwatch.StartNew(); root = Path.GetFullPath(root); if (requests.Any(request => request.ExportAudit is not null) && !freshProofs && !freshLibrary) @@ -1462,10 +1466,11 @@ void FailInternalErrors(int[] members, string[] names, string[] command, int cod Fail([members[k]], failure); } } - var comparer = OperatingSystem.IsWindows() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal; + var comparer = CollidingLocationComparer; var all = Enumerable.Range(0, requests.Count).ToArray(); var identities = new string[requests.Count]; var extractions = new ExtractionIdentity[requests.Count]; + IReadOnlyList? matchedPins = null; var proofKeys = new string[requests.Count]; // Symbols.lean hash for requests whose proofs import ExtractedSymbols, else null. var symbols = new string?[requests.Count]; @@ -1485,18 +1490,23 @@ SortedDictionary BoundRequestInputs(int index, SortedDictionary< return inputs; } foreach (var i in all) - try { proofs[i] = Within(root, Path.GetRelativePath(root, Path.GetFullPath(requests[i].Proof))); } + try { proofs[i] = Within(root, AbsolutePath(requests[i].Proof)); } catch (Exception error) { Fail([i], error); } - // Only the last request for an output may leave a certificate, as in sequential runs. - var last = all.GroupBy(i => outputs[i], comparer).ToDictionary(group => group.Key, group => group.Max(), comparer); - foreach (var output in last.Keys) File.Delete(Path.Combine(output, "verified.json")); + // Only the last request for an output may leave a certificate, as in sequential runs. Spellings that may name + // one output, through a link or in another case where case is ignored or unknown, can only lose one. + var locations = outputs.Select(ResolvedPath).ToArray(); + var last = all.GroupBy(i => locations[i], comparer) + .ToDictionary(group => group.Key, group => group.Max(), comparer); + foreach (var output in outputs.Distinct(StringComparer.Ordinal)) + File.Delete(Path.Combine(output, "verified.json")); var parallel = new ParallelOptions { MaxDegreeOfParallelism = Math.Max(1, parallelism) }; - void CheckExtractions(int[] members) + void CheckExtractions(int[] members, bool reportStage = true) { - Stage(members, "extraction integrity"); - Parallel.ForEach(Pending(members).GroupBy(i => outputs[i], comparer), parallel, group => + if (reportStage) Stage(members, "extraction integrity"); + // Grouped by exact resolved spelling, so every directory is checked whatever its volume's case rule. + Parallel.ForEach(Pending(members).GroupBy(i => locations[i], StringComparer.Ordinal), parallel, group => { - try { RunExtractor(["dotnet", ExtractorPath, "check", group.Key]); } + try { RunExtractor(["dotnet", ExtractorPath, "check", outputs[group.First()]]); } catch (Exception error) { Fail(group, error); } }); } @@ -1523,13 +1533,18 @@ SortedDictionary Common(SortedDictionary snapsho { snapshot = ConsumerSnapshot(root, excluded, fullBundleDependencies); var common = Common(snapshot); + // Each source is resolved once, so a batch costs one pass over the sources per request. + string[] consumerSources = batch + ? snapshot.Keys.Where(key => key.StartsWith("consumer:", StringComparison.Ordinal)) + .Select(key => ResolvedPath(key["consumer:".Length..])).ToArray() + : []; foreach (var i in live) try { - var output = outputs[i] + Path.DirectorySeparatorChar; - if (batch && snapshot.Keys.Any(key => key.StartsWith("consumer:" + output, OperatingSystem.IsWindows() - ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))) - throw new InvalidDataException("Batch outputs must lie outside the consumer sources, for example under artifacts/: " + outputs[i]); + if (consumerSources.Any(source => MayLieBelow(locations[i], source))) + throw new InvalidDataException( + "Batch outputs must lie outside the consumer sources, for example under artifacts/: " + + outputs[i]); before[i] = BoundRequestInputs(i, common); if (!before[i].ContainsKey("consumer:" + proofs[i])) throw new InvalidDataException("Proof must be a Lean source under the consumer root, outside excluded build/artifact directories."); @@ -1553,11 +1568,11 @@ SortedDictionary Common(SortedDictionary snapsho Fail(live, error); return outcomes; } - void Recheck(int[] members) + void Recheck(int[] members, bool reportStage = true) { var started = DateTimeOffset.UtcNow; var elapsed = System.Diagnostics.Stopwatch.StartNew(); - CheckExtractions(members); + CheckExtractions(members, reportStage); members = Pending(members); if (members.Length == 0) return; try @@ -1577,6 +1592,20 @@ void Recheck(int[] members) live = Pending(all); if (live.Length == 0) return outcomes; + if (deepRun is not null) + { + Stage(live, "arguments"); + try + { + extractions[0] = ExtractionIdentity.Read(outputs[0]); + matchedPins = deepRun.Begin(extractions[0]); + } + catch (Exception error) + { + Fail(live, error); + return outcomes; + } + } Stage(live, "Lean library build"); string certifiedLibrary; Dictionary certifiedBase; @@ -1585,6 +1614,11 @@ void Recheck(int[] members) try { Library(Inputs(proofs[live[0]], requests[live[0]].Settings)); + if (deepChecks is not null) + { + deepRun!.Attempt!.RequireIdentity(ExtractionIdentity.Read(outputs[0])); + PrepareBindingLibrary(this.session!); + } certifiedLibrary = library!; certifiedBase = baseModules!.Where(pair => ModuleName(pair.Key) == "CIL" || ModuleName(pair.Key).StartsWith("CIL.", StringComparison.Ordinal)).ToDictionary(); @@ -1604,7 +1638,7 @@ void Recheck(int[] members) { try { - extractions[i] = ExtractionIdentity.Read(outputs[i]); + extractions[i] ??= ExtractionIdentity.Read(outputs[i]); identities[i] = extractions[i].Sha256; // PDB names bind only proofs that import them; identity.json does not cover the PDB. // A result cached without a statement view must not satisfy a run that prints one. @@ -1612,14 +1646,16 @@ void Recheck(int[] members) (symbols[i] is null ? "" : "\n" + symbols[i]) + (viewsStatement ? "\nstatement-view" : "") + ProofOptionsKey + (requests[i].Statement is { } statement ? "\nstatement " + JsonSerializer.Serialize(statement) : "")); - if (checkOnly || requests[i].ExpectRejection || ReferenceOnlyImports(extractions[i])) continue; + if (deepChecks is not null || checkOnly || requests[i].ExpectRejection || + ReferenceOnlyImports(extractions[i])) continue; var receipt = Path.Combine(Path.GetTempPath(), "leancil-consumer-receipt-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(receipt); try { if (!RestoreSuccess(proofKeys[i], receipt)) continue; - var report = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(receipt, "report.json")))!; - if (report.schema != 4) continue; + // A receipt whose certificate this run does not accept is a miss, never a failure. + if (ReadCertificate(File.ReadAllText(Path.Combine(receipt, "report.json")), BindingMode.Default, + out _) is not { } report) continue; var cachedMessages = Path.Combine(receipt, ProofMessagesFile); outcomes[i].Messages = File.Exists(cachedMessages) ? JsonSerializer.Deserialize(File.ReadAllText(cachedMessages)) : null; @@ -1641,12 +1677,14 @@ void Recheck(int[] members) JsonElement current = extractions[i].Root; var report = JsonSerializer.Deserialize(outcomes[i].Detail!)! with { - schema = 4, identitySha256 = identities[i], + identitySha256 = identities[i], assemblySha256 = Sha(requests[i].Assembly), inputs = before[i], imports = PublishedImports(current), contractImports = ContractImportAssumptions(current), contractDependencies = DeploymentDependencies(current) }; outcomes[i].Detail = null; - if (last[outputs[i]] == i) File.WriteAllText(Path.Combine(outputs[i], "verified.json"), JsonSerializer.Serialize(report, Json) + "\n"); + if (last[locations[i]] == i) + File.WriteAllText(Path.Combine(outputs[i], "verified.json"), + JsonSerializer.Serialize(report, Json) + "\n"); outcomes[i].Report = report; CompleteTiming(i); CacheProgress("certified consumer proof: " + requests[i].Theorem); @@ -1704,6 +1742,19 @@ void Recheck(int[] members) void VerifyGroup(int[] members, string session, bool first, bool share) { + void RequireDeepBindingStable() + { + if (deepChecks is not null) RequirePreparedBindingLibrary(); + } + var bindingModules = deepChecks is null ? null : CertifiedModuleDirectory(bindingSession!); + (string[] Directories, string LeanPath) ImportDirectories() + { + var resolved = ConsumerImportDirectories(session); + if (bindingModules is null) return resolved; + RequireDeepBindingStable(); + return ([.. resolved.Directories, bindingModules], + resolved.LeanPath + Path.PathSeparator + bindingModules); + } var components = componentPlans[members[0]]; bool checkRejectedProofs = members.Any(index => requests[index].ExpectRejection); Dictionary bound = new(WithExtraction(sources, Sha(Path.Combine(outputs[members[0]], "Extracted.lean"))), StringComparer.Ordinal); @@ -1739,6 +1790,17 @@ void VerifyGroup(int[] members, string session, bool first, bool share) i => requestModules[i], i => requests[i].ExportAudit!.Source, StringComparer.Ordinal), generatedSources: generatedSources, components: components, checkRejectedProofs: checkRejectedProofs); var generatedPackage = prepared.Generated; + if (deepChecks is not null) + { + var frameworkCopy = Within(session, Path.GetRelativePath(root, + dependencies.Single(dependency => dependency.Name == "leancil").Source)); + var packageRoots = dependencies.Where(dependency => dependency.Name != "leancil") + .Select(dependency => Within(session, Path.GetRelativePath(root, dependency.Source))) + .Prepend(session); + // Lake lists source directories with links followed, so the session holds them by location. + RequireConsumerDeepNamespaces(ConsumerSourceDirectories(session, packageRoots) + .Where(directory => !SameLocation(directory, frameworkCopy) && Inside(session, directory))); + } foreach (var member in members) outcomes[member].Sources = prepared.Sources; var generatedInputPaths = prepared.Copied.Keys.Where(path => path.StartsWith(generatedPackage + Path.DirectorySeparatorChar, StringComparison.Ordinal)) @@ -1766,11 +1828,12 @@ void RequireInterfaceArtifacts() if (!first && relativeImportDirectories.Length > 0) initialImportDirectories = relativeImportDirectories.Select(path => Path.GetFullPath(path, session)).ToArray(); else if (!first) - initialImportDirectories = ConsumerImportDirectories(session).Directories; + initialImportDirectories = ImportDirectories().Directories; else { - initialImportDirectories = ConsumerImportDirectories(session).Directories; - relativeImportDirectories = initialImportDirectories.Select(path => Path.GetRelativePath(session, path)).ToArray(); + initialImportDirectories = ImportDirectories().Directories; + relativeImportDirectories = initialImportDirectories.Where(path => path != bindingModules) + .Select(path => Path.GetRelativePath(session, path)).ToArray(); // Lean sources are bound per module inside each generation, not by this key. var configuration = prepared.Copied.Where(pair => Path.GetFileName(pair.Key) is "lakefile.toml" or "lakefile.lean" or "lake-manifest.json" or "lean-toolchain") .OrderBy(pair => pair.Key, StringComparer.Ordinal) @@ -1780,6 +1843,7 @@ void RequireInterfaceArtifacts() supportArtifacts = RestoreConsumerSupport(supportKey, bound, support, dependent: false); } RestorePortableSupport(session, trusted); + RequireDeepBindingStable(); var sharedRecord = ReadConsumerSupportRecord(Path.Combine(support, ConsumerSupportRecordName)); // Initial persistent restoration already selected and authenticated this bound // closure; its copied artifacts do not include the support manifest. @@ -1796,6 +1860,7 @@ void RequireInterfaceArtifacts() File.Copy(Within(support, file), target, true); } if (restoredArtifacts.Count > 0) CacheProgress("certified consumer support library"); + RequireDeepBindingStable(); var certificateRestoration = new SortedDictionary(restoredArtifacts, StringComparer.Ordinal); // Not part of restoredArtifacts: these are rebuilt only if stale and always replayed. SortedDictionary RestoreCertificateSupport(string key, bool? dependent, @@ -1814,6 +1879,7 @@ SortedDictionary RestoreCertificateSupport(string key, bool? dep certificateRestoration[file] = dependentFiles[file]; } if (dependentFiles.Count > 0) CacheProgress("certified extraction-dependent consumer modules"); + RequireDeepBindingStable(); return dependentFiles; } finally { Directory.Delete(own, true); } @@ -1824,6 +1890,7 @@ SortedDictionary RestoreCertificateSupport(string key, bool? dep CheckGroup(members, session, names, bound, first, share); return; } + Dictionary? checkedClosures = deepChecks is null ? null : []; if (interfaceModules.Length > 0) { Stage(members, "contract interface validation"); @@ -1831,13 +1898,14 @@ SortedDictionary RestoreCertificateSupport(string key, bool? dep progress: "Compiling published contract interfaces"); // The audit, the replay and the proof must all read the bytes compiled here. acceptedInterfaceArtifacts = InterfaceArtifacts(); - string interfaceLeanPath = ConsumerImportDirectories(session).LeanPath; + string interfaceLeanPath = ImportDirectories().LeanPath; Stage(members, "kernel replay"); RequireInterfaceArtifacts(); + RequireDeepBindingStable(); string InterfaceArtifact(string module) => Path.Combine(generatedPackage, ".lake/build/lib/lean", module.Replace('.', '/') + ".olean"); if (!TryReplayContractInterfaces(extractions[members[0]], interfaceModules, interfaceLeanPath, - InterfaceArtifact)) + InterfaceArtifact, checkedClosures)) { Dictionary interfaceFailures = ReplayConsumerModules(session, interfaceModules, interfaceLeanPath); @@ -1847,7 +1915,7 @@ string InterfaceArtifact(string module) => string.Join("\n", interfaceFailures.Values)); RequireInterfaceArtifacts(); CheckContractInterfaces(extractions[members[0]], interfaceModules, interfaceLeanPath, - InterfaceArtifact); + InterfaceArtifact, checkedClosures: checkedClosures); } RequireInterfaceArtifacts(); } @@ -1977,6 +2045,7 @@ void RequireGeneratedArtifacts() string[] command = ["lake", "build", .. names.Select(name => "+" + name + ":olean")]; var build = Execute(command, session, progress: "Building fresh consumer proof and dependencies", internalErrors: false); + RequireDeepBindingStable(); RequireExtractionArtifacts(); RequireGeneratedArtifacts(); FailInternalErrors(members, names, command, build.Code, build.Output); @@ -1988,7 +2057,7 @@ void RequireGeneratedArtifacts() if (buildFailure is null || members.Length > 1) { if (members.Length == 1) Stage(members, "kernel replay"); - (importDirectories, leanPath) = ConsumerImportDirectories(session); + (importDirectories, leanPath) = ImportDirectories(); // Lake writes a module's olean and trace only after it checks successfully. built = names.Select(name => importDirectories.Any(directory => File.Exists(Path.Combine(directory, name + ".olean")) && File.Exists(Path.Combine(directory, name + ".trace")))).ToArray(); @@ -2050,15 +2119,58 @@ string ProofModule(int i) => importDirectories var supportUnchanged = restoredArtifacts.Count > 0 && restoredArtifacts.Where(pair => Path.GetFileName(pair.Key).Contains(".olean", StringComparison.Ordinal)).All(pair => File.Exists(Within(session, pair.Key)) && Sha(Within(session, pair.Key)) == pair.Value); - var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; var frameworkModules = CertifiedModuleDirectory(certifiedLibrary); var generatedModules = Path.GetFullPath(Path.Combine(generatedPackage, ".lake/build/lib/lean")); // No external commands run between this snapshot and the replay manifest. // Recorded base modules take their hashes from the producer's manifest; unrecorded ones are replayed. SortedDictionary DirectoryArtifacts(string directory) => - directory.Equals(frameworkModules, comparison) ? BaseModuleArtifacts() : ConsumerArtifactSnapshot(directory); + LocationComparer.Equals(directory, frameworkModules) ? BaseModuleArtifacts() + : ConsumerArtifactSnapshot(directory); var artifactSnapshots = importDirectories.Where(Directory.Exists) .Select(directory => (Directory: directory, Artifacts: DirectoryArtifacts(directory))).ToArray(); + DeepSessionContribution? deep = null; + if (deepChecks is not null) + { + foreach (var (_, artifacts) in artifactSnapshots) + if (artifacts.Keys.Any(path => ModuleName(path) is "BindingAudit" || + ModuleName(path).StartsWith("BindingAudit.", StringComparison.Ordinal))) + throw new InvalidDataException("Consumer module uses the reserved BindingAudit namespace."); + var extractedPaths = artifactSnapshots.SelectMany(snapshot => snapshot.Artifacts.Keys + .Where(path => path == "Extracted.olean") + .Select(path => Path.Combine(snapshot.Directory, path))).ToArray(); + if (extractedPaths.Length != 1 || !LocationComparer.Equals(extractedPaths[0], + Path.Combine(generatedModules, "Extracted.olean"))) + throw new InvalidDataException("Deep checks require the consumer's generated Extracted module."); + if (Directory.Exists(Path.Combine(session, "BindingAudit")) || + File.Exists(Path.Combine(session, "BindingAudit.lean"))) + throw new InvalidDataException("Consumer source uses the reserved BindingAudit namespace."); + SortedDictionary PhysicalArtifacts() => new(artifactSnapshots + .SelectMany(snapshot => DirectoryArtifacts(snapshot.Directory).Select(pair => + KeyValuePair.Create(Path.GetFullPath(Path.Combine(snapshot.Directory, pair.Key)), pair.Value))) + .ToDictionary(), StringComparer.Ordinal); + var priorArtifacts = PhysicalArtifacts(); + Stage(proceeding, "deep checks"); + RequireDeepBindingStable(); + RequireOnlyAdded(priorArtifacts, PhysicalArtifacts()); + var deepRoot = Path.Combine(session, ".deep-checks"); + Directory.CreateDirectory(deepRoot); + leanPath += Path.PathSeparator + deepRoot; + deep = BuildDeepSession(deepRoot, leanPath, extractedPaths[0], + extractions[members[0]], matchedPins!, () => Stage(proceeding, "deep-check sources"), + ConsumerDeepEnvironment(), checkedClosures); + if (deep.Restored) + { + deepCacheHits++; + CacheProgress("certified consumer deep checks: " + requests[members[0]].Theorem); + } + importDirectories = [.. importDirectories, deepRoot]; + artifactSnapshots = [.. artifactSnapshots.Select(snapshot => + (snapshot.Directory, Artifacts: DirectoryArtifacts(snapshot.Directory))), + (deepRoot, ConsumerArtifactSnapshot(deepRoot))]; + RequireOnlyAdded(priorArtifacts, PhysicalArtifacts(), [.. deep.Artifacts + .Where(pair => pair.Key != "Extracted").Select(pair => Path.ChangeExtension(pair.Value, null)!) ]); + RequireDeepBindingStable(); + } // The module parts on the import path now, in the shape of the replay manifest. Dictionary> ImportArtifactParts() => importDirectories .Where(Directory.Exists).SelectMany(directory => @@ -2071,7 +2183,11 @@ Dictionary> ImportArtifactParts() => im }); }) .OrderBy(pair => pair.Name, StringComparer.Ordinal).ToDictionary(pair => pair.Name, pair => pair.Parts); - var baseUnchanged = importDirectories.Any(path => path.Equals(frameworkModules, comparison)); + var baseUnchanged = importDirectories.Any(path => LocationComparer.Equals(path, frameworkModules)); + var certifiedCounts = ConsumerArtifactCounts(certifiedBase.Keys); + var interfaceCounts = ConsumerArtifactCounts(acceptedInterfaceArtifacts.Keys); + var preparedArtifacts = bindingPreparation?.SessionArtifacts; + var preparedCounts = ConsumerArtifactCounts(preparedArtifacts?.Keys ?? Enumerable.Empty()); var moduleNames = new HashSet(StringComparer.Ordinal); var discovered = new List<(string Name, bool Certified)>(); foreach (var (directory, artifacts) in artifactSnapshots) @@ -2081,9 +2197,19 @@ Dictionary> ImportArtifactParts() => im if (!moduleNames.Add(module.Key)) throw new InvalidDataException("Ambiguous consumer module: " + module.Key); if (File.Exists(Path.Combine(Toolchain, "lib/lean", module.Key.Replace('.', '/') + ".olean"))) throw new InvalidDataException("Consumer module shadows the trusted Lean toolchain: " + module.Key); - var unchanged = directory.Equals(frameworkModules, comparison) && - module.Count() == certifiedBase.Count(pair => ModuleName(pair.Key) == module.Key) && + var unchanged = LocationComparer.Equals(directory, frameworkModules) && + module.Count() == certifiedCounts.GetValueOrDefault(module.Key) && module.All(pair => certifiedBase.TryGetValue(pair.Key, out var hash) && hash == pair.Value); + bool preparedBinding = bindingModules is not null && + LocationComparer.Equals(directory, bindingModules) && + module.Count() == preparedCounts.GetValueOrDefault(module.Key) && + module.All(pair => preparedArtifacts!.TryGetValue(pair.Key, out var hash) && + hash == pair.Value); + if (deep is not null && !preparedBinding && + (module.Key == "StrictBinding" || + module.Key.StartsWith("StrictBinding.", StringComparison.Ordinal))) + throw new InvalidDataException( + "Consumer module shadows the prepared StrictBinding library: " + module.Key); if (!unchanged && (module.Key == "CIL" || module.Key.StartsWith("CIL.", StringComparison.Ordinal))) baseUnchanged = false; var certifiedSupport = supportUnchanged && module.All(pair => @@ -2091,11 +2217,13 @@ Dictionary> ImportArtifactParts() => im var relative = Path.GetRelativePath(session, Path.Combine(directory, pair.Key)); return restoredArtifacts.TryGetValue(relative.Replace('\\', '/'), out var hash) && hash == pair.Value; }); - bool publishedInterface = directory.Equals(generatedModules, comparison) && interfaceModuleNames.Contains(module.Key); - if (publishedInterface && (module.Count() != acceptedInterfaceArtifacts.Count(pair => ModuleName(pair.Key) == module.Key) || + bool publishedInterface = LocationComparer.Equals(directory, generatedModules) && + interfaceModuleNames.Contains(module.Key); + if (publishedInterface && (module.Count() != interfaceCounts.GetValueOrDefault(module.Key) || module.Any(pair => !acceptedInterfaceArtifacts.TryGetValue(pair.Key, out string? accepted) || accepted != pair.Value))) throw new InvalidDataException("Published contract interface artifacts changed after their closure audit: " + module.Key); - discovered.Add((module.Key, unchanged || certifiedSupport || publishedInterface)); + discovered.Add((module.Key, + unchanged || preparedBinding || certifiedSupport || publishedInterface)); } } if (!moduleNames.Contains("Extracted") || (checkRejectedProofs && !moduleNames.Contains("ExtractionAudit")) || @@ -2110,15 +2238,50 @@ Dictionary> ImportArtifactParts() => im .ToDictionary(pair => pair.Name, pair => pair.Path, StringComparer.Ordinal); var checkedArtifacts = JsonSerializer.Serialize(mainArtifacts.OrderBy(pair => pair.Name, StringComparer.Ordinal) .ToDictionary(pair => pair.Name, pair => pair.Parts)); - var portableSupport = ImportedConsumerSupport(session, trusted, currentArtifacts); + var checkedPhysicalArtifacts = deep is null ? null : BindingArtifactHashes(currentArtifacts.Values); + void RequireDeepStable() + { + if (deep is null) return; + deep.RequireStable(); + RequireDeepBindingStable(); + RequireExtractionArtifacts(); + RequireGeneratedArtifacts(); + RequireInterfaceArtifacts(); + if (!Same(checkedPhysicalArtifacts!, BindingArtifactHashes(currentArtifacts.Values)) || + checkedArtifacts != JsonSerializer.Serialize(ImportArtifactParts()) || + prepared.Copied.Any(pair => !File.Exists(pair.Key) || Sha(pair.Key) != pair.Value)) + throw new InvalidDataException("Deep-check consumer sources or compiled artifacts changed."); + } + void RequireDiagnosticInputs() + { + if (auditExecutable is not null) CheckedAuditExecutable(); + Recheck(proceeding, reportStage: false); + foreach (var index in proceeding) + { + if (!Live(index)) throw outcomes[index].Error!; + deepRun?.Attempt?.RequireIdentity(extractions[index]); + } + RequireCertifiedLibraryContents(); + RequireDeepStable(); + } + var portableSupport = ImportedConsumerSupport(session, trusted, currentArtifacts, + publishReceipts: deepChecks is null); var graph = members.Length > 1 || share || UseSuccessCache || exporting || restoredArtifacts.Count > 0 || symbolHash is not null ? ConsumerModuleImports(session, currentArtifacts) : null; // Cached support was checked against another Extracted or PDB, so its importers are always replayed. var extractionDependent = graph is null ? new HashSet(StringComparer.Ordinal) : DependentModules(graph, ["Extracted", "ExtractedSymbols"]); - var replayTargets = discovered.Where(module => - !baseUnchanged || extractionDependent.Contains(module.Name) || - (!module.Certified && !portableSupport.Contains(module.Name))).Select(module => module.Name).ToArray(); + // A restored deep result was replayed against these Extracted bytes and certified library, which a + // package shadowing a CIL module would no longer import, so it is replayed again then. + HashSet restoredDeep = deep is { Restored: true } && baseUnchanged + ? [.. deep.Modules.Where(module => module != "Extracted")] : []; + var replayTargets = discovered.Where(module => !restoredDeep.Contains(module.Name) && + (!baseUnchanged || extractionDependent.Contains(module.Name) || + (!module.Certified && !portableSupport.Contains(module.Name)))) + .Select(module => module.Name).ToArray(); + if (deep is not null) + replayTargets = [.. replayTargets.Concat(deep.Modules.Where(module => !restoredDeep.Contains(module))) + .Distinct(StringComparer.Ordinal)]; Dictionary? combinedAudits = null; bool jointReplayRejected = false; bool jointReplaySucceeded = false; @@ -2129,7 +2292,14 @@ Dictionary> ImportArtifactParts() => im bool completeConsumerReplay = discovered.All(module => module.Name == "CIL" || module.Name.StartsWith("CIL.", StringComparison.Ordinal) || replayedNames.Contains(module.Name)); string[] AuditModules(int index) => [requestModules[index], "Extracted", - .. HasContractImports(extractions[index]) ? ["ExtractedImports"] : Array.Empty()]; + .. HasContractImports(extractions[index]) ? ["ExtractedImports"] : Array.Empty(), + .. deep?.Modules.Where(module => module != "Extracted") ?? []]; + AuditRequest[] BindingRequestsForConsumer() => deep is null + ? [ExtractionCertificate("Extracted.translation_valid")] + : [.. deep.Requests.Select(request => request.Audit)]; + if (deep is not null && deep.Requests.Any(request => request.Audit.name == requests[members[0]].Theorem)) + throw new InvalidDataException("Proof theorem collides with a deep-check request: " + + requests[members[0]].Theorem); AuditRequest Statemented(int index, AuditRequest audit) => requests[index].Statement?.Apply(audit) ?? audit; AuditRequest ScopedProof(int index) @@ -2143,6 +2313,7 @@ AuditRequest ScopedProof(int index) } static bool RetainScopedAudit(string? error) => error is null || error.StartsWith( "Theorem dependency is outside its request's import closure:", StringComparison.Ordinal); + RequireDeepStable(); if (replayTargets.Length > 0 && fullImportAudit is null && (members.Length == 1 || completeConsumerReplay) && proceeding.All(index => (requests[index].ExportAudit is null || members.Length == 1) && @@ -2155,10 +2326,10 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit var proofAudits = exportAudit?.Theorems ?? proceeding.Select(ScopedProof).ToArray(); var audited = AuditOleans(leanPath, proceeding.SelectMany(AuditModules).Distinct(StringComparer.Ordinal).ToArray(), - [.. proofAudits, ExtractionCertificate("Extracted.translation_valid")], + [.. proofAudits, .. BindingRequestsForConsumer()], premise, closures: exportAudit?.Closures, replayTargets: replayTargets); jointReplaySucceeded = true; - combinedAudits = exportAudit is not null + combinedAudits = exportAudit is not null || deep is not null ? new Dictionary { [proceeding[0]] = audited } : proceeding.Select((index, position) => (index, position)) // A conservative owner scope may omit a transitive toolchain import. @@ -2179,10 +2350,12 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit HashSet? RequestClosure(int index) => graph is null ? null : ImportClosure(graph, [requestModules[index], "Extracted", .. checkRejectedProofs ? new[] { "ExtractionAudit" } : [], - .. HasContractImports(extractions[index]) ? ["ExtractedImports"] : Array.Empty()]); + .. HasContractImports(extractions[index]) ? ["ExtractedImports"] : Array.Empty(), + .. deep?.Modules ?? []]); var failures = new Dictionary(); if (!jointReplaySucceeded) { + if (deep is not null) RequireDiagnosticInputs(); if (members.Length == 1) // The failed fused audit already tried this exact joint target set. failures = jointReplayRejected @@ -2224,6 +2397,19 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit foreach (var (module, error) in own) failures.TryAdd(module, error); } } + if (deep is not null && failures.Count > 0) + { + Stage(proceeding, "replay diagnostics"); + RequireDiagnosticInputs(); + Dictionary? rejections = null; + try + { + rejections = DiagnoseReplayFailures(session, deep.OwnedFailures(failures.Keys), leanPath); + } + catch (Exception) { } + RequireDiagnosticInputs(); + if (rejections is not null) deep.RejectReplay(rejections, deepRun?.Attempt); + } foreach (var i in proceeding) { // A request depends only on its own proof's import closure and the extraction certificate. @@ -2236,7 +2422,9 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit proceeding = Pending(proceeding); if (proceeding.Length == 0) return; // Audit the certificate and proof in one import environment containing only replayed oleans. - var audits = new Dictionary(); + var audits = new Dictionary(); string certificateText = ""; foreach (var i in proceeding) { @@ -2244,20 +2432,39 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit { Stage([i], "extraction certificate audit"); var module = requestModules[i]; - string[] auditModules = [module, "Extracted", - .. HasContractImports(extractions[i]) ? ["ExtractedImports"] : Array.Empty()]; + string[] auditModules = AuditModules(i); AuditRequest[] proofAudits = requests[i].ExportAudit?.Theorems ?? [Statemented(i, ProofTheorem(requests[i].Theorem, extractions[i]))]; var audited = combinedAudits?.GetValueOrDefault(i) ?? AuditOleans(leanPath, auditModules, - [.. proofAudits, ExtractionCertificate("Extracted.translation_valid")], ImportPremise(extractions[i]), + [.. proofAudits, .. BindingRequestsForConsumer()], ImportPremise(extractions[i]), closures: requests[i].ExportAudit?.Closures); foreach (string audit in auditModules) RequireAuditedModule(audited, audit, currentArtifacts[audit]); - AuditedAxioms(audited.theorems[^1]); - certificateText = AuditText(audited.theorems[^1]); + var namedAudits = audited.theorems.ToDictionary(result => result.name, StringComparer.Ordinal); + var expectedNames = proofAudits.Concat(BindingRequestsForConsumer()) + .Select(request => request.name).ToHashSet(StringComparer.Ordinal); + if (!expectedNames.SetEquals(namedAudits.Keys)) + throw new InvalidDataException("Consumer audit returned an unexpected theorem set."); + var certificateAudit = namedAudits["Extracted.translation_valid"]; + AuditedAxioms(certificateAudit); + certificateText = AuditText(certificateAudit); Stage([i], "theorem audit"); RequireAuditedConstants(audited); + deep?.RequireAudited(audited); + deepRun?.Attempt?.RequireIdentity(extractions[i]); + if (deep is not null) + { + Recheck([i], reportStage: false); + if (!Live(i)) throw outcomes[i].Error!; + RequireCertifiedLibraryContents(); + } + RequireDeepStable(); + deep?.RequireTheorems(audited, [.. proofAudits, .. BindingRequestsForConsumer()], + deepRun?.Attempt); foreach (AuditedTheorem checkedTheorem in audited.theorems) AuditedAxioms(checkedTheorem); - audits[i] = (AuditText(audited.theorems[0]), AuditedAxioms(audited.theorems[0]), audited); + var proofAudit = namedAudits[proofAudits[0].name]; + var refutation = ExtractedRefutation(proofAudit, extractions[i].Root, + required: requests[i].Statement?.Refuted == true && proofAudit.matches == true); + audits[i] = (AuditText(proofAudit), AuditedAxioms(proofAudit), audited, proofAudit, refutation); } catch (Exception error) { Fail([i], error); } } @@ -2271,6 +2478,7 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit throw new InvalidOperationException("Sources changed in the fresh consumer build"); if (checkedArtifacts != JsonSerializer.Serialize(ImportArtifactParts())) throw new InvalidOperationException("Compiled artifacts changed during consumer verification"); + RequireDeepStable(); bool contractExportAudit = proceeding.Any(i => requests[i].ExportAudit is not null); foreach (int i in proceeding) requests[i].ExportAudit?.Checked(session, requestModules[i], leanPath, audits[i].Result); @@ -2278,6 +2486,7 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit foreach (int i in proceeding) if (fullImportAudit is not null) fullResults.Add(i, fullImportAudit(outputs[i], session, requestModules[i], leanPath)); + RequireDeepStable(); if (contractExportAudit || fullImportAudit is not null) { // Only callbacks can run further verification code after the first recheck. @@ -2293,13 +2502,20 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit } // Publish only after the complete declaration replay, exact theorem // audit, extraction recheck and source-stability checks succeeded. - if (failures.Count == 0 && graph is not null) + void PublishSupport() { + if (failures.Count != 0 || graph is null) return; + RequireDeepStable(); var dependent = DependentModules(graph, ["Extracted", "ExtractedSymbols", "ExtractionAudit", .. names, .. components.Sources.Keys]); // Extraction-dependent modules are recorded too, bound to this Extracted; export uses only the others. // Records bind no PDB names, so importers of ExtractedSymbols stay unbound and are never cached. var excluded = DependentModules(graph, names); + if (deep is not null) + { + excluded.UnionWith(deep.Modules.Where(module => module != "Extracted")); + excluded.UnionWith(preparedCounts.Keys); + } excluded.Add("ExtractedSymbols"); excluded.UnionWith(members.SelectMany(member => selections[member]).Where(SelectionModules.IsSymbols)); var collected = UseSuccessCache || share || exporting @@ -2307,15 +2523,35 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit try { if (exporting) + { + RequireDeepStable(); + void RequireNormalizedSupportStable() + { + Stage(proceeding, "support normalization"); + Recheck(proceeding); + foreach (var member in proceeding) + if (!Live(member)) + throw outcomes[member].Error ?? new InvalidOperationException( + "Consumer request ended during support normalization."); + RequireDeepStable(); + } AddExportedSupport(export, session, root, before[proceeding[0]], - currentArtifacts.Where(pair => !dependent.Contains(pair.Key)).ToDictionary(), graph, collected); + currentArtifacts.Where(pair => !dependent.Contains(pair.Key) && + (deep is null || !excluded.Contains(pair.Key))).ToDictionary(), graph, collected, + publishReceipts: deep is null, + requireStable: deep is null ? null : RequireNormalizedSupportStable); + } if (collected is not null) { if (UseSuccessCache && Same(generatedInputs, GeneratedInputs())) + { + RequireDeepStable(); PublishConsumerCertificateSupport(certificateSupportKey, collected, certificateModules.ToHashSet(StringComparer.Ordinal)); + } if (supportKey.Length > 0) { + RequireDeepStable(); PublishSuccess(supportKey + "-" + Sha(Path.Combine(collected, ConsumerSupportRecordName)), collected, supportKey); if (UseSuccessCache) EvictStaleSupport(); } @@ -2334,6 +2570,7 @@ static bool RetainScopedAudit(string? error) => error is null || error.StartsWit } finally { if (collected is not null && Directory.Exists(collected)) Directory.Delete(collected, true); } } + if (deep is null) PublishSupport(); // Read from the audited proof module after every check above; the receipt caches it with the result. void ShowConsumerProof(int i, string receipt) { @@ -2350,8 +2587,13 @@ void ShowConsumerProof(int i, string receipt) ? assumptions.Deserialize() : []; RequireImplementationImports(extractions[i]); fullResults.TryGetValue(i, out FullImportResult? fullResult); - var report = new Report(4, Sha(requests[i].Assembly), identities[i], requests[i].Theorem, audits[i].Axioms, - audits[i].Audit, before[i], libraryKey, "consumer-" + libraryBuild, extractionAudit, initializedTypes, + var report = new Report(CertificateSchema, Sha(requests[i].Assembly), identities[i], + requests[i].Theorem, audits[i].Axioms, audits[i].Audit, before[i], + deepChecks is null ? BindingSection.Default : DeepBindingSection(deepChecks, matchedPins!, + deep!.Requests, new(bindingSourcesKey!, + File.ReadAllText(Path.Combine(bindingSession!, "lean-toolchain")).Trim(), + Sha(LeanExecutable)), audits[i].Result), + libraryKey, "consumer-" + libraryBuild, extractionAudit, initializedTypes, consumerSupportSha256: baseUnchanged && portableSupport.Count > 0 ? Environment.GetEnvironmentVariable("LEANCIL_CONSUMER_SUPPORT_SHA256") : null, staticValues: StaticValueAssumptions(extraction), @@ -2361,13 +2603,33 @@ void ShowConsumerProof(int i, string receipt) contractDependencies: fullResult?.Dependencies ?? DeploymentDependencies(extraction), compositionTheorem: fullResult?.Theorem, compositionAudit: fullResult?.Audit, initializationAssumptions: InitializationAssumptions(extraction), - statementIdentity: requests[i].Statement is null ? null : Identity(audits[i].Result.theorems[0])); + statementIdentity: requests[i].Statement is null ? null : Identity(audits[i].Proof), + refutation: audits[i].Refutation); var receipt = Path.Combine(session, "certified-result-" + i); Directory.CreateDirectory(receipt); File.WriteAllText(Path.Combine(receipt, "report.json"), JsonSerializer.Serialize(report, Json)); ShowConsumerProof(i, receipt); - PublishSuccess(proofKeys[i], receipt); - if (last[outputs[i]] == i) File.WriteAllText(Path.Combine(outputs[i], "verified.json"), JsonSerializer.Serialize(report, Json) + "\n"); + if (deep is not null) + { + Recheck([i]); + if (!Live(i)) continue; + RequireDeepStable(); + var deepStaging = Path.Combine(Path.GetTempPath(), "leancil-deep-" + Guid.NewGuid().ToString("N")); + try + { + // Only a result replayed against the certified CIL modules may be restored without replay. + var deepPublication = baseUnchanged ? StageDeepResult(deep, deepStaging) : null; + Stage([i], "deep result publication"); + PublishSupport(); + RequireDeepStable(); + if (deepPublication is not null) PublishSuccess(deep.ResultKey!, deepPublication); + } + finally { if (Directory.Exists(deepStaging)) Directory.Delete(deepStaging, true); } + } + if (deep is null) PublishSuccess(proofKeys[i], receipt); + if (last[locations[i]] == i) + File.WriteAllText(Path.Combine(outputs[i], "verified.json"), + JsonSerializer.Serialize(report, Json) + "\n"); outcomes[i].Report = report; CompleteTiming(i); } @@ -2412,9 +2674,7 @@ void CheckGroup(int[] members, string session, string[] names, } } if (!share) return; - var modules = directories.Where(directory => Path.GetFullPath(directory).StartsWith( - session + Path.DirectorySeparatorChar, OperatingSystem.IsWindows() - ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)) + var modules = directories.Where(directory => ResolvedRelativePath(session, directory) is not (null or ".")) .Where(Directory.Exists).SelectMany(directory => Directory.EnumerateFiles( directory, "*.olean", SearchOption.AllDirectories).Select(path => (Name: ModuleName(Path.GetRelativePath(directory, path)), Path: path))) diff --git a/scripts/ContractExport.cs b/scripts/ContractExport.cs index b0185ff22..102e19993 100644 --- a/scripts/ContractExport.cs +++ b/scripts/ContractExport.cs @@ -51,7 +51,8 @@ internal static void ExportContracts(string[] args) } ContractExportManifest manifest = ContractExportManifest.Read(manifestPath); byte[] implementation = File.ReadAllBytes(manifest.Assembly); - using ModuleDefinition module = ModuleDefinition.ReadModule(new MemoryStream(implementation)); + using PinnedAssemblies pinned = new(implementation, ContractInterface.Hash(implementation), []); + ModuleDefinition module = pinned.Selected; output ??= Path.Combine(Path.GetDirectoryName(manifest.Assembly)!, module.Assembly.Name.Name + SidecarExtension); if (!output.EndsWith(SidecarExtension, StringComparison.Ordinal)) @@ -275,11 +276,9 @@ static IEnumerable FrameworkBundleSources() => static string? SnapshotSource(SortedDictionary snapshot, string path) { - StringComparison comparison = - OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; path = Path.GetFullPath(path); return snapshot.Where(pair => pair.Key.StartsWith(ConsumerKey, StringComparison.Ordinal) && - Path.GetFullPath(pair.Key[ConsumerKey.Length..]).Equals(path, comparison)) + LocationComparer.Equals(Path.GetFullPath(pair.Key[ConsumerKey.Length..]), path)) .Select(pair => pair.Value).FirstOrDefault(); } @@ -378,11 +377,10 @@ static string ExportProof(ContractExportManifest manifest, ContractExportRequest if (settings.schema != 1 || string.IsNullOrWhiteSpace(settings.proof)) throw new InvalidDataException("Invalid export verification settings."); if (settings.proofRoot is not null && - Path.GetFullPath(settings.proofRoot, request.Artifact) != manifest.ProofRoot) + !SameLocation(Path.GetFullPath(settings.proofRoot, request.Artifact), manifest.ProofRoot)) throw new InvalidDataException("Export artifact proofRoot differs from the producer manifest."); - string proof = Path.GetFullPath(settings.proof, request.Artifact); - Within(manifest.ProofRoot, Path.GetRelativePath(manifest.ProofRoot, proof)); - return proof; + // Spelled under the proof root, as the source snapshot names it. + return Within(manifest.ProofRoot, Path.GetFullPath(settings.proof, request.Artifact)); } static InterfaceModule[] MergeContractInterfaces(InterfaceModule[] own, IEnumerable imported) diff --git a/scripts/CoreExamples.cs b/scripts/CoreExamples.cs index d9e8b5dad..bfc2dd62e 100644 --- a/scripts/CoreExamples.cs +++ b/scripts/CoreExamples.cs @@ -5,8 +5,14 @@ partial class Program { ["Add"] = "System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64)", ["AddModel"] = "System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64)", + ["DecodedAdd"] = "System.UInt64 Examples.Arithmetic::Add(System.UInt64,System.UInt64)", ["Choose"] = "System.UInt64 Examples.Arithmetic::Choose(System.Boolean,System.UInt64,System.UInt64)", ["Calls"] = "System.UInt64 Examples.Arithmetic::Calls(System.UInt64)", + ["CallsSummary"] = "System.UInt64 Examples.Arithmetic::Calls(System.UInt64)", + ["CountCallsSummary"] = "System.UInt32 Examples.Loops::CountCalls(System.UInt32)", + ["IdentitySummary"] = "System.UInt64 Examples.GenericMethods::Identity64(System.UInt64)", + ["RepeatedSummary"] = "System.UInt64 Examples.GenericMethods::Repeated(System.UInt64)", + ["VectorSummary"] = "System.UInt64 Examples.DivisionIntrinsics::UnsafeLowNext(System.UInt64)", ["Alias"] = "System.UInt64 Examples.Arithmetic::Alias(System.UInt64&,System.UInt64&)", ["Struct"] = "System.UInt64 Examples.Arithmetic::Struct(System.UInt64)", ["StructCopy"] = "System.UInt64 Examples.Arithmetic::StructCopy(System.UInt64)", @@ -16,22 +22,36 @@ partial class Program ["Escape"] = "System.UInt64& Examples.Arithmetic::Escape()" }; + // The theorem of each core example that does not prove ExampleProof.correctness. + static readonly Dictionary CoreExampleTheorems = new() + { + ["DecodedAdd"] = "ExampleProof.correctness_decoded", ["WrongAdd"] = "ExampleProof.refuted" + }; + // Each output retains its own checked extraction, proof identity and certificate. void VerifyCoreExamples(string outputRoot, Action passed, bool separate = false) { var assembly = At("Examples/Assembly/bin/Examples.dll"); var requests = CoreExampleSignatures.Select(pair => new GeneratedVerification( pair.Key == "Add" ? "Arithmetic::Add(ulong,ulong)" : pair.Value, - At($"Examples/Proofs/{pair.Key}.lean"), Path.Combine(outputRoot, pair.Key))).ToArray(); + At($"Examples/Proofs/{pair.Key}.lean"), Path.Combine(outputRoot, pair.Key), + CoreExampleTheorems.GetValueOrDefault(pair.Key, "ExampleProof.correctness"))).ToArray(); if (separate) foreach (var request in requests) - Verify(assembly, "Examples", request.Entry, request.Proof, request.Output); + Verify(assembly, "Examples", request.Entry, request.Proof, request.Output, theorem: request.Theorem); else VerifyGeneratedBatch(assembly, "Examples", requests); foreach (var request in requests) { var name = Path.GetFileName(request.Output); - Verify(assembly, "Examples", CoreExampleSignatures[name], request.Proof, request.Output, check: true); + Verify(assembly, "Examples", CoreExampleSignatures[name], request.Proof, request.Output, + theorem: request.Theorem, check: true); passed(name + " kernel proof and artifact identity"); } + // Add decodes in full, so its extraction defines the method from its decoder and writes no literal for it. + var add = File.ReadAllText(Path.Combine(outputRoot, "Add", "Extracted.lean")); + if (!add.Contains("\ncil_decoded_method method0 := ", StringComparison.Ordinal) || + add.Contains("\ndef method0 : Method", StringComparison.Ordinal)) + throw new InvalidOperationException("The extraction of Add writes a method literal."); + passed("Add defined from its decoder with no method literal"); } } diff --git a/scripts/DeepCheckInputs.cs b/scripts/DeepCheckInputs.cs new file mode 100644 index 000000000..26483598d --- /dev/null +++ b/scripts/DeepCheckInputs.cs @@ -0,0 +1,499 @@ +using System.Buffers.Binary; +using System.Collections.ObjectModel; +using System.Reflection; +using System.Reflection.Metadata; +using System.Reflection.Metadata.Ecma335; +using System.Reflection.PortableExecutable; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; + +/// +/// A SHA-256 digest held as its 32 bytes and compared by value; its lowercase hexadecimal text is for reports and +/// generated names only. +/// +internal readonly record struct Sha256Digest +{ + /// The bytes of a SHA-256 digest. + internal const int Length = 32; + + const int HalfLength = Length / 2; + readonly UInt128 high; + readonly UInt128 low; + + Sha256Digest(ReadOnlySpan digest) + { + high = BinaryPrimitives.ReadUInt128BigEndian(digest); + low = BinaryPrimitives.ReadUInt128BigEndian(digest[HalfLength..]); + } + + /// The digest of . + internal static Sha256Digest Of(ReadOnlySpan data) + { + Span digest = stackalloc byte[Length]; + SHA256.HashData(data, digest); + return new(digest); + } + + /// The digest 64 hexadecimal digits of either case spell; anything else names no digest. + internal static Sha256Digest Parse(ReadOnlySpan text, string source) + { + if (text.Length != 2 * Length || text.ContainsAnyExcept("0123456789abcdefABCDEF")) + throw new ArgumentException($"Invalid SHA-256 digest in {source}."); + return new(Convert.FromHexString(text)); + } + + /// Writes the digest's 32 bytes, most significant first. + internal void CopyTo(Span destination) + { + BinaryPrimitives.WriteUInt128BigEndian(destination, high); + BinaryPrimitives.WriteUInt128BigEndian(destination[HalfLength..Length], low); + } + + /// The 64 lowercase hexadecimal digits of the digest. + public override string ToString() + { + Span digest = stackalloc byte[Length]; + CopyTo(digest); + return Convert.ToHexStringLower(digest); + } +} + +/// Consumer-stated image bytes and digest-indexed extraction pins; these are inputs, not evidence. +internal sealed class DeepCheckInputs +{ + /// A file snapshot whose bytes have the consumer-stated SHA-256 digest. + internal sealed class Image + { + readonly byte[] bytes; + + internal Image(string path, Sha256Digest digest, byte[] bytes) + { + Path = path; + Digest = digest; + this.bytes = bytes; + } + + internal string Path { get; } + internal Sha256Digest Digest { get; } + internal ReadOnlySpan Bytes => bytes; + internal PEReader OpenPe() => new(new MemoryStream(bytes, writable: false)); + } + + /// An extraction pin matched by digest; its display identity does not select the image. + internal sealed record Pin(string Identity, Image Image, bool Platform = false, + bool? ObjectCoverage = null, bool? TupleCoverage = null, bool? KeyCoverage = null, + bool Reference = false); + + readonly Dictionary byDigest; + + DeepCheckInputs(string path, Sha256Digest digest, List images, Dictionary byDigest) + { + Path = path; + Digest = digest; + Images = images.AsReadOnly(); + this.byDigest = byDigest; + } + + internal string Path { get; } + internal Sha256Digest Digest { get; } + internal ReadOnlyCollection Images { get; } + + /// Reads and checks every stated file once per normalized path, retaining the checked bytes. + internal static DeepCheckInputs Read(string path) + { + path = System.IO.Path.GetFullPath(path); + var source = File.ReadAllBytes(path); + var text = new UTF8Encoding(false, true).GetString(source); + var directory = System.IO.Path.GetDirectoryName(path)!; + Dictionary paths = new(StringComparer.Ordinal); + Dictionary digests = []; + List images = []; + using var reader = new StringReader(text); + var lineNumber = 0; + while (reader.ReadLine() is { } line) + { + lineNumber++; + var escaped = line.StartsWith('\\'); + var start = escaped ? 1 : 0; + if (line.Length < start + 67 || line[start + 64] != ' ' || + line[start + 65] is not (' ' or '*')) + throw InvalidLine(); + var digest = Sha256Digest.Parse(line.AsSpan(start, 2 * Sha256Digest.Length), $"{path}:{lineNumber}"); + var name = line[(start + 66)..]; + if (escaped) name = Unescape(name, path, lineNumber); + var file = System.IO.Path.GetFullPath(name, directory); + if (paths.TryGetValue(file, out var previous)) + { + if (previous != digest) + throw new ArgumentException($"Conflicting SHA-256 digests for {file} in {path}."); + continue; + } + paths.Add(file, digest); + var bytes = File.ReadAllBytes(file); + var actual = Sha256Digest.Of(bytes); + if (actual != digest) + throw new ArgumentException($"SHA-256 of {file} is {actual}; {path} states {digest}."); + var image = new Image(file, digest, bytes); + images.Add(image); + digests.TryAdd(digest, image); + } + if (images.Count == 0) throw new ArgumentException($"No SHA-256 records in {path}."); + return new(path, Sha256Digest.Of(source), images, digests); + + ArgumentException InvalidLine() => + new($"Invalid sha256sum record at {path}:{lineNumber}."); + } + + /// Matches every selected and referenced assembly by its recorded digest in linear total work. + internal ReadOnlyCollection MatchPins(JsonElement identity) + { + List pins = []; + Add(identity, "assemblySha256"); + if (identity.TryGetProperty("references", out var references)) + foreach (var reference in references.EnumerateArray()) Add(reference, "sha256"); + var sourceCount = pins.Count; + AddPlatformImages(pins, identity); + AddReferenceImages(pins, sourceCount); + AddForwardedTypeImages(pins, sourceCount); + return pins.AsReadOnly(); + + void Add(JsonElement record, string digestProperty) + { + var name = record.GetProperty("assemblyIdentity").GetString() + ?? throw new ArgumentException("Missing pinned assembly identity."); + var digest = Sha256Digest.Parse(record.GetProperty(digestProperty).GetString().AsSpan(), "identity.json"); + if (!byDigest.TryGetValue(digest, out var image)) + throw new ArgumentException($"No line of {Path} has the digest identity.json pins for {name}."); + pins.Add(new(name, image)); + } + } + + /// Match direct AssemblyRefs of the extraction pins to digest-checked images. + /// The kernel checks the physical rows, the carried tokens and the definition keys. + void AddReferenceImages(List pins, int sourceCount) + { + HashSet required = new(StringComparer.Ordinal); + var malformed = false; + foreach (var pin in pins.Take(sourceCount).DistinctBy(pin => pin.Image.Digest)) + { + try + { + using var pe = pin.Image.OpenPe(); + var metadata = pe.GetMetadataReader(); + foreach (var handle in metadata.AssemblyReferences) + { + var reference = metadata.GetAssemblyReference(handle); + if (InvalidTokenWidth(metadata, reference)) + malformed = true; + else + required.Add(reference.GetAssemblyName().FullName); + } + } + catch (BadImageFormatException) { return; } // The image audit diagnoses malformed extraction pins. + } + if (required.Count == 0 && !malformed) return; + var digests = pins.Select(pin => pin.Image.Digest).ToHashSet(); + var pinnedNames = pins.Select(pin => pin.Identity).ToHashSet(StringComparer.Ordinal); + var matched = required.Where(pinnedNames.Contains).ToHashSet(StringComparer.Ordinal); + foreach (var image in byDigest.Values.Where(image => !digests.Contains(image.Digest))) + { + string identity; + try + { + using var pe = image.OpenPe(); + if (!pe.HasMetadata) continue; + var metadata = pe.GetMetadataReader(); + if (!metadata.IsAssembly) continue; + identity = metadata.GetAssemblyDefinition().GetAssemblyName().FullName; + } + catch (BadImageFormatException) { continue; } + if (!required.Contains(identity) || pinnedNames.Contains(identity)) continue; + if (!matched.Add(identity)) throw new ArgumentException($"More than one image for {identity} in {Path}."); + pins.Add(new(identity, image, Reference: true)); + } + if (matched.Count > 0 || malformed) + pins[0] = pins[0] with { KeyCoverage = !malformed && matched.Count == required.Count }; + } + + /// Follow top-level TypeRefs from extraction pins through supplied forwarding images. + /// The Lean audit checks each candidate path against the actual scoped declaration. + void AddForwardedTypeImages(List pins, int sourceCount) + { + if (pins.Select(pin => pin.Image.Digest).Distinct().Count() == byDigest.Count) return; + var available = byDigest.Values.Select(image => (Image: image, Identity: ReadAssemblyName(image)?.FullName)) + .Where(item => item.Identity is not null) + .ToLookup(item => item.Identity!, item => item.Image, StringComparer.Ordinal); + var selected = pins.Select(pin => pin.Identity).ToHashSet(StringComparer.Ordinal); + Queue<(string Scope, string Namespace, string Name)> pending = new(); + foreach (var pin in pins.Take(sourceCount).DistinctBy(pin => pin.Image.Digest)) + { + try + { + using var pe = pin.Image.OpenPe(); + var metadata = pe.GetMetadataReader(); + foreach (var handle in metadata.TypeReferences) + { + var type = metadata.GetTypeReference(handle); + if (type.ResolutionScope.Kind != HandleKind.AssemblyReference) continue; + var assembly = metadata.GetAssemblyReference((AssemblyReferenceHandle)type.ResolutionScope); + if (!InvalidTokenWidth(metadata, assembly)) + pending.Enqueue((assembly.GetAssemblyName().FullName, + metadata.GetString(type.Namespace), metadata.GetString(type.Name))); + } + } + catch (BadImageFormatException) { } // The image and row audits diagnose malformed extraction pins. + } + HashSet<(string, string, string)> visited = []; + Dictionary> forwarders = new(StringComparer.Ordinal); + while (pending.TryDequeue(out var type)) + { + if (!visited.Add(type) || !available.Contains(type.Scope)) continue; + if (!forwarders.TryGetValue(type.Scope, out var index)) + { + var candidates = available[type.Scope].ToArray(); + if (candidates.Length != 1) + throw new InvalidDataException("Multiple supplied images for " + type.Scope); + if (selected.Add(type.Scope)) pins.Add(new(type.Scope, candidates[0], Reference: true)); + index = ReadForwarders(candidates[0]); + forwarders.Add(type.Scope, index); + } + foreach (var target in index[(type.Namespace, type.Name)]) + pending.Enqueue((target, type.Namespace, type.Name)); + } + } + + static ILookup<(string, string), string> ReadForwarders(Image image) + { + List<((string, string) Type, string Target)> rows = []; + try + { + using var pe = image.OpenPe(); + var metadata = pe.GetMetadataReader(); + foreach (var handle in metadata.ExportedTypes) + { + var type = metadata.GetExportedType(handle); + if (!type.IsForwarder || type.Implementation.Kind != HandleKind.AssemblyReference) continue; + var assembly = metadata.GetAssemblyReference((AssemblyReferenceHandle)type.Implementation); + if (!InvalidTokenWidth(metadata, assembly)) + rows.Add(((metadata.GetString(type.Namespace), metadata.GetString(type.Name)), + assembly.GetAssemblyName().FullName)); + } + } + catch (BadImageFormatException) { } // The kernel still checks every selected name in the supplied source. + return rows.ToLookup(row => row.Type, row => row.Target); + } + + static bool InvalidTokenWidth(MetadataReader reader, AssemblyReference reference) => + (reference.Flags & AssemblyFlags.PublicKey) == 0 && + reader.GetBlobReader(reference.PublicKeyOrToken).Length is not (0 or 8); + + /// Find platform inputs by full assembly identity in the retained bytes. The kernel independently + /// resolves every scope from method evidence and checks the defining image and its physical fields. + void AddPlatformImages(List pins, JsonElement extraction) + { + var direct = pins.ToLookup(pin => pin.Image.Digest); + if (direct.Count == byDigest.Count && !pins.Any(pin => CouldBePlatform(pin.Identity))) return; + var available = byDigest.Values + .Where(image => !direct.Contains(image.Digest) || direct[image.Digest].Any(pin => CouldBePlatform(pin.Identity))) + .Select(image => (Image: image, Name: PlatformIdentity(image))) + .Where(candidate => candidate.Name is not null) + .ToLookup(candidate => candidate.Name!, candidate => candidate.Image, StringComparer.Ordinal); + if (available.Count == 0) return; + + HashSet required = new(StringComparer.Ordinal); + foreach (var pin in pins.DistinctBy(pin => pin.Image.Digest)) + { + using var pe = pin.Image.OpenPe(); + var metadata = pe.GetMetadataReader(); + if (available.Contains(pin.Identity)) required.Add(pin.Identity); + foreach (var handle in metadata.AssemblyReferences) + { + var reference = metadata.GetAssemblyReference(handle).GetAssemblyName(); + if (CoreLibrary(reference)) required.Add(reference.FullName); + } + } + const string pairName = "ValueTuple`2"; + // The admitted pair TypeRef has this exact scope (CIL.Extraction.runtimeScope). + const string pairScope = "System.Runtime, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"; + const string pairPrefix = pairScope + "::System." + pairName + "<"; + HashSet pairs = new(StringComparer.Ordinal); + if (required.Contains(pairScope) && extraction.TryGetProperty("layoutIdentities", out var layouts) && + layouts.EnumerateArray().Any(layout => layout.GetString()!.StartsWith(pairPrefix, StringComparison.Ordinal))) + pairs.Add(pairScope); + + FollowForwarders(required, "Object"); + FollowForwarders(pairs, pairName); + var objectsComplete = required.All(available.Contains); + var pairsComplete = pairs.All(available.Contains); + foreach (var name in required.Union(pairs).Order(StringComparer.Ordinal)) + { + if (available[name].SingleOrDefault() is { } image && !direct.Contains(image.Digest)) + pins.Add(new(name, image, Platform: true)); + } + for (var index = 0; index < pins.Count; index++) + { + var pin = pins[index]; + pins[index] = pin with + { + ObjectCoverage = required.Contains(pin.Identity) ? objectsComplete : null, + TupleCoverage = pairs.Contains(pin.Identity) ? pairsComplete : null + }; + } + + void FollowForwarders(HashSet scopes, string typeName) + { + Queue pending = new(scopes); + while (pending.TryDequeue(out var name)) + { + var matches = available[name].ToArray(); + if (matches.Length == 0) continue; + if (matches.Length != 1) + throw new InvalidDataException("Multiple images supplied for platform assembly " + name); + using var pe = matches[0].OpenPe(); + var metadata = pe.GetMetadataReader(); + foreach (var handle in metadata.ExportedTypes) + { + var type = metadata.GetExportedType(handle); + if (!type.IsForwarder || type.Implementation.Kind != HandleKind.AssemblyReference || + !metadata.StringComparer.Equals(type.Name, typeName) || + !metadata.StringComparer.Equals(type.Namespace, "System")) continue; + var target = metadata.GetAssemblyReference((AssemblyReferenceHandle)type.Implementation); + var assembly = target.GetAssemblyName(); + if (CoreLibrary(assembly) && scopes.Add(assembly.FullName)) pending.Enqueue(assembly.FullName); + } + } + } + + static bool CouldBePlatform(string identity) + { + var comma = identity.IndexOf(','); + return comma > 0 && PlatformToken(identity.AsSpan(0, comma)) is not null; + } + } + + static string? PlatformIdentity(Image image) => + ReadAssemblyName(image) is { } name && CoreLibrary(name) ? name.FullName : null; + + static AssemblyName? ReadAssemblyName(Image image) + { + try + { + using var pe = image.OpenPe(); + if (!pe.HasMetadata) return null; + var metadata = pe.GetMetadataReader(); + if (!metadata.IsAssembly) return null; + return metadata.GetAssemblyDefinition().GetAssemblyName(); + } + catch (BadImageFormatException) { return null; } // A digest list can include arbitrary unused files. + } + + /// The physical AssemblyRef and token width of a rejected publisher-token check. + internal static string? ReferenceKeyDiagnostic(IEnumerable pins) + { + foreach (var pin in pins.Where(pin => !pin.Platform && !pin.Reference)) + { + using var pe = pin.Image.OpenPe(); + var metadata = pe.GetMetadataReader(); + foreach (var handle in metadata.AssemblyReferences) + { + var reference = metadata.GetAssemblyReference(handle); + if (InvalidTokenWidth(metadata, reference)) + return $"{pin.Identity}, AssemblyRef {metadata.GetString(reference.Name)} has a " + + $"{metadata.GetBlobReader(reference.PublicKeyOrToken).Length}-byte public key token."; + } + } + return null; + } + + /// Physical field details for an already rejected kernel theorem. + internal static string? ObjectFieldDiagnostic(IEnumerable pins) + { + foreach (var pin in pins) + { + using var pe = pin.Image.OpenPe(); + var metadata = pe.GetMetadataReader(); + foreach (var handle in metadata.TypeDefinitions) + { + var type = metadata.GetTypeDefinition(handle); + if (!type.GetDeclaringType().IsNil || !metadata.StringComparer.Equals(type.Name, "Object") || + !metadata.StringComparer.Equals(type.Namespace, "System")) continue; + foreach (var field in type.GetFields()) + if ((metadata.GetFieldDefinition(field).Attributes & FieldAttributes.Static) == 0) + return $"System.Object in {pin.Identity} declares instance field 0x{MetadataTokens.GetToken(field):x8}."; + } + } + return null; + } + + /// The first declared instance field that disagrees with the pair's names or FIELD VAR signatures. + /// The diagnostic accompanies a kernel rejection and plays no part in accepting the image. + internal static string? TupleFieldDiagnostic(IEnumerable pins) + { + foreach (var pin in pins) + { + using var pe = pin.Image.OpenPe(); + var metadata = pe.GetMetadataReader(); + foreach (var handle in metadata.TypeDefinitions) + { + var type = metadata.GetTypeDefinition(handle); + if (!type.GetDeclaringType().IsNil || !metadata.StringComparer.Equals(type.Name, "ValueTuple`2") || + !metadata.StringComparer.Equals(type.Namespace, "System")) continue; + var slot = 0; + foreach (var handleField in type.GetFields()) + { + var field = metadata.GetFieldDefinition(handleField); + if ((field.Attributes & FieldAttributes.Static) != 0) continue; + var name = metadata.GetString(field.Name); + var signature = metadata.GetBlobBytes(field.Signature); + if (slot >= 2 || name != "Item" + (slot + 1) || + signature is not [0x06, 0x13, var parameter] || parameter != slot) + return $"System.ValueTuple`2 in {pin.Identity}, field 0x{MetadataTokens.GetToken(handleField):x8}" + + $" at instance slot {slot}: {name}, signature {Convert.ToHexStringLower(signature)}."; + slot++; + } + if (slot != 2) return $"System.ValueTuple`2 in {pin.Identity} declares {slot} instance fields."; + } + } + return null; + } + + static bool CoreLibrary(AssemblyName assembly) + { + var token = PlatformToken(assembly.Name); + return token is not null && string.IsNullOrEmpty(assembly.CultureName) && + assembly.GetPublicKeyToken() is { Length: 8 } key && Convert.ToHexStringLower(key) == token; + } + + static string? PlatformToken(ReadOnlySpan name) => name switch + { + "System.Runtime" => "b03f5f7f11d50a3a", + "System.Private.CoreLib" => "7cec85d7bea7798e", + "mscorlib" => "b77a5c561934e089", + "netstandard" => "cc7b13ffcd2ddd51", + _ => null + }; + + static string Unescape(string name, string path, int line) + { + StringBuilder decoded = new(name.Length); + for (var index = 0; index < name.Length; index++) + { + var character = name[index]; + if (character == '\\') + { + if (++index == name.Length) + throw new ArgumentException($"Incomplete filename escape at {path}:{line}."); + character = name[index] switch + { + '\\' => '\\', + 'n' => '\n', + 'r' => '\r', + _ => throw new ArgumentException($"Invalid filename escape at {path}:{line}.") + }; + } + decoded.Append(character); + } + return decoded.ToString(); + } +} diff --git a/scripts/DeepChecks.cs b/scripts/DeepChecks.cs new file mode 100644 index 000000000..d904636f3 --- /dev/null +++ b/scripts/DeepChecks.cs @@ -0,0 +1,1214 @@ +using System.Globalization; +using System.Numerics; +using System.Text; +using System.Text.Json; + +// Deep verification checks that the method bodies and carried metadata rows of an extraction are +// those of assembly files whose SHA-256 digests the caller states. +partial class Program +{ + /// One call's retained inputs and attempt, available to its caller after a failure. + sealed class DeepCheckRun(DeepCheckInputs inputs) + { + bool claimed; + internal DeepCheckInputs Inputs { get; } = inputs; + internal DeepCheckAttempt? Attempt { get; private set; } + + internal static DeepCheckRun? Claim(DeepCheckInputs? inputs, DeepCheckRun? run) + { + if (inputs is null) + { + if (run is not null) throw new ArgumentException("Deep-check holder requires inputs."); + return null; + } + run ??= new(inputs); + if (!ReferenceEquals(inputs, run.Inputs)) + throw new ArgumentException("Deep-check holder inputs do not match."); + if (run.claimed) throw new ArgumentException("Deep-check holder was already used."); + run.claimed = true; + return run; + } + + internal IReadOnlyList Begin(ExtractionIdentity identity) + { + if (!claimed || Attempt is not null) + throw new InvalidOperationException("Deep-check attempt requires one claimed request."); + var pins = Inputs.MatchPins(identity.Root); + Attempt = new(Inputs, identity.Sha256, pins); + return pins; + } + } + + /// Rejected targets of an incomplete verification; successful facts are never retained. + sealed class DeepCheckAttempt + { + readonly FailureReports.BindingFailureDigest digests; + readonly (string Identity, Sha256Digest Digest)[] images; + readonly HashSet imageDigests; + readonly DeepCheckInputs.Pin[] checkedPins; + readonly Dictionary rejectedImages = []; + readonly Dictionary rejectedFacts = []; + internal string IdentitySha256 { get; } + + internal DeepCheckAttempt(DeepCheckInputs inputs, string identitySha256, + IReadOnlyList pins) + { + digests = new(inputs.Path, inputs.Digest.ToString()); + IdentitySha256 = identitySha256; + checkedPins = [.. pins]; + images = [.. pins.Select(pin => (pin.Identity, pin.Image.Digest))]; + imageDigests = [.. images.Select(image => image.Digest)]; + } + + internal void RequireIdentity(ExtractionIdentity identity) + { + if (IdentitySha256 != identity.Sha256 || + IdentitySha256 != Sha(Path.Combine(identity.Output, "identity.json"))) + throw new InvalidDataException("Deep-check extraction identity changed."); + } + + internal void RejectImage(Sha256Digest digest, string reason) + { + if (!imageDigests.Contains(digest)) throw new ArgumentException("Unknown deep-check image."); + ArgumentException.ThrowIfNullOrWhiteSpace(reason); + rejectedImages.TryAdd(digest, reason); + } + + internal void RejectFact(BindingFact fact, string reason) + { + if (!Enum.IsDefined(fact)) throw new ArgumentException("Unknown binding fact."); + ArgumentException.ThrowIfNullOrWhiteSpace(reason); + try + { + var detail = fact switch + { + BindingFact.ObjectFields => DeepCheckInputs.ObjectFieldDiagnostic( + checkedPins.Where(pin => pin.ObjectCoverage.HasValue)), + BindingFact.ValueTupleFieldOrder => DeepCheckInputs.TupleFieldDiagnostic( + checkedPins.Where(pin => pin.TupleCoverage.HasValue)), + BindingFact.PublicKeyTokens => DeepCheckInputs.ReferenceKeyDiagnostic(checkedPins), + _ => null + }; + if (detail is not null) reason += ". " + detail; + } + catch (BadImageFormatException) { } // Preserve the kernel diagnostic for malformed metadata. + rejectedFacts.TryAdd(fact, reason); + } + + internal FailureReports.BindingFailure Failure(string stage, string diagnostic) + { + ArgumentException.ThrowIfNullOrWhiteSpace(stage); + var incomplete = "verification did not complete: " + stage + + (string.IsNullOrWhiteSpace(diagnostic) ? "" : ": " + diagnostic); + return new(digests, + [.. images.Select(image => new FailureReports.BindingFailureImage(image.Identity, + image.Digest.ToString(), + rejectedImages.ContainsKey(image.Digest) ? FailureReports.BindingFailureStatus.Failed + : FailureReports.BindingFailureStatus.NotCovered, + rejectedImages.GetValueOrDefault(image.Digest) ?? incomplete))], + [.. Enum.GetValues().Select(fact => new FailureReports.BindingFailureFact( + JsonSerializer.SerializeToElement(fact).GetString()!, + rejectedFacts.ContainsKey(fact) ? FailureReports.BindingFailureStatus.Failed + : FailureReports.BindingFailureStatus.NotCovered, + rejectedFacts.GetValueOrDefault(fact) ?? incomplete))]); + } + } + + /// A generated request's optional subject; a request cannot name both kinds. + abstract record BindingSubject + { + internal sealed record Fact(BindingFact Value, bool Complete = true) : BindingSubject; + internal sealed record Image(Sha256Digest Digest) : BindingSubject; + } + + /// Exact bytes and the digest independently checked by their generated module. + record BindingData(Sha256Digest Digest, byte[] Bytes) + { + /// The generated module that holds the file's bytes, named by its full digest. + internal string Module => "BindingAudit.I" + Digest; + + internal string Theorem => Module + ".digest"; + } + + sealed record BindingImagePin(string Identity, Sha256Digest Digest, byte[] Bytes) : BindingData(Digest, Bytes); + + sealed record BindingPublication(string Identity, BindingImagePin Producer, string InterfaceDigest, + string Profile, string Contract, BindingData Sidecar, BindingData Closure) + { + internal string[] Fields => [Identity, Producer.Identity, Producer.Digest.ToString(), InterfaceDigest, + Profile, Contract, Closure.Digest.ToString()]; + } + + /// What a run audits: the extraction, the files it pinned and where the generated modules go. + sealed record BindingPlan(string Output, BindingImagePin[] Images, int Methods, + bool? ObjectFields = null, bool? TupleFields = null, bool? PublicKeys = null, + BindingPublication[]? Imports = null) + { + internal IEnumerable Files => Images.Cast() + .Concat((Imports ?? []).SelectMany(import => new[] { import.Sidecar, import.Closure })) + .DistinctBy(file => file.Digest); + } + + /// + /// Artifacts and requests that must join the caller's extraction and proof audit. A contribution with a result + /// key may be published once the run succeeds; a restored one holds artifacts an earlier run replayed. + /// + sealed record DeepSessionContribution(string[] Modules, BindingTargets Targets, + SortedDictionary Artifacts, SortedDictionary Hashes, + SortedDictionary SourceHashes, string SourceDirectory, string? ResultKey = null, + bool Restored = false) + { + internal BindingRequest[] Requests => Targets.Requests; + + internal string[] OwnedFailures(IEnumerable failures) => failures.Where(module => + Targets.Modules.TryGetValue(module, out var subject) && subject is not null).ToArray(); + + /// Only trusted generated module ownership selects a rejected binding subject. + internal void RejectReplay(IReadOnlyDictionary rejections, + DeepCheckAttempt? attempt) + { + if (attempt is null) return; + foreach (var (module, rejection) in rejections) + { + if (!Targets.Modules.TryGetValue(module, out var subject)) continue; + var reason = $"Kernel rejected {rejection.Declaration}: {rejection.Kind}"; + switch (subject) + { + case BindingSubject.Fact fact: attempt.RejectFact(fact.Value, reason); break; + case BindingSubject.Image image: attempt.RejectImage(image.Digest, reason); break; + } + } + } + + /// Attributes only complete named audit rejections after physical guards have passed. + internal void RequireTheorems(AuditedOleans audited, IReadOnlyList expected, + DeepCheckAttempt? attempt) + { + var named = audited.theorems.ToDictionary(result => result.name, StringComparer.Ordinal); + var expectedNames = expected.Select(request => request.name).ToHashSet(StringComparer.Ordinal); + if (expectedNames.Count != expected.Count || !expectedNames.SetEquals(named.Keys) || + Targets.Theorems.Keys.Any(name => !expectedNames.Contains(name)) || + audited.theorems.Any(result => result.error is null && result.axioms is null)) + throw new InvalidDataException("Incomplete deep-check theorem audit result."); + RequireAudited(audited); + foreach (var request in Requests) + { + var result = named[request.Audit.name]; + string? rejection = result.error; + if (rejection is null && result.axioms is { } axioms && + axioms.Any(axiom => !ApprovedAxioms.Contains(axiom))) + rejection = "Unapproved proof axioms: " + string.Join(", ", axioms); + if (rejection is not null && attempt is not null) + switch (Targets.Theorems[request.Audit.name]) + { + case BindingSubject.Fact fact: attempt.RejectFact(fact.Value, rejection); break; + case BindingSubject.Image image: attempt.RejectImage(image.Digest, rejection); break; + } + } + foreach (var request in Requests) AuditedAxioms(named[request.Audit.name]); + } + + internal void RequireStable() + { + if (!Same(Hashes, BindingArtifactHashes(Artifacts.Values))) + throw new InvalidDataException("Deep-check artifact changed"); + if (!Directory.Exists(SourceDirectory) || !Same(SourceHashes, BindingSourceHashes(SourceDirectory))) + throw new InvalidDataException("Deep-check source changed: " + SourceDirectory); + } + + internal void RequireAudited(AuditedOleans audited) + { + RequireStable(); + foreach (var (module, path) in Artifacts) RequireAuditedModule(audited, module, path); + } + } + + /// The exact generated source inventory in the contribution's owned directory. + static SortedDictionary BindingSourceHashes(string directory) => + HashFiles(Directory.EnumerateFiles(directory, "*.lean", SearchOption.AllDirectories) + .Select(path => (path, path))); + + /// Hashes the exact compiled module families, enumerating each containing directory once. + static SortedDictionary BindingArtifactHashes(IEnumerable artifacts) => + HashFiles(ModuleFamilyParts(artifacts).Select(path => (path, path))); + + /// + /// Every part of each compiled module family: the module's `.olean` and each file beside it named by that + /// file and a further extension. A family whose module file is missing is refused. + /// + static List ModuleFamilyParts(IEnumerable artifacts) + { + var families = artifacts.Select(Path.GetFullPath) + .GroupBy(artifact => Path.GetDirectoryName(artifact)!, LocationComparer); + List files = []; + foreach (var family in families) + { + var expected = family.ToHashSet(LocationComparer); + foreach (var artifact in expected) + if (!File.Exists(artifact)) + throw new InvalidDataException("Deep-check artifact changed: missing " + artifact); + foreach (var path in Directory.EnumerateFiles(family.Key)) + { + var filename = Path.GetFileName(path); + // Any case finds a candidate; the directory's case rule then decides whether it is an expected file. + var marker = filename.IndexOf(".olean", StringComparison.OrdinalIgnoreCase); + if (marker < 0) continue; + var end = marker + ".olean".Length; + if ((end == filename.Length || filename[end] == '.') && + expected.Contains(Path.Combine(family.Key, filename[..end]))) + files.Add(path); + } + } + return files; + } + + /// + /// Builds deep modules against the caller's existing extraction in its verification session. Given the key of + /// the certified environment and extraction, restores modules an earlier successful run built and replayed for + /// the same generated sources instead. + /// + DeepSessionContribution BuildDeepSession(string session, string leanPath, string extractedOlean, + ExtractionIdentity identity, IReadOnlyList matchedPins, Action? sourcesReady = null, + string? environment = null, Dictionary? checkedClosures = null) + { + session = Path.GetFullPath(session); + extractedOlean = Path.GetFullPath(extractedOlean); + if (!File.Exists(extractedOlean)) throw new FileNotFoundException("Missing extraction module", extractedOlean); + var extractedHashes = BindingArtifactHashes([extractedOlean]); + Dictionary bytes = []; + var pins = matchedPins.Select(pin => + { + if (!bytes.TryGetValue(pin.Image.Digest, out var snapshot)) + bytes.Add(pin.Image.Digest, snapshot = pin.Image.Bytes.ToArray()); + return new BindingImagePin(pin.Identity, pin.Image.Digest, snapshot); + }).ToArray(); + BindingPlan plan = new(session, pins, + identity.Root.GetProperty("methods").GetArrayLength(), matchedPins.FirstOrDefault(pin => + pin.ObjectCoverage.HasValue)?.ObjectCoverage, matchedPins.FirstOrDefault(pin => + pin.TupleCoverage.HasValue)?.TupleCoverage, matchedPins.FirstOrDefault(pin => + pin.KeyCoverage.HasValue)?.KeyCoverage, ReadPublishedImports(identity, pins, checkedClosures)); + var directory = Path.Combine(session, "BindingAudit"); + if (Directory.Exists(directory)) + throw new InvalidOperationException("Deep-check modules already exist in the session: " + directory); + Directory.CreateDirectory(directory); + var targets = new BindingTargets(BindingRequests(plan)); + WriteBindingSources(plan); + sourcesReady?.Invoke(); + var sources = BindingSourceHashes(directory); + // Replay is the only kernel check of the generated modules, so a restored result must bind every import. + var imports = environment is null ? null : DeepImportClosure(session, leanPath, directory); + deepImports = imports; + var resultKey = environment is null || imports is null ? null : DeepResultKey(environment, + bindingSourcesKey ?? throw new InvalidOperationException("StrictBinding has not been prepared"), + bindingPreparation?.Toolchain ?? + throw new InvalidOperationException("StrictBinding has not been prepared"), + imports, Relative(directory, sources), pins, targets.Requests); + var restored = resultKey is not null && RestoreSuccess(resultKey, session); + Dictionary failures = restored ? new(StringComparer.Ordinal) + : BuildBindingModules(plan, leanPath); + if (failures.Count != 0) + throw new InvalidOperationException("Deep-check module build failed: " + + string.Join("; ", failures.Select(pair => pair.Key + ": " + pair.Value))); + string[] modules = [.. targets.Requests.Select(request => request.Module).Distinct(StringComparer.Ordinal)]; + SortedDictionary artifacts = new(StringComparer.Ordinal) { ["Extracted"] = extractedOlean }; + foreach (var module in modules.Where(module => module != "Extracted")) + { + var source = ModuleSource(session, module); + var artifact = Path.ChangeExtension(source, ".olean"); + artifacts.Add(module, artifact); + } + if (!Same(extractedHashes, BindingArtifactHashes([extractedOlean]))) + throw new InvalidDataException("Deep-check artifact changed: " + extractedOlean); + DeepSessionContribution contribution = new(modules, targets, artifacts, + BindingArtifactHashes(artifacts.Values), sources, directory, resultKey, restored); + contribution.RequireStable(); + return contribution; + + static SortedDictionary Relative(string root, SortedDictionary hashes) => + new(hashes.ToDictionary(pair => Path.GetRelativePath(root, pair.Key).Replace('\\', '/'), + pair => pair.Value), StringComparer.Ordinal); + } + + /// + /// The key of a deep result: the certified environment and extraction it was checked in, the prepared + /// StrictBinding sources and toolchain, the compiled modules the generated modules import transitively, and + /// the generated sources, pins and requests. The generated sources hold the image bytes, identities and page size. + /// + static string DeepResultKey(string environment, string bindingSources, string toolchain, + SortedDictionary imports, SortedDictionary sources, + IReadOnlyList pins, IReadOnlyList requests) => + ContentKey(JsonSerializer.Serialize(new + { + format = "deep-result-v1", + environment, + bindingSources, + toolchain, + imports, + sources, + pins = pins.Select(pin => new { identity = pin.Identity, digest = pin.Digest.ToString() }), + requests = requests.Select(request => new + { + module = request.Module, + audit = request.Audit, + fact = (request.Subject as BindingSubject.Fact)?.Value, + image = (request.Subject as BindingSubject.Image)?.Digest.ToString() + }) + })); + + /// + /// The environment a consumer's deep modules build in, beyond the compiled imports their key binds: the + /// verifier and the certified library. + /// + string ConsumerDeepEnvironment() => ContentKey(JsonSerializer.Serialize(new + { + format = "consumer-deep-environment-v2", + policy = VerificationPolicy, + library = libraryKey ?? throw new InvalidOperationException("The certified library is not selected") + })); + + /// The compiled import closure the key of the last keyed deep session bound. + SortedDictionary? deepImports; + + /// + /// The work the last deep import closure did: header-reader launches, compiled headers read, module parts + /// hashed and the bytes those parts hold. + /// + internal readonly record struct DeepClosureWork(int Launches, int Headers, int Hashed, long HashedBytes); + + DeepClosureWork deepClosureWork; + + // The library guard holds certified files to the bytes read here, so each is read once per process. + readonly Dictionary certifiedHeaders = new(LocationComparer); + readonly Dictionary certifiedPartHashes = new(LocationComparer); + readonly Dictionary generatedImports = new(StringComparer.Ordinal); + + /// + /// The content hash of every compiled module part the modules generated below `directory` import, + /// transitively, by its name below its search root. Each import resolves as Lean resolves it: to the first + /// root of `leanPath` that holds it, then to the toolchain, whose modules the toolchain key binds. + /// + SortedDictionary DeepImportClosure(string session, string leanPath, string directory) + { + var toolchain = Path.GetFullPath(Path.Combine(Toolchain, "lib", "lean")); + var certified = library is null ? null : Path.GetFullPath(Path.Combine(library, ".lake/build/lib/lean")); + if (certified is not null) RequireUnchangedLibrary(); + bool Certified(string root) => certified is not null && LocationComparer.Equals(root, certified); + Dictionary compiled = new(StringComparer.Ordinal); + foreach (var root in leanPath.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries) + .Select(Path.GetFullPath) + .Where(root => !LocationComparer.Equals(root, toolchain) && Directory.Exists(root))) + foreach (var path in Directory.EnumerateFiles(root, "*.olean", SearchOption.AllDirectories)) + compiled.TryAdd(ModuleName(Path.GetRelativePath(root, path).Replace('\\', '/')), (root, path)); + int launches = 0; + var generated = Directory.EnumerateFiles(directory, "*.lean", SearchOption.AllDirectories) + .ToDictionary(path => Path.ChangeExtension(Path.GetRelativePath(session, path), null) + .Replace(Path.DirectorySeparatorChar, '.'), File.ReadAllText, StringComparer.Ordinal); + var unparsed = generated.Where(pair => !generatedImports.ContainsKey(ContentKey(pair.Value))).ToArray(); + if (unparsed.Length != 0) + { + launches++; + var parsed = ContractModuleImports(session, new(unparsed.ToDictionary(), StringComparer.Ordinal)); + foreach (var (module, source) in unparsed) generatedImports[ContentKey(source)] = parsed[module]; + } + // One launch reads every header this process has not read from the certified library. + var unread = compiled + .Where(pair => !Certified(pair.Value.Root) || !certifiedHeaders.ContainsKey(pair.Value.Path)) + .ToDictionary(pair => pair.Key, pair => pair.Value.Path); + var read = unread.Count == 0 ? [] : ConsumerModuleImports(session, unread); + if (unread.Count != 0) launches++; + foreach (var (module, imports) in read) + if (Certified(compiled[module].Root)) certifiedHeaders[compiled[module].Path] = imports; + var pending = new Queue(generated.Values.SelectMany(source => generatedImports[ContentKey(source)]) + .Where(module => !generated.ContainsKey(module))); + HashSet closure = new(StringComparer.Ordinal); + while (pending.TryDequeue(out var module)) + { + if (!closure.Add(module)) continue; + if (compiled.TryGetValue(module, out var found)) + foreach (var imported in read.TryGetValue(module, out var imports) ? imports + : certifiedHeaders[found.Path]) pending.Enqueue(imported); + else if (!File.Exists(Path.Combine(toolchain, + module.Replace('.', Path.DirectorySeparatorChar) + ".olean"))) + throw new InvalidDataException("Deep-check import is not compiled: " + module); + } + Dictionary names = new(LocationComparer); + List unhashed = []; + List certifiedUnhashed = []; + foreach (var (root, olean) in closure.Where(compiled.ContainsKey).Select(module => compiled[module])) + // Companion parts lie beside their module, so each part is named below its module's root. + foreach (var part in ModuleFamilyParts([olean])) + { + names[part] = Path.GetRelativePath(root, part).Replace('\\', '/'); + if (!Certified(root)) unhashed.Add(part); + else if (!certifiedPartHashes.ContainsKey(part)) certifiedUnhashed.Add(part); + } + var hashed = HashFiles(unhashed.Concat(certifiedUnhashed).Select(part => (part, part))); + foreach (var part in certifiedUnhashed) certifiedPartHashes[part] = hashed[part]; + deepClosureWork = new(launches, unread.Count, hashed.Count, + hashed.Keys.Sum(part => new FileInfo(part).Length)); + return new(names.ToDictionary(pair => pair.Value, + pair => hashed.TryGetValue(pair.Key, out var hash) ? hash : certifiedPartHashes[pair.Key]), + StringComparer.Ordinal); + } + + /// + /// Copies the generated modules a keyed deep contribution built into a new directory below + /// `staging`, checking each copy against the audited hashes, for publication once the run succeeds. + /// Restored and unkeyed contributions stage nothing. + /// + static string? StageDeepResult(DeepSessionContribution deep, string staging) + { + if (deep.ResultKey is null || deep.Restored) return null; + deep.RequireStable(); + var root = Path.GetDirectoryName(deep.SourceDirectory)!; + var publication = Path.Combine(staging, "deep-cache"); + Directory.CreateDirectory(publication); + foreach (var (path, hash) in deep.Hashes) + { + var generated = Path.GetRelativePath(deep.SourceDirectory, path); + if (generated.StartsWith("..", StringComparison.Ordinal) || Path.IsPathRooted(generated)) continue; + var target = Path.Combine(publication, Path.GetRelativePath(root, path)); + Directory.CreateDirectory(Path.GetDirectoryName(target)!); + File.Copy(path, target); + if (Sha(target) != hash) throw new InvalidDataException("Deep-check artifact changed: " + path); + } + return publication; + } + + /// Read the completeness constructor in the jointly audited theorem's type. + static bool CoverageComplete(AuditedTheorem theorem) + { + // The structural audit binds the proposition and every extraction and image constant. + // Read only its final argument from the replayed statement. + if (theorem.term is not { } term || theorem.termError is not null) + throw new InvalidDataException($"Cannot read the coverage argument of {theorem.name}: " + + (theorem.termError ?? "audit returned no statement term")); + var nodes = term.GetProperty("nodes"); + var application = nodes[term.GetProperty("root").GetInt32()]; + if (application[0].GetString() == "app") + { + var argument = nodes[application[2].GetInt32()]; + if (argument.GetArrayLength() == 4 && argument[0].GetString() == "const" && + argument[2].GetArrayLength() == 2 && argument[2][0].GetString() == "Bool") + return argument[2][1].GetString() switch + { + "true" => true, + "false" => false, + _ => throw new InvalidDataException("Invalid coverage constructor") + }; + } + throw new InvalidDataException("Expected a literal coverage argument"); + } + + /// Report each fact and image from the requests that passed the joint audit. + static BindingSection DeepBindingSection(DeepCheckInputs inputs, IReadOnlyList pins, + IReadOnlyList audited, StatementSources sources, AuditedOleans oleanAudit) + { + Dictionary factTheorems = []; + Dictionary imageTheorems = []; + foreach (var request in audited) + { + if (request.CoveragePredicate is not null && + !CoverageComplete(oleanAudit.theorems.Single(result => result.name == request.Audit.name))) + continue; + var distinct = request.Subject switch + { + BindingSubject.Fact { Complete: true } fact => factTheorems.TryAdd(fact.Value, request.Audit.name), + BindingSubject.Image image => imageTheorems.TryAdd(image.Digest, request.Audit.name), + _ => true + }; + if (!distinct) throw new InvalidOperationException("Two deep-check requests name one subject: " + + request.Audit.name); + } + var pinnedDigests = pins.Select(pin => pin.Image.Digest).ToHashSet(); + ImageResult[] images = + [ + .. pins.Select(pin => + { + var role = pin.Platform ? ImageRole.Platform : pin.Reference ? ImageRole.Reference : ImageRole.Pinned; + return imageTheorems.TryGetValue(pin.Image.Digest, out var theorem) + ? new ImageResult(pin.Identity, pin.Image.Digest.ToString(), role, + BindingStatus.Reconfirmed, theorem, null) + : new ImageResult(pin.Identity, pin.Image.Digest.ToString(), role, + BindingStatus.NotCovered, null, "no checked theorem states the digest of this image"); + }), + .. inputs.Images.Where(image => !pinnedDigests.Contains(image.Digest)).Select(image => + new ImageResult(image.Path, image.Digest.ToString(), ImageRole.Unused, BindingStatus.NotCovered, null, + "no extraction pin or checked binding claim names this image")) + ]; + BindingFactResult[] facts = [.. Enum.GetValues().Select(fact => + factTheorems.TryGetValue(fact, out var theorem) + ? new BindingFactResult(fact, BindingStatus.Reconfirmed, theorem, null) + : new BindingFactResult(fact, BindingStatus.NotCovered, null, NotCoveredReason(fact)))]; + return new(BindingMode.Deep, new(inputs.Path, inputs.Digest.ToString()), sources, images, facts); + } + + /// Why a deep run that audited no theorem about a fact does not cover it. + static string NotCoveredReason(BindingFact fact) => fact switch + { + BindingFact.BodyBytes => "no checked theorem binds the method bodies to the pinned images", + BindingFact.MetadataRows => + "no checked theorem binds the carried metadata rows and heap entries to the pinned images", + BindingFact.PlatformTypeKinds => "the extraction selects no platform type or a defining or forwarding image is missing", + BindingFact.EnumUnderlyingTypes => "the extraction selects no enum or a defining or forwarding image is missing", + BindingFact.ValueTupleFieldOrder => "the supplied images do not cover a selected System.ValueTuple pair", + BindingFact.ObjectFields => "the supplied images do not cover the extraction's platform definition of System.Object", + BindingFact.LayoutContent => + "no value layout was selected, or a layout or import lacks physical coverage", + BindingFact.DispatchCatalogs => + "no virtual class slot was selected, or a hierarchy, reference or import lacks physical coverage", + BindingFact.StaticData => "no checked theorem binds the static field data to the pinned images", + BindingFact.GenericConstraints => + "no generic parameter was selected, or a declaration, layout or import lacks physical coverage", + BindingFact.PublicKeyTokens => "the supplied images do not cover the extraction pins' AssemblyRef targets", + BindingFact.ImportIdentities => "no complete check binds every generic call to its pinned producer and published contract", + _ => throw new InvalidOperationException("Missing binding fact reason: " + fact) + }; + + const string ImagesModule = "BindingAudit.Images"; + const string LayoutContentModule = "BindingAudit.LayoutContent"; + const string LayoutContentTheorem = "BindingAudit.layoutContent"; + const string ImportsModule = "BindingAudit.GenericImports"; + const string ImportsTheorem = "BindingAudit.genericImports"; + const string DispatchCatalogsModule = "BindingAudit.DispatchCatalogs"; + const string DispatchCatalogsTheorem = "BindingAudit.dispatchCatalogs"; + const string GenericConstraintsModule = "BindingAudit.GenericConstraints"; + const string GenericConstraintsTheorem = "BindingAudit.genericConstraints"; + const string PlatformKindsModule = "BindingAudit.PlatformKinds"; + const string PlatformKindsTheorem = "BindingAudit.platformKinds"; + const string EnumStorageModule = "BindingAudit.EnumStorage"; + const string EnumStorageTheorem = "BindingAudit.enumStorage"; + const string PublicKeysModule = "BindingAudit.PublicKeys"; + const string TupleFieldsModule = "BindingAudit.TupleFields"; + const string ObjectFieldsModule = "BindingAudit.ObjectFields"; + const string StaticDataModule = "BindingAudit.StaticData"; + const string BodiesModule = "BindingAudit.Bodies"; + const string MetadataRowsModule = "BindingAudit.MetadataRows"; + + /// The facts proved page by page over the method evidence, in the order their modules build. + static readonly BindingFact[] PagedFacts = [BindingFact.BodyBytes, BindingFact.MetadataRows]; + + /// + /// A paged fact's generated module and the namespace of its pages; the theorem that states it, the proposition + /// it states about `BindingAudit.images` and `Extracted.methodEvidence`, and the check and claims its pages + /// read; the lemma its proposition conjoins before the pages, proved once over the whole evidence, if any; and + /// the module title, summary and theorem docstring. + /// + static (string Module, string Pages, string Theorem, string Proposition, string Check, string Claims, + (string Theorem, string Proposition, string Docstring)? Once, string Title, string Summary, string Statement) + PagedFact(BindingFact fact) => fact switch + { + BindingFact.BodyBytes => (BodiesModule, "Bodies", "bodies", "StrictBinding.BodiesBound", + "StrictBinding.bodyClaimBound", "StrictBinding.bodyClaims", null, "Method bodies in the pinned images", + "Every method body of the extraction's evidence is the bytes at the place the MethodDef row of its\n" + + "token gives in the pinned image of its assembly", + "The method bodies of `Extracted.methodEvidence` are in the pinned images."), + BindingFact.MetadataRows => (MetadataRowsModule, "MetadataRows", "metadataRows", + "StrictBinding.MetadataRowsBound", "StrictBinding.methodMetadataBound", "StrictBinding.metadataClaims", + ("blocks", "StrictBinding.ClaimsHold StrictBinding.assemblyBlockBound images\n" + + " (StrictBinding.assemblyBlocks (StrictBinding.metadataClaims Extracted.methodEvidence))", + "Each distinct Assembly block of the evidence is the pinned image's."), + "Method metadata in the pinned images", + "Every metadata block of the extraction's evidence holds the table header, Assembly row, table rows\n" + + "and heap entries of the pinned image its identity names, under one numbering of the #Strings and\n" + + "#Blob offsets per heap and with MethodDef RVAs zeroed, each distinct Assembly block checked once", + "The metadata blocks of `Extracted.methodEvidence` belong to the pinned images."), + _ => throw new ArgumentOutOfRangeException(nameof(fact), fact, "Not a paged binding fact") + }; + + /// The method bodies per page until the page cap applies; each page is one kernel evaluation. + const int PageMethodsTarget = 250; + + /// The most pages; each page prepares the image again, so this bounds the preparation work. + const int MaximumPages = 8; + + // Pages number ceil(methods / 250) up to 8, and then grow as methods / 8, so the image is prepared at most + // nine times whatever the method count. + static int BindingPageCount(int methods) => + Math.Clamp((methods + PageMethodsTarget - 1) / PageMethodsTarget, 1, MaximumPages); + + static int BindingPageSize(int methods) + { + var pages = BindingPageCount(methods); + return Math.Max(1, (methods + pages - 1) / pages); + } + + static int BindingPageCount(int methods, int pageSize) => Math.Max(1, (methods + pageSize - 1) / pageSize); + + string? bindingSession, bindingSourcesKey, bindingLeanVersion; + BindingLibraryPreparation? bindingPreparation; + + /// The exact library and session artifacts certified for the strict binding sources. + sealed record BindingLibraryPreparation(string Library, string LibraryKey, string Toolchain, + SortedDictionary BaseArtifacts, SortedDictionary SessionArtifacts, + SortedDictionary CertifiedArtifacts); + + /// Builds and replays StrictBinding in the caller's selected, certified library session. + void PrepareBindingLibrary(string package) + { + if (session is null || library is null || libraryKey is null || certifiedModules is null || + !SameLocation(package, session)) + throw new InvalidOperationException("StrictBinding requires the selected certified library session"); + RequireUnchangedLibrary(); + if (bindingPreparation is not null) + { + if (!Same(RequirePreparedBindingLibrary(), certifiedModules)) + throw new InvalidOperationException("The certified library changed before preparing StrictBinding"); + return; + } + var beforeBase = ModuleArtifacts(library); + var beforeSession = ModuleArtifacts(session); + var current = new SortedDictionary(beforeBase, StringComparer.Ordinal); + foreach (var (name, hash) in beforeSession) current[name] = hash; + if (!Same(current, certifiedModules) || + baseModules!.Any(pair => !beforeBase.TryGetValue(pair.Key, out var hash) || hash != pair.Value)) + throw new InvalidOperationException("The certified library changed before preparing StrictBinding"); + var toolchain = BindingToolchainKey(session); + CopyBindingSources(session); + var sourcesKey = BindingSourcesKey(session); + var leanVersion = Run([LeanExecutable, "--version"]).Trim(); + Run(["lake", "build", "StrictBinding"], session, progress: "Building the strict binding library"); + var afterBase = ModuleArtifacts(library); + var afterSession = ModuleArtifacts(session); + current = new(afterBase, StringComparer.Ordinal); + foreach (var (name, hash) in afterSession) current[name] = hash; + if (!Same(beforeBase, afterBase) || + !ModuleArtifactsPreserved(beforeSession, afterSession) || + !ModuleArtifactsPreserved(certifiedModules, current)) + throw new InvalidOperationException("The certified library changed while building StrictBinding"); + string[] added = + [.. current.Keys.Where(name => !certifiedModules.ContainsKey(name)).Select(ModuleName).Distinct()]; + if (sourcesKey != BindingSourcesKey(session) || toolchain != BindingToolchainKey(session)) + throw new InvalidOperationException("The strict binding sources or toolchain changed during preparation"); + if (added.Length != 0) + ReplayCheckedModules(added, SessionLeanPath(session), "Kernel replay of the strict binding library"); + if (!Same(afterBase, ModuleArtifacts(library)) || !Same(afterSession, ModuleArtifacts(session)) || + sourcesKey != BindingSourcesKey(session) || toolchain != BindingToolchainKey(session)) + throw new InvalidOperationException("The strict binding library changed during preparation"); + certifiedModules = current; + bindingSession = session; + bindingSourcesKey = sourcesKey; + bindingLeanVersion = leanVersion; + bindingPreparation = new(library, libraryKey, toolchain, afterBase, afterSession, + new(current, StringComparer.Ordinal)); + } + + /// The prepared library retains its sources, toolchain and every original artifact family. + SortedDictionary RequirePreparedBindingLibrary() + { + var prepared = bindingPreparation ?? + throw new InvalidOperationException("StrictBinding has not been prepared"); + RequireUnchangedLibrary(); + if (session is null || bindingSession != session || prepared.Library != library || + prepared.LibraryKey != libraryKey || prepared.Toolchain != BindingToolchainKey(session) || + bindingSourcesKey != BindingSourcesKey(session)) + throw new InvalidOperationException("The prepared StrictBinding library changed"); + var baseArtifacts = ModuleArtifacts(library!); + var sessionArtifacts = ModuleArtifacts(session); + var current = new SortedDictionary(baseArtifacts, StringComparer.Ordinal); + foreach (var (name, hash) in sessionArtifacts) current[name] = hash; + if (!Same(prepared.BaseArtifacts, baseArtifacts) || + !ModuleArtifactsPreserved(prepared.SessionArtifacts, sessionArtifacts) || + !ModuleArtifactsPreserved(prepared.CertifiedArtifacts, current)) + throw new InvalidOperationException("The prepared StrictBinding library changed"); + return current; + } + + /// Existing module families retain exactly their artifacts; additional families are permitted. + static bool ModuleArtifactsPreserved(SortedDictionary before, + SortedDictionary after) + { + HashSet families = new(StringComparer.Ordinal); + foreach (var (name, hash) in before) + { + if (!after.TryGetValue(name, out var current) || current != hash) return false; + families.Add(ModuleName(name)); + } + foreach (var name in after.Keys) + if (families.Contains(ModuleName(name)) && !before.ContainsKey(name)) return false; + return true; + } + + /// The toolchain declaration and compiler bytes used to prepare the binding library. + string BindingToolchainKey(string package) => ContentKey(Sha(Path.Combine(package, "lean-toolchain")) + + "\n" + Toolchain + "\n" + Sha(LeanExecutable)); + + /// Copies the StrictBinding library's sources into a package, below the same relative paths. + static void CopyBindingSources(string package) + { + var sources = Directory.EnumerateFiles(At("StrictBinding"), "*.lean", SearchOption.AllDirectories) + .Append(At("StrictBinding.lean")); + foreach (var source in sources) + { + var destination = Path.Combine(package, Path.GetRelativePath(Root, source)); + Directory.CreateDirectory(Path.GetDirectoryName(destination)!); + File.Copy(source, destination, true); + } + } + + /// Derives a coverage bit from the physical checks and proves its value for the selected methods. + static string CoverageSource(string check, string proposition, string theorem, string macro, string module, + string arguments = "Extracted.platformDefinitions Extracted.methodEvidence", + string additionalImports = "", string declarations = "") => $$""" + import BindingAudit.Images + import Extracted + import StrictBinding.{{module}} + import Lean{{additionalImports}} + + namespace BindingAudit{{declarations}} + + open Lean Elab Term in + elab "{{macro}}%" : term => do + return toExpr ((StrictBinding.{{check}} images {{arguments}}).toOption == some true) + + set_option debug.skipKernelTC true in + /-- Supplied definitions pass the physical check. The last argument records completeness. -/ + theorem {{theorem}} : StrictBinding.{{proposition}} images {{arguments}} {{macro}}% := by decide +kernel + + end BindingAudit + + """; + + /// Writes one image module per digest and retains every pinned identity association. + static void WriteBindingSources(BindingPlan plan) + { + foreach (var image in plan.Files) + File.WriteAllText(ModuleSource(plan.Output, image.Module), ImageModuleSource(image)); + File.WriteAllText(ModuleSource(plan.Output, ImagesModule), ImagesModuleSource(plan)); + File.WriteAllText(ModuleSource(plan.Output, EnumStorageModule), CoverageSource( + "checkEnumStorage", "EnumStorageChecked", "enumStorage", "enumCoverage", "EnumStorage")); + File.WriteAllText(ModuleSource(plan.Output, LayoutContentModule), CoverageSource( + "checkLayoutContent", "LayoutContentChecked", "layoutContent", "layoutCoverage", + "LayoutContent", "Extracted.rawLayouts Extracted.imports")); + File.WriteAllText(ModuleSource(plan.Output, DispatchCatalogsModule), CoverageSource( + "ClassDispatch.check", "DispatchCatalogsChecked", "dispatchCatalogs", "dispatchCoverage", + "DispatchCatalogs", "Extracted.assemblies Extracted.methodEvidence Extracted.imports")); + File.WriteAllText(ModuleSource(plan.Output, GenericConstraintsModule), CoverageSource( + "checkGenericConstraints", "GenericConstraintsChecked", "genericConstraints", "constraintCoverage", + "GenericConstraints", "Extracted.methodEvidence Extracted.rawLayouts Extracted.imports")); + File.WriteAllText(ModuleSource(plan.Output, PlatformKindsModule), CoverageSource( + "checkPlatformKinds", "PlatformKindsChecked", "platformKinds", "kindCoverage", "PlatformKinds")); + File.WriteAllText(ModuleSource(plan.Output, StaticDataModule), """ + import BindingAudit.Images + import Extracted + import StrictBinding.StaticData + + namespace BindingAudit + + set_option debug.skipKernelTC true in + /-- The complete RVA data of each static site agrees with the pinned assembly. -/ + theorem staticData : StrictBinding.StaticDataBound images Extracted.methodEvidence := by + decide +kernel + + end BindingAudit + + """); + if (plan.ObjectFields is { } complete) + File.WriteAllText(ModuleSource(plan.Output, ObjectFieldsModule), $$""" + import BindingAudit.Images + import Extracted + import StrictBinding.ObjectFields + + namespace BindingAudit + + set_option debug.skipKernelTC true in + /-- The supplied reference images agree with these methods about System.Object fields. -/ + theorem objectFields : + StrictBinding.{{ObjectProposition(complete)}} images Extracted.methodEvidence := by decide +kernel + + end BindingAudit + + """); + if (plan.TupleFields is { } pairComplete) + File.WriteAllText(ModuleSource(plan.Output, TupleFieldsModule), $$""" + import BindingAudit.Images + import Extracted + import StrictBinding.TupleFields + + namespace BindingAudit + + set_option debug.skipKernelTC true in + /-- The pinned platform pair declares the selected layout's two fields in generic slot order. -/ + theorem tupleFields : StrictBinding.{{TupleProposition(pairComplete)}} images + Extracted.methodEvidence Extracted.rawLayouts := by decide +kernel + + end BindingAudit + + """); + if (plan.PublicKeys is { } keysComplete) + File.WriteAllText(ModuleSource(plan.Output, PublicKeysModule), $$""" + import BindingAudit.Images + import Extracted + import StrictBinding.PublicKeys + + namespace BindingAudit + + set_option debug.skipKernelTC true in + /-- The supplied reference images carry the keys named by the extraction pins. -/ + theorem publicKeyTokens : StrictBinding.{{KeyProposition(keysComplete)}} images + Extracted.assemblies Extracted.methodEvidence := by decide +kernel + + end BindingAudit + + """); + if (plan.Imports is { } publications) + File.WriteAllText(ModuleSource(plan.Output, ImportsModule), PublishedImportsSource(publications)); + foreach (var fact in PagedFacts) + File.WriteAllText(ModuleSource(plan.Output, PagedFact(fact).Module), PagedFactModuleSource(fact, + plan.Methods, BindingPageSize(plan.Methods))); + } + + /// Only a selected physical producer and a replayed interface can supply a publication. + static BindingPublication[]? ReadPublishedImports(ExtractionIdentity identity, BindingImagePin[] pins, + Dictionary? closures) + { + var imports = identity.Imports; + if (imports.Length == 0 || closures is null) return null; + List publications = []; + Dictionary sidecars = new(LocationComparer); + foreach (var import in imports.OrderBy(entry => entry.GetProperty("slot").GetInt32())) + { + if (!import.TryGetProperty("typeArguments", out var arguments) || + arguments.ValueKind != JsonValueKind.Array || arguments.GetArrayLength() == 0 || + import.GetProperty("profile").GetString() != "scalar" || + !import.TryGetProperty("typedSignature", out var signature) || + signature.ValueKind != JsonValueKind.Object) return null; + string Field(string name) => import.GetProperty(name).GetString()!; + string constant = Field("contractConstant"); + if (!signature.TryGetProperty("owner", out var owner) || owner.ValueKind != JsonValueKind.Object || + !owner.TryGetProperty("scope", out var scopeName)) return null; + var scope = scopeName.GetString(); + var producer = pins.SingleOrDefault(pin => pin.Identity == scope && + pin.Digest.ToString() == Field("implementationSha256")); + if (producer is null || !closures.TryGetValue(constant, out var closure)) return null; + string path = ExtractionIdentity.Resolve(identity.Output, Field("sidecarPath")); + if (!sidecars.TryGetValue(path, out var sidecar)) + { + if (!File.Exists(path)) return null; + byte[] bytes = File.ReadAllBytes(path); + sidecars.Add(path, sidecar = new(Sha256Digest.Of(bytes), bytes)); + } + if (sidecar.Digest.ToString() != Field("sidecarSha256")) + throw new InvalidDataException("Published contracts sidecar digest mismatch: " + path); + var canonical = Encoding.UTF8.GetBytes(closure.canonical!); + var digest = Sha256Digest.Of(canonical); + if (digest.ToString() != Field("closureDigest")) + throw new InvalidDataException("Published closure digest mismatch: " + constant); + publications.Add(new(Field("identity"), producer, Field("interfaceDigest"), Field("profile"), + constant, sidecar, new(digest, canonical))); + } + return [.. publications]; + } + + static string PublishedImportsSource(BindingPublication[] imports) + { + var modules = string.Join("\n", imports.Select(item => item.Sidecar.Module).Distinct() + .Select(module => "import " + module)); + var entries = string.Join(",\n ", imports.Select(item => + "mkAppN (mkConst ``StrictBinding.GenericImports.Publication.mk) #[" + + string.Join(", ", item.Fields.Select(field => "mkStrLit " + LeanString(field))) + + ", mkConst ``" + item.Sidecar.Module + ".image]")); + var declarations = "\n\n" + $$""" + open Lean in + elab "publications%" : term => do + let type := mkConst ``StrictBinding.GenericImports.Publication + let entries : Array Expr := #[ + {{entries}}] + return entries.foldr (mkApp3 (mkConst ``List.cons [.zero]) type) + (mkApp (mkConst ``List.nil [.zero]) type) + """; + return CoverageSource("GenericImports.check", "GenericImportsChecked", "genericImports", + "genericImportCoverage", "GenericImports", "Extracted.profile Extracted.methodEvidence Extracted.imports publications%", + "\n" + modules, declarations); + } + + static string KeyProposition(bool complete) => complete ? "PublicKeysBound" : "SuppliedPublicKeysValid"; + + static string TupleProposition(bool complete) => complete ? "TupleFieldsBound" : "SuppliedTupleFieldsValid"; + + static string ObjectProposition(bool complete) => complete ? "ObjectFieldsBound" : "SuppliedObjectFieldsValid"; + + static string ModuleSource(string directory, string module) => + Path.Combine(directory, module.Replace('.', Path.DirectorySeparatorChar) + ".lean"); + + static string ImageModuleSource(BindingData image) => $$""" + import StrictBinding.Sha256 + import CIL.Extraction.FixedDigits + + /-! + # Image of an assembly file + + The bytes of one assembly file as an `ImageBytes` constant, and the SHA-256 digest of those + bytes proved page by page. Each page lemma is kernel-checked only when this module is replayed. + -/ + + namespace {{image.Module}} + + {{BindingImage.Definitions(image.Bytes)}} + {{BindingImage.PagedDigestTheorem(image.Bytes, skipCompileCheck: true)}} + end {{image.Module}} + """; + + static string ImagesModuleSource(BindingPlan plan) + { + var imports = string.Concat(plan.Images.DistinctBy(image => image.Digest) + .Select(image => $"import {image.Module}\n")); + var entries = string.Join(",\n ", plan.Images.Select(image => + $"{{ identity := {LeanString(image.Identity)}, image := {image.Module}.image }}")); + return $""" + import Lean + import StrictBinding.Image + {imports} + /-! + # The pinned images + + Each assembly the extraction read, under the identity the extraction records for it, with the + image constant whose SHA-256 digest its own module proves. + -/ + + namespace BindingAudit + + open Lean Elab in + /-- Exact pinned-image list constructors with independently elaborated elements. -/ + elab "pinnedImages%[" items:term,* "]" : term => do + let type := mkConst ``StrictBinding.PinnedImage + let values <- items.getElems.mapM fun item => Term.elabTermEnsuringType item (some type) + return values.foldr (mkApp3 (mkConst ``List.cons [.zero]) type) + (mkApp (mkConst ``List.nil [.zero]) type) + + /-- The pinned images the audit reads. -/ + def images : List StrictBinding.PinnedImage := + pinnedImages%[{entries}] + + /-- The identities and image constants of `images`, as the driver states them. -/ + theorem images_eq : images = + pinnedImages%[{entries}] := rfl + + end BindingAudit + """; + } + + /// + /// The module of a paged fact: one theorem per page of the claims the method evidence makes, each its own kernel + /// evaluation, and the fact's theorem, which the kernel-checked composition `claimsHold_of_pages` derives for + /// the whole evidence. The composition needs the pages to number enough to cover the evidence and to be the + /// first pages in order, so no claim is skipped, repeated or reordered. + /// + static string PagedFactModuleSource(BindingFact fact, int methods, int size) + { + var paged = PagedFact(fact); + var pages = BindingPageCount(methods, size); + var lemmas = string.Concat(Enumerable.Range(0, pages).Select(page => $$""" + set_option debug.skipKernelTC true in + /-- The claims of page {{page}} hold in the pinned images. -/ + theorem page{{page}} : StrictBinding.ClaimsHold {{paged.Check}} images + (StrictBinding.claimPage ({{paged.Claims}} Extracted.methodEvidence) {{size}} {{page}}) := by + decide +kernel + + + """)); + var chain = string.Concat(Enumerable.Range(0, pages).Reverse() + .Select(page => $"(.succ {paged.Pages}.page{page} ")); + var composed = $"StrictBinding.claimsHold_of_pages (size := {size}) (count := {pages}) " + + $"{paged.Pages}.prepared\n (by decide +kernel) {chain}.zero{new string(')', pages)}"; + var once = paged.Once is { } whole ? $$""" + set_option debug.skipKernelTC true in + /-- {{whole.Docstring}} -/ + theorem {{whole.Theorem}} : {{whole.Proposition}} := by + decide +kernel + + + """ : ""; + var proof = paged.Once is { } conjoined + ? $"And.intro {paged.Pages}.{conjoined.Theorem}\n " + + $"({composed.Replace("\n", "\n ", StringComparison.Ordinal)})" + : composed; + return $$""" + import BindingAudit.Images + import Extracted + import StrictBinding.Evidence + + /-! + # {{paged.Title}} + + {{paged.Summary}}, proved page by page. The preparation and each page lemma are kernel-checked + when this module is replayed. + -/ + + namespace BindingAudit + namespace {{paged.Pages}} + + set_option debug.skipKernelTC true in + /-- The pinned images decode and spell the identities they are pinned under. -/ + theorem prepared : StrictBinding.ok (StrictBinding.prepare images) = true := by decide +kernel + + {{once}}{{lemmas}}end {{paged.Pages}} + + /-- {{paged.Statement}} -/ + theorem {{paged.Theorem}} : {{paged.Proposition}} images Extracted.methodEvidence := + {{proof}} + + end BindingAudit + """; + } + + /// A theorem the audit expects in a module, with the type the driver derives from the plan. + sealed record BindingRequest(string Module, AuditRequest Audit, BindingSubject? Subject = null, + string? CoveragePredicate = null); + + /// Generator-owned request and module subjects, indexed before any generated code runs. + sealed class BindingTargets(BindingRequest[] requests) + { + internal BindingRequest[] Requests { get; } = requests; + internal Dictionary Theorems { get; } = requests.ToDictionary( + request => request.Audit.name, request => request.Subject, StringComparer.Ordinal); + internal Dictionary Modules { get; } = requests.ToDictionary( + request => request.Module, request => request.Subject, StringComparer.Ordinal); + } + + /// The audited theorems with expected types built from the plan, never from module text. + static BindingRequest[] BindingRequests(BindingPlan plan) + { + static object Const(string name, params int[] levels) => + levels.Length == 0 ? new { @const = name } : new { @const = name, levels }; + static object App(params object[] parts) => new { app = parts }; + static BindingRequest Coverage(string module, string theorem, string predicate, BindingFact fact, + params string[] definitions) + { + object[] parts = [Const("StrictBinding." + predicate), Const("BindingAudit.images"), + .. definitions.Select(name => Const("Extracted." + name)), "_"]; + return new(module, new(theorem, App(parts), ApprovedAxioms, term: true), new BindingSubject.Fact(fact), + predicate); + } + static JsonElement Nat(Sha256Digest digest) + { + Span bytes = stackalloc byte[Sha256Digest.Length]; + digest.CopyTo(bytes); + var value = new BigInteger(bytes, isUnsigned: true, isBigEndian: true); + return JsonDocument.Parse("{\"nat\":" + value.ToString(CultureInfo.InvariantCulture) + "}").RootElement; + } + var pinned = Const("StrictBinding.PinnedImage"); + var images = new + { + list = new + { + type = pinned, + level = 0, + items = plan.Images.Select(image => + App(Const("StrictBinding.PinnedImage.mk"), new { str = image.Identity }, + Const(image.Module + ".image"))).ToArray() + } + }; + var imagesEq = App(Const("Eq", 1), App(Const("List", 0), pinned), Const("BindingAudit.images"), images); + List platform = []; + if (plan.ObjectFields is { } complete) + { + var statement = App(Const("StrictBinding." + ObjectProposition(complete)), Const("BindingAudit.images"), + Const("Extracted.methodEvidence")); + platform.Add(new(ObjectFieldsModule, new("BindingAudit.objectFields", statement, ApprovedAxioms), + new BindingSubject.Fact(BindingFact.ObjectFields, complete))); + } + if (plan.TupleFields is { } pairComplete) + { + var statement = App(Const("StrictBinding." + TupleProposition(pairComplete)), Const("BindingAudit.images"), + Const("Extracted.methodEvidence"), Const("Extracted.rawLayouts")); + platform.Add(new(TupleFieldsModule, new("BindingAudit.tupleFields", statement, ApprovedAxioms), + new BindingSubject.Fact(BindingFact.ValueTupleFieldOrder, pairComplete))); + } + if (plan.PublicKeys is { } keysComplete) + { + var statement = App(Const("StrictBinding." + KeyProposition(keysComplete)), Const("BindingAudit.images"), + Const("Extracted.assemblies"), Const("Extracted.methodEvidence")); + platform.Add(new(PublicKeysModule, new("BindingAudit.publicKeyTokens", statement, ApprovedAxioms), + new BindingSubject.Fact(BindingFact.PublicKeyTokens, keysComplete))); + } + if (plan.Imports is { } publications) + { + object Publication(BindingPublication input) => App([Const("StrictBinding.GenericImports.Publication.mk"), + .. input.Fields.Select(field => (object)new { str = field }), Const(input.Sidecar.Module + ".image")]); + var expected = new { list = new { type = Const("StrictBinding.GenericImports.Publication"), level = 0, + items = publications.Select(Publication).ToArray() } }; + var statement = App(Const("StrictBinding.GenericImportsChecked"), Const("BindingAudit.images"), + Const("Extracted.profile"), Const("Extracted.methodEvidence"), Const("Extracted.imports"), expected, "_"); + platform.Add(new(ImportsModule, new(ImportsTheorem, statement, ApprovedAxioms, term: true), + new BindingSubject.Fact(BindingFact.ImportIdentities), "GenericImportsChecked")); + } + return + [ + new("Extracted", ExtractionCertificate("Extracted.translation_valid")), + new(ImagesModule, new("BindingAudit.images_eq", imagesEq, ApprovedAxioms)), + Coverage(LayoutContentModule, LayoutContentTheorem, "LayoutContentChecked", BindingFact.LayoutContent, + "rawLayouts", "imports"), + Coverage(DispatchCatalogsModule, DispatchCatalogsTheorem, "DispatchCatalogsChecked", + BindingFact.DispatchCatalogs, "assemblies", "methodEvidence", "imports"), + Coverage(GenericConstraintsModule, GenericConstraintsTheorem, "GenericConstraintsChecked", + BindingFact.GenericConstraints, "methodEvidence", "rawLayouts", "imports"), + Coverage(PlatformKindsModule, PlatformKindsTheorem, "PlatformKindsChecked", BindingFact.PlatformTypeKinds, + "platformDefinitions", "methodEvidence"), + Coverage(EnumStorageModule, EnumStorageTheorem, "EnumStorageChecked", BindingFact.EnumUnderlyingTypes, + "platformDefinitions", "methodEvidence"), + .. PagedFacts.Select(fact => (Fact: fact, Paged: PagedFact(fact))).Select(item => new BindingRequest( + item.Paged.Module, new("BindingAudit." + item.Paged.Theorem, App(Const(item.Paged.Proposition), + Const("BindingAudit.images"), Const("Extracted.methodEvidence")), ApprovedAxioms), + new BindingSubject.Fact(item.Fact))), + new(StaticDataModule, new("BindingAudit.staticData", App(Const("StrictBinding.StaticDataBound"), + Const("BindingAudit.images"), Const("Extracted.methodEvidence")), ApprovedAxioms), + new BindingSubject.Fact(BindingFact.StaticData)), + .. platform, + .. plan.Files.Select(image => new BindingRequest(image.Module, + new(image.Theorem, + App(Const("StrictBinding.DigestIs"), Const(image.Module + ".image"), Nat(image.Digest)), + ApprovedAxioms), image is BindingImagePin ? new BindingSubject.Image(image.Digest) : null)) + ]; + } + + /// + /// Builds the binding modules against the selected extraction and records failures by module. + /// A module whose prerequisite failed is recorded as skipped. + /// + Dictionary BuildBindingModules(BindingPlan plan, string leanPath) + { + string? Build(string module) + { + var source = ModuleSource(plan.Output, module); + var olean = Path.ChangeExtension(source, ".olean"); + var result = Execute([LeanExecutable, "--root=" + plan.Output, "-o", olean, source], leanPath: leanPath, + progress: "Checking " + module, environment: LeanEnvironment); + return result.Code == 0 ? null : BindingDiagnostic(result.Output); + } + Dictionary failures = new(StringComparer.Ordinal); + foreach (var image in plan.Files) + if (Build(image.Module) is { } error) failures[image.Module] = "build: " + error; + if (plan.Files.Any(image => failures.ContainsKey(image.Module))) + failures[ImagesModule] = "build: an image module did not build"; + else if (Build(ImagesModule) is { } imagesError) failures[ImagesModule] = "build: " + imagesError; + var facts = PagedFacts.Select(fact => PagedFact(fact).Module) + .Append(StaticDataModule).Append(EnumStorageModule).Append(PlatformKindsModule) + .Append(GenericConstraintsModule).Append(LayoutContentModule).Append(DispatchCatalogsModule); + if (plan.Imports is not null) facts = facts.Append(ImportsModule); + if (plan.ObjectFields.HasValue) facts = facts.Append(ObjectFieldsModule); + if (plan.TupleFields.HasValue) facts = facts.Append(TupleFieldsModule); + if (plan.PublicKeys.HasValue) facts = facts.Append(PublicKeysModule); + foreach (var module in facts) + if (failures.ContainsKey(ImagesModule)) failures[module] = "build: " + ImagesModule + " did not build"; + else if (Build(module) is { } pagedError) failures[module] = "build: " + pagedError; + return failures; + } + + static string BindingSourcesKey(string root) => ContentKey(string.Join("\n", Directory + .EnumerateFiles(Path.Combine(root, "StrictBinding"), "*.lean", SearchOption.AllDirectories) + .Append(Path.Combine(root, "StrictBinding.lean")) + .Select(path => Path.GetRelativePath(root, path).Replace('\\', '/') + "=" + Sha(path)) + .Order(StringComparer.Ordinal))); + + /// The first error of a Lean message, flattened and shortened for a report line. + static string BindingDiagnostic(string output) + { + var lines = output.Split('\n').Select(line => line.Trim()).Where(line => line.Length > 0).ToArray(); + var first = Math.Max(0, + Array.FindIndex(lines, line => line.Contains("error", StringComparison.OrdinalIgnoreCase) || + line.Contains("while replaying declaration", StringComparison.Ordinal))); + var text = string.Join(" ", lines.Skip(first).Take(3)); + return text.Length > 300 ? text[..300] + "..." : text; + } +} diff --git a/scripts/Documentation.cs b/scripts/Documentation.cs index f6e39bd5f..809599044 100644 --- a/scripts/Documentation.cs +++ b/scripts/Documentation.cs @@ -1,3 +1,5 @@ +using System.Buffers; +using System.Globalization; using System.Text; using System.Text.RegularExpressions; @@ -89,6 +91,61 @@ internal static string[] Documents(string root) => internal static string Relative(string root, string path) => Path.GetRelativePath(root, path).Replace('\\', '/'); + /// Extensions of the tracked files that are text. + static readonly HashSet TextExtensions = new(StringComparer.OrdinalIgnoreCase) + { ".lean", ".cs", ".md", ".toml", ".json", ".yml", ".txt", ".tsv", ".csv" }; + + /// The bytes below 0x20 that text may not hold: all but tab, line feed and carriage return. + static readonly SearchValues ControlBytes = SearchValues.Create( + [.. Enumerable.Range(0, 0x20).Where(value => value is not (9 or 10 or 13)).Select(value => (byte)value)]); + + /// + /// Whether a character above ASCII shows nothing, shows as a space or reorders the text around it: a + /// format character such as a zero-width space or a bidirectional override, a line or paragraph + /// separator, a space other than U+0020, or a control character. + /// + static bool IsInvisible(Rune rune) => Rune.GetUnicodeCategory(rune) is UnicodeCategory.Format + or UnicodeCategory.LineSeparator or UnicodeCategory.ParagraphSeparator or UnicodeCategory.SpaceSeparator + or UnicodeCategory.Control; + + /// + /// One problem per control byte and per invisible character in the text files among + /// (paths relative to the root), each read once. A shell that expands a backtick + /// escape writes a control byte into a docstring, and git then treats the file as binary. A reader of a + /// diff cannot see an invisible character, and a bidirectional override shows code in another order than + /// the compiler reads it; source spells such a character as an escape. + /// + internal static List ControlCharacterProblems(string root, IEnumerable tracked) + { + var problems = new List(); + foreach (var relative in tracked.Where(path => TextExtensions.Contains(Path.GetExtension(path)))) + { + var path = Path.Combine(root, relative); + if (!File.Exists(path)) continue; + var bytes = File.ReadAllBytes(path); + var (line, offset) = (1, 0); + while (bytes.AsSpan(offset).IndexOfAny(ControlBytes) is var found and >= 0) + { + line += bytes.AsSpan(offset, found).Count((byte)'\n'); + offset += found; + problems.Add( + $"{relative}:{line}: control byte 0x{bytes[offset]:X2}; text holds only tab and line ends"); + offset++; + } + (line, offset) = (1, 0); + while (bytes.AsSpan(offset).IndexOfAnyInRange((byte)0x80, (byte)0xFF) is var found and >= 0) + { + line += bytes.AsSpan(offset, found).Count((byte)'\n'); + offset += found; + Rune.DecodeFromUtf8(bytes.AsSpan(offset), out var rune, out var consumed); + if (IsInvisible(rune)) + problems.Add($"{relative}:{line}: invisible character U+{rune.Value:X4}; write it as an escape"); + offset += consumed; + } + } + return problems; + } + /// /// Whether a path under the root names an entry with exactly this spelling, as a case-sensitive /// file system on CI resolves it; Windows would also match other cases and trailing dots. @@ -136,14 +193,14 @@ internal static DocumentPage Read(string root, string path) string? audience = null; for (var index = 0; index < lines.Length; index++) { - if (Patterns.Audience().Match(lines[index]) is { Success: true } declared) + if (Patterns.Audience.Match(lines[index]) is { Success: true } declared) { if (audience is not null) problems.Add($"{name}:{index + 1}: a document declares one audience"); audience = declared.Groups["value"].Value; if (audience is not (Users or Maintainers)) problems.Add($"{name}:{index + 1}: audience must be `{Users}` or `{Maintainers}`"); } - var open = Patterns.Fence().Match(lines[index]); + var open = Patterns.Fence.Match(lines[index]); if (!open.Success) { prose.Add(new(name, index + 1, lines[index])); @@ -236,9 +293,9 @@ HashSet AnchorsOf(string path) => foreach (var line in page.Prose) { var at = $"{line.Document}:{line.Line}"; - foreach (Match match in Patterns.LineCitation().Matches(line.Text)) + foreach (Match match in Patterns.LineCitation.Matches(line.Text)) failures.Add($"{at}: `{match.Value}` cites a source line; cite a declaration name"); - foreach (Match match in Patterns.AnyLink().Matches(line.Text)) + foreach (Match match in Patterns.AnyLink.Matches(line.Text)) { references++; var target = match.Groups["target"].Value; @@ -256,7 +313,7 @@ HashSet AnchorsOf(string path) => if (hash >= 0 && linked.EndsWith(".md", StringComparison.Ordinal) && File.Exists(linked) && !AnchorsOf(linked).Contains(target[(hash + 1)..])) failures.Add($"{at}: `{target}` names no heading of {relative}"); - if (Patterns.NamedLink().Match(match.Value) is { Success: true } named && + if (Patterns.NamedLink.Match(match.Value) is { Success: true } named && Path.GetExtension(linked) is ".lean" or ".cs" && File.Exists(linked)) { var label = named.Groups["label"].Value; @@ -266,19 +323,19 @@ HashSet AnchorsOf(string path) => failures.Add($"{at}: `{label}` -> {relative}: {problem}"); } } - foreach (Match match in Patterns.CodeSpan().Matches(line.Text)) + foreach (Match match in Patterns.CodeSpan.Matches(line.Text)) { var span = match.Groups["span"].Value; - if (Patterns.RepositoryPath().IsMatch(span)) + if (Patterns.RepositoryPath.IsMatch(span)) { references++; var path = span.TrimEnd('/'); if (!ExistsExactly(root, Join(root, path))) failures.Add($"{at}: `{span}` does not exist"); } - if (userFacing && (Unshipped(span) || Patterns.TestModule().IsMatch(span))) + if (userFacing && (Unshipped(span) || Patterns.TestModule.IsMatch(span))) failures.Add($"{at}: `{span}`: {UnshippedReason}"); - if (Patterns.CommandSpan().Match(span) is { Success: true } command) + if (Patterns.CommandSpan.Match(span) is { Success: true } command) { references++; if (!commands.Contains(command.Value)) @@ -388,8 +445,8 @@ static IEnumerable CheckWalkthrough(string root, DocumentPage page) /// internal static string[] NormalizedOutput(IEnumerable lines) { - var normalized = lines.Select(line => Patterns.SourcePosition().Replace( - Patterns.Sha256().Replace(line.TrimEnd(), ""), "${file}::")).ToList(); + var normalized = lines.Select(line => Patterns.SourcePosition.Replace( + Patterns.Sha256.Replace(line.TrimEnd(), ""), "${file}::")).ToList(); while (normalized.Count > 0 && normalized[^1].Length == 0) normalized.RemoveAt(normalized.Count - 1); return [.. normalized]; } @@ -516,9 +573,9 @@ internal static List CheckSuiteTable(string root, DocumentPage page, IRe if (heading < 0) return [$"{document}: no `{SuiteHeading}` table lists the suites `scripts/Test.cs` accepts"]; HashSet listed = new(StringComparer.Ordinal); - foreach (var line in page.Prose.Skip(heading + 1).TakeWhile(line => !Patterns.Heading().IsMatch(line.Text))) + foreach (var line in page.Prose.Skip(heading + 1).TakeWhile(line => !Patterns.Heading.IsMatch(line.Text))) { - if (Patterns.SuiteRow().Match(line.Text) is not { Success: true } row) continue; + if (Patterns.SuiteRow.Match(line.Text) is not { Success: true } row) continue; var name = row.Groups["name"].Value; if (!listed.Add(name)) failures.Add($"{line.Document}:{line.Line}: suite `{name}` is listed twice"); @@ -548,7 +605,9 @@ internal static List CheckSuiteTable(string root, DocumentPage page, IRe [ "pureArity", "scalars", "instructionStep", "staticInstruction", "checkedIntrinsic", "checkedValue", "numericValue", "checkedAt", "instruction", "memoryInstruction", - "otherMemoryInstruction", "observerCount", "unspecifiedOperands" + "otherMemoryInstruction", "observerCount", "unspecifiedOperands", "runModel", "observedCall", + "intrinsicCall", "featureCall", "referenceCompareCall", "referenceSubtractCall", "typedTransferCall", + "skipInitCall" ]; /// Keep the consumer fixture on the umbrella and away from evaluator implementation names. @@ -564,7 +623,7 @@ internal static IEnumerable PublicProofProblems(string document, string[ if (line.Trim() != "import CIL") yield return $"{document}:{index + 1}: public proof fixtures must import only `CIL`"; } - foreach (var name in Patterns.SimpName().Matches(line).Select(match => match.Value).Distinct()) + foreach (var name in Patterns.SimpName.Matches(line).Select(match => match.Value).Distinct()) if (ProofImplementationNames.Contains(name.Split('.')[^1])) yield return $"{document}:{index + 1}: `{name}` is an implementation helper, " + "not part of the public proof surface"; @@ -575,18 +634,22 @@ internal static IEnumerable PublicProofProblems(string document, string[ /// /// The interpreter's own functions, by full name: the instruction dispatchers, the run and invocation - /// loops, and the checks of arguments and results. The stepping tactics rewrite past them, so a proof - /// whose simp list names one runs the method by unfolding the interpreter and breaks when it changes. + /// loops with their equations, and the checks of arguments and results. A proof whose simp list names one + /// runs the method by unfolding the interpreter and breaks when it changes. /// static readonly string[] EvaluatorInternals = [ "CIL.step", "CIL.Safety.step", "CIL.Safety.run", "CIL.Safety.invoke", "CIL.Safety.pureArity", + "CIL.Safety.runWith", "CIL.Safety.invokeWith", "CIL.Safety.run_zero", "CIL.Safety.run_succ", + "CIL.Safety.invoke_eq", "CIL.Safety.scalars", "CIL.Safety.instructionStep", "CIL.Safety.staticInstruction", "CIL.Safety.checkedIntrinsic", "CIL.Safety.checkedValue", "CIL.Safety.numericValue", "CIL.Safety.checkedAt", "CIL.Safety.formValue", "CIL.Safety.storeValue", "CIL.Safety.instruction", "CIL.Safety.memoryInstruction", "CIL.Safety.otherMemoryInstruction", - "CIL.Safety.observerCount", "CIL.Safety.unspecifiedOperands", - "CIL.Value.hasUnspecified", "CIL.Safety.Value.hasUnspecified", + "CIL.Safety.observerCount", "CIL.Safety.unspecifiedOperands", "CIL.Safety.runModel", + "CIL.Safety.observedCall", "CIL.Safety.intrinsicCall", "CIL.Safety.featureCall", + "CIL.Safety.referenceCompareCall", "CIL.Safety.referenceSubtractCall", "CIL.Safety.typedTransferCall", + "CIL.Safety.skipInitCall", "CIL.Value.hasUnspecified", "CIL.Safety.Value.hasUnspecified", "Except.mapError", "Bind.bind", "Except.bind", "Pure.pure", "Except.pure" ]; @@ -600,8 +663,8 @@ internal static IEnumerable PublicProofProblems(string document, string[ internal static IEnumerable SimpListProblems(string document, int firstLine, string[] lines) { var text = string.Join('\n', lines.Select(StripComment)); - var bound = Patterns.Have().Matches(text).Select(match => match.Groups["name"].Value).ToHashSet(); - foreach (Match call in Patterns.SimpList().Matches(text)) + var bound = Patterns.Have.Matches(text).Select(match => match.Groups["name"].Value).ToHashSet(); + foreach (Match call in Patterns.SimpList.Matches(text)) { var start = call.Index + call.Length; var depth = 1; @@ -609,7 +672,7 @@ internal static IEnumerable SimpListProblems(string document, int firstL for (; end < text.Length && depth > 0; end++) depth += text[end] switch { '[' => 1, ']' => -1, _ => 0 }; var line = firstLine + text[..call.Index].Count(character => character == '\n'); - foreach (var name in Patterns.SimpName().Matches(text[start..Math.Max(start, end - 1)]) + foreach (var name in Patterns.SimpName.Matches(text[start..Math.Max(start, end - 1)]) .Select(match => match.Value).Distinct()) if (!bound.Contains(name) && EvaluatorInternals.Any(full => full == name || full.EndsWith("." + name, StringComparison.Ordinal))) @@ -634,10 +697,10 @@ internal static List CheckExamples(string root) var lines = File.ReadAllLines(path); failures.AddRange(SimpListProblems(document, 1, lines)); if (document == PublicProofFixture) failures.AddRange(PublicProofProblems(document, lines)); - var printed = lines.Select(line => Patterns.PrintAxioms().Match(line)).Where(match => match.Success) + var printed = lines.Select(line => Patterns.PrintAxioms.Match(line)).Where(match => match.Success) .Select(match => match.Groups["name"].Value.Split('.')[^1]).ToHashSet(StringComparer.Ordinal); for (var index = 0; index < lines.Length; index++) - if (Patterns.Theorem().Match(lines[index]) is { Success: true } theorem && + if (Patterns.Theorem.Match(lines[index]) is { Success: true } theorem && !printed.Contains(theorem.Groups["name"].Value.Split('.')[^1])) failures.Add($"{document}:{index + 1}: theorem `{theorem.Groups["name"].Value}` has no " + "`#print axioms` line; print its axioms after it"); @@ -656,10 +719,10 @@ static HashSet Anchors(string path) var anchors = new HashSet(StringComparer.Ordinal); foreach (var line in File.ReadLines(path)) { - if (Patterns.ExplicitAnchor().Match(line) is { Success: true } named) + if (Patterns.ExplicitAnchor.Match(line) is { Success: true } named) anchors.Add(named.Groups["name"].Value); - if (Patterns.Heading().Match(line) is not { Success: true } heading) continue; - var slug = Patterns.AnchorPunctuation().Replace(heading.Groups["text"].Value.Trim().ToLowerInvariant(), "") + if (Patterns.Heading.Match(line) is not { Success: true } heading) continue; + var slug = Patterns.AnchorPunctuation.Replace(heading.Groups["text"].Value.Trim().ToLowerInvariant(), "") .Replace(' ', '-'); var unique = slug; for (var repeat = 1; !anchors.Add(unique); repeat++) unique = $"{slug}-{repeat}"; @@ -687,7 +750,7 @@ static IEnumerable CheckBlock(string root, LeanBlock block, bool userFac var at = $"{block.Document}:{block.Line}"; for (var offset = 0; offset < block.Lines.Length; offset++) { - if (Patterns.Import().Match(block.Lines[offset]) is { Success: true } import && + if (Patterns.Import.Match(block.Lines[offset]) is { Success: true } import && ImportProblem(root, import.Groups["module"].Value, block.Kind) is { } problem) yield return $"{block.Document}:{block.Line + offset}: {problem}"; if (block.Lines[offset].Length > LeanLineLimit) @@ -696,7 +759,7 @@ static IEnumerable CheckBlock(string root, LeanBlock block, bool userFac } switch (block.Kind) { - case SnippetKind.Checked when block.Lines.Any(line => Patterns.Extraction().IsMatch(StripComment(line))): + case SnippetKind.Checked when block.Lines.Any(line => Patterns.Extraction.IsMatch(StripComment(line))): yield return $"{at}: the block needs an extraction; mark it `excerpt ` or `extracted`"; break; case SnippetKind.Excerpt: @@ -704,7 +767,7 @@ static IEnumerable CheckBlock(string root, LeanBlock block, bool userFac yield break; } for (var offset = 0; offset < block.Lines.Length; offset++) - foreach (Match command in Patterns.Command().Matches(StripComment(block.Lines[offset]))) + foreach (Match command in Patterns.Command.Matches(StripComment(block.Lines[offset]))) if (!commands.Contains(command.Value)) yield return $"{block.Document}:{block.Line + offset}: `{command.Value}` {NotCommand}"; foreach (var problem in SimpListProblems(block.Document, block.Line, block.Lines)) yield return problem; @@ -775,7 +838,7 @@ internal static HashSet Commands(string root) var names = new HashSet(StringComparer.Ordinal); if (!Directory.Exists(library)) return names; foreach (var file in Directory.EnumerateFiles(library, "*.lean", SearchOption.AllDirectories)) - foreach (Match match in Patterns.DeclaredCommand().Matches(File.ReadAllText(file))) + foreach (Match match in Patterns.DeclaredCommand.Matches(File.ReadAllText(file))) names.Add(match.Groups["name"].Value); return names; } @@ -787,7 +850,7 @@ internal static HashSet Commands(string root) internal static SnippetBatch[] Snippets(IEnumerable pages) { static (string Module, int Offset)[] Imports(LeanBlock block) => block.Lines - .Select((line, offset) => (Patterns.Import().Match(line).Groups["module"].Value, offset)) + .Select((line, offset) => (Patterns.Import.Match(line).Groups["module"].Value, offset)) .Where(import => import.Item1 != "").DistinctBy(import => import.Item1).ToArray(); var batches = new List(); var checkedBlocks = pages.SelectMany(page => page.Blocks).Where(block => block.Kind == SnippetKind.Checked); @@ -803,7 +866,7 @@ internal static SnippetBatch[] Snippets(IEnumerable pages) { batch.Emit($"namespace DocSnippet{++batch.Count}"); for (var offset = 0; offset < block.Lines.Length; offset++) - batch.Emit(Patterns.Import().IsMatch(block.Lines[offset]) ? "" : block.Lines[offset], + batch.Emit(Patterns.Import.IsMatch(block.Lines[offset]) ? "" : block.Lines[offset], new(block.Document, block.Line + offset)); batch.Emit($"end DocSnippet{batch.Count}"); } @@ -878,7 +941,7 @@ bool IsModule(string name) => File.Exists(Path.Combine(root, name)) || IEnumerable<(string Text, bool Prose, SnippetOrigin Origin)> Texts(DocumentPage page) { foreach (var line in page.Prose) - foreach (Match span in Patterns.CodeSpan().Matches(line.Text)) + foreach (Match span in Patterns.CodeSpan.Matches(line.Text)) yield return (span.Groups["span"].Value, true, new(line.Document, line.Line)); foreach (var block in page.Blocks.Where(block => block.Kind is SnippetKind.Extracted or SnippetKind.Illustrative)) @@ -889,7 +952,7 @@ bool IsModule(string name) => File.Exists(Path.Combine(root, name)) || foreach (var page in pages) foreach (var (text, prose, origin) in Texts(page)) { - foreach (Match match in Patterns.LibraryName().Matches(text)) + foreach (Match match in Patterns.LibraryName.Matches(text)) if (!IsModule(match.Value) && seen.Add((match.Value, origin.Document, origin.Line))) yield return (CitedKind.Rooted, match.Value, origin); if (!prose || Unrooted(text) is not { } kind || documented.Contains(text) || @@ -905,7 +968,7 @@ static HashSet DeclaredNames(IEnumerable sources) { var names = new HashSet(StringComparer.Ordinal); foreach (var source in sources) - foreach (var match in Patterns.LeanDeclaration().Matches(source).Concat(Patterns.Have().Matches(source))) + foreach (var match in Patterns.LeanDeclaration.Matches(source).Concat(Patterns.Have.Matches(source))) { var name = match.Groups["name"].Value; names.Add(name); @@ -923,10 +986,10 @@ static HashSet DeclaredNames(IEnumerable sources) static CitedKind? Unrooted(string span) { - if (Patterns.CommandSpan().Match(span) is { Success: true } command && command.Length == span.Length) + if (Patterns.CommandSpan.Match(span) is { Success: true } command && command.Length == span.Length) return CitedKind.Keyword; - if (Patterns.SnakeName().IsMatch(span)) return CitedKind.Library; - if (!Patterns.CapitalDottedName().IsMatch(span) || Patterns.LibraryName().IsMatch(span)) return null; + if (Patterns.SnakeName.IsMatch(span)) return CitedKind.Library; + if (!Patterns.CapitalDottedName.IsMatch(span) || Patterns.LibraryName.IsMatch(span)) return null; var parts = span.Split('.'); return ForeignRoots.Contains(parts[0]) || FileExtensions.Contains(parts[^1]) ? null : CitedKind.Library; } @@ -984,7 +1047,7 @@ internal static List Diagnostics(SnippetBatch batch, string batchPath, s var lines = output.ReplaceLineEndings("\n").Split('\n'); for (var index = 0; index < lines.Length; index++) { - var match = Patterns.Diagnostic().Match(lines[index]); + var match = Patterns.Diagnostic.Match(lines[index]); if (!match.Success) { if (lines[index].Contains("sorryAx", StringComparison.Ordinal)) @@ -993,7 +1056,7 @@ internal static List Diagnostics(SnippetBatch batch, string batchPath, s } if (match.Groups["severity"].Value == "info") continue; var message = new StringBuilder(match.Groups["message"].Value); - while (index + 1 < lines.Length && !Patterns.Diagnostic().IsMatch(lines[index + 1]) && + while (index + 1 < lines.Length && !Patterns.Diagnostic.IsMatch(lines[index + 1]) && lines[index + 1].Length != 0) message.Append("\n ").Append(lines[++index]); var line = int.Parse(match.Groups["line"].Value) - 1; @@ -1013,7 +1076,7 @@ internal static List Diagnostics(SnippetBatch batch, string batchPath, s if (label.Contains('/')) return target.Replace('\\', '/').EndsWith("/" + label, StringComparison.Ordinal) ? null : "a path label must name the linked file"; - if (!Patterns.DottedName().IsMatch(label)) + if (!Patterns.DottedName.IsMatch(label)) return "the label is neither a declaration name, a quoted string nor a path"; return target.EndsWith(".lean", StringComparison.Ordinal) ? LeanProblem(label, source) : CSharpProblem(label, source); @@ -1055,10 +1118,10 @@ internal static DeclarationSource Read(string path) { var text = File.ReadAllText(path); return new(text, - Names(Patterns.LeanDeclaration().Matches(text)), - Names(Patterns.LeanNamespace().Matches(text)), - Names(Patterns.CSharpType().Matches(text)), - Names(Patterns.CSharpMember().Matches(text).Where(declaration => + Names(Patterns.LeanDeclaration.Matches(text)), + Names(Patterns.LeanNamespace.Matches(text)), + Names(Patterns.CSharpType.Matches(text)), + Names(Patterns.CSharpMember.Matches(text).Where(declaration => !Statements.Contains(declaration.Groups["prefix"].Value.TrimEnd().Split(' ', '\t')[^1], StringComparer.Ordinal)))); } @@ -1070,110 +1133,110 @@ static HashSet Names(IEnumerable matches) => static partial class Patterns { [GeneratedRegex(@"^\s*$")] - internal static partial Regex Audience(); + internal static partial Regex Audience { get; } [GeneratedRegex(@"^(?[ \t]*)(?`{3,}|~{3,})(?[^`]*)$")] - internal static partial Regex Fence(); + internal static partial Regex Fence { get; } [GeneratedRegex(@"\[(?:[^\[\]]|\[[^\]]*\])*\]\((?[^)\s]+)\)")] - internal static partial Regex AnyLink(); + internal static partial Regex AnyLink { get; } [GeneratedRegex(@"^#{1,6}[ \t]+(?.*?)[ \t#]*$")] - internal static partial Regex Heading(); + internal static partial Regex Heading { get; } [GeneratedRegex(@"^\|\s*`(?[^`]+)`\s*\|")] - internal static partial Regex SuiteRow(); + internal static partial Regex SuiteRow { get; } [GeneratedRegex(@"[^\w\- ]")] - internal static partial Regex AnchorPunctuation(); + internal static partial Regex AnchorPunctuation { get; } [GeneratedRegex(@"[^""]+)""")] - internal static partial Regex ExplicitAnchor(); + internal static partial Regex ExplicitAnchor { get; } [GeneratedRegex(@"\A\[`(?